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