reg.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  1. /*
  2. * Copyright 2008 Andrew Riedi
  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 const WCHAR short_hklm[] = {'H','K','L','M',0};
  27. static const WCHAR short_hkcu[] = {'H','K','C','U',0};
  28. static const WCHAR short_hkcr[] = {'H','K','C','R',0};
  29. static const WCHAR short_hku[] = {'H','K','U',0};
  30. static const WCHAR short_hkcc[] = {'H','K','C','C',0};
  31. static const WCHAR long_hklm[] = {'H','K','E','Y','_','L','O','C','A','L','_','M','A','C','H','I','N','E',0};
  32. static const WCHAR long_hkcu[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','U','S','E','R',0};
  33. static const WCHAR long_hkcr[] = {'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T',0};
  34. static const WCHAR long_hku[] = {'H','K','E','Y','_','U','S','E','R','S',0};
  35. static const WCHAR long_hkcc[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','C','O','N','F','I','G',0};
  36. static const struct
  37. {
  38. HKEY key;
  39. const WCHAR *short_name;
  40. const WCHAR *long_name;
  41. }
  42. root_rels[] =
  43. {
  44. {HKEY_LOCAL_MACHINE, short_hklm, long_hklm},
  45. {HKEY_CURRENT_USER, short_hkcu, long_hkcu},
  46. {HKEY_CLASSES_ROOT, short_hkcr, long_hkcr},
  47. {HKEY_USERS, short_hku, long_hku},
  48. {HKEY_CURRENT_CONFIG, short_hkcc, long_hkcc},
  49. };
  50. static const WCHAR type_none[] = {'R','E','G','_','N','O','N','E',0};
  51. static const WCHAR type_sz[] = {'R','E','G','_','S','Z',0};
  52. static const WCHAR type_expand_sz[] = {'R','E','G','_','E','X','P','A','N','D','_','S','Z',0};
  53. static const WCHAR type_binary[] = {'R','E','G','_','B','I','N','A','R','Y',0};
  54. static const WCHAR type_dword[] = {'R','E','G','_','D','W','O','R','D',0};
  55. static const WCHAR type_dword_le[] = {'R','E','G','_','D','W','O','R','D','_','L','I','T','T','L','E','_','E','N','D','I','A','N',0};
  56. static const WCHAR type_dword_be[] = {'R','E','G','_','D','W','O','R','D','_','B','I','G','_','E','N','D','I','A','N',0};
  57. static const WCHAR type_multi_sz[] = {'R','E','G','_','M','U','L','T','I','_','S','Z',0};
  58. static const struct
  59. {
  60. DWORD type;
  61. const WCHAR *name;
  62. }
  63. type_rels[] =
  64. {
  65. {REG_NONE, type_none},
  66. {REG_SZ, type_sz},
  67. {REG_EXPAND_SZ, type_expand_sz},
  68. {REG_BINARY, type_binary},
  69. {REG_DWORD, type_dword},
  70. {REG_DWORD_LITTLE_ENDIAN, type_dword_le},
  71. {REG_DWORD_BIG_ENDIAN, type_dword_be},
  72. {REG_MULTI_SZ, type_multi_sz},
  73. };
  74. static const WCHAR newlineW[] = {'\n',0};
  75. void *heap_xalloc(size_t size)
  76. {
  77. void *buf = heap_alloc(size);
  78. if (!buf)
  79. {
  80. ERR("Out of memory!\n");
  81. exit(1);
  82. }
  83. return buf;
  84. }
  85. void *heap_xrealloc(void *buf, size_t size)
  86. {
  87. void *new_buf = heap_realloc(buf, size);
  88. if (!new_buf)
  89. {
  90. ERR("Out of memory!\n");
  91. exit(1);
  92. }
  93. return new_buf;
  94. }
  95. void output_writeconsole(const WCHAR *str, DWORD wlen)
  96. {
  97. DWORD count, ret;
  98. ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), str, wlen, &count, NULL);
  99. if (!ret)
  100. {
  101. DWORD len;
  102. char *msgA;
  103. /* On Windows WriteConsoleW() fails if the output is redirected. So fall
  104. * back to WriteFile(), assuming the console encoding is still the right
  105. * one in that case.
  106. */
  107. len = WideCharToMultiByte(GetConsoleOutputCP(), 0, str, wlen, NULL, 0, NULL, NULL);
  108. msgA = heap_xalloc(len);
  109. WideCharToMultiByte(GetConsoleOutputCP(), 0, str, wlen, msgA, len, NULL, NULL);
  110. WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE);
  111. heap_free(msgA);
  112. }
  113. }
  114. static void output_formatstring(const WCHAR *fmt, __ms_va_list va_args)
  115. {
  116. WCHAR *str;
  117. DWORD len;
  118. SetLastError(NO_ERROR);
  119. len = FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER,
  120. fmt, 0, 0, (WCHAR *)&str, 0, &va_args);
  121. if (len == 0 && GetLastError() != NO_ERROR)
  122. {
  123. WINE_FIXME("Could not format string: le=%u, fmt=%s\n", GetLastError(), wine_dbgstr_w(fmt));
  124. return;
  125. }
  126. output_writeconsole(str, len);
  127. LocalFree(str);
  128. }
  129. void WINAPIV output_message(unsigned int id, ...)
  130. {
  131. WCHAR fmt[1024];
  132. __ms_va_list va_args;
  133. if (!LoadStringW(GetModuleHandleW(NULL), id, fmt, ARRAY_SIZE(fmt)))
  134. {
  135. WINE_FIXME("LoadString failed with %d\n", GetLastError());
  136. return;
  137. }
  138. __ms_va_start(va_args, id);
  139. output_formatstring(fmt, va_args);
  140. __ms_va_end(va_args);
  141. }
  142. static void WINAPIV output_string(const WCHAR *fmt, ...)
  143. {
  144. __ms_va_list va_args;
  145. __ms_va_start(va_args, fmt);
  146. output_formatstring(fmt, va_args);
  147. __ms_va_end(va_args);
  148. }
  149. /* ask_confirm() adapted from programs/cmd/builtins.c */
  150. BOOL ask_confirm(unsigned int msgid, WCHAR *reg_info)
  151. {
  152. HMODULE hmod;
  153. WCHAR Ybuffer[4];
  154. WCHAR Nbuffer[4];
  155. WCHAR defval[32];
  156. WCHAR answer[MAX_PATH];
  157. WCHAR *str;
  158. DWORD count;
  159. hmod = GetModuleHandleW(NULL);
  160. LoadStringW(hmod, STRING_YES, Ybuffer, ARRAY_SIZE(Ybuffer));
  161. LoadStringW(hmod, STRING_NO, Nbuffer, ARRAY_SIZE(Nbuffer));
  162. LoadStringW(hmod, STRING_DEFAULT_VALUE, defval, ARRAY_SIZE(defval));
  163. str = (reg_info && *reg_info) ? reg_info : defval;
  164. while (1)
  165. {
  166. output_message(msgid, str);
  167. output_message(STRING_YESNO);
  168. ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE), answer, ARRAY_SIZE(answer), &count, NULL);
  169. answer[0] = towupper(answer[0]);
  170. if (answer[0] == Ybuffer[0])
  171. return TRUE;
  172. if (answer[0] == Nbuffer[0])
  173. return FALSE;
  174. }
  175. }
  176. static inline BOOL path_rootname_cmp(const WCHAR *input_path, const WCHAR *rootkey_name)
  177. {
  178. DWORD length = lstrlenW(rootkey_name);
  179. return (!wcsnicmp(input_path, rootkey_name, length) &&
  180. (input_path[length] == 0 || input_path[length] == '\\'));
  181. }
  182. HKEY path_get_rootkey(const WCHAR *path)
  183. {
  184. DWORD i;
  185. for (i = 0; i < ARRAY_SIZE(root_rels); i++)
  186. {
  187. if (path_rootname_cmp(path, root_rels[i].short_name) ||
  188. path_rootname_cmp(path, root_rels[i].long_name))
  189. return root_rels[i].key;
  190. }
  191. return NULL;
  192. }
  193. static DWORD wchar_get_type(const WCHAR *type_name)
  194. {
  195. DWORD i;
  196. if (!type_name)
  197. return REG_SZ;
  198. for (i = 0; i < ARRAY_SIZE(type_rels); i++)
  199. {
  200. if (!wcsicmp(type_rels[i].name, type_name))
  201. return type_rels[i].type;
  202. }
  203. return ~0u;
  204. }
  205. /* hexchar_to_byte from programs/regedit/hexedit.c */
  206. static inline BYTE hexchar_to_byte(WCHAR ch)
  207. {
  208. if (ch >= '0' && ch <= '9')
  209. return ch - '0';
  210. else if (ch >= 'a' && ch <= 'f')
  211. return ch - 'a' + 10;
  212. else if (ch >= 'A' && ch <= 'F')
  213. return ch - 'A' + 10;
  214. else
  215. return -1;
  216. }
  217. static LPBYTE get_regdata(const WCHAR *data, DWORD reg_type, WCHAR separator, DWORD *reg_count)
  218. {
  219. static const WCHAR empty;
  220. LPBYTE out_data = NULL;
  221. *reg_count = 0;
  222. if (!data) data = &empty;
  223. switch (reg_type)
  224. {
  225. case REG_NONE:
  226. case REG_SZ:
  227. case REG_EXPAND_SZ:
  228. {
  229. *reg_count = (lstrlenW(data) + 1) * sizeof(WCHAR);
  230. out_data = heap_xalloc(*reg_count);
  231. lstrcpyW((LPWSTR)out_data,data);
  232. break;
  233. }
  234. case REG_DWORD:
  235. /* case REG_DWORD_LITTLE_ENDIAN: */
  236. case REG_DWORD_BIG_ENDIAN: /* Yes, this is correct! */
  237. {
  238. LPWSTR rest;
  239. unsigned long val;
  240. val = wcstoul(data, &rest, (towlower(data[1]) == 'x') ? 16 : 10);
  241. if (*rest || data[0] == '-' || (val == ~0u && errno == ERANGE)) {
  242. output_message(STRING_MISSING_INTEGER);
  243. break;
  244. }
  245. *reg_count = sizeof(DWORD);
  246. out_data = heap_xalloc(*reg_count);
  247. ((LPDWORD)out_data)[0] = val;
  248. break;
  249. }
  250. case REG_BINARY:
  251. {
  252. BYTE hex0, hex1;
  253. int i = 0, destByteIndex = 0, datalen = lstrlenW(data);
  254. *reg_count = ((datalen + datalen % 2) / 2) * sizeof(BYTE);
  255. out_data = heap_xalloc(*reg_count);
  256. if(datalen % 2)
  257. {
  258. hex1 = hexchar_to_byte(data[i++]);
  259. if(hex1 == 0xFF)
  260. goto no_hex_data;
  261. out_data[destByteIndex++] = hex1;
  262. }
  263. for(;i + 1 < datalen;i += 2)
  264. {
  265. hex0 = hexchar_to_byte(data[i]);
  266. hex1 = hexchar_to_byte(data[i + 1]);
  267. if(hex0 == 0xFF || hex1 == 0xFF)
  268. goto no_hex_data;
  269. out_data[destByteIndex++] = (hex0 << 4) | hex1;
  270. }
  271. break;
  272. no_hex_data:
  273. /* cleanup, print error */
  274. heap_free(out_data);
  275. output_message(STRING_MISSING_HEXDATA);
  276. out_data = NULL;
  277. break;
  278. }
  279. case REG_MULTI_SZ:
  280. {
  281. int i, destindex, len = lstrlenW(data);
  282. WCHAR *buffer = heap_xalloc((len + 2) * sizeof(WCHAR));
  283. for (i = 0, destindex = 0; i < len; i++, destindex++)
  284. {
  285. if (!separator && data[i] == '\\' && data[i + 1] == '0')
  286. {
  287. buffer[destindex] = 0;
  288. i++;
  289. }
  290. else if (data[i] == separator)
  291. buffer[destindex] = 0;
  292. else
  293. buffer[destindex] = data[i];
  294. if (destindex && !buffer[destindex - 1] && (!buffer[destindex] || destindex == 1))
  295. {
  296. heap_free(buffer);
  297. output_message(STRING_INVALID_STRING);
  298. return NULL;
  299. }
  300. }
  301. buffer[destindex] = 0;
  302. if (destindex && buffer[destindex - 1])
  303. buffer[++destindex] = 0;
  304. *reg_count = (destindex + 1) * sizeof(WCHAR);
  305. return (BYTE *)buffer;
  306. }
  307. default:
  308. output_message(STRING_UNHANDLED_TYPE, reg_type, data);
  309. }
  310. return out_data;
  311. }
  312. static BOOL sane_path(const WCHAR *key)
  313. {
  314. unsigned int i = lstrlenW(key);
  315. if (i < 3 || (key[i - 1] == '\\' && key[i - 2] == '\\'))
  316. {
  317. output_message(STRING_INVALID_KEY);
  318. return FALSE;
  319. }
  320. if (key[0] == '\\' && key[1] == '\\' && key[2] != '\\')
  321. {
  322. output_message(STRING_NO_REMOTE);
  323. return FALSE;
  324. }
  325. return TRUE;
  326. }
  327. static int reg_add(HKEY root, WCHAR *path, WCHAR *value_name, BOOL value_empty,
  328. WCHAR *type, WCHAR separator, WCHAR *data, BOOL force)
  329. {
  330. HKEY key;
  331. if (RegCreateKeyW(root, path, &key) != ERROR_SUCCESS)
  332. {
  333. output_message(STRING_INVALID_KEY);
  334. return 1;
  335. }
  336. if (value_name || value_empty || data)
  337. {
  338. DWORD reg_type;
  339. DWORD reg_count = 0;
  340. BYTE* reg_data = NULL;
  341. if (!force)
  342. {
  343. if (RegQueryValueExW(key, value_name, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
  344. {
  345. if (!ask_confirm(STRING_OVERWRITE_VALUE, value_name))
  346. {
  347. RegCloseKey(key);
  348. output_message(STRING_CANCELLED);
  349. return 0;
  350. }
  351. }
  352. }
  353. reg_type = wchar_get_type(type);
  354. if (reg_type == ~0u)
  355. {
  356. RegCloseKey(key);
  357. output_message(STRING_UNSUPPORTED_TYPE, type);
  358. return 1;
  359. }
  360. if ((reg_type == REG_DWORD || reg_type == REG_DWORD_BIG_ENDIAN) && !data)
  361. {
  362. RegCloseKey(key);
  363. output_message(STRING_INVALID_CMDLINE);
  364. return 1;
  365. }
  366. if (!(reg_data = get_regdata(data, reg_type, separator, &reg_count)))
  367. {
  368. RegCloseKey(key);
  369. return 1;
  370. }
  371. RegSetValueExW(key, value_name, 0, reg_type, reg_data, reg_count);
  372. heap_free(reg_data);
  373. }
  374. RegCloseKey(key);
  375. output_message(STRING_SUCCESS);
  376. return 0;
  377. }
  378. static int reg_delete(HKEY root, WCHAR *path, WCHAR *key_name, WCHAR *value_name,
  379. BOOL value_empty, BOOL value_all, BOOL force)
  380. {
  381. HKEY key;
  382. if (!force)
  383. {
  384. BOOL ret;
  385. if (value_name || value_empty)
  386. ret = ask_confirm(STRING_DELETE_VALUE, value_name);
  387. else if (value_all)
  388. ret = ask_confirm(STRING_DELETE_VALUEALL, key_name);
  389. else
  390. ret = ask_confirm(STRING_DELETE_SUBKEY, key_name);
  391. if (!ret)
  392. {
  393. output_message(STRING_CANCELLED);
  394. return 0;
  395. }
  396. }
  397. /* Delete subtree only if no /v* option is given */
  398. if (!value_name && !value_empty && !value_all)
  399. {
  400. if (RegDeleteTreeW(root, path) != ERROR_SUCCESS)
  401. {
  402. output_message(STRING_CANNOT_FIND);
  403. return 1;
  404. }
  405. output_message(STRING_SUCCESS);
  406. return 0;
  407. }
  408. if (RegOpenKeyW(root, path, &key) != ERROR_SUCCESS)
  409. {
  410. output_message(STRING_CANNOT_FIND);
  411. return 1;
  412. }
  413. if (value_all)
  414. {
  415. DWORD max_value_len = 256, value_len;
  416. WCHAR *value_name;
  417. LONG rc;
  418. value_name = heap_xalloc(max_value_len * sizeof(WCHAR));
  419. while (1)
  420. {
  421. value_len = max_value_len;
  422. rc = RegEnumValueW(key, 0, value_name, &value_len, NULL, NULL, NULL, NULL);
  423. if (rc == ERROR_SUCCESS)
  424. {
  425. rc = RegDeleteValueW(key, value_name);
  426. if (rc != ERROR_SUCCESS)
  427. {
  428. heap_free(value_name);
  429. RegCloseKey(key);
  430. output_message(STRING_VALUEALL_FAILED, key_name);
  431. return 1;
  432. }
  433. }
  434. else if (rc == ERROR_MORE_DATA)
  435. {
  436. max_value_len *= 2;
  437. value_name = heap_xrealloc(value_name, max_value_len * sizeof(WCHAR));
  438. }
  439. else break;
  440. }
  441. heap_free(value_name);
  442. }
  443. else if (value_name || value_empty)
  444. {
  445. if (RegDeleteValueW(key, value_empty ? NULL : value_name) != ERROR_SUCCESS)
  446. {
  447. RegCloseKey(key);
  448. output_message(STRING_CANNOT_FIND);
  449. return 1;
  450. }
  451. }
  452. RegCloseKey(key);
  453. output_message(STRING_SUCCESS);
  454. return 0;
  455. }
  456. static WCHAR *reg_data_to_wchar(DWORD type, const BYTE *src, DWORD size_bytes)
  457. {
  458. WCHAR *buffer = NULL;
  459. int i;
  460. switch (type)
  461. {
  462. case REG_SZ:
  463. case REG_EXPAND_SZ:
  464. buffer = heap_xalloc(size_bytes);
  465. lstrcpyW(buffer, (WCHAR *)src);
  466. break;
  467. case REG_NONE:
  468. case REG_BINARY:
  469. {
  470. WCHAR *ptr;
  471. static const WCHAR fmt[] = {'%','0','2','X',0};
  472. buffer = heap_xalloc((size_bytes * 2 + 1) * sizeof(WCHAR));
  473. ptr = buffer;
  474. for (i = 0; i < size_bytes; i++)
  475. ptr += swprintf(ptr, 3, fmt, src[i]);
  476. break;
  477. }
  478. case REG_DWORD:
  479. /* case REG_DWORD_LITTLE_ENDIAN: */
  480. case REG_DWORD_BIG_ENDIAN:
  481. {
  482. const int zero_x_dword = 10;
  483. static const WCHAR fmt[] = {'0','x','%','x',0};
  484. buffer = heap_xalloc((zero_x_dword + 1) * sizeof(WCHAR));
  485. swprintf(buffer, zero_x_dword + 1, fmt, *(DWORD *)src);
  486. break;
  487. }
  488. case REG_MULTI_SZ:
  489. {
  490. const int two_wchars = 2 * sizeof(WCHAR);
  491. DWORD tmp_size;
  492. const WCHAR *tmp = (const WCHAR *)src;
  493. int len, destindex;
  494. if (size_bytes <= two_wchars)
  495. {
  496. buffer = heap_xalloc(sizeof(WCHAR));
  497. *buffer = 0;
  498. return buffer;
  499. }
  500. tmp_size = size_bytes - two_wchars; /* exclude both null terminators */
  501. buffer = heap_xalloc(tmp_size * 2 + sizeof(WCHAR));
  502. len = tmp_size / sizeof(WCHAR);
  503. for (i = 0, destindex = 0; i < len; i++, destindex++)
  504. {
  505. if (tmp[i])
  506. buffer[destindex] = tmp[i];
  507. else
  508. {
  509. buffer[destindex++] = '\\';
  510. buffer[destindex] = '0';
  511. }
  512. }
  513. buffer[destindex] = 0;
  514. break;
  515. }
  516. }
  517. return buffer;
  518. }
  519. static const WCHAR *reg_type_to_wchar(DWORD type)
  520. {
  521. int i, array_size = ARRAY_SIZE(type_rels);
  522. for (i = 0; i < array_size; i++)
  523. {
  524. if (type == type_rels[i].type)
  525. return type_rels[i].name;
  526. }
  527. return NULL;
  528. }
  529. static void output_value(const WCHAR *value_name, DWORD type, BYTE *data, DWORD data_size)
  530. {
  531. static const WCHAR fmt[] = {' ',' ',' ',' ','%','1',0};
  532. WCHAR defval[32];
  533. WCHAR *reg_data;
  534. if (value_name && value_name[0])
  535. output_string(fmt, value_name);
  536. else
  537. {
  538. LoadStringW(GetModuleHandleW(NULL), STRING_DEFAULT_VALUE, defval, ARRAY_SIZE(defval));
  539. output_string(fmt, defval);
  540. }
  541. output_string(fmt, reg_type_to_wchar(type));
  542. if (data)
  543. {
  544. reg_data = reg_data_to_wchar(type, data, data_size);
  545. output_string(fmt, reg_data);
  546. heap_free(reg_data);
  547. }
  548. else
  549. {
  550. LoadStringW(GetModuleHandleW(NULL), STRING_VALUE_NOT_SET, defval, ARRAY_SIZE(defval));
  551. output_string(fmt, defval);
  552. }
  553. output_string(newlineW);
  554. }
  555. WCHAR *build_subkey_path(WCHAR *path, DWORD path_len, WCHAR *subkey_name, DWORD subkey_len)
  556. {
  557. WCHAR *subkey_path;
  558. static const WCHAR fmt[] = {'%','s','\\','%','s',0};
  559. subkey_path = heap_xalloc((path_len + subkey_len + 2) * sizeof(WCHAR));
  560. swprintf(subkey_path, path_len + subkey_len + 2, fmt, path, subkey_name);
  561. return subkey_path;
  562. }
  563. static unsigned int num_values_found = 0;
  564. static int query_value(HKEY key, WCHAR *value_name, WCHAR *path, BOOL recurse)
  565. {
  566. LONG rc;
  567. DWORD max_data_bytes = 2048, data_size;
  568. DWORD subkey_len;
  569. DWORD type, path_len, i;
  570. BYTE *data;
  571. WCHAR fmt[] = {'%','1','\n',0};
  572. WCHAR *subkey_name, *subkey_path;
  573. HKEY subkey;
  574. data = heap_xalloc(max_data_bytes);
  575. for (;;)
  576. {
  577. data_size = max_data_bytes;
  578. rc = RegQueryValueExW(key, value_name, NULL, &type, data, &data_size);
  579. if (rc == ERROR_MORE_DATA)
  580. {
  581. max_data_bytes = data_size;
  582. data = heap_xrealloc(data, max_data_bytes);
  583. }
  584. else break;
  585. }
  586. if (rc == ERROR_SUCCESS)
  587. {
  588. output_string(fmt, path);
  589. output_value(value_name, type, data, data_size);
  590. output_string(newlineW);
  591. num_values_found++;
  592. }
  593. heap_free(data);
  594. if (!recurse)
  595. {
  596. if (rc == ERROR_FILE_NOT_FOUND)
  597. {
  598. if (value_name && *value_name)
  599. {
  600. output_message(STRING_CANNOT_FIND);
  601. return 1;
  602. }
  603. output_string(fmt, path);
  604. output_value(NULL, REG_SZ, NULL, 0);
  605. }
  606. return 0;
  607. }
  608. subkey_name = heap_xalloc(MAX_SUBKEY_LEN * sizeof(WCHAR));
  609. path_len = lstrlenW(path);
  610. i = 0;
  611. for (;;)
  612. {
  613. subkey_len = MAX_SUBKEY_LEN;
  614. rc = RegEnumKeyExW(key, i, subkey_name, &subkey_len, NULL, NULL, NULL, NULL);
  615. if (rc == ERROR_SUCCESS)
  616. {
  617. subkey_path = build_subkey_path(path, path_len, subkey_name, subkey_len);
  618. if (!RegOpenKeyExW(key, subkey_name, 0, KEY_READ, &subkey))
  619. {
  620. query_value(subkey, value_name, subkey_path, recurse);
  621. RegCloseKey(subkey);
  622. }
  623. heap_free(subkey_path);
  624. i++;
  625. }
  626. else break;
  627. }
  628. heap_free(subkey_name);
  629. return 0;
  630. }
  631. static int query_all(HKEY key, WCHAR *path, BOOL recurse)
  632. {
  633. LONG rc;
  634. DWORD max_value_len = 256, value_len;
  635. DWORD max_data_bytes = 2048, data_size;
  636. DWORD subkey_len;
  637. DWORD i, type, path_len;
  638. WCHAR fmt[] = {'%','1','\n',0};
  639. WCHAR fmt_path[] = {'%','1','\\','%','2','\n',0};
  640. WCHAR *value_name, *subkey_name, *subkey_path;
  641. BYTE *data;
  642. HKEY subkey;
  643. output_string(fmt, path);
  644. value_name = heap_xalloc(max_value_len * sizeof(WCHAR));
  645. data = heap_xalloc(max_data_bytes);
  646. i = 0;
  647. for (;;)
  648. {
  649. value_len = max_value_len;
  650. data_size = max_data_bytes;
  651. rc = RegEnumValueW(key, i, value_name, &value_len, NULL, &type, data, &data_size);
  652. if (rc == ERROR_SUCCESS)
  653. {
  654. output_value(value_name, type, data, data_size);
  655. i++;
  656. }
  657. else if (rc == ERROR_MORE_DATA)
  658. {
  659. if (data_size > max_data_bytes)
  660. {
  661. max_data_bytes = data_size;
  662. data = heap_xrealloc(data, max_data_bytes);
  663. }
  664. else
  665. {
  666. max_value_len *= 2;
  667. value_name = heap_xrealloc(value_name, max_value_len * sizeof(WCHAR));
  668. }
  669. }
  670. else break;
  671. }
  672. heap_free(data);
  673. heap_free(value_name);
  674. if (i || recurse)
  675. output_string(newlineW);
  676. subkey_name = heap_xalloc(MAX_SUBKEY_LEN * sizeof(WCHAR));
  677. path_len = lstrlenW(path);
  678. i = 0;
  679. for (;;)
  680. {
  681. subkey_len = MAX_SUBKEY_LEN;
  682. rc = RegEnumKeyExW(key, i, subkey_name, &subkey_len, NULL, NULL, NULL, NULL);
  683. if (rc == ERROR_SUCCESS)
  684. {
  685. if (recurse)
  686. {
  687. subkey_path = build_subkey_path(path, path_len, subkey_name, subkey_len);
  688. if (!RegOpenKeyExW(key, subkey_name, 0, KEY_READ, &subkey))
  689. {
  690. query_all(subkey, subkey_path, recurse);
  691. RegCloseKey(subkey);
  692. }
  693. heap_free(subkey_path);
  694. }
  695. else output_string(fmt_path, path, subkey_name);
  696. i++;
  697. }
  698. else break;
  699. }
  700. heap_free(subkey_name);
  701. if (i && !recurse)
  702. output_string(newlineW);
  703. return 0;
  704. }
  705. static int reg_query(HKEY root, WCHAR *path, WCHAR *key_name, WCHAR *value_name,
  706. BOOL value_empty, BOOL recurse)
  707. {
  708. HKEY key;
  709. int ret;
  710. if (RegOpenKeyExW(root, path, 0, KEY_READ, &key) != ERROR_SUCCESS)
  711. {
  712. output_message(STRING_CANNOT_FIND);
  713. return 1;
  714. }
  715. output_string(newlineW);
  716. if (value_name || value_empty)
  717. {
  718. ret = query_value(key, value_name, key_name, recurse);
  719. if (recurse)
  720. output_message(STRING_MATCHES_FOUND, num_values_found);
  721. }
  722. else
  723. ret = query_all(key, key_name, recurse);
  724. RegCloseKey(key);
  725. return ret;
  726. }
  727. static WCHAR *get_long_key(HKEY root, WCHAR *path)
  728. {
  729. DWORD i, array_size = ARRAY_SIZE(root_rels), len;
  730. WCHAR *long_key;
  731. WCHAR fmt[] = {'%','s','\\','%','s',0};
  732. for (i = 0; i < array_size; i++)
  733. {
  734. if (root == root_rels[i].key)
  735. break;
  736. }
  737. len = lstrlenW(root_rels[i].long_name);
  738. if (!path)
  739. {
  740. long_key = heap_xalloc((len + 1) * sizeof(WCHAR));
  741. lstrcpyW(long_key, root_rels[i].long_name);
  742. return long_key;
  743. }
  744. len += lstrlenW(path) + 1; /* add one for the backslash */
  745. long_key = heap_xalloc((len + 1) * sizeof(WCHAR));
  746. swprintf(long_key, len + 1, fmt, root_rels[i].long_name, path);
  747. return long_key;
  748. }
  749. BOOL parse_registry_key(const WCHAR *key, HKEY *root, WCHAR **path, WCHAR **long_key)
  750. {
  751. if (!sane_path(key))
  752. return FALSE;
  753. *path = wcschr(key, '\\');
  754. if (*path) (*path)++;
  755. *root = path_get_rootkey(key);
  756. if (!*root)
  757. {
  758. if (*path) *(*path - 1) = 0;
  759. output_message(STRING_INVALID_SYSTEM_KEY, key);
  760. return FALSE;
  761. }
  762. *long_key = get_long_key(*root, *path);
  763. return TRUE;
  764. }
  765. static BOOL is_switch(const WCHAR *s, const WCHAR c)
  766. {
  767. if (lstrlenW(s) > 2)
  768. return FALSE;
  769. if ((s[0] == '/' || s[0] == '-') && (s[1] == c || s[1] == towupper(c)))
  770. return TRUE;
  771. return FALSE;
  772. }
  773. static BOOL is_help_switch(const WCHAR *s)
  774. {
  775. if (is_switch(s, '?') || is_switch(s, 'h'))
  776. return TRUE;
  777. return FALSE;
  778. }
  779. enum operations {
  780. REG_ADD,
  781. REG_DELETE,
  782. REG_IMPORT,
  783. REG_EXPORT,
  784. REG_QUERY,
  785. REG_INVALID
  786. };
  787. static enum operations get_operation(const WCHAR *str, int *op_help)
  788. {
  789. struct op_info { const WCHAR *op; int id; int help_id; };
  790. static const WCHAR add[] = {'a','d','d',0};
  791. static const WCHAR delete[] = {'d','e','l','e','t','e',0};
  792. static const WCHAR import[] = {'i','m','p','o','r','t',0};
  793. static const WCHAR export[] = {'e','x','p','o','r','t',0};
  794. static const WCHAR query[] = {'q','u','e','r','y',0};
  795. static const struct op_info op_array[] =
  796. {
  797. { add, REG_ADD, STRING_ADD_USAGE },
  798. { delete, REG_DELETE, STRING_DELETE_USAGE },
  799. { import, REG_IMPORT, STRING_IMPORT_USAGE },
  800. { export, REG_EXPORT, STRING_EXPORT_USAGE },
  801. { query, REG_QUERY, STRING_QUERY_USAGE },
  802. { NULL, -1, 0 }
  803. };
  804. const struct op_info *ptr;
  805. for (ptr = op_array; ptr->op; ptr++)
  806. {
  807. if (!lstrcmpiW(str, ptr->op))
  808. {
  809. *op_help = ptr->help_id;
  810. return ptr->id;
  811. }
  812. }
  813. return REG_INVALID;
  814. }
  815. int __cdecl wmain(int argc, WCHAR *argvW[])
  816. {
  817. int i, op, op_help, ret;
  818. BOOL show_op_help = FALSE;
  819. static const WCHAR switchVAW[] = {'v','a',0};
  820. static const WCHAR switchVEW[] = {'v','e',0};
  821. WCHAR *key_name, *path, *value_name = NULL, *type = NULL, *data = NULL, separator = '\0';
  822. BOOL value_empty = FALSE, value_all = FALSE, recurse = FALSE, force = FALSE;
  823. HKEY root;
  824. if (argc == 1)
  825. {
  826. output_message(STRING_INVALID_SYNTAX);
  827. output_message(STRING_REG_HELP);
  828. return 1;
  829. }
  830. if (is_help_switch(argvW[1]))
  831. {
  832. output_message(STRING_USAGE);
  833. return 0;
  834. }
  835. op = get_operation(argvW[1], &op_help);
  836. if (op == REG_INVALID)
  837. {
  838. output_message(STRING_INVALID_OPTION, argvW[1]);
  839. output_message(STRING_REG_HELP);
  840. return 1;
  841. }
  842. if (argc > 2)
  843. show_op_help = is_help_switch(argvW[2]);
  844. if (argc == 2 || ((show_op_help || op == REG_IMPORT) && argc > 3))
  845. {
  846. output_message(STRING_INVALID_SYNTAX);
  847. output_message(STRING_FUNC_HELP, wcsupr(argvW[1]));
  848. return 1;
  849. }
  850. else if (show_op_help)
  851. {
  852. output_message(op_help);
  853. return 0;
  854. }
  855. if (op == REG_IMPORT)
  856. return reg_import(argvW[2]);
  857. if (op == REG_EXPORT)
  858. return reg_export(argc, argvW);
  859. if (!parse_registry_key(argvW[2], &root, &path, &key_name))
  860. return 1;
  861. for (i = 3; i < argc; i++)
  862. {
  863. if (argvW[i][0] == '/' || argvW[i][0] == '-')
  864. {
  865. WCHAR *ptr = &argvW[i][1];
  866. if (!lstrcmpiW(ptr, switchVEW))
  867. {
  868. value_empty = TRUE;
  869. continue;
  870. }
  871. else if (!lstrcmpiW(ptr, switchVAW))
  872. {
  873. value_all = TRUE;
  874. continue;
  875. }
  876. else if (!ptr[0] || ptr[1])
  877. {
  878. output_message(STRING_INVALID_CMDLINE);
  879. return 1;
  880. }
  881. switch(towlower(argvW[i][1]))
  882. {
  883. case 'v':
  884. if (value_name || !(value_name = argvW[++i]))
  885. {
  886. output_message(STRING_INVALID_CMDLINE);
  887. return 1;
  888. }
  889. break;
  890. case 't':
  891. if (type || !(type = argvW[++i]))
  892. {
  893. output_message(STRING_INVALID_CMDLINE);
  894. return 1;
  895. }
  896. break;
  897. case 'd':
  898. if (data || !(data = argvW[++i]))
  899. {
  900. output_message(STRING_INVALID_CMDLINE);
  901. return 1;
  902. }
  903. break;
  904. case 's':
  905. if (op == REG_QUERY)
  906. {
  907. recurse = TRUE;
  908. break;
  909. }
  910. ptr = argvW[++i];
  911. if (!ptr || lstrlenW(ptr) != 1)
  912. {
  913. output_message(STRING_INVALID_CMDLINE);
  914. return 1;
  915. }
  916. separator = ptr[0];
  917. break;
  918. case 'f':
  919. force = TRUE;
  920. break;
  921. default:
  922. output_message(STRING_INVALID_CMDLINE);
  923. return 1;
  924. }
  925. }
  926. }
  927. if ((value_name && value_empty) || (value_name && value_all) || (value_empty && value_all))
  928. {
  929. output_message(STRING_INVALID_CMDLINE);
  930. return 1;
  931. }
  932. if (op == REG_ADD)
  933. ret = reg_add(root, path, value_name, value_empty, type, separator, data, force);
  934. else if (op == REG_DELETE)
  935. ret = reg_delete(root, path, key_name, value_name, value_empty, value_all, force);
  936. else
  937. ret = reg_query(root, path, key_name, value_name, value_empty, recurse);
  938. return ret;
  939. }