add.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Copyright 2016-2017, 2021 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 "reg.h"
  20. static DWORD wchar_get_type(const WCHAR *type_name)
  21. {
  22. DWORD i;
  23. if (!type_name)
  24. return REG_SZ;
  25. for (i = 0; i < ARRAY_SIZE(type_rels); i++)
  26. {
  27. if (!wcsicmp(type_rels[i].name, type_name))
  28. return type_rels[i].type;
  29. }
  30. return ~0u;
  31. }
  32. /* hexchar_to_byte from programs/regedit/hexedit.c */
  33. static inline BYTE hexchar_to_byte(WCHAR ch)
  34. {
  35. if (ch >= '0' && ch <= '9')
  36. return ch - '0';
  37. else if (ch >= 'a' && ch <= 'f')
  38. return ch - 'a' + 10;
  39. else if (ch >= 'A' && ch <= 'F')
  40. return ch - 'A' + 10;
  41. else
  42. return -1;
  43. }
  44. static BOOL get_regdata(const WCHAR *data, DWORD reg_type, WCHAR separator,
  45. BYTE **data_bytes, DWORD *size_bytes)
  46. {
  47. static const WCHAR empty;
  48. *size_bytes = 0;
  49. if (!data) data = &empty;
  50. switch (reg_type)
  51. {
  52. case REG_NONE:
  53. case REG_SZ:
  54. case REG_EXPAND_SZ:
  55. {
  56. *size_bytes = (lstrlenW(data) + 1) * sizeof(WCHAR);
  57. *data_bytes = malloc(*size_bytes);
  58. lstrcpyW((WCHAR *)*data_bytes, data);
  59. break;
  60. }
  61. case REG_DWORD:
  62. /* case REG_DWORD_LITTLE_ENDIAN: */
  63. case REG_DWORD_BIG_ENDIAN: /* Yes, this is correct! */
  64. {
  65. LPWSTR rest;
  66. unsigned long val;
  67. val = wcstoul(data, &rest, (towlower(data[1]) == 'x') ? 16 : 10);
  68. if (*rest || data[0] == '-' || (val == ~0u && errno == ERANGE)) {
  69. output_message(STRING_MISSING_NUMBER);
  70. return FALSE;
  71. }
  72. *size_bytes = sizeof(DWORD);
  73. *data_bytes = malloc(*size_bytes);
  74. *(DWORD *)*data_bytes = val;
  75. break;
  76. }
  77. case REG_BINARY:
  78. {
  79. BYTE hex0, hex1, *ptr;
  80. int i = 0, destByteIndex = 0, datalen = lstrlenW(data);
  81. if (!datalen) return TRUE;
  82. *size_bytes = ((datalen + datalen % 2) / 2) * sizeof(BYTE);
  83. *data_bytes = malloc(*size_bytes);
  84. if (datalen % 2)
  85. {
  86. hex1 = hexchar_to_byte(data[i++]);
  87. if (hex1 == 0xFF)
  88. goto no_hex_data;
  89. *data_bytes[destByteIndex++] = hex1;
  90. }
  91. ptr = *data_bytes;
  92. for (; i + 1 < datalen; i += 2)
  93. {
  94. hex0 = hexchar_to_byte(data[i]);
  95. hex1 = hexchar_to_byte(data[i + 1]);
  96. if (hex0 == 0xFF || hex1 == 0xFF)
  97. goto no_hex_data;
  98. ptr[destByteIndex++] = (hex0 << 4) | hex1;
  99. }
  100. break;
  101. no_hex_data:
  102. free(*data_bytes);
  103. *data_bytes = NULL;
  104. output_message(STRING_MISSING_HEXDATA);
  105. return FALSE;
  106. }
  107. case REG_MULTI_SZ:
  108. {
  109. int i, destindex, len = lstrlenW(data);
  110. WCHAR *buffer = malloc((len + 2) * sizeof(WCHAR));
  111. for (i = 0, destindex = 0; i < len; i++, destindex++)
  112. {
  113. if (!separator && data[i] == '\\' && data[i + 1] == '0')
  114. {
  115. buffer[destindex] = 0;
  116. i++;
  117. }
  118. else if (data[i] == separator)
  119. buffer[destindex] = 0;
  120. else
  121. buffer[destindex] = data[i];
  122. if (destindex && !buffer[destindex - 1] && (!buffer[destindex] || destindex == 1))
  123. {
  124. free(buffer);
  125. output_message(STRING_INVALID_STRING);
  126. return FALSE;
  127. }
  128. }
  129. buffer[destindex] = 0;
  130. if (destindex && buffer[destindex - 1])
  131. buffer[++destindex] = 0;
  132. *size_bytes = (destindex + 1) * sizeof(WCHAR);
  133. *data_bytes = (BYTE *)buffer;
  134. break;
  135. }
  136. default:
  137. output_message(STRING_UNHANDLED_TYPE, reg_type, data);
  138. }
  139. return TRUE;
  140. }
  141. static int run_add(HKEY root, WCHAR *path, REGSAM sam, WCHAR *value_name, BOOL value_empty,
  142. WCHAR *type, WCHAR separator, WCHAR *data, BOOL force)
  143. {
  144. HKEY hkey;
  145. DWORD dispos, data_type, data_size;
  146. BYTE *reg_data = NULL;
  147. LONG rc;
  148. if (RegCreateKeyExW(root, path, 0, NULL, REG_OPTION_NON_VOLATILE,
  149. KEY_READ|KEY_WRITE|sam, NULL, &hkey, &dispos))
  150. {
  151. output_message(STRING_ACCESS_DENIED);
  152. return 1;
  153. }
  154. if (!force && dispos == REG_OPENED_EXISTING_KEY)
  155. {
  156. if (RegQueryValueExW(hkey, value_name, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
  157. {
  158. if (!ask_confirm(STRING_OVERWRITE_VALUE, value_name))
  159. {
  160. RegCloseKey(hkey);
  161. output_message(STRING_CANCELLED);
  162. return 0;
  163. }
  164. }
  165. }
  166. data_type = wchar_get_type(type);
  167. if (data_type == ~0u)
  168. {
  169. RegCloseKey(hkey);
  170. output_message(STRING_UNSUPPORTED_TYPE, type);
  171. return 1;
  172. }
  173. if ((data_type == REG_DWORD || data_type == REG_DWORD_BIG_ENDIAN) && !data)
  174. {
  175. RegCloseKey(hkey);
  176. output_message(STRING_INVALID_CMDLINE);
  177. return 1;
  178. }
  179. if (!get_regdata(data, data_type, separator, &reg_data, &data_size))
  180. {
  181. RegCloseKey(hkey);
  182. return 1;
  183. }
  184. rc = RegSetValueExW(hkey, value_name, 0, data_type, reg_data, data_size);
  185. free(reg_data);
  186. RegCloseKey(hkey);
  187. if (rc)
  188. {
  189. output_message(STRING_ACCESS_DENIED);
  190. return 1;
  191. }
  192. output_message(STRING_SUCCESS);
  193. return 0;
  194. }
  195. int reg_add(int argc, WCHAR *argvW[])
  196. {
  197. HKEY root;
  198. WCHAR *path, *value_name = NULL, *type = NULL, *data = NULL, separator = '\0';
  199. BOOL value_empty = FALSE, force = FALSE;
  200. REGSAM sam = 0;
  201. int i;
  202. if (!parse_registry_key(argvW[2], &root, &path))
  203. return 1;
  204. for (i = 3; i < argc; i++)
  205. {
  206. WCHAR *str;
  207. if (argvW[i][0] != '/' && argvW[i][0] != '-')
  208. goto invalid;
  209. str = &argvW[i][1];
  210. if (!lstrcmpiW(str, L"ve"))
  211. {
  212. if (value_empty) goto invalid;
  213. value_empty = TRUE;
  214. continue;
  215. }
  216. else if (!lstrcmpiW(str, L"reg:32"))
  217. {
  218. if (sam & KEY_WOW64_32KEY) goto invalid;
  219. sam |= KEY_WOW64_32KEY;
  220. continue;
  221. }
  222. else if (!lstrcmpiW(str, L"reg:64"))
  223. {
  224. if (sam & KEY_WOW64_64KEY) goto invalid;
  225. sam |= KEY_WOW64_64KEY;
  226. continue;
  227. }
  228. else if (!str[0] || str[1])
  229. goto invalid;
  230. switch (towlower(*str))
  231. {
  232. case 'v':
  233. if (value_name || !(value_name = argvW[++i]))
  234. goto invalid;
  235. break;
  236. case 't':
  237. if (type || !(type = argvW[++i]))
  238. goto invalid;
  239. break;
  240. case 'd':
  241. if (data || !(data = argvW[++i]))
  242. goto invalid;
  243. break;
  244. case 's':
  245. str = argvW[++i];
  246. if (separator || !str || lstrlenW(str) != 1)
  247. goto invalid;
  248. separator = str[0];
  249. break;
  250. case 'f':
  251. if (force) goto invalid;
  252. force = TRUE;
  253. break;
  254. default:
  255. goto invalid;
  256. }
  257. }
  258. if (value_name && value_empty)
  259. goto invalid;
  260. if (sam == (KEY_WOW64_32KEY|KEY_WOW64_64KEY))
  261. goto invalid;
  262. return run_add(root, path, sam, value_name, value_empty, type, separator, data, force);
  263. invalid:
  264. output_message(STRING_INVALID_SYNTAX);
  265. output_message(STRING_FUNC_HELP, wcsupr(argvW[1]));
  266. return 1;
  267. }