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"
39 #include "wimlib/write.h"
41 #include <libxml/encoding.h>
42 #include <libxml/parser.h>
43 #include <libxml/tree.h>
44 #include <libxml/xmlwriter.h>
49 /* Structures used to form an in-memory representation of the XML data (other
50 * than the raw parse tree from libxml). */
52 struct windows_version {
64 tchar *installation_type;
65 tchar *pkeyconfigversion;
70 tchar *default_language;
73 bool windows_version_exists;
74 struct windows_version windows_version;
79 bool windows_info_exists;
85 u64 last_modification_time;
86 struct windows_info windows_info;
90 tchar *display_description;
93 struct wim_lookup_table *lookup_table; /* temporary field */
96 /* A struct wim_info structure corresponds to the entire XML data for a WIM file. */
100 /* Array of `struct image_info's, one for each image in the WIM that is
101 * mentioned in the XML data. */
102 struct image_info *images;
105 struct xml_string_spec {
110 #define ELEM(STRING_NAME, MEMBER_NAME) \
111 {STRING_NAME, offsetof(struct image_info, MEMBER_NAME)}
112 static const struct xml_string_spec
113 image_info_xml_string_specs[] = {
115 ELEM("DESCRIPTION", description),
116 ELEM("DISPLAYNAME", display_name),
117 ELEM("DISPLAYDESCRIPTION", display_description),
118 ELEM("FLAGS", flags),
122 #define ELEM(STRING_NAME, MEMBER_NAME) \
123 {STRING_NAME, offsetof(struct windows_info, MEMBER_NAME)}
124 static const struct xml_string_spec
125 windows_info_xml_string_specs[] = {
126 ELEM("PRODUCTNAME", product_name),
127 ELEM("EDITIONID", edition_id),
128 ELEM("INSTALLATIONTYPE", installation_type),
130 ELEM("PRODUCTTYPE", product_type),
131 ELEM("PRODUCTSUITE", product_suite),
136 wim_info_get_total_bytes(const struct wim_info *info)
139 return info->total_bytes;
145 wim_info_get_image_hard_link_bytes(const struct wim_info *info, int image)
148 return info->images[image - 1].hard_link_bytes;
154 wim_info_get_image_total_bytes(const struct wim_info *info, int image)
157 return info->images[image - 1].total_bytes;
163 wim_info_get_num_images(const struct wim_info *info)
166 return info->num_images;
172 wim_info_set_wimboot(struct wim_info *info, int image, bool value)
174 info->images[image - 1].wimboot = value;
178 wim_info_get_wimboot(const struct wim_info *info, int image)
180 return info->images[image - 1].wimboot;
183 /* Architecture constants are from w64 mingw winnt.h */
184 #define PROCESSOR_ARCHITECTURE_INTEL 0
185 #define PROCESSOR_ARCHITECTURE_MIPS 1
186 #define PROCESSOR_ARCHITECTURE_ALPHA 2
187 #define PROCESSOR_ARCHITECTURE_PPC 3
188 #define PROCESSOR_ARCHITECTURE_SHX 4
189 #define PROCESSOR_ARCHITECTURE_ARM 5
190 #define PROCESSOR_ARCHITECTURE_IA64 6
191 #define PROCESSOR_ARCHITECTURE_ALPHA64 7
192 #define PROCESSOR_ARCHITECTURE_MSIL 8
193 #define PROCESSOR_ARCHITECTURE_AMD64 9
194 #define PROCESSOR_ARCHITECTURE_IA32_ON_WIN64 10
196 /* Returns a statically allocated string that is a string representation of the
197 * architecture number. */
202 case PROCESSOR_ARCHITECTURE_INTEL:
204 case PROCESSOR_ARCHITECTURE_MIPS:
206 case PROCESSOR_ARCHITECTURE_ARM:
208 case PROCESSOR_ARCHITECTURE_IA64:
210 case PROCESSOR_ARCHITECTURE_AMD64:
218 /* Iterate through the children of an xmlNode. */
219 #define for_node_child(parent, child) \
220 for (child = parent->children; child != NULL; child = child->next)
222 /* Utility functions for xmlNodes */
224 node_is_element(xmlNode *node)
226 return node->type == XML_ELEMENT_NODE;
230 node_is_text(xmlNode *node)
232 return node->type == XML_TEXT_NODE;
236 node_name_is(xmlNode *node, const char *name)
238 /* For now, both upper case and lower case element names are accepted. */
239 return strcasecmp((const char *)node->name, name) == 0;
243 node_get_number(const xmlNode *u64_node, int base)
246 for_node_child(u64_node, child)
247 if (node_is_text(child))
248 return strtoull(child->content, NULL, base);
252 /* Finds the text node that is a child of an element node and returns its
253 * content converted to a 64-bit unsigned integer. Returns 0 if no text node is
256 node_get_u64(const xmlNode *u64_node)
258 return node_get_number(u64_node, 10);
261 /* Like node_get_u64(), but expects a number in base 16. */
263 node_get_hex_u64(const xmlNode *u64_node)
265 return node_get_number(u64_node, 16);
269 node_get_string(const xmlNode *string_node, tchar **tstr_ret)
278 for_node_child(string_node, child) {
279 if (node_is_text(child) && child->content) {
280 ret = utf8_to_tstr_simple(child->content, &tstr);
290 /* Returns the timestamp from a time node. It has child elements <HIGHPART> and
291 * <LOWPART> that are then used to construct a 64-bit timestamp. */
293 node_get_timestamp(const xmlNode *time_node)
298 for_node_child(time_node, child) {
299 if (!node_is_element(child))
301 if (node_name_is(child, "HIGHPART"))
302 high_part = node_get_hex_u64(child);
303 else if (node_name_is(child, "LOWPART"))
304 low_part = node_get_hex_u64(child);
306 return (u64)low_part | ((u64)high_part << 32);
309 /* Used to sort an array of struct image_infos by their image indices. */
311 sort_by_index(const void *p1, const void *p2)
313 int index_1 = ((const struct image_info*)p1)->index;
314 int index_2 = ((const struct image_info*)p2)->index;
315 if (index_1 < index_2)
317 else if (index_1 > index_2)
324 /* Frees memory allocated inside a struct windows_info structure. */
326 destroy_windows_info(struct windows_info *windows_info)
328 FREE(windows_info->product_name);
329 FREE(windows_info->edition_id);
330 FREE(windows_info->installation_type);
331 FREE(windows_info->hal);
332 FREE(windows_info->product_type);
333 FREE(windows_info->product_suite);
334 FREE(windows_info->pkeyconfigversion);
335 for (size_t i = 0; i < windows_info->num_languages; i++)
336 FREE(windows_info->languages[i]);
337 FREE(windows_info->languages);
338 FREE(windows_info->default_language);
339 FREE(windows_info->system_root);
342 /* Frees memory allocated inside a struct image_info structure. */
344 destroy_image_info(struct image_info *image_info)
346 FREE(image_info->name);
347 FREE(image_info->description);
348 FREE(image_info->flags);
349 FREE(image_info->display_name);
350 FREE(image_info->display_description);
351 destroy_windows_info(&image_info->windows_info);
352 memset(image_info, 0, sizeof(struct image_info));
356 free_wim_info(struct wim_info *info)
360 for (int i = 0; i < info->num_images; i++)
361 destroy_image_info(&info->images[i]);
368 /* Reads the information from a <VERSION> element inside the <WINDOWS> element.
371 xml_read_windows_version(const xmlNode *version_node,
372 struct windows_version* windows_version)
375 for_node_child(version_node, child) {
376 if (!node_is_element(child))
378 if (node_name_is(child, "MAJOR"))
379 windows_version->major = node_get_u64(child);
380 else if (node_name_is(child, "MINOR"))
381 windows_version->minor = node_get_u64(child);
382 else if (node_name_is(child, "BUILD"))
383 windows_version->build = node_get_u64(child);
384 else if (node_name_is(child, "SPBUILD"))
385 windows_version->sp_build = node_get_u64(child);
386 else if (node_name_is(child, "SPLEVEL"))
387 windows_version->sp_level = node_get_u64(child);
391 /* Reads the information from a <LANGUAGE> element inside a <WINDOWS> element.
394 xml_read_languages(const xmlNode *languages_node,
395 tchar ***languages_ret,
396 size_t *num_languages_ret,
397 tchar **default_language_ret)
400 size_t num_languages = 0;
404 for_node_child(languages_node, child)
405 if (node_is_element(child) && node_name_is(child, "LANGUAGE"))
408 languages = CALLOC(num_languages, sizeof(languages[0]));
410 return WIMLIB_ERR_NOMEM;
412 *languages_ret = languages;
413 *num_languages_ret = num_languages;
416 for_node_child(languages_node, child) {
417 if (!node_is_element(child))
419 if (node_name_is(child, "LANGUAGE"))
420 ret = node_get_string(child, languages++);
421 else if (node_name_is(child, "DEFAULT"))
422 ret = node_get_string(child, default_language_ret);
429 /* Reads the information from a <WINDOWS> element inside an <IMAGE> element. */
431 xml_read_windows_info(const xmlNode *windows_node,
432 struct windows_info *windows_info)
437 for_node_child(windows_node, child) {
438 if (!node_is_element(child))
440 if (node_name_is(child, "ARCH")) {
441 windows_info->arch = node_get_u64(child);
442 } else if (node_name_is(child, "PRODUCTNAME")) {
443 ret = node_get_string(child,
444 &windows_info->product_name);
445 } else if (node_name_is(child, "EDITIONID")) {
446 ret = node_get_string(child,
447 &windows_info->edition_id);
448 } else if (node_name_is(child, "INSTALLATIONTYPE")) {
449 ret = node_get_string(child,
450 &windows_info->installation_type);
451 } else if (node_name_is(child, "PRODUCTTYPE")) {
452 ret = node_get_string(child,
453 &windows_info->product_type);
454 } else if (node_name_is(child, "PRODUCTSUITE")) {
455 ret = node_get_string(child,
456 &windows_info->product_suite);
457 } else if (node_name_is(child, "LANGUAGES")) {
458 ret = xml_read_languages(child,
459 &windows_info->languages,
460 &windows_info->num_languages,
461 &windows_info->default_language);
462 } else if (node_name_is(child, "VERSION")) {
463 xml_read_windows_version(child,
464 &windows_info->windows_version);
465 windows_info->windows_version_exists = true;
466 } else if (node_name_is(child, "SYSTEMROOT")) {
467 ret = node_get_string(child, &windows_info->system_root);
468 } else if (node_name_is(child, "HAL")) {
469 ret = node_get_string(child, &windows_info->hal);
470 } else if (node_name_is(child, "SERVICINGDATA")) {
473 for_node_child(child, grandchild) {
474 if (node_is_element(grandchild) &&
475 node_name_is(grandchild, "PKEYCONFIGVERSION"))
477 ret = node_get_string(grandchild,
478 &windows_info->pkeyconfigversion);
489 /* Reads the information from an <IMAGE> element. */
491 xml_read_image_info(xmlNode *image_node, struct image_info *image_info)
497 index_prop = xmlGetProp(image_node, "INDEX");
499 image_info->index = atoi(index_prop);
502 image_info->index = 1;
506 for_node_child(image_node, child) {
507 if (!node_is_element(child))
509 if (node_name_is(child, "DIRCOUNT"))
510 image_info->dir_count = node_get_u64(child);
511 else if (node_name_is(child, "FILECOUNT"))
512 image_info->file_count = node_get_u64(child);
513 else if (node_name_is(child, "TOTALBYTES"))
514 image_info->total_bytes = node_get_u64(child);
515 else if (node_name_is(child, "HARDLINKBYTES"))
516 image_info->hard_link_bytes = node_get_u64(child);
517 else if (node_name_is(child, "CREATIONTIME"))
518 image_info->creation_time = node_get_timestamp(child);
519 else if (node_name_is(child, "LASTMODIFICATIONTIME"))
520 image_info->last_modification_time = node_get_timestamp(child);
521 else if (node_name_is(child, "WINDOWS")) {
522 DEBUG("Found <WINDOWS> tag");
523 ret = xml_read_windows_info(child,
524 &image_info->windows_info);
525 image_info->windows_info_exists = true;
526 } else if (node_name_is(child, "NAME")) {
527 ret = node_get_string(child, &image_info->name);
528 } else if (node_name_is(child, "DESCRIPTION")) {
529 ret = node_get_string(child, &image_info->description);
530 } else if (node_name_is(child, "FLAGS")) {
531 ret = node_get_string(child, &image_info->flags);
532 } else if (node_name_is(child, "DISPLAYNAME")) {
533 ret = node_get_string(child, &image_info->display_name);
534 } else if (node_name_is(child, "DISPLAYDESCRIPTION")) {
535 ret = node_get_string(child, &image_info->display_description);
536 } else if (node_name_is(child, "WIMBOOT")) {
537 if (node_get_u64(child) == 1) {
538 image_info->wimboot = true;
544 if (!image_info->name) {
546 /*WARNING("Image with index %d has no name", image_info->index);*/
547 empty_name = MALLOC(sizeof(tchar));
549 return WIMLIB_ERR_NOMEM;
550 *empty_name = T('\0');
551 image_info->name = empty_name;
556 /* Reads the information from a <WIM> element, which should be the root element
557 * of the XML tree. */
559 xml_read_wim_info(const xmlNode *wim_node, struct wim_info **wim_info_ret)
561 struct wim_info *wim_info;
567 wim_info = CALLOC(1, sizeof(struct wim_info));
569 return WIMLIB_ERR_NOMEM;
571 /* Count how many images there are. */
573 for_node_child(wim_node, child) {
574 if (node_is_element(child) && node_name_is(child, "IMAGE")) {
575 if (num_images == INT_MAX) {
576 return WIMLIB_ERR_IMAGE_COUNT;
582 if (num_images > 0) {
583 /* Allocate the array of struct image_infos and fill them in. */
584 wim_info->images = CALLOC(num_images, sizeof(wim_info->images[0]));
585 if (!wim_info->images) {
586 ret = WIMLIB_ERR_NOMEM;
589 wim_info->num_images = num_images;
591 for_node_child(wim_node, child) {
592 if (!node_is_element(child))
594 if (node_name_is(child, "IMAGE")) {
595 DEBUG("Found <IMAGE> tag");
596 ret = xml_read_image_info(child,
597 &wim_info->images[i]);
601 } else if (node_name_is(child, "TOTALBYTES")) {
602 wim_info->total_bytes = node_get_u64(child);
603 } else if (node_name_is(child, "ESD")) {
605 for_node_child(child, esdchild) {
606 if (node_is_element(esdchild) &&
607 node_name_is(esdchild, "ENCRYPTED"))
609 ret = WIMLIB_ERR_WIM_IS_ENCRYPTED;
616 /* Sort the array of image info by image index. */
617 qsort(wim_info->images, num_images,
618 sizeof(struct image_info), sort_by_index);
620 /* Make sure the image indices make sense */
621 for (i = 0; i < num_images; i++) {
622 if (wim_info->images[i].index != i + 1) {
623 ERROR("WIM images are not indexed [1...%d] "
624 "in XML data as expected",
626 return WIMLIB_ERR_IMAGE_COUNT;
631 *wim_info_ret = wim_info;
634 free_wim_info(wim_info);
638 /* Prints the information contained in a `struct windows_info'.
640 * Warning: any strings printed here are in UTF-8 encoding. If the locale
641 * character encoding is not UTF-8, the printed strings may be garbled. */
643 print_windows_info(const struct windows_info *windows_info)
645 const struct windows_version *windows_version;
647 tprintf(T("Architecture: %"TS"\n"),
648 get_arch(windows_info->arch));
650 if (windows_info->product_name) {
651 tprintf(T("Product Name: %"TS"\n"),
652 windows_info->product_name);
655 if (windows_info->edition_id) {
656 tprintf(T("Edition ID: %"TS"\n"),
657 windows_info->edition_id);
660 if (windows_info->installation_type) {
661 tprintf(T("Installation Type: %"TS"\n"),
662 windows_info->installation_type);
665 if (windows_info->hal) {
666 tprintf(T("HAL: %"TS"\n"),
670 if (windows_info->product_type) {
671 tprintf(T("Product Type: %"TS"\n"),
672 windows_info->product_type);
675 if (windows_info->product_suite) {
676 tprintf(T("Product Suite: %"TS"\n"),
677 windows_info->product_suite);
680 tprintf(T("Languages: "));
681 for (size_t i = 0; i < windows_info->num_languages; i++) {
683 tfputs(windows_info->languages[i], stdout);
687 if (windows_info->default_language) {
688 tprintf(T("Default Language: %"TS"\n"),
689 windows_info->default_language);
691 if (windows_info->system_root) {
692 tprintf(T("System Root: %"TS"\n"),
693 windows_info->system_root);
696 if (windows_info->windows_version_exists) {
697 windows_version = &windows_info->windows_version;
698 tprintf(T("Major Version: %"PRIu64"\n"),
699 windows_version->major);
700 tprintf(T("Minor Version: %"PRIu64"\n"),
701 windows_version->minor);
702 tprintf(T("Build: %"PRIu64"\n"),
703 windows_version->build);
704 tprintf(T("Service Pack Build: %"PRIu64"\n"),
705 windows_version->sp_build);
706 tprintf(T("Service Pack Level: %"PRIu64"\n"),
707 windows_version->sp_level);
712 xml_write_string(xmlTextWriter *writer, const char *name,
717 int rc = tstr_to_utf8_simple(tstr, &utf8_str);
720 rc = xmlTextWriterWriteElement(writer, name, utf8_str);
729 xml_write_strings_from_specs(xmlTextWriter *writer,
730 const void *struct_with_strings,
731 const struct xml_string_spec specs[],
734 for (size_t i = 0; i < num_specs; i++) {
735 int rc = xml_write_string(writer, specs[i].name,
736 *(const tchar * const *)
737 (struct_with_strings + specs[i].offset));
745 dup_strings_from_specs(const void *old_struct_with_strings,
746 void *new_struct_with_strings,
747 const struct xml_string_spec specs[],
750 for (size_t i = 0; i < num_specs; i++) {
751 const tchar *old_str = *(const tchar * const *)
752 ((const void*)old_struct_with_strings + specs[i].offset);
753 tchar **new_str_p = (tchar **)((void*)new_struct_with_strings + specs[i].offset);
755 *new_str_p = TSTRDUP(old_str);
757 return WIMLIB_ERR_NOMEM;
763 /* Writes the information contained in a `struct windows_version' to the XML
764 * document being written. This is the <VERSION> element inside the <WINDOWS>
767 xml_write_windows_version(xmlTextWriter *writer,
768 const struct windows_version *version)
771 rc = xmlTextWriterStartElement(writer, "VERSION");
775 rc = xmlTextWriterWriteFormatElement(writer, "MAJOR", "%"PRIu64,
780 rc = xmlTextWriterWriteFormatElement(writer, "MINOR", "%"PRIu64,
785 rc = xmlTextWriterWriteFormatElement(writer, "BUILD", "%"PRIu64,
790 rc = xmlTextWriterWriteFormatElement(writer, "SPBUILD", "%"PRIu64,
795 rc = xmlTextWriterWriteFormatElement(writer, "SPLEVEL", "%"PRIu64,
800 rc = xmlTextWriterEndElement(writer); /* </VERSION> */
807 /* Writes the information contained in a `struct windows_info' to the XML
808 * document being written. This is the <WINDOWS> element. */
810 xml_write_windows_info(xmlTextWriter *writer,
811 const struct windows_info *windows_info)
814 rc = xmlTextWriterStartElement(writer, "WINDOWS");
818 rc = xmlTextWriterWriteFormatElement(writer, "ARCH", "%"PRIu64,
823 rc = xml_write_strings_from_specs(writer,
825 windows_info_xml_string_specs,
826 ARRAY_LEN(windows_info_xml_string_specs));
830 if (windows_info->num_languages) {
831 rc = xmlTextWriterStartElement(writer, "LANGUAGES");
835 for (size_t i = 0; i < windows_info->num_languages; i++) {
836 rc = xml_write_string(writer, "LANGUAGE",
837 windows_info->languages[i]);
842 rc = xml_write_string(writer, "DEFAULT",
843 windows_info->default_language);
847 rc = xmlTextWriterEndElement(writer); /* </LANGUAGES> */
852 if (windows_info->pkeyconfigversion) {
853 rc = xmlTextWriterStartElement(writer, "SERVICINGDATA");
857 rc = xml_write_string(writer, "PKEYCONFIGVERSION",
858 windows_info->pkeyconfigversion);
862 rc = xmlTextWriterEndElement(writer);
867 if (windows_info->windows_version_exists) {
868 rc = xml_write_windows_version(writer, &windows_info->windows_version);
873 rc = xml_write_string(writer, "SYSTEMROOT", windows_info->system_root);
877 rc = xmlTextWriterEndElement(writer); /* </WINDOWS> */
884 /* Writes a time element to the XML document being constructed in memory. */
886 xml_write_time(xmlTextWriter *writer, const char *element_name, u64 time)
889 rc = xmlTextWriterStartElement(writer, element_name);
893 rc = xmlTextWriterWriteFormatElement(writer, "HIGHPART",
894 "0x%08"PRIX32, (u32)(time >> 32));
898 rc = xmlTextWriterWriteFormatElement(writer, "LOWPART",
899 "0x%08"PRIX32, (u32)time);
903 rc = xmlTextWriterEndElement(writer); /* </@element_name> */
909 /* Writes an <IMAGE> element to the XML document. */
911 xml_write_image_info(xmlTextWriter *writer, const struct image_info *image_info)
914 rc = xmlTextWriterStartElement(writer, "IMAGE");
918 rc = xmlTextWriterWriteFormatAttribute(writer, "INDEX", "%d",
923 rc = xmlTextWriterWriteFormatElement(writer, "DIRCOUNT", "%"PRIu64,
924 image_info->dir_count);
928 rc = xmlTextWriterWriteFormatElement(writer, "FILECOUNT", "%"PRIu64,
929 image_info->file_count);
933 rc = xmlTextWriterWriteFormatElement(writer, "TOTALBYTES", "%"PRIu64,
934 image_info->total_bytes);
938 rc = xmlTextWriterWriteFormatElement(writer, "HARDLINKBYTES", "%"PRIu64,
939 image_info->hard_link_bytes);
943 rc = xml_write_time(writer, "CREATIONTIME", image_info->creation_time);
947 rc = xml_write_time(writer, "LASTMODIFICATIONTIME",
948 image_info->last_modification_time);
952 if (image_info->windows_info_exists) {
953 rc = xml_write_windows_info(writer, &image_info->windows_info);
958 rc = xml_write_strings_from_specs(writer, image_info,
959 image_info_xml_string_specs,
960 ARRAY_LEN(image_info_xml_string_specs));
964 if (image_info->wimboot) {
965 rc = xmlTextWriterWriteFormatElement(writer, "WIMBOOT", "%d", 1);
970 rc = xmlTextWriterEndElement(writer); /* </IMAGE> */
979 /* Makes space for another image in the XML information and return a pointer to
981 static struct image_info *
982 add_image_info_struct(struct wim_info *wim_info)
984 struct image_info *images;
986 images = CALLOC(wim_info->num_images + 1, sizeof(struct image_info));
989 memcpy(images, wim_info->images,
990 wim_info->num_images * sizeof(struct image_info));
991 FREE(wim_info->images);
992 wim_info->images = images;
993 wim_info->num_images++;
994 return &images[wim_info->num_images - 1];
998 clone_windows_info(const struct windows_info *old, struct windows_info *new)
1002 ret = dup_strings_from_specs(old, new, windows_info_xml_string_specs,
1003 ARRAY_LEN(windows_info_xml_string_specs));
1007 if (old->pkeyconfigversion) {
1008 new->pkeyconfigversion = TSTRDUP(old->pkeyconfigversion);
1009 if (new->pkeyconfigversion == NULL)
1010 return WIMLIB_ERR_NOMEM;
1013 if (old->languages) {
1014 new->languages = CALLOC(old->num_languages, sizeof(new->languages[0]));
1015 if (!new->languages)
1016 return WIMLIB_ERR_NOMEM;
1017 new->num_languages = old->num_languages;
1018 for (size_t i = 0; i < new->num_languages; i++) {
1019 if (!old->languages[i])
1021 new->languages[i] = TSTRDUP(old->languages[i]);
1022 if (!new->languages[i])
1023 return WIMLIB_ERR_NOMEM;
1026 if (old->default_language &&
1027 !(new->default_language = TSTRDUP(old->default_language)))
1028 return WIMLIB_ERR_NOMEM;
1029 if (old->system_root && !(new->system_root = TSTRDUP(old->system_root)))
1030 return WIMLIB_ERR_NOMEM;
1031 if (old->windows_version_exists) {
1032 new->windows_version_exists = true;
1033 memcpy(&new->windows_version, &old->windows_version,
1034 sizeof(old->windows_version));
1040 clone_image_info(const struct image_info *old, struct image_info *new)
1044 new->dir_count = old->dir_count;
1045 new->file_count = old->file_count;
1046 new->total_bytes = old->total_bytes;
1047 new->hard_link_bytes = old->hard_link_bytes;
1048 new->creation_time = old->creation_time;
1049 new->last_modification_time = old->last_modification_time;
1051 ret = dup_strings_from_specs(old, new,
1052 image_info_xml_string_specs,
1053 ARRAY_LEN(image_info_xml_string_specs));
1057 if (old->windows_info_exists) {
1058 new->windows_info_exists = true;
1059 ret = clone_windows_info(&old->windows_info,
1060 &new->windows_info);
1067 /* Copies the XML information for an image between WIM files.
1069 * @dest_image_name and @dest_image_description are ignored if they are NULL;
1070 * otherwise, they are used to override the image name and/or image description
1071 * from the XML data in the source WIM file.
1073 * On failure, WIMLIB_ERR_NOMEM is returned and no changes are made. Otherwise,
1074 * 0 is returned and the WIM information at *new_wim_info_p is modified.
1077 xml_export_image(const struct wim_info *old_wim_info,
1079 struct wim_info **new_wim_info_p,
1080 const tchar *dest_image_name,
1081 const tchar *dest_image_description)
1083 struct wim_info *new_wim_info;
1084 struct image_info *image_info;
1087 DEBUG("Copying XML data between WIM files for source image %d.", image);
1089 wimlib_assert(old_wim_info != NULL);
1090 wimlib_assert(image >= 1 && image <= old_wim_info->num_images);
1092 if (*new_wim_info_p) {
1093 new_wim_info = *new_wim_info_p;
1095 new_wim_info = CALLOC(1, sizeof(struct wim_info));
1100 image_info = add_image_info_struct(new_wim_info);
1104 ret = clone_image_info(&old_wim_info->images[image - 1], image_info);
1106 goto err_destroy_image_info;
1108 image_info->index = new_wim_info->num_images;
1110 if (dest_image_name) {
1111 FREE(image_info->name);
1112 image_info->name = TSTRDUP(dest_image_name);
1113 if (!image_info->name)
1114 goto err_destroy_image_info;
1116 if (dest_image_description) {
1117 FREE(image_info->description);
1118 image_info->description = TSTRDUP(dest_image_description);
1119 if (!image_info->description)
1120 goto err_destroy_image_info;
1122 *new_wim_info_p = new_wim_info;
1124 err_destroy_image_info:
1125 destroy_image_info(image_info);
1127 if (new_wim_info != *new_wim_info_p)
1128 free_wim_info(new_wim_info);
1129 return WIMLIB_ERR_NOMEM;
1132 /* Removes an image from the XML information. */
1134 xml_delete_image(struct wim_info **wim_info_p, int image)
1136 struct wim_info *wim_info;
1138 wim_info = *wim_info_p;
1139 wimlib_assert(image >= 1 && image <= wim_info->num_images);
1140 DEBUG("Deleting image %d from the XML data.", image);
1142 destroy_image_info(&wim_info->images[image - 1]);
1144 memmove(&wim_info->images[image - 1],
1145 &wim_info->images[image],
1146 (wim_info->num_images - image) * sizeof(struct image_info));
1148 if (--wim_info->num_images == 0) {
1149 free_wim_info(wim_info);
1152 for (int i = image - 1; i < wim_info->num_images; i++)
1153 wim_info->images[i].index--;
1158 xml_get_max_image_name_len(const WIMStruct *wim)
1161 for (u32 i = 0; i < wim->hdr.image_count; i++)
1162 max_len = max(max_len, tstrlen(wim->wim_info->images[i].name));
1167 xml_set_memory_allocator(void *(*malloc_func)(size_t),
1168 void (*free_func)(void *),
1169 void *(*realloc_func)(void *, size_t))
1171 xmlMemSetup(free_func, malloc_func, realloc_func, STRDUP);
1175 calculate_dentry_statistics(struct wim_dentry *dentry, void *arg)
1177 struct image_info *info = arg;
1178 const struct wim_inode *inode = dentry->d_inode;
1179 struct wim_lookup_table_entry *lte;
1181 /* Update directory count and file count.
1183 * Each dentry counts as either a file or a directory, but not both.
1184 * The root directory is an exception: it is not counted at all.
1186 * Symbolic links and junction points (and presumably other reparse
1187 * points) count as regular files. This is despite the fact that
1188 * junction points have FILE_ATTRIBUTE_DIRECTORY set.
1190 if (dentry_is_root(dentry))
1193 if (inode_is_directory(inode))
1199 * Update total bytes and hard link bytes.
1201 * Unfortunately there are some inconsistencies/bugs in the way this is
1204 * If there are no alternate data streams in the image, the "total
1205 * bytes" is the sum of the size of the un-named data stream of each
1206 * inode times the link count of that inode. In other words, it would
1207 * be the total number of bytes of regular files you would have if you
1208 * extracted the full image without any hard-links. The "hard link
1209 * bytes" is equal to the "total bytes" minus the size of the un-named
1210 * data stream of each inode. In other words, the "hard link bytes"
1211 * counts the size of the un-named data stream for all the links to each
1212 * inode except the first one.
1214 * Reparse points and directories don't seem to be counted in either the
1215 * total bytes or the hard link bytes.
1217 * And now we get to the most confusing part, the alternate data
1218 * streams. They are not counted in the "total bytes". However, if the
1219 * link count of an inode with alternate data streams is 2 or greater,
1220 * the size of all the alternate data streams is included in the "hard
1221 * link bytes", and this size is multiplied by the link count (NOT one
1222 * less than the link count).
1224 lte = inode_unnamed_lte(inode, info->lookup_table);
1226 info->total_bytes += lte->size;
1227 if (!dentry_is_first_in_inode(dentry))
1228 info->hard_link_bytes += lte->size;
1231 if (inode->i_nlink >= 2 && dentry_is_first_in_inode(dentry)) {
1232 for (unsigned i = 0; i < inode->i_num_ads; i++) {
1233 if (inode->i_ads_entries[i].stream_name_nbytes) {
1234 lte = inode_stream_lte(inode, i + 1, info->lookup_table);
1236 info->hard_link_bytes += inode->i_nlink *
1246 * Calculate what to put in the <FILECOUNT>, <DIRCOUNT>, <TOTALBYTES>, and
1247 * <HARDLINKBYTES> elements of each <IMAGE>.
1249 * Please note there is no official documentation for exactly how this is done.
1250 * But, see calculate_dentry_statistics().
1253 xml_update_image_info(WIMStruct *wim, int image)
1255 struct image_info *image_info;
1257 DEBUG("Updating the image info for image %d", image);
1259 image_info = &wim->wim_info->images[image - 1];
1261 image_info->file_count = 0;
1262 image_info->dir_count = 0;
1263 image_info->total_bytes = 0;
1264 image_info->hard_link_bytes = 0;
1265 image_info->lookup_table = wim->lookup_table;
1267 for_dentry_in_tree(wim->image_metadata[image - 1]->root_dentry,
1268 calculate_dentry_statistics,
1270 image_info->last_modification_time = get_wim_timestamp();
1273 /* Adds an image to the XML information. */
1275 xml_add_image(WIMStruct *wim, const tchar *name)
1277 struct wim_info *wim_info;
1278 struct image_info *image_info;
1280 wimlib_assert(name != NULL);
1282 /* If this is the first image, allocate the struct wim_info. Otherwise
1283 * use the existing struct wim_info. */
1284 if (wim->wim_info) {
1285 wim_info = wim->wim_info;
1287 wim_info = CALLOC(1, sizeof(struct wim_info));
1289 return WIMLIB_ERR_NOMEM;
1292 image_info = add_image_info_struct(wim_info);
1294 goto out_free_wim_info;
1296 if (!(image_info->name = TSTRDUP(name)))
1297 goto out_destroy_image_info;
1299 wim->wim_info = wim_info;
1300 image_info->index = wim_info->num_images;
1301 image_info->creation_time = get_wim_timestamp();
1302 xml_update_image_info(wim, image_info->index);
1305 out_destroy_image_info:
1306 destroy_image_info(image_info);
1307 wim_info->num_images--;
1309 if (wim_info != wim->wim_info)
1311 return WIMLIB_ERR_NOMEM;
1314 /* Prints information about the specified image from struct wim_info structure.
1317 print_image_info(const struct wim_info *wim_info, int image)
1319 const struct image_info *image_info;
1323 wimlib_assert(image >= 1 && image <= wim_info->num_images);
1325 image_info = &wim_info->images[image - 1];
1327 tprintf(T("Index: %d\n"), image_info->index);
1328 tprintf(T("Name: %"TS"\n"), image_info->name);
1330 /* Always print the Description: part even if there is no
1332 if (image_info->description)
1333 desc = image_info->description;
1336 tprintf(T("Description: %"TS"\n"), desc);
1338 if (image_info->display_name) {
1339 tprintf(T("Display Name: %"TS"\n"),
1340 image_info->display_name);
1343 if (image_info->display_description) {
1344 tprintf(T("Display Description: %"TS"\n"),
1345 image_info->display_description);
1348 tprintf(T("Directory Count: %"PRIu64"\n"), image_info->dir_count);
1349 tprintf(T("File Count: %"PRIu64"\n"), image_info->file_count);
1350 tprintf(T("Total Bytes: %"PRIu64"\n"), image_info->total_bytes);
1351 tprintf(T("Hard Link Bytes: %"PRIu64"\n"), image_info->hard_link_bytes);
1353 wim_timestamp_to_str(image_info->creation_time, buf, sizeof(buf));
1354 tprintf(T("Creation Time: %"TS"\n"), buf);
1356 wim_timestamp_to_str(image_info->last_modification_time, buf, sizeof(buf));
1357 tprintf(T("Last Modification Time: %"TS"\n"), buf);
1358 if (image_info->windows_info_exists)
1359 print_windows_info(&image_info->windows_info);
1360 if (image_info->flags)
1361 tprintf(T("Flags: %"TS"\n"), image_info->flags);
1362 tprintf(T("WIMBoot compatible: %"TS"\n"),
1363 image_info->wimboot ? T("yes") : T("no"));
1368 libxml_global_init(void)
1371 xmlInitCharEncodingHandlers();
1375 libxml_global_cleanup(void)
1378 xmlCleanupCharEncodingHandlers();
1381 /* Reads the XML data from a WIM file. */
1383 read_wim_xml_data(WIMStruct *wim)
1392 ret = wimlib_get_xml_data(wim, &buf, &bufsize);
1397 doc = xmlReadMemory((const char *)xml_data, bufsize,
1398 NULL, "UTF-16LE", 0);
1400 ERROR("Failed to parse XML data");
1401 ret = WIMLIB_ERR_XML;
1402 goto out_free_xml_data;
1405 root = xmlDocGetRootElement(doc);
1406 if (!root || !node_is_element(root) || !node_name_is(root, "WIM")) {
1407 ERROR("WIM XML data is invalid");
1408 ret = WIMLIB_ERR_XML;
1412 ret = xml_read_wim_info(root, &wim->wim_info);
1421 /* Prepares an in-memory buffer containing the UTF-16LE XML data for a WIM file.
1423 * total_bytes is the number to write in <TOTALBYTES>, or
1424 * WIM_TOTALBYTES_USE_EXISTING to use the existing value in memory, or
1425 * WIM_TOTALBYTES_OMIT to omit <TOTALBYTES> entirely.
1428 prepare_wim_xml_data(WIMStruct *wim, int image, u64 total_bytes,
1429 u8 **xml_data_ret, size_t *xml_len_ret)
1431 xmlCharEncodingHandler *encoding_handler;
1433 xmlOutputBuffer *outbuf;
1434 xmlTextWriter *writer;
1437 const xmlChar *content;
1442 /* Open an xmlTextWriter that writes to an in-memory buffer using
1443 * UTF-16LE encoding. */
1445 encoding_handler = xmlGetCharEncodingHandler(XML_CHAR_ENCODING_UTF16LE);
1446 if (!encoding_handler) {
1447 ERROR("Failed to get XML character encoding handler for UTF-16LE");
1448 ret = WIMLIB_ERR_LIBXML_UTF16_HANDLER_NOT_AVAILABLE;
1452 buf = xmlBufferCreate();
1454 ERROR("Failed to create xmlBuffer");
1455 ret = WIMLIB_ERR_NOMEM;
1459 outbuf = xmlOutputBufferCreateBuffer(buf, encoding_handler);
1461 ERROR("Failed to allocate xmlOutputBuffer");
1462 ret = WIMLIB_ERR_NOMEM;
1463 goto out_buffer_free;
1466 writer = xmlNewTextWriter(outbuf);
1468 ERROR("Failed to allocate xmlTextWriter");
1469 ret = WIMLIB_ERR_NOMEM;
1470 goto out_output_buffer_close;
1473 /* Write the XML document. */
1475 ret = xmlTextWriterStartElement(writer, "WIM");
1477 goto out_write_error;
1479 /* The contents of the <TOTALBYTES> element in the XML data, under the
1480 * <WIM> element (not the <IMAGE> element), is for non-split WIMs the
1481 * size of the WIM file excluding the XML data and integrity table.
1482 * For split WIMs, <TOTALBYTES> takes into account the entire WIM, not
1483 * just the current part. */
1484 if (total_bytes != WIM_TOTALBYTES_OMIT) {
1485 if (total_bytes == WIM_TOTALBYTES_USE_EXISTING) {
1487 total_bytes = wim->wim_info->total_bytes;
1491 ret = xmlTextWriterWriteFormatElement(writer, "TOTALBYTES",
1492 "%"PRIu64, total_bytes);
1494 goto out_write_error;
1497 if (image == WIMLIB_ALL_IMAGES) {
1499 last = wim->hdr.image_count;
1505 for (int i = first; i <= last; i++) {
1506 ret = xml_write_image_info(writer, &wim->wim_info->images[i - 1]);
1509 goto out_write_error;
1510 goto out_free_text_writer;
1514 ret = xmlTextWriterEndElement(writer);
1516 goto out_write_error;
1518 ret = xmlTextWriterEndDocument(writer);
1520 goto out_write_error;
1522 ret = xmlTextWriterFlush(writer);
1524 goto out_write_error;
1526 /* Retrieve the buffer into which the document was written. */
1528 content = xmlBufferContent(buf);
1529 len = xmlBufferLength(buf);
1531 /* Copy the data into a new buffer, and prefix it with the UTF-16LE BOM
1532 * (byte order mark), which is required by MS's software to understand
1536 xml_data = MALLOC(xml_len);
1538 ret = WIMLIB_ERR_NOMEM;
1539 goto out_free_text_writer;
1543 memcpy(&xml_data[2], content, len);
1545 /* Clean up libxml objects and return success. */
1546 *xml_data_ret = xml_data;
1547 *xml_len_ret = xml_len;
1549 out_free_text_writer:
1550 /* xmlFreeTextWriter will free the attached xmlOutputBuffer. */
1551 xmlFreeTextWriter(writer);
1552 goto out_buffer_free;
1553 out_output_buffer_close:
1554 xmlOutputBufferClose(outbuf);
1558 DEBUG("ret=%d", ret);
1562 ERROR("Error writing XML data");
1563 ret = WIMLIB_ERR_WRITE;
1564 goto out_free_text_writer;
1567 /* Writes the XML data to a WIM file. */
1569 write_wim_xml_data(WIMStruct *wim, int image, u64 total_bytes,
1570 struct wim_reshdr *out_reshdr,
1571 int write_resource_flags)
1577 DEBUG("Writing WIM XML data (image=%d, offset=%"PRIu64")",
1578 image, total_bytes, wim->out_fd.offset);
1580 ret = prepare_wim_xml_data(wim, image, total_bytes,
1581 &xml_data, &xml_len);
1585 /* Write the XML data uncompressed. Although wimlib can handle
1586 * compressed XML data, MS software cannot. */
1587 ret = write_wim_resource_from_buffer(xml_data,
1589 WIM_RESHDR_FLAG_METADATA,
1591 WIMLIB_COMPRESSION_TYPE_NONE,
1595 write_resource_flags);
1597 DEBUG("ret=%d", ret);
1601 /* API function documented in wimlib.h */
1602 WIMLIBAPI const tchar *
1603 wimlib_get_image_name(const WIMStruct *wim, int image)
1605 if (image < 1 || image > wim->hdr.image_count)
1607 return wim->wim_info->images[image - 1].name;
1610 /* API function documented in wimlib.h */
1611 WIMLIBAPI const tchar *
1612 wimlib_get_image_description(const WIMStruct *wim, int image)
1614 if (image < 1 || image > wim->hdr.image_count)
1616 return wim->wim_info->images[image - 1].description;
1619 /* API function documented in wimlib.h */
1621 wimlib_image_name_in_use(const WIMStruct *wim, const tchar *name)
1623 if (!name || !*name)
1625 for (int i = 1; i <= wim->hdr.image_count; i++)
1626 if (!tstrcmp(wim->wim_info->images[i - 1].name, name))
1632 /* API function documented in wimlib.h */
1634 wimlib_get_xml_data(WIMStruct *wim, void **buf_ret, size_t *bufsize_ret)
1636 const struct wim_reshdr *xml_reshdr;
1638 if (wim->filename == NULL && filedes_is_seekable(&wim->in_fd))
1639 return WIMLIB_ERR_INVALID_PARAM;
1641 if (buf_ret == NULL || bufsize_ret == NULL)
1642 return WIMLIB_ERR_INVALID_PARAM;
1644 xml_reshdr = &wim->hdr.xml_data_reshdr;
1646 DEBUG("Reading XML data.");
1647 *bufsize_ret = xml_reshdr->uncompressed_size;
1648 return wim_reshdr_to_data(xml_reshdr, wim, buf_ret);
1652 wimlib_extract_xml_data(WIMStruct *wim, FILE *fp)
1658 ret = wimlib_get_xml_data(wim, &buf, &bufsize);
1662 if (fwrite(buf, 1, bufsize, fp) != bufsize) {
1663 ERROR_WITH_ERRNO("Failed to extract XML data");
1664 ret = WIMLIB_ERR_WRITE;
1670 /* API function documented in wimlib.h */
1672 wimlib_set_image_name(WIMStruct *wim, int image, const tchar *name)
1678 DEBUG("Setting the name of image %d to %"TS, image, name);
1680 ret = can_modify_wim(wim);
1687 if (image < 1 || image > wim->hdr.image_count) {
1688 ERROR("%d is not a valid image", image);
1689 return WIMLIB_ERR_INVALID_IMAGE;
1692 for (i = 1; i <= wim->hdr.image_count; i++) {
1695 if (!tstrcmp(wim->wim_info->images[i - 1].name, name)) {
1696 ERROR("The name \"%"TS"\" is already in use in the WIM!",
1698 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
1704 return WIMLIB_ERR_NOMEM;
1706 FREE(wim->wim_info->images[image - 1].name);
1707 wim->wim_info->images[image - 1].name = p;
1712 do_set_image_info_str(WIMStruct *wim, int image, const tchar *tstr,
1716 tchar **dest_tstr_p;
1719 ret = can_modify_wim(wim);
1723 if (image < 1 || image > wim->hdr.image_count) {
1724 ERROR("%d is not a valid image", image);
1725 return WIMLIB_ERR_INVALID_IMAGE;
1728 tstr_copy = TSTRDUP(tstr);
1730 return WIMLIB_ERR_NOMEM;
1734 dest_tstr_p = (tchar**)((void*)&wim->wim_info->images[image - 1] + offset);
1737 *dest_tstr_p = tstr_copy;
1741 /* API function documented in wimlib.h */
1743 wimlib_set_image_descripton(WIMStruct *wim, int image,
1744 const tchar *description)
1746 return do_set_image_info_str(wim, image, description,
1747 offsetof(struct image_info, description));
1750 /* API function documented in wimlib.h */
1752 wimlib_set_image_flags(WIMStruct *wim, int image, const tchar *flags)
1754 return do_set_image_info_str(wim, image, flags,
1755 offsetof(struct image_info, flags));