msi: Allow whitespace after the property name when setting a property in the dialog.
[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 szInstaller_Features[] = {
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 'F','e','a','t','u','r','e','s',0 };
62
63 static const WCHAR szUserDataFeatures_fmt[] = {
64 'S','o','f','t','w','a','r','e','\\',
65 'M','i','c','r','o','s','o','f','t','\\',
66 'W','i','n','d','o','w','s','\\',
67 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
68 'I','n','s','t','a','l','l','e','r','\\',
69 'U','s','e','r','D','a','t','a','\\',
70 '%','s','\\','P','r','o','d','u','c','t','s','\\',
71 '%','s','\\','F','e','a','t','u','r','e','s',0};
72
73 static const WCHAR szInstaller_Features_fmt[] = {
74 'S','o','f','t','w','a','r','e','\\',
75 'M','i','c','r','o','s','o','f','t','\\',
76 'W','i','n','d','o','w','s','\\',
77 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
78 'I','n','s','t','a','l','l','e','r','\\',
79 'F','e','a','t','u','r','e','s','\\',
80 '%','s',0};
81
82 static const WCHAR szInstaller_Components[] = {
83 'S','o','f','t','w','a','r','e','\\',
84 'M','i','c','r','o','s','o','f','t','\\',
85 'W','i','n','d','o','w','s','\\',
86 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
87 'I','n','s','t','a','l','l','e','r','\\',
88 'C','o','m','p','o','n','e','n','t','s',0 };
89
90 static const WCHAR szInstaller_Components_fmt[] = {
91 'S','o','f','t','w','a','r','e','\\',
92 'M','i','c','r','o','s','o','f','t','\\',
93 'W','i','n','d','o','w','s','\\',
94 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
95 'I','n','s','t','a','l','l','e','r','\\',
96 'C','o','m','p','o','n','e','n','t','s','\\',
97 '%','s',0};
98
99 static const WCHAR szUser_Components_fmt[] = {
100 'S','o','f','t','w','a','r','e','\\',
101 'M','i','c','r','o','s','o','f','t','\\',
102 'I','n','s','t','a','l','l','e','r','\\',
103 'C','o','m','p','o','n','e','n','t','s','\\',
104 '%','s',0};
105
106 static const WCHAR szUserDataComp_fmt[] = {
107 'S','o','f','t','w','a','r','e','\\',
108 'M','i','c','r','o','s','o','f','t','\\',
109 'W','i','n','d','o','w','s','\\',
110 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
111 'I','n','s','t','a','l','l','e','r','\\',
112 'U','s','e','r','D','a','t','a','\\',
113 '%','s','\\','C','o','m','p','o','n','e','n','t','s','\\','%','s',0};
114
115 static const WCHAR szUninstall_fmt[] = {
116 'S','o','f','t','w','a','r','e','\\',
117 'M','i','c','r','o','s','o','f','t','\\',
118 'W','i','n','d','o','w','s','\\',
119 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
120 'U','n','i','n','s','t','a','l','l','\\',
121 '%','s',0 };
122
123 static const WCHAR szUserProduct_fmt[] = {
124 'S','o','f','t','w','a','r','e','\\',
125 'M','i','c','r','o','s','o','f','t','\\',
126 'I','n','s','t','a','l','l','e','r','\\',
127 'P','r','o','d','u','c','t','s','\\',
128 '%','s',0};
129
130 static const WCHAR szUserPatch_fmt[] = {
131 'S','o','f','t','w','a','r','e','\\',
132 'M','i','c','r','o','s','o','f','t','\\',
133 'I','n','s','t','a','l','l','e','r','\\',
134 'P','a','t','c','h','e','s','\\',
135 '%','s',0};
136
137 static const WCHAR szInstaller_Products[] = {
138 'S','o','f','t','w','a','r','e','\\',
139 'M','i','c','r','o','s','o','f','t','\\',
140 'W','i','n','d','o','w','s','\\',
141 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
142 'I','n','s','t','a','l','l','e','r','\\',
143 'P','r','o','d','u','c','t','s',0};
144
145 static const WCHAR szInstaller_Products_fmt[] = {
146 'S','o','f','t','w','a','r','e','\\',
147 'M','i','c','r','o','s','o','f','t','\\',
148 'W','i','n','d','o','w','s','\\',
149 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
150 'I','n','s','t','a','l','l','e','r','\\',
151 'P','r','o','d','u','c','t','s','\\',
152 '%','s',0};
153
154 static const WCHAR szInstaller_Patches_fmt[] = {
155 'S','o','f','t','w','a','r','e','\\',
156 'M','i','c','r','o','s','o','f','t','\\',
157 'W','i','n','d','o','w','s','\\',
158 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
159 'I','n','s','t','a','l','l','e','r','\\',
160 'P','a','t','c','h','e','s','\\',
161 '%','s',0};
162
163 static const WCHAR szInstaller_UpgradeCodes_fmt[] = {
164 'S','o','f','t','w','a','r','e','\\',
165 'M','i','c','r','o','s','o','f','t','\\',
166 'W','i','n','d','o','w','s','\\',
167 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
168 'I','n','s','t','a','l','l','e','r','\\',
169 'U','p','g','r','a','d','e','C','o','d','e','s','\\',
170 '%','s',0};
171
172 static const WCHAR szInstaller_UserUpgradeCodes_fmt[] = {
173 'S','o','f','t','w','a','r','e','\\',
174 'M','i','c','r','o','s','o','f','t','\\',
175 'I','n','s','t','a','l','l','e','r','\\',
176 'U','p','g','r','a','d','e','C','o','d','e','s','\\',
177 '%','s',0};
178
179 static const WCHAR szUserDataProd_fmt[] = {
180 'S','o','f','t','w','a','r','e','\\',
181 'M','i','c','r','o','s','o','f','t','\\',
182 'W','i','n','d','o','w','s','\\',
183 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
184 'I','n','s','t','a','l','l','e','r','\\',
185 'U','s','e','r','D','a','t','a','\\',
186 '%','s','\\','P','r','o','d','u','c','t','s','\\','%','s',0};
187
188 static const WCHAR szInstallProperties_fmt[] = {
189 'S','o','f','t','w','a','r','e','\\',
190 'M','i','c','r','o','s','o','f','t','\\',
191 'W','i','n','d','o','w','s','\\',
192 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
193 'I','n','s','t','a','l','l','e','r','\\',
194 'U','s','e','r','D','a','t','a','\\',
195 '%','s','\\','P','r','o','d','u','c','t','s','\\','%','s','\\',
196 'I','n','s','t','a','l','l','P','r','o','p','e','r','t','i','e','s',0};
197
198 static const WCHAR szInstaller_LocalSystemProductCodes_fmt[] = {
199 'S','o','f','t','w','a','r','e','\\',
200 'M','i','c','r','o','s','o','f','t','\\',
201 'W','i','n','d','o','w','s','\\',
202 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
203 'I','n','s','t','a','l','l','e','r','\\',
204 'U','s','e','r','D','a','t','a','\\',
205 'S','-','1','-','5','-','1','8','\\','P','r','o','d','u','c','t','s','\\',
206 '%','s','\\','I','n','s','t','a','l','l','P','r','o','p','e','r','t','i','e','s',0};
207
208 static const WCHAR szInstaller_LocalSystemComponent_fmt[] = {
209 'S','o','f','t','w','a','r','e','\\',
210 'M','i','c','r','o','s','o','f','t','\\',
211 'W','i','n','d','o','w','s','\\',
212 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
213 'I','n','s','t','a','l','l','e','r','\\',
214 'U','s','e','r','D','a','t','a','\\',
215 'S','-','1','-','5','-','1','8','\\',
216 'C','o','m','p','o','n','e','n','t','s','\\','%','s',0};
217
218 static const WCHAR szInstaller_LocalClassesProd_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 'P','r','o','d','u','c','t','s','\\','%','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 #define SQUISH_GUID_SIZE 33
235
236 BOOL unsquash_guid(LPCWSTR in, LPWSTR out)
237 {
238     DWORD i,n=0;
239
240     out[n++]='{';
241     for(i=0; i<8; i++)
242         out[n++] = in[7-i];
243     out[n++]='-';
244     for(i=0; i<4; i++)
245         out[n++] = in[11-i];
246     out[n++]='-';
247     for(i=0; i<4; i++)
248         out[n++] = in[15-i];
249     out[n++]='-';
250     for(i=0; i<2; i++)
251     {
252         out[n++] = in[17+i*2];
253         out[n++] = in[16+i*2];
254     }
255     out[n++]='-';
256     for( ; i<8; i++)
257     {
258         out[n++] = in[17+i*2];
259         out[n++] = in[16+i*2];
260     }
261     out[n++]='}';
262     out[n]=0;
263     return TRUE;
264 }
265
266 BOOL squash_guid(LPCWSTR in, LPWSTR out)
267 {
268     DWORD i,n=1;
269     GUID guid;
270
271     out[0] = 0;
272
273     if (FAILED(CLSIDFromString((LPOLESTR)in, &guid)))
274         return FALSE;
275
276     for(i=0; i<8; i++)
277         out[7-i] = in[n++];
278     n++;
279     for(i=0; i<4; i++)
280         out[11-i] = in[n++];
281     n++;
282     for(i=0; i<4; i++)
283         out[15-i] = in[n++];
284     n++;
285     for(i=0; i<2; i++)
286     {
287         out[17+i*2] = in[n++];
288         out[16+i*2] = in[n++];
289     }
290     n++;
291     for( ; i<8; i++)
292     {
293         out[17+i*2] = in[n++];
294         out[16+i*2] = in[n++];
295     }
296     out[32]=0;
297     return TRUE;
298 }
299
300
301 /* tables for encoding and decoding base85 */
302 static const unsigned char table_dec85[0x80] = {
303 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
304 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
305 0xff,0x00,0xff,0xff,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0xff,
306 0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14,0x15,0xff,0xff,0xff,0x16,0xff,0x17,
307 0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,
308 0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x31,0x32,0x33,0xff,0x34,0x35,0x36,
309 0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,0x40,0x41,0x42,0x43,0x44,0x45,0x46,
310 0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,0x50,0x51,0x52,0xff,0x53,0x54,0xff,
311 };
312
313 static const char table_enc85[] =
314 "!$%&'()*+,-.0123456789=?@ABCDEFGHIJKLMNO"
315 "PQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwx"
316 "yz{}~";
317
318 /*
319  *  Converts a base85 encoded guid into a GUID pointer
320  *  Base85 encoded GUIDs should be 20 characters long.
321  *
322  *  returns TRUE if successful, FALSE if not
323  */
324 BOOL decode_base85_guid( LPCWSTR str, GUID *guid )
325 {
326     DWORD i, val = 0, base = 1, *p;
327
328     if (!str)
329         return FALSE;
330
331     p = (DWORD*) guid;
332     for( i=0; i<20; i++ )
333     {
334         if( (i%5) == 0 )
335         {
336             val = 0;
337             base = 1;
338         }
339         val += table_dec85[str[i]] * base;
340         if( str[i] >= 0x80 )
341             return FALSE;
342         if( table_dec85[str[i]] == 0xff )
343             return FALSE;
344         if( (i%5) == 4 )
345             p[i/5] = val;
346         base *= 85;
347     }
348     return TRUE;
349 }
350
351 /*
352  *  Encodes a base85 guid given a GUID pointer
353  *  Caller should provide a 21 character buffer for the encoded string.
354  *
355  *  returns TRUE if successful, FALSE if not
356  */
357 BOOL encode_base85_guid( GUID *guid, LPWSTR str )
358 {
359     unsigned int x, *p, i;
360
361     p = (unsigned int*) guid;
362     for( i=0; i<4; i++ )
363     {
364         x = p[i];
365         *str++ = table_enc85[x%85];
366         x = x/85;
367         *str++ = table_enc85[x%85];
368         x = x/85;
369         *str++ = table_enc85[x%85];
370         x = x/85;
371         *str++ = table_enc85[x%85];
372         x = x/85;
373         *str++ = table_enc85[x%85];
374     }
375     *str = 0;
376
377     return TRUE;
378 }
379
380 DWORD msi_version_str_to_dword(LPCWSTR p)
381 {
382     DWORD major, minor = 0, build = 0, version = 0;
383
384     if (!p)
385         return version;
386
387     major = atoiW(p);
388
389     p = strchrW(p, '.');
390     if (p)
391     {
392         minor = atoiW(p+1);
393         p = strchrW(p+1, '.');
394         if (p)
395             build = atoiW(p+1);
396     }
397
398     return MAKELONG(build, MAKEWORD(minor, major));
399 }
400
401 LPWSTR msi_version_dword_to_str(DWORD version)
402 {
403     const WCHAR fmt[] = { '%','u','.','%','u','.','%','u',0 };
404     LPWSTR str = msi_alloc(20);
405     sprintfW(str, fmt,
406              (version&0xff000000)>>24,
407              (version&0x00ff0000)>>16,
408               version&0x0000ffff);
409     return str;
410 }
411
412 LONG msi_reg_set_val_str( HKEY hkey, LPCWSTR name, LPCWSTR value )
413 {
414     DWORD len = value ? (lstrlenW(value) + 1) * sizeof (WCHAR) : 0;
415     return RegSetValueExW( hkey, name, 0, REG_SZ, (const BYTE *)value, len );
416 }
417
418 LONG msi_reg_set_val_multi_str( HKEY hkey, LPCWSTR name, LPCWSTR value )
419 {
420     LPCWSTR p = value;
421     while (*p) p += lstrlenW(p) + 1;
422     return RegSetValueExW( hkey, name, 0, REG_MULTI_SZ,
423                            (const BYTE *)value, (p + 1 - value) * sizeof(WCHAR) );
424 }
425
426 LONG msi_reg_set_val_dword( HKEY hkey, LPCWSTR name, DWORD val )
427 {
428     return RegSetValueExW( hkey, name, 0, REG_DWORD, (LPBYTE)&val, sizeof (DWORD) );
429 }
430
431 LONG msi_reg_set_subkey_val( HKEY hkey, LPCWSTR path, LPCWSTR name, LPCWSTR val )
432 {
433     HKEY hsubkey = 0;
434     LONG r;
435
436     r = RegCreateKeyW( hkey, path, &hsubkey );
437     if (r != ERROR_SUCCESS)
438         return r;
439     r = msi_reg_set_val_str( hsubkey, name, val );
440     RegCloseKey( hsubkey );
441     return r;
442 }
443
444 LPWSTR msi_reg_get_val_str( HKEY hkey, LPCWSTR name )
445 {
446     DWORD len = 0;
447     LPWSTR val;
448     LONG r;
449
450     r = RegQueryValueExW(hkey, name, NULL, NULL, NULL, &len);
451     if (r != ERROR_SUCCESS)
452         return NULL;
453
454     len += sizeof (WCHAR);
455     val = msi_alloc( len );
456     if (!val)
457         return NULL;
458     val[0] = 0;
459     RegQueryValueExW(hkey, name, NULL, NULL, (LPBYTE) val, &len);
460     return val;
461 }
462
463 BOOL msi_reg_get_val_dword( HKEY hkey, LPCWSTR name, DWORD *val)
464 {
465     DWORD type, len = sizeof (DWORD);
466     LONG r = RegQueryValueExW(hkey, name, NULL, &type, (LPBYTE) val, &len);
467     return r == ERROR_SUCCESS && type == REG_DWORD;
468 }
469
470 static UINT get_user_sid(LPWSTR *usersid)
471 {
472     HANDLE token;
473     BYTE buf[1024];
474     DWORD size;
475     PTOKEN_USER user;
476
477     if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token))
478         return ERROR_FUNCTION_FAILED;
479
480     size = sizeof(buf);
481     if (!GetTokenInformation(token, TokenUser, (void *)buf, size, &size))
482         return ERROR_FUNCTION_FAILED;
483
484     user = (PTOKEN_USER)buf;
485     if (!ConvertSidToStringSidW(user->User.Sid, usersid))
486         return ERROR_FUNCTION_FAILED;
487
488     return ERROR_SUCCESS;
489 }
490
491 UINT MSIREG_OpenUninstallKey(LPCWSTR szProduct, HKEY* key, BOOL create)
492 {
493     UINT rc;
494     WCHAR keypath[0x200];
495     TRACE("%s\n",debugstr_w(szProduct));
496
497     sprintfW(keypath,szUninstall_fmt,szProduct);
498
499     if (create)
500         rc = RegCreateKeyW(HKEY_LOCAL_MACHINE, keypath, key);
501     else
502         rc = RegOpenKeyW(HKEY_LOCAL_MACHINE, keypath, key);
503
504     return rc;
505 }
506
507 UINT MSIREG_DeleteUninstallKey(LPCWSTR szProduct)
508 {
509     WCHAR keypath[0x200];
510     TRACE("%s\n",debugstr_w(szProduct));
511
512     sprintfW(keypath,szUninstall_fmt,szProduct);
513
514     return RegDeleteTreeW(HKEY_LOCAL_MACHINE, keypath);
515 }
516
517 UINT MSIREG_OpenUserProductsKey(LPCWSTR szProduct, HKEY* key, BOOL create)
518 {
519     UINT rc;
520     WCHAR squished_pc[GUID_SIZE];
521     WCHAR keypath[0x200];
522
523     TRACE("%s\n",debugstr_w(szProduct));
524     if (!squash_guid(szProduct,squished_pc))
525         return ERROR_FUNCTION_FAILED;
526     TRACE("squished (%s)\n", debugstr_w(squished_pc));
527
528     sprintfW(keypath,szUserProduct_fmt,squished_pc);
529
530     if (create)
531         rc = RegCreateKeyW(HKEY_CURRENT_USER,keypath,key);
532     else
533         rc = RegOpenKeyW(HKEY_CURRENT_USER,keypath,key);
534
535     return rc;
536 }
537
538 UINT MSIREG_DeleteUserProductKey(LPCWSTR szProduct)
539 {
540     WCHAR squished_pc[GUID_SIZE];
541     WCHAR keypath[0x200];
542
543     TRACE("%s\n",debugstr_w(szProduct));
544     if (!squash_guid(szProduct,squished_pc))
545         return ERROR_FUNCTION_FAILED;
546     TRACE("squished (%s)\n", debugstr_w(squished_pc));
547
548     sprintfW(keypath,szUserProduct_fmt,squished_pc);
549
550     return RegDeleteTreeW(HKEY_CURRENT_USER, keypath);
551 }
552
553 UINT MSIREG_OpenUserPatchesKey(LPCWSTR szPatch, HKEY* key, BOOL create)
554 {
555     UINT rc;
556     WCHAR squished_pc[GUID_SIZE];
557     WCHAR keypath[0x200];
558
559     TRACE("%s\n",debugstr_w(szPatch));
560     if (!squash_guid(szPatch,squished_pc))
561         return ERROR_FUNCTION_FAILED;
562     TRACE("squished (%s)\n", debugstr_w(squished_pc));
563
564     sprintfW(keypath,szUserPatch_fmt,squished_pc);
565
566     if (create)
567         rc = RegCreateKeyW(HKEY_CURRENT_USER,keypath,key);
568     else
569         rc = RegOpenKeyW(HKEY_CURRENT_USER,keypath,key);
570
571     return rc;
572 }
573
574 UINT MSIREG_OpenUserFeaturesKey(LPCWSTR szProduct, HKEY* key, BOOL create)
575 {
576     UINT rc;
577     WCHAR squished_pc[GUID_SIZE];
578     WCHAR keypath[0x200];
579
580     TRACE("%s\n",debugstr_w(szProduct));
581     if (!squash_guid(szProduct,squished_pc))
582         return ERROR_FUNCTION_FAILED;
583     TRACE("squished (%s)\n", debugstr_w(squished_pc));
584
585     sprintfW(keypath,szUserFeatures_fmt,squished_pc);
586
587     if (create)
588         rc = RegCreateKeyW(HKEY_CURRENT_USER,keypath,key);
589     else
590         rc = RegOpenKeyW(HKEY_CURRENT_USER,keypath,key);
591
592     return rc;
593 }
594
595 UINT MSIREG_DeleteUserFeaturesKey(LPCWSTR szProduct)
596 {
597     WCHAR squished_pc[GUID_SIZE];
598     WCHAR keypath[0x200];
599
600     TRACE("%s\n",debugstr_w(szProduct));
601     if (!squash_guid(szProduct,squished_pc))
602         return ERROR_FUNCTION_FAILED;
603     TRACE("squished (%s)\n", debugstr_w(squished_pc));
604
605     sprintfW(keypath,szUserFeatures_fmt,squished_pc);
606     return RegDeleteTreeW(HKEY_CURRENT_USER, keypath);
607 }
608
609 UINT MSIREG_OpenFeatures(HKEY* key)
610 {
611     return RegCreateKeyW(HKEY_LOCAL_MACHINE,szInstaller_Features,key);
612 }
613
614 UINT MSIREG_OpenFeaturesKey(LPCWSTR szProduct, HKEY* key, BOOL create)
615 {
616     UINT rc;
617     WCHAR squished_pc[GUID_SIZE];
618     WCHAR keypath[0x200];
619
620     TRACE("%s\n",debugstr_w(szProduct));
621     if (!squash_guid(szProduct,squished_pc))
622         return ERROR_FUNCTION_FAILED;
623     TRACE("squished (%s)\n", debugstr_w(squished_pc));
624
625     sprintfW(keypath,szInstaller_Features_fmt,squished_pc);
626
627     if (create)
628         rc = RegCreateKeyW(HKEY_LOCAL_MACHINE,keypath,key);
629     else
630         rc = RegOpenKeyW(HKEY_LOCAL_MACHINE,keypath,key);
631
632     return rc;
633 }
634
635 UINT MSIREG_OpenUserDataFeaturesKey(LPCWSTR szProduct, HKEY *key, BOOL create)
636 {
637     UINT rc;
638     WCHAR squished_pc[GUID_SIZE];
639     WCHAR keypath[0x200];
640     LPWSTR usersid;
641
642     TRACE("%s\n", debugstr_w(szProduct));
643     if (!squash_guid(szProduct, squished_pc))
644         return ERROR_FUNCTION_FAILED;
645     TRACE("squished (%s)\n", debugstr_w(squished_pc));
646
647     rc = get_user_sid(&usersid);
648     if (rc != ERROR_SUCCESS || !usersid)
649     {
650         ERR("Failed to retrieve user SID: %d\n", rc);
651         return rc;
652     }
653
654     sprintfW(keypath, szUserDataFeatures_fmt, usersid, squished_pc);
655
656     if (create)
657         rc = RegCreateKeyW(HKEY_LOCAL_MACHINE, keypath, key);
658     else
659         rc = RegOpenKeyW(HKEY_LOCAL_MACHINE, keypath, key);
660
661     msi_free(usersid);
662     return rc;
663 }
664
665 UINT MSIREG_OpenComponents(HKEY* key)
666 {
667     return RegCreateKeyW(HKEY_LOCAL_MACHINE,szInstaller_Components,key);
668 }
669
670 UINT MSIREG_OpenComponentsKey(LPCWSTR szComponent, HKEY* key, BOOL create)
671 {
672     UINT rc;
673     WCHAR squished_cc[GUID_SIZE];
674     WCHAR keypath[0x200];
675
676     TRACE("%s\n",debugstr_w(szComponent));
677     if (!squash_guid(szComponent,squished_cc))
678         return ERROR_FUNCTION_FAILED;
679     TRACE("squished (%s)\n", debugstr_w(squished_cc));
680
681     sprintfW(keypath,szInstaller_Components_fmt,squished_cc);
682
683     if (create)
684         rc = RegCreateKeyW(HKEY_LOCAL_MACHINE,keypath,key);
685     else
686         rc = RegOpenKeyW(HKEY_LOCAL_MACHINE,keypath,key);
687
688     return rc;
689 }
690
691 UINT MSIREG_OpenUserComponentsKey(LPCWSTR szComponent, HKEY* key, BOOL create)
692 {
693     UINT rc;
694     WCHAR squished_cc[GUID_SIZE];
695     WCHAR keypath[0x200];
696
697     TRACE("%s\n",debugstr_w(szComponent));
698     if (!squash_guid(szComponent,squished_cc))
699         return ERROR_FUNCTION_FAILED;
700     TRACE("squished (%s)\n", debugstr_w(squished_cc));
701
702     sprintfW(keypath,szUser_Components_fmt,squished_cc);
703
704     if (create)
705         rc = RegCreateKeyW(HKEY_CURRENT_USER,keypath,key);
706     else
707         rc = RegOpenKeyW(HKEY_CURRENT_USER,keypath,key);
708
709     return rc;
710 }
711
712 UINT MSIREG_OpenUserDataComponentKey(LPCWSTR szComponent, HKEY *key, BOOL create)
713 {
714     UINT rc;
715     WCHAR comp[GUID_SIZE];
716     WCHAR keypath[0x200];
717     LPWSTR usersid;
718
719     TRACE("%s\n", debugstr_w(szComponent));
720     if (!squash_guid(szComponent, comp))
721         return ERROR_FUNCTION_FAILED;
722     TRACE("squished (%s)\n", debugstr_w(comp));
723
724     rc = get_user_sid(&usersid);
725     if (rc != ERROR_SUCCESS || !usersid)
726     {
727         ERR("Failed to retrieve user SID: %d\n", rc);
728         return rc;
729     }
730
731     sprintfW(keypath, szUserDataComp_fmt, usersid, comp);
732
733     if (create)
734         rc = RegCreateKeyW(HKEY_LOCAL_MACHINE, keypath, key);
735     else
736         rc = RegOpenKeyW(HKEY_LOCAL_MACHINE, keypath, key);
737
738     msi_free(usersid);
739     return rc;
740 }
741
742 UINT MSIREG_DeleteUserDataComponentKey(LPCWSTR szComponent)
743 {
744     UINT rc;
745     WCHAR comp[GUID_SIZE];
746     WCHAR keypath[0x200];
747     LPWSTR usersid;
748
749     TRACE("%s\n", debugstr_w(szComponent));
750     if (!squash_guid(szComponent, comp))
751         return ERROR_FUNCTION_FAILED;
752     TRACE("squished (%s)\n", debugstr_w(comp));
753
754     rc = get_user_sid(&usersid);
755     if (rc != ERROR_SUCCESS || !usersid)
756     {
757         ERR("Failed to retrieve user SID: %d\n", rc);
758         return rc;
759     }
760
761     sprintfW(keypath, szUserDataComp_fmt, usersid, comp);
762
763     msi_free(usersid);
764     return RegDeleteTreeW(HKEY_LOCAL_MACHINE, keypath);
765 }
766
767 UINT MSIREG_OpenUserDataProductKey(LPCWSTR szProduct, HKEY *key, BOOL create)
768 {
769     UINT rc;
770     WCHAR squished_pc[GUID_SIZE];
771     WCHAR keypath[0x200];
772     LPWSTR usersid;
773
774     TRACE("%s\n", debugstr_w(szProduct));
775     if (!squash_guid(szProduct, squished_pc))
776         return ERROR_FUNCTION_FAILED;
777     TRACE("squished (%s)\n", debugstr_w(squished_pc));
778
779     rc = get_user_sid(&usersid);
780     if (rc != ERROR_SUCCESS || !usersid)
781     {
782         ERR("Failed to retrieve user SID: %d\n", rc);
783         return rc;
784     }
785
786     sprintfW(keypath, szUserDataProd_fmt, usersid, squished_pc);
787
788     if (create)
789         rc = RegCreateKeyW(HKEY_LOCAL_MACHINE, keypath, key);
790     else
791         rc = RegOpenKeyW(HKEY_LOCAL_MACHINE, keypath, key);
792
793     msi_free(usersid);
794     return rc;
795 }
796
797 UINT MSIREG_OpenInstallPropertiesKey(LPCWSTR szProduct, HKEY *key, BOOL create)
798 {
799     UINT rc;
800     WCHAR squished_pc[GUID_SIZE];
801     WCHAR keypath[0x200];
802     LPWSTR usersid;
803
804     TRACE("%s\n", debugstr_w(szProduct));
805     if (!squash_guid(szProduct, squished_pc))
806         return ERROR_FUNCTION_FAILED;
807     TRACE("squished (%s)\n", debugstr_w(squished_pc));
808
809     rc = get_user_sid(&usersid);
810     if (rc != ERROR_SUCCESS || !usersid)
811     {
812         ERR("Failed to retrieve user SID: %d\n", rc);
813         return rc;
814     }
815
816     sprintfW(keypath, szInstallProperties_fmt, usersid, squished_pc);
817
818     if (create)
819         rc = RegCreateKeyW(HKEY_LOCAL_MACHINE, keypath, key);
820     else
821         rc = RegOpenKeyW(HKEY_LOCAL_MACHINE, keypath, key);
822
823     msi_free(usersid);
824     return rc;
825 }
826
827 UINT MSIREG_DeleteUserDataProductKey(LPCWSTR szProduct)
828 {
829     UINT rc;
830     WCHAR squished_pc[GUID_SIZE];
831     WCHAR keypath[0x200];
832     LPWSTR usersid;
833
834     TRACE("%s\n", debugstr_w(szProduct));
835     if (!squash_guid(szProduct, squished_pc))
836         return ERROR_FUNCTION_FAILED;
837     TRACE("squished (%s)\n", debugstr_w(squished_pc));
838
839     rc = get_user_sid(&usersid);
840     if (rc != ERROR_SUCCESS || !usersid)
841     {
842         ERR("Failed to retrieve user SID: %d\n", rc);
843         return rc;
844     }
845
846     sprintfW(keypath, szUserDataProd_fmt, usersid, squished_pc);
847
848     msi_free(usersid);
849     return RegDeleteTreeW(HKEY_LOCAL_MACHINE, keypath);
850 }
851
852 UINT MSIREG_OpenProducts(HKEY* key)
853 {
854     return RegCreateKeyW(HKEY_LOCAL_MACHINE,szInstaller_Products,key);
855 }
856
857 UINT MSIREG_OpenProductsKey(LPCWSTR szProduct, HKEY* key, BOOL create)
858 {
859     UINT rc;
860     WCHAR squished_pc[GUID_SIZE];
861     WCHAR keypath[0x200];
862
863     TRACE("%s\n",debugstr_w(szProduct));
864     if (!squash_guid(szProduct,squished_pc))
865         return ERROR_FUNCTION_FAILED;
866     TRACE("squished (%s)\n", debugstr_w(squished_pc));
867
868     sprintfW(keypath,szInstaller_Products_fmt,squished_pc);
869
870     if (create)
871         rc = RegCreateKeyW(HKEY_LOCAL_MACHINE,keypath,key);
872     else
873         rc = RegOpenKeyW(HKEY_LOCAL_MACHINE,keypath,key);
874
875     return rc;
876 }
877
878 UINT MSIREG_DeleteProductKey(LPCWSTR szProduct)
879 {
880     WCHAR squished_pc[GUID_SIZE];
881     WCHAR keypath[0x200];
882
883     TRACE("%s\n", debugstr_w(szProduct));
884     if (!squash_guid(szProduct, squished_pc))
885         return ERROR_FUNCTION_FAILED;
886     TRACE("squished (%s)\n", debugstr_w(squished_pc));
887
888     sprintfW(keypath, szInstaller_Products_fmt, squished_pc);
889
890     return RegDeleteTreeW(HKEY_LOCAL_MACHINE, keypath);
891 }
892
893 UINT MSIREG_OpenPatchesKey(LPCWSTR szPatch, HKEY* key, BOOL create)
894 {
895     UINT rc;
896     WCHAR squished_pc[GUID_SIZE];
897     WCHAR keypath[0x200];
898
899     TRACE("%s\n",debugstr_w(szPatch));
900     if (!squash_guid(szPatch,squished_pc))
901         return ERROR_FUNCTION_FAILED;
902     TRACE("squished (%s)\n", debugstr_w(squished_pc));
903
904     sprintfW(keypath,szInstaller_Patches_fmt,squished_pc);
905
906     if (create)
907         rc = RegCreateKeyW(HKEY_LOCAL_MACHINE,keypath,key);
908     else
909         rc = RegOpenKeyW(HKEY_LOCAL_MACHINE,keypath,key);
910
911     return rc;
912 }
913
914 UINT MSIREG_OpenUpgradeCodesKey(LPCWSTR szUpgradeCode, HKEY* key, BOOL create)
915 {
916     UINT rc;
917     WCHAR squished_pc[GUID_SIZE];
918     WCHAR keypath[0x200];
919
920     TRACE("%s\n",debugstr_w(szUpgradeCode));
921     if (!squash_guid(szUpgradeCode,squished_pc))
922         return ERROR_FUNCTION_FAILED;
923     TRACE("squished (%s)\n", debugstr_w(squished_pc));
924
925     sprintfW(keypath,szInstaller_UpgradeCodes_fmt,squished_pc);
926
927     if (create)
928         rc = RegCreateKeyW(HKEY_LOCAL_MACHINE,keypath,key);
929     else
930         rc = RegOpenKeyW(HKEY_LOCAL_MACHINE,keypath,key);
931
932     return rc;
933 }
934
935 UINT MSIREG_OpenUserUpgradeCodesKey(LPCWSTR szUpgradeCode, HKEY* key, BOOL create)
936 {
937     UINT rc;
938     WCHAR squished_pc[GUID_SIZE];
939     WCHAR keypath[0x200];
940
941     TRACE("%s\n",debugstr_w(szUpgradeCode));
942     if (!squash_guid(szUpgradeCode,squished_pc))
943         return ERROR_FUNCTION_FAILED;
944     TRACE("squished (%s)\n", debugstr_w(squished_pc));
945
946     sprintfW(keypath,szInstaller_UserUpgradeCodes_fmt,squished_pc);
947
948     if (create)
949         rc = RegCreateKeyW(HKEY_CURRENT_USER,keypath,key);
950     else
951         rc = RegOpenKeyW(HKEY_CURRENT_USER,keypath,key);
952
953     return rc;
954 }
955
956 UINT MSIREG_OpenLocalSystemProductKey(LPCWSTR szProductCode, HKEY *key, BOOL create)
957 {
958     WCHAR squished_pc[GUID_SIZE];
959     WCHAR keypath[0x200];
960
961     TRACE("%s\n", debugstr_w(szProductCode));
962
963     if (!squash_guid(szProductCode, squished_pc))
964         return ERROR_FUNCTION_FAILED;
965
966     TRACE("squished (%s)\n", debugstr_w(squished_pc));
967
968     sprintfW(keypath, szInstaller_LocalSystemProductCodes_fmt, squished_pc);
969
970     if (create)
971         return RegCreateKeyW(HKEY_LOCAL_MACHINE, keypath, key);
972
973     return RegOpenKeyW(HKEY_LOCAL_MACHINE, keypath, key);
974 }
975
976 UINT MSIREG_OpenLocalSystemComponentKey(LPCWSTR szComponent, HKEY *key, BOOL create)
977 {
978     WCHAR squished_pc[GUID_SIZE];
979     WCHAR keypath[0x200];
980
981     TRACE("%s\n", debugstr_w(szComponent));
982
983     if (!squash_guid(szComponent, squished_pc))
984         return ERROR_FUNCTION_FAILED;
985
986     TRACE("squished (%s)\n", debugstr_w(squished_pc));
987
988     sprintfW(keypath, szInstaller_LocalSystemComponent_fmt, squished_pc);
989
990     if (create)
991         return RegCreateKeyW(HKEY_LOCAL_MACHINE, keypath, key);
992
993     return RegOpenKeyW(HKEY_LOCAL_MACHINE, keypath, key);
994 }
995
996 UINT MSIREG_OpenLocalClassesProductKey(LPCWSTR szProductCode, HKEY *key, BOOL create)
997 {
998     WCHAR squished_pc[GUID_SIZE];
999     WCHAR keypath[0x200];
1000
1001     TRACE("%s\n", debugstr_w(szProductCode));
1002
1003     if (!squash_guid(szProductCode, squished_pc))
1004         return ERROR_FUNCTION_FAILED;
1005
1006     TRACE("squished (%s)\n", debugstr_w(squished_pc));
1007
1008     sprintfW(keypath, szInstaller_LocalClassesProd_fmt, squished_pc);
1009
1010     if (create)
1011         return RegCreateKeyW(HKEY_LOCAL_MACHINE, keypath, key);
1012
1013     return RegOpenKeyW(HKEY_LOCAL_MACHINE, keypath, key);
1014 }
1015
1016 UINT MSIREG_OpenLocalManagedProductKey(LPCWSTR szProductCode, HKEY *key, BOOL create)
1017 {
1018     WCHAR squished_pc[GUID_SIZE];
1019     WCHAR keypath[0x200];
1020     LPWSTR usersid;
1021     UINT r;
1022
1023     TRACE("%s\n", debugstr_w(szProductCode));
1024
1025     if (!squash_guid(szProductCode, squished_pc))
1026         return ERROR_FUNCTION_FAILED;
1027
1028     TRACE("squished (%s)\n", debugstr_w(squished_pc));
1029
1030     r = get_user_sid(&usersid);
1031     if (r != ERROR_SUCCESS || !usersid)
1032     {
1033         ERR("Failed to retrieve user SID: %d\n", r);
1034         return r;
1035     }
1036
1037     sprintfW(keypath, szInstaller_LocalManagedProd_fmt, usersid, squished_pc);
1038     msi_free(usersid);
1039
1040     if (create)
1041         return RegCreateKeyW(HKEY_LOCAL_MACHINE, keypath, key);
1042
1043     return RegOpenKeyW(HKEY_LOCAL_MACHINE, keypath, key);
1044 }
1045
1046 /*************************************************************************
1047  *  MsiDecomposeDescriptorW   [MSI.@]
1048  *
1049  * Decomposes an MSI descriptor into product, feature and component parts.
1050  * An MSI descriptor is a string of the form:
1051  *   [base 85 guid] [feature code] '>' [base 85 guid]
1052  *
1053  * PARAMS
1054  *   szDescriptor  [I]  the descriptor to decompose
1055  *   szProduct     [O]  buffer of MAX_FEATURE_CHARS+1 for the product guid
1056  *   szFeature     [O]  buffer of MAX_FEATURE_CHARS+1 for the feature code
1057  *   szComponent   [O]  buffer of MAX_FEATURE_CHARS+1 for the component guid
1058  *   pUsed         [O]  the length of the descriptor
1059  *
1060  * RETURNS
1061  *   ERROR_SUCCESS             if everything worked correctly
1062  *   ERROR_INVALID_PARAMETER   if the descriptor was invalid
1063  *
1064  */
1065 UINT WINAPI MsiDecomposeDescriptorW( LPCWSTR szDescriptor, LPWSTR szProduct,
1066                 LPWSTR szFeature, LPWSTR szComponent, LPDWORD pUsed )
1067 {
1068     UINT r, len;
1069     LPWSTR p;
1070     GUID product, component;
1071
1072     TRACE("%s %p %p %p %p\n", debugstr_w(szDescriptor), szProduct,
1073           szFeature, szComponent, pUsed);
1074
1075     r = decode_base85_guid( szDescriptor, &product );
1076     if( !r )
1077         return ERROR_INVALID_PARAMETER;
1078
1079     TRACE("product %s\n", debugstr_guid( &product ));
1080
1081     p = strchrW(&szDescriptor[20],'>');
1082     if( !p )
1083         return ERROR_INVALID_PARAMETER;
1084
1085     len = (p - &szDescriptor[20]);
1086     if( len > MAX_FEATURE_CHARS )
1087         return ERROR_INVALID_PARAMETER;
1088
1089     TRACE("feature %s\n", debugstr_wn( &szDescriptor[20], len ));
1090
1091     r = decode_base85_guid( p+1, &component );
1092     if( !r )
1093         return ERROR_INVALID_PARAMETER;
1094
1095     TRACE("component %s\n", debugstr_guid( &component ));
1096
1097     if (szProduct)
1098         StringFromGUID2( &product, szProduct, MAX_FEATURE_CHARS+1 );
1099     if (szComponent)
1100         StringFromGUID2( &component, szComponent, MAX_FEATURE_CHARS+1 );
1101     if (szFeature)
1102     {
1103         memcpy( szFeature, &szDescriptor[20], len*sizeof(WCHAR) );
1104         szFeature[len] = 0;
1105     }
1106     len = ( &p[21] - szDescriptor );
1107
1108     TRACE("length = %d\n", len);
1109     *pUsed = len;
1110
1111     return ERROR_SUCCESS;
1112 }
1113
1114 UINT WINAPI MsiDecomposeDescriptorA( LPCSTR szDescriptor, LPSTR szProduct,
1115                 LPSTR szFeature, LPSTR szComponent, LPDWORD pUsed )
1116 {
1117     WCHAR product[MAX_FEATURE_CHARS+1];
1118     WCHAR feature[MAX_FEATURE_CHARS+1];
1119     WCHAR component[MAX_FEATURE_CHARS+1];
1120     LPWSTR str = NULL, p = NULL, f = NULL, c = NULL;
1121     UINT r;
1122
1123     TRACE("%s %p %p %p %p\n", debugstr_a(szDescriptor), szProduct,
1124           szFeature, szComponent, pUsed);
1125
1126     str = strdupAtoW( szDescriptor );
1127     if( szDescriptor && !str )
1128         return ERROR_OUTOFMEMORY;
1129
1130     if (szProduct)
1131         p = product;
1132     if (szFeature)
1133         f = feature;
1134     if (szComponent)
1135         c = component;
1136
1137     r = MsiDecomposeDescriptorW( str, p, f, c, pUsed );
1138
1139     if (r == ERROR_SUCCESS)
1140     {
1141         WideCharToMultiByte( CP_ACP, 0, p, -1,
1142                              szProduct, MAX_FEATURE_CHARS+1, NULL, NULL );
1143         WideCharToMultiByte( CP_ACP, 0, f, -1,
1144                              szFeature, MAX_FEATURE_CHARS+1, NULL, NULL );
1145         WideCharToMultiByte( CP_ACP, 0, c, -1,
1146                              szComponent, MAX_FEATURE_CHARS+1, NULL, NULL );
1147     }
1148
1149     msi_free( str );
1150
1151     return r;
1152 }
1153
1154 UINT WINAPI MsiEnumProductsA(DWORD index, LPSTR lpguid)
1155 {
1156     DWORD r;
1157     WCHAR szwGuid[GUID_SIZE];
1158
1159     TRACE("%d %p\n", index, lpguid);
1160
1161     if (NULL == lpguid)
1162         return ERROR_INVALID_PARAMETER;
1163     r = MsiEnumProductsW(index, szwGuid);
1164     if( r == ERROR_SUCCESS )
1165         WideCharToMultiByte(CP_ACP, 0, szwGuid, -1, lpguid, GUID_SIZE, NULL, NULL);
1166
1167     return r;
1168 }
1169
1170 UINT WINAPI MsiEnumProductsW(DWORD index, LPWSTR lpguid)
1171 {
1172     HKEY hkeyProducts = 0;
1173     DWORD r;
1174     WCHAR szKeyName[SQUISH_GUID_SIZE];
1175
1176     TRACE("%d %p\n", index, lpguid);
1177
1178     if (NULL == lpguid)
1179         return ERROR_INVALID_PARAMETER;
1180
1181     r = MSIREG_OpenProducts(&hkeyProducts);
1182     if( r != ERROR_SUCCESS )
1183         return ERROR_NO_MORE_ITEMS;
1184
1185     r = RegEnumKeyW(hkeyProducts, index, szKeyName, SQUISH_GUID_SIZE);
1186     if( r == ERROR_SUCCESS )
1187         unsquash_guid(szKeyName, lpguid);
1188     RegCloseKey(hkeyProducts);
1189
1190     return r;
1191 }
1192
1193 UINT WINAPI MsiEnumFeaturesA(LPCSTR szProduct, DWORD index, 
1194       LPSTR szFeature, LPSTR szParent)
1195 {
1196     DWORD r;
1197     WCHAR szwFeature[GUID_SIZE], szwParent[GUID_SIZE];
1198     LPWSTR szwProduct = NULL;
1199
1200     TRACE("%s %d %p %p\n", debugstr_a(szProduct), index, szFeature, szParent);
1201
1202     if( szProduct )
1203     {
1204         szwProduct = strdupAtoW( szProduct );
1205         if( !szwProduct )
1206             return ERROR_OUTOFMEMORY;
1207     }
1208
1209     r = MsiEnumFeaturesW(szwProduct, index, szwFeature, szwParent);
1210     if( r == ERROR_SUCCESS )
1211     {
1212         WideCharToMultiByte(CP_ACP, 0, szwFeature, -1,
1213                             szFeature, GUID_SIZE, NULL, NULL);
1214         WideCharToMultiByte(CP_ACP, 0, szwParent, -1,
1215                             szParent, GUID_SIZE, NULL, NULL);
1216     }
1217
1218     msi_free( szwProduct);
1219
1220     return r;
1221 }
1222
1223 UINT WINAPI MsiEnumFeaturesW(LPCWSTR szProduct, DWORD index, 
1224       LPWSTR szFeature, LPWSTR szParent)
1225 {
1226     HKEY hkeyProduct = 0;
1227     DWORD r, sz;
1228
1229     TRACE("%s %d %p %p\n", debugstr_w(szProduct), index, szFeature, szParent);
1230
1231     if( !szProduct )
1232         return ERROR_INVALID_PARAMETER;
1233
1234     r = MSIREG_OpenFeaturesKey(szProduct,&hkeyProduct,FALSE);
1235     if( r != ERROR_SUCCESS )
1236         return ERROR_NO_MORE_ITEMS;
1237
1238     sz = GUID_SIZE;
1239     r = RegEnumValueW(hkeyProduct, index, szFeature, &sz, NULL, NULL, NULL, NULL);
1240     RegCloseKey(hkeyProduct);
1241
1242     return r;
1243 }
1244
1245 UINT WINAPI MsiEnumComponentsA(DWORD index, LPSTR lpguid)
1246 {
1247     DWORD r;
1248     WCHAR szwGuid[GUID_SIZE];
1249
1250     TRACE("%d %p\n", index, lpguid);
1251
1252     r = MsiEnumComponentsW(index, szwGuid);
1253     if( r == ERROR_SUCCESS )
1254         WideCharToMultiByte(CP_ACP, 0, szwGuid, -1, lpguid, GUID_SIZE, NULL, NULL);
1255
1256     return r;
1257 }
1258
1259 UINT WINAPI MsiEnumComponentsW(DWORD index, LPWSTR lpguid)
1260 {
1261     HKEY hkeyComponents = 0;
1262     DWORD r;
1263     WCHAR szKeyName[SQUISH_GUID_SIZE];
1264
1265     TRACE("%d %p\n", index, lpguid);
1266
1267     r = MSIREG_OpenComponents(&hkeyComponents);
1268     if( r != ERROR_SUCCESS )
1269         return ERROR_NO_MORE_ITEMS;
1270
1271     r = RegEnumKeyW(hkeyComponents, index, szKeyName, SQUISH_GUID_SIZE);
1272     if( r == ERROR_SUCCESS )
1273         unsquash_guid(szKeyName, lpguid);
1274     RegCloseKey(hkeyComponents);
1275
1276     return r;
1277 }
1278
1279 UINT WINAPI MsiEnumClientsA(LPCSTR szComponent, DWORD index, LPSTR szProduct)
1280 {
1281     DWORD r;
1282     WCHAR szwProduct[GUID_SIZE];
1283     LPWSTR szwComponent = NULL;
1284
1285     TRACE("%s %d %p\n", debugstr_a(szComponent), index, szProduct);
1286
1287     if ( !szProduct )
1288         return ERROR_INVALID_PARAMETER;
1289
1290     if( szComponent )
1291     {
1292         szwComponent = strdupAtoW( szComponent );
1293         if( !szwComponent )
1294             return ERROR_OUTOFMEMORY;
1295     }
1296
1297     r = MsiEnumClientsW(szComponent?szwComponent:NULL, index, szwProduct);
1298     if( r == ERROR_SUCCESS )
1299     {
1300         WideCharToMultiByte(CP_ACP, 0, szwProduct, -1,
1301                             szProduct, GUID_SIZE, NULL, NULL);
1302     }
1303
1304     msi_free( szwComponent);
1305
1306     return r;
1307 }
1308
1309 UINT WINAPI MsiEnumClientsW(LPCWSTR szComponent, DWORD index, LPWSTR szProduct)
1310 {
1311     HKEY hkeyComp = 0;
1312     DWORD r, sz;
1313     WCHAR szValName[SQUISH_GUID_SIZE];
1314
1315     TRACE("%s %d %p\n", debugstr_w(szComponent), index, szProduct);
1316
1317     if (!szComponent || !*szComponent || !szProduct)
1318         return ERROR_INVALID_PARAMETER;
1319
1320     if (MSIREG_OpenUserDataComponentKey(szComponent, &hkeyComp, FALSE) != ERROR_SUCCESS &&
1321         MSIREG_OpenLocalSystemComponentKey(szComponent, &hkeyComp, FALSE) != ERROR_SUCCESS)
1322         return ERROR_UNKNOWN_COMPONENT;
1323
1324     /* see if there are any products at all */
1325     sz = SQUISH_GUID_SIZE;
1326     r = RegEnumValueW(hkeyComp, 0, szValName, &sz, NULL, NULL, NULL, NULL);
1327     if (r != ERROR_SUCCESS)
1328     {
1329         RegCloseKey(hkeyComp);
1330
1331         if (index != 0)
1332             return ERROR_INVALID_PARAMETER;
1333
1334         return ERROR_UNKNOWN_COMPONENT;
1335     }
1336
1337     sz = SQUISH_GUID_SIZE;
1338     r = RegEnumValueW(hkeyComp, index, szValName, &sz, NULL, NULL, NULL, NULL);
1339     if( r == ERROR_SUCCESS )
1340         unsquash_guid(szValName, szProduct);
1341
1342     RegCloseKey(hkeyComp);
1343
1344     return r;
1345 }
1346
1347 static UINT WINAPI MSI_EnumComponentQualifiers( LPCWSTR szComponent, DWORD iIndex,
1348                 awstring *lpQualBuf, LPDWORD pcchQual,
1349                 awstring *lpAppBuf, LPDWORD pcchAppBuf )
1350 {
1351     DWORD name_sz, val_sz, name_max, val_max, type, ofs;
1352     LPWSTR name = NULL, val = NULL;
1353     UINT r, r2;
1354     HKEY key;
1355
1356     TRACE("%s %08x %p %p %p %p\n", debugstr_w(szComponent), iIndex,
1357           lpQualBuf, pcchQual, lpAppBuf, pcchAppBuf);
1358
1359     if (!szComponent)
1360         return ERROR_INVALID_PARAMETER;
1361
1362     r = MSIREG_OpenUserComponentsKey( szComponent, &key, FALSE );
1363     if (r != ERROR_SUCCESS)
1364         return ERROR_UNKNOWN_COMPONENT;
1365
1366     /* figure out how big the name is we want to return */
1367     name_max = 0x10;
1368     r = ERROR_OUTOFMEMORY;
1369     name = msi_alloc( name_max * sizeof(WCHAR) );
1370     if (!name)
1371         goto end;
1372
1373     val_max = 0x10;
1374     r = ERROR_OUTOFMEMORY;
1375     val = msi_alloc( val_max );
1376     if (!val)
1377         goto end;
1378
1379     /* loop until we allocate enough memory */
1380     while (1)
1381     {
1382         name_sz = name_max;
1383         val_sz = val_max;
1384         r = RegEnumValueW( key, iIndex, name, &name_sz,
1385                            NULL, &type, (LPBYTE)val, &val_sz );
1386         if (r == ERROR_SUCCESS)
1387             break;
1388         if (r != ERROR_MORE_DATA)
1389             goto end;
1390  
1391         if (type != REG_MULTI_SZ)
1392         {
1393             ERR("component data has wrong type (%d)\n", type);
1394             goto end;
1395         }
1396
1397         r = ERROR_OUTOFMEMORY;
1398         if ((name_sz+1) >= name_max)
1399         {
1400             name_max *= 2;
1401             msi_free( name );
1402             name = msi_alloc( name_max * sizeof (WCHAR) );
1403             if (!name)
1404                 goto end;
1405             continue;
1406         }
1407         if (val_sz > val_max)
1408         {
1409             val_max = val_sz + sizeof (WCHAR);
1410             msi_free( val );
1411             val = msi_alloc( val_max * sizeof (WCHAR) );
1412             if (!val)
1413                 goto end;
1414             continue;
1415         }
1416         ERR("should be enough data, but isn't %d %d\n", name_sz, val_sz );
1417         goto end;
1418     }
1419
1420     ofs = 0;
1421     r = MsiDecomposeDescriptorW( val, NULL, NULL, NULL, &ofs );
1422     if (r != ERROR_SUCCESS)
1423         goto end;
1424
1425     TRACE("Providing %s and %s\n", debugstr_w(name), debugstr_w(val+ofs));
1426
1427     r = msi_strcpy_to_awstring( name, lpQualBuf, pcchQual );
1428     r2 = msi_strcpy_to_awstring( val+ofs, lpAppBuf, pcchAppBuf );
1429
1430     if (r2 != ERROR_SUCCESS)
1431         r = r2;
1432
1433 end:
1434     msi_free(val);
1435     msi_free(name);
1436     RegCloseKey(key);
1437
1438     return r;
1439 }
1440
1441 /*************************************************************************
1442  *  MsiEnumComponentQualifiersA [MSI.@]
1443  */
1444 UINT WINAPI MsiEnumComponentQualifiersA( LPCSTR szComponent, DWORD iIndex,
1445                 LPSTR lpQualifierBuf, LPDWORD pcchQualifierBuf,
1446                 LPSTR lpApplicationDataBuf, LPDWORD pcchApplicationDataBuf )
1447 {
1448     awstring qual, appdata;
1449     LPWSTR comp;
1450     UINT r;
1451
1452     TRACE("%s %08x %p %p %p %p\n", debugstr_a(szComponent), iIndex,
1453           lpQualifierBuf, pcchQualifierBuf, lpApplicationDataBuf,
1454           pcchApplicationDataBuf);
1455
1456     comp = strdupAtoW( szComponent );
1457     if (szComponent && !comp)
1458         return ERROR_OUTOFMEMORY;
1459
1460     qual.unicode = FALSE;
1461     qual.str.a = lpQualifierBuf;
1462
1463     appdata.unicode = FALSE;
1464     appdata.str.a = lpApplicationDataBuf;
1465
1466     r = MSI_EnumComponentQualifiers( comp, iIndex,
1467               &qual, pcchQualifierBuf, &appdata, pcchApplicationDataBuf );
1468     msi_free( comp );
1469     return r;
1470 }
1471
1472 /*************************************************************************
1473  *  MsiEnumComponentQualifiersW [MSI.@]
1474  */
1475 UINT WINAPI MsiEnumComponentQualifiersW( LPCWSTR szComponent, DWORD iIndex,
1476                 LPWSTR lpQualifierBuf, LPDWORD pcchQualifierBuf,
1477                 LPWSTR lpApplicationDataBuf, LPDWORD pcchApplicationDataBuf )
1478 {
1479     awstring qual, appdata;
1480
1481     TRACE("%s %08x %p %p %p %p\n", debugstr_w(szComponent), iIndex,
1482           lpQualifierBuf, pcchQualifierBuf, lpApplicationDataBuf,
1483           pcchApplicationDataBuf);
1484
1485     qual.unicode = TRUE;
1486     qual.str.w = lpQualifierBuf;
1487
1488     appdata.unicode = TRUE;
1489     appdata.str.w = lpApplicationDataBuf;
1490
1491     return MSI_EnumComponentQualifiers( szComponent, iIndex,
1492                  &qual, pcchQualifierBuf, &appdata, pcchApplicationDataBuf );
1493 }
1494
1495 /*************************************************************************
1496  *  MsiEnumRelatedProductsW   [MSI.@]
1497  *
1498  */
1499 UINT WINAPI MsiEnumRelatedProductsW(LPCWSTR szUpgradeCode, DWORD dwReserved,
1500                                     DWORD iProductIndex, LPWSTR lpProductBuf)
1501 {
1502     UINT r;
1503     HKEY hkey;
1504     DWORD dwSize = SQUISH_GUID_SIZE;
1505     WCHAR szKeyName[SQUISH_GUID_SIZE];
1506
1507     TRACE("%s %u %u %p\n", debugstr_w(szUpgradeCode), dwReserved,
1508           iProductIndex, lpProductBuf);
1509
1510     if (NULL == szUpgradeCode)
1511         return ERROR_INVALID_PARAMETER;
1512     if (NULL == lpProductBuf)
1513         return ERROR_INVALID_PARAMETER;
1514
1515     r = MSIREG_OpenUpgradeCodesKey(szUpgradeCode, &hkey, FALSE);
1516     if (r != ERROR_SUCCESS)
1517         return ERROR_NO_MORE_ITEMS;
1518
1519     r = RegEnumValueW(hkey, iProductIndex, szKeyName, &dwSize, NULL, NULL, NULL, NULL);
1520     if( r == ERROR_SUCCESS )
1521         unsquash_guid(szKeyName, lpProductBuf);
1522     RegCloseKey(hkey);
1523
1524     return r;
1525 }
1526
1527 /*************************************************************************
1528  *  MsiEnumRelatedProductsA   [MSI.@]
1529  *
1530  */
1531 UINT WINAPI MsiEnumRelatedProductsA(LPCSTR szUpgradeCode, DWORD dwReserved,
1532                                     DWORD iProductIndex, LPSTR lpProductBuf)
1533 {
1534     LPWSTR szwUpgradeCode = NULL;
1535     WCHAR productW[GUID_SIZE];
1536     UINT r;
1537
1538     TRACE("%s %u %u %p\n", debugstr_a(szUpgradeCode), dwReserved,
1539           iProductIndex, lpProductBuf);
1540
1541     if (szUpgradeCode)
1542     {
1543         szwUpgradeCode = strdupAtoW( szUpgradeCode );
1544         if( !szwUpgradeCode )
1545             return ERROR_OUTOFMEMORY;
1546     }
1547
1548     r = MsiEnumRelatedProductsW( szwUpgradeCode, dwReserved,
1549                                  iProductIndex, productW );
1550     if (r == ERROR_SUCCESS)
1551     {
1552         WideCharToMultiByte( CP_ACP, 0, productW, GUID_SIZE,
1553                              lpProductBuf, GUID_SIZE, NULL, NULL );
1554     }
1555     msi_free( szwUpgradeCode);
1556     return r;
1557 }
1558
1559 /***********************************************************************
1560  * MsiEnumPatchesA            [MSI.@]
1561  */
1562 UINT WINAPI MsiEnumPatchesA( LPCSTR szProduct, DWORD iPatchIndex,
1563         LPSTR lpPatchBuf, LPSTR lpTransformsBuf, LPDWORD pcchTransformsBuf)
1564 {
1565     FIXME("%s %d %p %p %p\n", debugstr_a(szProduct),
1566           iPatchIndex, lpPatchBuf, lpTransformsBuf, pcchTransformsBuf);
1567     return ERROR_NO_MORE_ITEMS;
1568 }
1569
1570 /***********************************************************************
1571  * MsiEnumPatchesW            [MSI.@]
1572  */
1573 UINT WINAPI MsiEnumPatchesW( LPCWSTR szProduct, DWORD iPatchIndex,
1574         LPWSTR lpPatchBuf, LPWSTR lpTransformsBuf, LPDWORD pcchTransformsBuf)
1575 {
1576     FIXME("%s %d %p %p %p\n", debugstr_w(szProduct),
1577           iPatchIndex, lpPatchBuf, lpTransformsBuf, pcchTransformsBuf);
1578     return ERROR_NO_MORE_ITEMS;
1579 }
1580
1581 UINT WINAPI MsiEnumProductsExA( LPCSTR szProductCode, LPCSTR szUserSid,
1582         DWORD dwContext, DWORD dwIndex, CHAR szInstalledProductCode[39],
1583         MSIINSTALLCONTEXT* pdwInstalledContext, LPSTR szSid, LPDWORD pcchSid)
1584 {
1585     FIXME("%s %s %d %d %p %p %p %p\n", debugstr_a(szProductCode), debugstr_a(szUserSid),
1586           dwContext, dwIndex, szInstalledProductCode, pdwInstalledContext,
1587           szSid, pcchSid);
1588     return ERROR_NO_MORE_ITEMS;
1589 }
1590
1591 UINT WINAPI MsiEnumProductsExW( LPCWSTR szProductCode, LPCWSTR szUserSid,
1592         DWORD dwContext, DWORD dwIndex, WCHAR szInstalledProductCode[39],
1593         MSIINSTALLCONTEXT* pdwInstalledContext, LPWSTR szSid, LPDWORD pcchSid)
1594 {
1595     FIXME("%s %s %d %d %p %p %p %p\n", debugstr_w(szProductCode), debugstr_w(szUserSid),
1596           dwContext, dwIndex, szInstalledProductCode, pdwInstalledContext,
1597           szSid, pcchSid);
1598     return ERROR_NO_MORE_ITEMS;
1599 }