registry_reg.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /**
  2. * WinPR: Windows Portable Runtime
  3. * Windows Registry (.reg file format)
  4. *
  5. * Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include <errno.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <winpr/wtypes.h>
  27. #include <winpr/crt.h>
  28. #include "registry_reg.h"
  29. #include "../log.h"
  30. #define TAG WINPR_TAG("registry")
  31. #define WINPR_HKLM_HIVE "/etc/winpr/HKLM.reg"
  32. static void reg_print_key(Reg* reg, RegKey* key);
  33. static void reg_print_value(Reg* reg, RegVal* value);
  34. struct reg_data_type
  35. {
  36. char* tag;
  37. int length;
  38. DWORD type;
  39. };
  40. static struct reg_data_type REG_DATA_TYPE_TABLE[] = { { "\"", 1, REG_SZ },
  41. { "dword:", 6, REG_DWORD },
  42. { "str:\"", 5, REG_SZ },
  43. { "str(2):\"", 8, REG_EXPAND_SZ },
  44. { "str(7):\"", 8, REG_MULTI_SZ },
  45. { "hex:", 4, REG_BINARY },
  46. { "hex(2):\"", 8, REG_EXPAND_SZ },
  47. { "hex(7):\"", 8, REG_MULTI_SZ },
  48. { "hex(b):\"", 8, REG_QWORD },
  49. { NULL, 0, 0 } };
  50. static char* REG_DATA_TYPE_STRINGS[] = { "REG_NONE",
  51. "REG_SZ",
  52. "REG_EXPAND_SZ",
  53. "REG_BINARY",
  54. "REG_DWORD",
  55. "REG_DWORD_BIG_ENDIAN",
  56. "REG_LINK",
  57. "REG_MULTI_SZ",
  58. "REG_RESOURCE_LIST",
  59. "REG_FULL_RESOURCE_DESCRIPTOR",
  60. "REG_RESOURCE_REQUIREMENTS_LIST",
  61. "REG_QWORD" };
  62. static void reg_load_start(Reg* reg)
  63. {
  64. INT64 file_size;
  65. _fseeki64(reg->fp, 0, SEEK_END);
  66. file_size = _ftelli64(reg->fp);
  67. _fseeki64(reg->fp, 0, SEEK_SET);
  68. reg->line = NULL;
  69. reg->next_line = NULL;
  70. reg->buffer = NULL;
  71. if (file_size < 1)
  72. return;
  73. reg->buffer = (char*)malloc(file_size + 2);
  74. if (!reg->buffer)
  75. return;
  76. if (fread(reg->buffer, file_size, 1, reg->fp) != 1)
  77. {
  78. free(reg->buffer);
  79. reg->buffer = NULL;
  80. return;
  81. }
  82. reg->buffer[file_size] = '\n';
  83. reg->buffer[file_size + 1] = '\0';
  84. reg->next_line = strtok(reg->buffer, "\n");
  85. }
  86. static void reg_load_finish(Reg* reg)
  87. {
  88. if (!reg)
  89. return;
  90. if (reg->buffer)
  91. {
  92. free(reg->buffer);
  93. reg->buffer = NULL;
  94. }
  95. }
  96. static RegVal* reg_load_value(Reg* reg, RegKey* key)
  97. {
  98. int index;
  99. char* p[5];
  100. int length;
  101. char* name;
  102. char* type;
  103. char* data;
  104. RegVal* value;
  105. p[0] = reg->line + 1;
  106. p[1] = strstr(p[0], "\"=");
  107. p[2] = p[1] + 2;
  108. type = p[2];
  109. if (p[2][0] == '"')
  110. p[3] = p[2];
  111. else
  112. p[3] = strchr(p[2], ':');
  113. data = p[3] + 1;
  114. length = p[1] - p[0];
  115. name = (char*)malloc(length + 1);
  116. if (!name)
  117. return NULL;
  118. memcpy(name, p[0], length);
  119. name[length] = '\0';
  120. value = (RegVal*)malloc(sizeof(RegVal));
  121. if (!value)
  122. {
  123. free(name);
  124. return NULL;
  125. }
  126. value->name = name;
  127. value->type = REG_NONE;
  128. value->next = value->prev = NULL;
  129. for (index = 0; REG_DATA_TYPE_TABLE[index].length > 0; index++)
  130. {
  131. if (strncmp(type, REG_DATA_TYPE_TABLE[index].tag, REG_DATA_TYPE_TABLE[index].length) == 0)
  132. {
  133. value->type = REG_DATA_TYPE_TABLE[index].type;
  134. break;
  135. }
  136. }
  137. if (value->type == REG_DWORD)
  138. {
  139. unsigned long val;
  140. errno = 0;
  141. val = strtoul(data, NULL, 16);
  142. if ((errno != 0) || (val > UINT32_MAX))
  143. {
  144. free(value);
  145. free(name);
  146. return NULL;
  147. }
  148. value->data.dword = val;
  149. }
  150. else if (value->type == REG_SZ)
  151. {
  152. p[4] = strchr(data, '"');
  153. p[4][0] = '\0';
  154. value->data.string = _strdup(data);
  155. if (!value->data.string)
  156. {
  157. free(value);
  158. free(name);
  159. return NULL;
  160. }
  161. }
  162. else
  163. {
  164. WLog_ERR(TAG, "unimplemented format: %s", REG_DATA_TYPE_STRINGS[value->type]);
  165. }
  166. if (!key->values)
  167. {
  168. key->values = value;
  169. }
  170. else
  171. {
  172. RegVal* pValue = key->values;
  173. while (pValue->next != NULL)
  174. {
  175. pValue = pValue->next;
  176. }
  177. pValue->next = value;
  178. value->prev = pValue;
  179. }
  180. return value;
  181. }
  182. static BOOL reg_load_has_next_line(Reg* reg)
  183. {
  184. if (!reg)
  185. return 0;
  186. return (reg->next_line != NULL) ? 1 : 0;
  187. }
  188. static char* reg_load_get_next_line(Reg* reg)
  189. {
  190. if (!reg)
  191. return NULL;
  192. reg->line = reg->next_line;
  193. reg->next_line = strtok(NULL, "\n");
  194. reg->line_length = strlen(reg->line);
  195. return reg->line;
  196. }
  197. static char* reg_load_peek_next_line(Reg* reg)
  198. {
  199. return reg->next_line;
  200. }
  201. static void reg_insert_key(Reg* reg, RegKey* key, RegKey* subkey)
  202. {
  203. char* name;
  204. char* path;
  205. char* save;
  206. int length;
  207. path = _strdup(subkey->name);
  208. if (!path)
  209. return;
  210. name = strtok_s(path, "\\", &save);
  211. while (name != NULL)
  212. {
  213. if (strcmp(key->name, name) == 0)
  214. {
  215. length = strlen(name);
  216. name += length + 1;
  217. subkey->subname = _strdup(name);
  218. /* TODO: free allocated memory in error case */
  219. if (!subkey->subname)
  220. {
  221. free(path);
  222. return;
  223. }
  224. }
  225. name = strtok_s(NULL, "\\", &save);
  226. }
  227. free(path);
  228. }
  229. static RegKey* reg_load_key(Reg* reg, RegKey* key)
  230. {
  231. char* p[2];
  232. int length;
  233. char* line;
  234. RegKey* subkey;
  235. p[0] = reg->line + 1;
  236. p[1] = strrchr(p[0], ']');
  237. subkey = (RegKey*)malloc(sizeof(RegKey));
  238. if (!subkey)
  239. return NULL;
  240. subkey->values = NULL;
  241. subkey->prev = subkey->next = NULL;
  242. length = p[1] - p[0];
  243. subkey->name = (char*)malloc(length + 1);
  244. if (!subkey->name)
  245. {
  246. free(subkey);
  247. return NULL;
  248. }
  249. memcpy(subkey->name, p[0], length);
  250. subkey->name[length] = '\0';
  251. while (reg_load_has_next_line(reg))
  252. {
  253. line = reg_load_peek_next_line(reg);
  254. if (line[0] == '[')
  255. break;
  256. reg_load_get_next_line(reg);
  257. if (reg->line[0] == '"')
  258. {
  259. reg_load_value(reg, subkey);
  260. }
  261. }
  262. reg_insert_key(reg, key, subkey);
  263. if (!key->subkeys)
  264. {
  265. key->subkeys = subkey;
  266. }
  267. else
  268. {
  269. RegKey* pKey = key->subkeys;
  270. while (pKey->next != NULL)
  271. {
  272. pKey = pKey->next;
  273. }
  274. pKey->next = subkey;
  275. subkey->prev = pKey;
  276. }
  277. return subkey;
  278. }
  279. static void reg_load(Reg* reg)
  280. {
  281. reg_load_start(reg);
  282. while (reg_load_has_next_line(reg))
  283. {
  284. reg_load_get_next_line(reg);
  285. if (reg->line[0] == '[')
  286. {
  287. reg_load_key(reg, reg->root_key);
  288. }
  289. }
  290. reg_load_finish(reg);
  291. }
  292. static void reg_unload_value(Reg* reg, RegVal* value)
  293. {
  294. if (value->type == REG_DWORD)
  295. {
  296. }
  297. else if (value->type == REG_SZ)
  298. {
  299. free(value->data.string);
  300. }
  301. else
  302. {
  303. WLog_ERR(TAG, "unimplemented format: %s", REG_DATA_TYPE_STRINGS[value->type]);
  304. }
  305. free(value);
  306. }
  307. static void reg_unload_key(Reg* reg, RegKey* key)
  308. {
  309. RegVal* pValue;
  310. RegVal* pValueNext;
  311. pValue = key->values;
  312. while (pValue != NULL)
  313. {
  314. pValueNext = pValue->next;
  315. reg_unload_value(reg, pValue);
  316. pValue = pValueNext;
  317. }
  318. free(key->name);
  319. free(key);
  320. }
  321. static void reg_unload(Reg* reg)
  322. {
  323. RegKey* pKey;
  324. RegKey* pKeyNext;
  325. pKey = reg->root_key->subkeys;
  326. while (pKey != NULL)
  327. {
  328. pKeyNext = pKey->next;
  329. reg_unload_key(reg, pKey);
  330. pKey = pKeyNext;
  331. }
  332. free(reg->root_key);
  333. }
  334. Reg* reg_open(BOOL read_only)
  335. {
  336. Reg* reg;
  337. reg = (Reg*)malloc(sizeof(Reg));
  338. if (!reg)
  339. return NULL;
  340. reg->read_only = read_only;
  341. reg->filename = WINPR_HKLM_HIVE;
  342. if (reg->read_only)
  343. {
  344. reg->fp = fopen(reg->filename, "r");
  345. }
  346. else
  347. {
  348. reg->fp = fopen(reg->filename, "r+");
  349. if (!reg->fp)
  350. reg->fp = fopen(reg->filename, "w+");
  351. }
  352. if (!reg->fp)
  353. {
  354. free(reg);
  355. return NULL;
  356. }
  357. reg->root_key = (RegKey*)malloc(sizeof(RegKey));
  358. if (!reg->root_key)
  359. {
  360. fclose(reg->fp);
  361. free(reg);
  362. return NULL;
  363. }
  364. reg->root_key->values = NULL;
  365. reg->root_key->subkeys = NULL;
  366. reg->root_key->name = "HKEY_LOCAL_MACHINE";
  367. reg_load(reg);
  368. return reg;
  369. }
  370. void reg_close(Reg* reg)
  371. {
  372. if (reg)
  373. {
  374. reg_unload(reg);
  375. fclose(reg->fp);
  376. free(reg);
  377. }
  378. }
  379. void reg_print_value(Reg* reg, RegVal* value)
  380. {
  381. WLog_INFO(TAG, "\"%s\"=", value->name);
  382. if (value->type == REG_DWORD)
  383. {
  384. WLog_INFO(TAG, "dword:%08" PRIX32 "", value->data.dword);
  385. }
  386. else if (value->type == REG_SZ)
  387. {
  388. WLog_INFO(TAG, "%s\"", value->data.string);
  389. }
  390. else
  391. {
  392. WLog_ERR(TAG, "unimplemented format: %s", REG_DATA_TYPE_STRINGS[value->type]);
  393. }
  394. }
  395. void reg_print_key(Reg* reg, RegKey* key)
  396. {
  397. RegVal* pValue;
  398. pValue = key->values;
  399. WLog_INFO(TAG, "[%s]", key->name);
  400. while (pValue != NULL)
  401. {
  402. reg_print_value(reg, pValue);
  403. pValue = pValue->next;
  404. }
  405. }
  406. static void reg_print(Reg* reg)
  407. {
  408. RegKey* pKey;
  409. pKey = reg->root_key->subkeys;
  410. while (pKey != NULL)
  411. {
  412. reg_print_key(reg, pKey);
  413. pKey = pKey->next;
  414. }
  415. }