include: Add missing definitions for LPDIRECT3DRM2 and LPDIRECT3DRM3.
[wine/multimedia.git] / dlls / fusion / asmenum.c
blobd4ea905e02572eaf4fbe8046b55bc916bb4f4a01
1 /*
2 * IAssemblyEnum implementation
4 * Copyright 2008 James Hawkins
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
23 #define COBJMACROS
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "ole2.h"
29 #include "guiddef.h"
30 #include "fusion.h"
31 #include "corerror.h"
32 #include "fusionpriv.h"
34 #include "wine/debug.h"
35 #include "wine/unicode.h"
36 #include "wine/list.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(fusion);
40 typedef struct _tagASMNAME
42 struct list entry;
43 IAssemblyName *name;
44 } ASMNAME;
46 typedef struct
48 const IAssemblyEnumVtbl *lpIAssemblyEnumVtbl;
50 struct list assemblies;
51 struct list *iter;
52 LONG ref;
53 } IAssemblyEnumImpl;
55 static HRESULT WINAPI IAssemblyEnumImpl_QueryInterface(IAssemblyEnum *iface,
56 REFIID riid, LPVOID *ppobj)
58 IAssemblyEnumImpl *This = (IAssemblyEnumImpl *)iface;
60 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
62 *ppobj = NULL;
64 if (IsEqualIID(riid, &IID_IUnknown) ||
65 IsEqualIID(riid, &IID_IAssemblyEnum))
67 IUnknown_AddRef(iface);
68 *ppobj = This;
69 return S_OK;
72 WARN("(%p, %s, %p): not found\n", This, debugstr_guid(riid), ppobj);
73 return E_NOINTERFACE;
76 static ULONG WINAPI IAssemblyEnumImpl_AddRef(IAssemblyEnum *iface)
78 IAssemblyEnumImpl *This = (IAssemblyEnumImpl *)iface;
79 ULONG refCount = InterlockedIncrement(&This->ref);
81 TRACE("(%p)->(ref before = %u)\n", This, refCount - 1);
83 return refCount;
86 static ULONG WINAPI IAssemblyEnumImpl_Release(IAssemblyEnum *iface)
88 IAssemblyEnumImpl *This = (IAssemblyEnumImpl *)iface;
89 ULONG refCount = InterlockedDecrement(&This->ref);
90 struct list *item, *cursor;
92 TRACE("(%p)->(ref before = %u)\n", This, refCount + 1);
94 if (!refCount)
96 LIST_FOR_EACH_SAFE(item, cursor, &This->assemblies)
98 ASMNAME *asmname = LIST_ENTRY(item, ASMNAME, entry);
100 list_remove(&asmname->entry);
101 IAssemblyName_Release(asmname->name);
102 HeapFree(GetProcessHeap(), 0, asmname);
105 HeapFree(GetProcessHeap(), 0, This);
108 return refCount;
111 static HRESULT WINAPI IAssemblyEnumImpl_GetNextAssembly(IAssemblyEnum *iface,
112 LPVOID pvReserved,
113 IAssemblyName **ppName,
114 DWORD dwFlags)
116 IAssemblyEnumImpl *asmenum = (IAssemblyEnumImpl *)iface;
117 ASMNAME *asmname;
119 TRACE("(%p, %p, %p, %d)\n", iface, pvReserved, ppName, dwFlags);
121 if (!ppName)
122 return E_INVALIDARG;
124 asmname = LIST_ENTRY(asmenum->iter, ASMNAME, entry);
125 if (!asmname)
126 return S_FALSE;
128 *ppName = asmname->name;
129 IAssemblyName_AddRef(*ppName);
131 asmenum->iter = list_next(&asmenum->assemblies, asmenum->iter);
133 return S_OK;
136 static HRESULT WINAPI IAssemblyEnumImpl_Reset(IAssemblyEnum *iface)
138 IAssemblyEnumImpl *asmenum = (IAssemblyEnumImpl *)iface;
140 TRACE("(%p)\n", iface);
142 asmenum->iter = list_head(&asmenum->assemblies);
143 return S_OK;
146 static HRESULT WINAPI IAssemblyEnumImpl_Clone(IAssemblyEnum *iface,
147 IAssemblyEnum **ppEnum)
149 FIXME("(%p, %p) stub!\n", iface, ppEnum);
150 return E_NOTIMPL;
153 static const IAssemblyEnumVtbl AssemblyEnumVtbl = {
154 IAssemblyEnumImpl_QueryInterface,
155 IAssemblyEnumImpl_AddRef,
156 IAssemblyEnumImpl_Release,
157 IAssemblyEnumImpl_GetNextAssembly,
158 IAssemblyEnumImpl_Reset,
159 IAssemblyEnumImpl_Clone
162 static void parse_name(IAssemblyName *name, int depth, LPWSTR path, LPWSTR buf)
164 WCHAR disp[MAX_PATH];
165 LPCWSTR verptr, pubkeyptr;
166 HRESULT hr;
167 DWORD size, major_size, minor_size, build_size, revision_size;
168 WORD major, minor, build, revision;
170 static const WCHAR star[] = {'*',0};
171 static const WCHAR ss_fmt[] = {'%','s','\\','%','s',0};
172 static const WCHAR verpubkey[] = {'%','s','\\','%','s','_','_','%','s',0};
173 static const WCHAR ver_fmt[] = {'%','u','.','%','u','.','%','u','.','%','u',0};
175 WCHAR version[24]; /* strlen("65535") * 4 + 3 + 1 */
176 WCHAR token_str[TOKEN_LENGTH + 1];
177 BYTE token[BYTES_PER_TOKEN];
179 if (depth == 0)
181 size = MAX_PATH;
182 *disp = '\0';
183 hr = IAssemblyName_GetName(name, &size, disp);
184 if (SUCCEEDED(hr))
185 sprintfW(buf, ss_fmt, path, disp);
186 else
187 sprintfW(buf, ss_fmt, path, star);
189 else if (depth == 1)
191 major_size = sizeof(major);
192 IAssemblyName_GetProperty(name, ASM_NAME_MAJOR_VERSION, &major, &major_size);
194 minor_size = sizeof(minor);
195 IAssemblyName_GetProperty(name, ASM_NAME_MINOR_VERSION, &minor, &minor_size);
197 build_size = sizeof(build);
198 IAssemblyName_GetProperty(name, ASM_NAME_BUILD_NUMBER, &build, &build_size);
200 revision_size = sizeof(revision);
201 IAssemblyName_GetProperty(name, ASM_NAME_REVISION_NUMBER, &revision, &revision_size);
203 if (!major_size || !minor_size || !build_size || !revision_size) verptr = star;
204 else
206 sprintfW(version, ver_fmt, major, minor, build, revision);
207 verptr = version;
210 size = sizeof(token);
211 IAssemblyName_GetProperty(name, ASM_NAME_PUBLIC_KEY_TOKEN, token, &size);
213 if (!size) pubkeyptr = star;
214 else
216 token_to_str(token, token_str);
217 pubkeyptr = token_str;
220 sprintfW(buf, verpubkey, path, verptr, pubkeyptr);
224 static int compare_assembly_names(ASMNAME *asmname1, ASMNAME *asmname2)
226 int ret;
227 WORD version1, version2;
228 WCHAR name1[MAX_PATH], name2[MAX_PATH];
229 WCHAR token_str1[TOKEN_LENGTH + 1], token_str2[TOKEN_LENGTH + 1];
230 BYTE token1[BYTES_PER_TOKEN], token2[BYTES_PER_TOKEN];
231 DWORD size, i;
233 size = sizeof(name1);
234 IAssemblyName_GetProperty(asmname1->name, ASM_NAME_NAME, &name1, &size);
235 size = sizeof(name2);
236 IAssemblyName_GetProperty(asmname2->name, ASM_NAME_NAME, &name2, &size);
238 if ((ret = strcmpiW(name1, name2))) return ret;
240 for (i = ASM_NAME_MAJOR_VERSION; i < ASM_NAME_CULTURE; i++)
242 size = sizeof(version1);
243 IAssemblyName_GetProperty(asmname1->name, i, &version1, &size);
244 size = sizeof(version2);
245 IAssemblyName_GetProperty(asmname2->name, i, &version2, &size);
247 if (version1 < version2) return -1;
248 if (version1 > version2) return 1;
251 /* FIXME: compare cultures */
253 size = sizeof(token1);
254 IAssemblyName_GetProperty(asmname1->name, ASM_NAME_PUBLIC_KEY_TOKEN, token1, &size);
255 size = sizeof(token2);
256 IAssemblyName_GetProperty(asmname2->name, ASM_NAME_PUBLIC_KEY_TOKEN, token2, &size);
258 token_to_str(token1, token_str1);
259 token_to_str(token2, token_str2);
261 if ((ret = strcmpiW(token_str1, token_str2))) return ret;
263 return 0;
266 /* insert assembly in list preserving sort order */
267 static void insert_assembly(struct list *assemblies, ASMNAME *to_insert)
269 struct list *item;
271 LIST_FOR_EACH(item, assemblies)
273 ASMNAME *name = LIST_ENTRY(item, ASMNAME, entry);
275 if (compare_assembly_names(name, to_insert) > 0)
277 list_add_before(&name->entry, &to_insert->entry);
278 return;
281 list_add_tail(assemblies, &to_insert->entry);
284 static HRESULT enum_gac_assemblies(struct list *assemblies, IAssemblyName *name,
285 int depth, LPWSTR path)
287 WIN32_FIND_DATAW ffd;
288 WCHAR buf[MAX_PATH];
289 WCHAR disp[MAX_PATH];
290 WCHAR asmpath[MAX_PATH];
291 ASMNAME *asmname;
292 HANDLE hfind;
293 LPWSTR ptr;
294 HRESULT hr = S_OK;
296 static WCHAR parent[MAX_PATH];
298 static const WCHAR dot[] = {'.',0};
299 static const WCHAR dotdot[] = {'.','.',0};
300 static const WCHAR search_fmt[] = {'%','s','\\','*',0};
301 static const WCHAR dblunder[] = {'_','_',0};
302 static const WCHAR path_fmt[] = {'%','s','\\','%','s','\\','%','s','.','d','l','l',0};
303 static const WCHAR fmt[] = {'%','s',',',' ','V','e','r','s','i','o','n','=','%','s',',',' ',
304 'C','u','l','t','u','r','e','=','n','e','u','t','r','a','l',',',' ',
305 'P','u','b','l','i','c','K','e','y','T','o','k','e','n','=','%','s',0};
306 static const WCHAR ss_fmt[] = {'%','s','\\','%','s',0};
308 if (name)
309 parse_name(name, depth, path, buf);
310 else
311 sprintfW(buf, search_fmt, path);
313 hfind = FindFirstFileW(buf, &ffd);
314 if (hfind == INVALID_HANDLE_VALUE)
315 return S_OK;
319 if (!lstrcmpW(ffd.cFileName, dot) || !lstrcmpW(ffd.cFileName, dotdot))
320 continue;
322 if (depth == 0)
324 if (name)
325 ptr = strrchrW(buf, '\\') + 1;
326 else
327 ptr = ffd.cFileName;
329 lstrcpyW(parent, ptr);
331 else if (depth == 1)
333 sprintfW(asmpath, path_fmt, path, ffd.cFileName, parent);
335 ptr = strstrW(ffd.cFileName, dblunder);
336 *ptr = '\0';
337 ptr += 2;
339 sprintfW(disp, fmt, parent, ffd.cFileName, ptr);
341 asmname = HeapAlloc(GetProcessHeap(), 0, sizeof(ASMNAME));
342 if (!asmname)
344 hr = E_OUTOFMEMORY;
345 break;
348 hr = CreateAssemblyNameObject(&asmname->name, disp,
349 CANOF_PARSE_DISPLAY_NAME, NULL);
350 if (FAILED(hr))
352 HeapFree(GetProcessHeap(), 0, asmname);
353 break;
356 hr = IAssemblyName_SetPath(asmname->name, asmpath);
357 if (FAILED(hr))
359 IAssemblyName_Release(asmname->name);
360 HeapFree(GetProcessHeap(), 0, asmname);
361 break;
364 insert_assembly(assemblies, asmname);
365 continue;
368 sprintfW(buf, ss_fmt, path, ffd.cFileName);
369 hr = enum_gac_assemblies(assemblies, name, depth + 1, buf);
370 if (FAILED(hr))
371 break;
372 } while (FindNextFileW(hfind, &ffd) != 0);
374 FindClose(hfind);
375 return hr;
378 static HRESULT enumerate_gac(IAssemblyEnumImpl *asmenum, IAssemblyName *pName)
380 WCHAR path[MAX_PATH];
381 WCHAR buf[MAX_PATH];
382 HRESULT hr;
383 DWORD size;
385 static WCHAR under32[] = {'_','3','2',0};
386 static WCHAR msil[] = {'_','M','S','I','L',0};
388 size = MAX_PATH;
389 hr = GetCachePath(ASM_CACHE_GAC, buf, &size);
390 if (FAILED(hr))
391 return hr;
393 lstrcpyW(path, buf);
394 lstrcatW(path, under32);
395 hr = enum_gac_assemblies(&asmenum->assemblies, pName, 0, path);
396 if (FAILED(hr))
397 return hr;
399 lstrcpyW(path, buf);
400 lstrcatW(path, msil);
401 hr = enum_gac_assemblies(&asmenum->assemblies, pName, 0, path);
402 if (FAILED(hr))
403 return hr;
405 hr = enum_gac_assemblies(&asmenum->assemblies, pName, 0, buf);
406 if (FAILED(hr))
407 return hr;
409 return S_OK;
412 /******************************************************************
413 * CreateAssemblyEnum (FUSION.@)
415 HRESULT WINAPI CreateAssemblyEnum(IAssemblyEnum **pEnum, IUnknown *pUnkReserved,
416 IAssemblyName *pName, DWORD dwFlags, LPVOID pvReserved)
418 IAssemblyEnumImpl *asmenum;
419 HRESULT hr;
421 TRACE("(%p, %p, %p, %08x, %p)\n", pEnum, pUnkReserved,
422 pName, dwFlags, pvReserved);
424 if (!pEnum)
425 return E_INVALIDARG;
427 if (dwFlags == 0 || dwFlags == ASM_CACHE_ROOT)
428 return E_INVALIDARG;
430 asmenum = HeapAlloc(GetProcessHeap(), 0, sizeof(IAssemblyEnumImpl));
431 if (!asmenum)
432 return E_OUTOFMEMORY;
434 asmenum->lpIAssemblyEnumVtbl = &AssemblyEnumVtbl;
435 asmenum->ref = 1;
436 list_init(&asmenum->assemblies);
438 if (dwFlags & ASM_CACHE_GAC)
440 hr = enumerate_gac(asmenum, pName);
441 if (FAILED(hr))
443 HeapFree(GetProcessHeap(), 0, asmenum);
444 return hr;
448 asmenum->iter = list_head(&asmenum->assemblies);
449 *pEnum = (IAssemblyEnum *)asmenum;
451 return S_OK;