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 {
52 utf8char *product_name;
54 utf8char *installation_type;
56 utf8char *product_type;
57 utf8char *product_suite;
59 utf8char *default_language;
61 utf8char *system_root;
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;
77 utf8char *description;
78 utf8char *display_name;
79 utf8char *display_description;
82 struct wim_lookup_table *lookup_table;
87 /* Returns a statically allocated string that is a string representation of the
88 * architecture number. */
99 /* XXX Are there other arch values? */
106 /* Iterate through the children of an xmlNode. */
107 #define for_node_child(parent, child) \
108 for (child = parent->children; child != NULL; child = child->next)
110 /* Utility functions for xmlNodes */
112 node_is_element(xmlNode *node)
114 return node->type == XML_ELEMENT_NODE;
118 node_is_text(xmlNode *node)
120 return node->type == XML_TEXT_NODE;
124 node_name_is(xmlNode *node, const utf8char *name)
126 /* For now, both upper case and lower case element names are accepted. */
127 return strcasecmp((const char *)node->name, name) == 0;
130 /* Finds the text node that is a child of an element node and returns its
131 * content converted to a 64-bit unsigned integer. Returns 0 if no text node is
134 node_get_u64(const xmlNode *u64_node)
137 for_node_child(u64_node, child)
138 if (node_is_text(child))
139 return strtoull((const char *)child->content, NULL, 10);
143 /* Like node_get_u64(), but expects a number in base 16. */
145 node_get_hex_u64(const xmlNode *u64_node)
148 for_node_child(u64_node, child)
149 if (node_is_text(child))
150 return strtoull(child->content, NULL, 16);
155 node_get_string(const xmlNode *string_node, utf8char **str)
160 for_node_child(string_node, child) {
161 if (node_is_text(child) && child->content) {
162 p = STRDUP(child->content);
164 return WIMLIB_ERR_NOMEM;
172 /* Returns the timestamp from a time node. It has child elements <HIGHPART> and
173 * <LOWPART> that are then used to construct a 64-bit timestamp. */
175 node_get_timestamp(const xmlNode *time_node)
180 for_node_child(time_node, child) {
181 if (!node_is_element(child))
183 if (node_name_is(child, "HIGHPART"))
184 high_part = node_get_hex_u64(child);
185 else if (node_name_is(child, "LOWPART"))
186 low_part = node_get_hex_u64(child);
188 return (u64)low_part | ((u64)high_part << 32);
191 /* Used to sort an array of struct image_infos by their image indices. */
193 sort_by_index(const void *p1, const void *p2)
195 int index_1 = ((const struct image_info*)p1)->index;
196 int index_2 = ((const struct image_info*)p2)->index;
197 if (index_1 < index_2)
199 else if (index_1 > index_2)
206 /* Frees memory allocated inside a struct windows_info structure. */
208 destroy_windows_info(struct windows_info *windows_info)
210 FREE(windows_info->product_name);
211 FREE(windows_info->edition_id);
212 FREE(windows_info->installation_type);
213 FREE(windows_info->hal);
214 FREE(windows_info->product_type);
215 FREE(windows_info->product_suite);
216 for (size_t i = 0; i < windows_info->num_languages; i++)
217 FREE(windows_info->languages[i]);
218 FREE(windows_info->languages);
219 FREE(windows_info->default_language);
220 FREE(windows_info->system_root);
223 /* Frees memory allocated inside a struct image_info structure. */
225 destroy_image_info(struct image_info *image_info)
227 FREE(image_info->name);
228 FREE(image_info->description);
229 FREE(image_info->flags);
230 FREE(image_info->display_name);
231 FREE(image_info->display_description);
232 destroy_windows_info(&image_info->windows_info);
233 memset(image_info, 0, sizeof(struct image_info));
237 free_wim_info(struct wim_info *info)
241 for (int i = 0; i < info->num_images; i++)
242 destroy_image_info(&info->images[i]);
249 /* Reads the information from a <VERSION> element inside the <WINDOWS> element.
252 xml_read_windows_version(const xmlNode *version_node,
253 struct windows_version* windows_version)
256 for_node_child(version_node, child) {
257 if (!node_is_element(child))
259 if (node_name_is(child, "MAJOR"))
260 windows_version->major = node_get_u64(child);
261 else if (node_name_is(child, "MINOR"))
262 windows_version->minor = node_get_u64(child);
263 else if (node_name_is(child, "BUILD"))
264 windows_version->build = node_get_u64(child);
265 else if (node_name_is(child, "SPBUILD"))
266 windows_version->sp_build = node_get_u64(child);
267 else if (node_name_is(child, "SPLEVEL"))
268 windows_version->sp_level = node_get_u64(child);
272 /* Reads the information from a <LANGUAGE> element inside a <WINDOWS> element.
275 xml_read_languages(const xmlNode *languages_node,
276 utf8char ***languages_ret,
277 size_t *num_languages_ret,
278 utf8char **default_language_ret)
281 size_t num_languages = 0;
282 utf8char **languages;
285 for_node_child(languages_node, child)
286 if (node_is_element(child) && node_name_is(child, "LANGUAGE"))
289 languages = CALLOC(num_languages, sizeof(languages[0]));
291 return WIMLIB_ERR_NOMEM;
293 *languages_ret = languages;
294 *num_languages_ret = num_languages;
297 for_node_child(languages_node, child) {
298 if (!node_is_element(child))
300 if (node_name_is(child, "LANGUAGE"))
301 ret = node_get_string(child, languages++);
302 else if (node_name_is(child, "DEFAULT"))
303 ret = node_get_string(child, default_language_ret);
310 /* Reads the information from a <WINDOWS> element inside an <IMAGE> element. */
312 xml_read_windows_info(const xmlNode *windows_node,
313 struct windows_info *windows_info)
318 for_node_child(windows_node, child) {
319 if (!node_is_element(child))
321 if (node_name_is(child, "ARCH")) {
322 windows_info->arch = node_get_u64(child);
323 } else if (node_name_is(child, "PRODUCTNAME")) {
324 ret = node_get_string(child,
325 &windows_info->product_name);
326 } else if (node_name_is(child, "EDITIONID")) {
327 ret = node_get_string(child,
328 &windows_info->edition_id);
329 } else if (node_name_is(child, "INSTALLATIONTYPE")) {
330 ret = node_get_string(child,
331 &windows_info->installation_type);
332 } else if (node_name_is(child, "PRODUCTTYPE")) {
333 ret = node_get_string(child,
334 &windows_info->product_type);
335 } else if (node_name_is(child, "PRODUCTSUITE")) {
336 ret = node_get_string(child,
337 &windows_info->product_suite);
338 } else if (node_name_is(child, "LANGUAGES")) {
339 ret = xml_read_languages(child,
340 &windows_info->languages,
341 &windows_info->num_languages,
342 &windows_info->default_language);
343 } else if (node_name_is(child, "VERSION")) {
344 xml_read_windows_version(child,
345 &windows_info->windows_version);
346 windows_info->windows_version_exists = true;
347 } else if (node_name_is(child, "SYSTEMROOT")) {
348 ret = node_get_string(child, &windows_info->system_root);
349 } else if (node_name_is(child, "HAL")) {
350 ret = node_get_string(child, &windows_info->hal);
358 /* Reads the information from an <IMAGE> element. */
360 xml_read_image_info(xmlNode *image_node, struct image_info *image_info)
366 index_prop = xmlGetProp(image_node, "INDEX");
368 image_info->index = atoi(index_prop);
371 image_info->index = 1;
375 for_node_child(image_node, child) {
376 if (!node_is_element(child))
378 if (node_name_is(child, "DIRCOUNT"))
379 image_info->dir_count = node_get_u64(child);
380 else if (node_name_is(child, "FILECOUNT"))
381 image_info->file_count = node_get_u64(child);
382 else if (node_name_is(child, "TOTALBYTES"))
383 image_info->total_bytes = node_get_u64(child);
384 else if (node_name_is(child, "HARDLINKBYTES"))
385 image_info->hard_link_bytes = node_get_u64(child);
386 else if (node_name_is(child, "CREATIONTIME"))
387 image_info->creation_time = node_get_timestamp(child);
388 else if (node_name_is(child, "LASTMODIFICATIONTIME"))
389 image_info->last_modification_time = node_get_timestamp(child);
390 else if (node_name_is(child, "WINDOWS")) {
391 DEBUG("Found <WINDOWS> tag");
392 ret = xml_read_windows_info(child,
393 &image_info->windows_info);
394 image_info->windows_info_exists = true;
395 } else if (node_name_is(child, "NAME")) {
396 ret = node_get_string(child, &image_info->name);
397 } else if (node_name_is(child, "DESCRIPTION")) {
398 ret = node_get_string(child, &image_info->description);
399 } else if (node_name_is(child, "FLAGS")) {
400 ret = node_get_string(child, &image_info->flags);
401 } else if (node_name_is(child, "DISPLAYNAME")) {
402 ret = node_get_string(child, &image_info->display_name);
403 } else if (node_name_is(child, "DISPLAYDESCRIPTION")) {
404 ret = node_get_string(child, &image_info->display_description);
409 if (!image_info->name) {
410 utf8char *empty_name;
411 WARNING("Image with index %d has no name", image_info->index);
412 empty_name = MALLOC(1);
414 return WIMLIB_ERR_NOMEM;
416 image_info->name = empty_name;
421 /* Reads the information from a <WIM> element, which should be the root element
422 * of the XML tree. */
424 xml_read_wim_info(const xmlNode *wim_node, struct wim_info **wim_info_ret)
426 struct wim_info *wim_info;
432 wim_info = CALLOC(1, sizeof(struct wim_info));
434 return WIMLIB_ERR_NOMEM;
436 /* Count how many images there are. */
438 for_node_child(wim_node, child) {
439 if (node_is_element(child) && node_name_is(child, "IMAGE")) {
440 if (num_images == INT_MAX) {
441 return WIMLIB_ERR_IMAGE_COUNT;
447 if (num_images > 0) {
448 /* Allocate the array of struct image_infos and fill them in. */
449 wim_info->images = CALLOC(num_images, sizeof(wim_info->images[0]));
450 if (!wim_info->images) {
451 ret = WIMLIB_ERR_NOMEM;
454 wim_info->num_images = num_images;
456 for_node_child(wim_node, child) {
457 if (!node_is_element(child))
459 if (node_name_is(child, "IMAGE")) {
460 DEBUG("Found <IMAGE> tag");
461 ret = xml_read_image_info(child,
462 &wim_info->images[i]);
466 } else if (node_name_is(child, "TOTALBYTES")) {
467 wim_info->total_bytes = node_get_u64(child);
471 /* Sort the array of image info by image index. */
472 qsort(wim_info->images, num_images,
473 sizeof(struct image_info), sort_by_index);
475 /* Make sure the image indices make sense */
476 for (i = 0; i < num_images; i++) {
477 if (wim_info->images[i].index != i + 1) {
478 ERROR("WIM images are not indexed [1...%d] "
479 "in XML data as expected",
481 return WIMLIB_ERR_IMAGE_COUNT;
486 *wim_info_ret = wim_info;
489 free_wim_info(wim_info);
493 /* Prints the information contained in a `struct windows_info'.
495 * Warning: any strings printed here are in UTF-8 encoding. If the locale
496 * character encoding is not UTF-8, the printed strings may be garbled. */
498 print_windows_info(const struct windows_info *windows_info)
500 const struct windows_version *windows_version;
502 wimlib_printf("Architecture: %s\n",
503 get_arch(windows_info->arch) ?: "unknown");
505 if (windows_info->product_name)
506 wimlib_printf("Product Name: %U\n",
507 windows_info->product_name);
509 if (windows_info->edition_id)
510 wimlib_printf("Edition ID: %U\n",
511 windows_info->edition_id);
513 if (windows_info->installation_type)
514 wimlib_printf("Installation Type: %U\n",
515 windows_info->installation_type);
517 if (windows_info->hal)
518 wimlib_printf("HAL: %U\n",
521 if (windows_info->product_type)
522 wimlib_printf("Product Type: %U\n",
523 windows_info->product_type);
525 if (windows_info->product_suite)
526 wimlib_printf("Product Suite: %U\n",
527 windows_info->product_suite);
529 printf("Languages: ");
530 for (size_t i = 0; i < windows_info->num_languages; i++) {
531 wimlib_printf("%U", windows_info->languages[i]);
535 if (windows_info->default_language)
536 wimlib_printf("Default Language: %U\n",
537 windows_info->default_language);
538 if (windows_info->system_root)
539 wimlib_printf("System Root: %U\n",
540 windows_info->system_root);
541 if (windows_info->windows_version_exists) {
542 windows_version = &windows_info->windows_version;
543 printf("Major Version: %"PRIu64"\n",
544 windows_version->major);
545 printf("Minor Version: %"PRIu64"\n",
546 windows_version->minor);
547 printf("Build: %"PRIu64"\n",
548 windows_version->build);
549 printf("Service Pack Build: %"PRIu64"\n",
550 windows_version->sp_build);
551 printf("Service Pack Level: %"PRIu64"\n",
552 windows_version->sp_level);
557 /* Writes the information contained in a `struct windows_version' to the XML
558 * document being written. This is the <VERSION> element inside the <WINDOWS>
561 xml_write_windows_version(xmlTextWriter *writer,
562 const struct windows_version *version)
565 rc = xmlTextWriterStartElement(writer, "VERSION");
569 rc = xmlTextWriterWriteFormatElement(writer, "MAJOR", "%"PRIu64,
574 rc = xmlTextWriterWriteFormatElement(writer, "MINOR", "%"PRIu64,
579 rc = xmlTextWriterWriteFormatElement(writer, "BUILD", "%"PRIu64,
584 rc = xmlTextWriterWriteFormatElement(writer, "SPBUILD", "%"PRIu64,
589 rc = xmlTextWriterWriteFormatElement(writer, "SPLEVEL", "%"PRIu64,
594 return xmlTextWriterEndElement(writer); /* </VERSION> */
597 /* Writes the information contained in a `struct windows_info' to the XML
598 * document being written. This is the <WINDOWS> element. */
600 xml_write_windows_info(xmlTextWriter *writer,
601 const struct windows_info *windows_info)
604 rc = xmlTextWriterStartElement(writer, "WINDOWS");
608 rc = xmlTextWriterWriteFormatElement(writer, "ARCH", "%"PRIu64,
613 if (windows_info->product_name) {
614 rc = xmlTextWriterWriteElement(writer, "PRODUCTNAME",
615 windows_info->product_name);
620 if (windows_info->edition_id) {
621 rc = xmlTextWriterWriteElement(writer, "EDITIONID",
622 windows_info->edition_id);
627 if (windows_info->installation_type) {
628 rc = xmlTextWriterWriteElement(writer, "INSTALLATIONTYPE",
629 windows_info->installation_type);
634 if (windows_info->hal) {
635 rc = xmlTextWriterWriteElement(writer, "HAL",
641 if (windows_info->product_type) {
642 rc = xmlTextWriterWriteElement(writer, "PRODUCTTYPE",
643 windows_info->product_type);
648 if (windows_info->product_suite) {
649 rc = xmlTextWriterWriteElement(writer, "PRODUCTSUITE",
650 windows_info->product_suite);
655 if (windows_info->num_languages) {
656 rc = xmlTextWriterStartElement(writer, "LANGUAGES");
660 for (size_t i = 0; i < windows_info->num_languages; i++) {
661 rc = xmlTextWriterWriteElement(writer, "LANGUAGE",
662 windows_info->languages[i]);
666 rc = xmlTextWriterWriteElement(writer, "DEFAULT",
667 windows_info->default_language);
671 rc = xmlTextWriterEndElement(writer); /* </LANGUAGES> */
676 if (windows_info->windows_version_exists) {
677 rc = xml_write_windows_version(writer, &windows_info->windows_version);
682 if (windows_info->system_root) {
683 rc = xmlTextWriterWriteElement(writer, "SYSTEMROOT",
684 windows_info->system_root);
689 return xmlTextWriterEndElement(writer); /* </WINDOWS> */
692 /* Writes a time element to the XML document being constructed in memory. */
694 xml_write_time(xmlTextWriter *writer, const utf8char *element_name, u64 time)
697 rc = xmlTextWriterStartElement(writer, element_name);
701 rc = xmlTextWriterWriteFormatElement(writer, "HIGHPART",
702 "0x%08"PRIX32, (u32)(time >> 32));
706 rc = xmlTextWriterWriteFormatElement(writer, "LOWPART",
707 "0x%08"PRIX32, (u32)time);
711 rc = xmlTextWriterEndElement(writer); /* </@element_name> */
717 /* Writes an <IMAGE> element to the XML document. */
719 xml_write_image_info(xmlTextWriter *writer, const struct image_info *image_info)
722 rc = xmlTextWriterStartElement(writer, "IMAGE");
726 rc = xmlTextWriterWriteFormatAttribute(writer, "INDEX", "%d",
731 rc = xmlTextWriterWriteFormatElement(writer, "DIRCOUNT", "%"PRIu64,
732 image_info->dir_count);
736 rc = xmlTextWriterWriteFormatElement(writer, "FILECOUNT", "%"PRIu64,
737 image_info->file_count);
741 rc = xmlTextWriterWriteFormatElement(writer, "TOTALBYTES", "%"PRIu64,
742 image_info->total_bytes);
746 rc = xmlTextWriterWriteFormatElement(writer, "HARDLINKBYTES", "%"PRIu64,
747 image_info->hard_link_bytes);
751 rc = xml_write_time(writer, "CREATIONTIME", image_info->creation_time);
755 rc = xml_write_time(writer, "LASTMODIFICATIONTIME",
756 image_info->last_modification_time);
760 if (image_info->windows_info_exists) {
761 rc = xml_write_windows_info(writer, &image_info->windows_info);
766 if (image_info->name) {
767 rc = xmlTextWriterWriteElement(writer, "NAME",
773 if (image_info->description) {
774 rc = xmlTextWriterWriteElement(writer, "DESCRIPTION",
775 image_info->description);
779 if (image_info->display_name) {
780 rc = xmlTextWriterWriteElement(writer, "DISPLAYNAME",
781 image_info->display_name);
785 if (image_info->display_description) {
786 rc = xmlTextWriterWriteElement(writer, "DISPLAYDESCRIPTION",
787 image_info->display_description);
792 if (image_info->flags) {
793 rc = xmlTextWriterWriteElement(writer, "FLAGS",
799 return xmlTextWriterEndElement(writer); /* </IMAGE> */
804 /* Makes space for another image in the XML information and return a pointer to
806 static struct image_info *
807 add_image_info_struct(struct wim_info *wim_info)
809 struct image_info *images;
811 images = CALLOC(wim_info->num_images + 1, sizeof(struct image_info));
814 memcpy(images, wim_info->images,
815 wim_info->num_images * sizeof(struct image_info));
816 FREE(wim_info->images);
817 wim_info->images = images;
818 wim_info->num_images++;
819 return &images[wim_info->num_images - 1];
823 clone_windows_info(const struct windows_info *old, struct windows_info *new)
825 if (old->product_name && !(new->product_name = STRDUP(old->product_name)))
826 return WIMLIB_ERR_NOMEM;
827 if (old->edition_id && !(new->edition_id = STRDUP(old->edition_id)))
828 return WIMLIB_ERR_NOMEM;
829 if (old->installation_type && !(new->installation_type =
830 STRDUP(old->installation_type)))
831 return WIMLIB_ERR_NOMEM;
832 if (old->hal && !(new->hal = STRDUP(old->hal)))
833 return WIMLIB_ERR_NOMEM;
834 if (old->product_type && !(new->product_type = STRDUP(old->product_type)))
835 return WIMLIB_ERR_NOMEM;
836 if (old->product_suite && !(new->product_suite = STRDUP(old->product_suite)))
837 return WIMLIB_ERR_NOMEM;
839 if (old->languages) {
840 new->languages = CALLOC(old->num_languages, sizeof(new->languages[0]));
842 return WIMLIB_ERR_NOMEM;
843 new->num_languages = old->num_languages;
844 for (size_t i = 0; i < new->num_languages; i++) {
845 if (!old->languages[i])
847 new->languages[i] = STRDUP(old->languages[i]);
848 if (!new->languages[i])
849 return WIMLIB_ERR_NOMEM;
852 if (old->default_language &&
853 !(new->default_language = STRDUP(old->default_language)))
854 return WIMLIB_ERR_NOMEM;
855 if (old->system_root && !(new->system_root = STRDUP(old->system_root)))
856 return WIMLIB_ERR_NOMEM;
857 if (old->windows_version_exists) {
858 new->windows_version_exists = true;
859 memcpy(&new->windows_version, &old->windows_version,
860 sizeof(old->windows_version));
866 clone_image_info(const struct image_info *old, struct image_info *new)
868 new->dir_count = old->dir_count;
869 new->file_count = old->file_count;
870 new->total_bytes = old->total_bytes;
871 new->hard_link_bytes = old->hard_link_bytes;
872 new->creation_time = old->creation_time;
873 new->last_modification_time = old->last_modification_time;
875 if (!(new->name = STRDUP(old->name)))
876 return WIMLIB_ERR_NOMEM;
878 if (old->description)
879 if (!(new->description = STRDUP(old->description)))
880 return WIMLIB_ERR_NOMEM;
882 if (old->display_name)
883 if (!(new->display_name = STRDUP(old->display_name)))
884 return WIMLIB_ERR_NOMEM;
886 if (old->display_description)
887 if (!(new->display_description = STRDUP(old->display_description)))
888 return WIMLIB_ERR_NOMEM;
891 if (!(new->flags = STRDUP(old->flags)))
892 return WIMLIB_ERR_NOMEM;
894 if (old->windows_info_exists) {
895 new->windows_info_exists = true;
896 return clone_windows_info(&old->windows_info,
902 /* Copies the XML information for an image between WIM files.
904 * @dest_image_name and @dest_image_description are ignored if they are NULL;
905 * otherwise, they are used to override the image name and/or image description
906 * from the XML data in the source WIM file.
908 * On failure, WIMLIB_ERR_NOMEM is returned and no changes are made. Otherwise,
909 * 0 is returned and the WIM information at *new_wim_info_p is modified.
912 xml_export_image(const struct wim_info *old_wim_info,
914 struct wim_info **new_wim_info_p,
915 const utf8char *dest_image_name,
916 const utf8char *dest_image_description)
918 struct wim_info *new_wim_info;
919 struct image_info *image_info;
922 DEBUG("Copying XML data between WIM files for source image %d.", image);
924 wimlib_assert(old_wim_info != NULL);
925 wimlib_assert(image >= 1 && image <= old_wim_info->num_images);
927 if (*new_wim_info_p) {
928 new_wim_info = *new_wim_info_p;
930 new_wim_info = CALLOC(1, sizeof(struct wim_info));
935 image_info = add_image_info_struct(new_wim_info);
939 ret = clone_image_info(&old_wim_info->images[image - 1], image_info);
941 goto err_destroy_image_info;
943 image_info->index = new_wim_info->num_images;
945 if (dest_image_name) {
946 FREE(image_info->name);
947 image_info->name = STRDUP(dest_image_name);
948 if (!image_info->name)
949 goto err_destroy_image_info;
951 if (dest_image_description) {
952 FREE(image_info->description);
953 image_info->description = STRDUP(dest_image_description);
954 if (!image_info->description)
955 goto err_destroy_image_info;
957 *new_wim_info_p = new_wim_info;
959 err_destroy_image_info:
960 destroy_image_info(image_info);
962 if (new_wim_info != *new_wim_info_p)
963 free_wim_info(new_wim_info);
964 return WIMLIB_ERR_NOMEM;
967 /* Removes an image from the XML information. */
969 xml_delete_image(struct wim_info **wim_info_p, int image)
971 struct wim_info *wim_info;
973 DEBUG("Deleting image %d from the XML data.", image);
975 wim_info = *wim_info_p;
977 destroy_image_info(&wim_info->images[image - 1]);
979 memmove(&wim_info->images[image - 1],
980 &wim_info->images[image],
981 (wim_info->num_images - image) * sizeof(struct image_info));
983 if (--wim_info->num_images == 0) {
984 free_wim_info(wim_info);
987 for (int i = image - 1; i < wim_info->num_images; i++)
988 wim_info->images[i].index--;
993 xml_get_max_image_name_len(const WIMStruct *w)
998 for (int i = 0; i < w->wim_info->num_images; i++) {
999 len = strlen(w->wim_info->images[i].name);
1007 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
1009 xml_set_memory_allocator(void *(*malloc_func)(size_t),
1010 void (*free_func)(void *),
1011 void *(*realloc_func)(void *, size_t))
1013 xmlMemSetup(free_func, malloc_func, realloc_func, STRDUP);
1018 calculate_dentry_statistics(struct wim_dentry *dentry, void *arg)
1020 struct image_info *info = arg;
1021 struct wim_lookup_table *lookup_table = info->lookup_table;
1022 const struct wim_inode *inode = dentry->d_inode;
1023 struct wim_lookup_table_entry *lte;
1025 /* Update directory count and file count.
1027 * Each dentry counts as either a file or a directory, but not both.
1028 * The root directory is an exception: it is not counted at all.
1030 * Symbolic links and junction points (and presumably other reparse
1031 * points) count as regular files. This is despite the fact that
1032 * junction points have FILE_ATTRIBUTE_DIRECTORY set.
1034 if (dentry_is_root(dentry))
1037 if (inode_is_directory(inode))
1043 * Update total bytes and hard link bytes.
1045 * Unfortunately there are some inconsistencies/bugs in the way this is
1048 * If there are no alternate data streams in the image, the "total
1049 * bytes" is the sum of the size of the un-named data stream of each
1050 * inode times the link count of that inode. In other words, it would
1051 * be the total number of bytes of regular files you would have if you
1052 * extracted the full image without any hard-links. The "hard link
1053 * bytes" is equal to the "total bytes" minus the size of the un-named
1054 * data stream of each inode. In other words, the "hard link bytes"
1055 * counts the size of the un-named data stream for all the links to each
1056 * inode except the first one.
1058 * Reparse points and directories don't seem to be counted in either the
1059 * total bytes or the hard link bytes.
1061 * And now we get to the most confusing part, the alternate data
1062 * streams. They are not counted in the "total bytes". However, if the
1063 * link count of an inode with alternate data streams is 2 or greater,
1064 * the size of all the alternate data streams is included in the "hard
1065 * link bytes", and this size is multiplied by the link count (NOT one
1066 * less than the link count).
1068 lte = inode_unnamed_lte(inode, info->lookup_table);
1070 info->total_bytes += wim_resource_size(lte);
1071 if (!dentry_is_first_in_inode(dentry))
1072 info->hard_link_bytes += wim_resource_size(lte);
1075 if (inode->i_nlink >= 2 && dentry_is_first_in_inode(dentry)) {
1076 for (unsigned i = 0; i < inode->i_num_ads; i++) {
1077 if (inode->i_ads_entries[i].stream_name_nbytes) {
1078 lte = inode_stream_lte(inode, i + 1, lookup_table);
1080 info->hard_link_bytes += inode->i_nlink *
1081 wim_resource_size(lte);
1090 * Calculate what to put in the <FILECOUNT>, <DIRCOUNT>, <TOTALBYTES>, and
1091 * <HARDLINKBYTES> elements of each <IMAGE>.
1093 * Please note there is no official documentation for exactly how this is done.
1094 * But, see calculate_dentry_statistics().
1097 xml_update_image_info(WIMStruct *w, int image)
1099 struct image_info *image_info;
1100 utf8char *flags_save;
1102 DEBUG("Updating the image info for image %d", image);
1104 image_info = &w->wim_info->images[image - 1];
1106 image_info->file_count = 0;
1107 image_info->dir_count = 0;
1108 image_info->total_bytes = 0;
1109 image_info->hard_link_bytes = 0;
1111 flags_save = image_info->flags;
1112 image_info->lookup_table = w->lookup_table;
1113 for_dentry_in_tree(w->image_metadata[image - 1].root_dentry,
1114 calculate_dentry_statistics,
1116 image_info->flags = flags_save;
1117 image_info->last_modification_time = get_wim_timestamp();
1120 /* Adds an image to the XML information. */
1122 xml_add_image(WIMStruct *w, const utf8char *name)
1124 struct wim_info *wim_info;
1125 struct image_info *image_info;
1127 wimlib_assert(name != NULL);
1129 /* If this is the first image, allocate the struct wim_info. Otherwise
1130 * use the existing struct wim_info. */
1132 wim_info = w->wim_info;
1134 wim_info = CALLOC(1, sizeof(struct wim_info));
1136 return WIMLIB_ERR_NOMEM;
1139 image_info = add_image_info_struct(wim_info);
1141 goto out_free_wim_info;
1143 if (!(image_info->name = STRDUP(name)))
1144 goto out_destroy_image_info;
1146 w->wim_info = wim_info;
1147 image_info->index = wim_info->num_images;
1148 image_info->creation_time = get_wim_timestamp();
1149 xml_update_image_info(w, image_info->index);
1152 out_destroy_image_info:
1153 destroy_image_info(image_info);
1154 wim_info->num_images--;
1156 if (wim_info != w->wim_info)
1158 return WIMLIB_ERR_NOMEM;
1161 /* Prints information about the specified image from struct wim_info structure.
1164 print_image_info(const struct wim_info *wim_info, int image)
1166 const struct image_info *image_info;
1167 const utf8char *desc;
1170 wimlib_assert(image >= 1 && image <= wim_info->num_images);
1172 image_info = &wim_info->images[image - 1];
1174 printf("Index: %d\n", image_info->index);
1175 wimlib_printf("Name: %U\n", image_info->name);
1177 /* Always print the Description: part even if there is no
1179 if (image_info->description)
1180 desc = image_info->description;
1183 wimlib_printf("Description: %U\n", desc);
1185 if (image_info->display_name)
1186 wimlib_printf("Display Name: %U\n",
1187 image_info->display_name);
1189 if (image_info->display_description)
1190 wimlib_printf("Display Description: %U\n",
1191 image_info->display_description);
1193 printf("Directory Count: %"PRIu64"\n", image_info->dir_count);
1194 printf("File Count: %"PRIu64"\n", image_info->file_count);
1195 printf("Total Bytes: %"PRIu64"\n", image_info->total_bytes);
1196 printf("Hard Link Bytes: %"PRIu64"\n", image_info->hard_link_bytes);
1198 wim_timestamp_to_str(image_info->creation_time, buf, sizeof(buf));
1199 printf("Creation Time: %s\n", buf);
1201 wim_timestamp_to_str(image_info->creation_time, buf, sizeof(buf));
1202 printf("Last Modification Time: %s\n", buf);
1203 if (image_info->windows_info_exists)
1204 print_windows_info(&image_info->windows_info);
1205 if (image_info->flags)
1206 printf("Flags: %s\n", image_info->flags);
1211 libxml_global_init()
1214 xmlInitCharEncodingHandlers();
1218 libxml_global_cleanup()
1221 xmlCleanupCharEncodingHandlers();
1225 * Reads the XML data from a WIM file.
1228 read_xml_data(FILE *fp, const struct resource_entry *res_entry,
1229 utf16lechar **xml_data_ret, struct wim_info **info_ret)
1231 utf16lechar *xml_data;
1236 DEBUG("XML data is %"PRIu64" bytes at offset %"PRIu64"",
1237 (u64)res_entry->size, res_entry->offset);
1239 if (resource_is_compressed(res_entry)) {
1240 ERROR("XML data is supposed to be uncompressed");
1241 ret = WIMLIB_ERR_XML;
1245 if (res_entry->size < 2) {
1246 ERROR("XML data must be at least 2 bytes long");
1247 ret = WIMLIB_ERR_XML;
1251 xml_data = MALLOC(res_entry->size + 3);
1253 ret = WIMLIB_ERR_NOMEM;
1257 ret = read_uncompressed_resource(fp, res_entry->offset,
1258 res_entry->size, xml_data);
1260 goto out_free_xml_data;
1262 /* Null-terminate just in case */
1263 ((u8*)xml_data)[res_entry->size] = 0;
1264 ((u8*)xml_data)[res_entry->size + 1] = 0;
1265 ((u8*)xml_data)[res_entry->size + 2] = 0;
1267 DEBUG("Parsing XML using libxml2 to create XML tree");
1269 doc = xmlReadMemory((const char *)xml_data,
1270 res_entry->size, "noname.xml", "UTF-16", 0);
1273 ERROR("Failed to parse XML data");
1274 ret = WIMLIB_ERR_XML;
1275 goto out_free_xml_data;
1278 DEBUG("Constructing WIM information structure from XML tree.");
1280 root = xmlDocGetRootElement(doc);
1282 ERROR("WIM XML data is an empty XML document");
1283 ret = WIMLIB_ERR_XML;
1287 if (!node_is_element(root) || !node_name_is(root, "WIM")) {
1288 ERROR("Expected <WIM> for the root XML element (found <%s>)",
1290 ret = WIMLIB_ERR_XML;
1294 ret = xml_read_wim_info(root, info_ret);
1298 *xml_data_ret = xml_data;
1301 DEBUG("Freeing XML tree.");
1309 #define CHECK_RET ({ if (ret < 0) { \
1310 ERROR("Error writing XML data"); \
1311 ret = WIMLIB_ERR_WRITE; \
1312 goto out_free_text_writer; \
1316 * Writes XML data to a WIM file.
1318 * If @total_bytes is non-zero, it specifies what to write to the TOTALBYTES
1319 * element in the XML data. If zero, TOTALBYTES is given the default value of
1320 * the offset of the XML data.
1323 write_xml_data(const struct wim_info *wim_info, int image, FILE *out,
1324 u64 total_bytes, struct resource_entry *out_res_entry)
1326 xmlCharEncodingHandler *encoding_handler;
1327 xmlOutputBuffer *out_buffer;
1328 xmlTextWriter *writer;
1333 wimlib_assert(image == WIMLIB_ALL_IMAGES ||
1334 (wim_info != NULL && image >= 1 &&
1335 image <= wim_info->num_images));
1337 start_offset = ftello(out);
1338 if (start_offset == -1)
1339 return WIMLIB_ERR_WRITE;
1341 DEBUG("Writing XML data for image %d at offset %"PRIu64,
1342 image, start_offset);
1344 /* 2 bytes endianness marker for UTF-16LE. This is _required_ for WIM
1346 if ((putc(0xff, out)) == EOF || (putc(0xfe, out) == EOF)) {
1347 ERROR_WITH_ERRNO("Error writing XML data");
1348 return WIMLIB_ERR_WRITE;
1351 /* The contents of the <TOTALBYTES> element in the XML data, under the
1352 * <WIM> element (not the <IMAGE> element), is for non-split WIMs the
1353 * size of the WIM file excluding the XML data and integrity table.
1354 * This should be equal to the current position in the output stream,
1355 * since the XML data and integrity table are the last elements of the
1358 * For split WIMs, <TOTALBYTES> takes into account the entire WIM, not
1359 * just the current part. In that case, @total_bytes should be passed
1360 * in to this function. */
1361 if (total_bytes == 0)
1362 total_bytes = start_offset;
1364 /* The encoding of the XML data must be UTF-16LE. */
1365 encoding_handler = xmlGetCharEncodingHandler(XML_CHAR_ENCODING_UTF16LE);
1366 if (!encoding_handler) {
1367 ERROR("Failed to get XML character encoding handler for UTF-16LE");
1368 ret = WIMLIB_ERR_LIBXML_UTF16_HANDLER_NOT_AVAILABLE;
1372 out_buffer = xmlOutputBufferCreateFile(out, encoding_handler);
1374 ERROR("Failed to allocate xmlOutputBuffer");
1375 ret = WIMLIB_ERR_NOMEM;
1379 writer = xmlNewTextWriter(out_buffer);
1381 ERROR("Failed to allocate xmlTextWriter");
1382 ret = WIMLIB_ERR_NOMEM;
1383 goto out_output_buffer_close;
1386 DEBUG("Writing <WIM> element");
1388 ret = xmlTextWriterStartElement(writer, "WIM");
1391 ret = xmlTextWriterWriteFormatElement(writer, "TOTALBYTES", "%"PRIu64,
1395 if (wim_info != NULL) {
1397 if (image == WIMLIB_ALL_IMAGES) {
1399 last = wim_info->num_images;
1404 DEBUG("Writing %d <IMAGE> elements", last - first + 1);
1405 for (int i = first; i <= last; i++) {
1406 ret = xml_write_image_info(writer, &wim_info->images[i - 1]);
1411 ret = xmlTextWriterEndElement(writer);
1414 ret = xmlTextWriterEndDocument(writer);
1417 DEBUG("Ended XML document");
1419 /* Call xmlFreeTextWriter() before ftello() because the former will
1420 * flush the file stream. */
1421 xmlFreeTextWriter(writer);
1424 end_offset = ftello(out);
1425 if (end_offset == -1) {
1426 ret = WIMLIB_ERR_WRITE;
1429 out_res_entry->offset = start_offset;
1430 out_res_entry->size = end_offset - start_offset;
1431 out_res_entry->original_size = end_offset - start_offset;
1432 out_res_entry->flags = WIM_RESHDR_FLAG_METADATA;
1434 out_free_text_writer:
1435 /* xmlFreeTextWriter will free the attached xmlOutputBuffer. */
1436 xmlFreeTextWriter(writer);
1438 out_output_buffer_close:
1439 if (out_buffer != NULL)
1440 xmlOutputBufferClose(out_buffer);
1443 DEBUG("Successfully wrote XML data");
1447 /* Returns the name of the specified image. */
1448 WIMLIBAPI const utf8char *
1449 wimlib_get_image_name(const WIMStruct *w, int image)
1451 if (image < 1 || image > w->hdr.image_count)
1453 return w->wim_info->images[image - 1].name;
1456 /* Returns the description of the specified image. */
1457 WIMLIBAPI const utf8char *
1458 wimlib_get_image_description(const WIMStruct *w, int image)
1460 if (image < 1 || image > w->hdr.image_count)
1462 return w->wim_info->images[image - 1].description;
1465 /* Determines if an image name is already used by some image in the WIM. */
1467 wimlib_image_name_in_use(const WIMStruct *w, const utf8char *name)
1469 if (!name || !*name)
1471 for (int i = 1; i <= w->hdr.image_count; i++)
1472 if (strcmp(w->wim_info->images[i - 1].name, name) == 0)
1477 /* Extracts the raw XML data to a file stream. */
1479 wimlib_extract_xml_data(WIMStruct *w, FILE *fp)
1481 size_t bytes_written;
1484 return WIMLIB_ERR_INVALID_PARAM;
1485 bytes_written = fwrite(w->xml_data, 1, w->hdr.xml_res_entry.size, fp);
1486 if (bytes_written != w->hdr.xml_res_entry.size) {
1487 ERROR_WITH_ERRNO("Failed to extract XML data");
1488 return WIMLIB_ERR_WRITE;
1493 /* Sets the name of an image in the WIM. */
1495 wimlib_set_image_name(WIMStruct *w, int image, const utf8char *name)
1500 DEBUG("Setting the name of image %d to %s", image, name);
1502 if (!name || !*name) {
1503 ERROR("Must specify a non-empty string for the image name");
1504 return WIMLIB_ERR_INVALID_PARAM;
1507 if (image < 1 || image > w->hdr.image_count) {
1508 ERROR("%d is not a valid image", image);
1509 return WIMLIB_ERR_INVALID_IMAGE;
1512 for (i = 1; i <= w->hdr.image_count; i++) {
1515 if (strcmp(w->wim_info->images[i - 1].name, name) == 0) {
1516 ERROR("The name `%U' is already in use in the WIM!",
1518 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
1524 return WIMLIB_ERR_NOMEM;
1526 FREE(w->wim_info->images[image - 1].name);
1527 w->wim_info->images[image - 1].name = p;
1531 /* Sets the description of an image in the WIM. */
1533 wimlib_set_image_descripton(WIMStruct *w, int image,
1534 const utf8char *description)
1538 if (image < 1 || image > w->hdr.image_count) {
1539 ERROR("%d is not a valid image", image);
1540 return WIMLIB_ERR_INVALID_IMAGE;
1543 p = STRDUP(description);
1545 return WIMLIB_ERR_NOMEM;
1549 FREE(w->wim_info->images[image - 1].description);
1550 w->wim_info->images[image - 1].description = p;
1554 /* Set the <FLAGS> element of a WIM image */
1556 wimlib_set_image_flags(WIMStruct *w, int image, const utf8char *flags)
1560 if (image < 1 || image > w->hdr.image_count) {
1561 ERROR("%d is not a valid image", image);
1562 return WIMLIB_ERR_INVALID_IMAGE;
1567 return WIMLIB_ERR_NOMEM;
1571 FREE(w->wim_info->images[image - 1].flags);
1572 w->wim_info->images[image - 1].flags = p;