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.
29 main (int argc, char *argv[])
31 if (argc < 3 || argc > 4) {
32 fprintf (stderr, "hivexget regfile path [key]\n");
37 char *key = argv[3]; /* could be NULL */
39 if (path[0] != '\\') {
40 fprintf (stderr, "hivexget: path must start with a \\ character\n");
43 if (path[1] == '\\') {
45 fprintf (stderr, "hivexget: %s: \\ characters in path are doubled - are you escaping the path parameter correctly?\n", path);
49 hive_h *h = hivex_open (file, 0);
56 /* Navigate to the desired node. */
57 hive_node_h node = hivex_root (h);
61 char *p = path+1, *pnext;
64 len = strcspn (p, "\\");
76 node = hivex_node_get_child (h, node, p);
80 /* else node not found */
81 fprintf (stderr, "hivexget: %s: %s: path element not found\n",
89 /* Get the desired key, or print all keys. */
94 if (key[0] == '@' && key[1] == '\0') /* default key written as "@" */
95 value = hivex_node_get_value (h, node, "");
97 value = hivex_node_get_value (h, node, key);
102 /* else key not found */
103 fprintf (stderr, "hivexget: %s: key not found\n", key);
107 /* Print the value. */
110 if (hivex_value_type (h, value, &t, &len) == -1)
115 case hive_t_expand_string:
117 char *str = hivex_value_string (h, value);
121 puts (str); /* note: this adds a single \n character */
127 case hive_t_dword_be: {
128 int32_t j = hivex_value_dword (h, value);
129 printf ("%" PRIi32 "\n", j);
134 int64_t j = hivex_value_qword (h, value);
135 printf ("%" PRIi64 "\n", j);
139 case hive_t_multiple_strings: {
140 char **strs = hivex_value_multiple_strings (h, value);
144 for (j = 0; strs[j] != NULL; ++j) {
154 case hive_t_resource_list:
155 case hive_t_full_resource_description:
156 case hive_t_resource_requirements_list:
158 char *data = hivex_value_value (h, value, &t, &len);
162 if (fwrite (data, 1, len, stdout) != len)
170 /* No key specified, so print all keys in this node. We do this
171 * in a format which looks like the output of regedit, although
172 * this isn't a particularly useful format.
174 hive_value_h *values;
176 values = hivex_node_values (h, node);
181 for (i = 0; values[i] != 0; ++i) {
182 char *key = hivex_value_key (h, values[i]);
183 if (!key) goto error;
188 for (j = 0; key[j] != 0; ++j) {
189 if (key[j] == '"' || key[j] == '\\')
195 printf ("\"@\""); /* default key in regedit files */
201 if (hivex_value_type (h, values[i], &t, &len) == -1)
206 case hive_t_expand_string:
208 char *str = hivex_value_string (h, values[i]);
212 if (t != hive_t_string)
213 printf ("str(%d):", t);
216 for (j = 0; str[j] != 0; ++j) {
217 if (str[j] == '"' || str[j] == '\\')
227 case hive_t_dword_be: {
228 int32_t j = hivex_value_dword (h, values[i]);
229 printf ("dword:%08" PRIx32 "\"", j);
233 case hive_t_qword: /* sic */
236 case hive_t_multiple_strings:
237 case hive_t_resource_list:
238 case hive_t_full_resource_description:
239 case hive_t_resource_requirements_list:
241 char *data = hivex_value_value (h, values[i], &t, &len);
245 printf ("hex(%d):", t);
247 for (j = 0; j < len; ++j) {
250 printf ("%02x", data[j]);
262 if (hivex_close (h) == -1)