wined3d: Allow using more than MAX_COMBINED_SAMPLERS texture image units.
[wine.git] / programs / regedit / regproc.c
blob25c024060c55e380b217699b013a53151a80ab6b
1 /*
2 * Registry processing routines. Routines, common for registry
3 * processing frontends.
5 * Copyright 1999 Sylvain St-Germain
6 * Copyright 2002 Andriy Palamarchuk
7 * Copyright 2008 Alexander N. Sørnes <alex@thehandofagony.com>
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.
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.
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
24 #include <limits.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <fcntl.h>
28 #include <io.h>
29 #include <windows.h>
30 #include <winnt.h>
31 #include <winreg.h>
32 #include <assert.h>
33 #include <wine/unicode.h>
34 #include <wine/debug.h>
35 #include "regproc.h"
37 #define REG_VAL_BUF_SIZE 4096
39 /* maximal number of characters in hexadecimal data line,
40 * including the indentation, but not including the '\' character
42 #define REG_FILE_HEX_LINE_LEN (2 + 25 * 3)
44 extern const WCHAR* reg_class_namesW[];
46 static HKEY reg_class_keys[] = {
47 HKEY_LOCAL_MACHINE, HKEY_USERS, HKEY_CLASSES_ROOT,
48 HKEY_CURRENT_CONFIG, HKEY_CURRENT_USER, HKEY_DYN_DATA
51 #define ARRAY_SIZE(A) (sizeof(A)/sizeof(*A))
53 /* return values */
54 #define NOT_ENOUGH_MEMORY 1
55 #define IO_ERROR 2
57 /* processing macros */
59 /* common check of memory allocation results */
60 #define CHECK_ENOUGH_MEMORY(p) \
61 if (!(p)) \
62 { \
63 output_message(STRING_OUT_OF_MEMORY, __FILE__, __LINE__); \
64 exit(NOT_ENOUGH_MEMORY); \
67 /******************************************************************************
68 * Allocates memory and converts input from multibyte to wide chars
69 * Returned string must be freed by the caller
71 static WCHAR* GetWideString(const char* strA)
73 if(strA)
75 WCHAR* strW;
76 int len = MultiByteToWideChar(CP_ACP, 0, strA, -1, NULL, 0);
78 strW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
79 CHECK_ENOUGH_MEMORY(strW);
80 MultiByteToWideChar(CP_ACP, 0, strA, -1, strW, len);
81 return strW;
83 return NULL;
86 /******************************************************************************
87 * Allocates memory and converts input from multibyte to wide chars
88 * Returned string must be freed by the caller
90 static WCHAR* GetWideStringN(const char* strA, int chars, DWORD *len)
92 if(strA)
94 WCHAR* strW;
95 *len = MultiByteToWideChar(CP_ACP, 0, strA, chars, NULL, 0);
97 strW = HeapAlloc(GetProcessHeap(), 0, *len * sizeof(WCHAR));
98 CHECK_ENOUGH_MEMORY(strW);
99 MultiByteToWideChar(CP_ACP, 0, strA, chars, strW, *len);
100 return strW;
102 *len = 0;
103 return NULL;
106 /******************************************************************************
107 * Allocates memory and converts input from wide chars to multibyte
108 * Returned string must be freed by the caller
110 char* GetMultiByteString(const WCHAR* strW)
112 if(strW)
114 char* strA;
115 int len = WideCharToMultiByte(CP_ACP, 0, strW, -1, NULL, 0, NULL, NULL);
117 strA = HeapAlloc(GetProcessHeap(), 0, len);
118 CHECK_ENOUGH_MEMORY(strA);
119 WideCharToMultiByte(CP_ACP, 0, strW, -1, strA, len, NULL, NULL);
120 return strA;
122 return NULL;
125 /******************************************************************************
126 * Allocates memory and converts input from wide chars to multibyte
127 * Returned string must be freed by the caller
129 static char* GetMultiByteStringN(const WCHAR* strW, int chars, DWORD* len)
131 if(strW)
133 char* strA;
134 *len = WideCharToMultiByte(CP_ACP, 0, strW, chars, NULL, 0, NULL, NULL);
136 strA = HeapAlloc(GetProcessHeap(), 0, *len);
137 CHECK_ENOUGH_MEMORY(strA);
138 WideCharToMultiByte(CP_ACP, 0, strW, chars, strA, *len, NULL, NULL);
139 return strA;
141 *len = 0;
142 return NULL;
145 /******************************************************************************
146 * Converts a hex representation of a DWORD into a DWORD.
148 static BOOL convertHexToDWord(WCHAR* str, DWORD *dw)
150 char buf[9];
151 char dummy;
153 WideCharToMultiByte(CP_ACP, 0, str, -1, buf, 9, NULL, NULL);
154 if (lstrlenW(str) > 8 || sscanf(buf, "%x%c", dw, &dummy) != 1) {
155 output_message(STRING_INVALID_HEX);
156 return FALSE;
158 return TRUE;
161 /******************************************************************************
162 * Converts a hex comma separated values list into a binary string.
164 static BYTE* convertHexCSVToHex(WCHAR *str, DWORD *size)
166 WCHAR *s;
167 BYTE *d, *data;
169 /* The worst case is 1 digit + 1 comma per byte */
170 *size=(lstrlenW(str)+1)/2;
171 data=HeapAlloc(GetProcessHeap(), 0, *size);
172 CHECK_ENOUGH_MEMORY(data);
174 s = str;
175 d = data;
176 *size=0;
177 while (*s != '\0') {
178 UINT wc;
179 WCHAR *end;
181 wc = strtoulW(s,&end,16);
182 if (end == s || wc > 0xff || (*end && *end != ',')) {
183 output_message(STRING_CSV_HEX_ERROR, s);
184 HeapFree(GetProcessHeap(), 0, data);
185 return NULL;
187 *d++ =(BYTE)wc;
188 (*size)++;
189 if (*end) end++;
190 s = end;
193 return data;
196 /******************************************************************************
197 * This function returns the HKEY associated with the data type encoded in the
198 * value. It modifies the input parameter (key value) in order to skip this
199 * "now useless" data type information.
201 * Note: Updated based on the algorithm used in 'server/registry.c'
203 static DWORD getDataType(LPWSTR *lpValue, DWORD* parse_type)
205 struct data_type { const WCHAR *tag; int len; int type; int parse_type; };
207 static const WCHAR quote[] = {'"'};
208 static const WCHAR str[] = {'s','t','r',':','"'};
209 static const WCHAR str2[] = {'s','t','r','(','2',')',':','"'};
210 static const WCHAR hex[] = {'h','e','x',':'};
211 static const WCHAR dword[] = {'d','w','o','r','d',':'};
212 static const WCHAR hexp[] = {'h','e','x','('};
214 static const struct data_type data_types[] = { /* actual type */ /* type to assume for parsing */
215 { quote, 1, REG_SZ, REG_SZ },
216 { str, 5, REG_SZ, REG_SZ },
217 { str2, 8, REG_EXPAND_SZ, REG_SZ },
218 { hex, 4, REG_BINARY, REG_BINARY },
219 { dword, 6, REG_DWORD, REG_DWORD },
220 { hexp, 4, -1, REG_BINARY },
221 { NULL, 0, 0, 0 }
224 const struct data_type *ptr;
225 int type;
227 for (ptr = data_types; ptr->tag; ptr++) {
228 if (strncmpW( ptr->tag, *lpValue, ptr->len ))
229 continue;
231 /* Found! */
232 *parse_type = ptr->parse_type;
233 type=ptr->type;
234 *lpValue+=ptr->len;
235 if (type == -1) {
236 WCHAR* end;
238 /* "hex(xx):" is special */
239 type = (int)strtoulW( *lpValue , &end, 16 );
240 if (**lpValue=='\0' || *end!=')' || *(end+1)!=':') {
241 type=REG_NONE;
242 } else {
243 *lpValue = end + 2;
246 return type;
248 *parse_type=REG_NONE;
249 return REG_NONE;
252 /******************************************************************************
253 * Replaces escape sequences with the characters.
255 static int REGPROC_unescape_string(WCHAR* str)
257 int str_idx = 0; /* current character under analysis */
258 int val_idx = 0; /* the last character of the unescaped string */
259 int len = lstrlenW(str);
260 for (str_idx = 0; str_idx < len; str_idx++, val_idx++) {
261 if (str[str_idx] == '\\') {
262 str_idx++;
263 switch (str[str_idx]) {
264 case 'n':
265 str[val_idx] = '\n';
266 break;
267 case 'r':
268 str[val_idx] = '\r';
269 break;
270 case '0':
271 str[val_idx] = '\0';
272 break;
273 case '\\':
274 case '"':
275 str[val_idx] = str[str_idx];
276 break;
277 default:
278 output_message(STRING_ESCAPE_SEQUENCE, str[str_idx]);
279 str[val_idx] = str[str_idx];
280 break;
282 } else {
283 str[val_idx] = str[str_idx];
286 str[val_idx] = '\0';
287 return val_idx;
290 static BOOL parseKeyName(LPWSTR lpKeyName, HKEY *hKey, LPWSTR *lpKeyPath)
292 WCHAR* lpSlash = NULL;
293 unsigned int i, len;
295 if (lpKeyName == NULL)
296 return FALSE;
298 for(i = 0; *(lpKeyName+i) != 0; i++)
300 if(*(lpKeyName+i) == '\\')
302 lpSlash = lpKeyName+i;
303 break;
307 if (lpSlash)
309 len = lpSlash-lpKeyName;
311 else
313 len = lstrlenW(lpKeyName);
314 lpSlash = lpKeyName+len;
316 *hKey = NULL;
318 for (i = 0; i < ARRAY_SIZE(reg_class_keys); i++) {
319 if (CompareStringW(LOCALE_USER_DEFAULT, 0, lpKeyName, len, reg_class_namesW[i], -1) == CSTR_EQUAL &&
320 len == lstrlenW(reg_class_namesW[i])) {
321 *hKey = reg_class_keys[i];
322 break;
326 if (*hKey == NULL)
327 return FALSE;
330 if (*lpSlash != '\0')
331 lpSlash++;
332 *lpKeyPath = lpSlash;
333 return TRUE;
336 /* Globals used by the setValue() & co */
337 static WCHAR *currentKeyName;
338 static HKEY currentKeyHandle = NULL;
340 /* Registry data types */
341 static const WCHAR type_none[] = {'R','E','G','_','N','O','N','E',0};
342 static const WCHAR type_sz[] = {'R','E','G','_','S','Z',0};
343 static const WCHAR type_expand_sz[] = {'R','E','G','_','E','X','P','A','N','D','_','S','Z',0};
344 static const WCHAR type_binary[] = {'R','E','G','_','B','I','N','A','R','Y',0};
345 static const WCHAR type_dword[] = {'R','E','G','_','D','W','O','R','D',0};
346 static const WCHAR type_dword_le[] = {'R','E','G','_','D','W','O','R','D','_','L','I','T','T','L','E','_','E','N','D','I','A','N',0};
347 static const WCHAR type_dword_be[] = {'R','E','G','_','D','W','O','R','D','_','B','I','G','_','E','N','D','I','A','N',0};
348 static const WCHAR type_multi_sz[] = {'R','E','G','_','M','U','L','T','I','_','S','Z',0};
350 static const struct
352 DWORD type;
353 const WCHAR *name;
355 type_rels[] =
357 {REG_NONE, type_none},
358 {REG_SZ, type_sz},
359 {REG_EXPAND_SZ, type_expand_sz},
360 {REG_BINARY, type_binary},
361 {REG_DWORD, type_dword},
362 {REG_DWORD_LITTLE_ENDIAN, type_dword_le},
363 {REG_DWORD_BIG_ENDIAN, type_dword_be},
364 {REG_MULTI_SZ, type_multi_sz},
367 static const WCHAR *reg_type_to_wchar(DWORD type)
369 int i, array_size = ARRAY_SIZE(type_rels);
371 for (i = 0; i < array_size; i++)
373 if (type == type_rels[i].type)
374 return type_rels[i].name;
376 return NULL;
379 /******************************************************************************
380 * Sets the value with name val_name to the data in val_data for the currently
381 * opened key.
383 * Parameters:
384 * val_name - name of the registry value
385 * val_data - registry value data
387 static LONG setValue(WCHAR* val_name, WCHAR* val_data, BOOL is_unicode)
389 LONG res;
390 DWORD dwDataType, dwParseType;
391 LPBYTE lpbData;
392 DWORD dwData, dwLen;
393 WCHAR del[] = {'-',0};
395 if ( (val_name == NULL) || (val_data == NULL) )
396 return ERROR_INVALID_PARAMETER;
398 if (lstrcmpW(val_data, del) == 0)
400 res=RegDeleteValueW(currentKeyHandle,val_name);
401 return (res == ERROR_FILE_NOT_FOUND ? ERROR_SUCCESS : res);
404 /* Get the data type stored into the value field */
405 dwDataType = getDataType(&val_data, &dwParseType);
407 if (dwParseType == REG_SZ) /* no conversion for string */
409 dwLen = REGPROC_unescape_string(val_data);
410 if(!dwLen || val_data[dwLen-1] != '"')
411 return ERROR_INVALID_DATA;
412 val_data[dwLen-1] = '\0'; /* remove last quotes */
413 lpbData = (BYTE*) val_data;
414 dwLen = dwLen * sizeof(WCHAR); /* size is in bytes */
416 else if (dwParseType == REG_DWORD) /* Convert the dword types */
418 if (!convertHexToDWord(val_data, &dwData))
419 return ERROR_INVALID_DATA;
420 lpbData = (BYTE*)&dwData;
421 dwLen = sizeof(dwData);
423 else if (dwParseType == REG_BINARY) /* Convert the binary data */
425 lpbData = convertHexCSVToHex(val_data, &dwLen);
426 if (!lpbData)
427 return ERROR_INVALID_DATA;
429 if((dwDataType == REG_MULTI_SZ || dwDataType == REG_EXPAND_SZ) && !is_unicode)
431 LPBYTE tmp = lpbData;
432 lpbData = (LPBYTE)GetWideStringN((char*)lpbData, dwLen, &dwLen);
433 dwLen *= sizeof(WCHAR);
434 HeapFree(GetProcessHeap(), 0, tmp);
437 else /* unknown format */
439 output_message(STRING_UNKNOWN_DATA_FORMAT, reg_type_to_wchar(dwDataType));
440 return ERROR_INVALID_DATA;
443 res = RegSetValueExW(
444 currentKeyHandle,
445 val_name,
446 0, /* Reserved */
447 dwDataType,
448 lpbData,
449 dwLen);
450 if (dwParseType == REG_BINARY)
451 HeapFree(GetProcessHeap(), 0, lpbData);
452 return res;
455 /******************************************************************************
456 * A helper function for processRegEntry() that opens the current key.
457 * That key must be closed by calling closeKey().
459 static LONG openKeyW(WCHAR* stdInput)
461 HKEY keyClass;
462 WCHAR* keyPath;
463 DWORD dwDisp;
464 LONG res;
466 /* Sanity checks */
467 if (stdInput == NULL)
468 return ERROR_INVALID_PARAMETER;
470 /* Get the registry class */
471 if (!parseKeyName(stdInput, &keyClass, &keyPath))
472 return ERROR_INVALID_PARAMETER;
474 res = RegCreateKeyExW(
475 keyClass, /* Class */
476 keyPath, /* Sub Key */
477 0, /* MUST BE 0 */
478 NULL, /* object type */
479 REG_OPTION_NON_VOLATILE, /* option, REG_OPTION_NON_VOLATILE ... */
480 KEY_ALL_ACCESS, /* access mask, KEY_ALL_ACCESS */
481 NULL, /* security attribute */
482 &currentKeyHandle, /* result */
483 &dwDisp); /* disposition, REG_CREATED_NEW_KEY or
484 REG_OPENED_EXISTING_KEY */
486 if (res == ERROR_SUCCESS)
488 currentKeyName = HeapAlloc(GetProcessHeap(), 0, (strlenW(stdInput) + 1) * sizeof(WCHAR));
489 CHECK_ENOUGH_MEMORY(currentKeyName);
490 strcpyW(currentKeyName, stdInput);
492 else
493 currentKeyHandle = NULL;
495 return res;
499 /******************************************************************************
500 * Close the currently opened key.
502 static void closeKey(void)
504 if (currentKeyHandle)
506 HeapFree(GetProcessHeap(), 0, currentKeyName);
507 RegCloseKey(currentKeyHandle);
508 currentKeyHandle = NULL;
512 /******************************************************************************
513 * This function is a wrapper for the setValue function. It prepares the
514 * land and cleans the area once completed.
515 * Note: this function modifies the line parameter.
517 * line - registry file unwrapped line. Should have the registry value name and
518 * complete registry value data.
520 static void processSetValue(WCHAR* line, BOOL is_unicode)
522 WCHAR* val_name; /* registry value name */
523 WCHAR* val_data; /* registry value data */
524 int line_idx = 0; /* current character under analysis */
525 LONG res;
527 /* get value name */
528 while ( isspaceW(line[line_idx]) ) line_idx++;
529 if (line[line_idx] == '@' && line[line_idx + 1] == '=') {
530 line[line_idx] = '\0';
531 val_name = line;
532 line_idx++;
533 } else if (line[line_idx] == '\"') {
534 line_idx++;
535 val_name = line + line_idx;
536 while (line[line_idx]) {
537 if (line[line_idx] == '\\') /* skip escaped character */
539 line_idx += 2;
540 } else {
541 if (line[line_idx] == '\"') {
542 line[line_idx] = '\0';
543 line_idx++;
544 break;
545 } else {
546 line_idx++;
550 while ( isspaceW(line[line_idx]) ) line_idx++;
551 if (!line[line_idx]) {
552 output_message(STRING_UNEXPECTED_EOL, line);
553 return;
555 if (line[line_idx] != '=') {
556 line[line_idx] = '\"';
557 output_message(STRING_UNRECOGNIZED_LINE, line);
558 return;
561 } else {
562 output_message(STRING_UNRECOGNIZED_LINE, line);
563 return;
565 line_idx++; /* skip the '=' character */
567 while ( isspaceW(line[line_idx]) ) line_idx++;
568 val_data = line + line_idx;
569 /* trim trailing blanks */
570 line_idx = strlenW(val_data);
571 while (line_idx > 0 && isspaceW(val_data[line_idx-1])) line_idx--;
572 val_data[line_idx] = '\0';
574 REGPROC_unescape_string(val_name);
575 res = setValue(val_name, val_data, is_unicode);
576 if ( res != ERROR_SUCCESS )
577 output_message(STRING_SETVALUE_FAILED, val_name, currentKeyName);
580 /******************************************************************************
581 * This function receives the currently read entry and performs the
582 * corresponding action.
583 * isUnicode affects parsing of REG_MULTI_SZ values
585 static void processRegEntry(WCHAR* stdInput, BOOL isUnicode)
587 if ( stdInput[0] == '[') /* We are reading a new key */
589 WCHAR* keyEnd;
590 closeKey(); /* Close the previous key */
592 /* Get rid of the square brackets */
593 stdInput++;
594 keyEnd = strrchrW(stdInput, ']');
595 if (keyEnd)
596 *keyEnd='\0';
598 /* delete the key if we encounter '-' at the start of reg key */
599 if (stdInput[0] == '-')
600 delete_registry_key(stdInput + 1);
601 else if (openKeyW(stdInput) != ERROR_SUCCESS)
602 output_message(STRING_OPEN_KEY_FAILED, stdInput);
603 } else if( currentKeyHandle &&
604 (( stdInput[0] == '@') || /* reading a default @=data pair */
605 ( stdInput[0] == '\"'))) /* reading a new value=data pair */
607 processSetValue(stdInput, isUnicode);
611 /* version for Windows 3.1 */
612 static void processRegEntry31(WCHAR *line)
614 int key_end = 0;
615 WCHAR *value;
616 int res;
618 static WCHAR empty[] = {0};
619 static WCHAR hkcr[] = {'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T'};
621 if (strncmpW(line, hkcr, sizeof(hkcr) / sizeof(WCHAR))) return;
623 /* get key name */
624 while (line[key_end] && !isspaceW(line[key_end])) key_end++;
626 value = line + key_end;
627 while (isspaceW(value[0])) value++;
629 if (value[0] == '=') value++;
630 if (value[0] == ' ') value++; /* at most one space is skipped */
632 line[key_end] = '\0';
633 if (openKeyW(line) != ERROR_SUCCESS)
634 output_message(STRING_OPEN_KEY_FAILED, line);
636 res = RegSetValueExW(
637 currentKeyHandle,
638 empty,
639 0, /* Reserved */
640 REG_SZ,
641 (BYTE *)value,
642 (strlenW(value) + 1) * sizeof(WCHAR));
643 if (res != ERROR_SUCCESS)
644 output_message(STRING_SETVALUE_FAILED, empty, currentKeyName);
646 closeKey();
649 /* version constants */
651 #define REG_VERSION_31 3
652 #define REG_VERSION_40 4
653 #define REG_VERSION_50 5
655 /******************************************************************************
656 * Processes a registry file.
657 * Correctly processes comments (in # and ; form), line continuation.
659 * Parameters:
660 * in - input stream to read from
661 * first_chars - beginning of stream, read due to Unicode check
663 static void processRegLinesA(FILE *in, char* first_chars)
665 char *buf = NULL; /* the line read from the input stream */
666 unsigned long line_size = REG_VAL_BUF_SIZE;
667 size_t chars_in_buf = -1;
668 char *s; /* A pointer to buf for fread */
669 char *line; /* The start of the current line */
670 WCHAR *lineW;
671 unsigned long version = 0;
673 static const char header_31[] = "REGEDIT";
674 static const char header_40[] = "REGEDIT4";
675 static const char header_50[] = "Windows Registry Editor Version 5.00";
677 buf = HeapAlloc(GetProcessHeap(), 0, line_size);
678 CHECK_ENOUGH_MEMORY(buf);
679 s = buf;
680 line = buf;
682 memcpy(line, first_chars, 2);
684 if (first_chars)
685 s += 2;
687 while (!feof(in)) {
688 size_t size_remaining;
689 int size_to_get;
690 char *s_eol = NULL; /* various local uses */
692 /* Do we need to expand the buffer? */
693 assert(s >= buf && s <= buf + line_size);
694 size_remaining = line_size - (s - buf);
695 if (size_remaining < 3) /* we need at least 3 bytes of room for \r\n\0 */
697 char *new_buffer;
698 size_t new_size = line_size + REG_VAL_BUF_SIZE;
699 if (new_size > line_size) /* no arithmetic overflow */
700 new_buffer = HeapReAlloc(GetProcessHeap(), 0, buf, new_size);
701 else
702 new_buffer = NULL;
703 CHECK_ENOUGH_MEMORY(new_buffer);
704 buf = new_buffer;
705 line = buf;
706 s = buf + line_size - size_remaining;
707 line_size = new_size;
708 size_remaining = line_size - (s - buf);
711 /* Get as much as possible into the buffer, terminating on EOF,
712 * error or once we have read the maximum amount. Abort on error.
714 size_to_get = (size_remaining > INT_MAX ? INT_MAX : size_remaining);
716 chars_in_buf = fread(s, 1, size_to_get - 1, in);
717 s[chars_in_buf] = 0;
719 if (chars_in_buf == 0) {
720 if (ferror(in)) {
721 perror("While reading input");
722 exit(IO_ERROR);
723 } else {
724 assert(feof(in));
725 *s = '\0';
729 /* If we didn't read the end-of-line sequence or EOF, go around again */
730 while (1)
732 s_eol = strpbrk(line, "\r\n");
733 if (!s_eol) {
734 /* Move the stub of the line to the start of the buffer so
735 * we get the maximum space to read into, and so we don't
736 * have to recalculate 'line' if the buffer expands */
737 MoveMemory(buf, line, strlen(line) + 1);
738 line = buf;
739 s = strchr(line, '\0');
740 break;
743 /* If we find a comment line, discard it and go around again */
744 if (line [0] == '#' || line [0] == ';') {
745 if (*s_eol == '\r' && *(s_eol + 1) == '\n')
746 line = s_eol + 2;
747 else
748 line = s_eol + 1;
749 continue;
752 /* If there is a concatenating '\\', go around again */
753 if (*(s_eol - 1) == '\\') {
754 char *next_line = s_eol + 1;
756 if (*s_eol == '\r' && *(s_eol + 1) == '\n')
757 next_line++;
759 while (*(next_line + 1) == ' ' || *(next_line + 1) == '\t')
760 next_line++;
762 MoveMemory(s_eol - 1, next_line, chars_in_buf - (next_line - s) + 1);
763 chars_in_buf -= next_line - s_eol + 1;
764 continue;
767 /* Remove any line feed. Leave s_eol on the last \0 */
768 if (*s_eol == '\r' && *(s_eol + 1) == '\n')
769 *s_eol++ = '\0';
770 *s_eol = '\0';
772 /* Check if the line is a header string */
773 if (!strcmp(line, header_31)) {
774 version = REG_VERSION_31;
775 } else if (!strcmp(line, header_40)) {
776 version = REG_VERSION_40;
777 } else if (!strcmp(line, header_50)) {
778 version = REG_VERSION_50;
779 } else {
780 lineW = GetWideString(line);
781 if (version == REG_VERSION_31) {
782 processRegEntry31(lineW);
783 } else if(version == REG_VERSION_40 || version == REG_VERSION_50) {
784 processRegEntry(lineW, FALSE);
786 HeapFree(GetProcessHeap(), 0, lineW);
788 line = s_eol + 1;
791 closeKey();
793 HeapFree(GetProcessHeap(), 0, buf);
796 static void processRegLinesW(FILE *in)
798 WCHAR* buf = NULL; /* line read from input stream */
799 ULONG lineSize = REG_VAL_BUF_SIZE;
800 size_t CharsInBuf = -1;
802 WCHAR* s; /* The pointer into buf for where the current fgets should read */
803 WCHAR* line; /* The start of the current line */
805 buf = HeapAlloc(GetProcessHeap(), 0, lineSize * sizeof(WCHAR));
806 CHECK_ENOUGH_MEMORY(buf);
808 s = buf;
809 line = buf;
811 while(!feof(in)) {
812 size_t size_remaining;
813 int size_to_get;
814 WCHAR *s_eol = NULL; /* various local uses */
816 /* Do we need to expand the buffer ? */
817 assert (s >= buf && s <= buf + lineSize);
818 size_remaining = lineSize - (s-buf);
819 if (size_remaining < 2) /* room for 1 character and the \0 */
821 WCHAR *new_buffer;
822 size_t new_size = lineSize + (REG_VAL_BUF_SIZE / sizeof(WCHAR));
823 if (new_size > lineSize) /* no arithmetic overflow */
824 new_buffer = HeapReAlloc (GetProcessHeap(), 0, buf, new_size * sizeof(WCHAR));
825 else
826 new_buffer = NULL;
827 CHECK_ENOUGH_MEMORY(new_buffer);
828 buf = new_buffer;
829 line = buf;
830 s = buf + lineSize - size_remaining;
831 lineSize = new_size;
832 size_remaining = lineSize - (s-buf);
835 /* Get as much as possible into the buffer, terminated either by
836 * eof, error or getting the maximum amount. Abort on error.
838 size_to_get = (size_remaining > INT_MAX ? INT_MAX : size_remaining);
840 CharsInBuf = fread(s, sizeof(WCHAR), size_to_get - 1, in);
841 s[CharsInBuf] = 0;
843 if (CharsInBuf == 0) {
844 if (ferror(in)) {
845 perror ("While reading input");
846 exit (IO_ERROR);
847 } else {
848 assert (feof(in));
849 *s = '\0';
850 /* It is not clear to me from the definition that the
851 * contents of the buffer are well defined on detecting
852 * an eof without managing to read anything.
857 /* If we didn't read the eol nor the eof go around for the rest */
858 while(1)
860 const WCHAR line_endings[] = {'\r','\n',0};
861 s_eol = strpbrkW(line, line_endings);
863 if(!s_eol) {
864 /* Move the stub of the line to the start of the buffer so
865 * we get the maximum space to read into, and so we don't
866 * have to recalculate 'line' if the buffer expands */
867 MoveMemory(buf, line, (strlenW(line)+1) * sizeof(WCHAR));
868 line = buf;
869 s = strchrW(line, '\0');
870 break;
873 /* If it is a comment line then discard it and go around again */
874 if (*line == '#' || *line == ';') {
875 if (*s_eol == '\r' && *(s_eol+1) == '\n')
876 line = s_eol + 2;
877 else
878 line = s_eol + 1;
879 continue;
882 /* If there is a concatenating \\ then go around again */
883 if (*(s_eol-1) == '\\') {
884 WCHAR* NextLine = s_eol + 1;
886 if(*s_eol == '\r' && *(s_eol+1) == '\n')
887 NextLine++;
889 while(*(NextLine+1) == ' ' || *(NextLine+1) == '\t')
890 NextLine++;
892 MoveMemory(s_eol - 1, NextLine, (CharsInBuf - (NextLine - s) + 1)*sizeof(WCHAR));
893 CharsInBuf -= NextLine - s_eol + 1;
894 continue;
897 /* Remove any line feed. Leave s_eol on the last \0 */
898 if (*s_eol == '\r' && *(s_eol + 1) == '\n')
899 *s_eol++ = '\0';
900 *s_eol = '\0';
902 processRegEntry(line, TRUE);
903 line = s_eol + 1;
907 closeKey();
909 HeapFree(GetProcessHeap(), 0, buf);
912 /******************************************************************************
913 * Checks whether the buffer has enough room for the string or required size.
914 * Resizes the buffer if necessary.
916 * Parameters:
917 * buffer - pointer to a buffer for string
918 * len - current length of the buffer in characters.
919 * required_len - length of the string to place to the buffer in characters.
920 * The length does not include the terminating null character.
922 static void REGPROC_resize_char_buffer(WCHAR **buffer, DWORD *len, DWORD required_len)
924 required_len++;
925 if (required_len > *len) {
926 *len = required_len;
927 if (!*buffer)
928 *buffer = HeapAlloc(GetProcessHeap(), 0, *len * sizeof(**buffer));
929 else
930 *buffer = HeapReAlloc(GetProcessHeap(), 0, *buffer, *len * sizeof(**buffer));
931 CHECK_ENOUGH_MEMORY(*buffer);
935 /******************************************************************************
936 * Same as REGPROC_resize_char_buffer() but on a regular buffer.
938 * Parameters:
939 * buffer - pointer to a buffer
940 * len - current size of the buffer in bytes
941 * required_size - size of the data to place in the buffer in bytes
943 static void REGPROC_resize_binary_buffer(BYTE **buffer, DWORD *size, DWORD required_size)
945 if (required_size > *size) {
946 *size = required_size;
947 if (!*buffer)
948 *buffer = HeapAlloc(GetProcessHeap(), 0, *size);
949 else
950 *buffer = HeapReAlloc(GetProcessHeap(), 0, *buffer, *size);
951 CHECK_ENOUGH_MEMORY(*buffer);
955 /******************************************************************************
956 * Prints string str to file
958 static void REGPROC_export_string(WCHAR **line_buf, DWORD *line_buf_size, DWORD *line_len, WCHAR *str, DWORD str_len)
960 DWORD i, pos;
961 DWORD extra = 0;
963 REGPROC_resize_char_buffer(line_buf, line_buf_size, *line_len + str_len + 10);
965 /* escaping characters */
966 pos = *line_len;
967 for (i = 0; i < str_len; i++) {
968 WCHAR c = str[i];
969 switch (c) {
970 case '\n':
971 extra++;
972 REGPROC_resize_char_buffer(line_buf, line_buf_size, *line_len + str_len + extra);
973 (*line_buf)[pos++] = '\\';
974 (*line_buf)[pos++] = 'n';
975 break;
977 case '\r':
978 extra++;
979 REGPROC_resize_char_buffer(line_buf, line_buf_size, *line_len + str_len + extra);
980 (*line_buf)[pos++] = '\\';
981 (*line_buf)[pos++] = 'r';
982 break;
984 case '\\':
985 case '"':
986 extra++;
987 REGPROC_resize_char_buffer(line_buf, line_buf_size, *line_len + str_len + extra);
988 (*line_buf)[pos++] = '\\';
989 /* Fall through */
991 default:
992 (*line_buf)[pos++] = c;
993 break;
996 (*line_buf)[pos] = '\0';
997 *line_len = pos;
1000 static void REGPROC_export_binary(WCHAR **line_buf, DWORD *line_buf_size, DWORD *line_len, DWORD type, BYTE *value, DWORD value_size, BOOL unicode)
1002 DWORD hex_pos, data_pos;
1003 const WCHAR *hex_prefix;
1004 const WCHAR hex[] = {'h','e','x',':',0};
1005 WCHAR hex_buf[17];
1006 const WCHAR concat[] = {'\\','\r','\n',' ',' ',0};
1007 DWORD concat_prefix, concat_len;
1008 const WCHAR newline[] = {'\r','\n',0};
1009 CHAR* value_multibyte = NULL;
1011 if (type == REG_BINARY) {
1012 hex_prefix = hex;
1013 } else {
1014 const WCHAR hex_format[] = {'h','e','x','(','%','x',')',':',0};
1015 hex_prefix = hex_buf;
1016 sprintfW(hex_buf, hex_format, type);
1017 if ((type == REG_SZ || type == REG_EXPAND_SZ || type == REG_MULTI_SZ) && !unicode)
1019 value_multibyte = GetMultiByteStringN((WCHAR*)value, value_size / sizeof(WCHAR), &value_size);
1020 value = (BYTE*)value_multibyte;
1024 concat_len = lstrlenW(concat);
1025 concat_prefix = 2;
1027 hex_pos = *line_len;
1028 *line_len += lstrlenW(hex_prefix);
1029 data_pos = *line_len;
1030 *line_len += value_size * 3;
1031 /* - The 2 spaces that concat places at the start of the
1032 * line effectively reduce the space available for data.
1033 * - If the value name and hex prefix are very long
1034 * ( > REG_FILE_HEX_LINE_LEN) or *line_len divides
1035 * without a remainder then we may overestimate
1036 * the needed number of lines by one. But that's ok.
1037 * - The trailing '\r' takes the place of a comma so
1038 * we only need to add 1 for the trailing '\n'
1040 *line_len += *line_len / (REG_FILE_HEX_LINE_LEN - concat_prefix) * concat_len + 1;
1041 REGPROC_resize_char_buffer(line_buf, line_buf_size, *line_len);
1042 lstrcpyW(*line_buf + hex_pos, hex_prefix);
1043 if (value_size)
1045 const WCHAR format[] = {'%','0','2','x',0};
1046 DWORD i, column;
1048 column = data_pos; /* no line wrap yet */
1049 i = 0;
1050 while (1)
1052 sprintfW(*line_buf + data_pos, format, (unsigned int)value[i]);
1053 data_pos += 2;
1054 if (++i == value_size)
1055 break;
1057 (*line_buf)[data_pos++] = ',';
1058 column += 3;
1060 /* wrap the line */
1061 if (column >= REG_FILE_HEX_LINE_LEN) {
1062 lstrcpyW(*line_buf + data_pos, concat);
1063 data_pos += concat_len;
1064 column = concat_prefix;
1068 lstrcpyW(*line_buf + data_pos, newline);
1069 HeapFree(GetProcessHeap(), 0, value_multibyte);
1072 /******************************************************************************
1073 * Writes the given line to a file, in multi-byte or wide characters
1075 static void REGPROC_write_line(FILE *file, const WCHAR* str, BOOL unicode)
1077 if(unicode)
1079 fwrite(str, sizeof(WCHAR), lstrlenW(str), file);
1080 } else
1082 char* strA = GetMultiByteString(str);
1083 fputs(strA, file);
1084 HeapFree(GetProcessHeap(), 0, strA);
1088 /******************************************************************************
1089 * Writes contents of the registry key to the specified file stream.
1091 * Parameters:
1092 * file - writable file stream to export registry branch to.
1093 * key - registry branch to export.
1094 * reg_key_name_buf - name of the key with registry class.
1095 * Is resized if necessary.
1096 * reg_key_name_size - length of the buffer for the registry class in characters.
1097 * val_name_buf - buffer for storing value name.
1098 * Is resized if necessary.
1099 * val_name_size - length of the buffer for storing value names in characters.
1100 * val_buf - buffer for storing values while extracting.
1101 * Is resized if necessary.
1102 * val_size - size of the buffer for storing values in bytes.
1104 static void export_hkey(FILE *file, HKEY key,
1105 WCHAR **reg_key_name_buf, DWORD *reg_key_name_size,
1106 WCHAR **val_name_buf, DWORD *val_name_size,
1107 BYTE **val_buf, DWORD *val_size,
1108 WCHAR **line_buf, DWORD *line_buf_size,
1109 BOOL unicode)
1111 DWORD max_sub_key_len;
1112 DWORD max_val_name_len;
1113 DWORD max_val_size;
1114 DWORD curr_len;
1115 DWORD i;
1116 LONG ret;
1117 WCHAR key_format[] = {'\r','\n','[','%','s',']','\r','\n',0};
1119 /* get size information and resize the buffers if necessary */
1120 if (RegQueryInfoKeyW(key, NULL, NULL, NULL, NULL,
1121 &max_sub_key_len, NULL,
1122 NULL, &max_val_name_len, &max_val_size, NULL, NULL
1123 ) != ERROR_SUCCESS)
1124 return;
1125 curr_len = strlenW(*reg_key_name_buf);
1126 REGPROC_resize_char_buffer(reg_key_name_buf, reg_key_name_size,
1127 max_sub_key_len + curr_len + 1);
1128 REGPROC_resize_char_buffer(val_name_buf, val_name_size,
1129 max_val_name_len);
1130 REGPROC_resize_binary_buffer(val_buf, val_size, max_val_size);
1131 REGPROC_resize_char_buffer(line_buf, line_buf_size, lstrlenW(*reg_key_name_buf) + 4);
1132 /* output data for the current key */
1133 sprintfW(*line_buf, key_format, *reg_key_name_buf);
1134 REGPROC_write_line(file, *line_buf, unicode);
1136 /* print all the values */
1137 i = 0;
1138 for (;;) {
1139 DWORD value_type;
1140 DWORD val_name_size1 = *val_name_size;
1141 DWORD val_size1 = *val_size;
1142 ret = RegEnumValueW(key, i, *val_name_buf, &val_name_size1, NULL,
1143 &value_type, *val_buf, &val_size1);
1144 if (ret == ERROR_MORE_DATA) {
1145 /* Increase the size of the buffers and retry */
1146 REGPROC_resize_char_buffer(val_name_buf, val_name_size, val_name_size1);
1147 REGPROC_resize_binary_buffer(val_buf, val_size, val_size1);
1148 } else if (ret == ERROR_SUCCESS) {
1149 DWORD line_len;
1150 i++;
1152 if ((*val_name_buf)[0]) {
1153 const WCHAR val_start[] = {'"','%','s','"','=',0};
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);
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);
1170 switch (value_type) {
1171 case REG_SZ:
1173 WCHAR* wstr = (WCHAR*)*val_buf;
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;
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;
1188 REGPROC_export_string(line_buf, line_buf_size, &line_len, wstr, lstrlenW(wstr));
1190 REGPROC_resize_char_buffer(line_buf, line_buf_size, line_len + lstrlenW(end));
1191 lstrcpyW(*line_buf + line_len, end);
1193 break;
1196 case REG_DWORD:
1198 WCHAR format[] = {'d','w','o','r','d',':','%','0','8','x','\r','\n',0};
1200 REGPROC_resize_char_buffer(line_buf, line_buf_size, line_len + 15);
1201 sprintfW(*line_buf + line_len, format, *((DWORD *)*val_buf));
1202 break;
1205 default:
1207 output_message(STRING_UNSUPPORTED_TYPE, reg_type_to_wchar(value_type), *reg_key_name_buf);
1208 output_message(STRING_EXPORT_AS_BINARY, *val_name_buf);
1210 /* falls through */
1211 case REG_EXPAND_SZ:
1212 case REG_MULTI_SZ:
1213 /* falls through */
1214 case REG_BINARY:
1215 REGPROC_export_binary(line_buf, line_buf_size, &line_len, value_type, *val_buf, val_size1, unicode);
1217 REGPROC_write_line(file, *line_buf, unicode);
1219 else break;
1222 i = 0;
1223 (*reg_key_name_buf)[curr_len] = '\\';
1224 for (;;) {
1225 DWORD buf_size = *reg_key_name_size - curr_len - 1;
1227 ret = RegEnumKeyExW(key, i, *reg_key_name_buf + curr_len + 1, &buf_size,
1228 NULL, NULL, NULL, NULL);
1229 if (ret == ERROR_MORE_DATA) {
1230 /* Increase the size of the buffer and retry */
1231 REGPROC_resize_char_buffer(reg_key_name_buf, reg_key_name_size, curr_len + 1 + buf_size);
1232 } else if (ret == ERROR_SUCCESS) {
1233 HKEY subkey;
1235 i++;
1236 if (RegOpenKeyW(key, *reg_key_name_buf + curr_len + 1,
1237 &subkey) == ERROR_SUCCESS) {
1238 export_hkey(file, subkey, reg_key_name_buf, reg_key_name_size,
1239 val_name_buf, val_name_size, val_buf, val_size,
1240 line_buf, line_buf_size, unicode);
1241 RegCloseKey(subkey);
1243 else break;
1245 else break;
1247 (*reg_key_name_buf)[curr_len] = '\0';
1250 /******************************************************************************
1251 * Open file in binary mode for export.
1253 static FILE *REGPROC_open_export_file(WCHAR *file_name, BOOL unicode)
1255 FILE *file;
1256 WCHAR dash = '-';
1258 if (strncmpW(file_name,&dash,1)==0) {
1259 file=stdout;
1260 _setmode(_fileno(file), _O_BINARY);
1261 } else
1263 WCHAR wb_mode[] = {'w','b',0};
1264 WCHAR regedit[] = {'r','e','g','e','d','i','t',0};
1266 file = _wfopen(file_name, wb_mode);
1267 if (!file) {
1268 _wperror(regedit);
1269 output_message(STRING_CANNOT_OPEN_FILE, file_name);
1270 exit(1);
1273 if(unicode)
1275 const BYTE unicode_seq[] = {0xff,0xfe};
1276 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'};
1277 fwrite(unicode_seq, sizeof(BYTE), sizeof(unicode_seq)/sizeof(unicode_seq[0]), file);
1278 fwrite(header, sizeof(WCHAR), sizeof(header)/sizeof(header[0]), file);
1279 } else
1281 fputs("REGEDIT4\r\n", file);
1284 return file;
1287 /******************************************************************************
1288 * Writes contents of the registry key to the specified file stream.
1290 * Parameters:
1291 * file_name - name of a file to export registry branch to.
1292 * reg_key_name - registry branch to export. The whole registry is exported if
1293 * reg_key_name is NULL or contains an empty string.
1295 BOOL export_registry_key(WCHAR *file_name, WCHAR *reg_key_name, DWORD format)
1297 WCHAR *reg_key_name_buf;
1298 WCHAR *val_name_buf;
1299 BYTE *val_buf;
1300 WCHAR *line_buf;
1301 DWORD reg_key_name_size = KEY_MAX_LEN;
1302 DWORD val_name_size = KEY_MAX_LEN;
1303 DWORD val_size = REG_VAL_BUF_SIZE;
1304 DWORD line_buf_size = KEY_MAX_LEN + REG_VAL_BUF_SIZE;
1305 FILE *file = NULL;
1306 BOOL unicode = (format == REG_FORMAT_5);
1308 reg_key_name_buf = HeapAlloc(GetProcessHeap(), 0,
1309 reg_key_name_size * sizeof(*reg_key_name_buf));
1310 val_name_buf = HeapAlloc(GetProcessHeap(), 0,
1311 val_name_size * sizeof(*val_name_buf));
1312 val_buf = HeapAlloc(GetProcessHeap(), 0, val_size);
1313 line_buf = HeapAlloc(GetProcessHeap(), 0, line_buf_size * sizeof(*line_buf));
1314 CHECK_ENOUGH_MEMORY(reg_key_name_buf && val_name_buf && val_buf && line_buf);
1316 if (reg_key_name && reg_key_name[0]) {
1317 HKEY reg_key_class;
1318 WCHAR *branch_name = NULL;
1319 HKEY key;
1321 REGPROC_resize_char_buffer(&reg_key_name_buf, &reg_key_name_size,
1322 lstrlenW(reg_key_name));
1323 lstrcpyW(reg_key_name_buf, reg_key_name);
1325 /* open the specified key */
1326 if (!parseKeyName(reg_key_name, &reg_key_class, &branch_name)) {
1327 output_message(STRING_INCORRECT_REG_CLASS, reg_key_name);
1328 exit(1);
1330 if (!branch_name[0]) {
1331 /* no branch - registry class is specified */
1332 file = REGPROC_open_export_file(file_name, unicode);
1333 export_hkey(file, reg_key_class,
1334 &reg_key_name_buf, &reg_key_name_size,
1335 &val_name_buf, &val_name_size,
1336 &val_buf, &val_size, &line_buf,
1337 &line_buf_size, unicode);
1338 } else if (RegOpenKeyW(reg_key_class, branch_name, &key) == ERROR_SUCCESS) {
1339 file = REGPROC_open_export_file(file_name, unicode);
1340 export_hkey(file, key,
1341 &reg_key_name_buf, &reg_key_name_size,
1342 &val_name_buf, &val_name_size,
1343 &val_buf, &val_size, &line_buf,
1344 &line_buf_size, unicode);
1345 RegCloseKey(key);
1346 } else {
1347 output_message(STRING_REG_KEY_NOT_FOUND, reg_key_name);
1349 } else {
1350 unsigned int i;
1352 /* export all registry classes */
1353 file = REGPROC_open_export_file(file_name, unicode);
1354 for (i = 0; i < ARRAY_SIZE(reg_class_keys); i++) {
1355 /* do not export HKEY_CLASSES_ROOT */
1356 if (reg_class_keys[i] != HKEY_CLASSES_ROOT &&
1357 reg_class_keys[i] != HKEY_CURRENT_USER &&
1358 reg_class_keys[i] != HKEY_CURRENT_CONFIG &&
1359 reg_class_keys[i] != HKEY_DYN_DATA) {
1360 lstrcpyW(reg_key_name_buf, reg_class_namesW[i]);
1361 export_hkey(file, reg_class_keys[i],
1362 &reg_key_name_buf, &reg_key_name_size,
1363 &val_name_buf, &val_name_size,
1364 &val_buf, &val_size, &line_buf,
1365 &line_buf_size, unicode);
1370 if (file) {
1371 fclose(file);
1373 HeapFree(GetProcessHeap(), 0, reg_key_name);
1374 HeapFree(GetProcessHeap(), 0, val_name_buf);
1375 HeapFree(GetProcessHeap(), 0, val_buf);
1376 HeapFree(GetProcessHeap(), 0, line_buf);
1377 return TRUE;
1380 /******************************************************************************
1381 * Reads contents of the specified file into the registry.
1383 BOOL import_registry_file(FILE* reg_file)
1385 if (reg_file)
1387 BYTE s[2];
1388 if (fread( s, 2, 1, reg_file) == 1)
1390 if (s[0] == 0xff && s[1] == 0xfe)
1392 processRegLinesW(reg_file);
1393 } else
1395 processRegLinesA(reg_file, (char*)s);
1398 return TRUE;
1400 return FALSE;
1403 /******************************************************************************
1404 * Removes the registry key with all subkeys. Parses full key name.
1406 * Parameters:
1407 * reg_key_name - full name of registry branch to delete. Ignored if is NULL,
1408 * empty, points to register key class, does not exist.
1410 void delete_registry_key(WCHAR *reg_key_name)
1412 WCHAR *key_name = NULL;
1413 HKEY key_class;
1415 if (!reg_key_name || !reg_key_name[0])
1416 return;
1418 if (!parseKeyName(reg_key_name, &key_class, &key_name)) {
1419 output_message(STRING_INCORRECT_REG_CLASS, reg_key_name);
1420 exit(1);
1422 if (!*key_name) {
1423 output_message(STRING_DELETE_REG_CLASS_FAILED, reg_key_name);
1424 exit(1);
1427 RegDeleteTreeW(key_class, key_name);