Move the guid squishing functions out of msi.c and make a new
[wine] / dlls / msi / registry.c
1 /*
2  * Implementation of the Microsoft Installer (msi.dll)
3  *
4  * Copyright 2005 Mike McCormak 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 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 };
95
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};
102
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};
110
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};
119
120 BOOL unsquash_guid(LPCWSTR in, LPWSTR out)
121 {
122     DWORD i,n=0;
123
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++)
135     {
136         out[n++] = in[17+i*2];
137         out[n++] = in[16+i*2];
138     }
139     out[n++]='-';
140     for( ; i<8; i++)
141     {
142         out[n++] = in[17+i*2];
143         out[n++] = in[16+i*2];
144     }
145     out[n++]='}';
146     out[n]=0;
147     return TRUE;
148 }
149
150 BOOL squash_guid(LPCWSTR in, LPWSTR out)
151 {
152     DWORD i,n=0;
153
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++)
169     {
170         out[17+i*2] = in[n++];
171         out[16+i*2] = in[n++];
172     }
173     if(in[n++] != '-')
174         return FALSE;
175     for( ; i<8; i++)
176     {
177         out[17+i*2] = in[n++];
178         out[16+i*2] = in[n++];
179     }
180     out[32]=0;
181     if(in[n++] != '}')
182         return FALSE;
183     if(in[n])
184         return FALSE;
185     return TRUE;
186 }
187
188
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,
199 };
200
201 static const char table_enc85[] =
202 "!$%&'()*+,-.0123456789=?@ABCDEFGHIJKLMNO"
203 "PQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwx"
204 "yz{}~";
205
206 /*
207  *  Converts a base85 encoded guid into a GUID pointer
208  *  Base85 encoded GUIDs should be 20 characters long.
209  *
210  *  returns TRUE if successful, FALSE if not
211  */
212 BOOL decode_base85_guid( LPCWSTR str, GUID *guid )
213 {
214     DWORD i, val = 0, base = 1, *p;
215
216     p = (DWORD*) guid;
217     for( i=0; i<20; i++ )
218     {
219         if( (i%5) == 0 )
220         {
221             val = 0;
222             base = 1;
223         }
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;
232     }
233     return TRUE;
234 }
235
236 /*
237  *  Encodes a base85 guid given a GUID pointer
238  *  Caller should provide a 21 character buffer for the encoded string.
239  *
240  *  returns TRUE if successful, FALSE if not
241  */
242 BOOL encode_base85_guid( GUID *guid, LPWSTR str )
243 {
244     unsigned int x, *p, i;
245
246     p = (unsigned int*) guid;
247     for( i=0; i<4; i++ )
248     {
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];
259     }
260     *str = 0;
261
262     return TRUE;
263 }
264
265
266 UINT MSIREG_OpenUninstallKey(LPCWSTR szProduct, HKEY* key, BOOL create)
267 {
268     UINT rc;
269     WCHAR keypath[0x200];
270     TRACE("%s\n",debugstr_w(szProduct));
271
272     sprintfW(keypath,szUninstall_fmt,szProduct);
273
274     if (create)
275         rc = RegCreateKeyW(HKEY_LOCAL_MACHINE, keypath, key);
276     else
277         rc = RegOpenKeyW(HKEY_LOCAL_MACHINE, keypath, key);
278
279     return rc;
280 }
281
282 UINT MSIREG_OpenUserProductsKey(LPCWSTR szProduct, HKEY* key, BOOL create)
283 {
284     UINT rc;
285     WCHAR squished_pc[GUID_SIZE];
286     WCHAR keypath[0x200];
287
288     TRACE("%s\n",debugstr_w(szProduct));
289     squash_guid(szProduct,squished_pc);
290     TRACE("squished (%s)\n", debugstr_w(squished_pc));
291
292     sprintfW(keypath,szUserProduct_fmt,squished_pc);
293
294     if (create)
295         rc = RegCreateKeyW(HKEY_CURRENT_USER,keypath,key);
296     else
297         rc = RegOpenKeyW(HKEY_CURRENT_USER,keypath,key);
298
299     return rc;
300 }
301
302 UINT MSIREG_OpenUserFeaturesKey(LPCWSTR szProduct, HKEY* key, BOOL create)
303 {
304     UINT rc;
305     WCHAR squished_pc[GUID_SIZE];
306     WCHAR keypath[0x200];
307
308     TRACE("%s\n",debugstr_w(szProduct));
309     squash_guid(szProduct,squished_pc);
310     TRACE("squished (%s)\n", debugstr_w(squished_pc));
311
312     sprintfW(keypath,szUserFeatures_fmt,squished_pc);
313
314     if (create)
315         rc = RegCreateKeyW(HKEY_CURRENT_USER,keypath,key);
316     else
317         rc = RegOpenKeyW(HKEY_CURRENT_USER,keypath,key);
318
319     return rc;
320 }
321
322 UINT MSIREG_OpenFeatures(HKEY* key)
323 {
324     return RegCreateKeyW(HKEY_LOCAL_MACHINE,szInstaller_Features,key);
325 }
326
327 UINT MSIREG_OpenFeaturesKey(LPCWSTR szProduct, HKEY* key, BOOL create)
328 {
329     UINT rc;
330     WCHAR squished_pc[GUID_SIZE];
331     WCHAR keypath[0x200];
332
333     TRACE("%s\n",debugstr_w(szProduct));
334     squash_guid(szProduct,squished_pc);
335     TRACE("squished (%s)\n", debugstr_w(squished_pc));
336
337     sprintfW(keypath,szInstaller_Features_fmt,squished_pc);
338
339     if (create)
340         rc = RegCreateKeyW(HKEY_LOCAL_MACHINE,keypath,key);
341     else
342         rc = RegOpenKeyW(HKEY_LOCAL_MACHINE,keypath,key);
343
344     return rc;
345 }
346
347 UINT MSIREG_OpenComponents(HKEY* key)
348 {
349     return RegCreateKeyW(HKEY_LOCAL_MACHINE,szInstaller_Components,key);
350 }
351
352 UINT MSIREG_OpenComponentsKey(LPCWSTR szComponent, HKEY* key, BOOL create)
353 {
354     UINT rc;
355     WCHAR squished_cc[GUID_SIZE];
356     WCHAR keypath[0x200];
357
358     TRACE("%s\n",debugstr_w(szComponent));
359     squash_guid(szComponent,squished_cc);
360     TRACE("squished (%s)\n", debugstr_w(squished_cc));
361
362     sprintfW(keypath,szInstaller_Components_fmt,squished_cc);
363
364     if (create)
365         rc = RegCreateKeyW(HKEY_LOCAL_MACHINE,keypath,key);
366     else
367         rc = RegOpenKeyW(HKEY_LOCAL_MACHINE,keypath,key);
368
369     return rc;
370 }
371
372 UINT MSIREG_OpenProductsKey(LPCWSTR szProduct, HKEY* key, BOOL create)
373 {
374     UINT rc;
375     WCHAR squished_pc[GUID_SIZE];
376     WCHAR keypath[0x200];
377
378     TRACE("%s\n",debugstr_w(szProduct));
379     squash_guid(szProduct,squished_pc);
380     TRACE("squished (%s)\n", debugstr_w(squished_pc));
381
382     sprintfW(keypath,szInstaller_Products_fmt,squished_pc);
383
384     if (create)
385         rc = RegCreateKeyW(HKEY_LOCAL_MACHINE,keypath,key);
386     else
387         rc = RegOpenKeyW(HKEY_LOCAL_MACHINE,keypath,key);
388
389     return rc;
390 }