]> wimlib.net Git - wimlib/blobdiff - src/lzms-decompress.c
Make --disable-multithreaded-compression work again
[wimlib] / src / lzms-decompress.c
index e64ce2db1c1667ff5e2e886a459c8756a19dfcbf..40d35bf6e2610e945a1034f19dc303e40ffa7d2b 100644 (file)
@@ -46,7 +46,7 @@
  * of the bits within the 16-bit coding units is such that the first bit is the
  * high-order bit and the last bit is the low-order bit.
  *
- * From these two logical bitstreams, a LZMS decompressor can reconstitute the
+ * From these two logical bitstreams, an LZMS decompressor can reconstitute the
  * series of items that make up the LZMS data representation.  Each such item
  * may be a literal byte or a match.  Matches may be either traditional LZ77
  * matches or "delta" matches, either of which can have its offset encoded
@@ -56,7 +56,7 @@
  * sequence of bytes beginning at the current position and extending for the
  * length is exactly equal to the equal-length sequence of bytes at the offset
  * back in the window.  On the other hand, a delta match consists of a length,
- * raw offset, and power.  It asserts that the sequence of bytes of beginning at
+ * raw offset, and power.  It asserts that the sequence of bytes beginning at
  * the current position and extending for the length is equal to the bytewise
  * sum of the two equal-length sequences of bytes (2**power) and (raw_offset *
  * 2**power) bytes before the current position, minus bytewise the sequence of
  * filled in with the next 16 bits from the forwards bitstream.
  *
  * To decode each bit, the range decoder requires a probability that is
- * logically a real  number between 0 and 1.  Multiplying this
- * probability by the current range and taking the floor gives the bound between
- * the 0-bit region of the range and the 1-bit region of the range.  However, in
- * LZMS, probabilities are restricted to values of n/64 where n is an integer is
+ * logically a real number between 0 and 1.  Multiplying this probability by the
+ * current range and taking the floor gives the bound between the 0-bit region
+ * of the range and the 1-bit region of the range.  However, in LZMS,
+ * probabilities are restricted to values of n/64 where n is an integer is
  * between 1 and 63 inclusively, so the implementation may use integer
  * operations instead.  Following calculation of the bound, if the current code
  * is in the 0-bit region, the new range becomes the current code and the
  * decoded bit is 0; otherwise, the bound must be subtracted from both the range
  * and the code, and the decoded bit is 1.  More information about range coding
- * can be found https://en.wikipedia.org/wiki/Range_encoding.  Furthermore, note
- * that the LZMA format also uses range coding and has public domain code
+ * can be found at https://en.wikipedia.org/wiki/Range_encoding.  Furthermore,
+ * note that the LZMA format also uses range coding and has public domain code
  * available for it.
  *
  * The probability used to range-decode each bit must be taken from a table, of
  * bitstream.  For this, there are 5 different Huffman codes used:
  *
  *  - The literal code, used for decoding literal bytes.  Each of the 256
- *    symbols represents literal byte.  This code must be rebuilt whenever 1024
- *    symbols have been decoded with it.
+ *    symbols represents a literal byte.  This code must be rebuilt whenever
+ *    1024 symbols have been decoded with it.
  *
  *  - The LZ offset code, used for decoding the offsets of standard LZ77
  *    matches.  Each symbol represents a position slot, which corresponds to a
  *
  * Codewords in all the LZMS Huffman codes are limited to 15 bits.  If the
  * canonical code for a given set of symbol frequencies has any codewords longer
- * than 15 bits, all frequencies must be divided by 2, rounding up, and the code
- * construction must be attempted again.
+ * than 15 bits, then all frequencies must be divided by 2, rounding up, and the
+ * code construction must be attempted again.
  *
- * A LZMS-compressed block seemingly cannot have a size greater than or equal to
- * the original uncompressed size.  In such cases the block must be stored
+ * A LZMS-compressed block seemingly cannot have a compressed size greater than
+ * or equal to the uncompressed size.  In such cases the block must be stored
  * uncompressed.
  *
  * After all LZMS items have been decoded, the data must be postprocessed to
 #endif
 
 #include "wimlib.h"
-#include "wimlib/compress.h"
-#include "wimlib/decompress.h"
+#include "wimlib/compress_common.h"
+#include "wimlib/decompressor_ops.h"
+#include "wimlib/decompress_common.h"
 #include "wimlib/error.h"
 #include "wimlib/lzms.h"
 #include "wimlib/util.h"
