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