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)
136 return info->total_bytes;
142 wim_info_get_image_hard_link_bytes(const struct wim_info *info, int image)
145 return info->images[image - 1].hard_link_bytes;
151 wim_info_get_image_total_bytes(const struct wim_info *info, int image)
154 return info->images[image - 1].total_bytes;
160 wim_info_get_num_images(const struct wim_info *info)
163 return info->num_images;
168 /* Architecture constants are from w64 mingw winnt.h */
169 #define PROCESSOR_ARCHITECTURE_INTEL 0
170 #define PROCESSOR_ARCHITECTURE_MIPS 1
171 #define PROCESSOR_ARCHITECTURE_ALPHA 2
172 #define PROCESSOR_ARCHITECTURE_PPC 3
173 #define PROCESSOR_ARCHITECTURE_SHX 4
174 #define PROCESSOR_ARCHITECTURE_ARM 5
175 #define PROCESSOR_ARCHITECTURE_IA64 6
176 #define PROCESSOR_ARCHITECTURE_ALPHA64 7
177 #define PROCESSOR_ARCHITECTURE_MSIL 8
178 #define PROCESSOR_ARCHITECTURE_AMD64 9
179 #define PROCESSOR_ARCHITECTURE_IA32_ON_WIN64 10
181 /* Returns a statically allocated string that is a string representation of the
182 * architecture number. */
187 case PROCESSOR_ARCHITECTURE_INTEL:
189 case PROCESSOR_ARCHITECTURE_MIPS:
191 case PROCESSOR_ARCHITECTURE_ARM:
193 case PROCESSOR_ARCHITECTURE_IA64:
195 case PROCESSOR_ARCHITECTURE_AMD64:
203 /* Iterate through the children of an xmlNode. */
204 #define for_node_child(parent, child) \
205 for (child = parent->children; child != NULL; child = child->next)
207 /* Utility functions for xmlNodes */
209 node_is_element(xmlNode *node)
211 return node->type == XML_ELEMENT_NODE;
215 node_is_text(xmlNode *node)
217 return node->type == XML_TEXT_NODE;
221 node_name_is(xmlNode *node, const char *name)
223 /* For now, both upper case and lower case element names are accepted. */
224 return strcasecmp((const char *)node->name, name) == 0;
228 node_get_number(const xmlNode *u64_node, int base)
231 for_node_child(u64_node, child)
232 if (node_is_text(child))
233 return strtoull(child->content, NULL, base);
237 /* Finds the text node that is a child of an element node and returns its
238 * content converted to a 64-bit unsigned integer. Returns 0 if no text node is
241 node_get_u64(const xmlNode *u64_node)
243 return node_get_number(u64_node, 10);
246 /* Like node_get_u64(), but expects a number in base 16. */
248 node_get_hex_u64(const xmlNode *u64_node)
250 return node_get_number(u64_node, 16);
254 node_get_string(const xmlNode *string_node, tchar **tstr_ret)
260 for_node_child(string_node, child) {
261 if (node_is_text(child) && child->content) {
262 ret = utf8_to_tstr_simple(child->content, &tstr);
272 /* Returns the timestamp from a time node. It has child elements <HIGHPART> and
273 * <LOWPART> that are then used to construct a 64-bit timestamp. */
275 node_get_timestamp(const xmlNode *time_node)
280 for_node_child(time_node, child) {
281 if (!node_is_element(child))
283 if (node_name_is(child, "HIGHPART"))
284 high_part = node_get_hex_u64(child);
285 else if (node_name_is(child, "LOWPART"))
286 low_part = node_get_hex_u64(child);
288 return (u64)low_part | ((u64)high_part << 32);
291 /* Used to sort an array of struct image_infos by their image indices. */
293 sort_by_index(const void *p1, const void *p2)
295 int index_1 = ((const struct image_info*)p1)->index;
296 int index_2 = ((const struct image_info*)p2)->index;
297 if (index_1 < index_2)
299 else if (index_1 > index_2)
306 /* Frees memory allocated inside a struct windows_info structure. */
308 destroy_windows_info(struct windows_info *windows_info)
310 FREE(windows_info->product_name);
311 FREE(windows_info->edition_id);
312 FREE(windows_info->installation_type);
313 FREE(windows_info->hal);
314 FREE(windows_info->product_type);
315 FREE(windows_info->product_suite);
316 for (size_t i = 0; i < windows_info->num_languages; i++)
317 FREE(windows_info->languages[i]);
318 FREE(windows_info->languages);
319 FREE(windows_info->default_language);
320 FREE(windows_info->system_root);
323 /* Frees memory allocated inside a struct image_info structure. */
325 destroy_image_info(struct image_info *image_info)
327 FREE(image_info->name);
328 FREE(image_info->description);
329 FREE(image_info->flags);
330 FREE(image_info->display_name);
331 FREE(image_info->display_description);
332 destroy_windows_info(&image_info->windows_info);
333 memset(image_info, 0, sizeof(struct image_info));
337 free_wim_info(struct wim_info *info)
341 for (int i = 0; i < info->num_images; i++)
342 destroy_image_info(&info->images[i]);
349 /* Reads the information from a <VERSION> element inside the <WINDOWS> element.
352 xml_read_windows_version(const xmlNode *version_node,
353 struct windows_version* windows_version)
356 for_node_child(version_node, child) {
357 if (!node_is_element(child))
359 if (node_name_is(child, "MAJOR"))
360 windows_version->major = node_get_u64(child);
361 else if (node_name_is(child, "MINOR"))
362 windows_version->minor = node_get_u64(child);
363 else if (node_name_is(child, "BUILD"))
364 windows_version->build = node_get_u64(child);
365 else if (node_name_is(child, "SPBUILD"))
366 windows_version->sp_build = node_get_u64(child);
367 else if (node_name_is(child, "SPLEVEL"))
368 windows_version->sp_level = node_get_u64(child);
372 /* Reads the information from a <LANGUAGE> element inside a <WINDOWS> element.
375 xml_read_languages(const xmlNode *languages_node,
376 tchar ***languages_ret,
377 size_t *num_languages_ret,
378 tchar **default_language_ret)
381 size_t num_languages = 0;
385 for_node_child(languages_node, child)
386 if (node_is_element(child) && node_name_is(child, "LANGUAGE"))
389 languages = CALLOC(num_languages, sizeof(languages[0]));
391 return WIMLIB_ERR_NOMEM;
393 *languages_ret = languages;
394 *num_languages_ret = num_languages;
397 for_node_child(languages_node, child) {
398 if (!node_is_element(child))
400 if (node_name_is(child, "LANGUAGE"))
401 ret = node_get_string(child, languages++);
402 else if (node_name_is(child, "DEFAULT"))
403 ret = node_get_string(child, default_language_ret);
410 /* Reads the information from a <WINDOWS> element inside an <IMAGE> element. */
412 xml_read_windows_info(const xmlNode *windows_node,
413 struct windows_info *windows_info)
418 for_node_child(windows_node, child) {
419 if (!node_is_element(child))
421 if (node_name_is(child, "ARCH")) {
422 windows_info->arch = node_get_u64(child);
423 } else if (node_name_is(child, "PRODUCTNAME")) {
424 ret = node_get_string(child,
425 &windows_info->product_name);
426 } else if (node_name_is(child, "EDITIONID")) {
427 ret = node_get_string(child,
428 &windows_info->edition_id);
429 } else if (node_name_is(child, "INSTALLATIONTYPE")) {
430 ret = node_get_string(child,
431 &windows_info->installation_type);
432 } else if (node_name_is(child, "PRODUCTTYPE")) {
433 ret = node_get_string(child,
434 &windows_info->product_type);
435 } else if (node_name_is(child, "PRODUCTSUITE")) {
436 ret = node_get_string(child,
437 &windows_info->product_suite);
438 } else if (node_name_is(child, "LANGUAGES")) {
439 ret = xml_read_languages(child,
440 &windows_info->languages,
441 &windows_info->num_languages,
442 &windows_info->default_language);
443 } else if (node_name_is(child, "VERSION")) {
444 xml_read_windows_version(child,
445 &windows_info->windows_version);
446 windows_info->windows_version_exists = true;
447 } else if (node_name_is(child, "SYSTEMROOT")) {
448 ret = node_get_string(child, &windows_info->system_root);
449 } else if (node_name_is(child, "HAL")) {
450 ret = node_get_string(child, &windows_info->hal);
458 /* Reads the information from an <IMAGE> element. */
460 xml_read_image_info(xmlNode *image_node, struct image_info *image_info)
466 index_prop = xmlGetProp(image_node, "INDEX");
468 image_info->index = atoi(index_prop);
471 image_info->index = 1;
475 for_node_child(image_node, child) {
476 if (!node_is_element(child))
478 if (node_name_is(child, "DIRCOUNT"))
479 image_info->dir_count = node_get_u64(child);
480 else if (node_name_is(child, "FILECOUNT"))
481 image_info->file_count = node_get_u64(child);
482 else if (node_name_is(child, "TOTALBYTES"))
483 image_info->total_bytes = node_get_u64(child);
484 else if (node_name_is(child, "HARDLINKBYTES"))
485 image_info->hard_link_bytes = node_get_u64(child);
486 else if (node_name_is(child, "CREATIONTIME"))
487 image_info->creation_time = node_get_timestamp(child);
488 else if (node_name_is(child, "LASTMODIFICATIONTIME"))
489 image_info->last_modification_time = node_get_timestamp(child);
490 else if (node_name_is(child, "WINDOWS")) {
491 DEBUG("Found <WINDOWS> tag");
492 ret = xml_read_windows_info(child,
493 &image_info->windows_info);
494 image_info->windows_info_exists = true;
495 } else if (node_name_is(child, "NAME")) {
496 ret = node_get_string(child, &image_info->name);
497 } else if (node_name_is(child, "DESCRIPTION")) {
498 ret = node_get_string(child, &image_info->description);
499 } else if (node_name_is(child, "FLAGS")) {
500 ret = node_get_string(child, &image_info->flags);
501 } else if (node_name_is(child, "DISPLAYNAME")) {
502 ret = node_get_string(child, &image_info->display_name);
503 } else if (node_name_is(child, "DISPLAYDESCRIPTION")) {
504 ret = node_get_string(child, &image_info->display_description);
509 if (!image_info->name) {
511 /*WARNING("Image with index %d has no name", image_info->index);*/
512 empty_name = MALLOC(sizeof(tchar));
514 return WIMLIB_ERR_NOMEM;
515 *empty_name = T('\0');
516 image_info->name = empty_name;
521 /* Reads the information from a <WIM> element, which should be the root element
522 * of the XML tree. */
524 xml_read_wim_info(const xmlNode *wim_node, struct wim_info **wim_info_ret)
526 struct wim_info *wim_info;
532 wim_info = CALLOC(1, sizeof(struct wim_info));
534 return WIMLIB_ERR_NOMEM;
536 /* Count how many images there are. */
538 for_node_child(wim_node, child) {
539 if (node_is_element(child) && node_name_is(child, "IMAGE")) {
540 if (num_images == INT_MAX) {
541 return WIMLIB_ERR_IMAGE_COUNT;
547 if (num_images > 0) {
548 /* Allocate the array of struct image_infos and fill them in. */
549 wim_info->images = CALLOC(num_images, sizeof(wim_info->images[0]));
550 if (!wim_info->images) {
551 ret = WIMLIB_ERR_NOMEM;
554 wim_info->num_images = num_images;
556 for_node_child(wim_node, child) {
557 if (!node_is_element(child))
559 if (node_name_is(child, "IMAGE")) {
560 DEBUG("Found <IMAGE> tag");
561 ret = xml_read_image_info(child,
562 &wim_info->images[i]);
566 } else if (node_name_is(child, "TOTALBYTES")) {
567 wim_info->total_bytes = node_get_u64(child);
571 /* Sort the array of image info by image index. */
572 qsort(wim_info->images, num_images,
573 sizeof(struct image_info), sort_by_index);
575 /* Make sure the image indices make sense */
576 for (i = 0; i < num_images; i++) {
577 if (wim_info->images[i].index != i + 1) {
578 ERROR("WIM images are not indexed [1...%d] "
579 "in XML data as expected",
581 return WIMLIB_ERR_IMAGE_COUNT;
586 *wim_info_ret = wim_info;
589 free_wim_info(wim_info);
593 /* Prints the information contained in a `struct windows_info'.
595 * Warning: any strings printed here are in UTF-8 encoding. If the locale
596 * character encoding is not UTF-8, the printed strings may be garbled. */
598 print_windows_info(const struct windows_info *windows_info)
600 const struct windows_version *windows_version;
602 tprintf(T("Architecture: %"TS"\n"),
603 get_arch(windows_info->arch));
605 if (windows_info->product_name) {
606 tprintf(T("Product Name: %"TS"\n"),
607 windows_info->product_name);
610 if (windows_info->edition_id) {
611 tprintf(T("Edition ID: %"TS"\n"),
612 windows_info->edition_id);
615 if (windows_info->installation_type) {
616 tprintf(T("Installation Type: %"TS"\n"),
617 windows_info->installation_type);
620 if (windows_info->hal) {
621 tprintf(T("HAL: %"TS"\n"),
625 if (windows_info->product_type) {
626 tprintf(T("Product Type: %"TS"\n"),
627 windows_info->product_type);
630 if (windows_info->product_suite) {
631 tprintf(T("Product Suite: %"TS"\n"),
632 windows_info->product_suite);
635 tprintf(T("Languages: "));
636 for (size_t i = 0; i < windows_info->num_languages; i++) {
638 tfputs(windows_info->languages[i], stdout);
642 if (windows_info->default_language) {
643 tprintf(T("Default Language: %"TS"\n"),
644 windows_info->default_language);
646 if (windows_info->system_root) {
647 tprintf(T("System Root: %"TS"\n"),
648 windows_info->system_root);
651 if (windows_info->windows_version_exists) {
652 windows_version = &windows_info->windows_version;
653 tprintf(T("Major Version: %"PRIu64"\n"),
654 windows_version->major);
655 tprintf(T("Minor Version: %"PRIu64"\n"),
656 windows_version->minor);
657 tprintf(T("Build: %"PRIu64"\n"),
658 windows_version->build);
659 tprintf(T("Service Pack Build: %"PRIu64"\n"),
660 windows_version->sp_build);
661 tprintf(T("Service Pack Level: %"PRIu64"\n"),
662 windows_version->sp_level);
667 xml_write_string(xmlTextWriter *writer, const char *name,
672 int rc = tstr_to_utf8_simple(tstr, &utf8_str);
675 rc = xmlTextWriterWriteElement(writer, name, utf8_str);
684 xml_write_strings_from_specs(xmlTextWriter *writer,
685 const void *struct_with_strings,
686 const struct xml_string_spec specs[],
689 for (size_t i = 0; i < num_specs; i++) {
690 int rc = xml_write_string(writer, specs[i].name,
691 *(const tchar * const *)
692 (struct_with_strings + specs[i].offset));
700 dup_strings_from_specs(const void *old_struct_with_strings,
701 void *new_struct_with_strings,
702 const struct xml_string_spec specs[],
705 for (size_t i = 0; i < num_specs; i++) {
706 const tchar *old_str = *(const tchar * const *)
707 ((const void*)old_struct_with_strings + specs[i].offset);
708 tchar **new_str_p = (tchar **)((void*)new_struct_with_strings + specs[i].offset);
710 *new_str_p = TSTRDUP(old_str);
712 return WIMLIB_ERR_NOMEM;
718 /* Writes the information contained in a `struct windows_version' to the XML
719 * document being written. This is the <VERSION> element inside the <WINDOWS>
722 xml_write_windows_version(xmlTextWriter *writer,
723 const struct windows_version *version)
726 rc = xmlTextWriterStartElement(writer, "VERSION");
730 rc = xmlTextWriterWriteFormatElement(writer, "MAJOR", "%"PRIu64,
735 rc = xmlTextWriterWriteFormatElement(writer, "MINOR", "%"PRIu64,
740 rc = xmlTextWriterWriteFormatElement(writer, "BUILD", "%"PRIu64,
745 rc = xmlTextWriterWriteFormatElement(writer, "SPBUILD", "%"PRIu64,
750 rc = xmlTextWriterWriteFormatElement(writer, "SPLEVEL", "%"PRIu64,
755 rc = xmlTextWriterEndElement(writer); /* </VERSION> */
762 /* Writes the information contained in a `struct windows_info' to the XML
763 * document being written. This is the <WINDOWS> element. */
765 xml_write_windows_info(xmlTextWriter *writer,
766 const struct windows_info *windows_info)
769 rc = xmlTextWriterStartElement(writer, "WINDOWS");
773 rc = xmlTextWriterWriteFormatElement(writer, "ARCH", "%"PRIu64,
778 rc = xml_write_strings_from_specs(writer,
780 windows_info_xml_string_specs,
781 ARRAY_LEN(windows_info_xml_string_specs));
785 if (windows_info->num_languages) {
786 rc = xmlTextWriterStartElement(writer, "LANGUAGES");
790 for (size_t i = 0; i < windows_info->num_languages; i++) {
791 rc = xml_write_string(writer, "LANGUAGE",
792 windows_info->languages[i]);
797 rc = xml_write_string(writer, "DEFAULT",
798 windows_info->default_language);
802 rc = xmlTextWriterEndElement(writer); /* </LANGUAGES> */
807 if (windows_info->windows_version_exists) {
808 rc = xml_write_windows_version(writer, &windows_info->windows_version);
813 rc = xml_write_string(writer, "SYSTEMROOT", windows_info->system_root);
817 rc = xmlTextWriterEndElement(writer); /* </WINDOWS> */
824 /* Writes a time element to the XML document being constructed in memory. */
826 xml_write_time(xmlTextWriter *writer, const char *element_name, u64 time)
829 rc = xmlTextWriterStartElement(writer, element_name);
833 rc = xmlTextWriterWriteFormatElement(writer, "HIGHPART",
834 "0x%08"PRIX32, (u32)(time >> 32));
838 rc = xmlTextWriterWriteFormatElement(writer, "LOWPART",
839 "0x%08"PRIX32, (u32)time);
843 rc = xmlTextWriterEndElement(writer); /* </@element_name> */
849 /* Writes an <IMAGE> element to the XML document. */
851 xml_write_image_info(xmlTextWriter *writer, const struct image_info *image_info)
854 rc = xmlTextWriterStartElement(writer, "IMAGE");
858 rc = xmlTextWriterWriteFormatAttribute(writer, "INDEX", "%d",
863 rc = xmlTextWriterWriteFormatElement(writer, "DIRCOUNT", "%"PRIu64,
864 image_info->dir_count);
868 rc = xmlTextWriterWriteFormatElement(writer, "FILECOUNT", "%"PRIu64,
869 image_info->file_count);
873 rc = xmlTextWriterWriteFormatElement(writer, "TOTALBYTES", "%"PRIu64,
874 image_info->total_bytes);
878 rc = xmlTextWriterWriteFormatElement(writer, "HARDLINKBYTES", "%"PRIu64,
879 image_info->hard_link_bytes);
883 rc = xml_write_time(writer, "CREATIONTIME", image_info->creation_time);
887 rc = xml_write_time(writer, "LASTMODIFICATIONTIME",
888 image_info->last_modification_time);
892 if (image_info->windows_info_exists) {
893 rc = xml_write_windows_info(writer, &image_info->windows_info);
898 rc = xml_write_strings_from_specs(writer, image_info,
899 image_info_xml_string_specs,
900 ARRAY_LEN(image_info_xml_string_specs));
904 rc = xmlTextWriterEndElement(writer); /* </IMAGE> */
913 /* Makes space for another image in the XML information and return a pointer to
915 static struct image_info *
916 add_image_info_struct(struct wim_info *wim_info)
918 struct image_info *images;
920 images = CALLOC(wim_info->num_images + 1, sizeof(struct image_info));
923 memcpy(images, wim_info->images,
924 wim_info->num_images * sizeof(struct image_info));
925 FREE(wim_info->images);
926 wim_info->images = images;
927 wim_info->num_images++;
928 return &images[wim_info->num_images - 1];
932 clone_windows_info(const struct windows_info *old, struct windows_info *new)
936 ret = dup_strings_from_specs(old, new, windows_info_xml_string_specs,
937 ARRAY_LEN(windows_info_xml_string_specs));
941 if (old->languages) {
942 new->languages = CALLOC(old->num_languages, sizeof(new->languages[0]));
944 return WIMLIB_ERR_NOMEM;
945 new->num_languages = old->num_languages;
946 for (size_t i = 0; i < new->num_languages; i++) {
947 if (!old->languages[i])
949 new->languages[i] = TSTRDUP(old->languages[i]);
950 if (!new->languages[i])
951 return WIMLIB_ERR_NOMEM;
954 if (old->default_language &&
955 !(new->default_language = TSTRDUP(old->default_language)))
956 return WIMLIB_ERR_NOMEM;
957 if (old->system_root && !(new->system_root = TSTRDUP(old->system_root)))
958 return WIMLIB_ERR_NOMEM;
959 if (old->windows_version_exists) {
960 new->windows_version_exists = true;
961 memcpy(&new->windows_version, &old->windows_version,
962 sizeof(old->windows_version));
968 clone_image_info(const struct image_info *old, struct image_info *new)
972 new->dir_count = old->dir_count;
973 new->file_count = old->file_count;
974 new->total_bytes = old->total_bytes;
975 new->hard_link_bytes = old->hard_link_bytes;
976 new->creation_time = old->creation_time;
977 new->last_modification_time = old->last_modification_time;
979 ret = dup_strings_from_specs(old, new,
980 image_info_xml_string_specs,
981 ARRAY_LEN(image_info_xml_string_specs));
985 if (old->windows_info_exists) {
986 new->windows_info_exists = true;
987 ret = clone_windows_info(&old->windows_info,
995 /* Copies the XML information for an image between WIM files.
997 * @dest_image_name and @dest_image_description are ignored if they are NULL;
998 * otherwise, they are used to override the image name and/or image description
999 * from the XML data in the source WIM file.
1001 * On failure, WIMLIB_ERR_NOMEM is returned and no changes are made. Otherwise,
1002 * 0 is returned and the WIM information at *new_wim_info_p is modified.
1005 xml_export_image(const struct wim_info *old_wim_info,
1007 struct wim_info **new_wim_info_p,
1008 const tchar *dest_image_name,
1009 const tchar *dest_image_description)
1011 struct wim_info *new_wim_info;
1012 struct image_info *image_info;
1015 DEBUG("Copying XML data between WIM files for source image %d.", image);
1017 wimlib_assert(old_wim_info != NULL);
1018 wimlib_assert(image >= 1 && image <= old_wim_info->num_images);
1020 if (*new_wim_info_p) {
1021 new_wim_info = *new_wim_info_p;
1023 new_wim_info = CALLOC(1, sizeof(struct wim_info));
1028 image_info = add_image_info_struct(new_wim_info);
1032 ret = clone_image_info(&old_wim_info->images[image - 1], image_info);
1034 goto err_destroy_image_info;
1036 image_info->index = new_wim_info->num_images;
1038 if (dest_image_name) {
1039 FREE(image_info->name);
1040 image_info->name = TSTRDUP(dest_image_name);
1041 if (!image_info->name)
1042 goto err_destroy_image_info;
1044 if (dest_image_description) {
1045 FREE(image_info->description);
1046 image_info->description = TSTRDUP(dest_image_description);
1047 if (!image_info->description)
1048 goto err_destroy_image_info;
1050 *new_wim_info_p = new_wim_info;
1052 err_destroy_image_info:
1053 destroy_image_info(image_info);
1055 if (new_wim_info != *new_wim_info_p)
1056 free_wim_info(new_wim_info);
1057 return WIMLIB_ERR_NOMEM;
1060 /* Removes an image from the XML information. */
1062 xml_delete_image(struct wim_info **wim_info_p, int image)
1064 struct wim_info *wim_info;
1066 wim_info = *wim_info_p;
1067 wimlib_assert(image >= 1 && image <= wim_info->num_images);
1068 DEBUG("Deleting image %d from the XML data.", image);
1070 destroy_image_info(&wim_info->images[image - 1]);
1072 memmove(&wim_info->images[image - 1],
1073 &wim_info->images[image],
1074 (wim_info->num_images - image) * sizeof(struct image_info));
1076 if (--wim_info->num_images == 0) {
1077 free_wim_info(wim_info);
1080 for (int i = image - 1; i < wim_info->num_images; i++)
1081 wim_info->images[i].index--;
1086 xml_get_max_image_name_len(const WIMStruct *wim)
1089 for (u32 i = 0; i < wim->hdr.image_count; i++)
1090 max_len = max(max_len, tstrlen(wim->wim_info->images[i].name));
1094 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
1096 xml_set_memory_allocator(void *(*malloc_func)(size_t),
1097 void (*free_func)(void *),
1098 void *(*realloc_func)(void *, size_t))
1100 xmlMemSetup(free_func, malloc_func, realloc_func, STRDUP);
1105 calculate_dentry_statistics(struct wim_dentry *dentry, void *arg)
1107 struct image_info *info = arg;
1108 const struct wim_inode *inode = dentry->d_inode;
1109 struct wim_lookup_table_entry *lte;
1111 /* Update directory count and file count.
1113 * Each dentry counts as either a file or a directory, but not both.
1114 * The root directory is an exception: it is not counted at all.
1116 * Symbolic links and junction points (and presumably other reparse
1117 * points) count as regular files. This is despite the fact that
1118 * junction points have FILE_ATTRIBUTE_DIRECTORY set.
1120 if (dentry_is_root(dentry))
1123 if (inode_is_directory(inode))
1129 * Update total bytes and hard link bytes.
1131 * Unfortunately there are some inconsistencies/bugs in the way this is
1134 * If there are no alternate data streams in the image, the "total
1135 * bytes" is the sum of the size of the un-named data stream of each
1136 * inode times the link count of that inode. In other words, it would
1137 * be the total number of bytes of regular files you would have if you
1138 * extracted the full image without any hard-links. The "hard link
1139 * bytes" is equal to the "total bytes" minus the size of the un-named
1140 * data stream of each inode. In other words, the "hard link bytes"
1141 * counts the size of the un-named data stream for all the links to each
1142 * inode except the first one.
1144 * Reparse points and directories don't seem to be counted in either the
1145 * total bytes or the hard link bytes.
1147 * And now we get to the most confusing part, the alternate data
1148 * streams. They are not counted in the "total bytes". However, if the
1149 * link count of an inode with alternate data streams is 2 or greater,
1150 * the size of all the alternate data streams is included in the "hard
1151 * link bytes", and this size is multiplied by the link count (NOT one
1152 * less than the link count).
1154 lte = inode_unnamed_lte(inode, info->lookup_table);
1156 info->total_bytes += wim_resource_size(lte);
1157 if (!dentry_is_first_in_inode(dentry))
1158 info->hard_link_bytes += wim_resource_size(lte);
1161 if (inode->i_nlink >= 2 && dentry_is_first_in_inode(dentry)) {
1162 for (unsigned i = 0; i < inode->i_num_ads; i++) {
1163 if (inode->i_ads_entries[i].stream_name_nbytes) {
1164 lte = inode_stream_lte(inode, i + 1, info->lookup_table);
1166 info->hard_link_bytes += inode->i_nlink *
1167 wim_resource_size(lte);
1176 * Calculate what to put in the <FILECOUNT>, <DIRCOUNT>, <TOTALBYTES>, and
1177 * <HARDLINKBYTES> elements of each <IMAGE>.
1179 * Please note there is no official documentation for exactly how this is done.
1180 * But, see calculate_dentry_statistics().
1183 xml_update_image_info(WIMStruct *wim, int image)
1185 struct image_info *image_info;
1187 DEBUG("Updating the image info for image %d", image);
1189 image_info = &wim->wim_info->images[image - 1];
1191 image_info->file_count = 0;
1192 image_info->dir_count = 0;
1193 image_info->total_bytes = 0;
1194 image_info->hard_link_bytes = 0;
1195 image_info->lookup_table = wim->lookup_table;
1197 for_dentry_in_tree(wim->image_metadata[image - 1]->root_dentry,
1198 calculate_dentry_statistics,
1200 image_info->last_modification_time = get_wim_timestamp();
1203 /* Adds an image to the XML information. */
1205 xml_add_image(WIMStruct *wim, const tchar *name)
1207 struct wim_info *wim_info;
1208 struct image_info *image_info;
1210 wimlib_assert(name != NULL);
1212 /* If this is the first image, allocate the struct wim_info. Otherwise
1213 * use the existing struct wim_info. */
1214 if (wim->wim_info) {
1215 wim_info = wim->wim_info;
1217 wim_info = CALLOC(1, sizeof(struct wim_info));
1219 return WIMLIB_ERR_NOMEM;
1222 image_info = add_image_info_struct(wim_info);
1224 goto out_free_wim_info;
1226 if (!(image_info->name = TSTRDUP(name)))
1227 goto out_destroy_image_info;
1229 wim->wim_info = wim_info;
1230 image_info->index = wim_info->num_images;
1231 image_info->creation_time = get_wim_timestamp();
1232 xml_update_image_info(wim, image_info->index);
1235 out_destroy_image_info:
1236 destroy_image_info(image_info);
1237 wim_info->num_images--;
1239 if (wim_info != wim->wim_info)
1241 return WIMLIB_ERR_NOMEM;
1244 /* Prints information about the specified image from struct wim_info structure.
1247 print_image_info(const struct wim_info *wim_info, int image)
1249 const struct image_info *image_info;
1253 wimlib_assert(image >= 1 && image <= wim_info->num_images);
1255 image_info = &wim_info->images[image - 1];
1257 tprintf(T("Index: %d\n"), image_info->index);
1258 tprintf(T("Name: %"TS"\n"), image_info->name);
1260 /* Always print the Description: part even if there is no
1262 if (image_info->description)
1263 desc = image_info->description;
1266 tprintf(T("Description: %"TS"\n"), desc);
1268 if (image_info->display_name) {
1269 tprintf(T("Display Name: %"TS"\n"),
1270 image_info->display_name);
1273 if (image_info->display_description) {
1274 tprintf(T("Display Description: %"TS"\n"),
1275 image_info->display_description);
1278 tprintf(T("Directory Count: %"PRIu64"\n"), image_info->dir_count);
1279 tprintf(T("File Count: %"PRIu64"\n"), image_info->file_count);
1280 tprintf(T("Total Bytes: %"PRIu64"\n"), image_info->total_bytes);
1281 tprintf(T("Hard Link Bytes: %"PRIu64"\n"), image_info->hard_link_bytes);
1283 wim_timestamp_to_str(image_info->creation_time, buf, sizeof(buf));
1284 tprintf(T("Creation Time: %"TS"\n"), buf);
1286 wim_timestamp_to_str(image_info->last_modification_time, buf, sizeof(buf));
1287 tprintf(T("Last Modification Time: %"TS"\n"), buf);
1288 if (image_info->windows_info_exists)
1289 print_windows_info(&image_info->windows_info);
1290 if (image_info->flags)
1291 tprintf(T("Flags: %"TS"\n"), image_info->flags);
1296 libxml_global_init(void)
1299 xmlInitCharEncodingHandlers();
1303 libxml_global_cleanup(void)
1306 xmlCleanupCharEncodingHandlers();
1309 /* Reads the XML data from a WIM file. */
1311 read_wim_xml_data(WIMStruct *wim)
1318 const struct resource_entry *res_entry;
1320 res_entry = &wim->hdr.xml_res_entry;
1322 DEBUG("Reading XML data: %"PRIu64" bytes at offset %"PRIu64"",
1323 (u64)res_entry->size, res_entry->offset);
1325 ret = res_entry_to_data(res_entry, wim, &buf);
1330 doc = xmlReadMemory((const char *)xml_data, res_entry->original_size,
1331 NULL, "UTF-16LE", 0);
1333 ERROR("Failed to parse XML data");
1334 ret = WIMLIB_ERR_XML;
1335 goto out_free_xml_data;
1338 root = xmlDocGetRootElement(doc);
1339 if (!root || !node_is_element(root) || !node_name_is(root, "WIM")) {
1340 ERROR("WIM XML data is invalid");
1341 ret = WIMLIB_ERR_XML;
1345 ret = xml_read_wim_info(root, &wim->wim_info);
1354 /* Prepares an in-memory buffer containing the UTF-16LE XML data for a WIM file.
1356 * total_bytes is the number to write in <TOTALBYTES>, or
1357 * WIM_TOTALBYTES_USE_EXISTING to use the existing value in memory, or
1358 * WIM_TOTALBYTES_OMIT to omit <TOTALBYTES> entirely.
1361 prepare_wim_xml_data(WIMStruct *wim, int image, u64 total_bytes,
1362 u8 **xml_data_ret, size_t *xml_len_ret)
1364 xmlCharEncodingHandler *encoding_handler;
1366 xmlOutputBuffer *outbuf;
1367 xmlTextWriter *writer;
1370 const xmlChar *content;
1375 /* Open an xmlTextWriter that writes to an in-memory buffer using
1376 * UTF-16LE encoding. */
1378 encoding_handler = xmlGetCharEncodingHandler(XML_CHAR_ENCODING_UTF16LE);
1379 if (!encoding_handler) {
1380 ERROR("Failed to get XML character encoding handler for UTF-16LE");
1381 ret = WIMLIB_ERR_LIBXML_UTF16_HANDLER_NOT_AVAILABLE;
1385 buf = xmlBufferCreate();
1387 ERROR("Failed to create xmlBuffer");
1388 ret = WIMLIB_ERR_NOMEM;
1392 outbuf = xmlOutputBufferCreateBuffer(buf, encoding_handler);
1394 ERROR("Failed to allocate xmlOutputBuffer");
1395 ret = WIMLIB_ERR_NOMEM;
1396 goto out_buffer_free;
1399 writer = xmlNewTextWriter(outbuf);
1401 ERROR("Failed to allocate xmlTextWriter");
1402 ret = WIMLIB_ERR_NOMEM;
1403 goto out_output_buffer_close;
1406 /* Write the XML document. */
1408 ret = xmlTextWriterStartElement(writer, "WIM");
1410 goto out_write_error;
1412 /* The contents of the <TOTALBYTES> element in the XML data, under the
1413 * <WIM> element (not the <IMAGE> element), is for non-split WIMs the
1414 * size of the WIM file excluding the XML data and integrity table.
1415 * For split WIMs, <TOTALBYTES> takes into account the entire WIM, not
1416 * just the current part. */
1417 if (total_bytes != WIM_TOTALBYTES_OMIT) {
1418 if (total_bytes == WIM_TOTALBYTES_USE_EXISTING) {
1420 total_bytes = wim->wim_info->total_bytes;
1424 ret = xmlTextWriterWriteFormatElement(writer, "TOTALBYTES",
1425 "%"PRIu64, total_bytes);
1427 goto out_write_error;
1430 if (image == WIMLIB_ALL_IMAGES) {
1432 last = wim->hdr.image_count;
1438 for (int i = first; i <= last; i++) {
1439 ret = xml_write_image_info(writer, &wim->wim_info->images[i - 1]);
1442 goto out_write_error;
1443 goto out_free_text_writer;
1447 ret = xmlTextWriterEndElement(writer);
1449 goto out_write_error;
1451 ret = xmlTextWriterEndDocument(writer);
1453 goto out_write_error;
1455 ret = xmlTextWriterFlush(writer);
1457 goto out_write_error;
1459 /* Retrieve the buffer into which the document was written. */
1461 content = xmlBufferContent(buf);
1462 len = xmlBufferLength(buf);
1464 /* Copy the data into a new buffer, and prefix it with the UTF-16LE BOM
1465 * (byte order mark), which is required by MS's software to understand
1469 xml_data = MALLOC(xml_len);
1471 ret = WIMLIB_ERR_NOMEM;
1472 goto out_free_text_writer;
1476 memcpy(&xml_data[2], content, len);
1478 /* Clean up libxml objects and return success. */
1479 *xml_data_ret = xml_data;
1480 *xml_len_ret = xml_len;
1482 out_free_text_writer:
1483 /* xmlFreeTextWriter will free the attached xmlOutputBuffer. */
1484 xmlFreeTextWriter(writer);
1485 goto out_buffer_free;
1486 out_output_buffer_close:
1487 xmlOutputBufferClose(outbuf);
1491 DEBUG("ret=%d", ret);
1495 ERROR("Error writing XML data");
1496 ret = WIMLIB_ERR_WRITE;
1497 goto out_free_text_writer;
1500 /* Writes the XML data to a WIM file. */
1502 write_wim_xml_data(WIMStruct *wim, int image, u64 total_bytes,
1503 struct resource_entry *out_res_entry,
1504 int write_resource_flags)
1510 DEBUG("Writing WIM XML data (image=%d, offset=%"PRIu64")",
1511 image, total_bytes, wim->out_fd.offset);
1513 ret = prepare_wim_xml_data(wim, image, total_bytes,
1514 &xml_data, &xml_len);
1518 /* Write the XML data uncompressed. Although wimlib can handle
1519 * compressed XML data, MS software cannot. */
1520 ret = write_wim_resource_from_buffer(xml_data,
1522 WIM_RESHDR_FLAG_METADATA,
1524 WIMLIB_COMPRESSION_TYPE_NONE,
1527 write_resource_flags,
1534 /* API function documented in wimlib.h */
1535 WIMLIBAPI const tchar *
1536 wimlib_get_image_name(const WIMStruct *wim, int image)
1538 if (image < 1 || image > wim->hdr.image_count)
1540 return wim->wim_info->images[image - 1].name;
1543 /* API function documented in wimlib.h */
1544 WIMLIBAPI const tchar *
1545 wimlib_get_image_description(const WIMStruct *wim, int image)
1547 if (image < 1 || image > wim->hdr.image_count)
1549 return wim->wim_info->images[image - 1].description;
1552 /* API function documented in wimlib.h */
1554 wimlib_image_name_in_use(const WIMStruct *wim, const tchar *name)
1556 if (!name || !*name)
1558 for (int i = 1; i <= wim->hdr.image_count; i++)
1559 if (!tstrcmp(wim->wim_info->images[i - 1].name, name))
1565 /* API function documented in wimlib.h */
1567 wimlib_get_xml_data(WIMStruct *wim, void **buf_ret, size_t *bufsize_ret)
1569 if (wim->filename == NULL)
1570 return WIMLIB_ERR_INVALID_PARAM;
1572 if (buf_ret == NULL || bufsize_ret == NULL)
1573 return WIMLIB_ERR_INVALID_PARAM;
1575 *bufsize_ret = wim->hdr.xml_res_entry.original_size;
1576 return res_entry_to_data(&wim->hdr.xml_res_entry, wim, buf_ret);
1580 wimlib_extract_xml_data(WIMStruct *wim, FILE *fp)
1586 ret = wimlib_get_xml_data(wim, &buf, &bufsize);
1590 if (fwrite(buf, 1, bufsize, fp) != bufsize) {
1591 ERROR_WITH_ERRNO("Failed to extract XML data");
1592 ret = WIMLIB_ERR_WRITE;
1598 /* API function documented in wimlib.h */
1600 wimlib_set_image_name(WIMStruct *wim, int image, const tchar *name)
1606 DEBUG("Setting the name of image %d to %"TS, image, name);
1608 ret = can_modify_wim(wim);
1615 if (image < 1 || image > wim->hdr.image_count) {
1616 ERROR("%d is not a valid image", image);
1617 return WIMLIB_ERR_INVALID_IMAGE;
1620 for (i = 1; i <= wim->hdr.image_count; i++) {
1623 if (!tstrcmp(wim->wim_info->images[i - 1].name, name)) {
1624 ERROR("The name \"%"TS"\" is already in use in the WIM!",
1626 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
1632 return WIMLIB_ERR_NOMEM;
1634 FREE(wim->wim_info->images[image - 1].name);
1635 wim->wim_info->images[image - 1].name = p;
1640 do_set_image_info_str(WIMStruct *wim, int image, const tchar *tstr,
1644 tchar **dest_tstr_p;
1647 ret = can_modify_wim(wim);
1651 if (image < 1 || image > wim->hdr.image_count) {
1652 ERROR("%d is not a valid image", image);
1653 return WIMLIB_ERR_INVALID_IMAGE;
1656 tstr_copy = TSTRDUP(tstr);
1658 return WIMLIB_ERR_NOMEM;
1662 dest_tstr_p = (tchar**)((void*)&wim->wim_info->images[image - 1] + offset);
1665 *dest_tstr_p = tstr_copy;
1669 /* API function documented in wimlib.h */
1671 wimlib_set_image_descripton(WIMStruct *wim, int image,
1672 const tchar *description)
1674 return do_set_image_info_str(wim, image, description,
1675 offsetof(struct image_info, description));
1678 /* API function documented in wimlib.h */
1680 wimlib_set_image_flags(WIMStruct *wim, int image, const tchar *flags)
1682 return do_set_image_info_str(wim, image, flags,
1683 offsetof(struct image_info, flags));