rpcrt4: Retrieve the maximum token length from the security provider rather than...
[wine] / dlls / quartz / filtermapper.c
1 /*
2  * IFilterMapper & IFilterMapper2 Implementations
3  *
4  * Copyright 2003 Robert Shearman
5  * Copyright 2004 Christian Costa
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #define NONAMELESSUNION
23 #define NONAMELESSSTRUCT
24 #include <stdarg.h>
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "winreg.h"
30 #include "winerror.h"
31
32 #include "quartz_private.h"
33
34 #include "ole2.h"
35 #include "olectl.h"
36 #include "strmif.h"
37 #include "wine/unicode.h"
38 #include "uuids.h"
39
40 #include "wine/debug.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
43
44 typedef struct FilterMapper2Impl
45 {
46     const IFilterMapper2Vtbl *lpVtbl;
47     const IFilterMapperVtbl  *lpVtblFilterMapper;
48     LONG refCount;
49 } FilterMapper2Impl;
50
51 static const IFilterMapper2Vtbl fm2vtbl;
52 static const IFilterMapperVtbl fmvtbl;
53
54 static inline FilterMapper2Impl *impl_from_IFilterMapper( IFilterMapper *iface )
55 {
56     return (FilterMapper2Impl *)((char*)iface - FIELD_OFFSET(FilterMapper2Impl, lpVtblFilterMapper));
57 }
58
59 static const WCHAR wszClsidSlash[] = {'C','L','S','I','D','\\',0};
60 static const WCHAR wszSlashInstance[] = {'\\','I','n','s','t','a','n','c','e','\\',0};
61 static const WCHAR wszSlash[] = {'\\',0};
62
63 /* CLSID property in media category Moniker */
64 static const WCHAR wszClsidName[] = {'C','L','S','I','D',0};
65 /* FriendlyName property in media category Moniker */
66 static const WCHAR wszFriendlyName[] = {'F','r','i','e','n','d','l','y','N','a','m','e',0};
67 /* Merit property in media category Moniker (CLSID_ActiveMovieCategories only) */
68 static const WCHAR wszMeritName[] = {'M','e','r','i','t',0};
69 /* FilterData property in media category Moniker (not CLSID_ActiveMovieCategories) */
70 static const WCHAR wszFilterDataName[] = {'F','i','l','t','e','r','D','a','t','a',0};
71 /* For filters registered with IFilterMapper */
72 static const WCHAR wszFilterSlash[] = {'F','i','l','t','e','r','\\',0};
73 static const WCHAR wszFilter[] = {'F','i','l','t','e','r',0};
74 /* For pins registered with IFilterMapper */
75 static const WCHAR wszPins[] = {'P','i','n','s',0};
76 static const WCHAR wszAllowedMany[] = {'A','l','l','o','w','e','d','M','a','n','y',0};
77 static const WCHAR wszAllowedZero[] = {'A','l','l','o','w','e','d','Z','e','r','o',0};
78 static const WCHAR wszDirection[] = {'D','i','r','e','c','t','i','o','n',0};
79 static const WCHAR wszIsRendered[] = {'I','s','R','e','n','d','e','r','e','d',0};
80 /* For types registered with IFilterMapper */
81 static const WCHAR wszTypes[] = {'T','y','p','e','s',0};
82
83
84 /* registry format for REGFILTER2 */
85 struct REG_RF
86 {
87     DWORD dwVersion;
88     DWORD dwMerit;
89     DWORD dwPins;
90     DWORD dwUnused;
91 };
92
93 struct REG_RFP
94 {
95     BYTE signature[4]; /* e.g. "0pi3" */
96     DWORD dwFlags;
97     DWORD dwInstances;
98     DWORD dwMediaTypes;
99     DWORD dwMediums;
100     DWORD bCategory; /* is there a category clsid? */
101     /* optional: dwOffsetCategoryClsid */
102 };
103
104 struct REG_TYPE
105 {
106     BYTE signature[4]; /* e.g. "0ty3" */
107     DWORD dwUnused;
108     DWORD dwOffsetMajor;
109     DWORD dwOffsetMinor;
110 };
111
112 struct MONIKER_MERIT
113 {
114     IMoniker * pMoniker;
115     DWORD dwMerit;
116 };
117
118 struct Vector
119 {
120     LPBYTE pData;
121     int capacity; /* in bytes */
122     int current; /* pointer to next free byte */
123 };
124
125 /* returns the position it was added at */
126 static int add_data(struct Vector * v, const BYTE * pData, int size)
127 {
128     int index = v->current;
129     if (v->current + size > v->capacity)
130     {
131         LPBYTE pOldData = v->pData;
132         v->capacity = (v->capacity + size) * 2;
133         v->pData = CoTaskMemAlloc(v->capacity);
134         memcpy(v->pData, pOldData, v->current);
135         CoTaskMemFree(pOldData);
136     }
137     memcpy(v->pData + v->current, pData, size);
138     v->current += size;
139     return index;
140 }
141
142 static int find_data(struct Vector * v, const BYTE * pData, int size)
143 {
144     int index;
145     for (index = 0; index < v->current; index++)
146         if (!memcmp(v->pData + index, pData, size))
147             return index;
148     /* not found */
149     return -1;
150 }
151
152 static void delete_vector(struct Vector * v)
153 {
154     CoTaskMemFree(v->pData);
155     v->current = 0;
156     v->capacity = 0;
157 }
158
159 HRESULT FilterMapper2_create(IUnknown *pUnkOuter, LPVOID *ppObj)
160 {
161     FilterMapper2Impl * pFM2impl;
162
163     TRACE("(%p, %p)\n", pUnkOuter, ppObj);
164
165     if (pUnkOuter)
166         return CLASS_E_NOAGGREGATION;
167
168     pFM2impl = CoTaskMemAlloc(sizeof(*pFM2impl));
169     if (!pFM2impl)
170         return E_OUTOFMEMORY;
171
172     pFM2impl->lpVtbl = &fm2vtbl;
173     pFM2impl->lpVtblFilterMapper = &fmvtbl;
174     pFM2impl->refCount = 1;
175
176     *ppObj = pFM2impl;
177
178     TRACE("-- created at %p\n", pFM2impl);
179
180     return S_OK;
181 }
182
183 HRESULT FilterMapper_create(IUnknown *pUnkOuter, LPVOID *ppObj)
184 {
185     FilterMapper2Impl *pFM2impl;
186     HRESULT hr;
187
188     TRACE("(%p, %p)\n", pUnkOuter, ppObj);
189
190     hr = FilterMapper2_create(pUnkOuter, (LPVOID*)&pFM2impl);
191     if (FAILED(hr))
192         return hr;
193
194     *ppObj = &pFM2impl->lpVtblFilterMapper;
195
196     return hr;
197 }
198
199 /*** IUnknown methods ***/
200
201 static HRESULT WINAPI FilterMapper2_QueryInterface(IFilterMapper2 * iface, REFIID riid, LPVOID *ppv)
202 {
203     FilterMapper2Impl *This = (FilterMapper2Impl *)iface;
204
205     TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
206
207     *ppv = NULL;
208
209     if (IsEqualIID(riid, &IID_IUnknown))
210         *ppv = iface;
211     else if (IsEqualIID(riid, &IID_IFilterMapper2))
212         *ppv = iface;
213     else if (IsEqualIID(riid, &IID_IFilterMapper))
214         *ppv = &This->lpVtblFilterMapper;
215
216     if (*ppv != NULL)
217     {
218         IUnknown_AddRef((IUnknown *)*ppv);
219         return S_OK;
220     }
221
222     FIXME("No interface for %s\n", debugstr_guid(riid));
223     return E_NOINTERFACE;
224 }
225
226 static ULONG WINAPI FilterMapper2_AddRef(IFilterMapper2 * iface)
227 {
228     FilterMapper2Impl *This = (FilterMapper2Impl *)iface;
229     ULONG refCount = InterlockedIncrement(&This->refCount);
230
231     TRACE("(%p)->() AddRef from %d\n", This, refCount - 1);
232
233     return refCount;
234 }
235
236 static ULONG WINAPI FilterMapper2_Release(IFilterMapper2 * iface)
237 {
238     FilterMapper2Impl *This = (FilterMapper2Impl *)iface;
239     ULONG refCount = InterlockedDecrement(&This->refCount);
240
241     TRACE("(%p)->() Release from %d\n", This, refCount + 1);
242
243     if (refCount == 0)
244     {
245         CoTaskMemFree(This);
246         return 0;
247     }
248     return refCount;
249 }
250
251 /*** IFilterMapper2 methods ***/
252
253 static HRESULT WINAPI FilterMapper2_CreateCategory(
254     IFilterMapper2 * iface,
255     REFCLSID clsidCategory,
256     DWORD dwCategoryMerit,
257     LPCWSTR szDescription)
258 {
259     LPWSTR wClsidAMCat = NULL;
260     LPWSTR wClsidCategory = NULL;
261     WCHAR wszKeyName[strlenW(wszClsidSlash) + strlenW(wszSlashInstance) + (CHARS_IN_GUID-1) * 2 + 1];
262     HKEY hKey = NULL;
263     LONG lRet;
264     HRESULT hr;
265
266     TRACE("(%s, %x, %s)\n", debugstr_guid(clsidCategory), dwCategoryMerit, debugstr_w(szDescription));
267
268     hr = StringFromCLSID(&CLSID_ActiveMovieCategories, &wClsidAMCat);
269
270     if (SUCCEEDED(hr))
271     {
272         hr = StringFromCLSID(clsidCategory, &wClsidCategory);
273     }
274
275     if (SUCCEEDED(hr))
276     {
277         strcpyW(wszKeyName, wszClsidSlash);
278         strcatW(wszKeyName, wClsidAMCat);
279         strcatW(wszKeyName, wszSlashInstance);
280         strcatW(wszKeyName, wClsidCategory);
281
282         lRet = RegCreateKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
283         hr = HRESULT_FROM_WIN32(lRet);
284     }
285
286     if (SUCCEEDED(hr))
287     {
288         lRet = RegSetValueExW(hKey, wszFriendlyName, 0, REG_SZ, (const BYTE*)szDescription, (strlenW(szDescription) + 1) * sizeof(WCHAR));
289         hr = HRESULT_FROM_WIN32(lRet);
290     }
291
292     if (SUCCEEDED(hr))
293     {
294         lRet = RegSetValueExW(hKey, wszClsidName, 0, REG_SZ, (LPBYTE)wClsidCategory, (strlenW(wClsidCategory) + 1) * sizeof(WCHAR));
295         hr = HRESULT_FROM_WIN32(lRet);
296     }
297
298     if (SUCCEEDED(hr))
299     {
300         lRet = RegSetValueExW(hKey, wszMeritName, 0, REG_DWORD, (LPBYTE)&dwCategoryMerit, sizeof(dwCategoryMerit));
301         hr = HRESULT_FROM_WIN32(lRet);
302     }
303
304     CloseHandle(hKey);
305     CoTaskMemFree(wClsidCategory);
306     CoTaskMemFree(wClsidAMCat);
307
308     return hr;
309 }
310
311 static HRESULT WINAPI FilterMapper2_UnregisterFilter(
312     IFilterMapper2 * iface,
313     const CLSID *pclsidCategory,
314     const OLECHAR *szInstance,
315     REFCLSID Filter)
316 {
317     WCHAR wszKeyName[MAX_PATH];
318     LPWSTR wClsidCategory = NULL;
319     LPWSTR wFilter = NULL;
320     HRESULT hr;
321
322     TRACE("(%p, %s, %s)\n", pclsidCategory, debugstr_w(szInstance), debugstr_guid(Filter));
323
324     if (!pclsidCategory)
325         pclsidCategory = &CLSID_LegacyAmFilterCategory;
326
327     hr = StringFromCLSID(pclsidCategory, &wClsidCategory);
328
329     if (SUCCEEDED(hr))
330     {
331         strcpyW(wszKeyName, wszClsidSlash);
332         strcatW(wszKeyName, wClsidCategory);
333         strcatW(wszKeyName, wszSlashInstance);
334         if (szInstance)
335             strcatW(wszKeyName, szInstance);
336         else
337         {
338             hr = StringFromCLSID(Filter, &wFilter);
339             if (SUCCEEDED(hr))
340                 strcatW(wszKeyName, wFilter);
341         }
342     }
343
344     if (SUCCEEDED(hr))
345     {
346         LONG lRet = RegDeleteKeyW(HKEY_CLASSES_ROOT, wszKeyName);
347         hr = HRESULT_FROM_WIN32(lRet);
348     }
349
350     CoTaskMemFree(wClsidCategory);
351     CoTaskMemFree(wFilter);
352
353     return hr;
354 }
355
356 static HRESULT FM2_WriteFriendlyName(IPropertyBag * pPropBag, LPCWSTR szName)
357 {
358     VARIANT var;
359
360     V_VT(&var) = VT_BSTR;
361     V_UNION(&var, bstrVal) = (BSTR)szName;
362
363     return IPropertyBag_Write(pPropBag, wszFriendlyName, &var);
364 }
365
366 static HRESULT FM2_WriteClsid(IPropertyBag * pPropBag, REFCLSID clsid)
367 {
368     LPWSTR wszClsid = NULL;
369     VARIANT var;
370     HRESULT hr;
371
372     hr = StringFromCLSID(clsid, &wszClsid);
373
374     if (SUCCEEDED(hr))
375     {
376         V_VT(&var) = VT_BSTR;
377         V_UNION(&var, bstrVal) = wszClsid;
378         hr = IPropertyBag_Write(pPropBag, wszClsidName, &var);
379     }
380     CoTaskMemFree(wszClsid);
381     return hr;
382 }
383
384 static HRESULT FM2_WriteFilterData(IPropertyBag * pPropBag, const REGFILTER2 * prf2)
385 {
386     VARIANT var;
387     int size = sizeof(struct REG_RF);
388     unsigned int i;
389     struct Vector mainStore = {NULL, 0, 0};
390     struct Vector clsidStore = {NULL, 0, 0};
391     struct REG_RF rrf;
392     SAFEARRAY * psa;
393     SAFEARRAYBOUND saBound;
394     HRESULT hr = S_OK;
395
396     rrf.dwVersion = prf2->dwVersion;
397     rrf.dwMerit = prf2->dwMerit;
398     rrf.dwPins = prf2->u.s1.cPins2;
399     rrf.dwUnused = 0;
400
401     add_data(&mainStore, (LPBYTE)&rrf, sizeof(rrf));
402
403     for (i = 0; i < prf2->u.s1.cPins2; i++)
404     {
405         size += sizeof(struct REG_RFP);
406         if (prf2->u.s1.rgPins2[i].clsPinCategory)
407             size += sizeof(DWORD);
408         size += prf2->u.s1.rgPins2[i].nMediaTypes * sizeof(struct REG_TYPE);
409         size += prf2->u.s1.rgPins2[i].nMediums * sizeof(DWORD);
410     }
411
412     for (i = 0; i < prf2->u.s1.cPins2; i++)
413     {
414         struct REG_RFP rrfp;
415         REGFILTERPINS2 rgPin2 = prf2->u.s1.rgPins2[i];
416         unsigned int j;
417
418         rrfp.signature[0] = '0';
419         rrfp.signature[1] = 'p';
420         rrfp.signature[2] = 'i';
421         rrfp.signature[3] = '3';
422         rrfp.signature[0] += i;
423         rrfp.dwFlags = rgPin2.dwFlags;
424         rrfp.dwInstances = rgPin2.cInstances;
425         rrfp.dwMediaTypes = rgPin2.nMediaTypes;
426         rrfp.dwMediums = rgPin2.nMediums;
427         rrfp.bCategory = rgPin2.clsPinCategory ? 1 : 0;
428
429         add_data(&mainStore, (LPBYTE)&rrfp, sizeof(rrfp));
430         if (rrfp.bCategory)
431         {
432             DWORD index = find_data(&clsidStore, (const BYTE*)rgPin2.clsPinCategory, sizeof(CLSID));
433             if (index == -1)
434                 index = add_data(&clsidStore, (const BYTE*)rgPin2.clsPinCategory, sizeof(CLSID));
435             index += size;
436
437             add_data(&mainStore, (LPBYTE)&index, sizeof(index));
438         }
439
440         for (j = 0; j < rgPin2.nMediaTypes; j++)
441         {
442             struct REG_TYPE rt;
443             rt.signature[0] = '0';
444             rt.signature[1] = 't';
445             rt.signature[2] = 'y';
446             rt.signature[3] = '3';
447             rt.signature[0] += j;
448
449             rt.dwUnused = 0;
450             rt.dwOffsetMajor = find_data(&clsidStore, (const BYTE*)rgPin2.lpMediaType[j].clsMajorType, sizeof(CLSID));
451             if (rt.dwOffsetMajor == -1)
452                 rt.dwOffsetMajor = add_data(&clsidStore, (const BYTE*)rgPin2.lpMediaType[j].clsMajorType, sizeof(CLSID));
453             rt.dwOffsetMajor += size;
454             rt.dwOffsetMinor = find_data(&clsidStore, (const BYTE*)rgPin2.lpMediaType[j].clsMinorType, sizeof(CLSID));
455             if (rt.dwOffsetMinor == -1)
456                 rt.dwOffsetMinor = add_data(&clsidStore, (const BYTE*)rgPin2.lpMediaType[j].clsMinorType, sizeof(CLSID));
457             rt.dwOffsetMinor += size;
458
459             add_data(&mainStore, (LPBYTE)&rt, sizeof(rt));
460         }
461
462         for (j = 0; j < rgPin2.nMediums; j++)
463         {
464             DWORD index = find_data(&clsidStore, (const BYTE*)(rgPin2.lpMedium + j), sizeof(REGPINMEDIUM));
465             if (index == -1)
466                 index = add_data(&clsidStore, (const BYTE*)(rgPin2.lpMedium + j), sizeof(REGPINMEDIUM));
467             index += size;
468
469             add_data(&mainStore, (LPBYTE)&index, sizeof(index));
470         }
471     }
472
473     saBound.lLbound = 0;
474     saBound.cElements = mainStore.current + clsidStore.current;
475     psa = SafeArrayCreate(VT_UI1, 1, &saBound);
476     if (!psa)
477     {
478         ERR("Couldn't create SAFEARRAY\n");
479         hr = E_FAIL;
480     }
481
482     if (SUCCEEDED(hr))
483     {
484         LPBYTE pbSAData;
485         hr = SafeArrayAccessData(psa, (LPVOID *)&pbSAData);
486         if (SUCCEEDED(hr))
487         {
488             memcpy(pbSAData, mainStore.pData, mainStore.current);
489             memcpy(pbSAData + mainStore.current, clsidStore.pData, clsidStore.current);
490             hr = SafeArrayUnaccessData(psa);
491         }
492     }
493
494     V_VT(&var) = VT_ARRAY | VT_UI1;
495     V_UNION(&var, parray) = psa;
496
497     if (SUCCEEDED(hr))
498         hr = IPropertyBag_Write(pPropBag, wszFilterDataName, &var);
499
500     if (psa)
501         SafeArrayDestroy(psa);
502
503     delete_vector(&mainStore);
504     delete_vector(&clsidStore);
505     return hr;
506 }
507
508 static HRESULT FM2_ReadFilterData(IPropertyBag * pPropBag, REGFILTER2 * prf2)
509 {
510     VARIANT var;
511     HRESULT hr;
512     LPBYTE pData = NULL;
513     struct REG_RF * prrf;
514     LPBYTE pCurrent;
515     DWORD i;
516     REGFILTERPINS2 * rgPins2;
517
518     VariantInit(&var);
519     V_VT(&var) = VT_ARRAY | VT_UI1;
520
521     hr = IPropertyBag_Read(pPropBag, wszFilterDataName, &var, NULL);
522
523     if (SUCCEEDED(hr))
524         hr = SafeArrayAccessData(V_UNION(&var, parray), (LPVOID*)&pData);
525
526     if (SUCCEEDED(hr))
527     {
528         prrf = (struct REG_RF *)pData;
529         pCurrent = pData;
530
531         if (prrf->dwVersion != 2)
532         {
533             FIXME("Filter registry version %d not supported\n", prrf->dwVersion);
534             ZeroMemory(prf2, sizeof(*prf2));
535             hr = E_FAIL;
536         }
537     }
538
539     if (SUCCEEDED(hr))
540     {
541         TRACE("version = %d, merit = %x, #pins = %d, unused = %x\n",
542             prrf->dwVersion, prrf->dwMerit, prrf->dwPins, prrf->dwUnused);
543
544         prf2->dwVersion = prrf->dwVersion;
545         prf2->dwMerit = prrf->dwMerit;
546         prf2->u.s1.cPins2 = prrf->dwPins;
547         rgPins2 = CoTaskMemAlloc(prrf->dwPins * sizeof(*rgPins2));
548         prf2->u.s1.rgPins2 = rgPins2;
549         pCurrent += sizeof(struct REG_RF);
550
551         for (i = 0; i < prrf->dwPins; i++)
552         {
553             struct REG_RFP * prrfp = (struct REG_RFP *)pCurrent;
554             REGPINTYPES * lpMediaType;
555             REGPINMEDIUM * lpMedium;
556             UINT j;
557
558             /* FIXME: check signature */
559
560             TRACE("\tsignature = %s\n", debugstr_an((const char*)prrfp->signature, 4));
561
562             TRACE("\tpin[%d]: flags = %x, instances = %d, media types = %d, mediums = %d\n",
563                 i, prrfp->dwFlags, prrfp->dwInstances, prrfp->dwMediaTypes, prrfp->dwMediums);
564
565             rgPins2[i].dwFlags = prrfp->dwFlags;
566             rgPins2[i].cInstances = prrfp->dwInstances;
567             rgPins2[i].nMediaTypes = prrfp->dwMediaTypes;
568             rgPins2[i].nMediums = prrfp->dwMediums;
569             pCurrent += sizeof(struct REG_RFP);
570             if (prrfp->bCategory)
571             {
572                 CLSID * clsCat = CoTaskMemAlloc(sizeof(CLSID));
573                 memcpy(clsCat, pData + *(DWORD*)(pCurrent), sizeof(CLSID));
574                 pCurrent += sizeof(DWORD);
575                 rgPins2[i].clsPinCategory = clsCat;
576             }
577             else
578                 rgPins2[i].clsPinCategory = NULL;
579
580             if (rgPins2[i].nMediaTypes > 0)
581                 lpMediaType = CoTaskMemAlloc(rgPins2[i].nMediaTypes * sizeof(*lpMediaType));
582             else
583                 lpMediaType = NULL;
584
585             rgPins2[i].lpMediaType = lpMediaType;
586
587             for (j = 0; j < rgPins2[i].nMediaTypes; j++)
588             {
589                 struct REG_TYPE * prt = (struct REG_TYPE *)pCurrent;
590                 CLSID * clsMajor = CoTaskMemAlloc(sizeof(CLSID));
591                 CLSID * clsMinor = CoTaskMemAlloc(sizeof(CLSID));
592
593                 /* FIXME: check signature */
594                 TRACE("\t\tsignature = %s\n", debugstr_an((const char*)prt->signature, 4));
595
596                 memcpy(clsMajor, pData + prt->dwOffsetMajor, sizeof(CLSID));
597                 memcpy(clsMinor, pData + prt->dwOffsetMinor, sizeof(CLSID));
598
599                 lpMediaType[j].clsMajorType = clsMajor;
600                 lpMediaType[j].clsMinorType = clsMinor;
601
602                 pCurrent += sizeof(*prt);
603             }
604
605             if (rgPins2[i].nMediums > 0)
606                 lpMedium = CoTaskMemAlloc(rgPins2[i].nMediums * sizeof(*lpMedium));
607             else
608                 lpMedium = NULL;
609
610             rgPins2[i].lpMedium = lpMedium;
611
612             for (j = 0; j < rgPins2[i].nMediums; j++)
613             {
614                 DWORD dwOffset = *(DWORD *)pCurrent;
615
616                 memcpy(lpMedium + j, pData + dwOffset, sizeof(REGPINMEDIUM));
617
618                 pCurrent += sizeof(dwOffset);
619             }
620         }
621
622     }
623
624     if (pData)
625         SafeArrayUnaccessData(V_UNION(&var, parray));
626
627     VariantClear(&var);
628
629     return hr;
630 }
631
632 static void FM2_DeleteRegFilter(REGFILTER2 * prf2)
633 {
634     UINT i;
635     for (i = 0; i < prf2->u.s1.cPins2; i++)
636     {
637         UINT j;
638         if (prf2->u.s1.rgPins2[i].clsPinCategory)
639             CoTaskMemFree((LPVOID)prf2->u.s1.rgPins2[i].clsPinCategory);
640
641         for (j = 0; j < prf2->u.s1.rgPins2[i].nMediaTypes; j++)
642         {
643             CoTaskMemFree((LPVOID)prf2->u.s1.rgPins2[i].lpMediaType[j].clsMajorType);
644             CoTaskMemFree((LPVOID)prf2->u.s1.rgPins2[i].lpMediaType[j].clsMinorType);
645         }
646         CoTaskMemFree((LPVOID)prf2->u.s1.rgPins2[i].lpMedium);
647     }
648 }
649
650 static HRESULT WINAPI FilterMapper2_RegisterFilter(
651     IFilterMapper2 * iface,
652     REFCLSID clsidFilter,
653     LPCWSTR szName,
654     IMoniker **ppMoniker,
655     const CLSID *pclsidCategory,
656     const OLECHAR *szInstance,
657     const REGFILTER2 *prf2)
658 {
659     IParseDisplayName * pParser = NULL;
660     IBindCtx * pBindCtx = NULL;
661     IMoniker * pMoniker = NULL;
662     IPropertyBag * pPropBag = NULL;
663     HRESULT hr;
664     LPWSTR pwszParseName = NULL;
665     LPWSTR pCurrent;
666     static const WCHAR wszDevice[] = {'@','d','e','v','i','c','e',':','s','w',':',0};
667     int nameLen;
668     ULONG ulEaten;
669     LPWSTR szClsidTemp = NULL;
670     REGFILTER2 regfilter2;
671     REGFILTERPINS2* pregfp2 = NULL;
672
673     TRACE("(%s, %s, %p, %s, %s, %p)\n",
674         debugstr_guid(clsidFilter),
675         debugstr_w(szName),
676         ppMoniker,
677         debugstr_guid(pclsidCategory),
678         debugstr_w(szInstance),
679         prf2);
680
681     if (prf2->dwVersion == 2)
682     {
683         regfilter2 = *prf2;
684     }
685     else if (prf2->dwVersion == 1)
686     {
687         ULONG i;
688         DWORD flags;
689         /* REGFILTER2 structure is converted from version 1 to 2. Tested on Win2k. */
690         regfilter2.dwVersion = 2;
691         regfilter2.dwMerit = prf2->dwMerit;
692         regfilter2.u.s1.cPins2 = prf2->u.s.cPins;
693         pregfp2 = (REGFILTERPINS2*) CoTaskMemAlloc(prf2->u.s.cPins * sizeof(REGFILTERPINS2));
694         regfilter2.u.s1.rgPins2 = pregfp2;
695         for (i = 0; i < prf2->u.s.cPins; i++)
696         {
697             flags = 0;
698             if (prf2->u.s.rgPins[i].bRendered)
699                 flags |= REG_PINFLAG_B_RENDERER;
700             if (prf2->u.s.rgPins[i].bOutput)
701                 flags |= REG_PINFLAG_B_OUTPUT;
702             if (prf2->u.s.rgPins[i].bZero)
703                 flags |= REG_PINFLAG_B_ZERO;
704             if (prf2->u.s.rgPins[i].bMany)
705                 flags |= REG_PINFLAG_B_MANY;
706             pregfp2[i].dwFlags = flags;
707             pregfp2[i].cInstances = 1;
708             pregfp2[i].nMediaTypes = prf2->u.s.rgPins[i].nMediaTypes;
709             pregfp2[i].lpMediaType = prf2->u.s.rgPins[i].lpMediaType;
710             pregfp2[i].nMediums = 0;
711             pregfp2[i].lpMedium = NULL;
712             pregfp2[i].clsPinCategory = NULL;
713         }
714     }
715     else
716     {
717         FIXME("dwVersion other that 1 or 2 not supported at the moment\n");
718         return E_NOTIMPL;
719     }
720
721     if (ppMoniker)
722         *ppMoniker = NULL;
723
724     if (!pclsidCategory)
725         /* MSDN mentions the inexistent CLSID_ActiveMovieFilters GUID.
726          * In fact this is the CLSID_LegacyAmFilterCategory one */
727         pclsidCategory = &CLSID_LegacyAmFilterCategory;
728
729     /* sizeof... will include the null terminator and
730      * the + 1 is for the separator ('\\'). The -1 is
731      * because CHARS_IN_GUID includes the null terminator
732      */
733     nameLen = sizeof(wszDevice)/sizeof(wszDevice[0]) + CHARS_IN_GUID - 1 + 1;
734
735     if (szInstance)
736         nameLen += strlenW(szInstance);
737     else
738         nameLen += CHARS_IN_GUID - 1; /* CHARS_IN_GUID includes null terminator */
739
740     pCurrent = pwszParseName = CoTaskMemAlloc(nameLen*sizeof(WCHAR));
741     if (!pwszParseName)
742         return E_OUTOFMEMORY;
743
744     strcpyW(pwszParseName, wszDevice);
745     pCurrent += strlenW(wszDevice);
746
747     hr = StringFromCLSID(pclsidCategory, &szClsidTemp);
748
749     if (SUCCEEDED(hr))
750     {
751         memcpy(pCurrent, szClsidTemp, CHARS_IN_GUID * sizeof(WCHAR));
752         pCurrent += CHARS_IN_GUID - 1;
753         pCurrent[0] = '\\';
754
755         if (szInstance)
756             strcpyW(pCurrent+1, szInstance);
757         else
758         {
759             CoTaskMemFree(szClsidTemp);
760             szClsidTemp = NULL;
761
762             hr = StringFromCLSID(clsidFilter, &szClsidTemp);
763             if (SUCCEEDED(hr))
764                 strcpyW(pCurrent+1, szClsidTemp);
765         }
766     }
767
768     if (SUCCEEDED(hr))
769         hr = CoCreateInstance(&CLSID_CDeviceMoniker, NULL, CLSCTX_INPROC, &IID_IParseDisplayName, (LPVOID *)&pParser);
770
771     if (SUCCEEDED(hr))
772         hr = CreateBindCtx(0, &pBindCtx);
773
774     if (SUCCEEDED(hr))
775         hr = IParseDisplayName_ParseDisplayName(pParser, pBindCtx, pwszParseName, &ulEaten, &pMoniker);
776
777     if (pBindCtx)
778         IBindCtx_Release(pBindCtx);
779     if (pParser)
780         IParseDisplayName_Release(pParser);
781
782     if (SUCCEEDED(hr))
783         hr = IMoniker_BindToStorage(pMoniker, NULL, NULL, &IID_IPropertyBag, (LPVOID)&pPropBag);
784
785     if (SUCCEEDED(hr))
786         hr = FM2_WriteFriendlyName(pPropBag, szName);
787
788     if (SUCCEEDED(hr))
789         hr = FM2_WriteClsid(pPropBag, clsidFilter);
790
791     if (SUCCEEDED(hr))
792         hr = FM2_WriteFilterData(pPropBag, &regfilter2);
793
794     if (pPropBag)
795         IPropertyBag_Release(pPropBag);
796     CoTaskMemFree(szClsidTemp);
797
798     if (SUCCEEDED(hr) && ppMoniker)
799         *ppMoniker = pMoniker;
800     else if (pMoniker)
801         IMoniker_Release(pMoniker);
802
803     CoTaskMemFree(pregfp2);
804
805     TRACE("-- returning %x\n", hr);
806
807     return hr;
808 }
809
810 /* internal helper function */
811 static BOOL MatchTypes(
812     BOOL bExactMatch,
813     DWORD nPinTypes,
814     const REGPINTYPES * pPinTypes,
815     DWORD nMatchTypes,
816     const GUID * pMatchTypes)
817 {
818     BOOL bMatch = FALSE;
819     DWORD j;
820
821     if ((nMatchTypes == 0) && (nPinTypes > 0))
822         bMatch = TRUE;
823
824     for (j = 0; j < nPinTypes; j++)
825     {
826         DWORD i;
827         for (i = 0; i < nMatchTypes*2; i+=2)
828         {
829             if (((!bExactMatch && IsEqualGUID(pPinTypes[j].clsMajorType, &GUID_NULL)) || IsEqualGUID(&pMatchTypes[i], &GUID_NULL) || IsEqualGUID(pPinTypes[j].clsMajorType, &pMatchTypes[i])) &&
830                 ((!bExactMatch && IsEqualGUID(pPinTypes[j].clsMinorType, &GUID_NULL)) || IsEqualGUID(&pMatchTypes[i+1], &GUID_NULL) || IsEqualGUID(pPinTypes[j].clsMinorType, &pMatchTypes[i+1])))
831             {
832                 bMatch = TRUE;
833                 break;
834             }
835         }
836     }
837     return bMatch;
838 }
839
840 /* internal helper function for qsort of MONIKER_MERIT array */
841 static int mm_compare(const void * left, const void * right)
842 {
843     const struct MONIKER_MERIT * mmLeft = (const struct MONIKER_MERIT *)left;
844     const struct MONIKER_MERIT * mmRight = (const struct MONIKER_MERIT *)right;
845
846     if (mmLeft->dwMerit == mmRight->dwMerit)
847         return 0;
848     if (mmLeft->dwMerit > mmRight->dwMerit)
849         return -1;
850     return 1;
851 }
852
853 /* NOTES:
854  *   Exact match means whether or not to treat GUID_NULL's in filter data as wild cards
855  *    (GUID_NULL's in input to function automatically treated as wild cards)
856  *   Input/Output needed means match only on criteria if TRUE (with zero input types
857  *    meaning match any input/output pin as long as one exists), otherwise match any
858  *    filter that meets the rest of the requirements.
859  */
860 static HRESULT WINAPI FilterMapper2_EnumMatchingFilters(
861     IFilterMapper2 * iface,
862     IEnumMoniker **ppEnum,
863     DWORD dwFlags,
864     BOOL bExactMatch,
865     DWORD dwMerit,
866     BOOL bInputNeeded,
867     DWORD cInputTypes,
868     const GUID *pInputTypes,
869     const REGPINMEDIUM *pMedIn,
870     const CLSID *pPinCategoryIn,
871     BOOL bRender,
872     BOOL bOutputNeeded,
873     DWORD cOutputTypes,
874     const GUID *pOutputTypes,
875     const REGPINMEDIUM *pMedOut,
876     const CLSID *pPinCategoryOut)
877 {
878     ICreateDevEnum * pCreateDevEnum;
879     IMoniker * pMonikerCat;
880     IEnumMoniker * pEnumCat;
881     HRESULT hr;
882     struct Vector monikers = {NULL, 0, 0};
883
884     TRACE("(%p, %x, %s, %x, %s, %d, %p, %p, %p, %s, %s, %p, %p, %p)\n",
885         ppEnum,
886         dwFlags,
887         bExactMatch ? "true" : "false",
888         dwMerit,
889         bInputNeeded ? "true" : "false",
890         cInputTypes,
891         pInputTypes,
892         pMedIn,
893         pPinCategoryIn,
894         bRender ? "true" : "false",
895         bOutputNeeded ? "true" : "false",
896         pOutputTypes,
897         pMedOut,
898         pPinCategoryOut);
899
900     if (dwFlags != 0)
901     {
902         FIXME("dwFlags = %x not implemented\n", dwFlags);
903     }
904
905     *ppEnum = NULL;
906
907     hr = CoCreateInstance(&CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC, &IID_ICreateDevEnum, (LPVOID*)&pCreateDevEnum);
908
909     if (SUCCEEDED(hr))
910         hr = ICreateDevEnum_CreateClassEnumerator(pCreateDevEnum, &CLSID_ActiveMovieCategories, &pEnumCat, 0);
911
912     while (IEnumMoniker_Next(pEnumCat, 1, &pMonikerCat, NULL) == S_OK)
913     {
914         IPropertyBag * pPropBagCat = NULL;
915         VARIANT var;
916         HRESULT hrSub; /* this is so that one buggy filter
917                           doesn't make the whole lot fail */
918
919         VariantInit(&var);
920
921         hrSub = IMoniker_BindToStorage(pMonikerCat, NULL, NULL, &IID_IPropertyBag, (LPVOID*)&pPropBagCat);
922
923         if (SUCCEEDED(hrSub))
924             hrSub = IPropertyBag_Read(pPropBagCat, wszMeritName, &var, NULL);
925
926         if (SUCCEEDED(hrSub) && (V_UNION(&var, ulVal) >= dwMerit))
927         {
928             CLSID clsidCat;
929             IEnumMoniker * pEnum;
930             IMoniker * pMoniker;
931
932             VariantClear(&var);
933
934             if (TRACE_ON(quartz))
935             {
936                 VARIANT temp;
937                 V_VT(&temp) = VT_EMPTY;
938                 IPropertyBag_Read(pPropBagCat, wszFriendlyName, &temp, NULL);
939                 TRACE("Considering category %s\n", debugstr_w(V_UNION(&temp, bstrVal)));
940                 VariantClear(&temp);
941             }
942
943             hrSub = IPropertyBag_Read(pPropBagCat, wszClsidName, &var, NULL);
944
945             if (SUCCEEDED(hrSub))
946                 hrSub = CLSIDFromString(V_UNION(&var, bstrVal), &clsidCat);
947
948             if (SUCCEEDED(hrSub))
949                 hrSub = ICreateDevEnum_CreateClassEnumerator(pCreateDevEnum, &clsidCat, &pEnum, 0);
950
951             if (hrSub == S_OK)
952             {
953                 while (IEnumMoniker_Next(pEnum, 1, &pMoniker, NULL) == S_OK)
954                 {
955                     IPropertyBag * pPropBag = NULL;
956                     REGFILTER2 rf2;
957                     DWORD i;
958                     BOOL bInputMatch = !bInputNeeded;
959                     BOOL bOutputMatch = !bOutputNeeded;
960
961                     ZeroMemory(&rf2, sizeof(rf2));
962
963                     hrSub = IMoniker_BindToStorage(pMoniker, NULL, NULL, &IID_IPropertyBag, (LPVOID*)&pPropBag);
964
965                     if (TRACE_ON(quartz))
966                     {
967                         VARIANT temp;
968                         V_VT(&temp) = VT_EMPTY;
969                         IPropertyBag_Read(pPropBag, wszFriendlyName, &temp, NULL);
970                         TRACE("Considering filter %s\n", debugstr_w(V_UNION(&temp, bstrVal)));
971                         VariantClear(&temp);
972                     }
973
974                     if (SUCCEEDED(hrSub))
975                         hrSub = FM2_ReadFilterData(pPropBag, &rf2);
976
977                     /* Logic used for bInputMatch expression:
978                      * There exists some pin such that bInputNeeded implies (pin is an input and
979                      * (bRender implies pin has render flag) and major/minor types members of
980                      * pInputTypes )
981                      * bOutputMatch is similar, but without the "bRender implies ..." part
982                      * and substituting variables names containing input for output
983                      */
984
985                     /* determine whether filter meets requirements */
986                     if (SUCCEEDED(hrSub) && (rf2.dwMerit >= dwMerit))
987                     {
988                         for (i = 0; (i < rf2.u.s1.cPins2) && (!bInputMatch || !bOutputMatch); i++)
989                         {
990                             const REGFILTERPINS2 * rfp2 = rf2.u.s1.rgPins2 + i;
991
992                             bInputMatch = bInputMatch || (!(rfp2->dwFlags & REG_PINFLAG_B_OUTPUT) &&
993                                 (!bRender || (rfp2->dwFlags & REG_PINFLAG_B_RENDERER)) &&
994                                 MatchTypes(bExactMatch, rfp2->nMediaTypes, rfp2->lpMediaType, cInputTypes, pInputTypes));
995                             bOutputMatch = bOutputMatch || ((rfp2->dwFlags & REG_PINFLAG_B_OUTPUT) &&
996                                 MatchTypes(bExactMatch, rfp2->nMediaTypes, rfp2->lpMediaType, cOutputTypes, pOutputTypes));
997                         }
998
999                         if (bInputMatch && bOutputMatch)
1000                         {
1001                             struct MONIKER_MERIT mm = {pMoniker, rf2.dwMerit};
1002                             IMoniker_AddRef(pMoniker);
1003                             add_data(&monikers, (LPBYTE)&mm, sizeof(mm));
1004                         }
1005                     }
1006
1007                     FM2_DeleteRegFilter(&rf2);
1008                     if (pPropBag)
1009                         IPropertyBag_Release(pPropBag);
1010                     IMoniker_Release(pMoniker);
1011                 }
1012                 IEnumMoniker_Release(pEnum);
1013             }
1014         }
1015
1016         VariantClear(&var);
1017         if (pPropBagCat)
1018             IPropertyBag_Release(pPropBagCat);
1019         IMoniker_Release(pMonikerCat);
1020     }
1021
1022     if (SUCCEEDED(hr))
1023     {
1024         IMoniker ** ppMoniker;
1025         unsigned int i;
1026         ULONG nMonikerCount = monikers.current / sizeof(struct MONIKER_MERIT);
1027
1028         /* sort the monikers in descending merit order */
1029         qsort(monikers.pData, nMonikerCount,
1030               sizeof(struct MONIKER_MERIT),
1031               mm_compare);
1032
1033         /* construct an IEnumMoniker interface */
1034         ppMoniker = CoTaskMemAlloc(nMonikerCount * sizeof(IMoniker *));
1035         for (i = 0; i < nMonikerCount; i++)
1036         {
1037             /* no need to AddRef here as already AddRef'd above */
1038             ppMoniker[i] = ((struct MONIKER_MERIT *)monikers.pData)[i].pMoniker;
1039         }
1040         hr = EnumMonikerImpl_Create(ppMoniker, nMonikerCount, ppEnum);
1041         CoTaskMemFree(ppMoniker);
1042     }
1043
1044     delete_vector(&monikers);
1045     IEnumMoniker_Release(pEnumCat);
1046     ICreateDevEnum_Release(pCreateDevEnum);
1047
1048     return hr;
1049 }
1050
1051 static const IFilterMapper2Vtbl fm2vtbl =
1052 {
1053
1054     FilterMapper2_QueryInterface,
1055     FilterMapper2_AddRef,
1056     FilterMapper2_Release,
1057
1058     FilterMapper2_CreateCategory,
1059     FilterMapper2_UnregisterFilter,
1060     FilterMapper2_RegisterFilter,
1061     FilterMapper2_EnumMatchingFilters
1062 };
1063
1064 /*** IUnknown methods ***/
1065
1066 static HRESULT WINAPI FilterMapper_QueryInterface(IFilterMapper * iface, REFIID riid, LPVOID *ppv)
1067 {
1068     FilterMapper2Impl *This = impl_from_IFilterMapper(iface);
1069
1070     TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
1071
1072     return FilterMapper2_QueryInterface((IFilterMapper2*)&This->lpVtbl, riid, ppv);
1073 }
1074
1075 static ULONG WINAPI FilterMapper_AddRef(IFilterMapper * iface)
1076 {
1077     FilterMapper2Impl *This = impl_from_IFilterMapper(iface);
1078
1079     return FilterMapper2_AddRef((IFilterMapper2*)This);
1080 }
1081
1082 static ULONG WINAPI FilterMapper_Release(IFilterMapper * iface)
1083 {
1084     FilterMapper2Impl *This = impl_from_IFilterMapper(iface);
1085
1086     return FilterMapper2_Release((IFilterMapper2*)This);
1087 }
1088
1089 /*** IFilterMapper methods ***/
1090
1091 static HRESULT WINAPI FilterMapper_EnumMatchingFilters(
1092     IFilterMapper * iface,
1093     IEnumRegFilters **ppEnum,
1094     DWORD dwMerit,
1095     BOOL bInputNeeded,
1096     CLSID clsInMaj,
1097     CLSID clsInSub,
1098     BOOL bRender,
1099     BOOL bOutputNeeded,
1100     CLSID clsOutMaj,
1101     CLSID clsOutSub)
1102 {
1103     FilterMapper2Impl *This = impl_from_IFilterMapper(iface);
1104     GUID InputType[2];
1105     GUID OutputType[2];
1106     IEnumMoniker* ppEnumMoniker;
1107     IMoniker* IMon;
1108     ULONG nb;
1109     ULONG idx = 0, nb_mon = 0;
1110     REGFILTER* regfilters;
1111     HRESULT hr;
1112
1113     TRACE("(%p/%p)->(%p, %x, %s, %s, %s, %s, %s, %s, %s) stub!\n",
1114         iface,This,
1115         ppEnum,
1116         dwMerit,
1117         bInputNeeded ? "true" : "false",
1118         debugstr_guid(&clsInMaj),
1119         debugstr_guid(&clsInSub),
1120         bRender ? "true" : "false",
1121         bOutputNeeded ? "true" : "false",
1122         debugstr_guid(&clsOutMaj),
1123         debugstr_guid(&clsOutSub));
1124
1125     InputType[0] = clsInMaj;
1126     InputType[1] = clsInSub;
1127     OutputType[0] = clsOutMaj;
1128     OutputType[1] = clsOutSub;
1129
1130     hr = IFilterMapper2_EnumMatchingFilters((IFilterMapper2*)This,
1131                                        &ppEnumMoniker,
1132                                        0,
1133                                        TRUE,
1134                                        dwMerit,
1135                                        bInputNeeded,
1136                                        1,
1137                                        InputType,
1138                                        NULL,
1139                                        &GUID_NULL,
1140                                        bRender,
1141                                        bOutputNeeded,
1142                                        1,
1143                                        OutputType,
1144                                        NULL,
1145                                        &GUID_NULL);
1146
1147     if (!SUCCEEDED(hr))
1148         return hr;
1149     
1150     while(IEnumMoniker_Next(ppEnumMoniker, 1, &IMon, &nb) == S_OK)
1151     {
1152         IMoniker_Release(IMon);
1153         nb_mon++;
1154     }
1155
1156     *ppEnum = NULL;
1157     if (!nb_mon)
1158     {
1159         IEnumMoniker_Release(ppEnumMoniker);
1160         return IEnumRegFiltersImpl_Construct(NULL, 0, ppEnum);
1161     }
1162
1163     regfilters = CoTaskMemAlloc(nb_mon * sizeof(REGFILTER));
1164     if (!regfilters)
1165     {
1166         IEnumMoniker_Release(ppEnumMoniker);
1167         return E_OUTOFMEMORY;
1168     }
1169     
1170     IEnumMoniker_Reset(ppEnumMoniker);
1171     while(IEnumMoniker_Next(ppEnumMoniker, 1, &IMon, &nb) == S_OK)
1172     {
1173         IPropertyBag * pPropBagCat = NULL;
1174         VARIANT var;
1175         HRESULT hrSub;
1176         GUID clsid;
1177         int len;
1178
1179         VariantInit(&var);
1180         V_VT(&var) = VT_BSTR;
1181
1182         hrSub = IMoniker_BindToStorage(IMon, NULL, NULL, &IID_IPropertyBag, (LPVOID*)&pPropBagCat);
1183
1184         if (SUCCEEDED(hrSub))
1185             hrSub = IPropertyBag_Read(pPropBagCat, wszClsidName, &var, NULL);
1186
1187         if (SUCCEEDED(hrSub))
1188             hrSub = CLSIDFromString(V_UNION(&var, bstrVal), &clsid);
1189
1190         if (SUCCEEDED(hrSub))
1191             hrSub = IPropertyBag_Read(pPropBagCat, wszFriendlyName, &var, NULL);
1192
1193         if (SUCCEEDED(hrSub))
1194         {
1195             len = (strlenW((WCHAR*)&V_UNION(&var, bstrVal))+1) * sizeof(WCHAR);
1196             if (!(regfilters[idx].Name = CoTaskMemAlloc(len*2)))
1197                 hr = E_OUTOFMEMORY;
1198         }
1199
1200         if (SUCCEEDED(hrSub))
1201         {
1202             memcpy(regfilters[idx].Name, &V_UNION(&var, bstrVal), len);
1203             regfilters[idx].Clsid = clsid;
1204             idx++;
1205         }
1206
1207         if (pPropBagCat)
1208             IPropertyBag_Release(pPropBagCat);
1209         IMoniker_Release(IMon);
1210     }
1211
1212     /* In case of release all resources */
1213     if (!SUCCEEDED(hr))
1214     {
1215         for (idx = 0; idx < nb_mon; idx++)
1216             CoTaskMemFree(regfilters[idx].Name);
1217         CoTaskMemFree(regfilters);
1218         IEnumMoniker_Release(ppEnumMoniker);
1219         return hr;
1220     }
1221
1222     hr = IEnumRegFiltersImpl_Construct(regfilters, nb_mon, ppEnum);
1223     CoTaskMemFree(regfilters);
1224     IEnumMoniker_Release(ppEnumMoniker);
1225     
1226     return hr;
1227 }
1228
1229
1230 static HRESULT WINAPI FilterMapper_RegisterFilter(IFilterMapper * iface, CLSID clsid, LPCWSTR szName, DWORD dwMerit)
1231 {
1232     HRESULT hr;
1233     LPWSTR wszClsid = NULL;
1234     HKEY hKey;
1235     LONG lRet;
1236     WCHAR wszKeyName[strlenW(wszFilterSlash) + (CHARS_IN_GUID-1) + 1];
1237
1238     TRACE("(%p)->(%s, %s, %x)\n", iface, debugstr_guid(&clsid), debugstr_w(szName), dwMerit);
1239
1240     hr = StringFromCLSID(&clsid, &wszClsid);
1241
1242     if (SUCCEEDED(hr))
1243     {
1244         strcpyW(wszKeyName, wszFilterSlash);
1245         strcatW(wszKeyName, wszClsid);
1246     
1247         lRet = RegCreateKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
1248         hr = HRESULT_FROM_WIN32(lRet);
1249     }
1250
1251     if (SUCCEEDED(hr))
1252     {
1253         lRet = RegSetValueExW(hKey, NULL, 0, REG_SZ, (const BYTE*)szName, strlenW(szName) + 1);
1254         hr = HRESULT_FROM_WIN32(lRet);
1255         CloseHandle(hKey);
1256     }
1257
1258     if (SUCCEEDED(hr))
1259     {
1260         strcpyW(wszKeyName, wszClsidSlash);
1261         strcatW(wszKeyName, wszClsid);
1262     
1263         lRet = RegCreateKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
1264         hr = HRESULT_FROM_WIN32(lRet);
1265     }
1266
1267     if (SUCCEEDED(hr))
1268     {
1269         lRet = RegSetValueExW(hKey, wszMeritName, 0, REG_DWORD, (LPBYTE)&dwMerit, sizeof(dwMerit));
1270         hr = HRESULT_FROM_WIN32(lRet);
1271         CloseHandle(hKey);
1272     }
1273     
1274     return hr;
1275 }
1276
1277 static HRESULT WINAPI FilterMapper_RegisterFilterInstance(IFilterMapper * iface, CLSID clsid, LPCWSTR szName, CLSID *MRId)
1278 {
1279     TRACE("(%p)->(%s, %s, %p)\n", iface, debugstr_guid(&clsid), debugstr_w(szName), MRId);
1280
1281     /* Not implemented in Windows (tested on Win2k) */
1282
1283     return E_NOTIMPL;
1284 }
1285
1286 static HRESULT WINAPI FilterMapper_RegisterPin(
1287     IFilterMapper * iface,
1288     CLSID Filter,
1289     LPCWSTR szName,
1290     BOOL bRendered,
1291     BOOL bOutput,
1292     BOOL bZero,
1293     BOOL bMany,
1294     CLSID ConnectsToFilter,
1295     LPCWSTR ConnectsToPin)
1296 {
1297     HRESULT hr;
1298     LONG lRet;
1299     LPWSTR wszClsid = NULL;
1300     HKEY hKey = NULL;
1301     HKEY hPinsKey = NULL;
1302     WCHAR * wszPinsKeyName;
1303     WCHAR wszKeyName[strlenW(wszClsidSlash) + (CHARS_IN_GUID-1) + 1];
1304
1305     TRACE("(%p)->(%s, %s, %d, %d, %d, %d, %s, %s)\n", iface, debugstr_guid(&Filter), debugstr_w(szName), bRendered,
1306                 bOutput, bZero, bMany, debugstr_guid(&ConnectsToFilter), debugstr_w(ConnectsToPin));
1307
1308     hr = StringFromCLSID(&Filter, &wszClsid);
1309
1310     if (SUCCEEDED(hr))
1311     {
1312         strcpyW(wszKeyName, wszClsidSlash);
1313         strcatW(wszKeyName, wszClsid);
1314
1315         lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, KEY_WRITE, &hKey);
1316         hr = HRESULT_FROM_WIN32(lRet);
1317     }
1318
1319     if (SUCCEEDED(hr))
1320     {
1321         wszPinsKeyName = CoTaskMemAlloc((strlenW(wszPins) + 1 + strlenW(szName) + 1) * 2);
1322         if (!wszPinsKeyName)
1323              hr = E_OUTOFMEMORY;
1324     }
1325
1326     if (SUCCEEDED(hr))
1327     {
1328         strcpyW(wszPinsKeyName, wszPins);
1329         strcatW(wszPinsKeyName, wszSlash);
1330         strcatW(wszPinsKeyName, szName);
1331     
1332         lRet = RegCreateKeyExW(hKey, wszPinsKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hPinsKey, NULL);
1333         hr = HRESULT_FROM_WIN32(lRet);
1334         CoTaskMemFree(wszPinsKeyName);
1335     }
1336
1337     if (SUCCEEDED(hr))
1338     {
1339         lRet = RegSetValueExW(hPinsKey, wszAllowedMany, 0, REG_DWORD, (LPBYTE)&bMany, sizeof(bMany));
1340         hr = HRESULT_FROM_WIN32(lRet);
1341     }
1342
1343     if (SUCCEEDED(hr))
1344     {
1345         lRet = RegSetValueExW(hPinsKey, wszAllowedZero, 0, REG_DWORD, (LPBYTE)&bZero, sizeof(bZero));
1346         hr = HRESULT_FROM_WIN32(lRet);
1347     }
1348
1349     if (SUCCEEDED(hr))
1350     {
1351         lRet = RegSetValueExW(hPinsKey, wszDirection, 0, REG_DWORD, (LPBYTE)&bOutput, sizeof(bOutput));
1352         hr = HRESULT_FROM_WIN32(lRet);
1353     }
1354
1355     if (SUCCEEDED(hr))
1356     {
1357         lRet = RegSetValueExW(hPinsKey, wszIsRendered, 0, REG_DWORD, (LPBYTE)&bRendered, sizeof(bRendered));
1358         hr = HRESULT_FROM_WIN32(lRet);
1359     }
1360
1361     if (SUCCEEDED(hr))
1362     {
1363         lRet = RegCreateKeyExW(hPinsKey, wszTypes, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, NULL, NULL);
1364         hr = HRESULT_FROM_WIN32(lRet);
1365     }
1366
1367     CoTaskMemFree(wszClsid);
1368     if (hKey)
1369         CloseHandle(hKey);
1370     if (hPinsKey)
1371         CloseHandle(hPinsKey);
1372
1373     return hr;
1374 }
1375
1376
1377 static HRESULT WINAPI FilterMapper_RegisterPinType(
1378     IFilterMapper * iface,
1379     CLSID clsFilter,
1380     LPCWSTR szName,
1381     CLSID clsMajorType,
1382     CLSID clsSubType)
1383 {
1384     HRESULT hr;
1385     LONG lRet;
1386     LPWSTR wszClsid = NULL;
1387     LPWSTR wszClsidMajorType = NULL;
1388     LPWSTR wszClsidSubType = NULL;
1389     HKEY hKey = NULL;
1390     WCHAR * wszTypesKey;
1391     WCHAR wszKeyName[strlenW(wszClsidSlash) + (CHARS_IN_GUID-1) + 1];
1392
1393     TRACE("(%p)->(%s, %s, %s, %s)\n", iface, debugstr_guid(&clsFilter), debugstr_w(szName),
1394                     debugstr_guid(&clsMajorType), debugstr_guid(&clsSubType));
1395
1396     hr = StringFromCLSID(&clsFilter, &wszClsid);
1397
1398     if (SUCCEEDED(hr))
1399     {
1400         hr = StringFromCLSID(&clsMajorType, &wszClsidMajorType);
1401     }
1402
1403     if (SUCCEEDED(hr))
1404     {
1405         hr = StringFromCLSID(&clsSubType, &wszClsidSubType);
1406     }
1407
1408     if (SUCCEEDED(hr))
1409     {
1410         wszTypesKey = CoTaskMemAlloc((strlenW(wszClsidSlash) + strlenW(wszClsid) + strlenW(wszPins) +
1411                         strlenW(szName) + strlenW(wszTypes) + 3 + 1) * 2);
1412         if (!wszTypesKey)
1413             hr = E_OUTOFMEMORY;
1414     }
1415
1416     if (SUCCEEDED(hr))
1417     {
1418         strcpyW(wszTypesKey, wszClsidSlash);
1419         strcatW(wszTypesKey, wszClsid);
1420         strcatW(wszTypesKey, wszSlash);
1421         strcatW(wszTypesKey, wszPins);
1422         strcatW(wszTypesKey, wszSlash);
1423         strcatW(wszTypesKey, szName);
1424         strcatW(wszTypesKey, wszSlash);
1425         strcatW(wszTypesKey, wszTypes);
1426
1427         lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszTypesKey, 0, KEY_WRITE, &hKey);
1428         hr = HRESULT_FROM_WIN32(lRet);
1429         CoTaskMemFree(wszTypesKey);
1430     }
1431
1432     if (SUCCEEDED(hr))
1433     {
1434         strcpyW(wszKeyName, wszClsidMajorType);
1435         strcatW(wszKeyName, wszSlash);
1436         strcatW(wszKeyName, wszClsidSubType);
1437
1438         lRet = RegCreateKeyExW(hKey, wszKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, NULL, NULL);
1439         hr = HRESULT_FROM_WIN32(lRet);
1440         CloseHandle(hKey);
1441     }
1442
1443     CoTaskMemFree(wszClsid);
1444     CoTaskMemFree(wszClsidMajorType);
1445     CoTaskMemFree(wszClsidSubType);
1446
1447     return hr;
1448 }
1449
1450 static HRESULT WINAPI FilterMapper_UnregisterFilter(IFilterMapper * iface, CLSID Filter)
1451 {
1452     HRESULT hr;
1453     LONG lRet;
1454     LPWSTR wszClsid = NULL;
1455     HKEY hKey;
1456     WCHAR wszKeyName[strlenW(wszClsidSlash) + (CHARS_IN_GUID-1) + 1];
1457
1458     TRACE("(%p)->(%s)\n", iface, debugstr_guid(&Filter));
1459
1460     hr = StringFromCLSID(&Filter, &wszClsid);
1461
1462     if (SUCCEEDED(hr))
1463     {
1464         lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszFilter, 0, KEY_WRITE, &hKey);
1465         hr = HRESULT_FROM_WIN32(lRet);
1466     }
1467
1468     if (SUCCEEDED(hr))
1469     {
1470         lRet = RegDeleteKeyW(hKey, wszClsid);
1471         hr = HRESULT_FROM_WIN32(lRet);
1472         CloseHandle(hKey);
1473     }
1474
1475     if (SUCCEEDED(hr))
1476     {
1477         strcpyW(wszKeyName, wszClsidSlash);
1478         strcatW(wszKeyName, wszClsid);
1479
1480         lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, KEY_WRITE, &hKey);
1481         hr = HRESULT_FROM_WIN32(lRet);
1482     }
1483
1484     if (SUCCEEDED(hr))
1485     {
1486         lRet = RegDeleteKeyW(hKey, wszMeritName);
1487         hr = HRESULT_FROM_WIN32(lRet);
1488         CloseHandle(hKey);
1489     }
1490
1491     CoTaskMemFree(wszClsid);
1492
1493     return hr;
1494 }
1495
1496 static HRESULT WINAPI FilterMapper_UnregisterFilterInstance(IFilterMapper * iface, CLSID MRId)
1497 {
1498     TRACE("(%p)->(%s)\n", iface, debugstr_guid(&MRId));
1499
1500     /* Not implemented in Windows (tested on Win2k) */
1501
1502     return E_NOTIMPL;
1503 }
1504
1505 static HRESULT WINAPI FilterMapper_UnregisterPin(IFilterMapper * iface, CLSID Filter, LPCWSTR Name)
1506 {
1507     HRESULT hr;
1508     LONG lRet;
1509     LPWSTR wszClsid = NULL;
1510     HKEY hKey = NULL;
1511     WCHAR * wszPinNameKey;
1512     WCHAR wszKeyName[strlenW(wszClsidSlash) + (CHARS_IN_GUID-1) + 1];
1513
1514     TRACE("(%p)->(%s, %s)\n", iface, debugstr_guid(&Filter), debugstr_w(Name));
1515
1516     if (!Name)
1517         return E_INVALIDARG;
1518
1519     hr = StringFromCLSID(&Filter, &wszClsid);
1520
1521     if (SUCCEEDED(hr))
1522     {
1523         strcpyW(wszKeyName, wszClsidSlash);
1524         strcatW(wszKeyName, wszClsid);
1525
1526         lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, KEY_WRITE, &hKey);
1527         hr = HRESULT_FROM_WIN32(lRet);
1528     }
1529
1530     if (SUCCEEDED(hr))
1531     {
1532         wszPinNameKey = CoTaskMemAlloc((strlenW(wszPins) + 1 + strlenW(Name) + 1) * 2);
1533         if (!wszPinNameKey)
1534             hr = E_OUTOFMEMORY;
1535     }
1536
1537     if (SUCCEEDED(hr))
1538     {
1539         strcpyW(wszPinNameKey, wszPins);
1540         strcatW(wszPinNameKey, wszSlash);
1541         strcatW(wszPinNameKey, Name);
1542
1543         lRet = RegDeleteKeyW(hKey, wszPinNameKey);
1544         hr = HRESULT_FROM_WIN32(lRet);
1545         CoTaskMemFree(wszPinNameKey);
1546     }
1547
1548     CoTaskMemFree(wszClsid);
1549     if (hKey)
1550         CloseHandle(hKey);
1551
1552     return hr;
1553 }
1554
1555 static const IFilterMapperVtbl fmvtbl =
1556 {
1557
1558     FilterMapper_QueryInterface,
1559     FilterMapper_AddRef,
1560     FilterMapper_Release,
1561
1562     FilterMapper_RegisterFilter,
1563     FilterMapper_RegisterFilterInstance,
1564     FilterMapper_RegisterPin,
1565     FilterMapper_RegisterPinType,
1566     FilterMapper_UnregisterFilter,
1567     FilterMapper_UnregisterFilterInstance,
1568     FilterMapper_UnregisterPin,
1569     FilterMapper_EnumMatchingFilters
1570 };