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.
41 /* Memory-mapped (readonly) registry file. */
44 struct ntreg_header *hdr;
47 /* Use a bitmap to store which file offsets are valid (point to a
48 * used block). We only need to store 1 bit per 32 bits of the file
49 * (because blocks are 4-byte aligned). We found that the average
50 * block size in a registry file is ~50 bytes. So roughly 1 in 12
51 * bits in the bitmap will be set, making it likely a more efficient
52 * structure than a hash table.
55 #define BITMAP_SET(bitmap,off) (bitmap[(off)>>5] |= 1 << (((off)>>2)&7))
56 #define BITMAP_CLR(bitmap,off) (bitmap[(off)>>5] &= ~ (1 << (((off)>>2)&7)))
57 #define BITMAP_TST(bitmap,off) (bitmap[(off)>>5] & (1 << (((off)>>2)&7)))
58 #define IS_VALID_BLOCK(h,off) \
59 (((off) & 3) == 0 && \
61 (off) < (h)->size && \
62 BITMAP_TST((h)->bitmap,(off)))
64 /* Fields from the header, extracted from little-endianness. */
65 size_t rootoffs; /* Root key offset (always an nk-block). */
68 size_t pages; /* Number of hbin pages read. */
69 size_t blocks; /* Total number of blocks found. */
70 size_t used_blocks; /* Total number of used blocks found. */
71 size_t used_size; /* Total size (bytes) of used blocks. */
74 /* NB. All fields are little endian. */
76 char magic[4]; /* "regf" */
79 char last_modified[8];
80 uint32_t unknown3; /* 1 */
81 uint32_t unknown4; /* 3 */
82 uint32_t unknown5; /* 0 */
83 uint32_t unknown6; /* 1 */
84 uint32_t offset; /* offset of root key record - 4KB */
85 uint32_t blocks; /* size in bytes of data (filesize - 4KB) */
86 uint32_t unknown7; /* 1 */
87 char name[0x1fc-0x2c];
88 uint32_t csum; /* checksum: sum of 32 bit words 0-0x1fb. */
89 } __attribute__((__packed__));
91 struct ntreg_hbin_page {
92 char magic[4]; /* "hbin" */
93 uint32_t offset_first; /* offset from 1st block */
94 uint32_t offset_next; /* offset of next (relative to this) */
96 /* Linked list of blocks follows here. */
97 } __attribute__((__packed__));
99 struct ntreg_hbin_block {
100 int32_t seg_len; /* length of this block (-ve for used block) */
101 char id[2]; /* the block type (eg. "nk" for nk record) */
102 /* Block data follows here. */
103 } __attribute__((__packed__));
105 #define BLOCK_ID_EQ(h,offs,eqid) \
106 (strncmp (((struct ntreg_hbin_block *)((h)->addr + (offs)))->id, (eqid), 2) == 0)
109 block_len (hive_h *h, size_t blkoff, int *used)
111 struct ntreg_hbin_block *block;
112 block = (struct ntreg_hbin_block *) (h->addr + blkoff);
114 int32_t len = le32toh (block->seg_len);
125 struct ntreg_nk_record {
126 int32_t seg_len; /* length (always -ve because used) */
127 char id[2]; /* "nk" */
130 uint32_t parent; /* offset of owner/parent */
131 uint32_t nr_subkeys; /* number of subkeys */
133 uint32_t subkey_lf; /* lf record containing list of subkeys */
135 uint32_t nr_values; /* number of values */
136 uint32_t vallist; /* value-list record */
137 uint32_t sk; /* offset of sk-record */
138 uint32_t classname; /* offset of classname record */
141 uint16_t name_len; /* length of name */
142 uint16_t classname_len; /* length of classname */
143 char name[1]; /* name follows here */
144 } __attribute__((__packed__));
146 struct ntreg_lf_record {
148 char id[2]; /* "lf" */
149 uint16_t nr_keys; /* number of keys in this record */
151 uint32_t offset; /* offset of nk-record for this subkey */
152 char name[4]; /* first 4 characters of subkey name */
154 } __attribute__((__packed__));
156 struct ntreg_ri_record {
158 char id[2]; /* "ri" */
159 uint16_t nr_offsets; /* number of pointers to lh records */
160 uint32_t offset[1]; /* list of pointers to lh records */
161 } __attribute__((__packed__));
163 /* This has no ID header. */
164 struct ntreg_value_list {
166 uint32_t offset[1]; /* list of pointers to vk records */
167 } __attribute__((__packed__));
169 struct ntreg_vk_record {
170 int32_t seg_len; /* length (always -ve because used) */
171 char id[2]; /* "vk" */
172 uint16_t name_len; /* length of name */
173 /* length of the data:
174 * If data_len is <= 4, then it's stored inline.
175 * If data_len is 0x80000000, then it's an inline dword.
176 * Top bit may be set or not set at random.
179 uint32_t data_offset; /* pointer to the data (or data if inline) */
180 hive_type data_type; /* type of the data */
181 uint16_t unknown1; /* possibly always 1 */
183 char name[1]; /* key name follows here */
184 } __attribute__((__packed__));
187 hivex_open (const char *filename, int flags)
191 h = calloc (1, sizeof *h);
195 h->msglvl = flags & HIVEX_OPEN_MSGLVL_MASK;
197 const char *debug = getenv ("HIVEX_DEBUG");
198 if (debug && strcmp (debug, "1") == 0)
202 printf ("hivex_open: created handle %p\n", h);
204 h->fd = open (filename, O_RDONLY);
209 if (fstat (h->fd, &statbuf) == -1)
212 h->size = statbuf.st_size;
214 h->addr = mmap (NULL, h->size, PROT_READ, MAP_SHARED, h->fd, 0);
215 if (h->addr == MAP_FAILED)
219 printf ("hivex_open: mapped file at %p\n", h->addr);
222 if (h->hdr->magic[0] != 'r' ||
223 h->hdr->magic[1] != 'e' ||
224 h->hdr->magic[2] != 'g' ||
225 h->hdr->magic[3] != 'f') {
226 fprintf (stderr, "hivex: %s: not a Windows NT Registry hive file\n",
232 h->bitmap = calloc (1 + h->size / 32, 1);
234 #if 0 /* Doesn't work. */
235 /* Header checksum. */
236 uint32_t *daddr = h->addr;
239 for (i = 0; i < 0x1fc / 4; ++i) {
240 sum += le32toh (*daddr);
243 if (sum != le32toh (h->hdr->csum)) {
244 fprintf (stderr, "hivex: %s: bad checksum in hive header\n", filename);
250 h->rootoffs = le32toh (h->hdr->offset) + 0x1000;
253 printf ("hivex_open: root offset = %zu\n", h->rootoffs);
255 /* We'll set this flag when we see a block with the root offset (ie.
258 int seen_root_block = 0, bad_root_block = 0;
260 /* Read the pages and blocks. The aim here is to be robust against
261 * corrupt or malicious registries. So we make sure the loops
262 * always make forward progress. We add the address of each block
263 * we read to a hash table so pointers will only reference the start
267 struct ntreg_hbin_page *page;
268 for (off = 0x1000; off < h->size; off += le32toh (page->offset_next)) {
271 page = (struct ntreg_hbin_page *) (h->addr + off);
272 if (page->magic[0] != 'h' ||
273 page->magic[1] != 'b' ||
274 page->magic[2] != 'i' ||
275 page->magic[3] != 'n') {
276 /* This error is seemingly common in uncorrupt registry files. */
278 fprintf (stderr, "hivex: %s: ignoring trailing garbage at end of file (at %zu, after %zu pages)\n",
279 filename, off, h->pages);
285 printf ("hivex_open: page at %zu\n", off);
287 if (le32toh (page->offset_next) <= sizeof (struct ntreg_hbin_page) ||
288 (le32toh (page->offset_next) & 3) != 0) {
289 fprintf (stderr, "hivex: %s: pagesize %d at %zu, bad registry\n",
290 filename, le32toh (page->offset_next), off);
295 /* Read the blocks in this page. */
297 struct ntreg_hbin_block *block;
299 for (blkoff = off + 0x20;
300 blkoff < off + le32toh (page->offset_next);
304 int is_root = blkoff == h->rootoffs;
308 block = (struct ntreg_hbin_block *) (h->addr + blkoff);
310 seg_len = block_len (h, blkoff, &used);
311 if (seg_len <= 4 || (seg_len & 3) != 0) {
312 fprintf (stderr, "hivex: %s: block size %d at %zu, bad registry\n",
313 filename, le32toh (block->seg_len), blkoff);
319 printf ("hivex_open: %s block id %d,%d at %zu%s\n",
320 used ? "used" : "free", block->id[0], block->id[1], blkoff,
321 is_root ? " (root)" : "");
323 if (is_root && !used)
328 h->used_size += seg_len;
330 /* Root block must be an nk-block. */
331 if (is_root && (block->id[0] != 'n' || block->id[1] != 'k'))
334 /* Note this blkoff is a valid address. */
335 BITMAP_SET (h->bitmap, blkoff);
340 if (!seen_root_block) {
341 fprintf (stderr, "hivex: %s: no root block found\n", filename);
346 if (bad_root_block) {
347 fprintf (stderr, "hivex: %s: bad root block (free or not nk)\n", filename);
353 printf ("hivex_open: successfully read Windows Registry hive file:\n"
356 " blocks used: %zu\n"
357 " bytes used: %zu\n",
358 h->pages, h->blocks, h->used_blocks, h->used_size);
366 if (h->addr && h->size && h->addr != MAP_FAILED)
367 munmap (h->addr, h->size);
377 hivex_close (hive_h *h)
382 munmap (h->addr, h->size);
390 hivex_root (hive_h *h)
392 hive_node_h ret = h->rootoffs;
393 if (!IS_VALID_BLOCK (h, ret)) {
401 hivex_node_name (hive_h *h, hive_node_h node)
403 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
408 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
410 /* AFAIK the node name is always plain ASCII, so no conversion
411 * to UTF-8 is necessary. However we do need to nul-terminate
415 /* nk->name_len is unsigned, 16 bit, so this is safe ... However
416 * we have to make sure the length doesn't exceed the block length.
418 size_t len = le16toh (nk->name_len);
419 size_t seg_len = block_len (h, node, NULL);
420 if (sizeof (struct ntreg_nk_record) + len - 1 > seg_len) {
422 printf ("hivex_node_name: returning EFAULT because node name is too long (%zu, %zu)\n",
428 char *ret = malloc (len + 1);
431 memcpy (ret, nk->name, len);
437 /* I think the documentation for the sk and classname fields in the nk
438 * record is wrong, or else the offset field is in the wrong place.
439 * Otherwise this makes no sense. Disabled this for now -- it's not
440 * useful for reading the registry anyway.
444 hivex_node_security (hive_h *h, hive_node_h node)
446 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
451 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
453 hive_node_h ret = le32toh (nk->sk);
455 if (!IS_VALID_BLOCK (h, ret)) {
463 hivex_node_classname (hive_h *h, hive_node_h node)
465 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
470 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
472 hive_node_h ret = le32toh (nk->classname);
474 if (!IS_VALID_BLOCK (h, ret)) {
483 hivex_node_children (hive_h *h, hive_node_h node)
485 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
490 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
492 size_t nr_subkeys_in_nk = le32toh (nk->nr_subkeys);
494 /* Deal with the common "no subkeys" case quickly. */
496 if (nr_subkeys_in_nk == 0) {
497 ret = malloc (sizeof (hive_node_h));
504 /* Arbitrarily limit the number of subkeys we will ever deal with. */
505 if (nr_subkeys_in_nk > 1000000) {
510 /* The subkey_lf field can point either to an lf-record, which is
511 * the common case, or if there are lots of subkeys, to an
514 size_t subkey_lf = le32toh (nk->subkey_lf);
516 if (!IS_VALID_BLOCK (h, subkey_lf)) {
518 printf ("hivex_node_children: returning EFAULT because subkey_lf is not a valid block (%zu)\n",
524 struct ntreg_hbin_block *block =
525 (struct ntreg_hbin_block *) (h->addr + subkey_lf);
527 /* Points to lf-record? (Note, also "lh" but that is basically the
528 * same as "lf" as far as we are concerned here).
530 if (block->id[0] == 'l' && (block->id[1] == 'f' || block->id[1] == 'h')) {
531 struct ntreg_lf_record *lf = (struct ntreg_lf_record *) block;
533 /* Check number of subkeys in the nk-record matches number of subkeys
536 size_t nr_subkeys_in_lf = le16toh (lf->nr_keys);
539 printf ("hivex_node_children: nr_subkeys_in_nk = %zu, nr_subkeys_in_lf = %zu\n",
540 nr_subkeys_in_nk, nr_subkeys_in_lf);
542 if (nr_subkeys_in_nk != nr_subkeys_in_lf) {
547 size_t len = block_len (h, subkey_lf, NULL);
548 if (8 + nr_subkeys_in_lf * 8 > len) {
550 printf ("hivex_node_children: returning EFAULT because too many subkeys (%zu, %zu)\n",
551 nr_subkeys_in_lf, len);
556 /* Allocate space for the returned values. Note that
557 * nr_subkeys_in_lf is limited to a 16 bit value.
559 ret = malloc ((1 + nr_subkeys_in_lf) * sizeof (hive_node_h));
564 for (i = 0; i < nr_subkeys_in_lf; ++i) {
565 hive_node_h subkey = lf->keys[i].offset;
567 if (!IS_VALID_BLOCK (h, subkey)) {
569 printf ("hivex_node_children: returning EFAULT because subkey is not a valid block (%zu)\n",
580 /* Points to ri-record? */
581 else if (block->id[0] == 'r' && block->id[1] == 'i') {
582 struct ntreg_ri_record *ri = (struct ntreg_ri_record *) block;
584 size_t nr_offsets = le16toh (ri->nr_offsets);
586 /* Count total number of children. */
588 for (i = 0; i < nr_offsets; ++i) {
589 hive_node_h offset = ri->offset[i];
591 if (!IS_VALID_BLOCK (h, offset)) {
593 printf ("hivex_node_children: returning EFAULT because ri-offset is not a valid block (%zu)\n",
598 if (!BLOCK_ID_EQ (h, offset, "lf") && !BLOCK_ID_EQ (h, offset, "lh")) {
603 struct ntreg_lf_record *lf =
604 (struct ntreg_lf_record *) (h->addr + offset);
606 count += le16toh (lf->nr_keys);
610 printf ("hivex_node_children: nr_subkeys_in_nk = %zu, counted = %zu\n",
611 nr_subkeys_in_nk, count);
613 if (nr_subkeys_in_nk != count) {
618 /* Copy list of children. Note nr_subkeys_in_nk is limited to
619 * something reasonable above.
621 ret = malloc ((1 + nr_subkeys_in_nk) * sizeof (hive_node_h));
626 for (i = 0; i < nr_offsets; ++i) {
627 hive_node_h offset = ri->offset[i];
629 if (!IS_VALID_BLOCK (h, offset)) {
631 printf ("hivex_node_children: returning EFAULT because ri-offset is not a valid block (%zu)\n",
636 if (!BLOCK_ID_EQ (h, offset, "lf") && !BLOCK_ID_EQ (h, offset, "lh")) {
641 struct ntreg_lf_record *lf =
642 (struct ntreg_lf_record *) (h->addr + offset);
645 for (j = 0; j < le16toh (lf->nr_keys); ++j) {
646 hive_node_h subkey = lf->keys[j].offset;
648 if (!IS_VALID_BLOCK (h, subkey)) {
650 printf ("hivex_node_children: returning EFAULT because indirect subkey is not a valid block (%zu)\n",
656 ret[count++] = subkey;
669 /* Very inefficient, but at least having a separate API call
670 * allows us to make it more efficient in future.
673 hivex_node_get_child (hive_h *h, hive_node_h node, const char *nname)
675 hive_node_h *children = NULL;
679 children = hivex_node_children (h, node);
680 if (!children) goto error;
683 for (i = 0; children[i] != 0; ++i) {
684 name = hivex_node_name (h, children[i]);
685 if (!name) goto error;
686 if (strcasecmp (name, nname) == 0) {
690 free (name); name = NULL;
700 hivex_node_parent (hive_h *h, hive_node_h node)
702 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
707 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
709 hive_node_h ret = le32toh (nk->parent);
711 printf ("parent = %zu\n", ret);
712 if (!IS_VALID_BLOCK (h, ret)) {
714 printf ("hivex_node_parent: returning EFAULT because parent is not a valid block (%zu)\n",
723 hivex_node_values (hive_h *h, hive_node_h node)
725 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
730 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
732 size_t nr_values = le32toh (nk->nr_values);
735 printf ("hivex_node_values: nr_values = %zu\n", nr_values);
737 /* Deal with the common "no values" case quickly. */
739 if (nr_values == 0) {
740 ret = malloc (sizeof (hive_node_h));
747 /* Arbitrarily limit the number of values we will ever deal with. */
748 if (nr_values > 100000) {
753 /* Get the value list and check it looks reasonable. */
754 size_t vlist_offset = le32toh (nk->vallist);
755 vlist_offset += 0x1000;
756 if (!IS_VALID_BLOCK (h, vlist_offset)) {
758 printf ("hivex_node_values: returning EFAULT because value list is not a valid block (%zu)\n",
764 struct ntreg_value_list *vlist =
765 (struct ntreg_value_list *) (h->addr + vlist_offset);
767 size_t len = block_len (h, vlist_offset, NULL);
768 if (4 + nr_values * 4 > len) {
770 printf ("hivex_node_values: returning EFAULT because value list is too long (%zu, %zu)\n",
776 /* Allocate return array and copy values in. */
777 ret = malloc ((1 + nr_values) * sizeof (hive_node_h));
782 for (i = 0; i < nr_values; ++i) {
783 hive_node_h value = vlist->offset[i];
785 if (!IS_VALID_BLOCK (h, value)) {
787 printf ("hivex_node_values: returning EFAULT because value is not a valid block (%zu)\n",
800 /* Very inefficient, but at least having a separate API call
801 * allows us to make it more efficient in future.
804 hivex_node_get_value (hive_h *h, hive_node_h node, const char *key)
806 hive_value_h *values = NULL;
808 hive_value_h ret = 0;
810 values = hivex_node_values (h, node);
811 if (!values) goto error;
814 for (i = 0; values[i] != 0; ++i) {
815 name = hivex_value_key (h, values[i]);
816 if (!name) goto error;
817 if (strcasecmp (name, key) == 0) {
821 free (name); name = NULL;
831 hivex_value_key (hive_h *h, hive_value_h value)
833 if (!IS_VALID_BLOCK (h, value) || !BLOCK_ID_EQ (h, value, "vk")) {
838 struct ntreg_vk_record *vk = (struct ntreg_vk_record *) (h->addr + value);
840 /* AFAIK the key is always plain ASCII, so no conversion to UTF-8 is
841 * necessary. However we do need to nul-terminate the string.
844 /* vk->name_len is unsigned, 16 bit, so this is safe ... However
845 * we have to make sure the length doesn't exceed the block length.
847 size_t len = le16toh (vk->name_len);
848 size_t seg_len = block_len (h, value, NULL);
849 if (sizeof (struct ntreg_vk_record) + len - 1 > seg_len) {
851 printf ("hivex_value_key: returning EFAULT because key length is too long (%zu, %zu)\n",
857 char *ret = malloc (len + 1);
860 memcpy (ret, vk->name, len);
866 hivex_value_type (hive_h *h, hive_value_h value, hive_type *t, size_t *len)
868 if (!IS_VALID_BLOCK (h, value) || !BLOCK_ID_EQ (h, value, "vk")) {
873 struct ntreg_vk_record *vk = (struct ntreg_vk_record *) (h->addr + value);
876 *t = le32toh (vk->data_type);
879 *len = le32toh (vk->data_len);
880 if (*len == 0x80000000) { /* special case */
882 if (t) *t = hive_t_dword;
891 hivex_value_value (hive_h *h, hive_value_h value,
892 hive_type *t_rtn, size_t *len_rtn)
894 if (!IS_VALID_BLOCK (h, value) || !BLOCK_ID_EQ (h, value, "vk")) {
899 struct ntreg_vk_record *vk = (struct ntreg_vk_record *) (h->addr + value);
904 t = le32toh (vk->data_type);
906 len = le32toh (vk->data_len);
907 if (len == 0x80000000) { /* special case */
914 printf ("hivex_value_value: value=%zu, t=%d, len=%zu\n",
922 /* Arbitrarily limit the length that we will read. */
928 char *ret = malloc (len);
932 /* If length is <= 4 it's always stored inline. */
934 memcpy (ret, (char *) &vk->data_offset, len);
938 size_t data_offset = vk->data_offset;
939 data_offset += 0x1000;
940 if (!IS_VALID_BLOCK (h, data_offset)) {
942 printf ("hivex_value_value: returning EFAULT because data offset is not a valid block (%zu)\n",
949 /* Check that the declared size isn't larger than the block its in. */
950 size_t blen = block_len (h, data_offset, NULL);
953 printf ("hivex_value_value: returning EFAULT because data is longer than its block (%zu, %zu)\n",
960 char *data = h->addr + data_offset + 4;
961 memcpy (ret, data, len);
966 windows_utf16_to_utf8 (/* const */ char *input, size_t len)
968 iconv_t ic = iconv_open ("UTF-8", "UTF-16");
969 if (ic == (iconv_t) -1)
972 /* iconv(3) has an insane interface ... */
974 /* Mostly UTF-8 will be smaller, so this is a good initial guess. */
975 size_t outalloc = len;
979 size_t outlen = outalloc;
980 char *out = malloc (outlen + 1);
990 size_t r = iconv (ic, &inp, &inlen, &outp, &outlen);
991 if (r == (size_t) -1) {
992 if (errno == E2BIG) {
993 /* Try again with a larger output buffer. */
999 /* Else some conversion failure, eg. EILSEQ, EINVAL. */
1015 hivex_value_string (hive_h *h, hive_value_h value)
1019 char *data = hivex_value_value (h, value, &t, &len);
1024 if (t != hive_t_string && t != hive_t_expand_string && t != hive_t_link) {
1030 char *ret = windows_utf16_to_utf8 (data, len);
1039 free_strings (char **argv)
1044 for (i = 0; argv[i] != NULL; ++i)
1050 /* Get the length of a UTF-16 format string. Handle the string as
1051 * pairs of bytes, looking for the first \0\0 pair.
1054 utf16_string_len_in_bytes (const char *str)
1058 while (str[0] || str[1]) {
1066 /* http://blogs.msdn.com/oldnewthing/archive/2009/10/08/9904646.aspx */
1068 hivex_value_multiple_strings (hive_h *h, hive_value_h value)
1072 char *data = hivex_value_value (h, value, &t, &len);
1077 if (t != hive_t_multiple_strings) {
1083 size_t nr_strings = 0;
1084 char **ret = malloc ((1 + nr_strings) * sizeof (char *));
1094 while (p < data + len && (plen = utf16_string_len_in_bytes (p)) > 0) {
1096 char **ret2 = realloc (ret, (1 + nr_strings) * sizeof (char *));
1104 ret[nr_strings-1] = windows_utf16_to_utf8 (p, plen);
1105 ret[nr_strings] = NULL;
1106 if (ret[nr_strings-1] == NULL) {
1112 p += plen + 2 /* skip over UTF-16 \0\0 at the end of this string */;
1120 hivex_value_dword (hive_h *h, hive_value_h value)
1124 char *data = hivex_value_value (h, value, &t, &len);
1129 if ((t != hive_t_dword && t != hive_t_dword_be) || len != 4) {
1135 int32_t ret = *(int32_t*)data;
1137 if (t == hive_t_dword) /* little endian */
1138 ret = le32toh (ret);
1140 ret = be32toh (ret);
1146 hivex_value_qword (hive_h *h, hive_value_h value)
1150 char *data = hivex_value_value (h, value, &t, &len);
1155 if (t != hive_t_qword || len != 8) {
1161 int64_t ret = *(int64_t*)data;
1163 ret = le64toh (ret); /* always little endian */
1169 hivex_visit (hive_h *h, const struct hivex_visitor *visitor, size_t len,
1170 void *opaque, int flags)
1172 return hivex_visit_node (h, hivex_root (h), visitor, len, opaque, flags);
1175 static int hivex__visit_node (hive_h *h, hive_node_h node, const struct hivex_visitor *vtor, char *unvisited, void *opaque, int flags);
1178 hivex_visit_node (hive_h *h, hive_node_h node,
1179 const struct hivex_visitor *visitor, size_t len, void *opaque,
1182 struct hivex_visitor vtor;
1183 memset (&vtor, 0, sizeof vtor);
1185 /* Note that len might be larger *or smaller* than the expected size. */
1186 size_t copysize = len <= sizeof vtor ? len : sizeof vtor;
1187 memcpy (&vtor, visitor, copysize);
1189 /* This bitmap records unvisited nodes, so we don't loop if the
1190 * registry contains cycles.
1192 char *unvisited = malloc (1 + h->size / 32);
1193 if (unvisited == NULL)
1195 memcpy (unvisited, h->bitmap, 1 + h->size / 32);
1197 int r = hivex__visit_node (h, node, &vtor, unvisited, opaque, flags);
1203 hivex__visit_node (hive_h *h, hive_node_h node,
1204 const struct hivex_visitor *vtor, char *unvisited,
1205 void *opaque, int flags)
1207 int skip_bad = flags & HIVEX_VISIT_SKIP_BAD;
1209 hive_value_h *values = NULL;
1210 hive_node_h *children = NULL;
1216 /* Return -1 on all callback errors. However on internal errors,
1217 * check if skip_bad is set and suppress those errors if so.
1221 if (!BITMAP_TST (unvisited, node)) {
1223 printf ("hivex__visit_node: contains cycle: visited node %zu already\n",
1227 return skip_bad ? 0 : -1;
1229 BITMAP_CLR (unvisited, node);
1231 name = hivex_node_name (h, node);
1232 if (!name) return skip_bad ? 0 : -1;
1233 if (vtor->node_start && vtor->node_start (h, opaque, node, name) == -1)
1236 values = hivex_node_values (h, node);
1238 ret = skip_bad ? 0 : -1;
1242 for (i = 0; values[i] != 0; ++i) {
1246 if (hivex_value_type (h, values[i], &t, &len) == -1) {
1247 ret = skip_bad ? 0 : -1;
1251 key = hivex_value_key (h, values[i]);
1253 ret = skip_bad ? 0 : -1;
1259 str = hivex_value_value (h, values[i], &t, &len);
1261 ret = skip_bad ? 0 : -1;
1264 if (t != hive_t_none) {
1265 ret = skip_bad ? 0 : -1;
1268 if (vtor->value_none &&
1269 vtor->value_none (h, opaque, node, values[i], t, len, key, str) == -1)
1271 free (str); str = NULL;
1275 case hive_t_expand_string:
1277 str = hivex_value_string (h, values[i]);
1279 if (errno != EILSEQ && errno != EINVAL) {
1280 ret = skip_bad ? 0 : -1;
1283 if (vtor->value_string_invalid_utf16) {
1284 str = hivex_value_value (h, values[i], &t, &len);
1285 if (vtor->value_string_invalid_utf16 (h, opaque, node, values[i], t, len, key, str) == -1)
1287 free (str); str = NULL;
1291 if (vtor->value_string &&
1292 vtor->value_string (h, opaque, node, values[i], t, len, key, str) == -1)
1294 free (str); str = NULL;
1298 case hive_t_dword_be: {
1299 int32_t i32 = hivex_value_dword (h, values[i]);
1300 if (vtor->value_dword &&
1301 vtor->value_dword (h, opaque, node, values[i], t, len, key, i32) == -1)
1306 case hive_t_qword: {
1307 int64_t i64 = hivex_value_qword (h, values[i]);
1308 if (vtor->value_qword &&
1309 vtor->value_qword (h, opaque, node, values[i], t, len, key, i64) == -1)
1315 str = hivex_value_value (h, values[i], &t, &len);
1317 ret = skip_bad ? 0 : -1;
1320 if (t != hive_t_binary) {
1321 ret = skip_bad ? 0 : -1;
1324 if (vtor->value_binary &&
1325 vtor->value_binary (h, opaque, node, values[i], t, len, key, str) == -1)
1327 free (str); str = NULL;
1330 case hive_t_multiple_strings:
1331 strs = hivex_value_multiple_strings (h, values[i]);
1333 if (errno != EILSEQ && errno != EINVAL) {
1334 ret = skip_bad ? 0 : -1;
1337 if (vtor->value_string_invalid_utf16) {
1338 str = hivex_value_value (h, values[i], &t, &len);
1339 if (vtor->value_string_invalid_utf16 (h, opaque, node, values[i], t, len, key, str) == -1)
1341 free (str); str = NULL;
1345 if (vtor->value_multiple_strings &&
1346 vtor->value_multiple_strings (h, opaque, node, values[i], t, len, key, strs) == -1)
1348 free_strings (strs); strs = NULL;
1351 case hive_t_resource_list:
1352 case hive_t_full_resource_description:
1353 case hive_t_resource_requirements_list:
1355 str = hivex_value_value (h, values[i], &t, &len);
1357 ret = skip_bad ? 0 : -1;
1360 if (vtor->value_other &&
1361 vtor->value_other (h, opaque, node, values[i], t, len, key, str) == -1)
1363 free (str); str = NULL;
1367 free (key); key = NULL;
1370 children = hivex_node_children (h, node);
1371 if (children == NULL) {
1372 ret = skip_bad ? 0 : -1;
1376 for (i = 0; children[i] != 0; ++i) {
1378 printf ("hivex__visit_node: %s: visiting subkey %d (%zu)\n",
1379 name, i, children[i]);
1381 if (hivex__visit_node (h, children[i], vtor, unvisited, opaque, flags) == -1)
1385 if (vtor->node_end && vtor->node_end (h, opaque, node, name) == -1)
1396 free_strings (strs);