+static void
+try_rpfix(u8 *rpbuf, u16 *rpbuflen_p, struct win32_apply_ctx *ctx)
+{
+ struct reparse_data rpdata;
+ size_t orig_subst_name_nchars;
+ const wchar_t *relpath;
+ size_t relpath_nchars;
+ size_t target_ntpath_nchars;
+ size_t fixed_subst_name_nchars;
+ const wchar_t *fixed_print_name;
+ size_t fixed_print_name_nchars;
+
+ if (parse_reparse_data(rpbuf, *rpbuflen_p, &rpdata)) {
+ /* Do nothing if the reparse data is invalid. */
+ return;
+ }
+
+ if (rpdata.rptag == WIM_IO_REPARSE_TAG_SYMLINK &&
+ (rpdata.rpflags & SYMBOLIC_LINK_RELATIVE))
+ {
+ /* Do nothing if it's a relative symbolic link. */
+ return;
+ }
+
+ /* Build the new substitute name from the NT namespace path to the
+ * target directory, then a path separator, then the "device relative"
+ * part of the old substitute name. */
+
+ orig_subst_name_nchars = rpdata.substitute_name_nbytes / sizeof(wchar_t);
+
+ relpath = get_device_relative_path(rpdata.substitute_name,
+ orig_subst_name_nchars);
+ relpath_nchars = orig_subst_name_nchars -
+ (relpath - rpdata.substitute_name);
+
+ target_ntpath_nchars = ctx->target_ntpath.Length / sizeof(wchar_t);
+
+ fixed_subst_name_nchars = target_ntpath_nchars + 1 + relpath_nchars;
+ wchar_t fixed_subst_name[fixed_subst_name_nchars];
+
+ wmemcpy(fixed_subst_name, ctx->target_ntpath.Buffer,
+ target_ntpath_nchars);
+ fixed_subst_name[target_ntpath_nchars] = L'\\';
+ wmemcpy(&fixed_subst_name[target_ntpath_nchars + 1],
+ relpath, relpath_nchars);
+ /* Doesn't need to be null-terminated. */
+
+ /* Print name should be Win32, but not all NT names can even be
+ * translated to Win32 names. But we can at least delete the top-level
+ * directory, such as \??\, and this will have the expected result in
+ * the usual case. */
+ fixed_print_name = skip_nt_toplevel_component(fixed_subst_name,
+ fixed_subst_name_nchars);
+ fixed_print_name_nchars = fixed_subst_name_nchars - (fixed_print_name -
+ fixed_subst_name);
+
+ rpdata.substitute_name = fixed_subst_name;
+ rpdata.substitute_name_nbytes = fixed_subst_name_nchars * sizeof(wchar_t);
+ rpdata.print_name = (wchar_t *)fixed_print_name;
+ rpdata.print_name_nbytes = fixed_print_name_nchars * sizeof(wchar_t);
+ make_reparse_buffer(&rpdata, rpbuf, rpbuflen_p);
+}
+
+/* Sets reparse data on the specified file. This handles "fixing" the targets
+ * of absolute symbolic links and junctions if WIMLIB_EXTRACT_FLAG_RPFIX was
+ * specified. */
+static int
+set_reparse_data(const struct wim_dentry *dentry,
+ const void *_rpbuf, u16 rpbuflen, struct win32_apply_ctx *ctx)
+{
+ const struct wim_inode *inode = dentry->d_inode;
+ const void *rpbuf = _rpbuf;
+
+ if ((ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX)
+ && !inode->i_not_rpfixed
+ && (inode->i_reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK ||
+ inode->i_reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT))
+ {
+ memcpy(&ctx->rpfixbuf, _rpbuf, rpbuflen);
+ try_rpfix((u8 *)&ctx->rpfixbuf, &rpbuflen, ctx);
+ rpbuf = &ctx->rpfixbuf;
+ }
+ return do_set_reparse_data(dentry, rpbuf, rpbuflen, ctx);
+
+}
+
+/* Import the next block of raw encrypted data */
+static DWORD WINAPI
+import_encrypted_data(PBYTE pbData, PVOID pvCallbackContext, PULONG Length)
+{
+ struct win32_apply_ctx *ctx = pvCallbackContext;
+ ULONG copy_len;
+
+ copy_len = min(ctx->encrypted_size - ctx->encrypted_offset, *Length);
+ memcpy(pbData, &ctx->data_buffer[ctx->encrypted_offset], copy_len);
+ ctx->encrypted_offset += copy_len;
+ *Length = copy_len;
+ return ERROR_SUCCESS;
+}
+
+/* Write the raw encrypted data to the already-created file corresponding to
+ * @dentry.
+ *
+ * The raw encrypted data is provided in ctx->data_buffer, and its size is
+ * ctx->encrypted_size. */