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