Debian Codegraph @

lzfse/lzfse_internal.h

raw symbols SCIP
1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /*
3 * Copyright (c) 2015-2016, Apple Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8 *
9 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
10 * in the documentation and/or other materials provided with the distribution.
11 *
12 * 3. Neither the name of the copyright holder(s) nor the names of any contributors may be used to endorse or promote products derived
13 * from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
17 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
19 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
20 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
21 */
22
23 #ifndef LZFSE_INTERNAL_H
24 #define LZFSE_INTERNAL_H
25
26 #include "lzfse_fse.h"
27 #include <linux/limits.h>
28 #include <linux/stddef.h>
29
30 /*
31 * Throughout LZFSE we refer to "L", "M" and "D"; these will always appear as
32 * a triplet, and represent a "usual" LZ-style literal and match pair. "L"
33 * is the number of literal bytes, "M" is the number of match bytes, and "D"
34 * is the match "distance"; the distance in bytes between the current pointer
35 * and the start of the match.
36 */
37 #define LZFSE_ENCODE_HASH_VALUES (1 << LZFSE_ENCODE_HASH_BITS)
38 #define LZFSE_ENCODE_L_SYMBOLS 20
39 #define LZFSE_ENCODE_M_SYMBOLS 20
40 #define LZFSE_ENCODE_D_SYMBOLS 64
41 #define LZFSE_ENCODE_LITERAL_SYMBOLS 256
42 #define LZFSE_ENCODE_L_STATES 64
43 #define LZFSE_ENCODE_M_STATES 64
44 #define LZFSE_ENCODE_D_STATES 256
45 #define LZFSE_ENCODE_LITERAL_STATES 1024
46 #define LZFSE_MATCHES_PER_BLOCK 10000
47 #define LZFSE_LITERALS_PER_BLOCK (4 * LZFSE_MATCHES_PER_BLOCK)
48 #define LZFSE_DECODE_LITERALS_PER_BLOCK (4 * LZFSE_DECODE_MATCHES_PER_BLOCK)
49
50 /*
51 * LZFSE internal status. These values are used by internal LZFSE routines
52 * as return codes. There should not be any good reason to change their
53 * values; it is plausible that additional codes might be added in the
54 * future.
55 */
56 #define LZFSE_STATUS_OK 0
57 #define LZFSE_STATUS_SRC_EMPTY -1
58 #define LZFSE_STATUS_DST_FULL -2
59 #define LZFSE_STATUS_ERROR -3
60
61 /*
62 * Type representing an offset between elements in a buffer. On 64-bit
63 * systems, this is stored in a 64-bit container to avoid extra sign-
64 * extension operations in addressing arithmetic, but the value is always
65 * representable as a 32-bit signed value in LZFSE's usage.
66 */
67 #if defined(_M_AMD64) || defined(__x86_64__) || defined(__arm64__)
68 typedef int64_t lzfse_offset;
69 #else
70 typedef int32_t lzfse_offset;
71 #endif
72
73 #if !defined(__APPLE__) || !defined(__MACH__)
74 typedef uint64_t uintmax_t;
75 #endif
76
77 /*! @abstract Decoder state object for lzfse compressed blocks. */
78 typedef struct {
79 /* Number of matches remaining in the block. */
80 uint32_t n_matches;
81 /* Number of bytes used to encode L, M, D triplets for the block. */
82 uint32_t n_lmd_payload_bytes;
83 /* Pointer to the next literal to emit. */
84 const uint8_t *current_literal;
85 /*
86 * L, M, D triplet for the match currently being emitted. This is used
87 * only if we need to restart after reaching the end of the destination
88 * buffer in the middle of a literal or match.
89 */
90 int32_t l_value, m_value, d_value;
91 /* FSE stream object. */
92 fse_in_stream lmd_in_stream;
93 /*
94 * Offset of L,M,D encoding in the input buffer. Because we read through
95 * an FSE stream *backwards* while decoding, this is decremented as we
96 * move through a block.
97 */
98 uint32_t lmd_in_buf;
99 /* The current state of the L, M, and D FSE decoders. */
100 uint16_t l_state, m_state, d_state;
101 /*
102 * Internal FSE decoder tables for the current block. These have
103 * alignment forced to 8 bytes to guarantee that a single state's
104 * entry cannot span two cachelines.
105 */
106 struct fse_value_decoder_entry l_decoder[LZFSE_ENCODE_L_STATES] __aligned(8);
107 struct fse_value_decoder_entry m_decoder[LZFSE_ENCODE_M_STATES] __aligned(8);
108 struct fse_value_decoder_entry d_decoder[LZFSE_ENCODE_D_STATES] __aligned(8);
109 int32_t literal_decoder[LZFSE_ENCODE_LITERAL_STATES];
110 /*
111 * The literal stream for the block, plus padding to allow for faster
112 * copy operations.
113 */
114 uint8_t literals[LZFSE_LITERALS_PER_BLOCK + 64];
115 } lzfse_compressed_block_decoder_state;
116
117 /* Decoder state object for uncompressed blocks. */
118 typedef struct {
119 uint32_t n_raw_bytes;
120 } uncompressed_block_decoder_state;
121
122 /*! @abstract Decoder state object for lzvn-compressed blocks. */
123 typedef struct {
124 uint32_t n_raw_bytes;
125 uint32_t n_payload_bytes;
126 uint32_t d_prev;
127 } lzvn_compressed_block_decoder_state;
128
129 /*! @abstract Decoder state object. */
130 typedef struct {
131 /*
132 * Pointer to next byte to read from source buffer (this is advanced as
133 * we decode; src_begin describe the buffer and do not change).
134 */
135 const uint8_t *src;
136 /* Pointer to first byte of source buffer. */
137 const uint8_t *src_begin;
138 /* Pointer to one byte past the end of the source buffer. */
139 const uint8_t *src_end;
140 /*
141 * Pointer to the next byte to write to destination buffer (this is
142 * advanced as we decode; dst_begin and dst_end describe the buffer and
143 * do not change).
144 */
145 uint8_t *dst;
146 /* Pointer to first byte of destination buffer. */
147 uint8_t *dst_begin;
148 /* Pointer to one byte past the end of the destination buffer. */
149 uint8_t *dst_end;
150 /* 1 if we have reached the end of the stream, 0 otherwise. */
151 int end_of_stream;
152 /*
153 * magic number of the current block if we are within a block,
154 * LZFSE_NO_BLOCK_MAGIC otherwise.
155 */
156 uint32_t block_magic;
157 lzfse_compressed_block_decoder_state compressed_lzfse_block_state;
158 lzvn_compressed_block_decoder_state compressed_lzvn_block_state;
159 uncompressed_block_decoder_state uncompressed_block_state;
160 } lzfse_decoder_state;
161
162 /* MARK: - Block header objects */
163
164 #define LZFSE_NO_BLOCK_MAGIC 0x00000000 /* 0 (invalid) */
165 #define LZFSE_ENDOFSTREAM_BLOCK_MAGIC 0x24787662 /* bvx$ (end of stream) */
166 #define LZFSE_UNCOMPRESSED_BLOCK_MAGIC 0x2d787662 /* bvx- (raw data) */
167 #define LZFSE_COMPRESSEDV1_BLOCK_MAGIC 0x31787662 /* bvx1 (lzfse compressed, uncompressed tables) */
168 #define LZFSE_COMPRESSEDV2_BLOCK_MAGIC 0x32787662 /* bvx2 (lzfse compressed, compressed tables) */
169 #define LZFSE_COMPRESSEDLZVN_BLOCK_MAGIC 0x6e787662 /* bvxn (lzvn compressed) */
170
171 /*! @abstract Uncompressed block header in encoder stream. */
172 typedef struct {
173 /* Magic number, always LZFSE_UNCOMPRESSED_BLOCK_MAGIC. */
174 uint32_t magic;
175 /* Number of raw bytes in block. */
176 uint32_t n_raw_bytes;
177 } uncompressed_block_header;
178
179 /*! @abstract Compressed block header with uncompressed tables. */
180 typedef struct {
181 /* Magic number, always LZFSE_COMPRESSEDV1_BLOCK_MAGIC. */
182 uint32_t magic;
183 /* Number of decoded (output) bytes in block. */
184 uint32_t n_raw_bytes;
185 /* Number of encoded (source) bytes in block. */
186 uint32_t n_payload_bytes;
187 /* Number of literal bytes output by block (*not* the number of literals). */
188 uint32_t n_literals;
189 /* Number of matches in block (which is also the number of literals). */
190 uint32_t n_matches;
191 /* Number of bytes used to encode literals. */
192 uint32_t n_literal_payload_bytes;
193 /* Number of bytes used to encode matches. */
194 uint32_t n_lmd_payload_bytes;
195
196 /*
197 * Final encoder states for the block, which will be the initial states
198 * for the decoder:
199 */
200 /* Final accum_nbits for literals stream. */
201 int32_t literal_bits;
202 /*
203 * There are four interleaved streams of literals, so there are four
204 * final states.
205 */
206 uint16_t literal_state[4];
207 /* accum_nbits for the l, m, d stream. */
208 int32_t lmd_bits;
209 /* Final L (literal length) state. */
210 uint16_t l_state;
211 /* Final M (match length) state. */
212 uint16_t m_state;
213 /* Final D (match distance) state. */
214 uint16_t d_state;
215
216 /*
217 * Normalized frequency tables for each stream. Sum of values in each
218 * array is the number of states.
219 */
220 uint16_t l_freq[LZFSE_ENCODE_L_SYMBOLS];
221 uint16_t m_freq[LZFSE_ENCODE_M_SYMBOLS];
222 uint16_t d_freq[LZFSE_ENCODE_D_SYMBOLS];
223 uint16_t literal_freq[LZFSE_ENCODE_LITERAL_SYMBOLS];
224 } lzfse_compressed_block_header_v1;
225
226 /*! @abstract Compressed block header with compressed tables. Note that because
227 * freq[] is compressed, the structure-as-stored-in-the-stream is *truncated*;
228 * we only store the used bytes of freq[]. This means that some extra care must
229 * be taken when reading one of these headers from the stream.
230 */
231 typedef struct {
232 /* Magic number, always LZFSE_COMPRESSEDV2_BLOCK_MAGIC. */
233 uint32_t magic;
234 /* Number of decoded (output) bytes in block. */
235 uint32_t n_raw_bytes;
236 /*
237 * The fields n_payload_bytes ... d_state from the
238 * lzfse_compressed_block_header_v1 object are packed into three 64-bit
239 * fields in the compressed header, as follows:
240 *
241 * offset bits value
242 * 0 20 n_literals
243 * 20 20 n_literal_payload_bytes
244 * 40 20 n_matches
245 * 60 3 literal_bits
246 * 63 1 --- unused ---
247 *
248 * 0 10 literal_state[0]
249 * 10 10 literal_state[1]
250 * 20 10 literal_state[2]
251 * 30 10 literal_state[3]
252 * 40 20 n_lmd_payload_bytes
253 * 60 3 lmd_bits
254 * 63 1 --- unused ---
255 *
256 * 0 32 header_size (total header size in bytes; this does not
257 * correspond to a field in the uncompressed header version,
258 * but is required; we wouldn't know the size of the
259 * compresssed header otherwise.
260 * 32 10 l_state
261 * 42 10 m_state
262 * 52 10 d_state
263 * 62 2 --- unused ---
264 */
265 uint64_t packed_fields[3];
266 /*
267 * Variable size freq tables, using a Huffman-style fixed encoding.
268 * Size allocated here is an upper bound (all values stored on 16 bits).
269 */
270 uint8_t freq[2 * (LZFSE_ENCODE_L_SYMBOLS + LZFSE_ENCODE_M_SYMBOLS + LZFSE_ENCODE_D_SYMBOLS +
271 LZFSE_ENCODE_LITERAL_SYMBOLS)];
272 } __packed __aligned(1) lzfse_compressed_block_header_v2;
273
274 /*! @abstract LZVN compressed block header. */
275 typedef struct {
276 /* Magic number, always LZFSE_COMPRESSEDLZVN_BLOCK_MAGIC. */
277 uint32_t magic;
278 /* Number of decoded (output) bytes. */
279 uint32_t n_raw_bytes;
280 /* Number of encoded (source) bytes. */
281 uint32_t n_payload_bytes;
282 } lzvn_compressed_block_header;
283
284 int lzfse_decode(lzfse_decoder_state *s);
285 size_t lzvn_decode_scratch_size(void);
286 size_t lzvn_decode_buffer(void *__restrict dst, size_t dst_size, const void *__restrict src,
287 size_t src_size);
288
289 /*! @abstract Signed offset in buffers, stored on either 32 or 64 bits. */
290 #if defined(_M_AMD64) || defined(__x86_64__) || defined(__arm64__)
291 typedef int64_t lzvn_offset;
292 #else
293 typedef int32_t lzvn_offset;
294 #endif
295
296 /* MARK: - LZFSE utility functions */
297
298 /*! @abstract Load bytes from memory location SRC. */
299 static __always_inline uint16_t load2(const void *ptr)
300 {
301 uint16_t data;
302
303 memcpy(&data, ptr, sizeof(data));
304 return data;
305 }
306
307 static __always_inline uint32_t load4(const void *ptr)
308 {
309 uint32_t data;
310
311 memcpy(&data, ptr, sizeof(data));
312 return data;
313 }
314
315 static __always_inline uint64_t load8(const void *ptr)
316 {
317 uint64_t data;
318
319 memcpy(&data, ptr, sizeof(data));
320 return data;
321 }
322
323 /*! @abstract Store bytes to memory location DST. */
324 static __always_inline void store2(void *ptr, uint16_t data)
325 {
326 memcpy(ptr, &data, sizeof(data));
327 }
328
329 static __always_inline void store4(void *ptr, uint32_t data)
330 {
331 memcpy(ptr, &data, sizeof(data));
332 }
333
334 static __always_inline void store8(void *ptr, uint64_t data)
335 {
336 memcpy(ptr, &data, sizeof(data));
337 }
338
339 /*! @abstract Load+store bytes from locations SRC to DST. Not intended for use
340 * with overlapping buffers. Note that for LZ-style compression, you need
341 * copies to behave like naive memcpy( ) implementations do, splatting the
342 * leading sequence if the buffers overlap. This copy does not do that, so
343 * should not be used with overlapping buffers.
344 */
345 static __always_inline void copy8(void *dst, const void *src)
346 {
347 store8(dst, load8(src));
348 }
349 static __always_inline void copy16(void *dst, const void *src)
350 {
351 uint64_t m0 = load8(src);
352 uint64_t m1 = load8((const unsigned char *)src + 8);
353
354 store8(dst, m0);
355 store8((unsigned char *)dst + 8, m1);
356 }
357
358 /*
359 * ===============================================================
360 * Bitfield Operations
361 */
362
363 /*! @abstract Extracts \p width bits from \p container, starting with \p lsb; if
364 * we view \p container as a bit array, we extract \c container[lsb:lsb+width].
365 */
366 static __always_inline uintmax_t extract(uintmax_t container, unsigned int lsb, unsigned int width)
367 {
368 static const size_t container_width = sizeof(container) * 8;
369
370 if (width == container_width)
371 return container;
372 return (container >> lsb) & (((uintmax_t)1 << width) - 1);
373 }
374
375 /*! @abstract Inserts \p width bits from \p data into \p container, starting with \p lsb.
376 * Viewed as bit arrays, the operations is:
377 * @code
378 * container[:lsb] is unchanged
379 * container[lsb:lsb+width] <-- data[0:width]
380 * container[lsb+width:] is unchanged
381 * @endcode
382 */
383 static __always_inline uintmax_t insert(uintmax_t container, uintmax_t data, unsigned int lsb,
384 unsigned int width)
385 {
386 static const size_t container_width = sizeof(container) * 8;
387 uintmax_t mask;
388
389 if (width == container_width)
390 return container;
391 mask = ((uintmax_t)1 << width) - 1;
392 return (container & ~(mask << lsb)) | (data & mask) << lsb;
393 }
394
395 /*! @abstract Perform sanity checks on the values of lzfse_compressed_block_header_v1.
396 * Test that the field values are in the allowed limits, verify that the
397 * frequency tables sum to value less than total number of states.
398 * @return 0 if all tests passed.
399 * @return negative error code with 1 bit set for each failed test.
400 */
401 static __always_inline int
402 lzfse_check_block_header_v1(const lzfse_compressed_block_header_v1 *header)
403 {
404 int tests_results = 0;
405 uint16_t literal_state[4];
406 int res;
407
408 tests_results = tests_results |
409 ((header->magic == LZFSE_COMPRESSEDV1_BLOCK_MAGIC) ? 0 : (1 << 0));
410 tests_results = tests_results |
411 ((header->n_literals <= LZFSE_LITERALS_PER_BLOCK) ? 0 : (1 << 1));
412 tests_results = tests_results |
413 ((header->n_matches <= LZFSE_MATCHES_PER_BLOCK) ? 0 : (1 << 2));
414
415 memcpy(literal_state, header->literal_state, sizeof(uint16_t) * 4);
416
417 tests_results = tests_results |
418 ((literal_state[0] < LZFSE_ENCODE_LITERAL_STATES) ? 0 : (1 << 3));
419 tests_results = tests_results |
420 ((literal_state[1] < LZFSE_ENCODE_LITERAL_STATES) ? 0 : (1 << 4));
421 tests_results = tests_results |
422 ((literal_state[2] < LZFSE_ENCODE_LITERAL_STATES) ? 0 : (1 << 5));
423 tests_results = tests_results |
424 ((literal_state[3] < LZFSE_ENCODE_LITERAL_STATES) ? 0 : (1 << 6));
425
426 tests_results = tests_results | ((header->l_state < LZFSE_ENCODE_L_STATES) ? 0 : (1 << 7));
427 tests_results = tests_results | ((header->m_state < LZFSE_ENCODE_M_STATES) ? 0 : (1 << 8));
428 tests_results = tests_results | ((header->d_state < LZFSE_ENCODE_D_STATES) ? 0 : (1 << 9));
429
430 res = fse_check_freq(header->l_freq, LZFSE_ENCODE_L_SYMBOLS, LZFSE_ENCODE_L_STATES);
431 tests_results = tests_results | ((res == 0) ? 0 : (1 << 10));
432 res = fse_check_freq(header->m_freq, LZFSE_ENCODE_M_SYMBOLS, LZFSE_ENCODE_M_STATES);
433 tests_results = tests_results | ((res == 0) ? 0 : (1 << 11));
434 res = fse_check_freq(header->d_freq, LZFSE_ENCODE_D_SYMBOLS, LZFSE_ENCODE_D_STATES);
435 tests_results = tests_results | ((res == 0) ? 0 : (1 << 12));
436 res = fse_check_freq(header->literal_freq, LZFSE_ENCODE_LITERAL_SYMBOLS,
437 LZFSE_ENCODE_LITERAL_STATES);
438 tests_results = tests_results | ((res == 0) ? 0 : (1 << 13));
439
440 if (tests_results) {
441 /* each 1 bit is a test that failed (except for the sign bit) */
442 return tests_results | 0x80000000;
443 }
444
445 return 0; /* OK */
446 }
447
448 /*! @abstract The L, M, D data streams are all encoded as a "base" value, which is
449 * FSE-encoded, and an "extra bits" value, which is the difference between
450 * value and base, and is simply represented as a raw bit value (because it
451 * is the low-order bits of a larger number, not much entropy can be
452 * extracted from these bits by more complex encoding schemes). The following
453 * tables represent the number of low-order bits to encode separately and the
454 * base values for each of L, M, and D.
455 *
456 * @note The inverse tables for mapping the other way are significantly larger.
457 * Those tables have been split out to lzfse_encode_tables.h in order to keep
458 * this file relatively small.
459 */
460 static const uint8_t l_extra_bits[LZFSE_ENCODE_L_SYMBOLS] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
461 0, 0, 0, 0, 0, 0, 2, 3, 5, 8 };
462 static const int32_t l_base_value[LZFSE_ENCODE_L_SYMBOLS] = {
463 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 20, 28, 60
464 };
465 static const uint8_t m_extra_bits[LZFSE_ENCODE_M_SYMBOLS] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
466 0, 0, 0, 0, 0, 0, 3, 5, 8, 11 };
467 static const int32_t m_base_value[LZFSE_ENCODE_M_SYMBOLS] = { 0, 1, 2, 3, 4, 5, 6,
468 7, 8, 9, 10, 11, 12, 13,
469 14, 15, 16, 24, 56, 312 };
470 static const uint8_t d_extra_bits[LZFSE_ENCODE_D_SYMBOLS] = {
471 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5,
472 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10,
473 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15
474 };
475 static const int32_t d_base_value[LZFSE_ENCODE_D_SYMBOLS] = {
476 0, 1, 2, 3, 4, 6, 8, 10, 12, 16, 20,
477 24, 28, 36, 44, 52, 60, 76, 92, 108, 124, 156,
478 188, 220, 252, 316, 380, 444, 508, 636, 764, 892, 1020,
479 1276, 1532, 1788, 2044, 2556, 3068, 3580, 4092, 5116, 6140, 7164,
480 8188, 10236, 12284, 14332, 16380, 20476, 24572, 28668, 32764, 40956, 49148,
481 57340, 65532, 81916, 98300, 114684, 131068, 163836, 196604, 229372
482 };
483
484 #endif /* LZFSE_INTERNAL_H */