hivex: Store filename in hive handle.
[libguestfs.git] / hivex / hivex.c
1 /* hivex - Windows Registry "hive" extraction library.
2  * Copyright (C) 2009 Red Hat Inc.
3  * Derived from code by Petter Nordahl-Hagen under a compatible license:
4  *   Copyright (c) 1997-2007 Petter Nordahl-Hagen.
5  * Derived from code by Markus Stephany under a compatible license:
6  *   Copyright (c) 2000-2004, Markus Stephany.
7  *
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.
12  *
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.
17  *
18  * See file LICENSE for the full license.
19  */
20
21 #include <config.h>
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdint.h>
26 #include <stddef.h>
27 #include <inttypes.h>
28 #include <string.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31 #include <errno.h>
32 #include <iconv.h>
33 #include <sys/mman.h>
34 #include <sys/stat.h>
35 #include <assert.h>
36 #ifdef HAVE_ENDIAN_H
37 #include <endian.h>
38 #endif
39 #ifdef HAVE_BYTESWAP_H
40 #include <byteswap.h>
41 #endif
42
43 #define STREQ(a,b) (strcmp((a),(b)) == 0)
44 #define STRCASEEQ(a,b) (strcasecmp((a),(b)) == 0)
45 //#define STRNEQ(a,b) (strcmp((a),(b)) != 0)
46 //#define STRCASENEQ(a,b) (strcasecmp((a),(b)) != 0)
47 #define STREQLEN(a,b,n) (strncmp((a),(b),(n)) == 0)
48 //#define STRCASEEQLEN(a,b,n) (strncasecmp((a),(b),(n)) == 0)
49 //#define STRNEQLEN(a,b,n) (strncmp((a),(b),(n)) != 0)
50 //#define STRCASENEQLEN(a,b,n) (strncasecmp((a),(b),(n)) != 0)
51 //#define STRPREFIX(a,b) (strncmp((a),(b),strlen((b))) == 0)
52
53 #if __BYTE_ORDER == __LITTLE_ENDIAN
54 #ifndef be32toh
55 #define be32toh(x) __bswap_32 (x)
56 #endif
57 #ifndef be64toh
58 #define be64toh(x) __bswap_64 (x)
59 #endif
60 #ifndef le16toh
61 #define le16toh(x) (x)
62 #endif
63 #ifndef le32toh
64 #define le32toh(x) (x)
65 #endif
66 #ifndef le64toh
67 #define le64toh(x) (x)
68 #endif
69 #else
70 #ifndef be32toh
71 #define be32toh(x) (x)
72 #endif
73 #ifndef be64toh
74 #define be64toh(x) (x)
75 #endif
76 #ifndef le16toh
77 #define le16toh(x) __bswap_16 (x)
78 #endif
79 #ifndef le32toh
80 #define le32toh(x) __bswap_32 (x)
81 #endif
82 #ifndef le64toh
83 #define le64toh(x) __bswap_64 (x)
84 #endif
85 #endif
86
87 #include "hivex.h"
88
89 static char *windows_utf16_to_utf8 (/* const */ char *input, size_t len);
90
91 struct hive_h {
92   char *filename;
93   int fd;
94   size_t size;
95   int msglvl;
96
97   /* Memory-mapped (readonly) registry file. */
98   union {
99     char *addr;
100     struct ntreg_header *hdr;
101   };
102
103   /* Use a bitmap to store which file offsets are valid (point to a
104    * used block).  We only need to store 1 bit per 32 bits of the file
105    * (because blocks are 4-byte aligned).  We found that the average
106    * block size in a registry file is ~50 bytes.  So roughly 1 in 12
107    * bits in the bitmap will be set, making it likely a more efficient
108    * structure than a hash table.
109    */
110   char *bitmap;
111 #define BITMAP_SET(bitmap,off) (bitmap[(off)>>5] |= 1 << (((off)>>2)&7))
112 #define BITMAP_CLR(bitmap,off) (bitmap[(off)>>5] &= ~ (1 << (((off)>>2)&7)))
113 #define BITMAP_TST(bitmap,off) (bitmap[(off)>>5] & (1 << (((off)>>2)&7)))
114 #define IS_VALID_BLOCK(h,off)               \
115   (((off) & 3) == 0 &&                      \
116    (off) >= 0x1000 &&                       \
117    (off) < (h)->size &&                     \
118    BITMAP_TST((h)->bitmap,(off)))
119
120   /* Fields from the header, extracted from little-endianness hell. */
121   size_t rootoffs;              /* Root key offset (always an nk-block). */
122   size_t endpages;              /* Offset of end of pages. */
123
124   /* Stats. */
125   size_t pages;                 /* Number of hbin pages read. */
126   size_t blocks;                /* Total number of blocks found. */
127   size_t used_blocks;           /* Total number of used blocks found. */
128   size_t used_size;             /* Total size (bytes) of used blocks. */
129 };
130
131 /* NB. All fields are little endian. */
132 struct ntreg_header {
133   char magic[4];                /* "regf" */
134   uint32_t sequence1;
135   uint32_t sequence2;
136   char last_modified[8];
137   uint32_t major_ver;           /* 1 */
138   uint32_t minor_ver;           /* 3 */
139   uint32_t unknown5;            /* 0 */
140   uint32_t unknown6;            /* 1 */
141   uint32_t offset;              /* offset of root key record - 4KB */
142   uint32_t blocks;              /* pointer AFTER last hbin in file - 4KB */
143   uint32_t unknown7;            /* 1 */
144   /* 0x30 */
145   char name[64];                /* original file name of hive */
146   char unknown_guid1[16];
147   char unknown_guid2[16];
148   /* 0x90 */
149   uint32_t unknown8;
150   char unknown_guid3[16];
151   uint32_t unknown9;
152   /* 0xa8 */
153   char unknown10[340];
154   /* 0x1fc */
155   uint32_t csum;                /* checksum: xor of dwords 0-0x1fb. */
156   /* 0x200 */
157   char unknown11[3528];
158   /* 0xfc8 */
159   char unknown_guid4[16];
160   char unknown_guid5[16];
161   char unknown_guid6[16];
162   uint32_t unknown12;
163   uint32_t unknown13;
164   /* 0x1000 */
165 } __attribute__((__packed__));
166
167 struct ntreg_hbin_page {
168   char magic[4];                /* "hbin" */
169   uint32_t offset_first;        /* offset from 1st block */
170   uint32_t offset_next;         /* offset of next (relative to this) */
171   char unknown[20];
172   /* Linked list of blocks follows here. */
173 } __attribute__((__packed__));
174
175 struct ntreg_hbin_block {
176   int32_t seg_len;              /* length of this block (-ve for used block) */
177   char id[2];                   /* the block type (eg. "nk" for nk record) */
178   /* Block data follows here. */
179 } __attribute__((__packed__));
180
181 #define BLOCK_ID_EQ(h,offs,eqid) \
182   (STREQLEN (((struct ntreg_hbin_block *)((h)->addr + (offs)))->id, (eqid), 2))
183
184 static size_t
185 block_len (hive_h *h, size_t blkoff, int *used)
186 {
187   struct ntreg_hbin_block *block;
188   block = (struct ntreg_hbin_block *) (h->addr + blkoff);
189
190   int32_t len = le32toh (block->seg_len);
191   if (len < 0) {
192     if (used) *used = 1;
193     len = -len;
194   } else {
195     if (used) *used = 0;
196   }
197
198   return (size_t) len;
199 }
200
201 struct ntreg_nk_record {
202   int32_t seg_len;              /* length (always -ve because used) */
203   char id[2];                   /* "nk" */
204   uint16_t flags;
205   char timestamp[12];
206   uint32_t parent;              /* offset of owner/parent */
207   uint32_t nr_subkeys;          /* number of subkeys */
208   uint32_t unknown1;
209   uint32_t subkey_lf;           /* lf record containing list of subkeys */
210   uint32_t unknown2;
211   uint32_t nr_values;           /* number of values */
212   uint32_t vallist;             /* value-list record */
213   uint32_t sk;                  /* offset of sk-record */
214   uint32_t classname;           /* offset of classname record */
215   char unknown3[16];
216   uint32_t unknown4;
217   uint16_t name_len;            /* length of name */
218   uint16_t classname_len;       /* length of classname */
219   char name[1];                 /* name follows here */
220 } __attribute__((__packed__));
221
222 struct ntreg_lf_record {
223   int32_t seg_len;
224   char id[2];                   /* "lf" */
225   uint16_t nr_keys;             /* number of keys in this record */
226   struct {
227     uint32_t offset;            /* offset of nk-record for this subkey */
228     char name[4];               /* first 4 characters of subkey name */
229   } keys[1];
230 } __attribute__((__packed__));
231
232 struct ntreg_ri_record {
233   int32_t seg_len;
234   char id[2];                   /* "ri" */
235   uint16_t nr_offsets;          /* number of pointers to lh records */
236   uint32_t offset[1];           /* list of pointers to lh records */
237 } __attribute__((__packed__));
238
239 /* This has no ID header. */
240 struct ntreg_value_list {
241   int32_t seg_len;
242   uint32_t offset[1];           /* list of pointers to vk records */
243 } __attribute__((__packed__));
244
245 struct ntreg_vk_record {
246   int32_t seg_len;              /* length (always -ve because used) */
247   char id[2];                   /* "vk" */
248   uint16_t name_len;            /* length of name */
249   /* length of the data:
250    * If data_len is <= 4, then it's stored inline.
251    * If data_len is 0x80000000, then it's an inline dword.
252    * Top bit may be set or not set at random.
253    */
254   uint32_t data_len;
255   uint32_t data_offset;         /* pointer to the data (or data if inline) */
256   hive_type data_type;          /* type of the data */
257   uint16_t flags;               /* bit 0 set => key name ASCII,
258                                    bit 0 clr => key name UTF-16.
259                                    Only seen ASCII here in the wild. */
260   uint16_t unknown2;
261   char name[1];                 /* key name follows here */
262 } __attribute__((__packed__));
263
264 hive_h *
265 hivex_open (const char *filename, int flags)
266 {
267   hive_h *h = NULL;
268
269   assert (sizeof (struct ntreg_header) == 0x1000);
270   assert (offsetof (struct ntreg_header, csum) == 0x1fc);
271
272   h = calloc (1, sizeof *h);
273   if (h == NULL)
274     goto error;
275
276   h->msglvl = flags & HIVEX_OPEN_MSGLVL_MASK;
277
278   const char *debug = getenv ("HIVEX_DEBUG");
279   if (debug && STREQ (debug, "1"))
280     h->msglvl = 2;
281
282   if (h->msglvl >= 2)
283     fprintf (stderr, "hivex_open: created handle %p\n", h);
284
285   h->filename = strdup (filename);
286   if (h->filename == NULL)
287     goto error;
288
289   h->fd = open (filename, O_RDONLY);
290   if (h->fd == -1)
291     goto error;
292
293   struct stat statbuf;
294   if (fstat (h->fd, &statbuf) == -1)
295     goto error;
296
297   h->size = statbuf.st_size;
298
299   h->addr = mmap (NULL, h->size, PROT_READ, MAP_SHARED, h->fd, 0);
300   if (h->addr == MAP_FAILED)
301     goto error;
302
303   if (h->msglvl >= 2)
304     fprintf (stderr, "hivex_open: mapped file at %p\n", h->addr);
305
306   /* Check header. */
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",
312              filename);
313     errno = ENOTSUP;
314     goto error;
315   }
316
317   /* Check major version. */
318   uint32_t major_ver = le32toh (h->hdr->major_ver);
319   if (major_ver != 1) {
320     fprintf (stderr,
321              "hivex: %s: hive file major version %" PRIu32 " (expected 1)\n",
322              filename, major_ver);
323     errno = ENOTSUP;
324     goto error;
325   }
326
327   h->bitmap = calloc (1 + h->size / 32, 1);
328   if (h->bitmap == NULL)
329     goto error;
330
331   /* Header checksum. */
332   uint32_t *daddr = (uint32_t *) h->addr;
333   size_t i;
334   uint32_t sum = 0;
335   for (i = 0; i < 0x1fc / 4; ++i) {
336     sum ^= le32toh (*daddr);
337     daddr++;
338   }
339
340   if (sum != le32toh (h->hdr->csum)) {
341     fprintf (stderr, "hivex: %s: bad checksum in hive header\n", filename);
342     errno = EINVAL;
343     goto error;
344   }
345
346   if (h->msglvl >= 2) {
347     char *name = windows_utf16_to_utf8 (h->hdr->name, 64);
348
349     fprintf (stderr,
350              "hivex_open: header fields:\n"
351              "  file version             %" PRIu32 ".%" PRIu32 "\n"
352              "  sequence nos             %" PRIu32 " %" PRIu32 "\n"
353              "    (sequences nos should match if hive was synched at shutdown)\n"
354              "  original file name       %s\n"
355              "    (only 32 chars are stored, name is probably truncated)\n"
356              "  root offset              0x%x + 0x1000\n"
357              "  end of last page         0x%x + 0x1000 (total file size 0x%zx)\n"
358              "  checksum                 0x%x (calculated 0x%x)\n",
359              major_ver, le32toh (h->hdr->minor_ver),
360              le32toh (h->hdr->sequence1), le32toh (h->hdr->sequence2),
361              name ? name : "(conversion failed)",
362              le32toh (h->hdr->offset),
363              le32toh (h->hdr->blocks), h->size,
364              le32toh (h->hdr->csum), sum);
365     free (name);
366   }
367
368   h->rootoffs = le32toh (h->hdr->offset) + 0x1000;
369   h->endpages = le32toh (h->hdr->blocks) + 0x1000;
370
371   if (h->msglvl >= 2)
372     fprintf (stderr, "hivex_open: root offset = 0x%zx\n", h->rootoffs);
373
374   /* We'll set this flag when we see a block with the root offset (ie.
375    * the root block).
376    */
377   int seen_root_block = 0, bad_root_block = 0;
378
379   /* Read the pages and blocks.  The aim here is to be robust against
380    * corrupt or malicious registries.  So we make sure the loops
381    * always make forward progress.  We add the address of each block
382    * we read to a hash table so pointers will only reference the start
383    * of valid blocks.
384    */
385   size_t off;
386   struct ntreg_hbin_page *page;
387   for (off = 0x1000; off < h->size; off += le32toh (page->offset_next)) {
388     if (off >= h->endpages)
389       break;
390
391     page = (struct ntreg_hbin_page *) (h->addr + off);
392     if (page->magic[0] != 'h' ||
393         page->magic[1] != 'b' ||
394         page->magic[2] != 'i' ||
395         page->magic[3] != 'n') {
396       fprintf (stderr, "hivex: %s: trailing garbage at end of file (at 0x%zx, after %zu pages)\n",
397                filename, off, h->pages);
398       errno = ENOTSUP;
399       goto error;
400     }
401
402     if (h->msglvl >= 2)
403       fprintf (stderr, "hivex_open: page at 0x%zx\n", off);
404     h->pages++;
405
406     if (le32toh (page->offset_next) <= sizeof (struct ntreg_hbin_page) ||
407         (le32toh (page->offset_next) & 3) != 0) {
408       fprintf (stderr, "hivex: %s: pagesize %d at %zu, bad registry\n",
409                filename, le32toh (page->offset_next), off);
410       errno = ENOTSUP;
411       goto error;
412     }
413
414     /* Read the blocks in this page. */
415     size_t blkoff;
416     struct ntreg_hbin_block *block;
417     int32_t seg_len;
418     for (blkoff = off + 0x20;
419          blkoff < off + le32toh (page->offset_next);
420          blkoff += seg_len) {
421       h->blocks++;
422
423       int is_root = blkoff == h->rootoffs;
424       if (is_root)
425         seen_root_block = 1;
426
427       block = (struct ntreg_hbin_block *) (h->addr + blkoff);
428       int used;
429       seg_len = block_len (h, blkoff, &used);
430       if (seg_len <= 4 || (seg_len & 3) != 0) {
431         fprintf (stderr, "hivex: %s: block size %d at %zu, bad registry\n",
432                  filename, le32toh (block->seg_len), blkoff);
433         errno = ENOTSUP;
434         goto error;
435       }
436
437       if (h->msglvl >= 2)
438         fprintf (stderr, "hivex_open: %s block id %d,%d at 0x%zx%s\n",
439                  used ? "used" : "free", block->id[0], block->id[1], blkoff,
440                  is_root ? " (root)" : "");
441
442       if (is_root && !used)
443         bad_root_block = 1;
444
445       if (used) {
446         h->used_blocks++;
447         h->used_size += seg_len;
448
449         /* Root block must be an nk-block. */
450         if (is_root && (block->id[0] != 'n' || block->id[1] != 'k'))
451           bad_root_block = 1;
452
453         /* Note this blkoff is a valid address. */
454         BITMAP_SET (h->bitmap, blkoff);
455       }
456     }
457   }
458
459   if (!seen_root_block) {
460     fprintf (stderr, "hivex: %s: no root block found\n", filename);
461     errno = ENOTSUP;
462     goto error;
463   }
464
465   if (bad_root_block) {
466     fprintf (stderr, "hivex: %s: bad root block (free or not nk)\n", filename);
467     errno = ENOTSUP;
468     goto error;
469   }
470
471   if (h->msglvl >= 1)
472     fprintf (stderr,
473              "hivex_open: successfully read Windows Registry hive file:\n"
474              "  pages:                  %zu\n"
475              "  blocks:                 %zu\n"
476              "  blocks used:            %zu\n"
477              "  bytes used:             %zu\n",
478              h->pages, h->blocks, h->used_blocks, h->used_size);
479
480   return h;
481
482  error:;
483   int err = errno;
484   if (h) {
485     free (h->bitmap);
486     if (h->addr && h->size && h->addr != MAP_FAILED)
487       munmap (h->addr, h->size);
488     if (h->fd >= 0)
489       close (h->fd);
490     free (h->filename);
491     free (h);
492   }
493   errno = err;
494   return NULL;
495 }
496
497 int
498 hivex_close (hive_h *h)
499 {
500   int r;
501
502   free (h->bitmap);
503   munmap (h->addr, h->size);
504   r = close (h->fd);
505   free (h->filename);
506   free (h);
507
508   return r;
509 }
510
511 hive_node_h
512 hivex_root (hive_h *h)
513 {
514   hive_node_h ret = h->rootoffs;
515   if (!IS_VALID_BLOCK (h, ret)) {
516     errno = ENOKEY;
517     return 0;
518   }
519   return ret;
520 }
521
522 char *
523 hivex_node_name (hive_h *h, hive_node_h node)
524 {
525   if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
526     errno = EINVAL;
527     return NULL;
528   }
529
530   struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
531
532   /* AFAIK the node name is always plain ASCII, so no conversion
533    * to UTF-8 is necessary.  However we do need to nul-terminate
534    * the string.
535    */
536
537   /* nk->name_len is unsigned, 16 bit, so this is safe ...  However
538    * we have to make sure the length doesn't exceed the block length.
539    */
540   size_t len = le16toh (nk->name_len);
541   size_t seg_len = block_len (h, node, NULL);
542   if (sizeof (struct ntreg_nk_record) + len - 1 > seg_len) {
543     if (h->msglvl >= 2)
544       fprintf (stderr, "hivex_node_name: returning EFAULT because node name is too long (%zu, %zu)\n",
545               len, seg_len);
546     errno = EFAULT;
547     return NULL;
548   }
549
550   char *ret = malloc (len + 1);
551   if (ret == NULL)
552     return NULL;
553   memcpy (ret, nk->name, len);
554   ret[len] = '\0';
555   return ret;
556 }
557
558 #if 0
559 /* I think the documentation for the sk and classname fields in the nk
560  * record is wrong, or else the offset field is in the wrong place.
561  * Otherwise this makes no sense.  Disabled this for now -- it's not
562  * useful for reading the registry anyway.
563  */
564
565 hive_security_h
566 hivex_node_security (hive_h *h, hive_node_h node)
567 {
568   if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
569     errno = EINVAL;
570     return 0;
571   }
572
573   struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
574
575   hive_node_h ret = le32toh (nk->sk);
576   ret += 0x1000;
577   if (!IS_VALID_BLOCK (h, ret)) {
578     errno = EFAULT;
579     return 0;
580   }
581   return ret;
582 }
583
584 hive_classname_h
585 hivex_node_classname (hive_h *h, hive_node_h node)
586 {
587   if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
588     errno = EINVAL;
589     return 0;
590   }
591
592   struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
593
594   hive_node_h ret = le32toh (nk->classname);
595   ret += 0x1000;
596   if (!IS_VALID_BLOCK (h, ret)) {
597     errno = EFAULT;
598     return 0;
599   }
600   return ret;
601 }
602 #endif
603
604 hive_node_h *
605 hivex_node_children (hive_h *h, hive_node_h node)
606 {
607   if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
608     errno = EINVAL;
609     return NULL;
610   }
611
612   struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
613
614   size_t nr_subkeys_in_nk = le32toh (nk->nr_subkeys);
615
616   /* Deal with the common "no subkeys" case quickly. */
617   hive_node_h *ret;
618   if (nr_subkeys_in_nk == 0) {
619     ret = malloc (sizeof (hive_node_h));
620     if (ret == NULL)
621       return NULL;
622     ret[0] = 0;
623     return ret;
624   }
625
626   /* Arbitrarily limit the number of subkeys we will ever deal with. */
627   if (nr_subkeys_in_nk > 1000000) {
628     errno = ERANGE;
629     return NULL;
630   }
631
632   /* The subkey_lf field can point either to an lf-record, which is
633    * the common case, or if there are lots of subkeys, to an
634    * ri-record.
635    */
636   size_t subkey_lf = le32toh (nk->subkey_lf);
637   subkey_lf += 0x1000;
638   if (!IS_VALID_BLOCK (h, subkey_lf)) {
639     if (h->msglvl >= 2)
640       fprintf (stderr, "hivex_node_children: returning EFAULT because subkey_lf is not a valid block (%zu)\n",
641                subkey_lf);
642     errno = EFAULT;
643     return NULL;
644   }
645
646   struct ntreg_hbin_block *block =
647     (struct ntreg_hbin_block *) (h->addr + subkey_lf);
648
649   /* Points to lf-record?  (Note, also "lh" but that is basically the
650    * same as "lf" as far as we are concerned here).
651    */
652   if (block->id[0] == 'l' && (block->id[1] == 'f' || block->id[1] == 'h')) {
653     struct ntreg_lf_record *lf = (struct ntreg_lf_record *) block;
654
655     /* Check number of subkeys in the nk-record matches number of subkeys
656      * in the lf-record.
657      */
658     size_t nr_subkeys_in_lf = le16toh (lf->nr_keys);
659
660     if (h->msglvl >= 2)
661       fprintf (stderr, "hivex_node_children: nr_subkeys_in_nk = %zu, nr_subkeys_in_lf = %zu\n",
662                nr_subkeys_in_nk, nr_subkeys_in_lf);
663
664     if (nr_subkeys_in_nk != nr_subkeys_in_lf) {
665       errno = ENOTSUP;
666       return NULL;
667     }
668
669     size_t len = block_len (h, subkey_lf, NULL);
670     if (8 + nr_subkeys_in_lf * 8 > len) {
671       if (h->msglvl >= 2)
672         fprintf (stderr, "hivex_node_children: returning EFAULT because too many subkeys (%zu, %zu)\n",
673                  nr_subkeys_in_lf, len);
674       errno = EFAULT;
675       return NULL;
676     }
677
678     /* Allocate space for the returned values.  Note that
679      * nr_subkeys_in_lf is limited to a 16 bit value.
680      */
681     ret = malloc ((1 + nr_subkeys_in_lf) * sizeof (hive_node_h));
682     if (ret == NULL)
683       return NULL;
684
685     size_t i;
686     for (i = 0; i < nr_subkeys_in_lf; ++i) {
687       hive_node_h subkey = lf->keys[i].offset;
688       subkey += 0x1000;
689       if (!IS_VALID_BLOCK (h, subkey)) {
690         if (h->msglvl >= 2)
691           fprintf (stderr, "hivex_node_children: returning EFAULT because subkey is not a valid block (0x%zx)\n",
692                    subkey);
693         errno = EFAULT;
694         free (ret);
695         return NULL;
696       }
697       ret[i] = subkey;
698     }
699     ret[i] = 0;
700     return ret;
701   }
702   /* Points to ri-record? */
703   else if (block->id[0] == 'r' && block->id[1] == 'i') {
704     struct ntreg_ri_record *ri = (struct ntreg_ri_record *) block;
705
706     size_t nr_offsets = le16toh (ri->nr_offsets);
707
708     /* Count total number of children. */
709     size_t i, count = 0;
710     for (i = 0; i < nr_offsets; ++i) {
711       hive_node_h offset = ri->offset[i];
712       offset += 0x1000;
713       if (!IS_VALID_BLOCK (h, offset)) {
714         if (h->msglvl >= 2)
715           fprintf (stderr, "hivex_node_children: returning EFAULT because ri-offset is not a valid block (0x%zx)\n",
716                    offset);
717         errno = EFAULT;
718         return NULL;
719       }
720       if (!BLOCK_ID_EQ (h, offset, "lf") && !BLOCK_ID_EQ (h, offset, "lh")) {
721         errno = ENOTSUP;
722         return NULL;
723       }
724
725       struct ntreg_lf_record *lf =
726         (struct ntreg_lf_record *) (h->addr + offset);
727
728       count += le16toh (lf->nr_keys);
729     }
730
731     if (h->msglvl >= 2)
732       fprintf (stderr, "hivex_node_children: nr_subkeys_in_nk = %zu, counted = %zu\n",
733                nr_subkeys_in_nk, count);
734
735     if (nr_subkeys_in_nk != count) {
736       errno = ENOTSUP;
737       return NULL;
738     }
739
740     /* Copy list of children.  Note nr_subkeys_in_nk is limited to
741      * something reasonable above.
742      */
743     ret = malloc ((1 + nr_subkeys_in_nk) * sizeof (hive_node_h));
744     if (ret == NULL)
745       return NULL;
746
747     count = 0;
748     for (i = 0; i < nr_offsets; ++i) {
749       hive_node_h offset = ri->offset[i];
750       offset += 0x1000;
751       if (!IS_VALID_BLOCK (h, offset)) {
752         if (h->msglvl >= 2)
753           fprintf (stderr, "hivex_node_children: returning EFAULT because ri-offset is not a valid block (0x%zx)\n",
754                    offset);
755         errno = EFAULT;
756         return NULL;
757       }
758       if (!BLOCK_ID_EQ (h, offset, "lf") && !BLOCK_ID_EQ (h, offset, "lh")) {
759         errno = ENOTSUP;
760         return NULL;
761       }
762
763       struct ntreg_lf_record *lf =
764         (struct ntreg_lf_record *) (h->addr + offset);
765
766       size_t j;
767       for (j = 0; j < le16toh (lf->nr_keys); ++j) {
768         hive_node_h subkey = lf->keys[j].offset;
769         subkey += 0x1000;
770         if (!IS_VALID_BLOCK (h, subkey)) {
771           if (h->msglvl >= 2)
772             fprintf (stderr, "hivex_node_children: returning EFAULT because indirect subkey is not a valid block (0x%zx)\n",
773                      subkey);
774           errno = EFAULT;
775           free (ret);
776           return NULL;
777         }
778         ret[count++] = subkey;
779       }
780     }
781     ret[count] = 0;
782
783     return ret;
784   }
785   else {
786     errno = ENOTSUP;
787     return NULL;
788   }
789 }
790
791 /* Very inefficient, but at least having a separate API call
792  * allows us to make it more efficient in future.
793  */
794 hive_node_h
795 hivex_node_get_child (hive_h *h, hive_node_h node, const char *nname)
796 {
797   hive_node_h *children = NULL;
798   char *name = NULL;
799   hive_node_h ret = 0;
800
801   children = hivex_node_children (h, node);
802   if (!children) goto error;
803
804   size_t i;
805   for (i = 0; children[i] != 0; ++i) {
806     name = hivex_node_name (h, children[i]);
807     if (!name) goto error;
808     if (STRCASEEQ (name, nname)) {
809       ret = children[i];
810       break;
811     }
812     free (name); name = NULL;
813   }
814
815  error:
816   free (children);
817   free (name);
818   return ret;
819 }
820
821 hive_node_h
822 hivex_node_parent (hive_h *h, hive_node_h node)
823 {
824   if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
825     errno = EINVAL;
826     return 0;
827   }
828
829   struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
830
831   hive_node_h ret = le32toh (nk->parent);
832   ret += 0x1000;
833   if (!IS_VALID_BLOCK (h, ret)) {
834     if (h->msglvl >= 2)
835       fprintf (stderr, "hivex_node_parent: returning EFAULT because parent is not a valid block (0x%zx)\n",
836               ret);
837     errno = EFAULT;
838     return 0;
839   }
840   return ret;
841 }
842
843 hive_value_h *
844 hivex_node_values (hive_h *h, hive_node_h node)
845 {
846   if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
847     errno = EINVAL;
848     return 0;
849   }
850
851   struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
852
853   size_t nr_values = le32toh (nk->nr_values);
854
855   if (h->msglvl >= 2)
856     fprintf (stderr, "hivex_node_values: nr_values = %zu\n", nr_values);
857
858   /* Deal with the common "no values" case quickly. */
859   hive_node_h *ret;
860   if (nr_values == 0) {
861     ret = malloc (sizeof (hive_node_h));
862     if (ret == NULL)
863       return NULL;
864     ret[0] = 0;
865     return ret;
866   }
867
868   /* Arbitrarily limit the number of values we will ever deal with. */
869   if (nr_values > 100000) {
870     errno = ERANGE;
871     return NULL;
872   }
873
874   /* Get the value list and check it looks reasonable. */
875   size_t vlist_offset = le32toh (nk->vallist);
876   vlist_offset += 0x1000;
877   if (!IS_VALID_BLOCK (h, vlist_offset)) {
878     if (h->msglvl >= 2)
879       fprintf (stderr, "hivex_node_values: returning EFAULT because value list is not a valid block (0x%zx)\n",
880                vlist_offset);
881     errno = EFAULT;
882     return NULL;
883   }
884
885   struct ntreg_value_list *vlist =
886     (struct ntreg_value_list *) (h->addr + vlist_offset);
887
888   size_t len = block_len (h, vlist_offset, NULL);
889   if (4 + nr_values * 4 > len) {
890     if (h->msglvl >= 2)
891       fprintf (stderr, "hivex_node_values: returning EFAULT because value list is too long (%zu, %zu)\n",
892                nr_values, len);
893     errno = EFAULT;
894     return NULL;
895   }
896
897   /* Allocate return array and copy values in. */
898   ret = malloc ((1 + nr_values) * sizeof (hive_node_h));
899   if (ret == NULL)
900     return NULL;
901
902   size_t i;
903   for (i = 0; i < nr_values; ++i) {
904     hive_node_h value = vlist->offset[i];
905     value += 0x1000;
906     if (!IS_VALID_BLOCK (h, value)) {
907       if (h->msglvl >= 2)
908         fprintf (stderr, "hivex_node_values: returning EFAULT because value is not a valid block (0x%zx)\n",
909                  value);
910       errno = EFAULT;
911       free (ret);
912       return NULL;
913     }
914     ret[i] = value;
915   }
916
917   ret[i] = 0;
918   return ret;
919 }
920
921 /* Very inefficient, but at least having a separate API call
922  * allows us to make it more efficient in future.
923  */
924 hive_value_h
925 hivex_node_get_value (hive_h *h, hive_node_h node, const char *key)
926 {
927   hive_value_h *values = NULL;
928   char *name = NULL;
929   hive_value_h ret = 0;
930
931   values = hivex_node_values (h, node);
932   if (!values) goto error;
933
934   size_t i;
935   for (i = 0; values[i] != 0; ++i) {
936     name = hivex_value_key (h, values[i]);
937     if (!name) goto error;
938     if (STRCASEEQ (name, key)) {
939       ret = values[i];
940       break;
941     }
942     free (name); name = NULL;
943   }
944
945  error:
946   free (values);
947   free (name);
948   return ret;
949 }
950
951 char *
952 hivex_value_key (hive_h *h, hive_value_h value)
953 {
954   if (!IS_VALID_BLOCK (h, value) || !BLOCK_ID_EQ (h, value, "vk")) {
955     errno = EINVAL;
956     return 0;
957   }
958
959   struct ntreg_vk_record *vk = (struct ntreg_vk_record *) (h->addr + value);
960
961   /* AFAIK the key is always plain ASCII, so no conversion to UTF-8 is
962    * necessary.  However we do need to nul-terminate the string.
963    */
964
965   /* vk->name_len is unsigned, 16 bit, so this is safe ...  However
966    * we have to make sure the length doesn't exceed the block length.
967    */
968   size_t len = le16toh (vk->name_len);
969   size_t seg_len = block_len (h, value, NULL);
970   if (sizeof (struct ntreg_vk_record) + len - 1 > seg_len) {
971     if (h->msglvl >= 2)
972       fprintf (stderr, "hivex_value_key: returning EFAULT because key length is too long (%zu, %zu)\n",
973                len, seg_len);
974     errno = EFAULT;
975     return NULL;
976   }
977
978   char *ret = malloc (len + 1);
979   if (ret == NULL)
980     return NULL;
981   memcpy (ret, vk->name, len);
982   ret[len] = '\0';
983   return ret;
984 }
985
986 int
987 hivex_value_type (hive_h *h, hive_value_h value, hive_type *t, size_t *len)
988 {
989   if (!IS_VALID_BLOCK (h, value) || !BLOCK_ID_EQ (h, value, "vk")) {
990     errno = EINVAL;
991     return -1;
992   }
993
994   struct ntreg_vk_record *vk = (struct ntreg_vk_record *) (h->addr + value);
995
996   if (t)
997     *t = le32toh (vk->data_type);
998
999   if (len) {
1000     *len = le32toh (vk->data_len);
1001     if (*len == 0x80000000) {   /* special case */
1002       *len = 4;
1003       if (t) *t = hive_t_dword;
1004     }
1005     *len &= 0x7fffffff;
1006   }
1007
1008   return 0;
1009 }
1010
1011 char *
1012 hivex_value_value (hive_h *h, hive_value_h value,
1013                    hive_type *t_rtn, size_t *len_rtn)
1014 {
1015   if (!IS_VALID_BLOCK (h, value) || !BLOCK_ID_EQ (h, value, "vk")) {
1016     errno = EINVAL;
1017     return NULL;
1018   }
1019
1020   struct ntreg_vk_record *vk = (struct ntreg_vk_record *) (h->addr + value);
1021
1022   hive_type t;
1023   size_t len;
1024
1025   t = le32toh (vk->data_type);
1026
1027   len = le32toh (vk->data_len);
1028   if (len == 0x80000000) {      /* special case */
1029     len = 4;
1030     t = hive_t_dword;
1031   }
1032   len &= 0x7fffffff;
1033
1034   if (h->msglvl >= 2)
1035     fprintf (stderr, "hivex_value_value: value=0x%zx, t=%d, len=%zu\n",
1036              value, t, len);
1037
1038   if (t_rtn)
1039     *t_rtn = t;
1040   if (len_rtn)
1041     *len_rtn = len;
1042
1043   /* Arbitrarily limit the length that we will read. */
1044   if (len > 1000000) {
1045     errno = ERANGE;
1046     return NULL;
1047   }
1048
1049   char *ret = malloc (len);
1050   if (ret == NULL)
1051     return NULL;
1052
1053   /* If length is <= 4 it's always stored inline. */
1054   if (len <= 4) {
1055     memcpy (ret, (char *) &vk->data_offset, len);
1056     return ret;
1057   }
1058
1059   size_t data_offset = vk->data_offset;
1060   data_offset += 0x1000;
1061   if (!IS_VALID_BLOCK (h, data_offset)) {
1062     if (h->msglvl >= 2)
1063       fprintf (stderr, "hivex_value_value: returning EFAULT because data offset is not a valid block (0x%zx)\n",
1064                data_offset);
1065     errno = EFAULT;
1066     free (ret);
1067     return NULL;
1068   }
1069
1070   /* Check that the declared size isn't larger than the block its in. */
1071   size_t blen = block_len (h, data_offset, NULL);
1072   if (len > blen) {
1073     if (h->msglvl >= 2)
1074       fprintf (stderr, "hivex_value_value: returning EFAULT because data is longer than its block (data 0x%zx, data len %zu, block len %zu)\n",
1075                data_offset, len, blen);
1076     errno = EFAULT;
1077     free (ret);
1078     return NULL;
1079   }
1080
1081   char *data = h->addr + data_offset + 4;
1082   memcpy (ret, data, len);
1083   return ret;
1084 }
1085
1086 static char *
1087 windows_utf16_to_utf8 (/* const */ char *input, size_t len)
1088 {
1089   iconv_t ic = iconv_open ("UTF-8", "UTF-16");
1090   if (ic == (iconv_t) -1)
1091     return NULL;
1092
1093   /* iconv(3) has an insane interface ... */
1094
1095   /* Mostly UTF-8 will be smaller, so this is a good initial guess. */
1096   size_t outalloc = len;
1097
1098  again:;
1099   size_t inlen = len;
1100   size_t outlen = outalloc;
1101   char *out = malloc (outlen + 1);
1102   if (out == NULL) {
1103     int err = errno;
1104     iconv_close (ic);
1105     errno = err;
1106     return NULL;
1107   }
1108   char *inp = input;
1109   char *outp = out;
1110
1111   size_t r = iconv (ic, &inp, &inlen, &outp, &outlen);
1112   if (r == (size_t) -1) {
1113     if (errno == E2BIG) {
1114       size_t prev = outalloc;
1115       /* Try again with a larger output buffer. */
1116       free (out);
1117       outalloc *= 2;
1118       if (outalloc < prev)
1119         return NULL;
1120       goto again;
1121     }
1122     else {
1123       /* Else some conversion failure, eg. EILSEQ, EINVAL. */
1124       int err = errno;
1125       iconv_close (ic);
1126       free (out);
1127       errno = err;
1128       return NULL;
1129     }
1130   }
1131
1132   *outp = '\0';
1133   iconv_close (ic);
1134
1135   return out;
1136 }
1137
1138 char *
1139 hivex_value_string (hive_h *h, hive_value_h value)
1140 {
1141   hive_type t;
1142   size_t len;
1143   char *data = hivex_value_value (h, value, &t, &len);
1144
1145   if (data == NULL)
1146     return NULL;
1147
1148   if (t != hive_t_string && t != hive_t_expand_string && t != hive_t_link) {
1149     free (data);
1150     errno = EINVAL;
1151     return NULL;
1152   }
1153
1154   char *ret = windows_utf16_to_utf8 (data, len);
1155   free (data);
1156   if (ret == NULL)
1157     return NULL;
1158
1159   return ret;
1160 }
1161
1162 static void
1163 free_strings (char **argv)
1164 {
1165   if (argv) {
1166     size_t i;
1167
1168     for (i = 0; argv[i] != NULL; ++i)
1169       free (argv[i]);
1170     free (argv);
1171   }
1172 }
1173
1174 /* Get the length of a UTF-16 format string.  Handle the string as
1175  * pairs of bytes, looking for the first \0\0 pair.
1176  */
1177 static size_t
1178 utf16_string_len_in_bytes (const char *str)
1179 {
1180   size_t ret = 0;
1181
1182   while (str[0] || str[1]) {
1183     str += 2;
1184     ret += 2;
1185   }
1186
1187   return ret;
1188 }
1189
1190 /* http://blogs.msdn.com/oldnewthing/archive/2009/10/08/9904646.aspx */
1191 char **
1192 hivex_value_multiple_strings (hive_h *h, hive_value_h value)
1193 {
1194   hive_type t;
1195   size_t len;
1196   char *data = hivex_value_value (h, value, &t, &len);
1197
1198   if (data == NULL)
1199     return NULL;
1200
1201   if (t != hive_t_multiple_strings) {
1202     free (data);
1203     errno = EINVAL;
1204     return NULL;
1205   }
1206
1207   size_t nr_strings = 0;
1208   char **ret = malloc ((1 + nr_strings) * sizeof (char *));
1209   if (ret == NULL) {
1210     free (data);
1211     return NULL;
1212   }
1213   ret[0] = NULL;
1214
1215   char *p = data;
1216   size_t plen;
1217
1218   while (p < data + len && (plen = utf16_string_len_in_bytes (p)) > 0) {
1219     nr_strings++;
1220     char **ret2 = realloc (ret, (1 + nr_strings) * sizeof (char *));
1221     if (ret2 == NULL) {
1222       free_strings (ret);
1223       free (data);
1224       return NULL;
1225     }
1226     ret = ret2;
1227
1228     ret[nr_strings-1] = windows_utf16_to_utf8 (p, plen);
1229     ret[nr_strings] = NULL;
1230     if (ret[nr_strings-1] == NULL) {
1231       free_strings (ret);
1232       free (data);
1233       return NULL;
1234     }
1235
1236     p += plen + 2 /* skip over UTF-16 \0\0 at the end of this string */;
1237   }
1238
1239   free (data);
1240   return ret;
1241 }
1242
1243 int32_t
1244 hivex_value_dword (hive_h *h, hive_value_h value)
1245 {
1246   hive_type t;
1247   size_t len;
1248   char *data = hivex_value_value (h, value, &t, &len);
1249
1250   if (data == NULL)
1251     return -1;
1252
1253   if ((t != hive_t_dword && t != hive_t_dword_be) || len != 4) {
1254     free (data);
1255     errno = EINVAL;
1256     return -1;
1257   }
1258
1259   int32_t ret = *(int32_t*)data;
1260   free (data);
1261   if (t == hive_t_dword)        /* little endian */
1262     ret = le32toh (ret);
1263   else
1264     ret = be32toh (ret);
1265
1266   return ret;
1267 }
1268
1269 int64_t
1270 hivex_value_qword (hive_h *h, hive_value_h value)
1271 {
1272   hive_type t;
1273   size_t len;
1274   char *data = hivex_value_value (h, value, &t, &len);
1275
1276   if (data == NULL)
1277     return -1;
1278
1279   if (t != hive_t_qword || len != 8) {
1280     free (data);
1281     errno = EINVAL;
1282     return -1;
1283   }
1284
1285   int64_t ret = *(int64_t*)data;
1286   free (data);
1287   ret = le64toh (ret);          /* always little endian */
1288
1289   return ret;
1290 }
1291
1292 int
1293 hivex_visit (hive_h *h, const struct hivex_visitor *visitor, size_t len,
1294              void *opaque, int flags)
1295 {
1296   return hivex_visit_node (h, hivex_root (h), visitor, len, opaque, flags);
1297 }
1298
1299 static int hivex__visit_node (hive_h *h, hive_node_h node, const struct hivex_visitor *vtor, char *unvisited, void *opaque, int flags);
1300
1301 int
1302 hivex_visit_node (hive_h *h, hive_node_h node,
1303                   const struct hivex_visitor *visitor, size_t len, void *opaque,
1304                   int flags)
1305 {
1306   struct hivex_visitor vtor;
1307   memset (&vtor, 0, sizeof vtor);
1308
1309   /* Note that len might be larger *or smaller* than the expected size. */
1310   size_t copysize = len <= sizeof vtor ? len : sizeof vtor;
1311   memcpy (&vtor, visitor, copysize);
1312
1313   /* This bitmap records unvisited nodes, so we don't loop if the
1314    * registry contains cycles.
1315    */
1316   char *unvisited = malloc (1 + h->size / 32);
1317   if (unvisited == NULL)
1318     return -1;
1319   memcpy (unvisited, h->bitmap, 1 + h->size / 32);
1320
1321   int r = hivex__visit_node (h, node, &vtor, unvisited, opaque, flags);
1322   free (unvisited);
1323   return r;
1324 }
1325
1326 static int
1327 hivex__visit_node (hive_h *h, hive_node_h node,
1328                    const struct hivex_visitor *vtor, char *unvisited,
1329                    void *opaque, int flags)
1330 {
1331   int skip_bad = flags & HIVEX_VISIT_SKIP_BAD;
1332   char *name = NULL;
1333   hive_value_h *values = NULL;
1334   hive_node_h *children = NULL;
1335   char *key = NULL;
1336   char *str = NULL;
1337   char **strs = NULL;
1338   int i;
1339
1340   /* Return -1 on all callback errors.  However on internal errors,
1341    * check if skip_bad is set and suppress those errors if so.
1342    */
1343   int ret = -1;
1344
1345   if (!BITMAP_TST (unvisited, node)) {
1346     if (h->msglvl >= 2)
1347       fprintf (stderr, "hivex__visit_node: contains cycle: visited node 0x%zx already\n",
1348                node);
1349
1350     errno = ELOOP;
1351     return skip_bad ? 0 : -1;
1352   }
1353   BITMAP_CLR (unvisited, node);
1354
1355   name = hivex_node_name (h, node);
1356   if (!name) return skip_bad ? 0 : -1;
1357   if (vtor->node_start && vtor->node_start (h, opaque, node, name) == -1)
1358     goto error;
1359
1360   values = hivex_node_values (h, node);
1361   if (!values) {
1362     ret = skip_bad ? 0 : -1;
1363     goto error;
1364   }
1365
1366   for (i = 0; values[i] != 0; ++i) {
1367     hive_type t;
1368     size_t len;
1369
1370     if (hivex_value_type (h, values[i], &t, &len) == -1) {
1371       ret = skip_bad ? 0 : -1;
1372       goto error;
1373     }
1374
1375     key = hivex_value_key (h, values[i]);
1376     if (key == NULL) {
1377       ret = skip_bad ? 0 : -1;
1378       goto error;
1379     }
1380
1381     switch (t) {
1382     case hive_t_none:
1383       str = hivex_value_value (h, values[i], &t, &len);
1384       if (str == NULL) {
1385         ret = skip_bad ? 0 : -1;
1386         goto error;
1387       }
1388       if (t != hive_t_none) {
1389         ret = skip_bad ? 0 : -1;
1390         goto error;
1391       }
1392       if (vtor->value_none &&
1393           vtor->value_none (h, opaque, node, values[i], t, len, key, str) == -1)
1394         goto error;
1395       free (str); str = NULL;
1396       break;
1397
1398     case hive_t_string:
1399     case hive_t_expand_string:
1400     case hive_t_link:
1401       str = hivex_value_string (h, values[i]);
1402       if (str == NULL) {
1403         if (errno != EILSEQ && errno != EINVAL) {
1404           ret = skip_bad ? 0 : -1;
1405           goto error;
1406         }
1407         if (vtor->value_string_invalid_utf16) {
1408           str = hivex_value_value (h, values[i], &t, &len);
1409           if (vtor->value_string_invalid_utf16 (h, opaque, node, values[i], t, len, key, str) == -1)
1410             goto error;
1411           free (str); str = NULL;
1412         }
1413         break;
1414       }
1415       if (vtor->value_string &&
1416           vtor->value_string (h, opaque, node, values[i], t, len, key, str) == -1)
1417         goto error;
1418       free (str); str = NULL;
1419       break;
1420
1421     case hive_t_dword:
1422     case hive_t_dword_be: {
1423       int32_t i32 = hivex_value_dword (h, values[i]);
1424       if (vtor->value_dword &&
1425           vtor->value_dword (h, opaque, node, values[i], t, len, key, i32) == -1)
1426         goto error;
1427       break;
1428     }
1429
1430     case hive_t_qword: {
1431       int64_t i64 = hivex_value_qword (h, values[i]);
1432       if (vtor->value_qword &&
1433           vtor->value_qword (h, opaque, node, values[i], t, len, key, i64) == -1)
1434         goto error;
1435       break;
1436     }
1437
1438     case hive_t_binary:
1439       str = hivex_value_value (h, values[i], &t, &len);
1440       if (str == NULL) {
1441         ret = skip_bad ? 0 : -1;
1442         goto error;
1443       }
1444       if (t != hive_t_binary) {
1445         ret = skip_bad ? 0 : -1;
1446         goto error;
1447       }
1448       if (vtor->value_binary &&
1449           vtor->value_binary (h, opaque, node, values[i], t, len, key, str) == -1)
1450         goto error;
1451       free (str); str = NULL;
1452       break;
1453
1454     case hive_t_multiple_strings:
1455       strs = hivex_value_multiple_strings (h, values[i]);
1456       if (strs == NULL) {
1457         if (errno != EILSEQ && errno != EINVAL) {
1458           ret = skip_bad ? 0 : -1;
1459           goto error;
1460         }
1461         if (vtor->value_string_invalid_utf16) {
1462           str = hivex_value_value (h, values[i], &t, &len);
1463           if (vtor->value_string_invalid_utf16 (h, opaque, node, values[i], t, len, key, str) == -1)
1464             goto error;
1465           free (str); str = NULL;
1466         }
1467         break;
1468       }
1469       if (vtor->value_multiple_strings &&
1470           vtor->value_multiple_strings (h, opaque, node, values[i], t, len, key, strs) == -1)
1471         goto error;
1472       free_strings (strs); strs = NULL;
1473       break;
1474
1475     case hive_t_resource_list:
1476     case hive_t_full_resource_description:
1477     case hive_t_resource_requirements_list:
1478     default:
1479       str = hivex_value_value (h, values[i], &t, &len);
1480       if (str == NULL) {
1481         ret = skip_bad ? 0 : -1;
1482         goto error;
1483       }
1484       if (vtor->value_other &&
1485           vtor->value_other (h, opaque, node, values[i], t, len, key, str) == -1)
1486         goto error;
1487       free (str); str = NULL;
1488       break;
1489     }
1490
1491     free (key); key = NULL;
1492   }
1493
1494   children = hivex_node_children (h, node);
1495   if (children == NULL) {
1496     ret = skip_bad ? 0 : -1;
1497     goto error;
1498   }
1499
1500   for (i = 0; children[i] != 0; ++i) {
1501     if (h->msglvl >= 2)
1502       fprintf (stderr, "hivex__visit_node: %s: visiting subkey %d (0x%zx)\n",
1503                name, i, children[i]);
1504
1505     if (hivex__visit_node (h, children[i], vtor, unvisited, opaque, flags) == -1)
1506       goto error;
1507   }
1508
1509   if (vtor->node_end && vtor->node_end (h, opaque, node, name) == -1)
1510     goto error;
1511
1512   ret = 0;
1513
1514  error:
1515   free (name);
1516   free (values);
1517   free (children);
1518   free (key);
1519   free (str);
1520   free_strings (strs);
1521   return ret;
1522 }