1 /* 2 * Copyright 2017 Hugh McMaster 3 * 4 * This library is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Lesser General Public 6 * License as published by the Free Software Foundation; either 7 * version 2.1 of the License, or (at your option) any later version. 8 * 9 * This library is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * Lesser General Public License for more details. 13 * 14 * You should have received a copy of the GNU Lesser General Public 15 * License along with this library; if not, write to the Free Software 16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA 17 */ 18 19 #include <windef.h> 20 #include <winbase.h> 21 #include <winreg.h> 22 #include <stdio.h> 23 24 #include <wine/unicode.h> 25 #include <wine/debug.h> 26 27 #include "reg.h" 28 29 WINE_DEFAULT_DEBUG_CHANNEL(reg); 30 31 static WCHAR *GetWideString(const char *strA) 32 { 33 if (strA) 34 { 35 WCHAR *strW; 36 int len = MultiByteToWideChar(CP_ACP, 0, strA, -1, NULL, 0); 37 38 strW = heap_xalloc(len * sizeof(WCHAR)); 39 MultiByteToWideChar(CP_ACP, 0, strA, -1, strW, len); 40 return strW; 41 } 42 return NULL; 43 } 44 45 static WCHAR *GetWideStringN(const char *strA, int size, DWORD *len) 46 { 47 if (strA) 48 { 49 WCHAR *strW; 50 *len = MultiByteToWideChar(CP_ACP, 0, strA, size, NULL, 0); 51 52 strW = heap_xalloc(*len * sizeof(WCHAR)); 53 MultiByteToWideChar(CP_ACP, 0, strA, size, strW, *len); 54 return strW; 55 } 56 *len = 0; 57 return NULL; 58 } 59 60 static WCHAR *(*get_line)(FILE *); 61 62 /* parser definitions */ 63 enum parser_state 64 { 65 HEADER, /* parsing the registry file version header */ 66 PARSE_WIN31_LINE, /* parsing a Windows 3.1 registry line */ 67 LINE_START, /* at the beginning of a registry line */ 68 KEY_NAME, /* parsing a key name */ 69 DELETE_KEY, /* deleting a registry key */ 70 DEFAULT_VALUE_NAME, /* parsing a default value name */ 71 QUOTED_VALUE_NAME, /* parsing a double-quoted value name */ 72 DATA_START, /* preparing for data parsing operations */ 73 DELETE_VALUE, /* deleting a registry value */ 74 DATA_TYPE, /* parsing the registry data type */ 75 STRING_DATA, /* parsing REG_SZ data */ 76 DWORD_DATA, /* parsing DWORD data */ 77 HEX_DATA, /* parsing REG_BINARY, REG_NONE, REG_EXPAND_SZ or REG_MULTI_SZ data */ 78 EOL_BACKSLASH, /* preparing to parse multiple lines of hex data */ 79 HEX_MULTILINE, /* parsing multiple lines of hex data */ 80 UNKNOWN_DATA, /* parsing an unhandled or invalid data type */ 81 SET_VALUE, /* adding a value to the registry */ 82 NB_PARSER_STATES 83 }; 84 85 struct parser 86 { 87 FILE *file; /* pointer to a registry file */ 88 WCHAR two_wchars[2]; /* first two characters from the encoding check */ 89 BOOL is_unicode; /* parsing Unicode or ASCII data */ 90 short int reg_version; /* registry file version */ 91 HKEY hkey; /* current registry key */ 92 WCHAR *key_name; /* current key name */ 93 WCHAR *value_name; /* value name */ 94 DWORD parse_type; /* generic data type for parsing */ 95 DWORD data_type; /* data type */ 96 void *data; /* value data */ 97 DWORD data_size; /* size of the data (in bytes) */ 98 BOOL backslash; /* TRUE if the current line contains a backslash */ 99 enum parser_state state; /* current parser state */ 100 }; 101 102 typedef WCHAR *(*parser_state_func)(struct parser *parser, WCHAR *pos); 103 104 /* parser state machine functions */ 105 static WCHAR *header_state(struct parser *parser, WCHAR *pos); 106 static WCHAR *parse_win31_line_state(struct parser *parser, WCHAR *pos); 107 static WCHAR *line_start_state(struct parser *parser, WCHAR *pos); 108 static WCHAR *key_name_state(struct parser *parser, WCHAR *pos); 109 static WCHAR *delete_key_state(struct parser *parser, WCHAR *pos); 110 static WCHAR *default_value_name_state(struct parser *parser, WCHAR *pos); 111 static WCHAR *quoted_value_name_state(struct parser *parser, WCHAR *pos); 112 static WCHAR *data_start_state(struct parser *parser, WCHAR *pos); 113 static WCHAR *delete_value_state(struct parser *parser, WCHAR *pos); 114 static WCHAR *data_type_state(struct parser *parser, WCHAR *pos); 115 static WCHAR *string_data_state(struct parser *parser, WCHAR *pos); 116 static WCHAR *dword_data_state(struct parser *parser, WCHAR *pos); 117 static WCHAR *hex_data_state(struct parser *parser, WCHAR *pos); 118 static WCHAR *eol_backslash_state(struct parser *parser, WCHAR *pos); 119 static WCHAR *hex_multiline_state(struct parser *parser, WCHAR *pos); 120 static WCHAR *unknown_data_state(struct parser *parser, WCHAR *pos); 121 static WCHAR *set_value_state(struct parser *parser, WCHAR *pos); 122 123 static const parser_state_func parser_funcs[NB_PARSER_STATES] = 124 { 125 header_state, /* HEADER */ 126 parse_win31_line_state, /* PARSE_WIN31_LINE */ 127 line_start_state, /* LINE_START */ 128 key_name_state, /* KEY_NAME */ 129 delete_key_state, /* DELETE_KEY */ 130 default_value_name_state, /* DEFAULT_VALUE_NAME */ 131 quoted_value_name_state, /* QUOTED_VALUE_NAME */ 132 data_start_state, /* DATA_START */ 133 delete_value_state, /* DELETE_VALUE */ 134 data_type_state, /* DATA_TYPE */ 135 string_data_state, /* STRING_DATA */ 136 dword_data_state, /* DWORD_DATA */ 137 hex_data_state, /* HEX_DATA */ 138 eol_backslash_state, /* EOL_BACKSLASH */ 139 hex_multiline_state, /* HEX_MULTILINE */ 140 unknown_data_state, /* UNKNOWN_DATA */ 141 set_value_state, /* SET_VALUE */ 142 }; 143 144 /* set the new parser state and return the previous one */ 145 static inline enum parser_state set_state(struct parser *parser, enum parser_state state) 146 { 147 enum parser_state ret = parser->state; 148 parser->state = state; 149 return ret; 150 } 151 152 /****************************************************************************** 153 * Converts a hex representation of a DWORD into a DWORD. 154 */ 155 static BOOL convert_hex_to_dword(WCHAR *str, DWORD *dw) 156 { 157 WCHAR *p, *end; 158 int count = 0; 159 160 while (*str == ' ' || *str == '\t') str++; 161 if (!*str) goto error; 162 163 p = str; 164 while (isxdigitW(*p)) 165 { 166 count++; 167 p++; 168 } 169 if (count > 8) goto error; 170 171 end = p; 172 while (*p == ' ' || *p == '\t') p++; 173 if (*p && *p != ';') goto error; 174 175 *end = 0; 176 *dw = strtoulW(str, &end, 16); 177 return TRUE; 178 179 error: 180 return FALSE; 181 } 182 183 /****************************************************************************** 184 * Converts comma-separated hex data into a binary string and modifies 185 * the input parameter to skip the concatenating backslash, if found. 186 * 187 * Returns TRUE or FALSE to indicate whether parsing was successful. 188 */ 189 static BOOL convert_hex_csv_to_hex(struct parser *parser, WCHAR **str) 190 { 191 size_t size; 192 BYTE *d; 193 WCHAR *s; 194 195 parser->backslash = FALSE; 196 197 /* The worst case is 1 digit + 1 comma per byte */ 198 size = ((lstrlenW(*str) + 1) / 2) + parser->data_size; 199 parser->data = heap_xrealloc(parser->data, size); 200 201 s = *str; 202 d = (BYTE *)parser->data + parser->data_size; 203 204 while (*s) 205 { 206 WCHAR *end; 207 unsigned long wc; 208 209 wc = strtoulW(s, &end, 16); 210 if (wc > 0xff) return FALSE; 211 212 if (s == end && wc == 0) 213 { 214 while (*end == ' ' || *end == '\t') end++; 215 if (*end == '\\') 216 { 217 parser->backslash = TRUE; 218 *str = end + 1; 219 return TRUE; 220 } 221 else if (*end == ';') 222 return TRUE; 223 return FALSE; 224 } 225 226 *d++ = wc; 227 parser->data_size++; 228 229 if (*end && *end != ',') 230 { 231 while (*end == ' ' || *end == '\t') end++; 232 if (*end && *end != ';') return FALSE; 233 return TRUE; 234 } 235 236 if (*end) end++; 237 s = end; 238 } 239 240 return TRUE; 241 } 242 243 /****************************************************************************** 244 * Parses the data type of the registry value being imported and modifies 245 * the input parameter to skip the string representation of the data type. 246 * 247 * Returns TRUE or FALSE to indicate whether a data type was found. 248 */ 249 static BOOL parse_data_type(struct parser *parser, WCHAR **line) 250 { 251 struct data_type { const WCHAR *tag; int len; int type; int parse_type; }; 252 253 static const WCHAR quote[] = {'"'}; 254 static const WCHAR hex[] = {'h','e','x',':'}; 255 static const WCHAR dword[] = {'d','w','o','r','d',':'}; 256 static const WCHAR hexp[] = {'h','e','x','('}; 257 258 static const struct data_type data_types[] = { 259 /* tag len type parse type */ 260 { quote, 1, REG_SZ, REG_SZ }, 261 { hex, 4, REG_BINARY, REG_BINARY }, 262 { dword, 6, REG_DWORD, REG_DWORD }, 263 { hexp, 4, -1, REG_BINARY }, /* REG_NONE, REG_EXPAND_SZ, REG_MULTI_SZ */ 264 { NULL, 0, 0, 0 } 265 }; 266 267 const struct data_type *ptr; 268 269 for (ptr = data_types; ptr->tag; ptr++) 270 { 271 if (strncmpW(ptr->tag, *line, ptr->len)) 272 continue; 273 274 parser->parse_type = ptr->parse_type; 275 parser->data_type = ptr->parse_type; 276 *line += ptr->len; 277 278 if (ptr->type == -1) 279 { 280 WCHAR *end; 281 DWORD val; 282 283 if (!**line || tolowerW((*line)[1]) == 'x') 284 return FALSE; 285 286 /* "hex(xx):" is special */ 287 val = wcstoul(*line, &end, 16); 288 if (*end != ')' || *(end + 1) != ':' || (val == ~0u && errno == ERANGE)) 289 return FALSE; 290 291 parser->data_type = val; 292 *line = end + 2; 293 } 294 return TRUE; 295 } 296 return FALSE; 297 } 298 299 /****************************************************************************** 300 * Replaces escape sequences with their character equivalents and 301 * null-terminates the string on the first non-escaped double quote. 302 * 303 * Assigns a pointer to the remaining unparsed data in the line. 304 * Returns TRUE or FALSE to indicate whether a closing double quote was found. 305 */ 306 static BOOL unescape_string(WCHAR *str, WCHAR **unparsed) 307 { 308 int str_idx = 0; /* current character under analysis */ 309 int val_idx = 0; /* the last character of the unescaped string */ 310 int len = lstrlenW(str); 311 BOOL ret; 312 313 for (str_idx = 0; str_idx < len; str_idx++, val_idx++) 314 { 315 if (str[str_idx] == '\\') 316 { 317 str_idx++; 318 switch (str[str_idx]) 319 { 320 case 'n': 321 str[val_idx] = '\n'; 322 break; 323 case 'r': 324 str[val_idx] = '\r'; 325 break; 326 case '0': 327 str[val_idx] = '\0'; 328 break; 329 case '\\': 330 case '"': 331 str[val_idx] = str[str_idx]; 332 break; 333 default: 334 if (!str[str_idx]) return FALSE; 335 output_message(STRING_ESCAPE_SEQUENCE, str[str_idx]); 336 str[val_idx] = str[str_idx]; 337 break; 338 } 339 } 340 else if (str[str_idx] == '"') 341 break; 342 else 343 str[val_idx] = str[str_idx]; 344 } 345 346 ret = (str[str_idx] == '"'); 347 *unparsed = str + str_idx + 1; 348 str[val_idx] = '\0'; 349 return ret; 350 } 351 352 static HKEY parse_key_name(WCHAR *key_name, WCHAR **key_path) 353 { 354 if (!key_name) return 0; 355 356 *key_path = strchrW(key_name, '\\'); 357 if (*key_path) (*key_path)++; 358 359 return path_get_rootkey(key_name); 360 } 361 362 static void close_key(struct parser *parser) 363 { 364 if (parser->hkey) 365 { 366 heap_free(parser->key_name); 367 parser->key_name = NULL; 368 369 RegCloseKey(parser->hkey); 370 parser->hkey = NULL; 371 } 372 } 373 374 static LONG open_key(struct parser *parser, WCHAR *path) 375 { 376 HKEY key_class; 377 WCHAR *key_path; 378 LONG res; 379 380 close_key(parser); 381 382 /* Get the registry class */ 383 if (!path || !(key_class = parse_key_name(path, &key_path))) 384 return ERROR_INVALID_PARAMETER; 385 386 res = RegCreateKeyExW(key_class, key_path, 0, NULL, REG_OPTION_NON_VOLATILE, 387 KEY_ALL_ACCESS, NULL, &parser->hkey, NULL); 388 389 if (res == ERROR_SUCCESS) 390 { 391 parser->key_name = heap_xalloc((lstrlenW(path) + 1) * sizeof(WCHAR)); 392 lstrcpyW(parser->key_name, path); 393 } 394 else 395 parser->hkey = NULL; 396 397 return res; 398 } 399 400 static void free_parser_data(struct parser *parser) 401 { 402 if (parser->parse_type == REG_DWORD || parser->parse_type == REG_BINARY) 403 heap_free(parser->data); 404 405 parser->data = NULL; 406 parser->data_size = 0; 407 } 408 409 static void prepare_hex_string_data(struct parser *parser) 410 { 411 if (parser->data_type == REG_EXPAND_SZ || parser->data_type == REG_MULTI_SZ) 412 { 413 if (parser->is_unicode) 414 { 415 WCHAR *data = parser->data; 416 DWORD len = parser->data_size / sizeof(WCHAR); 417 418 if (data[len - 1] != 0) 419 { 420 data[len] = 0; 421 parser->data_size += sizeof(WCHAR); 422 } 423 } 424 else 425 { 426 BYTE *data = parser->data; 427 428 if (data[parser->data_size - 1] != 0) 429 { 430 data[parser->data_size] = 0; 431 parser->data_size++; 432 } 433 434 parser->data = GetWideStringN(parser->data, parser->data_size, &parser->data_size); 435 parser->data_size *= sizeof(WCHAR); 436 heap_free(data); 437 } 438 } 439 } 440 441 enum reg_versions { 442 REG_VERSION_31, 443 REG_VERSION_40, 444 REG_VERSION_50, 445 REG_VERSION_FUZZY, 446 REG_VERSION_INVALID 447 }; 448 449 static enum reg_versions parse_file_header(const WCHAR *s) 450 { 451 static const WCHAR header_31[] = {'R','E','G','E','D','I','T',0}; 452 static const WCHAR header_40[] = {'R','E','G','E','D','I','T','4',0}; 453 static const WCHAR header_50[] = {'W','i','n','d','o','w','s',' ', 454 'R','e','g','i','s','t','r','y',' ','E','d','i','t','o','r',' ', 455 'V','e','r','s','i','o','n',' ','5','.','0','0',0}; 456 457 while (*s == ' ' || *s == '\t') s++; 458 459 if (!strcmpW(s, header_31)) 460 return REG_VERSION_31; 461 462 if (!strcmpW(s, header_40)) 463 return REG_VERSION_40; 464 465 if (!strcmpW(s, header_50)) 466 return REG_VERSION_50; 467 468 /* The Windows version accepts registry file headers beginning with "REGEDIT" and ending 469 * with other characters, as long as "REGEDIT" appears at the start of the line. For example, 470 * "REGEDIT 4", "REGEDIT9" and "REGEDIT4FOO" are all treated as valid file headers. 471 * In all such cases, however, the contents of the registry file are not imported. 472 */ 473 if (!strncmpW(s, header_31, 7)) /* "REGEDIT" without NUL */ 474 return REG_VERSION_FUZZY; 475 476 return REG_VERSION_INVALID; 477 } 478 479 /* handler for parser HEADER state */ 480 static WCHAR *header_state(struct parser *parser, WCHAR *pos) 481 { 482 WCHAR *line, *header; 483 484 if (!(line = get_line(parser->file))) 485 return NULL; 486 487 if (!parser->is_unicode) 488 { 489 header = heap_xalloc((lstrlenW(line) + 3) * sizeof(WCHAR)); 490 header[0] = parser->two_wchars[0]; 491 header[1] = parser->two_wchars[1]; 492 lstrcpyW(header + 2, line); 493 parser->reg_version = parse_file_header(header); 494 heap_free(header); 495 } 496 else parser->reg_version = parse_file_header(line); 497 498 switch (parser->reg_version) 499 { 500 case REG_VERSION_31: 501 set_state(parser, PARSE_WIN31_LINE); 502 break; 503 case REG_VERSION_40: 504 case REG_VERSION_50: 505 set_state(parser, LINE_START); 506 break; 507 default: 508 get_line(NULL); /* Reset static variables */ 509 return NULL; 510 } 511 512 return line; 513 } 514 515 /* handler for parser PARSE_WIN31_LINE state */ 516 static WCHAR *parse_win31_line_state(struct parser *parser, WCHAR *pos) 517 { 518 WCHAR *line, *value; 519 static WCHAR hkcr[] = {'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T'}; 520 unsigned int key_end = 0; 521 522 if (!(line = get_line(parser->file))) 523 return NULL; 524 525 if (strncmpW(line, hkcr, ARRAY_SIZE(hkcr))) 526 return line; 527 528 /* get key name */ 529 while (line[key_end] && !isspaceW(line[key_end])) key_end++; 530 531 value = line + key_end; 532 while (*value == ' ' || *value == '\t') value++; 533 534 if (*value == '=') value++; 535 if (*value == ' ') value++; /* at most one space is skipped */ 536 537 line[key_end] = 0; 538 539 if (open_key(parser, line) != ERROR_SUCCESS) 540 { 541 output_message(STRING_OPEN_KEY_FAILED, line); 542 return line; 543 } 544 545 parser->value_name = NULL; 546 parser->data_type = REG_SZ; 547 parser->data = value; 548 parser->data_size = (lstrlenW(value) + 1) * sizeof(WCHAR); 549 550 set_state(parser, SET_VALUE); 551 return value; 552 } 553 554 /* handler for parser LINE_START state */ 555 static WCHAR *line_start_state(struct parser *parser, WCHAR *pos) 556 { 557 WCHAR *line, *p; 558 559 if (!(line = get_line(parser->file))) 560 return NULL; 561 562 for (p = line; *p; p++) 563 { 564 switch (*p) 565 { 566 case '[': 567 set_state(parser, KEY_NAME); 568 return p + 1; 569 case '@': 570 set_state(parser, DEFAULT_VALUE_NAME); 571 return p; 572 case '"': 573 set_state(parser, QUOTED_VALUE_NAME); 574 return p + 1; 575 case ' ': 576 case '\t': 577 break; 578 default: 579 return p; 580 } 581 } 582 583 return p; 584 } 585 586 /* handler for parser KEY_NAME state */ 587 static WCHAR *key_name_state(struct parser *parser, WCHAR *pos) 588 { 589 WCHAR *p = pos, *key_end; 590 591 if (*p == ' ' || *p == '\t' || !(key_end = strrchrW(p, ']'))) 592 goto done; 593 594 *key_end = 0; 595 596 if (*p == '-') 597 { 598 set_state(parser, DELETE_KEY); 599 return p + 1; 600 } 601 else if (open_key(parser, p) != ERROR_SUCCESS) 602 output_message(STRING_OPEN_KEY_FAILED, p); 603 604 done: 605 set_state(parser, LINE_START); 606 return p; 607 } 608 609 /* handler for parser DELETE_KEY state */ 610 static WCHAR *delete_key_state(struct parser *parser, WCHAR *pos) 611 { 612 WCHAR *p = pos; 613 614 close_key(parser); 615 616 if (*p == 'H' || *p == 'h') 617 { 618 HKEY root; 619 WCHAR *path; 620 621 root = parse_key_name(p, &path); 622 623 if (root && path && *path) 624 RegDeleteTreeW(root, path); 625 } 626 627 set_state(parser, LINE_START); 628 return p; 629 } 630 631 /* handler for parser DEFAULT_VALUE_NAME state */ 632 static WCHAR *default_value_name_state(struct parser *parser, WCHAR *pos) 633 { 634 heap_free(parser->value_name); 635 parser->value_name = NULL; 636 637 set_state(parser, DATA_START); 638 return pos + 1; 639 } 640 641 /* handler for parser QUOTED_VALUE_NAME state */ 642 static WCHAR *quoted_value_name_state(struct parser *parser, WCHAR *pos) 643 { 644 WCHAR *val_name = pos, *p; 645 646 if (parser->value_name) 647 { 648 heap_free(parser->value_name); 649 parser->value_name = NULL; 650 } 651 652 if (!unescape_string(val_name, &p)) 653 goto invalid; 654 655 /* copy the value name in case we need to parse multiple lines and the buffer is overwritten */ 656 parser->value_name = heap_xalloc((lstrlenW(val_name) + 1) * sizeof(WCHAR)); 657 lstrcpyW(parser->value_name, val_name); 658 659 set_state(parser, DATA_START); 660 return p; 661 662 invalid: 663 set_state(parser, LINE_START); 664 return val_name; 665 } 666 667 /* handler for parser DATA_START state */ 668 static WCHAR *data_start_state(struct parser *parser, WCHAR *pos) 669 { 670 WCHAR *p = pos; 671 unsigned int len; 672 673 while (*p == ' ' || *p == '\t') p++; 674 if (*p != '=') goto invalid; 675 p++; 676 while (*p == ' ' || *p == '\t') p++; 677 678 /* trim trailing whitespace */ 679 len = strlenW(p); 680 while (len > 0 && (p[len - 1] == ' ' || p[len - 1] == '\t')) len--; 681 p[len] = 0; 682 683 if (*p == '-') 684 set_state(parser, DELETE_VALUE); 685 else 686 set_state(parser, DATA_TYPE); 687 return p; 688 689 invalid: 690 set_state(parser, LINE_START); 691 return p; 692 } 693 694 /* handler for parser DELETE_VALUE state */ 695 static WCHAR *delete_value_state(struct parser *parser, WCHAR *pos) 696 { 697 WCHAR *p = pos + 1; 698 699 while (*p == ' ' || *p == '\t') p++; 700 if (*p && *p != ';') goto done; 701 702 RegDeleteValueW(parser->hkey, parser->value_name); 703 704 done: 705 set_state(parser, LINE_START); 706 return p; 707 } 708 709 /* handler for parser DATA_TYPE state */ 710 static WCHAR *data_type_state(struct parser *parser, WCHAR *pos) 711 { 712 WCHAR *line = pos; 713 714 if (!parse_data_type(parser, &line)) 715 { 716 set_state(parser, LINE_START); 717 return line; 718 } 719 720 switch (parser->parse_type) 721 { 722 case REG_SZ: 723 set_state(parser, STRING_DATA); 724 break; 725 case REG_DWORD: 726 set_state(parser, DWORD_DATA); 727 break; 728 case REG_BINARY: /* all hex data types, including undefined */ 729 set_state(parser, HEX_DATA); 730 break; 731 default: 732 set_state(parser, UNKNOWN_DATA); 733 } 734 735 return line; 736 } 737 738 /* handler for parser STRING_DATA state */ 739 static WCHAR *string_data_state(struct parser *parser, WCHAR *pos) 740 { 741 WCHAR *line; 742 743 parser->data = pos; 744 745 if (!unescape_string(parser->data, &line)) 746 goto invalid; 747 748 while (*line == ' ' || *line == '\t') line++; 749 if (*line && *line != ';') goto invalid; 750 751 parser->data_size = (lstrlenW(parser->data) + 1) * sizeof(WCHAR); 752 753 set_state(parser, SET_VALUE); 754 return line; 755 756 invalid: 757 free_parser_data(parser); 758 set_state(parser, LINE_START); 759 return line; 760 } 761 762 /* handler for parser DWORD_DATA state */ 763 static WCHAR *dword_data_state(struct parser *parser, WCHAR *pos) 764 { 765 WCHAR *line = pos; 766 767 parser->data = heap_xalloc(sizeof(DWORD)); 768 769 if (!convert_hex_to_dword(line, parser->data)) 770 goto invalid; 771 772 parser->data_size = sizeof(DWORD); 773 774 set_state(parser, SET_VALUE); 775 return line; 776 777 invalid: 778 free_parser_data(parser); 779 set_state(parser, LINE_START); 780 return line; 781 } 782 783 /* handler for parser HEX_DATA state */ 784 static WCHAR *hex_data_state(struct parser *parser, WCHAR *pos) 785 { 786 WCHAR *line = pos; 787 788 if (!*line) 789 goto set_value; 790 791 if (!convert_hex_csv_to_hex(parser, &line)) 792 goto invalid; 793 794 if (parser->backslash) 795 { 796 set_state(parser, EOL_BACKSLASH); 797 return line; 798 } 799 800 prepare_hex_string_data(parser); 801 802 set_value: 803 set_state(parser, SET_VALUE); 804 return line; 805 806 invalid: 807 free_parser_data(parser); 808 set_state(parser, LINE_START); 809 return line; 810 } 811 812 /* handler for parser EOL_BACKSLASH state */ 813 static WCHAR *eol_backslash_state(struct parser *parser, WCHAR *pos) 814 { 815 WCHAR *p = pos; 816 817 while (*p == ' ' || *p == '\t') p++; 818 if (*p && *p != ';') goto invalid; 819 820 set_state(parser, HEX_MULTILINE); 821 return pos; 822 823 invalid: 824 free_parser_data(parser); 825 set_state(parser, LINE_START); 826 return p; 827 } 828 829 /* handler for parser HEX_MULTILINE state */ 830 static WCHAR *hex_multiline_state(struct parser *parser, WCHAR *pos) 831 { 832 WCHAR *line; 833 834 if (!(line = get_line(parser->file))) 835 { 836 prepare_hex_string_data(parser); 837 set_state(parser, SET_VALUE); 838 return pos; 839 } 840 841 while (*line == ' ' || *line == '\t') line++; 842 if (!*line || *line == ';') return line; 843 844 if (!isxdigitW(*line)) goto invalid; 845 846 set_state(parser, HEX_DATA); 847 return line; 848 849 invalid: 850 free_parser_data(parser); 851 set_state(parser, LINE_START); 852 return line; 853 } 854 855 /* handler for parser UNKNOWN_DATA state */ 856 static WCHAR *unknown_data_state(struct parser *parser, WCHAR *pos) 857 { 858 FIXME("Unknown registry data type [0x%x]\n", parser->data_type); 859 860 set_state(parser, LINE_START); 861 return pos; 862 } 863 864 /* handler for parser SET_VALUE state */ 865 static WCHAR *set_value_state(struct parser *parser, WCHAR *pos) 866 { 867 RegSetValueExW(parser->hkey, parser->value_name, 0, parser->data_type, 868 parser->data, parser->data_size); 869 870 free_parser_data(parser); 871 872 if (parser->reg_version == REG_VERSION_31) 873 set_state(parser, PARSE_WIN31_LINE); 874 else 875 set_state(parser, LINE_START); 876 877 return pos; 878 } 879 880 #define REG_VAL_BUF_SIZE 4096 881 882 static WCHAR *get_lineA(FILE *fp) 883 { 884 static WCHAR *lineW; 885 static size_t size; 886 static char *buf, *next; 887 char *line; 888 889 heap_free(lineW); 890 891 if (!fp) goto cleanup; 892 893 if (!size) 894 { 895 size = REG_VAL_BUF_SIZE; 896 buf = heap_xalloc(size); 897 *buf = 0; 898 next = buf; 899 } 900 line = next; 901 902 while (next) 903 { 904 char *p = strpbrk(line, "\r\n"); 905 if (!p) 906 { 907 size_t len, count; 908 len = strlen(next); 909 memmove(buf, next, len + 1); 910 if (size - len < 3) 911 { 912 size *= 2; 913 buf = heap_xrealloc(buf, size); 914 } 915 if (!(count = fread(buf + len, 1, size - len - 1, fp))) 916 { 917 next = NULL; 918 lineW = GetWideString(buf); 919 return lineW; 920 } 921 buf[len + count] = 0; 922 next = buf; 923 line = buf; 924 continue; 925 } 926 next = p + 1; 927 if (*p == '\r' && *(p + 1) == '\n') next++; 928 *p = 0; 929 lineW = GetWideString(line); 930 return lineW; 931 } 932 933 cleanup: 934 lineW = NULL; 935 if (size) heap_free(buf); 936 size = 0; 937 return NULL; 938 } 939 940 static WCHAR *get_lineW(FILE *fp) 941 { 942 static size_t size; 943 static WCHAR *buf, *next; 944 WCHAR *line; 945 946 if (!fp) goto cleanup; 947 948 if (!size) 949 { 950 size = REG_VAL_BUF_SIZE; 951 buf = heap_xalloc(size * sizeof(WCHAR)); 952 *buf = 0; 953 next = buf; 954 } 955 line = next; 956 957 while (next) 958 { 959 static const WCHAR line_endings[] = {'\r','\n',0}; 960 WCHAR *p = strpbrkW(line, line_endings); 961 if (!p) 962 { 963 size_t len, count; 964 len = strlenW(next); 965 memmove(buf, next, (len + 1) * sizeof(WCHAR)); 966 if (size - len < 3) 967 { 968 size *= 2; 969 buf = heap_xrealloc(buf, size * sizeof(WCHAR)); 970 } 971 if (!(count = fread(buf + len, sizeof(WCHAR), size - len - 1, fp))) 972 { 973 next = NULL; 974 return buf; 975 } 976 buf[len + count] = 0; 977 next = buf; 978 line = buf; 979 continue; 980 } 981 next = p + 1; 982 if (*p == '\r' && *(p + 1) == '\n') next++; 983 *p = 0; 984 return line; 985 } 986 987 cleanup: 988 if (size) heap_free(buf); 989 size = 0; 990 return NULL; 991 } 992 993 int reg_import(const WCHAR *filename) 994 { 995 FILE *fp; 996 static const WCHAR rb_mode[] = {'r','b',0}; 997 BYTE s[2]; 998 struct parser parser; 999 WCHAR *pos; 1000 1001 fp = _wfopen(filename, rb_mode); 1002 if (!fp) 1003 { 1004 output_message(STRING_FILE_NOT_FOUND, filename); 1005 return 1; 1006 } 1007 1008 if (fread(s, sizeof(WCHAR), 1, fp) != 1) 1009 goto error; 1010 1011 parser.is_unicode = (s[0] == 0xff && s[1] == 0xfe); 1012 get_line = parser.is_unicode ? get_lineW : get_lineA; 1013 1014 parser.file = fp; 1015 parser.two_wchars[0] = s[0]; 1016 parser.two_wchars[1] = s[1]; 1017 parser.reg_version = -1; 1018 parser.hkey = NULL; 1019 parser.key_name = NULL; 1020 parser.value_name = NULL; 1021 parser.parse_type = 0; 1022 parser.data_type = 0; 1023 parser.data = NULL; 1024 parser.data_size = 0; 1025 parser.backslash = FALSE; 1026 parser.state = HEADER; 1027 1028 pos = parser.two_wchars; 1029 1030 /* parser main loop */ 1031 while (pos) 1032 pos = (parser_funcs[parser.state])(&parser, pos); 1033 1034 if (parser.reg_version == REG_VERSION_INVALID) 1035 goto error; 1036 1037 heap_free(parser.value_name); 1038 close_key(&parser); 1039 1040 fclose(fp); 1041 return 0; 1042 1043 error: 1044 fclose(fp); 1045 return 1; 1046 } 1047