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