1 /* hivex - Windows Registry "hive" extraction library.
2 * Copyright (C) 2009-2011 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.
38 #include "full-read.h"
39 #include "full-write.h"
41 #define STREQ(a,b) (strcmp((a),(b)) == 0)
42 #define STRCASEEQ(a,b) (strcasecmp((a),(b)) == 0)
43 //#define STRNEQ(a,b) (strcmp((a),(b)) != 0)
44 //#define STRCASENEQ(a,b) (strcasecmp((a),(b)) != 0)
45 #define STREQLEN(a,b,n) (strncmp((a),(b),(n)) == 0)
46 //#define STRCASEEQLEN(a,b,n) (strncasecmp((a),(b),(n)) == 0)
47 //#define STRNEQLEN(a,b,n) (strncmp((a),(b),(n)) != 0)
48 //#define STRCASENEQLEN(a,b,n) (strncasecmp((a),(b),(n)) != 0)
49 #define STRPREFIX(a,b) (strncmp((a),(b),strlen((b))) == 0)
52 #include "byte_conversions.h"
54 /* These limits are in place to stop really stupid stuff and/or exploits. */
55 #define HIVEX_MAX_SUBKEYS 15000
56 #define HIVEX_MAX_VALUES 10000
57 #define HIVEX_MAX_VALUE_LEN 1000000
58 #define HIVEX_MAX_ALLOCATION 1000000
60 static char *windows_utf16_to_utf8 (/* const */ char *input, size_t len);
61 static size_t utf16_string_len_in_bytes_max (const char *str, size_t len);
70 /* Registry file, memory mapped if read-only, or malloc'd if writing. */
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 hell. */
94 size_t rootoffs; /* Root key offset (always an nk-block). */
95 size_t endpages; /* Offset of end of pages. */
96 int64_t last_modified; /* mtime of base block. */
99 size_t endblocks; /* Offset to next block allocation (0
100 if not allocated anything yet). */
103 /* NB. All fields are little endian. */
104 struct ntreg_header {
105 char magic[4]; /* "regf" */
108 int64_t last_modified;
109 uint32_t major_ver; /* 1 */
110 uint32_t minor_ver; /* 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; /* pointer AFTER last hbin in file - 4KB */
115 uint32_t unknown7; /* 1 */
117 char name[64]; /* original file name of hive */
118 char unknown_guid1[16];
119 char unknown_guid2[16];
122 char unknown_guid3[16];
127 uint32_t csum; /* checksum: xor of dwords 0-0x1fb. */
129 char unknown11[3528];
131 char unknown_guid4[16];
132 char unknown_guid5[16];
133 char unknown_guid6[16];
137 } __attribute__((__packed__));
139 struct ntreg_hbin_page {
140 char magic[4]; /* "hbin" */
141 uint32_t offset_first; /* offset from 1st block */
142 uint32_t page_size; /* size of this page (multiple of 4KB) */
144 /* Linked list of blocks follows here. */
145 } __attribute__((__packed__));
147 struct ntreg_hbin_block {
148 int32_t seg_len; /* length of this block (-ve for used block) */
149 char id[2]; /* the block type (eg. "nk" for nk record) */
150 /* Block data follows here. */
151 } __attribute__((__packed__));
153 #define BLOCK_ID_EQ(h,offs,eqid) \
154 (STREQLEN (((struct ntreg_hbin_block *)((h)->addr + (offs)))->id, (eqid), 2))
157 block_len (hive_h *h, size_t blkoff, int *used)
159 struct ntreg_hbin_block *block;
160 block = (struct ntreg_hbin_block *) (h->addr + blkoff);
162 int32_t len = le32toh (block->seg_len);
173 struct ntreg_nk_record {
174 int32_t seg_len; /* length (always -ve because used) */
175 char id[2]; /* "nk" */
179 uint32_t parent; /* offset of owner/parent */
180 uint32_t nr_subkeys; /* number of subkeys */
181 uint32_t nr_subkeys_volatile;
182 uint32_t subkey_lf; /* lf record containing list of subkeys */
183 uint32_t subkey_lf_volatile;
184 uint32_t nr_values; /* number of values */
185 uint32_t vallist; /* value-list record */
186 uint32_t sk; /* offset of sk-record */
187 uint32_t classname; /* offset of classname record */
188 uint16_t max_subkey_name_len; /* maximum length of a subkey name in bytes
189 if the subkey was reencoded as UTF-16LE */
192 uint32_t max_vk_name_len; /* maximum length of any vk name in bytes
193 if the name was reencoded as UTF-16LE */
194 uint32_t max_vk_data_len; /* maximum length of any vk data in bytes */
196 uint16_t name_len; /* length of name */
197 uint16_t classname_len; /* length of classname */
198 char name[1]; /* name follows here */
199 } __attribute__((__packed__));
201 struct ntreg_lf_record {
203 char id[2]; /* "lf"|"lh" */
204 uint16_t nr_keys; /* number of keys in this record */
206 uint32_t offset; /* offset of nk-record for this subkey */
207 char hash[4]; /* hash of subkey name */
209 } __attribute__((__packed__));
211 struct ntreg_ri_record {
213 char id[2]; /* "ri" */
214 uint16_t nr_offsets; /* number of pointers to lh records */
215 uint32_t offset[1]; /* list of pointers to lh records */
216 } __attribute__((__packed__));
218 /* This has no ID header. */
219 struct ntreg_value_list {
221 uint32_t offset[1]; /* list of pointers to vk records */
222 } __attribute__((__packed__));
224 struct ntreg_vk_record {
225 int32_t seg_len; /* length (always -ve because used) */
226 char id[2]; /* "vk" */
227 uint16_t name_len; /* length of name */
228 /* length of the data:
229 * If data_len is <= 4, then it's stored inline.
230 * Top bit is set to indicate inline.
233 uint32_t data_offset; /* pointer to the data (or data if inline) */
234 uint32_t data_type; /* type of the data */
235 uint16_t flags; /* bit 0 set => key name ASCII,
236 bit 0 clr => key name UTF-16.
237 Only seen ASCII here in the wild.
238 NB: this is CLEAR for default key. */
240 char name[1]; /* key name follows here */
241 } __attribute__((__packed__));
243 struct ntreg_sk_record {
244 int32_t seg_len; /* length (always -ve because used) */
245 char id[2]; /* "sk" */
247 uint32_t sk_next; /* linked into a circular list */
249 uint32_t refcount; /* reference count */
250 uint32_t sec_len; /* length of security info */
251 char sec_desc[1]; /* security info follows */
252 } __attribute__((__packed__));
255 header_checksum (const hive_h *h)
257 uint32_t *daddr = (uint32_t *) h->addr;
261 for (i = 0; i < 0x1fc / 4; ++i) {
262 sum ^= le32toh (*daddr);
269 #define HIVEX_OPEN_MSGLVL_MASK (HIVEX_OPEN_VERBOSE|HIVEX_OPEN_DEBUG)
272 hivex_open (const char *filename, int flags)
276 assert (sizeof (struct ntreg_header) == 0x1000);
277 assert (offsetof (struct ntreg_header, csum) == 0x1fc);
279 h = calloc (1, sizeof *h);
283 h->msglvl = flags & HIVEX_OPEN_MSGLVL_MASK;
285 const char *debug = getenv ("HIVEX_DEBUG");
286 if (debug && STREQ (debug, "1"))
290 fprintf (stderr, "hivex_open: created handle %p\n", h);
292 h->writable = !!(flags & HIVEX_OPEN_WRITE);
293 h->filename = strdup (filename);
294 if (h->filename == NULL)
298 h->fd = open (filename, O_RDONLY | O_CLOEXEC);
300 h->fd = open (filename, O_RDONLY);
305 fcntl (h->fd, F_SETFD, FD_CLOEXEC);
309 if (fstat (h->fd, &statbuf) == -1)
312 h->size = statbuf.st_size;
315 h->addr = mmap (NULL, h->size, PROT_READ, MAP_SHARED, h->fd, 0);
316 if (h->addr == MAP_FAILED)
320 fprintf (stderr, "hivex_open: mapped file at %p\n", h->addr);
322 h->addr = malloc (h->size);
326 if (full_read (h->fd, h->addr, h->size) < h->size)
329 /* We don't need the file descriptor along this path, since we
330 * have read all the data.
332 if (close (h->fd) == -1)
338 if (h->hdr->magic[0] != 'r' ||
339 h->hdr->magic[1] != 'e' ||
340 h->hdr->magic[2] != 'g' ||
341 h->hdr->magic[3] != 'f') {
342 fprintf (stderr, "hivex: %s: not a Windows NT Registry hive file\n",
348 /* Check major version. */
349 uint32_t major_ver = le32toh (h->hdr->major_ver);
350 if (major_ver != 1) {
352 "hivex: %s: hive file major version %" PRIu32 " (expected 1)\n",
353 filename, major_ver);
358 h->bitmap = calloc (1 + h->size / 32, 1);
359 if (h->bitmap == NULL)
362 /* Header checksum. */
363 uint32_t sum = header_checksum (h);
364 if (sum != le32toh (h->hdr->csum)) {
365 fprintf (stderr, "hivex: %s: bad checksum in hive header\n", filename);
370 /* Last modified time. */
371 h->last_modified = le64toh ((int64_t) h->hdr->last_modified);
373 if (h->msglvl >= 2) {
374 char *name = windows_utf16_to_utf8 (h->hdr->name, 64);
377 "hivex_open: header fields:\n"
378 " file version %" PRIu32 ".%" PRIu32 "\n"
379 " sequence nos %" PRIu32 " %" PRIu32 "\n"
380 " (sequences nos should match if hive was synched at shutdown)\n"
381 " last modified %" PRIu64 "\n"
382 " (Windows filetime, x 100 ns since 1601-01-01)\n"
383 " original file name %s\n"
384 " (only 32 chars are stored, name is probably truncated)\n"
385 " root offset 0x%x + 0x1000\n"
386 " end of last page 0x%x + 0x1000 (total file size 0x%zx)\n"
387 " checksum 0x%x (calculated 0x%x)\n",
388 major_ver, le32toh (h->hdr->minor_ver),
389 le32toh (h->hdr->sequence1), le32toh (h->hdr->sequence2),
391 name ? name : "(conversion failed)",
392 le32toh (h->hdr->offset),
393 le32toh (h->hdr->blocks), h->size,
394 le32toh (h->hdr->csum), sum);
398 h->rootoffs = le32toh (h->hdr->offset) + 0x1000;
399 h->endpages = le32toh (h->hdr->blocks) + 0x1000;
402 fprintf (stderr, "hivex_open: root offset = 0x%zx\n", h->rootoffs);
404 /* We'll set this flag when we see a block with the root offset (ie.
407 int seen_root_block = 0, bad_root_block = 0;
409 /* Collect some stats. */
410 size_t pages = 0; /* Number of hbin pages read. */
411 size_t smallest_page = SIZE_MAX, largest_page = 0;
412 size_t blocks = 0; /* Total number of blocks found. */
413 size_t smallest_block = SIZE_MAX, largest_block = 0, blocks_bytes = 0;
414 size_t used_blocks = 0; /* Total number of used blocks found. */
415 size_t used_size = 0; /* Total size (bytes) of used blocks. */
417 /* Read the pages and blocks. The aim here is to be robust against
418 * corrupt or malicious registries. So we make sure the loops
419 * always make forward progress. We add the address of each block
420 * we read to a hash table so pointers will only reference the start
424 struct ntreg_hbin_page *page;
425 for (off = 0x1000; off < h->size; off += le32toh (page->page_size)) {
426 if (off >= h->endpages)
429 page = (struct ntreg_hbin_page *) (h->addr + off);
430 if (page->magic[0] != 'h' ||
431 page->magic[1] != 'b' ||
432 page->magic[2] != 'i' ||
433 page->magic[3] != 'n') {
434 fprintf (stderr, "hivex: %s: trailing garbage at end of file "
435 "(at 0x%zx, after %zu pages)\n",
436 filename, off, pages);
441 size_t page_size = le32toh (page->page_size);
443 fprintf (stderr, "hivex_open: page at 0x%zx, size %zu\n", off, page_size);
445 if (page_size < smallest_page) smallest_page = page_size;
446 if (page_size > largest_page) largest_page = page_size;
448 if (page_size <= sizeof (struct ntreg_hbin_page) ||
449 (page_size & 0x0fff) != 0) {
450 fprintf (stderr, "hivex: %s: page size %zu at 0x%zx, bad registry\n",
451 filename, page_size, off);
456 /* Read the blocks in this page. */
458 struct ntreg_hbin_block *block;
460 for (blkoff = off + 0x20;
461 blkoff < off + page_size;
465 int is_root = blkoff == h->rootoffs;
469 block = (struct ntreg_hbin_block *) (h->addr + blkoff);
471 seg_len = block_len (h, blkoff, &used);
472 if (seg_len <= 4 || (seg_len & 3) != 0) {
473 fprintf (stderr, "hivex: %s: block size %" PRIu32 " at 0x%zx,"
475 filename, le32toh (block->seg_len), blkoff);
481 fprintf (stderr, "hivex_open: %s block id %d,%d at 0x%zx size %zu%s\n",
482 used ? "used" : "free", block->id[0], block->id[1], blkoff,
483 seg_len, is_root ? " (root)" : "");
485 blocks_bytes += seg_len;
486 if (seg_len < smallest_block) smallest_block = seg_len;
487 if (seg_len > largest_block) largest_block = seg_len;
489 if (is_root && !used)
494 used_size += seg_len;
496 /* Root block must be an nk-block. */
497 if (is_root && (block->id[0] != 'n' || block->id[1] != 'k'))
500 /* Note this blkoff is a valid address. */
501 BITMAP_SET (h->bitmap, blkoff);
506 if (!seen_root_block) {
507 fprintf (stderr, "hivex: %s: no root block found\n", filename);
512 if (bad_root_block) {
513 fprintf (stderr, "hivex: %s: bad root block (free or not nk)\n", filename);
520 "hivex_open: successfully read Windows Registry hive file:\n"
521 " pages: %zu [sml: %zu, lge: %zu]\n"
522 " blocks: %zu [sml: %zu, avg: %zu, lge: %zu]\n"
523 " blocks used: %zu\n"
524 " bytes used: %zu\n",
525 pages, smallest_page, largest_page,
526 blocks, smallest_block, blocks_bytes / blocks, largest_block,
527 used_blocks, used_size);
535 if (h->addr && h->size && h->addr != MAP_FAILED) {
537 munmap (h->addr, h->size);
551 hivex_close (hive_h *h)
556 fprintf (stderr, "hivex_close\n");
560 munmap (h->addr, h->size);
573 /*----------------------------------------------------------------------
578 hivex_root (hive_h *h)
580 hive_node_h ret = h->rootoffs;
581 if (!IS_VALID_BLOCK (h, ret)) {
582 errno = HIVEX_NO_KEY;
589 hivex_node_name (hive_h *h, hive_node_h node)
591 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
596 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
598 /* AFAIK the node name is always plain ASCII, so no conversion
599 * to UTF-8 is necessary. However we do need to nul-terminate
603 /* nk->name_len is unsigned, 16 bit, so this is safe ... However
604 * we have to make sure the length doesn't exceed the block length.
606 size_t len = le16toh (nk->name_len);
607 size_t seg_len = block_len (h, node, NULL);
608 if (sizeof (struct ntreg_nk_record) + len - 1 > seg_len) {
610 fprintf (stderr, "hivex_node_name: returning EFAULT because node name"
611 " is too long (%zu, %zu)\n",
617 char *ret = malloc (len + 1);
620 memcpy (ret, nk->name, len);
626 timestamp_check (hive_h *h, hive_node_h node, int64_t timestamp)
630 fprintf (stderr, "hivex: timestamp_check: "
631 "negative time reported at %zu: %" PRIi64 "\n",
641 hivex_last_modified (hive_h *h)
643 return timestamp_check (h, 0, h->last_modified);
647 hivex_node_timestamp (hive_h *h, hive_node_h node)
651 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
656 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
658 ret = le64toh (nk->timestamp);
659 return timestamp_check (h, node, ret);
663 /* I think the documentation for the sk and classname fields in the nk
664 * record is wrong, or else the offset field is in the wrong place.
665 * Otherwise this makes no sense. Disabled this for now -- it's not
666 * useful for reading the registry anyway.
670 hivex_node_security (hive_h *h, hive_node_h node)
672 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
677 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
679 hive_node_h ret = le32toh (nk->sk);
681 if (!IS_VALID_BLOCK (h, ret)) {
689 hivex_node_classname (hive_h *h, hive_node_h node)
691 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
696 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
698 hive_node_h ret = le32toh (nk->classname);
700 if (!IS_VALID_BLOCK (h, ret)) {
708 /* Structure for returning 0-terminated lists of offsets (nodes,
718 init_offset_list (struct offset_list *list)
722 list->offsets = NULL;
725 #define INIT_OFFSET_LIST(name) \
726 struct offset_list name; \
727 init_offset_list (&name)
729 /* Preallocates the offset_list, but doesn't make the contents longer. */
731 grow_offset_list (struct offset_list *list, size_t alloc)
733 assert (alloc >= list->len);
734 size_t *p = realloc (list->offsets, alloc * sizeof (size_t));
743 add_to_offset_list (struct offset_list *list, size_t offset)
745 if (list->len >= list->alloc) {
746 if (grow_offset_list (list, list->alloc ? list->alloc * 2 : 4) == -1)
749 list->offsets[list->len] = offset;
755 free_offset_list (struct offset_list *list)
757 free (list->offsets);
761 return_offset_list (struct offset_list *list)
763 if (add_to_offset_list (list, 0) == -1)
765 return list->offsets; /* caller frees */
768 /* Iterate over children, returning child nodes and intermediate blocks. */
769 #define GET_CHILDREN_NO_CHECK_NK 1
772 get_children (hive_h *h, hive_node_h node,
773 hive_node_h **children_ret, size_t **blocks_ret,
776 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
781 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
783 size_t nr_subkeys_in_nk = le32toh (nk->nr_subkeys);
785 INIT_OFFSET_LIST (children);
786 INIT_OFFSET_LIST (blocks);
788 /* Deal with the common "no subkeys" case quickly. */
789 if (nr_subkeys_in_nk == 0)
792 /* Arbitrarily limit the number of subkeys we will ever deal with. */
793 if (nr_subkeys_in_nk > HIVEX_MAX_SUBKEYS) {
795 fprintf (stderr, "hivex: get_children: returning ERANGE because "
796 "nr_subkeys_in_nk > HIVEX_MAX_SUBKEYS (%zu > %d)\n",
797 nr_subkeys_in_nk, HIVEX_MAX_SUBKEYS);
802 /* Preallocate space for the children. */
803 if (grow_offset_list (&children, nr_subkeys_in_nk) == -1)
806 /* The subkey_lf field can point either to an lf-record, which is
807 * the common case, or if there are lots of subkeys, to an
810 size_t subkey_lf = le32toh (nk->subkey_lf);
812 if (!IS_VALID_BLOCK (h, subkey_lf)) {
814 fprintf (stderr, "hivex_node_children: returning EFAULT"
815 " because subkey_lf is not a valid block (0x%zx)\n",
821 if (add_to_offset_list (&blocks, subkey_lf) == -1)
824 struct ntreg_hbin_block *block =
825 (struct ntreg_hbin_block *) (h->addr + subkey_lf);
827 /* Points to lf-record? (Note, also "lh" but that is basically the
828 * same as "lf" as far as we are concerned here).
830 if (block->id[0] == 'l' && (block->id[1] == 'f' || block->id[1] == 'h')) {
831 struct ntreg_lf_record *lf = (struct ntreg_lf_record *) block;
833 /* Check number of subkeys in the nk-record matches number of subkeys
836 size_t nr_subkeys_in_lf = le16toh (lf->nr_keys);
839 fprintf (stderr, "hivex_node_children: nr_subkeys_in_nk = %zu,"
840 " nr_subkeys_in_lf = %zu\n",
841 nr_subkeys_in_nk, nr_subkeys_in_lf);
843 if (nr_subkeys_in_nk != nr_subkeys_in_lf) {
848 size_t len = block_len (h, subkey_lf, NULL);
849 if (8 + nr_subkeys_in_lf * 8 > len) {
851 fprintf (stderr, "hivex_node_children: returning EFAULT"
852 " because too many subkeys (%zu, %zu)\n",
853 nr_subkeys_in_lf, len);
859 for (i = 0; i < nr_subkeys_in_lf; ++i) {
860 hive_node_h subkey = le32toh (lf->keys[i].offset);
862 if (!(flags & GET_CHILDREN_NO_CHECK_NK)) {
863 if (!IS_VALID_BLOCK (h, subkey)) {
865 fprintf (stderr, "hivex_node_children: returning EFAULT"
866 " because subkey is not a valid block (0x%zx)\n",
872 if (add_to_offset_list (&children, subkey) == -1)
877 /* Points to ri-record? */
878 else if (block->id[0] == 'r' && block->id[1] == 'i') {
879 struct ntreg_ri_record *ri = (struct ntreg_ri_record *) block;
881 size_t nr_offsets = le16toh (ri->nr_offsets);
883 /* Count total number of children. */
885 for (i = 0; i < nr_offsets; ++i) {
886 hive_node_h offset = le32toh (ri->offset[i]);
888 if (!IS_VALID_BLOCK (h, offset)) {
890 fprintf (stderr, "hivex_node_children: returning EFAULT"
891 " because ri-offset is not a valid block (0x%zx)\n",
896 if (!BLOCK_ID_EQ (h, offset, "lf") && !BLOCK_ID_EQ (h, offset, "lh")) {
898 fprintf (stderr, "get_children: returning ENOTSUP"
899 " because ri-record offset does not point to lf/lh (0x%zx)\n",
905 if (add_to_offset_list (&blocks, offset) == -1)
908 struct ntreg_lf_record *lf =
909 (struct ntreg_lf_record *) (h->addr + offset);
911 count += le16toh (lf->nr_keys);
915 fprintf (stderr, "hivex_node_children: nr_subkeys_in_nk = %zu,"
917 nr_subkeys_in_nk, count);
919 if (nr_subkeys_in_nk != count) {
924 /* Copy list of children. Note nr_subkeys_in_nk is limited to
925 * something reasonable above.
927 for (i = 0; i < nr_offsets; ++i) {
928 hive_node_h offset = le32toh (ri->offset[i]);
930 if (!IS_VALID_BLOCK (h, offset)) {
932 fprintf (stderr, "hivex_node_children: returning EFAULT"
933 " because ri-offset is not a valid block (0x%zx)\n",
938 if (!BLOCK_ID_EQ (h, offset, "lf") && !BLOCK_ID_EQ (h, offset, "lh")) {
940 fprintf (stderr, "get_children: returning ENOTSUP"
941 " because ri-record offset does not point to lf/lh (0x%zx)\n",
947 struct ntreg_lf_record *lf =
948 (struct ntreg_lf_record *) (h->addr + offset);
951 for (j = 0; j < le16toh (lf->nr_keys); ++j) {
952 hive_node_h subkey = le32toh (lf->keys[j].offset);
954 if (!(flags & GET_CHILDREN_NO_CHECK_NK)) {
955 if (!IS_VALID_BLOCK (h, subkey)) {
957 fprintf (stderr, "hivex_node_children: returning EFAULT"
958 " because indirect subkey is not a valid block (0x%zx)\n",
964 if (add_to_offset_list (&children, subkey) == -1)
970 /* else not supported, set errno and fall through */
972 fprintf (stderr, "get_children: returning ENOTSUP"
973 " because subkey block is not lf/lh/ri (0x%zx, %d, %d)\n",
974 subkey_lf, block->id[0], block->id[1]);
977 free_offset_list (&children);
978 free_offset_list (&blocks);
982 *children_ret = return_offset_list (&children);
983 *blocks_ret = return_offset_list (&blocks);
984 if (!*children_ret || !*blocks_ret)
990 hivex_node_children (hive_h *h, hive_node_h node)
992 hive_node_h *children;
995 if (get_children (h, node, &children, &blocks, 0) == -1)
1002 /* Very inefficient, but at least having a separate API call
1003 * allows us to make it more efficient in future.
1006 hivex_node_get_child (hive_h *h, hive_node_h node, const char *nname)
1008 hive_node_h *children = NULL;
1010 hive_node_h ret = 0;
1012 children = hivex_node_children (h, node);
1013 if (!children) goto error;
1016 for (i = 0; children[i] != 0; ++i) {
1017 name = hivex_node_name (h, children[i]);
1018 if (!name) goto error;
1019 if (STRCASEEQ (name, nname)) {
1023 free (name); name = NULL;
1033 hivex_node_parent (hive_h *h, hive_node_h node)
1035 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
1040 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
1042 hive_node_h ret = le32toh (nk->parent);
1044 if (!IS_VALID_BLOCK (h, ret)) {
1046 fprintf (stderr, "hivex_node_parent: returning EFAULT"
1047 " because parent is not a valid block (0x%zx)\n",
1056 get_values (hive_h *h, hive_node_h node,
1057 hive_value_h **values_ret, size_t **blocks_ret)
1059 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
1064 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
1066 size_t nr_values = le32toh (nk->nr_values);
1069 fprintf (stderr, "hivex_node_values: nr_values = %zu\n", nr_values);
1071 INIT_OFFSET_LIST (values);
1072 INIT_OFFSET_LIST (blocks);
1074 /* Deal with the common "no values" case quickly. */
1078 /* Arbitrarily limit the number of values we will ever deal with. */
1079 if (nr_values > HIVEX_MAX_VALUES) {
1081 fprintf (stderr, "hivex: get_values: returning ERANGE"
1082 " because nr_values > HIVEX_MAX_VALUES (%zu > %d)\n",
1083 nr_values, HIVEX_MAX_VALUES);
1088 /* Preallocate space for the values. */
1089 if (grow_offset_list (&values, nr_values) == -1)
1092 /* Get the value list and check it looks reasonable. */
1093 size_t vlist_offset = le32toh (nk->vallist);
1094 vlist_offset += 0x1000;
1095 if (!IS_VALID_BLOCK (h, vlist_offset)) {
1097 fprintf (stderr, "hivex_node_values: returning EFAULT"
1098 " because value list is not a valid block (0x%zx)\n",
1104 if (add_to_offset_list (&blocks, vlist_offset) == -1)
1107 struct ntreg_value_list *vlist =
1108 (struct ntreg_value_list *) (h->addr + vlist_offset);
1110 size_t len = block_len (h, vlist_offset, NULL);
1111 if (4 + nr_values * 4 > len) {
1113 fprintf (stderr, "hivex_node_values: returning EFAULT"
1114 " because value list is too long (%zu, %zu)\n",
1121 for (i = 0; i < nr_values; ++i) {
1122 hive_node_h value = le32toh (vlist->offset[i]);
1124 if (!IS_VALID_BLOCK (h, value)) {
1126 fprintf (stderr, "hivex_node_values: returning EFAULT"
1127 " because value is not a valid block (0x%zx)\n",
1132 if (add_to_offset_list (&values, value) == -1)
1137 *values_ret = return_offset_list (&values);
1138 *blocks_ret = return_offset_list (&blocks);
1139 if (!*values_ret || !*blocks_ret)
1144 free_offset_list (&values);
1145 free_offset_list (&blocks);
1150 hivex_node_values (hive_h *h, hive_node_h node)
1152 hive_value_h *values;
1155 if (get_values (h, node, &values, &blocks) == -1)
1162 /* Very inefficient, but at least having a separate API call
1163 * allows us to make it more efficient in future.
1166 hivex_node_get_value (hive_h *h, hive_node_h node, const char *key)
1168 hive_value_h *values = NULL;
1170 hive_value_h ret = 0;
1172 values = hivex_node_values (h, node);
1173 if (!values) goto error;
1176 for (i = 0; values[i] != 0; ++i) {
1177 name = hivex_value_key (h, values[i]);
1178 if (!name) goto error;
1179 if (STRCASEEQ (name, key)) {
1183 free (name); name = NULL;
1193 hivex_value_key (hive_h *h, hive_value_h value)
1195 if (!IS_VALID_BLOCK (h, value) || !BLOCK_ID_EQ (h, value, "vk")) {
1200 struct ntreg_vk_record *vk = (struct ntreg_vk_record *) (h->addr + value);
1202 /* AFAIK the key is always plain ASCII, so no conversion to UTF-8 is
1203 * necessary. However we do need to nul-terminate the string.
1206 /* vk->name_len is unsigned, 16 bit, so this is safe ... However
1207 * we have to make sure the length doesn't exceed the block length.
1209 size_t len = le16toh (vk->name_len);
1210 size_t seg_len = block_len (h, value, NULL);
1211 if (sizeof (struct ntreg_vk_record) + len - 1 > seg_len) {
1213 fprintf (stderr, "hivex_value_key: returning EFAULT"
1214 " because key length is too long (%zu, %zu)\n",
1220 char *ret = malloc (len + 1);
1223 memcpy (ret, vk->name, len);
1229 hivex_value_type (hive_h *h, hive_value_h value, hive_type *t, size_t *len)
1231 if (!IS_VALID_BLOCK (h, value) || !BLOCK_ID_EQ (h, value, "vk")) {
1236 struct ntreg_vk_record *vk = (struct ntreg_vk_record *) (h->addr + value);
1239 *t = le32toh (vk->data_type);
1242 *len = le32toh (vk->data_len);
1243 *len &= 0x7fffffff; /* top bit indicates if data is stored inline */
1250 hivex_value_value (hive_h *h, hive_value_h value,
1251 hive_type *t_rtn, size_t *len_rtn)
1253 if (!IS_VALID_BLOCK (h, value) || !BLOCK_ID_EQ (h, value, "vk")) {
1258 struct ntreg_vk_record *vk = (struct ntreg_vk_record *) (h->addr + value);
1264 t = le32toh (vk->data_type);
1266 len = le32toh (vk->data_len);
1267 is_inline = !!(len & 0x80000000);
1271 fprintf (stderr, "hivex_value_value: value=0x%zx, t=%d, len=%zu, inline=%d\n",
1272 value, t, len, is_inline);
1279 if (is_inline && len > 4) {
1284 /* Arbitrarily limit the length that we will read. */
1285 if (len > HIVEX_MAX_VALUE_LEN) {
1287 fprintf (stderr, "hivex_value_value: returning ERANGE because data "
1288 "length > HIVEX_MAX_VALUE_LEN (%zu > %d)\n",
1289 len, HIVEX_MAX_SUBKEYS);
1294 char *ret = malloc (len);
1299 memcpy (ret, (char *) &vk->data_offset, len);
1303 size_t data_offset = le32toh (vk->data_offset);
1304 data_offset += 0x1000;
1305 if (!IS_VALID_BLOCK (h, data_offset)) {
1307 fprintf (stderr, "hivex_value_value: returning EFAULT because data "
1308 "offset is not a valid block (0x%zx)\n",
1315 /* Check that the declared size isn't larger than the block its in.
1317 * XXX Some apparently valid registries are seen to have this,
1318 * so turn this into a warning and substitute the smaller length
1321 size_t blen = block_len (h, data_offset, NULL);
1322 if (len > blen - 4 /* subtract 4 for block header */) {
1324 fprintf (stderr, "hivex_value_value: warning: declared data length "
1325 "is longer than the block it is in "
1326 "(data 0x%zx, data len %zu, block len %zu)\n",
1327 data_offset, len, blen);
1330 /* Return the smaller length to the caller too. */
1335 char *data = h->addr + data_offset + 4;
1336 memcpy (ret, data, len);
1341 windows_utf16_to_utf8 (/* const */ char *input, size_t len)
1343 iconv_t ic = iconv_open ("UTF-8", "UTF-16");
1344 if (ic == (iconv_t) -1)
1347 /* iconv(3) has an insane interface ... */
1349 /* Mostly UTF-8 will be smaller, so this is a good initial guess. */
1350 size_t outalloc = len;
1354 size_t outlen = outalloc;
1355 char *out = malloc (outlen + 1);
1365 size_t r = iconv (ic, &inp, &inlen, &outp, &outlen);
1366 if (r == (size_t) -1) {
1367 if (errno == E2BIG) {
1369 size_t prev = outalloc;
1370 /* Try again with a larger output buffer. */
1373 if (outalloc < prev) {
1381 /* Else some conversion failure, eg. EILSEQ, EINVAL. */
1397 hivex_value_string (hive_h *h, hive_value_h value)
1401 char *data = hivex_value_value (h, value, &t, &len);
1406 if (t != hive_t_string && t != hive_t_expand_string && t != hive_t_link) {
1412 /* Deal with the case where Windows has allocated a large buffer
1413 * full of random junk, and only the first few bytes of the buffer
1414 * contain a genuine UTF-16 string.
1416 * In this case, iconv would try to process the junk bytes as UTF-16
1417 * and inevitably find an illegal sequence (EILSEQ). Instead, stop
1418 * after we find the first \0\0.
1420 * (Found by Hilko Bengen in a fresh Windows XP SOFTWARE hive).
1422 size_t slen = utf16_string_len_in_bytes_max (data, len);
1426 char *ret = windows_utf16_to_utf8 (data, len);
1435 free_strings (char **argv)
1440 for (i = 0; argv[i] != NULL; ++i)
1446 /* Get the length of a UTF-16 format string. Handle the string as
1447 * pairs of bytes, looking for the first \0\0 pair. Only read up to
1448 * 'len' maximum bytes.
1451 utf16_string_len_in_bytes_max (const char *str, size_t len)
1455 while (len >= 2 && (str[0] || str[1])) {
1464 /* http://blogs.msdn.com/oldnewthing/archive/2009/10/08/9904646.aspx */
1466 hivex_value_multiple_strings (hive_h *h, hive_value_h value)
1470 char *data = hivex_value_value (h, value, &t, &len);
1475 if (t != hive_t_multiple_strings) {
1481 size_t nr_strings = 0;
1482 char **ret = malloc ((1 + nr_strings) * sizeof (char *));
1492 while (p < data + len &&
1493 (plen = utf16_string_len_in_bytes_max (p, data + len - p)) > 0) {
1495 char **ret2 = realloc (ret, (1 + nr_strings) * sizeof (char *));
1503 ret[nr_strings-1] = windows_utf16_to_utf8 (p, plen);
1504 ret[nr_strings] = NULL;
1505 if (ret[nr_strings-1] == NULL) {
1511 p += plen + 2 /* skip over UTF-16 \0\0 at the end of this string */;
1519 hivex_value_dword (hive_h *h, hive_value_h value)
1523 char *data = hivex_value_value (h, value, &t, &len);
1528 if ((t != hive_t_dword && t != hive_t_dword_be) || len != 4) {
1534 int32_t ret = *(int32_t*)data;
1536 if (t == hive_t_dword) /* little endian */
1537 ret = le32toh (ret);
1539 ret = be32toh (ret);
1545 hivex_value_qword (hive_h *h, hive_value_h value)
1549 char *data = hivex_value_value (h, value, &t, &len);
1554 if (t != hive_t_qword || len != 8) {
1560 int64_t ret = *(int64_t*)data;
1562 ret = le64toh (ret); /* always little endian */
1567 /*----------------------------------------------------------------------
1572 hivex_visit (hive_h *h, const struct hivex_visitor *visitor, size_t len,
1573 void *opaque, int flags)
1575 return hivex_visit_node (h, hivex_root (h), visitor, len, opaque, flags);
1578 static int hivex__visit_node (hive_h *h, hive_node_h node,
1579 const struct hivex_visitor *vtor,
1580 char *unvisited, void *opaque, int flags);
1583 hivex_visit_node (hive_h *h, hive_node_h node,
1584 const struct hivex_visitor *visitor, size_t len, void *opaque,
1587 struct hivex_visitor vtor;
1588 memset (&vtor, 0, sizeof vtor);
1590 /* Note that len might be larger *or smaller* than the expected size. */
1591 size_t copysize = len <= sizeof vtor ? len : sizeof vtor;
1592 memcpy (&vtor, visitor, copysize);
1594 /* This bitmap records unvisited nodes, so we don't loop if the
1595 * registry contains cycles.
1597 char *unvisited = malloc (1 + h->size / 32);
1598 if (unvisited == NULL)
1600 memcpy (unvisited, h->bitmap, 1 + h->size / 32);
1602 int r = hivex__visit_node (h, node, &vtor, unvisited, opaque, flags);
1608 hivex__visit_node (hive_h *h, hive_node_h node,
1609 const struct hivex_visitor *vtor, char *unvisited,
1610 void *opaque, int flags)
1612 int skip_bad = flags & HIVEX_VISIT_SKIP_BAD;
1614 hive_value_h *values = NULL;
1615 hive_node_h *children = NULL;
1621 /* Return -1 on all callback errors. However on internal errors,
1622 * check if skip_bad is set and suppress those errors if so.
1626 if (!BITMAP_TST (unvisited, node)) {
1628 fprintf (stderr, "hivex__visit_node: contains cycle:"
1629 " visited node 0x%zx already\n",
1633 return skip_bad ? 0 : -1;
1635 BITMAP_CLR (unvisited, node);
1637 name = hivex_node_name (h, node);
1638 if (!name) return skip_bad ? 0 : -1;
1639 if (vtor->node_start && vtor->node_start (h, opaque, node, name) == -1)
1642 values = hivex_node_values (h, node);
1644 ret = skip_bad ? 0 : -1;
1648 for (i = 0; values[i] != 0; ++i) {
1652 if (hivex_value_type (h, values[i], &t, &len) == -1) {
1653 ret = skip_bad ? 0 : -1;
1657 key = hivex_value_key (h, values[i]);
1659 ret = skip_bad ? 0 : -1;
1663 if (vtor->value_any) {
1664 str = hivex_value_value (h, values[i], &t, &len);
1666 ret = skip_bad ? 0 : -1;
1669 if (vtor->value_any (h, opaque, node, values[i], t, len, key, str) == -1)
1671 free (str); str = NULL;
1676 str = hivex_value_value (h, values[i], &t, &len);
1678 ret = skip_bad ? 0 : -1;
1681 if (t != hive_t_none) {
1682 ret = skip_bad ? 0 : -1;
1685 if (vtor->value_none &&
1686 vtor->value_none (h, opaque, node, values[i], t, len, key, str) == -1)
1688 free (str); str = NULL;
1692 case hive_t_expand_string:
1694 str = hivex_value_string (h, values[i]);
1696 if (errno != EILSEQ && errno != EINVAL) {
1697 ret = skip_bad ? 0 : -1;
1700 if (vtor->value_string_invalid_utf16) {
1701 str = hivex_value_value (h, values[i], &t, &len);
1702 if (vtor->value_string_invalid_utf16 (h, opaque, node, values[i],
1703 t, len, key, str) == -1)
1705 free (str); str = NULL;
1709 if (vtor->value_string &&
1710 vtor->value_string (h, opaque, node, values[i],
1711 t, len, key, str) == -1)
1713 free (str); str = NULL;
1717 case hive_t_dword_be: {
1718 int32_t i32 = hivex_value_dword (h, values[i]);
1719 if (vtor->value_dword &&
1720 vtor->value_dword (h, opaque, node, values[i],
1721 t, len, key, i32) == -1)
1726 case hive_t_qword: {
1727 int64_t i64 = hivex_value_qword (h, values[i]);
1728 if (vtor->value_qword &&
1729 vtor->value_qword (h, opaque, node, values[i],
1730 t, len, key, i64) == -1)
1736 str = hivex_value_value (h, values[i], &t, &len);
1738 ret = skip_bad ? 0 : -1;
1741 if (t != hive_t_binary) {
1742 ret = skip_bad ? 0 : -1;
1745 if (vtor->value_binary &&
1746 vtor->value_binary (h, opaque, node, values[i],
1747 t, len, key, str) == -1)
1749 free (str); str = NULL;
1752 case hive_t_multiple_strings:
1753 strs = hivex_value_multiple_strings (h, values[i]);
1755 if (errno != EILSEQ && errno != EINVAL) {
1756 ret = skip_bad ? 0 : -1;
1759 if (vtor->value_string_invalid_utf16) {
1760 str = hivex_value_value (h, values[i], &t, &len);
1761 if (vtor->value_string_invalid_utf16 (h, opaque, node, values[i],
1762 t, len, key, str) == -1)
1764 free (str); str = NULL;
1768 if (vtor->value_multiple_strings &&
1769 vtor->value_multiple_strings (h, opaque, node, values[i],
1770 t, len, key, strs) == -1)
1772 free_strings (strs); strs = NULL;
1775 case hive_t_resource_list:
1776 case hive_t_full_resource_description:
1777 case hive_t_resource_requirements_list:
1779 str = hivex_value_value (h, values[i], &t, &len);
1781 ret = skip_bad ? 0 : -1;
1784 if (vtor->value_other &&
1785 vtor->value_other (h, opaque, node, values[i],
1786 t, len, key, str) == -1)
1788 free (str); str = NULL;
1793 free (key); key = NULL;
1796 children = hivex_node_children (h, node);
1797 if (children == NULL) {
1798 ret = skip_bad ? 0 : -1;
1802 for (i = 0; children[i] != 0; ++i) {
1804 fprintf (stderr, "hivex__visit_node: %s: visiting subkey %d (0x%zx)\n",
1805 name, i, children[i]);
1807 if (hivex__visit_node (h, children[i], vtor, unvisited, opaque, flags) == -1)
1811 if (vtor->node_end && vtor->node_end (h, opaque, node, name) == -1)
1822 free_strings (strs);
1826 /*----------------------------------------------------------------------
1830 /* Allocate an hbin (page), extending the malloc'd space if necessary,
1831 * and updating the hive handle fields (but NOT the hive disk header
1832 * -- the hive disk header is updated when we commit). This function
1833 * also extends the bitmap if necessary.
1835 * 'allocation_hint' is the size of the block allocation we would like
1836 * to make. Normally registry blocks are very small (avg 50 bytes)
1837 * and are contained in standard-sized pages (4KB), but the registry
1838 * can support blocks which are larger than a standard page, in which
1839 * case it creates a page of 8KB, 12KB etc.
1842 * > 0 : offset of first usable byte of new page (after page header)
1843 * 0 : error (errno set)
1846 allocate_page (hive_h *h, size_t allocation_hint)
1848 /* In almost all cases this will be 1. */
1849 size_t nr_4k_pages =
1850 1 + (allocation_hint + sizeof (struct ntreg_hbin_page) - 1) / 4096;
1851 assert (nr_4k_pages >= 1);
1853 /* 'extend' is the number of bytes to extend the file by. Note that
1854 * hives found in the wild often contain slack between 'endpages'
1855 * and the actual end of the file, so we don't always need to make
1858 ssize_t extend = h->endpages + nr_4k_pages * 4096 - h->size;
1860 if (h->msglvl >= 2) {
1861 fprintf (stderr, "allocate_page: current endpages = 0x%zx,"
1862 " current size = 0x%zx\n",
1863 h->endpages, h->size);
1864 fprintf (stderr, "allocate_page: extending file by %zd bytes"
1865 " (<= 0 if no extension)\n",
1870 size_t oldsize = h->size;
1871 size_t newsize = h->size + extend;
1872 char *newaddr = realloc (h->addr, newsize);
1873 if (newaddr == NULL)
1876 size_t oldbitmapsize = 1 + oldsize / 32;
1877 size_t newbitmapsize = 1 + newsize / 32;
1878 char *newbitmap = realloc (h->bitmap, newbitmapsize);
1879 if (newbitmap == NULL) {
1886 h->bitmap = newbitmap;
1888 memset (h->addr + oldsize, 0, newsize - oldsize);
1889 memset (h->bitmap + oldbitmapsize, 0, newbitmapsize - oldbitmapsize);
1892 size_t offset = h->endpages;
1893 h->endpages += nr_4k_pages * 4096;
1896 fprintf (stderr, "allocate_page: new endpages = 0x%zx, new size = 0x%zx\n",
1897 h->endpages, h->size);
1899 /* Write the hbin header. */
1900 struct ntreg_hbin_page *page =
1901 (struct ntreg_hbin_page *) (h->addr + offset);
1902 page->magic[0] = 'h';
1903 page->magic[1] = 'b';
1904 page->magic[2] = 'i';
1905 page->magic[3] = 'n';
1906 page->offset_first = htole32 (offset - 0x1000);
1907 page->page_size = htole32 (nr_4k_pages * 4096);
1908 memset (page->unknown, 0, sizeof (page->unknown));
1911 fprintf (stderr, "allocate_page: new page at 0x%zx\n", offset);
1913 /* Offset of first usable byte after the header. */
1914 return offset + sizeof (struct ntreg_hbin_page);
1917 /* Allocate a single block, first allocating an hbin (page) at the end
1918 * of the current file if necessary. NB. To keep the implementation
1919 * simple and more likely to be correct, we do not reuse existing free
1922 * seg_len is the size of the block (this INCLUDES the block header).
1923 * The header of the block is initialized to -seg_len (negative to
1924 * indicate used). id[2] is the block ID (type), eg. "nk" for nk-
1925 * record. The block bitmap is updated to show this block as valid.
1926 * The rest of the contents of the block will be zero.
1928 * **NB** Because allocate_block may reallocate the memory, all
1929 * pointers into the memory become potentially invalid. I really
1930 * love writing in C, can't you tell?
1933 * > 0 : offset of new block
1934 * 0 : error (errno set)
1937 allocate_block (hive_h *h, size_t seg_len, const char id[2])
1945 /* The caller probably forgot to include the header. Note that
1946 * value lists have no ID field, so seg_len == 4 would be possible
1947 * for them, albeit unusual.
1950 fprintf (stderr, "allocate_block: refusing too small allocation (%zu),"
1951 " returning ERANGE\n", seg_len);
1956 /* Refuse really large allocations. */
1957 if (seg_len > HIVEX_MAX_ALLOCATION) {
1959 fprintf (stderr, "allocate_block: refusing large allocation (%zu),"
1960 " returning ERANGE\n", seg_len);
1965 /* Round up allocation to multiple of 8 bytes. All blocks must be
1966 * on an 8 byte boundary.
1968 seg_len = (seg_len + 7) & ~7;
1970 /* Allocate a new page if necessary. */
1971 if (h->endblocks == 0 || h->endblocks + seg_len > h->endpages) {
1972 size_t newendblocks = allocate_page (h, seg_len);
1973 if (newendblocks == 0)
1975 h->endblocks = newendblocks;
1978 size_t offset = h->endblocks;
1981 fprintf (stderr, "allocate_block: new block at 0x%zx, size %zu\n",
1984 struct ntreg_hbin_block *blockhdr =
1985 (struct ntreg_hbin_block *) (h->addr + offset);
1987 memset (blockhdr, 0, seg_len);
1989 blockhdr->seg_len = htole32 (- (int32_t) seg_len);
1990 if (id[0] && id[1] && seg_len >= sizeof (struct ntreg_hbin_block)) {
1991 blockhdr->id[0] = id[0];
1992 blockhdr->id[1] = id[1];
1995 BITMAP_SET (h->bitmap, offset);
1997 h->endblocks += seg_len;
1999 /* If there is space after the last block in the last page, then we
2000 * have to put a dummy free block header here to mark the rest of
2003 ssize_t rem = h->endpages - h->endblocks;
2006 fprintf (stderr, "allocate_block: marking remainder of page free"
2007 " starting at 0x%zx, size %zd\n", h->endblocks, rem);
2011 blockhdr = (struct ntreg_hbin_block *) (h->addr + h->endblocks);
2012 blockhdr->seg_len = htole32 ((int32_t) rem);
2018 /* 'offset' must point to a valid, used block. This function marks
2019 * the block unused (by updating the seg_len field) and invalidates
2020 * the bitmap. It does NOT do this recursively, so to avoid creating
2021 * unreachable used blocks, callers may have to recurse over the hive
2022 * structures. Also callers must ensure there are no references to
2023 * this block from other parts of the hive.
2026 mark_block_unused (hive_h *h, size_t offset)
2028 assert (h->writable);
2029 assert (IS_VALID_BLOCK (h, offset));
2032 fprintf (stderr, "mark_block_unused: marking 0x%zx unused\n", offset);
2034 struct ntreg_hbin_block *blockhdr =
2035 (struct ntreg_hbin_block *) (h->addr + offset);
2037 size_t seg_len = block_len (h, offset, NULL);
2038 blockhdr->seg_len = htole32 (seg_len);
2040 BITMAP_CLR (h->bitmap, offset);
2043 /* Delete all existing values at this node. */
2045 delete_values (hive_h *h, hive_node_h node)
2047 assert (h->writable);
2049 hive_value_h *values;
2051 if (get_values (h, node, &values, &blocks) == -1)
2055 for (i = 0; blocks[i] != 0; ++i)
2056 mark_block_unused (h, blocks[i]);
2060 for (i = 0; values[i] != 0; ++i) {
2061 struct ntreg_vk_record *vk =
2062 (struct ntreg_vk_record *) (h->addr + values[i]);
2066 len = le32toh (vk->data_len);
2067 is_inline = !!(len & 0x80000000); /* top bit indicates is inline */
2070 if (!is_inline) { /* non-inline, so remove data block */
2071 size_t data_offset = le32toh (vk->data_offset);
2072 data_offset += 0x1000;
2073 mark_block_unused (h, data_offset);
2076 /* remove vk record */
2077 mark_block_unused (h, values[i]);
2082 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
2083 nk->nr_values = htole32 (0);
2084 nk->vallist = htole32 (0xffffffff);
2090 hivex_commit (hive_h *h, const char *filename, int flags)
2102 filename = filename ? : h->filename;
2103 int fd = open (filename, O_WRONLY|O_CREAT|O_TRUNC|O_NOCTTY, 0666);
2107 /* Update the header fields. */
2108 uint32_t sequence = le32toh (h->hdr->sequence1);
2110 h->hdr->sequence1 = htole32 (sequence);
2111 h->hdr->sequence2 = htole32 (sequence);
2112 /* XXX Ought to update h->hdr->last_modified. */
2113 h->hdr->blocks = htole32 (h->endpages - 0x1000);
2115 /* Recompute header checksum. */
2116 uint32_t sum = header_checksum (h);
2117 h->hdr->csum = htole32 (sum);
2120 fprintf (stderr, "hivex_commit: new header checksum: 0x%x\n", sum);
2122 if (full_write (fd, h->addr, h->size) != h->size) {
2129 if (close (fd) == -1)
2135 /* Calculate the hash for a lf or lh record offset.
2138 calc_hash (const char *type, const char *name, char *ret)
2140 size_t len = strlen (name);
2142 if (STRPREFIX (type, "lf"))
2143 /* Old-style, not used in current registries. */
2144 memcpy (ret, name, len < 4 ? len : 4);
2146 /* New-style for lh-records. */
2149 for (i = 0; i < len; ++i) {
2150 c = c_toupper (name[i]);
2154 *((uint32_t *) ret) = htole32 (h);
2158 /* Create a completely new lh-record containing just the single node. */
2160 new_lh_record (hive_h *h, const char *name, hive_node_h node)
2162 static const char id[2] = { 'l', 'h' };
2163 size_t seg_len = sizeof (struct ntreg_lf_record);
2164 size_t offset = allocate_block (h, seg_len, id);
2168 struct ntreg_lf_record *lh = (struct ntreg_lf_record *) (h->addr + offset);
2169 lh->nr_keys = htole16 (1);
2170 lh->keys[0].offset = htole32 (node - 0x1000);
2171 calc_hash ("lh", name, lh->keys[0].hash);
2176 /* Insert node into existing lf/lh-record at position.
2177 * This allocates a new record and marks the old one as unused.
2180 insert_lf_record (hive_h *h, size_t old_offs, size_t posn,
2181 const char *name, hive_node_h node)
2183 assert (IS_VALID_BLOCK (h, old_offs));
2185 /* Work around C stupidity.
2186 * http://www.redhat.com/archives/libguestfs/2010-February/msg00056.html
2188 int test = BLOCK_ID_EQ (h, old_offs, "lf") || BLOCK_ID_EQ (h, old_offs, "lh");
2191 struct ntreg_lf_record *old_lf =
2192 (struct ntreg_lf_record *) (h->addr + old_offs);
2193 size_t nr_keys = le16toh (old_lf->nr_keys);
2195 nr_keys++; /* in new record ... */
2197 size_t seg_len = sizeof (struct ntreg_lf_record) + (nr_keys-1) * 8;
2199 /* Copy the old_lf->id in case it moves during allocate_block. */
2201 memcpy (id, old_lf->id, sizeof id);
2203 size_t new_offs = allocate_block (h, seg_len, id);
2207 /* old_lf could have been invalidated by allocate_block. */
2208 old_lf = (struct ntreg_lf_record *) (h->addr + old_offs);
2210 struct ntreg_lf_record *new_lf =
2211 (struct ntreg_lf_record *) (h->addr + new_offs);
2212 new_lf->nr_keys = htole16 (nr_keys);
2214 /* Copy the keys until we reach posn, insert the new key there, then
2215 * copy the remaining keys.
2218 for (i = 0; i < posn; ++i)
2219 new_lf->keys[i] = old_lf->keys[i];
2221 new_lf->keys[i].offset = htole32 (node - 0x1000);
2222 calc_hash (new_lf->id, name, new_lf->keys[i].hash);
2224 for (i = posn+1; i < nr_keys; ++i)
2225 new_lf->keys[i] = old_lf->keys[i-1];
2227 /* Old block is unused, return new block. */
2228 mark_block_unused (h, old_offs);
2232 /* Compare name with name in nk-record. */
2234 compare_name_with_nk_name (hive_h *h, const char *name, hive_node_h nk_offs)
2236 assert (IS_VALID_BLOCK (h, nk_offs));
2237 assert (BLOCK_ID_EQ (h, nk_offs, "nk"));
2239 /* Name in nk is not necessarily nul-terminated. */
2240 char *nname = hivex_node_name (h, nk_offs);
2242 /* Unfortunately we don't have a way to return errors here. */
2244 perror ("compare_name_with_nk_name");
2248 int r = strcasecmp (name, nname);
2255 hivex_node_add_child (hive_h *h, hive_node_h parent, const char *name)
2262 if (!IS_VALID_BLOCK (h, parent) || !BLOCK_ID_EQ (h, parent, "nk")) {
2267 if (name == NULL || strlen (name) == 0) {
2272 if (hivex_node_get_child (h, parent, name) != 0) {
2277 /* Create the new nk-record. */
2278 static const char nk_id[2] = { 'n', 'k' };
2279 size_t seg_len = sizeof (struct ntreg_nk_record) + strlen (name);
2280 hive_node_h node = allocate_block (h, seg_len, nk_id);
2285 fprintf (stderr, "hivex_node_add_child: allocated new nk-record"
2286 " for child at 0x%zx\n", node);
2288 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
2289 nk->flags = htole16 (0x0020); /* key is ASCII. */
2290 nk->parent = htole32 (parent - 0x1000);
2291 nk->subkey_lf = htole32 (0xffffffff);
2292 nk->subkey_lf_volatile = htole32 (0xffffffff);
2293 nk->vallist = htole32 (0xffffffff);
2294 nk->classname = htole32 (0xffffffff);
2295 nk->name_len = htole16 (strlen (name));
2296 strcpy (nk->name, name);
2298 /* Inherit parent sk. */
2299 struct ntreg_nk_record *parent_nk =
2300 (struct ntreg_nk_record *) (h->addr + parent);
2301 size_t parent_sk_offset = le32toh (parent_nk->sk);
2302 parent_sk_offset += 0x1000;
2303 if (!IS_VALID_BLOCK (h, parent_sk_offset) ||
2304 !BLOCK_ID_EQ (h, parent_sk_offset, "sk")) {
2306 fprintf (stderr, "hivex_node_add_child: returning EFAULT"
2307 " because parent sk is not a valid block (%zu)\n",
2312 struct ntreg_sk_record *sk =
2313 (struct ntreg_sk_record *) (h->addr + parent_sk_offset);
2314 sk->refcount = htole32 (le32toh (sk->refcount) + 1);
2315 nk->sk = htole32 (parent_sk_offset - 0x1000);
2317 /* Inherit parent timestamp. */
2318 nk->timestamp = parent_nk->timestamp;
2320 /* What I found out the hard way (not documented anywhere): the
2321 * subkeys in lh-records must be kept sorted. If you just add a
2322 * subkey in a non-sorted position (eg. just add it at the end) then
2323 * Windows won't see the subkey _and_ Windows will corrupt the hive
2324 * itself when it modifies or saves it.
2326 * So use get_children() to get a list of intermediate
2327 * lf/lh-records. get_children() returns these in reading order
2328 * (which is sorted), so we look for the lf/lh-records in sequence
2329 * until we find the key name just after the one we are inserting,
2330 * and we insert the subkey just before it.
2332 * The only other case is the no-subkeys case, where we have to
2333 * create a brand new lh-record.
2335 hive_node_h *unused;
2338 if (get_children (h, parent, &unused, &blocks, 0) == -1)
2343 size_t nr_subkeys_in_parent_nk = le32toh (parent_nk->nr_subkeys);
2344 if (nr_subkeys_in_parent_nk == 0) { /* No subkeys case. */
2345 /* Free up any existing intermediate blocks. */
2346 for (i = 0; blocks[i] != 0; ++i)
2347 mark_block_unused (h, blocks[i]);
2348 size_t lh_offs = new_lh_record (h, name, node);
2354 /* Recalculate pointers that could have been invalidated by
2355 * previous call to allocate_block (via new_lh_record).
2357 nk = (struct ntreg_nk_record *) (h->addr + node);
2358 parent_nk = (struct ntreg_nk_record *) (h->addr + parent);
2361 fprintf (stderr, "hivex_node_add_child: no keys, allocated new"
2362 " lh-record at 0x%zx\n", lh_offs);
2364 parent_nk->subkey_lf = htole32 (lh_offs - 0x1000);
2366 else { /* Insert subkeys case. */
2367 size_t old_offs = 0, new_offs = 0;
2368 struct ntreg_lf_record *old_lf = NULL;
2370 /* Find lf/lh key name just after the one we are inserting. */
2371 for (i = 0; blocks[i] != 0; ++i) {
2372 if (BLOCK_ID_EQ (h, blocks[i], "lf") ||
2373 BLOCK_ID_EQ (h, blocks[i], "lh")) {
2374 old_offs = blocks[i];
2375 old_lf = (struct ntreg_lf_record *) (h->addr + old_offs);
2376 for (j = 0; j < le16toh (old_lf->nr_keys); ++j) {
2377 hive_node_h nk_offs = le32toh (old_lf->keys[j].offset);
2379 if (compare_name_with_nk_name (h, name, nk_offs) < 0)
2385 /* Insert it at the end.
2386 * old_offs points to the last lf record, set j.
2388 assert (old_offs != 0); /* should never happen if nr_subkeys > 0 */
2389 j = le16toh (old_lf->nr_keys);
2394 fprintf (stderr, "hivex_node_add_child: insert key in existing"
2395 " lh-record at 0x%zx, posn %zu\n", old_offs, j);
2397 new_offs = insert_lf_record (h, old_offs, j, name, node);
2398 if (new_offs == 0) {
2403 /* Recalculate pointers that could have been invalidated by
2404 * previous call to allocate_block (via insert_lf_record).
2406 nk = (struct ntreg_nk_record *) (h->addr + node);
2407 parent_nk = (struct ntreg_nk_record *) (h->addr + parent);
2410 fprintf (stderr, "hivex_node_add_child: new lh-record at 0x%zx\n",
2413 /* If the lf/lh-record was directly referenced by the parent nk,
2414 * then update the parent nk.
2416 if (le32toh (parent_nk->subkey_lf) + 0x1000 == old_offs)
2417 parent_nk->subkey_lf = htole32 (new_offs - 0x1000);
2418 /* Else we have to look for the intermediate ri-record and update
2422 for (i = 0; blocks[i] != 0; ++i) {
2423 if (BLOCK_ID_EQ (h, blocks[i], "ri")) {
2424 struct ntreg_ri_record *ri =
2425 (struct ntreg_ri_record *) (h->addr + blocks[i]);
2426 for (j = 0; j < le16toh (ri->nr_offsets); ++j)
2427 if (le32toh (ri->offset[j] + 0x1000) == old_offs) {
2428 ri->offset[j] = htole32 (new_offs - 0x1000);
2434 /* Not found .. This is an internal error. */
2436 fprintf (stderr, "hivex_node_add_child: returning ENOTSUP"
2437 " because could not find ri->lf link\n");
2449 /* Update nr_subkeys in parent nk. */
2450 nr_subkeys_in_parent_nk++;
2451 parent_nk->nr_subkeys = htole32 (nr_subkeys_in_parent_nk);
2453 /* Update max_subkey_name_len in parent nk. */
2454 uint16_t max = le16toh (parent_nk->max_subkey_name_len);
2455 if (max < strlen (name) * 2) /* *2 because "recoded" in UTF16-LE. */
2456 parent_nk->max_subkey_name_len = htole16 (strlen (name) * 2);
2461 /* Decrement the refcount of an sk-record, and if it reaches zero,
2462 * unlink it from the chain and delete it.
2465 delete_sk (hive_h *h, size_t sk_offset)
2467 if (!IS_VALID_BLOCK (h, sk_offset) || !BLOCK_ID_EQ (h, sk_offset, "sk")) {
2469 fprintf (stderr, "delete_sk: not an sk record: 0x%zx\n", sk_offset);
2474 struct ntreg_sk_record *sk = (struct ntreg_sk_record *) (h->addr + sk_offset);
2476 if (sk->refcount == 0) {
2478 fprintf (stderr, "delete_sk: sk record already has refcount 0: 0x%zx\n",
2486 if (sk->refcount == 0) {
2487 size_t sk_prev_offset = sk->sk_prev;
2488 sk_prev_offset += 0x1000;
2490 size_t sk_next_offset = sk->sk_next;
2491 sk_next_offset += 0x1000;
2493 /* Update sk_prev/sk_next SKs, unless they both point back to this
2494 * cell in which case we are deleting the last SK.
2496 if (sk_prev_offset != sk_offset && sk_next_offset != sk_offset) {
2497 struct ntreg_sk_record *sk_prev =
2498 (struct ntreg_sk_record *) (h->addr + sk_prev_offset);
2499 struct ntreg_sk_record *sk_next =
2500 (struct ntreg_sk_record *) (h->addr + sk_next_offset);
2502 sk_prev->sk_next = htole32 (sk_next_offset - 0x1000);
2503 sk_next->sk_prev = htole32 (sk_prev_offset - 0x1000);
2506 /* Refcount is zero so really delete this block. */
2507 mark_block_unused (h, sk_offset);
2513 /* Callback from hivex_node_delete_child which is called to delete a
2514 * node AFTER its subnodes have been visited. The subnodes have been
2515 * deleted but we still have to delete any lf/lh/li/ri records and the
2516 * value list block and values, followed by deleting the node itself.
2519 delete_node (hive_h *h, void *opaque, hive_node_h node, const char *name)
2521 /* Get the intermediate blocks. The subkeys have already been
2522 * deleted by this point, so tell get_children() not to check for
2523 * validity of the nk-records.
2525 hive_node_h *unused;
2527 if (get_children (h, node, &unused, &blocks, GET_CHILDREN_NO_CHECK_NK) == -1)
2531 /* We don't care what's in these intermediate blocks, so we can just
2532 * delete them unconditionally.
2535 for (i = 0; blocks[i] != 0; ++i)
2536 mark_block_unused (h, blocks[i]);
2540 /* Delete the values in the node. */
2541 if (delete_values (h, node) == -1)
2544 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
2546 /* If the NK references an SK, delete it. */
2547 size_t sk_offs = le32toh (nk->sk);
2548 if (sk_offs != 0xffffffff) {
2550 if (delete_sk (h, sk_offs) == -1)
2552 nk->sk = htole32 (0xffffffff);
2555 /* If the NK references a classname, delete it. */
2556 size_t cl_offs = le32toh (nk->classname);
2557 if (cl_offs != 0xffffffff) {
2559 mark_block_unused (h, cl_offs);
2560 nk->classname = htole32 (0xffffffff);
2563 /* Delete the node itself. */
2564 mark_block_unused (h, node);
2570 hivex_node_delete_child (hive_h *h, hive_node_h node)
2577 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
2582 if (node == hivex_root (h)) {
2584 fprintf (stderr, "hivex_node_delete_child: cannot delete root node\n");
2589 hive_node_h parent = hivex_node_parent (h, node);
2593 /* Delete node and all its children and values recursively. */
2594 static const struct hivex_visitor visitor = { .node_end = delete_node };
2595 if (hivex_visit_node (h, node, &visitor, sizeof visitor, NULL, 0) == -1)
2598 /* Delete the link from parent to child. We need to find the lf/lh
2599 * record which contains the offset and remove the offset from that
2600 * record, then decrement the element count in that record, and
2601 * decrement the overall number of subkeys stored in the parent
2604 hive_node_h *unused;
2606 if (get_children (h, parent, &unused, &blocks, GET_CHILDREN_NO_CHECK_NK)== -1)
2611 for (i = 0; blocks[i] != 0; ++i) {
2612 struct ntreg_hbin_block *block =
2613 (struct ntreg_hbin_block *) (h->addr + blocks[i]);
2615 if (block->id[0] == 'l' && (block->id[1] == 'f' || block->id[1] == 'h')) {
2616 struct ntreg_lf_record *lf = (struct ntreg_lf_record *) block;
2618 size_t nr_subkeys_in_lf = le16toh (lf->nr_keys);
2620 for (j = 0; j < nr_subkeys_in_lf; ++j)
2621 if (le32toh (lf->keys[j].offset) + 0x1000 == node) {
2622 for (; j < nr_subkeys_in_lf - 1; ++j)
2623 memcpy (&lf->keys[j], &lf->keys[j+1], sizeof (lf->keys[j]));
2624 lf->nr_keys = htole16 (nr_subkeys_in_lf - 1);
2630 fprintf (stderr, "hivex_node_delete_child: could not find parent"
2631 " to child link\n");
2636 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + parent);
2637 size_t nr_subkeys_in_nk = le32toh (nk->nr_subkeys);
2638 nk->nr_subkeys = htole32 (nr_subkeys_in_nk - 1);
2641 fprintf (stderr, "hivex_node_delete_child: updating nr_subkeys"
2642 " in parent 0x%zx to %zu\n", parent, nr_subkeys_in_nk);
2648 hivex_node_set_values (hive_h *h, hive_node_h node,
2649 size_t nr_values, const hive_set_value *values,
2657 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
2662 /* Delete all existing values. */
2663 if (delete_values (h, node) == -1)
2669 /* Allocate value list node. Value lists have no id field. */
2670 static const char nul_id[2] = { 0, 0 };
2672 sizeof (struct ntreg_value_list) + (nr_values - 1) * sizeof (uint32_t);
2673 size_t vallist_offs = allocate_block (h, seg_len, nul_id);
2674 if (vallist_offs == 0)
2677 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
2678 nk->nr_values = htole32 (nr_values);
2679 nk->vallist = htole32 (vallist_offs - 0x1000);
2681 struct ntreg_value_list *vallist =
2682 (struct ntreg_value_list *) (h->addr + vallist_offs);
2685 for (i = 0; i < nr_values; ++i) {
2686 /* Allocate vk record to store this (key, value) pair. */
2687 static const char vk_id[2] = { 'v', 'k' };
2688 seg_len = sizeof (struct ntreg_vk_record) + strlen (values[i].key);
2689 size_t vk_offs = allocate_block (h, seg_len, vk_id);
2693 /* Recalculate pointers that could have been invalidated by
2694 * previous call to allocate_block.
2696 nk = (struct ntreg_nk_record *) (h->addr + node);
2697 vallist = (struct ntreg_value_list *) (h->addr + vallist_offs);
2699 vallist->offset[i] = htole32 (vk_offs - 0x1000);
2701 struct ntreg_vk_record *vk = (struct ntreg_vk_record *) (h->addr + vk_offs);
2702 size_t name_len = strlen (values[i].key);
2703 vk->name_len = htole16 (name_len);
2704 strcpy (vk->name, values[i].key);
2705 vk->data_type = htole32 (values[i].t);
2706 uint32_t len = values[i].len;
2707 if (len <= 4) /* store it inline => set MSB flag */
2709 vk->data_len = htole32 (len);
2710 vk->flags = name_len == 0 ? 0 : 1;
2712 if (values[i].len <= 4) /* store it inline */
2713 memcpy (&vk->data_offset, values[i].value, values[i].len);
2715 size_t offs = allocate_block (h, values[i].len + 4, nul_id);
2719 /* Recalculate pointers that could have been invalidated by
2720 * previous call to allocate_block.
2722 nk = (struct ntreg_nk_record *) (h->addr + node);
2723 vallist = (struct ntreg_value_list *) (h->addr + vallist_offs);
2724 vk = (struct ntreg_vk_record *) (h->addr + vk_offs);
2726 memcpy (h->addr + offs + 4, values[i].value, values[i].len);
2727 vk->data_offset = htole32 (offs - 0x1000);
2730 if (name_len * 2 > le32toh (nk->max_vk_name_len))
2731 /* * 2 for UTF16-LE "reencoding" */
2732 nk->max_vk_name_len = htole32 (name_len * 2);
2733 if (values[i].len > le32toh (nk->max_vk_data_len))
2734 nk->max_vk_data_len = htole32 (values[i].len);
2741 hivex_node_set_value (hive_h *h, hive_node_h node,
2742 const hive_set_value *val, int flags)
2744 hive_value_h *prev_values = hivex_node_values (h, node);
2745 if (prev_values == NULL)
2750 size_t nr_values = 0;
2751 for (hive_value_h *itr = prev_values; *itr != 0; ++itr)
2754 hive_set_value *values = malloc ((nr_values + 1) * (sizeof (hive_set_value)));
2756 goto leave_prev_values;
2759 int idx_of_val = -1;
2760 hive_value_h *prev_val;
2761 for (prev_val = prev_values; *prev_val != 0; ++prev_val) {
2765 hive_set_value *value = &values[prev_val - prev_values];
2767 char *valval = hivex_value_value (h, *prev_val, &t, &len);
2768 if (valval == NULL) goto leave_partial;
2771 value->value = valval;
2775 char *valkey = hivex_value_key (h, *prev_val);
2776 if (valkey == NULL) goto leave_partial;
2779 value->key = valkey;
2781 if (STRCASEEQ (valkey, val->key))
2782 idx_of_val = prev_val - prev_values;
2785 if (idx_of_val > -1) {
2786 free (values[idx_of_val].key);
2787 free (values[idx_of_val].value);
2789 idx_of_val = nr_values;
2793 hive_set_value *value = &values[idx_of_val];
2794 *value = (hive_set_value){
2795 .key = strdup (val->key),
2796 .value = malloc (val->len),
2801 if (value->key == NULL || value->value == NULL) goto leave_partial;
2802 memcpy (value->value, val->value, val->len);
2804 retval = hivex_node_set_values (h, node, nr_values, values, 0);
2807 for (int i = 0; i < alloc_ct; i += 2) {
2808 free (values[i / 2].value);
2809 if (i + 1 < alloc_ct && values[i / 2].key != NULL)
2810 free (values[i / 2].key);