Sync with trunk (r48545)
[reactos.git] / dll / win32 / fusion / asmenum.c
1 /*
2 * IAssemblyEnum implementation
3 *
4 * Copyright 2008 James Hawkins
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include <stdarg.h>
22
23 #define COBJMACROS
24
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"
33
34 #include "wine/debug.h"
35 #include "wine/unicode.h"
36 #include "wine/list.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(fusion);
39
40 typedef struct _tagASMNAME
41 {
42 struct list entry;
43 IAssemblyName *name;
44 } ASMNAME;
45
46 typedef struct
47 {
48 const IAssemblyEnumVtbl *lpIAssemblyEnumVtbl;
49
50 struct list assemblies;
51 struct list *iter;
52 LONG ref;
53 } IAssemblyEnumImpl;
54
55 static HRESULT WINAPI IAssemblyEnumImpl_QueryInterface(IAssemblyEnum *iface,
56 REFIID riid, LPVOID *ppobj)
57 {
58 IAssemblyEnumImpl *This = (IAssemblyEnumImpl *)iface;
59
60 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
61
62 *ppobj = NULL;
63
64 if (IsEqualIID(riid, &IID_IUnknown) ||
65 IsEqualIID(riid, &IID_IAssemblyEnum))
66 {
67 IUnknown_AddRef(iface);
68 *ppobj = This;
69 return S_OK;
70 }
71
72 WARN("(%p, %s, %p): not found\n", This, debugstr_guid(riid), ppobj);
73 return E_NOINTERFACE;
74 }
75
76 static ULONG WINAPI IAssemblyEnumImpl_AddRef(IAssemblyEnum *iface)
77 {
78 IAssemblyEnumImpl *This = (IAssemblyEnumImpl *)iface;
79 ULONG refCount = InterlockedIncrement(&This->ref);
80
81 TRACE("(%p)->(ref before = %u)\n", This, refCount - 1);
82
83 return refCount;
84 }
85
86 static ULONG WINAPI IAssemblyEnumImpl_Release(IAssemblyEnum *iface)
87 {
88 IAssemblyEnumImpl *This = (IAssemblyEnumImpl *)iface;
89 ULONG refCount = InterlockedDecrement(&This->ref);
90 struct list *item, *cursor;
91
92 TRACE("(%p)->(ref before = %u)\n", This, refCount + 1);
93
94 if (!refCount)
95 {
96 LIST_FOR_EACH_SAFE(item, cursor, &This->assemblies)
97 {
98 ASMNAME *asmname = LIST_ENTRY(item, ASMNAME, entry);
99
100 list_remove(&asmname->entry);
101 IAssemblyName_Release(asmname->name);
102 HeapFree(GetProcessHeap(), 0, asmname);
103 }
104
105 HeapFree(GetProcessHeap(), 0, This);
106 }
107
108 return refCount;
109 }
110
111 static HRESULT WINAPI IAssemblyEnumImpl_GetNextAssembly(IAssemblyEnum *iface,
112 LPVOID pvReserved,
113 IAssemblyName **ppName,
114 DWORD dwFlags)
115 {
116 IAssemblyEnumImpl *asmenum = (IAssemblyEnumImpl *)iface;
117 ASMNAME *asmname;
118
119 TRACE("(%p, %p, %p, %d)\n", iface, pvReserved, ppName, dwFlags);
120
121 if (!ppName)
122 return E_INVALIDARG;
123
124 asmname = LIST_ENTRY(asmenum->iter, ASMNAME, entry);
125 if (!asmname)
126 return S_FALSE;
127
128 *ppName = asmname->name;
129 IAssemblyName_AddRef(*ppName);
130
131 asmenum->iter = list_next(&asmenum->assemblies, asmenum->iter);
132
133 return S_OK;
134 }
135
136 static HRESULT WINAPI IAssemblyEnumImpl_Reset(IAssemblyEnum *iface)
137 {
138 IAssemblyEnumImpl *asmenum = (IAssemblyEnumImpl *)iface;
139
140 TRACE("(%p)\n", iface);
141
142 asmenum->iter = list_head(&asmenum->assemblies);
143 return S_OK;
144 }
145
146 static HRESULT WINAPI IAssemblyEnumImpl_Clone(IAssemblyEnum *iface,
147 IAssemblyEnum **ppEnum)
148 {
149 FIXME("(%p, %p) stub!\n", iface, ppEnum);
150 return E_NOTIMPL;
151 }
152
153 static const IAssemblyEnumVtbl AssemblyEnumVtbl = {
154 IAssemblyEnumImpl_QueryInterface,
155 IAssemblyEnumImpl_AddRef,
156 IAssemblyEnumImpl_Release,
157 IAssemblyEnumImpl_GetNextAssembly,
158 IAssemblyEnumImpl_Reset,
159 IAssemblyEnumImpl_Clone
160 };
161
162 static void parse_name(IAssemblyName *name, int depth, LPWSTR path, LPWSTR buf)
163 {
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;
169
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};
174
175 WCHAR version[24]; /* strlen("65535") * 4 + 3 + 1 */
176 WCHAR token_str[TOKEN_LENGTH + 1];
177 BYTE token[BYTES_PER_TOKEN];
178
179 if (depth == 0)
180 {
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);
188 }
189 else if (depth == 1)
190 {
191 major_size = sizeof(major);
192 IAssemblyName_GetProperty(name, ASM_NAME_MAJOR_VERSION, &major, &major_size);
193
194 minor_size = sizeof(minor);
195 IAssemblyName_GetProperty(name, ASM_NAME_MINOR_VERSION, &minor, &minor_size);
196
197 build_size = sizeof(build);
198 IAssemblyName_GetProperty(name, ASM_NAME_BUILD_NUMBER, &build, &build_size);
199
200 revision_size = sizeof(revision);
201 IAssemblyName_GetProperty(name, ASM_NAME_REVISION_NUMBER, &revision, &revision_size);
202
203 if (!major_size || !minor_size || !build_size || !revision_size) verptr = star;
204 else
205 {
206 sprintfW(version, ver_fmt, major, minor, build, revision);
207 verptr = version;
208 }
209
210 size = sizeof(token);
211 IAssemblyName_GetProperty(name, ASM_NAME_PUBLIC_KEY_TOKEN, token, &size);
212
213 if (!size) pubkeyptr = star;
214 else
215 {
216 token_to_str(token, token_str);
217 pubkeyptr = token_str;
218 }
219
220 sprintfW(buf, verpubkey, path, verptr, pubkeyptr);
221 }
222 }
223
224 static int compare_assembly_names(ASMNAME *asmname1, ASMNAME *asmname2)
225 {
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;
232
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);
237
238 if ((ret = strcmpiW(name1, name2))) return ret;
239
240 for (i = ASM_NAME_MAJOR_VERSION; i < ASM_NAME_CULTURE; i++)
241 {
242 size = sizeof(version1);
243 IAssemblyName_GetProperty(asmname1->name, i, &version1, &size);
244 size = sizeof(version2);
245 IAssemblyName_GetProperty(asmname2->name, i, &version2, &size);
246
247 if (version1 < version2) return -1;
248 if (version1 > version2) return 1;
249 }
250
251 /* FIXME: compare cultures */
252
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);
257
258 token_to_str(token1, token_str1);
259 token_to_str(token2, token_str2);
260
261 if ((ret = strcmpiW(token_str1, token_str2))) return ret;
262
263 return 0;
264 }
265
266 /* insert assembly in list preserving sort order */
267 static void insert_assembly(struct list *assemblies, ASMNAME *to_insert)
268 {
269 struct list *item;
270
271 LIST_FOR_EACH(item, assemblies)
272 {
273 ASMNAME *name = LIST_ENTRY(item, ASMNAME, entry);
274
275 if (compare_assembly_names(name, to_insert) > 0)
276 {
277 list_add_before(&name->entry, &to_insert->entry);
278 return;
279 }
280 }
281 list_add_tail(assemblies, &to_insert->entry);
282 }
283
284 static HRESULT enum_gac_assemblies(struct list *assemblies, IAssemblyName *name,
285 int depth, LPWSTR path)
286 {
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;
294
295 static WCHAR parent[MAX_PATH];
296
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};
306
307 if (name)
308 parse_name(name, depth, path, buf);
309 else
310 sprintfW(buf, search_fmt, path);
311
312 hfind = FindFirstFileW(buf, &ffd);
313 if (hfind == INVALID_HANDLE_VALUE)
314 return S_OK;
315
316 do
317 {
318 if (!lstrcmpW(ffd.cFileName, dot) || !lstrcmpW(ffd.cFileName, dotdot))
319 continue;
320
321 if (depth == 0)
322 {
323 if (name)
324 ptr = strrchrW(buf, '\\') + 1;
325 else
326 ptr = ffd.cFileName;
327
328 sprintfW(parent, parent_fmt, ptr);
329 }
330 else if (depth == 1)
331 {
332 ptr = strstrW(ffd.cFileName, dblunder);
333 *ptr = '\0';
334 ptr += 2;
335 sprintfW(buf, fmt, ffd.cFileName, ptr);
336
337 lstrcpyW(disp, parent);
338 lstrcatW(disp, buf);
339
340 asmname = HeapAlloc(GetProcessHeap(), 0, sizeof(ASMNAME));
341 if (!asmname)
342 {
343 hr = E_OUTOFMEMORY;
344 break;
345 }
346
347 hr = CreateAssemblyNameObject(&asmname->name, disp,
348 CANOF_PARSE_DISPLAY_NAME, NULL);
349 if (FAILED(hr))
350 {
351 HeapFree(GetProcessHeap(), 0, asmname);
352 break;
353 }
354
355 insert_assembly(assemblies, asmname);
356 continue;
357 }
358
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);
364
365 FindClose(hfind);
366 return hr;
367 }
368
369 static HRESULT enumerate_gac(IAssemblyEnumImpl *asmenum, IAssemblyName *pName)
370 {
371 WCHAR path[MAX_PATH];
372 WCHAR buf[MAX_PATH];
373 HRESULT hr;
374 DWORD size;
375
376 static WCHAR under32[] = {'_','3','2',0};
377 static WCHAR msil[] = {'_','M','S','I','L',0};
378
379 size = MAX_PATH;
380 hr = GetCachePath(ASM_CACHE_GAC, buf, &size);
381 if (FAILED(hr))
382 return hr;
383
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;
389
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;
395
396 hr = enum_gac_assemblies(&asmenum->assemblies, pName, 0, buf);
397 if (FAILED(hr))
398 return hr;
399
400 return S_OK;
401 }
402
403 /******************************************************************
404 * CreateAssemblyEnum (FUSION.@)
405 */
406 HRESULT WINAPI CreateAssemblyEnum(IAssemblyEnum **pEnum, IUnknown *pUnkReserved,
407 IAssemblyName *pName, DWORD dwFlags, LPVOID pvReserved)
408 {
409 IAssemblyEnumImpl *asmenum;
410 HRESULT hr;
411
412 TRACE("(%p, %p, %p, %08x, %p)\n", pEnum, pUnkReserved,
413 pName, dwFlags, pvReserved);
414
415 if (!pEnum)
416 return E_INVALIDARG;
417
418 if (dwFlags == 0 || dwFlags == ASM_CACHE_ROOT)
419 return E_INVALIDARG;
420
421 asmenum = HeapAlloc(GetProcessHeap(), 0, sizeof(IAssemblyEnumImpl));
422 if (!asmenum)
423 return E_OUTOFMEMORY;
424
425 asmenum->lpIAssemblyEnumVtbl = &AssemblyEnumVtbl;
426 asmenum->ref = 1;
427 list_init(&asmenum->assemblies);
428
429 if (dwFlags & ASM_CACHE_GAC)
430 {
431 hr = enumerate_gac(asmenum, pName);
432 if (FAILED(hr))
433 {
434 HeapFree(GetProcessHeap(), 0, asmenum);
435 return hr;
436 }
437 }
438
439 asmenum->iter = list_head(&asmenum->assemblies);
440 *pEnum = (IAssemblyEnum *)asmenum;
441
442 return S_OK;
443 }