- /* Read the root directory entry starts after security data, on an
- * 8-byte aligned address.
- *
- * The security data starts with a 4-byte integer giving its total
- * length. */
- get_u32(buf, &dentry_offset);
- dentry_offset += (8 - dentry_offset % 8) % 8;
-
- ret = read_dentry(buf, res_entry->original_size, dentry_offset, dentry);
- if (ret != 0)
- goto err1;
-
- /* This is the root dentry, so set its pointers correctly. */
- dentry->parent = dentry;
- dentry->next = dentry;
- dentry->prev = dentry;
-
- /* Now read the entire directory entry tree. */
- ret = read_dentry_tree(buf, res_entry->original_size, dentry);
- if (ret != 0)
- goto err2;
-
- /* Calculate the full paths in the dentry tree. */
- ret = for_dentry_in_tree(dentry, calculate_dentry_full_path, NULL);
- if (ret != 0)
- goto err2;
-
- *root_dentry_p = dentry;
- FREE(buf);
- return ret;
-err2:
- free_dentry_tree(dentry, NULL, false);
-err1:
- FREE(buf);
+typedef int (*read_resource_prefix_handler_t)(const struct wim_lookup_table_entry *lte,
+ u64 size,
+ consume_data_callback_t cb,
+ void *ctx_or_buf,
+ int flags);
+
+/*
+ * Read the first @size bytes from a generic "resource", which may be located in
+ * the WIM (compressed or uncompressed), in an external file, or directly in an
+ * in-memory buffer.
+ *
+ * Feed the data either to a callback function (cb != NULL, passing it
+ * ctx_or_buf), or write it directly into a buffer (cb == NULL, ctx_or_buf
+ * specifies the buffer, which must have room for @size bytes).
+ *
+ * When using a callback function, it is called with chunks up to 32768 bytes in
+ * size until the resource is exhausted.
+ *
+ * If the resource is located in a WIM file, @flags can be:
+ * * WIMLIB_RESOURCE_FLAG_THREADSAFE_READ if it must be safe to access the resource
+ * concurrently by multiple threads.
+ * * WIMLIB_RESOURCE_FLAG_RAW if the raw compressed data is to be supplied
+ * instead of the uncompressed data.
+ * Otherwise, the @flags are ignored.
+ */
+int
+read_resource_prefix(const struct wim_lookup_table_entry *lte,
+ u64 size, consume_data_callback_t cb, void *ctx_or_buf,
+ int flags)
+{
+ static const read_resource_prefix_handler_t handlers[] = {
+ [RESOURCE_IN_WIM] = read_wim_resource_prefix,
+ [RESOURCE_IN_FILE_ON_DISK] = read_file_on_disk_prefix,
+ [RESOURCE_IN_ATTACHED_BUFFER] = read_buffer_prefix,
+ #ifdef WITH_FUSE
+ [RESOURCE_IN_STAGING_FILE] = read_file_on_disk_prefix,
+ #endif
+ #ifdef WITH_NTFS_3G
+ [RESOURCE_IN_NTFS_VOLUME] = read_ntfs_file_prefix,
+ #endif
+ #ifdef __WIN32__
+ [RESOURCE_WIN32] = read_win32_file_prefix,
+ [RESOURCE_WIN32_ENCRYPTED] = read_win32_encrypted_file_prefix,
+ #endif
+ };
+ wimlib_assert(lte->resource_location < ARRAY_LEN(handlers)
+ && handlers[lte->resource_location] != NULL);
+ return handlers[lte->resource_location](lte, size, cb, ctx_or_buf, flags);
+}
+
+int
+read_full_resource_into_buf(const struct wim_lookup_table_entry *lte,
+ void *buf, bool thread_safe)
+{
+ return read_resource_prefix(lte,
+ wim_resource_size(lte),
+ NULL, buf,
+ thread_safe ? WIMLIB_RESOURCE_FLAG_THREADSAFE_READ : 0);
+}
+
+struct extract_ctx {
+ SHA_CTX sha_ctx;
+ consume_data_callback_t extract_chunk;
+ void *extract_chunk_arg;
+};
+
+static int
+extract_chunk_sha1_wrapper(const void *chunk, size_t chunk_size,
+ void *_ctx)
+{
+ struct extract_ctx *ctx = _ctx;
+
+ sha1_update(&ctx->sha_ctx, chunk, chunk_size);
+ return ctx->extract_chunk(chunk, chunk_size, ctx->extract_chunk_arg);
+}
+
+/* Extracts the first @size bytes of a WIM resource to somewhere. In the
+ * process, the SHA1 message digest of the resource is checked if the full
+ * resource is being extracted.
+ *
+ * @extract_chunk is a function that is called to extract each chunk of the
+ * resource. */
+int
+extract_wim_resource(const struct wim_lookup_table_entry *lte,
+ u64 size,
+ consume_data_callback_t extract_chunk,
+ void *extract_chunk_arg)
+{
+ int ret;
+ if (size == wim_resource_size(lte)) {
+ /* Do SHA1 */
+ struct extract_ctx ctx;
+ ctx.extract_chunk = extract_chunk;
+ ctx.extract_chunk_arg = extract_chunk_arg;
+ sha1_init(&ctx.sha_ctx);
+ ret = read_resource_prefix(lte, size,
+ extract_chunk_sha1_wrapper,
+ &ctx, 0);
+ if (ret == 0) {
+ u8 hash[SHA1_HASH_SIZE];
+ sha1_final(hash, &ctx.sha_ctx);
+ if (!hashes_equal(hash, lte->hash)) {
+ #ifdef ENABLE_ERROR_MESSAGES
+ ERROR_WITH_ERRNO("Invalid SHA1 message digest "
+ "on the following WIM resource:");
+ print_lookup_table_entry(lte, stderr);
+ if (lte->resource_location == RESOURCE_IN_WIM)
+ ERROR("The WIM file appears to be corrupt!");
+ ret = WIMLIB_ERR_INVALID_RESOURCE_HASH;
+ #endif
+ }
+ }
+ } else {
+ /* Don't do SHA1 */
+ ret = read_resource_prefix(lte, size, extract_chunk,
+ extract_chunk_arg, 0);
+ }