+ imagex_error(T("Invalid compression type \"%"TS"\"! Must be "
+ "\"maximum\", \"fast\", or \"none\"."), optarg);
+ return WIMLIB_COMPRESSION_TYPE_INVALID;
+ }
+}
+
+static void
+set_compress_slow(void)
+{
+ static const struct wimlib_lzx_compressor_params lzx_slow_params = {
+ .hdr = {
+ .size = sizeof(struct wimlib_lzx_compressor_params),
+ },
+ .algorithm = WIMLIB_LZX_ALGORITHM_SLOW,
+ .alg_params = {
+ .slow = {
+ .use_len2_matches = 1,
+ .nice_match_length = 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,
+ },
+ },
+ };
+
+ static const struct wimlib_lzms_compressor_params lzms_slow_params = {
+ .hdr = {
+ .size = sizeof(struct wimlib_lzms_compressor_params),
+ },
+ .min_match_length = 2,
+ .max_match_length = UINT32_MAX,
+ .nice_match_length = 96,
+ .max_search_depth = 100,
+ .max_matches_per_pos = 10,
+ .optim_array_length = 1024,
+ };
+
+ wimlib_set_default_compressor_params(WIMLIB_COMPRESSION_TYPE_LZX,
+ &lzx_slow_params.hdr);
+
+ wimlib_set_default_compressor_params(WIMLIB_COMPRESSION_TYPE_LZMS,
+ &lzms_slow_params.hdr);
+}
+
+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);
+}
+
+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('#') || *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;
+