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/unicode.h>
31 #include <wine/debug.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 static void *heap_xalloc(size_t size
)
49 void *buf
= HeapAlloc(GetProcessHeap(), 0, size
);
52 ERR("Out of memory!\n");
58 /******************************************************************************
59 * Allocates memory and converts input from multibyte to wide chars
60 * Returned string must be freed by the caller
62 static WCHAR
* GetWideString(const char* strA
)
67 int len
= MultiByteToWideChar(CP_ACP
, 0, strA
, -1, NULL
, 0);
69 strW
= heap_xalloc(len
* sizeof(WCHAR
));
70 MultiByteToWideChar(CP_ACP
, 0, strA
, -1, strW
, len
);
76 /******************************************************************************
77 * Allocates memory and converts input from multibyte to wide chars
78 * Returned string must be freed by the caller
80 static WCHAR
* GetWideStringN(const char* strA
, int chars
, DWORD
*len
)
85 *len
= MultiByteToWideChar(CP_ACP
, 0, strA
, chars
, NULL
, 0);
87 strW
= heap_xalloc(*len
* sizeof(WCHAR
));
88 MultiByteToWideChar(CP_ACP
, 0, strA
, chars
, strW
, *len
);
95 /******************************************************************************
96 * Allocates memory and converts input from wide chars to multibyte
97 * Returned string must be freed by the caller
99 char* GetMultiByteString(const WCHAR
* strW
)
104 int len
= WideCharToMultiByte(CP_ACP
, 0, strW
, -1, NULL
, 0, NULL
, NULL
);
106 strA
= heap_xalloc(len
);
107 WideCharToMultiByte(CP_ACP
, 0, strW
, -1, strA
, len
, NULL
, NULL
);
113 /******************************************************************************
114 * Allocates memory and converts input from wide chars to multibyte
115 * Returned string must be freed by the caller
117 static char* GetMultiByteStringN(const WCHAR
* strW
, int chars
, DWORD
* len
)
122 *len
= WideCharToMultiByte(CP_ACP
, 0, strW
, chars
, NULL
, 0, NULL
, NULL
);
124 strA
= heap_xalloc(*len
);
125 WideCharToMultiByte(CP_ACP
, 0, strW
, chars
, strA
, *len
, NULL
, NULL
);
132 static WCHAR
*(*get_line
)(FILE *);
134 /* parser definitions */
137 HEADER
, /* parsing the registry file version header */
138 PARSE_WIN31_LINE
, /* parsing a Windows 3.1 registry line */
139 LINE_START
, /* at the beginning of a registry line */
140 KEY_NAME
, /* parsing a key name */
141 DELETE_KEY
, /* deleting a registry key */
142 DEFAULT_VALUE_NAME
, /* parsing a default value name */
143 QUOTED_VALUE_NAME
, /* parsing a double-quoted value name */
144 DATA_START
, /* preparing for data parsing operations */
145 DELETE_VALUE
, /* deleting a registry value */
146 DATA_TYPE
, /* parsing the registry data type */
147 STRING_DATA
, /* parsing REG_SZ data */
148 DWORD_DATA
, /* parsing DWORD data */
149 HEX_DATA
, /* parsing REG_BINARY, REG_NONE, REG_EXPAND_SZ or REG_MULTI_SZ data */
150 EOL_BACKSLASH
, /* preparing to parse multiple lines of hex data */
151 HEX_MULTILINE
, /* parsing multiple lines of hex data */
152 UNKNOWN_DATA
, /* parsing an unhandled or invalid data type */
153 SET_VALUE
, /* adding a value to the registry */
159 FILE *file
; /* pointer to a registry file */
160 WCHAR two_wchars
[2]; /* first two characters from the encoding check */
161 BOOL is_unicode
; /* parsing Unicode or ASCII data */
162 short int reg_version
; /* registry file version */
163 HKEY hkey
; /* current registry key */
164 WCHAR
*key_name
; /* current key name */
165 WCHAR
*value_name
; /* value name */
166 DWORD parse_type
; /* generic data type for parsing */
167 DWORD data_type
; /* data type */
168 void *data
; /* value data */
169 DWORD data_size
; /* size of the data (in bytes) */
170 BOOL backslash
; /* TRUE if the current line contains a backslash */
171 enum parser_state state
; /* current parser state */
174 typedef WCHAR
*(*parser_state_func
)(struct parser
*parser
, WCHAR
*pos
);
176 /* parser state machine functions */
177 static WCHAR
*header_state(struct parser
*parser
, WCHAR
*pos
);
178 static WCHAR
*parse_win31_line_state(struct parser
*parser
, WCHAR
*pos
);
179 static WCHAR
*line_start_state(struct parser
*parser
, WCHAR
*pos
);
180 static WCHAR
*key_name_state(struct parser
*parser
, WCHAR
*pos
);
181 static WCHAR
*delete_key_state(struct parser
*parser
, WCHAR
*pos
);
182 static WCHAR
*default_value_name_state(struct parser
*parser
, WCHAR
*pos
);
183 static WCHAR
*quoted_value_name_state(struct parser
*parser
, WCHAR
*pos
);
184 static WCHAR
*data_start_state(struct parser
*parser
, WCHAR
*pos
);
185 static WCHAR
*delete_value_state(struct parser
*parser
, WCHAR
*pos
);
186 static WCHAR
*data_type_state(struct parser
*parser
, WCHAR
*pos
);
187 static WCHAR
*string_data_state(struct parser
*parser
, WCHAR
*pos
);
188 static WCHAR
*dword_data_state(struct parser
*parser
, WCHAR
*pos
);
189 static WCHAR
*hex_data_state(struct parser
*parser
, WCHAR
*pos
);
190 static WCHAR
*eol_backslash_state(struct parser
*parser
, WCHAR
*pos
);
191 static WCHAR
*hex_multiline_state(struct parser
*parser
, WCHAR
*pos
);
192 static WCHAR
*unknown_data_state(struct parser
*parser
, WCHAR
*pos
);
193 static WCHAR
*set_value_state(struct parser
*parser
, WCHAR
*pos
);
195 static const parser_state_func parser_funcs
[NB_PARSER_STATES
] =
197 header_state
, /* HEADER */
198 parse_win31_line_state
, /* PARSE_WIN31_LINE */
199 line_start_state
, /* LINE_START */
200 key_name_state
, /* KEY_NAME */
201 delete_key_state
, /* DELETE_KEY */
202 default_value_name_state
, /* DEFAULT_VALUE_NAME */
203 quoted_value_name_state
, /* QUOTED_VALUE_NAME */
204 data_start_state
, /* DATA_START */
205 delete_value_state
, /* DELETE_VALUE */
206 data_type_state
, /* DATA_TYPE */
207 string_data_state
, /* STRING_DATA */
208 dword_data_state
, /* DWORD_DATA */
209 hex_data_state
, /* HEX_DATA */
210 eol_backslash_state
, /* EOL_BACKSLASH */
211 hex_multiline_state
, /* HEX_MULTILINE */
212 unknown_data_state
, /* UNKNOWN_DATA */
213 set_value_state
, /* SET_VALUE */
216 /* set the new parser state and return the previous one */
217 static inline enum parser_state
set_state(struct parser
*parser
, enum parser_state state
)
219 enum parser_state ret
= parser
->state
;
220 parser
->state
= state
;
224 static void *resize_buffer(void *buf
, size_t count
)
229 new_buf
= HeapReAlloc(GetProcessHeap(), 0, buf
, count
);
231 new_buf
= HeapAlloc(GetProcessHeap(), 0, count
);
233 CHECK_ENOUGH_MEMORY(new_buf
);
237 /******************************************************************************
238 * Converts a hex representation of a DWORD into a DWORD.
240 static BOOL
convert_hex_to_dword(WCHAR
*str
, DWORD
*dw
)
245 while (*str
== ' ' || *str
== '\t') str
++;
246 if (!*str
) goto error
;
249 while (isxdigitW(*p
))
254 if (count
> 8) goto error
;
257 while (*p
== ' ' || *p
== '\t') p
++;
258 if (*p
&& *p
!= ';') goto error
;
261 *dw
= strtoulW(str
, &end
, 16);
268 /******************************************************************************
269 * Converts comma-separated hex data into a binary string and modifies
270 * the input parameter to skip the concatenating backslash, if found.
272 * Returns TRUE or FALSE to indicate whether parsing was successful.
274 static BOOL
convert_hex_csv_to_hex(struct parser
*parser
, WCHAR
**str
)
280 parser
->backslash
= FALSE
;
282 /* The worst case is 1 digit + 1 comma per byte */
283 size
= ((lstrlenW(*str
) + 1) / 2) + parser
->data_size
;
284 parser
->data
= resize_buffer(parser
->data
, size
);
287 d
= (BYTE
*)parser
->data
+ parser
->data_size
;
294 wc
= strtoulW(s
, &end
, 16);
295 if (wc
> 0xff) return FALSE
;
297 if (s
== end
&& wc
== 0)
299 while (*end
== ' ' || *end
== '\t') end
++;
302 parser
->backslash
= TRUE
;
306 else if (*end
== ';')
314 if (*end
&& *end
!= ',')
316 while (*end
== ' ' || *end
== '\t') end
++;
317 if (*end
&& *end
!= ';') return FALSE
;
328 /******************************************************************************
329 * Parses the data type of the registry value being imported and modifies
330 * the input parameter to skip the string representation of the data type.
332 * Returns TRUE or FALSE to indicate whether a data type was found.
334 static BOOL
parse_data_type(struct parser
*parser
, WCHAR
**line
)
336 struct data_type
{ const WCHAR
*tag
; int len
; int type
; int parse_type
; };
338 static const WCHAR quote
[] = {'"'};
339 static const WCHAR hex
[] = {'h','e','x',':'};
340 static const WCHAR dword
[] = {'d','w','o','r','d',':'};
341 static const WCHAR hexp
[] = {'h','e','x','('};
343 static const struct data_type data_types
[] = {
344 /* tag len type parse type */
345 { quote
, 1, REG_SZ
, REG_SZ
},
346 { hex
, 4, REG_BINARY
, REG_BINARY
},
347 { dword
, 6, REG_DWORD
, REG_DWORD
},
348 { hexp
, 4, -1, REG_BINARY
}, /* REG_NONE, REG_EXPAND_SZ, REG_MULTI_SZ */
352 const struct data_type
*ptr
;
354 for (ptr
= data_types
; ptr
->tag
; ptr
++)
356 if (strncmpW(ptr
->tag
, *line
, ptr
->len
))
359 parser
->parse_type
= ptr
->parse_type
;
360 parser
->data_type
= ptr
->parse_type
;
368 if (!**line
|| tolowerW((*line
)[1]) == 'x')
371 /* "hex(xx):" is special */
372 val
= wcstoul(*line
, &end
, 16);
373 if (*end
!= ')' || *(end
+ 1) != ':' || (val
== ~0u && errno
== ERANGE
))
376 parser
->data_type
= val
;
384 /******************************************************************************
385 * Replaces escape sequences with their character equivalents and
386 * null-terminates the string on the first non-escaped double quote.
388 * Assigns a pointer to the remaining unparsed data in the line.
389 * Returns TRUE or FALSE to indicate whether a closing double quote was found.
391 static BOOL
REGPROC_unescape_string(WCHAR
*str
, WCHAR
**unparsed
)
393 int str_idx
= 0; /* current character under analysis */
394 int val_idx
= 0; /* the last character of the unescaped string */
395 int len
= lstrlenW(str
);
398 for (str_idx
= 0; str_idx
< len
; str_idx
++, val_idx
++) {
399 if (str
[str_idx
] == '\\') {
401 switch (str
[str_idx
]) {
413 str
[val_idx
] = str
[str_idx
];
416 if (!str
[str_idx
]) return FALSE
;
417 output_message(STRING_ESCAPE_SEQUENCE
, str
[str_idx
]);
418 str
[val_idx
] = str
[str_idx
];
421 } else if (str
[str_idx
] == '"') {
424 str
[val_idx
] = str
[str_idx
];
428 ret
= (str
[str_idx
] == '"');
429 *unparsed
= str
+ str_idx
+ 1;
434 static HKEY
parse_key_name(WCHAR
*key_name
, WCHAR
**key_path
)
438 if (!key_name
) return 0;
440 *key_path
= strchrW(key_name
, '\\');
441 if (*key_path
) (*key_path
)++;
443 for (i
= 0; i
< ARRAY_SIZE(reg_class_keys
); i
++)
445 int len
= lstrlenW(reg_class_namesW
[i
]);
446 if (!strncmpiW(key_name
, reg_class_namesW
[i
], len
) &&
447 (key_name
[len
] == 0 || key_name
[len
] == '\\'))
449 return reg_class_keys
[i
];
456 static void close_key(struct parser
*parser
)
460 HeapFree(GetProcessHeap(), 0, parser
->key_name
);
461 parser
->key_name
= NULL
;
463 RegCloseKey(parser
->hkey
);
468 /******************************************************************************
469 * Opens the registry key given by the input path.
470 * This key must be closed by calling close_key().
472 static LONG
open_key(struct parser
*parser
, WCHAR
*path
)
480 /* Get the registry class */
481 if (!path
|| !(key_class
= parse_key_name(path
, &key_path
)))
482 return ERROR_INVALID_PARAMETER
;
484 res
= RegCreateKeyExW(key_class
, key_path
, 0, NULL
, REG_OPTION_NON_VOLATILE
,
485 KEY_ALL_ACCESS
, NULL
, &parser
->hkey
, NULL
);
487 if (res
== ERROR_SUCCESS
)
489 parser
->key_name
= heap_xalloc((lstrlenW(path
) + 1) * sizeof(WCHAR
));
490 lstrcpyW(parser
->key_name
, path
);
498 static void free_parser_data(struct parser
*parser
)
500 if (parser
->parse_type
== REG_DWORD
|| parser
->parse_type
== REG_BINARY
)
501 HeapFree(GetProcessHeap(), 0, parser
->data
);
504 parser
->data_size
= 0;
507 static void prepare_hex_string_data(struct parser
*parser
)
509 if (parser
->data_type
== REG_EXPAND_SZ
|| parser
->data_type
== REG_MULTI_SZ
)
511 BYTE
*data
= parser
->data
;
513 if (data
[parser
->data_size
- 1] != 0)
515 data
[parser
->data_size
] = 0;
519 if (!parser
->is_unicode
)
521 parser
->data
= GetWideStringN(parser
->data
, parser
->data_size
, &parser
->data_size
);
522 parser
->data_size
*= sizeof(WCHAR
);
523 HeapFree(GetProcessHeap(), 0, data
);
536 static enum reg_versions
parse_file_header(const WCHAR
*s
)
538 static const WCHAR header_31
[] = {'R','E','G','E','D','I','T',0};
539 static const WCHAR header_40
[] = {'R','E','G','E','D','I','T','4',0};
540 static const WCHAR header_50
[] = {'W','i','n','d','o','w','s',' ',
541 'R','e','g','i','s','t','r','y',' ','E','d','i','t','o','r',' ',
542 'V','e','r','s','i','o','n',' ','5','.','0','0',0};
544 while (*s
== ' ' || *s
== '\t') s
++;
546 if (!strcmpW(s
, header_31
))
547 return REG_VERSION_31
;
549 if (!strcmpW(s
, header_40
))
550 return REG_VERSION_40
;
552 if (!strcmpW(s
, header_50
))
553 return REG_VERSION_50
;
555 /* The Windows version accepts registry file headers beginning with "REGEDIT" and ending
556 * with other characters, as long as "REGEDIT" appears at the start of the line. For example,
557 * "REGEDIT 4", "REGEDIT9" and "REGEDIT4FOO" are all treated as valid file headers.
558 * In all such cases, however, the contents of the registry file are not imported.
560 if (!strncmpW(s
, header_31
, 7)) /* "REGEDIT" without NUL */
561 return REG_VERSION_FUZZY
;
563 return REG_VERSION_INVALID
;
566 /* handler for parser HEADER state */
567 static WCHAR
*header_state(struct parser
*parser
, WCHAR
*pos
)
569 WCHAR
*line
, *header
;
571 if (!(line
= get_line(parser
->file
)))
574 if (!parser
->is_unicode
)
576 header
= heap_xalloc((lstrlenW(line
) + 3) * sizeof(WCHAR
));
577 header
[0] = parser
->two_wchars
[0];
578 header
[1] = parser
->two_wchars
[1];
579 lstrcpyW(header
+ 2, line
);
580 parser
->reg_version
= parse_file_header(header
);
581 HeapFree(GetProcessHeap(), 0, header
);
583 else parser
->reg_version
= parse_file_header(line
);
585 switch (parser
->reg_version
)
588 set_state(parser
, PARSE_WIN31_LINE
);
592 set_state(parser
, LINE_START
);
595 get_line(NULL
); /* Reset static variables */
602 /* handler for parser PARSE_WIN31_LINE state */
603 static WCHAR
*parse_win31_line_state(struct parser
*parser
, WCHAR
*pos
)
606 static WCHAR hkcr
[] = {'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T'};
607 unsigned int key_end
= 0;
609 if (!(line
= get_line(parser
->file
)))
612 if (strncmpW(line
, hkcr
, ARRAY_SIZE(hkcr
)))
616 while (line
[key_end
] && !isspaceW(line
[key_end
])) key_end
++;
618 value
= line
+ key_end
;
619 while (*value
== ' ' || *value
== '\t') value
++;
621 if (*value
== '=') value
++;
622 if (*value
== ' ') value
++; /* at most one space is skipped */
626 if (open_key(parser
, line
) != ERROR_SUCCESS
)
628 output_message(STRING_OPEN_KEY_FAILED
, line
);
632 parser
->value_name
= NULL
;
633 parser
->data_type
= REG_SZ
;
634 parser
->data
= value
;
635 parser
->data_size
= (lstrlenW(value
) + 1) * sizeof(WCHAR
);
637 set_state(parser
, SET_VALUE
);
641 /* handler for parser LINE_START state */
642 static WCHAR
*line_start_state(struct parser
*parser
, WCHAR
*pos
)
646 if (!(line
= get_line(parser
->file
)))
649 for (p
= line
; *p
; p
++)
654 set_state(parser
, KEY_NAME
);
657 set_state(parser
, DEFAULT_VALUE_NAME
);
660 set_state(parser
, QUOTED_VALUE_NAME
);
673 /* handler for parser KEY_NAME state */
674 static WCHAR
*key_name_state(struct parser
*parser
, WCHAR
*pos
)
676 WCHAR
*p
= pos
, *key_end
;
678 if (*p
== ' ' || *p
== '\t' || !(key_end
= strrchrW(p
, ']')))
685 set_state(parser
, DELETE_KEY
);
688 else if (open_key(parser
, p
) != ERROR_SUCCESS
)
689 output_message(STRING_OPEN_KEY_FAILED
, p
);
692 set_state(parser
, LINE_START
);
696 /* handler for parser DELETE_KEY state */
697 static WCHAR
*delete_key_state(struct parser
*parser
, WCHAR
*pos
)
701 if (*p
== 'H' || *p
== 'h')
702 delete_registry_key(p
);
704 set_state(parser
, LINE_START
);
708 /* handler for parser DEFAULT_VALUE_NAME state */
709 static WCHAR
*default_value_name_state(struct parser
*parser
, WCHAR
*pos
)
711 HeapFree(GetProcessHeap(), 0, parser
->value_name
);
712 parser
->value_name
= NULL
;
714 set_state(parser
, DATA_START
);
718 /* handler for parser QUOTED_VALUE_NAME state */
719 static WCHAR
*quoted_value_name_state(struct parser
*parser
, WCHAR
*pos
)
721 WCHAR
*val_name
= pos
, *p
;
723 if (parser
->value_name
)
725 HeapFree(GetProcessHeap(), 0, parser
->value_name
);
726 parser
->value_name
= NULL
;
729 if (!REGPROC_unescape_string(val_name
, &p
))
732 /* copy the value name in case we need to parse multiple lines and the buffer is overwritten */
733 parser
->value_name
= heap_xalloc((lstrlenW(val_name
) + 1) * sizeof(WCHAR
));
734 lstrcpyW(parser
->value_name
, val_name
);
736 set_state(parser
, DATA_START
);
740 set_state(parser
, LINE_START
);
744 /* handler for parser DATA_START state */
745 static WCHAR
*data_start_state(struct parser
*parser
, WCHAR
*pos
)
750 while (*p
== ' ' || *p
== '\t') p
++;
751 if (*p
!= '=') goto done
;
753 while (*p
== ' ' || *p
== '\t') p
++;
755 /* trim trailing whitespace */
757 while (len
> 0 && (p
[len
- 1] == ' ' || p
[len
- 1] == '\t')) len
--;
761 set_state(parser
, DELETE_VALUE
);
763 set_state(parser
, DATA_TYPE
);
767 set_state(parser
, LINE_START
);
771 /* handler for parser DELETE_VALUE state */
772 static WCHAR
*delete_value_state(struct parser
*parser
, WCHAR
*pos
)
776 while (*p
== ' ' || *p
== '\t') p
++;
777 if (*p
&& *p
!= ';') goto done
;
779 RegDeleteValueW(parser
->hkey
, parser
->value_name
);
782 set_state(parser
, LINE_START
);
786 /* handler for parser DATA_TYPE state */
787 static WCHAR
*data_type_state(struct parser
*parser
, WCHAR
*pos
)
791 if (!parse_data_type(parser
, &line
))
793 set_state(parser
, LINE_START
);
797 switch (parser
->parse_type
)
800 set_state(parser
, STRING_DATA
);
803 set_state(parser
, DWORD_DATA
);
805 case REG_BINARY
: /* all hex data types, including undefined */
806 set_state(parser
, HEX_DATA
);
809 set_state(parser
, UNKNOWN_DATA
);
815 /* handler for parser STRING_DATA state */
816 static WCHAR
*string_data_state(struct parser
*parser
, WCHAR
*pos
)
822 if (!REGPROC_unescape_string(parser
->data
, &line
))
825 while (*line
== ' ' || *line
== '\t') line
++;
826 if (*line
&& *line
!= ';') goto invalid
;
828 parser
->data_size
= (lstrlenW(parser
->data
) + 1) * sizeof(WCHAR
);
830 set_state(parser
, SET_VALUE
);
834 free_parser_data(parser
);
835 set_state(parser
, LINE_START
);
839 /* handler for parser DWORD_DATA state */
840 static WCHAR
*dword_data_state(struct parser
*parser
, WCHAR
*pos
)
844 parser
->data
= heap_xalloc(sizeof(DWORD
));
846 if (!convert_hex_to_dword(line
, parser
->data
))
849 parser
->data_size
= sizeof(DWORD
);
851 set_state(parser
, SET_VALUE
);
855 free_parser_data(parser
);
856 set_state(parser
, LINE_START
);
860 /* handler for parser HEX_DATA state */
861 static WCHAR
*hex_data_state(struct parser
*parser
, WCHAR
*pos
)
865 if (!convert_hex_csv_to_hex(parser
, &line
))
868 if (parser
->backslash
)
870 set_state(parser
, EOL_BACKSLASH
);
874 prepare_hex_string_data(parser
);
876 set_state(parser
, SET_VALUE
);
880 free_parser_data(parser
);
881 set_state(parser
, LINE_START
);
885 /* handler for parser EOL_BACKSLASH state */
886 static WCHAR
*eol_backslash_state(struct parser
*parser
, WCHAR
*pos
)
890 while (*p
== ' ' || *p
== '\t') p
++;
891 if (*p
&& *p
!= ';') goto invalid
;
893 set_state(parser
, HEX_MULTILINE
);
897 free_parser_data(parser
);
898 set_state(parser
, LINE_START
);
902 /* handler for parser HEX_MULTILINE state */
903 static WCHAR
*hex_multiline_state(struct parser
*parser
, WCHAR
*pos
)
907 if (!(line
= get_line(parser
->file
)))
909 prepare_hex_string_data(parser
);
910 set_state(parser
, SET_VALUE
);
914 while (*line
== ' ' || *line
== '\t') line
++;
915 if (!*line
|| *line
== ';') return line
;
917 if (!isxdigitW(*line
)) goto invalid
;
919 set_state(parser
, HEX_DATA
);
923 free_parser_data(parser
);
924 set_state(parser
, LINE_START
);
928 /* handler for parser UNKNOWN_DATA state */
929 static WCHAR
*unknown_data_state(struct parser
*parser
, WCHAR
*pos
)
931 output_message(STRING_UNKNOWN_DATA_FORMAT
, parser
->data_type
);
933 set_state(parser
, LINE_START
);
937 /* handler for parser SET_VALUE state */
938 static WCHAR
*set_value_state(struct parser
*parser
, WCHAR
*pos
)
940 RegSetValueExW(parser
->hkey
, parser
->value_name
, 0, parser
->data_type
,
941 parser
->data
, parser
->data_size
);
943 free_parser_data(parser
);
945 if (parser
->reg_version
== REG_VERSION_31
)
946 set_state(parser
, PARSE_WIN31_LINE
);
948 set_state(parser
, LINE_START
);
953 static WCHAR
*get_lineA(FILE *fp
)
957 static char *buf
, *next
;
960 HeapFree(GetProcessHeap(), 0, lineW
);
962 if (!fp
) goto cleanup
;
966 size
= REG_VAL_BUF_SIZE
;
967 buf
= heap_xalloc(size
);
975 char *p
= strpbrk(line
, "\r\n");
980 memmove(buf
, next
, len
+ 1);
983 char *new_buf
= HeapReAlloc(GetProcessHeap(), 0, buf
, size
* 2);
984 CHECK_ENOUGH_MEMORY(new_buf
);
988 if (!(count
= fread(buf
+ len
, 1, size
- len
- 1, fp
)))
991 lineW
= GetWideString(buf
);
994 buf
[len
+ count
] = 0;
1000 if (*p
== '\r' && *(p
+ 1) == '\n') next
++;
1002 lineW
= GetWideString(line
);
1008 if (size
) HeapFree(GetProcessHeap(), 0, buf
);
1013 static WCHAR
*get_lineW(FILE *fp
)
1016 static WCHAR
*buf
, *next
;
1019 if (!fp
) goto cleanup
;
1023 size
= REG_VAL_BUF_SIZE
;
1024 buf
= heap_xalloc(size
* sizeof(WCHAR
));
1032 static const WCHAR line_endings
[] = {'\r','\n',0};
1033 WCHAR
*p
= strpbrkW(line
, line_endings
);
1037 len
= strlenW(next
);
1038 memmove(buf
, next
, (len
+ 1) * sizeof(WCHAR
));
1041 WCHAR
*new_buf
= HeapReAlloc(GetProcessHeap(), 0, buf
, (size
* 2) * sizeof(WCHAR
));
1042 CHECK_ENOUGH_MEMORY(new_buf
);
1046 if (!(count
= fread(buf
+ len
, sizeof(WCHAR
), size
- len
- 1, fp
)))
1051 buf
[len
+ count
] = 0;
1057 if (*p
== '\r' && *(p
+ 1) == '\n') next
++;
1063 if (size
) HeapFree(GetProcessHeap(), 0, buf
);
1068 /******************************************************************************
1069 * Reads contents of the specified file into the registry.
1071 BOOL
import_registry_file(FILE *reg_file
)
1074 struct parser parser
;
1077 if (!reg_file
|| (fread(s
, 2, 1, reg_file
) != 1))
1080 parser
.is_unicode
= (s
[0] == 0xff && s
[1] == 0xfe);
1081 get_line
= parser
.is_unicode
? get_lineW
: get_lineA
;
1083 parser
.file
= reg_file
;
1084 parser
.two_wchars
[0] = s
[0];
1085 parser
.two_wchars
[1] = s
[1];
1086 parser
.reg_version
= -1;
1088 parser
.key_name
= NULL
;
1089 parser
.value_name
= NULL
;
1090 parser
.parse_type
= 0;
1091 parser
.data_type
= 0;
1093 parser
.data_size
= 0;
1094 parser
.backslash
= FALSE
;
1095 parser
.state
= HEADER
;
1097 pos
= parser
.two_wchars
;
1099 /* parser main loop */
1101 pos
= (parser_funcs
[parser
.state
])(&parser
, pos
);
1103 if (parser
.reg_version
== REG_VERSION_FUZZY
|| parser
.reg_version
== REG_VERSION_INVALID
)
1104 return parser
.reg_version
== REG_VERSION_FUZZY
;
1106 HeapFree(GetProcessHeap(), 0, parser
.value_name
);
1112 /******************************************************************************
1113 * Removes the registry key with all subkeys. Parses full key name.
1116 * reg_key_name - full name of registry branch to delete. Ignored if is NULL,
1117 * empty, points to register key class, does not exist.
1119 void delete_registry_key(WCHAR
*reg_key_name
)
1121 WCHAR
*key_name
= NULL
;
1124 if (!reg_key_name
|| !reg_key_name
[0])
1127 if (!(key_class
= parse_key_name(reg_key_name
, &key_name
)))
1129 if (key_name
) *(key_name
- 1) = 0;
1130 error_exit(STRING_INVALID_SYSTEM_KEY
, reg_key_name
);
1133 if (!key_name
|| !*key_name
)
1134 error_exit(STRING_DELETE_FAILED
, reg_key_name
);
1136 RegDeleteTreeW(key_class
, key_name
);
1139 static void REGPROC_write_line(FILE *fp
, const WCHAR
*str
, BOOL unicode
)
1142 fwrite(str
, sizeof(WCHAR
), lstrlenW(str
), fp
);
1145 char *strA
= GetMultiByteString(str
);
1147 HeapFree(GetProcessHeap(), 0, strA
);
1151 static WCHAR
*REGPROC_escape_string(WCHAR
*str
, size_t str_len
, size_t *line_len
)
1153 size_t i
, escape_count
, pos
;
1156 for (i
= 0, escape_count
= 0; i
< str_len
; i
++)
1159 if (c
== '\r' || c
== '\n' || c
== '\\' || c
== '"' || c
== '\0')
1163 buf
= heap_xalloc((str_len
+ escape_count
+ 1) * sizeof(WCHAR
));
1165 for (i
= 0, pos
= 0; i
< str_len
; i
++, pos
++)
1201 static size_t export_value_name(FILE *fp
, WCHAR
*name
, size_t len
, BOOL unicode
)
1203 static const WCHAR quoted_fmt
[] = {'"','%','s','"','=',0};
1204 static const WCHAR default_name
[] = {'@','=',0};
1209 WCHAR
*str
= REGPROC_escape_string(name
, len
, &line_len
);
1210 WCHAR
*buf
= heap_xalloc((line_len
+ 4) * sizeof(WCHAR
));
1211 line_len
= sprintfW(buf
, quoted_fmt
, str
);
1212 REGPROC_write_line(fp
, buf
, unicode
);
1213 HeapFree(GetProcessHeap(), 0, buf
);
1214 HeapFree(GetProcessHeap(), 0, str
);
1218 line_len
= lstrlenW(default_name
);
1219 REGPROC_write_line(fp
, default_name
, unicode
);
1225 static void export_string_data(WCHAR
**buf
, WCHAR
*data
, size_t size
)
1227 size_t len
, line_len
;
1229 static const WCHAR fmt
[] = {'"','%','s','"',0};
1231 len
= size
/ sizeof(WCHAR
) - 1;
1232 str
= REGPROC_escape_string(data
, len
, &line_len
);
1233 *buf
= heap_xalloc((line_len
+ 3) * sizeof(WCHAR
));
1234 sprintfW(*buf
, fmt
, str
);
1235 HeapFree(GetProcessHeap(), 0, str
);
1238 static void export_dword_data(WCHAR
**buf
, DWORD
*data
)
1240 static const WCHAR fmt
[] = {'d','w','o','r','d',':','%','0','8','x',0};
1242 *buf
= heap_xalloc(15 * sizeof(WCHAR
));
1243 sprintfW(*buf
, fmt
, *data
);
1246 static size_t export_hex_data_type(FILE *fp
, DWORD type
, BOOL unicode
)
1248 static const WCHAR hex
[] = {'h','e','x',':',0};
1249 static const WCHAR hexp_fmt
[] = {'h','e','x','(','%','x',')',':',0};
1252 if (type
== REG_BINARY
)
1254 line_len
= lstrlenW(hex
);
1255 REGPROC_write_line(fp
, hex
, unicode
);
1259 WCHAR
*buf
= heap_xalloc(15 * sizeof(WCHAR
));
1260 line_len
= sprintfW(buf
, hexp_fmt
, type
);
1261 REGPROC_write_line(fp
, buf
, unicode
);
1262 HeapFree(GetProcessHeap(), 0, buf
);
1268 #define MAX_HEX_CHARS 77
1270 static void export_hex_data(FILE *fp
, WCHAR
**buf
, DWORD type
, DWORD line_len
,
1271 void *data
, DWORD size
, BOOL unicode
)
1273 static const WCHAR fmt
[] = {'%','0','2','x',0};
1274 static const WCHAR hex_concat
[] = {'\\','\r','\n',' ',' ',0};
1275 size_t num_commas
, i
, pos
;
1277 line_len
+= export_hex_data_type(fp
, type
, unicode
);
1279 if (!unicode
&& (type
== REG_EXPAND_SZ
|| type
== REG_MULTI_SZ
))
1280 data
= GetMultiByteStringN(data
, size
/ sizeof(WCHAR
), &size
);
1282 num_commas
= size
- 1;
1283 *buf
= heap_xalloc(size
* 3 * sizeof(WCHAR
));
1285 for (i
= 0, pos
= 0; i
< size
; i
++)
1287 pos
+= sprintfW(*buf
+ pos
, fmt
, ((BYTE
*)data
)[i
]);
1288 if (i
== num_commas
) break;
1289 (*buf
)[pos
++] = ',';
1293 if (line_len
>= MAX_HEX_CHARS
)
1295 REGPROC_write_line(fp
, *buf
, unicode
);
1296 REGPROC_write_line(fp
, hex_concat
, unicode
);
1303 static void export_newline(FILE *fp
, BOOL unicode
)
1305 static const WCHAR newline
[] = {'\r','\n',0};
1307 REGPROC_write_line(fp
, newline
, unicode
);
1310 static void export_data(FILE *fp
, WCHAR
*value_name
, DWORD value_len
, DWORD type
,
1311 void *data
, size_t size
, BOOL unicode
)
1314 size_t line_len
= export_value_name(fp
, value_name
, value_len
, unicode
);
1319 export_string_data(&buf
, data
, size
);
1322 export_dword_data(&buf
, data
);
1329 export_hex_data(fp
, &buf
, type
, line_len
, data
, size
, unicode
);
1333 REGPROC_write_line(fp
, buf
, unicode
);
1334 HeapFree(GetProcessHeap(), 0, buf
);
1335 export_newline(fp
, unicode
);
1338 static WCHAR
*build_subkey_path(WCHAR
*path
, DWORD path_len
, WCHAR
*subkey_name
, DWORD subkey_len
)
1341 static const WCHAR fmt
[] = {'%','s','\\','%','s',0};
1343 subkey_path
= heap_xalloc((path_len
+ subkey_len
+ 2) * sizeof(WCHAR
));
1344 sprintfW(subkey_path
, fmt
, path
, subkey_name
);
1349 static void export_key_name(FILE *fp
, WCHAR
*name
, BOOL unicode
)
1351 static const WCHAR fmt
[] = {'\r','\n','[','%','s',']','\r','\n',0};
1354 buf
= heap_xalloc((lstrlenW(name
) + 7) * sizeof(WCHAR
));
1355 sprintfW(buf
, fmt
, name
);
1356 REGPROC_write_line(fp
, buf
, unicode
);
1357 HeapFree(GetProcessHeap(), 0, buf
);
1360 #define MAX_SUBKEY_LEN 257
1362 static int export_registry_data(FILE *fp
, HKEY key
, WCHAR
*path
, BOOL unicode
)
1365 DWORD max_value_len
= 256, value_len
;
1366 DWORD max_data_bytes
= 2048, data_size
;
1368 DWORD i
, type
, path_len
;
1369 WCHAR
*value_name
, *subkey_name
, *subkey_path
;
1373 export_key_name(fp
, path
, unicode
);
1375 value_name
= heap_xalloc(max_value_len
* sizeof(WCHAR
));
1376 data
= heap_xalloc(max_data_bytes
);
1381 value_len
= max_value_len
;
1382 data_size
= max_data_bytes
;
1383 rc
= RegEnumValueW(key
, i
, value_name
, &value_len
, NULL
, &type
, data
, &data_size
);
1384 if (rc
== ERROR_SUCCESS
)
1386 export_data(fp
, value_name
, value_len
, type
, data
, data_size
, unicode
);
1389 else if (rc
== ERROR_MORE_DATA
)
1391 if (data_size
> max_data_bytes
)
1393 max_data_bytes
= data_size
;
1394 data
= resize_buffer(data
, max_data_bytes
);
1399 value_name
= resize_buffer(value_name
, max_value_len
* sizeof(WCHAR
));
1405 HeapFree(GetProcessHeap(), 0, data
);
1406 HeapFree(GetProcessHeap(), 0, value_name
);
1408 subkey_name
= heap_xalloc(MAX_SUBKEY_LEN
* sizeof(WCHAR
));
1410 path_len
= lstrlenW(path
);
1415 subkey_len
= MAX_SUBKEY_LEN
;
1416 rc
= RegEnumKeyExW(key
, i
, subkey_name
, &subkey_len
, NULL
, NULL
, NULL
, NULL
);
1417 if (rc
== ERROR_SUCCESS
)
1419 subkey_path
= build_subkey_path(path
, path_len
, subkey_name
, subkey_len
);
1420 if (!RegOpenKeyExW(key
, subkey_name
, 0, KEY_READ
, &subkey
))
1422 export_registry_data(fp
, subkey
, subkey_path
, unicode
);
1423 RegCloseKey(subkey
);
1425 HeapFree(GetProcessHeap(), 0, subkey_path
);
1431 HeapFree(GetProcessHeap(), 0, subkey_name
);
1435 static FILE *REGPROC_open_export_file(WCHAR
*file_name
, BOOL unicode
)
1438 static const WCHAR hyphen
[] = {'-',0};
1440 if (!strcmpW(file_name
, hyphen
))
1443 _setmode(_fileno(file
), _O_BINARY
);
1447 static const WCHAR wb_mode
[] = {'w','b',0};
1449 file
= _wfopen(file_name
, wb_mode
);
1452 static const WCHAR regedit
[] = {'r','e','g','e','d','i','t',0};
1454 error_exit(STRING_CANNOT_OPEN_FILE
, file_name
);
1460 static const BYTE bom
[] = {0xff,0xfe};
1461 static const WCHAR header
[] = {'W','i','n','d','o','w','s',' ',
1462 'R','e','g','i','s','t','r','y',' ','E','d','i','t','o','r',' ',
1463 'V','e','r','s','i','o','n',' ','5','.','0','0','\r','\n'};
1465 fwrite(bom
, sizeof(BYTE
), ARRAY_SIZE(bom
), file
);
1466 fwrite(header
, sizeof(WCHAR
), ARRAY_SIZE(header
), file
);
1469 fputs("REGEDIT4\r\n", file
);
1474 static HKEY
open_export_key(HKEY key_class
, WCHAR
*subkey
, WCHAR
*path
)
1478 if (!RegOpenKeyExW(key_class
, subkey
, 0, KEY_READ
, &key
))
1481 output_message(STRING_OPEN_KEY_FAILED
, path
);
1485 static BOOL
export_key(WCHAR
*file_name
, WCHAR
*path
, BOOL unicode
)
1487 HKEY key_class
, key
;
1492 if (!(key_class
= parse_key_name(path
, &subkey
)))
1494 if (subkey
) *(subkey
- 1) = 0;
1495 output_message(STRING_INVALID_SYSTEM_KEY
, path
);
1499 if (!(key
= open_export_key(key_class
, subkey
, path
)))
1502 fp
= REGPROC_open_export_file(file_name
, unicode
);
1503 ret
= export_registry_data(fp
, key
, path
, unicode
);
1504 export_newline(fp
, unicode
);
1511 static BOOL
export_all(WCHAR
*file_name
, WCHAR
*path
, BOOL unicode
)
1515 HKEY classes
[] = {HKEY_LOCAL_MACHINE
, HKEY_USERS
}, key
;
1518 fp
= REGPROC_open_export_file(file_name
, unicode
);
1520 for (i
= 0; i
< ARRAY_SIZE(classes
); i
++)
1522 if (!(key
= open_export_key(classes
[i
], NULL
, path
)))
1528 class_name
= heap_xalloc((lstrlenW(reg_class_namesW
[i
]) + 1) * sizeof(WCHAR
));
1529 lstrcpyW(class_name
, reg_class_namesW
[i
]);
1531 export_registry_data(fp
, classes
[i
], class_name
, unicode
);
1533 HeapFree(GetProcessHeap(), 0, class_name
);
1537 export_newline(fp
, unicode
);
1543 BOOL
export_registry_key(WCHAR
*file_name
, WCHAR
*path
, DWORD format
)
1545 BOOL unicode
= (format
== REG_FORMAT_5
);
1548 return export_key(file_name
, path
, unicode
);
1550 return export_all(file_name
, path
, unicode
);