gdiplus: Send paths to gdi32 in device coordinates.
[wine.git] / programs / regedit / regproc.c
blob6e6ea473c7577b152e4ecea0b5f969431bc7dccf
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 <stdio.h>
26 #include <stdlib.h>
27 #include <fcntl.h>
28 #include <io.h>
29 #include <windows.h>
30 #include <wine/unicode.h>
31 #include <wine/debug.h>
32 #include "regproc.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(regedit);
36 #define REG_VAL_BUF_SIZE 4096
38 extern const WCHAR* reg_class_namesW[];
40 static HKEY reg_class_keys[] = {
41 HKEY_LOCAL_MACHINE, HKEY_USERS, HKEY_CLASSES_ROOT,
42 HKEY_CURRENT_CONFIG, HKEY_CURRENT_USER, HKEY_DYN_DATA
45 #define ARRAY_SIZE(A) (sizeof(A)/sizeof(*A))
47 void *heap_xalloc(size_t size)
49 void *buf = HeapAlloc(GetProcessHeap(), 0, size);
50 if (!buf)
52 ERR("Out of memory!\n");
53 exit(1);
55 return buf;
58 void *heap_xrealloc(void *buf, size_t size)
60 void *new_buf;
62 if (buf)
63 new_buf = HeapReAlloc(GetProcessHeap(), 0, buf, size);
64 else
65 new_buf = HeapAlloc(GetProcessHeap(), 0, size);
67 if (!new_buf)
69 ERR("Out of memory!\n");
70 exit(1);
73 return new_buf;
76 BOOL heap_free(void *buf)
78 return HeapFree(GetProcessHeap(), 0, buf);
81 /******************************************************************************
82 * Allocates memory and converts input from multibyte to wide chars
83 * Returned string must be freed by the caller
85 static WCHAR* GetWideString(const char* strA)
87 if(strA)
89 WCHAR* strW;
90 int len = MultiByteToWideChar(CP_ACP, 0, strA, -1, NULL, 0);
92 strW = heap_xalloc(len * sizeof(WCHAR));
93 MultiByteToWideChar(CP_ACP, 0, strA, -1, strW, len);
94 return strW;
96 return NULL;
99 /******************************************************************************
100 * Allocates memory and converts input from multibyte to wide chars
101 * Returned string must be freed by the caller
103 static WCHAR* GetWideStringN(const char* strA, int chars, DWORD *len)
105 if(strA)
107 WCHAR* strW;
108 *len = MultiByteToWideChar(CP_ACP, 0, strA, chars, NULL, 0);
110 strW = heap_xalloc(*len * sizeof(WCHAR));
111 MultiByteToWideChar(CP_ACP, 0, strA, chars, strW, *len);
112 return strW;
114 *len = 0;
115 return NULL;
118 /******************************************************************************
119 * Allocates memory and converts input from wide chars to multibyte
120 * Returned string must be freed by the caller
122 char* GetMultiByteString(const WCHAR* strW)
124 if(strW)
126 char* strA;
127 int len = WideCharToMultiByte(CP_ACP, 0, strW, -1, NULL, 0, NULL, NULL);
129 strA = heap_xalloc(len);
130 WideCharToMultiByte(CP_ACP, 0, strW, -1, strA, len, NULL, NULL);
131 return strA;
133 return NULL;
136 /******************************************************************************
137 * Allocates memory and converts input from wide chars to multibyte
138 * Returned string must be freed by the caller
140 static char* GetMultiByteStringN(const WCHAR* strW, int chars, DWORD* len)
142 if(strW)
144 char* strA;
145 *len = WideCharToMultiByte(CP_ACP, 0, strW, chars, NULL, 0, NULL, NULL);
147 strA = heap_xalloc(*len);
148 WideCharToMultiByte(CP_ACP, 0, strW, chars, strA, *len, NULL, NULL);
149 return strA;
151 *len = 0;
152 return NULL;
155 static WCHAR *(*get_line)(FILE *);
157 /* parser definitions */
158 enum parser_state
160 HEADER, /* parsing the registry file version header */
161 PARSE_WIN31_LINE, /* parsing a Windows 3.1 registry line */
162 LINE_START, /* at the beginning of a registry line */
163 KEY_NAME, /* parsing a key name */
164 DELETE_KEY, /* deleting a registry key */
165 DEFAULT_VALUE_NAME, /* parsing a default value name */
166 QUOTED_VALUE_NAME, /* parsing a double-quoted value name */
167 DATA_START, /* preparing for data parsing operations */
168 DELETE_VALUE, /* deleting a registry value */
169 DATA_TYPE, /* parsing the registry data type */
170 STRING_DATA, /* parsing REG_SZ data */
171 DWORD_DATA, /* parsing DWORD data */
172 HEX_DATA, /* parsing REG_BINARY, REG_NONE, REG_EXPAND_SZ or REG_MULTI_SZ data */
173 EOL_BACKSLASH, /* preparing to parse multiple lines of hex data */
174 HEX_MULTILINE, /* parsing multiple lines of hex data */
175 UNKNOWN_DATA, /* parsing an unhandled or invalid data type */
176 SET_VALUE, /* adding a value to the registry */
177 NB_PARSER_STATES
180 struct parser
182 FILE *file; /* pointer to a registry file */
183 WCHAR two_wchars[2]; /* first two characters from the encoding check */
184 BOOL is_unicode; /* parsing Unicode or ASCII data */
185 short int reg_version; /* registry file version */
186 HKEY hkey; /* current registry key */
187 WCHAR *key_name; /* current key name */
188 WCHAR *value_name; /* value name */
189 DWORD parse_type; /* generic data type for parsing */
190 DWORD data_type; /* data type */
191 void *data; /* value data */
192 DWORD data_size; /* size of the data (in bytes) */
193 BOOL backslash; /* TRUE if the current line contains a backslash */
194 enum parser_state state; /* current parser state */
197 typedef WCHAR *(*parser_state_func)(struct parser *parser, WCHAR *pos);
199 /* parser state machine functions */
200 static WCHAR *header_state(struct parser *parser, WCHAR *pos);
201 static WCHAR *parse_win31_line_state(struct parser *parser, WCHAR *pos);
202 static WCHAR *line_start_state(struct parser *parser, WCHAR *pos);
203 static WCHAR *key_name_state(struct parser *parser, WCHAR *pos);
204 static WCHAR *delete_key_state(struct parser *parser, WCHAR *pos);
205 static WCHAR *default_value_name_state(struct parser *parser, WCHAR *pos);
206 static WCHAR *quoted_value_name_state(struct parser *parser, WCHAR *pos);
207 static WCHAR *data_start_state(struct parser *parser, WCHAR *pos);
208 static WCHAR *delete_value_state(struct parser *parser, WCHAR *pos);
209 static WCHAR *data_type_state(struct parser *parser, WCHAR *pos);
210 static WCHAR *string_data_state(struct parser *parser, WCHAR *pos);
211 static WCHAR *dword_data_state(struct parser *parser, WCHAR *pos);
212 static WCHAR *hex_data_state(struct parser *parser, WCHAR *pos);
213 static WCHAR *eol_backslash_state(struct parser *parser, WCHAR *pos);
214 static WCHAR *hex_multiline_state(struct parser *parser, WCHAR *pos);
215 static WCHAR *unknown_data_state(struct parser *parser, WCHAR *pos);
216 static WCHAR *set_value_state(struct parser *parser, WCHAR *pos);
218 static const parser_state_func parser_funcs[NB_PARSER_STATES] =
220 header_state, /* HEADER */
221 parse_win31_line_state, /* PARSE_WIN31_LINE */
222 line_start_state, /* LINE_START */
223 key_name_state, /* KEY_NAME */
224 delete_key_state, /* DELETE_KEY */
225 default_value_name_state, /* DEFAULT_VALUE_NAME */
226 quoted_value_name_state, /* QUOTED_VALUE_NAME */
227 data_start_state, /* DATA_START */
228 delete_value_state, /* DELETE_VALUE */
229 data_type_state, /* DATA_TYPE */
230 string_data_state, /* STRING_DATA */
231 dword_data_state, /* DWORD_DATA */
232 hex_data_state, /* HEX_DATA */
233 eol_backslash_state, /* EOL_BACKSLASH */
234 hex_multiline_state, /* HEX_MULTILINE */
235 unknown_data_state, /* UNKNOWN_DATA */
236 set_value_state, /* SET_VALUE */
239 /* set the new parser state and return the previous one */
240 static inline enum parser_state set_state(struct parser *parser, enum parser_state state)
242 enum parser_state ret = parser->state;
243 parser->state = state;
244 return ret;
247 /******************************************************************************
248 * Converts a hex representation of a DWORD into a DWORD.
250 static BOOL convert_hex_to_dword(WCHAR *str, DWORD *dw)
252 WCHAR *p, *end;
253 int count = 0;
255 while (*str == ' ' || *str == '\t') str++;
256 if (!*str) goto error;
258 p = str;
259 while (isxdigitW(*p))
261 count++;
262 p++;
264 if (count > 8) goto error;
266 end = p;
267 while (*p == ' ' || *p == '\t') p++;
268 if (*p && *p != ';') goto error;
270 *end = 0;
271 *dw = strtoulW(str, &end, 16);
272 return TRUE;
274 error:
275 return FALSE;
278 /******************************************************************************
279 * Converts comma-separated hex data into a binary string and modifies
280 * the input parameter to skip the concatenating backslash, if found.
282 * Returns TRUE or FALSE to indicate whether parsing was successful.
284 static BOOL convert_hex_csv_to_hex(struct parser *parser, WCHAR **str)
286 size_t size;
287 BYTE *d;
288 WCHAR *s;
290 parser->backslash = FALSE;
292 /* The worst case is 1 digit + 1 comma per byte */
293 size = ((lstrlenW(*str) + 1) / 2) + parser->data_size;
294 parser->data = heap_xrealloc(parser->data, size);
296 s = *str;
297 d = (BYTE *)parser->data + parser->data_size;
299 while (*s)
301 WCHAR *end;
302 unsigned long wc;
304 wc = strtoulW(s, &end, 16);
305 if (wc > 0xff) return FALSE;
307 if (s == end && wc == 0)
309 while (*end == ' ' || *end == '\t') end++;
310 if (*end == '\\')
312 parser->backslash = TRUE;
313 *str = end + 1;
314 return TRUE;
316 else if (*end == ';')
317 return TRUE;
318 return FALSE;
321 *d++ = wc;
322 parser->data_size++;
324 if (*end && *end != ',')
326 while (*end == ' ' || *end == '\t') end++;
327 if (*end && *end != ';') return FALSE;
328 return TRUE;
331 if (*end) end++;
332 s = end;
335 return TRUE;
338 /******************************************************************************
339 * Parses the data type of the registry value being imported and modifies
340 * the input parameter to skip the string representation of the data type.
342 * Returns TRUE or FALSE to indicate whether a data type was found.
344 static BOOL parse_data_type(struct parser *parser, WCHAR **line)
346 struct data_type { const WCHAR *tag; int len; int type; int parse_type; };
348 static const WCHAR quote[] = {'"'};
349 static const WCHAR hex[] = {'h','e','x',':'};
350 static const WCHAR dword[] = {'d','w','o','r','d',':'};
351 static const WCHAR hexp[] = {'h','e','x','('};
353 static const struct data_type data_types[] = {
354 /* tag len type parse type */
355 { quote, 1, REG_SZ, REG_SZ },
356 { hex, 4, REG_BINARY, REG_BINARY },
357 { dword, 6, REG_DWORD, REG_DWORD },
358 { hexp, 4, -1, REG_BINARY }, /* REG_NONE, REG_EXPAND_SZ, REG_MULTI_SZ */
359 { NULL, 0, 0, 0 }
362 const struct data_type *ptr;
364 for (ptr = data_types; ptr->tag; ptr++)
366 if (strncmpW(ptr->tag, *line, ptr->len))
367 continue;
369 parser->parse_type = ptr->parse_type;
370 parser->data_type = ptr->parse_type;
371 *line += ptr->len;
373 if (ptr->type == -1)
375 WCHAR *end;
376 DWORD val;
378 if (!**line || tolowerW((*line)[1]) == 'x')
379 return FALSE;
381 /* "hex(xx):" is special */
382 val = wcstoul(*line, &end, 16);
383 if (*end != ')' || *(end + 1) != ':' || (val == ~0u && errno == ERANGE))
384 return FALSE;
386 parser->data_type = val;
387 *line = end + 2;
389 return TRUE;
391 return FALSE;
394 /******************************************************************************
395 * Replaces escape sequences with their character equivalents and
396 * null-terminates the string on the first non-escaped double quote.
398 * Assigns a pointer to the remaining unparsed data in the line.
399 * Returns TRUE or FALSE to indicate whether a closing double quote was found.
401 static BOOL REGPROC_unescape_string(WCHAR *str, WCHAR **unparsed)
403 int str_idx = 0; /* current character under analysis */
404 int val_idx = 0; /* the last character of the unescaped string */
405 int len = lstrlenW(str);
406 BOOL ret;
408 for (str_idx = 0; str_idx < len; str_idx++, val_idx++) {
409 if (str[str_idx] == '\\') {
410 str_idx++;
411 switch (str[str_idx]) {
412 case 'n':
413 str[val_idx] = '\n';
414 break;
415 case 'r':
416 str[val_idx] = '\r';
417 break;
418 case '0':
419 str[val_idx] = '\0';
420 break;
421 case '\\':
422 case '"':
423 str[val_idx] = str[str_idx];
424 break;
425 default:
426 if (!str[str_idx]) return FALSE;
427 output_message(STRING_ESCAPE_SEQUENCE, str[str_idx]);
428 str[val_idx] = str[str_idx];
429 break;
431 } else if (str[str_idx] == '"') {
432 break;
433 } else {
434 str[val_idx] = str[str_idx];
438 ret = (str[str_idx] == '"');
439 *unparsed = str + str_idx + 1;
440 str[val_idx] = '\0';
441 return ret;
444 static HKEY parse_key_name(WCHAR *key_name, WCHAR **key_path)
446 unsigned int i;
448 if (!key_name) return 0;
450 *key_path = strchrW(key_name, '\\');
451 if (*key_path) (*key_path)++;
453 for (i = 0; i < ARRAY_SIZE(reg_class_keys); i++)
455 int len = lstrlenW(reg_class_namesW[i]);
456 if (!strncmpiW(key_name, reg_class_namesW[i], len) &&
457 (key_name[len] == 0 || key_name[len] == '\\'))
459 return reg_class_keys[i];
463 return 0;
466 static void close_key(struct parser *parser)
468 if (parser->hkey)
470 heap_free(parser->key_name);
471 parser->key_name = NULL;
473 RegCloseKey(parser->hkey);
474 parser->hkey = NULL;
478 /******************************************************************************
479 * Opens the registry key given by the input path.
480 * This key must be closed by calling close_key().
482 static LONG open_key(struct parser *parser, WCHAR *path)
484 HKEY key_class;
485 WCHAR *key_path;
486 LONG res;
488 close_key(parser);
490 /* Get the registry class */
491 if (!path || !(key_class = parse_key_name(path, &key_path)))
492 return ERROR_INVALID_PARAMETER;
494 res = RegCreateKeyExW(key_class, key_path, 0, NULL, REG_OPTION_NON_VOLATILE,
495 KEY_ALL_ACCESS, NULL, &parser->hkey, NULL);
497 if (res == ERROR_SUCCESS)
499 parser->key_name = heap_xalloc((lstrlenW(path) + 1) * sizeof(WCHAR));
500 lstrcpyW(parser->key_name, path);
502 else
503 parser->hkey = NULL;
505 return res;
508 static void free_parser_data(struct parser *parser)
510 if (parser->parse_type == REG_DWORD || parser->parse_type == REG_BINARY)
511 heap_free(parser->data);
513 parser->data = NULL;
514 parser->data_size = 0;
517 static void prepare_hex_string_data(struct parser *parser)
519 if (parser->data_type == REG_EXPAND_SZ || parser->data_type == REG_MULTI_SZ)
521 if (parser->is_unicode)
523 WCHAR *data = parser->data;
524 DWORD len = parser->data_size / sizeof(WCHAR);
526 if (data[len - 1] != 0)
528 data[len] = 0;
529 parser->data_size += sizeof(WCHAR);
532 else
534 BYTE *data = parser->data;
536 if (data[parser->data_size - 1] != 0)
538 data[parser->data_size] = 0;
539 parser->data_size++;
542 parser->data = GetWideStringN(parser->data, parser->data_size, &parser->data_size);
543 parser->data_size *= sizeof(WCHAR);
544 heap_free(data);
549 enum reg_versions {
550 REG_VERSION_31,
551 REG_VERSION_40,
552 REG_VERSION_50,
553 REG_VERSION_FUZZY,
554 REG_VERSION_INVALID
557 static enum reg_versions parse_file_header(const WCHAR *s)
559 static const WCHAR header_31[] = {'R','E','G','E','D','I','T',0};
560 static const WCHAR header_40[] = {'R','E','G','E','D','I','T','4',0};
561 static const WCHAR header_50[] = {'W','i','n','d','o','w','s',' ',
562 'R','e','g','i','s','t','r','y',' ','E','d','i','t','o','r',' ',
563 'V','e','r','s','i','o','n',' ','5','.','0','0',0};
565 while (*s == ' ' || *s == '\t') s++;
567 if (!strcmpW(s, header_31))
568 return REG_VERSION_31;
570 if (!strcmpW(s, header_40))
571 return REG_VERSION_40;
573 if (!strcmpW(s, header_50))
574 return REG_VERSION_50;
576 /* The Windows version accepts registry file headers beginning with "REGEDIT" and ending
577 * with other characters, as long as "REGEDIT" appears at the start of the line. For example,
578 * "REGEDIT 4", "REGEDIT9" and "REGEDIT4FOO" are all treated as valid file headers.
579 * In all such cases, however, the contents of the registry file are not imported.
581 if (!strncmpW(s, header_31, 7)) /* "REGEDIT" without NUL */
582 return REG_VERSION_FUZZY;
584 return REG_VERSION_INVALID;
587 /* handler for parser HEADER state */
588 static WCHAR *header_state(struct parser *parser, WCHAR *pos)
590 WCHAR *line, *header;
592 if (!(line = get_line(parser->file)))
593 return NULL;
595 if (!parser->is_unicode)
597 header = heap_xalloc((lstrlenW(line) + 3) * sizeof(WCHAR));
598 header[0] = parser->two_wchars[0];
599 header[1] = parser->two_wchars[1];
600 lstrcpyW(header + 2, line);
601 parser->reg_version = parse_file_header(header);
602 heap_free(header);
604 else parser->reg_version = parse_file_header(line);
606 switch (parser->reg_version)
608 case REG_VERSION_31:
609 set_state(parser, PARSE_WIN31_LINE);
610 break;
611 case REG_VERSION_40:
612 case REG_VERSION_50:
613 set_state(parser, LINE_START);
614 break;
615 default:
616 get_line(NULL); /* Reset static variables */
617 return NULL;
620 return line;
623 /* handler for parser PARSE_WIN31_LINE state */
624 static WCHAR *parse_win31_line_state(struct parser *parser, WCHAR *pos)
626 WCHAR *line, *value;
627 static WCHAR hkcr[] = {'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T'};
628 unsigned int key_end = 0;
630 if (!(line = get_line(parser->file)))
631 return NULL;
633 if (strncmpW(line, hkcr, ARRAY_SIZE(hkcr)))
634 return line;
636 /* get key name */
637 while (line[key_end] && !isspaceW(line[key_end])) key_end++;
639 value = line + key_end;
640 while (*value == ' ' || *value == '\t') value++;
642 if (*value == '=') value++;
643 if (*value == ' ') value++; /* at most one space is skipped */
645 line[key_end] = 0;
647 if (open_key(parser, line) != ERROR_SUCCESS)
649 output_message(STRING_OPEN_KEY_FAILED, line);
650 return line;
653 parser->value_name = NULL;
654 parser->data_type = REG_SZ;
655 parser->data = value;
656 parser->data_size = (lstrlenW(value) + 1) * sizeof(WCHAR);
658 set_state(parser, SET_VALUE);
659 return value;
662 /* handler for parser LINE_START state */
663 static WCHAR *line_start_state(struct parser *parser, WCHAR *pos)
665 WCHAR *line, *p;
667 if (!(line = get_line(parser->file)))
668 return NULL;
670 for (p = line; *p; p++)
672 switch (*p)
674 case '[':
675 set_state(parser, KEY_NAME);
676 return p + 1;
677 case '@':
678 set_state(parser, DEFAULT_VALUE_NAME);
679 return p;
680 case '"':
681 set_state(parser, QUOTED_VALUE_NAME);
682 return p + 1;
683 case ' ':
684 case '\t':
685 break;
686 default:
687 return p;
691 return p;
694 /* handler for parser KEY_NAME state */
695 static WCHAR *key_name_state(struct parser *parser, WCHAR *pos)
697 WCHAR *p = pos, *key_end;
699 if (*p == ' ' || *p == '\t' || !(key_end = strrchrW(p, ']')))
700 goto done;
702 *key_end = 0;
704 if (*p == '-')
706 set_state(parser, DELETE_KEY);
707 return p + 1;
709 else if (open_key(parser, p) != ERROR_SUCCESS)
710 output_message(STRING_OPEN_KEY_FAILED, p);
712 done:
713 set_state(parser, LINE_START);
714 return p;
717 /* handler for parser DELETE_KEY state */
718 static WCHAR *delete_key_state(struct parser *parser, WCHAR *pos)
720 WCHAR *p = pos;
722 close_key(parser);
724 if (*p == 'H' || *p == 'h')
725 delete_registry_key(p);
727 set_state(parser, LINE_START);
728 return p;
731 /* handler for parser DEFAULT_VALUE_NAME state */
732 static WCHAR *default_value_name_state(struct parser *parser, WCHAR *pos)
734 heap_free(parser->value_name);
735 parser->value_name = NULL;
737 set_state(parser, DATA_START);
738 return pos + 1;
741 /* handler for parser QUOTED_VALUE_NAME state */
742 static WCHAR *quoted_value_name_state(struct parser *parser, WCHAR *pos)
744 WCHAR *val_name = pos, *p;
746 if (parser->value_name)
748 heap_free(parser->value_name);
749 parser->value_name = NULL;
752 if (!REGPROC_unescape_string(val_name, &p))
753 goto invalid;
755 /* copy the value name in case we need to parse multiple lines and the buffer is overwritten */
756 parser->value_name = heap_xalloc((lstrlenW(val_name) + 1) * sizeof(WCHAR));
757 lstrcpyW(parser->value_name, val_name);
759 set_state(parser, DATA_START);
760 return p;
762 invalid:
763 set_state(parser, LINE_START);
764 return val_name;
767 /* handler for parser DATA_START state */
768 static WCHAR *data_start_state(struct parser *parser, WCHAR *pos)
770 WCHAR *p = pos;
771 unsigned int len;
773 while (*p == ' ' || *p == '\t') p++;
774 if (*p != '=') goto done;
775 p++;
776 while (*p == ' ' || *p == '\t') p++;
778 /* trim trailing whitespace */
779 len = strlenW(p);
780 while (len > 0 && (p[len - 1] == ' ' || p[len - 1] == '\t')) len--;
781 p[len] = 0;
783 if (*p == '-')
784 set_state(parser, DELETE_VALUE);
785 else
786 set_state(parser, DATA_TYPE);
787 return p;
789 done:
790 set_state(parser, LINE_START);
791 return p;
794 /* handler for parser DELETE_VALUE state */
795 static WCHAR *delete_value_state(struct parser *parser, WCHAR *pos)
797 WCHAR *p = pos + 1;
799 while (*p == ' ' || *p == '\t') p++;
800 if (*p && *p != ';') goto done;
802 RegDeleteValueW(parser->hkey, parser->value_name);
804 done:
805 set_state(parser, LINE_START);
806 return p;
809 /* handler for parser DATA_TYPE state */
810 static WCHAR *data_type_state(struct parser *parser, WCHAR *pos)
812 WCHAR *line = pos;
814 if (!parse_data_type(parser, &line))
816 set_state(parser, LINE_START);
817 return line;
820 switch (parser->parse_type)
822 case REG_SZ:
823 set_state(parser, STRING_DATA);
824 break;
825 case REG_DWORD:
826 set_state(parser, DWORD_DATA);
827 break;
828 case REG_BINARY: /* all hex data types, including undefined */
829 set_state(parser, HEX_DATA);
830 break;
831 default:
832 set_state(parser, UNKNOWN_DATA);
835 return line;
838 /* handler for parser STRING_DATA state */
839 static WCHAR *string_data_state(struct parser *parser, WCHAR *pos)
841 WCHAR *line;
843 parser->data = pos;
845 if (!REGPROC_unescape_string(parser->data, &line))
846 goto invalid;
848 while (*line == ' ' || *line == '\t') line++;
849 if (*line && *line != ';') goto invalid;
851 parser->data_size = (lstrlenW(parser->data) + 1) * sizeof(WCHAR);
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 DWORD_DATA state */
863 static WCHAR *dword_data_state(struct parser *parser, WCHAR *pos)
865 WCHAR *line = pos;
867 parser->data = heap_xalloc(sizeof(DWORD));
869 if (!convert_hex_to_dword(line, parser->data))
870 goto invalid;
872 parser->data_size = sizeof(DWORD);
874 set_state(parser, SET_VALUE);
875 return line;
877 invalid:
878 free_parser_data(parser);
879 set_state(parser, LINE_START);
880 return line;
883 /* handler for parser HEX_DATA state */
884 static WCHAR *hex_data_state(struct parser *parser, WCHAR *pos)
886 WCHAR *line = pos;
888 if (!convert_hex_csv_to_hex(parser, &line))
889 goto invalid;
891 if (parser->backslash)
893 set_state(parser, EOL_BACKSLASH);
894 return line;
897 prepare_hex_string_data(parser);
899 set_state(parser, SET_VALUE);
900 return line;
902 invalid:
903 free_parser_data(parser);
904 set_state(parser, LINE_START);
905 return line;
908 /* handler for parser EOL_BACKSLASH state */
909 static WCHAR *eol_backslash_state(struct parser *parser, WCHAR *pos)
911 WCHAR *p = pos;
913 while (*p == ' ' || *p == '\t') p++;
914 if (*p && *p != ';') goto invalid;
916 set_state(parser, HEX_MULTILINE);
917 return pos;
919 invalid:
920 free_parser_data(parser);
921 set_state(parser, LINE_START);
922 return p;
925 /* handler for parser HEX_MULTILINE state */
926 static WCHAR *hex_multiline_state(struct parser *parser, WCHAR *pos)
928 WCHAR *line;
930 if (!(line = get_line(parser->file)))
932 prepare_hex_string_data(parser);
933 set_state(parser, SET_VALUE);
934 return pos;
937 while (*line == ' ' || *line == '\t') line++;
938 if (!*line || *line == ';') return line;
940 if (!isxdigitW(*line)) goto invalid;
942 set_state(parser, HEX_DATA);
943 return line;
945 invalid:
946 free_parser_data(parser);
947 set_state(parser, LINE_START);
948 return line;
951 /* handler for parser UNKNOWN_DATA state */
952 static WCHAR *unknown_data_state(struct parser *parser, WCHAR *pos)
954 output_message(STRING_UNKNOWN_DATA_FORMAT, parser->data_type);
956 set_state(parser, LINE_START);
957 return pos;
960 /* handler for parser SET_VALUE state */
961 static WCHAR *set_value_state(struct parser *parser, WCHAR *pos)
963 RegSetValueExW(parser->hkey, parser->value_name, 0, parser->data_type,
964 parser->data, parser->data_size);
966 free_parser_data(parser);
968 if (parser->reg_version == REG_VERSION_31)
969 set_state(parser, PARSE_WIN31_LINE);
970 else
971 set_state(parser, LINE_START);
973 return pos;
976 static WCHAR *get_lineA(FILE *fp)
978 static WCHAR *lineW;
979 static size_t size;
980 static char *buf, *next;
981 char *line;
983 heap_free(lineW);
985 if (!fp) goto cleanup;
987 if (!size)
989 size = REG_VAL_BUF_SIZE;
990 buf = heap_xalloc(size);
991 *buf = 0;
992 next = buf;
994 line = next;
996 while (next)
998 char *p = strpbrk(line, "\r\n");
999 if (!p)
1001 size_t len, count;
1002 len = strlen(next);
1003 memmove(buf, next, len + 1);
1004 if (size - len < 3)
1006 size *= 2;
1007 buf = heap_xrealloc(buf, size);
1009 if (!(count = fread(buf + len, 1, size - len - 1, fp)))
1011 next = NULL;
1012 lineW = GetWideString(buf);
1013 return lineW;
1015 buf[len + count] = 0;
1016 next = buf;
1017 line = buf;
1018 continue;
1020 next = p + 1;
1021 if (*p == '\r' && *(p + 1) == '\n') next++;
1022 *p = 0;
1023 lineW = GetWideString(line);
1024 return lineW;
1027 cleanup:
1028 lineW = NULL;
1029 if (size) heap_free(buf);
1030 size = 0;
1031 return NULL;
1034 static WCHAR *get_lineW(FILE *fp)
1036 static size_t size;
1037 static WCHAR *buf, *next;
1038 WCHAR *line;
1040 if (!fp) goto cleanup;
1042 if (!size)
1044 size = REG_VAL_BUF_SIZE;
1045 buf = heap_xalloc(size * sizeof(WCHAR));
1046 *buf = 0;
1047 next = buf;
1049 line = next;
1051 while (next)
1053 static const WCHAR line_endings[] = {'\r','\n',0};
1054 WCHAR *p = strpbrkW(line, line_endings);
1055 if (!p)
1057 size_t len, count;
1058 len = strlenW(next);
1059 memmove(buf, next, (len + 1) * sizeof(WCHAR));
1060 if (size - len < 3)
1062 size *= 2;
1063 buf = heap_xrealloc(buf, size * sizeof(WCHAR));
1065 if (!(count = fread(buf + len, sizeof(WCHAR), size - len - 1, fp)))
1067 next = NULL;
1068 return buf;
1070 buf[len + count] = 0;
1071 next = buf;
1072 line = buf;
1073 continue;
1075 next = p + 1;
1076 if (*p == '\r' && *(p + 1) == '\n') next++;
1077 *p = 0;
1078 return line;
1081 cleanup:
1082 if (size) heap_free(buf);
1083 size = 0;
1084 return NULL;
1087 /******************************************************************************
1088 * Reads contents of the specified file into the registry.
1090 BOOL import_registry_file(FILE *reg_file)
1092 BYTE s[2];
1093 struct parser parser;
1094 WCHAR *pos;
1096 if (!reg_file || (fread(s, 2, 1, reg_file) != 1))
1097 return FALSE;
1099 parser.is_unicode = (s[0] == 0xff && s[1] == 0xfe);
1100 get_line = parser.is_unicode ? get_lineW : get_lineA;
1102 parser.file = reg_file;
1103 parser.two_wchars[0] = s[0];
1104 parser.two_wchars[1] = s[1];
1105 parser.reg_version = -1;
1106 parser.hkey = NULL;
1107 parser.key_name = NULL;
1108 parser.value_name = NULL;
1109 parser.parse_type = 0;
1110 parser.data_type = 0;
1111 parser.data = NULL;
1112 parser.data_size = 0;
1113 parser.backslash = FALSE;
1114 parser.state = HEADER;
1116 pos = parser.two_wchars;
1118 /* parser main loop */
1119 while (pos)
1120 pos = (parser_funcs[parser.state])(&parser, pos);
1122 if (parser.reg_version == REG_VERSION_FUZZY || parser.reg_version == REG_VERSION_INVALID)
1123 return parser.reg_version == REG_VERSION_FUZZY;
1125 heap_free(parser.value_name);
1126 close_key(&parser);
1128 return TRUE;
1131 /******************************************************************************
1132 * Removes the registry key with all subkeys. Parses full key name.
1134 * Parameters:
1135 * reg_key_name - full name of registry branch to delete. Ignored if is NULL,
1136 * empty, points to register key class, does not exist.
1138 void delete_registry_key(WCHAR *reg_key_name)
1140 WCHAR *key_name = NULL;
1141 HKEY key_class;
1143 if (!reg_key_name || !reg_key_name[0])
1144 return;
1146 if (!(key_class = parse_key_name(reg_key_name, &key_name)))
1148 if (key_name) *(key_name - 1) = 0;
1149 error_exit(STRING_INVALID_SYSTEM_KEY, reg_key_name);
1152 if (!key_name || !*key_name)
1153 error_exit(STRING_DELETE_FAILED, reg_key_name);
1155 RegDeleteTreeW(key_class, key_name);
1158 static void REGPROC_write_line(FILE *fp, const WCHAR *str, BOOL unicode)
1160 if (unicode)
1161 fwrite(str, sizeof(WCHAR), lstrlenW(str), fp);
1162 else
1164 char *strA = GetMultiByteString(str);
1165 fputs(strA, fp);
1166 heap_free(strA);
1170 static WCHAR *REGPROC_escape_string(WCHAR *str, size_t str_len, size_t *line_len)
1172 size_t i, escape_count, pos;
1173 WCHAR *buf;
1175 for (i = 0, escape_count = 0; i < str_len; i++)
1177 WCHAR c = str[i];
1178 if (c == '\r' || c == '\n' || c == '\\' || c == '"' || c == '\0')
1179 escape_count++;
1182 buf = heap_xalloc((str_len + escape_count + 1) * sizeof(WCHAR));
1184 for (i = 0, pos = 0; i < str_len; i++, pos++)
1186 WCHAR c = str[i];
1188 switch (c)
1190 case '\r':
1191 buf[pos++] = '\\';
1192 buf[pos] = 'r';
1193 break;
1194 case '\n':
1195 buf[pos++] = '\\';
1196 buf[pos] = 'n';
1197 break;
1198 case '\\':
1199 buf[pos++] = '\\';
1200 buf[pos] = '\\';
1201 break;
1202 case '"':
1203 buf[pos++] = '\\';
1204 buf[pos] = '"';
1205 break;
1206 case '\0':
1207 buf[pos++] = '\\';
1208 buf[pos] = '0';
1209 break;
1210 default:
1211 buf[pos] = c;
1215 buf[pos] = 0;
1216 *line_len = pos;
1217 return buf;
1220 static size_t export_value_name(FILE *fp, WCHAR *name, size_t len, BOOL unicode)
1222 static const WCHAR quoted_fmt[] = {'"','%','s','"','=',0};
1223 static const WCHAR default_name[] = {'@','=',0};
1224 size_t line_len;
1226 if (name && *name)
1228 WCHAR *str = REGPROC_escape_string(name, len, &line_len);
1229 WCHAR *buf = heap_xalloc((line_len + 4) * sizeof(WCHAR));
1230 line_len = sprintfW(buf, quoted_fmt, str);
1231 REGPROC_write_line(fp, buf, unicode);
1232 heap_free(buf);
1233 heap_free(str);
1235 else
1237 line_len = lstrlenW(default_name);
1238 REGPROC_write_line(fp, default_name, unicode);
1241 return line_len;
1244 static void export_string_data(WCHAR **buf, WCHAR *data, size_t size)
1246 size_t len, line_len;
1247 WCHAR *str;
1248 static const WCHAR fmt[] = {'"','%','s','"',0};
1250 len = size / sizeof(WCHAR) - 1;
1251 str = REGPROC_escape_string(data, len, &line_len);
1252 *buf = heap_xalloc((line_len + 3) * sizeof(WCHAR));
1253 sprintfW(*buf, fmt, str);
1254 heap_free(str);
1257 static void export_dword_data(WCHAR **buf, DWORD *data)
1259 static const WCHAR fmt[] = {'d','w','o','r','d',':','%','0','8','x',0};
1261 *buf = heap_xalloc(15 * sizeof(WCHAR));
1262 sprintfW(*buf, fmt, *data);
1265 static size_t export_hex_data_type(FILE *fp, DWORD type, BOOL unicode)
1267 static const WCHAR hex[] = {'h','e','x',':',0};
1268 static const WCHAR hexp_fmt[] = {'h','e','x','(','%','x',')',':',0};
1269 size_t line_len;
1271 if (type == REG_BINARY)
1273 line_len = lstrlenW(hex);
1274 REGPROC_write_line(fp, hex, unicode);
1276 else
1278 WCHAR *buf = heap_xalloc(15 * sizeof(WCHAR));
1279 line_len = sprintfW(buf, hexp_fmt, type);
1280 REGPROC_write_line(fp, buf, unicode);
1281 heap_free(buf);
1284 return line_len;
1287 #define MAX_HEX_CHARS 77
1289 static void export_hex_data(FILE *fp, WCHAR **buf, DWORD type, DWORD line_len,
1290 void *data, DWORD size, BOOL unicode)
1292 static const WCHAR fmt[] = {'%','0','2','x',0};
1293 static const WCHAR hex_concat[] = {'\\','\r','\n',' ',' ',0};
1294 size_t num_commas, i, pos;
1296 line_len += export_hex_data_type(fp, type, unicode);
1298 if (!unicode && (type == REG_EXPAND_SZ || type == REG_MULTI_SZ))
1299 data = GetMultiByteStringN(data, size / sizeof(WCHAR), &size);
1301 num_commas = size - 1;
1302 *buf = heap_xalloc(size * 3 * sizeof(WCHAR));
1304 for (i = 0, pos = 0; i < size; i++)
1306 pos += sprintfW(*buf + pos, fmt, ((BYTE *)data)[i]);
1307 if (i == num_commas) break;
1308 (*buf)[pos++] = ',';
1309 (*buf)[pos] = 0;
1310 line_len += 3;
1312 if (line_len >= MAX_HEX_CHARS)
1314 REGPROC_write_line(fp, *buf, unicode);
1315 REGPROC_write_line(fp, hex_concat, unicode);
1316 line_len = 2;
1317 pos = 0;
1322 static void export_newline(FILE *fp, BOOL unicode)
1324 static const WCHAR newline[] = {'\r','\n',0};
1326 REGPROC_write_line(fp, newline, unicode);
1329 static void export_data(FILE *fp, WCHAR *value_name, DWORD value_len, DWORD type,
1330 void *data, size_t size, BOOL unicode)
1332 WCHAR *buf = NULL;
1333 size_t line_len = export_value_name(fp, value_name, value_len, unicode);
1335 switch (type)
1337 case REG_SZ:
1338 export_string_data(&buf, data, size);
1339 break;
1340 case REG_DWORD:
1341 export_dword_data(&buf, data);
1342 break;
1343 case REG_NONE:
1344 case REG_EXPAND_SZ:
1345 case REG_BINARY:
1346 case REG_MULTI_SZ:
1347 default:
1348 export_hex_data(fp, &buf, type, line_len, data, size, unicode);
1349 break;
1352 REGPROC_write_line(fp, buf, unicode);
1353 heap_free(buf);
1354 export_newline(fp, unicode);
1357 static WCHAR *build_subkey_path(WCHAR *path, DWORD path_len, WCHAR *subkey_name, DWORD subkey_len)
1359 WCHAR *subkey_path;
1360 static const WCHAR fmt[] = {'%','s','\\','%','s',0};
1362 subkey_path = heap_xalloc((path_len + subkey_len + 2) * sizeof(WCHAR));
1363 sprintfW(subkey_path, fmt, path, subkey_name);
1365 return subkey_path;
1368 static void export_key_name(FILE *fp, WCHAR *name, BOOL unicode)
1370 static const WCHAR fmt[] = {'\r','\n','[','%','s',']','\r','\n',0};
1371 WCHAR *buf;
1373 buf = heap_xalloc((lstrlenW(name) + 7) * sizeof(WCHAR));
1374 sprintfW(buf, fmt, name);
1375 REGPROC_write_line(fp, buf, unicode);
1376 heap_free(buf);
1379 #define MAX_SUBKEY_LEN 257
1381 static int export_registry_data(FILE *fp, HKEY key, WCHAR *path, BOOL unicode)
1383 LONG rc;
1384 DWORD max_value_len = 256, value_len;
1385 DWORD max_data_bytes = 2048, data_size;
1386 DWORD subkey_len;
1387 DWORD i, type, path_len;
1388 WCHAR *value_name, *subkey_name, *subkey_path;
1389 BYTE *data;
1390 HKEY subkey;
1392 export_key_name(fp, path, unicode);
1394 value_name = heap_xalloc(max_value_len * sizeof(WCHAR));
1395 data = heap_xalloc(max_data_bytes);
1397 i = 0;
1398 for (;;)
1400 value_len = max_value_len;
1401 data_size = max_data_bytes;
1402 rc = RegEnumValueW(key, i, value_name, &value_len, NULL, &type, data, &data_size);
1403 if (rc == ERROR_SUCCESS)
1405 export_data(fp, value_name, value_len, type, data, data_size, unicode);
1406 i++;
1408 else if (rc == ERROR_MORE_DATA)
1410 if (data_size > max_data_bytes)
1412 max_data_bytes = data_size;
1413 data = heap_xrealloc(data, max_data_bytes);
1415 else
1417 max_value_len *= 2;
1418 value_name = heap_xrealloc(value_name, max_value_len * sizeof(WCHAR));
1421 else break;
1424 heap_free(data);
1425 heap_free(value_name);
1427 subkey_name = heap_xalloc(MAX_SUBKEY_LEN * sizeof(WCHAR));
1429 path_len = lstrlenW(path);
1431 i = 0;
1432 for (;;)
1434 subkey_len = MAX_SUBKEY_LEN;
1435 rc = RegEnumKeyExW(key, i, subkey_name, &subkey_len, NULL, NULL, NULL, NULL);
1436 if (rc == ERROR_SUCCESS)
1438 subkey_path = build_subkey_path(path, path_len, subkey_name, subkey_len);
1439 if (!RegOpenKeyExW(key, subkey_name, 0, KEY_READ, &subkey))
1441 export_registry_data(fp, subkey, subkey_path, unicode);
1442 RegCloseKey(subkey);
1444 heap_free(subkey_path);
1445 i++;
1447 else break;
1450 heap_free(subkey_name);
1451 return 0;
1454 static FILE *REGPROC_open_export_file(WCHAR *file_name, BOOL unicode)
1456 FILE *file;
1457 static const WCHAR hyphen[] = {'-',0};
1459 if (!strcmpW(file_name, hyphen))
1461 file = stdout;
1462 _setmode(_fileno(file), _O_BINARY);
1464 else
1466 static const WCHAR wb_mode[] = {'w','b',0};
1468 file = _wfopen(file_name, wb_mode);
1469 if (!file)
1471 static const WCHAR regedit[] = {'r','e','g','e','d','i','t',0};
1472 _wperror(regedit);
1473 error_exit(STRING_CANNOT_OPEN_FILE, file_name);
1477 if (unicode)
1479 static const BYTE bom[] = {0xff,0xfe};
1480 static const WCHAR header[] = {'W','i','n','d','o','w','s',' ',
1481 'R','e','g','i','s','t','r','y',' ','E','d','i','t','o','r',' ',
1482 'V','e','r','s','i','o','n',' ','5','.','0','0','\r','\n'};
1484 fwrite(bom, sizeof(BYTE), ARRAY_SIZE(bom), file);
1485 fwrite(header, sizeof(WCHAR), ARRAY_SIZE(header), file);
1487 else
1488 fputs("REGEDIT4\r\n", file);
1490 return file;
1493 static HKEY open_export_key(HKEY key_class, WCHAR *subkey, WCHAR *path)
1495 HKEY key;
1497 if (!RegOpenKeyExW(key_class, subkey, 0, KEY_READ, &key))
1498 return key;
1500 output_message(STRING_OPEN_KEY_FAILED, path);
1501 return NULL;
1504 static BOOL export_key(WCHAR *file_name, WCHAR *path, BOOL unicode)
1506 HKEY key_class, key;
1507 WCHAR *subkey;
1508 FILE *fp;
1509 BOOL ret;
1511 if (!(key_class = parse_key_name(path, &subkey)))
1513 if (subkey) *(subkey - 1) = 0;
1514 output_message(STRING_INVALID_SYSTEM_KEY, path);
1515 return FALSE;
1518 if (!(key = open_export_key(key_class, subkey, path)))
1519 return FALSE;
1521 fp = REGPROC_open_export_file(file_name, unicode);
1522 ret = export_registry_data(fp, key, path, unicode);
1523 export_newline(fp, unicode);
1524 fclose(fp);
1526 RegCloseKey(key);
1527 return ret;
1530 static BOOL export_all(WCHAR *file_name, WCHAR *path, BOOL unicode)
1532 FILE *fp;
1533 int i;
1534 HKEY classes[] = {HKEY_LOCAL_MACHINE, HKEY_USERS}, key;
1535 WCHAR *class_name;
1537 fp = REGPROC_open_export_file(file_name, unicode);
1539 for (i = 0; i < ARRAY_SIZE(classes); i++)
1541 if (!(key = open_export_key(classes[i], NULL, path)))
1543 fclose(fp);
1544 return FALSE;
1547 class_name = heap_xalloc((lstrlenW(reg_class_namesW[i]) + 1) * sizeof(WCHAR));
1548 lstrcpyW(class_name, reg_class_namesW[i]);
1550 export_registry_data(fp, classes[i], class_name, unicode);
1552 heap_free(class_name);
1553 RegCloseKey(key);
1556 export_newline(fp, unicode);
1557 fclose(fp);
1559 return TRUE;
1562 BOOL export_registry_key(WCHAR *file_name, WCHAR *path, DWORD format)
1564 BOOL unicode = (format == REG_FORMAT_5);
1566 if (path && *path)
1567 return export_key(file_name, path, unicode);
1568 else
1569 return export_all(file_name, path, unicode);