- /* Calculate how many chunks the resource conists of in its entirety. */
- u64 num_chunks = (resource_uncompressed_size + WIM_CHUNK_SIZE - 1) /
- WIM_CHUNK_SIZE;
- /* As mentioned, the first chunk has no entry in the chunk table. */
- u64 num_chunk_entries = num_chunks - 1;
-
-
- /* The index of the chunk that the read starts at. */
- u64 start_chunk = offset / WIM_CHUNK_SIZE;
- /* The byte offset at which the read starts, within the start chunk. */
- u64 start_chunk_offset = offset % WIM_CHUNK_SIZE;
-
- /* The index of the chunk that contains the last byte of the read. */
- u64 end_chunk = (offset + len - 1) / WIM_CHUNK_SIZE;
- /* The byte offset of the last byte of the read, within the end chunk */
- u64 end_chunk_offset = (offset + len - 1) % WIM_CHUNK_SIZE;
-
- /* Number of chunks that are actually needed to read the requested part
- * of the file. */
- u64 num_needed_chunks = end_chunk - start_chunk + 1;
-
- /* If the end chunk is not the last chunk, an extra chunk entry is
- * needed because we need to know the offset of the chunk after the last
- * chunk read to figure out the size of the last read chunk. */
- if (end_chunk != num_chunks - 1)
- num_needed_chunks++;
-
- /* Declare the chunk table. It will only contain offsets for the chunks
- * that are actually needed for this read. */
- u64 chunk_offsets[num_needed_chunks];
-
- /* Set the implicit offset of the first chunk if it is included in the
- * needed chunks.
- *
- * Note: M$'s documentation includes a picture that shows the first
- * chunk starting right after the chunk entry table, labeled as offset
- * 0x10. However, in the actual file format, the offset is measured
- * from the end of the chunk entry table, so the first chunk has an
- * offset of 0. */
- if (start_chunk == 0)
- chunk_offsets[0] = 0;
-
- /* According to M$'s documentation, if the uncompressed size of
- * the file is greater than 4 GB, the chunk entries are 8-byte
- * integers. Otherwise, they are 4-byte integers. */
- u64 chunk_entry_size = (resource_uncompressed_size >= (u64)1 << 32) ?
- 8 : 4;
-
- /* Size of the full chunk table in the WIM file. */
- u64 chunk_table_size = chunk_entry_size * num_chunk_entries;
-
- /* Read the needed chunk offsets from the table in the WIM file. */
-
- /* Index, in the WIM file, of the first needed entry in the
- * chunk table. */
- u64 start_table_idx = (start_chunk == 0) ? 0 : start_chunk - 1;
-
- /* Number of entries we need to actually read from the chunk
- * table (excludes the implicit first chunk). */
- u64 num_needed_chunk_entries = (start_chunk == 0) ?
- num_needed_chunks - 1 : num_needed_chunks;
-
- /* Skip over unneeded chunk table entries. */
- u64 file_offset_of_needed_chunk_entries = resource_offset +
- start_table_idx * chunk_entry_size;
- if (fseeko(fp, file_offset_of_needed_chunk_entries, SEEK_SET) != 0) {
- ERROR_WITH_ERRNO("Failed to seek to byte %"PRIu64" to read "
- "chunk table of compressed resource",
- file_offset_of_needed_chunk_entries);
- return WIMLIB_ERR_READ;
+/*
+ * Read data from a compressed WIM resource.
+ *
+ * @rdesc
+ * Description of the compressed WIM resource to read from.
+ * @ranges
+ * Nonoverlapping, nonempty ranges of the uncompressed resource data to
+ * read, sorted by increasing offset.
+ * @num_ranges
+ * Number of ranges in @ranges; must be at least 1.
+ * @cbs
+ * Structure which provides the consume_chunk() callback to feed the data
+ * being read. Each call provides the next chunk of the requested data,
+ * uncompressed. Each chunk will be nonempty and will not cross range
+ * boundaries but otherwise will be of unspecified size.
+ *
+ * Possible return values:
+ *
+ * WIMLIB_ERR_SUCCESS (0)
+ * WIMLIB_ERR_READ (errno set)
+ * WIMLIB_ERR_UNEXPECTED_END_OF_FILE (errno set to 0)
+ * WIMLIB_ERR_NOMEM (errno set to ENOMEM)
+ * WIMLIB_ERR_DECOMPRESSION (errno set to EINVAL)
+ * WIMLIB_ERR_INVALID_CHUNK_SIZE (errno set to EINVAL)
+ *
+ * or other error code returned by the cbs->consume_chunk() function.
+ */
+static int
+read_compressed_wim_resource(const struct wim_resource_descriptor * const rdesc,
+ const struct data_range * const ranges,
+ const size_t num_ranges,
+ const struct read_blob_callbacks *cbs)
+{
+ int ret;
+ u64 *chunk_offsets = NULL;
+ u8 *ubuf = NULL;
+ void *cbuf = NULL;
+ bool chunk_offsets_malloced = false;
+ bool ubuf_malloced = false;
+ bool cbuf_malloced = false;
+ struct wimlib_decompressor *decompressor = NULL;
+
+ /* Sanity checks */
+ wimlib_assert(num_ranges != 0);
+ for (size_t i = 0; i < num_ranges; i++) {
+ wimlib_assert(ranges[i].offset + ranges[i].size > ranges[i].offset &&
+ ranges[i].offset + ranges[i].size <= rdesc->uncompressed_size);
+ }
+ for (size_t i = 0; i < num_ranges - 1; i++)
+ wimlib_assert(ranges[i].offset + ranges[i].size <= ranges[i + 1].offset);
+
+ /* Get the offsets of the first and last bytes of the read. */
+ const u64 first_offset = ranges[0].offset;
+ const u64 last_offset = ranges[num_ranges - 1].offset + ranges[num_ranges - 1].size - 1;
+
+ /* Get the file descriptor for the WIM. */
+ struct filedes * const in_fd = &rdesc->wim->in_fd;
+
+ /* Determine if we're reading a pipable resource from a pipe or not. */
+ const bool is_pipe_read = (rdesc->is_pipable && !filedes_is_seekable(in_fd));
+
+ /* Determine if the chunk table is in an alternate format. */
+ const bool alt_chunk_table = (rdesc->flags & WIM_RESHDR_FLAG_SOLID)
+ && !is_pipe_read;
+
+ /* Get the maximum size of uncompressed chunks in this resource, which
+ * we require be a power of 2. */
+ u64 cur_read_offset = rdesc->offset_in_wim;
+ int ctype = rdesc->compression_type;
+ u32 chunk_size = rdesc->chunk_size;
+ if (alt_chunk_table) {
+ /* Alternate chunk table format. Its header specifies the chunk
+ * size and compression format. Note: it could be read here;
+ * however, the relevant data was already loaded into @rdesc by
+ * read_blob_table(). */
+ cur_read_offset += sizeof(struct alt_chunk_table_header_disk);