bcrypt: Add support for AES encryption on macOS.
[wine.git] / dlls / quartz / filtermapper.c
blob4e4cb37dcdb588558d0962d01c02fad9ffff8f2b
1 /*
2 * IFilterMapper & IFilterMapper3 Implementations
4 * Copyright 2003 Robert Shearman
5 * Copyright 2004 Christian Costa
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.
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.
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
22 #define NONAMELESSUNION
23 #define NONAMELESSSTRUCT
24 #include <stdarg.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "winreg.h"
30 #include "winerror.h"
32 #include "quartz_private.h"
34 #include "ole2.h"
35 #include "olectl.h"
36 #include "strmif.h"
37 #include "wine/unicode.h"
38 #include "uuids.h"
39 #include "initguid.h"
40 #include "fil_data.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
46 #define ARRAYSIZE(array) (sizeof(array)/sizeof((array)[0]))
48 typedef struct FilterMapper3Impl
50 IUnknown IUnknown_inner;
51 IFilterMapper3 IFilterMapper3_iface;
52 IFilterMapper IFilterMapper_iface;
53 IAMFilterData IAMFilterData_iface;
54 IUnknown *outer_unk;
55 LONG ref;
56 } FilterMapper3Impl;
58 static inline FilterMapper3Impl *impl_from_IFilterMapper3( IFilterMapper3 *iface )
60 return CONTAINING_RECORD(iface, FilterMapper3Impl, IFilterMapper3_iface);
63 static inline FilterMapper3Impl *impl_from_IFilterMapper( IFilterMapper *iface )
65 return CONTAINING_RECORD(iface, FilterMapper3Impl, IFilterMapper_iface);
68 static inline FilterMapper3Impl *impl_from_IAMFilterData( IAMFilterData *iface )
70 return CONTAINING_RECORD(iface, FilterMapper3Impl, IAMFilterData_iface);
73 static inline FilterMapper3Impl *impl_from_IUnknown( IUnknown *iface )
75 return CONTAINING_RECORD(iface, FilterMapper3Impl, IUnknown_inner);
78 static const WCHAR wszClsidSlash[] = {'C','L','S','I','D','\\',0};
79 static const WCHAR wszSlashInstance[] = {'\\','I','n','s','t','a','n','c','e','\\',0};
80 static const WCHAR wszSlash[] = {'\\',0};
82 /* CLSID property in media category Moniker */
83 static const WCHAR wszClsidName[] = {'C','L','S','I','D',0};
84 /* FriendlyName property in media category Moniker */
85 static const WCHAR wszFriendlyName[] = {'F','r','i','e','n','d','l','y','N','a','m','e',0};
86 /* Merit property in media category Moniker (CLSID_ActiveMovieCategories only) */
87 static const WCHAR wszMeritName[] = {'M','e','r','i','t',0};
88 /* FilterData property in media category Moniker (not CLSID_ActiveMovieCategories) */
89 static const WCHAR wszFilterDataName[] = {'F','i','l','t','e','r','D','a','t','a',0};
90 /* For filters registered with IFilterMapper */
91 static const WCHAR wszFilterSlash[] = {'F','i','l','t','e','r','\\',0};
92 static const WCHAR wszFilter[] = {'F','i','l','t','e','r',0};
93 /* For pins registered with IFilterMapper */
94 static const WCHAR wszPins[] = {'P','i','n','s',0};
95 static const WCHAR wszAllowedMany[] = {'A','l','l','o','w','e','d','M','a','n','y',0};
96 static const WCHAR wszAllowedZero[] = {'A','l','l','o','w','e','d','Z','e','r','o',0};
97 static const WCHAR wszDirection[] = {'D','i','r','e','c','t','i','o','n',0};
98 static const WCHAR wszIsRendered[] = {'I','s','R','e','n','d','e','r','e','d',0};
99 /* For types registered with IFilterMapper */
100 static const WCHAR wszTypes[] = {'T','y','p','e','s',0};
103 /* registry format for REGFILTER2 */
104 struct REG_RF
106 DWORD dwVersion;
107 DWORD dwMerit;
108 DWORD dwPins;
109 DWORD dwUnused;
112 struct REG_RFP
114 BYTE signature[4]; /* e.g. "0pi3" */
115 DWORD dwFlags;
116 DWORD dwInstances;
117 DWORD dwMediaTypes;
118 DWORD dwMediums;
119 DWORD bCategory; /* is there a category clsid? */
120 /* optional: dwOffsetCategoryClsid */
123 struct REG_TYPE
125 BYTE signature[4]; /* e.g. "0ty3" */
126 DWORD dwUnused;
127 DWORD dwOffsetMajor;
128 DWORD dwOffsetMinor;
131 struct MONIKER_MERIT
133 IMoniker * pMoniker;
134 DWORD dwMerit;
137 struct Vector
139 LPBYTE pData;
140 int capacity; /* in bytes */
141 int current; /* pointer to next free byte */
144 /* returns the position it was added at */
145 static int add_data(struct Vector * v, const BYTE * pData, int size)
147 int index = v->current;
148 if (v->current + size > v->capacity)
150 LPBYTE pOldData = v->pData;
151 v->capacity = (v->capacity + size) * 2;
152 v->pData = CoTaskMemAlloc(v->capacity);
153 memcpy(v->pData, pOldData, v->current);
154 CoTaskMemFree(pOldData);
156 memcpy(v->pData + v->current, pData, size);
157 v->current += size;
158 return index;
161 static int find_data(const struct Vector * v, const BYTE * pData, int size)
163 int index;
164 for (index = 0; index < v->current; index++)
165 if (!memcmp(v->pData + index, pData, size))
166 return index;
167 /* not found */
168 return -1;
171 static void delete_vector(struct Vector * v)
173 CoTaskMemFree(v->pData);
174 v->current = 0;
175 v->capacity = 0;
178 /*** IUnknown (inner) methods ***/
180 static HRESULT WINAPI Inner_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
182 FilterMapper3Impl *This = impl_from_IUnknown(iface);
184 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
186 *ppv = NULL;
187 if (IsEqualIID(riid, &IID_IUnknown))
188 *ppv = &This->IUnknown_inner;
189 else if (IsEqualIID(riid, &IID_IFilterMapper2) || IsEqualIID(riid, &IID_IFilterMapper3))
190 *ppv = &This->IFilterMapper3_iface;
191 else if (IsEqualIID(riid, &IID_IFilterMapper))
192 *ppv = &This->IFilterMapper_iface;
193 else if (IsEqualIID(riid, &IID_IAMFilterData))
194 *ppv = &This->IAMFilterData_iface;
196 if (*ppv != NULL)
198 IUnknown_AddRef((IUnknown *)*ppv);
199 return S_OK;
202 FIXME("No interface for %s\n", debugstr_guid(riid));
203 return E_NOINTERFACE;
206 static ULONG WINAPI Inner_AddRef(IUnknown *iface)
208 FilterMapper3Impl *This = impl_from_IUnknown(iface);
209 ULONG ref = InterlockedIncrement(&This->ref);
211 TRACE("(%p)->(): new ref = %d\n", This, ref);
213 return ref;
216 static ULONG WINAPI Inner_Release(IUnknown *iface)
218 FilterMapper3Impl *This = impl_from_IUnknown(iface);
219 ULONG ref = InterlockedDecrement(&This->ref);
221 TRACE("(%p)->(): new ref = %d\n", This, ref);
223 if (ref == 0)
224 CoTaskMemFree(This);
226 return ref;
229 static const IUnknownVtbl IInner_VTable =
231 Inner_QueryInterface,
232 Inner_AddRef,
233 Inner_Release
236 static HRESULT WINAPI FilterMapper3_QueryInterface(IFilterMapper3 * iface, REFIID riid, LPVOID *ppv)
238 FilterMapper3Impl *This = impl_from_IFilterMapper3(iface);
240 return IUnknown_QueryInterface(This->outer_unk, riid, ppv);
243 static ULONG WINAPI FilterMapper3_AddRef(IFilterMapper3 * iface)
245 FilterMapper3Impl *This = impl_from_IFilterMapper3(iface);
247 return IUnknown_AddRef(This->outer_unk);
250 static ULONG WINAPI FilterMapper3_Release(IFilterMapper3 * iface)
252 FilterMapper3Impl *This = impl_from_IFilterMapper3(iface);
254 return IUnknown_Release(This->outer_unk);
257 /*** IFilterMapper3 methods ***/
259 static HRESULT WINAPI FilterMapper3_CreateCategory(
260 IFilterMapper3 * iface,
261 REFCLSID clsidCategory,
262 DWORD dwCategoryMerit,
263 LPCWSTR szDescription)
265 LPWSTR wClsidAMCat = NULL;
266 LPWSTR wClsidCategory = NULL;
267 WCHAR wszKeyName[ARRAYSIZE(wszClsidSlash)-1 + ARRAYSIZE(wszSlashInstance)-1 + (CHARS_IN_GUID-1) * 2 + 1];
268 HKEY hKey = NULL;
269 LONG lRet;
270 HRESULT hr;
272 TRACE("(%s, %x, %s)\n", debugstr_guid(clsidCategory), dwCategoryMerit, debugstr_w(szDescription));
274 hr = StringFromCLSID(&CLSID_ActiveMovieCategories, &wClsidAMCat);
276 if (SUCCEEDED(hr))
278 hr = StringFromCLSID(clsidCategory, &wClsidCategory);
281 if (SUCCEEDED(hr))
283 strcpyW(wszKeyName, wszClsidSlash);
284 strcatW(wszKeyName, wClsidAMCat);
285 strcatW(wszKeyName, wszSlashInstance);
286 strcatW(wszKeyName, wClsidCategory);
288 lRet = RegCreateKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
289 hr = HRESULT_FROM_WIN32(lRet);
292 if (SUCCEEDED(hr))
294 lRet = RegSetValueExW(hKey, wszFriendlyName, 0, REG_SZ, (const BYTE*)szDescription, (strlenW(szDescription) + 1) * sizeof(WCHAR));
295 hr = HRESULT_FROM_WIN32(lRet);
298 if (SUCCEEDED(hr))
300 lRet = RegSetValueExW(hKey, wszClsidName, 0, REG_SZ, (LPBYTE)wClsidCategory, (strlenW(wClsidCategory) + 1) * sizeof(WCHAR));
301 hr = HRESULT_FROM_WIN32(lRet);
304 if (SUCCEEDED(hr))
306 lRet = RegSetValueExW(hKey, wszMeritName, 0, REG_DWORD, (LPBYTE)&dwCategoryMerit, sizeof(dwCategoryMerit));
307 hr = HRESULT_FROM_WIN32(lRet);
310 RegCloseKey(hKey);
311 CoTaskMemFree(wClsidCategory);
312 CoTaskMemFree(wClsidAMCat);
314 return hr;
317 static HRESULT WINAPI FilterMapper3_UnregisterFilter(
318 IFilterMapper3 * iface,
319 const CLSID *pclsidCategory,
320 const OLECHAR *szInstance,
321 REFCLSID Filter)
323 WCHAR wszKeyName[MAX_PATH];
324 LPWSTR wClsidCategory = NULL;
325 LPWSTR wFilter = NULL;
326 HRESULT hr;
328 TRACE("(%p, %s, %s)\n", pclsidCategory, debugstr_w(szInstance), debugstr_guid(Filter));
330 if (!pclsidCategory)
331 pclsidCategory = &CLSID_LegacyAmFilterCategory;
333 hr = StringFromCLSID(pclsidCategory, &wClsidCategory);
335 if (SUCCEEDED(hr))
337 strcpyW(wszKeyName, wszClsidSlash);
338 strcatW(wszKeyName, wClsidCategory);
339 strcatW(wszKeyName, wszSlashInstance);
340 if (szInstance)
341 strcatW(wszKeyName, szInstance);
342 else
344 hr = StringFromCLSID(Filter, &wFilter);
345 if (SUCCEEDED(hr))
346 strcatW(wszKeyName, wFilter);
350 if (SUCCEEDED(hr))
352 LONG lRet = RegDeleteKeyW(HKEY_CLASSES_ROOT, wszKeyName);
353 hr = HRESULT_FROM_WIN32(lRet);
356 CoTaskMemFree(wClsidCategory);
357 CoTaskMemFree(wFilter);
359 return hr;
362 static HRESULT FM2_WriteFriendlyName(IPropertyBag * pPropBag, LPCWSTR szName)
364 VARIANT var;
365 HRESULT ret;
366 BSTR value;
368 V_VT(&var) = VT_BSTR;
369 V_BSTR(&var) = value = SysAllocString(szName);
371 ret = IPropertyBag_Write(pPropBag, wszFriendlyName, &var);
372 SysFreeString(value);
374 return ret;
377 static HRESULT FM2_WriteClsid(IPropertyBag * pPropBag, REFCLSID clsid)
379 LPWSTR wszClsid = NULL;
380 VARIANT var;
381 HRESULT hr;
383 hr = StringFromCLSID(clsid, &wszClsid);
385 if (SUCCEEDED(hr))
387 V_VT(&var) = VT_BSTR;
388 V_BSTR(&var) = wszClsid;
389 hr = IPropertyBag_Write(pPropBag, wszClsidName, &var);
391 CoTaskMemFree(wszClsid);
392 return hr;
395 static HRESULT FM2_WriteFilterData(const REGFILTER2 * prf2, BYTE **ppData, ULONG *pcbData)
397 int size = sizeof(struct REG_RF);
398 unsigned int i;
399 struct Vector mainStore = {NULL, 0, 0};
400 struct Vector clsidStore = {NULL, 0, 0};
401 struct REG_RF rrf;
402 HRESULT hr = S_OK;
404 rrf.dwVersion = prf2->dwVersion;
405 rrf.dwMerit = prf2->dwMerit;
406 rrf.dwPins = prf2->u.s2.cPins2;
407 rrf.dwUnused = 0;
409 add_data(&mainStore, (LPBYTE)&rrf, sizeof(rrf));
411 for (i = 0; i < prf2->u.s2.cPins2; i++)
413 size += sizeof(struct REG_RFP);
414 if (prf2->u.s2.rgPins2[i].clsPinCategory)
415 size += sizeof(DWORD);
416 size += prf2->u.s2.rgPins2[i].nMediaTypes * sizeof(struct REG_TYPE);
417 size += prf2->u.s2.rgPins2[i].nMediums * sizeof(DWORD);
420 for (i = 0; i < prf2->u.s2.cPins2; i++)
422 struct REG_RFP rrfp;
423 REGFILTERPINS2 rgPin2 = prf2->u.s2.rgPins2[i];
424 unsigned int j;
426 rrfp.signature[0] = '0';
427 rrfp.signature[1] = 'p';
428 rrfp.signature[2] = 'i';
429 rrfp.signature[3] = '3';
430 rrfp.signature[0] += i;
431 rrfp.dwFlags = rgPin2.dwFlags;
432 rrfp.dwInstances = rgPin2.cInstances;
433 rrfp.dwMediaTypes = rgPin2.nMediaTypes;
434 rrfp.dwMediums = rgPin2.nMediums;
435 rrfp.bCategory = rgPin2.clsPinCategory ? 1 : 0;
437 add_data(&mainStore, (LPBYTE)&rrfp, sizeof(rrfp));
438 if (rrfp.bCategory)
440 DWORD index = find_data(&clsidStore, (const BYTE*)rgPin2.clsPinCategory, sizeof(CLSID));
441 if (index == -1)
442 index = add_data(&clsidStore, (const BYTE*)rgPin2.clsPinCategory, sizeof(CLSID));
443 index += size;
445 add_data(&mainStore, (LPBYTE)&index, sizeof(index));
448 for (j = 0; j < rgPin2.nMediaTypes; j++)
450 struct REG_TYPE rt;
451 const CLSID * clsMinorType = rgPin2.lpMediaType[j].clsMinorType ? rgPin2.lpMediaType[j].clsMinorType : &MEDIASUBTYPE_NULL;
452 rt.signature[0] = '0';
453 rt.signature[1] = 't';
454 rt.signature[2] = 'y';
455 rt.signature[3] = '3';
456 rt.signature[0] += j;
457 rt.dwUnused = 0;
458 rt.dwOffsetMajor = find_data(&clsidStore, (const BYTE*)rgPin2.lpMediaType[j].clsMajorType, sizeof(CLSID));
459 if (rt.dwOffsetMajor == -1)
460 rt.dwOffsetMajor = add_data(&clsidStore, (const BYTE*)rgPin2.lpMediaType[j].clsMajorType, sizeof(CLSID));
461 rt.dwOffsetMajor += size;
462 rt.dwOffsetMinor = find_data(&clsidStore, (const BYTE*)clsMinorType, sizeof(CLSID));
463 if (rt.dwOffsetMinor == -1)
464 rt.dwOffsetMinor = add_data(&clsidStore, (const BYTE*)clsMinorType, sizeof(CLSID));
465 rt.dwOffsetMinor += size;
467 add_data(&mainStore, (LPBYTE)&rt, sizeof(rt));
470 for (j = 0; j < rgPin2.nMediums; j++)
472 DWORD index = find_data(&clsidStore, (const BYTE*)(rgPin2.lpMedium + j), sizeof(REGPINMEDIUM));
473 if (index == -1)
474 index = add_data(&clsidStore, (const BYTE*)(rgPin2.lpMedium + j), sizeof(REGPINMEDIUM));
475 index += size;
477 add_data(&mainStore, (LPBYTE)&index, sizeof(index));
481 if (SUCCEEDED(hr))
483 *pcbData = mainStore.current + clsidStore.current;
484 *ppData = CoTaskMemAlloc(*pcbData);
485 if (!*ppData)
486 hr = E_OUTOFMEMORY;
489 if (SUCCEEDED(hr))
491 memcpy(*ppData, mainStore.pData, mainStore.current);
492 memcpy((*ppData) + mainStore.current, clsidStore.pData, clsidStore.current);
495 delete_vector(&mainStore);
496 delete_vector(&clsidStore);
497 return hr;
500 static HRESULT FM2_ReadFilterData(BYTE *pData, REGFILTER2 * prf2)
502 HRESULT hr = S_OK;
503 struct REG_RF * prrf;
504 LPBYTE pCurrent;
505 DWORD i;
506 REGFILTERPINS2 * rgPins2;
508 prrf = (struct REG_RF *)pData;
509 pCurrent = pData;
511 if (prrf->dwVersion != 2)
513 FIXME("Filter registry version %d not supported\n", prrf->dwVersion);
514 ZeroMemory(prf2, sizeof(*prf2));
515 hr = E_FAIL;
518 if (SUCCEEDED(hr))
520 TRACE("version = %d, merit = %x, #pins = %d, unused = %x\n",
521 prrf->dwVersion, prrf->dwMerit, prrf->dwPins, prrf->dwUnused);
523 prf2->dwVersion = prrf->dwVersion;
524 prf2->dwMerit = prrf->dwMerit;
525 prf2->u.s2.cPins2 = prrf->dwPins;
526 rgPins2 = CoTaskMemAlloc(prrf->dwPins * sizeof(*rgPins2));
527 prf2->u.s2.rgPins2 = rgPins2;
528 pCurrent += sizeof(struct REG_RF);
530 for (i = 0; i < prrf->dwPins; i++)
532 struct REG_RFP * prrfp = (struct REG_RFP *)pCurrent;
533 REGPINTYPES * lpMediaType;
534 REGPINMEDIUM * lpMedium;
535 UINT j;
537 /* FIXME: check signature */
539 TRACE("\tsignature = %s\n", debugstr_an((const char*)prrfp->signature, 4));
541 TRACE("\tpin[%d]: flags = %x, instances = %d, media types = %d, mediums = %d\n",
542 i, prrfp->dwFlags, prrfp->dwInstances, prrfp->dwMediaTypes, prrfp->dwMediums);
544 rgPins2[i].dwFlags = prrfp->dwFlags;
545 rgPins2[i].cInstances = prrfp->dwInstances;
546 rgPins2[i].nMediaTypes = prrfp->dwMediaTypes;
547 rgPins2[i].nMediums = prrfp->dwMediums;
548 pCurrent += sizeof(struct REG_RFP);
549 if (prrfp->bCategory)
551 CLSID * clsCat = CoTaskMemAlloc(sizeof(CLSID));
552 memcpy(clsCat, pData + *(DWORD*)(pCurrent), sizeof(CLSID));
553 pCurrent += sizeof(DWORD);
554 rgPins2[i].clsPinCategory = clsCat;
556 else
557 rgPins2[i].clsPinCategory = NULL;
559 if (rgPins2[i].nMediaTypes > 0)
560 lpMediaType = CoTaskMemAlloc(rgPins2[i].nMediaTypes * sizeof(*lpMediaType));
561 else
562 lpMediaType = NULL;
564 rgPins2[i].lpMediaType = lpMediaType;
566 for (j = 0; j < rgPins2[i].nMediaTypes; j++)
568 struct REG_TYPE * prt = (struct REG_TYPE *)pCurrent;
569 CLSID * clsMajor = CoTaskMemAlloc(sizeof(CLSID));
570 CLSID * clsMinor = CoTaskMemAlloc(sizeof(CLSID));
572 /* FIXME: check signature */
573 TRACE("\t\tsignature = %s\n", debugstr_an((const char*)prt->signature, 4));
575 memcpy(clsMajor, pData + prt->dwOffsetMajor, sizeof(CLSID));
576 memcpy(clsMinor, pData + prt->dwOffsetMinor, sizeof(CLSID));
578 lpMediaType[j].clsMajorType = clsMajor;
579 lpMediaType[j].clsMinorType = clsMinor;
581 pCurrent += sizeof(*prt);
584 if (rgPins2[i].nMediums > 0)
585 lpMedium = CoTaskMemAlloc(rgPins2[i].nMediums * sizeof(*lpMedium));
586 else
587 lpMedium = NULL;
589 rgPins2[i].lpMedium = lpMedium;
591 for (j = 0; j < rgPins2[i].nMediums; j++)
593 DWORD dwOffset = *(DWORD *)pCurrent;
595 memcpy(lpMedium + j, pData + dwOffset, sizeof(REGPINMEDIUM));
597 pCurrent += sizeof(dwOffset);
603 return hr;
606 static void FM2_DeleteRegFilter(REGFILTER2 * prf2)
608 UINT i;
609 for (i = 0; i < prf2->u.s2.cPins2; i++)
611 UINT j;
612 CoTaskMemFree((void*)prf2->u.s2.rgPins2[i].clsPinCategory);
614 for (j = 0; j < prf2->u.s2.rgPins2[i].nMediaTypes; j++)
616 CoTaskMemFree((LPVOID)prf2->u.s2.rgPins2[i].lpMediaType[j].clsMajorType);
617 CoTaskMemFree((LPVOID)prf2->u.s2.rgPins2[i].lpMediaType[j].clsMinorType);
619 CoTaskMemFree((LPVOID)prf2->u.s2.rgPins2[i].lpMediaType);
620 CoTaskMemFree((LPVOID)prf2->u.s2.rgPins2[i].lpMedium);
622 CoTaskMemFree((LPVOID)prf2->u.s2.rgPins2);
625 static HRESULT WINAPI FilterMapper3_RegisterFilter(
626 IFilterMapper3 * iface,
627 REFCLSID clsidFilter,
628 LPCWSTR szName,
629 IMoniker **ppMoniker,
630 const CLSID *pclsidCategory,
631 const OLECHAR *szInstance,
632 const REGFILTER2 *prf2)
634 IParseDisplayName * pParser = NULL;
635 IBindCtx * pBindCtx = NULL;
636 IMoniker * pMoniker = NULL;
637 IPropertyBag * pPropBag = NULL;
638 HRESULT hr;
639 LPWSTR pwszParseName = NULL;
640 LPWSTR pCurrent;
641 static const WCHAR wszDevice[] = {'@','d','e','v','i','c','e',':','s','w',':',0};
642 int nameLen;
643 ULONG ulEaten;
644 LPWSTR szClsidTemp = NULL;
645 REGFILTER2 regfilter2;
646 REGFILTERPINS2* pregfp2 = NULL;
648 TRACE("(%s, %s, %p, %s, %s, %p)\n",
649 debugstr_guid(clsidFilter),
650 debugstr_w(szName),
651 ppMoniker,
652 debugstr_guid(pclsidCategory),
653 debugstr_w(szInstance),
654 prf2);
656 if (prf2->dwVersion == 2)
658 regfilter2 = *prf2;
660 else if (prf2->dwVersion == 1)
662 ULONG i;
663 DWORD flags;
664 /* REGFILTER2 structure is converted from version 1 to 2. Tested on Win2k. */
665 regfilter2.dwVersion = 2;
666 regfilter2.dwMerit = prf2->dwMerit;
667 regfilter2.u.s2.cPins2 = prf2->u.s1.cPins;
668 pregfp2 = CoTaskMemAlloc(prf2->u.s1.cPins * sizeof(REGFILTERPINS2));
669 regfilter2.u.s2.rgPins2 = pregfp2;
670 for (i = 0; i < prf2->u.s1.cPins; i++)
672 flags = 0;
673 if (prf2->u.s1.rgPins[i].bRendered)
674 flags |= REG_PINFLAG_B_RENDERER;
675 if (prf2->u.s1.rgPins[i].bOutput)
676 flags |= REG_PINFLAG_B_OUTPUT;
677 if (prf2->u.s1.rgPins[i].bZero)
678 flags |= REG_PINFLAG_B_ZERO;
679 if (prf2->u.s1.rgPins[i].bMany)
680 flags |= REG_PINFLAG_B_MANY;
681 pregfp2[i].dwFlags = flags;
682 pregfp2[i].cInstances = 1;
683 pregfp2[i].nMediaTypes = prf2->u.s1.rgPins[i].nMediaTypes;
684 pregfp2[i].lpMediaType = prf2->u.s1.rgPins[i].lpMediaType;
685 pregfp2[i].nMediums = 0;
686 pregfp2[i].lpMedium = NULL;
687 pregfp2[i].clsPinCategory = NULL;
690 else
692 FIXME("dwVersion other that 1 or 2 not supported at the moment\n");
693 return E_NOTIMPL;
696 if (ppMoniker)
697 *ppMoniker = NULL;
699 if (!pclsidCategory)
700 /* MSDN mentions the inexistent CLSID_ActiveMovieFilters GUID.
701 * In fact this is the CLSID_LegacyAmFilterCategory one */
702 pclsidCategory = &CLSID_LegacyAmFilterCategory;
704 /* sizeof... will include the null terminator and
705 * the + 1 is for the separator ('\\'). The -1 is
706 * because CHARS_IN_GUID includes the null terminator
708 nameLen = sizeof(wszDevice)/sizeof(wszDevice[0]) + CHARS_IN_GUID - 1 + 1;
710 if (szInstance)
711 nameLen += strlenW(szInstance);
712 else
713 nameLen += CHARS_IN_GUID - 1; /* CHARS_IN_GUID includes null terminator */
715 pCurrent = pwszParseName = CoTaskMemAlloc(nameLen*sizeof(WCHAR));
716 if (!pwszParseName)
717 return E_OUTOFMEMORY;
719 strcpyW(pwszParseName, wszDevice);
720 pCurrent += strlenW(wszDevice);
722 hr = StringFromCLSID(pclsidCategory, &szClsidTemp);
724 if (SUCCEEDED(hr))
726 memcpy(pCurrent, szClsidTemp, CHARS_IN_GUID * sizeof(WCHAR));
727 pCurrent += CHARS_IN_GUID - 1;
728 pCurrent[0] = '\\';
730 if (szInstance)
731 strcpyW(pCurrent+1, szInstance);
732 else
734 CoTaskMemFree(szClsidTemp);
735 szClsidTemp = NULL;
737 hr = StringFromCLSID(clsidFilter, &szClsidTemp);
738 if (SUCCEEDED(hr))
739 strcpyW(pCurrent+1, szClsidTemp);
743 if (SUCCEEDED(hr))
744 hr = CoCreateInstance(&CLSID_CDeviceMoniker, NULL, CLSCTX_INPROC, &IID_IParseDisplayName, (LPVOID *)&pParser);
746 if (SUCCEEDED(hr))
747 hr = CreateBindCtx(0, &pBindCtx);
749 if (SUCCEEDED(hr))
750 hr = IParseDisplayName_ParseDisplayName(pParser, pBindCtx, pwszParseName, &ulEaten, &pMoniker);
752 if (pBindCtx)
753 IBindCtx_Release(pBindCtx);
754 if (pParser)
755 IParseDisplayName_Release(pParser);
757 if (SUCCEEDED(hr))
758 hr = IMoniker_BindToStorage(pMoniker, NULL, NULL, &IID_IPropertyBag, (LPVOID)&pPropBag);
760 if (SUCCEEDED(hr))
761 hr = FM2_WriteFriendlyName(pPropBag, szName);
763 if (SUCCEEDED(hr))
764 hr = FM2_WriteClsid(pPropBag, clsidFilter);
766 if (SUCCEEDED(hr))
768 BYTE *pData;
769 ULONG cbData;
771 hr = FM2_WriteFilterData(&regfilter2, &pData, &cbData);
772 if (SUCCEEDED(hr))
774 VARIANT var;
775 SAFEARRAY *psa;
776 SAFEARRAYBOUND saBound;
778 saBound.lLbound = 0;
779 saBound.cElements = cbData;
780 psa = SafeArrayCreate(VT_UI1, 1, &saBound);
781 if (!psa)
783 ERR("Couldn't create SAFEARRAY\n");
784 hr = E_FAIL;
787 if (SUCCEEDED(hr))
789 LPBYTE pbSAData;
790 hr = SafeArrayAccessData(psa, (LPVOID *)&pbSAData);
791 if (SUCCEEDED(hr))
793 memcpy(pbSAData, pData, cbData);
794 hr = SafeArrayUnaccessData(psa);
798 V_VT(&var) = VT_ARRAY | VT_UI1;
799 V_ARRAY(&var) = psa;
801 if (SUCCEEDED(hr))
802 hr = IPropertyBag_Write(pPropBag, wszFilterDataName, &var);
804 if (psa)
805 SafeArrayDestroy(psa);
806 CoTaskMemFree(pData);
810 if (pPropBag)
811 IPropertyBag_Release(pPropBag);
812 CoTaskMemFree(szClsidTemp);
813 CoTaskMemFree(pwszParseName);
815 if (SUCCEEDED(hr) && ppMoniker)
816 *ppMoniker = pMoniker;
817 else if (pMoniker)
818 IMoniker_Release(pMoniker);
820 CoTaskMemFree(pregfp2);
822 TRACE("-- returning %x\n", hr);
824 return hr;
827 /* internal helper function */
828 static BOOL MatchTypes(
829 BOOL bExactMatch,
830 DWORD nPinTypes,
831 const REGPINTYPES * pPinTypes,
832 DWORD nMatchTypes,
833 const GUID * pMatchTypes)
835 BOOL bMatch = FALSE;
836 DWORD j;
838 if ((nMatchTypes == 0) && (nPinTypes > 0))
839 bMatch = TRUE;
841 for (j = 0; j < nPinTypes; j++)
843 DWORD i;
844 for (i = 0; i < nMatchTypes*2; i+=2)
846 if (((!bExactMatch && IsEqualGUID(pPinTypes[j].clsMajorType, &GUID_NULL)) || IsEqualGUID(&pMatchTypes[i], &GUID_NULL) || IsEqualGUID(pPinTypes[j].clsMajorType, &pMatchTypes[i])) &&
847 ((!bExactMatch && IsEqualGUID(pPinTypes[j].clsMinorType, &GUID_NULL)) || IsEqualGUID(&pMatchTypes[i+1], &GUID_NULL) || IsEqualGUID(pPinTypes[j].clsMinorType, &pMatchTypes[i+1])))
849 bMatch = TRUE;
850 break;
854 return bMatch;
857 /* internal helper function for qsort of MONIKER_MERIT array */
858 static int mm_compare(const void * left, const void * right)
860 const struct MONIKER_MERIT * mmLeft = left;
861 const struct MONIKER_MERIT * mmRight = right;
863 if (mmLeft->dwMerit == mmRight->dwMerit)
864 return 0;
865 if (mmLeft->dwMerit > mmRight->dwMerit)
866 return -1;
867 return 1;
870 /* NOTES:
871 * Exact match means whether or not to treat GUID_NULL's in filter data as wild cards
872 * (GUID_NULL's in input to function automatically treated as wild cards)
873 * Input/Output needed means match only on criteria if TRUE (with zero input types
874 * meaning match any input/output pin as long as one exists), otherwise match any
875 * filter that meets the rest of the requirements.
877 static HRESULT WINAPI FilterMapper3_EnumMatchingFilters(
878 IFilterMapper3 * iface,
879 IEnumMoniker **ppEnum,
880 DWORD dwFlags,
881 BOOL bExactMatch,
882 DWORD dwMerit,
883 BOOL bInputNeeded,
884 DWORD cInputTypes,
885 const GUID *pInputTypes,
886 const REGPINMEDIUM *pMedIn,
887 const CLSID *pPinCategoryIn,
888 BOOL bRender,
889 BOOL bOutputNeeded,
890 DWORD cOutputTypes,
891 const GUID *pOutputTypes,
892 const REGPINMEDIUM *pMedOut,
893 const CLSID *pPinCategoryOut)
895 ICreateDevEnum * pCreateDevEnum;
896 IMoniker * pMonikerCat;
897 IEnumMoniker * pEnumCat;
898 HRESULT hr;
899 struct Vector monikers = {NULL, 0, 0};
901 TRACE("(%p, %x, %s, %x, %s, %d, %p, %p, %p, %s, %s, %p, %p, %p)\n",
902 ppEnum,
903 dwFlags,
904 bExactMatch ? "true" : "false",
905 dwMerit,
906 bInputNeeded ? "true" : "false",
907 cInputTypes,
908 pInputTypes,
909 pMedIn,
910 pPinCategoryIn,
911 bRender ? "true" : "false",
912 bOutputNeeded ? "true" : "false",
913 pOutputTypes,
914 pMedOut,
915 pPinCategoryOut);
917 if (dwFlags != 0)
919 FIXME("dwFlags = %x not implemented\n", dwFlags);
922 *ppEnum = NULL;
924 hr = CoCreateInstance(&CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC, &IID_ICreateDevEnum, (LPVOID*)&pCreateDevEnum);
925 if (FAILED(hr))
926 return hr;
928 hr = ICreateDevEnum_CreateClassEnumerator(pCreateDevEnum, &CLSID_ActiveMovieCategories, &pEnumCat, 0);
929 if (FAILED(hr)) {
930 ICreateDevEnum_Release(pCreateDevEnum);
931 return hr;
934 while (IEnumMoniker_Next(pEnumCat, 1, &pMonikerCat, NULL) == S_OK)
936 IPropertyBag * pPropBagCat = NULL;
937 VARIANT var;
938 HRESULT hrSub; /* this is so that one buggy filter
939 doesn't make the whole lot fail */
941 VariantInit(&var);
943 hrSub = IMoniker_BindToStorage(pMonikerCat, NULL, NULL, &IID_IPropertyBag, (LPVOID*)&pPropBagCat);
945 if (SUCCEEDED(hrSub))
946 hrSub = IPropertyBag_Read(pPropBagCat, wszMeritName, &var, NULL);
948 if (SUCCEEDED(hrSub) && (V_UI4(&var) >= dwMerit))
950 CLSID clsidCat;
951 IEnumMoniker * pEnum;
952 IMoniker * pMoniker;
954 VariantClear(&var);
956 if (TRACE_ON(quartz))
958 VARIANT temp;
959 V_VT(&temp) = VT_EMPTY;
960 IPropertyBag_Read(pPropBagCat, wszFriendlyName, &temp, NULL);
961 TRACE("Considering category %s\n", debugstr_w(V_BSTR(&temp)));
962 VariantClear(&temp);
965 hrSub = IPropertyBag_Read(pPropBagCat, wszClsidName, &var, NULL);
967 if (SUCCEEDED(hrSub))
968 hrSub = CLSIDFromString(V_BSTR(&var), &clsidCat);
970 if (SUCCEEDED(hrSub))
971 hrSub = ICreateDevEnum_CreateClassEnumerator(pCreateDevEnum, &clsidCat, &pEnum, 0);
973 if (hrSub == S_OK)
975 while (IEnumMoniker_Next(pEnum, 1, &pMoniker, NULL) == S_OK)
977 IPropertyBag * pPropBag = NULL;
978 VARIANT var;
979 BYTE *pData = NULL;
980 REGFILTER2 rf2;
981 DWORD i;
982 BOOL bInputMatch = !bInputNeeded;
983 BOOL bOutputMatch = !bOutputNeeded;
985 ZeroMemory(&rf2, sizeof(rf2));
986 VariantInit(&var);
988 hrSub = IMoniker_BindToStorage(pMoniker, NULL, NULL, &IID_IPropertyBag, (LPVOID*)&pPropBag);
990 if (TRACE_ON(quartz))
992 VARIANT temp;
993 V_VT(&temp) = VT_EMPTY;
994 IPropertyBag_Read(pPropBag, wszFriendlyName, &temp, NULL);
995 TRACE("Considering filter %s\n", debugstr_w(V_BSTR(&temp)));
996 VariantClear(&temp);
999 if (SUCCEEDED(hrSub))
1001 hrSub = IPropertyBag_Read(pPropBag, wszFilterDataName, &var, NULL);
1004 if (SUCCEEDED(hrSub))
1005 hrSub = SafeArrayAccessData(V_ARRAY(&var), (LPVOID*)&pData);
1007 if (SUCCEEDED(hrSub))
1008 hrSub = FM2_ReadFilterData(pData, &rf2);
1010 if (pData)
1011 SafeArrayUnaccessData(V_ARRAY(&var));
1013 VariantClear(&var);
1015 /* Logic used for bInputMatch expression:
1016 * There exists some pin such that bInputNeeded implies (pin is an input and
1017 * (bRender implies pin has render flag) and major/minor types members of
1018 * pInputTypes )
1019 * bOutputMatch is similar, but without the "bRender implies ..." part
1020 * and substituting variables names containing input for output
1023 /* determine whether filter meets requirements */
1024 if (SUCCEEDED(hrSub) && (rf2.dwMerit >= dwMerit))
1026 for (i = 0; (i < rf2.u.s2.cPins2) && (!bInputMatch || !bOutputMatch); i++)
1028 const REGFILTERPINS2 * rfp2 = rf2.u.s2.rgPins2 + i;
1030 bInputMatch = bInputMatch || (!(rfp2->dwFlags & REG_PINFLAG_B_OUTPUT) &&
1031 (!bRender || (rfp2->dwFlags & REG_PINFLAG_B_RENDERER)) &&
1032 MatchTypes(bExactMatch, rfp2->nMediaTypes, rfp2->lpMediaType, cInputTypes, pInputTypes));
1033 bOutputMatch = bOutputMatch || ((rfp2->dwFlags & REG_PINFLAG_B_OUTPUT) &&
1034 MatchTypes(bExactMatch, rfp2->nMediaTypes, rfp2->lpMediaType, cOutputTypes, pOutputTypes));
1037 if (bInputMatch && bOutputMatch)
1039 struct MONIKER_MERIT mm = {pMoniker, rf2.dwMerit};
1040 IMoniker_AddRef(pMoniker);
1041 add_data(&monikers, (LPBYTE)&mm, sizeof(mm));
1045 FM2_DeleteRegFilter(&rf2);
1046 if (pPropBag)
1047 IPropertyBag_Release(pPropBag);
1048 IMoniker_Release(pMoniker);
1050 IEnumMoniker_Release(pEnum);
1054 VariantClear(&var);
1055 if (pPropBagCat)
1056 IPropertyBag_Release(pPropBagCat);
1057 IMoniker_Release(pMonikerCat);
1060 if (SUCCEEDED(hr))
1062 IMoniker ** ppMoniker;
1063 unsigned int i;
1064 ULONG nMonikerCount = monikers.current / sizeof(struct MONIKER_MERIT);
1066 /* sort the monikers in descending merit order */
1067 qsort(monikers.pData, nMonikerCount,
1068 sizeof(struct MONIKER_MERIT),
1069 mm_compare);
1071 /* construct an IEnumMoniker interface */
1072 ppMoniker = CoTaskMemAlloc(nMonikerCount * sizeof(IMoniker *));
1073 for (i = 0; i < nMonikerCount; i++)
1075 /* no need to AddRef here as already AddRef'd above */
1076 ppMoniker[i] = ((struct MONIKER_MERIT *)monikers.pData)[i].pMoniker;
1078 hr = EnumMonikerImpl_Create(ppMoniker, nMonikerCount, ppEnum);
1079 CoTaskMemFree(ppMoniker);
1082 delete_vector(&monikers);
1083 IEnumMoniker_Release(pEnumCat);
1084 ICreateDevEnum_Release(pCreateDevEnum);
1086 return hr;
1089 static HRESULT WINAPI FilterMapper3_GetICreateDevEnum(IFilterMapper3 *iface, ICreateDevEnum **ppEnum)
1091 TRACE("(%p, %p)\n", iface, ppEnum);
1092 if (!ppEnum)
1093 return E_POINTER;
1094 return CoCreateInstance(&CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC, &IID_ICreateDevEnum, (void**)ppEnum);
1097 static const IFilterMapper3Vtbl fm3vtbl =
1100 FilterMapper3_QueryInterface,
1101 FilterMapper3_AddRef,
1102 FilterMapper3_Release,
1104 FilterMapper3_CreateCategory,
1105 FilterMapper3_UnregisterFilter,
1106 FilterMapper3_RegisterFilter,
1107 FilterMapper3_EnumMatchingFilters,
1108 FilterMapper3_GetICreateDevEnum
1111 /*** IUnknown methods ***/
1113 static HRESULT WINAPI FilterMapper_QueryInterface(IFilterMapper * iface, REFIID riid, LPVOID *ppv)
1115 FilterMapper3Impl *This = impl_from_IFilterMapper(iface);
1117 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
1119 return FilterMapper3_QueryInterface(&This->IFilterMapper3_iface, riid, ppv);
1122 static ULONG WINAPI FilterMapper_AddRef(IFilterMapper * iface)
1124 FilterMapper3Impl *This = impl_from_IFilterMapper(iface);
1126 return IUnknown_AddRef(This->outer_unk);
1129 static ULONG WINAPI FilterMapper_Release(IFilterMapper * iface)
1131 FilterMapper3Impl *This = impl_from_IFilterMapper(iface);
1133 return IUnknown_Release(This->outer_unk);
1136 /*** IFilterMapper methods ***/
1138 static HRESULT WINAPI FilterMapper_EnumMatchingFilters(
1139 IFilterMapper * iface,
1140 IEnumRegFilters **ppEnum,
1141 DWORD dwMerit,
1142 BOOL bInputNeeded,
1143 CLSID clsInMaj,
1144 CLSID clsInSub,
1145 BOOL bRender,
1146 BOOL bOutputNeeded,
1147 CLSID clsOutMaj,
1148 CLSID clsOutSub)
1150 FilterMapper3Impl *This = impl_from_IFilterMapper(iface);
1151 GUID InputType[2];
1152 GUID OutputType[2];
1153 IEnumMoniker* ppEnumMoniker;
1154 IMoniker* IMon;
1155 ULONG nb;
1156 ULONG idx = 0, nb_mon = 0;
1157 REGFILTER* regfilters;
1158 HRESULT hr;
1160 TRACE("(%p/%p)->(%p, %x, %s, %s, %s, %s, %s, %s, %s)\n",
1161 This,
1162 iface,
1163 ppEnum,
1164 dwMerit,
1165 bInputNeeded ? "true" : "false",
1166 debugstr_guid(&clsInMaj),
1167 debugstr_guid(&clsInSub),
1168 bRender ? "true" : "false",
1169 bOutputNeeded ? "true" : "false",
1170 debugstr_guid(&clsOutMaj),
1171 debugstr_guid(&clsOutSub));
1173 InputType[0] = clsInMaj;
1174 InputType[1] = clsInSub;
1175 OutputType[0] = clsOutMaj;
1176 OutputType[1] = clsOutSub;
1178 *ppEnum = NULL;
1180 hr = IFilterMapper3_EnumMatchingFilters(&This->IFilterMapper3_iface, &ppEnumMoniker, 0, TRUE,
1181 dwMerit, bInputNeeded, 1, InputType, NULL, &GUID_NULL, bRender, bOutputNeeded, 1,
1182 OutputType, NULL, &GUID_NULL);
1184 if (FAILED(hr))
1185 return hr;
1187 while(IEnumMoniker_Next(ppEnumMoniker, 1, &IMon, &nb) == S_OK)
1189 IMoniker_Release(IMon);
1190 nb_mon++;
1193 if (!nb_mon)
1195 IEnumMoniker_Release(ppEnumMoniker);
1196 return IEnumRegFiltersImpl_Construct(NULL, 0, ppEnum);
1199 regfilters = CoTaskMemAlloc(nb_mon * sizeof(REGFILTER));
1200 if (!regfilters)
1202 IEnumMoniker_Release(ppEnumMoniker);
1203 return E_OUTOFMEMORY;
1205 ZeroMemory(regfilters, nb_mon * sizeof(REGFILTER)); /* will prevent bad free of Name in case of error. */
1207 IEnumMoniker_Reset(ppEnumMoniker);
1208 while(IEnumMoniker_Next(ppEnumMoniker, 1, &IMon, &nb) == S_OK)
1210 IPropertyBag * pPropBagCat = NULL;
1211 VARIANT var;
1212 HRESULT hrSub;
1213 GUID clsid;
1214 int len;
1216 VariantInit(&var);
1218 hrSub = IMoniker_BindToStorage(IMon, NULL, NULL, &IID_IPropertyBag, (LPVOID*)&pPropBagCat);
1220 if (SUCCEEDED(hrSub))
1221 hrSub = IPropertyBag_Read(pPropBagCat, wszClsidName, &var, NULL);
1223 if (SUCCEEDED(hrSub))
1224 hrSub = CLSIDFromString(V_BSTR(&var), &clsid);
1226 VariantClear(&var);
1228 if (SUCCEEDED(hrSub))
1229 hrSub = IPropertyBag_Read(pPropBagCat, wszFriendlyName, &var, NULL);
1231 if (SUCCEEDED(hrSub))
1233 len = (strlenW(V_BSTR(&var))+1) * sizeof(WCHAR);
1234 if (!(regfilters[idx].Name = CoTaskMemAlloc(len*2)))
1235 hr = E_OUTOFMEMORY;
1238 if (SUCCEEDED(hrSub) && regfilters[idx].Name)
1240 memcpy(regfilters[idx].Name, V_BSTR(&var), len);
1241 regfilters[idx].Clsid = clsid;
1242 idx++;
1245 if (pPropBagCat)
1246 IPropertyBag_Release(pPropBagCat);
1247 IMoniker_Release(IMon);
1248 VariantClear(&var);
1251 if (SUCCEEDED(hr))
1253 hr = IEnumRegFiltersImpl_Construct(regfilters, nb_mon, ppEnum);
1256 for (idx = 0; idx < nb_mon; idx++)
1257 CoTaskMemFree(regfilters[idx].Name);
1258 CoTaskMemFree(regfilters);
1259 IEnumMoniker_Release(ppEnumMoniker);
1261 return hr;
1265 static HRESULT WINAPI FilterMapper_RegisterFilter(IFilterMapper * iface, CLSID clsid, LPCWSTR szName, DWORD dwMerit)
1267 HRESULT hr;
1268 LPWSTR wszClsid = NULL;
1269 HKEY hKey;
1270 LONG lRet;
1271 WCHAR wszKeyName[ARRAYSIZE(wszFilterSlash)-1 + (CHARS_IN_GUID-1) + 1];
1273 TRACE("(%p)->(%s, %s, %x)\n", iface, debugstr_guid(&clsid), debugstr_w(szName), dwMerit);
1275 hr = StringFromCLSID(&clsid, &wszClsid);
1277 if (SUCCEEDED(hr))
1279 strcpyW(wszKeyName, wszFilterSlash);
1280 strcatW(wszKeyName, wszClsid);
1282 lRet = RegCreateKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
1283 hr = HRESULT_FROM_WIN32(lRet);
1286 if (SUCCEEDED(hr))
1288 lRet = RegSetValueExW(hKey, NULL, 0, REG_SZ, (const BYTE*)szName, (strlenW(szName) + 1) * sizeof(WCHAR));
1289 hr = HRESULT_FROM_WIN32(lRet);
1290 RegCloseKey(hKey);
1293 if (SUCCEEDED(hr))
1295 strcpyW(wszKeyName, wszClsidSlash);
1296 strcatW(wszKeyName, wszClsid);
1298 lRet = RegCreateKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
1299 hr = HRESULT_FROM_WIN32(lRet);
1302 if (SUCCEEDED(hr))
1304 lRet = RegSetValueExW(hKey, wszMeritName, 0, REG_DWORD, (LPBYTE)&dwMerit, sizeof(dwMerit));
1305 hr = HRESULT_FROM_WIN32(lRet);
1306 RegCloseKey(hKey);
1309 CoTaskMemFree(wszClsid);
1311 return hr;
1314 static HRESULT WINAPI FilterMapper_RegisterFilterInstance(IFilterMapper * iface, CLSID clsid, LPCWSTR szName, CLSID *MRId)
1316 TRACE("(%p)->(%s, %s, %p)\n", iface, debugstr_guid(&clsid), debugstr_w(szName), MRId);
1318 /* Not implemented in Windows (tested on Win2k) */
1320 return E_NOTIMPL;
1323 static HRESULT WINAPI FilterMapper_RegisterPin(
1324 IFilterMapper * iface,
1325 CLSID Filter,
1326 LPCWSTR szName,
1327 BOOL bRendered,
1328 BOOL bOutput,
1329 BOOL bZero,
1330 BOOL bMany,
1331 CLSID ConnectsToFilter,
1332 LPCWSTR ConnectsToPin)
1334 HRESULT hr;
1335 LONG lRet;
1336 LPWSTR wszClsid = NULL;
1337 HKEY hKey = NULL;
1338 HKEY hPinsKey = NULL;
1339 WCHAR * wszPinsKeyName;
1340 WCHAR wszKeyName[ARRAYSIZE(wszClsidSlash)-1 + (CHARS_IN_GUID-1) + 1];
1342 TRACE("(%p)->(%s, %s, %d, %d, %d, %d, %s, %s)\n", iface, debugstr_guid(&Filter), debugstr_w(szName), bRendered,
1343 bOutput, bZero, bMany, debugstr_guid(&ConnectsToFilter), debugstr_w(ConnectsToPin));
1345 hr = StringFromCLSID(&Filter, &wszClsid);
1347 if (SUCCEEDED(hr))
1349 strcpyW(wszKeyName, wszClsidSlash);
1350 strcatW(wszKeyName, wszClsid);
1352 lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, KEY_WRITE, &hKey);
1353 hr = HRESULT_FROM_WIN32(lRet);
1356 if (SUCCEEDED(hr))
1358 wszPinsKeyName = CoTaskMemAlloc((strlenW(wszPins) + 1 + strlenW(szName) + 1) * 2);
1359 if (!wszPinsKeyName)
1360 hr = E_OUTOFMEMORY;
1363 if (SUCCEEDED(hr))
1365 strcpyW(wszPinsKeyName, wszPins);
1366 strcatW(wszPinsKeyName, wszSlash);
1367 strcatW(wszPinsKeyName, szName);
1369 lRet = RegCreateKeyExW(hKey, wszPinsKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hPinsKey, NULL);
1370 hr = HRESULT_FROM_WIN32(lRet);
1371 CoTaskMemFree(wszPinsKeyName);
1374 if (SUCCEEDED(hr))
1376 lRet = RegSetValueExW(hPinsKey, wszAllowedMany, 0, REG_DWORD, (LPBYTE)&bMany, sizeof(bMany));
1377 hr = HRESULT_FROM_WIN32(lRet);
1380 if (SUCCEEDED(hr))
1382 lRet = RegSetValueExW(hPinsKey, wszAllowedZero, 0, REG_DWORD, (LPBYTE)&bZero, sizeof(bZero));
1383 hr = HRESULT_FROM_WIN32(lRet);
1386 if (SUCCEEDED(hr))
1388 lRet = RegSetValueExW(hPinsKey, wszDirection, 0, REG_DWORD, (LPBYTE)&bOutput, sizeof(bOutput));
1389 hr = HRESULT_FROM_WIN32(lRet);
1392 if (SUCCEEDED(hr))
1394 lRet = RegSetValueExW(hPinsKey, wszIsRendered, 0, REG_DWORD, (LPBYTE)&bRendered, sizeof(bRendered));
1395 hr = HRESULT_FROM_WIN32(lRet);
1398 if (SUCCEEDED(hr))
1400 HKEY hkeyDummy = NULL;
1402 lRet = RegCreateKeyExW(hPinsKey, wszTypes, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkeyDummy, NULL);
1403 hr = HRESULT_FROM_WIN32(lRet);
1405 if (hkeyDummy) RegCloseKey(hkeyDummy);
1408 CoTaskMemFree(wszClsid);
1409 if (hKey)
1410 RegCloseKey(hKey);
1411 if (hPinsKey)
1412 RegCloseKey(hPinsKey);
1414 return hr;
1418 static HRESULT WINAPI FilterMapper_RegisterPinType(
1419 IFilterMapper * iface,
1420 CLSID clsFilter,
1421 LPCWSTR szName,
1422 CLSID clsMajorType,
1423 CLSID clsSubType)
1425 HRESULT hr;
1426 LONG lRet;
1427 LPWSTR wszClsid = NULL;
1428 LPWSTR wszClsidMajorType = NULL;
1429 LPWSTR wszClsidSubType = NULL;
1430 HKEY hKey = NULL;
1431 WCHAR * wszTypesKey;
1432 WCHAR wszKeyName[MAX_PATH];
1434 TRACE("(%p)->(%s, %s, %s, %s)\n", iface, debugstr_guid(&clsFilter), debugstr_w(szName),
1435 debugstr_guid(&clsMajorType), debugstr_guid(&clsSubType));
1437 hr = StringFromCLSID(&clsFilter, &wszClsid);
1439 if (SUCCEEDED(hr))
1441 hr = StringFromCLSID(&clsMajorType, &wszClsidMajorType);
1444 if (SUCCEEDED(hr))
1446 hr = StringFromCLSID(&clsSubType, &wszClsidSubType);
1449 if (SUCCEEDED(hr))
1451 wszTypesKey = CoTaskMemAlloc((strlenW(wszClsidSlash) + strlenW(wszClsid) + strlenW(wszPins) +
1452 strlenW(szName) + strlenW(wszTypes) + 3 + 1) * 2);
1453 if (!wszTypesKey)
1454 hr = E_OUTOFMEMORY;
1457 if (SUCCEEDED(hr))
1459 strcpyW(wszTypesKey, wszClsidSlash);
1460 strcatW(wszTypesKey, wszClsid);
1461 strcatW(wszTypesKey, wszSlash);
1462 strcatW(wszTypesKey, wszPins);
1463 strcatW(wszTypesKey, wszSlash);
1464 strcatW(wszTypesKey, szName);
1465 strcatW(wszTypesKey, wszSlash);
1466 strcatW(wszTypesKey, wszTypes);
1468 lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszTypesKey, 0, KEY_WRITE, &hKey);
1469 hr = HRESULT_FROM_WIN32(lRet);
1470 CoTaskMemFree(wszTypesKey);
1473 if (SUCCEEDED(hr))
1475 HKEY hkeyDummy = NULL;
1477 strcpyW(wszKeyName, wszClsidMajorType);
1478 strcatW(wszKeyName, wszSlash);
1479 strcatW(wszKeyName, wszClsidSubType);
1481 lRet = RegCreateKeyExW(hKey, wszKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkeyDummy, NULL);
1482 hr = HRESULT_FROM_WIN32(lRet);
1483 RegCloseKey(hKey);
1485 if (hkeyDummy) RegCloseKey(hkeyDummy);
1488 CoTaskMemFree(wszClsid);
1489 CoTaskMemFree(wszClsidMajorType);
1490 CoTaskMemFree(wszClsidSubType);
1492 return hr;
1495 static HRESULT WINAPI FilterMapper_UnregisterFilter(IFilterMapper * iface, CLSID Filter)
1497 HRESULT hr;
1498 LONG lRet;
1499 LPWSTR wszClsid = NULL;
1500 HKEY hKey;
1501 WCHAR wszKeyName[ARRAYSIZE(wszClsidSlash)-1 + (CHARS_IN_GUID-1) + 1];
1503 TRACE("(%p)->(%s)\n", iface, debugstr_guid(&Filter));
1505 hr = StringFromCLSID(&Filter, &wszClsid);
1507 if (SUCCEEDED(hr))
1509 lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszFilter, 0, KEY_WRITE, &hKey);
1510 hr = HRESULT_FROM_WIN32(lRet);
1513 if (SUCCEEDED(hr))
1515 lRet = RegDeleteKeyW(hKey, wszClsid);
1516 hr = HRESULT_FROM_WIN32(lRet);
1517 RegCloseKey(hKey);
1520 if (SUCCEEDED(hr))
1522 strcpyW(wszKeyName, wszClsidSlash);
1523 strcatW(wszKeyName, wszClsid);
1525 lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, KEY_WRITE, &hKey);
1526 hr = HRESULT_FROM_WIN32(lRet);
1529 if (SUCCEEDED(hr))
1531 lRet = RegDeleteValueW(hKey, wszMeritName);
1532 if (lRet != ERROR_SUCCESS)
1533 hr = HRESULT_FROM_WIN32(lRet);
1535 lRet = RegDeleteTreeW(hKey, wszPins);
1536 if (lRet != ERROR_SUCCESS)
1537 hr = HRESULT_FROM_WIN32(lRet);
1539 RegCloseKey(hKey);
1542 CoTaskMemFree(wszClsid);
1544 return hr;
1547 static HRESULT WINAPI FilterMapper_UnregisterFilterInstance(IFilterMapper * iface, CLSID MRId)
1549 TRACE("(%p)->(%s)\n", iface, debugstr_guid(&MRId));
1551 /* Not implemented in Windows (tested on Win2k) */
1553 return E_NOTIMPL;
1556 static HRESULT WINAPI FilterMapper_UnregisterPin(IFilterMapper * iface, CLSID Filter, LPCWSTR Name)
1558 HRESULT hr;
1559 LONG lRet;
1560 LPWSTR wszClsid = NULL;
1561 HKEY hKey = NULL;
1562 WCHAR * wszPinNameKey;
1563 WCHAR wszKeyName[ARRAYSIZE(wszClsidSlash)-1 + (CHARS_IN_GUID-1) + 1];
1565 TRACE("(%p)->(%s, %s)\n", iface, debugstr_guid(&Filter), debugstr_w(Name));
1567 if (!Name)
1568 return E_INVALIDARG;
1570 hr = StringFromCLSID(&Filter, &wszClsid);
1572 if (SUCCEEDED(hr))
1574 strcpyW(wszKeyName, wszClsidSlash);
1575 strcatW(wszKeyName, wszClsid);
1577 lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszKeyName, 0, KEY_WRITE, &hKey);
1578 hr = HRESULT_FROM_WIN32(lRet);
1581 if (SUCCEEDED(hr))
1583 wszPinNameKey = CoTaskMemAlloc((strlenW(wszPins) + 1 + strlenW(Name) + 1) * 2);
1584 if (!wszPinNameKey)
1585 hr = E_OUTOFMEMORY;
1588 if (SUCCEEDED(hr))
1590 strcpyW(wszPinNameKey, wszPins);
1591 strcatW(wszPinNameKey, wszSlash);
1592 strcatW(wszPinNameKey, Name);
1594 lRet = RegDeleteTreeW(hKey, wszPinNameKey);
1595 hr = HRESULT_FROM_WIN32(lRet);
1596 CoTaskMemFree(wszPinNameKey);
1599 CoTaskMemFree(wszClsid);
1600 if (hKey)
1601 RegCloseKey(hKey);
1603 return hr;
1606 static const IFilterMapperVtbl fmvtbl =
1609 FilterMapper_QueryInterface,
1610 FilterMapper_AddRef,
1611 FilterMapper_Release,
1613 FilterMapper_RegisterFilter,
1614 FilterMapper_RegisterFilterInstance,
1615 FilterMapper_RegisterPin,
1616 FilterMapper_RegisterPinType,
1617 FilterMapper_UnregisterFilter,
1618 FilterMapper_UnregisterFilterInstance,
1619 FilterMapper_UnregisterPin,
1620 FilterMapper_EnumMatchingFilters
1624 /*** IUnknown methods ***/
1625 static HRESULT WINAPI AMFilterData_QueryInterface(IAMFilterData * iface, REFIID riid, LPVOID *ppv)
1627 FilterMapper3Impl *This = impl_from_IAMFilterData(iface);
1629 return IUnknown_QueryInterface(This->outer_unk, riid, ppv);
1632 static ULONG WINAPI AMFilterData_AddRef(IAMFilterData * iface)
1634 FilterMapper3Impl *This = impl_from_IAMFilterData(iface);
1636 return IUnknown_AddRef(This->outer_unk);
1639 static ULONG WINAPI AMFilterData_Release(IAMFilterData * iface)
1641 FilterMapper3Impl *This = impl_from_IAMFilterData(iface);
1643 return IUnknown_Release(This->outer_unk);
1646 /*** IAMFilterData methods ***/
1647 static HRESULT WINAPI AMFilterData_ParseFilterData(IAMFilterData* iface,
1648 BYTE *pData, ULONG cb,
1649 BYTE **ppRegFilter2)
1651 FilterMapper3Impl *This = impl_from_IAMFilterData(iface);
1652 HRESULT hr = S_OK;
1653 static REGFILTER2 *prf2;
1655 TRACE("(%p/%p)->(%p, %d, %p)\n", This, iface, pData, cb, ppRegFilter2);
1657 prf2 = CoTaskMemAlloc(sizeof(*prf2));
1658 if (!prf2)
1659 return E_OUTOFMEMORY;
1660 *ppRegFilter2 = (BYTE *)&prf2;
1662 hr = FM2_ReadFilterData(pData, prf2);
1663 if (FAILED(hr))
1665 CoTaskMemFree(prf2);
1666 *ppRegFilter2 = NULL;
1669 return hr;
1672 static HRESULT WINAPI AMFilterData_CreateFilterData(IAMFilterData* iface,
1673 REGFILTER2 *prf2,
1674 BYTE **pRegFilterData,
1675 ULONG *pcb)
1677 FilterMapper3Impl *This = impl_from_IAMFilterData(iface);
1679 TRACE("(%p/%p)->(%p, %p, %p)\n", This, iface, prf2, pRegFilterData, pcb);
1681 return FM2_WriteFilterData(prf2, pRegFilterData, pcb);
1684 static const IAMFilterDataVtbl AMFilterDataVtbl = {
1685 AMFilterData_QueryInterface,
1686 AMFilterData_AddRef,
1687 AMFilterData_Release,
1688 AMFilterData_ParseFilterData,
1689 AMFilterData_CreateFilterData
1692 HRESULT FilterMapper2_create(IUnknown *pUnkOuter, LPVOID *ppObj)
1694 FilterMapper3Impl * pFM2impl;
1696 TRACE("(%p, %p)\n", pUnkOuter, ppObj);
1698 pFM2impl = CoTaskMemAlloc(sizeof(*pFM2impl));
1699 if (!pFM2impl)
1700 return E_OUTOFMEMORY;
1702 pFM2impl->IUnknown_inner.lpVtbl = &IInner_VTable;
1703 pFM2impl->IFilterMapper3_iface.lpVtbl = &fm3vtbl;
1704 pFM2impl->IFilterMapper_iface.lpVtbl = &fmvtbl;
1705 pFM2impl->IAMFilterData_iface.lpVtbl = &AMFilterDataVtbl;
1706 pFM2impl->ref = 1;
1708 if (pUnkOuter)
1709 pFM2impl->outer_unk = pUnkOuter;
1710 else
1711 pFM2impl->outer_unk = &pFM2impl->IUnknown_inner;
1713 *ppObj = &pFM2impl->IUnknown_inner;
1715 TRACE("-- created at %p\n", pFM2impl);
1717 return S_OK;
1720 HRESULT FilterMapper_create(IUnknown *pUnkOuter, LPVOID *ppObj)
1722 FilterMapper3Impl *pFM2impl;
1723 HRESULT hr;
1725 TRACE("(%p, %p)\n", pUnkOuter, ppObj);
1727 hr = FilterMapper2_create(pUnkOuter, (LPVOID*)&pFM2impl);
1728 if (FAILED(hr))
1729 return hr;
1731 *ppObj = &pFM2impl->IFilterMapper_iface;
1733 return hr;