- /* Allocate the array of struct image_infos and fill them in. */
- wim_info->images = CALLOC(num_images, sizeof(wim_info->images[0]));
- if (!wim_info->images) {
- ret = WIMLIB_ERR_NOMEM;
- ERROR("Out of memory!");
- goto err;
- }
- wim_info->num_images = num_images;
- cur_image_info = wim_info->images;
- for_node_child(wim_node, child) {
- if (!node_is_element(child))
- continue;
- if (node_name_is(child, "IMAGE")) {
- DEBUG("Found <IMAGE> tag");
- ret = xml_read_image_info(child, cur_image_info++);
- if (ret != 0)
- goto err;
- } else if (node_name_is(child, "TOTALBYTES")) {
- wim_info->total_bytes = node_get_u64(child);
+ /* Sort the array of image info by image index. */
+ qsort(wim_info->images, num_images,
+ sizeof(struct image_info), sort_by_index);
+
+ /* Make sure the image indices make sense */
+ for (i = 0; i < num_images; i++) {
+ if (wim_info->images[i].index != i + 1) {
+ ERROR("WIM images are not indexed [1...%d] "
+ "in XML data as expected",
+ num_images);
+ return WIMLIB_ERR_IMAGE_COUNT;
+ }