crypt32: Fixed day/month mismatch in time encoding routines.
[wine] / dlls / advpack / reg.c
1 /*
2  * Advpack registry functions
3  *
4  * Copyright 2004 Huw D M Davies
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <stdarg.h>
22 #include "windef.h"
23 #include "winbase.h"
24 #include "winreg.h"
25 #include "winerror.h"
26 #include "winuser.h"
27 #include "winternl.h"
28 #include "setupapi.h"
29 #include "advpub.h"
30 #include "wine/unicode.h"
31 #include "wine/debug.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(advpack);
34
35 static const WCHAR REGINST[] = {'R','E','G','I','N','S','T',0};
36 static const WCHAR Strings[] = {'S','t','r','i','n','g','s',0};
37 static const WCHAR MOD_PATH[] = {'_','M','O','D','_','P','A','T','H',0};
38 static const WCHAR SYS_MOD_PATH[] = {'_','S','Y','S','_','M','O','D','_','P','A','T','H',0};
39 static const WCHAR SystemRoot[] = {'S','y','s','t','e','m','R','o','o','t',0};
40 static const WCHAR escaped_SystemRoot[] = {'%','S','y','s','t','e','m','R','o','o','t','%',0};
41
42 static BOOL get_temp_ini_path(LPWSTR name)
43 {
44     WCHAR tmp_dir[MAX_PATH];
45     WCHAR prefix[] = {'a','v','p',0};
46
47     if(!GetTempPathW(sizeof(tmp_dir)/sizeof(WCHAR), tmp_dir))
48        return FALSE;
49
50     if(!GetTempFileNameW(tmp_dir, prefix, 0, name))
51         return FALSE;
52     return TRUE;
53 }
54
55 static BOOL create_tmp_ini_file(HMODULE hm, WCHAR *ini_file)
56 {
57     HRSRC hrsrc;
58     HGLOBAL hmem = NULL;
59     DWORD rsrc_size, bytes_written;
60     VOID *rsrc_data;
61     HANDLE hf = INVALID_HANDLE_VALUE;
62
63     if(!get_temp_ini_path(ini_file)) {
64         ERR("Can't get temp ini file path\n");
65         goto error;
66     }
67
68     if(!(hrsrc = FindResourceW(hm, REGINST, REGINST))) {
69         ERR("Can't find REGINST resource\n");
70         goto error;
71     }
72
73     rsrc_size = SizeofResource(hm, hrsrc);
74     hmem = LoadResource(hm, hrsrc);
75     rsrc_data = LockResource(hmem);
76
77     if(!rsrc_data || !rsrc_size) {
78         ERR("Can't load REGINST resource\n");
79         goto error;
80     }       
81
82     if((hf = CreateFileW(ini_file, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
83                          FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) {
84         ERR("Unable to create temp ini file\n");
85         goto error;
86     }
87     if(!WriteFile(hf, rsrc_data, rsrc_size, &bytes_written, NULL) || rsrc_size != bytes_written) {
88         ERR("Write failed\n");
89         goto error;
90     }
91     FreeResource(hmem);
92     CloseHandle(hf);
93     return TRUE;
94 error:
95     if(hmem) FreeResource(hmem);
96     if(hf != INVALID_HANDLE_VALUE) CloseHandle(hf);
97     return FALSE;
98 }
99
100 static void strentry_atow(STRENTRYA *aentry, STRENTRYW *wentry)
101 {
102     DWORD name_len, val_len;
103
104     name_len = MultiByteToWideChar(CP_ACP, 0, aentry->pszName, -1, NULL, 0);
105     val_len = MultiByteToWideChar(CP_ACP, 0, aentry->pszName, -1, NULL, 0);
106
107     wentry->pszName = HeapAlloc(GetProcessHeap(), 0, name_len * sizeof(WCHAR));
108     wentry->pszValue = HeapAlloc(GetProcessHeap(), 0, val_len * sizeof(WCHAR));
109
110     MultiByteToWideChar(CP_ACP, 0, aentry->pszName, -1, wentry->pszName, name_len);
111     MultiByteToWideChar(CP_ACP, 0, aentry->pszName, -1, wentry->pszValue, val_len);
112 }
113
114 static STRTABLEW *strtable_atow(const STRTABLEA *atable)
115 {
116     STRTABLEW *wtable;
117     DWORD j;
118
119     wtable = HeapAlloc(GetProcessHeap(), 0, sizeof(STRTABLEW));
120     wtable->pse = HeapAlloc(GetProcessHeap(), 0, atable->cEntries * sizeof(STRENTRYW));
121     wtable->cEntries = atable->cEntries;
122
123     for (j = 0; j < wtable->cEntries; j++)
124         strentry_atow(&atable->pse[j], &wtable->pse[j]);
125
126     return wtable;
127 }
128
129 static void free_strtable(STRTABLEW *wtable)
130 {
131     DWORD j;
132
133     for (j = 0; j < wtable->cEntries; j++)
134     {
135         HeapFree(GetProcessHeap(), 0, wtable->pse[j].pszName);
136         HeapFree(GetProcessHeap(), 0, wtable->pse[j].pszValue);
137     }
138
139     HeapFree(GetProcessHeap(), 0, wtable->pse);
140     HeapFree(GetProcessHeap(), 0, wtable);
141 }
142
143 /***********************************************************************
144  *          RegInstallA (advpack.@)
145  *
146  * See RegInstallW.
147  */
148 HRESULT WINAPI RegInstallA(HMODULE hm, LPCSTR pszSection, const STRTABLEA* pstTable)
149 {
150     UNICODE_STRING section;
151     STRTABLEW *wtable;
152     HRESULT hr;
153
154     TRACE("(%p, %s, %p)\n", hm, debugstr_a(pszSection), pstTable);
155
156     if (pstTable)
157         wtable = strtable_atow(pstTable);
158     else
159         wtable = NULL;
160
161     RtlCreateUnicodeStringFromAsciiz(&section, pszSection);
162
163     hr = RegInstallW(hm, section.Buffer, wtable);
164
165     if (pstTable)
166         free_strtable(wtable);
167
168     RtlFreeUnicodeString(&section);
169
170     return hr;
171 }
172
173 /***********************************************************************
174  *          RegInstallW (advpack.@)
175  *
176  * Loads an INF from a string resource, adds entries to the string
177  * substitution table, and executes the INF.
178  *
179  * PARAMS
180  *   hm         [I] Module that contains the REGINST resouce.
181  *   pszSection [I] The INF section to execute.
182  *   pstTable   [I] Table of string substitutions.
183  * 
184  * RETURNS
185  *   Success: S_OK.
186  *   Failure: E_FAIL.
187  */
188 HRESULT WINAPI RegInstallW(HMODULE hm, LPCWSTR pszSection, const STRTABLEW* pstTable)
189 {
190     int i;
191     WCHAR tmp_ini_path[MAX_PATH];
192     WCHAR mod_path[MAX_PATH + 2], sys_mod_path[MAX_PATH + 2], sys_root[MAX_PATH];
193     HINF hinf;
194     static const WCHAR quote[] = {'\"',0};
195
196     TRACE("(%p, %s, %p)\n", hm, debugstr_w(pszSection), pstTable);
197
198     if(!create_tmp_ini_file(hm, tmp_ini_path))
199         return E_FAIL;
200
201     /* Write a couple of pre-defined strings */
202     mod_path[0] = '\"';
203     GetModuleFileNameW(hm, mod_path + 1, sizeof(mod_path)/sizeof(WCHAR) - 2);
204     strcatW(mod_path, quote);
205     WritePrivateProfileStringW(Strings, MOD_PATH, mod_path, tmp_ini_path);
206     
207     *sys_root = '\0';
208     GetEnvironmentVariableW(SystemRoot, sys_root, sizeof(sys_root)/sizeof(WCHAR));
209     if(!strncmpiW(sys_root, mod_path + 1, strlenW(sys_root))) {
210         sys_mod_path[0] = '\"';
211         strcpyW(sys_mod_path + 1, escaped_SystemRoot);
212         strcatW(sys_mod_path, mod_path + 1 + strlenW(sys_root));
213     } else {
214         FIXME("SYS_MOD_PATH needs more work\n");
215         strcpyW(sys_mod_path, mod_path);
216     }
217     WritePrivateProfileStringW(Strings, SYS_MOD_PATH, sys_mod_path, tmp_ini_path);
218
219     /* Write the additional string table */
220     if (pstTable) for(i = 0; i < pstTable->cEntries; i++) {
221         WCHAR tmp_value[MAX_PATH + 2];
222
223         tmp_value[0] = '\"';
224         lstrcpyW(tmp_value + 1, pstTable->pse[i].pszValue);
225         lstrcatW(tmp_value, quote);
226
227         WritePrivateProfileStringW(Strings, pstTable->pse[i].pszName, tmp_value, tmp_ini_path);
228     }
229     /* flush cache */
230     WritePrivateProfileStringW(NULL, NULL, NULL, tmp_ini_path);
231
232
233     if((hinf = SetupOpenInfFileW(tmp_ini_path, NULL, INF_STYLE_WIN4, NULL)) ==
234        INVALID_HANDLE_VALUE) {
235         ERR("Setupapi can't open inf\n");
236         return E_FAIL;
237     }
238
239     /* append any layout files */
240     SetupOpenAppendInfFileW(NULL, hinf, NULL);
241
242     /* Need to do a lot more here */
243     SetupInstallFromInfSectionW(NULL, hinf, pszSection,
244                                 SPINST_INIFILES | SPINST_REGISTRY | SPINST_PROFILEITEMS,
245                                 HKEY_LOCAL_MACHINE, NULL, 0, NULL, NULL, NULL, NULL);
246     
247     SetupCloseInfFile(hinf);
248     DeleteFileW(tmp_ini_path);
249
250     return S_OK;
251 }
252
253 /***********************************************************************
254  *          RegRestoreAllA (advpack.@)
255  *
256  * See RegRestoreAllW.
257  */
258 HRESULT WINAPI RegRestoreAllA(HWND hWnd, LPSTR pszTitleString, HKEY hkBackupKey)
259 {
260     UNICODE_STRING title;
261     HRESULT hr;
262
263     TRACE("(%p, %s, %p)\n", hWnd, debugstr_a(pszTitleString), hkBackupKey);
264
265     RtlCreateUnicodeStringFromAsciiz(&title, pszTitleString);
266
267     hr = RegRestoreAllW(hWnd, title.Buffer, hkBackupKey);
268
269     RtlFreeUnicodeString(&title);
270
271     return hr;
272 }
273
274 /***********************************************************************
275  *          RegRestoreAllW (advpack.@)
276  *
277  * Restores all saved registry entries.
278  *
279  * PARAMS
280  *   hWnd           [I] Handle to the window used for the display.
281  *   pszTitleString [I] Title of the window.
282  *   hkBackupKey    [I] Handle to the backup key.
283  *
284  * RETURNS
285  *   Success: S_OK.
286  *   Failure: E_FAIL.
287  *
288  * BUGS
289  *   Unimplemented.
290  */
291 HRESULT WINAPI RegRestoreAllW(HWND hWnd, LPWSTR pszTitleString, HKEY hkBackupKey)
292 {
293     FIXME("(%p, %s, %p) stub\n", hWnd, debugstr_w(pszTitleString), hkBackupKey);
294     
295     return E_FAIL;   
296 }
297
298 /***********************************************************************
299  *          RegSaveRestoreA (advpack.@)
300  *
301  * See RegSaveRestoreW.
302  */
303 HRESULT WINAPI RegSaveRestoreA(HWND hWnd, LPCSTR pszTitleString, HKEY hkBackupKey,
304                                LPCSTR pcszRootKey, LPCSTR pcszSubKey,
305                                LPCSTR pcszValueName, DWORD dwFlags)
306 {
307     UNICODE_STRING title, root, subkey, value;
308     HRESULT hr;
309
310     TRACE("(%p, %s, %p, %s, %s, %s, %ld)\n", hWnd, debugstr_a(pszTitleString),
311           hkBackupKey, debugstr_a(pcszRootKey), debugstr_a(pcszSubKey),
312           debugstr_a(pcszValueName), dwFlags);
313
314     RtlCreateUnicodeStringFromAsciiz(&title, pszTitleString);
315     RtlCreateUnicodeStringFromAsciiz(&root, pcszRootKey);
316     RtlCreateUnicodeStringFromAsciiz(&subkey, pcszSubKey);
317     RtlCreateUnicodeStringFromAsciiz(&value, pcszValueName);
318
319     hr = RegSaveRestoreW(hWnd, title.Buffer, hkBackupKey, root.Buffer,
320                          subkey.Buffer, value.Buffer, dwFlags);
321
322     RtlFreeUnicodeString(&title);
323     RtlFreeUnicodeString(&root);
324     RtlFreeUnicodeString(&subkey);
325     RtlFreeUnicodeString(&value);
326
327     return hr;
328 }
329
330 /***********************************************************************
331  *          RegSaveRestoreW (advpack.@)
332  *
333  * Saves or restores the specified registry value.
334  *
335  * PARAMS
336  *   hWnd           [I] Handle to the window used for the display.
337  *   pszTitleString [I] Title of the window.
338  *   hkBackupKey    [I] Key used to store the backup data.
339  *   pcszRootKey    [I] Root key of the registry value
340  *   pcszSubKey     [I] Sub key of the registry value.
341  *   pcszValueName  [I] Value to save or restore. 
342  *   dwFlags        [I] See advpub.h.
343  * 
344  * RETURNS
345  *   Success: S_OK.
346  *   Failure: E_FAIL.
347  *
348  * BUGS
349  *   Unimplemented.
350  */
351 HRESULT WINAPI RegSaveRestoreW(HWND hWnd, LPCWSTR pszTitleString, HKEY hkBackupKey,
352                                LPCWSTR pcszRootKey, LPCWSTR pcszSubKey,
353                                LPCWSTR pcszValueName, DWORD dwFlags)
354 {
355     FIXME("(%p, %s, %p, %s, %s, %s, %ld): stub\n", hWnd, debugstr_w(pszTitleString),
356           hkBackupKey, debugstr_w(pcszRootKey), debugstr_w(pcszSubKey),
357           debugstr_w(pcszValueName), dwFlags);
358
359     return E_FAIL;   
360 }
361
362 /***********************************************************************
363  *          RegSaveRestoreOnINFA (advpack.@)
364  *
365  * See RegSaveRestoreOnINFW.
366  */
367 HRESULT WINAPI RegSaveRestoreOnINFA(HWND hWnd, LPCSTR pszTitle, LPCSTR pszINF,
368                                     LPCSTR pszSection, HKEY hHKLMBackKey,
369                                     HKEY hHKCUBackKey, DWORD dwFlags)
370 {
371     UNICODE_STRING title, inf, section;
372     HRESULT hr;
373
374     TRACE("(%p, %s, %s, %s, %p, %p, %ld)\n", hWnd, debugstr_a(pszTitle),
375           debugstr_a(pszINF), debugstr_a(pszSection),
376           hHKLMBackKey, hHKCUBackKey, dwFlags);
377
378     RtlCreateUnicodeStringFromAsciiz(&title, pszTitle);
379     RtlCreateUnicodeStringFromAsciiz(&inf, pszINF);
380     RtlCreateUnicodeStringFromAsciiz(&section, pszSection);
381
382     hr = RegSaveRestoreOnINFW(hWnd, title.Buffer, inf.Buffer, section.Buffer,
383                               hHKLMBackKey, hHKCUBackKey, dwFlags);
384
385     RtlFreeUnicodeString(&title);
386     RtlFreeUnicodeString(&inf);
387     RtlFreeUnicodeString(&section);
388
389     return hr;
390 }
391
392 /***********************************************************************
393  *          RegSaveRestoreOnINFW (advpack.@)
394  *
395  * Saves or restores the specified INF Reg section.
396  *
397  * PARAMS
398  *   hWnd         [I] Handle to the window used for the display.
399  *   pszTitle     [I] Title of the window.
400  *   pszINF       [I] Filename of the INF.
401  *   pszSection   [I] Section to save or restore.
402  *   hHKLMBackKey [I] Opened key in HKLM to store data.
403  *   hHKCUBackKey [I] Opened key in HKCU to store data.
404  *   dwFlags      [I] See advpub.h
405  *
406  * RETURNS
407  *   Success: S_OK.
408  *   Failure: E_FAIL.
409  *
410  * BUGS
411  *   Unimplemented.
412  */
413 HRESULT WINAPI RegSaveRestoreOnINFW(HWND hWnd, LPCWSTR pszTitle, LPCWSTR pszINF,
414                                     LPCWSTR pszSection, HKEY hHKLMBackKey,
415                                     HKEY hHKCUBackKey, DWORD dwFlags)
416 {
417     FIXME("(%p, %s, %s, %s, %p, %p, %ld): stub\n", hWnd, debugstr_w(pszTitle),
418           debugstr_w(pszINF), debugstr_w(pszSection),
419           hHKLMBackKey, hHKCUBackKey, dwFlags);
420
421     return E_FAIL;   
422 }