+struct win32_encrypted_read_ctx {
+ consume_data_callback_t read_prefix_cb;
+ void *read_prefix_ctx_or_buf;
+ int wimlib_err_code;
+ void *buf;
+ size_t buf_filled;
+ u64 bytes_remaining;
+};
+
+static DWORD WINAPI
+win32_encrypted_export_cb(unsigned char *_data, void *_ctx, unsigned long len)
+{
+ const void *data = _data;
+ struct win32_encrypted_read_ctx *ctx = _ctx;
+ int ret;
+
+ DEBUG("len = %lu", len);
+ if (ctx->read_prefix_cb) {
+ /* The length of the buffer passed to the ReadEncryptedFileRaw()
+ * export callback is undocumented, so we assume it may be of
+ * arbitrary size. */
+ size_t bytes_to_buffer = min(ctx->bytes_remaining - ctx->buf_filled,
+ len);
+ while (bytes_to_buffer) {
+ size_t bytes_to_copy_to_buf =
+ min(bytes_to_buffer, WIM_CHUNK_SIZE - ctx->buf_filled);
+
+ memcpy(ctx->buf + ctx->buf_filled, data,
+ bytes_to_copy_to_buf);
+ ctx->buf_filled += bytes_to_copy_to_buf;
+ data += bytes_to_copy_to_buf;
+ bytes_to_buffer -= bytes_to_copy_to_buf;
+
+ if (ctx->buf_filled == WIM_CHUNK_SIZE ||
+ ctx->buf_filled == ctx->bytes_remaining)
+ {
+ ret = (*ctx->read_prefix_cb)(ctx->buf,
+ ctx->buf_filled,
+ ctx->read_prefix_ctx_or_buf);
+ if (ret) {
+ ctx->wimlib_err_code = ret;
+ /* Shouldn't matter what error code is returned
+ * here, as long as it isn't ERROR_SUCCESS. */
+ return ERROR_READ_FAULT;
+ }
+ ctx->bytes_remaining -= ctx->buf_filled;
+ ctx->buf_filled = 0;
+ }
+ }
+ } else {
+ size_t len_to_copy = min(len, ctx->bytes_remaining);
+ memcpy(ctx->read_prefix_ctx_or_buf, data, len_to_copy);
+ ctx->bytes_remaining -= len_to_copy;
+ ctx->read_prefix_ctx_or_buf += len_to_copy;
+ }
+ return ERROR_SUCCESS;
+}
+
+int
+read_win32_encrypted_file_prefix(const struct wim_lookup_table_entry *lte,
+ u64 size,
+ consume_data_callback_t cb,
+ void *ctx_or_buf,
+ int _ignored_flags)
+{
+ struct win32_encrypted_read_ctx export_ctx;
+ DWORD err;
+ void *file_ctx;
+ int ret;
+
+ DEBUG("Reading %"PRIu64" bytes from encryted file \"%ls\"",
+ size, lte->file_on_disk);
+
+ export_ctx.read_prefix_cb = cb;
+ export_ctx.read_prefix_ctx_or_buf = ctx_or_buf;
+ export_ctx.wimlib_err_code = 0;
+ if (cb) {
+ export_ctx.buf = MALLOC(WIM_CHUNK_SIZE);
+ if (!export_ctx.buf)
+ return WIMLIB_ERR_NOMEM;
+ } else {
+ export_ctx.buf = NULL;
+ }
+ export_ctx.bytes_remaining = size;
+
+ err = OpenEncryptedFileRawW(lte->file_on_disk, 0, &file_ctx);
+ if (err != ERROR_SUCCESS) {
+ ERROR("Failed to open encrypted file \"%ls\" for raw read",
+ lte->file_on_disk);
+ win32_error(err);
+ ret = WIMLIB_ERR_OPEN;
+ goto out_free_buf;
+ }
+ err = ReadEncryptedFileRaw(win32_encrypted_export_cb,
+ &export_ctx, file_ctx);
+ if (err != ERROR_SUCCESS) {
+ ERROR("Failed to read encrypted file \"%ls\"",
+ lte->file_on_disk);
+ win32_error(err);
+ ret = export_ctx.wimlib_err_code;
+ if (ret == 0)
+ ret = WIMLIB_ERR_READ;
+ } else if (export_ctx.bytes_remaining != 0) {
+ ERROR("Only could read %"PRIu64" of %"PRIu64" bytes from "
+ "encryted file \"%ls\"",
+ size - export_ctx.bytes_remaining, size,
+ lte->file_on_disk);
+ ret = WIMLIB_ERR_READ;
+ } else {
+ ret = 0;
+ }
+ CloseEncryptedFileRaw(file_ctx);
+out_free_buf:
+ FREE(export_ctx.buf);
+ return ret;
+}
+
+/* Given a path, which may not yet exist, get a set of flags that describe the
+ * features of the volume the path is on. */
+static int
+win32_get_vol_flags(const wchar_t *path, unsigned *vol_flags_ret)
+{
+ wchar_t *volume;
+ BOOL bret;
+ DWORD vol_flags;
+
+ if (path[0] != L'\0' && path[0] != L'\\' &&
+ path[0] != L'/' && path[1] == L':')
+ {
+ /* Path starts with a drive letter; use it. */
+ volume = alloca(4 * sizeof(wchar_t));
+ volume[0] = path[0];
+ volume[1] = path[1];
+ volume[2] = L'\\';
+ volume[3] = L'\0';
+ } else {
+ /* Path does not start with a drive letter; use the volume of
+ * the current working directory. */
+ volume = NULL;
+ }
+ bret = GetVolumeInformationW(volume, /* lpRootPathName */
+ NULL, /* lpVolumeNameBuffer */
+ 0, /* nVolumeNameSize */
+ NULL, /* lpVolumeSerialNumber */
+ NULL, /* lpMaximumComponentLength */
+ &vol_flags, /* lpFileSystemFlags */
+ NULL, /* lpFileSystemNameBuffer */
+ 0); /* nFileSystemNameSize */
+ if (!bret) {
+ DWORD err = GetLastError();
+ WARNING("Failed to get volume information for path \"%ls\"", path);
+ win32_error(err);
+ vol_flags = 0xffffffff;
+ }
+
+ DEBUG("using vol_flags = %x", vol_flags);
+ *vol_flags_ret = vol_flags;
+ return 0;
+}
+
+