5ca08506c481fb6a9308cfa7d1ce18382651dc7e
[reactos.git] / reactos / 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(keyClass, /* Class */
462 keyPath, /* Sub Key */
463 0, /* MUST BE 0 */
464 NULL, /* object type */
465 REG_OPTION_NON_VOLATILE, /* option, REG_OPTION_NON_VOLATILE ... */
466 KEY_ALL_ACCESS, /* access mask, KEY_ALL_ACCESS */
467 NULL, /* security attribute */
468 &currentKeyHandle, /* result */
469 &dwDisp); /* disposition, REG_CREATED_NEW_KEY or
470 REG_OPENED_EXISTING_KEY */
471
472 if (res == ERROR_SUCCESS)
473 currentKeyName = GetMultiByteString(stdInput);
474 else
475 currentKeyHandle = NULL;
476
477 return res;
478
479 }
480
481 /******************************************************************************
482 * Close the currently opened key.
483 */
484 static void closeKey(void)
485 {
486 if (currentKeyHandle)
487 {
488 HeapFree(GetProcessHeap(), 0, currentKeyName);
489 RegCloseKey(currentKeyHandle);
490 currentKeyHandle = NULL;
491 }
492 }
493
494 /******************************************************************************
495 * This function is a wrapper for the setValue function. It prepares the
496 * land and cleans the area once completed.
497 * Note: this function modifies the line parameter.
498 *
499 * line - registry file unwrapped line. Should have the registry value name and
500 * complete registry value data.
501 */
502 static void processSetValue(WCHAR* line, BOOL is_unicode)
503 {
504 WCHAR* val_name; /* registry value name */
505 WCHAR* val_data; /* registry value data */
506 int line_idx = 0; /* current character under analysis */
507 LONG res;
508
509 /* get value name */
510 while ( iswspace(line[line_idx]) ) line_idx++;
511 if (line[line_idx] == '@' && line[line_idx + 1] == '=')
512 {
513 line[line_idx] = '\0';
514 val_name = line;
515 line_idx++;
516 }
517 else if (line[line_idx] == '\"')
518 {
519 line_idx++;
520 val_name = line + line_idx;
521 while (TRUE)
522 {
523 if (line[line_idx] == '\\') /* skip escaped character */
524 {
525 line_idx += 2;
526 }
527 else
528 {
529 if (line[line_idx] == '\"')
530 {
531 line[line_idx] = '\0';
532 line_idx++;
533 break;
534 }
535 else
536 {
537 line_idx++;
538 }
539 }
540 }
541 while ( iswspace(line[line_idx]) ) line_idx++;
542 if (line[line_idx] != '=')
543 {
544 char* lineA;
545 line[line_idx] = '\"';
546 lineA = GetMultiByteString(line);
547 fprintf(stderr,"Warning! unrecognized line:\n%s\n", lineA);
548 HeapFree(GetProcessHeap(), 0, lineA);
549 return;
550 }
551
552 }
553 else
554 {
555 char* lineA = GetMultiByteString(line);
556 fprintf(stderr,"Warning! unrecognized line:\n%s\n", lineA);
557 HeapFree(GetProcessHeap(), 0, lineA);
558 return;
559 }
560 line_idx++; /* skip the '=' character */
561
562 while ( iswspace(line[line_idx]) ) line_idx++;
563 val_data = line + line_idx;
564 /* trim trailing blanks */
565 line_idx = lstrlenW(val_data);
566 while (line_idx > 0 && iswspace(val_data[line_idx-1])) line_idx--;
567 val_data[line_idx] = '\0';
568
569 REGPROC_unescape_string(val_name);
570 res = setValue(val_name, val_data, is_unicode);
571 if ( res != ERROR_SUCCESS )
572 {
573 char* val_nameA = GetMultiByteString(val_name);
574 char* val_dataA = GetMultiByteString(val_data);
575 fprintf(stderr,"%S: ERROR Key %s not created. Value: %s, Data: %s\n",
576 getAppName(),
577 currentKeyName,
578 val_nameA,
579 val_dataA);
580 HeapFree(GetProcessHeap(), 0, val_nameA);
581 HeapFree(GetProcessHeap(), 0, val_dataA);
582 }
583 }
584
585 /******************************************************************************
586 * This function receives the currently read entry and performs the
587 * corresponding action.
588 * isUnicode affects parsing of REG_MULTI_SZ values
589 */
590 static void processRegEntry(WCHAR* stdInput, BOOL isUnicode)
591 {
592 /*
593 * We encountered the end of the file, make sure we
594 * close the opened key and exit
595 */
596 if (stdInput == NULL)
597 {
598 closeKey();
599 return;
600 }
601
602 if ( stdInput[0] == L'[') /* We are reading a new key */
603 {
604 WCHAR* keyEnd;
605 closeKey(); /* Close the previous key */
606
607 /* Get rid of the square brackets */
608 stdInput++;
609 keyEnd = wcsrchr(stdInput, L']');
610 if (keyEnd)
611 *keyEnd='\0';
612
613 /* delete the key if we encounter '-' at the start of reg key */
614 if ( stdInput[0] == '-')
615 {
616 delete_registry_key(stdInput + 1);
617 }
618 else if ( openKeyW(stdInput) != ERROR_SUCCESS )
619 {
620 char* stdInputA = GetMultiByteString(stdInput);
621 fprintf(stderr,"%S: setValue failed to open key %s\n",
622 getAppName(), stdInputA);
623 HeapFree(GetProcessHeap(), 0, stdInputA);
624 }
625 }
626 else if( currentKeyHandle &&
627 (( stdInput[0] == '@') || /* reading a default @=data pair */
628 ( stdInput[0] == '\"'))) /* reading a new value=data pair */
629 {
630 processSetValue(stdInput, isUnicode);
631 }
632 else
633 {
634 /* Since we are assuming that the file format is valid we must be
635 * reading a blank line which indicates the end of this key processing
636 */
637 closeKey();
638 }
639 }
640
641 /******************************************************************************
642 * Processes a registry file.
643 * Correctly processes comments (in # form), line continuation.
644 *
645 * Parameters:
646 * in - input stream to read from
647 */
648 static void processRegLinesA(FILE *in)
649 {
650 LPSTR line = NULL; /* line read from input stream */
651 ULONG lineSize = REG_VAL_BUF_SIZE;
652
653 line = HeapAlloc(GetProcessHeap(), 0, lineSize);
654 CHECK_ENOUGH_MEMORY(line);
655
656 while (!feof(in))
657 {
658 LPSTR s; /* The pointer into line for where the current fgets should read */
659 LPSTR check;
660 WCHAR* lineW;
661 s = line;
662
663 for (;;)
664 {
665 size_t size_remaining;
666 int size_to_get;
667 char *s_eol; /* various local uses */
668
669 /* Do we need to expand the buffer ? */
670 assert (s >= line && s <= line + lineSize);
671 size_remaining = lineSize - (s-line);
672 if (size_remaining < 2) /* room for 1 character and the \0 */
673 {
674 char *new_buffer;
675 size_t new_size = lineSize + REG_VAL_BUF_SIZE;
676 if (new_size > lineSize) /* no arithmetic overflow */
677 new_buffer = HeapReAlloc (GetProcessHeap(), 0, line, new_size);
678 else
679 new_buffer = NULL;
680 CHECK_ENOUGH_MEMORY(new_buffer);
681 line = new_buffer;
682 s = line + lineSize - size_remaining;
683 lineSize = new_size;
684 size_remaining = lineSize - (s-line);
685 }
686
687 /* Get as much as possible into the buffer, terminated either by
688 * eof, error, eol or getting the maximum amount. Abort on error.
689 */
690 size_to_get = (size_remaining > INT_MAX ? INT_MAX : size_remaining);
691
692 check = fgets (s, size_to_get, in);
693
694 if (check == NULL)
695 {
696 if (ferror(in))
697 {
698 perror ("While reading input");
699 exit (IO_ERROR);
700 }
701 else
702 {
703 assert (feof(in));
704 *s = '\0';
705 /* It is not clear to me from the definition that the
706 * contents of the buffer are well defined on detecting
707 * an eof without managing to read anything.
708 */
709 }
710 }
711
712 /* If we didn't read the eol nor the eof go around for the rest */
713 s_eol = strchr (s, '\n');
714 if (!feof (in) && !s_eol)
715 {
716 s = strchr (s, '\0');
717 /* It should be s + size_to_get - 1 but this is safer */
718 continue;
719 }
720
721 /* If it is a comment line then discard it and go around again */
722 if (line [0] == '#')
723 {
724 s = line;
725 continue;
726 }
727
728 /* Remove any line feed. Leave s_eol on the \0 */
729 if (s_eol)
730 {
731 *s_eol = '\0';
732 if (s_eol > line && *(s_eol-1) == '\r')
733 *--s_eol = '\0';
734 }
735 else
736 s_eol = strchr (s, '\0');
737
738 /* If there is a concatenating \\ then go around again */
739 if (s_eol > line && *(s_eol-1) == '\\')
740 {
741 int c;
742 s = s_eol-1;
743
744 do
745 {
746 c = fgetc(in);
747 }
748 while(c == ' ' || c == '\t');
749
750 if(c == EOF)
751 {
752 fprintf(stderr,"%S: ERROR - invalid continuation.\n",
753 getAppName());
754 }
755 else
756 {
757 *s = c;
758 s++;
759 }
760 continue;
761 }
762
763 lineW = GetWideString(line);
764
765 break; /* That is the full virtual line */
766 }
767
768 processRegEntry(lineW, FALSE);
769 HeapFree(GetProcessHeap(), 0, lineW);
770 }
771 processRegEntry(NULL, FALSE);
772
773 HeapFree(GetProcessHeap(), 0, line);
774 }
775
776 static void processRegLinesW(FILE *in)
777 {
778 WCHAR* buf = NULL; /* line read from input stream */
779 ULONG lineSize = REG_VAL_BUF_SIZE;
780 size_t CharsInBuf = -1;
781
782 WCHAR* s; /* The pointer into buf for where the current fgets should read */
783 WCHAR* line; /* The start of the current line */
784
785 buf = HeapAlloc(GetProcessHeap(), 0, lineSize * sizeof(WCHAR));
786 CHECK_ENOUGH_MEMORY(buf);
787
788 s = buf;
789 line = buf;
790
791 while(!feof(in))
792 {
793 size_t size_remaining;
794 int size_to_get;
795 WCHAR *s_eol = NULL; /* various local uses */
796
797 /* Do we need to expand the buffer ? */
798 assert (s >= buf && s <= buf + lineSize);
799 size_remaining = lineSize - (s-buf);
800 if (size_remaining < 2) /* room for 1 character and the \0 */
801 {
802 WCHAR *new_buffer;
803 size_t new_size = lineSize + (REG_VAL_BUF_SIZE / sizeof(WCHAR));
804 if (new_size > lineSize) /* no arithmetic overflow */
805 new_buffer = HeapReAlloc (GetProcessHeap(), 0, buf, new_size * sizeof(WCHAR));
806 else
807 new_buffer = NULL;
808 CHECK_ENOUGH_MEMORY(new_buffer);
809 buf = new_buffer;
810 line = buf;
811 s = buf + lineSize - size_remaining;
812 lineSize = new_size;
813 size_remaining = lineSize - (s-buf);
814 }
815
816 /* Get as much as possible into the buffer, terminated either by
817 * eof, error or getting the maximum amount. Abort on error.
818 */
819 size_to_get = (size_remaining > INT_MAX ? INT_MAX : size_remaining);
820
821 CharsInBuf = fread(s, sizeof(WCHAR), size_to_get - 1, in);
822 s[CharsInBuf] = 0;
823
824 if (CharsInBuf == 0)
825 {
826 if (ferror(in))
827 {
828 perror ("While reading input");
829 exit (IO_ERROR);
830 }
831 else
832 {
833 assert (feof(in));
834 *s = '\0';
835 /* It is not clear to me from the definition that the
836 * contents of the buffer are well defined on detecting
837 * an eof without managing to read anything.
838 */
839 }
840 }
841
842 /* If we didn't read the eol nor the eof go around for the rest */
843 while(1)
844 {
845 s_eol = wcschr(line, '\n');
846
847 if(!s_eol)
848 {
849 /* Move the stub of the line to the start of the buffer so
850 * we get the maximum space to read into, and so we don't
851 * have to recalculate 'line' if the buffer expands */
852 MoveMemory(buf, line, (lstrlenW(line) + 1) * sizeof(WCHAR));
853 line = buf;
854 s = wcschr(line, '\0');
855 break;
856 }
857
858 /* If it is a comment line then discard it and go around again */
859 if (*line == '#')
860 {
861 line = s_eol + 1;
862 continue;
863 }
864
865 /* If there is a concatenating \\ then go around again */
866 if ((*(s_eol-1) == '\\') ||
867 (*(s_eol-1) == '\r' && *(s_eol-2) == '\\'))
868 {
869 WCHAR* NextLine = s_eol;
870
871 while(*(NextLine+1) == ' ' || *(NextLine+1) == '\t')
872 NextLine++;
873
874 NextLine++;
875
876 if(*(s_eol-1) == '\r')
877 s_eol--;
878
879 MoveMemory(s_eol - 1, NextLine, (CharsInBuf - (NextLine - s) + 1)*sizeof(WCHAR));
880 CharsInBuf -= NextLine - s_eol + 1;
881 s_eol = 0;
882 continue;
883 }
884
885 /* Remove any line feed. Leave s_eol on the \0 */
886 if (s_eol)
887 {
888 *s_eol = '\0';
889 if (s_eol > buf && *(s_eol-1) == '\r')
890 *(s_eol-1) = '\0';
891 }
892
893 if(!s_eol)
894 break;
895
896 processRegEntry(line, TRUE);
897 line = s_eol + 1;
898 s_eol = 0;
899 continue; /* That is the full virtual line */
900 }
901 }
902
903 processRegEntry(NULL, TRUE);
904
905 HeapFree(GetProcessHeap(), 0, buf);
906 }
907
908 /****************************************************************************
909 * REGPROC_print_error
910 *
911 * Print the message for GetLastError
912 */
913
914 static void REGPROC_print_error(void)
915 {
916 LPVOID lpMsgBuf;
917 DWORD error_code;
918 int status;
919
920 error_code = GetLastError ();
921 status = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
922 NULL, error_code, 0, (LPTSTR) &lpMsgBuf, 0, NULL);
923 if (!status)
924 {
925 fprintf(stderr,"%S: Cannot display message for error %ld, status %ld\n",
926 getAppName(), error_code, GetLastError());
927 exit(1);
928 }
929 puts(lpMsgBuf);
930 LocalFree(lpMsgBuf);
931 exit(1);
932 }
933
934 /******************************************************************************
935 * Checks whether the buffer has enough room for the string or required size.
936 * Resizes the buffer if necessary.
937 *
938 * Parameters:
939 * buffer - pointer to a buffer for string
940 * len - current length of the buffer in characters.
941 * required_len - length of the string to place to the buffer in characters.
942 * The length does not include the terminating null character.
943 */
944 static void REGPROC_resize_char_buffer(WCHAR **buffer, DWORD *len, DWORD required_len)
945 {
946 required_len++;
947 if (required_len > *len)
948 {
949 *len = required_len;
950 if (!*buffer)
951 *buffer = HeapAlloc(GetProcessHeap(), 0, *len * sizeof(**buffer));
952 else
953 *buffer = HeapReAlloc(GetProcessHeap(), 0, *buffer, *len * sizeof(**buffer));
954 CHECK_ENOUGH_MEMORY(*buffer);
955 }
956 }
957
958 /******************************************************************************
959 * Same as REGPROC_resize_char_buffer() but on a regular buffer.
960 *
961 * Parameters:
962 * buffer - pointer to a buffer
963 * len - current size of the buffer in bytes
964 * required_size - size of the data to place in the buffer in bytes
965 */
966 static void REGPROC_resize_binary_buffer(BYTE **buffer, DWORD *size, DWORD required_size)
967 {
968 if (required_size > *size)
969 {
970 *size = required_size;
971 if (!*buffer)
972 *buffer = HeapAlloc(GetProcessHeap(), 0, *size);
973 else
974 *buffer = HeapReAlloc(GetProcessHeap(), 0, *buffer, *size);
975 CHECK_ENOUGH_MEMORY(*buffer);
976 }
977 }
978
979 /******************************************************************************
980 * Prints string str to file
981 */
982 static void REGPROC_export_string(WCHAR **line_buf, DWORD *line_buf_size, DWORD *line_len, WCHAR *str, DWORD str_len)
983 {
984 DWORD i, pos;
985 DWORD extra = 0;
986
987 REGPROC_resize_char_buffer(line_buf, line_buf_size, *line_len + str_len + 10);
988
989 /* escaping characters */
990 pos = *line_len;
991 for (i = 0; i < str_len; i++)
992 {
993 WCHAR c = str[i];
994 switch (c)
995 {
996 case '\n':
997 extra++;
998 REGPROC_resize_char_buffer(line_buf, line_buf_size, *line_len + str_len + extra);
999 (*line_buf)[pos++] = '\\';
1000 (*line_buf)[pos++] = 'n';
1001 break;
1002
1003 case '\\':
1004 case '"':
1005 extra++;
1006 REGPROC_resize_char_buffer(line_buf, line_buf_size, *line_len + str_len + extra);
1007 (*line_buf)[pos++] = '\\';
1008 /* Fall through */
1009
1010 default:
1011 (*line_buf)[pos++] = c;
1012 break;
1013 }
1014 }
1015 (*line_buf)[pos] = '\0';
1016 *line_len = pos;
1017 }
1018
1019 static void REGPROC_export_binary(WCHAR **line_buf, DWORD *line_buf_size, DWORD *line_len, DWORD type, BYTE *value, DWORD value_size, BOOL unicode)
1020 {
1021 DWORD hex_pos, data_pos;
1022 const WCHAR *hex_prefix;
1023 const WCHAR hex[] = {'h','e','x',':',0};
1024 WCHAR hex_buf[17];
1025 const WCHAR concat[] = {'\\','\n',' ',' ',0};
1026 DWORD concat_prefix, concat_len;
1027 const WCHAR newline[] = {'\n',0};
1028 CHAR* value_multibyte = NULL;
1029
1030 if (type == REG_BINARY)
1031 {
1032 hex_prefix = hex;
1033 }
1034 else
1035 {
1036 const WCHAR hex_format[] = {'h','e','x','(','%','u',')',':',0};
1037 hex_prefix = hex_buf;
1038 wsprintfW(hex_buf, hex_format, type);
1039 if ((type == REG_SZ || type == REG_EXPAND_SZ || type == REG_MULTI_SZ) && !unicode)
1040 {
1041 value_multibyte = GetMultiByteStringN((WCHAR*)value, value_size / sizeof(WCHAR), &value_size);
1042 value = (BYTE*)value_multibyte;
1043 }
1044 }
1045
1046 concat_len = lstrlenW(concat);
1047 concat_prefix = 2;
1048
1049 hex_pos = *line_len;
1050 *line_len += lstrlenW(hex_prefix);
1051 data_pos = *line_len;
1052 *line_len += value_size * 3;
1053 /* - The 2 spaces that concat places at the start of the
1054 * line effectively reduce the space available for data.
1055 * - If the value name and hex prefix are very long
1056 * ( > REG_FILE_HEX_LINE_LEN) then we may overestimate
1057 * the needed number of lines by one. But that's ok.
1058 * - The trailing linefeed takes the place of a comma so
1059 * it's accounted for already.
1060 */
1061 *line_len += *line_len / (REG_FILE_HEX_LINE_LEN - concat_prefix) * concat_len;
1062 REGPROC_resize_char_buffer(line_buf, line_buf_size, *line_len);
1063 lstrcpyW(*line_buf + hex_pos, hex_prefix);
1064 if (value_size)
1065 {
1066 const WCHAR format[] = {'%','0','2','x',0};
1067 DWORD i, column;
1068
1069 column = data_pos; /* no line wrap yet */
1070 i = 0;
1071 while (1)
1072 {
1073 wsprintfW(*line_buf + data_pos, format, (unsigned int)value[i]);
1074 data_pos += 2;
1075 if (++i == value_size)
1076 break;
1077
1078 (*line_buf)[data_pos++] = ',';
1079 column += 3;
1080
1081 /* wrap the line */
1082 if (column >= REG_FILE_HEX_LINE_LEN)
1083 {
1084 lstrcpyW(*line_buf + data_pos, concat);
1085 data_pos += concat_len;
1086 column = concat_prefix;
1087 }
1088 }
1089 }
1090 lstrcpyW(*line_buf + data_pos, newline);
1091 HeapFree(GetProcessHeap(), 0, value_multibyte);
1092 }
1093
1094 /******************************************************************************
1095 * Writes the given line to a file, in multi-byte or wide characters
1096 */
1097 static void REGPROC_write_line(FILE *file, const WCHAR* str, BOOL unicode)
1098 {
1099 int i;
1100 if (unicode)
1101 {
1102 for(i = 0; str[i]; i++)
1103 {
1104 if (str[i] == L'\n')
1105 fputwc(L'\r', file);
1106 fputwc(str[i], file);
1107 }
1108 }
1109 else
1110 {
1111 char* strA = GetMultiByteString(str);
1112 fputs(strA, file);
1113 HeapFree(GetProcessHeap(), 0, strA);
1114 }
1115 }
1116
1117 /******************************************************************************
1118 * Writes contents of the registry key to the specified file stream.
1119 *
1120 * Parameters:
1121 * file - writable file stream to export registry branch to.
1122 * key - registry branch to export.
1123 * reg_key_name_buf - name of the key with registry class.
1124 * Is resized if necessary.
1125 * reg_key_name_size - length of the buffer for the registry class in characters.
1126 * val_name_buf - buffer for storing value name.
1127 * Is resized if necessary.
1128 * val_name_size - length of the buffer for storing value names in characters.
1129 * val_buf - buffer for storing values while extracting.
1130 * Is resized if necessary.
1131 * val_size - size of the buffer for storing values in bytes.
1132 */
1133 static void export_hkey(FILE *file, HKEY key,
1134 WCHAR **reg_key_name_buf, DWORD *reg_key_name_size,
1135 WCHAR **val_name_buf, DWORD *val_name_size,
1136 BYTE **val_buf, DWORD *val_size,
1137 WCHAR **line_buf, DWORD *line_buf_size,
1138 BOOL unicode)
1139 {
1140 DWORD max_sub_key_len;
1141 DWORD max_val_name_len;
1142 DWORD max_val_size;
1143 DWORD curr_len;
1144 DWORD i;
1145 BOOL more_data;
1146 LONG ret;
1147 WCHAR key_format[] = {'\n','[','%','s',']','\n',0};
1148
1149 /* get size information and resize the buffers if necessary */
1150 if (RegQueryInfoKeyW(key, NULL, NULL, NULL, NULL,
1151 &max_sub_key_len, NULL,
1152 NULL, &max_val_name_len, &max_val_size, NULL, NULL
1153 ) != ERROR_SUCCESS)
1154 {
1155 REGPROC_print_error();
1156 }
1157 curr_len = lstrlenW(*reg_key_name_buf);
1158 REGPROC_resize_char_buffer(reg_key_name_buf, reg_key_name_size,
1159 max_sub_key_len + curr_len + 1);
1160 REGPROC_resize_char_buffer(val_name_buf, val_name_size,
1161 max_val_name_len);
1162 REGPROC_resize_binary_buffer(val_buf, val_size, max_val_size);
1163 REGPROC_resize_char_buffer(line_buf, line_buf_size, lstrlenW(*reg_key_name_buf) + 4);
1164 /* output data for the current key */
1165 wsprintfW(*line_buf, key_format, *reg_key_name_buf);
1166 REGPROC_write_line(file, *line_buf, unicode);
1167
1168 /* print all the values */
1169 i = 0;
1170 more_data = TRUE;
1171 while(more_data)
1172 {
1173 DWORD value_type;
1174 DWORD val_name_size1 = *val_name_size;
1175 DWORD val_size1 = *val_size;
1176 ret = RegEnumValueW(key, i, *val_name_buf, &val_name_size1, NULL,
1177 &value_type, *val_buf, &val_size1);
1178 if (ret == ERROR_MORE_DATA)
1179 {
1180 /* Increase the size of the buffers and retry */
1181 REGPROC_resize_char_buffer(val_name_buf, val_name_size, val_name_size1);
1182 REGPROC_resize_binary_buffer(val_buf, val_size, val_size1);
1183 }
1184 else if (ret != ERROR_SUCCESS)
1185 {
1186 more_data = FALSE;
1187 if (ret != ERROR_NO_MORE_ITEMS)
1188 {
1189 REGPROC_print_error();
1190 }
1191 }
1192 else
1193 {
1194 DWORD line_len;
1195 i++;
1196
1197 if ((*val_name_buf)[0])
1198 {
1199 const WCHAR val_start[] = {'"','%','s','"','=',0};
1200
1201 line_len = 0;
1202 REGPROC_export_string(line_buf, line_buf_size, &line_len, *val_name_buf, lstrlenW(*val_name_buf));
1203 REGPROC_resize_char_buffer(val_name_buf, val_name_size, lstrlenW(*line_buf) + 1);
1204 lstrcpyW(*val_name_buf, *line_buf);
1205
1206 line_len = 3 + lstrlenW(*val_name_buf);
1207 REGPROC_resize_char_buffer(line_buf, line_buf_size, line_len);
1208 wsprintfW(*line_buf, val_start, *val_name_buf);
1209 }
1210 else
1211 {
1212 const WCHAR std_val[] = {'@','=',0};
1213 line_len = 2;
1214 REGPROC_resize_char_buffer(line_buf, line_buf_size, line_len);
1215 lstrcpyW(*line_buf, std_val);
1216 }
1217
1218 switch (value_type)
1219 {
1220 case REG_SZ:
1221 {
1222 WCHAR* wstr = (WCHAR*)*val_buf;
1223
1224 if (val_size1 < sizeof(WCHAR) || val_size1 % sizeof(WCHAR) ||
1225 wstr[val_size1 / sizeof(WCHAR) - 1])
1226 {
1227 REGPROC_export_binary(line_buf, line_buf_size, &line_len, value_type, *val_buf, val_size1, unicode);
1228 }
1229 else
1230 {
1231 const WCHAR start[] = {'"',0};
1232 const WCHAR end[] = {'"','\n',0};
1233 DWORD len;
1234
1235 len = lstrlenW(start);
1236 REGPROC_resize_char_buffer(line_buf, line_buf_size, line_len + len);
1237 lstrcpyW(*line_buf + line_len, start);
1238 line_len += len;
1239
1240 /* At this point we know wstr is '\0'-terminated
1241 * so we can substract 1 from the size
1242 */
1243 REGPROC_export_string(line_buf, line_buf_size, &line_len, wstr, val_size1 / sizeof(WCHAR) - 1);
1244
1245 REGPROC_resize_char_buffer(line_buf, line_buf_size, line_len + lstrlenW(end));
1246 lstrcpyW(*line_buf + line_len, end);
1247 }
1248 break;
1249 }
1250
1251 case REG_DWORD:
1252 {
1253 WCHAR format[] = {'d','w','o','r','d',':','%','0','8','x','\n',0};
1254
1255 REGPROC_resize_char_buffer(line_buf, line_buf_size, line_len + 15);
1256 wsprintfW(*line_buf + line_len, format, *((DWORD *)*val_buf));
1257 break;
1258 }
1259
1260 default:
1261 {
1262 char* key_nameA = GetMultiByteString(*reg_key_name_buf);
1263 char* value_nameA = GetMultiByteString(*val_name_buf);
1264 fprintf(stderr,"%S: warning - unsupported registry format '%ld', "
1265 "treat as binary\n",
1266 getAppName(), value_type);
1267 fprintf(stderr,"key name: \"%s\"\n", key_nameA);
1268 fprintf(stderr,"value name:\"%s\"\n\n", value_nameA);
1269 HeapFree(GetProcessHeap(), 0, key_nameA);
1270 HeapFree(GetProcessHeap(), 0, value_nameA);
1271 }
1272 /* falls through */
1273 case REG_EXPAND_SZ:
1274 case REG_MULTI_SZ:
1275 /* falls through */
1276 case REG_BINARY:
1277 REGPROC_export_binary(line_buf, line_buf_size, &line_len, value_type, *val_buf, val_size1, unicode);
1278 }
1279 REGPROC_write_line(file, *line_buf, unicode);
1280 }
1281 }
1282
1283 i = 0;
1284 more_data = TRUE;
1285 (*reg_key_name_buf)[curr_len] = '\\';
1286 while(more_data)
1287 {
1288 DWORD buf_size = *reg_key_name_size - curr_len - 1;
1289
1290 ret = RegEnumKeyExW(key, i, *reg_key_name_buf + curr_len + 1, &buf_size,
1291 NULL, NULL, NULL, NULL);
1292 if (ret == ERROR_MORE_DATA)
1293 {
1294 /* Increase the size of the buffer and retry */
1295 REGPROC_resize_char_buffer(reg_key_name_buf, reg_key_name_size, curr_len + 1 + buf_size);
1296 }
1297 else if (ret != ERROR_SUCCESS)
1298 {
1299 more_data = FALSE;
1300 if (ret != ERROR_NO_MORE_ITEMS)
1301 {
1302 REGPROC_print_error();
1303 }
1304 }
1305 else
1306 {
1307 HKEY subkey;
1308
1309 i++;
1310 if (RegOpenKeyW(key, *reg_key_name_buf + curr_len + 1,
1311 &subkey) == ERROR_SUCCESS)
1312 {
1313 export_hkey(file, subkey, reg_key_name_buf, reg_key_name_size,
1314 val_name_buf, val_name_size, val_buf, val_size,
1315 line_buf, line_buf_size, unicode);
1316 RegCloseKey(subkey);
1317 }
1318 else
1319 {
1320 REGPROC_print_error();
1321 }
1322 }
1323 }
1324 (*reg_key_name_buf)[curr_len] = '\0';
1325 }
1326
1327 /******************************************************************************
1328 * Open file for export.
1329 */
1330 static FILE *REGPROC_open_export_file(WCHAR *file_name, BOOL unicode)
1331 {
1332 FILE *file;
1333 WCHAR dash = '-';
1334
1335 if (wcsncmp(file_name, &dash, 1) == 0)
1336 file = stdout;
1337 else
1338 {
1339 if (unicode)
1340 file = _wfopen(file_name, L"wb");
1341 else
1342 file = _wfopen(file_name, L"w");
1343 if (!file)
1344 {
1345 CHAR* file_nameA = GetMultiByteString(file_name);
1346 perror("");
1347 fprintf(stderr,"%S: Can't open file \"%s\"\n", getAppName(), file_nameA);
1348 HeapFree(GetProcessHeap(), 0, file_nameA);
1349 exit(1);
1350 }
1351 }
1352 if (unicode)
1353 {
1354 const BYTE unicode_seq[] = {0xff,0xfe};
1355 const WCHAR header[] = L"Windows Registry Editor Version 5.00\r\n";
1356 fwrite(unicode_seq, sizeof(BYTE), COUNT_OF(unicode_seq), file);
1357 fwrite(header, sizeof(WCHAR), lstrlenW(header), file);
1358 }
1359 else
1360 {
1361 fputs("REGEDIT4\n", file);
1362 }
1363
1364 return file;
1365 }
1366
1367 /******************************************************************************
1368 * Writes contents of the registry key to the specified file stream.
1369 *
1370 * Parameters:
1371 * file_name - name of a file to export registry branch to.
1372 * reg_key_name - registry branch to export. The whole registry is exported if
1373 * reg_key_name is NULL or contains an empty string.
1374 */
1375 BOOL export_registry_key(WCHAR *file_name, WCHAR *reg_key_name, DWORD format)
1376 {
1377 WCHAR *reg_key_name_buf;
1378 WCHAR *val_name_buf;
1379 BYTE *val_buf;
1380 WCHAR *line_buf;
1381 DWORD reg_key_name_size = KEY_MAX_LEN;
1382 DWORD val_name_size = KEY_MAX_LEN;
1383 DWORD val_size = REG_VAL_BUF_SIZE;
1384 DWORD line_buf_size = KEY_MAX_LEN + REG_VAL_BUF_SIZE;
1385 FILE *file = NULL;
1386 BOOL unicode = (format == REG_FORMAT_5);
1387
1388 reg_key_name_buf = HeapAlloc(GetProcessHeap(), 0,
1389 reg_key_name_size * sizeof(*reg_key_name_buf));
1390 val_name_buf = HeapAlloc(GetProcessHeap(), 0,
1391 val_name_size * sizeof(*val_name_buf));
1392 val_buf = HeapAlloc(GetProcessHeap(), 0, val_size);
1393 line_buf = HeapAlloc(GetProcessHeap(), 0, line_buf_size * sizeof(*line_buf));
1394 CHECK_ENOUGH_MEMORY(reg_key_name_buf && val_name_buf && val_buf && line_buf);
1395
1396 if (reg_key_name && reg_key_name[0])
1397 {
1398 HKEY reg_key_class;
1399 WCHAR *branch_name = NULL;
1400 HKEY key;
1401
1402 REGPROC_resize_char_buffer(&reg_key_name_buf, &reg_key_name_size,
1403 lstrlenW(reg_key_name));
1404 lstrcpyW(reg_key_name_buf, reg_key_name);
1405
1406 /* open the specified key */
1407 if (!parseKeyName(reg_key_name, &reg_key_class, &branch_name))
1408 {
1409 CHAR* key_nameA = GetMultiByteString(reg_key_name);
1410 fprintf(stderr,"%S: Incorrect registry class specification in '%s'\n",
1411 getAppName(), key_nameA);
1412 HeapFree(GetProcessHeap(), 0, key_nameA);
1413 exit(1);
1414 }
1415 if (!branch_name[0])
1416 {
1417 /* no branch - registry class is specified */
1418 file = REGPROC_open_export_file(file_name, unicode);
1419 export_hkey(file, reg_key_class,
1420 &reg_key_name_buf, &reg_key_name_size,
1421 &val_name_buf, &val_name_size,
1422 &val_buf, &val_size, &line_buf,
1423 &line_buf_size, unicode);
1424 }
1425 else if (RegOpenKeyW(reg_key_class, branch_name, &key) == ERROR_SUCCESS)
1426 {
1427 file = REGPROC_open_export_file(file_name, unicode);
1428 export_hkey(file, key,
1429 &reg_key_name_buf, &reg_key_name_size,
1430 &val_name_buf, &val_name_size,
1431 &val_buf, &val_size, &line_buf,
1432 &line_buf_size, unicode);
1433 RegCloseKey(key);
1434 }
1435 else
1436 {
1437 CHAR* key_nameA = GetMultiByteString(reg_key_name);
1438 fprintf(stderr,"%S: Can't export. Registry key '%s' does not exist!\n",
1439 getAppName(), key_nameA);
1440 HeapFree(GetProcessHeap(), 0, key_nameA);
1441 REGPROC_print_error();
1442 }
1443 }
1444 else
1445 {
1446 unsigned int i;
1447
1448 /* export all registry classes */
1449 file = REGPROC_open_export_file(file_name, unicode);
1450 for (i = 0; i < REG_CLASS_NUMBER; i++)
1451 {
1452 /* do not export HKEY_CLASSES_ROOT */
1453 if (reg_class_keys[i] != HKEY_CLASSES_ROOT &&
1454 reg_class_keys[i] != HKEY_CURRENT_USER &&
1455 reg_class_keys[i] != HKEY_CURRENT_CONFIG &&
1456 reg_class_keys[i] != HKEY_DYN_DATA)
1457 {
1458 lstrcpyW(reg_key_name_buf, reg_class_namesW[i]);
1459 export_hkey(file, reg_class_keys[i],
1460 &reg_key_name_buf, &reg_key_name_size,
1461 &val_name_buf, &val_name_size,
1462 &val_buf, &val_size, &line_buf,
1463 &line_buf_size, unicode);
1464 }
1465 }
1466 }
1467
1468 if (file)
1469 {
1470 fclose(file);
1471 }
1472 HeapFree(GetProcessHeap(), 0, reg_key_name);
1473 HeapFree(GetProcessHeap(), 0, val_name_buf);
1474 HeapFree(GetProcessHeap(), 0, val_buf);
1475 HeapFree(GetProcessHeap(), 0, line_buf);
1476 return TRUE;
1477 }
1478
1479 /******************************************************************************
1480 * Reads contents of the specified file into the registry.
1481 */
1482 BOOL import_registry_file(FILE* reg_file)
1483 {
1484 if (reg_file)
1485 {
1486 BYTE s[2];
1487 if (fread( s, 2, 1, reg_file) == 1)
1488 {
1489 if (s[0] == 0xff && s[1] == 0xfe)
1490 {
1491 processRegLinesW(reg_file);
1492 }
1493 else
1494 {
1495 fseek(reg_file, 0, SEEK_SET);
1496 processRegLinesA(reg_file);
1497 }
1498 }
1499 return TRUE;
1500 }
1501 return FALSE;
1502 }
1503
1504 /******************************************************************************
1505 * Removes the registry key with all subkeys. Parses full key name.
1506 *
1507 * Parameters:
1508 * reg_key_name - full name of registry branch to delete. Ignored if is NULL,
1509 * empty, points to register key class, does not exist.
1510 */
1511 void delete_registry_key(WCHAR *reg_key_name)
1512 {
1513 WCHAR *key_name = NULL;
1514 HKEY key_class;
1515
1516 if (!reg_key_name || !reg_key_name[0])
1517 return;
1518
1519 if (!parseKeyName(reg_key_name, &key_class, &key_name))
1520 {
1521 char* reg_key_nameA = GetMultiByteString(reg_key_name);
1522 fprintf(stderr,"%S: Incorrect registry class specification in '%s'\n",
1523 getAppName(), reg_key_nameA);
1524 HeapFree(GetProcessHeap(), 0, reg_key_nameA);
1525 exit(1);
1526 }
1527 if (!*key_name)
1528 {
1529 char* reg_key_nameA = GetMultiByteString(reg_key_name);
1530 fprintf(stderr,"%S: Can't delete registry class '%s'\n",
1531 getAppName(), reg_key_nameA);
1532 HeapFree(GetProcessHeap(), 0, reg_key_nameA);
1533 exit(1);
1534 }
1535
1536 SHDeleteKey(key_class, key_name);
1537 }