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_hard_link_bytes(const struct wim_info *info, int image)
143 return info->images[image - 1].hard_link_bytes;
147 wim_info_get_image_total_bytes(const struct wim_info *info, int image)
149 return info->images[image - 1].total_bytes;
153 wim_info_get_num_images(const struct wim_info *info)
155 return info->num_images;
158 /* Returns a statically allocated string that is a string representation of the
159 * architecture number. */
170 /* XXX Are there other arch values? */
177 /* Iterate through the children of an xmlNode. */
178 #define for_node_child(parent, child) \
179 for (child = parent->children; child != NULL; child = child->next)
181 /* Utility functions for xmlNodes */
183 node_is_element(xmlNode *node)
185 return node->type == XML_ELEMENT_NODE;
189 node_is_text(xmlNode *node)
191 return node->type == XML_TEXT_NODE;
195 node_name_is(xmlNode *node, const char *name)
197 /* For now, both upper case and lower case element names are accepted. */
198 return strcasecmp((const char *)node->name, name) == 0;
202 node_get_number(const xmlNode *u64_node, int base)
205 for_node_child(u64_node, child)
206 if (node_is_text(child))
207 return strtoull(child->content, NULL, base);
211 /* Finds the text node that is a child of an element node and returns its
212 * content converted to a 64-bit unsigned integer. Returns 0 if no text node is
215 node_get_u64(const xmlNode *u64_node)
217 return node_get_number(u64_node, 10);
220 /* Like node_get_u64(), but expects a number in base 16. */
222 node_get_hex_u64(const xmlNode *u64_node)
224 return node_get_number(u64_node, 16);
228 node_get_string(const xmlNode *string_node, tchar **tstr_ret)
234 for_node_child(string_node, child) {
235 if (node_is_text(child) && child->content) {
236 ret = utf8_to_tstr_simple(child->content, &tstr);
246 /* Returns the timestamp from a time node. It has child elements <HIGHPART> and
247 * <LOWPART> that are then used to construct a 64-bit timestamp. */
249 node_get_timestamp(const xmlNode *time_node)
254 for_node_child(time_node, child) {
255 if (!node_is_element(child))
257 if (node_name_is(child, "HIGHPART"))
258 high_part = node_get_hex_u64(child);
259 else if (node_name_is(child, "LOWPART"))
260 low_part = node_get_hex_u64(child);
262 return (u64)low_part | ((u64)high_part << 32);
265 /* Used to sort an array of struct image_infos by their image indices. */
267 sort_by_index(const void *p1, const void *p2)
269 int index_1 = ((const struct image_info*)p1)->index;
270 int index_2 = ((const struct image_info*)p2)->index;
271 if (index_1 < index_2)
273 else if (index_1 > index_2)
280 /* Frees memory allocated inside a struct windows_info structure. */
282 destroy_windows_info(struct windows_info *windows_info)
284 FREE(windows_info->product_name);
285 FREE(windows_info->edition_id);
286 FREE(windows_info->installation_type);
287 FREE(windows_info->hal);
288 FREE(windows_info->product_type);
289 FREE(windows_info->product_suite);
290 for (size_t i = 0; i < windows_info->num_languages; i++)
291 FREE(windows_info->languages[i]);
292 FREE(windows_info->languages);
293 FREE(windows_info->default_language);
294 FREE(windows_info->system_root);
297 /* Frees memory allocated inside a struct image_info structure. */
299 destroy_image_info(struct image_info *image_info)
301 FREE(image_info->name);
302 FREE(image_info->description);
303 FREE(image_info->flags);
304 FREE(image_info->display_name);
305 FREE(image_info->display_description);
306 destroy_windows_info(&image_info->windows_info);
307 memset(image_info, 0, sizeof(struct image_info));
311 free_wim_info(struct wim_info *info)
315 for (int i = 0; i < info->num_images; i++)
316 destroy_image_info(&info->images[i]);
323 /* Reads the information from a <VERSION> element inside the <WINDOWS> element.
326 xml_read_windows_version(const xmlNode *version_node,
327 struct windows_version* windows_version)
330 for_node_child(version_node, child) {
331 if (!node_is_element(child))
333 if (node_name_is(child, "MAJOR"))
334 windows_version->major = node_get_u64(child);
335 else if (node_name_is(child, "MINOR"))
336 windows_version->minor = node_get_u64(child);
337 else if (node_name_is(child, "BUILD"))
338 windows_version->build = node_get_u64(child);
339 else if (node_name_is(child, "SPBUILD"))
340 windows_version->sp_build = node_get_u64(child);
341 else if (node_name_is(child, "SPLEVEL"))
342 windows_version->sp_level = node_get_u64(child);
346 /* Reads the information from a <LANGUAGE> element inside a <WINDOWS> element.
349 xml_read_languages(const xmlNode *languages_node,
350 tchar ***languages_ret,
351 size_t *num_languages_ret,
352 tchar **default_language_ret)
355 size_t num_languages = 0;
359 for_node_child(languages_node, child)
360 if (node_is_element(child) && node_name_is(child, "LANGUAGE"))
363 languages = CALLOC(num_languages, sizeof(languages[0]));
365 return WIMLIB_ERR_NOMEM;
367 *languages_ret = languages;
368 *num_languages_ret = num_languages;
371 for_node_child(languages_node, child) {
372 if (!node_is_element(child))
374 if (node_name_is(child, "LANGUAGE"))
375 ret = node_get_string(child, languages++);
376 else if (node_name_is(child, "DEFAULT"))
377 ret = node_get_string(child, default_language_ret);
384 /* Reads the information from a <WINDOWS> element inside an <IMAGE> element. */
386 xml_read_windows_info(const xmlNode *windows_node,
387 struct windows_info *windows_info)
392 for_node_child(windows_node, child) {
393 if (!node_is_element(child))
395 if (node_name_is(child, "ARCH")) {
396 windows_info->arch = node_get_u64(child);
397 } else if (node_name_is(child, "PRODUCTNAME")) {
398 ret = node_get_string(child,
399 &windows_info->product_name);
400 } else if (node_name_is(child, "EDITIONID")) {
401 ret = node_get_string(child,
402 &windows_info->edition_id);
403 } else if (node_name_is(child, "INSTALLATIONTYPE")) {
404 ret = node_get_string(child,
405 &windows_info->installation_type);
406 } else if (node_name_is(child, "PRODUCTTYPE")) {
407 ret = node_get_string(child,
408 &windows_info->product_type);
409 } else if (node_name_is(child, "PRODUCTSUITE")) {
410 ret = node_get_string(child,
411 &windows_info->product_suite);
412 } else if (node_name_is(child, "LANGUAGES")) {
413 ret = xml_read_languages(child,
414 &windows_info->languages,
415 &windows_info->num_languages,
416 &windows_info->default_language);
417 } else if (node_name_is(child, "VERSION")) {
418 xml_read_windows_version(child,
419 &windows_info->windows_version);
420 windows_info->windows_version_exists = true;
421 } else if (node_name_is(child, "SYSTEMROOT")) {
422 ret = node_get_string(child, &windows_info->system_root);
423 } else if (node_name_is(child, "HAL")) {
424 ret = node_get_string(child, &windows_info->hal);
432 /* Reads the information from an <IMAGE> element. */
434 xml_read_image_info(xmlNode *image_node, struct image_info *image_info)
440 index_prop = xmlGetProp(image_node, "INDEX");
442 image_info->index = atoi(index_prop);
445 image_info->index = 1;
449 for_node_child(image_node, child) {
450 if (!node_is_element(child))
452 if (node_name_is(child, "DIRCOUNT"))
453 image_info->dir_count = node_get_u64(child);
454 else if (node_name_is(child, "FILECOUNT"))
455 image_info->file_count = node_get_u64(child);
456 else if (node_name_is(child, "TOTALBYTES"))
457 image_info->total_bytes = node_get_u64(child);
458 else if (node_name_is(child, "HARDLINKBYTES"))
459 image_info->hard_link_bytes = node_get_u64(child);
460 else if (node_name_is(child, "CREATIONTIME"))
461 image_info->creation_time = node_get_timestamp(child);
462 else if (node_name_is(child, "LASTMODIFICATIONTIME"))
463 image_info->last_modification_time = node_get_timestamp(child);
464 else if (node_name_is(child, "WINDOWS")) {
465 DEBUG("Found <WINDOWS> tag");
466 ret = xml_read_windows_info(child,
467 &image_info->windows_info);
468 image_info->windows_info_exists = true;
469 } else if (node_name_is(child, "NAME")) {
470 ret = node_get_string(child, &image_info->name);
471 } else if (node_name_is(child, "DESCRIPTION")) {
472 ret = node_get_string(child, &image_info->description);
473 } else if (node_name_is(child, "FLAGS")) {
474 ret = node_get_string(child, &image_info->flags);
475 } else if (node_name_is(child, "DISPLAYNAME")) {
476 ret = node_get_string(child, &image_info->display_name);
477 } else if (node_name_is(child, "DISPLAYDESCRIPTION")) {
478 ret = node_get_string(child, &image_info->display_description);
483 if (!image_info->name) {
485 /*WARNING("Image with index %d has no name", image_info->index);*/
486 empty_name = MALLOC(sizeof(tchar));
488 return WIMLIB_ERR_NOMEM;
489 *empty_name = T('\0');
490 image_info->name = empty_name;
495 /* Reads the information from a <WIM> element, which should be the root element
496 * of the XML tree. */
498 xml_read_wim_info(const xmlNode *wim_node, struct wim_info **wim_info_ret)
500 struct wim_info *wim_info;
506 wim_info = CALLOC(1, sizeof(struct wim_info));
508 return WIMLIB_ERR_NOMEM;
510 /* Count how many images there are. */
512 for_node_child(wim_node, child) {
513 if (node_is_element(child) && node_name_is(child, "IMAGE")) {
514 if (num_images == INT_MAX) {
515 return WIMLIB_ERR_IMAGE_COUNT;
521 if (num_images > 0) {
522 /* Allocate the array of struct image_infos and fill them in. */
523 wim_info->images = CALLOC(num_images, sizeof(wim_info->images[0]));
524 if (!wim_info->images) {
525 ret = WIMLIB_ERR_NOMEM;
528 wim_info->num_images = num_images;
530 for_node_child(wim_node, child) {
531 if (!node_is_element(child))
533 if (node_name_is(child, "IMAGE")) {
534 DEBUG("Found <IMAGE> tag");
535 ret = xml_read_image_info(child,
536 &wim_info->images[i]);
540 } else if (node_name_is(child, "TOTALBYTES")) {
541 wim_info->total_bytes = node_get_u64(child);
545 /* Sort the array of image info by image index. */
546 qsort(wim_info->images, num_images,
547 sizeof(struct image_info), sort_by_index);
549 /* Make sure the image indices make sense */
550 for (i = 0; i < num_images; i++) {
551 if (wim_info->images[i].index != i + 1) {
552 ERROR("WIM images are not indexed [1...%d] "
553 "in XML data as expected",
555 return WIMLIB_ERR_IMAGE_COUNT;
560 *wim_info_ret = wim_info;
563 free_wim_info(wim_info);
567 /* Prints the information contained in a `struct windows_info'.
569 * Warning: any strings printed here are in UTF-8 encoding. If the locale
570 * character encoding is not UTF-8, the printed strings may be garbled. */
572 print_windows_info(const struct windows_info *windows_info)
574 const struct windows_version *windows_version;
576 tprintf(T("Architecture: %"TS"\n"),
577 get_arch(windows_info->arch));
579 if (windows_info->product_name) {
580 tprintf(T("Product Name: %"TS"\n"),
581 windows_info->product_name);
584 if (windows_info->edition_id) {
585 tprintf(T("Edition ID: %"TS"\n"),
586 windows_info->edition_id);
589 if (windows_info->installation_type) {
590 tprintf(T("Installation Type: %"TS"\n"),
591 windows_info->installation_type);
594 if (windows_info->hal) {
595 tprintf(T("HAL: %"TS"\n"),
599 if (windows_info->product_type) {
600 tprintf(T("Product Type: %"TS"\n"),
601 windows_info->product_type);
604 if (windows_info->product_suite) {
605 tprintf(T("Product Suite: %"TS"\n"),
606 windows_info->product_suite);
609 tprintf(T("Languages: "));
610 for (size_t i = 0; i < windows_info->num_languages; i++) {
612 tfputs(windows_info->languages[i], stdout);
616 if (windows_info->default_language) {
617 tprintf(T("Default Language: %"TS"\n"),
618 windows_info->default_language);
620 if (windows_info->system_root) {
621 tprintf(T("System Root: %"TS"\n"),
622 windows_info->system_root);
625 if (windows_info->windows_version_exists) {
626 windows_version = &windows_info->windows_version;
627 tprintf(T("Major Version: %"PRIu64"\n"),
628 windows_version->major);
629 tprintf(T("Minor Version: %"PRIu64"\n"),
630 windows_version->minor);
631 tprintf(T("Build: %"PRIu64"\n"),
632 windows_version->build);
633 tprintf(T("Service Pack Build: %"PRIu64"\n"),
634 windows_version->sp_build);
635 tprintf(T("Service Pack Level: %"PRIu64"\n"),
636 windows_version->sp_level);
641 xml_write_string(xmlTextWriter *writer, const char *name,
646 int rc = tstr_to_utf8_simple(tstr, &utf8_str);
649 rc = xmlTextWriterWriteElement(writer, name, utf8_str);
658 xml_write_strings_from_specs(xmlTextWriter *writer,
659 const void *struct_with_strings,
660 const struct xml_string_spec specs[],
663 for (size_t i = 0; i < num_specs; i++) {
664 int rc = xml_write_string(writer, specs[i].name,
665 *(const tchar * const *)
666 (struct_with_strings + specs[i].offset));
674 dup_strings_from_specs(const void *old_struct_with_strings,
675 void *new_struct_with_strings,
676 const struct xml_string_spec specs[],
679 for (size_t i = 0; i < num_specs; i++) {
680 const tchar *old_str = *(const tchar * const *)
681 ((const void*)old_struct_with_strings + specs[i].offset);
682 tchar **new_str_p = (tchar **)((void*)new_struct_with_strings + specs[i].offset);
684 *new_str_p = TSTRDUP(old_str);
686 return WIMLIB_ERR_NOMEM;
692 /* Writes the information contained in a `struct windows_version' to the XML
693 * document being written. This is the <VERSION> element inside the <WINDOWS>
696 xml_write_windows_version(xmlTextWriter *writer,
697 const struct windows_version *version)
700 rc = xmlTextWriterStartElement(writer, "VERSION");
704 rc = xmlTextWriterWriteFormatElement(writer, "MAJOR", "%"PRIu64,
709 rc = xmlTextWriterWriteFormatElement(writer, "MINOR", "%"PRIu64,
714 rc = xmlTextWriterWriteFormatElement(writer, "BUILD", "%"PRIu64,
719 rc = xmlTextWriterWriteFormatElement(writer, "SPBUILD", "%"PRIu64,
724 rc = xmlTextWriterWriteFormatElement(writer, "SPLEVEL", "%"PRIu64,
729 return xmlTextWriterEndElement(writer); /* </VERSION> */
732 /* Writes the information contained in a `struct windows_info' to the XML
733 * document being written. This is the <WINDOWS> element. */
735 xml_write_windows_info(xmlTextWriter *writer,
736 const struct windows_info *windows_info)
739 rc = xmlTextWriterStartElement(writer, "WINDOWS");
743 rc = xmlTextWriterWriteFormatElement(writer, "ARCH", "%"PRIu64,
748 rc = xml_write_strings_from_specs(writer,
750 windows_info_xml_string_specs,
751 ARRAY_LEN(windows_info_xml_string_specs));
755 if (windows_info->num_languages) {
756 rc = xmlTextWriterStartElement(writer, "LANGUAGES");
760 for (size_t i = 0; i < windows_info->num_languages; i++) {
761 rc = xml_write_string(writer, "LANGUAGE",
762 windows_info->languages[i]);
767 rc = xml_write_string(writer, "DEFAULT",
768 windows_info->default_language);
772 rc = xmlTextWriterEndElement(writer); /* </LANGUAGES> */
777 if (windows_info->windows_version_exists) {
778 rc = xml_write_windows_version(writer, &windows_info->windows_version);
783 rc = xml_write_string(writer, "SYSTEMROOT",
784 windows_info->system_root);
788 return xmlTextWriterEndElement(writer); /* </WINDOWS> */
791 /* Writes a time element to the XML document being constructed in memory. */
793 xml_write_time(xmlTextWriter *writer, const char *element_name, u64 time)
796 rc = xmlTextWriterStartElement(writer, element_name);
800 rc = xmlTextWriterWriteFormatElement(writer, "HIGHPART",
801 "0x%08"PRIX32, (u32)(time >> 32));
805 rc = xmlTextWriterWriteFormatElement(writer, "LOWPART",
806 "0x%08"PRIX32, (u32)time);
810 rc = xmlTextWriterEndElement(writer); /* </@element_name> */
816 /* Writes an <IMAGE> element to the XML document. */
818 xml_write_image_info(xmlTextWriter *writer, const struct image_info *image_info)
821 rc = xmlTextWriterStartElement(writer, "IMAGE");
825 rc = xmlTextWriterWriteFormatAttribute(writer, "INDEX", "%d",
830 rc = xmlTextWriterWriteFormatElement(writer, "DIRCOUNT", "%"PRIu64,
831 image_info->dir_count);
835 rc = xmlTextWriterWriteFormatElement(writer, "FILECOUNT", "%"PRIu64,
836 image_info->file_count);
840 rc = xmlTextWriterWriteFormatElement(writer, "TOTALBYTES", "%"PRIu64,
841 image_info->total_bytes);
845 rc = xmlTextWriterWriteFormatElement(writer, "HARDLINKBYTES", "%"PRIu64,
846 image_info->hard_link_bytes);
850 rc = xml_write_time(writer, "CREATIONTIME", image_info->creation_time);
854 rc = xml_write_time(writer, "LASTMODIFICATIONTIME",
855 image_info->last_modification_time);
859 if (image_info->windows_info_exists) {
860 rc = xml_write_windows_info(writer, &image_info->windows_info);
865 rc = xml_write_strings_from_specs(writer, image_info,
866 image_info_xml_string_specs,
867 ARRAY_LEN(image_info_xml_string_specs));
871 rc = xmlTextWriterEndElement(writer); /* </IMAGE> */
879 /* Makes space for another image in the XML information and return a pointer to
881 static struct image_info *
882 add_image_info_struct(struct wim_info *wim_info)
884 struct image_info *images;
886 images = CALLOC(wim_info->num_images + 1, sizeof(struct image_info));
889 memcpy(images, wim_info->images,
890 wim_info->num_images * sizeof(struct image_info));
891 FREE(wim_info->images);
892 wim_info->images = images;
893 wim_info->num_images++;
894 return &images[wim_info->num_images - 1];
898 clone_windows_info(const struct windows_info *old, struct windows_info *new)
902 ret = dup_strings_from_specs(old, new, windows_info_xml_string_specs,
903 ARRAY_LEN(windows_info_xml_string_specs));
907 if (old->languages) {
908 new->languages = CALLOC(old->num_languages, sizeof(new->languages[0]));
910 return WIMLIB_ERR_NOMEM;
911 new->num_languages = old->num_languages;
912 for (size_t i = 0; i < new->num_languages; i++) {
913 if (!old->languages[i])
915 new->languages[i] = TSTRDUP(old->languages[i]);
916 if (!new->languages[i])
917 return WIMLIB_ERR_NOMEM;
920 if (old->default_language &&
921 !(new->default_language = TSTRDUP(old->default_language)))
922 return WIMLIB_ERR_NOMEM;
923 if (old->system_root && !(new->system_root = TSTRDUP(old->system_root)))
924 return WIMLIB_ERR_NOMEM;
925 if (old->windows_version_exists) {
926 new->windows_version_exists = true;
927 memcpy(&new->windows_version, &old->windows_version,
928 sizeof(old->windows_version));
934 clone_image_info(const struct image_info *old, struct image_info *new)
938 new->dir_count = old->dir_count;
939 new->file_count = old->file_count;
940 new->total_bytes = old->total_bytes;
941 new->hard_link_bytes = old->hard_link_bytes;
942 new->creation_time = old->creation_time;
943 new->last_modification_time = old->last_modification_time;
945 ret = dup_strings_from_specs(old, new,
946 image_info_xml_string_specs,
947 ARRAY_LEN(image_info_xml_string_specs));
951 if (old->windows_info_exists) {
952 new->windows_info_exists = true;
953 ret = clone_windows_info(&old->windows_info,
961 /* Copies the XML information for an image between WIM files.
963 * @dest_image_name and @dest_image_description are ignored if they are NULL;
964 * otherwise, they are used to override the image name and/or image description
965 * from the XML data in the source WIM file.
967 * On failure, WIMLIB_ERR_NOMEM is returned and no changes are made. Otherwise,
968 * 0 is returned and the WIM information at *new_wim_info_p is modified.
971 xml_export_image(const struct wim_info *old_wim_info,
973 struct wim_info **new_wim_info_p,
974 const tchar *dest_image_name,
975 const tchar *dest_image_description)
977 struct wim_info *new_wim_info;
978 struct image_info *image_info;
981 DEBUG("Copying XML data between WIM files for source image %d.", image);
983 wimlib_assert(old_wim_info != NULL);
984 wimlib_assert(image >= 1 && image <= old_wim_info->num_images);
986 if (*new_wim_info_p) {
987 new_wim_info = *new_wim_info_p;
989 new_wim_info = CALLOC(1, sizeof(struct wim_info));
994 image_info = add_image_info_struct(new_wim_info);
998 ret = clone_image_info(&old_wim_info->images[image - 1], image_info);
1000 goto err_destroy_image_info;
1002 image_info->index = new_wim_info->num_images;
1004 if (dest_image_name) {
1005 FREE(image_info->name);
1006 image_info->name = TSTRDUP(dest_image_name);
1007 if (!image_info->name)
1008 goto err_destroy_image_info;
1010 if (dest_image_description) {
1011 FREE(image_info->description);
1012 image_info->description = TSTRDUP(dest_image_description);
1013 if (!image_info->description)
1014 goto err_destroy_image_info;
1016 *new_wim_info_p = new_wim_info;
1018 err_destroy_image_info:
1019 destroy_image_info(image_info);
1021 if (new_wim_info != *new_wim_info_p)
1022 free_wim_info(new_wim_info);
1023 return WIMLIB_ERR_NOMEM;
1026 /* Removes an image from the XML information. */
1028 xml_delete_image(struct wim_info **wim_info_p, int image)
1030 struct wim_info *wim_info;
1032 wim_info = *wim_info_p;
1033 wimlib_assert(image >= 1 && image <= wim_info->num_images);
1034 DEBUG("Deleting image %d from the XML data.", image);
1036 destroy_image_info(&wim_info->images[image - 1]);
1038 memmove(&wim_info->images[image - 1],
1039 &wim_info->images[image],
1040 (wim_info->num_images - image) * sizeof(struct image_info));
1042 if (--wim_info->num_images == 0) {
1043 free_wim_info(wim_info);
1046 for (int i = image - 1; i < wim_info->num_images; i++)
1047 wim_info->images[i].index--;
1052 xml_get_max_image_name_len(const WIMStruct *wim)
1055 for (u32 i = 0; i < wim->hdr.image_count; i++)
1056 max_len = max(max_len, tstrlen(wim->wim_info->images[i].name));
1060 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
1062 xml_set_memory_allocator(void *(*malloc_func)(size_t),
1063 void (*free_func)(void *),
1064 void *(*realloc_func)(void *, size_t))
1066 xmlMemSetup(free_func, malloc_func, realloc_func, STRDUP);
1071 calculate_dentry_statistics(struct wim_dentry *dentry, void *arg)
1073 struct image_info *info = arg;
1074 const struct wim_inode *inode = dentry->d_inode;
1075 struct wim_lookup_table_entry *lte;
1077 /* Update directory count and file count.
1079 * Each dentry counts as either a file or a directory, but not both.
1080 * The root directory is an exception: it is not counted at all.
1082 * Symbolic links and junction points (and presumably other reparse
1083 * points) count as regular files. This is despite the fact that
1084 * junction points have FILE_ATTRIBUTE_DIRECTORY set.
1086 if (dentry_is_root(dentry))
1089 if (inode_is_directory(inode))
1095 * Update total bytes and hard link bytes.
1097 * Unfortunately there are some inconsistencies/bugs in the way this is
1100 * If there are no alternate data streams in the image, the "total
1101 * bytes" is the sum of the size of the un-named data stream of each
1102 * inode times the link count of that inode. In other words, it would
1103 * be the total number of bytes of regular files you would have if you
1104 * extracted the full image without any hard-links. The "hard link
1105 * bytes" is equal to the "total bytes" minus the size of the un-named
1106 * data stream of each inode. In other words, the "hard link bytes"
1107 * counts the size of the un-named data stream for all the links to each
1108 * inode except the first one.
1110 * Reparse points and directories don't seem to be counted in either the
1111 * total bytes or the hard link bytes.
1113 * And now we get to the most confusing part, the alternate data
1114 * streams. They are not counted in the "total bytes". However, if the
1115 * link count of an inode with alternate data streams is 2 or greater,
1116 * the size of all the alternate data streams is included in the "hard
1117 * link bytes", and this size is multiplied by the link count (NOT one
1118 * less than the link count).
1120 lte = inode_unnamed_lte(inode, info->lookup_table);
1122 info->total_bytes += wim_resource_size(lte);
1123 if (!dentry_is_first_in_inode(dentry))
1124 info->hard_link_bytes += wim_resource_size(lte);
1127 if (inode->i_nlink >= 2 && dentry_is_first_in_inode(dentry)) {
1128 for (unsigned i = 0; i < inode->i_num_ads; i++) {
1129 if (inode->i_ads_entries[i].stream_name_nbytes) {
1130 lte = inode_stream_lte(inode, i + 1, info->lookup_table);
1132 info->hard_link_bytes += inode->i_nlink *
1133 wim_resource_size(lte);
1142 * Calculate what to put in the <FILECOUNT>, <DIRCOUNT>, <TOTALBYTES>, and
1143 * <HARDLINKBYTES> elements of each <IMAGE>.
1145 * Please note there is no official documentation for exactly how this is done.
1146 * But, see calculate_dentry_statistics().
1149 xml_update_image_info(WIMStruct *wim, int image)
1151 struct image_info *image_info;
1153 DEBUG("Updating the image info for image %d", image);
1155 image_info = &wim->wim_info->images[image - 1];
1157 image_info->file_count = 0;
1158 image_info->dir_count = 0;
1159 image_info->total_bytes = 0;
1160 image_info->hard_link_bytes = 0;
1161 image_info->lookup_table = wim->lookup_table;
1163 for_dentry_in_tree(wim->image_metadata[image - 1]->root_dentry,
1164 calculate_dentry_statistics,
1166 image_info->last_modification_time = get_wim_timestamp();
1169 /* Adds an image to the XML information. */
1171 xml_add_image(WIMStruct *wim, const tchar *name)
1173 struct wim_info *wim_info;
1174 struct image_info *image_info;
1176 wimlib_assert(name != NULL);
1178 /* If this is the first image, allocate the struct wim_info. Otherwise
1179 * use the existing struct wim_info. */
1180 if (wim->wim_info) {
1181 wim_info = wim->wim_info;
1183 wim_info = CALLOC(1, sizeof(struct wim_info));
1185 return WIMLIB_ERR_NOMEM;
1188 image_info = add_image_info_struct(wim_info);
1190 goto out_free_wim_info;
1192 if (!(image_info->name = TSTRDUP(name)))
1193 goto out_destroy_image_info;
1195 wim->wim_info = wim_info;
1196 image_info->index = wim_info->num_images;
1197 image_info->creation_time = get_wim_timestamp();
1198 xml_update_image_info(wim, image_info->index);
1201 out_destroy_image_info:
1202 destroy_image_info(image_info);
1203 wim_info->num_images--;
1205 if (wim_info != wim->wim_info)
1207 return WIMLIB_ERR_NOMEM;
1210 /* Prints information about the specified image from struct wim_info structure.
1213 print_image_info(const struct wim_info *wim_info, int image)
1215 const struct image_info *image_info;
1219 wimlib_assert(image >= 1 && image <= wim_info->num_images);
1221 image_info = &wim_info->images[image - 1];
1223 tprintf(T("Index: %d\n"), image_info->index);
1224 tprintf(T("Name: %"TS"\n"), image_info->name);
1226 /* Always print the Description: part even if there is no
1228 if (image_info->description)
1229 desc = image_info->description;
1232 tprintf(T("Description: %"TS"\n"), desc);
1234 if (image_info->display_name) {
1235 tprintf(T("Display Name: %"TS"\n"),
1236 image_info->display_name);
1239 if (image_info->display_description) {
1240 tprintf(T("Display Description: %"TS"\n"),
1241 image_info->display_description);
1244 tprintf(T("Directory Count: %"PRIu64"\n"), image_info->dir_count);
1245 tprintf(T("File Count: %"PRIu64"\n"), image_info->file_count);
1246 tprintf(T("Total Bytes: %"PRIu64"\n"), image_info->total_bytes);
1247 tprintf(T("Hard Link Bytes: %"PRIu64"\n"), image_info->hard_link_bytes);
1249 wim_timestamp_to_str(image_info->creation_time, buf, sizeof(buf));
1250 tprintf(T("Creation Time: %"TS"\n"), buf);
1252 wim_timestamp_to_str(image_info->last_modification_time, buf, sizeof(buf));
1253 tprintf(T("Last Modification Time: %"TS"\n"), buf);
1254 if (image_info->windows_info_exists)
1255 print_windows_info(&image_info->windows_info);
1256 if (image_info->flags)
1257 tprintf(T("Flags: %"TS"\n"), image_info->flags);
1262 libxml_global_init(void)
1265 xmlInitCharEncodingHandlers();
1269 libxml_global_cleanup(void)
1272 xmlCleanupCharEncodingHandlers();
1275 /* Reads the XML data from a WIM file. */
1277 read_wim_xml_data(WIMStruct *wim)
1283 const struct resource_entry *res_entry;
1285 res_entry = &wim->hdr.xml_res_entry;
1287 DEBUG("Reading XML data: %"PRIu64" bytes at offset %"PRIu64"",
1288 (u64)res_entry->size, res_entry->offset);
1290 ret = res_entry_to_data(res_entry, wim, (void**)&xml_data);
1294 doc = xmlReadMemory((const char *)xml_data, res_entry->original_size,
1295 NULL, "UTF-16LE", 0);
1297 ERROR("Failed to parse XML data");
1298 ret = WIMLIB_ERR_XML;
1299 goto out_free_xml_data;
1302 root = xmlDocGetRootElement(doc);
1303 if (!root || !node_is_element(root) || !node_name_is(root, "WIM")) {
1304 ERROR("WIM XML data is invalid");
1305 ret = WIMLIB_ERR_XML;
1309 ret = xml_read_wim_info(root, &wim->wim_info);
1318 /* Prepares an in-memory buffer containing the UTF-16LE XML data for a WIM file.
1320 * total_bytes is the number to write in <TOTALBYTES>, or
1321 * WIM_TOTALBYTES_USE_EXISTING to use the existing value in memory, or
1322 * WIM_TOTALBYTES_OMIT to omit <TOTALBYTES> entirely.
1325 prepare_wim_xml_data(WIMStruct *wim, int image, u64 total_bytes,
1326 u8 **xml_data_ret, size_t *xml_len_ret)
1328 xmlCharEncodingHandler *encoding_handler;
1330 xmlOutputBuffer *outbuf;
1331 xmlTextWriter *writer;
1334 const xmlChar *content;
1339 /* Open an xmlTextWriter that writes to an in-memory buffer using
1340 * UTF-16LE encoding. */
1342 encoding_handler = xmlGetCharEncodingHandler(XML_CHAR_ENCODING_UTF16LE);
1343 if (!encoding_handler) {
1344 ERROR("Failed to get XML character encoding handler for UTF-16LE");
1345 ret = WIMLIB_ERR_LIBXML_UTF16_HANDLER_NOT_AVAILABLE;
1349 buf = xmlBufferCreate();
1351 ERROR("Failed to create xmlBuffer");
1352 ret = WIMLIB_ERR_NOMEM;
1356 outbuf = xmlOutputBufferCreateBuffer(buf, encoding_handler);
1358 ERROR("Failed to allocate xmlOutputBuffer");
1359 ret = WIMLIB_ERR_NOMEM;
1360 goto out_buffer_free;
1363 writer = xmlNewTextWriter(outbuf);
1365 ERROR("Failed to allocate xmlTextWriter");
1366 ret = WIMLIB_ERR_NOMEM;
1367 goto out_output_buffer_close;
1370 /* Write the XML document. */
1372 ret = xmlTextWriterStartElement(writer, "WIM");
1374 goto out_write_error;
1376 /* The contents of the <TOTALBYTES> element in the XML data, under the
1377 * <WIM> element (not the <IMAGE> element), is for non-split WIMs the
1378 * size of the WIM file excluding the XML data and integrity table.
1379 * For split WIMs, <TOTALBYTES> takes into account the entire WIM, not
1380 * just the current part. */
1381 if (total_bytes != WIM_TOTALBYTES_OMIT) {
1382 if (total_bytes == WIM_TOTALBYTES_USE_EXISTING) {
1384 total_bytes = wim->wim_info->total_bytes;
1388 ret = xmlTextWriterWriteFormatElement(writer, "TOTALBYTES",
1389 "%"PRIu64, total_bytes);
1391 goto out_write_error;
1394 if (image == WIMLIB_ALL_IMAGES) {
1396 last = wim->hdr.image_count;
1402 for (int i = first; i <= last; i++) {
1403 ret = xml_write_image_info(writer, &wim->wim_info->images[i - 1]);
1406 goto out_write_error;
1407 goto out_free_text_writer;
1411 ret = xmlTextWriterEndElement(writer);
1413 goto out_write_error;
1415 ret = xmlTextWriterEndDocument(writer);
1417 goto out_write_error;
1419 ret = xmlTextWriterFlush(writer);
1421 goto out_write_error;
1423 /* Retrieve the buffer into which the document was written. */
1425 content = xmlBufferContent(buf);
1426 len = xmlBufferLength(buf);
1428 /* Copy the data into a new buffer, and prefix it with the UTF-16LE BOM
1429 * (byte order mark), which is required by MS's software to understand
1433 xml_data = MALLOC(xml_len);
1435 ret = WIMLIB_ERR_NOMEM;
1436 goto out_free_text_writer;
1440 memcpy(&xml_data[2], content, len);
1442 /* Clean up libxml objects and return success. */
1443 *xml_data_ret = xml_data;
1444 *xml_len_ret = xml_len;
1446 out_free_text_writer:
1447 /* xmlFreeTextWriter will free the attached xmlOutputBuffer. */
1448 xmlFreeTextWriter(writer);
1449 goto out_buffer_free;
1450 out_output_buffer_close:
1451 xmlOutputBufferClose(outbuf);
1458 ERROR("Error writing XML data");
1459 ret = WIMLIB_ERR_WRITE;
1460 goto out_free_text_writer;
1463 /* Writes the XML data to a WIM file. */
1465 write_wim_xml_data(WIMStruct *wim, int image, u64 total_bytes,
1466 struct resource_entry *out_res_entry,
1467 int write_resource_flags)
1473 DEBUG("Writing WIM XML data (image=%d, offset=%"PRIu64")",
1474 image, total_bytes, wim->out_fd.offset);
1476 ret = prepare_wim_xml_data(wim, image, total_bytes,
1477 &xml_data, &xml_len);
1481 /* Write the XML data uncompressed. Although wimlib can handle
1482 * compressed XML data, MS software cannot. */
1483 ret = write_wim_resource_from_buffer(xml_data,
1485 WIM_RESHDR_FLAG_METADATA,
1487 WIMLIB_COMPRESSION_TYPE_NONE,
1490 write_resource_flags);
1495 /* API function documented in wimlib.h */
1496 WIMLIBAPI const tchar *
1497 wimlib_get_image_name(const WIMStruct *wim, int image)
1499 if (image < 1 || image > wim->hdr.image_count)
1501 return wim->wim_info->images[image - 1].name;
1504 /* API function documented in wimlib.h */
1505 WIMLIBAPI const tchar *
1506 wimlib_get_image_description(const WIMStruct *wim, int image)
1508 if (image < 1 || image > wim->hdr.image_count)
1510 return wim->wim_info->images[image - 1].description;
1513 /* API function documented in wimlib.h */
1515 wimlib_image_name_in_use(const WIMStruct *wim, const tchar *name)
1517 if (!name || !*name)
1519 for (int i = 1; i <= wim->hdr.image_count; i++)
1520 if (!tstrcmp(wim->wim_info->images[i - 1].name, name))
1526 /* API function documented in wimlib.h */
1528 wimlib_extract_xml_data(WIMStruct *wim, FILE *fp)
1535 return WIMLIB_ERR_INVALID_PARAM;
1537 ret = res_entry_to_data(&wim->hdr.xml_res_entry, wim, &buf);
1541 size = wim->hdr.xml_res_entry.original_size;
1542 if (fwrite(buf, 1, size, fp) != size) {
1543 ERROR_WITH_ERRNO("Failed to extract XML data");
1544 ret = WIMLIB_ERR_WRITE;
1555 /* API function documented in wimlib.h */
1557 wimlib_set_image_name(WIMStruct *wim, int image, const tchar *name)
1563 DEBUG("Setting the name of image %d to %"TS, image, name);
1565 ret = can_modify_wim(wim);
1572 if (image < 1 || image > wim->hdr.image_count) {
1573 ERROR("%d is not a valid image", image);
1574 return WIMLIB_ERR_INVALID_IMAGE;
1577 for (i = 1; i <= wim->hdr.image_count; i++) {
1580 if (!tstrcmp(wim->wim_info->images[i - 1].name, name)) {
1581 ERROR("The name \"%"TS"\" is already in use in the WIM!",
1583 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
1589 return WIMLIB_ERR_NOMEM;
1591 FREE(wim->wim_info->images[image - 1].name);
1592 wim->wim_info->images[image - 1].name = p;
1597 do_set_image_info_str(WIMStruct *wim, int image, const tchar *tstr,
1601 tchar **dest_tstr_p;
1604 ret = can_modify_wim(wim);
1608 if (image < 1 || image > wim->hdr.image_count) {
1609 ERROR("%d is not a valid image", image);
1610 return WIMLIB_ERR_INVALID_IMAGE;
1613 tstr_copy = TSTRDUP(tstr);
1615 return WIMLIB_ERR_NOMEM;
1619 dest_tstr_p = (tchar**)((void*)&wim->wim_info->images[image - 1] + offset);
1622 *dest_tstr_p = tstr_copy;
1626 /* API function documented in wimlib.h */
1628 wimlib_set_image_descripton(WIMStruct *wim, int image,
1629 const tchar *description)
1631 return do_set_image_info_str(wim, image, description,
1632 offsetof(struct image_info, description));
1635 /* API function documented in wimlib.h */
1637 wimlib_set_image_flags(WIMStruct *wim, int image, const tchar *flags)
1639 return do_set_image_info_str(wim, image, flags,
1640 offsetof(struct image_info, flags));