mshtml: Added IHTMLTxtRange::compareEndPoints implementation.
[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_OpenUserProductsKey(LPCWSTR szProduct, HKEY* key, BOOL create)
508 {
509     UINT rc;
510     WCHAR squished_pc[GUID_SIZE];
511     WCHAR keypath[0x200];
512
513     TRACE("%s\n",debugstr_w(szProduct));
514     if (!squash_guid(szProduct,squished_pc))
515         return ERROR_FUNCTION_FAILED;
516     TRACE("squished (%s)\n", debugstr_w(squished_pc));
517
518     sprintfW(keypath,szUserProduct_fmt,squished_pc);
519
520     if (create)
521         rc = RegCreateKeyW(HKEY_CURRENT_USER,keypath,key);
522     else
523         rc = RegOpenKeyW(HKEY_CURRENT_USER,keypath,key);
524
525     return rc;
526 }
527
528 UINT MSIREG_DeleteUserProductKey(LPCWSTR szProduct)
529 {
530     WCHAR squished_pc[GUID_SIZE];
531     WCHAR keypath[0x200];
532
533     TRACE("%s\n",debugstr_w(szProduct));
534     if (!squash_guid(szProduct,squished_pc))
535         return ERROR_FUNCTION_FAILED;
536     TRACE("squished (%s)\n", debugstr_w(squished_pc));
537
538     sprintfW(keypath,szUserProduct_fmt,squished_pc);
539
540     return RegDeleteTreeW(HKEY_CURRENT_USER, keypath);
541 }
542
543 UINT MSIREG_OpenUserPatchesKey(LPCWSTR szPatch, HKEY* key, BOOL create)
544 {
545     UINT rc;
546     WCHAR squished_pc[GUID_SIZE];
547     WCHAR keypath[0x200];
548
549     TRACE("%s\n",debugstr_w(szPatch));
550     if (!squash_guid(szPatch,squished_pc))
551         return ERROR_FUNCTION_FAILED;
552     TRACE("squished (%s)\n", debugstr_w(squished_pc));
553
554     sprintfW(keypath,szUserPatch_fmt,squished_pc);
555
556     if (create)
557         rc = RegCreateKeyW(HKEY_CURRENT_USER,keypath,key);
558     else
559         rc = RegOpenKeyW(HKEY_CURRENT_USER,keypath,key);
560
561     return rc;
562 }
563
564 UINT MSIREG_OpenUserFeaturesKey(LPCWSTR szProduct, HKEY* key, BOOL create)
565 {
566     UINT rc;
567     WCHAR squished_pc[GUID_SIZE];
568     WCHAR keypath[0x200];
569
570     TRACE("%s\n",debugstr_w(szProduct));
571     if (!squash_guid(szProduct,squished_pc))
572         return ERROR_FUNCTION_FAILED;
573     TRACE("squished (%s)\n", debugstr_w(squished_pc));
574
575     sprintfW(keypath,szUserFeatures_fmt,squished_pc);
576
577     if (create)
578         rc = RegCreateKeyW(HKEY_CURRENT_USER,keypath,key);
579     else
580         rc = RegOpenKeyW(HKEY_CURRENT_USER,keypath,key);
581
582     return rc;
583 }
584
585 UINT MSIREG_DeleteUserFeaturesKey(LPCWSTR szProduct)
586 {
587     WCHAR squished_pc[GUID_SIZE];
588     WCHAR keypath[0x200];
589
590     TRACE("%s\n",debugstr_w(szProduct));
591     if (!squash_guid(szProduct,squished_pc))
592         return ERROR_FUNCTION_FAILED;
593     TRACE("squished (%s)\n", debugstr_w(squished_pc));
594
595     sprintfW(keypath,szUserFeatures_fmt,squished_pc);
596     return RegDeleteTreeW(HKEY_CURRENT_USER, keypath);
597 }
598
599 UINT MSIREG_OpenFeatures(HKEY* key)
600 {
601     return RegCreateKeyW(HKEY_LOCAL_MACHINE,szInstaller_Features,key);
602 }
603
604 UINT MSIREG_OpenFeaturesKey(LPCWSTR szProduct, HKEY* key, BOOL create)
605 {
606     UINT rc;
607     WCHAR squished_pc[GUID_SIZE];
608     WCHAR keypath[0x200];
609
610     TRACE("%s\n",debugstr_w(szProduct));
611     if (!squash_guid(szProduct,squished_pc))
612         return ERROR_FUNCTION_FAILED;
613     TRACE("squished (%s)\n", debugstr_w(squished_pc));
614
615     sprintfW(keypath,szInstaller_Features_fmt,squished_pc);
616
617     if (create)
618         rc = RegCreateKeyW(HKEY_LOCAL_MACHINE,keypath,key);
619     else
620         rc = RegOpenKeyW(HKEY_LOCAL_MACHINE,keypath,key);
621
622     return rc;
623 }
624
625 UINT MSIREG_OpenUserDataFeaturesKey(LPCWSTR szProduct, HKEY *key, BOOL create)
626 {
627     UINT rc;
628     WCHAR squished_pc[GUID_SIZE];
629     WCHAR keypath[0x200];
630     LPWSTR usersid;
631
632     TRACE("%s\n", debugstr_w(szProduct));
633     if (!squash_guid(szProduct, squished_pc))
634         return ERROR_FUNCTION_FAILED;
635     TRACE("squished (%s)\n", debugstr_w(squished_pc));
636
637     rc = get_user_sid(&usersid);
638     if (rc != ERROR_SUCCESS || !usersid)
639     {
640         ERR("Failed to retrieve user SID: %d\n", rc);
641         return rc;
642     }
643
644     sprintfW(keypath, szUserDataFeatures_fmt, usersid, squished_pc);
645
646     if (create)
647         rc = RegCreateKeyW(HKEY_LOCAL_MACHINE, keypath, key);
648     else
649         rc = RegOpenKeyW(HKEY_LOCAL_MACHINE, keypath, key);
650
651     msi_free(usersid);
652     return rc;
653 }
654
655 UINT MSIREG_OpenComponents(HKEY* key)
656 {
657     return RegCreateKeyW(HKEY_LOCAL_MACHINE,szInstaller_Components,key);
658 }
659
660 UINT MSIREG_OpenComponentsKey(LPCWSTR szComponent, HKEY* key, BOOL create)
661 {
662     UINT rc;
663     WCHAR squished_cc[GUID_SIZE];
664     WCHAR keypath[0x200];
665
666     TRACE("%s\n",debugstr_w(szComponent));
667     if (!squash_guid(szComponent,squished_cc))
668         return ERROR_FUNCTION_FAILED;
669     TRACE("squished (%s)\n", debugstr_w(squished_cc));
670
671     sprintfW(keypath,szInstaller_Components_fmt,squished_cc);
672
673     if (create)
674         rc = RegCreateKeyW(HKEY_LOCAL_MACHINE,keypath,key);
675     else
676         rc = RegOpenKeyW(HKEY_LOCAL_MACHINE,keypath,key);
677
678     return rc;
679 }
680
681 UINT MSIREG_OpenUserComponentsKey(LPCWSTR szComponent, HKEY* key, BOOL create)
682 {
683     UINT rc;
684     WCHAR squished_cc[GUID_SIZE];
685     WCHAR keypath[0x200];
686
687     TRACE("%s\n",debugstr_w(szComponent));
688     if (!squash_guid(szComponent,squished_cc))
689         return ERROR_FUNCTION_FAILED;
690     TRACE("squished (%s)\n", debugstr_w(squished_cc));
691
692     sprintfW(keypath,szUser_Components_fmt,squished_cc);
693
694     if (create)
695         rc = RegCreateKeyW(HKEY_CURRENT_USER,keypath,key);
696     else
697         rc = RegOpenKeyW(HKEY_CURRENT_USER,keypath,key);
698
699     return rc;
700 }
701
702 UINT MSIREG_OpenUserDataComponentKey(LPCWSTR szComponent, HKEY *key, BOOL create)
703 {
704     UINT rc;
705     WCHAR comp[GUID_SIZE];
706     WCHAR keypath[0x200];
707     LPWSTR usersid;
708
709     TRACE("%s\n", debugstr_w(szComponent));
710     if (!squash_guid(szComponent, comp))
711         return ERROR_FUNCTION_FAILED;
712     TRACE("squished (%s)\n", debugstr_w(comp));
713
714     rc = get_user_sid(&usersid);
715     if (rc != ERROR_SUCCESS || !usersid)
716     {
717         ERR("Failed to retrieve user SID: %d\n", rc);
718         return rc;
719     }
720
721     sprintfW(keypath, szUserDataComp_fmt, usersid, comp);
722
723     if (create)
724         rc = RegCreateKeyW(HKEY_LOCAL_MACHINE, keypath, key);
725     else
726         rc = RegOpenKeyW(HKEY_LOCAL_MACHINE, keypath, key);
727
728     msi_free(usersid);
729     return rc;
730 }
731
732 UINT MSIREG_OpenUserDataProductKey(LPCWSTR szProduct, HKEY *key, BOOL create)
733 {
734     UINT rc;
735     WCHAR squished_pc[GUID_SIZE];
736     WCHAR keypath[0x200];
737     LPWSTR usersid;
738
739     TRACE("%s\n", debugstr_w(szProduct));
740     if (!squash_guid(szProduct, squished_pc))
741         return ERROR_FUNCTION_FAILED;
742     TRACE("squished (%s)\n", debugstr_w(squished_pc));
743
744     rc = get_user_sid(&usersid);
745     if (rc != ERROR_SUCCESS || !usersid)
746     {
747         ERR("Failed to retrieve user SID: %d\n", rc);
748         return rc;
749     }
750
751     sprintfW(keypath, szUserDataProd_fmt, usersid, squished_pc);
752
753     if (create)
754         rc = RegCreateKeyW(HKEY_LOCAL_MACHINE, keypath, key);
755     else
756         rc = RegOpenKeyW(HKEY_LOCAL_MACHINE, keypath, key);
757
758     msi_free(usersid);
759     return rc;
760 }
761
762 UINT MSIREG_OpenInstallPropertiesKey(LPCWSTR szProduct, HKEY *key, BOOL create)
763 {
764     UINT rc;
765     WCHAR squished_pc[GUID_SIZE];
766     WCHAR keypath[0x200];
767     LPWSTR usersid;
768
769     TRACE("%s\n", debugstr_w(szProduct));
770     if (!squash_guid(szProduct, squished_pc))
771         return ERROR_FUNCTION_FAILED;
772     TRACE("squished (%s)\n", debugstr_w(squished_pc));
773
774     rc = get_user_sid(&usersid);
775     if (rc != ERROR_SUCCESS || !usersid)
776     {
777         ERR("Failed to retrieve user SID: %d\n", rc);
778         return rc;
779     }
780
781     sprintfW(keypath, szInstallProperties_fmt, usersid, squished_pc);
782
783     if (create)
784         rc = RegCreateKeyW(HKEY_LOCAL_MACHINE, keypath, key);
785     else
786         rc = RegOpenKeyW(HKEY_LOCAL_MACHINE, keypath, key);
787
788     msi_free(usersid);
789     return rc;
790 }
791
792 UINT MSIREG_DeleteUserDataProductKey(LPCWSTR szProduct)
793 {
794     UINT rc;
795     WCHAR squished_pc[GUID_SIZE];
796     WCHAR keypath[0x200];
797     LPWSTR usersid;
798
799     TRACE("%s\n", debugstr_w(szProduct));
800     if (!squash_guid(szProduct, squished_pc))
801         return ERROR_FUNCTION_FAILED;
802     TRACE("squished (%s)\n", debugstr_w(squished_pc));
803
804     rc = get_user_sid(&usersid);
805     if (rc != ERROR_SUCCESS || !usersid)
806     {
807         ERR("Failed to retrieve user SID: %d\n", rc);
808         return rc;
809     }
810
811     sprintfW(keypath, szUserDataProd_fmt, usersid, squished_pc);
812
813     msi_free(usersid);
814     return RegDeleteTreeW(HKEY_LOCAL_MACHINE, keypath);
815 }
816
817 UINT MSIREG_OpenProducts(HKEY* key)
818 {
819     return RegCreateKeyW(HKEY_LOCAL_MACHINE,szInstaller_Products,key);
820 }
821
822 UINT MSIREG_OpenProductsKey(LPCWSTR szProduct, HKEY* key, BOOL create)
823 {
824     UINT rc;
825     WCHAR squished_pc[GUID_SIZE];
826     WCHAR keypath[0x200];
827
828     TRACE("%s\n",debugstr_w(szProduct));
829     if (!squash_guid(szProduct,squished_pc))
830         return ERROR_FUNCTION_FAILED;
831     TRACE("squished (%s)\n", debugstr_w(squished_pc));
832
833     sprintfW(keypath,szInstaller_Products_fmt,squished_pc);
834
835     if (create)
836         rc = RegCreateKeyW(HKEY_LOCAL_MACHINE,keypath,key);
837     else
838         rc = RegOpenKeyW(HKEY_LOCAL_MACHINE,keypath,key);
839
840     return rc;
841 }
842
843 UINT MSIREG_DeleteProductKey(LPCWSTR szProduct)
844 {
845     WCHAR squished_pc[GUID_SIZE];
846     WCHAR keypath[0x200];
847
848     TRACE("%s\n", debugstr_w(szProduct));
849     if (!squash_guid(szProduct, squished_pc))
850         return ERROR_FUNCTION_FAILED;
851     TRACE("squished (%s)\n", debugstr_w(squished_pc));
852
853     sprintfW(keypath, szInstaller_Products_fmt, squished_pc);
854
855     return RegDeleteTreeW(HKEY_LOCAL_MACHINE, keypath);
856 }
857
858 UINT MSIREG_OpenPatchesKey(LPCWSTR szPatch, HKEY* key, BOOL create)
859 {
860     UINT rc;
861     WCHAR squished_pc[GUID_SIZE];
862     WCHAR keypath[0x200];
863
864     TRACE("%s\n",debugstr_w(szPatch));
865     if (!squash_guid(szPatch,squished_pc))
866         return ERROR_FUNCTION_FAILED;
867     TRACE("squished (%s)\n", debugstr_w(squished_pc));
868
869     sprintfW(keypath,szInstaller_Patches_fmt,squished_pc);
870
871     if (create)
872         rc = RegCreateKeyW(HKEY_LOCAL_MACHINE,keypath,key);
873     else
874         rc = RegOpenKeyW(HKEY_LOCAL_MACHINE,keypath,key);
875
876     return rc;
877 }
878
879 UINT MSIREG_OpenUpgradeCodesKey(LPCWSTR szUpgradeCode, HKEY* key, BOOL create)
880 {
881     UINT rc;
882     WCHAR squished_pc[GUID_SIZE];
883     WCHAR keypath[0x200];
884
885     TRACE("%s\n",debugstr_w(szUpgradeCode));
886     if (!squash_guid(szUpgradeCode,squished_pc))
887         return ERROR_FUNCTION_FAILED;
888     TRACE("squished (%s)\n", debugstr_w(squished_pc));
889
890     sprintfW(keypath,szInstaller_UpgradeCodes_fmt,squished_pc);
891
892     if (create)
893         rc = RegCreateKeyW(HKEY_LOCAL_MACHINE,keypath,key);
894     else
895         rc = RegOpenKeyW(HKEY_LOCAL_MACHINE,keypath,key);
896
897     return rc;
898 }
899
900 UINT MSIREG_OpenUserUpgradeCodesKey(LPCWSTR szUpgradeCode, HKEY* key, BOOL create)
901 {
902     UINT rc;
903     WCHAR squished_pc[GUID_SIZE];
904     WCHAR keypath[0x200];
905
906     TRACE("%s\n",debugstr_w(szUpgradeCode));
907     if (!squash_guid(szUpgradeCode,squished_pc))
908         return ERROR_FUNCTION_FAILED;
909     TRACE("squished (%s)\n", debugstr_w(squished_pc));
910
911     sprintfW(keypath,szInstaller_UserUpgradeCodes_fmt,squished_pc);
912
913     if (create)
914         rc = RegCreateKeyW(HKEY_CURRENT_USER,keypath,key);
915     else
916         rc = RegOpenKeyW(HKEY_CURRENT_USER,keypath,key);
917
918     return rc;
919 }
920
921 UINT MSIREG_OpenLocalSystemProductKey(LPCWSTR szProductCode, HKEY *key, BOOL create)
922 {
923     WCHAR squished_pc[GUID_SIZE];
924     WCHAR keypath[0x200];
925
926     TRACE("%s\n", debugstr_w(szProductCode));
927
928     if (!squash_guid(szProductCode, squished_pc))
929         return ERROR_FUNCTION_FAILED;
930
931     TRACE("squished (%s)\n", debugstr_w(squished_pc));
932
933     sprintfW(keypath, szInstaller_LocalSystemProductCodes_fmt, squished_pc);
934
935     if (create)
936         return RegCreateKeyW(HKEY_LOCAL_MACHINE, keypath, key);
937
938     return RegOpenKeyW(HKEY_LOCAL_MACHINE, keypath, key);
939 }
940
941 UINT MSIREG_OpenLocalSystemComponentKey(LPCWSTR szComponent, HKEY *key, BOOL create)
942 {
943     WCHAR squished_pc[GUID_SIZE];
944     WCHAR keypath[0x200];
945
946     TRACE("%s\n", debugstr_w(szComponent));
947
948     if (!squash_guid(szComponent, squished_pc))
949         return ERROR_FUNCTION_FAILED;
950
951     TRACE("squished (%s)\n", debugstr_w(squished_pc));
952
953     sprintfW(keypath, szInstaller_LocalSystemComponent_fmt, squished_pc);
954
955     if (create)
956         return RegCreateKeyW(HKEY_LOCAL_MACHINE, keypath, key);
957
958     return RegOpenKeyW(HKEY_LOCAL_MACHINE, keypath, key);
959 }
960
961 UINT MSIREG_OpenLocalClassesProductKey(LPCWSTR szProductCode, HKEY *key, BOOL create)
962 {
963     WCHAR squished_pc[GUID_SIZE];
964     WCHAR keypath[0x200];
965
966     TRACE("%s\n", debugstr_w(szProductCode));
967
968     if (!squash_guid(szProductCode, squished_pc))
969         return ERROR_FUNCTION_FAILED;
970
971     TRACE("squished (%s)\n", debugstr_w(squished_pc));
972
973     sprintfW(keypath, szInstaller_LocalClassesProd_fmt, squished_pc);
974
975     if (create)
976         return RegCreateKeyW(HKEY_LOCAL_MACHINE, keypath, key);
977
978     return RegOpenKeyW(HKEY_LOCAL_MACHINE, keypath, key);
979 }
980
981 UINT MSIREG_OpenLocalManagedProductKey(LPCWSTR szProductCode, HKEY *key, BOOL create)
982 {
983     WCHAR squished_pc[GUID_SIZE];
984     WCHAR keypath[0x200];
985     LPWSTR usersid;
986     UINT r;
987
988     TRACE("%s\n", debugstr_w(szProductCode));
989
990     if (!squash_guid(szProductCode, squished_pc))
991         return ERROR_FUNCTION_FAILED;
992
993     TRACE("squished (%s)\n", debugstr_w(squished_pc));
994
995     r = get_user_sid(&usersid);
996     if (r != ERROR_SUCCESS || !usersid)
997     {
998         ERR("Failed to retrieve user SID: %d\n", r);
999         return r;
1000     }
1001
1002     sprintfW(keypath, szInstaller_LocalManagedProd_fmt, usersid, squished_pc);
1003     msi_free(usersid);
1004
1005     if (create)
1006         return RegCreateKeyW(HKEY_LOCAL_MACHINE, keypath, key);
1007
1008     return RegOpenKeyW(HKEY_LOCAL_MACHINE, keypath, key);
1009 }
1010
1011 /*************************************************************************
1012  *  MsiDecomposeDescriptorW   [MSI.@]
1013  *
1014  * Decomposes an MSI descriptor into product, feature and component parts.
1015  * An MSI descriptor is a string of the form:
1016  *   [base 85 guid] [feature code] '>' [base 85 guid]
1017  *
1018  * PARAMS
1019  *   szDescriptor  [I]  the descriptor to decompose
1020  *   szProduct     [O]  buffer of MAX_FEATURE_CHARS+1 for the product guid
1021  *   szFeature     [O]  buffer of MAX_FEATURE_CHARS+1 for the feature code
1022  *   szComponent   [O]  buffer of MAX_FEATURE_CHARS+1 for the component guid
1023  *   pUsed         [O]  the length of the descriptor
1024  *
1025  * RETURNS
1026  *   ERROR_SUCCESS             if everything worked correctly
1027  *   ERROR_INVALID_PARAMETER   if the descriptor was invalid
1028  *
1029  */
1030 UINT WINAPI MsiDecomposeDescriptorW( LPCWSTR szDescriptor, LPWSTR szProduct,
1031                 LPWSTR szFeature, LPWSTR szComponent, LPDWORD pUsed )
1032 {
1033     UINT r, len;
1034     LPWSTR p;
1035     GUID product, component;
1036
1037     TRACE("%s %p %p %p %p\n", debugstr_w(szDescriptor), szProduct,
1038           szFeature, szComponent, pUsed);
1039
1040     r = decode_base85_guid( szDescriptor, &product );
1041     if( !r )
1042         return ERROR_INVALID_PARAMETER;
1043
1044     TRACE("product %s\n", debugstr_guid( &product ));
1045
1046     p = strchrW(&szDescriptor[20],'>');
1047     if( !p )
1048         return ERROR_INVALID_PARAMETER;
1049
1050     len = (p - &szDescriptor[20]);
1051     if( len > MAX_FEATURE_CHARS )
1052         return ERROR_INVALID_PARAMETER;
1053
1054     TRACE("feature %s\n", debugstr_wn( &szDescriptor[20], len ));
1055
1056     r = decode_base85_guid( p+1, &component );
1057     if( !r )
1058         return ERROR_INVALID_PARAMETER;
1059
1060     TRACE("component %s\n", debugstr_guid( &component ));
1061
1062     if (szProduct)
1063         StringFromGUID2( &product, szProduct, MAX_FEATURE_CHARS+1 );
1064     if (szComponent)
1065         StringFromGUID2( &component, szComponent, MAX_FEATURE_CHARS+1 );
1066     if (szFeature)
1067     {
1068         memcpy( szFeature, &szDescriptor[20], len*sizeof(WCHAR) );
1069         szFeature[len] = 0;
1070     }
1071     len = ( &p[21] - szDescriptor );
1072
1073     TRACE("length = %d\n", len);
1074     *pUsed = len;
1075
1076     return ERROR_SUCCESS;
1077 }
1078
1079 UINT WINAPI MsiDecomposeDescriptorA( LPCSTR szDescriptor, LPSTR szProduct,
1080                 LPSTR szFeature, LPSTR szComponent, LPDWORD pUsed )
1081 {
1082     WCHAR product[MAX_FEATURE_CHARS+1];
1083     WCHAR feature[MAX_FEATURE_CHARS+1];
1084     WCHAR component[MAX_FEATURE_CHARS+1];
1085     LPWSTR str = NULL, p = NULL, f = NULL, c = NULL;
1086     UINT r;
1087
1088     TRACE("%s %p %p %p %p\n", debugstr_a(szDescriptor), szProduct,
1089           szFeature, szComponent, pUsed);
1090
1091     str = strdupAtoW( szDescriptor );
1092     if( szDescriptor && !str )
1093         return ERROR_OUTOFMEMORY;
1094
1095     if (szProduct)
1096         p = product;
1097     if (szFeature)
1098         f = feature;
1099     if (szComponent)
1100         c = component;
1101
1102     r = MsiDecomposeDescriptorW( str, p, f, c, pUsed );
1103
1104     if (r == ERROR_SUCCESS)
1105     {
1106         WideCharToMultiByte( CP_ACP, 0, p, -1,
1107                              szProduct, MAX_FEATURE_CHARS+1, NULL, NULL );
1108         WideCharToMultiByte( CP_ACP, 0, f, -1,
1109                              szFeature, MAX_FEATURE_CHARS+1, NULL, NULL );
1110         WideCharToMultiByte( CP_ACP, 0, c, -1,
1111                              szComponent, MAX_FEATURE_CHARS+1, NULL, NULL );
1112     }
1113
1114     msi_free( str );
1115
1116     return r;
1117 }
1118
1119 UINT WINAPI MsiEnumProductsA(DWORD index, LPSTR lpguid)
1120 {
1121     DWORD r;
1122     WCHAR szwGuid[GUID_SIZE];
1123
1124     TRACE("%d %p\n", index, lpguid);
1125
1126     if (NULL == lpguid)
1127         return ERROR_INVALID_PARAMETER;
1128     r = MsiEnumProductsW(index, szwGuid);
1129     if( r == ERROR_SUCCESS )
1130         WideCharToMultiByte(CP_ACP, 0, szwGuid, -1, lpguid, GUID_SIZE, NULL, NULL);
1131
1132     return r;
1133 }
1134
1135 UINT WINAPI MsiEnumProductsW(DWORD index, LPWSTR lpguid)
1136 {
1137     HKEY hkeyProducts = 0;
1138     DWORD r;
1139     WCHAR szKeyName[SQUISH_GUID_SIZE];
1140
1141     TRACE("%d %p\n", index, lpguid);
1142
1143     if (NULL == lpguid)
1144         return ERROR_INVALID_PARAMETER;
1145
1146     r = MSIREG_OpenProducts(&hkeyProducts);
1147     if( r != ERROR_SUCCESS )
1148         return ERROR_NO_MORE_ITEMS;
1149
1150     r = RegEnumKeyW(hkeyProducts, index, szKeyName, SQUISH_GUID_SIZE);
1151     if( r == ERROR_SUCCESS )
1152         unsquash_guid(szKeyName, lpguid);
1153     RegCloseKey(hkeyProducts);
1154
1155     return r;
1156 }
1157
1158 UINT WINAPI MsiEnumFeaturesA(LPCSTR szProduct, DWORD index, 
1159       LPSTR szFeature, LPSTR szParent)
1160 {
1161     DWORD r;
1162     WCHAR szwFeature[GUID_SIZE], szwParent[GUID_SIZE];
1163     LPWSTR szwProduct = NULL;
1164
1165     TRACE("%s %d %p %p\n", debugstr_a(szProduct), index, szFeature, szParent);
1166
1167     if( szProduct )
1168     {
1169         szwProduct = strdupAtoW( szProduct );
1170         if( !szwProduct )
1171             return ERROR_OUTOFMEMORY;
1172     }
1173
1174     r = MsiEnumFeaturesW(szwProduct, index, szwFeature, szwParent);
1175     if( r == ERROR_SUCCESS )
1176     {
1177         WideCharToMultiByte(CP_ACP, 0, szwFeature, -1,
1178                             szFeature, GUID_SIZE, NULL, NULL);
1179         WideCharToMultiByte(CP_ACP, 0, szwParent, -1,
1180                             szParent, GUID_SIZE, NULL, NULL);
1181     }
1182
1183     msi_free( szwProduct);
1184
1185     return r;
1186 }
1187
1188 UINT WINAPI MsiEnumFeaturesW(LPCWSTR szProduct, DWORD index, 
1189       LPWSTR szFeature, LPWSTR szParent)
1190 {
1191     HKEY hkeyProduct = 0;
1192     DWORD r, sz;
1193
1194     TRACE("%s %d %p %p\n", debugstr_w(szProduct), index, szFeature, szParent);
1195
1196     if( !szProduct )
1197         return ERROR_INVALID_PARAMETER;
1198
1199     r = MSIREG_OpenFeaturesKey(szProduct,&hkeyProduct,FALSE);
1200     if( r != ERROR_SUCCESS )
1201         return ERROR_NO_MORE_ITEMS;
1202
1203     sz = GUID_SIZE;
1204     r = RegEnumValueW(hkeyProduct, index, szFeature, &sz, NULL, NULL, NULL, NULL);
1205     RegCloseKey(hkeyProduct);
1206
1207     return r;
1208 }
1209
1210 UINT WINAPI MsiEnumComponentsA(DWORD index, LPSTR lpguid)
1211 {
1212     DWORD r;
1213     WCHAR szwGuid[GUID_SIZE];
1214
1215     TRACE("%d %p\n", index, lpguid);
1216
1217     r = MsiEnumComponentsW(index, szwGuid);
1218     if( r == ERROR_SUCCESS )
1219         WideCharToMultiByte(CP_ACP, 0, szwGuid, -1, lpguid, GUID_SIZE, NULL, NULL);
1220
1221     return r;
1222 }
1223
1224 UINT WINAPI MsiEnumComponentsW(DWORD index, LPWSTR lpguid)
1225 {
1226     HKEY hkeyComponents = 0;
1227     DWORD r;
1228     WCHAR szKeyName[SQUISH_GUID_SIZE];
1229
1230     TRACE("%d %p\n", index, lpguid);
1231
1232     r = MSIREG_OpenComponents(&hkeyComponents);
1233     if( r != ERROR_SUCCESS )
1234         return ERROR_NO_MORE_ITEMS;
1235
1236     r = RegEnumKeyW(hkeyComponents, index, szKeyName, SQUISH_GUID_SIZE);
1237     if( r == ERROR_SUCCESS )
1238         unsquash_guid(szKeyName, lpguid);
1239     RegCloseKey(hkeyComponents);
1240
1241     return r;
1242 }
1243
1244 UINT WINAPI MsiEnumClientsA(LPCSTR szComponent, DWORD index, LPSTR szProduct)
1245 {
1246     DWORD r;
1247     WCHAR szwProduct[GUID_SIZE];
1248     LPWSTR szwComponent = NULL;
1249
1250     TRACE("%s %d %p\n", debugstr_a(szComponent), index, szProduct);
1251
1252     if( szComponent )
1253     {
1254         szwComponent = strdupAtoW( szComponent );
1255         if( !szwComponent )
1256             return ERROR_OUTOFMEMORY;
1257     }
1258
1259     r = MsiEnumClientsW(szComponent?szwComponent:NULL, index, szwProduct);
1260     if( r == ERROR_SUCCESS )
1261     {
1262         WideCharToMultiByte(CP_ACP, 0, szwProduct, -1,
1263                             szProduct, GUID_SIZE, NULL, NULL);
1264     }
1265
1266     msi_free( szwComponent);
1267
1268     return r;
1269 }
1270
1271 UINT WINAPI MsiEnumClientsW(LPCWSTR szComponent, DWORD index, LPWSTR szProduct)
1272 {
1273     HKEY hkeyComp = 0;
1274     DWORD r, sz;
1275     WCHAR szValName[SQUISH_GUID_SIZE];
1276
1277     TRACE("%s %d %p\n", debugstr_w(szComponent), index, szProduct);
1278
1279     r = MSIREG_OpenComponentsKey(szComponent,&hkeyComp,FALSE);
1280     if( r != ERROR_SUCCESS )
1281         return ERROR_NO_MORE_ITEMS;
1282
1283     sz = SQUISH_GUID_SIZE;
1284     r = RegEnumValueW(hkeyComp, index, szValName, &sz, NULL, NULL, NULL, NULL);
1285     if( r == ERROR_SUCCESS )
1286         unsquash_guid(szValName, szProduct);
1287
1288     RegCloseKey(hkeyComp);
1289
1290     return r;
1291 }
1292
1293 static UINT WINAPI MSI_EnumComponentQualifiers( LPCWSTR szComponent, DWORD iIndex,
1294                 awstring *lpQualBuf, LPDWORD pcchQual,
1295                 awstring *lpAppBuf, LPDWORD pcchAppBuf )
1296 {
1297     DWORD name_sz, val_sz, name_max, val_max, type, ofs;
1298     LPWSTR name = NULL, val = NULL;
1299     UINT r, r2;
1300     HKEY key;
1301
1302     TRACE("%s %08x %p %p %p %p\n", debugstr_w(szComponent), iIndex,
1303           lpQualBuf, pcchQual, lpAppBuf, pcchAppBuf);
1304
1305     if (!szComponent)
1306         return ERROR_INVALID_PARAMETER;
1307
1308     r = MSIREG_OpenUserComponentsKey( szComponent, &key, FALSE );
1309     if (r != ERROR_SUCCESS)
1310         return ERROR_UNKNOWN_COMPONENT;
1311
1312     /* figure out how big the name is we want to return */
1313     name_max = 0x10;
1314     r = ERROR_OUTOFMEMORY;
1315     name = msi_alloc( name_max * sizeof(WCHAR) );
1316     if (!name)
1317         goto end;
1318
1319     val_max = 0x10;
1320     r = ERROR_OUTOFMEMORY;
1321     val = msi_alloc( val_max );
1322     if (!val)
1323         goto end;
1324
1325     /* loop until we allocate enough memory */
1326     while (1)
1327     {
1328         name_sz = name_max;
1329         val_sz = val_max;
1330         r = RegEnumValueW( key, iIndex, name, &name_sz,
1331                            NULL, &type, (LPBYTE)val, &val_sz );
1332         if (r == ERROR_SUCCESS)
1333             break;
1334         if (r != ERROR_MORE_DATA)
1335             goto end;
1336  
1337         if (type != REG_MULTI_SZ)
1338         {
1339             ERR("component data has wrong type (%d)\n", type);
1340             goto end;
1341         }
1342
1343         r = ERROR_OUTOFMEMORY;
1344         if ((name_sz+1) >= name_max)
1345         {
1346             name_max *= 2;
1347             msi_free( name );
1348             name = msi_alloc( name_max * sizeof (WCHAR) );
1349             if (!name)
1350                 goto end;
1351             continue;
1352         }
1353         if (val_sz > val_max)
1354         {
1355             val_max = val_sz + sizeof (WCHAR);
1356             msi_free( val );
1357             val = msi_alloc( val_max * sizeof (WCHAR) );
1358             if (!val)
1359                 goto end;
1360             continue;
1361         }
1362         ERR("should be enough data, but isn't %d %d\n", name_sz, val_sz );
1363         goto end;
1364     }
1365
1366     ofs = 0;
1367     r = MsiDecomposeDescriptorW( val, NULL, NULL, NULL, &ofs );
1368     if (r != ERROR_SUCCESS)
1369         goto end;
1370
1371     TRACE("Providing %s and %s\n", debugstr_w(name), debugstr_w(val+ofs));
1372
1373     r = msi_strcpy_to_awstring( name, lpQualBuf, pcchQual );
1374     r2 = msi_strcpy_to_awstring( val+ofs, lpAppBuf, pcchAppBuf );
1375
1376     if (r2 != ERROR_SUCCESS)
1377         r = r2;
1378
1379 end:
1380     msi_free(val);
1381     msi_free(name);
1382     RegCloseKey(key);
1383
1384     return r;
1385 }
1386
1387 /*************************************************************************
1388  *  MsiEnumComponentQualifiersA [MSI.@]
1389  */
1390 UINT WINAPI MsiEnumComponentQualifiersA( LPCSTR szComponent, DWORD iIndex,
1391                 LPSTR lpQualifierBuf, LPDWORD pcchQualifierBuf,
1392                 LPSTR lpApplicationDataBuf, LPDWORD pcchApplicationDataBuf )
1393 {
1394     awstring qual, appdata;
1395     LPWSTR comp;
1396     UINT r;
1397
1398     TRACE("%s %08x %p %p %p %p\n", debugstr_a(szComponent), iIndex,
1399           lpQualifierBuf, pcchQualifierBuf, lpApplicationDataBuf,
1400           pcchApplicationDataBuf);
1401
1402     comp = strdupAtoW( szComponent );
1403     if (szComponent && !comp)
1404         return ERROR_OUTOFMEMORY;
1405
1406     qual.unicode = FALSE;
1407     qual.str.a = lpQualifierBuf;
1408
1409     appdata.unicode = FALSE;
1410     appdata.str.a = lpApplicationDataBuf;
1411
1412     r = MSI_EnumComponentQualifiers( comp, iIndex,
1413               &qual, pcchQualifierBuf, &appdata, pcchApplicationDataBuf );
1414     msi_free( comp );
1415     return r;
1416 }
1417
1418 /*************************************************************************
1419  *  MsiEnumComponentQualifiersW [MSI.@]
1420  */
1421 UINT WINAPI MsiEnumComponentQualifiersW( LPCWSTR szComponent, DWORD iIndex,
1422                 LPWSTR lpQualifierBuf, LPDWORD pcchQualifierBuf,
1423                 LPWSTR lpApplicationDataBuf, LPDWORD pcchApplicationDataBuf )
1424 {
1425     awstring qual, appdata;
1426
1427     TRACE("%s %08x %p %p %p %p\n", debugstr_w(szComponent), iIndex,
1428           lpQualifierBuf, pcchQualifierBuf, lpApplicationDataBuf,
1429           pcchApplicationDataBuf);
1430
1431     qual.unicode = TRUE;
1432     qual.str.w = lpQualifierBuf;
1433
1434     appdata.unicode = TRUE;
1435     appdata.str.w = lpApplicationDataBuf;
1436
1437     return MSI_EnumComponentQualifiers( szComponent, iIndex,
1438                  &qual, pcchQualifierBuf, &appdata, pcchApplicationDataBuf );
1439 }
1440
1441 /*************************************************************************
1442  *  MsiEnumRelatedProductsW   [MSI.@]
1443  *
1444  */
1445 UINT WINAPI MsiEnumRelatedProductsW(LPCWSTR szUpgradeCode, DWORD dwReserved,
1446                                     DWORD iProductIndex, LPWSTR lpProductBuf)
1447 {
1448     UINT r;
1449     HKEY hkey;
1450     DWORD dwSize = SQUISH_GUID_SIZE;
1451     WCHAR szKeyName[SQUISH_GUID_SIZE];
1452
1453     TRACE("%s %u %u %p\n", debugstr_w(szUpgradeCode), dwReserved,
1454           iProductIndex, lpProductBuf);
1455
1456     if (NULL == szUpgradeCode)
1457         return ERROR_INVALID_PARAMETER;
1458     if (NULL == lpProductBuf)
1459         return ERROR_INVALID_PARAMETER;
1460
1461     r = MSIREG_OpenUpgradeCodesKey(szUpgradeCode, &hkey, FALSE);
1462     if (r != ERROR_SUCCESS)
1463         return ERROR_NO_MORE_ITEMS;
1464
1465     r = RegEnumValueW(hkey, iProductIndex, szKeyName, &dwSize, NULL, NULL, NULL, NULL);
1466     if( r == ERROR_SUCCESS )
1467         unsquash_guid(szKeyName, lpProductBuf);
1468     RegCloseKey(hkey);
1469
1470     return r;
1471 }
1472
1473 /*************************************************************************
1474  *  MsiEnumRelatedProductsA   [MSI.@]
1475  *
1476  */
1477 UINT WINAPI MsiEnumRelatedProductsA(LPCSTR szUpgradeCode, DWORD dwReserved,
1478                                     DWORD iProductIndex, LPSTR lpProductBuf)
1479 {
1480     LPWSTR szwUpgradeCode = NULL;
1481     WCHAR productW[GUID_SIZE];
1482     UINT r;
1483
1484     TRACE("%s %u %u %p\n", debugstr_a(szUpgradeCode), dwReserved,
1485           iProductIndex, lpProductBuf);
1486
1487     if (szUpgradeCode)
1488     {
1489         szwUpgradeCode = strdupAtoW( szUpgradeCode );
1490         if( !szwUpgradeCode )
1491             return ERROR_OUTOFMEMORY;
1492     }
1493
1494     r = MsiEnumRelatedProductsW( szwUpgradeCode, dwReserved,
1495                                  iProductIndex, productW );
1496     if (r == ERROR_SUCCESS)
1497     {
1498         WideCharToMultiByte( CP_ACP, 0, productW, GUID_SIZE,
1499                              lpProductBuf, GUID_SIZE, NULL, NULL );
1500     }
1501     msi_free( szwUpgradeCode);
1502     return r;
1503 }
1504
1505 /***********************************************************************
1506  * MsiEnumPatchesA            [MSI.@]
1507  */
1508 UINT WINAPI MsiEnumPatchesA( LPCSTR szProduct, DWORD iPatchIndex,
1509         LPSTR lpPatchBuf, LPSTR lpTransformsBuf, LPDWORD pcchTransformsBuf)
1510 {
1511     FIXME("%s %d %p %p %p\n", debugstr_a(szProduct),
1512           iPatchIndex, lpPatchBuf, lpTransformsBuf, pcchTransformsBuf);
1513     return ERROR_NO_MORE_ITEMS;
1514 }
1515
1516 /***********************************************************************
1517  * MsiEnumPatchesW            [MSI.@]
1518  */
1519 UINT WINAPI MsiEnumPatchesW( LPCWSTR szProduct, DWORD iPatchIndex,
1520         LPWSTR lpPatchBuf, LPWSTR lpTransformsBuf, LPDWORD pcchTransformsBuf)
1521 {
1522     FIXME("%s %d %p %p %p\n", debugstr_w(szProduct),
1523           iPatchIndex, lpPatchBuf, lpTransformsBuf, pcchTransformsBuf);
1524     return ERROR_NO_MORE_ITEMS;
1525 }
1526
1527 UINT WINAPI MsiEnumProductsExA( LPCSTR szProductCode, LPCSTR szUserSid,
1528         DWORD dwContext, DWORD dwIndex, CHAR szInstalledProductCode[39],
1529         MSIINSTALLCONTEXT* pdwInstalledContext, LPSTR szSid, LPDWORD pcchSid)
1530 {
1531     FIXME("%s %s %d %d %p %p %p %p\n", debugstr_a(szProductCode), debugstr_a(szUserSid),
1532           dwContext, dwIndex, szInstalledProductCode, pdwInstalledContext,
1533           szSid, pcchSid);
1534     return ERROR_NO_MORE_ITEMS;
1535 }
1536
1537 UINT WINAPI MsiEnumProductsExW( LPCWSTR szProductCode, LPCWSTR szUserSid,
1538         DWORD dwContext, DWORD dwIndex, WCHAR szInstalledProductCode[39],
1539         MSIINSTALLCONTEXT* pdwInstalledContext, LPWSTR szSid, LPDWORD pcchSid)
1540 {
1541     FIXME("%s %s %d %d %p %p %p %p\n", debugstr_w(szProductCode), debugstr_w(szUserSid),
1542           dwContext, dwIndex, szInstalledProductCode, pdwInstalledContext,
1543           szSid, pcchSid);
1544     return ERROR_NO_MORE_ITEMS;
1545 }