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