+
+ /* Retrieve the final SHA1 message digest. */
+ sha1_final(hash, &ctx->sha_ctx);
+
+ if (lte->unhashed) {
+ if (ctx->flags & COMPUTE_MISSING_STREAM_HASHES) {
+ /* No SHA1 message digest was previously present for the
+ * stream. Set it to the one just calculated. */
+ DEBUG("Set SHA1 message digest for stream "
+ "(size=%"PRIu64").", lte->size);
+ copy_hash(lte->hash, hash);
+ }
+ } else {
+ if (ctx->flags & VERIFY_STREAM_HASHES) {
+ /* The stream already had a SHA1 message digest present. Verify
+ * that it is the same as the calculated value. */
+ if (!hashes_equal(hash, lte->hash)) {
+ if (wimlib_print_errors) {
+ ERROR("Invalid SHA1 message digest "
+ "on the following WIM stream:");
+ print_lookup_table_entry(lte, stderr);
+ }
+ ret = WIMLIB_ERR_INVALID_RESOURCE_HASH;
+ errno = EINVAL;
+ goto out_next_cb;
+ }
+ DEBUG("SHA1 message digest okay for "
+ "stream (size=%"PRIu64").", lte->size);
+ }
+ }
+ ret = 0;
+out_next_cb:
+ if (ctx->cbs.end_stream == NULL)
+ return ret;
+ else
+ return (*ctx->cbs.end_stream)(lte, ret, ctx->cbs.end_stream_ctx);
+}
+
+static int
+read_full_stream_with_cbs(struct wim_lookup_table_entry *lte,
+ const struct read_stream_list_callbacks *cbs)
+{
+ 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);