Use the more idiomatic form of iterating through the list.
[wine] / dlls / quartz / filtermapper.c
1 /*
2  * IFilterMapper & IFilterMapper2 Implementations
3  *
4  * Copyright 2003 Robert Shearman
5  * Copyright 2004 Christian Costa
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #define NONAMELESSUNION
23 #define NONAMELESSSTRUCT
24 #include <stdarg.h>
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "winreg.h"
30 #include "winerror.h"
31
32 #include "quartz_private.h"
33
34 #define COM_NO_WINDOWS_H
35 #include "ole2.h"
36 #include "olectl.h"
37 #include "strmif.h"
38 #include "wine/unicode.h"
39 #include "uuids.h"
40
41 #include "wine/debug.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
44
45 typedef struct FilterMapper2Impl
46 {
47     IFilterMapper2Vtbl *lpVtbl;
48     IFilterMapperVtbl  *lpVtblFilterMapper;
49     ULONG refCount;
50 } FilterMapper2Impl;
51
52 static struct IFilterMapper2Vtbl fm2vtbl;
53 static struct IFilterMapperVtbl fmvtbl;
54
55 #define _IFilterMapper_Offset ((int)(&(((FilterMapper2Impl*)0)->lpVtblFilterMapper)))
56 #define ICOM_THIS_From_IFilterMapper(impl, iface) impl* This = (impl*)(((char*)iface)-_IFilterMapper_Offset)
57
58 static const WCHAR wszClsidSlash[] = {'C','L','S','I','D','\\',0};
59 static const WCHAR wszSlashInstance[] = {'\\','I','n','s','t','a','n','c','e','\\',0};
60 static const WCHAR wszSlash[] = {'\\',0};
61
62 /* CLSID property in media category Moniker */
63 static const WCHAR wszClsidName[] = {'C','L','S','I','D',0};
64 /* FriendlyName property in media category Moniker */
65 static const WCHAR wszFriendlyName[] = {'F','r','i','e','n','d','l','y','N','a','m','e',0};
66 /* Merit property in media category Moniker (CLSID_ActiveMovieCategories only) */
67 static const WCHAR wszMeritName[] = {'M','e','r','i','t',0};
68 /* FilterData property in media category Moniker (not CLSID_ActiveMovieCategories) */
69 static const WCHAR wszFilterDataName[] = {'F','i','l','t','e','r','D','a','t','a',0};
70 /* For filters registered with IFilterMapper */
71 static const WCHAR wszFilterSlash[] = {'F','i','l','t','e','r','\\',0};
72 static const WCHAR wszFilter[] = {'F','i','l','t','e','r',0};
73 /* For pins registered with IFilterMapper */
74 static const WCHAR wszPins[] = {'P','i','n','s',0};
75 static const WCHAR wszAllowedMany[] = {'A','l','l','o','w','e','d','M','a','n','y',0};
76 static const WCHAR wszAllowedZero[] = {'A','l','l','o','w','e','d','Z','e','r','o',0};
77 static const WCHAR wszDirection[] = {'D','i','r','e','c','t','i','o','n',0};
78 static const WCHAR wszIsRendered[] = {'I','s','R','e','n','d','e','r','e','d',0};
79 /* For types registered with IFilterMapper */
80 static const WCHAR wszTypes[] = {'T','y','p','e','s',0};
81
82
83 /* registry format for REGFILTER2 */
84 struct REG_RF
85 {
86     DWORD dwVersion;
87     DWORD dwMerit;
88     DWORD dwPins;
89     DWORD dwUnused;
90 };
91
92 struct REG_RFP
93 {
94     BYTE signature[4]; /* e.g. "0pi3" */
95     DWORD dwFlags;
96     DWORD dwInstances;
97     DWORD dwMediaTypes;
98     DWORD dwMediums;
99     DWORD bCategory; /* is there a category clsid? */
100     /* optional: dwOffsetCategoryClsid */
101 };
102
103 struct REG_TYPE
104 {
105     BYTE signature[4]; /* e.g. "0ty3" */
106     DWORD dwUnused;
107     DWORD dwOffsetMajor;
108     DWORD dwOffsetMinor;
109 };
110
111 struct MONIKER_MERIT
112 {
113     IMoniker * pMoniker;
114     DWORD dwMerit;
115 };
116
117 struct Vector
118 {
119     LPBYTE pData;
120     int capacity; /* in bytes */
121     int current; /* pointer to next free byte */
122 };
123
124 /* returns the position it was added at */
125 static int add_data(struct Vector * v, const BYTE * pData, int size)
126 {
127     int index = v->current;
128     if (v->current + size > v->capacity)
129     {
130         LPBYTE pOldData = v->pData;
131         v->capacity = (v->capacity + size) * 2;
132         v->pData = CoTaskMemAlloc(v->capacity);
133         memcpy(v->pData, pOldData, v->current);
134         CoTaskMemFree(pOldData);
135     }
136     memcpy(v->pData + v->current, pData, size);
137     v->current += size;
138     return index;
139 }
140
141 static int find_data(struct Vector * v, const BYTE * pData, int size)
142 {
143     int index;
144     for (index = 0; index < v->current; index++)
145         if (!memcmp(v->pData + index, pData, size))
146             return index;
147     /* not found */
148     return -1;
149 }
150
151 static void delete_vector(struct Vector * v)
152 {
153     if (v->pData)
154         CoTaskMemFree(v->pData);
155     v->current = 0;
156     v->capacity = 0;
157 }
158
159 HRESULT FilterMapper2_create(IUnknown *pUnkOuter, LPVOID *ppObj)
160 {
161     FilterMapper2Impl * pFM2impl;
162
163     TRACE("(%p, %p)\n", pUnkOuter, ppObj);
164
165     if (pUnkOuter)
166         return CLASS_E_NOAGGREGATION;
167
168     pFM2impl = CoTaskMemAlloc(sizeof(*pFM2impl));
169     if (!pFM2impl)
170         return E_OUTOFMEMORY;
171
172     pFM2impl->lpVtbl = &fm2vtbl;
173     pFM2impl->lpVtblFilterMapper = &fmvtbl;
174     pFM2impl->refCount = 1;
175
176     *ppObj = pFM2impl;
177
178     TRACE("-- created at %p\n", pFM2impl);
179
180     return S_OK;
181 }
182
183 /*** IUnknown methods ***/
184
185 static HRESULT WINAPI FilterMapper2_QueryInterface(IFilterMapper2 * iface, REFIID riid, LPVOID *ppv)
186 {
187     FilterMapper2Impl *This = (FilterMapper2Impl *)iface;
188
189     TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
190
191     *ppv = NULL;
192
193     if (IsEqualIID(riid, &IID_IUnknown))
194         *ppv = iface;
195     else if (IsEqualIID(riid, &IID_IFilterMapper2))
196         *ppv = iface;
197     else if (IsEqualIID(riid, &IID_IFilterMapper))
198         *ppv = &This->lpVtblFilterMapper;
199
200     if (*ppv != NULL)
201     {
202         IUnknown_AddRef((IUnknown *)*ppv);
203         return S_OK;
204     }
205
206     FIXME("No interface for %s\n", debugstr_guid(riid));
207     return E_NOINTERFACE;
208 }
209
210 static ULONG WINAPI FilterMapper2_AddRef(IFilterMapper2 * iface)
211 {
212     FilterMapper2Impl *This = (FilterMapper2Impl *)iface;
213     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     HRESULT hr;
248
249     TRACE("(%s, %lx, %s)\n", debugstr_guid(clsidCategory), dwCategoryMerit, debugstr_w(szDescription));
250
251     hr = StringFromCLSID(&CLSID_ActiveMovieCategories, &wClsidAMCat);
252
253     if (SUCCEEDED(hr))
254     {
255         hr = StringFromCLSID(clsidCategory, &wClsidCategory);
256     }
257
258     if (SUCCEEDED(hr))
259     {
260         strcpyW(wszKeyName, wszClsidSlash);
261         strcatW(wszKeyName, wClsidAMCat);
262         strcatW(wszKeyName, wszSlashInstance);
263         strcatW(wszKeyName, wClsidCategory);
264
265         hr = HRESULT_FROM_WIN32(RegCreateKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL));
266     }
267
268     if (SUCCEEDED(hr))
269     {
270         hr = HRESULT_FROM_WIN32(RegSetValueExW(hKey, wszFriendlyName, 0, REG_SZ, (const BYTE*)szDescription, (strlenW(szDescription) + 1) * sizeof(WCHAR)));
271     }
272
273     if (SUCCEEDED(hr))
274     {
275         hr = HRESULT_FROM_WIN32(RegSetValueExW(hKey, wszClsidName, 0, REG_SZ, (LPBYTE)wClsidCategory, (strlenW(wClsidCategory) + 1) * sizeof(WCHAR)));
276     }
277
278     if (SUCCEEDED(hr))
279     {
280         hr = HRESULT_FROM_WIN32(RegSetValueExW(hKey, wszMeritName, 0, REG_DWORD, (LPBYTE)&dwCategoryMerit, sizeof(dwCategoryMerit)));
281     }
282
283     CloseHandle(hKey);
284
285     if (wClsidCategory)
286         CoTaskMemFree(wClsidCategory);
287     if (wClsidAMCat)
288         CoTaskMemFree(wClsidAMCat);
289
290     return hr;
291 }
292
293 static HRESULT WINAPI FilterMapper2_UnregisterFilter(
294     IFilterMapper2 * iface,
295     const CLSID *pclsidCategory,
296     const OLECHAR *szInstance,
297     REFCLSID Filter)
298 {
299     WCHAR wszKeyName[MAX_PATH];
300     LPWSTR wClsidCategory = NULL;
301     LPWSTR wFilter = NULL;
302     HRESULT hr;
303
304     TRACE("(%p, %s, %s)\n", pclsidCategory, debugstr_w(szInstance), debugstr_guid(Filter));
305
306     if (!pclsidCategory)
307         pclsidCategory = &CLSID_LegacyAmFilterCategory;
308
309     hr = StringFromCLSID(pclsidCategory, &wClsidCategory);
310
311     if (SUCCEEDED(hr))
312     {
313         strcpyW(wszKeyName, wszClsidSlash);
314         strcatW(wszKeyName, wClsidCategory);
315         strcatW(wszKeyName, wszSlashInstance);
316         if (szInstance)
317             strcatW(wszKeyName, szInstance);
318         else
319         {
320             hr = StringFromCLSID(Filter, &wFilter);
321             if (SUCCEEDED(hr))
322                 strcatW(wszKeyName, wFilter);
323         }
324     }
325
326     if (SUCCEEDED(hr))
327     {
328         hr = HRESULT_FROM_WIN32(RegDeleteKeyW(HKEY_CLASSES_ROOT, wszKeyName));
329     }
330
331     if (wClsidCategory)
332         CoTaskMemFree(wClsidCategory);
333     if (wFilter)
334         CoTaskMemFree(wFilter);
335
336     return hr;
337 }
338
339 static HRESULT FM2_WriteFriendlyName(IPropertyBag * pPropBag, LPCWSTR szName)
340 {
341     VARIANT var;
342
343     V_VT(&var) = VT_BSTR;
344     V_UNION(&var, bstrVal) = (BSTR)szName;
345
346     return IPropertyBag_Write(pPropBag, wszFriendlyName, &var);
347 }
348
349 static HRESULT FM2_WriteClsid(IPropertyBag * pPropBag, REFCLSID clsid)
350 {
351     LPWSTR wszClsid = NULL;
352     VARIANT var;
353     HRESULT hr;
354
355     hr = StringFromCLSID(clsid, &wszClsid);
356
357     if (SUCCEEDED(hr))
358     {
359         V_VT(&var) = VT_BSTR;
360         V_UNION(&var, bstrVal) = wszClsid;
361         hr = IPropertyBag_Write(pPropBag, wszClsidName, &var);
362     }
363     if (wszClsid)
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 %ld 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 = %ld, merit = %lx, #pins = %ld, unused = %lx\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(prrfp->signature, 4));
545
546             TRACE("\tpin[%ld]: flags = %lx, instances = %ld, media types = %ld, mediums = %ld\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(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             if (szClsidTemp)
744             {
745                 CoTaskMemFree(szClsidTemp);
746                 szClsidTemp = NULL;
747             }
748             hr = StringFromCLSID(clsidFilter, &szClsidTemp);
749             if (SUCCEEDED(hr))
750                 strcpyW(pCurrent+1, szClsidTemp);
751         }
752     }
753
754     if (SUCCEEDED(hr))
755         hr = CoCreateInstance(&CLSID_CDeviceMoniker, NULL, CLSCTX_INPROC, &IID_IParseDisplayName, (LPVOID *)&pParser);
756
757     if (SUCCEEDED(hr))
758         hr = CreateBindCtx(0, &pBindCtx);
759
760     if (SUCCEEDED(hr))
761         hr = IParseDisplayName_ParseDisplayName(pParser, pBindCtx, pwszParseName, &ulEaten, &pMoniker);
762
763     if (pBindCtx)
764         IBindCtx_Release(pBindCtx);
765     if (pParser)
766         IParseDisplayName_Release(pParser);
767
768     if (SUCCEEDED(hr))
769         hr = IMoniker_BindToStorage(pMoniker, NULL, NULL, &IID_IPropertyBag, (LPVOID)&pPropBag);
770
771     if (SUCCEEDED(hr))
772         hr = FM2_WriteFriendlyName(pPropBag, szName);
773
774     if (SUCCEEDED(hr))
775         hr = FM2_WriteClsid(pPropBag, clsidFilter);
776
777     if (SUCCEEDED(hr))
778         hr = FM2_WriteFilterData(pPropBag, &regfilter2);
779
780     if (pPropBag)
781         IPropertyBag_Release(pPropBag);
782     if (szClsidTemp)
783         CoTaskMemFree(szClsidTemp);
784
785     if (SUCCEEDED(hr) && ppMoniker)
786         *ppMoniker = pMoniker;
787     else if (pMoniker)
788         IMoniker_Release(pMoniker);
789
790     if (pregfp2)
791         CoTaskMemFree(pregfp2);
792
793     TRACE("-- returning %lx\n", hr);
794
795     return hr;
796 }
797
798 /* internal helper function */
799 static BOOL MatchTypes(
800     BOOL bExactMatch,
801     DWORD nPinTypes,
802     const REGPINTYPES * pPinTypes,
803     DWORD nMatchTypes,
804     const GUID * pMatchTypes)
805 {
806     BOOL bMatch = FALSE;
807     DWORD j;
808
809     if ((nMatchTypes == 0) && (nPinTypes > 0))
810         bMatch = TRUE;
811
812     for (j = 0; j < nPinTypes; j++)
813     {
814         DWORD i;
815         for (i = 0; i < nMatchTypes*2; i+=2)
816         {
817             if (((!bExactMatch && IsEqualGUID(pPinTypes[j].clsMajorType, &GUID_NULL)) || IsEqualGUID(&pMatchTypes[i], &GUID_NULL) || IsEqualGUID(pPinTypes[j].clsMajorType, &pMatchTypes[i])) &&
818                 ((!bExactMatch && IsEqualGUID(pPinTypes[j].clsMinorType, &GUID_NULL)) || IsEqualGUID(&pMatchTypes[i+1], &GUID_NULL) || IsEqualGUID(pPinTypes[j].clsMinorType, &pMatchTypes[i+1])))
819             {
820                 bMatch = TRUE;
821                 break;
822             }
823         }
824     }
825     return bMatch;
826 }
827
828 /* internal helper function for qsort of MONIKER_MERIT array */
829 static int mm_compare(const void * left, const void * right)
830 {
831     const struct MONIKER_MERIT * mmLeft = (const struct MONIKER_MERIT *)left;
832     const struct MONIKER_MERIT * mmRight = (const struct MONIKER_MERIT *)right;
833
834     if (mmLeft->dwMerit == mmRight->dwMerit)
835         return 0;
836     if (mmLeft->dwMerit > mmRight->dwMerit)
837         return -1;
838     return 1;
839 }
840
841 /* NOTES:
842  *   Exact match means whether or not to treat GUID_NULL's in filter data as wild cards
843  *    (GUID_NULL's in input to function automatically treated as wild cards)
844  *   Input/Output needed means match only on criteria if TRUE (with zero input types
845  *    meaning match any input/output pin as long as one exists), otherwise match any
846  *    filter that meets the rest of the requirements.
847  */
848 static HRESULT WINAPI FilterMapper2_EnumMatchingFilters(
849     IFilterMapper2 * iface,
850     IEnumMoniker **ppEnum,
851     DWORD dwFlags,
852     BOOL bExactMatch,
853     DWORD dwMerit,
854     BOOL bInputNeeded,
855     DWORD cInputTypes,
856     const GUID *pInputTypes,
857     const REGPINMEDIUM *pMedIn,
858     const CLSID *pPinCategoryIn,
859     BOOL bRender,
860     BOOL bOutputNeeded,
861     DWORD cOutputTypes,
862     const GUID *pOutputTypes,
863     const REGPINMEDIUM *pMedOut,
864     const CLSID *pPinCategoryOut)
865 {
866     ICreateDevEnum * pCreateDevEnum;
867     IMoniker * pMonikerCat;
868     IEnumMoniker * pEnumCat;
869     HRESULT hr;
870     struct Vector monikers = {NULL, 0, 0};
871
872     TRACE("(%p, %lx, %s, %lx, %s, %ld, %p, %p, %p, %s, %s, %p, %p, %p)\n",
873         ppEnum,
874         dwFlags,
875         bExactMatch ? "true" : "false",
876         dwMerit,
877         bInputNeeded ? "true" : "false",
878         cInputTypes,
879         pInputTypes,
880         pMedIn,
881         pPinCategoryIn,
882         bRender ? "true" : "false",
883         bOutputNeeded ? "true" : "false",
884         pOutputTypes,
885         pMedOut,
886         pPinCategoryOut);
887
888     if (dwFlags != 0)
889     {
890         FIXME("dwFlags = %lx not implemented\n", dwFlags);
891     }
892
893     *ppEnum = NULL;
894
895     hr = CoCreateInstance(&CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC, &IID_ICreateDevEnum, (LPVOID*)&pCreateDevEnum);
896
897     if (SUCCEEDED(hr))
898         hr = ICreateDevEnum_CreateClassEnumerator(pCreateDevEnum, &CLSID_ActiveMovieCategories, &pEnumCat, 0);
899
900     while (IEnumMoniker_Next(pEnumCat, 1, &pMonikerCat, NULL) == S_OK)
901     {
902         IPropertyBag * pPropBagCat = NULL;
903         VARIANT var;
904         HRESULT hrSub; /* this is so that one buggy filter
905                           doesn't make the whole lot fail */
906
907         VariantInit(&var);
908
909         hrSub = IMoniker_BindToStorage(pMonikerCat, NULL, NULL, &IID_IPropertyBag, (LPVOID*)&pPropBagCat);
910
911         if (SUCCEEDED(hrSub))
912             hrSub = IPropertyBag_Read(pPropBagCat, wszMeritName, &var, NULL);
913
914         if (SUCCEEDED(hrSub) && (V_UNION(&var, ulVal) >= dwMerit))
915         {
916             CLSID clsidCat;
917             IEnumMoniker * pEnum;
918             IMoniker * pMoniker;
919
920             VariantClear(&var);
921
922             if (TRACE_ON(quartz))
923             {
924                 VARIANT temp;
925                 V_VT(&temp) = VT_EMPTY;
926                 IPropertyBag_Read(pPropBagCat, wszFriendlyName, &temp, NULL);
927                 TRACE("Considering category %s\n", debugstr_w(V_UNION(&temp, bstrVal)));
928                 VariantClear(&temp);
929             }
930
931             hrSub = IPropertyBag_Read(pPropBagCat, wszClsidName, &var, NULL);
932
933             if (SUCCEEDED(hrSub))
934                 hrSub = CLSIDFromString(V_UNION(&var, bstrVal), &clsidCat);
935
936             if (SUCCEEDED(hrSub))
937                 hrSub = ICreateDevEnum_CreateClassEnumerator(pCreateDevEnum, &clsidCat, &pEnum, 0);
938
939             if (hrSub == S_OK)
940             {
941                 while (IEnumMoniker_Next(pEnum, 1, &pMoniker, NULL) == S_OK)
942                 {
943                     IPropertyBag * pPropBag = NULL;
944                     REGFILTER2 rf2;
945                     DWORD i;
946                     BOOL bInputMatch = !bInputNeeded;
947                     BOOL bOutputMatch = !bOutputNeeded;
948
949                     ZeroMemory(&rf2, sizeof(rf2));
950
951                     hrSub = IMoniker_BindToStorage(pMoniker, NULL, NULL, &IID_IPropertyBag, (LPVOID*)&pPropBag);
952
953                     if (TRACE_ON(quartz))
954                     {
955                         VARIANT temp;
956                         V_VT(&temp) = VT_EMPTY;
957                         IPropertyBag_Read(pPropBag, wszFriendlyName, &temp, NULL);
958                         TRACE("Considering filter %s\n", debugstr_w(V_UNION(&temp, bstrVal)));
959                         VariantClear(&temp);
960                     }
961
962                     if (SUCCEEDED(hrSub))
963                         hrSub = FM2_ReadFilterData(pPropBag, &rf2);
964
965                     /* Logic used for bInputMatch expression:
966                      * There exists some pin such that bInputNeeded implies (pin is an input and
967                      * (bRender implies pin has render flag) and major/minor types members of
968                      * pInputTypes )
969                      * bOutputMatch is similar, but without the "bRender implies ..." part
970                      * and substituting variables names containing input for output
971                      */
972
973                     /* determine whether filter meets requirements */
974                     if (SUCCEEDED(hrSub) && (rf2.dwMerit >= dwMerit))
975                     {
976                         for (i = 0; (i < rf2.u.s1.cPins2) && (!bInputMatch || !bOutputMatch); i++)
977                         {
978                             const REGFILTERPINS2 * rfp2 = rf2.u.s1.rgPins2 + i;
979
980                             bInputMatch = bInputMatch || (!(rfp2->dwFlags & REG_PINFLAG_B_OUTPUT) &&
981                                 (!bRender || (rfp2->dwFlags & REG_PINFLAG_B_RENDERER)) &&
982                                 MatchTypes(bExactMatch, rfp2->nMediaTypes, rfp2->lpMediaType, cInputTypes, pInputTypes));
983                             bOutputMatch = bOutputMatch || ((rfp2->dwFlags & REG_PINFLAG_B_OUTPUT) &&
984                                 MatchTypes(bExactMatch, rfp2->nMediaTypes, rfp2->lpMediaType, cOutputTypes, pOutputTypes));
985                         }
986
987                         if (bInputMatch && bOutputMatch)
988                         {
989                             struct MONIKER_MERIT mm = {pMoniker, rf2.dwMerit};
990                             IMoniker_AddRef(pMoniker);
991                             add_data(&monikers, (LPBYTE)&mm, sizeof(mm));
992                         }
993                     }
994
995                     FM2_DeleteRegFilter(&rf2);
996                     if (pPropBag)
997                         IPropertyBag_Release(pPropBag);
998                     IMoniker_Release(pMoniker);
999                 }
1000                 IEnumMoniker_Release(pEnum);
1001             }
1002         }
1003
1004         VariantClear(&var);
1005         if (pPropBagCat)
1006             IPropertyBag_Release(pPropBagCat);
1007         IMoniker_Release(pMonikerCat);
1008     }
1009
1010     if (SUCCEEDED(hr))
1011     {
1012         IMoniker ** ppMoniker;
1013         unsigned int i;
1014         ULONG nMonikerCount = monikers.current / sizeof(struct MONIKER_MERIT);
1015
1016         /* sort the monikers in descending merit order */
1017         qsort(monikers.pData, nMonikerCount,
1018               sizeof(struct MONIKER_MERIT),
1019               mm_compare);
1020
1021         /* construct an IEnumMoniker interface */
1022         ppMoniker = CoTaskMemAlloc(nMonikerCount * sizeof(IMoniker *));
1023         for (i = 0; i < nMonikerCount; i++)
1024         {
1025             /* no need to AddRef here as already AddRef'd above */
1026             ppMoniker[i] = ((struct MONIKER_MERIT *)monikers.pData)[i].pMoniker;
1027         }
1028         hr = EnumMonikerImpl_Create(ppMoniker, nMonikerCount, ppEnum);
1029         CoTaskMemFree(ppMoniker);
1030     }
1031
1032     delete_vector(&monikers);
1033     IEnumMoniker_Release(pEnumCat);
1034     ICreateDevEnum_Release(pCreateDevEnum);
1035
1036     return hr;
1037 }
1038
1039 static IFilterMapper2Vtbl fm2vtbl =
1040 {
1041
1042     FilterMapper2_QueryInterface,
1043     FilterMapper2_AddRef,
1044     FilterMapper2_Release,
1045
1046     FilterMapper2_CreateCategory,
1047     FilterMapper2_UnregisterFilter,
1048     FilterMapper2_RegisterFilter,
1049     FilterMapper2_EnumMatchingFilters
1050 };
1051
1052 /*** IUnknown methods ***/
1053
1054 static HRESULT WINAPI FilterMapper_QueryInterface(IFilterMapper * iface, REFIID riid, LPVOID *ppv)
1055 {
1056     ICOM_THIS_From_IFilterMapper(FilterMapper2Impl, iface);
1057
1058     TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
1059
1060     return FilterMapper2_QueryInterface((IFilterMapper2*)&This->lpVtbl, riid, ppv);
1061 }
1062
1063 static ULONG WINAPI FilterMapper_AddRef(IFilterMapper * iface)
1064 {
1065     ICOM_THIS_From_IFilterMapper(FilterMapper2Impl, iface);
1066
1067     return FilterMapper2_AddRef((IFilterMapper2*)This);
1068 }
1069
1070 static ULONG WINAPI FilterMapper_Release(IFilterMapper * iface)
1071 {
1072     ICOM_THIS_From_IFilterMapper(FilterMapper2Impl, iface);
1073
1074     return FilterMapper2_Release((IFilterMapper2*)This);
1075 }
1076
1077 /*** IFilterMapper methods ***/
1078
1079 static HRESULT WINAPI FilterMapper_EnumMatchingFilters(
1080     IFilterMapper * iface,
1081     IEnumRegFilters **ppEnum,
1082     DWORD dwMerit,
1083     BOOL bInputNeeded,
1084     CLSID clsInMaj,
1085     CLSID clsInSub,
1086     BOOL bRender,
1087     BOOL bOutputNeeded,
1088     CLSID clsOutMaj,
1089     CLSID clsOutSub)
1090 {
1091     ICOM_THIS_From_IFilterMapper(FilterMapper2Impl, iface);
1092     GUID InputType[2];
1093     GUID OutputType[2];
1094     IEnumMoniker* ppEnumMoniker;
1095     IMoniker* IMon;
1096     ULONG nb;
1097     ULONG idx = 0, nb_mon = 0;
1098     REGFILTER* regfilters;
1099     HRESULT hr;
1100
1101     TRACE("(%p/%p)->(%p, %lx, %s, %s, %s, %s, %s, %s, %s) stub!\n",
1102         iface,This,
1103         ppEnum,
1104         dwMerit,
1105         bInputNeeded ? "true" : "false",
1106         debugstr_guid(&clsInMaj),
1107         debugstr_guid(&clsInSub),
1108         bRender ? "true" : "false",
1109         bOutputNeeded ? "true" : "false",
1110         debugstr_guid(&clsOutMaj),
1111         debugstr_guid(&clsOutSub));
1112
1113     InputType[0] = clsInMaj;
1114     InputType[1] = clsInSub;
1115     OutputType[0] = clsOutMaj;
1116     OutputType[1] = clsOutSub;
1117
1118     hr = IFilterMapper2_EnumMatchingFilters((IFilterMapper2*)This,
1119                                        &ppEnumMoniker,
1120                                        0,
1121                                        TRUE,
1122                                        dwMerit,
1123                                        bInputNeeded,
1124                                        1,
1125                                        InputType,
1126                                        NULL,
1127                                        &GUID_NULL,
1128                                        bRender,
1129                                        bOutputNeeded,
1130                                        1,
1131                                        OutputType,
1132                                        NULL,
1133                                        &GUID_NULL);
1134
1135     if (!SUCCEEDED(hr))
1136         return hr;
1137     
1138     while(IEnumMoniker_Next(ppEnumMoniker, 1, &IMon, &nb) == S_OK)
1139     {
1140         IMoniker_Release(IMon);
1141         nb_mon++;
1142     }
1143
1144     *ppEnum = NULL;
1145     if (!nb_mon)
1146     {
1147         IEnumMoniker_Release(ppEnumMoniker);
1148         return IEnumRegFiltersImpl_Construct(NULL, 0, ppEnum);
1149     }
1150
1151     regfilters = CoTaskMemAlloc(nb_mon * sizeof(REGFILTER));
1152     if (!regfilters)
1153     {
1154         IEnumMoniker_Release(ppEnumMoniker);
1155         return E_OUTOFMEMORY;
1156     }
1157     
1158     IEnumMoniker_Reset(ppEnumMoniker);
1159     while(IEnumMoniker_Next(ppEnumMoniker, 1, &IMon, &nb) == S_OK)
1160     {
1161         IPropertyBag * pPropBagCat = NULL;
1162         VARIANT var;
1163         HRESULT hrSub;
1164         GUID clsid;
1165         int len;
1166
1167         VariantInit(&var);
1168         V_VT(&var) = VT_BSTR;
1169
1170         hrSub = IMoniker_BindToStorage(IMon, NULL, NULL, &IID_IPropertyBag, (LPVOID*)&pPropBagCat);
1171
1172         if (SUCCEEDED(hrSub))
1173             hrSub = IPropertyBag_Read(pPropBagCat, wszClsidName, &var, NULL);
1174
1175         if (SUCCEEDED(hrSub))
1176             hrSub = CLSIDFromString(V_UNION(&var, bstrVal), &clsid);
1177
1178         if (SUCCEEDED(hrSub))
1179             hrSub = IPropertyBag_Read(pPropBagCat, wszFriendlyName, &var, NULL);
1180
1181         if (SUCCEEDED(hrSub))
1182         {
1183             len = (strlenW((WCHAR*)&V_UNION(&var, bstrVal))+1) * sizeof(WCHAR);
1184             if (!(regfilters[idx].Name = CoTaskMemAlloc(len*2)))
1185                 hr = E_OUTOFMEMORY;
1186         }
1187
1188         if (SUCCEEDED(hrSub))
1189         {
1190             memcpy(regfilters[idx].Name, &V_UNION(&var, bstrVal), len);
1191             regfilters[idx].Clsid = clsid;
1192             idx++;
1193         }
1194
1195         if (pPropBagCat)
1196             IPropertyBag_Release(pPropBagCat);
1197         IMoniker_Release(IMon);
1198     }
1199
1200     /* In case of release all resources */
1201     if (!SUCCEEDED(hr))
1202     {
1203         for (idx = 0; idx < nb_mon; idx++)
1204             CoTaskMemFree(regfilters[idx].Name);
1205         CoTaskMemFree(regfilters);
1206         IEnumMoniker_Release(ppEnumMoniker);
1207         return hr;
1208     }
1209
1210     hr = IEnumRegFiltersImpl_Construct(regfilters, nb_mon, ppEnum);
1211     CoTaskMemFree(regfilters);
1212     IEnumMoniker_Release(ppEnumMoniker);
1213     
1214     return hr;
1215 }
1216
1217
1218 static HRESULT WINAPI FilterMapper_RegisterFilter(IFilterMapper * iface, CLSID clsid, LPCWSTR szName, DWORD dwMerit)
1219 {
1220     HRESULT hr;
1221     LPWSTR wszClsid = NULL;
1222     HKEY hKey;
1223     WCHAR wszKeyName[strlenW(wszFilterSlash) + (CHARS_IN_GUID-1) + 1];
1224
1225     TRACE("(%p)->(%s, %s, %lx)\n", iface, debugstr_guid(&clsid), debugstr_w(szName), dwMerit);
1226
1227     hr = StringFromCLSID(&clsid, &wszClsid);
1228
1229     if (SUCCEEDED(hr))
1230     {
1231         strcpyW(wszKeyName, wszFilterSlash);
1232         strcatW(wszKeyName, wszClsid);
1233     
1234         hr = HRESULT_FROM_WIN32(RegCreateKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL));
1235     }
1236
1237     if (SUCCEEDED(hr))
1238     {
1239         hr = HRESULT_FROM_WIN32(RegSetValueExW(hKey, NULL, 0, REG_SZ, (const BYTE*)szName, strlenW(szName) + 1));
1240         CloseHandle(hKey);
1241     }
1242
1243     if (SUCCEEDED(hr))
1244     {
1245         strcpyW(wszKeyName, wszClsidSlash);
1246         strcatW(wszKeyName, wszClsid);
1247     
1248         hr = HRESULT_FROM_WIN32(RegCreateKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL));
1249     }
1250
1251     if (SUCCEEDED(hr))
1252     {
1253         hr = HRESULT_FROM_WIN32(RegSetValueExW(hKey, wszMeritName, 0, REG_DWORD, (LPBYTE)&dwMerit, sizeof(dwMerit)));
1254         CloseHandle(hKey);
1255     }
1256     
1257     return hr;
1258 }
1259
1260 static HRESULT WINAPI FilterMapper_RegisterFilterInstance(IFilterMapper * iface, CLSID clsid, LPCWSTR szName, CLSID *MRId)
1261 {
1262     TRACE("(%p)->(%s, %s, %p)\n", iface, debugstr_guid(&clsid), debugstr_w(szName), MRId);
1263
1264     /* Not implemented in Windows (tested on Win2k) */
1265
1266     return E_NOTIMPL;
1267 }
1268
1269 static HRESULT WINAPI FilterMapper_RegisterPin(
1270     IFilterMapper * iface,
1271     CLSID Filter,
1272     LPCWSTR szName,
1273     BOOL bRendered,
1274     BOOL bOutput,
1275     BOOL bZero,
1276     BOOL bMany,
1277     CLSID ConnectsToFilter,
1278     LPCWSTR ConnectsToPin)
1279 {
1280     HRESULT hr;
1281     LPWSTR wszClsid = NULL;
1282     HKEY hKey = NULL;
1283     HKEY hPinsKey = NULL;
1284     WCHAR * wszPinsKeyName;
1285     WCHAR wszKeyName[strlenW(wszClsidSlash) + (CHARS_IN_GUID-1) + 1];
1286
1287     TRACE("(%p)->(%s, %s, %d, %d, %d, %d, %s, %s)\n", iface, debugstr_guid(&Filter), debugstr_w(szName), bRendered,
1288                 bOutput, bZero, bMany, debugstr_guid(&ConnectsToFilter), debugstr_w(ConnectsToPin));
1289
1290     hr = StringFromCLSID(&Filter, &wszClsid);
1291
1292     if (SUCCEEDED(hr))
1293     {
1294         strcpyW(wszKeyName, wszClsidSlash);
1295         strcatW(wszKeyName, wszClsid);
1296
1297         hr = HRESULT_FROM_WIN32(RegOpenKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, KEY_WRITE, &hKey));
1298     }
1299
1300     if (SUCCEEDED(hr))
1301     {
1302         wszPinsKeyName = CoTaskMemAlloc((strlenW(wszPins) + 1 + strlenW(szName) + 1) * 2);
1303         if (!wszPinsKeyName)
1304              hr = E_OUTOFMEMORY;
1305     }
1306
1307     if (SUCCEEDED(hr))
1308     {
1309         strcpyW(wszPinsKeyName, wszPins);
1310         strcatW(wszPinsKeyName, wszSlash);
1311         strcatW(wszPinsKeyName, szName);
1312     
1313         hr = HRESULT_FROM_WIN32(RegCreateKeyExW(hKey, wszPinsKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hPinsKey, NULL));
1314         CoTaskMemFree(wszPinsKeyName);
1315     }
1316
1317     if (SUCCEEDED(hr))
1318     {
1319         hr = HRESULT_FROM_WIN32(RegSetValueExW(hPinsKey, wszAllowedMany, 0, REG_DWORD, (LPBYTE)&bMany, sizeof(bMany)));
1320     }
1321
1322     if (SUCCEEDED(hr))
1323     {
1324         hr = HRESULT_FROM_WIN32(RegSetValueExW(hPinsKey, wszAllowedZero, 0, REG_DWORD, (LPBYTE)&bZero, sizeof(bZero)));
1325     }
1326
1327     if (SUCCEEDED(hr))
1328     {
1329         hr = HRESULT_FROM_WIN32(RegSetValueExW(hPinsKey, wszDirection, 0, REG_DWORD, (LPBYTE)&bOutput, sizeof(bOutput)));
1330     }
1331
1332     if (SUCCEEDED(hr))
1333     {
1334         hr = HRESULT_FROM_WIN32(RegSetValueExW(hPinsKey, wszIsRendered, 0, REG_DWORD, (LPBYTE)&bRendered, sizeof(bRendered)));
1335     }
1336
1337     if (SUCCEEDED(hr))
1338     {
1339         hr = HRESULT_FROM_WIN32(RegCreateKeyExW(hPinsKey, wszTypes, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, NULL, NULL));
1340     }
1341
1342     if (wszClsid)
1343         CoTaskMemFree(wszClsid);
1344     if (hKey)
1345         CloseHandle(hKey);
1346     if (hPinsKey)
1347         CloseHandle(hPinsKey);
1348
1349     return hr;
1350 }
1351
1352
1353 static HRESULT WINAPI FilterMapper_RegisterPinType(
1354     IFilterMapper * iface,
1355     CLSID clsFilter,
1356     LPCWSTR szName,
1357     CLSID clsMajorType,
1358     CLSID clsSubType)
1359 {
1360     HRESULT hr;
1361     LPWSTR wszClsid = NULL;
1362     LPWSTR wszClsidMajorType = NULL;
1363     LPWSTR wszClsidSubType = NULL;
1364     HKEY hKey = NULL;
1365     WCHAR * wszTypesKey;
1366     WCHAR wszKeyName[strlenW(wszClsidSlash) + (CHARS_IN_GUID-1) + 1];
1367
1368     TRACE("(%p)->(%s, %s, %s, %s)\n", iface, debugstr_guid(&clsFilter), debugstr_w(szName),
1369                     debugstr_guid(&clsMajorType), debugstr_guid(&clsSubType));
1370
1371     hr = StringFromCLSID(&clsFilter, &wszClsid);
1372
1373     if (SUCCEEDED(hr))
1374     {
1375         hr = StringFromCLSID(&clsMajorType, &wszClsidMajorType);
1376     }
1377
1378     if (SUCCEEDED(hr))
1379     {
1380         hr = StringFromCLSID(&clsSubType, &wszClsidSubType);
1381     }
1382
1383     if (SUCCEEDED(hr))
1384     {
1385         wszTypesKey = CoTaskMemAlloc((strlenW(wszClsidSlash) + strlenW(wszClsid) + strlenW(wszPins) +
1386                         strlenW(szName) + strlenW(wszTypes) + 3 + 1) * 2);
1387         if (!wszTypesKey)
1388             hr = E_OUTOFMEMORY;
1389     }
1390
1391     if (SUCCEEDED(hr))
1392     {
1393         strcpyW(wszTypesKey, wszClsidSlash);
1394         strcatW(wszTypesKey, wszClsid);
1395         strcatW(wszTypesKey, wszSlash);
1396         strcatW(wszTypesKey, wszPins);
1397         strcatW(wszTypesKey, wszSlash);
1398         strcatW(wszTypesKey, szName);
1399         strcatW(wszTypesKey, wszSlash);
1400         strcatW(wszTypesKey, wszTypes);
1401
1402         hr = HRESULT_FROM_WIN32(RegOpenKeyExW(HKEY_CLASSES_ROOT, wszTypesKey, 0, KEY_WRITE, &hKey));
1403         CoTaskMemFree(wszTypesKey);
1404     }
1405
1406     if (SUCCEEDED(hr))
1407     {
1408         strcpyW(wszKeyName, wszClsidMajorType);
1409         strcatW(wszKeyName, wszSlash);
1410         strcatW(wszKeyName, wszClsidSubType);
1411
1412         hr = HRESULT_FROM_WIN32(RegCreateKeyExW(hKey, wszKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, NULL, NULL));
1413         CloseHandle(hKey);
1414     }
1415
1416     if (wszClsid)
1417         CoTaskMemFree(wszClsid);
1418     if (wszClsidMajorType)
1419         CoTaskMemFree(wszClsidMajorType);
1420     if (wszClsidSubType)
1421         CoTaskMemFree(wszClsidSubType);
1422
1423     return hr;
1424 }
1425
1426 static HRESULT WINAPI FilterMapper_UnregisterFilter(IFilterMapper * iface, CLSID Filter)
1427 {
1428     HRESULT hr;
1429     LPWSTR wszClsid = NULL;
1430     HKEY hKey;
1431     WCHAR wszKeyName[strlenW(wszClsidSlash) + (CHARS_IN_GUID-1) + 1];
1432
1433     TRACE("(%p)->(%s)\n", iface, debugstr_guid(&Filter));
1434
1435     hr = StringFromCLSID(&Filter, &wszClsid);
1436
1437     if (SUCCEEDED(hr))
1438     {
1439         hr = HRESULT_FROM_WIN32(RegOpenKeyExW(HKEY_CLASSES_ROOT, wszFilter, 0, KEY_WRITE, &hKey));
1440     }
1441
1442     if (SUCCEEDED(hr))
1443     {
1444         hr = HRESULT_FROM_WIN32(RegDeleteKeyW(hKey, wszClsid));
1445         CloseHandle(hKey);
1446     }
1447
1448     if (SUCCEEDED(hr))
1449     {
1450         strcpyW(wszKeyName, wszClsidSlash);
1451         strcatW(wszKeyName, wszClsid);
1452
1453         hr = HRESULT_FROM_WIN32(RegOpenKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, KEY_WRITE, &hKey));
1454     }
1455
1456     if (SUCCEEDED(hr))
1457     {
1458         hr = HRESULT_FROM_WIN32(RegDeleteKeyW(hKey, wszMeritName));
1459         CloseHandle(hKey);
1460     }
1461
1462     if (wszClsid)
1463         CoTaskMemFree(wszClsid);
1464
1465     return hr;
1466 }
1467
1468 static HRESULT WINAPI FilterMapper_UnregisterFilterInstance(IFilterMapper * iface, CLSID MRId)
1469 {
1470     TRACE("(%p)->(%s)\n", iface, debugstr_guid(&MRId));
1471
1472     /* Not implemented in Windows (tested on Win2k) */
1473
1474     return E_NOTIMPL;
1475 }
1476
1477 static HRESULT WINAPI FilterMapper_UnregisterPin(IFilterMapper * iface, CLSID Filter, LPCWSTR Name)
1478 {
1479     HRESULT hr;
1480     LPWSTR wszClsid = NULL;
1481     HKEY hKey = NULL;
1482     WCHAR * wszPinNameKey;
1483     WCHAR wszKeyName[strlenW(wszClsidSlash) + (CHARS_IN_GUID-1) + 1];
1484
1485     TRACE("(%p)->(%s, %s)\n", iface, debugstr_guid(&Filter), debugstr_w(Name));
1486
1487     if (!Name)
1488         return E_INVALIDARG;
1489
1490     hr = StringFromCLSID(&Filter, &wszClsid);
1491
1492     if (SUCCEEDED(hr))
1493     {
1494         strcpyW(wszKeyName, wszClsidSlash);
1495         strcatW(wszKeyName, wszClsid);
1496
1497         hr = HRESULT_FROM_WIN32(RegOpenKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, KEY_WRITE, &hKey));
1498     }
1499
1500     if (SUCCEEDED(hr))
1501     {
1502         wszPinNameKey = CoTaskMemAlloc((strlenW(wszPins) + 1 + strlenW(Name) + 1) * 2);
1503         if (!wszPinNameKey)
1504             hr = E_OUTOFMEMORY;
1505     }
1506
1507     if (SUCCEEDED(hr))
1508     {
1509         strcpyW(wszPinNameKey, wszPins);
1510         strcatW(wszPinNameKey, wszSlash);
1511         strcatW(wszPinNameKey, Name);
1512
1513         hr = HRESULT_FROM_WIN32(RegDeleteKeyW(hKey, wszPinNameKey));
1514         CoTaskMemFree(wszPinNameKey);
1515     }
1516
1517     if (wszClsid)
1518         CoTaskMemFree(wszClsid);
1519     if (hKey)
1520         CloseHandle(hKey);
1521
1522     return hr;
1523 }
1524
1525 static IFilterMapperVtbl fmvtbl =
1526 {
1527
1528     FilterMapper_QueryInterface,
1529     FilterMapper_AddRef,
1530     FilterMapper_Release,
1531
1532     FilterMapper_RegisterFilter,
1533     FilterMapper_RegisterFilterInstance,
1534     FilterMapper_RegisterPin,
1535     FilterMapper_RegisterPinType,
1536     FilterMapper_UnregisterFilter,
1537     FilterMapper_UnregisterFilterInstance,
1538     FilterMapper_UnregisterPin,
1539     FilterMapper_EnumMatchingFilters
1540 };