d3dx9: Factor out is_parameter_used() function.
[wine.git] / programs / regedit / regproc.c
blobea0b6f813356a4845767427d9a50de9eba50a413
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 <errno.h>
25 #include <limits.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <fcntl.h>
29 #include <io.h>
30 #include <windows.h>
31 #include <winnt.h>
32 #include <winreg.h>
33 #include <assert.h>
34 #include <wine/unicode.h>
35 #include <wine/debug.h>
36 #include "regproc.h"
38 #define REG_VAL_BUF_SIZE 4096
40 /* maximal number of characters in hexadecimal data line,
41 * including the indentation, but not including the '\' character
43 #define REG_FILE_HEX_LINE_LEN (2 + 25 * 3)
45 extern const WCHAR* reg_class_namesW[];
47 static HKEY reg_class_keys[] = {
48 HKEY_LOCAL_MACHINE, HKEY_USERS, HKEY_CLASSES_ROOT,
49 HKEY_CURRENT_CONFIG, HKEY_CURRENT_USER, HKEY_DYN_DATA
52 #define ARRAY_SIZE(A) (sizeof(A)/sizeof(*A))
54 /******************************************************************************
55 * Allocates memory and converts input from multibyte to wide chars
56 * Returned string must be freed by the caller
58 static WCHAR* GetWideString(const char* strA)
60 if(strA)
62 WCHAR* strW;
63 int len = MultiByteToWideChar(CP_ACP, 0, strA, -1, NULL, 0);
65 strW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
66 CHECK_ENOUGH_MEMORY(strW);
67 MultiByteToWideChar(CP_ACP, 0, strA, -1, strW, len);
68 return strW;
70 return NULL;
73 /******************************************************************************
74 * Allocates memory and converts input from multibyte to wide chars
75 * Returned string must be freed by the caller
77 static WCHAR* GetWideStringN(const char* strA, int chars, DWORD *len)
79 if(strA)
81 WCHAR* strW;
82 *len = MultiByteToWideChar(CP_ACP, 0, strA, chars, NULL, 0);
84 strW = HeapAlloc(GetProcessHeap(), 0, *len * sizeof(WCHAR));
85 CHECK_ENOUGH_MEMORY(strW);
86 MultiByteToWideChar(CP_ACP, 0, strA, chars, strW, *len);
87 return strW;
89 *len = 0;
90 return NULL;
93 /******************************************************************************
94 * Allocates memory and converts input from wide chars to multibyte
95 * Returned string must be freed by the caller
97 char* GetMultiByteString(const WCHAR* strW)
99 if(strW)
101 char* strA;
102 int len = WideCharToMultiByte(CP_ACP, 0, strW, -1, NULL, 0, NULL, NULL);
104 strA = HeapAlloc(GetProcessHeap(), 0, len);
105 CHECK_ENOUGH_MEMORY(strA);
106 WideCharToMultiByte(CP_ACP, 0, strW, -1, strA, len, NULL, NULL);
107 return strA;
109 return NULL;
112 /******************************************************************************
113 * Allocates memory and converts input from wide chars to multibyte
114 * Returned string must be freed by the caller
116 static char* GetMultiByteStringN(const WCHAR* strW, int chars, DWORD* len)
118 if(strW)
120 char* strA;
121 *len = WideCharToMultiByte(CP_ACP, 0, strW, chars, NULL, 0, NULL, NULL);
123 strA = HeapAlloc(GetProcessHeap(), 0, *len);
124 CHECK_ENOUGH_MEMORY(strA);
125 WideCharToMultiByte(CP_ACP, 0, strW, chars, strA, *len, NULL, NULL);
126 return strA;
128 *len = 0;
129 return NULL;
132 static WCHAR *(*get_line)(FILE *);
134 /* parser definitions */
135 enum parser_state
137 HEADER, /* parsing the registry file version header */
138 PARSE_WIN31_LINE, /* parsing a Windows 3.1 registry line */
139 LINE_START, /* at the beginning of a registry line */
140 KEY_NAME, /* parsing a key name */
141 DELETE_KEY, /* deleting a registry key */
142 DEFAULT_VALUE_NAME, /* parsing a default value name */
143 QUOTED_VALUE_NAME, /* parsing a double-quoted value name */
144 DATA_START, /* preparing for data parsing operations */
145 DELETE_VALUE, /* deleting a registry value */
146 DATA_TYPE, /* parsing the registry data type */
147 STRING_DATA, /* parsing REG_SZ data */
148 DWORD_DATA, /* parsing DWORD data */
149 HEX_DATA, /* parsing REG_BINARY, REG_NONE, REG_EXPAND_SZ or REG_MULTI_SZ data */
150 EOL_BACKSLASH, /* preparing to parse multiple lines of hex data */
151 HEX_MULTILINE, /* parsing multiple lines of hex data */
152 UNKNOWN_DATA, /* parsing an unhandled or invalid data type */
153 SET_VALUE, /* adding a value to the registry */
154 NB_PARSER_STATES
157 struct parser
159 FILE *file; /* pointer to a registry file */
160 WCHAR two_wchars[2]; /* first two characters from the encoding check */
161 BOOL is_unicode; /* parsing Unicode or ASCII data */
162 short int reg_version; /* registry file version */
163 HKEY hkey; /* current registry key */
164 WCHAR *key_name; /* current key name */
165 WCHAR *value_name; /* value name */
166 DWORD parse_type; /* generic data type for parsing */
167 DWORD data_type; /* data type */
168 void *data; /* value data */
169 DWORD data_size; /* size of the data (in bytes) */
170 BOOL backslash; /* TRUE if the current line contains a backslash */
171 enum parser_state state; /* current parser state */
174 typedef WCHAR *(*parser_state_func)(struct parser *parser, WCHAR *pos);
176 /* parser state machine functions */
177 static WCHAR *header_state(struct parser *parser, WCHAR *pos);
178 static WCHAR *parse_win31_line_state(struct parser *parser, WCHAR *pos);
179 static WCHAR *line_start_state(struct parser *parser, WCHAR *pos);
180 static WCHAR *key_name_state(struct parser *parser, WCHAR *pos);
181 static WCHAR *delete_key_state(struct parser *parser, WCHAR *pos);
182 static WCHAR *default_value_name_state(struct parser *parser, WCHAR *pos);
183 static WCHAR *quoted_value_name_state(struct parser *parser, WCHAR *pos);
184 static WCHAR *data_start_state(struct parser *parser, WCHAR *pos);
185 static WCHAR *delete_value_state(struct parser *parser, WCHAR *pos);
186 static WCHAR *data_type_state(struct parser *parser, WCHAR *pos);
187 static WCHAR *string_data_state(struct parser *parser, WCHAR *pos);
188 static WCHAR *dword_data_state(struct parser *parser, WCHAR *pos);
189 static WCHAR *hex_data_state(struct parser *parser, WCHAR *pos);
190 static WCHAR *eol_backslash_state(struct parser *parser, WCHAR *pos);
191 static WCHAR *hex_multiline_state(struct parser *parser, WCHAR *pos);
192 static WCHAR *unknown_data_state(struct parser *parser, WCHAR *pos);
193 static WCHAR *set_value_state(struct parser *parser, WCHAR *pos);
195 static const parser_state_func parser_funcs[NB_PARSER_STATES] =
197 header_state, /* HEADER */
198 parse_win31_line_state, /* PARSE_WIN31_LINE */
199 line_start_state, /* LINE_START */
200 key_name_state, /* KEY_NAME */
201 delete_key_state, /* DELETE_KEY */
202 default_value_name_state, /* DEFAULT_VALUE_NAME */
203 quoted_value_name_state, /* QUOTED_VALUE_NAME */
204 data_start_state, /* DATA_START */
205 delete_value_state, /* DELETE_VALUE */
206 data_type_state, /* DATA_TYPE */
207 string_data_state, /* STRING_DATA */
208 dword_data_state, /* DWORD_DATA */
209 hex_data_state, /* HEX_DATA */
210 eol_backslash_state, /* EOL_BACKSLASH */
211 hex_multiline_state, /* HEX_MULTILINE */
212 unknown_data_state, /* UNKNOWN_DATA */
213 set_value_state, /* SET_VALUE */
216 /* set the new parser state and return the previous one */
217 static inline enum parser_state set_state(struct parser *parser, enum parser_state state)
219 enum parser_state ret = parser->state;
220 parser->state = state;
221 return ret;
224 static void *resize_buffer(void *buf, size_t count)
226 void *new_buf;
228 if (buf)
229 new_buf = HeapReAlloc(GetProcessHeap(), 0, buf, count);
230 else
231 new_buf = HeapAlloc(GetProcessHeap(), 0, count);
233 CHECK_ENOUGH_MEMORY(new_buf);
234 return new_buf;
237 /******************************************************************************
238 * Converts a hex representation of a DWORD into a DWORD.
240 static BOOL convert_hex_to_dword(WCHAR *str, DWORD *dw)
242 WCHAR *p, *end;
243 int count = 0;
245 while (*str == ' ' || *str == '\t') str++;
246 if (!*str) goto error;
248 p = str;
249 while (isxdigitW(*p))
251 count++;
252 p++;
254 if (count > 8) goto error;
256 end = p;
257 while (*p == ' ' || *p == '\t') p++;
258 if (*p && *p != ';') goto error;
260 *end = 0;
261 *dw = strtoulW(str, &end, 16);
262 return TRUE;
264 error:
265 return FALSE;
268 /******************************************************************************
269 * Converts comma-separated hex data into a binary string and modifies
270 * the input parameter to skip the concatenating backslash, if found.
272 * Returns TRUE or FALSE to indicate whether parsing was successful.
274 static BOOL convert_hex_csv_to_hex(struct parser *parser, WCHAR **str)
276 size_t size;
277 BYTE *d;
278 WCHAR *s;
280 parser->backslash = FALSE;
282 /* The worst case is 1 digit + 1 comma per byte */
283 size = ((lstrlenW(*str) + 1) / 2) + parser->data_size;
284 parser->data = resize_buffer(parser->data, size);
286 s = *str;
287 d = (BYTE *)parser->data + parser->data_size;
289 while (*s)
291 WCHAR *end;
292 unsigned long wc;
294 wc = strtoulW(s, &end, 16);
295 if (wc > 0xff) return FALSE;
297 if (s == end && wc == 0)
299 while (*end == ' ' || *end == '\t') end++;
300 if (*end == '\\')
302 parser->backslash = TRUE;
303 *str = end + 1;
304 return TRUE;
306 else if (*end == ';')
307 return TRUE;
308 return FALSE;
311 *d++ = wc;
312 parser->data_size++;
314 if (*end && *end != ',')
316 while (*end == ' ' || *end == '\t') end++;
317 if (*end && *end != ';') return FALSE;
318 return TRUE;
321 if (*end) end++;
322 s = end;
325 return TRUE;
328 /******************************************************************************
329 * Parses the data type of the registry value being imported and modifies
330 * the input parameter to skip the string representation of the data type.
332 * Returns TRUE or FALSE to indicate whether a data type was found.
334 static BOOL parse_data_type(struct parser *parser, WCHAR **line)
336 struct data_type { const WCHAR *tag; int len; int type; int parse_type; };
338 static const WCHAR quote[] = {'"'};
339 static const WCHAR hex[] = {'h','e','x',':'};
340 static const WCHAR dword[] = {'d','w','o','r','d',':'};
341 static const WCHAR hexp[] = {'h','e','x','('};
343 static const struct data_type data_types[] = {
344 /* tag len type parse type */
345 { quote, 1, REG_SZ, REG_SZ },
346 { hex, 4, REG_BINARY, REG_BINARY },
347 { dword, 6, REG_DWORD, REG_DWORD },
348 { hexp, 4, -1, REG_BINARY }, /* REG_NONE, REG_EXPAND_SZ, REG_MULTI_SZ */
349 { NULL, 0, 0, 0 }
352 const struct data_type *ptr;
354 for (ptr = data_types; ptr->tag; ptr++)
356 if (strncmpW(ptr->tag, *line, ptr->len))
357 continue;
359 parser->parse_type = ptr->parse_type;
360 parser->data_type = ptr->parse_type;
361 *line += ptr->len;
363 if (ptr->type == -1)
365 WCHAR *end;
366 DWORD val;
368 if (!**line || tolowerW((*line)[1]) == 'x')
369 return FALSE;
371 /* "hex(xx):" is special */
372 val = wcstoul(*line, &end, 16);
373 if (*end != ')' || *(end + 1) != ':' || (val == ~0u && errno == ERANGE))
374 return FALSE;
376 parser->data_type = val;
377 *line = end + 2;
379 return TRUE;
381 return FALSE;
384 /******************************************************************************
385 * Replaces escape sequences with their character equivalents and
386 * null-terminates the string on the first non-escaped double quote.
388 * Assigns a pointer to the remaining unparsed data in the line.
389 * Returns TRUE or FALSE to indicate whether a closing double quote was found.
391 static BOOL REGPROC_unescape_string(WCHAR *str, WCHAR **unparsed)
393 int str_idx = 0; /* current character under analysis */
394 int val_idx = 0; /* the last character of the unescaped string */
395 int len = lstrlenW(str);
396 BOOL ret;
398 for (str_idx = 0; str_idx < len; str_idx++, val_idx++) {
399 if (str[str_idx] == '\\') {
400 str_idx++;
401 switch (str[str_idx]) {
402 case 'n':
403 str[val_idx] = '\n';
404 break;
405 case 'r':
406 str[val_idx] = '\r';
407 break;
408 case '0':
409 str[val_idx] = '\0';
410 break;
411 case '\\':
412 case '"':
413 str[val_idx] = str[str_idx];
414 break;
415 default:
416 output_message(STRING_ESCAPE_SEQUENCE, str[str_idx]);
417 str[val_idx] = str[str_idx];
418 break;
420 } else if (str[str_idx] == '"') {
421 break;
422 } else {
423 str[val_idx] = str[str_idx];
427 ret = (str[str_idx] == '"');
428 *unparsed = str + str_idx + 1;
429 str[val_idx] = '\0';
430 return ret;
433 static HKEY parse_key_name(WCHAR *key_name, WCHAR **key_path)
435 unsigned int i;
437 if (!key_name) return 0;
439 *key_path = strchrW(key_name, '\\');
440 if (*key_path) (*key_path)++;
442 for (i = 0; i < ARRAY_SIZE(reg_class_keys); i++)
444 int len = lstrlenW(reg_class_namesW[i]);
445 if (!strncmpiW(key_name, reg_class_namesW[i], len) &&
446 (key_name[len] == 0 || key_name[len] == '\\'))
448 return reg_class_keys[i];
452 return 0;
455 static void close_key(struct parser *parser)
457 if (parser->hkey)
459 HeapFree(GetProcessHeap(), 0, parser->key_name);
460 parser->key_name = NULL;
462 RegCloseKey(parser->hkey);
463 parser->hkey = NULL;
467 /******************************************************************************
468 * Opens the registry key given by the input path.
469 * This key must be closed by calling close_key().
471 static LONG open_key(struct parser *parser, WCHAR *path)
473 HKEY key_class;
474 WCHAR *key_path;
475 LONG res;
477 close_key(parser);
479 /* Get the registry class */
480 if (!path || !(key_class = parse_key_name(path, &key_path)))
481 return ERROR_INVALID_PARAMETER;
483 res = RegCreateKeyExW(key_class, key_path, 0, NULL, REG_OPTION_NON_VOLATILE,
484 KEY_ALL_ACCESS, NULL, &parser->hkey, NULL);
486 if (res == ERROR_SUCCESS)
488 parser->key_name = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(path) + 1) * sizeof(WCHAR));
489 CHECK_ENOUGH_MEMORY(parser->key_name);
490 lstrcpyW(parser->key_name, path);
492 else
493 parser->hkey = NULL;
495 return res;
498 static void free_parser_data(struct parser *parser)
500 if (parser->parse_type == REG_DWORD || parser->parse_type == REG_BINARY)
501 HeapFree(GetProcessHeap(), 0, parser->data);
503 parser->data = NULL;
504 parser->data_size = 0;
507 static void prepare_hex_string_data(struct parser *parser)
509 if (parser->data_type == REG_EXPAND_SZ || parser->data_type == REG_MULTI_SZ)
511 BYTE *data = parser->data;
513 if (data[parser->data_size - 1] != 0)
515 data[parser->data_size] = 0;
516 parser->data_size++;
519 if (!parser->is_unicode)
521 parser->data = GetWideStringN(parser->data, parser->data_size, &parser->data_size);
522 parser->data_size *= sizeof(WCHAR);
523 HeapFree(GetProcessHeap(), 0, data);
528 enum reg_versions {
529 REG_VERSION_31,
530 REG_VERSION_40,
531 REG_VERSION_50,
532 REG_VERSION_FUZZY,
533 REG_VERSION_INVALID
536 static enum reg_versions parse_file_header(const WCHAR *s)
538 static const WCHAR header_31[] = {'R','E','G','E','D','I','T',0};
539 static const WCHAR header_40[] = {'R','E','G','E','D','I','T','4',0};
540 static const WCHAR header_50[] = {'W','i','n','d','o','w','s',' ',
541 'R','e','g','i','s','t','r','y',' ','E','d','i','t','o','r',' ',
542 'V','e','r','s','i','o','n',' ','5','.','0','0',0};
544 while (*s == ' ' || *s == '\t') s++;
546 if (!strcmpW(s, header_31))
547 return REG_VERSION_31;
549 if (!strcmpW(s, header_40))
550 return REG_VERSION_40;
552 if (!strcmpW(s, header_50))
553 return REG_VERSION_50;
555 /* The Windows version accepts registry file headers beginning with "REGEDIT" and ending
556 * with other characters, as long as "REGEDIT" appears at the start of the line. For example,
557 * "REGEDIT 4", "REGEDIT9" and "REGEDIT4FOO" are all treated as valid file headers.
558 * In all such cases, however, the contents of the registry file are not imported.
560 if (!strncmpW(s, header_31, 7)) /* "REGEDIT" without NUL */
561 return REG_VERSION_FUZZY;
563 return REG_VERSION_INVALID;
566 /* handler for parser HEADER state */
567 static WCHAR *header_state(struct parser *parser, WCHAR *pos)
569 WCHAR *line, *header;
571 if (!(line = get_line(parser->file)))
572 return NULL;
574 if (!parser->is_unicode)
576 header = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(line) + 3) * sizeof(WCHAR));
577 CHECK_ENOUGH_MEMORY(header);
578 header[0] = parser->two_wchars[0];
579 header[1] = parser->two_wchars[1];
580 lstrcpyW(header + 2, line);
581 parser->reg_version = parse_file_header(header);
582 HeapFree(GetProcessHeap(), 0, header);
584 else parser->reg_version = parse_file_header(line);
586 switch (parser->reg_version)
588 case REG_VERSION_31:
589 set_state(parser, PARSE_WIN31_LINE);
590 break;
591 case REG_VERSION_40:
592 case REG_VERSION_50:
593 set_state(parser, LINE_START);
594 break;
595 default:
596 get_line(NULL); /* Reset static variables */
597 return NULL;
600 return line;
603 /* handler for parser PARSE_WIN31_LINE state */
604 static WCHAR *parse_win31_line_state(struct parser *parser, WCHAR *pos)
606 WCHAR *line, *value;
607 static WCHAR hkcr[] = {'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T'};
608 unsigned int key_end = 0;
610 if (!(line = get_line(parser->file)))
611 return NULL;
613 if (strncmpW(line, hkcr, ARRAY_SIZE(hkcr)))
614 return line;
616 /* get key name */
617 while (line[key_end] && !isspaceW(line[key_end])) key_end++;
619 value = line + key_end;
620 while (*value == ' ' || *value == '\t') value++;
622 if (*value == '=') value++;
623 if (*value == ' ') value++; /* at most one space is skipped */
625 line[key_end] = 0;
627 if (open_key(parser, line) != ERROR_SUCCESS)
629 output_message(STRING_OPEN_KEY_FAILED, line);
630 return line;
633 parser->value_name = NULL;
634 parser->data_type = REG_SZ;
635 parser->data = value;
636 parser->data_size = (lstrlenW(value) + 1) * sizeof(WCHAR);
638 set_state(parser, SET_VALUE);
639 return value;
642 /* handler for parser LINE_START state */
643 static WCHAR *line_start_state(struct parser *parser, WCHAR *pos)
645 WCHAR *line, *p;
647 if (!(line = get_line(parser->file)))
648 return NULL;
650 for (p = line; *p; p++)
652 switch (*p)
654 case '[':
655 set_state(parser, KEY_NAME);
656 return p + 1;
657 case '@':
658 set_state(parser, DEFAULT_VALUE_NAME);
659 return p;
660 case '"':
661 set_state(parser, QUOTED_VALUE_NAME);
662 return p + 1;
663 case ' ':
664 case '\t':
665 break;
666 default:
667 return p;
671 return p;
674 /* handler for parser KEY_NAME state */
675 static WCHAR *key_name_state(struct parser *parser, WCHAR *pos)
677 WCHAR *p = pos, *key_end;
679 if (*p == ' ' || *p == '\t' || !(key_end = strrchrW(p, ']')))
680 goto done;
682 *key_end = 0;
684 if (*p == '-')
686 set_state(parser, DELETE_KEY);
687 return p + 1;
689 else if (open_key(parser, p) != ERROR_SUCCESS)
690 output_message(STRING_OPEN_KEY_FAILED, p);
692 done:
693 set_state(parser, LINE_START);
694 return p;
697 /* handler for parser DELETE_KEY state */
698 static WCHAR *delete_key_state(struct parser *parser, WCHAR *pos)
700 WCHAR *p = pos;
702 if (*p == 'H' || *p == 'h')
703 delete_registry_key(p);
705 set_state(parser, LINE_START);
706 return p;
709 /* handler for parser DEFAULT_VALUE_NAME state */
710 static WCHAR *default_value_name_state(struct parser *parser, WCHAR *pos)
712 parser->value_name = NULL;
714 set_state(parser, DATA_START);
715 return pos + 1;
718 /* handler for parser QUOTED_VALUE_NAME state */
719 static WCHAR *quoted_value_name_state(struct parser *parser, WCHAR *pos)
721 WCHAR *val_name = pos, *p;
723 if (parser->value_name)
725 HeapFree(GetProcessHeap(), 0, parser->value_name);
726 parser->value_name = NULL;
729 if (!REGPROC_unescape_string(val_name, &p))
730 goto invalid;
732 /* copy the value name in case we need to parse multiple lines and the buffer is overwritten */
733 parser->value_name = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(val_name) + 1) * sizeof(WCHAR));
734 CHECK_ENOUGH_MEMORY(parser->value_name);
735 lstrcpyW(parser->value_name, val_name);
737 set_state(parser, DATA_START);
738 return p;
740 invalid:
741 set_state(parser, LINE_START);
742 return p;
745 /* handler for parser DATA_START state */
746 static WCHAR *data_start_state(struct parser *parser, WCHAR *pos)
748 WCHAR *p = pos;
749 unsigned int len;
751 while (*p == ' ' || *p == '\t') p++;
752 if (*p != '=') goto done;
753 p++;
754 while (*p == ' ' || *p == '\t') p++;
756 /* trim trailing whitespace */
757 len = strlenW(p);
758 while (len > 0 && (p[len - 1] == ' ' || p[len - 1] == '\t')) len--;
759 p[len] = 0;
761 if (*p == '-')
762 set_state(parser, DELETE_VALUE);
763 else
764 set_state(parser, DATA_TYPE);
765 return p;
767 done:
768 set_state(parser, LINE_START);
769 return p;
772 /* handler for parser DELETE_VALUE state */
773 static WCHAR *delete_value_state(struct parser *parser, WCHAR *pos)
775 WCHAR *p = pos + 1;
777 while (*p == ' ' || *p == '\t') p++;
778 if (*p && *p != ';') goto done;
780 RegDeleteValueW(parser->hkey, parser->value_name);
782 done:
783 set_state(parser, LINE_START);
784 return p;
787 /* handler for parser DATA_TYPE state */
788 static WCHAR *data_type_state(struct parser *parser, WCHAR *pos)
790 WCHAR *line = pos;
792 if (!parse_data_type(parser, &line))
794 set_state(parser, LINE_START);
795 return line;
798 switch (parser->parse_type)
800 case REG_SZ:
801 set_state(parser, STRING_DATA);
802 break;
803 case REG_DWORD:
804 set_state(parser, DWORD_DATA);
805 break;
806 case REG_BINARY: /* all hex data types, including undefined */
807 set_state(parser, HEX_DATA);
808 break;
809 default:
810 set_state(parser, UNKNOWN_DATA);
813 return line;
816 /* handler for parser STRING_DATA state */
817 static WCHAR *string_data_state(struct parser *parser, WCHAR *pos)
819 WCHAR *line;
821 parser->data = pos;
823 if (!REGPROC_unescape_string(parser->data, &line))
824 goto invalid;
826 while (*line == ' ' || *line == '\t') line++;
827 if (*line && *line != ';') goto invalid;
829 parser->data_size = (lstrlenW(parser->data) + 1) * sizeof(WCHAR);
831 set_state(parser, SET_VALUE);
832 return line;
834 invalid:
835 free_parser_data(parser);
836 set_state(parser, LINE_START);
837 return line;
840 /* handler for parser DWORD_DATA state */
841 static WCHAR *dword_data_state(struct parser *parser, WCHAR *pos)
843 WCHAR *line = pos;
845 parser->data = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD));
846 CHECK_ENOUGH_MEMORY(parser->data);
848 if (!convert_hex_to_dword(line, parser->data))
849 goto invalid;
851 parser->data_size = sizeof(DWORD);
853 set_state(parser, SET_VALUE);
854 return line;
856 invalid:
857 free_parser_data(parser);
858 set_state(parser, LINE_START);
859 return line;
862 /* handler for parser HEX_DATA state */
863 static WCHAR *hex_data_state(struct parser *parser, WCHAR *pos)
865 WCHAR *line = pos;
867 if (!convert_hex_csv_to_hex(parser, &line))
868 goto invalid;
870 if (parser->backslash)
872 set_state(parser, EOL_BACKSLASH);
873 return line;
876 prepare_hex_string_data(parser);
878 set_state(parser, SET_VALUE);
879 return line;
881 invalid:
882 free_parser_data(parser);
883 set_state(parser, LINE_START);
884 return line;
887 /* handler for parser EOL_BACKSLASH state */
888 static WCHAR *eol_backslash_state(struct parser *parser, WCHAR *pos)
890 WCHAR *p = pos;
892 while (*p == ' ' || *p == '\t') p++;
893 if (*p && *p != ';') goto invalid;
895 set_state(parser, HEX_MULTILINE);
896 return pos;
898 invalid:
899 free_parser_data(parser);
900 set_state(parser, LINE_START);
901 return p;
904 /* handler for parser HEX_MULTILINE state */
905 static WCHAR *hex_multiline_state(struct parser *parser, WCHAR *pos)
907 WCHAR *line;
909 if (!(line = get_line(parser->file)))
911 prepare_hex_string_data(parser);
912 set_state(parser, SET_VALUE);
913 return pos;
916 while (*line == ' ' || *line == '\t') line++;
917 if (!*line || *line == ';') return line;
919 if (!isxdigitW(*line)) goto invalid;
921 set_state(parser, HEX_DATA);
922 return line;
924 invalid:
925 free_parser_data(parser);
926 set_state(parser, LINE_START);
927 return line;
930 /* handler for parser UNKNOWN_DATA state */
931 static WCHAR *unknown_data_state(struct parser *parser, WCHAR *pos)
933 output_message(STRING_UNKNOWN_DATA_FORMAT, parser->data_type);
935 set_state(parser, LINE_START);
936 return pos;
939 /* handler for parser SET_VALUE state */
940 static WCHAR *set_value_state(struct parser *parser, WCHAR *pos)
942 RegSetValueExW(parser->hkey, parser->value_name, 0, parser->data_type,
943 parser->data, parser->data_size);
945 free_parser_data(parser);
947 if (parser->reg_version == REG_VERSION_31)
948 set_state(parser, PARSE_WIN31_LINE);
949 else
950 set_state(parser, LINE_START);
952 return pos;
955 static WCHAR *get_lineA(FILE *fp)
957 static WCHAR *lineW;
958 static size_t size;
959 static char *buf, *next;
960 char *line;
962 HeapFree(GetProcessHeap(), 0, lineW);
964 if (!fp) goto cleanup;
966 if (!size)
968 size = REG_VAL_BUF_SIZE;
969 buf = HeapAlloc(GetProcessHeap(), 0, size);
970 CHECK_ENOUGH_MEMORY(buf);
971 *buf = 0;
972 next = buf;
974 line = next;
976 while (next)
978 char *p = strpbrk(line, "\r\n");
979 if (!p)
981 size_t len, count;
982 len = strlen(next);
983 memmove(buf, next, len + 1);
984 if (size - len < 3)
986 char *new_buf = HeapReAlloc(GetProcessHeap(), 0, buf, size * 2);
987 CHECK_ENOUGH_MEMORY(new_buf);
988 buf = new_buf;
989 size *= 2;
991 if (!(count = fread(buf + len, 1, size - len - 1, fp)))
993 next = NULL;
994 lineW = GetWideString(buf);
995 return lineW;
997 buf[len + count] = 0;
998 next = buf;
999 line = buf;
1000 continue;
1002 next = p + 1;
1003 if (*p == '\r' && *(p + 1) == '\n') next++;
1004 *p = 0;
1005 lineW = GetWideString(line);
1006 return lineW;
1009 cleanup:
1010 lineW = NULL;
1011 if (size) HeapFree(GetProcessHeap(), 0, buf);
1012 size = 0;
1013 return NULL;
1016 static WCHAR *get_lineW(FILE *fp)
1018 static size_t size;
1019 static WCHAR *buf, *next;
1020 WCHAR *line;
1022 if (!fp) goto cleanup;
1024 if (!size)
1026 size = REG_VAL_BUF_SIZE;
1027 buf = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
1028 CHECK_ENOUGH_MEMORY(buf);
1029 *buf = 0;
1030 next = buf;
1032 line = next;
1034 while (next)
1036 static const WCHAR line_endings[] = {'\r','\n',0};
1037 WCHAR *p = strpbrkW(line, line_endings);
1038 if (!p)
1040 size_t len, count;
1041 len = strlenW(next);
1042 memmove(buf, next, (len + 1) * sizeof(WCHAR));
1043 if (size - len < 3)
1045 WCHAR *new_buf = HeapReAlloc(GetProcessHeap(), 0, buf, (size * 2) * sizeof(WCHAR));
1046 CHECK_ENOUGH_MEMORY(new_buf);
1047 buf = new_buf;
1048 size *= 2;
1050 if (!(count = fread(buf + len, sizeof(WCHAR), size - len - 1, fp)))
1052 next = NULL;
1053 return buf;
1055 buf[len + count] = 0;
1056 next = buf;
1057 line = buf;
1058 continue;
1060 next = p + 1;
1061 if (*p == '\r' && *(p + 1) == '\n') next++;
1062 *p = 0;
1063 return line;
1066 cleanup:
1067 if (size) HeapFree(GetProcessHeap(), 0, buf);
1068 size = 0;
1069 return NULL;
1072 /******************************************************************************
1073 * Checks whether the buffer has enough room for the string or required size.
1074 * Resizes the buffer if necessary.
1076 * Parameters:
1077 * buffer - pointer to a buffer for string
1078 * len - current length of the buffer in characters.
1079 * required_len - length of the string to place to the buffer in characters.
1080 * The length does not include the terminating null character.
1082 static void REGPROC_resize_char_buffer(WCHAR **buffer, DWORD *len, DWORD required_len)
1084 required_len++;
1085 if (required_len > *len) {
1086 *len = required_len;
1087 if (!*buffer)
1088 *buffer = HeapAlloc(GetProcessHeap(), 0, *len * sizeof(**buffer));
1089 else
1090 *buffer = HeapReAlloc(GetProcessHeap(), 0, *buffer, *len * sizeof(**buffer));
1091 CHECK_ENOUGH_MEMORY(*buffer);
1095 /******************************************************************************
1096 * Same as REGPROC_resize_char_buffer() but on a regular buffer.
1098 * Parameters:
1099 * buffer - pointer to a buffer
1100 * len - current size of the buffer in bytes
1101 * required_size - size of the data to place in the buffer in bytes
1103 static void REGPROC_resize_binary_buffer(BYTE **buffer, DWORD *size, DWORD required_size)
1105 if (required_size > *size) {
1106 *size = required_size;
1107 if (!*buffer)
1108 *buffer = HeapAlloc(GetProcessHeap(), 0, *size);
1109 else
1110 *buffer = HeapReAlloc(GetProcessHeap(), 0, *buffer, *size);
1111 CHECK_ENOUGH_MEMORY(*buffer);
1115 /******************************************************************************
1116 * Prints string str to file
1118 static void REGPROC_export_string(WCHAR **line_buf, DWORD *line_buf_size, DWORD *line_len, WCHAR *str, DWORD str_len)
1120 DWORD i, pos;
1121 DWORD extra = 0;
1123 REGPROC_resize_char_buffer(line_buf, line_buf_size, *line_len + str_len + 10);
1125 /* escaping characters */
1126 pos = *line_len;
1127 for (i = 0; i < str_len; i++) {
1128 WCHAR c = str[i];
1129 switch (c) {
1130 case '\n':
1131 extra++;
1132 REGPROC_resize_char_buffer(line_buf, line_buf_size, *line_len + str_len + extra);
1133 (*line_buf)[pos++] = '\\';
1134 (*line_buf)[pos++] = 'n';
1135 break;
1137 case '\r':
1138 extra++;
1139 REGPROC_resize_char_buffer(line_buf, line_buf_size, *line_len + str_len + extra);
1140 (*line_buf)[pos++] = '\\';
1141 (*line_buf)[pos++] = 'r';
1142 break;
1144 case '\\':
1145 case '"':
1146 extra++;
1147 REGPROC_resize_char_buffer(line_buf, line_buf_size, *line_len + str_len + extra);
1148 (*line_buf)[pos++] = '\\';
1149 /* Fall through */
1151 default:
1152 (*line_buf)[pos++] = c;
1153 break;
1156 (*line_buf)[pos] = '\0';
1157 *line_len = pos;
1160 static void REGPROC_export_binary(WCHAR **line_buf, DWORD *line_buf_size, DWORD *line_len, DWORD type, BYTE *value, DWORD value_size, BOOL unicode)
1162 DWORD hex_pos, data_pos;
1163 const WCHAR *hex_prefix;
1164 const WCHAR hex[] = {'h','e','x',':',0};
1165 WCHAR hex_buf[17];
1166 const WCHAR concat[] = {'\\','\r','\n',' ',' ',0};
1167 DWORD concat_prefix, concat_len;
1168 const WCHAR newline[] = {'\r','\n',0};
1169 CHAR* value_multibyte = NULL;
1171 if (type == REG_BINARY) {
1172 hex_prefix = hex;
1173 } else {
1174 const WCHAR hex_format[] = {'h','e','x','(','%','x',')',':',0};
1175 hex_prefix = hex_buf;
1176 sprintfW(hex_buf, hex_format, type);
1177 if ((type == REG_SZ || type == REG_EXPAND_SZ || type == REG_MULTI_SZ) && !unicode)
1179 value_multibyte = GetMultiByteStringN((WCHAR*)value, value_size / sizeof(WCHAR), &value_size);
1180 value = (BYTE*)value_multibyte;
1184 concat_len = lstrlenW(concat);
1185 concat_prefix = 2;
1187 hex_pos = *line_len;
1188 *line_len += lstrlenW(hex_prefix);
1189 data_pos = *line_len;
1190 *line_len += value_size * 3;
1191 /* - The 2 spaces that concat places at the start of the
1192 * line effectively reduce the space available for data.
1193 * - If the value name and hex prefix are very long
1194 * ( > REG_FILE_HEX_LINE_LEN) or *line_len divides
1195 * without a remainder then we may overestimate
1196 * the needed number of lines by one. But that's ok.
1197 * - The trailing '\r' takes the place of a comma so
1198 * we only need to add 1 for the trailing '\n'
1200 *line_len += *line_len / (REG_FILE_HEX_LINE_LEN - concat_prefix) * concat_len + 1;
1201 REGPROC_resize_char_buffer(line_buf, line_buf_size, *line_len);
1202 lstrcpyW(*line_buf + hex_pos, hex_prefix);
1203 if (value_size)
1205 const WCHAR format[] = {'%','0','2','x',0};
1206 DWORD i, column;
1208 column = data_pos; /* no line wrap yet */
1209 i = 0;
1210 while (1)
1212 sprintfW(*line_buf + data_pos, format, (unsigned int)value[i]);
1213 data_pos += 2;
1214 if (++i == value_size)
1215 break;
1217 (*line_buf)[data_pos++] = ',';
1218 column += 3;
1220 /* wrap the line */
1221 if (column >= REG_FILE_HEX_LINE_LEN) {
1222 lstrcpyW(*line_buf + data_pos, concat);
1223 data_pos += concat_len;
1224 column = concat_prefix;
1228 lstrcpyW(*line_buf + data_pos, newline);
1229 HeapFree(GetProcessHeap(), 0, value_multibyte);
1232 /******************************************************************************
1233 * Writes the given line to a file, in multi-byte or wide characters
1235 static void REGPROC_write_line(FILE *file, const WCHAR* str, BOOL unicode)
1237 if(unicode)
1239 fwrite(str, sizeof(WCHAR), lstrlenW(str), file);
1240 } else
1242 char* strA = GetMultiByteString(str);
1243 fputs(strA, file);
1244 HeapFree(GetProcessHeap(), 0, strA);
1248 /******************************************************************************
1249 * Writes contents of the registry key to the specified file stream.
1251 * Parameters:
1252 * file - writable file stream to export registry branch to.
1253 * key - registry branch to export.
1254 * reg_key_name_buf - name of the key with registry class.
1255 * Is resized if necessary.
1256 * reg_key_name_size - length of the buffer for the registry class in characters.
1257 * val_name_buf - buffer for storing value name.
1258 * Is resized if necessary.
1259 * val_name_size - length of the buffer for storing value names in characters.
1260 * val_buf - buffer for storing values while extracting.
1261 * Is resized if necessary.
1262 * val_size - size of the buffer for storing values in bytes.
1264 static void export_hkey(FILE *file, HKEY key,
1265 WCHAR **reg_key_name_buf, DWORD *reg_key_name_size,
1266 WCHAR **val_name_buf, DWORD *val_name_size,
1267 BYTE **val_buf, DWORD *val_size,
1268 WCHAR **line_buf, DWORD *line_buf_size,
1269 BOOL unicode)
1271 DWORD max_sub_key_len;
1272 DWORD max_val_name_len;
1273 DWORD max_val_size;
1274 DWORD curr_len;
1275 DWORD i;
1276 LONG ret;
1277 WCHAR key_format[] = {'\r','\n','[','%','s',']','\r','\n',0};
1279 /* get size information and resize the buffers if necessary */
1280 if (RegQueryInfoKeyW(key, NULL, NULL, NULL, NULL,
1281 &max_sub_key_len, NULL,
1282 NULL, &max_val_name_len, &max_val_size, NULL, NULL
1283 ) != ERROR_SUCCESS)
1284 return;
1285 curr_len = strlenW(*reg_key_name_buf);
1286 REGPROC_resize_char_buffer(reg_key_name_buf, reg_key_name_size,
1287 max_sub_key_len + curr_len + 1);
1288 REGPROC_resize_char_buffer(val_name_buf, val_name_size,
1289 max_val_name_len);
1290 REGPROC_resize_binary_buffer(val_buf, val_size, max_val_size);
1291 REGPROC_resize_char_buffer(line_buf, line_buf_size, lstrlenW(*reg_key_name_buf) + 4);
1292 /* output data for the current key */
1293 sprintfW(*line_buf, key_format, *reg_key_name_buf);
1294 REGPROC_write_line(file, *line_buf, unicode);
1296 /* print all the values */
1297 i = 0;
1298 for (;;) {
1299 DWORD value_type;
1300 DWORD val_name_size1 = *val_name_size;
1301 DWORD val_size1 = *val_size;
1302 ret = RegEnumValueW(key, i, *val_name_buf, &val_name_size1, NULL,
1303 &value_type, *val_buf, &val_size1);
1304 if (ret == ERROR_MORE_DATA) {
1305 /* Increase the size of the buffers and retry */
1306 REGPROC_resize_char_buffer(val_name_buf, val_name_size, val_name_size1);
1307 REGPROC_resize_binary_buffer(val_buf, val_size, val_size1);
1308 } else if (ret == ERROR_SUCCESS) {
1309 DWORD line_len;
1310 i++;
1312 if ((*val_name_buf)[0]) {
1313 const WCHAR val_start[] = {'"','%','s','"','=',0};
1315 line_len = 0;
1316 REGPROC_export_string(line_buf, line_buf_size, &line_len, *val_name_buf, lstrlenW(*val_name_buf));
1317 REGPROC_resize_char_buffer(val_name_buf, val_name_size, lstrlenW(*line_buf) + 1);
1318 lstrcpyW(*val_name_buf, *line_buf);
1320 line_len = 3 + lstrlenW(*val_name_buf);
1321 REGPROC_resize_char_buffer(line_buf, line_buf_size, line_len);
1322 sprintfW(*line_buf, val_start, *val_name_buf);
1323 } else {
1324 const WCHAR std_val[] = {'@','=',0};
1325 line_len = 2;
1326 REGPROC_resize_char_buffer(line_buf, line_buf_size, line_len);
1327 lstrcpyW(*line_buf, std_val);
1330 switch (value_type) {
1331 case REG_SZ:
1333 WCHAR* wstr = (WCHAR*)*val_buf;
1335 if (val_size1 < sizeof(WCHAR) || val_size1 % sizeof(WCHAR) ||
1336 wstr[val_size1 / sizeof(WCHAR) - 1]) {
1337 REGPROC_export_binary(line_buf, line_buf_size, &line_len, value_type, *val_buf, val_size1, unicode);
1338 } else {
1339 const WCHAR start[] = {'"',0};
1340 const WCHAR end[] = {'"','\r','\n',0};
1341 DWORD len;
1343 len = lstrlenW(start);
1344 REGPROC_resize_char_buffer(line_buf, line_buf_size, line_len + len);
1345 lstrcpyW(*line_buf + line_len, start);
1346 line_len += len;
1348 REGPROC_export_string(line_buf, line_buf_size, &line_len, wstr, lstrlenW(wstr));
1350 REGPROC_resize_char_buffer(line_buf, line_buf_size, line_len + lstrlenW(end));
1351 lstrcpyW(*line_buf + line_len, end);
1353 break;
1356 case REG_DWORD:
1358 WCHAR format[] = {'d','w','o','r','d',':','%','0','8','x','\r','\n',0};
1360 REGPROC_resize_char_buffer(line_buf, line_buf_size, line_len + 15);
1361 sprintfW(*line_buf + line_len, format, *((DWORD *)*val_buf));
1362 break;
1365 case REG_NONE:
1366 case REG_EXPAND_SZ:
1367 case REG_MULTI_SZ:
1368 case REG_BINARY:
1369 default:
1370 REGPROC_export_binary(line_buf, line_buf_size, &line_len, value_type, *val_buf, val_size1, unicode);
1372 REGPROC_write_line(file, *line_buf, unicode);
1374 else break;
1377 i = 0;
1378 (*reg_key_name_buf)[curr_len] = '\\';
1379 for (;;) {
1380 DWORD buf_size = *reg_key_name_size - curr_len - 1;
1382 ret = RegEnumKeyExW(key, i, *reg_key_name_buf + curr_len + 1, &buf_size,
1383 NULL, NULL, NULL, NULL);
1384 if (ret == ERROR_MORE_DATA) {
1385 /* Increase the size of the buffer and retry */
1386 REGPROC_resize_char_buffer(reg_key_name_buf, reg_key_name_size, curr_len + 1 + buf_size);
1387 } else if (ret == ERROR_SUCCESS) {
1388 HKEY subkey;
1390 i++;
1391 if (RegOpenKeyW(key, *reg_key_name_buf + curr_len + 1,
1392 &subkey) == ERROR_SUCCESS) {
1393 export_hkey(file, subkey, reg_key_name_buf, reg_key_name_size,
1394 val_name_buf, val_name_size, val_buf, val_size,
1395 line_buf, line_buf_size, unicode);
1396 RegCloseKey(subkey);
1398 else break;
1400 else break;
1402 (*reg_key_name_buf)[curr_len] = '\0';
1405 /******************************************************************************
1406 * Open file in binary mode for export.
1408 static FILE *REGPROC_open_export_file(WCHAR *file_name, BOOL unicode)
1410 FILE *file;
1411 WCHAR dash = '-';
1413 if (strncmpW(file_name,&dash,1)==0) {
1414 file=stdout;
1415 _setmode(_fileno(file), _O_BINARY);
1416 } else
1418 WCHAR wb_mode[] = {'w','b',0};
1419 WCHAR regedit[] = {'r','e','g','e','d','i','t',0};
1421 file = _wfopen(file_name, wb_mode);
1422 if (!file) {
1423 _wperror(regedit);
1424 output_message(STRING_CANNOT_OPEN_FILE, file_name);
1425 exit(1);
1428 if(unicode)
1430 const BYTE unicode_seq[] = {0xff,0xfe};
1431 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'};
1432 fwrite(unicode_seq, sizeof(BYTE), sizeof(unicode_seq)/sizeof(unicode_seq[0]), file);
1433 fwrite(header, sizeof(WCHAR), sizeof(header)/sizeof(header[0]), file);
1434 } else
1436 fputs("REGEDIT4\r\n", file);
1439 return file;
1442 /******************************************************************************
1443 * Writes contents of the registry key to the specified file stream.
1445 * Parameters:
1446 * file_name - name of a file to export registry branch to.
1447 * reg_key_name - registry branch to export. The whole registry is exported if
1448 * reg_key_name is NULL or contains an empty string.
1450 BOOL export_registry_key(WCHAR *file_name, WCHAR *reg_key_name, DWORD format)
1452 WCHAR *reg_key_name_buf;
1453 WCHAR *val_name_buf;
1454 BYTE *val_buf;
1455 WCHAR *line_buf;
1456 DWORD reg_key_name_size = KEY_MAX_LEN;
1457 DWORD val_name_size = KEY_MAX_LEN;
1458 DWORD val_size = REG_VAL_BUF_SIZE;
1459 DWORD line_buf_size = KEY_MAX_LEN + REG_VAL_BUF_SIZE;
1460 FILE *file = NULL;
1461 BOOL unicode = (format == REG_FORMAT_5);
1463 reg_key_name_buf = HeapAlloc(GetProcessHeap(), 0,
1464 reg_key_name_size * sizeof(*reg_key_name_buf));
1465 val_name_buf = HeapAlloc(GetProcessHeap(), 0,
1466 val_name_size * sizeof(*val_name_buf));
1467 val_buf = HeapAlloc(GetProcessHeap(), 0, val_size);
1468 line_buf = HeapAlloc(GetProcessHeap(), 0, line_buf_size * sizeof(*line_buf));
1469 CHECK_ENOUGH_MEMORY(reg_key_name_buf && val_name_buf && val_buf && line_buf);
1471 if (reg_key_name && reg_key_name[0]) {
1472 HKEY reg_key_class;
1473 WCHAR *branch_name = NULL;
1474 HKEY key;
1476 REGPROC_resize_char_buffer(&reg_key_name_buf, &reg_key_name_size,
1477 lstrlenW(reg_key_name));
1478 lstrcpyW(reg_key_name_buf, reg_key_name);
1480 /* open the specified key */
1481 if (!(reg_key_class = parse_key_name(reg_key_name, &branch_name))) {
1482 output_message(STRING_INCORRECT_REG_CLASS, reg_key_name);
1483 exit(1);
1485 if (!branch_name || !*branch_name) {
1486 /* no branch - registry class is specified */
1487 file = REGPROC_open_export_file(file_name, unicode);
1488 export_hkey(file, reg_key_class,
1489 &reg_key_name_buf, &reg_key_name_size,
1490 &val_name_buf, &val_name_size,
1491 &val_buf, &val_size, &line_buf,
1492 &line_buf_size, unicode);
1493 } else if (RegOpenKeyW(reg_key_class, branch_name, &key) == ERROR_SUCCESS) {
1494 file = REGPROC_open_export_file(file_name, unicode);
1495 export_hkey(file, key,
1496 &reg_key_name_buf, &reg_key_name_size,
1497 &val_name_buf, &val_name_size,
1498 &val_buf, &val_size, &line_buf,
1499 &line_buf_size, unicode);
1500 RegCloseKey(key);
1501 } else {
1502 output_message(STRING_REG_KEY_NOT_FOUND, reg_key_name);
1504 } else {
1505 unsigned int i;
1507 /* export all registry classes */
1508 file = REGPROC_open_export_file(file_name, unicode);
1509 for (i = 0; i < ARRAY_SIZE(reg_class_keys); i++) {
1510 /* do not export HKEY_CLASSES_ROOT */
1511 if (reg_class_keys[i] != HKEY_CLASSES_ROOT &&
1512 reg_class_keys[i] != HKEY_CURRENT_USER &&
1513 reg_class_keys[i] != HKEY_CURRENT_CONFIG &&
1514 reg_class_keys[i] != HKEY_DYN_DATA) {
1515 lstrcpyW(reg_key_name_buf, reg_class_namesW[i]);
1516 export_hkey(file, reg_class_keys[i],
1517 &reg_key_name_buf, &reg_key_name_size,
1518 &val_name_buf, &val_name_size,
1519 &val_buf, &val_size, &line_buf,
1520 &line_buf_size, unicode);
1525 if (file) {
1526 fclose(file);
1528 HeapFree(GetProcessHeap(), 0, reg_key_name);
1529 HeapFree(GetProcessHeap(), 0, val_name_buf);
1530 HeapFree(GetProcessHeap(), 0, val_buf);
1531 HeapFree(GetProcessHeap(), 0, line_buf);
1532 return TRUE;
1535 /******************************************************************************
1536 * Reads contents of the specified file into the registry.
1538 BOOL import_registry_file(FILE *reg_file)
1540 BYTE s[2];
1541 struct parser parser;
1542 WCHAR *pos;
1544 if (!reg_file || (fread(s, 2, 1, reg_file) != 1))
1545 return FALSE;
1547 parser.is_unicode = (s[0] == 0xff && s[1] == 0xfe);
1548 get_line = parser.is_unicode ? get_lineW : get_lineA;
1550 parser.file = reg_file;
1551 parser.two_wchars[0] = s[0];
1552 parser.two_wchars[1] = s[1];
1553 parser.reg_version = -1;
1554 parser.hkey = NULL;
1555 parser.key_name = NULL;
1556 parser.value_name = NULL;
1557 parser.parse_type = 0;
1558 parser.data_type = 0;
1559 parser.data = NULL;
1560 parser.data_size = 0;
1561 parser.backslash = FALSE;
1562 parser.state = HEADER;
1564 pos = parser.two_wchars;
1566 /* parser main loop */
1567 while (pos)
1568 pos = (parser_funcs[parser.state])(&parser, pos);
1570 if (parser.reg_version == REG_VERSION_FUZZY || parser.reg_version == REG_VERSION_INVALID)
1571 return parser.reg_version == REG_VERSION_FUZZY;
1573 HeapFree(GetProcessHeap(), 0, parser.value_name);
1574 close_key(&parser);
1576 return TRUE;
1579 /******************************************************************************
1580 * Removes the registry key with all subkeys. Parses full key name.
1582 * Parameters:
1583 * reg_key_name - full name of registry branch to delete. Ignored if is NULL,
1584 * empty, points to register key class, does not exist.
1586 void delete_registry_key(WCHAR *reg_key_name)
1588 WCHAR *key_name = NULL;
1589 HKEY key_class;
1591 if (!reg_key_name || !reg_key_name[0])
1592 return;
1594 if (!(key_class = parse_key_name(reg_key_name, &key_name))) {
1595 output_message(STRING_INCORRECT_REG_CLASS, reg_key_name);
1596 exit(1);
1598 if (!*key_name) {
1599 output_message(STRING_DELETE_REG_CLASS_FAILED, reg_key_name);
1600 exit(1);
1603 RegDeleteTreeW(key_class, key_name);