fusion: Reorder some code to avoid memory leak (coverity).
[wine] / dlls / fusion / fusion.c
1 /*
2  * Implementation of the Fusion API
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 "fusion.h"
30 #include "wine/debug.h"
31 #include "wine/unicode.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(fusion);
34
35 /******************************************************************
36  *  ClearDownloadCache   (FUSION.@)
37  */
38 HRESULT WINAPI ClearDownloadCache(void)
39 {
40     FIXME("stub!\n");
41     return E_NOTIMPL;
42 }
43
44 /******************************************************************
45  *  CreateInstallReferenceEnum   (FUSION.@)
46  */
47 HRESULT WINAPI CreateInstallReferenceEnum(IInstallReferenceEnum **ppRefEnum,
48                                           IAssemblyName *pName, DWORD dwFlags,
49                                           LPVOID pvReserved)
50 {
51     FIXME("(%p, %p, %08x, %p) stub!\n", ppRefEnum, pName, dwFlags, pvReserved);
52     return E_NOTIMPL;
53 }
54
55 /******************************************************************
56  *  CreateApplicationContext   (FUSION.@)
57  */
58 HRESULT WINAPI CreateApplicationContext(IAssemblyName *name, void *ctx)
59 {
60     FIXME("%p, %p\n", name, ctx);
61     return E_NOTIMPL;
62 }
63
64 static HRESULT (WINAPI *pGetCORVersion)(LPWSTR pbuffer, DWORD cchBuffer,
65                                         DWORD *dwLength);
66
67 static HRESULT get_corversion(LPWSTR version, DWORD size)
68 {
69     HMODULE hmscoree;
70     HRESULT hr;
71     DWORD len;
72
73     hmscoree = LoadLibraryA("mscoree.dll");
74     if (!hmscoree)
75         return E_FAIL;
76
77     pGetCORVersion = (void *)GetProcAddress(hmscoree, "GetCORVersion");
78     if (!pGetCORVersion)
79         hr = E_FAIL;
80     else
81         hr = pGetCORVersion(version, size, &len);
82
83     FreeLibrary(hmscoree);
84     return hr;
85 }
86
87 /******************************************************************
88  *  GetCachePath   (FUSION.@)
89  */
90 HRESULT WINAPI GetCachePath(ASM_CACHE_FLAGS dwCacheFlags, LPWSTR pwzCachePath,
91                             PDWORD pcchPath)
92 {
93     static const WCHAR assembly[] = {'\\','a','s','s','e','m','b','l','y',0};
94     static const WCHAR gac[] = {'\\','G','A','C',0};
95     static const WCHAR nativeimg[] = {'N','a','t','i','v','e','I','m','a','g','e','s','_',0};
96     static const WCHAR dotnet[] = {'\\','M','i','c','r','o','s','o','f','t','.','N','E','T',0};
97 #ifdef _WIN64
98     static const WCHAR zapfmt[] = {'%','s','\\','%','s','\\','%','s','%','s','_','6','4',0};
99 #else
100     static const WCHAR zapfmt[] = {'%','s','\\','%','s','\\','%','s','%','s','_','3','2',0};
101 #endif
102     WCHAR path[MAX_PATH], windir[MAX_PATH], version[MAX_PATH];
103     DWORD len;
104     HRESULT hr = S_OK;
105
106     TRACE("(%08x, %p, %p)\n", dwCacheFlags, pwzCachePath, pcchPath);
107
108     if (!pcchPath)
109         return E_INVALIDARG;
110
111     len = GetWindowsDirectoryW(windir, MAX_PATH);
112     strcpyW(path, windir);
113
114     switch (dwCacheFlags)
115     {
116         case ASM_CACHE_ZAP:
117         {
118             hr = get_corversion(version, MAX_PATH);
119             if (FAILED(hr))
120                 return hr;
121
122             len = sprintfW(path, zapfmt, windir, assembly + 1, nativeimg, version);
123             break;
124         }
125         case ASM_CACHE_GAC:
126         {
127             strcpyW(path + len, assembly);
128             len += sizeof(assembly)/sizeof(WCHAR) - 1;
129             strcpyW(path + len, gac);
130             len += sizeof(gac)/sizeof(WCHAR) - 1;
131             break;
132         }
133         case ASM_CACHE_DOWNLOAD:
134         {
135             FIXME("Download cache not implemented\n");
136             return E_FAIL;
137         }
138         case ASM_CACHE_ROOT:
139             strcpyW(path + len, assembly);
140             len += sizeof(assembly)/sizeof(WCHAR) - 1;
141             break;
142         case ASM_CACHE_ROOT_EX:
143             strcpyW(path + len, dotnet);
144             len += sizeof(dotnet)/sizeof(WCHAR) - 1;
145             strcpyW(path + len, assembly);
146             len += sizeof(assembly)/sizeof(WCHAR) - 1;
147             break;
148         default:
149             return E_INVALIDARG;
150     }
151
152     len++;
153     if (*pcchPath <= len || !pwzCachePath)
154         hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
155     else if (pwzCachePath)
156         strcpyW(pwzCachePath, path);
157
158     *pcchPath = len;
159     return hr;
160 }