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