regproc.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570
  1. /*
  2. * Registry processing routines. Routines, common for registry
  3. * processing frontends.
  4. *
  5. * Copyright 1999 Sylvain St-Germain
  6. * Copyright 2002 Andriy Palamarchuk
  7. * Copyright 2008 Alexander N. Sørnes <alex@thehandofagony.com>
  8. *
  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.
  13. *
  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.
  18. *
  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
  22. */
  23. #include <errno.h>
  24. #include <stdlib.h>
  25. #include <fcntl.h>
  26. #include <io.h>
  27. #include <windows.h>
  28. #include <commctrl.h>
  29. #include <wine/debug.h>
  30. #include <wine/heap.h>
  31. #include "main.h"
  32. WINE_DEFAULT_DEBUG_CHANNEL(regedit);
  33. #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
  37. };
  38. void *heap_xalloc(size_t size)
  39. {
  40. void *buf = heap_alloc(size);
  41. if (!buf)
  42. {
  43. ERR("Out of memory!\n");
  44. exit(1);
  45. }
  46. return buf;
  47. }
  48. void *heap_xrealloc(void *buf, size_t size)
  49. {
  50. void *new_buf = heap_realloc(buf, size);
  51. if (!new_buf)
  52. {
  53. ERR("Out of memory!\n");
  54. exit(1);
  55. }
  56. return new_buf;
  57. }
  58. /******************************************************************************
  59. * Allocates memory and converts input from multibyte to wide chars
  60. * Returned string must be freed by the caller
  61. */
  62. static WCHAR* GetWideString(const char* strA)
  63. {
  64. if(strA)
  65. {
  66. WCHAR* strW;
  67. int len = MultiByteToWideChar(CP_ACP, 0, strA, -1, NULL, 0);
  68. strW = heap_xalloc(len * sizeof(WCHAR));
  69. MultiByteToWideChar(CP_ACP, 0, strA, -1, strW, len);
  70. return strW;
  71. }
  72. return NULL;
  73. }
  74. /******************************************************************************
  75. * Allocates memory and converts input from multibyte to wide chars
  76. * Returned string must be freed by the caller
  77. */
  78. static WCHAR* GetWideStringN(const char* strA, int chars, DWORD *len)
  79. {
  80. if(strA)
  81. {
  82. WCHAR* strW;
  83. *len = MultiByteToWideChar(CP_ACP, 0, strA, chars, NULL, 0);
  84. strW = heap_xalloc(*len * sizeof(WCHAR));
  85. MultiByteToWideChar(CP_ACP, 0, strA, chars, strW, *len);
  86. return strW;
  87. }
  88. *len = 0;
  89. return NULL;
  90. }
  91. /******************************************************************************
  92. * Allocates memory and converts input from wide chars to multibyte
  93. * Returned string must be freed by the caller
  94. */
  95. char* GetMultiByteString(const WCHAR* strW)
  96. {
  97. if(strW)
  98. {
  99. char* strA;
  100. int len = WideCharToMultiByte(CP_ACP, 0, strW, -1, NULL, 0, NULL, NULL);
  101. strA = heap_xalloc(len);
  102. WideCharToMultiByte(CP_ACP, 0, strW, -1, strA, len, NULL, NULL);
  103. return strA;
  104. }
  105. return NULL;
  106. }
  107. /******************************************************************************
  108. * Allocates memory and converts input from wide chars to multibyte
  109. * Returned string must be freed by the caller
  110. */
  111. static char* GetMultiByteStringN(const WCHAR* strW, int chars, DWORD* len)
  112. {
  113. if(strW)
  114. {
  115. char* strA;
  116. *len = WideCharToMultiByte(CP_ACP, 0, strW, chars, NULL, 0, NULL, NULL);
  117. strA = heap_xalloc(*len);
  118. WideCharToMultiByte(CP_ACP, 0, strW, chars, strA, *len, NULL, NULL);
  119. return strA;
  120. }
  121. *len = 0;
  122. return NULL;
  123. }
  124. static WCHAR *(*get_line)(FILE *);
  125. /* parser definitions */
  126. enum parser_state
  127. {
  128. HEADER, /* parsing the registry file version header */
  129. PARSE_WIN31_LINE, /* parsing a Windows 3.1 registry line */
  130. LINE_START, /* at the beginning of a registry line */
  131. KEY_NAME, /* parsing a key name */
  132. DELETE_KEY, /* deleting a registry key */
  133. DEFAULT_VALUE_NAME, /* parsing a default value name */
  134. QUOTED_VALUE_NAME, /* parsing a double-quoted value name */
  135. DATA_START, /* preparing for data parsing operations */
  136. DELETE_VALUE, /* deleting a registry value */
  137. DATA_TYPE, /* parsing the registry data type */
  138. STRING_DATA, /* parsing REG_SZ data */
  139. DWORD_DATA, /* parsing DWORD data */
  140. HEX_DATA, /* parsing REG_BINARY, REG_NONE, REG_EXPAND_SZ or REG_MULTI_SZ data */
  141. EOL_BACKSLASH, /* preparing to parse multiple lines of hex data */
  142. HEX_MULTILINE, /* parsing multiple lines of hex data */
  143. UNKNOWN_DATA, /* parsing an unhandled or invalid data type */
  144. SET_VALUE, /* adding a value to the registry */
  145. NB_PARSER_STATES
  146. };
  147. struct parser
  148. {
  149. FILE *file; /* pointer to a registry file */
  150. WCHAR two_wchars[2]; /* first two characters from the encoding check */
  151. BOOL is_unicode; /* parsing Unicode or ASCII data */
  152. short int reg_version; /* registry file version */
  153. HKEY hkey; /* current registry key */
  154. WCHAR *key_name; /* current key name */
  155. WCHAR *value_name; /* value name */
  156. DWORD parse_type; /* generic data type for parsing */
  157. DWORD data_type; /* data type */
  158. void *data; /* value data */
  159. DWORD data_size; /* size of the data (in bytes) */
  160. BOOL backslash; /* TRUE if the current line contains a backslash */
  161. enum parser_state state; /* current parser state */
  162. };
  163. typedef WCHAR *(*parser_state_func)(struct parser *parser, WCHAR *pos);
  164. /* parser state machine functions */
  165. static WCHAR *header_state(struct parser *parser, WCHAR *pos);
  166. static WCHAR *parse_win31_line_state(struct parser *parser, WCHAR *pos);
  167. static WCHAR *line_start_state(struct parser *parser, WCHAR *pos);
  168. static WCHAR *key_name_state(struct parser *parser, WCHAR *pos);
  169. static WCHAR *delete_key_state(struct parser *parser, WCHAR *pos);
  170. static WCHAR *default_value_name_state(struct parser *parser, WCHAR *pos);
  171. static WCHAR *quoted_value_name_state(struct parser *parser, WCHAR *pos);
  172. static WCHAR *data_start_state(struct parser *parser, WCHAR *pos);
  173. static WCHAR *delete_value_state(struct parser *parser, WCHAR *pos);
  174. static WCHAR *data_type_state(struct parser *parser, WCHAR *pos);
  175. static WCHAR *string_data_state(struct parser *parser, WCHAR *pos);
  176. static WCHAR *dword_data_state(struct parser *parser, WCHAR *pos);
  177. static WCHAR *hex_data_state(struct parser *parser, WCHAR *pos);
  178. static WCHAR *eol_backslash_state(struct parser *parser, WCHAR *pos);
  179. static WCHAR *hex_multiline_state(struct parser *parser, WCHAR *pos);
  180. static WCHAR *unknown_data_state(struct parser *parser, WCHAR *pos);
  181. static WCHAR *set_value_state(struct parser *parser, WCHAR *pos);
  182. static const parser_state_func parser_funcs[NB_PARSER_STATES] =
  183. {
  184. header_state, /* HEADER */
  185. parse_win31_line_state, /* PARSE_WIN31_LINE */
  186. line_start_state, /* LINE_START */
  187. key_name_state, /* KEY_NAME */
  188. delete_key_state, /* DELETE_KEY */
  189. default_value_name_state, /* DEFAULT_VALUE_NAME */
  190. quoted_value_name_state, /* QUOTED_VALUE_NAME */
  191. data_start_state, /* DATA_START */
  192. delete_value_state, /* DELETE_VALUE */
  193. data_type_state, /* DATA_TYPE */
  194. string_data_state, /* STRING_DATA */
  195. dword_data_state, /* DWORD_DATA */
  196. hex_data_state, /* HEX_DATA */
  197. eol_backslash_state, /* EOL_BACKSLASH */
  198. hex_multiline_state, /* HEX_MULTILINE */
  199. unknown_data_state, /* UNKNOWN_DATA */
  200. set_value_state, /* SET_VALUE */
  201. };
  202. /* set the new parser state and return the previous one */
  203. static inline enum parser_state set_state(struct parser *parser, enum parser_state state)
  204. {
  205. enum parser_state ret = parser->state;
  206. parser->state = state;
  207. return ret;
  208. }
  209. /******************************************************************************
  210. * Converts a hex representation of a DWORD into a DWORD.
  211. */
  212. static BOOL convert_hex_to_dword(WCHAR *str, DWORD *dw)
  213. {
  214. WCHAR *p, *end;
  215. int count = 0;
  216. while (*str == ' ' || *str == '\t') str++;
  217. if (!*str) goto error;
  218. p = str;
  219. while (iswxdigit(*p))
  220. {
  221. count++;
  222. p++;
  223. }
  224. if (count > 8) goto error;
  225. end = p;
  226. while (*p == ' ' || *p == '\t') p++;
  227. if (*p && *p != ';') goto error;
  228. *end = 0;
  229. *dw = wcstoul(str, &end, 16);
  230. return TRUE;
  231. error:
  232. return FALSE;
  233. }
  234. /******************************************************************************
  235. * Converts comma-separated hex data into a binary string and modifies
  236. * the input parameter to skip the concatenating backslash, if found.
  237. *
  238. * Returns TRUE or FALSE to indicate whether parsing was successful.
  239. */
  240. static BOOL convert_hex_csv_to_hex(struct parser *parser, WCHAR **str)
  241. {
  242. size_t size;
  243. BYTE *d;
  244. WCHAR *s;
  245. parser->backslash = FALSE;
  246. /* The worst case is 1 digit + 1 comma per byte */
  247. size = ((lstrlenW(*str) + 1) / 2) + parser->data_size;
  248. parser->data = heap_xrealloc(parser->data, size);
  249. s = *str;
  250. d = (BYTE *)parser->data + parser->data_size;
  251. while (*s)
  252. {
  253. WCHAR *end;
  254. unsigned long wc;
  255. wc = wcstoul(s, &end, 16);
  256. if (wc > 0xff) return FALSE;
  257. if (s == end && wc == 0)
  258. {
  259. while (*end == ' ' || *end == '\t') end++;
  260. if (*end == '\\')
  261. {
  262. parser->backslash = TRUE;
  263. *str = end + 1;
  264. return TRUE;
  265. }
  266. else if (*end == ';')
  267. return TRUE;
  268. return FALSE;
  269. }
  270. *d++ = wc;
  271. parser->data_size++;
  272. if (*end && *end != ',')
  273. {
  274. while (*end == ' ' || *end == '\t') end++;
  275. if (*end && *end != ';') return FALSE;
  276. return TRUE;
  277. }
  278. if (*end) end++;
  279. s = end;
  280. }
  281. return TRUE;
  282. }
  283. /******************************************************************************
  284. * Parses the data type of the registry value being imported and modifies
  285. * the input parameter to skip the string representation of the data type.
  286. *
  287. * Returns TRUE or FALSE to indicate whether a data type was found.
  288. */
  289. static BOOL parse_data_type(struct parser *parser, WCHAR **line)
  290. {
  291. struct data_type { const WCHAR *tag; int len; int type; int parse_type; };
  292. static const WCHAR quote[] = {'"'};
  293. static const WCHAR hex[] = {'h','e','x',':'};
  294. static const WCHAR dword[] = {'d','w','o','r','d',':'};
  295. static const WCHAR hexp[] = {'h','e','x','('};
  296. static const struct data_type data_types[] = {
  297. /* tag len type parse type */
  298. { quote, 1, REG_SZ, REG_SZ },
  299. { hex, 4, REG_BINARY, REG_BINARY },
  300. { dword, 6, REG_DWORD, REG_DWORD },
  301. { hexp, 4, -1, REG_BINARY }, /* REG_NONE, REG_EXPAND_SZ, REG_MULTI_SZ */
  302. { NULL, 0, 0, 0 }
  303. };
  304. const struct data_type *ptr;
  305. for (ptr = data_types; ptr->tag; ptr++)
  306. {
  307. if (wcsncmp(ptr->tag, *line, ptr->len))
  308. continue;
  309. parser->parse_type = ptr->parse_type;
  310. parser->data_type = ptr->parse_type;
  311. *line += ptr->len;
  312. if (ptr->type == -1)
  313. {
  314. WCHAR *end;
  315. DWORD val;
  316. if (!**line || towlower((*line)[1]) == 'x')
  317. return FALSE;
  318. /* "hex(xx):" is special */
  319. val = wcstoul(*line, &end, 16);
  320. if (*end != ')' || *(end + 1) != ':' || (val == ~0u && errno == ERANGE))
  321. return FALSE;
  322. parser->data_type = val;
  323. *line = end + 2;
  324. }
  325. return TRUE;
  326. }
  327. return FALSE;
  328. }
  329. /******************************************************************************
  330. * Replaces escape sequences with their character equivalents and
  331. * null-terminates the string on the first non-escaped double quote.
  332. *
  333. * Assigns a pointer to the remaining unparsed data in the line.
  334. * Returns TRUE or FALSE to indicate whether a closing double quote was found.
  335. */
  336. static BOOL REGPROC_unescape_string(WCHAR *str, WCHAR **unparsed)
  337. {
  338. int str_idx = 0; /* current character under analysis */
  339. int val_idx = 0; /* the last character of the unescaped string */
  340. int len = lstrlenW(str);
  341. BOOL ret;
  342. for (str_idx = 0; str_idx < len; str_idx++, val_idx++) {
  343. if (str[str_idx] == '\\') {
  344. str_idx++;
  345. switch (str[str_idx]) {
  346. case 'n':
  347. str[val_idx] = '\n';
  348. break;
  349. case 'r':
  350. str[val_idx] = '\r';
  351. break;
  352. case '0':
  353. return FALSE;
  354. case '\\':
  355. case '"':
  356. str[val_idx] = str[str_idx];
  357. break;
  358. default:
  359. if (!str[str_idx]) return FALSE;
  360. output_message(STRING_ESCAPE_SEQUENCE, str[str_idx]);
  361. str[val_idx] = str[str_idx];
  362. break;
  363. }
  364. } else if (str[str_idx] == '"') {
  365. break;
  366. } else {
  367. str[val_idx] = str[str_idx];
  368. }
  369. }
  370. ret = (str[str_idx] == '"');
  371. *unparsed = str + str_idx + 1;
  372. str[val_idx] = '\0';
  373. return ret;
  374. }
  375. static HKEY parse_key_name(WCHAR *key_name, WCHAR **key_path)
  376. {
  377. unsigned int i;
  378. if (!key_name) return 0;
  379. *key_path = wcschr(key_name, '\\');
  380. if (*key_path) (*key_path)++;
  381. for (i = 0; i < ARRAY_SIZE(reg_class_keys); i++)
  382. {
  383. int len = lstrlenW(reg_class_namesW[i]);
  384. if (!wcsnicmp(key_name, reg_class_namesW[i], len) &&
  385. (key_name[len] == 0 || key_name[len] == '\\'))
  386. {
  387. return reg_class_keys[i];
  388. }
  389. }
  390. return 0;
  391. }
  392. static void close_key(struct parser *parser)
  393. {
  394. if (parser->hkey)
  395. {
  396. heap_free(parser->key_name);
  397. parser->key_name = NULL;
  398. RegCloseKey(parser->hkey);
  399. parser->hkey = NULL;
  400. }
  401. }
  402. /******************************************************************************
  403. * Opens the registry key given by the input path.
  404. * This key must be closed by calling close_key().
  405. */
  406. static LONG open_key(struct parser *parser, WCHAR *path)
  407. {
  408. HKEY key_class;
  409. WCHAR *key_path;
  410. LONG res;
  411. close_key(parser);
  412. /* Get the registry class */
  413. if (!path || !(key_class = parse_key_name(path, &key_path)))
  414. return ERROR_INVALID_PARAMETER;
  415. res = RegCreateKeyExW(key_class, key_path, 0, NULL, REG_OPTION_NON_VOLATILE,
  416. KEY_ALL_ACCESS, NULL, &parser->hkey, NULL);
  417. if (res == ERROR_SUCCESS)
  418. {
  419. parser->key_name = heap_xalloc((lstrlenW(path) + 1) * sizeof(WCHAR));
  420. lstrcpyW(parser->key_name, path);
  421. }
  422. else
  423. parser->hkey = NULL;
  424. return res;
  425. }
  426. static void free_parser_data(struct parser *parser)
  427. {
  428. if (parser->parse_type == REG_DWORD || parser->parse_type == REG_BINARY)
  429. heap_free(parser->data);
  430. parser->data = NULL;
  431. parser->data_size = 0;
  432. }
  433. static void prepare_hex_string_data(struct parser *parser)
  434. {
  435. if (parser->data_type == REG_EXPAND_SZ || parser->data_type == REG_MULTI_SZ ||
  436. parser->data_type == REG_SZ)
  437. {
  438. if (parser->is_unicode)
  439. {
  440. WCHAR *data = parser->data;
  441. DWORD len = parser->data_size / sizeof(WCHAR);
  442. if (data[len - 1] != 0)
  443. {
  444. data[len] = 0;
  445. parser->data_size += sizeof(WCHAR);
  446. }
  447. }
  448. else
  449. {
  450. BYTE *data = parser->data;
  451. if (data[parser->data_size - 1] != 0)
  452. {
  453. data[parser->data_size] = 0;
  454. parser->data_size++;
  455. }
  456. parser->data = GetWideStringN(parser->data, parser->data_size, &parser->data_size);
  457. parser->data_size *= sizeof(WCHAR);
  458. heap_free(data);
  459. }
  460. }
  461. }
  462. enum reg_versions {
  463. REG_VERSION_31,
  464. REG_VERSION_40,
  465. REG_VERSION_50,
  466. REG_VERSION_FUZZY,
  467. REG_VERSION_INVALID
  468. };
  469. static enum reg_versions parse_file_header(const WCHAR *s)
  470. {
  471. static const WCHAR header_31[] = {'R','E','G','E','D','I','T',0};
  472. static const WCHAR header_40[] = {'R','E','G','E','D','I','T','4',0};
  473. static const WCHAR header_50[] = {'W','i','n','d','o','w','s',' ',
  474. 'R','e','g','i','s','t','r','y',' ','E','d','i','t','o','r',' ',
  475. 'V','e','r','s','i','o','n',' ','5','.','0','0',0};
  476. while (*s == ' ' || *s == '\t') s++;
  477. if (!lstrcmpW(s, header_31))
  478. return REG_VERSION_31;
  479. if (!lstrcmpW(s, header_40))
  480. return REG_VERSION_40;
  481. if (!lstrcmpW(s, header_50))
  482. return REG_VERSION_50;
  483. /* The Windows version accepts registry file headers beginning with "REGEDIT" and ending
  484. * with other characters, as long as "REGEDIT" appears at the start of the line. For example,
  485. * "REGEDIT 4", "REGEDIT9" and "REGEDIT4FOO" are all treated as valid file headers.
  486. * In all such cases, however, the contents of the registry file are not imported.
  487. */
  488. if (!wcsncmp(s, header_31, 7)) /* "REGEDIT" without NUL */
  489. return REG_VERSION_FUZZY;
  490. return REG_VERSION_INVALID;
  491. }
  492. /* handler for parser HEADER state */
  493. static WCHAR *header_state(struct parser *parser, WCHAR *pos)
  494. {
  495. WCHAR *line, *header;
  496. if (!(line = get_line(parser->file)))
  497. return NULL;
  498. if (!parser->is_unicode)
  499. {
  500. header = heap_xalloc((lstrlenW(line) + 3) * sizeof(WCHAR));
  501. header[0] = parser->two_wchars[0];
  502. header[1] = parser->two_wchars[1];
  503. lstrcpyW(header + 2, line);
  504. parser->reg_version = parse_file_header(header);
  505. heap_free(header);
  506. }
  507. else parser->reg_version = parse_file_header(line);
  508. switch (parser->reg_version)
  509. {
  510. case REG_VERSION_31:
  511. set_state(parser, PARSE_WIN31_LINE);
  512. break;
  513. case REG_VERSION_40:
  514. case REG_VERSION_50:
  515. set_state(parser, LINE_START);
  516. break;
  517. default:
  518. get_line(NULL); /* Reset static variables */
  519. return NULL;
  520. }
  521. return line;
  522. }
  523. /* handler for parser PARSE_WIN31_LINE state */
  524. static WCHAR *parse_win31_line_state(struct parser *parser, WCHAR *pos)
  525. {
  526. WCHAR *line, *value;
  527. static WCHAR hkcr[] = {'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T'};
  528. unsigned int key_end = 0;
  529. if (!(line = get_line(parser->file)))
  530. return NULL;
  531. if (wcsncmp(line, hkcr, ARRAY_SIZE(hkcr)))
  532. return line;
  533. /* get key name */
  534. while (line[key_end] && !iswspace(line[key_end])) key_end++;
  535. value = line + key_end;
  536. while (*value == ' ' || *value == '\t') value++;
  537. if (*value == '=') value++;
  538. if (*value == ' ') value++; /* at most one space is skipped */
  539. line[key_end] = 0;
  540. if (open_key(parser, line) != ERROR_SUCCESS)
  541. {
  542. output_message(STRING_OPEN_KEY_FAILED, line);
  543. return line;
  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. set_state(parser, SET_VALUE);
  550. return value;
  551. }
  552. /* handler for parser LINE_START state */
  553. static WCHAR *line_start_state(struct parser *parser, WCHAR *pos)
  554. {
  555. WCHAR *line, *p;
  556. if (!(line = get_line(parser->file)))
  557. return NULL;
  558. for (p = line; *p; p++)
  559. {
  560. switch (*p)
  561. {
  562. case '[':
  563. set_state(parser, KEY_NAME);
  564. return p + 1;
  565. case '@':
  566. set_state(parser, DEFAULT_VALUE_NAME);
  567. return p;
  568. case '"':
  569. set_state(parser, QUOTED_VALUE_NAME);
  570. return p + 1;
  571. case ' ':
  572. case '\t':
  573. break;
  574. default:
  575. return p;
  576. }
  577. }
  578. return p;
  579. }
  580. /* handler for parser KEY_NAME state */
  581. static WCHAR *key_name_state(struct parser *parser, WCHAR *pos)
  582. {
  583. WCHAR *p = pos, *key_end;
  584. if (*p == ' ' || *p == '\t' || !(key_end = wcsrchr(p, ']')))
  585. goto done;
  586. *key_end = 0;
  587. if (*p == '-')
  588. {
  589. set_state(parser, DELETE_KEY);
  590. return p + 1;
  591. }
  592. else if (open_key(parser, p) != ERROR_SUCCESS)
  593. output_message(STRING_OPEN_KEY_FAILED, p);
  594. done:
  595. set_state(parser, LINE_START);
  596. return p;
  597. }
  598. /* handler for parser DELETE_KEY state */
  599. static WCHAR *delete_key_state(struct parser *parser, WCHAR *pos)
  600. {
  601. WCHAR *p = pos;
  602. close_key(parser);
  603. if (*p == 'H' || *p == 'h')
  604. delete_registry_key(p);
  605. set_state(parser, LINE_START);
  606. return p;
  607. }
  608. /* handler for parser DEFAULT_VALUE_NAME state */
  609. static WCHAR *default_value_name_state(struct parser *parser, WCHAR *pos)
  610. {
  611. heap_free(parser->value_name);
  612. parser->value_name = NULL;
  613. set_state(parser, DATA_START);
  614. return pos + 1;
  615. }
  616. /* handler for parser QUOTED_VALUE_NAME state */
  617. static WCHAR *quoted_value_name_state(struct parser *parser, WCHAR *pos)
  618. {
  619. WCHAR *val_name = pos, *p;
  620. heap_free(parser->value_name);
  621. parser->value_name = NULL;
  622. if (!REGPROC_unescape_string(val_name, &p))
  623. goto invalid;
  624. /* copy the value name in case we need to parse multiple lines and the buffer is overwritten */
  625. parser->value_name = heap_xalloc((lstrlenW(val_name) + 1) * sizeof(WCHAR));
  626. lstrcpyW(parser->value_name, val_name);
  627. set_state(parser, DATA_START);
  628. return p;
  629. invalid:
  630. set_state(parser, LINE_START);
  631. return val_name;
  632. }
  633. /* handler for parser DATA_START state */
  634. static WCHAR *data_start_state(struct parser *parser, WCHAR *pos)
  635. {
  636. WCHAR *p = pos;
  637. unsigned int len;
  638. while (*p == ' ' || *p == '\t') p++;
  639. if (*p != '=') goto done;
  640. p++;
  641. while (*p == ' ' || *p == '\t') p++;
  642. /* trim trailing whitespace */
  643. len = lstrlenW(p);
  644. while (len > 0 && (p[len - 1] == ' ' || p[len - 1] == '\t')) len--;
  645. p[len] = 0;
  646. if (*p == '-')
  647. set_state(parser, DELETE_VALUE);
  648. else
  649. set_state(parser, DATA_TYPE);
  650. return p;
  651. done:
  652. set_state(parser, LINE_START);
  653. return p;
  654. }
  655. /* handler for parser DELETE_VALUE state */
  656. static WCHAR *delete_value_state(struct parser *parser, WCHAR *pos)
  657. {
  658. WCHAR *p = pos + 1;
  659. while (*p == ' ' || *p == '\t') p++;
  660. if (*p && *p != ';') goto done;
  661. RegDeleteValueW(parser->hkey, parser->value_name);
  662. done:
  663. set_state(parser, LINE_START);
  664. return p;
  665. }
  666. /* handler for parser DATA_TYPE state */
  667. static WCHAR *data_type_state(struct parser *parser, WCHAR *pos)
  668. {
  669. WCHAR *line = pos;
  670. if (!parse_data_type(parser, &line))
  671. {
  672. set_state(parser, LINE_START);
  673. return line;
  674. }
  675. switch (parser->parse_type)
  676. {
  677. case REG_SZ:
  678. set_state(parser, STRING_DATA);
  679. break;
  680. case REG_DWORD:
  681. set_state(parser, DWORD_DATA);
  682. break;
  683. case REG_BINARY: /* all hex data types, including undefined */
  684. set_state(parser, HEX_DATA);
  685. break;
  686. default:
  687. set_state(parser, UNKNOWN_DATA);
  688. }
  689. return line;
  690. }
  691. /* handler for parser STRING_DATA state */
  692. static WCHAR *string_data_state(struct parser *parser, WCHAR *pos)
  693. {
  694. WCHAR *line;
  695. parser->data = pos;
  696. if (!REGPROC_unescape_string(parser->data, &line))
  697. goto invalid;
  698. while (*line == ' ' || *line == '\t') line++;
  699. if (*line && *line != ';') goto invalid;
  700. parser->data_size = (lstrlenW(parser->data) + 1) * sizeof(WCHAR);
  701. set_state(parser, SET_VALUE);
  702. return line;
  703. invalid:
  704. free_parser_data(parser);
  705. set_state(parser, LINE_START);
  706. return line;
  707. }
  708. /* handler for parser DWORD_DATA state */
  709. static WCHAR *dword_data_state(struct parser *parser, WCHAR *pos)
  710. {
  711. WCHAR *line = pos;
  712. parser->data = heap_xalloc(sizeof(DWORD));
  713. if (!convert_hex_to_dword(line, parser->data))
  714. goto invalid;
  715. parser->data_size = sizeof(DWORD);
  716. set_state(parser, SET_VALUE);
  717. return line;
  718. invalid:
  719. free_parser_data(parser);
  720. set_state(parser, LINE_START);
  721. return line;
  722. }
  723. /* handler for parser HEX_DATA state */
  724. static WCHAR *hex_data_state(struct parser *parser, WCHAR *pos)
  725. {
  726. WCHAR *line = pos;
  727. if (!*line)
  728. goto set_value;
  729. if (!convert_hex_csv_to_hex(parser, &line))
  730. goto invalid;
  731. if (parser->backslash)
  732. {
  733. set_state(parser, EOL_BACKSLASH);
  734. return line;
  735. }
  736. prepare_hex_string_data(parser);
  737. set_value:
  738. set_state(parser, SET_VALUE);
  739. return line;
  740. invalid:
  741. free_parser_data(parser);
  742. set_state(parser, LINE_START);
  743. return line;
  744. }
  745. /* handler for parser EOL_BACKSLASH state */
  746. static WCHAR *eol_backslash_state(struct parser *parser, WCHAR *pos)
  747. {
  748. WCHAR *p = pos;
  749. while (*p == ' ' || *p == '\t') p++;
  750. if (*p && *p != ';') goto invalid;
  751. set_state(parser, HEX_MULTILINE);
  752. return pos;
  753. invalid:
  754. free_parser_data(parser);
  755. set_state(parser, LINE_START);
  756. return p;
  757. }
  758. /* handler for parser HEX_MULTILINE state */
  759. static WCHAR *hex_multiline_state(struct parser *parser, WCHAR *pos)
  760. {
  761. WCHAR *line;
  762. if (!(line = get_line(parser->file)))
  763. {
  764. prepare_hex_string_data(parser);
  765. set_state(parser, SET_VALUE);
  766. return pos;
  767. }
  768. while (*line == ' ' || *line == '\t') line++;
  769. if (!*line || *line == ';') return line;
  770. if (!iswxdigit(*line)) goto invalid;
  771. set_state(parser, HEX_DATA);
  772. return line;
  773. invalid:
  774. free_parser_data(parser);
  775. set_state(parser, LINE_START);
  776. return line;
  777. }
  778. /* handler for parser UNKNOWN_DATA state */
  779. static WCHAR *unknown_data_state(struct parser *parser, WCHAR *pos)
  780. {
  781. output_message(STRING_UNKNOWN_DATA_FORMAT, parser->data_type);
  782. set_state(parser, LINE_START);
  783. return pos;
  784. }
  785. /* handler for parser SET_VALUE state */
  786. static WCHAR *set_value_state(struct parser *parser, WCHAR *pos)
  787. {
  788. RegSetValueExW(parser->hkey, parser->value_name, 0, parser->data_type,
  789. parser->data, parser->data_size);
  790. free_parser_data(parser);
  791. if (parser->reg_version == REG_VERSION_31)
  792. set_state(parser, PARSE_WIN31_LINE);
  793. else
  794. set_state(parser, LINE_START);
  795. return pos;
  796. }
  797. static WCHAR *get_lineA(FILE *fp)
  798. {
  799. static WCHAR *lineW;
  800. static size_t size;
  801. static char *buf, *next;
  802. char *line;
  803. heap_free(lineW);
  804. if (!fp) goto cleanup;
  805. if (!size)
  806. {
  807. size = REG_VAL_BUF_SIZE;
  808. buf = heap_xalloc(size);
  809. *buf = 0;
  810. next = buf;
  811. }
  812. line = next;
  813. while (next)
  814. {
  815. char *p = strpbrk(line, "\r\n");
  816. if (!p)
  817. {
  818. size_t len, count;
  819. len = strlen(next);
  820. memmove(buf, next, len + 1);
  821. if (size - len < 3)
  822. {
  823. size *= 2;
  824. buf = heap_xrealloc(buf, size);
  825. }
  826. if (!(count = fread(buf + len, 1, size - len - 1, fp)))
  827. {
  828. next = NULL;
  829. lineW = GetWideString(buf);
  830. return lineW;
  831. }
  832. buf[len + count] = 0;
  833. next = buf;
  834. line = buf;
  835. continue;
  836. }
  837. next = p + 1;
  838. if (*p == '\r' && *(p + 1) == '\n') next++;
  839. *p = 0;
  840. lineW = GetWideString(line);
  841. return lineW;
  842. }
  843. cleanup:
  844. lineW = NULL;
  845. if (size) heap_free(buf);
  846. size = 0;
  847. return NULL;
  848. }
  849. static WCHAR *get_lineW(FILE *fp)
  850. {
  851. static size_t size;
  852. static WCHAR *buf, *next;
  853. WCHAR *line;
  854. if (!fp) goto cleanup;
  855. if (!size)
  856. {
  857. size = REG_VAL_BUF_SIZE;
  858. buf = heap_xalloc(size * sizeof(WCHAR));
  859. *buf = 0;
  860. next = buf;
  861. }
  862. line = next;
  863. while (next)
  864. {
  865. static const WCHAR line_endings[] = {'\r','\n',0};
  866. WCHAR *p = wcspbrk(line, line_endings);
  867. if (!p)
  868. {
  869. size_t len, count;
  870. len = lstrlenW(next);
  871. memmove(buf, next, (len + 1) * sizeof(WCHAR));
  872. if (size - len < 3)
  873. {
  874. size *= 2;
  875. buf = heap_xrealloc(buf, size * sizeof(WCHAR));
  876. }
  877. if (!(count = fread(buf + len, sizeof(WCHAR), size - len - 1, fp)))
  878. {
  879. next = NULL;
  880. return buf;
  881. }
  882. buf[len + count] = 0;
  883. next = buf;
  884. line = buf;
  885. continue;
  886. }
  887. next = p + 1;
  888. if (*p == '\r' && *(p + 1) == '\n') next++;
  889. *p = 0;
  890. return line;
  891. }
  892. cleanup:
  893. if (size) heap_free(buf);
  894. size = 0;
  895. return NULL;
  896. }
  897. /******************************************************************************
  898. * Reads contents of the specified file into the registry.
  899. */
  900. BOOL import_registry_file(FILE *reg_file)
  901. {
  902. BYTE s[2];
  903. struct parser parser;
  904. WCHAR *pos;
  905. if (!reg_file || (fread(s, 2, 1, reg_file) != 1))
  906. return FALSE;
  907. parser.is_unicode = (s[0] == 0xff && s[1] == 0xfe);
  908. get_line = parser.is_unicode ? get_lineW : get_lineA;
  909. parser.file = reg_file;
  910. parser.two_wchars[0] = s[0];
  911. parser.two_wchars[1] = s[1];
  912. parser.reg_version = -1;
  913. parser.hkey = NULL;
  914. parser.key_name = NULL;
  915. parser.value_name = NULL;
  916. parser.parse_type = 0;
  917. parser.data_type = 0;
  918. parser.data = NULL;
  919. parser.data_size = 0;
  920. parser.backslash = FALSE;
  921. parser.state = HEADER;
  922. pos = parser.two_wchars;
  923. /* parser main loop */
  924. while (pos)
  925. pos = (parser_funcs[parser.state])(&parser, pos);
  926. if (parser.reg_version == REG_VERSION_FUZZY || parser.reg_version == REG_VERSION_INVALID)
  927. return parser.reg_version == REG_VERSION_FUZZY;
  928. heap_free(parser.value_name);
  929. close_key(&parser);
  930. return TRUE;
  931. }
  932. /******************************************************************************
  933. * Removes the registry key with all subkeys. Parses full key name.
  934. *
  935. * Parameters:
  936. * reg_key_name - full name of registry branch to delete. Ignored if is NULL,
  937. * empty, points to register key class, does not exist.
  938. */
  939. void delete_registry_key(WCHAR *reg_key_name)
  940. {
  941. WCHAR *key_name = NULL;
  942. HKEY key_class;
  943. if (!reg_key_name || !reg_key_name[0])
  944. return;
  945. if (!(key_class = parse_key_name(reg_key_name, &key_name)))
  946. {
  947. if (key_name) *(key_name - 1) = 0;
  948. error_exit(STRING_INVALID_SYSTEM_KEY, reg_key_name);
  949. }
  950. if (!key_name || !*key_name)
  951. error_exit(STRING_DELETE_FAILED, reg_key_name);
  952. RegDeleteTreeW(key_class, key_name);
  953. }
  954. static void REGPROC_write_line(FILE *fp, const WCHAR *str, BOOL unicode)
  955. {
  956. if (unicode)
  957. fwrite(str, sizeof(WCHAR), lstrlenW(str), fp);
  958. else
  959. {
  960. char *strA = GetMultiByteString(str);
  961. fputs(strA, fp);
  962. heap_free(strA);
  963. }
  964. }
  965. static WCHAR *REGPROC_escape_string(WCHAR *str, size_t str_len, size_t *line_len)
  966. {
  967. size_t i, escape_count, pos;
  968. WCHAR *buf;
  969. for (i = 0, escape_count = 0; i < str_len; i++)
  970. {
  971. WCHAR c = str[i];
  972. if (!c) break;
  973. if (c == '\r' || c == '\n' || c == '\\' || c == '"')
  974. escape_count++;
  975. }
  976. buf = heap_xalloc((str_len + escape_count + 1) * sizeof(WCHAR));
  977. for (i = 0, pos = 0; i < str_len; i++, pos++)
  978. {
  979. WCHAR c = str[i];
  980. if (!c) break;
  981. switch (c)
  982. {
  983. case '\r':
  984. buf[pos++] = '\\';
  985. buf[pos] = 'r';
  986. break;
  987. case '\n':
  988. buf[pos++] = '\\';
  989. buf[pos] = 'n';
  990. break;
  991. case '\\':
  992. buf[pos++] = '\\';
  993. buf[pos] = '\\';
  994. break;
  995. case '"':
  996. buf[pos++] = '\\';
  997. buf[pos] = '"';
  998. break;
  999. default:
  1000. buf[pos] = c;
  1001. }
  1002. }
  1003. buf[pos] = 0;
  1004. *line_len = pos;
  1005. return buf;
  1006. }
  1007. static size_t export_value_name(FILE *fp, WCHAR *name, size_t len, BOOL unicode)
  1008. {
  1009. static const WCHAR quoted_fmt[] = {'"','%','s','"','=',0};
  1010. static const WCHAR default_name[] = {'@','=',0};
  1011. size_t line_len;
  1012. if (name && *name)
  1013. {
  1014. WCHAR *str = REGPROC_escape_string(name, len, &line_len);
  1015. WCHAR *buf = heap_xalloc((line_len + 4) * sizeof(WCHAR));
  1016. line_len = swprintf(buf, line_len + 4, quoted_fmt, str);
  1017. REGPROC_write_line(fp, buf, unicode);
  1018. heap_free(buf);
  1019. heap_free(str);
  1020. }
  1021. else
  1022. {
  1023. line_len = lstrlenW(default_name);
  1024. REGPROC_write_line(fp, default_name, unicode);
  1025. }
  1026. return line_len;
  1027. }
  1028. static void export_string_data(WCHAR **buf, WCHAR *data, size_t size)
  1029. {
  1030. size_t len = 0, line_len;
  1031. WCHAR *str;
  1032. static const WCHAR fmt[] = {'"','%','s','"',0};
  1033. if (size)
  1034. len = size / sizeof(WCHAR) - 1;
  1035. str = REGPROC_escape_string(data, len, &line_len);
  1036. *buf = heap_xalloc((line_len + 3) * sizeof(WCHAR));
  1037. swprintf(*buf, line_len + 3, fmt, str);
  1038. heap_free(str);
  1039. }
  1040. static void export_dword_data(WCHAR **buf, DWORD *data)
  1041. {
  1042. static const WCHAR fmt[] = {'d','w','o','r','d',':','%','0','8','x',0};
  1043. *buf = heap_xalloc(15 * sizeof(WCHAR));
  1044. swprintf(*buf, 15, fmt, *data);
  1045. }
  1046. static size_t export_hex_data_type(FILE *fp, DWORD type, BOOL unicode)
  1047. {
  1048. static const WCHAR hex[] = {'h','e','x',':',0};
  1049. static const WCHAR hexp_fmt[] = {'h','e','x','(','%','x',')',':',0};
  1050. size_t line_len;
  1051. if (type == REG_BINARY)
  1052. {
  1053. line_len = lstrlenW(hex);
  1054. REGPROC_write_line(fp, hex, unicode);
  1055. }
  1056. else
  1057. {
  1058. WCHAR *buf = heap_xalloc(15 * sizeof(WCHAR));
  1059. line_len = swprintf(buf, 15, hexp_fmt, type);
  1060. REGPROC_write_line(fp, buf, unicode);
  1061. heap_free(buf);
  1062. }
  1063. return line_len;
  1064. }
  1065. #define MAX_HEX_CHARS 77
  1066. static void export_hex_data(FILE *fp, WCHAR **buf, DWORD type, DWORD line_len,
  1067. void *data, DWORD size, BOOL unicode)
  1068. {
  1069. static const WCHAR fmt[] = {'%','0','2','x',0};
  1070. static const WCHAR hex_concat[] = {'\\','\r','\n',' ',' ',0};
  1071. size_t num_commas, i, pos;
  1072. line_len += export_hex_data_type(fp, type, unicode);
  1073. if (!size) return;
  1074. if (!unicode && (type == REG_EXPAND_SZ || type == REG_MULTI_SZ))
  1075. data = GetMultiByteStringN(data, size / sizeof(WCHAR), &size);
  1076. num_commas = size - 1;
  1077. *buf = heap_xalloc(size * 3 * sizeof(WCHAR));
  1078. for (i = 0, pos = 0; i < size; i++)
  1079. {
  1080. pos += swprintf(*buf + pos, 3, fmt, ((BYTE *)data)[i]);
  1081. if (i == num_commas) break;
  1082. (*buf)[pos++] = ',';
  1083. (*buf)[pos] = 0;
  1084. line_len += 3;
  1085. if (line_len >= MAX_HEX_CHARS)
  1086. {
  1087. REGPROC_write_line(fp, *buf, unicode);
  1088. REGPROC_write_line(fp, hex_concat, unicode);
  1089. line_len = 2;
  1090. pos = 0;
  1091. }
  1092. }
  1093. }
  1094. static void export_newline(FILE *fp, BOOL unicode)
  1095. {
  1096. static const WCHAR newline[] = {'\r','\n',0};
  1097. REGPROC_write_line(fp, newline, unicode);
  1098. }
  1099. static void export_data(FILE *fp, WCHAR *value_name, DWORD value_len, DWORD type,
  1100. void *data, size_t size, BOOL unicode)
  1101. {
  1102. WCHAR *buf = NULL;
  1103. size_t line_len = export_value_name(fp, value_name, value_len, unicode);
  1104. switch (type)
  1105. {
  1106. case REG_SZ:
  1107. export_string_data(&buf, data, size);
  1108. break;
  1109. case REG_DWORD:
  1110. if (size)
  1111. {
  1112. export_dword_data(&buf, data);
  1113. break;
  1114. }
  1115. /* fall through */
  1116. case REG_NONE:
  1117. case REG_EXPAND_SZ:
  1118. case REG_BINARY:
  1119. case REG_MULTI_SZ:
  1120. default:
  1121. export_hex_data(fp, &buf, type, line_len, data, size, unicode);
  1122. break;
  1123. }
  1124. if (size || type == REG_SZ)
  1125. {
  1126. REGPROC_write_line(fp, buf, unicode);
  1127. heap_free(buf);
  1128. }
  1129. export_newline(fp, unicode);
  1130. }
  1131. static WCHAR *build_subkey_path(WCHAR *path, DWORD path_len, WCHAR *subkey_name, DWORD subkey_len)
  1132. {
  1133. WCHAR *subkey_path;
  1134. static const WCHAR fmt[] = {'%','s','\\','%','s',0};
  1135. subkey_path = heap_xalloc((path_len + subkey_len + 2) * sizeof(WCHAR));
  1136. swprintf(subkey_path, path_len + subkey_len + 2, fmt, path, subkey_name);
  1137. return subkey_path;
  1138. }
  1139. static void export_key_name(FILE *fp, WCHAR *name, BOOL unicode)
  1140. {
  1141. static const WCHAR fmt[] = {'\r','\n','[','%','s',']','\r','\n',0};
  1142. WCHAR *buf;
  1143. buf = heap_xalloc((lstrlenW(name) + 7) * sizeof(WCHAR));
  1144. swprintf(buf, lstrlenW(name) + 7, fmt, name);
  1145. REGPROC_write_line(fp, buf, unicode);
  1146. heap_free(buf);
  1147. }
  1148. #define MAX_SUBKEY_LEN 257
  1149. static int export_registry_data(FILE *fp, HKEY key, WCHAR *path, BOOL unicode)
  1150. {
  1151. LONG rc;
  1152. DWORD max_value_len = 256, value_len;
  1153. DWORD max_data_bytes = 2048, data_size;
  1154. DWORD subkey_len;
  1155. DWORD i, type, path_len;
  1156. WCHAR *value_name, *subkey_name, *subkey_path;
  1157. BYTE *data;
  1158. HKEY subkey;
  1159. export_key_name(fp, path, unicode);
  1160. value_name = heap_xalloc(max_value_len * sizeof(WCHAR));
  1161. data = heap_xalloc(max_data_bytes);
  1162. i = 0;
  1163. for (;;)
  1164. {
  1165. value_len = max_value_len;
  1166. data_size = max_data_bytes;
  1167. rc = RegEnumValueW(key, i, value_name, &value_len, NULL, &type, data, &data_size);
  1168. if (rc == ERROR_SUCCESS)
  1169. {
  1170. export_data(fp, value_name, value_len, type, data, data_size, unicode);
  1171. i++;
  1172. }
  1173. else if (rc == ERROR_MORE_DATA)
  1174. {
  1175. if (data_size > max_data_bytes)
  1176. {
  1177. max_data_bytes = data_size;
  1178. data = heap_xrealloc(data, max_data_bytes);
  1179. }
  1180. else
  1181. {
  1182. max_value_len *= 2;
  1183. value_name = heap_xrealloc(value_name, max_value_len * sizeof(WCHAR));
  1184. }
  1185. }
  1186. else break;
  1187. }
  1188. heap_free(data);
  1189. heap_free(value_name);
  1190. subkey_name = heap_xalloc(MAX_SUBKEY_LEN * sizeof(WCHAR));
  1191. path_len = lstrlenW(path);
  1192. i = 0;
  1193. for (;;)
  1194. {
  1195. subkey_len = MAX_SUBKEY_LEN;
  1196. rc = RegEnumKeyExW(key, i, subkey_name, &subkey_len, NULL, NULL, NULL, NULL);
  1197. if (rc == ERROR_SUCCESS)
  1198. {
  1199. subkey_path = build_subkey_path(path, path_len, subkey_name, subkey_len);
  1200. if (!RegOpenKeyExW(key, subkey_name, 0, KEY_READ, &subkey))
  1201. {
  1202. export_registry_data(fp, subkey, subkey_path, unicode);
  1203. RegCloseKey(subkey);
  1204. }
  1205. heap_free(subkey_path);
  1206. i++;
  1207. }
  1208. else break;
  1209. }
  1210. heap_free(subkey_name);
  1211. return 0;
  1212. }
  1213. static FILE *REGPROC_open_export_file(WCHAR *file_name, BOOL unicode)
  1214. {
  1215. FILE *file;
  1216. static const WCHAR hyphen[] = {'-',0};
  1217. if (!lstrcmpW(file_name, hyphen))
  1218. {
  1219. file = stdout;
  1220. _setmode(_fileno(file), _O_BINARY);
  1221. }
  1222. else
  1223. {
  1224. static const WCHAR wb_mode[] = {'w','b',0};
  1225. file = _wfopen(file_name, wb_mode);
  1226. if (!file)
  1227. {
  1228. static const WCHAR regedit[] = {'r','e','g','e','d','i','t',0};
  1229. _wperror(regedit);
  1230. error_exit(STRING_CANNOT_OPEN_FILE, file_name);
  1231. }
  1232. }
  1233. if (unicode)
  1234. {
  1235. static const BYTE bom[] = {0xff,0xfe};
  1236. static const WCHAR header[] = {'W','i','n','d','o','w','s',' ',
  1237. 'R','e','g','i','s','t','r','y',' ','E','d','i','t','o','r',' ',
  1238. 'V','e','r','s','i','o','n',' ','5','.','0','0','\r','\n'};
  1239. fwrite(bom, sizeof(BYTE), ARRAY_SIZE(bom), file);
  1240. fwrite(header, sizeof(WCHAR), ARRAY_SIZE(header), file);
  1241. }
  1242. else
  1243. fputs("REGEDIT4\r\n", file);
  1244. return file;
  1245. }
  1246. static HKEY open_export_key(HKEY key_class, WCHAR *subkey, WCHAR *path)
  1247. {
  1248. HKEY key;
  1249. if (!RegOpenKeyExW(key_class, subkey, 0, KEY_READ, &key))
  1250. return key;
  1251. output_message(STRING_OPEN_KEY_FAILED, path);
  1252. return NULL;
  1253. }
  1254. static BOOL export_key(WCHAR *file_name, WCHAR *path, BOOL unicode)
  1255. {
  1256. HKEY key_class, key;
  1257. WCHAR *subkey;
  1258. FILE *fp;
  1259. BOOL ret;
  1260. if (!(key_class = parse_key_name(path, &subkey)))
  1261. {
  1262. if (subkey) *(subkey - 1) = 0;
  1263. output_message(STRING_INVALID_SYSTEM_KEY, path);
  1264. return FALSE;
  1265. }
  1266. if (!(key = open_export_key(key_class, subkey, path)))
  1267. return FALSE;
  1268. fp = REGPROC_open_export_file(file_name, unicode);
  1269. ret = export_registry_data(fp, key, path, unicode);
  1270. export_newline(fp, unicode);
  1271. fclose(fp);
  1272. RegCloseKey(key);
  1273. return ret;
  1274. }
  1275. static BOOL export_all(WCHAR *file_name, WCHAR *path, BOOL unicode)
  1276. {
  1277. FILE *fp;
  1278. int i;
  1279. HKEY classes[] = {HKEY_LOCAL_MACHINE, HKEY_USERS}, key;
  1280. WCHAR *class_name;
  1281. fp = REGPROC_open_export_file(file_name, unicode);
  1282. for (i = 0; i < ARRAY_SIZE(classes); i++)
  1283. {
  1284. if (!(key = open_export_key(classes[i], NULL, path)))
  1285. {
  1286. fclose(fp);
  1287. return FALSE;
  1288. }
  1289. class_name = heap_xalloc((lstrlenW(reg_class_namesW[i]) + 1) * sizeof(WCHAR));
  1290. lstrcpyW(class_name, reg_class_namesW[i]);
  1291. export_registry_data(fp, classes[i], class_name, unicode);
  1292. heap_free(class_name);
  1293. RegCloseKey(key);
  1294. }
  1295. export_newline(fp, unicode);
  1296. fclose(fp);
  1297. return TRUE;
  1298. }
  1299. BOOL export_registry_key(WCHAR *file_name, WCHAR *path, DWORD format)
  1300. {
  1301. BOOL unicode = (format == REG_FORMAT_5);
  1302. if (path && *path)
  1303. return export_key(file_name, path, unicode);
  1304. else
  1305. return export_all(file_name, path, unicode);
  1306. }