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.
31 main (int argc, char *argv[])
33 if (argc < 3 || argc > 4) {
34 fprintf (stderr, "hivexget regfile path [key]\n");
39 char *key = argv[3]; /* could be NULL */
41 if (path[0] != '\\') {
42 fprintf (stderr, "hivexget: path must start with a \\ character\n");
45 if (path[1] == '\\') {
47 fprintf (stderr, "hivexget: %s: \\ characters in path are doubled - are you escaping the path parameter correctly?\n", path);
51 hive_h *h = hivex_open (file, 0);
58 /* Navigate to the desired node. */
59 hive_node_h node = hivex_root (h);
63 char *p = path+1, *pnext;
66 len = strcspn (p, "\\");
78 node = hivex_node_get_child (h, node, p);
82 /* else node not found */
83 fprintf (stderr, "hivexget: %s: %s: path element not found\n",
91 /* Get the desired key, or print all keys. */
96 if (key[0] == '@' && key[1] == '\0') /* default key written as "@" */
97 value = hivex_node_get_value (h, node, "");
99 value = hivex_node_get_value (h, node, key);
104 /* else key not found */
105 fprintf (stderr, "hivexget: %s: key not found\n", key);
109 /* Print the value. */
112 if (hivex_value_type (h, value, &t, &len) == -1)
117 case hive_t_expand_string:
119 char *str = hivex_value_string (h, value);
123 puts (str); /* note: this adds a single \n character */
129 case hive_t_dword_be: {
130 int32_t j = hivex_value_dword (h, value);
131 printf ("%" PRIi32 "\n", j);
136 int64_t j = hivex_value_qword (h, value);
137 printf ("%" PRIi64 "\n", j);
141 case hive_t_multiple_strings: {
142 char **strs = hivex_value_multiple_strings (h, value);
146 for (j = 0; strs[j] != NULL; ++j) {
156 case hive_t_resource_list:
157 case hive_t_full_resource_description:
158 case hive_t_resource_requirements_list:
160 char *data = hivex_value_value (h, value, &t, &len);
164 if (fwrite (data, 1, len, stdout) != len)
172 /* No key specified, so print all keys in this node. We do this
173 * in a format which looks like the output of regedit, although
174 * this isn't a particularly useful format.
176 hive_value_h *values;
178 values = hivex_node_values (h, node);
183 for (i = 0; values[i] != 0; ++i) {
184 char *key = hivex_value_key (h, values[i]);
185 if (!key) goto error;
190 for (j = 0; key[j] != 0; ++j) {
191 if (key[j] == '"' || key[j] == '\\')
197 printf ("\"@\""); /* default key in regedit files */
203 if (hivex_value_type (h, values[i], &t, &len) == -1)
208 case hive_t_expand_string:
210 char *str = hivex_value_string (h, values[i]);
214 if (t != hive_t_string)
215 printf ("str(%d):", t);
218 for (j = 0; str[j] != 0; ++j) {
219 if (str[j] == '"' || str[j] == '\\')
229 case hive_t_dword_be: {
230 int32_t j = hivex_value_dword (h, values[i]);
231 printf ("dword:%08" PRIx32 "\"", j);
235 case hive_t_qword: /* sic */
238 case hive_t_multiple_strings:
239 case hive_t_resource_list:
240 case hive_t_full_resource_description:
241 case hive_t_resource_requirements_list:
243 char *data = hivex_value_value (h, values[i], &t, &len);
247 printf ("hex(%d):", t);
249 for (j = 0; j < len; ++j) {
252 printf ("%02x", data[j]);
264 if (hivex_close (h) == -1)