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
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
34 #include "fusionpriv.h"
36 #include "wine/debug.h"
37 #include "wine/unicode.h"
38 #include "wine/list.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(fusion);
42 typedef struct _tagASMNAME
50 IAssemblyEnum IAssemblyEnum_iface;
52 struct list assemblies;
57 static inline IAssemblyEnumImpl *impl_from_IAssemblyEnum(IAssemblyEnum *iface)
59 return CONTAINING_RECORD(iface, IAssemblyEnumImpl, IAssemblyEnum_iface);
62 static HRESULT WINAPI IAssemblyEnumImpl_QueryInterface(IAssemblyEnum *iface,
63 REFIID riid, LPVOID *ppobj)
65 IAssemblyEnumImpl *This = impl_from_IAssemblyEnum(iface);
67 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
71 if (IsEqualIID(riid, &IID_IUnknown) ||
72 IsEqualIID(riid, &IID_IAssemblyEnum))
74 IUnknown_AddRef(iface);
79 WARN("(%p, %s, %p): not found\n", This, debugstr_guid(riid), ppobj);
83 static ULONG WINAPI IAssemblyEnumImpl_AddRef(IAssemblyEnum *iface)
85 IAssemblyEnumImpl *This = impl_from_IAssemblyEnum(iface);
86 ULONG refCount = InterlockedIncrement(&This->ref);
88 TRACE("(%p)->(ref before = %u)\n", This, refCount - 1);
93 static ULONG WINAPI IAssemblyEnumImpl_Release(IAssemblyEnum *iface)
95 IAssemblyEnumImpl *This = impl_from_IAssemblyEnum(iface);
96 ULONG refCount = InterlockedDecrement(&This->ref);
97 struct list *item, *cursor;
99 TRACE("(%p)->(ref before = %u)\n", This, refCount + 1);
103 LIST_FOR_EACH_SAFE(item, cursor, &This->assemblies)
105 ASMNAME *asmname = LIST_ENTRY(item, ASMNAME, entry);
107 list_remove(&asmname->entry);
108 IAssemblyName_Release(asmname->name);
109 HeapFree(GetProcessHeap(), 0, asmname);
112 HeapFree(GetProcessHeap(), 0, This);
118 static HRESULT WINAPI IAssemblyEnumImpl_GetNextAssembly(IAssemblyEnum *iface,
120 IAssemblyName **ppName,
123 IAssemblyEnumImpl *asmenum = impl_from_IAssemblyEnum(iface);
126 TRACE("(%p, %p, %p, %d)\n", iface, pvReserved, ppName, dwFlags);
131 asmname = LIST_ENTRY(asmenum->iter, ASMNAME, entry);
135 *ppName = asmname->name;
136 IAssemblyName_AddRef(*ppName);
138 asmenum->iter = list_next(&asmenum->assemblies, asmenum->iter);
143 static HRESULT WINAPI IAssemblyEnumImpl_Reset(IAssemblyEnum *iface)
145 IAssemblyEnumImpl *asmenum = impl_from_IAssemblyEnum(iface);
147 TRACE("(%p)\n", iface);
149 asmenum->iter = list_head(&asmenum->assemblies);
153 static HRESULT WINAPI IAssemblyEnumImpl_Clone(IAssemblyEnum *iface,
154 IAssemblyEnum **ppEnum)
156 FIXME("(%p, %p) stub!\n", iface, ppEnum);
160 static const IAssemblyEnumVtbl AssemblyEnumVtbl = {
161 IAssemblyEnumImpl_QueryInterface,
162 IAssemblyEnumImpl_AddRef,
163 IAssemblyEnumImpl_Release,
164 IAssemblyEnumImpl_GetNextAssembly,
165 IAssemblyEnumImpl_Reset,
166 IAssemblyEnumImpl_Clone
169 static void parse_name(IAssemblyName *name, int depth, LPWSTR path, LPWSTR buf)
171 WCHAR disp[MAX_PATH];
172 LPCWSTR verptr, pubkeyptr;
174 DWORD size, major_size, minor_size, build_size, revision_size;
175 WORD major, minor, build, revision;
177 static const WCHAR star[] = {'*',0};
178 static const WCHAR ss_fmt[] = {'%','s','\\','%','s',0};
179 static const WCHAR verpubkey[] = {'%','s','\\','%','s','_','_','%','s',0};
180 static const WCHAR ver_fmt[] = {'%','u','.','%','u','.','%','u','.','%','u',0};
182 WCHAR version[24]; /* strlen("65535") * 4 + 3 + 1 */
183 WCHAR token_str[TOKEN_LENGTH + 1];
184 BYTE token[BYTES_PER_TOKEN];
190 hr = IAssemblyName_GetName(name, &size, disp);
192 sprintfW(buf, ss_fmt, path, disp);
194 sprintfW(buf, ss_fmt, path, star);
198 major_size = sizeof(major);
199 IAssemblyName_GetProperty(name, ASM_NAME_MAJOR_VERSION, &major, &major_size);
201 minor_size = sizeof(minor);
202 IAssemblyName_GetProperty(name, ASM_NAME_MINOR_VERSION, &minor, &minor_size);
204 build_size = sizeof(build);
205 IAssemblyName_GetProperty(name, ASM_NAME_BUILD_NUMBER, &build, &build_size);
207 revision_size = sizeof(revision);
208 IAssemblyName_GetProperty(name, ASM_NAME_REVISION_NUMBER, &revision, &revision_size);
210 if (!major_size || !minor_size || !build_size || !revision_size) verptr = star;
213 sprintfW(version, ver_fmt, major, minor, build, revision);
217 size = sizeof(token);
218 IAssemblyName_GetProperty(name, ASM_NAME_PUBLIC_KEY_TOKEN, token, &size);
220 if (!size) pubkeyptr = star;
223 token_to_str(token, token_str);
224 pubkeyptr = token_str;
227 sprintfW(buf, verpubkey, path, verptr, pubkeyptr);
231 static int compare_assembly_names(ASMNAME *asmname1, ASMNAME *asmname2)
234 WORD version1, version2;
235 WCHAR name1[MAX_PATH], name2[MAX_PATH];
236 WCHAR token_str1[TOKEN_LENGTH + 1], token_str2[TOKEN_LENGTH + 1];
237 BYTE token1[BYTES_PER_TOKEN], token2[BYTES_PER_TOKEN];
240 size = sizeof(name1);
241 IAssemblyName_GetProperty(asmname1->name, ASM_NAME_NAME, name1, &size);
242 size = sizeof(name2);
243 IAssemblyName_GetProperty(asmname2->name, ASM_NAME_NAME, name2, &size);
245 if ((ret = strcmpiW(name1, name2))) return ret;
247 for (i = ASM_NAME_MAJOR_VERSION; i < ASM_NAME_CULTURE; i++)
249 size = sizeof(version1);
250 IAssemblyName_GetProperty(asmname1->name, i, &version1, &size);
251 size = sizeof(version2);
252 IAssemblyName_GetProperty(asmname2->name, i, &version2, &size);
254 if (version1 < version2) return -1;
255 if (version1 > version2) return 1;
258 /* FIXME: compare cultures */
260 size = sizeof(token1);
261 IAssemblyName_GetProperty(asmname1->name, ASM_NAME_PUBLIC_KEY_TOKEN, token1, &size);
262 size = sizeof(token2);
263 IAssemblyName_GetProperty(asmname2->name, ASM_NAME_PUBLIC_KEY_TOKEN, token2, &size);
265 token_to_str(token1, token_str1);
266 token_to_str(token2, token_str2);
268 if ((ret = strcmpiW(token_str1, token_str2))) return ret;
273 /* insert assembly in list preserving sort order */
274 static void insert_assembly(struct list *assemblies, ASMNAME *to_insert)
278 LIST_FOR_EACH(item, assemblies)
280 ASMNAME *name = LIST_ENTRY(item, ASMNAME, entry);
282 if (compare_assembly_names(name, to_insert) > 0)
284 list_add_before(&name->entry, &to_insert->entry);
288 list_add_tail(assemblies, &to_insert->entry);
291 static HRESULT enum_gac_assemblies(struct list *assemblies, IAssemblyName *name,
292 int depth, LPWSTR path)
294 static const WCHAR dot[] = {'.',0};
295 static const WCHAR dotdot[] = {'.','.',0};
296 static const WCHAR search_fmt[] = {'%','s','\\','*',0};
297 static const WCHAR dblunder[] = {'_','_',0};
298 static const WCHAR path_fmt[] = {'%','s','\\','%','s','\\','%','s','.','d','l','l',0};
299 static const WCHAR fmt[] = {'%','s',',',' ','V','e','r','s','i','o','n','=','%','s',',',' ',
300 'C','u','l','t','u','r','e','=','n','e','u','t','r','a','l',',',' ',
301 'P','u','b','l','i','c','K','e','y','T','o','k','e','n','=','%','s',0};
302 static const WCHAR ss_fmt[] = {'%','s','\\','%','s',0};
303 static const WCHAR v40[] = {'v','4','.','0','_'};
304 WIN32_FIND_DATAW ffd;
305 WCHAR buf[MAX_PATH], disp[MAX_PATH], asmpath[MAX_PATH];
306 static WCHAR parent[MAX_PATH];
313 parse_name(name, depth, path, buf);
315 sprintfW(buf, search_fmt, path);
317 hfind = FindFirstFileW(buf, &ffd);
318 if (hfind == INVALID_HANDLE_VALUE)
323 if (!lstrcmpW(ffd.cFileName, dot) || !lstrcmpW(ffd.cFileName, dotdot))
329 ptr = strrchrW(buf, '\\') + 1;
333 lstrcpyW(parent, ptr);
337 unsigned int prefix_len = sizeof(v40)/sizeof(WCHAR);
338 const WCHAR *token, *version = ffd.cFileName;
340 sprintfW(asmpath, path_fmt, path, ffd.cFileName, parent);
342 ptr = strstrW(ffd.cFileName, dblunder);
346 if (strlenW(ffd.cFileName) >= prefix_len &&
347 !memcmp(ffd.cFileName, v40, sizeof(v40))) version += prefix_len;
349 sprintfW(disp, fmt, parent, version, token);
351 asmname = HeapAlloc(GetProcessHeap(), 0, sizeof(ASMNAME));
358 hr = CreateAssemblyNameObject(&asmname->name, disp,
359 CANOF_PARSE_DISPLAY_NAME, NULL);
362 HeapFree(GetProcessHeap(), 0, asmname);
366 hr = IAssemblyName_SetPath(asmname->name, asmpath);
369 IAssemblyName_Release(asmname->name);
370 HeapFree(GetProcessHeap(), 0, asmname);
374 insert_assembly(assemblies, asmname);
378 sprintfW(buf, ss_fmt, path, ffd.cFileName);
379 hr = enum_gac_assemblies(assemblies, name, depth + 1, buf);
382 } while (FindNextFileW(hfind, &ffd) != 0);
388 static HRESULT enumerate_gac(IAssemblyEnumImpl *asmenum, IAssemblyName *pName)
390 static const WCHAR gac[] = {'\\','G','A','C',0};
391 static const WCHAR gac_32[] = {'\\','G','A','C','_','3','2',0};
392 static const WCHAR gac_64[] = {'\\','G','A','C','_','6','4',0};
393 static const WCHAR gac_msil[] = {'\\','G','A','C','_','M','S','I','L',0};
394 WCHAR path[MAX_PATH], buf[MAX_PATH];
400 hr = GetCachePath(ASM_CACHE_ROOT_EX, buf, &size);
405 GetNativeSystemInfo(&info);
406 if (info.u.s.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
408 strcpyW(path + size - 1, gac_64);
409 hr = enum_gac_assemblies(&asmenum->assemblies, pName, 0, path);
413 strcpyW(path + size - 1, gac_32);
414 hr = enum_gac_assemblies(&asmenum->assemblies, pName, 0, path);
418 strcpyW(path + size - 1, gac_msil);
419 hr = enum_gac_assemblies(&asmenum->assemblies, pName, 0, path);
424 hr = GetCachePath(ASM_CACHE_ROOT, buf, &size);
429 if (info.u.s.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
431 strcpyW(path + size - 1, gac_64);
432 hr = enum_gac_assemblies(&asmenum->assemblies, pName, 0, path);
436 strcpyW(path + size - 1, gac_32);
437 hr = enum_gac_assemblies(&asmenum->assemblies, pName, 0, path);
441 strcpyW(path + size - 1, gac_msil);
442 hr = enum_gac_assemblies(&asmenum->assemblies, pName, 0, path);
446 strcpyW(path + size - 1, gac);
447 hr = enum_gac_assemblies(&asmenum->assemblies, pName, 0, path);
454 /******************************************************************
455 * CreateAssemblyEnum (FUSION.@)
457 HRESULT WINAPI CreateAssemblyEnum(IAssemblyEnum **pEnum, IUnknown *pUnkReserved,
458 IAssemblyName *pName, DWORD dwFlags, LPVOID pvReserved)
460 IAssemblyEnumImpl *asmenum;
463 TRACE("(%p, %p, %p, %08x, %p)\n", pEnum, pUnkReserved,
464 pName, dwFlags, pvReserved);
469 if (dwFlags == 0 || dwFlags == ASM_CACHE_ROOT)
472 asmenum = HeapAlloc(GetProcessHeap(), 0, sizeof(IAssemblyEnumImpl));
474 return E_OUTOFMEMORY;
476 asmenum->IAssemblyEnum_iface.lpVtbl = &AssemblyEnumVtbl;
478 list_init(&asmenum->assemblies);
480 if (dwFlags & ASM_CACHE_GAC)
482 hr = enumerate_gac(asmenum, pName);
485 HeapFree(GetProcessHeap(), 0, asmenum);
490 asmenum->iter = list_head(&asmenum->assemblies);
491 *pEnum = &asmenum->IAssemblyEnum_iface;