- Convert ordinals to their real names.
[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 = S_OK;
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 (ppMoniker)
648         FIXME("ppMoniker != NULL not supported at the moment\n");
649
650     if (prf2->dwVersion != 2)
651     {
652         FIXME("dwVersion != 2 not supported at the moment\n");
653         return E_NOTIMPL;
654     }
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     pwszParseName = CoTaskMemAlloc(nameLen);
671     pCurrent = pwszParseName;
672     if (!pwszParseName)
673         return E_OUTOFMEMORY;
674
675     strcpyW(pwszParseName, wszDevice);
676     pCurrent += strlenW(wszDevice);
677
678     hr = StringFromCLSID(pclsidCategory, &szClsidTemp);
679     strcpyW(pCurrent, szClsidTemp);
680     pCurrent += CHARS_IN_GUID - 1;
681     pCurrent[0] = '\\';
682
683     if (SUCCEEDED(hr))
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             strcpyW(pCurrent+1, szClsidTemp);
696         }
697     }
698
699     if (SUCCEEDED(hr))
700         hr = CoCreateInstance(&CLSID_CDeviceMoniker, NULL, CLSCTX_INPROC, &IID_IParseDisplayName, (LPVOID *)&pParser);
701
702     if (SUCCEEDED(hr))
703         hr = CreateBindCtx(0, &pBindCtx);
704
705     if (SUCCEEDED(hr))
706     {
707         hr = IParseDisplayName_ParseDisplayName(pParser, pBindCtx, pwszParseName, &ulEaten, &pMoniker);
708     }
709
710     if (pBindCtx)
711         IBindCtx_Release(pBindCtx); pBindCtx = NULL;
712
713     if (pParser)
714         IParseDisplayName_Release(pParser); pParser = NULL;
715
716     if (SUCCEEDED(hr))
717         hr = IMoniker_BindToStorage(pMoniker, NULL, NULL, &IID_IPropertyBag, (LPVOID)&pPropBag);
718
719     if (SUCCEEDED(hr))
720         hr = FM2_WriteFriendlyName(pPropBag, szName);
721
722     if (SUCCEEDED(hr))
723         hr = FM2_WriteClsid(pPropBag, clsidFilter);
724
725     if (SUCCEEDED(hr))
726         hr = FM2_WriteFilterData(pPropBag, prf2);
727
728     if (pMoniker)
729         IMoniker_Release(pMoniker); pMoniker = NULL;
730
731     if (pPropBag)
732         IPropertyBag_Release(pPropBag); pPropBag = NULL;
733
734     if (szClsidTemp)
735         CoTaskMemFree(szClsidTemp);
736
737     TRACE("-- returning %lx\n", hr);
738
739     return hr;
740 }
741
742 /* internal helper function */
743 static BOOL MatchTypes(
744     BOOL bExactMatch,
745     DWORD nPinTypes,
746     const REGPINTYPES * pPinTypes,
747     DWORD nMatchTypes,
748     const GUID * pMatchTypes)
749 {
750     BOOL bMatch = FALSE;
751     DWORD j;
752
753     if ((nMatchTypes == 0) && (nPinTypes > 0))
754         bMatch = TRUE;
755
756     for (j = 0; j < nPinTypes; j++)
757     {
758         DWORD i;
759         for (i = 0; i < nMatchTypes*2; i+=2)
760         {
761             if (((!bExactMatch && IsEqualGUID(pPinTypes[j].clsMajorType, &GUID_NULL)) || IsEqualGUID(&pMatchTypes[i], &GUID_NULL) || IsEqualGUID(pPinTypes[j].clsMajorType, &pMatchTypes[i])) &&
762                 ((!bExactMatch && IsEqualGUID(pPinTypes[j].clsMinorType, &GUID_NULL)) || IsEqualGUID(&pMatchTypes[i+1], &GUID_NULL) || IsEqualGUID(pPinTypes[j].clsMinorType, &pMatchTypes[i+1])))
763             {
764                 bMatch = TRUE;
765                 break;
766             }
767         }
768     }
769     return bMatch;
770 }
771
772 /* internal helper function for qsort of MONIKER_MERIT array */
773 static int mm_compare(const void * left, const void * right)
774 {
775     const struct MONIKER_MERIT * mmLeft = (struct MONIKER_MERIT *)left;
776     const struct MONIKER_MERIT * mmRight = (struct MONIKER_MERIT *)right;
777
778     if (mmLeft->dwMerit == mmRight->dwMerit)
779         return 0;
780     if (mmLeft->dwMerit > mmRight->dwMerit)
781         return -1;
782     return 1;
783 }
784
785 /* NOTES:
786  *   Exact match means whether or not to treat GUID_NULL's in filter data as wild cards
787  *    (GUID_NULL's in input to function automatically treated as wild cards)
788  *   Input/Output needed means match only on criteria if TRUE (with zero input types
789  *    meaning match any input/output pin as long as one exists), otherwise match any
790  *    filter that meets the rest of the requirements.
791  */
792 static HRESULT WINAPI FilterMapper2_EnumMatchingFilters(
793     IFilterMapper2 * iface,
794     IEnumMoniker **ppEnum,
795     DWORD dwFlags,
796     BOOL bExactMatch,
797     DWORD dwMerit,
798     BOOL bInputNeeded,
799     DWORD cInputTypes,
800     const GUID *pInputTypes,
801     const REGPINMEDIUM *pMedIn,
802     const CLSID *pPinCategoryIn,
803     BOOL bRender,
804     BOOL bOutputNeeded,
805     DWORD cOutputTypes,
806     const GUID *pOutputTypes,
807     const REGPINMEDIUM *pMedOut,
808     const CLSID *pPinCategoryOut)
809 {
810     ICreateDevEnum * pCreateDevEnum;
811     IMoniker * pMonikerCat;
812     IEnumMoniker * pEnumCat;
813     HRESULT hr;
814     struct Vector monikers = {NULL, 0, 0};
815
816     TRACE("(%p, %lx, %s, %lx, %s, %ld, %p, %p, %p, %s, %s, %p, %p, %p)\n",
817         ppEnum,
818         dwFlags,
819         bExactMatch ? "true" : "false",
820         dwMerit,
821         bInputNeeded ? "true" : "false",
822         cInputTypes,
823         pInputTypes,
824         pMedIn,
825         pPinCategoryIn,
826         bRender ? "true" : "false",
827         bOutputNeeded ? "true" : "false",
828         pOutputTypes,
829         pMedOut,
830         pPinCategoryOut);
831
832     if (dwFlags != 0)
833     {
834         FIXME("dwFlags = %lx not implemented\n", dwFlags);
835     }
836
837     *ppEnum = NULL;
838
839     hr = CoCreateInstance(&CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC, &IID_ICreateDevEnum, (LPVOID*)&pCreateDevEnum);
840
841     if (SUCCEEDED(hr))
842         hr = ICreateDevEnum_CreateClassEnumerator(pCreateDevEnum, &CLSID_ActiveMovieCategories, &pEnumCat, 0);
843
844     while (IEnumMoniker_Next(pEnumCat, 1, &pMonikerCat, NULL) == S_OK)
845     {
846         IPropertyBag * pPropBagCat = NULL;
847         VARIANT var;
848         HRESULT hrSub; /* this is so that one buggy filter
849                           doesn't make the whole lot fail */
850
851         VariantInit(&var);
852
853         hrSub = IMoniker_BindToStorage(pMonikerCat, NULL, NULL, &IID_IPropertyBag, (LPVOID*)&pPropBagCat);
854
855         if (SUCCEEDED(hrSub))
856             hrSub = IPropertyBag_Read(pPropBagCat, wszMeritName, &var, NULL);
857
858         if (SUCCEEDED(hrSub) && (V_UNION(&var, ulVal) >= dwMerit))
859         {
860             CLSID clsidCat;
861             IEnumMoniker * pEnum;
862             IMoniker * pMoniker;
863
864             VariantClear(&var);
865
866             if (TRACE_ON(quartz))
867             {
868                 VARIANT temp;
869                 V_VT(&temp) = VT_EMPTY;
870                 IPropertyBag_Read(pPropBagCat, wszFriendlyName, &temp, NULL);
871                 TRACE("Considering category %s\n", debugstr_w(V_UNION(&temp, bstrVal)));
872                 VariantClear(&temp);
873             }
874
875             hrSub = IPropertyBag_Read(pPropBagCat, wszClsidName, &var, NULL);
876
877             if (SUCCEEDED(hrSub))
878                 hrSub = CLSIDFromString(V_UNION(&var, bstrVal), &clsidCat);
879
880             if (SUCCEEDED(hrSub))
881                 hrSub = ICreateDevEnum_CreateClassEnumerator(pCreateDevEnum, &clsidCat, &pEnum, 0);
882
883             if (SUCCEEDED(hrSub))
884             {
885                 while (IEnumMoniker_Next(pEnum, 1, &pMoniker, NULL) == S_OK)
886                 {
887                     IPropertyBag * pPropBag = NULL;
888                     REGFILTER2 rf2;
889                     DWORD i;
890                     BOOL bInputMatch = !bInputNeeded;
891                     BOOL bOutputMatch = !bOutputNeeded;
892
893                     ZeroMemory(&rf2, sizeof(rf2));
894
895                     hrSub = IMoniker_BindToStorage(pMoniker, NULL, NULL, &IID_IPropertyBag, (LPVOID*)&pPropBag);
896
897                     if (SUCCEEDED(hrSub))
898                         hrSub = FM2_ReadFilterData(pPropBag, &rf2);
899
900                     /* Logic used for bInputMatch expression:
901                      * There exists some pin such that bInputNeeded implies (pin is an input and
902                      * (bRender implies pin has render flag) and major/minor types members of
903                      * pInputTypes )
904                      * bOutputMatch is similar, but without the "bRender implies ..." part
905                      * and substituting variables names containing input for output
906                      */
907
908                     /* determine whether filter meets requirements */
909                     if (SUCCEEDED(hrSub) && (rf2.dwMerit >= dwMerit))
910                     {
911                         for (i = 0; (i < rf2.u.s1.cPins2) && (!bInputMatch || !bOutputMatch); i++)
912                         {
913                             const REGFILTERPINS2 * rfp2 = rf2.u.s1.rgPins2 + i;
914
915                             bInputMatch = bInputMatch || (!(rfp2->dwFlags & REG_PINFLAG_B_OUTPUT) &&
916                                 (!bRender || (rfp2->dwFlags & REG_PINFLAG_B_RENDERER)) &&
917                                 MatchTypes(bExactMatch, rfp2->nMediaTypes, rfp2->lpMediaType, cInputTypes, pInputTypes));
918                             bOutputMatch = bOutputMatch || ((rfp2->dwFlags & REG_PINFLAG_B_OUTPUT) &&
919                                 MatchTypes(bExactMatch, rfp2->nMediaTypes, rfp2->lpMediaType, cOutputTypes, pOutputTypes));
920                         }
921
922                         if (bInputMatch && bOutputMatch)
923                         {
924                             struct MONIKER_MERIT mm = {pMoniker, rf2.dwMerit};
925                             IMoniker_AddRef(pMoniker);
926                             add_data(&monikers, (LPBYTE)&mm, sizeof(mm));
927                         }
928                     }
929
930                     FM2_DeleteRegFilter(&rf2);
931                     if (pPropBag)
932                         IPropertyBag_Release(pPropBag);
933                     IMoniker_Release(pMoniker);
934                 }
935                 IEnumMoniker_Release(pEnum);
936             }
937         }
938
939         VariantClear(&var);
940         if (pPropBagCat)
941             IPropertyBag_Release(pPropBagCat);
942         IMoniker_Release(pMonikerCat);
943     }
944
945
946     if (SUCCEEDED(hr))
947     {
948         IMoniker ** ppMoniker;
949         int i;
950         ULONG nMonikerCount = monikers.current / sizeof(struct MONIKER_MERIT);
951
952         /* sort the monikers in descending merit order */
953         qsort(monikers.pData, nMonikerCount,
954               sizeof(struct MONIKER_MERIT),
955               mm_compare);
956
957         /* construct an IEnumMoniker interface */
958         ppMoniker = CoTaskMemAlloc(nMonikerCount * sizeof(IMoniker *));
959         for (i = 0; i < nMonikerCount; i++)
960         {
961             /* no need to AddRef here as already AddRef'd above */
962             ppMoniker[i] = ((struct MONIKER_MERIT *)monikers.pData)[i].pMoniker;
963         }
964         hr = EnumMonikerImpl_Create(ppMoniker, nMonikerCount, ppEnum);
965         CoTaskMemFree(ppMoniker);
966     }
967
968     delete_vector(&monikers);
969     IEnumMoniker_Release(pEnumCat);
970     ICreateDevEnum_Release(pCreateDevEnum);
971
972     return hr;
973 }
974
975 static ICOM_VTABLE(IFilterMapper2) fm2vtbl =
976 {
977     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
978
979     FilterMapper2_QueryInterface,
980     FilterMapper2_AddRef,
981     FilterMapper2_Release,
982
983     FilterMapper2_CreateCategory,
984     FilterMapper2_UnregisterFilter,
985     FilterMapper2_RegisterFilter,
986     FilterMapper2_EnumMatchingFilters
987 };
988
989 /*** IUnknown methods ***/
990
991 static HRESULT WINAPI FilterMapper_QueryInterface(IFilterMapper * iface, REFIID riid, LPVOID *ppv)
992 {
993     ICOM_THIS_From_IFilterMapper(FilterMapper2Impl, iface);
994
995     TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
996
997     return FilterMapper2_QueryInterface((IFilterMapper2*)&This->lpVtbl, riid, ppv);
998 }
999
1000 static ULONG WINAPI FilterMapper_AddRef(IFilterMapper * iface)
1001 {
1002     ICOM_THIS_From_IFilterMapper(FilterMapper2Impl, iface);
1003
1004     return FilterMapper2_AddRef((IFilterMapper2*)This);
1005 }
1006
1007 static ULONG WINAPI FilterMapper_Release(IFilterMapper * iface)
1008 {
1009     ICOM_THIS_From_IFilterMapper(FilterMapper2Impl, iface);
1010
1011     return FilterMapper2_Release((IFilterMapper2*)This);
1012 }
1013
1014 /*** IFilterMapper methods ***/
1015
1016 static HRESULT WINAPI FilterMapper_EnumMatchingFilters(
1017     IFilterMapper * iface,
1018     IEnumRegFilters **ppEnum,
1019     DWORD dwMerit,
1020     BOOL bInputNeeded,
1021     CLSID clsInMaj,
1022     CLSID clsInSub,
1023     BOOL bRender,
1024     BOOL bOutputNeeded,
1025     CLSID clsOutMaj,
1026     CLSID clsOutSub)
1027 {
1028     FIXME("stub\n");
1029     return E_NOTIMPL;
1030 }
1031
1032
1033 static HRESULT WINAPI FilterMapper_RegisterFilter(IFilterMapper * iface, CLSID clsid, LPCWSTR szName, DWORD dwMerit)
1034 {
1035     FIXME("stub\n");
1036     return E_NOTIMPL;
1037 }
1038
1039 static HRESULT WINAPI FilterMapper_RegisterFilterInstance(IFilterMapper * iface, CLSID clsid, LPCWSTR szName, CLSID *MRId)
1040 {
1041     FIXME("stub\n");
1042     return E_NOTIMPL;
1043 }
1044
1045 static HRESULT WINAPI FilterMapper_RegisterPin(
1046     IFilterMapper * iface,
1047     CLSID Filter,
1048     LPCWSTR szName,
1049     BOOL bRendered,
1050     BOOL bOutput,
1051     BOOL bZero,
1052     BOOL bMany,
1053     CLSID ConnectsToFilter,
1054     LPCWSTR ConnectsToPin)
1055 {
1056     FIXME("stub\n");
1057     return E_NOTIMPL;
1058 }
1059
1060
1061 static HRESULT WINAPI FilterMapper_RegisterPinType(
1062     IFilterMapper * iface,
1063     CLSID clsFilter,
1064     LPCWSTR szName,
1065     CLSID clsMajorType,
1066     CLSID clsSubType)
1067 {
1068     FIXME("stub\n");
1069     return E_NOTIMPL;
1070 }
1071
1072 static HRESULT WINAPI FilterMapper_UnregisterFilter(IFilterMapper * iface, CLSID Filter)
1073 {
1074     FIXME("stub\n");
1075     return E_NOTIMPL;
1076 }
1077
1078 static HRESULT WINAPI FilterMapper_UnregisterFilterInstance(IFilterMapper * iface, CLSID MRId)
1079 {
1080     FIXME("stub\n");
1081     return E_NOTIMPL;
1082 }
1083
1084 static HRESULT WINAPI FilterMapper_UnregisterPin(IFilterMapper * iface, CLSID Filter, LPCWSTR Name)
1085 {
1086     FIXME("stub\n");
1087     return E_NOTIMPL;
1088 }
1089
1090 static ICOM_VTABLE(IFilterMapper) fmvtbl =
1091 {
1092     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1093
1094     FilterMapper_QueryInterface,
1095     FilterMapper_AddRef,
1096     FilterMapper_Release,
1097
1098     FilterMapper_RegisterFilter,
1099     FilterMapper_RegisterFilterInstance,
1100     FilterMapper_RegisterPin,
1101     FilterMapper_RegisterPinType,
1102     FilterMapper_UnregisterFilter,
1103     FilterMapper_UnregisterFilterInstance,
1104     FilterMapper_UnregisterPin,
1105     FilterMapper_EnumMatchingFilters
1106 };