mciseq: Remove unneeded assignments.
[wine/multimedia.git] / dlls / fusion / asmenum.c
blob3706f07f511d0b3ac37eea4bb473da88acae97e3
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 ASMNAME *asmname;
291 HANDLE hfind;
292 LPWSTR ptr;
293 HRESULT hr = S_OK;
295 static WCHAR parent[MAX_PATH];
297 static const WCHAR dot[] = {'.',0};
298 static const WCHAR dotdot[] = {'.','.',0};
299 static const WCHAR search_fmt[] = {'%','s','\\','*',0};
300 static const WCHAR parent_fmt[] = {'%','s',',',' ',0};
301 static const WCHAR dblunder[] = {'_','_',0};
302 static const WCHAR fmt[] = {'V','e','r','s','i','o','n','=','%','s',',',' ',
303 'C','u','l','t','u','r','e','=','n','e','u','t','r','a','l',',',' ',
304 'P','u','b','l','i','c','K','e','y','T','o','k','e','n','=','%','s',0};
305 static const WCHAR ss_fmt[] = {'%','s','\\','%','s',0};
307 if (name)
308 parse_name(name, depth, path, buf);
309 else
310 sprintfW(buf, search_fmt, path);
312 hfind = FindFirstFileW(buf, &ffd);
313 if (hfind == INVALID_HANDLE_VALUE)
314 return S_OK;
318 if (!lstrcmpW(ffd.cFileName, dot) || !lstrcmpW(ffd.cFileName, dotdot))
319 continue;
321 if (depth == 0)
323 if (name)
324 ptr = strrchrW(buf, '\\') + 1;
325 else
326 ptr = ffd.cFileName;
328 sprintfW(parent, parent_fmt, ptr);
330 else if (depth == 1)
332 ptr = strstrW(ffd.cFileName, dblunder);
333 *ptr = '\0';
334 ptr += 2;
335 sprintfW(buf, fmt, ffd.cFileName, ptr);
337 lstrcpyW(disp, parent);
338 lstrcatW(disp, buf);
340 asmname = HeapAlloc(GetProcessHeap(), 0, sizeof(ASMNAME));
341 if (!asmname)
343 hr = E_OUTOFMEMORY;
344 break;
347 hr = CreateAssemblyNameObject(&asmname->name, disp,
348 CANOF_PARSE_DISPLAY_NAME, NULL);
349 if (FAILED(hr))
351 HeapFree(GetProcessHeap(), 0, asmname);
352 break;
355 insert_assembly(assemblies, asmname);
356 continue;
359 sprintfW(buf, ss_fmt, path, ffd.cFileName);
360 hr = enum_gac_assemblies(assemblies, name, depth + 1, buf);
361 if (FAILED(hr))
362 break;
363 } while (FindNextFileW(hfind, &ffd) != 0);
365 FindClose(hfind);
366 return hr;
369 static HRESULT enumerate_gac(IAssemblyEnumImpl *asmenum, IAssemblyName *pName)
371 WCHAR path[MAX_PATH];
372 WCHAR buf[MAX_PATH];
373 HRESULT hr;
374 DWORD size;
376 static WCHAR under32[] = {'_','3','2',0};
377 static WCHAR msil[] = {'_','M','S','I','L',0};
379 size = MAX_PATH;
380 hr = GetCachePath(ASM_CACHE_GAC, buf, &size);
381 if (FAILED(hr))
382 return hr;
384 lstrcpyW(path, buf);
385 lstrcatW(path, under32);
386 hr = enum_gac_assemblies(&asmenum->assemblies, pName, 0, path);
387 if (FAILED(hr))
388 return hr;
390 lstrcpyW(path, buf);
391 lstrcatW(path, msil);
392 hr = enum_gac_assemblies(&asmenum->assemblies, pName, 0, path);
393 if (FAILED(hr))
394 return hr;
396 hr = enum_gac_assemblies(&asmenum->assemblies, pName, 0, buf);
397 if (FAILED(hr))
398 return hr;
400 return S_OK;
403 /******************************************************************
404 * CreateAssemblyEnum (FUSION.@)
406 HRESULT WINAPI CreateAssemblyEnum(IAssemblyEnum **pEnum, IUnknown *pUnkReserved,
407 IAssemblyName *pName, DWORD dwFlags, LPVOID pvReserved)
409 IAssemblyEnumImpl *asmenum;
410 HRESULT hr;
412 TRACE("(%p, %p, %p, %08x, %p)\n", pEnum, pUnkReserved,
413 pName, dwFlags, pvReserved);
415 if (!pEnum)
416 return E_INVALIDARG;
418 if (dwFlags == 0 || dwFlags == ASM_CACHE_ROOT)
419 return E_INVALIDARG;
421 asmenum = HeapAlloc(GetProcessHeap(), 0, sizeof(IAssemblyEnumImpl));
422 if (!asmenum)
423 return E_OUTOFMEMORY;
425 asmenum->lpIAssemblyEnumVtbl = &AssemblyEnumVtbl;
426 asmenum->ref = 1;
427 list_init(&asmenum->assemblies);
429 if (dwFlags & ASM_CACHE_GAC)
431 hr = enumerate_gac(asmenum, pName);
432 if (FAILED(hr))
434 HeapFree(GetProcessHeap(), 0, asmenum);
435 return hr;
439 asmenum->iter = list_head(&asmenum->assemblies);
440 *pEnum = (IAssemblyEnum *)asmenum;
442 return S_OK;