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