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>
47 /* Structures used to form an in-memory representation of the XML data (other
48 * than the raw parse tree from libxml). */
50 struct windows_version {
62 tchar *installation_type;
67 tchar *default_language;
70 bool windows_version_exists;
71 struct windows_version windows_version;
76 bool windows_info_exists;
82 u64 last_modification_time;
83 struct windows_info windows_info;
87 tchar *display_description;
89 struct wim_lookup_table *lookup_table; /* temporary field */
92 struct xml_string_spec {
97 #define ELEM(STRING_NAME, MEMBER_NAME) \
98 {STRING_NAME, offsetof(struct image_info, MEMBER_NAME)}
99 static const struct xml_string_spec
100 image_info_xml_string_specs[] = {
102 ELEM("DESCRIPTION", description),
103 ELEM("DISPLAYNAME", display_name),
104 ELEM("DISPLAYDESCRIPTION", display_description),
105 ELEM("FLAGS", flags),
109 #define ELEM(STRING_NAME, MEMBER_NAME) \
110 {STRING_NAME, offsetof(struct windows_info, MEMBER_NAME)}
111 static const struct xml_string_spec
112 windows_info_xml_string_specs[] = {
113 ELEM("PRODUCTNAME", product_name),
114 ELEM("EDITIONID", edition_id),
115 ELEM("INSTALLATIONTYPE", installation_type),
117 ELEM("PRODUCTTYPE", product_type),
118 ELEM("PRODUCTSUITE", product_suite),
123 /* Returns a statically allocated string that is a string representation of the
124 * architecture number. */
135 /* XXX Are there other arch values? */
142 /* Iterate through the children of an xmlNode. */
143 #define for_node_child(parent, child) \
144 for (child = parent->children; child != NULL; child = child->next)
146 /* Utility functions for xmlNodes */
148 node_is_element(xmlNode *node)
150 return node->type == XML_ELEMENT_NODE;
154 node_is_text(xmlNode *node)
156 return node->type == XML_TEXT_NODE;
160 node_name_is(xmlNode *node, const char *name)
162 /* For now, both upper case and lower case element names are accepted. */
163 return strcasecmp((const char *)node->name, name) == 0;
167 node_get_number(const xmlNode *u64_node, int base)
170 for_node_child(u64_node, child)
171 if (node_is_text(child))
172 return strtoull(child->content, NULL, base);
176 /* Finds the text node that is a child of an element node and returns its
177 * content converted to a 64-bit unsigned integer. Returns 0 if no text node is
180 node_get_u64(const xmlNode *u64_node)
182 return node_get_number(u64_node, 10);
185 /* Like node_get_u64(), but expects a number in base 16. */
187 node_get_hex_u64(const xmlNode *u64_node)
189 return node_get_number(u64_node, 16);
193 node_get_string(const xmlNode *string_node, tchar **tstr_ret)
199 for_node_child(string_node, child) {
200 if (node_is_text(child) && child->content) {
201 ret = utf8_to_tstr_simple(child->content, &tstr);
211 /* Returns the timestamp from a time node. It has child elements <HIGHPART> and
212 * <LOWPART> that are then used to construct a 64-bit timestamp. */
214 node_get_timestamp(const xmlNode *time_node)
219 for_node_child(time_node, child) {
220 if (!node_is_element(child))
222 if (node_name_is(child, "HIGHPART"))
223 high_part = node_get_hex_u64(child);
224 else if (node_name_is(child, "LOWPART"))
225 low_part = node_get_hex_u64(child);
227 return (u64)low_part | ((u64)high_part << 32);
230 /* Used to sort an array of struct image_infos by their image indices. */
232 sort_by_index(const void *p1, const void *p2)
234 int index_1 = ((const struct image_info*)p1)->index;
235 int index_2 = ((const struct image_info*)p2)->index;
236 if (index_1 < index_2)
238 else if (index_1 > index_2)
245 /* Frees memory allocated inside a struct windows_info structure. */
247 destroy_windows_info(struct windows_info *windows_info)
249 FREE(windows_info->product_name);
250 FREE(windows_info->edition_id);
251 FREE(windows_info->installation_type);
252 FREE(windows_info->hal);
253 FREE(windows_info->product_type);
254 FREE(windows_info->product_suite);
255 for (size_t i = 0; i < windows_info->num_languages; i++)
256 FREE(windows_info->languages[i]);
257 FREE(windows_info->languages);
258 FREE(windows_info->default_language);
259 FREE(windows_info->system_root);
262 /* Frees memory allocated inside a struct image_info structure. */
264 destroy_image_info(struct image_info *image_info)
266 FREE(image_info->name);
267 FREE(image_info->description);
268 FREE(image_info->flags);
269 FREE(image_info->display_name);
270 FREE(image_info->display_description);
271 destroy_windows_info(&image_info->windows_info);
272 memset(image_info, 0, sizeof(struct image_info));
276 free_wim_info(struct wim_info *info)
280 for (int i = 0; i < info->num_images; i++)
281 destroy_image_info(&info->images[i]);
288 /* Reads the information from a <VERSION> element inside the <WINDOWS> element.
291 xml_read_windows_version(const xmlNode *version_node,
292 struct windows_version* windows_version)
295 for_node_child(version_node, child) {
296 if (!node_is_element(child))
298 if (node_name_is(child, "MAJOR"))
299 windows_version->major = node_get_u64(child);
300 else if (node_name_is(child, "MINOR"))
301 windows_version->minor = node_get_u64(child);
302 else if (node_name_is(child, "BUILD"))
303 windows_version->build = node_get_u64(child);
304 else if (node_name_is(child, "SPBUILD"))
305 windows_version->sp_build = node_get_u64(child);
306 else if (node_name_is(child, "SPLEVEL"))
307 windows_version->sp_level = node_get_u64(child);
311 /* Reads the information from a <LANGUAGE> element inside a <WINDOWS> element.
314 xml_read_languages(const xmlNode *languages_node,
315 tchar ***languages_ret,
316 size_t *num_languages_ret,
317 tchar **default_language_ret)
320 size_t num_languages = 0;
324 for_node_child(languages_node, child)
325 if (node_is_element(child) && node_name_is(child, "LANGUAGE"))
328 languages = CALLOC(num_languages, sizeof(languages[0]));
330 return WIMLIB_ERR_NOMEM;
332 *languages_ret = languages;
333 *num_languages_ret = num_languages;
336 for_node_child(languages_node, child) {
337 if (!node_is_element(child))
339 if (node_name_is(child, "LANGUAGE"))
340 ret = node_get_string(child, languages++);
341 else if (node_name_is(child, "DEFAULT"))
342 ret = node_get_string(child, default_language_ret);
349 /* Reads the information from a <WINDOWS> element inside an <IMAGE> element. */
351 xml_read_windows_info(const xmlNode *windows_node,
352 struct windows_info *windows_info)
357 for_node_child(windows_node, child) {
358 if (!node_is_element(child))
360 if (node_name_is(child, "ARCH")) {
361 windows_info->arch = node_get_u64(child);
362 } else if (node_name_is(child, "PRODUCTNAME")) {
363 ret = node_get_string(child,
364 &windows_info->product_name);
365 } else if (node_name_is(child, "EDITIONID")) {
366 ret = node_get_string(child,
367 &windows_info->edition_id);
368 } else if (node_name_is(child, "INSTALLATIONTYPE")) {
369 ret = node_get_string(child,
370 &windows_info->installation_type);
371 } else if (node_name_is(child, "PRODUCTTYPE")) {
372 ret = node_get_string(child,
373 &windows_info->product_type);
374 } else if (node_name_is(child, "PRODUCTSUITE")) {
375 ret = node_get_string(child,
376 &windows_info->product_suite);
377 } else if (node_name_is(child, "LANGUAGES")) {
378 ret = xml_read_languages(child,
379 &windows_info->languages,
380 &windows_info->num_languages,
381 &windows_info->default_language);
382 } else if (node_name_is(child, "VERSION")) {
383 xml_read_windows_version(child,
384 &windows_info->windows_version);
385 windows_info->windows_version_exists = true;
386 } else if (node_name_is(child, "SYSTEMROOT")) {
387 ret = node_get_string(child, &windows_info->system_root);
388 } else if (node_name_is(child, "HAL")) {
389 ret = node_get_string(child, &windows_info->hal);
397 /* Reads the information from an <IMAGE> element. */
399 xml_read_image_info(xmlNode *image_node, struct image_info *image_info)
405 index_prop = xmlGetProp(image_node, "INDEX");
407 image_info->index = atoi(index_prop);
410 image_info->index = 1;
414 for_node_child(image_node, child) {
415 if (!node_is_element(child))
417 if (node_name_is(child, "DIRCOUNT"))
418 image_info->dir_count = node_get_u64(child);
419 else if (node_name_is(child, "FILECOUNT"))
420 image_info->file_count = node_get_u64(child);
421 else if (node_name_is(child, "TOTALBYTES"))
422 image_info->total_bytes = node_get_u64(child);
423 else if (node_name_is(child, "HARDLINKBYTES"))
424 image_info->hard_link_bytes = node_get_u64(child);
425 else if (node_name_is(child, "CREATIONTIME"))
426 image_info->creation_time = node_get_timestamp(child);
427 else if (node_name_is(child, "LASTMODIFICATIONTIME"))
428 image_info->last_modification_time = node_get_timestamp(child);
429 else if (node_name_is(child, "WINDOWS")) {
430 DEBUG("Found <WINDOWS> tag");
431 ret = xml_read_windows_info(child,
432 &image_info->windows_info);
433 image_info->windows_info_exists = true;
434 } else if (node_name_is(child, "NAME")) {
435 ret = node_get_string(child, &image_info->name);
436 } else if (node_name_is(child, "DESCRIPTION")) {
437 ret = node_get_string(child, &image_info->description);
438 } else if (node_name_is(child, "FLAGS")) {
439 ret = node_get_string(child, &image_info->flags);
440 } else if (node_name_is(child, "DISPLAYNAME")) {
441 ret = node_get_string(child, &image_info->display_name);
442 } else if (node_name_is(child, "DISPLAYDESCRIPTION")) {
443 ret = node_get_string(child, &image_info->display_description);
448 if (!image_info->name) {
450 /*WARNING("Image with index %d has no name", image_info->index);*/
451 empty_name = MALLOC(sizeof(tchar));
453 return WIMLIB_ERR_NOMEM;
454 *empty_name = T('\0');
455 image_info->name = empty_name;
460 /* Reads the information from a <WIM> element, which should be the root element
461 * of the XML tree. */
463 xml_read_wim_info(const xmlNode *wim_node, struct wim_info **wim_info_ret)
465 struct wim_info *wim_info;
471 wim_info = CALLOC(1, sizeof(struct wim_info));
473 return WIMLIB_ERR_NOMEM;
475 /* Count how many images there are. */
477 for_node_child(wim_node, child) {
478 if (node_is_element(child) && node_name_is(child, "IMAGE")) {
479 if (num_images == INT_MAX) {
480 return WIMLIB_ERR_IMAGE_COUNT;
486 if (num_images > 0) {
487 /* Allocate the array of struct image_infos and fill them in. */
488 wim_info->images = CALLOC(num_images, sizeof(wim_info->images[0]));
489 if (!wim_info->images) {
490 ret = WIMLIB_ERR_NOMEM;
493 wim_info->num_images = num_images;
495 for_node_child(wim_node, child) {
496 if (!node_is_element(child))
498 if (node_name_is(child, "IMAGE")) {
499 DEBUG("Found <IMAGE> tag");
500 ret = xml_read_image_info(child,
501 &wim_info->images[i]);
505 } else if (node_name_is(child, "TOTALBYTES")) {
506 wim_info->total_bytes = node_get_u64(child);
510 /* Sort the array of image info by image index. */
511 qsort(wim_info->images, num_images,
512 sizeof(struct image_info), sort_by_index);
514 /* Make sure the image indices make sense */
515 for (i = 0; i < num_images; i++) {
516 if (wim_info->images[i].index != i + 1) {
517 ERROR("WIM images are not indexed [1...%d] "
518 "in XML data as expected",
520 return WIMLIB_ERR_IMAGE_COUNT;
525 *wim_info_ret = wim_info;
528 free_wim_info(wim_info);
532 /* Prints the information contained in a `struct windows_info'.
534 * Warning: any strings printed here are in UTF-8 encoding. If the locale
535 * character encoding is not UTF-8, the printed strings may be garbled. */
537 print_windows_info(const struct windows_info *windows_info)
539 const struct windows_version *windows_version;
541 tprintf(T("Architecture: %"TS"\n"),
542 get_arch(windows_info->arch));
544 if (windows_info->product_name) {
545 tprintf(T("Product Name: %"TS"\n"),
546 windows_info->product_name);
549 if (windows_info->edition_id) {
550 tprintf(T("Edition ID: %"TS"\n"),
551 windows_info->edition_id);
554 if (windows_info->installation_type) {
555 tprintf(T("Installation Type: %"TS"\n"),
556 windows_info->installation_type);
559 if (windows_info->hal) {
560 tprintf(T("HAL: %"TS"\n"),
564 if (windows_info->product_type) {
565 tprintf(T("Product Type: %"TS"\n"),
566 windows_info->product_type);
569 if (windows_info->product_suite) {
570 tprintf(T("Product Suite: %"TS"\n"),
571 windows_info->product_suite);
574 tprintf(T("Languages: "));
575 for (size_t i = 0; i < windows_info->num_languages; i++) {
577 tfputs(windows_info->languages[i], stdout);
581 if (windows_info->default_language) {
582 tprintf(T("Default Language: %"TS"\n"),
583 windows_info->default_language);
585 if (windows_info->system_root) {
586 tprintf(T("System Root: %"TS"\n"),
587 windows_info->system_root);
590 if (windows_info->windows_version_exists) {
591 windows_version = &windows_info->windows_version;
592 tprintf(T("Major Version: %"PRIu64"\n"),
593 windows_version->major);
594 tprintf(T("Minor Version: %"PRIu64"\n"),
595 windows_version->minor);
596 tprintf(T("Build: %"PRIu64"\n"),
597 windows_version->build);
598 tprintf(T("Service Pack Build: %"PRIu64"\n"),
599 windows_version->sp_build);
600 tprintf(T("Service Pack Level: %"PRIu64"\n"),
601 windows_version->sp_level);
606 xml_write_string(xmlTextWriter *writer, const char *name,
611 int rc = tstr_to_utf8_simple(tstr, &utf8_str);
614 rc = xmlTextWriterWriteElement(writer, name, utf8_str);
623 xml_write_strings_from_specs(xmlTextWriter *writer,
624 const void *struct_with_strings,
625 const struct xml_string_spec specs[],
628 for (size_t i = 0; i < num_specs; i++) {
629 int rc = xml_write_string(writer, specs[i].name,
630 *(const tchar * const *)
631 (struct_with_strings + specs[i].offset));
639 dup_strings_from_specs(const void *old_struct_with_strings,
640 void *new_struct_with_strings,
641 const struct xml_string_spec specs[],
644 for (size_t i = 0; i < num_specs; i++) {
645 const tchar *old_str = *(const tchar * const *)
646 ((const void*)old_struct_with_strings + specs[i].offset);
647 tchar **new_str_p = (tchar **)((void*)new_struct_with_strings + specs[i].offset);
649 *new_str_p = TSTRDUP(old_str);
651 return WIMLIB_ERR_NOMEM;
657 /* Writes the information contained in a `struct windows_version' to the XML
658 * document being written. This is the <VERSION> element inside the <WINDOWS>
661 xml_write_windows_version(xmlTextWriter *writer,
662 const struct windows_version *version)
665 rc = xmlTextWriterStartElement(writer, "VERSION");
669 rc = xmlTextWriterWriteFormatElement(writer, "MAJOR", "%"PRIu64,
674 rc = xmlTextWriterWriteFormatElement(writer, "MINOR", "%"PRIu64,
679 rc = xmlTextWriterWriteFormatElement(writer, "BUILD", "%"PRIu64,
684 rc = xmlTextWriterWriteFormatElement(writer, "SPBUILD", "%"PRIu64,
689 rc = xmlTextWriterWriteFormatElement(writer, "SPLEVEL", "%"PRIu64,
694 return xmlTextWriterEndElement(writer); /* </VERSION> */
697 /* Writes the information contained in a `struct windows_info' to the XML
698 * document being written. This is the <WINDOWS> element. */
700 xml_write_windows_info(xmlTextWriter *writer,
701 const struct windows_info *windows_info)
704 rc = xmlTextWriterStartElement(writer, "WINDOWS");
708 rc = xmlTextWriterWriteFormatElement(writer, "ARCH", "%"PRIu64,
713 rc = xml_write_strings_from_specs(writer,
715 windows_info_xml_string_specs,
716 ARRAY_LEN(windows_info_xml_string_specs));
720 if (windows_info->num_languages) {
721 rc = xmlTextWriterStartElement(writer, "LANGUAGES");
725 for (size_t i = 0; i < windows_info->num_languages; i++) {
726 rc = xml_write_string(writer, "LANGUAGE",
727 windows_info->languages[i]);
732 rc = xml_write_string(writer, "DEFAULT",
733 windows_info->default_language);
737 rc = xmlTextWriterEndElement(writer); /* </LANGUAGES> */
742 if (windows_info->windows_version_exists) {
743 rc = xml_write_windows_version(writer, &windows_info->windows_version);
748 rc = xml_write_string(writer, "SYSTEMROOT",
749 windows_info->system_root);
753 return xmlTextWriterEndElement(writer); /* </WINDOWS> */
756 /* Writes a time element to the XML document being constructed in memory. */
758 xml_write_time(xmlTextWriter *writer, const char *element_name, u64 time)
761 rc = xmlTextWriterStartElement(writer, element_name);
765 rc = xmlTextWriterWriteFormatElement(writer, "HIGHPART",
766 "0x%08"PRIX32, (u32)(time >> 32));
770 rc = xmlTextWriterWriteFormatElement(writer, "LOWPART",
771 "0x%08"PRIX32, (u32)time);
775 rc = xmlTextWriterEndElement(writer); /* </@element_name> */
781 /* Writes an <IMAGE> element to the XML document. */
783 xml_write_image_info(xmlTextWriter *writer, const struct image_info *image_info)
786 rc = xmlTextWriterStartElement(writer, "IMAGE");
790 rc = xmlTextWriterWriteFormatAttribute(writer, "INDEX", "%d",
795 rc = xmlTextWriterWriteFormatElement(writer, "DIRCOUNT", "%"PRIu64,
796 image_info->dir_count);
800 rc = xmlTextWriterWriteFormatElement(writer, "FILECOUNT", "%"PRIu64,
801 image_info->file_count);
805 rc = xmlTextWriterWriteFormatElement(writer, "TOTALBYTES", "%"PRIu64,
806 image_info->total_bytes);
810 rc = xmlTextWriterWriteFormatElement(writer, "HARDLINKBYTES", "%"PRIu64,
811 image_info->hard_link_bytes);
815 rc = xml_write_time(writer, "CREATIONTIME", image_info->creation_time);
819 rc = xml_write_time(writer, "LASTMODIFICATIONTIME",
820 image_info->last_modification_time);
824 if (image_info->windows_info_exists) {
825 rc = xml_write_windows_info(writer, &image_info->windows_info);
830 rc = xml_write_strings_from_specs(writer, image_info,
831 image_info_xml_string_specs,
832 ARRAY_LEN(image_info_xml_string_specs));
836 rc = xmlTextWriterEndElement(writer); /* </IMAGE> */
844 /* Makes space for another image in the XML information and return a pointer to
846 static struct image_info *
847 add_image_info_struct(struct wim_info *wim_info)
849 struct image_info *images;
851 images = CALLOC(wim_info->num_images + 1, sizeof(struct image_info));
854 memcpy(images, wim_info->images,
855 wim_info->num_images * sizeof(struct image_info));
856 FREE(wim_info->images);
857 wim_info->images = images;
858 wim_info->num_images++;
859 return &images[wim_info->num_images - 1];
863 clone_windows_info(const struct windows_info *old, struct windows_info *new)
867 ret = dup_strings_from_specs(old, new, windows_info_xml_string_specs,
868 ARRAY_LEN(windows_info_xml_string_specs));
872 if (old->languages) {
873 new->languages = CALLOC(old->num_languages, sizeof(new->languages[0]));
875 return WIMLIB_ERR_NOMEM;
876 new->num_languages = old->num_languages;
877 for (size_t i = 0; i < new->num_languages; i++) {
878 if (!old->languages[i])
880 new->languages[i] = TSTRDUP(old->languages[i]);
881 if (!new->languages[i])
882 return WIMLIB_ERR_NOMEM;
885 if (old->default_language &&
886 !(new->default_language = TSTRDUP(old->default_language)))
887 return WIMLIB_ERR_NOMEM;
888 if (old->system_root && !(new->system_root = TSTRDUP(old->system_root)))
889 return WIMLIB_ERR_NOMEM;
890 if (old->windows_version_exists) {
891 new->windows_version_exists = true;
892 memcpy(&new->windows_version, &old->windows_version,
893 sizeof(old->windows_version));
899 clone_image_info(const struct image_info *old, struct image_info *new)
903 new->dir_count = old->dir_count;
904 new->file_count = old->file_count;
905 new->total_bytes = old->total_bytes;
906 new->hard_link_bytes = old->hard_link_bytes;
907 new->creation_time = old->creation_time;
908 new->last_modification_time = old->last_modification_time;
910 ret = dup_strings_from_specs(old, new,
911 image_info_xml_string_specs,
912 ARRAY_LEN(image_info_xml_string_specs));
916 if (old->windows_info_exists) {
917 new->windows_info_exists = true;
918 ret = clone_windows_info(&old->windows_info,
926 /* Copies the XML information for an image between WIM files.
928 * @dest_image_name and @dest_image_description are ignored if they are NULL;
929 * otherwise, they are used to override the image name and/or image description
930 * from the XML data in the source WIM file.
932 * On failure, WIMLIB_ERR_NOMEM is returned and no changes are made. Otherwise,
933 * 0 is returned and the WIM information at *new_wim_info_p is modified.
936 xml_export_image(const struct wim_info *old_wim_info,
938 struct wim_info **new_wim_info_p,
939 const tchar *dest_image_name,
940 const tchar *dest_image_description)
942 struct wim_info *new_wim_info;
943 struct image_info *image_info;
946 DEBUG("Copying XML data between WIM files for source image %d.", image);
948 wimlib_assert(old_wim_info != NULL);
949 wimlib_assert(image >= 1 && image <= old_wim_info->num_images);
951 if (*new_wim_info_p) {
952 new_wim_info = *new_wim_info_p;
954 new_wim_info = CALLOC(1, sizeof(struct wim_info));
959 image_info = add_image_info_struct(new_wim_info);
963 ret = clone_image_info(&old_wim_info->images[image - 1], image_info);
965 goto err_destroy_image_info;
967 image_info->index = new_wim_info->num_images;
969 if (dest_image_name) {
970 FREE(image_info->name);
971 image_info->name = TSTRDUP(dest_image_name);
972 if (!image_info->name)
973 goto err_destroy_image_info;
975 if (dest_image_description) {
976 FREE(image_info->description);
977 image_info->description = TSTRDUP(dest_image_description);
978 if (!image_info->description)
979 goto err_destroy_image_info;
981 *new_wim_info_p = new_wim_info;
983 err_destroy_image_info:
984 destroy_image_info(image_info);
986 if (new_wim_info != *new_wim_info_p)
987 free_wim_info(new_wim_info);
988 return WIMLIB_ERR_NOMEM;
991 /* Removes an image from the XML information. */
993 xml_delete_image(struct wim_info **wim_info_p, int image)
995 struct wim_info *wim_info;
997 wim_info = *wim_info_p;
998 wimlib_assert(image >= 1 && image <= wim_info->num_images);
999 DEBUG("Deleting image %d from the XML data.", image);
1001 destroy_image_info(&wim_info->images[image - 1]);
1003 memmove(&wim_info->images[image - 1],
1004 &wim_info->images[image],
1005 (wim_info->num_images - image) * sizeof(struct image_info));
1007 if (--wim_info->num_images == 0) {
1008 free_wim_info(wim_info);
1011 for (int i = image - 1; i < wim_info->num_images; i++)
1012 wim_info->images[i].index--;
1017 xml_get_max_image_name_len(const WIMStruct *wim)
1020 if (wim->wim_info) {
1021 for (int i = 0; i < wim->wim_info->num_images; i++) {
1022 size_t len = tstrlen(wim->wim_info->images[i].name);
1030 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
1032 xml_set_memory_allocator(void *(*malloc_func)(size_t),
1033 void (*free_func)(void *),
1034 void *(*realloc_func)(void *, size_t))
1036 xmlMemSetup(free_func, malloc_func, realloc_func, STRDUP);
1041 calculate_dentry_statistics(struct wim_dentry *dentry, void *arg)
1043 struct image_info *info = arg;
1044 const struct wim_inode *inode = dentry->d_inode;
1045 struct wim_lookup_table_entry *lte;
1047 /* Update directory count and file count.
1049 * Each dentry counts as either a file or a directory, but not both.
1050 * The root directory is an exception: it is not counted at all.
1052 * Symbolic links and junction points (and presumably other reparse
1053 * points) count as regular files. This is despite the fact that
1054 * junction points have FILE_ATTRIBUTE_DIRECTORY set.
1056 if (dentry_is_root(dentry))
1059 if (inode_is_directory(inode))
1065 * Update total bytes and hard link bytes.
1067 * Unfortunately there are some inconsistencies/bugs in the way this is
1070 * If there are no alternate data streams in the image, the "total
1071 * bytes" is the sum of the size of the un-named data stream of each
1072 * inode times the link count of that inode. In other words, it would
1073 * be the total number of bytes of regular files you would have if you
1074 * extracted the full image without any hard-links. The "hard link
1075 * bytes" is equal to the "total bytes" minus the size of the un-named
1076 * data stream of each inode. In other words, the "hard link bytes"
1077 * counts the size of the un-named data stream for all the links to each
1078 * inode except the first one.
1080 * Reparse points and directories don't seem to be counted in either the
1081 * total bytes or the hard link bytes.
1083 * And now we get to the most confusing part, the alternate data
1084 * streams. They are not counted in the "total bytes". However, if the
1085 * link count of an inode with alternate data streams is 2 or greater,
1086 * the size of all the alternate data streams is included in the "hard
1087 * link bytes", and this size is multiplied by the link count (NOT one
1088 * less than the link count).
1090 lte = inode_unnamed_lte(inode, info->lookup_table);
1092 info->total_bytes += wim_resource_size(lte);
1093 if (!dentry_is_first_in_inode(dentry))
1094 info->hard_link_bytes += wim_resource_size(lte);
1097 if (inode->i_nlink >= 2 && dentry_is_first_in_inode(dentry)) {
1098 for (unsigned i = 0; i < inode->i_num_ads; i++) {
1099 if (inode->i_ads_entries[i].stream_name_nbytes) {
1100 lte = inode_stream_lte(inode, i + 1, info->lookup_table);
1102 info->hard_link_bytes += inode->i_nlink *
1103 wim_resource_size(lte);
1112 * Calculate what to put in the <FILECOUNT>, <DIRCOUNT>, <TOTALBYTES>, and
1113 * <HARDLINKBYTES> elements of each <IMAGE>.
1115 * Please note there is no official documentation for exactly how this is done.
1116 * But, see calculate_dentry_statistics().
1119 xml_update_image_info(WIMStruct *wim, int image)
1121 struct image_info *image_info;
1123 DEBUG("Updating the image info for image %d", image);
1125 image_info = &wim->wim_info->images[image - 1];
1127 image_info->file_count = 0;
1128 image_info->dir_count = 0;
1129 image_info->total_bytes = 0;
1130 image_info->hard_link_bytes = 0;
1131 image_info->lookup_table = wim->lookup_table;
1133 for_dentry_in_tree(wim->image_metadata[image - 1]->root_dentry,
1134 calculate_dentry_statistics,
1136 image_info->last_modification_time = get_wim_timestamp();
1139 /* Adds an image to the XML information. */
1141 xml_add_image(WIMStruct *wim, const tchar *name)
1143 struct wim_info *wim_info;
1144 struct image_info *image_info;
1146 wimlib_assert(name != NULL);
1148 /* If this is the first image, allocate the struct wim_info. Otherwise
1149 * use the existing struct wim_info. */
1150 if (wim->wim_info) {
1151 wim_info = wim->wim_info;
1153 wim_info = CALLOC(1, sizeof(struct wim_info));
1155 return WIMLIB_ERR_NOMEM;
1158 image_info = add_image_info_struct(wim_info);
1160 goto out_free_wim_info;
1162 if (!(image_info->name = TSTRDUP(name)))
1163 goto out_destroy_image_info;
1165 wim->wim_info = wim_info;
1166 image_info->index = wim_info->num_images;
1167 image_info->creation_time = get_wim_timestamp();
1168 xml_update_image_info(wim, image_info->index);
1171 out_destroy_image_info:
1172 destroy_image_info(image_info);
1173 wim_info->num_images--;
1175 if (wim_info != wim->wim_info)
1177 return WIMLIB_ERR_NOMEM;
1180 /* Prints information about the specified image from struct wim_info structure.
1183 print_image_info(const struct wim_info *wim_info, int image)
1185 const struct image_info *image_info;
1189 wimlib_assert(image >= 1 && image <= wim_info->num_images);
1191 image_info = &wim_info->images[image - 1];
1193 tprintf(T("Index: %d\n"), image_info->index);
1194 tprintf(T("Name: %"TS"\n"), image_info->name);
1196 /* Always print the Description: part even if there is no
1198 if (image_info->description)
1199 desc = image_info->description;
1202 tprintf(T("Description: %"TS"\n"), desc);
1204 if (image_info->display_name) {
1205 tprintf(T("Display Name: %"TS"\n"),
1206 image_info->display_name);
1209 if (image_info->display_description) {
1210 tprintf(T("Display Description: %"TS"\n"),
1211 image_info->display_description);
1214 tprintf(T("Directory Count: %"PRIu64"\n"), image_info->dir_count);
1215 tprintf(T("File Count: %"PRIu64"\n"), image_info->file_count);
1216 tprintf(T("Total Bytes: %"PRIu64"\n"), image_info->total_bytes);
1217 tprintf(T("Hard Link Bytes: %"PRIu64"\n"), image_info->hard_link_bytes);
1219 wim_timestamp_to_str(image_info->creation_time, buf, sizeof(buf));
1220 tprintf(T("Creation Time: %"TS"\n"), buf);
1222 wim_timestamp_to_str(image_info->last_modification_time, buf, sizeof(buf));
1223 tprintf(T("Last Modification Time: %"TS"\n"), buf);
1224 if (image_info->windows_info_exists)
1225 print_windows_info(&image_info->windows_info);
1226 if (image_info->flags)
1227 tprintf(T("Flags: %"TS"\n"), image_info->flags);
1232 libxml_global_init(void)
1235 xmlInitCharEncodingHandlers();
1239 libxml_global_cleanup(void)
1242 xmlCleanupCharEncodingHandlers();
1246 * Reads the XML data from a WIM file.
1249 read_xml_data(int in_fd,
1250 const struct resource_entry *res_entry,
1251 struct wim_info **info_ret)
1253 utf16lechar *xml_data;
1258 DEBUG("XML data is %"PRIu64" bytes at offset %"PRIu64"",
1259 (u64)res_entry->size, res_entry->offset);
1261 if (resource_is_compressed(res_entry)) {
1262 ERROR("XML data is supposed to be uncompressed");
1263 ret = WIMLIB_ERR_XML;
1267 if (res_entry->size < 2) {
1268 ERROR("XML data must be at least 2 bytes long");
1269 ret = WIMLIB_ERR_XML;
1273 xml_data = MALLOC(res_entry->size + 3);
1275 ret = WIMLIB_ERR_NOMEM;
1279 if (full_pread(in_fd, xml_data,
1280 res_entry->size, res_entry->offset) != res_entry->size)
1282 ERROR_WITH_ERRNO("Error reading XML data");
1283 ret = WIMLIB_ERR_READ;
1284 goto out_free_xml_data;
1287 /* Null-terminate just in case */
1288 ((u8*)xml_data)[res_entry->size] = 0;
1289 ((u8*)xml_data)[res_entry->size + 1] = 0;
1290 ((u8*)xml_data)[res_entry->size + 2] = 0;
1292 DEBUG("Parsing XML using libxml2 to create XML tree");
1294 doc = xmlReadMemory((const char *)xml_data,
1295 res_entry->size, "noname.xml", "UTF-16", 0);
1298 ERROR("Failed to parse XML data");
1299 ret = WIMLIB_ERR_XML;
1300 goto out_free_xml_data;
1303 DEBUG("Constructing WIM information structure from XML tree.");
1305 root = xmlDocGetRootElement(doc);
1307 ERROR("WIM XML data is an empty XML document");
1308 ret = WIMLIB_ERR_XML;
1312 if (!node_is_element(root) || !node_name_is(root, "WIM")) {
1313 ERROR("Expected <WIM> for the root XML element");
1314 ret = WIMLIB_ERR_XML;
1317 ret = xml_read_wim_info(root, info_ret);
1319 DEBUG("Freeing XML tree.");
1328 * Writes XML data to a WIM file.
1330 * If @total_bytes is non-zero, it specifies what to write to the TOTALBYTES
1331 * element in the XML data. If zero, TOTALBYTES is given the default value of
1332 * the offset of the XML data.
1335 write_xml_data(const struct wim_info *wim_info, int image, int out_fd,
1336 u64 total_bytes, struct resource_entry *out_res_entry)
1338 xmlCharEncodingHandler *encoding_handler;
1339 xmlOutputBuffer *out_buffer;
1340 xmlTextWriter *writer;
1345 wimlib_assert(image == WIMLIB_ALL_IMAGES ||
1346 (wim_info != NULL && image >= 1 &&
1347 image <= wim_info->num_images));
1349 start_offset = filedes_offset(out_fd);
1350 if (start_offset == -1)
1351 return WIMLIB_ERR_WRITE;
1353 DEBUG("Writing XML data for image %d at offset %"PRIu64,
1354 image, start_offset);
1356 /* 2 bytes endianness marker for UTF-16LE. This is _required_ for WIM
1358 static u8 bom[2] = {0xff, 0xfe};
1359 if (full_write(out_fd, bom, 2) != 2) {
1360 ERROR_WITH_ERRNO("Error writing XML data");
1361 return WIMLIB_ERR_WRITE;
1364 /* The contents of the <TOTALBYTES> element in the XML data, under the
1365 * <WIM> element (not the <IMAGE> element), is for non-split WIMs the
1366 * size of the WIM file excluding the XML data and integrity table.
1367 * This should be equal to the current position in the output stream,
1368 * since the XML data and integrity table are the last elements of the
1371 * For split WIMs, <TOTALBYTES> takes into account the entire WIM, not
1372 * just the current part. In that case, @total_bytes should be passed
1373 * in to this function. */
1374 if (total_bytes == 0)
1375 total_bytes = start_offset;
1377 /* The encoding of the XML data must be UTF-16LE. */
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 out_buffer = xmlOutputBufferCreateFd(out_fd, encoding_handler);
1387 ERROR("Failed to allocate xmlOutputBuffer");
1388 ret = WIMLIB_ERR_NOMEM;
1392 writer = xmlNewTextWriter(out_buffer);
1394 ERROR("Failed to allocate xmlTextWriter");
1395 ret = WIMLIB_ERR_NOMEM;
1396 goto out_output_buffer_close;
1399 DEBUG("Writing <WIM> element");
1401 ret = xmlTextWriterStartElement(writer, "WIM");
1403 goto out_write_error;
1405 ret = xmlTextWriterWriteFormatElement(writer, "TOTALBYTES", "%"PRIu64,
1408 goto out_write_error;
1410 if (wim_info != NULL) {
1412 if (image == WIMLIB_ALL_IMAGES) {
1414 last = wim_info->num_images;
1419 DEBUG("Writing %d <IMAGE> elements", last - first + 1);
1420 for (int i = first; i <= last; i++) {
1421 ret = xml_write_image_info(writer, &wim_info->images[i - 1]);
1424 goto out_write_error;
1425 goto out_free_text_writer;
1430 ret = xmlTextWriterEndElement(writer);
1432 goto out_write_error;
1434 ret = xmlTextWriterEndDocument(writer);
1436 goto out_write_error;
1438 ret = xmlTextWriterFlush(writer);
1440 goto out_write_error;
1442 DEBUG("Ended XML document");
1444 end_offset = filedes_offset(out_fd);
1445 if (end_offset == -1) {
1446 ret = WIMLIB_ERR_WRITE;
1449 out_res_entry->offset = start_offset;
1450 out_res_entry->size = end_offset - start_offset;
1451 out_res_entry->original_size = end_offset - start_offset;
1452 out_res_entry->flags = WIM_RESHDR_FLAG_METADATA;
1454 out_free_text_writer:
1455 /* xmlFreeTextWriter will free the attached xmlOutputBuffer. */
1456 xmlFreeTextWriter(writer);
1458 out_output_buffer_close:
1459 xmlOutputBufferClose(out_buffer);
1462 DEBUG("Successfully wrote XML data");
1465 ERROR("Error writing XML data");
1466 ret = WIMLIB_ERR_WRITE;
1467 goto out_free_text_writer;
1470 /* Returns the name of the specified image. */
1471 WIMLIBAPI const tchar *
1472 wimlib_get_image_name(const WIMStruct *wim, int image)
1474 if (image < 1 || image > wim->hdr.image_count)
1476 return wim->wim_info->images[image - 1].name;
1479 /* Returns the description of the specified image. */
1480 WIMLIBAPI const tchar *
1481 wimlib_get_image_description(const WIMStruct *wim, int image)
1483 if (image < 1 || image > wim->hdr.image_count)
1485 return wim->wim_info->images[image - 1].description;
1488 /* Determines if an image name is already used by some image in the WIM. */
1490 wimlib_image_name_in_use(const WIMStruct *wim, const tchar *name)
1492 if (!name || !*name)
1494 for (int i = 1; i <= wim->hdr.image_count; i++)
1495 if (!tstrcmp(wim->wim_info->images[i - 1].name, name))
1501 /* Extracts the raw XML data to a file stream. */
1503 wimlib_extract_xml_data(WIMStruct *wim, FILE *fp)
1509 size = wim->hdr.xml_res_entry.size;
1510 if (sizeof(size_t) < sizeof(u64))
1511 if (size != wim->hdr.xml_res_entry.size)
1512 return WIMLIB_ERR_INVALID_PARAM;
1516 return WIMLIB_ERR_NOMEM;
1518 if (full_pread(wim->in_fd,
1520 wim->hdr.xml_res_entry.size,
1521 wim->hdr.xml_res_entry.offset) != wim->hdr.xml_res_entry.size)
1523 ERROR_WITH_ERRNO("Error reading XML data");
1524 ret = WIMLIB_ERR_READ;
1528 if (fwrite(buf, 1, size, fp) != size) {
1529 ERROR_WITH_ERRNO("Failed to extract XML data");
1530 ret = WIMLIB_ERR_WRITE;
1539 /* Sets the name of an image in the WIM. */
1541 wimlib_set_image_name(WIMStruct *wim, int image, const tchar *name)
1547 DEBUG("Setting the name of image %d to %"TS, image, name);
1549 ret = can_modify_wim(wim);
1556 if (image < 1 || image > wim->hdr.image_count) {
1557 ERROR("%d is not a valid image", image);
1558 return WIMLIB_ERR_INVALID_IMAGE;
1561 for (i = 1; i <= wim->hdr.image_count; i++) {
1564 if (!tstrcmp(wim->wim_info->images[i - 1].name, name)) {
1565 ERROR("The name \"%"TS"\" is already in use in the WIM!",
1567 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
1573 return WIMLIB_ERR_NOMEM;
1575 FREE(wim->wim_info->images[image - 1].name);
1576 wim->wim_info->images[image - 1].name = p;
1581 do_set_image_info_str(WIMStruct *wim, int image, const tchar *tstr,
1585 tchar **dest_tstr_p;
1588 ret = can_modify_wim(wim);
1592 if (image < 1 || image > wim->hdr.image_count) {
1593 ERROR("%d is not a valid image", image);
1594 return WIMLIB_ERR_INVALID_IMAGE;
1597 tstr_copy = TSTRDUP(tstr);
1599 return WIMLIB_ERR_NOMEM;
1603 dest_tstr_p = (tchar**)((void*)&wim->wim_info->images[image - 1] + offset);
1606 *dest_tstr_p = tstr_copy;
1610 /* Sets the description of an image in the WIM. */
1612 wimlib_set_image_descripton(WIMStruct *wim, int image,
1613 const tchar *description)
1615 return do_set_image_info_str(wim, image, description,
1616 offsetof(struct image_info, description));
1619 /* Set the <FLAGS> element of a WIM image */
1621 wimlib_set_image_flags(WIMStruct *wim, int image, const tchar *flags)
1623 return do_set_image_info_str(wim, image, flags,
1624 offsetof(struct image_info, flags));