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