]> wimlib.net Git - wimlib/blob - src/xml.c
77e58757bea26042b8364696a9d10ec17d1d06bd
[wimlib] / src / xml.c
1 /*
2  * xml.c
3  *
4  * Deals with the XML information in WIM files.  Uses the C library libxml2.
5  */
6
7 /*
8  * Copyright (C) 2012 Eric Biggers
9  *
10  * This file is part of wimlib, a library for working with WIM files.
11  *
12  * wimlib is free software; you can redistribute it and/or modify it under the
13  * terms of the GNU Lesser General Public License as published by the Free
14  * Software Foundation; either version 2.1 of the License, or (at your option)
15  * any later version.
16  *
17  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
18  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19  * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
20  * details.
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * along with wimlib; if not, see http://www.gnu.org/licenses/.
24  */
25
26 #include "wimlib_internal.h"
27 #include "dentry.h"
28 #include "xml.h"
29 #include "timestamp.h"
30 #include <string.h>
31
32 #include <libxml/parser.h>
33 #include <libxml/tree.h>
34 #include <libxml/xmlwriter.h>
35
36 /* Structures used to form an in-memory representation of the XML data (other
37  * than the raw parse tree from libxml). */
38
39 struct windows_version {
40         u64 major;
41         u64 minor;
42         u64 build;
43         u64 sp_build;
44         u64 sp_level;
45 };
46
47 struct windows_info {
48         u64    arch;
49         char  *product_name;
50         char  *edition_id;
51         char  *installation_type;
52         char  *hal;
53         char  *product_type;
54         char  *product_suite;
55         char **languages;
56         char  *default_language;
57         u64    num_languages;
58         char  *system_root;
59         bool   windows_version_exists;
60         struct windows_version windows_version;
61 };
62
63 struct image_info {
64         u64   index;
65         u64   dir_count;
66         u64   file_count;
67         u64   total_bytes;
68         u64   hard_link_bytes;
69         u64   creation_time;
70         u64   last_modification_time;
71         bool  windows_info_exists;
72         struct windows_info windows_info;
73         char *name;
74         char *description;
75         char  *display_name;
76         char  *display_description;
77         char  *flags;
78 };
79
80
81 /* Returns a statically allocated string that is a string representation of the
82  * architecture number. */
83 static const char *get_arch(int arch)
84 {
85         static char buf[20];
86         switch (arch) {
87         case 0:
88                 return "x86";
89         case 6:
90                 return "ia64";
91         case 9:
92                 return "x86_64";
93         /* XXX Are there other arch values? */
94         default:
95                 snprintf(buf, sizeof(buf), "%d (unknown)", arch);
96                 return buf;
97         }
98 }
99
100
101 /* Iterate through the children of an xmlNode. */
102 #define for_node_child(parent, child)   \
103         for (child = parent->children; child != NULL; child = child->next)
104
105 /* Utility functions for xmlNodes */
106 static inline bool node_is_element(xmlNode *node)
107 {
108         return node->type == XML_ELEMENT_NODE;
109 }
110
111 static inline bool node_is_text(xmlNode *node)
112 {
113         return node->type == XML_TEXT_NODE;
114 }
115
116 static inline bool node_is_attribute(xmlNode *node)
117 {
118         return node->type == XML_ATTRIBUTE_NODE;
119 }
120
121 static inline bool node_name_is(xmlNode *node, const char *name)
122 {
123         /* For now, both upper case and lower case element names are accepted. */
124         return strcasecmp((const char *)node->name, name) == 0;
125 }
126
127 /* Finds the text node that is a child of an element node and returns its
128  * content converted to a 64-bit unsigned integer.  Returns 0 if no text node is
129  * found. */
130 static u64 node_get_u64(const xmlNode *u64_node)
131 {
132         xmlNode *child;
133         for_node_child(u64_node, child)
134                 if (node_is_text(child))
135                         return strtoull((const char *)child->content, NULL, 10);
136         return 0;
137 }
138
139 /* Like node_get_u64(), but expects a number in base 16. */
140 static u64 node_get_hex_u64(const xmlNode *u64_node)
141 {
142         xmlNode *child;
143         for_node_child(u64_node, child)
144                 if (node_is_text(child))
145                         return strtoull(child->content, NULL, 16);
146         return 0;
147 }
148
149 static int node_get_string(const xmlNode *string_node, char **str)
150 {
151         xmlNode *child;
152         char *p = NULL;
153
154         for_node_child(string_node, child) {
155                 if (node_is_text(child) && child->content) {
156                         p = STRDUP(child->content);
157                         if (!p) {
158                                 ERROR("Out of memory");
159                                 return WIMLIB_ERR_NOMEM;
160                         }
161                         break;
162                 }
163         }
164         *str = p;
165         return 0;
166 }
167
168 /* Returns the timestamp from a time node.  It has child elements <HIGHPART> and
169  * <LOWPART> that are then used to construct a 64-bit timestamp. */
170 static u64 node_get_timestamp(const xmlNode *time_node)
171 {
172         u32 high_part = 0;
173         u32 low_part = 0;
174         xmlNode *child;
175         for_node_child(time_node, child) {
176                 if (!node_is_element(child))
177                         continue;
178                 if (node_name_is(child, "HIGHPART"))
179                         high_part = node_get_hex_u64(child);
180                 else if (node_name_is(child, "LOWPART"))
181                         low_part = node_get_hex_u64(child);
182         }
183         return (u64)low_part | ((u64)high_part << 32);
184 }
185
186 /* Used to sort an array of struct image_infos by their image indices. */
187 static int sort_by_index(const void *p1, const void *p2)
188 {
189         u64 index_1 = ((struct image_info*)p1)->index;
190         u64 index_2 = ((struct image_info*)p1)->index;
191         if (index_1 < index_2)
192                 return -1;
193         else if (index_1 > index_2)
194                 return 1;
195         else
196                 return 0;
197 }
198
199
200 /* Frees memory allocated inside a struct windows_info structure. */
201 static void destroy_windows_info(struct windows_info *windows_info)
202 {
203         uint i;
204
205         FREE(windows_info->product_name);
206         FREE(windows_info->edition_id);
207         FREE(windows_info->installation_type);
208         FREE(windows_info->product_type);
209         for (i = 0; i < windows_info->num_languages; i++)
210                 FREE(windows_info->languages[i]);
211         FREE(windows_info->languages);
212         FREE(windows_info->system_root);
213 }
214
215 /* Frees memory allocated inside a struct image_info structure. */
216 static void destroy_image_info(struct image_info *image_info)
217 {
218         FREE(image_info->name);
219         FREE(image_info->description);
220         FREE(image_info->flags);
221         FREE(image_info->display_name);
222         FREE(image_info->display_description);
223         destroy_windows_info(&image_info->windows_info);
224         memset(image_info, 0, sizeof(struct image_info));
225 }
226
227 void free_wim_info(struct wim_info *info)
228 {
229         uint i;
230         if (info) {
231                 if (info->images) {
232                         for (i = 0; i < info->num_images; i++)
233                                 destroy_image_info(&info->images[i]);
234                         FREE(info->images);
235                 }
236                 FREE(info);
237         }
238 }
239
240 /* Reads the information from a <VERSION> element inside the <WINDOWS> element.
241  * */
242 static void xml_read_windows_version(const xmlNode *version_node, 
243                                      struct windows_version* windows_version)
244 {
245         xmlNode *child;
246         for_node_child(version_node, child) {
247                 if (!node_is_element(child))
248                         continue;
249                 if (node_name_is(child, "MAJOR"))
250                         windows_version->major    = node_get_u64(child);
251                 else if (node_name_is(child, "MINOR"))
252                         windows_version->minor    = node_get_u64(child);
253                 else if (node_name_is(child, "BUILD"))
254                         windows_version->build    = node_get_u64(child);
255                 else if (node_name_is(child, "SPBUILD"))
256                         windows_version->sp_build = node_get_u64(child);
257                 else if (node_name_is(child, "SPLEVEL"))
258                         windows_version->sp_level = node_get_u64(child);
259         }
260 }
261
262 /* Reads the information from a <LANGUAGE> element inside a <WINDOWS> element.
263  * */
264 static int xml_read_languages(const xmlNode *languages_node, 
265                               char ***languages_ret, 
266                               u64 *num_languages_ret,
267                               char **default_language_ret)
268 {
269         xmlNode *child;
270         uint i;
271         uint num_languages;
272         char **languages;
273         int ret;
274
275         num_languages = 0;
276         for_node_child(languages_node, child)
277                 if (node_is_element(child) && node_name_is(child, "LANGUAGE"))
278                         num_languages++;
279
280         languages = CALLOC(num_languages, sizeof(char*));
281         if (!languages) {
282                 ERROR("Out of memory");
283                 return WIMLIB_ERR_NOMEM;
284         }
285
286         *languages_ret = languages;
287         *num_languages_ret = num_languages;
288
289         i = 0;
290         ret = 0;
291         for_node_child(languages_node, child) {
292                 if (!node_is_element(child))
293                         continue;
294                 if (node_name_is(child, "LANGUAGE"))
295                         ret = node_get_string(child, &languages[i++]);
296                 else if (node_name_is(child, "DEFAULT"))
297                         ret = node_get_string(child, default_language_ret);
298                 if (ret != 0)
299                         return ret;
300         }
301         return ret;
302 }
303
304 /* Reads the information from a <WINDOWS> element inside an <IMAGE> element. */
305 static int xml_read_windows_info(const xmlNode *windows_node, 
306                                  struct windows_info *windows_info)
307 {
308         xmlNode *child;
309         int ret = 0;
310
311         for_node_child(windows_node, child) {
312                 if (!node_is_element(child))
313                         continue;
314                 if (node_name_is(child, "ARCH")) {
315                         windows_info->arch = node_get_u64(child);
316                 } else if (node_name_is(child, "PRODUCTNAME")) {
317                         ret = node_get_string(child, 
318                                               &windows_info->product_name);
319                 } else if (node_name_is(child, "EDITIONID")) {
320                         ret = node_get_string(child, 
321                                               &windows_info->edition_id);
322                 } else if (node_name_is(child, "INSTALLATIONTYPE")) {
323                         ret = node_get_string(child, 
324                                               &windows_info->installation_type);
325                 } else if (node_name_is(child, "PRODUCTTYPE")) {
326                         ret = node_get_string(child, 
327                                               &windows_info->product_type);
328                 } else if (node_name_is(child, "PRODUCTSUITE")) {
329                         ret = node_get_string(child, 
330                                               &windows_info->product_suite);
331                 } else if (node_name_is(child, "LANGUAGES")) {
332                         ret = xml_read_languages(child, 
333                                                  &windows_info->languages,
334                                                  &windows_info->num_languages,
335                                                  &windows_info->default_language);
336                 } else if (node_name_is(child, "VERSION")) {
337                         xml_read_windows_version(child, 
338                                                 &windows_info->windows_version);
339                         windows_info->windows_version_exists = true;
340                 } else if (node_name_is(child, "SYSTEMROOT")) {
341                         ret = node_get_string(child, &windows_info->system_root);
342                 } else if (node_name_is(child, "HAL")) {
343                         ret = node_get_string(child, &windows_info->hal);
344                 }
345
346                 if (ret != 0)
347                         return ret;
348         }
349         return ret;
350 }
351
352 /* Reads the information from an <IMAGE> element. */
353 static int xml_read_image_info(xmlNode *image_node, 
354                                struct image_info *image_info)
355 {
356         xmlNode *child;
357         xmlChar *index_prop;
358         int ret;
359         
360         index_prop = xmlGetProp(image_node, "INDEX");
361         if (index_prop) {
362                 char *tmp;
363                 image_info->index = strtoul(index_prop, &tmp, 10);
364                 FREE(index_prop);
365         } else {
366                 image_info->index = 0;
367         }
368
369         ret = 0;
370         for_node_child(image_node, child) {
371                 if (!node_is_element(child))
372                         continue;
373                 if (node_name_is(child, "DIRCOUNT"))
374                         image_info->dir_count = node_get_u64(child);
375                 else if (node_name_is(child, "FILECOUNT"))
376                         image_info->file_count = node_get_u64(child);
377                 else if (node_name_is(child, "TOTALBYTES"))
378                         image_info->total_bytes = node_get_u64(child);
379                 else if (node_name_is(child, "HARDLINKBYTES"))
380                         image_info->hard_link_bytes = node_get_u64(child);
381                 else if (node_name_is(child, "CREATIONTIME"))
382                         image_info->creation_time = node_get_timestamp(child);
383                 else if (node_name_is(child, "LASTMODIFICATIONTIME"))
384                         image_info->last_modification_time = node_get_timestamp(child);
385                 else if (node_name_is(child, "WINDOWS")) {
386                         DEBUG("Found <WINDOWS> tag");
387                         ret = xml_read_windows_info(child,
388                                                     &image_info->windows_info);
389                         image_info->windows_info_exists = true;
390                 } else if (node_name_is(child, "NAME")) {
391                         ret = node_get_string(child, &image_info->name);
392                 } else if (node_name_is(child, "DESCRIPTION")) {
393                         ret = node_get_string(child, &image_info->description);
394                 } else if (node_name_is(child, "FLAGS")) {
395                         ret = node_get_string(child, &image_info->flags);
396                 } else if (node_name_is(child, "DISPLAYNAME")) {
397                         ret = node_get_string(child, &image_info->display_name);
398                 } else if (node_name_is(child, "DISPLAYDESCRIPTION")) {
399                         ret = node_get_string(child, &image_info->display_description);
400                 }
401                 if (ret != 0)
402                         return ret;
403         }
404         if (!image_info->name) {
405                 WARNING("Image with index %"PRIu64" has no name",
406                         image_info->index);
407                 image_info->name = MALLOC(1);
408                 if (!image_info->name) {
409                         ERROR("Out of memory");
410                         return WIMLIB_ERR_NOMEM;
411                 }
412                 image_info->name[0] = '\0';
413         }
414         return ret;
415 }
416
417 /* Reads the information from a <WIM> element, which should be the root element
418  * of the XML tree. */
419 static int xml_read_wim_info(const xmlNode *wim_node,
420                              struct wim_info **wim_info_ret)
421 {
422         struct wim_info *wim_info;
423         xmlNode *child;
424         int ret;
425         uint num_images;
426         struct image_info *cur_image_info;
427
428         wim_info = CALLOC(1, sizeof(struct wim_info));
429         if (!wim_info) {
430                 ERROR("Out of memory");
431                 return WIMLIB_ERR_NOMEM;
432         }
433
434         /* Count how many images there are. */
435         num_images = 0;
436         for_node_child(wim_node, child)
437                 if (node_is_element(child) && node_name_is(child, "IMAGE"))
438                         num_images++;
439
440         if (num_images == 0)
441                 goto done;
442
443         /* Allocate the array of struct image_infos and fill them in. */
444         wim_info->images = CALLOC(num_images, sizeof(wim_info->images[0]));
445         if (!wim_info->images) {
446                 ret = WIMLIB_ERR_NOMEM;
447                 ERROR("Out of memory!");
448                 goto err;
449         }
450         wim_info->num_images = num_images;
451         cur_image_info = wim_info->images;
452         for_node_child(wim_node, child) {
453                 if (!node_is_element(child))
454                         continue;
455                 if (node_name_is(child, "IMAGE")) {
456                         DEBUG("Found <IMAGE> tag");
457                         ret = xml_read_image_info(child, cur_image_info++);
458                         if (ret != 0)
459                                 goto err;
460                 } else if (node_name_is(child, "TOTALBYTES")) {
461                         wim_info->total_bytes = node_get_u64(child);
462                 }
463         }
464
465         /* Sort the array of struct image_infos by image index. */
466         qsort(wim_info->images, wim_info->num_images, 
467               sizeof(struct image_info), sort_by_index);
468 done:
469         *wim_info_ret = wim_info;
470         return 0;
471 err:
472         free_wim_info(wim_info);
473         return ret;
474 }
475
476 /* Prints the information contained in a struct windows_info structure. */
477 static void print_windows_info(const struct windows_info *windows_info)
478 {
479         uint i;
480         const struct windows_version *windows_version;
481
482         printf("Architecture:           %s\n", get_arch(windows_info->arch));
483         printf("Product Name:           %s\n", windows_info->product_name);
484         printf("Edition ID:             %s\n", windows_info->edition_id);
485         printf("Installation Type:      %s\n", windows_info->installation_type);
486         if (windows_info->hal)
487                 printf("HAL:                    %s\n", windows_info->hal);
488         printf("Product Type:           %s\n", windows_info->product_type);
489         if (windows_info->product_suite)
490                 printf("Product Suite:          %s\n", windows_info->product_suite);
491         printf("Languages:              ");
492         for (i = 0; i < windows_info->num_languages; i++) {
493                 fputs(windows_info->languages[i], stdout);
494                 putchar(' ');
495         }
496         putchar('\n');
497         printf("Default Language:       %s\n", windows_info->default_language);
498         printf("System Root:            %s\n", windows_info->system_root);
499         if (windows_info->windows_version_exists) {
500                 windows_version = &windows_info->windows_version;
501                 printf("Major Version:          %"PRIu64"\n", 
502                                 windows_version->major);
503                 printf("Minor Version:          %"PRIu64"\n", 
504                                 windows_version->minor);
505                 printf("Build:                  %"PRIu64"\n", 
506                                 windows_version->build);
507                 printf("Service Pack Build:     %"PRIu64"\n", 
508                                 windows_version->sp_build);
509                 printf("Service Pack Level:     %"PRIu64"\n", 
510                                 windows_version->sp_level);
511         }
512 }
513
514
515 /* Writes the information contained in a struct windows_version structure to the XML
516  * document being constructed in memory.  This is the <VERSION> element inside
517  * the <WINDOWS> element. */
518 static int xml_write_windows_version(xmlTextWriter *writer, 
519                                         const struct windows_version *version)
520 {
521         int rc;
522         rc = xmlTextWriterStartElement(writer, "VERSION");
523         if (rc < 0)
524                 return rc;
525
526         rc = xmlTextWriterWriteFormatElement(writer, "MAJOR", "%"PRIu64, 
527                                                                 version->major);
528         if (rc < 0)
529                 return rc;
530
531         rc = xmlTextWriterWriteFormatElement(writer, "MINOR", "%"PRIu64, 
532                                                                 version->minor);
533         if (rc < 0)
534                 return rc;
535
536         rc = xmlTextWriterWriteFormatElement(writer, "BUILD", "%"PRIu64, 
537                                                                 version->build);
538         if (rc < 0)
539                 return rc;
540
541         rc = xmlTextWriterWriteFormatElement(writer, "SPBUILD", "%"PRIu64, 
542                                                                 version->sp_build);
543         if (rc < 0)
544                 return rc;
545
546         rc = xmlTextWriterWriteFormatElement(writer, "SPLEVEL", "%"PRIu64, 
547                                                                 version->sp_level);
548         if (rc < 0)
549                 return rc;
550
551         return xmlTextWriterEndElement(writer); /* </VERSION> */
552 }
553
554 /* Writes the information contained in a struct windows_info structure to the XML
555  * document being constructed in memory. This is the <WINDOWS> element. */
556 static int xml_write_windows_info(xmlTextWriter *writer, 
557                                         const struct windows_info *windows_info)
558 {
559         int rc;
560         rc = xmlTextWriterStartElement(writer, "WINDOWS");
561         if (rc < 0)
562                 return rc;
563
564
565         rc = xmlTextWriterWriteFormatElement(writer, "ARCH", "%"PRIu64, 
566                                                         windows_info->arch);
567         if (rc < 0)
568                 return rc;
569         
570         if (windows_info->product_name) {
571                 rc = xmlTextWriterWriteElement(writer, "PRODUCTNAME", 
572                                                         windows_info->product_name);
573                 if (rc < 0)
574                         return rc;
575         }
576
577         if (windows_info->edition_id) {
578                 rc = xmlTextWriterWriteElement(writer, "EDITIONID", 
579                                                         windows_info->edition_id);
580                 if (rc < 0)
581                         return rc;
582         }
583
584         if (windows_info->installation_type) {
585                 rc = xmlTextWriterWriteElement(writer, "INSTALLATIONTYPE", 
586                                                         windows_info->installation_type);
587                 if (rc < 0)
588                         return rc;
589         }
590
591         if (windows_info->hal) {
592                 rc = xmlTextWriterWriteElement(writer, "HAL", 
593                                                         windows_info->hal);
594                 if (rc < 0)
595                         return rc;
596         }
597
598         if (windows_info->system_root) {
599                 rc = xmlTextWriterWriteElement(writer, "SYSTEMROOT", 
600                                                 windows_info->system_root);
601                         if (rc < 0)
602                                 return rc;
603         }
604
605         if (windows_info->product_type) {
606                 rc = xmlTextWriterWriteElement(writer, "PRODUCTTYPE", 
607                                                 windows_info->product_type);
608                 if (rc < 0)
609                         return rc;
610         }
611
612         if (windows_info->product_suite) {
613                 rc = xmlTextWriterWriteElement(writer, "PRODUCTSUITE", 
614                                                 windows_info->product_suite);
615                         if (rc < 0)
616                                 return rc;
617         }
618
619         if (windows_info->num_languages) {
620                 rc = xmlTextWriterStartElement(writer, "LANGUAGES");
621                 if (rc < 0)
622                         return rc;
623
624                 for (int i = 0; i < windows_info->num_languages; i++) {
625                         rc = xmlTextWriterWriteElement(writer, "LANGUAGE", 
626                                                         windows_info->languages[i]);
627                         if (rc < 0)
628                                 return rc;
629                 }
630                 rc = xmlTextWriterWriteElement(writer, "DEFAULT", 
631                                                 windows_info->default_language);
632                 if (rc < 0)
633                         return rc;
634
635                 rc = xmlTextWriterEndElement(writer); /* </LANGUAGES> */
636                 if (rc < 0)
637                         return rc;
638         }
639
640         if (windows_info->windows_version_exists) {
641                 rc = xml_write_windows_version(writer, &windows_info->windows_version);
642                 if (rc < 0)
643                         return rc;
644         }
645
646         return xmlTextWriterEndElement(writer); /* </WINDOWS> */
647 }
648
649 /* Writes a time element to the XML document being constructed in memory. */
650 static int xml_write_time(xmlTextWriter *writer, const char *element_name, 
651                                                                 u64 time) 
652 {
653         int rc;
654         rc = xmlTextWriterStartElement(writer, element_name);
655         if (rc < 0)
656                 return rc;
657
658         rc = xmlTextWriterWriteFormatElement(writer, "HIGHPART", 
659                                         "0x%"PRIX32, (u32)(time >> 32));
660         if (rc < 0)
661                 return rc;
662
663         rc = xmlTextWriterWriteFormatElement(writer, "LOWPART",
664                                                 "0x%"PRIX32, (u32)time);
665         if (rc < 0)
666                 return rc;
667
668         rc = xmlTextWriterEndElement(writer); /* </@element_name> */
669         if (rc < 0)
670                 return rc;
671         return 0;
672 }
673
674
675 /* Writes an <IMAGE> element to the XML document. */
676 static int xml_write_image_info(xmlTextWriter *writer, 
677                                 const struct image_info *image_info)
678 {
679         int rc;
680         rc = xmlTextWriterStartElement(writer, "IMAGE");
681         if (rc < 0)
682                 return rc;
683
684         rc = xmlTextWriterWriteFormatAttribute(writer, "INDEX", "%"PRIu64, 
685                                                 image_info->index);
686         if (rc < 0)
687                 return rc;
688
689         rc = xmlTextWriterWriteFormatElement(writer, "DIRCOUNT", "%"PRIu64, 
690                                                 image_info->dir_count);
691         if (rc < 0)
692                 return rc;
693
694         rc = xmlTextWriterWriteFormatElement(writer, "FILECOUNT", "%"PRIu64, 
695                                                 image_info->file_count);
696         if (rc < 0)
697                 return rc;
698
699         rc = xmlTextWriterWriteFormatElement(writer, "TOTALBYTES", "%"PRIu64, 
700                                                 image_info->total_bytes);
701         if (rc < 0)
702                 return rc;
703
704         rc = xmlTextWriterWriteFormatElement(writer, "HARDLINKBYTES", "%"PRIu64, 
705                                                 image_info->hard_link_bytes);
706         if (rc < 0)
707                 return rc;
708
709         rc = xml_write_time(writer, "CREATIONTIME", 
710                                                 image_info->creation_time);
711         if (rc < 0)
712                 return rc;
713
714         rc = xml_write_time(writer, "LASTMODIFICATIONTIME", 
715                                                 image_info->last_modification_time);
716         if (rc < 0)
717                 return rc;
718
719         if (image_info->windows_info_exists) {
720                 rc = xml_write_windows_info(writer, &image_info->windows_info);
721                 if (rc < 0)
722                         return rc;
723         } else {
724                 DEBUG("<WINDOWS> tag does not exist.");
725         }
726
727         if (image_info->name) {
728                 rc = xmlTextWriterWriteElement(writer, "NAME", image_info->name);
729                 if (rc < 0)
730                         return rc;
731         }
732         if (image_info->description) {
733                 rc = xmlTextWriterWriteElement(writer, "DESCRIPTION", 
734                                                         image_info->description);
735                 if (rc < 0)
736                         return rc;
737         }
738         if (image_info->display_name) {
739                 rc = xmlTextWriterWriteElement(writer, "DISPLAYNAME", 
740                                                 image_info->display_name);
741                 if (rc < 0)
742                         return rc;
743         }
744         if (image_info->display_description) {
745                 rc = xmlTextWriterWriteElement(writer, "DISPLAYDESCRIPTION", 
746                                                 image_info->display_description);
747                 if (rc < 0)
748                         return rc;
749         }
750
751         if (image_info->flags) {
752                 rc = xmlTextWriterWriteElement(writer, "FLAGS",
753                                 image_info->flags);
754                 if (rc < 0)
755                         return rc;
756         }
757
758         return xmlTextWriterEndElement(writer); /* </IMAGE> */
759 }
760
761
762
763 /* Makes space for another image in the XML information and return a pointer to
764  * it.*/
765 static struct image_info *add_image_info_struct(struct wim_info *wim_info)
766 {
767         struct image_info *images;
768
769         images = CALLOC(wim_info->num_images + 1, sizeof(struct image_info));
770         if (!images)
771                 return NULL;
772         memcpy(images, wim_info->images,
773                wim_info->num_images * sizeof(struct image_info));
774         FREE(wim_info->images);
775         wim_info->images = images;
776         wim_info->num_images++;
777         return &images[wim_info->num_images - 1];
778 }
779
780 static int clone_windows_info(const struct windows_info *old, 
781                               struct windows_info *new)
782 {
783         uint i;
784
785         if (old->product_name && !(new->product_name = STRDUP(old->product_name)))
786                 return WIMLIB_ERR_NOMEM;
787         if (old->edition_id && !(new->edition_id = STRDUP(old->edition_id)))
788                 return WIMLIB_ERR_NOMEM;
789         if (old->installation_type && !(new->installation_type = 
790                                         STRDUP(old->installation_type)))
791                 return WIMLIB_ERR_NOMEM;
792         if (old->hal && !(new->hal = STRDUP(old->hal)))
793                 return WIMLIB_ERR_NOMEM;
794         if (old->product_type && !(new->product_type = STRDUP(old->product_type)))
795                 return WIMLIB_ERR_NOMEM;
796         if (old->product_suite && !(new->product_suite = STRDUP(old->product_suite)))
797                 return WIMLIB_ERR_NOMEM;
798
799         if (old->languages) {
800                 new->languages = CALLOC(old->num_languages, sizeof(char*));
801                 if (!new->languages)
802                         return WIMLIB_ERR_NOMEM;
803                 new->num_languages = old->num_languages;
804                 for (i = 0; i < new->num_languages; i++) {
805                         if (!old->languages[i])
806                                 continue;
807                         new->languages[i] = STRDUP(old->languages[i]);
808                         if (!new->languages[i])
809                                 return WIMLIB_ERR_NOMEM;
810                 }
811         }
812         if (old->default_language && 
813                         !(new->default_language = STRDUP(old->default_language)))
814                 return WIMLIB_ERR_NOMEM;
815         if (old->system_root && !(new->system_root = STRDUP(old->system_root)))
816                 return WIMLIB_ERR_NOMEM;
817         return 0;
818 }
819
820 static int clone_image_info(const struct image_info *old, struct image_info *new)
821 {
822         int ret;
823
824         new->dir_count              = old->dir_count;
825         new->file_count             = old->file_count;
826         new->total_bytes            = old->total_bytes;
827         new->hard_link_bytes        = old->hard_link_bytes;
828         new->creation_time          = old->creation_time;
829         new->last_modification_time = old->last_modification_time;
830
831         if (!(new->name = STRDUP(old->name)))
832                 return WIMLIB_ERR_NOMEM;
833
834         if (old->description)
835                 if (!(new->description = STRDUP(old->description)))
836                         return WIMLIB_ERR_NOMEM;
837
838         if (old->display_name)
839                 if (!(new->display_name = STRDUP(old->display_name)))
840                         return WIMLIB_ERR_NOMEM;
841
842         if (old->display_description)
843                 if (!(new->display_description = STRDUP(old->display_description)))
844                         return WIMLIB_ERR_NOMEM;
845
846         if (old->flags)
847                 if (!(new->flags = STRDUP(old->flags)))
848                         return WIMLIB_ERR_NOMEM;
849
850         if (old->windows_info_exists) {
851                 new->windows_info_exists = true;
852                 return clone_windows_info(&old->windows_info, 
853                                           &new->windows_info);
854         }
855         return 0;
856 }
857
858 /* Copies the XML information for an image between WIM files. 
859  *
860  * @dest_image_name and @dest_image_description are ignored if they are NULL;
861  * otherwise, they are used to override the image name and/or image description
862  * from the XML data in the source WIM file. */
863 int xml_export_image(const struct wim_info *old_wim_info, 
864                      int image, 
865                      struct wim_info **new_wim_info_p, 
866                      const char *dest_image_name, 
867                      const char *dest_image_description)
868 {
869         struct wim_info *new_wim_info;
870         struct image_info *image_info;
871         int ret;
872         char *name;
873         char *desc;
874
875         DEBUG("Copying XML data between WIM files for source image %d.", image);
876
877         wimlib_assert(image >= 1 && image <= old_wim_info->num_images);
878
879
880         if (*new_wim_info_p) {
881                 new_wim_info = *new_wim_info_p;
882         } else {
883                 new_wim_info = CALLOC(1, sizeof(struct wim_info));
884                 if (!new_wim_info)
885                         goto err;
886         }
887
888         image_info = add_image_info_struct(new_wim_info);
889         if (!image_info)
890                 goto err;
891
892         ret = clone_image_info(&old_wim_info->images[image - 1], image_info);
893         if (ret != 0)
894                 goto err;
895
896         image_info->index = new_wim_info->num_images;
897
898         if (dest_image_name) {
899                 FREE(image_info->name);
900                 image_info->name = STRDUP(dest_image_name);
901                 if (!image_info->name)
902                         goto err;
903         }
904         if (dest_image_description) {
905                 FREE(image_info->description);
906                 image_info->description = STRDUP(dest_image_description);
907                 if (!image_info->description)
908                         goto err;
909         }
910         *new_wim_info_p = new_wim_info;
911         return 0;
912 err:
913         ERROR("Out of memory");
914         free_wim_info(new_wim_info);
915         return WIMLIB_ERR_NOMEM;
916 }
917
918 /* Removes an image from the XML information. */
919 void xml_delete_image(struct wim_info **wim_info_p, int image)
920 {
921         struct wim_info *wim_info;
922         int i;
923
924         DEBUG("Deleting image %d from the XML data.", image);
925         
926         wim_info = *wim_info_p;
927
928         wimlib_assert(wim_info);
929         wimlib_assert(image >= 1 && image <= wim_info->num_images);
930
931         destroy_image_info(&wim_info->images[image - 1]);
932
933         for (i = image - 1; i < wim_info->num_images - 1; i++) {
934                 memcpy(&wim_info->images[i], &wim_info->images[i + 1],
935                                         sizeof(struct image_info));
936                 wim_info->images[i].index--;
937         }
938
939         if (--wim_info->num_images == 0) {
940                 free_wim_info(wim_info);
941                 *wim_info_p = NULL;
942         }
943 }
944
945 size_t xml_get_max_image_name_len(const WIMStruct *w)
946 {
947         size_t len = 0;
948         uint i;
949         uint num_images = w->wim_info->num_images;
950         for (i = 0; i < num_images; i++)
951                 len = max(len, strlen(w->wim_info->images[i].name));
952         return len;
953 }
954
955 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
956 void xml_set_memory_allocator(void *(*malloc_func)(size_t),
957                                    void (*free_func)(void *),
958                                    void *(*realloc_func)(void *, size_t))
959 {
960         xmlMemSetup(free_func, malloc_func, realloc_func, STRDUP);
961 }
962 #endif
963
964 void xml_update_image_info(WIMStruct *w, int image)
965 {
966         struct image_info *image_info;
967         struct dentry *root; 
968
969         DEBUG("Updating the image info for image %d", image);
970
971         image_info = &w->wim_info->images[image - 1];
972         root = w->image_metadata[image - 1].root_dentry;
973
974         calculate_dir_tree_statistics(root, w->lookup_table, 
975                                       &image_info->dir_count,
976                                       &image_info->file_count, 
977                                       &image_info->total_bytes,
978                                       &image_info->hard_link_bytes);
979
980         image_info->last_modification_time = get_timestamp();
981 }
982
983 /* Adds an image to the XML information. */
984 int xml_add_image(WIMStruct *w, struct dentry *root_dentry, const char *name, 
985                   const char *description, const char *flags_element)
986 {
987         struct wim_info *wim_info;
988         struct image_info *image_info;
989
990         wimlib_assert(name);
991
992         DEBUG("Adding image: name = %s, description = %s, flags_element = %s",
993               name, description, flags_element);
994
995         /* If this is the first image, allocate the struct wim_info.  Otherwise
996          * use the existing struct wim_info. */
997         if (w->wim_info) {
998                 wim_info = w->wim_info;
999         } else {
1000                 DEBUG("Allocing struct wim_info with 1 image");
1001                 wim_info = CALLOC(1, sizeof(struct wim_info));
1002                 if (!wim_info) {
1003                         ERROR("Could not allocate WIM information struct--- "
1004                               "out of memory");
1005                         return WIMLIB_ERR_NOMEM;
1006                 }
1007         }
1008
1009         image_info = add_image_info_struct(wim_info);
1010         if (!image_info)
1011                 goto out_free_wim_info;
1012
1013         if (!(image_info->name = STRDUP(name)))
1014                 goto out_destroy_image_info;
1015
1016         if (description && !(image_info->description = STRDUP(description)))
1017                 goto out_destroy_image_info;
1018         if (flags_element && !(image_info->flags = STRDUP(flags_element)))
1019                 goto out_destroy_image_info;
1020                 
1021         w->wim_info = wim_info;
1022         image_info->index = wim_info->num_images;
1023         image_info->creation_time = get_timestamp();
1024         xml_update_image_info(w, image_info->index);
1025         return 0;
1026
1027 out_destroy_image_info:
1028         destroy_image_info(image_info);
1029 out_free_wim_info:
1030         if (w->wim_info)
1031                 wim_info->num_images--;
1032         else
1033                 FREE(wim_info);
1034         ERROR("Out of memory");
1035         return WIMLIB_ERR_NOMEM;
1036 }
1037
1038 /* Prints information about the specified image from struct wim_info structure. 
1039  * @image may be WIM_ALL_IMAGES. */
1040 void print_image_info(const struct wim_info *wim_info, int image)
1041 {
1042         uint i;
1043         const struct image_info *image_info;
1044         const char *desc;
1045         time_t ctime;
1046         time_t mtime;
1047
1048
1049         if (image == WIM_ALL_IMAGES) {
1050                 for (i = 1; i <= wim_info->num_images; i++)
1051                         print_image_info(wim_info, i);
1052         } else {
1053                 image_info = &wim_info->images[image - 1];
1054
1055                 printf("Index:                  %"PRIu64"\n", 
1056                         image_info->index);
1057                 printf("Name:                   %s\n", 
1058                         image_info->name);
1059
1060                 /* Always print the Description: part even if there is no
1061                  * description. */
1062                 if (image_info->description)
1063                         desc = image_info->description;
1064                 else
1065                         desc = "";
1066                 printf("Description:            %s\n", desc);
1067
1068                 if (image_info->display_name)
1069                         printf("Display Name:           %s\n", 
1070                                 image_info->display_name);
1071
1072                 if (image_info->display_description)
1073                         printf("Display Description:    %s\n", 
1074                                 image_info->display_description);
1075
1076                 printf("Directory Count:        %"PRIu64"\n", 
1077                                 image_info->dir_count);
1078                 printf("File Count:             %"PRIu64"\n", 
1079                                 image_info->file_count);
1080                 printf("Total Bytes:            %"PRIu64"\n", 
1081                                 image_info->total_bytes);
1082                 printf("Hard Link Bytes:        %"PRIu64"\n", 
1083                                 image_info->hard_link_bytes);
1084
1085                 ctime = ms_timestamp_to_unix(image_info->creation_time);
1086                 mtime = ms_timestamp_to_unix(image_info->last_modification_time);
1087
1088                 printf("Creation Time:          %s", asctime(localtime(&ctime)));
1089                 printf("Last Modification Time: %s", asctime(localtime(&mtime)));
1090                 if (image_info->windows_info_exists)
1091                         print_windows_info(&image_info->windows_info);
1092                 if (image_info->flags)
1093                         printf("Flags:                  %s\n", image_info->flags);
1094                 putchar('\n');
1095         }
1096 }
1097
1098 /* 
1099  * Reads the XML data from a WIM file.
1100  */
1101 int read_xml_data(FILE *fp, const struct resource_entry *res, u8 **xml_data_ret,
1102                   struct wim_info **info_ret)
1103 {
1104         u8 *xml_data;
1105         xmlDoc *doc;
1106         xmlNode *root;
1107         int ret;
1108
1109         DEBUG("XML data is %"PRIu64" bytes at offset %"PRIu64"", 
1110               (u64)res->size, res->offset);
1111
1112         if (resource_is_compressed(res)) {
1113                 ERROR("XML data is supposed to be uncompressed");
1114                 ret = WIMLIB_ERR_XML;
1115                 goto err0;
1116         }
1117         if (res->size < 2) {
1118                 ERROR("XML data must be at least 2 bytes");
1119                 ret = WIMLIB_ERR_XML;
1120                 goto err0;
1121         }
1122
1123         xml_data = MALLOC(res->size + 2);
1124         if (!xml_data) {
1125                 ret = WIMLIB_ERR_NOMEM;
1126                 goto err0;
1127         }
1128         ret = read_full_resource(fp, res->size, res->size, res->offset, 
1129                                  WIM_COMPRESSION_TYPE_NONE, xml_data);
1130         if (ret != 0)
1131                 goto err1;
1132
1133         xml_data[res->size] = 0;
1134         xml_data[res->size + 1] = 0;
1135
1136         DEBUG("Parsing XML using libxml2 to create XML tree.");
1137
1138         doc = xmlReadMemory(xml_data, res->size, "noname.xml", "UTF-16", 0);
1139
1140
1141         if (!doc) {
1142                 ERROR("Failed to parse XML data");
1143                 ret = WIMLIB_ERR_XML;
1144                 goto err1;
1145         }
1146
1147         DEBUG("Constructing WIM information structure from XML tree.");
1148
1149         root = xmlDocGetRootElement(doc);
1150         if (!root) {
1151                 ERROR("Empty XML document");
1152                 ret = WIMLIB_ERR_XML;
1153                 goto err2;
1154         }
1155
1156         if (!node_is_element(root) || !node_name_is(root, "WIM")) {
1157                 ERROR("Expected <WIM> for the root XML element (found <%s>)",
1158                       root->name);
1159                 ret = WIMLIB_ERR_XML;
1160                 goto err2;
1161         }
1162
1163         ret = xml_read_wim_info(root, info_ret);
1164         if (ret != 0)
1165                 goto err2;
1166
1167         DEBUG("Freeing XML tree.");
1168
1169         xmlFreeDoc(doc);
1170         xmlCleanupParser();
1171         *xml_data_ret = xml_data;
1172         return 0;
1173 err2:
1174         xmlFreeDoc(doc);
1175 err1:
1176         FREE(xml_data);
1177 err0:
1178         xmlCleanupParser();
1179         return ret;
1180 }
1181
1182 #define CHECK_RET  ({   if (ret < 0)  { \
1183                                 ERROR("Error writing XML data"); \
1184                                 ret = WIMLIB_ERR_WRITE; \
1185                                 goto err2; \
1186                         } })
1187
1188 /* 
1189  * Writes XML data to a WIM file.
1190  *
1191  * If @total_bytes is non-zero, it specifies what to write to the TOTALBYTES
1192  * element in the XML data.  If zero, TOTALBYTES is given the default value of
1193  * the offset of the XML data.
1194  */
1195 int write_xml_data(const struct wim_info *wim_info, int image, FILE *out, 
1196                    u64 total_bytes)
1197 {
1198         xmlBuffer     *buf;
1199         xmlTextWriter *writer;
1200         char          *utf16_str;
1201         int ret;
1202         int num_images;
1203         int i;
1204         const xmlChar *content;
1205         size_t len;
1206         size_t utf16_len;
1207         size_t bytes_written;
1208
1209         wimlib_assert(image == WIM_ALL_IMAGES || 
1210                         (wim_info != NULL && image >= 1 && 
1211                          image <= wim_info->num_images));
1212
1213         /* The contents of the <TOTALBYTES> element in the XML data, under the
1214          * <WIM> element not the <IMAGE> element, is (for non-spit WIMs) the
1215          * size of the WIM file excluding the XML data and integrity table,
1216          * which is the current offset, since the XML data goes at the end of
1217          * the WIM file before the integrity table. */
1218         if (total_bytes == 0) {
1219                 total_bytes = ftello(out);
1220                 if (total_bytes == (u64)-1)
1221                         return WIMLIB_ERR_WRITE;
1222         }
1223
1224         DEBUG("Creating XML buffer and text writer.");
1225         buf = xmlBufferCreate();
1226         if (!buf) {
1227                 ERROR("Failed to allocate XML buffer");
1228                 ret = WIMLIB_ERR_NOMEM;
1229                 goto err0;
1230         }
1231         writer = xmlNewTextWriterMemory(buf, 0);
1232         if (!writer) {
1233                 ERROR("Failed to allocate XML writer");
1234                 ret = WIMLIB_ERR_NOMEM;
1235                 goto err1;
1236         }
1237
1238         /* XXX */
1239         /* M$'s WIM files do not have XML declarations, so do not write one.
1240          * I'm not sure how we can force the document to be written in UTF-16
1241          * without calling xmlTextWriterStartDocument(), though, so currently it
1242          * is composed in a buffer UTF-8, then converted to UTF-16. */
1243 #if 0
1244         ret = xmlTextWriterStartDocument(writer, NULL, "UTF-16", NULL);
1245         CHECK_RET;
1246 #endif
1247
1248         DEBUG("Writing <WIM> element");
1249         ret = xmlTextWriterStartElement(writer, "WIM");
1250         CHECK_RET;
1251
1252         ret = xmlTextWriterWriteFormatElement(writer, "TOTALBYTES", "%"PRIu64,
1253                                               total_bytes);
1254         CHECK_RET;
1255
1256         if (wim_info)
1257                 num_images = wim_info->num_images;
1258         else
1259                 num_images = 0;
1260         DEBUG("Writing %u <IMAGE> elements", num_images);
1261
1262         for (i = 1; i <= num_images; i++) {
1263                 if (image != WIM_ALL_IMAGES && i != image)
1264                         continue;
1265                 DEBUG("Writing <IMAGE> element for image %d", i);
1266                 ret = xml_write_image_info(writer, &wim_info->images[i - 1]);
1267                 CHECK_RET;
1268         }
1269
1270         ret = xmlTextWriterEndElement(writer);
1271         CHECK_RET;
1272
1273         ret = xmlTextWriterEndDocument(writer);
1274         CHECK_RET;
1275
1276         DEBUG("Done composing XML document. Now converting to UTF-16 and "
1277               "writing it to the output file.");
1278
1279         content = xmlBufferContent(buf);
1280         len = xmlBufferLength(buf);
1281
1282         utf16_str = utf8_to_utf16(content, len, &utf16_len);
1283         if (!utf16_str) {
1284                 ret = WIMLIB_ERR_NOMEM;
1285                 goto err2;
1286         }
1287
1288         if ((putc(0xff, out)) == EOF || (putc(0xfe, out) == EOF) || 
1289                 ((bytes_written = fwrite(utf16_str, 1, utf16_len, out))
1290                                 != utf16_len)) {
1291                 ERROR_WITH_ERRNO("Error writing XML data");
1292                 ret = WIMLIB_ERR_WRITE;
1293                 goto err3;
1294         }
1295
1296         DEBUG("Cleaning up.");
1297
1298         ret = 0;
1299 err3:
1300         FREE(utf16_str);
1301 err2:
1302         xmlFreeTextWriter(writer);
1303 err1:
1304         xmlBufferFree(buf);
1305 err0:
1306         return ret;
1307 }
1308
1309 /* Returns the name of the specified image. */
1310 WIMLIBAPI const char *wimlib_get_image_name(const WIMStruct *w, int image)
1311 {
1312         DEBUG("Getting the name of image %d", image);
1313         if (image < 1 || image > w->hdr.image_count)
1314                 return NULL;
1315
1316         return w->wim_info->images[image - 1].name;
1317 }
1318
1319 /* Returns the description of the specified image. */
1320 WIMLIBAPI const char *wimlib_get_image_description(const WIMStruct *w, 
1321                                                    int image)
1322 {
1323         DEBUG("Getting the description of image %d", image);
1324         if (image < 1 || image > w->hdr.image_count)
1325                 return NULL;
1326
1327         return w->wim_info->images[image - 1].description;
1328 }
1329
1330 /* Determines if an image name is already used by some image in the WIM. */
1331 WIMLIBAPI bool wimlib_image_name_in_use(const WIMStruct *w, const char *name)
1332 {
1333         int i;
1334
1335         DEBUG("Checking to see if the image name `%s' is already in use", name);
1336         if (!name || !w->wim_info)
1337                 return false;
1338         for (i = 1; i <= w->wim_info->num_images; i++)
1339                 if (strcmp(w->wim_info->images[i - 1].name, name) == 0)
1340                         return true;
1341
1342         return false;
1343 }
1344
1345 WIMLIBAPI int wimlib_extract_xml_data(WIMStruct *w, FILE *fp)
1346 {
1347         DEBUG("Extracting the XML data.");
1348         if (fwrite(w->xml_data, 1, w->hdr.xml_res_entry.size, fp) != 
1349                         w->hdr.xml_res_entry.size) {
1350                 ERROR_WITH_ERRNO("Failed to extract XML data");
1351                 return WIMLIB_ERR_WRITE;
1352         }
1353         return 0;
1354 }
1355
1356 /* Sets the name of an image in the WIM. */
1357 WIMLIBAPI int wimlib_set_image_name(WIMStruct *w, int image, const char *name)
1358 {
1359         char *p;
1360         int i;
1361
1362         DEBUG("Setting the name of image %d to %s", image, name);
1363
1364         if (!name || !*name) {
1365                 ERROR("Must specify a non-empty string for the image name");
1366                 return WIMLIB_ERR_INVALID_PARAM;
1367         }
1368         if (image < 1 || image > w->hdr.image_count) {
1369                 ERROR("%d is not a valid image", image);
1370                 return WIMLIB_ERR_INVALID_IMAGE;
1371         }
1372
1373         for (i = 1; i <= w->hdr.image_count; i++) {
1374                 if (i == image)
1375                         continue;
1376                 if (strcmp(w->wim_info->images[i - 1].name, name) == 0) {
1377                         ERROR("The name `%s' is already used for image %d",
1378                               name, i);
1379                         return WIMLIB_ERR_IMAGE_NAME_COLLISION;
1380                 }
1381         }
1382
1383         p = STRDUP(name);
1384         if (!p) {
1385                 ERROR("Out of memory");
1386                 return WIMLIB_ERR_NOMEM;
1387         }
1388         FREE(w->wim_info->images[image - 1].name);
1389         w->wim_info->images[image - 1].name = p;
1390         return 0;
1391 }
1392
1393 /* Sets the description of an image in the WIM. */
1394 WIMLIBAPI int wimlib_set_image_descripton(WIMStruct *w, int image, 
1395                                           const char *description)
1396 {
1397         char *p;
1398
1399         DEBUG("Setting the description of image %d to %s", image, description);
1400
1401         if (image < 1 || image > w->hdr.image_count) {
1402                 ERROR("%d is not a valid image", image);
1403                 return WIMLIB_ERR_INVALID_IMAGE;
1404         }
1405         if (description) {
1406                 p = STRDUP(description);
1407                 if (!p) {
1408                         ERROR("Out of memory");
1409                         return WIMLIB_ERR_NOMEM;
1410                 }
1411         } else {
1412                 p = NULL;
1413         }
1414         FREE(w->wim_info->images[image - 1].description);
1415         w->wim_info->images[image - 1].description = p;
1416         return 0;
1417 }