+ int ret;
+
+ ret = (*cbs->begin_stream)(lte, false, cbs->begin_stream_ctx);
+ if (ret)
+ return ret;
+
+ ret = read_stream_prefix(lte, lte->size, cbs->consume_chunk,
+ cbs->consume_chunk_ctx);
+
+ return (*cbs->end_stream)(lte, ret, cbs->end_stream_ctx);
+}
+
+/* Read the full data of the specified stream, passing the data into the
+ * specified callbacks (all of which are optional) and either checking or
+ * computing the SHA1 message digest of the stream. */
+static int
+read_full_stream_with_sha1(struct wim_lookup_table_entry *lte,
+ const struct read_stream_list_callbacks *cbs)
+{
+ struct hasher_context hasher_ctx = {
+ .flags = VERIFY_STREAM_HASHES | COMPUTE_MISSING_STREAM_HASHES,
+ .cbs = *cbs,
+ };
+ struct read_stream_list_callbacks hasher_cbs = {
+ .begin_stream = hasher_begin_stream,
+ .begin_stream_ctx = &hasher_ctx,
+ .consume_chunk = hasher_consume_chunk,
+ .consume_chunk_ctx = &hasher_ctx,
+ .end_stream = hasher_end_stream,
+ .end_stream_ctx = &hasher_ctx,
+
+ };
+ return read_full_stream_with_cbs(lte, &hasher_cbs);
+}
+
+/*
+ * Read a list of streams, each of which may be in any supported location (e.g.
+ * in a WIM or in an external file). Unlike read_stream_prefix() or the
+ * functions which call it, this function optimizes the case where multiple
+ * streams are packed into a single compressed WIM resource and reads them all
+ * consecutively, only decompressing the data one time.
+ *
+ * @stream_list
+ * List of streams (represented as `struct wim_lookup_table_entry's) to
+ * read.
+ * @list_head_offset
+ * Offset of the `struct list_head' within each `struct
+ * wim_lookup_table_entry' that makes up the @stream_list.
+ * @cbs
+ * Callback functions to accept the stream data.
+ * @flags
+ * Bitwise OR of zero or more of the following flags:
+ *
+ * VERIFY_STREAM_HASHES:
+ * For all streams being read that have already had SHA1 message
+ * digests computed, calculate the SHA1 message digest of the read
+ * data and compare it with the previously computed value. If they
+ * do not match, return WIMLIB_ERR_INVALID_RESOURCE_HASH.
+ *
+ * COMPUTE_MISSING_STREAM_HASHES
+ * For all streams being read that have not yet had their SHA1
+ * message digests computed, calculate and save their SHA1 message
+ * digests.
+ *
+ * STREAM_LIST_ALREADY_SORTED
+ * @stream_list is already sorted in sequential order for reading.
+ *
+ * The callback functions are allowed to delete the current stream from the list
+ * if necessary.
+ *
+ * Returns 0 on success; a nonzero error code on failure. Failure can occur due
+ * to an error reading the data or due to an error status being returned by any
+ * of the callback functions.
+ */
+int
+read_stream_list(struct list_head *stream_list,
+ size_t list_head_offset,
+ const struct read_stream_list_callbacks *cbs,
+ int flags)
+{
+ int ret;
+ struct list_head *cur, *next;
+ struct wim_lookup_table_entry *lte;
+ struct hasher_context *hasher_ctx;
+ struct read_stream_list_callbacks *sink_cbs;
+
+ if (!(flags & STREAM_LIST_ALREADY_SORTED)) {
+ ret = sort_stream_list_by_sequential_order(stream_list, list_head_offset);
+ if (ret)
+ return ret;
+ }
+
+ if (flags & (VERIFY_STREAM_HASHES | COMPUTE_MISSING_STREAM_HASHES)) {
+ hasher_ctx = alloca(sizeof(*hasher_ctx));
+ *hasher_ctx = (struct hasher_context) {
+ .flags = flags,
+ .cbs = *cbs,
+ };
+ sink_cbs = alloca(sizeof(*sink_cbs));
+ *sink_cbs = (struct read_stream_list_callbacks) {
+ .begin_stream = hasher_begin_stream,
+ .begin_stream_ctx = hasher_ctx,
+ .consume_chunk = hasher_consume_chunk,
+ .consume_chunk_ctx = hasher_ctx,
+ .end_stream = hasher_end_stream,
+ .end_stream_ctx = hasher_ctx,
+ };