winhttp: Set callbacks needed by OpenSSL for multithreaded use.
[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((LPOLESTR)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     static const WCHAR emptyW[] = {0};
420     DWORD len;
421     if (!value) value = emptyW;
422     len = (lstrlenW(value) + 1) * sizeof (WCHAR);
423     return RegSetValueExW( hkey, name, 0, REG_SZ, (const BYTE *)value, len );
424 }
425
426 LONG msi_reg_set_val_multi_str( HKEY hkey, LPCWSTR name, LPCWSTR value )
427 {
428     LPCWSTR p = value;
429     while (*p) p += lstrlenW(p) + 1;
430     return RegSetValueExW( hkey, name, 0, REG_MULTI_SZ,
431                            (const BYTE *)value, (p + 1 - value) * sizeof(WCHAR) );
432 }
433
434 LONG msi_reg_set_val_dword( HKEY hkey, LPCWSTR name, DWORD val )
435 {
436     return RegSetValueExW( hkey, name, 0, REG_DWORD, (LPBYTE)&val, sizeof (DWORD) );
437 }
438
439 LONG msi_reg_set_subkey_val( HKEY hkey, LPCWSTR path, LPCWSTR name, LPCWSTR val )
440 {
441     HKEY hsubkey = 0;
442     LONG r;
443
444     r = RegCreateKeyW( hkey, path, &hsubkey );
445     if (r != ERROR_SUCCESS)
446         return r;
447     r = msi_reg_set_val_str( hsubkey, name, val );
448     RegCloseKey( hsubkey );
449     return r;
450 }
451
452 LPWSTR msi_reg_get_val_str( HKEY hkey, LPCWSTR name )
453 {
454     DWORD len = 0;
455     LPWSTR val;
456     LONG r;
457
458     r = RegQueryValueExW(hkey, name, NULL, NULL, NULL, &len);
459     if (r != ERROR_SUCCESS)
460         return NULL;
461
462     len += sizeof (WCHAR);
463     val = msi_alloc( len );
464     if (!val)
465         return NULL;
466     val[0] = 0;
467     RegQueryValueExW(hkey, name, NULL, NULL, (LPBYTE) val, &len);
468     return val;
469 }
470
471 BOOL msi_reg_get_val_dword( HKEY hkey, LPCWSTR name, DWORD *val)
472 {
473     DWORD type, len = sizeof (DWORD);
474     LONG r = RegQueryValueExW(hkey, name, NULL, &type, (LPBYTE) val, &len);
475     return r == ERROR_SUCCESS && type == REG_DWORD;
476 }
477
478 static UINT get_user_sid(LPWSTR *usersid)
479 {
480     HANDLE token;
481     BYTE buf[1024];
482     DWORD size;
483     PTOKEN_USER user;
484
485     if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token))
486         return ERROR_FUNCTION_FAILED;
487
488     size = sizeof(buf);
489     if (!GetTokenInformation(token, TokenUser, buf, size, &size)) {
490         CloseHandle(token);
491         return ERROR_FUNCTION_FAILED;
492     }
493
494     user = (PTOKEN_USER)buf;
495     if (!ConvertSidToStringSidW(user->User.Sid, usersid)) {
496         CloseHandle(token);
497         return ERROR_FUNCTION_FAILED;
498     }
499     CloseHandle(token);
500     return ERROR_SUCCESS;
501 }
502
503 UINT MSIREG_OpenUninstallKey(LPCWSTR szProduct, HKEY* key, BOOL create)
504 {
505     UINT rc;
506     WCHAR keypath[0x200];
507     TRACE("%s\n",debugstr_w(szProduct));
508
509     sprintfW(keypath,szUninstall_fmt,szProduct);
510
511     if (create)
512         rc = RegCreateKeyW(HKEY_LOCAL_MACHINE, keypath, key);
513     else
514         rc = RegOpenKeyW(HKEY_LOCAL_MACHINE, keypath, key);
515
516     return rc;
517 }
518
519 UINT MSIREG_DeleteUninstallKey(LPCWSTR szProduct)
520 {
521     WCHAR keypath[0x200];
522     TRACE("%s\n",debugstr_w(szProduct));
523
524     sprintfW(keypath,szUninstall_fmt,szProduct);
525
526     return RegDeleteTreeW(HKEY_LOCAL_MACHINE, keypath);
527 }
528
529 UINT MSIREG_OpenProductKey(LPCWSTR szProduct, LPCWSTR szUserSid,
530                            MSIINSTALLCONTEXT context, HKEY *key, BOOL create)
531 {
532     UINT r;
533     LPWSTR usersid = NULL;
534     HKEY root = HKEY_LOCAL_MACHINE;
535     WCHAR squished_pc[GUID_SIZE];
536     WCHAR keypath[MAX_PATH];
537
538     TRACE("(%s, %d, %d)\n", debugstr_w(szProduct), context, create);
539
540     if (!squash_guid(szProduct, squished_pc))
541         return ERROR_FUNCTION_FAILED;
542
543     TRACE("squished (%s)\n", debugstr_w(squished_pc));
544
545     if (context == MSIINSTALLCONTEXT_MACHINE)
546     {
547         sprintfW(keypath, szInstaller_LocalClassesProd_fmt, squished_pc);
548     }
549     else if (context == MSIINSTALLCONTEXT_USERUNMANAGED)
550     {
551         root = HKEY_CURRENT_USER;
552         sprintfW(keypath, szUserProduct_fmt, squished_pc);
553     }
554     else
555     {
556         if (!szUserSid)
557         {
558             r = get_user_sid(&usersid);
559             if (r != ERROR_SUCCESS || !usersid)
560             {
561                 ERR("Failed to retrieve user SID: %d\n", r);
562                 return r;
563             }
564
565             szUserSid = usersid;
566         }
567
568         sprintfW(keypath, szInstaller_LocalManagedProd_fmt,
569                  szUserSid, squished_pc);
570         LocalFree(usersid);
571     }
572
573     if (create)
574         return RegCreateKeyW(root, keypath, key);
575
576     return RegOpenKeyW(root, keypath, key);
577 }
578
579 UINT MSIREG_DeleteUserProductKey(LPCWSTR szProduct)
580 {
581     WCHAR squished_pc[GUID_SIZE];
582     WCHAR keypath[0x200];
583
584     TRACE("%s\n",debugstr_w(szProduct));
585     if (!squash_guid(szProduct,squished_pc))
586         return ERROR_FUNCTION_FAILED;
587     TRACE("squished (%s)\n", debugstr_w(squished_pc));
588
589     sprintfW(keypath,szUserProduct_fmt,squished_pc);
590
591     return RegDeleteTreeW(HKEY_CURRENT_USER, keypath);
592 }
593
594 UINT MSIREG_OpenUserPatchesKey(LPCWSTR szPatch, HKEY* key, BOOL create)
595 {
596     UINT rc;
597     WCHAR squished_pc[GUID_SIZE];
598     WCHAR keypath[0x200];
599
600     TRACE("%s\n",debugstr_w(szPatch));
601     if (!squash_guid(szPatch,squished_pc))
602         return ERROR_FUNCTION_FAILED;
603     TRACE("squished (%s)\n", debugstr_w(squished_pc));
604
605     sprintfW(keypath,szUserPatch_fmt,squished_pc);
606
607     if (create)
608         rc = RegCreateKeyW(HKEY_CURRENT_USER,keypath,key);
609     else
610         rc = RegOpenKeyW(HKEY_CURRENT_USER,keypath,key);
611
612     return rc;
613 }
614
615 UINT MSIREG_OpenFeaturesKey(LPCWSTR szProduct, MSIINSTALLCONTEXT context,
616                             HKEY *key, BOOL create)
617 {
618     UINT r;
619     LPWSTR usersid;
620     HKEY root = HKEY_LOCAL_MACHINE;
621     WCHAR squished_pc[GUID_SIZE];
622     WCHAR keypath[MAX_PATH];
623
624     TRACE("(%s, %d, %d)\n", debugstr_w(szProduct), context, create);
625
626     if (!squash_guid(szProduct, squished_pc))
627         return ERROR_FUNCTION_FAILED;
628
629     TRACE("squished (%s)\n", debugstr_w(squished_pc));
630
631     if (context == MSIINSTALLCONTEXT_MACHINE)
632     {
633         sprintfW(keypath, szInstaller_LocalClassesFeat_fmt, squished_pc);
634     }
635     else if (context == MSIINSTALLCONTEXT_USERUNMANAGED)
636     {
637         root = HKEY_CURRENT_USER;
638         sprintfW(keypath, szUserFeatures_fmt, squished_pc);
639     }
640     else
641     {
642         r = get_user_sid(&usersid);
643         if (r != ERROR_SUCCESS || !usersid)
644         {
645             ERR("Failed to retrieve user SID: %d\n", r);
646             return r;
647         }
648
649         sprintfW(keypath, szInstaller_LocalManagedFeat_fmt, usersid, squished_pc);
650         LocalFree(usersid);
651     }
652
653     if (create)
654         return RegCreateKeyW(root, keypath, key);
655
656     return RegOpenKeyW(root, keypath, key);
657 }
658
659 UINT MSIREG_DeleteUserFeaturesKey(LPCWSTR szProduct)
660 {
661     WCHAR squished_pc[GUID_SIZE];
662     WCHAR keypath[0x200];
663
664     TRACE("%s\n",debugstr_w(szProduct));
665     if (!squash_guid(szProduct,squished_pc))
666         return ERROR_FUNCTION_FAILED;
667     TRACE("squished (%s)\n", debugstr_w(squished_pc));
668
669     sprintfW(keypath,szUserFeatures_fmt,squished_pc);
670     return RegDeleteTreeW(HKEY_CURRENT_USER, keypath);
671 }
672
673 static UINT MSIREG_OpenInstallerFeaturesKey(LPCWSTR szProduct, HKEY* key, BOOL create)
674 {
675     UINT rc;
676     WCHAR squished_pc[GUID_SIZE];
677     WCHAR keypath[0x200];
678
679     TRACE("%s\n",debugstr_w(szProduct));
680     if (!squash_guid(szProduct,squished_pc))
681         return ERROR_FUNCTION_FAILED;
682     TRACE("squished (%s)\n", debugstr_w(squished_pc));
683
684     sprintfW(keypath,szInstaller_Features_fmt,squished_pc);
685
686     if (create)
687         rc = RegCreateKeyW(HKEY_LOCAL_MACHINE,keypath,key);
688     else
689         rc = RegOpenKeyW(HKEY_LOCAL_MACHINE,keypath,key);
690
691     return rc;
692 }
693
694 UINT MSIREG_OpenUserDataFeaturesKey(LPCWSTR szProduct, MSIINSTALLCONTEXT context,
695                                     HKEY *key, BOOL create)
696 {
697     UINT r;
698     LPWSTR usersid;
699     WCHAR squished_pc[GUID_SIZE];
700     WCHAR keypath[0x200];
701
702     TRACE("(%s, %d, %d)\n", debugstr_w(szProduct), context, create);
703
704     if (!squash_guid(szProduct, squished_pc))
705         return ERROR_FUNCTION_FAILED;
706
707     TRACE("squished (%s)\n", debugstr_w(squished_pc));
708
709     if (context == MSIINSTALLCONTEXT_MACHINE)
710     {
711         sprintfW(keypath, szUserDataFeatures_fmt, szLocalSid, squished_pc);
712     }
713     else
714     {
715         r = get_user_sid(&usersid);
716         if (r != ERROR_SUCCESS || !usersid)
717         {
718             ERR("Failed to retrieve user SID: %d\n", r);
719             return r;
720         }
721
722         sprintfW(keypath, szUserDataFeatures_fmt, usersid, squished_pc);
723         LocalFree(usersid);
724     }
725
726     if (create)
727         return RegCreateKeyW(HKEY_LOCAL_MACHINE, keypath, key);
728
729     return RegOpenKeyW(HKEY_LOCAL_MACHINE, keypath, key);
730 }
731
732 UINT MSIREG_OpenUserComponentsKey(LPCWSTR szComponent, HKEY* key, BOOL create)
733 {
734     UINT rc;
735     WCHAR squished_cc[GUID_SIZE];
736     WCHAR keypath[0x200];
737
738     TRACE("%s\n",debugstr_w(szComponent));
739     if (!squash_guid(szComponent,squished_cc))
740         return ERROR_FUNCTION_FAILED;
741     TRACE("squished (%s)\n", debugstr_w(squished_cc));
742
743     sprintfW(keypath,szUser_Components_fmt,squished_cc);
744
745     if (create)
746         rc = RegCreateKeyW(HKEY_CURRENT_USER,keypath,key);
747     else
748         rc = RegOpenKeyW(HKEY_CURRENT_USER,keypath,key);
749
750     return rc;
751 }
752
753 UINT MSIREG_OpenUserDataComponentKey(LPCWSTR szComponent, LPCWSTR szUserSid,
754                                      HKEY *key, BOOL create)
755 {
756     UINT rc;
757     WCHAR comp[GUID_SIZE];
758     WCHAR keypath[0x200];
759     LPWSTR usersid;
760
761     TRACE("%s\n", debugstr_w(szComponent));
762     if (!squash_guid(szComponent, comp))
763         return ERROR_FUNCTION_FAILED;
764     TRACE("squished (%s)\n", debugstr_w(comp));
765
766     if (!szUserSid)
767     {
768         rc = get_user_sid(&usersid);
769         if (rc != ERROR_SUCCESS || !usersid)
770         {
771             ERR("Failed to retrieve user SID: %d\n", rc);
772             return rc;
773         }
774
775         sprintfW(keypath, szUserDataComp_fmt, usersid, comp);
776         LocalFree(usersid);
777     }
778     else
779         sprintfW(keypath, szUserDataComp_fmt, szUserSid, comp);
780
781     if (create)
782         rc = RegCreateKeyW(HKEY_LOCAL_MACHINE, keypath, key);
783     else
784         rc = RegOpenKeyW(HKEY_LOCAL_MACHINE, keypath, key);
785
786     return rc;
787 }
788
789 UINT MSIREG_DeleteUserDataComponentKey(LPCWSTR szComponent, LPCWSTR szUserSid)
790 {
791     UINT rc;
792     WCHAR comp[GUID_SIZE];
793     WCHAR keypath[0x200];
794     LPWSTR usersid;
795
796     TRACE("%s\n", debugstr_w(szComponent));
797     if (!squash_guid(szComponent, comp))
798         return ERROR_FUNCTION_FAILED;
799     TRACE("squished (%s)\n", debugstr_w(comp));
800
801     if (!szUserSid)
802     {
803         rc = get_user_sid(&usersid);
804         if (rc != ERROR_SUCCESS || !usersid)
805         {
806             ERR("Failed to retrieve user SID: %d\n", rc);
807             return rc;
808         }
809
810         sprintfW(keypath, szUserDataComp_fmt, usersid, comp);
811         LocalFree(usersid);
812     }
813     else
814         sprintfW(keypath, szUserDataComp_fmt, szUserSid, comp);
815
816     return RegDeleteTreeW(HKEY_LOCAL_MACHINE, keypath);
817 }
818
819 UINT MSIREG_OpenUserDataProductKey(LPCWSTR szProduct, MSIINSTALLCONTEXT dwContext,
820                                    LPCWSTR szUserSid, HKEY *key, BOOL create)
821 {
822     UINT rc;
823     WCHAR squished_pc[GUID_SIZE];
824     WCHAR keypath[0x200];
825     LPWSTR usersid;
826
827     TRACE("%s\n", debugstr_w(szProduct));
828     if (!squash_guid(szProduct, squished_pc))
829         return ERROR_FUNCTION_FAILED;
830     TRACE("squished (%s)\n", debugstr_w(squished_pc));
831
832     if (dwContext == MSIINSTALLCONTEXT_MACHINE)
833         sprintfW(keypath, szUserDataProd_fmt, szLocalSid, squished_pc);
834     else if (szUserSid)
835         sprintfW(keypath, szUserDataProd_fmt, szUserSid, squished_pc);
836     else
837     {
838         rc = get_user_sid(&usersid);
839         if (rc != ERROR_SUCCESS || !usersid)
840         {
841             ERR("Failed to retrieve user SID: %d\n", rc);
842             return rc;
843         }
844
845         sprintfW(keypath, szUserDataProd_fmt, usersid, squished_pc);
846         LocalFree(usersid);
847     }
848
849     if (create)
850         rc = RegCreateKeyW(HKEY_LOCAL_MACHINE, keypath, key);
851     else
852         rc = RegOpenKeyW(HKEY_LOCAL_MACHINE, keypath, key);
853
854     return rc;
855 }
856
857 UINT MSIREG_OpenUserDataPatchKey(LPCWSTR szPatch, MSIINSTALLCONTEXT dwContext,
858                                  HKEY *key, BOOL create)
859 {
860     UINT rc;
861     WCHAR squished_patch[GUID_SIZE];
862     WCHAR keypath[0x200];
863     LPWSTR usersid;
864
865     TRACE("%s\n", debugstr_w(szPatch));
866     if (!squash_guid(szPatch, squished_patch))
867         return ERROR_FUNCTION_FAILED;
868     TRACE("squished (%s)\n", debugstr_w(squished_patch));
869
870     if (dwContext == MSIINSTALLCONTEXT_MACHINE)
871         sprintfW(keypath, szUserDataPatch_fmt, szLocalSid, squished_patch);
872     else
873     {
874         rc = get_user_sid(&usersid);
875         if (rc != ERROR_SUCCESS || !usersid)
876         {
877             ERR("Failed to retrieve user SID: %d\n", rc);
878             return rc;
879         }
880
881         sprintfW(keypath, szUserDataPatch_fmt, usersid, squished_patch);
882         LocalFree(usersid);
883     }
884
885     if (create)
886         return RegCreateKeyW(HKEY_LOCAL_MACHINE, keypath, key);
887
888     return RegOpenKeyW(HKEY_LOCAL_MACHINE, keypath, key);
889 }
890
891 UINT MSIREG_OpenInstallProps(LPCWSTR szProduct, MSIINSTALLCONTEXT dwContext,
892                              LPCWSTR szUserSid, HKEY *key, BOOL create)
893 {
894     UINT rc;
895     LPWSTR usersid;
896     WCHAR squished_pc[GUID_SIZE];
897     WCHAR keypath[0x200];
898
899     TRACE("%s\n", debugstr_w(szProduct));
900     if (!squash_guid(szProduct, squished_pc))
901         return ERROR_FUNCTION_FAILED;
902     TRACE("squished (%s)\n", debugstr_w(squished_pc));
903
904     if (dwContext == MSIINSTALLCONTEXT_MACHINE)
905         sprintfW(keypath, szInstallProperties_fmt, szLocalSid, squished_pc);
906     else if (szUserSid)
907         sprintfW(keypath, szInstallProperties_fmt, szUserSid, squished_pc);
908     else
909     {
910         rc = get_user_sid(&usersid);
911         if (rc != ERROR_SUCCESS || !usersid)
912         {
913             ERR("Failed to retrieve user SID: %d\n", rc);
914             return rc;
915         }
916
917         sprintfW(keypath, szInstallProperties_fmt, usersid, squished_pc);
918         LocalFree(usersid);
919     }
920
921     if (create)
922         return RegCreateKeyW(HKEY_LOCAL_MACHINE, keypath, key);
923
924     return RegOpenKeyW(HKEY_LOCAL_MACHINE, keypath, key);
925 }
926
927 UINT MSIREG_DeleteUserDataProductKey(LPCWSTR szProduct)
928 {
929     UINT rc;
930     WCHAR squished_pc[GUID_SIZE];
931     WCHAR keypath[0x200];
932     LPWSTR usersid;
933
934     TRACE("%s\n", debugstr_w(szProduct));
935     if (!squash_guid(szProduct, squished_pc))
936         return ERROR_FUNCTION_FAILED;
937     TRACE("squished (%s)\n", debugstr_w(squished_pc));
938
939     rc = get_user_sid(&usersid);
940     if (rc != ERROR_SUCCESS || !usersid)
941     {
942         ERR("Failed to retrieve user SID: %d\n", rc);
943         return rc;
944     }
945
946     sprintfW(keypath, szUserDataProd_fmt, usersid, squished_pc);
947
948     LocalFree(usersid);
949     return RegDeleteTreeW(HKEY_LOCAL_MACHINE, keypath);
950 }
951
952 UINT MSIREG_DeleteProductKey(LPCWSTR szProduct)
953 {
954     WCHAR squished_pc[GUID_SIZE];
955     WCHAR keypath[0x200];
956
957     TRACE("%s\n", debugstr_w(szProduct));
958     if (!squash_guid(szProduct, squished_pc))
959         return ERROR_FUNCTION_FAILED;
960     TRACE("squished (%s)\n", debugstr_w(squished_pc));
961
962     sprintfW(keypath, szInstaller_Products_fmt, squished_pc);
963
964     return RegDeleteTreeW(HKEY_LOCAL_MACHINE, keypath);
965 }
966
967 UINT MSIREG_OpenPatchesKey(LPCWSTR szPatch, HKEY* key, BOOL create)
968 {
969     UINT rc;
970     WCHAR squished_pc[GUID_SIZE];
971     WCHAR keypath[0x200];
972
973     TRACE("%s\n",debugstr_w(szPatch));
974     if (!squash_guid(szPatch,squished_pc))
975         return ERROR_FUNCTION_FAILED;
976     TRACE("squished (%s)\n", debugstr_w(squished_pc));
977
978     sprintfW(keypath,szInstaller_Patches_fmt,squished_pc);
979
980     if (create)
981         rc = RegCreateKeyW(HKEY_LOCAL_MACHINE,keypath,key);
982     else
983         rc = RegOpenKeyW(HKEY_LOCAL_MACHINE,keypath,key);
984
985     return rc;
986 }
987
988 UINT MSIREG_OpenUpgradeCodesKey(LPCWSTR szUpgradeCode, HKEY* key, BOOL create)
989 {
990     UINT rc;
991     WCHAR squished_pc[GUID_SIZE];
992     WCHAR keypath[0x200];
993
994     TRACE("%s\n",debugstr_w(szUpgradeCode));
995     if (!squash_guid(szUpgradeCode,squished_pc))
996         return ERROR_FUNCTION_FAILED;
997     TRACE("squished (%s)\n", debugstr_w(squished_pc));
998
999     sprintfW(keypath,szInstaller_UpgradeCodes_fmt,squished_pc);
1000
1001     if (create)
1002         rc = RegCreateKeyW(HKEY_LOCAL_MACHINE,keypath,key);
1003     else
1004         rc = RegOpenKeyW(HKEY_LOCAL_MACHINE,keypath,key);
1005
1006     return rc;
1007 }
1008
1009 UINT MSIREG_OpenUserUpgradeCodesKey(LPCWSTR szUpgradeCode, HKEY* key, BOOL create)
1010 {
1011     UINT rc;
1012     WCHAR squished_pc[GUID_SIZE];
1013     WCHAR keypath[0x200];
1014
1015     TRACE("%s\n",debugstr_w(szUpgradeCode));
1016     if (!squash_guid(szUpgradeCode,squished_pc))
1017         return ERROR_FUNCTION_FAILED;
1018     TRACE("squished (%s)\n", debugstr_w(squished_pc));
1019
1020     sprintfW(keypath,szInstaller_UserUpgradeCodes_fmt,squished_pc);
1021
1022     if (create)
1023         rc = RegCreateKeyW(HKEY_CURRENT_USER,keypath,key);
1024     else
1025         rc = RegOpenKeyW(HKEY_CURRENT_USER,keypath,key);
1026
1027     return rc;
1028 }
1029
1030 UINT MSIREG_DeleteUserUpgradeCodesKey(LPCWSTR szUpgradeCode)
1031 {
1032     WCHAR squished_pc[GUID_SIZE];
1033     WCHAR keypath[0x200];
1034
1035     TRACE("%s\n",debugstr_w(szUpgradeCode));
1036     if (!squash_guid(szUpgradeCode,squished_pc))
1037         return ERROR_FUNCTION_FAILED;
1038     TRACE("squished (%s)\n", debugstr_w(squished_pc));
1039
1040     sprintfW(keypath,szInstaller_UserUpgradeCodes_fmt,squished_pc);
1041
1042     return RegDeleteTreeW(HKEY_CURRENT_USER, keypath);
1043 }
1044
1045 UINT MSIREG_DeleteLocalClassesProductKey(LPCWSTR szProductCode)
1046 {
1047     WCHAR squished_pc[GUID_SIZE];
1048     WCHAR keypath[0x200];
1049
1050     TRACE("%s\n", debugstr_w(szProductCode));
1051
1052     if (!squash_guid(szProductCode, squished_pc))
1053         return ERROR_FUNCTION_FAILED;
1054
1055     TRACE("squished (%s)\n", debugstr_w(squished_pc));
1056
1057     sprintfW(keypath, szInstaller_LocalClassesProd_fmt, squished_pc);
1058
1059     return RegDeleteTreeW(HKEY_LOCAL_MACHINE, keypath);
1060 }
1061
1062 UINT MSIREG_DeleteLocalClassesFeaturesKey(LPCWSTR szProductCode)
1063 {
1064     WCHAR squished_pc[GUID_SIZE];
1065     WCHAR keypath[0x200];
1066
1067     TRACE("%s\n", debugstr_w(szProductCode));
1068
1069     if (!squash_guid(szProductCode, squished_pc))
1070         return ERROR_FUNCTION_FAILED;
1071
1072     TRACE("squished (%s)\n", debugstr_w(squished_pc));
1073
1074     sprintfW(keypath, szInstaller_LocalClassesFeat_fmt, squished_pc);
1075
1076     return RegDeleteTreeW(HKEY_LOCAL_MACHINE, keypath);
1077 }
1078
1079 UINT MSIREG_OpenClassesUpgradeCodesKey(LPCWSTR szUpgradeCode, HKEY* key, BOOL create)
1080 {
1081     WCHAR squished_pc[GUID_SIZE];
1082     WCHAR keypath[0x200];
1083
1084     TRACE("%s\n", debugstr_w(szUpgradeCode));
1085     if (!squash_guid(szUpgradeCode, squished_pc))
1086         return ERROR_FUNCTION_FAILED;
1087     TRACE("squished (%s)\n", debugstr_w(squished_pc));
1088
1089     sprintfW(keypath, szInstaller_ClassesUpgrade_fmt, squished_pc);
1090
1091     if (create)
1092         return RegCreateKeyW(HKEY_CLASSES_ROOT, keypath, key);
1093
1094     return RegOpenKeyW(HKEY_CLASSES_ROOT, keypath, key);
1095 }
1096
1097 /*************************************************************************
1098  *  MsiDecomposeDescriptorW   [MSI.@]
1099  *
1100  * Decomposes an MSI descriptor into product, feature and component parts.
1101  * An MSI descriptor is a string of the form:
1102  *   [base 85 guid] [feature code] '>' [base 85 guid]
1103  *
1104  * PARAMS
1105  *   szDescriptor  [I]  the descriptor to decompose
1106  *   szProduct     [O]  buffer of MAX_FEATURE_CHARS+1 for the product guid
1107  *   szFeature     [O]  buffer of MAX_FEATURE_CHARS+1 for the feature code
1108  *   szComponent   [O]  buffer of MAX_FEATURE_CHARS+1 for the component guid
1109  *   pUsed         [O]  the length of the descriptor
1110  *
1111  * RETURNS
1112  *   ERROR_SUCCESS             if everything worked correctly
1113  *   ERROR_INVALID_PARAMETER   if the descriptor was invalid
1114  *
1115  */
1116 UINT WINAPI MsiDecomposeDescriptorW( LPCWSTR szDescriptor, LPWSTR szProduct,
1117                 LPWSTR szFeature, LPWSTR szComponent, LPDWORD pUsed )
1118 {
1119     UINT r, len;
1120     LPWSTR p;
1121     GUID product, component;
1122
1123     TRACE("%s %p %p %p %p\n", debugstr_w(szDescriptor), szProduct,
1124           szFeature, szComponent, pUsed);
1125
1126     r = decode_base85_guid( szDescriptor, &product );
1127     if( !r )
1128         return ERROR_INVALID_PARAMETER;
1129
1130     TRACE("product %s\n", debugstr_guid( &product ));
1131
1132     p = strchrW(&szDescriptor[20],'>');
1133     if( !p )
1134         return ERROR_INVALID_PARAMETER;
1135
1136     len = (p - &szDescriptor[20]);
1137     if( len > MAX_FEATURE_CHARS )
1138         return ERROR_INVALID_PARAMETER;
1139
1140     TRACE("feature %s\n", debugstr_wn( &szDescriptor[20], len ));
1141
1142     r = decode_base85_guid( p+1, &component );
1143     if( !r )
1144         return ERROR_INVALID_PARAMETER;
1145
1146     TRACE("component %s\n", debugstr_guid( &component ));
1147
1148     if (szProduct)
1149         StringFromGUID2( &product, szProduct, MAX_FEATURE_CHARS+1 );
1150     if (szComponent)
1151         StringFromGUID2( &component, szComponent, MAX_FEATURE_CHARS+1 );
1152     if (szFeature)
1153     {
1154         memcpy( szFeature, &szDescriptor[20], len*sizeof(WCHAR) );
1155         szFeature[len] = 0;
1156     }
1157     len = ( &p[21] - szDescriptor );
1158
1159     TRACE("length = %d\n", len);
1160     *pUsed = len;
1161
1162     return ERROR_SUCCESS;
1163 }
1164
1165 UINT WINAPI MsiDecomposeDescriptorA( LPCSTR szDescriptor, LPSTR szProduct,
1166                 LPSTR szFeature, LPSTR szComponent, LPDWORD pUsed )
1167 {
1168     WCHAR product[MAX_FEATURE_CHARS+1];
1169     WCHAR feature[MAX_FEATURE_CHARS+1];
1170     WCHAR component[MAX_FEATURE_CHARS+1];
1171     LPWSTR str = NULL, p = NULL, f = NULL, c = NULL;
1172     UINT r;
1173
1174     TRACE("%s %p %p %p %p\n", debugstr_a(szDescriptor), szProduct,
1175           szFeature, szComponent, pUsed);
1176
1177     str = strdupAtoW( szDescriptor );
1178     if( szDescriptor && !str )
1179         return ERROR_OUTOFMEMORY;
1180
1181     if (szProduct)
1182         p = product;
1183     if (szFeature)
1184         f = feature;
1185     if (szComponent)
1186         c = component;
1187
1188     r = MsiDecomposeDescriptorW( str, p, f, c, pUsed );
1189
1190     if (r == ERROR_SUCCESS)
1191     {
1192         WideCharToMultiByte( CP_ACP, 0, p, -1,
1193                              szProduct, MAX_FEATURE_CHARS+1, NULL, NULL );
1194         WideCharToMultiByte( CP_ACP, 0, f, -1,
1195                              szFeature, MAX_FEATURE_CHARS+1, NULL, NULL );
1196         WideCharToMultiByte( CP_ACP, 0, c, -1,
1197                              szComponent, MAX_FEATURE_CHARS+1, NULL, NULL );
1198     }
1199
1200     msi_free( str );
1201
1202     return r;
1203 }
1204
1205 UINT WINAPI MsiEnumProductsA(DWORD index, LPSTR lpguid)
1206 {
1207     DWORD r;
1208     WCHAR szwGuid[GUID_SIZE];
1209
1210     TRACE("%d %p\n", index, lpguid);
1211
1212     if (NULL == lpguid)
1213         return ERROR_INVALID_PARAMETER;
1214     r = MsiEnumProductsW(index, szwGuid);
1215     if( r == ERROR_SUCCESS )
1216         WideCharToMultiByte(CP_ACP, 0, szwGuid, -1, lpguid, GUID_SIZE, NULL, NULL);
1217
1218     return r;
1219 }
1220
1221 UINT WINAPI MsiEnumProductsW(DWORD index, LPWSTR lpguid)
1222 {
1223     UINT r;
1224     WCHAR szKeyName[SQUISH_GUID_SIZE];
1225     HKEY key;
1226     DWORD machine_count, managed_count, unmanaged_count;
1227     WCHAR keypath[MAX_PATH];
1228     LPWSTR usersid = NULL;
1229
1230     static DWORD last_index;
1231
1232     TRACE("%d %p\n", index, lpguid);
1233
1234     if (NULL == lpguid)
1235         return ERROR_INVALID_PARAMETER;
1236
1237     if (index && index - last_index != 1)
1238         return ERROR_INVALID_PARAMETER;
1239
1240     r = RegCreateKeyW(HKEY_LOCAL_MACHINE, szInstaller_LocalClassesProd, &key);
1241     if( r != ERROR_SUCCESS )
1242         return ERROR_NO_MORE_ITEMS;
1243
1244     r = RegQueryInfoKeyW(key, NULL, NULL, NULL, &machine_count, NULL, NULL,
1245                          NULL, NULL, NULL, NULL, NULL);
1246     if( r != ERROR_SUCCESS )
1247     {
1248         RegCloseKey(key);
1249         return ERROR_NO_MORE_ITEMS;
1250     }
1251
1252     if (machine_count && index <= machine_count)
1253     {
1254         r = RegEnumKeyW(key, index, szKeyName, SQUISH_GUID_SIZE);
1255         if( r == ERROR_SUCCESS )
1256         {
1257             unsquash_guid(szKeyName, lpguid);
1258             last_index = index;
1259             RegCloseKey(key);
1260             return ERROR_SUCCESS;
1261         }
1262     }
1263     RegCloseKey(key);
1264
1265     r = get_user_sid(&usersid);
1266     if (r != ERROR_SUCCESS || !usersid)
1267     {
1268         ERR("Failed to retrieve user SID: %d\n", r);
1269         return r;
1270     }
1271     sprintfW(keypath, szInstaller_LocalManaged_fmt, usersid);
1272     LocalFree(usersid);
1273
1274     r = RegCreateKeyW(HKEY_LOCAL_MACHINE, keypath, &key);
1275     if( r != ERROR_SUCCESS )
1276         return ERROR_NO_MORE_ITEMS;
1277
1278     r = RegQueryInfoKeyW(key, NULL, NULL, NULL, &managed_count, NULL, NULL,
1279                          NULL, NULL, NULL, NULL, NULL);
1280     if( r != ERROR_SUCCESS )
1281     {
1282         RegCloseKey(key);
1283         return ERROR_NO_MORE_ITEMS;
1284     }
1285
1286     if (managed_count && index <= machine_count + managed_count)
1287     {
1288         r = RegEnumKeyW(key, index - machine_count, szKeyName, SQUISH_GUID_SIZE);
1289         if( r == ERROR_SUCCESS )
1290         {
1291             unsquash_guid(szKeyName, lpguid);
1292             last_index = index;
1293             RegCloseKey(key);
1294             return ERROR_SUCCESS;
1295         }
1296     }
1297     RegCloseKey(key);
1298
1299     r = RegCreateKeyW(HKEY_CURRENT_USER, szUserProduct, &key);
1300     if( r != ERROR_SUCCESS )
1301         return ERROR_NO_MORE_ITEMS;
1302
1303     r = RegQueryInfoKeyW(key, NULL, NULL, NULL, &unmanaged_count, NULL, NULL,
1304                          NULL, NULL, NULL, NULL, NULL);
1305     if( r != ERROR_SUCCESS )
1306     {
1307         RegCloseKey(key);
1308         return ERROR_NO_MORE_ITEMS;
1309     }
1310
1311     if (unmanaged_count && index <= machine_count + managed_count + unmanaged_count)
1312     {
1313         r = RegEnumKeyW(key, index - machine_count - managed_count, szKeyName, SQUISH_GUID_SIZE);
1314         if( r == ERROR_SUCCESS )
1315         {
1316             unsquash_guid(szKeyName, lpguid);
1317             last_index = index;
1318             RegCloseKey(key);
1319             return ERROR_SUCCESS;
1320         }
1321     }
1322     RegCloseKey(key);
1323
1324     return ERROR_NO_MORE_ITEMS;
1325 }
1326
1327 UINT WINAPI MsiEnumFeaturesA(LPCSTR szProduct, DWORD index, 
1328       LPSTR szFeature, LPSTR szParent)
1329 {
1330     DWORD r;
1331     WCHAR szwFeature[GUID_SIZE], szwParent[GUID_SIZE];
1332     LPWSTR szwProduct = NULL;
1333
1334     TRACE("%s %d %p %p\n", debugstr_a(szProduct), index, szFeature, szParent);
1335
1336     if( szProduct )
1337     {
1338         szwProduct = strdupAtoW( szProduct );
1339         if( !szwProduct )
1340             return ERROR_OUTOFMEMORY;
1341     }
1342
1343     r = MsiEnumFeaturesW(szwProduct, index, szwFeature, szwParent);
1344     if( r == ERROR_SUCCESS )
1345     {
1346         WideCharToMultiByte(CP_ACP, 0, szwFeature, -1,
1347                             szFeature, GUID_SIZE, NULL, NULL);
1348         WideCharToMultiByte(CP_ACP, 0, szwParent, -1,
1349                             szParent, GUID_SIZE, NULL, NULL);
1350     }
1351
1352     msi_free( szwProduct);
1353
1354     return r;
1355 }
1356
1357 UINT WINAPI MsiEnumFeaturesW(LPCWSTR szProduct, DWORD index, 
1358       LPWSTR szFeature, LPWSTR szParent)
1359 {
1360     HKEY hkeyProduct = 0;
1361     DWORD r, sz;
1362
1363     TRACE("%s %d %p %p\n", debugstr_w(szProduct), index, szFeature, szParent);
1364
1365     if( !szProduct )
1366         return ERROR_INVALID_PARAMETER;
1367
1368     r = MSIREG_OpenInstallerFeaturesKey(szProduct,&hkeyProduct,FALSE);
1369     if( r != ERROR_SUCCESS )
1370         return ERROR_NO_MORE_ITEMS;
1371
1372     sz = GUID_SIZE;
1373     r = RegEnumValueW(hkeyProduct, index, szFeature, &sz, NULL, NULL, NULL, NULL);
1374     RegCloseKey(hkeyProduct);
1375
1376     return r;
1377 }
1378
1379 UINT WINAPI MsiEnumComponentsA(DWORD index, LPSTR lpguid)
1380 {
1381     DWORD r;
1382     WCHAR szwGuid[GUID_SIZE];
1383
1384     TRACE("%d %p\n", index, lpguid);
1385
1386     r = MsiEnumComponentsW(index, szwGuid);
1387     if( r == ERROR_SUCCESS )
1388         WideCharToMultiByte(CP_ACP, 0, szwGuid, -1, lpguid, GUID_SIZE, NULL, NULL);
1389
1390     return r;
1391 }
1392
1393 UINT WINAPI MsiEnumComponentsW(DWORD index, LPWSTR lpguid)
1394 {
1395     HKEY hkeyComponents = 0;
1396     DWORD r;
1397     WCHAR szKeyName[SQUISH_GUID_SIZE];
1398
1399     TRACE("%d %p\n", index, lpguid);
1400
1401     r = RegCreateKeyW(HKEY_LOCAL_MACHINE, szInstaller_Components, &hkeyComponents);
1402     if( r != ERROR_SUCCESS )
1403         return ERROR_NO_MORE_ITEMS;
1404
1405     r = RegEnumKeyW(hkeyComponents, index, szKeyName, SQUISH_GUID_SIZE);
1406     if( r == ERROR_SUCCESS )
1407         unsquash_guid(szKeyName, lpguid);
1408     RegCloseKey(hkeyComponents);
1409
1410     return r;
1411 }
1412
1413 UINT WINAPI MsiEnumClientsA(LPCSTR szComponent, DWORD index, LPSTR szProduct)
1414 {
1415     DWORD r;
1416     WCHAR szwProduct[GUID_SIZE];
1417     LPWSTR szwComponent = NULL;
1418
1419     TRACE("%s %d %p\n", debugstr_a(szComponent), index, szProduct);
1420
1421     if ( !szProduct )
1422         return ERROR_INVALID_PARAMETER;
1423
1424     if( szComponent )
1425     {
1426         szwComponent = strdupAtoW( szComponent );
1427         if( !szwComponent )
1428             return ERROR_OUTOFMEMORY;
1429     }
1430
1431     r = MsiEnumClientsW(szComponent?szwComponent:NULL, index, szwProduct);
1432     if( r == ERROR_SUCCESS )
1433     {
1434         WideCharToMultiByte(CP_ACP, 0, szwProduct, -1,
1435                             szProduct, GUID_SIZE, NULL, NULL);
1436     }
1437
1438     msi_free( szwComponent);
1439
1440     return r;
1441 }
1442
1443 UINT WINAPI MsiEnumClientsW(LPCWSTR szComponent, DWORD index, LPWSTR szProduct)
1444 {
1445     HKEY hkeyComp = 0;
1446     DWORD r, sz;
1447     WCHAR szValName[SQUISH_GUID_SIZE];
1448
1449     TRACE("%s %d %p\n", debugstr_w(szComponent), index, szProduct);
1450
1451     if (!szComponent || !*szComponent || !szProduct)
1452         return ERROR_INVALID_PARAMETER;
1453
1454     if (MSIREG_OpenUserDataComponentKey(szComponent, NULL, &hkeyComp, FALSE) != ERROR_SUCCESS &&
1455         MSIREG_OpenUserDataComponentKey(szComponent, szLocalSid, &hkeyComp, FALSE) != ERROR_SUCCESS)
1456         return ERROR_UNKNOWN_COMPONENT;
1457
1458     /* see if there are any products at all */
1459     sz = SQUISH_GUID_SIZE;
1460     r = RegEnumValueW(hkeyComp, 0, szValName, &sz, NULL, NULL, NULL, NULL);
1461     if (r != ERROR_SUCCESS)
1462     {
1463         RegCloseKey(hkeyComp);
1464
1465         if (index != 0)
1466             return ERROR_INVALID_PARAMETER;
1467
1468         return ERROR_UNKNOWN_COMPONENT;
1469     }
1470
1471     sz = SQUISH_GUID_SIZE;
1472     r = RegEnumValueW(hkeyComp, index, szValName, &sz, NULL, NULL, NULL, NULL);
1473     if( r == ERROR_SUCCESS )
1474         unsquash_guid(szValName, szProduct);
1475
1476     RegCloseKey(hkeyComp);
1477
1478     return r;
1479 }
1480
1481 static UINT MSI_EnumComponentQualifiers( LPCWSTR szComponent, DWORD iIndex,
1482                 awstring *lpQualBuf, LPDWORD pcchQual,
1483                 awstring *lpAppBuf, LPDWORD pcchAppBuf )
1484 {
1485     DWORD name_sz, val_sz, name_max, val_max, type, ofs;
1486     LPWSTR name = NULL, val = NULL;
1487     UINT r, r2;
1488     HKEY key;
1489
1490     TRACE("%s %08x %p %p %p %p\n", debugstr_w(szComponent), iIndex,
1491           lpQualBuf, pcchQual, lpAppBuf, pcchAppBuf);
1492
1493     if (!szComponent)
1494         return ERROR_INVALID_PARAMETER;
1495
1496     r = MSIREG_OpenUserComponentsKey( szComponent, &key, FALSE );
1497     if (r != ERROR_SUCCESS)
1498         return ERROR_UNKNOWN_COMPONENT;
1499
1500     /* figure out how big the name is we want to return */
1501     name_max = 0x10;
1502     r = ERROR_OUTOFMEMORY;
1503     name = msi_alloc( name_max * sizeof(WCHAR) );
1504     if (!name)
1505         goto end;
1506
1507     val_max = 0x10;
1508     r = ERROR_OUTOFMEMORY;
1509     val = msi_alloc( val_max );
1510     if (!val)
1511         goto end;
1512
1513     /* loop until we allocate enough memory */
1514     while (1)
1515     {
1516         name_sz = name_max;
1517         val_sz = val_max;
1518         r = RegEnumValueW( key, iIndex, name, &name_sz,
1519                            NULL, &type, (LPBYTE)val, &val_sz );
1520         if (r == ERROR_SUCCESS)
1521             break;
1522         if (r != ERROR_MORE_DATA)
1523             goto end;
1524  
1525         if (type != REG_MULTI_SZ)
1526         {
1527             ERR("component data has wrong type (%d)\n", type);
1528             goto end;
1529         }
1530
1531         r = ERROR_OUTOFMEMORY;
1532         if ((name_sz+1) >= name_max)
1533         {
1534             name_max *= 2;
1535             msi_free( name );
1536             name = msi_alloc( name_max * sizeof (WCHAR) );
1537             if (!name)
1538                 goto end;
1539             continue;
1540         }
1541         if (val_sz > val_max)
1542         {
1543             val_max = val_sz + sizeof (WCHAR);
1544             msi_free( val );
1545             val = msi_alloc( val_max * sizeof (WCHAR) );
1546             if (!val)
1547                 goto end;
1548             continue;
1549         }
1550         ERR("should be enough data, but isn't %d %d\n", name_sz, val_sz );
1551         goto end;
1552     }
1553
1554     ofs = 0;
1555     r = MsiDecomposeDescriptorW( val, NULL, NULL, NULL, &ofs );
1556     if (r != ERROR_SUCCESS)
1557         goto end;
1558
1559     TRACE("Providing %s and %s\n", debugstr_w(name), debugstr_w(val+ofs));
1560
1561     r = msi_strcpy_to_awstring( name, lpQualBuf, pcchQual );
1562     r2 = msi_strcpy_to_awstring( val+ofs, lpAppBuf, pcchAppBuf );
1563
1564     if (r2 != ERROR_SUCCESS)
1565         r = r2;
1566
1567 end:
1568     msi_free(val);
1569     msi_free(name);
1570     RegCloseKey(key);
1571
1572     return r;
1573 }
1574
1575 /*************************************************************************
1576  *  MsiEnumComponentQualifiersA [MSI.@]
1577  */
1578 UINT WINAPI MsiEnumComponentQualifiersA( LPCSTR szComponent, DWORD iIndex,
1579                 LPSTR lpQualifierBuf, LPDWORD pcchQualifierBuf,
1580                 LPSTR lpApplicationDataBuf, LPDWORD pcchApplicationDataBuf )
1581 {
1582     awstring qual, appdata;
1583     LPWSTR comp;
1584     UINT r;
1585
1586     TRACE("%s %08x %p %p %p %p\n", debugstr_a(szComponent), iIndex,
1587           lpQualifierBuf, pcchQualifierBuf, lpApplicationDataBuf,
1588           pcchApplicationDataBuf);
1589
1590     comp = strdupAtoW( szComponent );
1591     if (szComponent && !comp)
1592         return ERROR_OUTOFMEMORY;
1593
1594     qual.unicode = FALSE;
1595     qual.str.a = lpQualifierBuf;
1596
1597     appdata.unicode = FALSE;
1598     appdata.str.a = lpApplicationDataBuf;
1599
1600     r = MSI_EnumComponentQualifiers( comp, iIndex,
1601               &qual, pcchQualifierBuf, &appdata, pcchApplicationDataBuf );
1602     msi_free( comp );
1603     return r;
1604 }
1605
1606 /*************************************************************************
1607  *  MsiEnumComponentQualifiersW [MSI.@]
1608  */
1609 UINT WINAPI MsiEnumComponentQualifiersW( LPCWSTR szComponent, DWORD iIndex,
1610                 LPWSTR lpQualifierBuf, LPDWORD pcchQualifierBuf,
1611                 LPWSTR lpApplicationDataBuf, LPDWORD pcchApplicationDataBuf )
1612 {
1613     awstring qual, appdata;
1614
1615     TRACE("%s %08x %p %p %p %p\n", debugstr_w(szComponent), iIndex,
1616           lpQualifierBuf, pcchQualifierBuf, lpApplicationDataBuf,
1617           pcchApplicationDataBuf);
1618
1619     qual.unicode = TRUE;
1620     qual.str.w = lpQualifierBuf;
1621
1622     appdata.unicode = TRUE;
1623     appdata.str.w = lpApplicationDataBuf;
1624
1625     return MSI_EnumComponentQualifiers( szComponent, iIndex,
1626                  &qual, pcchQualifierBuf, &appdata, pcchApplicationDataBuf );
1627 }
1628
1629 /*************************************************************************
1630  *  MsiEnumRelatedProductsW   [MSI.@]
1631  *
1632  */
1633 UINT WINAPI MsiEnumRelatedProductsW(LPCWSTR szUpgradeCode, DWORD dwReserved,
1634                                     DWORD iProductIndex, LPWSTR lpProductBuf)
1635 {
1636     UINT r;
1637     HKEY hkey;
1638     DWORD dwSize = SQUISH_GUID_SIZE;
1639     WCHAR szKeyName[SQUISH_GUID_SIZE];
1640
1641     TRACE("%s %u %u %p\n", debugstr_w(szUpgradeCode), dwReserved,
1642           iProductIndex, lpProductBuf);
1643
1644     if (NULL == szUpgradeCode)
1645         return ERROR_INVALID_PARAMETER;
1646     if (NULL == lpProductBuf)
1647         return ERROR_INVALID_PARAMETER;
1648
1649     r = MSIREG_OpenUpgradeCodesKey(szUpgradeCode, &hkey, FALSE);
1650     if (r != ERROR_SUCCESS)
1651         return ERROR_NO_MORE_ITEMS;
1652
1653     r = RegEnumValueW(hkey, iProductIndex, szKeyName, &dwSize, NULL, NULL, NULL, NULL);
1654     if( r == ERROR_SUCCESS )
1655         unsquash_guid(szKeyName, lpProductBuf);
1656     RegCloseKey(hkey);
1657
1658     return r;
1659 }
1660
1661 /*************************************************************************
1662  *  MsiEnumRelatedProductsA   [MSI.@]
1663  *
1664  */
1665 UINT WINAPI MsiEnumRelatedProductsA(LPCSTR szUpgradeCode, DWORD dwReserved,
1666                                     DWORD iProductIndex, LPSTR lpProductBuf)
1667 {
1668     LPWSTR szwUpgradeCode = NULL;
1669     WCHAR productW[GUID_SIZE];
1670     UINT r;
1671
1672     TRACE("%s %u %u %p\n", debugstr_a(szUpgradeCode), dwReserved,
1673           iProductIndex, lpProductBuf);
1674
1675     if (szUpgradeCode)
1676     {
1677         szwUpgradeCode = strdupAtoW( szUpgradeCode );
1678         if( !szwUpgradeCode )
1679             return ERROR_OUTOFMEMORY;
1680     }
1681
1682     r = MsiEnumRelatedProductsW( szwUpgradeCode, dwReserved,
1683                                  iProductIndex, productW );
1684     if (r == ERROR_SUCCESS)
1685     {
1686         WideCharToMultiByte( CP_ACP, 0, productW, GUID_SIZE,
1687                              lpProductBuf, GUID_SIZE, NULL, NULL );
1688     }
1689     msi_free( szwUpgradeCode);
1690     return r;
1691 }
1692
1693 /***********************************************************************
1694  * MsiEnumPatchesExA            [MSI.@]
1695  */
1696 UINT WINAPI MsiEnumPatchesExA(LPCSTR szProductCode, LPCSTR szUserSid,
1697         DWORD dwContext, DWORD dwFilter, DWORD dwIndex, LPSTR szPatchCode,
1698         LPSTR szTargetProductCode, MSIINSTALLCONTEXT *pdwTargetProductContext,
1699         LPSTR szTargetUserSid, LPDWORD pcchTargetUserSid)
1700 {
1701     LPWSTR prodcode = NULL;
1702     LPWSTR usersid = NULL;
1703     LPWSTR targsid = NULL;
1704     WCHAR patch[GUID_SIZE];
1705     WCHAR targprod[GUID_SIZE];
1706     DWORD len;
1707     UINT r;
1708
1709     TRACE("(%s, %s, %d, %d, %d, %p, %p, %p, %p, %p)\n",
1710           debugstr_a(szProductCode), debugstr_a(szUserSid), dwContext, dwFilter,
1711           dwIndex, szPatchCode, szTargetProductCode, pdwTargetProductContext,
1712           szTargetUserSid, pcchTargetUserSid);
1713
1714     if (szTargetUserSid && !pcchTargetUserSid)
1715         return ERROR_INVALID_PARAMETER;
1716
1717     if (szProductCode) prodcode = strdupAtoW(szProductCode);
1718     if (szUserSid) usersid = strdupAtoW(szUserSid);
1719
1720     r = MsiEnumPatchesExW(prodcode, usersid, dwContext, dwFilter, dwIndex,
1721                           patch, targprod, pdwTargetProductContext,
1722                           NULL, &len);
1723     if (r != ERROR_SUCCESS)
1724         goto done;
1725
1726     WideCharToMultiByte(CP_ACP, 0, patch, -1, szPatchCode,
1727                         GUID_SIZE, NULL, NULL);
1728     WideCharToMultiByte(CP_ACP, 0, targprod, -1, szTargetProductCode,
1729                         GUID_SIZE, NULL, NULL);
1730
1731     if (!szTargetUserSid)
1732     {
1733         if (pcchTargetUserSid)
1734             *pcchTargetUserSid = len;
1735
1736         goto done;
1737     }
1738
1739     targsid = msi_alloc(++len * sizeof(WCHAR));
1740     if (!targsid)
1741     {
1742         r = ERROR_OUTOFMEMORY;
1743         goto done;
1744     }
1745
1746     r = MsiEnumPatchesExW(prodcode, usersid, dwContext, dwFilter, dwIndex,
1747                           patch, targprod, pdwTargetProductContext,
1748                           targsid, &len);
1749     if (r != ERROR_SUCCESS || !szTargetUserSid)
1750         goto done;
1751
1752     WideCharToMultiByte(CP_ACP, 0, targsid, -1, szTargetUserSid,
1753                         *pcchTargetUserSid, NULL, NULL);
1754
1755     len = lstrlenW(targsid);
1756     if (*pcchTargetUserSid < len + 1)
1757     {
1758         r = ERROR_MORE_DATA;
1759         *pcchTargetUserSid = len * sizeof(WCHAR);
1760     }
1761     else
1762         *pcchTargetUserSid = len;
1763
1764 done:
1765     msi_free(prodcode);
1766     msi_free(usersid);
1767     msi_free(targsid);
1768
1769     return r;
1770 }
1771
1772 static UINT msi_get_patch_state(LPCWSTR prodcode, LPCWSTR usersid,
1773                                 MSIINSTALLCONTEXT context,
1774                                 LPWSTR patch, MSIPATCHSTATE *state)
1775 {
1776     DWORD type, val, size;
1777     HKEY prod, hkey = 0;
1778     HKEY udpatch = 0;
1779     LONG res;
1780     UINT r = ERROR_NO_MORE_ITEMS;
1781
1782     static const WCHAR szPatches[] = {'P','a','t','c','h','e','s',0};
1783     static const WCHAR szState[] = {'S','t','a','t','e',0};
1784
1785     *state = MSIPATCHSTATE_INVALID;
1786
1787     r = MSIREG_OpenUserDataProductKey(prodcode, context,
1788                                       usersid, &prod, FALSE);
1789     if (r != ERROR_SUCCESS)
1790         return ERROR_NO_MORE_ITEMS;
1791
1792     res = RegOpenKeyExW(prod, szPatches, 0, KEY_READ, &hkey);
1793     if (res != ERROR_SUCCESS)
1794         goto done;
1795
1796     res = RegOpenKeyExW(hkey, patch, 0, KEY_READ, &udpatch);
1797     if (res != ERROR_SUCCESS)
1798         goto done;
1799
1800     size = sizeof(DWORD);
1801     res = RegGetValueW(udpatch, NULL, szState, RRF_RT_DWORD, &type, &val, &size);
1802     if (res != ERROR_SUCCESS ||
1803         val < MSIPATCHSTATE_APPLIED || val > MSIPATCHSTATE_REGISTERED)
1804     {
1805         r = ERROR_BAD_CONFIGURATION;
1806         goto done;
1807     }
1808
1809     *state = val;
1810     r = ERROR_SUCCESS;
1811
1812 done:
1813     RegCloseKey(udpatch);
1814     RegCloseKey(hkey);
1815     RegCloseKey(prod);
1816
1817     return r;
1818 }
1819
1820 static UINT msi_check_product_patches(LPCWSTR prodcode, LPCWSTR usersid,
1821         MSIINSTALLCONTEXT context, DWORD filter, DWORD index, DWORD *idx,
1822         LPWSTR patch, LPWSTR targetprod, MSIINSTALLCONTEXT *targetctx,
1823         LPWSTR targetsid, DWORD *sidsize, LPWSTR *transforms)
1824 {
1825     MSIPATCHSTATE state = MSIPATCHSTATE_INVALID;
1826     LPWSTR ptr, patches = NULL;
1827     HKEY prod, patchkey = 0;
1828     HKEY localprod = 0, localpatch = 0;
1829     DWORD type, size;
1830     LONG res;
1831     UINT temp, r = ERROR_NO_MORE_ITEMS;
1832
1833     static const WCHAR szPatches[] = {'P','a','t','c','h','e','s',0};
1834     static const WCHAR szState[] = {'S','t','a','t','e',0};
1835     static const WCHAR szEmpty[] = {0};
1836
1837     if (MSIREG_OpenProductKey(prodcode, usersid, context,
1838                               &prod, FALSE) != ERROR_SUCCESS)
1839         return ERROR_NO_MORE_ITEMS;
1840
1841     size = 0;
1842     res = RegGetValueW(prod, szPatches, szPatches, RRF_RT_ANY, &type, NULL,
1843                        &size);
1844     if (res != ERROR_SUCCESS)
1845         goto done;
1846
1847     if (type != REG_MULTI_SZ)
1848     {
1849         r = ERROR_BAD_CONFIGURATION;
1850         goto done;
1851     }
1852
1853     patches = msi_alloc(size);
1854     if (!patches)
1855     {
1856         r = ERROR_OUTOFMEMORY;
1857         goto done;
1858     }
1859
1860     res = RegGetValueW(prod, szPatches, szPatches, RRF_RT_ANY, &type,
1861                        patches, &size);
1862     if (res != ERROR_SUCCESS)
1863         goto done;
1864
1865     ptr = patches;
1866     for (ptr = patches; *ptr && r == ERROR_NO_MORE_ITEMS; ptr += lstrlenW(ptr))
1867     {
1868         if (!unsquash_guid(ptr, patch))
1869         {
1870             r = ERROR_BAD_CONFIGURATION;
1871             goto done;
1872         }
1873
1874         size = 0;
1875         res = RegGetValueW(prod, szPatches, ptr, RRF_RT_REG_SZ,
1876                            &type, NULL, &size);
1877         if (res != ERROR_SUCCESS)
1878             continue;
1879
1880         if (transforms)
1881         {
1882             *transforms = msi_alloc(size);
1883             if (!*transforms)
1884             {
1885                 r = ERROR_OUTOFMEMORY;
1886                 goto done;
1887             }
1888
1889             res = RegGetValueW(prod, szPatches, ptr, RRF_RT_REG_SZ,
1890                                &type, *transforms, &size);
1891             if (res != ERROR_SUCCESS)
1892                 continue;
1893         }
1894
1895         if (context == MSIINSTALLCONTEXT_USERMANAGED)
1896         {
1897             if (!(filter & MSIPATCHSTATE_APPLIED))
1898             {
1899                 temp = msi_get_patch_state(prodcode, usersid, context,
1900                                            ptr, &state);
1901                 if (temp == ERROR_BAD_CONFIGURATION)
1902                 {
1903                     r = ERROR_BAD_CONFIGURATION;
1904                     goto done;
1905                 }
1906
1907                 if (temp != ERROR_SUCCESS || !(filter & state))
1908                     continue;
1909             }
1910         }
1911         else if (context == MSIINSTALLCONTEXT_USERUNMANAGED)
1912         {
1913             if (!(filter & MSIPATCHSTATE_APPLIED))
1914             {
1915                 temp = msi_get_patch_state(prodcode, usersid, context,
1916                                            ptr, &state);
1917                 if (temp == ERROR_BAD_CONFIGURATION)
1918                 {
1919                     r = ERROR_BAD_CONFIGURATION;
1920                     goto done;
1921                 }
1922
1923                 if (temp != ERROR_SUCCESS || !(filter & state))
1924                     continue;
1925             }
1926             else
1927             {
1928                 temp = MSIREG_OpenUserDataPatchKey(patch, context,
1929                                                    &patchkey, FALSE);
1930                 RegCloseKey(patchkey);
1931                 if (temp != ERROR_SUCCESS)
1932                     continue;
1933             }
1934         }
1935         else if (context == MSIINSTALLCONTEXT_MACHINE)
1936         {
1937             usersid = szEmpty;
1938
1939             if (MSIREG_OpenUserDataProductKey(prodcode, context, NULL, &localprod, FALSE) == ERROR_SUCCESS &&
1940                 RegOpenKeyExW(localprod, szPatches, 0, KEY_READ, &localpatch) == ERROR_SUCCESS &&
1941                 RegOpenKeyExW(localpatch, ptr, 0, KEY_READ, &patchkey) == ERROR_SUCCESS)
1942             {
1943                 res = RegGetValueW(patchkey, NULL, szState, RRF_RT_REG_DWORD,
1944                                    &type, &state, &size);
1945
1946                 if (!(filter & state))
1947                     res = ERROR_NO_MORE_ITEMS;
1948
1949                 RegCloseKey(patchkey);
1950             }
1951
1952             RegCloseKey(localpatch);
1953             RegCloseKey(localprod);
1954
1955             if (res != ERROR_SUCCESS)
1956                 continue;
1957         }
1958
1959         if (*idx < index)
1960         {
1961             (*idx)++;
1962             continue;
1963         }
1964
1965         r = ERROR_SUCCESS;
1966         if (targetprod)
1967             lstrcpyW(targetprod, prodcode);
1968
1969         if (targetctx)
1970             *targetctx = context;
1971
1972         if (targetsid)
1973         {
1974             lstrcpynW(targetsid, usersid, *sidsize);
1975             if (lstrlenW(usersid) >= *sidsize)
1976                 r = ERROR_MORE_DATA;
1977         }
1978
1979         if (sidsize)
1980         {
1981             *sidsize = lstrlenW(usersid);
1982             if (!targetsid)
1983                 *sidsize *= sizeof(WCHAR);
1984         }
1985     }
1986
1987 done:
1988     RegCloseKey(prod);
1989     msi_free(patches);
1990
1991     return r;
1992 }
1993
1994 static UINT msi_enum_patches(LPCWSTR szProductCode, LPCWSTR szUserSid,
1995         DWORD dwContext, DWORD dwFilter, DWORD dwIndex, DWORD *idx,
1996         LPWSTR szPatchCode, LPWSTR szTargetProductCode,
1997         MSIINSTALLCONTEXT *pdwTargetProductContext, LPWSTR szTargetUserSid,
1998         LPDWORD pcchTargetUserSid, LPWSTR *szTransforms)
1999 {
2000     LPWSTR usersid = NULL;
2001     UINT r = ERROR_INVALID_PARAMETER;
2002
2003     if (!szUserSid)
2004     {
2005         get_user_sid(&usersid);
2006         szUserSid = usersid;
2007     }
2008
2009     if (dwContext & MSIINSTALLCONTEXT_USERMANAGED)
2010     {
2011         r = msi_check_product_patches(szProductCode, szUserSid,
2012                                       MSIINSTALLCONTEXT_USERMANAGED, dwFilter,
2013                                       dwIndex, idx, szPatchCode,
2014                                       szTargetProductCode,
2015                                       pdwTargetProductContext, szTargetUserSid,
2016                                       pcchTargetUserSid, szTransforms);
2017         if (r != ERROR_NO_MORE_ITEMS)
2018             goto done;
2019     }
2020
2021     if (dwContext & MSIINSTALLCONTEXT_USERUNMANAGED)
2022     {
2023         r = msi_check_product_patches(szProductCode, szUserSid,
2024                                       MSIINSTALLCONTEXT_USERUNMANAGED, dwFilter,
2025                                       dwIndex, idx, szPatchCode,
2026                                       szTargetProductCode,
2027                                       pdwTargetProductContext, szTargetUserSid,
2028                                       pcchTargetUserSid, szTransforms);
2029         if (r != ERROR_NO_MORE_ITEMS)
2030             goto done;
2031     }
2032
2033     if (dwContext & MSIINSTALLCONTEXT_MACHINE)
2034     {
2035         r = msi_check_product_patches(szProductCode, szUserSid,
2036                                       MSIINSTALLCONTEXT_MACHINE, dwFilter,
2037                                       dwIndex, idx, szPatchCode,
2038                                       szTargetProductCode,
2039                                       pdwTargetProductContext, szTargetUserSid,
2040                                       pcchTargetUserSid, szTransforms);
2041         if (r != ERROR_NO_MORE_ITEMS)
2042             goto done;
2043     }
2044
2045 done:
2046     LocalFree(usersid);
2047     return r;
2048 }
2049
2050 /***********************************************************************
2051  * MsiEnumPatchesExW            [MSI.@]
2052  */
2053 UINT WINAPI MsiEnumPatchesExW(LPCWSTR szProductCode, LPCWSTR szUserSid,
2054         DWORD dwContext, DWORD dwFilter, DWORD dwIndex, LPWSTR szPatchCode,
2055         LPWSTR szTargetProductCode, MSIINSTALLCONTEXT *pdwTargetProductContext,
2056         LPWSTR szTargetUserSid, LPDWORD pcchTargetUserSid)
2057 {
2058     WCHAR squished_pc[GUID_SIZE];
2059     DWORD idx = 0;
2060     UINT r;
2061
2062     static int last_index = 0;
2063
2064     TRACE("(%s, %s, %d, %d, %d, %p, %p, %p, %p, %p)\n",
2065           debugstr_w(szProductCode), debugstr_w(szUserSid), dwContext, dwFilter,
2066           dwIndex, szPatchCode, szTargetProductCode, pdwTargetProductContext,
2067           szTargetUserSid, pcchTargetUserSid);
2068
2069     if (!szProductCode || !squash_guid(szProductCode, squished_pc))
2070         return ERROR_INVALID_PARAMETER;
2071
2072     if (!lstrcmpW(szUserSid, szLocalSid))
2073         return ERROR_INVALID_PARAMETER;
2074
2075     if (dwContext & MSIINSTALLCONTEXT_MACHINE && szUserSid)
2076         return ERROR_INVALID_PARAMETER;
2077
2078     if (dwContext <= MSIINSTALLCONTEXT_NONE ||
2079         dwContext > MSIINSTALLCONTEXT_ALL)
2080         return ERROR_INVALID_PARAMETER;
2081
2082     if (dwFilter <= MSIPATCHSTATE_INVALID || dwFilter > MSIPATCHSTATE_ALL)
2083         return ERROR_INVALID_PARAMETER;
2084
2085     if (dwIndex && dwIndex - last_index != 1)
2086         return ERROR_INVALID_PARAMETER;
2087
2088     if (dwIndex == 0)
2089         last_index = 0;
2090
2091     r = msi_enum_patches(szProductCode, szUserSid, dwContext, dwFilter,
2092                          dwIndex, &idx, szPatchCode, szTargetProductCode,
2093                          pdwTargetProductContext, szTargetUserSid,
2094                          pcchTargetUserSid, NULL);
2095
2096     if (r == ERROR_SUCCESS)
2097         last_index = dwIndex;
2098
2099     return r;
2100 }
2101
2102 /***********************************************************************
2103  * MsiEnumPatchesA            [MSI.@]
2104  */
2105 UINT WINAPI MsiEnumPatchesA(LPCSTR szProduct, DWORD iPatchIndex,
2106         LPSTR lpPatchBuf, LPSTR lpTransformsBuf, LPDWORD pcchTransformsBuf)
2107 {
2108     LPWSTR product, transforms;
2109     WCHAR patch[GUID_SIZE];
2110     DWORD len;
2111     UINT r;
2112
2113     TRACE("(%s %d %p %p %p)\n", debugstr_a(szProduct), iPatchIndex,
2114           lpPatchBuf, lpTransformsBuf, pcchTransformsBuf);
2115
2116     if (!szProduct || !lpPatchBuf || !lpTransformsBuf || !pcchTransformsBuf)
2117         return ERROR_INVALID_PARAMETER;
2118
2119     product = strdupAtoW(szProduct);
2120     if (!product)
2121         return ERROR_OUTOFMEMORY;
2122
2123     len = *pcchTransformsBuf;
2124     transforms = msi_alloc( len * sizeof(WCHAR) );
2125     if (!transforms)
2126     {
2127         r = ERROR_OUTOFMEMORY;
2128         goto done;
2129     }
2130
2131     r = MsiEnumPatchesW(product, iPatchIndex, patch, transforms, &len);
2132     if (r != ERROR_SUCCESS && r != ERROR_MORE_DATA)
2133         goto done;
2134
2135     WideCharToMultiByte(CP_ACP, 0, patch, -1, lpPatchBuf,
2136                         GUID_SIZE, NULL, NULL);
2137
2138     if (!WideCharToMultiByte(CP_ACP, 0, transforms, -1, lpTransformsBuf,
2139                              *pcchTransformsBuf, NULL, NULL))
2140         r = ERROR_MORE_DATA;
2141
2142     if (r == ERROR_MORE_DATA)
2143     {
2144         lpTransformsBuf[*pcchTransformsBuf - 1] = '\0';
2145         *pcchTransformsBuf = len * 2;
2146     }
2147     else
2148         *pcchTransformsBuf = strlen( lpTransformsBuf );
2149
2150 done:
2151     msi_free(transforms);
2152     msi_free(product);
2153
2154     return r;
2155 }
2156
2157 /***********************************************************************
2158  * MsiEnumPatchesW            [MSI.@]
2159  */
2160 UINT WINAPI MsiEnumPatchesW(LPCWSTR szProduct, DWORD iPatchIndex,
2161         LPWSTR lpPatchBuf, LPWSTR lpTransformsBuf, LPDWORD pcchTransformsBuf)
2162 {
2163     WCHAR squished_pc[GUID_SIZE];
2164     LPWSTR transforms = NULL;
2165     HKEY prod;
2166     DWORD idx = 0;
2167     UINT r;
2168
2169     TRACE("(%s %d %p %p %p)\n", debugstr_w(szProduct), iPatchIndex,
2170           lpPatchBuf, lpTransformsBuf, pcchTransformsBuf);
2171
2172     if (!szProduct || !squash_guid(szProduct, squished_pc))
2173         return ERROR_INVALID_PARAMETER;
2174
2175     if (!lpPatchBuf || !lpTransformsBuf || !pcchTransformsBuf)
2176         return ERROR_INVALID_PARAMETER;
2177
2178     if (MSIREG_OpenProductKey(szProduct, NULL, MSIINSTALLCONTEXT_USERMANAGED,
2179                               &prod, FALSE) != ERROR_SUCCESS &&
2180         MSIREG_OpenProductKey(szProduct, NULL, MSIINSTALLCONTEXT_USERUNMANAGED,
2181                               &prod, FALSE) != ERROR_SUCCESS &&
2182         MSIREG_OpenProductKey(szProduct, NULL, MSIINSTALLCONTEXT_MACHINE,
2183                               &prod, FALSE) != ERROR_SUCCESS)
2184         return ERROR_UNKNOWN_PRODUCT;
2185
2186     RegCloseKey(prod);
2187
2188     r = msi_enum_patches(szProduct, NULL, MSIINSTALLCONTEXT_ALL,
2189                          MSIPATCHSTATE_ALL, iPatchIndex, &idx, lpPatchBuf,
2190                          NULL, NULL, NULL, NULL, &transforms);
2191     if (r != ERROR_SUCCESS)
2192         goto done;
2193
2194     lstrcpynW(lpTransformsBuf, transforms, *pcchTransformsBuf);
2195     if (*pcchTransformsBuf <= lstrlenW(transforms))
2196     {
2197         r = ERROR_MORE_DATA;
2198         *pcchTransformsBuf = lstrlenW(transforms);
2199     }
2200     else
2201         *pcchTransformsBuf = lstrlenW(transforms);
2202
2203 done:
2204     msi_free(transforms);
2205     return r;
2206 }
2207
2208 UINT WINAPI MsiEnumProductsExA( LPCSTR szProductCode, LPCSTR szUserSid,
2209         DWORD dwContext, DWORD dwIndex, CHAR szInstalledProductCode[39],
2210         MSIINSTALLCONTEXT* pdwInstalledContext, LPSTR szSid, LPDWORD pcchSid)
2211 {
2212     FIXME("%s %s %d %d %p %p %p %p\n", debugstr_a(szProductCode), debugstr_a(szUserSid),
2213           dwContext, dwIndex, szInstalledProductCode, pdwInstalledContext,
2214           szSid, pcchSid);
2215     return ERROR_NO_MORE_ITEMS;
2216 }
2217
2218 UINT WINAPI MsiEnumProductsExW( LPCWSTR szProductCode, LPCWSTR szUserSid,
2219         DWORD dwContext, DWORD dwIndex, WCHAR szInstalledProductCode[39],
2220         MSIINSTALLCONTEXT* pdwInstalledContext, LPWSTR szSid, LPDWORD pcchSid)
2221 {
2222     FIXME("%s %s %d %d %p %p %p %p\n", debugstr_w(szProductCode), debugstr_w(szUserSid),
2223           dwContext, dwIndex, szInstalledProductCode, pdwInstalledContext,
2224           szSid, pcchSid);
2225     return ERROR_NO_MORE_ITEMS;
2226 }