1 /* hivexget - Get single subkeys or values from a hive.
2 * Copyright (C) 2009 Red Hat Inc.
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.
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.
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.
30 enum { EXIT_NOT_FOUND = 2 };
33 main (int argc, char *argv[])
35 if (argc < 3 || argc > 4) {
36 fprintf (stderr, "hivexget regfile path [key]\n");
41 char *key = argv[3]; /* could be NULL */
43 if (path[0] != '\\') {
44 fprintf (stderr, "hivexget: path must start with a \\ character\n");
47 if (path[1] == '\\') {
49 fprintf (stderr, "hivexget: %s: \\ characters in path are doubled - are you escaping the path parameter correctly?\n", path);
53 hive_h *h = hivex_open (file, 0);
60 /* Navigate to the desired node. */
61 hive_node_h node = hivex_root (h);
65 char *p = path+1, *pnext;
68 len = strcspn (p, "\\");
80 node = hivex_node_get_child (h, node, p);
84 /* else node not found */
85 fprintf (stderr, "hivexget: %s: %s: path element not found\n",
87 exit (EXIT_NOT_FOUND);
93 /* Get the desired key, or print all keys. */
98 if (key[0] == '@' && key[1] == '\0') /* default key written as "@" */
99 value = hivex_node_get_value (h, node, "");
101 value = hivex_node_get_value (h, node, key);
106 /* else key not found */
107 fprintf (stderr, "hivexget: %s: key not found\n", key);
108 exit (EXIT_NOT_FOUND);
111 /* Print the value. */
114 if (hivex_value_type (h, value, &t, &len) == -1)
119 case hive_t_expand_string:
121 char *str = hivex_value_string (h, value);
125 puts (str); /* note: this adds a single \n character */
131 case hive_t_dword_be: {
132 int32_t j = hivex_value_dword (h, value);
133 printf ("%" PRIi32 "\n", j);
138 int64_t j = hivex_value_qword (h, value);
139 printf ("%" PRIi64 "\n", j);
143 case hive_t_multiple_strings: {
144 char **strs = hivex_value_multiple_strings (h, value);
148 for (j = 0; strs[j] != NULL; ++j) {
158 case hive_t_resource_list:
159 case hive_t_full_resource_description:
160 case hive_t_resource_requirements_list:
162 char *data = hivex_value_value (h, value, &t, &len);
166 if (fwrite (data, 1, len, stdout) != len)
174 /* No key specified, so print all keys in this node. We do this
175 * in a format which looks like the output of regedit, although
176 * this isn't a particularly useful format.
178 hive_value_h *values;
180 values = hivex_node_values (h, node);
185 for (i = 0; values[i] != 0; ++i) {
186 char *key = hivex_value_key (h, values[i]);
187 if (!key) goto error;
192 for (j = 0; key[j] != 0; ++j) {
193 if (key[j] == '"' || key[j] == '\\')
199 printf ("\"@\""); /* default key in regedit files */
205 if (hivex_value_type (h, values[i], &t, &len) == -1)
210 case hive_t_expand_string:
212 char *str = hivex_value_string (h, values[i]);
216 if (t != hive_t_string)
217 printf ("str(%d):", t);
220 for (j = 0; str[j] != 0; ++j) {
221 if (str[j] == '"' || str[j] == '\\')
231 case hive_t_dword_be: {
232 int32_t j = hivex_value_dword (h, values[i]);
233 printf ("dword:%08" PRIx32 "\"", j);
237 case hive_t_qword: /* sic */
240 case hive_t_multiple_strings:
241 case hive_t_resource_list:
242 case hive_t_full_resource_description:
243 case hive_t_resource_requirements_list:
245 char *data = hivex_value_value (h, values[i], &t, &len);
249 printf ("hex(%d):", t);
251 for (j = 0; j < len; ++j) {
254 printf ("%02x", data[j]);
266 if (hivex_close (h) == -1)