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