+ imagex_error(T("Invalid compression type \"%"TS"\"! Must be "
+ "\"maximum\", \"fast\", or \"none\"."), optarg);
+ return WIMLIB_COMPRESSION_TYPE_INVALID;
+ }
+}
+
+static int
+set_compress_slow(void)
+{
+ int ret;
+ static const struct wimlib_lzx_compressor_params slow_params = {
+ .hdr = {
+ .size = sizeof(struct wimlib_lzx_compressor_params),
+ },
+ .algorithm = WIMLIB_LZX_ALGORITHM_SLOW,
+ .alg_params = {
+ .slow = {
+ .use_len2_matches = 1,
+ .num_fast_bytes = 96,
+ .num_optim_passes = 4,
+ .max_search_depth = 100,
+ .max_matches_per_pos = 10,
+ .main_nostat_cost = 15,
+ .len_nostat_cost = 15,
+ .aligned_nostat_cost = 7,
+ },
+ },
+ };
+ ret = wimlib_set_default_compressor_params(WIMLIB_COMPRESSION_TYPE_LZX,
+ &slow_params.hdr);
+ if (ret)
+ imagex_error(T("Couldn't set slow compression parameters.!"));
+ return ret;
+}
+
+struct string_set {
+ const tchar **strings;
+ unsigned num_strings;
+ unsigned num_alloc_strings;
+};
+
+#define STRING_SET_INITIALIZER \
+ { .strings = NULL, .num_strings = 0, .num_alloc_strings = 0, }
+
+#define STRING_SET(_strings) \
+ struct string_set _strings = STRING_SET_INITIALIZER
+
+static int
+string_set_append(struct string_set *set, const tchar *glob)
+{
+ unsigned num_alloc_strings = set->num_alloc_strings;
+
+ if (set->num_strings == num_alloc_strings) {
+ const tchar **new_strings;
+
+ num_alloc_strings += 4;
+ new_strings = realloc(set->strings,
+ sizeof(set->strings[0]) * num_alloc_strings);
+ if (!new_strings) {
+ imagex_error(T("Out of memory!"));
+ return -1;
+ }
+ set->strings = new_strings;
+ set->num_alloc_strings = num_alloc_strings;
+ }
+ set->strings[set->num_strings++] = glob;
+ return 0;
+}
+
+static void
+string_set_destroy(struct string_set *set)
+{
+ free(set->strings);
+}
+
+static int
+wim_reference_globs(WIMStruct *wim, struct string_set *set, int open_flags)
+{
+ return wimlib_reference_resource_files(wim, set->strings,
+ set->num_strings,
+ WIMLIB_REF_FLAG_GLOB_ENABLE,
+ open_flags,
+ imagex_progress_func);
+}
+
+static void
+do_resource_not_found_warning(const tchar *wimfile,
+ const struct wimlib_wim_info *info,
+ const struct string_set *refglobs)
+{
+ if (info->total_parts > 1) {
+ if (refglobs->num_strings == 0) {
+ imagex_error(T("\"%"TS"\" is part of a split WIM. "
+ "Use --ref to specify the other parts."),
+ wimfile);
+ } else {
+ imagex_error(T("Perhaps the '--ref' argument did not "
+ "specify all other parts of the split "
+ "WIM?"));
+ }
+ } else {
+ imagex_error(T("If this is a delta WIM, use the --ref argument "
+ "to specify the WIM(s) on which it is based."));
+ }
+}
+
+/* Returns the size of a file given its name, or -1 if the file does not exist
+ * or its size cannot be determined. */
+static off_t
+file_get_size(const tchar *filename)
+{
+ struct stat st;
+ if (tstat(filename, &st) == 0)
+ return st.st_size;
+ else
+ return (off_t)-1;
+}
+
+enum {
+ PARSE_STRING_SUCCESS = 0,
+ PARSE_STRING_FAILURE = 1,
+ PARSE_STRING_NONE = 2,
+};
+
+/*
+ * Parses a string token from an array of characters.
+ *
+ * Tokens are either whitespace-delimited, or double or single-quoted.
+ *
+ * @line_p: Pointer to the pointer to the line of data. Will be updated
+ * to point past the string token iff the return value is
+ * PARSE_STRING_SUCCESS. If *len_p > 0, (*line_p)[*len_p - 1] must
+ * be '\0'.
+ *
+ * @len_p: @len_p initially stores the length of the line of data, which may
+ * be 0, and it will be updated to the number of bytes remaining in
+ * the line iff the return value is PARSE_STRING_SUCCESS.
+ *
+ * @fn_ret: Iff the return value is PARSE_STRING_SUCCESS, a pointer to the
+ * parsed string token will be returned here.
+ *
+ * Returns: PARSE_STRING_SUCCESS if a string token was successfully parsed; or
+ * PARSE_STRING_FAILURE if the data was invalid due to a missing
+ * closing quote; or PARSE_STRING_NONE if the line ended before the
+ * beginning of a string token was found.
+ */
+static int
+parse_string(tchar **line_p, size_t *len_p, tchar **fn_ret)
+{
+ size_t len = *len_p;
+ tchar *line = *line_p;
+ tchar *fn;
+ tchar quote_char;
+
+ /* Skip leading whitespace */
+ for (;;) {
+ if (len == 0)
+ return PARSE_STRING_NONE;
+ if (!istspace(*line) && *line != T('\0'))
+ break;
+ line++;
+ len--;
+ }
+ quote_char = *line;
+ if (quote_char == T('"') || quote_char == T('\'')) {
+ /* Quoted string */
+ line++;
+ len--;
+ fn = line;
+ line = tmemchr(line, quote_char, len);
+ if (!line) {
+ imagex_error(T("Missing closing quote: %"TS), fn - 1);
+ return PARSE_STRING_FAILURE;
+ }
+ } else {
+ /* Unquoted string. Go until whitespace. Line is terminated
+ * by '\0', so no need to check 'len'. */
+ fn = line;
+ do {
+ line++;
+ } while (!istspace(*line) && *line != T('\0'));
+ }
+ *line = T('\0');
+ len -= line - fn;
+ *len_p = len;
+ *line_p = line;
+ *fn_ret = fn;
+ return PARSE_STRING_SUCCESS;
+}
+
+/* Parses a line of data (not an empty line or comment) in the source list file
+ * format. (See the man page for 'wimlib-imagex capture' for details on this
+ * format and the meaning.)
+ *
+ * @line: Line of data to be parsed. line[len - 1] must be '\0', unless
+ * len == 0. The data in @line will be modified by this function call.
+ *
+ * @len: Length of the line of data.
+ *
+ * @source: On success, the capture source and target described by the line is
+ * written into this destination. Note that it will contain pointers
+ * to data in the @line array.
+ *
+ * Returns true if the line was valid; false otherwise. */
+static bool
+parse_source_list_line(tchar *line, size_t len,
+ struct wimlib_capture_source *source)
+{
+ /* SOURCE [DEST] */
+ int ret;
+ ret = parse_string(&line, &len, &source->fs_source_path);
+ if (ret != PARSE_STRING_SUCCESS)
+ return false;
+ ret = parse_string(&line, &len, &source->wim_target_path);
+ if (ret == PARSE_STRING_NONE)
+ source->wim_target_path = source->fs_source_path;
+ return ret != PARSE_STRING_FAILURE;
+}
+
+/* Returns %true if the given line of length @len > 0 is a comment or empty line
+ * in the source list file format. */
+static bool
+is_comment_line(const tchar *line, size_t len)
+{
+ for (;;) {
+ if (*line == T('#'))
+ return true;
+ if (!istspace(*line) && *line != T('\0'))
+ return false;
+ ++line;
+ --len;
+ if (len == 0)
+ return true;
+ }
+}
+
+static ssize_t
+text_file_count_lines(tchar **contents_p, size_t *nchars_p)
+{
+ ssize_t nlines = 0;
+ tchar *contents = *contents_p;
+ size_t nchars = *nchars_p;
+ size_t i;
+
+ for (i = 0; i < nchars; i++)
+ if (contents[i] == T('\n'))
+ nlines++;
+
+ /* Handle last line not terminated by a newline */
+ if (nchars != 0 && contents[nchars - 1] != T('\n')) {
+ contents = realloc(contents, (nchars + 1) * sizeof(tchar));
+ if (!contents) {
+ imagex_error(T("Out of memory!"));
+ return -1;
+ }
+ contents[nchars] = T('\n');
+ *contents_p = contents;
+ nchars++;
+ nlines++;
+ }
+ *nchars_p = nchars;
+ return nlines;
+}
+
+/* Parses a file in the source list format. (See the man page for
+ * 'wimlib-imagex capture' for details on this format and the meaning.)
+ *
+ * @source_list_contents: Contents of the source list file. Note that this
+ * buffer will be modified to save memory allocations,
+ * and cannot be freed until the returned array of
+ * wimlib_capture_source's has also been freed.
+ *
+ * @source_list_nbytes: Number of bytes of data in the @source_list_contents
+ * buffer.
+ *
+ * @nsources_ret: On success, the length of the returned array is
+ * returned here.
+ *
+ * Returns: An array of `struct wimlib_capture_source's that can be passed to
+ * the wimlib_add_image_multisource() function to specify how a WIM image is to
+ * be created. */
+static struct wimlib_capture_source *
+parse_source_list(tchar **source_list_contents_p, size_t source_list_nchars,
+ size_t *nsources_ret)
+{
+ ssize_t nlines;
+ tchar *p;
+ struct wimlib_capture_source *sources;
+ size_t i, j;
+
+ nlines = text_file_count_lines(source_list_contents_p,
+ &source_list_nchars);
+ if (nlines < 0)
+ return NULL;
+
+ /* Always allocate at least 1 slot, just in case the implementation of
+ * calloc() returns NULL if 0 bytes are requested. */
+ sources = calloc(nlines ?: 1, sizeof(*sources));
+ if (!sources) {
+ imagex_error(T("out of memory"));
+ return NULL;
+ }
+ p = *source_list_contents_p;
+ j = 0;
+ for (i = 0; i < nlines; i++) {
+ /* XXX: Could use rawmemchr() here instead, but it may not be
+ * available on all platforms. */
+ tchar *endp = tmemchr(p, T('\n'), source_list_nchars);
+ size_t len = endp - p + 1;
+ *endp = T('\0');
+ if (!is_comment_line(p, len)) {
+ if (!parse_source_list_line(p, len, &sources[j++])) {
+ free(sources);
+ return NULL;
+ }
+ }
+ p = endp + 1;
+
+ }
+ *nsources_ret = j;
+ return sources;
+}
+
+
+enum capture_config_section {
+ CAPTURE_CONFIG_NO_SECTION,
+ CAPTURE_CONFIG_EXCLUSION_SECTION,
+ CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION,
+ CAPTURE_CONFIG_IGNORE_SECTION,
+};
+
+enum {
+ CAPTURE_CONFIG_INVALID_SECTION,
+ CAPTURE_CONFIG_CHANGED_SECTION,
+ CAPTURE_CONFIG_SAME_SECTION,
+};
+
+static int
+check_config_section(tchar *line, size_t len,
+ enum capture_config_section *cur_section)
+{
+ while (istspace(*line))
+ line++;
+
+ if (*line != T('['))
+ return CAPTURE_CONFIG_SAME_SECTION;
+
+ line++;
+ tchar *endbrace = tstrrchr(line, T(']'));
+ if (!endbrace)
+ return CAPTURE_CONFIG_SAME_SECTION;
+
+ if (!tmemcmp(line, T("ExclusionList"), endbrace - line)) {
+ *cur_section = CAPTURE_CONFIG_EXCLUSION_SECTION;
+ } else if (!tmemcmp(line, T("ExclusionException"), endbrace - line)) {
+ *cur_section = CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION;
+ } else if (!tmemcmp(line, T("CompressionExclusionList"), endbrace - line)) {
+ *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
+ tfputs(T("WARNING: Ignoring [CompressionExclusionList] section "
+ "of capture config file\n"),
+ stderr);
+ } else if (!tmemcmp(line, T("AlignmentList"), endbrace - line)) {
+ *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
+ tfputs(T("WARNING: Ignoring [AlignmentList] section "
+ "of capture config file\n"),
+ stderr);
+ } else {
+ imagex_error(T("Invalid capture config file section \"%"TS"\""),
+ line - 1);
+ return CAPTURE_CONFIG_INVALID_SECTION;
+ }
+ return CAPTURE_CONFIG_CHANGED_SECTION;
+}
+
+
+static bool
+pattern_list_add_pattern(struct wimlib_pattern_list *pat_list,
+ tchar *pat)
+{
+ if (pat_list->num_pats == pat_list->num_allocated_pats) {
+ tchar **pats;
+ size_t num_allocated_pats = pat_list->num_pats + 8;
+
+ pats = realloc(pat_list->pats,
+ num_allocated_pats * sizeof(pat_list->pats[0]));
+ if (!pats) {
+ imagex_error(T("Out of memory!"));
+ return false;
+ }
+ pat_list->pats = pats;
+ pat_list->num_allocated_pats = num_allocated_pats;