import.c 27 KB

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