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