4 * Code shared between the compressor and decompressor for the LZMS compression
9 * Copyright (C) 2013, 2014 Eric Biggers
11 * This file is free software; you can redistribute it and/or modify it under
12 * the terms of the GNU Lesser General Public License as published by the Free
13 * Software Foundation; either version 3 of the License, or (at your option) any
16 * This file is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this file; if not, see http://www.gnu.org/licenses/.
29 #include "wimlib/endianness.h"
30 #include "wimlib/lzms.h"
31 #include "wimlib/util.h"
35 /***************************************************************
36 * Constant tables initialized by lzms_compute_slots(): *
37 ***************************************************************/
39 /* Table: offset slot => offset slot base value */
40 u32 lzms_offset_slot_base[LZMS_MAX_NUM_OFFSET_SYMS + 1];
42 /* Table: offset slot => number of extra offset bits */
43 u8 lzms_extra_offset_bits[LZMS_MAX_NUM_OFFSET_SYMS];
45 /* Table: length slot => length slot base value */
46 u32 lzms_length_slot_base[LZMS_NUM_LEN_SYMS + 1];
48 /* Table: length slot => number of extra length bits */
49 u8 lzms_extra_length_bits[LZMS_NUM_LEN_SYMS];
52 lzms_get_slot(u32 value, const u32 slot_base_tab[], unsigned num_slots)
55 unsigned r = num_slots - 1;
58 unsigned slot = (l + r) / 2;
59 if (value >= slot_base_tab[slot]) {
60 if (value < slot_base_tab[slot + 1])
71 lzms_decode_delta_rle_slot_bases(u32 slot_bases[],
73 const u8 delta_run_lens[],
74 unsigned num_run_lens,
76 unsigned expected_num_slots)
82 for (unsigned i = 0; i < num_run_lens; i++) {
83 unsigned run_len = delta_run_lens[i];
87 extra_bits[slot - 1] = order;
88 slot_bases[slot] = base;
94 LZMS_ASSERT(slot == expected_num_slots);
96 slot_bases[slot] = final;
97 extra_bits[slot - 1] = bsr32(slot_bases[slot] - slot_bases[slot - 1]);
100 /* Initialize the global offset and length slot tables. */
102 lzms_compute_slots(void)
104 /* If an explicit formula that maps LZMS offset and length slots to slot
105 * bases exists, then it could be used here. But until one is found,
106 * the following code fills in the slots using the observation that the
107 * increase from one slot base to the next is an increasing power of 2.
108 * Therefore, run-length encoding of the delta of adjacent entries can
110 static const u8 offset_slot_delta_run_lens[] = {
111 9, 0, 9, 7, 10, 15, 15, 20,
112 20, 30, 33, 40, 42, 45, 60, 73,
116 static const u8 length_slot_delta_run_lens[] = {
117 27, 4, 6, 4, 5, 2, 1, 1,
118 1, 1, 1, 0, 0, 0, 0, 0,
123 lzms_decode_delta_rle_slot_bases(lzms_offset_slot_base,
124 lzms_extra_offset_bits,
125 offset_slot_delta_run_lens,
126 ARRAY_LEN(offset_slot_delta_run_lens),
128 LZMS_MAX_NUM_OFFSET_SYMS);
131 lzms_decode_delta_rle_slot_bases(lzms_length_slot_base,
132 lzms_extra_length_bits,
133 length_slot_delta_run_lens,
134 ARRAY_LEN(length_slot_delta_run_lens),
139 /* Initialize the global offset and length slot tables if not already done. */
141 lzms_init_slots(void)
143 static pthread_once_t once = PTHREAD_ONCE_INIT;
145 pthread_once(&once, lzms_compute_slots);
149 lzms_maybe_do_x86_translation(u8 data[restrict], s32 i, s32 num_op_bytes,
150 s32 * restrict closest_target_usage_p,
151 s32 last_target_usages[restrict],
152 s32 max_trans_offset, bool undo)
157 if (i - *closest_target_usage_p <= max_trans_offset) {
158 LZMS_DEBUG("Undid x86 translation at position %d "
159 "(opcode 0x%02x)", i, data[i]);
160 le32 *p32 = (le32*)&data[i + num_op_bytes];
161 u32 n = le32_to_cpu(*p32);
162 *p32 = cpu_to_le32(n - i);
164 pos = i + le16_to_cpu(*(const le16*)&data[i + num_op_bytes]);
166 pos = i + le16_to_cpu(*(const le16*)&data[i + num_op_bytes]);
168 if (i - *closest_target_usage_p <= max_trans_offset) {
169 LZMS_DEBUG("Did x86 translation at position %d "
170 "(opcode 0x%02x)", i, data[i]);
171 le32 *p32 = (le32*)&data[i + num_op_bytes];
172 u32 n = le32_to_cpu(*p32);
173 *p32 = cpu_to_le32(n + i);
177 i += num_op_bytes + sizeof(le32) - 1;
179 if (i - last_target_usages[pos] <= LZMS_X86_MAX_GOOD_TARGET_OFFSET)
180 *closest_target_usage_p = i;
182 last_target_usages[pos] = i;
188 lzms_may_x86_translate(const u8 p[restrict], s32 *restrict max_offset_ret)
190 /* Switch on first byte of the opcode, assuming it is really an x86
192 *max_offset_ret = LZMS_X86_MAX_TRANSLATION_OFFSET;
196 if (p[2] == 0x5 || p[2] == 0xd) {
197 /* Load relative (x86_64) */
200 } else if (p[1] == 0x8d) {
201 if ((p[2] & 0x7) == 0x5) {
202 /* Load effective address relative (x86_64) */
210 if ((p[2] & 0x7) == 0x5) {
211 /* Load effective address relative (x86_64) */
219 *max_offset_ret = LZMS_X86_MAX_TRANSLATION_OFFSET / 2;
228 if (p[1] == 0x83 && p[2] == 0x05) {
229 /* Lock add relative */
246 * Translate relative addresses embedded in x86 instructions into absolute
247 * addresses (@undo == %false), or undo this translation (@undo == %true).
249 * Absolute addresses are usually more compressible by LZ factorization.
251 * @last_target_usages must be a temporary array of length >= 65536.
254 lzms_x86_filter(u8 data[restrict], s32 size,
255 s32 last_target_usages[restrict], bool undo)
258 * Note: this filter runs unconditionally and uses a custom algorithm to
259 * detect data regions that probably contain x86 code.
261 * 'closest_target_usage' tracks the most recent position that has a
262 * good chance of being an x86 instruction. When the filter detects a
263 * likely x86 instruction, it updates this variable and considers the
264 * next 1023 bytes of data as valid for x86 translations.
266 * If part of the data does not, in fact, contain x86 machine code, then
267 * 'closest_target_usage' will, very likely, eventually fall more than
268 * 1023 bytes behind the current position. This results in x86
269 * translations being disabled until the next likely x86 instruction is
272 * Translations on relative call (e8 opcode) instructions are slightly
273 * more restricted. They require that the most recent likely x86
274 * instruction was in the last 511 bytes, rather than the last 1023
277 * To identify "likely x86 instructions", the algorithm attempts to
278 * track the position of the most recent potential relative-addressing
279 * instruction that referenced each possible memory address. If it
280 * finds two references to the same memory address within a 65535 byte
281 * window, the second reference is flagged as a likely x86 instruction.
282 * Since the instructions considered for translation necessarily use
283 * relative addressing, the algorithm does a tentative translation into
284 * absolute addresses. In addition, so that memory addresses can be
285 * looked up in an array of reasonable size (in this code,
286 * 'last_target_usages'), only the low-order 2 bytes of each address are
287 * considered significant.
290 s32 closest_target_usage = -LZMS_X86_MAX_TRANSLATION_OFFSET - 1;
292 for (s32 i = 0; i < 65536; i++)
293 last_target_usages[i] = -LZMS_X86_MAX_GOOD_TARGET_OFFSET - 1;
295 for (s32 i = 1; i < size - 16; ) {
296 s32 max_trans_offset;
299 n = lzms_may_x86_translate(data + i, &max_trans_offset);
301 if (max_trans_offset) {
302 /* Recognized opcode. */
303 i = lzms_maybe_do_x86_translation(data, i, n,
304 &closest_target_usage,
309 /* Not a recognized opcode. */
316 lzms_init_lz_lru_queues(struct lzms_lz_lru_queues *lz)
318 /* Recent offsets for LZ matches */
319 for (u32 i = 0; i < LZMS_NUM_RECENT_OFFSETS + 1; i++)
320 lz->recent_offsets[i] = i + 1;
323 lz->upcoming_offset = 0;
327 lzms_init_delta_lru_queues(struct lzms_delta_lru_queues *delta)
329 /* Recent offsets and powers for LZ matches */
330 for (u32 i = 0; i < LZMS_NUM_RECENT_OFFSETS + 1; i++) {
331 delta->recent_offsets[i] = i + 1;
332 delta->recent_powers[i] = 0;
334 delta->prev_offset = 0;
335 delta->prev_power = 0;
336 delta->upcoming_offset = 0;
337 delta->upcoming_power = 0;
342 lzms_init_lru_queues(struct lzms_lru_queues *lru)
344 lzms_init_lz_lru_queues(&lru->lz);
345 lzms_init_delta_lru_queues(&lru->delta);
349 lzms_update_lz_lru_queue(struct lzms_lz_lru_queues *lz)
351 if (lz->prev_offset != 0) {
352 for (int i = LZMS_NUM_RECENT_OFFSETS - 1; i >= 0; i--)
353 lz->recent_offsets[i + 1] = lz->recent_offsets[i];
354 lz->recent_offsets[0] = lz->prev_offset;
356 lz->prev_offset = lz->upcoming_offset;
360 lzms_update_delta_lru_queues(struct lzms_delta_lru_queues *delta)
362 if (delta->prev_offset != 0) {
363 for (int i = LZMS_NUM_RECENT_OFFSETS - 1; i >= 0; i--) {
364 delta->recent_offsets[i + 1] = delta->recent_offsets[i];
365 delta->recent_powers[i + 1] = delta->recent_powers[i];
367 delta->recent_offsets[0] = delta->prev_offset;
368 delta->recent_powers[0] = delta->prev_power;
371 delta->prev_offset = delta->upcoming_offset;
372 delta->prev_power = delta->upcoming_power;
376 lzms_update_lru_queues(struct lzms_lru_queues *lru)
378 lzms_update_lz_lru_queue(&lru->lz);
379 lzms_update_delta_lru_queues(&lru->delta);