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
30 #include <wine/debug.h>
31 #include <wine/heap.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
);
48 ERR("Out of memory!\n");
54 void *heap_xrealloc(void *buf
, size_t size
)
56 void *new_buf
= heap_realloc(buf
, size
);
60 ERR("Out of memory!\n");
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
)
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
);
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
)
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
);
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
)
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
);
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
)
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
);
141 static WCHAR
*(*get_line
)(FILE *);
143 /* parser definitions */
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 */
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
;
233 /******************************************************************************
234 * Converts a hex representation of a DWORD into a DWORD.
236 static BOOL
convert_hex_to_dword(WCHAR
*str
, DWORD
*dw
)
241 while (*str
== ' ' || *str
== '\t') str
++;
242 if (!*str
) goto error
;
245 while (iswxdigit(*p
))
250 if (count
> 8) goto error
;
253 while (*p
== ' ' || *p
== '\t') p
++;
254 if (*p
&& *p
!= ';') goto error
;
257 *dw
= wcstoul(str
, &end
, 16);
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
)
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
);
283 d
= (BYTE
*)parser
->data
+ parser
->data_size
;
290 wc
= wcstoul(s
, &end
, 16);
291 if (wc
> 0xff) return FALSE
;
293 if (s
== end
&& wc
== 0)
295 while (*end
== ' ' || *end
== '\t') end
++;
298 parser
->backslash
= TRUE
;
302 else if (*end
== ';')
310 if (*end
&& *end
!= ',')
312 while (*end
== ' ' || *end
== '\t') end
++;
313 if (*end
&& *end
!= ';') return FALSE
;
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 */
348 const struct data_type
*ptr
;
350 for (ptr
= data_types
; ptr
->tag
; ptr
++)
352 if (wcsncmp(ptr
->tag
, *line
, ptr
->len
))
355 parser
->parse_type
= ptr
->parse_type
;
356 parser
->data_type
= ptr
->parse_type
;
364 if (!**line
|| towlower((*line
)[1]) == 'x')
367 /* "hex(xx):" is special */
368 val
= wcstoul(*line
, &end
, 16);
369 if (*end
!= ')' || *(end
+ 1) != ':' || (val
== ~0u && errno
== ERANGE
))
372 parser
->data_type
= val
;
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
);
394 for (str_idx
= 0; str_idx
< len
; str_idx
++, val_idx
++) {
395 if (str
[str_idx
] == '\\') {
397 switch (str
[str_idx
]) {
409 str
[val_idx
] = str
[str_idx
];
412 if (!str
[str_idx
]) return FALSE
;
413 output_message(STRING_ESCAPE_SEQUENCE
, str
[str_idx
]);
414 str
[val_idx
] = str
[str_idx
];
417 } else if (str
[str_idx
] == '"') {
420 str
[val_idx
] = str
[str_idx
];
424 ret
= (str
[str_idx
] == '"');
425 *unparsed
= str
+ str_idx
+ 1;
430 static HKEY
parse_key_name(WCHAR
*key_name
, WCHAR
**key_path
)
434 if (!key_name
) return 0;
436 *key_path
= wcschr(key_name
, '\\');
437 if (*key_path
) (*key_path
)++;
439 for (i
= 0; i
< ARRAY_SIZE(reg_class_keys
); i
++)
441 int len
= lstrlenW(reg_class_namesW
[i
]);
442 if (!wcsnicmp(key_name
, reg_class_namesW
[i
], len
) &&
443 (key_name
[len
] == 0 || key_name
[len
] == '\\'))
445 return reg_class_keys
[i
];
452 static void close_key(struct parser
*parser
)
456 heap_free(parser
->key_name
);
457 parser
->key_name
= NULL
;
459 RegCloseKey(parser
->hkey
);
464 /******************************************************************************
465 * Opens the registry key given by the input path.
466 * This key must be closed by calling close_key().
468 static LONG
open_key(struct parser
*parser
, WCHAR
*path
)
476 /* Get the registry class */
477 if (!path
|| !(key_class
= parse_key_name(path
, &key_path
)))
478 return ERROR_INVALID_PARAMETER
;
480 res
= RegCreateKeyExW(key_class
, key_path
, 0, NULL
, REG_OPTION_NON_VOLATILE
,
481 KEY_ALL_ACCESS
, NULL
, &parser
->hkey
, NULL
);
483 if (res
== ERROR_SUCCESS
)
485 parser
->key_name
= heap_xalloc((lstrlenW(path
) + 1) * sizeof(WCHAR
));
486 lstrcpyW(parser
->key_name
, path
);
494 static void free_parser_data(struct parser
*parser
)
496 if (parser
->parse_type
== REG_DWORD
|| parser
->parse_type
== REG_BINARY
)
497 heap_free(parser
->data
);
500 parser
->data_size
= 0;
503 static void prepare_hex_string_data(struct parser
*parser
)
505 if (parser
->data_type
== REG_EXPAND_SZ
|| parser
->data_type
== REG_MULTI_SZ
||
506 parser
->data_type
== REG_SZ
)
508 if (parser
->is_unicode
)
510 WCHAR
*data
= parser
->data
;
511 DWORD len
= parser
->data_size
/ sizeof(WCHAR
);
513 if (data
[len
- 1] != 0)
516 parser
->data_size
+= sizeof(WCHAR
);
521 BYTE
*data
= parser
->data
;
523 if (data
[parser
->data_size
- 1] != 0)
525 data
[parser
->data_size
] = 0;
529 parser
->data
= GetWideStringN(parser
->data
, parser
->data_size
, &parser
->data_size
);
530 parser
->data_size
*= sizeof(WCHAR
);
544 static enum reg_versions
parse_file_header(const WCHAR
*s
)
546 static const WCHAR header_31
[] = {'R','E','G','E','D','I','T',0};
547 static const WCHAR header_40
[] = {'R','E','G','E','D','I','T','4',0};
548 static const WCHAR header_50
[] = {'W','i','n','d','o','w','s',' ',
549 'R','e','g','i','s','t','r','y',' ','E','d','i','t','o','r',' ',
550 'V','e','r','s','i','o','n',' ','5','.','0','0',0};
552 while (*s
== ' ' || *s
== '\t') s
++;
554 if (!lstrcmpW(s
, header_31
))
555 return REG_VERSION_31
;
557 if (!lstrcmpW(s
, header_40
))
558 return REG_VERSION_40
;
560 if (!lstrcmpW(s
, header_50
))
561 return REG_VERSION_50
;
563 /* The Windows version accepts registry file headers beginning with "REGEDIT" and ending
564 * with other characters, as long as "REGEDIT" appears at the start of the line. For example,
565 * "REGEDIT 4", "REGEDIT9" and "REGEDIT4FOO" are all treated as valid file headers.
566 * In all such cases, however, the contents of the registry file are not imported.
568 if (!wcsncmp(s
, header_31
, 7)) /* "REGEDIT" without NUL */
569 return REG_VERSION_FUZZY
;
571 return REG_VERSION_INVALID
;
574 /* handler for parser HEADER state */
575 static WCHAR
*header_state(struct parser
*parser
, WCHAR
*pos
)
577 WCHAR
*line
, *header
;
579 if (!(line
= get_line(parser
->file
)))
582 if (!parser
->is_unicode
)
584 header
= heap_xalloc((lstrlenW(line
) + 3) * sizeof(WCHAR
));
585 header
[0] = parser
->two_wchars
[0];
586 header
[1] = parser
->two_wchars
[1];
587 lstrcpyW(header
+ 2, line
);
588 parser
->reg_version
= parse_file_header(header
);
591 else parser
->reg_version
= parse_file_header(line
);
593 switch (parser
->reg_version
)
596 set_state(parser
, PARSE_WIN31_LINE
);
600 set_state(parser
, LINE_START
);
603 get_line(NULL
); /* Reset static variables */
610 /* handler for parser PARSE_WIN31_LINE state */
611 static WCHAR
*parse_win31_line_state(struct parser
*parser
, WCHAR
*pos
)
614 static WCHAR hkcr
[] = {'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T'};
615 unsigned int key_end
= 0;
617 if (!(line
= get_line(parser
->file
)))
620 if (wcsncmp(line
, hkcr
, ARRAY_SIZE(hkcr
)))
624 while (line
[key_end
] && !iswspace(line
[key_end
])) key_end
++;
626 value
= line
+ key_end
;
627 while (*value
== ' ' || *value
== '\t') value
++;
629 if (*value
== '=') value
++;
630 if (*value
== ' ') value
++; /* at most one space is skipped */
634 if (open_key(parser
, line
) != ERROR_SUCCESS
)
636 output_message(STRING_OPEN_KEY_FAILED
, line
);
640 parser
->value_name
= NULL
;
641 parser
->data_type
= REG_SZ
;
642 parser
->data
= value
;
643 parser
->data_size
= (lstrlenW(value
) + 1) * sizeof(WCHAR
);
645 set_state(parser
, SET_VALUE
);
649 /* handler for parser LINE_START state */
650 static WCHAR
*line_start_state(struct parser
*parser
, WCHAR
*pos
)
654 if (!(line
= get_line(parser
->file
)))
657 for (p
= line
; *p
; p
++)
662 set_state(parser
, KEY_NAME
);
665 set_state(parser
, DEFAULT_VALUE_NAME
);
668 set_state(parser
, QUOTED_VALUE_NAME
);
681 /* handler for parser KEY_NAME state */
682 static WCHAR
*key_name_state(struct parser
*parser
, WCHAR
*pos
)
684 WCHAR
*p
= pos
, *key_end
;
686 if (*p
== ' ' || *p
== '\t' || !(key_end
= wcsrchr(p
, ']')))
693 set_state(parser
, DELETE_KEY
);
696 else if (open_key(parser
, p
) != ERROR_SUCCESS
)
697 output_message(STRING_OPEN_KEY_FAILED
, p
);
700 set_state(parser
, LINE_START
);
704 /* handler for parser DELETE_KEY state */
705 static WCHAR
*delete_key_state(struct parser
*parser
, WCHAR
*pos
)
711 if (*p
== 'H' || *p
== 'h')
712 delete_registry_key(p
);
714 set_state(parser
, LINE_START
);
718 /* handler for parser DEFAULT_VALUE_NAME state */
719 static WCHAR
*default_value_name_state(struct parser
*parser
, WCHAR
*pos
)
721 heap_free(parser
->value_name
);
722 parser
->value_name
= NULL
;
724 set_state(parser
, DATA_START
);
728 /* handler for parser QUOTED_VALUE_NAME state */
729 static WCHAR
*quoted_value_name_state(struct parser
*parser
, WCHAR
*pos
)
731 WCHAR
*val_name
= pos
, *p
;
733 heap_free(parser
->value_name
);
734 parser
->value_name
= NULL
;
736 if (!REGPROC_unescape_string(val_name
, &p
))
739 /* copy the value name in case we need to parse multiple lines and the buffer is overwritten */
740 parser
->value_name
= heap_xalloc((lstrlenW(val_name
) + 1) * sizeof(WCHAR
));
741 lstrcpyW(parser
->value_name
, val_name
);
743 set_state(parser
, DATA_START
);
747 set_state(parser
, LINE_START
);
751 /* handler for parser DATA_START state */
752 static WCHAR
*data_start_state(struct parser
*parser
, WCHAR
*pos
)
757 while (*p
== ' ' || *p
== '\t') p
++;
758 if (*p
!= '=') goto done
;
760 while (*p
== ' ' || *p
== '\t') p
++;
762 /* trim trailing whitespace */
764 while (len
> 0 && (p
[len
- 1] == ' ' || p
[len
- 1] == '\t')) len
--;
768 set_state(parser
, DELETE_VALUE
);
770 set_state(parser
, DATA_TYPE
);
774 set_state(parser
, LINE_START
);
778 /* handler for parser DELETE_VALUE state */
779 static WCHAR
*delete_value_state(struct parser
*parser
, WCHAR
*pos
)
783 while (*p
== ' ' || *p
== '\t') p
++;
784 if (*p
&& *p
!= ';') goto done
;
786 RegDeleteValueW(parser
->hkey
, parser
->value_name
);
789 set_state(parser
, LINE_START
);
793 /* handler for parser DATA_TYPE state */
794 static WCHAR
*data_type_state(struct parser
*parser
, WCHAR
*pos
)
798 if (!parse_data_type(parser
, &line
))
800 set_state(parser
, LINE_START
);
804 switch (parser
->parse_type
)
807 set_state(parser
, STRING_DATA
);
810 set_state(parser
, DWORD_DATA
);
812 case REG_BINARY
: /* all hex data types, including undefined */
813 set_state(parser
, HEX_DATA
);
816 set_state(parser
, UNKNOWN_DATA
);
822 /* handler for parser STRING_DATA state */
823 static WCHAR
*string_data_state(struct parser
*parser
, WCHAR
*pos
)
829 if (!REGPROC_unescape_string(parser
->data
, &line
))
832 while (*line
== ' ' || *line
== '\t') line
++;
833 if (*line
&& *line
!= ';') goto invalid
;
835 parser
->data_size
= (lstrlenW(parser
->data
) + 1) * sizeof(WCHAR
);
837 set_state(parser
, SET_VALUE
);
841 free_parser_data(parser
);
842 set_state(parser
, LINE_START
);
846 /* handler for parser DWORD_DATA state */
847 static WCHAR
*dword_data_state(struct parser
*parser
, WCHAR
*pos
)
851 parser
->data
= heap_xalloc(sizeof(DWORD
));
853 if (!convert_hex_to_dword(line
, parser
->data
))
856 parser
->data_size
= sizeof(DWORD
);
858 set_state(parser
, SET_VALUE
);
862 free_parser_data(parser
);
863 set_state(parser
, LINE_START
);
867 /* handler for parser HEX_DATA state */
868 static WCHAR
*hex_data_state(struct parser
*parser
, WCHAR
*pos
)
875 if (!convert_hex_csv_to_hex(parser
, &line
))
878 if (parser
->backslash
)
880 set_state(parser
, EOL_BACKSLASH
);
884 prepare_hex_string_data(parser
);
887 set_state(parser
, SET_VALUE
);
891 free_parser_data(parser
);
892 set_state(parser
, LINE_START
);
896 /* handler for parser EOL_BACKSLASH state */
897 static WCHAR
*eol_backslash_state(struct parser
*parser
, WCHAR
*pos
)
901 while (*p
== ' ' || *p
== '\t') p
++;
902 if (*p
&& *p
!= ';') goto invalid
;
904 set_state(parser
, HEX_MULTILINE
);
908 free_parser_data(parser
);
909 set_state(parser
, LINE_START
);
913 /* handler for parser HEX_MULTILINE state */
914 static WCHAR
*hex_multiline_state(struct parser
*parser
, WCHAR
*pos
)
918 if (!(line
= get_line(parser
->file
)))
920 prepare_hex_string_data(parser
);
921 set_state(parser
, SET_VALUE
);
925 while (*line
== ' ' || *line
== '\t') line
++;
926 if (!*line
|| *line
== ';') return line
;
928 if (!iswxdigit(*line
)) goto invalid
;
930 set_state(parser
, HEX_DATA
);
934 free_parser_data(parser
);
935 set_state(parser
, LINE_START
);
939 /* handler for parser UNKNOWN_DATA state */
940 static WCHAR
*unknown_data_state(struct parser
*parser
, WCHAR
*pos
)
942 output_message(STRING_UNKNOWN_DATA_FORMAT
, parser
->data_type
);
944 set_state(parser
, LINE_START
);
948 /* handler for parser SET_VALUE state */
949 static WCHAR
*set_value_state(struct parser
*parser
, WCHAR
*pos
)
951 RegSetValueExW(parser
->hkey
, parser
->value_name
, 0, parser
->data_type
,
952 parser
->data
, parser
->data_size
);
954 free_parser_data(parser
);
956 if (parser
->reg_version
== REG_VERSION_31
)
957 set_state(parser
, PARSE_WIN31_LINE
);
959 set_state(parser
, LINE_START
);
964 static WCHAR
*get_lineA(FILE *fp
)
968 static char *buf
, *next
;
973 if (!fp
) goto cleanup
;
977 size
= REG_VAL_BUF_SIZE
;
978 buf
= heap_xalloc(size
);
986 char *p
= strpbrk(line
, "\r\n");
991 memmove(buf
, next
, len
+ 1);
995 buf
= heap_xrealloc(buf
, size
);
997 if (!(count
= fread(buf
+ len
, 1, size
- len
- 1, fp
)))
1000 lineW
= GetWideString(buf
);
1003 buf
[len
+ count
] = 0;
1009 if (*p
== '\r' && *(p
+ 1) == '\n') next
++;
1011 lineW
= GetWideString(line
);
1017 if (size
) heap_free(buf
);
1022 static WCHAR
*get_lineW(FILE *fp
)
1025 static WCHAR
*buf
, *next
;
1028 if (!fp
) goto cleanup
;
1032 size
= REG_VAL_BUF_SIZE
;
1033 buf
= heap_xalloc(size
* sizeof(WCHAR
));
1041 static const WCHAR line_endings
[] = {'\r','\n',0};
1042 WCHAR
*p
= wcspbrk(line
, line_endings
);
1046 len
= lstrlenW(next
);
1047 memmove(buf
, next
, (len
+ 1) * sizeof(WCHAR
));
1051 buf
= heap_xrealloc(buf
, size
* sizeof(WCHAR
));
1053 if (!(count
= fread(buf
+ len
, sizeof(WCHAR
), size
- len
- 1, fp
)))
1058 buf
[len
+ count
] = 0;
1064 if (*p
== '\r' && *(p
+ 1) == '\n') next
++;
1070 if (size
) heap_free(buf
);
1075 /******************************************************************************
1076 * Reads contents of the specified file into the registry.
1078 BOOL
import_registry_file(FILE *reg_file
)
1081 struct parser parser
;
1084 if (!reg_file
|| (fread(s
, 2, 1, reg_file
) != 1))
1087 parser
.is_unicode
= (s
[0] == 0xff && s
[1] == 0xfe);
1088 get_line
= parser
.is_unicode
? get_lineW
: get_lineA
;
1090 parser
.file
= reg_file
;
1091 parser
.two_wchars
[0] = s
[0];
1092 parser
.two_wchars
[1] = s
[1];
1093 parser
.reg_version
= -1;
1095 parser
.key_name
= NULL
;
1096 parser
.value_name
= NULL
;
1097 parser
.parse_type
= 0;
1098 parser
.data_type
= 0;
1100 parser
.data_size
= 0;
1101 parser
.backslash
= FALSE
;
1102 parser
.state
= HEADER
;
1104 pos
= parser
.two_wchars
;
1106 /* parser main loop */
1108 pos
= (parser_funcs
[parser
.state
])(&parser
, pos
);
1110 if (parser
.reg_version
== REG_VERSION_FUZZY
|| parser
.reg_version
== REG_VERSION_INVALID
)
1111 return parser
.reg_version
== REG_VERSION_FUZZY
;
1113 heap_free(parser
.value_name
);
1119 /******************************************************************************
1120 * Removes the registry key with all subkeys. Parses full key name.
1123 * reg_key_name - full name of registry branch to delete. Ignored if is NULL,
1124 * empty, points to register key class, does not exist.
1126 void delete_registry_key(WCHAR
*reg_key_name
)
1128 WCHAR
*key_name
= NULL
;
1131 if (!reg_key_name
|| !reg_key_name
[0])
1134 if (!(key_class
= parse_key_name(reg_key_name
, &key_name
)))
1136 if (key_name
) *(key_name
- 1) = 0;
1137 error_exit(STRING_INVALID_SYSTEM_KEY
, reg_key_name
);
1140 if (!key_name
|| !*key_name
)
1141 error_exit(STRING_DELETE_FAILED
, reg_key_name
);
1143 RegDeleteTreeW(key_class
, key_name
);
1146 static void REGPROC_write_line(FILE *fp
, const WCHAR
*str
, BOOL unicode
)
1149 fwrite(str
, sizeof(WCHAR
), lstrlenW(str
), fp
);
1152 char *strA
= GetMultiByteString(str
);
1158 static WCHAR
*REGPROC_escape_string(WCHAR
*str
, size_t str_len
, size_t *line_len
)
1160 size_t i
, escape_count
, pos
;
1163 for (i
= 0, escape_count
= 0; i
< str_len
; i
++)
1169 if (c
== '\r' || c
== '\n' || c
== '\\' || c
== '"')
1173 buf
= heap_xalloc((str_len
+ escape_count
+ 1) * sizeof(WCHAR
));
1175 for (i
= 0, pos
= 0; i
< str_len
; i
++, pos
++)
1209 static size_t export_value_name(FILE *fp
, WCHAR
*name
, size_t len
, BOOL unicode
)
1211 static const WCHAR quoted_fmt
[] = {'"','%','s','"','=',0};
1212 static const WCHAR default_name
[] = {'@','=',0};
1217 WCHAR
*str
= REGPROC_escape_string(name
, len
, &line_len
);
1218 WCHAR
*buf
= heap_xalloc((line_len
+ 4) * sizeof(WCHAR
));
1219 line_len
= swprintf(buf
, line_len
+ 4, quoted_fmt
, str
);
1220 REGPROC_write_line(fp
, buf
, unicode
);
1226 line_len
= lstrlenW(default_name
);
1227 REGPROC_write_line(fp
, default_name
, unicode
);
1233 static void export_string_data(WCHAR
**buf
, WCHAR
*data
, size_t size
)
1235 size_t len
= 0, line_len
;
1237 static const WCHAR fmt
[] = {'"','%','s','"',0};
1240 len
= size
/ sizeof(WCHAR
) - 1;
1241 str
= REGPROC_escape_string(data
, len
, &line_len
);
1242 *buf
= heap_xalloc((line_len
+ 3) * sizeof(WCHAR
));
1243 swprintf(*buf
, line_len
+ 3, fmt
, str
);
1247 static void export_dword_data(WCHAR
**buf
, DWORD
*data
)
1249 static const WCHAR fmt
[] = {'d','w','o','r','d',':','%','0','8','x',0};
1251 *buf
= heap_xalloc(15 * sizeof(WCHAR
));
1252 swprintf(*buf
, 15, fmt
, *data
);
1255 static size_t export_hex_data_type(FILE *fp
, DWORD type
, BOOL unicode
)
1257 static const WCHAR hex
[] = {'h','e','x',':',0};
1258 static const WCHAR hexp_fmt
[] = {'h','e','x','(','%','x',')',':',0};
1261 if (type
== REG_BINARY
)
1263 line_len
= lstrlenW(hex
);
1264 REGPROC_write_line(fp
, hex
, unicode
);
1268 WCHAR
*buf
= heap_xalloc(15 * sizeof(WCHAR
));
1269 line_len
= swprintf(buf
, 15, hexp_fmt
, type
);
1270 REGPROC_write_line(fp
, buf
, unicode
);
1277 #define MAX_HEX_CHARS 77
1279 static void export_hex_data(FILE *fp
, WCHAR
**buf
, DWORD type
, DWORD line_len
,
1280 void *data
, DWORD size
, BOOL unicode
)
1282 static const WCHAR fmt
[] = {'%','0','2','x',0};
1283 static const WCHAR hex_concat
[] = {'\\','\r','\n',' ',' ',0};
1284 size_t num_commas
, i
, pos
;
1286 line_len
+= export_hex_data_type(fp
, type
, unicode
);
1290 if (!unicode
&& (type
== REG_EXPAND_SZ
|| type
== REG_MULTI_SZ
))
1291 data
= GetMultiByteStringN(data
, size
/ sizeof(WCHAR
), &size
);
1293 num_commas
= size
- 1;
1294 *buf
= heap_xalloc(size
* 3 * sizeof(WCHAR
));
1296 for (i
= 0, pos
= 0; i
< size
; i
++)
1298 pos
+= swprintf(*buf
+ pos
, 3, fmt
, ((BYTE
*)data
)[i
]);
1299 if (i
== num_commas
) break;
1300 (*buf
)[pos
++] = ',';
1304 if (line_len
>= MAX_HEX_CHARS
)
1306 REGPROC_write_line(fp
, *buf
, unicode
);
1307 REGPROC_write_line(fp
, hex_concat
, unicode
);
1314 static void export_newline(FILE *fp
, BOOL unicode
)
1316 static const WCHAR newline
[] = {'\r','\n',0};
1318 REGPROC_write_line(fp
, newline
, unicode
);
1321 static void export_data(FILE *fp
, WCHAR
*value_name
, DWORD value_len
, DWORD type
,
1322 void *data
, size_t size
, BOOL unicode
)
1325 size_t line_len
= export_value_name(fp
, value_name
, value_len
, unicode
);
1330 export_string_data(&buf
, data
, size
);
1335 export_dword_data(&buf
, data
);
1344 export_hex_data(fp
, &buf
, type
, line_len
, data
, size
, unicode
);
1348 if (size
|| type
== REG_SZ
)
1350 REGPROC_write_line(fp
, buf
, unicode
);
1354 export_newline(fp
, unicode
);
1357 static WCHAR
*build_subkey_path(WCHAR
*path
, DWORD path_len
, WCHAR
*subkey_name
, DWORD subkey_len
)
1360 static const WCHAR fmt
[] = {'%','s','\\','%','s',0};
1362 subkey_path
= heap_xalloc((path_len
+ subkey_len
+ 2) * sizeof(WCHAR
));
1363 swprintf(subkey_path
, path_len
+ subkey_len
+ 2, fmt
, path
, subkey_name
);
1368 static void export_key_name(FILE *fp
, WCHAR
*name
, BOOL unicode
)
1370 static const WCHAR fmt
[] = {'\r','\n','[','%','s',']','\r','\n',0};
1373 buf
= heap_xalloc((lstrlenW(name
) + 7) * sizeof(WCHAR
));
1374 swprintf(buf
, lstrlenW(name
) + 7, fmt
, name
);
1375 REGPROC_write_line(fp
, buf
, unicode
);
1379 #define MAX_SUBKEY_LEN 257
1381 static int export_registry_data(FILE *fp
, HKEY key
, WCHAR
*path
, BOOL unicode
)
1384 DWORD max_value_len
= 256, value_len
;
1385 DWORD max_data_bytes
= 2048, data_size
;
1387 DWORD i
, type
, path_len
;
1388 WCHAR
*value_name
, *subkey_name
, *subkey_path
;
1392 export_key_name(fp
, path
, unicode
);
1394 value_name
= heap_xalloc(max_value_len
* sizeof(WCHAR
));
1395 data
= heap_xalloc(max_data_bytes
);
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
);
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
);
1418 value_name
= heap_xrealloc(value_name
, max_value_len
* sizeof(WCHAR
));
1425 heap_free(value_name
);
1427 subkey_name
= heap_xalloc(MAX_SUBKEY_LEN
* sizeof(WCHAR
));
1429 path_len
= lstrlenW(path
);
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
);
1450 heap_free(subkey_name
);
1454 static FILE *REGPROC_open_export_file(WCHAR
*file_name
, BOOL unicode
)
1457 static const WCHAR hyphen
[] = {'-',0};
1459 if (!lstrcmpW(file_name
, hyphen
))
1462 _setmode(_fileno(file
), _O_BINARY
);
1466 static const WCHAR wb_mode
[] = {'w','b',0};
1468 file
= _wfopen(file_name
, wb_mode
);
1471 static const WCHAR regedit
[] = {'r','e','g','e','d','i','t',0};
1473 error_exit(STRING_CANNOT_OPEN_FILE
, file_name
);
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
);
1488 fputs("REGEDIT4\r\n", file
);
1493 static HKEY
open_export_key(HKEY key_class
, WCHAR
*subkey
, WCHAR
*path
)
1497 if (!RegOpenKeyExW(key_class
, subkey
, 0, KEY_READ
, &key
))
1500 output_message(STRING_OPEN_KEY_FAILED
, path
);
1504 static BOOL
export_key(WCHAR
*file_name
, WCHAR
*path
, BOOL unicode
)
1506 HKEY key_class
, key
;
1511 if (!(key_class
= parse_key_name(path
, &subkey
)))
1513 if (subkey
) *(subkey
- 1) = 0;
1514 output_message(STRING_INVALID_SYSTEM_KEY
, path
);
1518 if (!(key
= open_export_key(key_class
, subkey
, path
)))
1521 fp
= REGPROC_open_export_file(file_name
, unicode
);
1522 ret
= export_registry_data(fp
, key
, path
, unicode
);
1523 export_newline(fp
, unicode
);
1530 static BOOL
export_all(WCHAR
*file_name
, WCHAR
*path
, BOOL unicode
)
1534 HKEY classes
[] = {HKEY_LOCAL_MACHINE
, HKEY_USERS
}, key
;
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
)))
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
);
1556 export_newline(fp
, unicode
);
1562 BOOL
export_registry_key(WCHAR
*file_name
, WCHAR
*path
, DWORD format
)
1564 BOOL unicode
= (format
== REG_FORMAT_5
);
1567 return export_key(file_name
, path
, unicode
);
1569 return export_all(file_name
, path
, unicode
);