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.
37 #include <libxml/xmlwriter.h>
43 #define _(str) dgettext(PACKAGE, (str))
44 //#define N_(str) dgettext(PACKAGE, (str))
50 static char *filetime_to_8601 (int64_t windows_ticks);
52 /* Callback functions. */
53 static int node_start (hive_h *, void *, hive_node_h, const char *name);
54 static int node_end (hive_h *, void *, hive_node_h, const char *name);
55 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);
56 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);
57 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);
58 static int value_dword (hive_h *, void *, hive_node_h, hive_value_h, hive_type t, size_t len, const char *key, int32_t);
59 static int value_qword (hive_h *, void *, hive_node_h, hive_value_h, hive_type t, size_t len, const char *key, int64_t);
60 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);
61 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);
62 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);
64 static struct hivex_visitor visitor = {
65 .node_start = node_start,
67 .value_string = value_string,
68 .value_multiple_strings = value_multiple_strings,
69 .value_string_invalid_utf16 = value_string_invalid_utf16,
70 .value_dword = value_dword,
71 .value_qword = value_qword,
72 .value_binary = value_binary,
73 .value_none = value_none,
74 .value_other = value_other
77 #define XML_CHECK(proc, args) \
79 if ((proc args) == -1) { \
80 fprintf (stderr, _("%s: failed to write XML document\n"), #proc); \
81 exit (EXIT_FAILURE); \
86 main (int argc, char *argv[])
88 setlocale (LC_ALL, "");
89 #ifdef HAVE_BINDTEXTDOMAIN
90 bindtextdomain (PACKAGE, LOCALEBASEDIR);
98 while ((c = getopt (argc, argv, "dk")) != EOF) {
101 open_flags |= HIVEX_OPEN_DEBUG;
104 visit_flags |= HIVEX_VISIT_SKIP_BAD;
107 fprintf (stderr, "hivexml [-dk] regfile > output.xml\n");
112 if (optind + 1 != argc) {
113 fprintf (stderr, _("hivexml: missing name of input file\n"));
117 hive_h *h = hivex_open (argv[optind], open_flags);
119 perror (argv[optind]);
123 /* Note both this macro, and xmlTextWriterStartDocument leak memory. There
124 * doesn't seem to be any way to recover that memory, but it's not a
129 xmlTextWriterPtr writer;
130 writer = xmlNewTextWriterFilename ("/dev/stdout", 0);
131 if (writer == NULL) {
132 fprintf (stderr, _("xmlNewTextWriterFilename: failed to create XML writer\n"));
136 XML_CHECK (xmlTextWriterStartDocument, (writer, NULL, "utf-8", NULL));
137 XML_CHECK (xmlTextWriterStartElement, (writer, BAD_CAST "hive"));
139 int64_t hive_mtime = hivex_last_modified (h);
140 if (hive_mtime >= 0) {
141 char *timebuf = filetime_to_8601 (hive_mtime);
143 XML_CHECK (xmlTextWriterStartElement, (writer, BAD_CAST "mtime"));
144 XML_CHECK (xmlTextWriterWriteString, (writer, BAD_CAST timebuf));
145 XML_CHECK (xmlTextWriterEndElement, (writer));
150 if (hivex_visit (h, &visitor, sizeof visitor, writer, visit_flags) == -1) {
151 perror (argv[optind]);
155 if (hivex_close (h) == -1) {
156 perror (argv[optind]);
160 XML_CHECK (xmlTextWriterEndElement, (writer));
161 XML_CHECK (xmlTextWriterEndDocument, (writer));
162 xmlFreeTextWriter (writer);
167 /* Convert Windows filetime to ISO 8601 format.
168 * http://stackoverflow.com/questions/6161776/convert-windows-filetime-to-second-in-unix-linux/6161842#6161842
170 * Source for time_t->char* conversion: Fiwalk version 0.6.14's
173 * The caller should free the returned buffer.
175 * This function returns NULL on a 0 input. In the context of
176 * hives, which only have mtimes, 0 will always be a complete
180 #define WINDOWS_TICK 10000000LL
181 #define SEC_TO_UNIX_EPOCH 11644473600LL
182 #define TIMESTAMP_BUF_LEN 32
185 filetime_to_8601 (int64_t windows_ticks)
191 if (windows_ticks == 0LL)
194 t = windows_ticks / WINDOWS_TICK - SEC_TO_UNIX_EPOCH;
199 ret = malloc (TIMESTAMP_BUF_LEN);
205 if (strftime (ret, TIMESTAMP_BUF_LEN, "%FT%TZ", tm) == 0) {
214 node_start (hive_h *h, void *writer_v, hive_node_h node, const char *name)
216 int64_t last_modified;
220 xmlTextWriterPtr writer = (xmlTextWriterPtr) writer_v;
221 XML_CHECK (xmlTextWriterStartElement, (writer, BAD_CAST "node"));
222 XML_CHECK (xmlTextWriterWriteAttribute, (writer, BAD_CAST "name", BAD_CAST name));
224 if (node == hivex_root (h)) {
225 XML_CHECK (xmlTextWriterWriteAttribute, (writer, BAD_CAST "root", BAD_CAST "1"));
228 last_modified = hivex_node_timestamp (h, node);
229 if (last_modified >= 0) {
230 timebuf = filetime_to_8601 (last_modified);
232 XML_CHECK (xmlTextWriterStartElement, (writer, BAD_CAST "mtime"));
233 XML_CHECK (xmlTextWriterWriteString, (writer, BAD_CAST timebuf));
234 XML_CHECK (xmlTextWriterEndElement, (writer));
243 node_end (hive_h *h, void *writer_v, hive_node_h node, const char *name)
245 xmlTextWriterPtr writer = (xmlTextWriterPtr) writer_v;
246 XML_CHECK (xmlTextWriterEndElement, (writer));
251 start_value (xmlTextWriterPtr writer,
252 const char *key, const char *type, const char *encoding)
254 XML_CHECK (xmlTextWriterStartElement, (writer, BAD_CAST "value"));
255 XML_CHECK (xmlTextWriterWriteAttribute, (writer, BAD_CAST "type", BAD_CAST type));
257 XML_CHECK (xmlTextWriterWriteAttribute, (writer, BAD_CAST "encoding", BAD_CAST encoding));
259 XML_CHECK (xmlTextWriterWriteAttribute, (writer, BAD_CAST "key", BAD_CAST key));
260 else /* default key */
261 XML_CHECK (xmlTextWriterWriteAttribute, (writer, BAD_CAST "default", BAD_CAST "1"));
265 end_value (xmlTextWriterPtr writer)
267 XML_CHECK (xmlTextWriterEndElement, (writer));
271 value_string (hive_h *h, void *writer_v, hive_node_h node, hive_value_h value,
272 hive_type t, size_t len, const char *key, const char *str)
274 xmlTextWriterPtr writer = (xmlTextWriterPtr) writer_v;
278 case hive_t_string: type = "string"; break;
279 case hive_t_expand_string: type = "expand"; break;
280 case hive_t_link: type = "link"; break;
285 case hive_t_dword_be:
286 case hive_t_multiple_strings:
287 case hive_t_resource_list:
288 case hive_t_full_resource_description:
289 case hive_t_resource_requirements_list:
291 abort (); /* internal error - should not happen */
297 start_value (writer, key, type, NULL);
298 XML_CHECK (xmlTextWriterStartAttribute, (writer, BAD_CAST "value"));
299 XML_CHECK (xmlTextWriterWriteString, (writer, BAD_CAST str));
300 XML_CHECK (xmlTextWriterEndAttribute, (writer));
306 value_multiple_strings (hive_h *h, void *writer_v, hive_node_h node,
307 hive_value_h value, hive_type t, size_t len,
308 const char *key, char **argv)
310 xmlTextWriterPtr writer = (xmlTextWriterPtr) writer_v;
311 start_value (writer, key, "string-list", NULL);
314 for (i = 0; argv[i] != NULL; ++i) {
315 XML_CHECK (xmlTextWriterStartElement, (writer, BAD_CAST "string"));
316 XML_CHECK (xmlTextWriterWriteString, (writer, BAD_CAST argv[i]));
317 XML_CHECK (xmlTextWriterEndElement, (writer));
325 value_string_invalid_utf16 (hive_h *h, void *writer_v, hive_node_h node,
326 hive_value_h value, hive_type t, size_t len,
328 const char *str /* original data */)
330 xmlTextWriterPtr writer = (xmlTextWriterPtr) writer_v;
334 case hive_t_string: type = "bad-string"; break;
335 case hive_t_expand_string: type = "bad-expand"; break;
336 case hive_t_link: type = "bad-link"; break;
337 case hive_t_multiple_strings: type = "bad-string-list"; break;
342 case hive_t_dword_be:
343 case hive_t_resource_list:
344 case hive_t_full_resource_description:
345 case hive_t_resource_requirements_list:
347 abort (); /* internal error - should not happen */
353 start_value (writer, key, type, "base64");
354 XML_CHECK (xmlTextWriterStartAttribute, (writer, BAD_CAST "value"));
355 XML_CHECK (xmlTextWriterWriteBase64, (writer, str, 0, len));
356 XML_CHECK (xmlTextWriterEndAttribute, (writer));
363 value_dword (hive_h *h, void *writer_v, hive_node_h node, hive_value_h value,
364 hive_type t, size_t len, const char *key, int32_t v)
366 xmlTextWriterPtr writer = (xmlTextWriterPtr) writer_v;
367 start_value (writer, key, "int32", NULL);
368 XML_CHECK (xmlTextWriterWriteFormatAttribute, (writer, BAD_CAST "value", "%" PRIi32, v));
374 value_qword (hive_h *h, void *writer_v, hive_node_h node, hive_value_h value,
375 hive_type t, size_t len, const char *key, int64_t v)
377 xmlTextWriterPtr writer = (xmlTextWriterPtr) writer_v;
378 start_value (writer, key, "int64", NULL);
379 XML_CHECK (xmlTextWriterWriteFormatAttribute, (writer, BAD_CAST "value", "%" PRIi64, v));
385 value_binary (hive_h *h, void *writer_v, hive_node_h node, hive_value_h value,
386 hive_type t, size_t len, const char *key, const char *v)
388 xmlTextWriterPtr writer = (xmlTextWriterPtr) writer_v;
389 start_value (writer, key, "binary", "base64");
390 XML_CHECK (xmlTextWriterStartAttribute, (writer, BAD_CAST "value"));
391 XML_CHECK (xmlTextWriterWriteBase64, (writer, v, 0, len));
392 XML_CHECK (xmlTextWriterEndAttribute, (writer));
398 value_none (hive_h *h, void *writer_v, hive_node_h node, hive_value_h value,
399 hive_type t, size_t len, const char *key, const char *v)
401 xmlTextWriterPtr writer = (xmlTextWriterPtr) writer_v;
402 start_value (writer, key, "none", "base64");
404 XML_CHECK (xmlTextWriterStartAttribute, (writer, BAD_CAST "value"));
405 XML_CHECK (xmlTextWriterWriteBase64, (writer, v, 0, len));
406 XML_CHECK (xmlTextWriterEndAttribute, (writer));
413 value_other (hive_h *h, void *writer_v, hive_node_h node, hive_value_h value,
414 hive_type t, size_t len, const char *key, const char *v)
416 xmlTextWriterPtr writer = (xmlTextWriterPtr) writer_v;
423 case hive_t_dword_be:
426 case hive_t_expand_string:
428 case hive_t_multiple_strings:
429 abort (); /* internal error - should not happen */
431 case hive_t_resource_list: type = "resource-list"; break;
432 case hive_t_full_resource_description: type = "resource-description"; break;
433 case hive_t_resource_requirements_list: type = "resource-requirements"; break;
439 start_value (writer, key, type, "base64");
441 XML_CHECK (xmlTextWriterStartAttribute, (writer, BAD_CAST "value"));
442 XML_CHECK (xmlTextWriterWriteBase64, (writer, v, 0, len));
443 XML_CHECK (xmlTextWriterEndAttribute, (writer));