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;
94 /* Note: must update clone_image_info() if adding new fields here */
96 struct wim_lookup_table *lookup_table; /* temporary field */
99 /* A struct wim_info structure corresponds to the entire XML data for a WIM file. */
103 /* Array of `struct image_info's, one for each image in the WIM that is
104 * mentioned in the XML data. */
105 struct image_info *images;
108 struct xml_string_spec {
113 #define ELEM(STRING_NAME, MEMBER_NAME) \
114 {STRING_NAME, offsetof(struct image_info, MEMBER_NAME)}
115 static const struct xml_string_spec
116 image_info_xml_string_specs[] = {
118 ELEM("DESCRIPTION", description),
119 ELEM("DISPLAYNAME", display_name),
120 ELEM("DISPLAYDESCRIPTION", display_description),
121 ELEM("FLAGS", flags),
125 #define ELEM(STRING_NAME, MEMBER_NAME) \
126 {STRING_NAME, offsetof(struct windows_info, MEMBER_NAME)}
127 static const struct xml_string_spec
128 windows_info_xml_string_specs[] = {
129 ELEM("PRODUCTNAME", product_name),
130 ELEM("EDITIONID", edition_id),
131 ELEM("INSTALLATIONTYPE", installation_type),
133 ELEM("PRODUCTTYPE", product_type),
134 ELEM("PRODUCTSUITE", product_suite),
139 wim_info_get_total_bytes(const struct wim_info *info)
142 return info->total_bytes;
148 wim_info_get_image_hard_link_bytes(const struct wim_info *info, int image)
151 return info->images[image - 1].hard_link_bytes;
157 wim_info_get_image_total_bytes(const struct wim_info *info, int image)
160 return info->images[image - 1].total_bytes;
166 wim_info_get_num_images(const struct wim_info *info)
169 return info->num_images;
175 wim_info_set_wimboot(struct wim_info *info, int image, bool value)
177 info->images[image - 1].wimboot = value;
181 wim_info_get_wimboot(const struct wim_info *info, int image)
183 return info->images[image - 1].wimboot;
186 /* Architecture constants are from w64 mingw winnt.h */
187 #define PROCESSOR_ARCHITECTURE_INTEL 0
188 #define PROCESSOR_ARCHITECTURE_MIPS 1
189 #define PROCESSOR_ARCHITECTURE_ALPHA 2
190 #define PROCESSOR_ARCHITECTURE_PPC 3
191 #define PROCESSOR_ARCHITECTURE_SHX 4
192 #define PROCESSOR_ARCHITECTURE_ARM 5
193 #define PROCESSOR_ARCHITECTURE_IA64 6
194 #define PROCESSOR_ARCHITECTURE_ALPHA64 7
195 #define PROCESSOR_ARCHITECTURE_MSIL 8
196 #define PROCESSOR_ARCHITECTURE_AMD64 9
197 #define PROCESSOR_ARCHITECTURE_IA32_ON_WIN64 10
199 /* Returns a statically allocated string that is a string representation of the
200 * architecture number. */
205 case PROCESSOR_ARCHITECTURE_INTEL:
207 case PROCESSOR_ARCHITECTURE_MIPS:
209 case PROCESSOR_ARCHITECTURE_ARM:
211 case PROCESSOR_ARCHITECTURE_IA64:
213 case PROCESSOR_ARCHITECTURE_AMD64:
221 /* Iterate through the children of an xmlNode. */
222 #define for_node_child(parent, child) \
223 for (child = parent->children; child != NULL; child = child->next)
225 /* Utility functions for xmlNodes */
227 node_is_element(xmlNode *node)
229 return node->type == XML_ELEMENT_NODE;
233 node_is_text(xmlNode *node)
235 return node->type == XML_TEXT_NODE;
239 node_name_is(xmlNode *node, const char *name)
241 /* For now, both upper case and lower case element names are accepted. */
242 return strcasecmp((const char *)node->name, name) == 0;
246 node_get_number(const xmlNode *u64_node, int base)
249 for_node_child(u64_node, child)
250 if (node_is_text(child))
251 return strtoull(child->content, NULL, base);
255 /* Finds the text node that is a child of an element node and returns its
256 * content converted to a 64-bit unsigned integer. Returns 0 if no text node is
259 node_get_u64(const xmlNode *u64_node)
261 return node_get_number(u64_node, 10);
264 /* Like node_get_u64(), but expects a number in base 16. */
266 node_get_hex_u64(const xmlNode *u64_node)
268 return node_get_number(u64_node, 16);
272 node_get_string(const xmlNode *string_node, tchar **tstr_ret)
279 for_node_child(string_node, child)
280 if (node_is_text(child) && child->content)
281 return utf8_to_tstr_simple(child->content, tstr_ret);
285 /* Returns the timestamp from a time node. It has child elements <HIGHPART> and
286 * <LOWPART> that are then used to construct a 64-bit timestamp. */
288 node_get_timestamp(const xmlNode *time_node)
293 for_node_child(time_node, child) {
294 if (!node_is_element(child))
296 if (node_name_is(child, "HIGHPART"))
297 high_part = node_get_hex_u64(child);
298 else if (node_name_is(child, "LOWPART"))
299 low_part = node_get_hex_u64(child);
301 return (u64)low_part | ((u64)high_part << 32);
304 /* Used to sort an array of struct image_infos by their image indices. */
306 sort_by_index(const void *p1, const void *p2)
308 int index_1 = ((const struct image_info*)p1)->index;
309 int index_2 = ((const struct image_info*)p2)->index;
310 if (index_1 < index_2)
312 else if (index_1 > index_2)
319 /* Frees memory allocated inside a struct windows_info structure. */
321 destroy_windows_info(struct windows_info *windows_info)
323 FREE(windows_info->product_name);
324 FREE(windows_info->edition_id);
325 FREE(windows_info->installation_type);
326 FREE(windows_info->hal);
327 FREE(windows_info->product_type);
328 FREE(windows_info->product_suite);
329 FREE(windows_info->pkeyconfigversion);
330 for (size_t i = 0; i < windows_info->num_languages; i++)
331 FREE(windows_info->languages[i]);
332 FREE(windows_info->languages);
333 FREE(windows_info->default_language);
334 FREE(windows_info->system_root);
337 /* Frees memory allocated inside a struct image_info structure. */
339 destroy_image_info(struct image_info *image_info)
341 FREE(image_info->name);
342 FREE(image_info->description);
343 FREE(image_info->flags);
344 FREE(image_info->display_name);
345 FREE(image_info->display_description);
346 destroy_windows_info(&image_info->windows_info);
347 memset(image_info, 0, sizeof(struct image_info));
351 free_wim_info(struct wim_info *info)
355 for (int i = 0; i < info->num_images; i++)
356 destroy_image_info(&info->images[i]);
363 /* Reads the information from a <VERSION> element inside the <WINDOWS> element.
366 xml_read_windows_version(const xmlNode *version_node,
367 struct windows_version* windows_version)
370 for_node_child(version_node, child) {
371 if (!node_is_element(child))
373 if (node_name_is(child, "MAJOR"))
374 windows_version->major = node_get_u64(child);
375 else if (node_name_is(child, "MINOR"))
376 windows_version->minor = node_get_u64(child);
377 else if (node_name_is(child, "BUILD"))
378 windows_version->build = node_get_u64(child);
379 else if (node_name_is(child, "SPBUILD"))
380 windows_version->sp_build = node_get_u64(child);
381 else if (node_name_is(child, "SPLEVEL"))
382 windows_version->sp_level = node_get_u64(child);
386 /* Reads the information from a <LANGUAGE> element inside a <WINDOWS> element.
389 xml_read_languages(const xmlNode *languages_node,
390 tchar ***languages_ret,
391 size_t *num_languages_ret,
392 tchar **default_language_ret)
395 size_t num_languages = 0;
399 for_node_child(languages_node, child)
400 if (node_is_element(child) && node_name_is(child, "LANGUAGE"))
403 languages = CALLOC(num_languages, sizeof(languages[0]));
405 return WIMLIB_ERR_NOMEM;
407 *languages_ret = languages;
408 *num_languages_ret = num_languages;
411 for_node_child(languages_node, child) {
412 if (!node_is_element(child))
414 if (node_name_is(child, "LANGUAGE"))
415 ret = node_get_string(child, languages++);
416 else if (node_name_is(child, "DEFAULT"))
417 ret = node_get_string(child, default_language_ret);
424 /* Reads the information from a <WINDOWS> element inside an <IMAGE> element. */
426 xml_read_windows_info(const xmlNode *windows_node,
427 struct windows_info *windows_info)
432 for_node_child(windows_node, child) {
433 if (!node_is_element(child))
435 if (node_name_is(child, "ARCH")) {
436 windows_info->arch = node_get_u64(child);
437 } else if (node_name_is(child, "PRODUCTNAME")) {
438 ret = node_get_string(child,
439 &windows_info->product_name);
440 } else if (node_name_is(child, "EDITIONID")) {
441 ret = node_get_string(child,
442 &windows_info->edition_id);
443 } else if (node_name_is(child, "INSTALLATIONTYPE")) {
444 ret = node_get_string(child,
445 &windows_info->installation_type);
446 } else if (node_name_is(child, "PRODUCTTYPE")) {
447 ret = node_get_string(child,
448 &windows_info->product_type);
449 } else if (node_name_is(child, "PRODUCTSUITE")) {
450 ret = node_get_string(child,
451 &windows_info->product_suite);
452 } else if (node_name_is(child, "LANGUAGES")) {
453 ret = xml_read_languages(child,
454 &windows_info->languages,
455 &windows_info->num_languages,
456 &windows_info->default_language);
457 } else if (node_name_is(child, "VERSION")) {
458 xml_read_windows_version(child,
459 &windows_info->windows_version);
460 windows_info->windows_version_exists = true;
461 } else if (node_name_is(child, "SYSTEMROOT")) {
462 ret = node_get_string(child, &windows_info->system_root);
463 } else if (node_name_is(child, "HAL")) {
464 ret = node_get_string(child, &windows_info->hal);
465 } else if (node_name_is(child, "SERVICINGDATA")) {
468 for_node_child(child, grandchild) {
469 if (node_is_element(grandchild) &&
470 node_name_is(grandchild, "PKEYCONFIGVERSION"))
472 ret = node_get_string(grandchild,
473 &windows_info->pkeyconfigversion);
484 /* Reads the information from an <IMAGE> element. */
486 xml_read_image_info(xmlNode *image_node, struct image_info *image_info)
492 index_prop = xmlGetProp(image_node, "INDEX");
494 image_info->index = atoi(index_prop);
497 image_info->index = 1;
501 for_node_child(image_node, child) {
502 if (!node_is_element(child))
504 if (node_name_is(child, "DIRCOUNT"))
505 image_info->dir_count = node_get_u64(child);
506 else if (node_name_is(child, "FILECOUNT"))
507 image_info->file_count = node_get_u64(child);
508 else if (node_name_is(child, "TOTALBYTES"))
509 image_info->total_bytes = node_get_u64(child);
510 else if (node_name_is(child, "HARDLINKBYTES"))
511 image_info->hard_link_bytes = node_get_u64(child);
512 else if (node_name_is(child, "CREATIONTIME"))
513 image_info->creation_time = node_get_timestamp(child);
514 else if (node_name_is(child, "LASTMODIFICATIONTIME"))
515 image_info->last_modification_time = node_get_timestamp(child);
516 else if (node_name_is(child, "WINDOWS")) {
517 DEBUG("Found <WINDOWS> tag");
518 ret = xml_read_windows_info(child,
519 &image_info->windows_info);
520 image_info->windows_info_exists = true;
521 } else if (node_name_is(child, "NAME")) {
522 ret = node_get_string(child, &image_info->name);
523 } else if (node_name_is(child, "DESCRIPTION")) {
524 ret = node_get_string(child, &image_info->description);
525 } else if (node_name_is(child, "FLAGS")) {
526 ret = node_get_string(child, &image_info->flags);
527 } else if (node_name_is(child, "DISPLAYNAME")) {
528 ret = node_get_string(child, &image_info->display_name);
529 } else if (node_name_is(child, "DISPLAYDESCRIPTION")) {
530 ret = node_get_string(child, &image_info->display_description);
531 } else if (node_name_is(child, "WIMBOOT")) {
532 if (node_get_u64(child) == 1) {
533 image_info->wimboot = true;
539 if (!image_info->name) {
541 /*WARNING("Image with index %d has no name", image_info->index);*/
542 empty_name = MALLOC(sizeof(tchar));
544 return WIMLIB_ERR_NOMEM;
545 *empty_name = T('\0');
546 image_info->name = empty_name;
551 /* Reads the information from a <WIM> element, which should be the root element
552 * of the XML tree. */
554 xml_read_wim_info(const xmlNode *wim_node, struct wim_info **wim_info_ret)
556 struct wim_info *wim_info;
562 wim_info = CALLOC(1, sizeof(struct wim_info));
564 return WIMLIB_ERR_NOMEM;
566 /* Count how many images there are. */
568 for_node_child(wim_node, child) {
569 if (node_is_element(child) && node_name_is(child, "IMAGE")) {
570 if (unlikely(num_images == MAX_IMAGES)) {
571 ret = WIMLIB_ERR_IMAGE_COUNT;
578 if (num_images > 0) {
579 /* Allocate the array of struct image_infos and fill them in. */
580 wim_info->images = CALLOC(num_images, sizeof(wim_info->images[0]));
581 if (!wim_info->images) {
582 ret = WIMLIB_ERR_NOMEM;
585 wim_info->num_images = num_images;
587 for_node_child(wim_node, child) {
588 if (!node_is_element(child))
590 if (node_name_is(child, "IMAGE")) {
591 DEBUG("Found <IMAGE> tag");
592 ret = xml_read_image_info(child,
593 &wim_info->images[i]);
597 } else if (node_name_is(child, "TOTALBYTES")) {
598 wim_info->total_bytes = node_get_u64(child);
599 } else if (node_name_is(child, "ESD")) {
601 for_node_child(child, esdchild) {
602 if (node_is_element(esdchild) &&
603 node_name_is(esdchild, "ENCRYPTED"))
605 ret = WIMLIB_ERR_WIM_IS_ENCRYPTED;
612 /* Sort the array of image info by image index. */
613 qsort(wim_info->images, num_images,
614 sizeof(struct image_info), sort_by_index);
616 /* Make sure the image indices make sense */
617 for (i = 0; i < num_images; i++) {
618 if (wim_info->images[i].index != i + 1) {
619 ERROR("WIM images are not indexed [1...%d] "
620 "in XML data as expected",
622 ret = WIMLIB_ERR_IMAGE_COUNT;
628 *wim_info_ret = wim_info;
631 free_wim_info(wim_info);
635 /* Prints the information contained in a `struct windows_info'.
637 * Warning: any strings printed here are in UTF-8 encoding. If the locale
638 * character encoding is not UTF-8, the printed strings may be garbled. */
640 print_windows_info(const struct windows_info *windows_info)
642 const struct windows_version *windows_version;
644 tprintf(T("Architecture: %"TS"\n"),
645 get_arch(windows_info->arch));
647 if (windows_info->product_name) {
648 tprintf(T("Product Name: %"TS"\n"),
649 windows_info->product_name);
652 if (windows_info->edition_id) {
653 tprintf(T("Edition ID: %"TS"\n"),
654 windows_info->edition_id);
657 if (windows_info->installation_type) {
658 tprintf(T("Installation Type: %"TS"\n"),
659 windows_info->installation_type);
662 if (windows_info->hal) {
663 tprintf(T("HAL: %"TS"\n"),
667 if (windows_info->product_type) {
668 tprintf(T("Product Type: %"TS"\n"),
669 windows_info->product_type);
672 if (windows_info->product_suite) {
673 tprintf(T("Product Suite: %"TS"\n"),
674 windows_info->product_suite);
677 tprintf(T("Languages: "));
678 for (size_t i = 0; i < windows_info->num_languages; i++) {
680 tfputs(windows_info->languages[i], stdout);
684 if (windows_info->default_language) {
685 tprintf(T("Default Language: %"TS"\n"),
686 windows_info->default_language);
688 if (windows_info->system_root) {
689 tprintf(T("System Root: %"TS"\n"),
690 windows_info->system_root);
693 if (windows_info->windows_version_exists) {
694 windows_version = &windows_info->windows_version;
695 tprintf(T("Major Version: %"PRIu64"\n"),
696 windows_version->major);
697 tprintf(T("Minor Version: %"PRIu64"\n"),
698 windows_version->minor);
699 tprintf(T("Build: %"PRIu64"\n"),
700 windows_version->build);
701 tprintf(T("Service Pack Build: %"PRIu64"\n"),
702 windows_version->sp_build);
703 tprintf(T("Service Pack Level: %"PRIu64"\n"),
704 windows_version->sp_level);
709 xml_write_string(xmlTextWriter *writer, const char *name,
714 int rc = tstr_to_utf8_simple(tstr, &utf8_str);
717 rc = xmlTextWriterWriteElement(writer, name, utf8_str);
726 xml_write_strings_from_specs(xmlTextWriter *writer,
727 const void *struct_with_strings,
728 const struct xml_string_spec specs[],
731 for (size_t i = 0; i < num_specs; i++) {
732 int rc = xml_write_string(writer, specs[i].name,
733 *(const tchar * const *)
734 (struct_with_strings + specs[i].offset));
742 dup_strings_from_specs(const void *old_struct_with_strings,
743 void *new_struct_with_strings,
744 const struct xml_string_spec specs[],
747 for (size_t i = 0; i < num_specs; i++) {
748 const tchar *old_str = *(const tchar * const *)
749 ((const void*)old_struct_with_strings + specs[i].offset);
750 tchar **new_str_p = (tchar **)((void*)new_struct_with_strings + specs[i].offset);
752 *new_str_p = TSTRDUP(old_str);
754 return WIMLIB_ERR_NOMEM;
760 /* Writes the information contained in a `struct windows_version' to the XML
761 * document being written. This is the <VERSION> element inside the <WINDOWS>
764 xml_write_windows_version(xmlTextWriter *writer,
765 const struct windows_version *version)
768 rc = xmlTextWriterStartElement(writer, "VERSION");
772 rc = xmlTextWriterWriteFormatElement(writer, "MAJOR", "%"PRIu64,
777 rc = xmlTextWriterWriteFormatElement(writer, "MINOR", "%"PRIu64,
782 rc = xmlTextWriterWriteFormatElement(writer, "BUILD", "%"PRIu64,
787 rc = xmlTextWriterWriteFormatElement(writer, "SPBUILD", "%"PRIu64,
792 rc = xmlTextWriterWriteFormatElement(writer, "SPLEVEL", "%"PRIu64,
797 rc = xmlTextWriterEndElement(writer); /* </VERSION> */
804 /* Writes the information contained in a `struct windows_info' to the XML
805 * document being written. This is the <WINDOWS> element. */
807 xml_write_windows_info(xmlTextWriter *writer,
808 const struct windows_info *windows_info)
811 rc = xmlTextWriterStartElement(writer, "WINDOWS");
815 rc = xmlTextWriterWriteFormatElement(writer, "ARCH", "%"PRIu64,
820 rc = xml_write_strings_from_specs(writer,
822 windows_info_xml_string_specs,
823 ARRAY_LEN(windows_info_xml_string_specs));
827 if (windows_info->num_languages) {
828 rc = xmlTextWriterStartElement(writer, "LANGUAGES");
832 for (size_t i = 0; i < windows_info->num_languages; i++) {
833 rc = xml_write_string(writer, "LANGUAGE",
834 windows_info->languages[i]);
839 rc = xml_write_string(writer, "DEFAULT",
840 windows_info->default_language);
844 rc = xmlTextWriterEndElement(writer); /* </LANGUAGES> */
849 if (windows_info->pkeyconfigversion) {
850 rc = xmlTextWriterStartElement(writer, "SERVICINGDATA");
854 rc = xml_write_string(writer, "PKEYCONFIGVERSION",
855 windows_info->pkeyconfigversion);
859 rc = xmlTextWriterEndElement(writer);
864 if (windows_info->windows_version_exists) {
865 rc = xml_write_windows_version(writer, &windows_info->windows_version);
870 rc = xml_write_string(writer, "SYSTEMROOT", windows_info->system_root);
874 rc = xmlTextWriterEndElement(writer); /* </WINDOWS> */
881 /* Writes a time element to the XML document being constructed in memory. */
883 xml_write_time(xmlTextWriter *writer, const char *element_name, u64 time)
886 rc = xmlTextWriterStartElement(writer, element_name);
890 rc = xmlTextWriterWriteFormatElement(writer, "HIGHPART",
891 "0x%08"PRIX32, (u32)(time >> 32));
895 rc = xmlTextWriterWriteFormatElement(writer, "LOWPART",
896 "0x%08"PRIX32, (u32)time);
900 rc = xmlTextWriterEndElement(writer); /* </@element_name> */
906 /* Writes an <IMAGE> element to the XML document. */
908 xml_write_image_info(xmlTextWriter *writer, const struct image_info *image_info)
911 rc = xmlTextWriterStartElement(writer, "IMAGE");
915 rc = xmlTextWriterWriteFormatAttribute(writer, "INDEX", "%d",
920 rc = xmlTextWriterWriteFormatElement(writer, "DIRCOUNT", "%"PRIu64,
921 image_info->dir_count);
925 rc = xmlTextWriterWriteFormatElement(writer, "FILECOUNT", "%"PRIu64,
926 image_info->file_count);
930 rc = xmlTextWriterWriteFormatElement(writer, "TOTALBYTES", "%"PRIu64,
931 image_info->total_bytes);
935 rc = xmlTextWriterWriteFormatElement(writer, "HARDLINKBYTES", "%"PRIu64,
936 image_info->hard_link_bytes);
940 rc = xml_write_time(writer, "CREATIONTIME", image_info->creation_time);
944 rc = xml_write_time(writer, "LASTMODIFICATIONTIME",
945 image_info->last_modification_time);
949 if (image_info->windows_info_exists) {
950 rc = xml_write_windows_info(writer, &image_info->windows_info);
955 rc = xml_write_strings_from_specs(writer, image_info,
956 image_info_xml_string_specs,
957 ARRAY_LEN(image_info_xml_string_specs));
961 if (image_info->wimboot) {
962 rc = xmlTextWriterWriteFormatElement(writer, "WIMBOOT", "%d", 1);
967 rc = xmlTextWriterEndElement(writer); /* </IMAGE> */
976 /* Makes space for another image in the XML information and return a pointer to
978 static struct image_info *
979 add_image_info_struct(struct wim_info *wim_info)
981 struct image_info *images;
983 images = CALLOC(wim_info->num_images + 1, sizeof(struct image_info));
986 memcpy(images, wim_info->images,
987 wim_info->num_images * sizeof(struct image_info));
988 FREE(wim_info->images);
989 wim_info->images = images;
990 wim_info->num_images++;
991 return &images[wim_info->num_images - 1];
995 clone_windows_info(const struct windows_info *old, struct windows_info *new)
999 new->arch = old->arch;
1001 ret = dup_strings_from_specs(old, new, windows_info_xml_string_specs,
1002 ARRAY_LEN(windows_info_xml_string_specs));
1006 if (old->pkeyconfigversion) {
1007 new->pkeyconfigversion = TSTRDUP(old->pkeyconfigversion);
1008 if (new->pkeyconfigversion == NULL)
1009 return WIMLIB_ERR_NOMEM;
1012 if (old->languages) {
1013 new->languages = CALLOC(old->num_languages, sizeof(new->languages[0]));
1014 if (!new->languages)
1015 return WIMLIB_ERR_NOMEM;
1016 new->num_languages = old->num_languages;
1017 for (size_t i = 0; i < new->num_languages; i++) {
1018 if (!old->languages[i])
1020 new->languages[i] = TSTRDUP(old->languages[i]);
1021 if (!new->languages[i])
1022 return WIMLIB_ERR_NOMEM;
1025 if (old->default_language &&
1026 !(new->default_language = TSTRDUP(old->default_language)))
1027 return WIMLIB_ERR_NOMEM;
1028 if (old->system_root && !(new->system_root = TSTRDUP(old->system_root)))
1029 return WIMLIB_ERR_NOMEM;
1030 if (old->windows_version_exists) {
1031 new->windows_version_exists = true;
1032 memcpy(&new->windows_version, &old->windows_version,
1033 sizeof(old->windows_version));
1039 clone_image_info(const struct image_info *old, struct image_info *new)
1043 new->dir_count = old->dir_count;
1044 new->file_count = old->file_count;
1045 new->total_bytes = old->total_bytes;
1046 new->hard_link_bytes = old->hard_link_bytes;
1047 new->creation_time = old->creation_time;
1048 new->last_modification_time = old->last_modification_time;
1050 ret = dup_strings_from_specs(old, new,
1051 image_info_xml_string_specs,
1052 ARRAY_LEN(image_info_xml_string_specs));
1056 if (old->windows_info_exists) {
1057 new->windows_info_exists = true;
1058 ret = clone_windows_info(&old->windows_info,
1059 &new->windows_info);
1063 new->wimboot = old->wimboot;
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;
1180 /* Update directory count and file count.
1182 * Each dentry counts as either a file or a directory, but not both.
1183 * The root directory is an exception: it is not counted at all.
1185 * Symbolic links and junction points (and presumably other reparse
1186 * points) count as regular files. This is despite the fact that
1187 * junction points have FILE_ATTRIBUTE_DIRECTORY set.
1190 if (!dentry_is_root(dentry)) {
1191 if (inode_is_directory(inode))
1198 * Update total bytes and hard link bytes.
1200 * We try to act the same as the MS implementation, even though there
1201 * are some inconsistencies/bugs in the way it operates.
1203 * If there are no alternate data streams in the image, the "total
1204 * bytes" is the sum of the size of the un-named data stream of each
1205 * inode times the link count of that inode. In other words, it would
1206 * be the total number of bytes of regular files you would have if you
1207 * extracted the full image without any hard-links. The "hard link
1208 * bytes" is equal to the "total bytes" minus the size of the un-named
1209 * data stream of each inode. In other words, the "hard link bytes"
1210 * counts the size of the un-named data stream for all the links to each
1211 * inode except the first one.
1213 * Reparse points and directories don't seem to be counted in either the
1214 * total bytes or the hard link bytes.
1216 * And now we get to the most confusing part, the alternate data
1217 * streams. They are not counted in the "total bytes". However, if the
1218 * link count of an inode with alternate data streams is 2 or greater,
1219 * the size of all the alternate data streams is included in the "hard
1220 * link bytes", and this size is multiplied by the link count (NOT one
1221 * less than the link count).
1223 if (!(inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
1224 FILE_ATTRIBUTE_REPARSE_POINT)))
1226 struct wim_lookup_table_entry *lte;
1228 lte = inode_unnamed_lte(inode, info->lookup_table);
1230 info->total_bytes += lte->size;
1231 if (!dentry_is_first_in_inode(dentry))
1232 info->hard_link_bytes += lte->size;
1235 if (inode->i_nlink >= 2 && dentry_is_first_in_inode(dentry)) {
1236 for (unsigned i = 0; i < inode->i_num_ads; i++) {
1237 if (inode->i_ads_entries[i].stream_name_nbytes) {
1238 lte = inode_stream_lte(inode, i + 1, info->lookup_table);
1240 info->hard_link_bytes += inode->i_nlink *
1251 * Calculate what to put in the <FILECOUNT>, <DIRCOUNT>, <TOTALBYTES>, and
1252 * <HARDLINKBYTES> elements of each <IMAGE>.
1254 * Please note there is no official documentation for exactly how this is done.
1255 * But, see calculate_dentry_statistics().
1258 xml_update_image_info(WIMStruct *wim, int image)
1260 struct image_info *image_info;
1262 DEBUG("Updating the image info for image %d", image);
1264 image_info = &wim->wim_info->images[image - 1];
1266 image_info->file_count = 0;
1267 image_info->dir_count = 0;
1268 image_info->total_bytes = 0;
1269 image_info->hard_link_bytes = 0;
1270 image_info->lookup_table = wim->lookup_table;
1272 for_dentry_in_tree(wim->image_metadata[image - 1]->root_dentry,
1273 calculate_dentry_statistics,
1275 image_info->last_modification_time = get_wim_timestamp();
1278 /* Adds an image to the XML information. */
1280 xml_add_image(WIMStruct *wim, const tchar *name)
1282 struct wim_info *wim_info;
1283 struct image_info *image_info;
1285 wimlib_assert(name != NULL);
1287 /* If this is the first image, allocate the struct wim_info. Otherwise
1288 * use the existing struct wim_info. */
1289 if (wim->wim_info) {
1290 wim_info = wim->wim_info;
1292 wim_info = CALLOC(1, sizeof(struct wim_info));
1294 return WIMLIB_ERR_NOMEM;
1297 image_info = add_image_info_struct(wim_info);
1299 goto out_free_wim_info;
1301 if (!(image_info->name = TSTRDUP(name)))
1302 goto out_destroy_image_info;
1304 wim->wim_info = wim_info;
1305 image_info->index = wim_info->num_images;
1306 image_info->creation_time = get_wim_timestamp();
1307 xml_update_image_info(wim, image_info->index);
1310 out_destroy_image_info:
1311 destroy_image_info(image_info);
1312 wim_info->num_images--;
1314 if (wim_info != wim->wim_info)
1316 return WIMLIB_ERR_NOMEM;
1319 /* Prints information about the specified image from struct wim_info structure.
1322 print_image_info(const struct wim_info *wim_info, int image)
1324 const struct image_info *image_info;
1328 wimlib_assert(image >= 1 && image <= wim_info->num_images);
1330 image_info = &wim_info->images[image - 1];
1332 tprintf(T("Index: %d\n"), image_info->index);
1333 tprintf(T("Name: %"TS"\n"), image_info->name);
1335 /* Always print the Description: part even if there is no
1337 if (image_info->description)
1338 desc = image_info->description;
1341 tprintf(T("Description: %"TS"\n"), desc);
1343 if (image_info->display_name) {
1344 tprintf(T("Display Name: %"TS"\n"),
1345 image_info->display_name);
1348 if (image_info->display_description) {
1349 tprintf(T("Display Description: %"TS"\n"),
1350 image_info->display_description);
1353 tprintf(T("Directory Count: %"PRIu64"\n"), image_info->dir_count);
1354 tprintf(T("File Count: %"PRIu64"\n"), image_info->file_count);
1355 tprintf(T("Total Bytes: %"PRIu64"\n"), image_info->total_bytes);
1356 tprintf(T("Hard Link Bytes: %"PRIu64"\n"), image_info->hard_link_bytes);
1358 wim_timestamp_to_str(image_info->creation_time, buf, sizeof(buf));
1359 tprintf(T("Creation Time: %"TS"\n"), buf);
1361 wim_timestamp_to_str(image_info->last_modification_time, buf, sizeof(buf));
1362 tprintf(T("Last Modification Time: %"TS"\n"), buf);
1363 if (image_info->windows_info_exists)
1364 print_windows_info(&image_info->windows_info);
1365 if (image_info->flags)
1366 tprintf(T("Flags: %"TS"\n"), image_info->flags);
1367 tprintf(T("WIMBoot compatible: %"TS"\n"),
1368 image_info->wimboot ? T("yes") : T("no"));
1373 libxml_global_init(void)
1376 xmlInitCharEncodingHandlers();
1380 libxml_global_cleanup(void)
1383 xmlCleanupCharEncodingHandlers();
1386 /* Reads the XML data from a WIM file. */
1388 read_wim_xml_data(WIMStruct *wim)
1397 ret = wimlib_get_xml_data(wim, &buf, &bufsize);
1402 doc = xmlReadMemory((const char *)xml_data, bufsize,
1403 NULL, "UTF-16LE", 0);
1405 ERROR("Failed to parse XML data");
1406 ret = WIMLIB_ERR_XML;
1407 goto out_free_xml_data;
1410 root = xmlDocGetRootElement(doc);
1411 if (!root || !node_is_element(root) || !node_name_is(root, "WIM")) {
1412 ERROR("WIM XML data is invalid");
1413 ret = WIMLIB_ERR_XML;
1417 ret = xml_read_wim_info(root, &wim->wim_info);
1426 /* Prepares an in-memory buffer containing the UTF-16LE XML data for a WIM file.
1428 * total_bytes is the number to write in <TOTALBYTES>, or
1429 * WIM_TOTALBYTES_USE_EXISTING to use the existing value in memory, or
1430 * WIM_TOTALBYTES_OMIT to omit <TOTALBYTES> entirely.
1433 prepare_wim_xml_data(WIMStruct *wim, int image, u64 total_bytes,
1434 u8 **xml_data_ret, size_t *xml_len_ret)
1436 xmlCharEncodingHandler *encoding_handler;
1438 xmlOutputBuffer *outbuf;
1439 xmlTextWriter *writer;
1442 const xmlChar *content;
1447 /* Open an xmlTextWriter that writes to an in-memory buffer using
1448 * UTF-16LE encoding. */
1450 encoding_handler = xmlGetCharEncodingHandler(XML_CHAR_ENCODING_UTF16LE);
1451 if (!encoding_handler) {
1452 ERROR("Failed to get XML character encoding handler for UTF-16LE");
1453 ret = WIMLIB_ERR_LIBXML_UTF16_HANDLER_NOT_AVAILABLE;
1457 buf = xmlBufferCreate();
1459 ERROR("Failed to create xmlBuffer");
1460 ret = WIMLIB_ERR_NOMEM;
1464 outbuf = xmlOutputBufferCreateBuffer(buf, encoding_handler);
1466 ERROR("Failed to allocate xmlOutputBuffer");
1467 ret = WIMLIB_ERR_NOMEM;
1468 goto out_buffer_free;
1471 writer = xmlNewTextWriter(outbuf);
1473 ERROR("Failed to allocate xmlTextWriter");
1474 ret = WIMLIB_ERR_NOMEM;
1475 goto out_output_buffer_close;
1478 /* Write the XML document. */
1480 ret = xmlTextWriterStartElement(writer, "WIM");
1482 goto out_write_error;
1484 /* The contents of the <TOTALBYTES> element in the XML data, under the
1485 * <WIM> element (not the <IMAGE> element), is for non-split WIMs the
1486 * size of the WIM file excluding the XML data and integrity table.
1487 * For split WIMs, <TOTALBYTES> takes into account the entire WIM, not
1488 * just the current part. */
1489 if (total_bytes != WIM_TOTALBYTES_OMIT) {
1490 if (total_bytes == WIM_TOTALBYTES_USE_EXISTING) {
1492 total_bytes = wim->wim_info->total_bytes;
1496 ret = xmlTextWriterWriteFormatElement(writer, "TOTALBYTES",
1497 "%"PRIu64, total_bytes);
1499 goto out_write_error;
1502 if (image == WIMLIB_ALL_IMAGES) {
1504 last = wim->hdr.image_count;
1510 for (int i = first; i <= last; i++) {
1511 ret = xml_write_image_info(writer, &wim->wim_info->images[i - 1]);
1514 goto out_write_error;
1515 goto out_free_text_writer;
1519 ret = xmlTextWriterEndElement(writer);
1521 goto out_write_error;
1523 ret = xmlTextWriterEndDocument(writer);
1525 goto out_write_error;
1527 ret = xmlTextWriterFlush(writer);
1529 goto out_write_error;
1531 /* Retrieve the buffer into which the document was written. */
1533 content = xmlBufferContent(buf);
1534 len = xmlBufferLength(buf);
1536 /* Copy the data into a new buffer, and prefix it with the UTF-16LE BOM
1537 * (byte order mark), which is required by MS's software to understand
1541 xml_data = MALLOC(xml_len);
1543 ret = WIMLIB_ERR_NOMEM;
1544 goto out_free_text_writer;
1548 memcpy(&xml_data[2], content, len);
1550 /* Clean up libxml objects and return success. */
1551 *xml_data_ret = xml_data;
1552 *xml_len_ret = xml_len;
1554 out_free_text_writer:
1555 /* xmlFreeTextWriter will free the attached xmlOutputBuffer. */
1556 xmlFreeTextWriter(writer);
1557 goto out_buffer_free;
1558 out_output_buffer_close:
1559 xmlOutputBufferClose(outbuf);
1563 DEBUG("ret=%d", ret);
1567 ERROR("Error writing XML data");
1568 ret = WIMLIB_ERR_WRITE;
1569 goto out_free_text_writer;
1572 /* Writes the XML data to a WIM file. */
1574 write_wim_xml_data(WIMStruct *wim, int image, u64 total_bytes,
1575 struct wim_reshdr *out_reshdr,
1576 int write_resource_flags)
1582 DEBUG("Writing WIM XML data (image=%d, offset=%"PRIu64")",
1583 image, wim->out_fd.offset);
1585 ret = prepare_wim_xml_data(wim, image, total_bytes,
1586 &xml_data, &xml_len);
1590 /* Write the XML data uncompressed. Although wimlib can handle
1591 * compressed XML data, MS software cannot. */
1592 ret = write_wim_resource_from_buffer(xml_data,
1594 WIM_RESHDR_FLAG_METADATA,
1596 WIMLIB_COMPRESSION_TYPE_NONE,
1600 write_resource_flags);
1602 DEBUG("ret=%d", ret);
1606 /* API function documented in wimlib.h */
1607 WIMLIBAPI const tchar *
1608 wimlib_get_image_name(const WIMStruct *wim, int image)
1610 if (image < 1 || image > wim->hdr.image_count)
1612 return wim->wim_info->images[image - 1].name;
1615 /* API function documented in wimlib.h */
1616 WIMLIBAPI const tchar *
1617 wimlib_get_image_description(const WIMStruct *wim, int image)
1619 if (image < 1 || image > wim->hdr.image_count)
1621 return wim->wim_info->images[image - 1].description;
1624 /* API function documented in wimlib.h */
1626 wimlib_image_name_in_use(const WIMStruct *wim, const tchar *name)
1628 if (!name || !*name)
1630 for (int i = 1; i <= wim->hdr.image_count; i++)
1631 if (!tstrcmp(wim->wim_info->images[i - 1].name, name))
1637 /* API function documented in wimlib.h */
1639 wimlib_get_xml_data(WIMStruct *wim, void **buf_ret, size_t *bufsize_ret)
1641 const struct wim_reshdr *xml_reshdr;
1643 if (wim->filename == NULL && filedes_is_seekable(&wim->in_fd))
1644 return WIMLIB_ERR_INVALID_PARAM;
1646 if (buf_ret == NULL || bufsize_ret == NULL)
1647 return WIMLIB_ERR_INVALID_PARAM;
1649 xml_reshdr = &wim->hdr.xml_data_reshdr;
1651 DEBUG("Reading XML data.");
1652 *bufsize_ret = xml_reshdr->uncompressed_size;
1653 return wim_reshdr_to_data(xml_reshdr, wim, buf_ret);
1657 wimlib_extract_xml_data(WIMStruct *wim, FILE *fp)
1663 ret = wimlib_get_xml_data(wim, &buf, &bufsize);
1667 if (fwrite(buf, 1, bufsize, fp) != bufsize) {
1668 ERROR_WITH_ERRNO("Failed to extract XML data");
1669 ret = WIMLIB_ERR_WRITE;
1675 /* API function documented in wimlib.h */
1677 wimlib_set_image_name(WIMStruct *wim, int image, const tchar *name)
1683 DEBUG("Setting the name of image %d to %"TS, image, name);
1685 ret = can_modify_wim(wim);
1692 if (image < 1 || image > wim->hdr.image_count) {
1693 ERROR("%d is not a valid image", image);
1694 return WIMLIB_ERR_INVALID_IMAGE;
1697 for (i = 1; i <= wim->hdr.image_count; i++) {
1700 if (!tstrcmp(wim->wim_info->images[i - 1].name, name)) {
1701 ERROR("The name \"%"TS"\" is already in use in the WIM!",
1703 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
1709 return WIMLIB_ERR_NOMEM;
1711 FREE(wim->wim_info->images[image - 1].name);
1712 wim->wim_info->images[image - 1].name = p;
1717 do_set_image_info_str(WIMStruct *wim, int image, const tchar *tstr,
1721 tchar **dest_tstr_p;
1724 ret = can_modify_wim(wim);
1728 if (image < 1 || image > wim->hdr.image_count) {
1729 ERROR("%d is not a valid image", image);
1730 return WIMLIB_ERR_INVALID_IMAGE;
1733 tstr_copy = TSTRDUP(tstr);
1735 return WIMLIB_ERR_NOMEM;
1739 dest_tstr_p = (tchar**)((void*)&wim->wim_info->images[image - 1] + offset);
1742 *dest_tstr_p = tstr_copy;
1746 /* API function documented in wimlib.h */
1748 wimlib_set_image_descripton(WIMStruct *wim, int image,
1749 const tchar *description)
1751 return do_set_image_info_str(wim, image, description,
1752 offsetof(struct image_info, description));
1755 /* API function documented in wimlib.h */
1757 wimlib_set_image_flags(WIMStruct *wim, int image, const tchar *flags)
1759 return do_set_image_info_str(wim, image, flags,
1760 offsetof(struct image_info, flags));