msi: Make MsiEnumRelatedProducts enumerate registry value names, not subkeys.
[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_fmt[] = {
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','\\',
117 '%','s',0};
118
119 static const WCHAR szInstaller_UpgradeCodes_fmt[] = {
120 'S','o','f','t','w','a','r','e','\\',
121 'M','i','c','r','o','s','o','f','t','\\',
122 'W','i','n','d','o','w','s','\\',
123 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
124 'I','n','s','t','a','l','l','e','r','\\',
125 'U','p','g','r','a','d','e','C','o','d','e','s','\\',
126 '%','s',0};
127
128 static const WCHAR szInstaller_UserUpgradeCodes_fmt[] = {
129 'S','o','f','t','w','a','r','e','\\',
130 'M','i','c','r','o','s','o','f','t','\\',
131 'I','n','s','t','a','l','l','e','r','\\',
132 'U','p','g','r','a','d','e','C','o','d','e','s','\\',
133 '%','s',0};
134
135
136 #define SQUISH_GUID_SIZE 33
137
138 BOOL unsquash_guid(LPCWSTR in, LPWSTR out)
139 {
140     DWORD i,n=0;
141
142     out[n++]='{';
143     for(i=0; i<8; i++)
144         out[n++] = in[7-i];
145     out[n++]='-';
146     for(i=0; i<4; i++)
147         out[n++] = in[11-i];
148     out[n++]='-';
149     for(i=0; i<4; i++)
150         out[n++] = in[15-i];
151     out[n++]='-';
152     for(i=0; i<2; i++)
153     {
154         out[n++] = in[17+i*2];
155         out[n++] = in[16+i*2];
156     }
157     out[n++]='-';
158     for( ; i<8; i++)
159     {
160         out[n++] = in[17+i*2];
161         out[n++] = in[16+i*2];
162     }
163     out[n++]='}';
164     out[n]=0;
165     return TRUE;
166 }
167
168 BOOL squash_guid(LPCWSTR in, LPWSTR out)
169 {
170     DWORD i,n=1;
171     GUID guid;
172
173     if (FAILED(CLSIDFromString((LPOLESTR)in, &guid)))
174         return FALSE;
175
176     for(i=0; i<8; i++)
177         out[7-i] = in[n++];
178     n++;
179     for(i=0; i<4; i++)
180         out[11-i] = in[n++];
181     n++;
182     for(i=0; i<4; i++)
183         out[15-i] = in[n++];
184     n++;
185     for(i=0; i<2; i++)
186     {
187         out[17+i*2] = in[n++];
188         out[16+i*2] = in[n++];
189     }
190     n++;
191     for( ; i<8; i++)
192     {
193         out[17+i*2] = in[n++];
194         out[16+i*2] = in[n++];
195     }
196     out[32]=0;
197     return TRUE;
198 }
199
200
201 /* tables for encoding and decoding base85 */
202 static const unsigned char table_dec85[0x80] = {
203 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
204 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
205 0xff,0x00,0xff,0xff,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0xff,
206 0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14,0x15,0xff,0xff,0xff,0x16,0xff,0x17,
207 0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,
208 0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x31,0x32,0x33,0xff,0x34,0x35,0x36,
209 0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,0x40,0x41,0x42,0x43,0x44,0x45,0x46,
210 0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,0x50,0x51,0x52,0xff,0x53,0x54,0xff,
211 };
212
213 static const char table_enc85[] =
214 "!$%&'()*+,-.0123456789=?@ABCDEFGHIJKLMNO"
215 "PQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwx"
216 "yz{}~";
217
218 /*
219  *  Converts a base85 encoded guid into a GUID pointer
220  *  Base85 encoded GUIDs should be 20 characters long.
221  *
222  *  returns TRUE if successful, FALSE if not
223  */
224 BOOL decode_base85_guid( LPCWSTR str, GUID *guid )
225 {
226     DWORD i, val = 0, base = 1, *p;
227
228     if (!str)
229         return FALSE;
230
231     p = (DWORD*) guid;
232     for( i=0; i<20; i++ )
233     {
234         if( (i%5) == 0 )
235         {
236             val = 0;
237             base = 1;
238         }
239         val += table_dec85[str[i]] * base;
240         if( str[i] >= 0x80 )
241             return FALSE;
242         if( table_dec85[str[i]] == 0xff )
243             return FALSE;
244         if( (i%5) == 4 )
245             p[i/5] = val;
246         base *= 85;
247     }
248     return TRUE;
249 }
250
251 /*
252  *  Encodes a base85 guid given a GUID pointer
253  *  Caller should provide a 21 character buffer for the encoded string.
254  *
255  *  returns TRUE if successful, FALSE if not
256  */
257 BOOL encode_base85_guid( GUID *guid, LPWSTR str )
258 {
259     unsigned int x, *p, i;
260
261     p = (unsigned int*) guid;
262     for( i=0; i<4; i++ )
263     {
264         x = p[i];
265         *str++ = table_enc85[x%85];
266         x = x/85;
267         *str++ = table_enc85[x%85];
268         x = x/85;
269         *str++ = table_enc85[x%85];
270         x = x/85;
271         *str++ = table_enc85[x%85];
272         x = x/85;
273         *str++ = table_enc85[x%85];
274     }
275     *str = 0;
276
277     return TRUE;
278 }
279
280 DWORD msi_version_str_to_dword(LPCWSTR p)
281 {
282     DWORD major, minor = 0, build = 0, version = 0;
283
284     if (!p)
285         return version;
286
287     major = atoiW(p);
288
289     p = strchrW(p, '.');
290     if (p)
291     {
292         minor = atoiW(p+1);
293         p = strchrW(p+1, '.');
294         if (p)
295             build = atoiW(p+1);
296     }
297
298     return MAKELONG(build, MAKEWORD(minor, major));
299 }
300
301 LPWSTR msi_version_dword_to_str(DWORD version)
302 {
303     const WCHAR fmt[] = { '%','u','.','%','u','.','%','u',0 };
304     LPWSTR str = msi_alloc(20);
305     sprintfW(str, fmt,
306              (version&0xff000000)>>24,
307              (version&0x00ff0000)>>16,
308               version&0x0000ffff);
309     return str;
310 }
311
312 LONG msi_reg_set_val_str( HKEY hkey, LPCWSTR name, LPCWSTR value )
313 {
314     DWORD len = value ? (lstrlenW(value) + 1) * sizeof (WCHAR) : 0;
315     return RegSetValueExW( hkey, name, 0, REG_SZ, (const BYTE *)value, len );
316 }
317
318 LONG msi_reg_set_val_multi_str( HKEY hkey, LPCWSTR name, LPCWSTR value )
319 {
320     LPCWSTR p = value;
321     while (*p) p += lstrlenW(p) + 1;
322     return RegSetValueExW( hkey, name, 0, REG_MULTI_SZ,
323                            (const BYTE *)value, (p + 1 - value) * sizeof(WCHAR) );
324 }
325
326 LONG msi_reg_set_val_dword( HKEY hkey, LPCWSTR name, DWORD val )
327 {
328     return RegSetValueExW( hkey, name, 0, REG_DWORD, (LPBYTE)&val, sizeof (DWORD) );
329 }
330
331 LONG msi_reg_set_subkey_val( HKEY hkey, LPCWSTR path, LPCWSTR name, LPCWSTR val )
332 {
333     HKEY hsubkey = 0;
334     LONG r;
335
336     r = RegCreateKeyW( hkey, path, &hsubkey );
337     if (r != ERROR_SUCCESS)
338         return r;
339     r = msi_reg_set_val_str( hsubkey, name, val );
340     RegCloseKey( hsubkey );
341     return r;
342 }
343
344 LPWSTR msi_reg_get_val_str( HKEY hkey, LPCWSTR name )
345 {
346     DWORD len = 0;
347     LPWSTR val;
348     LONG r;
349
350     r = RegQueryValueExW(hkey, name, NULL, NULL, NULL, &len);
351     if (r != ERROR_SUCCESS)
352         return NULL;
353
354     len += sizeof (WCHAR);
355     val = msi_alloc( len );
356     if (!val)
357         return NULL;
358     val[0] = 0;
359     RegQueryValueExW(hkey, name, NULL, NULL, (LPBYTE) val, &len);
360     return val;
361 }
362
363 BOOL msi_reg_get_val_dword( HKEY hkey, LPCWSTR name, DWORD *val)
364 {
365     DWORD type, len = sizeof (DWORD);
366     LONG r = RegQueryValueExW(hkey, name, NULL, &type, (LPBYTE) val, &len);
367     return r == ERROR_SUCCESS && type == REG_DWORD;
368 }
369
370 UINT MSIREG_OpenUninstallKey(LPCWSTR szProduct, HKEY* key, BOOL create)
371 {
372     UINT rc;
373     WCHAR keypath[0x200];
374     TRACE("%s\n",debugstr_w(szProduct));
375
376     sprintfW(keypath,szUninstall_fmt,szProduct);
377
378     if (create)
379         rc = RegCreateKeyW(HKEY_LOCAL_MACHINE, keypath, key);
380     else
381         rc = RegOpenKeyW(HKEY_LOCAL_MACHINE, keypath, key);
382
383     return rc;
384 }
385
386 UINT MSIREG_OpenUserProductsKey(LPCWSTR szProduct, HKEY* key, BOOL create)
387 {
388     UINT rc;
389     WCHAR squished_pc[GUID_SIZE];
390     WCHAR keypath[0x200];
391
392     TRACE("%s\n",debugstr_w(szProduct));
393     squash_guid(szProduct,squished_pc);
394     TRACE("squished (%s)\n", debugstr_w(squished_pc));
395
396     sprintfW(keypath,szUserProduct_fmt,squished_pc);
397
398     if (create)
399         rc = RegCreateKeyW(HKEY_CURRENT_USER,keypath,key);
400     else
401         rc = RegOpenKeyW(HKEY_CURRENT_USER,keypath,key);
402
403     return rc;
404 }
405
406 UINT MSIREG_OpenUserFeaturesKey(LPCWSTR szProduct, HKEY* key, BOOL create)
407 {
408     UINT rc;
409     WCHAR squished_pc[GUID_SIZE];
410     WCHAR keypath[0x200];
411
412     TRACE("%s\n",debugstr_w(szProduct));
413     squash_guid(szProduct,squished_pc);
414     TRACE("squished (%s)\n", debugstr_w(squished_pc));
415
416     sprintfW(keypath,szUserFeatures_fmt,squished_pc);
417
418     if (create)
419         rc = RegCreateKeyW(HKEY_CURRENT_USER,keypath,key);
420     else
421         rc = RegOpenKeyW(HKEY_CURRENT_USER,keypath,key);
422
423     return rc;
424 }
425
426 UINT MSIREG_OpenFeatures(HKEY* key)
427 {
428     return RegCreateKeyW(HKEY_LOCAL_MACHINE,szInstaller_Features,key);
429 }
430
431 UINT MSIREG_OpenFeaturesKey(LPCWSTR szProduct, HKEY* key, BOOL create)
432 {
433     UINT rc;
434     WCHAR squished_pc[GUID_SIZE];
435     WCHAR keypath[0x200];
436
437     TRACE("%s\n",debugstr_w(szProduct));
438     squash_guid(szProduct,squished_pc);
439     TRACE("squished (%s)\n", debugstr_w(squished_pc));
440
441     sprintfW(keypath,szInstaller_Features_fmt,squished_pc);
442
443     if (create)
444         rc = RegCreateKeyW(HKEY_LOCAL_MACHINE,keypath,key);
445     else
446         rc = RegOpenKeyW(HKEY_LOCAL_MACHINE,keypath,key);
447
448     return rc;
449 }
450
451 UINT MSIREG_OpenComponents(HKEY* key)
452 {
453     return RegCreateKeyW(HKEY_LOCAL_MACHINE,szInstaller_Components,key);
454 }
455
456 UINT MSIREG_OpenComponentsKey(LPCWSTR szComponent, HKEY* key, BOOL create)
457 {
458     UINT rc;
459     WCHAR squished_cc[GUID_SIZE];
460     WCHAR keypath[0x200];
461
462     TRACE("%s\n",debugstr_w(szComponent));
463     squash_guid(szComponent,squished_cc);
464     TRACE("squished (%s)\n", debugstr_w(squished_cc));
465
466     sprintfW(keypath,szInstaller_Components_fmt,squished_cc);
467
468     if (create)
469         rc = RegCreateKeyW(HKEY_LOCAL_MACHINE,keypath,key);
470     else
471         rc = RegOpenKeyW(HKEY_LOCAL_MACHINE,keypath,key);
472
473     return rc;
474 }
475
476 UINT MSIREG_OpenUserComponentsKey(LPCWSTR szComponent, HKEY* key, BOOL create)
477 {
478     UINT rc;
479     WCHAR squished_cc[GUID_SIZE];
480     WCHAR keypath[0x200];
481
482     TRACE("%s\n",debugstr_w(szComponent));
483     squash_guid(szComponent,squished_cc);
484     TRACE("squished (%s)\n", debugstr_w(squished_cc));
485
486     sprintfW(keypath,szUser_Components_fmt,squished_cc);
487
488     if (create)
489         rc = RegCreateKeyW(HKEY_CURRENT_USER,keypath,key);
490     else
491         rc = RegOpenKeyW(HKEY_CURRENT_USER,keypath,key);
492
493     return rc;
494 }
495
496 UINT MSIREG_OpenProductsKey(LPCWSTR szProduct, HKEY* key, BOOL create)
497 {
498     UINT rc;
499     WCHAR squished_pc[GUID_SIZE];
500     WCHAR keypath[0x200];
501
502     TRACE("%s\n",debugstr_w(szProduct));
503     squash_guid(szProduct,squished_pc);
504     TRACE("squished (%s)\n", debugstr_w(squished_pc));
505
506     sprintfW(keypath,szInstaller_Products_fmt,squished_pc);
507
508     if (create)
509         rc = RegCreateKeyW(HKEY_LOCAL_MACHINE,keypath,key);
510     else
511         rc = RegOpenKeyW(HKEY_LOCAL_MACHINE,keypath,key);
512
513     return rc;
514 }
515
516 UINT MSIREG_OpenUpgradeCodesKey(LPCWSTR szUpgradeCode, HKEY* key, BOOL create)
517 {
518     UINT rc;
519     WCHAR squished_pc[GUID_SIZE];
520     WCHAR keypath[0x200];
521
522     TRACE("%s\n",debugstr_w(szUpgradeCode));
523     squash_guid(szUpgradeCode,squished_pc);
524     TRACE("squished (%s)\n", debugstr_w(squished_pc));
525
526     sprintfW(keypath,szInstaller_UpgradeCodes_fmt,squished_pc);
527
528     if (create)
529         rc = RegCreateKeyW(HKEY_LOCAL_MACHINE,keypath,key);
530     else
531         rc = RegOpenKeyW(HKEY_LOCAL_MACHINE,keypath,key);
532
533     return rc;
534 }
535
536 UINT MSIREG_OpenUserUpgradeCodesKey(LPCWSTR szUpgradeCode, HKEY* key, BOOL create)
537 {
538     UINT rc;
539     WCHAR squished_pc[GUID_SIZE];
540     WCHAR keypath[0x200];
541
542     TRACE("%s\n",debugstr_w(szUpgradeCode));
543     squash_guid(szUpgradeCode,squished_pc);
544     TRACE("squished (%s)\n", debugstr_w(squished_pc));
545
546     sprintfW(keypath,szInstaller_UserUpgradeCodes_fmt,squished_pc);
547
548     if (create)
549         rc = RegCreateKeyW(HKEY_CURRENT_USER,keypath,key);
550     else
551         rc = RegOpenKeyW(HKEY_CURRENT_USER,keypath,key);
552
553     return rc;
554 }
555
556
557 /*************************************************************************
558  *  MsiDecomposeDescriptorW   [MSI.@]
559  *
560  * Decomposes an MSI descriptor into product, feature and component parts.
561  * An MSI descriptor is a string of the form:
562  *   [base 85 guid] [feature code] '>' [base 85 guid]
563  *
564  * PARAMS
565  *   szDescriptor  [I]  the descriptor to decompose
566  *   szProduct     [O]  buffer of MAX_FEATURE_CHARS+1 for the product guid
567  *   szFeature     [O]  buffer of MAX_FEATURE_CHARS+1 for the feature code
568  *   szComponent   [O]  buffer of MAX_FEATURE_CHARS+1 for the component guid
569  *   pUsed         [O]  the length of the descriptor
570  *
571  * RETURNS
572  *   ERROR_SUCCESS             if everything worked correctly
573  *   ERROR_INVALID_PARAMETER   if the descriptor was invalid
574  *
575  */
576 UINT WINAPI MsiDecomposeDescriptorW( LPCWSTR szDescriptor, LPWSTR szProduct,
577                 LPWSTR szFeature, LPWSTR szComponent, DWORD *pUsed )
578 {
579     UINT r, len;
580     LPWSTR p;
581     GUID product, component;
582
583     TRACE("%s %p %p %p %p\n", debugstr_w(szDescriptor), szProduct,
584           szFeature, szComponent, pUsed);
585
586     r = decode_base85_guid( szDescriptor, &product );
587     if( !r )
588         return ERROR_INVALID_PARAMETER;
589
590     TRACE("product %s\n", debugstr_guid( &product ));
591
592     p = strchrW(&szDescriptor[20],'>');
593     if( !p )
594         return ERROR_INVALID_PARAMETER;
595
596     len = (p - &szDescriptor[20]);
597     if( len > MAX_FEATURE_CHARS )
598         return ERROR_INVALID_PARAMETER;
599
600     TRACE("feature %s\n", debugstr_wn( &szDescriptor[20], len ));
601
602     r = decode_base85_guid( p+1, &component );
603     if( !r )
604         return ERROR_INVALID_PARAMETER;
605
606     TRACE("component %s\n", debugstr_guid( &component ));
607
608     if (szProduct)
609         StringFromGUID2( &product, szProduct, MAX_FEATURE_CHARS+1 );
610     if (szComponent)
611         StringFromGUID2( &component, szComponent, MAX_FEATURE_CHARS+1 );
612     if (szFeature)
613     {
614         memcpy( szFeature, &szDescriptor[20], len*sizeof(WCHAR) );
615         szFeature[len] = 0;
616     }
617     len = ( &p[21] - szDescriptor );
618
619     TRACE("length = %d\n", len);
620     *pUsed = len;
621
622     return ERROR_SUCCESS;
623 }
624
625 UINT WINAPI MsiDecomposeDescriptorA( LPCSTR szDescriptor, LPSTR szProduct,
626                 LPSTR szFeature, LPSTR szComponent, DWORD *pUsed )
627 {
628     WCHAR product[MAX_FEATURE_CHARS+1];
629     WCHAR feature[MAX_FEATURE_CHARS+1];
630     WCHAR component[MAX_FEATURE_CHARS+1];
631     LPWSTR str = NULL, p = NULL, f = NULL, c = NULL;
632     UINT r;
633
634     TRACE("%s %p %p %p %p\n", debugstr_a(szDescriptor), szProduct,
635           szFeature, szComponent, pUsed);
636
637     str = strdupAtoW( szDescriptor );
638     if( szDescriptor && !str )
639         return ERROR_OUTOFMEMORY;
640
641     if (szProduct)
642         p = product;
643     if (szFeature)
644         f = feature;
645     if (szComponent)
646         c = component;
647
648     r = MsiDecomposeDescriptorW( str, p, f, c, pUsed );
649
650     if (r == ERROR_SUCCESS)
651     {
652         WideCharToMultiByte( CP_ACP, 0, p, -1,
653                              szProduct, MAX_FEATURE_CHARS+1, NULL, NULL );
654         WideCharToMultiByte( CP_ACP, 0, f, -1,
655                              szFeature, MAX_FEATURE_CHARS+1, NULL, NULL );
656         WideCharToMultiByte( CP_ACP, 0, c, -1,
657                              szComponent, MAX_FEATURE_CHARS+1, NULL, NULL );
658     }
659
660     msi_free( str );
661
662     return r;
663 }
664
665 UINT WINAPI MsiEnumProductsA(DWORD index, LPSTR lpguid)
666 {
667     DWORD r;
668     WCHAR szwGuid[GUID_SIZE];
669
670     TRACE("%d %p\n", index, lpguid);
671
672     if (NULL == lpguid)
673         return ERROR_INVALID_PARAMETER;
674     r = MsiEnumProductsW(index, szwGuid);
675     if( r == ERROR_SUCCESS )
676         WideCharToMultiByte(CP_ACP, 0, szwGuid, -1, lpguid, GUID_SIZE, NULL, NULL);
677
678     return r;
679 }
680
681 UINT WINAPI MsiEnumProductsW(DWORD index, LPWSTR lpguid)
682 {
683     HKEY hkeyFeatures = 0;
684     DWORD r;
685     WCHAR szKeyName[SQUISH_GUID_SIZE];
686
687     TRACE("%d %p\n", index, lpguid);
688
689     if (NULL == lpguid)
690         return ERROR_INVALID_PARAMETER;
691
692     r = MSIREG_OpenFeatures(&hkeyFeatures);
693     if( r != ERROR_SUCCESS )
694         return ERROR_NO_MORE_ITEMS;
695
696     r = RegEnumKeyW(hkeyFeatures, index, szKeyName, SQUISH_GUID_SIZE);
697     if( r == ERROR_SUCCESS )
698         unsquash_guid(szKeyName, lpguid);
699     RegCloseKey(hkeyFeatures);
700
701     return r;
702 }
703
704 UINT WINAPI MsiEnumFeaturesA(LPCSTR szProduct, DWORD index, 
705       LPSTR szFeature, LPSTR szParent)
706 {
707     DWORD r;
708     WCHAR szwFeature[GUID_SIZE], szwParent[GUID_SIZE];
709     LPWSTR szwProduct = NULL;
710
711     TRACE("%s %d %p %p\n", debugstr_a(szProduct), index, szFeature, szParent);
712
713     if( szProduct )
714     {
715         szwProduct = strdupAtoW( szProduct );
716         if( !szwProduct )
717             return ERROR_OUTOFMEMORY;
718     }
719
720     r = MsiEnumFeaturesW(szwProduct, index, szwFeature, szwParent);
721     if( r == ERROR_SUCCESS )
722     {
723         WideCharToMultiByte(CP_ACP, 0, szwFeature, -1,
724                             szFeature, GUID_SIZE, NULL, NULL);
725         WideCharToMultiByte(CP_ACP, 0, szwParent, -1,
726                             szParent, GUID_SIZE, NULL, NULL);
727     }
728
729     msi_free( szwProduct);
730
731     return r;
732 }
733
734 UINT WINAPI MsiEnumFeaturesW(LPCWSTR szProduct, DWORD index, 
735       LPWSTR szFeature, LPWSTR szParent)
736 {
737     HKEY hkeyProduct = 0;
738     DWORD r, sz;
739
740     TRACE("%s %d %p %p\n", debugstr_w(szProduct), index, szFeature, szParent);
741
742     if( !szProduct )
743         return ERROR_INVALID_PARAMETER;
744
745     r = MSIREG_OpenFeaturesKey(szProduct,&hkeyProduct,FALSE);
746     if( r != ERROR_SUCCESS )
747         return ERROR_NO_MORE_ITEMS;
748
749     sz = GUID_SIZE;
750     r = RegEnumValueW(hkeyProduct, index, szFeature, &sz, NULL, NULL, NULL, NULL);
751     RegCloseKey(hkeyProduct);
752
753     return r;
754 }
755
756 UINT WINAPI MsiEnumComponentsA(DWORD index, LPSTR lpguid)
757 {
758     DWORD r;
759     WCHAR szwGuid[GUID_SIZE];
760
761     TRACE("%d %p\n", index, lpguid);
762
763     r = MsiEnumComponentsW(index, szwGuid);
764     if( r == ERROR_SUCCESS )
765         WideCharToMultiByte(CP_ACP, 0, szwGuid, -1, lpguid, GUID_SIZE, NULL, NULL);
766
767     return r;
768 }
769
770 UINT WINAPI MsiEnumComponentsW(DWORD index, LPWSTR lpguid)
771 {
772     HKEY hkeyComponents = 0;
773     DWORD r;
774     WCHAR szKeyName[SQUISH_GUID_SIZE];
775
776     TRACE("%d %p\n", index, lpguid);
777
778     r = MSIREG_OpenComponents(&hkeyComponents);
779     if( r != ERROR_SUCCESS )
780         return ERROR_NO_MORE_ITEMS;
781
782     r = RegEnumKeyW(hkeyComponents, index, szKeyName, SQUISH_GUID_SIZE);
783     if( r == ERROR_SUCCESS )
784         unsquash_guid(szKeyName, lpguid);
785     RegCloseKey(hkeyComponents);
786
787     return r;
788 }
789
790 UINT WINAPI MsiEnumClientsA(LPCSTR szComponent, DWORD index, LPSTR szProduct)
791 {
792     DWORD r;
793     WCHAR szwProduct[GUID_SIZE];
794     LPWSTR szwComponent = NULL;
795
796     TRACE("%s %d %p\n", debugstr_a(szComponent), index, szProduct);
797
798     if( szComponent )
799     {
800         szwComponent = strdupAtoW( szComponent );
801         if( !szwComponent )
802             return ERROR_OUTOFMEMORY;
803     }
804
805     r = MsiEnumClientsW(szComponent?szwComponent:NULL, index, szwProduct);
806     if( r == ERROR_SUCCESS )
807     {
808         WideCharToMultiByte(CP_ACP, 0, szwProduct, -1,
809                             szProduct, GUID_SIZE, NULL, NULL);
810     }
811
812     msi_free( szwComponent);
813
814     return r;
815 }
816
817 UINT WINAPI MsiEnumClientsW(LPCWSTR szComponent, DWORD index, LPWSTR szProduct)
818 {
819     HKEY hkeyComp = 0;
820     DWORD r, sz;
821     WCHAR szValName[SQUISH_GUID_SIZE];
822
823     TRACE("%s %d %p\n", debugstr_w(szComponent), index, szProduct);
824
825     r = MSIREG_OpenComponentsKey(szComponent,&hkeyComp,FALSE);
826     if( r != ERROR_SUCCESS )
827         return ERROR_NO_MORE_ITEMS;
828
829     sz = SQUISH_GUID_SIZE;
830     r = RegEnumValueW(hkeyComp, index, szValName, &sz, NULL, NULL, NULL, NULL);
831     if( r == ERROR_SUCCESS )
832         unsquash_guid(szValName, szProduct);
833
834     RegCloseKey(hkeyComp);
835
836     return r;
837 }
838
839 static UINT WINAPI MSI_EnumComponentQualifiers( LPCWSTR szComponent, DWORD iIndex,
840                 awstring *lpQualBuf, DWORD* pcchQual,
841                 awstring *lpAppBuf, DWORD* pcchAppBuf )
842 {
843     DWORD name_sz, val_sz, name_max, val_max, type, ofs;
844     LPWSTR name = NULL, val = NULL;
845     UINT r, r2;
846     HKEY key;
847
848     TRACE("%s %08x %p %p %p %p\n", debugstr_w(szComponent), iIndex,
849           lpQualBuf, pcchQual, lpAppBuf, pcchAppBuf);
850
851     if (!szComponent)
852         return ERROR_INVALID_PARAMETER;
853
854     r = MSIREG_OpenUserComponentsKey( szComponent, &key, FALSE );
855     if (r != ERROR_SUCCESS)
856         return ERROR_UNKNOWN_COMPONENT;
857
858     /* figure out how big the name is we want to return */
859     name_max = 0x10;
860     r = ERROR_OUTOFMEMORY;
861     name = msi_alloc( name_max * sizeof(WCHAR) );
862     if (!name)
863         goto end;
864
865     val_max = 0x10;
866     r = ERROR_OUTOFMEMORY;
867     val = msi_alloc( val_max );
868     if (!val)
869         goto end;
870
871     /* loop until we allocate enough memory */
872     while (1)
873     {
874         name_sz = name_max;
875         val_sz = val_max;
876         r = RegEnumValueW( key, iIndex, name, &name_sz,
877                            NULL, &type, (LPBYTE)val, &val_sz );
878         if (r == ERROR_SUCCESS)
879             break;
880         if (r != ERROR_MORE_DATA)
881             goto end;
882  
883         if (type != REG_MULTI_SZ)
884         {
885             ERR("component data has wrong type (%d)\n", type);
886             goto end;
887         }
888
889         r = ERROR_OUTOFMEMORY;
890         if ((name_sz+1) >= name_max)
891         {
892             name_max *= 2;
893             msi_free( name );
894             name = msi_alloc( name_max * sizeof (WCHAR) );
895             if (!name)
896                 goto end;
897             continue;
898         }
899         if (val_sz > val_max)
900         {
901             val_max = val_sz + sizeof (WCHAR);
902             msi_free( val );
903             val = msi_alloc( val_max * sizeof (WCHAR) );
904             if (!val)
905                 goto end;
906             continue;
907         }
908         ERR("should be enough data, but isn't %d %d\n", name_sz, val_sz );
909         goto end;
910     }
911
912     ofs = 0;
913     r = MsiDecomposeDescriptorW( val, NULL, NULL, NULL, &ofs );
914     if (r != ERROR_SUCCESS)
915         goto end;
916
917     TRACE("Providing %s and %s\n", debugstr_w(name), debugstr_w(val+ofs));
918
919     r = msi_strcpy_to_awstring( name, lpQualBuf, pcchQual );
920     r2 = msi_strcpy_to_awstring( val+ofs, lpAppBuf, pcchAppBuf );
921
922     if (r2 != ERROR_SUCCESS)
923         r = r2;
924
925 end:
926     msi_free(val);
927     msi_free(name);
928     RegCloseKey(key);
929
930     return r;
931 }
932
933 /*************************************************************************
934  *  MsiEnumComponentQualifiersA [MSI.@]
935  */
936 UINT WINAPI MsiEnumComponentQualifiersA( LPCSTR szComponent, DWORD iIndex,
937                 LPSTR lpQualifierBuf, DWORD* pcchQualifierBuf,
938                 LPSTR lpApplicationDataBuf, DWORD* pcchApplicationDataBuf )
939 {
940     awstring qual, appdata;
941     LPWSTR comp;
942     UINT r;
943
944     TRACE("%s %08x %p %p %p %p\n", debugstr_a(szComponent), iIndex,
945           lpQualifierBuf, pcchQualifierBuf, lpApplicationDataBuf,
946           pcchApplicationDataBuf);
947
948     comp = strdupAtoW( szComponent );
949     if (szComponent && !comp)
950         return ERROR_OUTOFMEMORY;
951
952     qual.unicode = FALSE;
953     qual.str.a = lpQualifierBuf;
954
955     appdata.unicode = FALSE;
956     appdata.str.a = lpApplicationDataBuf;
957
958     r = MSI_EnumComponentQualifiers( comp, iIndex,
959               &qual, pcchQualifierBuf, &appdata, pcchApplicationDataBuf );
960     msi_free( comp );
961     return r;
962 }
963
964 /*************************************************************************
965  *  MsiEnumComponentQualifiersW [MSI.@]
966  */
967 UINT WINAPI MsiEnumComponentQualifiersW( LPCWSTR szComponent, DWORD iIndex,
968                 LPWSTR lpQualifierBuf, DWORD* pcchQualifierBuf,
969                 LPWSTR lpApplicationDataBuf, DWORD* pcchApplicationDataBuf )
970 {
971     awstring qual, appdata;
972
973     TRACE("%s %08x %p %p %p %p\n", debugstr_w(szComponent), iIndex,
974           lpQualifierBuf, pcchQualifierBuf, lpApplicationDataBuf,
975           pcchApplicationDataBuf);
976
977     qual.unicode = TRUE;
978     qual.str.w = lpQualifierBuf;
979
980     appdata.unicode = TRUE;
981     appdata.str.w = lpApplicationDataBuf;
982
983     return MSI_EnumComponentQualifiers( szComponent, iIndex,
984                  &qual, pcchQualifierBuf, &appdata, pcchApplicationDataBuf );
985 }
986
987 /*************************************************************************
988  *  MsiEnumRelatedProductsW   [MSI.@]
989  *
990  */
991 UINT WINAPI MsiEnumRelatedProductsW(LPCWSTR szUpgradeCode, DWORD dwReserved,
992                                     DWORD iProductIndex, LPWSTR lpProductBuf)
993 {
994     UINT r;
995     HKEY hkey;
996     DWORD dwSize = SQUISH_GUID_SIZE;
997     WCHAR szKeyName[SQUISH_GUID_SIZE];
998
999     TRACE("%s %u %u %p\n", debugstr_w(szUpgradeCode), dwReserved,
1000           iProductIndex, lpProductBuf);
1001
1002     if (NULL == szUpgradeCode)
1003         return ERROR_INVALID_PARAMETER;
1004     if (NULL == lpProductBuf)
1005         return ERROR_INVALID_PARAMETER;
1006
1007     r = MSIREG_OpenUpgradeCodesKey(szUpgradeCode, &hkey, FALSE);
1008     if (r != ERROR_SUCCESS)
1009         return ERROR_NO_MORE_ITEMS;
1010
1011     r = RegEnumValueW(hkey, iProductIndex, szKeyName, &dwSize, NULL, NULL, NULL, NULL);
1012     if( r == ERROR_SUCCESS )
1013         unsquash_guid(szKeyName, lpProductBuf);
1014     RegCloseKey(hkey);
1015
1016     return r;
1017 }
1018
1019 /*************************************************************************
1020  *  MsiEnumRelatedProductsA   [MSI.@]
1021  *
1022  */
1023 UINT WINAPI MsiEnumRelatedProductsA(LPCSTR szUpgradeCode, DWORD dwReserved,
1024                                     DWORD iProductIndex, LPSTR lpProductBuf)
1025 {
1026     LPWSTR szwUpgradeCode = NULL;
1027     WCHAR productW[GUID_SIZE];
1028     UINT r;
1029
1030     TRACE("%s %u %u %p\n", debugstr_a(szUpgradeCode), dwReserved,
1031           iProductIndex, lpProductBuf);
1032
1033     if (szUpgradeCode)
1034     {
1035         szwUpgradeCode = strdupAtoW( szUpgradeCode );
1036         if( !szwUpgradeCode )
1037             return ERROR_OUTOFMEMORY;
1038     }
1039
1040     r = MsiEnumRelatedProductsW( szwUpgradeCode, dwReserved,
1041                                  iProductIndex, productW );
1042     if (r == ERROR_SUCCESS)
1043     {
1044         WideCharToMultiByte( CP_ACP, 0, productW, GUID_SIZE,
1045                              lpProductBuf, GUID_SIZE, NULL, NULL );
1046     }
1047     msi_free( szwUpgradeCode);
1048     return r;
1049 }
1050
1051 /***********************************************************************
1052  * MsiEnumPatchesA            [MSI.@]
1053  */
1054 UINT WINAPI MsiEnumPatchesA( LPCSTR szProduct, DWORD iPatchIndex,
1055         LPSTR lpPatchBuf, LPSTR lpTransformsBuf, DWORD* pcchTransformsBuf)
1056 {
1057     FIXME("%s %d %p %p %p\n", debugstr_a(szProduct),
1058           iPatchIndex, lpPatchBuf, lpTransformsBuf, pcchTransformsBuf);
1059     return ERROR_NO_MORE_ITEMS;
1060 }
1061
1062 /***********************************************************************
1063  * MsiEnumPatchesW            [MSI.@]
1064  */
1065 UINT WINAPI MsiEnumPatchesW( LPCWSTR szProduct, DWORD iPatchIndex,
1066         LPWSTR lpPatchBuf, LPWSTR lpTransformsBuf, DWORD* pcchTransformsBuf)
1067 {
1068     FIXME("%s %d %p %p %p\n", debugstr_w(szProduct),
1069           iPatchIndex, lpPatchBuf, lpTransformsBuf, pcchTransformsBuf);
1070     return ERROR_NO_MORE_ITEMS;
1071 }
1072
1073 UINT WINAPI MsiEnumProductsExA( LPCSTR szProductCode, LPCSTR szUserSid,
1074         DWORD dwContext, DWORD dwIndex, LPSTR szInstalledProductCode,
1075         MSIINSTALLCONTEXT* pdwInstalledContext, LPSTR szSid, LPDWORD pcchSid)
1076 {
1077     FIXME("%s %s %d %d %p %p %p %p\n", debugstr_a(szProductCode), debugstr_a(szUserSid),
1078           dwContext, dwIndex, szInstalledProductCode, pdwInstalledContext,
1079           szSid, pcchSid);
1080     return ERROR_NO_MORE_ITEMS;
1081 }
1082
1083 UINT WINAPI MsiEnumProductsExW( LPCWSTR szProductCode, LPCWSTR szUserSid,
1084         DWORD dwContext, DWORD dwIndex, LPWSTR szInstalledProductCode,
1085         MSIINSTALLCONTEXT* pdwInstalledContext, LPWSTR szSid, LPDWORD pcchSid)
1086 {
1087     FIXME("%s %s %d %d %p %p %p %p\n", debugstr_w(szProductCode), debugstr_w(szUserSid),
1088           dwContext, dwIndex, szInstalledProductCode, pdwInstalledContext,
1089           szSid, pcchSid);
1090     return ERROR_NO_MORE_ITEMS;
1091 }