Add enum support.
[wine/gsoc_dplay.git] / dlls / msi / registry.c
blobd0916a7eb49cb87307948b1b7a6d413b16dff548
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2005 Mike McCormak for CodeWeavers
5 * Copyright 2005 Aric Stewart for CodeWeavers
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.
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.
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
22 #include <stdarg.h>
24 #define COBJMACROS
25 #define NONAMELESSUNION
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"
40 WINE_DEFAULT_DEBUG_CHANNEL(msi);
43 /*
44 * This module will be all the helper functions for registry access by the
45 * installer bits.
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};
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 };
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};
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 };
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};
88 static const WCHAR szUninstall_fmt[] = {
89 'S','o','f','t','w','a','r','e','\\',
90 'M','i','c','r','o','s','o','f','t','\\',
91 'W','i','n','d','o','w','s','\\',
92 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
93 'U','n','i','n','s','t','a','l','l','\\',
94 '%','s',0 };
96 static const WCHAR szUserProduct_fmt[] = {
97 'S','o','f','t','w','a','r','e','\\',
98 'M','i','c','r','o','s','o','f','t','\\',
99 'I','n','s','t','a','l','l','e','r','\\',
100 'P','r','o','d','u','c','t','s','\\',
101 '%','s',0};
103 static const WCHAR szInstaller_Products[] = {
104 'S','o','f','t','w','a','r','e','\\',
105 'M','i','c','r','o','s','o','f','t','\\',
106 'W','i','n','d','o','w','s','\\',
107 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
108 'I','n','s','t','a','l','l','e','r','\\',
109 'P','r','o','d','u','c','t','s',0};
111 static const WCHAR szInstaller_Products_fmt[] = {
112 'S','o','f','t','w','a','r','e','\\',
113 'M','i','c','r','o','s','o','f','t','\\',
114 'W','i','n','d','o','w','s','\\',
115 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
116 'I','n','s','t','a','l','l','e','r','\\',
117 'P','r','o','d','u','c','t','s','\\',
118 '%','s',0};
120 BOOL unsquash_guid(LPCWSTR in, LPWSTR out)
122 DWORD i,n=0;
124 out[n++]='{';
125 for(i=0; i<8; i++)
126 out[n++] = in[7-i];
127 out[n++]='-';
128 for(i=0; i<4; i++)
129 out[n++] = in[11-i];
130 out[n++]='-';
131 for(i=0; i<4; i++)
132 out[n++] = in[15-i];
133 out[n++]='-';
134 for(i=0; i<2; i++)
136 out[n++] = in[17+i*2];
137 out[n++] = in[16+i*2];
139 out[n++]='-';
140 for( ; i<8; i++)
142 out[n++] = in[17+i*2];
143 out[n++] = in[16+i*2];
145 out[n++]='}';
146 out[n]=0;
147 return TRUE;
150 BOOL squash_guid(LPCWSTR in, LPWSTR out)
152 DWORD i,n=0;
154 if(in[n++] != '{')
155 return FALSE;
156 for(i=0; i<8; i++)
157 out[7-i] = in[n++];
158 if(in[n++] != '-')
159 return FALSE;
160 for(i=0; i<4; i++)
161 out[11-i] = in[n++];
162 if(in[n++] != '-')
163 return FALSE;
164 for(i=0; i<4; i++)
165 out[15-i] = in[n++];
166 if(in[n++] != '-')
167 return FALSE;
168 for(i=0; i<2; i++)
170 out[17+i*2] = in[n++];
171 out[16+i*2] = in[n++];
173 if(in[n++] != '-')
174 return FALSE;
175 for( ; i<8; i++)
177 out[17+i*2] = in[n++];
178 out[16+i*2] = in[n++];
180 out[32]=0;
181 if(in[n++] != '}')
182 return FALSE;
183 if(in[n])
184 return FALSE;
185 return TRUE;
189 /* tables for encoding and decoding base85 */
190 static const unsigned char table_dec85[0x80] = {
191 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
192 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
193 0xff,0x00,0xff,0xff,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0xff,
194 0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14,0x15,0xff,0xff,0xff,0x16,0xff,0x17,
195 0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,
196 0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x31,0x32,0x33,0xff,0x34,0x35,0x36,
197 0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,0x40,0x41,0x42,0x43,0x44,0x45,0x46,
198 0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,0x50,0x51,0x52,0xff,0x53,0x54,0xff,
201 static const char table_enc85[] =
202 "!$%&'()*+,-.0123456789=?@ABCDEFGHIJKLMNO"
203 "PQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwx"
204 "yz{}~";
207 * Converts a base85 encoded guid into a GUID pointer
208 * Base85 encoded GUIDs should be 20 characters long.
210 * returns TRUE if successful, FALSE if not
212 BOOL decode_base85_guid( LPCWSTR str, GUID *guid )
214 DWORD i, val = 0, base = 1, *p;
216 p = (DWORD*) guid;
217 for( i=0; i<20; i++ )
219 if( (i%5) == 0 )
221 val = 0;
222 base = 1;
224 val += table_dec85[str[i]] * base;
225 if( str[i] >= 0x80 )
226 return FALSE;
227 if( table_dec85[str[i]] == 0xff )
228 return FALSE;
229 if( (i%5) == 4 )
230 p[i/5] = val;
231 base *= 85;
233 return TRUE;
237 * Encodes a base85 guid given a GUID pointer
238 * Caller should provide a 21 character buffer for the encoded string.
240 * returns TRUE if successful, FALSE if not
242 BOOL encode_base85_guid( GUID *guid, LPWSTR str )
244 unsigned int x, *p, i;
246 p = (unsigned int*) guid;
247 for( i=0; i<4; i++ )
249 x = p[i];
250 *str++ = table_enc85[x%85];
251 x = x/85;
252 *str++ = table_enc85[x%85];
253 x = x/85;
254 *str++ = table_enc85[x%85];
255 x = x/85;
256 *str++ = table_enc85[x%85];
257 x = x/85;
258 *str++ = table_enc85[x%85];
260 *str = 0;
262 return TRUE;
266 UINT MSIREG_OpenUninstallKey(LPCWSTR szProduct, HKEY* key, BOOL create)
268 UINT rc;
269 WCHAR keypath[0x200];
270 TRACE("%s\n",debugstr_w(szProduct));
272 sprintfW(keypath,szUninstall_fmt,szProduct);
274 if (create)
275 rc = RegCreateKeyW(HKEY_LOCAL_MACHINE, keypath, key);
276 else
277 rc = RegOpenKeyW(HKEY_LOCAL_MACHINE, keypath, key);
279 return rc;
282 UINT MSIREG_OpenUserProductsKey(LPCWSTR szProduct, HKEY* key, BOOL create)
284 UINT rc;
285 WCHAR squished_pc[GUID_SIZE];
286 WCHAR keypath[0x200];
288 TRACE("%s\n",debugstr_w(szProduct));
289 squash_guid(szProduct,squished_pc);
290 TRACE("squished (%s)\n", debugstr_w(squished_pc));
292 sprintfW(keypath,szUserProduct_fmt,squished_pc);
294 if (create)
295 rc = RegCreateKeyW(HKEY_CURRENT_USER,keypath,key);
296 else
297 rc = RegOpenKeyW(HKEY_CURRENT_USER,keypath,key);
299 return rc;
302 UINT MSIREG_OpenUserFeaturesKey(LPCWSTR szProduct, HKEY* key, BOOL create)
304 UINT rc;
305 WCHAR squished_pc[GUID_SIZE];
306 WCHAR keypath[0x200];
308 TRACE("%s\n",debugstr_w(szProduct));
309 squash_guid(szProduct,squished_pc);
310 TRACE("squished (%s)\n", debugstr_w(squished_pc));
312 sprintfW(keypath,szUserFeatures_fmt,squished_pc);
314 if (create)
315 rc = RegCreateKeyW(HKEY_CURRENT_USER,keypath,key);
316 else
317 rc = RegOpenKeyW(HKEY_CURRENT_USER,keypath,key);
319 return rc;
322 UINT MSIREG_OpenFeatures(HKEY* key)
324 return RegCreateKeyW(HKEY_LOCAL_MACHINE,szInstaller_Features,key);
327 UINT MSIREG_OpenFeaturesKey(LPCWSTR szProduct, HKEY* key, BOOL create)
329 UINT rc;
330 WCHAR squished_pc[GUID_SIZE];
331 WCHAR keypath[0x200];
333 TRACE("%s\n",debugstr_w(szProduct));
334 squash_guid(szProduct,squished_pc);
335 TRACE("squished (%s)\n", debugstr_w(squished_pc));
337 sprintfW(keypath,szInstaller_Features_fmt,squished_pc);
339 if (create)
340 rc = RegCreateKeyW(HKEY_LOCAL_MACHINE,keypath,key);
341 else
342 rc = RegOpenKeyW(HKEY_LOCAL_MACHINE,keypath,key);
344 return rc;
347 UINT MSIREG_OpenComponents(HKEY* key)
349 return RegCreateKeyW(HKEY_LOCAL_MACHINE,szInstaller_Components,key);
352 UINT MSIREG_OpenComponentsKey(LPCWSTR szComponent, HKEY* key, BOOL create)
354 UINT rc;
355 WCHAR squished_cc[GUID_SIZE];
356 WCHAR keypath[0x200];
358 TRACE("%s\n",debugstr_w(szComponent));
359 squash_guid(szComponent,squished_cc);
360 TRACE("squished (%s)\n", debugstr_w(squished_cc));
362 sprintfW(keypath,szInstaller_Components_fmt,squished_cc);
364 if (create)
365 rc = RegCreateKeyW(HKEY_LOCAL_MACHINE,keypath,key);
366 else
367 rc = RegOpenKeyW(HKEY_LOCAL_MACHINE,keypath,key);
369 return rc;
372 UINT MSIREG_OpenProductsKey(LPCWSTR szProduct, HKEY* key, BOOL create)
374 UINT rc;
375 WCHAR squished_pc[GUID_SIZE];
376 WCHAR keypath[0x200];
378 TRACE("%s\n",debugstr_w(szProduct));
379 squash_guid(szProduct,squished_pc);
380 TRACE("squished (%s)\n", debugstr_w(squished_pc));
382 sprintfW(keypath,szInstaller_Products_fmt,squished_pc);
384 if (create)
385 rc = RegCreateKeyW(HKEY_LOCAL_MACHINE,keypath,key);
386 else
387 rc = RegOpenKeyW(HKEY_LOCAL_MACHINE,keypath,key);
389 return rc;
392 /*************************************************************************
393 * MsiDecomposeDescriptorW [MSI.@]
395 * Decomposes an MSI descriptor into product, feature and component parts.
396 * An MSI descriptor is a string of the form:
397 * [base 85 guid] [feature code] '>' [base 85 guid]
399 * PARAMS
400 * szDescriptor [I] the descriptor to decompose
401 * szProduct [O] buffer of MAX_FEATURE_CHARS for the product guid
402 * szFeature [O] buffer of MAX_FEATURE_CHARS for the feature code
403 * szComponent [O] buffer of MAX_FEATURE_CHARS for the component guid
404 * pUsed [O] the length of the descriptor
406 * RETURNS
407 * ERROR_SUCCESS if everything worked correctly
408 * ERROR_INVALID_PARAMETER if the descriptor was invalid
411 UINT WINAPI MsiDecomposeDescriptorW( LPCWSTR szDescriptor, LPWSTR szProduct,
412 LPWSTR szFeature, LPWSTR szComponent, DWORD *pUsed )
414 UINT r, len;
415 LPWSTR p;
416 GUID product, component;
418 TRACE("%s %p %p %p %p\n", debugstr_w(szDescriptor), szProduct,
419 szFeature, szComponent, pUsed);
421 r = decode_base85_guid( szDescriptor, &product );
422 if( !r )
423 return ERROR_INVALID_PARAMETER;
425 TRACE("product %s\n", debugstr_guid( &product ));
427 p = strchrW(&szDescriptor[20],'>');
428 if( !p )
429 return ERROR_INVALID_PARAMETER;
431 len = (p - &szDescriptor[20]);
432 if( len > MAX_FEATURE_CHARS )
433 return ERROR_INVALID_PARAMETER;
434 memcpy( szFeature, &szDescriptor[20], len*sizeof(WCHAR) );
435 szFeature[len] = 0;
437 TRACE("feature %s\n", debugstr_w( szFeature ));
439 r = decode_base85_guid( p+1, &component );
440 if( !r )
441 return ERROR_INVALID_PARAMETER;
443 TRACE("component %s\n", debugstr_guid( &component ));
445 StringFromGUID2( &product, szProduct, MAX_FEATURE_CHARS+1 );
446 StringFromGUID2( &component, szComponent, MAX_FEATURE_CHARS+1 );
447 len = ( &p[21] - szDescriptor );
449 TRACE("length = %d\n", len);
450 *pUsed = len;
452 return ERROR_SUCCESS;
455 UINT WINAPI MsiDecomposeDescriptorA( LPCSTR szDescriptor, LPSTR szProduct,
456 LPSTR szFeature, LPSTR szComponent, DWORD *pUsed )
458 WCHAR product[MAX_FEATURE_CHARS+1];
459 WCHAR feature[MAX_FEATURE_CHARS+1];
460 WCHAR component[MAX_FEATURE_CHARS+1];
461 LPWSTR str = NULL;
462 UINT r, len;
464 TRACE("%s %p %p %p %p\n", debugstr_a(szDescriptor), szProduct,
465 szFeature, szComponent, pUsed);
467 if( szDescriptor )
469 len = MultiByteToWideChar( CP_ACP, 0, szDescriptor, -1, NULL, 0 );
470 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
471 MultiByteToWideChar( CP_ACP, 0, szDescriptor, -1, str, len );
474 r = MsiDecomposeDescriptorW( str, product, feature, component, pUsed );
476 WideCharToMultiByte( CP_ACP, 0, product, MAX_FEATURE_CHARS+1,
477 szProduct, MAX_FEATURE_CHARS+1, NULL, NULL );
478 WideCharToMultiByte( CP_ACP, 0, feature, MAX_FEATURE_CHARS+1,
479 szFeature, MAX_FEATURE_CHARS+1, NULL, NULL );
480 WideCharToMultiByte( CP_ACP, 0, component, MAX_FEATURE_CHARS+1,
481 szComponent, MAX_FEATURE_CHARS+1, NULL, NULL );
483 HeapFree( GetProcessHeap(), 0, str );
485 return r;