/*
* lzms-decompress.c
- *
- * A decompressor for the LZMS compression format.
*/
/*
#include "wimlib/util.h"
#include <limits.h>
-#include <pthread.h>
#define LZMS_DECODE_TABLE_BITS 10
size_t num_le16_remaining;
};
-/* Probability entry for use by the range decoder when in a specific state. */
-struct lzms_probability_entry {
-
- /* Number of zeroes in the most recent LZMS_PROBABILITY_MAX bits that
- * have been decoded using this probability entry. This is a cached
- * value because it can be computed as LZMS_PROBABILITY_MAX minus the
- * Hamming weight of the low-order LZMS_PROBABILITY_MAX bits of
- * @recent_bits. */
- u32 num_recent_zero_bits;
-
- /* The most recent LZMS_PROBABILITY_MAX bits that have been decoded
- * using this probability entry. The size of this variable, in bits,
- * must be at least LZMS_PROBABILITY_MAX. */
- u64 recent_bits;
-};
-
/* Structure used for range decoding. This wraps around `struct
* lzms_range_decoder_raw' to use and maintain probability entries. */
struct lzms_range_decoder {
struct lzms_huffman_decoder delta_power_decoder;
struct lzms_huffman_decoder delta_offset_decoder;
- /* LRU (least-recently-used) queue of LZ match offsets. */
- u64 recent_lz_offsets[LZMS_NUM_RECENT_OFFSETS + 1];
-
- /* LRU (least-recently-used) queue of delta match powers. */
- u32 recent_delta_powers[LZMS_NUM_RECENT_OFFSETS + 1];
-
- /* LRU (least-recently-used) queue of delta match offsets. */
- u32 recent_delta_offsets[LZMS_NUM_RECENT_OFFSETS + 1];
-
- /* These variables are used to delay updates to the LRU queues by one
- * decoded item. */
- u32 prev_lz_offset;
- u32 prev_delta_power;
- u32 prev_delta_offset;
- u32 upcoming_lz_offset;
- u32 upcoming_delta_power;
- u32 upcoming_delta_offset;
+ /* LRU (least-recently-used) queues for match information. */
+ struct lzms_lru_queues lru;
- /* Used for postprocessing */
+ /* Used for postprocessing. */
s32 last_target_usages[65536];
};
-/* A table that maps position slots to their base values. These are constants
- * computed at runtime by lzms_compute_slot_bases(). */
-static u32 lzms_position_slot_base[LZMS_MAX_NUM_OFFSET_SYMS + 1];
-
-/* A table that maps length slots to their base values. These are constants
- * computed at runtime by lzms_compute_slot_bases(). */
-static u32 lzms_length_slot_base[LZMS_NUM_LEN_SYMS + 1];
-
-static void
-lzms_decode_delta_rle_slot_bases(u32 slot_bases[],
- const u8 delta_run_lens[], size_t num_run_lens)
-{
- u32 delta = 1;
- u32 base = 0;
- size_t slot = 0;
- for (size_t i = 0; i < num_run_lens; i++) {
- u8 run_len = delta_run_lens[i];
- while (run_len--) {
- base += delta;
- slot_bases[slot++] = base;
- }
- delta <<= 1;
- }
-}
-
-/* Initialize the global position and length slot tables. */
-static void
-lzms_compute_slot_bases(void)
-{
- /* If an explicit formula that maps LZMS position and length slots to
- * slot bases exists, then it could be used here. But until one is
- * found, the following code fills in the slots using the observation
- * that the increase from one slot base to the next is an increasing
- * power of 2. Therefore, run-length encoding of the delta of adjacent
- * entries can be used. */
- static const u8 position_slot_delta_run_lens[] = {
- 9, 0, 9, 7, 10, 15, 15, 20,
- 20, 30, 33, 40, 42, 45, 60, 73,
- 80, 85, 95, 105, 6,
- };
-
- static const u8 length_slot_delta_run_lens[] = {
- 27, 4, 6, 4, 5, 2, 1, 1,
- 1, 1, 1, 0, 0, 0, 0, 0,
- 1,
- };
-
- lzms_decode_delta_rle_slot_bases(lzms_position_slot_base,
- position_slot_delta_run_lens,
- ARRAY_LEN(position_slot_delta_run_lens));
-
- lzms_position_slot_base[LZMS_MAX_NUM_OFFSET_SYMS] = 0x7fffffff;
-
- lzms_decode_delta_rle_slot_bases(lzms_length_slot_base,
- length_slot_delta_run_lens,
- ARRAY_LEN(length_slot_delta_run_lens));
-
- lzms_length_slot_base[LZMS_NUM_LEN_SYMS] = 0x400108ab;
-}
-
-/* Initialize the global position length slot tables if not done so already. */
-static void
-lzms_init_slot_bases(void)
-{
- static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
- static bool already_computed = false;
-
- if (unlikely(!already_computed)) {
- pthread_mutex_lock(&mutex);
- if (!already_computed) {
- lzms_compute_slot_bases();
- already_computed = true;
- }
- pthread_mutex_unlock(&mutex);
- }
-}
-
-/* Return the position slot for the specified offset. */
-static u32
-lzms_get_position_slot_raw(u32 offset)
-{
- u32 position_slot = 0;
- while (lzms_position_slot_base[position_slot + 1] <= offset)
- position_slot++;
- return position_slot;
-}
-
/* Initialize the input bitstream @is to read forwards from the specified
* compressed data buffer @in that is @in_limit 16-bit integers long. */
static void
static void
lzms_rebuild_adaptive_huffman_code(struct lzms_huffman_decoder *dec)
{
- int ret;
/* XXX: This implementation makes use of code already implemented for
* the XPRESS and LZX compression formats. However, since for the
dec->num_syms);
make_canonical_huffman_code(dec->num_syms, LZMS_MAX_CODEWORD_LEN,
dec->sym_freqs, dec->lens, dec->codewords);
- ret = make_huffman_decode_table(dec->decode_table, dec->num_syms,
- LZMS_DECODE_TABLE_BITS, dec->lens,
- LZMS_MAX_CODEWORD_LEN);
+#if defined(ENABLE_LZMS_DEBUG)
+ int ret =
+#endif
+ make_huffman_decode_table(dec->decode_table, dec->num_syms,
+ LZMS_DECODE_TABLE_BITS, dec->lens,
+ LZMS_MAX_CODEWORD_LEN);
LZMS_ASSERT(ret == 0);
}
/* Decode and return the next Huffman-encoded symbol from the LZMS-compressed
* block using the specified Huffman decoder. */
static u32
-lzms_decode_huffman_symbol(struct lzms_huffman_decoder *dec)
+lzms_huffman_decode_symbol(struct lzms_huffman_decoder *dec)
{
const u8 *lens = dec->lens;
const u16 *decode_table = dec->decode_table;
/* Read the slot (position slot, length slot, etc.), which is encoded as
* a Huffman symbol. */
- slot = lzms_decode_huffman_symbol(dec);
+ slot = lzms_huffman_decode_symbol(dec);
LZMS_ASSERT(dec->slot_base_tab != NULL);
if (!lzms_range_decode_bit(&ctx->lz_repeat_match_range_decoders[i]))
break;
- offset = ctx->recent_lz_offsets[i];
+ offset = ctx->lru.lz.recent_offsets[i];
for (; i < LZMS_NUM_RECENT_OFFSETS; i++)
- ctx->recent_lz_offsets[i] = ctx->recent_lz_offsets[i + 1];
+ ctx->lru.lz.recent_offsets[i] = ctx->lru.lz.recent_offsets[i + 1];
}
/* Decode match length, which is always given explicitly (there is no
* LRU queue for repeat lengths). */
length = lzms_decode_value(&ctx->length_decoder);
- ctx->upcoming_lz_offset = offset;
+ ctx->lru.lz.upcoming_offset = offset;
LZMS_DEBUG("Decoded %s LZ match: length=%u, offset=%u",
(bit ? "repeat" : "explicit"), length, offset);
bit = lzms_range_decode_bit(&ctx->delta_match_range_decoder);
if (bit == 0) {
- power = lzms_decode_huffman_symbol(&ctx->delta_power_decoder);
+ power = lzms_huffman_decode_symbol(&ctx->delta_power_decoder);
raw_offset = lzms_decode_value(&ctx->delta_offset_decoder);
} else {
int i;
if (!lzms_range_decode_bit(&ctx->delta_repeat_match_range_decoders[i]))
break;
- power = ctx->recent_delta_powers[i];
- raw_offset = ctx->recent_delta_offsets[i];
+ power = ctx->lru.delta.recent_powers[i];
+ raw_offset = ctx->lru.delta.recent_offsets[i];
for (; i < LZMS_NUM_RECENT_OFFSETS; i++) {
- ctx->recent_delta_powers[i] = ctx->recent_delta_powers[i + 1];
- ctx->recent_delta_offsets[i] = ctx->recent_delta_offsets[i + 1];
+ ctx->lru.delta.recent_powers[i] = ctx->lru.delta.recent_powers[i + 1];
+ ctx->lru.delta.recent_offsets[i] = ctx->lru.delta.recent_offsets[i + 1];
}
}
length = lzms_decode_value(&ctx->length_decoder);
- ctx->upcoming_delta_power = power;
- ctx->upcoming_delta_offset = raw_offset;
+ ctx->lru.delta.upcoming_power = power;
+ ctx->lru.delta.upcoming_offset = raw_offset;
LZMS_DEBUG("Decoded %s delta match: length=%u, power=%u, raw_offset=%u",
(bit ? "repeat" : "explicit"), length, power, raw_offset);
static int
lzms_decode_literal(struct lzms_decompressor *ctx)
{
- u8 literal = lzms_decode_huffman_symbol(&ctx->literal_decoder);
+ u8 literal = lzms_huffman_decode_symbol(&ctx->literal_decoder);
LZMS_DEBUG("Decoded literal: 0x%02x", literal);
return lzms_copy_literal(ctx, literal);
}
{
int ret;
- ctx->upcoming_delta_offset = 0;
- ctx->upcoming_lz_offset = 0;
- ctx->upcoming_delta_power = 0;
+ ctx->lru.lz.upcoming_offset = 0;
+ ctx->lru.delta.upcoming_power = 0;
+ ctx->lru.delta.upcoming_offset = 0;
if (lzms_range_decode_bit(&ctx->main_range_decoder))
ret = lzms_decode_match(ctx);
return ret;
/* Update LRU queues */
- if (ctx->prev_lz_offset != 0) {
- 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) {
- for (int i = LZMS_NUM_RECENT_OFFSETS - 1; i >= 0; i--) {
- ctx->recent_delta_powers[i + 1] = ctx->recent_delta_powers[i];
- ctx->recent_delta_offsets[i + 1] = ctx->recent_delta_offsets[i];
- }
- ctx->recent_delta_powers[0] = ctx->prev_delta_power;
- ctx->recent_delta_offsets[0] = ctx->prev_delta_offset;
- }
-
- ctx->prev_lz_offset = ctx->upcoming_lz_offset;
- ctx->prev_delta_offset = ctx->upcoming_delta_offset;
- ctx->prev_delta_power = ctx->upcoming_delta_power;
+ lzms_update_lru_queues(&ctx->lru);
return 0;
}
/* Calculate the number of position slots needed for this compressed
* block. */
- num_position_slots = lzms_get_position_slot_raw(ulen - 1) + 1;
+ num_position_slots = lzms_get_position_slot(ulen - 1) + 1;
LZMS_DEBUG("Using %u position slots", num_position_slots);
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;
-
- for (size_t i = 0; i < LZMS_NUM_RECENT_OFFSETS + 1; i++) {
- ctx->recent_delta_powers[i] = 0;
- ctx->recent_delta_offsets[i] = i + 1;
- }
- ctx->prev_lz_offset = 0;
- ctx->prev_delta_offset = 0;
- ctx->prev_delta_power = 0;
- ctx->upcoming_lz_offset = 0;
- ctx->upcoming_delta_offset = 0;
- ctx->upcoming_delta_power = 0;
+ /* Initialize LRU match information. */
+ lzms_init_lru_queues(&ctx->lru);
LZMS_DEBUG("Decompressor successfully initialized");
}
return 0;
}
-static s32
-lzms_try_x86_translation(u8 *ubuf, s32 i, s32 num_op_bytes,
- s32 *closest_target_usage_p, s32 last_target_usages[],
- s32 max_trans_offset)
-{
- u16 pos;
-
- if (i - *closest_target_usage_p <= max_trans_offset) {
- LZMS_DEBUG("Performed x86 translation at position %d "
- "(opcode 0x%02x)", i, ubuf[i]);
- le32 *p32 = (le32*)&ubuf[i + num_op_bytes];
- u32 n = le32_to_cpu(*p32);
- *p32 = cpu_to_le32(n - i);
- }
-
- pos = i + le16_to_cpu(*(const le16*)&ubuf[i + num_op_bytes]);
-
- i += num_op_bytes + sizeof(le32) - 1;
-
- if (i - last_target_usages[pos] <= LZMS_X86_MAX_GOOD_TARGET_OFFSET)
- *closest_target_usage_p = i;
-
- last_target_usages[pos] = i;
-
- return i + 1;
-}
-
-static s32
-lzms_process_x86_translation(u8 *ubuf, s32 i, s32 *closest_target_usage_p,
- s32 last_target_usages[])
-{
- /* Switch on first byte of the opcode, assuming it is really an x86
- * instruction. */
- switch (ubuf[i]) {
- case 0x48:
- if (ubuf[i + 1] == 0x8b) {
- if (ubuf[i + 2] == 0x5 || ubuf[i + 2] == 0xd) {
- /* Load relative (x86_64) */
- return lzms_try_x86_translation(ubuf, i, 3,
- closest_target_usage_p,
- last_target_usages,
- LZMS_X86_MAX_TRANSLATION_OFFSET);
- }
- } else if (ubuf[i + 1] == 0x8d) {
- if ((ubuf[i + 2] & 0x7) == 0x5) {
- /* Load effective address relative (x86_64) */
- return lzms_try_x86_translation(ubuf, i, 3,
- closest_target_usage_p,
- last_target_usages,
- LZMS_X86_MAX_TRANSLATION_OFFSET);
- }
- }
- break;
-
- case 0x4c:
- if (ubuf[i + 1] == 0x8d) {
- if ((ubuf[i + 2] & 0x7) == 0x5) {
- /* Load effective address relative (x86_64) */
- return lzms_try_x86_translation(ubuf, i, 3,
- closest_target_usage_p,
- last_target_usages,
- LZMS_X86_MAX_TRANSLATION_OFFSET);
- }
- }
- break;
-
- case 0xe8:
- /* Call relative */
- return lzms_try_x86_translation(ubuf, i, 1, closest_target_usage_p,
- last_target_usages,
- LZMS_X86_MAX_TRANSLATION_OFFSET / 2);
-
- case 0xe9:
- /* Jump relative */
- return i + 5;
-
- case 0xf0:
- if (ubuf[i + 1] == 0x83 && ubuf[i + 2] == 0x05) {
- /* Lock add relative */
- return lzms_try_x86_translation(ubuf, i, 3,
- closest_target_usage_p,
- last_target_usages,
- LZMS_X86_MAX_TRANSLATION_OFFSET);
- }
- break;
-
- case 0xff:
- if (ubuf[i + 1] == 0x15) {
- /* Call indirect */
- return lzms_try_x86_translation(ubuf, i, 2,
- closest_target_usage_p,
- last_target_usages,
- LZMS_X86_MAX_TRANSLATION_OFFSET);
- }
- break;
- }
- return i + 1;
-}
-
-/* Postprocess the uncompressed data by undoing the translation of relative
- * addresses embedded in x86 instructions into absolute addresses.
- *
- * There does not appear to be any way to check to see if this postprocessing
- * 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, 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;
-
- /* 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;
-
- /* Check each byte in the buffer for an x86 opcode for which a
- * translation may be possible. No translations are done on any
- * instructions starting in the last 11 bytes of the buffer. */
- for (s32 i = 0; i < ulen - 11; )
- i = lzms_process_x86_translation(ubuf, i, &closest_target_usage,
- last_target_usages);
-}
-
static int
lzms_decompress(const void *compressed_data, size_t compressed_size,
void *uncompressed_data, size_t uncompressed_size, void *_ctx)
* searched for a position of INT32_MAX or greater. */
if (uncompressed_size >= INT32_MAX) {
LZMS_DEBUG("Uncompressed length too large "
- "(got %u, expected < INT32_MAX)", ulen);
+ "(got %zu, expected < INT32_MAX)",
+ uncompressed_size);
return -1;
}
return -1;
/* Postprocess the data. */
- lzms_postprocess_data(uncompressed_data, uncompressed_size,
- ctx->last_target_usages);
+ lzms_x86_filter(uncompressed_data, uncompressed_size,
+ ctx->last_target_usages, true);
LZMS_DEBUG("Decompression successful.");
return 0;