In GetDiskFreeSpace:
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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
40 WINE_DEFAULT_DEBUG_CHANNEL(msi);
41
42
43 /* 
44  * This module will be all the helper functions for registry access by the
45  * installer bits. 
46  */
47 static const WCHAR szUserFeatures_fmt[] = {
48 'S','o','f','t','w','a','r','e','\\',
49 'M','i','c','r','o','s','o','f','t','\\',
50 'I','n','s','t','a','l','l','e','r','\\',
51 'F','e','a','t','u','r','e','s','\\',
52 '%','s',0};
53
54 static const WCHAR szInstaller_Features[] = {
55 'S','o','f','t','w','a','r','e','\\',
56 'M','i','c','r','o','s','o','f','t','\\',
57 'W','i','n','d','o','w','s','\\',
58 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
59 'I','n','s','t','a','l','l','e','r','\\',
60 'F','e','a','t','u','r','e','s',0 };
61
62 static const WCHAR szInstaller_Features_fmt[] = {
63 'S','o','f','t','w','a','r','e','\\',
64 'M','i','c','r','o','s','o','f','t','\\',
65 'W','i','n','d','o','w','s','\\',
66 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
67 'I','n','s','t','a','l','l','e','r','\\',
68 'F','e','a','t','u','r','e','s','\\',
69 '%','s',0};
70
71 static const WCHAR szInstaller_Components[] = {
72 'S','o','f','t','w','a','r','e','\\',
73 'M','i','c','r','o','s','o','f','t','\\',
74 'W','i','n','d','o','w','s','\\',
75 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
76 'I','n','s','t','a','l','l','e','r','\\',
77 'C','o','m','p','o','n','e','n','t','s',0 };
78
79 static const WCHAR szInstaller_Components_fmt[] = {
80 'S','o','f','t','w','a','r','e','\\',
81 'M','i','c','r','o','s','o','f','t','\\',
82 'W','i','n','d','o','w','s','\\',
83 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
84 'I','n','s','t','a','l','l','e','r','\\',
85 'C','o','m','p','o','n','e','n','t','s','\\',
86 '%','s',0};
87
88 static const WCHAR szUser_Components_fmt[] = {
89 'S','o','f','t','w','a','r','e','\\',
90 'M','i','c','r','o','s','o','f','t','\\',
91 'I','n','s','t','a','l','l','e','r','\\',
92 'C','o','m','p','o','n','e','n','t','s','\\',
93 '%','s',0};
94
95 static const WCHAR szUninstall_fmt[] = {
96 'S','o','f','t','w','a','r','e','\\',
97 'M','i','c','r','o','s','o','f','t','\\',
98 'W','i','n','d','o','w','s','\\',
99 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
100 'U','n','i','n','s','t','a','l','l','\\',
101 '%','s',0 };
102
103 static const WCHAR szUserProduct_fmt[] = {
104 'S','o','f','t','w','a','r','e','\\',
105 'M','i','c','r','o','s','o','f','t','\\',
106 'I','n','s','t','a','l','l','e','r','\\',
107 'P','r','o','d','u','c','t','s','\\',
108 '%','s',0};
109
110 static const WCHAR szInstaller_Products[] = {
111 'S','o','f','t','w','a','r','e','\\',
112 'M','i','c','r','o','s','o','f','t','\\',
113 'W','i','n','d','o','w','s','\\',
114 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
115 'I','n','s','t','a','l','l','e','r','\\',
116 'P','r','o','d','u','c','t','s',0};
117
118 static const WCHAR szInstaller_Products_fmt[] = {
119 'S','o','f','t','w','a','r','e','\\',
120 'M','i','c','r','o','s','o','f','t','\\',
121 'W','i','n','d','o','w','s','\\',
122 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
123 'I','n','s','t','a','l','l','e','r','\\',
124 'P','r','o','d','u','c','t','s','\\',
125 '%','s',0};
126
127 static const WCHAR szInstaller_UpgradeCodes[] = {
128 'S','o','f','t','w','a','r','e','\\',
129 'M','i','c','r','o','s','o','f','t','\\',
130 'W','i','n','d','o','w','s','\\',
131 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
132 'I','n','s','t','a','l','l','e','r','\\',
133 'U','p','g','r','a','d','e','C','o','d','e','s',0};
134
135 static const WCHAR szInstaller_UpgradeCodes_fmt[] = {
136 'S','o','f','t','w','a','r','e','\\',
137 'M','i','c','r','o','s','o','f','t','\\',
138 'W','i','n','d','o','w','s','\\',
139 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
140 'I','n','s','t','a','l','l','e','r','\\',
141 'U','p','g','r','a','d','e','C','o','d','e','s','\\',
142 '%','s',0};
143
144 static const WCHAR szInstaller_UserUpgradeCodes[] = {
145 'S','o','f','t','w','a','r','e','\\',
146 'M','i','c','r','o','s','o','f','t','\\',
147 'I','n','s','t','a','l','l','e','r','\\',
148 'U','p','g','r','a','d','e','C','o','d','e','s',0};
149
150 static const WCHAR szInstaller_UserUpgradeCodes_fmt[] = {
151 'S','o','f','t','w','a','r','e','\\',
152 'M','i','c','r','o','s','o','f','t','\\',
153 'I','n','s','t','a','l','l','e','r','\\',
154 'U','p','g','r','a','d','e','C','o','d','e','s','\\',
155 '%','s',0};
156
157
158 #define SQUISH_GUID_SIZE 33
159
160 BOOL unsquash_guid(LPCWSTR in, LPWSTR out)
161 {
162     DWORD i,n=0;
163
164     out[n++]='{';
165     for(i=0; i<8; i++)
166         out[n++] = in[7-i];
167     out[n++]='-';
168     for(i=0; i<4; i++)
169         out[n++] = in[11-i];
170     out[n++]='-';
171     for(i=0; i<4; i++)
172         out[n++] = in[15-i];
173     out[n++]='-';
174     for(i=0; i<2; i++)
175     {
176         out[n++] = in[17+i*2];
177         out[n++] = in[16+i*2];
178     }
179     out[n++]='-';
180     for( ; i<8; i++)
181     {
182         out[n++] = in[17+i*2];
183         out[n++] = in[16+i*2];
184     }
185     out[n++]='}';
186     out[n]=0;
187     return TRUE;
188 }
189
190 BOOL squash_guid(LPCWSTR in, LPWSTR out)
191 {
192     DWORD i,n=0;
193
194     if(in[n++] != '{')
195         return FALSE;
196     for(i=0; i<8; i++)
197         out[7-i] = in[n++];
198     if(in[n++] != '-')
199         return FALSE;
200     for(i=0; i<4; i++)
201         out[11-i] = in[n++];
202     if(in[n++] != '-')
203         return FALSE;
204     for(i=0; i<4; i++)
205         out[15-i] = in[n++];
206     if(in[n++] != '-')
207         return FALSE;
208     for(i=0; i<2; i++)
209     {
210         out[17+i*2] = in[n++];
211         out[16+i*2] = in[n++];
212     }
213     if(in[n++] != '-')
214         return FALSE;
215     for( ; i<8; i++)
216     {
217         out[17+i*2] = in[n++];
218         out[16+i*2] = in[n++];
219     }
220     out[32]=0;
221     if(in[n++] != '}')
222         return FALSE;
223     if(in[n])
224         return FALSE;
225     return TRUE;
226 }
227
228
229 /* tables for encoding and decoding base85 */
230 static const unsigned char table_dec85[0x80] = {
231 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
232 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
233 0xff,0x00,0xff,0xff,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0xff,
234 0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14,0x15,0xff,0xff,0xff,0x16,0xff,0x17,
235 0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,
236 0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x31,0x32,0x33,0xff,0x34,0x35,0x36,
237 0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,0x40,0x41,0x42,0x43,0x44,0x45,0x46,
238 0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,0x50,0x51,0x52,0xff,0x53,0x54,0xff,
239 };
240
241 static const char table_enc85[] =
242 "!$%&'()*+,-.0123456789=?@ABCDEFGHIJKLMNO"
243 "PQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwx"
244 "yz{}~";
245
246 /*
247  *  Converts a base85 encoded guid into a GUID pointer
248  *  Base85 encoded GUIDs should be 20 characters long.
249  *
250  *  returns TRUE if successful, FALSE if not
251  */
252 BOOL decode_base85_guid( LPCWSTR str, GUID *guid )
253 {
254     DWORD i, val = 0, base = 1, *p;
255
256     p = (DWORD*) guid;
257     for( i=0; i<20; i++ )
258     {
259         if( (i%5) == 0 )
260         {
261             val = 0;
262             base = 1;
263         }
264         val += table_dec85[str[i]] * base;
265         if( str[i] >= 0x80 )
266             return FALSE;
267         if( table_dec85[str[i]] == 0xff )
268             return FALSE;
269         if( (i%5) == 4 )
270             p[i/5] = val;
271         base *= 85;
272     }
273     return TRUE;
274 }
275
276 /*
277  *  Encodes a base85 guid given a GUID pointer
278  *  Caller should provide a 21 character buffer for the encoded string.
279  *
280  *  returns TRUE if successful, FALSE if not
281  */
282 BOOL encode_base85_guid( GUID *guid, LPWSTR str )
283 {
284     unsigned int x, *p, i;
285
286     p = (unsigned int*) guid;
287     for( i=0; i<4; i++ )
288     {
289         x = p[i];
290         *str++ = table_enc85[x%85];
291         x = x/85;
292         *str++ = table_enc85[x%85];
293         x = x/85;
294         *str++ = table_enc85[x%85];
295         x = x/85;
296         *str++ = table_enc85[x%85];
297         x = x/85;
298         *str++ = table_enc85[x%85];
299     }
300     *str = 0;
301
302     return TRUE;
303 }
304
305
306 UINT MSIREG_OpenUninstallKey(LPCWSTR szProduct, HKEY* key, BOOL create)
307 {
308     UINT rc;
309     WCHAR keypath[0x200];
310     TRACE("%s\n",debugstr_w(szProduct));
311
312     sprintfW(keypath,szUninstall_fmt,szProduct);
313
314     if (create)
315         rc = RegCreateKeyW(HKEY_LOCAL_MACHINE, keypath, key);
316     else
317         rc = RegOpenKeyW(HKEY_LOCAL_MACHINE, keypath, key);
318
319     return rc;
320 }
321
322 UINT MSIREG_OpenUserProductsKey(LPCWSTR szProduct, HKEY* key, BOOL create)
323 {
324     UINT rc;
325     WCHAR squished_pc[GUID_SIZE];
326     WCHAR keypath[0x200];
327
328     TRACE("%s\n",debugstr_w(szProduct));
329     squash_guid(szProduct,squished_pc);
330     TRACE("squished (%s)\n", debugstr_w(squished_pc));
331
332     sprintfW(keypath,szUserProduct_fmt,squished_pc);
333
334     if (create)
335         rc = RegCreateKeyW(HKEY_CURRENT_USER,keypath,key);
336     else
337         rc = RegOpenKeyW(HKEY_CURRENT_USER,keypath,key);
338
339     return rc;
340 }
341
342 UINT MSIREG_OpenUserFeaturesKey(LPCWSTR szProduct, HKEY* key, BOOL create)
343 {
344     UINT rc;
345     WCHAR squished_pc[GUID_SIZE];
346     WCHAR keypath[0x200];
347
348     TRACE("%s\n",debugstr_w(szProduct));
349     squash_guid(szProduct,squished_pc);
350     TRACE("squished (%s)\n", debugstr_w(squished_pc));
351
352     sprintfW(keypath,szUserFeatures_fmt,squished_pc);
353
354     if (create)
355         rc = RegCreateKeyW(HKEY_CURRENT_USER,keypath,key);
356     else
357         rc = RegOpenKeyW(HKEY_CURRENT_USER,keypath,key);
358
359     return rc;
360 }
361
362 UINT MSIREG_OpenFeatures(HKEY* key)
363 {
364     return RegCreateKeyW(HKEY_LOCAL_MACHINE,szInstaller_Features,key);
365 }
366
367 UINT MSIREG_OpenFeaturesKey(LPCWSTR szProduct, HKEY* key, BOOL create)
368 {
369     UINT rc;
370     WCHAR squished_pc[GUID_SIZE];
371     WCHAR keypath[0x200];
372
373     TRACE("%s\n",debugstr_w(szProduct));
374     squash_guid(szProduct,squished_pc);
375     TRACE("squished (%s)\n", debugstr_w(squished_pc));
376
377     sprintfW(keypath,szInstaller_Features_fmt,squished_pc);
378
379     if (create)
380         rc = RegCreateKeyW(HKEY_LOCAL_MACHINE,keypath,key);
381     else
382         rc = RegOpenKeyW(HKEY_LOCAL_MACHINE,keypath,key);
383
384     return rc;
385 }
386
387 UINT MSIREG_OpenComponents(HKEY* key)
388 {
389     return RegCreateKeyW(HKEY_LOCAL_MACHINE,szInstaller_Components,key);
390 }
391
392 UINT MSIREG_OpenComponentsKey(LPCWSTR szComponent, HKEY* key, BOOL create)
393 {
394     UINT rc;
395     WCHAR squished_cc[GUID_SIZE];
396     WCHAR keypath[0x200];
397
398     TRACE("%s\n",debugstr_w(szComponent));
399     squash_guid(szComponent,squished_cc);
400     TRACE("squished (%s)\n", debugstr_w(squished_cc));
401
402     sprintfW(keypath,szInstaller_Components_fmt,squished_cc);
403
404     if (create)
405         rc = RegCreateKeyW(HKEY_LOCAL_MACHINE,keypath,key);
406     else
407         rc = RegOpenKeyW(HKEY_LOCAL_MACHINE,keypath,key);
408
409     return rc;
410 }
411
412 UINT MSIREG_OpenUserComponentsKey(LPCWSTR szComponent, HKEY* key, BOOL create)
413 {
414     UINT rc;
415     WCHAR squished_cc[GUID_SIZE];
416     WCHAR keypath[0x200];
417
418     TRACE("%s\n",debugstr_w(szComponent));
419     squash_guid(szComponent,squished_cc);
420     TRACE("squished (%s)\n", debugstr_w(squished_cc));
421
422     sprintfW(keypath,szUser_Components_fmt,squished_cc);
423
424     if (create)
425         rc = RegCreateKeyW(HKEY_CURRENT_USER,keypath,key);
426     else
427         rc = RegOpenKeyW(HKEY_CURRENT_USER,keypath,key);
428
429     return rc;
430 }
431
432 UINT MSIREG_OpenProductsKey(LPCWSTR szProduct, HKEY* key, BOOL create)
433 {
434     UINT rc;
435     WCHAR squished_pc[GUID_SIZE];
436     WCHAR keypath[0x200];
437
438     TRACE("%s\n",debugstr_w(szProduct));
439     squash_guid(szProduct,squished_pc);
440     TRACE("squished (%s)\n", debugstr_w(squished_pc));
441
442     sprintfW(keypath,szInstaller_Products_fmt,squished_pc);
443
444     if (create)
445         rc = RegCreateKeyW(HKEY_LOCAL_MACHINE,keypath,key);
446     else
447         rc = RegOpenKeyW(HKEY_LOCAL_MACHINE,keypath,key);
448
449     return rc;
450 }
451
452 UINT MSIREG_OpenUpgradeCodesKey(LPCWSTR szUpgradeCode, HKEY* key, BOOL create)
453 {
454     UINT rc;
455     WCHAR squished_pc[GUID_SIZE];
456     WCHAR keypath[0x200];
457
458     TRACE("%s\n",debugstr_w(szUpgradeCode));
459     squash_guid(szUpgradeCode,squished_pc);
460     TRACE("squished (%s)\n", debugstr_w(squished_pc));
461
462     sprintfW(keypath,szInstaller_UpgradeCodes_fmt,squished_pc);
463
464     if (create)
465         rc = RegCreateKeyW(HKEY_LOCAL_MACHINE,keypath,key);
466     else
467         rc = RegOpenKeyW(HKEY_LOCAL_MACHINE,keypath,key);
468
469     return rc;
470 }
471
472 UINT MSIREG_OpenUserUpgradeCodesKey(LPCWSTR szUpgradeCode, HKEY* key, BOOL create)
473 {
474     UINT rc;
475     WCHAR squished_pc[GUID_SIZE];
476     WCHAR keypath[0x200];
477
478     TRACE("%s\n",debugstr_w(szUpgradeCode));
479     squash_guid(szUpgradeCode,squished_pc);
480     TRACE("squished (%s)\n", debugstr_w(squished_pc));
481
482     sprintfW(keypath,szInstaller_UserUpgradeCodes_fmt,squished_pc);
483
484     if (create)
485         rc = RegCreateKeyW(HKEY_CURRENT_USER,keypath,key);
486     else
487         rc = RegOpenKeyW(HKEY_CURRENT_USER,keypath,key);
488
489     return rc;
490 }
491
492
493 /*************************************************************************
494  *  MsiDecomposeDescriptorW   [MSI.@]
495  *
496  * Decomposes an MSI descriptor into product, feature and component parts.
497  * An MSI descriptor is a string of the form:
498  *   [base 85 guid] [feature code] '>' [base 85 guid]
499  *
500  * PARAMS
501  *   szDescriptor  [I]  the descriptor to decompose
502  *   szProduct     [O]  buffer of MAX_FEATURE_CHARS for the product guid
503  *   szFeature     [O]  buffer of MAX_FEATURE_CHARS for the feature code
504  *   szComponent   [O]  buffer of MAX_FEATURE_CHARS for the component guid
505  *   pUsed         [O]  the length of the descriptor
506  *
507  * RETURNS
508  *   ERROR_SUCCESS             if everything worked correctly
509  *   ERROR_INVALID_PARAMETER   if the descriptor was invalid
510  *
511  */
512 UINT WINAPI MsiDecomposeDescriptorW( LPCWSTR szDescriptor, LPWSTR szProduct,
513                 LPWSTR szFeature, LPWSTR szComponent, DWORD *pUsed )
514 {
515     UINT r, len;
516     LPWSTR p;
517     GUID product, component;
518
519     TRACE("%s %p %p %p %p\n", debugstr_w(szDescriptor), szProduct,
520           szFeature, szComponent, pUsed);
521
522     r = decode_base85_guid( szDescriptor, &product );
523     if( !r )
524         return ERROR_INVALID_PARAMETER;
525
526     TRACE("product %s\n", debugstr_guid( &product ));
527
528     p = strchrW(&szDescriptor[20],'>');
529     if( !p )
530         return ERROR_INVALID_PARAMETER;
531
532     len = (p - &szDescriptor[20]);
533     if( len > MAX_FEATURE_CHARS )
534         return ERROR_INVALID_PARAMETER;
535     memcpy( szFeature, &szDescriptor[20], len*sizeof(WCHAR) );
536     szFeature[len] = 0;
537
538     TRACE("feature %s\n", debugstr_w( szFeature ));
539
540     r = decode_base85_guid( p+1, &component );
541     if( !r )
542         return ERROR_INVALID_PARAMETER;
543
544     TRACE("component %s\n", debugstr_guid( &component ));
545
546     StringFromGUID2( &product, szProduct, MAX_FEATURE_CHARS+1 );
547     StringFromGUID2( &component, szComponent, MAX_FEATURE_CHARS+1 );
548     len = ( &p[21] - szDescriptor );
549
550     TRACE("length = %d\n", len);
551     *pUsed = len;
552
553     return ERROR_SUCCESS;
554 }
555
556 UINT WINAPI MsiDecomposeDescriptorA( LPCSTR szDescriptor, LPSTR szProduct,
557                 LPSTR szFeature, LPSTR szComponent, DWORD *pUsed )
558 {
559     WCHAR product[MAX_FEATURE_CHARS+1];
560     WCHAR feature[MAX_FEATURE_CHARS+1];
561     WCHAR component[MAX_FEATURE_CHARS+1];
562     LPWSTR str = NULL;
563     UINT r;
564
565     TRACE("%s %p %p %p %p\n", debugstr_a(szDescriptor), szProduct,
566           szFeature, szComponent, pUsed);
567
568     if( szDescriptor )
569     {
570         str = strdupAtoW( szDescriptor );
571         if( !str )
572             return ERROR_OUTOFMEMORY;
573     }
574
575     r = MsiDecomposeDescriptorW( str, product, feature, component, pUsed );
576
577     WideCharToMultiByte( CP_ACP, 0, product, MAX_FEATURE_CHARS+1,
578                          szProduct, MAX_FEATURE_CHARS+1, NULL, NULL );
579     WideCharToMultiByte( CP_ACP, 0, feature, MAX_FEATURE_CHARS+1,
580                          szFeature, MAX_FEATURE_CHARS+1, NULL, NULL );
581     WideCharToMultiByte( CP_ACP, 0, component, MAX_FEATURE_CHARS+1,
582                          szComponent, MAX_FEATURE_CHARS+1, NULL, NULL );
583
584     msi_free( str );
585
586     return r;
587 }
588
589 UINT WINAPI MsiEnumProductsA(DWORD index, LPSTR lpguid)
590 {
591     DWORD r;
592     WCHAR szwGuid[GUID_SIZE];
593
594     TRACE("%ld %p\n",index,lpguid);
595     
596     if (NULL == lpguid)
597         return ERROR_INVALID_PARAMETER;
598     r = MsiEnumProductsW(index, szwGuid);
599     if( r == ERROR_SUCCESS )
600         WideCharToMultiByte(CP_ACP, 0, szwGuid, -1, lpguid, GUID_SIZE, NULL, NULL);
601
602     return r;
603 }
604
605 UINT WINAPI MsiEnumProductsW(DWORD index, LPWSTR lpguid)
606 {
607     HKEY hkeyFeatures = 0;
608     DWORD r;
609     WCHAR szKeyName[SQUISH_GUID_SIZE];
610
611     TRACE("%ld %p\n",index,lpguid);
612
613     if (NULL == lpguid)
614         return ERROR_INVALID_PARAMETER;
615
616     r = MSIREG_OpenFeatures(&hkeyFeatures);
617     if( r != ERROR_SUCCESS )
618         return ERROR_NO_MORE_ITEMS;
619
620     r = RegEnumKeyW(hkeyFeatures, index, szKeyName, SQUISH_GUID_SIZE);
621     if( r == ERROR_SUCCESS )
622         unsquash_guid(szKeyName, lpguid);
623     RegCloseKey(hkeyFeatures);
624
625     return r;
626 }
627
628 UINT WINAPI MsiEnumFeaturesA(LPCSTR szProduct, DWORD index, 
629       LPSTR szFeature, LPSTR szParent)
630 {
631     DWORD r;
632     WCHAR szwFeature[GUID_SIZE], szwParent[GUID_SIZE];
633     LPWSTR szwProduct = NULL;
634
635     TRACE("%s %ld %p %p\n",debugstr_a(szProduct),index,szFeature,szParent);
636
637     if( szProduct )
638     {
639         szwProduct = strdupAtoW( szProduct );
640         if( !szwProduct )
641             return ERROR_OUTOFMEMORY;
642     }
643
644     r = MsiEnumFeaturesW(szwProduct, index, szwFeature, szwParent);
645     if( r == ERROR_SUCCESS )
646     {
647         WideCharToMultiByte(CP_ACP, 0, szwFeature, -1,
648                             szFeature, GUID_SIZE, NULL, NULL);
649         WideCharToMultiByte(CP_ACP, 0, szwParent, -1,
650                             szParent, GUID_SIZE, NULL, NULL);
651     }
652
653     msi_free( szwProduct);
654
655     return r;
656 }
657
658 UINT WINAPI MsiEnumFeaturesW(LPCWSTR szProduct, DWORD index, 
659       LPWSTR szFeature, LPWSTR szParent)
660 {
661     HKEY hkeyProduct = 0;
662     DWORD r, sz;
663
664     TRACE("%s %ld %p %p\n",debugstr_w(szProduct),index,szFeature,szParent);
665
666     r = MSIREG_OpenFeaturesKey(szProduct,&hkeyProduct,FALSE);
667     if( r != ERROR_SUCCESS )
668         return ERROR_NO_MORE_ITEMS;
669
670     sz = GUID_SIZE;
671     r = RegEnumValueW(hkeyProduct, index, szFeature, &sz, NULL, NULL, NULL, NULL);
672     RegCloseKey(hkeyProduct);
673
674     return r;
675 }
676
677 UINT WINAPI MsiEnumComponentsA(DWORD index, LPSTR lpguid)
678 {
679     DWORD r;
680     WCHAR szwGuid[GUID_SIZE];
681
682     TRACE("%ld %p\n",index,lpguid);
683
684     r = MsiEnumComponentsW(index, szwGuid);
685     if( r == ERROR_SUCCESS )
686         WideCharToMultiByte(CP_ACP, 0, szwGuid, -1, lpguid, GUID_SIZE, NULL, NULL);
687
688     return r;
689 }
690
691 UINT WINAPI MsiEnumComponentsW(DWORD index, LPWSTR lpguid)
692 {
693     HKEY hkeyComponents = 0;
694     DWORD r;
695     WCHAR szKeyName[SQUISH_GUID_SIZE];
696
697     TRACE("%ld %p\n",index,lpguid);
698
699     r = MSIREG_OpenComponents(&hkeyComponents);
700     if( r != ERROR_SUCCESS )
701         return ERROR_NO_MORE_ITEMS;
702
703     r = RegEnumKeyW(hkeyComponents, index, szKeyName, SQUISH_GUID_SIZE);
704     if( r == ERROR_SUCCESS )
705         unsquash_guid(szKeyName, lpguid);
706     RegCloseKey(hkeyComponents);
707
708     return r;
709 }
710
711 UINT WINAPI MsiEnumClientsA(LPCSTR szComponent, DWORD index, LPSTR szProduct)
712 {
713     DWORD r;
714     WCHAR szwProduct[GUID_SIZE];
715     LPWSTR szwComponent = NULL;
716
717     TRACE("%s %ld %p\n",debugstr_a(szComponent),index,szProduct);
718
719     if( szComponent )
720     {
721         szwComponent = strdupAtoW( szComponent );
722         if( !szwComponent )
723             return ERROR_OUTOFMEMORY;
724     }
725
726     r = MsiEnumClientsW(szComponent?szwComponent:NULL, index, szwProduct);
727     if( r == ERROR_SUCCESS )
728     {
729         WideCharToMultiByte(CP_ACP, 0, szwProduct, -1,
730                             szProduct, GUID_SIZE, NULL, NULL);
731     }
732
733     msi_free( szwComponent);
734
735     return r;
736 }
737
738 UINT WINAPI MsiEnumClientsW(LPCWSTR szComponent, DWORD index, LPWSTR szProduct)
739 {
740     HKEY hkeyComp = 0;
741     DWORD r, sz;
742     WCHAR szValName[SQUISH_GUID_SIZE];
743
744     TRACE("%s %ld %p\n",debugstr_w(szComponent),index,szProduct);
745
746     r = MSIREG_OpenComponentsKey(szComponent,&hkeyComp,FALSE);
747     if( r != ERROR_SUCCESS )
748         return ERROR_NO_MORE_ITEMS;
749
750     sz = SQUISH_GUID_SIZE;
751     r = RegEnumValueW(hkeyComp, index, szValName, &sz, NULL, NULL, NULL, NULL);
752     if( r == ERROR_SUCCESS )
753         unsquash_guid(szValName, szProduct);
754
755     RegCloseKey(hkeyComp);
756
757     return r;
758 }
759
760 /*************************************************************************
761  *  MsiEnumComponentQualifiersA [MSI.@]
762  *
763  */
764 UINT WINAPI MsiEnumComponentQualifiersA( LPSTR szComponent, DWORD iIndex,
765                 LPSTR lpQualifierBuf, DWORD* pcchQualifierBuf,
766                 LPSTR lpApplicationDataBuf, DWORD* pcchApplicationDataBuf)
767 {
768     LPWSTR szwComponent;
769     LPWSTR lpwQualifierBuf;
770     DWORD pcchwQualifierBuf;
771     LPWSTR lpwApplicationDataBuf;
772     DWORD pcchwApplicationDataBuf;
773     DWORD rc;
774     DWORD length;
775
776     TRACE("%s %08lx %p %p %p %p\n", debugstr_a(szComponent), iIndex,
777           lpQualifierBuf, pcchQualifierBuf, lpApplicationDataBuf,
778           pcchApplicationDataBuf);
779
780     szwComponent = strdupAtoW(szComponent);
781
782     if (lpQualifierBuf)
783         lpwQualifierBuf = msi_alloc( (*pcchQualifierBuf) * sizeof(WCHAR));
784     else
785         lpwQualifierBuf = NULL;
786
787     if (pcchQualifierBuf)
788         pcchwQualifierBuf = *pcchQualifierBuf;
789     else
790         pcchwQualifierBuf = 0;
791
792     if (lpApplicationDataBuf)
793        lpwApplicationDataBuf = msi_alloc( (*pcchApplicationDataBuf) * sizeof(WCHAR));
794     else
795         lpwApplicationDataBuf = NULL;
796
797     if (pcchApplicationDataBuf)
798         pcchwApplicationDataBuf = *pcchApplicationDataBuf;
799     else
800         pcchwApplicationDataBuf = 0;
801
802     rc = MsiEnumComponentQualifiersW( szwComponent, iIndex, lpwQualifierBuf, 
803                     &pcchwQualifierBuf, lpwApplicationDataBuf,
804                     &pcchwApplicationDataBuf);
805
806     /*
807      * A bit of wizardry to report back the length without the null.
808      * just in case the buffer is too small and is filled.
809      */
810     if (lpQualifierBuf)
811     {
812         length = WideCharToMultiByte(CP_ACP, 0, lpwQualifierBuf, -1,
813                         lpQualifierBuf, *pcchQualifierBuf, NULL, NULL); 
814
815         if (*pcchQualifierBuf == length && lpQualifierBuf[length-1])
816             *pcchQualifierBuf = length;
817         else
818             *pcchQualifierBuf = length - 1;
819     }
820     if (lpApplicationDataBuf)
821     {
822         length = WideCharToMultiByte(CP_ACP, 0,
823                         lpwApplicationDataBuf, -1, lpApplicationDataBuf,
824                         *pcchApplicationDataBuf, NULL, NULL); 
825
826         if (*pcchApplicationDataBuf == length && lpApplicationDataBuf[length-1])
827             *pcchApplicationDataBuf = length;
828         else
829             *pcchApplicationDataBuf = length - 1;
830     }
831
832     msi_free(lpwApplicationDataBuf);
833     msi_free(lpwQualifierBuf);
834     msi_free(szwComponent);
835
836     return rc;
837 }
838
839 /*************************************************************************
840  *  MsiEnumComponentQualifiersW [MSI.@]
841  *
842  */
843 UINT WINAPI MsiEnumComponentQualifiersW( LPWSTR szComponent, DWORD iIndex,
844                 LPWSTR lpQualifierBuf, DWORD* pcchQualifierBuf,
845                 LPWSTR lpApplicationDataBuf, DWORD* pcchApplicationDataBuf )
846 {
847     UINT rc;
848     HKEY key;
849     DWORD actual_pcchQualifierBuf = 0;
850     DWORD actual_pcchApplicationDataBuf = 0;
851     LPWSTR full_buffer = NULL;
852     DWORD full_buffer_size = 0;
853     LPWSTR ptr;
854
855     TRACE("%s %08lx %p %p %p %p\n", debugstr_w(szComponent), iIndex,
856           lpQualifierBuf, pcchQualifierBuf, lpApplicationDataBuf,
857           pcchApplicationDataBuf);
858
859     if (pcchQualifierBuf)
860         actual_pcchQualifierBuf = *pcchQualifierBuf * sizeof(WCHAR);
861     if (pcchApplicationDataBuf)
862         actual_pcchApplicationDataBuf = *pcchApplicationDataBuf * sizeof(WCHAR);
863     
864     rc = MSIREG_OpenUserComponentsKey(szComponent, &key, FALSE);
865     if (rc != ERROR_SUCCESS)
866         return ERROR_UNKNOWN_COMPONENT;
867
868     full_buffer_size = (52 * sizeof(WCHAR)) + actual_pcchApplicationDataBuf;
869     full_buffer = msi_alloc(full_buffer_size);
870     
871     rc = RegEnumValueW(key, iIndex, lpQualifierBuf, pcchQualifierBuf, NULL, 
872                     NULL, (LPBYTE)full_buffer, &full_buffer_size);
873
874     if (rc == ERROR_MORE_DATA)
875     {
876         msi_free(full_buffer);
877         full_buffer_size+=sizeof(WCHAR);
878         full_buffer = msi_alloc(full_buffer_size);
879         rc = RegEnumValueW(key, iIndex, lpQualifierBuf, pcchQualifierBuf, NULL, 
880                     NULL, (LPBYTE)full_buffer, &full_buffer_size);
881     }
882     
883     RegCloseKey(key);
884
885     if (rc == ERROR_SUCCESS)
886     {
887         if (lpApplicationDataBuf && pcchApplicationDataBuf)
888         {
889             ptr = full_buffer;
890             /* Skip the first guid */
891             ptr += 21;
892     
893             /* Skip the name and the component guid if it exists */
894             if (strchrW(ptr,'<'))
895                 ptr = strchrW(ptr,'<');
896             else 
897                 ptr = strchrW(ptr,'>') + 21;
898
899             lstrcpynW(lpApplicationDataBuf,ptr,*pcchApplicationDataBuf);
900             *pcchApplicationDataBuf = strlenW(ptr);
901         }
902         if (lpQualifierBuf && pcchQualifierBuf)
903             *pcchQualifierBuf /= sizeof(WCHAR); 
904         TRACE("Providing %s and %s\n", debugstr_w(lpQualifierBuf), 
905                         debugstr_w(lpApplicationDataBuf));
906     }
907
908     msi_free(full_buffer);
909
910     return rc;
911 }
912
913 /*************************************************************************
914  *  MsiEnumRelatedProductsW   [MSI.@]
915  *
916  */
917 UINT WINAPI MsiEnumRelatedProductsW(LPCWSTR szUpgradeCode, DWORD dwReserved,
918                                     DWORD iProductIndex, LPWSTR lpProductBuf)
919 {
920     UINT r;
921     HKEY hkey;
922     WCHAR szKeyName[SQUISH_GUID_SIZE];
923
924     TRACE("%s %lu %lu %p\n", debugstr_w(szUpgradeCode), dwReserved,
925           iProductIndex, lpProductBuf);
926
927     if (NULL == szUpgradeCode)
928         return ERROR_INVALID_PARAMETER;
929     if (NULL == lpProductBuf)
930         return ERROR_INVALID_PARAMETER;
931
932     r = MSIREG_OpenUpgradeCodesKey(szUpgradeCode, &hkey, FALSE);
933     if (r != ERROR_SUCCESS)
934         return ERROR_NO_MORE_ITEMS;
935
936     r = RegEnumKeyW(hkey, iProductIndex, szKeyName, SQUISH_GUID_SIZE);
937     if( r == ERROR_SUCCESS )
938         unsquash_guid(szKeyName, lpProductBuf);
939     RegCloseKey(hkey);
940
941     return r;
942 }
943
944 /*************************************************************************
945  *  MsiEnumRelatedProductsA   [MSI.@]
946  *
947  */
948 UINT WINAPI MsiEnumRelatedProductsA(LPCSTR szUpgradeCode, DWORD dwReserved,
949                                     DWORD iProductIndex, LPSTR lpProductBuf)
950 {
951     LPWSTR szwUpgradeCode = NULL;
952     WCHAR productW[GUID_SIZE];
953     UINT r;
954
955     TRACE("%s %lu %lu %p\n", debugstr_a(szUpgradeCode), dwReserved,
956           iProductIndex, lpProductBuf);
957
958     if (szUpgradeCode)
959     {
960         szwUpgradeCode = strdupAtoW( szUpgradeCode );
961         if( !szwUpgradeCode )
962             return ERROR_OUTOFMEMORY;
963     }
964
965     r = MsiEnumRelatedProductsW( szwUpgradeCode, dwReserved,
966                                  iProductIndex, productW );
967     if (r == ERROR_SUCCESS)
968     {
969         WideCharToMultiByte( CP_ACP, 0, productW, GUID_SIZE,
970                              lpProductBuf, GUID_SIZE, NULL, NULL );
971     }
972     msi_free( szwUpgradeCode);
973     return r;
974 }