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