@@ -381,6 +382,9 @@ struct lzms_decompressor {
        u32 upcoming_lz_offset;
        u32 upcoming_delta_power;
        u32 upcoming_delta_offset;
+
+       /* Used for postprocessing  */
+       s32 last_target_usages[65536];
 };
 
 /* A table that maps position slots to their base values.  These are constants
@@ -660,8 +664,8 @@ lzms_rebuild_adaptive_huffman_code(struct lzms_huffman_decoder *dec)
 {
        int ret;
 
-       /* XXX:  This implementation that makes use of code already implemented
-        * for the XPRESS and LZX compression formats.  However, since for the
+       /* XXX:  This implementation makes use of code already implemented for
+        * the XPRESS and LZX compression formats.  However, since for the
         * adaptive codes used in LZMS we don't actually need the explicit codes
         * themselves, only the decode tables, it may be possible to optimize
         * this by somehow directly building or updating the Huffman decode
@@ -768,7 +772,7 @@ lzms_copy_literal(struct lzms_decompressor *ctx, u8 literal)
        return 0;
 }
 
-/* Validate a LZ match and copy it to the output buffer.  */
+/* Validate an LZ match and copy it to the output buffer.  */
 static int
 lzms_copy_lz_match(struct lzms_decompressor *ctx, u32 length, u32 offset)
 {
@@ -951,7 +955,6 @@ lzms_decode_item(struct lzms_decompressor *ctx)
                for (int i = LZMS_NUM_RECENT_OFFSETS - 1; i >= 0; i--)
                        ctx->recent_lz_offsets[i + 1] = ctx->recent_lz_offsets[i];
                ctx->recent_lz_offsets[0] = ctx->prev_lz_offset;
-
        }
 
        if (ctx->prev_delta_offset != 0) {
@@ -997,7 +1000,7 @@ lzms_init_huffman_decoder(struct lzms_huffman_decoder *dec,
                dec->sym_freqs[i] = 1;
 }
 
-/* Prepare to decode items from a LZMS-compressed block.  */
+/* Prepare to decode items from an LZMS-compressed block.  */
 static void
 lzms_init_decompressor(struct lzms_decompressor *ctx,
                       const void *cdata, unsigned clen,
@@ -1022,14 +1025,8 @@ lzms_init_decompressor(struct lzms_decompressor *ctx,
        /* Initialize position and length slot bases if not done already.  */
        lzms_init_slot_bases();
 
-       /* Like in other compression formats such as LZX and DEFLATE, match
-        * offsets in LZMS are represented as a position slot, which corresponds
-        * to a fixed lesser or equal match offset, followed by a
-        * position-slot-dependent number of extra bits that gives an additional
-        * offset from that position slot.  Because the full number of position
-        * slots may exceed the length of the compressed block, here we
-        * calculate the number of position slots that will actually be used in
-        * the compressed representation.  */
+       /* Calculate the number of position slots needed for this compressed
+        * block.  */
        num_position_slots = lzms_get_position_slot_raw(ulen - 1) + 1;
 
        LZMS_DEBUG("Using %u position slots", num_position_slots);
@@ -1057,8 +1054,8 @@ lzms_init_decompressor(struct lzms_decompressor *ctx,
                                  LZMS_DELTA_POWER_CODE_REBUILD_FREQ);
 
 
-       /* Initialize range decoders (all of which wrap around the same
-        * lzms_range_decoder_raw).  */
+       /* Initialize range decodersall of which wrap around the same
+        * lzms_range_decoder_raw.  */
        lzms_init_range_decoder(&ctx->main_range_decoder,
                                &ctx->rd, LZMS_NUM_MAIN_STATES);
 
@@ -1079,7 +1076,6 @@ lzms_init_decompressor(struct lzms_decompressor *ctx,
                lzms_init_range_decoder(&ctx->delta_repeat_match_range_decoders[i],
                                        &ctx->rd, LZMS_NUM_DELTA_REPEAT_MATCH_STATES);
 
-
        /* Initialize the LRU queue for recent match offsets.  */
        for (size_t i = 0; i < LZMS_NUM_RECENT_OFFSETS + 1; i++)
                ctx->recent_lz_offsets[i] = i + 1;
@@ -1101,18 +1097,16 @@ lzms_init_decompressor(struct lzms_decompressor *ctx,
 /* Decode the series of literals and matches from the LZMS-compressed data.
  * Returns 0 on success; nonzero if the compressed data is invalid.  */
 static int
-lzms_decode_items(const u8 *cdata, size_t clen, u8 *ubuf, size_t ulen)
+lzms_decode_items(const u8 *cdata, size_t clen, u8 *ubuf, size_t ulen,
+                 struct lzms_decompressor *ctx)
 {
-       /* XXX: The context could be allocated on the heap.  */
-       struct lzms_decompressor ctx;
-
        /* Initialize the LZMS decompressor.  */
-       lzms_init_decompressor(&ctx, cdata, clen, ubuf, ulen);
+       lzms_init_decompressor(ctx, cdata, clen, ubuf, ulen);
 
        /* Decode the sequence of items.  */
-       while (ctx.out_next != ctx.out_end) {
-               LZMS_DEBUG("Position %u", ctx.out_next - ctx.out_begin);
-               if (lzms_decode_item(&ctx))
+       while (ctx->out_next != ctx->out_end) {
+               LZMS_DEBUG("Position %u", ctx->out_next - ctx->out_begin);
+               if (lzms_decode_item(ctx))
                        return -1;
        }
        return 0;
@@ -1224,17 +1218,15 @@ lzms_process_x86_translation(u8 *ubuf, s32 i, s32 *closest_target_usage_p,
  * actually needs to be done (or to plug in alternate filters, like in LZMA),
  * and the corresponding preprocessing seems to be done unconditionally.  */
 static void
-lzms_postprocess_data(u8 *ubuf, s32 ulen)
+lzms_postprocess_data(u8 *ubuf, s32 ulen, s32 *last_target_usages)
 {
        /* Offset (from beginning of buffer) of the most recent reference to a
         * seemingly valid target address.  */
        s32 closest_target_usage = -LZMS_X86_MAX_TRANSLATION_OFFSET - 1;
 
-       /* Offset (from beginning of buffer) of the most recently used target
-        * address beginning with two bytes equal to the array index.
-        *
-        * XXX: This array could be allocated on the heap.  */
-       s32 last_target_usages[65536];
+       /* Initialize the last_target_usages array.  Each entry will contain the
+        * offset (from beginning of buffer) of the most recently used target
+        * address beginning with two bytes equal to the array index.  */
        for (s32 i = 0; i < 65536; i++)
                last_target_usages[i] = -LZMS_X86_MAX_GOOD_TARGET_OFFSET - 1;
 
@@ -1246,48 +1238,81 @@ lzms_postprocess_data(u8 *ubuf, s32 ulen)
                                                 last_target_usages);
 }
 
-/* API function documented in wimlib.h  */
-WIMLIBAPI int
-wimlib_lzms_decompress(const void *cdata, unsigned clen,
-                      void *ubuf, unsigned ulen)
+static int
+lzms_decompress(const void *compressed_data, size_t compressed_size,
+               void *uncompressed_data, size_t uncompressed_size, void *_ctx)
 {
+       struct lzms_decompressor *ctx = _ctx;
+
        /* The range decoder requires that a minimum of 4 bytes of compressed
         * data be initially available.  */
-       if (clen < 4) {
-               LZMS_DEBUG("Compressed length too small (got %u, expected >= 4)",
-                          clen);
+       if (compressed_size < 4) {
+               LZMS_DEBUG("Compressed size too small (got %zu, expected >= 4)",
+                          compressed_size);
                return -1;
        }
 
        /* A LZMS-compressed data block should be evenly divisible into 16-bit
         * integers.  */
-       if (clen % 2 != 0) {
-               LZMS_DEBUG("Compressed length not divisible by 2 (got %u)", clen);
+       if (compressed_size % 2 != 0) {
+               LZMS_DEBUG("Compressed size not divisible by 2 (got %zu)",
+                          compressed_size);
                return -1;
        }
 
        /* Handle the trivial case where nothing needs to be decompressed.
         * (Necessary because a window of size 0 does not have a valid position
         * slot.)  */
-       if (ulen == 0)
+       if (uncompressed_size == 0)
                return 0;
 
        /* The x86 post-processor requires that the uncompressed length fit into
         * a signed 32-bit integer.  Also, the position slot table cannot be
         * searched for a position of INT32_MAX or greater.  */
-       if (ulen >= INT32_MAX) {
+       if (uncompressed_size >= INT32_MAX) {
                LZMS_DEBUG("Uncompressed length too large "
                           "(got %u, expected < INT32_MAX)", ulen);
                return -1;
        }
 
        /* Decode the literals and matches.  */
-       if (lzms_decode_items(cdata, clen, ubuf, ulen))
+       if (lzms_decode_items(compressed_data, compressed_size,
+                             uncompressed_data, uncompressed_size, ctx))
                return -1;
 
        /* Postprocess the data.  */
-       lzms_postprocess_data(ubuf, ulen);
+       lzms_postprocess_data(uncompressed_data, uncompressed_size,
+                             ctx->last_target_usages);
 
        LZMS_DEBUG("Decompression successful.");
        return 0;
 }
+
+static void
+lzms_free_decompressor(void *_ctx)
+{
+       struct lzms_decompressor *ctx = _ctx;
+
+       FREE(ctx);
+}
+
+static int
+lzms_create_decompressor(size_t max_block_size,
+                        const struct wimlib_decompressor_params_header *params,
+                        void **ctx_ret)
+{
+       struct lzms_decompressor *ctx;
+
+       ctx = MALLOC(sizeof(struct lzms_decompressor));
+       if (ctx == NULL)
+               return WIMLIB_ERR_NOMEM;
+
+       *ctx_ret = ctx;
+       return 0;
+}
+
+const struct decompressor_ops lzms_decompressor_ops = {
+       .create_decompressor  = lzms_create_decompressor,
+       .decompress           = lzms_decompress,
+       .free_decompressor    = lzms_free_decompressor,
+};