libdeflate 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/.github/workflows/test.yml +34 -0
- data/README.md +1 -6
- data/ext/libdeflate/extconf.rb +18 -7
- data/ext/libdeflate/libdeflate_ext.c +17 -17
- data/lib/libdeflate/version.rb +1 -1
- data/libdeflate.gemspec +2 -1
- metadata +13 -84
- data/.gitmodules +0 -3
- data/.travis.yml +0 -5
- data/ext/libdeflate/libdeflate/.gitignore +0 -19
- data/ext/libdeflate/libdeflate/COPYING +0 -21
- data/ext/libdeflate/libdeflate/Makefile +0 -231
- data/ext/libdeflate/libdeflate/Makefile.msc +0 -64
- data/ext/libdeflate/libdeflate/NEWS +0 -57
- data/ext/libdeflate/libdeflate/README.md +0 -170
- data/ext/libdeflate/libdeflate/common/common_defs.h +0 -351
- data/ext/libdeflate/libdeflate/common/compiler_gcc.h +0 -134
- data/ext/libdeflate/libdeflate/common/compiler_msc.h +0 -95
- data/ext/libdeflate/libdeflate/lib/adler32.c +0 -213
- data/ext/libdeflate/libdeflate/lib/adler32_impl.h +0 -281
- data/ext/libdeflate/libdeflate/lib/aligned_malloc.c +0 -57
- data/ext/libdeflate/libdeflate/lib/aligned_malloc.h +0 -13
- data/ext/libdeflate/libdeflate/lib/bt_matchfinder.h +0 -357
- data/ext/libdeflate/libdeflate/lib/crc32.c +0 -368
- data/ext/libdeflate/libdeflate/lib/crc32_impl.h +0 -286
- data/ext/libdeflate/libdeflate/lib/crc32_table.h +0 -526
- data/ext/libdeflate/libdeflate/lib/decompress_impl.h +0 -404
- data/ext/libdeflate/libdeflate/lib/deflate_compress.c +0 -2817
- data/ext/libdeflate/libdeflate/lib/deflate_compress.h +0 -14
- data/ext/libdeflate/libdeflate/lib/deflate_constants.h +0 -66
- data/ext/libdeflate/libdeflate/lib/deflate_decompress.c +0 -889
- data/ext/libdeflate/libdeflate/lib/gzip_compress.c +0 -95
- data/ext/libdeflate/libdeflate/lib/gzip_constants.h +0 -45
- data/ext/libdeflate/libdeflate/lib/gzip_decompress.c +0 -130
- data/ext/libdeflate/libdeflate/lib/hc_matchfinder.h +0 -405
- data/ext/libdeflate/libdeflate/lib/lib_common.h +0 -35
- data/ext/libdeflate/libdeflate/lib/matchfinder_avx2.h +0 -53
- data/ext/libdeflate/libdeflate/lib/matchfinder_common.h +0 -205
- data/ext/libdeflate/libdeflate/lib/matchfinder_neon.h +0 -61
- data/ext/libdeflate/libdeflate/lib/matchfinder_sse2.h +0 -53
- data/ext/libdeflate/libdeflate/lib/unaligned.h +0 -202
- data/ext/libdeflate/libdeflate/lib/x86_cpu_features.c +0 -169
- data/ext/libdeflate/libdeflate/lib/x86_cpu_features.h +0 -48
- data/ext/libdeflate/libdeflate/lib/zlib_compress.c +0 -87
- data/ext/libdeflate/libdeflate/lib/zlib_constants.h +0 -21
- data/ext/libdeflate/libdeflate/lib/zlib_decompress.c +0 -91
- data/ext/libdeflate/libdeflate/libdeflate.h +0 -274
- data/ext/libdeflate/libdeflate/programs/benchmark.c +0 -558
- data/ext/libdeflate/libdeflate/programs/checksum.c +0 -197
- data/ext/libdeflate/libdeflate/programs/detect.sh +0 -62
- data/ext/libdeflate/libdeflate/programs/gzip.c +0 -603
- data/ext/libdeflate/libdeflate/programs/prog_util.c +0 -530
- data/ext/libdeflate/libdeflate/programs/prog_util.h +0 -162
- data/ext/libdeflate/libdeflate/programs/test_checksums.c +0 -135
- data/ext/libdeflate/libdeflate/programs/tgetopt.c +0 -118
- data/ext/libdeflate/libdeflate/tools/afl-fuzz/Makefile +0 -12
- data/ext/libdeflate/libdeflate/tools/afl-fuzz/deflate_compress/fuzz.c +0 -40
- data/ext/libdeflate/libdeflate/tools/afl-fuzz/deflate_compress/inputs/0 +0 -0
- data/ext/libdeflate/libdeflate/tools/afl-fuzz/deflate_decompress/fuzz.c +0 -28
- data/ext/libdeflate/libdeflate/tools/afl-fuzz/deflate_decompress/inputs/0 +0 -3
- data/ext/libdeflate/libdeflate/tools/afl-fuzz/gzip_decompress/fuzz.c +0 -28
- data/ext/libdeflate/libdeflate/tools/afl-fuzz/gzip_decompress/inputs/0 +0 -0
- data/ext/libdeflate/libdeflate/tools/afl-fuzz/prepare_for_fuzz.sh +0 -14
- data/ext/libdeflate/libdeflate/tools/afl-fuzz/zlib_decompress/fuzz.c +0 -28
- data/ext/libdeflate/libdeflate/tools/afl-fuzz/zlib_decompress/inputs/0 +0 -3
- data/ext/libdeflate/libdeflate/tools/android_build.sh +0 -104
- data/ext/libdeflate/libdeflate/tools/checksum_benchmarks.sh +0 -76
- data/ext/libdeflate/libdeflate/tools/exec_tests.sh +0 -30
- data/ext/libdeflate/libdeflate/tools/gen_crc32_multipliers.c +0 -108
- data/ext/libdeflate/libdeflate/tools/gen_crc32_table.c +0 -100
- data/ext/libdeflate/libdeflate/tools/gzip_tests.sh +0 -412
- data/ext/libdeflate/libdeflate/tools/make-windows-releases +0 -21
- data/ext/libdeflate/libdeflate/tools/mips_build.sh +0 -9
- data/ext/libdeflate/libdeflate/tools/msc_test.bat +0 -3
- data/ext/libdeflate/libdeflate/tools/pgo_build.sh +0 -23
- data/ext/libdeflate/libdeflate/tools/produce_gzip_benchmark_table.sh +0 -37
- data/ext/libdeflate/libdeflate/tools/run_tests.sh +0 -305
- data/ext/libdeflate/libdeflate/tools/windows_build.sh +0 -10
|
@@ -1,404 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* decompress_impl.h
|
|
3
|
-
*
|
|
4
|
-
* Originally public domain; changes after 2016-09-07 are copyrighted.
|
|
5
|
-
*
|
|
6
|
-
* Copyright 2016 Eric Biggers
|
|
7
|
-
*
|
|
8
|
-
* Permission is hereby granted, free of charge, to any person
|
|
9
|
-
* obtaining a copy of this software and associated documentation
|
|
10
|
-
* files (the "Software"), to deal in the Software without
|
|
11
|
-
* restriction, including without limitation the rights to use,
|
|
12
|
-
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
13
|
-
* copies of the Software, and to permit persons to whom the
|
|
14
|
-
* Software is furnished to do so, subject to the following
|
|
15
|
-
* conditions:
|
|
16
|
-
*
|
|
17
|
-
* The above copyright notice and this permission notice shall be
|
|
18
|
-
* included in all copies or substantial portions of the Software.
|
|
19
|
-
*
|
|
20
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
21
|
-
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
22
|
-
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
23
|
-
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
24
|
-
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
25
|
-
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
26
|
-
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
27
|
-
* OTHER DEALINGS IN THE SOFTWARE.
|
|
28
|
-
*/
|
|
29
|
-
|
|
30
|
-
/*
|
|
31
|
-
* This is the actual DEFLATE decompression routine, lifted out of
|
|
32
|
-
* deflate_decompress.c so that it can be compiled multiple times with different
|
|
33
|
-
* target instruction sets.
|
|
34
|
-
*/
|
|
35
|
-
|
|
36
|
-
static enum libdeflate_result ATTRIBUTES
|
|
37
|
-
FUNCNAME(struct libdeflate_decompressor * restrict d,
|
|
38
|
-
const void * restrict in, size_t in_nbytes,
|
|
39
|
-
void * restrict out, size_t out_nbytes_avail,
|
|
40
|
-
size_t *actual_out_nbytes_ret)
|
|
41
|
-
{
|
|
42
|
-
u8 *out_next = out;
|
|
43
|
-
u8 * const out_end = out_next + out_nbytes_avail;
|
|
44
|
-
const u8 *in_next = in;
|
|
45
|
-
const u8 * const in_end = in_next + in_nbytes;
|
|
46
|
-
bitbuf_t bitbuf = 0;
|
|
47
|
-
unsigned bitsleft = 0;
|
|
48
|
-
size_t overrun_count = 0;
|
|
49
|
-
unsigned i;
|
|
50
|
-
unsigned is_final_block;
|
|
51
|
-
unsigned block_type;
|
|
52
|
-
u16 len;
|
|
53
|
-
u16 nlen;
|
|
54
|
-
unsigned num_litlen_syms;
|
|
55
|
-
unsigned num_offset_syms;
|
|
56
|
-
u16 tmp16;
|
|
57
|
-
u32 tmp32;
|
|
58
|
-
|
|
59
|
-
next_block:
|
|
60
|
-
/* Starting to read the next block. */
|
|
61
|
-
;
|
|
62
|
-
|
|
63
|
-
STATIC_ASSERT(CAN_ENSURE(1 + 2 + 5 + 5 + 4));
|
|
64
|
-
ENSURE_BITS(1 + 2 + 5 + 5 + 4);
|
|
65
|
-
|
|
66
|
-
/* BFINAL: 1 bit */
|
|
67
|
-
is_final_block = POP_BITS(1);
|
|
68
|
-
|
|
69
|
-
/* BTYPE: 2 bits */
|
|
70
|
-
block_type = POP_BITS(2);
|
|
71
|
-
|
|
72
|
-
if (block_type == DEFLATE_BLOCKTYPE_DYNAMIC_HUFFMAN) {
|
|
73
|
-
|
|
74
|
-
/* Dynamic Huffman block. */
|
|
75
|
-
|
|
76
|
-
/* The order in which precode lengths are stored. */
|
|
77
|
-
static const u8 deflate_precode_lens_permutation[DEFLATE_NUM_PRECODE_SYMS] = {
|
|
78
|
-
16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
unsigned num_explicit_precode_lens;
|
|
82
|
-
|
|
83
|
-
/* Read the codeword length counts. */
|
|
84
|
-
|
|
85
|
-
STATIC_ASSERT(DEFLATE_NUM_LITLEN_SYMS == ((1 << 5) - 1) + 257);
|
|
86
|
-
num_litlen_syms = POP_BITS(5) + 257;
|
|
87
|
-
|
|
88
|
-
STATIC_ASSERT(DEFLATE_NUM_OFFSET_SYMS == ((1 << 5) - 1) + 1);
|
|
89
|
-
num_offset_syms = POP_BITS(5) + 1;
|
|
90
|
-
|
|
91
|
-
STATIC_ASSERT(DEFLATE_NUM_PRECODE_SYMS == ((1 << 4) - 1) + 4);
|
|
92
|
-
num_explicit_precode_lens = POP_BITS(4) + 4;
|
|
93
|
-
|
|
94
|
-
/* Read the precode codeword lengths. */
|
|
95
|
-
STATIC_ASSERT(DEFLATE_MAX_PRE_CODEWORD_LEN == (1 << 3) - 1);
|
|
96
|
-
if (CAN_ENSURE(DEFLATE_NUM_PRECODE_SYMS * 3)) {
|
|
97
|
-
|
|
98
|
-
ENSURE_BITS(DEFLATE_NUM_PRECODE_SYMS * 3);
|
|
99
|
-
|
|
100
|
-
for (i = 0; i < num_explicit_precode_lens; i++)
|
|
101
|
-
d->u.precode_lens[deflate_precode_lens_permutation[i]] = POP_BITS(3);
|
|
102
|
-
} else {
|
|
103
|
-
for (i = 0; i < num_explicit_precode_lens; i++) {
|
|
104
|
-
ENSURE_BITS(3);
|
|
105
|
-
d->u.precode_lens[deflate_precode_lens_permutation[i]] = POP_BITS(3);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
for (; i < DEFLATE_NUM_PRECODE_SYMS; i++)
|
|
110
|
-
d->u.precode_lens[deflate_precode_lens_permutation[i]] = 0;
|
|
111
|
-
|
|
112
|
-
/* Build the decode table for the precode. */
|
|
113
|
-
SAFETY_CHECK(build_precode_decode_table(d));
|
|
114
|
-
|
|
115
|
-
/* Expand the literal/length and offset codeword lengths. */
|
|
116
|
-
for (i = 0; i < num_litlen_syms + num_offset_syms; ) {
|
|
117
|
-
u32 entry;
|
|
118
|
-
unsigned presym;
|
|
119
|
-
u8 rep_val;
|
|
120
|
-
unsigned rep_count;
|
|
121
|
-
|
|
122
|
-
ENSURE_BITS(DEFLATE_MAX_PRE_CODEWORD_LEN + 7);
|
|
123
|
-
|
|
124
|
-
/* (The code below assumes that the precode decode table
|
|
125
|
-
* does not have any subtables.) */
|
|
126
|
-
STATIC_ASSERT(PRECODE_TABLEBITS == DEFLATE_MAX_PRE_CODEWORD_LEN);
|
|
127
|
-
|
|
128
|
-
/* Read the next precode symbol. */
|
|
129
|
-
entry = d->u.l.precode_decode_table[BITS(DEFLATE_MAX_PRE_CODEWORD_LEN)];
|
|
130
|
-
REMOVE_BITS(entry & HUFFDEC_LENGTH_MASK);
|
|
131
|
-
presym = entry >> HUFFDEC_RESULT_SHIFT;
|
|
132
|
-
|
|
133
|
-
if (presym < 16) {
|
|
134
|
-
/* Explicit codeword length */
|
|
135
|
-
d->u.l.lens[i++] = presym;
|
|
136
|
-
continue;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/* Run-length encoded codeword lengths */
|
|
140
|
-
|
|
141
|
-
/* Note: we don't need verify that the repeat count
|
|
142
|
-
* doesn't overflow the number of elements, since we
|
|
143
|
-
* have enough extra spaces to allow for the worst-case
|
|
144
|
-
* overflow (138 zeroes when only 1 length was
|
|
145
|
-
* remaining).
|
|
146
|
-
*
|
|
147
|
-
* In the case of the small repeat counts (presyms 16
|
|
148
|
-
* and 17), it is fastest to always write the maximum
|
|
149
|
-
* number of entries. That gets rid of branches that
|
|
150
|
-
* would otherwise be required.
|
|
151
|
-
*
|
|
152
|
-
* It is not just because of the numerical order that
|
|
153
|
-
* our checks go in the order 'presym < 16', 'presym ==
|
|
154
|
-
* 16', and 'presym == 17'. For typical data this is
|
|
155
|
-
* ordered from most frequent to least frequent case.
|
|
156
|
-
*/
|
|
157
|
-
STATIC_ASSERT(DEFLATE_MAX_LENS_OVERRUN == 138 - 1);
|
|
158
|
-
|
|
159
|
-
if (presym == 16) {
|
|
160
|
-
/* Repeat the previous length 3 - 6 times */
|
|
161
|
-
SAFETY_CHECK(i != 0);
|
|
162
|
-
rep_val = d->u.l.lens[i - 1];
|
|
163
|
-
STATIC_ASSERT(3 + ((1 << 2) - 1) == 6);
|
|
164
|
-
rep_count = 3 + POP_BITS(2);
|
|
165
|
-
d->u.l.lens[i + 0] = rep_val;
|
|
166
|
-
d->u.l.lens[i + 1] = rep_val;
|
|
167
|
-
d->u.l.lens[i + 2] = rep_val;
|
|
168
|
-
d->u.l.lens[i + 3] = rep_val;
|
|
169
|
-
d->u.l.lens[i + 4] = rep_val;
|
|
170
|
-
d->u.l.lens[i + 5] = rep_val;
|
|
171
|
-
i += rep_count;
|
|
172
|
-
} else if (presym == 17) {
|
|
173
|
-
/* Repeat zero 3 - 10 times */
|
|
174
|
-
STATIC_ASSERT(3 + ((1 << 3) - 1) == 10);
|
|
175
|
-
rep_count = 3 + POP_BITS(3);
|
|
176
|
-
d->u.l.lens[i + 0] = 0;
|
|
177
|
-
d->u.l.lens[i + 1] = 0;
|
|
178
|
-
d->u.l.lens[i + 2] = 0;
|
|
179
|
-
d->u.l.lens[i + 3] = 0;
|
|
180
|
-
d->u.l.lens[i + 4] = 0;
|
|
181
|
-
d->u.l.lens[i + 5] = 0;
|
|
182
|
-
d->u.l.lens[i + 6] = 0;
|
|
183
|
-
d->u.l.lens[i + 7] = 0;
|
|
184
|
-
d->u.l.lens[i + 8] = 0;
|
|
185
|
-
d->u.l.lens[i + 9] = 0;
|
|
186
|
-
i += rep_count;
|
|
187
|
-
} else {
|
|
188
|
-
/* Repeat zero 11 - 138 times */
|
|
189
|
-
STATIC_ASSERT(11 + ((1 << 7) - 1) == 138);
|
|
190
|
-
rep_count = 11 + POP_BITS(7);
|
|
191
|
-
memset(&d->u.l.lens[i], 0,
|
|
192
|
-
rep_count * sizeof(d->u.l.lens[i]));
|
|
193
|
-
i += rep_count;
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
} else if (block_type == DEFLATE_BLOCKTYPE_UNCOMPRESSED) {
|
|
197
|
-
|
|
198
|
-
/* Uncompressed block: copy 'len' bytes literally from the input
|
|
199
|
-
* buffer to the output buffer. */
|
|
200
|
-
|
|
201
|
-
ALIGN_INPUT();
|
|
202
|
-
|
|
203
|
-
SAFETY_CHECK(in_end - in_next >= 4);
|
|
204
|
-
|
|
205
|
-
len = READ_U16();
|
|
206
|
-
nlen = READ_U16();
|
|
207
|
-
|
|
208
|
-
SAFETY_CHECK(len == (u16)~nlen);
|
|
209
|
-
if (unlikely(len > out_end - out_next))
|
|
210
|
-
return LIBDEFLATE_INSUFFICIENT_SPACE;
|
|
211
|
-
SAFETY_CHECK(len <= in_end - in_next);
|
|
212
|
-
|
|
213
|
-
memcpy(out_next, in_next, len);
|
|
214
|
-
in_next += len;
|
|
215
|
-
out_next += len;
|
|
216
|
-
|
|
217
|
-
goto block_done;
|
|
218
|
-
|
|
219
|
-
} else {
|
|
220
|
-
SAFETY_CHECK(block_type == DEFLATE_BLOCKTYPE_STATIC_HUFFMAN);
|
|
221
|
-
|
|
222
|
-
/* Static Huffman block: set the static Huffman codeword
|
|
223
|
-
* lengths. Then the remainder is the same as decompressing a
|
|
224
|
-
* dynamic Huffman block. */
|
|
225
|
-
|
|
226
|
-
STATIC_ASSERT(DEFLATE_NUM_LITLEN_SYMS == 288);
|
|
227
|
-
STATIC_ASSERT(DEFLATE_NUM_OFFSET_SYMS == 32);
|
|
228
|
-
|
|
229
|
-
for (i = 0; i < 144; i++)
|
|
230
|
-
d->u.l.lens[i] = 8;
|
|
231
|
-
for (; i < 256; i++)
|
|
232
|
-
d->u.l.lens[i] = 9;
|
|
233
|
-
for (; i < 280; i++)
|
|
234
|
-
d->u.l.lens[i] = 7;
|
|
235
|
-
for (; i < 288; i++)
|
|
236
|
-
d->u.l.lens[i] = 8;
|
|
237
|
-
|
|
238
|
-
for (; i < 288 + 32; i++)
|
|
239
|
-
d->u.l.lens[i] = 5;
|
|
240
|
-
|
|
241
|
-
num_litlen_syms = 288;
|
|
242
|
-
num_offset_syms = 32;
|
|
243
|
-
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
/* Decompressing a Huffman block (either dynamic or static) */
|
|
247
|
-
|
|
248
|
-
SAFETY_CHECK(build_offset_decode_table(d, num_litlen_syms, num_offset_syms));
|
|
249
|
-
SAFETY_CHECK(build_litlen_decode_table(d, num_litlen_syms, num_offset_syms));
|
|
250
|
-
|
|
251
|
-
/* The main DEFLATE decode loop */
|
|
252
|
-
for (;;) {
|
|
253
|
-
u32 entry;
|
|
254
|
-
u32 length;
|
|
255
|
-
u32 offset;
|
|
256
|
-
|
|
257
|
-
/* Decode a litlen symbol. */
|
|
258
|
-
ENSURE_BITS(DEFLATE_MAX_LITLEN_CODEWORD_LEN);
|
|
259
|
-
entry = d->u.litlen_decode_table[BITS(LITLEN_TABLEBITS)];
|
|
260
|
-
if (entry & HUFFDEC_SUBTABLE_POINTER) {
|
|
261
|
-
/* Litlen subtable required (uncommon case) */
|
|
262
|
-
REMOVE_BITS(LITLEN_TABLEBITS);
|
|
263
|
-
entry = d->u.litlen_decode_table[
|
|
264
|
-
((entry >> HUFFDEC_RESULT_SHIFT) & 0xFFFF) +
|
|
265
|
-
BITS(entry & HUFFDEC_LENGTH_MASK)];
|
|
266
|
-
}
|
|
267
|
-
REMOVE_BITS(entry & HUFFDEC_LENGTH_MASK);
|
|
268
|
-
if (entry & HUFFDEC_LITERAL) {
|
|
269
|
-
/* Literal */
|
|
270
|
-
if (unlikely(out_next == out_end))
|
|
271
|
-
return LIBDEFLATE_INSUFFICIENT_SPACE;
|
|
272
|
-
*out_next++ = (u8)(entry >> HUFFDEC_RESULT_SHIFT);
|
|
273
|
-
continue;
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
/* Match or end-of-block */
|
|
277
|
-
|
|
278
|
-
entry >>= HUFFDEC_RESULT_SHIFT;
|
|
279
|
-
ENSURE_BITS(MAX_ENSURE);
|
|
280
|
-
|
|
281
|
-
/* Pop the extra length bits and add them to the length base to
|
|
282
|
-
* produce the full length. */
|
|
283
|
-
length = (entry >> HUFFDEC_LENGTH_BASE_SHIFT) +
|
|
284
|
-
POP_BITS(entry & HUFFDEC_EXTRA_LENGTH_BITS_MASK);
|
|
285
|
-
|
|
286
|
-
/* The match destination must not end after the end of the
|
|
287
|
-
* output buffer. For efficiency, combine this check with the
|
|
288
|
-
* end-of-block check. We're using 0 for the special
|
|
289
|
-
* end-of-block length, so subtract 1 and it turn it into
|
|
290
|
-
* SIZE_MAX. */
|
|
291
|
-
STATIC_ASSERT(HUFFDEC_END_OF_BLOCK_LENGTH == 0);
|
|
292
|
-
if (unlikely((size_t)length - 1 >= out_end - out_next)) {
|
|
293
|
-
if (unlikely(length != HUFFDEC_END_OF_BLOCK_LENGTH))
|
|
294
|
-
return LIBDEFLATE_INSUFFICIENT_SPACE;
|
|
295
|
-
goto block_done;
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
/* Decode the match offset. */
|
|
299
|
-
|
|
300
|
-
entry = d->offset_decode_table[BITS(OFFSET_TABLEBITS)];
|
|
301
|
-
if (entry & HUFFDEC_SUBTABLE_POINTER) {
|
|
302
|
-
/* Offset subtable required (uncommon case) */
|
|
303
|
-
REMOVE_BITS(OFFSET_TABLEBITS);
|
|
304
|
-
entry = d->offset_decode_table[
|
|
305
|
-
((entry >> HUFFDEC_RESULT_SHIFT) & 0xFFFF) +
|
|
306
|
-
BITS(entry & HUFFDEC_LENGTH_MASK)];
|
|
307
|
-
}
|
|
308
|
-
REMOVE_BITS(entry & HUFFDEC_LENGTH_MASK);
|
|
309
|
-
entry >>= HUFFDEC_RESULT_SHIFT;
|
|
310
|
-
|
|
311
|
-
STATIC_ASSERT(CAN_ENSURE(DEFLATE_MAX_EXTRA_LENGTH_BITS +
|
|
312
|
-
DEFLATE_MAX_OFFSET_CODEWORD_LEN) &&
|
|
313
|
-
CAN_ENSURE(DEFLATE_MAX_EXTRA_OFFSET_BITS));
|
|
314
|
-
if (!CAN_ENSURE(DEFLATE_MAX_EXTRA_LENGTH_BITS +
|
|
315
|
-
DEFLATE_MAX_OFFSET_CODEWORD_LEN +
|
|
316
|
-
DEFLATE_MAX_EXTRA_OFFSET_BITS))
|
|
317
|
-
ENSURE_BITS(DEFLATE_MAX_EXTRA_OFFSET_BITS);
|
|
318
|
-
|
|
319
|
-
/* Pop the extra offset bits and add them to the offset base to
|
|
320
|
-
* produce the full offset. */
|
|
321
|
-
offset = (entry & HUFFDEC_OFFSET_BASE_MASK) +
|
|
322
|
-
POP_BITS(entry >> HUFFDEC_EXTRA_OFFSET_BITS_SHIFT);
|
|
323
|
-
|
|
324
|
-
/* The match source must not begin before the beginning of the
|
|
325
|
-
* output buffer. */
|
|
326
|
-
SAFETY_CHECK(offset <= out_next - (const u8 *)out);
|
|
327
|
-
|
|
328
|
-
/* Copy the match: 'length' bytes at 'out_next - offset' to
|
|
329
|
-
* 'out_next'. */
|
|
330
|
-
|
|
331
|
-
if (UNALIGNED_ACCESS_IS_FAST &&
|
|
332
|
-
length <= (3 * WORDBYTES) &&
|
|
333
|
-
offset >= WORDBYTES &&
|
|
334
|
-
length + (3 * WORDBYTES) <= out_end - out_next)
|
|
335
|
-
{
|
|
336
|
-
/* Fast case: short length, no overlaps if we copy one
|
|
337
|
-
* word at a time, and we aren't getting too close to
|
|
338
|
-
* the end of the output array. */
|
|
339
|
-
copy_word_unaligned(out_next - offset + (0 * WORDBYTES),
|
|
340
|
-
out_next + (0 * WORDBYTES));
|
|
341
|
-
copy_word_unaligned(out_next - offset + (1 * WORDBYTES),
|
|
342
|
-
out_next + (1 * WORDBYTES));
|
|
343
|
-
copy_word_unaligned(out_next - offset + (2 * WORDBYTES),
|
|
344
|
-
out_next + (2 * WORDBYTES));
|
|
345
|
-
} else {
|
|
346
|
-
const u8 *src = out_next - offset;
|
|
347
|
-
u8 *dst = out_next;
|
|
348
|
-
u8 *end = out_next + length;
|
|
349
|
-
|
|
350
|
-
if (UNALIGNED_ACCESS_IS_FAST &&
|
|
351
|
-
likely(out_end - end >= WORDBYTES - 1)) {
|
|
352
|
-
if (offset >= WORDBYTES) {
|
|
353
|
-
copy_word_unaligned(src, dst);
|
|
354
|
-
src += WORDBYTES;
|
|
355
|
-
dst += WORDBYTES;
|
|
356
|
-
if (dst < end) {
|
|
357
|
-
do {
|
|
358
|
-
copy_word_unaligned(src, dst);
|
|
359
|
-
src += WORDBYTES;
|
|
360
|
-
dst += WORDBYTES;
|
|
361
|
-
} while (dst < end);
|
|
362
|
-
}
|
|
363
|
-
} else if (offset == 1) {
|
|
364
|
-
machine_word_t v = repeat_byte(*(dst - 1));
|
|
365
|
-
do {
|
|
366
|
-
store_word_unaligned(v, dst);
|
|
367
|
-
src += WORDBYTES;
|
|
368
|
-
dst += WORDBYTES;
|
|
369
|
-
} while (dst < end);
|
|
370
|
-
} else {
|
|
371
|
-
*dst++ = *src++;
|
|
372
|
-
*dst++ = *src++;
|
|
373
|
-
do {
|
|
374
|
-
*dst++ = *src++;
|
|
375
|
-
} while (dst < end);
|
|
376
|
-
}
|
|
377
|
-
} else {
|
|
378
|
-
*dst++ = *src++;
|
|
379
|
-
*dst++ = *src++;
|
|
380
|
-
do {
|
|
381
|
-
*dst++ = *src++;
|
|
382
|
-
} while (dst < end);
|
|
383
|
-
}
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
out_next += length;
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
block_done:
|
|
390
|
-
/* Finished decoding a block. */
|
|
391
|
-
|
|
392
|
-
if (!is_final_block)
|
|
393
|
-
goto next_block;
|
|
394
|
-
|
|
395
|
-
/* That was the last block. */
|
|
396
|
-
|
|
397
|
-
if (actual_out_nbytes_ret) {
|
|
398
|
-
*actual_out_nbytes_ret = out_next - (u8 *)out;
|
|
399
|
-
} else {
|
|
400
|
-
if (out_next != out_end)
|
|
401
|
-
return LIBDEFLATE_SHORT_OUTPUT;
|
|
402
|
-
}
|
|
403
|
-
return LIBDEFLATE_SUCCESS;
|
|
404
|
-
}
|