gdiplus: Add test for GdipGetPathGradientSurroundColorsWithCount.
[wine] / dlls / 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     IAssemblyEnum IAssemblyEnum_iface;
49
50     struct list assemblies;
51     struct list *iter;
52     LONG ref;
53 } IAssemblyEnumImpl;
54
55 static inline IAssemblyEnumImpl *impl_from_IAssemblyEnum(IAssemblyEnum *iface)
56 {
57     return CONTAINING_RECORD(iface, IAssemblyEnumImpl, IAssemblyEnum_iface);
58 }
59
60 static HRESULT WINAPI IAssemblyEnumImpl_QueryInterface(IAssemblyEnum *iface,
61                                                        REFIID riid, LPVOID *ppobj)
62 {
63     IAssemblyEnumImpl *This = impl_from_IAssemblyEnum(iface);
64
65     TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
66
67     *ppobj = NULL;
68
69     if (IsEqualIID(riid, &IID_IUnknown) ||
70         IsEqualIID(riid, &IID_IAssemblyEnum))
71     {
72         IUnknown_AddRef(iface);
73         *ppobj = This;
74         return S_OK;
75     }
76
77     WARN("(%p, %s, %p): not found\n", This, debugstr_guid(riid), ppobj);
78     return E_NOINTERFACE;
79 }
80
81 static ULONG WINAPI IAssemblyEnumImpl_AddRef(IAssemblyEnum *iface)
82 {
83     IAssemblyEnumImpl *This = impl_from_IAssemblyEnum(iface);
84     ULONG refCount = InterlockedIncrement(&This->ref);
85
86     TRACE("(%p)->(ref before = %u)\n", This, refCount - 1);
87
88     return refCount;
89 }
90
91 static ULONG WINAPI IAssemblyEnumImpl_Release(IAssemblyEnum *iface)
92 {
93     IAssemblyEnumImpl *This = impl_from_IAssemblyEnum(iface);
94     ULONG refCount = InterlockedDecrement(&This->ref);
95     struct list *item, *cursor;
96
97     TRACE("(%p)->(ref before = %u)\n", This, refCount + 1);
98
99     if (!refCount)
100     {
101         LIST_FOR_EACH_SAFE(item, cursor, &This->assemblies)
102         {
103             ASMNAME *asmname = LIST_ENTRY(item, ASMNAME, entry);
104
105             list_remove(&asmname->entry);
106             IAssemblyName_Release(asmname->name);
107             HeapFree(GetProcessHeap(), 0, asmname);
108         }
109
110         HeapFree(GetProcessHeap(), 0, This);
111     }
112
113     return refCount;
114 }
115
116 static HRESULT WINAPI IAssemblyEnumImpl_GetNextAssembly(IAssemblyEnum *iface,
117                                                         LPVOID pvReserved,
118                                                         IAssemblyName **ppName,
119                                                         DWORD dwFlags)
120 {
121     IAssemblyEnumImpl *asmenum = impl_from_IAssemblyEnum(iface);
122     ASMNAME *asmname;
123
124     TRACE("(%p, %p, %p, %d)\n", iface, pvReserved, ppName, dwFlags);
125
126     if (!ppName)
127         return E_INVALIDARG;
128
129     asmname = LIST_ENTRY(asmenum->iter, ASMNAME, entry);
130     if (!asmname)
131         return S_FALSE;
132
133     *ppName = asmname->name;
134     IAssemblyName_AddRef(*ppName);
135
136     asmenum->iter = list_next(&asmenum->assemblies, asmenum->iter);
137
138     return S_OK;
139 }
140
141 static HRESULT WINAPI IAssemblyEnumImpl_Reset(IAssemblyEnum *iface)
142 {
143     IAssemblyEnumImpl *asmenum = impl_from_IAssemblyEnum(iface);
144
145     TRACE("(%p)\n", iface);
146
147     asmenum->iter = list_head(&asmenum->assemblies);
148     return S_OK;
149 }
150
151 static HRESULT WINAPI IAssemblyEnumImpl_Clone(IAssemblyEnum *iface,
152                                                IAssemblyEnum **ppEnum)
153 {
154     FIXME("(%p, %p) stub!\n", iface, ppEnum);
155     return E_NOTIMPL;
156 }
157
158 static const IAssemblyEnumVtbl AssemblyEnumVtbl = {
159     IAssemblyEnumImpl_QueryInterface,
160     IAssemblyEnumImpl_AddRef,
161     IAssemblyEnumImpl_Release,
162     IAssemblyEnumImpl_GetNextAssembly,
163     IAssemblyEnumImpl_Reset,
164     IAssemblyEnumImpl_Clone
165 };
166
167 static void parse_name(IAssemblyName *name, int depth, LPWSTR path, LPWSTR buf)
168 {
169     WCHAR disp[MAX_PATH];
170     LPCWSTR verptr, pubkeyptr;
171     HRESULT hr;
172     DWORD size, major_size, minor_size, build_size, revision_size;
173     WORD major, minor, build, revision;
174
175     static const WCHAR star[] = {'*',0};
176     static const WCHAR ss_fmt[] = {'%','s','\\','%','s',0};
177     static const WCHAR verpubkey[] = {'%','s','\\','%','s','_','_','%','s',0};
178     static const WCHAR ver_fmt[] = {'%','u','.','%','u','.','%','u','.','%','u',0};
179
180     WCHAR version[24]; /* strlen("65535") * 4 + 3 + 1 */
181     WCHAR token_str[TOKEN_LENGTH + 1];
182     BYTE token[BYTES_PER_TOKEN];
183
184     if (depth == 0)
185     {
186         size = MAX_PATH;
187         *disp = '\0';
188         hr = IAssemblyName_GetName(name, &size, disp);
189         if (SUCCEEDED(hr))
190             sprintfW(buf, ss_fmt, path, disp);
191         else
192             sprintfW(buf, ss_fmt, path, star);
193     }
194     else if (depth == 1)
195     {
196         major_size = sizeof(major);
197         IAssemblyName_GetProperty(name, ASM_NAME_MAJOR_VERSION, &major, &major_size);
198
199         minor_size = sizeof(minor);
200         IAssemblyName_GetProperty(name, ASM_NAME_MINOR_VERSION, &minor, &minor_size);
201
202         build_size = sizeof(build);
203         IAssemblyName_GetProperty(name, ASM_NAME_BUILD_NUMBER, &build, &build_size);
204
205         revision_size = sizeof(revision);
206         IAssemblyName_GetProperty(name, ASM_NAME_REVISION_NUMBER, &revision, &revision_size);
207
208         if (!major_size || !minor_size || !build_size || !revision_size) verptr = star;
209         else
210         {
211             sprintfW(version, ver_fmt, major, minor, build, revision);
212             verptr = version;
213         }
214
215         size = sizeof(token);
216         IAssemblyName_GetProperty(name, ASM_NAME_PUBLIC_KEY_TOKEN, token, &size);
217
218         if (!size) pubkeyptr = star;
219         else
220         {
221             token_to_str(token, token_str);
222             pubkeyptr = token_str;
223         }
224
225         sprintfW(buf, verpubkey, path, verptr, pubkeyptr);
226     }
227 }
228
229 static int compare_assembly_names(ASMNAME *asmname1, ASMNAME *asmname2)
230 {
231     int ret;
232     WORD version1, version2;
233     WCHAR name1[MAX_PATH], name2[MAX_PATH];
234     WCHAR token_str1[TOKEN_LENGTH + 1], token_str2[TOKEN_LENGTH + 1];
235     BYTE token1[BYTES_PER_TOKEN], token2[BYTES_PER_TOKEN];
236     DWORD size, i;
237
238     size = sizeof(name1);
239     IAssemblyName_GetProperty(asmname1->name, ASM_NAME_NAME, name1, &size);
240     size = sizeof(name2);
241     IAssemblyName_GetProperty(asmname2->name, ASM_NAME_NAME, name2, &size);
242
243     if ((ret = strcmpiW(name1, name2))) return ret;
244
245     for (i = ASM_NAME_MAJOR_VERSION; i < ASM_NAME_CULTURE; i++)
246     {
247         size = sizeof(version1);
248         IAssemblyName_GetProperty(asmname1->name, i, &version1, &size);
249         size = sizeof(version2);
250         IAssemblyName_GetProperty(asmname2->name, i, &version2, &size);
251
252         if (version1 < version2) return -1;
253         if (version1 > version2) return 1;
254     }
255
256     /* FIXME: compare cultures */
257
258     size = sizeof(token1);
259     IAssemblyName_GetProperty(asmname1->name, ASM_NAME_PUBLIC_KEY_TOKEN, token1, &size);
260     size = sizeof(token2);
261     IAssemblyName_GetProperty(asmname2->name, ASM_NAME_PUBLIC_KEY_TOKEN, token2, &size);
262
263     token_to_str(token1, token_str1);
264     token_to_str(token2, token_str2);
265
266     if ((ret = strcmpiW(token_str1, token_str2))) return ret;
267
268     return 0;
269 }
270
271 /* insert assembly in list preserving sort order */
272 static void insert_assembly(struct list *assemblies, ASMNAME *to_insert)
273 {
274     struct list *item;
275
276     LIST_FOR_EACH(item, assemblies)
277     {
278         ASMNAME *name = LIST_ENTRY(item, ASMNAME, entry);
279
280         if (compare_assembly_names(name, to_insert) > 0)
281         {
282             list_add_before(&name->entry, &to_insert->entry);
283             return;
284         }
285     }
286     list_add_tail(assemblies, &to_insert->entry);
287 }
288
289 static HRESULT enum_gac_assemblies(struct list *assemblies, IAssemblyName *name,
290                                    int depth, LPWSTR path)
291 {
292     WIN32_FIND_DATAW ffd;
293     WCHAR buf[MAX_PATH];
294     WCHAR disp[MAX_PATH];
295     WCHAR asmpath[MAX_PATH];
296     ASMNAME *asmname;
297     HANDLE hfind;
298     LPWSTR ptr;
299     HRESULT hr = S_OK;
300
301     static WCHAR parent[MAX_PATH];
302
303     static const WCHAR dot[] = {'.',0};
304     static const WCHAR dotdot[] = {'.','.',0};
305     static const WCHAR search_fmt[] = {'%','s','\\','*',0};
306     static const WCHAR dblunder[] = {'_','_',0};
307     static const WCHAR path_fmt[] = {'%','s','\\','%','s','\\','%','s','.','d','l','l',0};
308     static const WCHAR fmt[] = {'%','s',',',' ','V','e','r','s','i','o','n','=','%','s',',',' ',
309         'C','u','l','t','u','r','e','=','n','e','u','t','r','a','l',',',' ',
310         'P','u','b','l','i','c','K','e','y','T','o','k','e','n','=','%','s',0};
311     static const WCHAR ss_fmt[] = {'%','s','\\','%','s',0};
312
313     if (name)
314         parse_name(name, depth, path, buf);
315     else
316         sprintfW(buf, search_fmt, path);
317
318     hfind = FindFirstFileW(buf, &ffd);
319     if (hfind == INVALID_HANDLE_VALUE)
320         return S_OK;
321
322     do
323     {
324         if (!lstrcmpW(ffd.cFileName, dot) || !lstrcmpW(ffd.cFileName, dotdot))
325             continue;
326
327         if (depth == 0)
328         {
329             if (name)
330                 ptr = strrchrW(buf, '\\') + 1;
331             else
332                 ptr = ffd.cFileName;
333
334             lstrcpyW(parent, ptr);
335         }
336         else if (depth == 1)
337         {
338             sprintfW(asmpath, path_fmt, path, ffd.cFileName, parent);
339
340             ptr = strstrW(ffd.cFileName, dblunder);
341             *ptr = '\0';
342             ptr += 2;
343
344             sprintfW(disp, fmt, parent, ffd.cFileName, ptr);
345
346             asmname = HeapAlloc(GetProcessHeap(), 0, sizeof(ASMNAME));
347             if (!asmname)
348             {
349                 hr = E_OUTOFMEMORY;
350                 break;
351             }
352
353             hr = CreateAssemblyNameObject(&asmname->name, disp,
354                                           CANOF_PARSE_DISPLAY_NAME, NULL);
355             if (FAILED(hr))
356             {
357                 HeapFree(GetProcessHeap(), 0, asmname);
358                 break;
359             }
360
361             hr = IAssemblyName_SetPath(asmname->name, asmpath);
362             if (FAILED(hr))
363             {
364                 IAssemblyName_Release(asmname->name);
365                 HeapFree(GetProcessHeap(), 0, asmname);
366                 break;
367             }
368
369             insert_assembly(assemblies, asmname);
370             continue;
371         }
372
373         sprintfW(buf, ss_fmt, path, ffd.cFileName);
374         hr = enum_gac_assemblies(assemblies, name, depth + 1, buf);
375         if (FAILED(hr))
376             break;
377     } while (FindNextFileW(hfind, &ffd) != 0);
378
379     FindClose(hfind);
380     return hr;
381 }
382
383 static HRESULT enumerate_gac(IAssemblyEnumImpl *asmenum, IAssemblyName *pName)
384 {
385     WCHAR path[MAX_PATH];
386     WCHAR buf[MAX_PATH];
387     HRESULT hr;
388     DWORD size;
389
390     static WCHAR under32[] = {'_','3','2',0};
391     static WCHAR msil[] = {'_','M','S','I','L',0};
392
393     size = MAX_PATH;
394     hr = GetCachePath(ASM_CACHE_GAC, buf, &size);
395     if (FAILED(hr))
396         return hr;
397
398     lstrcpyW(path, buf);
399     lstrcatW(path, under32);
400     hr = enum_gac_assemblies(&asmenum->assemblies, pName, 0, path);
401     if (FAILED(hr))
402         return hr;
403
404     lstrcpyW(path, buf);
405     lstrcatW(path, msil);
406     hr = enum_gac_assemblies(&asmenum->assemblies, pName, 0, path);
407     if (FAILED(hr))
408         return hr;
409
410     hr = enum_gac_assemblies(&asmenum->assemblies, pName, 0, buf);
411     if (FAILED(hr))
412         return hr;
413
414     return S_OK;
415 }
416
417 /******************************************************************
418  *  CreateAssemblyEnum   (FUSION.@)
419  */
420 HRESULT WINAPI CreateAssemblyEnum(IAssemblyEnum **pEnum, IUnknown *pUnkReserved,
421                                   IAssemblyName *pName, DWORD dwFlags, LPVOID pvReserved)
422 {
423     IAssemblyEnumImpl *asmenum;
424     HRESULT hr;
425
426     TRACE("(%p, %p, %p, %08x, %p)\n", pEnum, pUnkReserved,
427           pName, dwFlags, pvReserved);
428
429     if (!pEnum)
430         return E_INVALIDARG;
431
432     if (dwFlags == 0 || dwFlags == ASM_CACHE_ROOT)
433         return E_INVALIDARG;
434
435     asmenum = HeapAlloc(GetProcessHeap(), 0, sizeof(IAssemblyEnumImpl));
436     if (!asmenum)
437         return E_OUTOFMEMORY;
438
439     asmenum->IAssemblyEnum_iface.lpVtbl = &AssemblyEnumVtbl;
440     asmenum->ref = 1;
441     list_init(&asmenum->assemblies);
442
443     if (dwFlags & ASM_CACHE_GAC)
444     {
445         hr = enumerate_gac(asmenum, pName);
446         if (FAILED(hr))
447         {
448             HeapFree(GetProcessHeap(), 0, asmenum);
449             return hr;
450         }
451     }
452
453     asmenum->iter = list_head(&asmenum->assemblies);
454     *pEnum = &asmenum->IAssemblyEnum_iface;
455
456     return S_OK;
457 }