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