import.c 27 KB

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