fix for client crash in lf hitag eload. Hitag simulation still now working (wip)
[RRG-proxmark3.git] / common / lz4 / lz4.c
blobbfa4e6294db63e5a3282b653131c08df7caee2df
1 /*
2 LZ4 - Fast LZ compression algorithm
3 Copyright (C) 2011-present, Yann Collet.
5 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are
9 met:
11 * Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 * Redistributions in binary form must reproduce the above
14 copyright notice, this list of conditions and the following disclaimer
15 in the documentation and/or other materials provided with the
16 distribution.
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 You can contact the author at :
31 - LZ4 homepage : http://www.lz4.org
32 - LZ4 source repository : https://github.com/lz4/lz4
35 /*-************************************
36 * Tuning parameters
37 **************************************/
39 * LZ4_HEAPMODE :
40 * Select how default compression functions will allocate memory for their hash table,
41 * in memory stack (0:default, fastest), or in memory heap (1:requires malloc()).
43 #ifndef LZ4_HEAPMODE
44 # define LZ4_HEAPMODE 0
45 #endif
48 * LZ4_ACCELERATION_DEFAULT :
49 * Select "acceleration" for LZ4_compress_fast() when parameter value <= 0
51 #define LZ4_ACCELERATION_DEFAULT 1
53 * LZ4_ACCELERATION_MAX :
54 * Any "acceleration" value higher than this threshold
55 * get treated as LZ4_ACCELERATION_MAX instead (fix #876)
57 #define LZ4_ACCELERATION_MAX 65537
60 /*-************************************
61 * CPU Feature Detection
62 **************************************/
63 /* LZ4_FORCE_MEMORY_ACCESS
64 * By default, access to unaligned memory is controlled by `memcpy()`, which is safe and portable.
65 * Unfortunately, on some target/compiler combinations, the generated assembly is sub-optimal.
66 * The below switch allow to select different access method for improved performance.
67 * Method 0 (default) : use `memcpy()`. Safe and portable.
68 * Method 1 : `__packed` statement. It depends on compiler extension (ie, not portable).
69 * This method is safe if your compiler supports it, and *generally* as fast or faster than `memcpy`.
70 * Method 2 : direct access. This method is portable but violate C standard.
71 * It can generate buggy code on targets which assembly generation depends on alignment.
72 * But in some circumstances, it's the only known way to get the most performance (ie GCC + ARMv6)
73 * See https://fastcompression.blogspot.fr/2015/08/accessing-unaligned-memory.html for details.
74 * Prefer these methods in priority order (0 > 1 > 2)
76 #ifndef LZ4_FORCE_MEMORY_ACCESS /* can be defined externally */
77 # if defined(__GNUC__) && \
78 ( defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) \
79 || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_6T2__) )
80 # define LZ4_FORCE_MEMORY_ACCESS 2
81 # elif (defined(__INTEL_COMPILER) && !defined(_WIN32)) || defined(__GNUC__)
82 # define LZ4_FORCE_MEMORY_ACCESS 1
83 # endif
84 #endif
87 * LZ4_FORCE_SW_BITCOUNT
88 * Define this parameter if your target system or compiler does not support hardware bit count
90 #if defined(_MSC_VER) && defined(_WIN32_WCE) /* Visual Studio for WinCE doesn't support Hardware bit count */
91 # undef LZ4_FORCE_SW_BITCOUNT /* avoid double def */
92 # define LZ4_FORCE_SW_BITCOUNT
93 #endif
97 /*-************************************
98 * Dependency
99 **************************************/
101 * LZ4_SRC_INCLUDED:
102 * Amalgamation flag, whether lz4.c is included
104 #ifndef LZ4_SRC_INCLUDED
105 # define LZ4_SRC_INCLUDED 1
106 #endif
108 #ifndef LZ4_STATIC_LINKING_ONLY
109 #define LZ4_STATIC_LINKING_ONLY
110 #endif
112 #ifndef LZ4_DISABLE_DEPRECATE_WARNINGS
113 #define LZ4_DISABLE_DEPRECATE_WARNINGS /* due to LZ4_decompress_safe_withPrefix64k */
114 #endif
116 #define LZ4_STATIC_LINKING_ONLY /* LZ4_DISTANCE_MAX */
117 #include "lz4.h"
118 /* see also "memory routines" below */
121 /*-************************************
122 * Compiler Options
123 **************************************/
124 #ifdef _MSC_VER /* Visual Studio */
125 # include <intrin.h>
126 # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
127 # pragma warning(disable : 4293) /* disable: C4293: too large shift (32-bits) */
128 #endif /* _MSC_VER */
130 #ifndef LZ4_FORCE_INLINE
131 # ifdef _MSC_VER /* Visual Studio */
132 # define LZ4_FORCE_INLINE static __forceinline
133 # else
134 # if defined (__cplusplus) || defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */
135 # ifdef __GNUC__
136 # define LZ4_FORCE_INLINE static inline __attribute__((always_inline))
137 # else
138 # define LZ4_FORCE_INLINE static inline
139 # endif
140 # else
141 # define LZ4_FORCE_INLINE static
142 # endif /* __STDC_VERSION__ */
143 # endif /* _MSC_VER */
144 #endif /* LZ4_FORCE_INLINE */
146 /* LZ4_FORCE_O2_GCC_PPC64LE and LZ4_FORCE_O2_INLINE_GCC_PPC64LE
147 * gcc on ppc64le generates an unrolled SIMDized loop for LZ4_wildCopy8,
148 * together with a simple 8-byte copy loop as a fall-back path.
149 * However, this optimization hurts the decompression speed by >30%,
150 * because the execution does not go to the optimized loop
151 * for typical compressible data, and all of the preamble checks
152 * before going to the fall-back path become useless overhead.
153 * This optimization happens only with the -O3 flag, and -O2 generates
154 * a simple 8-byte copy loop.
155 * With gcc on ppc64le, all of the LZ4_decompress_* and LZ4_wildCopy8
156 * functions are annotated with __attribute__((optimize("O2"))),
157 * and also LZ4_wildCopy8 is forcibly inlined, so that the O2 attribute
158 * of LZ4_wildCopy8 does not affect the compression speed.
160 #if defined(__PPC64__) && defined(__LITTLE_ENDIAN__) && defined(__GNUC__) && !defined(__clang__)
161 # define LZ4_FORCE_O2_GCC_PPC64LE __attribute__((optimize("O2")))
162 # define LZ4_FORCE_O2_INLINE_GCC_PPC64LE __attribute__((optimize("O2"))) LZ4_FORCE_INLINE
163 #else
164 # define LZ4_FORCE_O2_GCC_PPC64LE
165 # define LZ4_FORCE_O2_INLINE_GCC_PPC64LE static
166 #endif
168 #if (defined(__GNUC__) && (__GNUC__ >= 3)) || (defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 800)) || defined(__clang__)
169 # define expect(expr,value) (__builtin_expect ((expr),(value)) )
170 #else
171 # define expect(expr,value) (expr)
172 #endif
174 #ifndef likely
175 #define likely(expr) expect((expr) != 0, 1)
176 #endif
177 #ifndef unlikely
178 #define unlikely(expr) expect((expr) != 0, 0)
179 #endif
182 /*-************************************
183 * Memory routines
184 **************************************/
185 #include <stdlib.h> /* malloc, calloc, free */
186 #define ALLOC(s) malloc(s)
187 #define ALLOC_AND_ZERO(s) calloc(1,s)
188 #define FREEMEM(p) free(p)
189 #include <string.h> /* memset, memcpy */
190 #define MEM_INIT(p,v,s) memset((p),(v),(s))
193 /*-************************************
194 * Common Constants
195 **************************************/
196 #define MINMATCH 4
198 #define WILDCOPYLENGTH 8
199 #define LASTLITERALS 5 /* see ../doc/lz4_Block_format.md#parsing-restrictions */
200 #define MFLIMIT 12 /* see ../doc/lz4_Block_format.md#parsing-restrictions */
201 #define MATCH_SAFEGUARD_DISTANCE ((2*WILDCOPYLENGTH) - MINMATCH) /* ensure it's possible to write 2 x wildcopyLength without overflowing output buffer */
202 #define FASTLOOP_SAFE_DISTANCE 64
203 static const int LZ4_minLength = (MFLIMIT + 1);
205 #define KB *(1 <<10)
206 #define MB *(1 <<20)
207 #define GB *(1U<<30)
209 #define LZ4_DISTANCE_ABSOLUTE_MAX 65535
210 #if (LZ4_DISTANCE_MAX > LZ4_DISTANCE_ABSOLUTE_MAX) /* max supported by LZ4 format */
211 # error "LZ4_DISTANCE_MAX is too big : must be <= 65535"
212 #endif
214 #define ML_BITS 4
215 #define ML_MASK ((1U<<ML_BITS)-1)
216 #define RUN_BITS (8-ML_BITS)
217 #define RUN_MASK ((1U<<RUN_BITS)-1)
220 /*-************************************
221 * Error detection
222 **************************************/
223 #if defined(LZ4_DEBUG) && (LZ4_DEBUG>=1)
224 # include <assert.h>
225 #else
226 # ifndef assert
227 # define assert(condition) ((void)0)
228 # endif
229 #endif
231 #define LZ4_STATIC_ASSERT(c) { enum { LZ4_static_assert = 1/(int)(!!(c)) }; } /* use after variable declarations */
233 #if defined(LZ4_DEBUG) && (LZ4_DEBUG>=2)
234 # include <stdio.h>
235 static int g_debuglog_enable = 1;
236 # define DEBUGLOG(l, ...) { \
237 if ((g_debuglog_enable) && (l<=LZ4_DEBUG)) { \
238 fprintf(stderr, __FILE__ ": "); \
239 fprintf(stderr, __VA_ARGS__); \
240 fprintf(stderr, " \n"); \
242 #else
243 # define DEBUGLOG(l, ...) {} /* disabled */
244 #endif
247 /*-************************************
248 * Types
249 **************************************/
250 #include <limits.h>
251 #if defined(__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
252 # include <stdint.h>
253 typedef uint8_t BYTE;
254 typedef uint16_t U16;
255 typedef uint32_t U32;
256 typedef int32_t S32;
257 typedef uint64_t U64;
258 typedef uintptr_t uptrval;
259 #else
260 # if UINT_MAX != 4294967295UL
261 # error "LZ4 code (when not C++ or C99) assumes that sizeof(int) == 4"
262 # endif
263 typedef unsigned char BYTE;
264 typedef unsigned short U16;
265 typedef unsigned int U32;
266 typedef signed int S32;
267 typedef unsigned long long U64;
268 typedef size_t uptrval; /* generally true, except OpenVMS-64 */
269 #endif
271 #if defined(__x86_64__)
272 typedef U64 reg_t; /* 64-bits in x32 mode */
273 #else
274 typedef size_t reg_t; /* 32-bits in x32 mode */
275 #endif
277 typedef enum {
278 notLimited = 0,
279 limitedOutput = 1,
280 fillOutput = 2
281 } limitedOutput_directive;
284 /*-************************************
285 * Reading and writing into memory
286 **************************************/
289 * LZ4 relies on memcpy with a constant size being inlined. In freestanding
290 * environments, the compiler can't assume the implementation of memcpy() is
291 * standard compliant, so it can't apply its specialized memcpy() inlining
292 * logic. When possible, use __builtin_memcpy() to tell the compiler to analyze
293 * memcpy() as if it were standard compliant, so it can inline it in freestanding
294 * environments. This is needed when decompressing the Linux Kernel, for example.
296 #if defined(__GNUC__) && (__GNUC__ >= 4)
297 #define LZ4_memcpy(dst, src, size) __builtin_memcpy(dst, src, size)
298 #else
299 #define LZ4_memcpy(dst, src, size) memcpy(dst, src, size)
300 #endif
302 static unsigned LZ4_isLittleEndian(void) {
303 const union { U32 u; BYTE c[4]; } one = { 1 }; /* don't use static : performance detrimental */
304 return one.c[0];
308 #if defined(LZ4_FORCE_MEMORY_ACCESS) && (LZ4_FORCE_MEMORY_ACCESS==2)
309 /* lie to the compiler about data alignment; use with caution */
311 static U16 LZ4_read16(const void *memPtr) { return *(const U16 *) memPtr; }
312 static U32 LZ4_read32(const void *memPtr) { return *(const U32 *) memPtr; }
313 static reg_t LZ4_read_ARCH(const void *memPtr) { return *(const reg_t *) memPtr; }
315 static void LZ4_write16(void *memPtr, U16 value) { *(U16 *)memPtr = value; }
316 static void LZ4_write32(void *memPtr, U32 value) { *(U32 *)memPtr = value; }
318 #elif defined(LZ4_FORCE_MEMORY_ACCESS) && (LZ4_FORCE_MEMORY_ACCESS==1)
320 /* __pack instructions are safer, but compiler specific, hence potentially problematic for some compilers */
321 /* currently only defined for gcc and icc */
322 typedef union { U16 u16; U32 u32; reg_t uArch; } __attribute__((packed)) unalign;
324 static U16 LZ4_read16(const void *ptr) { return ((const unalign *)ptr)->u16; }
325 // Tolerate reads on buffer boundary
326 ATTRIBUTE_NO_SANITIZE_ADDRESS
327 static U32 LZ4_read32(const void *ptr) { return ((const unalign *)ptr)->u32; }
328 static reg_t LZ4_read_ARCH(const void *ptr) { return ((const unalign *)ptr)->uArch; }
330 static void LZ4_write16(void *memPtr, U16 value) { ((unalign *)memPtr)->u16 = value; }
331 static void LZ4_write32(void *memPtr, U32 value) { ((unalign *)memPtr)->u32 = value; }
333 #else /* safe and portable access using memcpy() */
335 static U16 LZ4_read16(const void *memPtr) {
336 U16 val;
337 LZ4_memcpy(&val, memPtr, sizeof(val));
338 return val;
341 static U32 LZ4_read32(const void *memPtr) {
342 U32 val;
343 LZ4_memcpy(&val, memPtr, sizeof(val));
344 return val;
347 static reg_t LZ4_read_ARCH(const void *memPtr) {
348 reg_t val;
349 LZ4_memcpy(&val, memPtr, sizeof(val));
350 return val;
353 static void LZ4_write16(void *memPtr, U16 value) {
354 LZ4_memcpy(memPtr, &value, sizeof(value));
357 static void LZ4_write32(void *memPtr, U32 value) {
358 LZ4_memcpy(memPtr, &value, sizeof(value));
361 #endif /* LZ4_FORCE_MEMORY_ACCESS */
364 static U16 LZ4_readLE16(const void *memPtr) {
365 if (LZ4_isLittleEndian()) {
366 return LZ4_read16(memPtr);
367 } else {
368 const BYTE *p = (const BYTE *)memPtr;
369 return (U16)((U16)p[0] + (p[1] << 8));
373 static void LZ4_writeLE16(void *memPtr, U16 value) {
374 if (LZ4_isLittleEndian()) {
375 LZ4_write16(memPtr, value);
376 } else {
377 BYTE *p = (BYTE *)memPtr;
378 p[0] = (BYTE) value;
379 p[1] = (BYTE)(value >> 8);
383 /* customized variant of memcpy, which can overwrite up to 8 bytes beyond dstEnd */
384 LZ4_FORCE_O2_INLINE_GCC_PPC64LE
385 void LZ4_wildCopy8(void *dstPtr, const void *srcPtr, void *dstEnd) {
386 BYTE *d = (BYTE *)dstPtr;
387 const BYTE *s = (const BYTE *)srcPtr;
388 BYTE *const e = (BYTE *)dstEnd;
390 do { LZ4_memcpy(d, s, 8); d += 8; s += 8; }
391 while (d < e);
394 static const unsigned inc32table[8] = {0, 1, 2, 1, 0, 4, 4, 4};
395 static const int dec64table[8] = {0, 0, 0, -1, -4, 1, 2, 3};
398 #ifndef LZ4_FAST_DEC_LOOP
399 # if defined __i386__ || defined _M_IX86 || defined __x86_64__ || defined _M_X64
400 # define LZ4_FAST_DEC_LOOP 1
401 # elif defined(__aarch64__) && !defined(__clang__)
402 /* On aarch64, we disable this optimization for clang because on certain
403 * mobile chipsets, performance is reduced with clang. For information
404 * refer to https://github.com/lz4/lz4/pull/707 */
405 # define LZ4_FAST_DEC_LOOP 1
406 # else
407 # define LZ4_FAST_DEC_LOOP 0
408 # endif
409 #endif
411 #if LZ4_FAST_DEC_LOOP
413 LZ4_FORCE_O2_INLINE_GCC_PPC64LE void
414 LZ4_memcpy_using_offset_base(BYTE *dstPtr, const BYTE *srcPtr, BYTE *dstEnd, const size_t offset) {
415 if (offset < 8) {
416 dstPtr[0] = srcPtr[0];
417 dstPtr[1] = srcPtr[1];
418 dstPtr[2] = srcPtr[2];
419 dstPtr[3] = srcPtr[3];
420 srcPtr += inc32table[offset];
421 LZ4_memcpy(dstPtr + 4, srcPtr, 4);
422 srcPtr -= dec64table[offset];
423 dstPtr += 8;
424 } else {
425 LZ4_memcpy(dstPtr, srcPtr, 8);
426 dstPtr += 8;
427 srcPtr += 8;
430 LZ4_wildCopy8(dstPtr, srcPtr, dstEnd);
433 /* customized variant of memcpy, which can overwrite up to 32 bytes beyond dstEnd
434 * this version copies two times 16 bytes (instead of one time 32 bytes)
435 * because it must be compatible with offsets >= 16. */
436 LZ4_FORCE_O2_INLINE_GCC_PPC64LE void
437 LZ4_wildCopy32(void *dstPtr, const void *srcPtr, void *dstEnd) {
438 BYTE *d = (BYTE *)dstPtr;
439 const BYTE *s = (const BYTE *)srcPtr;
440 BYTE *const e = (BYTE *)dstEnd;
442 do { LZ4_memcpy(d, s, 16); LZ4_memcpy(d + 16, s + 16, 16); d += 32; s += 32; }
443 while (d < e);
446 /* LZ4_memcpy_using_offset() presumes :
447 * - dstEnd >= dstPtr + MINMATCH
448 * - there is at least 8 bytes available to write after dstEnd */
449 LZ4_FORCE_O2_INLINE_GCC_PPC64LE void
450 LZ4_memcpy_using_offset(BYTE *dstPtr, const BYTE *srcPtr, BYTE *dstEnd, const size_t offset) {
451 BYTE v[8];
453 assert(dstEnd >= dstPtr + MINMATCH);
454 LZ4_write32(dstPtr, 0); /* silence an msan warning when offset==0 */
456 switch (offset) {
457 case 1:
458 memset(v, *srcPtr, 8);
459 break;
460 case 2:
461 LZ4_memcpy(v, srcPtr, 2);
462 LZ4_memcpy(&v[2], srcPtr, 2);
463 LZ4_memcpy(&v[4], &v[0], 4);
464 break;
465 case 4:
466 LZ4_memcpy(v, srcPtr, 4);
467 LZ4_memcpy(&v[4], srcPtr, 4);
468 break;
469 default:
470 LZ4_memcpy_using_offset_base(dstPtr, srcPtr, dstEnd, offset);
471 return;
474 LZ4_memcpy(dstPtr, v, 8);
475 dstPtr += 8;
476 while (dstPtr < dstEnd) {
477 LZ4_memcpy(dstPtr, v, 8);
478 dstPtr += 8;
481 #endif
484 /*-************************************
485 * Common functions
486 **************************************/
487 static unsigned LZ4_NbCommonBytes(reg_t val) {
488 assert(val != 0);
489 if (LZ4_isLittleEndian()) {
490 if (sizeof(val) == 8) {
491 # if defined(_MSC_VER) && (_MSC_VER >= 1800) && defined(_M_AMD64) && !defined(LZ4_FORCE_SW_BITCOUNT)
492 /* x64 CPUS without BMI support interpret `TZCNT` as `REP BSF` */
493 return (unsigned)_tzcnt_u64(val) >> 3;
494 # elif defined(_MSC_VER) && defined(_WIN64) && !defined(LZ4_FORCE_SW_BITCOUNT)
495 unsigned long r = 0;
496 _BitScanForward64(&r, (U64)val);
497 return (unsigned)r >> 3;
498 # elif (defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 3) || \
499 ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))))) && \
500 !defined(LZ4_FORCE_SW_BITCOUNT)
501 return (unsigned)__builtin_ctzll((U64)val) >> 3;
502 # else
503 const U64 m = 0x0101010101010101ULL;
504 val ^= val - 1;
505 return (unsigned)(((U64)((val & (m - 1)) * m)) >> 56);
506 # endif
507 } else /* 32 bits */ {
508 # if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
509 unsigned long r;
510 _BitScanForward(&r, (U32)val);
511 return (unsigned)r >> 3;
512 # elif (defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 3) || \
513 ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))))) && \
514 !defined(__TINYC__) && !defined(LZ4_FORCE_SW_BITCOUNT)
515 return (unsigned)__builtin_ctz((U32)val) >> 3;
516 # else
517 const U32 m = 0x01010101;
518 return (unsigned)((((val - 1) ^ val) & (m - 1)) * m) >> 24;
519 # endif
521 } else { /* Big Endian CPU */
522 if (sizeof(val) == 8) {
523 # if (defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 3) || \
524 ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))))) && \
525 !defined(__TINYC__) && !defined(LZ4_FORCE_SW_BITCOUNT)
526 return (unsigned)__builtin_clzll((U64)val) >> 3;
527 # else
528 #if 1
529 /* this method is probably faster,
530 * but adds a 128 bytes lookup table */
531 static const unsigned char ctz7_tab[128] = {
532 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
533 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
534 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
535 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
536 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
537 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
538 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
539 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
541 U64 const mask = 0x0101010101010101ULL;
542 U64 const t = (((val >> 8) - mask) | val) & mask;
543 return ctz7_tab[(t * 0x0080402010080402ULL) >> 57];
544 #else
545 /* this method doesn't consume memory space like the previous one,
546 * but it contains several branches,
547 * that may end up slowing execution */
548 static const U32 by32 = sizeof(val) * 4; /* 32 on 64 bits (goal), 16 on 32 bits.
549 Just to avoid some static analyzer complaining about shift by 32 on 32-bits target.
550 Note that this code path is never triggered in 32-bits mode. */
551 unsigned r;
552 if (!(val >> by32)) { r = 4; }
553 else { r = 0; val >>= by32; }
554 if (!(val >> 16)) { r += 2; val >>= 8; }
555 else { val >>= 24; }
556 r += (!val);
557 return r;
558 #endif
559 # endif
560 } else /* 32 bits */ {
561 # if (defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 3) || \
562 ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))))) && \
563 !defined(LZ4_FORCE_SW_BITCOUNT)
564 return (unsigned)__builtin_clz((U32)val) >> 3;
565 # else
566 val >>= 8;
567 val = ((((val + 0x00FFFF00) | 0x00FFFFFF) + val) |
568 (val + 0x00FF0000)) >> 24;
569 return (unsigned)val ^ 3;
570 # endif
576 #define STEPSIZE sizeof(reg_t)
577 LZ4_FORCE_INLINE
578 unsigned LZ4_count(const BYTE *pIn, const BYTE *pMatch, const BYTE *pInLimit) {
579 const BYTE *const pStart = pIn;
581 if (likely(pIn < pInLimit - (STEPSIZE - 1))) {
582 reg_t const diff = LZ4_read_ARCH(pMatch) ^ LZ4_read_ARCH(pIn);
583 if (!diff) {
584 pIn += STEPSIZE;
585 pMatch += STEPSIZE;
586 } else {
587 return LZ4_NbCommonBytes(diff);
591 while (likely(pIn < pInLimit - (STEPSIZE - 1))) {
592 reg_t const diff = LZ4_read_ARCH(pMatch) ^ LZ4_read_ARCH(pIn);
593 if (!diff) { pIn += STEPSIZE; pMatch += STEPSIZE; continue; }
594 pIn += LZ4_NbCommonBytes(diff);
595 return (unsigned)(pIn - pStart);
598 if ((STEPSIZE == 8) && (pIn < (pInLimit - 3)) && (LZ4_read32(pMatch) == LZ4_read32(pIn))) { pIn += 4; pMatch += 4; }
599 if ((pIn < (pInLimit - 1)) && (LZ4_read16(pMatch) == LZ4_read16(pIn))) { pIn += 2; pMatch += 2; }
600 if ((pIn < pInLimit) && (*pMatch == *pIn)) pIn++;
601 return (unsigned)(pIn - pStart);
605 #ifndef LZ4_COMMONDEFS_ONLY
606 /*-************************************
607 * Local Constants
608 **************************************/
609 static const int LZ4_64Klimit = ((64 KB) + (MFLIMIT - 1));
610 static const U32 LZ4_skipTrigger = 6; /* Increase this value ==> compression run slower on incompressible data */
613 /*-************************************
614 * Local Structures and types
615 **************************************/
616 typedef enum { clearedTable = 0, byPtr, byU32, byU16 } tableType_t;
619 * This enum distinguishes several different modes of accessing previous
620 * content in the stream.
622 * - noDict : There is no preceding content.
623 * - withPrefix64k : Table entries up to ctx->dictSize before the current blob
624 * blob being compressed are valid and refer to the preceding
625 * content (of length ctx->dictSize), which is available
626 * contiguously preceding in memory the content currently
627 * being compressed.
628 * - usingExtDict : Like withPrefix64k, but the preceding content is somewhere
629 * else in memory, starting at ctx->dictionary with length
630 * ctx->dictSize.
631 * - usingDictCtx : Like usingExtDict, but everything concerning the preceding
632 * content is in a separate context, pointed to by
633 * ctx->dictCtx. ctx->dictionary, ctx->dictSize, and table
634 * entries in the current context that refer to positions
635 * preceding the beginning of the current compression are
636 * ignored. Instead, ctx->dictCtx->dictionary and ctx->dictCtx
637 * ->dictSize describe the location and size of the preceding
638 * content, and matches are found by looking in the ctx
639 * ->dictCtx->hashTable.
641 typedef enum { noDict = 0, withPrefix64k, usingExtDict, usingDictCtx } dict_directive;
642 typedef enum { noDictIssue = 0, dictSmall } dictIssue_directive;
645 /*-************************************
646 * Local Utils
647 **************************************/
648 int LZ4_versionNumber(void) { return LZ4_VERSION_NUMBER; }
649 const char *LZ4_versionString(void) { return LZ4_VERSION_STRING; }
650 int LZ4_compressBound(int isize) { return LZ4_COMPRESSBOUND(isize); }
651 int LZ4_sizeofState(void) { return LZ4_STREAMSIZE; }
654 /*-************************************
655 * Internal Definitions used in Tests
656 **************************************/
657 #if defined (__cplusplus)
658 extern "C" {
659 #endif
661 int LZ4_compress_forceExtDict(LZ4_stream_t *LZ4_dict, const char *src, char *dst, int srcSize);
663 int LZ4_decompress_safe_forceExtDict(const char *src, char *dst,
664 int compressedSize, int dstCapacity,
665 const void *dictStart, size_t dictSize);
667 #if defined (__cplusplus)
669 #endif
671 /*-******************************
672 * Compression functions
673 ********************************/
674 LZ4_FORCE_INLINE U32 LZ4_hash4(U32 sequence, tableType_t const tableType) {
675 if (tableType == byU16)
676 return ((sequence * 2654435761U) >> ((MINMATCH * 8) - (LZ4_HASHLOG + 1)));
677 else
678 return ((sequence * 2654435761U) >> ((MINMATCH * 8) - LZ4_HASHLOG));
681 LZ4_FORCE_INLINE U32 LZ4_hash5(U64 sequence, tableType_t const tableType) {
682 const U32 hashLog = (tableType == byU16) ? LZ4_HASHLOG + 1 : LZ4_HASHLOG;
683 if (LZ4_isLittleEndian()) {
684 const U64 prime5bytes = 889523592379ULL;
685 return (U32)(((sequence << 24) * prime5bytes) >> (64 - hashLog));
686 } else {
687 const U64 prime8bytes = 11400714785074694791ULL;
688 return (U32)(((sequence >> 24) * prime8bytes) >> (64 - hashLog));
692 LZ4_FORCE_INLINE U32 LZ4_hashPosition(const void *const p, tableType_t const tableType) {
693 if ((sizeof(reg_t) == 8) && (tableType != byU16)) return LZ4_hash5(LZ4_read_ARCH(p), tableType);
694 return LZ4_hash4(LZ4_read32(p), tableType);
697 LZ4_FORCE_INLINE void LZ4_clearHash(U32 h, void *tableBase, tableType_t const tableType) {
698 switch (tableType) {
699 default: /* fallthrough */
700 case clearedTable: { /* illegal! */ assert(0); return; }
701 case byPtr:
702 { const BYTE **hashTable = (const BYTE **)tableBase; hashTable[h] = NULL; return; }
703 case byU32:
704 { U32 *hashTable = (U32 *) tableBase; hashTable[h] = 0; return; }
705 case byU16:
706 { U16 *hashTable = (U16 *) tableBase; hashTable[h] = 0; return; }
710 LZ4_FORCE_INLINE void LZ4_putIndexOnHash(U32 idx, U32 h, void *tableBase, tableType_t const tableType) {
711 switch (tableType) {
712 default: /* fallthrough */
713 case clearedTable: /* fallthrough */
714 case byPtr: { /* illegal! */ assert(0); return; }
715 case byU32:
716 { U32 *hashTable = (U32 *) tableBase; hashTable[h] = idx; return; }
717 case byU16:
718 { U16 *hashTable = (U16 *) tableBase; assert(idx < 65536); hashTable[h] = (U16)idx; return; }
722 LZ4_FORCE_INLINE void LZ4_putPositionOnHash(const BYTE *p, U32 h,
723 void *tableBase, tableType_t const tableType,
724 const BYTE *srcBase) {
725 switch (tableType) {
726 case clearedTable: { /* illegal! */ assert(0); return; }
727 case byPtr:
728 { const BYTE **hashTable = (const BYTE **)tableBase; hashTable[h] = p; return; }
729 case byU32:
730 { U32 *hashTable = (U32 *) tableBase; hashTable[h] = (U32)(p - srcBase); return; }
731 case byU16:
732 { U16 *hashTable = (U16 *) tableBase; hashTable[h] = (U16)(p - srcBase); return; }
736 LZ4_FORCE_INLINE void LZ4_putPosition(const BYTE *p, void *tableBase, tableType_t tableType, const BYTE *srcBase) {
737 U32 const h = LZ4_hashPosition(p, tableType);
738 LZ4_putPositionOnHash(p, h, tableBase, tableType, srcBase);
741 /* LZ4_getIndexOnHash() :
742 * Index of match position registered in hash table.
743 * hash position must be calculated by using base+index, or dictBase+index.
744 * Assumption 1 : only valid if tableType == byU32 or byU16.
745 * Assumption 2 : h is presumed valid (within limits of hash table)
747 LZ4_FORCE_INLINE U32 LZ4_getIndexOnHash(U32 h, const void *tableBase, tableType_t tableType) {
748 LZ4_STATIC_ASSERT(LZ4_MEMORY_USAGE > 2);
749 if (tableType == byU32) {
750 const U32 *const hashTable = (const U32 *) tableBase;
751 assert(h < (1U << (LZ4_MEMORY_USAGE - 2)));
752 return hashTable[h];
754 if (tableType == byU16) {
755 const U16 *const hashTable = (const U16 *) tableBase;
756 assert(h < (1U << (LZ4_MEMORY_USAGE - 1)));
757 return hashTable[h];
759 assert(0);
760 return 0; /* forbidden case */
763 static const BYTE *LZ4_getPositionOnHash(U32 h, const void *tableBase, tableType_t tableType, const BYTE *srcBase) {
764 if (tableType == byPtr) { const BYTE *const *hashTable = (const BYTE * const *) tableBase; return hashTable[h]; }
765 if (tableType == byU32) { const U32 *const hashTable = (const U32 *) tableBase; return hashTable[h] + srcBase; }
766 { const U16 *const hashTable = (const U16 *) tableBase; return hashTable[h] + srcBase; } /* default, to ensure a return */
769 LZ4_FORCE_INLINE const BYTE *
770 LZ4_getPosition(const BYTE *p,
771 const void *tableBase, tableType_t tableType,
772 const BYTE *srcBase) {
773 U32 const h = LZ4_hashPosition(p, tableType);
774 return LZ4_getPositionOnHash(h, tableBase, tableType, srcBase);
777 LZ4_FORCE_INLINE void
778 LZ4_prepareTable(LZ4_stream_t_internal *const cctx,
779 const int srcSize,
780 const tableType_t tableType) {
781 /* If the table hasn't been used, it's guaranteed to be zeroed out, and is
782 * therefore safe to use no matter what mode we're in. Otherwise, we figure
783 * out if it's safe to leave as is or whether it needs to be reset.
785 if ((tableType_t)cctx->tableType != clearedTable) {
786 assert(srcSize >= 0);
787 if ((tableType_t)cctx->tableType != tableType
788 || ((tableType == byU16) && cctx->currentOffset + (unsigned)srcSize >= 0xFFFFU)
789 || ((tableType == byU32) && cctx->currentOffset > 1 GB)
790 || tableType == byPtr
791 || srcSize >= 4 KB) {
792 DEBUGLOG(4, "LZ4_prepareTable: Resetting table in %p", cctx);
793 MEM_INIT(cctx->hashTable, 0, LZ4_HASHTABLESIZE);
794 cctx->currentOffset = 0;
795 cctx->tableType = (U32)clearedTable;
796 } else {
797 DEBUGLOG(4, "LZ4_prepareTable: Re-use hash table (no reset)");
801 /* Adding a gap, so all previous entries are > LZ4_DISTANCE_MAX back, is faster
802 * than compressing without a gap. However, compressing with
803 * currentOffset == 0 is faster still, so we preserve that case.
805 if (cctx->currentOffset != 0 && tableType == byU32) {
806 DEBUGLOG(5, "LZ4_prepareTable: adding 64KB to currentOffset");
807 cctx->currentOffset += 64 KB;
810 /* Finally, clear history */
811 cctx->dictCtx = NULL;
812 cctx->dictionary = NULL;
813 cctx->dictSize = 0;
816 /** LZ4_compress_generic() :
817 * inlined, to ensure branches are decided at compilation time.
818 * Presumed already validated at this stage:
819 * - source != NULL
820 * - inputSize > 0
822 LZ4_FORCE_INLINE int LZ4_compress_generic_validated(
823 LZ4_stream_t_internal *const cctx,
824 const char *const src,
825 char *const dst,
826 const int srcSize,
827 int *inputConsumed, /* only written when outputDirective == fillOutput */
828 const int dstCapacity,
829 const limitedOutput_directive outputDirective,
830 const tableType_t tableType,
831 const dict_directive dictDirective,
832 const dictIssue_directive dictIssue,
833 const int acceleration) {
834 int result;
835 const BYTE *ip = (const BYTE *) src;
837 U32 const startIndex = cctx->currentOffset;
838 const BYTE *base = (const BYTE *) src - startIndex;
839 const BYTE *lowLimit;
841 const LZ4_stream_t_internal *dictCtx = (const LZ4_stream_t_internal *) cctx->dictCtx;
842 const BYTE *const dictionary =
843 dictDirective == usingDictCtx ? dictCtx->dictionary : cctx->dictionary;
844 const U32 dictSize =
845 dictDirective == usingDictCtx ? dictCtx->dictSize : cctx->dictSize;
846 const U32 dictDelta = (dictDirective == usingDictCtx) ? startIndex - dictCtx->currentOffset : 0; /* make indexes in dictCtx comparable with index in current context */
848 int const maybe_extMem = (dictDirective == usingExtDict) || (dictDirective == usingDictCtx);
849 U32 const prefixIdxLimit = startIndex - dictSize; /* used when dictDirective == dictSmall */
850 const BYTE *const dictEnd = dictionary ? dictionary + dictSize : dictionary;
851 const BYTE *anchor = (const BYTE *) src;
852 const BYTE *const iend = ip + srcSize;
853 const BYTE *const mflimitPlusOne = iend - MFLIMIT + 1;
854 const BYTE *const matchlimit = iend - LASTLITERALS;
856 /* the dictCtx currentOffset is indexed on the start of the dictionary,
857 * while a dictionary in the current context precedes the currentOffset */
858 const BYTE *dictBase = !dictionary ? NULL : (dictDirective == usingDictCtx) ?
859 dictionary + dictSize - dictCtx->currentOffset :
860 dictionary + dictSize - startIndex;
862 BYTE *op = (BYTE *) dst;
863 BYTE *const olimit = op + dstCapacity;
865 U32 offset = 0;
866 U32 forwardH;
868 DEBUGLOG(5, "LZ4_compress_generic_validated: srcSize=%i, tableType=%u", srcSize, tableType);
869 assert(ip != NULL);
870 /* If init conditions are not met, we don't have to mark stream
871 * as having dirty context, since no action was taken yet */
872 if (outputDirective == fillOutput && dstCapacity < 1) { return 0; } /* Impossible to store anything */
873 if ((tableType == byU16) && (srcSize >= LZ4_64Klimit)) { return 0; } /* Size too large (not within 64K limit) */
874 if (tableType == byPtr) assert(dictDirective == noDict); /* only supported use case with byPtr */
875 assert(acceleration >= 1);
877 lowLimit = (const BYTE *)src - (dictDirective == withPrefix64k ? dictSize : 0);
879 /* Update context state */
880 if (dictDirective == usingDictCtx) {
881 /* Subsequent linked blocks can't use the dictionary. */
882 /* Instead, they use the block we just compressed. */
883 cctx->dictCtx = NULL;
884 cctx->dictSize = (U32)srcSize;
885 } else {
886 cctx->dictSize += (U32)srcSize;
888 cctx->currentOffset += (U32)srcSize;
889 cctx->tableType = (U32)tableType;
891 if (srcSize < LZ4_minLength) goto _last_literals; /* Input too small, no compression (all literals) */
893 /* First Byte */
894 LZ4_putPosition(ip, cctx->hashTable, tableType, base);
895 ip++;
896 forwardH = LZ4_hashPosition(ip, tableType);
898 /* Main Loop */
899 for (; ;) {
900 const BYTE *match;
901 BYTE *token;
902 const BYTE *filledIp;
904 /* Find a match */
905 if (tableType == byPtr) {
906 const BYTE *forwardIp = ip;
907 int step = 1;
908 int searchMatchNb = acceleration << LZ4_skipTrigger;
909 do {
910 U32 const h = forwardH;
911 ip = forwardIp;
912 forwardIp += step;
913 step = (searchMatchNb++ >> LZ4_skipTrigger);
915 if (unlikely(forwardIp > mflimitPlusOne)) goto _last_literals;
916 assert(ip < mflimitPlusOne);
918 match = LZ4_getPositionOnHash(h, cctx->hashTable, tableType, base);
919 forwardH = LZ4_hashPosition(forwardIp, tableType);
920 LZ4_putPositionOnHash(ip, h, cctx->hashTable, tableType, base);
922 } while ((match + LZ4_DISTANCE_MAX < ip)
923 || (LZ4_read32(match) != LZ4_read32(ip)));
925 } else { /* byU32, byU16 */
927 const BYTE *forwardIp = ip;
928 int step = 1;
929 int searchMatchNb = acceleration << LZ4_skipTrigger;
930 do {
931 U32 const h = forwardH;
932 U32 const current = (U32)(forwardIp - base);
933 U32 matchIndex = LZ4_getIndexOnHash(h, cctx->hashTable, tableType);
934 assert(matchIndex <= current);
935 assert(forwardIp - base < (ptrdiff_t)(2 GB - 1));
936 ip = forwardIp;
937 forwardIp += step;
938 step = (searchMatchNb++ >> LZ4_skipTrigger);
940 if (unlikely(forwardIp > mflimitPlusOne)) goto _last_literals;
941 assert(ip < mflimitPlusOne);
943 if (dictDirective == usingDictCtx) {
944 if (matchIndex < startIndex) {
945 /* there was no match, try the dictionary */
946 assert(tableType == byU32);
947 matchIndex = LZ4_getIndexOnHash(h, dictCtx->hashTable, byU32);
948 match = dictBase + matchIndex;
949 matchIndex += dictDelta; /* make dictCtx index comparable with current context */
950 lowLimit = dictionary;
951 } else {
952 match = base + matchIndex;
953 lowLimit = (const BYTE *)src;
955 } else if (dictDirective == usingExtDict) {
956 if (matchIndex < startIndex) {
957 DEBUGLOG(7, "extDict candidate: matchIndex=%5u < startIndex=%5u", matchIndex, startIndex);
958 assert(startIndex - matchIndex >= MINMATCH);
959 match = dictBase + matchIndex;
960 lowLimit = dictionary;
961 } else {
962 match = base + matchIndex;
963 lowLimit = (const BYTE *)src;
965 } else { /* single continuous memory segment */
966 match = base + matchIndex;
968 forwardH = LZ4_hashPosition(forwardIp, tableType);
969 LZ4_putIndexOnHash(current, h, cctx->hashTable, tableType);
971 DEBUGLOG(7, "candidate at pos=%u (offset=%u \n", matchIndex, current - matchIndex);
972 if ((dictIssue == dictSmall) && (matchIndex < prefixIdxLimit)) { continue; } /* match outside of valid area */
973 assert(matchIndex < current);
974 if (((tableType != byU16) || (LZ4_DISTANCE_MAX < LZ4_DISTANCE_ABSOLUTE_MAX))
975 && (matchIndex + LZ4_DISTANCE_MAX < current)) {
976 continue;
977 } /* too far */
978 assert((current - matchIndex) <= LZ4_DISTANCE_MAX); /* match now expected within distance */
980 if (LZ4_read32(match) == LZ4_read32(ip)) {
981 if (maybe_extMem) offset = current - matchIndex;
982 break; /* match found */
985 } while (1);
988 /* Catch up */
989 filledIp = ip;
990 while (((ip > anchor) & (match > lowLimit)) && (unlikely(ip[-1] == match[-1]))) { ip--; match--; }
992 /* Encode Literals */
994 unsigned const litLength = (unsigned)(ip - anchor);
995 token = op++;
996 if ((outputDirective == limitedOutput) && /* Check output buffer overflow */
997 (unlikely(op + litLength + (2 + 1 + LASTLITERALS) + (litLength / 255) > olimit))) {
998 return 0; /* cannot compress within `dst` budget. Stored indexes in hash table are nonetheless fine */
1000 if ((outputDirective == fillOutput) &&
1001 (unlikely(op + (litLength + 240) / 255 /* litlen */ + litLength /* literals */ + 2 /* offset */ + 1 /* token */ + MFLIMIT - MINMATCH /* min last literals so last match is <= end - MFLIMIT */ > olimit))) {
1002 op--;
1003 goto _last_literals;
1005 if (litLength >= RUN_MASK) {
1006 int len = (int)(litLength - RUN_MASK);
1007 *token = (RUN_MASK << ML_BITS);
1008 for (; len >= 255 ; len -= 255) * op++ = 255;
1009 *op++ = (BYTE)len;
1010 } else *token = (BYTE)(litLength << ML_BITS);
1012 /* Copy Literals */
1013 LZ4_wildCopy8(op, anchor, op + litLength);
1014 op += litLength;
1015 DEBUGLOG(6, "seq.start:%i, literals=%u, match.start:%i",
1016 (int)(anchor - (const BYTE *)src), litLength, (int)(ip - (const BYTE *)src));
1019 _next_match:
1020 /* at this stage, the following variables must be correctly set :
1021 * - ip : at start of LZ operation
1022 * - match : at start of previous pattern occurence; can be within current prefix, or within extDict
1023 * - offset : if maybe_ext_memSegment==1 (constant)
1024 * - lowLimit : must be == dictionary to mean "match is within extDict"; must be == source otherwise
1025 * - token and *token : position to write 4-bits for match length; higher 4-bits for literal length supposed already written
1028 if ((outputDirective == fillOutput) &&
1029 (op + 2 /* offset */ + 1 /* token */ + MFLIMIT - MINMATCH /* min last literals so last match is <= end - MFLIMIT */ > olimit)) {
1030 /* the match was too close to the end, rewind and go to last literals */
1031 op = token;
1032 goto _last_literals;
1035 /* Encode Offset */
1036 if (maybe_extMem) { /* static test */
1037 DEBUGLOG(6, " with offset=%u (ext if > %i)", offset, (int)(ip - (const BYTE *)src));
1038 assert(offset <= LZ4_DISTANCE_MAX && offset > 0);
1039 LZ4_writeLE16(op, (U16)offset);
1040 op += 2;
1041 } else {
1042 DEBUGLOG(6, " with offset=%u (same segment)", (U32)(ip - match));
1043 assert(ip - match <= LZ4_DISTANCE_MAX);
1044 LZ4_writeLE16(op, (U16)(ip - match));
1045 op += 2;
1048 /* Encode MatchLength */
1050 unsigned matchCode;
1052 if ((dictDirective == usingExtDict || dictDirective == usingDictCtx)
1053 && (lowLimit == dictionary) /* match within extDict */) {
1054 const BYTE *limit = ip + (dictEnd - match);
1055 assert(dictEnd > match);
1056 if (limit > matchlimit) limit = matchlimit;
1057 matchCode = LZ4_count(ip + MINMATCH, match + MINMATCH, limit);
1058 ip += (size_t)matchCode + MINMATCH;
1059 if (ip == limit) {
1060 unsigned const more = LZ4_count(limit, (const BYTE *)src, matchlimit);
1061 matchCode += more;
1062 ip += more;
1064 DEBUGLOG(6, " with matchLength=%u starting in extDict", matchCode + MINMATCH);
1065 } else {
1066 matchCode = LZ4_count(ip + MINMATCH, match + MINMATCH, matchlimit);
1067 ip += (size_t)matchCode + MINMATCH;
1068 DEBUGLOG(6, " with matchLength=%u", matchCode + MINMATCH);
1071 if ((outputDirective) && /* Check output buffer overflow */
1072 (unlikely(op + (1 + LASTLITERALS) + (matchCode + 240) / 255 > olimit))) {
1073 if (outputDirective == fillOutput) {
1074 /* Match description too long : reduce it */
1075 U32 newMatchCode = 15 /* in token */ - 1 /* to avoid needing a zero byte */ + ((U32)(olimit - op) - 1 - LASTLITERALS) * 255;
1076 ip -= matchCode - newMatchCode;
1077 assert(newMatchCode < matchCode);
1078 matchCode = newMatchCode;
1079 if (unlikely(ip <= filledIp)) {
1080 /* We have already filled up to filledIp so if ip ends up less than filledIp
1081 * we have positions in the hash table beyond the current position. This is
1082 * a problem if we reuse the hash table. So we have to remove these positions
1083 * from the hash table.
1085 const BYTE *ptr;
1086 DEBUGLOG(5, "Clearing %u positions", (U32)(filledIp - ip));
1087 for (ptr = ip; ptr <= filledIp; ++ptr) {
1088 U32 const h = LZ4_hashPosition(ptr, tableType);
1089 LZ4_clearHash(h, cctx->hashTable, tableType);
1092 } else {
1093 assert(outputDirective == limitedOutput);
1094 return 0; /* cannot compress within `dst` budget. Stored indexes in hash table are nonetheless fine */
1097 if (matchCode >= ML_MASK) {
1098 *token += ML_MASK;
1099 matchCode -= ML_MASK;
1100 LZ4_write32(op, 0xFFFFFFFF);
1101 while (matchCode >= 4 * 255) {
1102 op += 4;
1103 LZ4_write32(op, 0xFFFFFFFF);
1104 matchCode -= 4 * 255;
1106 op += matchCode / 255;
1107 *op++ = (BYTE)(matchCode % 255);
1108 } else
1109 *token += (BYTE)(matchCode);
1111 /* Ensure we have enough space for the last literals. */
1112 assert(!(outputDirective == fillOutput && op + 1 + LASTLITERALS > olimit));
1114 anchor = ip;
1116 /* Test end of chunk */
1117 if (ip >= mflimitPlusOne) break;
1119 /* Fill table */
1120 LZ4_putPosition(ip - 2, cctx->hashTable, tableType, base);
1122 /* Test next position */
1123 if (tableType == byPtr) {
1125 match = LZ4_getPosition(ip, cctx->hashTable, tableType, base);
1126 LZ4_putPosition(ip, cctx->hashTable, tableType, base);
1127 if ((match + LZ4_DISTANCE_MAX >= ip)
1128 && (LZ4_read32(match) == LZ4_read32(ip)))
1129 { token = op++; *token = 0; goto _next_match; }
1131 } else { /* byU32, byU16 */
1133 U32 const h = LZ4_hashPosition(ip, tableType);
1134 U32 const current = (U32)(ip - base);
1135 U32 matchIndex = LZ4_getIndexOnHash(h, cctx->hashTable, tableType);
1136 assert(matchIndex < current);
1137 if (dictDirective == usingDictCtx) {
1138 if (matchIndex < startIndex) {
1139 /* there was no match, try the dictionary */
1140 matchIndex = LZ4_getIndexOnHash(h, dictCtx->hashTable, byU32);
1141 match = dictBase + matchIndex;
1142 lowLimit = dictionary; /* required for match length counter */
1143 matchIndex += dictDelta;
1144 } else {
1145 match = base + matchIndex;
1146 lowLimit = (const BYTE *)src; /* required for match length counter */
1148 } else if (dictDirective == usingExtDict) {
1149 if (matchIndex < startIndex) {
1150 match = dictBase + matchIndex;
1151 lowLimit = dictionary; /* required for match length counter */
1152 } else {
1153 match = base + matchIndex;
1154 lowLimit = (const BYTE *)src; /* required for match length counter */
1156 } else { /* single memory segment */
1157 match = base + matchIndex;
1159 LZ4_putIndexOnHash(current, h, cctx->hashTable, tableType);
1160 assert(matchIndex < current);
1161 if (((dictIssue == dictSmall) ? (matchIndex >= prefixIdxLimit) : 1)
1162 && (((tableType == byU16) && (LZ4_DISTANCE_MAX == LZ4_DISTANCE_ABSOLUTE_MAX)) ? 1 : (matchIndex + LZ4_DISTANCE_MAX >= current))
1163 && (LZ4_read32(match) == LZ4_read32(ip))) {
1164 token = op++;
1165 *token = 0;
1166 if (maybe_extMem) offset = current - matchIndex;
1167 DEBUGLOG(6, "seq.start:%i, literals=%u, match.start:%i",
1168 (int)(anchor - (const BYTE *)src), 0, (int)(ip - (const BYTE *)src));
1169 goto _next_match;
1173 /* Prepare next loop */
1174 forwardH = LZ4_hashPosition(++ip, tableType);
1178 _last_literals:
1179 /* Encode Last Literals */
1181 size_t lastRun = (size_t)(iend - anchor);
1182 if ((outputDirective) && /* Check output buffer overflow */
1183 (op + lastRun + 1 + ((lastRun + 255 - RUN_MASK) / 255) > olimit)) {
1184 if (outputDirective == fillOutput) {
1185 /* adapt lastRun to fill 'dst' */
1186 assert(olimit >= op);
1187 lastRun = (size_t)(olimit - op) - 1/*token*/;
1188 lastRun -= (lastRun + 256 - RUN_MASK) / 256; /*additional length tokens*/
1189 } else {
1190 assert(outputDirective == limitedOutput);
1191 return 0; /* cannot compress within `dst` budget. Stored indexes in hash table are nonetheless fine */
1194 DEBUGLOG(6, "Final literal run : %i literals", (int)lastRun);
1195 if (lastRun >= RUN_MASK) {
1196 size_t accumulator = lastRun - RUN_MASK;
1197 *op++ = RUN_MASK << ML_BITS;
1198 for (; accumulator >= 255 ; accumulator -= 255) * op++ = 255;
1199 *op++ = (BYTE) accumulator;
1200 } else {
1201 *op++ = (BYTE)(lastRun << ML_BITS);
1203 LZ4_memcpy(op, anchor, lastRun);
1204 ip = anchor + lastRun;
1205 op += lastRun;
1208 if (outputDirective == fillOutput) {
1209 *inputConsumed = (int)(((const char *)ip) - src);
1211 result = (int)(((char *)op) - dst);
1212 assert(result > 0);
1213 DEBUGLOG(5, "LZ4_compress_generic: compressed %i bytes into %i bytes", srcSize, result);
1214 return result;
1217 /** LZ4_compress_generic() :
1218 * inlined, to ensure branches are decided at compilation time;
1219 * takes care of src == (NULL, 0)
1220 * and forward the rest to LZ4_compress_generic_validated */
1221 LZ4_FORCE_INLINE int LZ4_compress_generic(
1222 LZ4_stream_t_internal *const cctx,
1223 const char *const src,
1224 char *const dst,
1225 const int srcSize,
1226 int *inputConsumed, /* only written when outputDirective == fillOutput */
1227 const int dstCapacity,
1228 const limitedOutput_directive outputDirective,
1229 const tableType_t tableType,
1230 const dict_directive dictDirective,
1231 const dictIssue_directive dictIssue,
1232 const int acceleration) {
1233 DEBUGLOG(5, "LZ4_compress_generic: srcSize=%i, dstCapacity=%i",
1234 srcSize, dstCapacity);
1236 if ((U32)srcSize > (U32)LZ4_MAX_INPUT_SIZE) { return 0; } /* Unsupported srcSize, too large (or negative) */
1237 if (srcSize == 0) { /* src == NULL supported if srcSize == 0 */
1238 if (outputDirective != notLimited && dstCapacity <= 0) return 0; /* no output, can't write anything */
1239 DEBUGLOG(5, "Generating an empty block");
1240 assert(outputDirective == notLimited || dstCapacity >= 1);
1241 assert(dst != NULL);
1242 dst[0] = 0;
1243 if (outputDirective == fillOutput) {
1244 assert(inputConsumed != NULL);
1245 *inputConsumed = 0;
1247 return 1;
1249 assert(src != NULL);
1251 return LZ4_compress_generic_validated(cctx, src, dst, srcSize,
1252 inputConsumed, /* only written into if outputDirective == fillOutput */
1253 dstCapacity, outputDirective,
1254 tableType, dictDirective, dictIssue, acceleration);
1258 int LZ4_compress_fast_extState(void *state, const char *src, char *dst, int srcSize, int dstCapacity, int acceleration) {
1259 LZ4_stream_t_internal *const ctx = & LZ4_initStream(state, sizeof(LZ4_stream_t)) -> internal_donotuse;
1260 assert(ctx != NULL);
1261 if (acceleration < 1) acceleration = LZ4_ACCELERATION_DEFAULT;
1262 if (acceleration > LZ4_ACCELERATION_MAX) acceleration = LZ4_ACCELERATION_MAX;
1263 if (dstCapacity >= LZ4_compressBound(srcSize)) {
1264 if (srcSize < LZ4_64Klimit) {
1265 return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, 0, notLimited, byU16, noDict, noDictIssue, acceleration);
1266 } else {
1267 const tableType_t tableType = ((sizeof(void *) == 4) && ((uptrval)src > LZ4_DISTANCE_MAX)) ? byPtr : byU32;
1268 return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, 0, notLimited, tableType, noDict, noDictIssue, acceleration);
1270 } else {
1271 if (srcSize < LZ4_64Klimit) {
1272 return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, dstCapacity, limitedOutput, byU16, noDict, noDictIssue, acceleration);
1273 } else {
1274 const tableType_t tableType = ((sizeof(void *) == 4) && ((uptrval)src > LZ4_DISTANCE_MAX)) ? byPtr : byU32;
1275 return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, dstCapacity, limitedOutput, tableType, noDict, noDictIssue, acceleration);
1281 * LZ4_compress_fast_extState_fastReset() :
1282 * A variant of LZ4_compress_fast_extState().
1284 * Using this variant avoids an expensive initialization step. It is only safe
1285 * to call if the state buffer is known to be correctly initialized already
1286 * (see comment in lz4.h on LZ4_resetStream_fast() for a definition of
1287 * "correctly initialized").
1289 int LZ4_compress_fast_extState_fastReset(void *state, const char *src, char *dst, int srcSize, int dstCapacity, int acceleration) {
1290 LZ4_stream_t_internal *ctx = &((LZ4_stream_t *)state)->internal_donotuse;
1291 if (acceleration < 1) acceleration = LZ4_ACCELERATION_DEFAULT;
1292 if (acceleration > LZ4_ACCELERATION_MAX) acceleration = LZ4_ACCELERATION_MAX;
1294 if (dstCapacity >= LZ4_compressBound(srcSize)) {
1295 if (srcSize < LZ4_64Klimit) {
1296 const tableType_t tableType = byU16;
1297 LZ4_prepareTable(ctx, srcSize, tableType);
1298 if (ctx->currentOffset) {
1299 return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, 0, notLimited, tableType, noDict, dictSmall, acceleration);
1300 } else {
1301 return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, 0, notLimited, tableType, noDict, noDictIssue, acceleration);
1303 } else {
1304 const tableType_t tableType = ((sizeof(void *) == 4) && ((uptrval)src > LZ4_DISTANCE_MAX)) ? byPtr : byU32;
1305 LZ4_prepareTable(ctx, srcSize, tableType);
1306 return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, 0, notLimited, tableType, noDict, noDictIssue, acceleration);
1308 } else {
1309 if (srcSize < LZ4_64Klimit) {
1310 const tableType_t tableType = byU16;
1311 LZ4_prepareTable(ctx, srcSize, tableType);
1312 if (ctx->currentOffset) {
1313 return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, dstCapacity, limitedOutput, tableType, noDict, dictSmall, acceleration);
1314 } else {
1315 return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, dstCapacity, limitedOutput, tableType, noDict, noDictIssue, acceleration);
1317 } else {
1318 const tableType_t tableType = ((sizeof(void *) == 4) && ((uptrval)src > LZ4_DISTANCE_MAX)) ? byPtr : byU32;
1319 LZ4_prepareTable(ctx, srcSize, tableType);
1320 return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, dstCapacity, limitedOutput, tableType, noDict, noDictIssue, acceleration);
1326 int LZ4_compress_fast(const char *src, char *dst, int srcSize, int dstCapacity, int acceleration) {
1327 int result;
1328 #if (LZ4_HEAPMODE)
1329 LZ4_stream_t *ctxPtr = ALLOC(sizeof(LZ4_stream_t)); /* malloc-calloc always properly aligned */
1330 if (ctxPtr == NULL) return 0;
1331 #else
1332 LZ4_stream_t ctx;
1333 LZ4_stream_t *const ctxPtr = &ctx;
1334 #endif
1335 result = LZ4_compress_fast_extState(ctxPtr, src, dst, srcSize, dstCapacity, acceleration);
1337 #if (LZ4_HEAPMODE)
1338 FREEMEM(ctxPtr);
1339 #endif
1340 return result;
1344 int LZ4_compress_default(const char *src, char *dst, int srcSize, int dstCapacity) {
1345 return LZ4_compress_fast(src, dst, srcSize, dstCapacity, 1);
1349 /* Note!: This function leaves the stream in an unclean/broken state!
1350 * It is not safe to subsequently use the same state with a _fastReset() or
1351 * _continue() call without resetting it. */
1352 static int LZ4_compress_destSize_extState(LZ4_stream_t *state, const char *src, char *dst, int *srcSizePtr, int targetDstSize) {
1353 void *const s = LZ4_initStream(state, sizeof(*state));
1354 assert(s != NULL);
1355 (void)s;
1357 if (targetDstSize >= LZ4_compressBound(*srcSizePtr)) { /* compression success is guaranteed */
1358 return LZ4_compress_fast_extState(state, src, dst, *srcSizePtr, targetDstSize, 1);
1359 } else {
1360 if (*srcSizePtr < LZ4_64Klimit) {
1361 return LZ4_compress_generic(&state->internal_donotuse, src, dst, *srcSizePtr, srcSizePtr, targetDstSize, fillOutput, byU16, noDict, noDictIssue, 1);
1362 } else {
1363 tableType_t const addrMode = ((sizeof(void *) == 4) && ((uptrval)src > LZ4_DISTANCE_MAX)) ? byPtr : byU32;
1364 return LZ4_compress_generic(&state->internal_donotuse, src, dst, *srcSizePtr, srcSizePtr, targetDstSize, fillOutput, addrMode, noDict, noDictIssue, 1);
1370 int LZ4_compress_destSize(const char *src, char *dst, int *srcSizePtr, int targetDstSize) {
1371 #if (LZ4_HEAPMODE)
1372 LZ4_stream_t *ctx = (LZ4_stream_t *)ALLOC(sizeof(LZ4_stream_t)); /* malloc-calloc always properly aligned */
1373 if (ctx == NULL) return 0;
1374 #else
1375 LZ4_stream_t ctxBody;
1376 LZ4_stream_t *ctx = &ctxBody;
1377 #endif
1379 int result = LZ4_compress_destSize_extState(ctx, src, dst, srcSizePtr, targetDstSize);
1381 #if (LZ4_HEAPMODE)
1382 FREEMEM(ctx);
1383 #endif
1384 return result;
1389 /*-******************************
1390 * Streaming functions
1391 ********************************/
1393 LZ4_stream_t *LZ4_createStream(void) {
1394 LZ4_stream_t *const lz4s = (LZ4_stream_t *)ALLOC(sizeof(LZ4_stream_t));
1395 LZ4_STATIC_ASSERT(LZ4_STREAMSIZE >= sizeof(LZ4_stream_t_internal)); /* A compilation error here means LZ4_STREAMSIZE is not large enough */
1396 DEBUGLOG(4, "LZ4_createStream %p", lz4s);
1397 if (lz4s == NULL) return NULL;
1398 LZ4_initStream(lz4s, sizeof(*lz4s));
1399 return lz4s;
1402 #ifndef _MSC_VER /* for some reason, Visual fails the aligment test on 32-bit x86 :
1403 it reports an aligment of 8-bytes,
1404 while actually aligning LZ4_stream_t on 4 bytes. */
1405 static size_t LZ4_stream_t_alignment(void) {
1406 typedef struct { char c; LZ4_stream_t t; } t_a;
1407 return sizeof(t_a) - sizeof(LZ4_stream_t);
1409 #endif
1411 LZ4_stream_t *LZ4_initStream(void *buffer, size_t size) {
1412 DEBUGLOG(5, "LZ4_initStream");
1413 if (buffer == NULL) { return NULL; }
1414 if (size < sizeof(LZ4_stream_t)) { return NULL; }
1415 #ifndef _MSC_VER /* for some reason, Visual fails the aligment test on 32-bit x86 :
1416 it reports an aligment of 8-bytes,
1417 while actually aligning LZ4_stream_t on 4 bytes. */
1418 if (((size_t)buffer) & (LZ4_stream_t_alignment() - 1)) { return NULL; } /* alignment check */
1419 #endif
1420 MEM_INIT(buffer, 0, sizeof(LZ4_stream_t));
1421 return (LZ4_stream_t *)buffer;
1424 /* resetStream is now deprecated,
1425 * prefer initStream() which is more general */
1426 void LZ4_resetStream(LZ4_stream_t *LZ4_stream) {
1427 DEBUGLOG(5, "LZ4_resetStream (ctx:%p)", LZ4_stream);
1428 MEM_INIT(LZ4_stream, 0, sizeof(LZ4_stream_t));
1431 void LZ4_resetStream_fast(LZ4_stream_t *ctx) {
1432 LZ4_prepareTable(&(ctx->internal_donotuse), 0, byU32);
1435 int LZ4_freeStream(LZ4_stream_t *LZ4_stream) {
1436 if (!LZ4_stream) return 0; /* support free on NULL */
1437 DEBUGLOG(5, "LZ4_freeStream %p", LZ4_stream);
1438 FREEMEM(LZ4_stream);
1439 return (0);
1443 #define HASH_UNIT sizeof(reg_t)
1444 int LZ4_loadDict(LZ4_stream_t *LZ4_dict, const char *dictionary, int dictSize) {
1445 LZ4_stream_t_internal *dict = &LZ4_dict->internal_donotuse;
1446 const tableType_t tableType = byU32;
1447 const BYTE *p = (const BYTE *)dictionary;
1448 const BYTE *const dictEnd = p + dictSize;
1449 const BYTE *base;
1451 DEBUGLOG(4, "LZ4_loadDict (%i bytes from %p into %p)", dictSize, dictionary, LZ4_dict);
1453 /* It's necessary to reset the context,
1454 * and not just continue it with prepareTable()
1455 * to avoid any risk of generating overflowing matchIndex
1456 * when compressing using this dictionary */
1457 LZ4_resetStream(LZ4_dict);
1459 /* We always increment the offset by 64 KB, since, if the dict is longer,
1460 * we truncate it to the last 64k, and if it's shorter, we still want to
1461 * advance by a whole window length so we can provide the guarantee that
1462 * there are only valid offsets in the window, which allows an optimization
1463 * in LZ4_compress_fast_continue() where it uses noDictIssue even when the
1464 * dictionary isn't a full 64k. */
1465 dict->currentOffset += 64 KB;
1467 if (dictSize < (int)HASH_UNIT) {
1468 return 0;
1471 if ((dictEnd - p) > 64 KB) p = dictEnd - 64 KB;
1472 base = dictEnd - dict->currentOffset;
1473 dict->dictionary = p;
1474 dict->dictSize = (U32)(dictEnd - p);
1475 dict->tableType = (U32)tableType;
1477 while (p <= dictEnd - HASH_UNIT) {
1478 LZ4_putPosition(p, dict->hashTable, tableType, base);
1479 p += 3;
1482 return (int)dict->dictSize;
1485 void LZ4_attach_dictionary(LZ4_stream_t *workingStream, const LZ4_stream_t *dictionaryStream) {
1486 const LZ4_stream_t_internal *dictCtx = dictionaryStream == NULL ? NULL :
1487 &(dictionaryStream->internal_donotuse);
1489 DEBUGLOG(4, "LZ4_attach_dictionary (%p, %p, size %u)",
1490 workingStream, dictionaryStream,
1491 dictCtx != NULL ? dictCtx->dictSize : 0);
1493 if (dictCtx != NULL) {
1494 /* If the current offset is zero, we will never look in the
1495 * external dictionary context, since there is no value a table
1496 * entry can take that indicate a miss. In that case, we need
1497 * to bump the offset to something non-zero.
1499 if (workingStream->internal_donotuse.currentOffset == 0) {
1500 workingStream->internal_donotuse.currentOffset = 64 KB;
1503 /* Don't actually attach an empty dictionary.
1505 if (dictCtx->dictSize == 0) {
1506 dictCtx = NULL;
1509 workingStream->internal_donotuse.dictCtx = dictCtx;
1513 static void LZ4_renormDictT(LZ4_stream_t_internal *LZ4_dict, int nextSize) {
1514 assert(nextSize >= 0);
1515 if (LZ4_dict->currentOffset + (unsigned)nextSize > 0x80000000) { /* potential ptrdiff_t overflow (32-bits mode) */
1516 /* rescale hash table */
1517 U32 const delta = LZ4_dict->currentOffset - 64 KB;
1518 const BYTE *dictEnd = LZ4_dict->dictionary + LZ4_dict->dictSize;
1519 int i;
1520 DEBUGLOG(4, "LZ4_renormDictT");
1521 for (i = 0; i < LZ4_HASH_SIZE_U32; i++) {
1522 if (LZ4_dict->hashTable[i] < delta) LZ4_dict->hashTable[i] = 0;
1523 else LZ4_dict->hashTable[i] -= delta;
1525 LZ4_dict->currentOffset = 64 KB;
1526 if (LZ4_dict->dictSize > 64 KB) LZ4_dict->dictSize = 64 KB;
1527 LZ4_dict->dictionary = dictEnd - LZ4_dict->dictSize;
1532 int LZ4_compress_fast_continue(LZ4_stream_t *LZ4_stream,
1533 const char *src, char *dst,
1534 int srcSize, int dstCapacity,
1535 int acceleration) {
1536 const tableType_t tableType = byU32;
1537 LZ4_stream_t_internal *streamPtr = &LZ4_stream->internal_donotuse;
1538 const BYTE *dictEnd = streamPtr->dictionary + streamPtr->dictSize;
1540 DEBUGLOG(5, "LZ4_compress_fast_continue (srcSize=%i)", srcSize);
1542 LZ4_renormDictT(streamPtr, srcSize); /* avoid index overflow */
1543 if (acceleration < 1) acceleration = LZ4_ACCELERATION_DEFAULT;
1544 if (acceleration > LZ4_ACCELERATION_MAX) acceleration = LZ4_ACCELERATION_MAX;
1546 /* invalidate tiny dictionaries */
1547 if ((streamPtr->dictSize - 1 < 4 - 1) /* intentional underflow */
1548 && (dictEnd != (const BYTE *)src)) {
1549 DEBUGLOG(5, "LZ4_compress_fast_continue: dictSize(%u) at addr:%p is too small", streamPtr->dictSize, streamPtr->dictionary);
1550 streamPtr->dictSize = 0;
1551 streamPtr->dictionary = (const BYTE *)src;
1552 dictEnd = (const BYTE *)src;
1555 /* Check overlapping input/dictionary space */
1557 const BYTE *srcEnd = (const BYTE *) src + srcSize;
1558 if ((srcEnd > streamPtr->dictionary) && (srcEnd < dictEnd)) {
1559 streamPtr->dictSize = (U32)(dictEnd - srcEnd);
1560 if (streamPtr->dictSize > 64 KB) streamPtr->dictSize = 64 KB;
1561 if (streamPtr->dictSize < 4) streamPtr->dictSize = 0;
1562 streamPtr->dictionary = dictEnd - streamPtr->dictSize;
1566 /* prefix mode : source data follows dictionary */
1567 if (dictEnd == (const BYTE *)src) {
1568 if ((streamPtr->dictSize < 64 KB) && (streamPtr->dictSize < streamPtr->currentOffset))
1569 return LZ4_compress_generic(streamPtr, src, dst, srcSize, NULL, dstCapacity, limitedOutput, tableType, withPrefix64k, dictSmall, acceleration);
1570 else
1571 return LZ4_compress_generic(streamPtr, src, dst, srcSize, NULL, dstCapacity, limitedOutput, tableType, withPrefix64k, noDictIssue, acceleration);
1574 /* external dictionary mode */
1576 int result;
1577 if (streamPtr->dictCtx) {
1578 /* We depend here on the fact that dictCtx'es (produced by
1579 * LZ4_loadDict) guarantee that their tables contain no references
1580 * to offsets between dictCtx->currentOffset - 64 KB and
1581 * dictCtx->currentOffset - dictCtx->dictSize. This makes it safe
1582 * to use noDictIssue even when the dict isn't a full 64 KB.
1584 if (srcSize > 4 KB) {
1585 /* For compressing large blobs, it is faster to pay the setup
1586 * cost to copy the dictionary's tables into the active context,
1587 * so that the compression loop is only looking into one table.
1589 LZ4_memcpy(streamPtr, streamPtr->dictCtx, sizeof(*streamPtr));
1590 result = LZ4_compress_generic(streamPtr, src, dst, srcSize, NULL, dstCapacity, limitedOutput, tableType, usingExtDict, noDictIssue, acceleration);
1591 } else {
1592 result = LZ4_compress_generic(streamPtr, src, dst, srcSize, NULL, dstCapacity, limitedOutput, tableType, usingDictCtx, noDictIssue, acceleration);
1594 } else {
1595 if ((streamPtr->dictSize < 64 KB) && (streamPtr->dictSize < streamPtr->currentOffset)) {
1596 result = LZ4_compress_generic(streamPtr, src, dst, srcSize, NULL, dstCapacity, limitedOutput, tableType, usingExtDict, dictSmall, acceleration);
1597 } else {
1598 result = LZ4_compress_generic(streamPtr, src, dst, srcSize, NULL, dstCapacity, limitedOutput, tableType, usingExtDict, noDictIssue, acceleration);
1601 streamPtr->dictionary = (const BYTE *)src;
1602 streamPtr->dictSize = (U32)srcSize;
1603 return result;
1608 /* Hidden debug function, to force-test external dictionary mode */
1609 int LZ4_compress_forceExtDict(LZ4_stream_t *LZ4_dict, const char *src, char *dst, int srcSize) {
1610 LZ4_stream_t_internal *streamPtr = &LZ4_dict->internal_donotuse;
1611 int result;
1613 LZ4_renormDictT(streamPtr, srcSize);
1615 if ((streamPtr->dictSize < 64 KB) && (streamPtr->dictSize < streamPtr->currentOffset)) {
1616 result = LZ4_compress_generic(streamPtr, src, dst, srcSize, NULL, 0, notLimited, byU32, usingExtDict, dictSmall, 1);
1617 } else {
1618 result = LZ4_compress_generic(streamPtr, src, dst, srcSize, NULL, 0, notLimited, byU32, usingExtDict, noDictIssue, 1);
1621 streamPtr->dictionary = (const BYTE *)src;
1622 streamPtr->dictSize = (U32)srcSize;
1624 return result;
1628 /*! LZ4_saveDict() :
1629 * If previously compressed data block is not guaranteed to remain available at its memory location,
1630 * save it into a safer place (char* safeBuffer).
1631 * Note : you don't need to call LZ4_loadDict() afterwards,
1632 * dictionary is immediately usable, you can therefore call LZ4_compress_fast_continue().
1633 * Return : saved dictionary size in bytes (necessarily <= dictSize), or 0 if error.
1635 int LZ4_saveDict(LZ4_stream_t *LZ4_dict, char *safeBuffer, int dictSize) {
1636 LZ4_stream_t_internal *const dict = &LZ4_dict->internal_donotuse;
1637 const BYTE *const previousDictEnd = dict->dictionary + dict->dictSize;
1639 if ((U32)dictSize > 64 KB) { dictSize = 64 KB; } /* useless to define a dictionary > 64 KB */
1640 if ((U32)dictSize > dict->dictSize) { dictSize = (int)dict->dictSize; }
1642 memmove(safeBuffer, previousDictEnd - dictSize, dictSize);
1644 dict->dictionary = (const BYTE *)safeBuffer;
1645 dict->dictSize = (U32)dictSize;
1647 return dictSize;
1652 /*-*******************************
1653 * Decompression functions
1654 ********************************/
1656 typedef enum { endOnOutputSize = 0, endOnInputSize = 1 } endCondition_directive;
1657 typedef enum { decode_full_block = 0, partial_decode = 1 } earlyEnd_directive;
1659 #undef MIN
1660 #define MIN(a,b) ( (a) < (b) ? (a) : (b) )
1662 /* Read the variable-length literal or match length.
1664 * ip - pointer to use as input.
1665 * lencheck - end ip. Return an error if ip advances >= lencheck.
1666 * loop_check - check ip >= lencheck in body of loop. Returns loop_error if so.
1667 * initial_check - check ip >= lencheck before start of loop. Returns initial_error if so.
1668 * error (output) - error code. Should be set to 0 before call.
1670 typedef enum { loop_error = -2, initial_error = -1, ok = 0 } variable_length_error;
1671 LZ4_FORCE_INLINE unsigned
1672 read_variable_length(const BYTE **ip, const BYTE *lencheck,
1673 int loop_check, int initial_check,
1674 variable_length_error *error) {
1675 U32 length = 0;
1676 U32 s;
1677 if (initial_check && unlikely((*ip) >= lencheck)) { /* overflow detection */
1678 *error = initial_error;
1679 return length;
1681 do {
1682 s = **ip;
1683 (*ip)++;
1684 length += s;
1685 if (loop_check && unlikely((*ip) >= lencheck)) { /* overflow detection */
1686 *error = loop_error;
1687 return length;
1689 } while (s == 255);
1691 return length;
1694 int LZ4_decompress_generic(const char *const src, char *const dst, int srcSize, int outputSize, endCondition_directive endOnInput, earlyEnd_directive partialDecoding,
1695 dict_directive dict, const BYTE *const lowPrefix, const BYTE *const dictStart, const size_t dictSize);
1697 /*! LZ4_decompress_generic() :
1698 * This generic decompression function covers all use cases.
1699 * It shall be instantiated several times, using different sets of directives.
1700 * Note that it is important for performance that this function really get inlined,
1701 * in order to remove useless branches during compilation optimization.
1704 LZ4_decompress_generic(
1705 const char *const src,
1706 char *const dst,
1707 int srcSize,
1708 int outputSize, /* If endOnInput==endOnInputSize, this value is `dstCapacity` */
1710 endCondition_directive endOnInput, /* endOnOutputSize, endOnInputSize */
1711 earlyEnd_directive partialDecoding, /* full, partial */
1712 dict_directive dict, /* noDict, withPrefix64k, usingExtDict */
1713 const BYTE *const lowPrefix, /* always <= dst, == dst when no prefix */
1714 const BYTE *const dictStart, /* only if dict==usingExtDict */
1715 const size_t dictSize /* note : = 0 if noDict */
1717 if (src == NULL) { return -1; }
1720 const BYTE *ip = (const BYTE *) src;
1721 const BYTE *const iend = ip + srcSize;
1723 BYTE *op = (BYTE *) dst;
1724 BYTE *const oend = op + outputSize;
1725 BYTE *cpy;
1727 const BYTE *const dictEnd = (dictStart == NULL) ? NULL : dictStart + dictSize;
1729 const int safeDecode = (endOnInput == endOnInputSize);
1730 const int checkOffset = ((safeDecode) && (dictSize < (int)(64 KB)));
1733 /* Set up the "end" pointers for the shortcut. */
1734 const BYTE *const shortiend = iend - (endOnInput ? 14 : 8) /*maxLL*/ - 2 /*offset*/;
1735 const BYTE *const shortoend = oend - (endOnInput ? 14 : 8) /*maxLL*/ - 18 /*maxML*/;
1737 const BYTE *match;
1738 size_t offset;
1739 unsigned token;
1740 size_t length;
1743 DEBUGLOG(5, "LZ4_decompress_generic (srcSize:%i, dstSize:%i)", srcSize, outputSize);
1745 /* Special cases */
1746 assert(lowPrefix <= op);
1747 if ((endOnInput) && (unlikely(outputSize == 0))) {
1748 /* Empty output buffer */
1749 if (partialDecoding) return 0;
1750 return ((srcSize == 1) && (*ip == 0)) ? 0 : -1;
1752 if ((!endOnInput) && (unlikely(outputSize == 0))) { return (*ip == 0 ? 1 : -1); }
1753 if ((endOnInput) && unlikely(srcSize == 0)) { return -1; }
1755 /* Currently the fast loop shows a regression on qualcomm arm chips. */
1756 #if LZ4_FAST_DEC_LOOP
1757 if ((oend - op) < FASTLOOP_SAFE_DISTANCE) {
1758 DEBUGLOG(6, "skip fast decode loop");
1759 goto safe_decode;
1762 /* Fast loop : decode sequences as long as output < iend-FASTLOOP_SAFE_DISTANCE */
1763 while (1) {
1764 /* Main fastloop assertion: We can always wildcopy FASTLOOP_SAFE_DISTANCE */
1765 assert(oend - op >= FASTLOOP_SAFE_DISTANCE);
1766 if (endOnInput) { assert(ip < iend); }
1767 token = *ip++;
1768 length = token >> ML_BITS; /* literal length */
1770 assert(!endOnInput || ip <= iend); /* ip < iend before the increment */
1772 /* decode literal length */
1773 if (length == RUN_MASK) {
1774 variable_length_error error = ok;
1775 length += read_variable_length(&ip, iend - RUN_MASK, (int)endOnInput, (int)endOnInput, &error);
1776 if (error == initial_error) { goto _output_error; }
1777 if ((safeDecode) && unlikely((uptrval)(op) + length < (uptrval)(op))) { goto _output_error; } /* overflow detection */
1778 if ((safeDecode) && unlikely((uptrval)(ip) + length < (uptrval)(ip))) { goto _output_error; } /* overflow detection */
1780 /* copy literals */
1781 cpy = op + length;
1782 LZ4_STATIC_ASSERT(MFLIMIT >= WILDCOPYLENGTH);
1783 if (endOnInput) { /* LZ4_decompress_safe() */
1784 if ((cpy > oend - 32) || (ip + length > iend - 32)) { goto safe_literal_copy; }
1785 LZ4_wildCopy32(op, ip, cpy);
1786 } else { /* LZ4_decompress_fast() */
1787 if (cpy > oend - 8) { goto safe_literal_copy; }
1788 LZ4_wildCopy8(op, ip, cpy); /* LZ4_decompress_fast() cannot copy more than 8 bytes at a time :
1789 * it doesn't know input length, and only relies on end-of-block properties */
1791 ip += length;
1792 op = cpy;
1793 } else {
1794 cpy = op + length;
1795 if (endOnInput) { /* LZ4_decompress_safe() */
1796 DEBUGLOG(7, "copy %u bytes in a 16-bytes stripe", (unsigned)length);
1797 /* We don't need to check oend, since we check it once for each loop below */
1798 if (ip > iend - (16 + 1/*max lit + offset + nextToken*/)) { goto safe_literal_copy; }
1799 /* Literals can only be 14, but hope compilers optimize if we copy by a register size */
1800 LZ4_memcpy(op, ip, 16);
1801 } else { /* LZ4_decompress_fast() */
1802 /* LZ4_decompress_fast() cannot copy more than 8 bytes at a time :
1803 * it doesn't know input length, and relies on end-of-block properties */
1804 LZ4_memcpy(op, ip, 8);
1805 if (length > 8) { LZ4_memcpy(op + 8, ip + 8, 8); }
1807 ip += length;
1808 op = cpy;
1811 /* get offset */
1812 offset = LZ4_readLE16(ip);
1813 ip += 2;
1814 match = op - offset;
1815 assert(match <= op);
1817 /* get matchlength */
1818 length = token & ML_MASK;
1820 if (length == ML_MASK) {
1821 variable_length_error error = ok;
1822 if ((checkOffset) && (unlikely(match + dictSize < lowPrefix))) { goto _output_error; } /* Error : offset outside buffers */
1823 length += read_variable_length(&ip, iend - LASTLITERALS + 1, (int)endOnInput, 0, &error);
1824 if (error != ok) { goto _output_error; }
1825 if ((safeDecode) && unlikely((uptrval)(op) + length < (uptrval)op)) { goto _output_error; } /* overflow detection */
1826 length += MINMATCH;
1827 if (op + length >= oend - FASTLOOP_SAFE_DISTANCE) {
1828 goto safe_match_copy;
1830 } else {
1831 length += MINMATCH;
1832 if (op + length >= oend - FASTLOOP_SAFE_DISTANCE) {
1833 goto safe_match_copy;
1836 /* Fastpath check: Avoids a branch in LZ4_wildCopy32 if true */
1837 if ((dict == withPrefix64k) || (match >= lowPrefix)) {
1838 if (offset >= 8) {
1839 assert(match >= lowPrefix);
1840 assert(match <= op);
1841 assert(op + 18 <= oend);
1843 LZ4_memcpy(op, match, 8);
1844 LZ4_memcpy(op + 8, match + 8, 8);
1845 LZ4_memcpy(op + 16, match + 16, 2);
1846 op += length;
1847 continue;
1852 if (checkOffset && (unlikely(match + dictSize < lowPrefix))) { goto _output_error; } /* Error : offset outside buffers */
1853 /* match starting within external dictionary */
1854 if ((dict == usingExtDict) && (match < lowPrefix)) {
1855 if (unlikely(op + length > oend - LASTLITERALS)) {
1856 if (partialDecoding) {
1857 DEBUGLOG(7, "partialDecoding: dictionary match, close to dstEnd");
1858 length = MIN(length, (size_t)(oend - op));
1859 } else {
1860 goto _output_error; /* end-of-block condition violated */
1864 if (length <= (size_t)(lowPrefix - match)) {
1865 /* match fits entirely within external dictionary : just copy */
1866 memmove(op, dictEnd - (lowPrefix - match), length);
1867 op += length;
1868 } else {
1869 /* match stretches into both external dictionary and current block */
1870 size_t const copySize = (size_t)(lowPrefix - match);
1871 size_t const restSize = length - copySize;
1872 LZ4_memcpy(op, dictEnd - copySize, copySize);
1873 op += copySize;
1874 if (restSize > (size_t)(op - lowPrefix)) { /* overlap copy */
1875 BYTE *const endOfMatch = op + restSize;
1876 const BYTE *copyFrom = lowPrefix;
1877 while (op < endOfMatch) { *op++ = *copyFrom++; }
1878 } else {
1879 LZ4_memcpy(op, lowPrefix, restSize);
1880 op += restSize;
1883 continue;
1886 /* copy match within block */
1887 cpy = op + length;
1889 assert((op <= oend) && (oend - op >= 32));
1890 if (unlikely(offset < 16)) {
1891 LZ4_memcpy_using_offset(op, match, cpy, offset);
1892 } else {
1893 LZ4_wildCopy32(op, match, cpy);
1896 op = cpy; /* wildcopy correction */
1898 safe_decode:
1899 #endif
1901 /* Main Loop : decode remaining sequences where output < FASTLOOP_SAFE_DISTANCE */
1902 while (1) {
1903 token = *ip++;
1904 length = token >> ML_BITS; /* literal length */
1906 assert(!endOnInput || ip <= iend); /* ip < iend before the increment */
1908 /* A two-stage shortcut for the most common case:
1909 * 1) If the literal length is 0..14, and there is enough space,
1910 * enter the shortcut and copy 16 bytes on behalf of the literals
1911 * (in the fast mode, only 8 bytes can be safely copied this way).
1912 * 2) Further if the match length is 4..18, copy 18 bytes in a similar
1913 * manner; but we ensure that there's enough space in the output for
1914 * those 18 bytes earlier, upon entering the shortcut (in other words,
1915 * there is a combined check for both stages).
1917 if ((endOnInput ? length != RUN_MASK : length <= 8)
1918 /* strictly "less than" on input, to re-enter the loop with at least one byte */
1919 && likely((endOnInput ? ip < shortiend : 1) & (op <= shortoend))) {
1920 /* Copy the literals */
1921 LZ4_memcpy(op, ip, endOnInput ? 16 : 8);
1922 op += length;
1923 ip += length;
1925 /* The second stage: prepare for match copying, decode full info.
1926 * If it doesn't work out, the info won't be wasted. */
1927 length = token & ML_MASK; /* match length */
1928 offset = LZ4_readLE16(ip);
1929 ip += 2;
1930 match = op - offset;
1931 assert(match <= op); /* check overflow */
1933 /* Do not deal with overlapping matches. */
1934 if ((length != ML_MASK)
1935 && (offset >= 8)
1936 && (dict == withPrefix64k || match >= lowPrefix)) {
1937 /* Copy the match. */
1938 LZ4_memcpy(op + 0, match + 0, 8);
1939 LZ4_memcpy(op + 8, match + 8, 8);
1940 LZ4_memcpy(op + 16, match + 16, 2);
1941 op += length + MINMATCH;
1942 /* Both stages worked, load the next token. */
1943 continue;
1946 /* The second stage didn't work out, but the info is ready.
1947 * Propel it right to the point of match copying. */
1948 goto _copy_match;
1951 /* decode literal length */
1952 if (length == RUN_MASK) {
1953 variable_length_error error = ok;
1954 length += read_variable_length(&ip, iend - RUN_MASK, (int)endOnInput, (int)endOnInput, &error);
1955 if (error == initial_error) { goto _output_error; }
1956 if ((safeDecode) && unlikely((uptrval)(op) + length < (uptrval)(op))) { goto _output_error; } /* overflow detection */
1957 if ((safeDecode) && unlikely((uptrval)(ip) + length < (uptrval)(ip))) { goto _output_error; } /* overflow detection */
1960 /* copy literals */
1961 cpy = op + length;
1962 #if LZ4_FAST_DEC_LOOP
1963 safe_literal_copy:
1964 #endif
1965 LZ4_STATIC_ASSERT(MFLIMIT >= WILDCOPYLENGTH);
1966 if (((endOnInput) && ((cpy > oend - MFLIMIT) || (ip + length > iend - (2 + 1 + LASTLITERALS))))
1967 || ((!endOnInput) && (cpy > oend - WILDCOPYLENGTH))) {
1968 /* We've either hit the input parsing restriction or the output parsing restriction.
1969 * In the normal scenario, decoding a full block, it must be the last sequence,
1970 * otherwise it's an error (invalid input or dimensions).
1971 * In partialDecoding scenario, it's necessary to ensure there is no buffer overflow.
1973 if (partialDecoding) {
1974 /* Since we are partial decoding we may be in this block because of the output parsing
1975 * restriction, which is not valid since the output buffer is allowed to be undersized.
1977 assert(endOnInput);
1978 DEBUGLOG(7, "partialDecoding: copying literals, close to input or output end")
1979 DEBUGLOG(7, "partialDecoding: literal length = %u", (unsigned)length);
1980 DEBUGLOG(7, "partialDecoding: remaining space in dstBuffer : %i", (int)(oend - op));
1981 DEBUGLOG(7, "partialDecoding: remaining space in srcBuffer : %i", (int)(iend - ip));
1982 /* Finishing in the middle of a literals segment,
1983 * due to lack of input.
1985 if (ip + length > iend) {
1986 length = (size_t)(iend - ip);
1987 cpy = op + length;
1989 /* Finishing in the middle of a literals segment,
1990 * due to lack of output space.
1992 if (cpy > oend) {
1993 cpy = oend;
1994 assert(op <= oend);
1995 length = (size_t)(oend - op);
1997 } else {
1998 /* We must be on the last sequence because of the parsing limitations so check
1999 * that we exactly regenerate the original size (must be exact when !endOnInput).
2001 if ((!endOnInput) && (cpy != oend)) { goto _output_error; }
2002 /* We must be on the last sequence (or invalid) because of the parsing limitations
2003 * so check that we exactly consume the input and don't overrun the output buffer.
2005 if ((endOnInput) && ((ip + length != iend) || (cpy > oend))) {
2006 DEBUGLOG(6, "should have been last run of literals")
2007 DEBUGLOG(6, "ip(%p) + length(%i) = %p != iend (%p)", ip, (int)length, ip + length, iend);
2008 DEBUGLOG(6, "or cpy(%p) > oend(%p)", cpy, oend);
2009 goto _output_error;
2012 memmove(op, ip, length); /* supports overlapping memory regions; only matters for in-place decompression scenarios */
2013 ip += length;
2014 op += length;
2015 /* Necessarily EOF when !partialDecoding.
2016 * When partialDecoding, it is EOF if we've either
2017 * filled the output buffer or
2018 * can't proceed with reading an offset for following match.
2020 if (!partialDecoding || (cpy == oend) || (ip >= (iend - 2))) {
2021 break;
2023 } else {
2024 LZ4_wildCopy8(op, ip, cpy); /* may overwrite up to WILDCOPYLENGTH beyond cpy */
2025 ip += length;
2026 op = cpy;
2029 /* get offset */
2030 offset = LZ4_readLE16(ip);
2031 ip += 2;
2032 match = op - offset;
2034 /* get matchlength */
2035 length = token & ML_MASK;
2037 _copy_match:
2038 if (length == ML_MASK) {
2039 variable_length_error error = ok;
2040 length += read_variable_length(&ip, iend - LASTLITERALS + 1, (int)endOnInput, 0, &error);
2041 if (error != ok) goto _output_error;
2042 if ((safeDecode) && unlikely((uptrval)(op) + length < (uptrval)op)) goto _output_error; /* overflow detection */
2044 length += MINMATCH;
2046 #if LZ4_FAST_DEC_LOOP
2047 safe_match_copy:
2048 #endif
2049 if ((checkOffset) && (unlikely(match + dictSize < lowPrefix))) goto _output_error; /* Error : offset outside buffers */
2050 /* match starting within external dictionary */
2051 if ((dict == usingExtDict) && (match < lowPrefix)) {
2052 if (unlikely(op + length > oend - LASTLITERALS)) {
2053 if (partialDecoding) length = MIN(length, (size_t)(oend - op));
2054 else goto _output_error; /* doesn't respect parsing restriction */
2057 if (length <= (size_t)(lowPrefix - match)) {
2058 /* match fits entirely within external dictionary : just copy */
2059 memmove(op, dictEnd - (lowPrefix - match), length);
2060 op += length;
2061 } else {
2062 /* match stretches into both external dictionary and current block */
2063 size_t const copySize = (size_t)(lowPrefix - match);
2064 size_t const restSize = length - copySize;
2065 LZ4_memcpy(op, dictEnd - copySize, copySize);
2066 op += copySize;
2067 if (restSize > (size_t)(op - lowPrefix)) { /* overlap copy */
2068 BYTE *const endOfMatch = op + restSize;
2069 const BYTE *copyFrom = lowPrefix;
2070 while (op < endOfMatch) *op++ = *copyFrom++;
2071 } else {
2072 LZ4_memcpy(op, lowPrefix, restSize);
2073 op += restSize;
2076 continue;
2078 assert(match >= lowPrefix);
2080 /* copy match within block */
2081 cpy = op + length;
2083 /* partialDecoding : may end anywhere within the block */
2084 assert(op <= oend);
2085 if (partialDecoding && (cpy > oend - MATCH_SAFEGUARD_DISTANCE)) {
2086 size_t const mlen = MIN(length, (size_t)(oend - op));
2087 const BYTE *const matchEnd = match + mlen;
2088 BYTE *const copyEnd = op + mlen;
2089 if (matchEnd > op) { /* overlap copy */
2090 while (op < copyEnd) { *op++ = *match++; }
2091 } else {
2092 LZ4_memcpy(op, match, mlen);
2094 op = copyEnd;
2095 if (op == oend) { break; }
2096 continue;
2099 if (unlikely(offset < 8)) {
2100 LZ4_write32(op, 0); /* silence msan warning when offset==0 */
2101 op[0] = match[0];
2102 op[1] = match[1];
2103 op[2] = match[2];
2104 op[3] = match[3];
2105 match += inc32table[offset];
2106 LZ4_memcpy(op + 4, match, 4);
2107 match -= dec64table[offset];
2108 } else {
2109 LZ4_memcpy(op, match, 8);
2110 match += 8;
2112 op += 8;
2114 if (unlikely(cpy > oend - MATCH_SAFEGUARD_DISTANCE)) {
2115 BYTE *const oCopyLimit = oend - (WILDCOPYLENGTH - 1);
2116 if (cpy > oend - LASTLITERALS) { goto _output_error; } /* Error : last LASTLITERALS bytes must be literals (uncompressed) */
2117 if (op < oCopyLimit) {
2118 LZ4_wildCopy8(op, match, oCopyLimit);
2119 match += oCopyLimit - op;
2120 op = oCopyLimit;
2122 while (op < cpy) { *op++ = *match++; }
2123 } else {
2124 LZ4_memcpy(op, match, 8);
2125 if (length > 16) { LZ4_wildCopy8(op + 8, match + 8, cpy); }
2127 op = cpy; /* wildcopy correction */
2130 /* end of decoding */
2131 if (endOnInput) {
2132 DEBUGLOG(5, "decoded %i bytes", (int)(((char *)op) - dst));
2133 return (int)(((char *)op) - dst); /* Nb of output bytes decoded */
2134 } else {
2135 return (int)(((const char *)ip) - src); /* Nb of input bytes read */
2138 /* Overflow error detected */
2139 _output_error:
2140 return (int)(-(((const char *)ip) - src)) - 1;
2145 /*===== Instantiate the API decoding functions. =====*/
2147 LZ4_FORCE_O2_GCC_PPC64LE
2148 int LZ4_decompress_safe(const char *src, char *dst, int compressedSize, int maxDecompressedSize) {
2149 return LZ4_decompress_generic(src, dst, compressedSize, maxDecompressedSize,
2150 endOnInputSize, decode_full_block, noDict,
2151 (BYTE *)dst, NULL, 0);
2154 LZ4_FORCE_O2_GCC_PPC64LE
2155 int LZ4_decompress_safe_partial(const char *src, char *dst, int compressedSize, int targetOutputSize, int dstCapacity) {
2156 dstCapacity = MIN(targetOutputSize, dstCapacity);
2157 return LZ4_decompress_generic(src, dst, compressedSize, dstCapacity,
2158 endOnInputSize, partial_decode,
2159 noDict, (BYTE *)dst, NULL, 0);
2162 LZ4_FORCE_O2_GCC_PPC64LE
2163 int LZ4_decompress_fast(const char *src, char *dst, int originalSize) {
2164 return LZ4_decompress_generic(src, dst, 0, originalSize,
2165 endOnOutputSize, decode_full_block, withPrefix64k,
2166 (BYTE *)dst - 64 KB, NULL, 0);
2169 /*===== Instantiate a few more decoding cases, used more than once. =====*/
2171 LZ4_FORCE_O2_GCC_PPC64LE /* Exported, an obsolete API function. */
2172 int LZ4_decompress_safe_withPrefix64k(const char *src, char *dst, int compressedSize, int dstCapacity) {
2173 return LZ4_decompress_generic(src, dst, compressedSize, dstCapacity,
2174 endOnInputSize, decode_full_block, withPrefix64k,
2175 (BYTE *)dst - 64 KB, NULL, 0);
2178 /* Another obsolete API function, paired with the previous one. */
2179 int LZ4_decompress_fast_withPrefix64k(const char *src, char *dst, int originalSize) {
2180 /* LZ4_decompress_fast doesn't validate match offsets,
2181 * and thus serves well with any prefixed dictionary. */
2182 return LZ4_decompress_fast(src, dst, originalSize);
2185 LZ4_FORCE_O2_GCC_PPC64LE
2186 static int LZ4_decompress_safe_withSmallPrefix(const char *src, char *dst, int compressedSize, int dstCapacity,
2187 size_t prefixSize) {
2188 return LZ4_decompress_generic(src, dst, compressedSize, dstCapacity,
2189 endOnInputSize, decode_full_block, noDict,
2190 (BYTE *)dst - prefixSize, NULL, 0);
2193 LZ4_FORCE_O2_GCC_PPC64LE
2194 int LZ4_decompress_safe_forceExtDict(const char *src, char *dst,
2195 int compressedSize, int dstCapacity,
2196 const void *dictStart, size_t dictSize) {
2197 return LZ4_decompress_generic(src, dst, compressedSize, dstCapacity,
2198 endOnInputSize, decode_full_block, usingExtDict,
2199 (BYTE *)dst, (const BYTE *)dictStart, dictSize);
2202 LZ4_FORCE_O2_GCC_PPC64LE
2203 static int LZ4_decompress_fast_extDict(const char *src, char *dst, int originalSize,
2204 const void *dictStart, size_t dictSize) {
2205 return LZ4_decompress_generic(src, dst, 0, originalSize,
2206 endOnOutputSize, decode_full_block, usingExtDict,
2207 (BYTE *)dst, (const BYTE *)dictStart, dictSize);
2210 /* The "double dictionary" mode, for use with e.g. ring buffers: the first part
2211 * of the dictionary is passed as prefix, and the second via dictStart + dictSize.
2212 * These routines are used only once, in LZ4_decompress_*_continue().
2214 LZ4_FORCE_INLINE
2215 int LZ4_decompress_safe_doubleDict(const char *src, char *dst, int compressedSize, int dstCapacity,
2216 size_t prefixSize, const void *dictStart, size_t dictSize) {
2217 return LZ4_decompress_generic(src, dst, compressedSize, dstCapacity,
2218 endOnInputSize, decode_full_block, usingExtDict,
2219 (BYTE *)dst - prefixSize, (const BYTE *)dictStart, dictSize);
2222 LZ4_FORCE_INLINE
2223 int LZ4_decompress_fast_doubleDict(const char *src, char *dst, int originalSize,
2224 size_t prefixSize, const void *dictStart, size_t dictSize) {
2225 return LZ4_decompress_generic(src, dst, 0, originalSize,
2226 endOnOutputSize, decode_full_block, usingExtDict,
2227 (BYTE *)dst - prefixSize, (const BYTE *)dictStart, dictSize);
2230 /*===== streaming decompression functions =====*/
2232 LZ4_streamDecode_t *LZ4_createStreamDecode(void) {
2233 LZ4_streamDecode_t *lz4s = (LZ4_streamDecode_t *) ALLOC_AND_ZERO(sizeof(LZ4_streamDecode_t));
2234 LZ4_STATIC_ASSERT(LZ4_STREAMDECODESIZE >= sizeof(LZ4_streamDecode_t_internal)); /* A compilation error here means LZ4_STREAMDECODESIZE is not large enough */
2235 return lz4s;
2238 int LZ4_freeStreamDecode(LZ4_streamDecode_t *LZ4_stream) {
2239 if (LZ4_stream == NULL) { return 0; } /* support free on NULL */
2240 FREEMEM(LZ4_stream);
2241 return 0;
2244 /*! LZ4_setStreamDecode() :
2245 * Use this function to instruct where to find the dictionary.
2246 * This function is not necessary if previous data is still available where it was decoded.
2247 * Loading a size of 0 is allowed (same effect as no dictionary).
2248 * @return : 1 if OK, 0 if error
2250 int LZ4_setStreamDecode(LZ4_streamDecode_t *LZ4_streamDecode, const char *dictionary, int dictSize) {
2251 LZ4_streamDecode_t_internal *lz4sd = &LZ4_streamDecode->internal_donotuse;
2252 lz4sd->prefixSize = (size_t) dictSize;
2253 lz4sd->prefixEnd = (const BYTE *) dictionary + dictSize;
2254 lz4sd->externalDict = NULL;
2255 lz4sd->extDictSize = 0;
2256 return 1;
2259 /*! LZ4_decoderRingBufferSize() :
2260 * when setting a ring buffer for streaming decompression (optional scenario),
2261 * provides the minimum size of this ring buffer
2262 * to be compatible with any source respecting maxBlockSize condition.
2263 * Note : in a ring buffer scenario,
2264 * blocks are presumed decompressed next to each other.
2265 * When not enough space remains for next block (remainingSize < maxBlockSize),
2266 * decoding resumes from beginning of ring buffer.
2267 * @return : minimum ring buffer size,
2268 * or 0 if there is an error (invalid maxBlockSize).
2270 int LZ4_decoderRingBufferSize(int maxBlockSize) {
2271 if (maxBlockSize < 0) return 0;
2272 if (maxBlockSize > LZ4_MAX_INPUT_SIZE) return 0;
2273 if (maxBlockSize < 16) maxBlockSize = 16;
2274 return LZ4_DECODER_RING_BUFFER_SIZE(maxBlockSize);
2278 *_continue() :
2279 These decoding functions allow decompression of multiple blocks in "streaming" mode.
2280 Previously decoded blocks must still be available at the memory position where they were decoded.
2281 If it's not possible, save the relevant part of decoded data into a safe buffer,
2282 and indicate where it stands using LZ4_setStreamDecode()
2284 LZ4_FORCE_O2_GCC_PPC64LE
2285 int LZ4_decompress_safe_continue(LZ4_streamDecode_t *LZ4_streamDecode, const char *src, char *dst, int compressedSize, int dstCapacity) {
2286 LZ4_streamDecode_t_internal *lz4sd = &LZ4_streamDecode->internal_donotuse;
2287 int result;
2289 if (lz4sd->prefixSize == 0) {
2290 /* The first call, no dictionary yet. */
2291 assert(lz4sd->extDictSize == 0);
2292 result = LZ4_decompress_safe(src, dst, compressedSize, dstCapacity);
2293 if (result <= 0) return result;
2294 lz4sd->prefixSize = (size_t)result;
2295 lz4sd->prefixEnd = (BYTE *)dst + result;
2296 } else if (lz4sd->prefixEnd == (BYTE *)dst) {
2297 /* They're rolling the current segment. */
2298 if (lz4sd->prefixSize >= 64 KB - 1)
2299 result = LZ4_decompress_safe_withPrefix64k(src, dst, compressedSize, dstCapacity);
2300 else if (lz4sd->extDictSize == 0)
2301 result = LZ4_decompress_safe_withSmallPrefix(src, dst, compressedSize, dstCapacity,
2302 lz4sd->prefixSize);
2303 else
2304 result = LZ4_decompress_safe_doubleDict(src, dst, compressedSize, dstCapacity,
2305 lz4sd->prefixSize, lz4sd->externalDict, lz4sd->extDictSize);
2306 if (result <= 0) return result;
2307 lz4sd->prefixSize += (size_t)result;
2308 lz4sd->prefixEnd += result;
2309 } else {
2310 /* The buffer wraps around, or they're switching to another buffer. */
2311 lz4sd->extDictSize = lz4sd->prefixSize;
2312 lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize;
2313 result = LZ4_decompress_safe_forceExtDict(src, dst, compressedSize, dstCapacity,
2314 lz4sd->externalDict, lz4sd->extDictSize);
2315 if (result <= 0) return result;
2316 lz4sd->prefixSize = (size_t)result;
2317 lz4sd->prefixEnd = (BYTE *)dst + result;
2320 return result;
2323 LZ4_FORCE_O2_GCC_PPC64LE
2324 int LZ4_decompress_fast_continue(LZ4_streamDecode_t *LZ4_streamDecode, const char *src, char *dst, int originalSize) {
2325 LZ4_streamDecode_t_internal *lz4sd = &LZ4_streamDecode->internal_donotuse;
2326 int result;
2327 assert(originalSize >= 0);
2329 if (lz4sd->prefixSize == 0) {
2330 assert(lz4sd->extDictSize == 0);
2331 result = LZ4_decompress_fast(src, dst, originalSize);
2332 if (result <= 0) return result;
2333 lz4sd->prefixSize = (size_t)originalSize;
2334 lz4sd->prefixEnd = (BYTE *)dst + originalSize;
2335 } else if (lz4sd->prefixEnd == (BYTE *)dst) {
2336 if (lz4sd->prefixSize >= 64 KB - 1 || lz4sd->extDictSize == 0)
2337 result = LZ4_decompress_fast(src, dst, originalSize);
2338 else
2339 result = LZ4_decompress_fast_doubleDict(src, dst, originalSize,
2340 lz4sd->prefixSize, lz4sd->externalDict, lz4sd->extDictSize);
2341 if (result <= 0) return result;
2342 lz4sd->prefixSize += (size_t)originalSize;
2343 lz4sd->prefixEnd += originalSize;
2344 } else {
2345 lz4sd->extDictSize = lz4sd->prefixSize;
2346 lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize;
2347 result = LZ4_decompress_fast_extDict(src, dst, originalSize,
2348 lz4sd->externalDict, lz4sd->extDictSize);
2349 if (result <= 0) return result;
2350 lz4sd->prefixSize = (size_t)originalSize;
2351 lz4sd->prefixEnd = (BYTE *)dst + originalSize;
2354 return result;
2359 Advanced decoding functions :
2360 *_usingDict() :
2361 These decoding functions work the same as "_continue" ones,
2362 the dictionary must be explicitly provided within parameters
2365 int LZ4_decompress_safe_usingDict(const char *src, char *dst, int compressedSize, int dstCapacity, const char *dictStart, int dictSize) {
2366 if (dictSize == 0)
2367 return LZ4_decompress_safe(src, dst, compressedSize, dstCapacity);
2368 if (dictStart + dictSize == dst) {
2369 if (dictSize >= 64 KB - 1) {
2370 return LZ4_decompress_safe_withPrefix64k(src, dst, compressedSize, dstCapacity);
2372 assert(dictSize >= 0);
2373 return LZ4_decompress_safe_withSmallPrefix(src, dst, compressedSize, dstCapacity, (size_t)dictSize);
2375 assert(dictSize >= 0);
2376 return LZ4_decompress_safe_forceExtDict(src, dst, compressedSize, dstCapacity, dictStart, (size_t)dictSize);
2379 int LZ4_decompress_fast_usingDict(const char *src, char *dst, int originalSize, const char *dictStart, int dictSize) {
2380 if (dictSize == 0 || dictStart + dictSize == dst)
2381 return LZ4_decompress_fast(src, dst, originalSize);
2382 assert(dictSize >= 0);
2383 return LZ4_decompress_fast_extDict(src, dst, originalSize, dictStart, (size_t)dictSize);
2387 /*=*************************************************
2388 * Obsolete Functions
2389 ***************************************************/
2390 /* obsolete compression functions */
2391 int LZ4_compress_limitedOutput(const char *src, char *dst, int srcSize, int dstCapacity) {
2392 return LZ4_compress_default(src, dst, srcSize, dstCapacity);
2394 int LZ4_compress(const char *src, char *dst, int srcSize) {
2395 return LZ4_compress_default(src, dst, srcSize, LZ4_compressBound(srcSize));
2397 int LZ4_compress_limitedOutput_withState(void *state, const char *src, char *dst, int srcSize, int dstSize) {
2398 return LZ4_compress_fast_extState(state, src, dst, srcSize, dstSize, 1);
2400 int LZ4_compress_withState(void *state, const char *src, char *dst, int srcSize) {
2401 return LZ4_compress_fast_extState(state, src, dst, srcSize, LZ4_compressBound(srcSize), 1);
2403 int LZ4_compress_limitedOutput_continue(LZ4_stream_t *LZ4_stream, const char *src, char *dst, int srcSize, int dstCapacity) {
2404 return LZ4_compress_fast_continue(LZ4_stream, src, dst, srcSize, dstCapacity, 1);
2406 int LZ4_compress_continue(LZ4_stream_t *LZ4_stream, const char *src, char *dst, int srcSize) {
2407 return LZ4_compress_fast_continue(LZ4_stream, src, dst, srcSize, LZ4_compressBound(srcSize), 1);
2411 These decompression functions are deprecated and should no longer be used.
2412 They are only provided here for compatibility with older user programs.
2413 - LZ4_uncompress is totally equivalent to LZ4_decompress_fast
2414 - LZ4_uncompress_unknownOutputSize is totally equivalent to LZ4_decompress_safe
2416 int LZ4_uncompress(const char *src, char *dst, int outputSize) {
2417 return LZ4_decompress_fast(src, dst, outputSize);
2419 int LZ4_uncompress_unknownOutputSize(const char *src, char *dst, int isize, int dstCapacity) {
2420 return LZ4_decompress_safe(src, dst, isize, dstCapacity);
2423 /* Obsolete Streaming functions */
2425 int LZ4_sizeofStreamState(void) { return LZ4_STREAMSIZE; }
2427 int LZ4_resetStreamState(void *state, char *inputBuffer) {
2428 (void)inputBuffer;
2429 LZ4_resetStream((LZ4_stream_t *)state);
2430 return 0;
2433 void *LZ4_create(char *inputBuffer) {
2434 (void)inputBuffer;
2435 return LZ4_createStream();
2438 char *LZ4_slideInputBuffer(void *state) {
2439 /* avoid const char * -> char * conversion warning */
2440 return (char *)(uptrval)((LZ4_stream_t *)state)->internal_donotuse.dictionary;
2443 #endif /* LZ4_COMMONDEFS_ONLY */