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