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
34 #include <wine/unicode.h>
35 #include <wine/debug.h>
38 #define REG_VAL_BUF_SIZE 4096
40 /* maximal number of characters in hexadecimal data line,
41 * including the indentation, but not including the '\' character
43 #define REG_FILE_HEX_LINE_LEN (2 + 25 * 3)
45 extern const WCHAR
* reg_class_namesW
[];
47 static HKEY reg_class_keys
[] = {
48 HKEY_LOCAL_MACHINE
, HKEY_USERS
, HKEY_CLASSES_ROOT
,
49 HKEY_CURRENT_CONFIG
, HKEY_CURRENT_USER
, HKEY_DYN_DATA
52 #define ARRAY_SIZE(A) (sizeof(A)/sizeof(*A))
54 /******************************************************************************
55 * Allocates memory and converts input from multibyte to wide chars
56 * Returned string must be freed by the caller
58 static WCHAR
* GetWideString(const char* strA
)
63 int len
= MultiByteToWideChar(CP_ACP
, 0, strA
, -1, NULL
, 0);
65 strW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
66 CHECK_ENOUGH_MEMORY(strW
);
67 MultiByteToWideChar(CP_ACP
, 0, strA
, -1, strW
, len
);
73 /******************************************************************************
74 * Allocates memory and converts input from multibyte to wide chars
75 * Returned string must be freed by the caller
77 static WCHAR
* GetWideStringN(const char* strA
, int chars
, DWORD
*len
)
82 *len
= MultiByteToWideChar(CP_ACP
, 0, strA
, chars
, NULL
, 0);
84 strW
= HeapAlloc(GetProcessHeap(), 0, *len
* sizeof(WCHAR
));
85 CHECK_ENOUGH_MEMORY(strW
);
86 MultiByteToWideChar(CP_ACP
, 0, strA
, chars
, strW
, *len
);
93 /******************************************************************************
94 * Allocates memory and converts input from wide chars to multibyte
95 * Returned string must be freed by the caller
97 char* GetMultiByteString(const WCHAR
* strW
)
102 int len
= WideCharToMultiByte(CP_ACP
, 0, strW
, -1, NULL
, 0, NULL
, NULL
);
104 strA
= HeapAlloc(GetProcessHeap(), 0, len
);
105 CHECK_ENOUGH_MEMORY(strA
);
106 WideCharToMultiByte(CP_ACP
, 0, strW
, -1, strA
, len
, NULL
, NULL
);
112 /******************************************************************************
113 * Allocates memory and converts input from wide chars to multibyte
114 * Returned string must be freed by the caller
116 static char* GetMultiByteStringN(const WCHAR
* strW
, int chars
, DWORD
* len
)
121 *len
= WideCharToMultiByte(CP_ACP
, 0, strW
, chars
, NULL
, 0, NULL
, NULL
);
123 strA
= HeapAlloc(GetProcessHeap(), 0, *len
);
124 CHECK_ENOUGH_MEMORY(strA
);
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 output_message(STRING_ESCAPE_SEQUENCE
, str
[str_idx
]);
417 str
[val_idx
] = str
[str_idx
];
420 } else if (str
[str_idx
] == '"') {
423 str
[val_idx
] = str
[str_idx
];
427 ret
= (str
[str_idx
] == '"');
428 *unparsed
= str
+ str_idx
+ 1;
433 static HKEY
parse_key_name(WCHAR
*key_name
, WCHAR
**key_path
)
437 if (!key_name
) return 0;
439 *key_path
= strchrW(key_name
, '\\');
440 if (*key_path
) (*key_path
)++;
442 for (i
= 0; i
< ARRAY_SIZE(reg_class_keys
); i
++)
444 int len
= lstrlenW(reg_class_namesW
[i
]);
445 if (!strncmpiW(key_name
, reg_class_namesW
[i
], len
) &&
446 (key_name
[len
] == 0 || key_name
[len
] == '\\'))
448 return reg_class_keys
[i
];
455 static void close_key(struct parser
*parser
)
459 HeapFree(GetProcessHeap(), 0, parser
->key_name
);
460 parser
->key_name
= NULL
;
462 RegCloseKey(parser
->hkey
);
467 /******************************************************************************
468 * Opens the registry key given by the input path.
469 * This key must be closed by calling close_key().
471 static LONG
open_key(struct parser
*parser
, WCHAR
*path
)
479 /* Get the registry class */
480 if (!path
|| !(key_class
= parse_key_name(path
, &key_path
)))
481 return ERROR_INVALID_PARAMETER
;
483 res
= RegCreateKeyExW(key_class
, key_path
, 0, NULL
, REG_OPTION_NON_VOLATILE
,
484 KEY_ALL_ACCESS
, NULL
, &parser
->hkey
, NULL
);
486 if (res
== ERROR_SUCCESS
)
488 parser
->key_name
= HeapAlloc(GetProcessHeap(), 0, (lstrlenW(path
) + 1) * sizeof(WCHAR
));
489 CHECK_ENOUGH_MEMORY(parser
->key_name
);
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
= HeapAlloc(GetProcessHeap(), 0, (lstrlenW(line
) + 3) * sizeof(WCHAR
));
577 CHECK_ENOUGH_MEMORY(header
);
578 header
[0] = parser
->two_wchars
[0];
579 header
[1] = parser
->two_wchars
[1];
580 lstrcpyW(header
+ 2, line
);
581 parser
->reg_version
= parse_file_header(header
);
582 HeapFree(GetProcessHeap(), 0, header
);
584 else parser
->reg_version
= parse_file_header(line
);
586 switch (parser
->reg_version
)
589 set_state(parser
, PARSE_WIN31_LINE
);
593 set_state(parser
, LINE_START
);
596 get_line(NULL
); /* Reset static variables */
603 /* handler for parser PARSE_WIN31_LINE state */
604 static WCHAR
*parse_win31_line_state(struct parser
*parser
, WCHAR
*pos
)
607 static WCHAR hkcr
[] = {'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T'};
608 unsigned int key_end
= 0;
610 if (!(line
= get_line(parser
->file
)))
613 if (strncmpW(line
, hkcr
, ARRAY_SIZE(hkcr
)))
617 while (line
[key_end
] && !isspaceW(line
[key_end
])) key_end
++;
619 value
= line
+ key_end
;
620 while (*value
== ' ' || *value
== '\t') value
++;
622 if (*value
== '=') value
++;
623 if (*value
== ' ') value
++; /* at most one space is skipped */
627 if (open_key(parser
, line
) != ERROR_SUCCESS
)
629 output_message(STRING_OPEN_KEY_FAILED
, line
);
633 parser
->value_name
= NULL
;
634 parser
->data_type
= REG_SZ
;
635 parser
->data
= value
;
636 parser
->data_size
= (lstrlenW(value
) + 1) * sizeof(WCHAR
);
638 set_state(parser
, SET_VALUE
);
642 /* handler for parser LINE_START state */
643 static WCHAR
*line_start_state(struct parser
*parser
, WCHAR
*pos
)
647 if (!(line
= get_line(parser
->file
)))
650 for (p
= line
; *p
; p
++)
655 set_state(parser
, KEY_NAME
);
658 set_state(parser
, DEFAULT_VALUE_NAME
);
661 set_state(parser
, QUOTED_VALUE_NAME
);
674 /* handler for parser KEY_NAME state */
675 static WCHAR
*key_name_state(struct parser
*parser
, WCHAR
*pos
)
677 WCHAR
*p
= pos
, *key_end
;
679 if (*p
== ' ' || *p
== '\t' || !(key_end
= strrchrW(p
, ']')))
686 set_state(parser
, DELETE_KEY
);
689 else if (open_key(parser
, p
) != ERROR_SUCCESS
)
690 output_message(STRING_OPEN_KEY_FAILED
, p
);
693 set_state(parser
, LINE_START
);
697 /* handler for parser DELETE_KEY state */
698 static WCHAR
*delete_key_state(struct parser
*parser
, WCHAR
*pos
)
702 if (*p
== 'H' || *p
== 'h')
703 delete_registry_key(p
);
705 set_state(parser
, LINE_START
);
709 /* handler for parser DEFAULT_VALUE_NAME state */
710 static WCHAR
*default_value_name_state(struct parser
*parser
, WCHAR
*pos
)
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
= HeapAlloc(GetProcessHeap(), 0, (lstrlenW(val_name
) + 1) * sizeof(WCHAR
));
734 CHECK_ENOUGH_MEMORY(parser
->value_name
);
735 lstrcpyW(parser
->value_name
, val_name
);
737 set_state(parser
, DATA_START
);
741 set_state(parser
, LINE_START
);
745 /* handler for parser DATA_START state */
746 static WCHAR
*data_start_state(struct parser
*parser
, WCHAR
*pos
)
751 while (*p
== ' ' || *p
== '\t') p
++;
752 if (*p
!= '=') goto done
;
754 while (*p
== ' ' || *p
== '\t') p
++;
756 /* trim trailing whitespace */
758 while (len
> 0 && (p
[len
- 1] == ' ' || p
[len
- 1] == '\t')) len
--;
762 set_state(parser
, DELETE_VALUE
);
764 set_state(parser
, DATA_TYPE
);
768 set_state(parser
, LINE_START
);
772 /* handler for parser DELETE_VALUE state */
773 static WCHAR
*delete_value_state(struct parser
*parser
, WCHAR
*pos
)
777 while (*p
== ' ' || *p
== '\t') p
++;
778 if (*p
&& *p
!= ';') goto done
;
780 RegDeleteValueW(parser
->hkey
, parser
->value_name
);
783 set_state(parser
, LINE_START
);
787 /* handler for parser DATA_TYPE state */
788 static WCHAR
*data_type_state(struct parser
*parser
, WCHAR
*pos
)
792 if (!parse_data_type(parser
, &line
))
794 set_state(parser
, LINE_START
);
798 switch (parser
->parse_type
)
801 set_state(parser
, STRING_DATA
);
804 set_state(parser
, DWORD_DATA
);
806 case REG_BINARY
: /* all hex data types, including undefined */
807 set_state(parser
, HEX_DATA
);
810 set_state(parser
, UNKNOWN_DATA
);
816 /* handler for parser STRING_DATA state */
817 static WCHAR
*string_data_state(struct parser
*parser
, WCHAR
*pos
)
823 if (!REGPROC_unescape_string(parser
->data
, &line
))
826 while (*line
== ' ' || *line
== '\t') line
++;
827 if (*line
&& *line
!= ';') goto invalid
;
829 parser
->data_size
= (lstrlenW(parser
->data
) + 1) * sizeof(WCHAR
);
831 set_state(parser
, SET_VALUE
);
835 free_parser_data(parser
);
836 set_state(parser
, LINE_START
);
840 /* handler for parser DWORD_DATA state */
841 static WCHAR
*dword_data_state(struct parser
*parser
, WCHAR
*pos
)
845 parser
->data
= HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD
));
846 CHECK_ENOUGH_MEMORY(parser
->data
);
848 if (!convert_hex_to_dword(line
, parser
->data
))
851 parser
->data_size
= sizeof(DWORD
);
853 set_state(parser
, SET_VALUE
);
857 free_parser_data(parser
);
858 set_state(parser
, LINE_START
);
862 /* handler for parser HEX_DATA state */
863 static WCHAR
*hex_data_state(struct parser
*parser
, WCHAR
*pos
)
867 if (!convert_hex_csv_to_hex(parser
, &line
))
870 if (parser
->backslash
)
872 set_state(parser
, EOL_BACKSLASH
);
876 prepare_hex_string_data(parser
);
878 set_state(parser
, SET_VALUE
);
882 free_parser_data(parser
);
883 set_state(parser
, LINE_START
);
887 /* handler for parser EOL_BACKSLASH state */
888 static WCHAR
*eol_backslash_state(struct parser
*parser
, WCHAR
*pos
)
892 while (*p
== ' ' || *p
== '\t') p
++;
893 if (*p
&& *p
!= ';') goto invalid
;
895 set_state(parser
, HEX_MULTILINE
);
899 free_parser_data(parser
);
900 set_state(parser
, LINE_START
);
904 /* handler for parser HEX_MULTILINE state */
905 static WCHAR
*hex_multiline_state(struct parser
*parser
, WCHAR
*pos
)
909 if (!(line
= get_line(parser
->file
)))
911 prepare_hex_string_data(parser
);
912 set_state(parser
, SET_VALUE
);
916 while (*line
== ' ' || *line
== '\t') line
++;
917 if (!*line
|| *line
== ';') return line
;
919 if (!isxdigitW(*line
)) goto invalid
;
921 set_state(parser
, HEX_DATA
);
925 free_parser_data(parser
);
926 set_state(parser
, LINE_START
);
930 /* handler for parser UNKNOWN_DATA state */
931 static WCHAR
*unknown_data_state(struct parser
*parser
, WCHAR
*pos
)
933 output_message(STRING_UNKNOWN_DATA_FORMAT
, parser
->data_type
);
935 set_state(parser
, LINE_START
);
939 /* handler for parser SET_VALUE state */
940 static WCHAR
*set_value_state(struct parser
*parser
, WCHAR
*pos
)
942 RegSetValueExW(parser
->hkey
, parser
->value_name
, 0, parser
->data_type
,
943 parser
->data
, parser
->data_size
);
945 free_parser_data(parser
);
947 if (parser
->reg_version
== REG_VERSION_31
)
948 set_state(parser
, PARSE_WIN31_LINE
);
950 set_state(parser
, LINE_START
);
955 static WCHAR
*get_lineA(FILE *fp
)
959 static char *buf
, *next
;
962 HeapFree(GetProcessHeap(), 0, lineW
);
964 if (!fp
) goto cleanup
;
968 size
= REG_VAL_BUF_SIZE
;
969 buf
= HeapAlloc(GetProcessHeap(), 0, size
);
970 CHECK_ENOUGH_MEMORY(buf
);
978 char *p
= strpbrk(line
, "\r\n");
983 memmove(buf
, next
, len
+ 1);
986 char *new_buf
= HeapReAlloc(GetProcessHeap(), 0, buf
, size
* 2);
987 CHECK_ENOUGH_MEMORY(new_buf
);
991 if (!(count
= fread(buf
+ len
, 1, size
- len
- 1, fp
)))
994 lineW
= GetWideString(buf
);
997 buf
[len
+ count
] = 0;
1003 if (*p
== '\r' && *(p
+ 1) == '\n') next
++;
1005 lineW
= GetWideString(line
);
1011 if (size
) HeapFree(GetProcessHeap(), 0, buf
);
1016 static WCHAR
*get_lineW(FILE *fp
)
1019 static WCHAR
*buf
, *next
;
1022 if (!fp
) goto cleanup
;
1026 size
= REG_VAL_BUF_SIZE
;
1027 buf
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(WCHAR
));
1028 CHECK_ENOUGH_MEMORY(buf
);
1036 static const WCHAR line_endings
[] = {'\r','\n',0};
1037 WCHAR
*p
= strpbrkW(line
, line_endings
);
1041 len
= strlenW(next
);
1042 memmove(buf
, next
, (len
+ 1) * sizeof(WCHAR
));
1045 WCHAR
*new_buf
= HeapReAlloc(GetProcessHeap(), 0, buf
, (size
* 2) * sizeof(WCHAR
));
1046 CHECK_ENOUGH_MEMORY(new_buf
);
1050 if (!(count
= fread(buf
+ len
, sizeof(WCHAR
), size
- len
- 1, fp
)))
1055 buf
[len
+ count
] = 0;
1061 if (*p
== '\r' && *(p
+ 1) == '\n') next
++;
1067 if (size
) HeapFree(GetProcessHeap(), 0, buf
);
1072 /******************************************************************************
1073 * Checks whether the buffer has enough room for the string or required size.
1074 * Resizes the buffer if necessary.
1077 * buffer - pointer to a buffer for string
1078 * len - current length of the buffer in characters.
1079 * required_len - length of the string to place to the buffer in characters.
1080 * The length does not include the terminating null character.
1082 static void REGPROC_resize_char_buffer(WCHAR
**buffer
, DWORD
*len
, DWORD required_len
)
1085 if (required_len
> *len
) {
1086 *len
= required_len
;
1088 *buffer
= HeapAlloc(GetProcessHeap(), 0, *len
* sizeof(**buffer
));
1090 *buffer
= HeapReAlloc(GetProcessHeap(), 0, *buffer
, *len
* sizeof(**buffer
));
1091 CHECK_ENOUGH_MEMORY(*buffer
);
1095 /******************************************************************************
1096 * Same as REGPROC_resize_char_buffer() but on a regular buffer.
1099 * buffer - pointer to a buffer
1100 * len - current size of the buffer in bytes
1101 * required_size - size of the data to place in the buffer in bytes
1103 static void REGPROC_resize_binary_buffer(BYTE
**buffer
, DWORD
*size
, DWORD required_size
)
1105 if (required_size
> *size
) {
1106 *size
= required_size
;
1108 *buffer
= HeapAlloc(GetProcessHeap(), 0, *size
);
1110 *buffer
= HeapReAlloc(GetProcessHeap(), 0, *buffer
, *size
);
1111 CHECK_ENOUGH_MEMORY(*buffer
);
1115 /******************************************************************************
1116 * Prints string str to file
1118 static void REGPROC_export_string(WCHAR
**line_buf
, DWORD
*line_buf_size
, DWORD
*line_len
, WCHAR
*str
, DWORD str_len
)
1123 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, *line_len
+ str_len
+ 10);
1125 /* escaping characters */
1127 for (i
= 0; i
< str_len
; i
++) {
1132 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, *line_len
+ str_len
+ extra
);
1133 (*line_buf
)[pos
++] = '\\';
1134 (*line_buf
)[pos
++] = 'n';
1139 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, *line_len
+ str_len
+ extra
);
1140 (*line_buf
)[pos
++] = '\\';
1141 (*line_buf
)[pos
++] = 'r';
1147 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, *line_len
+ str_len
+ extra
);
1148 (*line_buf
)[pos
++] = '\\';
1152 (*line_buf
)[pos
++] = c
;
1156 (*line_buf
)[pos
] = '\0';
1160 static void REGPROC_export_binary(WCHAR
**line_buf
, DWORD
*line_buf_size
, DWORD
*line_len
, DWORD type
, BYTE
*value
, DWORD value_size
, BOOL unicode
)
1162 DWORD hex_pos
, data_pos
;
1163 const WCHAR
*hex_prefix
;
1164 const WCHAR hex
[] = {'h','e','x',':',0};
1166 const WCHAR concat
[] = {'\\','\r','\n',' ',' ',0};
1167 DWORD concat_prefix
, concat_len
;
1168 const WCHAR newline
[] = {'\r','\n',0};
1169 CHAR
* value_multibyte
= NULL
;
1171 if (type
== REG_BINARY
) {
1174 const WCHAR hex_format
[] = {'h','e','x','(','%','x',')',':',0};
1175 hex_prefix
= hex_buf
;
1176 sprintfW(hex_buf
, hex_format
, type
);
1177 if ((type
== REG_SZ
|| type
== REG_EXPAND_SZ
|| type
== REG_MULTI_SZ
) && !unicode
)
1179 value_multibyte
= GetMultiByteStringN((WCHAR
*)value
, value_size
/ sizeof(WCHAR
), &value_size
);
1180 value
= (BYTE
*)value_multibyte
;
1184 concat_len
= lstrlenW(concat
);
1187 hex_pos
= *line_len
;
1188 *line_len
+= lstrlenW(hex_prefix
);
1189 data_pos
= *line_len
;
1190 *line_len
+= value_size
* 3;
1191 /* - The 2 spaces that concat places at the start of the
1192 * line effectively reduce the space available for data.
1193 * - If the value name and hex prefix are very long
1194 * ( > REG_FILE_HEX_LINE_LEN) or *line_len divides
1195 * without a remainder then we may overestimate
1196 * the needed number of lines by one. But that's ok.
1197 * - The trailing '\r' takes the place of a comma so
1198 * we only need to add 1 for the trailing '\n'
1200 *line_len
+= *line_len
/ (REG_FILE_HEX_LINE_LEN
- concat_prefix
) * concat_len
+ 1;
1201 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, *line_len
);
1202 lstrcpyW(*line_buf
+ hex_pos
, hex_prefix
);
1205 const WCHAR format
[] = {'%','0','2','x',0};
1208 column
= data_pos
; /* no line wrap yet */
1212 sprintfW(*line_buf
+ data_pos
, format
, (unsigned int)value
[i
]);
1214 if (++i
== value_size
)
1217 (*line_buf
)[data_pos
++] = ',';
1221 if (column
>= REG_FILE_HEX_LINE_LEN
) {
1222 lstrcpyW(*line_buf
+ data_pos
, concat
);
1223 data_pos
+= concat_len
;
1224 column
= concat_prefix
;
1228 lstrcpyW(*line_buf
+ data_pos
, newline
);
1229 HeapFree(GetProcessHeap(), 0, value_multibyte
);
1232 /******************************************************************************
1233 * Writes the given line to a file, in multi-byte or wide characters
1235 static void REGPROC_write_line(FILE *file
, const WCHAR
* str
, BOOL unicode
)
1239 fwrite(str
, sizeof(WCHAR
), lstrlenW(str
), file
);
1242 char* strA
= GetMultiByteString(str
);
1244 HeapFree(GetProcessHeap(), 0, strA
);
1248 /******************************************************************************
1249 * Writes contents of the registry key to the specified file stream.
1252 * file - writable file stream to export registry branch to.
1253 * key - registry branch to export.
1254 * reg_key_name_buf - name of the key with registry class.
1255 * Is resized if necessary.
1256 * reg_key_name_size - length of the buffer for the registry class in characters.
1257 * val_name_buf - buffer for storing value name.
1258 * Is resized if necessary.
1259 * val_name_size - length of the buffer for storing value names in characters.
1260 * val_buf - buffer for storing values while extracting.
1261 * Is resized if necessary.
1262 * val_size - size of the buffer for storing values in bytes.
1264 static void export_hkey(FILE *file
, HKEY key
,
1265 WCHAR
**reg_key_name_buf
, DWORD
*reg_key_name_size
,
1266 WCHAR
**val_name_buf
, DWORD
*val_name_size
,
1267 BYTE
**val_buf
, DWORD
*val_size
,
1268 WCHAR
**line_buf
, DWORD
*line_buf_size
,
1271 DWORD max_sub_key_len
;
1272 DWORD max_val_name_len
;
1277 WCHAR key_format
[] = {'\r','\n','[','%','s',']','\r','\n',0};
1279 /* get size information and resize the buffers if necessary */
1280 if (RegQueryInfoKeyW(key
, NULL
, NULL
, NULL
, NULL
,
1281 &max_sub_key_len
, NULL
,
1282 NULL
, &max_val_name_len
, &max_val_size
, NULL
, NULL
1285 curr_len
= strlenW(*reg_key_name_buf
);
1286 REGPROC_resize_char_buffer(reg_key_name_buf
, reg_key_name_size
,
1287 max_sub_key_len
+ curr_len
+ 1);
1288 REGPROC_resize_char_buffer(val_name_buf
, val_name_size
,
1290 REGPROC_resize_binary_buffer(val_buf
, val_size
, max_val_size
);
1291 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, lstrlenW(*reg_key_name_buf
) + 4);
1292 /* output data for the current key */
1293 sprintfW(*line_buf
, key_format
, *reg_key_name_buf
);
1294 REGPROC_write_line(file
, *line_buf
, unicode
);
1296 /* print all the values */
1300 DWORD val_name_size1
= *val_name_size
;
1301 DWORD val_size1
= *val_size
;
1302 ret
= RegEnumValueW(key
, i
, *val_name_buf
, &val_name_size1
, NULL
,
1303 &value_type
, *val_buf
, &val_size1
);
1304 if (ret
== ERROR_MORE_DATA
) {
1305 /* Increase the size of the buffers and retry */
1306 REGPROC_resize_char_buffer(val_name_buf
, val_name_size
, val_name_size1
);
1307 REGPROC_resize_binary_buffer(val_buf
, val_size
, val_size1
);
1308 } else if (ret
== ERROR_SUCCESS
) {
1312 if ((*val_name_buf
)[0]) {
1313 const WCHAR val_start
[] = {'"','%','s','"','=',0};
1316 REGPROC_export_string(line_buf
, line_buf_size
, &line_len
, *val_name_buf
, lstrlenW(*val_name_buf
));
1317 REGPROC_resize_char_buffer(val_name_buf
, val_name_size
, lstrlenW(*line_buf
) + 1);
1318 lstrcpyW(*val_name_buf
, *line_buf
);
1320 line_len
= 3 + lstrlenW(*val_name_buf
);
1321 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, line_len
);
1322 sprintfW(*line_buf
, val_start
, *val_name_buf
);
1324 const WCHAR std_val
[] = {'@','=',0};
1326 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, line_len
);
1327 lstrcpyW(*line_buf
, std_val
);
1330 switch (value_type
) {
1333 WCHAR
* wstr
= (WCHAR
*)*val_buf
;
1335 if (val_size1
< sizeof(WCHAR
) || val_size1
% sizeof(WCHAR
) ||
1336 wstr
[val_size1
/ sizeof(WCHAR
) - 1]) {
1337 REGPROC_export_binary(line_buf
, line_buf_size
, &line_len
, value_type
, *val_buf
, val_size1
, unicode
);
1339 const WCHAR start
[] = {'"',0};
1340 const WCHAR end
[] = {'"','\r','\n',0};
1343 len
= lstrlenW(start
);
1344 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, line_len
+ len
);
1345 lstrcpyW(*line_buf
+ line_len
, start
);
1348 REGPROC_export_string(line_buf
, line_buf_size
, &line_len
, wstr
, lstrlenW(wstr
));
1350 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, line_len
+ lstrlenW(end
));
1351 lstrcpyW(*line_buf
+ line_len
, end
);
1358 WCHAR format
[] = {'d','w','o','r','d',':','%','0','8','x','\r','\n',0};
1360 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, line_len
+ 15);
1361 sprintfW(*line_buf
+ line_len
, format
, *((DWORD
*)*val_buf
));
1370 REGPROC_export_binary(line_buf
, line_buf_size
, &line_len
, value_type
, *val_buf
, val_size1
, unicode
);
1372 REGPROC_write_line(file
, *line_buf
, unicode
);
1378 (*reg_key_name_buf
)[curr_len
] = '\\';
1380 DWORD buf_size
= *reg_key_name_size
- curr_len
- 1;
1382 ret
= RegEnumKeyExW(key
, i
, *reg_key_name_buf
+ curr_len
+ 1, &buf_size
,
1383 NULL
, NULL
, NULL
, NULL
);
1384 if (ret
== ERROR_MORE_DATA
) {
1385 /* Increase the size of the buffer and retry */
1386 REGPROC_resize_char_buffer(reg_key_name_buf
, reg_key_name_size
, curr_len
+ 1 + buf_size
);
1387 } else if (ret
== ERROR_SUCCESS
) {
1391 if (RegOpenKeyW(key
, *reg_key_name_buf
+ curr_len
+ 1,
1392 &subkey
) == ERROR_SUCCESS
) {
1393 export_hkey(file
, subkey
, reg_key_name_buf
, reg_key_name_size
,
1394 val_name_buf
, val_name_size
, val_buf
, val_size
,
1395 line_buf
, line_buf_size
, unicode
);
1396 RegCloseKey(subkey
);
1402 (*reg_key_name_buf
)[curr_len
] = '\0';
1405 /******************************************************************************
1406 * Open file in binary mode for export.
1408 static FILE *REGPROC_open_export_file(WCHAR
*file_name
, BOOL unicode
)
1413 if (strncmpW(file_name
,&dash
,1)==0) {
1415 _setmode(_fileno(file
), _O_BINARY
);
1418 WCHAR wb_mode
[] = {'w','b',0};
1419 WCHAR regedit
[] = {'r','e','g','e','d','i','t',0};
1421 file
= _wfopen(file_name
, wb_mode
);
1424 output_message(STRING_CANNOT_OPEN_FILE
, file_name
);
1430 const BYTE unicode_seq
[] = {0xff,0xfe};
1431 const WCHAR header
[] = {'W','i','n','d','o','w','s',' ','R','e','g','i','s','t','r','y',' ','E','d','i','t','o','r',' ','V','e','r','s','i','o','n',' ','5','.','0','0','\r','\n'};
1432 fwrite(unicode_seq
, sizeof(BYTE
), sizeof(unicode_seq
)/sizeof(unicode_seq
[0]), file
);
1433 fwrite(header
, sizeof(WCHAR
), sizeof(header
)/sizeof(header
[0]), file
);
1436 fputs("REGEDIT4\r\n", file
);
1442 /******************************************************************************
1443 * Writes contents of the registry key to the specified file stream.
1446 * file_name - name of a file to export registry branch to.
1447 * reg_key_name - registry branch to export. The whole registry is exported if
1448 * reg_key_name is NULL or contains an empty string.
1450 BOOL
export_registry_key(WCHAR
*file_name
, WCHAR
*reg_key_name
, DWORD format
)
1452 WCHAR
*reg_key_name_buf
;
1453 WCHAR
*val_name_buf
;
1456 DWORD reg_key_name_size
= KEY_MAX_LEN
;
1457 DWORD val_name_size
= KEY_MAX_LEN
;
1458 DWORD val_size
= REG_VAL_BUF_SIZE
;
1459 DWORD line_buf_size
= KEY_MAX_LEN
+ REG_VAL_BUF_SIZE
;
1461 BOOL unicode
= (format
== REG_FORMAT_5
);
1463 reg_key_name_buf
= HeapAlloc(GetProcessHeap(), 0,
1464 reg_key_name_size
* sizeof(*reg_key_name_buf
));
1465 val_name_buf
= HeapAlloc(GetProcessHeap(), 0,
1466 val_name_size
* sizeof(*val_name_buf
));
1467 val_buf
= HeapAlloc(GetProcessHeap(), 0, val_size
);
1468 line_buf
= HeapAlloc(GetProcessHeap(), 0, line_buf_size
* sizeof(*line_buf
));
1469 CHECK_ENOUGH_MEMORY(reg_key_name_buf
&& val_name_buf
&& val_buf
&& line_buf
);
1471 if (reg_key_name
&& reg_key_name
[0]) {
1473 WCHAR
*branch_name
= NULL
;
1476 REGPROC_resize_char_buffer(®_key_name_buf
, ®_key_name_size
,
1477 lstrlenW(reg_key_name
));
1478 lstrcpyW(reg_key_name_buf
, reg_key_name
);
1480 /* open the specified key */
1481 if (!(reg_key_class
= parse_key_name(reg_key_name
, &branch_name
))) {
1482 output_message(STRING_INCORRECT_REG_CLASS
, reg_key_name
);
1485 if (!branch_name
|| !*branch_name
) {
1486 /* no branch - registry class is specified */
1487 file
= REGPROC_open_export_file(file_name
, unicode
);
1488 export_hkey(file
, reg_key_class
,
1489 ®_key_name_buf
, ®_key_name_size
,
1490 &val_name_buf
, &val_name_size
,
1491 &val_buf
, &val_size
, &line_buf
,
1492 &line_buf_size
, unicode
);
1493 } else if (RegOpenKeyW(reg_key_class
, branch_name
, &key
) == ERROR_SUCCESS
) {
1494 file
= REGPROC_open_export_file(file_name
, unicode
);
1495 export_hkey(file
, key
,
1496 ®_key_name_buf
, ®_key_name_size
,
1497 &val_name_buf
, &val_name_size
,
1498 &val_buf
, &val_size
, &line_buf
,
1499 &line_buf_size
, unicode
);
1502 output_message(STRING_REG_KEY_NOT_FOUND
, reg_key_name
);
1507 /* export all registry classes */
1508 file
= REGPROC_open_export_file(file_name
, unicode
);
1509 for (i
= 0; i
< ARRAY_SIZE(reg_class_keys
); i
++) {
1510 /* do not export HKEY_CLASSES_ROOT */
1511 if (reg_class_keys
[i
] != HKEY_CLASSES_ROOT
&&
1512 reg_class_keys
[i
] != HKEY_CURRENT_USER
&&
1513 reg_class_keys
[i
] != HKEY_CURRENT_CONFIG
&&
1514 reg_class_keys
[i
] != HKEY_DYN_DATA
) {
1515 lstrcpyW(reg_key_name_buf
, reg_class_namesW
[i
]);
1516 export_hkey(file
, reg_class_keys
[i
],
1517 ®_key_name_buf
, ®_key_name_size
,
1518 &val_name_buf
, &val_name_size
,
1519 &val_buf
, &val_size
, &line_buf
,
1520 &line_buf_size
, unicode
);
1528 HeapFree(GetProcessHeap(), 0, reg_key_name
);
1529 HeapFree(GetProcessHeap(), 0, val_name_buf
);
1530 HeapFree(GetProcessHeap(), 0, val_buf
);
1531 HeapFree(GetProcessHeap(), 0, line_buf
);
1535 /******************************************************************************
1536 * Reads contents of the specified file into the registry.
1538 BOOL
import_registry_file(FILE *reg_file
)
1541 struct parser parser
;
1544 if (!reg_file
|| (fread(s
, 2, 1, reg_file
) != 1))
1547 parser
.is_unicode
= (s
[0] == 0xff && s
[1] == 0xfe);
1548 get_line
= parser
.is_unicode
? get_lineW
: get_lineA
;
1550 parser
.file
= reg_file
;
1551 parser
.two_wchars
[0] = s
[0];
1552 parser
.two_wchars
[1] = s
[1];
1553 parser
.reg_version
= -1;
1555 parser
.key_name
= NULL
;
1556 parser
.value_name
= NULL
;
1557 parser
.parse_type
= 0;
1558 parser
.data_type
= 0;
1560 parser
.data_size
= 0;
1561 parser
.backslash
= FALSE
;
1562 parser
.state
= HEADER
;
1564 pos
= parser
.two_wchars
;
1566 /* parser main loop */
1568 pos
= (parser_funcs
[parser
.state
])(&parser
, pos
);
1570 if (parser
.reg_version
== REG_VERSION_FUZZY
|| parser
.reg_version
== REG_VERSION_INVALID
)
1571 return parser
.reg_version
== REG_VERSION_FUZZY
;
1573 HeapFree(GetProcessHeap(), 0, parser
.value_name
);
1579 /******************************************************************************
1580 * Removes the registry key with all subkeys. Parses full key name.
1583 * reg_key_name - full name of registry branch to delete. Ignored if is NULL,
1584 * empty, points to register key class, does not exist.
1586 void delete_registry_key(WCHAR
*reg_key_name
)
1588 WCHAR
*key_name
= NULL
;
1591 if (!reg_key_name
|| !reg_key_name
[0])
1594 if (!(key_class
= parse_key_name(reg_key_name
, &key_name
))) {
1595 output_message(STRING_INCORRECT_REG_CLASS
, reg_key_name
);
1599 output_message(STRING_DELETE_REG_CLASS_FAILED
, reg_key_name
);
1603 RegDeleteTreeW(key_class
, key_name
);