+ int start_src_image;
+ int end_src_image;
+ int orig_dest_image_count;
+ int image;
+ bool all_images = (src_image == WIMLIB_ALL_IMAGES);
+
+ /* Check for sane parameters. */
+ if (export_flags & ~(WIMLIB_EXPORT_FLAG_BOOT |
+ WIMLIB_EXPORT_FLAG_NO_NAMES |
+ WIMLIB_EXPORT_FLAG_NO_DESCRIPTIONS |
+ WIMLIB_EXPORT_FLAG_GIFT |
+ WIMLIB_EXPORT_FLAG_WIMBOOT))
+ return WIMLIB_ERR_INVALID_PARAM;
+
+ if (!src_wim || !dest_wim)
+ return WIMLIB_ERR_INVALID_PARAM;
+
+ if (!wim_has_metadata(src_wim) || !wim_has_metadata(dest_wim))
+ return WIMLIB_ERR_METADATA_NOT_FOUND;
+
+ if (all_images) {
+ /* Multi-image export. */
+ if ((!(export_flags & WIMLIB_EXPORT_FLAG_NO_NAMES) &&
+ dest_name) ||
+ (!(export_flags & WIMLIB_EXPORT_FLAG_NO_DESCRIPTIONS) &&
+ dest_description))
+ {
+ ERROR("Image name and description must be "
+ "left NULL for multi-image export");
+ return WIMLIB_ERR_INVALID_PARAM;
+ }
+ start_src_image = 1;
+ end_src_image = src_wim->hdr.image_count;
+ } else {
+ start_src_image = src_image;
+ end_src_image = src_image;
+ }
+ orig_dest_image_count = dest_wim->hdr.image_count;
+
+ /* We don't yet support having a single WIMStruct contain duplicate
+ * 'image_metadata' structures, so we must forbid this from happening.
+ * A duplication is possible if 'src_wim == dest_wim', if the same image
+ * is exported to the same destination WIMStruct multiple times, or if
+ * an image is exported in an A => B => A manner. */
+ for (src_image = start_src_image;
+ src_image <= end_src_image; src_image++)
+ {
+ const struct wim_image_metadata *src_imd =
+ src_wim->image_metadata[src_image - 1];
+ for (int i = 0; i < dest_wim->hdr.image_count; i++)
+ if (dest_wim->image_metadata[i] == src_imd)
+ return WIMLIB_ERR_DUPLICATE_EXPORTED_IMAGE;