4 * Deals with the XML information in WIM files. Uses the C library libxml2.
8 * Copyright (C) 2012, 2013 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/.
30 #include "wimlib/dentry.h"
31 #include "wimlib/encoding.h"
32 #include "wimlib/error.h"
33 #include "wimlib/file_io.h"
34 #include "wimlib/lookup_table.h"
35 #include "wimlib/metadata.h"
36 #include "wimlib/resource.h"
37 #include "wimlib/timestamp.h"
38 #include "wimlib/xml.h"
40 #include <libxml/encoding.h>
41 #include <libxml/parser.h>
42 #include <libxml/tree.h>
43 #include <libxml/xmlwriter.h>
48 /* Structures used to form an in-memory representation of the XML data (other
49 * than the raw parse tree from libxml). */
51 struct windows_version {
63 tchar *installation_type;
68 tchar *default_language;
71 bool windows_version_exists;
72 struct windows_version windows_version;
77 bool windows_info_exists;
83 u64 last_modification_time;
84 struct windows_info windows_info;
88 tchar *display_description;
90 struct wim_lookup_table *lookup_table; /* temporary field */
93 /* A struct wim_info structure corresponds to the entire XML data for a WIM file. */
97 /* Array of `struct image_info's, one for each image in the WIM that is
98 * mentioned in the XML data. */
99 struct image_info *images;
102 struct xml_string_spec {
107 #define ELEM(STRING_NAME, MEMBER_NAME) \
108 {STRING_NAME, offsetof(struct image_info, MEMBER_NAME)}
109 static const struct xml_string_spec
110 image_info_xml_string_specs[] = {
112 ELEM("DESCRIPTION", description),
113 ELEM("DISPLAYNAME", display_name),
114 ELEM("DISPLAYDESCRIPTION", display_description),
115 ELEM("FLAGS", flags),
119 #define ELEM(STRING_NAME, MEMBER_NAME) \
120 {STRING_NAME, offsetof(struct windows_info, MEMBER_NAME)}
121 static const struct xml_string_spec
122 windows_info_xml_string_specs[] = {
123 ELEM("PRODUCTNAME", product_name),
124 ELEM("EDITIONID", edition_id),
125 ELEM("INSTALLATIONTYPE", installation_type),
127 ELEM("PRODUCTTYPE", product_type),
128 ELEM("PRODUCTSUITE", product_suite),
133 wim_info_get_total_bytes(const struct wim_info *info)
137 return info->total_bytes;
141 wim_info_get_image_total_bytes(const struct wim_info *info, int image)
143 return info->images[image - 1].total_bytes;
147 wim_info_get_num_images(const struct wim_info *info)
149 return info->num_images;
152 /* Returns a statically allocated string that is a string representation of the
153 * architecture number. */
164 /* XXX Are there other arch values? */
171 /* Iterate through the children of an xmlNode. */
172 #define for_node_child(parent, child) \
173 for (child = parent->children; child != NULL; child = child->next)
175 /* Utility functions for xmlNodes */
177 node_is_element(xmlNode *node)
179 return node->type == XML_ELEMENT_NODE;
183 node_is_text(xmlNode *node)
185 return node->type == XML_TEXT_NODE;
189 node_name_is(xmlNode *node, const char *name)
191 /* For now, both upper case and lower case element names are accepted. */
192 return strcasecmp((const char *)node->name, name) == 0;
196 node_get_number(const xmlNode *u64_node, int base)
199 for_node_child(u64_node, child)
200 if (node_is_text(child))
201 return strtoull(child->content, NULL, base);
205 /* Finds the text node that is a child of an element node and returns its
206 * content converted to a 64-bit unsigned integer. Returns 0 if no text node is
209 node_get_u64(const xmlNode *u64_node)
211 return node_get_number(u64_node, 10);
214 /* Like node_get_u64(), but expects a number in base 16. */
216 node_get_hex_u64(const xmlNode *u64_node)
218 return node_get_number(u64_node, 16);
222 node_get_string(const xmlNode *string_node, tchar **tstr_ret)
228 for_node_child(string_node, child) {
229 if (node_is_text(child) && child->content) {
230 ret = utf8_to_tstr_simple(child->content, &tstr);
240 /* Returns the timestamp from a time node. It has child elements <HIGHPART> and
241 * <LOWPART> that are then used to construct a 64-bit timestamp. */
243 node_get_timestamp(const xmlNode *time_node)
248 for_node_child(time_node, child) {
249 if (!node_is_element(child))
251 if (node_name_is(child, "HIGHPART"))
252 high_part = node_get_hex_u64(child);
253 else if (node_name_is(child, "LOWPART"))
254 low_part = node_get_hex_u64(child);
256 return (u64)low_part | ((u64)high_part << 32);
259 /* Used to sort an array of struct image_infos by their image indices. */
261 sort_by_index(const void *p1, const void *p2)
263 int index_1 = ((const struct image_info*)p1)->index;
264 int index_2 = ((const struct image_info*)p2)->index;
265 if (index_1 < index_2)
267 else if (index_1 > index_2)
274 /* Frees memory allocated inside a struct windows_info structure. */
276 destroy_windows_info(struct windows_info *windows_info)
278 FREE(windows_info->product_name);
279 FREE(windows_info->edition_id);
280 FREE(windows_info->installation_type);
281 FREE(windows_info->hal);
282 FREE(windows_info->product_type);
283 FREE(windows_info->product_suite);
284 for (size_t i = 0; i < windows_info->num_languages; i++)
285 FREE(windows_info->languages[i]);
286 FREE(windows_info->languages);
287 FREE(windows_info->default_language);
288 FREE(windows_info->system_root);
291 /* Frees memory allocated inside a struct image_info structure. */
293 destroy_image_info(struct image_info *image_info)
295 FREE(image_info->name);
296 FREE(image_info->description);
297 FREE(image_info->flags);
298 FREE(image_info->display_name);
299 FREE(image_info->display_description);
300 destroy_windows_info(&image_info->windows_info);
301 memset(image_info, 0, sizeof(struct image_info));
305 free_wim_info(struct wim_info *info)
309 for (int i = 0; i < info->num_images; i++)
310 destroy_image_info(&info->images[i]);
317 /* Reads the information from a <VERSION> element inside the <WINDOWS> element.
320 xml_read_windows_version(const xmlNode *version_node,
321 struct windows_version* windows_version)
324 for_node_child(version_node, child) {
325 if (!node_is_element(child))
327 if (node_name_is(child, "MAJOR"))
328 windows_version->major = node_get_u64(child);
329 else if (node_name_is(child, "MINOR"))
330 windows_version->minor = node_get_u64(child);
331 else if (node_name_is(child, "BUILD"))
332 windows_version->build = node_get_u64(child);
333 else if (node_name_is(child, "SPBUILD"))
334 windows_version->sp_build = node_get_u64(child);
335 else if (node_name_is(child, "SPLEVEL"))
336 windows_version->sp_level = node_get_u64(child);
340 /* Reads the information from a <LANGUAGE> element inside a <WINDOWS> element.
343 xml_read_languages(const xmlNode *languages_node,
344 tchar ***languages_ret,
345 size_t *num_languages_ret,
346 tchar **default_language_ret)
349 size_t num_languages = 0;
353 for_node_child(languages_node, child)
354 if (node_is_element(child) && node_name_is(child, "LANGUAGE"))
357 languages = CALLOC(num_languages, sizeof(languages[0]));
359 return WIMLIB_ERR_NOMEM;
361 *languages_ret = languages;
362 *num_languages_ret = num_languages;
365 for_node_child(languages_node, child) {
366 if (!node_is_element(child))
368 if (node_name_is(child, "LANGUAGE"))
369 ret = node_get_string(child, languages++);
370 else if (node_name_is(child, "DEFAULT"))
371 ret = node_get_string(child, default_language_ret);
378 /* Reads the information from a <WINDOWS> element inside an <IMAGE> element. */
380 xml_read_windows_info(const xmlNode *windows_node,
381 struct windows_info *windows_info)
386 for_node_child(windows_node, child) {
387 if (!node_is_element(child))
389 if (node_name_is(child, "ARCH")) {
390 windows_info->arch = node_get_u64(child);
391 } else if (node_name_is(child, "PRODUCTNAME")) {
392 ret = node_get_string(child,
393 &windows_info->product_name);
394 } else if (node_name_is(child, "EDITIONID")) {
395 ret = node_get_string(child,
396 &windows_info->edition_id);
397 } else if (node_name_is(child, "INSTALLATIONTYPE")) {
398 ret = node_get_string(child,
399 &windows_info->installation_type);
400 } else if (node_name_is(child, "PRODUCTTYPE")) {
401 ret = node_get_string(child,
402 &windows_info->product_type);
403 } else if (node_name_is(child, "PRODUCTSUITE")) {
404 ret = node_get_string(child,
405 &windows_info->product_suite);
406 } else if (node_name_is(child, "LANGUAGES")) {
407 ret = xml_read_languages(child,
408 &windows_info->languages,
409 &windows_info->num_languages,
410 &windows_info->default_language);
411 } else if (node_name_is(child, "VERSION")) {
412 xml_read_windows_version(child,
413 &windows_info->windows_version);
414 windows_info->windows_version_exists = true;
415 } else if (node_name_is(child, "SYSTEMROOT")) {
416 ret = node_get_string(child, &windows_info->system_root);
417 } else if (node_name_is(child, "HAL")) {
418 ret = node_get_string(child, &windows_info->hal);
426 /* Reads the information from an <IMAGE> element. */
428 xml_read_image_info(xmlNode *image_node, struct image_info *image_info)
434 index_prop = xmlGetProp(image_node, "INDEX");
436 image_info->index = atoi(index_prop);
439 image_info->index = 1;
443 for_node_child(image_node, child) {
444 if (!node_is_element(child))
446 if (node_name_is(child, "DIRCOUNT"))
447 image_info->dir_count = node_get_u64(child);
448 else if (node_name_is(child, "FILECOUNT"))
449 image_info->file_count = node_get_u64(child);
450 else if (node_name_is(child, "TOTALBYTES"))
451 image_info->total_bytes = node_get_u64(child);
452 else if (node_name_is(child, "HARDLINKBYTES"))
453 image_info->hard_link_bytes = node_get_u64(child);
454 else if (node_name_is(child, "CREATIONTIME"))
455 image_info->creation_time = node_get_timestamp(child);
456 else if (node_name_is(child, "LASTMODIFICATIONTIME"))
457 image_info->last_modification_time = node_get_timestamp(child);
458 else if (node_name_is(child, "WINDOWS")) {
459 DEBUG("Found <WINDOWS> tag");
460 ret = xml_read_windows_info(child,
461 &image_info->windows_info);
462 image_info->windows_info_exists = true;
463 } else if (node_name_is(child, "NAME")) {
464 ret = node_get_string(child, &image_info->name);
465 } else if (node_name_is(child, "DESCRIPTION")) {
466 ret = node_get_string(child, &image_info->description);
467 } else if (node_name_is(child, "FLAGS")) {
468 ret = node_get_string(child, &image_info->flags);
469 } else if (node_name_is(child, "DISPLAYNAME")) {
470 ret = node_get_string(child, &image_info->display_name);
471 } else if (node_name_is(child, "DISPLAYDESCRIPTION")) {
472 ret = node_get_string(child, &image_info->display_description);
477 if (!image_info->name) {
479 /*WARNING("Image with index %d has no name", image_info->index);*/
480 empty_name = MALLOC(sizeof(tchar));
482 return WIMLIB_ERR_NOMEM;
483 *empty_name = T('\0');
484 image_info->name = empty_name;
489 /* Reads the information from a <WIM> element, which should be the root element
490 * of the XML tree. */
492 xml_read_wim_info(const xmlNode *wim_node, struct wim_info **wim_info_ret)
494 struct wim_info *wim_info;
500 wim_info = CALLOC(1, sizeof(struct wim_info));
502 return WIMLIB_ERR_NOMEM;
504 /* Count how many images there are. */
506 for_node_child(wim_node, child) {
507 if (node_is_element(child) && node_name_is(child, "IMAGE")) {
508 if (num_images == INT_MAX) {
509 return WIMLIB_ERR_IMAGE_COUNT;
515 if (num_images > 0) {
516 /* Allocate the array of struct image_infos and fill them in. */
517 wim_info->images = CALLOC(num_images, sizeof(wim_info->images[0]));
518 if (!wim_info->images) {
519 ret = WIMLIB_ERR_NOMEM;
522 wim_info->num_images = num_images;
524 for_node_child(wim_node, child) {
525 if (!node_is_element(child))
527 if (node_name_is(child, "IMAGE")) {
528 DEBUG("Found <IMAGE> tag");
529 ret = xml_read_image_info(child,
530 &wim_info->images[i]);
534 } else if (node_name_is(child, "TOTALBYTES")) {
535 wim_info->total_bytes = node_get_u64(child);
539 /* Sort the array of image info by image index. */
540 qsort(wim_info->images, num_images,
541 sizeof(struct image_info), sort_by_index);
543 /* Make sure the image indices make sense */
544 for (i = 0; i < num_images; i++) {
545 if (wim_info->images[i].index != i + 1) {
546 ERROR("WIM images are not indexed [1...%d] "
547 "in XML data as expected",
549 return WIMLIB_ERR_IMAGE_COUNT;
554 *wim_info_ret = wim_info;
557 free_wim_info(wim_info);
561 /* Prints the information contained in a `struct windows_info'.
563 * Warning: any strings printed here are in UTF-8 encoding. If the locale
564 * character encoding is not UTF-8, the printed strings may be garbled. */
566 print_windows_info(const struct windows_info *windows_info)
568 const struct windows_version *windows_version;
570 tprintf(T("Architecture: %"TS"\n"),
571 get_arch(windows_info->arch));
573 if (windows_info->product_name) {
574 tprintf(T("Product Name: %"TS"\n"),
575 windows_info->product_name);
578 if (windows_info->edition_id) {
579 tprintf(T("Edition ID: %"TS"\n"),
580 windows_info->edition_id);
583 if (windows_info->installation_type) {
584 tprintf(T("Installation Type: %"TS"\n"),
585 windows_info->installation_type);
588 if (windows_info->hal) {
589 tprintf(T("HAL: %"TS"\n"),
593 if (windows_info->product_type) {
594 tprintf(T("Product Type: %"TS"\n"),
595 windows_info->product_type);
598 if (windows_info->product_suite) {
599 tprintf(T("Product Suite: %"TS"\n"),
600 windows_info->product_suite);
603 tprintf(T("Languages: "));
604 for (size_t i = 0; i < windows_info->num_languages; i++) {
606 tfputs(windows_info->languages[i], stdout);
610 if (windows_info->default_language) {
611 tprintf(T("Default Language: %"TS"\n"),
612 windows_info->default_language);
614 if (windows_info->system_root) {
615 tprintf(T("System Root: %"TS"\n"),
616 windows_info->system_root);
619 if (windows_info->windows_version_exists) {
620 windows_version = &windows_info->windows_version;
621 tprintf(T("Major Version: %"PRIu64"\n"),
622 windows_version->major);
623 tprintf(T("Minor Version: %"PRIu64"\n"),
624 windows_version->minor);
625 tprintf(T("Build: %"PRIu64"\n"),
626 windows_version->build);
627 tprintf(T("Service Pack Build: %"PRIu64"\n"),
628 windows_version->sp_build);
629 tprintf(T("Service Pack Level: %"PRIu64"\n"),
630 windows_version->sp_level);
635 xml_write_string(xmlTextWriter *writer, const char *name,
640 int rc = tstr_to_utf8_simple(tstr, &utf8_str);
643 rc = xmlTextWriterWriteElement(writer, name, utf8_str);
652 xml_write_strings_from_specs(xmlTextWriter *writer,
653 const void *struct_with_strings,
654 const struct xml_string_spec specs[],
657 for (size_t i = 0; i < num_specs; i++) {
658 int rc = xml_write_string(writer, specs[i].name,
659 *(const tchar * const *)
660 (struct_with_strings + specs[i].offset));
668 dup_strings_from_specs(const void *old_struct_with_strings,
669 void *new_struct_with_strings,
670 const struct xml_string_spec specs[],
673 for (size_t i = 0; i < num_specs; i++) {
674 const tchar *old_str = *(const tchar * const *)
675 ((const void*)old_struct_with_strings + specs[i].offset);
676 tchar **new_str_p = (tchar **)((void*)new_struct_with_strings + specs[i].offset);
678 *new_str_p = TSTRDUP(old_str);
680 return WIMLIB_ERR_NOMEM;
686 /* Writes the information contained in a `struct windows_version' to the XML
687 * document being written. This is the <VERSION> element inside the <WINDOWS>
690 xml_write_windows_version(xmlTextWriter *writer,
691 const struct windows_version *version)
694 rc = xmlTextWriterStartElement(writer, "VERSION");
698 rc = xmlTextWriterWriteFormatElement(writer, "MAJOR", "%"PRIu64,
703 rc = xmlTextWriterWriteFormatElement(writer, "MINOR", "%"PRIu64,
708 rc = xmlTextWriterWriteFormatElement(writer, "BUILD", "%"PRIu64,
713 rc = xmlTextWriterWriteFormatElement(writer, "SPBUILD", "%"PRIu64,
718 rc = xmlTextWriterWriteFormatElement(writer, "SPLEVEL", "%"PRIu64,
723 return xmlTextWriterEndElement(writer); /* </VERSION> */
726 /* Writes the information contained in a `struct windows_info' to the XML
727 * document being written. This is the <WINDOWS> element. */
729 xml_write_windows_info(xmlTextWriter *writer,
730 const struct windows_info *windows_info)
733 rc = xmlTextWriterStartElement(writer, "WINDOWS");
737 rc = xmlTextWriterWriteFormatElement(writer, "ARCH", "%"PRIu64,
742 rc = xml_write_strings_from_specs(writer,
744 windows_info_xml_string_specs,
745 ARRAY_LEN(windows_info_xml_string_specs));
749 if (windows_info->num_languages) {
750 rc = xmlTextWriterStartElement(writer, "LANGUAGES");
754 for (size_t i = 0; i < windows_info->num_languages; i++) {
755 rc = xml_write_string(writer, "LANGUAGE",
756 windows_info->languages[i]);
761 rc = xml_write_string(writer, "DEFAULT",
762 windows_info->default_language);
766 rc = xmlTextWriterEndElement(writer); /* </LANGUAGES> */
771 if (windows_info->windows_version_exists) {
772 rc = xml_write_windows_version(writer, &windows_info->windows_version);
777 rc = xml_write_string(writer, "SYSTEMROOT",
778 windows_info->system_root);
782 return xmlTextWriterEndElement(writer); /* </WINDOWS> */
785 /* Writes a time element to the XML document being constructed in memory. */
787 xml_write_time(xmlTextWriter *writer, const char *element_name, u64 time)
790 rc = xmlTextWriterStartElement(writer, element_name);
794 rc = xmlTextWriterWriteFormatElement(writer, "HIGHPART",
795 "0x%08"PRIX32, (u32)(time >> 32));
799 rc = xmlTextWriterWriteFormatElement(writer, "LOWPART",
800 "0x%08"PRIX32, (u32)time);
804 rc = xmlTextWriterEndElement(writer); /* </@element_name> */
810 /* Writes an <IMAGE> element to the XML document. */
812 xml_write_image_info(xmlTextWriter *writer, const struct image_info *image_info)
815 rc = xmlTextWriterStartElement(writer, "IMAGE");
819 rc = xmlTextWriterWriteFormatAttribute(writer, "INDEX", "%d",
824 rc = xmlTextWriterWriteFormatElement(writer, "DIRCOUNT", "%"PRIu64,
825 image_info->dir_count);
829 rc = xmlTextWriterWriteFormatElement(writer, "FILECOUNT", "%"PRIu64,
830 image_info->file_count);
834 rc = xmlTextWriterWriteFormatElement(writer, "TOTALBYTES", "%"PRIu64,
835 image_info->total_bytes);
839 rc = xmlTextWriterWriteFormatElement(writer, "HARDLINKBYTES", "%"PRIu64,
840 image_info->hard_link_bytes);
844 rc = xml_write_time(writer, "CREATIONTIME", image_info->creation_time);
848 rc = xml_write_time(writer, "LASTMODIFICATIONTIME",
849 image_info->last_modification_time);
853 if (image_info->windows_info_exists) {
854 rc = xml_write_windows_info(writer, &image_info->windows_info);
859 rc = xml_write_strings_from_specs(writer, image_info,
860 image_info_xml_string_specs,
861 ARRAY_LEN(image_info_xml_string_specs));
865 rc = xmlTextWriterEndElement(writer); /* </IMAGE> */
873 /* Makes space for another image in the XML information and return a pointer to
875 static struct image_info *
876 add_image_info_struct(struct wim_info *wim_info)
878 struct image_info *images;
880 images = CALLOC(wim_info->num_images + 1, sizeof(struct image_info));
883 memcpy(images, wim_info->images,
884 wim_info->num_images * sizeof(struct image_info));
885 FREE(wim_info->images);
886 wim_info->images = images;
887 wim_info->num_images++;
888 return &images[wim_info->num_images - 1];
892 clone_windows_info(const struct windows_info *old, struct windows_info *new)
896 ret = dup_strings_from_specs(old, new, windows_info_xml_string_specs,
897 ARRAY_LEN(windows_info_xml_string_specs));
901 if (old->languages) {
902 new->languages = CALLOC(old->num_languages, sizeof(new->languages[0]));
904 return WIMLIB_ERR_NOMEM;
905 new->num_languages = old->num_languages;
906 for (size_t i = 0; i < new->num_languages; i++) {
907 if (!old->languages[i])
909 new->languages[i] = TSTRDUP(old->languages[i]);
910 if (!new->languages[i])
911 return WIMLIB_ERR_NOMEM;
914 if (old->default_language &&
915 !(new->default_language = TSTRDUP(old->default_language)))
916 return WIMLIB_ERR_NOMEM;
917 if (old->system_root && !(new->system_root = TSTRDUP(old->system_root)))
918 return WIMLIB_ERR_NOMEM;
919 if (old->windows_version_exists) {
920 new->windows_version_exists = true;
921 memcpy(&new->windows_version, &old->windows_version,
922 sizeof(old->windows_version));
928 clone_image_info(const struct image_info *old, struct image_info *new)
932 new->dir_count = old->dir_count;
933 new->file_count = old->file_count;
934 new->total_bytes = old->total_bytes;
935 new->hard_link_bytes = old->hard_link_bytes;
936 new->creation_time = old->creation_time;
937 new->last_modification_time = old->last_modification_time;
939 ret = dup_strings_from_specs(old, new,
940 image_info_xml_string_specs,
941 ARRAY_LEN(image_info_xml_string_specs));
945 if (old->windows_info_exists) {
946 new->windows_info_exists = true;
947 ret = clone_windows_info(&old->windows_info,
955 /* Copies the XML information for an image between WIM files.
957 * @dest_image_name and @dest_image_description are ignored if they are NULL;
958 * otherwise, they are used to override the image name and/or image description
959 * from the XML data in the source WIM file.
961 * On failure, WIMLIB_ERR_NOMEM is returned and no changes are made. Otherwise,
962 * 0 is returned and the WIM information at *new_wim_info_p is modified.
965 xml_export_image(const struct wim_info *old_wim_info,
967 struct wim_info **new_wim_info_p,
968 const tchar *dest_image_name,
969 const tchar *dest_image_description)
971 struct wim_info *new_wim_info;
972 struct image_info *image_info;
975 DEBUG("Copying XML data between WIM files for source image %d.", image);
977 wimlib_assert(old_wim_info != NULL);
978 wimlib_assert(image >= 1 && image <= old_wim_info->num_images);
980 if (*new_wim_info_p) {
981 new_wim_info = *new_wim_info_p;
983 new_wim_info = CALLOC(1, sizeof(struct wim_info));
988 image_info = add_image_info_struct(new_wim_info);
992 ret = clone_image_info(&old_wim_info->images[image - 1], image_info);
994 goto err_destroy_image_info;
996 image_info->index = new_wim_info->num_images;
998 if (dest_image_name) {
999 FREE(image_info->name);
1000 image_info->name = TSTRDUP(dest_image_name);
1001 if (!image_info->name)
1002 goto err_destroy_image_info;
1004 if (dest_image_description) {
1005 FREE(image_info->description);
1006 image_info->description = TSTRDUP(dest_image_description);
1007 if (!image_info->description)
1008 goto err_destroy_image_info;
1010 *new_wim_info_p = new_wim_info;
1012 err_destroy_image_info:
1013 destroy_image_info(image_info);
1015 if (new_wim_info != *new_wim_info_p)
1016 free_wim_info(new_wim_info);
1017 return WIMLIB_ERR_NOMEM;
1020 /* Removes an image from the XML information. */
1022 xml_delete_image(struct wim_info **wim_info_p, int image)
1024 struct wim_info *wim_info;
1026 wim_info = *wim_info_p;
1027 wimlib_assert(image >= 1 && image <= wim_info->num_images);
1028 DEBUG("Deleting image %d from the XML data.", image);
1030 destroy_image_info(&wim_info->images[image - 1]);
1032 memmove(&wim_info->images[image - 1],
1033 &wim_info->images[image],
1034 (wim_info->num_images - image) * sizeof(struct image_info));
1036 if (--wim_info->num_images == 0) {
1037 free_wim_info(wim_info);
1040 for (int i = image - 1; i < wim_info->num_images; i++)
1041 wim_info->images[i].index--;
1046 xml_get_max_image_name_len(const WIMStruct *wim)
1049 for (u32 i = 0; i < wim->hdr.image_count; i++)
1050 max_len = max(max_len, tstrlen(wim->wim_info->images[i].name));
1054 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
1056 xml_set_memory_allocator(void *(*malloc_func)(size_t),
1057 void (*free_func)(void *),
1058 void *(*realloc_func)(void *, size_t))
1060 xmlMemSetup(free_func, malloc_func, realloc_func, STRDUP);
1065 calculate_dentry_statistics(struct wim_dentry *dentry, void *arg)
1067 struct image_info *info = arg;
1068 const struct wim_inode *inode = dentry->d_inode;
1069 struct wim_lookup_table_entry *lte;
1071 /* Update directory count and file count.
1073 * Each dentry counts as either a file or a directory, but not both.
1074 * The root directory is an exception: it is not counted at all.
1076 * Symbolic links and junction points (and presumably other reparse
1077 * points) count as regular files. This is despite the fact that
1078 * junction points have FILE_ATTRIBUTE_DIRECTORY set.
1080 if (dentry_is_root(dentry))
1083 if (inode_is_directory(inode))
1089 * Update total bytes and hard link bytes.
1091 * Unfortunately there are some inconsistencies/bugs in the way this is
1094 * If there are no alternate data streams in the image, the "total
1095 * bytes" is the sum of the size of the un-named data stream of each
1096 * inode times the link count of that inode. In other words, it would
1097 * be the total number of bytes of regular files you would have if you
1098 * extracted the full image without any hard-links. The "hard link
1099 * bytes" is equal to the "total bytes" minus the size of the un-named
1100 * data stream of each inode. In other words, the "hard link bytes"
1101 * counts the size of the un-named data stream for all the links to each
1102 * inode except the first one.
1104 * Reparse points and directories don't seem to be counted in either the
1105 * total bytes or the hard link bytes.
1107 * And now we get to the most confusing part, the alternate data
1108 * streams. They are not counted in the "total bytes". However, if the
1109 * link count of an inode with alternate data streams is 2 or greater,
1110 * the size of all the alternate data streams is included in the "hard
1111 * link bytes", and this size is multiplied by the link count (NOT one
1112 * less than the link count).
1114 lte = inode_unnamed_lte(inode, info->lookup_table);
1116 info->total_bytes += wim_resource_size(lte);
1117 if (!dentry_is_first_in_inode(dentry))
1118 info->hard_link_bytes += wim_resource_size(lte);
1121 if (inode->i_nlink >= 2 && dentry_is_first_in_inode(dentry)) {
1122 for (unsigned i = 0; i < inode->i_num_ads; i++) {
1123 if (inode->i_ads_entries[i].stream_name_nbytes) {
1124 lte = inode_stream_lte(inode, i + 1, info->lookup_table);
1126 info->hard_link_bytes += inode->i_nlink *
1127 wim_resource_size(lte);
1136 * Calculate what to put in the <FILECOUNT>, <DIRCOUNT>, <TOTALBYTES>, and
1137 * <HARDLINKBYTES> elements of each <IMAGE>.
1139 * Please note there is no official documentation for exactly how this is done.
1140 * But, see calculate_dentry_statistics().
1143 xml_update_image_info(WIMStruct *wim, int image)
1145 struct image_info *image_info;
1147 DEBUG("Updating the image info for image %d", image);
1149 image_info = &wim->wim_info->images[image - 1];
1151 image_info->file_count = 0;
1152 image_info->dir_count = 0;
1153 image_info->total_bytes = 0;
1154 image_info->hard_link_bytes = 0;
1155 image_info->lookup_table = wim->lookup_table;
1157 for_dentry_in_tree(wim->image_metadata[image - 1]->root_dentry,
1158 calculate_dentry_statistics,
1160 image_info->last_modification_time = get_wim_timestamp();
1163 /* Adds an image to the XML information. */
1165 xml_add_image(WIMStruct *wim, const tchar *name)
1167 struct wim_info *wim_info;
1168 struct image_info *image_info;
1170 wimlib_assert(name != NULL);
1172 /* If this is the first image, allocate the struct wim_info. Otherwise
1173 * use the existing struct wim_info. */
1174 if (wim->wim_info) {
1175 wim_info = wim->wim_info;
1177 wim_info = CALLOC(1, sizeof(struct wim_info));
1179 return WIMLIB_ERR_NOMEM;
1182 image_info = add_image_info_struct(wim_info);
1184 goto out_free_wim_info;
1186 if (!(image_info->name = TSTRDUP(name)))
1187 goto out_destroy_image_info;
1189 wim->wim_info = wim_info;
1190 image_info->index = wim_info->num_images;
1191 image_info->creation_time = get_wim_timestamp();
1192 xml_update_image_info(wim, image_info->index);
1195 out_destroy_image_info:
1196 destroy_image_info(image_info);
1197 wim_info->num_images--;
1199 if (wim_info != wim->wim_info)
1201 return WIMLIB_ERR_NOMEM;
1204 /* Prints information about the specified image from struct wim_info structure.
1207 print_image_info(const struct wim_info *wim_info, int image)
1209 const struct image_info *image_info;
1213 wimlib_assert(image >= 1 && image <= wim_info->num_images);
1215 image_info = &wim_info->images[image - 1];
1217 tprintf(T("Index: %d\n"), image_info->index);
1218 tprintf(T("Name: %"TS"\n"), image_info->name);
1220 /* Always print the Description: part even if there is no
1222 if (image_info->description)
1223 desc = image_info->description;
1226 tprintf(T("Description: %"TS"\n"), desc);
1228 if (image_info->display_name) {
1229 tprintf(T("Display Name: %"TS"\n"),
1230 image_info->display_name);
1233 if (image_info->display_description) {
1234 tprintf(T("Display Description: %"TS"\n"),
1235 image_info->display_description);
1238 tprintf(T("Directory Count: %"PRIu64"\n"), image_info->dir_count);
1239 tprintf(T("File Count: %"PRIu64"\n"), image_info->file_count);
1240 tprintf(T("Total Bytes: %"PRIu64"\n"), image_info->total_bytes);
1241 tprintf(T("Hard Link Bytes: %"PRIu64"\n"), image_info->hard_link_bytes);
1243 wim_timestamp_to_str(image_info->creation_time, buf, sizeof(buf));
1244 tprintf(T("Creation Time: %"TS"\n"), buf);
1246 wim_timestamp_to_str(image_info->last_modification_time, buf, sizeof(buf));
1247 tprintf(T("Last Modification Time: %"TS"\n"), buf);
1248 if (image_info->windows_info_exists)
1249 print_windows_info(&image_info->windows_info);
1250 if (image_info->flags)
1251 tprintf(T("Flags: %"TS"\n"), image_info->flags);
1256 libxml_global_init(void)
1259 xmlInitCharEncodingHandlers();
1263 libxml_global_cleanup(void)
1266 xmlCleanupCharEncodingHandlers();
1269 /* Reads the XML data from a WIM file. */
1271 read_wim_xml_data(WIMStruct *wim)
1277 const struct resource_entry *res_entry;
1279 res_entry = &wim->hdr.xml_res_entry;
1281 DEBUG("Reading XML data: %"PRIu64" bytes at offset %"PRIu64"",
1282 (u64)res_entry->size, res_entry->offset);
1284 ret = res_entry_to_data(res_entry, wim, (void**)&xml_data);
1288 doc = xmlReadMemory((const char *)xml_data, res_entry->original_size,
1289 NULL, "UTF-16LE", 0);
1291 ERROR("Failed to parse XML data");
1292 ret = WIMLIB_ERR_XML;
1293 goto out_free_xml_data;
1296 root = xmlDocGetRootElement(doc);
1297 if (!root || !node_is_element(root) || !node_name_is(root, "WIM")) {
1298 ERROR("WIM XML data is invalid");
1299 ret = WIMLIB_ERR_XML;
1303 ret = xml_read_wim_info(root, &wim->wim_info);
1312 /* Prepares an in-memory buffer containing the UTF-16LE XML data for a WIM file.
1314 * total_bytes is the number to write in <TOTALBYTES>, or
1315 * WIM_TOTALBYTES_USE_EXISTING to use the existing value in memory, or
1316 * WIM_TOTALBYTES_OMIT to omit <TOTALBYTES> entirely.
1319 prepare_wim_xml_data(WIMStruct *wim, int image, u64 total_bytes,
1320 u8 **xml_data_ret, size_t *xml_len_ret)
1322 xmlCharEncodingHandler *encoding_handler;
1324 xmlOutputBuffer *outbuf;
1325 xmlTextWriter *writer;
1328 const xmlChar *content;
1333 /* Open an xmlTextWriter that writes to an in-memory buffer using
1334 * UTF-16LE encoding. */
1336 encoding_handler = xmlGetCharEncodingHandler(XML_CHAR_ENCODING_UTF16LE);
1337 if (!encoding_handler) {
1338 ERROR("Failed to get XML character encoding handler for UTF-16LE");
1339 ret = WIMLIB_ERR_LIBXML_UTF16_HANDLER_NOT_AVAILABLE;
1343 buf = xmlBufferCreate();
1345 ERROR("Failed to create xmlBuffer");
1346 ret = WIMLIB_ERR_NOMEM;
1350 outbuf = xmlOutputBufferCreateBuffer(buf, encoding_handler);
1352 ERROR("Failed to allocate xmlOutputBuffer");
1353 ret = WIMLIB_ERR_NOMEM;
1354 goto out_buffer_free;
1357 writer = xmlNewTextWriter(outbuf);
1359 ERROR("Failed to allocate xmlTextWriter");
1360 ret = WIMLIB_ERR_NOMEM;
1361 goto out_output_buffer_close;
1364 /* Write the XML document. */
1366 ret = xmlTextWriterStartElement(writer, "WIM");
1368 goto out_write_error;
1370 /* The contents of the <TOTALBYTES> element in the XML data, under the
1371 * <WIM> element (not the <IMAGE> element), is for non-split WIMs the
1372 * size of the WIM file excluding the XML data and integrity table.
1373 * For split WIMs, <TOTALBYTES> takes into account the entire WIM, not
1374 * just the current part. */
1375 if (total_bytes != WIM_TOTALBYTES_OMIT) {
1376 if (total_bytes == WIM_TOTALBYTES_USE_EXISTING) {
1378 total_bytes = wim->wim_info->total_bytes;
1382 ret = xmlTextWriterWriteFormatElement(writer, "TOTALBYTES",
1383 "%"PRIu64, total_bytes);
1385 goto out_write_error;
1388 if (image == WIMLIB_ALL_IMAGES) {
1390 last = wim->hdr.image_count;
1396 for (int i = first; i <= last; i++) {
1397 ret = xml_write_image_info(writer, &wim->wim_info->images[i - 1]);
1400 goto out_write_error;
1401 goto out_free_text_writer;
1405 ret = xmlTextWriterEndElement(writer);
1407 goto out_write_error;
1409 ret = xmlTextWriterEndDocument(writer);
1411 goto out_write_error;
1413 ret = xmlTextWriterFlush(writer);
1415 goto out_write_error;
1417 /* Retrieve the buffer into which the document was written. */
1419 content = xmlBufferContent(buf);
1420 len = xmlBufferLength(buf);
1422 /* Copy the data into a new buffer, and prefix it with the UTF-16LE BOM
1423 * (byte order mark), which is required by MS's software to understand
1427 xml_data = MALLOC(xml_len);
1429 ret = WIMLIB_ERR_NOMEM;
1430 goto out_free_text_writer;
1434 memcpy(&xml_data[2], content, len);
1436 /* Clean up libxml objects and return success. */
1437 *xml_data_ret = xml_data;
1438 *xml_len_ret = xml_len;
1440 out_free_text_writer:
1441 /* xmlFreeTextWriter will free the attached xmlOutputBuffer. */
1442 xmlFreeTextWriter(writer);
1443 goto out_buffer_free;
1444 out_output_buffer_close:
1445 xmlOutputBufferClose(outbuf);
1452 ERROR("Error writing XML data");
1453 ret = WIMLIB_ERR_WRITE;
1454 goto out_free_text_writer;
1457 /* Writes the XML data to a WIM file. */
1459 write_wim_xml_data(WIMStruct *wim, int image, u64 total_bytes,
1460 struct resource_entry *out_res_entry,
1461 int write_resource_flags)
1467 DEBUG("Writing WIM XML data (image=%d, offset=%"PRIu64")",
1468 image, total_bytes, wim->out_fd.offset);
1470 ret = prepare_wim_xml_data(wim, image, total_bytes,
1471 &xml_data, &xml_len);
1475 /* Write the XML data uncompressed. Although wimlib can handle
1476 * compressed XML data, MS software cannot. */
1477 ret = write_wim_resource_from_buffer(xml_data,
1479 WIM_RESHDR_FLAG_METADATA,
1481 WIMLIB_COMPRESSION_TYPE_NONE,
1484 write_resource_flags);
1489 /* API function documented in wimlib.h */
1490 WIMLIBAPI const tchar *
1491 wimlib_get_image_name(const WIMStruct *wim, int image)
1493 if (image < 1 || image > wim->hdr.image_count)
1495 return wim->wim_info->images[image - 1].name;
1498 /* API function documented in wimlib.h */
1499 WIMLIBAPI const tchar *
1500 wimlib_get_image_description(const WIMStruct *wim, int image)
1502 if (image < 1 || image > wim->hdr.image_count)
1504 return wim->wim_info->images[image - 1].description;
1507 /* API function documented in wimlib.h */
1509 wimlib_image_name_in_use(const WIMStruct *wim, const tchar *name)
1511 if (!name || !*name)
1513 for (int i = 1; i <= wim->hdr.image_count; i++)
1514 if (!tstrcmp(wim->wim_info->images[i - 1].name, name))
1520 /* API function documented in wimlib.h */
1522 wimlib_extract_xml_data(WIMStruct *wim, FILE *fp)
1528 ret = res_entry_to_data(&wim->hdr.xml_res_entry, wim, &buf);
1532 size = wim->hdr.xml_res_entry.original_size;
1533 if (fwrite(buf, 1, size, fp) != size) {
1534 ERROR_WITH_ERRNO("Failed to extract XML data");
1535 ret = WIMLIB_ERR_WRITE;
1546 /* API function documented in wimlib.h */
1548 wimlib_set_image_name(WIMStruct *wim, int image, const tchar *name)
1554 DEBUG("Setting the name of image %d to %"TS, image, name);
1556 ret = can_modify_wim(wim);
1563 if (image < 1 || image > wim->hdr.image_count) {
1564 ERROR("%d is not a valid image", image);
1565 return WIMLIB_ERR_INVALID_IMAGE;
1568 for (i = 1; i <= wim->hdr.image_count; i++) {
1571 if (!tstrcmp(wim->wim_info->images[i - 1].name, name)) {
1572 ERROR("The name \"%"TS"\" is already in use in the WIM!",
1574 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
1580 return WIMLIB_ERR_NOMEM;
1582 FREE(wim->wim_info->images[image - 1].name);
1583 wim->wim_info->images[image - 1].name = p;
1588 do_set_image_info_str(WIMStruct *wim, int image, const tchar *tstr,
1592 tchar **dest_tstr_p;
1595 ret = can_modify_wim(wim);
1599 if (image < 1 || image > wim->hdr.image_count) {
1600 ERROR("%d is not a valid image", image);
1601 return WIMLIB_ERR_INVALID_IMAGE;
1604 tstr_copy = TSTRDUP(tstr);
1606 return WIMLIB_ERR_NOMEM;
1610 dest_tstr_p = (tchar**)((void*)&wim->wim_info->images[image - 1] + offset);
1613 *dest_tstr_p = tstr_copy;
1617 /* API function documented in wimlib.h */
1619 wimlib_set_image_descripton(WIMStruct *wim, int image,
1620 const tchar *description)
1622 return do_set_image_info_str(wim, image, description,
1623 offsetof(struct image_info, description));
1626 /* API function documented in wimlib.h */
1628 wimlib_set_image_flags(WIMStruct *wim, int image, const tchar *flags)
1630 return do_set_image_info_str(wim, image, flags,
1631 offsetof(struct image_info, flags));