+get_system_compression_format(int extract_flags)
+{
+ if (extract_flags & WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS4K)
+ return FILE_PROVIDER_COMPRESSION_FORMAT_XPRESS4K;
+
+ if (extract_flags & WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS8K)
+ return FILE_PROVIDER_COMPRESSION_FORMAT_XPRESS8K;
+
+ if (extract_flags & WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS16K)
+ return FILE_PROVIDER_COMPRESSION_FORMAT_XPRESS16K;
+
+ return FILE_PROVIDER_COMPRESSION_FORMAT_LZX;
+}
+
+
+static const wchar_t *
+get_system_compression_format_string(int format)
+{
+ switch (format) {
+ case FILE_PROVIDER_COMPRESSION_FORMAT_XPRESS4K:
+ return L"XPRESS4K";
+ case FILE_PROVIDER_COMPRESSION_FORMAT_XPRESS8K:
+ return L"XPRESS8K";
+ case FILE_PROVIDER_COMPRESSION_FORMAT_XPRESS16K:
+ return L"XPRESS16K";
+ default:
+ return L"LZX";
+ }
+}
+
+static NTSTATUS
+set_system_compression(HANDLE h, int format)
+{
+ NTSTATUS status;
+ IO_STATUS_BLOCK iosb;
+ struct {
+ struct wof_external_info wof_info;
+ struct file_provider_external_info file_info;
+ } in = {
+ .wof_info = {
+ .version = WOF_CURRENT_VERSION,
+ .provider = WOF_PROVIDER_FILE,
+ },
+ .file_info = {
+ .version = FILE_PROVIDER_CURRENT_VERSION,
+ .compression_format = format,
+ },
+ };
+
+ /* We intentionally use NtFsControlFile() rather than DeviceIoControl()
+ * here because the "compressing this object would not save space"
+ * status code does not map to a valid Win32 error code on older
+ * versions of Windows (before Windows 10?). This can be a problem if
+ * the WOFADK driver is being used rather than the regular WOF, since
+ * WOFADK can be used on older versions of Windows. */
+ status = (*func_NtFsControlFile)(h, NULL, NULL, NULL, &iosb,
+ FSCTL_SET_EXTERNAL_BACKING,
+ &in, sizeof(in), NULL, 0);
+
+ if (status == 0xC000046F) /* "Compressing this object would not save space." */
+ return STATUS_SUCCESS;
+
+ return status;
+}
+
+/* Hard-coded list of files which the Windows bootloader may need to access
+ * before the WOF driver has been loaded. */
+static wchar_t *bootloader_pattern_strings[] = {
+ L"*winload.*",
+ L"*winresume.*",
+ L"\\Windows\\AppPatch\\drvmain.sdb",
+ L"\\Windows\\Boot\\DVD\\*",
+ L"\\Windows\\Boot\\EFI\\*",
+ L"\\Windows\\bootstat.dat",
+ L"\\Windows\\Fonts\\vgaoem.fon",
+ L"\\Windows\\Fonts\\vgasys.fon",
+ L"\\Windows\\INF\\errata.inf",
+ L"\\Windows\\System32\\config\\*",
+ L"\\Windows\\System32\\ntkrnlpa.exe",
+ L"\\Windows\\System32\\ntoskrnl.exe",
+ L"\\Windows\\System32\\bootvid.dll",
+ L"\\Windows\\System32\\ci.dll",
+ L"\\Windows\\System32\\hal*.dll",
+ L"\\Windows\\System32\\mcupdate_AuthenticAMD.dll",
+ L"\\Windows\\System32\\mcupdate_GenuineIntel.dll",
+ L"\\Windows\\System32\\pshed.dll",
+ L"\\Windows\\System32\\apisetschema.dll",
+ L"\\Windows\\System32\\api-ms-win*.dll",
+ L"\\Windows\\System32\\ext-ms-win*.dll",
+ L"\\Windows\\System32\\KernelBase.dll",
+ L"\\Windows\\System32\\drivers\\*.sys",
+ L"\\Windows\\System32\\*.nls",
+ L"\\Windows\\System32\\kbd*.dll",
+ L"\\Windows\\System32\\kd*.dll",
+ L"\\Windows\\System32\\clfs.sys",
+ L"\\Windows\\System32\\CodeIntegrity\\driver.stl",
+};
+
+static const struct string_set bootloader_patterns = {
+ .strings = bootloader_pattern_strings,
+ .num_strings = ARRAY_LEN(bootloader_pattern_strings),
+};
+
+static NTSTATUS
+set_system_compression_on_inode(struct wim_inode *inode, int format,
+ struct win32_apply_ctx *ctx)
+{
+ bool retried = false;
+ NTSTATUS status;
+ HANDLE h;
+
+ /* If it may be needed for compatibility with the Windows bootloader,
+ * force this file to XPRESS4K or uncompressed format. The bootloader
+ * of Windows 10 supports XPRESS4K only; older versions don't support
+ * system compression at all. */
+ if (!is_image_windows_10_or_later(ctx) ||
+ format != FILE_PROVIDER_COMPRESSION_FORMAT_XPRESS4K)
+ {
+ /* We need to check the patterns against every name of the
+ * inode, in case any of them match. */
+ struct wim_dentry *dentry;
+ inode_for_each_extraction_alias(dentry, inode) {
+ bool incompatible;
+ bool warned;
+
+ if (calculate_dentry_full_path(dentry)) {
+ ERROR("Unable to compute file path!");
+ return STATUS_NO_MEMORY;
+ }
+
+ incompatible = match_pattern_list(dentry->d_full_path,
+ &bootloader_patterns);
+ FREE(dentry->d_full_path);
+ dentry->d_full_path = NULL;
+
+ if (!incompatible)
+ continue;
+
+ warned = (ctx->num_system_compression_exclusions++ > 0);
+
+ if (is_image_windows_10_or_later(ctx)) {
+ /* Force to XPRESS4K */
+ if (!warned) {
+ WARNING("For compatibility with the "
+ "Windows bootloader, some "
+ "files are being\n"
+ " compacted "
+ "using the XPRESS4K format "
+ "instead of the %"TS" format\n"
+ " you requested.",
+ get_system_compression_format_string(format));
+ }
+ format = FILE_PROVIDER_COMPRESSION_FORMAT_XPRESS4K;
+ break;
+ } else {
+ /* Force to uncompressed */
+ if (!warned) {
+ WARNING("For compatibility with the "
+ "Windows bootloader, some "
+ "files will not\n"
+ " be compressed with"
+ " system compression "
+ "(\"compacted\").");
+ }
+ return STATUS_SUCCESS;
+ }
+
+ }
+ }
+
+ /* Open the extracted file. */
+ status = create_file(&h, GENERIC_READ | GENERIC_WRITE, NULL,
+ 0, FILE_OPEN, 0,
+ inode_first_extraction_dentry(inode), ctx);
+
+ if (!NT_SUCCESS(status))
+ return status;
+retry:
+ /* Compress the file. If the attempt fails with "invalid device
+ * request", then attach wof.sys (or wofadk.sys) and retry. */
+ status = set_system_compression(h, format);
+ if (unlikely(status == STATUS_INVALID_DEVICE_REQUEST && !retried)) {
+ wchar_t drive_path[7];
+ if (!win32_get_drive_path(ctx->common.target, drive_path) &&
+ win32_try_to_attach_wof(drive_path + 4)) {
+ retried = true;
+ goto retry;
+ }
+ }
+
+ (*func_NtClose)(h);
+ return status;
+}
+
+/*
+ * This function is called when doing a "compact-mode" extraction and we just
+ * finished extracting a blob to one or more locations. For each location that
+ * was the unnamed data stream of a file, this function compresses the
+ * corresponding file using System Compression, if allowed.
+ *
+ * Note: we're doing the compression immediately after extracting the data
+ * rather than during a separate compression pass. This way should be faster
+ * since the operating system should still have the file's data cached.
+ *
+ * Note: we're having the operating system do the compression, which is not
+ * ideal because wimlib could create the compressed data faster and more
+ * efficiently (the compressed data format is identical to a WIM resource). But
+ * we seemingly don't have a choice because WOF prevents applications from
+ * creating its reparse points.
+ */
+static void
+handle_system_compression(struct blob_descriptor *blob, struct win32_apply_ctx *ctx)
+{
+ const struct blob_extraction_target *targets = blob_extraction_targets(blob);
+
+ const int format = get_system_compression_format(ctx->common.extract_flags);
+
+ for (u32 i = 0; i < blob->out_refcnt; i++) {
+ struct wim_inode *inode = targets[i].inode;
+ struct wim_inode_stream *strm = targets[i].stream;
+ NTSTATUS status;
+
+ if (!stream_is_unnamed_data_stream(strm))
+ continue;
+
+ if (will_externally_back_inode(inode, ctx, NULL, false) != 0)
+ continue;
+
+ status = set_system_compression_on_inode(inode, format, ctx);
+ if (likely(NT_SUCCESS(status)))
+ continue;
+
+ if (status == STATUS_INVALID_DEVICE_REQUEST) {
+ WARNING(
+ "The request to compress the extracted files using System Compression\n"
+" will not be honored because the operating system or target volume\n"
+" does not support it. System Compression is only supported on\n"
+" Windows 10 and later, and only on NTFS volumes.");
+ ctx->common.extract_flags &= ~COMPACT_FLAGS;
+ return;
+ }
+
+ ctx->num_system_compression_failures++;
+ if (ctx->num_system_compression_failures < 10) {
+ winnt_warning(status, L"\"%ls\": Failed to compress "
+ "extracted file using System Compression",
+ current_path(ctx));
+ } else if (ctx->num_system_compression_failures == 10) {
+ WARNING("Suppressing further warnings about "
+ "System Compression failures.");
+ }
+ }
+}
+
+/* Called when a blob has been fully read for extraction on Windows */
+static int
+end_extract_blob(struct blob_descriptor *blob, int status, void *_ctx)