hivex: Documentation update.
[libguestfs.git] / hivex / hivexsh.c
1 /* hivexsh - Hive shell.
2  * Copyright (C) 2009 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdint.h>
25 #include <inttypes.h>
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include <assert.h>
29 #include <errno.h>
30
31 #ifdef HAVE_LIBREADLINE
32 #include <readline/readline.h>
33 #include <readline/history.h>
34 #endif
35
36 #ifdef HAVE_GETTEXT
37 #include "gettext.h"
38 #define _(str) dgettext(PACKAGE, (str))
39 //#define N_(str) dgettext(PACKAGE, (str))
40 #else
41 #define _(str) str
42 //#define N_(str) str
43 #endif
44
45 #define STREQ(a,b) (strcmp((a),(b)) == 0)
46 #define STRCASEEQ(a,b) (strcasecmp((a),(b)) == 0)
47 #define STRNEQ(a,b) (strcmp((a),(b)) != 0)
48 //#define STRCASENEQ(a,b) (strcasecmp((a),(b)) != 0)
49 //#define STREQLEN(a,b,n) (strncmp((a),(b),(n)) == 0)
50 //#define STRCASEEQLEN(a,b,n) (strncasecmp((a),(b),(n)) == 0)
51 //#define STRNEQLEN(a,b,n) (strncmp((a),(b),(n)) != 0)
52 //#define STRCASENEQLEN(a,b,n) (strncasecmp((a),(b),(n)) != 0)
53 #define STRPREFIX(a,b) (strncmp((a),(b),strlen((b))) == 0)
54
55 #include "c-ctype.h"
56 #include "xstrtol.h"
57
58 #include "hivex.h"
59 #include "byte_conversions.h"
60
61 static int quit = 0;
62 static int is_tty;
63 static hive_h *h = NULL;
64 static char *prompt_string = NULL; /* Normal prompt string. */
65 static char *loaded = NULL;     /* Basename of loaded file, if any. */
66 static hive_node_h cwd;         /* Current node. */
67 static int open_flags = 0;      /* Flags used when loading a hive file. */
68
69 static void usage (void) __attribute__((noreturn));
70 static void print_node_path (hive_node_h, FILE *);
71 static void set_prompt_string (void);
72 static void initialize_readline (void);
73 static void cleanup_readline (void);
74 static void add_history_line (const char *);
75 static char *rl_gets (const char *prompt_string);
76 static void sort_strings (char **strings, int len);
77 static int get_xdigit (char c);
78 static int dispatch (char *cmd, char *args);
79 static int cmd_cd (char *path);
80 static int cmd_close (char *path);
81 static int cmd_commit (char *path);
82 static int cmd_del (char *args);
83 static int cmd_help (char *args);
84 static int cmd_load (char *hivefile);
85 static int cmd_ls (char *args);
86 static int cmd_lsval (char *args);
87 static int cmd_setval (char *args);
88
89 static void
90 usage (void)
91 {
92   fprintf (stderr, "hivexsh [-dfw] [hivefile]\n");
93   exit (EXIT_FAILURE);
94 }
95
96 int
97 main (int argc, char *argv[])
98 {
99   setlocale (LC_ALL, "");
100   bindtextdomain (PACKAGE, LOCALEBASEDIR);
101   textdomain (PACKAGE);
102
103   int c;
104   const char *filename = NULL;
105
106   set_prompt_string ();
107
108   while ((c = getopt (argc, argv, "dfw")) != EOF) {
109     switch (c) {
110     case 'd':
111       open_flags |= HIVEX_OPEN_DEBUG;
112       break;
113     case 'f':
114       filename = optarg;
115       break;
116     case 'w':
117       open_flags |= HIVEX_OPEN_WRITE;
118       break;
119     default:
120       usage ();
121     }
122   }
123
124   if (optind < argc) {
125     if (optind + 1 != argc)
126       usage ();
127     if (cmd_load (argv[optind]) == -1)
128       exit (EXIT_FAILURE);
129   }
130
131   /* -f filename parameter */
132   if (filename) {
133     close (0);
134     if (open (filename, O_RDONLY) == -1) {
135       perror (filename);
136       exit (EXIT_FAILURE);
137     }
138   }
139
140   /* Main loop. */
141   is_tty = isatty (0);
142   initialize_readline ();
143
144   if (is_tty)
145     printf (_(
146 "\n"
147 "Welcome to hivexsh, the hivex interactive shell for examining\n"
148 "Windows Registry binary hive files.\n"
149 "\n"
150 "Type: 'help' for help summary\n"
151 "      'quit' to quit the shell\n"
152 "\n"));
153
154   while (!quit) {
155     char *buf = rl_gets (prompt_string);
156     if (!buf) {
157       quit = 1;
158       if (is_tty)
159         printf ("\n");
160       break;
161     }
162
163     while (*buf && c_isspace (*buf))
164       buf++;
165
166     /* Ignore blank line. */
167     if (!*buf) continue;
168
169     /* If the next character is '#' then this is a comment. */
170     if (*buf == '#') continue;
171
172     /* Parsing is very simple - much simpler than guestfish.  This is
173      * because Registry keys often contain spaces, and we don't want
174      * to bother with quoting.  Therefore here we just split at the
175      * first whitespace into "cmd<whitespace>arg(s)".  We let the
176      * command decide how to deal with arg(s), if at all.
177      */
178     size_t len = strcspn (buf, " \t");
179
180     if (len == 0) continue;
181
182     char *cmd = buf;
183     char *args;
184     size_t i = 0;
185
186     if (buf[len] == '\0') {
187       /* This is mostly safe.  Although the cmd_* functions do sometimes
188        * modify args, then shouldn't do so when args is "".
189        */
190       args = (char *) "";
191       goto got_command;
192     }
193
194     buf[len] = '\0';
195     args = buf + len + 1 + strspn (&buf[len+1], " \t");
196
197     len = strlen (args);
198     while (len > 0 && c_isspace (args[len-1])) {
199       args[len-1] = '\0';
200       len--;
201     }
202
203   got_command:
204     /*printf ("command: '%s'  args: '%s'\n", cmd, args)*/;
205     int r = dispatch (cmd, args);
206     if (!is_tty && r == -1)
207       exit (EXIT_FAILURE);
208   }
209
210   cleanup_readline ();
211   free (prompt_string);
212   free (loaded);
213   if (h) hivex_close (h);
214   exit (0);
215 }
216
217 /* Set the prompt string.  This is called whenever it could change, eg.
218  * after loading a file or changing directory.
219  */
220 static void
221 set_prompt_string (void)
222 {
223   free (prompt_string);
224   prompt_string = NULL;
225
226   FILE *fp;
227   char *ptr;
228   size_t size;
229   fp = open_memstream (&ptr, &size);
230   if (fp == NULL) {
231     perror ("open_memstream");
232     exit (EXIT_FAILURE);
233   }
234
235   if (h) {
236     assert (loaded != NULL);
237     assert (cwd != 0);
238
239     fputs (loaded, fp);
240     print_node_path (cwd, fp);
241   }
242
243   fprintf (fp, "> ");
244   fclose (fp);
245   prompt_string = ptr;
246 }
247
248 /* Print the \full\path of a node. */
249 static void
250 print_node_path (hive_node_h node, FILE *fp)
251 {
252   hive_node_h root = hivex_root (h);
253
254   if (node == root) {
255     fputc ('\\', fp);
256     return;
257   }
258
259   hive_node_h parent = hivex_node_parent (h, node);
260   if (parent == 0) {
261     fprintf (stderr, _("hivexsh: error getting parent of node %zu\n"), node);
262     return;
263   }
264   print_node_path (parent, fp);
265
266   if (parent != root)
267     fputc ('\\', fp);
268
269   char *name = hivex_node_name (h, node);
270   if (name == NULL) {
271     fprintf (stderr, _("hivexsh: error getting node name of node %zx\n"), node);
272     return;
273   }
274
275   fputs (name, fp);
276   free (name);
277 }
278
279 static char *line_read = NULL;
280
281 static char *
282 rl_gets (const char *prompt_string)
283 {
284 #ifdef HAVE_LIBREADLINE
285
286   if (is_tty) {
287     if (line_read) {
288       free (line_read);
289       line_read = NULL;
290     }
291
292     line_read = readline (prompt_string);
293
294     if (line_read && *line_read)
295       add_history_line (line_read);
296
297     return line_read;
298   }
299
300 #endif /* HAVE_LIBREADLINE */
301
302   static char buf[8192];
303   int len;
304
305   if (is_tty)
306     printf ("%s", prompt_string);
307   line_read = fgets (buf, sizeof buf, stdin);
308
309   if (line_read) {
310     len = strlen (line_read);
311     if (len > 0 && buf[len-1] == '\n') buf[len-1] = '\0';
312   }
313
314   return line_read;
315 }
316
317 #ifdef HAVE_LIBREADLINE
318 static char histfile[1024];
319 static int nr_history_lines = 0;
320 #endif
321
322 static void
323 initialize_readline (void)
324 {
325 #ifdef HAVE_LIBREADLINE
326   const char *home;
327
328   home = getenv ("HOME");
329   if (home) {
330     snprintf (histfile, sizeof histfile, "%s/.hivexsh", home);
331     using_history ();
332     (void) read_history (histfile);
333   }
334
335   rl_readline_name = "hivexsh";
336 #endif
337 }
338
339 static void
340 cleanup_readline (void)
341 {
342 #ifdef HAVE_LIBREADLINE
343   int fd;
344
345   if (histfile[0] != '\0') {
346     fd = open (histfile, O_WRONLY|O_CREAT, 0644);
347     if (fd == -1) {
348       perror (histfile);
349       return;
350     }
351     close (fd);
352
353     (void) append_history (nr_history_lines, histfile);
354   }
355 #endif
356 }
357
358 static void
359 add_history_line (const char *line)
360 {
361 #ifdef HAVE_LIBREADLINE
362   add_history (line);
363   nr_history_lines++;
364 #endif
365 }
366
367 static int
368 compare (const void *vp1, const void *vp2)
369 {
370   char * const *p1 = (char * const *) vp1;
371   char * const *p2 = (char * const *) vp2;
372   return strcasecmp (*p1, *p2);
373 }
374
375 static void
376 sort_strings (char **strings, int len)
377 {
378   qsort (strings, len, sizeof (char *), compare);
379 }
380
381 static int
382 get_xdigit (char c)
383 {
384   switch (c) {
385   case '0'...'9': return c - '0';
386   case 'a'...'f': return c - 'a' + 10;
387   case 'A'...'F': return c - 'A' + 10;
388   default: return -1;
389   }
390 }
391
392 static int
393 dispatch (char *cmd, char *args)
394 {
395   if (STRCASEEQ (cmd, "help"))
396     return cmd_help (args);
397   else if (STRCASEEQ (cmd, "load"))
398     return cmd_load (args);
399   else if (STRCASEEQ (cmd, "exit") ||
400            STRCASEEQ (cmd, "q") ||
401            STRCASEEQ (cmd, "quit")) {
402     quit = 1;
403     return 0;
404   }
405
406   /* If no hive file is loaded (!h) then only the small selection of
407    * commands above will work.
408    */
409   if (!h) {
410     fprintf (stderr, _("hivexsh: you must load a hive file first using 'load hivefile'\n"));
411     return -1;
412   }
413
414   if (STRCASEEQ (cmd, "cd"))
415     return cmd_cd (args);
416   else if (STRCASEEQ (cmd, "close") || STRCASEEQ (cmd, "unload"))
417     return cmd_close (args);
418   else if (STRCASEEQ (cmd, "commit"))
419     return cmd_commit (args);
420   else if (STRCASEEQ (cmd, "del"))
421     return cmd_del (args);
422   else if (STRCASEEQ (cmd, "ls"))
423     return cmd_ls (args);
424   else if (STRCASEEQ (cmd, "lsval"))
425     return cmd_lsval (args);
426   else if (STRCASEEQ (cmd, "setval"))
427     return cmd_setval (args);
428   else {
429     fprintf (stderr, _("hivexsh: unknown command '%s', use 'help' for help summary\n"),
430              cmd);
431     return -1;
432   }
433 }
434
435 static int
436 cmd_load (char *hivefile)
437 {
438   if (STREQ (hivefile, "")) {
439     fprintf (stderr, _("hivexsh: load: no hive file name given to load\n"));
440     return -1;
441   }
442
443   if (h) hivex_close (h);
444   h = NULL;
445
446   free (loaded);
447   loaded = NULL;
448
449   cwd = 0;
450
451   h = hivex_open (hivefile, open_flags);
452   if (h == NULL) {
453     fprintf (stderr,
454              _(
455 "hivexsh: failed to open hive file: %s: %m\n"
456 "\n"
457 "If you think this file is a valid Windows binary hive file (_not_\n"
458 "a regedit *.reg file) then please run this command again using the\n"
459 "hivexsh option '-d' and attach the complete output _and_ the hive file\n"
460 "which fails into a bug report at https://bugzilla.redhat.com/\n"
461 "\n"),
462              hivefile);
463     return -1;
464   }
465
466   /* Get the basename of the file for the prompt. */
467   char *p = strrchr (hivefile, '/');
468   if (p)
469     loaded = strdup (p+1);
470   else
471     loaded = strdup (hivefile);
472   if (!loaded) {
473     perror ("strdup");
474     exit (EXIT_FAILURE);
475   }
476
477   cwd = hivex_root (h);
478
479   set_prompt_string ();
480
481   return 0;
482 }
483
484 static int
485 cmd_close (char *args)
486 {
487   if (STRNEQ (args, "")) {
488     fprintf (stderr, _("hivexsh: '%s' command should not be given arguments\n"),
489              "close");
490     return -1;
491   }
492
493   if (h) hivex_close (h);
494   h = NULL;
495
496   free (loaded);
497   loaded = NULL;
498
499   cwd = 0;
500
501   set_prompt_string ();
502
503   return 0;
504 }
505
506 static int
507 cmd_commit (char *path)
508 {
509   if (STREQ (path, ""))
510     path = NULL;
511
512   if (hivex_commit (h, path, 0) == -1) {
513     perror ("hivexsh: commit");
514     return -1;
515   }
516
517   return 0;
518 }
519
520 static int
521 cmd_cd (char *path)
522 {
523   if (STREQ (path, "")) {
524     print_node_path (cwd, stdout);
525     fputc ('\n', stdout);
526     return 0;
527   }
528
529   if (path[0] == '\\' && path[1] == '\\') {
530     fprintf (stderr, _("%s: %s: \\ characters in path are doubled - are you escaping the path parameter correctly?\n"), "hivexsh", path);
531     return -1;
532   }
533
534   hive_node_h new_cwd = cwd;
535   hive_node_h root = hivex_root (h);
536
537   if (path[0] == '\\') {
538     new_cwd = root;
539     path++;
540   }
541
542   while (path[0]) {
543     size_t len = strcspn (path, "\\");
544     if (len == 0) {
545       path++;
546       continue;
547     }
548
549     char *elem = path;
550     path = path[len] == '\0' ? &path[len] : &path[len+1];
551     elem[len] = '\0';
552
553     if (len == 1 && STREQ (elem, "."))
554       continue;
555
556     if (len == 2 && STREQ (elem, "..")) {
557       if (new_cwd != root)
558         new_cwd = hivex_node_parent (h, new_cwd);
559       continue;
560     }
561
562     new_cwd = hivex_node_get_child (h, new_cwd, elem);
563     if (new_cwd == 0) {
564       fprintf (stderr, _("hivexsh: cd: subkey '%s' not found\n"),
565                elem);
566       return -1;
567     }
568   }
569
570   if (new_cwd != cwd) {
571     cwd = new_cwd;
572     set_prompt_string ();
573   }
574
575   return 0;
576 }
577
578 static int
579 cmd_help (char *args)
580 {
581   printf (_(
582 "Navigate through the hive's keys using the 'cd' command, as if it\n"
583 "contained a filesystem, and use 'ls' to list the subkeys of the\n"
584 "current key.  Full documentation is in the hivexsh(1) manual page.\n"));
585
586   return 0;
587 }
588
589 static int
590 cmd_ls (char *args)
591 {
592   if (STRNEQ (args, "")) {
593     fprintf (stderr, _("hivexsh: '%s' command should not be given arguments\n"),
594              "ls");
595     return -1;
596   }
597
598   /* Get the subkeys. */
599   hive_node_h *children = hivex_node_children (h, cwd);
600   if (children == NULL) {
601     perror ("ls");
602     return -1;
603   }
604
605   /* Get names for each subkey. */
606   size_t len;
607   for (len = 0; children[len] != 0; ++len)
608     ;
609
610   char **names = calloc (len, sizeof (char *));
611   if (names == NULL) {
612     perror ("malloc");
613     exit (EXIT_FAILURE);
614   }
615
616   int ret = -1;
617   size_t i;
618   for (i = 0; i < len; ++i) {
619     names[i] = hivex_node_name (h, children[i]);
620     if (names[i] == NULL) {
621       perror ("hivex_node_name");
622       goto error;
623     }
624   }
625
626   /* Sort the names. */
627   sort_strings (names, len);
628
629   for (i = 0; i < len; ++i)
630     printf ("%s\n", names[i]);
631
632   ret = 0;
633  error:
634   free (children);
635   for (i = 0; i < len; ++i)
636     free (names[i]);
637   free (names);
638   return ret;
639 }
640
641 static int
642 cmd_lsval (char *key)
643 {
644   if (STRNEQ (key, "")) {
645     hive_value_h value;
646
647     errno = 0;
648     if (STREQ (key, "@"))       /* default key written as "@" */
649       value = hivex_node_get_value (h, cwd, "");
650     else
651       value = hivex_node_get_value (h, cwd, key);
652
653     if (value == 0) {
654       if (errno)
655         goto error;
656       /* else key not found */
657       fprintf (stderr, _("%s: %s: key not found\n"), "hivexsh", key);
658       return -1;
659     }
660
661     /* Print the value. */
662     hive_type t;
663     size_t len;
664     if (hivex_value_type (h, value, &t, &len) == -1)
665       goto error;
666
667     switch (t) {
668     case hive_t_string:
669     case hive_t_expand_string:
670     case hive_t_link: {
671       char *str = hivex_value_string (h, value);
672       if (!str)
673         goto error;
674
675       puts (str); /* note: this adds a single \n character */
676       free (str);
677       break;
678     }
679
680     case hive_t_dword:
681     case hive_t_dword_be: {
682       int32_t j = hivex_value_dword (h, value);
683       printf ("%" PRIi32 "\n", j);
684       break;
685     }
686
687     case hive_t_qword: {
688       int64_t j = hivex_value_qword (h, value);
689       printf ("%" PRIi64 "\n", j);
690       break;
691     }
692
693     case hive_t_multiple_strings: {
694       char **strs = hivex_value_multiple_strings (h, value);
695       if (!strs)
696         goto error;
697       size_t j;
698       for (j = 0; strs[j] != NULL; ++j) {
699         puts (strs[j]);
700         free (strs[j]);
701       }
702       free (strs);
703       break;
704     }
705
706     case hive_t_none:
707     case hive_t_binary:
708     case hive_t_resource_list:
709     case hive_t_full_resource_description:
710     case hive_t_resource_requirements_list:
711     default: {
712       char *data = hivex_value_value (h, value, &t, &len);
713       if (!data)
714         goto error;
715
716       if (fwrite (data, 1, len, stdout) != len)
717         goto error;
718
719       free (data);
720       break;
721     }
722     } /* switch */
723   } else {
724     /* No key specified, so print all keys in this node.  We do this
725      * in a format which looks like the output of regedit, although
726      * this isn't a particularly useful format.
727      */
728     hive_value_h *values;
729
730     values = hivex_node_values (h, cwd);
731     if (values == NULL)
732       goto error;
733
734     size_t i;
735     for (i = 0; values[i] != 0; ++i) {
736       char *key = hivex_value_key (h, values[i]);
737       if (!key) goto error;
738
739       if (*key) {
740         putchar ('"');
741         size_t j;
742         for (j = 0; key[j] != 0; ++j) {
743           if (key[j] == '"' || key[j] == '\\')
744             putchar ('\\');
745           putchar (key[j]);
746         }
747         putchar ('"');
748       } else
749         printf ("\"@\"");       /* default key in regedit files */
750       putchar ('=');
751       free (key);
752
753       hive_type t;
754       size_t len;
755       if (hivex_value_type (h, values[i], &t, &len) == -1)
756         goto error;
757
758       switch (t) {
759       case hive_t_string:
760       case hive_t_expand_string:
761       case hive_t_link: {
762         char *str = hivex_value_string (h, values[i]);
763         if (!str)
764           goto error;
765
766         if (t != hive_t_string)
767           printf ("str(%d):", t);
768         putchar ('"');
769         size_t j;
770         for (j = 0; str[j] != 0; ++j) {
771           if (str[j] == '"' || str[j] == '\\')
772             putchar ('\\');
773           putchar (str[j]);
774         }
775         putchar ('"');
776         free (str);
777         break;
778       }
779
780       case hive_t_dword:
781       case hive_t_dword_be: {
782         int32_t j = hivex_value_dword (h, values[i]);
783         printf ("dword:%08" PRIx32 "\"", j);
784         break;
785       }
786
787       case hive_t_qword: /* sic */
788       case hive_t_none:
789       case hive_t_binary:
790       case hive_t_multiple_strings:
791       case hive_t_resource_list:
792       case hive_t_full_resource_description:
793       case hive_t_resource_requirements_list:
794       default: {
795         char *data = hivex_value_value (h, values[i], &t, &len);
796         if (!data)
797           goto error;
798
799         printf ("hex(%d):", t);
800         size_t j;
801         for (j = 0; j < len; ++j) {
802           if (j > 0)
803             putchar (',');
804           printf ("%02x", data[j]);
805         }
806         break;
807       }
808       } /* switch */
809
810       putchar ('\n');
811     } /* for */
812
813     free (values);
814   }
815
816   return 0;
817
818  error:
819   perror ("hivexsh: lsval");
820   return -1;
821 }
822
823 static int
824 cmd_setval (char *nrvals_str)
825 {
826   strtol_error xerr;
827
828   /* Parse number of values. */
829   long nrvals;
830   xerr = xstrtol (nrvals_str, NULL, 0, &nrvals, "");
831   if (xerr != LONGINT_OK) {
832     fprintf (stderr, _("%s: %s: invalid integer parameter (%s returned %d)\n"),
833              "setval", "nrvals", "xstrtol", xerr);
834     return -1;
835   }
836   if (nrvals < 0 || nrvals > 1000) {
837     fprintf (stderr, _("%s: %s: integer out of range\n"),
838              "setval", "nrvals");
839     return -1;
840   }
841
842   struct hive_set_value *values =
843     calloc (nrvals, sizeof (struct hive_set_value));
844   if (values == NULL) {
845     perror ("calloc");
846     exit (EXIT_FAILURE);
847   }
848
849   int ret = -1;
850
851   /* Read nrvals * 2 lines of input, nrvals * (key, value) pairs, as
852    * explained in the man page.
853    */
854   int prompt = isatty (0) ? 2 : 0;
855   int i, j;
856   for (i = 0; i < nrvals; ++i) {
857     /* Read key. */
858     char *buf = rl_gets ("  key> ");
859     if (!buf) {
860       fprintf (stderr, _("hivexsh: setval: unexpected end of input\n"));
861       quit = 1;
862       goto error;
863     }
864
865     /* Note that buf will be overwritten by the next call to rl_gets. */
866     if (STREQ (buf, "@"))
867       values[i].key = strdup ("");
868     else
869       values[i].key = strdup (buf);
870     if (values[i].key == NULL) {
871       perror ("strdup");
872       exit (EXIT_FAILURE);
873     }
874
875     /* Read value. */
876     buf = rl_gets ("value> ");
877     if (!buf) {
878       fprintf (stderr, _("hivexsh: setval: unexpected end of input\n"));
879       quit = 1;
880       goto error;
881     }
882
883     if (STREQ (buf, "none")) {
884       values[i].t = hive_t_none;
885       values[i].len = 0;
886     }
887     else if (STRPREFIX (buf, "string:")) {
888       buf += 7;
889       values[i].t = hive_t_string;
890       int nr_chars = strlen (buf);
891       values[i].len = 2 * (nr_chars + 1);
892       values[i].value = malloc (values[i].len);
893       if (!values[i].value) {
894         perror ("malloc");
895         exit (EXIT_FAILURE);
896       }
897       for (j = 0; j <= /* sic */ nr_chars; ++j) {
898         if (buf[j] & 0x80) {
899           fprintf (stderr, _("hivexsh: string(utf16le): only 7 bit ASCII strings are supported for input\n"));
900           goto error;
901         }
902         values[i].value[2*j] = buf[j];
903         values[i].value[2*j+1] = '\0';
904       }
905     }
906     else if (STRPREFIX (buf, "expandstring:")) {
907       buf += 13;
908       values[i].t = hive_t_string;
909       int nr_chars = strlen (buf);
910       values[i].len = 2 * (nr_chars + 1);
911       values[i].value = malloc (values[i].len);
912       if (!values[i].value) {
913         perror ("malloc");
914         exit (EXIT_FAILURE);
915       }
916       for (j = 0; j <= /* sic */ nr_chars; ++j) {
917         if (buf[j] & 0x80) {
918           fprintf (stderr, _("hivexsh: string(utf16le): only 7 bit ASCII strings are supported for input\n"));
919           goto error;
920         }
921         values[i].value[2*j] = buf[j];
922         values[i].value[2*j+1] = '\0';
923       }
924     }
925     else if (STRPREFIX (buf, "dword:")) {
926       buf += 6;
927       values[i].t = hive_t_dword;
928       values[i].len = 4;
929       values[i].value = malloc (4);
930       if (!values[i].value) {
931         perror ("malloc");
932         exit (EXIT_FAILURE);
933       }
934       long n;
935       xerr = xstrtol (buf, NULL, 0, &n, "");
936       if (xerr != LONGINT_OK) {
937         fprintf (stderr, _("%s: %s: invalid integer parameter (%s returned %d)\n"),
938                  "setval", "dword", "xstrtol", xerr);
939         goto error;
940       }
941       if (n < 0 || n > UINT32_MAX) {
942         fprintf (stderr, _("%s: %s: integer out of range\n"),
943                  "setval", "dword");
944         goto error;
945       }
946       uint32_t u32 = htole32 (n);
947       memcpy (values[i].value, &u32, 4);
948     }
949     else if (STRPREFIX (buf, "qword:")) {
950       buf += 6;
951       values[i].t = hive_t_qword;
952       values[i].len = 8;
953       values[i].value = malloc (8);
954       if (!values[i].value) {
955         perror ("malloc");
956         exit (EXIT_FAILURE);
957       }
958       long long n;
959       xerr = xstrtoll (buf, NULL, 0, &n, "");
960       if (xerr != LONGINT_OK) {
961         fprintf (stderr, _("%s: %s: invalid integer parameter (%s returned %d)\n"),
962                  "setval", "dword", "xstrtoll", xerr);
963         goto error;
964       }
965 #if 0
966       if (n < 0 || n > UINT64_MAX) {
967         fprintf (stderr, _("%s: %s: integer out of range\n"),
968                  "setval", "dword");
969         goto error;
970       }
971 #endif
972       uint64_t u64 = htole64 (n);
973       memcpy (values[i].value, &u64, 4);
974     }
975     else if (STRPREFIX (buf, "hex:")) {
976       /* Read the type. */
977       buf += 4;
978       size_t len = strcspn (buf, ":");
979       char *nextbuf;
980       if (buf[len] == '\0')     /* "hex:t" */
981         nextbuf = &buf[len];
982       else {                    /* "hex:t:..." */
983         buf[len] = '\0';
984         nextbuf = &buf[len+1];
985       }
986
987       long t;
988       xerr = xstrtol (buf, NULL, 0, &t, "");
989       if (xerr != LONGINT_OK) {
990         fprintf (stderr, _("%s: %s: invalid integer parameter (%s returned %d)\n"),
991                  "setval", "hex", "xstrtol", xerr);
992         goto error;
993       }
994       if (t < 0 || t > UINT32_MAX) {
995         fprintf (stderr, _("%s: %s: integer out of range\n"),
996                  "setval", "hex");
997         goto error;
998       }
999       values[i].t = t;
1000
1001       /* Read the hex data. */
1002       buf = nextbuf;
1003
1004       /* The allocation length is an overestimate, but it doesn't matter. */
1005       values[i].value = malloc (1 + strlen (buf) / 2);
1006       if (!values[i].value) {
1007         perror ("malloc");
1008         exit (EXIT_FAILURE);
1009       }
1010       values[i].len = 0;
1011
1012       while (*buf) {
1013         int c = 0;
1014
1015         for (j = 0; *buf && j < 2; buf++) {
1016           if (c_isxdigit (*buf)) { /* NB: ignore non-hex digits. */
1017             c <<= 4;
1018             c |= get_xdigit (*buf);
1019             j++;
1020           }
1021         }
1022
1023         if (j == 2) values[i].value[values[i].len++] = c;
1024         else if (j == 1) {
1025           fprintf (stderr, _("hivexsh: setval: trailing garbage after hex string\n"));
1026           goto error;
1027         }
1028       }
1029     }
1030     else {
1031       fprintf (stderr,
1032                _("hivexsh: setval: cannot parse value string, please refer to the man page hivexsh(1) for help: %s\n"),
1033                buf);
1034       goto error;
1035     }
1036   }
1037
1038   ret = hivex_node_set_values (h, cwd, nrvals, values, 0);
1039
1040  error:
1041   /* Free values array. */
1042   for (i = 0; i < nrvals; ++i) {
1043     free (values[i].key);
1044     free (values[i].value);
1045   }
1046   free (values);
1047
1048   return ret;
1049 }
1050
1051 static int
1052 cmd_del (char *args)
1053 {
1054   if (STRNEQ (args, "")) {
1055     fprintf (stderr, _("hivexsh: '%s' command should not be given arguments\n"),
1056              "del");
1057     return -1;
1058   }
1059
1060   if (cwd == hivex_root (h)) {
1061     fprintf (stderr, _("hivexsh: del: the root node cannot be deleted\n"));
1062     return -1;
1063   }
1064
1065   hive_node_h new_cwd = hivex_node_parent (h, cwd);
1066
1067   if (hivex_node_delete_child (h, cwd) == -1) {
1068     perror ("del");
1069     return -1;
1070   }
1071
1072   cwd = new_cwd;
1073   set_prompt_string ();
1074   return 0;
1075 }