[REGEDIT]
[reactos.git] / reactos / base / applications / regedit / regproc.c
1 /*
2 * Registry processing routines. Routines, common for registry
3 * processing frontends.
4 *
5 * Copyright 1999 Sylvain St-Germain
6 * Copyright 2002 Andriy Palamarchuk
7 * Copyright 2008 Alexander N. S?rnes <alex@thehandofagony.com>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24 #include "regedit.h"
25
26 #define REG_VAL_BUF_SIZE 4096
27
28 /* maximal number of characters in hexadecimal data line,
29 * including the indentation, but not including the '\' character
30 */
31 #define REG_FILE_HEX_LINE_LEN (2 + 25 * 3)
32
33 static const CHAR *reg_class_names[] = {
34 "HKEY_LOCAL_MACHINE", "HKEY_USERS", "HKEY_CLASSES_ROOT",
35 "HKEY_CURRENT_CONFIG", "HKEY_CURRENT_USER", "HKEY_DYN_DATA"
36 };
37
38 #define REG_CLASS_NUMBER (sizeof(reg_class_names) / sizeof(reg_class_names[0]))
39
40 const WCHAR* reg_class_namesW[REG_CLASS_NUMBER] = {
41 L"HKEY_LOCAL_MACHINE", L"HKEY_USERS", L"HKEY_CLASSES_ROOT",
42 L"HKEY_CURRENT_CONFIG", L"HKEY_CURRENT_USER", L"HKEY_DYN_DATA"
43 };
44
45 static HKEY reg_class_keys[REG_CLASS_NUMBER] = {
46 HKEY_LOCAL_MACHINE, HKEY_USERS, HKEY_CLASSES_ROOT,
47 HKEY_CURRENT_CONFIG, HKEY_CURRENT_USER, HKEY_DYN_DATA
48 };
49
50 /* return values */
51 #define NOT_ENOUGH_MEMORY 1
52 #define IO_ERROR 2
53
54 /* processing macros */
55
56 /* common check of memory allocation results */
57 #define CHECK_ENOUGH_MEMORY(p) \
58 if (!(p)) \
59 { \
60 fprintf(stderr,"%s: file %s, line %d: Not enough memory\n", \
61 getAppName(), __FILE__, __LINE__); \
62 exit(NOT_ENOUGH_MEMORY); \
63 }
64
65 /******************************************************************************
66 * Allocates memory and converts input from multibyte to wide chars
67 * Returned string must be freed by the caller
68 */
69 WCHAR* GetWideString(const char* strA)
70 {
71 if(strA)
72 {
73 WCHAR* strW;
74 int len = MultiByteToWideChar(CP_ACP, 0, strA, -1, NULL, 0);
75
76 strW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
77 CHECK_ENOUGH_MEMORY(strW);
78 MultiByteToWideChar(CP_ACP, 0, strA, -1, strW, len);
79 return strW;
80 }
81 return NULL;
82 }
83
84 /******************************************************************************
85 * Allocates memory and converts input from multibyte to wide chars
86 * Returned string must be freed by the caller
87 */
88 static WCHAR* GetWideStringN(const char* strA, int chars, DWORD *len)
89 {
90 if(strA)
91 {
92 WCHAR* strW;
93 *len = MultiByteToWideChar(CP_ACP, 0, strA, chars, NULL, 0);
94
95 strW = HeapAlloc(GetProcessHeap(), 0, *len * sizeof(WCHAR));
96 CHECK_ENOUGH_MEMORY(strW);
97 MultiByteToWideChar(CP_ACP, 0, strA, chars, strW, *len);
98 return strW;
99 }
100 *len = 0;
101 return NULL;
102 }
103
104 /******************************************************************************
105 * Allocates memory and converts input from wide chars to multibyte
106 * Returned string must be freed by the caller
107 */
108 char* GetMultiByteString(const WCHAR* strW)
109 {
110 if(strW)
111 {
112 char* strA;
113 int len = WideCharToMultiByte(CP_ACP, 0, strW, -1, NULL, 0, NULL, NULL);
114
115 strA = HeapAlloc(GetProcessHeap(), 0, len);
116 CHECK_ENOUGH_MEMORY(strA);
117 WideCharToMultiByte(CP_ACP, 0, strW, -1, strA, len, NULL, NULL);
118 return strA;
119 }
120 return NULL;
121 }
122
123 /******************************************************************************
124 * Allocates memory and converts input from wide chars to multibyte
125 * Returned string must be freed by the caller
126 */
127 static char* GetMultiByteStringN(const WCHAR* strW, int chars, DWORD* len)
128 {
129 if(strW)
130 {
131 char* strA;
132 *len = WideCharToMultiByte(CP_ACP, 0, strW, chars, NULL, 0, NULL, NULL);
133
134 strA = HeapAlloc(GetProcessHeap(), 0, *len);
135 CHECK_ENOUGH_MEMORY(strA);
136 WideCharToMultiByte(CP_ACP, 0, strW, chars, strA, *len, NULL, NULL);
137 return strA;
138 }
139 *len = 0;
140 return NULL;
141 }
142
143 /******************************************************************************
144 * Converts a hex representation of a DWORD into a DWORD.
145 */
146 static BOOL convertHexToDWord(WCHAR* str, DWORD *dw)
147 {
148 char buf[9];
149 char dummy;
150
151 WideCharToMultiByte(CP_ACP, 0, str, -1, buf, 9, NULL, NULL);
152 if (lstrlenW(str) > 8 || sscanf(buf, "%lx%c", dw, &dummy) != 1) {
153 fprintf(stderr,"%s: ERROR, invalid hex value\n", getAppName());
154 return FALSE;
155 }
156 return TRUE;
157 }
158
159 /******************************************************************************
160 * Converts a hex comma separated values list into a binary string.
161 */
162 static BYTE* convertHexCSVToHex(WCHAR *str, DWORD *size)
163 {
164 WCHAR *s;
165 BYTE *d, *data;
166
167 /* The worst case is 1 digit + 1 comma per byte */
168 *size=(lstrlenW(str)+1)/2;
169 data=HeapAlloc(GetProcessHeap(), 0, *size);
170 CHECK_ENOUGH_MEMORY(data);
171
172 s = str;
173 d = data;
174 *size=0;
175 while (*s != '\0') {
176 UINT wc;
177 WCHAR *end;
178
179 wc = wcstoul(s,&end, 16);
180 if (end == s || wc > 0xff || (*end && *end != L',')) {
181 char* strA = GetMultiByteString(s);
182 fprintf(stderr,"%s: ERROR converting CSV hex stream. Invalid value at '%s'\n",
183 getAppName(), strA);
184 HeapFree(GetProcessHeap(), 0, data);
185 HeapFree(GetProcessHeap(), 0, strA);
186 return NULL;
187 }
188 *d++ =(BYTE)wc;
189 (*size)++;
190 if (*end) end++;
191 s = end;
192 }
193
194 return data;
195 }
196
197 /******************************************************************************
198 * This function returns the HKEY associated with the data type encoded in the
199 * value. It modifies the input parameter (key value) in order to skip this
200 * "now useless" data type information.
201 *
202 * Note: Updated based on the algorithm used in 'server/registry.c'
203 */
204 static DWORD getDataType(LPWSTR *lpValue, DWORD* parse_type)
205 {
206 struct data_type { const WCHAR *tag; int len; int type; int parse_type; };
207
208 static const WCHAR quote[] = {'"'};
209 static const WCHAR str[] = {'s','t','r',':','"'};
210 static const WCHAR str2[] = {'s','t','r','(','2',')',':','"'};
211 static const WCHAR hex[] = {'h','e','x',':'};
212 static const WCHAR dword[] = {'d','w','o','r','d',':'};
213 static const WCHAR hexp[] = {'h','e','x','('};
214
215 static const struct data_type data_types[] = { /* actual type */ /* type to assume for parsing */
216 { quote, 1, REG_SZ, REG_SZ },
217 { str, 5, REG_SZ, REG_SZ },
218 { str2, 8, REG_EXPAND_SZ, REG_SZ },
219 { hex, 4, REG_BINARY, REG_BINARY },
220 { dword, 6, REG_DWORD, REG_DWORD },
221 { hexp, 4, -1, REG_BINARY },
222 { NULL, 0, 0, 0 }
223 };
224
225 const struct data_type *ptr;
226 int type;
227
228 for (ptr = data_types; ptr->tag; ptr++) {
229 if (wcsncmp(ptr->tag, *lpValue, ptr->len))
230 continue;
231
232 /* Found! */
233 *parse_type = ptr->parse_type;
234 type=ptr->type;
235 *lpValue+=ptr->len;
236 if (type == -1) {
237 WCHAR* end;
238
239 /* "hex(xx):" is special */
240 type = (int)wcstoul( *lpValue , &end, 16 );
241 if (**lpValue=='\0' || *end!=')' || *(end+1)!=':') {
242 type=REG_NONE;
243 } else {
244 *lpValue = end + 2;
245 }
246 }
247 return type;
248 }
249 *parse_type=REG_NONE;
250 return REG_NONE;
251 }
252
253 /******************************************************************************
254 * Replaces escape sequences with the characters.
255 */
256 static void REGPROC_unescape_string(WCHAR* str)
257 {
258 int str_idx = 0; /* current character under analysis */
259 int val_idx = 0; /* the last character of the unescaped string */
260 int len = lstrlenW(str);
261 for (str_idx = 0; str_idx < len; str_idx++, val_idx++) {
262 if (str[str_idx] == '\\') {
263 str_idx++;
264 switch (str[str_idx]) {
265 case 'n':
266 str[val_idx] = '\n';
267 break;
268 case '\\':
269 case '"':
270 str[val_idx] = str[str_idx];
271 break;
272 default:
273 fprintf(stderr,"Warning! Unrecognized escape sequence: \\%c'\n",
274 str[str_idx]);
275 str[val_idx] = str[str_idx];
276 break;
277 }
278 } else {
279 str[val_idx] = str[str_idx];
280 }
281 }
282 str[val_idx] = '\0';
283 }
284
285 static BOOL parseKeyName(LPWSTR lpKeyName, HKEY *hKey, LPWSTR *lpKeyPath)
286 {
287 WCHAR* lpSlash = NULL;
288 unsigned int i, len;
289
290 if (lpKeyName == NULL)
291 return FALSE;
292
293 for(i = 0; *(lpKeyName + i) != 0; i++)
294 {
295 if(*(lpKeyName+i) == '\\')
296 {
297 lpSlash = lpKeyName + i;
298 break;
299 }
300 }
301
302 if (lpSlash)
303 {
304 len = lpSlash-lpKeyName;
305 }
306 else
307 {
308 len = lstrlenW(lpKeyName);
309 lpSlash = lpKeyName+len;
310 }
311 *hKey = NULL;
312
313 for (i = 0; i < REG_CLASS_NUMBER; i++) {
314 if (CompareStringW(LOCALE_USER_DEFAULT, 0, lpKeyName, len, reg_class_namesW[i], len) == CSTR_EQUAL &&
315 len == lstrlenW(reg_class_namesW[i])) {
316 *hKey = reg_class_keys[i];
317 break;
318 }
319 }
320
321 if (*hKey == NULL)
322 return FALSE;
323
324 if (*lpSlash != '\0')
325 lpSlash++;
326 *lpKeyPath = lpSlash;
327 return TRUE;
328 }
329
330 /* Globals used by the setValue() & co */
331 static LPSTR currentKeyName;
332 static HKEY currentKeyHandle = NULL;
333
334 /******************************************************************************
335 * Sets the value with name val_name to the data in val_data for the currently
336 * opened key.
337 *
338 * Parameters:
339 * val_name - name of the registry value
340 * val_data - registry value data
341 */
342 static LONG setValue(WCHAR* val_name, WCHAR* val_data, BOOL is_unicode)
343 {
344 LONG res;
345 DWORD dwDataType, dwParseType;
346 LPBYTE lpbData;
347 DWORD dwData, dwLen;
348 WCHAR del[] = {'-',0};
349
350 if ( (val_name == NULL) || (val_data == NULL) )
351 return ERROR_INVALID_PARAMETER;
352
353 if (lstrcmpW(val_data, del) == 0)
354 {
355 res=RegDeleteValueW(currentKeyHandle,val_name);
356 return (res == ERROR_FILE_NOT_FOUND ? ERROR_SUCCESS : res);
357 }
358
359 /* Get the data type stored into the value field */
360 dwDataType = getDataType(&val_data, &dwParseType);
361
362 if (dwParseType == REG_SZ) /* no conversion for string */
363 {
364 REGPROC_unescape_string(val_data);
365 /* Compute dwLen after REGPROC_unescape_string because it may
366 * have changed the string length and we don't want to store
367 * the extra garbage in the registry.
368 */
369 dwLen = lstrlenW(val_data);
370 if (dwLen>0 && val_data[dwLen-1]=='"')
371 {
372 dwLen--;
373 val_data[dwLen]='\0';
374 }
375 lpbData = (BYTE*) val_data;
376 dwLen++; /* include terminating null */
377 dwLen = dwLen * sizeof(WCHAR); /* size is in bytes */
378 }
379 else if (dwParseType == REG_DWORD) /* Convert the dword types */
380 {
381 if (!convertHexToDWord(val_data, &dwData))
382 return ERROR_INVALID_DATA;
383 lpbData = (BYTE*)&dwData;
384 dwLen = sizeof(dwData);
385 }
386 else if (dwParseType == REG_BINARY) /* Convert the binary data */
387 {
388 lpbData = convertHexCSVToHex(val_data, &dwLen);
389 if (!lpbData)
390 return ERROR_INVALID_DATA;
391
392 if((dwDataType == REG_MULTI_SZ || dwDataType == REG_EXPAND_SZ) && !is_unicode)
393 {
394 LPBYTE tmp = lpbData;
395 lpbData = (LPBYTE)GetWideStringN((char*)lpbData, dwLen, &dwLen);
396 dwLen *= sizeof(WCHAR);
397 HeapFree(GetProcessHeap(), 0, tmp);
398 }
399 }
400 else /* unknown format */
401 {
402 fprintf(stderr,"%s: ERROR, unknown data format\n", getAppName());
403 return ERROR_INVALID_DATA;
404 }
405
406 res = RegSetValueExW(
407 currentKeyHandle,
408 val_name,
409 0, /* Reserved */
410 dwDataType,
411 lpbData,
412 dwLen);
413 if (dwParseType == REG_BINARY)
414 HeapFree(GetProcessHeap(), 0, lpbData);
415 return res;
416 }
417
418 /******************************************************************************
419 * A helper function for processRegEntry() that opens the current key.
420 * That key must be closed by calling closeKey().
421 */
422 static LONG openKeyW(WCHAR* stdInput)
423 {
424 HKEY keyClass;
425 WCHAR* keyPath;
426 DWORD dwDisp;
427 LONG res;
428
429 /* Sanity checks */
430 if (stdInput == NULL)
431 return ERROR_INVALID_PARAMETER;
432
433 /* Get the registry class */
434 if (!parseKeyName(stdInput, &keyClass, &keyPath))
435 return ERROR_INVALID_PARAMETER;
436
437 res = RegCreateKeyExW(
438 keyClass, /* Class */
439 keyPath, /* Sub Key */
440 0, /* MUST BE 0 */
441 NULL, /* object type */
442 REG_OPTION_NON_VOLATILE, /* option, REG_OPTION_NON_VOLATILE ... */
443 KEY_ALL_ACCESS, /* access mask, KEY_ALL_ACCESS */
444 NULL, /* security attribute */
445 &currentKeyHandle, /* result */
446 &dwDisp); /* disposition, REG_CREATED_NEW_KEY or
447 REG_OPENED_EXISTING_KEY */
448
449 if (res == ERROR_SUCCESS)
450 currentKeyName = GetMultiByteString(stdInput);
451 else
452 currentKeyHandle = NULL;
453
454 return res;
455
456 }
457
458 /******************************************************************************
459 * Close the currently opened key.
460 */
461 static void closeKey(void)
462 {
463 if (currentKeyHandle)
464 {
465 HeapFree(GetProcessHeap(), 0, currentKeyName);
466 RegCloseKey(currentKeyHandle);
467 currentKeyHandle = NULL;
468 }
469 }
470
471 /******************************************************************************
472 * This function is a wrapper for the setValue function. It prepares the
473 * land and cleans the area once completed.
474 * Note: this function modifies the line parameter.
475 *
476 * line - registry file unwrapped line. Should have the registry value name and
477 * complete registry value data.
478 */
479 static void processSetValue(WCHAR* line, BOOL is_unicode)
480 {
481 WCHAR* val_name; /* registry value name */
482 WCHAR* val_data; /* registry value data */
483 int line_idx = 0; /* current character under analysis */
484 LONG res;
485
486 /* get value name */
487 while ( iswspace(line[line_idx]) ) line_idx++;
488 if (line[line_idx] == '@' && line[line_idx + 1] == '=') {
489 line[line_idx] = '\0';
490 val_name = line;
491 line_idx++;
492 } else if (line[line_idx] == '\"') {
493 line_idx++;
494 val_name = line + line_idx;
495 while (TRUE) {
496 if (line[line_idx] == '\\') /* skip escaped character */
497 {
498 line_idx += 2;
499 } else {
500 if (line[line_idx] == '\"') {
501 line[line_idx] = '\0';
502 line_idx++;
503 break;
504 } else {
505 line_idx++;
506 }
507 }
508 }
509 while ( iswspace(line[line_idx]) ) line_idx++;
510 if (line[line_idx] != '=') {
511 char* lineA;
512 line[line_idx] = '\"';
513 lineA = GetMultiByteString(line);
514 fprintf(stderr,"Warning! unrecognized line:\n%s\n", lineA);
515 HeapFree(GetProcessHeap(), 0, lineA);
516 return;
517 }
518
519 } else {
520 char* lineA = GetMultiByteString(line);
521 fprintf(stderr,"Warning! unrecognized line:\n%s\n", lineA);
522 HeapFree(GetProcessHeap(), 0, lineA);
523 return;
524 }
525 line_idx++; /* skip the '=' character */
526
527 while ( iswspace(line[line_idx]) ) line_idx++;
528 val_data = line + line_idx;
529 /* trim trailing blanks */
530 line_idx = lstrlenW(val_data);
531 while (line_idx > 0 && iswspace(val_data[line_idx-1])) line_idx--;
532 val_data[line_idx] = '\0';
533
534 REGPROC_unescape_string(val_name);
535 res = setValue(val_name, val_data, is_unicode);
536 if ( res != ERROR_SUCCESS )
537 {
538 char* val_nameA = GetMultiByteString(val_name);
539 char* val_dataA = GetMultiByteString(val_data);
540 fprintf(stderr,"%s: ERROR Key %s not created. Value: %s, Data: %s\n",
541 getAppName(),
542 currentKeyName,
543 val_nameA,
544 val_dataA);
545 HeapFree(GetProcessHeap(), 0, val_nameA);
546 HeapFree(GetProcessHeap(), 0, val_dataA);
547 }
548 }
549
550 /******************************************************************************
551 * This function receives the currently read entry and performs the
552 * corresponding action.
553 * isUnicode affects parsing of REG_MULTI_SZ values
554 */
555 static void processRegEntry(WCHAR* stdInput, BOOL isUnicode)
556 {
557 /*
558 * We encountered the end of the file, make sure we
559 * close the opened key and exit
560 */
561 if (stdInput == NULL) {
562 closeKey();
563 return;
564 }
565
566 if ( stdInput[0] == L'[') /* We are reading a new key */
567 {
568 WCHAR* keyEnd;
569 closeKey(); /* Close the previous key */
570
571 /* Get rid of the square brackets */
572 stdInput++;
573 keyEnd = wcsrchr(stdInput, L']');
574 if (keyEnd)
575 *keyEnd='\0';
576
577 /* delete the key if we encounter '-' at the start of reg key */
578 if ( stdInput[0] == '-')
579 {
580 delete_registry_key(stdInput + 1);
581 } else if ( openKeyW(stdInput) != ERROR_SUCCESS )
582 {
583 char* stdInputA = GetMultiByteString(stdInput);
584 fprintf(stderr,"%s: setValue failed to open key %s\n",
585 getAppName(), stdInputA);
586 HeapFree(GetProcessHeap(), 0, stdInputA);
587 }
588 } else if( currentKeyHandle &&
589 (( stdInput[0] == '@') || /* reading a default @=data pair */
590 ( stdInput[0] == '\"'))) /* reading a new value=data pair */
591 {
592 processSetValue(stdInput, isUnicode);
593 } else
594 {
595 /* Since we are assuming that the file format is valid we must be
596 * reading a blank line which indicates the end of this key processing
597 */
598 closeKey();
599 }
600 }
601
602 /******************************************************************************
603 * Processes a registry file.
604 * Correctly processes comments (in # form), line continuation.
605 *
606 * Parameters:
607 * in - input stream to read from
608 */
609 static void processRegLinesA(FILE *in)
610 {
611 LPSTR line = NULL; /* line read from input stream */
612 ULONG lineSize = REG_VAL_BUF_SIZE;
613
614 line = HeapAlloc(GetProcessHeap(), 0, lineSize);
615 CHECK_ENOUGH_MEMORY(line);
616
617 while (!feof(in)) {
618 LPSTR s; /* The pointer into line for where the current fgets should read */
619 LPSTR check;
620 WCHAR* lineW;
621 s = line;
622
623 for (;;) {
624 size_t size_remaining;
625 int size_to_get;
626 char *s_eol; /* various local uses */
627
628 /* Do we need to expand the buffer ? */
629 assert (s >= line && s <= line + lineSize);
630 size_remaining = lineSize - (s-line);
631 if (size_remaining < 2) /* room for 1 character and the \0 */
632 {
633 char *new_buffer;
634 size_t new_size = lineSize + REG_VAL_BUF_SIZE;
635 if (new_size > lineSize) /* no arithmetic overflow */
636 new_buffer = HeapReAlloc (GetProcessHeap(), 0, line, new_size);
637 else
638 new_buffer = NULL;
639 CHECK_ENOUGH_MEMORY(new_buffer);
640 line = new_buffer;
641 s = line + lineSize - size_remaining;
642 lineSize = new_size;
643 size_remaining = lineSize - (s-line);
644 }
645
646 /* Get as much as possible into the buffer, terminated either by
647 * eof, error, eol or getting the maximum amount. Abort on error.
648 */
649 size_to_get = (size_remaining > INT_MAX ? INT_MAX : size_remaining);
650
651 check = fgets (s, size_to_get, in);
652
653 if (check == NULL) {
654 if (ferror(in)) {
655 perror ("While reading input");
656 exit (IO_ERROR);
657 } else {
658 assert (feof(in));
659 *s = '\0';
660 /* It is not clear to me from the definition that the
661 * contents of the buffer are well defined on detecting
662 * an eof without managing to read anything.
663 */
664 }
665 }
666
667 /* If we didn't read the eol nor the eof go around for the rest */
668 s_eol = strchr (s, '\n');
669 if (!feof (in) && !s_eol) {
670 s = strchr (s, '\0');
671 /* It should be s + size_to_get - 1 but this is safer */
672 continue;
673 }
674
675 /* If it is a comment line then discard it and go around again */
676 if (line [0] == '#') {
677 s = line;
678 continue;
679 }
680
681 /* Remove any line feed. Leave s_eol on the \0 */
682 if (s_eol) {
683 *s_eol = '\0';
684 if (s_eol > line && *(s_eol-1) == '\r')
685 *--s_eol = '\0';
686 } else
687 s_eol = strchr (s, '\0');
688
689 /* If there is a concatenating \\ then go around again */
690 if (s_eol > line && *(s_eol-1) == '\\') {
691 int c;
692 s = s_eol-1;
693
694 do
695 {
696 c = fgetc(in);
697 } while(c == ' ' || c == '\t');
698
699 if(c == EOF)
700 {
701 fprintf(stderr,"%s: ERROR - invalid continuation.\n",
702 getAppName());
703 }
704 else
705 {
706 *s = c;
707 s++;
708 }
709 continue;
710 }
711
712 lineW = GetWideString(line);
713
714 break; /* That is the full virtual line */
715 }
716
717 processRegEntry(lineW, FALSE);
718 HeapFree(GetProcessHeap(), 0, lineW);
719 }
720 processRegEntry(NULL, FALSE);
721
722 HeapFree(GetProcessHeap(), 0, line);
723 }
724
725 static void processRegLinesW(FILE *in)
726 {
727 WCHAR* buf = NULL; /* line read from input stream */
728 ULONG lineSize = REG_VAL_BUF_SIZE;
729 size_t CharsInBuf = -1;
730
731 WCHAR* s; /* The pointer into buf for where the current fgets should read */
732 WCHAR* line; /* The start of the current line */
733
734 buf = HeapAlloc(GetProcessHeap(), 0, lineSize * sizeof(WCHAR));
735 CHECK_ENOUGH_MEMORY(buf);
736
737 s = buf;
738 line = buf;
739
740 while(!feof(in)) {
741 size_t size_remaining;
742 int size_to_get;
743 WCHAR *s_eol = NULL; /* various local uses */
744
745 /* Do we need to expand the buffer ? */
746 assert (s >= buf && s <= buf + lineSize);
747 size_remaining = lineSize - (s-buf);
748 if (size_remaining < 2) /* room for 1 character and the \0 */
749 {
750 WCHAR *new_buffer;
751 size_t new_size = lineSize + (REG_VAL_BUF_SIZE / sizeof(WCHAR));
752 if (new_size > lineSize) /* no arithmetic overflow */
753 new_buffer = HeapReAlloc (GetProcessHeap(), 0, buf, new_size * sizeof(WCHAR));
754 else
755 new_buffer = NULL;
756 CHECK_ENOUGH_MEMORY(new_buffer);
757 buf = new_buffer;
758 line = buf;
759 s = buf + lineSize - size_remaining;
760 lineSize = new_size;
761 size_remaining = lineSize - (s-buf);
762 }
763
764 /* Get as much as possible into the buffer, terminated either by
765 * eof, error or getting the maximum amount. Abort on error.
766 */
767 size_to_get = (size_remaining > INT_MAX ? INT_MAX : size_remaining);
768
769 CharsInBuf = fread(s, sizeof(WCHAR), size_to_get - 1, in);
770 s[CharsInBuf] = 0;
771
772 if (CharsInBuf == 0) {
773 if (ferror(in)) {
774 perror ("While reading input");
775 exit (IO_ERROR);
776 } else {
777 assert (feof(in));
778 *s = '\0';
779 /* It is not clear to me from the definition that the
780 * contents of the buffer are well defined on detecting
781 * an eof without managing to read anything.
782 */
783 }
784 }
785
786 /* If we didn't read the eol nor the eof go around for the rest */
787 while(1)
788 {
789 s_eol = wcschr(line, '\n');
790
791 if(!s_eol) {
792 /* Move the stub of the line to the start of the buffer so
793 * we get the maximum space to read into, and so we don't
794 * have to recalculate 'line' if the buffer expands */
795 MoveMemory(buf, line, (lstrlenW(line) + 1) * sizeof(WCHAR));
796 line = buf;
797 s = wcschr(line, '\0');
798 break;
799 }
800
801 /* If it is a comment line then discard it and go around again */
802 if (*line == '#') {
803 line = s_eol + 1;
804 continue;
805 }
806
807 /* If there is a concatenating \\ then go around again */
808 if ((*(s_eol-1) == '\\') ||
809 (*(s_eol-1) == '\r' && *(s_eol-2) == '\\')) {
810 WCHAR* NextLine = s_eol;
811
812 while(*(NextLine+1) == ' ' || *(NextLine+1) == '\t')
813 NextLine++;
814
815 NextLine++;
816
817 if(*(s_eol-1) == '\r')
818 s_eol--;
819
820 MoveMemory(s_eol - 1, NextLine, (CharsInBuf - (NextLine - s) + 1)*sizeof(WCHAR));
821 CharsInBuf -= NextLine - s_eol + 1;
822 s_eol = 0;
823 continue;
824 }
825
826 /* Remove any line feed. Leave s_eol on the \0 */
827 if (s_eol) {
828 *s_eol = '\0';
829 if (s_eol > buf && *(s_eol-1) == '\r')
830 *(s_eol-1) = '\0';
831 }
832
833 if(!s_eol)
834 break;
835
836 processRegEntry(line, TRUE);
837 line = s_eol + 1;
838 s_eol = 0;
839 continue; /* That is the full virtual line */
840 }
841 }
842
843 processRegEntry(NULL, TRUE);
844
845 HeapFree(GetProcessHeap(), 0, buf);
846 }
847
848 /****************************************************************************
849 * REGPROC_print_error
850 *
851 * Print the message for GetLastError
852 */
853
854 static void REGPROC_print_error(void)
855 {
856 LPVOID lpMsgBuf;
857 DWORD error_code;
858 int status;
859
860 error_code = GetLastError ();
861 status = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
862 NULL, error_code, 0, (LPTSTR) &lpMsgBuf, 0, NULL);
863 if (!status) {
864 fprintf(stderr,"%s: Cannot display message for error %ld, status %ld\n",
865 getAppName(), error_code, GetLastError());
866 exit(1);
867 }
868 puts(lpMsgBuf);
869 LocalFree(lpMsgBuf);
870 exit(1);
871 }
872
873 /******************************************************************************
874 * Checks whether the buffer has enough room for the string or required size.
875 * Resizes the buffer if necessary.
876 *
877 * Parameters:
878 * buffer - pointer to a buffer for string
879 * len - current length of the buffer in characters.
880 * required_len - length of the string to place to the buffer in characters.
881 * The length does not include the terminating null character.
882 */
883 static void REGPROC_resize_char_buffer(WCHAR **buffer, DWORD *len, DWORD required_len)
884 {
885 required_len++;
886 if (required_len > *len) {
887 *len = required_len;
888 if (!*buffer)
889 *buffer = HeapAlloc(GetProcessHeap(), 0, *len * sizeof(**buffer));
890 else
891 *buffer = HeapReAlloc(GetProcessHeap(), 0, *buffer, *len * sizeof(**buffer));
892 CHECK_ENOUGH_MEMORY(*buffer);
893 }
894 }
895
896 /******************************************************************************
897 * Same as REGPROC_resize_char_buffer() but on a regular buffer.
898 *
899 * Parameters:
900 * buffer - pointer to a buffer
901 * len - current size of the buffer in bytes
902 * required_size - size of the data to place in the buffer in bytes
903 */
904 static void REGPROC_resize_binary_buffer(BYTE **buffer, DWORD *size, DWORD required_size)
905 {
906 if (required_size > *size) {
907 *size = required_size;
908 if (!*buffer)
909 *buffer = HeapAlloc(GetProcessHeap(), 0, *size);
910 else
911 *buffer = HeapReAlloc(GetProcessHeap(), 0, *buffer, *size);
912 CHECK_ENOUGH_MEMORY(*buffer);
913 }
914 }
915
916 /******************************************************************************
917 * Prints string str to file
918 */
919 static void REGPROC_export_string(WCHAR **line_buf, DWORD *line_buf_size, DWORD *line_len, WCHAR *str, DWORD str_len)
920 {
921 DWORD i, pos;
922 DWORD extra = 0;
923
924 REGPROC_resize_char_buffer(line_buf, line_buf_size, *line_len + str_len + 10);
925
926 /* escaping characters */
927 pos = *line_len;
928 for (i = 0; i < str_len; i++) {
929 WCHAR c = str[i];
930 switch (c) {
931 case '\n':
932 extra++;
933 REGPROC_resize_char_buffer(line_buf, line_buf_size, *line_len + str_len + extra);
934 (*line_buf)[pos++] = '\\';
935 (*line_buf)[pos++] = 'n';
936 break;
937
938 case '\\':
939 case '"':
940 extra++;
941 REGPROC_resize_char_buffer(line_buf, line_buf_size, *line_len + str_len + extra);
942 (*line_buf)[pos++] = '\\';
943 /* Fall through */
944
945 default:
946 (*line_buf)[pos++] = c;
947 break;
948 }
949 }
950 (*line_buf)[pos] = '\0';
951 *line_len = pos;
952 }
953
954 static void REGPROC_export_binary(WCHAR **line_buf, DWORD *line_buf_size, DWORD *line_len, DWORD type, BYTE *value, DWORD value_size, BOOL unicode)
955 {
956 DWORD hex_pos, data_pos;
957 const WCHAR *hex_prefix;
958 const WCHAR hex[] = {'h','e','x',':',0};
959 WCHAR hex_buf[17];
960 const WCHAR concat[] = {'\\','\n',' ',' ',0};
961 DWORD concat_prefix, concat_len;
962 const WCHAR newline[] = {'\n',0};
963 CHAR* value_multibyte = NULL;
964
965 if (type == REG_BINARY) {
966 hex_prefix = hex;
967 } else {
968 const WCHAR hex_format[] = {'h','e','x','(','%','u',')',':',0};
969 hex_prefix = hex_buf;
970 wsprintfW(hex_buf, hex_format, type);
971 if ((type == REG_SZ || type == REG_EXPAND_SZ || type == REG_MULTI_SZ) && !unicode)
972 {
973 value_multibyte = GetMultiByteStringN((WCHAR*)value, value_size / sizeof(WCHAR), &value_size);
974 value = (BYTE*)value_multibyte;
975 }
976 }
977
978 concat_len = lstrlenW(concat);
979 concat_prefix = 2;
980
981 hex_pos = *line_len;
982 *line_len += lstrlenW(hex_prefix);
983 data_pos = *line_len;
984 *line_len += value_size * 3;
985 /* - The 2 spaces that concat places at the start of the
986 * line effectively reduce the space available for data.
987 * - If the value name and hex prefix are very long
988 * ( > REG_FILE_HEX_LINE_LEN) then we may overestimate
989 * the needed number of lines by one. But that's ok.
990 * - The trailing linefeed takes the place of a comma so
991 * it's accounted for already.
992 */
993 *line_len += *line_len / (REG_FILE_HEX_LINE_LEN - concat_prefix) * concat_len;
994 REGPROC_resize_char_buffer(line_buf, line_buf_size, *line_len);
995 lstrcpyW(*line_buf + hex_pos, hex_prefix);
996 if (value_size)
997 {
998 const WCHAR format[] = {'%','0','2','x',0};
999 DWORD i, column;
1000
1001 column = data_pos; /* no line wrap yet */
1002 i = 0;
1003 while (1)
1004 {
1005 wsprintfW(*line_buf + data_pos, format, (unsigned int)value[i]);
1006 data_pos += 2;
1007 if (++i == value_size)
1008 break;
1009
1010 (*line_buf)[data_pos++] = ',';
1011 column += 3;
1012
1013 /* wrap the line */
1014 if (column >= REG_FILE_HEX_LINE_LEN) {
1015 lstrcpyW(*line_buf + data_pos, concat);
1016 data_pos += concat_len;
1017 column = concat_prefix;
1018 }
1019 }
1020 }
1021 lstrcpyW(*line_buf + data_pos, newline);
1022 HeapFree(GetProcessHeap(), 0, value_multibyte);
1023 }
1024
1025 /******************************************************************************
1026 * Writes the given line to a file, in multi-byte or wide characters
1027 */
1028 static void REGPROC_write_line(FILE *file, const WCHAR* str, BOOL unicode)
1029 {
1030 int i;
1031 if (unicode)
1032 {
1033 for(i = 0; str[i]; i++)
1034 {
1035 if (str[i] == L'\n')
1036 fputwc(L'\r', file);
1037 fputwc(str[i], file);
1038 }
1039 }
1040 else
1041 {
1042 char* strA = GetMultiByteString(str);
1043 fputs(strA, file);
1044 HeapFree(GetProcessHeap(), 0, strA);
1045 }
1046 }
1047
1048 /******************************************************************************
1049 * Writes contents of the registry key to the specified file stream.
1050 *
1051 * Parameters:
1052 * file - writable file stream to export registry branch to.
1053 * key - registry branch to export.
1054 * reg_key_name_buf - name of the key with registry class.
1055 * Is resized if necessary.
1056 * reg_key_name_size - length of the buffer for the registry class in characters.
1057 * val_name_buf - buffer for storing value name.
1058 * Is resized if necessary.
1059 * val_name_size - length of the buffer for storing value names in characters.
1060 * val_buf - buffer for storing values while extracting.
1061 * Is resized if necessary.
1062 * val_size - size of the buffer for storing values in bytes.
1063 */
1064 static void export_hkey(FILE *file, HKEY key,
1065 WCHAR **reg_key_name_buf, DWORD *reg_key_name_size,
1066 WCHAR **val_name_buf, DWORD *val_name_size,
1067 BYTE **val_buf, DWORD *val_size,
1068 WCHAR **line_buf, DWORD *line_buf_size,
1069 BOOL unicode)
1070 {
1071 DWORD max_sub_key_len;
1072 DWORD max_val_name_len;
1073 DWORD max_val_size;
1074 DWORD curr_len;
1075 DWORD i;
1076 BOOL more_data;
1077 LONG ret;
1078 WCHAR key_format[] = {'\n','[','%','s',']','\n',0};
1079
1080 /* get size information and resize the buffers if necessary */
1081 if (RegQueryInfoKeyW(key, NULL, NULL, NULL, NULL,
1082 &max_sub_key_len, NULL,
1083 NULL, &max_val_name_len, &max_val_size, NULL, NULL
1084 ) != ERROR_SUCCESS) {
1085 REGPROC_print_error();
1086 }
1087 curr_len = lstrlenW(*reg_key_name_buf);
1088 REGPROC_resize_char_buffer(reg_key_name_buf, reg_key_name_size,
1089 max_sub_key_len + curr_len + 1);
1090 REGPROC_resize_char_buffer(val_name_buf, val_name_size,
1091 max_val_name_len);
1092 REGPROC_resize_binary_buffer(val_buf, val_size, max_val_size);
1093 REGPROC_resize_char_buffer(line_buf, line_buf_size, lstrlenW(*reg_key_name_buf) + 4);
1094 /* output data for the current key */
1095 wsprintfW(*line_buf, key_format, *reg_key_name_buf);
1096 REGPROC_write_line(file, *line_buf, unicode);
1097
1098 /* print all the values */
1099 i = 0;
1100 more_data = TRUE;
1101 while(more_data) {
1102 DWORD value_type;
1103 DWORD val_name_size1 = *val_name_size;
1104 DWORD val_size1 = *val_size;
1105 ret = RegEnumValueW(key, i, *val_name_buf, &val_name_size1, NULL,
1106 &value_type, *val_buf, &val_size1);
1107 if (ret == ERROR_MORE_DATA) {
1108 /* Increase the size of the buffers and retry */
1109 REGPROC_resize_char_buffer(val_name_buf, val_name_size, val_name_size1);
1110 REGPROC_resize_binary_buffer(val_buf, val_size, val_size1);
1111 } else if (ret != ERROR_SUCCESS) {
1112 more_data = FALSE;
1113 if (ret != ERROR_NO_MORE_ITEMS) {
1114 REGPROC_print_error();
1115 }
1116 } else {
1117 DWORD line_len;
1118 i++;
1119
1120 if ((*val_name_buf)[0]) {
1121 const WCHAR val_start[] = {'"','%','s','"','=',0};
1122
1123 line_len = 0;
1124 REGPROC_export_string(line_buf, line_buf_size, &line_len, *val_name_buf, lstrlenW(*val_name_buf));
1125 REGPROC_resize_char_buffer(val_name_buf, val_name_size, lstrlenW(*line_buf) + 1);
1126 lstrcpyW(*val_name_buf, *line_buf);
1127
1128 line_len = 3 + lstrlenW(*val_name_buf);
1129 REGPROC_resize_char_buffer(line_buf, line_buf_size, line_len);
1130 wsprintfW(*line_buf, val_start, *val_name_buf);
1131 } else {
1132 const WCHAR std_val[] = {'@','=',0};
1133 line_len = 2;
1134 REGPROC_resize_char_buffer(line_buf, line_buf_size, line_len);
1135 lstrcpyW(*line_buf, std_val);
1136 }
1137
1138 switch (value_type) {
1139 case REG_SZ:
1140 {
1141 WCHAR* wstr = (WCHAR*)*val_buf;
1142
1143 if (val_size1 < sizeof(WCHAR) || val_size1 % sizeof(WCHAR) ||
1144 wstr[val_size1 / sizeof(WCHAR) - 1]) {
1145 REGPROC_export_binary(line_buf, line_buf_size, &line_len, value_type, *val_buf, val_size1, unicode);
1146 } else {
1147 const WCHAR start[] = {'"',0};
1148 const WCHAR end[] = {'"','\n',0};
1149 DWORD len;
1150
1151 len = lstrlenW(start);
1152 REGPROC_resize_char_buffer(line_buf, line_buf_size, line_len + len);
1153 lstrcpyW(*line_buf + line_len, start);
1154 line_len += len;
1155
1156 /* At this point we know wstr is '\0'-terminated
1157 * so we can substract 1 from the size
1158 */
1159 REGPROC_export_string(line_buf, line_buf_size, &line_len, wstr, val_size1 / sizeof(WCHAR) - 1);
1160
1161 REGPROC_resize_char_buffer(line_buf, line_buf_size, line_len + lstrlenW(end));
1162 lstrcpyW(*line_buf + line_len, end);
1163 }
1164 break;
1165 }
1166
1167 case REG_DWORD:
1168 {
1169 WCHAR format[] = {'d','w','o','r','d',':','%','0','8','x','\n',0};
1170
1171 REGPROC_resize_char_buffer(line_buf, line_buf_size, line_len + 15);
1172 wsprintfW(*line_buf + line_len, format, *((DWORD *)*val_buf));
1173 break;
1174 }
1175
1176 default:
1177 {
1178 char* key_nameA = GetMultiByteString(*reg_key_name_buf);
1179 char* value_nameA = GetMultiByteString(*val_name_buf);
1180 fprintf(stderr,"%s: warning - unsupported registry format '%ld', "
1181 "treat as binary\n",
1182 getAppName(), value_type);
1183 fprintf(stderr,"key name: \"%s\"\n", key_nameA);
1184 fprintf(stderr,"value name:\"%s\"\n\n", value_nameA);
1185 HeapFree(GetProcessHeap(), 0, key_nameA);
1186 HeapFree(GetProcessHeap(), 0, value_nameA);
1187 }
1188 /* falls through */
1189 case REG_EXPAND_SZ:
1190 case REG_MULTI_SZ:
1191 /* falls through */
1192 case REG_BINARY:
1193 REGPROC_export_binary(line_buf, line_buf_size, &line_len, value_type, *val_buf, val_size1, unicode);
1194 }
1195 REGPROC_write_line(file, *line_buf, unicode);
1196 }
1197 }
1198
1199 i = 0;
1200 more_data = TRUE;
1201 (*reg_key_name_buf)[curr_len] = '\\';
1202 while(more_data) {
1203 DWORD buf_size = *reg_key_name_size - curr_len - 1;
1204
1205 ret = RegEnumKeyExW(key, i, *reg_key_name_buf + curr_len + 1, &buf_size,
1206 NULL, NULL, NULL, NULL);
1207 if (ret == ERROR_MORE_DATA) {
1208 /* Increase the size of the buffer and retry */
1209 REGPROC_resize_char_buffer(reg_key_name_buf, reg_key_name_size, curr_len + 1 + buf_size);
1210 } else if (ret != ERROR_SUCCESS) {
1211 more_data = FALSE;
1212 if (ret != ERROR_NO_MORE_ITEMS) {
1213 REGPROC_print_error();
1214 }
1215 } else {
1216 HKEY subkey;
1217
1218 i++;
1219 if (RegOpenKeyW(key, *reg_key_name_buf + curr_len + 1,
1220 &subkey) == ERROR_SUCCESS) {
1221 export_hkey(file, subkey, reg_key_name_buf, reg_key_name_size,
1222 val_name_buf, val_name_size, val_buf, val_size,
1223 line_buf, line_buf_size, unicode);
1224 RegCloseKey(subkey);
1225 } else {
1226 REGPROC_print_error();
1227 }
1228 }
1229 }
1230 (*reg_key_name_buf)[curr_len] = '\0';
1231 }
1232
1233 /******************************************************************************
1234 * Open file for export.
1235 */
1236 static FILE *REGPROC_open_export_file(WCHAR *file_name, BOOL unicode)
1237 {
1238 FILE *file;
1239 WCHAR dash = '-';
1240
1241 if (wcsncmp(file_name, &dash, 1) == 0)
1242 file = stdout;
1243 else
1244 {
1245 if (unicode)
1246 file = _wfopen(file_name, L"wb");
1247 else
1248 file = _wfopen(file_name, L"w");
1249 if (!file) {
1250 CHAR* file_nameA = GetMultiByteString(file_name);
1251 perror("");
1252 fprintf(stderr,"%s: Can't open file \"%s\"\n", getAppName(), file_nameA);
1253 HeapFree(GetProcessHeap(), 0, file_nameA);
1254 exit(1);
1255 }
1256 }
1257 if (unicode)
1258 {
1259 const BYTE unicode_seq[] = {0xff,0xfe};
1260 const WCHAR header[] = L"Windows Registry Editor Version 5.00\r\n";
1261 fwrite(unicode_seq, sizeof(BYTE), COUNT_OF(unicode_seq), file);
1262 fwrite(header, sizeof(WCHAR), lstrlenW(header), file);
1263 }
1264 else
1265 {
1266 fputs("REGEDIT4\n", file);
1267 }
1268
1269 return file;
1270 }
1271
1272 /******************************************************************************
1273 * Writes contents of the registry key to the specified file stream.
1274 *
1275 * Parameters:
1276 * file_name - name of a file to export registry branch to.
1277 * reg_key_name - registry branch to export. The whole registry is exported if
1278 * reg_key_name is NULL or contains an empty string.
1279 */
1280 BOOL export_registry_key(WCHAR *file_name, WCHAR *reg_key_name, DWORD format)
1281 {
1282 WCHAR *reg_key_name_buf;
1283 WCHAR *val_name_buf;
1284 BYTE *val_buf;
1285 WCHAR *line_buf;
1286 DWORD reg_key_name_size = KEY_MAX_LEN;
1287 DWORD val_name_size = KEY_MAX_LEN;
1288 DWORD val_size = REG_VAL_BUF_SIZE;
1289 DWORD line_buf_size = KEY_MAX_LEN + REG_VAL_BUF_SIZE;
1290 FILE *file = NULL;
1291 BOOL unicode = (format == REG_FORMAT_5);
1292
1293 reg_key_name_buf = HeapAlloc(GetProcessHeap(), 0,
1294 reg_key_name_size * sizeof(*reg_key_name_buf));
1295 val_name_buf = HeapAlloc(GetProcessHeap(), 0,
1296 val_name_size * sizeof(*val_name_buf));
1297 val_buf = HeapAlloc(GetProcessHeap(), 0, val_size);
1298 line_buf = HeapAlloc(GetProcessHeap(), 0, line_buf_size * sizeof(*line_buf));
1299 CHECK_ENOUGH_MEMORY(reg_key_name_buf && val_name_buf && val_buf && line_buf);
1300
1301 if (reg_key_name && reg_key_name[0]) {
1302 HKEY reg_key_class;
1303 WCHAR *branch_name = NULL;
1304 HKEY key;
1305
1306 REGPROC_resize_char_buffer(&reg_key_name_buf, &reg_key_name_size,
1307 lstrlenW(reg_key_name));
1308 lstrcpyW(reg_key_name_buf, reg_key_name);
1309
1310 /* open the specified key */
1311 if (!parseKeyName(reg_key_name, &reg_key_class, &branch_name)) {
1312 CHAR* key_nameA = GetMultiByteString(reg_key_name);
1313 fprintf(stderr,"%s: Incorrect registry class specification in '%s'\n",
1314 getAppName(), key_nameA);
1315 HeapFree(GetProcessHeap(), 0, key_nameA);
1316 exit(1);
1317 }
1318 if (!branch_name[0]) {
1319 /* no branch - registry class is specified */
1320 file = REGPROC_open_export_file(file_name, unicode);
1321 export_hkey(file, reg_key_class,
1322 &reg_key_name_buf, &reg_key_name_size,
1323 &val_name_buf, &val_name_size,
1324 &val_buf, &val_size, &line_buf,
1325 &line_buf_size, unicode);
1326 } else if (RegOpenKeyW(reg_key_class, branch_name, &key) == ERROR_SUCCESS) {
1327 file = REGPROC_open_export_file(file_name, unicode);
1328 export_hkey(file, key,
1329 &reg_key_name_buf, &reg_key_name_size,
1330 &val_name_buf, &val_name_size,
1331 &val_buf, &val_size, &line_buf,
1332 &line_buf_size, unicode);
1333 RegCloseKey(key);
1334 } else {
1335 CHAR* key_nameA = GetMultiByteString(reg_key_name);
1336 fprintf(stderr,"%s: Can't export. Registry key '%s' does not exist!\n",
1337 getAppName(), key_nameA);
1338 HeapFree(GetProcessHeap(), 0, key_nameA);
1339 REGPROC_print_error();
1340 }
1341 } else {
1342 unsigned int i;
1343
1344 /* export all registry classes */
1345 file = REGPROC_open_export_file(file_name, unicode);
1346 for (i = 0; i < REG_CLASS_NUMBER; i++) {
1347 /* do not export HKEY_CLASSES_ROOT */
1348 if (reg_class_keys[i] != HKEY_CLASSES_ROOT &&
1349 reg_class_keys[i] != HKEY_CURRENT_USER &&
1350 reg_class_keys[i] != HKEY_CURRENT_CONFIG &&
1351 reg_class_keys[i] != HKEY_DYN_DATA) {
1352 lstrcpyW(reg_key_name_buf, reg_class_namesW[i]);
1353 export_hkey(file, reg_class_keys[i],
1354 &reg_key_name_buf, &reg_key_name_size,
1355 &val_name_buf, &val_name_size,
1356 &val_buf, &val_size, &line_buf,
1357 &line_buf_size, unicode);
1358 }
1359 }
1360 }
1361
1362 if (file) {
1363 fclose(file);
1364 }
1365 HeapFree(GetProcessHeap(), 0, reg_key_name);
1366 HeapFree(GetProcessHeap(), 0, val_name_buf);
1367 HeapFree(GetProcessHeap(), 0, val_buf);
1368 HeapFree(GetProcessHeap(), 0, line_buf);
1369 return TRUE;
1370 }
1371
1372 /******************************************************************************
1373 * Reads contents of the specified file into the registry.
1374 */
1375 BOOL import_registry_file(FILE* reg_file)
1376 {
1377 if (reg_file)
1378 {
1379 BYTE s[2];
1380 if (fread( s, 2, 1, reg_file) == 1)
1381 {
1382 if (s[0] == 0xff && s[1] == 0xfe)
1383 {
1384 processRegLinesW(reg_file);
1385 } else
1386 {
1387 fseek(reg_file, 0, SEEK_SET);
1388 processRegLinesA(reg_file);
1389 }
1390 }
1391 return TRUE;
1392 }
1393 return FALSE;
1394 }
1395
1396 /******************************************************************************
1397 * Removes the registry key with all subkeys. Parses full key name.
1398 *
1399 * Parameters:
1400 * reg_key_name - full name of registry branch to delete. Ignored if is NULL,
1401 * empty, points to register key class, does not exist.
1402 */
1403 void delete_registry_key(WCHAR *reg_key_name)
1404 {
1405 WCHAR *key_name = NULL;
1406 HKEY key_class;
1407
1408 if (!reg_key_name || !reg_key_name[0])
1409 return;
1410
1411 if (!parseKeyName(reg_key_name, &key_class, &key_name)) {
1412 char* reg_key_nameA = GetMultiByteString(reg_key_name);
1413 fprintf(stderr,"%s: Incorrect registry class specification in '%s'\n",
1414 getAppName(), reg_key_nameA);
1415 HeapFree(GetProcessHeap(), 0, reg_key_nameA);
1416 exit(1);
1417 }
1418 if (!*key_name) {
1419 char* reg_key_nameA = GetMultiByteString(reg_key_name);
1420 fprintf(stderr,"%s: Can't delete registry class '%s'\n",
1421 getAppName(), reg_key_nameA);
1422 HeapFree(GetProcessHeap(), 0, reg_key_nameA);
1423 exit(1);
1424 }
1425
1426 SHDeleteKey(key_class, key_name);
1427 }