1 /* hivex - Windows Registry "hive" extraction library.
2 * Copyright (C) 2009 Red Hat Inc.
3 * Derived from code by Petter Nordahl-Hagen under a compatible license:
4 * Copyright (c) 1997-2007 Petter Nordahl-Hagen.
5 * Derived from code by Markus Stephany under a compatible license:
6 * Copyright (c) 2000-2004, Markus Stephany.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation;
11 * version 2.1 of the License.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * See file LICENSE for the full license.
39 #ifdef HAVE_BYTESWAP_H
43 #define STREQ(a,b) (strcmp((a),(b)) == 0)
44 #define STRCASEEQ(a,b) (strcasecmp((a),(b)) == 0)
45 //#define STRNEQ(a,b) (strcmp((a),(b)) != 0)
46 //#define STRCASENEQ(a,b) (strcasecmp((a),(b)) != 0)
47 #define STREQLEN(a,b,n) (strncmp((a),(b),(n)) == 0)
48 //#define STRCASEEQLEN(a,b,n) (strncasecmp((a),(b),(n)) == 0)
49 //#define STRNEQLEN(a,b,n) (strncmp((a),(b),(n)) != 0)
50 //#define STRCASENEQLEN(a,b,n) (strncasecmp((a),(b),(n)) != 0)
51 //#define STRPREFIX(a,b) (strncmp((a),(b),strlen((b))) == 0)
53 #if __BYTE_ORDER == __LITTLE_ENDIAN
55 #define be32toh(x) __bswap_32 (x)
58 #define be64toh(x) __bswap_64 (x)
61 #define le16toh(x) (x)
64 #define le32toh(x) (x)
67 #define le64toh(x) (x)
71 #define be32toh(x) (x)
74 #define be64toh(x) (x)
77 #define le16toh(x) __bswap_16 (x)
80 #define le32toh(x) __bswap_32 (x)
83 #define le64toh(x) __bswap_64 (x)
89 static char *windows_utf16_to_utf8 (/* const */ char *input, size_t len);
96 /* Memory-mapped (readonly) registry file. */
99 struct ntreg_header *hdr;
102 /* Use a bitmap to store which file offsets are valid (point to a
103 * used block). We only need to store 1 bit per 32 bits of the file
104 * (because blocks are 4-byte aligned). We found that the average
105 * block size in a registry file is ~50 bytes. So roughly 1 in 12
106 * bits in the bitmap will be set, making it likely a more efficient
107 * structure than a hash table.
110 #define BITMAP_SET(bitmap,off) (bitmap[(off)>>5] |= 1 << (((off)>>2)&7))
111 #define BITMAP_CLR(bitmap,off) (bitmap[(off)>>5] &= ~ (1 << (((off)>>2)&7)))
112 #define BITMAP_TST(bitmap,off) (bitmap[(off)>>5] & (1 << (((off)>>2)&7)))
113 #define IS_VALID_BLOCK(h,off) \
114 (((off) & 3) == 0 && \
116 (off) < (h)->size && \
117 BITMAP_TST((h)->bitmap,(off)))
119 /* Fields from the header, extracted from little-endianness hell. */
120 size_t rootoffs; /* Root key offset (always an nk-block). */
121 size_t endpages; /* Offset of end of pages. */
124 size_t pages; /* Number of hbin pages read. */
125 size_t blocks; /* Total number of blocks found. */
126 size_t used_blocks; /* Total number of used blocks found. */
127 size_t used_size; /* Total size (bytes) of used blocks. */
130 /* NB. All fields are little endian. */
131 struct ntreg_header {
132 char magic[4]; /* "regf" */
135 char last_modified[8];
136 uint32_t major_ver; /* 1 */
137 uint32_t minor_ver; /* 3 */
138 uint32_t unknown5; /* 0 */
139 uint32_t unknown6; /* 1 */
140 uint32_t offset; /* offset of root key record - 4KB */
141 uint32_t blocks; /* pointer AFTER last hbin in file - 4KB */
142 uint32_t unknown7; /* 1 */
144 char name[64]; /* original file name of hive */
145 char unknown_guid1[16];
146 char unknown_guid2[16];
149 char unknown_guid3[16];
154 uint32_t csum; /* checksum: xor of dwords 0-0x1fb. */
156 char unknown11[3528];
158 char unknown_guid4[16];
159 char unknown_guid5[16];
160 char unknown_guid6[16];
164 } __attribute__((__packed__));
166 struct ntreg_hbin_page {
167 char magic[4]; /* "hbin" */
168 uint32_t offset_first; /* offset from 1st block */
169 uint32_t offset_next; /* offset of next (relative to this) */
171 /* Linked list of blocks follows here. */
172 } __attribute__((__packed__));
174 struct ntreg_hbin_block {
175 int32_t seg_len; /* length of this block (-ve for used block) */
176 char id[2]; /* the block type (eg. "nk" for nk record) */
177 /* Block data follows here. */
178 } __attribute__((__packed__));
180 #define BLOCK_ID_EQ(h,offs,eqid) \
181 (STREQLEN (((struct ntreg_hbin_block *)((h)->addr + (offs)))->id, (eqid), 2))
184 block_len (hive_h *h, size_t blkoff, int *used)
186 struct ntreg_hbin_block *block;
187 block = (struct ntreg_hbin_block *) (h->addr + blkoff);
189 int32_t len = le32toh (block->seg_len);
200 struct ntreg_nk_record {
201 int32_t seg_len; /* length (always -ve because used) */
202 char id[2]; /* "nk" */
205 uint32_t parent; /* offset of owner/parent */
206 uint32_t nr_subkeys; /* number of subkeys */
208 uint32_t subkey_lf; /* lf record containing list of subkeys */
210 uint32_t nr_values; /* number of values */
211 uint32_t vallist; /* value-list record */
212 uint32_t sk; /* offset of sk-record */
213 uint32_t classname; /* offset of classname record */
216 uint16_t name_len; /* length of name */
217 uint16_t classname_len; /* length of classname */
218 char name[1]; /* name follows here */
219 } __attribute__((__packed__));
221 struct ntreg_lf_record {
223 char id[2]; /* "lf" */
224 uint16_t nr_keys; /* number of keys in this record */
226 uint32_t offset; /* offset of nk-record for this subkey */
227 char name[4]; /* first 4 characters of subkey name */
229 } __attribute__((__packed__));
231 struct ntreg_ri_record {
233 char id[2]; /* "ri" */
234 uint16_t nr_offsets; /* number of pointers to lh records */
235 uint32_t offset[1]; /* list of pointers to lh records */
236 } __attribute__((__packed__));
238 /* This has no ID header. */
239 struct ntreg_value_list {
241 uint32_t offset[1]; /* list of pointers to vk records */
242 } __attribute__((__packed__));
244 struct ntreg_vk_record {
245 int32_t seg_len; /* length (always -ve because used) */
246 char id[2]; /* "vk" */
247 uint16_t name_len; /* length of name */
248 /* length of the data:
249 * If data_len is <= 4, then it's stored inline.
250 * If data_len is 0x80000000, then it's an inline dword.
251 * Top bit may be set or not set at random.
254 uint32_t data_offset; /* pointer to the data (or data if inline) */
255 hive_type data_type; /* type of the data */
256 uint16_t flags; /* bit 0 set => key name ASCII,
257 bit 0 clr => key name UTF-16.
258 Only seen ASCII here in the wild. */
260 char name[1]; /* key name follows here */
261 } __attribute__((__packed__));
264 hivex_open (const char *filename, int flags)
268 assert (sizeof (struct ntreg_header) == 0x1000);
269 assert (offsetof (struct ntreg_header, csum) == 0x1fc);
271 h = calloc (1, sizeof *h);
275 h->msglvl = flags & HIVEX_OPEN_MSGLVL_MASK;
277 const char *debug = getenv ("HIVEX_DEBUG");
278 if (debug && STREQ (debug, "1"))
282 fprintf (stderr, "hivex_open: created handle %p\n", h);
284 h->fd = open (filename, O_RDONLY);
289 if (fstat (h->fd, &statbuf) == -1)
292 h->size = statbuf.st_size;
294 h->addr = mmap (NULL, h->size, PROT_READ, MAP_SHARED, h->fd, 0);
295 if (h->addr == MAP_FAILED)
299 fprintf (stderr, "hivex_open: mapped file at %p\n", h->addr);
302 if (h->hdr->magic[0] != 'r' ||
303 h->hdr->magic[1] != 'e' ||
304 h->hdr->magic[2] != 'g' ||
305 h->hdr->magic[3] != 'f') {
306 fprintf (stderr, "hivex: %s: not a Windows NT Registry hive file\n",
312 /* Check major version. */
313 uint32_t major_ver = le32toh (h->hdr->major_ver);
314 if (major_ver != 1) {
316 "hivex: %s: hive file major version %" PRIu32 " (expected 1)\n",
317 filename, major_ver);
322 h->bitmap = calloc (1 + h->size / 32, 1);
323 if (h->bitmap == NULL)
326 /* Header checksum. */
327 uint32_t *daddr = (uint32_t *) h->addr;
330 for (i = 0; i < 0x1fc / 4; ++i) {
331 sum ^= le32toh (*daddr);
335 if (sum != le32toh (h->hdr->csum)) {
336 fprintf (stderr, "hivex: %s: bad checksum in hive header\n", filename);
341 if (h->msglvl >= 2) {
342 char *name = windows_utf16_to_utf8 (h->hdr->name, 64);
345 "hivex_open: header fields:\n"
346 " file version %" PRIu32 ".%" PRIu32 "\n"
347 " sequence nos %" PRIu32 " %" PRIu32 "\n"
348 " (sequences nos should match if hive was synched at shutdown)\n"
349 " original file name %s\n"
350 " (only 32 chars are stored, name is probably truncated)\n"
351 " root offset 0x%x + 0x1000\n"
352 " end of last page 0x%x + 0x1000 (total file size 0x%zx)\n"
353 " checksum 0x%x (calculated 0x%x)\n",
354 major_ver, le32toh (h->hdr->minor_ver),
355 le32toh (h->hdr->sequence1), le32toh (h->hdr->sequence2),
356 name ? name : "(conversion failed)",
357 le32toh (h->hdr->offset),
358 le32toh (h->hdr->blocks), h->size,
359 le32toh (h->hdr->csum), sum);
363 h->rootoffs = le32toh (h->hdr->offset) + 0x1000;
364 h->endpages = le32toh (h->hdr->blocks) + 0x1000;
367 fprintf (stderr, "hivex_open: root offset = 0x%zx\n", h->rootoffs);
369 /* We'll set this flag when we see a block with the root offset (ie.
372 int seen_root_block = 0, bad_root_block = 0;
374 /* Read the pages and blocks. The aim here is to be robust against
375 * corrupt or malicious registries. So we make sure the loops
376 * always make forward progress. We add the address of each block
377 * we read to a hash table so pointers will only reference the start
381 struct ntreg_hbin_page *page;
382 for (off = 0x1000; off < h->size; off += le32toh (page->offset_next)) {
383 if (off >= h->endpages)
386 page = (struct ntreg_hbin_page *) (h->addr + off);
387 if (page->magic[0] != 'h' ||
388 page->magic[1] != 'b' ||
389 page->magic[2] != 'i' ||
390 page->magic[3] != 'n') {
391 fprintf (stderr, "hivex: %s: trailing garbage at end of file (at 0x%zx, after %zu pages)\n",
392 filename, off, h->pages);
398 fprintf (stderr, "hivex_open: page at 0x%zx\n", off);
401 if (le32toh (page->offset_next) <= sizeof (struct ntreg_hbin_page) ||
402 (le32toh (page->offset_next) & 3) != 0) {
403 fprintf (stderr, "hivex: %s: pagesize %d at %zu, bad registry\n",
404 filename, le32toh (page->offset_next), off);
409 /* Read the blocks in this page. */
411 struct ntreg_hbin_block *block;
413 for (blkoff = off + 0x20;
414 blkoff < off + le32toh (page->offset_next);
418 int is_root = blkoff == h->rootoffs;
422 block = (struct ntreg_hbin_block *) (h->addr + blkoff);
424 seg_len = block_len (h, blkoff, &used);
425 if (seg_len <= 4 || (seg_len & 3) != 0) {
426 fprintf (stderr, "hivex: %s: block size %d at %zu, bad registry\n",
427 filename, le32toh (block->seg_len), blkoff);
433 fprintf (stderr, "hivex_open: %s block id %d,%d at 0x%zx%s\n",
434 used ? "used" : "free", block->id[0], block->id[1], blkoff,
435 is_root ? " (root)" : "");
437 if (is_root && !used)
442 h->used_size += seg_len;
444 /* Root block must be an nk-block. */
445 if (is_root && (block->id[0] != 'n' || block->id[1] != 'k'))
448 /* Note this blkoff is a valid address. */
449 BITMAP_SET (h->bitmap, blkoff);
454 if (!seen_root_block) {
455 fprintf (stderr, "hivex: %s: no root block found\n", filename);
460 if (bad_root_block) {
461 fprintf (stderr, "hivex: %s: bad root block (free or not nk)\n", filename);
468 "hivex_open: successfully read Windows Registry hive file:\n"
471 " blocks used: %zu\n"
472 " bytes used: %zu\n",
473 h->pages, h->blocks, h->used_blocks, h->used_size);
481 if (h->addr && h->size && h->addr != MAP_FAILED)
482 munmap (h->addr, h->size);
492 hivex_close (hive_h *h)
497 munmap (h->addr, h->size);
505 hivex_root (hive_h *h)
507 hive_node_h ret = h->rootoffs;
508 if (!IS_VALID_BLOCK (h, ret)) {
516 hivex_node_name (hive_h *h, hive_node_h node)
518 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
523 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
525 /* AFAIK the node name is always plain ASCII, so no conversion
526 * to UTF-8 is necessary. However we do need to nul-terminate
530 /* nk->name_len is unsigned, 16 bit, so this is safe ... However
531 * we have to make sure the length doesn't exceed the block length.
533 size_t len = le16toh (nk->name_len);
534 size_t seg_len = block_len (h, node, NULL);
535 if (sizeof (struct ntreg_nk_record) + len - 1 > seg_len) {
537 fprintf (stderr, "hivex_node_name: returning EFAULT because node name is too long (%zu, %zu)\n",
543 char *ret = malloc (len + 1);
546 memcpy (ret, nk->name, len);
552 /* I think the documentation for the sk and classname fields in the nk
553 * record is wrong, or else the offset field is in the wrong place.
554 * Otherwise this makes no sense. Disabled this for now -- it's not
555 * useful for reading the registry anyway.
559 hivex_node_security (hive_h *h, hive_node_h node)
561 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
566 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
568 hive_node_h ret = le32toh (nk->sk);
570 if (!IS_VALID_BLOCK (h, ret)) {
578 hivex_node_classname (hive_h *h, hive_node_h node)
580 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
585 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
587 hive_node_h ret = le32toh (nk->classname);
589 if (!IS_VALID_BLOCK (h, ret)) {
598 hivex_node_children (hive_h *h, hive_node_h node)
600 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
605 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
607 size_t nr_subkeys_in_nk = le32toh (nk->nr_subkeys);
609 /* Deal with the common "no subkeys" case quickly. */
611 if (nr_subkeys_in_nk == 0) {
612 ret = malloc (sizeof (hive_node_h));
619 /* Arbitrarily limit the number of subkeys we will ever deal with. */
620 if (nr_subkeys_in_nk > 1000000) {
625 /* The subkey_lf field can point either to an lf-record, which is
626 * the common case, or if there are lots of subkeys, to an
629 size_t subkey_lf = le32toh (nk->subkey_lf);
631 if (!IS_VALID_BLOCK (h, subkey_lf)) {
633 fprintf (stderr, "hivex_node_children: returning EFAULT because subkey_lf is not a valid block (%zu)\n",
639 struct ntreg_hbin_block *block =
640 (struct ntreg_hbin_block *) (h->addr + subkey_lf);
642 /* Points to lf-record? (Note, also "lh" but that is basically the
643 * same as "lf" as far as we are concerned here).
645 if (block->id[0] == 'l' && (block->id[1] == 'f' || block->id[1] == 'h')) {
646 struct ntreg_lf_record *lf = (struct ntreg_lf_record *) block;
648 /* Check number of subkeys in the nk-record matches number of subkeys
651 size_t nr_subkeys_in_lf = le16toh (lf->nr_keys);
654 fprintf (stderr, "hivex_node_children: nr_subkeys_in_nk = %zu, nr_subkeys_in_lf = %zu\n",
655 nr_subkeys_in_nk, nr_subkeys_in_lf);
657 if (nr_subkeys_in_nk != nr_subkeys_in_lf) {
662 size_t len = block_len (h, subkey_lf, NULL);
663 if (8 + nr_subkeys_in_lf * 8 > len) {
665 fprintf (stderr, "hivex_node_children: returning EFAULT because too many subkeys (%zu, %zu)\n",
666 nr_subkeys_in_lf, len);
671 /* Allocate space for the returned values. Note that
672 * nr_subkeys_in_lf is limited to a 16 bit value.
674 ret = malloc ((1 + nr_subkeys_in_lf) * sizeof (hive_node_h));
679 for (i = 0; i < nr_subkeys_in_lf; ++i) {
680 hive_node_h subkey = lf->keys[i].offset;
682 if (!IS_VALID_BLOCK (h, subkey)) {
684 fprintf (stderr, "hivex_node_children: returning EFAULT because subkey is not a valid block (0x%zx)\n",
695 /* Points to ri-record? */
696 else if (block->id[0] == 'r' && block->id[1] == 'i') {
697 struct ntreg_ri_record *ri = (struct ntreg_ri_record *) block;
699 size_t nr_offsets = le16toh (ri->nr_offsets);
701 /* Count total number of children. */
703 for (i = 0; i < nr_offsets; ++i) {
704 hive_node_h offset = ri->offset[i];
706 if (!IS_VALID_BLOCK (h, offset)) {
708 fprintf (stderr, "hivex_node_children: returning EFAULT because ri-offset is not a valid block (0x%zx)\n",
713 if (!BLOCK_ID_EQ (h, offset, "lf") && !BLOCK_ID_EQ (h, offset, "lh")) {
718 struct ntreg_lf_record *lf =
719 (struct ntreg_lf_record *) (h->addr + offset);
721 count += le16toh (lf->nr_keys);
725 fprintf (stderr, "hivex_node_children: nr_subkeys_in_nk = %zu, counted = %zu\n",
726 nr_subkeys_in_nk, count);
728 if (nr_subkeys_in_nk != count) {
733 /* Copy list of children. Note nr_subkeys_in_nk is limited to
734 * something reasonable above.
736 ret = malloc ((1 + nr_subkeys_in_nk) * sizeof (hive_node_h));
741 for (i = 0; i < nr_offsets; ++i) {
742 hive_node_h offset = ri->offset[i];
744 if (!IS_VALID_BLOCK (h, offset)) {
746 fprintf (stderr, "hivex_node_children: returning EFAULT because ri-offset is not a valid block (0x%zx)\n",
751 if (!BLOCK_ID_EQ (h, offset, "lf") && !BLOCK_ID_EQ (h, offset, "lh")) {
756 struct ntreg_lf_record *lf =
757 (struct ntreg_lf_record *) (h->addr + offset);
760 for (j = 0; j < le16toh (lf->nr_keys); ++j) {
761 hive_node_h subkey = lf->keys[j].offset;
763 if (!IS_VALID_BLOCK (h, subkey)) {
765 fprintf (stderr, "hivex_node_children: returning EFAULT because indirect subkey is not a valid block (0x%zx)\n",
771 ret[count++] = subkey;
784 /* Very inefficient, but at least having a separate API call
785 * allows us to make it more efficient in future.
788 hivex_node_get_child (hive_h *h, hive_node_h node, const char *nname)
790 hive_node_h *children = NULL;
794 children = hivex_node_children (h, node);
795 if (!children) goto error;
798 for (i = 0; children[i] != 0; ++i) {
799 name = hivex_node_name (h, children[i]);
800 if (!name) goto error;
801 if (STRCASEEQ (name, nname)) {
805 free (name); name = NULL;
815 hivex_node_parent (hive_h *h, hive_node_h node)
817 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
822 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
824 hive_node_h ret = le32toh (nk->parent);
826 if (!IS_VALID_BLOCK (h, ret)) {
828 fprintf (stderr, "hivex_node_parent: returning EFAULT because parent is not a valid block (0x%zx)\n",
837 hivex_node_values (hive_h *h, hive_node_h node)
839 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
844 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
846 size_t nr_values = le32toh (nk->nr_values);
849 fprintf (stderr, "hivex_node_values: nr_values = %zu\n", nr_values);
851 /* Deal with the common "no values" case quickly. */
853 if (nr_values == 0) {
854 ret = malloc (sizeof (hive_node_h));
861 /* Arbitrarily limit the number of values we will ever deal with. */
862 if (nr_values > 100000) {
867 /* Get the value list and check it looks reasonable. */
868 size_t vlist_offset = le32toh (nk->vallist);
869 vlist_offset += 0x1000;
870 if (!IS_VALID_BLOCK (h, vlist_offset)) {
872 fprintf (stderr, "hivex_node_values: returning EFAULT because value list is not a valid block (0x%zx)\n",
878 struct ntreg_value_list *vlist =
879 (struct ntreg_value_list *) (h->addr + vlist_offset);
881 size_t len = block_len (h, vlist_offset, NULL);
882 if (4 + nr_values * 4 > len) {
884 fprintf (stderr, "hivex_node_values: returning EFAULT because value list is too long (%zu, %zu)\n",
890 /* Allocate return array and copy values in. */
891 ret = malloc ((1 + nr_values) * sizeof (hive_node_h));
896 for (i = 0; i < nr_values; ++i) {
897 hive_node_h value = vlist->offset[i];
899 if (!IS_VALID_BLOCK (h, value)) {
901 fprintf (stderr, "hivex_node_values: returning EFAULT because value is not a valid block (0x%zx)\n",
914 /* Very inefficient, but at least having a separate API call
915 * allows us to make it more efficient in future.
918 hivex_node_get_value (hive_h *h, hive_node_h node, const char *key)
920 hive_value_h *values = NULL;
922 hive_value_h ret = 0;
924 values = hivex_node_values (h, node);
925 if (!values) goto error;
928 for (i = 0; values[i] != 0; ++i) {
929 name = hivex_value_key (h, values[i]);
930 if (!name) goto error;
931 if (STRCASEEQ (name, key)) {
935 free (name); name = NULL;
945 hivex_value_key (hive_h *h, hive_value_h value)
947 if (!IS_VALID_BLOCK (h, value) || !BLOCK_ID_EQ (h, value, "vk")) {
952 struct ntreg_vk_record *vk = (struct ntreg_vk_record *) (h->addr + value);
954 /* AFAIK the key is always plain ASCII, so no conversion to UTF-8 is
955 * necessary. However we do need to nul-terminate the string.
958 /* vk->name_len is unsigned, 16 bit, so this is safe ... However
959 * we have to make sure the length doesn't exceed the block length.
961 size_t len = le16toh (vk->name_len);
962 size_t seg_len = block_len (h, value, NULL);
963 if (sizeof (struct ntreg_vk_record) + len - 1 > seg_len) {
965 fprintf (stderr, "hivex_value_key: returning EFAULT because key length is too long (%zu, %zu)\n",
971 char *ret = malloc (len + 1);
974 memcpy (ret, vk->name, len);
980 hivex_value_type (hive_h *h, hive_value_h value, hive_type *t, size_t *len)
982 if (!IS_VALID_BLOCK (h, value) || !BLOCK_ID_EQ (h, value, "vk")) {
987 struct ntreg_vk_record *vk = (struct ntreg_vk_record *) (h->addr + value);
990 *t = le32toh (vk->data_type);
993 *len = le32toh (vk->data_len);
994 if (*len == 0x80000000) { /* special case */
996 if (t) *t = hive_t_dword;
1005 hivex_value_value (hive_h *h, hive_value_h value,
1006 hive_type *t_rtn, size_t *len_rtn)
1008 if (!IS_VALID_BLOCK (h, value) || !BLOCK_ID_EQ (h, value, "vk")) {
1013 struct ntreg_vk_record *vk = (struct ntreg_vk_record *) (h->addr + value);
1018 t = le32toh (vk->data_type);
1020 len = le32toh (vk->data_len);
1021 if (len == 0x80000000) { /* special case */
1028 fprintf (stderr, "hivex_value_value: value=0x%zx, t=%d, len=%zu\n",
1036 /* Arbitrarily limit the length that we will read. */
1037 if (len > 1000000) {
1042 char *ret = malloc (len);
1046 /* If length is <= 4 it's always stored inline. */
1048 memcpy (ret, (char *) &vk->data_offset, len);
1052 size_t data_offset = vk->data_offset;
1053 data_offset += 0x1000;
1054 if (!IS_VALID_BLOCK (h, data_offset)) {
1056 fprintf (stderr, "hivex_value_value: returning EFAULT because data offset is not a valid block (0x%zx)\n",
1063 /* Check that the declared size isn't larger than the block its in. */
1064 size_t blen = block_len (h, data_offset, NULL);
1067 fprintf (stderr, "hivex_value_value: returning EFAULT because data is longer than its block (data 0x%zx, data len %zu, block len %zu)\n",
1068 data_offset, len, blen);
1074 char *data = h->addr + data_offset + 4;
1075 memcpy (ret, data, len);
1080 windows_utf16_to_utf8 (/* const */ char *input, size_t len)
1082 iconv_t ic = iconv_open ("UTF-8", "UTF-16");
1083 if (ic == (iconv_t) -1)
1086 /* iconv(3) has an insane interface ... */
1088 /* Mostly UTF-8 will be smaller, so this is a good initial guess. */
1089 size_t outalloc = len;
1093 size_t outlen = outalloc;
1094 char *out = malloc (outlen + 1);
1104 size_t r = iconv (ic, &inp, &inlen, &outp, &outlen);
1105 if (r == (size_t) -1) {
1106 if (errno == E2BIG) {
1107 size_t prev = outalloc;
1108 /* Try again with a larger output buffer. */
1111 if (outalloc < prev)
1116 /* Else some conversion failure, eg. EILSEQ, EINVAL. */
1132 hivex_value_string (hive_h *h, hive_value_h value)
1136 char *data = hivex_value_value (h, value, &t, &len);
1141 if (t != hive_t_string && t != hive_t_expand_string && t != hive_t_link) {
1147 char *ret = windows_utf16_to_utf8 (data, len);
1156 free_strings (char **argv)
1161 for (i = 0; argv[i] != NULL; ++i)
1167 /* Get the length of a UTF-16 format string. Handle the string as
1168 * pairs of bytes, looking for the first \0\0 pair.
1171 utf16_string_len_in_bytes (const char *str)
1175 while (str[0] || str[1]) {
1183 /* http://blogs.msdn.com/oldnewthing/archive/2009/10/08/9904646.aspx */
1185 hivex_value_multiple_strings (hive_h *h, hive_value_h value)
1189 char *data = hivex_value_value (h, value, &t, &len);
1194 if (t != hive_t_multiple_strings) {
1200 size_t nr_strings = 0;
1201 char **ret = malloc ((1 + nr_strings) * sizeof (char *));
1211 while (p < data + len && (plen = utf16_string_len_in_bytes (p)) > 0) {
1213 char **ret2 = realloc (ret, (1 + nr_strings) * sizeof (char *));
1221 ret[nr_strings-1] = windows_utf16_to_utf8 (p, plen);
1222 ret[nr_strings] = NULL;
1223 if (ret[nr_strings-1] == NULL) {
1229 p += plen + 2 /* skip over UTF-16 \0\0 at the end of this string */;
1237 hivex_value_dword (hive_h *h, hive_value_h value)
1241 char *data = hivex_value_value (h, value, &t, &len);
1246 if ((t != hive_t_dword && t != hive_t_dword_be) || len != 4) {
1252 int32_t ret = *(int32_t*)data;
1254 if (t == hive_t_dword) /* little endian */
1255 ret = le32toh (ret);
1257 ret = be32toh (ret);
1263 hivex_value_qword (hive_h *h, hive_value_h value)
1267 char *data = hivex_value_value (h, value, &t, &len);
1272 if (t != hive_t_qword || len != 8) {
1278 int64_t ret = *(int64_t*)data;
1280 ret = le64toh (ret); /* always little endian */
1286 hivex_visit (hive_h *h, const struct hivex_visitor *visitor, size_t len,
1287 void *opaque, int flags)
1289 return hivex_visit_node (h, hivex_root (h), visitor, len, opaque, flags);
1292 static int hivex__visit_node (hive_h *h, hive_node_h node, const struct hivex_visitor *vtor, char *unvisited, void *opaque, int flags);
1295 hivex_visit_node (hive_h *h, hive_node_h node,
1296 const struct hivex_visitor *visitor, size_t len, void *opaque,
1299 struct hivex_visitor vtor;
1300 memset (&vtor, 0, sizeof vtor);
1302 /* Note that len might be larger *or smaller* than the expected size. */
1303 size_t copysize = len <= sizeof vtor ? len : sizeof vtor;
1304 memcpy (&vtor, visitor, copysize);
1306 /* This bitmap records unvisited nodes, so we don't loop if the
1307 * registry contains cycles.
1309 char *unvisited = malloc (1 + h->size / 32);
1310 if (unvisited == NULL)
1312 memcpy (unvisited, h->bitmap, 1 + h->size / 32);
1314 int r = hivex__visit_node (h, node, &vtor, unvisited, opaque, flags);
1320 hivex__visit_node (hive_h *h, hive_node_h node,
1321 const struct hivex_visitor *vtor, char *unvisited,
1322 void *opaque, int flags)
1324 int skip_bad = flags & HIVEX_VISIT_SKIP_BAD;
1326 hive_value_h *values = NULL;
1327 hive_node_h *children = NULL;
1333 /* Return -1 on all callback errors. However on internal errors,
1334 * check if skip_bad is set and suppress those errors if so.
1338 if (!BITMAP_TST (unvisited, node)) {
1340 fprintf (stderr, "hivex__visit_node: contains cycle: visited node 0x%zx already\n",
1344 return skip_bad ? 0 : -1;
1346 BITMAP_CLR (unvisited, node);
1348 name = hivex_node_name (h, node);
1349 if (!name) return skip_bad ? 0 : -1;
1350 if (vtor->node_start && vtor->node_start (h, opaque, node, name) == -1)
1353 values = hivex_node_values (h, node);
1355 ret = skip_bad ? 0 : -1;
1359 for (i = 0; values[i] != 0; ++i) {
1363 if (hivex_value_type (h, values[i], &t, &len) == -1) {
1364 ret = skip_bad ? 0 : -1;
1368 key = hivex_value_key (h, values[i]);
1370 ret = skip_bad ? 0 : -1;
1376 str = hivex_value_value (h, values[i], &t, &len);
1378 ret = skip_bad ? 0 : -1;
1381 if (t != hive_t_none) {
1382 ret = skip_bad ? 0 : -1;
1385 if (vtor->value_none &&
1386 vtor->value_none (h, opaque, node, values[i], t, len, key, str) == -1)
1388 free (str); str = NULL;
1392 case hive_t_expand_string:
1394 str = hivex_value_string (h, values[i]);
1396 if (errno != EILSEQ && errno != EINVAL) {
1397 ret = skip_bad ? 0 : -1;
1400 if (vtor->value_string_invalid_utf16) {
1401 str = hivex_value_value (h, values[i], &t, &len);
1402 if (vtor->value_string_invalid_utf16 (h, opaque, node, values[i], t, len, key, str) == -1)
1404 free (str); str = NULL;
1408 if (vtor->value_string &&
1409 vtor->value_string (h, opaque, node, values[i], t, len, key, str) == -1)
1411 free (str); str = NULL;
1415 case hive_t_dword_be: {
1416 int32_t i32 = hivex_value_dword (h, values[i]);
1417 if (vtor->value_dword &&
1418 vtor->value_dword (h, opaque, node, values[i], t, len, key, i32) == -1)
1423 case hive_t_qword: {
1424 int64_t i64 = hivex_value_qword (h, values[i]);
1425 if (vtor->value_qword &&
1426 vtor->value_qword (h, opaque, node, values[i], t, len, key, i64) == -1)
1432 str = hivex_value_value (h, values[i], &t, &len);
1434 ret = skip_bad ? 0 : -1;
1437 if (t != hive_t_binary) {
1438 ret = skip_bad ? 0 : -1;
1441 if (vtor->value_binary &&
1442 vtor->value_binary (h, opaque, node, values[i], t, len, key, str) == -1)
1444 free (str); str = NULL;
1447 case hive_t_multiple_strings:
1448 strs = hivex_value_multiple_strings (h, values[i]);
1450 if (errno != EILSEQ && errno != EINVAL) {
1451 ret = skip_bad ? 0 : -1;
1454 if (vtor->value_string_invalid_utf16) {
1455 str = hivex_value_value (h, values[i], &t, &len);
1456 if (vtor->value_string_invalid_utf16 (h, opaque, node, values[i], t, len, key, str) == -1)
1458 free (str); str = NULL;
1462 if (vtor->value_multiple_strings &&
1463 vtor->value_multiple_strings (h, opaque, node, values[i], t, len, key, strs) == -1)
1465 free_strings (strs); strs = NULL;
1468 case hive_t_resource_list:
1469 case hive_t_full_resource_description:
1470 case hive_t_resource_requirements_list:
1472 str = hivex_value_value (h, values[i], &t, &len);
1474 ret = skip_bad ? 0 : -1;
1477 if (vtor->value_other &&
1478 vtor->value_other (h, opaque, node, values[i], t, len, key, str) == -1)
1480 free (str); str = NULL;
1484 free (key); key = NULL;
1487 children = hivex_node_children (h, node);
1488 if (children == NULL) {
1489 ret = skip_bad ? 0 : -1;
1493 for (i = 0; children[i] != 0; ++i) {
1495 fprintf (stderr, "hivex__visit_node: %s: visiting subkey %d (0x%zx)\n",
1496 name, i, children[i]);
1498 if (hivex__visit_node (h, children[i], vtor, unvisited, opaque, flags) == -1)
1502 if (vtor->node_end && vtor->node_end (h, opaque, node, name) == -1)
1513 free_strings (strs);