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.
37 #ifdef HAVE_BYTESWAP_H
41 #if __BYTE_ORDER == __LITTLE_ENDIAN
43 #define be32toh(x) __bswap_32 (x)
46 #define be64toh(x) __bswap_64 (x)
49 #define le32toh(x) (x)
53 #define be32toh(x) (x)
56 #define be64toh(x) (x)
59 #define le32toh(x) __bswap_32 (x)
70 /* Memory-mapped (readonly) registry file. */
73 struct ntreg_header *hdr;
76 /* Use a bitmap to store which file offsets are valid (point to a
77 * used block). We only need to store 1 bit per 32 bits of the file
78 * (because blocks are 4-byte aligned). We found that the average
79 * block size in a registry file is ~50 bytes. So roughly 1 in 12
80 * bits in the bitmap will be set, making it likely a more efficient
81 * structure than a hash table.
84 #define BITMAP_SET(bitmap,off) (bitmap[(off)>>5] |= 1 << (((off)>>2)&7))
85 #define BITMAP_CLR(bitmap,off) (bitmap[(off)>>5] &= ~ (1 << (((off)>>2)&7)))
86 #define BITMAP_TST(bitmap,off) (bitmap[(off)>>5] & (1 << (((off)>>2)&7)))
87 #define IS_VALID_BLOCK(h,off) \
88 (((off) & 3) == 0 && \
90 (off) < (h)->size && \
91 BITMAP_TST((h)->bitmap,(off)))
93 /* Fields from the header, extracted from little-endianness. */
94 size_t rootoffs; /* Root key offset (always an nk-block). */
97 size_t pages; /* Number of hbin pages read. */
98 size_t blocks; /* Total number of blocks found. */
99 size_t used_blocks; /* Total number of used blocks found. */
100 size_t used_size; /* Total size (bytes) of used blocks. */
103 /* NB. All fields are little endian. */
104 struct ntreg_header {
105 char magic[4]; /* "regf" */
108 char last_modified[8];
109 uint32_t unknown3; /* 1 */
110 uint32_t unknown4; /* 3 */
111 uint32_t unknown5; /* 0 */
112 uint32_t unknown6; /* 1 */
113 uint32_t offset; /* offset of root key record - 4KB */
114 uint32_t blocks; /* size in bytes of data (filesize - 4KB) */
115 uint32_t unknown7; /* 1 */
116 char name[0x1fc-0x2c];
117 uint32_t csum; /* checksum: sum of 32 bit words 0-0x1fb. */
118 } __attribute__((__packed__));
120 struct ntreg_hbin_page {
121 char magic[4]; /* "hbin" */
122 uint32_t offset_first; /* offset from 1st block */
123 uint32_t offset_next; /* offset of next (relative to this) */
125 /* Linked list of blocks follows here. */
126 } __attribute__((__packed__));
128 struct ntreg_hbin_block {
129 int32_t seg_len; /* length of this block (-ve for used block) */
130 char id[2]; /* the block type (eg. "nk" for nk record) */
131 /* Block data follows here. */
132 } __attribute__((__packed__));
134 #define BLOCK_ID_EQ(h,offs,eqid) \
135 (strncmp (((struct ntreg_hbin_block *)((h)->addr + (offs)))->id, (eqid), 2) == 0)
138 block_len (hive_h *h, size_t blkoff, int *used)
140 struct ntreg_hbin_block *block;
141 block = (struct ntreg_hbin_block *) (h->addr + blkoff);
143 int32_t len = le32toh (block->seg_len);
154 struct ntreg_nk_record {
155 int32_t seg_len; /* length (always -ve because used) */
156 char id[2]; /* "nk" */
159 uint32_t parent; /* offset of owner/parent */
160 uint32_t nr_subkeys; /* number of subkeys */
162 uint32_t subkey_lf; /* lf record containing list of subkeys */
164 uint32_t nr_values; /* number of values */
165 uint32_t vallist; /* value-list record */
166 uint32_t sk; /* offset of sk-record */
167 uint32_t classname; /* offset of classname record */
170 uint16_t name_len; /* length of name */
171 uint16_t classname_len; /* length of classname */
172 char name[1]; /* name follows here */
173 } __attribute__((__packed__));
175 struct ntreg_lf_record {
177 char id[2]; /* "lf" */
178 uint16_t nr_keys; /* number of keys in this record */
180 uint32_t offset; /* offset of nk-record for this subkey */
181 char name[4]; /* first 4 characters of subkey name */
183 } __attribute__((__packed__));
185 struct ntreg_ri_record {
187 char id[2]; /* "ri" */
188 uint16_t nr_offsets; /* number of pointers to lh records */
189 uint32_t offset[1]; /* list of pointers to lh records */
190 } __attribute__((__packed__));
192 /* This has no ID header. */
193 struct ntreg_value_list {
195 uint32_t offset[1]; /* list of pointers to vk records */
196 } __attribute__((__packed__));
198 struct ntreg_vk_record {
199 int32_t seg_len; /* length (always -ve because used) */
200 char id[2]; /* "vk" */
201 uint16_t name_len; /* length of name */
202 /* length of the data:
203 * If data_len is <= 4, then it's stored inline.
204 * If data_len is 0x80000000, then it's an inline dword.
205 * Top bit may be set or not set at random.
208 uint32_t data_offset; /* pointer to the data (or data if inline) */
209 hive_type data_type; /* type of the data */
210 uint16_t unknown1; /* possibly always 1 */
212 char name[1]; /* key name follows here */
213 } __attribute__((__packed__));
216 hivex_open (const char *filename, int flags)
220 h = calloc (1, sizeof *h);
224 h->msglvl = flags & HIVEX_OPEN_MSGLVL_MASK;
226 const char *debug = getenv ("HIVEX_DEBUG");
227 if (debug && strcmp (debug, "1") == 0)
231 printf ("hivex_open: created handle %p\n", h);
233 h->fd = open (filename, O_RDONLY);
238 if (fstat (h->fd, &statbuf) == -1)
241 h->size = statbuf.st_size;
243 h->addr = mmap (NULL, h->size, PROT_READ, MAP_SHARED, h->fd, 0);
244 if (h->addr == MAP_FAILED)
248 printf ("hivex_open: mapped file at %p\n", h->addr);
251 if (h->hdr->magic[0] != 'r' ||
252 h->hdr->magic[1] != 'e' ||
253 h->hdr->magic[2] != 'g' ||
254 h->hdr->magic[3] != 'f') {
255 fprintf (stderr, "hivex: %s: not a Windows NT Registry hive file\n",
261 h->bitmap = calloc (1 + h->size / 32, 1);
263 #if 0 /* Doesn't work. */
264 /* Header checksum. */
265 uint32_t *daddr = h->addr;
268 for (i = 0; i < 0x1fc / 4; ++i) {
269 sum += le32toh (*daddr);
272 if (sum != le32toh (h->hdr->csum)) {
273 fprintf (stderr, "hivex: %s: bad checksum in hive header\n", filename);
279 h->rootoffs = le32toh (h->hdr->offset) + 0x1000;
282 printf ("hivex_open: root offset = %zu\n", h->rootoffs);
284 /* We'll set this flag when we see a block with the root offset (ie.
287 int seen_root_block = 0, bad_root_block = 0;
289 /* Read the pages and blocks. The aim here is to be robust against
290 * corrupt or malicious registries. So we make sure the loops
291 * always make forward progress. We add the address of each block
292 * we read to a hash table so pointers will only reference the start
296 struct ntreg_hbin_page *page;
297 for (off = 0x1000; off < h->size; off += le32toh (page->offset_next)) {
300 page = (struct ntreg_hbin_page *) (h->addr + off);
301 if (page->magic[0] != 'h' ||
302 page->magic[1] != 'b' ||
303 page->magic[2] != 'i' ||
304 page->magic[3] != 'n') {
305 /* This error is seemingly common in uncorrupt registry files. */
307 fprintf (stderr, "hivex: %s: ignoring trailing garbage at end of file (at %zu, after %zu pages)\n",
308 filename, off, h->pages);
314 printf ("hivex_open: page at %zu\n", off);
316 if (le32toh (page->offset_next) <= sizeof (struct ntreg_hbin_page) ||
317 (le32toh (page->offset_next) & 3) != 0) {
318 fprintf (stderr, "hivex: %s: pagesize %d at %zu, bad registry\n",
319 filename, le32toh (page->offset_next), off);
324 /* Read the blocks in this page. */
326 struct ntreg_hbin_block *block;
328 for (blkoff = off + 0x20;
329 blkoff < off + le32toh (page->offset_next);
333 int is_root = blkoff == h->rootoffs;
337 block = (struct ntreg_hbin_block *) (h->addr + blkoff);
339 seg_len = block_len (h, blkoff, &used);
340 if (seg_len <= 4 || (seg_len & 3) != 0) {
341 fprintf (stderr, "hivex: %s: block size %d at %zu, bad registry\n",
342 filename, le32toh (block->seg_len), blkoff);
348 printf ("hivex_open: %s block id %d,%d at %zu%s\n",
349 used ? "used" : "free", block->id[0], block->id[1], blkoff,
350 is_root ? " (root)" : "");
352 if (is_root && !used)
357 h->used_size += seg_len;
359 /* Root block must be an nk-block. */
360 if (is_root && (block->id[0] != 'n' || block->id[1] != 'k'))
363 /* Note this blkoff is a valid address. */
364 BITMAP_SET (h->bitmap, blkoff);
369 if (!seen_root_block) {
370 fprintf (stderr, "hivex: %s: no root block found\n", filename);
375 if (bad_root_block) {
376 fprintf (stderr, "hivex: %s: bad root block (free or not nk)\n", filename);
382 printf ("hivex_open: successfully read Windows Registry hive file:\n"
385 " blocks used: %zu\n"
386 " bytes used: %zu\n",
387 h->pages, h->blocks, h->used_blocks, h->used_size);
395 if (h->addr && h->size && h->addr != MAP_FAILED)
396 munmap (h->addr, h->size);
406 hivex_close (hive_h *h)
411 munmap (h->addr, h->size);
419 hivex_root (hive_h *h)
421 hive_node_h ret = h->rootoffs;
422 if (!IS_VALID_BLOCK (h, ret)) {
430 hivex_node_name (hive_h *h, hive_node_h node)
432 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
437 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
439 /* AFAIK the node name is always plain ASCII, so no conversion
440 * to UTF-8 is necessary. However we do need to nul-terminate
444 /* nk->name_len is unsigned, 16 bit, so this is safe ... However
445 * we have to make sure the length doesn't exceed the block length.
447 size_t len = le16toh (nk->name_len);
448 size_t seg_len = block_len (h, node, NULL);
449 if (sizeof (struct ntreg_nk_record) + len - 1 > seg_len) {
451 printf ("hivex_node_name: returning EFAULT because node name is too long (%zu, %zu)\n",
457 char *ret = malloc (len + 1);
460 memcpy (ret, nk->name, len);
466 /* I think the documentation for the sk and classname fields in the nk
467 * record is wrong, or else the offset field is in the wrong place.
468 * Otherwise this makes no sense. Disabled this for now -- it's not
469 * useful for reading the registry anyway.
473 hivex_node_security (hive_h *h, hive_node_h node)
475 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
480 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
482 hive_node_h ret = le32toh (nk->sk);
484 if (!IS_VALID_BLOCK (h, ret)) {
492 hivex_node_classname (hive_h *h, hive_node_h node)
494 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
499 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
501 hive_node_h ret = le32toh (nk->classname);
503 if (!IS_VALID_BLOCK (h, ret)) {
512 hivex_node_children (hive_h *h, hive_node_h node)
514 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
519 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
521 size_t nr_subkeys_in_nk = le32toh (nk->nr_subkeys);
523 /* Deal with the common "no subkeys" case quickly. */
525 if (nr_subkeys_in_nk == 0) {
526 ret = malloc (sizeof (hive_node_h));
533 /* Arbitrarily limit the number of subkeys we will ever deal with. */
534 if (nr_subkeys_in_nk > 1000000) {
539 /* The subkey_lf field can point either to an lf-record, which is
540 * the common case, or if there are lots of subkeys, to an
543 size_t subkey_lf = le32toh (nk->subkey_lf);
545 if (!IS_VALID_BLOCK (h, subkey_lf)) {
547 printf ("hivex_node_children: returning EFAULT because subkey_lf is not a valid block (%zu)\n",
553 struct ntreg_hbin_block *block =
554 (struct ntreg_hbin_block *) (h->addr + subkey_lf);
556 /* Points to lf-record? (Note, also "lh" but that is basically the
557 * same as "lf" as far as we are concerned here).
559 if (block->id[0] == 'l' && (block->id[1] == 'f' || block->id[1] == 'h')) {
560 struct ntreg_lf_record *lf = (struct ntreg_lf_record *) block;
562 /* Check number of subkeys in the nk-record matches number of subkeys
565 size_t nr_subkeys_in_lf = le16toh (lf->nr_keys);
568 printf ("hivex_node_children: nr_subkeys_in_nk = %zu, nr_subkeys_in_lf = %zu\n",
569 nr_subkeys_in_nk, nr_subkeys_in_lf);
571 if (nr_subkeys_in_nk != nr_subkeys_in_lf) {
576 size_t len = block_len (h, subkey_lf, NULL);
577 if (8 + nr_subkeys_in_lf * 8 > len) {
579 printf ("hivex_node_children: returning EFAULT because too many subkeys (%zu, %zu)\n",
580 nr_subkeys_in_lf, len);
585 /* Allocate space for the returned values. Note that
586 * nr_subkeys_in_lf is limited to a 16 bit value.
588 ret = malloc ((1 + nr_subkeys_in_lf) * sizeof (hive_node_h));
593 for (i = 0; i < nr_subkeys_in_lf; ++i) {
594 hive_node_h subkey = lf->keys[i].offset;
596 if (!IS_VALID_BLOCK (h, subkey)) {
598 printf ("hivex_node_children: returning EFAULT because subkey is not a valid block (%zu)\n",
609 /* Points to ri-record? */
610 else if (block->id[0] == 'r' && block->id[1] == 'i') {
611 struct ntreg_ri_record *ri = (struct ntreg_ri_record *) block;
613 size_t nr_offsets = le16toh (ri->nr_offsets);
615 /* Count total number of children. */
617 for (i = 0; i < nr_offsets; ++i) {
618 hive_node_h offset = ri->offset[i];
620 if (!IS_VALID_BLOCK (h, offset)) {
622 printf ("hivex_node_children: returning EFAULT because ri-offset is not a valid block (%zu)\n",
627 if (!BLOCK_ID_EQ (h, offset, "lf") && !BLOCK_ID_EQ (h, offset, "lh")) {
632 struct ntreg_lf_record *lf =
633 (struct ntreg_lf_record *) (h->addr + offset);
635 count += le16toh (lf->nr_keys);
639 printf ("hivex_node_children: nr_subkeys_in_nk = %zu, counted = %zu\n",
640 nr_subkeys_in_nk, count);
642 if (nr_subkeys_in_nk != count) {
647 /* Copy list of children. Note nr_subkeys_in_nk is limited to
648 * something reasonable above.
650 ret = malloc ((1 + nr_subkeys_in_nk) * sizeof (hive_node_h));
655 for (i = 0; i < nr_offsets; ++i) {
656 hive_node_h offset = ri->offset[i];
658 if (!IS_VALID_BLOCK (h, offset)) {
660 printf ("hivex_node_children: returning EFAULT because ri-offset is not a valid block (%zu)\n",
665 if (!BLOCK_ID_EQ (h, offset, "lf") && !BLOCK_ID_EQ (h, offset, "lh")) {
670 struct ntreg_lf_record *lf =
671 (struct ntreg_lf_record *) (h->addr + offset);
674 for (j = 0; j < le16toh (lf->nr_keys); ++j) {
675 hive_node_h subkey = lf->keys[j].offset;
677 if (!IS_VALID_BLOCK (h, subkey)) {
679 printf ("hivex_node_children: returning EFAULT because indirect subkey is not a valid block (%zu)\n",
685 ret[count++] = subkey;
698 /* Very inefficient, but at least having a separate API call
699 * allows us to make it more efficient in future.
702 hivex_node_get_child (hive_h *h, hive_node_h node, const char *nname)
704 hive_node_h *children = NULL;
708 children = hivex_node_children (h, node);
709 if (!children) goto error;
712 for (i = 0; children[i] != 0; ++i) {
713 name = hivex_node_name (h, children[i]);
714 if (!name) goto error;
715 if (strcasecmp (name, nname) == 0) {
719 free (name); name = NULL;
729 hivex_node_parent (hive_h *h, hive_node_h node)
731 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
736 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
738 hive_node_h ret = le32toh (nk->parent);
740 printf ("parent = %zu\n", ret);
741 if (!IS_VALID_BLOCK (h, ret)) {
743 printf ("hivex_node_parent: returning EFAULT because parent is not a valid block (%zu)\n",
752 hivex_node_values (hive_h *h, hive_node_h node)
754 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
759 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
761 size_t nr_values = le32toh (nk->nr_values);
764 printf ("hivex_node_values: nr_values = %zu\n", nr_values);
766 /* Deal with the common "no values" case quickly. */
768 if (nr_values == 0) {
769 ret = malloc (sizeof (hive_node_h));
776 /* Arbitrarily limit the number of values we will ever deal with. */
777 if (nr_values > 100000) {
782 /* Get the value list and check it looks reasonable. */
783 size_t vlist_offset = le32toh (nk->vallist);
784 vlist_offset += 0x1000;
785 if (!IS_VALID_BLOCK (h, vlist_offset)) {
787 printf ("hivex_node_values: returning EFAULT because value list is not a valid block (%zu)\n",
793 struct ntreg_value_list *vlist =
794 (struct ntreg_value_list *) (h->addr + vlist_offset);
796 size_t len = block_len (h, vlist_offset, NULL);
797 if (4 + nr_values * 4 > len) {
799 printf ("hivex_node_values: returning EFAULT because value list is too long (%zu, %zu)\n",
805 /* Allocate return array and copy values in. */
806 ret = malloc ((1 + nr_values) * sizeof (hive_node_h));
811 for (i = 0; i < nr_values; ++i) {
812 hive_node_h value = vlist->offset[i];
814 if (!IS_VALID_BLOCK (h, value)) {
816 printf ("hivex_node_values: returning EFAULT because value is not a valid block (%zu)\n",
829 /* Very inefficient, but at least having a separate API call
830 * allows us to make it more efficient in future.
833 hivex_node_get_value (hive_h *h, hive_node_h node, const char *key)
835 hive_value_h *values = NULL;
837 hive_value_h ret = 0;
839 values = hivex_node_values (h, node);
840 if (!values) goto error;
843 for (i = 0; values[i] != 0; ++i) {
844 name = hivex_value_key (h, values[i]);
845 if (!name) goto error;
846 if (strcasecmp (name, key) == 0) {
850 free (name); name = NULL;
860 hivex_value_key (hive_h *h, hive_value_h value)
862 if (!IS_VALID_BLOCK (h, value) || !BLOCK_ID_EQ (h, value, "vk")) {
867 struct ntreg_vk_record *vk = (struct ntreg_vk_record *) (h->addr + value);
869 /* AFAIK the key is always plain ASCII, so no conversion to UTF-8 is
870 * necessary. However we do need to nul-terminate the string.
873 /* vk->name_len is unsigned, 16 bit, so this is safe ... However
874 * we have to make sure the length doesn't exceed the block length.
876 size_t len = le16toh (vk->name_len);
877 size_t seg_len = block_len (h, value, NULL);
878 if (sizeof (struct ntreg_vk_record) + len - 1 > seg_len) {
880 printf ("hivex_value_key: returning EFAULT because key length is too long (%zu, %zu)\n",
886 char *ret = malloc (len + 1);
889 memcpy (ret, vk->name, len);
895 hivex_value_type (hive_h *h, hive_value_h value, hive_type *t, size_t *len)
897 if (!IS_VALID_BLOCK (h, value) || !BLOCK_ID_EQ (h, value, "vk")) {
902 struct ntreg_vk_record *vk = (struct ntreg_vk_record *) (h->addr + value);
905 *t = le32toh (vk->data_type);
908 *len = le32toh (vk->data_len);
909 if (*len == 0x80000000) { /* special case */
911 if (t) *t = hive_t_dword;
920 hivex_value_value (hive_h *h, hive_value_h value,
921 hive_type *t_rtn, size_t *len_rtn)
923 if (!IS_VALID_BLOCK (h, value) || !BLOCK_ID_EQ (h, value, "vk")) {
928 struct ntreg_vk_record *vk = (struct ntreg_vk_record *) (h->addr + value);
933 t = le32toh (vk->data_type);
935 len = le32toh (vk->data_len);
936 if (len == 0x80000000) { /* special case */
943 printf ("hivex_value_value: value=%zu, t=%d, len=%zu\n",
951 /* Arbitrarily limit the length that we will read. */
957 char *ret = malloc (len);
961 /* If length is <= 4 it's always stored inline. */
963 memcpy (ret, (char *) &vk->data_offset, len);
967 size_t data_offset = vk->data_offset;
968 data_offset += 0x1000;
969 if (!IS_VALID_BLOCK (h, data_offset)) {
971 printf ("hivex_value_value: returning EFAULT because data offset is not a valid block (%zu)\n",
978 /* Check that the declared size isn't larger than the block its in. */
979 size_t blen = block_len (h, data_offset, NULL);
982 printf ("hivex_value_value: returning EFAULT because data is longer than its block (%zu, %zu)\n",
989 char *data = h->addr + data_offset + 4;
990 memcpy (ret, data, len);
995 windows_utf16_to_utf8 (/* const */ char *input, size_t len)
997 iconv_t ic = iconv_open ("UTF-8", "UTF-16");
998 if (ic == (iconv_t) -1)
1001 /* iconv(3) has an insane interface ... */
1003 /* Mostly UTF-8 will be smaller, so this is a good initial guess. */
1004 size_t outalloc = len;
1008 size_t outlen = outalloc;
1009 char *out = malloc (outlen + 1);
1019 size_t r = iconv (ic, &inp, &inlen, &outp, &outlen);
1020 if (r == (size_t) -1) {
1021 if (errno == E2BIG) {
1022 /* Try again with a larger output buffer. */
1028 /* Else some conversion failure, eg. EILSEQ, EINVAL. */
1044 hivex_value_string (hive_h *h, hive_value_h value)
1048 char *data = hivex_value_value (h, value, &t, &len);
1053 if (t != hive_t_string && t != hive_t_expand_string && t != hive_t_link) {
1059 char *ret = windows_utf16_to_utf8 (data, len);
1068 free_strings (char **argv)
1073 for (i = 0; argv[i] != NULL; ++i)
1079 /* Get the length of a UTF-16 format string. Handle the string as
1080 * pairs of bytes, looking for the first \0\0 pair.
1083 utf16_string_len_in_bytes (const char *str)
1087 while (str[0] || str[1]) {
1095 /* http://blogs.msdn.com/oldnewthing/archive/2009/10/08/9904646.aspx */
1097 hivex_value_multiple_strings (hive_h *h, hive_value_h value)
1101 char *data = hivex_value_value (h, value, &t, &len);
1106 if (t != hive_t_multiple_strings) {
1112 size_t nr_strings = 0;
1113 char **ret = malloc ((1 + nr_strings) * sizeof (char *));
1123 while (p < data + len && (plen = utf16_string_len_in_bytes (p)) > 0) {
1125 char **ret2 = realloc (ret, (1 + nr_strings) * sizeof (char *));
1133 ret[nr_strings-1] = windows_utf16_to_utf8 (p, plen);
1134 ret[nr_strings] = NULL;
1135 if (ret[nr_strings-1] == NULL) {
1141 p += plen + 2 /* skip over UTF-16 \0\0 at the end of this string */;
1149 hivex_value_dword (hive_h *h, hive_value_h value)
1153 char *data = hivex_value_value (h, value, &t, &len);
1158 if ((t != hive_t_dword && t != hive_t_dword_be) || len != 4) {
1164 int32_t ret = *(int32_t*)data;
1166 if (t == hive_t_dword) /* little endian */
1167 ret = le32toh (ret);
1169 ret = be32toh (ret);
1175 hivex_value_qword (hive_h *h, hive_value_h value)
1179 char *data = hivex_value_value (h, value, &t, &len);
1184 if (t != hive_t_qword || len != 8) {
1190 int64_t ret = *(int64_t*)data;
1192 ret = le64toh (ret); /* always little endian */
1198 hivex_visit (hive_h *h, const struct hivex_visitor *visitor, size_t len,
1199 void *opaque, int flags)
1201 return hivex_visit_node (h, hivex_root (h), visitor, len, opaque, flags);
1204 static int hivex__visit_node (hive_h *h, hive_node_h node, const struct hivex_visitor *vtor, char *unvisited, void *opaque, int flags);
1207 hivex_visit_node (hive_h *h, hive_node_h node,
1208 const struct hivex_visitor *visitor, size_t len, void *opaque,
1211 struct hivex_visitor vtor;
1212 memset (&vtor, 0, sizeof vtor);
1214 /* Note that len might be larger *or smaller* than the expected size. */
1215 size_t copysize = len <= sizeof vtor ? len : sizeof vtor;
1216 memcpy (&vtor, visitor, copysize);
1218 /* This bitmap records unvisited nodes, so we don't loop if the
1219 * registry contains cycles.
1221 char *unvisited = malloc (1 + h->size / 32);
1222 if (unvisited == NULL)
1224 memcpy (unvisited, h->bitmap, 1 + h->size / 32);
1226 int r = hivex__visit_node (h, node, &vtor, unvisited, opaque, flags);
1232 hivex__visit_node (hive_h *h, hive_node_h node,
1233 const struct hivex_visitor *vtor, char *unvisited,
1234 void *opaque, int flags)
1236 int skip_bad = flags & HIVEX_VISIT_SKIP_BAD;
1238 hive_value_h *values = NULL;
1239 hive_node_h *children = NULL;
1245 /* Return -1 on all callback errors. However on internal errors,
1246 * check if skip_bad is set and suppress those errors if so.
1250 if (!BITMAP_TST (unvisited, node)) {
1252 printf ("hivex__visit_node: contains cycle: visited node %zu already\n",
1256 return skip_bad ? 0 : -1;
1258 BITMAP_CLR (unvisited, node);
1260 name = hivex_node_name (h, node);
1261 if (!name) return skip_bad ? 0 : -1;
1262 if (vtor->node_start && vtor->node_start (h, opaque, node, name) == -1)
1265 values = hivex_node_values (h, node);
1267 ret = skip_bad ? 0 : -1;
1271 for (i = 0; values[i] != 0; ++i) {
1275 if (hivex_value_type (h, values[i], &t, &len) == -1) {
1276 ret = skip_bad ? 0 : -1;
1280 key = hivex_value_key (h, values[i]);
1282 ret = skip_bad ? 0 : -1;
1288 str = hivex_value_value (h, values[i], &t, &len);
1290 ret = skip_bad ? 0 : -1;
1293 if (t != hive_t_none) {
1294 ret = skip_bad ? 0 : -1;
1297 if (vtor->value_none &&
1298 vtor->value_none (h, opaque, node, values[i], t, len, key, str) == -1)
1300 free (str); str = NULL;
1304 case hive_t_expand_string:
1306 str = hivex_value_string (h, values[i]);
1308 if (errno != EILSEQ && errno != EINVAL) {
1309 ret = skip_bad ? 0 : -1;
1312 if (vtor->value_string_invalid_utf16) {
1313 str = hivex_value_value (h, values[i], &t, &len);
1314 if (vtor->value_string_invalid_utf16 (h, opaque, node, values[i], t, len, key, str) == -1)
1316 free (str); str = NULL;
1320 if (vtor->value_string &&
1321 vtor->value_string (h, opaque, node, values[i], t, len, key, str) == -1)
1323 free (str); str = NULL;
1327 case hive_t_dword_be: {
1328 int32_t i32 = hivex_value_dword (h, values[i]);
1329 if (vtor->value_dword &&
1330 vtor->value_dword (h, opaque, node, values[i], t, len, key, i32) == -1)
1335 case hive_t_qword: {
1336 int64_t i64 = hivex_value_qword (h, values[i]);
1337 if (vtor->value_qword &&
1338 vtor->value_qword (h, opaque, node, values[i], t, len, key, i64) == -1)
1344 str = hivex_value_value (h, values[i], &t, &len);
1346 ret = skip_bad ? 0 : -1;
1349 if (t != hive_t_binary) {
1350 ret = skip_bad ? 0 : -1;
1353 if (vtor->value_binary &&
1354 vtor->value_binary (h, opaque, node, values[i], t, len, key, str) == -1)
1356 free (str); str = NULL;
1359 case hive_t_multiple_strings:
1360 strs = hivex_value_multiple_strings (h, values[i]);
1362 if (errno != EILSEQ && errno != EINVAL) {
1363 ret = skip_bad ? 0 : -1;
1366 if (vtor->value_string_invalid_utf16) {
1367 str = hivex_value_value (h, values[i], &t, &len);
1368 if (vtor->value_string_invalid_utf16 (h, opaque, node, values[i], t, len, key, str) == -1)
1370 free (str); str = NULL;
1374 if (vtor->value_multiple_strings &&
1375 vtor->value_multiple_strings (h, opaque, node, values[i], t, len, key, strs) == -1)
1377 free_strings (strs); strs = NULL;
1380 case hive_t_resource_list:
1381 case hive_t_full_resource_description:
1382 case hive_t_resource_requirements_list:
1384 str = hivex_value_value (h, values[i], &t, &len);
1386 ret = skip_bad ? 0 : -1;
1389 if (vtor->value_other &&
1390 vtor->value_other (h, opaque, node, values[i], t, len, key, str) == -1)
1392 free (str); str = NULL;
1396 free (key); key = NULL;
1399 children = hivex_node_children (h, node);
1400 if (children == NULL) {
1401 ret = skip_bad ? 0 : -1;
1405 for (i = 0; children[i] != 0; ++i) {
1407 printf ("hivex__visit_node: %s: visiting subkey %d (%zu)\n",
1408 name, i, children[i]);
1410 if (hivex__visit_node (h, children[i], vtor, unvisited, opaque, flags) == -1)
1414 if (vtor->node_end && vtor->node_end (h, opaque, node, name) == -1)
1425 free_strings (strs);