msi: Check for return value of GetUserName (Coverity).
[wine] / dlls / msi / registry.c
1 /*
2  * Implementation of the Microsoft Installer (msi.dll)
3  *
4  * Copyright 2005 Mike McCormack for CodeWeavers
5  * Copyright 2005 Aric Stewart for CodeWeavers
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include <stdarg.h>
23
24 #define COBJMACROS
25 #define NONAMELESSUNION
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winreg.h"
30 #include "winnls.h"
31 #include "shlwapi.h"
32 #include "wine/debug.h"
33 #include "msi.h"
34 #include "msipriv.h"
35 #include "wincrypt.h"
36 #include "wine/unicode.h"
37 #include "winver.h"
38 #include "winuser.h"
39 #include "sddl.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(msi);
42
43 /* 
44  * This module will be all the helper functions for registry access by the
45  * installer bits. 
46  */
47
48 static const WCHAR szUserDataFeatures_fmt[] = {
49     'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
50     'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
51     'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
52     '%','s','\\','P','r','o','d','u','c','t','s','\\','%','s','\\','F','e','a','t','u','r','e','s',0};
53
54 static const WCHAR szUserDataComp_fmt[] = {
55     'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
56     'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
57     'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
58     '%','s','\\','C','o','m','p','o','n','e','n','t','s','\\','%','s',0};
59
60 static const WCHAR szUserDataComponents_fmt[] = {
61     'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
62     'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
63     'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
64     '%','s','\\','C','o','m','p','o','n','e','n','t','s',0};
65
66 static const WCHAR szUserDataProd_fmt[] = {
67     'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
68     'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
69     'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
70     '%','s','\\','P','r','o','d','u','c','t','s','\\','%','s',0};
71
72 static const WCHAR szUserDataProducts_fmt[] = {
73     'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
74     'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
75     'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
76     '%','s','\\','P','r','o','d','u','c','t','s',0};
77
78 static const WCHAR szUserDataPatch_fmt[] = {
79     'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
80     'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
81     'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
82     '%','s','\\','P','a','t','c','h','e','s','\\','%','s',0};
83
84 static const WCHAR szUserDataPatches_fmt[] = {
85     'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
86     'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
87     'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
88     '%','s','\\','P','a','t','c','h','e','s',0};
89
90 static const WCHAR szUserDataProductPatches_fmt[] = {
91     'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
92     'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
93     'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
94     '%','s','\\','P','r','o','d','u','c','t','s','\\','%','s','\\','P','a','t','c','h','e','s',0};
95
96 static const WCHAR szInstallProperties_fmt[] = {
97     'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
98     'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
99     'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
100     '%','s','\\','P','r','o','d','u','c','t','s','\\','%','s','\\',
101     'I','n','s','t','a','l','l','P','r','o','p','e','r','t','i','e','s',0};
102
103 static const WCHAR szInstaller_LocalManaged_fmt[] = {
104     'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
105     'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
106     'I','n','s','t','a','l','l','e','r','\\','M','a','n','a','g','e','d','\\','%','s','\\',
107     'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s',0};
108
109 static const WCHAR szInstaller_LocalManagedProd_fmt[] = {
110     'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
111     'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
112     'I','n','s','t','a','l','l','e','r','\\','M','a','n','a','g','e','d','\\','%','s','\\',
113     'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s','\\','%','s',0};
114
115 static const WCHAR szInstaller_LocalManagedFeat_fmt[] = {
116     'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
117     'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
118     'I','n','s','t','a','l','l','e','r','\\','M','a','n','a','g','e','d','\\','%','s','\\',
119     'I','n','s','t','a','l','l','e','r','\\','F','e','a','t','u','r','e','s','\\','%','s',0};
120
121 static const WCHAR szInstaller_Products[] = {
122     'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
123     'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
124     'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s',0};
125
126 static const WCHAR szInstaller_Patches[] = {
127     'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
128     'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
129     'I','n','s','t','a','l','l','e','r','\\','P','a','t','c','h','e','s',0};
130
131 static const WCHAR szInstaller_Components[] = {
132     'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
133     'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
134     'I','n','s','t','a','l','l','e','r','\\','C','o','m','p','o','n','e','n','t','s',0};
135
136 static const WCHAR szInstaller_LocalClassesProducts[] = {
137     'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
138     'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s',0};
139
140 static const WCHAR szInstaller_LocalClassesFeatures[] = {
141     'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
142     'I','n','s','t','a','l','l','e','r','\\','F','e','a','t','u','r','e','s',0};
143
144 static const WCHAR szInstaller_LocalClassesProd[] = {
145     'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
146     'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s','\\',0};
147
148 static const WCHAR szInstaller_LocalClassesFeat[] = {
149     'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
150     'I','n','s','t','a','l','l','e','r','\\','F','e','a','t','u','r','e','s','\\',0};
151
152 static const WCHAR szInstaller_ClassesUpgradeCode[] = {
153     'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
154     'I','n','s','t','a','l','l','e','r','\\','U','p','g','r','a','d','e','C','o','d','e','s','\\',0};
155
156 static const WCHAR szInstaller_ClassesUpgradeCodes[] = {
157     'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
158     'I','n','s','t','a','l','l','e','r','\\','U','p','g','r','a','d','e','C','o','d','e','s',0};
159
160 static const WCHAR szInstaller_Features[] = {
161     'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
162     'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
163     'I','n','s','t','a','l','l','e','r','\\','F','e','a','t','u','r','e','s','\\',0};
164
165 static const WCHAR szInstaller_UpgradeCodes[] = {
166     'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
167     'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
168     'I','n','s','t','a','l','l','e','r','\\','U','p','g','r','a','d','e','C','o','d','e','s','\\',0};
169
170 static const WCHAR szInstaller_UserUpgradeCodes[] = {
171     'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
172     'I','n','s','t','a','l','l','e','r','\\','U','p','g','r','a','d','e','C','o','d','e','s','\\',0};
173
174 static const WCHAR szUninstall[] = {
175     'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
176     'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
177     'U','n','i','n','s','t','a','l','l','\\',0};
178
179 static const WCHAR szUninstall_32node[] = {
180     'S','o','f','t','w','a','r','e','\\','W','o','w','6','4','3','2','N','o','d','e','\\',
181     'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
182     'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','U','n','i','n','s','t','a','l','l','\\',0};
183
184 static const WCHAR szUserComponents[] = {
185     'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
186     'I','n','s','t','a','l','l','e','r','\\','C','o','m','p','o','n','e','n','t','s','\\',0};
187
188 static const WCHAR szUserFeatures[] = {
189     'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
190     'I','n','s','t','a','l','l','e','r','\\','F','e','a','t','u','r','e','s','\\',0};
191
192 static const WCHAR szUserProducts[] = {
193     'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
194     'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s','\\',0};
195
196 static const WCHAR szUserPatches[] = {
197     'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
198     'I','n','s','t','a','l','l','e','r','\\','P','a','t','c','h','e','s','\\',0};
199
200 BOOL unsquash_guid(LPCWSTR in, LPWSTR out)
201 {
202     DWORD i,n=0;
203
204     if (lstrlenW(in) != 32)
205         return FALSE;
206
207     out[n++]='{';
208     for(i=0; i<8; i++)
209         out[n++] = in[7-i];
210     out[n++]='-';
211     for(i=0; i<4; i++)
212         out[n++] = in[11-i];
213     out[n++]='-';
214     for(i=0; i<4; i++)
215         out[n++] = in[15-i];
216     out[n++]='-';
217     for(i=0; i<2; i++)
218     {
219         out[n++] = in[17+i*2];
220         out[n++] = in[16+i*2];
221     }
222     out[n++]='-';
223     for( ; i<8; i++)
224     {
225         out[n++] = in[17+i*2];
226         out[n++] = in[16+i*2];
227     }
228     out[n++]='}';
229     out[n]=0;
230     return TRUE;
231 }
232
233 BOOL squash_guid(LPCWSTR in, LPWSTR out)
234 {
235     DWORD i,n=1;
236     GUID guid;
237
238     out[0] = 0;
239
240     if (FAILED(CLSIDFromString((LPCOLESTR)in, &guid)))
241         return FALSE;
242
243     for(i=0; i<8; i++)
244         out[7-i] = in[n++];
245     n++;
246     for(i=0; i<4; i++)
247         out[11-i] = in[n++];
248     n++;
249     for(i=0; i<4; i++)
250         out[15-i] = in[n++];
251     n++;
252     for(i=0; i<2; i++)
253     {
254         out[17+i*2] = in[n++];
255         out[16+i*2] = in[n++];
256     }
257     n++;
258     for( ; i<8; i++)
259     {
260         out[17+i*2] = in[n++];
261         out[16+i*2] = in[n++];
262     }
263     out[32]=0;
264     return TRUE;
265 }
266
267
268 /* tables for encoding and decoding base85 */
269 static const unsigned char table_dec85[0x80] = {
270 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
271 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
272 0xff,0x00,0xff,0xff,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0xff,
273 0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14,0x15,0xff,0xff,0xff,0x16,0xff,0x17,
274 0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,
275 0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x31,0x32,0x33,0xff,0x34,0x35,0x36,
276 0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,0x40,0x41,0x42,0x43,0x44,0x45,0x46,
277 0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,0x50,0x51,0x52,0xff,0x53,0x54,0xff,
278 };
279
280 static const char table_enc85[] =
281 "!$%&'()*+,-.0123456789=?@ABCDEFGHIJKLMNO"
282 "PQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwx"
283 "yz{}~";
284
285 /*
286  *  Converts a base85 encoded guid into a GUID pointer
287  *  Base85 encoded GUIDs should be 20 characters long.
288  *
289  *  returns TRUE if successful, FALSE if not
290  */
291 BOOL decode_base85_guid( LPCWSTR str, GUID *guid )
292 {
293     DWORD i, val = 0, base = 1, *p;
294
295     if (!str)
296         return FALSE;
297
298     p = (DWORD*) guid;
299     for( i=0; i<20; i++ )
300     {
301         if( (i%5) == 0 )
302         {
303             val = 0;
304             base = 1;
305         }
306         val += table_dec85[str[i]] * base;
307         if( str[i] >= 0x80 )
308             return FALSE;
309         if( table_dec85[str[i]] == 0xff )
310             return FALSE;
311         if( (i%5) == 4 )
312             p[i/5] = val;
313         base *= 85;
314     }
315     return TRUE;
316 }
317
318 /*
319  *  Encodes a base85 guid given a GUID pointer
320  *  Caller should provide a 21 character buffer for the encoded string.
321  *
322  *  returns TRUE if successful, FALSE if not
323  */
324 BOOL encode_base85_guid( GUID *guid, LPWSTR str )
325 {
326     unsigned int x, *p, i;
327
328     p = (unsigned int*) guid;
329     for( i=0; i<4; i++ )
330     {
331         x = p[i];
332         *str++ = table_enc85[x%85];
333         x = x/85;
334         *str++ = table_enc85[x%85];
335         x = x/85;
336         *str++ = table_enc85[x%85];
337         x = x/85;
338         *str++ = table_enc85[x%85];
339         x = x/85;
340         *str++ = table_enc85[x%85];
341     }
342     *str = 0;
343
344     return TRUE;
345 }
346
347 DWORD msi_version_str_to_dword(LPCWSTR p)
348 {
349     DWORD major, minor = 0, build = 0, version = 0;
350
351     if (!p)
352         return version;
353
354     major = atoiW(p);
355
356     p = strchrW(p, '.');
357     if (p)
358     {
359         minor = atoiW(p+1);
360         p = strchrW(p+1, '.');
361         if (p)
362             build = atoiW(p+1);
363     }
364
365     return MAKELONG(build, MAKEWORD(minor, major));
366 }
367
368 LONG msi_reg_set_val_str( HKEY hkey, LPCWSTR name, LPCWSTR value )
369 {
370     DWORD len;
371     if (!value) value = szEmpty;
372     len = (lstrlenW(value) + 1) * sizeof (WCHAR);
373     return RegSetValueExW( hkey, name, 0, REG_SZ, (const BYTE *)value, len );
374 }
375
376 LONG msi_reg_set_val_multi_str( HKEY hkey, LPCWSTR name, LPCWSTR value )
377 {
378     LPCWSTR p = value;
379     while (*p) p += lstrlenW(p) + 1;
380     return RegSetValueExW( hkey, name, 0, REG_MULTI_SZ,
381                            (const BYTE *)value, (p + 1 - value) * sizeof(WCHAR) );
382 }
383
384 LONG msi_reg_set_val_dword( HKEY hkey, LPCWSTR name, DWORD val )
385 {
386     return RegSetValueExW( hkey, name, 0, REG_DWORD, (LPBYTE)&val, sizeof (DWORD) );
387 }
388
389 LONG msi_reg_set_subkey_val( HKEY hkey, LPCWSTR path, LPCWSTR name, LPCWSTR val )
390 {
391     HKEY hsubkey = 0;
392     LONG r;
393
394     r = RegCreateKeyW( hkey, path, &hsubkey );
395     if (r != ERROR_SUCCESS)
396         return r;
397     r = msi_reg_set_val_str( hsubkey, name, val );
398     RegCloseKey( hsubkey );
399     return r;
400 }
401
402 LPWSTR msi_reg_get_val_str( HKEY hkey, LPCWSTR name )
403 {
404     DWORD len = 0;
405     LPWSTR val;
406     LONG r;
407
408     r = RegQueryValueExW(hkey, name, NULL, NULL, NULL, &len);
409     if (r != ERROR_SUCCESS)
410         return NULL;
411
412     len += sizeof (WCHAR);
413     val = msi_alloc( len );
414     if (!val)
415         return NULL;
416     val[0] = 0;
417     RegQueryValueExW(hkey, name, NULL, NULL, (LPBYTE) val, &len);
418     return val;
419 }
420
421 BOOL msi_reg_get_val_dword( HKEY hkey, LPCWSTR name, DWORD *val)
422 {
423     DWORD type, len = sizeof (DWORD);
424     LONG r = RegQueryValueExW(hkey, name, NULL, &type, (LPBYTE) val, &len);
425     return r == ERROR_SUCCESS && type == REG_DWORD;
426 }
427
428 static WCHAR *get_user_sid(void)
429 {
430     HANDLE token;
431     DWORD size = 256;
432     TOKEN_USER *user;
433     WCHAR *ret;
434
435     if (!OpenProcessToken( GetCurrentProcess(), TOKEN_QUERY, &token )) return NULL;
436     if (!(user = msi_alloc( size )))
437     {
438         CloseHandle( token );
439         return NULL;
440     }
441     if (!GetTokenInformation( token, TokenUser, user, size, &size ))
442     {
443         msi_free( user );
444         if (GetLastError() != ERROR_INSUFFICIENT_BUFFER || !(user = msi_alloc( size )))
445         {
446             CloseHandle( token );
447             return NULL;
448         }
449         GetTokenInformation( token, TokenUser, user, size, &size );
450     }
451     CloseHandle( token );
452     if (!ConvertSidToStringSidW( user->User.Sid, &ret ))
453     {
454         msi_free( user );
455         return NULL;
456     }
457     msi_free( user );
458     return ret;
459 }
460
461 UINT MSIREG_OpenUninstallKey(const WCHAR *product, enum platform platform, HKEY *key, BOOL create)
462 {
463     WCHAR keypath[0x200];
464
465     TRACE("%s\n", debugstr_w(product));
466
467     if (is_64bit && platform == PLATFORM_INTEL)
468     {
469         strcpyW(keypath, szUninstall_32node);
470         strcatW(keypath, product);
471     }
472     else
473     {
474         strcpyW(keypath, szUninstall);
475         strcatW(keypath, product);
476     }
477     if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, KEY_ALL_ACCESS, NULL, key, NULL);
478     return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, KEY_ALL_ACCESS, key);
479 }
480
481 UINT MSIREG_DeleteUninstallKey(const WCHAR *product, enum platform platform)
482 {
483     WCHAR keypath[0x200];
484
485     TRACE("%s\n", debugstr_w(product));
486
487     if (is_64bit && platform == PLATFORM_INTEL)
488     {
489         strcpyW(keypath, szUninstall_32node);
490         strcatW(keypath, product);
491     }
492     else
493     {
494         strcpyW(keypath, szUninstall);
495         strcatW(keypath, product);
496     }
497     return RegDeleteTreeW(HKEY_LOCAL_MACHINE, keypath);
498 }
499
500 UINT MSIREG_OpenProductKey(LPCWSTR szProduct, LPCWSTR szUserSid, MSIINSTALLCONTEXT context, HKEY *key, BOOL create)
501 {
502     LPWSTR usersid = NULL;
503     HKEY root = HKEY_LOCAL_MACHINE;
504     REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
505     WCHAR squished_pc[GUID_SIZE], keypath[MAX_PATH];
506
507     if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
508     TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
509
510     if (context == MSIINSTALLCONTEXT_MACHINE)
511     {
512         strcpyW(keypath, szInstaller_LocalClassesProd);
513         strcatW(keypath, squished_pc);
514     }
515     else if (context == MSIINSTALLCONTEXT_USERUNMANAGED)
516     {
517         root = HKEY_CURRENT_USER;
518         strcpyW(keypath, szUserProducts);
519         strcatW(keypath, squished_pc);
520     }
521     else
522     {
523         if (!szUserSid)
524         {
525             if (!(usersid = get_user_sid()))
526             {
527                 ERR("Failed to retrieve user SID\n");
528                 return ERROR_FUNCTION_FAILED;
529             }
530             szUserSid = usersid;
531         }
532         sprintfW(keypath, szInstaller_LocalManagedProd_fmt, szUserSid, squished_pc);
533         LocalFree(usersid);
534     }
535     if (create) return RegCreateKeyExW(root, keypath, 0, NULL, 0, access, NULL, key, NULL);
536     return RegOpenKeyExW(root, keypath, 0, access, key);
537 }
538
539 UINT MSIREG_DeleteUserProductKey(LPCWSTR szProduct)
540 {
541     WCHAR squished_pc[GUID_SIZE], keypath[0x200];
542
543     if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
544     TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
545
546     strcpyW(keypath, szUserProducts);
547     strcatW(keypath, squished_pc);
548     return RegDeleteTreeW(HKEY_CURRENT_USER, keypath);
549 }
550
551 UINT MSIREG_OpenUserPatchesKey(LPCWSTR szPatch, HKEY *key, BOOL create)
552 {
553     WCHAR squished_pc[GUID_SIZE], keypath[0x200];
554
555     if (!squash_guid(szPatch, squished_pc)) return ERROR_FUNCTION_FAILED;
556     TRACE("%s squished %s\n", debugstr_w(szPatch), debugstr_w(squished_pc));
557
558     strcpyW(keypath, szUserPatches);
559     strcatW(keypath, squished_pc);
560
561     if (create) return RegCreateKeyW(HKEY_CURRENT_USER, keypath, key);
562     return RegOpenKeyW(HKEY_CURRENT_USER, keypath, key);
563 }
564
565 UINT MSIREG_OpenFeaturesKey(LPCWSTR szProduct, MSIINSTALLCONTEXT context, HKEY *key, BOOL create)
566 {
567     LPWSTR usersid;
568     HKEY root = HKEY_LOCAL_MACHINE;
569     REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
570     WCHAR squished_pc[GUID_SIZE], keypath[MAX_PATH];
571
572     if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
573     TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
574
575     if (context == MSIINSTALLCONTEXT_MACHINE)
576     {
577         strcpyW(keypath, szInstaller_LocalClassesFeat);
578         strcatW(keypath, squished_pc);
579     }
580     else if (context == MSIINSTALLCONTEXT_USERUNMANAGED)
581     {
582         root = HKEY_CURRENT_USER;
583         strcpyW(keypath, szUserFeatures);
584         strcatW(keypath, squished_pc);
585     }
586     else
587     {
588         if (!(usersid = get_user_sid()))
589         {
590             ERR("Failed to retrieve user SID\n");
591             return ERROR_FUNCTION_FAILED;
592         }
593         sprintfW(keypath, szInstaller_LocalManagedFeat_fmt, usersid, squished_pc);
594         LocalFree(usersid);
595     }
596     if (create) return RegCreateKeyExW(root, keypath, 0, NULL, 0, access, NULL, key, NULL);
597     return RegOpenKeyExW(root, keypath, 0, access, key);
598 }
599
600 UINT MSIREG_DeleteUserFeaturesKey(LPCWSTR szProduct)
601 {
602     WCHAR squished_pc[GUID_SIZE], keypath[0x200];
603
604     if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
605     TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
606
607     strcpyW(keypath, szUserFeatures);
608     strcatW(keypath, squished_pc);
609     return RegDeleteTreeW(HKEY_CURRENT_USER, keypath);
610 }
611
612 static UINT MSIREG_OpenInstallerFeaturesKey(LPCWSTR szProduct, HKEY *key, BOOL create)
613 {
614     REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
615     WCHAR squished_pc[GUID_SIZE], keypath[0x200];
616
617     if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
618     TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
619
620     strcpyW(keypath, szInstaller_Features);
621     strcatW(keypath, squished_pc);
622
623     if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
624     return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
625 }
626
627 UINT MSIREG_OpenUserDataFeaturesKey(LPCWSTR szProduct, MSIINSTALLCONTEXT context, HKEY *key, BOOL create)
628 {
629     LPWSTR usersid;
630     REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
631     WCHAR squished_pc[GUID_SIZE], keypath[0x200];
632
633     if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
634     TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
635
636     if (context == MSIINSTALLCONTEXT_MACHINE)
637     {
638         sprintfW(keypath, szUserDataFeatures_fmt, szLocalSid, squished_pc);
639     }
640     else
641     {
642         if (!(usersid = get_user_sid()))
643         {
644             ERR("Failed to retrieve user SID\n");
645             return ERROR_FUNCTION_FAILED;
646         }
647         sprintfW(keypath, szUserDataFeatures_fmt, usersid, squished_pc);
648         LocalFree(usersid);
649     }
650     if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
651     return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
652 }
653
654 UINT MSIREG_OpenUserComponentsKey(LPCWSTR szComponent, HKEY *key, BOOL create)
655 {
656     WCHAR squished_cc[GUID_SIZE], keypath[0x200];
657
658     if (!squash_guid(szComponent, squished_cc)) return ERROR_FUNCTION_FAILED;
659     TRACE("%s squished %s\n", debugstr_w(szComponent), debugstr_w(squished_cc));
660
661     strcpyW(keypath, szUserComponents);
662     strcatW(keypath, squished_cc);
663
664     if (create) return RegCreateKeyW(HKEY_CURRENT_USER, keypath, key);
665     return RegOpenKeyW(HKEY_CURRENT_USER, keypath, key);
666 }
667
668 UINT MSIREG_OpenUserDataComponentKey(LPCWSTR szComponent, LPCWSTR szUserSid, HKEY *key, BOOL create)
669 {
670     REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
671     WCHAR comp[GUID_SIZE], keypath[0x200];
672     LPWSTR usersid;
673
674     if (!squash_guid(szComponent, comp)) return ERROR_FUNCTION_FAILED;
675     TRACE("%s squished %s\n", debugstr_w(szComponent), debugstr_w(comp));
676
677     if (!szUserSid)
678     {
679         if (!(usersid = get_user_sid()))
680         {
681             ERR("Failed to retrieve user SID\n");
682             return ERROR_FUNCTION_FAILED;
683         }
684         sprintfW(keypath, szUserDataComp_fmt, usersid, comp);
685         LocalFree(usersid);
686     }
687     else
688         sprintfW(keypath, szUserDataComp_fmt, szUserSid, comp);
689
690     if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
691     return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
692 }
693
694 UINT MSIREG_DeleteUserDataComponentKey(LPCWSTR szComponent, LPCWSTR szUserSid)
695 {
696     REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
697     WCHAR comp[GUID_SIZE], keypath[0x200];
698     LPWSTR usersid;
699     HKEY hkey;
700     LONG r;
701
702     if (!squash_guid(szComponent, comp)) return ERROR_FUNCTION_FAILED;
703     TRACE("%s squished %s\n", debugstr_w(szComponent), debugstr_w(comp));
704
705     if (!szUserSid)
706     {
707         if (!(usersid = get_user_sid()))
708         {
709             ERR("Failed to retrieve user SID\n");
710             return ERROR_FUNCTION_FAILED;
711         }
712         sprintfW(keypath, szUserDataComponents_fmt, usersid);
713         LocalFree(usersid);
714     }
715     else
716         sprintfW(keypath, szUserDataComponents_fmt, szUserSid);
717
718     if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, &hkey)) return ERROR_SUCCESS;
719     r = RegDeleteTreeW(hkey, comp);
720     RegCloseKey(hkey);
721     return r;
722 }
723
724 UINT MSIREG_OpenUserDataProductKey(LPCWSTR szProduct, MSIINSTALLCONTEXT dwContext, LPCWSTR szUserSid, HKEY *key, BOOL create)
725 {
726     REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
727     WCHAR squished_pc[GUID_SIZE], keypath[0x200];
728     LPWSTR usersid;
729
730     if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
731     TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
732
733     if (dwContext == MSIINSTALLCONTEXT_MACHINE)
734         sprintfW(keypath, szUserDataProd_fmt, szLocalSid, squished_pc);
735     else if (szUserSid)
736         sprintfW(keypath, szUserDataProd_fmt, szUserSid, squished_pc);
737     else
738     {
739         if (!(usersid = get_user_sid()))
740         {
741             ERR("Failed to retrieve user SID\n");
742             return ERROR_FUNCTION_FAILED;
743         }
744         sprintfW(keypath, szUserDataProd_fmt, usersid, squished_pc);
745         LocalFree(usersid);
746     }
747     if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
748     return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
749 }
750
751 UINT MSIREG_OpenUserDataPatchKey(LPCWSTR szPatch, MSIINSTALLCONTEXT dwContext, HKEY *key, BOOL create)
752 {
753     REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
754     WCHAR squished_patch[GUID_SIZE], keypath[0x200];
755     LPWSTR usersid;
756
757     if (!squash_guid(szPatch, squished_patch)) return ERROR_FUNCTION_FAILED;
758     TRACE("%s squished %s\n", debugstr_w(szPatch), debugstr_w(squished_patch));
759
760     if (dwContext == MSIINSTALLCONTEXT_MACHINE)
761         sprintfW(keypath, szUserDataPatch_fmt, szLocalSid, squished_patch);
762     else
763     {
764         if (!(usersid = get_user_sid()))
765         {
766             ERR("Failed to retrieve user SID\n");
767             return ERROR_FUNCTION_FAILED;
768         }
769         sprintfW(keypath, szUserDataPatch_fmt, usersid, squished_patch);
770         LocalFree(usersid);
771     }
772     if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
773     return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
774 }
775
776 UINT MSIREG_DeleteUserDataPatchKey(LPCWSTR patch, MSIINSTALLCONTEXT context)
777 {
778     REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
779     WCHAR squished_patch[GUID_SIZE], keypath[0x200];
780     LPWSTR usersid;
781     HKEY hkey;
782     LONG r;
783
784     if (!squash_guid(patch, squished_patch)) return ERROR_FUNCTION_FAILED;
785     TRACE("%s squished %s\n", debugstr_w(patch), debugstr_w(squished_patch));
786
787     if (context == MSIINSTALLCONTEXT_MACHINE)
788         sprintfW(keypath, szUserDataPatches_fmt, szLocalSid);
789     else
790     {
791         if (!(usersid = get_user_sid()))
792         {
793             ERR("Failed to retrieve user SID\n");
794             return ERROR_FUNCTION_FAILED;
795         }
796         sprintfW(keypath, szUserDataPatches_fmt, usersid);
797         LocalFree(usersid);
798     }
799     if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, &hkey)) return ERROR_SUCCESS;
800     r = RegDeleteTreeW(hkey, squished_patch);
801     RegCloseKey(hkey);
802     return r;
803 }
804
805 UINT MSIREG_OpenUserDataProductPatchesKey(LPCWSTR product, MSIINSTALLCONTEXT context, HKEY *key, BOOL create)
806 {
807     REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
808     WCHAR squished_product[GUID_SIZE], keypath[0x200];
809     LPWSTR usersid;
810
811     if (!squash_guid(product, squished_product)) return ERROR_FUNCTION_FAILED;
812     TRACE("%s squished %s\n", debugstr_w(product), debugstr_w(squished_product));
813
814     if (context == MSIINSTALLCONTEXT_MACHINE)
815         sprintfW(keypath, szUserDataProductPatches_fmt, szLocalSid, squished_product);
816     else
817     {
818         if (!(usersid = get_user_sid()))
819         {
820             ERR("Failed to retrieve user SID\n");
821             return ERROR_FUNCTION_FAILED;
822         }
823         sprintfW(keypath, szUserDataProductPatches_fmt, usersid, squished_product);
824         LocalFree(usersid);
825     }
826     if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
827     return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
828 }
829
830 UINT MSIREG_OpenInstallProps(LPCWSTR szProduct, MSIINSTALLCONTEXT dwContext, LPCWSTR szUserSid, HKEY *key, BOOL create)
831 {
832     LPWSTR usersid;
833     REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
834     WCHAR squished_pc[GUID_SIZE], keypath[0x200];
835
836     if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
837     TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
838
839     if (dwContext == MSIINSTALLCONTEXT_MACHINE)
840         sprintfW(keypath, szInstallProperties_fmt, szLocalSid, squished_pc);
841     else if (szUserSid)
842         sprintfW(keypath, szInstallProperties_fmt, szUserSid, squished_pc);
843     else
844     {
845         if (!(usersid = get_user_sid()))
846         {
847             ERR("Failed to retrieve user SID\n");
848             return ERROR_FUNCTION_FAILED;
849         }
850         sprintfW(keypath, szInstallProperties_fmt, usersid, squished_pc);
851         LocalFree(usersid);
852     }
853     if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
854     return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
855 }
856
857 UINT MSIREG_DeleteUserDataProductKey(LPCWSTR szProduct)
858 {
859     REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
860     WCHAR squished_pc[GUID_SIZE], keypath[0x200];
861     LPWSTR usersid;
862     HKEY hkey;
863     LONG r;
864
865     if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
866     TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
867
868     if (!(usersid = get_user_sid()))
869     {
870         ERR("Failed to retrieve user SID\n");
871         return ERROR_FUNCTION_FAILED;
872     }
873     sprintfW(keypath, szUserDataProducts_fmt, usersid);
874     LocalFree(usersid);
875
876     if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, &hkey)) return ERROR_SUCCESS;
877     r = RegDeleteTreeW(hkey, squished_pc);
878     RegCloseKey(hkey);
879     return r;
880 }
881
882 UINT MSIREG_DeleteProductKey(LPCWSTR szProduct)
883 {
884     REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
885     WCHAR squished_pc[GUID_SIZE];
886     HKEY hkey;
887     LONG r;
888
889     if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
890     TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
891
892     if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, szInstaller_Products, 0, access, &hkey)) return ERROR_SUCCESS;
893     r = RegDeleteTreeW(hkey, squished_pc);
894     RegCloseKey(hkey);
895     return r;
896 }
897
898 UINT MSIREG_OpenPatchesKey(LPCWSTR szPatch, HKEY *key, BOOL create)
899 {
900     REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
901     WCHAR squished_pc[GUID_SIZE], keypath[0x200];
902
903     if (!squash_guid(szPatch, squished_pc)) return ERROR_FUNCTION_FAILED;
904     TRACE("%s squished %s\n", debugstr_w(szPatch), debugstr_w(squished_pc));
905
906     sprintfW(keypath, szInstaller_Patches, squished_pc);
907
908     if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
909     return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
910 }
911
912 UINT MSIREG_OpenUpgradeCodesKey(LPCWSTR szUpgradeCode, HKEY *key, BOOL create)
913 {
914     REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
915     WCHAR squished_pc[GUID_SIZE], keypath[0x200];
916
917     if (!squash_guid(szUpgradeCode, squished_pc)) return ERROR_FUNCTION_FAILED;
918     TRACE("%s squished %s\n", debugstr_w(szUpgradeCode), debugstr_w(squished_pc));
919
920     strcpyW(keypath, szInstaller_UpgradeCodes);
921     strcatW(keypath, squished_pc);
922
923     if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
924     return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
925 }
926
927 UINT MSIREG_OpenUserUpgradeCodesKey(LPCWSTR szUpgradeCode, HKEY* key, BOOL create)
928 {
929     WCHAR squished_pc[GUID_SIZE], keypath[0x200];
930
931     if (!squash_guid(szUpgradeCode, squished_pc)) return ERROR_FUNCTION_FAILED;
932     TRACE("%s squished %s\n", debugstr_w(szUpgradeCode), debugstr_w(squished_pc));
933
934     strcpyW(keypath, szInstaller_UserUpgradeCodes);
935     strcatW(keypath, squished_pc);
936
937     if (create) return RegCreateKeyW(HKEY_CURRENT_USER, keypath, key);
938     return RegOpenKeyW(HKEY_CURRENT_USER, keypath, key);
939 }
940
941 UINT MSIREG_DeleteUserUpgradeCodesKey(LPCWSTR szUpgradeCode)
942 {
943     WCHAR squished_pc[GUID_SIZE], keypath[0x200];
944
945     if (!squash_guid(szUpgradeCode, squished_pc)) return ERROR_FUNCTION_FAILED;
946     TRACE("%s squished %s\n", debugstr_w(szUpgradeCode), debugstr_w(squished_pc));
947
948     strcpyW(keypath, szInstaller_UserUpgradeCodes);
949     strcatW(keypath, squished_pc);
950     return RegDeleteTreeW(HKEY_CURRENT_USER, keypath);
951 }
952
953 UINT MSIREG_DeleteLocalClassesProductKey(LPCWSTR szProductCode)
954 {
955     REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
956     WCHAR squished_pc[GUID_SIZE];
957     HKEY hkey;
958     LONG r;
959
960     if (!squash_guid(szProductCode, squished_pc)) return ERROR_FUNCTION_FAILED;
961     TRACE("%s squished %s\n", debugstr_w(szProductCode), debugstr_w(squished_pc));
962
963     if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, szInstaller_LocalClassesProducts, 0, access, &hkey)) return ERROR_SUCCESS;
964     r = RegDeleteTreeW(hkey, squished_pc);
965     RegCloseKey(hkey);
966     return r;
967 }
968
969 UINT MSIREG_DeleteLocalClassesFeaturesKey(LPCWSTR szProductCode)
970 {
971     REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
972     WCHAR squished_pc[GUID_SIZE];
973     HKEY hkey;
974     LONG r;
975
976     if (!squash_guid(szProductCode, squished_pc)) return ERROR_FUNCTION_FAILED;
977     TRACE("%s squished %s\n", debugstr_w(szProductCode), debugstr_w(squished_pc));
978
979     if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, szInstaller_LocalClassesFeatures, 0, access, &hkey)) return ERROR_SUCCESS;
980     r = RegDeleteTreeW(hkey, squished_pc);
981     RegCloseKey(hkey);
982     return r;
983 }
984
985 UINT MSIREG_OpenClassesUpgradeCodesKey(LPCWSTR szUpgradeCode, HKEY *key, BOOL create)
986 {
987     REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
988     WCHAR squished_pc[GUID_SIZE], keypath[0x200];
989
990     if (!squash_guid(szUpgradeCode, squished_pc)) return ERROR_FUNCTION_FAILED;
991     TRACE("%s squished %s\n", debugstr_w(szUpgradeCode), debugstr_w(squished_pc));
992
993     strcpyW(keypath, szInstaller_ClassesUpgradeCode);
994     strcatW(keypath, squished_pc);
995
996     if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
997     return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
998 }
999
1000 UINT MSIREG_DeleteClassesUpgradeCodesKey(LPCWSTR szUpgradeCode)
1001 {
1002     REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
1003     WCHAR squished_pc[GUID_SIZE];
1004     HKEY hkey;
1005     LONG r;
1006
1007     if (!squash_guid(szUpgradeCode, squished_pc)) return ERROR_FUNCTION_FAILED;
1008     TRACE("%s squished %s\n", debugstr_w(szUpgradeCode), debugstr_w(squished_pc));
1009
1010     if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, szInstaller_ClassesUpgradeCodes, 0, access, &hkey)) return ERROR_SUCCESS;
1011     r = RegDeleteTreeW(hkey, squished_pc);
1012     RegCloseKey(hkey);
1013     return r;
1014 }
1015
1016 /*************************************************************************
1017  *  MsiDecomposeDescriptorW   [MSI.@]
1018  *
1019  * Decomposes an MSI descriptor into product, feature and component parts.
1020  * An MSI descriptor is a string of the form:
1021  *   [base 85 guid] [feature code] '>' [base 85 guid]
1022  *
1023  * PARAMS
1024  *   szDescriptor  [I]  the descriptor to decompose
1025  *   szProduct     [O]  buffer of MAX_FEATURE_CHARS+1 for the product guid
1026  *   szFeature     [O]  buffer of MAX_FEATURE_CHARS+1 for the feature code
1027  *   szComponent   [O]  buffer of MAX_FEATURE_CHARS+1 for the component guid
1028  *   pUsed         [O]  the length of the descriptor
1029  *
1030  * RETURNS
1031  *   ERROR_SUCCESS             if everything worked correctly
1032  *   ERROR_INVALID_PARAMETER   if the descriptor was invalid
1033  *
1034  */
1035 UINT WINAPI MsiDecomposeDescriptorW( LPCWSTR szDescriptor, LPWSTR szProduct,
1036                 LPWSTR szFeature, LPWSTR szComponent, LPDWORD pUsed )
1037 {
1038     UINT r, len;
1039     LPWSTR p;
1040     GUID product, component;
1041
1042     TRACE("%s %p %p %p %p\n", debugstr_w(szDescriptor), szProduct,
1043           szFeature, szComponent, pUsed);
1044
1045     r = decode_base85_guid( szDescriptor, &product );
1046     if( !r )
1047         return ERROR_INVALID_PARAMETER;
1048
1049     TRACE("product %s\n", debugstr_guid( &product ));
1050
1051     p = strchrW(&szDescriptor[20],'>');
1052     if( !p )
1053         return ERROR_INVALID_PARAMETER;
1054
1055     len = (p - &szDescriptor[20]);
1056     if( len > MAX_FEATURE_CHARS )
1057         return ERROR_INVALID_PARAMETER;
1058
1059     TRACE("feature %s\n", debugstr_wn( &szDescriptor[20], len ));
1060
1061     r = decode_base85_guid( p+1, &component );
1062     if( !r )
1063         return ERROR_INVALID_PARAMETER;
1064
1065     TRACE("component %s\n", debugstr_guid( &component ));
1066
1067     if (szProduct)
1068         StringFromGUID2( &product, szProduct, MAX_FEATURE_CHARS+1 );
1069     if (szComponent)
1070         StringFromGUID2( &component, szComponent, MAX_FEATURE_CHARS+1 );
1071     if (szFeature)
1072     {
1073         memcpy( szFeature, &szDescriptor[20], len*sizeof(WCHAR) );
1074         szFeature[len] = 0;
1075     }
1076     len = ( &p[21] - szDescriptor );
1077
1078     TRACE("length = %d\n", len);
1079     if (pUsed) *pUsed = len;
1080
1081     return ERROR_SUCCESS;
1082 }
1083
1084 UINT WINAPI MsiDecomposeDescriptorA( LPCSTR szDescriptor, LPSTR szProduct,
1085                 LPSTR szFeature, LPSTR szComponent, LPDWORD pUsed )
1086 {
1087     WCHAR product[MAX_FEATURE_CHARS+1];
1088     WCHAR feature[MAX_FEATURE_CHARS+1];
1089     WCHAR component[MAX_FEATURE_CHARS+1];
1090     LPWSTR str = NULL, p = NULL, f = NULL, c = NULL;
1091     UINT r;
1092
1093     TRACE("%s %p %p %p %p\n", debugstr_a(szDescriptor), szProduct,
1094           szFeature, szComponent, pUsed);
1095
1096     str = strdupAtoW( szDescriptor );
1097     if( szDescriptor && !str )
1098         return ERROR_OUTOFMEMORY;
1099
1100     if (szProduct)
1101         p = product;
1102     if (szFeature)
1103         f = feature;
1104     if (szComponent)
1105         c = component;
1106
1107     r = MsiDecomposeDescriptorW( str, p, f, c, pUsed );
1108
1109     if (r == ERROR_SUCCESS)
1110     {
1111         WideCharToMultiByte( CP_ACP, 0, p, -1,
1112                              szProduct, MAX_FEATURE_CHARS+1, NULL, NULL );
1113         WideCharToMultiByte( CP_ACP, 0, f, -1,
1114                              szFeature, MAX_FEATURE_CHARS+1, NULL, NULL );
1115         WideCharToMultiByte( CP_ACP, 0, c, -1,
1116                              szComponent, MAX_FEATURE_CHARS+1, NULL, NULL );
1117     }
1118
1119     msi_free( str );
1120
1121     return r;
1122 }
1123
1124 UINT WINAPI MsiEnumProductsA(DWORD index, LPSTR lpguid)
1125 {
1126     DWORD r;
1127     WCHAR szwGuid[GUID_SIZE];
1128
1129     TRACE("%d %p\n", index, lpguid);
1130
1131     if (NULL == lpguid)
1132         return ERROR_INVALID_PARAMETER;
1133     r = MsiEnumProductsW(index, szwGuid);
1134     if( r == ERROR_SUCCESS )
1135         WideCharToMultiByte(CP_ACP, 0, szwGuid, -1, lpguid, GUID_SIZE, NULL, NULL);
1136
1137     return r;
1138 }
1139
1140 UINT WINAPI MsiEnumProductsW(DWORD index, LPWSTR lpguid)
1141 {
1142     static const WCHAR pathW[] = {
1143         'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
1144         'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s',0};
1145     UINT r;
1146     WCHAR szKeyName[SQUISH_GUID_SIZE];
1147     HKEY key;
1148     REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
1149     DWORD machine_count, managed_count, unmanaged_count;
1150     WCHAR keypath[MAX_PATH];
1151     LPWSTR usersid = NULL;
1152
1153     static DWORD last_index;
1154
1155     TRACE("%d %p\n", index, lpguid);
1156
1157     if (NULL == lpguid)
1158         return ERROR_INVALID_PARAMETER;
1159
1160     if (index && index - last_index != 1)
1161         return ERROR_INVALID_PARAMETER;
1162
1163     key = 0;
1164     r = RegCreateKeyExW(HKEY_LOCAL_MACHINE, szInstaller_LocalClassesProducts, 0, NULL, 0, access, NULL, &key, NULL);
1165     if( r != ERROR_SUCCESS ) goto failed;
1166
1167     r = RegQueryInfoKeyW(key, NULL, NULL, NULL, &machine_count, NULL, NULL,
1168                          NULL, NULL, NULL, NULL, NULL);
1169     if( r != ERROR_SUCCESS ) goto failed;
1170
1171     if (machine_count && index <= machine_count)
1172     {
1173         r = RegEnumKeyW(key, index, szKeyName, SQUISH_GUID_SIZE);
1174         if( r == ERROR_SUCCESS )
1175         {
1176             unsquash_guid(szKeyName, lpguid);
1177             last_index = index;
1178             RegCloseKey(key);
1179             return ERROR_SUCCESS;
1180         }
1181     }
1182     RegCloseKey(key);
1183
1184     key = 0;
1185     if (!(usersid = get_user_sid()))
1186     {
1187         ERR("Failed to retrieve user SID\n");
1188         last_index = 0;
1189         return ERROR_FUNCTION_FAILED;
1190     }
1191     sprintfW(keypath, szInstaller_LocalManaged_fmt, usersid);
1192     LocalFree(usersid);
1193
1194     r = RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, &key, NULL);
1195     if( r != ERROR_SUCCESS ) goto failed;
1196
1197     r = RegQueryInfoKeyW(key, NULL, NULL, NULL, &managed_count, NULL, NULL,
1198                          NULL, NULL, NULL, NULL, NULL);
1199     if( r != ERROR_SUCCESS ) goto failed;
1200
1201     if (managed_count && index <= machine_count + managed_count)
1202     {
1203         r = RegEnumKeyW(key, index - machine_count, szKeyName, SQUISH_GUID_SIZE);
1204         if( r == ERROR_SUCCESS )
1205         {
1206             unsquash_guid(szKeyName, lpguid);
1207             last_index = index;
1208             RegCloseKey(key);
1209             return ERROR_SUCCESS;
1210         }
1211     }
1212     RegCloseKey(key);
1213
1214     key = 0;
1215     r = RegCreateKeyW(HKEY_CURRENT_USER, pathW, &key);
1216     if( r != ERROR_SUCCESS ) goto failed;
1217
1218     r = RegQueryInfoKeyW(key, NULL, NULL, NULL, &unmanaged_count, NULL, NULL,
1219                          NULL, NULL, NULL, NULL, NULL);
1220     if( r != ERROR_SUCCESS ) goto failed;
1221
1222     if (unmanaged_count && index <= machine_count + managed_count + unmanaged_count)
1223     {
1224         r = RegEnumKeyW(key, index - machine_count - managed_count, szKeyName, SQUISH_GUID_SIZE);
1225         if( r == ERROR_SUCCESS )
1226         {
1227             unsquash_guid(szKeyName, lpguid);
1228             last_index = index;
1229             RegCloseKey(key);
1230             return ERROR_SUCCESS;
1231         }
1232     }
1233 failed:
1234     RegCloseKey(key);
1235     last_index = 0;
1236     return ERROR_NO_MORE_ITEMS;
1237 }
1238
1239 UINT WINAPI MsiEnumFeaturesA(LPCSTR szProduct, DWORD index, 
1240       LPSTR szFeature, LPSTR szParent)
1241 {
1242     DWORD r;
1243     WCHAR szwFeature[GUID_SIZE], szwParent[GUID_SIZE];
1244     LPWSTR szwProduct = NULL;
1245
1246     TRACE("%s %d %p %p\n", debugstr_a(szProduct), index, szFeature, szParent);
1247
1248     if( szProduct )
1249     {
1250         szwProduct = strdupAtoW( szProduct );
1251         if( !szwProduct )
1252             return ERROR_OUTOFMEMORY;
1253     }
1254
1255     r = MsiEnumFeaturesW(szwProduct, index, szwFeature, szwParent);
1256     if( r == ERROR_SUCCESS )
1257     {
1258         WideCharToMultiByte(CP_ACP, 0, szwFeature, -1,
1259                             szFeature, GUID_SIZE, NULL, NULL);
1260         WideCharToMultiByte(CP_ACP, 0, szwParent, -1,
1261                             szParent, GUID_SIZE, NULL, NULL);
1262     }
1263
1264     msi_free( szwProduct);
1265
1266     return r;
1267 }
1268
1269 UINT WINAPI MsiEnumFeaturesW(LPCWSTR szProduct, DWORD index, 
1270       LPWSTR szFeature, LPWSTR szParent)
1271 {
1272     HKEY hkeyProduct = 0;
1273     DWORD r, sz;
1274
1275     TRACE("%s %d %p %p\n", debugstr_w(szProduct), index, szFeature, szParent);
1276
1277     if( !szProduct )
1278         return ERROR_INVALID_PARAMETER;
1279
1280     r = MSIREG_OpenInstallerFeaturesKey(szProduct,&hkeyProduct,FALSE);
1281     if( r != ERROR_SUCCESS )
1282         return ERROR_NO_MORE_ITEMS;
1283
1284     sz = GUID_SIZE;
1285     r = RegEnumValueW(hkeyProduct, index, szFeature, &sz, NULL, NULL, NULL, NULL);
1286     RegCloseKey(hkeyProduct);
1287
1288     return r;
1289 }
1290
1291 UINT WINAPI MsiEnumComponentsA(DWORD index, LPSTR lpguid)
1292 {
1293     DWORD r;
1294     WCHAR szwGuid[GUID_SIZE];
1295
1296     TRACE("%d %p\n", index, lpguid);
1297
1298     r = MsiEnumComponentsW(index, szwGuid);
1299     if( r == ERROR_SUCCESS )
1300         WideCharToMultiByte(CP_ACP, 0, szwGuid, -1, lpguid, GUID_SIZE, NULL, NULL);
1301
1302     return r;
1303 }
1304
1305 UINT WINAPI MsiEnumComponentsW(DWORD index, LPWSTR lpguid)
1306 {
1307     HKEY hkey;
1308     REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
1309     WCHAR szKeyName[SQUISH_GUID_SIZE];
1310     DWORD r;
1311
1312     TRACE("%d %p\n", index, lpguid);
1313
1314     r = RegCreateKeyExW(HKEY_LOCAL_MACHINE, szInstaller_Components, 0, NULL, 0, access, NULL, &hkey, NULL);
1315     if( r != ERROR_SUCCESS )
1316         return ERROR_NO_MORE_ITEMS;
1317
1318     r = RegEnumKeyW(hkey, index, szKeyName, SQUISH_GUID_SIZE);
1319     if( r == ERROR_SUCCESS )
1320         unsquash_guid(szKeyName, lpguid);
1321
1322     RegCloseKey(hkey);
1323     return r;
1324 }
1325
1326 UINT WINAPI MsiEnumClientsA(LPCSTR szComponent, DWORD index, LPSTR szProduct)
1327 {
1328     DWORD r;
1329     WCHAR szwProduct[GUID_SIZE];
1330     LPWSTR szwComponent = NULL;
1331
1332     TRACE("%s %d %p\n", debugstr_a(szComponent), index, szProduct);
1333
1334     if ( !szProduct )
1335         return ERROR_INVALID_PARAMETER;
1336
1337     if( szComponent )
1338     {
1339         szwComponent = strdupAtoW( szComponent );
1340         if( !szwComponent )
1341             return ERROR_OUTOFMEMORY;
1342     }
1343
1344     r = MsiEnumClientsW(szComponent?szwComponent:NULL, index, szwProduct);
1345     if( r == ERROR_SUCCESS )
1346     {
1347         WideCharToMultiByte(CP_ACP, 0, szwProduct, -1,
1348                             szProduct, GUID_SIZE, NULL, NULL);
1349     }
1350
1351     msi_free( szwComponent);
1352
1353     return r;
1354 }
1355
1356 UINT WINAPI MsiEnumClientsW(LPCWSTR szComponent, DWORD index, LPWSTR szProduct)
1357 {
1358     HKEY hkeyComp = 0;
1359     DWORD r, sz;
1360     WCHAR szValName[SQUISH_GUID_SIZE];
1361
1362     TRACE("%s %d %p\n", debugstr_w(szComponent), index, szProduct);
1363
1364     if (!szComponent || !*szComponent || !szProduct)
1365         return ERROR_INVALID_PARAMETER;
1366
1367     if (MSIREG_OpenUserDataComponentKey(szComponent, NULL, &hkeyComp, FALSE) != ERROR_SUCCESS &&
1368         MSIREG_OpenUserDataComponentKey(szComponent, szLocalSid, &hkeyComp, FALSE) != ERROR_SUCCESS)
1369         return ERROR_UNKNOWN_COMPONENT;
1370
1371     /* see if there are any products at all */
1372     sz = SQUISH_GUID_SIZE;
1373     r = RegEnumValueW(hkeyComp, 0, szValName, &sz, NULL, NULL, NULL, NULL);
1374     if (r != ERROR_SUCCESS)
1375     {
1376         RegCloseKey(hkeyComp);
1377
1378         if (index != 0)
1379             return ERROR_INVALID_PARAMETER;
1380
1381         return ERROR_UNKNOWN_COMPONENT;
1382     }
1383
1384     sz = SQUISH_GUID_SIZE;
1385     r = RegEnumValueW(hkeyComp, index, szValName, &sz, NULL, NULL, NULL, NULL);
1386     if( r == ERROR_SUCCESS )
1387     {
1388         unsquash_guid(szValName, szProduct);
1389         TRACE("-> %s\n", debugstr_w(szProduct));
1390     }
1391     RegCloseKey(hkeyComp);
1392     return r;
1393 }
1394
1395 static UINT MSI_EnumComponentQualifiers( LPCWSTR szComponent, DWORD iIndex,
1396                 awstring *lpQualBuf, LPDWORD pcchQual,
1397                 awstring *lpAppBuf, LPDWORD pcchAppBuf )
1398 {
1399     DWORD name_sz, val_sz, name_max, val_max, type, ofs;
1400     LPWSTR name = NULL, val = NULL;
1401     UINT r, r2;
1402     HKEY key;
1403
1404     TRACE("%s %08x %p %p %p %p\n", debugstr_w(szComponent), iIndex,
1405           lpQualBuf, pcchQual, lpAppBuf, pcchAppBuf);
1406
1407     if (!szComponent)
1408         return ERROR_INVALID_PARAMETER;
1409
1410     r = MSIREG_OpenUserComponentsKey( szComponent, &key, FALSE );
1411     if (r != ERROR_SUCCESS)
1412         return ERROR_UNKNOWN_COMPONENT;
1413
1414     /* figure out how big the name is we want to return */
1415     name_max = 0x10;
1416     r = ERROR_OUTOFMEMORY;
1417     name = msi_alloc( name_max * sizeof(WCHAR) );
1418     if (!name)
1419         goto end;
1420
1421     val_max = 0x10;
1422     r = ERROR_OUTOFMEMORY;
1423     val = msi_alloc( val_max );
1424     if (!val)
1425         goto end;
1426
1427     /* loop until we allocate enough memory */
1428     while (1)
1429     {
1430         name_sz = name_max;
1431         val_sz = val_max;
1432         r = RegEnumValueW( key, iIndex, name, &name_sz,
1433                            NULL, &type, (LPBYTE)val, &val_sz );
1434         if (r == ERROR_SUCCESS)
1435             break;
1436         if (r != ERROR_MORE_DATA)
1437             goto end;
1438  
1439         if (type != REG_MULTI_SZ)
1440         {
1441             ERR("component data has wrong type (%d)\n", type);
1442             goto end;
1443         }
1444
1445         r = ERROR_OUTOFMEMORY;
1446         if ((name_sz+1) >= name_max)
1447         {
1448             name_max *= 2;
1449             msi_free( name );
1450             name = msi_alloc( name_max * sizeof (WCHAR) );
1451             if (!name)
1452                 goto end;
1453             continue;
1454         }
1455         if (val_sz > val_max)
1456         {
1457             val_max = val_sz + sizeof (WCHAR);
1458             msi_free( val );
1459             val = msi_alloc( val_max * sizeof (WCHAR) );
1460             if (!val)
1461                 goto end;
1462             continue;
1463         }
1464         ERR("should be enough data, but isn't %d %d\n", name_sz, val_sz );
1465         goto end;
1466     }
1467
1468     ofs = 0;
1469     r = MsiDecomposeDescriptorW( val, NULL, NULL, NULL, &ofs );
1470     if (r != ERROR_SUCCESS)
1471         goto end;
1472
1473     TRACE("Providing %s and %s\n", debugstr_w(name), debugstr_w(val+ofs));
1474
1475     r = msi_strcpy_to_awstring( name, lpQualBuf, pcchQual );
1476     r2 = msi_strcpy_to_awstring( val+ofs, lpAppBuf, pcchAppBuf );
1477
1478     if (r2 != ERROR_SUCCESS)
1479         r = r2;
1480
1481 end:
1482     msi_free(val);
1483     msi_free(name);
1484     RegCloseKey(key);
1485
1486     return r;
1487 }
1488
1489 /*************************************************************************
1490  *  MsiEnumComponentQualifiersA [MSI.@]
1491  */
1492 UINT WINAPI MsiEnumComponentQualifiersA( LPCSTR szComponent, DWORD iIndex,
1493                 LPSTR lpQualifierBuf, LPDWORD pcchQualifierBuf,
1494                 LPSTR lpApplicationDataBuf, LPDWORD pcchApplicationDataBuf )
1495 {
1496     awstring qual, appdata;
1497     LPWSTR comp;
1498     UINT r;
1499
1500     TRACE("%s %08x %p %p %p %p\n", debugstr_a(szComponent), iIndex,
1501           lpQualifierBuf, pcchQualifierBuf, lpApplicationDataBuf,
1502           pcchApplicationDataBuf);
1503
1504     comp = strdupAtoW( szComponent );
1505     if (szComponent && !comp)
1506         return ERROR_OUTOFMEMORY;
1507
1508     qual.unicode = FALSE;
1509     qual.str.a = lpQualifierBuf;
1510
1511     appdata.unicode = FALSE;
1512     appdata.str.a = lpApplicationDataBuf;
1513
1514     r = MSI_EnumComponentQualifiers( comp, iIndex,
1515               &qual, pcchQualifierBuf, &appdata, pcchApplicationDataBuf );
1516     msi_free( comp );
1517     return r;
1518 }
1519
1520 /*************************************************************************
1521  *  MsiEnumComponentQualifiersW [MSI.@]
1522  */
1523 UINT WINAPI MsiEnumComponentQualifiersW( LPCWSTR szComponent, DWORD iIndex,
1524                 LPWSTR lpQualifierBuf, LPDWORD pcchQualifierBuf,
1525                 LPWSTR lpApplicationDataBuf, LPDWORD pcchApplicationDataBuf )
1526 {
1527     awstring qual, appdata;
1528
1529     TRACE("%s %08x %p %p %p %p\n", debugstr_w(szComponent), iIndex,
1530           lpQualifierBuf, pcchQualifierBuf, lpApplicationDataBuf,
1531           pcchApplicationDataBuf);
1532
1533     qual.unicode = TRUE;
1534     qual.str.w = lpQualifierBuf;
1535
1536     appdata.unicode = TRUE;
1537     appdata.str.w = lpApplicationDataBuf;
1538
1539     return MSI_EnumComponentQualifiers( szComponent, iIndex,
1540                  &qual, pcchQualifierBuf, &appdata, pcchApplicationDataBuf );
1541 }
1542
1543 /*************************************************************************
1544  *  MsiEnumRelatedProductsW   [MSI.@]
1545  *
1546  */
1547 UINT WINAPI MsiEnumRelatedProductsW(LPCWSTR szUpgradeCode, DWORD dwReserved,
1548                                     DWORD iProductIndex, LPWSTR lpProductBuf)
1549 {
1550     UINT r;
1551     HKEY hkey;
1552     DWORD dwSize = SQUISH_GUID_SIZE;
1553     WCHAR szKeyName[SQUISH_GUID_SIZE];
1554
1555     TRACE("%s %u %u %p\n", debugstr_w(szUpgradeCode), dwReserved,
1556           iProductIndex, lpProductBuf);
1557
1558     if (NULL == szUpgradeCode)
1559         return ERROR_INVALID_PARAMETER;
1560     if (NULL == lpProductBuf)
1561         return ERROR_INVALID_PARAMETER;
1562
1563     r = MSIREG_OpenUpgradeCodesKey(szUpgradeCode, &hkey, FALSE);
1564     if (r != ERROR_SUCCESS)
1565         return ERROR_NO_MORE_ITEMS;
1566
1567     r = RegEnumValueW(hkey, iProductIndex, szKeyName, &dwSize, NULL, NULL, NULL, NULL);
1568     if( r == ERROR_SUCCESS )
1569         unsquash_guid(szKeyName, lpProductBuf);
1570     RegCloseKey(hkey);
1571
1572     return r;
1573 }
1574
1575 /*************************************************************************
1576  *  MsiEnumRelatedProductsA   [MSI.@]
1577  *
1578  */
1579 UINT WINAPI MsiEnumRelatedProductsA(LPCSTR szUpgradeCode, DWORD dwReserved,
1580                                     DWORD iProductIndex, LPSTR lpProductBuf)
1581 {
1582     LPWSTR szwUpgradeCode = NULL;
1583     WCHAR productW[GUID_SIZE];
1584     UINT r;
1585
1586     TRACE("%s %u %u %p\n", debugstr_a(szUpgradeCode), dwReserved,
1587           iProductIndex, lpProductBuf);
1588
1589     if (szUpgradeCode)
1590     {
1591         szwUpgradeCode = strdupAtoW( szUpgradeCode );
1592         if( !szwUpgradeCode )
1593             return ERROR_OUTOFMEMORY;
1594     }
1595
1596     r = MsiEnumRelatedProductsW( szwUpgradeCode, dwReserved,
1597                                  iProductIndex, productW );
1598     if (r == ERROR_SUCCESS)
1599     {
1600         WideCharToMultiByte( CP_ACP, 0, productW, GUID_SIZE,
1601                              lpProductBuf, GUID_SIZE, NULL, NULL );
1602     }
1603     msi_free( szwUpgradeCode);
1604     return r;
1605 }
1606
1607 /***********************************************************************
1608  * MsiEnumPatchesExA            [MSI.@]
1609  */
1610 UINT WINAPI MsiEnumPatchesExA(LPCSTR szProductCode, LPCSTR szUserSid,
1611         DWORD dwContext, DWORD dwFilter, DWORD dwIndex, LPSTR szPatchCode,
1612         LPSTR szTargetProductCode, MSIINSTALLCONTEXT *pdwTargetProductContext,
1613         LPSTR szTargetUserSid, LPDWORD pcchTargetUserSid)
1614 {
1615     LPWSTR prodcode = NULL;
1616     LPWSTR usersid = NULL;
1617     LPWSTR targsid = NULL;
1618     WCHAR patch[GUID_SIZE];
1619     WCHAR targprod[GUID_SIZE];
1620     DWORD len;
1621     UINT r;
1622
1623     TRACE("(%s, %s, %d, %d, %d, %p, %p, %p, %p, %p)\n",
1624           debugstr_a(szProductCode), debugstr_a(szUserSid), dwContext, dwFilter,
1625           dwIndex, szPatchCode, szTargetProductCode, pdwTargetProductContext,
1626           szTargetUserSid, pcchTargetUserSid);
1627
1628     if (szTargetUserSid && !pcchTargetUserSid)
1629         return ERROR_INVALID_PARAMETER;
1630
1631     if (szProductCode) prodcode = strdupAtoW(szProductCode);
1632     if (szUserSid) usersid = strdupAtoW(szUserSid);
1633
1634     r = MsiEnumPatchesExW(prodcode, usersid, dwContext, dwFilter, dwIndex,
1635                           patch, targprod, pdwTargetProductContext,
1636                           NULL, &len);
1637     if (r != ERROR_SUCCESS)
1638         goto done;
1639
1640     WideCharToMultiByte(CP_ACP, 0, patch, -1, szPatchCode,
1641                         GUID_SIZE, NULL, NULL);
1642     WideCharToMultiByte(CP_ACP, 0, targprod, -1, szTargetProductCode,
1643                         GUID_SIZE, NULL, NULL);
1644
1645     if (!szTargetUserSid)
1646     {
1647         if (pcchTargetUserSid)
1648             *pcchTargetUserSid = len;
1649
1650         goto done;
1651     }
1652
1653     targsid = msi_alloc(++len * sizeof(WCHAR));
1654     if (!targsid)
1655     {
1656         r = ERROR_OUTOFMEMORY;
1657         goto done;
1658     }
1659
1660     r = MsiEnumPatchesExW(prodcode, usersid, dwContext, dwFilter, dwIndex,
1661                           patch, targprod, pdwTargetProductContext,
1662                           targsid, &len);
1663     if (r != ERROR_SUCCESS || !szTargetUserSid)
1664         goto done;
1665
1666     WideCharToMultiByte(CP_ACP, 0, targsid, -1, szTargetUserSid,
1667                         *pcchTargetUserSid, NULL, NULL);
1668
1669     len = lstrlenW(targsid);
1670     if (*pcchTargetUserSid < len + 1)
1671     {
1672         r = ERROR_MORE_DATA;
1673         *pcchTargetUserSid = len * sizeof(WCHAR);
1674     }
1675     else
1676         *pcchTargetUserSid = len;
1677
1678 done:
1679     msi_free(prodcode);
1680     msi_free(usersid);
1681     msi_free(targsid);
1682
1683     return r;
1684 }
1685
1686 static UINT msi_get_patch_state(LPCWSTR prodcode, LPCWSTR usersid,
1687                                 MSIINSTALLCONTEXT context,
1688                                 LPWSTR patch, MSIPATCHSTATE *state)
1689 {
1690     DWORD type, val, size;
1691     HKEY prod, hkey = 0;
1692     HKEY udpatch = 0;
1693     LONG res;
1694     UINT r = ERROR_NO_MORE_ITEMS;
1695
1696     *state = MSIPATCHSTATE_INVALID;
1697
1698     r = MSIREG_OpenUserDataProductKey(prodcode, context,
1699                                       usersid, &prod, FALSE);
1700     if (r != ERROR_SUCCESS)
1701         return ERROR_NO_MORE_ITEMS;
1702
1703     res = RegOpenKeyExW(prod, szPatches, 0, KEY_READ, &hkey);
1704     if (res != ERROR_SUCCESS)
1705         goto done;
1706
1707     res = RegOpenKeyExW(hkey, patch, 0, KEY_READ, &udpatch);
1708     if (res != ERROR_SUCCESS)
1709         goto done;
1710
1711     size = sizeof(DWORD);
1712     res = RegGetValueW(udpatch, NULL, szState, RRF_RT_DWORD, &type, &val, &size);
1713     if (res != ERROR_SUCCESS ||
1714         val < MSIPATCHSTATE_APPLIED || val > MSIPATCHSTATE_REGISTERED)
1715     {
1716         r = ERROR_BAD_CONFIGURATION;
1717         goto done;
1718     }
1719
1720     *state = val;
1721     r = ERROR_SUCCESS;
1722
1723 done:
1724     RegCloseKey(udpatch);
1725     RegCloseKey(hkey);
1726     RegCloseKey(prod);
1727
1728     return r;
1729 }
1730
1731 static UINT msi_check_product_patches(LPCWSTR prodcode, LPCWSTR usersid,
1732         MSIINSTALLCONTEXT context, DWORD filter, DWORD index, DWORD *idx,
1733         LPWSTR patch, LPWSTR targetprod, MSIINSTALLCONTEXT *targetctx,
1734         LPWSTR targetsid, DWORD *sidsize, LPWSTR *transforms)
1735 {
1736     MSIPATCHSTATE state = MSIPATCHSTATE_INVALID;
1737     LPWSTR ptr, patches = NULL;
1738     HKEY prod, patchkey = 0;
1739     HKEY localprod = 0, localpatch = 0;
1740     DWORD type, size;
1741     LONG res;
1742     UINT temp, r = ERROR_NO_MORE_ITEMS;
1743
1744     if (MSIREG_OpenProductKey(prodcode, usersid, context,
1745                               &prod, FALSE) != ERROR_SUCCESS)
1746         return ERROR_NO_MORE_ITEMS;
1747
1748     size = 0;
1749     res = RegGetValueW(prod, szPatches, szPatches, RRF_RT_ANY, &type, NULL,
1750                        &size);
1751     if (res != ERROR_SUCCESS)
1752         goto done;
1753
1754     if (type != REG_MULTI_SZ)
1755     {
1756         r = ERROR_BAD_CONFIGURATION;
1757         goto done;
1758     }
1759
1760     patches = msi_alloc(size);
1761     if (!patches)
1762     {
1763         r = ERROR_OUTOFMEMORY;
1764         goto done;
1765     }
1766
1767     res = RegGetValueW(prod, szPatches, szPatches, RRF_RT_ANY, &type,
1768                        patches, &size);
1769     if (res != ERROR_SUCCESS)
1770         goto done;
1771
1772     for (ptr = patches; *ptr && r == ERROR_NO_MORE_ITEMS; ptr += lstrlenW(ptr) + 1)
1773     {
1774         if (!unsquash_guid(ptr, patch))
1775         {
1776             r = ERROR_BAD_CONFIGURATION;
1777             goto done;
1778         }
1779
1780         size = 0;
1781         res = RegGetValueW(prod, szPatches, ptr, RRF_RT_REG_SZ,
1782                            &type, NULL, &size);
1783         if (res != ERROR_SUCCESS)
1784             continue;
1785
1786         if (transforms)
1787         {
1788             *transforms = msi_alloc(size);
1789             if (!*transforms)
1790             {
1791                 r = ERROR_OUTOFMEMORY;
1792                 goto done;
1793             }
1794
1795             res = RegGetValueW(prod, szPatches, ptr, RRF_RT_REG_SZ,
1796                                &type, *transforms, &size);
1797             if (res != ERROR_SUCCESS)
1798                 continue;
1799         }
1800
1801         if (context == MSIINSTALLCONTEXT_USERMANAGED)
1802         {
1803             if (!(filter & MSIPATCHSTATE_APPLIED))
1804             {
1805                 temp = msi_get_patch_state(prodcode, usersid, context,
1806                                            ptr, &state);
1807                 if (temp == ERROR_BAD_CONFIGURATION)
1808                 {
1809                     r = ERROR_BAD_CONFIGURATION;
1810                     goto done;
1811                 }
1812
1813                 if (temp != ERROR_SUCCESS || !(filter & state))
1814                     continue;
1815             }
1816         }
1817         else if (context == MSIINSTALLCONTEXT_USERUNMANAGED)
1818         {
1819             if (!(filter & MSIPATCHSTATE_APPLIED))
1820             {
1821                 temp = msi_get_patch_state(prodcode, usersid, context,
1822                                            ptr, &state);
1823                 if (temp == ERROR_BAD_CONFIGURATION)
1824                 {
1825                     r = ERROR_BAD_CONFIGURATION;
1826                     goto done;
1827                 }
1828
1829                 if (temp != ERROR_SUCCESS || !(filter & state))
1830                     continue;
1831             }
1832             else
1833             {
1834                 temp = MSIREG_OpenUserDataPatchKey(patch, context,
1835                                                    &patchkey, FALSE);
1836                 RegCloseKey(patchkey);
1837                 if (temp != ERROR_SUCCESS)
1838                     continue;
1839             }
1840         }
1841         else if (context == MSIINSTALLCONTEXT_MACHINE)
1842         {
1843             usersid = szEmpty;
1844
1845             if (MSIREG_OpenUserDataProductKey(prodcode, context, NULL, &localprod, FALSE) == ERROR_SUCCESS &&
1846                 RegOpenKeyExW(localprod, szPatches, 0, KEY_READ, &localpatch) == ERROR_SUCCESS &&
1847                 RegOpenKeyExW(localpatch, ptr, 0, KEY_READ, &patchkey) == ERROR_SUCCESS)
1848             {
1849                 res = RegGetValueW(patchkey, NULL, szState, RRF_RT_REG_DWORD,
1850                                    &type, &state, &size);
1851
1852                 if (!(filter & state))
1853                     res = ERROR_NO_MORE_ITEMS;
1854
1855                 RegCloseKey(patchkey);
1856             }
1857
1858             RegCloseKey(localpatch);
1859             RegCloseKey(localprod);
1860
1861             if (res != ERROR_SUCCESS)
1862                 continue;
1863         }
1864
1865         if (*idx < index)
1866         {
1867             (*idx)++;
1868             continue;
1869         }
1870
1871         r = ERROR_SUCCESS;
1872         if (targetprod)
1873             lstrcpyW(targetprod, prodcode);
1874
1875         if (targetctx)
1876             *targetctx = context;
1877
1878         if (targetsid)
1879         {
1880             lstrcpynW(targetsid, usersid, *sidsize);
1881             if (lstrlenW(usersid) >= *sidsize)
1882                 r = ERROR_MORE_DATA;
1883         }
1884
1885         if (sidsize)
1886         {
1887             *sidsize = lstrlenW(usersid);
1888             if (!targetsid)
1889                 *sidsize *= sizeof(WCHAR);
1890         }
1891     }
1892
1893 done:
1894     RegCloseKey(prod);
1895     msi_free(patches);
1896
1897     return r;
1898 }
1899
1900 static UINT msi_enum_patches(LPCWSTR szProductCode, LPCWSTR szUserSid,
1901         DWORD dwContext, DWORD dwFilter, DWORD dwIndex, DWORD *idx,
1902         LPWSTR szPatchCode, LPWSTR szTargetProductCode,
1903         MSIINSTALLCONTEXT *pdwTargetProductContext, LPWSTR szTargetUserSid,
1904         LPDWORD pcchTargetUserSid, LPWSTR *szTransforms)
1905 {
1906     LPWSTR usersid = NULL;
1907     UINT r = ERROR_INVALID_PARAMETER;
1908
1909     if (!szUserSid)
1910     {
1911         szUserSid = usersid = get_user_sid();
1912         if (!usersid) return ERROR_FUNCTION_FAILED;
1913     }
1914
1915     if (dwContext & MSIINSTALLCONTEXT_USERMANAGED)
1916     {
1917         r = msi_check_product_patches(szProductCode, szUserSid,
1918                                       MSIINSTALLCONTEXT_USERMANAGED, dwFilter,
1919                                       dwIndex, idx, szPatchCode,
1920                                       szTargetProductCode,
1921                                       pdwTargetProductContext, szTargetUserSid,
1922                                       pcchTargetUserSid, szTransforms);
1923         if (r != ERROR_NO_MORE_ITEMS)
1924             goto done;
1925     }
1926
1927     if (dwContext & MSIINSTALLCONTEXT_USERUNMANAGED)
1928     {
1929         r = msi_check_product_patches(szProductCode, szUserSid,
1930                                       MSIINSTALLCONTEXT_USERUNMANAGED, dwFilter,
1931                                       dwIndex, idx, szPatchCode,
1932                                       szTargetProductCode,
1933                                       pdwTargetProductContext, szTargetUserSid,
1934                                       pcchTargetUserSid, szTransforms);
1935         if (r != ERROR_NO_MORE_ITEMS)
1936             goto done;
1937     }
1938
1939     if (dwContext & MSIINSTALLCONTEXT_MACHINE)
1940     {
1941         r = msi_check_product_patches(szProductCode, szUserSid,
1942                                       MSIINSTALLCONTEXT_MACHINE, dwFilter,
1943                                       dwIndex, idx, szPatchCode,
1944                                       szTargetProductCode,
1945                                       pdwTargetProductContext, szTargetUserSid,
1946                                       pcchTargetUserSid, szTransforms);
1947         if (r != ERROR_NO_MORE_ITEMS)
1948             goto done;
1949     }
1950
1951 done:
1952     LocalFree(usersid);
1953     return r;
1954 }
1955
1956 /***********************************************************************
1957  * MsiEnumPatchesExW            [MSI.@]
1958  */
1959 UINT WINAPI MsiEnumPatchesExW(LPCWSTR szProductCode, LPCWSTR szUserSid,
1960         DWORD dwContext, DWORD dwFilter, DWORD dwIndex, LPWSTR szPatchCode,
1961         LPWSTR szTargetProductCode, MSIINSTALLCONTEXT *pdwTargetProductContext,
1962         LPWSTR szTargetUserSid, LPDWORD pcchTargetUserSid)
1963 {
1964     WCHAR squished_pc[GUID_SIZE];
1965     DWORD idx = 0;
1966     UINT r;
1967
1968     static DWORD last_index;
1969
1970     TRACE("(%s, %s, %d, %d, %d, %p, %p, %p, %p, %p)\n",
1971           debugstr_w(szProductCode), debugstr_w(szUserSid), dwContext, dwFilter,
1972           dwIndex, szPatchCode, szTargetProductCode, pdwTargetProductContext,
1973           szTargetUserSid, pcchTargetUserSid);
1974
1975     if (!szProductCode || !squash_guid(szProductCode, squished_pc))
1976         return ERROR_INVALID_PARAMETER;
1977
1978     if (szUserSid && !strcmpW( szUserSid, szLocalSid ))
1979         return ERROR_INVALID_PARAMETER;
1980
1981     if (dwContext & MSIINSTALLCONTEXT_MACHINE && szUserSid)
1982         return ERROR_INVALID_PARAMETER;
1983
1984     if (dwContext <= MSIINSTALLCONTEXT_NONE ||
1985         dwContext > MSIINSTALLCONTEXT_ALL)
1986         return ERROR_INVALID_PARAMETER;
1987
1988     if (dwFilter <= MSIPATCHSTATE_INVALID || dwFilter > MSIPATCHSTATE_ALL)
1989         return ERROR_INVALID_PARAMETER;
1990
1991     if (dwIndex && dwIndex - last_index != 1)
1992         return ERROR_INVALID_PARAMETER;
1993
1994     if (dwIndex == 0)
1995         last_index = 0;
1996
1997     r = msi_enum_patches(szProductCode, szUserSid, dwContext, dwFilter,
1998                          dwIndex, &idx, szPatchCode, szTargetProductCode,
1999                          pdwTargetProductContext, szTargetUserSid,
2000                          pcchTargetUserSid, NULL);
2001
2002     if (r == ERROR_SUCCESS)
2003         last_index = dwIndex;
2004     else
2005         last_index = 0;
2006
2007     return r;
2008 }
2009
2010 /***********************************************************************
2011  * MsiEnumPatchesA            [MSI.@]
2012  */
2013 UINT WINAPI MsiEnumPatchesA(LPCSTR szProduct, DWORD iPatchIndex,
2014         LPSTR lpPatchBuf, LPSTR lpTransformsBuf, LPDWORD pcchTransformsBuf)
2015 {
2016     LPWSTR product, transforms;
2017     WCHAR patch[GUID_SIZE];
2018     DWORD len;
2019     UINT r;
2020
2021     TRACE("(%s %d %p %p %p)\n", debugstr_a(szProduct), iPatchIndex,
2022           lpPatchBuf, lpTransformsBuf, pcchTransformsBuf);
2023
2024     if (!szProduct || !lpPatchBuf || !lpTransformsBuf || !pcchTransformsBuf)
2025         return ERROR_INVALID_PARAMETER;
2026
2027     product = strdupAtoW(szProduct);
2028     if (!product)
2029         return ERROR_OUTOFMEMORY;
2030
2031     len = *pcchTransformsBuf;
2032     transforms = msi_alloc( len * sizeof(WCHAR) );
2033     if (!transforms)
2034     {
2035         r = ERROR_OUTOFMEMORY;
2036         goto done;
2037     }
2038
2039     r = MsiEnumPatchesW(product, iPatchIndex, patch, transforms, &len);
2040     if (r != ERROR_SUCCESS && r != ERROR_MORE_DATA)
2041         goto done;
2042
2043     WideCharToMultiByte(CP_ACP, 0, patch, -1, lpPatchBuf,
2044                         GUID_SIZE, NULL, NULL);
2045
2046     if (!WideCharToMultiByte(CP_ACP, 0, transforms, -1, lpTransformsBuf,
2047                              *pcchTransformsBuf, NULL, NULL))
2048         r = ERROR_MORE_DATA;
2049
2050     if (r == ERROR_MORE_DATA)
2051     {
2052         lpTransformsBuf[*pcchTransformsBuf - 1] = '\0';
2053         *pcchTransformsBuf = len * 2;
2054     }
2055     else
2056         *pcchTransformsBuf = strlen( lpTransformsBuf );
2057
2058 done:
2059     msi_free(transforms);
2060     msi_free(product);
2061
2062     return r;
2063 }
2064
2065 /***********************************************************************
2066  * MsiEnumPatchesW            [MSI.@]
2067  */
2068 UINT WINAPI MsiEnumPatchesW(LPCWSTR szProduct, DWORD iPatchIndex,
2069         LPWSTR lpPatchBuf, LPWSTR lpTransformsBuf, LPDWORD pcchTransformsBuf)
2070 {
2071     WCHAR squished_pc[GUID_SIZE];
2072     LPWSTR transforms = NULL;
2073     HKEY prod;
2074     DWORD idx = 0;
2075     UINT r;
2076
2077     TRACE("(%s %d %p %p %p)\n", debugstr_w(szProduct), iPatchIndex,
2078           lpPatchBuf, lpTransformsBuf, pcchTransformsBuf);
2079
2080     if (!szProduct || !squash_guid(szProduct, squished_pc))
2081         return ERROR_INVALID_PARAMETER;
2082
2083     if (!lpPatchBuf || !lpTransformsBuf || !pcchTransformsBuf)
2084         return ERROR_INVALID_PARAMETER;
2085
2086     if (MSIREG_OpenProductKey(szProduct, NULL, MSIINSTALLCONTEXT_USERMANAGED,
2087                               &prod, FALSE) != ERROR_SUCCESS &&
2088         MSIREG_OpenProductKey(szProduct, NULL, MSIINSTALLCONTEXT_USERUNMANAGED,
2089                               &prod, FALSE) != ERROR_SUCCESS &&
2090         MSIREG_OpenProductKey(szProduct, NULL, MSIINSTALLCONTEXT_MACHINE,
2091                               &prod, FALSE) != ERROR_SUCCESS)
2092         return ERROR_UNKNOWN_PRODUCT;
2093
2094     RegCloseKey(prod);
2095
2096     r = msi_enum_patches(szProduct, NULL, MSIINSTALLCONTEXT_ALL,
2097                          MSIPATCHSTATE_ALL, iPatchIndex, &idx, lpPatchBuf,
2098                          NULL, NULL, NULL, NULL, &transforms);
2099     if (r != ERROR_SUCCESS)
2100         goto done;
2101
2102     lstrcpynW(lpTransformsBuf, transforms, *pcchTransformsBuf);
2103     if (*pcchTransformsBuf <= lstrlenW(transforms))
2104     {
2105         r = ERROR_MORE_DATA;
2106         *pcchTransformsBuf = lstrlenW(transforms);
2107     }
2108     else
2109         *pcchTransformsBuf = lstrlenW(transforms);
2110
2111 done:
2112     msi_free(transforms);
2113     return r;
2114 }
2115
2116 UINT WINAPI MsiEnumProductsExA( LPCSTR szProductCode, LPCSTR szUserSid,
2117         DWORD dwContext, DWORD dwIndex, CHAR szInstalledProductCode[39],
2118         MSIINSTALLCONTEXT* pdwInstalledContext, LPSTR szSid, LPDWORD pcchSid)
2119 {
2120     FIXME("%s %s %d %d %p %p %p %p\n", debugstr_a(szProductCode), debugstr_a(szUserSid),
2121           dwContext, dwIndex, szInstalledProductCode, pdwInstalledContext,
2122           szSid, pcchSid);
2123     return ERROR_NO_MORE_ITEMS;
2124 }
2125
2126 UINT WINAPI MsiEnumProductsExW( LPCWSTR szProductCode, LPCWSTR szUserSid,
2127         DWORD dwContext, DWORD dwIndex, WCHAR szInstalledProductCode[39],
2128         MSIINSTALLCONTEXT* pdwInstalledContext, LPWSTR szSid, LPDWORD pcchSid)
2129 {
2130     FIXME("%s %s %d %d %p %p %p %p\n", debugstr_w(szProductCode), debugstr_w(szUserSid),
2131           dwContext, dwIndex, szInstalledProductCode, pdwInstalledContext,
2132           szSid, pcchSid);
2133     return ERROR_NO_MORE_ITEMS;
2134 }