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