+
+static tchar *
+translate_text_to_tstr(char *text, size_t num_bytes, size_t *num_tchars_ret)
+{
+#ifndef __WIN32__
+ /* On non-Windows, assume an ASCII-compatible encoding, such as UTF-8.
+ * */
+ *num_tchars_ret = num_bytes;
+ return text;
+#else /* !__WIN32__ */
+ /* On Windows, translate the text to UTF-16LE */
+ wchar_t *text_wstr;
+ size_t num_wchars;
+
+ if (num_bytes >= 2 &&
+ ((text[0] == 0xff && text[1] == 0xfe) ||
+ (text[0] <= 0x7f && text[1] == 0x00)))
+ {
+ /* File begins with 0xfeff, the BOM for UTF-16LE, or it begins
+ * with something that looks like an ASCII character encoded as
+ * a UTF-16LE code unit. Assume the file is encoded as
+ * UTF-16LE. This is not a 100% reliable check. */
+ num_wchars = num_bytes / 2;
+ text_wstr = (wchar_t*)text;
+ } else {
+ /* File does not look like UTF-16LE. Assume it is encoded in
+ * the current Windows code page. I think these are always
+ * ASCII-compatible, so any so-called "plain-text" (ASCII) files
+ * should work as expected. */
+ text_wstr = win32_mbs_to_wcs(text,
+ num_bytes,
+ &num_wchars);
+ free(text);
+ }
+ *num_tchars_ret = num_wchars;
+ return text_wstr;
+#endif /* __WIN32__ */
+}
+
+static tchar *
+file_get_text_contents(const tchar *filename, size_t *num_tchars_ret)
+{
+ char *contents;
+ size_t num_bytes;
+
+ contents = file_get_contents(filename, &num_bytes);
+ if (!contents)
+ return NULL;
+ return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
+}
+
+static tchar *
+stdin_get_text_contents(size_t *num_tchars_ret)
+{
+ char *contents;
+ size_t num_bytes;
+
+ contents = stdin_get_contents(&num_bytes);
+ if (!contents)
+ return NULL;
+ return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
+}
+
+/* Return 0 if a path names a file to which the current user has write access;
+ * -1 otherwise (and print an error message). */
+static int
+file_writable(const tchar *path)
+{
+ int ret;
+ ret = taccess(path, W_OK);
+ if (ret != 0)
+ imagex_error_with_errno(T("Can't modify \"%"TS"\""), path);
+ return ret;
+}
+
+#define TO_PERCENT(numerator, denominator) \
+ (((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
+
+/* Given an enumerated value for WIM compression type, return a descriptive
+ * string. */
+static const tchar *
+get_data_type(int ctype)
+{
+ switch (ctype) {
+ case WIMLIB_COMPRESSION_TYPE_NONE:
+ return T("uncompressed");
+ case WIMLIB_COMPRESSION_TYPE_LZX:
+ return T("LZX-compressed");
+ case WIMLIB_COMPRESSION_TYPE_XPRESS:
+ return T("XPRESS-compressed");
+ }
+ return NULL;
+}
+
+/* Progress callback function passed to various wimlib functions. */
+static int
+imagex_progress_func(enum wimlib_progress_msg msg,
+ const union wimlib_progress_info *info)
+{
+ unsigned percent_done;
+ if (imagex_be_quiet)
+ return 0;
+ switch (msg) {
+ case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
+ percent_done = TO_PERCENT(info->write_streams.completed_bytes,
+ info->write_streams.total_bytes);
+ if (info->write_streams.completed_streams == 0) {
+ const tchar *data_type;
+
+ data_type = get_data_type(info->write_streams.compression_type);
+ tprintf(T("Writing %"TS" data using %u thread%"TS"\n"),
+ data_type, info->write_streams.num_threads,
+ (info->write_streams.num_threads == 1) ? T("") : T("s"));
+ }
+ tprintf(T("\r%"PRIu64" MiB of %"PRIu64" MiB (uncompressed) "
+ "written (%u%% done)"),
+ info->write_streams.completed_bytes >> 20,
+ info->write_streams.total_bytes >> 20,
+ percent_done);
+ if (info->write_streams.completed_bytes >= info->write_streams.total_bytes)
+ tputchar(T('\n'));
+ break;
+ case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
+ tprintf(T("Scanning \"%"TS"\""), info->scan.source);
+ if (*info->scan.wim_target_path) {
+ tprintf(T(" (loading as WIM path: \"/%"TS"\")...\n"),
+ info->scan.wim_target_path);
+ } else {
+ tprintf(T(" (loading as root of WIM image)...\n"));
+ }
+ break;
+ case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
+ if (info->scan.excluded)
+ tprintf(T("Excluding \"%"TS"\" from capture\n"), info->scan.cur_path);
+ else
+ tprintf(T("Scanning \"%"TS"\"\n"), info->scan.cur_path);
+ break;
+ /*case WIMLIB_PROGRESS_MSG_SCAN_END:*/
+ /*break;*/
+ case WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY:
+ percent_done = TO_PERCENT(info->integrity.completed_bytes,
+ info->integrity.total_bytes);
+ tprintf(T("\rVerifying integrity of \"%"TS"\": %"PRIu64" MiB "
+ "of %"PRIu64" MiB (%u%%) done"),
+ info->integrity.filename,
+ info->integrity.completed_bytes >> 20,
+ info->integrity.total_bytes >> 20,
+ percent_done);
+ if (info->integrity.completed_bytes == info->integrity.total_bytes)
+ tputchar(T('\n'));
+ break;
+ case WIMLIB_PROGRESS_MSG_CALC_INTEGRITY:
+ percent_done = TO_PERCENT(info->integrity.completed_bytes,
+ info->integrity.total_bytes);
+ tprintf(T("\rCalculating integrity table for WIM: %"PRIu64" MiB "
+ "of %"PRIu64" MiB (%u%%) done"),
+ info->integrity.completed_bytes >> 20,
+ info->integrity.total_bytes >> 20,
+ percent_done);
+ if (info->integrity.completed_bytes == info->integrity.total_bytes)
+ tputchar(T('\n'));
+ break;
+ case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
+ tprintf(T("Applying image %d (\"%"TS"\") from \"%"TS"\" "
+ "to %"TS" \"%"TS"\"\n"),
+ info->extract.image,
+ info->extract.image_name,
+ info->extract.wimfile_name,
+ ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
+ T("NTFS volume") : T("directory")),
+ info->extract.target);
+ break;
+ case WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN:
+ tprintf(T("Extracting \"%"TS"\" from image %d (\"%"TS"\") "
+ "in \"%"TS"\" to \"%"TS"\"\n"),
+ info->extract.extract_root_wim_source_path,
+ info->extract.image,
+ info->extract.image_name,
+ info->extract.wimfile_name,
+ info->extract.target);
+ break;
+ /*case WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN:*/
+ /*tprintf(T("Applying directory structure to %"TS"\n"),*/
+ /*info->extract.target);*/
+ /*break;*/
+ case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
+ percent_done = TO_PERCENT(info->extract.completed_bytes,
+ info->extract.total_bytes);
+ tprintf(T("\rExtracting files: "
+ "%"PRIu64" MiB of %"PRIu64" MiB (%u%%) done"),
+ info->extract.completed_bytes >> 20,
+ info->extract.total_bytes >> 20,
+ percent_done);
+ if (info->extract.completed_bytes >= info->extract.total_bytes)
+ tputchar(T('\n'));
+ break;
+ case WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY:
+ tprintf(T("%"TS"\n"), info->extract.cur_path);
+ break;
+ case WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS:
+ if (info->extract.extract_root_wim_source_path[0] == T('\0'))
+ tprintf(T("Setting timestamps on all extracted files...\n"));
+ break;
+ case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END:
+ if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
+ tprintf(T("Unmounting NTFS volume \"%"TS"\"...\n"),
+ info->extract.target);
+ }
+ break;
+ case WIMLIB_PROGRESS_MSG_JOIN_STREAMS:
+ percent_done = TO_PERCENT(info->join.completed_bytes,
+ info->join.total_bytes);
+ tprintf(T("Writing resources from part %u of %u: "
+ "%"PRIu64 " MiB of %"PRIu64" MiB (%u%%) written\n"),
+ (info->join.completed_parts == info->join.total_parts) ?
+ info->join.completed_parts : info->join.completed_parts + 1,
+ info->join.total_parts,
+ info->join.completed_bytes >> 20,
+ info->join.total_bytes >> 20,
+ percent_done);
+ break;
+ case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
+ percent_done = TO_PERCENT(info->split.completed_bytes,
+ info->split.total_bytes);
+ tprintf(T("Writing \"%"TS"\": %"PRIu64" MiB of "
+ "%"PRIu64" MiB (%u%%) written\n"),
+ info->split.part_name,
+ info->split.completed_bytes >> 20,
+ info->split.total_bytes >> 20,
+ percent_done);
+ break;
+ case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
+ if (info->split.completed_bytes == info->split.total_bytes) {
+ tprintf(T("Finished writing %u split WIM parts\n"),
+ info->split.cur_part_number);
+ }
+ break;
+ default:
+ break;
+ }
+ fflush(stdout);
+ return 0;
+}
+
+/* Open all the split WIM parts that correspond to a file glob.
+ *
+ * @first_part specifies the first part of the split WIM and it may be either
+ * included or omitted from the glob. */
+static int
+open_swms_from_glob(const tchar *swm_glob,
+ const tchar *first_part,
+ int open_flags,
+ WIMStruct ***additional_swms_ret,
+ unsigned *num_additional_swms_ret)