hivexsh: lsval: Remove stray quotation mark.
[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     errno = 0;
563     new_cwd = hivex_node_get_child (h, new_cwd, elem);
564     if (new_cwd == 0) {
565       if (errno)
566         perror ("hivexsh: cd");
567       else
568         fprintf (stderr, _("hivexsh: cd: subkey '%s' not found\n"),
569                  elem);
570       return -1;
571     }
572   }
573
574   if (new_cwd != cwd) {
575     cwd = new_cwd;
576     set_prompt_string ();
577   }
578
579   return 0;
580 }
581
582 static int
583 cmd_help (char *args)
584 {
585   printf (_(
586 "Navigate through the hive's keys using the 'cd' command, as if it\n"
587 "contained a filesystem, and use 'ls' to list the subkeys of the\n"
588 "current key.  Full documentation is in the hivexsh(1) manual page.\n"));
589
590   return 0;
591 }
592
593 static int
594 cmd_ls (char *args)
595 {
596   if (STRNEQ (args, "")) {
597     fprintf (stderr, _("hivexsh: '%s' command should not be given arguments\n"),
598              "ls");
599     return -1;
600   }
601
602   /* Get the subkeys. */
603   hive_node_h *children = hivex_node_children (h, cwd);
604   if (children == NULL) {
605     perror ("ls");
606     return -1;
607   }
608
609   /* Get names for each subkey. */
610   size_t len;
611   for (len = 0; children[len] != 0; ++len)
612     ;
613
614   char **names = calloc (len, sizeof (char *));
615   if (names == NULL) {
616     perror ("malloc");
617     exit (EXIT_FAILURE);
618   }
619
620   int ret = -1;
621   size_t i;
622   for (i = 0; i < len; ++i) {
623     names[i] = hivex_node_name (h, children[i]);
624     if (names[i] == NULL) {
625       perror ("hivex_node_name");
626       goto error;
627     }
628   }
629
630   /* Sort the names. */
631   sort_strings (names, len);
632
633   for (i = 0; i < len; ++i)
634     printf ("%s\n", names[i]);
635
636   ret = 0;
637  error:
638   free (children);
639   for (i = 0; i < len; ++i)
640     free (names[i]);
641   free (names);
642   return ret;
643 }
644
645 static int
646 cmd_lsval (char *key)
647 {
648   if (STRNEQ (key, "")) {
649     hive_value_h value;
650
651     errno = 0;
652     if (STREQ (key, "@"))       /* default key written as "@" */
653       value = hivex_node_get_value (h, cwd, "");
654     else
655       value = hivex_node_get_value (h, cwd, key);
656
657     if (value == 0) {
658       if (errno)
659         goto error;
660       /* else key not found */
661       fprintf (stderr, _("%s: %s: key not found\n"), "hivexsh", key);
662       return -1;
663     }
664
665     /* Print the value. */
666     hive_type t;
667     size_t len;
668     if (hivex_value_type (h, value, &t, &len) == -1)
669       goto error;
670
671     switch (t) {
672     case hive_t_string:
673     case hive_t_expand_string:
674     case hive_t_link: {
675       char *str = hivex_value_string (h, value);
676       if (!str)
677         goto error;
678
679       puts (str); /* note: this adds a single \n character */
680       free (str);
681       break;
682     }
683
684     case hive_t_dword:
685     case hive_t_dword_be: {
686       int32_t j = hivex_value_dword (h, value);
687       printf ("%" PRIi32 "\n", j);
688       break;
689     }
690
691     case hive_t_qword: {
692       int64_t j = hivex_value_qword (h, value);
693       printf ("%" PRIi64 "\n", j);
694       break;
695     }
696
697     case hive_t_multiple_strings: {
698       char **strs = hivex_value_multiple_strings (h, value);
699       if (!strs)
700         goto error;
701       size_t j;
702       for (j = 0; strs[j] != NULL; ++j) {
703         puts (strs[j]);
704         free (strs[j]);
705       }
706       free (strs);
707       break;
708     }
709
710     case hive_t_none:
711     case hive_t_binary:
712     case hive_t_resource_list:
713     case hive_t_full_resource_description:
714     case hive_t_resource_requirements_list:
715     default: {
716       char *data = hivex_value_value (h, value, &t, &len);
717       if (!data)
718         goto error;
719
720       if (fwrite (data, 1, len, stdout) != len)
721         goto error;
722
723       free (data);
724       break;
725     }
726     } /* switch */
727   } else {
728     /* No key specified, so print all keys in this node.  We do this
729      * in a format which looks like the output of regedit, although
730      * this isn't a particularly useful format.
731      */
732     hive_value_h *values;
733
734     values = hivex_node_values (h, cwd);
735     if (values == NULL)
736       goto error;
737
738     size_t i;
739     for (i = 0; values[i] != 0; ++i) {
740       char *key = hivex_value_key (h, values[i]);
741       if (!key) goto error;
742
743       if (*key) {
744         putchar ('"');
745         size_t j;
746         for (j = 0; key[j] != 0; ++j) {
747           if (key[j] == '"' || key[j] == '\\')
748             putchar ('\\');
749           putchar (key[j]);
750         }
751         putchar ('"');
752       } else
753         printf ("\"@\"");       /* default key in regedit files */
754       putchar ('=');
755       free (key);
756
757       hive_type t;
758       size_t len;
759       if (hivex_value_type (h, values[i], &t, &len) == -1)
760         goto error;
761
762       switch (t) {
763       case hive_t_string:
764       case hive_t_expand_string:
765       case hive_t_link: {
766         char *str = hivex_value_string (h, values[i]);
767         if (!str)
768           goto error;
769
770         if (t != hive_t_string)
771           printf ("str(%d):", t);
772         putchar ('"');
773         size_t j;
774         for (j = 0; str[j] != 0; ++j) {
775           if (str[j] == '"' || str[j] == '\\')
776             putchar ('\\');
777           putchar (str[j]);
778         }
779         putchar ('"');
780         free (str);
781         break;
782       }
783
784       case hive_t_dword:
785       case hive_t_dword_be: {
786         int32_t j = hivex_value_dword (h, values[i]);
787         printf ("dword:%08" PRIx32, j);
788         break;
789       }
790
791       case hive_t_qword: /* sic */
792       case hive_t_none:
793       case hive_t_binary:
794       case hive_t_multiple_strings:
795       case hive_t_resource_list:
796       case hive_t_full_resource_description:
797       case hive_t_resource_requirements_list:
798       default: {
799         char *data = hivex_value_value (h, values[i], &t, &len);
800         if (!data)
801           goto error;
802
803         printf ("hex(%d):", t);
804         size_t j;
805         for (j = 0; j < len; ++j) {
806           if (j > 0)
807             putchar (',');
808           printf ("%02x", data[j]);
809         }
810         break;
811       }
812       } /* switch */
813
814       putchar ('\n');
815     } /* for */
816
817     free (values);
818   }
819
820   return 0;
821
822  error:
823   perror ("hivexsh: lsval");
824   return -1;
825 }
826
827 static int
828 cmd_setval (char *nrvals_str)
829 {
830   strtol_error xerr;
831
832   /* Parse number of values. */
833   long nrvals;
834   xerr = xstrtol (nrvals_str, NULL, 0, &nrvals, "");
835   if (xerr != LONGINT_OK) {
836     fprintf (stderr, _("%s: %s: invalid integer parameter (%s returned %d)\n"),
837              "setval", "nrvals", "xstrtol", xerr);
838     return -1;
839   }
840   if (nrvals < 0 || nrvals > 1000) {
841     fprintf (stderr, _("%s: %s: integer out of range\n"),
842              "setval", "nrvals");
843     return -1;
844   }
845
846   struct hive_set_value *values =
847     calloc (nrvals, sizeof (struct hive_set_value));
848   if (values == NULL) {
849     perror ("calloc");
850     exit (EXIT_FAILURE);
851   }
852
853   int ret = -1;
854
855   /* Read nrvals * 2 lines of input, nrvals * (key, value) pairs, as
856    * explained in the man page.
857    */
858   int prompt = isatty (0) ? 2 : 0;
859   int i, j;
860   for (i = 0; i < nrvals; ++i) {
861     /* Read key. */
862     char *buf = rl_gets ("  key> ");
863     if (!buf) {
864       fprintf (stderr, _("hivexsh: setval: unexpected end of input\n"));
865       quit = 1;
866       goto error;
867     }
868
869     /* Note that buf will be overwritten by the next call to rl_gets. */
870     if (STREQ (buf, "@"))
871       values[i].key = strdup ("");
872     else
873       values[i].key = strdup (buf);
874     if (values[i].key == NULL) {
875       perror ("strdup");
876       exit (EXIT_FAILURE);
877     }
878
879     /* Read value. */
880     buf = rl_gets ("value> ");
881     if (!buf) {
882       fprintf (stderr, _("hivexsh: setval: unexpected end of input\n"));
883       quit = 1;
884       goto error;
885     }
886
887     if (STREQ (buf, "none")) {
888       values[i].t = hive_t_none;
889       values[i].len = 0;
890     }
891     else if (STRPREFIX (buf, "string:")) {
892       buf += 7;
893       values[i].t = hive_t_string;
894       int nr_chars = strlen (buf);
895       values[i].len = 2 * (nr_chars + 1);
896       values[i].value = malloc (values[i].len);
897       if (!values[i].value) {
898         perror ("malloc");
899         exit (EXIT_FAILURE);
900       }
901       for (j = 0; j <= /* sic */ nr_chars; ++j) {
902         if (buf[j] & 0x80) {
903           fprintf (stderr, _("hivexsh: string(utf16le): only 7 bit ASCII strings are supported for input\n"));
904           goto error;
905         }
906         values[i].value[2*j] = buf[j];
907         values[i].value[2*j+1] = '\0';
908       }
909     }
910     else if (STRPREFIX (buf, "expandstring:")) {
911       buf += 13;
912       values[i].t = hive_t_string;
913       int nr_chars = strlen (buf);
914       values[i].len = 2 * (nr_chars + 1);
915       values[i].value = malloc (values[i].len);
916       if (!values[i].value) {
917         perror ("malloc");
918         exit (EXIT_FAILURE);
919       }
920       for (j = 0; j <= /* sic */ nr_chars; ++j) {
921         if (buf[j] & 0x80) {
922           fprintf (stderr, _("hivexsh: string(utf16le): only 7 bit ASCII strings are supported for input\n"));
923           goto error;
924         }
925         values[i].value[2*j] = buf[j];
926         values[i].value[2*j+1] = '\0';
927       }
928     }
929     else if (STRPREFIX (buf, "dword:")) {
930       buf += 6;
931       values[i].t = hive_t_dword;
932       values[i].len = 4;
933       values[i].value = malloc (4);
934       if (!values[i].value) {
935         perror ("malloc");
936         exit (EXIT_FAILURE);
937       }
938       long n;
939       xerr = xstrtol (buf, NULL, 0, &n, "");
940       if (xerr != LONGINT_OK) {
941         fprintf (stderr, _("%s: %s: invalid integer parameter (%s returned %d)\n"),
942                  "setval", "dword", "xstrtol", xerr);
943         goto error;
944       }
945       if (n < 0 || n > UINT32_MAX) {
946         fprintf (stderr, _("%s: %s: integer out of range\n"),
947                  "setval", "dword");
948         goto error;
949       }
950       uint32_t u32 = htole32 (n);
951       memcpy (values[i].value, &u32, 4);
952     }
953     else if (STRPREFIX (buf, "qword:")) {
954       buf += 6;
955       values[i].t = hive_t_qword;
956       values[i].len = 8;
957       values[i].value = malloc (8);
958       if (!values[i].value) {
959         perror ("malloc");
960         exit (EXIT_FAILURE);
961       }
962       long long n;
963       xerr = xstrtoll (buf, NULL, 0, &n, "");
964       if (xerr != LONGINT_OK) {
965         fprintf (stderr, _("%s: %s: invalid integer parameter (%s returned %d)\n"),
966                  "setval", "dword", "xstrtoll", xerr);
967         goto error;
968       }
969 #if 0
970       if (n < 0 || n > UINT64_MAX) {
971         fprintf (stderr, _("%s: %s: integer out of range\n"),
972                  "setval", "dword");
973         goto error;
974       }
975 #endif
976       uint64_t u64 = htole64 (n);
977       memcpy (values[i].value, &u64, 4);
978     }
979     else if (STRPREFIX (buf, "hex:")) {
980       /* Read the type. */
981       buf += 4;
982       size_t len = strcspn (buf, ":");
983       char *nextbuf;
984       if (buf[len] == '\0')     /* "hex:t" */
985         nextbuf = &buf[len];
986       else {                    /* "hex:t:..." */
987         buf[len] = '\0';
988         nextbuf = &buf[len+1];
989       }
990
991       long t;
992       xerr = xstrtol (buf, NULL, 0, &t, "");
993       if (xerr != LONGINT_OK) {
994         fprintf (stderr, _("%s: %s: invalid integer parameter (%s returned %d)\n"),
995                  "setval", "hex", "xstrtol", xerr);
996         goto error;
997       }
998       if (t < 0 || t > UINT32_MAX) {
999         fprintf (stderr, _("%s: %s: integer out of range\n"),
1000                  "setval", "hex");
1001         goto error;
1002       }
1003       values[i].t = t;
1004
1005       /* Read the hex data. */
1006       buf = nextbuf;
1007
1008       /* The allocation length is an overestimate, but it doesn't matter. */
1009       values[i].value = malloc (1 + strlen (buf) / 2);
1010       if (!values[i].value) {
1011         perror ("malloc");
1012         exit (EXIT_FAILURE);
1013       }
1014       values[i].len = 0;
1015
1016       while (*buf) {
1017         int c = 0;
1018
1019         for (j = 0; *buf && j < 2; buf++) {
1020           if (c_isxdigit (*buf)) { /* NB: ignore non-hex digits. */
1021             c <<= 4;
1022             c |= get_xdigit (*buf);
1023             j++;
1024           }
1025         }
1026
1027         if (j == 2) values[i].value[values[i].len++] = c;
1028         else if (j == 1) {
1029           fprintf (stderr, _("hivexsh: setval: trailing garbage after hex string\n"));
1030           goto error;
1031         }
1032       }
1033     }
1034     else {
1035       fprintf (stderr,
1036                _("hivexsh: setval: cannot parse value string, please refer to the man page hivexsh(1) for help: %s\n"),
1037                buf);
1038       goto error;
1039     }
1040   }
1041
1042   ret = hivex_node_set_values (h, cwd, nrvals, values, 0);
1043
1044  error:
1045   /* Free values array. */
1046   for (i = 0; i < nrvals; ++i) {
1047     free (values[i].key);
1048     free (values[i].value);
1049   }
1050   free (values);
1051
1052   return ret;
1053 }
1054
1055 static int
1056 cmd_del (char *args)
1057 {
1058   if (STRNEQ (args, "")) {
1059     fprintf (stderr, _("hivexsh: '%s' command should not be given arguments\n"),
1060              "del");
1061     return -1;
1062   }
1063
1064   if (cwd == hivex_root (h)) {
1065     fprintf (stderr, _("hivexsh: del: the root node cannot be deleted\n"));
1066     return -1;
1067   }
1068
1069   hive_node_h new_cwd = hivex_node_parent (h, cwd);
1070
1071   if (hivex_node_delete_child (h, cwd) == -1) {
1072     perror ("del");
1073     return -1;
1074   }
1075
1076   cwd = new_cwd;
1077   set_prompt_string ();
1078   return 0;
1079 }