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/.
27 #include "lookup_table.h"
28 #include "timestamp.h"
29 #include "wimlib_internal.h"
33 #include <libxml/parser.h>
34 #include <libxml/tree.h>
35 #include <libxml/xmlwriter.h>
36 #include <libxml/encoding.h>
39 /* Structures used to form an in-memory representation of the XML data (other
40 * than the raw parse tree from libxml). */
42 struct windows_version {
54 tchar *installation_type;
59 tchar *default_language;
62 bool windows_version_exists;
63 struct windows_version windows_version;
68 bool windows_info_exists;
74 u64 last_modification_time;
75 struct windows_info windows_info;
79 tchar *display_description;
83 struct xml_string_spec {
88 #define ELEM(STRING_NAME, MEMBER_NAME) \
89 {STRING_NAME, offsetof(struct image_info, MEMBER_NAME)}
90 static const struct xml_string_spec
91 image_info_xml_string_specs[] = {
93 ELEM("DESCRIPTION", description),
94 ELEM("DISPLAYNAME", display_name),
95 ELEM("DISPLAYDESCRIPTION", display_description),
100 #define ELEM(STRING_NAME, MEMBER_NAME) \
101 {STRING_NAME, offsetof(struct windows_info, MEMBER_NAME)}
102 static const struct xml_string_spec
103 windows_info_xml_string_specs[] = {
104 ELEM("PRODUCTNAME", product_name),
105 ELEM("EDITIONID", edition_id),
106 ELEM("INSTALLATIONTYPE", installation_type),
108 ELEM("PRODUCTTYPE", product_type),
109 ELEM("PRODUCTSUITE", product_suite),
114 /* Returns a statically allocated string that is a string representation of the
115 * architecture number. */
126 /* XXX Are there other arch values? */
133 /* Iterate through the children of an xmlNode. */
134 #define for_node_child(parent, child) \
135 for (child = parent->children; child != NULL; child = child->next)
137 /* Utility functions for xmlNodes */
139 node_is_element(xmlNode *node)
141 return node->type == XML_ELEMENT_NODE;
145 node_is_text(xmlNode *node)
147 return node->type == XML_TEXT_NODE;
151 node_name_is(xmlNode *node, const char *name)
153 /* For now, both upper case and lower case element names are accepted. */
154 return strcasecmp((const char *)node->name, name) == 0;
158 node_get_number(const xmlNode *u64_node, int base)
161 for_node_child(u64_node, child)
162 if (node_is_text(child))
163 return strtoull(child->content, NULL, base);
167 /* Finds the text node that is a child of an element node and returns its
168 * content converted to a 64-bit unsigned integer. Returns 0 if no text node is
171 node_get_u64(const xmlNode *u64_node)
173 return node_get_number(u64_node, 10);
176 /* Like node_get_u64(), but expects a number in base 16. */
178 node_get_hex_u64(const xmlNode *u64_node)
180 return node_get_number(u64_node, 16);
184 node_get_string(const xmlNode *string_node, tchar **tstr_ret)
190 for_node_child(string_node, child) {
191 if (node_is_text(child) && child->content) {
192 ret = utf8_to_tstr_simple(child->content, &tstr);
202 /* Returns the timestamp from a time node. It has child elements <HIGHPART> and
203 * <LOWPART> that are then used to construct a 64-bit timestamp. */
205 node_get_timestamp(const xmlNode *time_node)
210 for_node_child(time_node, child) {
211 if (!node_is_element(child))
213 if (node_name_is(child, "HIGHPART"))
214 high_part = node_get_hex_u64(child);
215 else if (node_name_is(child, "LOWPART"))
216 low_part = node_get_hex_u64(child);
218 return (u64)low_part | ((u64)high_part << 32);
221 /* Used to sort an array of struct image_infos by their image indices. */
223 sort_by_index(const void *p1, const void *p2)
225 int index_1 = ((const struct image_info*)p1)->index;
226 int index_2 = ((const struct image_info*)p2)->index;
227 if (index_1 < index_2)
229 else if (index_1 > index_2)
236 /* Frees memory allocated inside a struct windows_info structure. */
238 destroy_windows_info(struct windows_info *windows_info)
240 FREE(windows_info->product_name);
241 FREE(windows_info->edition_id);
242 FREE(windows_info->installation_type);
243 FREE(windows_info->hal);
244 FREE(windows_info->product_type);
245 FREE(windows_info->product_suite);
246 for (size_t i = 0; i < windows_info->num_languages; i++)
247 FREE(windows_info->languages[i]);
248 FREE(windows_info->languages);
249 FREE(windows_info->default_language);
250 FREE(windows_info->system_root);
253 /* Frees memory allocated inside a struct image_info structure. */
255 destroy_image_info(struct image_info *image_info)
257 FREE(image_info->name);
258 FREE(image_info->description);
259 FREE(image_info->flags);
260 FREE(image_info->display_name);
261 FREE(image_info->display_description);
262 destroy_windows_info(&image_info->windows_info);
263 memset(image_info, 0, sizeof(struct image_info));
267 free_wim_info(struct wim_info *info)
271 for (int i = 0; i < info->num_images; i++)
272 destroy_image_info(&info->images[i]);
279 /* Reads the information from a <VERSION> element inside the <WINDOWS> element.
282 xml_read_windows_version(const xmlNode *version_node,
283 struct windows_version* windows_version)
286 for_node_child(version_node, child) {
287 if (!node_is_element(child))
289 if (node_name_is(child, "MAJOR"))
290 windows_version->major = node_get_u64(child);
291 else if (node_name_is(child, "MINOR"))
292 windows_version->minor = node_get_u64(child);
293 else if (node_name_is(child, "BUILD"))
294 windows_version->build = node_get_u64(child);
295 else if (node_name_is(child, "SPBUILD"))
296 windows_version->sp_build = node_get_u64(child);
297 else if (node_name_is(child, "SPLEVEL"))
298 windows_version->sp_level = node_get_u64(child);
302 /* Reads the information from a <LANGUAGE> element inside a <WINDOWS> element.
305 xml_read_languages(const xmlNode *languages_node,
306 tchar ***languages_ret,
307 size_t *num_languages_ret,
308 tchar **default_language_ret)
311 size_t num_languages = 0;
315 for_node_child(languages_node, child)
316 if (node_is_element(child) && node_name_is(child, "LANGUAGE"))
319 languages = CALLOC(num_languages, sizeof(languages[0]));
321 return WIMLIB_ERR_NOMEM;
323 *languages_ret = languages;
324 *num_languages_ret = num_languages;
327 for_node_child(languages_node, child) {
328 if (!node_is_element(child))
330 if (node_name_is(child, "LANGUAGE"))
331 ret = node_get_string(child, languages++);
332 else if (node_name_is(child, "DEFAULT"))
333 ret = node_get_string(child, default_language_ret);
340 /* Reads the information from a <WINDOWS> element inside an <IMAGE> element. */
342 xml_read_windows_info(const xmlNode *windows_node,
343 struct windows_info *windows_info)
348 for_node_child(windows_node, child) {
349 if (!node_is_element(child))
351 if (node_name_is(child, "ARCH")) {
352 windows_info->arch = node_get_u64(child);
353 } else if (node_name_is(child, "PRODUCTNAME")) {
354 ret = node_get_string(child,
355 &windows_info->product_name);
356 } else if (node_name_is(child, "EDITIONID")) {
357 ret = node_get_string(child,
358 &windows_info->edition_id);
359 } else if (node_name_is(child, "INSTALLATIONTYPE")) {
360 ret = node_get_string(child,
361 &windows_info->installation_type);
362 } else if (node_name_is(child, "PRODUCTTYPE")) {
363 ret = node_get_string(child,
364 &windows_info->product_type);
365 } else if (node_name_is(child, "PRODUCTSUITE")) {
366 ret = node_get_string(child,
367 &windows_info->product_suite);
368 } else if (node_name_is(child, "LANGUAGES")) {
369 ret = xml_read_languages(child,
370 &windows_info->languages,
371 &windows_info->num_languages,
372 &windows_info->default_language);
373 } else if (node_name_is(child, "VERSION")) {
374 xml_read_windows_version(child,
375 &windows_info->windows_version);
376 windows_info->windows_version_exists = true;
377 } else if (node_name_is(child, "SYSTEMROOT")) {
378 ret = node_get_string(child, &windows_info->system_root);
379 } else if (node_name_is(child, "HAL")) {
380 ret = node_get_string(child, &windows_info->hal);
388 /* Reads the information from an <IMAGE> element. */
390 xml_read_image_info(xmlNode *image_node, struct image_info *image_info)
396 index_prop = xmlGetProp(image_node, "INDEX");
398 image_info->index = atoi(index_prop);
401 image_info->index = 1;
405 for_node_child(image_node, child) {
406 if (!node_is_element(child))
408 if (node_name_is(child, "DIRCOUNT"))
409 image_info->dir_count = node_get_u64(child);
410 else if (node_name_is(child, "FILECOUNT"))
411 image_info->file_count = node_get_u64(child);
412 else if (node_name_is(child, "TOTALBYTES"))
413 image_info->total_bytes = node_get_u64(child);
414 else if (node_name_is(child, "HARDLINKBYTES"))
415 image_info->hard_link_bytes = node_get_u64(child);
416 else if (node_name_is(child, "CREATIONTIME"))
417 image_info->creation_time = node_get_timestamp(child);
418 else if (node_name_is(child, "LASTMODIFICATIONTIME"))
419 image_info->last_modification_time = node_get_timestamp(child);
420 else if (node_name_is(child, "WINDOWS")) {
421 DEBUG("Found <WINDOWS> tag");
422 ret = xml_read_windows_info(child,
423 &image_info->windows_info);
424 image_info->windows_info_exists = true;
425 } else if (node_name_is(child, "NAME")) {
426 ret = node_get_string(child, &image_info->name);
427 } else if (node_name_is(child, "DESCRIPTION")) {
428 ret = node_get_string(child, &image_info->description);
429 } else if (node_name_is(child, "FLAGS")) {
430 ret = node_get_string(child, &image_info->flags);
431 } else if (node_name_is(child, "DISPLAYNAME")) {
432 ret = node_get_string(child, &image_info->display_name);
433 } else if (node_name_is(child, "DISPLAYDESCRIPTION")) {
434 ret = node_get_string(child, &image_info->display_description);
439 if (!image_info->name) {
441 WARNING("Image with index %d has no name", image_info->index);
442 empty_name = TMALLOC(1);
444 return WIMLIB_ERR_NOMEM;
445 *empty_name = T('\0');
446 image_info->name = empty_name;
451 /* Reads the information from a <WIM> element, which should be the root element
452 * of the XML tree. */
454 xml_read_wim_info(const xmlNode *wim_node, struct wim_info **wim_info_ret)
456 struct wim_info *wim_info;
462 wim_info = CALLOC(1, sizeof(struct wim_info));
464 return WIMLIB_ERR_NOMEM;
466 /* Count how many images there are. */
468 for_node_child(wim_node, child) {
469 if (node_is_element(child) && node_name_is(child, "IMAGE")) {
470 if (num_images == INT_MAX) {
471 return WIMLIB_ERR_IMAGE_COUNT;
477 if (num_images > 0) {
478 /* Allocate the array of struct image_infos and fill them in. */
479 wim_info->images = CALLOC(num_images, sizeof(wim_info->images[0]));
480 if (!wim_info->images) {
481 ret = WIMLIB_ERR_NOMEM;
484 wim_info->num_images = num_images;
486 for_node_child(wim_node, child) {
487 if (!node_is_element(child))
489 if (node_name_is(child, "IMAGE")) {
490 DEBUG("Found <IMAGE> tag");
491 ret = xml_read_image_info(child,
492 &wim_info->images[i]);
496 } else if (node_name_is(child, "TOTALBYTES")) {
497 wim_info->total_bytes = node_get_u64(child);
501 /* Sort the array of image info by image index. */
502 qsort(wim_info->images, num_images,
503 sizeof(struct image_info), sort_by_index);
505 /* Make sure the image indices make sense */
506 for (i = 0; i < num_images; i++) {
507 if (wim_info->images[i].index != i + 1) {
508 ERROR("WIM images are not indexed [1...%d] "
509 "in XML data as expected",
511 return WIMLIB_ERR_IMAGE_COUNT;
516 *wim_info_ret = wim_info;
519 free_wim_info(wim_info);
523 /* Prints the information contained in a `struct windows_info'.
525 * Warning: any strings printed here are in UTF-8 encoding. If the locale
526 * character encoding is not UTF-8, the printed strings may be garbled. */
528 print_windows_info(const struct windows_info *windows_info)
530 const struct windows_version *windows_version;
532 tprintf(T("Architecture: %"TS"\n"),
533 get_arch(windows_info->arch));
535 if (windows_info->product_name) {
536 tprintf(T("Product Name: %"TS"\n"),
537 windows_info->product_name);
540 if (windows_info->edition_id) {
541 tprintf(T("Edition ID: %"TS"\n"),
542 windows_info->edition_id);
545 if (windows_info->installation_type) {
546 tprintf(T("Installation Type: %"TS"\n"),
547 windows_info->installation_type);
550 if (windows_info->hal) {
551 tprintf(T("HAL: %"TS"\n"),
555 if (windows_info->product_type) {
556 tprintf(T("Product Type: %"TS"\n"),
557 windows_info->product_type);
560 if (windows_info->product_suite) {
561 tprintf(T("Product Suite: %"TS"\n"),
562 windows_info->product_suite);
565 tprintf(T("Languages: "));
566 for (size_t i = 0; i < windows_info->num_languages; i++) {
568 tfputs(windows_info->languages[i], stdout);
572 if (windows_info->default_language) {
573 tprintf(T("Default Language: %"TS"\n"),
574 windows_info->default_language);
576 if (windows_info->system_root) {
577 tprintf(T("System Root: %"TS"\n"),
578 windows_info->system_root);
581 if (windows_info->windows_version_exists) {
582 windows_version = &windows_info->windows_version;
583 tprintf(T("Major Version: %"PRIu64"\n"),
584 windows_version->major);
585 tprintf(T("Minor Version: %"PRIu64"\n"),
586 windows_version->minor);
587 tprintf(T("Build: %"PRIu64"\n"),
588 windows_version->build);
589 tprintf(T("Service Pack Build: %"PRIu64"\n"),
590 windows_version->sp_build);
591 tprintf(T("Service Pack Level: %"PRIu64"\n"),
592 windows_version->sp_level);
597 xml_write_string(xmlTextWriter *writer, const char *name,
602 int rc = tstr_to_utf8_simple(tstr, &utf8_str);
605 rc = xmlTextWriterWriteElement(writer, name, utf8_str);
614 xml_write_strings_from_specs(xmlTextWriter *writer,
615 const void *struct_with_strings,
616 const struct xml_string_spec specs[],
619 for (size_t i = 0; i < num_specs; i++) {
620 int rc = xml_write_string(writer, specs[i].name,
621 *(const tchar * const *)
622 (struct_with_strings + specs[i].offset));
630 dup_strings_from_specs(const void *old_struct_with_strings,
631 void *new_struct_with_strings,
632 const struct xml_string_spec specs[],
635 for (size_t i = 0; i < num_specs; i++) {
636 const tchar *old_str = *(const tchar * const *)
637 ((const void*)old_struct_with_strings + specs[i].offset);
638 tchar **new_str_p = (tchar **)((void*)new_struct_with_strings + specs[i].offset);
640 *new_str_p = TSTRDUP(old_str);
642 return WIMLIB_ERR_NOMEM;
648 /* Writes the information contained in a `struct windows_version' to the XML
649 * document being written. This is the <VERSION> element inside the <WINDOWS>
652 xml_write_windows_version(xmlTextWriter *writer,
653 const struct windows_version *version)
656 rc = xmlTextWriterStartElement(writer, "VERSION");
660 rc = xmlTextWriterWriteFormatElement(writer, "MAJOR", "%"PRIu64,
665 rc = xmlTextWriterWriteFormatElement(writer, "MINOR", "%"PRIu64,
670 rc = xmlTextWriterWriteFormatElement(writer, "BUILD", "%"PRIu64,
675 rc = xmlTextWriterWriteFormatElement(writer, "SPBUILD", "%"PRIu64,
680 rc = xmlTextWriterWriteFormatElement(writer, "SPLEVEL", "%"PRIu64,
685 return xmlTextWriterEndElement(writer); /* </VERSION> */
688 /* Writes the information contained in a `struct windows_info' to the XML
689 * document being written. This is the <WINDOWS> element. */
691 xml_write_windows_info(xmlTextWriter *writer,
692 const struct windows_info *windows_info)
695 rc = xmlTextWriterStartElement(writer, "WINDOWS");
699 rc = xmlTextWriterWriteFormatElement(writer, "ARCH", "%"PRIu64,
704 rc = xml_write_strings_from_specs(writer,
706 windows_info_xml_string_specs,
707 ARRAY_LEN(windows_info_xml_string_specs));
711 if (windows_info->num_languages) {
712 rc = xmlTextWriterStartElement(writer, "LANGUAGES");
716 for (size_t i = 0; i < windows_info->num_languages; i++) {
717 rc = xml_write_string(writer, "LANGUAGE",
718 windows_info->languages[i]);
723 rc = xml_write_string(writer, "DEFAULT",
724 windows_info->default_language);
728 rc = xmlTextWriterEndElement(writer); /* </LANGUAGES> */
733 if (windows_info->windows_version_exists) {
734 rc = xml_write_windows_version(writer, &windows_info->windows_version);
739 rc = xml_write_string(writer, "SYSTEMROOT",
740 windows_info->system_root);
744 return xmlTextWriterEndElement(writer); /* </WINDOWS> */
747 /* Writes a time element to the XML document being constructed in memory. */
749 xml_write_time(xmlTextWriter *writer, const char *element_name, u64 time)
752 rc = xmlTextWriterStartElement(writer, element_name);
756 rc = xmlTextWriterWriteFormatElement(writer, "HIGHPART",
757 "0x%08"PRIX32, (u32)(time >> 32));
761 rc = xmlTextWriterWriteFormatElement(writer, "LOWPART",
762 "0x%08"PRIX32, (u32)time);
766 rc = xmlTextWriterEndElement(writer); /* </@element_name> */
772 /* Writes an <IMAGE> element to the XML document. */
774 xml_write_image_info(xmlTextWriter *writer, const struct image_info *image_info)
777 rc = xmlTextWriterStartElement(writer, "IMAGE");
781 rc = xmlTextWriterWriteFormatAttribute(writer, "INDEX", "%d",
786 rc = xmlTextWriterWriteFormatElement(writer, "DIRCOUNT", "%"PRIu64,
787 image_info->dir_count);
791 rc = xmlTextWriterWriteFormatElement(writer, "FILECOUNT", "%"PRIu64,
792 image_info->file_count);
796 rc = xmlTextWriterWriteFormatElement(writer, "TOTALBYTES", "%"PRIu64,
797 image_info->total_bytes);
801 rc = xmlTextWriterWriteFormatElement(writer, "HARDLINKBYTES", "%"PRIu64,
802 image_info->hard_link_bytes);
806 rc = xml_write_time(writer, "CREATIONTIME", image_info->creation_time);
810 rc = xml_write_time(writer, "LASTMODIFICATIONTIME",
811 image_info->last_modification_time);
815 if (image_info->windows_info_exists) {
816 rc = xml_write_windows_info(writer, &image_info->windows_info);
821 rc = xml_write_strings_from_specs(writer, image_info,
822 image_info_xml_string_specs,
823 ARRAY_LEN(image_info_xml_string_specs));
827 rc = xmlTextWriterEndElement(writer); /* </IMAGE> */
835 /* Makes space for another image in the XML information and return a pointer to
837 static struct image_info *
838 add_image_info_struct(struct wim_info *wim_info)
840 struct image_info *images;
842 images = CALLOC(wim_info->num_images + 1, sizeof(struct image_info));
845 memcpy(images, wim_info->images,
846 wim_info->num_images * sizeof(struct image_info));
847 FREE(wim_info->images);
848 wim_info->images = images;
849 wim_info->num_images++;
850 return &images[wim_info->num_images - 1];
854 clone_windows_info(const struct windows_info *old, struct windows_info *new)
858 ret = dup_strings_from_specs(old, new, windows_info_xml_string_specs,
859 ARRAY_LEN(windows_info_xml_string_specs));
863 if (old->languages) {
864 new->languages = CALLOC(old->num_languages, sizeof(new->languages[0]));
866 return WIMLIB_ERR_NOMEM;
867 new->num_languages = old->num_languages;
868 for (size_t i = 0; i < new->num_languages; i++) {
869 if (!old->languages[i])
871 new->languages[i] = TSTRDUP(old->languages[i]);
872 if (!new->languages[i])
873 return WIMLIB_ERR_NOMEM;
876 if (old->default_language &&
877 !(new->default_language = TSTRDUP(old->default_language)))
878 return WIMLIB_ERR_NOMEM;
879 if (old->system_root && !(new->system_root = TSTRDUP(old->system_root)))
880 return WIMLIB_ERR_NOMEM;
881 if (old->windows_version_exists) {
882 new->windows_version_exists = true;
883 memcpy(&new->windows_version, &old->windows_version,
884 sizeof(old->windows_version));
890 clone_image_info(const struct image_info *old, struct image_info *new)
894 new->dir_count = old->dir_count;
895 new->file_count = old->file_count;
896 new->total_bytes = old->total_bytes;
897 new->hard_link_bytes = old->hard_link_bytes;
898 new->creation_time = old->creation_time;
899 new->last_modification_time = old->last_modification_time;
901 ret = dup_strings_from_specs(old, new,
902 image_info_xml_string_specs,
903 ARRAY_LEN(image_info_xml_string_specs));
907 if (old->windows_info_exists) {
908 new->windows_info_exists = true;
909 ret = clone_windows_info(&old->windows_info,
917 /* Copies the XML information for an image between WIM files.
919 * @dest_image_name and @dest_image_description are ignored if they are NULL;
920 * otherwise, they are used to override the image name and/or image description
921 * from the XML data in the source WIM file.
923 * On failure, WIMLIB_ERR_NOMEM is returned and no changes are made. Otherwise,
924 * 0 is returned and the WIM information at *new_wim_info_p is modified.
927 xml_export_image(const struct wim_info *old_wim_info,
929 struct wim_info **new_wim_info_p,
930 const tchar *dest_image_name,
931 const tchar *dest_image_description)
933 struct wim_info *new_wim_info;
934 struct image_info *image_info;
937 DEBUG("Copying XML data between WIM files for source image %d.", image);
939 wimlib_assert(old_wim_info != NULL);
940 wimlib_assert(image >= 1 && image <= old_wim_info->num_images);
942 if (*new_wim_info_p) {
943 new_wim_info = *new_wim_info_p;
945 new_wim_info = CALLOC(1, sizeof(struct wim_info));
950 image_info = add_image_info_struct(new_wim_info);
954 ret = clone_image_info(&old_wim_info->images[image - 1], image_info);
956 goto err_destroy_image_info;
958 image_info->index = new_wim_info->num_images;
960 if (dest_image_name) {
961 FREE(image_info->name);
962 image_info->name = TSTRDUP(dest_image_name);
963 if (!image_info->name)
964 goto err_destroy_image_info;
966 if (dest_image_description) {
967 FREE(image_info->description);
968 image_info->description = TSTRDUP(dest_image_description);
969 if (!image_info->description)
970 goto err_destroy_image_info;
972 *new_wim_info_p = new_wim_info;
974 err_destroy_image_info:
975 destroy_image_info(image_info);
977 if (new_wim_info != *new_wim_info_p)
978 free_wim_info(new_wim_info);
979 return WIMLIB_ERR_NOMEM;
982 /* Removes an image from the XML information. */
984 xml_delete_image(struct wim_info **wim_info_p, int image)
986 struct wim_info *wim_info;
988 wim_info = *wim_info_p;
989 wimlib_assert(image >= 1 && image <= wim_info->num_images);
990 DEBUG("Deleting image %d from the XML data.", image);
992 destroy_image_info(&wim_info->images[image - 1]);
994 memmove(&wim_info->images[image - 1],
995 &wim_info->images[image],
996 (wim_info->num_images - image) * sizeof(struct image_info));
998 if (--wim_info->num_images == 0) {
999 free_wim_info(wim_info);
1002 for (int i = image - 1; i < wim_info->num_images; i++)
1003 wim_info->images[i].index--;
1008 xml_get_max_image_name_len(const WIMStruct *w)
1012 for (int i = 0; i < w->wim_info->num_images; i++) {
1013 size_t len = tstrlen(w->wim_info->images[i].name);
1021 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
1023 xml_set_memory_allocator(void *(*malloc_func)(size_t),
1024 void (*free_func)(void *),
1025 void *(*realloc_func)(void *, size_t))
1027 xmlMemSetup(free_func, malloc_func, realloc_func, STRDUP);
1032 calculate_dentry_statistics(struct wim_dentry *dentry, void *arg)
1034 struct image_info *info = arg;
1035 const struct wim_inode *inode = dentry->d_inode;
1036 struct wim_lookup_table_entry *lte;
1038 /* Update directory count and file count.
1040 * Each dentry counts as either a file or a directory, but not both.
1041 * The root directory is an exception: it is not counted at all.
1043 * Symbolic links and junction points (and presumably other reparse
1044 * points) count as regular files. This is despite the fact that
1045 * junction points have FILE_ATTRIBUTE_DIRECTORY set.
1047 if (dentry_is_root(dentry))
1050 if (inode_is_directory(inode))
1056 * Update total bytes and hard link bytes.
1058 * Unfortunately there are some inconsistencies/bugs in the way this is
1061 * If there are no alternate data streams in the image, the "total
1062 * bytes" is the sum of the size of the un-named data stream of each
1063 * inode times the link count of that inode. In other words, it would
1064 * be the total number of bytes of regular files you would have if you
1065 * extracted the full image without any hard-links. The "hard link
1066 * bytes" is equal to the "total bytes" minus the size of the un-named
1067 * data stream of each inode. In other words, the "hard link bytes"
1068 * counts the size of the un-named data stream for all the links to each
1069 * inode except the first one.
1071 * Reparse points and directories don't seem to be counted in either the
1072 * total bytes or the hard link bytes.
1074 * And now we get to the most confusing part, the alternate data
1075 * streams. They are not counted in the "total bytes". However, if the
1076 * link count of an inode with alternate data streams is 2 or greater,
1077 * the size of all the alternate data streams is included in the "hard
1078 * link bytes", and this size is multiplied by the link count (NOT one
1079 * less than the link count).
1081 lte = inode_unnamed_lte_resolved(inode);
1083 info->total_bytes += wim_resource_size(lte);
1084 if (!dentry_is_first_in_inode(dentry))
1085 info->hard_link_bytes += wim_resource_size(lte);
1088 if (inode->i_nlink >= 2 && dentry_is_first_in_inode(dentry)) {
1089 for (unsigned i = 0; i < inode->i_num_ads; i++) {
1090 if (inode->i_ads_entries[i].stream_name_nbytes) {
1091 lte = inode_stream_lte_resolved(inode, i + 1);
1093 info->hard_link_bytes += inode->i_nlink *
1094 wim_resource_size(lte);
1103 * Calculate what to put in the <FILECOUNT>, <DIRCOUNT>, <TOTALBYTES>, and
1104 * <HARDLINKBYTES> elements of each <IMAGE>.
1106 * Please note there is no official documentation for exactly how this is done.
1107 * But, see calculate_dentry_statistics().
1110 xml_update_image_info(WIMStruct *w, int image)
1112 struct image_info *image_info;
1114 DEBUG("Updating the image info for image %d", image);
1116 image_info = &w->wim_info->images[image - 1];
1118 image_info->file_count = 0;
1119 image_info->dir_count = 0;
1120 image_info->total_bytes = 0;
1121 image_info->hard_link_bytes = 0;
1123 for_dentry_in_tree(w->image_metadata[image - 1]->root_dentry,
1124 calculate_dentry_statistics,
1126 image_info->last_modification_time = get_wim_timestamp();
1129 /* Adds an image to the XML information. */
1131 xml_add_image(WIMStruct *w, const tchar *name)
1133 struct wim_info *wim_info;
1134 struct image_info *image_info;
1136 wimlib_assert(name != NULL);
1138 /* If this is the first image, allocate the struct wim_info. Otherwise
1139 * use the existing struct wim_info. */
1141 wim_info = w->wim_info;
1143 wim_info = CALLOC(1, sizeof(struct wim_info));
1145 return WIMLIB_ERR_NOMEM;
1148 image_info = add_image_info_struct(wim_info);
1150 goto out_free_wim_info;
1152 if (!(image_info->name = TSTRDUP(name)))
1153 goto out_destroy_image_info;
1155 w->wim_info = wim_info;
1156 image_info->index = wim_info->num_images;
1157 image_info->creation_time = get_wim_timestamp();
1158 xml_update_image_info(w, image_info->index);
1161 out_destroy_image_info:
1162 destroy_image_info(image_info);
1163 wim_info->num_images--;
1165 if (wim_info != w->wim_info)
1167 return WIMLIB_ERR_NOMEM;
1170 /* Prints information about the specified image from struct wim_info structure.
1173 print_image_info(const struct wim_info *wim_info, int image)
1175 const struct image_info *image_info;
1179 wimlib_assert(image >= 1 && image <= wim_info->num_images);
1181 image_info = &wim_info->images[image - 1];
1183 tprintf(T("Index: %d\n"), image_info->index);
1184 tprintf(T("Name: %"TS"\n"), image_info->name);
1186 /* Always print the Description: part even if there is no
1188 if (image_info->description)
1189 desc = image_info->description;
1192 tprintf(T("Description: %"TS"\n"), desc);
1194 if (image_info->display_name) {
1195 tprintf(T("Display Name: %"TS"\n"),
1196 image_info->display_name);
1199 if (image_info->display_description) {
1200 tprintf(T("Display Description: %"TS"\n"),
1201 image_info->display_description);
1204 tprintf(T("Directory Count: %"PRIu64"\n"), image_info->dir_count);
1205 tprintf(T("File Count: %"PRIu64"\n"), image_info->file_count);
1206 tprintf(T("Total Bytes: %"PRIu64"\n"), image_info->total_bytes);
1207 tprintf(T("Hard Link Bytes: %"PRIu64"\n"), image_info->hard_link_bytes);
1209 wim_timestamp_to_str(image_info->creation_time, buf, sizeof(buf));
1210 tprintf(T("Creation Time: %"TS"\n"), buf);
1212 wim_timestamp_to_str(image_info->creation_time, buf, sizeof(buf));
1213 tprintf(T("Last Modification Time: %"TS"\n"), buf);
1214 if (image_info->windows_info_exists)
1215 print_windows_info(&image_info->windows_info);
1216 if (image_info->flags)
1217 tprintf(T("Flags: %"TS"\n"), image_info->flags);
1222 libxml_global_init()
1225 xmlInitCharEncodingHandlers();
1229 libxml_global_cleanup()
1232 xmlCleanupCharEncodingHandlers();
1236 * Reads the XML data from a WIM file.
1239 read_xml_data(FILE *fp, const struct resource_entry *res_entry,
1240 utf16lechar **xml_data_ret, struct wim_info **info_ret)
1242 utf16lechar *xml_data;
1247 DEBUG("XML data is %"PRIu64" bytes at offset %"PRIu64"",
1248 (u64)res_entry->size, res_entry->offset);
1250 if (resource_is_compressed(res_entry)) {
1251 ERROR("XML data is supposed to be uncompressed");
1252 ret = WIMLIB_ERR_XML;
1256 if (res_entry->size < 2) {
1257 ERROR("XML data must be at least 2 bytes long");
1258 ret = WIMLIB_ERR_XML;
1262 xml_data = MALLOC(res_entry->size + 3);
1264 ret = WIMLIB_ERR_NOMEM;
1268 ret = read_uncompressed_resource(fp, res_entry->offset,
1269 res_entry->size, xml_data);
1271 goto out_free_xml_data;
1273 /* Null-terminate just in case */
1274 ((u8*)xml_data)[res_entry->size] = 0;
1275 ((u8*)xml_data)[res_entry->size + 1] = 0;
1276 ((u8*)xml_data)[res_entry->size + 2] = 0;
1278 DEBUG("Parsing XML using libxml2 to create XML tree");
1280 doc = xmlReadMemory((const char *)xml_data,
1281 res_entry->size, "noname.xml", "UTF-16", 0);
1284 ERROR("Failed to parse XML data");
1285 ret = WIMLIB_ERR_XML;
1286 goto out_free_xml_data;
1289 DEBUG("Constructing WIM information structure from XML tree.");
1291 root = xmlDocGetRootElement(doc);
1293 ERROR("WIM XML data is an empty XML document");
1294 ret = WIMLIB_ERR_XML;
1298 if (!node_is_element(root) || !node_name_is(root, "WIM")) {
1299 ERROR("Expected <WIM> for the root XML element");
1300 ret = WIMLIB_ERR_XML;
1304 ret = xml_read_wim_info(root, info_ret);
1308 *xml_data_ret = xml_data;
1311 DEBUG("Freeing XML tree.");
1319 #define CHECK_RET ({ if (ret < 0) { \
1320 ERROR("Error writing XML data"); \
1321 ret = WIMLIB_ERR_WRITE; \
1322 goto out_free_text_writer; \
1326 * Writes XML data to a WIM file.
1328 * If @total_bytes is non-zero, it specifies what to write to the TOTALBYTES
1329 * element in the XML data. If zero, TOTALBYTES is given the default value of
1330 * the offset of the XML data.
1333 write_xml_data(const struct wim_info *wim_info, int image, FILE *out,
1334 u64 total_bytes, struct resource_entry *out_res_entry)
1336 xmlCharEncodingHandler *encoding_handler;
1337 xmlOutputBuffer *out_buffer;
1338 xmlTextWriter *writer;
1343 wimlib_assert(image == WIMLIB_ALL_IMAGES ||
1344 (wim_info != NULL && image >= 1 &&
1345 image <= wim_info->num_images));
1347 start_offset = ftello(out);
1348 if (start_offset == -1)
1349 return WIMLIB_ERR_WRITE;
1351 DEBUG("Writing XML data for image %d at offset %"PRIu64,
1352 image, start_offset);
1354 /* 2 bytes endianness marker for UTF-16LE. This is _required_ for WIM
1356 if ((putc(0xff, out)) == EOF || (putc(0xfe, out) == EOF)) {
1357 ERROR_WITH_ERRNO("Error writing XML data");
1358 return WIMLIB_ERR_WRITE;
1361 /* The contents of the <TOTALBYTES> element in the XML data, under the
1362 * <WIM> element (not the <IMAGE> element), is for non-split WIMs the
1363 * size of the WIM file excluding the XML data and integrity table.
1364 * This should be equal to the current position in the output stream,
1365 * since the XML data and integrity table are the last elements of the
1368 * For split WIMs, <TOTALBYTES> takes into account the entire WIM, not
1369 * just the current part. In that case, @total_bytes should be passed
1370 * in to this function. */
1371 if (total_bytes == 0)
1372 total_bytes = start_offset;
1374 /* The encoding of the XML data must be UTF-16LE. */
1375 encoding_handler = xmlGetCharEncodingHandler(XML_CHAR_ENCODING_UTF16LE);
1376 if (!encoding_handler) {
1377 ERROR("Failed to get XML character encoding handler for UTF-16LE");
1378 ret = WIMLIB_ERR_LIBXML_UTF16_HANDLER_NOT_AVAILABLE;
1382 out_buffer = xmlOutputBufferCreateFile(out, encoding_handler);
1384 ERROR("Failed to allocate xmlOutputBuffer");
1385 ret = WIMLIB_ERR_NOMEM;
1389 writer = xmlNewTextWriter(out_buffer);
1391 ERROR("Failed to allocate xmlTextWriter");
1392 ret = WIMLIB_ERR_NOMEM;
1393 goto out_output_buffer_close;
1396 DEBUG("Writing <WIM> element");
1398 ret = xmlTextWriterStartElement(writer, "WIM");
1401 ret = xmlTextWriterWriteFormatElement(writer, "TOTALBYTES", "%"PRIu64,
1405 if (wim_info != NULL) {
1407 if (image == WIMLIB_ALL_IMAGES) {
1409 last = wim_info->num_images;
1414 DEBUG("Writing %d <IMAGE> elements", last - first + 1);
1415 for (int i = first; i <= last; i++) {
1416 ret = xml_write_image_info(writer, &wim_info->images[i - 1]);
1419 goto out_free_text_writer;
1424 ret = xmlTextWriterEndElement(writer);
1427 ret = xmlTextWriterEndDocument(writer);
1430 DEBUG("Ended XML document");
1432 /* Call xmlFreeTextWriter() before ftello() because the former will
1433 * flush the file stream. */
1434 xmlFreeTextWriter(writer);
1437 end_offset = ftello(out);
1438 if (end_offset == -1) {
1439 ret = WIMLIB_ERR_WRITE;
1442 out_res_entry->offset = start_offset;
1443 out_res_entry->size = end_offset - start_offset;
1444 out_res_entry->original_size = end_offset - start_offset;
1445 out_res_entry->flags = WIM_RESHDR_FLAG_METADATA;
1447 out_free_text_writer:
1448 /* xmlFreeTextWriter will free the attached xmlOutputBuffer. */
1449 xmlFreeTextWriter(writer);
1451 out_output_buffer_close:
1452 if (out_buffer != NULL)
1453 xmlOutputBufferClose(out_buffer);
1456 DEBUG("Successfully wrote XML data");
1460 /* Returns the name of the specified image. */
1461 WIMLIBAPI const tchar *
1462 wimlib_get_image_name(const WIMStruct *w, int image)
1464 if (image < 1 || image > w->hdr.image_count)
1466 return w->wim_info->images[image - 1].name;
1469 /* Returns the description of the specified image. */
1470 WIMLIBAPI const tchar *
1471 wimlib_get_image_description(const WIMStruct *w, int image)
1473 if (image < 1 || image > w->hdr.image_count)
1475 return w->wim_info->images[image - 1].description;
1478 /* Determines if an image name is already used by some image in the WIM. */
1480 wimlib_image_name_in_use(const WIMStruct *w, const tchar *name)
1482 if (!name || !*name)
1484 for (int i = 1; i <= w->hdr.image_count; i++)
1485 if (!tstrcmp(w->wim_info->images[i - 1].name, name))
1491 /* Extracts the raw XML data to a file stream. */
1493 wimlib_extract_xml_data(WIMStruct *w, FILE *fp)
1495 size_t bytes_written;
1498 return WIMLIB_ERR_INVALID_PARAM;
1499 bytes_written = fwrite(w->xml_data, 1, w->hdr.xml_res_entry.size, fp);
1500 if (bytes_written != w->hdr.xml_res_entry.size) {
1501 ERROR_WITH_ERRNO("Failed to extract XML data");
1502 return WIMLIB_ERR_WRITE;
1507 /* Sets the name of an image in the WIM. */
1509 wimlib_set_image_name(WIMStruct *w, int image, const tchar *name)
1514 DEBUG("Setting the name of image %d to %"TS, image, name);
1516 if (!name || !*name) {
1517 ERROR("Must specify a non-empty string for the image name");
1518 return WIMLIB_ERR_INVALID_PARAM;
1521 if (image < 1 || image > w->hdr.image_count) {
1522 ERROR("%d is not a valid image", image);
1523 return WIMLIB_ERR_INVALID_IMAGE;
1526 for (i = 1; i <= w->hdr.image_count; i++) {
1529 if (tstrcmp(w->wim_info->images[i - 1].name, name) == 0) {
1530 ERROR("The name \"%"TS"\" is already in use in the WIM!",
1532 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
1538 return WIMLIB_ERR_NOMEM;
1540 FREE(w->wim_info->images[image - 1].name);
1541 w->wim_info->images[image - 1].name = p;
1546 do_set_image_info_str(WIMStruct *w, int image, const tchar *tstr,
1550 tchar **dest_tstr_p;
1552 if (image < 1 || image > w->hdr.image_count) {
1553 ERROR("%d is not a valid image", image);
1554 return WIMLIB_ERR_INVALID_IMAGE;
1557 tstr_copy = TSTRDUP(tstr);
1559 return WIMLIB_ERR_NOMEM;
1563 dest_tstr_p = (tchar**)((void*)&w->wim_info->images[image - 1] + offset);
1566 *dest_tstr_p = tstr_copy;
1570 /* Sets the description of an image in the WIM. */
1572 wimlib_set_image_descripton(WIMStruct *w, int image,
1573 const tchar *description)
1575 return do_set_image_info_str(w, image, description,
1576 offsetof(struct image_info, description));
1579 /* Set the <FLAGS> element of a WIM image */
1581 wimlib_set_image_flags(WIMStruct *w, int image, const tchar *flags)
1583 return do_set_image_info_str(w, image, flags,
1584 offsetof(struct image_info, flags));