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