+#include "wimlib/apply.h"
+#include "wimlib/assert.h"
+#include "wimlib/blob_table.h"
+#include "wimlib/dentry.h"
+#include "wimlib/encoding.h"
+#include "wimlib/endianness.h"
+#include "wimlib/error.h"
+#include "wimlib/metadata.h"
+#include "wimlib/pathlist.h"
+#include "wimlib/paths.h"
+#include "wimlib/pattern.h"
+#include "wimlib/reparse.h"
+#include "wimlib/resource.h"
+#include "wimlib/security.h"
+#include "wimlib/unix_data.h"
+#include "wimlib/wim.h"
+#include "wimlib/win32.h" /* for realpath() equivalent */
+#include "wimlib/xml.h"
+
+#define WIMLIB_EXTRACT_FLAG_FROM_PIPE 0x80000000
+#define WIMLIB_EXTRACT_FLAG_IMAGEMODE 0x40000000
+
+/* Keep in sync with wimlib.h */
+#define WIMLIB_EXTRACT_MASK_PUBLIC \
+ (WIMLIB_EXTRACT_FLAG_NTFS | \
+ WIMLIB_EXTRACT_FLAG_UNIX_DATA | \
+ WIMLIB_EXTRACT_FLAG_NO_ACLS | \
+ WIMLIB_EXTRACT_FLAG_STRICT_ACLS | \
+ WIMLIB_EXTRACT_FLAG_RPFIX | \
+ WIMLIB_EXTRACT_FLAG_NORPFIX | \
+ WIMLIB_EXTRACT_FLAG_TO_STDOUT | \
+ WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES | \
+ WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS | \
+ WIMLIB_EXTRACT_FLAG_STRICT_TIMESTAMPS | \
+ WIMLIB_EXTRACT_FLAG_STRICT_SHORT_NAMES | \
+ WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS | \
+ WIMLIB_EXTRACT_FLAG_GLOB_PATHS | \
+ WIMLIB_EXTRACT_FLAG_STRICT_GLOB | \
+ WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES | \
+ WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE | \
+ WIMLIB_EXTRACT_FLAG_WIMBOOT | \
+ WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS4K | \
+ WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS8K | \
+ WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS16K | \
+ WIMLIB_EXTRACT_FLAG_COMPACT_LZX \
+ )
+
+/* Send WIMLIB_PROGRESS_MSG_EXTRACT_FILE_STRUCTURE or
+ * WIMLIB_PROGRESS_MSG_EXTRACT_METADATA. */
+int
+do_file_extract_progress(struct apply_ctx *ctx, enum wimlib_progress_msg msg)
+{
+ ctx->count_until_file_progress = 500; /* Arbitrary value to limit calls */
+ return extract_progress(ctx, msg);
+}
+
+static int
+start_file_phase(struct apply_ctx *ctx, u64 end_file_count, enum wimlib_progress_msg msg)
+{
+ ctx->progress.extract.current_file_count = 0;
+ ctx->progress.extract.end_file_count = end_file_count;
+ return do_file_extract_progress(ctx, msg);
+}
+
+int
+start_file_structure_phase(struct apply_ctx *ctx, u64 end_file_count)
+{
+ return start_file_phase(ctx, end_file_count, WIMLIB_PROGRESS_MSG_EXTRACT_FILE_STRUCTURE);
+}
+
+int
+start_file_metadata_phase(struct apply_ctx *ctx, u64 end_file_count)
+{
+ return start_file_phase(ctx, end_file_count, WIMLIB_PROGRESS_MSG_EXTRACT_METADATA);
+}
+
+static int
+end_file_phase(struct apply_ctx *ctx, enum wimlib_progress_msg msg)
+{
+ ctx->progress.extract.current_file_count = ctx->progress.extract.end_file_count;
+ return do_file_extract_progress(ctx, msg);
+}
+
+int
+end_file_structure_phase(struct apply_ctx *ctx)
+{
+ return end_file_phase(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_FILE_STRUCTURE);
+}
+
+int
+end_file_metadata_phase(struct apply_ctx *ctx)
+{
+ return end_file_phase(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_METADATA);
+}
+
+#define PWM_FOUND_WIM_HDR (-1)
+
+/* Read the header for a blob in a pipable WIM. If @pwm_hdr_ret is not NULL,
+ * also look for a pipable WIM header and return PWM_FOUND_WIM_HDR if found. */
+static int
+read_pwm_blob_header(WIMStruct *pwm, u8 hash_ret[SHA1_HASH_SIZE],
+ struct wim_reshdr *reshdr_ret,
+ struct wim_header_disk *pwm_hdr_ret)
+{
+ int ret;
+ struct pwm_blob_hdr blob_hdr;
+ u64 magic;
+
+ ret = full_read(&pwm->in_fd, &blob_hdr, sizeof(blob_hdr));
+ if (unlikely(ret))
+ goto read_error;
+
+ magic = le64_to_cpu(blob_hdr.magic);
+
+ if (magic == PWM_MAGIC && pwm_hdr_ret != NULL) {
+ memcpy(pwm_hdr_ret, &blob_hdr, sizeof(blob_hdr));
+ ret = full_read(&pwm->in_fd,
+ (u8 *)pwm_hdr_ret + sizeof(blob_hdr),
+ sizeof(*pwm_hdr_ret) - sizeof(blob_hdr));
+ if (unlikely(ret))
+ goto read_error;
+ return PWM_FOUND_WIM_HDR;
+ }
+
+ if (unlikely(magic != PWM_BLOB_MAGIC)) {
+ ERROR("Data read on pipe is invalid (expected blob header)");
+ return WIMLIB_ERR_INVALID_PIPABLE_WIM;
+ }
+
+ copy_hash(hash_ret, blob_hdr.hash);
+
+ reshdr_ret->size_in_wim = 0; /* Not available */
+ reshdr_ret->flags = le32_to_cpu(blob_hdr.flags);
+ reshdr_ret->offset_in_wim = pwm->in_fd.offset;
+ reshdr_ret->uncompressed_size = le64_to_cpu(blob_hdr.uncompressed_size);
+
+ if (unlikely(reshdr_ret->uncompressed_size == 0)) {
+ ERROR("Data read on pipe is invalid (resource is of 0 size)");
+ return WIMLIB_ERR_INVALID_PIPABLE_WIM;
+ }
+
+ return 0;
+
+read_error:
+ if (ret == WIMLIB_ERR_UNEXPECTED_END_OF_FILE)
+ ERROR("The pipe ended before all needed data was sent!");
+ else
+ ERROR_WITH_ERRNO("Error reading pipable WIM from pipe");
+ return ret;
+}
+
+static int
+read_blobs_from_pipe(struct apply_ctx *ctx, const struct read_blob_callbacks *cbs)
+{
+ int ret;
+ u8 hash[SHA1_HASH_SIZE];
+ struct wim_reshdr reshdr;
+ struct wim_header_disk pwm_hdr;
+ struct wim_resource_descriptor rdesc;
+ struct blob_descriptor *blob;
+
+ copy_guid(ctx->progress.extract.guid, ctx->wim->hdr.guid);
+ ctx->progress.extract.part_number = ctx->wim->hdr.part_number;
+ ctx->progress.extract.total_parts = ctx->wim->hdr.total_parts;
+ ret = extract_progress(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN);
+ if (ret)
+ return ret;
+
+ while (ctx->num_blobs_remaining) {
+
+ ret = read_pwm_blob_header(ctx->wim, hash, &reshdr, &pwm_hdr);
+
+ if (ret == PWM_FOUND_WIM_HDR) {
+ u16 part_number = le16_to_cpu(pwm_hdr.part_number);
+ u16 total_parts = le16_to_cpu(pwm_hdr.total_parts);
+
+ if (part_number == ctx->progress.extract.part_number &&
+ total_parts == ctx->progress.extract.total_parts &&
+ guids_equal(pwm_hdr.guid, ctx->progress.extract.guid))
+ continue;
+
+ copy_guid(ctx->progress.extract.guid, pwm_hdr.guid);
+ ctx->progress.extract.part_number = part_number;
+ ctx->progress.extract.total_parts = total_parts;
+ ret = extract_progress(ctx, WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN);
+ if (ret)
+ return ret;
+
+ continue;