lzfse 0.0.1.pre.4
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 +7 -0
- data/.yardopts +1 -0
- data/LICENSE +21 -0
- data/README.md +38 -0
- data/ext/lzfse/LICENSE +27 -0
- data/ext/lzfse/ext.c +194 -0
- data/ext/lzfse/ext.h +14 -0
- data/ext/lzfse/extconf.rb +10 -0
- data/ext/lzfse/lzfse.h +136 -0
- data/ext/lzfse/lzfse_decode.c +72 -0
- data/ext/lzfse/lzfse_decode_base.c +630 -0
- data/ext/lzfse/lzfse_encode.c +163 -0
- data/ext/lzfse/lzfse_encode_base.c +830 -0
- data/ext/lzfse/lzfse_encode_tables.h +218 -0
- data/ext/lzfse/lzfse_fse.c +216 -0
- data/ext/lzfse/lzfse_fse.h +631 -0
- data/ext/lzfse/lzfse_internal.h +616 -0
- data/ext/lzfse/lzfse_tunables.h +60 -0
- data/ext/lzfse/lzvn_decode_base.c +711 -0
- data/ext/lzfse/lzvn_decode_base.h +68 -0
- data/ext/lzfse/lzvn_encode_base.c +593 -0
- data/ext/lzfse/lzvn_encode_base.h +116 -0
- data/lib/lzfse.rb +25 -0
- data/lib/lzfse/exceptions.rb +12 -0
- data/lib/lzfse/version.rb +5 -0
- data/vendor/bundle/ruby/2.7.0/gems/rainbow-3.0.0/LICENSE +20 -0
- data/vendor/bundle/ruby/2.7.0/gems/regexp_parser-2.1.1/LICENSE +22 -0
- data/vendor/bundle/ruby/2.7.0/gems/yard-0.9.26/LICENSE +22 -0
- metadata +126 -0
@@ -0,0 +1,68 @@
|
|
1
|
+
/*
|
2
|
+
Copyright (c) 2015-2016, Apple Inc. All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
5
|
+
|
6
|
+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
7
|
+
|
8
|
+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
|
9
|
+
in the documentation and/or other materials provided with the distribution.
|
10
|
+
|
11
|
+
3. Neither the name of the copyright holder(s) nor the names of any contributors may be used to endorse or promote products derived
|
12
|
+
from this software without specific prior written permission.
|
13
|
+
|
14
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
15
|
+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
16
|
+
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
17
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
18
|
+
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
19
|
+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
20
|
+
*/
|
21
|
+
|
22
|
+
// LZVN low-level decoder (v2)
|
23
|
+
// Functions in the low-level API should switch to these at some point.
|
24
|
+
// Apr 2014
|
25
|
+
|
26
|
+
#ifndef LZVN_DECODE_BASE_H
|
27
|
+
#define LZVN_DECODE_BASE_H
|
28
|
+
|
29
|
+
#include "lzfse_internal.h"
|
30
|
+
|
31
|
+
/*! @abstract Base decoder state. */
|
32
|
+
typedef struct {
|
33
|
+
|
34
|
+
// Decoder I/O
|
35
|
+
|
36
|
+
// Next byte to read in source buffer
|
37
|
+
const unsigned char *src;
|
38
|
+
// Next byte after source buffer
|
39
|
+
const unsigned char *src_end;
|
40
|
+
|
41
|
+
// Next byte to write in destination buffer (by decoder)
|
42
|
+
unsigned char *dst;
|
43
|
+
// Valid range for destination buffer is [dst_begin, dst_end - 1]
|
44
|
+
unsigned char *dst_begin;
|
45
|
+
unsigned char *dst_end;
|
46
|
+
// Next byte to read in destination buffer (modified by caller)
|
47
|
+
unsigned char *dst_current;
|
48
|
+
|
49
|
+
// Decoder state
|
50
|
+
|
51
|
+
// Partially expanded match, or 0,0,0.
|
52
|
+
// In that case, src points to the next literal to copy, or the next op-code
|
53
|
+
// if L==0.
|
54
|
+
size_t L, M, D;
|
55
|
+
|
56
|
+
// Distance for last emitted match, or 0
|
57
|
+
lzvn_offset d_prev;
|
58
|
+
|
59
|
+
// Did we decode end-of-stream?
|
60
|
+
int end_of_stream;
|
61
|
+
|
62
|
+
} lzvn_decoder_state;
|
63
|
+
|
64
|
+
/*! @abstract Decode source to destination.
|
65
|
+
* Updates \p state (src,dst,d_prev). */
|
66
|
+
void lzvn_decode(lzvn_decoder_state *state);
|
67
|
+
|
68
|
+
#endif // LZVN_DECODE_BASE_H
|
@@ -0,0 +1,593 @@
|
|
1
|
+
/*
|
2
|
+
Copyright (c) 2015-2016, Apple Inc. All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
5
|
+
|
6
|
+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
7
|
+
|
8
|
+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
|
9
|
+
in the documentation and/or other materials provided with the distribution.
|
10
|
+
|
11
|
+
3. Neither the name of the copyright holder(s) nor the names of any contributors may be used to endorse or promote products derived
|
12
|
+
from this software without specific prior written permission.
|
13
|
+
|
14
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
15
|
+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
16
|
+
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
17
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
18
|
+
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
19
|
+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
20
|
+
*/
|
21
|
+
|
22
|
+
// LZVN low-level encoder
|
23
|
+
|
24
|
+
#include "lzvn_encode_base.h"
|
25
|
+
|
26
|
+
#if defined(_MSC_VER) && !defined(__clang__)
|
27
|
+
# define restrict __restrict
|
28
|
+
#endif
|
29
|
+
|
30
|
+
// ===============================================================
|
31
|
+
// Coarse/fine copy, non overlapping buffers
|
32
|
+
|
33
|
+
/*! @abstract Copy at least \p nbytes bytes from \p src to \p dst, by blocks
|
34
|
+
* of 8 bytes (may go beyond range). No overlap.
|
35
|
+
* @return \p dst + \p nbytes. */
|
36
|
+
static inline unsigned char *lzvn_copy64(unsigned char *restrict dst,
|
37
|
+
const unsigned char *restrict src,
|
38
|
+
size_t nbytes) {
|
39
|
+
for (size_t i = 0; i < nbytes; i += 8)
|
40
|
+
store8(dst + i, load8(src + i));
|
41
|
+
return dst + nbytes;
|
42
|
+
}
|
43
|
+
|
44
|
+
/*! @abstract Copy exactly \p nbytes bytes from \p src to \p dst (respects range).
|
45
|
+
* No overlap.
|
46
|
+
* @return \p dst + \p nbytes. */
|
47
|
+
static inline unsigned char *lzvn_copy8(unsigned char *restrict dst,
|
48
|
+
const unsigned char *restrict src,
|
49
|
+
size_t nbytes) {
|
50
|
+
for (size_t i = 0; i < nbytes; i++)
|
51
|
+
dst[i] = src[i];
|
52
|
+
return dst + nbytes;
|
53
|
+
}
|
54
|
+
|
55
|
+
/*! @abstract Emit (L,0,0) instructions (final literal).
|
56
|
+
* We read at most \p L bytes from \p p.
|
57
|
+
* @param p input stream
|
58
|
+
* @param q1 the first byte after the output buffer.
|
59
|
+
* @return pointer to the next output, <= \p q1.
|
60
|
+
* @return \p q1 if output is full. In that case, output will be partially invalid.
|
61
|
+
*/
|
62
|
+
static inline unsigned char *emit_literal(const unsigned char *p,
|
63
|
+
unsigned char *q, unsigned char *q1,
|
64
|
+
size_t L) {
|
65
|
+
size_t x;
|
66
|
+
while (L > 15) {
|
67
|
+
x = L < 271 ? L : 271;
|
68
|
+
if (q + x + 10 >= q1)
|
69
|
+
goto OUT_FULL;
|
70
|
+
store2(q, 0xE0 + ((x - 16) << 8));
|
71
|
+
q += 2;
|
72
|
+
L -= x;
|
73
|
+
q = lzvn_copy8(q, p, x);
|
74
|
+
p += x;
|
75
|
+
}
|
76
|
+
if (L > 0) {
|
77
|
+
if (q + L + 10 >= q1)
|
78
|
+
goto OUT_FULL;
|
79
|
+
*q++ = 0xE0 + L; // 1110LLLL
|
80
|
+
q = lzvn_copy8(q, p, L);
|
81
|
+
}
|
82
|
+
return q;
|
83
|
+
|
84
|
+
OUT_FULL:
|
85
|
+
return q1;
|
86
|
+
}
|
87
|
+
|
88
|
+
/*! @abstract Emit (L,M,D) instructions. M>=3.
|
89
|
+
* @param p input stream pointing to the beginning of the literal. We read at
|
90
|
+
* most \p L+4 bytes from \p p.
|
91
|
+
* @param q1 the first byte after the output buffer.
|
92
|
+
* @return pointer to the next output, <= \p q1.
|
93
|
+
* @return \p q1 if output is full. In that case, output will be partially invalid.
|
94
|
+
*/
|
95
|
+
static inline unsigned char *emit(const unsigned char *p, unsigned char *q,
|
96
|
+
unsigned char *q1, size_t L, size_t M,
|
97
|
+
size_t D, size_t D_prev) {
|
98
|
+
size_t x;
|
99
|
+
while (L > 15) {
|
100
|
+
x = L < 271 ? L : 271;
|
101
|
+
if (q + x + 10 >= q1)
|
102
|
+
goto OUT_FULL;
|
103
|
+
store2(q, 0xE0 + ((x - 16) << 8));
|
104
|
+
q += 2;
|
105
|
+
L -= x;
|
106
|
+
q = lzvn_copy64(q, p, x);
|
107
|
+
p += x;
|
108
|
+
}
|
109
|
+
if (L > 3) {
|
110
|
+
if (q + L + 10 >= q1)
|
111
|
+
goto OUT_FULL;
|
112
|
+
*q++ = 0xE0 + L; // 1110LLLL
|
113
|
+
q = lzvn_copy64(q, p, L);
|
114
|
+
p += L;
|
115
|
+
L = 0;
|
116
|
+
}
|
117
|
+
x = M <= 10 - 2 * L ? M : 10 - 2 * L; // x = min(10-2*L,M)
|
118
|
+
M -= x;
|
119
|
+
x -= 3; // M = (x+3) + M' max value for x is 7-2*L
|
120
|
+
|
121
|
+
// Here L<4 literals remaining, we read them here
|
122
|
+
uint32_t literal = load4(p);
|
123
|
+
// P is not accessed after this point
|
124
|
+
|
125
|
+
// Relaxed capacity test covering all cases
|
126
|
+
if (q + 8 >= q1)
|
127
|
+
goto OUT_FULL;
|
128
|
+
|
129
|
+
if (D == D_prev) {
|
130
|
+
if (L == 0) {
|
131
|
+
*q++ = 0xF0 + (x + 3); // XM!
|
132
|
+
} else {
|
133
|
+
*q++ = (L << 6) + (x << 3) + 6; // LLxxx110
|
134
|
+
}
|
135
|
+
store4(q, literal);
|
136
|
+
q += L;
|
137
|
+
} else if (D < 2048 - 2 * 256) {
|
138
|
+
// Short dist D>>8 in 0..5
|
139
|
+
*q++ = (D >> 8) + (L << 6) + (x << 3); // LLxxxDDD
|
140
|
+
*q++ = D & 0xFF;
|
141
|
+
store4(q, literal);
|
142
|
+
q += L;
|
143
|
+
} else if (D >= (1 << 14) || M == 0 || (x + 3) + M > 34) {
|
144
|
+
// Long dist
|
145
|
+
*q++ = (L << 6) + (x << 3) + 7;
|
146
|
+
store2(q, D);
|
147
|
+
q += 2;
|
148
|
+
store4(q, literal);
|
149
|
+
q += L;
|
150
|
+
} else {
|
151
|
+
// Medium distance
|
152
|
+
x += M;
|
153
|
+
M = 0;
|
154
|
+
*q++ = 0xA0 + (x >> 2) + (L << 3);
|
155
|
+
store2(q, D << 2 | (x & 3));
|
156
|
+
q += 2;
|
157
|
+
store4(q, literal);
|
158
|
+
q += L;
|
159
|
+
}
|
160
|
+
|
161
|
+
// Issue remaining match
|
162
|
+
while (M > 15) {
|
163
|
+
if (q + 2 >= q1)
|
164
|
+
goto OUT_FULL;
|
165
|
+
x = M < 271 ? M : 271;
|
166
|
+
store2(q, 0xf0 + ((x - 16) << 8));
|
167
|
+
q += 2;
|
168
|
+
M -= x;
|
169
|
+
}
|
170
|
+
if (M > 0) {
|
171
|
+
if (q + 1 >= q1)
|
172
|
+
goto OUT_FULL;
|
173
|
+
*q++ = 0xF0 + M; // M = 0..15
|
174
|
+
}
|
175
|
+
|
176
|
+
return q;
|
177
|
+
|
178
|
+
OUT_FULL:
|
179
|
+
return q1;
|
180
|
+
}
|
181
|
+
|
182
|
+
// ===============================================================
|
183
|
+
// Conversions
|
184
|
+
|
185
|
+
/*! @abstract Return 32-bit value to store for offset x. */
|
186
|
+
static inline int32_t offset_to_s32(lzvn_offset x) { return (int32_t)x; }
|
187
|
+
|
188
|
+
/*! @abstract Get offset from 32-bit stored value x. */
|
189
|
+
static inline lzvn_offset offset_from_s32(int32_t x) { return (lzvn_offset)x; }
|
190
|
+
|
191
|
+
// ===============================================================
|
192
|
+
// Hash and Matching
|
193
|
+
|
194
|
+
/*! @abstract Get hash in range \c [0,LZVN_ENCODE_HASH_VALUES-1] from 3 bytes in i. */
|
195
|
+
static inline uint32_t hash3i(uint32_t i) {
|
196
|
+
i &= 0xffffff; // truncate to 24-bit input (slightly increases compression ratio)
|
197
|
+
uint32_t h = (i * (1 + (1 << 6) + (1 << 12))) >> 12;
|
198
|
+
return h & (LZVN_ENCODE_HASH_VALUES - 1);
|
199
|
+
}
|
200
|
+
|
201
|
+
/*! @abstract Return the number [0, 4] of zero bytes in \p x, starting from the
|
202
|
+
* least significant byte. */
|
203
|
+
static inline lzvn_offset trailing_zero_bytes(uint32_t x) {
|
204
|
+
return (x == 0) ? 4 : (__builtin_ctzl(x) >> 3);
|
205
|
+
}
|
206
|
+
|
207
|
+
/*! @abstract Return the number [0, 4] of matching chars between values at
|
208
|
+
* \p src+i and \p src+j, starting from the least significant byte.
|
209
|
+
* Assumes we can read 4 chars from each position. */
|
210
|
+
static inline lzvn_offset nmatch4(const unsigned char *src, lzvn_offset i,
|
211
|
+
lzvn_offset j) {
|
212
|
+
uint32_t vi = load4(src + i);
|
213
|
+
uint32_t vj = load4(src + j);
|
214
|
+
return trailing_zero_bytes(vi ^ vj);
|
215
|
+
}
|
216
|
+
|
217
|
+
/*! @abstract Check if l_begin, m_begin, m0_begin (m0_begin < m_begin) can be
|
218
|
+
* expanded to a match of length at least 3.
|
219
|
+
* @param m_begin new string to match.
|
220
|
+
* @param m0_begin candidate old string.
|
221
|
+
* @param src source buffer, with valid indices src_begin <= i < src_end.
|
222
|
+
* (src_begin may be <0)
|
223
|
+
* @return If a match can be found, return 1 and set all \p match fields,
|
224
|
+
* otherwise return 0.
|
225
|
+
* @note \p *match should be 0 before the call. */
|
226
|
+
static inline int lzvn_find_match(const unsigned char *src,
|
227
|
+
lzvn_offset src_begin,
|
228
|
+
lzvn_offset src_end, lzvn_offset l_begin,
|
229
|
+
lzvn_offset m0_begin, lzvn_offset m_begin,
|
230
|
+
lzvn_match_info *match) {
|
231
|
+
lzvn_offset n = nmatch4(src, m_begin, m0_begin);
|
232
|
+
if (n < 3)
|
233
|
+
return 0; // no match
|
234
|
+
|
235
|
+
lzvn_offset D = m_begin - m0_begin; // actual distance
|
236
|
+
if (D <= 0 || D > LZVN_ENCODE_MAX_DISTANCE)
|
237
|
+
return 0; // distance out of range
|
238
|
+
|
239
|
+
// Expand forward
|
240
|
+
lzvn_offset m_end = m_begin + n;
|
241
|
+
while (n == 4 && m_end + 4 < src_end) {
|
242
|
+
n = nmatch4(src, m_end, m_end - D);
|
243
|
+
m_end += n;
|
244
|
+
}
|
245
|
+
|
246
|
+
// Expand backwards over literal
|
247
|
+
while (m0_begin > src_begin && m_begin > l_begin &&
|
248
|
+
src[m_begin - 1] == src[m0_begin - 1]) {
|
249
|
+
m0_begin--;
|
250
|
+
m_begin--;
|
251
|
+
}
|
252
|
+
|
253
|
+
// OK, we keep it, update MATCH
|
254
|
+
lzvn_offset M = m_end - m_begin; // match length
|
255
|
+
match->m_begin = m_begin;
|
256
|
+
match->m_end = m_end;
|
257
|
+
match->K = M - ((D < 0x600) ? 2 : 3);
|
258
|
+
match->M = M;
|
259
|
+
match->D = D;
|
260
|
+
|
261
|
+
return 1; // OK
|
262
|
+
}
|
263
|
+
|
264
|
+
/*! @abstract Same as lzvn_find_match, but we already know that N bytes do
|
265
|
+
* match (N<=4). */
|
266
|
+
static inline int lzvn_find_matchN(const unsigned char *src,
|
267
|
+
lzvn_offset src_begin,
|
268
|
+
lzvn_offset src_end, lzvn_offset l_begin,
|
269
|
+
lzvn_offset m0_begin, lzvn_offset m_begin,
|
270
|
+
lzvn_offset n, lzvn_match_info *match) {
|
271
|
+
// We can skip the first comparison on 4 bytes
|
272
|
+
if (n < 3)
|
273
|
+
return 0; // no match
|
274
|
+
|
275
|
+
lzvn_offset D = m_begin - m0_begin; // actual distance
|
276
|
+
if (D <= 0 || D > LZVN_ENCODE_MAX_DISTANCE)
|
277
|
+
return 0; // distance out of range
|
278
|
+
|
279
|
+
// Expand forward
|
280
|
+
lzvn_offset m_end = m_begin + n;
|
281
|
+
while (n == 4 && m_end + 4 < src_end) {
|
282
|
+
n = nmatch4(src, m_end, m_end - D);
|
283
|
+
m_end += n;
|
284
|
+
}
|
285
|
+
|
286
|
+
// Expand backwards over literal
|
287
|
+
while (m0_begin > src_begin && m_begin > l_begin &&
|
288
|
+
src[m_begin - 1] == src[m0_begin - 1]) {
|
289
|
+
m0_begin--;
|
290
|
+
m_begin--;
|
291
|
+
}
|
292
|
+
|
293
|
+
// OK, we keep it, update MATCH
|
294
|
+
lzvn_offset M = m_end - m_begin; // match length
|
295
|
+
match->m_begin = m_begin;
|
296
|
+
match->m_end = m_end;
|
297
|
+
match->K = M - ((D < 0x600) ? 2 : 3);
|
298
|
+
match->M = M;
|
299
|
+
match->D = D;
|
300
|
+
|
301
|
+
return 1; // OK
|
302
|
+
}
|
303
|
+
|
304
|
+
// ===============================================================
|
305
|
+
// Encoder Backend
|
306
|
+
|
307
|
+
/*! @abstract Emit a match and update state.
|
308
|
+
* @return number of bytes written to \p dst. May be 0 if there is no more space
|
309
|
+
* in \p dst to emit the match. */
|
310
|
+
static inline lzvn_offset lzvn_emit_match(lzvn_encoder_state *state,
|
311
|
+
lzvn_match_info match) {
|
312
|
+
size_t L = (size_t)(match.m_begin - state->src_literal); // literal count
|
313
|
+
size_t M = (size_t)match.M; // match length
|
314
|
+
size_t D = (size_t)match.D; // match distance
|
315
|
+
size_t D_prev = (size_t)state->d_prev; // previously emitted match distance
|
316
|
+
unsigned char *dst = emit(state->src + state->src_literal, state->dst,
|
317
|
+
state->dst_end, L, M, D, D_prev);
|
318
|
+
// Check if DST is full
|
319
|
+
if (dst >= state->dst_end) {
|
320
|
+
return 0; // FULL
|
321
|
+
}
|
322
|
+
|
323
|
+
// Update state
|
324
|
+
lzvn_offset dst_used = dst - state->dst;
|
325
|
+
state->d_prev = match.D;
|
326
|
+
state->dst = dst;
|
327
|
+
state->src_literal = match.m_end;
|
328
|
+
return dst_used;
|
329
|
+
}
|
330
|
+
|
331
|
+
/*! @abstract Emit a n-bytes literal and update state.
|
332
|
+
* @return number of bytes written to \p dst. May be 0 if there is no more space
|
333
|
+
* in \p dst to emit the literal. */
|
334
|
+
static inline lzvn_offset lzvn_emit_literal(lzvn_encoder_state *state,
|
335
|
+
lzvn_offset n) {
|
336
|
+
size_t L = (size_t)n;
|
337
|
+
unsigned char *dst = emit_literal(state->src + state->src_literal, state->dst,
|
338
|
+
state->dst_end, L);
|
339
|
+
// Check if DST is full
|
340
|
+
if (dst >= state->dst_end)
|
341
|
+
return 0; // FULL
|
342
|
+
|
343
|
+
// Update state
|
344
|
+
lzvn_offset dst_used = dst - state->dst;
|
345
|
+
state->dst = dst;
|
346
|
+
state->src_literal += n;
|
347
|
+
return dst_used;
|
348
|
+
}
|
349
|
+
|
350
|
+
/*! @abstract Emit end-of-stream and update state.
|
351
|
+
* @return number of bytes written to \p dst. May be 0 if there is no more space
|
352
|
+
* in \p dst to emit the instruction. */
|
353
|
+
static inline lzvn_offset lzvn_emit_end_of_stream(lzvn_encoder_state *state) {
|
354
|
+
// Do we have 8 byte in dst?
|
355
|
+
if (state->dst_end < state->dst + 8)
|
356
|
+
return 0; // FULL
|
357
|
+
|
358
|
+
// Insert end marker and update state
|
359
|
+
store8(state->dst, 0x06); // end-of-stream command
|
360
|
+
state->dst += 8;
|
361
|
+
return 8; // dst_used
|
362
|
+
}
|
363
|
+
|
364
|
+
// ===============================================================
|
365
|
+
// Encoder Functions
|
366
|
+
|
367
|
+
/*! @abstract Initialize encoder table in \p state, uses current I/O parameters. */
|
368
|
+
static inline void lzvn_init_table(lzvn_encoder_state *state) {
|
369
|
+
lzvn_offset index = -LZVN_ENCODE_MAX_DISTANCE; // max match distance
|
370
|
+
if (index < state->src_begin)
|
371
|
+
index = state->src_begin;
|
372
|
+
uint32_t value = load4(state->src + index);
|
373
|
+
|
374
|
+
lzvn_encode_entry_type e;
|
375
|
+
for (int i = 0; i < 4; i++) {
|
376
|
+
e.indices[i] = offset_to_s32(index);
|
377
|
+
e.values[i] = value;
|
378
|
+
}
|
379
|
+
for (int u = 0; u < LZVN_ENCODE_HASH_VALUES; u++)
|
380
|
+
state->table[u] = e; // fill entire table
|
381
|
+
}
|
382
|
+
|
383
|
+
void lzvn_encode(lzvn_encoder_state *state) {
|
384
|
+
const lzvn_match_info NO_MATCH = {0};
|
385
|
+
|
386
|
+
for (; state->src_current < state->src_current_end; state->src_current++) {
|
387
|
+
// Get 4 bytes at src_current
|
388
|
+
uint32_t vi = load4(state->src + state->src_current);
|
389
|
+
|
390
|
+
// Compute new hash H at position I, and push value into position table
|
391
|
+
int h = hash3i(vi); // index of first entry
|
392
|
+
|
393
|
+
// Read table entries for H
|
394
|
+
lzvn_encode_entry_type e = state->table[h];
|
395
|
+
|
396
|
+
// Update entry with index=current and value=vi
|
397
|
+
lzvn_encode_entry_type updated_e; // rotate values, so we will replace the oldest
|
398
|
+
updated_e.indices[0] = offset_to_s32(state->src_current);
|
399
|
+
updated_e.indices[1] = e.indices[0];
|
400
|
+
updated_e.indices[2] = e.indices[1];
|
401
|
+
updated_e.indices[3] = e.indices[2];
|
402
|
+
updated_e.values[0] = vi;
|
403
|
+
updated_e.values[1] = e.values[0];
|
404
|
+
updated_e.values[2] = e.values[1];
|
405
|
+
updated_e.values[3] = e.values[2];
|
406
|
+
|
407
|
+
// Do not check matches if still in previously emitted match
|
408
|
+
if (state->src_current < state->src_literal)
|
409
|
+
goto after_emit;
|
410
|
+
|
411
|
+
// Update best with candidate if better
|
412
|
+
#define UPDATE(best, candidate) \
|
413
|
+
do { \
|
414
|
+
if (candidate.K > best.K || \
|
415
|
+
((candidate.K == best.K) && (candidate.m_end > best.m_end + 1))) { \
|
416
|
+
best = candidate; \
|
417
|
+
} \
|
418
|
+
} while (0)
|
419
|
+
// Check candidate. Keep if better.
|
420
|
+
#define CHECK_CANDIDATE(ik, nk) \
|
421
|
+
do { \
|
422
|
+
lzvn_match_info m1; \
|
423
|
+
if (lzvn_find_matchN(state->src, state->src_begin, state->src_end, \
|
424
|
+
state->src_literal, ik, state->src_current, nk, &m1)) { \
|
425
|
+
UPDATE(incoming, m1); \
|
426
|
+
} \
|
427
|
+
} while (0)
|
428
|
+
// Emit match M. Return if we don't have enough space in the destination buffer
|
429
|
+
#define EMIT_MATCH(m) \
|
430
|
+
do { \
|
431
|
+
if (lzvn_emit_match(state, m) == 0) \
|
432
|
+
return; \
|
433
|
+
} while (0)
|
434
|
+
// Emit literal of length L. Return if we don't have enough space in the
|
435
|
+
// destination buffer
|
436
|
+
#define EMIT_LITERAL(l) \
|
437
|
+
do { \
|
438
|
+
if (lzvn_emit_literal(state, l) == 0) \
|
439
|
+
return; \
|
440
|
+
} while (0)
|
441
|
+
|
442
|
+
lzvn_match_info incoming = NO_MATCH;
|
443
|
+
|
444
|
+
// Check candidates in order (closest first)
|
445
|
+
uint32_t diffs[4];
|
446
|
+
for (int k = 0; k < 4; k++)
|
447
|
+
diffs[k] = e.values[k] ^ vi; // XOR, 0 if equal
|
448
|
+
lzvn_offset ik; // index
|
449
|
+
lzvn_offset nk; // match byte count
|
450
|
+
|
451
|
+
// The values stored in e.xyzw are 32-bit signed indices, extended to signed
|
452
|
+
// type lzvn_offset
|
453
|
+
ik = offset_from_s32(e.indices[0]);
|
454
|
+
nk = trailing_zero_bytes(diffs[0]);
|
455
|
+
CHECK_CANDIDATE(ik, nk);
|
456
|
+
ik = offset_from_s32(e.indices[1]);
|
457
|
+
nk = trailing_zero_bytes(diffs[1]);
|
458
|
+
CHECK_CANDIDATE(ik, nk);
|
459
|
+
ik = offset_from_s32(e.indices[2]);
|
460
|
+
nk = trailing_zero_bytes(diffs[2]);
|
461
|
+
CHECK_CANDIDATE(ik, nk);
|
462
|
+
ik = offset_from_s32(e.indices[3]);
|
463
|
+
nk = trailing_zero_bytes(diffs[3]);
|
464
|
+
CHECK_CANDIDATE(ik, nk);
|
465
|
+
|
466
|
+
// Check candidate at previous distance
|
467
|
+
if (state->d_prev != 0) {
|
468
|
+
lzvn_match_info m1;
|
469
|
+
if (lzvn_find_match(state->src, state->src_begin, state->src_end,
|
470
|
+
state->src_literal, state->src_current - state->d_prev,
|
471
|
+
state->src_current, &m1)) {
|
472
|
+
m1.K = m1.M - 1; // fix K for D_prev
|
473
|
+
UPDATE(incoming, m1);
|
474
|
+
}
|
475
|
+
}
|
476
|
+
|
477
|
+
// Here we have the best candidate in incoming, may be NO_MATCH
|
478
|
+
|
479
|
+
// If no incoming match, and literal backlog becomes too high, emit pending
|
480
|
+
// match, or literals if there is no pending match
|
481
|
+
if (incoming.M == 0) {
|
482
|
+
if (state->src_current - state->src_literal >=
|
483
|
+
LZVN_ENCODE_MAX_LITERAL_BACKLOG) // at this point, we always have
|
484
|
+
// current >= literal
|
485
|
+
{
|
486
|
+
if (state->pending.M != 0) {
|
487
|
+
EMIT_MATCH(state->pending);
|
488
|
+
state->pending = NO_MATCH;
|
489
|
+
} else {
|
490
|
+
EMIT_LITERAL(271); // emit long literal (271 is the longest literal size we allow)
|
491
|
+
}
|
492
|
+
}
|
493
|
+
goto after_emit;
|
494
|
+
}
|
495
|
+
|
496
|
+
if (state->pending.M == 0) {
|
497
|
+
// NOTE. Here, we can also emit incoming right away. It will make the
|
498
|
+
// encoder 1.5x faster, at a cost of ~10% lower compression ratio:
|
499
|
+
// EMIT_MATCH(incoming);
|
500
|
+
// state->pending = NO_MATCH;
|
501
|
+
|
502
|
+
// No pending match, emit nothing, keep incoming
|
503
|
+
state->pending = incoming;
|
504
|
+
} else {
|
505
|
+
// Here we have both incoming and pending
|
506
|
+
if (state->pending.m_end <= incoming.m_begin) {
|
507
|
+
// No overlap: emit pending, keep incoming
|
508
|
+
EMIT_MATCH(state->pending);
|
509
|
+
state->pending = incoming;
|
510
|
+
} else {
|
511
|
+
// If pending is better, emit pending and discard incoming.
|
512
|
+
// Otherwise, emit incoming and discard pending.
|
513
|
+
if (incoming.K > state->pending.K)
|
514
|
+
state->pending = incoming;
|
515
|
+
EMIT_MATCH(state->pending);
|
516
|
+
state->pending = NO_MATCH;
|
517
|
+
}
|
518
|
+
}
|
519
|
+
|
520
|
+
after_emit:
|
521
|
+
|
522
|
+
// We commit state changes only after we tried to emit instructions, so we
|
523
|
+
// can restart in the same state in case dst was full and we quit the loop.
|
524
|
+
state->table[h] = updated_e;
|
525
|
+
|
526
|
+
} // i loop
|
527
|
+
|
528
|
+
// Do not emit pending match here. We do it only at the end of stream.
|
529
|
+
}
|
530
|
+
|
531
|
+
// ===============================================================
|
532
|
+
// API entry points
|
533
|
+
|
534
|
+
size_t lzvn_encode_scratch_size(void) { return LZVN_ENCODE_WORK_SIZE; }
|
535
|
+
|
536
|
+
static size_t lzvn_encode_partial(void *__restrict dst, size_t dst_size,
|
537
|
+
const void *__restrict src, size_t src_size,
|
538
|
+
size_t *src_used, void *__restrict work) {
|
539
|
+
// Min size checks to avoid accessing memory outside buffers.
|
540
|
+
if (dst_size < LZVN_ENCODE_MIN_DST_SIZE) {
|
541
|
+
*src_used = 0;
|
542
|
+
return 0;
|
543
|
+
}
|
544
|
+
// Max input size check (limit to offsets on uint32_t).
|
545
|
+
if (src_size > LZVN_ENCODE_MAX_SRC_SIZE) {
|
546
|
+
src_size = LZVN_ENCODE_MAX_SRC_SIZE;
|
547
|
+
}
|
548
|
+
|
549
|
+
// Setup encoder state
|
550
|
+
lzvn_encoder_state state;
|
551
|
+
memset(&state, 0, sizeof(state));
|
552
|
+
|
553
|
+
state.src = src;
|
554
|
+
state.src_begin = 0;
|
555
|
+
state.src_end = (lzvn_offset)src_size;
|
556
|
+
state.src_literal = 0;
|
557
|
+
state.src_current = 0;
|
558
|
+
state.dst = dst;
|
559
|
+
state.dst_begin = dst;
|
560
|
+
state.dst_end = (unsigned char *)dst + dst_size - 8; // reserve 8 bytes for end-of-stream
|
561
|
+
state.table = work;
|
562
|
+
|
563
|
+
// Do not encode if the input buffer is too small. We'll emit a literal instead.
|
564
|
+
if (src_size >= LZVN_ENCODE_MIN_SRC_SIZE) {
|
565
|
+
|
566
|
+
state.src_current_end = (lzvn_offset)src_size - LZVN_ENCODE_MIN_MARGIN;
|
567
|
+
lzvn_init_table(&state);
|
568
|
+
lzvn_encode(&state);
|
569
|
+
|
570
|
+
}
|
571
|
+
|
572
|
+
// No need to test the return value: src_literal will not be updated on failure,
|
573
|
+
// and we will fail later.
|
574
|
+
lzvn_emit_literal(&state, state.src_end - state.src_literal);
|
575
|
+
|
576
|
+
// Restore original size, so end-of-stream always succeeds, and emit it
|
577
|
+
state.dst_end = (unsigned char *)dst + dst_size;
|
578
|
+
lzvn_emit_end_of_stream(&state);
|
579
|
+
|
580
|
+
*src_used = state.src_literal;
|
581
|
+
return (size_t)(state.dst - state.dst_begin);
|
582
|
+
}
|
583
|
+
|
584
|
+
size_t lzvn_encode_buffer(void *__restrict dst, size_t dst_size,
|
585
|
+
const void *__restrict src, size_t src_size,
|
586
|
+
void *__restrict work) {
|
587
|
+
size_t src_used = 0;
|
588
|
+
size_t dst_used =
|
589
|
+
lzvn_encode_partial(dst, dst_size, src, src_size, &src_used, work);
|
590
|
+
if (src_used != src_size)
|
591
|
+
return 0; // could not encode entire input stream = fail
|
592
|
+
return dst_used; // return encoded size
|
593
|
+
}
|