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/blob_table.h"
36 #include "wimlib/dentry.h"
37 #include "wimlib/encoding.h"
38 #include "wimlib/error.h"
39 #include "wimlib/file_io.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 */
94 /* A struct wim_info structure corresponds to the entire XML data for a WIM file. */
98 /* Array of `struct image_info's, one for each image in the WIM that is
99 * mentioned in the XML data. */
100 struct image_info *images;
103 struct xml_string_spec {
108 #define ELEM(STRING_NAME, MEMBER_NAME) \
109 {STRING_NAME, offsetof(struct image_info, MEMBER_NAME)}
110 static const struct xml_string_spec
111 image_info_xml_string_specs[] = {
113 ELEM("DESCRIPTION", description),
114 ELEM("DISPLAYNAME", display_name),
115 ELEM("DISPLAYDESCRIPTION", display_description),
116 ELEM("FLAGS", flags),
120 #define ELEM(STRING_NAME, MEMBER_NAME) \
121 {STRING_NAME, offsetof(struct windows_info, MEMBER_NAME)}
122 static const struct xml_string_spec
123 windows_info_xml_string_specs[] = {
124 ELEM("PRODUCTNAME", product_name),
125 ELEM("EDITIONID", edition_id),
126 ELEM("INSTALLATIONTYPE", installation_type),
128 ELEM("PRODUCTTYPE", product_type),
129 ELEM("PRODUCTSUITE", product_suite),
134 wim_info_get_total_bytes(const struct wim_info *info)
137 return info->total_bytes;
143 wim_info_get_image_hard_link_bytes(const struct wim_info *info, int image)
146 return info->images[image - 1].hard_link_bytes;
152 wim_info_get_image_total_bytes(const struct wim_info *info, int image)
155 return info->images[image - 1].total_bytes;
161 wim_info_get_num_images(const struct wim_info *info)
164 return info->num_images;
170 wim_info_set_wimboot(struct wim_info *info, int image, bool value)
172 info->images[image - 1].wimboot = value;
176 wim_info_get_wimboot(const struct wim_info *info, int image)
178 return info->images[image - 1].wimboot;
181 /* Architecture constants are from w64 mingw winnt.h */
182 #define PROCESSOR_ARCHITECTURE_INTEL 0
183 #define PROCESSOR_ARCHITECTURE_MIPS 1
184 #define PROCESSOR_ARCHITECTURE_ALPHA 2
185 #define PROCESSOR_ARCHITECTURE_PPC 3
186 #define PROCESSOR_ARCHITECTURE_SHX 4
187 #define PROCESSOR_ARCHITECTURE_ARM 5
188 #define PROCESSOR_ARCHITECTURE_IA64 6
189 #define PROCESSOR_ARCHITECTURE_ALPHA64 7
190 #define PROCESSOR_ARCHITECTURE_MSIL 8
191 #define PROCESSOR_ARCHITECTURE_AMD64 9
192 #define PROCESSOR_ARCHITECTURE_IA32_ON_WIN64 10
194 /* Returns a statically allocated string that is a string representation of the
195 * architecture number. */
200 case PROCESSOR_ARCHITECTURE_INTEL:
202 case PROCESSOR_ARCHITECTURE_MIPS:
204 case PROCESSOR_ARCHITECTURE_ARM:
206 case PROCESSOR_ARCHITECTURE_IA64:
208 case PROCESSOR_ARCHITECTURE_AMD64:
216 /* Iterate through the children of an xmlNode. */
217 #define for_node_child(parent, child) \
218 for (child = parent->children; child != NULL; child = child->next)
220 /* Utility functions for xmlNodes */
222 node_is_element(xmlNode *node)
224 return node->type == XML_ELEMENT_NODE;
228 node_is_text(xmlNode *node)
230 return node->type == XML_TEXT_NODE;
234 node_name_is(xmlNode *node, const char *name)
236 /* For now, both upper case and lower case element names are accepted. */
237 return strcasecmp((const char *)node->name, name) == 0;
241 node_get_number(const xmlNode *u64_node, int base)
244 for_node_child(u64_node, child)
245 if (node_is_text(child))
246 return strtoull(child->content, NULL, base);
250 /* Finds the text node that is a child of an element node and returns its
251 * content converted to a 64-bit unsigned integer. Returns 0 if no text node is
254 node_get_u64(const xmlNode *u64_node)
256 return node_get_number(u64_node, 10);
259 /* Like node_get_u64(), but expects a number in base 16. */
261 node_get_hex_u64(const xmlNode *u64_node)
263 return node_get_number(u64_node, 16);
267 node_get_string(const xmlNode *string_node, tchar **tstr_ret)
274 for_node_child(string_node, child)
275 if (node_is_text(child) && child->content)
276 return utf8_to_tstr_simple(child->content, tstr_ret);
280 /* Returns the timestamp from a time node. It has child elements <HIGHPART> and
281 * <LOWPART> that are then used to construct a 64-bit timestamp. */
283 node_get_timestamp(const xmlNode *time_node)
288 for_node_child(time_node, child) {
289 if (!node_is_element(child))
291 if (node_name_is(child, "HIGHPART"))
292 high_part = node_get_hex_u64(child);
293 else if (node_name_is(child, "LOWPART"))
294 low_part = node_get_hex_u64(child);
296 return (u64)low_part | ((u64)high_part << 32);
299 /* Used to sort an array of struct image_infos by their image indices. */
301 sort_by_index(const void *p1, const void *p2)
303 int index_1 = ((const struct image_info*)p1)->index;
304 int index_2 = ((const struct image_info*)p2)->index;
305 if (index_1 < index_2)
307 else if (index_1 > index_2)
314 /* Frees memory allocated inside a struct windows_info structure. */
316 destroy_windows_info(struct windows_info *windows_info)
318 FREE(windows_info->product_name);
319 FREE(windows_info->edition_id);
320 FREE(windows_info->installation_type);
321 FREE(windows_info->hal);
322 FREE(windows_info->product_type);
323 FREE(windows_info->product_suite);
324 FREE(windows_info->pkeyconfigversion);
325 for (size_t i = 0; i < windows_info->num_languages; i++)
326 FREE(windows_info->languages[i]);
327 FREE(windows_info->languages);
328 FREE(windows_info->default_language);
329 FREE(windows_info->system_root);
332 /* Frees memory allocated inside a struct image_info structure. */
334 destroy_image_info(struct image_info *image_info)
336 FREE(image_info->name);
337 FREE(image_info->description);
338 FREE(image_info->flags);
339 FREE(image_info->display_name);
340 FREE(image_info->display_description);
341 destroy_windows_info(&image_info->windows_info);
342 memset(image_info, 0, sizeof(struct image_info));
346 free_wim_info(struct wim_info *info)
350 for (int i = 0; i < info->num_images; i++)
351 destroy_image_info(&info->images[i]);
358 /* Reads the information from a <VERSION> element inside the <WINDOWS> element.
361 xml_read_windows_version(const xmlNode *version_node,
362 struct windows_version* windows_version)
365 for_node_child(version_node, child) {
366 if (!node_is_element(child))
368 if (node_name_is(child, "MAJOR"))
369 windows_version->major = node_get_u64(child);
370 else if (node_name_is(child, "MINOR"))
371 windows_version->minor = node_get_u64(child);
372 else if (node_name_is(child, "BUILD"))
373 windows_version->build = node_get_u64(child);
374 else if (node_name_is(child, "SPBUILD"))
375 windows_version->sp_build = node_get_u64(child);
376 else if (node_name_is(child, "SPLEVEL"))
377 windows_version->sp_level = node_get_u64(child);
381 /* Reads the information from a <LANGUAGE> element inside a <WINDOWS> element.
384 xml_read_languages(const xmlNode *languages_node,
385 tchar ***languages_ret,
386 size_t *num_languages_ret,
387 tchar **default_language_ret)
390 size_t num_languages = 0;
394 for_node_child(languages_node, child)
395 if (node_is_element(child) && node_name_is(child, "LANGUAGE"))
398 languages = CALLOC(num_languages, sizeof(languages[0]));
400 return WIMLIB_ERR_NOMEM;
402 *languages_ret = languages;
403 *num_languages_ret = num_languages;
406 for_node_child(languages_node, child) {
407 if (!node_is_element(child))
409 if (node_name_is(child, "LANGUAGE"))
410 ret = node_get_string(child, languages++);
411 else if (node_name_is(child, "DEFAULT"))
412 ret = node_get_string(child, default_language_ret);
419 /* Reads the information from a <WINDOWS> element inside an <IMAGE> element. */
421 xml_read_windows_info(const xmlNode *windows_node,
422 struct windows_info *windows_info)
427 for_node_child(windows_node, child) {
428 if (!node_is_element(child))
430 if (node_name_is(child, "ARCH")) {
431 windows_info->arch = node_get_u64(child);
432 } else if (node_name_is(child, "PRODUCTNAME")) {
433 ret = node_get_string(child,
434 &windows_info->product_name);
435 } else if (node_name_is(child, "EDITIONID")) {
436 ret = node_get_string(child,
437 &windows_info->edition_id);
438 } else if (node_name_is(child, "INSTALLATIONTYPE")) {
439 ret = node_get_string(child,
440 &windows_info->installation_type);
441 } else if (node_name_is(child, "PRODUCTTYPE")) {
442 ret = node_get_string(child,
443 &windows_info->product_type);
444 } else if (node_name_is(child, "PRODUCTSUITE")) {
445 ret = node_get_string(child,
446 &windows_info->product_suite);
447 } else if (node_name_is(child, "LANGUAGES")) {
448 ret = xml_read_languages(child,
449 &windows_info->languages,
450 &windows_info->num_languages,
451 &windows_info->default_language);
452 } else if (node_name_is(child, "VERSION")) {
453 xml_read_windows_version(child,
454 &windows_info->windows_version);
455 windows_info->windows_version_exists = true;
456 } else if (node_name_is(child, "SYSTEMROOT")) {
457 ret = node_get_string(child, &windows_info->system_root);
458 } else if (node_name_is(child, "HAL")) {
459 ret = node_get_string(child, &windows_info->hal);
460 } else if (node_name_is(child, "SERVICINGDATA")) {
463 for_node_child(child, grandchild) {
464 if (node_is_element(grandchild) &&
465 node_name_is(grandchild, "PKEYCONFIGVERSION"))
467 ret = node_get_string(grandchild,
468 &windows_info->pkeyconfigversion);
479 /* Reads the information from an <IMAGE> element. */
481 xml_read_image_info(xmlNode *image_node, struct image_info *image_info)
487 index_prop = xmlGetProp(image_node, "INDEX");
489 image_info->index = atoi(index_prop);
492 image_info->index = 1;
496 for_node_child(image_node, child) {
497 if (!node_is_element(child))
499 if (node_name_is(child, "DIRCOUNT"))
500 image_info->dir_count = node_get_u64(child);
501 else if (node_name_is(child, "FILECOUNT"))
502 image_info->file_count = node_get_u64(child);
503 else if (node_name_is(child, "TOTALBYTES"))
504 image_info->total_bytes = node_get_u64(child);
505 else if (node_name_is(child, "HARDLINKBYTES"))
506 image_info->hard_link_bytes = node_get_u64(child);
507 else if (node_name_is(child, "CREATIONTIME"))
508 image_info->creation_time = node_get_timestamp(child);
509 else if (node_name_is(child, "LASTMODIFICATIONTIME"))
510 image_info->last_modification_time = node_get_timestamp(child);
511 else if (node_name_is(child, "WINDOWS")) {
512 ret = xml_read_windows_info(child,
513 &image_info->windows_info);
514 image_info->windows_info_exists = true;
515 } else if (node_name_is(child, "NAME")) {
516 ret = node_get_string(child, &image_info->name);
517 } else if (node_name_is(child, "DESCRIPTION")) {
518 ret = node_get_string(child, &image_info->description);
519 } else if (node_name_is(child, "FLAGS")) {
520 ret = node_get_string(child, &image_info->flags);
521 } else if (node_name_is(child, "DISPLAYNAME")) {
522 ret = node_get_string(child, &image_info->display_name);
523 } else if (node_name_is(child, "DISPLAYDESCRIPTION")) {
524 ret = node_get_string(child, &image_info->display_description);
525 } else if (node_name_is(child, "WIMBOOT")) {
526 if (node_get_u64(child) == 1) {
527 image_info->wimboot = true;
533 if (!image_info->name) {
535 empty_name = MALLOC(sizeof(tchar));
537 return WIMLIB_ERR_NOMEM;
538 *empty_name = T('\0');
539 image_info->name = empty_name;
544 /* Reads the information from a <WIM> element, which should be the root element
545 * of the XML tree. */
547 xml_read_wim_info(const xmlNode *wim_node, struct wim_info **wim_info_ret)
549 struct wim_info *wim_info;
555 wim_info = CALLOC(1, sizeof(struct wim_info));
557 return WIMLIB_ERR_NOMEM;
559 /* Count how many images there are. */
561 for_node_child(wim_node, child) {
562 if (node_is_element(child) && node_name_is(child, "IMAGE")) {
563 if (unlikely(num_images == MAX_IMAGES)) {
564 ret = WIMLIB_ERR_IMAGE_COUNT;
571 if (num_images > 0) {
572 /* Allocate the array of struct image_infos and fill them in. */
573 wim_info->images = CALLOC(num_images, sizeof(wim_info->images[0]));
574 if (!wim_info->images) {
575 ret = WIMLIB_ERR_NOMEM;
578 wim_info->num_images = num_images;
580 for_node_child(wim_node, child) {
581 if (!node_is_element(child))
583 if (node_name_is(child, "IMAGE")) {
584 ret = xml_read_image_info(child,
585 &wim_info->images[i]);
589 } else if (node_name_is(child, "TOTALBYTES")) {
590 wim_info->total_bytes = node_get_u64(child);
591 } else if (node_name_is(child, "ESD")) {
593 for_node_child(child, esdchild) {
594 if (node_is_element(esdchild) &&
595 node_name_is(esdchild, "ENCRYPTED"))
597 ret = WIMLIB_ERR_WIM_IS_ENCRYPTED;
604 /* Sort the array of image info by image index. */
605 qsort(wim_info->images, num_images,
606 sizeof(struct image_info), sort_by_index);
608 /* Make sure the image indices make sense */
609 for (i = 0; i < num_images; i++) {
610 if (wim_info->images[i].index != i + 1) {
611 ERROR("WIM images are not indexed [1...%d] "
612 "in XML data as expected",
614 ret = WIMLIB_ERR_IMAGE_COUNT;
620 *wim_info_ret = wim_info;
623 free_wim_info(wim_info);
627 /* Prints the information contained in a `struct windows_info'. */
629 print_windows_info(const struct windows_info *windows_info)
631 const struct windows_version *windows_version;
633 tprintf(T("Architecture: %"TS"\n"),
634 get_arch(windows_info->arch));
636 if (windows_info->product_name) {
637 tprintf(T("Product Name: %"TS"\n"),
638 windows_info->product_name);
641 if (windows_info->edition_id) {
642 tprintf(T("Edition ID: %"TS"\n"),
643 windows_info->edition_id);
646 if (windows_info->installation_type) {
647 tprintf(T("Installation Type: %"TS"\n"),
648 windows_info->installation_type);
651 if (windows_info->hal) {
652 tprintf(T("HAL: %"TS"\n"),
656 if (windows_info->product_type) {
657 tprintf(T("Product Type: %"TS"\n"),
658 windows_info->product_type);
661 if (windows_info->product_suite) {
662 tprintf(T("Product Suite: %"TS"\n"),
663 windows_info->product_suite);
666 tprintf(T("Languages: "));
667 for (size_t i = 0; i < windows_info->num_languages; i++) {
669 tfputs(windows_info->languages[i], stdout);
673 if (windows_info->default_language) {
674 tprintf(T("Default Language: %"TS"\n"),
675 windows_info->default_language);
677 if (windows_info->system_root) {
678 tprintf(T("System Root: %"TS"\n"),
679 windows_info->system_root);
682 if (windows_info->windows_version_exists) {
683 windows_version = &windows_info->windows_version;
684 tprintf(T("Major Version: %"PRIu64"\n"),
685 windows_version->major);
686 tprintf(T("Minor Version: %"PRIu64"\n"),
687 windows_version->minor);
688 tprintf(T("Build: %"PRIu64"\n"),
689 windows_version->build);
690 tprintf(T("Service Pack Build: %"PRIu64"\n"),
691 windows_version->sp_build);
692 tprintf(T("Service Pack Level: %"PRIu64"\n"),
693 windows_version->sp_level);
698 xml_write_string(xmlTextWriter *writer, const char *name,
703 int rc = tstr_to_utf8_simple(tstr, &utf8_str);
706 rc = xmlTextWriterWriteElement(writer, name, utf8_str);
715 xml_write_strings_from_specs(xmlTextWriter *writer,
716 const void *struct_with_strings,
717 const struct xml_string_spec specs[],
720 for (size_t i = 0; i < num_specs; i++) {
721 int rc = xml_write_string(writer, specs[i].name,
722 *(const tchar * const *)
723 (struct_with_strings + specs[i].offset));
731 dup_strings_from_specs(const void *old_struct_with_strings,
732 void *new_struct_with_strings,
733 const struct xml_string_spec specs[],
736 for (size_t i = 0; i < num_specs; i++) {
737 const tchar *old_str = *(const tchar * const *)
738 ((const void*)old_struct_with_strings + specs[i].offset);
739 tchar **new_str_p = (tchar **)((void*)new_struct_with_strings + specs[i].offset);
741 *new_str_p = TSTRDUP(old_str);
743 return WIMLIB_ERR_NOMEM;
749 /* Writes the information contained in a `struct windows_version' to the XML
750 * document being written. This is the <VERSION> element inside the <WINDOWS>
753 xml_write_windows_version(xmlTextWriter *writer,
754 const struct windows_version *version)
757 rc = xmlTextWriterStartElement(writer, "VERSION");
761 rc = xmlTextWriterWriteFormatElement(writer, "MAJOR", "%"PRIu64,
766 rc = xmlTextWriterWriteFormatElement(writer, "MINOR", "%"PRIu64,
771 rc = xmlTextWriterWriteFormatElement(writer, "BUILD", "%"PRIu64,
776 rc = xmlTextWriterWriteFormatElement(writer, "SPBUILD", "%"PRIu64,
781 rc = xmlTextWriterWriteFormatElement(writer, "SPLEVEL", "%"PRIu64,
786 rc = xmlTextWriterEndElement(writer); /* </VERSION> */
793 /* Writes the information contained in a `struct windows_info' to the XML
794 * document being written. This is the <WINDOWS> element. */
796 xml_write_windows_info(xmlTextWriter *writer,
797 const struct windows_info *windows_info)
800 rc = xmlTextWriterStartElement(writer, "WINDOWS");
804 rc = xmlTextWriterWriteFormatElement(writer, "ARCH", "%"PRIu64,
809 rc = xml_write_strings_from_specs(writer,
811 windows_info_xml_string_specs,
812 ARRAY_LEN(windows_info_xml_string_specs));
816 if (windows_info->num_languages) {
817 rc = xmlTextWriterStartElement(writer, "LANGUAGES");
821 for (size_t i = 0; i < windows_info->num_languages; i++) {
822 rc = xml_write_string(writer, "LANGUAGE",
823 windows_info->languages[i]);
828 rc = xml_write_string(writer, "DEFAULT",
829 windows_info->default_language);
833 rc = xmlTextWriterEndElement(writer); /* </LANGUAGES> */
838 if (windows_info->pkeyconfigversion) {
839 rc = xmlTextWriterStartElement(writer, "SERVICINGDATA");
843 rc = xml_write_string(writer, "PKEYCONFIGVERSION",
844 windows_info->pkeyconfigversion);
848 rc = xmlTextWriterEndElement(writer);
853 if (windows_info->windows_version_exists) {
854 rc = xml_write_windows_version(writer, &windows_info->windows_version);
859 rc = xml_write_string(writer, "SYSTEMROOT", windows_info->system_root);
863 rc = xmlTextWriterEndElement(writer); /* </WINDOWS> */
870 /* Writes a time element to the XML document being constructed in memory. */
872 xml_write_time(xmlTextWriter *writer, const char *element_name, u64 time)
875 rc = xmlTextWriterStartElement(writer, element_name);
879 rc = xmlTextWriterWriteFormatElement(writer, "HIGHPART",
880 "0x%08"PRIX32, (u32)(time >> 32));
884 rc = xmlTextWriterWriteFormatElement(writer, "LOWPART",
885 "0x%08"PRIX32, (u32)time);
889 rc = xmlTextWriterEndElement(writer); /* </@element_name> */
895 /* Writes an <IMAGE> element to the XML document. */
897 xml_write_image_info(xmlTextWriter *writer, const struct image_info *image_info,
902 rc = xmlTextWriterStartElement(writer, "IMAGE");
906 rc = xmlTextWriterWriteFormatAttribute(writer, "INDEX", "%d", index);
910 rc = xmlTextWriterWriteFormatElement(writer, "DIRCOUNT", "%"PRIu64,
911 image_info->dir_count);
915 rc = xmlTextWriterWriteFormatElement(writer, "FILECOUNT", "%"PRIu64,
916 image_info->file_count);
920 rc = xmlTextWriterWriteFormatElement(writer, "TOTALBYTES", "%"PRIu64,
921 image_info->total_bytes);
925 rc = xmlTextWriterWriteFormatElement(writer, "HARDLINKBYTES", "%"PRIu64,
926 image_info->hard_link_bytes);
930 rc = xml_write_time(writer, "CREATIONTIME", image_info->creation_time);
934 rc = xml_write_time(writer, "LASTMODIFICATIONTIME",
935 image_info->last_modification_time);
939 if (image_info->windows_info_exists) {
940 rc = xml_write_windows_info(writer, &image_info->windows_info);
945 rc = xml_write_strings_from_specs(writer, image_info,
946 image_info_xml_string_specs,
947 ARRAY_LEN(image_info_xml_string_specs));
951 if (image_info->wimboot) {
952 rc = xmlTextWriterWriteFormatElement(writer, "WIMBOOT", "%d", 1);
957 rc = xmlTextWriterEndElement(writer); /* </IMAGE> */
966 /* Makes space for another image in the XML information and return a pointer to
968 static struct image_info *
969 add_image_info_struct(struct wim_info *wim_info)
971 struct image_info *images;
973 images = CALLOC(wim_info->num_images + 1, sizeof(struct image_info));
976 memcpy(images, wim_info->images,
977 wim_info->num_images * sizeof(struct image_info));
978 FREE(wim_info->images);
979 wim_info->images = images;
980 wim_info->num_images++;
981 return &images[wim_info->num_images - 1];
985 clone_windows_info(const struct windows_info *old, struct windows_info *new)
989 new->arch = old->arch;
991 ret = dup_strings_from_specs(old, new, windows_info_xml_string_specs,
992 ARRAY_LEN(windows_info_xml_string_specs));
996 if (old->pkeyconfigversion) {
997 new->pkeyconfigversion = TSTRDUP(old->pkeyconfigversion);
998 if (new->pkeyconfigversion == NULL)
999 return WIMLIB_ERR_NOMEM;
1002 if (old->languages) {
1003 new->languages = CALLOC(old->num_languages, sizeof(new->languages[0]));
1004 if (!new->languages)
1005 return WIMLIB_ERR_NOMEM;
1006 new->num_languages = old->num_languages;
1007 for (size_t i = 0; i < new->num_languages; i++) {
1008 if (!old->languages[i])
1010 new->languages[i] = TSTRDUP(old->languages[i]);
1011 if (!new->languages[i])
1012 return WIMLIB_ERR_NOMEM;
1015 if (old->default_language &&
1016 !(new->default_language = TSTRDUP(old->default_language)))
1017 return WIMLIB_ERR_NOMEM;
1018 if (old->system_root && !(new->system_root = TSTRDUP(old->system_root)))
1019 return WIMLIB_ERR_NOMEM;
1020 if (old->windows_version_exists) {
1021 new->windows_version_exists = true;
1022 memcpy(&new->windows_version, &old->windows_version,
1023 sizeof(old->windows_version));
1029 clone_image_info(const struct image_info *old, struct image_info *new)
1033 new->dir_count = old->dir_count;
1034 new->file_count = old->file_count;
1035 new->total_bytes = old->total_bytes;
1036 new->hard_link_bytes = old->hard_link_bytes;
1037 new->creation_time = old->creation_time;
1038 new->last_modification_time = old->last_modification_time;
1040 ret = dup_strings_from_specs(old, new,
1041 image_info_xml_string_specs,
1042 ARRAY_LEN(image_info_xml_string_specs));
1046 if (old->windows_info_exists) {
1047 new->windows_info_exists = true;
1048 ret = clone_windows_info(&old->windows_info,
1049 &new->windows_info);
1053 new->wimboot = old->wimboot;
1057 /* Copies the XML information for an image between WIM files.
1059 * @dest_image_name and @dest_image_description are ignored if they are NULL;
1060 * otherwise, they are used to override the image name and/or image description
1061 * from the XML data in the source WIM file.
1063 * On failure, WIMLIB_ERR_NOMEM is returned and no changes are made. Otherwise,
1064 * 0 is returned and the WIM information at *new_wim_info_p is modified.
1067 xml_export_image(const struct wim_info *old_wim_info,
1069 struct wim_info **new_wim_info_p,
1070 const tchar *dest_image_name,
1071 const tchar *dest_image_description)
1073 struct wim_info *new_wim_info;
1074 struct image_info *image_info;
1077 DEBUG("Copying XML data between WIM files for source image %d.", image);
1079 wimlib_assert(old_wim_info != NULL);
1080 wimlib_assert(image >= 1 && image <= old_wim_info->num_images);
1082 if (*new_wim_info_p) {
1083 new_wim_info = *new_wim_info_p;
1085 new_wim_info = CALLOC(1, sizeof(struct wim_info));
1090 image_info = add_image_info_struct(new_wim_info);
1094 ret = clone_image_info(&old_wim_info->images[image - 1], image_info);
1096 goto err_destroy_image_info;
1098 image_info->index = new_wim_info->num_images;
1100 if (dest_image_name) {
1101 FREE(image_info->name);
1102 image_info->name = TSTRDUP(dest_image_name);
1103 if (!image_info->name)
1104 goto err_destroy_image_info;
1106 if (dest_image_description) {
1107 FREE(image_info->description);
1108 image_info->description = TSTRDUP(dest_image_description);
1109 if (!image_info->description)
1110 goto err_destroy_image_info;
1112 *new_wim_info_p = new_wim_info;
1114 err_destroy_image_info:
1115 destroy_image_info(image_info);
1117 if (new_wim_info != *new_wim_info_p)
1118 free_wim_info(new_wim_info);
1119 return WIMLIB_ERR_NOMEM;
1122 /* Removes an image from the XML information. */
1124 xml_delete_image(struct wim_info **wim_info_p, int image)
1126 struct wim_info *wim_info;
1128 wim_info = *wim_info_p;
1129 wimlib_assert(image >= 1 && image <= wim_info->num_images);
1130 DEBUG("Deleting image %d from the XML data.", image);
1132 destroy_image_info(&wim_info->images[image - 1]);
1134 memmove(&wim_info->images[image - 1],
1135 &wim_info->images[image],
1136 (wim_info->num_images - image) * sizeof(struct image_info));
1138 if (--wim_info->num_images == 0) {
1139 free_wim_info(wim_info);
1142 for (int i = image - 1; i < wim_info->num_images; i++)
1143 wim_info->images[i].index--;
1148 xml_get_max_image_name_len(const WIMStruct *wim)
1151 for (u32 i = 0; i < wim->hdr.image_count; i++)
1152 max_len = max(max_len, tstrlen(wim->wim_info->images[i].name));
1157 xml_set_memory_allocator(void *(*malloc_func)(size_t),
1158 void (*free_func)(void *),
1159 void *(*realloc_func)(void *, size_t))
1161 xmlMemSetup(free_func, malloc_func, realloc_func, STRDUP);
1165 inode_sum_stream_sizes(const struct wim_inode *inode,
1166 const struct blob_table *blob_table)
1170 for (unsigned i = 0; i < inode->i_num_streams; i++) {
1171 const struct blob_descriptor *blob;
1173 blob = stream_blob(&inode->i_streams[i], blob_table);
1175 total_size += blob->size;
1181 * Calculate what to put in the <FILECOUNT>, <DIRCOUNT>, <TOTALBYTES>, and
1182 * <HARDLINKBYTES> elements of the specified WIM image.
1184 * Note: since these stats are likely to be used for display purposes only, we
1185 * no longer attempt to duplicate WIMGAPI's weird bugs when calculating them.
1188 xml_update_image_info(WIMStruct *wim, int image)
1190 struct image_info *info;
1191 struct wim_image_metadata *imd;
1192 struct wim_inode *inode;
1195 info = &wim->wim_info->images[image - 1];
1196 imd = wim->image_metadata[image - 1];
1198 info->file_count = 0;
1199 info->dir_count = 0;
1200 info->total_bytes = 0;
1201 info->hard_link_bytes = 0;
1203 image_for_each_inode(inode, imd) {
1204 if (inode_is_directory(inode))
1205 info->dir_count += inode->i_nlink;
1207 info->file_count += inode->i_nlink;
1208 size = inode_sum_stream_sizes(inode, wim->blob_table);
1209 info->total_bytes += size * inode->i_nlink;
1210 info->hard_link_bytes += size * (inode->i_nlink - 1);
1213 info->last_modification_time = now_as_wim_timestamp();
1216 /* Adds an image to the XML information. */
1218 xml_add_image(WIMStruct *wim, const tchar *name)
1220 struct wim_info *wim_info;
1221 struct image_info *image_info;
1223 wimlib_assert(name != NULL);
1225 /* If this is the first image, allocate the struct wim_info. Otherwise
1226 * use the existing struct wim_info. */
1227 if (wim->wim_info) {
1228 wim_info = wim->wim_info;
1230 wim_info = CALLOC(1, sizeof(struct wim_info));
1232 return WIMLIB_ERR_NOMEM;
1235 image_info = add_image_info_struct(wim_info);
1237 goto out_free_wim_info;
1239 if (!(image_info->name = TSTRDUP(name)))
1240 goto out_destroy_image_info;
1242 wim->wim_info = wim_info;
1243 image_info->index = wim_info->num_images;
1244 image_info->creation_time = now_as_wim_timestamp();
1245 xml_update_image_info(wim, image_info->index);
1248 out_destroy_image_info:
1249 destroy_image_info(image_info);
1250 wim_info->num_images--;
1252 if (wim_info != wim->wim_info)
1254 return WIMLIB_ERR_NOMEM;
1257 /* Prints information about the specified image from struct wim_info structure.
1260 print_image_info(const struct wim_info *wim_info, int image)
1262 const struct image_info *image_info;
1266 wimlib_assert(image >= 1 && image <= wim_info->num_images);
1268 image_info = &wim_info->images[image - 1];
1270 tprintf(T("Index: %d\n"), image_info->index);
1271 tprintf(T("Name: %"TS"\n"), image_info->name);
1273 /* Always print the Description: part even if there is no
1275 if (image_info->description)
1276 desc = image_info->description;
1279 tprintf(T("Description: %"TS"\n"), desc);
1281 if (image_info->display_name) {
1282 tprintf(T("Display Name: %"TS"\n"),
1283 image_info->display_name);
1286 if (image_info->display_description) {
1287 tprintf(T("Display Description: %"TS"\n"),
1288 image_info->display_description);
1291 tprintf(T("Directory Count: %"PRIu64"\n"), image_info->dir_count);
1292 tprintf(T("File Count: %"PRIu64"\n"), image_info->file_count);
1293 tprintf(T("Total Bytes: %"PRIu64"\n"), image_info->total_bytes);
1294 tprintf(T("Hard Link Bytes: %"PRIu64"\n"), image_info->hard_link_bytes);
1296 wim_timestamp_to_str(image_info->creation_time, buf, sizeof(buf));
1297 tprintf(T("Creation Time: %"TS"\n"), buf);
1299 wim_timestamp_to_str(image_info->last_modification_time, buf, sizeof(buf));
1300 tprintf(T("Last Modification Time: %"TS"\n"), buf);
1301 if (image_info->windows_info_exists)
1302 print_windows_info(&image_info->windows_info);
1303 if (image_info->flags)
1304 tprintf(T("Flags: %"TS"\n"), image_info->flags);
1305 tprintf(T("WIMBoot compatible: %"TS"\n"),
1306 image_info->wimboot ? T("yes") : T("no"));
1311 libxml_global_init(void)
1314 xmlInitCharEncodingHandlers();
1318 libxml_global_cleanup(void)
1321 xmlCleanupCharEncodingHandlers();
1324 /* Reads the XML data from a WIM file. */
1326 read_wim_xml_data(WIMStruct *wim)
1335 ret = wimlib_get_xml_data(wim, &buf, &bufsize);
1340 doc = xmlReadMemory((const char *)xml_data, bufsize,
1341 NULL, "UTF-16LE", 0);
1343 ERROR("Failed to parse XML data");
1344 ret = WIMLIB_ERR_XML;
1345 goto out_free_xml_data;
1348 root = xmlDocGetRootElement(doc);
1349 if (!root || !node_is_element(root) || !node_name_is(root, "WIM")) {
1350 ERROR("WIM XML data is invalid");
1351 ret = WIMLIB_ERR_XML;
1355 ret = xml_read_wim_info(root, &wim->wim_info);
1364 /* Prepares an in-memory buffer containing the UTF-16LE XML data for a WIM file.
1366 * total_bytes is the number to write in <TOTALBYTES>, or
1367 * WIM_TOTALBYTES_USE_EXISTING to use the existing value in memory, or
1368 * WIM_TOTALBYTES_OMIT to omit <TOTALBYTES> entirely.
1371 prepare_wim_xml_data(WIMStruct *wim, int image, u64 total_bytes,
1372 u8 **xml_data_ret, size_t *xml_len_ret)
1374 xmlCharEncodingHandler *encoding_handler;
1376 xmlOutputBuffer *outbuf;
1377 xmlTextWriter *writer;
1379 const xmlChar *content;
1384 /* Open an xmlTextWriter that writes to an in-memory buffer using
1385 * UTF-16LE encoding. */
1387 encoding_handler = xmlGetCharEncodingHandler(XML_CHAR_ENCODING_UTF16LE);
1388 if (!encoding_handler) {
1389 ERROR("Failed to get XML character encoding handler for UTF-16LE");
1390 ret = WIMLIB_ERR_LIBXML_UTF16_HANDLER_NOT_AVAILABLE;
1394 buf = xmlBufferCreate();
1396 ERROR("Failed to create xmlBuffer");
1397 ret = WIMLIB_ERR_NOMEM;
1401 outbuf = xmlOutputBufferCreateBuffer(buf, encoding_handler);
1403 ERROR("Failed to allocate xmlOutputBuffer");
1404 ret = WIMLIB_ERR_NOMEM;
1405 goto out_buffer_free;
1408 writer = xmlNewTextWriter(outbuf);
1410 ERROR("Failed to allocate xmlTextWriter");
1411 ret = WIMLIB_ERR_NOMEM;
1412 goto out_output_buffer_close;
1415 /* Write the XML document. */
1417 ret = xmlTextWriterStartElement(writer, "WIM");
1419 goto out_write_error;
1421 /* The contents of the <TOTALBYTES> element in the XML data, under the
1422 * <WIM> element (not the <IMAGE> element), is for non-split WIMs the
1423 * size of the WIM file excluding the XML data and integrity table.
1424 * For split WIMs, <TOTALBYTES> takes into account the entire WIM, not
1425 * just the current part. */
1426 if (total_bytes != WIM_TOTALBYTES_OMIT) {
1427 if (total_bytes == WIM_TOTALBYTES_USE_EXISTING) {
1429 total_bytes = wim->wim_info->total_bytes;
1433 ret = xmlTextWriterWriteFormatElement(writer, "TOTALBYTES",
1434 "%"PRIu64, total_bytes);
1436 goto out_write_error;
1439 if (image == WIMLIB_ALL_IMAGES) {
1440 for (int i = 0; i < wim->hdr.image_count; i++) {
1441 ret = xml_write_image_info(writer,
1442 &wim->wim_info->images[i],
1445 goto out_write_error;
1447 goto out_free_text_writer;
1450 ret = xml_write_image_info(writer,
1451 &wim->wim_info->images[image - 1],
1454 goto out_write_error;
1456 goto out_free_text_writer;
1459 ret = xmlTextWriterEndElement(writer);
1461 goto out_write_error;
1463 ret = xmlTextWriterEndDocument(writer);
1465 goto out_write_error;
1467 ret = xmlTextWriterFlush(writer);
1469 goto out_write_error;
1471 /* Retrieve the buffer into which the document was written. */
1473 content = xmlBufferContent(buf);
1474 len = xmlBufferLength(buf);
1476 /* Copy the data into a new buffer, and prefix it with the UTF-16LE BOM
1477 * (byte order mark), which is required by MS's software to understand
1481 xml_data = MALLOC(xml_len);
1483 ret = WIMLIB_ERR_NOMEM;
1484 goto out_free_text_writer;
1488 memcpy(&xml_data[2], content, len);
1490 /* Clean up libxml objects and return success. */
1491 *xml_data_ret = xml_data;
1492 *xml_len_ret = xml_len;
1494 out_free_text_writer:
1495 /* xmlFreeTextWriter will free the attached xmlOutputBuffer. */
1496 xmlFreeTextWriter(writer);
1497 goto out_buffer_free;
1498 out_output_buffer_close:
1499 xmlOutputBufferClose(outbuf);
1503 DEBUG("ret=%d", ret);
1507 ERROR("Error writing XML data");
1508 ret = WIMLIB_ERR_WRITE;
1509 goto out_free_text_writer;
1512 /* Writes the XML data to a WIM file. */
1514 write_wim_xml_data(WIMStruct *wim, int image, u64 total_bytes,
1515 struct wim_reshdr *out_reshdr,
1516 int write_resource_flags)
1522 DEBUG("Writing WIM XML data (image=%d, offset=%"PRIu64")",
1523 image, wim->out_fd.offset);
1525 ret = prepare_wim_xml_data(wim, image, total_bytes,
1526 &xml_data, &xml_len);
1530 /* Write the XML data uncompressed. Although wimlib can handle
1531 * compressed XML data, MS software cannot. */
1532 ret = write_wim_resource_from_buffer(xml_data,
1536 WIMLIB_COMPRESSION_TYPE_NONE,
1540 write_resource_flags);
1542 DEBUG("ret=%d", ret);
1546 /* API function documented in wimlib.h */
1547 WIMLIBAPI const tchar *
1548 wimlib_get_image_name(const WIMStruct *wim, int image)
1550 if (image < 1 || image > wim->hdr.image_count)
1552 return wim->wim_info->images[image - 1].name;
1555 /* API function documented in wimlib.h */
1556 WIMLIBAPI const tchar *
1557 wimlib_get_image_description(const WIMStruct *wim, int image)
1559 if (image < 1 || image > wim->hdr.image_count)
1561 return wim->wim_info->images[image - 1].description;
1564 /* API function documented in wimlib.h */
1566 wimlib_image_name_in_use(const WIMStruct *wim, const tchar *name)
1568 if (!name || !*name)
1570 for (int i = 1; i <= wim->hdr.image_count; i++)
1571 if (!tstrcmp(wim->wim_info->images[i - 1].name, name))
1577 /* API function documented in wimlib.h */
1579 wimlib_get_xml_data(WIMStruct *wim, void **buf_ret, size_t *bufsize_ret)
1581 const struct wim_reshdr *xml_reshdr;
1583 if (wim->filename == NULL && filedes_is_seekable(&wim->in_fd))
1584 return WIMLIB_ERR_NO_FILENAME;
1586 if (buf_ret == NULL || bufsize_ret == NULL)
1587 return WIMLIB_ERR_INVALID_PARAM;
1589 xml_reshdr = &wim->hdr.xml_data_reshdr;
1591 DEBUG("Reading XML data.");
1592 *bufsize_ret = xml_reshdr->uncompressed_size;
1593 return wim_reshdr_to_data(xml_reshdr, wim, buf_ret);
1597 wimlib_extract_xml_data(WIMStruct *wim, FILE *fp)
1603 ret = wimlib_get_xml_data(wim, &buf, &bufsize);
1607 if (fwrite(buf, 1, bufsize, fp) != bufsize) {
1608 ERROR_WITH_ERRNO("Failed to extract XML data");
1609 ret = WIMLIB_ERR_WRITE;
1615 /* API function documented in wimlib.h */
1617 wimlib_set_image_name(WIMStruct *wim, int image, const tchar *name)
1625 if (image < 1 || image > wim->hdr.image_count)
1626 return WIMLIB_ERR_INVALID_IMAGE;
1629 for (i = 1; i <= wim->hdr.image_count; i++) {
1632 if (!tstrcmp(wim->wim_info->images[i - 1].name, name))
1633 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
1639 return WIMLIB_ERR_NOMEM;
1641 FREE(wim->wim_info->images[image - 1].name);
1642 wim->wim_info->images[image - 1].name = p;
1647 do_set_image_info_str(WIMStruct *wim, int image, const tchar *tstr,
1651 tchar **dest_tstr_p;
1653 if (image < 1 || image > wim->hdr.image_count) {
1654 ERROR("%d is not a valid image", image);
1655 return WIMLIB_ERR_INVALID_IMAGE;
1658 tstr_copy = TSTRDUP(tstr);
1660 return WIMLIB_ERR_NOMEM;
1664 dest_tstr_p = (tchar**)((void*)&wim->wim_info->images[image - 1] + offset);
1667 *dest_tstr_p = tstr_copy;
1671 /* API function documented in wimlib.h */
1673 wimlib_set_image_descripton(WIMStruct *wim, int image,
1674 const tchar *description)
1676 return do_set_image_info_str(wim, image, description,
1677 offsetof(struct image_info, description));
1680 /* API function documented in wimlib.h */
1682 wimlib_set_image_flags(WIMStruct *wim, int image, const tchar *flags)
1684 return do_set_image_info_str(wim, image, flags,
1685 offsetof(struct image_info, flags));