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