- define additional shell paths for CSIDL_... constants
[wine] / dlls / quartz / filtermapper.c
1 /*
2  * IFilterMapper & IFilterMapper2 Implementations
3  *
4  * Copyright 2003 Robert Shearman
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #define NONAMELESSUNION
22 #define NONAMELESSSTRUCT
23 #include <stdarg.h>
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "winreg.h"
29 #include "winerror.h"
30
31 #include "quartz_private.h"
32
33 #define COM_NO_WINDOWS_H
34 #include "ole2.h"
35 #include "olectl.h"
36 #include "strmif.h"
37 #include "wine/unicode.h"
38 #include "uuids.h"
39
40 #include "wine/debug.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
43
44 typedef struct FilterMapper2Impl
45 {
46     ICOM_VFIELD(IFilterMapper2);
47     ICOM_VTABLE(IFilterMapper) * lpVtblFilterMapper;
48     ULONG refCount;
49 } FilterMapper2Impl;
50
51 static struct ICOM_VTABLE(IFilterMapper2) fm2vtbl;
52 static struct ICOM_VTABLE(IFilterMapper) fmvtbl;
53
54 #define _IFilterMapper_Offset ((int)(&(((FilterMapper2Impl*)0)->lpVtblFilterMapper)))
55 #define ICOM_THIS_From_IFilterMapper(impl, iface) impl* This = (impl*)(((char*)iface)-_IFilterMapper_Offset)
56
57 static const WCHAR wszClsidSlash[] = {'C','L','S','I','D','\\',0};
58 static const WCHAR wszSlashInstance[] = {'\\','I','n','s','t','a','n','c','e','\\',0};
59
60 /* CLSID property in media category Moniker */
61 static const WCHAR wszClsidName[] = {'C','L','S','I','D',0};
62 /* FriendlyName property in media category Moniker */
63 static const WCHAR wszFriendlyName[] = {'F','r','i','e','n','d','l','y','N','a','m','e',0};
64 /* Merit property in media category Moniker (CLSID_ActiveMovieCategories only) */
65 static const WCHAR wszMeritName[] = {'M','e','r','i','t',0};
66 /* FilterData property in media category Moniker (not CLSID_ActiveMovieCategories) */
67 static const WCHAR wszFilterDataName[] = {'F','i','l','t','e','r','D','a','t','a',0};
68
69 /* registry format for REGFILTER2 */
70 struct REG_RF
71 {
72     DWORD dwVersion;
73     DWORD dwMerit;
74     DWORD dwPins;
75     DWORD dwUnused;
76 };
77
78 struct REG_RFP
79 {
80     BYTE signature[4]; /* e.g. "0pi3" */
81     DWORD dwFlags;
82     DWORD dwInstances;
83     DWORD dwMediaTypes;
84     DWORD dwMediums;
85     DWORD bCategory; /* is there a category clsid? */
86     /* optional: dwOffsetCategoryClsid */
87 };
88
89 struct REG_TYPE
90 {
91     BYTE signature[4]; /* e.g. "0ty3" */
92     DWORD dwUnused;
93     DWORD dwOffsetMajor;
94     DWORD dwOffsetMinor;
95 };
96
97 struct MONIKER_MERIT
98 {
99     IMoniker * pMoniker;
100     DWORD dwMerit;
101 };
102
103 struct Vector
104 {
105     LPBYTE pData;
106     int capacity; /* in bytes */
107     int current; /* pointer to next free byte */
108 };
109
110 /* returns the position it was added at */
111 static int add_data(struct Vector * v, const BYTE * pData, int size)
112 {
113     int index = v->current;
114     if (v->current + size > v->capacity)
115     {
116         LPBYTE pOldData = v->pData;
117         v->capacity = (v->capacity + size) * 2;
118         v->pData = CoTaskMemAlloc(v->capacity);
119         memcpy(v->pData, pOldData, v->current);
120         CoTaskMemFree(pOldData);
121     }
122     memcpy(v->pData + v->current, pData, size);
123     v->current += size;
124     return index;
125 }
126
127 static int find_data(struct Vector * v, const BYTE * pData, int size)
128 {
129     int index;
130     for (index = 0; index < v->current; index++)
131         if (!memcmp(v->pData + index, pData, size))
132             return index;
133     /* not found */
134     return -1;
135 }
136
137 static void delete_vector(struct Vector * v)
138 {
139     if (v->pData)
140         CoTaskMemFree(v->pData);
141     v->current = 0;
142     v->capacity = 0;
143 }
144
145 HRESULT FilterMapper2_create(IUnknown *pUnkOuter, LPVOID *ppObj)
146 {
147     FilterMapper2Impl * pFM2impl;
148
149     TRACE("(%p, %p)\n", pUnkOuter, ppObj);
150
151     if (pUnkOuter)
152         return CLASS_E_NOAGGREGATION;
153
154     pFM2impl = CoTaskMemAlloc(sizeof(*pFM2impl));
155     if (!pFM2impl)
156         return E_OUTOFMEMORY;
157
158     pFM2impl->lpVtbl = &fm2vtbl;
159     pFM2impl->lpVtblFilterMapper = &fmvtbl;
160     pFM2impl->refCount = 1;
161
162     *ppObj = pFM2impl;
163
164     TRACE("-- created at %p\n", pFM2impl);
165
166     return S_OK;
167 }
168
169 /*** IUnknown methods ***/
170
171 static HRESULT WINAPI FilterMapper2_QueryInterface(IFilterMapper2 * iface, REFIID riid, LPVOID *ppv)
172 {
173     ICOM_THIS(FilterMapper2Impl, iface);
174
175     TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
176
177     *ppv = NULL;
178
179     if (IsEqualIID(riid, &IID_IUnknown))
180         *ppv = iface;
181     else if (IsEqualIID(riid, &IID_IFilterMapper2))
182         *ppv = iface;
183     else if (IsEqualIID(riid, &IID_IFilterMapper))
184         *ppv = &This->lpVtblFilterMapper;
185
186     if (*ppv != NULL)
187     {
188         IUnknown_AddRef((IUnknown *)*ppv);
189         return S_OK;
190     }
191
192     FIXME("No interface for %s\n", debugstr_guid(riid));
193     return E_NOINTERFACE;
194 }
195
196 static ULONG WINAPI FilterMapper2_AddRef(IFilterMapper2 * iface)
197 {
198     ICOM_THIS(FilterMapper2Impl, iface);
199
200     TRACE("()\n");
201
202     return InterlockedIncrement(&This->refCount);
203 }
204
205 static ULONG WINAPI FilterMapper2_Release(IFilterMapper2 * iface)
206 {
207     ICOM_THIS(FilterMapper2Impl, iface);
208
209     TRACE("()\n");
210
211     if (InterlockedDecrement(&This->refCount) == 0)
212     {
213         CoTaskMemFree(This);
214         return 0;
215     }
216     return This->refCount;
217 }
218
219 /*** IFilterMapper2 methods ***/
220
221 static HRESULT WINAPI FilterMapper2_CreateCategory(
222     IFilterMapper2 * iface,
223     REFCLSID clsidCategory,
224     DWORD dwCategoryMerit,
225     LPCWSTR szDescription)
226 {
227     LPWSTR wClsidAMCat = NULL;
228     LPWSTR wClsidCategory = NULL;
229     WCHAR wszKeyName[strlenW(wszClsidSlash) + strlenW(wszSlashInstance) + (CHARS_IN_GUID-1) * 2 + 1];
230     HKEY hKey = NULL;
231     HRESULT hr;
232
233     TRACE("(%s, %lx, %s)\n", debugstr_guid(clsidCategory), dwCategoryMerit, debugstr_w(szDescription));
234
235     hr = StringFromCLSID(&CLSID_ActiveMovieCategories, &wClsidAMCat);
236
237     if (SUCCEEDED(hr))
238     {
239         hr = StringFromCLSID(clsidCategory, &wClsidCategory);
240     }
241
242     if (SUCCEEDED(hr))
243     {
244         strcpyW(wszKeyName, wszClsidSlash);
245         strcatW(wszKeyName, wClsidAMCat);
246         strcatW(wszKeyName, wszSlashInstance);
247         strcatW(wszKeyName, wClsidCategory);
248
249         hr = HRESULT_FROM_WIN32(RegCreateKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL));
250     }
251
252     if (SUCCEEDED(hr))
253     {
254         hr = HRESULT_FROM_WIN32(RegSetValueExW(hKey, wszFriendlyName, 0, REG_SZ, (LPBYTE)szDescription, strlenW(szDescription) + 1));
255     }
256
257     if (SUCCEEDED(hr))
258     {
259         hr = HRESULT_FROM_WIN32(RegSetValueExW(hKey, wszClsidName, 0, REG_SZ, (LPBYTE)wClsidCategory, strlenW(wClsidCategory) + 1));
260     }
261
262     if (SUCCEEDED(hr))
263     {
264         hr = HRESULT_FROM_WIN32(RegSetValueExW(hKey, wszMeritName, 0, REG_DWORD, (LPBYTE)&dwCategoryMerit, sizeof(dwCategoryMerit)));
265     }
266
267     CloseHandle(hKey);
268
269     if (wClsidCategory)
270         CoTaskMemFree(wClsidCategory);
271     if (wClsidAMCat)
272         CoTaskMemFree(wClsidAMCat);
273
274     return hr;
275 }
276
277 static HRESULT WINAPI FilterMapper2_UnregisterFilter(
278     IFilterMapper2 * iface,
279     const CLSID *pclsidCategory,
280     const OLECHAR *szInstance,
281     REFCLSID Filter)
282 {
283     WCHAR wszKeyName[MAX_PATH];
284     LPWSTR wClsidCategory = NULL;
285     LPWSTR wFilter = NULL;
286     HRESULT hr;
287
288     TRACE("(%p, %s, %s)\n", pclsidCategory, debugstr_w(szInstance), debugstr_guid(Filter));
289
290     if (!pclsidCategory)
291         pclsidCategory = &CLSID_LegacyAmFilterCategory;
292
293     hr = StringFromCLSID(pclsidCategory, &wClsidCategory);
294
295     if (SUCCEEDED(hr))
296     {
297         strcpyW(wszKeyName, wszClsidSlash);
298         strcatW(wszKeyName, wClsidCategory);
299         strcatW(wszKeyName, wszSlashInstance);
300         if (szInstance)
301             strcatW(wszKeyName, szInstance);
302         else
303         {
304             hr = StringFromCLSID(Filter, &wFilter);
305             if (SUCCEEDED(hr))
306                 strcatW(wszKeyName, wFilter);
307         }
308     }
309
310     if (SUCCEEDED(hr))
311     {
312         hr = HRESULT_FROM_WIN32(RegDeleteKeyW(HKEY_CLASSES_ROOT, wszKeyName));
313     }
314
315     if (wClsidCategory)
316         CoTaskMemFree(wClsidCategory);
317     if (wFilter)
318         CoTaskMemFree(wFilter);
319
320     return hr;
321 }
322
323 static HRESULT FM2_WriteFriendlyName(IPropertyBag * pPropBag, LPCWSTR szName)
324 {
325     VARIANT var;
326
327     V_VT(&var) = VT_BSTR;
328     V_UNION(&var, bstrVal) = (BSTR)szName;
329
330     return IPropertyBag_Write(pPropBag, wszFriendlyName, &var);
331 }
332
333 static HRESULT FM2_WriteClsid(IPropertyBag * pPropBag, REFCLSID clsid)
334 {
335     LPWSTR wszClsid = NULL;
336     VARIANT var;
337     HRESULT hr;
338
339     hr = StringFromCLSID(clsid, &wszClsid);
340
341     if (SUCCEEDED(hr))
342     {
343         V_VT(&var) = VT_BSTR;
344         V_UNION(&var, bstrVal) = wszClsid;
345         hr = IPropertyBag_Write(pPropBag, wszClsidName, &var);
346     }
347     if (wszClsid)
348         CoTaskMemFree(wszClsid);
349     return hr;
350 }
351
352 static HRESULT FM2_WriteFilterData(IPropertyBag * pPropBag, const REGFILTER2 * prf2)
353 {
354     VARIANT var;
355     int size = sizeof(struct REG_RF);
356     int i;
357     struct Vector mainStore = {NULL, 0, 0};
358     struct Vector clsidStore = {NULL, 0, 0};
359     struct REG_RF rrf;
360     SAFEARRAY * psa;
361     SAFEARRAYBOUND saBound;
362     HRESULT hr = S_OK;
363
364     rrf.dwVersion = prf2->dwVersion;
365     rrf.dwMerit = prf2->dwMerit;
366     rrf.dwPins = prf2->u.s1.cPins2;
367     rrf.dwUnused = 0;
368
369     add_data(&mainStore, (LPBYTE)&rrf, sizeof(rrf));
370
371     for (i = 0; i < prf2->u.s1.cPins2; i++)
372     {
373         size += sizeof(struct REG_RFP);
374         if (prf2->u.s1.rgPins2[i].clsPinCategory)
375             size += sizeof(DWORD);
376         size += prf2->u.s1.rgPins2[i].nMediaTypes * sizeof(struct REG_TYPE);
377         size += prf2->u.s1.rgPins2[i].nMediums * sizeof(DWORD);
378     }
379
380     for (i = 0; i < prf2->u.s1.cPins2; i++)
381     {
382         struct REG_RFP rrfp;
383         REGFILTERPINS2 rgPin2 = prf2->u.s1.rgPins2[i];
384         int j;
385
386         rrfp.signature[0] = '0';
387         rrfp.signature[1] = 'p';
388         rrfp.signature[2] = 'i';
389         rrfp.signature[3] = '3';
390         rrfp.signature[0] += i;
391         rrfp.dwFlags = rgPin2.dwFlags;
392         rrfp.dwInstances = rgPin2.cInstances;
393         rrfp.dwMediaTypes = rgPin2.nMediaTypes;
394         rrfp.dwMediums = rgPin2.nMediums;
395         rrfp.bCategory = rgPin2.clsPinCategory ? 1 : 0;
396
397         add_data(&mainStore, (LPBYTE)&rrfp, sizeof(rrfp));
398         if (rrfp.bCategory)
399         {
400             DWORD index = find_data(&clsidStore, (LPBYTE)rgPin2.clsPinCategory, sizeof(CLSID));
401             if (index == -1)
402                 index = add_data(&clsidStore, (LPBYTE)rgPin2.clsPinCategory, sizeof(CLSID));
403             index += size;
404
405             add_data(&mainStore, (LPBYTE)&index, sizeof(index));
406         }
407
408         for (j = 0; j < rgPin2.nMediaTypes; j++)
409         {
410             struct REG_TYPE rt;
411             rt.signature[0] = '0';
412             rt.signature[1] = 't';
413             rt.signature[2] = 'y';
414             rt.signature[3] = '3';
415             rt.signature[0] += j;
416
417             rt.dwUnused = 0;
418             rt.dwOffsetMajor = find_data(&clsidStore, (LPBYTE)rgPin2.lpMediaType[j].clsMajorType, sizeof(CLSID));
419             if (rt.dwOffsetMajor == -1)
420                 rt.dwOffsetMajor = add_data(&clsidStore, (LPBYTE)rgPin2.lpMediaType[j].clsMajorType, sizeof(CLSID));
421             rt.dwOffsetMajor += size;
422             rt.dwOffsetMinor = find_data(&clsidStore, (LPBYTE)rgPin2.lpMediaType[j].clsMinorType, sizeof(CLSID));
423             if (rt.dwOffsetMinor == -1)
424                 rt.dwOffsetMinor = add_data(&clsidStore, (LPBYTE)rgPin2.lpMediaType[j].clsMinorType, sizeof(CLSID));
425             rt.dwOffsetMinor += size;
426
427             add_data(&mainStore, (LPBYTE)&rt, sizeof(rt));
428         }
429
430         for (j = 0; j < rgPin2.nMediums; j++)
431         {
432             DWORD index = find_data(&clsidStore, (LPBYTE)(rgPin2.lpMedium + j), sizeof(REGPINMEDIUM));
433             if (index == -1)
434                 index = add_data(&clsidStore, (LPBYTE)(rgPin2.lpMedium + j), sizeof(REGPINMEDIUM));
435             index += size;
436
437             add_data(&mainStore, (LPBYTE)&index, sizeof(index));
438         }
439     }
440
441     saBound.lLbound = 0;
442     saBound.cElements = mainStore.current + clsidStore.current;
443     psa = SafeArrayCreate(VT_UI1, 1, &saBound);
444     if (!psa)
445     {
446         ERR("Couldn't create SAFEARRAY\n");
447         hr = E_FAIL;
448     }
449
450     if (SUCCEEDED(hr))
451     {
452         LPBYTE pbSAData;
453         hr = SafeArrayAccessData(psa, (LPVOID *)&pbSAData);
454         if (SUCCEEDED(hr))
455         {
456             memcpy(pbSAData, mainStore.pData, mainStore.current);
457             memcpy(pbSAData + mainStore.current, clsidStore.pData, clsidStore.current);
458             hr = SafeArrayUnaccessData(psa);
459         }
460     }
461
462     V_VT(&var) = VT_ARRAY | VT_UI1;
463     V_UNION(&var, parray) = psa;
464
465     if (SUCCEEDED(hr))
466         hr = IPropertyBag_Write(pPropBag, wszFilterDataName, &var);
467
468     if (psa)
469         SafeArrayDestroy(psa);
470
471     delete_vector(&mainStore);
472     delete_vector(&clsidStore);
473     return hr;
474 }
475
476 static HRESULT FM2_ReadFilterData(IPropertyBag * pPropBag, REGFILTER2 * prf2)
477 {
478     VARIANT var;
479     HRESULT hr;
480     LPBYTE pData = NULL;
481     struct REG_RF * prrf;
482     LPBYTE pCurrent;
483     DWORD i;
484     REGFILTERPINS2 * rgPins2;
485
486     VariantInit(&var);
487     V_VT(&var) = VT_ARRAY | VT_UI1;
488
489     hr = IPropertyBag_Read(pPropBag, wszFilterDataName, &var, NULL);
490
491     if (SUCCEEDED(hr))
492         hr = SafeArrayAccessData(V_UNION(&var, parray), (LPVOID*)&pData);
493
494     if (SUCCEEDED(hr))
495     {
496         prrf = (struct REG_RF *)pData;
497         pCurrent = pData;
498
499         if (prrf->dwVersion != 2)
500         {
501             FIXME("Filter registry version %ld not supported\n", prrf->dwVersion);
502             ZeroMemory(prf2, sizeof(*prf2));
503             hr = E_FAIL;
504         }
505     }
506
507     if (SUCCEEDED(hr))
508     {
509         TRACE("version = %ld, merit = %lx, #pins = %ld, unused = %lx\n",
510             prrf->dwVersion, prrf->dwMerit, prrf->dwPins, prrf->dwUnused);
511
512         prf2->dwVersion = prrf->dwVersion;
513         prf2->dwMerit = prrf->dwMerit;
514         prf2->u.s1.cPins2 = prrf->dwPins;
515         rgPins2 = CoTaskMemAlloc(prrf->dwPins * sizeof(*rgPins2));
516         prf2->u.s1.rgPins2 = rgPins2;
517         pCurrent += sizeof(struct REG_RF);
518
519         for (i = 0; i < prrf->dwPins; i++)
520         {
521             struct REG_RFP * prrfp = (struct REG_RFP *)pCurrent;
522             REGPINTYPES * lpMediaType;
523             REGPINMEDIUM * lpMedium;
524             UINT j;
525
526             /* FIXME: check signature */
527
528             TRACE("\tsignature = %s\n", debugstr_an(prrfp->signature, 4));
529
530             TRACE("\tpin[%ld]: flags = %lx, instances = %ld, media types = %ld, mediums = %ld\n",
531                 i, prrfp->dwFlags, prrfp->dwInstances, prrfp->dwMediaTypes, prrfp->dwMediums);
532
533             rgPins2[i].dwFlags = prrfp->dwFlags;
534             rgPins2[i].cInstances = prrfp->dwInstances;
535             rgPins2[i].nMediaTypes = prrfp->dwMediaTypes;
536             rgPins2[i].nMediums = prrfp->dwMediums;
537             pCurrent += sizeof(struct REG_RFP);
538             if (prrfp->bCategory)
539             {
540                 CLSID * clsCat = CoTaskMemAlloc(sizeof(CLSID));
541                 memcpy(clsCat, pData + *(DWORD*)(pCurrent), sizeof(CLSID));
542                 pCurrent += sizeof(DWORD);
543                 rgPins2[i].clsPinCategory = clsCat;
544             }
545             else
546                 rgPins2[i].clsPinCategory = NULL;
547
548             if (rgPins2[i].nMediaTypes > 0)
549                 lpMediaType = CoTaskMemAlloc(rgPins2[i].nMediaTypes * sizeof(*lpMediaType));
550             else
551                 lpMediaType = NULL;
552
553             rgPins2[i].lpMediaType = lpMediaType;
554
555             for (j = 0; j < rgPins2[i].nMediaTypes; j++)
556             {
557                 struct REG_TYPE * prt = (struct REG_TYPE *)pCurrent;
558                 CLSID * clsMajor = CoTaskMemAlloc(sizeof(CLSID));
559                 CLSID * clsMinor = CoTaskMemAlloc(sizeof(CLSID));
560
561                 /* FIXME: check signature */
562                 TRACE("\t\tsignature = %s\n", debugstr_an(prt->signature, 4));
563
564                 memcpy(clsMajor, pData + prt->dwOffsetMajor, sizeof(CLSID));
565                 memcpy(clsMinor, pData + prt->dwOffsetMinor, sizeof(CLSID));
566
567                 lpMediaType[j].clsMajorType = clsMajor;
568                 lpMediaType[j].clsMinorType = clsMinor;
569
570                 pCurrent += sizeof(*prt);
571             }
572
573             if (rgPins2[i].nMediums > 0)
574                 lpMedium = CoTaskMemAlloc(rgPins2[i].nMediums * sizeof(*lpMedium));
575             else
576                 lpMedium = NULL;
577
578             rgPins2[i].lpMedium = lpMedium;
579
580             for (j = 0; j < rgPins2[i].nMediums; j++)
581             {
582                 DWORD dwOffset = *(DWORD *)pCurrent;
583
584                 memcpy(lpMedium + j, pData + dwOffset, sizeof(REGPINMEDIUM));
585
586                 pCurrent += sizeof(dwOffset);
587             }
588         }
589
590     }
591
592     if (pData)
593         SafeArrayUnaccessData(V_UNION(&var, parray));
594
595     VariantClear(&var);
596
597     return hr;
598 }
599
600 static void FM2_DeleteRegFilter(REGFILTER2 * prf2)
601 {
602     UINT i;
603     for (i = 0; i < prf2->u.s1.cPins2; i++)
604     {
605         UINT j;
606         if (prf2->u.s1.rgPins2[i].clsPinCategory)
607             CoTaskMemFree((LPVOID)prf2->u.s1.rgPins2[i].clsPinCategory);
608
609         for (j = 0; j < prf2->u.s1.rgPins2[i].nMediaTypes; j++)
610         {
611             CoTaskMemFree((LPVOID)prf2->u.s1.rgPins2[i].lpMediaType[j].clsMajorType);
612             CoTaskMemFree((LPVOID)prf2->u.s1.rgPins2[i].lpMediaType[j].clsMinorType);
613         }
614         CoTaskMemFree((LPVOID)prf2->u.s1.rgPins2[i].lpMedium);
615     }
616 }
617
618 static HRESULT WINAPI FilterMapper2_RegisterFilter(
619     IFilterMapper2 * iface,
620     REFCLSID clsidFilter,
621     LPCWSTR szName,
622     IMoniker **ppMoniker,
623     const CLSID *pclsidCategory,
624     const OLECHAR *szInstance,
625     const REGFILTER2 *prf2)
626 {
627     IParseDisplayName * pParser = NULL;
628     IBindCtx * pBindCtx = NULL;
629     IMoniker * pMoniker = NULL;
630     IPropertyBag * pPropBag = NULL;
631     HRESULT hr;
632     LPWSTR pwszParseName = NULL;
633     LPWSTR pCurrent;
634     static const WCHAR wszDevice[] = {'@','d','e','v','i','c','e',':','s','w',':',0};
635     int nameLen;
636     ULONG ulEaten;
637     LPWSTR szClsidTemp = NULL;
638
639     TRACE("(%s, %s, %p, %s, %s, %p)\n",
640         debugstr_guid(clsidFilter),
641         debugstr_w(szName),
642         ppMoniker,
643         debugstr_guid(pclsidCategory),
644         debugstr_w(szInstance),
645         prf2);
646
647     if (prf2->dwVersion != 2)
648     {
649         FIXME("dwVersion != 2 not supported at the moment\n");
650         return E_NOTIMPL;
651     }
652
653     if (ppMoniker)
654         *ppMoniker = NULL;
655
656     if (!pclsidCategory)
657         pclsidCategory = &CLSID_ActiveMovieCategories;
658
659     /* sizeof... will include null terminator and
660      * the + 1 is for the separator ('\\'). The -1 is
661      * because CHARS_IN_GUID includes the null terminator
662      */
663     nameLen = sizeof(wszDevice)/sizeof(wszDevice[0]) + CHARS_IN_GUID - 1 + 1;
664
665     if (szInstance)
666         nameLen += strlenW(szInstance);
667     else
668         nameLen += CHARS_IN_GUID - 1; /* CHARS_IN_GUID includes null terminator */
669
670     pCurrent = pwszParseName = CoTaskMemAlloc(nameLen*sizeof(WCHAR));
671     if (!pwszParseName)
672         return E_OUTOFMEMORY;
673
674     strcpyW(pwszParseName, wszDevice);
675     pCurrent += strlenW(wszDevice);
676
677     hr = StringFromCLSID(pclsidCategory, &szClsidTemp);
678
679     if (SUCCEEDED(hr))
680     {
681         strncpyW(pCurrent, szClsidTemp, CHARS_IN_GUID);
682         pCurrent += CHARS_IN_GUID - 1;
683         pCurrent[0] = '\\';
684
685         if (szInstance)
686             strcpyW(pCurrent+1, szInstance);
687         else
688         {
689             if (szClsidTemp)
690             {
691                 CoTaskMemFree(szClsidTemp);
692                 szClsidTemp = NULL;
693             }
694             hr = StringFromCLSID(clsidFilter, &szClsidTemp);
695             if (SUCCEEDED(hr))
696                 strcpyW(pCurrent+1, szClsidTemp);
697         }
698     }
699
700     if (SUCCEEDED(hr))
701         hr = CoCreateInstance(&CLSID_CDeviceMoniker, NULL, CLSCTX_INPROC, &IID_IParseDisplayName, (LPVOID *)&pParser);
702
703     if (SUCCEEDED(hr))
704         hr = CreateBindCtx(0, &pBindCtx);
705
706     if (SUCCEEDED(hr))
707         hr = IParseDisplayName_ParseDisplayName(pParser, pBindCtx, pwszParseName, &ulEaten, &pMoniker);
708
709     if (pBindCtx)
710         IBindCtx_Release(pBindCtx); pBindCtx = NULL;
711     if (pParser)
712         IParseDisplayName_Release(pParser); pParser = NULL;
713
714     if (SUCCEEDED(hr))
715         hr = IMoniker_BindToStorage(pMoniker, NULL, NULL, &IID_IPropertyBag, (LPVOID)&pPropBag);
716
717     if (SUCCEEDED(hr))
718         hr = FM2_WriteFriendlyName(pPropBag, szName);
719
720     if (SUCCEEDED(hr))
721         hr = FM2_WriteClsid(pPropBag, clsidFilter);
722
723     if (SUCCEEDED(hr))
724         hr = FM2_WriteFilterData(pPropBag, prf2);
725
726     if (pPropBag)
727         IPropertyBag_Release(pPropBag); pPropBag = NULL;
728     if (szClsidTemp)
729         CoTaskMemFree(szClsidTemp);
730
731     if (SUCCEEDED(hr) && ppMoniker)
732         *ppMoniker = pMoniker;
733     else if (pMoniker)
734         IMoniker_Release(pMoniker); pMoniker = NULL;
735
736     TRACE("-- returning %lx\n", hr);
737
738     return hr;
739 }
740
741 /* internal helper function */
742 static BOOL MatchTypes(
743     BOOL bExactMatch,
744     DWORD nPinTypes,
745     const REGPINTYPES * pPinTypes,
746     DWORD nMatchTypes,
747     const GUID * pMatchTypes)
748 {
749     BOOL bMatch = FALSE;
750     DWORD j;
751
752     if ((nMatchTypes == 0) && (nPinTypes > 0))
753         bMatch = TRUE;
754
755     for (j = 0; j < nPinTypes; j++)
756     {
757         DWORD i;
758         for (i = 0; i < nMatchTypes*2; i+=2)
759         {
760             if (((!bExactMatch && IsEqualGUID(pPinTypes[j].clsMajorType, &GUID_NULL)) || IsEqualGUID(&pMatchTypes[i], &GUID_NULL) || IsEqualGUID(pPinTypes[j].clsMajorType, &pMatchTypes[i])) &&
761                 ((!bExactMatch && IsEqualGUID(pPinTypes[j].clsMinorType, &GUID_NULL)) || IsEqualGUID(&pMatchTypes[i+1], &GUID_NULL) || IsEqualGUID(pPinTypes[j].clsMinorType, &pMatchTypes[i+1])))
762             {
763                 bMatch = TRUE;
764                 break;
765             }
766         }
767     }
768     return bMatch;
769 }
770
771 /* internal helper function for qsort of MONIKER_MERIT array */
772 static int mm_compare(const void * left, const void * right)
773 {
774     const struct MONIKER_MERIT * mmLeft = (struct MONIKER_MERIT *)left;
775     const struct MONIKER_MERIT * mmRight = (struct MONIKER_MERIT *)right;
776
777     if (mmLeft->dwMerit == mmRight->dwMerit)
778         return 0;
779     if (mmLeft->dwMerit > mmRight->dwMerit)
780         return -1;
781     return 1;
782 }
783
784 /* NOTES:
785  *   Exact match means whether or not to treat GUID_NULL's in filter data as wild cards
786  *    (GUID_NULL's in input to function automatically treated as wild cards)
787  *   Input/Output needed means match only on criteria if TRUE (with zero input types
788  *    meaning match any input/output pin as long as one exists), otherwise match any
789  *    filter that meets the rest of the requirements.
790  */
791 static HRESULT WINAPI FilterMapper2_EnumMatchingFilters(
792     IFilterMapper2 * iface,
793     IEnumMoniker **ppEnum,
794     DWORD dwFlags,
795     BOOL bExactMatch,
796     DWORD dwMerit,
797     BOOL bInputNeeded,
798     DWORD cInputTypes,
799     const GUID *pInputTypes,
800     const REGPINMEDIUM *pMedIn,
801     const CLSID *pPinCategoryIn,
802     BOOL bRender,
803     BOOL bOutputNeeded,
804     DWORD cOutputTypes,
805     const GUID *pOutputTypes,
806     const REGPINMEDIUM *pMedOut,
807     const CLSID *pPinCategoryOut)
808 {
809     ICreateDevEnum * pCreateDevEnum;
810     IMoniker * pMonikerCat;
811     IEnumMoniker * pEnumCat;
812     HRESULT hr;
813     struct Vector monikers = {NULL, 0, 0};
814
815     TRACE("(%p, %lx, %s, %lx, %s, %ld, %p, %p, %p, %s, %s, %p, %p, %p)\n",
816         ppEnum,
817         dwFlags,
818         bExactMatch ? "true" : "false",
819         dwMerit,
820         bInputNeeded ? "true" : "false",
821         cInputTypes,
822         pInputTypes,
823         pMedIn,
824         pPinCategoryIn,
825         bRender ? "true" : "false",
826         bOutputNeeded ? "true" : "false",
827         pOutputTypes,
828         pMedOut,
829         pPinCategoryOut);
830
831     if (dwFlags != 0)
832     {
833         FIXME("dwFlags = %lx not implemented\n", dwFlags);
834     }
835
836     *ppEnum = NULL;
837
838     hr = CoCreateInstance(&CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC, &IID_ICreateDevEnum, (LPVOID*)&pCreateDevEnum);
839
840     if (SUCCEEDED(hr))
841         hr = ICreateDevEnum_CreateClassEnumerator(pCreateDevEnum, &CLSID_ActiveMovieCategories, &pEnumCat, 0);
842
843     while (IEnumMoniker_Next(pEnumCat, 1, &pMonikerCat, NULL) == S_OK)
844     {
845         IPropertyBag * pPropBagCat = NULL;
846         VARIANT var;
847         HRESULT hrSub; /* this is so that one buggy filter
848                           doesn't make the whole lot fail */
849
850         VariantInit(&var);
851
852         hrSub = IMoniker_BindToStorage(pMonikerCat, NULL, NULL, &IID_IPropertyBag, (LPVOID*)&pPropBagCat);
853
854         if (SUCCEEDED(hrSub))
855             hrSub = IPropertyBag_Read(pPropBagCat, wszMeritName, &var, NULL);
856
857         if (SUCCEEDED(hrSub) && (V_UNION(&var, ulVal) >= dwMerit))
858         {
859             CLSID clsidCat;
860             IEnumMoniker * pEnum;
861             IMoniker * pMoniker;
862
863             VariantClear(&var);
864
865             if (TRACE_ON(quartz))
866             {
867                 VARIANT temp;
868                 V_VT(&temp) = VT_EMPTY;
869                 IPropertyBag_Read(pPropBagCat, wszFriendlyName, &temp, NULL);
870                 TRACE("Considering category %s\n", debugstr_w(V_UNION(&temp, bstrVal)));
871                 VariantClear(&temp);
872             }
873
874             hrSub = IPropertyBag_Read(pPropBagCat, wszClsidName, &var, NULL);
875
876             if (SUCCEEDED(hrSub))
877                 hrSub = CLSIDFromString(V_UNION(&var, bstrVal), &clsidCat);
878
879             if (SUCCEEDED(hrSub))
880                 hrSub = ICreateDevEnum_CreateClassEnumerator(pCreateDevEnum, &clsidCat, &pEnum, 0);
881
882             if (SUCCEEDED(hrSub))
883             {
884                 while (IEnumMoniker_Next(pEnum, 1, &pMoniker, NULL) == S_OK)
885                 {
886                     IPropertyBag * pPropBag = NULL;
887                     REGFILTER2 rf2;
888                     DWORD i;
889                     BOOL bInputMatch = !bInputNeeded;
890                     BOOL bOutputMatch = !bOutputNeeded;
891
892                     ZeroMemory(&rf2, sizeof(rf2));
893
894                     hrSub = IMoniker_BindToStorage(pMoniker, NULL, NULL, &IID_IPropertyBag, (LPVOID*)&pPropBag);
895
896                     if (SUCCEEDED(hrSub))
897                         hrSub = FM2_ReadFilterData(pPropBag, &rf2);
898
899                     /* Logic used for bInputMatch expression:
900                      * There exists some pin such that bInputNeeded implies (pin is an input and
901                      * (bRender implies pin has render flag) and major/minor types members of
902                      * pInputTypes )
903                      * bOutputMatch is similar, but without the "bRender implies ..." part
904                      * and substituting variables names containing input for output
905                      */
906
907                     /* determine whether filter meets requirements */
908                     if (SUCCEEDED(hrSub) && (rf2.dwMerit >= dwMerit))
909                     {
910                         for (i = 0; (i < rf2.u.s1.cPins2) && (!bInputMatch || !bOutputMatch); i++)
911                         {
912                             const REGFILTERPINS2 * rfp2 = rf2.u.s1.rgPins2 + i;
913
914                             bInputMatch = bInputMatch || (!(rfp2->dwFlags & REG_PINFLAG_B_OUTPUT) &&
915                                 (!bRender || (rfp2->dwFlags & REG_PINFLAG_B_RENDERER)) &&
916                                 MatchTypes(bExactMatch, rfp2->nMediaTypes, rfp2->lpMediaType, cInputTypes, pInputTypes));
917                             bOutputMatch = bOutputMatch || ((rfp2->dwFlags & REG_PINFLAG_B_OUTPUT) &&
918                                 MatchTypes(bExactMatch, rfp2->nMediaTypes, rfp2->lpMediaType, cOutputTypes, pOutputTypes));
919                         }
920
921                         if (bInputMatch && bOutputMatch)
922                         {
923                             struct MONIKER_MERIT mm = {pMoniker, rf2.dwMerit};
924                             IMoniker_AddRef(pMoniker);
925                             add_data(&monikers, (LPBYTE)&mm, sizeof(mm));
926                         }
927                     }
928
929                     FM2_DeleteRegFilter(&rf2);
930                     if (pPropBag)
931                         IPropertyBag_Release(pPropBag);
932                     IMoniker_Release(pMoniker);
933                 }
934                 IEnumMoniker_Release(pEnum);
935             }
936         }
937
938         VariantClear(&var);
939         if (pPropBagCat)
940             IPropertyBag_Release(pPropBagCat);
941         IMoniker_Release(pMonikerCat);
942     }
943
944
945     if (SUCCEEDED(hr))
946     {
947         IMoniker ** ppMoniker;
948         int i;
949         ULONG nMonikerCount = monikers.current / sizeof(struct MONIKER_MERIT);
950
951         /* sort the monikers in descending merit order */
952         qsort(monikers.pData, nMonikerCount,
953               sizeof(struct MONIKER_MERIT),
954               mm_compare);
955
956         /* construct an IEnumMoniker interface */
957         ppMoniker = CoTaskMemAlloc(nMonikerCount * sizeof(IMoniker *));
958         for (i = 0; i < nMonikerCount; i++)
959         {
960             /* no need to AddRef here as already AddRef'd above */
961             ppMoniker[i] = ((struct MONIKER_MERIT *)monikers.pData)[i].pMoniker;
962         }
963         hr = EnumMonikerImpl_Create(ppMoniker, nMonikerCount, ppEnum);
964         CoTaskMemFree(ppMoniker);
965     }
966
967     delete_vector(&monikers);
968     IEnumMoniker_Release(pEnumCat);
969     ICreateDevEnum_Release(pCreateDevEnum);
970
971     return hr;
972 }
973
974 static ICOM_VTABLE(IFilterMapper2) fm2vtbl =
975 {
976     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
977
978     FilterMapper2_QueryInterface,
979     FilterMapper2_AddRef,
980     FilterMapper2_Release,
981
982     FilterMapper2_CreateCategory,
983     FilterMapper2_UnregisterFilter,
984     FilterMapper2_RegisterFilter,
985     FilterMapper2_EnumMatchingFilters
986 };
987
988 /*** IUnknown methods ***/
989
990 static HRESULT WINAPI FilterMapper_QueryInterface(IFilterMapper * iface, REFIID riid, LPVOID *ppv)
991 {
992     ICOM_THIS_From_IFilterMapper(FilterMapper2Impl, iface);
993
994     TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
995
996     return FilterMapper2_QueryInterface((IFilterMapper2*)&This->lpVtbl, riid, ppv);
997 }
998
999 static ULONG WINAPI FilterMapper_AddRef(IFilterMapper * iface)
1000 {
1001     ICOM_THIS_From_IFilterMapper(FilterMapper2Impl, iface);
1002
1003     return FilterMapper2_AddRef((IFilterMapper2*)This);
1004 }
1005
1006 static ULONG WINAPI FilterMapper_Release(IFilterMapper * iface)
1007 {
1008     ICOM_THIS_From_IFilterMapper(FilterMapper2Impl, iface);
1009
1010     return FilterMapper2_Release((IFilterMapper2*)This);
1011 }
1012
1013 /*** IFilterMapper methods ***/
1014
1015 static HRESULT WINAPI FilterMapper_EnumMatchingFilters(
1016     IFilterMapper * iface,
1017     IEnumRegFilters **ppEnum,
1018     DWORD dwMerit,
1019     BOOL bInputNeeded,
1020     CLSID clsInMaj,
1021     CLSID clsInSub,
1022     BOOL bRender,
1023     BOOL bOutputNeeded,
1024     CLSID clsOutMaj,
1025     CLSID clsOutSub)
1026 {
1027     FIXME("stub\n");
1028     return E_NOTIMPL;
1029 }
1030
1031
1032 static HRESULT WINAPI FilterMapper_RegisterFilter(IFilterMapper * iface, CLSID clsid, LPCWSTR szName, DWORD dwMerit)
1033 {
1034     FIXME("stub\n");
1035     return E_NOTIMPL;
1036 }
1037
1038 static HRESULT WINAPI FilterMapper_RegisterFilterInstance(IFilterMapper * iface, CLSID clsid, LPCWSTR szName, CLSID *MRId)
1039 {
1040     FIXME("stub\n");
1041     return E_NOTIMPL;
1042 }
1043
1044 static HRESULT WINAPI FilterMapper_RegisterPin(
1045     IFilterMapper * iface,
1046     CLSID Filter,
1047     LPCWSTR szName,
1048     BOOL bRendered,
1049     BOOL bOutput,
1050     BOOL bZero,
1051     BOOL bMany,
1052     CLSID ConnectsToFilter,
1053     LPCWSTR ConnectsToPin)
1054 {
1055     FIXME("stub\n");
1056     return E_NOTIMPL;
1057 }
1058
1059
1060 static HRESULT WINAPI FilterMapper_RegisterPinType(
1061     IFilterMapper * iface,
1062     CLSID clsFilter,
1063     LPCWSTR szName,
1064     CLSID clsMajorType,
1065     CLSID clsSubType)
1066 {
1067     FIXME("stub\n");
1068     return E_NOTIMPL;
1069 }
1070
1071 static HRESULT WINAPI FilterMapper_UnregisterFilter(IFilterMapper * iface, CLSID Filter)
1072 {
1073     FIXME("stub\n");
1074     return E_NOTIMPL;
1075 }
1076
1077 static HRESULT WINAPI FilterMapper_UnregisterFilterInstance(IFilterMapper * iface, CLSID MRId)
1078 {
1079     FIXME("stub\n");
1080     return E_NOTIMPL;
1081 }
1082
1083 static HRESULT WINAPI FilterMapper_UnregisterPin(IFilterMapper * iface, CLSID Filter, LPCWSTR Name)
1084 {
1085     FIXME("stub\n");
1086     return E_NOTIMPL;
1087 }
1088
1089 static ICOM_VTABLE(IFilterMapper) fmvtbl =
1090 {
1091     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1092
1093     FilterMapper_QueryInterface,
1094     FilterMapper_AddRef,
1095     FilterMapper_Release,
1096
1097     FilterMapper_RegisterFilter,
1098     FilterMapper_RegisterFilterInstance,
1099     FilterMapper_RegisterPin,
1100     FilterMapper_RegisterPinType,
1101     FilterMapper_UnregisterFilter,
1102     FilterMapper_UnregisterFilterInstance,
1103     FilterMapper_UnregisterPin,
1104     FilterMapper_EnumMatchingFilters
1105 };