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);
97 /* Memory-mapped (readonly) registry file. */
100 struct ntreg_header *hdr;
103 /* Use a bitmap to store which file offsets are valid (point to a
104 * used block). We only need to store 1 bit per 32 bits of the file
105 * (because blocks are 4-byte aligned). We found that the average
106 * block size in a registry file is ~50 bytes. So roughly 1 in 12
107 * bits in the bitmap will be set, making it likely a more efficient
108 * structure than a hash table.
111 #define BITMAP_SET(bitmap,off) (bitmap[(off)>>5] |= 1 << (((off)>>2)&7))
112 #define BITMAP_CLR(bitmap,off) (bitmap[(off)>>5] &= ~ (1 << (((off)>>2)&7)))
113 #define BITMAP_TST(bitmap,off) (bitmap[(off)>>5] & (1 << (((off)>>2)&7)))
114 #define IS_VALID_BLOCK(h,off) \
115 (((off) & 3) == 0 && \
117 (off) < (h)->size && \
118 BITMAP_TST((h)->bitmap,(off)))
120 /* Fields from the header, extracted from little-endianness hell. */
121 size_t rootoffs; /* Root key offset (always an nk-block). */
122 size_t endpages; /* Offset of end of pages. */
125 /* NB. All fields are little endian. */
126 struct ntreg_header {
127 char magic[4]; /* "regf" */
130 char last_modified[8];
131 uint32_t major_ver; /* 1 */
132 uint32_t minor_ver; /* 3 */
133 uint32_t unknown5; /* 0 */
134 uint32_t unknown6; /* 1 */
135 uint32_t offset; /* offset of root key record - 4KB */
136 uint32_t blocks; /* pointer AFTER last hbin in file - 4KB */
137 uint32_t unknown7; /* 1 */
139 char name[64]; /* original file name of hive */
140 char unknown_guid1[16];
141 char unknown_guid2[16];
144 char unknown_guid3[16];
149 uint32_t csum; /* checksum: xor of dwords 0-0x1fb. */
151 char unknown11[3528];
153 char unknown_guid4[16];
154 char unknown_guid5[16];
155 char unknown_guid6[16];
159 } __attribute__((__packed__));
161 struct ntreg_hbin_page {
162 char magic[4]; /* "hbin" */
163 uint32_t offset_first; /* offset from 1st block */
164 uint32_t page_size; /* size of this page (multiple of 4KB) */
166 /* Linked list of blocks follows here. */
167 } __attribute__((__packed__));
169 struct ntreg_hbin_block {
170 int32_t seg_len; /* length of this block (-ve for used block) */
171 char id[2]; /* the block type (eg. "nk" for nk record) */
172 /* Block data follows here. */
173 } __attribute__((__packed__));
175 #define BLOCK_ID_EQ(h,offs,eqid) \
176 (STREQLEN (((struct ntreg_hbin_block *)((h)->addr + (offs)))->id, (eqid), 2))
179 block_len (hive_h *h, size_t blkoff, int *used)
181 struct ntreg_hbin_block *block;
182 block = (struct ntreg_hbin_block *) (h->addr + blkoff);
184 int32_t len = le32toh (block->seg_len);
195 struct ntreg_nk_record {
196 int32_t seg_len; /* length (always -ve because used) */
197 char id[2]; /* "nk" */
201 uint32_t parent; /* offset of owner/parent */
202 uint32_t nr_subkeys; /* number of subkeys */
204 uint32_t subkey_lf; /* lf record containing list of subkeys */
206 uint32_t nr_values; /* number of values */
207 uint32_t vallist; /* value-list record */
208 uint32_t sk; /* offset of sk-record */
209 uint32_t classname; /* offset of classname record */
212 uint16_t name_len; /* length of name */
213 uint16_t classname_len; /* length of classname */
214 char name[1]; /* name follows here */
215 } __attribute__((__packed__));
217 struct ntreg_lf_record {
219 char id[2]; /* "lf" */
220 uint16_t nr_keys; /* number of keys in this record */
222 uint32_t offset; /* offset of nk-record for this subkey */
223 char hash[4]; /* hash of subkey name */
225 } __attribute__((__packed__));
227 struct ntreg_ri_record {
229 char id[2]; /* "ri" */
230 uint16_t nr_offsets; /* number of pointers to lh records */
231 uint32_t offset[1]; /* list of pointers to lh records */
232 } __attribute__((__packed__));
234 /* This has no ID header. */
235 struct ntreg_value_list {
237 uint32_t offset[1]; /* list of pointers to vk records */
238 } __attribute__((__packed__));
240 struct ntreg_vk_record {
241 int32_t seg_len; /* length (always -ve because used) */
242 char id[2]; /* "vk" */
243 uint16_t name_len; /* length of name */
244 /* length of the data:
245 * If data_len is <= 4, then it's stored inline.
246 * If data_len is 0x80000000, then it's an inline dword.
247 * Top bit may be set or not set at random.
250 uint32_t data_offset; /* pointer to the data (or data if inline) */
251 uint32_t data_type; /* type of the data */
252 uint16_t flags; /* bit 0 set => key name ASCII,
253 bit 0 clr => key name UTF-16.
254 Only seen ASCII here in the wild. */
256 char name[1]; /* key name follows here */
257 } __attribute__((__packed__));
260 header_checksum (hive_h *h)
262 uint32_t *daddr = (uint32_t *) h->addr;
266 for (i = 0; i < 0x1fc / 4; ++i) {
267 sum ^= le32toh (*daddr);
275 hivex_open (const char *filename, int flags)
279 assert (sizeof (struct ntreg_header) == 0x1000);
280 assert (offsetof (struct ntreg_header, csum) == 0x1fc);
282 h = calloc (1, sizeof *h);
286 h->msglvl = flags & HIVEX_OPEN_MSGLVL_MASK;
288 const char *debug = getenv ("HIVEX_DEBUG");
289 if (debug && STREQ (debug, "1"))
293 fprintf (stderr, "hivex_open: created handle %p\n", h);
295 h->filename = strdup (filename);
296 if (h->filename == NULL)
299 h->fd = open (filename, O_RDONLY);
304 if (fstat (h->fd, &statbuf) == -1)
307 h->size = statbuf.st_size;
309 h->addr = mmap (NULL, h->size, PROT_READ, MAP_SHARED, h->fd, 0);
310 if (h->addr == MAP_FAILED)
314 fprintf (stderr, "hivex_open: mapped file at %p\n", h->addr);
317 if (h->hdr->magic[0] != 'r' ||
318 h->hdr->magic[1] != 'e' ||
319 h->hdr->magic[2] != 'g' ||
320 h->hdr->magic[3] != 'f') {
321 fprintf (stderr, "hivex: %s: not a Windows NT Registry hive file\n",
327 /* Check major version. */
328 uint32_t major_ver = le32toh (h->hdr->major_ver);
329 if (major_ver != 1) {
331 "hivex: %s: hive file major version %" PRIu32 " (expected 1)\n",
332 filename, major_ver);
337 h->bitmap = calloc (1 + h->size / 32, 1);
338 if (h->bitmap == NULL)
341 /* Header checksum. */
342 uint32_t sum = header_checksum (h);
343 if (sum != le32toh (h->hdr->csum)) {
344 fprintf (stderr, "hivex: %s: bad checksum in hive header\n", filename);
349 if (h->msglvl >= 2) {
350 char *name = windows_utf16_to_utf8 (h->hdr->name, 64);
353 "hivex_open: header fields:\n"
354 " file version %" PRIu32 ".%" PRIu32 "\n"
355 " sequence nos %" PRIu32 " %" PRIu32 "\n"
356 " (sequences nos should match if hive was synched at shutdown)\n"
357 " original file name %s\n"
358 " (only 32 chars are stored, name is probably truncated)\n"
359 " root offset 0x%x + 0x1000\n"
360 " end of last page 0x%x + 0x1000 (total file size 0x%zx)\n"
361 " checksum 0x%x (calculated 0x%x)\n",
362 major_ver, le32toh (h->hdr->minor_ver),
363 le32toh (h->hdr->sequence1), le32toh (h->hdr->sequence2),
364 name ? name : "(conversion failed)",
365 le32toh (h->hdr->offset),
366 le32toh (h->hdr->blocks), h->size,
367 le32toh (h->hdr->csum), sum);
371 h->rootoffs = le32toh (h->hdr->offset) + 0x1000;
372 h->endpages = le32toh (h->hdr->blocks) + 0x1000;
375 fprintf (stderr, "hivex_open: root offset = 0x%zx\n", h->rootoffs);
377 /* We'll set this flag when we see a block with the root offset (ie.
380 int seen_root_block = 0, bad_root_block = 0;
382 /* Collect some stats. */
383 size_t pages = 0; /* Number of hbin pages read. */
384 size_t smallest_page = SIZE_MAX, largest_page = 0;
385 size_t blocks = 0; /* Total number of blocks found. */
386 size_t smallest_block = SIZE_MAX, largest_block = 0, blocks_bytes = 0;
387 size_t used_blocks = 0; /* Total number of used blocks found. */
388 size_t used_size = 0; /* Total size (bytes) of used blocks. */
390 /* Read the pages and blocks. The aim here is to be robust against
391 * corrupt or malicious registries. So we make sure the loops
392 * always make forward progress. We add the address of each block
393 * we read to a hash table so pointers will only reference the start
397 struct ntreg_hbin_page *page;
398 for (off = 0x1000; off < h->size; off += le32toh (page->page_size)) {
399 if (off >= h->endpages)
402 page = (struct ntreg_hbin_page *) (h->addr + off);
403 if (page->magic[0] != 'h' ||
404 page->magic[1] != 'b' ||
405 page->magic[2] != 'i' ||
406 page->magic[3] != 'n') {
407 fprintf (stderr, "hivex: %s: trailing garbage at end of file (at 0x%zx, after %zu pages)\n",
408 filename, off, pages);
413 size_t page_size = le32toh (page->page_size);
415 fprintf (stderr, "hivex_open: page at 0x%zx, size %zu\n", off, page_size);
417 if (page_size < smallest_page) smallest_page = page_size;
418 if (page_size > largest_page) largest_page = page_size;
420 if (page_size <= sizeof (struct ntreg_hbin_page) ||
421 (page_size & 0x0fff) != 0) {
422 fprintf (stderr, "hivex: %s: page size %zu at 0x%zx, bad registry\n",
423 filename, page_size, off);
428 /* Read the blocks in this page. */
430 struct ntreg_hbin_block *block;
432 for (blkoff = off + 0x20;
433 blkoff < off + page_size;
437 int is_root = blkoff == h->rootoffs;
441 block = (struct ntreg_hbin_block *) (h->addr + blkoff);
443 seg_len = block_len (h, blkoff, &used);
444 if (seg_len <= 4 || (seg_len & 3) != 0) {
445 fprintf (stderr, "hivex: %s: block size %" PRIu32 " at 0x%zx, bad registry\n",
446 filename, le32toh (block->seg_len), blkoff);
452 fprintf (stderr, "hivex_open: %s block id %d,%d at 0x%zx size %zu%s\n",
453 used ? "used" : "free", block->id[0], block->id[1], blkoff,
454 seg_len, is_root ? " (root)" : "");
456 blocks_bytes += seg_len;
457 if (seg_len < smallest_block) smallest_block = seg_len;
458 if (seg_len > largest_block) largest_block = seg_len;
460 if (is_root && !used)
465 used_size += seg_len;
467 /* Root block must be an nk-block. */
468 if (is_root && (block->id[0] != 'n' || block->id[1] != 'k'))
471 /* Note this blkoff is a valid address. */
472 BITMAP_SET (h->bitmap, blkoff);
477 if (!seen_root_block) {
478 fprintf (stderr, "hivex: %s: no root block found\n", filename);
483 if (bad_root_block) {
484 fprintf (stderr, "hivex: %s: bad root block (free or not nk)\n", filename);
491 "hivex_open: successfully read Windows Registry hive file:\n"
492 " pages: %zu [sml: %zu, lge: %zu]\n"
493 " blocks: %zu [sml: %zu, avg: %zu, lge: %zu]\n"
494 " blocks used: %zu\n"
495 " bytes used: %zu\n",
496 pages, smallest_page, largest_page,
497 blocks, smallest_block, blocks_bytes / blocks, largest_block,
498 used_blocks, used_size);
506 if (h->addr && h->size && h->addr != MAP_FAILED)
507 munmap (h->addr, h->size);
518 hivex_close (hive_h *h)
523 munmap (h->addr, h->size);
532 hivex_root (hive_h *h)
534 hive_node_h ret = h->rootoffs;
535 if (!IS_VALID_BLOCK (h, ret)) {
543 hivex_node_name (hive_h *h, hive_node_h node)
545 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
550 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
552 /* AFAIK the node name is always plain ASCII, so no conversion
553 * to UTF-8 is necessary. However we do need to nul-terminate
557 /* nk->name_len is unsigned, 16 bit, so this is safe ... However
558 * we have to make sure the length doesn't exceed the block length.
560 size_t len = le16toh (nk->name_len);
561 size_t seg_len = block_len (h, node, NULL);
562 if (sizeof (struct ntreg_nk_record) + len - 1 > seg_len) {
564 fprintf (stderr, "hivex_node_name: returning EFAULT because node name is too long (%zu, %zu)\n",
570 char *ret = malloc (len + 1);
573 memcpy (ret, nk->name, len);
579 /* I think the documentation for the sk and classname fields in the nk
580 * record is wrong, or else the offset field is in the wrong place.
581 * Otherwise this makes no sense. Disabled this for now -- it's not
582 * useful for reading the registry anyway.
586 hivex_node_security (hive_h *h, hive_node_h node)
588 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
593 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
595 hive_node_h ret = le32toh (nk->sk);
597 if (!IS_VALID_BLOCK (h, ret)) {
605 hivex_node_classname (hive_h *h, hive_node_h node)
607 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
612 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
614 hive_node_h ret = le32toh (nk->classname);
616 if (!IS_VALID_BLOCK (h, ret)) {
624 /* Structure for returning 0-terminated lists of offsets (nodes,
634 init_offset_list (struct offset_list *list)
638 list->offsets = NULL;
641 #define INIT_OFFSET_LIST(name) \
642 struct offset_list name; \
643 init_offset_list (&name)
645 /* Preallocates the offset_list, but doesn't make the contents longer. */
647 grow_offset_list (struct offset_list *list, size_t alloc)
649 assert (alloc >= list->len);
650 size_t *p = realloc (list->offsets, alloc * sizeof (size_t));
659 add_to_offset_list (struct offset_list *list, size_t offset)
661 if (list->len >= list->alloc) {
662 if (grow_offset_list (list, list->alloc ? list->alloc * 2 : 4) == -1)
665 list->offsets[list->len] = offset;
671 free_offset_list (struct offset_list *list)
673 free (list->offsets);
677 return_offset_list (struct offset_list *list)
679 if (add_to_offset_list (list, 0) == -1)
681 return list->offsets; /* caller frees */
684 /* Iterate over children, returning child nodes and intermediate blocks. */
686 get_children (hive_h *h, hive_node_h node,
687 hive_node_h **children_ret, size_t **blocks_ret)
689 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
694 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
696 size_t nr_subkeys_in_nk = le32toh (nk->nr_subkeys);
698 INIT_OFFSET_LIST (children);
699 INIT_OFFSET_LIST (blocks);
701 /* Deal with the common "no subkeys" case quickly. */
702 if (nr_subkeys_in_nk == 0)
705 /* Arbitrarily limit the number of subkeys we will ever deal with. */
706 if (nr_subkeys_in_nk > 1000000) {
711 /* Preallocate space for the children. */
712 if (grow_offset_list (&children, nr_subkeys_in_nk) == -1)
715 /* The subkey_lf field can point either to an lf-record, which is
716 * the common case, or if there are lots of subkeys, to an
719 size_t subkey_lf = le32toh (nk->subkey_lf);
721 if (!IS_VALID_BLOCK (h, subkey_lf)) {
723 fprintf (stderr, "hivex_node_children: returning EFAULT because subkey_lf is not a valid block (%zu)\n",
729 if (add_to_offset_list (&blocks, subkey_lf) == -1)
732 struct ntreg_hbin_block *block =
733 (struct ntreg_hbin_block *) (h->addr + subkey_lf);
735 /* Points to lf-record? (Note, also "lh" but that is basically the
736 * same as "lf" as far as we are concerned here).
738 if (block->id[0] == 'l' && (block->id[1] == 'f' || block->id[1] == 'h')) {
739 struct ntreg_lf_record *lf = (struct ntreg_lf_record *) block;
741 /* Check number of subkeys in the nk-record matches number of subkeys
744 size_t nr_subkeys_in_lf = le16toh (lf->nr_keys);
747 fprintf (stderr, "hivex_node_children: nr_subkeys_in_nk = %zu, nr_subkeys_in_lf = %zu\n",
748 nr_subkeys_in_nk, nr_subkeys_in_lf);
750 if (nr_subkeys_in_nk != nr_subkeys_in_lf) {
755 size_t len = block_len (h, subkey_lf, NULL);
756 if (8 + nr_subkeys_in_lf * 8 > len) {
758 fprintf (stderr, "hivex_node_children: returning EFAULT because too many subkeys (%zu, %zu)\n",
759 nr_subkeys_in_lf, len);
765 for (i = 0; i < nr_subkeys_in_lf; ++i) {
766 hive_node_h subkey = le32toh (lf->keys[i].offset);
768 if (!IS_VALID_BLOCK (h, subkey)) {
770 fprintf (stderr, "hivex_node_children: returning EFAULT because subkey is not a valid block (0x%zx)\n",
775 if (add_to_offset_list (&children, subkey) == -1)
780 /* Points to ri-record? */
781 else if (block->id[0] == 'r' && block->id[1] == 'i') {
782 struct ntreg_ri_record *ri = (struct ntreg_ri_record *) block;
784 size_t nr_offsets = le16toh (ri->nr_offsets);
786 /* Count total number of children. */
788 for (i = 0; i < nr_offsets; ++i) {
789 hive_node_h offset = ri->offset[i];
791 if (!IS_VALID_BLOCK (h, offset)) {
793 fprintf (stderr, "hivex_node_children: returning EFAULT because ri-offset is not a valid block (0x%zx)\n",
798 if (!BLOCK_ID_EQ (h, offset, "lf") && !BLOCK_ID_EQ (h, offset, "lh")) {
803 if (add_to_offset_list (&blocks, offset) == -1)
806 struct ntreg_lf_record *lf =
807 (struct ntreg_lf_record *) (h->addr + offset);
809 count += le16toh (lf->nr_keys);
813 fprintf (stderr, "hivex_node_children: nr_subkeys_in_nk = %zu, counted = %zu\n",
814 nr_subkeys_in_nk, count);
816 if (nr_subkeys_in_nk != count) {
821 /* Copy list of children. Note nr_subkeys_in_nk is limited to
822 * something reasonable above.
824 for (i = 0; i < nr_offsets; ++i) {
825 hive_node_h offset = ri->offset[i];
827 if (!IS_VALID_BLOCK (h, offset)) {
829 fprintf (stderr, "hivex_node_children: returning EFAULT because ri-offset is not a valid block (0x%zx)\n",
834 if (!BLOCK_ID_EQ (h, offset, "lf") && !BLOCK_ID_EQ (h, offset, "lh")) {
839 struct ntreg_lf_record *lf =
840 (struct ntreg_lf_record *) (h->addr + offset);
843 for (j = 0; j < le16toh (lf->nr_keys); ++j) {
844 hive_node_h subkey = le32toh (lf->keys[j].offset);
846 if (!IS_VALID_BLOCK (h, subkey)) {
848 fprintf (stderr, "hivex_node_children: returning EFAULT because indirect subkey is not a valid block (0x%zx)\n",
853 if (add_to_offset_list (&children, subkey) == -1)
859 /* else not supported, set errno and fall through */
862 free_offset_list (&children);
863 free_offset_list (&blocks);
867 *children_ret = return_offset_list (&children);
868 *blocks_ret = return_offset_list (&blocks);
869 if (!*children_ret || !*blocks_ret)
875 hivex_node_children (hive_h *h, hive_node_h node)
877 hive_node_h *children;
880 if (get_children (h, node, &children, &blocks) == -1)
887 /* Very inefficient, but at least having a separate API call
888 * allows us to make it more efficient in future.
891 hivex_node_get_child (hive_h *h, hive_node_h node, const char *nname)
893 hive_node_h *children = NULL;
897 children = hivex_node_children (h, node);
898 if (!children) goto error;
901 for (i = 0; children[i] != 0; ++i) {
902 name = hivex_node_name (h, children[i]);
903 if (!name) goto error;
904 if (STRCASEEQ (name, nname)) {
908 free (name); name = NULL;
918 hivex_node_parent (hive_h *h, hive_node_h node)
920 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
925 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
927 hive_node_h ret = le32toh (nk->parent);
929 if (!IS_VALID_BLOCK (h, ret)) {
931 fprintf (stderr, "hivex_node_parent: returning EFAULT because parent is not a valid block (0x%zx)\n",
940 get_values (hive_h *h, hive_node_h node,
941 hive_value_h **values_ret, size_t **blocks_ret)
943 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
948 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
950 size_t nr_values = le32toh (nk->nr_values);
953 fprintf (stderr, "hivex_node_values: nr_values = %zu\n", nr_values);
955 INIT_OFFSET_LIST (values);
956 INIT_OFFSET_LIST (blocks);
958 /* Deal with the common "no values" case quickly. */
962 /* Arbitrarily limit the number of values we will ever deal with. */
963 if (nr_values > 100000) {
968 /* Preallocate space for the values. */
969 if (grow_offset_list (&values, nr_values) == -1)
972 /* Get the value list and check it looks reasonable. */
973 size_t vlist_offset = le32toh (nk->vallist);
974 vlist_offset += 0x1000;
975 if (!IS_VALID_BLOCK (h, vlist_offset)) {
977 fprintf (stderr, "hivex_node_values: returning EFAULT because value list is not a valid block (0x%zx)\n",
983 if (add_to_offset_list (&blocks, vlist_offset) == -1)
986 struct ntreg_value_list *vlist =
987 (struct ntreg_value_list *) (h->addr + vlist_offset);
989 size_t len = block_len (h, vlist_offset, NULL);
990 if (4 + nr_values * 4 > len) {
992 fprintf (stderr, "hivex_node_values: returning EFAULT because value list is too long (%zu, %zu)\n",
999 for (i = 0; i < nr_values; ++i) {
1000 hive_node_h value = vlist->offset[i];
1002 if (!IS_VALID_BLOCK (h, value)) {
1004 fprintf (stderr, "hivex_node_values: returning EFAULT because value is not a valid block (0x%zx)\n",
1009 if (add_to_offset_list (&values, value) == -1)
1014 *values_ret = return_offset_list (&values);
1015 *blocks_ret = return_offset_list (&blocks);
1016 if (!*values_ret || !*blocks_ret)
1021 free_offset_list (&values);
1022 free_offset_list (&blocks);
1027 hivex_node_values (hive_h *h, hive_node_h node)
1029 hive_value_h *values;
1032 if (get_values (h, node, &values, &blocks) == -1)
1039 /* Very inefficient, but at least having a separate API call
1040 * allows us to make it more efficient in future.
1043 hivex_node_get_value (hive_h *h, hive_node_h node, const char *key)
1045 hive_value_h *values = NULL;
1047 hive_value_h ret = 0;
1049 values = hivex_node_values (h, node);
1050 if (!values) goto error;
1053 for (i = 0; values[i] != 0; ++i) {
1054 name = hivex_value_key (h, values[i]);
1055 if (!name) goto error;
1056 if (STRCASEEQ (name, key)) {
1060 free (name); name = NULL;
1070 hivex_value_key (hive_h *h, hive_value_h value)
1072 if (!IS_VALID_BLOCK (h, value) || !BLOCK_ID_EQ (h, value, "vk")) {
1077 struct ntreg_vk_record *vk = (struct ntreg_vk_record *) (h->addr + value);
1079 /* AFAIK the key is always plain ASCII, so no conversion to UTF-8 is
1080 * necessary. However we do need to nul-terminate the string.
1083 /* vk->name_len is unsigned, 16 bit, so this is safe ... However
1084 * we have to make sure the length doesn't exceed the block length.
1086 size_t len = le16toh (vk->name_len);
1087 size_t seg_len = block_len (h, value, NULL);
1088 if (sizeof (struct ntreg_vk_record) + len - 1 > seg_len) {
1090 fprintf (stderr, "hivex_value_key: returning EFAULT because key length is too long (%zu, %zu)\n",
1096 char *ret = malloc (len + 1);
1099 memcpy (ret, vk->name, len);
1105 hivex_value_type (hive_h *h, hive_value_h value, hive_type *t, size_t *len)
1107 if (!IS_VALID_BLOCK (h, value) || !BLOCK_ID_EQ (h, value, "vk")) {
1112 struct ntreg_vk_record *vk = (struct ntreg_vk_record *) (h->addr + value);
1115 *t = le32toh (vk->data_type);
1118 *len = le32toh (vk->data_len);
1119 if (*len == 0x80000000) { /* special case */
1121 if (t) *t = hive_t_dword;
1130 hivex_value_value (hive_h *h, hive_value_h value,
1131 hive_type *t_rtn, size_t *len_rtn)
1133 if (!IS_VALID_BLOCK (h, value) || !BLOCK_ID_EQ (h, value, "vk")) {
1138 struct ntreg_vk_record *vk = (struct ntreg_vk_record *) (h->addr + value);
1143 t = le32toh (vk->data_type);
1145 len = le32toh (vk->data_len);
1146 if (len == 0x80000000) { /* special case */
1153 fprintf (stderr, "hivex_value_value: value=0x%zx, t=%d, len=%zu\n",
1161 /* Arbitrarily limit the length that we will read. */
1162 if (len > 1000000) {
1167 char *ret = malloc (len);
1171 /* If length is <= 4 it's always stored inline. */
1173 memcpy (ret, (char *) &vk->data_offset, len);
1177 size_t data_offset = le32toh (vk->data_offset);
1178 data_offset += 0x1000;
1179 if (!IS_VALID_BLOCK (h, data_offset)) {
1181 fprintf (stderr, "hivex_value_value: returning EFAULT because data offset is not a valid block (0x%zx)\n",
1188 /* Check that the declared size isn't larger than the block its in. */
1189 size_t blen = block_len (h, data_offset, NULL);
1192 fprintf (stderr, "hivex_value_value: returning EFAULT because data is longer than its block (data 0x%zx, data len %zu, block len %zu)\n",
1193 data_offset, len, blen);
1199 char *data = h->addr + data_offset + 4;
1200 memcpy (ret, data, len);
1205 windows_utf16_to_utf8 (/* const */ char *input, size_t len)
1207 iconv_t ic = iconv_open ("UTF-8", "UTF-16");
1208 if (ic == (iconv_t) -1)
1211 /* iconv(3) has an insane interface ... */
1213 /* Mostly UTF-8 will be smaller, so this is a good initial guess. */
1214 size_t outalloc = len;
1218 size_t outlen = outalloc;
1219 char *out = malloc (outlen + 1);
1229 size_t r = iconv (ic, &inp, &inlen, &outp, &outlen);
1230 if (r == (size_t) -1) {
1231 if (errno == E2BIG) {
1232 size_t prev = outalloc;
1233 /* Try again with a larger output buffer. */
1236 if (outalloc < prev)
1241 /* Else some conversion failure, eg. EILSEQ, EINVAL. */
1257 hivex_value_string (hive_h *h, hive_value_h value)
1261 char *data = hivex_value_value (h, value, &t, &len);
1266 if (t != hive_t_string && t != hive_t_expand_string && t != hive_t_link) {
1272 char *ret = windows_utf16_to_utf8 (data, len);
1281 free_strings (char **argv)
1286 for (i = 0; argv[i] != NULL; ++i)
1292 /* Get the length of a UTF-16 format string. Handle the string as
1293 * pairs of bytes, looking for the first \0\0 pair.
1296 utf16_string_len_in_bytes (const char *str)
1300 while (str[0] || str[1]) {
1308 /* http://blogs.msdn.com/oldnewthing/archive/2009/10/08/9904646.aspx */
1310 hivex_value_multiple_strings (hive_h *h, hive_value_h value)
1314 char *data = hivex_value_value (h, value, &t, &len);
1319 if (t != hive_t_multiple_strings) {
1325 size_t nr_strings = 0;
1326 char **ret = malloc ((1 + nr_strings) * sizeof (char *));
1336 while (p < data + len && (plen = utf16_string_len_in_bytes (p)) > 0) {
1338 char **ret2 = realloc (ret, (1 + nr_strings) * sizeof (char *));
1346 ret[nr_strings-1] = windows_utf16_to_utf8 (p, plen);
1347 ret[nr_strings] = NULL;
1348 if (ret[nr_strings-1] == NULL) {
1354 p += plen + 2 /* skip over UTF-16 \0\0 at the end of this string */;
1362 hivex_value_dword (hive_h *h, hive_value_h value)
1366 char *data = hivex_value_value (h, value, &t, &len);
1371 if ((t != hive_t_dword && t != hive_t_dword_be) || len != 4) {
1377 int32_t ret = *(int32_t*)data;
1379 if (t == hive_t_dword) /* little endian */
1380 ret = le32toh (ret);
1382 ret = be32toh (ret);
1388 hivex_value_qword (hive_h *h, hive_value_h value)
1392 char *data = hivex_value_value (h, value, &t, &len);
1397 if (t != hive_t_qword || len != 8) {
1403 int64_t ret = *(int64_t*)data;
1405 ret = le64toh (ret); /* always little endian */
1411 hivex_visit (hive_h *h, const struct hivex_visitor *visitor, size_t len,
1412 void *opaque, int flags)
1414 return hivex_visit_node (h, hivex_root (h), visitor, len, opaque, flags);
1417 static int hivex__visit_node (hive_h *h, hive_node_h node, const struct hivex_visitor *vtor, char *unvisited, void *opaque, int flags);
1420 hivex_visit_node (hive_h *h, hive_node_h node,
1421 const struct hivex_visitor *visitor, size_t len, void *opaque,
1424 struct hivex_visitor vtor;
1425 memset (&vtor, 0, sizeof vtor);
1427 /* Note that len might be larger *or smaller* than the expected size. */
1428 size_t copysize = len <= sizeof vtor ? len : sizeof vtor;
1429 memcpy (&vtor, visitor, copysize);
1431 /* This bitmap records unvisited nodes, so we don't loop if the
1432 * registry contains cycles.
1434 char *unvisited = malloc (1 + h->size / 32);
1435 if (unvisited == NULL)
1437 memcpy (unvisited, h->bitmap, 1 + h->size / 32);
1439 int r = hivex__visit_node (h, node, &vtor, unvisited, opaque, flags);
1445 hivex__visit_node (hive_h *h, hive_node_h node,
1446 const struct hivex_visitor *vtor, char *unvisited,
1447 void *opaque, int flags)
1449 int skip_bad = flags & HIVEX_VISIT_SKIP_BAD;
1451 hive_value_h *values = NULL;
1452 hive_node_h *children = NULL;
1458 /* Return -1 on all callback errors. However on internal errors,
1459 * check if skip_bad is set and suppress those errors if so.
1463 if (!BITMAP_TST (unvisited, node)) {
1465 fprintf (stderr, "hivex__visit_node: contains cycle: visited node 0x%zx already\n",
1469 return skip_bad ? 0 : -1;
1471 BITMAP_CLR (unvisited, node);
1473 name = hivex_node_name (h, node);
1474 if (!name) return skip_bad ? 0 : -1;
1475 if (vtor->node_start && vtor->node_start (h, opaque, node, name) == -1)
1478 values = hivex_node_values (h, node);
1480 ret = skip_bad ? 0 : -1;
1484 for (i = 0; values[i] != 0; ++i) {
1488 if (hivex_value_type (h, values[i], &t, &len) == -1) {
1489 ret = skip_bad ? 0 : -1;
1493 key = hivex_value_key (h, values[i]);
1495 ret = skip_bad ? 0 : -1;
1499 if (vtor->value_any) {
1500 str = hivex_value_value (h, values[i], &t, &len);
1502 ret = skip_bad ? 0 : -1;
1505 if (vtor->value_any (h, opaque, node, values[i], t, len, key, str) == -1)
1507 free (str); str = NULL;
1512 str = hivex_value_value (h, values[i], &t, &len);
1514 ret = skip_bad ? 0 : -1;
1517 if (t != hive_t_none) {
1518 ret = skip_bad ? 0 : -1;
1521 if (vtor->value_none &&
1522 vtor->value_none (h, opaque, node, values[i], t, len, key, str) == -1)
1524 free (str); str = NULL;
1528 case hive_t_expand_string:
1530 str = hivex_value_string (h, values[i]);
1532 if (errno != EILSEQ && errno != EINVAL) {
1533 ret = skip_bad ? 0 : -1;
1536 if (vtor->value_string_invalid_utf16) {
1537 str = hivex_value_value (h, values[i], &t, &len);
1538 if (vtor->value_string_invalid_utf16 (h, opaque, node, values[i], t, len, key, str) == -1)
1540 free (str); str = NULL;
1544 if (vtor->value_string &&
1545 vtor->value_string (h, opaque, node, values[i], t, len, key, str) == -1)
1547 free (str); str = NULL;
1551 case hive_t_dword_be: {
1552 int32_t i32 = hivex_value_dword (h, values[i]);
1553 if (vtor->value_dword &&
1554 vtor->value_dword (h, opaque, node, values[i], t, len, key, i32) == -1)
1559 case hive_t_qword: {
1560 int64_t i64 = hivex_value_qword (h, values[i]);
1561 if (vtor->value_qword &&
1562 vtor->value_qword (h, opaque, node, values[i], t, len, key, i64) == -1)
1568 str = hivex_value_value (h, values[i], &t, &len);
1570 ret = skip_bad ? 0 : -1;
1573 if (t != hive_t_binary) {
1574 ret = skip_bad ? 0 : -1;
1577 if (vtor->value_binary &&
1578 vtor->value_binary (h, opaque, node, values[i], t, len, key, str) == -1)
1580 free (str); str = NULL;
1583 case hive_t_multiple_strings:
1584 strs = hivex_value_multiple_strings (h, values[i]);
1586 if (errno != EILSEQ && errno != EINVAL) {
1587 ret = skip_bad ? 0 : -1;
1590 if (vtor->value_string_invalid_utf16) {
1591 str = hivex_value_value (h, values[i], &t, &len);
1592 if (vtor->value_string_invalid_utf16 (h, opaque, node, values[i], t, len, key, str) == -1)
1594 free (str); str = NULL;
1598 if (vtor->value_multiple_strings &&
1599 vtor->value_multiple_strings (h, opaque, node, values[i], t, len, key, strs) == -1)
1601 free_strings (strs); strs = NULL;
1604 case hive_t_resource_list:
1605 case hive_t_full_resource_description:
1606 case hive_t_resource_requirements_list:
1608 str = hivex_value_value (h, values[i], &t, &len);
1610 ret = skip_bad ? 0 : -1;
1613 if (vtor->value_other &&
1614 vtor->value_other (h, opaque, node, values[i], t, len, key, str) == -1)
1616 free (str); str = NULL;
1621 free (key); key = NULL;
1624 children = hivex_node_children (h, node);
1625 if (children == NULL) {
1626 ret = skip_bad ? 0 : -1;
1630 for (i = 0; children[i] != 0; ++i) {
1632 fprintf (stderr, "hivex__visit_node: %s: visiting subkey %d (0x%zx)\n",
1633 name, i, children[i]);
1635 if (hivex__visit_node (h, children[i], vtor, unvisited, opaque, flags) == -1)
1639 if (vtor->node_end && vtor->node_end (h, opaque, node, name) == -1)
1650 free_strings (strs);