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