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