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 free software; you can redistribute it and/or modify it under
11 * the terms of the GNU Lesser General Public License as published by the Free
12 * Software Foundation; either version 3 of the License, or (at your option) any
15 * This file is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this file; if not, see http://www.gnu.org/licenses/.
28 #include <libxml/encoding.h>
29 #include <libxml/parser.h>
30 #include <libxml/tree.h>
31 #include <libxml/xmlwriter.h>
34 #include "wimlib/assert.h"
35 #include "wimlib/dentry.h"
36 #include "wimlib/encoding.h"
37 #include "wimlib/error.h"
38 #include "wimlib/file_io.h"
39 #include "wimlib/lookup_table.h"
40 #include "wimlib/metadata.h"
41 #include "wimlib/resource.h"
42 #include "wimlib/timestamp.h"
43 #include "wimlib/xml.h"
44 #include "wimlib/write.h"
46 /* Structures used to form an in-memory representation of the XML data (other
47 * than the raw parse tree from libxml). */
49 struct windows_version {
61 tchar *installation_type;
62 tchar *pkeyconfigversion;
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;
91 /* Note: must update clone_image_info() if adding new fields here */
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)
276 for_node_child(string_node, child)
277 if (node_is_text(child) && child->content)
278 return utf8_to_tstr_simple(child->content, tstr_ret);
282 /* Returns the timestamp from a time node. It has child elements <HIGHPART> and
283 * <LOWPART> that are then used to construct a 64-bit timestamp. */
285 node_get_timestamp(const xmlNode *time_node)
290 for_node_child(time_node, child) {
291 if (!node_is_element(child))
293 if (node_name_is(child, "HIGHPART"))
294 high_part = node_get_hex_u64(child);
295 else if (node_name_is(child, "LOWPART"))
296 low_part = node_get_hex_u64(child);
298 return (u64)low_part | ((u64)high_part << 32);
301 /* Used to sort an array of struct image_infos by their image indices. */
303 sort_by_index(const void *p1, const void *p2)
305 int index_1 = ((const struct image_info*)p1)->index;
306 int index_2 = ((const struct image_info*)p2)->index;
307 if (index_1 < index_2)
309 else if (index_1 > index_2)
316 /* Frees memory allocated inside a struct windows_info structure. */
318 destroy_windows_info(struct windows_info *windows_info)
320 FREE(windows_info->product_name);
321 FREE(windows_info->edition_id);
322 FREE(windows_info->installation_type);
323 FREE(windows_info->hal);
324 FREE(windows_info->product_type);
325 FREE(windows_info->product_suite);
326 FREE(windows_info->pkeyconfigversion);
327 for (size_t i = 0; i < windows_info->num_languages; i++)
328 FREE(windows_info->languages[i]);
329 FREE(windows_info->languages);
330 FREE(windows_info->default_language);
331 FREE(windows_info->system_root);
334 /* Frees memory allocated inside a struct image_info structure. */
336 destroy_image_info(struct image_info *image_info)
338 FREE(image_info->name);
339 FREE(image_info->description);
340 FREE(image_info->flags);
341 FREE(image_info->display_name);
342 FREE(image_info->display_description);
343 destroy_windows_info(&image_info->windows_info);
344 memset(image_info, 0, sizeof(struct image_info));
348 free_wim_info(struct wim_info *info)
352 for (int i = 0; i < info->num_images; i++)
353 destroy_image_info(&info->images[i]);
360 /* Reads the information from a <VERSION> element inside the <WINDOWS> element.
363 xml_read_windows_version(const xmlNode *version_node,
364 struct windows_version* windows_version)
367 for_node_child(version_node, child) {
368 if (!node_is_element(child))
370 if (node_name_is(child, "MAJOR"))
371 windows_version->major = node_get_u64(child);
372 else if (node_name_is(child, "MINOR"))
373 windows_version->minor = node_get_u64(child);
374 else if (node_name_is(child, "BUILD"))
375 windows_version->build = node_get_u64(child);
376 else if (node_name_is(child, "SPBUILD"))
377 windows_version->sp_build = node_get_u64(child);
378 else if (node_name_is(child, "SPLEVEL"))
379 windows_version->sp_level = node_get_u64(child);
383 /* Reads the information from a <LANGUAGE> element inside a <WINDOWS> element.
386 xml_read_languages(const xmlNode *languages_node,
387 tchar ***languages_ret,
388 size_t *num_languages_ret,
389 tchar **default_language_ret)
392 size_t num_languages = 0;
396 for_node_child(languages_node, child)
397 if (node_is_element(child) && node_name_is(child, "LANGUAGE"))
400 languages = CALLOC(num_languages, sizeof(languages[0]));
402 return WIMLIB_ERR_NOMEM;
404 *languages_ret = languages;
405 *num_languages_ret = num_languages;
408 for_node_child(languages_node, child) {
409 if (!node_is_element(child))
411 if (node_name_is(child, "LANGUAGE"))
412 ret = node_get_string(child, languages++);
413 else if (node_name_is(child, "DEFAULT"))
414 ret = node_get_string(child, default_language_ret);
421 /* Reads the information from a <WINDOWS> element inside an <IMAGE> element. */
423 xml_read_windows_info(const xmlNode *windows_node,
424 struct windows_info *windows_info)
429 for_node_child(windows_node, child) {
430 if (!node_is_element(child))
432 if (node_name_is(child, "ARCH")) {
433 windows_info->arch = node_get_u64(child);
434 } else if (node_name_is(child, "PRODUCTNAME")) {
435 ret = node_get_string(child,
436 &windows_info->product_name);
437 } else if (node_name_is(child, "EDITIONID")) {
438 ret = node_get_string(child,
439 &windows_info->edition_id);
440 } else if (node_name_is(child, "INSTALLATIONTYPE")) {
441 ret = node_get_string(child,
442 &windows_info->installation_type);
443 } else if (node_name_is(child, "PRODUCTTYPE")) {
444 ret = node_get_string(child,
445 &windows_info->product_type);
446 } else if (node_name_is(child, "PRODUCTSUITE")) {
447 ret = node_get_string(child,
448 &windows_info->product_suite);
449 } else if (node_name_is(child, "LANGUAGES")) {
450 ret = xml_read_languages(child,
451 &windows_info->languages,
452 &windows_info->num_languages,
453 &windows_info->default_language);
454 } else if (node_name_is(child, "VERSION")) {
455 xml_read_windows_version(child,
456 &windows_info->windows_version);
457 windows_info->windows_version_exists = true;
458 } else if (node_name_is(child, "SYSTEMROOT")) {
459 ret = node_get_string(child, &windows_info->system_root);
460 } else if (node_name_is(child, "HAL")) {
461 ret = node_get_string(child, &windows_info->hal);
462 } else if (node_name_is(child, "SERVICINGDATA")) {
465 for_node_child(child, grandchild) {
466 if (node_is_element(grandchild) &&
467 node_name_is(grandchild, "PKEYCONFIGVERSION"))
469 ret = node_get_string(grandchild,
470 &windows_info->pkeyconfigversion);
481 /* Reads the information from an <IMAGE> element. */
483 xml_read_image_info(xmlNode *image_node, struct image_info *image_info)
489 index_prop = xmlGetProp(image_node, "INDEX");
491 image_info->index = atoi(index_prop);
494 image_info->index = 1;
498 for_node_child(image_node, child) {
499 if (!node_is_element(child))
501 if (node_name_is(child, "DIRCOUNT"))
502 image_info->dir_count = node_get_u64(child);
503 else if (node_name_is(child, "FILECOUNT"))
504 image_info->file_count = node_get_u64(child);
505 else if (node_name_is(child, "TOTALBYTES"))
506 image_info->total_bytes = node_get_u64(child);
507 else if (node_name_is(child, "HARDLINKBYTES"))
508 image_info->hard_link_bytes = node_get_u64(child);
509 else if (node_name_is(child, "CREATIONTIME"))
510 image_info->creation_time = node_get_timestamp(child);
511 else if (node_name_is(child, "LASTMODIFICATIONTIME"))
512 image_info->last_modification_time = node_get_timestamp(child);
513 else if (node_name_is(child, "WINDOWS")) {
514 ret = xml_read_windows_info(child,
515 &image_info->windows_info);
516 image_info->windows_info_exists = true;
517 } else if (node_name_is(child, "NAME")) {
518 ret = node_get_string(child, &image_info->name);
519 } else if (node_name_is(child, "DESCRIPTION")) {
520 ret = node_get_string(child, &image_info->description);
521 } else if (node_name_is(child, "FLAGS")) {
522 ret = node_get_string(child, &image_info->flags);
523 } else if (node_name_is(child, "DISPLAYNAME")) {
524 ret = node_get_string(child, &image_info->display_name);
525 } else if (node_name_is(child, "DISPLAYDESCRIPTION")) {
526 ret = node_get_string(child, &image_info->display_description);
527 } else if (node_name_is(child, "WIMBOOT")) {
528 if (node_get_u64(child) == 1) {
529 image_info->wimboot = true;
535 if (!image_info->name) {
537 empty_name = MALLOC(sizeof(tchar));
539 return WIMLIB_ERR_NOMEM;
540 *empty_name = T('\0');
541 image_info->name = empty_name;
546 /* Reads the information from a <WIM> element, which should be the root element
547 * of the XML tree. */
549 xml_read_wim_info(const xmlNode *wim_node, struct wim_info **wim_info_ret)
551 struct wim_info *wim_info;
557 wim_info = CALLOC(1, sizeof(struct wim_info));
559 return WIMLIB_ERR_NOMEM;
561 /* Count how many images there are. */
563 for_node_child(wim_node, child) {
564 if (node_is_element(child) && node_name_is(child, "IMAGE")) {
565 if (unlikely(num_images == MAX_IMAGES)) {
566 ret = WIMLIB_ERR_IMAGE_COUNT;
573 if (num_images > 0) {
574 /* Allocate the array of struct image_infos and fill them in. */
575 wim_info->images = CALLOC(num_images, sizeof(wim_info->images[0]));
576 if (!wim_info->images) {
577 ret = WIMLIB_ERR_NOMEM;
580 wim_info->num_images = num_images;
582 for_node_child(wim_node, child) {
583 if (!node_is_element(child))
585 if (node_name_is(child, "IMAGE")) {
586 ret = xml_read_image_info(child,
587 &wim_info->images[i]);
591 } else if (node_name_is(child, "TOTALBYTES")) {
592 wim_info->total_bytes = node_get_u64(child);
593 } else if (node_name_is(child, "ESD")) {
595 for_node_child(child, esdchild) {
596 if (node_is_element(esdchild) &&
597 node_name_is(esdchild, "ENCRYPTED"))
599 ret = WIMLIB_ERR_WIM_IS_ENCRYPTED;
606 /* Sort the array of image info by image index. */
607 qsort(wim_info->images, num_images,
608 sizeof(struct image_info), sort_by_index);
610 /* Make sure the image indices make sense */
611 for (i = 0; i < num_images; i++) {
612 if (wim_info->images[i].index != i + 1) {
613 ERROR("WIM images are not indexed [1...%d] "
614 "in XML data as expected",
616 ret = WIMLIB_ERR_IMAGE_COUNT;
622 *wim_info_ret = wim_info;
625 free_wim_info(wim_info);
629 /* Prints the information contained in a `struct windows_info'. */
631 print_windows_info(const struct windows_info *windows_info)
633 const struct windows_version *windows_version;
635 tprintf(T("Architecture: %"TS"\n"),
636 get_arch(windows_info->arch));
638 if (windows_info->product_name) {
639 tprintf(T("Product Name: %"TS"\n"),
640 windows_info->product_name);
643 if (windows_info->edition_id) {
644 tprintf(T("Edition ID: %"TS"\n"),
645 windows_info->edition_id);
648 if (windows_info->installation_type) {
649 tprintf(T("Installation Type: %"TS"\n"),
650 windows_info->installation_type);
653 if (windows_info->hal) {
654 tprintf(T("HAL: %"TS"\n"),
658 if (windows_info->product_type) {
659 tprintf(T("Product Type: %"TS"\n"),
660 windows_info->product_type);
663 if (windows_info->product_suite) {
664 tprintf(T("Product Suite: %"TS"\n"),
665 windows_info->product_suite);
668 tprintf(T("Languages: "));
669 for (size_t i = 0; i < windows_info->num_languages; i++) {
671 tfputs(windows_info->languages[i], stdout);
675 if (windows_info->default_language) {
676 tprintf(T("Default Language: %"TS"\n"),
677 windows_info->default_language);
679 if (windows_info->system_root) {
680 tprintf(T("System Root: %"TS"\n"),
681 windows_info->system_root);
684 if (windows_info->windows_version_exists) {
685 windows_version = &windows_info->windows_version;
686 tprintf(T("Major Version: %"PRIu64"\n"),
687 windows_version->major);
688 tprintf(T("Minor Version: %"PRIu64"\n"),
689 windows_version->minor);
690 tprintf(T("Build: %"PRIu64"\n"),
691 windows_version->build);
692 tprintf(T("Service Pack Build: %"PRIu64"\n"),
693 windows_version->sp_build);
694 tprintf(T("Service Pack Level: %"PRIu64"\n"),
695 windows_version->sp_level);
700 xml_write_string(xmlTextWriter *writer, const char *name,
705 int rc = tstr_to_utf8_simple(tstr, &utf8_str);
708 rc = xmlTextWriterWriteElement(writer, name, utf8_str);
717 xml_write_strings_from_specs(xmlTextWriter *writer,
718 const void *struct_with_strings,
719 const struct xml_string_spec specs[],
722 for (size_t i = 0; i < num_specs; i++) {
723 int rc = xml_write_string(writer, specs[i].name,
724 *(const tchar * const *)
725 (struct_with_strings + specs[i].offset));
733 dup_strings_from_specs(const void *old_struct_with_strings,
734 void *new_struct_with_strings,
735 const struct xml_string_spec specs[],
738 for (size_t i = 0; i < num_specs; i++) {
739 const tchar *old_str = *(const tchar * const *)
740 ((const void*)old_struct_with_strings + specs[i].offset);
741 tchar **new_str_p = (tchar **)((void*)new_struct_with_strings + specs[i].offset);
743 *new_str_p = TSTRDUP(old_str);
745 return WIMLIB_ERR_NOMEM;
751 /* Writes the information contained in a `struct windows_version' to the XML
752 * document being written. This is the <VERSION> element inside the <WINDOWS>
755 xml_write_windows_version(xmlTextWriter *writer,
756 const struct windows_version *version)
759 rc = xmlTextWriterStartElement(writer, "VERSION");
763 rc = xmlTextWriterWriteFormatElement(writer, "MAJOR", "%"PRIu64,
768 rc = xmlTextWriterWriteFormatElement(writer, "MINOR", "%"PRIu64,
773 rc = xmlTextWriterWriteFormatElement(writer, "BUILD", "%"PRIu64,
778 rc = xmlTextWriterWriteFormatElement(writer, "SPBUILD", "%"PRIu64,
783 rc = xmlTextWriterWriteFormatElement(writer, "SPLEVEL", "%"PRIu64,
788 rc = xmlTextWriterEndElement(writer); /* </VERSION> */
795 /* Writes the information contained in a `struct windows_info' to the XML
796 * document being written. This is the <WINDOWS> element. */
798 xml_write_windows_info(xmlTextWriter *writer,
799 const struct windows_info *windows_info)
802 rc = xmlTextWriterStartElement(writer, "WINDOWS");
806 rc = xmlTextWriterWriteFormatElement(writer, "ARCH", "%"PRIu64,
811 rc = xml_write_strings_from_specs(writer,
813 windows_info_xml_string_specs,
814 ARRAY_LEN(windows_info_xml_string_specs));
818 if (windows_info->num_languages) {
819 rc = xmlTextWriterStartElement(writer, "LANGUAGES");
823 for (size_t i = 0; i < windows_info->num_languages; i++) {
824 rc = xml_write_string(writer, "LANGUAGE",
825 windows_info->languages[i]);
830 rc = xml_write_string(writer, "DEFAULT",
831 windows_info->default_language);
835 rc = xmlTextWriterEndElement(writer); /* </LANGUAGES> */
840 if (windows_info->pkeyconfigversion) {
841 rc = xmlTextWriterStartElement(writer, "SERVICINGDATA");
845 rc = xml_write_string(writer, "PKEYCONFIGVERSION",
846 windows_info->pkeyconfigversion);
850 rc = xmlTextWriterEndElement(writer);
855 if (windows_info->windows_version_exists) {
856 rc = xml_write_windows_version(writer, &windows_info->windows_version);
861 rc = xml_write_string(writer, "SYSTEMROOT", windows_info->system_root);
865 rc = xmlTextWriterEndElement(writer); /* </WINDOWS> */
872 /* Writes a time element to the XML document being constructed in memory. */
874 xml_write_time(xmlTextWriter *writer, const char *element_name, u64 time)
877 rc = xmlTextWriterStartElement(writer, element_name);
881 rc = xmlTextWriterWriteFormatElement(writer, "HIGHPART",
882 "0x%08"PRIX32, (u32)(time >> 32));
886 rc = xmlTextWriterWriteFormatElement(writer, "LOWPART",
887 "0x%08"PRIX32, (u32)time);
891 rc = xmlTextWriterEndElement(writer); /* </@element_name> */
897 /* Writes an <IMAGE> element to the XML document. */
899 xml_write_image_info(xmlTextWriter *writer, const struct image_info *image_info)
902 rc = xmlTextWriterStartElement(writer, "IMAGE");
906 rc = xmlTextWriterWriteFormatAttribute(writer, "INDEX", "%d",
911 rc = xmlTextWriterWriteFormatElement(writer, "DIRCOUNT", "%"PRIu64,
912 image_info->dir_count);
916 rc = xmlTextWriterWriteFormatElement(writer, "FILECOUNT", "%"PRIu64,
917 image_info->file_count);
921 rc = xmlTextWriterWriteFormatElement(writer, "TOTALBYTES", "%"PRIu64,
922 image_info->total_bytes);
926 rc = xmlTextWriterWriteFormatElement(writer, "HARDLINKBYTES", "%"PRIu64,
927 image_info->hard_link_bytes);
931 rc = xml_write_time(writer, "CREATIONTIME", image_info->creation_time);
935 rc = xml_write_time(writer, "LASTMODIFICATIONTIME",
936 image_info->last_modification_time);
940 if (image_info->windows_info_exists) {
941 rc = xml_write_windows_info(writer, &image_info->windows_info);
946 rc = xml_write_strings_from_specs(writer, image_info,
947 image_info_xml_string_specs,
948 ARRAY_LEN(image_info_xml_string_specs));
952 if (image_info->wimboot) {
953 rc = xmlTextWriterWriteFormatElement(writer, "WIMBOOT", "%d", 1);
958 rc = xmlTextWriterEndElement(writer); /* </IMAGE> */
967 /* Makes space for another image in the XML information and return a pointer to
969 static struct image_info *
970 add_image_info_struct(struct wim_info *wim_info)
972 struct image_info *images;
974 images = CALLOC(wim_info->num_images + 1, sizeof(struct image_info));
977 memcpy(images, wim_info->images,
978 wim_info->num_images * sizeof(struct image_info));
979 FREE(wim_info->images);
980 wim_info->images = images;
981 wim_info->num_images++;
982 return &images[wim_info->num_images - 1];
986 clone_windows_info(const struct windows_info *old, struct windows_info *new)
990 new->arch = old->arch;
992 ret = dup_strings_from_specs(old, new, windows_info_xml_string_specs,
993 ARRAY_LEN(windows_info_xml_string_specs));
997 if (old->pkeyconfigversion) {
998 new->pkeyconfigversion = TSTRDUP(old->pkeyconfigversion);
999 if (new->pkeyconfigversion == NULL)
1000 return WIMLIB_ERR_NOMEM;
1003 if (old->languages) {
1004 new->languages = CALLOC(old->num_languages, sizeof(new->languages[0]));
1005 if (!new->languages)
1006 return WIMLIB_ERR_NOMEM;
1007 new->num_languages = old->num_languages;
1008 for (size_t i = 0; i < new->num_languages; i++) {
1009 if (!old->languages[i])
1011 new->languages[i] = TSTRDUP(old->languages[i]);
1012 if (!new->languages[i])
1013 return WIMLIB_ERR_NOMEM;
1016 if (old->default_language &&
1017 !(new->default_language = TSTRDUP(old->default_language)))
1018 return WIMLIB_ERR_NOMEM;
1019 if (old->system_root && !(new->system_root = TSTRDUP(old->system_root)))
1020 return WIMLIB_ERR_NOMEM;
1021 if (old->windows_version_exists) {
1022 new->windows_version_exists = true;
1023 memcpy(&new->windows_version, &old->windows_version,
1024 sizeof(old->windows_version));
1030 clone_image_info(const struct image_info *old, struct image_info *new)
1034 new->dir_count = old->dir_count;
1035 new->file_count = old->file_count;
1036 new->total_bytes = old->total_bytes;
1037 new->hard_link_bytes = old->hard_link_bytes;
1038 new->creation_time = old->creation_time;
1039 new->last_modification_time = old->last_modification_time;
1041 ret = dup_strings_from_specs(old, new,
1042 image_info_xml_string_specs,
1043 ARRAY_LEN(image_info_xml_string_specs));
1047 if (old->windows_info_exists) {
1048 new->windows_info_exists = true;
1049 ret = clone_windows_info(&old->windows_info,
1050 &new->windows_info);
1054 new->wimboot = old->wimboot;
1058 /* Copies the XML information for an image between WIM files.
1060 * @dest_image_name and @dest_image_description are ignored if they are NULL;
1061 * otherwise, they are used to override the image name and/or image description
1062 * from the XML data in the source WIM file.
1064 * On failure, WIMLIB_ERR_NOMEM is returned and no changes are made. Otherwise,
1065 * 0 is returned and the WIM information at *new_wim_info_p is modified.
1068 xml_export_image(const struct wim_info *old_wim_info,
1070 struct wim_info **new_wim_info_p,
1071 const tchar *dest_image_name,
1072 const tchar *dest_image_description)
1074 struct wim_info *new_wim_info;
1075 struct image_info *image_info;
1078 DEBUG("Copying XML data between WIM files for source image %d.", image);
1080 wimlib_assert(old_wim_info != NULL);
1081 wimlib_assert(image >= 1 && image <= old_wim_info->num_images);
1083 if (*new_wim_info_p) {
1084 new_wim_info = *new_wim_info_p;
1086 new_wim_info = CALLOC(1, sizeof(struct wim_info));
1091 image_info = add_image_info_struct(new_wim_info);
1095 ret = clone_image_info(&old_wim_info->images[image - 1], image_info);
1097 goto err_destroy_image_info;
1099 image_info->index = new_wim_info->num_images;
1101 if (dest_image_name) {
1102 FREE(image_info->name);
1103 image_info->name = TSTRDUP(dest_image_name);
1104 if (!image_info->name)
1105 goto err_destroy_image_info;
1107 if (dest_image_description) {
1108 FREE(image_info->description);
1109 image_info->description = TSTRDUP(dest_image_description);
1110 if (!image_info->description)
1111 goto err_destroy_image_info;
1113 *new_wim_info_p = new_wim_info;
1115 err_destroy_image_info:
1116 destroy_image_info(image_info);
1118 if (new_wim_info != *new_wim_info_p)
1119 free_wim_info(new_wim_info);
1120 return WIMLIB_ERR_NOMEM;
1123 /* Removes an image from the XML information. */
1125 xml_delete_image(struct wim_info **wim_info_p, int image)
1127 struct wim_info *wim_info;
1129 wim_info = *wim_info_p;
1130 wimlib_assert(image >= 1 && image <= wim_info->num_images);
1131 DEBUG("Deleting image %d from the XML data.", image);
1133 destroy_image_info(&wim_info->images[image - 1]);
1135 memmove(&wim_info->images[image - 1],
1136 &wim_info->images[image],
1137 (wim_info->num_images - image) * sizeof(struct image_info));
1139 if (--wim_info->num_images == 0) {
1140 free_wim_info(wim_info);
1143 for (int i = image - 1; i < wim_info->num_images; i++)
1144 wim_info->images[i].index--;
1149 xml_get_max_image_name_len(const WIMStruct *wim)
1152 for (u32 i = 0; i < wim->hdr.image_count; i++)
1153 max_len = max(max_len, tstrlen(wim->wim_info->images[i].name));
1158 xml_set_memory_allocator(void *(*malloc_func)(size_t),
1159 void (*free_func)(void *),
1160 void *(*realloc_func)(void *, size_t))
1162 xmlMemSetup(free_func, malloc_func, realloc_func, STRDUP);
1166 calculate_dentry_statistics(struct wim_dentry *dentry, void *arg)
1168 struct image_info *info = arg;
1169 const struct wim_inode *inode = dentry->d_inode;
1171 /* Update directory count and file count.
1173 * Each dentry counts as either a file or a directory, but not both.
1174 * The root directory is an exception: it is not counted at all.
1176 * Symbolic links and junction points (and presumably other reparse
1177 * points) count as regular files. This is despite the fact that
1178 * junction points have FILE_ATTRIBUTE_DIRECTORY set.
1181 if (!dentry_is_root(dentry)) {
1182 if (inode_is_directory(inode))
1189 * Update total bytes and hard link bytes.
1191 * We try to act the same as the MS implementation, even though there
1192 * are some inconsistencies/bugs in the way it operates.
1194 * If there are no alternate data streams in the image, the "total
1195 * bytes" is the sum of the size of the un-named data stream of each
1196 * inode times the link count of that inode. In other words, it would
1197 * be the total number of bytes of regular files you would have if you
1198 * extracted the full image without any hard-links. The "hard link
1199 * bytes" is equal to the "total bytes" minus the size of the un-named
1200 * data stream of each inode. In other words, the "hard link bytes"
1201 * counts the size of the un-named data stream for all the links to each
1202 * inode except the first one.
1204 * Reparse points and directories don't seem to be counted in either the
1205 * total bytes or the hard link bytes.
1207 * And now we get to the most confusing part, the alternate data
1208 * streams. They are not counted in the "total bytes". However, if the
1209 * link count of an inode with alternate data streams is 2 or greater,
1210 * the size of all the alternate data streams is included in the "hard
1211 * link bytes", and this size is multiplied by the link count (NOT one
1212 * less than the link count).
1214 if (!(inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
1215 FILE_ATTRIBUTE_REPARSE_POINT)))
1217 struct wim_lookup_table_entry *lte;
1219 lte = inode_unnamed_lte(inode, info->lookup_table);
1221 info->total_bytes += lte->size;
1222 if (!dentry_is_first_in_inode(dentry))
1223 info->hard_link_bytes += lte->size;
1226 if (inode->i_nlink >= 2 && dentry_is_first_in_inode(dentry)) {
1227 for (unsigned i = 0; i < inode->i_num_ads; i++) {
1228 if (inode->i_ads_entries[i].stream_name_nbytes) {
1229 lte = inode_stream_lte(inode, i + 1, info->lookup_table);
1231 info->hard_link_bytes += inode->i_nlink *
1242 * Calculate what to put in the <FILECOUNT>, <DIRCOUNT>, <TOTALBYTES>, and
1243 * <HARDLINKBYTES> elements of the specified WIM image.
1246 xml_update_image_info(WIMStruct *wim, int image)
1248 struct image_info *image_info;
1250 DEBUG("Updating the image info for image %d", image);
1252 image_info = &wim->wim_info->images[image - 1];
1254 image_info->file_count = 0;
1255 image_info->dir_count = 0;
1256 image_info->total_bytes = 0;
1257 image_info->hard_link_bytes = 0;
1258 image_info->lookup_table = wim->lookup_table;
1260 for_dentry_in_tree(wim->image_metadata[image - 1]->root_dentry,
1261 calculate_dentry_statistics,
1263 image_info->last_modification_time = now_as_wim_timestamp();
1266 /* Adds an image to the XML information. */
1268 xml_add_image(WIMStruct *wim, const tchar *name)
1270 struct wim_info *wim_info;
1271 struct image_info *image_info;
1273 wimlib_assert(name != NULL);
1275 /* If this is the first image, allocate the struct wim_info. Otherwise
1276 * use the existing struct wim_info. */
1277 if (wim->wim_info) {
1278 wim_info = wim->wim_info;
1280 wim_info = CALLOC(1, sizeof(struct wim_info));
1282 return WIMLIB_ERR_NOMEM;
1285 image_info = add_image_info_struct(wim_info);
1287 goto out_free_wim_info;
1289 if (!(image_info->name = TSTRDUP(name)))
1290 goto out_destroy_image_info;
1292 wim->wim_info = wim_info;
1293 image_info->index = wim_info->num_images;
1294 image_info->creation_time = now_as_wim_timestamp();
1295 xml_update_image_info(wim, image_info->index);
1298 out_destroy_image_info:
1299 destroy_image_info(image_info);
1300 wim_info->num_images--;
1302 if (wim_info != wim->wim_info)
1304 return WIMLIB_ERR_NOMEM;
1307 /* Prints information about the specified image from struct wim_info structure.
1310 print_image_info(const struct wim_info *wim_info, int image)
1312 const struct image_info *image_info;
1316 wimlib_assert(image >= 1 && image <= wim_info->num_images);
1318 image_info = &wim_info->images[image - 1];
1320 tprintf(T("Index: %d\n"), image_info->index);
1321 tprintf(T("Name: %"TS"\n"), image_info->name);
1323 /* Always print the Description: part even if there is no
1325 if (image_info->description)
1326 desc = image_info->description;
1329 tprintf(T("Description: %"TS"\n"), desc);
1331 if (image_info->display_name) {
1332 tprintf(T("Display Name: %"TS"\n"),
1333 image_info->display_name);
1336 if (image_info->display_description) {
1337 tprintf(T("Display Description: %"TS"\n"),
1338 image_info->display_description);
1341 tprintf(T("Directory Count: %"PRIu64"\n"), image_info->dir_count);
1342 tprintf(T("File Count: %"PRIu64"\n"), image_info->file_count);
1343 tprintf(T("Total Bytes: %"PRIu64"\n"), image_info->total_bytes);
1344 tprintf(T("Hard Link Bytes: %"PRIu64"\n"), image_info->hard_link_bytes);
1346 wim_timestamp_to_str(image_info->creation_time, buf, sizeof(buf));
1347 tprintf(T("Creation Time: %"TS"\n"), buf);
1349 wim_timestamp_to_str(image_info->last_modification_time, buf, sizeof(buf));
1350 tprintf(T("Last Modification Time: %"TS"\n"), buf);
1351 if (image_info->windows_info_exists)
1352 print_windows_info(&image_info->windows_info);
1353 if (image_info->flags)
1354 tprintf(T("Flags: %"TS"\n"), image_info->flags);
1355 tprintf(T("WIMBoot compatible: %"TS"\n"),
1356 image_info->wimboot ? T("yes") : T("no"));
1361 libxml_global_init(void)
1364 xmlInitCharEncodingHandlers();
1368 libxml_global_cleanup(void)
1371 xmlCleanupCharEncodingHandlers();
1374 /* Reads the XML data from a WIM file. */
1376 read_wim_xml_data(WIMStruct *wim)
1385 ret = wimlib_get_xml_data(wim, &buf, &bufsize);
1390 doc = xmlReadMemory((const char *)xml_data, bufsize,
1391 NULL, "UTF-16LE", 0);
1393 ERROR("Failed to parse XML data");
1394 ret = WIMLIB_ERR_XML;
1395 goto out_free_xml_data;
1398 root = xmlDocGetRootElement(doc);
1399 if (!root || !node_is_element(root) || !node_name_is(root, "WIM")) {
1400 ERROR("WIM XML data is invalid");
1401 ret = WIMLIB_ERR_XML;
1405 ret = xml_read_wim_info(root, &wim->wim_info);
1414 /* Prepares an in-memory buffer containing the UTF-16LE XML data for a WIM file.
1416 * total_bytes is the number to write in <TOTALBYTES>, or
1417 * WIM_TOTALBYTES_USE_EXISTING to use the existing value in memory, or
1418 * WIM_TOTALBYTES_OMIT to omit <TOTALBYTES> entirely.
1421 prepare_wim_xml_data(WIMStruct *wim, int image, u64 total_bytes,
1422 u8 **xml_data_ret, size_t *xml_len_ret)
1424 xmlCharEncodingHandler *encoding_handler;
1426 xmlOutputBuffer *outbuf;
1427 xmlTextWriter *writer;
1430 const xmlChar *content;
1435 /* Open an xmlTextWriter that writes to an in-memory buffer using
1436 * UTF-16LE encoding. */
1438 encoding_handler = xmlGetCharEncodingHandler(XML_CHAR_ENCODING_UTF16LE);
1439 if (!encoding_handler) {
1440 ERROR("Failed to get XML character encoding handler for UTF-16LE");
1441 ret = WIMLIB_ERR_LIBXML_UTF16_HANDLER_NOT_AVAILABLE;
1445 buf = xmlBufferCreate();
1447 ERROR("Failed to create xmlBuffer");
1448 ret = WIMLIB_ERR_NOMEM;
1452 outbuf = xmlOutputBufferCreateBuffer(buf, encoding_handler);
1454 ERROR("Failed to allocate xmlOutputBuffer");
1455 ret = WIMLIB_ERR_NOMEM;
1456 goto out_buffer_free;
1459 writer = xmlNewTextWriter(outbuf);
1461 ERROR("Failed to allocate xmlTextWriter");
1462 ret = WIMLIB_ERR_NOMEM;
1463 goto out_output_buffer_close;
1466 /* Write the XML document. */
1468 ret = xmlTextWriterStartElement(writer, "WIM");
1470 goto out_write_error;
1472 /* The contents of the <TOTALBYTES> element in the XML data, under the
1473 * <WIM> element (not the <IMAGE> element), is for non-split WIMs the
1474 * size of the WIM file excluding the XML data and integrity table.
1475 * For split WIMs, <TOTALBYTES> takes into account the entire WIM, not
1476 * just the current part. */
1477 if (total_bytes != WIM_TOTALBYTES_OMIT) {
1478 if (total_bytes == WIM_TOTALBYTES_USE_EXISTING) {
1480 total_bytes = wim->wim_info->total_bytes;
1484 ret = xmlTextWriterWriteFormatElement(writer, "TOTALBYTES",
1485 "%"PRIu64, total_bytes);
1487 goto out_write_error;
1490 if (image == WIMLIB_ALL_IMAGES) {
1492 last = wim->hdr.image_count;
1498 for (int i = first; i <= last; i++) {
1499 ret = xml_write_image_info(writer, &wim->wim_info->images[i - 1]);
1502 goto out_write_error;
1503 goto out_free_text_writer;
1507 ret = xmlTextWriterEndElement(writer);
1509 goto out_write_error;
1511 ret = xmlTextWriterEndDocument(writer);
1513 goto out_write_error;
1515 ret = xmlTextWriterFlush(writer);
1517 goto out_write_error;
1519 /* Retrieve the buffer into which the document was written. */
1521 content = xmlBufferContent(buf);
1522 len = xmlBufferLength(buf);
1524 /* Copy the data into a new buffer, and prefix it with the UTF-16LE BOM
1525 * (byte order mark), which is required by MS's software to understand
1529 xml_data = MALLOC(xml_len);
1531 ret = WIMLIB_ERR_NOMEM;
1532 goto out_free_text_writer;
1536 memcpy(&xml_data[2], content, len);
1538 /* Clean up libxml objects and return success. */
1539 *xml_data_ret = xml_data;
1540 *xml_len_ret = xml_len;
1542 out_free_text_writer:
1543 /* xmlFreeTextWriter will free the attached xmlOutputBuffer. */
1544 xmlFreeTextWriter(writer);
1545 goto out_buffer_free;
1546 out_output_buffer_close:
1547 xmlOutputBufferClose(outbuf);
1551 DEBUG("ret=%d", ret);
1555 ERROR("Error writing XML data");
1556 ret = WIMLIB_ERR_WRITE;
1557 goto out_free_text_writer;
1560 /* Writes the XML data to a WIM file. */
1562 write_wim_xml_data(WIMStruct *wim, int image, u64 total_bytes,
1563 struct wim_reshdr *out_reshdr,
1564 int write_resource_flags)
1570 DEBUG("Writing WIM XML data (image=%d, offset=%"PRIu64")",
1571 image, wim->out_fd.offset);
1573 ret = prepare_wim_xml_data(wim, image, total_bytes,
1574 &xml_data, &xml_len);
1578 /* Write the XML data uncompressed. Although wimlib can handle
1579 * compressed XML data, MS software cannot. */
1580 ret = write_wim_resource_from_buffer(xml_data,
1582 WIM_RESHDR_FLAG_METADATA,
1584 WIMLIB_COMPRESSION_TYPE_NONE,
1588 write_resource_flags);
1590 DEBUG("ret=%d", ret);
1594 /* API function documented in wimlib.h */
1595 WIMLIBAPI const tchar *
1596 wimlib_get_image_name(const WIMStruct *wim, int image)
1598 if (image < 1 || image > wim->hdr.image_count)
1600 return wim->wim_info->images[image - 1].name;
1603 /* API function documented in wimlib.h */
1604 WIMLIBAPI const tchar *
1605 wimlib_get_image_description(const WIMStruct *wim, int image)
1607 if (image < 1 || image > wim->hdr.image_count)
1609 return wim->wim_info->images[image - 1].description;
1612 /* API function documented in wimlib.h */
1614 wimlib_image_name_in_use(const WIMStruct *wim, const tchar *name)
1616 if (!name || !*name)
1618 for (int i = 1; i <= wim->hdr.image_count; i++)
1619 if (!tstrcmp(wim->wim_info->images[i - 1].name, name))
1625 /* API function documented in wimlib.h */
1627 wimlib_get_xml_data(WIMStruct *wim, void **buf_ret, size_t *bufsize_ret)
1629 const struct wim_reshdr *xml_reshdr;
1631 if (wim->filename == NULL && filedes_is_seekable(&wim->in_fd))
1632 return WIMLIB_ERR_INVALID_PARAM;
1634 if (buf_ret == NULL || bufsize_ret == NULL)
1635 return WIMLIB_ERR_INVALID_PARAM;
1637 xml_reshdr = &wim->hdr.xml_data_reshdr;
1639 DEBUG("Reading XML data.");
1640 *bufsize_ret = xml_reshdr->uncompressed_size;
1641 return wim_reshdr_to_data(xml_reshdr, wim, buf_ret);
1645 wimlib_extract_xml_data(WIMStruct *wim, FILE *fp)
1651 ret = wimlib_get_xml_data(wim, &buf, &bufsize);
1655 if (fwrite(buf, 1, bufsize, fp) != bufsize) {
1656 ERROR_WITH_ERRNO("Failed to extract XML data");
1657 ret = WIMLIB_ERR_WRITE;
1663 /* API function documented in wimlib.h */
1665 wimlib_set_image_name(WIMStruct *wim, int image, const tchar *name)
1673 if (image < 1 || image > wim->hdr.image_count)
1674 return WIMLIB_ERR_INVALID_IMAGE;
1677 for (i = 1; i <= wim->hdr.image_count; i++) {
1680 if (!tstrcmp(wim->wim_info->images[i - 1].name, name))
1681 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
1687 return WIMLIB_ERR_NOMEM;
1689 FREE(wim->wim_info->images[image - 1].name);
1690 wim->wim_info->images[image - 1].name = p;
1695 do_set_image_info_str(WIMStruct *wim, int image, const tchar *tstr,
1699 tchar **dest_tstr_p;
1701 if (image < 1 || image > wim->hdr.image_count) {
1702 ERROR("%d is not a valid image", image);
1703 return WIMLIB_ERR_INVALID_IMAGE;
1706 tstr_copy = TSTRDUP(tstr);
1708 return WIMLIB_ERR_NOMEM;
1712 dest_tstr_p = (tchar**)((void*)&wim->wim_info->images[image - 1] + offset);
1715 *dest_tstr_p = tstr_copy;
1719 /* API function documented in wimlib.h */
1721 wimlib_set_image_descripton(WIMStruct *wim, int image,
1722 const tchar *description)
1724 return do_set_image_info_str(wim, image, description,
1725 offsetof(struct image_info, description));
1728 /* API function documented in wimlib.h */
1730 wimlib_set_image_flags(WIMStruct *wim, int image, const tchar *flags)
1732 return do_set_image_info_str(wim, image, flags,
1733 offsetof(struct image_info, flags));