1 /* hivex - Windows Registry "hive" extraction library.
2 * Copyright (C) 2009-2010 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 #include "full-read.h"
38 #include "full-write.h"
44 #define STREQ(a,b) (strcmp((a),(b)) == 0)
45 #define STRCASEEQ(a,b) (strcasecmp((a),(b)) == 0)
46 //#define STRNEQ(a,b) (strcmp((a),(b)) != 0)
47 //#define STRCASENEQ(a,b) (strcasecmp((a),(b)) != 0)
48 #define STREQLEN(a,b,n) (strncmp((a),(b),(n)) == 0)
49 //#define STRCASEEQLEN(a,b,n) (strncasecmp((a),(b),(n)) == 0)
50 //#define STRNEQLEN(a,b,n) (strncmp((a),(b),(n)) != 0)
51 //#define STRCASENEQLEN(a,b,n) (strncasecmp((a),(b),(n)) != 0)
52 //#define STRPREFIX(a,b) (strncmp((a),(b),strlen((b))) == 0)
55 #include "byte_conversions.h"
57 static char *windows_utf16_to_utf8 (/* const */ char *input, size_t len);
66 /* Registry file, memory mapped if read-only, or malloc'd if writing. */
69 struct ntreg_header *hdr;
72 /* Use a bitmap to store which file offsets are valid (point to a
73 * used block). We only need to store 1 bit per 32 bits of the file
74 * (because blocks are 4-byte aligned). We found that the average
75 * block size in a registry file is ~50 bytes. So roughly 1 in 12
76 * bits in the bitmap will be set, making it likely a more efficient
77 * structure than a hash table.
80 #define BITMAP_SET(bitmap,off) (bitmap[(off)>>5] |= 1 << (((off)>>2)&7))
81 #define BITMAP_CLR(bitmap,off) (bitmap[(off)>>5] &= ~ (1 << (((off)>>2)&7)))
82 #define BITMAP_TST(bitmap,off) (bitmap[(off)>>5] & (1 << (((off)>>2)&7)))
83 #define IS_VALID_BLOCK(h,off) \
84 (((off) & 3) == 0 && \
86 (off) < (h)->size && \
87 BITMAP_TST((h)->bitmap,(off)))
89 /* Fields from the header, extracted from little-endianness hell. */
90 size_t rootoffs; /* Root key offset (always an nk-block). */
91 size_t endpages; /* Offset of end of pages. */
94 size_t endblocks; /* Offset to next block allocation (0
95 if not allocated anything yet). */
98 /* NB. All fields are little endian. */
100 char magic[4]; /* "regf" */
103 char last_modified[8];
104 uint32_t major_ver; /* 1 */
105 uint32_t minor_ver; /* 3 */
106 uint32_t unknown5; /* 0 */
107 uint32_t unknown6; /* 1 */
108 uint32_t offset; /* offset of root key record - 4KB */
109 uint32_t blocks; /* pointer AFTER last hbin in file - 4KB */
110 uint32_t unknown7; /* 1 */
112 char name[64]; /* original file name of hive */
113 char unknown_guid1[16];
114 char unknown_guid2[16];
117 char unknown_guid3[16];
122 uint32_t csum; /* checksum: xor of dwords 0-0x1fb. */
124 char unknown11[3528];
126 char unknown_guid4[16];
127 char unknown_guid5[16];
128 char unknown_guid6[16];
132 } __attribute__((__packed__));
134 struct ntreg_hbin_page {
135 char magic[4]; /* "hbin" */
136 uint32_t offset_first; /* offset from 1st block */
137 uint32_t page_size; /* size of this page (multiple of 4KB) */
139 /* Linked list of blocks follows here. */
140 } __attribute__((__packed__));
142 struct ntreg_hbin_block {
143 int32_t seg_len; /* length of this block (-ve for used block) */
144 char id[2]; /* the block type (eg. "nk" for nk record) */
145 /* Block data follows here. */
146 } __attribute__((__packed__));
148 #define BLOCK_ID_EQ(h,offs,eqid) \
149 (STREQLEN (((struct ntreg_hbin_block *)((h)->addr + (offs)))->id, (eqid), 2))
152 block_len (hive_h *h, size_t blkoff, int *used)
154 struct ntreg_hbin_block *block;
155 block = (struct ntreg_hbin_block *) (h->addr + blkoff);
157 int32_t len = le32toh (block->seg_len);
168 struct ntreg_nk_record {
169 int32_t seg_len; /* length (always -ve because used) */
170 char id[2]; /* "nk" */
174 uint32_t parent; /* offset of owner/parent */
175 uint32_t nr_subkeys; /* number of subkeys */
176 uint32_t nr_subkeys_volatile;
177 uint32_t subkey_lf; /* lf record containing list of subkeys */
178 uint32_t subkey_lf_volatile;
179 uint32_t nr_values; /* number of values */
180 uint32_t vallist; /* value-list record */
181 uint32_t sk; /* offset of sk-record */
182 uint32_t classname; /* offset of classname record */
183 uint16_t max_subkey_name_len; /* maximum length of a subkey name in bytes
184 if the subkey was reencoded as UTF-16LE */
187 uint32_t max_vk_name_len; /* maximum length of any vk name in bytes
188 if the name was reencoded as UTF-16LE */
189 uint32_t max_vk_data_len; /* maximum length of any vk data in bytes */
191 uint16_t name_len; /* length of name */
192 uint16_t classname_len; /* length of classname */
193 char name[1]; /* name follows here */
194 } __attribute__((__packed__));
196 struct ntreg_lf_record {
198 char id[2]; /* "lf" */
199 uint16_t nr_keys; /* number of keys in this record */
201 uint32_t offset; /* offset of nk-record for this subkey */
202 char hash[4]; /* hash of subkey name */
204 } __attribute__((__packed__));
206 struct ntreg_ri_record {
208 char id[2]; /* "ri" */
209 uint16_t nr_offsets; /* number of pointers to lh records */
210 uint32_t offset[1]; /* list of pointers to lh records */
211 } __attribute__((__packed__));
213 /* This has no ID header. */
214 struct ntreg_value_list {
216 uint32_t offset[1]; /* list of pointers to vk records */
217 } __attribute__((__packed__));
219 struct ntreg_vk_record {
220 int32_t seg_len; /* length (always -ve because used) */
221 char id[2]; /* "vk" */
222 uint16_t name_len; /* length of name */
223 /* length of the data:
224 * If data_len is <= 4, then it's stored inline.
225 * If data_len is 0x80000000, then it's an inline dword.
226 * Top bit may be set or not set at random.
229 uint32_t data_offset; /* pointer to the data (or data if inline) */
230 uint32_t data_type; /* type of the data */
231 uint16_t flags; /* bit 0 set => key name ASCII,
232 bit 0 clr => key name UTF-16.
233 Only seen ASCII here in the wild.
234 NB: this is CLEAR for default key. */
236 char name[1]; /* key name follows here */
237 } __attribute__((__packed__));
240 header_checksum (const hive_h *h)
242 uint32_t *daddr = (uint32_t *) h->addr;
246 for (i = 0; i < 0x1fc / 4; ++i) {
247 sum ^= le32toh (*daddr);
255 hivex_open (const char *filename, int flags)
259 assert (sizeof (struct ntreg_header) == 0x1000);
260 assert (offsetof (struct ntreg_header, csum) == 0x1fc);
262 h = calloc (1, sizeof *h);
266 h->msglvl = flags & HIVEX_OPEN_MSGLVL_MASK;
268 const char *debug = getenv ("HIVEX_DEBUG");
269 if (debug && STREQ (debug, "1"))
273 fprintf (stderr, "hivex_open: created handle %p\n", h);
275 h->writable = !!(flags & HIVEX_OPEN_WRITE);
276 h->filename = strdup (filename);
277 if (h->filename == NULL)
280 h->fd = open (filename, O_RDONLY | O_CLOEXEC);
285 if (fstat (h->fd, &statbuf) == -1)
288 h->size = statbuf.st_size;
291 h->addr = mmap (NULL, h->size, PROT_READ, MAP_SHARED, h->fd, 0);
292 if (h->addr == MAP_FAILED)
296 fprintf (stderr, "hivex_open: mapped file at %p\n", h->addr);
298 h->addr = malloc (h->size);
302 if (full_read (h->fd, h->addr, h->size) < h->size)
307 if (h->hdr->magic[0] != 'r' ||
308 h->hdr->magic[1] != 'e' ||
309 h->hdr->magic[2] != 'g' ||
310 h->hdr->magic[3] != 'f') {
311 fprintf (stderr, "hivex: %s: not a Windows NT Registry hive file\n",
317 /* Check major version. */
318 uint32_t major_ver = le32toh (h->hdr->major_ver);
319 if (major_ver != 1) {
321 "hivex: %s: hive file major version %" PRIu32 " (expected 1)\n",
322 filename, major_ver);
327 h->bitmap = calloc (1 + h->size / 32, 1);
328 if (h->bitmap == NULL)
331 /* Header checksum. */
332 uint32_t sum = header_checksum (h);
333 if (sum != le32toh (h->hdr->csum)) {
334 fprintf (stderr, "hivex: %s: bad checksum in hive header\n", filename);
339 if (h->msglvl >= 2) {
340 char *name = windows_utf16_to_utf8 (h->hdr->name, 64);
343 "hivex_open: header fields:\n"
344 " file version %" PRIu32 ".%" PRIu32 "\n"
345 " sequence nos %" PRIu32 " %" PRIu32 "\n"
346 " (sequences nos should match if hive was synched at shutdown)\n"
347 " original file name %s\n"
348 " (only 32 chars are stored, name is probably truncated)\n"
349 " root offset 0x%x + 0x1000\n"
350 " end of last page 0x%x + 0x1000 (total file size 0x%zx)\n"
351 " checksum 0x%x (calculated 0x%x)\n",
352 major_ver, le32toh (h->hdr->minor_ver),
353 le32toh (h->hdr->sequence1), le32toh (h->hdr->sequence2),
354 name ? name : "(conversion failed)",
355 le32toh (h->hdr->offset),
356 le32toh (h->hdr->blocks), h->size,
357 le32toh (h->hdr->csum), sum);
361 h->rootoffs = le32toh (h->hdr->offset) + 0x1000;
362 h->endpages = le32toh (h->hdr->blocks) + 0x1000;
365 fprintf (stderr, "hivex_open: root offset = 0x%zx\n", h->rootoffs);
367 /* We'll set this flag when we see a block with the root offset (ie.
370 int seen_root_block = 0, bad_root_block = 0;
372 /* Collect some stats. */
373 size_t pages = 0; /* Number of hbin pages read. */
374 size_t smallest_page = SIZE_MAX, largest_page = 0;
375 size_t blocks = 0; /* Total number of blocks found. */
376 size_t smallest_block = SIZE_MAX, largest_block = 0, blocks_bytes = 0;
377 size_t used_blocks = 0; /* Total number of used blocks found. */
378 size_t used_size = 0; /* Total size (bytes) of used blocks. */
380 /* Read the pages and blocks. The aim here is to be robust against
381 * corrupt or malicious registries. So we make sure the loops
382 * always make forward progress. We add the address of each block
383 * we read to a hash table so pointers will only reference the start
387 struct ntreg_hbin_page *page;
388 for (off = 0x1000; off < h->size; off += le32toh (page->page_size)) {
389 if (off >= h->endpages)
392 page = (struct ntreg_hbin_page *) (h->addr + off);
393 if (page->magic[0] != 'h' ||
394 page->magic[1] != 'b' ||
395 page->magic[2] != 'i' ||
396 page->magic[3] != 'n') {
397 fprintf (stderr, "hivex: %s: trailing garbage at end of file (at 0x%zx, after %zu pages)\n",
398 filename, off, pages);
403 size_t page_size = le32toh (page->page_size);
405 fprintf (stderr, "hivex_open: page at 0x%zx, size %zu\n", off, page_size);
407 if (page_size < smallest_page) smallest_page = page_size;
408 if (page_size > largest_page) largest_page = page_size;
410 if (page_size <= sizeof (struct ntreg_hbin_page) ||
411 (page_size & 0x0fff) != 0) {
412 fprintf (stderr, "hivex: %s: page size %zu at 0x%zx, bad registry\n",
413 filename, page_size, off);
418 /* Read the blocks in this page. */
420 struct ntreg_hbin_block *block;
422 for (blkoff = off + 0x20;
423 blkoff < off + page_size;
427 int is_root = blkoff == h->rootoffs;
431 block = (struct ntreg_hbin_block *) (h->addr + blkoff);
433 seg_len = block_len (h, blkoff, &used);
434 if (seg_len <= 4 || (seg_len & 3) != 0) {
435 fprintf (stderr, "hivex: %s: block size %" PRIu32 " at 0x%zx, bad registry\n",
436 filename, le32toh (block->seg_len), blkoff);
442 fprintf (stderr, "hivex_open: %s block id %d,%d at 0x%zx size %zu%s\n",
443 used ? "used" : "free", block->id[0], block->id[1], blkoff,
444 seg_len, is_root ? " (root)" : "");
446 blocks_bytes += seg_len;
447 if (seg_len < smallest_block) smallest_block = seg_len;
448 if (seg_len > largest_block) largest_block = seg_len;
450 if (is_root && !used)
455 used_size += seg_len;
457 /* Root block must be an nk-block. */
458 if (is_root && (block->id[0] != 'n' || block->id[1] != 'k'))
461 /* Note this blkoff is a valid address. */
462 BITMAP_SET (h->bitmap, blkoff);
467 if (!seen_root_block) {
468 fprintf (stderr, "hivex: %s: no root block found\n", filename);
473 if (bad_root_block) {
474 fprintf (stderr, "hivex: %s: bad root block (free or not nk)\n", filename);
481 "hivex_open: successfully read Windows Registry hive file:\n"
482 " pages: %zu [sml: %zu, lge: %zu]\n"
483 " blocks: %zu [sml: %zu, avg: %zu, lge: %zu]\n"
484 " blocks used: %zu\n"
485 " bytes used: %zu\n",
486 pages, smallest_page, largest_page,
487 blocks, smallest_block, blocks_bytes / blocks, largest_block,
488 used_blocks, used_size);
496 if (h->addr && h->size && h->addr != MAP_FAILED) {
498 munmap (h->addr, h->size);
512 hivex_close (hive_h *h)
518 munmap (h->addr, h->size);
528 /*----------------------------------------------------------------------
533 hivex_root (hive_h *h)
535 hive_node_h ret = h->rootoffs;
536 if (!IS_VALID_BLOCK (h, ret)) {
544 hivex_node_name (hive_h *h, hive_node_h node)
546 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
551 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
553 /* AFAIK the node name is always plain ASCII, so no conversion
554 * to UTF-8 is necessary. However we do need to nul-terminate
558 /* nk->name_len is unsigned, 16 bit, so this is safe ... However
559 * we have to make sure the length doesn't exceed the block length.
561 size_t len = le16toh (nk->name_len);
562 size_t seg_len = block_len (h, node, NULL);
563 if (sizeof (struct ntreg_nk_record) + len - 1 > seg_len) {
565 fprintf (stderr, "hivex_node_name: returning EFAULT because node name is too long (%zu, %zu)\n",
571 char *ret = malloc (len + 1);
574 memcpy (ret, nk->name, len);
580 /* I think the documentation for the sk and classname fields in the nk
581 * record is wrong, or else the offset field is in the wrong place.
582 * Otherwise this makes no sense. Disabled this for now -- it's not
583 * useful for reading the registry anyway.
587 hivex_node_security (hive_h *h, hive_node_h node)
589 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
594 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
596 hive_node_h ret = le32toh (nk->sk);
598 if (!IS_VALID_BLOCK (h, ret)) {
606 hivex_node_classname (hive_h *h, hive_node_h node)
608 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
613 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
615 hive_node_h ret = le32toh (nk->classname);
617 if (!IS_VALID_BLOCK (h, ret)) {
625 /* Structure for returning 0-terminated lists of offsets (nodes,
635 init_offset_list (struct offset_list *list)
639 list->offsets = NULL;
642 #define INIT_OFFSET_LIST(name) \
643 struct offset_list name; \
644 init_offset_list (&name)
646 /* Preallocates the offset_list, but doesn't make the contents longer. */
648 grow_offset_list (struct offset_list *list, size_t alloc)
650 assert (alloc >= list->len);
651 size_t *p = realloc (list->offsets, alloc * sizeof (size_t));
660 add_to_offset_list (struct offset_list *list, size_t offset)
662 if (list->len >= list->alloc) {
663 if (grow_offset_list (list, list->alloc ? list->alloc * 2 : 4) == -1)
666 list->offsets[list->len] = offset;
672 free_offset_list (struct offset_list *list)
674 free (list->offsets);
678 return_offset_list (struct offset_list *list)
680 if (add_to_offset_list (list, 0) == -1)
682 return list->offsets; /* caller frees */
685 /* Iterate over children, returning child nodes and intermediate blocks. */
687 get_children (hive_h *h, hive_node_h node,
688 hive_node_h **children_ret, size_t **blocks_ret)
690 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
695 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
697 size_t nr_subkeys_in_nk = le32toh (nk->nr_subkeys);
699 INIT_OFFSET_LIST (children);
700 INIT_OFFSET_LIST (blocks);
702 /* Deal with the common "no subkeys" case quickly. */
703 if (nr_subkeys_in_nk == 0)
706 /* Arbitrarily limit the number of subkeys we will ever deal with. */
707 if (nr_subkeys_in_nk > 1000000) {
712 /* Preallocate space for the children. */
713 if (grow_offset_list (&children, nr_subkeys_in_nk) == -1)
716 /* The subkey_lf field can point either to an lf-record, which is
717 * the common case, or if there are lots of subkeys, to an
720 size_t subkey_lf = le32toh (nk->subkey_lf);
722 if (!IS_VALID_BLOCK (h, subkey_lf)) {
724 fprintf (stderr, "hivex_node_children: returning EFAULT because subkey_lf is not a valid block (%zu)\n",
730 if (add_to_offset_list (&blocks, subkey_lf) == -1)
733 struct ntreg_hbin_block *block =
734 (struct ntreg_hbin_block *) (h->addr + subkey_lf);
736 /* Points to lf-record? (Note, also "lh" but that is basically the
737 * same as "lf" as far as we are concerned here).
739 if (block->id[0] == 'l' && (block->id[1] == 'f' || block->id[1] == 'h')) {
740 struct ntreg_lf_record *lf = (struct ntreg_lf_record *) block;
742 /* Check number of subkeys in the nk-record matches number of subkeys
745 size_t nr_subkeys_in_lf = le16toh (lf->nr_keys);
748 fprintf (stderr, "hivex_node_children: nr_subkeys_in_nk = %zu, nr_subkeys_in_lf = %zu\n",
749 nr_subkeys_in_nk, nr_subkeys_in_lf);
751 if (nr_subkeys_in_nk != nr_subkeys_in_lf) {
756 size_t len = block_len (h, subkey_lf, NULL);
757 if (8 + nr_subkeys_in_lf * 8 > len) {
759 fprintf (stderr, "hivex_node_children: returning EFAULT because too many subkeys (%zu, %zu)\n",
760 nr_subkeys_in_lf, len);
766 for (i = 0; i < nr_subkeys_in_lf; ++i) {
767 hive_node_h subkey = le32toh (lf->keys[i].offset);
769 if (!IS_VALID_BLOCK (h, subkey)) {
771 fprintf (stderr, "hivex_node_children: returning EFAULT because subkey is not a valid block (0x%zx)\n",
776 if (add_to_offset_list (&children, subkey) == -1)
781 /* Points to ri-record? */
782 else if (block->id[0] == 'r' && block->id[1] == 'i') {
783 struct ntreg_ri_record *ri = (struct ntreg_ri_record *) block;
785 size_t nr_offsets = le16toh (ri->nr_offsets);
787 /* Count total number of children. */
789 for (i = 0; i < nr_offsets; ++i) {
790 hive_node_h offset = ri->offset[i];
792 if (!IS_VALID_BLOCK (h, offset)) {
794 fprintf (stderr, "hivex_node_children: returning EFAULT because ri-offset is not a valid block (0x%zx)\n",
799 if (!BLOCK_ID_EQ (h, offset, "lf") && !BLOCK_ID_EQ (h, offset, "lh")) {
804 if (add_to_offset_list (&blocks, offset) == -1)
807 struct ntreg_lf_record *lf =
808 (struct ntreg_lf_record *) (h->addr + offset);
810 count += le16toh (lf->nr_keys);
814 fprintf (stderr, "hivex_node_children: nr_subkeys_in_nk = %zu, counted = %zu\n",
815 nr_subkeys_in_nk, count);
817 if (nr_subkeys_in_nk != count) {
822 /* Copy list of children. Note nr_subkeys_in_nk is limited to
823 * something reasonable above.
825 for (i = 0; i < nr_offsets; ++i) {
826 hive_node_h offset = ri->offset[i];
828 if (!IS_VALID_BLOCK (h, offset)) {
830 fprintf (stderr, "hivex_node_children: returning EFAULT because ri-offset is not a valid block (0x%zx)\n",
835 if (!BLOCK_ID_EQ (h, offset, "lf") && !BLOCK_ID_EQ (h, offset, "lh")) {
840 struct ntreg_lf_record *lf =
841 (struct ntreg_lf_record *) (h->addr + offset);
844 for (j = 0; j < le16toh (lf->nr_keys); ++j) {
845 hive_node_h subkey = le32toh (lf->keys[j].offset);
847 if (!IS_VALID_BLOCK (h, subkey)) {
849 fprintf (stderr, "hivex_node_children: returning EFAULT because indirect subkey is not a valid block (0x%zx)\n",
854 if (add_to_offset_list (&children, subkey) == -1)
860 /* else not supported, set errno and fall through */
863 free_offset_list (&children);
864 free_offset_list (&blocks);
868 *children_ret = return_offset_list (&children);
869 *blocks_ret = return_offset_list (&blocks);
870 if (!*children_ret || !*blocks_ret)
876 hivex_node_children (hive_h *h, hive_node_h node)
878 hive_node_h *children;
881 if (get_children (h, node, &children, &blocks) == -1)
888 /* Very inefficient, but at least having a separate API call
889 * allows us to make it more efficient in future.
892 hivex_node_get_child (hive_h *h, hive_node_h node, const char *nname)
894 hive_node_h *children = NULL;
898 children = hivex_node_children (h, node);
899 if (!children) goto error;
902 for (i = 0; children[i] != 0; ++i) {
903 name = hivex_node_name (h, children[i]);
904 if (!name) goto error;
905 if (STRCASEEQ (name, nname)) {
909 free (name); name = NULL;
919 hivex_node_parent (hive_h *h, hive_node_h node)
921 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
926 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
928 hive_node_h ret = le32toh (nk->parent);
930 if (!IS_VALID_BLOCK (h, ret)) {
932 fprintf (stderr, "hivex_node_parent: returning EFAULT because parent is not a valid block (0x%zx)\n",
941 get_values (hive_h *h, hive_node_h node,
942 hive_value_h **values_ret, size_t **blocks_ret)
944 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
949 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
951 size_t nr_values = le32toh (nk->nr_values);
954 fprintf (stderr, "hivex_node_values: nr_values = %zu\n", nr_values);
956 INIT_OFFSET_LIST (values);
957 INIT_OFFSET_LIST (blocks);
959 /* Deal with the common "no values" case quickly. */
963 /* Arbitrarily limit the number of values we will ever deal with. */
964 if (nr_values > 100000) {
969 /* Preallocate space for the values. */
970 if (grow_offset_list (&values, nr_values) == -1)
973 /* Get the value list and check it looks reasonable. */
974 size_t vlist_offset = le32toh (nk->vallist);
975 vlist_offset += 0x1000;
976 if (!IS_VALID_BLOCK (h, vlist_offset)) {
978 fprintf (stderr, "hivex_node_values: returning EFAULT because value list is not a valid block (0x%zx)\n",
984 if (add_to_offset_list (&blocks, vlist_offset) == -1)
987 struct ntreg_value_list *vlist =
988 (struct ntreg_value_list *) (h->addr + vlist_offset);
990 size_t len = block_len (h, vlist_offset, NULL);
991 if (4 + nr_values * 4 > len) {
993 fprintf (stderr, "hivex_node_values: returning EFAULT because value list is too long (%zu, %zu)\n",
1000 for (i = 0; i < nr_values; ++i) {
1001 hive_node_h value = vlist->offset[i];
1003 if (!IS_VALID_BLOCK (h, value)) {
1005 fprintf (stderr, "hivex_node_values: returning EFAULT because value is not a valid block (0x%zx)\n",
1010 if (add_to_offset_list (&values, value) == -1)
1015 *values_ret = return_offset_list (&values);
1016 *blocks_ret = return_offset_list (&blocks);
1017 if (!*values_ret || !*blocks_ret)
1022 free_offset_list (&values);
1023 free_offset_list (&blocks);
1028 hivex_node_values (hive_h *h, hive_node_h node)
1030 hive_value_h *values;
1033 if (get_values (h, node, &values, &blocks) == -1)
1040 /* Very inefficient, but at least having a separate API call
1041 * allows us to make it more efficient in future.
1044 hivex_node_get_value (hive_h *h, hive_node_h node, const char *key)
1046 hive_value_h *values = NULL;
1048 hive_value_h ret = 0;
1050 values = hivex_node_values (h, node);
1051 if (!values) goto error;
1054 for (i = 0; values[i] != 0; ++i) {
1055 name = hivex_value_key (h, values[i]);
1056 if (!name) goto error;
1057 if (STRCASEEQ (name, key)) {
1061 free (name); name = NULL;
1071 hivex_value_key (hive_h *h, hive_value_h value)
1073 if (!IS_VALID_BLOCK (h, value) || !BLOCK_ID_EQ (h, value, "vk")) {
1078 struct ntreg_vk_record *vk = (struct ntreg_vk_record *) (h->addr + value);
1080 /* AFAIK the key is always plain ASCII, so no conversion to UTF-8 is
1081 * necessary. However we do need to nul-terminate the string.
1084 /* vk->name_len is unsigned, 16 bit, so this is safe ... However
1085 * we have to make sure the length doesn't exceed the block length.
1087 size_t len = le16toh (vk->name_len);
1088 size_t seg_len = block_len (h, value, NULL);
1089 if (sizeof (struct ntreg_vk_record) + len - 1 > seg_len) {
1091 fprintf (stderr, "hivex_value_key: returning EFAULT because key length is too long (%zu, %zu)\n",
1097 char *ret = malloc (len + 1);
1100 memcpy (ret, vk->name, len);
1106 hivex_value_type (hive_h *h, hive_value_h value, hive_type *t, size_t *len)
1108 if (!IS_VALID_BLOCK (h, value) || !BLOCK_ID_EQ (h, value, "vk")) {
1113 struct ntreg_vk_record *vk = (struct ntreg_vk_record *) (h->addr + value);
1116 *t = le32toh (vk->data_type);
1119 *len = le32toh (vk->data_len);
1120 if (*len == 0x80000000) { /* special case */
1122 if (t) *t = hive_t_dword;
1131 hivex_value_value (hive_h *h, hive_value_h value,
1132 hive_type *t_rtn, size_t *len_rtn)
1134 if (!IS_VALID_BLOCK (h, value) || !BLOCK_ID_EQ (h, value, "vk")) {
1139 struct ntreg_vk_record *vk = (struct ntreg_vk_record *) (h->addr + value);
1144 t = le32toh (vk->data_type);
1146 len = le32toh (vk->data_len);
1147 if (len == 0x80000000) { /* special case */
1154 fprintf (stderr, "hivex_value_value: value=0x%zx, t=%d, len=%zu\n",
1162 /* Arbitrarily limit the length that we will read. */
1163 if (len > 1000000) {
1168 char *ret = malloc (len);
1172 /* If length is <= 4 it's always stored inline. */
1174 memcpy (ret, (char *) &vk->data_offset, len);
1178 size_t data_offset = le32toh (vk->data_offset);
1179 data_offset += 0x1000;
1180 if (!IS_VALID_BLOCK (h, data_offset)) {
1182 fprintf (stderr, "hivex_value_value: returning EFAULT because data offset is not a valid block (0x%zx)\n",
1189 /* Check that the declared size isn't larger than the block its in.
1191 * XXX Some apparently valid registries are seen to have this,
1192 * so turn this into a warning and substitute the smaller length
1195 size_t blen = block_len (h, data_offset, NULL);
1196 if (len > blen - 4 /* subtract 4 for block header */) {
1198 fprintf (stderr, "hivex_value_value: warning: declared data length is longer than the block it is in (data 0x%zx, data len %zu, block len %zu)\n",
1199 data_offset, len, blen);
1203 char *data = h->addr + data_offset + 4;
1204 memcpy (ret, data, len);
1209 windows_utf16_to_utf8 (/* const */ char *input, size_t len)
1211 iconv_t ic = iconv_open ("UTF-8", "UTF-16");
1212 if (ic == (iconv_t) -1)
1215 /* iconv(3) has an insane interface ... */
1217 /* Mostly UTF-8 will be smaller, so this is a good initial guess. */
1218 size_t outalloc = len;
1222 size_t outlen = outalloc;
1223 char *out = malloc (outlen + 1);
1233 size_t r = iconv (ic, &inp, &inlen, &outp, &outlen);
1234 if (r == (size_t) -1) {
1235 if (errno == E2BIG) {
1236 size_t prev = outalloc;
1237 /* Try again with a larger output buffer. */
1240 if (outalloc < prev)
1245 /* Else some conversion failure, eg. EILSEQ, EINVAL. */
1261 hivex_value_string (hive_h *h, hive_value_h value)
1265 char *data = hivex_value_value (h, value, &t, &len);
1270 if (t != hive_t_string && t != hive_t_expand_string && t != hive_t_link) {
1276 char *ret = windows_utf16_to_utf8 (data, len);
1285 free_strings (char **argv)
1290 for (i = 0; argv[i] != NULL; ++i)
1296 /* Get the length of a UTF-16 format string. Handle the string as
1297 * pairs of bytes, looking for the first \0\0 pair.
1300 utf16_string_len_in_bytes (const char *str)
1304 while (str[0] || str[1]) {
1312 /* http://blogs.msdn.com/oldnewthing/archive/2009/10/08/9904646.aspx */
1314 hivex_value_multiple_strings (hive_h *h, hive_value_h value)
1318 char *data = hivex_value_value (h, value, &t, &len);
1323 if (t != hive_t_multiple_strings) {
1329 size_t nr_strings = 0;
1330 char **ret = malloc ((1 + nr_strings) * sizeof (char *));
1340 while (p < data + len && (plen = utf16_string_len_in_bytes (p)) > 0) {
1342 char **ret2 = realloc (ret, (1 + nr_strings) * sizeof (char *));
1350 ret[nr_strings-1] = windows_utf16_to_utf8 (p, plen);
1351 ret[nr_strings] = NULL;
1352 if (ret[nr_strings-1] == NULL) {
1358 p += plen + 2 /* skip over UTF-16 \0\0 at the end of this string */;
1366 hivex_value_dword (hive_h *h, hive_value_h value)
1370 char *data = hivex_value_value (h, value, &t, &len);
1375 if ((t != hive_t_dword && t != hive_t_dword_be) || len != 4) {
1381 int32_t ret = *(int32_t*)data;
1383 if (t == hive_t_dword) /* little endian */
1384 ret = le32toh (ret);
1386 ret = be32toh (ret);
1392 hivex_value_qword (hive_h *h, hive_value_h value)
1396 char *data = hivex_value_value (h, value, &t, &len);
1401 if (t != hive_t_qword || len != 8) {
1407 int64_t ret = *(int64_t*)data;
1409 ret = le64toh (ret); /* always little endian */
1414 /*----------------------------------------------------------------------
1419 hivex_visit (hive_h *h, const struct hivex_visitor *visitor, size_t len,
1420 void *opaque, int flags)
1422 return hivex_visit_node (h, hivex_root (h), visitor, len, opaque, flags);
1425 static int hivex__visit_node (hive_h *h, hive_node_h node, const struct hivex_visitor *vtor, char *unvisited, void *opaque, int flags);
1428 hivex_visit_node (hive_h *h, hive_node_h node,
1429 const struct hivex_visitor *visitor, size_t len, void *opaque,
1432 struct hivex_visitor vtor;
1433 memset (&vtor, 0, sizeof vtor);
1435 /* Note that len might be larger *or smaller* than the expected size. */
1436 size_t copysize = len <= sizeof vtor ? len : sizeof vtor;
1437 memcpy (&vtor, visitor, copysize);
1439 /* This bitmap records unvisited nodes, so we don't loop if the
1440 * registry contains cycles.
1442 char *unvisited = malloc (1 + h->size / 32);
1443 if (unvisited == NULL)
1445 memcpy (unvisited, h->bitmap, 1 + h->size / 32);
1447 int r = hivex__visit_node (h, node, &vtor, unvisited, opaque, flags);
1453 hivex__visit_node (hive_h *h, hive_node_h node,
1454 const struct hivex_visitor *vtor, char *unvisited,
1455 void *opaque, int flags)
1457 int skip_bad = flags & HIVEX_VISIT_SKIP_BAD;
1459 hive_value_h *values = NULL;
1460 hive_node_h *children = NULL;
1466 /* Return -1 on all callback errors. However on internal errors,
1467 * check if skip_bad is set and suppress those errors if so.
1471 if (!BITMAP_TST (unvisited, node)) {
1473 fprintf (stderr, "hivex__visit_node: contains cycle: visited node 0x%zx already\n",
1477 return skip_bad ? 0 : -1;
1479 BITMAP_CLR (unvisited, node);
1481 name = hivex_node_name (h, node);
1482 if (!name) return skip_bad ? 0 : -1;
1483 if (vtor->node_start && vtor->node_start (h, opaque, node, name) == -1)
1486 values = hivex_node_values (h, node);
1488 ret = skip_bad ? 0 : -1;
1492 for (i = 0; values[i] != 0; ++i) {
1496 if (hivex_value_type (h, values[i], &t, &len) == -1) {
1497 ret = skip_bad ? 0 : -1;
1501 key = hivex_value_key (h, values[i]);
1503 ret = skip_bad ? 0 : -1;
1507 if (vtor->value_any) {
1508 str = hivex_value_value (h, values[i], &t, &len);
1510 ret = skip_bad ? 0 : -1;
1513 if (vtor->value_any (h, opaque, node, values[i], t, len, key, str) == -1)
1515 free (str); str = NULL;
1520 str = hivex_value_value (h, values[i], &t, &len);
1522 ret = skip_bad ? 0 : -1;
1525 if (t != hive_t_none) {
1526 ret = skip_bad ? 0 : -1;
1529 if (vtor->value_none &&
1530 vtor->value_none (h, opaque, node, values[i], t, len, key, str) == -1)
1532 free (str); str = NULL;
1536 case hive_t_expand_string:
1538 str = hivex_value_string (h, values[i]);
1540 if (errno != EILSEQ && errno != EINVAL) {
1541 ret = skip_bad ? 0 : -1;
1544 if (vtor->value_string_invalid_utf16) {
1545 str = hivex_value_value (h, values[i], &t, &len);
1546 if (vtor->value_string_invalid_utf16 (h, opaque, node, values[i], t, len, key, str) == -1)
1548 free (str); str = NULL;
1552 if (vtor->value_string &&
1553 vtor->value_string (h, opaque, node, values[i], t, len, key, str) == -1)
1555 free (str); str = NULL;
1559 case hive_t_dword_be: {
1560 int32_t i32 = hivex_value_dword (h, values[i]);
1561 if (vtor->value_dword &&
1562 vtor->value_dword (h, opaque, node, values[i], t, len, key, i32) == -1)
1567 case hive_t_qword: {
1568 int64_t i64 = hivex_value_qword (h, values[i]);
1569 if (vtor->value_qword &&
1570 vtor->value_qword (h, opaque, node, values[i], t, len, key, i64) == -1)
1576 str = hivex_value_value (h, values[i], &t, &len);
1578 ret = skip_bad ? 0 : -1;
1581 if (t != hive_t_binary) {
1582 ret = skip_bad ? 0 : -1;
1585 if (vtor->value_binary &&
1586 vtor->value_binary (h, opaque, node, values[i], t, len, key, str) == -1)
1588 free (str); str = NULL;
1591 case hive_t_multiple_strings:
1592 strs = hivex_value_multiple_strings (h, values[i]);
1594 if (errno != EILSEQ && errno != EINVAL) {
1595 ret = skip_bad ? 0 : -1;
1598 if (vtor->value_string_invalid_utf16) {
1599 str = hivex_value_value (h, values[i], &t, &len);
1600 if (vtor->value_string_invalid_utf16 (h, opaque, node, values[i], t, len, key, str) == -1)
1602 free (str); str = NULL;
1606 if (vtor->value_multiple_strings &&
1607 vtor->value_multiple_strings (h, opaque, node, values[i], t, len, key, strs) == -1)
1609 free_strings (strs); strs = NULL;
1612 case hive_t_resource_list:
1613 case hive_t_full_resource_description:
1614 case hive_t_resource_requirements_list:
1616 str = hivex_value_value (h, values[i], &t, &len);
1618 ret = skip_bad ? 0 : -1;
1621 if (vtor->value_other &&
1622 vtor->value_other (h, opaque, node, values[i], t, len, key, str) == -1)
1624 free (str); str = NULL;
1629 free (key); key = NULL;
1632 children = hivex_node_children (h, node);
1633 if (children == NULL) {
1634 ret = skip_bad ? 0 : -1;
1638 for (i = 0; children[i] != 0; ++i) {
1640 fprintf (stderr, "hivex__visit_node: %s: visiting subkey %d (0x%zx)\n",
1641 name, i, children[i]);
1643 if (hivex__visit_node (h, children[i], vtor, unvisited, opaque, flags) == -1)
1647 if (vtor->node_end && vtor->node_end (h, opaque, node, name) == -1)
1658 free_strings (strs);
1662 /*----------------------------------------------------------------------
1666 /* Allocate an hbin (page), extending the malloc'd space if necessary,
1667 * and updating the hive handle fields (but NOT the hive disk header
1668 * -- the hive disk header is updated when we commit). This function
1669 * also extends the bitmap if necessary.
1671 * 'allocation_hint' is the size of the block allocation we would like
1672 * to make. Normally registry blocks are very small (avg 50 bytes)
1673 * and are contained in standard-sized pages (4KB), but the registry
1674 * can support blocks which are larger than a standard page, in which
1675 * case it creates a page of 8KB, 12KB etc.
1678 * > 0 : offset of first usable byte of new page (after page header)
1679 * 0 : error (errno set)
1682 allocate_page (hive_h *h, size_t allocation_hint)
1684 /* In almost all cases this will be 1. */
1685 size_t nr_4k_pages =
1686 1 + (allocation_hint + sizeof (struct ntreg_hbin_page) - 1) / 4096;
1687 assert (nr_4k_pages >= 1);
1689 /* 'extend' is the number of bytes to extend the file by. Note that
1690 * hives found in the wild often contain slack between 'endpages'
1691 * and the actual end of the file, so we don't always need to make
1694 ssize_t extend = h->endpages + nr_4k_pages * 4096 - h->size;
1696 if (h->msglvl >= 2) {
1697 fprintf (stderr, "allocate_page: current endpages = 0x%zx, current size = 0x%zx\n",
1698 h->endpages, h->size);
1699 fprintf (stderr, "allocate_page: extending file by %zd bytes (<= 0 if no extension)\n",
1704 size_t oldsize = h->size;
1705 size_t newsize = h->size + extend;
1706 char *newaddr = realloc (h->addr, newsize);
1707 if (newaddr == NULL)
1710 size_t oldbitmapsize = 1 + oldsize / 32;
1711 size_t newbitmapsize = 1 + newsize / 32;
1712 char *newbitmap = realloc (h->bitmap, newbitmapsize);
1713 if (newbitmap == NULL) {
1720 h->bitmap = newbitmap;
1722 memset (h->addr + oldsize, 0, newsize - oldsize);
1723 memset (h->bitmap + oldbitmapsize, 0, newbitmapsize - oldbitmapsize);
1726 size_t offset = h->endpages;
1727 h->endpages += nr_4k_pages * 4096;
1730 fprintf (stderr, "allocate_page: new endpages = 0x%zx, new size = 0x%zx\n",
1731 h->endpages, h->size);
1733 /* Write the hbin header. */
1734 struct ntreg_hbin_page *page =
1735 (struct ntreg_hbin_page *) (h->addr + offset);
1736 page->magic[0] = 'h';
1737 page->magic[1] = 'b';
1738 page->magic[2] = 'i';
1739 page->magic[3] = 'n';
1740 page->offset_first = htole32 (offset - 0x1000);
1741 page->page_size = htole32 (nr_4k_pages * 4096);
1742 memset (page->unknown, 0, sizeof (page->unknown));
1745 fprintf (stderr, "allocate_page: new page at 0x%zx\n", offset);
1747 /* Offset of first usable byte after the header. */
1748 return offset + sizeof (struct ntreg_hbin_page);
1751 /* Allocate a single block, first allocating an hbin (page) at the end
1752 * of the current file if necessary. NB. To keep the implementation
1753 * simple and more likely to be correct, we do not reuse existing free
1756 * seg_len is the size of the block (this INCLUDES the block header).
1757 * The header of the block is initialized to -seg_len (negative to
1758 * indicate used). id[2] is the block ID (type), eg. "nk" for nk-
1759 * record. The block bitmap is updated to show this block as valid.
1760 * The rest of the contents of the block will be zero.
1763 * > 0 : offset of new block
1764 * 0 : error (errno set)
1767 allocate_block (hive_h *h, size_t seg_len, const char id[2])
1775 /* The caller probably forgot to include the header. Note that
1776 * value lists have no ID field, so seg_len == 4 would be possible
1777 * for them, albeit unusual.
1780 fprintf (stderr, "allocate_block: refusing too small allocation (%zu), returning ERANGE\n",
1786 /* Refuse really large allocations. */
1787 if (seg_len > 1000000) {
1789 fprintf (stderr, "allocate_block: refusing large allocation (%zu), returning ERANGE\n",
1795 /* Round up allocation to multiple of 8 bytes. All blocks must be
1796 * on an 8 byte boundary.
1798 seg_len = (seg_len + 7) & ~7;
1800 /* Allocate a new page if necessary. */
1801 if (h->endblocks == 0 || h->endblocks + seg_len > h->endpages) {
1802 size_t newendblocks = allocate_page (h, seg_len);
1803 if (newendblocks == 0)
1805 h->endblocks = newendblocks;
1808 size_t offset = h->endblocks;
1811 fprintf (stderr, "allocate_block: new block at 0x%zx, size %zu\n",
1814 struct ntreg_hbin_block *blockhdr =
1815 (struct ntreg_hbin_block *) (h->addr + offset);
1817 blockhdr->seg_len = htole32 (- (int32_t) seg_len);
1818 if (id[0] && id[1] && seg_len >= 6) {
1819 blockhdr->id[0] = id[0];
1820 blockhdr->id[1] = id[1];
1823 h->endblocks += seg_len;
1825 /* If there is space after the last block in the last page, then we
1826 * have to put a dummy free block header here to mark the rest of
1829 ssize_t rem = h->endpages - h->endblocks;
1832 fprintf (stderr, "allocate_block: marking remainder of page free starting at 0x%zx, size %zd\n",
1837 blockhdr = (struct ntreg_hbin_block *) (h->addr + h->endblocks);
1838 blockhdr->seg_len = htole32 ((int32_t) rem);
1844 /* 'offset' must point to a valid, used block. This function marks
1845 * the block unused (by updating the seg_len field) and invalidates
1846 * the bitmap. It does NOT do this recursively, so to avoid creating
1847 * unreachable used blocks, callers may have to recurse over the hive
1848 * structures. Also callers must ensure there are no references to
1849 * this block from other parts of the hive.
1852 mark_block_unused (hive_h *h, size_t offset)
1854 assert (h->writable);
1855 assert (IS_VALID_BLOCK (h, offset));
1857 struct ntreg_hbin_block *blockhdr =
1858 (struct ntreg_hbin_block *) (h->addr + offset);
1860 size_t seg_len = block_len (h, offset, NULL);
1861 blockhdr->seg_len = htole32 (seg_len);
1863 BITMAP_CLR (h->bitmap, offset);
1866 /* Delete all existing values at this node. */
1868 delete_values (hive_h *h, hive_node_h node)
1870 assert (h->writable);
1872 hive_value_h *values;
1874 if (get_values (h, node, &values, &blocks) == -1)
1878 for (i = 0; blocks[i] != 0; ++i)
1879 mark_block_unused (h, blocks[i]);
1883 for (i = 0; values[i] != 0; ++i) {
1884 struct ntreg_vk_record *vk =
1885 (struct ntreg_vk_record *) (h->addr + values[i]);
1888 len = le32toh (vk->data_len);
1889 if (len == 0x80000000) /* special case */
1893 if (len > 4) { /* non-inline, so remove data block */
1894 size_t data_offset = le32toh (vk->data_offset);
1895 data_offset += 0x1000;
1896 mark_block_unused (h, data_offset);
1899 /* remove vk record */
1900 mark_block_unused (h, values[i]);
1905 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
1906 nk->nr_values = htole32 (0);
1907 nk->vallist = htole32 (0xffffffff);
1913 hivex_commit (hive_h *h, const char *filename, int flags)
1925 filename = filename ? : h->filename;
1926 int fd = open (filename, O_WRONLY|O_CREAT|O_TRUNC|O_NOCTTY, 0666);
1930 /* Update the header fields. */
1931 uint32_t sequence = le32toh (h->hdr->sequence1);
1933 h->hdr->sequence1 = htole32 (sequence);
1934 h->hdr->sequence2 = htole32 (sequence);
1935 /* XXX Ought to update h->hdr->last_modified. */
1936 h->hdr->blocks = htole32 (h->endpages - 0x1000);
1938 /* Recompute header checksum. */
1939 uint32_t sum = header_checksum (h);
1940 h->hdr->csum = htole32 (sum);
1943 fprintf (stderr, "hivex_commit: new header checksum: 0x%x\n", sum);
1945 if (full_write (fd, h->addr, h->size) != h->size) {
1952 if (close (fd) == -1)
1959 hivex_node_set_values (hive_h *h, hive_node_h node,
1960 size_t nr_values, const hive_set_value *values,
1968 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
1973 /* Delete all existing values. */
1974 if (delete_values (h, node) == -1)
1980 /* Allocate value list node. Value lists have no id field. */
1981 static const char nul_id[2] = { 0, 0 };
1983 sizeof (struct ntreg_value_list) + (nr_values - 1) * sizeof (uint32_t);
1984 size_t vallist_offs = allocate_block (h, seg_len, nul_id);
1985 if (vallist_offs == 0)
1988 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
1989 nk->nr_values = htole32 (nr_values);
1990 nk->vallist = htole32 (vallist_offs - 0x1000);
1992 struct ntreg_value_list *vallist =
1993 (struct ntreg_value_list *) (h->addr + vallist_offs);
1996 for (i = 0; i < nr_values; ++i) {
1997 /* Allocate vk record to store this (key, value) pair. */
1998 static const char vk_id[2] = { 'v', 'k' };
1999 seg_len = sizeof (struct ntreg_vk_record) + strlen (values[i].key);
2000 size_t vk_offs = allocate_block (h, seg_len, vk_id);
2004 vallist->offset[i] = htole32 (vk_offs - 0x1000);
2006 struct ntreg_vk_record *vk = (struct ntreg_vk_record *) (h->addr + vk_offs);
2007 size_t name_len = strlen (values[i].key);
2008 vk->name_len = htole16 (name_len);
2009 strcpy (vk->name, values[i].key);
2010 vk->data_type = htole32 (values[i].t);
2011 vk->data_len = htole16 (values[i].len);
2012 vk->flags = name_len == 0 ? 0 : 1;
2014 if (values[i].len <= 4) /* Store data inline. */
2015 memcpy (&vk->data_offset, values[i].value, values[i].len);
2017 size_t offs = allocate_block (h, values[i].len + 4, nul_id);
2020 memcpy (h->addr + offs + 4, values[i].value, values[i].len);
2021 vk->data_offset = htole32 (offs - 0x1000);
2024 if (name_len * 2 > le32toh (nk->max_vk_name_len))
2025 nk->max_vk_name_len = htole32 (name_len * 2);
2026 if (values[i].len > le32toh (nk->max_vk_data_len))
2027 nk->max_vk_data_len = htole32 (values[i].len);