quartz: Fix end of stream handling.
[wine] / dlls / quartz / filtermapper.c
1 /*
2  * IFilterMapper & IFilterMapper2 Implementations
3  *
4  * Copyright 2003 Robert Shearman
5  * Copyright 2004 Christian Costa
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #define NONAMELESSUNION
23 #define NONAMELESSSTRUCT
24 #include <stdarg.h>
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "winreg.h"
30 #include "winerror.h"
31
32 #include "quartz_private.h"
33
34 #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 #define ARRAYSIZE(array) (sizeof(array)/sizeof((array)[0]))
45
46 /* Unexposed IAMFilterData interface */
47 typedef struct IAMFilterData IAMFilterData;
48
49 typedef struct IAMFilterDataVtbl
50 {
51     BEGIN_INTERFACE
52
53     /*** IUnknown methods ***/
54     HRESULT (STDMETHODCALLTYPE *QueryInterface)(
55         IAMFilterData *This,
56         REFIID riid,
57         void **ppvObject);
58
59     ULONG (STDMETHODCALLTYPE *AddRef)(
60         IAMFilterData *This);
61
62     ULONG (STDMETHODCALLTYPE *Release)(
63         IAMFilterData *This);
64
65     /*** IAMFilterData methods ***/
66     HRESULT (STDMETHODCALLTYPE *ParseFilterData)(
67         IAMFilterData *This,
68         BYTE *pData,
69         ULONG cb,
70         BYTE **ppRegFilter2);
71
72     HRESULT (STDMETHODCALLTYPE *CreateFilterData)(
73         IAMFilterData* This,
74         REGFILTER2 *prf2,
75         BYTE **pRegFilterData,
76         ULONG *pcb);
77
78     END_INTERFACE
79 } IAMFilterDataVtbl;
80 struct IAMFilterData
81 {
82     const IAMFilterDataVtbl *lpVtbl;
83 };
84 const GUID IID_IAMFilterData = {
85  0x97f7c4d4, 0x547b, 0x4a5f, { 0x83,0x32, 0x53,0x64,0x30,0xad,0x2e,0x4d }
86 };
87
88
89 typedef struct FilterMapper2Impl
90 {
91     const IFilterMapper2Vtbl *lpVtbl;
92     const IFilterMapperVtbl  *lpVtblFilterMapper;
93     const IAMFilterDataVtbl  *lpVtblAMFilterData;
94     LONG refCount;
95 } FilterMapper2Impl;
96
97 static const IFilterMapper2Vtbl fm2vtbl;
98 static const IFilterMapperVtbl fmvtbl;
99 static const IAMFilterDataVtbl AMFilterDataVtbl;
100
101 static inline FilterMapper2Impl *impl_from_IFilterMapper( IFilterMapper *iface )
102 {
103     return (FilterMapper2Impl *)((char*)iface - FIELD_OFFSET(FilterMapper2Impl, lpVtblFilterMapper));
104 }
105
106 static inline FilterMapper2Impl *impl_from_IAMFilterData( IAMFilterData *iface )
107 {
108     return (FilterMapper2Impl *)((char*)iface - FIELD_OFFSET(FilterMapper2Impl, lpVtblAMFilterData));
109 }
110
111 static const WCHAR wszClsidSlash[] = {'C','L','S','I','D','\\',0};
112 static const WCHAR wszSlashInstance[] = {'\\','I','n','s','t','a','n','c','e','\\',0};
113 static const WCHAR wszSlash[] = {'\\',0};
114
115 /* CLSID property in media category Moniker */
116 static const WCHAR wszClsidName[] = {'C','L','S','I','D',0};
117 /* FriendlyName property in media category Moniker */
118 static const WCHAR wszFriendlyName[] = {'F','r','i','e','n','d','l','y','N','a','m','e',0};
119 /* Merit property in media category Moniker (CLSID_ActiveMovieCategories only) */
120 static const WCHAR wszMeritName[] = {'M','e','r','i','t',0};
121 /* FilterData property in media category Moniker (not CLSID_ActiveMovieCategories) */
122 static const WCHAR wszFilterDataName[] = {'F','i','l','t','e','r','D','a','t','a',0};
123 /* For filters registered with IFilterMapper */
124 static const WCHAR wszFilterSlash[] = {'F','i','l','t','e','r','\\',0};
125 static const WCHAR wszFilter[] = {'F','i','l','t','e','r',0};
126 /* For pins registered with IFilterMapper */
127 static const WCHAR wszPins[] = {'P','i','n','s',0};
128 static const WCHAR wszAllowedMany[] = {'A','l','l','o','w','e','d','M','a','n','y',0};
129 static const WCHAR wszAllowedZero[] = {'A','l','l','o','w','e','d','Z','e','r','o',0};
130 static const WCHAR wszDirection[] = {'D','i','r','e','c','t','i','o','n',0};
131 static const WCHAR wszIsRendered[] = {'I','s','R','e','n','d','e','r','e','d',0};
132 /* For types registered with IFilterMapper */
133 static const WCHAR wszTypes[] = {'T','y','p','e','s',0};
134
135
136 /* registry format for REGFILTER2 */
137 struct REG_RF
138 {
139     DWORD dwVersion;
140     DWORD dwMerit;
141     DWORD dwPins;
142     DWORD dwUnused;
143 };
144
145 struct REG_RFP
146 {
147     BYTE signature[4]; /* e.g. "0pi3" */
148     DWORD dwFlags;
149     DWORD dwInstances;
150     DWORD dwMediaTypes;
151     DWORD dwMediums;
152     DWORD bCategory; /* is there a category clsid? */
153     /* optional: dwOffsetCategoryClsid */
154 };
155
156 struct REG_TYPE
157 {
158     BYTE signature[4]; /* e.g. "0ty3" */
159     DWORD dwUnused;
160     DWORD dwOffsetMajor;
161     DWORD dwOffsetMinor;
162 };
163
164 struct MONIKER_MERIT
165 {
166     IMoniker * pMoniker;
167     DWORD dwMerit;
168 };
169
170 struct Vector
171 {
172     LPBYTE pData;
173     int capacity; /* in bytes */
174     int current; /* pointer to next free byte */
175 };
176
177 /* returns the position it was added at */
178 static int add_data(struct Vector * v, const BYTE * pData, int size)
179 {
180     int index = v->current;
181     if (v->current + size > v->capacity)
182     {
183         LPBYTE pOldData = v->pData;
184         v->capacity = (v->capacity + size) * 2;
185         v->pData = CoTaskMemAlloc(v->capacity);
186         memcpy(v->pData, pOldData, v->current);
187         CoTaskMemFree(pOldData);
188     }
189     memcpy(v->pData + v->current, pData, size);
190     v->current += size;
191     return index;
192 }
193
194 static int find_data(const struct Vector * v, const BYTE * pData, int size)
195 {
196     int index;
197     for (index = 0; index < v->current; index++)
198         if (!memcmp(v->pData + index, pData, size))
199             return index;
200     /* not found */
201     return -1;
202 }
203
204 static void delete_vector(struct Vector * v)
205 {
206     CoTaskMemFree(v->pData);
207     v->current = 0;
208     v->capacity = 0;
209 }
210
211 HRESULT FilterMapper2_create(IUnknown *pUnkOuter, LPVOID *ppObj)
212 {
213     FilterMapper2Impl * pFM2impl;
214
215     TRACE("(%p, %p)\n", pUnkOuter, ppObj);
216
217     if (pUnkOuter)
218         return CLASS_E_NOAGGREGATION;
219
220     pFM2impl = CoTaskMemAlloc(sizeof(*pFM2impl));
221     if (!pFM2impl)
222         return E_OUTOFMEMORY;
223
224     pFM2impl->lpVtbl = &fm2vtbl;
225     pFM2impl->lpVtblFilterMapper = &fmvtbl;
226     pFM2impl->lpVtblAMFilterData = &AMFilterDataVtbl;
227     pFM2impl->refCount = 1;
228
229     *ppObj = pFM2impl;
230
231     TRACE("-- created at %p\n", pFM2impl);
232
233     return S_OK;
234 }
235
236 HRESULT FilterMapper_create(IUnknown *pUnkOuter, LPVOID *ppObj)
237 {
238     FilterMapper2Impl *pFM2impl;
239     HRESULT hr;
240
241     TRACE("(%p, %p)\n", pUnkOuter, ppObj);
242
243     hr = FilterMapper2_create(pUnkOuter, (LPVOID*)&pFM2impl);
244     if (FAILED(hr))
245         return hr;
246
247     *ppObj = &pFM2impl->lpVtblFilterMapper;
248
249     return hr;
250 }
251
252 /*** IUnknown methods ***/
253
254 static HRESULT WINAPI FilterMapper2_QueryInterface(IFilterMapper2 * iface, REFIID riid, LPVOID *ppv)
255 {
256     FilterMapper2Impl *This = (FilterMapper2Impl *)iface;
257
258     TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
259
260     *ppv = NULL;
261
262     if (IsEqualIID(riid, &IID_IUnknown))
263         *ppv = iface;
264     else if (IsEqualIID(riid, &IID_IFilterMapper2))
265         *ppv = iface;
266     else if (IsEqualIID(riid, &IID_IFilterMapper))
267         *ppv = &This->lpVtblFilterMapper;
268     else if (IsEqualIID(riid, &IID_IAMFilterData))
269         *ppv = &This->lpVtblAMFilterData;
270
271     if (*ppv != NULL)
272     {
273         IUnknown_AddRef((IUnknown *)*ppv);
274         return S_OK;
275     }
276
277     FIXME("No interface for %s\n", debugstr_guid(riid));
278     return E_NOINTERFACE;
279 }
280
281 static ULONG WINAPI FilterMapper2_AddRef(IFilterMapper2 * iface)
282 {
283     FilterMapper2Impl *This = (FilterMapper2Impl *)iface;
284     ULONG refCount = InterlockedIncrement(&This->refCount);
285
286     TRACE("(%p)->() AddRef from %d\n", This, refCount - 1);
287
288     return refCount;
289 }
290
291 static ULONG WINAPI FilterMapper2_Release(IFilterMapper2 * iface)
292 {
293     FilterMapper2Impl *This = (FilterMapper2Impl *)iface;
294     ULONG refCount = InterlockedDecrement(&This->refCount);
295
296     TRACE("(%p)->() Release from %d\n", This, refCount + 1);
297
298     if (refCount == 0)
299     {
300         CoTaskMemFree(This);
301         return 0;
302     }
303     return refCount;
304 }
305
306 /*** IFilterMapper2 methods ***/
307
308 static HRESULT WINAPI FilterMapper2_CreateCategory(
309     IFilterMapper2 * iface,
310     REFCLSID clsidCategory,
311     DWORD dwCategoryMerit,
312     LPCWSTR szDescription)
313 {
314     LPWSTR wClsidAMCat = NULL;
315     LPWSTR wClsidCategory = NULL;
316     WCHAR wszKeyName[ARRAYSIZE(wszClsidSlash)-1 + ARRAYSIZE(wszSlashInstance)-1 + (CHARS_IN_GUID-1) * 2 + 1];
317     HKEY hKey = NULL;
318     LONG lRet;
319     HRESULT hr;
320
321     TRACE("(%s, %x, %s)\n", debugstr_guid(clsidCategory), dwCategoryMerit, debugstr_w(szDescription));
322
323     hr = StringFromCLSID(&CLSID_ActiveMovieCategories, &wClsidAMCat);
324
325     if (SUCCEEDED(hr))
326     {
327         hr = StringFromCLSID(clsidCategory, &wClsidCategory);
328     }
329
330     if (SUCCEEDED(hr))
331     {
332         strcpyW(wszKeyName, wszClsidSlash);
333         strcatW(wszKeyName, wClsidAMCat);
334         strcatW(wszKeyName, wszSlashInstance);
335         strcatW(wszKeyName, wClsidCategory);
336
337         lRet = RegCreateKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
338         hr = HRESULT_FROM_WIN32(lRet);
339     }
340
341     if (SUCCEEDED(hr))
342     {
343         lRet = RegSetValueExW(hKey, wszFriendlyName, 0, REG_SZ, (const BYTE*)szDescription, (strlenW(szDescription) + 1) * sizeof(WCHAR));
344         hr = HRESULT_FROM_WIN32(lRet);
345     }
346
347     if (SUCCEEDED(hr))
348     {
349         lRet = RegSetValueExW(hKey, wszClsidName, 0, REG_SZ, (LPBYTE)wClsidCategory, (strlenW(wClsidCategory) + 1) * sizeof(WCHAR));
350         hr = HRESULT_FROM_WIN32(lRet);
351     }
352
353     if (SUCCEEDED(hr))
354     {
355         lRet = RegSetValueExW(hKey, wszMeritName, 0, REG_DWORD, (LPBYTE)&dwCategoryMerit, sizeof(dwCategoryMerit));
356         hr = HRESULT_FROM_WIN32(lRet);
357     }
358
359     CloseHandle(hKey);
360     CoTaskMemFree(wClsidCategory);
361     CoTaskMemFree(wClsidAMCat);
362
363     return hr;
364 }
365
366 static HRESULT WINAPI FilterMapper2_UnregisterFilter(
367     IFilterMapper2 * iface,
368     const CLSID *pclsidCategory,
369     const OLECHAR *szInstance,
370     REFCLSID Filter)
371 {
372     WCHAR wszKeyName[MAX_PATH];
373     LPWSTR wClsidCategory = NULL;
374     LPWSTR wFilter = NULL;
375     HRESULT hr;
376
377     TRACE("(%p, %s, %s)\n", pclsidCategory, debugstr_w(szInstance), debugstr_guid(Filter));
378
379     if (!pclsidCategory)
380         pclsidCategory = &CLSID_LegacyAmFilterCategory;
381
382     hr = StringFromCLSID(pclsidCategory, &wClsidCategory);
383
384     if (SUCCEEDED(hr))
385     {
386         strcpyW(wszKeyName, wszClsidSlash);
387         strcatW(wszKeyName, wClsidCategory);
388         strcatW(wszKeyName, wszSlashInstance);
389         if (szInstance)
390             strcatW(wszKeyName, szInstance);
391         else
392         {
393             hr = StringFromCLSID(Filter, &wFilter);
394             if (SUCCEEDED(hr))
395                 strcatW(wszKeyName, wFilter);
396         }
397     }
398
399     if (SUCCEEDED(hr))
400     {
401         LONG lRet = RegDeleteKeyW(HKEY_CLASSES_ROOT, wszKeyName);
402         hr = HRESULT_FROM_WIN32(lRet);
403     }
404
405     CoTaskMemFree(wClsidCategory);
406     CoTaskMemFree(wFilter);
407
408     return hr;
409 }
410
411 static HRESULT FM2_WriteFriendlyName(IPropertyBag * pPropBag, LPCWSTR szName)
412 {
413     VARIANT var;
414
415     V_VT(&var) = VT_BSTR;
416     V_UNION(&var, bstrVal) = (BSTR)szName;
417
418     return IPropertyBag_Write(pPropBag, wszFriendlyName, &var);
419 }
420
421 static HRESULT FM2_WriteClsid(IPropertyBag * pPropBag, REFCLSID clsid)
422 {
423     LPWSTR wszClsid = NULL;
424     VARIANT var;
425     HRESULT hr;
426
427     hr = StringFromCLSID(clsid, &wszClsid);
428
429     if (SUCCEEDED(hr))
430     {
431         V_VT(&var) = VT_BSTR;
432         V_UNION(&var, bstrVal) = wszClsid;
433         hr = IPropertyBag_Write(pPropBag, wszClsidName, &var);
434     }
435     CoTaskMemFree(wszClsid);
436     return hr;
437 }
438
439 static HRESULT FM2_WriteFilterData(const REGFILTER2 * prf2, BYTE **ppData, ULONG *pcbData)
440 {
441     int size = sizeof(struct REG_RF);
442     unsigned int i;
443     struct Vector mainStore = {NULL, 0, 0};
444     struct Vector clsidStore = {NULL, 0, 0};
445     struct REG_RF rrf;
446     HRESULT hr = S_OK;
447
448     rrf.dwVersion = prf2->dwVersion;
449     rrf.dwMerit = prf2->dwMerit;
450     rrf.dwPins = prf2->u.s1.cPins2;
451     rrf.dwUnused = 0;
452
453     add_data(&mainStore, (LPBYTE)&rrf, sizeof(rrf));
454
455     for (i = 0; i < prf2->u.s1.cPins2; i++)
456     {
457         size += sizeof(struct REG_RFP);
458         if (prf2->u.s1.rgPins2[i].clsPinCategory)
459             size += sizeof(DWORD);
460         size += prf2->u.s1.rgPins2[i].nMediaTypes * sizeof(struct REG_TYPE);
461         size += prf2->u.s1.rgPins2[i].nMediums * sizeof(DWORD);
462     }
463
464     for (i = 0; i < prf2->u.s1.cPins2; i++)
465     {
466         struct REG_RFP rrfp;
467         REGFILTERPINS2 rgPin2 = prf2->u.s1.rgPins2[i];
468         unsigned int j;
469
470         rrfp.signature[0] = '0';
471         rrfp.signature[1] = 'p';
472         rrfp.signature[2] = 'i';
473         rrfp.signature[3] = '3';
474         rrfp.signature[0] += i;
475         rrfp.dwFlags = rgPin2.dwFlags;
476         rrfp.dwInstances = rgPin2.cInstances;
477         rrfp.dwMediaTypes = rgPin2.nMediaTypes;
478         rrfp.dwMediums = rgPin2.nMediums;
479         rrfp.bCategory = rgPin2.clsPinCategory ? 1 : 0;
480
481         add_data(&mainStore, (LPBYTE)&rrfp, sizeof(rrfp));
482         if (rrfp.bCategory)
483         {
484             DWORD index = find_data(&clsidStore, (const BYTE*)rgPin2.clsPinCategory, sizeof(CLSID));
485             if (index == -1)
486                 index = add_data(&clsidStore, (const BYTE*)rgPin2.clsPinCategory, sizeof(CLSID));
487             index += size;
488
489             add_data(&mainStore, (LPBYTE)&index, sizeof(index));
490         }
491
492         for (j = 0; j < rgPin2.nMediaTypes; j++)
493         {
494             struct REG_TYPE rt;
495             rt.signature[0] = '0';
496             rt.signature[1] = 't';
497             rt.signature[2] = 'y';
498             rt.signature[3] = '3';
499             rt.signature[0] += j;
500
501             rt.dwUnused = 0;
502             rt.dwOffsetMajor = find_data(&clsidStore, (const BYTE*)rgPin2.lpMediaType[j].clsMajorType, sizeof(CLSID));
503             if (rt.dwOffsetMajor == -1)
504                 rt.dwOffsetMajor = add_data(&clsidStore, (const BYTE*)rgPin2.lpMediaType[j].clsMajorType, sizeof(CLSID));
505             rt.dwOffsetMajor += size;
506             rt.dwOffsetMinor = find_data(&clsidStore, (const BYTE*)rgPin2.lpMediaType[j].clsMinorType, sizeof(CLSID));
507             if (rt.dwOffsetMinor == -1)
508                 rt.dwOffsetMinor = add_data(&clsidStore, (const BYTE*)rgPin2.lpMediaType[j].clsMinorType, sizeof(CLSID));
509             rt.dwOffsetMinor += size;
510
511             add_data(&mainStore, (LPBYTE)&rt, sizeof(rt));
512         }
513
514         for (j = 0; j < rgPin2.nMediums; j++)
515         {
516             DWORD index = find_data(&clsidStore, (const BYTE*)(rgPin2.lpMedium + j), sizeof(REGPINMEDIUM));
517             if (index == -1)
518                 index = add_data(&clsidStore, (const BYTE*)(rgPin2.lpMedium + j), sizeof(REGPINMEDIUM));
519             index += size;
520
521             add_data(&mainStore, (LPBYTE)&index, sizeof(index));
522         }
523     }
524
525     if (SUCCEEDED(hr))
526     {
527         *pcbData = mainStore.current + clsidStore.current;
528         *ppData = CoTaskMemAlloc(*pcbData);
529         if (!*ppData)
530             hr = E_OUTOFMEMORY;
531     }
532
533     if (SUCCEEDED(hr))
534     {
535         memcpy(*ppData, mainStore.pData, mainStore.current);
536         memcpy((*ppData) + mainStore.current, clsidStore.pData, clsidStore.current);
537     }
538
539     delete_vector(&mainStore);
540     delete_vector(&clsidStore);
541     return hr;
542 }
543
544 static HRESULT FM2_ReadFilterData(BYTE *pData, REGFILTER2 * prf2)
545 {
546     HRESULT hr = S_OK;
547     struct REG_RF * prrf;
548     LPBYTE pCurrent;
549     DWORD i;
550     REGFILTERPINS2 * rgPins2;
551
552     prrf = (struct REG_RF *)pData;
553     pCurrent = pData;
554
555     if (prrf->dwVersion != 2)
556     {
557         FIXME("Filter registry version %d not supported\n", prrf->dwVersion);
558         ZeroMemory(prf2, sizeof(*prf2));
559         hr = E_FAIL;
560     }
561
562     if (SUCCEEDED(hr))
563     {
564         TRACE("version = %d, merit = %x, #pins = %d, unused = %x\n",
565             prrf->dwVersion, prrf->dwMerit, prrf->dwPins, prrf->dwUnused);
566
567         prf2->dwVersion = prrf->dwVersion;
568         prf2->dwMerit = prrf->dwMerit;
569         prf2->u.s1.cPins2 = prrf->dwPins;
570         rgPins2 = CoTaskMemAlloc(prrf->dwPins * sizeof(*rgPins2));
571         prf2->u.s1.rgPins2 = rgPins2;
572         pCurrent += sizeof(struct REG_RF);
573
574         for (i = 0; i < prrf->dwPins; i++)
575         {
576             struct REG_RFP * prrfp = (struct REG_RFP *)pCurrent;
577             REGPINTYPES * lpMediaType;
578             REGPINMEDIUM * lpMedium;
579             UINT j;
580
581             /* FIXME: check signature */
582
583             TRACE("\tsignature = %s\n", debugstr_an((const char*)prrfp->signature, 4));
584
585             TRACE("\tpin[%d]: flags = %x, instances = %d, media types = %d, mediums = %d\n",
586                 i, prrfp->dwFlags, prrfp->dwInstances, prrfp->dwMediaTypes, prrfp->dwMediums);
587
588             rgPins2[i].dwFlags = prrfp->dwFlags;
589             rgPins2[i].cInstances = prrfp->dwInstances;
590             rgPins2[i].nMediaTypes = prrfp->dwMediaTypes;
591             rgPins2[i].nMediums = prrfp->dwMediums;
592             pCurrent += sizeof(struct REG_RFP);
593             if (prrfp->bCategory)
594             {
595                 CLSID * clsCat = CoTaskMemAlloc(sizeof(CLSID));
596                 memcpy(clsCat, pData + *(DWORD*)(pCurrent), sizeof(CLSID));
597                 pCurrent += sizeof(DWORD);
598                 rgPins2[i].clsPinCategory = clsCat;
599             }
600             else
601                 rgPins2[i].clsPinCategory = NULL;
602
603             if (rgPins2[i].nMediaTypes > 0)
604                 lpMediaType = CoTaskMemAlloc(rgPins2[i].nMediaTypes * sizeof(*lpMediaType));
605             else
606                 lpMediaType = NULL;
607
608             rgPins2[i].lpMediaType = lpMediaType;
609
610             for (j = 0; j < rgPins2[i].nMediaTypes; j++)
611             {
612                 struct REG_TYPE * prt = (struct REG_TYPE *)pCurrent;
613                 CLSID * clsMajor = CoTaskMemAlloc(sizeof(CLSID));
614                 CLSID * clsMinor = CoTaskMemAlloc(sizeof(CLSID));
615
616                 /* FIXME: check signature */
617                 TRACE("\t\tsignature = %s\n", debugstr_an((const char*)prt->signature, 4));
618
619                 memcpy(clsMajor, pData + prt->dwOffsetMajor, sizeof(CLSID));
620                 memcpy(clsMinor, pData + prt->dwOffsetMinor, sizeof(CLSID));
621
622                 lpMediaType[j].clsMajorType = clsMajor;
623                 lpMediaType[j].clsMinorType = clsMinor;
624
625                 pCurrent += sizeof(*prt);
626             }
627
628             if (rgPins2[i].nMediums > 0)
629                 lpMedium = CoTaskMemAlloc(rgPins2[i].nMediums * sizeof(*lpMedium));
630             else
631                 lpMedium = NULL;
632
633             rgPins2[i].lpMedium = lpMedium;
634
635             for (j = 0; j < rgPins2[i].nMediums; j++)
636             {
637                 DWORD dwOffset = *(DWORD *)pCurrent;
638
639                 memcpy(lpMedium + j, pData + dwOffset, sizeof(REGPINMEDIUM));
640
641                 pCurrent += sizeof(dwOffset);
642             }
643         }
644
645     }
646
647     return hr;
648 }
649
650 static void FM2_DeleteRegFilter(REGFILTER2 * prf2)
651 {
652     UINT i;
653     for (i = 0; i < prf2->u.s1.cPins2; i++)
654     {
655         UINT j;
656         if (prf2->u.s1.rgPins2[i].clsPinCategory)
657             CoTaskMemFree((LPVOID)prf2->u.s1.rgPins2[i].clsPinCategory);
658
659         for (j = 0; j < prf2->u.s1.rgPins2[i].nMediaTypes; j++)
660         {
661             CoTaskMemFree((LPVOID)prf2->u.s1.rgPins2[i].lpMediaType[j].clsMajorType);
662             CoTaskMemFree((LPVOID)prf2->u.s1.rgPins2[i].lpMediaType[j].clsMinorType);
663         }
664         CoTaskMemFree((LPVOID)prf2->u.s1.rgPins2[i].lpMediaType);
665         CoTaskMemFree((LPVOID)prf2->u.s1.rgPins2[i].lpMedium);
666     }
667     CoTaskMemFree((LPVOID)prf2->u.s1.rgPins2);
668 }
669
670 static HRESULT WINAPI FilterMapper2_RegisterFilter(
671     IFilterMapper2 * iface,
672     REFCLSID clsidFilter,
673     LPCWSTR szName,
674     IMoniker **ppMoniker,
675     const CLSID *pclsidCategory,
676     const OLECHAR *szInstance,
677     const REGFILTER2 *prf2)
678 {
679     IParseDisplayName * pParser = NULL;
680     IBindCtx * pBindCtx = NULL;
681     IMoniker * pMoniker = NULL;
682     IPropertyBag * pPropBag = NULL;
683     HRESULT hr;
684     LPWSTR pwszParseName = NULL;
685     LPWSTR pCurrent;
686     static const WCHAR wszDevice[] = {'@','d','e','v','i','c','e',':','s','w',':',0};
687     int nameLen;
688     ULONG ulEaten;
689     LPWSTR szClsidTemp = NULL;
690     REGFILTER2 regfilter2;
691     REGFILTERPINS2* pregfp2 = NULL;
692
693     TRACE("(%s, %s, %p, %s, %s, %p)\n",
694         debugstr_guid(clsidFilter),
695         debugstr_w(szName),
696         ppMoniker,
697         debugstr_guid(pclsidCategory),
698         debugstr_w(szInstance),
699         prf2);
700
701     if (prf2->dwVersion == 2)
702     {
703         regfilter2 = *prf2;
704     }
705     else if (prf2->dwVersion == 1)
706     {
707         ULONG i;
708         DWORD flags;
709         /* REGFILTER2 structure is converted from version 1 to 2. Tested on Win2k. */
710         regfilter2.dwVersion = 2;
711         regfilter2.dwMerit = prf2->dwMerit;
712         regfilter2.u.s1.cPins2 = prf2->u.s.cPins;
713         pregfp2 = CoTaskMemAlloc(prf2->u.s.cPins * sizeof(REGFILTERPINS2));
714         regfilter2.u.s1.rgPins2 = pregfp2;
715         for (i = 0; i < prf2->u.s.cPins; i++)
716         {
717             flags = 0;
718             if (prf2->u.s.rgPins[i].bRendered)
719                 flags |= REG_PINFLAG_B_RENDERER;
720             if (prf2->u.s.rgPins[i].bOutput)
721                 flags |= REG_PINFLAG_B_OUTPUT;
722             if (prf2->u.s.rgPins[i].bZero)
723                 flags |= REG_PINFLAG_B_ZERO;
724             if (prf2->u.s.rgPins[i].bMany)
725                 flags |= REG_PINFLAG_B_MANY;
726             pregfp2[i].dwFlags = flags;
727             pregfp2[i].cInstances = 1;
728             pregfp2[i].nMediaTypes = prf2->u.s.rgPins[i].nMediaTypes;
729             pregfp2[i].lpMediaType = prf2->u.s.rgPins[i].lpMediaType;
730             pregfp2[i].nMediums = 0;
731             pregfp2[i].lpMedium = NULL;
732             pregfp2[i].clsPinCategory = NULL;
733         }
734     }
735     else
736     {
737         FIXME("dwVersion other that 1 or 2 not supported at the moment\n");
738         return E_NOTIMPL;
739     }
740
741     if (ppMoniker)
742         *ppMoniker = NULL;
743
744     if (!pclsidCategory)
745         /* MSDN mentions the inexistent CLSID_ActiveMovieFilters GUID.
746          * In fact this is the CLSID_LegacyAmFilterCategory one */
747         pclsidCategory = &CLSID_LegacyAmFilterCategory;
748
749     /* sizeof... will include the null terminator and
750      * the + 1 is for the separator ('\\'). The -1 is
751      * because CHARS_IN_GUID includes the null terminator
752      */
753     nameLen = sizeof(wszDevice)/sizeof(wszDevice[0]) + CHARS_IN_GUID - 1 + 1;
754
755     if (szInstance)
756         nameLen += strlenW(szInstance);
757     else
758         nameLen += CHARS_IN_GUID - 1; /* CHARS_IN_GUID includes null terminator */
759
760     pCurrent = pwszParseName = CoTaskMemAlloc(nameLen*sizeof(WCHAR));
761     if (!pwszParseName)
762         return E_OUTOFMEMORY;
763
764     strcpyW(pwszParseName, wszDevice);
765     pCurrent += strlenW(wszDevice);
766
767     hr = StringFromCLSID(pclsidCategory, &szClsidTemp);
768
769     if (SUCCEEDED(hr))
770     {
771         memcpy(pCurrent, szClsidTemp, CHARS_IN_GUID * sizeof(WCHAR));
772         pCurrent += CHARS_IN_GUID - 1;
773         pCurrent[0] = '\\';
774
775         if (szInstance)
776             strcpyW(pCurrent+1, szInstance);
777         else
778         {
779             CoTaskMemFree(szClsidTemp);
780             szClsidTemp = NULL;
781
782             hr = StringFromCLSID(clsidFilter, &szClsidTemp);
783             if (SUCCEEDED(hr))
784                 strcpyW(pCurrent+1, szClsidTemp);
785         }
786     }
787
788     if (SUCCEEDED(hr))
789         hr = CoCreateInstance(&CLSID_CDeviceMoniker, NULL, CLSCTX_INPROC, &IID_IParseDisplayName, (LPVOID *)&pParser);
790
791     if (SUCCEEDED(hr))
792         hr = CreateBindCtx(0, &pBindCtx);
793
794     if (SUCCEEDED(hr))
795         hr = IParseDisplayName_ParseDisplayName(pParser, pBindCtx, pwszParseName, &ulEaten, &pMoniker);
796
797     if (pBindCtx)
798         IBindCtx_Release(pBindCtx);
799     if (pParser)
800         IParseDisplayName_Release(pParser);
801
802     if (SUCCEEDED(hr))
803         hr = IMoniker_BindToStorage(pMoniker, NULL, NULL, &IID_IPropertyBag, (LPVOID)&pPropBag);
804
805     if (SUCCEEDED(hr))
806         hr = FM2_WriteFriendlyName(pPropBag, szName);
807
808     if (SUCCEEDED(hr))
809         hr = FM2_WriteClsid(pPropBag, clsidFilter);
810
811     if (SUCCEEDED(hr))
812     {
813         BYTE *pData;
814         ULONG cbData;
815
816         hr = FM2_WriteFilterData(&regfilter2, &pData, &cbData);
817         if (SUCCEEDED(hr))
818         {
819             VARIANT var;
820             SAFEARRAY *psa;
821             SAFEARRAYBOUND saBound;
822
823             saBound.lLbound = 0;
824             saBound.cElements = cbData;
825             psa = SafeArrayCreate(VT_UI1, 1, &saBound);
826             if (!psa)
827             {
828                 ERR("Couldn't create SAFEARRAY\n");
829                 hr = E_FAIL;
830             }
831
832             if (SUCCEEDED(hr))
833             {
834                 LPBYTE pbSAData;
835                 hr = SafeArrayAccessData(psa, (LPVOID *)&pbSAData);
836                 if (SUCCEEDED(hr))
837                 {
838                     memcpy(pbSAData, pData, cbData);
839                     hr = SafeArrayUnaccessData(psa);
840                 }
841             }
842
843             V_VT(&var) = VT_ARRAY | VT_UI1;
844             V_UNION(&var, parray) = psa;
845
846             if (SUCCEEDED(hr))
847                 hr = IPropertyBag_Write(pPropBag, wszFilterDataName, &var);
848
849             if (psa)
850                 SafeArrayDestroy(psa);
851             CoTaskMemFree(pData);
852         }
853     }
854
855     if (pPropBag)
856         IPropertyBag_Release(pPropBag);
857     CoTaskMemFree(szClsidTemp);
858     CoTaskMemFree(pwszParseName);
859
860     if (SUCCEEDED(hr) && ppMoniker)
861         *ppMoniker = pMoniker;
862     else if (pMoniker)
863         IMoniker_Release(pMoniker);
864
865     CoTaskMemFree(pregfp2);
866
867     TRACE("-- returning %x\n", hr);
868
869     return hr;
870 }
871
872 /* internal helper function */
873 static BOOL MatchTypes(
874     BOOL bExactMatch,
875     DWORD nPinTypes,
876     const REGPINTYPES * pPinTypes,
877     DWORD nMatchTypes,
878     const GUID * pMatchTypes)
879 {
880     BOOL bMatch = FALSE;
881     DWORD j;
882
883     if ((nMatchTypes == 0) && (nPinTypes > 0))
884         bMatch = TRUE;
885
886     for (j = 0; j < nPinTypes; j++)
887     {
888         DWORD i;
889         for (i = 0; i < nMatchTypes*2; i+=2)
890         {
891             if (((!bExactMatch && IsEqualGUID(pPinTypes[j].clsMajorType, &GUID_NULL)) || IsEqualGUID(&pMatchTypes[i], &GUID_NULL) || IsEqualGUID(pPinTypes[j].clsMajorType, &pMatchTypes[i])) &&
892                 ((!bExactMatch && IsEqualGUID(pPinTypes[j].clsMinorType, &GUID_NULL)) || IsEqualGUID(&pMatchTypes[i+1], &GUID_NULL) || IsEqualGUID(pPinTypes[j].clsMinorType, &pMatchTypes[i+1])))
893             {
894                 bMatch = TRUE;
895                 break;
896             }
897         }
898     }
899     return bMatch;
900 }
901
902 /* internal helper function for qsort of MONIKER_MERIT array */
903 static int mm_compare(const void * left, const void * right)
904 {
905     const struct MONIKER_MERIT * mmLeft = (const struct MONIKER_MERIT *)left;
906     const struct MONIKER_MERIT * mmRight = (const struct MONIKER_MERIT *)right;
907
908     if (mmLeft->dwMerit == mmRight->dwMerit)
909         return 0;
910     if (mmLeft->dwMerit > mmRight->dwMerit)
911         return -1;
912     return 1;
913 }
914
915 /* NOTES:
916  *   Exact match means whether or not to treat GUID_NULL's in filter data as wild cards
917  *    (GUID_NULL's in input to function automatically treated as wild cards)
918  *   Input/Output needed means match only on criteria if TRUE (with zero input types
919  *    meaning match any input/output pin as long as one exists), otherwise match any
920  *    filter that meets the rest of the requirements.
921  */
922 static HRESULT WINAPI FilterMapper2_EnumMatchingFilters(
923     IFilterMapper2 * iface,
924     IEnumMoniker **ppEnum,
925     DWORD dwFlags,
926     BOOL bExactMatch,
927     DWORD dwMerit,
928     BOOL bInputNeeded,
929     DWORD cInputTypes,
930     const GUID *pInputTypes,
931     const REGPINMEDIUM *pMedIn,
932     const CLSID *pPinCategoryIn,
933     BOOL bRender,
934     BOOL bOutputNeeded,
935     DWORD cOutputTypes,
936     const GUID *pOutputTypes,
937     const REGPINMEDIUM *pMedOut,
938     const CLSID *pPinCategoryOut)
939 {
940     ICreateDevEnum * pCreateDevEnum;
941     IMoniker * pMonikerCat;
942     IEnumMoniker * pEnumCat;
943     HRESULT hr;
944     struct Vector monikers = {NULL, 0, 0};
945
946     TRACE("(%p, %x, %s, %x, %s, %d, %p, %p, %p, %s, %s, %p, %p, %p)\n",
947         ppEnum,
948         dwFlags,
949         bExactMatch ? "true" : "false",
950         dwMerit,
951         bInputNeeded ? "true" : "false",
952         cInputTypes,
953         pInputTypes,
954         pMedIn,
955         pPinCategoryIn,
956         bRender ? "true" : "false",
957         bOutputNeeded ? "true" : "false",
958         pOutputTypes,
959         pMedOut,
960         pPinCategoryOut);
961
962     if (dwFlags != 0)
963     {
964         FIXME("dwFlags = %x not implemented\n", dwFlags);
965     }
966
967     *ppEnum = NULL;
968
969     hr = CoCreateInstance(&CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC, &IID_ICreateDevEnum, (LPVOID*)&pCreateDevEnum);
970     if (FAILED(hr))
971         return hr;
972
973     hr = ICreateDevEnum_CreateClassEnumerator(pCreateDevEnum, &CLSID_ActiveMovieCategories, &pEnumCat, 0);
974     if (FAILED(hr)) {
975         ICreateDevEnum_Release(pCreateDevEnum);
976         return hr;
977     }
978
979     while (IEnumMoniker_Next(pEnumCat, 1, &pMonikerCat, NULL) == S_OK)
980     {
981         IPropertyBag * pPropBagCat = NULL;
982         VARIANT var;
983         HRESULT hrSub; /* this is so that one buggy filter
984                           doesn't make the whole lot fail */
985
986         VariantInit(&var);
987
988         hrSub = IMoniker_BindToStorage(pMonikerCat, NULL, NULL, &IID_IPropertyBag, (LPVOID*)&pPropBagCat);
989
990         if (SUCCEEDED(hrSub))
991             hrSub = IPropertyBag_Read(pPropBagCat, wszMeritName, &var, NULL);
992
993         if (SUCCEEDED(hrSub) && (V_UNION(&var, ulVal) >= dwMerit))
994         {
995             CLSID clsidCat;
996             IEnumMoniker * pEnum;
997             IMoniker * pMoniker;
998
999             VariantClear(&var);
1000
1001             if (TRACE_ON(quartz))
1002             {
1003                 VARIANT temp;
1004                 V_VT(&temp) = VT_EMPTY;
1005                 IPropertyBag_Read(pPropBagCat, wszFriendlyName, &temp, NULL);
1006                 TRACE("Considering category %s\n", debugstr_w(V_UNION(&temp, bstrVal)));
1007                 VariantClear(&temp);
1008             }
1009
1010             hrSub = IPropertyBag_Read(pPropBagCat, wszClsidName, &var, NULL);
1011
1012             if (SUCCEEDED(hrSub))
1013                 hrSub = CLSIDFromString(V_UNION(&var, bstrVal), &clsidCat);
1014
1015             if (SUCCEEDED(hrSub))
1016                 hrSub = ICreateDevEnum_CreateClassEnumerator(pCreateDevEnum, &clsidCat, &pEnum, 0);
1017
1018             if (hrSub == S_OK)
1019             {
1020                 while (IEnumMoniker_Next(pEnum, 1, &pMoniker, NULL) == S_OK)
1021                 {
1022                     IPropertyBag * pPropBag = NULL;
1023                     VARIANT var;
1024                     BYTE *pData = NULL;
1025                     REGFILTER2 rf2;
1026                     DWORD i;
1027                     BOOL bInputMatch = !bInputNeeded;
1028                     BOOL bOutputMatch = !bOutputNeeded;
1029
1030                     ZeroMemory(&rf2, sizeof(rf2));
1031                     VariantInit(&var);
1032
1033                     hrSub = IMoniker_BindToStorage(pMoniker, NULL, NULL, &IID_IPropertyBag, (LPVOID*)&pPropBag);
1034
1035                     if (TRACE_ON(quartz))
1036                     {
1037                         VARIANT temp;
1038                         V_VT(&temp) = VT_EMPTY;
1039                         IPropertyBag_Read(pPropBag, wszFriendlyName, &temp, NULL);
1040                         TRACE("Considering filter %s\n", debugstr_w(V_UNION(&temp, bstrVal)));
1041                         VariantClear(&temp);
1042                     }
1043
1044                     if (SUCCEEDED(hrSub))
1045                     {
1046                         hrSub = IPropertyBag_Read(pPropBag, wszFilterDataName, &var, NULL);
1047                     }
1048
1049                     if (SUCCEEDED(hrSub))
1050                         hrSub = SafeArrayAccessData(V_UNION(&var, parray), (LPVOID*)&pData);
1051
1052                     if (SUCCEEDED(hrSub))
1053                         hrSub = FM2_ReadFilterData(pData, &rf2);
1054
1055                     if (pData)
1056                         SafeArrayUnaccessData(V_UNION(&var, parray));
1057
1058                     VariantClear(&var);
1059
1060                     /* Logic used for bInputMatch expression:
1061                      * There exists some pin such that bInputNeeded implies (pin is an input and
1062                      * (bRender implies pin has render flag) and major/minor types members of
1063                      * pInputTypes )
1064                      * bOutputMatch is similar, but without the "bRender implies ..." part
1065                      * and substituting variables names containing input for output
1066                      */
1067
1068                     /* determine whether filter meets requirements */
1069                     if (SUCCEEDED(hrSub) && (rf2.dwMerit >= dwMerit))
1070                     {
1071                         for (i = 0; (i < rf2.u.s1.cPins2) && (!bInputMatch || !bOutputMatch); i++)
1072                         {
1073                             const REGFILTERPINS2 * rfp2 = rf2.u.s1.rgPins2 + i;
1074
1075                             bInputMatch = bInputMatch || (!(rfp2->dwFlags & REG_PINFLAG_B_OUTPUT) &&
1076                                 (!bRender || (rfp2->dwFlags & REG_PINFLAG_B_RENDERER)) &&
1077                                 MatchTypes(bExactMatch, rfp2->nMediaTypes, rfp2->lpMediaType, cInputTypes, pInputTypes));
1078                             bOutputMatch = bOutputMatch || ((rfp2->dwFlags & REG_PINFLAG_B_OUTPUT) &&
1079                                 MatchTypes(bExactMatch, rfp2->nMediaTypes, rfp2->lpMediaType, cOutputTypes, pOutputTypes));
1080                         }
1081
1082                         if (bInputMatch && bOutputMatch)
1083                         {
1084                             struct MONIKER_MERIT mm = {pMoniker, rf2.dwMerit};
1085                             IMoniker_AddRef(pMoniker);
1086                             add_data(&monikers, (LPBYTE)&mm, sizeof(mm));
1087                         }
1088                     }
1089
1090                     FM2_DeleteRegFilter(&rf2);
1091                     if (pPropBag)
1092                         IPropertyBag_Release(pPropBag);
1093                     IMoniker_Release(pMoniker);
1094                 }
1095                 IEnumMoniker_Release(pEnum);
1096             }
1097         }
1098
1099         VariantClear(&var);
1100         if (pPropBagCat)
1101             IPropertyBag_Release(pPropBagCat);
1102         IMoniker_Release(pMonikerCat);
1103     }
1104
1105     if (SUCCEEDED(hr))
1106     {
1107         IMoniker ** ppMoniker;
1108         unsigned int i;
1109         ULONG nMonikerCount = monikers.current / sizeof(struct MONIKER_MERIT);
1110
1111         /* sort the monikers in descending merit order */
1112         qsort(monikers.pData, nMonikerCount,
1113               sizeof(struct MONIKER_MERIT),
1114               mm_compare);
1115
1116         /* construct an IEnumMoniker interface */
1117         ppMoniker = CoTaskMemAlloc(nMonikerCount * sizeof(IMoniker *));
1118         for (i = 0; i < nMonikerCount; i++)
1119         {
1120             /* no need to AddRef here as already AddRef'd above */
1121             ppMoniker[i] = ((struct MONIKER_MERIT *)monikers.pData)[i].pMoniker;
1122         }
1123         hr = EnumMonikerImpl_Create(ppMoniker, nMonikerCount, ppEnum);
1124         CoTaskMemFree(ppMoniker);
1125     }
1126
1127     delete_vector(&monikers);
1128     IEnumMoniker_Release(pEnumCat);
1129     ICreateDevEnum_Release(pCreateDevEnum);
1130
1131     return hr;
1132 }
1133
1134 static const IFilterMapper2Vtbl fm2vtbl =
1135 {
1136
1137     FilterMapper2_QueryInterface,
1138     FilterMapper2_AddRef,
1139     FilterMapper2_Release,
1140
1141     FilterMapper2_CreateCategory,
1142     FilterMapper2_UnregisterFilter,
1143     FilterMapper2_RegisterFilter,
1144     FilterMapper2_EnumMatchingFilters
1145 };
1146
1147 /*** IUnknown methods ***/
1148
1149 static HRESULT WINAPI FilterMapper_QueryInterface(IFilterMapper * iface, REFIID riid, LPVOID *ppv)
1150 {
1151     FilterMapper2Impl *This = impl_from_IFilterMapper(iface);
1152
1153     TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
1154
1155     return FilterMapper2_QueryInterface((IFilterMapper2*)&This->lpVtbl, riid, ppv);
1156 }
1157
1158 static ULONG WINAPI FilterMapper_AddRef(IFilterMapper * iface)
1159 {
1160     FilterMapper2Impl *This = impl_from_IFilterMapper(iface);
1161
1162     return FilterMapper2_AddRef((IFilterMapper2*)This);
1163 }
1164
1165 static ULONG WINAPI FilterMapper_Release(IFilterMapper * iface)
1166 {
1167     FilterMapper2Impl *This = impl_from_IFilterMapper(iface);
1168
1169     return FilterMapper2_Release((IFilterMapper2*)This);
1170 }
1171
1172 /*** IFilterMapper methods ***/
1173
1174 static HRESULT WINAPI FilterMapper_EnumMatchingFilters(
1175     IFilterMapper * iface,
1176     IEnumRegFilters **ppEnum,
1177     DWORD dwMerit,
1178     BOOL bInputNeeded,
1179     CLSID clsInMaj,
1180     CLSID clsInSub,
1181     BOOL bRender,
1182     BOOL bOutputNeeded,
1183     CLSID clsOutMaj,
1184     CLSID clsOutSub)
1185 {
1186     FilterMapper2Impl *This = impl_from_IFilterMapper(iface);
1187     GUID InputType[2];
1188     GUID OutputType[2];
1189     IEnumMoniker* ppEnumMoniker;
1190     IMoniker* IMon;
1191     ULONG nb;
1192     ULONG idx = 0, nb_mon = 0;
1193     REGFILTER* regfilters;
1194     HRESULT hr;
1195
1196     TRACE("(%p/%p)->(%p, %x, %s, %s, %s, %s, %s, %s, %s) stub!\n",
1197         iface,This,
1198         ppEnum,
1199         dwMerit,
1200         bInputNeeded ? "true" : "false",
1201         debugstr_guid(&clsInMaj),
1202         debugstr_guid(&clsInSub),
1203         bRender ? "true" : "false",
1204         bOutputNeeded ? "true" : "false",
1205         debugstr_guid(&clsOutMaj),
1206         debugstr_guid(&clsOutSub));
1207
1208     InputType[0] = clsInMaj;
1209     InputType[1] = clsInSub;
1210     OutputType[0] = clsOutMaj;
1211     OutputType[1] = clsOutSub;
1212
1213     *ppEnum = NULL;
1214
1215     hr = IFilterMapper2_EnumMatchingFilters((IFilterMapper2*)This,
1216                                        &ppEnumMoniker,
1217                                        0,
1218                                        TRUE,
1219                                        dwMerit,
1220                                        bInputNeeded,
1221                                        1,
1222                                        InputType,
1223                                        NULL,
1224                                        &GUID_NULL,
1225                                        bRender,
1226                                        bOutputNeeded,
1227                                        1,
1228                                        OutputType,
1229                                        NULL,
1230                                        &GUID_NULL);
1231
1232     if (!SUCCEEDED(hr))
1233         return hr;
1234     
1235     while(IEnumMoniker_Next(ppEnumMoniker, 1, &IMon, &nb) == S_OK)
1236     {
1237         IMoniker_Release(IMon);
1238         nb_mon++;
1239     }
1240
1241     if (!nb_mon)
1242     {
1243         IEnumMoniker_Release(ppEnumMoniker);
1244         return IEnumRegFiltersImpl_Construct(NULL, 0, ppEnum);
1245     }
1246
1247     regfilters = CoTaskMemAlloc(nb_mon * sizeof(REGFILTER));
1248     if (!regfilters)
1249     {
1250         IEnumMoniker_Release(ppEnumMoniker);
1251         return E_OUTOFMEMORY;
1252     }
1253     ZeroMemory(regfilters, nb_mon * sizeof(REGFILTER)); /* will prevent bad free of Name in case of error. */
1254     
1255     IEnumMoniker_Reset(ppEnumMoniker);
1256     while(IEnumMoniker_Next(ppEnumMoniker, 1, &IMon, &nb) == S_OK)
1257     {
1258         IPropertyBag * pPropBagCat = NULL;
1259         VARIANT var;
1260         HRESULT hrSub;
1261         GUID clsid;
1262         int len;
1263
1264         VariantInit(&var);
1265
1266         hrSub = IMoniker_BindToStorage(IMon, NULL, NULL, &IID_IPropertyBag, (LPVOID*)&pPropBagCat);
1267
1268         if (SUCCEEDED(hrSub))
1269             hrSub = IPropertyBag_Read(pPropBagCat, wszClsidName, &var, NULL);
1270
1271         if (SUCCEEDED(hrSub))
1272             hrSub = CLSIDFromString(V_UNION(&var, bstrVal), &clsid);
1273
1274         VariantClear(&var);
1275
1276         if (SUCCEEDED(hrSub))
1277             hrSub = IPropertyBag_Read(pPropBagCat, wszFriendlyName, &var, NULL);
1278
1279         if (SUCCEEDED(hrSub))
1280         {
1281             len = (strlenW((WCHAR*)V_UNION(&var, bstrVal))+1) * sizeof(WCHAR);
1282             if (!(regfilters[idx].Name = CoTaskMemAlloc(len*2)))
1283                 hr = E_OUTOFMEMORY;
1284         }
1285
1286         if (SUCCEEDED(hrSub) && regfilters[idx].Name)
1287         {
1288             memcpy(regfilters[idx].Name, V_UNION(&var, bstrVal), len);
1289             regfilters[idx].Clsid = clsid;
1290             idx++;
1291         }
1292
1293         if (pPropBagCat)
1294             IPropertyBag_Release(pPropBagCat);
1295         IMoniker_Release(IMon);
1296         VariantClear(&var);
1297     }
1298
1299     if (SUCCEEDED(hr))
1300     {
1301         hr = IEnumRegFiltersImpl_Construct(regfilters, nb_mon, ppEnum);
1302     }
1303
1304     for (idx = 0; idx < nb_mon; idx++)
1305         CoTaskMemFree(regfilters[idx].Name);
1306     CoTaskMemFree(regfilters);
1307     IEnumMoniker_Release(ppEnumMoniker);
1308     
1309     return hr;
1310 }
1311
1312
1313 static HRESULT WINAPI FilterMapper_RegisterFilter(IFilterMapper * iface, CLSID clsid, LPCWSTR szName, DWORD dwMerit)
1314 {
1315     HRESULT hr;
1316     LPWSTR wszClsid = NULL;
1317     HKEY hKey;
1318     LONG lRet;
1319     WCHAR wszKeyName[strlenW(wszFilterSlash) + (CHARS_IN_GUID-1) + 1];
1320
1321     TRACE("(%p)->(%s, %s, %x)\n", iface, debugstr_guid(&clsid), debugstr_w(szName), dwMerit);
1322
1323     hr = StringFromCLSID(&clsid, &wszClsid);
1324
1325     if (SUCCEEDED(hr))
1326     {
1327         strcpyW(wszKeyName, wszFilterSlash);
1328         strcatW(wszKeyName, wszClsid);
1329     
1330         lRet = RegCreateKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
1331         hr = HRESULT_FROM_WIN32(lRet);
1332     }
1333
1334     if (SUCCEEDED(hr))
1335     {
1336         lRet = RegSetValueExW(hKey, NULL, 0, REG_SZ, (const BYTE*)szName, (strlenW(szName) + 1) * sizeof(WCHAR));
1337         hr = HRESULT_FROM_WIN32(lRet);
1338         CloseHandle(hKey);
1339     }
1340
1341     if (SUCCEEDED(hr))
1342     {
1343         strcpyW(wszKeyName, wszClsidSlash);
1344         strcatW(wszKeyName, wszClsid);
1345     
1346         lRet = RegCreateKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
1347         hr = HRESULT_FROM_WIN32(lRet);
1348     }
1349
1350     if (SUCCEEDED(hr))
1351     {
1352         lRet = RegSetValueExW(hKey, wszMeritName, 0, REG_DWORD, (LPBYTE)&dwMerit, sizeof(dwMerit));
1353         hr = HRESULT_FROM_WIN32(lRet);
1354         CloseHandle(hKey);
1355     }
1356     
1357     CoTaskMemFree(wszClsid);
1358
1359     return hr;
1360 }
1361
1362 static HRESULT WINAPI FilterMapper_RegisterFilterInstance(IFilterMapper * iface, CLSID clsid, LPCWSTR szName, CLSID *MRId)
1363 {
1364     TRACE("(%p)->(%s, %s, %p)\n", iface, debugstr_guid(&clsid), debugstr_w(szName), MRId);
1365
1366     /* Not implemented in Windows (tested on Win2k) */
1367
1368     return E_NOTIMPL;
1369 }
1370
1371 static HRESULT WINAPI FilterMapper_RegisterPin(
1372     IFilterMapper * iface,
1373     CLSID Filter,
1374     LPCWSTR szName,
1375     BOOL bRendered,
1376     BOOL bOutput,
1377     BOOL bZero,
1378     BOOL bMany,
1379     CLSID ConnectsToFilter,
1380     LPCWSTR ConnectsToPin)
1381 {
1382     HRESULT hr;
1383     LONG lRet;
1384     LPWSTR wszClsid = NULL;
1385     HKEY hKey = NULL;
1386     HKEY hPinsKey = NULL;
1387     WCHAR * wszPinsKeyName;
1388     WCHAR wszKeyName[strlenW(wszClsidSlash) + (CHARS_IN_GUID-1) + 1];
1389
1390     TRACE("(%p)->(%s, %s, %d, %d, %d, %d, %s, %s)\n", iface, debugstr_guid(&Filter), debugstr_w(szName), bRendered,
1391                 bOutput, bZero, bMany, debugstr_guid(&ConnectsToFilter), debugstr_w(ConnectsToPin));
1392
1393     hr = StringFromCLSID(&Filter, &wszClsid);
1394
1395     if (SUCCEEDED(hr))
1396     {
1397         strcpyW(wszKeyName, wszClsidSlash);
1398         strcatW(wszKeyName, wszClsid);
1399
1400         lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, KEY_WRITE, &hKey);
1401         hr = HRESULT_FROM_WIN32(lRet);
1402     }
1403
1404     if (SUCCEEDED(hr))
1405     {
1406         wszPinsKeyName = CoTaskMemAlloc((strlenW(wszPins) + 1 + strlenW(szName) + 1) * 2);
1407         if (!wszPinsKeyName)
1408              hr = E_OUTOFMEMORY;
1409     }
1410
1411     if (SUCCEEDED(hr))
1412     {
1413         strcpyW(wszPinsKeyName, wszPins);
1414         strcatW(wszPinsKeyName, wszSlash);
1415         strcatW(wszPinsKeyName, szName);
1416     
1417         lRet = RegCreateKeyExW(hKey, wszPinsKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hPinsKey, NULL);
1418         hr = HRESULT_FROM_WIN32(lRet);
1419         CoTaskMemFree(wszPinsKeyName);
1420     }
1421
1422     if (SUCCEEDED(hr))
1423     {
1424         lRet = RegSetValueExW(hPinsKey, wszAllowedMany, 0, REG_DWORD, (LPBYTE)&bMany, sizeof(bMany));
1425         hr = HRESULT_FROM_WIN32(lRet);
1426     }
1427
1428     if (SUCCEEDED(hr))
1429     {
1430         lRet = RegSetValueExW(hPinsKey, wszAllowedZero, 0, REG_DWORD, (LPBYTE)&bZero, sizeof(bZero));
1431         hr = HRESULT_FROM_WIN32(lRet);
1432     }
1433
1434     if (SUCCEEDED(hr))
1435     {
1436         lRet = RegSetValueExW(hPinsKey, wszDirection, 0, REG_DWORD, (LPBYTE)&bOutput, sizeof(bOutput));
1437         hr = HRESULT_FROM_WIN32(lRet);
1438     }
1439
1440     if (SUCCEEDED(hr))
1441     {
1442         lRet = RegSetValueExW(hPinsKey, wszIsRendered, 0, REG_DWORD, (LPBYTE)&bRendered, sizeof(bRendered));
1443         hr = HRESULT_FROM_WIN32(lRet);
1444     }
1445
1446     if (SUCCEEDED(hr))
1447     {
1448         HKEY hkeyDummy = NULL;
1449
1450         lRet = RegCreateKeyExW(hPinsKey, wszTypes, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkeyDummy, NULL);
1451         hr = HRESULT_FROM_WIN32(lRet);
1452
1453         if (hkeyDummy) RegCloseKey(hkeyDummy);
1454     }
1455
1456     CoTaskMemFree(wszClsid);
1457     if (hKey)
1458         CloseHandle(hKey);
1459     if (hPinsKey)
1460         CloseHandle(hPinsKey);
1461
1462     return hr;
1463 }
1464
1465
1466 static HRESULT WINAPI FilterMapper_RegisterPinType(
1467     IFilterMapper * iface,
1468     CLSID clsFilter,
1469     LPCWSTR szName,
1470     CLSID clsMajorType,
1471     CLSID clsSubType)
1472 {
1473     HRESULT hr;
1474     LONG lRet;
1475     LPWSTR wszClsid = NULL;
1476     LPWSTR wszClsidMajorType = NULL;
1477     LPWSTR wszClsidSubType = NULL;
1478     HKEY hKey = NULL;
1479     WCHAR * wszTypesKey;
1480     WCHAR wszKeyName[MAX_PATH];
1481
1482     TRACE("(%p)->(%s, %s, %s, %s)\n", iface, debugstr_guid(&clsFilter), debugstr_w(szName),
1483                     debugstr_guid(&clsMajorType), debugstr_guid(&clsSubType));
1484
1485     hr = StringFromCLSID(&clsFilter, &wszClsid);
1486
1487     if (SUCCEEDED(hr))
1488     {
1489         hr = StringFromCLSID(&clsMajorType, &wszClsidMajorType);
1490     }
1491
1492     if (SUCCEEDED(hr))
1493     {
1494         hr = StringFromCLSID(&clsSubType, &wszClsidSubType);
1495     }
1496
1497     if (SUCCEEDED(hr))
1498     {
1499         wszTypesKey = CoTaskMemAlloc((strlenW(wszClsidSlash) + strlenW(wszClsid) + strlenW(wszPins) +
1500                         strlenW(szName) + strlenW(wszTypes) + 3 + 1) * 2);
1501         if (!wszTypesKey)
1502             hr = E_OUTOFMEMORY;
1503     }
1504
1505     if (SUCCEEDED(hr))
1506     {
1507         strcpyW(wszTypesKey, wszClsidSlash);
1508         strcatW(wszTypesKey, wszClsid);
1509         strcatW(wszTypesKey, wszSlash);
1510         strcatW(wszTypesKey, wszPins);
1511         strcatW(wszTypesKey, wszSlash);
1512         strcatW(wszTypesKey, szName);
1513         strcatW(wszTypesKey, wszSlash);
1514         strcatW(wszTypesKey, wszTypes);
1515
1516         lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszTypesKey, 0, KEY_WRITE, &hKey);
1517         hr = HRESULT_FROM_WIN32(lRet);
1518         CoTaskMemFree(wszTypesKey);
1519     }
1520
1521     if (SUCCEEDED(hr))
1522     {
1523         HKEY hkeyDummy = NULL;
1524
1525         strcpyW(wszKeyName, wszClsidMajorType);
1526         strcatW(wszKeyName, wszSlash);
1527         strcatW(wszKeyName, wszClsidSubType);
1528
1529         lRet = RegCreateKeyExW(hKey, wszKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkeyDummy, NULL);
1530         hr = HRESULT_FROM_WIN32(lRet);
1531         CloseHandle(hKey);
1532
1533         if (hkeyDummy) RegCloseKey(hkeyDummy);
1534     }
1535
1536     CoTaskMemFree(wszClsid);
1537     CoTaskMemFree(wszClsidMajorType);
1538     CoTaskMemFree(wszClsidSubType);
1539
1540     return hr;
1541 }
1542
1543 static HRESULT WINAPI FilterMapper_UnregisterFilter(IFilterMapper * iface, CLSID Filter)
1544 {
1545     HRESULT hr;
1546     LONG lRet;
1547     LPWSTR wszClsid = NULL;
1548     HKEY hKey;
1549     WCHAR wszKeyName[strlenW(wszClsidSlash) + (CHARS_IN_GUID-1) + 1];
1550
1551     TRACE("(%p)->(%s)\n", iface, debugstr_guid(&Filter));
1552
1553     hr = StringFromCLSID(&Filter, &wszClsid);
1554
1555     if (SUCCEEDED(hr))
1556     {
1557         lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszFilter, 0, KEY_WRITE, &hKey);
1558         hr = HRESULT_FROM_WIN32(lRet);
1559     }
1560
1561     if (SUCCEEDED(hr))
1562     {
1563         lRet = RegDeleteKeyW(hKey, wszClsid);
1564         hr = HRESULT_FROM_WIN32(lRet);
1565         CloseHandle(hKey);
1566     }
1567
1568     if (SUCCEEDED(hr))
1569     {
1570         strcpyW(wszKeyName, wszClsidSlash);
1571         strcatW(wszKeyName, wszClsid);
1572
1573         lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, KEY_WRITE, &hKey);
1574         hr = HRESULT_FROM_WIN32(lRet);
1575     }
1576
1577     if (SUCCEEDED(hr))
1578     {
1579         lRet = RegDeleteValueW(hKey, wszMeritName);
1580         if (lRet != ERROR_SUCCESS)
1581             hr = HRESULT_FROM_WIN32(lRet);
1582
1583         lRet = RegDeleteTreeW(hKey, wszPins);
1584         if (lRet != ERROR_SUCCESS)
1585             hr = HRESULT_FROM_WIN32(lRet);
1586
1587         CloseHandle(hKey);
1588     }
1589
1590     CoTaskMemFree(wszClsid);
1591
1592     return hr;
1593 }
1594
1595 static HRESULT WINAPI FilterMapper_UnregisterFilterInstance(IFilterMapper * iface, CLSID MRId)
1596 {
1597     TRACE("(%p)->(%s)\n", iface, debugstr_guid(&MRId));
1598
1599     /* Not implemented in Windows (tested on Win2k) */
1600
1601     return E_NOTIMPL;
1602 }
1603
1604 static HRESULT WINAPI FilterMapper_UnregisterPin(IFilterMapper * iface, CLSID Filter, LPCWSTR Name)
1605 {
1606     HRESULT hr;
1607     LONG lRet;
1608     LPWSTR wszClsid = NULL;
1609     HKEY hKey = NULL;
1610     WCHAR * wszPinNameKey;
1611     WCHAR wszKeyName[strlenW(wszClsidSlash) + (CHARS_IN_GUID-1) + 1];
1612
1613     TRACE("(%p)->(%s, %s)\n", iface, debugstr_guid(&Filter), debugstr_w(Name));
1614
1615     if (!Name)
1616         return E_INVALIDARG;
1617
1618     hr = StringFromCLSID(&Filter, &wszClsid);
1619
1620     if (SUCCEEDED(hr))
1621     {
1622         strcpyW(wszKeyName, wszClsidSlash);
1623         strcatW(wszKeyName, wszClsid);
1624
1625         lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, KEY_WRITE, &hKey);
1626         hr = HRESULT_FROM_WIN32(lRet);
1627     }
1628
1629     if (SUCCEEDED(hr))
1630     {
1631         wszPinNameKey = CoTaskMemAlloc((strlenW(wszPins) + 1 + strlenW(Name) + 1) * 2);
1632         if (!wszPinNameKey)
1633             hr = E_OUTOFMEMORY;
1634     }
1635
1636     if (SUCCEEDED(hr))
1637     {
1638         strcpyW(wszPinNameKey, wszPins);
1639         strcatW(wszPinNameKey, wszSlash);
1640         strcatW(wszPinNameKey, Name);
1641
1642         lRet = RegDeleteTreeW(hKey, wszPinNameKey);
1643         hr = HRESULT_FROM_WIN32(lRet);
1644         CoTaskMemFree(wszPinNameKey);
1645     }
1646
1647     CoTaskMemFree(wszClsid);
1648     if (hKey)
1649         CloseHandle(hKey);
1650
1651     return hr;
1652 }
1653
1654 static const IFilterMapperVtbl fmvtbl =
1655 {
1656
1657     FilterMapper_QueryInterface,
1658     FilterMapper_AddRef,
1659     FilterMapper_Release,
1660
1661     FilterMapper_RegisterFilter,
1662     FilterMapper_RegisterFilterInstance,
1663     FilterMapper_RegisterPin,
1664     FilterMapper_RegisterPinType,
1665     FilterMapper_UnregisterFilter,
1666     FilterMapper_UnregisterFilterInstance,
1667     FilterMapper_UnregisterPin,
1668     FilterMapper_EnumMatchingFilters
1669 };
1670
1671
1672 /*** IUnknown methods ***/
1673 static HRESULT WINAPI AMFilterData_QueryInterface(IAMFilterData * iface, REFIID riid, LPVOID *ppv)
1674 {
1675     FilterMapper2Impl *This = impl_from_IAMFilterData(iface);
1676
1677     return FilterMapper2_QueryInterface((IFilterMapper2*)This, riid, ppv);
1678 }
1679
1680 static ULONG WINAPI AMFilterData_AddRef(IAMFilterData * iface)
1681 {
1682     FilterMapper2Impl *This = impl_from_IAMFilterData(iface);
1683
1684     return FilterMapper2_AddRef((IFilterMapper2*)This);
1685 }
1686
1687 static ULONG WINAPI AMFilterData_Release(IAMFilterData * iface)
1688 {
1689     FilterMapper2Impl *This = impl_from_IAMFilterData(iface);
1690
1691     return FilterMapper2_Release((IFilterMapper2*)This);
1692 }
1693
1694 /*** IAMFilterData methods ***/
1695 static HRESULT WINAPI AMFilterData_ParseFilterData(IAMFilterData* iface,
1696                                                    BYTE *pData, ULONG cb,
1697                                                    BYTE **ppRegFilter2)
1698 {
1699     FilterMapper2Impl *This = impl_from_IAMFilterData(iface);
1700     HRESULT hr = S_OK;
1701     REGFILTER2 *prf2;
1702
1703     TRACE("(%p/%p)->(%p, %d, %p)\n", This, iface, pData, cb, ppRegFilter2);
1704
1705     prf2 = CoTaskMemAlloc(sizeof(*prf2));
1706     if (!prf2)
1707         return E_OUTOFMEMORY;
1708     *ppRegFilter2 = (BYTE *)&prf2;
1709
1710     hr = FM2_ReadFilterData(pData, prf2);
1711     if (FAILED(hr))
1712     {
1713         CoTaskMemFree(prf2);
1714         *ppRegFilter2 = NULL;
1715     }
1716
1717     return hr;
1718 }
1719
1720 static HRESULT WINAPI AMFilterData_CreateFilterData(IAMFilterData* iface,
1721                                                     REGFILTER2 *prf2,
1722                                                     BYTE **pRegFilterData,
1723                                                     ULONG *pcb)
1724 {
1725     FilterMapper2Impl *This = impl_from_IAMFilterData(iface);
1726
1727     TRACE("(%p/%p)->(%p, %p, %p)\n", This, iface, prf2, pRegFilterData, pcb);
1728
1729     return FM2_WriteFilterData(prf2, pRegFilterData, pcb);
1730 }
1731
1732 static const IAMFilterDataVtbl AMFilterDataVtbl = {
1733     AMFilterData_QueryInterface,
1734     AMFilterData_AddRef,
1735     AMFilterData_Release,
1736     AMFilterData_ParseFilterData,
1737     AMFilterData_CreateFilterData
1738 };