1 /* hivexml - Convert Windows Registry "hive" to XML file.
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 #include <libxml/xmlwriter.h>
33 /* Callback functions. */
34 static int node_start (hive_h *, void *, hive_node_h, const char *name);
35 static int node_end (hive_h *, void *, hive_node_h, const char *name);
36 static int value_string (hive_h *, void *, hive_node_h, hive_value_h, hive_type t, size_t len, const char *key, const char *str);
37 static int value_multiple_strings (hive_h *, void *, hive_node_h, hive_value_h, hive_type t, size_t len, const char *key, char **argv);
38 static int value_string_invalid_utf16 (hive_h *, void *, hive_node_h, hive_value_h, hive_type t, size_t len, const char *key, const char *str);
39 static int value_dword (hive_h *, void *, hive_node_h, hive_value_h, hive_type t, size_t len, const char *key, int32_t);
40 static int value_qword (hive_h *, void *, hive_node_h, hive_value_h, hive_type t, size_t len, const char *key, int64_t);
41 static int value_binary (hive_h *, void *, hive_node_h, hive_value_h, hive_type t, size_t len, const char *key, const char *value);
42 static int value_none (hive_h *, void *, hive_node_h, hive_value_h, hive_type t, size_t len, const char *key, const char *value);
43 static int value_other (hive_h *, void *, hive_node_h, hive_value_h, hive_type t, size_t len, const char *key, const char *value);
45 static struct hivex_visitor visitor = {
46 .node_start = node_start,
48 .value_string = value_string,
49 .value_multiple_strings = value_multiple_strings,
50 .value_string_invalid_utf16 = value_string_invalid_utf16,
51 .value_dword = value_dword,
52 .value_qword = value_qword,
53 .value_binary = value_binary,
54 .value_none = value_none,
55 .value_other = value_other
58 #define XML_CHECK(proc, args) \
60 if ((proc args) == -1) { \
61 fprintf (stderr, "%s: failed to write XML document\n", #proc); \
62 exit (EXIT_FAILURE); \
67 main (int argc, char *argv[])
73 while ((c = getopt (argc, argv, "dk")) != EOF) {
76 open_flags |= HIVEX_OPEN_DEBUG;
79 visit_flags |= HIVEX_VISIT_SKIP_BAD;
82 fprintf (stderr, "hivexml [-dk] regfile > output.xml\n");
87 if (optind + 1 != argc) {
88 fprintf (stderr, "hivexml: missing name of input file\n");
92 hive_h *h = hivex_open (argv[optind], open_flags);
94 perror (argv[optind]);
98 /* Note both this macro, and xmlTextWriterStartDocument leak memory. There
99 * doesn't seem to be any way to recover that memory, but it's not a
104 xmlTextWriterPtr writer;
105 writer = xmlNewTextWriterFilename ("/dev/stdout", 0);
106 if (writer == NULL) {
107 fprintf (stderr, "xmlNewTextWriterFilename: failed to create XML writer\n");
111 XML_CHECK (xmlTextWriterStartDocument, (writer, NULL, "utf-8", NULL));
112 XML_CHECK (xmlTextWriterStartElement, (writer, BAD_CAST "hive"));
114 if (hivex_visit (h, &visitor, sizeof visitor, writer, visit_flags) == -1) {
115 perror (argv[optind]);
119 if (hivex_close (h) == -1) {
120 perror (argv[optind]);
124 XML_CHECK (xmlTextWriterEndElement, (writer));
125 XML_CHECK (xmlTextWriterEndDocument, (writer));
126 xmlFreeTextWriter (writer);
132 node_start (hive_h *h, void *writer_v, hive_node_h node, const char *name)
134 xmlTextWriterPtr writer = (xmlTextWriterPtr) writer_v;
135 XML_CHECK (xmlTextWriterStartElement, (writer, BAD_CAST "node"));
136 XML_CHECK (xmlTextWriterWriteAttribute, (writer, BAD_CAST "name", BAD_CAST name));
141 node_end (hive_h *h, void *writer_v, hive_node_h node, const char *name)
143 xmlTextWriterPtr writer = (xmlTextWriterPtr) writer_v;
144 XML_CHECK (xmlTextWriterEndElement, (writer));
149 start_value (xmlTextWriterPtr writer,
150 const char *key, const char *type, const char *encoding)
152 XML_CHECK (xmlTextWriterStartElement, (writer, BAD_CAST "value"));
153 XML_CHECK (xmlTextWriterWriteAttribute, (writer, BAD_CAST "type", BAD_CAST type));
155 XML_CHECK (xmlTextWriterWriteAttribute, (writer, BAD_CAST "encoding", BAD_CAST encoding));
157 XML_CHECK (xmlTextWriterWriteAttribute, (writer, BAD_CAST "key", BAD_CAST key));
158 else /* default key */
159 XML_CHECK (xmlTextWriterWriteAttribute, (writer, BAD_CAST "default", BAD_CAST "1"));
163 end_value (xmlTextWriterPtr writer)
165 XML_CHECK (xmlTextWriterEndElement, (writer));
169 value_string (hive_h *h, void *writer_v, hive_node_h node, hive_value_h value,
170 hive_type t, size_t len, const char *key, const char *str)
172 xmlTextWriterPtr writer = (xmlTextWriterPtr) writer_v;
176 case hive_t_string: type = "string"; break;
177 case hive_t_expand_string: type = "expand"; break;
178 case hive_t_link: type = "link"; break;
183 case hive_t_dword_be:
184 case hive_t_multiple_strings:
185 case hive_t_resource_list:
186 case hive_t_full_resource_description:
187 case hive_t_resource_requirements_list:
189 abort (); /* internal error - should not happen */
195 start_value (writer, key, type, NULL);
196 XML_CHECK (xmlTextWriterWriteString, (writer, BAD_CAST str));
202 value_multiple_strings (hive_h *h, void *writer_v, hive_node_h node,
203 hive_value_h value, hive_type t, size_t len,
204 const char *key, char **argv)
206 xmlTextWriterPtr writer = (xmlTextWriterPtr) writer_v;
207 start_value (writer, key, "string-list", NULL);
210 for (i = 0; argv[i] != NULL; ++i) {
211 XML_CHECK (xmlTextWriterStartElement, (writer, BAD_CAST "string"));
212 XML_CHECK (xmlTextWriterWriteString, (writer, BAD_CAST argv[i]));
213 XML_CHECK (xmlTextWriterEndElement, (writer));
221 value_string_invalid_utf16 (hive_h *h, void *writer_v, hive_node_h node,
222 hive_value_h value, hive_type t, size_t len,
224 const char *str /* original data */)
226 xmlTextWriterPtr writer = (xmlTextWriterPtr) writer_v;
230 case hive_t_string: type = "bad-string"; break;
231 case hive_t_expand_string: type = "bad-expand"; break;
232 case hive_t_link: type = "bad-link"; break;
233 case hive_t_multiple_strings: type = "bad-string-list"; break;
238 case hive_t_dword_be:
239 case hive_t_resource_list:
240 case hive_t_full_resource_description:
241 case hive_t_resource_requirements_list:
243 abort (); /* internal error - should not happen */
249 start_value (writer, key, type, "base64");
250 XML_CHECK (xmlTextWriterWriteBase64, (writer, str, 0, len));
257 value_dword (hive_h *h, void *writer_v, hive_node_h node, hive_value_h value,
258 hive_type t, size_t len, const char *key, int32_t v)
260 xmlTextWriterPtr writer = (xmlTextWriterPtr) writer_v;
261 start_value (writer, key, "int32", NULL);
262 XML_CHECK (xmlTextWriterWriteFormatString, (writer, "%" PRIi32, v));
268 value_qword (hive_h *h, void *writer_v, hive_node_h node, hive_value_h value,
269 hive_type t, size_t len, const char *key, int64_t v)
271 xmlTextWriterPtr writer = (xmlTextWriterPtr) writer_v;
272 start_value (writer, key, "int64", NULL);
273 XML_CHECK (xmlTextWriterWriteFormatString, (writer, "%" PRIi64, v));
279 value_binary (hive_h *h, void *writer_v, hive_node_h node, hive_value_h value,
280 hive_type t, size_t len, const char *key, const char *v)
282 xmlTextWriterPtr writer = (xmlTextWriterPtr) writer_v;
283 start_value (writer, key, "binary", "base64");
284 XML_CHECK (xmlTextWriterWriteBase64, (writer, v, 0, len));
290 value_none (hive_h *h, void *writer_v, hive_node_h node, hive_value_h value,
291 hive_type t, size_t len, const char *key, const char *v)
293 xmlTextWriterPtr writer = (xmlTextWriterPtr) writer_v;
294 start_value (writer, key, "none", "base64");
295 if (len > 0) XML_CHECK (xmlTextWriterWriteBase64, (writer, v, 0, len));
301 value_other (hive_h *h, void *writer_v, hive_node_h node, hive_value_h value,
302 hive_type t, size_t len, const char *key, const char *v)
304 xmlTextWriterPtr writer = (xmlTextWriterPtr) writer_v;
311 case hive_t_dword_be:
314 case hive_t_expand_string:
316 case hive_t_multiple_strings:
317 abort (); /* internal error - should not happen */
319 case hive_t_resource_list: type = "resource-list"; break;
320 case hive_t_full_resource_description: type = "resource-description"; break;
321 case hive_t_resource_requirements_list: type = "resource-requirements"; break;
327 start_value (writer, key, type, "base64");
328 if (len > 0) XML_CHECK (xmlTextWriterWriteBase64, (writer, v, 0, len));