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