4 * Deals with the XML information in WIM files. Uses the C library libxml2.
6 * Copyright (C) 2012 Eric Biggers
8 * wimlib - Library for working with WIM files
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
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.
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
24 #include "wimlib_internal.h"
27 #include "timestamp.h"
30 #include <libxml/parser.h>
31 #include <libxml/tree.h>
32 #include <libxml/xmlwriter.h>
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). */
37 struct windows_version {
49 char *installation_type;
54 char *default_language;
57 bool windows_version_exists;
58 struct windows_version windows_version;
68 u64 last_modification_time;
69 bool windows_info_exists;
70 struct windows_info windows_info;
74 char *display_description;
79 /* Returns a statically allocated string that is a string representation of the
80 * architecture number. */
81 static const char *get_arch(int arch)
91 /* XXX Are there other arch values? */
93 snprintf(buf, sizeof(buf), "%d (unknown)", arch);
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)
103 /* Utility functions for xmlNodes */
104 static inline bool node_is_element(xmlNode *node)
106 return node->type == XML_ELEMENT_NODE;
109 static inline bool node_is_text(xmlNode *node)
111 return node->type == XML_TEXT_NODE;
114 static inline bool node_is_attribute(xmlNode *node)
116 return node->type == XML_ATTRIBUTE_NODE;
119 static inline bool node_name_is(xmlNode *node, const char *name)
121 /* For now, both upper case and lower case element names are accepted. */
122 return strcasecmp((const char *)node->name, name) == 0;
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
128 static u64 node_get_u64(const xmlNode *u64_node)
131 for_node_child(u64_node, child)
132 if (node_is_text(child))
133 return strtoull((const char *)child->content, NULL, 10);
137 /* Like node_get_u64(), but expects a number in base 16. */
138 static u64 node_get_hex_u64(const xmlNode *u64_node)
141 for_node_child(u64_node, child)
142 if (node_is_text(child))
143 return strtoull(child->content, NULL, 16);
147 static int node_get_string(const xmlNode *string_node, char **str)
152 for_node_child(string_node, child) {
153 if (node_is_text(child) && child->content) {
154 p = STRDUP(child->content);
156 return WIMLIB_ERR_NOMEM;
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)
171 for_node_child(time_node, child) {
172 if (!node_is_element(child))
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);
179 return (u64)low_part | ((u64)high_part << 32);
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)
185 u64 index_1 = ((struct image_info*)p1)->index;
186 u64 index_2 = ((struct image_info*)p1)->index;
187 if (index_1 < index_2)
189 else if (index_1 > index_2)
196 /* Frees memory allocated inside a struct windows_info structure. */
197 static void destroy_windows_info(struct windows_info *windows_info)
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);
211 /* Frees memory allocated inside a struct image_info structure. */
212 static void destroy_image_info(struct image_info *image_info)
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));
223 void free_wim_info(struct wim_info *info)
228 for (i = 0; i < info->num_images; i++)
229 destroy_image_info(&info->images[i]);
236 /* Reads the information from a <VERSION> element inside the <WINDOWS> element.
238 static void xml_read_windows_version(const xmlNode *version_node,
239 struct windows_version* windows_version)
242 for_node_child(version_node, child) {
243 if (!node_is_element(child))
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);
258 /* Reads the information from a <LANGUAGE> element inside a <WINDOWS> element.
260 static int xml_read_languages(const xmlNode *languages_node,
261 char ***languages_ret,
262 u64 *num_languages_ret,
263 char **default_language_ret)
272 for_node_child(languages_node, child)
273 if (node_is_element(child) && node_name_is(child, "LANGUAGE"))
276 languages = CALLOC(num_languages, sizeof(char*));
278 return WIMLIB_ERR_NOMEM;
280 *languages_ret = languages;
281 *num_languages_ret = num_languages;
285 for_node_child(languages_node, child) {
286 if (!node_is_element(child))
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);
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)
305 for_node_child(windows_node, child) {
306 if (!node_is_element(child))
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);
346 /* Reads the information from an <IMAGE> element. */
347 static int xml_read_image_info(xmlNode *image_node,
348 struct image_info *image_info)
354 index_prop = xmlGetProp(image_node, "INDEX");
356 image_info->index = strtoul(index_prop, NULL, 10);
359 image_info->index = 0;
363 for_node_child(image_node, child) {
364 if (!node_is_element(child))
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);
397 if (!image_info->name) {
398 ERROR("Image with index %"PRIu64" has no name\n",
400 return WIMLIB_ERR_XML;
406 /* Reads the information from a <WIM> element, which should be the root element
407 * of the XML tree. */
408 static int xml_read_wim_info(const xmlNode *wim_node, struct wim_info **wim_info_ret)
410 struct wim_info *wim_info;
414 struct image_info *cur_image_info;
416 wim_info = CALLOC(1, sizeof(struct wim_info));
418 ERROR("Out of memory!\n");
419 return WIMLIB_ERR_NOMEM;
422 /* Count how many images there are. */
424 for_node_child(wim_node, child)
425 if (node_is_element(child) && node_name_is(child, "IMAGE"))
431 /* Allocate the array of struct image_infos and fill them in. */
432 wim_info->images = CALLOC(num_images, sizeof(wim_info->images[0]));
433 if (!wim_info->images) {
434 ret = WIMLIB_ERR_NOMEM;
435 ERROR("Out of memory!\n");
438 wim_info->num_images = num_images;
439 cur_image_info = wim_info->images;
440 for_node_child(wim_node, child) {
441 if (!node_is_element(child))
443 if (node_name_is(child, "IMAGE")) {
444 DEBUG("Found <IMAGE> tag\n");
445 ret = xml_read_image_info(child, cur_image_info++);
448 } else if (node_name_is(child, "TOTALBYTES")) {
449 wim_info->total_bytes = node_get_u64(child);
453 /* Sort the array of struct image_infos by image index. */
454 qsort(wim_info->images, wim_info->num_images,
455 sizeof(struct image_info), sort_by_index);
457 *wim_info_ret = wim_info;
460 free_wim_info(wim_info);
464 /* Prints the information contained in a struct windows_info structure. */
465 static void print_windows_info(const struct windows_info *windows_info)
468 const struct windows_version *windows_version;
470 printf("Architecture: %s\n", get_arch(windows_info->arch));
471 printf("Product Name: %s\n", windows_info->product_name);
472 printf("Edition ID: %s\n", windows_info->edition_id);
473 printf("Installation Type: %s\n", windows_info->installation_type);
474 if (windows_info->hal)
475 printf("HAL: %s\n", windows_info->hal);
476 printf("Product Type: %s\n", windows_info->product_type);
477 if (windows_info->product_suite)
478 printf("Product Suite: %s\n", windows_info->product_suite);
479 printf("Languages: ");
480 for (i = 0; i < windows_info->num_languages; i++) {
481 fputs(windows_info->languages[i], stdout);
485 printf("Default Language: %s\n", windows_info->default_language);
486 printf("System Root: %s\n", windows_info->system_root);
487 if (windows_info->windows_version_exists) {
488 windows_version = &windows_info->windows_version;
489 printf("Major Version: %"PRIu64"\n",
490 windows_version->major);
491 printf("Minor Version: %"PRIu64"\n",
492 windows_version->minor);
493 printf("Build: %"PRIu64"\n",
494 windows_version->build);
495 printf("Service Pack Build: %"PRIu64"\n",
496 windows_version->sp_build);
497 printf("Service Pack Level: %"PRIu64"\n",
498 windows_version->sp_level);
503 /* Writes the information contained in a struct windows_version structure to the XML
504 * document being constructed in memory. This is the <VERSION> element inside
505 * the <WINDOWS> element. */
506 static int xml_write_windows_version(xmlTextWriter *writer,
507 const struct windows_version *version)
510 rc = xmlTextWriterStartElement(writer, "VERSION");
514 rc = xmlTextWriterWriteFormatElement(writer, "MAJOR", "%"PRIu64,
519 rc = xmlTextWriterWriteFormatElement(writer, "MINOR", "%"PRIu64,
524 rc = xmlTextWriterWriteFormatElement(writer, "BUILD", "%"PRIu64,
529 rc = xmlTextWriterWriteFormatElement(writer, "SPBUILD", "%"PRIu64,
534 rc = xmlTextWriterWriteFormatElement(writer, "SPLEVEL", "%"PRIu64,
539 return xmlTextWriterEndElement(writer); /* </VERSION> */
542 /* Writes the information contained in a struct windows_info structure to the XML
543 * document being constructed in memory. This is the <WINDOWS> element. */
544 static int xml_write_windows_info(xmlTextWriter *writer,
545 const struct windows_info *windows_info)
548 rc = xmlTextWriterStartElement(writer, "WINDOWS");
553 rc = xmlTextWriterWriteFormatElement(writer, "ARCH", "%"PRIu64,
558 if (windows_info->product_name) {
559 rc = xmlTextWriterWriteElement(writer, "PRODUCTNAME",
560 windows_info->product_name);
565 if (windows_info->edition_id) {
566 rc = xmlTextWriterWriteElement(writer, "EDITIONID",
567 windows_info->edition_id);
572 if (windows_info->installation_type) {
573 rc = xmlTextWriterWriteElement(writer, "INSTALLATIONTYPE",
574 windows_info->installation_type);
579 if (windows_info->hal) {
580 rc = xmlTextWriterWriteElement(writer, "HAL",
586 if (windows_info->system_root) {
587 rc = xmlTextWriterWriteElement(writer, "SYSTEMROOT",
588 windows_info->system_root);
593 if (windows_info->product_type) {
594 rc = xmlTextWriterWriteElement(writer, "PRODUCTTYPE",
595 windows_info->product_type);
600 if (windows_info->product_suite) {
601 rc = xmlTextWriterWriteElement(writer, "PRODUCTSUITE",
602 windows_info->product_suite);
607 if (windows_info->num_languages) {
608 rc = xmlTextWriterStartElement(writer, "LANGUAGES");
612 for (int i = 0; i < windows_info->num_languages; i++) {
613 rc = xmlTextWriterWriteElement(writer, "LANGUAGE",
614 windows_info->languages[i]);
618 rc = xmlTextWriterWriteElement(writer, "DEFAULT",
619 windows_info->default_language);
623 rc = xmlTextWriterEndElement(writer); /* </LANGUAGES> */
628 if (windows_info->windows_version_exists) {
629 rc = xml_write_windows_version(writer, &windows_info->windows_version);
634 return xmlTextWriterEndElement(writer); /* </WINDOWS> */
637 /* Writes a time element to the XML document being constructed in memory. */
638 static int xml_write_time(xmlTextWriter *writer, const char *element_name,
642 rc = xmlTextWriterStartElement(writer, element_name);
646 rc = xmlTextWriterWriteFormatElement(writer, "HIGHPART",
647 "0x%"PRIX32, (u32)(time >> 32));
651 rc = xmlTextWriterWriteFormatElement(writer, "LOWPART",
652 "0x%"PRIX32, (u32)time);
656 rc = xmlTextWriterEndElement(writer); /* </@element_name> */
663 /* Writes an <IMAGE> element to the XML document. */
664 static int xml_write_image_info(xmlTextWriter *writer,
665 const struct image_info *image_info)
668 rc = xmlTextWriterStartElement(writer, "IMAGE");
672 rc = xmlTextWriterWriteFormatAttribute(writer, "INDEX", "%"PRIu64,
677 rc = xmlTextWriterWriteFormatElement(writer, "DIRCOUNT", "%"PRIu64,
678 image_info->dir_count);
682 rc = xmlTextWriterWriteFormatElement(writer, "FILECOUNT", "%"PRIu64,
683 image_info->file_count);
687 rc = xmlTextWriterWriteFormatElement(writer, "TOTALBYTES", "%"PRIu64,
688 image_info->total_bytes);
692 rc = xmlTextWriterWriteFormatElement(writer, "HARDLINKBYTES", "%"PRIu64,
693 image_info->hard_link_bytes);
697 rc = xml_write_time(writer, "CREATIONTIME",
698 image_info->creation_time);
702 rc = xml_write_time(writer, "LASTMODIFICATIONTIME",
703 image_info->last_modification_time);
707 if (image_info->windows_info_exists) {
708 rc = xml_write_windows_info(writer, &image_info->windows_info);
712 DEBUG("<WINDOWS> tag does not exist.\n");
715 if (image_info->name) {
716 rc = xmlTextWriterWriteElement(writer, "NAME", image_info->name);
720 if (image_info->description) {
721 rc = xmlTextWriterWriteElement(writer, "DESCRIPTION",
722 image_info->description);
726 if (image_info->display_name) {
727 rc = xmlTextWriterWriteElement(writer, "DISPLAYNAME",
728 image_info->display_name);
732 if (image_info->display_description) {
733 rc = xmlTextWriterWriteElement(writer, "DISPLAYDESCRIPTION",
734 image_info->display_description);
739 if (image_info->flags) {
740 rc = xmlTextWriterWriteElement(writer, "FLAGS",
746 return xmlTextWriterEndElement(writer); /* </IMAGE> */
751 /* Makes space for another image in the XML information and return a pointer to
753 static struct image_info *add_image_info_struct(struct wim_info *wim_info)
755 struct image_info *images;
757 images = CALLOC(wim_info->num_images + 1, sizeof(struct image_info));
760 memcpy(images, wim_info->images,
761 wim_info->num_images * sizeof(struct image_info));
762 FREE(wim_info->images);
763 wim_info->images = images;
764 wim_info->num_images++;
765 return &images[wim_info->num_images - 1];
768 static int clone_windows_info(const struct windows_info *old,
769 struct windows_info *new)
773 if (old->product_name && !(new->product_name = STRDUP(old->product_name)))
774 return WIMLIB_ERR_NOMEM;
775 if (old->edition_id && !(new->edition_id = STRDUP(old->edition_id)))
776 return WIMLIB_ERR_NOMEM;
777 if (old->installation_type && !(new->installation_type =
778 STRDUP(old->installation_type)))
779 return WIMLIB_ERR_NOMEM;
780 if (old->hal && !(new->hal = STRDUP(old->hal)))
781 return WIMLIB_ERR_NOMEM;
782 if (old->product_type && !(new->product_type = STRDUP(old->product_type)))
783 return WIMLIB_ERR_NOMEM;
784 if (old->product_suite && !(new->product_suite = STRDUP(old->product_suite)))
785 return WIMLIB_ERR_NOMEM;
787 if (old->languages) {
788 new->languages = CALLOC(old->num_languages, sizeof(char*));
790 return WIMLIB_ERR_NOMEM;
791 new->num_languages = old->num_languages;
792 for (i = 0; i < new->num_languages; i++) {
793 if (!old->languages[i])
795 new->languages[i] = STRDUP(old->languages[i]);
796 if (!new->languages[i])
797 return WIMLIB_ERR_NOMEM;
800 if (old->default_language &&
801 !(new->default_language = STRDUP(old->default_language)))
802 return WIMLIB_ERR_NOMEM;
803 if (old->system_root && !(new->system_root = STRDUP(old->system_root)))
804 return WIMLIB_ERR_NOMEM;
808 static int clone_image_info(const struct image_info *old, struct image_info *new)
812 new->dir_count = old->dir_count;
813 new->file_count = old->file_count;
814 new->total_bytes = old->total_bytes;
815 new->hard_link_bytes = old->hard_link_bytes;
816 new->creation_time = old->creation_time;
817 new->last_modification_time = old->last_modification_time;
819 if (!(new->name = STRDUP(old->name)))
820 return WIMLIB_ERR_NOMEM;
822 if (old->description)
823 if (!(new->description = STRDUP(old->description)))
824 return WIMLIB_ERR_NOMEM;
826 if (old->display_name)
827 if (!(new->display_name = STRDUP(old->display_name)))
828 return WIMLIB_ERR_NOMEM;
830 if (old->display_description)
831 if (!(new->display_description = STRDUP(old->display_description)))
832 return WIMLIB_ERR_NOMEM;
835 if (!(new->flags = STRDUP(old->flags)))
836 return WIMLIB_ERR_NOMEM;
838 if (old->windows_info_exists) {
839 new->windows_info_exists = true;
840 return clone_windows_info(&old->windows_info,
846 /* Copies the XML information for an image between WIM files.
848 * @dest_image_name and @dest_image_description are ignored if they are NULL;
849 * otherwise, they are used to override the image name and/or image description
850 * from the XML data in the source WIM file. */
851 int xml_export_image(const struct wim_info *old_wim_info,
853 struct wim_info **new_wim_info_p,
854 const char *dest_image_name,
855 const char *dest_image_description)
857 struct wim_info *new_wim_info;
858 struct image_info *image_info;
863 DEBUG("Copying XML data between WIM files for source image %d\n",
866 wimlib_assert(image >= 1 && image <= old_wim_info->num_images);
869 if (*new_wim_info_p) {
870 new_wim_info = *new_wim_info_p;
872 new_wim_info = CALLOC(1, sizeof(struct wim_info));
877 image_info = add_image_info_struct(new_wim_info);
881 ret = clone_image_info(&old_wim_info->images[image - 1], image_info);
885 image_info->index = new_wim_info->num_images;
887 if (dest_image_name) {
888 FREE(image_info->name);
889 image_info->name = STRDUP(dest_image_name);
890 if (!image_info->name)
893 if (dest_image_description) {
894 FREE(image_info->description);
895 image_info->description = STRDUP(dest_image_description);
896 if (!image_info->description)
899 *new_wim_info_p = new_wim_info;
902 ERROR("Out of memory!\n");
903 free_wim_info(new_wim_info);
904 return WIMLIB_ERR_NOMEM;
907 /* Removes an image from the XML information. */
908 void xml_delete_image(struct wim_info **wim_info_p, int image)
910 struct wim_info *wim_info;
913 DEBUG("Deleting image %d from the XML data\n", image);
915 wim_info = *wim_info_p;
917 wimlib_assert(wim_info);
918 wimlib_assert(image >= 1 && image <= wim_info->num_images);
920 destroy_image_info(&wim_info->images[image - 1]);
922 for (i = image - 1; i < wim_info->num_images - 1; i++) {
923 memcpy(&wim_info->images[i], &wim_info->images[i + 1],
924 sizeof(struct image_info));
925 wim_info->images[i].index--;
928 if (--wim_info->num_images == 0) {
929 free_wim_info(wim_info);
934 size_t xml_get_max_image_name_len(const WIMStruct *w)
938 uint num_images = w->wim_info->num_images;
939 for (i = 0; i < num_images; i++)
940 len = max(len, strlen(w->wim_info->images[i].name));
944 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
945 void xml_set_memory_allocator(void *(*malloc_func)(size_t),
946 void (*free_func)(void *),
947 void *(*realloc_func)(void *, size_t))
949 xmlMemSetup(free_func, malloc_func, realloc_func, STRDUP);
953 void xml_update_image_info(WIMStruct *w, int image)
955 struct image_info *image_info;
958 DEBUG("Updating the image info for image %d\n", image);
960 image_info = &w->wim_info->images[image - 1];
961 root = w->image_metadata[image - 1].root_dentry;
963 calculate_dir_tree_statistics(root, w->lookup_table,
964 &image_info->dir_count,
965 &image_info->file_count,
966 &image_info->total_bytes,
967 &image_info->hard_link_bytes);
969 image_info->last_modification_time = get_timestamp();
972 /* Adds an image to the XML information. */
973 int xml_add_image(WIMStruct *w, struct dentry *root_dentry, const char *name,
974 const char *description, const char *flags_element)
976 struct wim_info *wim_info;
977 struct image_info *image_info;
981 DEBUG("Adding image: name = %s, description = %s, flags_element = %s\n",
982 name, description, flags_element);
984 /* If this is the first image, allocate the struct wim_info. Otherwise
985 * use the existing struct wim_info. */
987 wim_info = w->wim_info;
989 DEBUG("Allocing struct wim_info with 1 image\n");
990 wim_info = CALLOC(1, sizeof(struct wim_info));
992 ERROR("Could not allocate WIM information struct--- "
994 return WIMLIB_ERR_NOMEM;
998 image_info = add_image_info_struct(wim_info);
1002 if (!(image_info->name = STRDUP(name)))
1005 if (description && !(image_info->description = STRDUP(description)))
1007 if (flags_element && !(image_info->flags = STRDUP(flags_element)))
1010 w->wim_info = wim_info;
1011 image_info->index = wim_info->num_images;
1012 image_info->creation_time = get_timestamp();
1013 xml_update_image_info(w, image_info->index);
1017 destroy_image_info(image_info);
1020 wim_info->num_images--;
1023 ERROR("Out of memory!\n");
1024 return WIMLIB_ERR_NOMEM;
1027 /* Prints information about the specified image from struct wim_info structure.
1028 * @image may be WIM_ALL_IMAGES. */
1029 void print_image_info(const struct wim_info *wim_info, int image)
1032 const struct image_info *image_info;
1037 DEBUG("Printing the image info for image %d\n", image);
1039 if (image == WIM_ALL_IMAGES) {
1040 for (i = 1; i <= wim_info->num_images; i++)
1041 print_image_info(wim_info, i);
1043 image_info = &wim_info->images[image - 1];
1045 printf("Index: %"PRIu64"\n",
1047 printf("Name: %s\n",
1050 /* Always print the Description: part even if there is no
1052 if (image_info->description)
1053 desc = image_info->description;
1056 printf("Description: %s\n", desc);
1058 if (image_info->display_name)
1059 printf("Display Name: %s\n",
1060 image_info->display_name);
1062 if (image_info->display_description)
1063 printf("Display Description: %s\n",
1064 image_info->display_description);
1066 printf("Directory Count: %"PRIu64"\n",
1067 image_info->dir_count);
1068 printf("File Count: %"PRIu64"\n",
1069 image_info->file_count);
1070 printf("Total Bytes: %"PRIu64"\n",
1071 image_info->total_bytes);
1072 printf("Hard Link Bytes: %"PRIu64"\n",
1073 image_info->hard_link_bytes);
1075 ctime = ms_timestamp_to_unix(image_info->creation_time);
1076 mtime = ms_timestamp_to_unix(image_info->last_modification_time);
1078 printf("Creation Time: %s", asctime(localtime(&ctime)));
1079 printf("Last Modification Time: %s", asctime(localtime(&mtime)));
1080 if (image_info->windows_info_exists)
1081 print_windows_info(&image_info->windows_info);
1082 if (image_info->flags)
1083 printf("Flags: %s\n", image_info->flags);
1089 * Reads the XML data from a WIM file.
1091 int read_xml_data(FILE *fp, const struct resource_entry *res, u8 **xml_data_ret,
1092 struct wim_info **info_ret)
1099 DEBUG("XML data is %"PRIu64" bytes long.\n", (u64)res->size);
1101 if (resource_is_compressed(res)) {
1102 ERROR("XML data is supposed to be uncompressed!\n");
1103 ret = WIMLIB_ERR_XML;
1106 if (res->size < 2) {
1107 ERROR("XML data must be at least 2 bytes!\n");
1108 ret = WIMLIB_ERR_XML;
1112 xml_data = MALLOC(res->size + 2);
1114 ret = WIMLIB_ERR_NOMEM;
1117 ret = read_full_resource(fp, res->size, res->size, res->offset,
1118 WIM_COMPRESSION_TYPE_NONE, xml_data);
1122 xml_data[res->size] = 0;
1123 xml_data[res->size + 1] = 0;
1125 DEBUG("Parsing XML using libxml2 to create XML tree.\n");
1127 doc = xmlReadMemory(xml_data, res->size, "noname.xml", "UTF-16", 0);
1131 ERROR("Failed to parse XML data!\n");
1132 ret = WIMLIB_ERR_XML;
1136 DEBUG("Constructing WIM information structure from XML tree.\n");
1138 root = xmlDocGetRootElement(doc);
1140 ERROR("Empty XML document!\n");
1141 ret = WIMLIB_ERR_XML;
1145 if (!node_is_element(root) || !node_name_is(root, "WIM")) {
1146 ERROR("Expected <WIM> for the root XML element! "
1147 "(found <%s>)\n", root->name);
1148 ret = WIMLIB_ERR_XML;
1152 ret = xml_read_wim_info(root, info_ret);
1156 DEBUG("Freeing XML tree.\n");
1160 *xml_data_ret = xml_data;
1171 #define CHECK_RET ({ if (ret < 0) { \
1172 ERROR("Error writing XML data!\n"); \
1173 ret = WIMLIB_ERR_WRITE; \
1178 * Writes XML data to a WIM file.
1180 int write_xml_data(const struct wim_info *wim_info, int image, FILE *out)
1183 xmlTextWriter *writer;
1189 const xmlChar *content;
1192 size_t bytes_written;
1194 wimlib_assert(image == WIM_ALL_IMAGES ||
1195 (wim_info != NULL && image >= 1 &&
1196 image <= wim_info->num_images));
1198 /* The contents of the <TOTALBYTES> element in the XML data, under the
1199 * <WIM> element not the <IMAGE> element, is the size of the WIM file
1200 * excluding the XML data and integrity table. Which is the current
1201 * offset, since the XML data goes at the end of the WIM file before the
1202 * integrity table. */
1203 total_bytes = ftello(out);
1204 if (total_bytes == -1)
1205 return WIMLIB_ERR_WRITE;
1207 DEBUG("Creating XML buffer and text writer\n");
1208 buf = xmlBufferCreate();
1210 ERROR("Failed to allocate XML buffer!\n");
1211 ret = WIMLIB_ERR_NOMEM;
1214 writer = xmlNewTextWriterMemory(buf, 0);
1216 ERROR("Failed to allocate XML writer!\n");
1217 ret = WIMLIB_ERR_NOMEM;
1222 /* M$'s WIM files do not have XML declarations, so do not write one.
1223 * I'm not sure how we can force the document to be written in UTF-16
1224 * without calling xmlTextWriterStartDocument(), though, so currently it
1225 * is composed in a buffer UTF-8, then converted to UTF-16. */
1227 ret = xmlTextWriterStartDocument(writer, NULL, "UTF-16", NULL);
1231 DEBUG("Writing <WIM> element\n");
1232 ret = xmlTextWriterStartElement(writer, "WIM");
1235 ret = xmlTextWriterWriteFormatElement(writer, "TOTALBYTES", "%"PRIu64,
1240 num_images = wim_info->num_images;
1243 DEBUG("Writing %u <IMAGE> elements\n", num_images);
1245 for (i = 1; i <= num_images; i++) {
1246 if (image != WIM_ALL_IMAGES && i != image)
1248 DEBUG("Writing <IMAGE> element for image %d\n", i);
1249 ret = xml_write_image_info(writer, &wim_info->images[i - 1]);
1253 ret = xmlTextWriterEndElement(writer);
1256 ret = xmlTextWriterEndDocument(writer);
1259 DEBUG("Done composing XML document. Now converting to UTF-16 and "
1260 "writing it to the output file.\n");
1262 content = xmlBufferContent(buf);
1263 len = xmlBufferLength(buf);
1265 utf16_str = utf8_to_utf16(content, len, &utf16_len);
1267 ret = WIMLIB_ERR_NOMEM;
1271 if ((putc(0xff, out)) == EOF || (putc(0xfe, out) == EOF) ||
1272 ((bytes_written = fwrite(utf16_str, 1, utf16_len, out))
1274 ERROR("Error writing XML data: %m\n");
1275 ret = WIMLIB_ERR_WRITE;
1279 DEBUG("Cleaning up.\n");
1285 xmlFreeTextWriter(writer);
1292 /* Returns the name of the specified image. */
1293 WIMLIBAPI const char *wimlib_get_image_name(const WIMStruct *w, int image)
1295 DEBUG("Getting the name of image %d\n", image);
1296 if (image < 1 || image > w->hdr.image_count)
1299 return w->wim_info->images[image - 1].name;
1302 /* Returns the description of the specified image. */
1303 WIMLIBAPI const char *wimlib_get_image_description(const WIMStruct *w,
1306 DEBUG("Getting the description of image %d\n", image);
1307 if (image < 1 || image > w->hdr.image_count)
1310 return w->wim_info->images[image - 1].description;
1313 /* Determines if an image name is already used by some image in the WIM. */
1314 WIMLIBAPI bool wimlib_image_name_in_use(const WIMStruct *w, const char *name)
1318 DEBUG("Checking to see if the image name `%s' is already "
1320 if (!name || !w->wim_info)
1322 for (i = 1; i <= w->wim_info->num_images; i++)
1323 if (strcmp(w->wim_info->images[i - 1].name, name) == 0)
1329 WIMLIBAPI int wimlib_extract_xml_data(WIMStruct *w, FILE *fp)
1331 DEBUG("Extracting the XML data.\n");
1332 if (fwrite(w->xml_data, 1, w->hdr.xml_res_entry.size, fp) !=
1333 w->hdr.xml_res_entry.size) {
1334 ERROR("Failed to extract XML data!\n");
1335 return WIMLIB_ERR_WRITE;
1340 /* Sets the name of an image in the WIM. */
1341 WIMLIBAPI int wimlib_set_image_name(WIMStruct *w, int image, const char *name)
1346 DEBUG("Setting the name of image %d to %s\n", image, name);
1348 if (!name || !*name) {
1349 ERROR("Must specify a non-empty string for the image "
1351 return WIMLIB_ERR_INVALID_PARAM;
1353 if (image < 1 || image > w->hdr.image_count) {
1354 ERROR("%d is not a valid image!\n", image);
1355 return WIMLIB_ERR_INVALID_IMAGE;
1358 for (i = 1; i <= w->hdr.image_count; i++) {
1361 if (strcmp(w->wim_info->images[i - 1].name, name) == 0) {
1362 ERROR("The name `%s' is already used for image %d!\n",
1364 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
1370 ERROR("Out of memory!\n");
1371 return WIMLIB_ERR_NOMEM;
1373 FREE(w->wim_info->images[image - 1].name);
1374 w->wim_info->images[image - 1].name = p;
1378 /* Sets the description of an image in the WIM. */
1379 WIMLIBAPI int wimlib_set_image_descripton(WIMStruct *w, int image,
1380 const char *description)
1384 DEBUG("Setting the description of image %d to %s\n", image,
1387 if (image < 1 || image > w->hdr.image_count) {
1388 ERROR("%d is not a valid image!\n", image);
1389 return WIMLIB_ERR_INVALID_IMAGE;
1392 p = STRDUP(description);
1394 ERROR("Out of memory!\n");
1395 return WIMLIB_ERR_NOMEM;
1400 FREE(w->wim_info->images[image - 1].description);
1401 w->wim_info->images[image - 1].description = p;