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.
35 #include <libxml/xmlwriter.h>
41 #define _(str) dgettext(PACKAGE, (str))
42 //#define N_(str) dgettext(PACKAGE, (str))
48 static char *filetime_to_8601 (int64_t windows_ticks);
50 /* Callback functions. */
51 static int node_start (hive_h *, void *, hive_node_h, const char *name);
52 static int node_end (hive_h *, void *, hive_node_h, const char *name);
53 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);
54 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);
55 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);
56 static int value_dword (hive_h *, void *, hive_node_h, hive_value_h, hive_type t, size_t len, const char *key, int32_t);
57 static int value_qword (hive_h *, void *, hive_node_h, hive_value_h, hive_type t, size_t len, const char *key, int64_t);
58 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);
59 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);
60 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);
62 static struct hivex_visitor visitor = {
63 .node_start = node_start,
65 .value_string = value_string,
66 .value_multiple_strings = value_multiple_strings,
67 .value_string_invalid_utf16 = value_string_invalid_utf16,
68 .value_dword = value_dword,
69 .value_qword = value_qword,
70 .value_binary = value_binary,
71 .value_none = value_none,
72 .value_other = value_other
75 #define XML_CHECK(proc, args) \
77 if ((proc args) == -1) { \
78 fprintf (stderr, _("%s: failed to write XML document\n"), #proc); \
79 exit (EXIT_FAILURE); \
84 main (int argc, char *argv[])
86 setlocale (LC_ALL, "");
87 #ifdef HAVE_BINDTEXTDOMAIN
88 bindtextdomain (PACKAGE, LOCALEBASEDIR);
96 while ((c = getopt (argc, argv, "dk")) != EOF) {
99 open_flags |= HIVEX_OPEN_DEBUG;
102 visit_flags |= HIVEX_VISIT_SKIP_BAD;
105 fprintf (stderr, "hivexml [-dk] regfile > output.xml\n");
110 if (optind + 1 != argc) {
111 fprintf (stderr, _("hivexml: missing name of input file\n"));
115 hive_h *h = hivex_open (argv[optind], open_flags);
117 perror (argv[optind]);
121 /* Note both this macro, and xmlTextWriterStartDocument leak memory. There
122 * doesn't seem to be any way to recover that memory, but it's not a
127 xmlTextWriterPtr writer;
128 writer = xmlNewTextWriterFilename ("/dev/stdout", 0);
129 if (writer == NULL) {
130 fprintf (stderr, _("xmlNewTextWriterFilename: failed to create XML writer\n"));
134 XML_CHECK (xmlTextWriterStartDocument, (writer, NULL, "utf-8", NULL));
135 XML_CHECK (xmlTextWriterStartElement, (writer, BAD_CAST "hive"));
137 int64_t hive_mtime = hivex_last_modified (h);
138 if (hive_mtime >= 0) {
139 char *timebuf = filetime_to_8601 (hive_mtime);
141 XML_CHECK (xmlTextWriterStartElement, (writer, BAD_CAST "mtime"));
142 XML_CHECK (xmlTextWriterWriteString, (writer, BAD_CAST timebuf));
143 XML_CHECK (xmlTextWriterEndElement, (writer));
148 if (hivex_visit (h, &visitor, sizeof visitor, writer, visit_flags) == -1) {
149 perror (argv[optind]);
153 if (hivex_close (h) == -1) {
154 perror (argv[optind]);
158 XML_CHECK (xmlTextWriterEndElement, (writer));
159 XML_CHECK (xmlTextWriterEndDocument, (writer));
160 xmlFreeTextWriter (writer);
165 /* Convert Windows filetime to ISO 8601 format.
166 * http://stackoverflow.com/questions/6161776/convert-windows-filetime-to-second-in-unix-linux/6161842#6161842
168 * Source for time_t->char* conversion: Fiwalk version 0.6.14's
171 * The caller should free the returned buffer.
174 #define WINDOWS_TICK 10000000LL
175 #define SEC_TO_UNIX_EPOCH 11644473600LL
176 #define TIMESTAMP_BUF_LEN 32
179 filetime_to_8601 (int64_t windows_ticks)
185 t = windows_ticks / WINDOWS_TICK - SEC_TO_UNIX_EPOCH;
190 ret = malloc (TIMESTAMP_BUF_LEN);
196 if (strftime (ret, TIMESTAMP_BUF_LEN, "%FT%TZ", tm) == 0) {
205 node_start (hive_h *h, void *writer_v, hive_node_h node, const char *name)
207 int64_t last_modified;
210 xmlTextWriterPtr writer = (xmlTextWriterPtr) writer_v;
211 XML_CHECK (xmlTextWriterStartElement, (writer, BAD_CAST "node"));
212 XML_CHECK (xmlTextWriterWriteAttribute, (writer, BAD_CAST "name", BAD_CAST name));
214 if (node == hivex_root (h)) {
215 XML_CHECK (xmlTextWriterWriteAttribute, (writer, BAD_CAST "root", BAD_CAST "1"));
218 last_modified = hivex_node_timestamp (h, node);
219 if (last_modified >= 0) {
220 timebuf = filetime_to_8601 (last_modified);
222 XML_CHECK (xmlTextWriterStartElement, (writer, BAD_CAST "mtime"));
223 XML_CHECK (xmlTextWriterWriteString, (writer, BAD_CAST timebuf));
224 XML_CHECK (xmlTextWriterEndElement, (writer));
233 node_end (hive_h *h, void *writer_v, hive_node_h node, const char *name)
235 xmlTextWriterPtr writer = (xmlTextWriterPtr) writer_v;
236 XML_CHECK (xmlTextWriterEndElement, (writer));
241 start_value (xmlTextWriterPtr writer,
242 const char *key, const char *type, const char *encoding)
244 XML_CHECK (xmlTextWriterStartElement, (writer, BAD_CAST "value"));
245 XML_CHECK (xmlTextWriterWriteAttribute, (writer, BAD_CAST "type", BAD_CAST type));
247 XML_CHECK (xmlTextWriterWriteAttribute, (writer, BAD_CAST "encoding", BAD_CAST encoding));
249 XML_CHECK (xmlTextWriterWriteAttribute, (writer, BAD_CAST "key", BAD_CAST key));
250 else /* default key */
251 XML_CHECK (xmlTextWriterWriteAttribute, (writer, BAD_CAST "default", BAD_CAST "1"));
255 end_value (xmlTextWriterPtr writer)
257 XML_CHECK (xmlTextWriterEndElement, (writer));
261 value_string (hive_h *h, void *writer_v, hive_node_h node, hive_value_h value,
262 hive_type t, size_t len, const char *key, const char *str)
264 xmlTextWriterPtr writer = (xmlTextWriterPtr) writer_v;
268 case hive_t_string: type = "string"; break;
269 case hive_t_expand_string: type = "expand"; break;
270 case hive_t_link: type = "link"; break;
275 case hive_t_dword_be:
276 case hive_t_multiple_strings:
277 case hive_t_resource_list:
278 case hive_t_full_resource_description:
279 case hive_t_resource_requirements_list:
281 abort (); /* internal error - should not happen */
287 start_value (writer, key, type, NULL);
288 XML_CHECK (xmlTextWriterWriteString, (writer, BAD_CAST str));
294 value_multiple_strings (hive_h *h, void *writer_v, hive_node_h node,
295 hive_value_h value, hive_type t, size_t len,
296 const char *key, char **argv)
298 xmlTextWriterPtr writer = (xmlTextWriterPtr) writer_v;
299 start_value (writer, key, "string-list", NULL);
302 for (i = 0; argv[i] != NULL; ++i) {
303 XML_CHECK (xmlTextWriterStartElement, (writer, BAD_CAST "string"));
304 XML_CHECK (xmlTextWriterWriteString, (writer, BAD_CAST argv[i]));
305 XML_CHECK (xmlTextWriterEndElement, (writer));
313 value_string_invalid_utf16 (hive_h *h, void *writer_v, hive_node_h node,
314 hive_value_h value, hive_type t, size_t len,
316 const char *str /* original data */)
318 xmlTextWriterPtr writer = (xmlTextWriterPtr) writer_v;
322 case hive_t_string: type = "bad-string"; break;
323 case hive_t_expand_string: type = "bad-expand"; break;
324 case hive_t_link: type = "bad-link"; break;
325 case hive_t_multiple_strings: type = "bad-string-list"; break;
330 case hive_t_dword_be:
331 case hive_t_resource_list:
332 case hive_t_full_resource_description:
333 case hive_t_resource_requirements_list:
335 abort (); /* internal error - should not happen */
341 start_value (writer, key, type, "base64");
342 XML_CHECK (xmlTextWriterWriteBase64, (writer, str, 0, len));
349 value_dword (hive_h *h, void *writer_v, hive_node_h node, hive_value_h value,
350 hive_type t, size_t len, const char *key, int32_t v)
352 xmlTextWriterPtr writer = (xmlTextWriterPtr) writer_v;
353 start_value (writer, key, "int32", NULL);
354 XML_CHECK (xmlTextWriterWriteFormatString, (writer, "%" PRIi32, v));
360 value_qword (hive_h *h, void *writer_v, hive_node_h node, hive_value_h value,
361 hive_type t, size_t len, const char *key, int64_t v)
363 xmlTextWriterPtr writer = (xmlTextWriterPtr) writer_v;
364 start_value (writer, key, "int64", NULL);
365 XML_CHECK (xmlTextWriterWriteFormatString, (writer, "%" PRIi64, v));
371 value_binary (hive_h *h, void *writer_v, hive_node_h node, hive_value_h value,
372 hive_type t, size_t len, const char *key, const char *v)
374 xmlTextWriterPtr writer = (xmlTextWriterPtr) writer_v;
375 start_value (writer, key, "binary", "base64");
376 XML_CHECK (xmlTextWriterWriteBase64, (writer, v, 0, len));
382 value_none (hive_h *h, void *writer_v, hive_node_h node, hive_value_h value,
383 hive_type t, size_t len, const char *key, const char *v)
385 xmlTextWriterPtr writer = (xmlTextWriterPtr) writer_v;
386 start_value (writer, key, "none", "base64");
387 if (len > 0) XML_CHECK (xmlTextWriterWriteBase64, (writer, v, 0, len));
393 value_other (hive_h *h, void *writer_v, hive_node_h node, hive_value_h value,
394 hive_type t, size_t len, const char *key, const char *v)
396 xmlTextWriterPtr writer = (xmlTextWriterPtr) writer_v;
403 case hive_t_dword_be:
406 case hive_t_expand_string:
408 case hive_t_multiple_strings:
409 abort (); /* internal error - should not happen */
411 case hive_t_resource_list: type = "resource-list"; break;
412 case hive_t_full_resource_description: type = "resource-description"; break;
413 case hive_t_resource_requirements_list: type = "resource-requirements"; break;
419 start_value (writer, key, type, "base64");
420 if (len > 0) XML_CHECK (xmlTextWriterWriteBase64, (writer, v, 0, len));