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
32 #define REG_VAL_BUF_SIZE 4096
34 static HKEY reg_class_keys
[] = {
35 HKEY_LOCAL_MACHINE
, HKEY_USERS
, HKEY_CLASSES_ROOT
,
36 HKEY_CURRENT_CONFIG
, HKEY_CURRENT_USER
, HKEY_DYN_DATA
39 /******************************************************************************
40 * Allocates memory and converts input from multibyte to wide chars
41 * Returned string must be freed by the caller
43 static WCHAR
* GetWideString(const char* strA
)
48 int len
= MultiByteToWideChar(CP_ACP
, 0, strA
, -1, NULL
, 0);
50 strW
= malloc(len
* sizeof(WCHAR
));
51 MultiByteToWideChar(CP_ACP
, 0, strA
, -1, strW
, len
);
57 /******************************************************************************
58 * Allocates memory and converts input from multibyte to wide chars
59 * Returned string must be freed by the caller
61 static WCHAR
* GetWideStringN(const char* strA
, int chars
, DWORD
*len
)
66 *len
= MultiByteToWideChar(CP_ACP
, 0, strA
, chars
, NULL
, 0);
68 strW
= malloc(*len
* sizeof(WCHAR
));
69 MultiByteToWideChar(CP_ACP
, 0, strA
, chars
, strW
, *len
);
76 /******************************************************************************
77 * Allocates memory and converts input from wide chars to multibyte
78 * Returned string must be freed by the caller
80 char* GetMultiByteString(const WCHAR
* strW
)
85 int len
= WideCharToMultiByte(CP_ACP
, 0, strW
, -1, NULL
, 0, NULL
, NULL
);
88 WideCharToMultiByte(CP_ACP
, 0, strW
, -1, strA
, len
, NULL
, NULL
);
94 /******************************************************************************
95 * Allocates memory and converts input from wide chars to multibyte
96 * Returned string must be freed by the caller
98 static char* GetMultiByteStringN(const WCHAR
* strW
, int chars
, DWORD
* len
)
103 *len
= WideCharToMultiByte(CP_ACP
, 0, strW
, chars
, NULL
, 0, NULL
, NULL
);
106 WideCharToMultiByte(CP_ACP
, 0, strW
, chars
, strA
, *len
, NULL
, NULL
);
113 static WCHAR
*(*get_line
)(FILE *);
115 /* parser definitions */
118 HEADER
, /* parsing the registry file version header */
119 PARSE_WIN31_LINE
, /* parsing a Windows 3.1 registry line */
120 LINE_START
, /* at the beginning of a registry line */
121 KEY_NAME
, /* parsing a key name */
122 DELETE_KEY
, /* deleting a registry key */
123 DEFAULT_VALUE_NAME
, /* parsing a default value name */
124 QUOTED_VALUE_NAME
, /* parsing a double-quoted value name */
125 DATA_START
, /* preparing for data parsing operations */
126 DELETE_VALUE
, /* deleting a registry value */
127 DATA_TYPE
, /* parsing the registry data type */
128 STRING_DATA
, /* parsing REG_SZ data */
129 DWORD_DATA
, /* parsing DWORD data */
130 HEX_DATA
, /* parsing REG_BINARY, REG_NONE, REG_EXPAND_SZ or REG_MULTI_SZ data */
131 EOL_BACKSLASH
, /* preparing to parse multiple lines of hex data */
132 HEX_MULTILINE
, /* parsing multiple lines of hex data */
133 UNKNOWN_DATA
, /* parsing an unhandled or invalid data type */
134 SET_VALUE
, /* adding a value to the registry */
140 FILE *file
; /* pointer to a registry file */
141 WCHAR two_wchars
[2]; /* first two characters from the encoding check */
142 BOOL is_unicode
; /* parsing Unicode or ASCII data */
143 short int reg_version
; /* registry file version */
144 HKEY hkey
; /* current registry key */
145 WCHAR
*key_name
; /* current key name */
146 WCHAR
*value_name
; /* value name */
147 DWORD parse_type
; /* generic data type for parsing */
148 DWORD data_type
; /* data type */
149 void *data
; /* value data */
150 DWORD data_size
; /* size of the data (in bytes) */
151 BOOL backslash
; /* TRUE if the current line contains a backslash */
152 enum parser_state state
; /* current parser state */
155 typedef WCHAR
*(*parser_state_func
)(struct parser
*parser
, WCHAR
*pos
);
157 /* parser state machine functions */
158 static WCHAR
*header_state(struct parser
*parser
, WCHAR
*pos
);
159 static WCHAR
*parse_win31_line_state(struct parser
*parser
, WCHAR
*pos
);
160 static WCHAR
*line_start_state(struct parser
*parser
, WCHAR
*pos
);
161 static WCHAR
*key_name_state(struct parser
*parser
, WCHAR
*pos
);
162 static WCHAR
*delete_key_state(struct parser
*parser
, WCHAR
*pos
);
163 static WCHAR
*default_value_name_state(struct parser
*parser
, WCHAR
*pos
);
164 static WCHAR
*quoted_value_name_state(struct parser
*parser
, WCHAR
*pos
);
165 static WCHAR
*data_start_state(struct parser
*parser
, WCHAR
*pos
);
166 static WCHAR
*delete_value_state(struct parser
*parser
, WCHAR
*pos
);
167 static WCHAR
*data_type_state(struct parser
*parser
, WCHAR
*pos
);
168 static WCHAR
*string_data_state(struct parser
*parser
, WCHAR
*pos
);
169 static WCHAR
*dword_data_state(struct parser
*parser
, WCHAR
*pos
);
170 static WCHAR
*hex_data_state(struct parser
*parser
, WCHAR
*pos
);
171 static WCHAR
*eol_backslash_state(struct parser
*parser
, WCHAR
*pos
);
172 static WCHAR
*hex_multiline_state(struct parser
*parser
, WCHAR
*pos
);
173 static WCHAR
*unknown_data_state(struct parser
*parser
, WCHAR
*pos
);
174 static WCHAR
*set_value_state(struct parser
*parser
, WCHAR
*pos
);
176 static const parser_state_func parser_funcs
[NB_PARSER_STATES
] =
178 header_state
, /* HEADER */
179 parse_win31_line_state
, /* PARSE_WIN31_LINE */
180 line_start_state
, /* LINE_START */
181 key_name_state
, /* KEY_NAME */
182 delete_key_state
, /* DELETE_KEY */
183 default_value_name_state
, /* DEFAULT_VALUE_NAME */
184 quoted_value_name_state
, /* QUOTED_VALUE_NAME */
185 data_start_state
, /* DATA_START */
186 delete_value_state
, /* DELETE_VALUE */
187 data_type_state
, /* DATA_TYPE */
188 string_data_state
, /* STRING_DATA */
189 dword_data_state
, /* DWORD_DATA */
190 hex_data_state
, /* HEX_DATA */
191 eol_backslash_state
, /* EOL_BACKSLASH */
192 hex_multiline_state
, /* HEX_MULTILINE */
193 unknown_data_state
, /* UNKNOWN_DATA */
194 set_value_state
, /* SET_VALUE */
197 /* set the new parser state and return the previous one */
198 static inline enum parser_state
set_state(struct parser
*parser
, enum parser_state state
)
200 enum parser_state ret
= parser
->state
;
201 parser
->state
= state
;
205 /******************************************************************************
206 * Converts a hex representation of a DWORD into a DWORD.
208 static BOOL
convert_hex_to_dword(WCHAR
*str
, DWORD
*dw
)
213 while (*str
== ' ' || *str
== '\t') str
++;
214 if (!*str
) goto error
;
217 while (iswxdigit(*p
))
222 if (count
> 8) goto error
;
225 while (*p
== ' ' || *p
== '\t') p
++;
226 if (*p
&& *p
!= ';') goto error
;
229 *dw
= wcstoul(str
, &end
, 16);
236 /******************************************************************************
237 * Converts comma-separated hex data into a binary string and modifies
238 * the input parameter to skip the concatenating backslash, if found.
240 * Returns TRUE or FALSE to indicate whether parsing was successful.
242 static BOOL
convert_hex_csv_to_hex(struct parser
*parser
, WCHAR
**str
)
248 parser
->backslash
= FALSE
;
250 /* The worst case is 1 digit + 1 comma per byte */
251 size
= ((lstrlenW(*str
) + 1) / 2) + parser
->data_size
;
252 parser
->data
= realloc(parser
->data
, size
);
255 d
= (BYTE
*)parser
->data
+ parser
->data_size
;
262 wc
= wcstoul(s
, &end
, 16);
263 if (wc
> 0xff) return FALSE
;
265 if (s
== end
&& wc
== 0)
267 while (*end
== ' ' || *end
== '\t') end
++;
270 parser
->backslash
= TRUE
;
274 else if (*end
== ';')
282 if (*end
&& *end
!= ',')
284 while (*end
== ' ' || *end
== '\t') end
++;
285 if (*end
&& *end
!= ';') return FALSE
;
296 /******************************************************************************
297 * Parses the data type of the registry value being imported and modifies
298 * the input parameter to skip the string representation of the data type.
300 * Returns TRUE or FALSE to indicate whether a data type was found.
302 static BOOL
parse_data_type(struct parser
*parser
, WCHAR
**line
)
304 struct data_type
{ const WCHAR
*tag
; int len
; int type
; int parse_type
; };
306 static const struct data_type data_types
[] = {
307 /* tag len type parse type */
308 { L
"\"", 1, REG_SZ
, REG_SZ
},
309 { L
"hex:", 4, REG_BINARY
, REG_BINARY
},
310 { L
"dword:", 6, REG_DWORD
, REG_DWORD
},
311 { L
"hex(", 4, -1, REG_BINARY
}, /* REG_NONE, REG_EXPAND_SZ, REG_MULTI_SZ */
315 const struct data_type
*ptr
;
317 for (ptr
= data_types
; ptr
->tag
; ptr
++)
319 if (wcsncmp(ptr
->tag
, *line
, ptr
->len
))
322 parser
->parse_type
= ptr
->parse_type
;
323 parser
->data_type
= ptr
->parse_type
;
331 if (!**line
|| towlower((*line
)[1]) == 'x')
334 /* "hex(xx):" is special */
335 val
= wcstoul(*line
, &end
, 16);
336 if (*end
!= ')' || *(end
+ 1) != ':' || (val
== ~0u && errno
== ERANGE
))
339 parser
->data_type
= val
;
347 /******************************************************************************
348 * Replaces escape sequences with their character equivalents and
349 * null-terminates the string on the first non-escaped double quote.
351 * Assigns a pointer to the remaining unparsed data in the line.
352 * Returns TRUE or FALSE to indicate whether a closing double quote was found.
354 static BOOL
REGPROC_unescape_string(WCHAR
*str
, WCHAR
**unparsed
)
356 int str_idx
= 0; /* current character under analysis */
357 int val_idx
= 0; /* the last character of the unescaped string */
358 int len
= lstrlenW(str
);
361 for (str_idx
= 0; str_idx
< len
; str_idx
++, val_idx
++) {
362 if (str
[str_idx
] == '\\') {
364 switch (str
[str_idx
]) {
375 str
[val_idx
] = str
[str_idx
];
378 if (!str
[str_idx
]) return FALSE
;
379 output_message(STRING_ESCAPE_SEQUENCE
, str
[str_idx
]);
380 str
[val_idx
] = str
[str_idx
];
383 } else if (str
[str_idx
] == '"') {
386 str
[val_idx
] = str
[str_idx
];
390 ret
= (str
[str_idx
] == '"');
391 *unparsed
= str
+ str_idx
+ 1;
396 static HKEY
parse_key_name(WCHAR
*key_name
, WCHAR
**key_path
)
400 if (!key_name
) return 0;
402 *key_path
= wcschr(key_name
, '\\');
403 if (*key_path
) (*key_path
)++;
405 for (i
= 0; i
< ARRAY_SIZE(reg_class_keys
); i
++)
407 int len
= lstrlenW(reg_class_namesW
[i
]);
408 if (!wcsnicmp(key_name
, reg_class_namesW
[i
], len
) &&
409 (key_name
[len
] == 0 || key_name
[len
] == '\\'))
411 return reg_class_keys
[i
];
418 static void close_key(struct parser
*parser
)
422 free(parser
->key_name
);
423 parser
->key_name
= NULL
;
425 RegCloseKey(parser
->hkey
);
430 /******************************************************************************
431 * Opens the registry key given by the input path.
432 * This key must be closed by calling close_key().
434 static LONG
open_key(struct parser
*parser
, WCHAR
*path
)
442 /* Get the registry class */
443 if (!path
|| !(key_class
= parse_key_name(path
, &key_path
)))
444 return ERROR_INVALID_PARAMETER
;
446 res
= RegCreateKeyExW(key_class
, key_path
, 0, NULL
, REG_OPTION_NON_VOLATILE
,
447 KEY_ALL_ACCESS
, NULL
, &parser
->hkey
, NULL
);
449 if (res
== ERROR_SUCCESS
)
451 parser
->key_name
= malloc((lstrlenW(path
) + 1) * sizeof(WCHAR
));
452 lstrcpyW(parser
->key_name
, path
);
460 static void free_parser_data(struct parser
*parser
)
462 if (parser
->parse_type
== REG_DWORD
|| parser
->parse_type
== REG_BINARY
)
466 parser
->data_size
= 0;
469 static void prepare_hex_string_data(struct parser
*parser
)
471 if (parser
->data_type
== REG_EXPAND_SZ
|| parser
->data_type
== REG_MULTI_SZ
||
472 parser
->data_type
== REG_SZ
)
474 if (parser
->is_unicode
)
476 WCHAR
*data
= parser
->data
;
477 DWORD len
= parser
->data_size
/ sizeof(WCHAR
);
479 if (data
[len
- 1] != 0)
482 parser
->data_size
+= sizeof(WCHAR
);
487 BYTE
*data
= parser
->data
;
489 if (data
[parser
->data_size
- 1] != 0)
491 data
[parser
->data_size
] = 0;
495 parser
->data
= GetWideStringN(parser
->data
, parser
->data_size
, &parser
->data_size
);
496 parser
->data_size
*= sizeof(WCHAR
);
510 static enum reg_versions
parse_file_header(const WCHAR
*s
)
512 static const WCHAR header_31
[] = L
"REGEDIT";
514 while (*s
== ' ' || *s
== '\t') s
++;
516 if (!lstrcmpW(s
, header_31
))
517 return REG_VERSION_31
;
519 if (!lstrcmpW(s
, L
"REGEDIT4"))
520 return REG_VERSION_40
;
522 if (!lstrcmpW(s
, L
"Windows Registry Editor Version 5.00"))
523 return REG_VERSION_50
;
525 /* The Windows version accepts registry file headers beginning with "REGEDIT" and ending
526 * with other characters, as long as "REGEDIT" appears at the start of the line. For example,
527 * "REGEDIT 4", "REGEDIT9" and "REGEDIT4FOO" are all treated as valid file headers.
528 * In all such cases, however, the contents of the registry file are not imported.
530 if (!wcsncmp(s
, header_31
, 7)) /* "REGEDIT" without NUL */
531 return REG_VERSION_FUZZY
;
533 return REG_VERSION_INVALID
;
536 /* handler for parser HEADER state */
537 static WCHAR
*header_state(struct parser
*parser
, WCHAR
*pos
)
539 WCHAR
*line
, *header
;
541 if (!(line
= get_line(parser
->file
)))
544 if (!parser
->is_unicode
)
546 header
= malloc((lstrlenW(line
) + 3) * sizeof(WCHAR
));
547 header
[0] = parser
->two_wchars
[0];
548 header
[1] = parser
->two_wchars
[1];
549 lstrcpyW(header
+ 2, line
);
550 parser
->reg_version
= parse_file_header(header
);
553 else parser
->reg_version
= parse_file_header(line
);
555 switch (parser
->reg_version
)
558 set_state(parser
, PARSE_WIN31_LINE
);
562 set_state(parser
, LINE_START
);
565 get_line(NULL
); /* Reset static variables */
572 /* handler for parser PARSE_WIN31_LINE state */
573 static WCHAR
*parse_win31_line_state(struct parser
*parser
, WCHAR
*pos
)
576 static WCHAR hkcr
[] = L
"HKEY_CLASSES_ROOT";
577 unsigned int key_end
= 0;
579 if (!(line
= get_line(parser
->file
)))
582 if (wcsncmp(line
, hkcr
, lstrlenW(hkcr
)))
586 while (line
[key_end
] && !iswspace(line
[key_end
])) key_end
++;
588 value
= line
+ key_end
;
589 while (*value
== ' ' || *value
== '\t') value
++;
591 if (*value
== '=') value
++;
592 if (*value
== ' ') value
++; /* at most one space is skipped */
596 if (open_key(parser
, line
) != ERROR_SUCCESS
)
598 output_message(STRING_OPEN_KEY_FAILED
, line
);
602 parser
->value_name
= NULL
;
603 parser
->data_type
= REG_SZ
;
604 parser
->data
= value
;
605 parser
->data_size
= (lstrlenW(value
) + 1) * sizeof(WCHAR
);
607 set_state(parser
, SET_VALUE
);
611 /* handler for parser LINE_START state */
612 static WCHAR
*line_start_state(struct parser
*parser
, WCHAR
*pos
)
616 if (!(line
= get_line(parser
->file
)))
619 for (p
= line
; *p
; p
++)
624 set_state(parser
, KEY_NAME
);
627 set_state(parser
, DEFAULT_VALUE_NAME
);
630 set_state(parser
, QUOTED_VALUE_NAME
);
643 /* handler for parser KEY_NAME state */
644 static WCHAR
*key_name_state(struct parser
*parser
, WCHAR
*pos
)
646 WCHAR
*p
= pos
, *key_end
;
648 if (*p
== ' ' || *p
== '\t' || !(key_end
= wcsrchr(p
, ']')))
655 set_state(parser
, DELETE_KEY
);
658 else if (open_key(parser
, p
) != ERROR_SUCCESS
)
659 output_message(STRING_OPEN_KEY_FAILED
, p
);
662 set_state(parser
, LINE_START
);
666 /* handler for parser DELETE_KEY state */
667 static WCHAR
*delete_key_state(struct parser
*parser
, WCHAR
*pos
)
673 if (*p
== 'H' || *p
== 'h')
674 delete_registry_key(p
);
676 set_state(parser
, LINE_START
);
680 /* handler for parser DEFAULT_VALUE_NAME state */
681 static WCHAR
*default_value_name_state(struct parser
*parser
, WCHAR
*pos
)
683 free(parser
->value_name
);
684 parser
->value_name
= NULL
;
686 set_state(parser
, DATA_START
);
690 /* handler for parser QUOTED_VALUE_NAME state */
691 static WCHAR
*quoted_value_name_state(struct parser
*parser
, WCHAR
*pos
)
693 WCHAR
*val_name
= pos
, *p
;
695 free(parser
->value_name
);
696 parser
->value_name
= NULL
;
698 if (!REGPROC_unescape_string(val_name
, &p
))
701 /* copy the value name in case we need to parse multiple lines and the buffer is overwritten */
702 parser
->value_name
= malloc((lstrlenW(val_name
) + 1) * sizeof(WCHAR
));
703 lstrcpyW(parser
->value_name
, val_name
);
705 set_state(parser
, DATA_START
);
709 set_state(parser
, LINE_START
);
713 /* handler for parser DATA_START state */
714 static WCHAR
*data_start_state(struct parser
*parser
, WCHAR
*pos
)
719 while (*p
== ' ' || *p
== '\t') p
++;
720 if (*p
!= '=') goto done
;
722 while (*p
== ' ' || *p
== '\t') p
++;
724 /* trim trailing whitespace */
726 while (len
> 0 && (p
[len
- 1] == ' ' || p
[len
- 1] == '\t')) len
--;
730 set_state(parser
, DELETE_VALUE
);
732 set_state(parser
, DATA_TYPE
);
736 set_state(parser
, LINE_START
);
740 /* handler for parser DELETE_VALUE state */
741 static WCHAR
*delete_value_state(struct parser
*parser
, WCHAR
*pos
)
745 while (*p
== ' ' || *p
== '\t') p
++;
746 if (*p
&& *p
!= ';') goto done
;
748 RegDeleteValueW(parser
->hkey
, parser
->value_name
);
751 set_state(parser
, LINE_START
);
755 /* handler for parser DATA_TYPE state */
756 static WCHAR
*data_type_state(struct parser
*parser
, WCHAR
*pos
)
760 if (!parse_data_type(parser
, &line
))
762 set_state(parser
, LINE_START
);
766 switch (parser
->parse_type
)
769 set_state(parser
, STRING_DATA
);
772 set_state(parser
, DWORD_DATA
);
774 case REG_BINARY
: /* all hex data types, including undefined */
775 set_state(parser
, HEX_DATA
);
778 set_state(parser
, UNKNOWN_DATA
);
784 /* handler for parser STRING_DATA state */
785 static WCHAR
*string_data_state(struct parser
*parser
, WCHAR
*pos
)
791 if (!REGPROC_unescape_string(parser
->data
, &line
))
794 while (*line
== ' ' || *line
== '\t') line
++;
795 if (*line
&& *line
!= ';') goto invalid
;
797 parser
->data_size
= (lstrlenW(parser
->data
) + 1) * sizeof(WCHAR
);
799 set_state(parser
, SET_VALUE
);
803 free_parser_data(parser
);
804 set_state(parser
, LINE_START
);
808 /* handler for parser DWORD_DATA state */
809 static WCHAR
*dword_data_state(struct parser
*parser
, WCHAR
*pos
)
813 parser
->data
= malloc(sizeof(DWORD
));
815 if (!convert_hex_to_dword(line
, parser
->data
))
818 parser
->data_size
= sizeof(DWORD
);
820 set_state(parser
, SET_VALUE
);
824 free_parser_data(parser
);
825 set_state(parser
, LINE_START
);
829 /* handler for parser HEX_DATA state */
830 static WCHAR
*hex_data_state(struct parser
*parser
, WCHAR
*pos
)
837 if (!convert_hex_csv_to_hex(parser
, &line
))
840 if (parser
->backslash
)
842 set_state(parser
, EOL_BACKSLASH
);
846 prepare_hex_string_data(parser
);
849 set_state(parser
, SET_VALUE
);
853 free_parser_data(parser
);
854 set_state(parser
, LINE_START
);
858 /* handler for parser EOL_BACKSLASH state */
859 static WCHAR
*eol_backslash_state(struct parser
*parser
, WCHAR
*pos
)
863 while (*p
== ' ' || *p
== '\t') p
++;
864 if (*p
&& *p
!= ';') goto invalid
;
866 set_state(parser
, HEX_MULTILINE
);
870 free_parser_data(parser
);
871 set_state(parser
, LINE_START
);
875 /* handler for parser HEX_MULTILINE state */
876 static WCHAR
*hex_multiline_state(struct parser
*parser
, WCHAR
*pos
)
880 if (!(line
= get_line(parser
->file
)))
882 prepare_hex_string_data(parser
);
883 set_state(parser
, SET_VALUE
);
887 while (*line
== ' ' || *line
== '\t') line
++;
888 if (!*line
|| *line
== ';') return line
;
890 if (!iswxdigit(*line
)) goto invalid
;
892 set_state(parser
, HEX_DATA
);
896 free_parser_data(parser
);
897 set_state(parser
, LINE_START
);
901 /* handler for parser UNKNOWN_DATA state */
902 static WCHAR
*unknown_data_state(struct parser
*parser
, WCHAR
*pos
)
904 output_message(STRING_UNKNOWN_DATA_FORMAT
, parser
->data_type
);
906 set_state(parser
, LINE_START
);
910 /* handler for parser SET_VALUE state */
911 static WCHAR
*set_value_state(struct parser
*parser
, WCHAR
*pos
)
913 RegSetValueExW(parser
->hkey
, parser
->value_name
, 0, parser
->data_type
,
914 parser
->data
, parser
->data_size
);
916 free_parser_data(parser
);
918 if (parser
->reg_version
== REG_VERSION_31
)
919 set_state(parser
, PARSE_WIN31_LINE
);
921 set_state(parser
, LINE_START
);
926 static WCHAR
*get_lineA(FILE *fp
)
930 static char *buf
, *next
;
935 if (!fp
) goto cleanup
;
939 size
= REG_VAL_BUF_SIZE
;
948 char *p
= strpbrk(line
, "\r\n");
953 memmove(buf
, next
, len
+ 1);
957 buf
= realloc(buf
, size
);
959 if (!(count
= fread(buf
+ len
, 1, size
- len
- 1, fp
)))
962 lineW
= GetWideString(buf
);
965 buf
[len
+ count
] = 0;
971 if (*p
== '\r' && *(p
+ 1) == '\n') next
++;
973 lineW
= GetWideString(line
);
984 static WCHAR
*get_lineW(FILE *fp
)
987 static WCHAR
*buf
, *next
;
990 if (!fp
) goto cleanup
;
994 size
= REG_VAL_BUF_SIZE
;
995 buf
= malloc(size
* sizeof(WCHAR
));
1003 WCHAR
*p
= wcspbrk(line
, L
"\r\n");
1007 len
= lstrlenW(next
);
1008 memmove(buf
, next
, (len
+ 1) * sizeof(WCHAR
));
1012 buf
= realloc(buf
, size
* sizeof(WCHAR
));
1014 if (!(count
= fread(buf
+ len
, sizeof(WCHAR
), size
- len
- 1, fp
)))
1019 buf
[len
+ count
] = 0;
1025 if (*p
== '\r' && *(p
+ 1) == '\n') next
++;
1031 if (size
) free(buf
);
1036 /******************************************************************************
1037 * Reads contents of the specified file into the registry.
1039 BOOL
import_registry_file(FILE *reg_file
)
1042 struct parser parser
;
1045 if (!reg_file
|| (fread(s
, 2, 1, reg_file
) != 1))
1048 parser
.is_unicode
= (s
[0] == 0xff && s
[1] == 0xfe);
1049 get_line
= parser
.is_unicode
? get_lineW
: get_lineA
;
1051 parser
.file
= reg_file
;
1052 parser
.two_wchars
[0] = s
[0];
1053 parser
.two_wchars
[1] = s
[1];
1054 parser
.reg_version
= -1;
1056 parser
.key_name
= NULL
;
1057 parser
.value_name
= NULL
;
1058 parser
.parse_type
= 0;
1059 parser
.data_type
= 0;
1061 parser
.data_size
= 0;
1062 parser
.backslash
= FALSE
;
1063 parser
.state
= HEADER
;
1065 pos
= parser
.two_wchars
;
1067 /* parser main loop */
1069 pos
= (parser_funcs
[parser
.state
])(&parser
, pos
);
1071 if (parser
.reg_version
== REG_VERSION_FUZZY
|| parser
.reg_version
== REG_VERSION_INVALID
)
1072 return parser
.reg_version
== REG_VERSION_FUZZY
;
1074 free(parser
.value_name
);
1080 /******************************************************************************
1081 * Removes the registry key with all subkeys. Parses full key name.
1084 * reg_key_name - full name of registry branch to delete. Ignored if is NULL,
1085 * empty, points to register key class, does not exist.
1087 void delete_registry_key(WCHAR
*reg_key_name
)
1089 WCHAR
*key_name
= NULL
;
1092 if (!reg_key_name
|| !reg_key_name
[0])
1095 if (!(key_class
= parse_key_name(reg_key_name
, &key_name
)))
1097 if (key_name
) *(key_name
- 1) = 0;
1098 error_exit(STRING_INVALID_SYSTEM_KEY
, reg_key_name
);
1101 if (!key_name
|| !*key_name
)
1102 error_exit(STRING_DELETE_FAILED
, reg_key_name
);
1104 RegDeleteTreeW(key_class
, key_name
);
1107 static void REGPROC_write_line(FILE *fp
, const WCHAR
*str
, BOOL unicode
)
1110 fwrite(str
, sizeof(WCHAR
), lstrlenW(str
), fp
);
1113 char *strA
= GetMultiByteString(str
);
1119 static WCHAR
*REGPROC_escape_string(WCHAR
*str
, size_t str_len
, size_t *line_len
)
1121 size_t i
, escape_count
, pos
;
1124 for (i
= 0, escape_count
= 0; i
< str_len
; i
++)
1130 if (c
== '\r' || c
== '\n' || c
== '\\' || c
== '"')
1134 buf
= malloc((str_len
+ escape_count
+ 1) * sizeof(WCHAR
));
1136 for (i
= 0, pos
= 0; i
< str_len
; i
++, pos
++)
1170 static size_t export_value_name(FILE *fp
, WCHAR
*name
, size_t len
, BOOL unicode
)
1172 static const WCHAR default_name
[] = L
"@=";
1177 WCHAR
*str
= REGPROC_escape_string(name
, len
, &line_len
);
1178 WCHAR
*buf
= malloc((line_len
+ 4) * sizeof(WCHAR
));
1179 line_len
= swprintf(buf
, line_len
+ 4, L
"\"%s\"=", str
);
1180 REGPROC_write_line(fp
, buf
, unicode
);
1186 line_len
= lstrlenW(default_name
);
1187 REGPROC_write_line(fp
, default_name
, unicode
);
1193 static void export_string_data(WCHAR
**buf
, WCHAR
*data
, size_t size
)
1195 size_t len
= 0, line_len
;
1199 len
= size
/ sizeof(WCHAR
) - 1;
1200 str
= REGPROC_escape_string(data
, len
, &line_len
);
1201 *buf
= malloc((line_len
+ 3) * sizeof(WCHAR
));
1202 swprintf(*buf
, line_len
+ 3, L
"\"%s\"", str
);
1206 static void export_dword_data(WCHAR
**buf
, DWORD
*data
)
1208 *buf
= malloc(15 * sizeof(WCHAR
));
1209 swprintf(*buf
, 15, L
"dword:%08x", *data
);
1212 static size_t export_hex_data_type(FILE *fp
, DWORD type
, BOOL unicode
)
1214 static const WCHAR hex
[] = L
"hex:";
1217 if (type
== REG_BINARY
)
1219 line_len
= lstrlenW(hex
);
1220 REGPROC_write_line(fp
, hex
, unicode
);
1224 WCHAR
*buf
= malloc(15 * sizeof(WCHAR
));
1225 line_len
= swprintf(buf
, 15, L
"hex(%x):", type
);
1226 REGPROC_write_line(fp
, buf
, unicode
);
1233 #define MAX_HEX_CHARS 77
1235 static void export_hex_data(FILE *fp
, WCHAR
**buf
, DWORD type
, DWORD line_len
,
1236 void *data
, DWORD size
, BOOL unicode
)
1238 size_t num_commas
, i
, pos
;
1240 line_len
+= export_hex_data_type(fp
, type
, unicode
);
1244 if (!unicode
&& (type
== REG_EXPAND_SZ
|| type
== REG_MULTI_SZ
))
1245 data
= GetMultiByteStringN(data
, size
/ sizeof(WCHAR
), &size
);
1247 num_commas
= size
- 1;
1248 *buf
= malloc(size
* 3 * sizeof(WCHAR
));
1250 for (i
= 0, pos
= 0; i
< size
; i
++)
1252 pos
+= swprintf(*buf
+ pos
, 3, L
"%02x", ((BYTE
*)data
)[i
]);
1253 if (i
== num_commas
) break;
1254 (*buf
)[pos
++] = ',';
1258 if (line_len
>= MAX_HEX_CHARS
)
1260 REGPROC_write_line(fp
, *buf
, unicode
);
1261 REGPROC_write_line(fp
, L
"\\\r\n ", unicode
);
1268 static void export_newline(FILE *fp
, BOOL unicode
)
1270 REGPROC_write_line(fp
, L
"\r\n", unicode
);
1273 static void export_data(FILE *fp
, WCHAR
*value_name
, DWORD value_len
, DWORD type
,
1274 void *data
, size_t size
, BOOL unicode
)
1277 size_t line_len
= export_value_name(fp
, value_name
, value_len
, unicode
);
1282 export_string_data(&buf
, data
, size
);
1287 export_dword_data(&buf
, data
);
1296 export_hex_data(fp
, &buf
, type
, line_len
, data
, size
, unicode
);
1300 if (size
|| type
== REG_SZ
)
1302 REGPROC_write_line(fp
, buf
, unicode
);
1306 export_newline(fp
, unicode
);
1309 static WCHAR
*build_subkey_path(WCHAR
*path
, DWORD path_len
, WCHAR
*subkey_name
, DWORD subkey_len
)
1313 subkey_path
= malloc((path_len
+ subkey_len
+ 2) * sizeof(WCHAR
));
1314 swprintf(subkey_path
, path_len
+ subkey_len
+ 2, L
"%s\\%s", path
, subkey_name
);
1319 static void export_key_name(FILE *fp
, WCHAR
*name
, BOOL unicode
)
1323 buf
= malloc((lstrlenW(name
) + 7) * sizeof(WCHAR
));
1324 swprintf(buf
, lstrlenW(name
) + 7, L
"\r\n[%s]\r\n", name
);
1325 REGPROC_write_line(fp
, buf
, unicode
);
1329 #define MAX_SUBKEY_LEN 257
1331 static int export_registry_data(FILE *fp
, HKEY key
, WCHAR
*path
, BOOL unicode
)
1334 DWORD max_value_len
= 256, value_len
;
1335 DWORD max_data_bytes
= 2048, data_size
;
1337 DWORD i
, type
, path_len
;
1338 WCHAR
*value_name
, *subkey_name
, *subkey_path
;
1342 export_key_name(fp
, path
, unicode
);
1344 value_name
= malloc(max_value_len
* sizeof(WCHAR
));
1345 data
= malloc(max_data_bytes
);
1350 value_len
= max_value_len
;
1351 data_size
= max_data_bytes
;
1352 rc
= RegEnumValueW(key
, i
, value_name
, &value_len
, NULL
, &type
, data
, &data_size
);
1353 if (rc
== ERROR_SUCCESS
)
1355 export_data(fp
, value_name
, value_len
, type
, data
, data_size
, unicode
);
1358 else if (rc
== ERROR_MORE_DATA
)
1360 if (data_size
> max_data_bytes
)
1362 max_data_bytes
= data_size
;
1363 data
= realloc(data
, max_data_bytes
);
1368 value_name
= realloc(value_name
, max_value_len
* sizeof(WCHAR
));
1377 subkey_name
= malloc(MAX_SUBKEY_LEN
* sizeof(WCHAR
));
1379 path_len
= lstrlenW(path
);
1384 subkey_len
= MAX_SUBKEY_LEN
;
1385 rc
= RegEnumKeyExW(key
, i
, subkey_name
, &subkey_len
, NULL
, NULL
, NULL
, NULL
);
1386 if (rc
== ERROR_SUCCESS
)
1388 subkey_path
= build_subkey_path(path
, path_len
, subkey_name
, subkey_len
);
1389 if (!RegOpenKeyExW(key
, subkey_name
, 0, KEY_READ
, &subkey
))
1391 export_registry_data(fp
, subkey
, subkey_path
, unicode
);
1392 RegCloseKey(subkey
);
1404 static FILE *REGPROC_open_export_file(WCHAR
*file_name
, BOOL unicode
)
1408 if (!lstrcmpW(file_name
, L
"-"))
1411 _setmode(_fileno(file
), _O_BINARY
);
1415 file
= _wfopen(file_name
, L
"wb");
1418 _wperror(L
"regedit");
1419 error_exit(STRING_CANNOT_OPEN_FILE
, file_name
);
1425 static const BYTE bom
[] = {0xff,0xfe};
1426 static const WCHAR header
[] = L
"Windows Registry Editor Version 5.00\r\n";
1428 fwrite(bom
, sizeof(BYTE
), ARRAY_SIZE(bom
), file
);
1429 fwrite(header
, sizeof(WCHAR
), lstrlenW(header
), file
);
1432 fputs("REGEDIT4\r\n", file
);
1437 static HKEY
open_export_key(HKEY key_class
, WCHAR
*subkey
, WCHAR
*path
)
1441 if (!RegOpenKeyExW(key_class
, subkey
, 0, KEY_READ
, &key
))
1444 output_message(STRING_OPEN_KEY_FAILED
, path
);
1448 static BOOL
export_key(WCHAR
*file_name
, WCHAR
*path
, BOOL unicode
)
1450 HKEY key_class
, key
;
1455 if (!(key_class
= parse_key_name(path
, &subkey
)))
1457 if (subkey
) *(subkey
- 1) = 0;
1458 output_message(STRING_INVALID_SYSTEM_KEY
, path
);
1462 if (!(key
= open_export_key(key_class
, subkey
, path
)))
1465 fp
= REGPROC_open_export_file(file_name
, unicode
);
1466 ret
= export_registry_data(fp
, key
, path
, unicode
);
1467 export_newline(fp
, unicode
);
1474 static BOOL
export_all(WCHAR
*file_name
, WCHAR
*path
, BOOL unicode
)
1478 HKEY classes
[] = {HKEY_LOCAL_MACHINE
, HKEY_USERS
}, key
;
1481 fp
= REGPROC_open_export_file(file_name
, unicode
);
1483 for (i
= 0; i
< ARRAY_SIZE(classes
); i
++)
1485 if (!(key
= open_export_key(classes
[i
], NULL
, path
)))
1491 class_name
= malloc((lstrlenW(reg_class_namesW
[i
]) + 1) * sizeof(WCHAR
));
1492 lstrcpyW(class_name
, reg_class_namesW
[i
]);
1494 export_registry_data(fp
, classes
[i
], class_name
, unicode
);
1500 export_newline(fp
, unicode
);
1506 BOOL
export_registry_key(WCHAR
*file_name
, WCHAR
*path
, DWORD format
)
1508 BOOL unicode
= (format
== REG_FORMAT_5
);
1511 return export_key(file_name
, path
, unicode
);
1513 return export_all(file_name
, path
, unicode
);