4 * Deals with the XML information in WIM files. Uses the C library libxml2.
8 * Copyright (C) 2012 Eric Biggers
10 * This file is part of wimlib, a library for working with WIM files.
12 * wimlib is free software; you can redistribute it and/or modify it under the
13 * terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 3 of the License, or (at your option)
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 General Public License for more
22 * You should have received a copy of the GNU General Public License
23 * along with wimlib; if not, see http://www.gnu.org/licenses/.
27 #include "lookup_table.h"
28 #include "timestamp.h"
29 #include "wimlib_internal.h"
33 #include <libxml/parser.h>
34 #include <libxml/tree.h>
35 #include <libxml/xmlwriter.h>
36 #include <libxml/encoding.h>
39 /* Structures used to form an in-memory representation of the XML data (other
40 * than the raw parse tree from libxml). */
42 struct windows_version {
54 char *installation_type;
59 char *default_language;
62 bool windows_version_exists;
63 struct windows_version windows_version;
68 bool windows_info_exists;
74 u64 last_modification_time;
75 struct windows_info windows_info;
79 char *display_description;
82 struct lookup_table *lookup_table;
87 /* Returns a statically allocated string that is a string representation of the
88 * architecture number. */
89 static const char *get_arch(int arch)
99 /* XXX Are there other arch values? */
101 snprintf(buf, sizeof(buf), "%d (unknown)", arch);
107 /* Iterate through the children of an xmlNode. */
108 #define for_node_child(parent, child) \
109 for (child = parent->children; child != NULL; child = child->next)
111 /* Utility functions for xmlNodes */
112 static inline bool node_is_element(xmlNode *node)
114 return node->type == XML_ELEMENT_NODE;
117 static inline bool node_is_text(xmlNode *node)
119 return node->type == XML_TEXT_NODE;
122 static inline bool node_name_is(xmlNode *node, const char *name)
124 /* For now, both upper case and lower case element names are accepted. */
125 return strcasecmp((const char *)node->name, name) == 0;
128 /* Finds the text node that is a child of an element node and returns its
129 * content converted to a 64-bit unsigned integer. Returns 0 if no text node is
131 static u64 node_get_u64(const xmlNode *u64_node)
134 for_node_child(u64_node, child)
135 if (node_is_text(child))
136 return strtoull((const char *)child->content, NULL, 10);
140 /* Like node_get_u64(), but expects a number in base 16. */
141 static u64 node_get_hex_u64(const xmlNode *u64_node)
144 for_node_child(u64_node, child)
145 if (node_is_text(child))
146 return strtoull(child->content, NULL, 16);
150 static int node_get_string(const xmlNode *string_node, char **str)
155 for_node_child(string_node, child) {
156 if (node_is_text(child) && child->content) {
157 p = STRDUP(child->content);
159 return WIMLIB_ERR_NOMEM;
167 /* Returns the timestamp from a time node. It has child elements <HIGHPART> and
168 * <LOWPART> that are then used to construct a 64-bit timestamp. */
169 static u64 node_get_timestamp(const xmlNode *time_node)
174 for_node_child(time_node, child) {
175 if (!node_is_element(child))
177 if (node_name_is(child, "HIGHPART"))
178 high_part = node_get_hex_u64(child);
179 else if (node_name_is(child, "LOWPART"))
180 low_part = node_get_hex_u64(child);
182 return (u64)low_part | ((u64)high_part << 32);
185 /* Used to sort an array of struct image_infos by their image indices. */
186 static int sort_by_index(const void *p1, const void *p2)
188 int index_1 = ((const struct image_info*)p1)->index;
189 int index_2 = ((const struct image_info*)p2)->index;
190 if (index_1 < index_2)
192 else if (index_1 > index_2)
199 /* Frees memory allocated inside a struct windows_info structure. */
200 static void destroy_windows_info(struct windows_info *windows_info)
202 FREE(windows_info->product_name);
203 FREE(windows_info->edition_id);
204 FREE(windows_info->installation_type);
205 FREE(windows_info->hal);
206 FREE(windows_info->product_type);
207 FREE(windows_info->product_suite);
208 for (size_t i = 0; i < windows_info->num_languages; i++)
209 FREE(windows_info->languages[i]);
210 FREE(windows_info->languages);
211 FREE(windows_info->default_language);
212 FREE(windows_info->system_root);
215 /* Frees memory allocated inside a struct image_info structure. */
216 static void destroy_image_info(struct image_info *image_info)
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));
227 void free_wim_info(struct wim_info *info)
231 for (int i = 0; i < info->num_images; i++)
232 destroy_image_info(&info->images[i]);
239 /* Reads the information from a <VERSION> element inside the <WINDOWS> element.
241 static void xml_read_windows_version(const xmlNode *version_node,
242 struct windows_version* windows_version)
245 for_node_child(version_node, child) {
246 if (!node_is_element(child))
248 if (node_name_is(child, "MAJOR"))
249 windows_version->major = node_get_u64(child);
250 else if (node_name_is(child, "MINOR"))
251 windows_version->minor = node_get_u64(child);
252 else if (node_name_is(child, "BUILD"))
253 windows_version->build = node_get_u64(child);
254 else if (node_name_is(child, "SPBUILD"))
255 windows_version->sp_build = node_get_u64(child);
256 else if (node_name_is(child, "SPLEVEL"))
257 windows_version->sp_level = node_get_u64(child);
261 /* Reads the information from a <LANGUAGE> element inside a <WINDOWS> element.
263 static int xml_read_languages(const xmlNode *languages_node,
264 char ***languages_ret,
265 size_t *num_languages_ret,
266 char **default_language_ret)
269 size_t num_languages = 0;
273 for_node_child(languages_node, child)
274 if (node_is_element(child) && node_name_is(child, "LANGUAGE"))
277 languages = CALLOC(num_languages, sizeof(languages[0]));
279 return WIMLIB_ERR_NOMEM;
281 *languages_ret = languages;
282 *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++);
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);
345 /* Reads the information from an <IMAGE> element. */
346 static int xml_read_image_info(xmlNode *image_node,
347 struct image_info *image_info)
353 index_prop = xmlGetProp(image_node, "INDEX");
355 image_info->index = atoi(index_prop);
358 image_info->index = 1;
362 for_node_child(image_node, child) {
363 if (!node_is_element(child))
365 if (node_name_is(child, "DIRCOUNT"))
366 image_info->dir_count = node_get_u64(child);
367 else if (node_name_is(child, "FILECOUNT"))
368 image_info->file_count = node_get_u64(child);
369 else if (node_name_is(child, "TOTALBYTES"))
370 image_info->total_bytes = node_get_u64(child);
371 else if (node_name_is(child, "HARDLINKBYTES"))
372 image_info->hard_link_bytes = node_get_u64(child);
373 else if (node_name_is(child, "CREATIONTIME"))
374 image_info->creation_time = node_get_timestamp(child);
375 else if (node_name_is(child, "LASTMODIFICATIONTIME"))
376 image_info->last_modification_time = node_get_timestamp(child);
377 else if (node_name_is(child, "WINDOWS")) {
378 DEBUG("Found <WINDOWS> tag");
379 ret = xml_read_windows_info(child,
380 &image_info->windows_info);
381 image_info->windows_info_exists = true;
382 } else if (node_name_is(child, "NAME")) {
383 ret = node_get_string(child, &image_info->name);
384 } else if (node_name_is(child, "DESCRIPTION")) {
385 ret = node_get_string(child, &image_info->description);
386 } else if (node_name_is(child, "FLAGS")) {
387 ret = node_get_string(child, &image_info->flags);
388 } else if (node_name_is(child, "DISPLAYNAME")) {
389 ret = node_get_string(child, &image_info->display_name);
390 } else if (node_name_is(child, "DISPLAYDESCRIPTION")) {
391 ret = node_get_string(child, &image_info->display_description);
396 if (!image_info->name) {
398 WARNING("Image with index %d has no name", image_info->index);
399 empty_name = MALLOC(1);
401 return WIMLIB_ERR_NOMEM;
403 image_info->name = empty_name;
408 /* Reads the information from a <WIM> element, which should be the root element
409 * of the XML tree. */
410 static int xml_read_wim_info(const xmlNode *wim_node,
411 struct wim_info **wim_info_ret)
413 struct wim_info *wim_info;
419 wim_info = CALLOC(1, sizeof(struct wim_info));
421 return WIMLIB_ERR_NOMEM;
423 /* Count how many images there are. */
425 for_node_child(wim_node, child) {
426 if (node_is_element(child) && node_name_is(child, "IMAGE")) {
427 if (num_images == INT_MAX) {
428 return WIMLIB_ERR_IMAGE_COUNT;
434 if (num_images > 0) {
435 /* Allocate the array of struct image_infos and fill them in. */
436 wim_info->images = CALLOC(num_images, sizeof(wim_info->images[0]));
437 if (!wim_info->images) {
438 ret = WIMLIB_ERR_NOMEM;
441 wim_info->num_images = num_images;
443 for_node_child(wim_node, child) {
444 if (!node_is_element(child))
446 if (node_name_is(child, "IMAGE")) {
447 DEBUG("Found <IMAGE> tag");
448 ret = xml_read_image_info(child,
449 &wim_info->images[i]);
453 } else if (node_name_is(child, "TOTALBYTES")) {
454 wim_info->total_bytes = node_get_u64(child);
458 /* Sort the array of image info by image index. */
459 qsort(wim_info->images, num_images,
460 sizeof(struct image_info), sort_by_index);
462 /* Make sure the image indices make sense */
463 for (i = 0; i < num_images; i++) {
464 if (wim_info->images[i].index != i + 1) {
465 ERROR("WIM images are not indexed [1...%d] "
466 "in XML data as expected",
468 return WIMLIB_ERR_IMAGE_COUNT;
473 *wim_info_ret = wim_info;
476 free_wim_info(wim_info);
480 /* Prints the information contained in a `struct windows_info'. */
481 static void print_windows_info(const struct windows_info *windows_info)
483 const struct windows_version *windows_version;
485 printf("Architecture: %s\n", get_arch(windows_info->arch));
487 if (windows_info->product_name)
488 printf("Product Name: %s\n",
489 windows_info->product_name);
491 if (windows_info->edition_id)
492 printf("Edition ID: %s\n",
493 windows_info->edition_id);
495 if (windows_info->installation_type)
496 printf("Installation Type: %s\n",
497 windows_info->installation_type);
499 if (windows_info->hal)
503 if (windows_info->product_type)
504 printf("Product Type: %s\n",
505 windows_info->product_type);
507 if (windows_info->product_suite)
508 printf("Product Suite: %s\n",
509 windows_info->product_suite);
511 printf("Languages: ");
512 for (size_t i = 0; i < windows_info->num_languages; i++) {
513 fputs(windows_info->languages[i], stdout);
517 if (windows_info->default_language)
518 printf("Default Language: %s\n",
519 windows_info->default_language);
520 if (windows_info->system_root)
521 printf("System Root: %s\n",
522 windows_info->system_root);
523 if (windows_info->windows_version_exists) {
524 windows_version = &windows_info->windows_version;
525 printf("Major Version: %"PRIu64"\n",
526 windows_version->major);
527 printf("Minor Version: %"PRIu64"\n",
528 windows_version->minor);
529 printf("Build: %"PRIu64"\n",
530 windows_version->build);
531 printf("Service Pack Build: %"PRIu64"\n",
532 windows_version->sp_build);
533 printf("Service Pack Level: %"PRIu64"\n",
534 windows_version->sp_level);
539 /* Writes the information contained in a `struct windows_version' to the XML
540 * document being written. This is the <VERSION> element inside the <WINDOWS>
542 static int xml_write_windows_version(xmlTextWriter *writer,
543 const struct windows_version *version)
546 rc = xmlTextWriterStartElement(writer, "VERSION");
550 rc = xmlTextWriterWriteFormatElement(writer, "MAJOR", "%"PRIu64,
555 rc = xmlTextWriterWriteFormatElement(writer, "MINOR", "%"PRIu64,
560 rc = xmlTextWriterWriteFormatElement(writer, "BUILD", "%"PRIu64,
565 rc = xmlTextWriterWriteFormatElement(writer, "SPBUILD", "%"PRIu64,
570 rc = xmlTextWriterWriteFormatElement(writer, "SPLEVEL", "%"PRIu64,
575 return xmlTextWriterEndElement(writer); /* </VERSION> */
578 /* Writes the information contained in a `struct windows_info' to the XML
579 * document being written. This is the <WINDOWS> element. */
580 static int xml_write_windows_info(xmlTextWriter *writer,
581 const struct windows_info *windows_info)
584 rc = xmlTextWriterStartElement(writer, "WINDOWS");
588 rc = xmlTextWriterWriteFormatElement(writer, "ARCH", "%"PRIu64,
593 if (windows_info->product_name) {
594 rc = xmlTextWriterWriteElement(writer, "PRODUCTNAME",
595 windows_info->product_name);
600 if (windows_info->edition_id) {
601 rc = xmlTextWriterWriteElement(writer, "EDITIONID",
602 windows_info->edition_id);
607 if (windows_info->installation_type) {
608 rc = xmlTextWriterWriteElement(writer, "INSTALLATIONTYPE",
609 windows_info->installation_type);
614 if (windows_info->hal) {
615 rc = xmlTextWriterWriteElement(writer, "HAL",
621 if (windows_info->product_type) {
622 rc = xmlTextWriterWriteElement(writer, "PRODUCTTYPE",
623 windows_info->product_type);
628 if (windows_info->product_suite) {
629 rc = xmlTextWriterWriteElement(writer, "PRODUCTSUITE",
630 windows_info->product_suite);
635 if (windows_info->num_languages) {
636 rc = xmlTextWriterStartElement(writer, "LANGUAGES");
640 for (size_t i = 0; i < windows_info->num_languages; i++) {
641 rc = xmlTextWriterWriteElement(writer, "LANGUAGE",
642 windows_info->languages[i]);
646 rc = xmlTextWriterWriteElement(writer, "DEFAULT",
647 windows_info->default_language);
651 rc = xmlTextWriterEndElement(writer); /* </LANGUAGES> */
656 if (windows_info->windows_version_exists) {
657 rc = xml_write_windows_version(writer, &windows_info->windows_version);
662 if (windows_info->system_root) {
663 rc = xmlTextWriterWriteElement(writer, "SYSTEMROOT",
664 windows_info->system_root);
669 return xmlTextWriterEndElement(writer); /* </WINDOWS> */
672 /* Writes a time element to the XML document being constructed in memory. */
673 static int xml_write_time(xmlTextWriter *writer, const char *element_name,
677 rc = xmlTextWriterStartElement(writer, element_name);
681 rc = xmlTextWriterWriteFormatElement(writer, "HIGHPART",
682 "0x%08"PRIX32, (u32)(time >> 32));
686 rc = xmlTextWriterWriteFormatElement(writer, "LOWPART",
687 "0x%08"PRIX32, (u32)time);
691 rc = xmlTextWriterEndElement(writer); /* </@element_name> */
697 /* Writes an <IMAGE> element to the XML document. */
698 static int xml_write_image_info(xmlTextWriter *writer,
699 const struct image_info *image_info)
702 rc = xmlTextWriterStartElement(writer, "IMAGE");
706 rc = xmlTextWriterWriteFormatAttribute(writer, "INDEX", "%d",
711 rc = xmlTextWriterWriteFormatElement(writer, "DIRCOUNT", "%"PRIu64,
712 image_info->dir_count);
716 rc = xmlTextWriterWriteFormatElement(writer, "FILECOUNT", "%"PRIu64,
717 image_info->file_count);
721 rc = xmlTextWriterWriteFormatElement(writer, "TOTALBYTES", "%"PRIu64,
722 image_info->total_bytes);
726 rc = xmlTextWriterWriteFormatElement(writer, "HARDLINKBYTES", "%"PRIu64,
727 image_info->hard_link_bytes);
731 rc = xml_write_time(writer, "CREATIONTIME", image_info->creation_time);
735 rc = xml_write_time(writer, "LASTMODIFICATIONTIME",
736 image_info->last_modification_time);
740 if (image_info->windows_info_exists) {
741 rc = xml_write_windows_info(writer, &image_info->windows_info);
746 if (image_info->name) {
747 rc = xmlTextWriterWriteElement(writer, "NAME",
753 if (image_info->description) {
754 rc = xmlTextWriterWriteElement(writer, "DESCRIPTION",
755 image_info->description);
759 if (image_info->display_name) {
760 rc = xmlTextWriterWriteElement(writer, "DISPLAYNAME",
761 image_info->display_name);
765 if (image_info->display_description) {
766 rc = xmlTextWriterWriteElement(writer, "DISPLAYDESCRIPTION",
767 image_info->display_description);
772 if (image_info->flags) {
773 rc = xmlTextWriterWriteElement(writer, "FLAGS",
779 return xmlTextWriterEndElement(writer); /* </IMAGE> */
784 /* Makes space for another image in the XML information and return a pointer to
786 static struct image_info *add_image_info_struct(struct wim_info *wim_info)
788 struct image_info *images;
790 images = CALLOC(wim_info->num_images + 1, sizeof(struct image_info));
793 memcpy(images, wim_info->images,
794 wim_info->num_images * sizeof(struct image_info));
795 FREE(wim_info->images);
796 wim_info->images = images;
797 wim_info->num_images++;
798 return &images[wim_info->num_images - 1];
801 static int clone_windows_info(const struct windows_info *old,
802 struct windows_info *new)
804 if (old->product_name && !(new->product_name = STRDUP(old->product_name)))
805 return WIMLIB_ERR_NOMEM;
806 if (old->edition_id && !(new->edition_id = STRDUP(old->edition_id)))
807 return WIMLIB_ERR_NOMEM;
808 if (old->installation_type && !(new->installation_type =
809 STRDUP(old->installation_type)))
810 return WIMLIB_ERR_NOMEM;
811 if (old->hal && !(new->hal = STRDUP(old->hal)))
812 return WIMLIB_ERR_NOMEM;
813 if (old->product_type && !(new->product_type = STRDUP(old->product_type)))
814 return WIMLIB_ERR_NOMEM;
815 if (old->product_suite && !(new->product_suite = STRDUP(old->product_suite)))
816 return WIMLIB_ERR_NOMEM;
818 if (old->languages) {
819 new->languages = CALLOC(old->num_languages, sizeof(char*));
821 return WIMLIB_ERR_NOMEM;
822 new->num_languages = old->num_languages;
823 for (size_t i = 0; i < new->num_languages; i++) {
824 if (!old->languages[i])
826 new->languages[i] = STRDUP(old->languages[i]);
827 if (!new->languages[i])
828 return WIMLIB_ERR_NOMEM;
831 if (old->default_language &&
832 !(new->default_language = STRDUP(old->default_language)))
833 return WIMLIB_ERR_NOMEM;
834 if (old->system_root && !(new->system_root = STRDUP(old->system_root)))
835 return WIMLIB_ERR_NOMEM;
836 if (old->windows_version_exists) {
837 new->windows_version_exists = true;
838 memcpy(&new->windows_version, &old->windows_version,
839 sizeof(old->windows_version));
844 static int clone_image_info(const struct image_info *old, struct image_info *new)
846 new->dir_count = old->dir_count;
847 new->file_count = old->file_count;
848 new->total_bytes = old->total_bytes;
849 new->hard_link_bytes = old->hard_link_bytes;
850 new->creation_time = old->creation_time;
851 new->last_modification_time = old->last_modification_time;
853 if (!(new->name = STRDUP(old->name)))
854 return WIMLIB_ERR_NOMEM;
856 if (old->description)
857 if (!(new->description = STRDUP(old->description)))
858 return WIMLIB_ERR_NOMEM;
860 if (old->display_name)
861 if (!(new->display_name = STRDUP(old->display_name)))
862 return WIMLIB_ERR_NOMEM;
864 if (old->display_description)
865 if (!(new->display_description = STRDUP(old->display_description)))
866 return WIMLIB_ERR_NOMEM;
869 if (!(new->flags = STRDUP(old->flags)))
870 return WIMLIB_ERR_NOMEM;
872 if (old->windows_info_exists) {
873 new->windows_info_exists = true;
874 return clone_windows_info(&old->windows_info,
880 /* Copies the XML information for an image between WIM files.
882 * @dest_image_name and @dest_image_description are ignored if they are NULL;
883 * otherwise, they are used to override the image name and/or image description
884 * from the XML data in the source WIM file.
886 * On failure, WIMLIB_ERR_NOMEM is returned and no changes are made. Otherwise,
887 * 0 is returned and the WIM information at *new_wim_info_p is modified.
889 int xml_export_image(const struct wim_info *old_wim_info,
891 struct wim_info **new_wim_info_p,
892 const char *dest_image_name,
893 const char *dest_image_description)
895 struct wim_info *new_wim_info;
896 struct image_info *image_info;
899 DEBUG("Copying XML data between WIM files for source image %d.", image);
901 wimlib_assert(old_wim_info != NULL);
902 wimlib_assert(image >= 1 && image <= old_wim_info->num_images);
904 if (*new_wim_info_p) {
905 new_wim_info = *new_wim_info_p;
907 new_wim_info = CALLOC(1, sizeof(struct wim_info));
912 image_info = add_image_info_struct(new_wim_info);
916 ret = clone_image_info(&old_wim_info->images[image - 1], image_info);
918 goto err_destroy_image_info;
920 image_info->index = new_wim_info->num_images;
922 if (dest_image_name) {
923 FREE(image_info->name);
924 image_info->name = STRDUP(dest_image_name);
925 if (!image_info->name)
926 goto err_destroy_image_info;
928 if (dest_image_description) {
929 FREE(image_info->description);
930 image_info->description = STRDUP(dest_image_description);
931 if (!image_info->description)
932 goto err_destroy_image_info;
934 *new_wim_info_p = new_wim_info;
936 err_destroy_image_info:
937 destroy_image_info(image_info);
939 if (new_wim_info != *new_wim_info_p)
940 free_wim_info(new_wim_info);
941 return WIMLIB_ERR_NOMEM;
944 /* Removes an image from the XML information. */
945 void xml_delete_image(struct wim_info **wim_info_p, int image)
947 struct wim_info *wim_info;
949 DEBUG("Deleting image %d from the XML data.", image);
951 wim_info = *wim_info_p;
953 destroy_image_info(&wim_info->images[image - 1]);
955 memcpy(&wim_info->images[image - 1],
956 &wim_info->images[image],
957 (wim_info->num_images - image) * sizeof(struct image_info));
959 if (--wim_info->num_images == 0) {
960 free_wim_info(wim_info);
963 for (int i = image - 1; i < wim_info->num_images; i++)
964 wim_info->images[i].index--;
968 size_t xml_get_max_image_name_len(const WIMStruct *w)
973 for (int i = 0; i < w->wim_info->num_images; i++) {
974 len = strlen(w->wim_info->images[i].name);
982 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
983 void xml_set_memory_allocator(void *(*malloc_func)(size_t),
984 void (*free_func)(void *),
985 void *(*realloc_func)(void *, size_t))
987 xmlMemSetup(free_func, malloc_func, realloc_func, STRDUP);
991 static int calculate_dentry_statistics(struct dentry *dentry, void *arg)
993 struct image_info *info = arg;
994 struct lookup_table *lookup_table = info->lookup_table;
995 const struct inode *inode = dentry->d_inode;
996 struct lookup_table_entry *lte;
998 /* Update directory count and file count.
1000 * Each dentry counts as either a file or a directory, but not both.
1001 * The root directory is an exception: it is not counted at all.
1003 * Symbolic links and junction points (and presumably other reparse
1004 * points) count as regular files. This is despite the fact that
1005 * junction points have FILE_ATTRIBUTE_DIRECTORY set.
1007 if (dentry_is_root(dentry))
1010 if (inode_is_directory(inode))
1016 * Update total bytes and hard link bytes.
1018 * Unfortunately there are some inconsistencies/bugs in the way this is
1021 * If there are no alternate data streams in the image, the "total
1022 * bytes" is the sum of the size of the un-named data stream of each
1023 * inode times the link count of that inode. In other words, it would
1024 * be the total number of bytes of regular files you would have if you
1025 * extracted the full image without any hard-links. The "hard link
1026 * bytes" is equal to the "total bytes" minus the size of the un-named
1027 * data stream of each inode. In other words, the "hard link bytes"
1028 * counts the size of the un-named data stream for all the links to each
1029 * inode except the first one.
1031 * Reparse points and directories don't seem to be counted in either the
1032 * total bytes or the hard link bytes.
1034 * And now we get to the most confusing part, the alternate data
1035 * streams. They are not counted in the "total bytes". However, if the
1036 * link count of an inode with alternate data streams is 2 or greater,
1037 * the size of all the alternate data streams is included in the "hard
1038 * link bytes", and this size is multiplied by the link count (NOT one
1039 * less than the link count).
1041 lte = inode_unnamed_lte(inode, info->lookup_table);
1043 info->total_bytes += wim_resource_size(lte);
1044 if (!dentry_is_first_in_inode(dentry))
1045 info->hard_link_bytes += wim_resource_size(lte);
1048 if (inode->link_count >= 2 && dentry_is_first_in_inode(dentry)) {
1049 for (unsigned i = 0; i < inode->num_ads; i++) {
1050 if (inode->ads_entries[i].stream_name_len) {
1051 lte = inode_stream_lte(inode, i + 1, lookup_table);
1053 info->hard_link_bytes += inode->link_count *
1054 wim_resource_size(lte);
1063 * Calculate what to put in the <FILECOUNT>, <DIRCOUNT>, <TOTALBYTES>, and
1064 * <HARDLINKBYTES> elements of each <IMAGE>.
1066 * Please note there is no official documentation for exactly how this is done.
1067 * But, see calculate_dentry_statistics().
1069 void xml_update_image_info(WIMStruct *w, int image)
1071 struct image_info *image_info;
1074 DEBUG("Updating the image info for image %d", image);
1076 image_info = &w->wim_info->images[image - 1];
1078 image_info->file_count = 0;
1079 image_info->dir_count = 0;
1080 image_info->total_bytes = 0;
1081 image_info->hard_link_bytes = 0;
1083 flags_save = image_info->flags;
1084 image_info->lookup_table = w->lookup_table;
1085 for_dentry_in_tree(w->image_metadata[image - 1].root_dentry,
1086 calculate_dentry_statistics,
1088 image_info->flags = flags_save;
1089 image_info->last_modification_time = get_wim_timestamp();
1092 /* Adds an image to the XML information. */
1093 int xml_add_image(WIMStruct *w, const char *name)
1095 struct wim_info *wim_info;
1096 struct image_info *image_info;
1098 wimlib_assert(name != NULL);
1100 /* If this is the first image, allocate the struct wim_info. Otherwise
1101 * use the existing struct wim_info. */
1103 wim_info = w->wim_info;
1105 wim_info = CALLOC(1, sizeof(struct wim_info));
1107 return WIMLIB_ERR_NOMEM;
1110 image_info = add_image_info_struct(wim_info);
1112 goto out_free_wim_info;
1114 if (!(image_info->name = STRDUP(name)))
1115 goto out_destroy_image_info;
1117 w->wim_info = wim_info;
1118 image_info->index = wim_info->num_images;
1119 image_info->creation_time = get_wim_timestamp();
1120 xml_update_image_info(w, image_info->index);
1123 out_destroy_image_info:
1124 destroy_image_info(image_info);
1125 wim_info->num_images--;
1127 if (wim_info != w->wim_info)
1129 return WIMLIB_ERR_NOMEM;
1132 /* Prints information about the specified image from struct wim_info structure.
1134 void print_image_info(const struct wim_info *wim_info, int image)
1136 const struct image_info *image_info;
1140 wimlib_assert(image >= 1 && image <= wim_info->num_images);
1142 image_info = &wim_info->images[image - 1];
1144 printf("Index: %d\n", image_info->index);
1145 printf("Name: %s\n", image_info->name);
1147 /* Always print the Description: part even if there is no
1149 if (image_info->description)
1150 desc = image_info->description;
1153 printf("Description: %s\n", desc);
1155 if (image_info->display_name)
1156 printf("Display Name: %s\n",
1157 image_info->display_name);
1159 if (image_info->display_description)
1160 printf("Display Description: %s\n",
1161 image_info->display_description);
1163 printf("Directory Count: %"PRIu64"\n", image_info->dir_count);
1164 printf("File Count: %"PRIu64"\n", image_info->file_count);
1165 printf("Total Bytes: %"PRIu64"\n", image_info->total_bytes);
1166 printf("Hard Link Bytes: %"PRIu64"\n", image_info->hard_link_bytes);
1168 wim_timestamp_to_str(image_info->creation_time, buf, sizeof(buf));
1169 printf("Creation Time: %s\n", buf);
1171 wim_timestamp_to_str(image_info->creation_time, buf, sizeof(buf));
1172 printf("Last Modification Time: %s\n", buf);
1173 if (image_info->windows_info_exists)
1174 print_windows_info(&image_info->windows_info);
1175 if (image_info->flags)
1176 printf("Flags: %s\n", image_info->flags);
1181 * Reads the XML data from a WIM file.
1183 int read_xml_data(FILE *fp, const struct resource_entry *res_entry,
1184 u8 **xml_data_ret, struct wim_info **info_ret)
1191 DEBUG("XML data is %"PRIu64" bytes at offset %"PRIu64"",
1192 (u64)res_entry->size, res_entry->offset);
1194 if (resource_is_compressed(res_entry)) {
1195 ERROR("XML data is supposed to be uncompressed");
1196 ret = WIMLIB_ERR_XML;
1197 goto out_cleanup_parser;
1200 if (res_entry->size < 2) {
1201 ERROR("XML data must be at least 2 bytes long");
1202 ret = WIMLIB_ERR_XML;
1203 goto out_cleanup_parser;
1206 xml_data = MALLOC(res_entry->size + 2);
1208 ret = WIMLIB_ERR_NOMEM;
1209 goto out_cleanup_parser;
1212 ret = read_uncompressed_resource(fp, res_entry->offset,
1213 res_entry->size, xml_data);
1215 goto out_free_xml_data;
1217 /* Null-terminate just in case */
1218 xml_data[res_entry->size] = 0;
1219 xml_data[res_entry->size + 1] = 0;
1221 DEBUG("Parsing XML using libxml2 to create XML tree");
1223 doc = xmlReadMemory(xml_data, res_entry->size,
1224 "noname.xml", "UTF-16", 0);
1227 ERROR("Failed to parse XML data");
1228 ret = WIMLIB_ERR_XML;
1229 goto out_free_xml_data;
1232 DEBUG("Constructing WIM information structure from XML tree.");
1234 root = xmlDocGetRootElement(doc);
1236 ERROR("WIM XML data is an empty XML document");
1237 ret = WIMLIB_ERR_XML;
1241 if (!node_is_element(root) || !node_name_is(root, "WIM")) {
1242 ERROR("Expected <WIM> for the root XML element (found <%s>)",
1244 ret = WIMLIB_ERR_XML;
1248 ret = xml_read_wim_info(root, info_ret);
1252 DEBUG("Freeing XML tree.");
1254 *xml_data_ret = xml_data;
1265 #define CHECK_RET ({ if (ret < 0) { \
1266 ERROR("Error writing XML data"); \
1267 ret = WIMLIB_ERR_WRITE; \
1268 goto out_free_text_writer; \
1272 * Writes XML data to a WIM file.
1274 * If @total_bytes is non-zero, it specifies what to write to the TOTALBYTES
1275 * element in the XML data. If zero, TOTALBYTES is given the default value of
1276 * the offset of the XML data.
1278 int write_xml_data(const struct wim_info *wim_info, int image, FILE *out,
1279 u64 total_bytes, struct resource_entry *out_res_entry)
1281 xmlCharEncodingHandler *encoding_handler;
1282 xmlOutputBuffer *out_buffer;
1283 xmlTextWriter *writer;
1288 wimlib_assert(image == WIMLIB_ALL_IMAGES ||
1289 (wim_info != NULL && image >= 1 &&
1290 image <= wim_info->num_images));
1292 start_offset = ftello(out);
1293 if (start_offset == -1)
1294 return WIMLIB_ERR_WRITE;
1296 DEBUG("Writing XML data for image %d at offset %"PRIu64,
1297 image, start_offset);
1299 /* 2 bytes endianness marker for UTF-16LE. This is _required_ for WIM
1301 if ((putc(0xff, out)) == EOF || (putc(0xfe, out) == EOF)) {
1302 ERROR_WITH_ERRNO("Error writing XML data");
1303 return WIMLIB_ERR_WRITE;
1306 /* The contents of the <TOTALBYTES> element in the XML data, under the
1307 * <WIM> element (not the <IMAGE> element), is for non-split WIMs the
1308 * size of the WIM file excluding the XML data and integrity table.
1309 * This should be equal to the current position in the output stream,
1310 * since the XML data and integrity table are the last elements of the
1313 * For split WIMs, <TOTALBYTES> takes into account the entire WIM, not
1314 * just the current part. In that case, @total_bytes should be passed
1315 * in to this function. */
1316 if (total_bytes == 0)
1317 total_bytes = start_offset;
1319 xmlInitCharEncodingHandlers();
1321 /* The encoding of the XML data must be UTF-16LE. */
1322 encoding_handler = xmlGetCharEncodingHandler(XML_CHAR_ENCODING_UTF16LE);
1323 if (!encoding_handler) {
1324 ERROR("Failed to get XML character encoding handler for UTF-16LE");
1325 ret = WIMLIB_ERR_LIBXML_UTF16_HANDLER_NOT_AVAILABLE;
1326 goto out_cleanup_char_encoding_handlers;
1329 out_buffer = xmlOutputBufferCreateFile(out, encoding_handler);
1331 ERROR("Failed to allocate xmlOutputBuffer");
1332 ret = WIMLIB_ERR_NOMEM;
1333 goto out_cleanup_char_encoding_handlers;
1336 writer = xmlNewTextWriter(out_buffer);
1338 ERROR("Failed to allocate xmlTextWriter");
1339 ret = WIMLIB_ERR_NOMEM;
1340 goto out_output_buffer_close;
1343 DEBUG("Writing <WIM> element");
1345 ret = xmlTextWriterStartElement(writer, "WIM");
1348 ret = xmlTextWriterWriteFormatElement(writer, "TOTALBYTES", "%"PRIu64,
1352 if (wim_info != NULL) {
1354 if (image == WIMLIB_ALL_IMAGES) {
1356 last = wim_info->num_images;
1361 DEBUG("Writing %d <IMAGE> elements", last - first + 1);
1362 for (int i = first; i <= last; i++) {
1363 ret = xml_write_image_info(writer, &wim_info->images[i - 1]);
1368 ret = xmlTextWriterEndElement(writer);
1371 ret = xmlTextWriterEndDocument(writer);
1374 DEBUG("Ended XML document");
1376 /* Call xmlFreeTextWriter() before ftello() because the former will
1377 * flush the file stream. */
1378 xmlFreeTextWriter(writer);
1381 end_offset = ftello(out);
1382 if (end_offset == -1) {
1383 ret = WIMLIB_ERR_WRITE;
1386 out_res_entry->offset = start_offset;
1387 out_res_entry->size = end_offset - start_offset;
1388 out_res_entry->original_size = end_offset - start_offset;
1389 out_res_entry->flags = WIM_RESHDR_FLAG_METADATA;
1391 out_free_text_writer:
1392 /* xmlFreeTextWriter will free the attached xmlOutputBuffer. */
1393 xmlFreeTextWriter(writer);
1395 out_output_buffer_close:
1396 if (out_buffer != NULL)
1397 xmlOutputBufferClose(out_buffer);
1398 out_cleanup_char_encoding_handlers:
1399 xmlCleanupCharEncodingHandlers();
1402 DEBUG("Successfully wrote XML data");
1406 /* Returns the name of the specified image. */
1407 WIMLIBAPI const char *wimlib_get_image_name(const WIMStruct *w, int image)
1409 if (image < 1 || image > w->hdr.image_count)
1411 return w->wim_info->images[image - 1].name;
1414 /* Returns the description of the specified image. */
1415 WIMLIBAPI const char *wimlib_get_image_description(const WIMStruct *w,
1418 if (image < 1 || image > w->hdr.image_count)
1420 return w->wim_info->images[image - 1].description;
1423 /* Determines if an image name is already used by some image in the WIM. */
1424 WIMLIBAPI bool wimlib_image_name_in_use(const WIMStruct *w, const char *name)
1426 if (!name || !*name)
1428 for (int i = 1; i <= w->hdr.image_count; i++)
1429 if (strcmp(w->wim_info->images[i - 1].name, name) == 0)
1434 /* Extracts the raw XML data to a file stream. */
1435 WIMLIBAPI int wimlib_extract_xml_data(WIMStruct *w, FILE *fp)
1438 return WIMLIB_ERR_INVALID_PARAM;
1439 if (fwrite(w->xml_data, 1, w->hdr.xml_res_entry.size, fp) !=
1440 w->hdr.xml_res_entry.size) {
1441 ERROR_WITH_ERRNO("Failed to extract XML data");
1442 return WIMLIB_ERR_WRITE;
1447 /* Sets the name of an image in the WIM. */
1448 WIMLIBAPI int wimlib_set_image_name(WIMStruct *w, int image, const char *name)
1453 DEBUG("Setting the name of image %d to %s", image, name);
1455 if (!name || !*name) {
1456 ERROR("Must specify a non-empty string for the image name");
1457 return WIMLIB_ERR_INVALID_PARAM;
1460 if (image < 1 || image > w->hdr.image_count) {
1461 ERROR("%d is not a valid image", image);
1462 return WIMLIB_ERR_INVALID_IMAGE;
1465 for (i = 1; i <= w->hdr.image_count; i++) {
1468 if (strcmp(w->wim_info->images[i - 1].name, name) == 0) {
1469 ERROR("The name `%s' is already used for image %d",
1471 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
1477 return WIMLIB_ERR_NOMEM;
1479 FREE(w->wim_info->images[image - 1].name);
1480 w->wim_info->images[image - 1].name = p;
1484 /* Sets the description of an image in the WIM. */
1485 WIMLIBAPI int wimlib_set_image_descripton(WIMStruct *w, int image,
1486 const char *description)
1490 if (image < 1 || image > w->hdr.image_count) {
1491 ERROR("%d is not a valid image", image);
1492 return WIMLIB_ERR_INVALID_IMAGE;
1495 p = STRDUP(description);
1497 return WIMLIB_ERR_NOMEM;
1501 FREE(w->wim_info->images[image - 1].description);
1502 w->wim_info->images[image - 1].description = p;
1506 /* Set the <FLAGS> element of a WIM image */
1507 WIMLIBAPI int wimlib_set_image_flags(WIMStruct *w, int image,
1512 if (image < 1 || image > w->hdr.image_count) {
1513 ERROR("%d is not a valid image", image);
1514 return WIMLIB_ERR_INVALID_IMAGE;
1519 return WIMLIB_ERR_NOMEM;
1523 FREE(w->wim_info->images[image - 1].flags);
1524 w->wim_info->images[image - 1].flags = p;