4 * Deals with the XML information in WIM files. Uses the C library libxml2.
8 * Copyright (C) 2012 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/.
26 #include "wimlib_internal.h"
29 #include "timestamp.h"
30 #include "lookup_table.h"
33 #include <libxml/parser.h>
34 #include <libxml/tree.h>
35 #include <libxml/xmlwriter.h>
37 /* Structures used to form an in-memory representation of the XML data (other
38 * than the raw parse tree from libxml). */
40 struct windows_version {
52 char *installation_type;
57 char *default_language;
60 bool windows_version_exists;
61 struct windows_version windows_version;
71 u64 last_modification_time;
72 bool windows_info_exists;
73 struct windows_info windows_info;
77 char *display_description;
80 struct lookup_table *lookup_table;
85 /* Returns a statically allocated string that is a string representation of the
86 * architecture number. */
87 static const char *get_arch(int arch)
97 /* XXX Are there other arch values? */
99 snprintf(buf, sizeof(buf), "%d (unknown)", arch);
105 /* Iterate through the children of an xmlNode. */
106 #define for_node_child(parent, child) \
107 for (child = parent->children; child != NULL; child = child->next)
109 /* Utility functions for xmlNodes */
110 static inline bool node_is_element(xmlNode *node)
112 return node->type == XML_ELEMENT_NODE;
115 static inline bool node_is_text(xmlNode *node)
117 return node->type == XML_TEXT_NODE;
120 static inline bool node_is_attribute(xmlNode *node)
122 return node->type == XML_ATTRIBUTE_NODE;
125 static inline bool node_name_is(xmlNode *node, const char *name)
127 /* For now, both upper case and lower case element names are accepted. */
128 return strcasecmp((const char *)node->name, name) == 0;
131 /* Finds the text node that is a child of an element node and returns its
132 * content converted to a 64-bit unsigned integer. Returns 0 if no text node is
134 static u64 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. */
144 static u64 node_get_hex_u64(const xmlNode *u64_node)
147 for_node_child(u64_node, child)
148 if (node_is_text(child))
149 return strtoull(child->content, NULL, 16);
153 static int node_get_string(const xmlNode *string_node, char **str)
158 for_node_child(string_node, child) {
159 if (node_is_text(child) && child->content) {
160 p = STRDUP(child->content);
162 ERROR("Out of memory");
163 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. */
174 static u64 node_get_timestamp(const xmlNode *time_node)
179 for_node_child(time_node, child) {
180 if (!node_is_element(child))
182 if (node_name_is(child, "HIGHPART"))
183 high_part = node_get_hex_u64(child);
184 else if (node_name_is(child, "LOWPART"))
185 low_part = node_get_hex_u64(child);
187 return (u64)low_part | ((u64)high_part << 32);
190 /* Used to sort an array of struct image_infos by their image indices. */
191 static int sort_by_index(const void *p1, const void *p2)
193 u64 index_1 = ((struct image_info*)p1)->index;
194 u64 index_2 = ((struct image_info*)p1)->index;
195 if (index_1 < index_2)
197 else if (index_1 > index_2)
204 /* Frees memory allocated inside a struct windows_info structure. */
205 static void destroy_windows_info(struct windows_info *windows_info)
209 FREE(windows_info->product_name);
210 FREE(windows_info->edition_id);
211 FREE(windows_info->installation_type);
212 FREE(windows_info->hal);
213 FREE(windows_info->product_type);
214 FREE(windows_info->product_suite);
215 for (i = 0; i < windows_info->num_languages; i++)
216 FREE(windows_info->languages[i]);
217 FREE(windows_info->languages);
218 FREE(windows_info->default_language);
219 FREE(windows_info->system_root);
222 /* Frees memory allocated inside a struct image_info structure. */
223 static void destroy_image_info(struct image_info *image_info)
225 FREE(image_info->name);
226 FREE(image_info->description);
227 FREE(image_info->flags);
228 FREE(image_info->display_name);
229 FREE(image_info->display_description);
230 destroy_windows_info(&image_info->windows_info);
231 memset(image_info, 0, sizeof(struct image_info));
234 void free_wim_info(struct wim_info *info)
239 for (i = 0; i < info->num_images; i++)
240 destroy_image_info(&info->images[i]);
247 /* Reads the information from a <VERSION> element inside the <WINDOWS> element.
249 static void xml_read_windows_version(const xmlNode *version_node,
250 struct windows_version* windows_version)
253 for_node_child(version_node, child) {
254 if (!node_is_element(child))
256 if (node_name_is(child, "MAJOR"))
257 windows_version->major = node_get_u64(child);
258 else if (node_name_is(child, "MINOR"))
259 windows_version->minor = node_get_u64(child);
260 else if (node_name_is(child, "BUILD"))
261 windows_version->build = node_get_u64(child);
262 else if (node_name_is(child, "SPBUILD"))
263 windows_version->sp_build = node_get_u64(child);
264 else if (node_name_is(child, "SPLEVEL"))
265 windows_version->sp_level = node_get_u64(child);
269 /* Reads the information from a <LANGUAGE> element inside a <WINDOWS> element.
271 static int xml_read_languages(const xmlNode *languages_node,
272 char ***languages_ret,
273 u64 *num_languages_ret,
274 char **default_language_ret)
283 for_node_child(languages_node, child)
284 if (node_is_element(child) && node_name_is(child, "LANGUAGE"))
287 languages = CALLOC(num_languages, sizeof(char*));
289 ERROR("Out of memory");
290 return WIMLIB_ERR_NOMEM;
293 *languages_ret = languages;
294 *num_languages_ret = num_languages;
298 for_node_child(languages_node, child) {
299 if (!node_is_element(child))
301 if (node_name_is(child, "LANGUAGE"))
302 ret = node_get_string(child, &languages[i++]);
303 else if (node_name_is(child, "DEFAULT"))
304 ret = node_get_string(child, default_language_ret);
311 /* Reads the information from a <WINDOWS> element inside an <IMAGE> element. */
312 static int 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);
359 /* Reads the information from an <IMAGE> element. */
360 static int xml_read_image_info(xmlNode *image_node,
361 struct image_info *image_info)
367 index_prop = xmlGetProp(image_node, "INDEX");
370 image_info->index = strtoul(index_prop, &tmp, 10);
373 image_info->index = 0;
377 for_node_child(image_node, child) {
378 if (!node_is_element(child))
380 if (node_name_is(child, "DIRCOUNT"))
381 image_info->dir_count = node_get_u64(child);
382 else if (node_name_is(child, "FILECOUNT"))
383 image_info->file_count = node_get_u64(child);
384 else if (node_name_is(child, "TOTALBYTES"))
385 image_info->total_bytes = node_get_u64(child);
386 else if (node_name_is(child, "HARDLINKBYTES"))
387 image_info->hard_link_bytes = node_get_u64(child);
388 else if (node_name_is(child, "CREATIONTIME"))
389 image_info->creation_time = node_get_timestamp(child);
390 else if (node_name_is(child, "LASTMODIFICATIONTIME"))
391 image_info->last_modification_time = node_get_timestamp(child);
392 else if (node_name_is(child, "WINDOWS")) {
393 DEBUG("Found <WINDOWS> tag");
394 ret = xml_read_windows_info(child,
395 &image_info->windows_info);
396 image_info->windows_info_exists = true;
397 } else if (node_name_is(child, "NAME")) {
398 ret = node_get_string(child, &image_info->name);
399 } else if (node_name_is(child, "DESCRIPTION")) {
400 ret = node_get_string(child, &image_info->description);
401 } else if (node_name_is(child, "FLAGS")) {
402 ret = node_get_string(child, &image_info->flags);
403 } else if (node_name_is(child, "DISPLAYNAME")) {
404 ret = node_get_string(child, &image_info->display_name);
405 } else if (node_name_is(child, "DISPLAYDESCRIPTION")) {
406 ret = node_get_string(child, &image_info->display_description);
411 if (!image_info->name) {
412 WARNING("Image with index %"PRIu64" has no name",
414 image_info->name = MALLOC(1);
415 if (!image_info->name) {
416 ERROR("Out of memory");
417 return WIMLIB_ERR_NOMEM;
419 image_info->name[0] = '\0';
424 /* Reads the information from a <WIM> element, which should be the root element
425 * of the XML tree. */
426 static int xml_read_wim_info(const xmlNode *wim_node,
427 struct wim_info **wim_info_ret)
429 struct wim_info *wim_info;
433 struct image_info *cur_image_info;
435 wim_info = CALLOC(1, sizeof(struct wim_info));
437 ERROR("Out of memory");
438 return WIMLIB_ERR_NOMEM;
441 /* Count how many images there are. */
443 for_node_child(wim_node, child)
444 if (node_is_element(child) && node_name_is(child, "IMAGE"))
450 /* Allocate the array of struct image_infos and fill them in. */
451 wim_info->images = CALLOC(num_images, sizeof(wim_info->images[0]));
452 if (!wim_info->images) {
453 ret = WIMLIB_ERR_NOMEM;
454 ERROR("Out of memory!");
457 wim_info->num_images = num_images;
458 cur_image_info = wim_info->images;
459 for_node_child(wim_node, child) {
460 if (!node_is_element(child))
462 if (node_name_is(child, "IMAGE")) {
463 DEBUG("Found <IMAGE> tag");
464 ret = xml_read_image_info(child, cur_image_info++);
467 } else if (node_name_is(child, "TOTALBYTES")) {
468 wim_info->total_bytes = node_get_u64(child);
472 /* Sort the array of struct image_infos by image index. */
473 qsort(wim_info->images, wim_info->num_images,
474 sizeof(struct image_info), sort_by_index);
476 *wim_info_ret = wim_info;
479 free_wim_info(wim_info);
483 /* Prints the information contained in a struct windows_info structure. */
484 static void print_windows_info(const struct windows_info *windows_info)
487 const struct windows_version *windows_version;
489 printf("Architecture: %s\n", get_arch(windows_info->arch));
490 printf("Product Name: %s\n", windows_info->product_name);
491 printf("Edition ID: %s\n", windows_info->edition_id);
492 printf("Installation Type: %s\n", windows_info->installation_type);
493 if (windows_info->hal)
494 printf("HAL: %s\n", windows_info->hal);
495 printf("Product Type: %s\n", windows_info->product_type);
496 if (windows_info->product_suite)
497 printf("Product Suite: %s\n", windows_info->product_suite);
498 printf("Languages: ");
499 for (i = 0; i < windows_info->num_languages; i++) {
500 fputs(windows_info->languages[i], stdout);
504 printf("Default Language: %s\n", windows_info->default_language);
505 printf("System Root: %s\n", windows_info->system_root);
506 if (windows_info->windows_version_exists) {
507 windows_version = &windows_info->windows_version;
508 printf("Major Version: %"PRIu64"\n",
509 windows_version->major);
510 printf("Minor Version: %"PRIu64"\n",
511 windows_version->minor);
512 printf("Build: %"PRIu64"\n",
513 windows_version->build);
514 printf("Service Pack Build: %"PRIu64"\n",
515 windows_version->sp_build);
516 printf("Service Pack Level: %"PRIu64"\n",
517 windows_version->sp_level);
522 /* Writes the information contained in a struct windows_version structure to the XML
523 * document being constructed in memory. This is the <VERSION> element inside
524 * the <WINDOWS> element. */
525 static int xml_write_windows_version(xmlTextWriter *writer,
526 const struct windows_version *version)
529 rc = xmlTextWriterStartElement(writer, "VERSION");
533 rc = xmlTextWriterWriteFormatElement(writer, "MAJOR", "%"PRIu64,
538 rc = xmlTextWriterWriteFormatElement(writer, "MINOR", "%"PRIu64,
543 rc = xmlTextWriterWriteFormatElement(writer, "BUILD", "%"PRIu64,
548 rc = xmlTextWriterWriteFormatElement(writer, "SPBUILD", "%"PRIu64,
553 rc = xmlTextWriterWriteFormatElement(writer, "SPLEVEL", "%"PRIu64,
558 return xmlTextWriterEndElement(writer); /* </VERSION> */
561 /* Writes the information contained in a struct windows_info structure to the XML
562 * document being constructed in memory. This is the <WINDOWS> element. */
563 static int xml_write_windows_info(xmlTextWriter *writer,
564 const struct windows_info *windows_info)
567 rc = xmlTextWriterStartElement(writer, "WINDOWS");
572 rc = xmlTextWriterWriteFormatElement(writer, "ARCH", "%"PRIu64,
577 if (windows_info->product_name) {
578 rc = xmlTextWriterWriteElement(writer, "PRODUCTNAME",
579 windows_info->product_name);
584 if (windows_info->edition_id) {
585 rc = xmlTextWriterWriteElement(writer, "EDITIONID",
586 windows_info->edition_id);
591 if (windows_info->installation_type) {
592 rc = xmlTextWriterWriteElement(writer, "INSTALLATIONTYPE",
593 windows_info->installation_type);
598 if (windows_info->hal) {
599 rc = xmlTextWriterWriteElement(writer, "HAL",
605 if (windows_info->system_root) {
606 rc = xmlTextWriterWriteElement(writer, "SYSTEMROOT",
607 windows_info->system_root);
612 if (windows_info->product_type) {
613 rc = xmlTextWriterWriteElement(writer, "PRODUCTTYPE",
614 windows_info->product_type);
619 if (windows_info->product_suite) {
620 rc = xmlTextWriterWriteElement(writer, "PRODUCTSUITE",
621 windows_info->product_suite);
626 if (windows_info->num_languages) {
627 rc = xmlTextWriterStartElement(writer, "LANGUAGES");
631 for (int i = 0; i < windows_info->num_languages; i++) {
632 rc = xmlTextWriterWriteElement(writer, "LANGUAGE",
633 windows_info->languages[i]);
637 rc = xmlTextWriterWriteElement(writer, "DEFAULT",
638 windows_info->default_language);
642 rc = xmlTextWriterEndElement(writer); /* </LANGUAGES> */
647 if (windows_info->windows_version_exists) {
648 rc = xml_write_windows_version(writer, &windows_info->windows_version);
653 return xmlTextWriterEndElement(writer); /* </WINDOWS> */
656 /* Writes a time element to the XML document being constructed in memory. */
657 static int xml_write_time(xmlTextWriter *writer, const char *element_name,
661 rc = xmlTextWriterStartElement(writer, element_name);
665 rc = xmlTextWriterWriteFormatElement(writer, "HIGHPART",
666 "0x%"PRIX32, (u32)(time >> 32));
670 rc = xmlTextWriterWriteFormatElement(writer, "LOWPART",
671 "0x%"PRIX32, (u32)time);
675 rc = xmlTextWriterEndElement(writer); /* </@element_name> */
682 /* Writes an <IMAGE> element to the XML document. */
683 static int xml_write_image_info(xmlTextWriter *writer,
684 const struct image_info *image_info)
687 rc = xmlTextWriterStartElement(writer, "IMAGE");
691 rc = xmlTextWriterWriteFormatAttribute(writer, "INDEX", "%"PRIu64,
696 rc = xmlTextWriterWriteFormatElement(writer, "DIRCOUNT", "%"PRIu64,
697 image_info->dir_count);
701 rc = xmlTextWriterWriteFormatElement(writer, "FILECOUNT", "%"PRIu64,
702 image_info->file_count);
706 rc = xmlTextWriterWriteFormatElement(writer, "TOTALBYTES", "%"PRIu64,
707 image_info->total_bytes);
711 rc = xmlTextWriterWriteFormatElement(writer, "HARDLINKBYTES", "%"PRIu64,
712 image_info->hard_link_bytes);
716 rc = xml_write_time(writer, "CREATIONTIME",
717 image_info->creation_time);
721 rc = xml_write_time(writer, "LASTMODIFICATIONTIME",
722 image_info->last_modification_time);
726 if (image_info->windows_info_exists) {
727 rc = xml_write_windows_info(writer, &image_info->windows_info);
731 DEBUG("<WINDOWS> tag does not exist.");
734 if (image_info->name) {
735 rc = xmlTextWriterWriteElement(writer, "NAME", image_info->name);
739 if (image_info->description) {
740 rc = xmlTextWriterWriteElement(writer, "DESCRIPTION",
741 image_info->description);
745 if (image_info->display_name) {
746 rc = xmlTextWriterWriteElement(writer, "DISPLAYNAME",
747 image_info->display_name);
751 if (image_info->display_description) {
752 rc = xmlTextWriterWriteElement(writer, "DISPLAYDESCRIPTION",
753 image_info->display_description);
758 if (image_info->flags) {
759 rc = xmlTextWriterWriteElement(writer, "FLAGS",
765 return xmlTextWriterEndElement(writer); /* </IMAGE> */
770 /* Makes space for another image in the XML information and return a pointer to
772 static struct image_info *add_image_info_struct(struct wim_info *wim_info)
774 struct image_info *images;
776 images = CALLOC(wim_info->num_images + 1, sizeof(struct image_info));
779 memcpy(images, wim_info->images,
780 wim_info->num_images * sizeof(struct image_info));
781 FREE(wim_info->images);
782 wim_info->images = images;
783 wim_info->num_images++;
784 return &images[wim_info->num_images - 1];
787 static int clone_windows_info(const struct windows_info *old,
788 struct windows_info *new)
792 if (old->product_name && !(new->product_name = STRDUP(old->product_name)))
793 return WIMLIB_ERR_NOMEM;
794 if (old->edition_id && !(new->edition_id = STRDUP(old->edition_id)))
795 return WIMLIB_ERR_NOMEM;
796 if (old->installation_type && !(new->installation_type =
797 STRDUP(old->installation_type)))
798 return WIMLIB_ERR_NOMEM;
799 if (old->hal && !(new->hal = STRDUP(old->hal)))
800 return WIMLIB_ERR_NOMEM;
801 if (old->product_type && !(new->product_type = STRDUP(old->product_type)))
802 return WIMLIB_ERR_NOMEM;
803 if (old->product_suite && !(new->product_suite = STRDUP(old->product_suite)))
804 return WIMLIB_ERR_NOMEM;
806 if (old->languages) {
807 new->languages = CALLOC(old->num_languages, sizeof(char*));
809 return WIMLIB_ERR_NOMEM;
810 new->num_languages = old->num_languages;
811 for (i = 0; i < new->num_languages; i++) {
812 if (!old->languages[i])
814 new->languages[i] = STRDUP(old->languages[i]);
815 if (!new->languages[i])
816 return WIMLIB_ERR_NOMEM;
819 if (old->default_language &&
820 !(new->default_language = STRDUP(old->default_language)))
821 return WIMLIB_ERR_NOMEM;
822 if (old->system_root && !(new->system_root = STRDUP(old->system_root)))
823 return WIMLIB_ERR_NOMEM;
824 if (old->windows_version_exists) {
825 new->windows_version_exists = true;
826 memcpy(&new->windows_version, &old->windows_version,
827 sizeof(old->windows_version));
832 static int clone_image_info(const struct image_info *old, struct image_info *new)
834 new->dir_count = old->dir_count;
835 new->file_count = old->file_count;
836 new->total_bytes = old->total_bytes;
837 new->hard_link_bytes = old->hard_link_bytes;
838 new->creation_time = old->creation_time;
839 new->last_modification_time = old->last_modification_time;
841 if (!(new->name = STRDUP(old->name)))
842 return WIMLIB_ERR_NOMEM;
844 if (old->description)
845 if (!(new->description = STRDUP(old->description)))
846 return WIMLIB_ERR_NOMEM;
848 if (old->display_name)
849 if (!(new->display_name = STRDUP(old->display_name)))
850 return WIMLIB_ERR_NOMEM;
852 if (old->display_description)
853 if (!(new->display_description = STRDUP(old->display_description)))
854 return WIMLIB_ERR_NOMEM;
857 if (!(new->flags = STRDUP(old->flags)))
858 return WIMLIB_ERR_NOMEM;
860 if (old->windows_info_exists) {
861 new->windows_info_exists = true;
862 return clone_windows_info(&old->windows_info,
868 /* Copies the XML information for an image between WIM files.
870 * @dest_image_name and @dest_image_description are ignored if they are NULL;
871 * otherwise, they are used to override the image name and/or image description
872 * from the XML data in the source WIM file.
874 * On failure, WIMLIB_ERR_NOMEM is returned and no changes are made. Otherwise,
875 * 0 is returned and the WIM information at *new_wim_info_p is modified.
877 int xml_export_image(const struct wim_info *old_wim_info,
879 struct wim_info **new_wim_info_p,
880 const char *dest_image_name,
881 const char *dest_image_description)
883 struct wim_info *new_wim_info;
884 struct image_info *image_info;
887 DEBUG("Copying XML data between WIM files for source image %d.", image);
889 wimlib_assert(image >= 1 && image <= old_wim_info->num_images);
891 if (*new_wim_info_p) {
892 new_wim_info = *new_wim_info_p;
894 new_wim_info = CALLOC(1, sizeof(struct wim_info));
899 image_info = add_image_info_struct(new_wim_info);
903 ret = clone_image_info(&old_wim_info->images[image - 1], image_info);
907 image_info->index = new_wim_info->num_images;
909 if (dest_image_name) {
910 FREE(image_info->name);
911 image_info->name = STRDUP(dest_image_name);
912 if (!image_info->name)
915 if (dest_image_description) {
916 FREE(image_info->description);
917 image_info->description = STRDUP(dest_image_description);
918 if (!image_info->description)
921 *new_wim_info_p = new_wim_info;
924 ERROR("Out of memory");
925 free_wim_info(new_wim_info);
926 return WIMLIB_ERR_NOMEM;
929 /* Removes an image from the XML information. */
930 void xml_delete_image(struct wim_info **wim_info_p, int image)
932 struct wim_info *wim_info;
935 DEBUG("Deleting image %d from the XML data.", image);
937 wim_info = *wim_info_p;
939 wimlib_assert(wim_info);
940 wimlib_assert(image >= 1 && image <= wim_info->num_images);
942 destroy_image_info(&wim_info->images[image - 1]);
944 for (i = image - 1; i < wim_info->num_images - 1; i++) {
945 memcpy(&wim_info->images[i], &wim_info->images[i + 1],
946 sizeof(struct image_info));
947 wim_info->images[i].index--;
950 if (--wim_info->num_images == 0) {
951 free_wim_info(wim_info);
956 size_t xml_get_max_image_name_len(const WIMStruct *w)
960 uint num_images = w->wim_info->num_images;
961 for (i = 0; i < num_images; i++)
962 len = max(len, strlen(w->wim_info->images[i].name));
966 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
967 void xml_set_memory_allocator(void *(*malloc_func)(size_t),
968 void (*free_func)(void *),
969 void *(*realloc_func)(void *, size_t))
971 xmlMemSetup(free_func, malloc_func, realloc_func, STRDUP);
975 static int calculate_dentry_statistics(struct dentry *dentry, void *arg)
977 struct image_info *info = arg;
978 struct lookup_table *lookup_table = info->lookup_table;
979 const struct inode *inode = dentry->d_inode;
980 struct lookup_table_entry *lte;
982 /* Update directory count and file count.
984 * Each dentry counts as either a file or a directory, but not both.
985 * The root directory is an exception: it is not counted at all.
987 * Symbolic links and junction points (and presumably other reparse
988 * points) count as regular files. This is despite the fact that
989 * junction points have FILE_ATTRIBUTE_DIRECTORY set.
991 if (dentry_is_root(dentry))
994 if (inode_is_directory(inode))
1000 * Update total bytes and hard link bytes.
1002 * Unfortunately there are some inconsistencies/bugs in the way this is
1005 * If there are no alternate data streams in the image, the "total
1006 * bytes" is the sum of the size of the un-named data stream of each
1007 * inode times the link count of that inode. In other words, it would
1008 * be the total number of bytes of regular files you would have if you
1009 * extracted the full image without any hard-links. The "hard link
1010 * bytes" is equal to the "total bytes" minus the size of the un-named
1011 * data stream of each inode. In other words, the "hard link bytes"
1012 * counts the size of the un-named data stream for all the links to each
1013 * inode except the first one.
1015 * Reparse points and directories don't seem to be counted in either the
1016 * total bytes or the hard link bytes.
1018 * And now we get to the most confusing part, the alternate data
1019 * streams. They are not counted in the "total bytes". However, if the
1020 * link count of an inode with alternate data streams is 2 or greater,
1021 * the size of all the alternate data streams is included in the "hard
1022 * link bytes", and this size is multiplied by the link count (NOT one
1023 * less than the link count).
1025 lte = inode_unnamed_lte(inode, info->lookup_table);
1027 info->total_bytes += wim_resource_size(lte);
1028 if (!dentry_is_first_in_inode(dentry))
1029 info->hard_link_bytes += wim_resource_size(lte);
1032 if (inode->link_count >= 2 && dentry_is_first_in_inode(dentry)) {
1033 for (unsigned i = 0; i < inode->num_ads; i++) {
1034 if (inode->ads_entries[i].stream_name_len) {
1035 lte = inode_stream_lte(inode, i + 1, lookup_table);
1037 info->hard_link_bytes += inode->link_count *
1038 wim_resource_size(lte);
1046 void xml_update_image_info(WIMStruct *w, int image)
1048 struct image_info *image_info;
1049 struct dentry *root;
1052 DEBUG("Updating the image info for image %d", image);
1054 image_info = &w->wim_info->images[image - 1];
1056 image_info->file_count = 0;
1057 image_info->dir_count = 0;
1058 image_info->total_bytes = 0;
1059 image_info->hard_link_bytes = 0;
1061 flags_save = image_info->flags;
1062 image_info->lookup_table = w->lookup_table;
1064 for_dentry_in_tree(w->image_metadata[image - 1].root_dentry,
1065 calculate_dentry_statistics,
1068 image_info->lookup_table = NULL;
1069 image_info->flags = flags_save;
1070 image_info->last_modification_time = get_wim_timestamp();
1073 /* Adds an image to the XML information. */
1074 int xml_add_image(WIMStruct *w, const char *name)
1076 struct wim_info *wim_info;
1077 struct image_info *image_info;
1079 wimlib_assert(name);
1081 DEBUG("Adding image: name = %s", name);
1083 /* If this is the first image, allocate the struct wim_info. Otherwise
1084 * use the existing struct wim_info. */
1086 wim_info = w->wim_info;
1088 DEBUG("Allocing struct wim_info with 1 image");
1089 wim_info = CALLOC(1, sizeof(struct wim_info));
1091 ERROR("Could not allocate WIM information struct--- "
1093 return WIMLIB_ERR_NOMEM;
1097 image_info = add_image_info_struct(wim_info);
1099 goto out_free_wim_info;
1101 if (!(image_info->name = STRDUP(name)))
1102 goto out_destroy_image_info;
1104 w->wim_info = wim_info;
1105 image_info->index = wim_info->num_images;
1106 image_info->creation_time = get_wim_timestamp();
1107 xml_update_image_info(w, image_info->index);
1110 out_destroy_image_info:
1111 destroy_image_info(image_info);
1112 wim_info->num_images--;
1114 if (wim_info != w->wim_info)
1116 ERROR("Out of memory");
1117 return WIMLIB_ERR_NOMEM;
1120 /* Prints information about the specified image from struct wim_info structure.
1121 * @image may be WIM_ALL_IMAGES. */
1122 void print_image_info(const struct wim_info *wim_info, int image)
1125 const struct image_info *image_info;
1129 if (image == WIM_ALL_IMAGES) {
1130 for (i = 1; i <= wim_info->num_images; i++)
1131 print_image_info(wim_info, i);
1136 image_info = &wim_info->images[image - 1];
1138 printf("Index: %"PRIu64"\n",
1140 printf("Name: %s\n",
1143 /* Always print the Description: part even if there is no
1145 if (image_info->description)
1146 desc = image_info->description;
1149 printf("Description: %s\n", desc);
1151 if (image_info->display_name)
1152 printf("Display Name: %s\n",
1153 image_info->display_name);
1155 if (image_info->display_description)
1156 printf("Display Description: %s\n",
1157 image_info->display_description);
1159 printf("Directory Count: %"PRIu64"\n",
1160 image_info->dir_count);
1161 printf("File Count: %"PRIu64"\n",
1162 image_info->file_count);
1163 printf("Total Bytes: %"PRIu64"\n",
1164 image_info->total_bytes);
1165 printf("Hard Link Bytes: %"PRIu64"\n",
1166 image_info->hard_link_bytes);
1168 time = wim_timestamp_to_unix(image_info->creation_time);
1169 p = asctime(gmtime(&time));
1170 *(strrchr(p, '\n')) = '\0';
1172 printf("Creation Time: %s UTC\n", p);
1174 time = wim_timestamp_to_unix(image_info->last_modification_time);
1175 p = asctime(gmtime(&time));
1176 *(strrchr(p, '\n')) = '\0';
1178 printf("Last Modification Time: %s UTC\n", p);
1179 if (image_info->windows_info_exists)
1180 print_windows_info(&image_info->windows_info);
1181 if (image_info->flags)
1182 printf("Flags: %s\n", image_info->flags);
1188 * Reads the XML data from a WIM file.
1190 int read_xml_data(FILE *fp, const struct resource_entry *res, u8 **xml_data_ret,
1191 struct wim_info **info_ret)
1198 DEBUG("XML data is %"PRIu64" bytes at offset %"PRIu64"",
1199 (u64)res->size, res->offset);
1201 if (resource_is_compressed(res)) {
1202 ERROR("XML data is supposed to be uncompressed");
1203 ret = WIMLIB_ERR_XML;
1204 goto out_cleanup_parser;
1206 if (res->size < 2) {
1207 ERROR("XML data must be at least 2 bytes");
1208 ret = WIMLIB_ERR_XML;
1209 goto out_cleanup_parser;
1212 xml_data = MALLOC(res->size + 2);
1214 ret = WIMLIB_ERR_NOMEM;
1215 goto out_cleanup_parser;
1217 ret = read_uncompressed_resource(fp, res->offset, res->size, xml_data);
1219 goto out_free_xml_data;
1221 xml_data[res->size] = 0;
1222 xml_data[res->size + 1] = 0;
1224 DEBUG("Parsing XML using libxml2 to create XML tree.");
1226 doc = xmlReadMemory(xml_data, res->size, "noname.xml", "UTF-16", 0);
1230 ERROR("Failed to parse XML data");
1231 ret = WIMLIB_ERR_XML;
1232 goto out_free_xml_data;
1235 DEBUG("Constructing WIM information structure from XML tree.");
1237 root = xmlDocGetRootElement(doc);
1239 ERROR("Empty XML document");
1240 ret = WIMLIB_ERR_XML;
1244 if (!node_is_element(root) || !node_name_is(root, "WIM")) {
1245 ERROR("Expected <WIM> for the root XML element (found <%s>)",
1247 ret = WIMLIB_ERR_XML;
1251 ret = xml_read_wim_info(root, info_ret);
1255 DEBUG("Freeing XML tree.");
1259 *xml_data_ret = xml_data;
1270 #define CHECK_RET ({ if (ret < 0) { \
1271 ERROR("Error writing XML data"); \
1272 ret = WIMLIB_ERR_WRITE; \
1277 * Writes XML data to a WIM file.
1279 * If @total_bytes is non-zero, it specifies what to write to the TOTALBYTES
1280 * element in the XML data. If zero, TOTALBYTES is given the default value of
1281 * the offset of the XML data.
1283 int write_xml_data(const struct wim_info *wim_info, int image, FILE *out,
1287 xmlTextWriter *writer;
1292 const xmlChar *content;
1295 size_t bytes_written;
1297 wimlib_assert(image == WIM_ALL_IMAGES ||
1298 (wim_info != NULL && image >= 1 &&
1299 image <= wim_info->num_images));
1301 /* The contents of the <TOTALBYTES> element in the XML data, under the
1302 * <WIM> element not the <IMAGE> element, is (for non-spit WIMs) the
1303 * size of the WIM file excluding the XML data and integrity table,
1304 * which is the current offset, since the XML data goes at the end of
1305 * the WIM file before the integrity table. */
1306 if (total_bytes == 0) {
1307 total_bytes = ftello(out);
1308 if (total_bytes == (u64)-1)
1309 return WIMLIB_ERR_WRITE;
1312 DEBUG("Creating XML buffer and text writer.");
1313 buf = xmlBufferCreate();
1315 ERROR("Failed to allocate XML buffer");
1316 ret = WIMLIB_ERR_NOMEM;
1319 writer = xmlNewTextWriterMemory(buf, 0);
1321 ERROR("Failed to allocate XML writer");
1322 ret = WIMLIB_ERR_NOMEM;
1327 /* M$'s WIM files do not have XML declarations, so do not write one.
1328 * I'm not sure how we can force the document to be written in UTF-16
1329 * without calling xmlTextWriterStartDocument(), though, so currently it
1330 * is composed in a buffer UTF-8, then converted to UTF-16. */
1332 ret = xmlTextWriterStartDocument(writer, NULL, "UTF-16", NULL);
1336 DEBUG("Writing <WIM> element");
1337 ret = xmlTextWriterStartElement(writer, "WIM");
1340 ret = xmlTextWriterWriteFormatElement(writer, "TOTALBYTES", "%"PRIu64,
1345 num_images = wim_info->num_images;
1348 DEBUG("Writing %u <IMAGE> elements", num_images);
1350 for (i = 1; i <= num_images; i++) {
1351 if (image != WIM_ALL_IMAGES && i != image)
1353 DEBUG("Writing <IMAGE> element for image %d", i);
1354 ret = xml_write_image_info(writer, &wim_info->images[i - 1]);
1358 ret = xmlTextWriterEndElement(writer);
1361 ret = xmlTextWriterEndDocument(writer);
1364 DEBUG("Done composing XML document. Now converting to UTF-16 and "
1365 "writing it to the output file.");
1367 content = xmlBufferContent(buf);
1368 len = xmlBufferLength(buf);
1370 utf16_str = utf8_to_utf16(content, len, &utf16_len);
1372 ret = WIMLIB_ERR_NOMEM;
1376 if ((putc(0xff, out)) == EOF || (putc(0xfe, out) == EOF) ||
1377 ((bytes_written = fwrite(utf16_str, 1, utf16_len, out))
1379 ERROR_WITH_ERRNO("Error writing XML data");
1380 ret = WIMLIB_ERR_WRITE;
1384 DEBUG("Cleaning up.");
1390 xmlFreeTextWriter(writer);
1397 /* Returns the name of the specified image. */
1398 WIMLIBAPI const char *wimlib_get_image_name(const WIMStruct *w, int image)
1400 DEBUG("Getting the name of image %d", image);
1401 if (image < 1 || image > w->hdr.image_count)
1404 return w->wim_info->images[image - 1].name;
1407 /* Returns the description of the specified image. */
1408 WIMLIBAPI const char *wimlib_get_image_description(const WIMStruct *w,
1411 DEBUG("Getting the description of image %d", image);
1412 if (image < 1 || image > w->hdr.image_count)
1415 return w->wim_info->images[image - 1].description;
1418 /* Determines if an image name is already used by some image in the WIM. */
1419 WIMLIBAPI bool wimlib_image_name_in_use(const WIMStruct *w, const char *name)
1423 DEBUG("Checking to see if the image name `%s' is already in use", name);
1424 if (!name || !w->wim_info)
1426 for (i = 1; i <= w->wim_info->num_images; i++)
1427 if (strcmp(w->wim_info->images[i - 1].name, name) == 0)
1433 WIMLIBAPI int wimlib_extract_xml_data(WIMStruct *w, FILE *fp)
1435 DEBUG("Extracting the XML data.");
1436 if (fwrite(w->xml_data, 1, w->hdr.xml_res_entry.size, fp) !=
1437 w->hdr.xml_res_entry.size) {
1438 ERROR_WITH_ERRNO("Failed to extract XML data");
1439 return WIMLIB_ERR_WRITE;
1444 /* Sets the name of an image in the WIM. */
1445 WIMLIBAPI int wimlib_set_image_name(WIMStruct *w, int image, const char *name)
1450 DEBUG("Setting the name of image %d to %s", image, name);
1453 return WIMLIB_ERR_INVALID_PARAM;
1455 if (!name || !*name) {
1456 ERROR("Must specify a non-empty string for the image name");
1457 return WIMLIB_ERR_INVALID_PARAM;
1459 if (image < 1 || image > w->hdr.image_count) {
1460 ERROR("%d is not a valid image", image);
1461 return WIMLIB_ERR_INVALID_IMAGE;
1464 for (i = 1; i <= w->hdr.image_count; i++) {
1467 if (strcmp(w->wim_info->images[i - 1].name, name) == 0) {
1468 ERROR("The name `%s' is already used for image %d",
1470 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
1476 ERROR("Out of memory");
1477 return WIMLIB_ERR_NOMEM;
1479 FREE(w->wim_info->images[image - 1].name);
1480 w->wim_info->images[image - 1].name = p;
1484 /* Sets the description of an image in the WIM. */
1485 WIMLIBAPI int wimlib_set_image_descripton(WIMStruct *w, int image,
1486 const char *description)
1490 DEBUG("Setting the description of image %d to %s", image, description);
1493 return WIMLIB_ERR_INVALID_PARAM;
1495 if (image < 1 || image > w->hdr.image_count) {
1496 ERROR("%d is not a valid image", image);
1497 return WIMLIB_ERR_INVALID_IMAGE;
1500 p = STRDUP(description);
1502 ERROR("Out of memory");
1503 return WIMLIB_ERR_NOMEM;
1508 FREE(w->wim_info->images[image - 1].description);
1509 w->wim_info->images[image - 1].description = p;
1513 WIMLIBAPI int wimlib_set_image_flags(WIMStruct *w, int image,
1518 DEBUG("Setting the flags of image %d to %s", image, flags);
1520 if (image < 1 || image > w->hdr.image_count) {
1521 ERROR("%d is not a valid image", image);
1522 return WIMLIB_ERR_INVALID_IMAGE;
1527 ERROR("Out of memory");
1528 return WIMLIB_ERR_NOMEM;
1533 FREE(w->wim_info->images[image - 1].flags);
1534 w->wim_info->images[image - 1].flags = p;