wined3d: Use wined3d_bit_scan() in vshader_get_input().
[wine.git] / programs / regedit / regproc.c
blob5bb991865e6454a8b4bcc5e9bcdb5f7240d5d4a1
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 <stdlib.h>
26 #include <fcntl.h>
27 #include <io.h>
28 #include <windows.h>
29 #include <commctrl.h>
30 #include <wine/debug.h>
31 #include <wine/heap.h>
32 #include "main.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(regedit);
36 #define REG_VAL_BUF_SIZE 4096
38 static HKEY reg_class_keys[] = {
39 HKEY_LOCAL_MACHINE, HKEY_USERS, HKEY_CLASSES_ROOT,
40 HKEY_CURRENT_CONFIG, HKEY_CURRENT_USER, HKEY_DYN_DATA
43 void *heap_xalloc(size_t size)
45 void *buf = heap_alloc(size);
46 if (!buf)
48 ERR("Out of memory!\n");
49 exit(1);
51 return buf;
54 void *heap_xrealloc(void *buf, size_t size)
56 void *new_buf = heap_realloc(buf, size);
58 if (!new_buf)
60 ERR("Out of memory!\n");
61 exit(1);
64 return new_buf;
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 = heap_xalloc(len * sizeof(WCHAR));
79 MultiByteToWideChar(CP_ACP, 0, strA, -1, strW, len);
80 return strW;
82 return NULL;
85 /******************************************************************************
86 * Allocates memory and converts input from multibyte to wide chars
87 * Returned string must be freed by the caller
89 static WCHAR* GetWideStringN(const char* strA, int chars, DWORD *len)
91 if(strA)
93 WCHAR* strW;
94 *len = MultiByteToWideChar(CP_ACP, 0, strA, chars, NULL, 0);
96 strW = heap_xalloc(*len * sizeof(WCHAR));
97 MultiByteToWideChar(CP_ACP, 0, strA, chars, strW, *len);
98 return strW;
100 *len = 0;
101 return NULL;
104 /******************************************************************************
105 * Allocates memory and converts input from wide chars to multibyte
106 * Returned string must be freed by the caller
108 char* GetMultiByteString(const WCHAR* strW)
110 if(strW)
112 char* strA;
113 int len = WideCharToMultiByte(CP_ACP, 0, strW, -1, NULL, 0, NULL, NULL);
115 strA = heap_xalloc(len);
116 WideCharToMultiByte(CP_ACP, 0, strW, -1, strA, len, NULL, NULL);
117 return strA;
119 return NULL;
122 /******************************************************************************
123 * Allocates memory and converts input from wide chars to multibyte
124 * Returned string must be freed by the caller
126 static char* GetMultiByteStringN(const WCHAR* strW, int chars, DWORD* len)
128 if(strW)
130 char* strA;
131 *len = WideCharToMultiByte(CP_ACP, 0, strW, chars, NULL, 0, NULL, NULL);
133 strA = heap_xalloc(*len);
134 WideCharToMultiByte(CP_ACP, 0, strW, chars, strA, *len, NULL, NULL);
135 return strA;
137 *len = 0;
138 return NULL;
141 static WCHAR *(*get_line)(FILE *);
143 /* parser definitions */
144 enum parser_state
146 HEADER, /* parsing the registry file version header */
147 PARSE_WIN31_LINE, /* parsing a Windows 3.1 registry line */
148 LINE_START, /* at the beginning of a registry line */
149 KEY_NAME, /* parsing a key name */
150 DELETE_KEY, /* deleting a registry key */
151 DEFAULT_VALUE_NAME, /* parsing a default value name */
152 QUOTED_VALUE_NAME, /* parsing a double-quoted value name */
153 DATA_START, /* preparing for data parsing operations */
154 DELETE_VALUE, /* deleting a registry value */
155 DATA_TYPE, /* parsing the registry data type */
156 STRING_DATA, /* parsing REG_SZ data */
157 DWORD_DATA, /* parsing DWORD data */
158 HEX_DATA, /* parsing REG_BINARY, REG_NONE, REG_EXPAND_SZ or REG_MULTI_SZ data */
159 EOL_BACKSLASH, /* preparing to parse multiple lines of hex data */
160 HEX_MULTILINE, /* parsing multiple lines of hex data */
161 UNKNOWN_DATA, /* parsing an unhandled or invalid data type */
162 SET_VALUE, /* adding a value to the registry */
163 NB_PARSER_STATES
166 struct parser
168 FILE *file; /* pointer to a registry file */
169 WCHAR two_wchars[2]; /* first two characters from the encoding check */
170 BOOL is_unicode; /* parsing Unicode or ASCII data */
171 short int reg_version; /* registry file version */
172 HKEY hkey; /* current registry key */
173 WCHAR *key_name; /* current key name */
174 WCHAR *value_name; /* value name */
175 DWORD parse_type; /* generic data type for parsing */
176 DWORD data_type; /* data type */
177 void *data; /* value data */
178 DWORD data_size; /* size of the data (in bytes) */
179 BOOL backslash; /* TRUE if the current line contains a backslash */
180 enum parser_state state; /* current parser state */
183 typedef WCHAR *(*parser_state_func)(struct parser *parser, WCHAR *pos);
185 /* parser state machine functions */
186 static WCHAR *header_state(struct parser *parser, WCHAR *pos);
187 static WCHAR *parse_win31_line_state(struct parser *parser, WCHAR *pos);
188 static WCHAR *line_start_state(struct parser *parser, WCHAR *pos);
189 static WCHAR *key_name_state(struct parser *parser, WCHAR *pos);
190 static WCHAR *delete_key_state(struct parser *parser, WCHAR *pos);
191 static WCHAR *default_value_name_state(struct parser *parser, WCHAR *pos);
192 static WCHAR *quoted_value_name_state(struct parser *parser, WCHAR *pos);
193 static WCHAR *data_start_state(struct parser *parser, WCHAR *pos);
194 static WCHAR *delete_value_state(struct parser *parser, WCHAR *pos);
195 static WCHAR *data_type_state(struct parser *parser, WCHAR *pos);
196 static WCHAR *string_data_state(struct parser *parser, WCHAR *pos);
197 static WCHAR *dword_data_state(struct parser *parser, WCHAR *pos);
198 static WCHAR *hex_data_state(struct parser *parser, WCHAR *pos);
199 static WCHAR *eol_backslash_state(struct parser *parser, WCHAR *pos);
200 static WCHAR *hex_multiline_state(struct parser *parser, WCHAR *pos);
201 static WCHAR *unknown_data_state(struct parser *parser, WCHAR *pos);
202 static WCHAR *set_value_state(struct parser *parser, WCHAR *pos);
204 static const parser_state_func parser_funcs[NB_PARSER_STATES] =
206 header_state, /* HEADER */
207 parse_win31_line_state, /* PARSE_WIN31_LINE */
208 line_start_state, /* LINE_START */
209 key_name_state, /* KEY_NAME */
210 delete_key_state, /* DELETE_KEY */
211 default_value_name_state, /* DEFAULT_VALUE_NAME */
212 quoted_value_name_state, /* QUOTED_VALUE_NAME */
213 data_start_state, /* DATA_START */
214 delete_value_state, /* DELETE_VALUE */
215 data_type_state, /* DATA_TYPE */
216 string_data_state, /* STRING_DATA */
217 dword_data_state, /* DWORD_DATA */
218 hex_data_state, /* HEX_DATA */
219 eol_backslash_state, /* EOL_BACKSLASH */
220 hex_multiline_state, /* HEX_MULTILINE */
221 unknown_data_state, /* UNKNOWN_DATA */
222 set_value_state, /* SET_VALUE */
225 /* set the new parser state and return the previous one */
226 static inline enum parser_state set_state(struct parser *parser, enum parser_state state)
228 enum parser_state ret = parser->state;
229 parser->state = state;
230 return ret;
233 /******************************************************************************
234 * Converts a hex representation of a DWORD into a DWORD.
236 static BOOL convert_hex_to_dword(WCHAR *str, DWORD *dw)
238 WCHAR *p, *end;
239 int count = 0;
241 while (*str == ' ' || *str == '\t') str++;
242 if (!*str) goto error;
244 p = str;
245 while (iswxdigit(*p))
247 count++;
248 p++;
250 if (count > 8) goto error;
252 end = p;
253 while (*p == ' ' || *p == '\t') p++;
254 if (*p && *p != ';') goto error;
256 *end = 0;
257 *dw = wcstoul(str, &end, 16);
258 return TRUE;
260 error:
261 return FALSE;
264 /******************************************************************************
265 * Converts comma-separated hex data into a binary string and modifies
266 * the input parameter to skip the concatenating backslash, if found.
268 * Returns TRUE or FALSE to indicate whether parsing was successful.
270 static BOOL convert_hex_csv_to_hex(struct parser *parser, WCHAR **str)
272 size_t size;
273 BYTE *d;
274 WCHAR *s;
276 parser->backslash = FALSE;
278 /* The worst case is 1 digit + 1 comma per byte */
279 size = ((lstrlenW(*str) + 1) / 2) + parser->data_size;
280 parser->data = heap_xrealloc(parser->data, size);
282 s = *str;
283 d = (BYTE *)parser->data + parser->data_size;
285 while (*s)
287 WCHAR *end;
288 unsigned long wc;
290 wc = wcstoul(s, &end, 16);
291 if (wc > 0xff) return FALSE;
293 if (s == end && wc == 0)
295 while (*end == ' ' || *end == '\t') end++;
296 if (*end == '\\')
298 parser->backslash = TRUE;
299 *str = end + 1;
300 return TRUE;
302 else if (*end == ';')
303 return TRUE;
304 return FALSE;
307 *d++ = wc;
308 parser->data_size++;
310 if (*end && *end != ',')
312 while (*end == ' ' || *end == '\t') end++;
313 if (*end && *end != ';') return FALSE;
314 return TRUE;
317 if (*end) end++;
318 s = end;
321 return TRUE;
324 /******************************************************************************
325 * Parses the data type of the registry value being imported and modifies
326 * the input parameter to skip the string representation of the data type.
328 * Returns TRUE or FALSE to indicate whether a data type was found.
330 static BOOL parse_data_type(struct parser *parser, WCHAR **line)
332 struct data_type { const WCHAR *tag; int len; int type; int parse_type; };
334 static const WCHAR quote[] = {'"'};
335 static const WCHAR hex[] = {'h','e','x',':'};
336 static const WCHAR dword[] = {'d','w','o','r','d',':'};
337 static const WCHAR hexp[] = {'h','e','x','('};
339 static const struct data_type data_types[] = {
340 /* tag len type parse type */
341 { quote, 1, REG_SZ, REG_SZ },
342 { hex, 4, REG_BINARY, REG_BINARY },
343 { dword, 6, REG_DWORD, REG_DWORD },
344 { hexp, 4, -1, REG_BINARY }, /* REG_NONE, REG_EXPAND_SZ, REG_MULTI_SZ */
345 { NULL, 0, 0, 0 }
348 const struct data_type *ptr;
350 for (ptr = data_types; ptr->tag; ptr++)
352 if (wcsncmp(ptr->tag, *line, ptr->len))
353 continue;
355 parser->parse_type = ptr->parse_type;
356 parser->data_type = ptr->parse_type;
357 *line += ptr->len;
359 if (ptr->type == -1)
361 WCHAR *end;
362 DWORD val;
364 if (!**line || towlower((*line)[1]) == 'x')
365 return FALSE;
367 /* "hex(xx):" is special */
368 val = wcstoul(*line, &end, 16);
369 if (*end != ')' || *(end + 1) != ':' || (val == ~0u && errno == ERANGE))
370 return FALSE;
372 parser->data_type = val;
373 *line = end + 2;
375 return TRUE;
377 return FALSE;
380 /******************************************************************************
381 * Replaces escape sequences with their character equivalents and
382 * null-terminates the string on the first non-escaped double quote.
384 * Assigns a pointer to the remaining unparsed data in the line.
385 * Returns TRUE or FALSE to indicate whether a closing double quote was found.
387 static BOOL REGPROC_unescape_string(WCHAR *str, WCHAR **unparsed)
389 int str_idx = 0; /* current character under analysis */
390 int val_idx = 0; /* the last character of the unescaped string */
391 int len = lstrlenW(str);
392 BOOL ret;
394 for (str_idx = 0; str_idx < len; str_idx++, val_idx++) {
395 if (str[str_idx] == '\\') {
396 str_idx++;
397 switch (str[str_idx]) {
398 case 'n':
399 str[val_idx] = '\n';
400 break;
401 case 'r':
402 str[val_idx] = '\r';
403 break;
404 case '0':
405 return FALSE;
406 case '\\':
407 case '"':
408 str[val_idx] = str[str_idx];
409 break;
410 default:
411 if (!str[str_idx]) return FALSE;
412 output_message(STRING_ESCAPE_SEQUENCE, str[str_idx]);
413 str[val_idx] = str[str_idx];
414 break;
416 } else if (str[str_idx] == '"') {
417 break;
418 } else {
419 str[val_idx] = str[str_idx];
423 ret = (str[str_idx] == '"');
424 *unparsed = str + str_idx + 1;
425 str[val_idx] = '\0';
426 return ret;
429 static HKEY parse_key_name(WCHAR *key_name, WCHAR **key_path)
431 unsigned int i;
433 if (!key_name) return 0;
435 *key_path = wcschr(key_name, '\\');
436 if (*key_path) (*key_path)++;
438 for (i = 0; i < ARRAY_SIZE(reg_class_keys); i++)
440 int len = lstrlenW(reg_class_namesW[i]);
441 if (!wcsnicmp(key_name, reg_class_namesW[i], len) &&
442 (key_name[len] == 0 || key_name[len] == '\\'))
444 return reg_class_keys[i];
448 return 0;
451 static void close_key(struct parser *parser)
453 if (parser->hkey)
455 heap_free(parser->key_name);
456 parser->key_name = NULL;
458 RegCloseKey(parser->hkey);
459 parser->hkey = NULL;
463 /******************************************************************************
464 * Opens the registry key given by the input path.
465 * This key must be closed by calling close_key().
467 static LONG open_key(struct parser *parser, WCHAR *path)
469 HKEY key_class;
470 WCHAR *key_path;
471 LONG res;
473 close_key(parser);
475 /* Get the registry class */
476 if (!path || !(key_class = parse_key_name(path, &key_path)))
477 return ERROR_INVALID_PARAMETER;
479 res = RegCreateKeyExW(key_class, key_path, 0, NULL, REG_OPTION_NON_VOLATILE,
480 KEY_ALL_ACCESS, NULL, &parser->hkey, NULL);
482 if (res == ERROR_SUCCESS)
484 parser->key_name = heap_xalloc((lstrlenW(path) + 1) * sizeof(WCHAR));
485 lstrcpyW(parser->key_name, path);
487 else
488 parser->hkey = NULL;
490 return res;
493 static void free_parser_data(struct parser *parser)
495 if (parser->parse_type == REG_DWORD || parser->parse_type == REG_BINARY)
496 heap_free(parser->data);
498 parser->data = NULL;
499 parser->data_size = 0;
502 static void prepare_hex_string_data(struct parser *parser)
504 if (parser->data_type == REG_EXPAND_SZ || parser->data_type == REG_MULTI_SZ ||
505 parser->data_type == REG_SZ)
507 if (parser->is_unicode)
509 WCHAR *data = parser->data;
510 DWORD len = parser->data_size / sizeof(WCHAR);
512 if (data[len - 1] != 0)
514 data[len] = 0;
515 parser->data_size += sizeof(WCHAR);
518 else
520 BYTE *data = parser->data;
522 if (data[parser->data_size - 1] != 0)
524 data[parser->data_size] = 0;
525 parser->data_size++;
528 parser->data = GetWideStringN(parser->data, parser->data_size, &parser->data_size);
529 parser->data_size *= sizeof(WCHAR);
530 heap_free(data);
535 enum reg_versions {
536 REG_VERSION_31,
537 REG_VERSION_40,
538 REG_VERSION_50,
539 REG_VERSION_FUZZY,
540 REG_VERSION_INVALID
543 static enum reg_versions parse_file_header(const WCHAR *s)
545 static const WCHAR header_31[] = {'R','E','G','E','D','I','T',0};
546 static const WCHAR header_40[] = {'R','E','G','E','D','I','T','4',0};
547 static const WCHAR header_50[] = {'W','i','n','d','o','w','s',' ',
548 'R','e','g','i','s','t','r','y',' ','E','d','i','t','o','r',' ',
549 'V','e','r','s','i','o','n',' ','5','.','0','0',0};
551 while (*s == ' ' || *s == '\t') s++;
553 if (!lstrcmpW(s, header_31))
554 return REG_VERSION_31;
556 if (!lstrcmpW(s, header_40))
557 return REG_VERSION_40;
559 if (!lstrcmpW(s, header_50))
560 return REG_VERSION_50;
562 /* The Windows version accepts registry file headers beginning with "REGEDIT" and ending
563 * with other characters, as long as "REGEDIT" appears at the start of the line. For example,
564 * "REGEDIT 4", "REGEDIT9" and "REGEDIT4FOO" are all treated as valid file headers.
565 * In all such cases, however, the contents of the registry file are not imported.
567 if (!wcsncmp(s, header_31, 7)) /* "REGEDIT" without NUL */
568 return REG_VERSION_FUZZY;
570 return REG_VERSION_INVALID;
573 /* handler for parser HEADER state */
574 static WCHAR *header_state(struct parser *parser, WCHAR *pos)
576 WCHAR *line, *header;
578 if (!(line = get_line(parser->file)))
579 return NULL;
581 if (!parser->is_unicode)
583 header = heap_xalloc((lstrlenW(line) + 3) * sizeof(WCHAR));
584 header[0] = parser->two_wchars[0];
585 header[1] = parser->two_wchars[1];
586 lstrcpyW(header + 2, line);
587 parser->reg_version = parse_file_header(header);
588 heap_free(header);
590 else parser->reg_version = parse_file_header(line);
592 switch (parser->reg_version)
594 case REG_VERSION_31:
595 set_state(parser, PARSE_WIN31_LINE);
596 break;
597 case REG_VERSION_40:
598 case REG_VERSION_50:
599 set_state(parser, LINE_START);
600 break;
601 default:
602 get_line(NULL); /* Reset static variables */
603 return NULL;
606 return line;
609 /* handler for parser PARSE_WIN31_LINE state */
610 static WCHAR *parse_win31_line_state(struct parser *parser, WCHAR *pos)
612 WCHAR *line, *value;
613 static WCHAR hkcr[] = {'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T'};
614 unsigned int key_end = 0;
616 if (!(line = get_line(parser->file)))
617 return NULL;
619 if (wcsncmp(line, hkcr, ARRAY_SIZE(hkcr)))
620 return line;
622 /* get key name */
623 while (line[key_end] && !iswspace(line[key_end])) key_end++;
625 value = line + key_end;
626 while (*value == ' ' || *value == '\t') value++;
628 if (*value == '=') value++;
629 if (*value == ' ') value++; /* at most one space is skipped */
631 line[key_end] = 0;
633 if (open_key(parser, line) != ERROR_SUCCESS)
635 output_message(STRING_OPEN_KEY_FAILED, line);
636 return line;
639 parser->value_name = NULL;
640 parser->data_type = REG_SZ;
641 parser->data = value;
642 parser->data_size = (lstrlenW(value) + 1) * sizeof(WCHAR);
644 set_state(parser, SET_VALUE);
645 return value;
648 /* handler for parser LINE_START state */
649 static WCHAR *line_start_state(struct parser *parser, WCHAR *pos)
651 WCHAR *line, *p;
653 if (!(line = get_line(parser->file)))
654 return NULL;
656 for (p = line; *p; p++)
658 switch (*p)
660 case '[':
661 set_state(parser, KEY_NAME);
662 return p + 1;
663 case '@':
664 set_state(parser, DEFAULT_VALUE_NAME);
665 return p;
666 case '"':
667 set_state(parser, QUOTED_VALUE_NAME);
668 return p + 1;
669 case ' ':
670 case '\t':
671 break;
672 default:
673 return p;
677 return p;
680 /* handler for parser KEY_NAME state */
681 static WCHAR *key_name_state(struct parser *parser, WCHAR *pos)
683 WCHAR *p = pos, *key_end;
685 if (*p == ' ' || *p == '\t' || !(key_end = wcsrchr(p, ']')))
686 goto done;
688 *key_end = 0;
690 if (*p == '-')
692 set_state(parser, DELETE_KEY);
693 return p + 1;
695 else if (open_key(parser, p) != ERROR_SUCCESS)
696 output_message(STRING_OPEN_KEY_FAILED, p);
698 done:
699 set_state(parser, LINE_START);
700 return p;
703 /* handler for parser DELETE_KEY state */
704 static WCHAR *delete_key_state(struct parser *parser, WCHAR *pos)
706 WCHAR *p = pos;
708 close_key(parser);
710 if (*p == 'H' || *p == 'h')
711 delete_registry_key(p);
713 set_state(parser, LINE_START);
714 return p;
717 /* handler for parser DEFAULT_VALUE_NAME state */
718 static WCHAR *default_value_name_state(struct parser *parser, WCHAR *pos)
720 heap_free(parser->value_name);
721 parser->value_name = NULL;
723 set_state(parser, DATA_START);
724 return pos + 1;
727 /* handler for parser QUOTED_VALUE_NAME state */
728 static WCHAR *quoted_value_name_state(struct parser *parser, WCHAR *pos)
730 WCHAR *val_name = pos, *p;
732 heap_free(parser->value_name);
733 parser->value_name = NULL;
735 if (!REGPROC_unescape_string(val_name, &p))
736 goto invalid;
738 /* copy the value name in case we need to parse multiple lines and the buffer is overwritten */
739 parser->value_name = heap_xalloc((lstrlenW(val_name) + 1) * sizeof(WCHAR));
740 lstrcpyW(parser->value_name, val_name);
742 set_state(parser, DATA_START);
743 return p;
745 invalid:
746 set_state(parser, LINE_START);
747 return val_name;
750 /* handler for parser DATA_START state */
751 static WCHAR *data_start_state(struct parser *parser, WCHAR *pos)
753 WCHAR *p = pos;
754 unsigned int len;
756 while (*p == ' ' || *p == '\t') p++;
757 if (*p != '=') goto done;
758 p++;
759 while (*p == ' ' || *p == '\t') p++;
761 /* trim trailing whitespace */
762 len = lstrlenW(p);
763 while (len > 0 && (p[len - 1] == ' ' || p[len - 1] == '\t')) len--;
764 p[len] = 0;
766 if (*p == '-')
767 set_state(parser, DELETE_VALUE);
768 else
769 set_state(parser, DATA_TYPE);
770 return p;
772 done:
773 set_state(parser, LINE_START);
774 return p;
777 /* handler for parser DELETE_VALUE state */
778 static WCHAR *delete_value_state(struct parser *parser, WCHAR *pos)
780 WCHAR *p = pos + 1;
782 while (*p == ' ' || *p == '\t') p++;
783 if (*p && *p != ';') goto done;
785 RegDeleteValueW(parser->hkey, parser->value_name);
787 done:
788 set_state(parser, LINE_START);
789 return p;
792 /* handler for parser DATA_TYPE state */
793 static WCHAR *data_type_state(struct parser *parser, WCHAR *pos)
795 WCHAR *line = pos;
797 if (!parse_data_type(parser, &line))
799 set_state(parser, LINE_START);
800 return line;
803 switch (parser->parse_type)
805 case REG_SZ:
806 set_state(parser, STRING_DATA);
807 break;
808 case REG_DWORD:
809 set_state(parser, DWORD_DATA);
810 break;
811 case REG_BINARY: /* all hex data types, including undefined */
812 set_state(parser, HEX_DATA);
813 break;
814 default:
815 set_state(parser, UNKNOWN_DATA);
818 return line;
821 /* handler for parser STRING_DATA state */
822 static WCHAR *string_data_state(struct parser *parser, WCHAR *pos)
824 WCHAR *line;
826 parser->data = pos;
828 if (!REGPROC_unescape_string(parser->data, &line))
829 goto invalid;
831 while (*line == ' ' || *line == '\t') line++;
832 if (*line && *line != ';') goto invalid;
834 parser->data_size = (lstrlenW(parser->data) + 1) * sizeof(WCHAR);
836 set_state(parser, SET_VALUE);
837 return line;
839 invalid:
840 free_parser_data(parser);
841 set_state(parser, LINE_START);
842 return line;
845 /* handler for parser DWORD_DATA state */
846 static WCHAR *dword_data_state(struct parser *parser, WCHAR *pos)
848 WCHAR *line = pos;
850 parser->data = heap_xalloc(sizeof(DWORD));
852 if (!convert_hex_to_dword(line, parser->data))
853 goto invalid;
855 parser->data_size = sizeof(DWORD);
857 set_state(parser, SET_VALUE);
858 return line;
860 invalid:
861 free_parser_data(parser);
862 set_state(parser, LINE_START);
863 return line;
866 /* handler for parser HEX_DATA state */
867 static WCHAR *hex_data_state(struct parser *parser, WCHAR *pos)
869 WCHAR *line = pos;
871 if (!*line)
872 goto set_value;
874 if (!convert_hex_csv_to_hex(parser, &line))
875 goto invalid;
877 if (parser->backslash)
879 set_state(parser, EOL_BACKSLASH);
880 return line;
883 prepare_hex_string_data(parser);
885 set_value:
886 set_state(parser, SET_VALUE);
887 return line;
889 invalid:
890 free_parser_data(parser);
891 set_state(parser, LINE_START);
892 return line;
895 /* handler for parser EOL_BACKSLASH state */
896 static WCHAR *eol_backslash_state(struct parser *parser, WCHAR *pos)
898 WCHAR *p = pos;
900 while (*p == ' ' || *p == '\t') p++;
901 if (*p && *p != ';') goto invalid;
903 set_state(parser, HEX_MULTILINE);
904 return pos;
906 invalid:
907 free_parser_data(parser);
908 set_state(parser, LINE_START);
909 return p;
912 /* handler for parser HEX_MULTILINE state */
913 static WCHAR *hex_multiline_state(struct parser *parser, WCHAR *pos)
915 WCHAR *line;
917 if (!(line = get_line(parser->file)))
919 prepare_hex_string_data(parser);
920 set_state(parser, SET_VALUE);
921 return pos;
924 while (*line == ' ' || *line == '\t') line++;
925 if (!*line || *line == ';') return line;
927 if (!iswxdigit(*line)) goto invalid;
929 set_state(parser, HEX_DATA);
930 return line;
932 invalid:
933 free_parser_data(parser);
934 set_state(parser, LINE_START);
935 return line;
938 /* handler for parser UNKNOWN_DATA state */
939 static WCHAR *unknown_data_state(struct parser *parser, WCHAR *pos)
941 output_message(STRING_UNKNOWN_DATA_FORMAT, parser->data_type);
943 set_state(parser, LINE_START);
944 return pos;
947 /* handler for parser SET_VALUE state */
948 static WCHAR *set_value_state(struct parser *parser, WCHAR *pos)
950 RegSetValueExW(parser->hkey, parser->value_name, 0, parser->data_type,
951 parser->data, parser->data_size);
953 free_parser_data(parser);
955 if (parser->reg_version == REG_VERSION_31)
956 set_state(parser, PARSE_WIN31_LINE);
957 else
958 set_state(parser, LINE_START);
960 return pos;
963 static WCHAR *get_lineA(FILE *fp)
965 static WCHAR *lineW;
966 static size_t size;
967 static char *buf, *next;
968 char *line;
970 heap_free(lineW);
972 if (!fp) goto cleanup;
974 if (!size)
976 size = REG_VAL_BUF_SIZE;
977 buf = heap_xalloc(size);
978 *buf = 0;
979 next = buf;
981 line = next;
983 while (next)
985 char *p = strpbrk(line, "\r\n");
986 if (!p)
988 size_t len, count;
989 len = strlen(next);
990 memmove(buf, next, len + 1);
991 if (size - len < 3)
993 size *= 2;
994 buf = heap_xrealloc(buf, size);
996 if (!(count = fread(buf + len, 1, size - len - 1, fp)))
998 next = NULL;
999 lineW = GetWideString(buf);
1000 return lineW;
1002 buf[len + count] = 0;
1003 next = buf;
1004 line = buf;
1005 continue;
1007 next = p + 1;
1008 if (*p == '\r' && *(p + 1) == '\n') next++;
1009 *p = 0;
1010 lineW = GetWideString(line);
1011 return lineW;
1014 cleanup:
1015 lineW = NULL;
1016 if (size) heap_free(buf);
1017 size = 0;
1018 return NULL;
1021 static WCHAR *get_lineW(FILE *fp)
1023 static size_t size;
1024 static WCHAR *buf, *next;
1025 WCHAR *line;
1027 if (!fp) goto cleanup;
1029 if (!size)
1031 size = REG_VAL_BUF_SIZE;
1032 buf = heap_xalloc(size * sizeof(WCHAR));
1033 *buf = 0;
1034 next = buf;
1036 line = next;
1038 while (next)
1040 static const WCHAR line_endings[] = {'\r','\n',0};
1041 WCHAR *p = wcspbrk(line, line_endings);
1042 if (!p)
1044 size_t len, count;
1045 len = lstrlenW(next);
1046 memmove(buf, next, (len + 1) * sizeof(WCHAR));
1047 if (size - len < 3)
1049 size *= 2;
1050 buf = heap_xrealloc(buf, size * sizeof(WCHAR));
1052 if (!(count = fread(buf + len, sizeof(WCHAR), size - len - 1, fp)))
1054 next = NULL;
1055 return buf;
1057 buf[len + count] = 0;
1058 next = buf;
1059 line = buf;
1060 continue;
1062 next = p + 1;
1063 if (*p == '\r' && *(p + 1) == '\n') next++;
1064 *p = 0;
1065 return line;
1068 cleanup:
1069 if (size) heap_free(buf);
1070 size = 0;
1071 return NULL;
1074 /******************************************************************************
1075 * Reads contents of the specified file into the registry.
1077 BOOL import_registry_file(FILE *reg_file)
1079 BYTE s[2];
1080 struct parser parser;
1081 WCHAR *pos;
1083 if (!reg_file || (fread(s, 2, 1, reg_file) != 1))
1084 return FALSE;
1086 parser.is_unicode = (s[0] == 0xff && s[1] == 0xfe);
1087 get_line = parser.is_unicode ? get_lineW : get_lineA;
1089 parser.file = reg_file;
1090 parser.two_wchars[0] = s[0];
1091 parser.two_wchars[1] = s[1];
1092 parser.reg_version = -1;
1093 parser.hkey = NULL;
1094 parser.key_name = NULL;
1095 parser.value_name = NULL;
1096 parser.parse_type = 0;
1097 parser.data_type = 0;
1098 parser.data = NULL;
1099 parser.data_size = 0;
1100 parser.backslash = FALSE;
1101 parser.state = HEADER;
1103 pos = parser.two_wchars;
1105 /* parser main loop */
1106 while (pos)
1107 pos = (parser_funcs[parser.state])(&parser, pos);
1109 if (parser.reg_version == REG_VERSION_FUZZY || parser.reg_version == REG_VERSION_INVALID)
1110 return parser.reg_version == REG_VERSION_FUZZY;
1112 heap_free(parser.value_name);
1113 close_key(&parser);
1115 return TRUE;
1118 /******************************************************************************
1119 * Removes the registry key with all subkeys. Parses full key name.
1121 * Parameters:
1122 * reg_key_name - full name of registry branch to delete. Ignored if is NULL,
1123 * empty, points to register key class, does not exist.
1125 void delete_registry_key(WCHAR *reg_key_name)
1127 WCHAR *key_name = NULL;
1128 HKEY key_class;
1130 if (!reg_key_name || !reg_key_name[0])
1131 return;
1133 if (!(key_class = parse_key_name(reg_key_name, &key_name)))
1135 if (key_name) *(key_name - 1) = 0;
1136 error_exit(STRING_INVALID_SYSTEM_KEY, reg_key_name);
1139 if (!key_name || !*key_name)
1140 error_exit(STRING_DELETE_FAILED, reg_key_name);
1142 RegDeleteTreeW(key_class, key_name);
1145 static void REGPROC_write_line(FILE *fp, const WCHAR *str, BOOL unicode)
1147 if (unicode)
1148 fwrite(str, sizeof(WCHAR), lstrlenW(str), fp);
1149 else
1151 char *strA = GetMultiByteString(str);
1152 fputs(strA, fp);
1153 heap_free(strA);
1157 static WCHAR *REGPROC_escape_string(WCHAR *str, size_t str_len, size_t *line_len)
1159 size_t i, escape_count, pos;
1160 WCHAR *buf;
1162 for (i = 0, escape_count = 0; i < str_len; i++)
1164 WCHAR c = str[i];
1166 if (!c) break;
1168 if (c == '\r' || c == '\n' || c == '\\' || c == '"')
1169 escape_count++;
1172 buf = heap_xalloc((str_len + escape_count + 1) * sizeof(WCHAR));
1174 for (i = 0, pos = 0; i < str_len; i++, pos++)
1176 WCHAR c = str[i];
1178 if (!c) break;
1180 switch (c)
1182 case '\r':
1183 buf[pos++] = '\\';
1184 buf[pos] = 'r';
1185 break;
1186 case '\n':
1187 buf[pos++] = '\\';
1188 buf[pos] = 'n';
1189 break;
1190 case '\\':
1191 buf[pos++] = '\\';
1192 buf[pos] = '\\';
1193 break;
1194 case '"':
1195 buf[pos++] = '\\';
1196 buf[pos] = '"';
1197 break;
1198 default:
1199 buf[pos] = c;
1203 buf[pos] = 0;
1204 *line_len = pos;
1205 return buf;
1208 static size_t export_value_name(FILE *fp, WCHAR *name, size_t len, BOOL unicode)
1210 static const WCHAR quoted_fmt[] = {'"','%','s','"','=',0};
1211 static const WCHAR default_name[] = {'@','=',0};
1212 size_t line_len;
1214 if (name && *name)
1216 WCHAR *str = REGPROC_escape_string(name, len, &line_len);
1217 WCHAR *buf = heap_xalloc((line_len + 4) * sizeof(WCHAR));
1218 line_len = swprintf(buf, line_len + 4, quoted_fmt, str);
1219 REGPROC_write_line(fp, buf, unicode);
1220 heap_free(buf);
1221 heap_free(str);
1223 else
1225 line_len = lstrlenW(default_name);
1226 REGPROC_write_line(fp, default_name, unicode);
1229 return line_len;
1232 static void export_string_data(WCHAR **buf, WCHAR *data, size_t size)
1234 size_t len = 0, line_len;
1235 WCHAR *str;
1236 static const WCHAR fmt[] = {'"','%','s','"',0};
1238 if (size)
1239 len = size / sizeof(WCHAR) - 1;
1240 str = REGPROC_escape_string(data, len, &line_len);
1241 *buf = heap_xalloc((line_len + 3) * sizeof(WCHAR));
1242 swprintf(*buf, line_len + 3, fmt, str);
1243 heap_free(str);
1246 static void export_dword_data(WCHAR **buf, DWORD *data)
1248 static const WCHAR fmt[] = {'d','w','o','r','d',':','%','0','8','x',0};
1250 *buf = heap_xalloc(15 * sizeof(WCHAR));
1251 swprintf(*buf, 15, fmt, *data);
1254 static size_t export_hex_data_type(FILE *fp, DWORD type, BOOL unicode)
1256 static const WCHAR hex[] = {'h','e','x',':',0};
1257 static const WCHAR hexp_fmt[] = {'h','e','x','(','%','x',')',':',0};
1258 size_t line_len;
1260 if (type == REG_BINARY)
1262 line_len = lstrlenW(hex);
1263 REGPROC_write_line(fp, hex, unicode);
1265 else
1267 WCHAR *buf = heap_xalloc(15 * sizeof(WCHAR));
1268 line_len = swprintf(buf, 15, hexp_fmt, type);
1269 REGPROC_write_line(fp, buf, unicode);
1270 heap_free(buf);
1273 return line_len;
1276 #define MAX_HEX_CHARS 77
1278 static void export_hex_data(FILE *fp, WCHAR **buf, DWORD type, DWORD line_len,
1279 void *data, DWORD size, BOOL unicode)
1281 static const WCHAR fmt[] = {'%','0','2','x',0};
1282 static const WCHAR hex_concat[] = {'\\','\r','\n',' ',' ',0};
1283 size_t num_commas, i, pos;
1285 line_len += export_hex_data_type(fp, type, unicode);
1287 if (!size) return;
1289 if (!unicode && (type == REG_EXPAND_SZ || type == REG_MULTI_SZ))
1290 data = GetMultiByteStringN(data, size / sizeof(WCHAR), &size);
1292 num_commas = size - 1;
1293 *buf = heap_xalloc(size * 3 * sizeof(WCHAR));
1295 for (i = 0, pos = 0; i < size; i++)
1297 pos += swprintf(*buf + pos, 3, fmt, ((BYTE *)data)[i]);
1298 if (i == num_commas) break;
1299 (*buf)[pos++] = ',';
1300 (*buf)[pos] = 0;
1301 line_len += 3;
1303 if (line_len >= MAX_HEX_CHARS)
1305 REGPROC_write_line(fp, *buf, unicode);
1306 REGPROC_write_line(fp, hex_concat, unicode);
1307 line_len = 2;
1308 pos = 0;
1313 static void export_newline(FILE *fp, BOOL unicode)
1315 static const WCHAR newline[] = {'\r','\n',0};
1317 REGPROC_write_line(fp, newline, unicode);
1320 static void export_data(FILE *fp, WCHAR *value_name, DWORD value_len, DWORD type,
1321 void *data, size_t size, BOOL unicode)
1323 WCHAR *buf = NULL;
1324 size_t line_len = export_value_name(fp, value_name, value_len, unicode);
1326 switch (type)
1328 case REG_SZ:
1329 export_string_data(&buf, data, size);
1330 break;
1331 case REG_DWORD:
1332 if (size)
1334 export_dword_data(&buf, data);
1335 break;
1337 /* fall through */
1338 case REG_NONE:
1339 case REG_EXPAND_SZ:
1340 case REG_BINARY:
1341 case REG_MULTI_SZ:
1342 default:
1343 export_hex_data(fp, &buf, type, line_len, data, size, unicode);
1344 break;
1347 if (size || type == REG_SZ)
1349 REGPROC_write_line(fp, buf, unicode);
1350 heap_free(buf);
1353 export_newline(fp, unicode);
1356 static WCHAR *build_subkey_path(WCHAR *path, DWORD path_len, WCHAR *subkey_name, DWORD subkey_len)
1358 WCHAR *subkey_path;
1359 static const WCHAR fmt[] = {'%','s','\\','%','s',0};
1361 subkey_path = heap_xalloc((path_len + subkey_len + 2) * sizeof(WCHAR));
1362 swprintf(subkey_path, path_len + subkey_len + 2, fmt, path, subkey_name);
1364 return subkey_path;
1367 static void export_key_name(FILE *fp, WCHAR *name, BOOL unicode)
1369 static const WCHAR fmt[] = {'\r','\n','[','%','s',']','\r','\n',0};
1370 WCHAR *buf;
1372 buf = heap_xalloc((lstrlenW(name) + 7) * sizeof(WCHAR));
1373 swprintf(buf, lstrlenW(name) + 7, fmt, name);
1374 REGPROC_write_line(fp, buf, unicode);
1375 heap_free(buf);
1378 #define MAX_SUBKEY_LEN 257
1380 static int export_registry_data(FILE *fp, HKEY key, WCHAR *path, BOOL unicode)
1382 LONG rc;
1383 DWORD max_value_len = 256, value_len;
1384 DWORD max_data_bytes = 2048, data_size;
1385 DWORD subkey_len;
1386 DWORD i, type, path_len;
1387 WCHAR *value_name, *subkey_name, *subkey_path;
1388 BYTE *data;
1389 HKEY subkey;
1391 export_key_name(fp, path, unicode);
1393 value_name = heap_xalloc(max_value_len * sizeof(WCHAR));
1394 data = heap_xalloc(max_data_bytes);
1396 i = 0;
1397 for (;;)
1399 value_len = max_value_len;
1400 data_size = max_data_bytes;
1401 rc = RegEnumValueW(key, i, value_name, &value_len, NULL, &type, data, &data_size);
1402 if (rc == ERROR_SUCCESS)
1404 export_data(fp, value_name, value_len, type, data, data_size, unicode);
1405 i++;
1407 else if (rc == ERROR_MORE_DATA)
1409 if (data_size > max_data_bytes)
1411 max_data_bytes = data_size;
1412 data = heap_xrealloc(data, max_data_bytes);
1414 else
1416 max_value_len *= 2;
1417 value_name = heap_xrealloc(value_name, max_value_len * sizeof(WCHAR));
1420 else break;
1423 heap_free(data);
1424 heap_free(value_name);
1426 subkey_name = heap_xalloc(MAX_SUBKEY_LEN * sizeof(WCHAR));
1428 path_len = lstrlenW(path);
1430 i = 0;
1431 for (;;)
1433 subkey_len = MAX_SUBKEY_LEN;
1434 rc = RegEnumKeyExW(key, i, subkey_name, &subkey_len, NULL, NULL, NULL, NULL);
1435 if (rc == ERROR_SUCCESS)
1437 subkey_path = build_subkey_path(path, path_len, subkey_name, subkey_len);
1438 if (!RegOpenKeyExW(key, subkey_name, 0, KEY_READ, &subkey))
1440 export_registry_data(fp, subkey, subkey_path, unicode);
1441 RegCloseKey(subkey);
1443 heap_free(subkey_path);
1444 i++;
1446 else break;
1449 heap_free(subkey_name);
1450 return 0;
1453 static FILE *REGPROC_open_export_file(WCHAR *file_name, BOOL unicode)
1455 FILE *file;
1456 static const WCHAR hyphen[] = {'-',0};
1458 if (!lstrcmpW(file_name, hyphen))
1460 file = stdout;
1461 _setmode(_fileno(file), _O_BINARY);
1463 else
1465 static const WCHAR wb_mode[] = {'w','b',0};
1467 file = _wfopen(file_name, wb_mode);
1468 if (!file)
1470 static const WCHAR regedit[] = {'r','e','g','e','d','i','t',0};
1471 _wperror(regedit);
1472 error_exit(STRING_CANNOT_OPEN_FILE, file_name);
1476 if (unicode)
1478 static const BYTE bom[] = {0xff,0xfe};
1479 static const WCHAR header[] = {'W','i','n','d','o','w','s',' ',
1480 'R','e','g','i','s','t','r','y',' ','E','d','i','t','o','r',' ',
1481 'V','e','r','s','i','o','n',' ','5','.','0','0','\r','\n'};
1483 fwrite(bom, sizeof(BYTE), ARRAY_SIZE(bom), file);
1484 fwrite(header, sizeof(WCHAR), ARRAY_SIZE(header), file);
1486 else
1487 fputs("REGEDIT4\r\n", file);
1489 return file;
1492 static HKEY open_export_key(HKEY key_class, WCHAR *subkey, WCHAR *path)
1494 HKEY key;
1496 if (!RegOpenKeyExW(key_class, subkey, 0, KEY_READ, &key))
1497 return key;
1499 output_message(STRING_OPEN_KEY_FAILED, path);
1500 return NULL;
1503 static BOOL export_key(WCHAR *file_name, WCHAR *path, BOOL unicode)
1505 HKEY key_class, key;
1506 WCHAR *subkey;
1507 FILE *fp;
1508 BOOL ret;
1510 if (!(key_class = parse_key_name(path, &subkey)))
1512 if (subkey) *(subkey - 1) = 0;
1513 output_message(STRING_INVALID_SYSTEM_KEY, path);
1514 return FALSE;
1517 if (!(key = open_export_key(key_class, subkey, path)))
1518 return FALSE;
1520 fp = REGPROC_open_export_file(file_name, unicode);
1521 ret = export_registry_data(fp, key, path, unicode);
1522 export_newline(fp, unicode);
1523 fclose(fp);
1525 RegCloseKey(key);
1526 return ret;
1529 static BOOL export_all(WCHAR *file_name, WCHAR *path, BOOL unicode)
1531 FILE *fp;
1532 int i;
1533 HKEY classes[] = {HKEY_LOCAL_MACHINE, HKEY_USERS}, key;
1534 WCHAR *class_name;
1536 fp = REGPROC_open_export_file(file_name, unicode);
1538 for (i = 0; i < ARRAY_SIZE(classes); i++)
1540 if (!(key = open_export_key(classes[i], NULL, path)))
1542 fclose(fp);
1543 return FALSE;
1546 class_name = heap_xalloc((lstrlenW(reg_class_namesW[i]) + 1) * sizeof(WCHAR));
1547 lstrcpyW(class_name, reg_class_namesW[i]);
1549 export_registry_data(fp, classes[i], class_name, unicode);
1551 heap_free(class_name);
1552 RegCloseKey(key);
1555 export_newline(fp, unicode);
1556 fclose(fp);
1558 return TRUE;
1561 BOOL export_registry_key(WCHAR *file_name, WCHAR *path, DWORD format)
1563 BOOL unicode = (format == REG_FORMAT_5);
1565 if (path && *path)
1566 return export_key(file_name, path, unicode);
1567 else
1568 return export_all(file_name, path, unicode);