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