Git conversion: Make reactos the root directory, move rosapps, rostests, wallpapers...
[reactos.git] / base / applications / cmdutils / reg / reg.c
1 /*
2 * Copyright 2008 Andrew Riedi
3 *
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.
8 *
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.
13 *
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
17 */
18
19 #include <stdarg.h>
20 #include <windef.h>
21 #include <winbase.h>
22 #include <winuser.h>
23 #include <winreg.h>
24 #include <wincon.h>
25 #include <shlwapi.h>
26 #include <wine/unicode.h>
27 #include <wine/debug.h>
28 #include "reg.h"
29
30 WINE_DEFAULT_DEBUG_CHANNEL(reg);
31
32 static const WCHAR short_hklm[] = {'H','K','L','M',0};
33 static const WCHAR short_hkcu[] = {'H','K','C','U',0};
34 static const WCHAR short_hkcr[] = {'H','K','C','R',0};
35 static const WCHAR short_hku[] = {'H','K','U',0};
36 static const WCHAR short_hkcc[] = {'H','K','C','C',0};
37 static const WCHAR long_hklm[] = {'H','K','E','Y','_','L','O','C','A','L','_','M','A','C','H','I','N','E',0};
38 static const WCHAR long_hkcu[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','U','S','E','R',0};
39 static const WCHAR long_hkcr[] = {'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T',0};
40 static const WCHAR long_hku[] = {'H','K','E','Y','_','U','S','E','R','S',0};
41 static const WCHAR long_hkcc[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','C','O','N','F','I','G',0};
42
43 static const struct
44 {
45 HKEY key;
46 const WCHAR *short_name;
47 const WCHAR *long_name;
48 }
49 root_rels[] =
50 {
51 {HKEY_LOCAL_MACHINE, short_hklm, long_hklm},
52 {HKEY_CURRENT_USER, short_hkcu, long_hkcu},
53 {HKEY_CLASSES_ROOT, short_hkcr, long_hkcr},
54 {HKEY_USERS, short_hku, long_hku},
55 {HKEY_CURRENT_CONFIG, short_hkcc, long_hkcc},
56 };
57
58 static const WCHAR type_none[] = {'R','E','G','_','N','O','N','E',0};
59 static const WCHAR type_sz[] = {'R','E','G','_','S','Z',0};
60 static const WCHAR type_expand_sz[] = {'R','E','G','_','E','X','P','A','N','D','_','S','Z',0};
61 static const WCHAR type_binary[] = {'R','E','G','_','B','I','N','A','R','Y',0};
62 static const WCHAR type_dword[] = {'R','E','G','_','D','W','O','R','D',0};
63 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};
64 static const WCHAR type_dword_be[] = {'R','E','G','_','D','W','O','R','D','_','B','I','G','_','E','N','D','I','A','N',0};
65 static const WCHAR type_multi_sz[] = {'R','E','G','_','M','U','L','T','I','_','S','Z',0};
66
67 static const struct
68 {
69 DWORD type;
70 const WCHAR *name;
71 }
72 type_rels[] =
73 {
74 {REG_NONE, type_none},
75 {REG_SZ, type_sz},
76 {REG_EXPAND_SZ, type_expand_sz},
77 {REG_BINARY, type_binary},
78 {REG_DWORD, type_dword},
79 {REG_DWORD_LITTLE_ENDIAN, type_dword_le},
80 {REG_DWORD_BIG_ENDIAN, type_dword_be},
81 {REG_MULTI_SZ, type_multi_sz},
82 };
83
84 void *heap_xalloc(size_t size)
85 {
86 void *buf = HeapAlloc(GetProcessHeap(), 0, size);
87 if (!buf)
88 {
89 ERR("Out of memory!\n");
90 exit(1);
91 }
92 return buf;
93 }
94
95 void *heap_xrealloc(void *buf, size_t size)
96 {
97 void *new_buf;
98
99 if (buf)
100 new_buf = HeapReAlloc(GetProcessHeap(), 0, buf, size);
101 else
102 new_buf = HeapAlloc(GetProcessHeap(), 0, size);
103
104 if (!new_buf)
105 {
106 ERR("Out of memory!\n");
107 exit(1);
108 }
109
110 return new_buf;
111 }
112
113 BOOL heap_free(void *buf)
114 {
115 return HeapFree(GetProcessHeap(), 0, buf);
116 }
117
118 static void output_writeconsole(const WCHAR *str, DWORD wlen)
119 {
120 DWORD count, ret;
121
122 ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), str, wlen, &count, NULL);
123 if (!ret)
124 {
125 DWORD len;
126 char *msgA;
127
128 /* On Windows WriteConsoleW() fails if the output is redirected. So fall
129 * back to WriteFile(), assuming the console encoding is still the right
130 * one in that case.
131 */
132 len = WideCharToMultiByte(GetConsoleOutputCP(), 0, str, wlen, NULL, 0, NULL, NULL);
133 msgA = heap_xalloc(len);
134
135 WideCharToMultiByte(GetConsoleOutputCP(), 0, str, wlen, msgA, len, NULL, NULL);
136 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE);
137 heap_free(msgA);
138 }
139 }
140
141 static void output_formatstring(const WCHAR *fmt, __ms_va_list va_args)
142 {
143 WCHAR *str;
144 DWORD len;
145
146 SetLastError(NO_ERROR);
147 len = FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER,
148 fmt, 0, 0, (WCHAR *)&str, 0, &va_args);
149 if (len == 0 && GetLastError() != NO_ERROR)
150 {
151 WINE_FIXME("Could not format string: le=%u, fmt=%s\n", GetLastError(), wine_dbgstr_w(fmt));
152 return;
153 }
154 output_writeconsole(str, len);
155 LocalFree(str);
156 }
157
158 void __cdecl output_message(unsigned int id, ...)
159 {
160 WCHAR fmt[1024];
161 __ms_va_list va_args;
162
163 if (!LoadStringW(GetModuleHandleW(NULL), id, fmt, ARRAY_SIZE(fmt)))
164 {
165 WINE_FIXME("LoadString failed with %d\n", GetLastError());
166 return;
167 }
168 __ms_va_start(va_args, id);
169 output_formatstring(fmt, va_args);
170 __ms_va_end(va_args);
171 }
172
173 static void __cdecl output_string(const WCHAR *fmt, ...)
174 {
175 __ms_va_list va_args;
176
177 __ms_va_start(va_args, fmt);
178 output_formatstring(fmt, va_args);
179 __ms_va_end(va_args);
180 }
181
182 /* ask_confirm() adapted from programs/cmd/builtins.c */
183 static BOOL ask_confirm(unsigned int msgid, WCHAR *reg_info)
184 {
185 HMODULE hmod;
186 WCHAR Ybuffer[4];
187 WCHAR Nbuffer[4];
188 WCHAR defval[32];
189 WCHAR answer[MAX_PATH];
190 WCHAR *str;
191 DWORD count;
192
193 hmod = GetModuleHandleW(NULL);
194 LoadStringW(hmod, STRING_YES, Ybuffer, ARRAY_SIZE(Ybuffer));
195 LoadStringW(hmod, STRING_NO, Nbuffer, ARRAY_SIZE(Nbuffer));
196 LoadStringW(hmod, STRING_DEFAULT_VALUE, defval, ARRAY_SIZE(defval));
197
198 str = (reg_info && *reg_info) ? reg_info : defval;
199
200 while (1)
201 {
202 output_message(msgid, str);
203 output_message(STRING_YESNO);
204 ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE), answer, ARRAY_SIZE(answer), &count, NULL);
205 answer[0] = toupperW(answer[0]);
206 if (answer[0] == Ybuffer[0])
207 return TRUE;
208 if (answer[0] == Nbuffer[0])
209 return FALSE;
210 }
211 }
212
213 static inline BOOL path_rootname_cmp(const WCHAR *input_path, const WCHAR *rootkey_name)
214 {
215 DWORD length = strlenW(rootkey_name);
216
217 return (!strncmpiW(input_path, rootkey_name, length) &&
218 (input_path[length] == 0 || input_path[length] == '\\'));
219 }
220
221 HKEY path_get_rootkey(const WCHAR *path)
222 {
223 DWORD i;
224
225 for (i = 0; i < ARRAY_SIZE(root_rels); i++)
226 {
227 if (path_rootname_cmp(path, root_rels[i].short_name) ||
228 path_rootname_cmp(path, root_rels[i].long_name))
229 return root_rels[i].key;
230 }
231
232 return NULL;
233 }
234
235 static DWORD wchar_get_type(const WCHAR *type_name)
236 {
237 DWORD i;
238
239 if (!type_name)
240 return REG_SZ;
241
242 for (i = 0; i < ARRAY_SIZE(type_rels); i++)
243 {
244 if (!strcmpiW(type_rels[i].name, type_name))
245 return type_rels[i].type;
246 }
247
248 return ~0u;
249 }
250
251 /* hexchar_to_byte from programs/regedit/hexedit.c */
252 static inline BYTE hexchar_to_byte(WCHAR ch)
253 {
254 if (ch >= '0' && ch <= '9')
255 return ch - '0';
256 else if (ch >= 'a' && ch <= 'f')
257 return ch - 'a' + 10;
258 else if (ch >= 'A' && ch <= 'F')
259 return ch - 'A' + 10;
260 else
261 return -1;
262 }
263
264 static LPBYTE get_regdata(const WCHAR *data, DWORD reg_type, WCHAR separator, DWORD *reg_count)
265 {
266 static const WCHAR empty;
267 LPBYTE out_data = NULL;
268 *reg_count = 0;
269
270 if (!data) data = &empty;
271
272 switch (reg_type)
273 {
274 case REG_NONE:
275 case REG_SZ:
276 case REG_EXPAND_SZ:
277 {
278 *reg_count = (lstrlenW(data) + 1) * sizeof(WCHAR);
279 out_data = heap_xalloc(*reg_count);
280 lstrcpyW((LPWSTR)out_data,data);
281 break;
282 }
283 case REG_DWORD:
284 /* case REG_DWORD_LITTLE_ENDIAN: */
285 case REG_DWORD_BIG_ENDIAN: /* Yes, this is correct! */
286 {
287 LPWSTR rest;
288 unsigned long val;
289 val = wcstoul(data, &rest, (tolowerW(data[1]) == 'x') ? 16 : 10);
290 if (*rest || data[0] == '-' || (val == ~0u && errno == ERANGE)) {
291 output_message(STRING_MISSING_INTEGER);
292 break;
293 }
294 *reg_count = sizeof(DWORD);
295 out_data = heap_xalloc(*reg_count);
296 ((LPDWORD)out_data)[0] = val;
297 break;
298 }
299 case REG_BINARY:
300 {
301 BYTE hex0, hex1;
302 int i = 0, destByteIndex = 0, datalen = lstrlenW(data);
303 *reg_count = ((datalen + datalen % 2) / 2) * sizeof(BYTE);
304 out_data = heap_xalloc(*reg_count);
305 if(datalen % 2)
306 {
307 hex1 = hexchar_to_byte(data[i++]);
308 if(hex1 == 0xFF)
309 goto no_hex_data;
310 out_data[destByteIndex++] = hex1;
311 }
312 for(;i + 1 < datalen;i += 2)
313 {
314 hex0 = hexchar_to_byte(data[i]);
315 hex1 = hexchar_to_byte(data[i + 1]);
316 if(hex0 == 0xFF || hex1 == 0xFF)
317 goto no_hex_data;
318 out_data[destByteIndex++] = (hex0 << 4) | hex1;
319 }
320 break;
321 no_hex_data:
322 /* cleanup, print error */
323 heap_free(out_data);
324 output_message(STRING_MISSING_HEXDATA);
325 out_data = NULL;
326 break;
327 }
328 case REG_MULTI_SZ:
329 {
330 int i, destindex, len = strlenW(data);
331 WCHAR *buffer = heap_xalloc((len + 2) * sizeof(WCHAR));
332
333 for (i = 0, destindex = 0; i < len; i++, destindex++)
334 {
335 if (!separator && data[i] == '\\' && data[i + 1] == '0')
336 {
337 buffer[destindex] = 0;
338 i++;
339 }
340 else if (data[i] == separator)
341 buffer[destindex] = 0;
342 else
343 buffer[destindex] = data[i];
344
345 if (destindex && !buffer[destindex - 1] && (!buffer[destindex] || destindex == 1))
346 {
347 heap_free(buffer);
348 output_message(STRING_INVALID_STRING);
349 return NULL;
350 }
351 }
352 buffer[destindex] = 0;
353 if (destindex && buffer[destindex - 1])
354 buffer[++destindex] = 0;
355 *reg_count = (destindex + 1) * sizeof(WCHAR);
356 return (BYTE *)buffer;
357 }
358 default:
359 output_message(STRING_UNHANDLED_TYPE, reg_type, data);
360 }
361
362 return out_data;
363 }
364
365 static BOOL sane_path(const WCHAR *key)
366 {
367 unsigned int i = strlenW(key);
368
369 if (i < 3 || (key[i - 1] == '\\' && key[i - 2] == '\\'))
370 {
371 output_message(STRING_INVALID_KEY);
372 return FALSE;
373 }
374
375 if (key[0] == '\\' && key[1] == '\\' && key[2] != '\\')
376 {
377 output_message(STRING_NO_REMOTE);
378 return FALSE;
379 }
380
381 return TRUE;
382 }
383
384 static int reg_add(HKEY root, WCHAR *path, WCHAR *value_name, BOOL value_empty,
385 WCHAR *type, WCHAR separator, WCHAR *data, BOOL force)
386 {
387 HKEY key;
388
389 if (RegCreateKeyW(root, path, &key) != ERROR_SUCCESS)
390 {
391 output_message(STRING_INVALID_KEY);
392 return 1;
393 }
394
395 if (value_name || value_empty || data)
396 {
397 DWORD reg_type;
398 DWORD reg_count = 0;
399 BYTE* reg_data = NULL;
400
401 if (!force)
402 {
403 if (RegQueryValueExW(key, value_name, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
404 {
405 if (!ask_confirm(STRING_OVERWRITE_VALUE, value_name))
406 {
407 RegCloseKey(key);
408 output_message(STRING_CANCELLED);
409 return 0;
410 }
411 }
412 }
413
414 reg_type = wchar_get_type(type);
415 if (reg_type == ~0u)
416 {
417 RegCloseKey(key);
418 output_message(STRING_UNSUPPORTED_TYPE, type);
419 return 1;
420 }
421 if ((reg_type == REG_DWORD || reg_type == REG_DWORD_BIG_ENDIAN) && !data)
422 {
423 RegCloseKey(key);
424 output_message(STRING_INVALID_CMDLINE);
425 return 1;
426 }
427
428 if (!(reg_data = get_regdata(data, reg_type, separator, &reg_count)))
429 {
430 RegCloseKey(key);
431 return 1;
432 }
433
434 RegSetValueExW(key, value_name, 0, reg_type, reg_data, reg_count);
435 heap_free(reg_data);
436 }
437
438 RegCloseKey(key);
439 output_message(STRING_SUCCESS);
440
441 return 0;
442 }
443
444 static int reg_delete(HKEY root, WCHAR *path, WCHAR *key_name, WCHAR *value_name,
445 BOOL value_empty, BOOL value_all, BOOL force)
446 {
447 HKEY key;
448
449 if (!force)
450 {
451 BOOL ret;
452
453 if (value_name || value_empty)
454 ret = ask_confirm(STRING_DELETE_VALUE, value_name);
455 else if (value_all)
456 ret = ask_confirm(STRING_DELETE_VALUEALL, key_name);
457 else
458 ret = ask_confirm(STRING_DELETE_SUBKEY, key_name);
459
460 if (!ret)
461 {
462 output_message(STRING_CANCELLED);
463 return 0;
464 }
465 }
466
467 /* Delete subtree only if no /v* option is given */
468 if (!value_name && !value_empty && !value_all)
469 {
470 if (RegDeleteTreeW(root, path) != ERROR_SUCCESS)
471 {
472 output_message(STRING_CANNOT_FIND);
473 return 1;
474 }
475 output_message(STRING_SUCCESS);
476 return 0;
477 }
478
479 if (RegOpenKeyW(root, path, &key) != ERROR_SUCCESS)
480 {
481 output_message(STRING_CANNOT_FIND);
482 return 1;
483 }
484
485 if (value_all)
486 {
487 DWORD max_value_len = 256, value_len;
488 WCHAR *value_name;
489 LONG rc;
490
491 value_name = heap_xalloc(max_value_len * sizeof(WCHAR));
492
493 while (1)
494 {
495 value_len = max_value_len;
496 rc = RegEnumValueW(key, 0, value_name, &value_len, NULL, NULL, NULL, NULL);
497 if (rc == ERROR_SUCCESS)
498 {
499 rc = RegDeleteValueW(key, value_name);
500 if (rc != ERROR_SUCCESS)
501 {
502 heap_free(value_name);
503 RegCloseKey(key);
504 output_message(STRING_VALUEALL_FAILED, key_name);
505 return 1;
506 }
507 }
508 else if (rc == ERROR_MORE_DATA)
509 {
510 max_value_len *= 2;
511 value_name = heap_xrealloc(value_name, max_value_len * sizeof(WCHAR));
512 }
513 else break;
514 }
515 heap_free(value_name);
516 }
517 else if (value_name || value_empty)
518 {
519 if (RegDeleteValueW(key, value_empty ? NULL : value_name) != ERROR_SUCCESS)
520 {
521 RegCloseKey(key);
522 output_message(STRING_CANNOT_FIND);
523 return 1;
524 }
525 }
526
527 RegCloseKey(key);
528 output_message(STRING_SUCCESS);
529 return 0;
530 }
531
532 static WCHAR *reg_data_to_wchar(DWORD type, const BYTE *src, DWORD size_bytes)
533 {
534 WCHAR *buffer = NULL;
535 int i;
536
537 switch (type)
538 {
539 case REG_SZ:
540 case REG_EXPAND_SZ:
541 buffer = heap_xalloc(size_bytes);
542 strcpyW(buffer, (WCHAR *)src);
543 break;
544 case REG_NONE:
545 case REG_BINARY:
546 {
547 WCHAR *ptr;
548 static const WCHAR fmt[] = {'%','0','2','X',0};
549
550 buffer = heap_xalloc((size_bytes * 2 + 1) * sizeof(WCHAR));
551 ptr = buffer;
552 for (i = 0; i < size_bytes; i++)
553 ptr += sprintfW(ptr, fmt, src[i]);
554 break;
555 }
556 case REG_DWORD:
557 /* case REG_DWORD_LITTLE_ENDIAN: */
558 case REG_DWORD_BIG_ENDIAN:
559 {
560 const int zero_x_dword = 10;
561 static const WCHAR fmt[] = {'0','x','%','x',0};
562
563 buffer = heap_xalloc((zero_x_dword + 1) * sizeof(WCHAR));
564 sprintfW(buffer, fmt, *(DWORD *)src);
565 break;
566 }
567 case REG_MULTI_SZ:
568 {
569 const int two_wchars = 2 * sizeof(WCHAR);
570 DWORD tmp_size;
571 const WCHAR *tmp = (const WCHAR *)src;
572 int len, destindex;
573
574 if (size_bytes <= two_wchars)
575 {
576 buffer = heap_xalloc(sizeof(WCHAR));
577 *buffer = 0;
578 return buffer;
579 }
580
581 tmp_size = size_bytes - two_wchars; /* exclude both null terminators */
582 buffer = heap_xalloc(tmp_size * 2 + sizeof(WCHAR));
583 len = tmp_size / sizeof(WCHAR);
584
585 for (i = 0, destindex = 0; i < len; i++, destindex++)
586 {
587 if (tmp[i])
588 buffer[destindex] = tmp[i];
589 else
590 {
591 buffer[destindex++] = '\\';
592 buffer[destindex] = '0';
593 }
594 }
595 buffer[destindex] = 0;
596 break;
597 }
598 }
599 return buffer;
600 }
601
602 static const WCHAR *reg_type_to_wchar(DWORD type)
603 {
604 int i, array_size = ARRAY_SIZE(type_rels);
605
606 for (i = 0; i < array_size; i++)
607 {
608 if (type == type_rels[i].type)
609 return type_rels[i].name;
610 }
611 return NULL;
612 }
613
614 static void output_value(const WCHAR *value_name, DWORD type, BYTE *data, DWORD data_size)
615 {
616 static const WCHAR fmt[] = {' ',' ',' ',' ','%','1',0};
617 static const WCHAR newlineW[] = {'\n',0};
618 WCHAR defval[32];
619 WCHAR *reg_data;
620
621 if (value_name && value_name[0])
622 output_string(fmt, value_name);
623 else
624 {
625 LoadStringW(GetModuleHandleW(NULL), STRING_DEFAULT_VALUE, defval, ARRAY_SIZE(defval));
626 output_string(fmt, defval);
627 }
628 output_string(fmt, reg_type_to_wchar(type));
629
630 if (data)
631 {
632 reg_data = reg_data_to_wchar(type, data, data_size);
633 output_string(fmt, reg_data);
634 heap_free(reg_data);
635 }
636 else
637 {
638 LoadStringW(GetModuleHandleW(NULL), STRING_VALUE_NOT_SET, defval, ARRAY_SIZE(defval));
639 output_string(fmt, defval);
640 }
641 output_string(newlineW);
642 }
643
644 static WCHAR *build_subkey_path(WCHAR *path, DWORD path_len, WCHAR *subkey_name, DWORD subkey_len)
645 {
646 WCHAR *subkey_path;
647 static const WCHAR fmt[] = {'%','s','\\','%','s',0};
648
649 subkey_path = heap_xalloc((path_len + subkey_len + 2) * sizeof(WCHAR));
650 sprintfW(subkey_path, fmt, path, subkey_name);
651
652 return subkey_path;
653 }
654
655 static unsigned int num_values_found = 0;
656
657 #define MAX_SUBKEY_LEN 257
658
659 static int query_value(HKEY key, WCHAR *value_name, WCHAR *path, BOOL recurse)
660 {
661 LONG rc;
662 DWORD max_data_bytes = 2048, data_size;
663 DWORD subkey_len;
664 DWORD type, path_len, i;
665 BYTE *data;
666 WCHAR fmt[] = {'%','1','\n',0};
667 WCHAR newlineW[] = {'\n',0};
668 WCHAR *subkey_name, *subkey_path;
669 HKEY subkey;
670
671 data = heap_xalloc(max_data_bytes);
672
673 for (;;)
674 {
675 data_size = max_data_bytes;
676 rc = RegQueryValueExW(key, value_name, NULL, &type, data, &data_size);
677 if (rc == ERROR_MORE_DATA)
678 {
679 max_data_bytes = data_size;
680 data = heap_xrealloc(data, max_data_bytes);
681 }
682 else break;
683 }
684
685 if (rc == ERROR_SUCCESS)
686 {
687 output_string(fmt, path);
688 output_value(value_name, type, data, data_size);
689 output_string(newlineW);
690 num_values_found++;
691 }
692
693 heap_free(data);
694
695 if (!recurse)
696 {
697 if (rc == ERROR_FILE_NOT_FOUND)
698 {
699 if (value_name && *value_name)
700 {
701 output_message(STRING_CANNOT_FIND);
702 return 1;
703 }
704 output_string(fmt, path);
705 output_value(NULL, REG_SZ, NULL, 0);
706 }
707 return 0;
708 }
709
710 subkey_name = heap_xalloc(MAX_SUBKEY_LEN * sizeof(WCHAR));
711
712 path_len = strlenW(path);
713
714 i = 0;
715 for (;;)
716 {
717 subkey_len = MAX_SUBKEY_LEN;
718 rc = RegEnumKeyExW(key, i, subkey_name, &subkey_len, NULL, NULL, NULL, NULL);
719 if (rc == ERROR_SUCCESS)
720 {
721 subkey_path = build_subkey_path(path, path_len, subkey_name, subkey_len);
722 if (!RegOpenKeyExW(key, subkey_name, 0, KEY_READ, &subkey))
723 {
724 query_value(subkey, value_name, subkey_path, recurse);
725 RegCloseKey(subkey);
726 }
727 heap_free(subkey_path);
728 i++;
729 }
730 else break;
731 }
732
733 heap_free(subkey_name);
734 return 0;
735 }
736
737 static int query_all(HKEY key, WCHAR *path, BOOL recurse)
738 {
739 LONG rc;
740 DWORD max_value_len = 256, value_len;
741 DWORD max_data_bytes = 2048, data_size;
742 DWORD subkey_len;
743 DWORD i, type, path_len;
744 WCHAR fmt[] = {'%','1','\n',0};
745 WCHAR fmt_path[] = {'%','1','\\','%','2','\n',0};
746 WCHAR *value_name, *subkey_name, *subkey_path;
747 WCHAR newlineW[] = {'\n',0};
748 BYTE *data;
749 HKEY subkey;
750
751 output_string(fmt, path);
752
753 value_name = heap_xalloc(max_value_len * sizeof(WCHAR));
754 data = heap_xalloc(max_data_bytes);
755
756 i = 0;
757 for (;;)
758 {
759 value_len = max_value_len;
760 data_size = max_data_bytes;
761 rc = RegEnumValueW(key, i, value_name, &value_len, NULL, &type, data, &data_size);
762 if (rc == ERROR_SUCCESS)
763 {
764 output_value(value_name, type, data, data_size);
765 i++;
766 }
767 else if (rc == ERROR_MORE_DATA)
768 {
769 if (data_size > max_data_bytes)
770 {
771 max_data_bytes = data_size;
772 data = heap_xrealloc(data, max_data_bytes);
773 }
774 else
775 {
776 max_value_len *= 2;
777 value_name = heap_xrealloc(value_name, max_value_len * sizeof(WCHAR));
778 }
779 }
780 else break;
781 }
782
783 heap_free(data);
784 heap_free(value_name);
785
786 if (i || recurse)
787 output_string(newlineW);
788
789 subkey_name = heap_xalloc(MAX_SUBKEY_LEN * sizeof(WCHAR));
790
791 path_len = strlenW(path);
792
793 i = 0;
794 for (;;)
795 {
796 subkey_len = MAX_SUBKEY_LEN;
797 rc = RegEnumKeyExW(key, i, subkey_name, &subkey_len, NULL, NULL, NULL, NULL);
798 if (rc == ERROR_SUCCESS)
799 {
800 if (recurse)
801 {
802 subkey_path = build_subkey_path(path, path_len, subkey_name, subkey_len);
803 if (!RegOpenKeyExW(key, subkey_name, 0, KEY_READ, &subkey))
804 {
805 query_all(subkey, subkey_path, recurse);
806 RegCloseKey(subkey);
807 }
808 heap_free(subkey_path);
809 }
810 else output_string(fmt_path, path, subkey_name);
811 i++;
812 }
813 else break;
814 }
815
816 heap_free(subkey_name);
817
818 if (i && !recurse)
819 output_string(newlineW);
820
821 return 0;
822 }
823
824 static int reg_query(HKEY root, WCHAR *path, WCHAR *key_name, WCHAR *value_name,
825 BOOL value_empty, BOOL recurse)
826 {
827 HKEY key;
828 WCHAR newlineW[] = {'\n',0};
829 int ret;
830
831 if (RegOpenKeyExW(root, path, 0, KEY_READ, &key) != ERROR_SUCCESS)
832 {
833 output_message(STRING_CANNOT_FIND);
834 return 1;
835 }
836
837 output_string(newlineW);
838
839 if (value_name || value_empty)
840 {
841 ret = query_value(key, value_name, key_name, recurse);
842 if (recurse)
843 output_message(STRING_MATCHES_FOUND, num_values_found);
844 }
845 else
846 ret = query_all(key, key_name, recurse);
847
848 RegCloseKey(key);
849
850 return ret;
851 }
852
853 static WCHAR *get_long_key(HKEY root, WCHAR *path)
854 {
855 DWORD i, array_size = ARRAY_SIZE(root_rels), len;
856 WCHAR *long_key;
857 WCHAR fmt[] = {'%','s','\\','%','s',0};
858
859 for (i = 0; i < array_size; i++)
860 {
861 if (root == root_rels[i].key)
862 break;
863 }
864
865 len = strlenW(root_rels[i].long_name);
866
867 if (!path)
868 {
869 long_key = heap_xalloc((len + 1) * sizeof(WCHAR));
870 strcpyW(long_key, root_rels[i].long_name);
871 return long_key;
872 }
873
874 len += strlenW(path) + 1; /* add one for the backslash */
875 long_key = heap_xalloc((len + 1) * sizeof(WCHAR));
876 sprintfW(long_key, fmt, root_rels[i].long_name, path);
877 return long_key;
878 }
879
880 static BOOL parse_registry_key(const WCHAR *key, HKEY *root, WCHAR **path, WCHAR **long_key)
881 {
882 if (!sane_path(key))
883 return FALSE;
884
885 *root = path_get_rootkey(key);
886 if (!*root)
887 {
888 output_message(STRING_INVALID_KEY);
889 return FALSE;
890 }
891
892 *path = strchrW(key, '\\');
893 if (*path) (*path)++;
894
895 *long_key = get_long_key(*root, *path);
896
897 return TRUE;
898 }
899
900 static BOOL is_help_switch(const WCHAR *s)
901 {
902 if (strlenW(s) > 2)
903 return FALSE;
904
905 if ((s[0] == '/' || s[0] == '-') && (s[1] == 'h' || s[1] == '?'))
906 return TRUE;
907
908 return FALSE;
909 }
910
911 enum operations {
912 REG_ADD,
913 REG_DELETE,
914 REG_IMPORT,
915 REG_QUERY,
916 REG_INVALID
917 };
918
919 static enum operations get_operation(const WCHAR *str, int *op_help)
920 {
921 struct op_info { const WCHAR *op; int id; int help_id; };
922
923 static const WCHAR add[] = {'a','d','d',0};
924 static const WCHAR delete[] = {'d','e','l','e','t','e',0};
925 static const WCHAR import[] = {'i','m','p','o','r','t',0};
926 static const WCHAR query[] = {'q','u','e','r','y',0};
927
928 static const struct op_info op_array[] =
929 {
930 { add, REG_ADD, STRING_ADD_USAGE },
931 { delete, REG_DELETE, STRING_DELETE_USAGE },
932 { import, REG_IMPORT, STRING_IMPORT_USAGE },
933 { query, REG_QUERY, STRING_QUERY_USAGE },
934 { NULL, -1, 0 }
935 };
936
937 const struct op_info *ptr;
938
939 for (ptr = op_array; ptr->op; ptr++)
940 {
941 if (!lstrcmpiW(str, ptr->op))
942 {
943 *op_help = ptr->help_id;
944 return ptr->id;
945 }
946 }
947
948 return REG_INVALID;
949 }
950
951 int wmain(int argc, WCHAR *argvW[])
952 {
953 int i, op, op_help, ret;
954 BOOL show_op_help = FALSE;
955 static const WCHAR switchVAW[] = {'v','a',0};
956 static const WCHAR switchVEW[] = {'v','e',0};
957 WCHAR *key_name, *path, *value_name = NULL, *type = NULL, *data = NULL, separator = '\0';
958 BOOL value_empty = FALSE, value_all = FALSE, recurse = FALSE, force = FALSE;
959 HKEY root;
960
961 if (argc == 1)
962 {
963 output_message(STRING_INVALID_SYNTAX);
964 output_message(STRING_REG_HELP);
965 return 1;
966 }
967
968 if (is_help_switch(argvW[1]))
969 {
970 output_message(STRING_USAGE);
971 return 0;
972 }
973
974 op = get_operation(argvW[1], &op_help);
975
976 if (op == REG_INVALID)
977 {
978 output_message(STRING_INVALID_OPTION, argvW[1]);
979 output_message(STRING_REG_HELP);
980 return 1;
981 }
982
983 if (argc > 2)
984 show_op_help = is_help_switch(argvW[2]);
985
986 if (argc == 2 || ((show_op_help || op == REG_IMPORT) && argc > 3))
987 {
988 output_message(STRING_INVALID_SYNTAX);
989 output_message(STRING_FUNC_HELP, struprW(argvW[1]));
990 return 1;
991 }
992 else if (show_op_help)
993 {
994 output_message(op_help);
995 return 0;
996 }
997
998 if (op == REG_IMPORT)
999 return reg_import(argvW[2]);
1000
1001 if (!parse_registry_key(argvW[2], &root, &path, &key_name))
1002 return 1;
1003
1004 for (i = 3; i < argc; i++)
1005 {
1006 if (argvW[i][0] == '/' || argvW[i][0] == '-')
1007 {
1008 WCHAR *ptr = &argvW[i][1];
1009
1010 if (!lstrcmpiW(ptr, switchVEW))
1011 {
1012 value_empty = TRUE;
1013 continue;
1014 }
1015 else if (!lstrcmpiW(ptr, switchVAW))
1016 {
1017 value_all = TRUE;
1018 continue;
1019 }
1020 else if (!ptr[0] || ptr[1])
1021 {
1022 output_message(STRING_INVALID_CMDLINE);
1023 return 1;
1024 }
1025
1026 switch(tolowerW(argvW[i][1]))
1027 {
1028 case 'v':
1029 if (value_name || !(value_name = argvW[++i]))
1030 {
1031 output_message(STRING_INVALID_CMDLINE);
1032 return 1;
1033 }
1034 break;
1035 case 't':
1036 if (type || !(type = argvW[++i]))
1037 {
1038 output_message(STRING_INVALID_CMDLINE);
1039 return 1;
1040 }
1041 break;
1042 case 'd':
1043 if (data || !(data = argvW[++i]))
1044 {
1045 output_message(STRING_INVALID_CMDLINE);
1046 return 1;
1047 }
1048 break;
1049 case 's':
1050 if (op == REG_QUERY)
1051 {
1052 recurse = TRUE;
1053 break;
1054 }
1055
1056 ptr = argvW[++i];
1057 if (!ptr || strlenW(ptr) != 1)
1058 {
1059 output_message(STRING_INVALID_CMDLINE);
1060 return 1;
1061 }
1062 separator = ptr[0];
1063 break;
1064 case 'f':
1065 force = TRUE;
1066 break;
1067 default:
1068 output_message(STRING_INVALID_CMDLINE);
1069 return 1;
1070 }
1071 }
1072 }
1073
1074 if ((value_name && value_empty) || (value_name && value_all) || (value_empty && value_all))
1075 {
1076 output_message(STRING_INVALID_CMDLINE);
1077 return 1;
1078 }
1079
1080 if (op == REG_ADD)
1081 ret = reg_add(root, path, value_name, value_empty, type, separator, data, force);
1082 else if (op == REG_DELETE)
1083 ret = reg_delete(root, path, key_name, value_name, value_empty, value_all, force);
1084 else
1085 ret = reg_query(root, path, key_name, value_name, value_empty, recurse);
1086 return ret;
1087 }