ntdll: Reimplement bsearch to avoid redundant and possibly out of bounds comparisons.
[wine/hramrach.git] / dlls / fusion / asmcache.c
blobf35de7f142a43aa8f0646406eefbc8c7bc3e0d4a
1 /*
2 * IAssemblyCache 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>
22 #include <stdio.h>
24 #define COBJMACROS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "winver.h"
30 #include "wincrypt.h"
31 #include "winreg.h"
32 #include "shlwapi.h"
33 #include "dbghelp.h"
34 #include "ole2.h"
35 #include "fusion.h"
36 #include "corerror.h"
38 #include "fusionpriv.h"
39 #include "wine/debug.h"
40 #include "wine/unicode.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(fusion);
44 static BOOL create_full_path(LPCWSTR path)
46 LPWSTR new_path;
47 BOOL ret = TRUE;
48 int len;
50 new_path = HeapAlloc(GetProcessHeap(), 0, (strlenW(path) + 1) * sizeof(WCHAR));
51 if (!new_path)
52 return FALSE;
54 strcpyW(new_path, path);
56 while ((len = strlenW(new_path)) && new_path[len - 1] == '\\')
57 new_path[len - 1] = 0;
59 while (!CreateDirectoryW(new_path, NULL))
61 LPWSTR slash;
62 DWORD last_error = GetLastError();
64 if(last_error == ERROR_ALREADY_EXISTS)
65 break;
67 if(last_error != ERROR_PATH_NOT_FOUND)
69 ret = FALSE;
70 break;
73 if(!(slash = strrchrW(new_path, '\\')))
75 ret = FALSE;
76 break;
79 len = slash - new_path;
80 new_path[len] = 0;
81 if(!create_full_path(new_path))
83 ret = FALSE;
84 break;
87 new_path[len] = '\\';
90 HeapFree(GetProcessHeap(), 0, new_path);
91 return ret;
94 static BOOL get_assembly_directory(LPWSTR dir, DWORD size)
96 static const WCHAR gac[] =
97 {'\\','a','s','s','e','m','b','l','y','\\','G','A','C','_','M','S','I','L',0};
99 FIXME("Ignoring assembly architecture\n");
101 GetWindowsDirectoryW(dir, size);
102 strcatW(dir, gac);
103 return TRUE;
106 /* IAssemblyCache */
108 typedef struct {
109 const IAssemblyCacheVtbl *lpIAssemblyCacheVtbl;
111 LONG ref;
112 } IAssemblyCacheImpl;
114 static HRESULT WINAPI IAssemblyCacheImpl_QueryInterface(IAssemblyCache *iface,
115 REFIID riid, LPVOID *ppobj)
117 IAssemblyCacheImpl *This = (IAssemblyCacheImpl *)iface;
119 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
121 *ppobj = NULL;
123 if (IsEqualIID(riid, &IID_IUnknown) ||
124 IsEqualIID(riid, &IID_IAssemblyCache))
126 IUnknown_AddRef(iface);
127 *ppobj = This;
128 return S_OK;
131 WARN("(%p, %s, %p): not found\n", This, debugstr_guid(riid), ppobj);
132 return E_NOINTERFACE;
135 static ULONG WINAPI IAssemblyCacheImpl_AddRef(IAssemblyCache *iface)
137 IAssemblyCacheImpl *This = (IAssemblyCacheImpl *)iface;
138 ULONG refCount = InterlockedIncrement(&This->ref);
140 TRACE("(%p)->(ref before = %u)\n", This, refCount - 1);
142 return refCount;
145 static ULONG WINAPI IAssemblyCacheImpl_Release(IAssemblyCache *iface)
147 IAssemblyCacheImpl *This = (IAssemblyCacheImpl *)iface;
148 ULONG refCount = InterlockedDecrement(&This->ref);
150 TRACE("(%p)->(ref before = %u)\n", This, refCount + 1);
152 if (!refCount)
153 HeapFree(GetProcessHeap(), 0, This);
155 return refCount;
158 static HRESULT WINAPI IAssemblyCacheImpl_UninstallAssembly(IAssemblyCache *iface,
159 DWORD dwFlags,
160 LPCWSTR pszAssemblyName,
161 LPCFUSION_INSTALL_REFERENCE pRefData,
162 ULONG *pulDisposition)
164 FIXME("(%p, %d, %s, %p, %p) stub!\n", iface, dwFlags,
165 debugstr_w(pszAssemblyName), pRefData, pulDisposition);
167 return E_NOTIMPL;
170 static HRESULT WINAPI IAssemblyCacheImpl_QueryAssemblyInfo(IAssemblyCache *iface,
171 DWORD dwFlags,
172 LPCWSTR pszAssemblyName,
173 ASSEMBLY_INFO *pAsmInfo)
175 IAssemblyName *asmname, *next = NULL;
176 IAssemblyEnum *asmenum = NULL;
177 HRESULT hr;
179 TRACE("(%p, %d, %s, %p)\n", iface, dwFlags,
180 debugstr_w(pszAssemblyName), pAsmInfo);
182 if (pAsmInfo)
184 if (pAsmInfo->cbAssemblyInfo == 0)
185 pAsmInfo->cbAssemblyInfo = sizeof(ASSEMBLY_INFO);
186 else if (pAsmInfo->cbAssemblyInfo != sizeof(ASSEMBLY_INFO))
187 return E_INVALIDARG;
190 hr = CreateAssemblyNameObject(&asmname, pszAssemblyName,
191 CANOF_PARSE_DISPLAY_NAME, NULL);
192 if (FAILED(hr))
193 return hr;
195 hr = CreateAssemblyEnum(&asmenum, NULL, asmname, ASM_CACHE_GAC, NULL);
196 if (FAILED(hr))
197 goto done;
199 hr = IAssemblyEnum_GetNextAssembly(asmenum, NULL, &next, 0);
200 if (hr == S_FALSE)
202 hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
203 goto done;
206 if (!pAsmInfo)
207 goto done;
209 pAsmInfo->dwAssemblyFlags = ASSEMBLYINFO_FLAG_INSTALLED;
211 done:
212 IAssemblyName_Release(asmname);
213 if (next) IAssemblyName_Release(next);
214 if (asmenum) IAssemblyEnum_Release(asmenum);
216 return hr;
219 static HRESULT WINAPI IAssemblyCacheImpl_CreateAssemblyCacheItem(IAssemblyCache *iface,
220 DWORD dwFlags,
221 PVOID pvReserved,
222 IAssemblyCacheItem **ppAsmItem,
223 LPCWSTR pszAssemblyName)
225 FIXME("(%p, %d, %p, %p, %s) stub!\n", iface, dwFlags, pvReserved,
226 ppAsmItem, debugstr_w(pszAssemblyName));
228 return E_NOTIMPL;
231 static HRESULT WINAPI IAssemblyCacheImpl_CreateAssemblyScavenger(IAssemblyCache *iface,
232 IUnknown **ppUnkReserved)
234 FIXME("(%p, %p) stub!\n", iface, ppUnkReserved);
235 return E_NOTIMPL;
238 static HRESULT WINAPI IAssemblyCacheImpl_InstallAssembly(IAssemblyCache *iface,
239 DWORD dwFlags,
240 LPCWSTR pszManifestFilePath,
241 LPCFUSION_INSTALL_REFERENCE pRefData)
243 static const WCHAR format[] =
244 {'%','s','\\','%','s','\\','%','s','_','_','%','s','\\',0};
246 ASSEMBLY *assembly;
247 LPWSTR filename;
248 LPWSTR name = NULL;
249 LPWSTR token = NULL;
250 LPWSTR version = NULL;
251 LPWSTR asmpath = NULL;
252 WCHAR path[MAX_PATH];
253 WCHAR asmdir[MAX_PATH];
254 LPWSTR ext;
255 HRESULT hr;
257 static const WCHAR ext_exe[] = {'.','e','x','e',0};
258 static const WCHAR ext_dll[] = {'.','d','l','l',0};
260 TRACE("(%p, %d, %s, %p)\n", iface, dwFlags,
261 debugstr_w(pszManifestFilePath), pRefData);
263 if (!pszManifestFilePath || !*pszManifestFilePath)
264 return E_INVALIDARG;
266 if (!(ext = strrchrW(pszManifestFilePath, '.')))
267 return HRESULT_FROM_WIN32(ERROR_INVALID_NAME);
269 if (lstrcmpiW(ext, ext_exe) && lstrcmpiW(ext, ext_dll))
270 return HRESULT_FROM_WIN32(ERROR_INVALID_NAME);
272 if (GetFileAttributesW(pszManifestFilePath) == INVALID_FILE_ATTRIBUTES)
273 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
275 hr = assembly_create(&assembly, pszManifestFilePath);
276 if (FAILED(hr))
278 hr = COR_E_ASSEMBLYEXPECTED;
279 goto done;
282 hr = assembly_get_name(assembly, &name);
283 if (FAILED(hr))
284 goto done;
286 hr = assembly_get_pubkey_token(assembly, &token);
287 if (FAILED(hr))
288 goto done;
290 hr = assembly_get_version(assembly, &version);
291 if (FAILED(hr))
292 goto done;
294 get_assembly_directory(asmdir, MAX_PATH);
296 sprintfW(path, format, asmdir, name, version, token);
298 create_full_path(path);
300 hr = assembly_get_path(assembly, &asmpath);
301 if (FAILED(hr))
302 goto done;
304 filename = PathFindFileNameW(asmpath);
306 strcatW(path, filename);
307 if (!CopyFileW(asmpath, path, FALSE))
308 hr = HRESULT_FROM_WIN32(GetLastError());
310 done:
311 HeapFree(GetProcessHeap(), 0, name);
312 HeapFree(GetProcessHeap(), 0, token);
313 HeapFree(GetProcessHeap(), 0, version);
314 HeapFree(GetProcessHeap(), 0, asmpath);
315 assembly_release(assembly);
316 return hr;
319 static const IAssemblyCacheVtbl AssemblyCacheVtbl = {
320 IAssemblyCacheImpl_QueryInterface,
321 IAssemblyCacheImpl_AddRef,
322 IAssemblyCacheImpl_Release,
323 IAssemblyCacheImpl_UninstallAssembly,
324 IAssemblyCacheImpl_QueryAssemblyInfo,
325 IAssemblyCacheImpl_CreateAssemblyCacheItem,
326 IAssemblyCacheImpl_CreateAssemblyScavenger,
327 IAssemblyCacheImpl_InstallAssembly
330 /******************************************************************
331 * CreateAssemblyCache (FUSION.@)
333 HRESULT WINAPI CreateAssemblyCache(IAssemblyCache **ppAsmCache, DWORD dwReserved)
335 IAssemblyCacheImpl *cache;
337 TRACE("(%p, %d)\n", ppAsmCache, dwReserved);
339 if (!ppAsmCache)
340 return E_INVALIDARG;
342 *ppAsmCache = NULL;
344 cache = HeapAlloc(GetProcessHeap(), 0, sizeof(IAssemblyCacheImpl));
345 if (!cache)
346 return E_OUTOFMEMORY;
348 cache->lpIAssemblyCacheVtbl = &AssemblyCacheVtbl;
349 cache->ref = 1;
351 *ppAsmCache = (IAssemblyCache *)cache;
353 return S_OK;
356 /* IAssemblyCacheItem */
358 typedef struct {
359 const IAssemblyCacheItemVtbl *lpIAssemblyCacheItemVtbl;
361 LONG ref;
362 } IAssemblyCacheItemImpl;
364 static HRESULT WINAPI IAssemblyCacheItemImpl_QueryInterface(IAssemblyCacheItem *iface,
365 REFIID riid, LPVOID *ppobj)
367 IAssemblyCacheItemImpl *This = (IAssemblyCacheItemImpl *)iface;
369 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
371 *ppobj = NULL;
373 if (IsEqualIID(riid, &IID_IUnknown) ||
374 IsEqualIID(riid, &IID_IAssemblyCacheItem))
376 IUnknown_AddRef(iface);
377 *ppobj = This;
378 return S_OK;
381 WARN("(%p, %s, %p): not found\n", This, debugstr_guid(riid), ppobj);
382 return E_NOINTERFACE;
385 static ULONG WINAPI IAssemblyCacheItemImpl_AddRef(IAssemblyCacheItem *iface)
387 IAssemblyCacheItemImpl *This = (IAssemblyCacheItemImpl *)iface;
388 ULONG refCount = InterlockedIncrement(&This->ref);
390 TRACE("(%p)->(ref before = %u)\n", This, refCount - 1);
392 return refCount;
395 static ULONG WINAPI IAssemblyCacheItemImpl_Release(IAssemblyCacheItem *iface)
397 IAssemblyCacheItemImpl *This = (IAssemblyCacheItemImpl *)iface;
398 ULONG refCount = InterlockedDecrement(&This->ref);
400 TRACE("(%p)->(ref before = %u)\n", This, refCount + 1);
402 if (!refCount)
403 HeapFree(GetProcessHeap(), 0, This);
405 return refCount;
408 static HRESULT WINAPI IAssemblyCacheItemImpl_CreateStream(IAssemblyCacheItem *iface,
409 DWORD dwFlags,
410 LPCWSTR pszStreamName,
411 DWORD dwFormat,
412 DWORD dwFormatFlags,
413 IStream **ppIStream,
414 ULARGE_INTEGER *puliMaxSize)
416 FIXME("(%p, %d, %s, %d, %d, %p, %p) stub!\n", iface, dwFlags,
417 debugstr_w(pszStreamName), dwFormat, dwFormatFlags, ppIStream, puliMaxSize);
419 return E_NOTIMPL;
422 static HRESULT WINAPI IAssemblyCacheItemImpl_Commit(IAssemblyCacheItem *iface,
423 DWORD dwFlags,
424 ULONG *pulDisposition)
426 FIXME("(%p, %d, %p) stub!\n", iface, dwFlags, pulDisposition);
427 return E_NOTIMPL;
430 static HRESULT WINAPI IAssemblyCacheItemImpl_AbortItem(IAssemblyCacheItem *iface)
432 FIXME("(%p) stub!\n", iface);
433 return E_NOTIMPL;
436 static const IAssemblyCacheItemVtbl AssemblyCacheItemVtbl = {
437 IAssemblyCacheItemImpl_QueryInterface,
438 IAssemblyCacheItemImpl_AddRef,
439 IAssemblyCacheItemImpl_Release,
440 IAssemblyCacheItemImpl_CreateStream,
441 IAssemblyCacheItemImpl_Commit,
442 IAssemblyCacheItemImpl_AbortItem