uxtheme/tests: Destroy the windows after we've used them.
[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
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     if (!str)
257         return FALSE;
258
259     p = (DWORD*) guid;
260     for( i=0; i<20; i++ )
261     {
262         if( (i%5) == 0 )
263         {
264             val = 0;
265             base = 1;
266         }
267         val += table_dec85[str[i]] * base;
268         if( str[i] >= 0x80 )
269             return FALSE;
270         if( table_dec85[str[i]] == 0xff )
271             return FALSE;
272         if( (i%5) == 4 )
273             p[i/5] = val;
274         base *= 85;
275     }
276     return TRUE;
277 }
278
279 /*
280  *  Encodes a base85 guid given a GUID pointer
281  *  Caller should provide a 21 character buffer for the encoded string.
282  *
283  *  returns TRUE if successful, FALSE if not
284  */
285 BOOL encode_base85_guid( GUID *guid, LPWSTR str )
286 {
287     unsigned int x, *p, i;
288
289     p = (unsigned int*) guid;
290     for( i=0; i<4; i++ )
291     {
292         x = p[i];
293         *str++ = table_enc85[x%85];
294         x = x/85;
295         *str++ = table_enc85[x%85];
296         x = x/85;
297         *str++ = table_enc85[x%85];
298         x = x/85;
299         *str++ = table_enc85[x%85];
300         x = x/85;
301         *str++ = table_enc85[x%85];
302     }
303     *str = 0;
304
305     return TRUE;
306 }
307
308
309 UINT MSIREG_OpenUninstallKey(LPCWSTR szProduct, HKEY* key, BOOL create)
310 {
311     UINT rc;
312     WCHAR keypath[0x200];
313     TRACE("%s\n",debugstr_w(szProduct));
314
315     sprintfW(keypath,szUninstall_fmt,szProduct);
316
317     if (create)
318         rc = RegCreateKeyW(HKEY_LOCAL_MACHINE, keypath, key);
319     else
320         rc = RegOpenKeyW(HKEY_LOCAL_MACHINE, keypath, key);
321
322     return rc;
323 }
324
325 UINT MSIREG_OpenUserProductsKey(LPCWSTR szProduct, HKEY* key, BOOL create)
326 {
327     UINT rc;
328     WCHAR squished_pc[GUID_SIZE];
329     WCHAR keypath[0x200];
330
331     TRACE("%s\n",debugstr_w(szProduct));
332     squash_guid(szProduct,squished_pc);
333     TRACE("squished (%s)\n", debugstr_w(squished_pc));
334
335     sprintfW(keypath,szUserProduct_fmt,squished_pc);
336
337     if (create)
338         rc = RegCreateKeyW(HKEY_CURRENT_USER,keypath,key);
339     else
340         rc = RegOpenKeyW(HKEY_CURRENT_USER,keypath,key);
341
342     return rc;
343 }
344
345 UINT MSIREG_OpenUserFeaturesKey(LPCWSTR szProduct, HKEY* key, BOOL create)
346 {
347     UINT rc;
348     WCHAR squished_pc[GUID_SIZE];
349     WCHAR keypath[0x200];
350
351     TRACE("%s\n",debugstr_w(szProduct));
352     squash_guid(szProduct,squished_pc);
353     TRACE("squished (%s)\n", debugstr_w(squished_pc));
354
355     sprintfW(keypath,szUserFeatures_fmt,squished_pc);
356
357     if (create)
358         rc = RegCreateKeyW(HKEY_CURRENT_USER,keypath,key);
359     else
360         rc = RegOpenKeyW(HKEY_CURRENT_USER,keypath,key);
361
362     return rc;
363 }
364
365 UINT MSIREG_OpenFeatures(HKEY* key)
366 {
367     return RegCreateKeyW(HKEY_LOCAL_MACHINE,szInstaller_Features,key);
368 }
369
370 UINT MSIREG_OpenFeaturesKey(LPCWSTR szProduct, HKEY* key, BOOL create)
371 {
372     UINT rc;
373     WCHAR squished_pc[GUID_SIZE];
374     WCHAR keypath[0x200];
375
376     TRACE("%s\n",debugstr_w(szProduct));
377     squash_guid(szProduct,squished_pc);
378     TRACE("squished (%s)\n", debugstr_w(squished_pc));
379
380     sprintfW(keypath,szInstaller_Features_fmt,squished_pc);
381
382     if (create)
383         rc = RegCreateKeyW(HKEY_LOCAL_MACHINE,keypath,key);
384     else
385         rc = RegOpenKeyW(HKEY_LOCAL_MACHINE,keypath,key);
386
387     return rc;
388 }
389
390 UINT MSIREG_OpenComponents(HKEY* key)
391 {
392     return RegCreateKeyW(HKEY_LOCAL_MACHINE,szInstaller_Components,key);
393 }
394
395 UINT MSIREG_OpenComponentsKey(LPCWSTR szComponent, HKEY* key, BOOL create)
396 {
397     UINT rc;
398     WCHAR squished_cc[GUID_SIZE];
399     WCHAR keypath[0x200];
400
401     TRACE("%s\n",debugstr_w(szComponent));
402     squash_guid(szComponent,squished_cc);
403     TRACE("squished (%s)\n", debugstr_w(squished_cc));
404
405     sprintfW(keypath,szInstaller_Components_fmt,squished_cc);
406
407     if (create)
408         rc = RegCreateKeyW(HKEY_LOCAL_MACHINE,keypath,key);
409     else
410         rc = RegOpenKeyW(HKEY_LOCAL_MACHINE,keypath,key);
411
412     return rc;
413 }
414
415 UINT MSIREG_OpenUserComponentsKey(LPCWSTR szComponent, HKEY* key, BOOL create)
416 {
417     UINT rc;
418     WCHAR squished_cc[GUID_SIZE];
419     WCHAR keypath[0x200];
420
421     TRACE("%s\n",debugstr_w(szComponent));
422     squash_guid(szComponent,squished_cc);
423     TRACE("squished (%s)\n", debugstr_w(squished_cc));
424
425     sprintfW(keypath,szUser_Components_fmt,squished_cc);
426
427     if (create)
428         rc = RegCreateKeyW(HKEY_CURRENT_USER,keypath,key);
429     else
430         rc = RegOpenKeyW(HKEY_CURRENT_USER,keypath,key);
431
432     return rc;
433 }
434
435 UINT MSIREG_OpenProductsKey(LPCWSTR szProduct, HKEY* key, BOOL create)
436 {
437     UINT rc;
438     WCHAR squished_pc[GUID_SIZE];
439     WCHAR keypath[0x200];
440
441     TRACE("%s\n",debugstr_w(szProduct));
442     squash_guid(szProduct,squished_pc);
443     TRACE("squished (%s)\n", debugstr_w(squished_pc));
444
445     sprintfW(keypath,szInstaller_Products_fmt,squished_pc);
446
447     if (create)
448         rc = RegCreateKeyW(HKEY_LOCAL_MACHINE,keypath,key);
449     else
450         rc = RegOpenKeyW(HKEY_LOCAL_MACHINE,keypath,key);
451
452     return rc;
453 }
454
455 UINT MSIREG_OpenUpgradeCodesKey(LPCWSTR szUpgradeCode, HKEY* key, BOOL create)
456 {
457     UINT rc;
458     WCHAR squished_pc[GUID_SIZE];
459     WCHAR keypath[0x200];
460
461     TRACE("%s\n",debugstr_w(szUpgradeCode));
462     squash_guid(szUpgradeCode,squished_pc);
463     TRACE("squished (%s)\n", debugstr_w(squished_pc));
464
465     sprintfW(keypath,szInstaller_UpgradeCodes_fmt,squished_pc);
466
467     if (create)
468         rc = RegCreateKeyW(HKEY_LOCAL_MACHINE,keypath,key);
469     else
470         rc = RegOpenKeyW(HKEY_LOCAL_MACHINE,keypath,key);
471
472     return rc;
473 }
474
475 UINT MSIREG_OpenUserUpgradeCodesKey(LPCWSTR szUpgradeCode, HKEY* key, BOOL create)
476 {
477     UINT rc;
478     WCHAR squished_pc[GUID_SIZE];
479     WCHAR keypath[0x200];
480
481     TRACE("%s\n",debugstr_w(szUpgradeCode));
482     squash_guid(szUpgradeCode,squished_pc);
483     TRACE("squished (%s)\n", debugstr_w(squished_pc));
484
485     sprintfW(keypath,szInstaller_UserUpgradeCodes_fmt,squished_pc);
486
487     if (create)
488         rc = RegCreateKeyW(HKEY_CURRENT_USER,keypath,key);
489     else
490         rc = RegOpenKeyW(HKEY_CURRENT_USER,keypath,key);
491
492     return rc;
493 }
494
495
496 /*************************************************************************
497  *  MsiDecomposeDescriptorW   [MSI.@]
498  *
499  * Decomposes an MSI descriptor into product, feature and component parts.
500  * An MSI descriptor is a string of the form:
501  *   [base 85 guid] [feature code] '>' [base 85 guid]
502  *
503  * PARAMS
504  *   szDescriptor  [I]  the descriptor to decompose
505  *   szProduct     [O]  buffer of MAX_FEATURE_CHARS+1 for the product guid
506  *   szFeature     [O]  buffer of MAX_FEATURE_CHARS+1 for the feature code
507  *   szComponent   [O]  buffer of MAX_FEATURE_CHARS+1 for the component guid
508  *   pUsed         [O]  the length of the descriptor
509  *
510  * RETURNS
511  *   ERROR_SUCCESS             if everything worked correctly
512  *   ERROR_INVALID_PARAMETER   if the descriptor was invalid
513  *
514  */
515 UINT WINAPI MsiDecomposeDescriptorW( LPCWSTR szDescriptor, LPWSTR szProduct,
516                 LPWSTR szFeature, LPWSTR szComponent, DWORD *pUsed )
517 {
518     UINT r, len;
519     LPWSTR p;
520     GUID product, component;
521
522     TRACE("%s %p %p %p %p\n", debugstr_w(szDescriptor), szProduct,
523           szFeature, szComponent, pUsed);
524
525     r = decode_base85_guid( szDescriptor, &product );
526     if( !r )
527         return ERROR_INVALID_PARAMETER;
528
529     TRACE("product %s\n", debugstr_guid( &product ));
530
531     p = strchrW(&szDescriptor[20],'>');
532     if( !p )
533         return ERROR_INVALID_PARAMETER;
534
535     len = (p - &szDescriptor[20]);
536     if( len > MAX_FEATURE_CHARS )
537         return ERROR_INVALID_PARAMETER;
538     if (szFeature)
539     {
540         memcpy( szFeature, &szDescriptor[20], len*sizeof(WCHAR) );
541         szFeature[len] = 0;
542     }
543
544     TRACE("feature %s\n", debugstr_w( &szDescriptor[20] ));
545
546     r = decode_base85_guid( p+1, &component );
547     if( !r )
548         return ERROR_INVALID_PARAMETER;
549
550     TRACE("component %s\n", debugstr_guid( &component ));
551
552     if (szProduct)
553         StringFromGUID2( &product, szProduct, MAX_FEATURE_CHARS+1 );
554     if (szComponent)
555         StringFromGUID2( &component, szComponent, MAX_FEATURE_CHARS+1 );
556     len = ( &p[21] - szDescriptor );
557
558     TRACE("length = %d\n", len);
559     *pUsed = len;
560
561     return ERROR_SUCCESS;
562 }
563
564 UINT WINAPI MsiDecomposeDescriptorA( LPCSTR szDescriptor, LPSTR szProduct,
565                 LPSTR szFeature, LPSTR szComponent, DWORD *pUsed )
566 {
567     WCHAR product[MAX_FEATURE_CHARS+1];
568     WCHAR feature[MAX_FEATURE_CHARS+1];
569     WCHAR component[MAX_FEATURE_CHARS+1];
570     LPWSTR str = NULL, p = NULL, f = NULL, c = NULL;
571     UINT r;
572
573     TRACE("%s %p %p %p %p\n", debugstr_a(szDescriptor), szProduct,
574           szFeature, szComponent, pUsed);
575
576     str = strdupAtoW( szDescriptor );
577     if( szDescriptor && !str )
578         return ERROR_OUTOFMEMORY;
579
580     if (szProduct)
581         p = product;
582     if (szFeature)
583         f = feature;
584     if (szComponent)
585         c = component;
586
587     r = MsiDecomposeDescriptorW( str, p, f, c, pUsed );
588
589     WideCharToMultiByte( CP_ACP, 0, p, MAX_FEATURE_CHARS+1,
590                          szProduct, MAX_FEATURE_CHARS+1, NULL, NULL );
591     WideCharToMultiByte( CP_ACP, 0, f, MAX_FEATURE_CHARS+1,
592                          szFeature, MAX_FEATURE_CHARS+1, NULL, NULL );
593     WideCharToMultiByte( CP_ACP, 0, c, MAX_FEATURE_CHARS+1,
594                          szComponent, MAX_FEATURE_CHARS+1, NULL, NULL );
595
596     msi_free( str );
597
598     return r;
599 }
600
601 UINT WINAPI MsiEnumProductsA(DWORD index, LPSTR lpguid)
602 {
603     DWORD r;
604     WCHAR szwGuid[GUID_SIZE];
605
606     TRACE("%ld %p\n",index,lpguid);
607     
608     if (NULL == lpguid)
609         return ERROR_INVALID_PARAMETER;
610     r = MsiEnumProductsW(index, szwGuid);
611     if( r == ERROR_SUCCESS )
612         WideCharToMultiByte(CP_ACP, 0, szwGuid, -1, lpguid, GUID_SIZE, NULL, NULL);
613
614     return r;
615 }
616
617 UINT WINAPI MsiEnumProductsW(DWORD index, LPWSTR lpguid)
618 {
619     HKEY hkeyFeatures = 0;
620     DWORD r;
621     WCHAR szKeyName[SQUISH_GUID_SIZE];
622
623     TRACE("%ld %p\n",index,lpguid);
624
625     if (NULL == lpguid)
626         return ERROR_INVALID_PARAMETER;
627
628     r = MSIREG_OpenFeatures(&hkeyFeatures);
629     if( r != ERROR_SUCCESS )
630         return ERROR_NO_MORE_ITEMS;
631
632     r = RegEnumKeyW(hkeyFeatures, index, szKeyName, SQUISH_GUID_SIZE);
633     if( r == ERROR_SUCCESS )
634         unsquash_guid(szKeyName, lpguid);
635     RegCloseKey(hkeyFeatures);
636
637     return r;
638 }
639
640 UINT WINAPI MsiEnumFeaturesA(LPCSTR szProduct, DWORD index, 
641       LPSTR szFeature, LPSTR szParent)
642 {
643     DWORD r;
644     WCHAR szwFeature[GUID_SIZE], szwParent[GUID_SIZE];
645     LPWSTR szwProduct = NULL;
646
647     TRACE("%s %ld %p %p\n",debugstr_a(szProduct),index,szFeature,szParent);
648
649     if( szProduct )
650     {
651         szwProduct = strdupAtoW( szProduct );
652         if( !szwProduct )
653             return ERROR_OUTOFMEMORY;
654     }
655
656     r = MsiEnumFeaturesW(szwProduct, index, szwFeature, szwParent);
657     if( r == ERROR_SUCCESS )
658     {
659         WideCharToMultiByte(CP_ACP, 0, szwFeature, -1,
660                             szFeature, GUID_SIZE, NULL, NULL);
661         WideCharToMultiByte(CP_ACP, 0, szwParent, -1,
662                             szParent, GUID_SIZE, NULL, NULL);
663     }
664
665     msi_free( szwProduct);
666
667     return r;
668 }
669
670 UINT WINAPI MsiEnumFeaturesW(LPCWSTR szProduct, DWORD index, 
671       LPWSTR szFeature, LPWSTR szParent)
672 {
673     HKEY hkeyProduct = 0;
674     DWORD r, sz;
675
676     TRACE("%s %ld %p %p\n",debugstr_w(szProduct),index,szFeature,szParent);
677
678     if( !szProduct )
679         return ERROR_INVALID_PARAMETER;
680
681     r = MSIREG_OpenFeaturesKey(szProduct,&hkeyProduct,FALSE);
682     if( r != ERROR_SUCCESS )
683         return ERROR_NO_MORE_ITEMS;
684
685     sz = GUID_SIZE;
686     r = RegEnumValueW(hkeyProduct, index, szFeature, &sz, NULL, NULL, NULL, NULL);
687     RegCloseKey(hkeyProduct);
688
689     return r;
690 }
691
692 UINT WINAPI MsiEnumComponentsA(DWORD index, LPSTR lpguid)
693 {
694     DWORD r;
695     WCHAR szwGuid[GUID_SIZE];
696
697     TRACE("%ld %p\n",index,lpguid);
698
699     r = MsiEnumComponentsW(index, szwGuid);
700     if( r == ERROR_SUCCESS )
701         WideCharToMultiByte(CP_ACP, 0, szwGuid, -1, lpguid, GUID_SIZE, NULL, NULL);
702
703     return r;
704 }
705
706 UINT WINAPI MsiEnumComponentsW(DWORD index, LPWSTR lpguid)
707 {
708     HKEY hkeyComponents = 0;
709     DWORD r;
710     WCHAR szKeyName[SQUISH_GUID_SIZE];
711
712     TRACE("%ld %p\n",index,lpguid);
713
714     r = MSIREG_OpenComponents(&hkeyComponents);
715     if( r != ERROR_SUCCESS )
716         return ERROR_NO_MORE_ITEMS;
717
718     r = RegEnumKeyW(hkeyComponents, index, szKeyName, SQUISH_GUID_SIZE);
719     if( r == ERROR_SUCCESS )
720         unsquash_guid(szKeyName, lpguid);
721     RegCloseKey(hkeyComponents);
722
723     return r;
724 }
725
726 UINT WINAPI MsiEnumClientsA(LPCSTR szComponent, DWORD index, LPSTR szProduct)
727 {
728     DWORD r;
729     WCHAR szwProduct[GUID_SIZE];
730     LPWSTR szwComponent = NULL;
731
732     TRACE("%s %ld %p\n",debugstr_a(szComponent),index,szProduct);
733
734     if( szComponent )
735     {
736         szwComponent = strdupAtoW( szComponent );
737         if( !szwComponent )
738             return ERROR_OUTOFMEMORY;
739     }
740
741     r = MsiEnumClientsW(szComponent?szwComponent:NULL, index, szwProduct);
742     if( r == ERROR_SUCCESS )
743     {
744         WideCharToMultiByte(CP_ACP, 0, szwProduct, -1,
745                             szProduct, GUID_SIZE, NULL, NULL);
746     }
747
748     msi_free( szwComponent);
749
750     return r;
751 }
752
753 UINT WINAPI MsiEnumClientsW(LPCWSTR szComponent, DWORD index, LPWSTR szProduct)
754 {
755     HKEY hkeyComp = 0;
756     DWORD r, sz;
757     WCHAR szValName[SQUISH_GUID_SIZE];
758
759     TRACE("%s %ld %p\n",debugstr_w(szComponent),index,szProduct);
760
761     r = MSIREG_OpenComponentsKey(szComponent,&hkeyComp,FALSE);
762     if( r != ERROR_SUCCESS )
763         return ERROR_NO_MORE_ITEMS;
764
765     sz = SQUISH_GUID_SIZE;
766     r = RegEnumValueW(hkeyComp, index, szValName, &sz, NULL, NULL, NULL, NULL);
767     if( r == ERROR_SUCCESS )
768         unsquash_guid(szValName, szProduct);
769
770     RegCloseKey(hkeyComp);
771
772     return r;
773 }
774
775 UINT WINAPI MSI_EnumComponentQualifiers( LPCWSTR szComponent, DWORD iIndex,
776                 awstring *lpQualBuf, DWORD* pcchQual,
777                 awstring *lpAppBuf, DWORD* pcchAppBuf )
778 {
779     DWORD name_sz, val_sz, name_max, val_max, type, ofs;
780     LPWSTR name = NULL, val = NULL;
781     UINT r, r2;
782     HKEY key;
783
784     TRACE("%s %08lx %p %p %p %p\n", debugstr_w(szComponent), iIndex,
785           lpQualBuf, pcchQual, lpAppBuf, pcchAppBuf);
786
787     if (!szComponent)
788         return ERROR_INVALID_PARAMETER;
789
790     r = MSIREG_OpenUserComponentsKey( szComponent, &key, FALSE );
791     if (r != ERROR_SUCCESS)
792         return ERROR_UNKNOWN_COMPONENT;
793
794     /* figure out how big the name is we want to return */
795     name_max = 0x10;
796     r = ERROR_OUTOFMEMORY;
797     name = msi_alloc( name_max * sizeof(WCHAR) );
798     if (!name)
799         goto end;
800
801     val_max = 0x10;
802     r = ERROR_OUTOFMEMORY;
803     val = msi_alloc( val_max );
804     if (!val)
805         goto end;
806
807     /* loop until we allocate enough memory */
808     while (1)
809     {
810         name_sz = name_max;
811         val_sz = val_max;
812         r = RegEnumValueW( key, iIndex, name, &name_sz,
813                            NULL, &type, (LPBYTE)val, &val_sz );
814         if (r == ERROR_SUCCESS)
815             break;
816         if (r != ERROR_MORE_DATA)
817             goto end;
818  
819         if (type != REG_MULTI_SZ)
820         {
821             ERR("component data has wrong type (%ld)\n", type);
822             goto end;
823         }
824
825         r = ERROR_OUTOFMEMORY;
826         if ((name_sz+1) >= name_max)
827         {
828             name_max *= 2;
829             msi_free( name );
830             name = msi_alloc( name_max * sizeof (WCHAR) );
831             if (!name)
832                 goto end;
833             continue;
834         }
835         if (val_sz > val_max)
836         {
837             val_max = val_sz + sizeof (WCHAR);
838             val = msi_alloc( val_max * sizeof (WCHAR) );
839             if (!val)
840                 goto end;
841             continue;
842         }
843         ERR("should be enough data, but isn't %ld %ld\n", name_sz, val_sz );
844         goto end;
845     }
846
847     ofs = 0;
848     r = MsiDecomposeDescriptorW( val, NULL, NULL, NULL, &ofs );
849     if (r != ERROR_SUCCESS)
850         goto end;
851
852     TRACE("Providing %s and %s\n", debugstr_w(name), debugstr_w(val+ofs));
853
854     r = msi_strcpy_to_awstring( name, lpQualBuf, pcchQual );
855     r2 = msi_strcpy_to_awstring( val+ofs, lpAppBuf, pcchAppBuf );
856
857     if (r2 != ERROR_SUCCESS)
858         r = r2;
859
860 end:
861     msi_free(val);
862     msi_free(name);
863     RegCloseKey(key);
864
865     return r;
866 }
867
868 /*************************************************************************
869  *  MsiEnumComponentQualifiersA [MSI.@]
870  */
871 UINT WINAPI MsiEnumComponentQualifiersA( LPCSTR szComponent, DWORD iIndex,
872                 LPSTR lpQualifierBuf, DWORD* pcchQualifierBuf,
873                 LPSTR lpApplicationDataBuf, DWORD* pcchApplicationDataBuf )
874 {
875     awstring qual, appdata;
876     LPWSTR comp;
877     UINT r;
878
879     TRACE("%s %08lx %p %p %p %p\n", debugstr_a(szComponent), iIndex,
880           lpQualifierBuf, pcchQualifierBuf, lpApplicationDataBuf,
881           pcchApplicationDataBuf);
882
883     comp = strdupAtoW( szComponent );
884     if (szComponent && !comp)
885         return ERROR_OUTOFMEMORY;
886
887     qual.unicode = FALSE;
888     qual.str.a = lpQualifierBuf;
889
890     appdata.unicode = FALSE;
891     appdata.str.a = lpApplicationDataBuf;
892
893     r = MSI_EnumComponentQualifiers( comp, iIndex,
894               &qual, pcchQualifierBuf, &appdata, pcchApplicationDataBuf );
895     msi_free( comp );
896     return r;
897 }
898
899 /*************************************************************************
900  *  MsiEnumComponentQualifiersW [MSI.@]
901  */
902 UINT WINAPI MsiEnumComponentQualifiersW( LPCWSTR szComponent, DWORD iIndex,
903                 LPWSTR lpQualifierBuf, DWORD* pcchQualifierBuf,
904                 LPWSTR lpApplicationDataBuf, DWORD* pcchApplicationDataBuf )
905 {
906     awstring qual, appdata;
907
908     TRACE("%s %08lx %p %p %p %p\n", debugstr_w(szComponent), iIndex,
909           lpQualifierBuf, pcchQualifierBuf, lpApplicationDataBuf,
910           pcchApplicationDataBuf);
911
912     qual.unicode = TRUE;
913     qual.str.w = lpQualifierBuf;
914
915     appdata.unicode = TRUE;
916     appdata.str.w = lpApplicationDataBuf;
917
918     return MSI_EnumComponentQualifiers( szComponent, iIndex,
919                  &qual, pcchQualifierBuf, &appdata, pcchApplicationDataBuf );
920 }
921
922 /*************************************************************************
923  *  MsiEnumRelatedProductsW   [MSI.@]
924  *
925  */
926 UINT WINAPI MsiEnumRelatedProductsW(LPCWSTR szUpgradeCode, DWORD dwReserved,
927                                     DWORD iProductIndex, LPWSTR lpProductBuf)
928 {
929     UINT r;
930     HKEY hkey;
931     WCHAR szKeyName[SQUISH_GUID_SIZE];
932
933     TRACE("%s %lu %lu %p\n", debugstr_w(szUpgradeCode), dwReserved,
934           iProductIndex, lpProductBuf);
935
936     if (NULL == szUpgradeCode)
937         return ERROR_INVALID_PARAMETER;
938     if (NULL == lpProductBuf)
939         return ERROR_INVALID_PARAMETER;
940
941     r = MSIREG_OpenUpgradeCodesKey(szUpgradeCode, &hkey, FALSE);
942     if (r != ERROR_SUCCESS)
943         return ERROR_NO_MORE_ITEMS;
944
945     r = RegEnumKeyW(hkey, iProductIndex, szKeyName, SQUISH_GUID_SIZE);
946     if( r == ERROR_SUCCESS )
947         unsquash_guid(szKeyName, lpProductBuf);
948     RegCloseKey(hkey);
949
950     return r;
951 }
952
953 /*************************************************************************
954  *  MsiEnumRelatedProductsA   [MSI.@]
955  *
956  */
957 UINT WINAPI MsiEnumRelatedProductsA(LPCSTR szUpgradeCode, DWORD dwReserved,
958                                     DWORD iProductIndex, LPSTR lpProductBuf)
959 {
960     LPWSTR szwUpgradeCode = NULL;
961     WCHAR productW[GUID_SIZE];
962     UINT r;
963
964     TRACE("%s %lu %lu %p\n", debugstr_a(szUpgradeCode), dwReserved,
965           iProductIndex, lpProductBuf);
966
967     if (szUpgradeCode)
968     {
969         szwUpgradeCode = strdupAtoW( szUpgradeCode );
970         if( !szwUpgradeCode )
971             return ERROR_OUTOFMEMORY;
972     }
973
974     r = MsiEnumRelatedProductsW( szwUpgradeCode, dwReserved,
975                                  iProductIndex, productW );
976     if (r == ERROR_SUCCESS)
977     {
978         WideCharToMultiByte( CP_ACP, 0, productW, GUID_SIZE,
979                              lpProductBuf, GUID_SIZE, NULL, NULL );
980     }
981     msi_free( szwUpgradeCode);
982     return r;
983 }