[QUARTZ]
[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 "quartz_private.h"
23
24 #include <initguid.h>
25 #include <fil_data.h>
26
27 #define ARRAYSIZE(array) (sizeof(array)/sizeof((array)[0]))
28
29 typedef struct FilterMapper3Impl
30 {
31 IUnknown IUnknown_inner;
32 IFilterMapper3 IFilterMapper3_iface;
33 IFilterMapper IFilterMapper_iface;
34 IAMFilterData IAMFilterData_iface;
35 IUnknown *outer_unk;
36 LONG ref;
37 } FilterMapper3Impl;
38
39 static inline FilterMapper3Impl *impl_from_IFilterMapper3( IFilterMapper3 *iface )
40 {
41 return CONTAINING_RECORD(iface, FilterMapper3Impl, IFilterMapper3_iface);
42 }
43
44 static inline FilterMapper3Impl *impl_from_IFilterMapper( IFilterMapper *iface )
45 {
46 return CONTAINING_RECORD(iface, FilterMapper3Impl, IFilterMapper_iface);
47 }
48
49 static inline FilterMapper3Impl *impl_from_IAMFilterData( IAMFilterData *iface )
50 {
51 return CONTAINING_RECORD(iface, FilterMapper3Impl, IAMFilterData_iface);
52 }
53
54 static inline FilterMapper3Impl *impl_from_IUnknown( IUnknown *iface )
55 {
56 return CONTAINING_RECORD(iface, FilterMapper3Impl, IUnknown_inner);
57 }
58
59 static const WCHAR wszClsidSlash[] = {'C','L','S','I','D','\\',0};
60 static const WCHAR wszSlashInstance[] = {'\\','I','n','s','t','a','n','c','e','\\',0};
61 static const WCHAR wszSlash[] = {'\\',0};
62
63 /* CLSID property in media category Moniker */
64 static const WCHAR wszClsidName[] = {'C','L','S','I','D',0};
65 /* FriendlyName property in media category Moniker */
66 static const WCHAR wszFriendlyName[] = {'F','r','i','e','n','d','l','y','N','a','m','e',0};
67 /* Merit property in media category Moniker (CLSID_ActiveMovieCategories only) */
68 static const WCHAR wszMeritName[] = {'M','e','r','i','t',0};
69 /* FilterData property in media category Moniker (not CLSID_ActiveMovieCategories) */
70 static const WCHAR wszFilterDataName[] = {'F','i','l','t','e','r','D','a','t','a',0};
71 /* For filters registered with IFilterMapper */
72 static const WCHAR wszFilterSlash[] = {'F','i','l','t','e','r','\\',0};
73 static const WCHAR wszFilter[] = {'F','i','l','t','e','r',0};
74 /* For pins registered with IFilterMapper */
75 static const WCHAR wszPins[] = {'P','i','n','s',0};
76 static const WCHAR wszAllowedMany[] = {'A','l','l','o','w','e','d','M','a','n','y',0};
77 static const WCHAR wszAllowedZero[] = {'A','l','l','o','w','e','d','Z','e','r','o',0};
78 static const WCHAR wszDirection[] = {'D','i','r','e','c','t','i','o','n',0};
79 static const WCHAR wszIsRendered[] = {'I','s','R','e','n','d','e','r','e','d',0};
80 /* For types registered with IFilterMapper */
81 static const WCHAR wszTypes[] = {'T','y','p','e','s',0};
82
83
84 /* registry format for REGFILTER2 */
85 struct REG_RF
86 {
87 DWORD dwVersion;
88 DWORD dwMerit;
89 DWORD dwPins;
90 DWORD dwUnused;
91 };
92
93 struct REG_RFP
94 {
95 BYTE signature[4]; /* e.g. "0pi3" */
96 DWORD dwFlags;
97 DWORD dwInstances;
98 DWORD dwMediaTypes;
99 DWORD dwMediums;
100 DWORD bCategory; /* is there a category clsid? */
101 /* optional: dwOffsetCategoryClsid */
102 };
103
104 struct REG_TYPE
105 {
106 BYTE signature[4]; /* e.g. "0ty3" */
107 DWORD dwUnused;
108 DWORD dwOffsetMajor;
109 DWORD dwOffsetMinor;
110 };
111
112 struct MONIKER_MERIT
113 {
114 IMoniker * pMoniker;
115 DWORD dwMerit;
116 };
117
118 struct Vector
119 {
120 LPBYTE pData;
121 int capacity; /* in bytes */
122 int current; /* pointer to next free byte */
123 };
124
125 /* returns the position it was added at */
126 static int add_data(struct Vector * v, const BYTE * pData, int size)
127 {
128 int index = v->current;
129 if (v->current + size > v->capacity)
130 {
131 LPBYTE pOldData = v->pData;
132 v->capacity = (v->capacity + size) * 2;
133 v->pData = CoTaskMemAlloc(v->capacity);
134 memcpy(v->pData, pOldData, v->current);
135 CoTaskMemFree(pOldData);
136 }
137 memcpy(v->pData + v->current, pData, size);
138 v->current += size;
139 return index;
140 }
141
142 static int find_data(const struct Vector * v, const BYTE * pData, int size)
143 {
144 int index;
145 for (index = 0; index < v->current; index++)
146 if (!memcmp(v->pData + index, pData, size))
147 return index;
148 /* not found */
149 return -1;
150 }
151
152 static void delete_vector(struct Vector * v)
153 {
154 CoTaskMemFree(v->pData);
155 v->current = 0;
156 v->capacity = 0;
157 }
158
159 /*** IUnknown (inner) methods ***/
160
161 static HRESULT WINAPI Inner_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
162 {
163 FilterMapper3Impl *This = impl_from_IUnknown(iface);
164
165 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
166
167 *ppv = NULL;
168 if (IsEqualIID(riid, &IID_IUnknown))
169 *ppv = &This->IUnknown_inner;
170 else if (IsEqualIID(riid, &IID_IFilterMapper2) || IsEqualIID(riid, &IID_IFilterMapper3))
171 *ppv = &This->IFilterMapper3_iface;
172 else if (IsEqualIID(riid, &IID_IFilterMapper))
173 *ppv = &This->IFilterMapper_iface;
174 else if (IsEqualIID(riid, &IID_IAMFilterData))
175 *ppv = &This->IAMFilterData_iface;
176
177 if (*ppv != NULL)
178 {
179 IUnknown_AddRef((IUnknown *)*ppv);
180 return S_OK;
181 }
182
183 FIXME("No interface for %s\n", debugstr_guid(riid));
184 return E_NOINTERFACE;
185 }
186
187 static ULONG WINAPI Inner_AddRef(IUnknown *iface)
188 {
189 FilterMapper3Impl *This = impl_from_IUnknown(iface);
190 ULONG ref = InterlockedIncrement(&This->ref);
191
192 TRACE("(%p)->(): new ref = %d\n", This, ref);
193
194 return ref;
195 }
196
197 static ULONG WINAPI Inner_Release(IUnknown *iface)
198 {
199 FilterMapper3Impl *This = impl_from_IUnknown(iface);
200 ULONG ref = InterlockedDecrement(&This->ref);
201
202 TRACE("(%p)->(): new ref = %d\n", This, ref);
203
204 if (ref == 0)
205 CoTaskMemFree(This);
206
207 return ref;
208 }
209
210 static const IUnknownVtbl IInner_VTable =
211 {
212 Inner_QueryInterface,
213 Inner_AddRef,
214 Inner_Release
215 };
216
217 static HRESULT WINAPI FilterMapper3_QueryInterface(IFilterMapper3 * iface, REFIID riid, LPVOID *ppv)
218 {
219 FilterMapper3Impl *This = impl_from_IFilterMapper3(iface);
220
221 return IUnknown_QueryInterface(This->outer_unk, riid, ppv);
222 }
223
224 static ULONG WINAPI FilterMapper3_AddRef(IFilterMapper3 * iface)
225 {
226 FilterMapper3Impl *This = impl_from_IFilterMapper3(iface);
227
228 return IUnknown_AddRef(This->outer_unk);
229 }
230
231 static ULONG WINAPI FilterMapper3_Release(IFilterMapper3 * iface)
232 {
233 FilterMapper3Impl *This = impl_from_IFilterMapper3(iface);
234
235 return IUnknown_Release(This->outer_unk);
236 }
237
238 /*** IFilterMapper3 methods ***/
239
240 static HRESULT WINAPI FilterMapper3_CreateCategory(
241 IFilterMapper3 * iface,
242 REFCLSID clsidCategory,
243 DWORD dwCategoryMerit,
244 LPCWSTR szDescription)
245 {
246 LPWSTR wClsidAMCat = NULL;
247 LPWSTR wClsidCategory = NULL;
248 WCHAR wszKeyName[ARRAYSIZE(wszClsidSlash)-1 + ARRAYSIZE(wszSlashInstance)-1 + (CHARS_IN_GUID-1) * 2 + 1];
249 HKEY hKey = NULL;
250 LONG lRet;
251 HRESULT hr;
252
253 TRACE("(%s, %x, %s)\n", debugstr_guid(clsidCategory), dwCategoryMerit, debugstr_w(szDescription));
254
255 hr = StringFromCLSID(&CLSID_ActiveMovieCategories, &wClsidAMCat);
256
257 if (SUCCEEDED(hr))
258 {
259 hr = StringFromCLSID(clsidCategory, &wClsidCategory);
260 }
261
262 if (SUCCEEDED(hr))
263 {
264 strcpyW(wszKeyName, wszClsidSlash);
265 strcatW(wszKeyName, wClsidAMCat);
266 strcatW(wszKeyName, wszSlashInstance);
267 strcatW(wszKeyName, wClsidCategory);
268
269 lRet = RegCreateKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
270 hr = HRESULT_FROM_WIN32(lRet);
271 }
272
273 if (SUCCEEDED(hr))
274 {
275 lRet = RegSetValueExW(hKey, wszFriendlyName, 0, REG_SZ, (const BYTE*)szDescription, (strlenW(szDescription) + 1) * sizeof(WCHAR));
276 hr = HRESULT_FROM_WIN32(lRet);
277 }
278
279 if (SUCCEEDED(hr))
280 {
281 lRet = RegSetValueExW(hKey, wszClsidName, 0, REG_SZ, (LPBYTE)wClsidCategory, (strlenW(wClsidCategory) + 1) * sizeof(WCHAR));
282 hr = HRESULT_FROM_WIN32(lRet);
283 }
284
285 if (SUCCEEDED(hr))
286 {
287 lRet = RegSetValueExW(hKey, wszMeritName, 0, REG_DWORD, (LPBYTE)&dwCategoryMerit, sizeof(dwCategoryMerit));
288 hr = HRESULT_FROM_WIN32(lRet);
289 }
290
291 RegCloseKey(hKey);
292 CoTaskMemFree(wClsidCategory);
293 CoTaskMemFree(wClsidAMCat);
294
295 return hr;
296 }
297
298 static HRESULT WINAPI FilterMapper3_UnregisterFilter(
299 IFilterMapper3 * iface,
300 const CLSID *pclsidCategory,
301 const OLECHAR *szInstance,
302 REFCLSID Filter)
303 {
304 WCHAR wszKeyName[MAX_PATH];
305 LPWSTR wClsidCategory = NULL;
306 LPWSTR wFilter = NULL;
307 HRESULT hr;
308
309 TRACE("(%p, %s, %s)\n", pclsidCategory, debugstr_w(szInstance), debugstr_guid(Filter));
310
311 if (!pclsidCategory)
312 pclsidCategory = &CLSID_LegacyAmFilterCategory;
313
314 hr = StringFromCLSID(pclsidCategory, &wClsidCategory);
315
316 if (SUCCEEDED(hr))
317 {
318 strcpyW(wszKeyName, wszClsidSlash);
319 strcatW(wszKeyName, wClsidCategory);
320 strcatW(wszKeyName, wszSlashInstance);
321 if (szInstance)
322 strcatW(wszKeyName, szInstance);
323 else
324 {
325 hr = StringFromCLSID(Filter, &wFilter);
326 if (SUCCEEDED(hr))
327 strcatW(wszKeyName, wFilter);
328 }
329 }
330
331 if (SUCCEEDED(hr))
332 {
333 LONG lRet = RegDeleteKeyW(HKEY_CLASSES_ROOT, wszKeyName);
334 hr = HRESULT_FROM_WIN32(lRet);
335 }
336
337 CoTaskMemFree(wClsidCategory);
338 CoTaskMemFree(wFilter);
339
340 return hr;
341 }
342
343 static HRESULT FM2_WriteFriendlyName(IPropertyBag * pPropBag, LPCWSTR szName)
344 {
345 VARIANT var;
346 HRESULT ret;
347 BSTR value;
348
349 V_VT(&var) = VT_BSTR;
350 V_UNION(&var, bstrVal) = value = SysAllocString(szName);
351
352 ret = IPropertyBag_Write(pPropBag, wszFriendlyName, &var);
353 SysFreeString(value);
354
355 return ret;
356 }
357
358 static HRESULT FM2_WriteClsid(IPropertyBag * pPropBag, REFCLSID clsid)
359 {
360 LPWSTR wszClsid = NULL;
361 VARIANT var;
362 HRESULT hr;
363
364 hr = StringFromCLSID(clsid, &wszClsid);
365
366 if (SUCCEEDED(hr))
367 {
368 V_VT(&var) = VT_BSTR;
369 V_UNION(&var, bstrVal) = wszClsid;
370 hr = IPropertyBag_Write(pPropBag, wszClsidName, &var);
371 }
372 CoTaskMemFree(wszClsid);
373 return hr;
374 }
375
376 static HRESULT FM2_WriteFilterData(const REGFILTER2 * prf2, BYTE **ppData, ULONG *pcbData)
377 {
378 int size = sizeof(struct REG_RF);
379 unsigned int i;
380 struct Vector mainStore = {NULL, 0, 0};
381 struct Vector clsidStore = {NULL, 0, 0};
382 struct REG_RF rrf;
383 HRESULT hr = S_OK;
384
385 rrf.dwVersion = prf2->dwVersion;
386 rrf.dwMerit = prf2->dwMerit;
387 rrf.dwPins = prf2->u.s2.cPins2;
388 rrf.dwUnused = 0;
389
390 add_data(&mainStore, (LPBYTE)&rrf, sizeof(rrf));
391
392 for (i = 0; i < prf2->u.s2.cPins2; i++)
393 {
394 size += sizeof(struct REG_RFP);
395 if (prf2->u.s2.rgPins2[i].clsPinCategory)
396 size += sizeof(DWORD);
397 size += prf2->u.s2.rgPins2[i].nMediaTypes * sizeof(struct REG_TYPE);
398 size += prf2->u.s2.rgPins2[i].nMediums * sizeof(DWORD);
399 }
400
401 for (i = 0; i < prf2->u.s2.cPins2; i++)
402 {
403 struct REG_RFP rrfp;
404 REGFILTERPINS2 rgPin2 = prf2->u.s2.rgPins2[i];
405 unsigned int j;
406
407 rrfp.signature[0] = '0';
408 rrfp.signature[1] = 'p';
409 rrfp.signature[2] = 'i';
410 rrfp.signature[3] = '3';
411 rrfp.signature[0] += i;
412 rrfp.dwFlags = rgPin2.dwFlags;
413 rrfp.dwInstances = rgPin2.cInstances;
414 rrfp.dwMediaTypes = rgPin2.nMediaTypes;
415 rrfp.dwMediums = rgPin2.nMediums;
416 rrfp.bCategory = rgPin2.clsPinCategory ? 1 : 0;
417
418 add_data(&mainStore, (LPBYTE)&rrfp, sizeof(rrfp));
419 if (rrfp.bCategory)
420 {
421 DWORD index = find_data(&clsidStore, (const BYTE*)rgPin2.clsPinCategory, sizeof(CLSID));
422 if (index == -1)
423 index = add_data(&clsidStore, (const BYTE*)rgPin2.clsPinCategory, sizeof(CLSID));
424 index += size;
425
426 add_data(&mainStore, (LPBYTE)&index, sizeof(index));
427 }
428
429 for (j = 0; j < rgPin2.nMediaTypes; j++)
430 {
431 struct REG_TYPE rt;
432 const CLSID * clsMinorType = rgPin2.lpMediaType[j].clsMinorType ? rgPin2.lpMediaType[j].clsMinorType : &MEDIASUBTYPE_NULL;
433 rt.signature[0] = '0';
434 rt.signature[1] = 't';
435 rt.signature[2] = 'y';
436 rt.signature[3] = '3';
437 rt.signature[0] += j;
438 rt.dwUnused = 0;
439 rt.dwOffsetMajor = find_data(&clsidStore, (const BYTE*)rgPin2.lpMediaType[j].clsMajorType, sizeof(CLSID));
440 if (rt.dwOffsetMajor == -1)
441 rt.dwOffsetMajor = add_data(&clsidStore, (const BYTE*)rgPin2.lpMediaType[j].clsMajorType, sizeof(CLSID));
442 rt.dwOffsetMajor += size;
443 rt.dwOffsetMinor = find_data(&clsidStore, (const BYTE*)clsMinorType, sizeof(CLSID));
444 if (rt.dwOffsetMinor == -1)
445 rt.dwOffsetMinor = add_data(&clsidStore, (const BYTE*)clsMinorType, sizeof(CLSID));
446 rt.dwOffsetMinor += size;
447
448 add_data(&mainStore, (LPBYTE)&rt, sizeof(rt));
449 }
450
451 for (j = 0; j < rgPin2.nMediums; j++)
452 {
453 DWORD index = find_data(&clsidStore, (const BYTE*)(rgPin2.lpMedium + j), sizeof(REGPINMEDIUM));
454 if (index == -1)
455 index = add_data(&clsidStore, (const BYTE*)(rgPin2.lpMedium + j), sizeof(REGPINMEDIUM));
456 index += size;
457
458 add_data(&mainStore, (LPBYTE)&index, sizeof(index));
459 }
460 }
461
462 if (SUCCEEDED(hr))
463 {
464 *pcbData = mainStore.current + clsidStore.current;
465 *ppData = CoTaskMemAlloc(*pcbData);
466 if (!*ppData)
467 hr = E_OUTOFMEMORY;
468 }
469
470 if (SUCCEEDED(hr))
471 {
472 memcpy(*ppData, mainStore.pData, mainStore.current);
473 memcpy((*ppData) + mainStore.current, clsidStore.pData, clsidStore.current);
474 }
475
476 delete_vector(&mainStore);
477 delete_vector(&clsidStore);
478 return hr;
479 }
480
481 static HRESULT FM2_ReadFilterData(BYTE *pData, REGFILTER2 * prf2)
482 {
483 HRESULT hr = S_OK;
484 struct REG_RF * prrf;
485 LPBYTE pCurrent;
486 DWORD i;
487 REGFILTERPINS2 * rgPins2;
488
489 prrf = (struct REG_RF *)pData;
490 pCurrent = pData;
491
492 if (prrf->dwVersion != 2)
493 {
494 FIXME("Filter registry version %d not supported\n", prrf->dwVersion);
495 ZeroMemory(prf2, sizeof(*prf2));
496 hr = E_FAIL;
497 }
498
499 if (SUCCEEDED(hr))
500 {
501 TRACE("version = %d, merit = %x, #pins = %d, unused = %x\n",
502 prrf->dwVersion, prrf->dwMerit, prrf->dwPins, prrf->dwUnused);
503
504 prf2->dwVersion = prrf->dwVersion;
505 prf2->dwMerit = prrf->dwMerit;
506 prf2->u.s2.cPins2 = prrf->dwPins;
507 rgPins2 = CoTaskMemAlloc(prrf->dwPins * sizeof(*rgPins2));
508 prf2->u.s2.rgPins2 = rgPins2;
509 pCurrent += sizeof(struct REG_RF);
510
511 for (i = 0; i < prrf->dwPins; i++)
512 {
513 struct REG_RFP * prrfp = (struct REG_RFP *)pCurrent;
514 REGPINTYPES * lpMediaType;
515 REGPINMEDIUM * lpMedium;
516 UINT j;
517
518 /* FIXME: check signature */
519
520 TRACE("\tsignature = %s\n", debugstr_an((const char*)prrfp->signature, 4));
521
522 TRACE("\tpin[%d]: flags = %x, instances = %d, media types = %d, mediums = %d\n",
523 i, prrfp->dwFlags, prrfp->dwInstances, prrfp->dwMediaTypes, prrfp->dwMediums);
524
525 rgPins2[i].dwFlags = prrfp->dwFlags;
526 rgPins2[i].cInstances = prrfp->dwInstances;
527 rgPins2[i].nMediaTypes = prrfp->dwMediaTypes;
528 rgPins2[i].nMediums = prrfp->dwMediums;
529 pCurrent += sizeof(struct REG_RFP);
530 if (prrfp->bCategory)
531 {
532 CLSID * clsCat = CoTaskMemAlloc(sizeof(CLSID));
533 memcpy(clsCat, pData + *(DWORD*)(pCurrent), sizeof(CLSID));
534 pCurrent += sizeof(DWORD);
535 rgPins2[i].clsPinCategory = clsCat;
536 }
537 else
538 rgPins2[i].clsPinCategory = NULL;
539
540 if (rgPins2[i].nMediaTypes > 0)
541 lpMediaType = CoTaskMemAlloc(rgPins2[i].nMediaTypes * sizeof(*lpMediaType));
542 else
543 lpMediaType = NULL;
544
545 rgPins2[i].lpMediaType = lpMediaType;
546
547 for (j = 0; j < rgPins2[i].nMediaTypes; j++)
548 {
549 struct REG_TYPE * prt = (struct REG_TYPE *)pCurrent;
550 CLSID * clsMajor = CoTaskMemAlloc(sizeof(CLSID));
551 CLSID * clsMinor = CoTaskMemAlloc(sizeof(CLSID));
552
553 /* FIXME: check signature */
554 TRACE("\t\tsignature = %s\n", debugstr_an((const char*)prt->signature, 4));
555
556 memcpy(clsMajor, pData + prt->dwOffsetMajor, sizeof(CLSID));
557 memcpy(clsMinor, pData + prt->dwOffsetMinor, sizeof(CLSID));
558
559 lpMediaType[j].clsMajorType = clsMajor;
560 lpMediaType[j].clsMinorType = clsMinor;
561
562 pCurrent += sizeof(*prt);
563 }
564
565 if (rgPins2[i].nMediums > 0)
566 lpMedium = CoTaskMemAlloc(rgPins2[i].nMediums * sizeof(*lpMedium));
567 else
568 lpMedium = NULL;
569
570 rgPins2[i].lpMedium = lpMedium;
571
572 for (j = 0; j < rgPins2[i].nMediums; j++)
573 {
574 DWORD dwOffset = *(DWORD *)pCurrent;
575
576 memcpy(lpMedium + j, pData + dwOffset, sizeof(REGPINMEDIUM));
577
578 pCurrent += sizeof(dwOffset);
579 }
580 }
581
582 }
583
584 return hr;
585 }
586
587 static void FM2_DeleteRegFilter(REGFILTER2 * prf2)
588 {
589 UINT i;
590 for (i = 0; i < prf2->u.s2.cPins2; i++)
591 {
592 UINT j;
593 if (prf2->u.s2.rgPins2[i].clsPinCategory)
594 CoTaskMemFree((LPVOID)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_UNION(&var, parray) = 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_UNION(&var, ulVal) >= 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_UNION(&temp, bstrVal)));
944 VariantClear(&temp);
945 }
946
947 hrSub = IPropertyBag_Read(pPropBagCat, wszClsidName, &var, NULL);
948
949 if (SUCCEEDED(hrSub))
950 hrSub = CLSIDFromString(V_UNION(&var, bstrVal), &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_UNION(&temp, bstrVal)));
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_UNION(&var, parray), (LPVOID*)&pData);
988
989 if (SUCCEEDED(hrSub))
990 hrSub = FM2_ReadFilterData(pData, &rf2);
991
992 if (pData)
993 SafeArrayUnaccessData(V_UNION(&var, parray));
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) stub!\n",
1143 iface,This,
1144 ppEnum,
1145 dwMerit,
1146 bInputNeeded ? "true" : "false",
1147 debugstr_guid(&clsInMaj),
1148 debugstr_guid(&clsInSub),
1149 bRender ? "true" : "false",
1150 bOutputNeeded ? "true" : "false",
1151 debugstr_guid(&clsOutMaj),
1152 debugstr_guid(&clsOutSub));
1153
1154 InputType[0] = clsInMaj;
1155 InputType[1] = clsInSub;
1156 OutputType[0] = clsOutMaj;
1157 OutputType[1] = clsOutSub;
1158
1159 *ppEnum = NULL;
1160
1161 hr = IFilterMapper3_EnumMatchingFilters(&This->IFilterMapper3_iface, &ppEnumMoniker, 0, TRUE,
1162 dwMerit, bInputNeeded, 1, InputType, NULL, &GUID_NULL, bRender, bOutputNeeded, 1,
1163 OutputType, NULL, &GUID_NULL);
1164
1165 if (FAILED(hr))
1166 return hr;
1167
1168 while(IEnumMoniker_Next(ppEnumMoniker, 1, &IMon, &nb) == S_OK)
1169 {
1170 IMoniker_Release(IMon);
1171 nb_mon++;
1172 }
1173
1174 if (!nb_mon)
1175 {
1176 IEnumMoniker_Release(ppEnumMoniker);
1177 return IEnumRegFiltersImpl_Construct(NULL, 0, ppEnum);
1178 }
1179
1180 regfilters = CoTaskMemAlloc(nb_mon * sizeof(REGFILTER));
1181 if (!regfilters)
1182 {
1183 IEnumMoniker_Release(ppEnumMoniker);
1184 return E_OUTOFMEMORY;
1185 }
1186 ZeroMemory(regfilters, nb_mon * sizeof(REGFILTER)); /* will prevent bad free of Name in case of error. */
1187
1188 IEnumMoniker_Reset(ppEnumMoniker);
1189 while(IEnumMoniker_Next(ppEnumMoniker, 1, &IMon, &nb) == S_OK)
1190 {
1191 IPropertyBag * pPropBagCat = NULL;
1192 VARIANT var;
1193 HRESULT hrSub;
1194 GUID clsid;
1195 int len;
1196
1197 VariantInit(&var);
1198
1199 hrSub = IMoniker_BindToStorage(IMon, NULL, NULL, &IID_IPropertyBag, (LPVOID*)&pPropBagCat);
1200
1201 if (SUCCEEDED(hrSub))
1202 hrSub = IPropertyBag_Read(pPropBagCat, wszClsidName, &var, NULL);
1203
1204 if (SUCCEEDED(hrSub))
1205 hrSub = CLSIDFromString(V_UNION(&var, bstrVal), &clsid);
1206
1207 VariantClear(&var);
1208
1209 if (SUCCEEDED(hrSub))
1210 hrSub = IPropertyBag_Read(pPropBagCat, wszFriendlyName, &var, NULL);
1211
1212 if (SUCCEEDED(hrSub))
1213 {
1214 len = (strlenW(V_UNION(&var, bstrVal))+1) * sizeof(WCHAR);
1215 if (!(regfilters[idx].Name = CoTaskMemAlloc(len*2)))
1216 hr = E_OUTOFMEMORY;
1217 }
1218
1219 if (SUCCEEDED(hrSub) && regfilters[idx].Name)
1220 {
1221 memcpy(regfilters[idx].Name, V_UNION(&var, bstrVal), len);
1222 regfilters[idx].Clsid = clsid;
1223 idx++;
1224 }
1225
1226 if (pPropBagCat)
1227 IPropertyBag_Release(pPropBagCat);
1228 IMoniker_Release(IMon);
1229 VariantClear(&var);
1230 }
1231
1232 if (SUCCEEDED(hr))
1233 {
1234 hr = IEnumRegFiltersImpl_Construct(regfilters, nb_mon, ppEnum);
1235 }
1236
1237 for (idx = 0; idx < nb_mon; idx++)
1238 CoTaskMemFree(regfilters[idx].Name);
1239 CoTaskMemFree(regfilters);
1240 IEnumMoniker_Release(ppEnumMoniker);
1241
1242 return hr;
1243 }
1244
1245
1246 static HRESULT WINAPI FilterMapper_RegisterFilter(IFilterMapper * iface, CLSID clsid, LPCWSTR szName, DWORD dwMerit)
1247 {
1248 HRESULT hr;
1249 LPWSTR wszClsid = NULL;
1250 HKEY hKey;
1251 LONG lRet;
1252 WCHAR wszKeyName[ARRAYSIZE(wszFilterSlash)-1 + (CHARS_IN_GUID-1) + 1];
1253
1254 TRACE("(%p)->(%s, %s, %x)\n", iface, debugstr_guid(&clsid), debugstr_w(szName), dwMerit);
1255
1256 hr = StringFromCLSID(&clsid, &wszClsid);
1257
1258 if (SUCCEEDED(hr))
1259 {
1260 strcpyW(wszKeyName, wszFilterSlash);
1261 strcatW(wszKeyName, wszClsid);
1262
1263 lRet = RegCreateKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
1264 hr = HRESULT_FROM_WIN32(lRet);
1265 }
1266
1267 if (SUCCEEDED(hr))
1268 {
1269 lRet = RegSetValueExW(hKey, NULL, 0, REG_SZ, (const BYTE*)szName, (strlenW(szName) + 1) * sizeof(WCHAR));
1270 hr = HRESULT_FROM_WIN32(lRet);
1271 RegCloseKey(hKey);
1272 }
1273
1274 if (SUCCEEDED(hr))
1275 {
1276 strcpyW(wszKeyName, wszClsidSlash);
1277 strcatW(wszKeyName, wszClsid);
1278
1279 lRet = RegCreateKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
1280 hr = HRESULT_FROM_WIN32(lRet);
1281 }
1282
1283 if (SUCCEEDED(hr))
1284 {
1285 lRet = RegSetValueExW(hKey, wszMeritName, 0, REG_DWORD, (LPBYTE)&dwMerit, sizeof(dwMerit));
1286 hr = HRESULT_FROM_WIN32(lRet);
1287 RegCloseKey(hKey);
1288 }
1289
1290 CoTaskMemFree(wszClsid);
1291
1292 return hr;
1293 }
1294
1295 static HRESULT WINAPI FilterMapper_RegisterFilterInstance(IFilterMapper * iface, CLSID clsid, LPCWSTR szName, CLSID *MRId)
1296 {
1297 TRACE("(%p)->(%s, %s, %p)\n", iface, debugstr_guid(&clsid), debugstr_w(szName), MRId);
1298
1299 /* Not implemented in Windows (tested on Win2k) */
1300
1301 return E_NOTIMPL;
1302 }
1303
1304 static HRESULT WINAPI FilterMapper_RegisterPin(
1305 IFilterMapper * iface,
1306 CLSID Filter,
1307 LPCWSTR szName,
1308 BOOL bRendered,
1309 BOOL bOutput,
1310 BOOL bZero,
1311 BOOL bMany,
1312 CLSID ConnectsToFilter,
1313 LPCWSTR ConnectsToPin)
1314 {
1315 HRESULT hr;
1316 LONG lRet;
1317 LPWSTR wszClsid = NULL;
1318 HKEY hKey = NULL;
1319 HKEY hPinsKey = NULL;
1320 WCHAR * wszPinsKeyName;
1321 WCHAR wszKeyName[ARRAYSIZE(wszClsidSlash)-1 + (CHARS_IN_GUID-1) + 1];
1322
1323 TRACE("(%p)->(%s, %s, %d, %d, %d, %d, %s, %s)\n", iface, debugstr_guid(&Filter), debugstr_w(szName), bRendered,
1324 bOutput, bZero, bMany, debugstr_guid(&ConnectsToFilter), debugstr_w(ConnectsToPin));
1325
1326 hr = StringFromCLSID(&Filter, &wszClsid);
1327
1328 if (SUCCEEDED(hr))
1329 {
1330 strcpyW(wszKeyName, wszClsidSlash);
1331 strcatW(wszKeyName, wszClsid);
1332
1333 lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, KEY_WRITE, &hKey);
1334 hr = HRESULT_FROM_WIN32(lRet);
1335 }
1336
1337 if (SUCCEEDED(hr))
1338 {
1339 wszPinsKeyName = CoTaskMemAlloc((strlenW(wszPins) + 1 + strlenW(szName) + 1) * 2);
1340 if (!wszPinsKeyName)
1341 hr = E_OUTOFMEMORY;
1342 }
1343
1344 if (SUCCEEDED(hr))
1345 {
1346 strcpyW(wszPinsKeyName, wszPins);
1347 strcatW(wszPinsKeyName, wszSlash);
1348 strcatW(wszPinsKeyName, szName);
1349
1350 lRet = RegCreateKeyExW(hKey, wszPinsKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hPinsKey, NULL);
1351 hr = HRESULT_FROM_WIN32(lRet);
1352 CoTaskMemFree(wszPinsKeyName);
1353 }
1354
1355 if (SUCCEEDED(hr))
1356 {
1357 lRet = RegSetValueExW(hPinsKey, wszAllowedMany, 0, REG_DWORD, (LPBYTE)&bMany, sizeof(bMany));
1358 hr = HRESULT_FROM_WIN32(lRet);
1359 }
1360
1361 if (SUCCEEDED(hr))
1362 {
1363 lRet = RegSetValueExW(hPinsKey, wszAllowedZero, 0, REG_DWORD, (LPBYTE)&bZero, sizeof(bZero));
1364 hr = HRESULT_FROM_WIN32(lRet);
1365 }
1366
1367 if (SUCCEEDED(hr))
1368 {
1369 lRet = RegSetValueExW(hPinsKey, wszDirection, 0, REG_DWORD, (LPBYTE)&bOutput, sizeof(bOutput));
1370 hr = HRESULT_FROM_WIN32(lRet);
1371 }
1372
1373 if (SUCCEEDED(hr))
1374 {
1375 lRet = RegSetValueExW(hPinsKey, wszIsRendered, 0, REG_DWORD, (LPBYTE)&bRendered, sizeof(bRendered));
1376 hr = HRESULT_FROM_WIN32(lRet);
1377 }
1378
1379 if (SUCCEEDED(hr))
1380 {
1381 HKEY hkeyDummy = NULL;
1382
1383 lRet = RegCreateKeyExW(hPinsKey, wszTypes, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkeyDummy, NULL);
1384 hr = HRESULT_FROM_WIN32(lRet);
1385
1386 if (hkeyDummy) RegCloseKey(hkeyDummy);
1387 }
1388
1389 CoTaskMemFree(wszClsid);
1390 if (hKey)
1391 RegCloseKey(hKey);
1392 if (hPinsKey)
1393 RegCloseKey(hPinsKey);
1394
1395 return hr;
1396 }
1397
1398
1399 static HRESULT WINAPI FilterMapper_RegisterPinType(
1400 IFilterMapper * iface,
1401 CLSID clsFilter,
1402 LPCWSTR szName,
1403 CLSID clsMajorType,
1404 CLSID clsSubType)
1405 {
1406 HRESULT hr;
1407 LONG lRet;
1408 LPWSTR wszClsid = NULL;
1409 LPWSTR wszClsidMajorType = NULL;
1410 LPWSTR wszClsidSubType = NULL;
1411 HKEY hKey = NULL;
1412 WCHAR * wszTypesKey;
1413 WCHAR wszKeyName[MAX_PATH];
1414
1415 TRACE("(%p)->(%s, %s, %s, %s)\n", iface, debugstr_guid(&clsFilter), debugstr_w(szName),
1416 debugstr_guid(&clsMajorType), debugstr_guid(&clsSubType));
1417
1418 hr = StringFromCLSID(&clsFilter, &wszClsid);
1419
1420 if (SUCCEEDED(hr))
1421 {
1422 hr = StringFromCLSID(&clsMajorType, &wszClsidMajorType);
1423 }
1424
1425 if (SUCCEEDED(hr))
1426 {
1427 hr = StringFromCLSID(&clsSubType, &wszClsidSubType);
1428 }
1429
1430 if (SUCCEEDED(hr))
1431 {
1432 wszTypesKey = CoTaskMemAlloc((strlenW(wszClsidSlash) + strlenW(wszClsid) + strlenW(wszPins) +
1433 strlenW(szName) + strlenW(wszTypes) + 3 + 1) * 2);
1434 if (!wszTypesKey)
1435 hr = E_OUTOFMEMORY;
1436 }
1437
1438 if (SUCCEEDED(hr))
1439 {
1440 strcpyW(wszTypesKey, wszClsidSlash);
1441 strcatW(wszTypesKey, wszClsid);
1442 strcatW(wszTypesKey, wszSlash);
1443 strcatW(wszTypesKey, wszPins);
1444 strcatW(wszTypesKey, wszSlash);
1445 strcatW(wszTypesKey, szName);
1446 strcatW(wszTypesKey, wszSlash);
1447 strcatW(wszTypesKey, wszTypes);
1448
1449 lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszTypesKey, 0, KEY_WRITE, &hKey);
1450 hr = HRESULT_FROM_WIN32(lRet);
1451 CoTaskMemFree(wszTypesKey);
1452 }
1453
1454 if (SUCCEEDED(hr))
1455 {
1456 HKEY hkeyDummy = NULL;
1457
1458 strcpyW(wszKeyName, wszClsidMajorType);
1459 strcatW(wszKeyName, wszSlash);
1460 strcatW(wszKeyName, wszClsidSubType);
1461
1462 lRet = RegCreateKeyExW(hKey, wszKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkeyDummy, NULL);
1463 hr = HRESULT_FROM_WIN32(lRet);
1464 RegCloseKey(hKey);
1465
1466 if (hkeyDummy) RegCloseKey(hkeyDummy);
1467 }
1468
1469 CoTaskMemFree(wszClsid);
1470 CoTaskMemFree(wszClsidMajorType);
1471 CoTaskMemFree(wszClsidSubType);
1472
1473 return hr;
1474 }
1475
1476 static HRESULT WINAPI FilterMapper_UnregisterFilter(IFilterMapper * iface, CLSID Filter)
1477 {
1478 HRESULT hr;
1479 LONG lRet;
1480 LPWSTR wszClsid = NULL;
1481 HKEY hKey;
1482 WCHAR wszKeyName[ARRAYSIZE(wszClsidSlash)-1 + (CHARS_IN_GUID-1) + 1];
1483
1484 TRACE("(%p)->(%s)\n", iface, debugstr_guid(&Filter));
1485
1486 hr = StringFromCLSID(&Filter, &wszClsid);
1487
1488 if (SUCCEEDED(hr))
1489 {
1490 lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszFilter, 0, KEY_WRITE, &hKey);
1491 hr = HRESULT_FROM_WIN32(lRet);
1492 }
1493
1494 if (SUCCEEDED(hr))
1495 {
1496 lRet = RegDeleteKeyW(hKey, wszClsid);
1497 hr = HRESULT_FROM_WIN32(lRet);
1498 RegCloseKey(hKey);
1499 }
1500
1501 if (SUCCEEDED(hr))
1502 {
1503 strcpyW(wszKeyName, wszClsidSlash);
1504 strcatW(wszKeyName, wszClsid);
1505
1506 lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, KEY_WRITE, &hKey);
1507 hr = HRESULT_FROM_WIN32(lRet);
1508 }
1509
1510 if (SUCCEEDED(hr))
1511 {
1512 lRet = RegDeleteValueW(hKey, wszMeritName);
1513 if (lRet != ERROR_SUCCESS)
1514 hr = HRESULT_FROM_WIN32(lRet);
1515
1516 lRet = RegDeleteTreeW(hKey, wszPins);
1517 if (lRet != ERROR_SUCCESS)
1518 hr = HRESULT_FROM_WIN32(lRet);
1519
1520 RegCloseKey(hKey);
1521 }
1522
1523 CoTaskMemFree(wszClsid);
1524
1525 return hr;
1526 }
1527
1528 static HRESULT WINAPI FilterMapper_UnregisterFilterInstance(IFilterMapper * iface, CLSID MRId)
1529 {
1530 TRACE("(%p)->(%s)\n", iface, debugstr_guid(&MRId));
1531
1532 /* Not implemented in Windows (tested on Win2k) */
1533
1534 return E_NOTIMPL;
1535 }
1536
1537 static HRESULT WINAPI FilterMapper_UnregisterPin(IFilterMapper * iface, CLSID Filter, LPCWSTR Name)
1538 {
1539 HRESULT hr;
1540 LONG lRet;
1541 LPWSTR wszClsid = NULL;
1542 HKEY hKey = NULL;
1543 WCHAR * wszPinNameKey;
1544 WCHAR wszKeyName[ARRAYSIZE(wszClsidSlash)-1 + (CHARS_IN_GUID-1) + 1];
1545
1546 TRACE("(%p)->(%s, %s)\n", iface, debugstr_guid(&Filter), debugstr_w(Name));
1547
1548 if (!Name)
1549 return E_INVALIDARG;
1550
1551 hr = StringFromCLSID(&Filter, &wszClsid);
1552
1553 if (SUCCEEDED(hr))
1554 {
1555 strcpyW(wszKeyName, wszClsidSlash);
1556 strcatW(wszKeyName, wszClsid);
1557
1558 lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, KEY_WRITE, &hKey);
1559 hr = HRESULT_FROM_WIN32(lRet);
1560 }
1561
1562 if (SUCCEEDED(hr))
1563 {
1564 wszPinNameKey = CoTaskMemAlloc((strlenW(wszPins) + 1 + strlenW(Name) + 1) * 2);
1565 if (!wszPinNameKey)
1566 hr = E_OUTOFMEMORY;
1567 }
1568
1569 if (SUCCEEDED(hr))
1570 {
1571 strcpyW(wszPinNameKey, wszPins);
1572 strcatW(wszPinNameKey, wszSlash);
1573 strcatW(wszPinNameKey, Name);
1574
1575 lRet = RegDeleteTreeW(hKey, wszPinNameKey);
1576 hr = HRESULT_FROM_WIN32(lRet);
1577 CoTaskMemFree(wszPinNameKey);
1578 }
1579
1580 CoTaskMemFree(wszClsid);
1581 if (hKey)
1582 RegCloseKey(hKey);
1583
1584 return hr;
1585 }
1586
1587 static const IFilterMapperVtbl fmvtbl =
1588 {
1589
1590 FilterMapper_QueryInterface,
1591 FilterMapper_AddRef,
1592 FilterMapper_Release,
1593
1594 FilterMapper_RegisterFilter,
1595 FilterMapper_RegisterFilterInstance,
1596 FilterMapper_RegisterPin,
1597 FilterMapper_RegisterPinType,
1598 FilterMapper_UnregisterFilter,
1599 FilterMapper_UnregisterFilterInstance,
1600 FilterMapper_UnregisterPin,
1601 FilterMapper_EnumMatchingFilters
1602 };
1603
1604
1605 /*** IUnknown methods ***/
1606 static HRESULT WINAPI AMFilterData_QueryInterface(IAMFilterData * iface, REFIID riid, LPVOID *ppv)
1607 {
1608 FilterMapper3Impl *This = impl_from_IAMFilterData(iface);
1609
1610 return IUnknown_QueryInterface(This->outer_unk, riid, ppv);
1611 }
1612
1613 static ULONG WINAPI AMFilterData_AddRef(IAMFilterData * iface)
1614 {
1615 FilterMapper3Impl *This = impl_from_IAMFilterData(iface);
1616
1617 return IUnknown_AddRef(This->outer_unk);
1618 }
1619
1620 static ULONG WINAPI AMFilterData_Release(IAMFilterData * iface)
1621 {
1622 FilterMapper3Impl *This = impl_from_IAMFilterData(iface);
1623
1624 return IUnknown_Release(This->outer_unk);
1625 }
1626
1627 /*** IAMFilterData methods ***/
1628 static HRESULT WINAPI AMFilterData_ParseFilterData(IAMFilterData* iface,
1629 BYTE *pData, ULONG cb,
1630 BYTE **ppRegFilter2)
1631 {
1632 FilterMapper3Impl *This = impl_from_IAMFilterData(iface);
1633 HRESULT hr = S_OK;
1634 static REGFILTER2 *prf2;
1635
1636 TRACE("(%p/%p)->(%p, %d, %p)\n", This, iface, pData, cb, ppRegFilter2);
1637
1638 prf2 = CoTaskMemAlloc(sizeof(*prf2));
1639 if (!prf2)
1640 return E_OUTOFMEMORY;
1641 *ppRegFilter2 = (BYTE *)&prf2;
1642
1643 hr = FM2_ReadFilterData(pData, prf2);
1644 if (FAILED(hr))
1645 {
1646 CoTaskMemFree(prf2);
1647 *ppRegFilter2 = NULL;
1648 }
1649
1650 return hr;
1651 }
1652
1653 static HRESULT WINAPI AMFilterData_CreateFilterData(IAMFilterData* iface,
1654 REGFILTER2 *prf2,
1655 BYTE **pRegFilterData,
1656 ULONG *pcb)
1657 {
1658 FilterMapper3Impl *This = impl_from_IAMFilterData(iface);
1659
1660 TRACE("(%p/%p)->(%p, %p, %p)\n", This, iface, prf2, pRegFilterData, pcb);
1661
1662 return FM2_WriteFilterData(prf2, pRegFilterData, pcb);
1663 }
1664
1665 static const IAMFilterDataVtbl AMFilterDataVtbl = {
1666 AMFilterData_QueryInterface,
1667 AMFilterData_AddRef,
1668 AMFilterData_Release,
1669 AMFilterData_ParseFilterData,
1670 AMFilterData_CreateFilterData
1671 };
1672
1673 HRESULT FilterMapper2_create(IUnknown *pUnkOuter, LPVOID *ppObj)
1674 {
1675 FilterMapper3Impl * pFM2impl;
1676
1677 TRACE("(%p, %p)\n", pUnkOuter, ppObj);
1678
1679 pFM2impl = CoTaskMemAlloc(sizeof(*pFM2impl));
1680 if (!pFM2impl)
1681 return E_OUTOFMEMORY;
1682
1683 pFM2impl->IUnknown_inner.lpVtbl = &IInner_VTable;
1684 pFM2impl->IFilterMapper3_iface.lpVtbl = &fm3vtbl;
1685 pFM2impl->IFilterMapper_iface.lpVtbl = &fmvtbl;
1686 pFM2impl->IAMFilterData_iface.lpVtbl = &AMFilterDataVtbl;
1687 pFM2impl->ref = 1;
1688
1689 if (pUnkOuter)
1690 pFM2impl->outer_unk = pUnkOuter;
1691 else
1692 pFM2impl->outer_unk = &pFM2impl->IUnknown_inner;
1693
1694 *ppObj = &pFM2impl->IUnknown_inner;
1695
1696 TRACE("-- created at %p\n", pFM2impl);
1697
1698 return S_OK;
1699 }
1700
1701 HRESULT FilterMapper_create(IUnknown *pUnkOuter, LPVOID *ppObj)
1702 {
1703 FilterMapper3Impl *pFM2impl;
1704 HRESULT hr;
1705
1706 TRACE("(%p, %p)\n", pUnkOuter, ppObj);
1707
1708 hr = FilterMapper2_create(pUnkOuter, (LPVOID*)&pFM2impl);
1709 if (FAILED(hr))
1710 return hr;
1711
1712 *ppObj = &pFM2impl->IFilterMapper_iface;
1713
1714 return hr;
1715 }