- DEBUG2("comp size = %"PRIu64", uncomp size = %"PRIu64", "
- "res offset = %"PRIu64"",
- resource_compressed_size,
- resource_uncompressed_size,
- resource_offset);
- DEBUG2("resource_ctype = %s, len = %"PRIu64", offset = %"PRIu64"",
- wimlib_get_compression_type_string(resource_ctype), len, offset);
- /* Trivial case */
- if (len == 0)
- return 0;
-
- int (*decompress)(const void *, uint, void *, uint);
- /* Set the appropriate decompress function. */
- if (resource_ctype == WIM_COMPRESSION_TYPE_LZX)
- decompress = lzx_decompress;
- else
- decompress = xpress_decompress;
-
- /* The structure of a compressed resource consists of a table of chunk
- * offsets followed by the chunks themselves. Each chunk consists of
- * compressed data, and there is one chunk for each WIM_CHUNK_SIZE =
- * 32768 bytes of the uncompressed file, with the last chunk having any
- * remaining bytes.
- *
- * The chunk offsets are measured relative to the end of the chunk
- * table. The first chunk is omitted from the table in the WIM file
- * because its offset is implicitly given by the fact that it directly
- * follows the chunk table and therefore must have an offset of 0.
- */
-
- /* 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;
+
+/* Decompress the specified chunk that uses the specified compression type
+ * @ctype, part of a WIM with default chunk size @wim_chunk_size. For LZX the
+ * separate @wim_chunk_size is needed because it determines the window size used
+ * for LZX compression. */
+static int
+decompress(const void *cchunk, unsigned clen, void *uchunk, unsigned ulen,
+ int ctype, u32 wim_chunk_size)
+{
+ switch (ctype) {
+ case WIMLIB_COMPRESSION_TYPE_LZX:
+ return wimlib_lzx_decompress2(cchunk, clen,
+ uchunk, ulen, wim_chunk_size);
+ case WIMLIB_COMPRESSION_TYPE_XPRESS:
+ return wimlib_xpress_decompress(cchunk, clen,
+ uchunk, ulen);
+ case WIMLIB_COMPRESSION_TYPE_LZMS:
+ return wimlib_lzms_decompress(cchunk, clen, uchunk, ulen);
+ default:
+ wimlib_assert(0);
+ return -1;