83 #define BITS_TYPE struct lzxd_stream
85 #define BITS_ORDER_MSB
86 #define READ_BYTES do { \
87 unsigned char b0, b1; \
88 READ_IF_NEEDED; b0 = *i_ptr++; \
89 READ_IF_NEEDED; b1 = *i_ptr++; \
90 INJECT_BITS((b1 << 8) | b0, 16); \
95 #define TABLEBITS(tbl) LZX_##tbl##_TABLEBITS
96 #define MAXSYMBOLS(tbl) LZX_##tbl##_MAXSYMBOLS
97 #define HUFF_TABLE(tbl,idx) lzx->tbl##_table[idx]
98 #define HUFF_LEN(tbl,idx) lzx->tbl##_len[idx]
99 #define HUFF_ERROR return lzx->error = MSPACK_ERR_DECRUNCH
103 #define BUILD_TABLE(tbl) \
104 if (make_decode_table(MAXSYMBOLS(tbl), TABLEBITS(tbl), \
105 &HUFF_LEN(tbl,0), &HUFF_TABLE(tbl,0))) \
107 D(("failed to build %s table", #tbl)) \
108 return lzx->error = MSPACK_ERR_DECRUNCH; \
111 #define BUILD_TABLE_MAYBE_EMPTY(tbl) do { \
112 lzx->tbl##_empty = 0; \
113 if (make_decode_table(MAXSYMBOLS(tbl), TABLEBITS(tbl), \
114 &HUFF_LEN(tbl,0), &HUFF_TABLE(tbl,0))) \
116 for (i = 0; i < MAXSYMBOLS(tbl); i++) { \
117 if (HUFF_LEN(tbl, i) > 0) { \
118 D(("failed to build %s table", #tbl)) \
119 return lzx->error = MSPACK_ERR_DECRUNCH; \
123 lzx->tbl##_empty = 1; \
131 #define READ_LENGTHS(tbl, first, last) do { \
133 if (lzxd_read_lens(lzx, &HUFF_LEN(tbl, 0), (first), \
134 (unsigned int)(last))) return lzx->error; \
139 unsigned int first,
unsigned int last)
142 register unsigned int bit_buffer;
143 register int bits_left,
i;
144 register unsigned short sym;
145 unsigned char *i_ptr, *i_end;
153 for (
x = 0;
x < 20;
x++) {
159 for (
x = first;
x < last; ) {
164 while (y--) lens[
x++] = 0;
169 while (y--) lens[
x++] = 0;
175 z = lens[
x] - z;
if (z < 0) z += 17;
176 while (y--) lens[
x++] = z;
180 z = lens[
x] - z;
if (z < 0) z += 17;
215 30, 32, 34, 36, 38, 42, 50, 66, 98, 162, 290
218 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8,
219 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16
222 0, 1, 2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128, 192, 256, 384, 512,
223 768, 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576, 32768,
224 49152, 65536, 98304, 131072, 196608, 262144, 393216, 524288, 655360,
225 786432, 917504, 1048576, 1179648, 1310720, 1441792, 1572864, 1703936,
226 1835008, 1966080, 2097152, 2228224, 2359296, 2490368, 2621440, 2752512,
227 2883584, 3014656, 3145728, 3276800, 3407872, 3538944, 3670016, 3801088,
228 3932160, 4063232, 4194304, 4325376, 4456448, 4587520, 4718592, 4849664,
229 4980736, 5111808, 5242880, 5373952, 5505024, 5636096, 5767168, 5898240,
230 6029312, 6160384, 6291456, 6422528, 6553600, 6684672, 6815744, 6946816,
231 7077888, 7208960, 7340032, 7471104, 7602176, 7733248, 7864320, 7995392,
232 8126464, 8257536, 8388608, 8519680, 8650752, 8781824, 8912896, 9043968,
233 9175040, 9306112, 9437184, 9568256, 9699328, 9830400, 9961472, 10092544,
234 10223616, 10354688, 10485760, 10616832, 10747904, 10878976, 11010048,
235 11141120, 11272192, 11403264, 11534336, 11665408, 11796480, 11927552,
236 12058624, 12189696, 12320768, 12451840, 12582912, 12713984, 12845056,
237 12976128, 13107200, 13238272, 13369344, 13500416, 13631488, 13762560,
238 13893632, 14024704, 14155776, 14286848, 14417920, 14548992, 14680064,
239 14811136, 14942208, 15073280, 15204352, 15335424, 15466496, 15597568,
240 15728640, 15859712, 15990784, 16121856, 16252928, 16384000, 16515072,
241 16646144, 16777216, 16908288, 17039360, 17170432, 17301504, 17432576,
242 17563648, 17694720, 17825792, 17956864, 18087936, 18219008, 18350080,
243 18481152, 18612224, 18743296, 18874368, 19005440, 19136512, 19267584,
244 19398656, 19529728, 19660800, 19791872, 19922944, 20054016, 20185088,
245 20316160, 20447232, 20578304, 20709376, 20840448, 20971520, 21102592,
246 21233664, 21364736, 21495808, 21626880, 21757952, 21889024, 22020096,
247 22151168, 22282240, 22413312, 22544384, 22675456, 22806528, 22937600,
248 23068672, 23199744, 23330816, 23461888, 23592960, 23724032, 23855104,
249 23986176, 24117248, 24248320, 24379392, 24510464, 24641536, 24772608,
250 24903680, 25034752, 25165824, 25296896, 25427968, 25559040, 25690112,
251 25821184, 25952256, 26083328, 26214400, 26345472, 26476544, 26607616,
252 26738688, 26869760, 27000832, 27131904, 27262976, 27394048, 27525120,
253 27656192, 27787264, 27918336, 28049408, 28180480, 28311552, 28442624,
254 28573696, 28704768, 28835840, 28966912, 29097984, 29229056, 29360128,
255 29491200, 29622272, 29753344, 29884416, 30015488, 30146560, 30277632,
256 30408704, 30539776, 30670848, 30801920, 30932992, 31064064, 31195136,
257 31326208, 31457280, 31588352, 31719424, 31850496, 31981568, 32112640,
258 32243712, 32374784, 32505856, 32636928, 32768000, 32899072, 33030144,
259 33161216, 33292288, 33423360
284 int input_buffer_size,
291 if (!system)
return NULL;
297 if (window_bits < 17 || window_bits > 25)
return NULL;
300 if (window_bits < 15 || window_bits > 21)
return NULL;
304 D((
"reset interval or output length < 0"))
309 input_buffer_size = (input_buffer_size + 1) & -2;
310 if (input_buffer_size < 2)
return NULL;
319 lzx->
inbuf = (
unsigned char *) system->
alloc(system, (
size_t) input_buffer_size);
332 lzx->
length = output_length;
361 D((
"only LZX DELTA streams support reference data"))
365 D((
"too late to set reference data after decoding starts"))
369 D((
"reference length (%u) is longer than the window",
length))
373 D((
"length > 0 but no system or input"))
390 if (lzx && out_bytes > 0) lzx->
length = out_bytes;
398 register unsigned short sym;
400 int match_length, length_footer, extra, verbatim_bits, bytes_todo;
401 int this_run, main_element, aligned_bits, j, warned = 0;
402 unsigned char *
window, *runsrc, *rundest,
buf[12];
403 unsigned int frame_size=0, end_frame, match_offset,
window_posn;
412 if ((
off_t)
i > out_bytes)
i = (
int) out_bytes;
433 while (lzx->
frame < end_frame) {
440 lzx->
sys->
message(
NULL,
"WARNING; invalid reset interval detected during LZX decompression");
477 while (bytes_todo > 0) {
522 for (rundest = &
buf[0],
i = 0;
i < 12;
i++) {
524 *rundest++ = *
i_ptr++;
532 D((
"bad block type"))
540 if (this_run > bytes_todo) this_run = bytes_todo;
543 bytes_todo -= this_run;
550 while (this_run > 0) {
565 D((
"LENGTH symbol needed but tree is empty"))
569 match_length += length_footer;
574 switch ((match_offset = (main_element >> 3))) {
575 case 0: match_offset =
R0;
break;
576 case 1: match_offset =
R1;
R1=
R0;
R0 = match_offset;
break;
577 case 2: match_offset =
R2;
R2=
R0;
R0 = match_offset;
break;
580 if (match_offset == 3) {
584 extra = (match_offset >= 36) ? 17 :
extra_bits[match_offset];
586 match_offset =
position_base[match_offset] - 2 + verbatim_bits;
590 extra = (match_offset >= 36) ? 17 :
extra_bits[match_offset];
595 match_offset += (verbatim_bits << 3);
597 match_offset += aligned_bits;
599 else if (extra == 3) {
601 match_offset += aligned_bits;
603 else if (extra > 0) {
605 match_offset += verbatim_bits;
637 match_length += extra_len;
641 D((
"match ran over window wrap"))
650 if (match_offset > lzx->
offset &&
653 D((
"match offset beyond LZX stream"))
659 D((
"match offset beyond window boundaries"))
665 i -= j;
while (j-- > 0) *rundest++ = *runsrc++;
668 while (
i-- > 0) *rundest++ = *runsrc++;
671 runsrc = rundest - match_offset;
672 while (
i-- > 0) *rundest++ = *runsrc++;
675 this_run -= match_length;
686 while (this_run > 0) {
691 if (
i > this_run)
i = this_run;
707 D((
"overrun went past end of block by %d (%d remaining)",
717 D((
"decode beyond output frame limits! %d != %d",
728 D((
"%ld avail bytes, new %d frame",
735 (lzx->
frame < 32768) && (frame_size > 10))
737 unsigned char *data = &lzx->
e8_buf[0];
738 unsigned char *dataend = &lzx->
e8_buf[frame_size - 10];
741 signed int abs_off, rel_off;
747 while (data < dataend) {
748 if (*data++ != 0xE8) { curpos++;
continue; }
749 abs_off = data[0] | (data[1]<<8) | (data[2]<<16) | (data[3]<<24);
750 if ((abs_off >= -curpos) && (abs_off < filesize)) {
751 rel_off = (abs_off >= 0) ? abs_off - curpos : abs_off + filesize;
752 data[0] = (
unsigned char) rel_off;
753 data[1] = (
unsigned char) (rel_off >> 8);
754 data[2] = (
unsigned char) (rel_off >> 16);
755 data[3] = (
unsigned char) (rel_off >> 24);
767 i = (out_bytes < (
off_t)frame_size) ? (
unsigned int)out_bytes : frame_size;
786 D((
"bytes left to output"))
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void length
unsigned char LENGTH_len[LZX_LENGTH_MAXSYMBOLS+LZX_LENTABLE_SAFETY]
struct mspack_system * sys
unsigned char PRETREE_len[LZX_PRETREE_MAXSYMBOLS+LZX_LENTABLE_SAFETY]
unsigned char intel_started
unsigned int ref_data_size
struct mspack_file * output
struct mspack_file * input
unsigned int block_remaining
unsigned char ALIGNED_len[LZX_ALIGNED_MAXSYMBOLS+LZX_LENTABLE_SAFETY]
signed int intel_filesize
unsigned char e8_buf[LZX_FRAME_SIZE]
unsigned char MAINTREE_len[LZX_MAINTREE_MAXSYMBOLS+LZX_LENTABLE_SAFETY]
unsigned int reset_interval
unsigned int block_length
unsigned char LENGTH_empty
unsigned char header_read
void(* copy)(void *src, void *dest, size_t bytes)
void(* message)(struct mspack_file *file, const char *format,...)
int(* read)(struct mspack_file *file, void *buffer, int bytes)
int(* write)(struct mspack_file *file, void *buffer, int bytes)
void *(* alloc)(struct mspack_system *self, size_t bytes)
static bool input(void *ud, zip_uint8_t *data, zip_uint64_t length)