]> wimlib.net Git - wimlib/blob - src/xml.c
Warning, not error, on missing image name
[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                 WARNING("Image with index %"PRIu64" has no name\n", 
399                                         image_info->index);
400                 image_info->name = MALLOC(1);
401                 if (!image_info->name) {
402                         ERROR("Out of memory!\n");
403                         return WIMLIB_ERR_NOMEM;
404                 }
405                 image_info->name[0] = '\0';
406                 return 0;
407         }
408         
409         return 0;
410 }
411
412 /* Reads the information from a <WIM> element, which should be the root element
413  * of the XML tree. */
414 static int xml_read_wim_info(const xmlNode *wim_node, struct wim_info **wim_info_ret)
415 {
416         struct wim_info *wim_info;
417         xmlNode *child;
418         int ret;
419         uint num_images;
420         struct image_info *cur_image_info;
421
422         wim_info = CALLOC(1, sizeof(struct wim_info));
423         if (!wim_info) {
424                 ERROR("Out of memory!\n");
425                 return WIMLIB_ERR_NOMEM;
426         }
427
428         /* Count how many images there are. */
429         num_images = 0;
430         for_node_child(wim_node, child)
431                 if (node_is_element(child) && node_name_is(child, "IMAGE"))
432                         num_images++;
433
434         if (num_images == 0)
435                 goto done;
436
437         /* Allocate the array of struct image_infos and fill them in. */
438         wim_info->images = CALLOC(num_images, sizeof(wim_info->images[0]));
439         if (!wim_info->images) {
440                 ret = WIMLIB_ERR_NOMEM;
441                 ERROR("Out of memory!\n");
442                 goto err;
443         }
444         wim_info->num_images = num_images;
445         cur_image_info = wim_info->images;
446         for_node_child(wim_node, child) {
447                 if (!node_is_element(child))
448                         continue;
449                 if (node_name_is(child, "IMAGE")) {
450                         DEBUG("Found <IMAGE> tag\n");
451                         ret = xml_read_image_info(child, cur_image_info++);
452                         if (ret != 0)
453                                 goto err;
454                 } else if (node_name_is(child, "TOTALBYTES")) {
455                         wim_info->total_bytes = node_get_u64(child);
456                 }
457         }
458
459         /* Sort the array of struct image_infos by image index. */
460         qsort(wim_info->images, wim_info->num_images, 
461               sizeof(struct image_info), sort_by_index);
462 done:
463         *wim_info_ret = wim_info;
464         return 0;
465 err:
466         free_wim_info(wim_info);
467         return ret;
468 }
469
470 /* Prints the information contained in a struct windows_info structure. */
471 static void print_windows_info(const struct windows_info *windows_info)
472 {
473         uint i;
474         const struct windows_version *windows_version;
475
476         printf("Architecture:           %s\n", get_arch(windows_info->arch));
477         printf("Product Name:           %s\n", windows_info->product_name);
478         printf("Edition ID:             %s\n", windows_info->edition_id);
479         printf("Installation Type:      %s\n", windows_info->installation_type);
480         if (windows_info->hal)
481                 printf("HAL:                    %s\n", windows_info->hal);
482         printf("Product Type:           %s\n", windows_info->product_type);
483         if (windows_info->product_suite)
484                 printf("Product Suite:          %s\n", windows_info->product_suite);
485         printf("Languages:              ");
486         for (i = 0; i < windows_info->num_languages; i++) {
487                 fputs(windows_info->languages[i], stdout);
488                 putchar(' ');
489         }
490         putchar('\n');
491         printf("Default Language:       %s\n", windows_info->default_language);
492         printf("System Root:            %s\n", windows_info->system_root);
493         if (windows_info->windows_version_exists) {
494                 windows_version = &windows_info->windows_version;
495                 printf("Major Version:          %"PRIu64"\n", 
496                                 windows_version->major);
497                 printf("Minor Version:          %"PRIu64"\n", 
498                                 windows_version->minor);
499                 printf("Build:                  %"PRIu64"\n", 
500                                 windows_version->build);
501                 printf("Service Pack Build:     %"PRIu64"\n", 
502                                 windows_version->sp_build);
503                 printf("Service Pack Level:     %"PRIu64"\n", 
504                                 windows_version->sp_level);
505         }
506 }
507
508
509 /* Writes the information contained in a struct windows_version structure to the XML
510  * document being constructed in memory.  This is the <VERSION> element inside
511  * the <WINDOWS> element. */
512 static int xml_write_windows_version(xmlTextWriter *writer, 
513                                         const struct windows_version *version)
514 {
515         int rc;
516         rc = xmlTextWriterStartElement(writer, "VERSION");
517         if (rc < 0)
518                 return rc;
519
520         rc = xmlTextWriterWriteFormatElement(writer, "MAJOR", "%"PRIu64, 
521                                                                 version->major);
522         if (rc < 0)
523                 return rc;
524
525         rc = xmlTextWriterWriteFormatElement(writer, "MINOR", "%"PRIu64, 
526                                                                 version->minor);
527         if (rc < 0)
528                 return rc;
529
530         rc = xmlTextWriterWriteFormatElement(writer, "BUILD", "%"PRIu64, 
531                                                                 version->build);
532         if (rc < 0)
533                 return rc;
534
535         rc = xmlTextWriterWriteFormatElement(writer, "SPBUILD", "%"PRIu64, 
536                                                                 version->sp_build);
537         if (rc < 0)
538                 return rc;
539
540         rc = xmlTextWriterWriteFormatElement(writer, "SPLEVEL", "%"PRIu64, 
541                                                                 version->sp_level);
542         if (rc < 0)
543                 return rc;
544
545         return xmlTextWriterEndElement(writer); /* </VERSION> */
546 }
547
548 /* Writes the information contained in a struct windows_info structure to the XML
549  * document being constructed in memory. This is the <WINDOWS> element. */
550 static int xml_write_windows_info(xmlTextWriter *writer, 
551                                         const struct windows_info *windows_info)
552 {
553         int rc;
554         rc = xmlTextWriterStartElement(writer, "WINDOWS");
555         if (rc < 0)
556                 return rc;
557
558
559         rc = xmlTextWriterWriteFormatElement(writer, "ARCH", "%"PRIu64, 
560                                                         windows_info->arch);
561         if (rc < 0)
562                 return rc;
563         
564         if (windows_info->product_name) {
565                 rc = xmlTextWriterWriteElement(writer, "PRODUCTNAME", 
566                                                         windows_info->product_name);
567                 if (rc < 0)
568                         return rc;
569         }
570
571         if (windows_info->edition_id) {
572                 rc = xmlTextWriterWriteElement(writer, "EDITIONID", 
573                                                         windows_info->edition_id);
574                 if (rc < 0)
575                         return rc;
576         }
577
578         if (windows_info->installation_type) {
579                 rc = xmlTextWriterWriteElement(writer, "INSTALLATIONTYPE", 
580                                                         windows_info->installation_type);
581                 if (rc < 0)
582                         return rc;
583         }
584
585         if (windows_info->hal) {
586                 rc = xmlTextWriterWriteElement(writer, "HAL", 
587                                                         windows_info->hal);
588                 if (rc < 0)
589                         return rc;
590         }
591
592         if (windows_info->system_root) {
593                 rc = xmlTextWriterWriteElement(writer, "SYSTEMROOT", 
594                                                 windows_info->system_root);
595                         if (rc < 0)
596                                 return rc;
597         }
598
599         if (windows_info->product_type) {
600                 rc = xmlTextWriterWriteElement(writer, "PRODUCTTYPE", 
601                                                 windows_info->product_type);
602                 if (rc < 0)
603                         return rc;
604         }
605
606         if (windows_info->product_suite) {
607                 rc = xmlTextWriterWriteElement(writer, "PRODUCTSUITE", 
608                                                 windows_info->product_suite);
609                         if (rc < 0)
610                                 return rc;
611         }
612
613         if (windows_info->num_languages) {
614                 rc = xmlTextWriterStartElement(writer, "LANGUAGES");
615                 if (rc < 0)
616                         return rc;
617
618                 for (int i = 0; i < windows_info->num_languages; i++) {
619                         rc = xmlTextWriterWriteElement(writer, "LANGUAGE", 
620                                                         windows_info->languages[i]);
621                         if (rc < 0)
622                                 return rc;
623                 }
624                 rc = xmlTextWriterWriteElement(writer, "DEFAULT", 
625                                                 windows_info->default_language);
626                 if (rc < 0)
627                         return rc;
628
629                 rc = xmlTextWriterEndElement(writer); /* </LANGUAGES> */
630                 if (rc < 0)
631                         return rc;
632         }
633
634         if (windows_info->windows_version_exists) {
635                 rc = xml_write_windows_version(writer, &windows_info->windows_version);
636                 if (rc < 0)
637                         return rc;
638         }
639
640         return xmlTextWriterEndElement(writer); /* </WINDOWS> */
641 }
642
643 /* Writes a time element to the XML document being constructed in memory. */
644 static int xml_write_time(xmlTextWriter *writer, const char *element_name, 
645                                                                 u64 time) 
646 {
647         int rc;
648         rc = xmlTextWriterStartElement(writer, element_name);
649         if (rc < 0)
650                 return rc;
651
652         rc = xmlTextWriterWriteFormatElement(writer, "HIGHPART", 
653                                         "0x%"PRIX32, (u32)(time >> 32));
654         if (rc < 0)
655                 return rc;
656
657         rc = xmlTextWriterWriteFormatElement(writer, "LOWPART",
658                                                 "0x%"PRIX32, (u32)time);
659         if (rc < 0)
660                 return rc;
661
662         rc = xmlTextWriterEndElement(writer); /* </@element_name> */
663         if (rc < 0)
664                 return rc;
665         return 0;
666 }
667
668
669 /* Writes an <IMAGE> element to the XML document. */
670 static int xml_write_image_info(xmlTextWriter *writer, 
671                                 const struct image_info *image_info)
672 {
673         int rc;
674         rc = xmlTextWriterStartElement(writer, "IMAGE");
675         if (rc < 0)
676                 return rc;
677
678         rc = xmlTextWriterWriteFormatAttribute(writer, "INDEX", "%"PRIu64, 
679                                                 image_info->index);
680         if (rc < 0)
681                 return rc;
682
683         rc = xmlTextWriterWriteFormatElement(writer, "DIRCOUNT", "%"PRIu64, 
684                                                 image_info->dir_count);
685         if (rc < 0)
686                 return rc;
687
688         rc = xmlTextWriterWriteFormatElement(writer, "FILECOUNT", "%"PRIu64, 
689                                                 image_info->file_count);
690         if (rc < 0)
691                 return rc;
692
693         rc = xmlTextWriterWriteFormatElement(writer, "TOTALBYTES", "%"PRIu64, 
694                                                 image_info->total_bytes);
695         if (rc < 0)
696                 return rc;
697
698         rc = xmlTextWriterWriteFormatElement(writer, "HARDLINKBYTES", "%"PRIu64, 
699                                                 image_info->hard_link_bytes);
700         if (rc < 0)
701                 return rc;
702
703         rc = xml_write_time(writer, "CREATIONTIME", 
704                                                 image_info->creation_time);
705         if (rc < 0)
706                 return rc;
707
708         rc = xml_write_time(writer, "LASTMODIFICATIONTIME", 
709                                                 image_info->last_modification_time);
710         if (rc < 0)
711                 return rc;
712
713         if (image_info->windows_info_exists) {
714                 rc = xml_write_windows_info(writer, &image_info->windows_info);
715                 if (rc < 0)
716                         return rc;
717         } else {
718                 DEBUG("<WINDOWS> tag does not exist.\n");
719         }
720
721         if (image_info->name) {
722                 rc = xmlTextWriterWriteElement(writer, "NAME", image_info->name);
723                 if (rc < 0)
724                         return rc;
725         }
726         if (image_info->description) {
727                 rc = xmlTextWriterWriteElement(writer, "DESCRIPTION", 
728                                                         image_info->description);
729                 if (rc < 0)
730                         return rc;
731         }
732         if (image_info->display_name) {
733                 rc = xmlTextWriterWriteElement(writer, "DISPLAYNAME", 
734                                                 image_info->display_name);
735                 if (rc < 0)
736                         return rc;
737         }
738         if (image_info->display_description) {
739                 rc = xmlTextWriterWriteElement(writer, "DISPLAYDESCRIPTION", 
740                                                 image_info->display_description);
741                 if (rc < 0)
742                         return rc;
743         }
744
745         if (image_info->flags) {
746                 rc = xmlTextWriterWriteElement(writer, "FLAGS",
747                                 image_info->flags);
748                 if (rc < 0)
749                         return rc;
750         }
751
752         return xmlTextWriterEndElement(writer); /* </IMAGE> */
753 }
754
755
756
757 /* Makes space for another image in the XML information and return a pointer to
758  * it.*/
759 static struct image_info *add_image_info_struct(struct wim_info *wim_info)
760 {
761         struct image_info *images;
762
763         images = CALLOC(wim_info->num_images + 1, sizeof(struct image_info));
764         if (!images)
765                 return NULL;
766         memcpy(images, wim_info->images, 
767                         wim_info->num_images * sizeof(struct image_info));
768         FREE(wim_info->images);
769         wim_info->images = images;
770         wim_info->num_images++;
771         return &images[wim_info->num_images - 1];
772 }
773
774 static int clone_windows_info(const struct windows_info *old, 
775                               struct windows_info *new)
776 {
777         uint i;
778
779         if (old->product_name && !(new->product_name = STRDUP(old->product_name)))
780                 return WIMLIB_ERR_NOMEM;
781         if (old->edition_id && !(new->edition_id = STRDUP(old->edition_id)))
782                 return WIMLIB_ERR_NOMEM;
783         if (old->installation_type && !(new->installation_type = 
784                                         STRDUP(old->installation_type)))
785                 return WIMLIB_ERR_NOMEM;
786         if (old->hal && !(new->hal = STRDUP(old->hal)))
787                 return WIMLIB_ERR_NOMEM;
788         if (old->product_type && !(new->product_type = STRDUP(old->product_type)))
789                 return WIMLIB_ERR_NOMEM;
790         if (old->product_suite && !(new->product_suite = STRDUP(old->product_suite)))
791                 return WIMLIB_ERR_NOMEM;
792
793         if (old->languages) {
794                 new->languages = CALLOC(old->num_languages, sizeof(char*));
795                 if (!new->languages)
796                         return WIMLIB_ERR_NOMEM;
797                 new->num_languages = old->num_languages;
798                 for (i = 0; i < new->num_languages; i++) {
799                         if (!old->languages[i])
800                                 continue;
801                         new->languages[i] = STRDUP(old->languages[i]);
802                         if (!new->languages[i])
803                                 return WIMLIB_ERR_NOMEM;
804                 }
805         }
806         if (old->default_language && 
807                         !(new->default_language = STRDUP(old->default_language)))
808                 return WIMLIB_ERR_NOMEM;
809         if (old->system_root && !(new->system_root = STRDUP(old->system_root)))
810                 return WIMLIB_ERR_NOMEM;
811         return 0;
812 }
813
814 static int clone_image_info(const struct image_info *old, struct image_info *new)
815 {
816         int ret;
817
818         new->dir_count              = old->dir_count;
819         new->file_count             = old->file_count;
820         new->total_bytes            = old->total_bytes;
821         new->hard_link_bytes        = old->hard_link_bytes;
822         new->creation_time          = old->creation_time;
823         new->last_modification_time = old->last_modification_time;
824
825         if (!(new->name = STRDUP(old->name)))
826                 return WIMLIB_ERR_NOMEM;
827
828         if (old->description)
829                 if (!(new->description = STRDUP(old->description)))
830                         return WIMLIB_ERR_NOMEM;
831
832         if (old->display_name)
833                 if (!(new->display_name = STRDUP(old->display_name)))
834                         return WIMLIB_ERR_NOMEM;
835
836         if (old->display_description)
837                 if (!(new->display_description = STRDUP(old->display_description)))
838                         return WIMLIB_ERR_NOMEM;
839
840         if (old->flags)
841                 if (!(new->flags = STRDUP(old->flags)))
842                         return WIMLIB_ERR_NOMEM;
843
844         if (old->windows_info_exists) {
845                 new->windows_info_exists = true;
846                 return clone_windows_info(&old->windows_info, 
847                                           &new->windows_info);
848         }
849         return 0;
850 }
851
852 /* Copies the XML information for an image between WIM files. 
853  *
854  * @dest_image_name and @dest_image_description are ignored if they are NULL;
855  * otherwise, they are used to override the image name and/or image description
856  * from the XML data in the source WIM file. */
857 int xml_export_image(const struct wim_info *old_wim_info, 
858                      int image, 
859                      struct wim_info **new_wim_info_p, 
860                      const char *dest_image_name, 
861                      const char *dest_image_description)
862 {
863         struct wim_info *new_wim_info;
864         struct image_info *image_info;
865         int ret;
866         char *name;
867         char *desc;
868
869         DEBUG("Copying XML data between WIM files for source image %d\n",
870                         image);
871
872         wimlib_assert(image >= 1 && image <= old_wim_info->num_images);
873
874
875         if (*new_wim_info_p) {
876                 new_wim_info = *new_wim_info_p;
877         } else {
878                 new_wim_info = CALLOC(1, sizeof(struct wim_info));
879                 if (!new_wim_info)
880                         goto err;
881         }
882
883         image_info = add_image_info_struct(new_wim_info);
884         if (!image_info)
885                 goto err;
886
887         ret = clone_image_info(&old_wim_info->images[image - 1], image_info);
888         if (ret != 0)
889                 goto err;
890
891         image_info->index = new_wim_info->num_images;
892
893         if (dest_image_name) {
894                 FREE(image_info->name);
895                 image_info->name = STRDUP(dest_image_name);
896                 if (!image_info->name)
897                         goto err;
898         }
899         if (dest_image_description) {
900                 FREE(image_info->description);
901                 image_info->description = STRDUP(dest_image_description);
902                 if (!image_info->description)
903                         goto err;
904         }
905         *new_wim_info_p = new_wim_info;
906         return 0;
907 err:
908         ERROR("Out of memory!\n");
909         free_wim_info(new_wim_info);
910         return WIMLIB_ERR_NOMEM;
911 }
912
913 /* Removes an image from the XML information. */
914 void xml_delete_image(struct wim_info **wim_info_p, int image)
915 {
916         struct wim_info *wim_info;
917         int i;
918
919         DEBUG("Deleting image %d from the XML data\n", image);
920         
921         wim_info = *wim_info_p;
922
923         wimlib_assert(wim_info);
924         wimlib_assert(image >= 1 && image <= wim_info->num_images);
925
926         destroy_image_info(&wim_info->images[image - 1]);
927
928         for (i = image - 1; i < wim_info->num_images - 1; i++) {
929                 memcpy(&wim_info->images[i], &wim_info->images[i + 1],
930                                         sizeof(struct image_info));
931                 wim_info->images[i].index--;
932         }
933
934         if (--wim_info->num_images == 0) {
935                 free_wim_info(wim_info);
936                 *wim_info_p = NULL;
937         }
938 }
939
940 size_t xml_get_max_image_name_len(const WIMStruct *w)
941 {
942         size_t len = 0;
943         uint i;
944         uint num_images = w->wim_info->num_images;
945         for (i = 0; i < num_images; i++)
946                 len = max(len, strlen(w->wim_info->images[i].name));
947         return len;
948 }
949
950 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
951 void xml_set_memory_allocator(void *(*malloc_func)(size_t),
952                                    void (*free_func)(void *),
953                                    void *(*realloc_func)(void *, size_t))
954 {
955         xmlMemSetup(free_func, malloc_func, realloc_func, STRDUP);
956 }
957 #endif
958
959 void xml_update_image_info(WIMStruct *w, int image)
960 {
961         struct image_info *image_info;
962         struct dentry *root; 
963
964         DEBUG("Updating the image info for image %d\n", image);
965
966         image_info = &w->wim_info->images[image - 1];
967         root = w->image_metadata[image - 1].root_dentry;
968
969         calculate_dir_tree_statistics(root, w->lookup_table, 
970                                       &image_info->dir_count,
971                                       &image_info->file_count, 
972                                       &image_info->total_bytes,
973                                       &image_info->hard_link_bytes);
974
975         image_info->last_modification_time = get_timestamp();
976 }
977
978 /* Adds an image to the XML information. */
979 int xml_add_image(WIMStruct *w, struct dentry *root_dentry, const char *name, 
980                   const char *description, const char *flags_element)
981 {
982         struct wim_info *wim_info;
983         struct image_info *image_info;
984
985         wimlib_assert(name);
986
987         DEBUG("Adding image: name = %s, description = %s, flags_element = %s\n",
988                         name, description, flags_element);
989
990         /* If this is the first image, allocate the struct wim_info.  Otherwise
991          * use the existing struct wim_info. */
992         if (w->wim_info) {
993                 wim_info = w->wim_info;
994         } else {
995                 DEBUG("Allocing struct wim_info with 1 image\n");
996                 wim_info = CALLOC(1, sizeof(struct wim_info));
997                 if (!wim_info) {
998                         ERROR("Could not allocate WIM information struct--- "
999                                         "out of memory!\n");
1000                         return WIMLIB_ERR_NOMEM;
1001                 }
1002         }
1003
1004         image_info = add_image_info_struct(wim_info);
1005         if (!image_info)
1006                 goto err_nomem1;
1007
1008         if (!(image_info->name = STRDUP(name)))
1009                 goto err_nomem2;
1010
1011         if (description && !(image_info->description = STRDUP(description)))
1012                 goto err_nomem2;
1013         if (flags_element && !(image_info->flags = STRDUP(flags_element)))
1014                 goto err_nomem2;
1015                 
1016         w->wim_info = wim_info;
1017         image_info->index = wim_info->num_images;
1018         image_info->creation_time = get_timestamp();
1019         xml_update_image_info(w, image_info->index);
1020         return 0;
1021
1022 err_nomem2:
1023         destroy_image_info(image_info);
1024 err_nomem1:
1025         if (w->wim_info)
1026                 wim_info->num_images--;
1027         else
1028                 FREE(wim_info);
1029         ERROR("Out of memory!\n");
1030         return WIMLIB_ERR_NOMEM;
1031 }
1032
1033 /* Prints information about the specified image from struct wim_info structure. 
1034  * @image may be WIM_ALL_IMAGES. */
1035 void print_image_info(const struct wim_info *wim_info, int image)
1036 {
1037         uint i;
1038         const struct image_info *image_info;
1039         const char *desc;
1040         time_t ctime;
1041         time_t mtime;
1042
1043         DEBUG("Printing the image info for image %d\n", image);
1044
1045         if (image == WIM_ALL_IMAGES) {
1046                 for (i = 1; i <= wim_info->num_images; i++)
1047                         print_image_info(wim_info, i);
1048         } else {
1049                 image_info = &wim_info->images[image - 1];
1050
1051                 printf("Index:                  %"PRIu64"\n", 
1052                         image_info->index);
1053                 printf("Name:                   %s\n", 
1054                         image_info->name);
1055
1056                 /* Always print the Description: part even if there is no
1057                  * description. */
1058                 if (image_info->description)
1059                         desc = image_info->description;
1060                 else
1061                         desc = "";
1062                 printf("Description:            %s\n", desc);
1063
1064                 if (image_info->display_name)
1065                         printf("Display Name:           %s\n", 
1066                                 image_info->display_name);
1067
1068                 if (image_info->display_description)
1069                         printf("Display Description:    %s\n", 
1070                                 image_info->display_description);
1071
1072                 printf("Directory Count:        %"PRIu64"\n", 
1073                                 image_info->dir_count);
1074                 printf("File Count:             %"PRIu64"\n", 
1075                                 image_info->file_count);
1076                 printf("Total Bytes:            %"PRIu64"\n", 
1077                                 image_info->total_bytes);
1078                 printf("Hard Link Bytes:        %"PRIu64"\n", 
1079                                 image_info->hard_link_bytes);
1080
1081                 ctime = ms_timestamp_to_unix(image_info->creation_time);
1082                 mtime = ms_timestamp_to_unix(image_info->last_modification_time);
1083
1084                 printf("Creation Time:          %s", asctime(localtime(&ctime)));
1085                 printf("Last Modification Time: %s", asctime(localtime(&mtime)));
1086                 if (image_info->windows_info_exists)
1087                         print_windows_info(&image_info->windows_info);
1088                 if (image_info->flags)
1089                         printf("Flags:                  %s\n", image_info->flags);
1090                 putchar('\n');
1091         }
1092 }
1093
1094 /* 
1095  * Reads the XML data from a WIM file.
1096  */
1097 int read_xml_data(FILE *fp, const struct resource_entry *res, u8 **xml_data_ret,
1098                   struct wim_info **info_ret)
1099 {
1100         u8 *xml_data;
1101         xmlDoc *doc;
1102         xmlNode *root;
1103         int ret;
1104
1105         DEBUG("XML data is %"PRIu64" bytes long.\n", (u64)res->size);
1106
1107         if (resource_is_compressed(res)) {
1108                 ERROR("XML data is supposed to be uncompressed!\n");
1109                 ret = WIMLIB_ERR_XML;
1110                 goto err0;
1111         }
1112         if (res->size < 2) {
1113                 ERROR("XML data must be at least 2 bytes!\n");
1114                 ret = WIMLIB_ERR_XML;
1115                 goto err0;
1116         }
1117
1118         xml_data = MALLOC(res->size + 2);
1119         if (!xml_data) {
1120                 ret = WIMLIB_ERR_NOMEM;
1121                 goto err0;
1122         }
1123         ret = read_full_resource(fp, res->size, res->size, res->offset, 
1124                                  WIM_COMPRESSION_TYPE_NONE, xml_data);
1125         if (ret != 0)
1126                 goto err1;
1127
1128         xml_data[res->size] = 0;
1129         xml_data[res->size + 1] = 0;
1130
1131         DEBUG("Parsing XML using libxml2 to create XML tree.\n");
1132
1133         doc = xmlReadMemory(xml_data, res->size, "noname.xml", "UTF-16", 0);
1134
1135
1136         if (!doc) {
1137                 ERROR("Failed to parse XML data!\n");
1138                 ret = WIMLIB_ERR_XML;
1139                 goto err1;
1140         }
1141
1142         DEBUG("Constructing WIM information structure from XML tree.\n");
1143
1144         root = xmlDocGetRootElement(doc);
1145         if (!root) {
1146                 ERROR("Empty XML document!\n");
1147                 ret = WIMLIB_ERR_XML;
1148                 goto err2;
1149         }
1150
1151         if (!node_is_element(root) || !node_name_is(root, "WIM")) {
1152                 ERROR("Expected <WIM> for the root XML element! "
1153                                 "(found <%s>)\n", root->name);
1154                 ret = WIMLIB_ERR_XML;
1155                 goto err2;
1156         }
1157
1158         ret = xml_read_wim_info(root, info_ret);
1159         if (ret != 0)
1160                 goto err2;
1161
1162         DEBUG("Freeing XML tree.\n");
1163
1164         xmlFreeDoc(doc);
1165         xmlCleanupParser();
1166         *xml_data_ret = xml_data;
1167         return 0;
1168 err2:
1169         xmlFreeDoc(doc);
1170 err1:
1171         FREE(xml_data);
1172 err0:
1173         xmlCleanupParser();
1174         return ret;
1175 }
1176
1177 #define CHECK_RET  ({   if (ret < 0)  { \
1178                                 ERROR("Error writing XML data!\n"); \
1179                                 ret = WIMLIB_ERR_WRITE; \
1180                                 goto err2; \
1181                         } })
1182
1183 /* 
1184  * Writes XML data to a WIM file.
1185  */
1186 int write_xml_data(const struct wim_info *wim_info, int image, FILE *out)
1187 {
1188         xmlBuffer     *buf;
1189         xmlTextWriter *writer;
1190         char          *utf16_str;
1191         int ret;
1192         off_t total_bytes;
1193         int num_images;
1194         int i;
1195         const xmlChar *content;
1196         size_t len;
1197         size_t utf16_len;
1198         size_t bytes_written;
1199
1200         wimlib_assert(image == WIM_ALL_IMAGES || 
1201                         (wim_info != NULL && image >= 1 && 
1202                          image <= wim_info->num_images));
1203
1204         /* The contents of the <TOTALBYTES> element in the XML data, under the
1205          * <WIM> element not the <IMAGE> element, is the size of the WIM file
1206          * excluding the XML data and integrity table.  Which is the current
1207          * offset, since the XML data goes at the end of the WIM file before the
1208          * integrity table. */
1209         total_bytes = ftello(out);
1210         if (total_bytes == -1)
1211                 return WIMLIB_ERR_WRITE;
1212
1213         DEBUG("Creating XML buffer and text writer\n");
1214         buf = xmlBufferCreate();
1215         if (!buf) {
1216                 ERROR("Failed to allocate XML buffer!\n");
1217                 ret = WIMLIB_ERR_NOMEM;
1218                 goto err0;
1219         }
1220         writer = xmlNewTextWriterMemory(buf, 0);
1221         if (!writer) {
1222                 ERROR("Failed to allocate XML writer!\n");
1223                 ret = WIMLIB_ERR_NOMEM;
1224                 goto err1;
1225         }
1226
1227         /* XXX */
1228         /* M$'s WIM files do not have XML declarations, so do not write one.
1229          * I'm not sure how we can force the document to be written in UTF-16
1230          * without calling xmlTextWriterStartDocument(), though, so currently it
1231          * is composed in a buffer UTF-8, then converted to UTF-16. */
1232 #if 0
1233         ret = xmlTextWriterStartDocument(writer, NULL, "UTF-16", NULL);
1234         CHECK_RET;
1235 #endif
1236
1237         DEBUG("Writing <WIM> element\n");
1238         ret = xmlTextWriterStartElement(writer, "WIM");
1239         CHECK_RET;
1240
1241         ret = xmlTextWriterWriteFormatElement(writer, "TOTALBYTES", "%"PRIu64,
1242                                               total_bytes);
1243         CHECK_RET;
1244
1245         if (wim_info)
1246                 num_images = wim_info->num_images;
1247         else
1248                 num_images = 0;
1249         DEBUG("Writing %u <IMAGE> elements\n", num_images);
1250
1251         for (i = 1; i <= num_images; i++) {
1252                 if (image != WIM_ALL_IMAGES && i != image)
1253                         continue;
1254                 DEBUG("Writing <IMAGE> element for image %d\n", i);
1255                 ret = xml_write_image_info(writer, &wim_info->images[i - 1]);
1256                 CHECK_RET;
1257         }
1258
1259         ret = xmlTextWriterEndElement(writer);
1260         CHECK_RET;
1261
1262         ret = xmlTextWriterEndDocument(writer);
1263         CHECK_RET;
1264
1265         DEBUG("Done composing XML document. Now converting to UTF-16 and "
1266                         "writing it to the output file.\n");
1267
1268         content = xmlBufferContent(buf);
1269         len = xmlBufferLength(buf);
1270
1271         utf16_str = utf8_to_utf16(content, len, &utf16_len);
1272         if (!utf16_str) {
1273                 ret = WIMLIB_ERR_NOMEM;
1274                 goto err2;
1275         }
1276
1277         if ((putc(0xff, out)) == EOF || (putc(0xfe, out) == EOF) || 
1278                 ((bytes_written = fwrite(utf16_str, 1, utf16_len, out))
1279                                 != utf16_len)) {
1280                 ERROR("Error writing XML data: %m\n");
1281                 ret = WIMLIB_ERR_WRITE;
1282                 goto err3;
1283         }
1284
1285         DEBUG("Cleaning up.\n");
1286
1287         ret = 0;
1288 err3:
1289         FREE(utf16_str);
1290 err2:
1291         xmlFreeTextWriter(writer);
1292 err1:
1293         xmlBufferFree(buf);
1294 err0:
1295         return ret;
1296 }
1297
1298 /* Returns the name of the specified image. */
1299 WIMLIBAPI const char *wimlib_get_image_name(const WIMStruct *w, int image)
1300 {
1301         DEBUG("Getting the name of image %d\n", image);
1302         if (image < 1 || image > w->hdr.image_count)
1303                 return NULL;
1304
1305         return w->wim_info->images[image - 1].name;
1306 }
1307
1308 /* Returns the description of the specified image. */
1309 WIMLIBAPI const char *wimlib_get_image_description(const WIMStruct *w, 
1310                                                    int image)
1311 {
1312         DEBUG("Getting the description of image %d\n", image);
1313         if (image < 1 || image > w->hdr.image_count)
1314                 return NULL;
1315
1316         return w->wim_info->images[image - 1].description;
1317 }
1318
1319 /* Determines if an image name is already used by some image in the WIM. */
1320 WIMLIBAPI bool wimlib_image_name_in_use(const WIMStruct *w, const char *name)
1321 {
1322         int i;
1323
1324         DEBUG("Checking to see if the image name `%s' is already "
1325                                                 "in use\n", name);
1326         if (!name || !w->wim_info)
1327                 return false;
1328         for (i = 1; i <= w->wim_info->num_images; i++)
1329                 if (strcmp(w->wim_info->images[i - 1].name, name) == 0)
1330                         return true;
1331
1332         return false;
1333 }
1334
1335 WIMLIBAPI int wimlib_extract_xml_data(WIMStruct *w, FILE *fp)
1336 {
1337         DEBUG("Extracting the XML data.\n");
1338         if (fwrite(w->xml_data, 1, w->hdr.xml_res_entry.size, fp) != 
1339                         w->hdr.xml_res_entry.size) {
1340                 ERROR("Failed to extract XML data!\n");
1341                 return WIMLIB_ERR_WRITE;
1342         }
1343         return 0;
1344 }
1345
1346 /* Sets the name of an image in the WIM. */
1347 WIMLIBAPI int wimlib_set_image_name(WIMStruct *w, int image, const char *name)
1348 {
1349         char *p;
1350         int i;
1351
1352         DEBUG("Setting the name of image %d to %s\n", image, name);
1353
1354         if (!name || !*name) {
1355                 ERROR("Must specify a non-empty string for the image "
1356                                 "name!\n");
1357                 return WIMLIB_ERR_INVALID_PARAM;
1358         }
1359         if (image < 1 || image > w->hdr.image_count) {
1360                 ERROR("%d is not a valid image!\n", image);
1361                 return WIMLIB_ERR_INVALID_IMAGE;
1362         }
1363
1364         for (i = 1; i <= w->hdr.image_count; i++) {
1365                 if (i == image)
1366                         continue;
1367                 if (strcmp(w->wim_info->images[i - 1].name, name) == 0) {
1368                         ERROR("The name `%s' is already used for image %d!\n",
1369                                         name, i);
1370                         return WIMLIB_ERR_IMAGE_NAME_COLLISION;
1371                 }
1372         }
1373
1374         p = STRDUP(name);
1375         if (!p) {
1376                 ERROR("Out of memory!\n");
1377                 return WIMLIB_ERR_NOMEM;
1378         }
1379         FREE(w->wim_info->images[image - 1].name);
1380         w->wim_info->images[image - 1].name = p;
1381         return 0;
1382 }
1383
1384 /* Sets the description of an image in the WIM. */
1385 WIMLIBAPI int wimlib_set_image_descripton(WIMStruct *w, int image, 
1386                                           const char *description)
1387 {
1388         char *p;
1389
1390         DEBUG("Setting the description of image %d to %s\n", image, 
1391               description);
1392
1393         if (image < 1 || image > w->hdr.image_count) {
1394                 ERROR("%d is not a valid image!\n", image);
1395                 return WIMLIB_ERR_INVALID_IMAGE;
1396         }
1397         if (description) {
1398                 p = STRDUP(description);
1399                 if (!p) {
1400                         ERROR("Out of memory!\n");
1401                         return WIMLIB_ERR_NOMEM;
1402                 }
1403         } else {
1404                 p = NULL;
1405         }
1406         FREE(w->wim_info->images[image - 1].description);
1407         w->wim_info->images[image - 1].description = p;
1408         return 0;
1409 }