| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 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 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 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 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 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 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 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 |
|
| 78 |
typedef struct { |
| 79 |
|
| 80 |
uint32_t n_matches; |
| 81 |
|
| 82 |
uint32_t n_lmd_payload_bytes; |
| 83 |
|
| 84 |
const uint8_t *current_literal; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
int32_t l_value, m_value, d_value; |
| 91 |
|
| 92 |
fse_in_stream lmd_in_stream; |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
uint32_t lmd_in_buf; |
| 99 |
|
| 100 |
uint16_t l_state, m_state, d_state; |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 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 |
|
| 112 |
|
| 113 |
|
| 114 |
uint8_t literals[LZFSE_LITERALS_PER_BLOCK + 64]; |
| 115 |
} lzfse_compressed_block_decoder_state; |
| 116 |
|
| 117 |
|
| 118 |
typedef struct { |
| 119 |
uint32_t n_raw_bytes; |
| 120 |
} uncompressed_block_decoder_state; |
| 121 |
|
| 122 |
|
| 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 |
|
| 130 |
typedef struct { |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
const uint8_t *src; |
| 136 |
|
| 137 |
const uint8_t *src_begin; |
| 138 |
|
| 139 |
const uint8_t *src_end; |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
uint8_t *dst; |
| 146 |
|
| 147 |
uint8_t *dst_begin; |
| 148 |
|
| 149 |
uint8_t *dst_end; |
| 150 |
|
| 151 |
int end_of_stream; |
| 152 |
|
| 153 |
|
| 154 |
|
| 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 |
|
| 163 |
|
| 164 |
#define LZFSE_NO_BLOCK_MAGIC 0x00000000 |
| 165 |
#define LZFSE_ENDOFSTREAM_BLOCK_MAGIC 0x24787662 |
| 166 |
#define LZFSE_UNCOMPRESSED_BLOCK_MAGIC 0x2d787662 |
| 167 |
#define LZFSE_COMPRESSEDV1_BLOCK_MAGIC 0x31787662 |
| 168 |
#define LZFSE_COMPRESSEDV2_BLOCK_MAGIC 0x32787662 |
| 169 |
#define LZFSE_COMPRESSEDLZVN_BLOCK_MAGIC 0x6e787662 |
| 170 |
|
| 171 |
|
| 172 |
typedef struct { |
| 173 |
|
| 174 |
uint32_t magic; |
| 175 |
|
| 176 |
uint32_t n_raw_bytes; |
| 177 |
} uncompressed_block_header; |
| 178 |
|
| 179 |
|
| 180 |
typedef struct { |
| 181 |
|
| 182 |
uint32_t magic; |
| 183 |
|
| 184 |
uint32_t n_raw_bytes; |
| 185 |
|
| 186 |
uint32_t n_payload_bytes; |
| 187 |
|
| 188 |
uint32_t n_literals; |
| 189 |
|
| 190 |
uint32_t n_matches; |
| 191 |
|
| 192 |
uint32_t n_literal_payload_bytes; |
| 193 |
|
| 194 |
uint32_t n_lmd_payload_bytes; |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
int32_t literal_bits; |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
uint16_t literal_state[4]; |
| 207 |
|
| 208 |
int32_t lmd_bits; |
| 209 |
|
| 210 |
uint16_t l_state; |
| 211 |
|
| 212 |
uint16_t m_state; |
| 213 |
|
| 214 |
uint16_t d_state; |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 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 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
typedef struct { |
| 232 |
|
| 233 |
uint32_t magic; |
| 234 |
|
| 235 |
uint32_t n_raw_bytes; |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
uint64_t packed_fields[3]; |
| 266 |
|
| 267 |
|
| 268 |
|
| 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 |
|
| 275 |
typedef struct { |
| 276 |
|
| 277 |
uint32_t magic; |
| 278 |
|
| 279 |
uint32_t n_raw_bytes; |
| 280 |
|
| 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 |
|
| 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 |
|
| 297 |
|
| 298 |
|
| 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 |
|
| 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 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 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 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 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 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 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 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 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 |
|
| 442 |
return tests_results | 0x80000000; |
| 443 |
} |
| 444 |
|
| 445 |
return 0; |
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 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 |