regedit: Use a helper function to export hex data and REG_DWORD data.
[wine.git] / programs / regedit / regproc.c
blobd7a55fd1646ad8155ae3a432fe4abab4f3cf0bb9
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 * Prints string str to file
1098 static void REGPROC_export_string(WCHAR **line_buf, DWORD *line_buf_size, DWORD *line_len, WCHAR *str, DWORD str_len)
1100 DWORD i, pos;
1101 DWORD extra = 0;
1103 REGPROC_resize_char_buffer(line_buf, line_buf_size, *line_len + str_len + 10);
1105 /* escaping characters */
1106 pos = *line_len;
1107 for (i = 0; i < str_len; i++) {
1108 WCHAR c = str[i];
1109 switch (c) {
1110 case '\n':
1111 extra++;
1112 REGPROC_resize_char_buffer(line_buf, line_buf_size, *line_len + str_len + extra);
1113 (*line_buf)[pos++] = '\\';
1114 (*line_buf)[pos++] = 'n';
1115 break;
1117 case '\r':
1118 extra++;
1119 REGPROC_resize_char_buffer(line_buf, line_buf_size, *line_len + str_len + extra);
1120 (*line_buf)[pos++] = '\\';
1121 (*line_buf)[pos++] = 'r';
1122 break;
1124 case '\\':
1125 case '"':
1126 extra++;
1127 REGPROC_resize_char_buffer(line_buf, line_buf_size, *line_len + str_len + extra);
1128 (*line_buf)[pos++] = '\\';
1129 /* Fall through */
1131 default:
1132 (*line_buf)[pos++] = c;
1133 break;
1136 (*line_buf)[pos] = '\0';
1137 *line_len = pos;
1140 static void REGPROC_export_binary(WCHAR **line_buf, DWORD *line_buf_size, DWORD *line_len, DWORD type, BYTE *value, DWORD value_size, BOOL unicode)
1142 DWORD hex_pos, data_pos;
1143 const WCHAR *hex_prefix;
1144 const WCHAR hex[] = {'h','e','x',':',0};
1145 WCHAR hex_buf[17];
1146 const WCHAR concat[] = {'\\','\r','\n',' ',' ',0};
1147 DWORD concat_prefix, concat_len;
1148 const WCHAR newline[] = {'\r','\n',0};
1149 CHAR* value_multibyte = NULL;
1151 if (type == REG_BINARY) {
1152 hex_prefix = hex;
1153 } else {
1154 const WCHAR hex_format[] = {'h','e','x','(','%','x',')',':',0};
1155 hex_prefix = hex_buf;
1156 sprintfW(hex_buf, hex_format, type);
1157 if ((type == REG_SZ || type == REG_EXPAND_SZ || type == REG_MULTI_SZ) && !unicode)
1159 value_multibyte = GetMultiByteStringN((WCHAR*)value, value_size / sizeof(WCHAR), &value_size);
1160 value = (BYTE*)value_multibyte;
1164 concat_len = lstrlenW(concat);
1165 concat_prefix = 2;
1167 hex_pos = *line_len;
1168 *line_len += lstrlenW(hex_prefix);
1169 data_pos = *line_len;
1170 *line_len += value_size * 3;
1171 /* - The 2 spaces that concat places at the start of the
1172 * line effectively reduce the space available for data.
1173 * - If the value name and hex prefix are very long
1174 * ( > REG_FILE_HEX_LINE_LEN) or *line_len divides
1175 * without a remainder then we may overestimate
1176 * the needed number of lines by one. But that's ok.
1177 * - The trailing '\r' takes the place of a comma so
1178 * we only need to add 1 for the trailing '\n'
1180 *line_len += *line_len / (REG_FILE_HEX_LINE_LEN - concat_prefix) * concat_len + 1;
1181 REGPROC_resize_char_buffer(line_buf, line_buf_size, *line_len);
1182 lstrcpyW(*line_buf + hex_pos, hex_prefix);
1183 if (value_size)
1185 const WCHAR format[] = {'%','0','2','x',0};
1186 DWORD i, column;
1188 column = data_pos; /* no line wrap yet */
1189 i = 0;
1190 while (1)
1192 sprintfW(*line_buf + data_pos, format, (unsigned int)value[i]);
1193 data_pos += 2;
1194 if (++i == value_size)
1195 break;
1197 (*line_buf)[data_pos++] = ',';
1198 column += 3;
1200 /* wrap the line */
1201 if (column >= REG_FILE_HEX_LINE_LEN) {
1202 lstrcpyW(*line_buf + data_pos, concat);
1203 data_pos += concat_len;
1204 column = concat_prefix;
1208 lstrcpyW(*line_buf + data_pos, newline);
1209 HeapFree(GetProcessHeap(), 0, value_multibyte);
1212 /******************************************************************************
1213 * Writes the given line to a file, in multi-byte or wide characters
1215 static void REGPROC_write_line(FILE *file, const WCHAR* str, BOOL unicode)
1217 if(unicode)
1219 fwrite(str, sizeof(WCHAR), lstrlenW(str), file);
1220 } else
1222 char* strA = GetMultiByteString(str);
1223 fputs(strA, file);
1224 HeapFree(GetProcessHeap(), 0, strA);
1228 /******************************************************************************
1229 * Writes contents of the registry key to the specified file stream.
1231 * Parameters:
1232 * file - writable file stream to export registry branch to.
1233 * key - registry branch to export.
1234 * reg_key_name_buf - name of the key with registry class.
1235 * Is resized if necessary.
1236 * reg_key_name_size - length of the buffer for the registry class in characters.
1237 * val_name_buf - buffer for storing value name.
1238 * Is resized if necessary.
1239 * val_name_size - length of the buffer for storing value names in characters.
1240 * val_buf - buffer for storing values while extracting.
1241 * Is resized if necessary.
1242 * val_size - size of the buffer for storing values in bytes.
1244 static void export_hkey(FILE *file, DWORD value_type, BYTE **val_buf, DWORD *val_size,
1245 WCHAR **line_buf, DWORD *line_buf_size, BOOL unicode)
1247 DWORD line_len = 0;
1248 DWORD val_size1 = *val_size;
1249 WCHAR *wstr = (WCHAR *)*val_buf;
1251 if (val_size1 < sizeof(WCHAR) || val_size1 % sizeof(WCHAR) ||
1252 wstr[val_size1 / sizeof(WCHAR) - 1]) {
1253 REGPROC_export_binary(line_buf, line_buf_size, &line_len, value_type, *val_buf, val_size1, unicode);
1254 } else {
1255 const WCHAR start[] = {'"',0};
1256 const WCHAR end[] = {'"','\r','\n',0};
1257 DWORD len;
1259 len = lstrlenW(start);
1260 REGPROC_resize_char_buffer(line_buf, line_buf_size, line_len + len);
1261 lstrcpyW(*line_buf + line_len, start);
1262 line_len += len;
1264 REGPROC_export_string(line_buf, line_buf_size, &line_len, wstr, lstrlenW(wstr));
1266 REGPROC_resize_char_buffer(line_buf, line_buf_size, line_len + lstrlenW(end));
1267 lstrcpyW(*line_buf + line_len, end);
1269 REGPROC_write_line(file, *line_buf, unicode);
1272 /******************************************************************************
1273 * Reads contents of the specified file into the registry.
1275 BOOL import_registry_file(FILE *reg_file)
1277 BYTE s[2];
1278 struct parser parser;
1279 WCHAR *pos;
1281 if (!reg_file || (fread(s, 2, 1, reg_file) != 1))
1282 return FALSE;
1284 parser.is_unicode = (s[0] == 0xff && s[1] == 0xfe);
1285 get_line = parser.is_unicode ? get_lineW : get_lineA;
1287 parser.file = reg_file;
1288 parser.two_wchars[0] = s[0];
1289 parser.two_wchars[1] = s[1];
1290 parser.reg_version = -1;
1291 parser.hkey = NULL;
1292 parser.key_name = NULL;
1293 parser.value_name = NULL;
1294 parser.parse_type = 0;
1295 parser.data_type = 0;
1296 parser.data = NULL;
1297 parser.data_size = 0;
1298 parser.backslash = FALSE;
1299 parser.state = HEADER;
1301 pos = parser.two_wchars;
1303 /* parser main loop */
1304 while (pos)
1305 pos = (parser_funcs[parser.state])(&parser, pos);
1307 if (parser.reg_version == REG_VERSION_FUZZY || parser.reg_version == REG_VERSION_INVALID)
1308 return parser.reg_version == REG_VERSION_FUZZY;
1310 HeapFree(GetProcessHeap(), 0, parser.value_name);
1311 close_key(&parser);
1313 return TRUE;
1316 /******************************************************************************
1317 * Removes the registry key with all subkeys. Parses full key name.
1319 * Parameters:
1320 * reg_key_name - full name of registry branch to delete. Ignored if is NULL,
1321 * empty, points to register key class, does not exist.
1323 void delete_registry_key(WCHAR *reg_key_name)
1325 WCHAR *key_name = NULL;
1326 HKEY key_class;
1328 if (!reg_key_name || !reg_key_name[0])
1329 return;
1331 if (!(key_class = parse_key_name(reg_key_name, &key_name)))
1333 if (key_name) *(key_name - 1) = 0;
1334 error_exit(STRING_INVALID_SYSTEM_KEY, reg_key_name);
1337 if (!*key_name)
1338 error_exit(STRING_DELETE_FAILED, reg_key_name);
1340 RegDeleteTreeW(key_class, key_name);
1343 static WCHAR *REGPROC_escape_string(WCHAR *str, size_t str_len, size_t *line_len)
1345 size_t i, escape_count, pos;
1346 WCHAR *buf;
1348 for (i = 0, escape_count = 0; i < str_len; i++)
1350 WCHAR c = str[i];
1351 if (c == '\r' || c == '\n' || c == '\\' || c == '"' || c == '\0')
1352 escape_count++;
1355 buf = resize_buffer(NULL, (str_len + escape_count + 1) * sizeof(WCHAR));
1357 for (i = 0, pos = 0; i < str_len; i++, pos++)
1359 WCHAR c = str[i];
1361 switch (c)
1363 case '\r':
1364 buf[pos++] = '\\';
1365 buf[pos] = 'r';
1366 break;
1367 case '\n':
1368 buf[pos++] = '\\';
1369 buf[pos] = 'n';
1370 break;
1371 case '\\':
1372 buf[pos++] = '\\';
1373 buf[pos] = '\\';
1374 break;
1375 case '"':
1376 buf[pos++] = '\\';
1377 buf[pos] = '"';
1378 break;
1379 case '\0':
1380 buf[pos++] = '\\';
1381 buf[pos] = '0';
1382 break;
1383 default:
1384 buf[pos] = c;
1388 buf[pos] = 0;
1389 *line_len = pos;
1390 return buf;
1393 static size_t export_value_name(FILE *fp, WCHAR *name, size_t len, BOOL unicode)
1395 static const WCHAR quoted_fmt[] = {'"','%','s','"','=',0};
1396 static const WCHAR default_name[] = {'@','=',0};
1397 size_t line_len;
1399 if (name && *name)
1401 WCHAR *str = REGPROC_escape_string(name, len, &line_len);
1402 WCHAR *buf = resize_buffer(NULL, (line_len + 4) * sizeof(WCHAR));
1403 line_len = sprintfW(buf, quoted_fmt, str);
1404 REGPROC_write_line(fp, buf, unicode);
1405 HeapFree(GetProcessHeap(), 0, buf);
1406 HeapFree(GetProcessHeap(), 0, str);
1408 else
1410 line_len = lstrlenW(default_name);
1411 REGPROC_write_line(fp, default_name, unicode);
1414 return line_len;
1417 static void export_dword_data(WCHAR **buf, DWORD *data)
1419 static const WCHAR fmt[] = {'d','w','o','r','d',':','%','0','8','x',0};
1421 *buf = resize_buffer(NULL, 15 * sizeof(WCHAR));
1422 sprintfW(*buf, fmt, *data);
1425 static size_t export_hex_data_type(FILE *fp, DWORD type, BOOL unicode)
1427 static const WCHAR hex[] = {'h','e','x',':',0};
1428 static const WCHAR hexp_fmt[] = {'h','e','x','(','%','x',')',':',0};
1429 size_t line_len;
1431 if (type == REG_BINARY)
1433 line_len = lstrlenW(hex);
1434 REGPROC_write_line(fp, hex, unicode);
1436 else
1438 WCHAR *buf = resize_buffer(NULL, 15 * sizeof(WCHAR));
1439 line_len = sprintfW(buf, hexp_fmt, type);
1440 REGPROC_write_line(fp, buf, unicode);
1441 HeapFree(GetProcessHeap(), 0, buf);
1444 return line_len;
1447 #define MAX_HEX_CHARS 77
1449 static void export_hex_data(FILE *fp, WCHAR **buf, DWORD type, DWORD line_len,
1450 void *data, DWORD size, BOOL unicode)
1452 static const WCHAR fmt[] = {'%','0','2','x',0};
1453 static const WCHAR hex_concat[] = {'\\','\r','\n',' ',' ',0};
1454 size_t num_commas, i, pos;
1456 line_len += export_hex_data_type(fp, type, unicode);
1458 if (!unicode && (type == REG_EXPAND_SZ || type == REG_MULTI_SZ))
1459 data = GetMultiByteStringN(data, size / sizeof(WCHAR), &size);
1461 num_commas = size - 1;
1462 *buf = resize_buffer(NULL, (size * 3) * sizeof(WCHAR));
1464 for (i = 0, pos = 0; i < size; i++)
1466 pos += sprintfW(*buf + pos, fmt, ((BYTE *)data)[i]);
1467 if (i == num_commas) break;
1468 (*buf)[pos++] = ',';
1469 (*buf)[pos] = 0;
1470 line_len += 3;
1472 if (line_len >= MAX_HEX_CHARS)
1474 REGPROC_write_line(fp, *buf, unicode);
1475 REGPROC_write_line(fp, hex_concat, unicode);
1476 line_len = 2;
1477 pos = 0;
1482 static void export_data(FILE *fp, DWORD type, size_t line_len, void *data, size_t size, BOOL unicode)
1484 WCHAR *buf = NULL;
1485 static const WCHAR newline[] = {'\r','\n',0};
1487 switch (type)
1489 case REG_DWORD:
1490 export_dword_data(&buf, data);
1491 break;
1492 case REG_NONE:
1493 case REG_EXPAND_SZ:
1494 case REG_BINARY:
1495 case REG_MULTI_SZ:
1496 default:
1497 export_hex_data(fp, &buf, type, line_len, data, size, unicode);
1498 break;
1501 REGPROC_write_line(fp, buf, unicode);
1502 HeapFree(GetProcessHeap(), 0, buf);
1503 REGPROC_write_line(fp, newline, unicode);
1506 static WCHAR *build_subkey_path(WCHAR *path, DWORD path_len, WCHAR *subkey_name, DWORD subkey_len)
1508 WCHAR *subkey_path;
1509 static const WCHAR fmt[] = {'%','s','\\','%','s',0};
1511 subkey_path = resize_buffer(NULL, (path_len + subkey_len + 2) * sizeof(WCHAR));
1512 sprintfW(subkey_path, fmt, path, subkey_name);
1514 return subkey_path;
1517 static void export_key_name(FILE *fp, WCHAR *name, BOOL unicode)
1519 static const WCHAR fmt[] = {'\r','\n','[','%','s',']','\r','\n',0};
1520 WCHAR *buf;
1522 buf = resize_buffer(NULL, (lstrlenW(name) + 7) * sizeof(WCHAR));
1523 sprintfW(buf, fmt, name);
1524 REGPROC_write_line(fp, buf, unicode);
1525 HeapFree(GetProcessHeap(), 0, buf);
1528 #define MAX_SUBKEY_LEN 257
1530 static int export_registry_data(FILE *fp, HKEY key, WCHAR *path, BOOL unicode)
1532 WCHAR *line_buf;
1533 DWORD line_buf_size = KEY_MAX_LEN + REG_VAL_BUF_SIZE;
1534 LONG rc;
1535 DWORD max_value_len = 256, value_len;
1536 DWORD max_data_bytes = 2048, data_size;
1537 DWORD subkey_len;
1538 DWORD i, type, path_len;
1539 WCHAR *value_name, *subkey_name, *subkey_path;
1540 BYTE *data;
1541 HKEY subkey;
1543 line_buf = HeapAlloc(GetProcessHeap(), 0, line_buf_size * sizeof(WCHAR));
1544 CHECK_ENOUGH_MEMORY(line_buf);
1546 export_key_name(fp, path, unicode);
1548 value_name = resize_buffer(NULL, max_value_len * sizeof(WCHAR));
1549 data = resize_buffer(NULL, max_data_bytes);
1551 i = 0;
1552 for (;;)
1554 value_len = max_value_len;
1555 data_size = max_data_bytes;
1556 rc = RegEnumValueW(key, i, value_name, &value_len, NULL, &type, data, &data_size);
1557 if (rc == ERROR_SUCCESS)
1559 size_t line_len = export_value_name(fp, value_name, value_len, unicode);
1560 if (type != REG_SZ)
1561 export_data(fp, type, line_len, data, data_size, unicode);
1562 else
1563 export_hkey(fp, type, &data, &data_size, &line_buf, &line_buf_size, unicode);
1564 i++;
1566 else if (rc == ERROR_MORE_DATA)
1568 if (data_size > max_data_bytes)
1570 max_data_bytes *= 2;
1571 data = resize_buffer(data, max_data_bytes);
1573 else
1575 max_value_len *= 2;
1576 value_name = resize_buffer(value_name, max_value_len * sizeof(WCHAR));
1579 else break;
1582 HeapFree(GetProcessHeap(), 0, data);
1583 HeapFree(GetProcessHeap(), 0, value_name);
1584 HeapFree(GetProcessHeap(), 0, line_buf);
1586 subkey_name = resize_buffer(NULL, MAX_SUBKEY_LEN * sizeof(WCHAR));
1588 path_len = lstrlenW(path);
1590 i = 0;
1591 for (;;)
1593 subkey_len = MAX_SUBKEY_LEN;
1594 rc = RegEnumKeyExW(key, i, subkey_name, &subkey_len, NULL, NULL, NULL, NULL);
1595 if (rc == ERROR_SUCCESS)
1597 subkey_path = build_subkey_path(path, path_len, subkey_name, subkey_len);
1598 if (!RegOpenKeyExW(key, subkey_name, 0, KEY_READ, &subkey))
1600 export_registry_data(fp, subkey, subkey_path, unicode);
1601 RegCloseKey(subkey);
1603 HeapFree(GetProcessHeap(), 0, subkey_path);
1604 i++;
1606 else break;
1609 HeapFree(GetProcessHeap(), 0, subkey_name);
1610 return 0;
1613 static FILE *REGPROC_open_export_file(WCHAR *file_name, BOOL unicode)
1615 FILE *file;
1616 static const WCHAR hyphen[] = {'-',0};
1618 if (!strcmpW(file_name, hyphen))
1620 file = stdout;
1621 _setmode(_fileno(file), _O_BINARY);
1623 else
1625 static const WCHAR wb_mode[] = {'w','b',0};
1627 file = _wfopen(file_name, wb_mode);
1628 if (!file)
1630 static const WCHAR regedit[] = {'r','e','g','e','d','i','t',0};
1631 _wperror(regedit);
1632 error_exit(STRING_CANNOT_OPEN_FILE, file_name);
1636 if (unicode)
1638 static const BYTE bom[] = {0xff,0xfe};
1639 static const WCHAR header[] = {'W','i','n','d','o','w','s',' ',
1640 'R','e','g','i','s','t','r','y',' ','E','d','i','t','o','r',' ',
1641 'V','e','r','s','i','o','n',' ','5','.','0','0','\r','\n'};
1643 fwrite(bom, sizeof(BYTE), ARRAY_SIZE(bom), file);
1644 fwrite(header, sizeof(WCHAR), ARRAY_SIZE(header), file);
1646 else
1647 fputs("REGEDIT4\r\n", file);
1649 return file;
1652 static HKEY open_export_key(HKEY key_class, WCHAR *subkey, WCHAR *path)
1654 HKEY key;
1656 if (!RegOpenKeyExW(key_class, subkey, 0, KEY_READ, &key))
1657 return key;
1659 output_message(STRING_OPEN_KEY_FAILED, path);
1660 return NULL;
1663 static BOOL export_key(WCHAR *file_name, WCHAR *path, BOOL unicode)
1665 HKEY key_class, key;
1666 WCHAR *subkey;
1667 FILE *fp;
1668 BOOL ret;
1670 if (!(key_class = parse_key_name(path, &subkey)))
1672 if (subkey) *(subkey - 1) = 0;
1673 output_message(STRING_INVALID_SYSTEM_KEY, path);
1674 return FALSE;
1677 if (!(key = open_export_key(key_class, subkey, path)))
1678 return FALSE;
1680 fp = REGPROC_open_export_file(file_name, unicode);
1681 ret = export_registry_data(fp, key, path, unicode);
1682 fclose(fp);
1684 RegCloseKey(key);
1685 return ret;
1688 static BOOL export_all(WCHAR *file_name, WCHAR *path, BOOL unicode)
1690 FILE *fp;
1691 int i;
1692 HKEY classes[] = {HKEY_LOCAL_MACHINE, HKEY_USERS}, key;
1693 WCHAR *class_name;
1695 fp = REGPROC_open_export_file(file_name, unicode);
1697 for (i = 0; i < ARRAY_SIZE(classes); i++)
1699 if (!(key = open_export_key(classes[i], NULL, path)))
1701 fclose(fp);
1702 return FALSE;
1705 class_name = resize_buffer(NULL, (lstrlenW(reg_class_namesW[i]) + 1) * sizeof(WCHAR));
1706 lstrcpyW(class_name, reg_class_namesW[i]);
1708 export_registry_data(fp, classes[i], class_name, unicode);
1710 HeapFree(GetProcessHeap(), 0, class_name);
1711 RegCloseKey(key);
1714 fclose(fp);
1715 return TRUE;
1718 BOOL export_registry_key(WCHAR *file_name, WCHAR *path, DWORD format)
1720 BOOL unicode = (format == REG_FORMAT_5);
1722 if (path && *path)
1723 return export_key(file_name, path, unicode);
1724 else
1725 return export_all(file_name, path, unicode);