oleaut32: Don't clear BSTR size.
[wine.git] / programs / reg / reg.c
blob00eaab016f5496c64ed0a6d0bf9b2b64b6068c1f
1 /*
2 * Copyright 2008 Andrew Riedi
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <windows.h>
20 #include <wine/unicode.h>
21 #include "reg.h"
23 #define ARRAY_SIZE(A) (sizeof(A)/sizeof(*A))
25 static const WCHAR short_hklm[] = {'H','K','L','M',0};
26 static const WCHAR short_hkcu[] = {'H','K','C','U',0};
27 static const WCHAR short_hkcr[] = {'H','K','C','R',0};
28 static const WCHAR short_hku[] = {'H','K','U',0};
29 static const WCHAR short_hkcc[] = {'H','K','C','C',0};
30 static const WCHAR long_hklm[] = {'H','K','E','Y','_','L','O','C','A','L','_','M','A','C','H','I','N','E',0};
31 static const WCHAR long_hkcu[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','U','S','E','R',0};
32 static const WCHAR long_hkcr[] = {'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T',0};
33 static const WCHAR long_hku[] = {'H','K','E','Y','_','U','S','E','R','S',0};
34 static const WCHAR long_hkcc[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','C','O','N','F','I','G',0};
36 static const struct
38 HKEY key;
39 const WCHAR *short_name;
40 const WCHAR *long_name;
42 root_rels[] =
44 {HKEY_LOCAL_MACHINE, short_hklm, long_hklm},
45 {HKEY_CURRENT_USER, short_hkcu, long_hkcu},
46 {HKEY_CLASSES_ROOT, short_hkcr, long_hkcr},
47 {HKEY_USERS, short_hku, long_hku},
48 {HKEY_CURRENT_CONFIG, short_hkcc, long_hkcc},
51 static const WCHAR type_none[] = {'R','E','G','_','N','O','N','E',0};
52 static const WCHAR type_sz[] = {'R','E','G','_','S','Z',0};
53 static const WCHAR type_expand_sz[] = {'R','E','G','_','E','X','P','A','N','D','_','S','Z',0};
54 static const WCHAR type_binary[] = {'R','E','G','_','B','I','N','A','R','Y',0};
55 static const WCHAR type_dword[] = {'R','E','G','_','D','W','O','R','D',0};
56 static const WCHAR type_dword_le[] = {'R','E','G','_','D','W','O','R','D','_','L','I','T','T','L','E','_','E','N','D','I','A','N',0};
57 static const WCHAR type_dword_be[] = {'R','E','G','_','D','W','O','R','D','_','B','I','G','_','E','N','D','I','A','N',0};
58 static const WCHAR type_multi_sz[] = {'R','E','G','_','M','U','L','T','I','_','S','Z',0};
60 static const struct
62 DWORD type;
63 const WCHAR *name;
65 type_rels[] =
67 {REG_NONE, type_none},
68 {REG_SZ, type_sz},
69 {REG_EXPAND_SZ, type_expand_sz},
70 {REG_BINARY, type_binary},
71 {REG_DWORD, type_dword},
72 {REG_DWORD_LITTLE_ENDIAN, type_dword_le},
73 {REG_DWORD_BIG_ENDIAN, type_dword_be},
74 {REG_MULTI_SZ, type_multi_sz},
77 static int reg_printfW(const WCHAR *msg, ...)
79 va_list va_args;
80 int wlen;
81 DWORD count, ret;
82 WCHAR msg_buffer[8192];
84 va_start(va_args, msg);
85 vsnprintfW(msg_buffer, 8192, msg, va_args);
86 va_end(va_args);
88 wlen = lstrlenW(msg_buffer);
89 ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), msg_buffer, wlen, &count, NULL);
90 if (!ret)
92 DWORD len;
93 char *msgA;
95 /* On Windows WriteConsoleW() fails if the output is redirected. So fall
96 * back to WriteFile(), assuming the console encoding is still the right
97 * one in that case.
99 len = WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer, wlen,
100 NULL, 0, NULL, NULL);
101 msgA = HeapAlloc(GetProcessHeap(), 0, len * sizeof(char));
102 if (!msgA)
103 return 0;
105 WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer, wlen, msgA, len,
106 NULL, NULL);
107 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE);
108 HeapFree(GetProcessHeap(), 0, msgA);
111 return count;
114 static int reg_message(int msg)
116 static const WCHAR formatW[] = {'%','s',0};
117 WCHAR msg_buffer[8192];
119 LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer,
120 sizeof(msg_buffer)/sizeof(WCHAR));
121 return reg_printfW(formatW, msg_buffer);
124 static inline BOOL path_rootname_cmp(const WCHAR *input_path, const WCHAR *rootkey_name)
126 DWORD length = strlenW(rootkey_name);
128 return (!strncmpiW(input_path, rootkey_name, length) &&
129 (input_path[length] == 0 || input_path[length] == '\\'));
132 static HKEY path_get_rootkey(const WCHAR *path)
134 DWORD i;
136 for (i = 0; i < ARRAY_SIZE(root_rels); i++)
138 if (path_rootname_cmp(path, root_rels[i].short_name) ||
139 path_rootname_cmp(path, root_rels[i].long_name))
140 return root_rels[i].key;
143 return NULL;
146 static DWORD wchar_get_type(const WCHAR *type_name)
148 DWORD i;
150 if (!type_name)
151 return REG_SZ;
153 for (i = 0; i < ARRAY_SIZE(type_rels); i++)
155 if (!strcmpiW(type_rels[i].name, type_name))
156 return type_rels[i].type;
159 return ~0u;
162 /* hexchar_to_byte from programs/regedit/hexedit.c */
163 static inline BYTE hexchar_to_byte(WCHAR ch)
165 if (ch >= '0' && ch <= '9')
166 return ch - '0';
167 else if (ch >= 'a' && ch <= 'f')
168 return ch - 'a' + 10;
169 else if (ch >= 'A' && ch <= 'F')
170 return ch - 'A' + 10;
171 else
172 return -1;
175 static LPBYTE get_regdata(LPWSTR data, DWORD reg_type, WCHAR separator, DWORD *reg_count)
177 LPBYTE out_data = NULL;
178 *reg_count = 0;
180 switch (reg_type)
182 case REG_SZ:
184 *reg_count = (lstrlenW(data) + 1) * sizeof(WCHAR);
185 out_data = HeapAlloc(GetProcessHeap(),0,*reg_count);
186 lstrcpyW((LPWSTR)out_data,data);
187 break;
189 case REG_DWORD:
191 LPWSTR rest;
192 DWORD val;
193 val = strtolW(data, &rest, 0);
194 if (rest == data) {
195 static const WCHAR nonnumber[] = {'E','r','r','o','r',':',' ','/','d',' ','r','e','q','u','i','r','e','s',' ','n','u','m','b','e','r','.','\n',0};
196 reg_printfW(nonnumber);
197 break;
199 *reg_count = sizeof(DWORD);
200 out_data = HeapAlloc(GetProcessHeap(),0,*reg_count);
201 ((LPDWORD)out_data)[0] = val;
202 break;
204 case REG_BINARY:
206 static const WCHAR nohex[] = {'E','r','r','o','r',':',' ','/','d',' ','r','e','q','u','i','r','e','s',' ','h','e','x',' ','d','a','t','a','.','\n',0};
207 BYTE hex0, hex1;
208 int i = 0, destByteIndex = 0, datalen = lstrlenW(data);
209 *reg_count = ((datalen + datalen % 2) / 2) * sizeof(BYTE);
210 out_data = HeapAlloc(GetProcessHeap(), 0, *reg_count);
211 if(datalen % 2)
213 hex1 = hexchar_to_byte(data[i++]);
214 if(hex1 == 0xFF)
215 goto no_hex_data;
216 out_data[destByteIndex++] = hex1;
218 for(;i + 1 < datalen;i += 2)
220 hex0 = hexchar_to_byte(data[i]);
221 hex1 = hexchar_to_byte(data[i + 1]);
222 if(hex0 == 0xFF || hex1 == 0xFF)
223 goto no_hex_data;
224 out_data[destByteIndex++] = (hex0 << 4) | hex1;
226 break;
227 no_hex_data:
228 /* cleanup, print error */
229 HeapFree(GetProcessHeap(), 0, out_data);
230 reg_printfW(nohex);
231 out_data = NULL;
232 break;
234 default:
236 static const WCHAR unhandled[] = {'U','n','h','a','n','d','l','e','d',' ','T','y','p','e',' ','0','x','%','x',' ',' ','d','a','t','a',' ','%','s','\n',0};
237 reg_printfW(unhandled, reg_type,data);
241 return out_data;
244 static BOOL sane_path(const WCHAR *key)
246 unsigned int i = strlenW(key);
248 if (i < 3 || (key[i - 1] == '\\' && key[i - 2] == '\\'))
250 reg_message(STRING_INVALID_KEY);
251 return FALSE;
254 if (key[0] == '\\' && key[1] == '\\' && key[2] != '\\')
256 reg_message(STRING_NO_REMOTE);
257 return FALSE;
260 return TRUE;
263 static int reg_add(WCHAR *key_name, WCHAR *value_name, BOOL value_empty,
264 WCHAR *type, WCHAR separator, WCHAR *data, BOOL force)
266 static const WCHAR stubW[] = {'A','D','D',' ','-',' ','%','s',
267 ' ','%','s',' ','%','d',' ','%','s',' ','%','s',' ','%','d','\n',0};
268 LPWSTR p;
269 HKEY root,subkey;
271 reg_printfW(stubW, key_name, value_name, value_empty, type, data, force);
273 if (!sane_path(key_name))
274 return 1;
276 p = strchrW(key_name,'\\');
277 if (!p)
279 reg_message(STRING_INVALID_KEY);
280 return 1;
282 p++;
284 root = path_get_rootkey(key_name);
285 if (!root)
287 reg_message(STRING_INVALID_KEY);
288 return 1;
291 if(RegCreateKeyW(root,p,&subkey)!=ERROR_SUCCESS)
293 reg_message(STRING_INVALID_KEY);
294 return 1;
297 if (value_name || data)
299 DWORD reg_type;
300 DWORD reg_count = 0;
301 BYTE* reg_data = NULL;
303 if (!force)
305 if (RegQueryValueW(subkey,value_name,NULL,NULL)==ERROR_SUCCESS)
307 /* FIXME: Prompt for overwrite */
311 reg_type = wchar_get_type(type);
312 if (reg_type == ~0u)
314 RegCloseKey(subkey);
315 reg_message(STRING_UNSUPPORTED_TYPE);
316 return 1;
319 if (data)
320 reg_data = get_regdata(data,reg_type,separator,&reg_count);
322 RegSetValueExW(subkey,value_name,0,reg_type,reg_data,reg_count);
323 HeapFree(GetProcessHeap(),0,reg_data);
326 RegCloseKey(subkey);
327 reg_message(STRING_SUCCESS);
329 return 0;
332 static int reg_delete(WCHAR *key_name, WCHAR *value_name, BOOL value_empty,
333 BOOL value_all, BOOL force)
335 LPWSTR p;
336 HKEY root,subkey;
338 static const WCHAR stubW[] = {'D','E','L','E','T','E',
339 ' ','-',' ','%','s',' ','%','s',' ','%','d',' ','%','d',' ','%','d','\n'
340 ,0};
341 reg_printfW(stubW, key_name, value_name, value_empty, value_all, force);
343 if (!sane_path(key_name))
344 return 1;
346 p = strchrW(key_name,'\\');
347 if (!p)
349 reg_message(STRING_INVALID_KEY);
350 return 1;
352 p++;
354 root = path_get_rootkey(key_name);
355 if (!root)
357 reg_message(STRING_INVALID_KEY);
358 return 1;
361 if (value_name && value_empty)
363 reg_message(STRING_INVALID_CMDLINE);
364 return 1;
367 if (value_empty && value_all)
369 reg_message(STRING_INVALID_CMDLINE);
370 return 1;
373 if (!force)
375 /* FIXME: Prompt for delete */
378 /* Delete subtree only if no /v* option is given */
379 if (!value_name && !value_empty && !value_all)
381 if (RegDeleteTreeW(root,p)!=ERROR_SUCCESS)
383 reg_message(STRING_CANNOT_FIND);
384 return 1;
386 reg_message(STRING_SUCCESS);
387 return 0;
390 if(RegOpenKeyW(root,p,&subkey)!=ERROR_SUCCESS)
392 reg_message(STRING_CANNOT_FIND);
393 return 1;
396 if (value_all)
398 LPWSTR szValue;
399 DWORD maxValue;
400 DWORD count;
401 LONG rc;
403 rc = RegQueryInfoKeyW(subkey, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
404 &maxValue, NULL, NULL, NULL);
405 if (rc != ERROR_SUCCESS)
407 /* FIXME: failure */
408 RegCloseKey(subkey);
409 return 1;
411 maxValue++;
412 szValue = HeapAlloc(GetProcessHeap(),0,maxValue*sizeof(WCHAR));
414 while (1)
416 count = maxValue;
417 rc = RegEnumValueW(subkey, 0, szValue, &count, NULL, NULL, NULL, NULL);
418 if (rc == ERROR_SUCCESS)
420 rc = RegDeleteValueW(subkey, szValue);
421 if (rc != ERROR_SUCCESS)
422 break;
424 else break;
426 if (rc != ERROR_SUCCESS)
428 /* FIXME delete failed */
431 else if (value_name)
433 if (RegDeleteValueW(subkey,value_name) != ERROR_SUCCESS)
435 RegCloseKey(subkey);
436 reg_message(STRING_CANNOT_FIND);
437 return 1;
440 else if (value_empty)
442 RegSetValueExW(subkey,NULL,0,REG_SZ,NULL,0);
445 RegCloseKey(subkey);
446 reg_message(STRING_SUCCESS);
447 return 0;
450 static int reg_query(WCHAR *key_name, WCHAR *value_name, BOOL value_empty,
451 BOOL subkey)
453 static const WCHAR stubW[] = {'S','T','U','B',' ','Q','U','E','R','Y',' ',
454 '-',' ','%','s',' ','%','s',' ','%','d',' ','%','d','\n',0};
455 reg_printfW(stubW, key_name, value_name, value_empty, subkey);
457 return 1;
460 int wmain(int argc, WCHAR *argvW[])
462 int i;
464 static const WCHAR addW[] = {'a','d','d',0};
465 static const WCHAR deleteW[] = {'d','e','l','e','t','e',0};
466 static const WCHAR queryW[] = {'q','u','e','r','y',0};
467 static const WCHAR slashDW[] = {'/','d',0};
468 static const WCHAR slashFW[] = {'/','f',0};
469 static const WCHAR slashHW[] = {'/','h',0};
470 static const WCHAR slashSW[] = {'/','s',0};
471 static const WCHAR slashTW[] = {'/','t',0};
472 static const WCHAR slashVW[] = {'/','v',0};
473 static const WCHAR slashVAW[] = {'/','v','a',0};
474 static const WCHAR slashVEW[] = {'/','v','e',0};
475 static const WCHAR slashHelpW[] = {'/','?',0};
477 if (argc < 2 || !lstrcmpW(argvW[1], slashHelpW)
478 || !lstrcmpiW(argvW[1], slashHW))
480 reg_message(STRING_USAGE);
481 return 0;
484 if (!lstrcmpiW(argvW[1], addW))
486 WCHAR *key_name, *value_name = NULL, *type = NULL, separator = '\0', *data = NULL;
487 BOOL value_empty = FALSE, force = FALSE;
489 if (argc < 3)
491 reg_message(STRING_INVALID_CMDLINE);
492 return 1;
494 else if (argc == 3 && (!lstrcmpW(argvW[2], slashHelpW) ||
495 !lstrcmpiW(argvW[2], slashHW)))
497 reg_message(STRING_ADD_USAGE);
498 return 0;
501 key_name = argvW[2];
502 for (i = 1; i < argc; i++)
504 if (!lstrcmpiW(argvW[i], slashVW))
505 value_name = argvW[++i];
506 else if (!lstrcmpiW(argvW[i], slashVEW))
507 value_empty = TRUE;
508 else if (!lstrcmpiW(argvW[i], slashTW))
509 type = argvW[++i];
510 else if (!lstrcmpiW(argvW[i], slashSW))
511 separator = argvW[++i][0];
512 else if (!lstrcmpiW(argvW[i], slashDW))
513 data = argvW[++i];
514 else if (!lstrcmpiW(argvW[i], slashFW))
515 force = TRUE;
517 return reg_add(key_name, value_name, value_empty, type, separator,
518 data, force);
520 else if (!lstrcmpiW(argvW[1], deleteW))
522 WCHAR *key_name, *value_name = NULL;
523 BOOL value_empty = FALSE, value_all = FALSE, force = FALSE;
525 if (argc < 3)
527 reg_message(STRING_INVALID_CMDLINE);
528 return 1;
530 else if (argc == 3 && (!lstrcmpW(argvW[2], slashHelpW) ||
531 !lstrcmpiW(argvW[2], slashHW)))
533 reg_message(STRING_DELETE_USAGE);
534 return 0;
537 key_name = argvW[2];
538 for (i = 1; i < argc; i++)
540 if (!lstrcmpiW(argvW[i], slashVW))
541 value_name = argvW[++i];
542 else if (!lstrcmpiW(argvW[i], slashVEW))
543 value_empty = TRUE;
544 else if (!lstrcmpiW(argvW[i], slashVAW))
545 value_all = TRUE;
546 else if (!lstrcmpiW(argvW[i], slashFW))
547 force = TRUE;
549 return reg_delete(key_name, value_name, value_empty, value_all, force);
551 else if (!lstrcmpiW(argvW[1], queryW))
553 WCHAR *key_name, *value_name = NULL;
554 BOOL value_empty = FALSE, subkey = FALSE;
556 if (argc < 3)
558 reg_message(STRING_INVALID_CMDLINE);
559 return 1;
561 else if (argc == 3 && (!lstrcmpW(argvW[2], slashHelpW) ||
562 !lstrcmpiW(argvW[2], slashHW)))
564 reg_message(STRING_QUERY_USAGE);
565 return 0;
568 key_name = argvW[2];
569 for (i = 1; i < argc; i++)
571 if (!lstrcmpiW(argvW[i], slashVW))
572 value_name = argvW[++i];
573 else if (!lstrcmpiW(argvW[i], slashVEW))
574 value_empty = TRUE;
575 else if (!lstrcmpiW(argvW[i], slashSW))
576 subkey = TRUE;
578 return reg_query(key_name, value_name, value_empty, subkey);
580 else
582 reg_message(STRING_INVALID_CMDLINE);
583 return 1;