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