winegstreamer: Make IMFTransform_ProcessOutput checks more consistent.
[wine.git] / programs / regedit / regproc.c
blob4f84c5883bec365db05c1c7c85bde3b3eb370854
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 <fcntl.h>
26 #include <io.h>
27 #include <windows.h>
28 #include <commctrl.h>
30 #include "main.h"
32 #define REG_VAL_BUF_SIZE 4096
34 static HKEY reg_class_keys[] = {
35 HKEY_LOCAL_MACHINE, HKEY_USERS, HKEY_CLASSES_ROOT,
36 HKEY_CURRENT_CONFIG, HKEY_CURRENT_USER, HKEY_DYN_DATA
39 /******************************************************************************
40 * Allocates memory and converts input from multibyte to wide chars
41 * Returned string must be freed by the caller
43 static WCHAR* GetWideString(const char* strA)
45 if(strA)
47 WCHAR* strW;
48 int len = MultiByteToWideChar(CP_ACP, 0, strA, -1, NULL, 0);
50 strW = malloc(len * sizeof(WCHAR));
51 MultiByteToWideChar(CP_ACP, 0, strA, -1, strW, len);
52 return strW;
54 return NULL;
57 /******************************************************************************
58 * Allocates memory and converts input from multibyte to wide chars
59 * Returned string must be freed by the caller
61 static WCHAR* GetWideStringN(const char* strA, int chars, DWORD *len)
63 if(strA)
65 WCHAR* strW;
66 *len = MultiByteToWideChar(CP_ACP, 0, strA, chars, NULL, 0);
68 strW = malloc(*len * sizeof(WCHAR));
69 MultiByteToWideChar(CP_ACP, 0, strA, chars, strW, *len);
70 return strW;
72 *len = 0;
73 return NULL;
76 /******************************************************************************
77 * Allocates memory and converts input from wide chars to multibyte
78 * Returned string must be freed by the caller
80 char* GetMultiByteString(const WCHAR* strW)
82 if(strW)
84 char* strA;
85 int len = WideCharToMultiByte(CP_ACP, 0, strW, -1, NULL, 0, NULL, NULL);
87 strA = malloc(len);
88 WideCharToMultiByte(CP_ACP, 0, strW, -1, strA, len, NULL, NULL);
89 return strA;
91 return NULL;
94 /******************************************************************************
95 * Allocates memory and converts input from wide chars to multibyte
96 * Returned string must be freed by the caller
98 static char* GetMultiByteStringN(const WCHAR* strW, int chars, DWORD* len)
100 if(strW)
102 char* strA;
103 *len = WideCharToMultiByte(CP_ACP, 0, strW, chars, NULL, 0, NULL, NULL);
105 strA = malloc(*len);
106 WideCharToMultiByte(CP_ACP, 0, strW, chars, strA, *len, NULL, NULL);
107 return strA;
109 *len = 0;
110 return NULL;
113 static WCHAR *(*get_line)(FILE *);
115 /* parser definitions */
116 enum parser_state
118 HEADER, /* parsing the registry file version header */
119 PARSE_WIN31_LINE, /* parsing a Windows 3.1 registry line */
120 LINE_START, /* at the beginning of a registry line */
121 KEY_NAME, /* parsing a key name */
122 DELETE_KEY, /* deleting a registry key */
123 DEFAULT_VALUE_NAME, /* parsing a default value name */
124 QUOTED_VALUE_NAME, /* parsing a double-quoted value name */
125 DATA_START, /* preparing for data parsing operations */
126 DELETE_VALUE, /* deleting a registry value */
127 DATA_TYPE, /* parsing the registry data type */
128 STRING_DATA, /* parsing REG_SZ data */
129 DWORD_DATA, /* parsing DWORD data */
130 HEX_DATA, /* parsing REG_BINARY, REG_NONE, REG_EXPAND_SZ or REG_MULTI_SZ data */
131 EOL_BACKSLASH, /* preparing to parse multiple lines of hex data */
132 HEX_MULTILINE, /* parsing multiple lines of hex data */
133 UNKNOWN_DATA, /* parsing an unhandled or invalid data type */
134 SET_VALUE, /* adding a value to the registry */
135 NB_PARSER_STATES
138 struct parser
140 FILE *file; /* pointer to a registry file */
141 WCHAR two_wchars[2]; /* first two characters from the encoding check */
142 BOOL is_unicode; /* parsing Unicode or ASCII data */
143 short int reg_version; /* registry file version */
144 HKEY hkey; /* current registry key */
145 WCHAR *key_name; /* current key name */
146 WCHAR *value_name; /* value name */
147 DWORD parse_type; /* generic data type for parsing */
148 DWORD data_type; /* data type */
149 void *data; /* value data */
150 DWORD data_size; /* size of the data (in bytes) */
151 BOOL backslash; /* TRUE if the current line contains a backslash */
152 enum parser_state state; /* current parser state */
155 typedef WCHAR *(*parser_state_func)(struct parser *parser, WCHAR *pos);
157 /* parser state machine functions */
158 static WCHAR *header_state(struct parser *parser, WCHAR *pos);
159 static WCHAR *parse_win31_line_state(struct parser *parser, WCHAR *pos);
160 static WCHAR *line_start_state(struct parser *parser, WCHAR *pos);
161 static WCHAR *key_name_state(struct parser *parser, WCHAR *pos);
162 static WCHAR *delete_key_state(struct parser *parser, WCHAR *pos);
163 static WCHAR *default_value_name_state(struct parser *parser, WCHAR *pos);
164 static WCHAR *quoted_value_name_state(struct parser *parser, WCHAR *pos);
165 static WCHAR *data_start_state(struct parser *parser, WCHAR *pos);
166 static WCHAR *delete_value_state(struct parser *parser, WCHAR *pos);
167 static WCHAR *data_type_state(struct parser *parser, WCHAR *pos);
168 static WCHAR *string_data_state(struct parser *parser, WCHAR *pos);
169 static WCHAR *dword_data_state(struct parser *parser, WCHAR *pos);
170 static WCHAR *hex_data_state(struct parser *parser, WCHAR *pos);
171 static WCHAR *eol_backslash_state(struct parser *parser, WCHAR *pos);
172 static WCHAR *hex_multiline_state(struct parser *parser, WCHAR *pos);
173 static WCHAR *unknown_data_state(struct parser *parser, WCHAR *pos);
174 static WCHAR *set_value_state(struct parser *parser, WCHAR *pos);
176 static const parser_state_func parser_funcs[NB_PARSER_STATES] =
178 header_state, /* HEADER */
179 parse_win31_line_state, /* PARSE_WIN31_LINE */
180 line_start_state, /* LINE_START */
181 key_name_state, /* KEY_NAME */
182 delete_key_state, /* DELETE_KEY */
183 default_value_name_state, /* DEFAULT_VALUE_NAME */
184 quoted_value_name_state, /* QUOTED_VALUE_NAME */
185 data_start_state, /* DATA_START */
186 delete_value_state, /* DELETE_VALUE */
187 data_type_state, /* DATA_TYPE */
188 string_data_state, /* STRING_DATA */
189 dword_data_state, /* DWORD_DATA */
190 hex_data_state, /* HEX_DATA */
191 eol_backslash_state, /* EOL_BACKSLASH */
192 hex_multiline_state, /* HEX_MULTILINE */
193 unknown_data_state, /* UNKNOWN_DATA */
194 set_value_state, /* SET_VALUE */
197 /* set the new parser state and return the previous one */
198 static inline enum parser_state set_state(struct parser *parser, enum parser_state state)
200 enum parser_state ret = parser->state;
201 parser->state = state;
202 return ret;
205 /******************************************************************************
206 * Converts a hex representation of a DWORD into a DWORD.
208 static BOOL convert_hex_to_dword(WCHAR *str, DWORD *dw)
210 WCHAR *p, *end;
211 int count = 0;
213 while (*str == ' ' || *str == '\t') str++;
214 if (!*str) goto error;
216 p = str;
217 while (iswxdigit(*p))
219 count++;
220 p++;
222 if (count > 8) goto error;
224 end = p;
225 while (*p == ' ' || *p == '\t') p++;
226 if (*p && *p != ';') goto error;
228 *end = 0;
229 *dw = wcstoul(str, &end, 16);
230 return TRUE;
232 error:
233 return FALSE;
236 /******************************************************************************
237 * Converts comma-separated hex data into a binary string and modifies
238 * the input parameter to skip the concatenating backslash, if found.
240 * Returns TRUE or FALSE to indicate whether parsing was successful.
242 static BOOL convert_hex_csv_to_hex(struct parser *parser, WCHAR **str)
244 size_t size;
245 BYTE *d;
246 WCHAR *s;
248 parser->backslash = FALSE;
250 /* The worst case is 1 digit + 1 comma per byte */
251 size = ((lstrlenW(*str) + 1) / 2) + parser->data_size;
252 parser->data = realloc(parser->data, size);
254 s = *str;
255 d = (BYTE *)parser->data + parser->data_size;
257 while (*s)
259 WCHAR *end;
260 unsigned long wc;
262 wc = wcstoul(s, &end, 16);
263 if (wc > 0xff) return FALSE;
265 if (s == end && wc == 0)
267 while (*end == ' ' || *end == '\t') end++;
268 if (*end == '\\')
270 parser->backslash = TRUE;
271 *str = end + 1;
272 return TRUE;
274 else if (*end == ';')
275 return TRUE;
276 return FALSE;
279 *d++ = wc;
280 parser->data_size++;
282 if (*end && *end != ',')
284 while (*end == ' ' || *end == '\t') end++;
285 if (*end && *end != ';') return FALSE;
286 return TRUE;
289 if (*end) end++;
290 s = end;
293 return TRUE;
296 /******************************************************************************
297 * Parses the data type of the registry value being imported and modifies
298 * the input parameter to skip the string representation of the data type.
300 * Returns TRUE or FALSE to indicate whether a data type was found.
302 static BOOL parse_data_type(struct parser *parser, WCHAR **line)
304 struct data_type { const WCHAR *tag; int len; int type; int parse_type; };
306 static const struct data_type data_types[] = {
307 /* tag len type parse type */
308 { L"\"", 1, REG_SZ, REG_SZ },
309 { L"hex:", 4, REG_BINARY, REG_BINARY },
310 { L"dword:", 6, REG_DWORD, REG_DWORD },
311 { L"hex(", 4, -1, REG_BINARY }, /* REG_NONE, REG_EXPAND_SZ, REG_MULTI_SZ */
312 { NULL, 0, 0, 0 }
315 const struct data_type *ptr;
317 for (ptr = data_types; ptr->tag; ptr++)
319 if (wcsncmp(ptr->tag, *line, ptr->len))
320 continue;
322 parser->parse_type = ptr->parse_type;
323 parser->data_type = ptr->parse_type;
324 *line += ptr->len;
326 if (ptr->type == -1)
328 WCHAR *end;
329 DWORD val;
331 if (!**line || towlower((*line)[1]) == 'x')
332 return FALSE;
334 /* "hex(xx):" is special */
335 val = wcstoul(*line, &end, 16);
336 if (*end != ')' || *(end + 1) != ':' || (val == ~0u && errno == ERANGE))
337 return FALSE;
339 parser->data_type = val;
340 *line = end + 2;
342 return TRUE;
344 return FALSE;
347 /******************************************************************************
348 * Replaces escape sequences with their character equivalents and
349 * null-terminates the string on the first non-escaped double quote.
351 * Assigns a pointer to the remaining unparsed data in the line.
352 * Returns TRUE or FALSE to indicate whether a closing double quote was found.
354 static BOOL REGPROC_unescape_string(WCHAR *str, WCHAR **unparsed)
356 int str_idx = 0; /* current character under analysis */
357 int val_idx = 0; /* the last character of the unescaped string */
358 int len = lstrlenW(str);
359 BOOL ret;
361 for (str_idx = 0; str_idx < len; str_idx++, val_idx++) {
362 if (str[str_idx] == '\\') {
363 str_idx++;
364 switch (str[str_idx]) {
365 case 'n':
366 str[val_idx] = '\n';
367 break;
368 case 'r':
369 str[val_idx] = '\r';
370 break;
371 case '0':
372 return FALSE;
373 case '\\':
374 case '"':
375 str[val_idx] = str[str_idx];
376 break;
377 default:
378 if (!str[str_idx]) return FALSE;
379 output_message(STRING_ESCAPE_SEQUENCE, str[str_idx]);
380 str[val_idx] = str[str_idx];
381 break;
383 } else if (str[str_idx] == '"') {
384 break;
385 } else {
386 str[val_idx] = str[str_idx];
390 ret = (str[str_idx] == '"');
391 *unparsed = str + str_idx + 1;
392 str[val_idx] = '\0';
393 return ret;
396 static HKEY parse_key_name(WCHAR *key_name, WCHAR **key_path)
398 unsigned int i;
400 if (!key_name) return 0;
402 *key_path = wcschr(key_name, '\\');
403 if (*key_path) (*key_path)++;
405 for (i = 0; i < ARRAY_SIZE(reg_class_keys); i++)
407 int len = lstrlenW(reg_class_namesW[i]);
408 if (!wcsnicmp(key_name, reg_class_namesW[i], len) &&
409 (key_name[len] == 0 || key_name[len] == '\\'))
411 return reg_class_keys[i];
415 return 0;
418 static void close_key(struct parser *parser)
420 if (parser->hkey)
422 free(parser->key_name);
423 parser->key_name = NULL;
425 RegCloseKey(parser->hkey);
426 parser->hkey = NULL;
430 /******************************************************************************
431 * Opens the registry key given by the input path.
432 * This key must be closed by calling close_key().
434 static LONG open_key(struct parser *parser, WCHAR *path)
436 HKEY key_class;
437 WCHAR *key_path;
438 LONG res;
440 close_key(parser);
442 /* Get the registry class */
443 if (!path || !(key_class = parse_key_name(path, &key_path)))
444 return ERROR_INVALID_PARAMETER;
446 res = RegCreateKeyExW(key_class, key_path, 0, NULL, REG_OPTION_NON_VOLATILE,
447 KEY_ALL_ACCESS, NULL, &parser->hkey, NULL);
449 if (res == ERROR_SUCCESS)
451 parser->key_name = malloc((lstrlenW(path) + 1) * sizeof(WCHAR));
452 lstrcpyW(parser->key_name, path);
454 else
455 parser->hkey = NULL;
457 return res;
460 static void free_parser_data(struct parser *parser)
462 if (parser->parse_type == REG_DWORD || parser->parse_type == REG_BINARY)
463 free(parser->data);
465 parser->data = NULL;
466 parser->data_size = 0;
469 static void prepare_hex_string_data(struct parser *parser)
471 if (parser->data_type == REG_EXPAND_SZ || parser->data_type == REG_MULTI_SZ ||
472 parser->data_type == REG_SZ)
474 if (parser->is_unicode)
476 WCHAR *data = parser->data;
477 DWORD len = parser->data_size / sizeof(WCHAR);
479 if (data[len - 1] != 0)
481 data[len] = 0;
482 parser->data_size += sizeof(WCHAR);
485 else
487 BYTE *data = parser->data;
489 if (data[parser->data_size - 1] != 0)
491 data[parser->data_size] = 0;
492 parser->data_size++;
495 parser->data = GetWideStringN(parser->data, parser->data_size, &parser->data_size);
496 parser->data_size *= sizeof(WCHAR);
497 free(data);
502 enum reg_versions {
503 REG_VERSION_31,
504 REG_VERSION_40,
505 REG_VERSION_50,
506 REG_VERSION_FUZZY,
507 REG_VERSION_INVALID
510 static enum reg_versions parse_file_header(const WCHAR *s)
512 static const WCHAR header_31[] = L"REGEDIT";
514 while (*s == ' ' || *s == '\t') s++;
516 if (!lstrcmpW(s, header_31))
517 return REG_VERSION_31;
519 if (!lstrcmpW(s, L"REGEDIT4"))
520 return REG_VERSION_40;
522 if (!lstrcmpW(s, L"Windows Registry Editor Version 5.00"))
523 return REG_VERSION_50;
525 /* The Windows version accepts registry file headers beginning with "REGEDIT" and ending
526 * with other characters, as long as "REGEDIT" appears at the start of the line. For example,
527 * "REGEDIT 4", "REGEDIT9" and "REGEDIT4FOO" are all treated as valid file headers.
528 * In all such cases, however, the contents of the registry file are not imported.
530 if (!wcsncmp(s, header_31, 7)) /* "REGEDIT" without NUL */
531 return REG_VERSION_FUZZY;
533 return REG_VERSION_INVALID;
536 /* handler for parser HEADER state */
537 static WCHAR *header_state(struct parser *parser, WCHAR *pos)
539 WCHAR *line, *header;
541 if (!(line = get_line(parser->file)))
542 return NULL;
544 if (!parser->is_unicode)
546 header = malloc((lstrlenW(line) + 3) * sizeof(WCHAR));
547 header[0] = parser->two_wchars[0];
548 header[1] = parser->two_wchars[1];
549 lstrcpyW(header + 2, line);
550 parser->reg_version = parse_file_header(header);
551 free(header);
553 else parser->reg_version = parse_file_header(line);
555 switch (parser->reg_version)
557 case REG_VERSION_31:
558 set_state(parser, PARSE_WIN31_LINE);
559 break;
560 case REG_VERSION_40:
561 case REG_VERSION_50:
562 set_state(parser, LINE_START);
563 break;
564 default:
565 get_line(NULL); /* Reset static variables */
566 return NULL;
569 return line;
572 /* handler for parser PARSE_WIN31_LINE state */
573 static WCHAR *parse_win31_line_state(struct parser *parser, WCHAR *pos)
575 WCHAR *line, *value;
576 static WCHAR hkcr[] = L"HKEY_CLASSES_ROOT";
577 unsigned int key_end = 0;
579 if (!(line = get_line(parser->file)))
580 return NULL;
582 if (wcsncmp(line, hkcr, lstrlenW(hkcr)))
583 return line;
585 /* get key name */
586 while (line[key_end] && !iswspace(line[key_end])) key_end++;
588 value = line + key_end;
589 while (*value == ' ' || *value == '\t') value++;
591 if (*value == '=') value++;
592 if (*value == ' ') value++; /* at most one space is skipped */
594 line[key_end] = 0;
596 if (open_key(parser, line) != ERROR_SUCCESS)
598 output_message(STRING_OPEN_KEY_FAILED, line);
599 return line;
602 parser->value_name = NULL;
603 parser->data_type = REG_SZ;
604 parser->data = value;
605 parser->data_size = (lstrlenW(value) + 1) * sizeof(WCHAR);
607 set_state(parser, SET_VALUE);
608 return value;
611 /* handler for parser LINE_START state */
612 static WCHAR *line_start_state(struct parser *parser, WCHAR *pos)
614 WCHAR *line, *p;
616 if (!(line = get_line(parser->file)))
617 return NULL;
619 for (p = line; *p; p++)
621 switch (*p)
623 case '[':
624 set_state(parser, KEY_NAME);
625 return p + 1;
626 case '@':
627 set_state(parser, DEFAULT_VALUE_NAME);
628 return p;
629 case '"':
630 set_state(parser, QUOTED_VALUE_NAME);
631 return p + 1;
632 case ' ':
633 case '\t':
634 break;
635 default:
636 return p;
640 return p;
643 /* handler for parser KEY_NAME state */
644 static WCHAR *key_name_state(struct parser *parser, WCHAR *pos)
646 WCHAR *p = pos, *key_end;
648 if (*p == ' ' || *p == '\t' || !(key_end = wcsrchr(p, ']')))
649 goto done;
651 *key_end = 0;
653 if (*p == '-')
655 set_state(parser, DELETE_KEY);
656 return p + 1;
658 else if (open_key(parser, p) != ERROR_SUCCESS)
659 output_message(STRING_OPEN_KEY_FAILED, p);
661 done:
662 set_state(parser, LINE_START);
663 return p;
666 /* handler for parser DELETE_KEY state */
667 static WCHAR *delete_key_state(struct parser *parser, WCHAR *pos)
669 WCHAR *p = pos;
671 close_key(parser);
673 if (*p == 'H' || *p == 'h')
674 delete_registry_key(p);
676 set_state(parser, LINE_START);
677 return p;
680 /* handler for parser DEFAULT_VALUE_NAME state */
681 static WCHAR *default_value_name_state(struct parser *parser, WCHAR *pos)
683 free(parser->value_name);
684 parser->value_name = NULL;
686 set_state(parser, DATA_START);
687 return pos + 1;
690 /* handler for parser QUOTED_VALUE_NAME state */
691 static WCHAR *quoted_value_name_state(struct parser *parser, WCHAR *pos)
693 WCHAR *val_name = pos, *p;
695 free(parser->value_name);
696 parser->value_name = NULL;
698 if (!REGPROC_unescape_string(val_name, &p))
699 goto invalid;
701 /* copy the value name in case we need to parse multiple lines and the buffer is overwritten */
702 parser->value_name = malloc((lstrlenW(val_name) + 1) * sizeof(WCHAR));
703 lstrcpyW(parser->value_name, val_name);
705 set_state(parser, DATA_START);
706 return p;
708 invalid:
709 set_state(parser, LINE_START);
710 return val_name;
713 /* handler for parser DATA_START state */
714 static WCHAR *data_start_state(struct parser *parser, WCHAR *pos)
716 WCHAR *p = pos;
717 unsigned int len;
719 while (*p == ' ' || *p == '\t') p++;
720 if (*p != '=') goto done;
721 p++;
722 while (*p == ' ' || *p == '\t') p++;
724 /* trim trailing whitespace */
725 len = lstrlenW(p);
726 while (len > 0 && (p[len - 1] == ' ' || p[len - 1] == '\t')) len--;
727 p[len] = 0;
729 if (*p == '-')
730 set_state(parser, DELETE_VALUE);
731 else
732 set_state(parser, DATA_TYPE);
733 return p;
735 done:
736 set_state(parser, LINE_START);
737 return p;
740 /* handler for parser DELETE_VALUE state */
741 static WCHAR *delete_value_state(struct parser *parser, WCHAR *pos)
743 WCHAR *p = pos + 1;
745 while (*p == ' ' || *p == '\t') p++;
746 if (*p && *p != ';') goto done;
748 RegDeleteValueW(parser->hkey, parser->value_name);
750 done:
751 set_state(parser, LINE_START);
752 return p;
755 /* handler for parser DATA_TYPE state */
756 static WCHAR *data_type_state(struct parser *parser, WCHAR *pos)
758 WCHAR *line = pos;
760 if (!parse_data_type(parser, &line))
762 set_state(parser, LINE_START);
763 return line;
766 switch (parser->parse_type)
768 case REG_SZ:
769 set_state(parser, STRING_DATA);
770 break;
771 case REG_DWORD:
772 set_state(parser, DWORD_DATA);
773 break;
774 case REG_BINARY: /* all hex data types, including undefined */
775 set_state(parser, HEX_DATA);
776 break;
777 default:
778 set_state(parser, UNKNOWN_DATA);
781 return line;
784 /* handler for parser STRING_DATA state */
785 static WCHAR *string_data_state(struct parser *parser, WCHAR *pos)
787 WCHAR *line;
789 parser->data = pos;
791 if (!REGPROC_unescape_string(parser->data, &line))
792 goto invalid;
794 while (*line == ' ' || *line == '\t') line++;
795 if (*line && *line != ';') goto invalid;
797 parser->data_size = (lstrlenW(parser->data) + 1) * sizeof(WCHAR);
799 set_state(parser, SET_VALUE);
800 return line;
802 invalid:
803 free_parser_data(parser);
804 set_state(parser, LINE_START);
805 return line;
808 /* handler for parser DWORD_DATA state */
809 static WCHAR *dword_data_state(struct parser *parser, WCHAR *pos)
811 WCHAR *line = pos;
813 parser->data = malloc(sizeof(DWORD));
815 if (!convert_hex_to_dword(line, parser->data))
816 goto invalid;
818 parser->data_size = sizeof(DWORD);
820 set_state(parser, SET_VALUE);
821 return line;
823 invalid:
824 free_parser_data(parser);
825 set_state(parser, LINE_START);
826 return line;
829 /* handler for parser HEX_DATA state */
830 static WCHAR *hex_data_state(struct parser *parser, WCHAR *pos)
832 WCHAR *line = pos;
834 if (!*line)
835 goto set_value;
837 if (!convert_hex_csv_to_hex(parser, &line))
838 goto invalid;
840 if (parser->backslash)
842 set_state(parser, EOL_BACKSLASH);
843 return line;
846 prepare_hex_string_data(parser);
848 set_value:
849 set_state(parser, SET_VALUE);
850 return line;
852 invalid:
853 free_parser_data(parser);
854 set_state(parser, LINE_START);
855 return line;
858 /* handler for parser EOL_BACKSLASH state */
859 static WCHAR *eol_backslash_state(struct parser *parser, WCHAR *pos)
861 WCHAR *p = pos;
863 while (*p == ' ' || *p == '\t') p++;
864 if (*p && *p != ';') goto invalid;
866 set_state(parser, HEX_MULTILINE);
867 return pos;
869 invalid:
870 free_parser_data(parser);
871 set_state(parser, LINE_START);
872 return p;
875 /* handler for parser HEX_MULTILINE state */
876 static WCHAR *hex_multiline_state(struct parser *parser, WCHAR *pos)
878 WCHAR *line;
880 if (!(line = get_line(parser->file)))
882 prepare_hex_string_data(parser);
883 set_state(parser, SET_VALUE);
884 return pos;
887 while (*line == ' ' || *line == '\t') line++;
888 if (!*line || *line == ';') return line;
890 if (!iswxdigit(*line)) goto invalid;
892 set_state(parser, HEX_DATA);
893 return line;
895 invalid:
896 free_parser_data(parser);
897 set_state(parser, LINE_START);
898 return line;
901 /* handler for parser UNKNOWN_DATA state */
902 static WCHAR *unknown_data_state(struct parser *parser, WCHAR *pos)
904 output_message(STRING_UNKNOWN_DATA_FORMAT, parser->data_type);
906 set_state(parser, LINE_START);
907 return pos;
910 /* handler for parser SET_VALUE state */
911 static WCHAR *set_value_state(struct parser *parser, WCHAR *pos)
913 RegSetValueExW(parser->hkey, parser->value_name, 0, parser->data_type,
914 parser->data, parser->data_size);
916 free_parser_data(parser);
918 if (parser->reg_version == REG_VERSION_31)
919 set_state(parser, PARSE_WIN31_LINE);
920 else
921 set_state(parser, LINE_START);
923 return pos;
926 static WCHAR *get_lineA(FILE *fp)
928 static WCHAR *lineW;
929 static size_t size;
930 static char *buf, *next;
931 char *line;
933 free(lineW);
935 if (!fp) goto cleanup;
937 if (!size)
939 size = REG_VAL_BUF_SIZE;
940 buf = malloc(size);
941 *buf = 0;
942 next = buf;
944 line = next;
946 while (next)
948 char *p = strpbrk(line, "\r\n");
949 if (!p)
951 size_t len, count;
952 len = strlen(next);
953 memmove(buf, next, len + 1);
954 if (size - len < 3)
956 size *= 2;
957 buf = realloc(buf, size);
959 if (!(count = fread(buf + len, 1, size - len - 1, fp)))
961 next = NULL;
962 lineW = GetWideString(buf);
963 return lineW;
965 buf[len + count] = 0;
966 next = buf;
967 line = buf;
968 continue;
970 next = p + 1;
971 if (*p == '\r' && *(p + 1) == '\n') next++;
972 *p = 0;
973 lineW = GetWideString(line);
974 return lineW;
977 cleanup:
978 lineW = NULL;
979 if (size) free(buf);
980 size = 0;
981 return NULL;
984 static WCHAR *get_lineW(FILE *fp)
986 static size_t size;
987 static WCHAR *buf, *next;
988 WCHAR *line;
990 if (!fp) goto cleanup;
992 if (!size)
994 size = REG_VAL_BUF_SIZE;
995 buf = malloc(size * sizeof(WCHAR));
996 *buf = 0;
997 next = buf;
999 line = next;
1001 while (next)
1003 WCHAR *p = wcspbrk(line, L"\r\n");
1004 if (!p)
1006 size_t len, count;
1007 len = lstrlenW(next);
1008 memmove(buf, next, (len + 1) * sizeof(WCHAR));
1009 if (size - len < 3)
1011 size *= 2;
1012 buf = realloc(buf, size * sizeof(WCHAR));
1014 if (!(count = fread(buf + len, sizeof(WCHAR), size - len - 1, fp)))
1016 next = NULL;
1017 return buf;
1019 buf[len + count] = 0;
1020 next = buf;
1021 line = buf;
1022 continue;
1024 next = p + 1;
1025 if (*p == '\r' && *(p + 1) == '\n') next++;
1026 *p = 0;
1027 return line;
1030 cleanup:
1031 if (size) free(buf);
1032 size = 0;
1033 return NULL;
1036 /******************************************************************************
1037 * Reads contents of the specified file into the registry.
1039 BOOL import_registry_file(FILE *reg_file)
1041 BYTE s[2];
1042 struct parser parser;
1043 WCHAR *pos;
1045 if (!reg_file || (fread(s, 2, 1, reg_file) != 1))
1046 return FALSE;
1048 parser.is_unicode = (s[0] == 0xff && s[1] == 0xfe);
1049 get_line = parser.is_unicode ? get_lineW : get_lineA;
1051 parser.file = reg_file;
1052 parser.two_wchars[0] = s[0];
1053 parser.two_wchars[1] = s[1];
1054 parser.reg_version = -1;
1055 parser.hkey = NULL;
1056 parser.key_name = NULL;
1057 parser.value_name = NULL;
1058 parser.parse_type = 0;
1059 parser.data_type = 0;
1060 parser.data = NULL;
1061 parser.data_size = 0;
1062 parser.backslash = FALSE;
1063 parser.state = HEADER;
1065 pos = parser.two_wchars;
1067 /* parser main loop */
1068 while (pos)
1069 pos = (parser_funcs[parser.state])(&parser, pos);
1071 if (parser.reg_version == REG_VERSION_FUZZY || parser.reg_version == REG_VERSION_INVALID)
1072 return parser.reg_version == REG_VERSION_FUZZY;
1074 free(parser.value_name);
1075 close_key(&parser);
1077 return TRUE;
1080 /******************************************************************************
1081 * Removes the registry key with all subkeys. Parses full key name.
1083 * Parameters:
1084 * reg_key_name - full name of registry branch to delete. Ignored if is NULL,
1085 * empty, points to register key class, does not exist.
1087 void delete_registry_key(WCHAR *reg_key_name)
1089 WCHAR *key_name = NULL;
1090 HKEY key_class;
1092 if (!reg_key_name || !reg_key_name[0])
1093 return;
1095 if (!(key_class = parse_key_name(reg_key_name, &key_name)))
1097 if (key_name) *(key_name - 1) = 0;
1098 error_exit(STRING_INVALID_SYSTEM_KEY, reg_key_name);
1101 if (!key_name || !*key_name)
1102 error_exit(STRING_DELETE_FAILED, reg_key_name);
1104 RegDeleteTreeW(key_class, key_name);
1107 static void REGPROC_write_line(FILE *fp, const WCHAR *str, BOOL unicode)
1109 if (unicode)
1110 fwrite(str, sizeof(WCHAR), lstrlenW(str), fp);
1111 else
1113 char *strA = GetMultiByteString(str);
1114 fputs(strA, fp);
1115 free(strA);
1119 static WCHAR *REGPROC_escape_string(WCHAR *str, size_t str_len, size_t *line_len)
1121 size_t i, escape_count, pos;
1122 WCHAR *buf;
1124 for (i = 0, escape_count = 0; i < str_len; i++)
1126 WCHAR c = str[i];
1128 if (!c) break;
1130 if (c == '\r' || c == '\n' || c == '\\' || c == '"')
1131 escape_count++;
1134 buf = malloc((str_len + escape_count + 1) * sizeof(WCHAR));
1136 for (i = 0, pos = 0; i < str_len; i++, pos++)
1138 WCHAR c = str[i];
1140 if (!c) break;
1142 switch (c)
1144 case '\r':
1145 buf[pos++] = '\\';
1146 buf[pos] = 'r';
1147 break;
1148 case '\n':
1149 buf[pos++] = '\\';
1150 buf[pos] = 'n';
1151 break;
1152 case '\\':
1153 buf[pos++] = '\\';
1154 buf[pos] = '\\';
1155 break;
1156 case '"':
1157 buf[pos++] = '\\';
1158 buf[pos] = '"';
1159 break;
1160 default:
1161 buf[pos] = c;
1165 buf[pos] = 0;
1166 *line_len = pos;
1167 return buf;
1170 static size_t export_value_name(FILE *fp, WCHAR *name, size_t len, BOOL unicode)
1172 static const WCHAR default_name[] = L"@=";
1173 size_t line_len;
1175 if (name && *name)
1177 WCHAR *str = REGPROC_escape_string(name, len, &line_len);
1178 WCHAR *buf = malloc((line_len + 4) * sizeof(WCHAR));
1179 line_len = swprintf(buf, line_len + 4, L"\"%s\"=", str);
1180 REGPROC_write_line(fp, buf, unicode);
1181 free(buf);
1182 free(str);
1184 else
1186 line_len = lstrlenW(default_name);
1187 REGPROC_write_line(fp, default_name, unicode);
1190 return line_len;
1193 static void export_string_data(WCHAR **buf, WCHAR *data, size_t size)
1195 size_t len = 0, line_len;
1196 WCHAR *str;
1198 if (size)
1199 len = size / sizeof(WCHAR) - 1;
1200 str = REGPROC_escape_string(data, len, &line_len);
1201 *buf = malloc((line_len + 3) * sizeof(WCHAR));
1202 swprintf(*buf, line_len + 3, L"\"%s\"", str);
1203 free(str);
1206 static void export_dword_data(WCHAR **buf, DWORD *data)
1208 *buf = malloc(15 * sizeof(WCHAR));
1209 swprintf(*buf, 15, L"dword:%08x", *data);
1212 static size_t export_hex_data_type(FILE *fp, DWORD type, BOOL unicode)
1214 static const WCHAR hex[] = L"hex:";
1215 size_t line_len;
1217 if (type == REG_BINARY)
1219 line_len = lstrlenW(hex);
1220 REGPROC_write_line(fp, hex, unicode);
1222 else
1224 WCHAR *buf = malloc(15 * sizeof(WCHAR));
1225 line_len = swprintf(buf, 15, L"hex(%x):", type);
1226 REGPROC_write_line(fp, buf, unicode);
1227 free(buf);
1230 return line_len;
1233 #define MAX_HEX_CHARS 77
1235 static void export_hex_data(FILE *fp, WCHAR **buf, DWORD type, DWORD line_len,
1236 void *data, DWORD size, BOOL unicode)
1238 size_t num_commas, i, pos;
1240 line_len += export_hex_data_type(fp, type, unicode);
1242 if (!size) return;
1244 if (!unicode && (type == REG_EXPAND_SZ || type == REG_MULTI_SZ))
1245 data = GetMultiByteStringN(data, size / sizeof(WCHAR), &size);
1247 num_commas = size - 1;
1248 *buf = malloc(size * 3 * sizeof(WCHAR));
1250 for (i = 0, pos = 0; i < size; i++)
1252 pos += swprintf(*buf + pos, 3, L"%02x", ((BYTE *)data)[i]);
1253 if (i == num_commas) break;
1254 (*buf)[pos++] = ',';
1255 (*buf)[pos] = 0;
1256 line_len += 3;
1258 if (line_len >= MAX_HEX_CHARS)
1260 REGPROC_write_line(fp, *buf, unicode);
1261 REGPROC_write_line(fp, L"\\\r\n ", unicode);
1262 line_len = 2;
1263 pos = 0;
1268 static void export_newline(FILE *fp, BOOL unicode)
1270 REGPROC_write_line(fp, L"\r\n", unicode);
1273 static void export_data(FILE *fp, WCHAR *value_name, DWORD value_len, DWORD type,
1274 void *data, size_t size, BOOL unicode)
1276 WCHAR *buf = NULL;
1277 size_t line_len = export_value_name(fp, value_name, value_len, unicode);
1279 switch (type)
1281 case REG_SZ:
1282 export_string_data(&buf, data, size);
1283 break;
1284 case REG_DWORD:
1285 if (size)
1287 export_dword_data(&buf, data);
1288 break;
1290 /* fall through */
1291 case REG_NONE:
1292 case REG_EXPAND_SZ:
1293 case REG_BINARY:
1294 case REG_MULTI_SZ:
1295 default:
1296 export_hex_data(fp, &buf, type, line_len, data, size, unicode);
1297 break;
1300 if (size || type == REG_SZ)
1302 REGPROC_write_line(fp, buf, unicode);
1303 free(buf);
1306 export_newline(fp, unicode);
1309 static WCHAR *build_subkey_path(WCHAR *path, DWORD path_len, WCHAR *subkey_name, DWORD subkey_len)
1311 WCHAR *subkey_path;
1313 subkey_path = malloc((path_len + subkey_len + 2) * sizeof(WCHAR));
1314 swprintf(subkey_path, path_len + subkey_len + 2, L"%s\\%s", path, subkey_name);
1316 return subkey_path;
1319 static void export_key_name(FILE *fp, WCHAR *name, BOOL unicode)
1321 WCHAR *buf;
1323 buf = malloc((lstrlenW(name) + 7) * sizeof(WCHAR));
1324 swprintf(buf, lstrlenW(name) + 7, L"\r\n[%s]\r\n", name);
1325 REGPROC_write_line(fp, buf, unicode);
1326 free(buf);
1329 #define MAX_SUBKEY_LEN 257
1331 static int export_registry_data(FILE *fp, HKEY key, WCHAR *path, BOOL unicode)
1333 LONG rc;
1334 DWORD max_value_len = 256, value_len;
1335 DWORD max_data_bytes = 2048, data_size;
1336 DWORD subkey_len;
1337 DWORD i, type, path_len;
1338 WCHAR *value_name, *subkey_name, *subkey_path;
1339 BYTE *data;
1340 HKEY subkey;
1342 export_key_name(fp, path, unicode);
1344 value_name = malloc(max_value_len * sizeof(WCHAR));
1345 data = malloc(max_data_bytes);
1347 i = 0;
1348 for (;;)
1350 value_len = max_value_len;
1351 data_size = max_data_bytes;
1352 rc = RegEnumValueW(key, i, value_name, &value_len, NULL, &type, data, &data_size);
1353 if (rc == ERROR_SUCCESS)
1355 export_data(fp, value_name, value_len, type, data, data_size, unicode);
1356 i++;
1358 else if (rc == ERROR_MORE_DATA)
1360 if (data_size > max_data_bytes)
1362 max_data_bytes = data_size;
1363 data = realloc(data, max_data_bytes);
1365 else
1367 max_value_len *= 2;
1368 value_name = realloc(value_name, max_value_len * sizeof(WCHAR));
1371 else break;
1374 free(data);
1375 free(value_name);
1377 subkey_name = malloc(MAX_SUBKEY_LEN * sizeof(WCHAR));
1379 path_len = lstrlenW(path);
1381 i = 0;
1382 for (;;)
1384 subkey_len = MAX_SUBKEY_LEN;
1385 rc = RegEnumKeyExW(key, i, subkey_name, &subkey_len, NULL, NULL, NULL, NULL);
1386 if (rc == ERROR_SUCCESS)
1388 subkey_path = build_subkey_path(path, path_len, subkey_name, subkey_len);
1389 if (!RegOpenKeyExW(key, subkey_name, 0, KEY_READ, &subkey))
1391 export_registry_data(fp, subkey, subkey_path, unicode);
1392 RegCloseKey(subkey);
1394 free(subkey_path);
1395 i++;
1397 else break;
1400 free(subkey_name);
1401 return 0;
1404 static FILE *REGPROC_open_export_file(WCHAR *file_name, BOOL unicode)
1406 FILE *file;
1408 if (!lstrcmpW(file_name, L"-"))
1410 file = stdout;
1411 _setmode(_fileno(file), _O_BINARY);
1413 else
1415 file = _wfopen(file_name, L"wb");
1416 if (!file)
1418 _wperror(L"regedit");
1419 error_exit(STRING_CANNOT_OPEN_FILE, file_name);
1423 if (unicode)
1425 static const BYTE bom[] = {0xff,0xfe};
1426 static const WCHAR header[] = L"Windows Registry Editor Version 5.00\r\n";
1428 fwrite(bom, sizeof(BYTE), ARRAY_SIZE(bom), file);
1429 fwrite(header, sizeof(WCHAR), lstrlenW(header), file);
1431 else
1432 fputs("REGEDIT4\r\n", file);
1434 return file;
1437 static HKEY open_export_key(HKEY key_class, WCHAR *subkey, WCHAR *path)
1439 HKEY key;
1441 if (!RegOpenKeyExW(key_class, subkey, 0, KEY_READ, &key))
1442 return key;
1444 output_message(STRING_OPEN_KEY_FAILED, path);
1445 return NULL;
1448 static BOOL export_key(WCHAR *file_name, WCHAR *path, BOOL unicode)
1450 HKEY key_class, key;
1451 WCHAR *subkey;
1452 FILE *fp;
1453 BOOL ret;
1455 if (!(key_class = parse_key_name(path, &subkey)))
1457 if (subkey) *(subkey - 1) = 0;
1458 output_message(STRING_INVALID_SYSTEM_KEY, path);
1459 return FALSE;
1462 if (!(key = open_export_key(key_class, subkey, path)))
1463 return FALSE;
1465 fp = REGPROC_open_export_file(file_name, unicode);
1466 ret = export_registry_data(fp, key, path, unicode);
1467 export_newline(fp, unicode);
1468 fclose(fp);
1470 RegCloseKey(key);
1471 return ret;
1474 static BOOL export_all(WCHAR *file_name, WCHAR *path, BOOL unicode)
1476 FILE *fp;
1477 int i;
1478 HKEY classes[] = {HKEY_LOCAL_MACHINE, HKEY_USERS}, key;
1479 WCHAR *class_name;
1481 fp = REGPROC_open_export_file(file_name, unicode);
1483 for (i = 0; i < ARRAY_SIZE(classes); i++)
1485 if (!(key = open_export_key(classes[i], NULL, path)))
1487 fclose(fp);
1488 return FALSE;
1491 class_name = malloc((lstrlenW(reg_class_namesW[i]) + 1) * sizeof(WCHAR));
1492 lstrcpyW(class_name, reg_class_namesW[i]);
1494 export_registry_data(fp, classes[i], class_name, unicode);
1496 free(class_name);
1497 RegCloseKey(key);
1500 export_newline(fp, unicode);
1501 fclose(fp);
1503 return TRUE;
1506 BOOL export_registry_key(WCHAR *file_name, WCHAR *path, DWORD format)
1508 BOOL unicode = (format == REG_FORMAT_5);
1510 if (path && *path)
1511 return export_key(file_name, path, unicode);
1512 else
1513 return export_all(file_name, path, unicode);