Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | LZ4 - Fast LZ compression algorithm |
3 | | Copyright (C) 2011-2020, Yann Collet. |
4 | | |
5 | | BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) |
6 | | |
7 | | Redistribution and use in source and binary forms, with or without |
8 | | modification, are permitted provided that the following conditions are |
9 | | met: |
10 | | |
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. |
17 | | |
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. |
29 | | |
30 | | You can contact the author at : |
31 | | - LZ4 homepage : http://www.lz4.org |
32 | | - LZ4 source repository : https://github.com/lz4/lz4 |
33 | | */ |
34 | | |
35 | | /*-************************************ |
36 | | * Tuning parameters |
37 | | **************************************/ |
38 | | /* |
39 | | * LZ4_HEAPMODE : |
40 | | * Select how stateless compression functions like `LZ4_compress_default()` |
41 | | * allocate memory for their hash table, |
42 | | * in memory stack (0:default, fastest), or in memory heap (1:requires malloc()). |
43 | | */ |
44 | | #ifndef LZ4_HEAPMODE |
45 | | # define LZ4_HEAPMODE 0 |
46 | | #endif |
47 | | |
48 | | /* |
49 | | * LZ4_ACCELERATION_DEFAULT : |
50 | | * Select "acceleration" for LZ4_compress_fast() when parameter value <= 0 |
51 | | */ |
52 | 0 | #define LZ4_ACCELERATION_DEFAULT 1 |
53 | | /* |
54 | | * LZ4_ACCELERATION_MAX : |
55 | | * Any "acceleration" value higher than this threshold |
56 | | * get treated as LZ4_ACCELERATION_MAX instead (fix #876) |
57 | | */ |
58 | 0 | #define LZ4_ACCELERATION_MAX 65537 |
59 | | |
60 | | |
61 | | /*-************************************ |
62 | | * CPU Feature Detection |
63 | | **************************************/ |
64 | | /* LZ4_FORCE_MEMORY_ACCESS |
65 | | * By default, access to unaligned memory is controlled by `memcpy()`, which is safe and portable. |
66 | | * Unfortunately, on some target/compiler combinations, the generated assembly is sub-optimal. |
67 | | * The below switch allow to select different access method for improved performance. |
68 | | * Method 0 (default) : use `memcpy()`. Safe and portable. |
69 | | * Method 1 : `__packed` statement. It depends on compiler extension (ie, not portable). |
70 | | * This method is safe if your compiler supports it, and *generally* as fast or faster than `memcpy`. |
71 | | * Method 2 : direct access. This method is portable but violate C standard. |
72 | | * It can generate buggy code on targets which assembly generation depends on alignment. |
73 | | * But in some circumstances, it's the only known way to get the most performance (ie GCC + ARMv6) |
74 | | * See https://fastcompression.blogspot.fr/2015/08/accessing-unaligned-memory.html for details. |
75 | | * Prefer these methods in priority order (0 > 1 > 2) |
76 | | */ |
77 | | #ifndef LZ4_FORCE_MEMORY_ACCESS /* can be defined externally */ |
78 | | # if defined(__GNUC__) && \ |
79 | | ( defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) \ |
80 | | || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_6T2__) ) |
81 | | # define LZ4_FORCE_MEMORY_ACCESS 2 |
82 | | # elif (defined(__INTEL_COMPILER) && !defined(_WIN32)) || defined(__GNUC__) |
83 | | # define LZ4_FORCE_MEMORY_ACCESS 1 |
84 | | # endif |
85 | | #endif |
86 | | |
87 | | /* |
88 | | * LZ4_FORCE_SW_BITCOUNT |
89 | | * Define this parameter if your target system or compiler does not support hardware bit count |
90 | | */ |
91 | | #if defined(_MSC_VER) && defined(_WIN32_WCE) /* Visual Studio for WinCE doesn't support Hardware bit count */ |
92 | | # undef LZ4_FORCE_SW_BITCOUNT /* avoid double def */ |
93 | | # define LZ4_FORCE_SW_BITCOUNT |
94 | | #endif |
95 | | |
96 | | |
97 | | |
98 | | /*-************************************ |
99 | | * Dependency |
100 | | **************************************/ |
101 | | /* |
102 | | * LZ4_SRC_INCLUDED: |
103 | | * Amalgamation flag, whether lz4.c is included |
104 | | */ |
105 | | #ifndef LZ4_SRC_INCLUDED |
106 | | # define LZ4_SRC_INCLUDED 1 |
107 | | #endif |
108 | | |
109 | | #ifndef LZ4_STATIC_LINKING_ONLY |
110 | | #define LZ4_STATIC_LINKING_ONLY |
111 | | #endif |
112 | | |
113 | | #ifndef LZ4_DISABLE_DEPRECATE_WARNINGS |
114 | | #define LZ4_DISABLE_DEPRECATE_WARNINGS /* due to LZ4_decompress_safe_withPrefix64k */ |
115 | | #endif |
116 | | |
117 | | #define LZ4_STATIC_LINKING_ONLY /* LZ4_DISTANCE_MAX */ |
118 | | #include "lz4.h" |
119 | | /* see also "memory routines" below */ |
120 | | |
121 | | |
122 | | /*-************************************ |
123 | | * Compiler Options |
124 | | **************************************/ |
125 | | #if defined(_MSC_VER) && (_MSC_VER >= 1400) /* Visual Studio 2005+ */ |
126 | | # include <intrin.h> /* only present in VS2005+ */ |
127 | | # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ |
128 | | # pragma warning(disable : 6237) /* disable: C6237: conditional expression is always 0 */ |
129 | | #endif /* _MSC_VER */ |
130 | | |
131 | | #ifndef LZ4_FORCE_INLINE |
132 | | # ifdef _MSC_VER /* Visual Studio */ |
133 | | # define LZ4_FORCE_INLINE static __forceinline |
134 | | # else |
135 | | # if defined (__cplusplus) || defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */ |
136 | | # ifdef __GNUC__ |
137 | | # define LZ4_FORCE_INLINE static inline __attribute__((always_inline)) |
138 | | # else |
139 | | # define LZ4_FORCE_INLINE static inline |
140 | | # endif |
141 | | # else |
142 | | # define LZ4_FORCE_INLINE static |
143 | | # endif /* __STDC_VERSION__ */ |
144 | | # endif /* _MSC_VER */ |
145 | | #endif /* LZ4_FORCE_INLINE */ |
146 | | |
147 | | /* LZ4_FORCE_O2 and LZ4_FORCE_INLINE |
148 | | * gcc on ppc64le generates an unrolled SIMDized loop for LZ4_wildCopy8, |
149 | | * together with a simple 8-byte copy loop as a fall-back path. |
150 | | * However, this optimization hurts the decompression speed by >30%, |
151 | | * because the execution does not go to the optimized loop |
152 | | * for typical compressible data, and all of the preamble checks |
153 | | * before going to the fall-back path become useless overhead. |
154 | | * This optimization happens only with the -O3 flag, and -O2 generates |
155 | | * a simple 8-byte copy loop. |
156 | | * With gcc on ppc64le, all of the LZ4_decompress_* and LZ4_wildCopy8 |
157 | | * functions are annotated with __attribute__((optimize("O2"))), |
158 | | * and also LZ4_wildCopy8 is forcibly inlined, so that the O2 attribute |
159 | | * of LZ4_wildCopy8 does not affect the compression speed. |
160 | | */ |
161 | | #if defined(__PPC64__) && defined(__LITTLE_ENDIAN__) && defined(__GNUC__) && !defined(__clang__) |
162 | | # define LZ4_FORCE_O2 __attribute__((optimize("O2"))) |
163 | | # undef LZ4_FORCE_INLINE |
164 | | # define LZ4_FORCE_INLINE static __inline __attribute__((optimize("O2"),always_inline)) |
165 | | #else |
166 | | # define LZ4_FORCE_O2 |
167 | | #endif |
168 | | |
169 | | #if (defined(__GNUC__) && (__GNUC__ >= 3)) || (defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 800)) || defined(__clang__) |
170 | 508M | # define expect(expr,value) (__builtin_expect ((expr),(value)) ) |
171 | | #else |
172 | | # define expect(expr,value) (expr) |
173 | | #endif |
174 | | |
175 | | #ifndef likely |
176 | 1.98M | #define likely(expr) expect((expr) != 0, 1) |
177 | | #endif |
178 | | #ifndef unlikely |
179 | 322k | #define unlikely(expr) expect((expr) != 0, 0) |
180 | | #endif |
181 | | |
182 | | /* Should the alignment test prove unreliable, for some reason, |
183 | | * it can be disabled by setting LZ4_ALIGN_TEST to 0 */ |
184 | | #ifndef LZ4_ALIGN_TEST /* can be externally provided */ |
185 | | # define LZ4_ALIGN_TEST 1 |
186 | | #endif |
187 | | |
188 | | |
189 | | /*-************************************ |
190 | | * Memory routines |
191 | | **************************************/ |
192 | | |
193 | | /*! LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION : |
194 | | * Disable relatively high-level LZ4/HC functions that use dynamic memory |
195 | | * allocation functions (malloc(), calloc(), free()). |
196 | | * |
197 | | * Note that this is a compile-time switch. And since it disables |
198 | | * public/stable LZ4 v1 API functions, we don't recommend using this |
199 | | * symbol to generate a library for distribution. |
200 | | * |
201 | | * The following public functions are removed when this symbol is defined. |
202 | | * - lz4 : LZ4_createStream, LZ4_freeStream, |
203 | | * LZ4_createStreamDecode, LZ4_freeStreamDecode, LZ4_create (deprecated) |
204 | | * - lz4hc : LZ4_createStreamHC, LZ4_freeStreamHC, |
205 | | * LZ4_createHC (deprecated), LZ4_freeHC (deprecated) |
206 | | * - lz4frame, lz4file : All LZ4F_* functions |
207 | | */ |
208 | | #if defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION) |
209 | | # define ALLOC(s) lz4_error_memory_allocation_is_disabled |
210 | | # define ALLOC_AND_ZERO(s) lz4_error_memory_allocation_is_disabled |
211 | | # define FREEMEM(p) lz4_error_memory_allocation_is_disabled |
212 | | #elif defined(LZ4_USER_MEMORY_FUNCTIONS) |
213 | | /* memory management functions can be customized by user project. |
214 | | * Below functions must exist somewhere in the Project |
215 | | * and be available at link time */ |
216 | | void* LZ4_malloc(size_t s); |
217 | | void* LZ4_calloc(size_t n, size_t s); |
218 | | void LZ4_free(void* p); |
219 | | # define ALLOC(s) LZ4_malloc(s) |
220 | | # define ALLOC_AND_ZERO(s) LZ4_calloc(1,s) |
221 | | # define FREEMEM(p) LZ4_free(p) |
222 | | #else |
223 | | # include <stdlib.h> /* malloc, calloc, free */ |
224 | 16.2k | # define ALLOC(s) malloc(s) |
225 | 0 | # define ALLOC_AND_ZERO(s) calloc(1,s) |
226 | 16.2k | # define FREEMEM(p) free(p) |
227 | | #endif |
228 | | |
229 | | #if ! LZ4_FREESTANDING |
230 | | # include <string.h> /* memset, memcpy */ |
231 | | #endif |
232 | | #if !defined(LZ4_memset) |
233 | 26.6k | # define LZ4_memset(p,v,s) memset((p),(v),(s)) |
234 | | #endif |
235 | 26.6k | #define MEM_INIT(p,v,s) LZ4_memset((p),(v),(s)) |
236 | | |
237 | | |
238 | | /*-************************************ |
239 | | * Common Constants |
240 | | **************************************/ |
241 | 750M | #define MINMATCH 4 |
242 | | |
243 | 5.83k | #define WILDCOPYLENGTH 8 |
244 | 227k | #define LASTLITERALS 5 /* see ../doc/lz4_Block_format.md#parsing-restrictions */ |
245 | 40.0k | #define MFLIMIT 12 /* see ../doc/lz4_Block_format.md#parsing-restrictions */ |
246 | 0 | #define MATCH_SAFEGUARD_DISTANCE ((2*WILDCOPYLENGTH) - MINMATCH) /* ensure it's possible to write 2 x wildcopyLength without overflowing output buffer */ |
247 | 50.2k | #define FASTLOOP_SAFE_DISTANCE 64 |
248 | | static const int LZ4_minLength = (MFLIMIT+1); |
249 | | |
250 | 25.5k | #define KB *(1 <<10) |
251 | | #define MB *(1 <<20) |
252 | 15.3k | #define GB *(1U<<30) |
253 | | |
254 | 0 | #define LZ4_DISTANCE_ABSOLUTE_MAX 65535 |
255 | | #if (LZ4_DISTANCE_MAX > LZ4_DISTANCE_ABSOLUTE_MAX) /* max supported by LZ4 format */ |
256 | | # error "LZ4_DISTANCE_MAX is too big : must be <= 65535" |
257 | | #endif |
258 | | |
259 | 70.8M | #define ML_BITS 4 |
260 | 46.3M | #define ML_MASK ((1U<<ML_BITS)-1) |
261 | 24.4M | #define RUN_BITS (8-ML_BITS) |
262 | 24.4M | #define RUN_MASK ((1U<<RUN_BITS)-1) |
263 | | |
264 | | |
265 | | /*-************************************ |
266 | | * Error detection |
267 | | **************************************/ |
268 | | #if defined(LZ4_DEBUG) && (LZ4_DEBUG>=1) |
269 | | # include <assert.h> |
270 | | #else |
271 | | # ifndef assert |
272 | | # define assert(condition) ((void)0) |
273 | | # endif |
274 | | #endif |
275 | | |
276 | 104k | #define LZ4_STATIC_ASSERT(c) { enum { LZ4_static_assert = 1/(int)(!!(c)) }; } /* use after variable declarations */ |
277 | | |
278 | | #if defined(LZ4_DEBUG) && (LZ4_DEBUG>=2) |
279 | | # include <stdio.h> |
280 | | static int g_debuglog_enable = 1; |
281 | | # define DEBUGLOG(l, ...) { \ |
282 | | if ((g_debuglog_enable) && (l<=LZ4_DEBUG)) { \ |
283 | | fprintf(stderr, __FILE__ " %i: ", __LINE__); \ |
284 | | fprintf(stderr, __VA_ARGS__); \ |
285 | | fprintf(stderr, " \n"); \ |
286 | | } } |
287 | | #else |
288 | 449M | # define DEBUGLOG(l, ...) {} /* disabled */ |
289 | | #endif |
290 | | |
291 | | static int LZ4_isAligned(const void* ptr, size_t alignment) |
292 | 23.8k | { |
293 | 23.8k | return ((size_t)ptr & (alignment -1)) == 0; |
294 | 23.8k | } Unexecuted instantiation: lz4.c:LZ4_isAligned Line | Count | Source | 292 | 23.8k | { | 293 | 23.8k | return ((size_t)ptr & (alignment -1)) == 0; | 294 | 23.8k | } |
|
295 | | |
296 | | |
297 | | /*-************************************ |
298 | | * Types |
299 | | **************************************/ |
300 | | #include <limits.h> |
301 | | #if defined(__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) |
302 | | # include <stdint.h> |
303 | | typedef uint8_t BYTE; |
304 | | typedef uint16_t U16; |
305 | | typedef uint32_t U32; |
306 | | typedef int32_t S32; |
307 | | typedef uint64_t U64; |
308 | | typedef uintptr_t uptrval; |
309 | | #else |
310 | | # if UINT_MAX != 4294967295UL |
311 | | # error "LZ4 code (when not C++ or C99) assumes that sizeof(int) == 4" |
312 | | # endif |
313 | | typedef unsigned char BYTE; |
314 | | typedef unsigned short U16; |
315 | | typedef unsigned int U32; |
316 | | typedef signed int S32; |
317 | | typedef unsigned long long U64; |
318 | | typedef size_t uptrval; /* generally true, except OpenVMS-64 */ |
319 | | #endif |
320 | | |
321 | | #if defined(__x86_64__) |
322 | | typedef U64 reg_t; /* 64-bits in x32 mode */ |
323 | | #else |
324 | | typedef size_t reg_t; /* 32-bits in x32 mode */ |
325 | | #endif |
326 | | |
327 | | typedef enum { |
328 | | notLimited = 0, |
329 | | limitedOutput = 1, |
330 | | fillOutput = 2 |
331 | | } limitedOutput_directive; |
332 | | |
333 | | |
334 | | /*-************************************ |
335 | | * Reading and writing into memory |
336 | | **************************************/ |
337 | | |
338 | | /** |
339 | | * LZ4 relies on memcpy with a constant size being inlined. In freestanding |
340 | | * environments, the compiler can't assume the implementation of memcpy() is |
341 | | * standard compliant, so it can't apply its specialized memcpy() inlining |
342 | | * logic. When possible, use __builtin_memcpy() to tell the compiler to analyze |
343 | | * memcpy() as if it were standard compliant, so it can inline it in freestanding |
344 | | * environments. This is needed when decompressing the Linux Kernel, for example. |
345 | | */ |
346 | | #if !defined(LZ4_memcpy) |
347 | | # if defined(__GNUC__) && (__GNUC__ >= 4) |
348 | 3.55M | # define LZ4_memcpy(dst, src, size) __builtin_memcpy(dst, src, size) |
349 | | # else |
350 | | # define LZ4_memcpy(dst, src, size) memcpy(dst, src, size) |
351 | | # endif |
352 | | #endif |
353 | | |
354 | | #if !defined(LZ4_memmove) |
355 | | # if defined(__GNUC__) && (__GNUC__ >= 4) |
356 | 10.1k | # define LZ4_memmove __builtin_memmove |
357 | | # else |
358 | | # define LZ4_memmove memmove |
359 | | # endif |
360 | | #endif |
361 | | |
362 | | static unsigned LZ4_isLittleEndian(void) |
363 | 4.17M | { |
364 | 4.17M | const union { U32 u; BYTE c[4]; } one = { 1 }; /* don't use static : performance detrimental */ |
365 | 4.17M | return one.c[0]; |
366 | 4.17M | } Line | Count | Source | 363 | 56.6k | { | 364 | 56.6k | const union { U32 u; BYTE c[4]; } one = { 1 }; /* don't use static : performance detrimental */ | 365 | 56.6k | return one.c[0]; | 366 | 56.6k | } |
lz4hc.c:LZ4_isLittleEndian Line | Count | Source | 363 | 4.11M | { | 364 | 4.11M | const union { U32 u; BYTE c[4]; } one = { 1 }; /* don't use static : performance detrimental */ | 365 | 4.11M | return one.c[0]; | 366 | 4.11M | } |
|
367 | | |
368 | | |
369 | | #if defined(LZ4_FORCE_MEMORY_ACCESS) && (LZ4_FORCE_MEMORY_ACCESS==2) |
370 | | /* lie to the compiler about data alignment; use with caution */ |
371 | | |
372 | | static U16 LZ4_read16(const void* memPtr) { return *(const U16*) memPtr; } |
373 | | static U32 LZ4_read32(const void* memPtr) { return *(const U32*) memPtr; } |
374 | | static reg_t LZ4_read_ARCH(const void* memPtr) { return *(const reg_t*) memPtr; } |
375 | | |
376 | | static void LZ4_write16(void* memPtr, U16 value) { *(U16*)memPtr = value; } |
377 | | static void LZ4_write32(void* memPtr, U32 value) { *(U32*)memPtr = value; } |
378 | | |
379 | | #elif defined(LZ4_FORCE_MEMORY_ACCESS) && (LZ4_FORCE_MEMORY_ACCESS==1) |
380 | | |
381 | | /* __pack instructions are safer, but compiler specific, hence potentially problematic for some compilers */ |
382 | | /* currently only defined for gcc and icc */ |
383 | | typedef struct { U16 u16; } __attribute__((packed)) LZ4_unalign16; |
384 | | typedef struct { U32 u32; } __attribute__((packed)) LZ4_unalign32; |
385 | | typedef struct { reg_t uArch; } __attribute__((packed)) LZ4_unalignST; |
386 | | |
387 | 643M | static U16 LZ4_read16(const void* ptr) { return ((const LZ4_unalign16*)ptr)->u16; } Line | Count | Source | 387 | 56.6k | static U16 LZ4_read16(const void* ptr) { return ((const LZ4_unalign16*)ptr)->u16; } |
Line | Count | Source | 387 | 642M | static U16 LZ4_read16(const void* ptr) { return ((const LZ4_unalign16*)ptr)->u16; } |
|
388 | 751M | static U32 LZ4_read32(const void* ptr) { return ((const LZ4_unalign32*)ptr)->u32; } Unexecuted instantiation: lz4.c:LZ4_read32 Line | Count | Source | 388 | 751M | static U32 LZ4_read32(const void* ptr) { return ((const LZ4_unalign32*)ptr)->u32; } |
|
389 | 749M | static reg_t LZ4_read_ARCH(const void* ptr) { return ((const LZ4_unalignST*)ptr)->uArch; } Unexecuted instantiation: lz4.c:LZ4_read_ARCH Line | Count | Source | 389 | 749M | static reg_t LZ4_read_ARCH(const void* ptr) { return ((const LZ4_unalignST*)ptr)->uArch; } |
|
390 | | |
391 | 73.7k | static void LZ4_write16(void* memPtr, U16 value) { ((LZ4_unalign16*)memPtr)->u16 = value; } Unexecuted instantiation: lz4.c:LZ4_write16 Line | Count | Source | 391 | 73.7k | static void LZ4_write16(void* memPtr, U16 value) { ((LZ4_unalign16*)memPtr)->u16 = value; } |
|
392 | 12.9k | static void LZ4_write32(void* memPtr, U32 value) { ((LZ4_unalign32*)memPtr)->u32 = value; } Line | Count | Source | 392 | 12.9k | static void LZ4_write32(void* memPtr, U32 value) { ((LZ4_unalign32*)memPtr)->u32 = value; } |
Unexecuted instantiation: lz4hc.c:LZ4_write32 |
393 | | |
394 | | #else /* safe and portable access using memcpy() */ |
395 | | |
396 | | static U16 LZ4_read16(const void* memPtr) |
397 | | { |
398 | | U16 val; LZ4_memcpy(&val, memPtr, sizeof(val)); return val; |
399 | | } |
400 | | |
401 | | static U32 LZ4_read32(const void* memPtr) |
402 | | { |
403 | | U32 val; LZ4_memcpy(&val, memPtr, sizeof(val)); return val; |
404 | | } |
405 | | |
406 | | static reg_t LZ4_read_ARCH(const void* memPtr) |
407 | | { |
408 | | reg_t val; LZ4_memcpy(&val, memPtr, sizeof(val)); return val; |
409 | | } |
410 | | |
411 | | static void LZ4_write16(void* memPtr, U16 value) |
412 | | { |
413 | | LZ4_memcpy(memPtr, &value, sizeof(value)); |
414 | | } |
415 | | |
416 | | static void LZ4_write32(void* memPtr, U32 value) |
417 | | { |
418 | | LZ4_memcpy(memPtr, &value, sizeof(value)); |
419 | | } |
420 | | |
421 | | #endif /* LZ4_FORCE_MEMORY_ACCESS */ |
422 | | |
423 | | |
424 | | static U16 LZ4_readLE16(const void* memPtr) |
425 | 56.6k | { |
426 | 56.6k | if (LZ4_isLittleEndian()) { |
427 | 56.6k | return LZ4_read16(memPtr); |
428 | 56.6k | } else { |
429 | 0 | const BYTE* p = (const BYTE*)memPtr; |
430 | 0 | return (U16)((U16)p[0] + (p[1]<<8)); |
431 | 0 | } |
432 | 56.6k | } Line | Count | Source | 425 | 56.6k | { | 426 | 56.6k | if (LZ4_isLittleEndian()) { | 427 | 56.6k | return LZ4_read16(memPtr); | 428 | 56.6k | } else { | 429 | 0 | const BYTE* p = (const BYTE*)memPtr; | 430 | 0 | return (U16)((U16)p[0] + (p[1]<<8)); | 431 | 0 | } | 432 | 56.6k | } |
Unexecuted instantiation: lz4hc.c:LZ4_readLE16 |
433 | | |
434 | | static void LZ4_writeLE16(void* memPtr, U16 value) |
435 | 73.7k | { |
436 | 73.7k | if (LZ4_isLittleEndian()) { |
437 | 73.7k | LZ4_write16(memPtr, value); |
438 | 73.7k | } else { |
439 | 0 | BYTE* p = (BYTE*)memPtr; |
440 | 0 | p[0] = (BYTE) value; |
441 | 0 | p[1] = (BYTE)(value>>8); |
442 | 0 | } |
443 | 73.7k | } Unexecuted instantiation: lz4.c:LZ4_writeLE16 Line | Count | Source | 435 | 73.7k | { | 436 | 73.7k | if (LZ4_isLittleEndian()) { | 437 | 73.7k | LZ4_write16(memPtr, value); | 438 | 73.7k | } else { | 439 | 0 | BYTE* p = (BYTE*)memPtr; | 440 | 0 | p[0] = (BYTE) value; | 441 | 0 | p[1] = (BYTE)(value>>8); | 442 | 0 | } | 443 | 73.7k | } |
|
444 | | |
445 | | /* customized variant of memcpy, which can overwrite up to 8 bytes beyond dstEnd */ |
446 | | LZ4_FORCE_INLINE |
447 | | void LZ4_wildCopy8(void* dstPtr, const void* srcPtr, void* dstEnd) |
448 | 94.4k | { |
449 | 94.4k | BYTE* d = (BYTE*)dstPtr; |
450 | 94.4k | const BYTE* s = (const BYTE*)srcPtr; |
451 | 94.4k | BYTE* const e = (BYTE*)dstEnd; |
452 | | |
453 | 1.22M | do { LZ4_memcpy(d,s,8); d+=8; s+=8; } while (d<e); |
454 | 94.4k | } Line | Count | Source | 448 | 20.6k | { | 449 | 20.6k | BYTE* d = (BYTE*)dstPtr; | 450 | 20.6k | const BYTE* s = (const BYTE*)srcPtr; | 451 | 20.6k | BYTE* const e = (BYTE*)dstEnd; | 452 | | | 453 | 1.14M | do { LZ4_memcpy(d,s,8); d+=8; s+=8; } while (d<e); | 454 | 20.6k | } |
Line | Count | Source | 448 | 73.7k | { | 449 | 73.7k | BYTE* d = (BYTE*)dstPtr; | 450 | 73.7k | const BYTE* s = (const BYTE*)srcPtr; | 451 | 73.7k | BYTE* const e = (BYTE*)dstEnd; | 452 | | | 453 | 81.1k | do { LZ4_memcpy(d,s,8); d+=8; s+=8; } while (d<e); | 454 | 73.7k | } |
|
455 | | |
456 | | static const unsigned inc32table[8] = {0, 1, 2, 1, 0, 4, 4, 4}; |
457 | | static const int dec64table[8] = {0, 0, 0, -1, -4, 1, 2, 3}; |
458 | | |
459 | | |
460 | | #ifndef LZ4_FAST_DEC_LOOP |
461 | | # if defined __i386__ || defined _M_IX86 || defined __x86_64__ || defined _M_X64 |
462 | | # define LZ4_FAST_DEC_LOOP 1 |
463 | | # elif defined(__aarch64__) && defined(__APPLE__) |
464 | | # define LZ4_FAST_DEC_LOOP 1 |
465 | | # elif defined(__aarch64__) && !defined(__clang__) |
466 | | /* On non-Apple aarch64, we disable this optimization for clang because |
467 | | * on certain mobile chipsets, performance is reduced with clang. For |
468 | | * more information refer to https://github.com/lz4/lz4/pull/707 */ |
469 | | # define LZ4_FAST_DEC_LOOP 1 |
470 | | # else |
471 | | # define LZ4_FAST_DEC_LOOP 0 |
472 | | # endif |
473 | | #endif |
474 | | |
475 | | #if LZ4_FAST_DEC_LOOP |
476 | | |
477 | | LZ4_FORCE_INLINE void |
478 | | LZ4_memcpy_using_offset_base(BYTE* dstPtr, const BYTE* srcPtr, BYTE* dstEnd, const size_t offset) |
479 | 2.93k | { |
480 | 2.93k | assert(srcPtr + offset == dstPtr); |
481 | 2.93k | if (offset < 8) { |
482 | 2.07k | LZ4_write32(dstPtr, 0); /* silence an msan warning when offset==0 */ |
483 | 2.07k | dstPtr[0] = srcPtr[0]; |
484 | 2.07k | dstPtr[1] = srcPtr[1]; |
485 | 2.07k | dstPtr[2] = srcPtr[2]; |
486 | 2.07k | dstPtr[3] = srcPtr[3]; |
487 | 2.07k | srcPtr += inc32table[offset]; |
488 | 2.07k | LZ4_memcpy(dstPtr+4, srcPtr, 4); |
489 | 2.07k | srcPtr -= dec64table[offset]; |
490 | 2.07k | dstPtr += 8; |
491 | 2.07k | } else { |
492 | 855 | LZ4_memcpy(dstPtr, srcPtr, 8); |
493 | 855 | dstPtr += 8; |
494 | 855 | srcPtr += 8; |
495 | 855 | } |
496 | | |
497 | 2.93k | LZ4_wildCopy8(dstPtr, srcPtr, dstEnd); |
498 | 2.93k | } lz4.c:LZ4_memcpy_using_offset_base Line | Count | Source | 479 | 2.93k | { | 480 | 2.93k | assert(srcPtr + offset == dstPtr); | 481 | 2.93k | if (offset < 8) { | 482 | 2.07k | LZ4_write32(dstPtr, 0); /* silence an msan warning when offset==0 */ | 483 | 2.07k | dstPtr[0] = srcPtr[0]; | 484 | 2.07k | dstPtr[1] = srcPtr[1]; | 485 | 2.07k | dstPtr[2] = srcPtr[2]; | 486 | 2.07k | dstPtr[3] = srcPtr[3]; | 487 | 2.07k | srcPtr += inc32table[offset]; | 488 | 2.07k | LZ4_memcpy(dstPtr+4, srcPtr, 4); | 489 | 2.07k | srcPtr -= dec64table[offset]; | 490 | 2.07k | dstPtr += 8; | 491 | 2.07k | } else { | 492 | 855 | LZ4_memcpy(dstPtr, srcPtr, 8); | 493 | 855 | dstPtr += 8; | 494 | 855 | srcPtr += 8; | 495 | 855 | } | 496 | | | 497 | 2.93k | LZ4_wildCopy8(dstPtr, srcPtr, dstEnd); | 498 | 2.93k | } |
Unexecuted instantiation: lz4hc.c:LZ4_memcpy_using_offset_base |
499 | | |
500 | | /* customized variant of memcpy, which can overwrite up to 32 bytes beyond dstEnd |
501 | | * this version copies two times 16 bytes (instead of one time 32 bytes) |
502 | | * because it must be compatible with offsets >= 16. */ |
503 | | LZ4_FORCE_INLINE void |
504 | | LZ4_wildCopy32(void* dstPtr, const void* srcPtr, void* dstEnd) |
505 | 14.5k | { |
506 | 14.5k | BYTE* d = (BYTE*)dstPtr; |
507 | 14.5k | const BYTE* s = (const BYTE*)srcPtr; |
508 | 14.5k | BYTE* const e = (BYTE*)dstEnd; |
509 | | |
510 | 306k | do { LZ4_memcpy(d,s,16); LZ4_memcpy(d+16,s+16,16); d+=32; s+=32; } while (d<e); |
511 | 14.5k | } Line | Count | Source | 505 | 14.5k | { | 506 | 14.5k | BYTE* d = (BYTE*)dstPtr; | 507 | 14.5k | const BYTE* s = (const BYTE*)srcPtr; | 508 | 14.5k | BYTE* const e = (BYTE*)dstEnd; | 509 | | | 510 | 306k | do { LZ4_memcpy(d,s,16); LZ4_memcpy(d+16,s+16,16); d+=32; s+=32; } while (d<e); | 511 | 14.5k | } |
Unexecuted instantiation: lz4hc.c:LZ4_wildCopy32 |
512 | | |
513 | | /* LZ4_memcpy_using_offset() presumes : |
514 | | * - dstEnd >= dstPtr + MINMATCH |
515 | | * - there is at least 8 bytes available to write after dstEnd */ |
516 | | LZ4_FORCE_INLINE void |
517 | | LZ4_memcpy_using_offset(BYTE* dstPtr, const BYTE* srcPtr, BYTE* dstEnd, const size_t offset) |
518 | 14.8k | { |
519 | 14.8k | BYTE v[8]; |
520 | | |
521 | 14.8k | assert(dstEnd >= dstPtr + MINMATCH); |
522 | | |
523 | 14.8k | switch(offset) { |
524 | 11.2k | case 1: |
525 | 11.2k | MEM_INIT(v, *srcPtr, 8); |
526 | 11.2k | break; |
527 | 291 | case 2: |
528 | 291 | LZ4_memcpy(v, srcPtr, 2); |
529 | 291 | LZ4_memcpy(&v[2], srcPtr, 2); |
530 | | #if defined(_MSC_VER) && (_MSC_VER <= 1933) /* MSVC 2022 ver 17.3 or earlier */ |
531 | | # pragma warning(push) |
532 | | # pragma warning(disable : 6385) /* warning C6385: Reading invalid data from 'v'. */ |
533 | | #endif |
534 | 291 | LZ4_memcpy(&v[4], v, 4); |
535 | | #if defined(_MSC_VER) && (_MSC_VER <= 1933) /* MSVC 2022 ver 17.3 or earlier */ |
536 | | # pragma warning(pop) |
537 | | #endif |
538 | 291 | break; |
539 | 414 | case 4: |
540 | 414 | LZ4_memcpy(v, srcPtr, 4); |
541 | 414 | LZ4_memcpy(&v[4], srcPtr, 4); |
542 | 414 | break; |
543 | 2.93k | default: |
544 | 2.93k | LZ4_memcpy_using_offset_base(dstPtr, srcPtr, dstEnd, offset); |
545 | 2.93k | return; |
546 | 14.8k | } |
547 | | |
548 | 11.9k | LZ4_memcpy(dstPtr, v, 8); |
549 | 11.9k | dstPtr += 8; |
550 | 1.59M | while (dstPtr < dstEnd) { |
551 | 1.58M | LZ4_memcpy(dstPtr, v, 8); |
552 | 1.58M | dstPtr += 8; |
553 | 1.58M | } |
554 | 11.9k | } lz4.c:LZ4_memcpy_using_offset Line | Count | Source | 518 | 14.8k | { | 519 | 14.8k | BYTE v[8]; | 520 | | | 521 | 14.8k | assert(dstEnd >= dstPtr + MINMATCH); | 522 | | | 523 | 14.8k | switch(offset) { | 524 | 11.2k | case 1: | 525 | 11.2k | MEM_INIT(v, *srcPtr, 8); | 526 | 11.2k | break; | 527 | 291 | case 2: | 528 | 291 | LZ4_memcpy(v, srcPtr, 2); | 529 | 291 | LZ4_memcpy(&v[2], srcPtr, 2); | 530 | | #if defined(_MSC_VER) && (_MSC_VER <= 1933) /* MSVC 2022 ver 17.3 or earlier */ | 531 | | # pragma warning(push) | 532 | | # pragma warning(disable : 6385) /* warning C6385: Reading invalid data from 'v'. */ | 533 | | #endif | 534 | 291 | LZ4_memcpy(&v[4], v, 4); | 535 | | #if defined(_MSC_VER) && (_MSC_VER <= 1933) /* MSVC 2022 ver 17.3 or earlier */ | 536 | | # pragma warning(pop) | 537 | | #endif | 538 | 291 | break; | 539 | 414 | case 4: | 540 | 414 | LZ4_memcpy(v, srcPtr, 4); | 541 | 414 | LZ4_memcpy(&v[4], srcPtr, 4); | 542 | 414 | break; | 543 | 2.93k | default: | 544 | 2.93k | LZ4_memcpy_using_offset_base(dstPtr, srcPtr, dstEnd, offset); | 545 | 2.93k | return; | 546 | 14.8k | } | 547 | | | 548 | 11.9k | LZ4_memcpy(dstPtr, v, 8); | 549 | 11.9k | dstPtr += 8; | 550 | 1.59M | while (dstPtr < dstEnd) { | 551 | 1.58M | LZ4_memcpy(dstPtr, v, 8); | 552 | 1.58M | dstPtr += 8; | 553 | 1.58M | } | 554 | 11.9k | } |
Unexecuted instantiation: lz4hc.c:LZ4_memcpy_using_offset |
555 | | #endif |
556 | | |
557 | | |
558 | | /*-************************************ |
559 | | * Common functions |
560 | | **************************************/ |
561 | | static unsigned LZ4_NbCommonBytes (reg_t val) |
562 | 4.02M | { |
563 | 4.02M | assert(val != 0); |
564 | 4.02M | if (LZ4_isLittleEndian()) { |
565 | 4.02M | if (sizeof(val) == 8) { |
566 | | # if defined(_MSC_VER) && (_MSC_VER >= 1800) && (defined(_M_AMD64) && !defined(_M_ARM64EC)) && !defined(LZ4_FORCE_SW_BITCOUNT) |
567 | | /*-************************************************************************************************* |
568 | | * ARM64EC is a Microsoft-designed ARM64 ABI compatible with AMD64 applications on ARM64 Windows 11. |
569 | | * The ARM64EC ABI does not support AVX/AVX2/AVX512 instructions, nor their relevant intrinsics |
570 | | * including _tzcnt_u64. Therefore, we need to neuter the _tzcnt_u64 code path for ARM64EC. |
571 | | ****************************************************************************************************/ |
572 | | # if defined(__clang__) && (__clang_major__ < 10) |
573 | | /* Avoid undefined clang-cl intrinsics issue. |
574 | | * See https://github.com/lz4/lz4/pull/1017 for details. */ |
575 | | return (unsigned)__builtin_ia32_tzcnt_u64(val) >> 3; |
576 | | # else |
577 | | /* x64 CPUS without BMI support interpret `TZCNT` as `REP BSF` */ |
578 | | return (unsigned)_tzcnt_u64(val) >> 3; |
579 | | # endif |
580 | | # elif defined(_MSC_VER) && defined(_WIN64) && !defined(LZ4_FORCE_SW_BITCOUNT) |
581 | | unsigned long r = 0; |
582 | | _BitScanForward64(&r, (U64)val); |
583 | | return (unsigned)r >> 3; |
584 | | # elif (defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 3) || \ |
585 | | ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))))) && \ |
586 | | !defined(LZ4_FORCE_SW_BITCOUNT) |
587 | 4.02M | return (unsigned)__builtin_ctzll((U64)val) >> 3; |
588 | | # else |
589 | | const U64 m = 0x0101010101010101ULL; |
590 | | val ^= val - 1; |
591 | | return (unsigned)(((U64)((val & (m - 1)) * m)) >> 56); |
592 | | # endif |
593 | 4.02M | } else /* 32 bits */ { |
594 | | # if defined(_MSC_VER) && (_MSC_VER >= 1400) && !defined(LZ4_FORCE_SW_BITCOUNT) |
595 | | unsigned long r; |
596 | | _BitScanForward(&r, (U32)val); |
597 | | return (unsigned)r >> 3; |
598 | | # elif (defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 3) || \ |
599 | | ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))))) && \ |
600 | | !defined(__TINYC__) && !defined(LZ4_FORCE_SW_BITCOUNT) |
601 | 0 | return (unsigned)__builtin_ctz((U32)val) >> 3; |
602 | | # else |
603 | | const U32 m = 0x01010101; |
604 | | return (unsigned)((((val - 1) ^ val) & (m - 1)) * m) >> 24; |
605 | | # endif |
606 | 0 | } |
607 | 4.02M | } else /* Big Endian CPU */ { |
608 | 0 | if (sizeof(val)==8) { |
609 | 0 | # if (defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 3) || \ |
610 | 0 | ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))))) && \ |
611 | 0 | !defined(__TINYC__) && !defined(LZ4_FORCE_SW_BITCOUNT) |
612 | 0 | return (unsigned)__builtin_clzll((U64)val) >> 3; |
613 | | # else |
614 | | #if 1 |
615 | | /* this method is probably faster, |
616 | | * but adds a 128 bytes lookup table */ |
617 | | static const unsigned char ctz7_tab[128] = { |
618 | | 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, |
619 | | 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, |
620 | | 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, |
621 | | 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, |
622 | | 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, |
623 | | 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, |
624 | | 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, |
625 | | 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, |
626 | | }; |
627 | | U64 const mask = 0x0101010101010101ULL; |
628 | | U64 const t = (((val >> 8) - mask) | val) & mask; |
629 | | return ctz7_tab[(t * 0x0080402010080402ULL) >> 57]; |
630 | | #else |
631 | | /* this method doesn't consume memory space like the previous one, |
632 | | * but it contains several branches, |
633 | | * that may end up slowing execution */ |
634 | | static const U32 by32 = sizeof(val)*4; /* 32 on 64 bits (goal), 16 on 32 bits. |
635 | | Just to avoid some static analyzer complaining about shift by 32 on 32-bits target. |
636 | | Note that this code path is never triggered in 32-bits mode. */ |
637 | | unsigned r; |
638 | | if (!(val>>by32)) { r=4; } else { r=0; val>>=by32; } |
639 | | if (!(val>>16)) { r+=2; val>>=8; } else { val>>=24; } |
640 | | r += (!val); |
641 | | return r; |
642 | | #endif |
643 | | # endif |
644 | 0 | } else /* 32 bits */ { |
645 | 0 | # if (defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 3) || \ |
646 | 0 | ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))))) && \ |
647 | 0 | !defined(LZ4_FORCE_SW_BITCOUNT) |
648 | 0 | return (unsigned)__builtin_clz((U32)val) >> 3; |
649 | | # else |
650 | | val >>= 8; |
651 | | val = ((((val + 0x00FFFF00) | 0x00FFFFFF) + val) | |
652 | | (val + 0x00FF0000)) >> 24; |
653 | | return (unsigned)val ^ 3; |
654 | | # endif |
655 | 0 | } |
656 | 0 | } |
657 | 4.02M | } Unexecuted instantiation: lz4.c:LZ4_NbCommonBytes lz4hc.c:LZ4_NbCommonBytes Line | Count | Source | 562 | 4.02M | { | 563 | 4.02M | assert(val != 0); | 564 | 4.02M | if (LZ4_isLittleEndian()) { | 565 | 4.02M | if (sizeof(val) == 8) { | 566 | | # if defined(_MSC_VER) && (_MSC_VER >= 1800) && (defined(_M_AMD64) && !defined(_M_ARM64EC)) && !defined(LZ4_FORCE_SW_BITCOUNT) | 567 | | /*-************************************************************************************************* | 568 | | * ARM64EC is a Microsoft-designed ARM64 ABI compatible with AMD64 applications on ARM64 Windows 11. | 569 | | * The ARM64EC ABI does not support AVX/AVX2/AVX512 instructions, nor their relevant intrinsics | 570 | | * including _tzcnt_u64. Therefore, we need to neuter the _tzcnt_u64 code path for ARM64EC. | 571 | | ****************************************************************************************************/ | 572 | | # if defined(__clang__) && (__clang_major__ < 10) | 573 | | /* Avoid undefined clang-cl intrinsics issue. | 574 | | * See https://github.com/lz4/lz4/pull/1017 for details. */ | 575 | | return (unsigned)__builtin_ia32_tzcnt_u64(val) >> 3; | 576 | | # else | 577 | | /* x64 CPUS without BMI support interpret `TZCNT` as `REP BSF` */ | 578 | | return (unsigned)_tzcnt_u64(val) >> 3; | 579 | | # endif | 580 | | # elif defined(_MSC_VER) && defined(_WIN64) && !defined(LZ4_FORCE_SW_BITCOUNT) | 581 | | unsigned long r = 0; | 582 | | _BitScanForward64(&r, (U64)val); | 583 | | return (unsigned)r >> 3; | 584 | | # elif (defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 3) || \ | 585 | | ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))))) && \ | 586 | | !defined(LZ4_FORCE_SW_BITCOUNT) | 587 | 4.02M | return (unsigned)__builtin_ctzll((U64)val) >> 3; | 588 | | # else | 589 | | const U64 m = 0x0101010101010101ULL; | 590 | | val ^= val - 1; | 591 | | return (unsigned)(((U64)((val & (m - 1)) * m)) >> 56); | 592 | | # endif | 593 | 4.02M | } else /* 32 bits */ { | 594 | | # if defined(_MSC_VER) && (_MSC_VER >= 1400) && !defined(LZ4_FORCE_SW_BITCOUNT) | 595 | | unsigned long r; | 596 | | _BitScanForward(&r, (U32)val); | 597 | | return (unsigned)r >> 3; | 598 | | # elif (defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 3) || \ | 599 | | ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))))) && \ | 600 | | !defined(__TINYC__) && !defined(LZ4_FORCE_SW_BITCOUNT) | 601 | 0 | return (unsigned)__builtin_ctz((U32)val) >> 3; | 602 | | # else | 603 | | const U32 m = 0x01010101; | 604 | | return (unsigned)((((val - 1) ^ val) & (m - 1)) * m) >> 24; | 605 | | # endif | 606 | 0 | } | 607 | 4.02M | } else /* Big Endian CPU */ { | 608 | 0 | if (sizeof(val)==8) { | 609 | 0 | # if (defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 3) || \ | 610 | 0 | ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))))) && \ | 611 | 0 | !defined(__TINYC__) && !defined(LZ4_FORCE_SW_BITCOUNT) | 612 | 0 | return (unsigned)__builtin_clzll((U64)val) >> 3; | 613 | | # else | 614 | | #if 1 | 615 | | /* this method is probably faster, | 616 | | * but adds a 128 bytes lookup table */ | 617 | | static const unsigned char ctz7_tab[128] = { | 618 | | 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, | 619 | | 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, | 620 | | 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, | 621 | | 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, | 622 | | 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, | 623 | | 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, | 624 | | 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, | 625 | | 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, | 626 | | }; | 627 | | U64 const mask = 0x0101010101010101ULL; | 628 | | U64 const t = (((val >> 8) - mask) | val) & mask; | 629 | | return ctz7_tab[(t * 0x0080402010080402ULL) >> 57]; | 630 | | #else | 631 | | /* this method doesn't consume memory space like the previous one, | 632 | | * but it contains several branches, | 633 | | * that may end up slowing execution */ | 634 | | static const U32 by32 = sizeof(val)*4; /* 32 on 64 bits (goal), 16 on 32 bits. | 635 | | Just to avoid some static analyzer complaining about shift by 32 on 32-bits target. | 636 | | Note that this code path is never triggered in 32-bits mode. */ | 637 | | unsigned r; | 638 | | if (!(val>>by32)) { r=4; } else { r=0; val>>=by32; } | 639 | | if (!(val>>16)) { r+=2; val>>=8; } else { val>>=24; } | 640 | | r += (!val); | 641 | | return r; | 642 | | #endif | 643 | | # endif | 644 | 0 | } else /* 32 bits */ { | 645 | 0 | # if (defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 3) || \ | 646 | 0 | ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))))) && \ | 647 | 0 | !defined(LZ4_FORCE_SW_BITCOUNT) | 648 | 0 | return (unsigned)__builtin_clz((U32)val) >> 3; | 649 | | # else | 650 | | val >>= 8; | 651 | | val = ((((val + 0x00FFFF00) | 0x00FFFFFF) + val) | | 652 | | (val + 0x00FF0000)) >> 24; | 653 | | return (unsigned)val ^ 3; | 654 | | # endif | 655 | 0 | } | 656 | 0 | } | 657 | 4.02M | } |
|
658 | | |
659 | | |
660 | 711M | #define STEPSIZE sizeof(reg_t) |
661 | | LZ4_FORCE_INLINE |
662 | | unsigned LZ4_count(const BYTE* pIn, const BYTE* pMatch, const BYTE* pInLimit) |
663 | 1.96M | { |
664 | 1.96M | const BYTE* const pStart = pIn; |
665 | | |
666 | 1.96M | if (likely(pIn < pInLimit-(STEPSIZE-1))) { |
667 | 1.88M | reg_t const diff = LZ4_read_ARCH(pMatch) ^ LZ4_read_ARCH(pIn); |
668 | 1.88M | if (!diff) { |
669 | 1.38M | pIn+=STEPSIZE; pMatch+=STEPSIZE; |
670 | 1.38M | } else { |
671 | 499k | return LZ4_NbCommonBytes(diff); |
672 | 499k | } } |
673 | | |
674 | 355M | while (likely(pIn < pInLimit-(STEPSIZE-1))) { |
675 | 355M | reg_t const diff = LZ4_read_ARCH(pMatch) ^ LZ4_read_ARCH(pIn); |
676 | 355M | if (!diff) { pIn+=STEPSIZE; pMatch+=STEPSIZE; continue; } |
677 | 1.36M | pIn += LZ4_NbCommonBytes(diff); |
678 | 1.36M | return (unsigned)(pIn - pStart); |
679 | 355M | } |
680 | | |
681 | 93.6k | if ((STEPSIZE==8) && (pIn<(pInLimit-3)) && (LZ4_read32(pMatch) == LZ4_read32(pIn))) { pIn+=4; pMatch+=4; } |
682 | 93.6k | if ((pIn<(pInLimit-1)) && (LZ4_read16(pMatch) == LZ4_read16(pIn))) { pIn+=2; pMatch+=2; } |
683 | 93.6k | if ((pIn<pInLimit) && (*pMatch == *pIn)) pIn++; |
684 | 93.6k | return (unsigned)(pIn - pStart); |
685 | 1.46M | } Unexecuted instantiation: lz4.c:LZ4_count Line | Count | Source | 663 | 1.96M | { | 664 | 1.96M | const BYTE* const pStart = pIn; | 665 | | | 666 | 1.96M | if (likely(pIn < pInLimit-(STEPSIZE-1))) { | 667 | 1.88M | reg_t const diff = LZ4_read_ARCH(pMatch) ^ LZ4_read_ARCH(pIn); | 668 | 1.88M | if (!diff) { | 669 | 1.38M | pIn+=STEPSIZE; pMatch+=STEPSIZE; | 670 | 1.38M | } else { | 671 | 499k | return LZ4_NbCommonBytes(diff); | 672 | 499k | } } | 673 | | | 674 | 355M | while (likely(pIn < pInLimit-(STEPSIZE-1))) { | 675 | 355M | reg_t const diff = LZ4_read_ARCH(pMatch) ^ LZ4_read_ARCH(pIn); | 676 | 355M | if (!diff) { pIn+=STEPSIZE; pMatch+=STEPSIZE; continue; } | 677 | 1.36M | pIn += LZ4_NbCommonBytes(diff); | 678 | 1.36M | return (unsigned)(pIn - pStart); | 679 | 355M | } | 680 | | | 681 | 93.6k | if ((STEPSIZE==8) && (pIn<(pInLimit-3)) && (LZ4_read32(pMatch) == LZ4_read32(pIn))) { pIn+=4; pMatch+=4; } | 682 | 93.6k | if ((pIn<(pInLimit-1)) && (LZ4_read16(pMatch) == LZ4_read16(pIn))) { pIn+=2; pMatch+=2; } | 683 | 93.6k | if ((pIn<pInLimit) && (*pMatch == *pIn)) pIn++; | 684 | 93.6k | return (unsigned)(pIn - pStart); | 685 | 1.46M | } |
|
686 | | |
687 | | |
688 | | #ifndef LZ4_COMMONDEFS_ONLY |
689 | | /*-************************************ |
690 | | * Local Constants |
691 | | **************************************/ |
692 | | static const int LZ4_64Klimit = ((64 KB) + (MFLIMIT-1)); |
693 | | static const U32 LZ4_skipTrigger = 6; /* Increase this value ==> compression run slower on incompressible data */ |
694 | | |
695 | | |
696 | | /*-************************************ |
697 | | * Local Structures and types |
698 | | **************************************/ |
699 | | typedef enum { clearedTable = 0, byPtr, byU32, byU16 } tableType_t; |
700 | | |
701 | | /** |
702 | | * This enum distinguishes several different modes of accessing previous |
703 | | * content in the stream. |
704 | | * |
705 | | * - noDict : There is no preceding content. |
706 | | * - withPrefix64k : Table entries up to ctx->dictSize before the current blob |
707 | | * blob being compressed are valid and refer to the preceding |
708 | | * content (of length ctx->dictSize), which is available |
709 | | * contiguously preceding in memory the content currently |
710 | | * being compressed. |
711 | | * - usingExtDict : Like withPrefix64k, but the preceding content is somewhere |
712 | | * else in memory, starting at ctx->dictionary with length |
713 | | * ctx->dictSize. |
714 | | * - usingDictCtx : Everything concerning the preceding content is |
715 | | * in a separate context, pointed to by ctx->dictCtx. |
716 | | * ctx->dictionary, ctx->dictSize, and table entries |
717 | | * in the current context that refer to positions |
718 | | * preceding the beginning of the current compression are |
719 | | * ignored. Instead, ctx->dictCtx->dictionary and ctx->dictCtx |
720 | | * ->dictSize describe the location and size of the preceding |
721 | | * content, and matches are found by looking in the ctx |
722 | | * ->dictCtx->hashTable. |
723 | | */ |
724 | | typedef enum { noDict = 0, withPrefix64k, usingExtDict, usingDictCtx } dict_directive; |
725 | | typedef enum { noDictIssue = 0, dictSmall } dictIssue_directive; |
726 | | |
727 | | |
728 | | /*-************************************ |
729 | | * Local Utils |
730 | | **************************************/ |
731 | 0 | int LZ4_versionNumber (void) { return LZ4_VERSION_NUMBER; } |
732 | 0 | const char* LZ4_versionString(void) { return LZ4_VERSION_STRING; } |
733 | 8.43k | int LZ4_compressBound(int isize) { return LZ4_COMPRESSBOUND(isize); } |
734 | 0 | int LZ4_sizeofState(void) { return sizeof(LZ4_stream_t); } |
735 | | |
736 | | |
737 | | /*-**************************************** |
738 | | * Internal Definitions, used only in Tests |
739 | | *******************************************/ |
740 | | #if defined (__cplusplus) |
741 | | extern "C" { |
742 | | #endif |
743 | | |
744 | | int LZ4_compress_forceExtDict (LZ4_stream_t* LZ4_dict, const char* source, char* dest, int srcSize); |
745 | | |
746 | | int LZ4_decompress_safe_forceExtDict(const char* source, char* dest, |
747 | | int compressedSize, int maxOutputSize, |
748 | | const void* dictStart, size_t dictSize); |
749 | | int LZ4_decompress_safe_partial_forceExtDict(const char* source, char* dest, |
750 | | int compressedSize, int targetOutputSize, int dstCapacity, |
751 | | const void* dictStart, size_t dictSize); |
752 | | #if defined (__cplusplus) |
753 | | } |
754 | | #endif |
755 | | |
756 | | /*-****************************** |
757 | | * Compression functions |
758 | | ********************************/ |
759 | | LZ4_FORCE_INLINE U32 LZ4_hash4(U32 sequence, tableType_t const tableType) |
760 | 0 | { |
761 | 0 | if (tableType == byU16) |
762 | 0 | return ((sequence * 2654435761U) >> ((MINMATCH*8)-(LZ4_HASHLOG+1))); |
763 | 0 | else |
764 | 0 | return ((sequence * 2654435761U) >> ((MINMATCH*8)-LZ4_HASHLOG)); |
765 | 0 | } |
766 | | |
767 | | LZ4_FORCE_INLINE U32 LZ4_hash5(U64 sequence, tableType_t const tableType) |
768 | 0 | { |
769 | 0 | const U32 hashLog = (tableType == byU16) ? LZ4_HASHLOG+1 : LZ4_HASHLOG; |
770 | 0 | if (LZ4_isLittleEndian()) { |
771 | 0 | const U64 prime5bytes = 889523592379ULL; |
772 | 0 | return (U32)(((sequence << 24) * prime5bytes) >> (64 - hashLog)); |
773 | 0 | } else { |
774 | 0 | const U64 prime8bytes = 11400714785074694791ULL; |
775 | 0 | return (U32)(((sequence >> 24) * prime8bytes) >> (64 - hashLog)); |
776 | 0 | } |
777 | 0 | } |
778 | | |
779 | | LZ4_FORCE_INLINE U32 LZ4_hashPosition(const void* const p, tableType_t const tableType) |
780 | 0 | { |
781 | 0 | if ((sizeof(reg_t)==8) && (tableType != byU16)) return LZ4_hash5(LZ4_read_ARCH(p), tableType); |
782 | 0 | return LZ4_hash4(LZ4_read32(p), tableType); |
783 | 0 | } |
784 | | |
785 | | LZ4_FORCE_INLINE void LZ4_clearHash(U32 h, void* tableBase, tableType_t const tableType) |
786 | 0 | { |
787 | 0 | switch (tableType) |
788 | 0 | { |
789 | 0 | default: /* fallthrough */ |
790 | 0 | case clearedTable: { /* illegal! */ assert(0); return; } |
791 | 0 | case byPtr: { const BYTE** hashTable = (const BYTE**)tableBase; hashTable[h] = NULL; return; } |
792 | 0 | case byU32: { U32* hashTable = (U32*) tableBase; hashTable[h] = 0; return; } |
793 | 0 | case byU16: { U16* hashTable = (U16*) tableBase; hashTable[h] = 0; return; } |
794 | 0 | } |
795 | 0 | } |
796 | | |
797 | | LZ4_FORCE_INLINE void LZ4_putIndexOnHash(U32 idx, U32 h, void* tableBase, tableType_t const tableType) |
798 | 0 | { |
799 | 0 | switch (tableType) |
800 | 0 | { |
801 | 0 | default: /* fallthrough */ |
802 | 0 | case clearedTable: /* fallthrough */ |
803 | 0 | case byPtr: { /* illegal! */ assert(0); return; } |
804 | 0 | case byU32: { U32* hashTable = (U32*) tableBase; hashTable[h] = idx; return; } |
805 | 0 | case byU16: { U16* hashTable = (U16*) tableBase; assert(idx < 65536); hashTable[h] = (U16)idx; return; } |
806 | 0 | } |
807 | 0 | } |
808 | | |
809 | | /* LZ4_putPosition*() : only used in byPtr mode */ |
810 | | LZ4_FORCE_INLINE void LZ4_putPositionOnHash(const BYTE* p, U32 h, |
811 | | void* tableBase, tableType_t const tableType) |
812 | 0 | { |
813 | 0 | const BYTE** const hashTable = (const BYTE**)tableBase; |
814 | 0 | assert(tableType == byPtr); (void)tableType; |
815 | 0 | hashTable[h] = p; |
816 | 0 | } |
817 | | |
818 | | LZ4_FORCE_INLINE void LZ4_putPosition(const BYTE* p, void* tableBase, tableType_t tableType) |
819 | 0 | { |
820 | 0 | U32 const h = LZ4_hashPosition(p, tableType); |
821 | 0 | LZ4_putPositionOnHash(p, h, tableBase, tableType); |
822 | 0 | } |
823 | | |
824 | | /* LZ4_getIndexOnHash() : |
825 | | * Index of match position registered in hash table. |
826 | | * hash position must be calculated by using base+index, or dictBase+index. |
827 | | * Assumption 1 : only valid if tableType == byU32 or byU16. |
828 | | * Assumption 2 : h is presumed valid (within limits of hash table) |
829 | | */ |
830 | | LZ4_FORCE_INLINE U32 LZ4_getIndexOnHash(U32 h, const void* tableBase, tableType_t tableType) |
831 | 0 | { |
832 | 0 | LZ4_STATIC_ASSERT(LZ4_MEMORY_USAGE > 2); |
833 | 0 | if (tableType == byU32) { |
834 | 0 | const U32* const hashTable = (const U32*) tableBase; |
835 | 0 | assert(h < (1U << (LZ4_MEMORY_USAGE-2))); |
836 | 0 | return hashTable[h]; |
837 | 0 | } |
838 | 0 | if (tableType == byU16) { |
839 | 0 | const U16* const hashTable = (const U16*) tableBase; |
840 | 0 | assert(h < (1U << (LZ4_MEMORY_USAGE-1))); |
841 | 0 | return hashTable[h]; |
842 | 0 | } |
843 | 0 | assert(0); return 0; /* forbidden case */ |
844 | 0 | } |
845 | | |
846 | | static const BYTE* LZ4_getPositionOnHash(U32 h, const void* tableBase, tableType_t tableType) |
847 | 0 | { |
848 | 0 | assert(tableType == byPtr); (void)tableType; |
849 | 0 | { const BYTE* const* hashTable = (const BYTE* const*) tableBase; return hashTable[h]; } |
850 | 0 | } |
851 | | |
852 | | LZ4_FORCE_INLINE const BYTE* |
853 | | LZ4_getPosition(const BYTE* p, |
854 | | const void* tableBase, tableType_t tableType) |
855 | 0 | { |
856 | 0 | U32 const h = LZ4_hashPosition(p, tableType); |
857 | 0 | return LZ4_getPositionOnHash(h, tableBase, tableType); |
858 | 0 | } |
859 | | |
860 | | LZ4_FORCE_INLINE void |
861 | | LZ4_prepareTable(LZ4_stream_t_internal* const cctx, |
862 | | const int inputSize, |
863 | 0 | const tableType_t tableType) { |
864 | | /* If the table hasn't been used, it's guaranteed to be zeroed out, and is |
865 | | * therefore safe to use no matter what mode we're in. Otherwise, we figure |
866 | | * out if it's safe to leave as is or whether it needs to be reset. |
867 | | */ |
868 | 0 | if ((tableType_t)cctx->tableType != clearedTable) { |
869 | 0 | assert(inputSize >= 0); |
870 | 0 | if ((tableType_t)cctx->tableType != tableType |
871 | 0 | || ((tableType == byU16) && cctx->currentOffset + (unsigned)inputSize >= 0xFFFFU) |
872 | 0 | || ((tableType == byU32) && cctx->currentOffset > 1 GB) |
873 | 0 | || tableType == byPtr |
874 | 0 | || inputSize >= 4 KB) |
875 | 0 | { |
876 | 0 | DEBUGLOG(4, "LZ4_prepareTable: Resetting table in %p", cctx); |
877 | 0 | MEM_INIT(cctx->hashTable, 0, LZ4_HASHTABLESIZE); |
878 | 0 | cctx->currentOffset = 0; |
879 | 0 | cctx->tableType = (U32)clearedTable; |
880 | 0 | } else { |
881 | 0 | DEBUGLOG(4, "LZ4_prepareTable: Re-use hash table (no reset)"); |
882 | 0 | } |
883 | 0 | } |
884 | | |
885 | | /* Adding a gap, so all previous entries are > LZ4_DISTANCE_MAX back, |
886 | | * is faster than compressing without a gap. |
887 | | * However, compressing with currentOffset == 0 is faster still, |
888 | | * so we preserve that case. |
889 | | */ |
890 | 0 | if (cctx->currentOffset != 0 && tableType == byU32) { |
891 | 0 | DEBUGLOG(5, "LZ4_prepareTable: adding 64KB to currentOffset"); |
892 | 0 | cctx->currentOffset += 64 KB; |
893 | 0 | } |
894 | | |
895 | | /* Finally, clear history */ |
896 | 0 | cctx->dictCtx = NULL; |
897 | 0 | cctx->dictionary = NULL; |
898 | 0 | cctx->dictSize = 0; |
899 | 0 | } |
900 | | |
901 | | /** LZ4_compress_generic() : |
902 | | * inlined, to ensure branches are decided at compilation time. |
903 | | * The following conditions are presumed already validated: |
904 | | * - source != NULL |
905 | | * - inputSize > 0 |
906 | | */ |
907 | | LZ4_FORCE_INLINE int LZ4_compress_generic_validated( |
908 | | LZ4_stream_t_internal* const cctx, |
909 | | const char* const source, |
910 | | char* const dest, |
911 | | const int inputSize, |
912 | | int* inputConsumed, /* only written when outputDirective == fillOutput */ |
913 | | const int maxOutputSize, |
914 | | const limitedOutput_directive outputDirective, |
915 | | const tableType_t tableType, |
916 | | const dict_directive dictDirective, |
917 | | const dictIssue_directive dictIssue, |
918 | | const int acceleration) |
919 | 0 | { |
920 | 0 | int result; |
921 | 0 | const BYTE* ip = (const BYTE*)source; |
922 | |
|
923 | 0 | U32 const startIndex = cctx->currentOffset; |
924 | 0 | const BYTE* base = (const BYTE*)source - startIndex; |
925 | 0 | const BYTE* lowLimit; |
926 | |
|
927 | 0 | const LZ4_stream_t_internal* dictCtx = (const LZ4_stream_t_internal*) cctx->dictCtx; |
928 | 0 | const BYTE* const dictionary = |
929 | 0 | dictDirective == usingDictCtx ? dictCtx->dictionary : cctx->dictionary; |
930 | 0 | const U32 dictSize = |
931 | 0 | dictDirective == usingDictCtx ? dictCtx->dictSize : cctx->dictSize; |
932 | 0 | const U32 dictDelta = |
933 | 0 | (dictDirective == usingDictCtx) ? startIndex - dictCtx->currentOffset : 0; /* make indexes in dictCtx comparable with indexes in current context */ |
934 | |
|
935 | 0 | int const maybe_extMem = (dictDirective == usingExtDict) || (dictDirective == usingDictCtx); |
936 | 0 | U32 const prefixIdxLimit = startIndex - dictSize; /* used when dictDirective == dictSmall */ |
937 | 0 | const BYTE* const dictEnd = dictionary ? dictionary + dictSize : dictionary; |
938 | 0 | const BYTE* anchor = (const BYTE*) source; |
939 | 0 | const BYTE* const iend = ip + inputSize; |
940 | 0 | const BYTE* const mflimitPlusOne = iend - MFLIMIT + 1; |
941 | 0 | const BYTE* const matchlimit = iend - LASTLITERALS; |
942 | | |
943 | | /* the dictCtx currentOffset is indexed on the start of the dictionary, |
944 | | * while a dictionary in the current context precedes the currentOffset */ |
945 | 0 | const BYTE* dictBase = (dictionary == NULL) ? NULL : |
946 | 0 | (dictDirective == usingDictCtx) ? |
947 | 0 | dictionary + dictSize - dictCtx->currentOffset : |
948 | 0 | dictionary + dictSize - startIndex; |
949 | |
|
950 | 0 | BYTE* op = (BYTE*) dest; |
951 | 0 | BYTE* const olimit = op + maxOutputSize; |
952 | |
|
953 | 0 | U32 offset = 0; |
954 | 0 | U32 forwardH; |
955 | |
|
956 | 0 | DEBUGLOG(5, "LZ4_compress_generic_validated: srcSize=%i, tableType=%u", inputSize, tableType); |
957 | 0 | assert(ip != NULL); |
958 | 0 | if (tableType == byU16) assert(inputSize<LZ4_64Klimit); /* Size too large (not within 64K limit) */ |
959 | 0 | if (tableType == byPtr) assert(dictDirective==noDict); /* only supported use case with byPtr */ |
960 | | /* If init conditions are not met, we don't have to mark stream |
961 | | * as having dirty context, since no action was taken yet */ |
962 | 0 | if (outputDirective == fillOutput && maxOutputSize < 1) { return 0; } /* Impossible to store anything */ |
963 | 0 | assert(acceleration >= 1); |
964 | | |
965 | 0 | lowLimit = (const BYTE*)source - (dictDirective == withPrefix64k ? dictSize : 0); |
966 | | |
967 | | /* Update context state */ |
968 | 0 | if (dictDirective == usingDictCtx) { |
969 | | /* Subsequent linked blocks can't use the dictionary. */ |
970 | | /* Instead, they use the block we just compressed. */ |
971 | 0 | cctx->dictCtx = NULL; |
972 | 0 | cctx->dictSize = (U32)inputSize; |
973 | 0 | } else { |
974 | 0 | cctx->dictSize += (U32)inputSize; |
975 | 0 | } |
976 | 0 | cctx->currentOffset += (U32)inputSize; |
977 | 0 | cctx->tableType = (U32)tableType; |
978 | |
|
979 | 0 | if (inputSize<LZ4_minLength) goto _last_literals; /* Input too small, no compression (all literals) */ |
980 | | |
981 | | /* First Byte */ |
982 | 0 | { U32 const h = LZ4_hashPosition(ip, tableType); |
983 | 0 | if (tableType == byPtr) { |
984 | 0 | LZ4_putPositionOnHash(ip, h, cctx->hashTable, byPtr); |
985 | 0 | } else { |
986 | 0 | LZ4_putIndexOnHash(startIndex, h, cctx->hashTable, tableType); |
987 | 0 | } } |
988 | 0 | ip++; forwardH = LZ4_hashPosition(ip, tableType); |
989 | | |
990 | | /* Main Loop */ |
991 | 0 | for ( ; ; ) { |
992 | 0 | const BYTE* match; |
993 | 0 | BYTE* token; |
994 | 0 | const BYTE* filledIp; |
995 | | |
996 | | /* Find a match */ |
997 | 0 | if (tableType == byPtr) { |
998 | 0 | const BYTE* forwardIp = ip; |
999 | 0 | int step = 1; |
1000 | 0 | int searchMatchNb = acceleration << LZ4_skipTrigger; |
1001 | 0 | do { |
1002 | 0 | U32 const h = forwardH; |
1003 | 0 | ip = forwardIp; |
1004 | 0 | forwardIp += step; |
1005 | 0 | step = (searchMatchNb++ >> LZ4_skipTrigger); |
1006 | |
|
1007 | 0 | if (unlikely(forwardIp > mflimitPlusOne)) goto _last_literals; |
1008 | 0 | assert(ip < mflimitPlusOne); |
1009 | | |
1010 | 0 | match = LZ4_getPositionOnHash(h, cctx->hashTable, tableType); |
1011 | 0 | forwardH = LZ4_hashPosition(forwardIp, tableType); |
1012 | 0 | LZ4_putPositionOnHash(ip, h, cctx->hashTable, tableType); |
1013 | |
|
1014 | 0 | } while ( (match+LZ4_DISTANCE_MAX < ip) |
1015 | 0 | || (LZ4_read32(match) != LZ4_read32(ip)) ); |
1016 | |
|
1017 | 0 | } else { /* byU32, byU16 */ |
1018 | |
|
1019 | 0 | const BYTE* forwardIp = ip; |
1020 | 0 | int step = 1; |
1021 | 0 | int searchMatchNb = acceleration << LZ4_skipTrigger; |
1022 | 0 | do { |
1023 | 0 | U32 const h = forwardH; |
1024 | 0 | U32 const current = (U32)(forwardIp - base); |
1025 | 0 | U32 matchIndex = LZ4_getIndexOnHash(h, cctx->hashTable, tableType); |
1026 | 0 | assert(matchIndex <= current); |
1027 | 0 | assert(forwardIp - base < (ptrdiff_t)(2 GB - 1)); |
1028 | 0 | ip = forwardIp; |
1029 | 0 | forwardIp += step; |
1030 | 0 | step = (searchMatchNb++ >> LZ4_skipTrigger); |
1031 | |
|
1032 | 0 | if (unlikely(forwardIp > mflimitPlusOne)) goto _last_literals; |
1033 | 0 | assert(ip < mflimitPlusOne); |
1034 | | |
1035 | 0 | if (dictDirective == usingDictCtx) { |
1036 | 0 | if (matchIndex < startIndex) { |
1037 | | /* there was no match, try the dictionary */ |
1038 | 0 | assert(tableType == byU32); |
1039 | 0 | matchIndex = LZ4_getIndexOnHash(h, dictCtx->hashTable, byU32); |
1040 | 0 | match = dictBase + matchIndex; |
1041 | 0 | matchIndex += dictDelta; /* make dictCtx index comparable with current context */ |
1042 | 0 | lowLimit = dictionary; |
1043 | 0 | } else { |
1044 | 0 | match = base + matchIndex; |
1045 | 0 | lowLimit = (const BYTE*)source; |
1046 | 0 | } |
1047 | 0 | } else if (dictDirective == usingExtDict) { |
1048 | 0 | if (matchIndex < startIndex) { |
1049 | 0 | DEBUGLOG(7, "extDict candidate: matchIndex=%5u < startIndex=%5u", matchIndex, startIndex); |
1050 | 0 | assert(startIndex - matchIndex >= MINMATCH); |
1051 | 0 | assert(dictBase); |
1052 | 0 | match = dictBase + matchIndex; |
1053 | 0 | lowLimit = dictionary; |
1054 | 0 | } else { |
1055 | 0 | match = base + matchIndex; |
1056 | 0 | lowLimit = (const BYTE*)source; |
1057 | 0 | } |
1058 | 0 | } else { /* single continuous memory segment */ |
1059 | 0 | match = base + matchIndex; |
1060 | 0 | } |
1061 | 0 | forwardH = LZ4_hashPosition(forwardIp, tableType); |
1062 | 0 | LZ4_putIndexOnHash(current, h, cctx->hashTable, tableType); |
1063 | |
|
1064 | 0 | DEBUGLOG(7, "candidate at pos=%u (offset=%u \n", matchIndex, current - matchIndex); |
1065 | 0 | if ((dictIssue == dictSmall) && (matchIndex < prefixIdxLimit)) { continue; } /* match outside of valid area */ |
1066 | 0 | assert(matchIndex < current); |
1067 | 0 | if ( ((tableType != byU16) || (LZ4_DISTANCE_MAX < LZ4_DISTANCE_ABSOLUTE_MAX)) |
1068 | 0 | && (matchIndex+LZ4_DISTANCE_MAX < current)) { |
1069 | 0 | continue; |
1070 | 0 | } /* too far */ |
1071 | 0 | assert((current - matchIndex) <= LZ4_DISTANCE_MAX); /* match now expected within distance */ |
1072 | | |
1073 | 0 | if (LZ4_read32(match) == LZ4_read32(ip)) { |
1074 | 0 | if (maybe_extMem) offset = current - matchIndex; |
1075 | 0 | break; /* match found */ |
1076 | 0 | } |
1077 | |
|
1078 | 0 | } while(1); |
1079 | 0 | } |
1080 | | |
1081 | | /* Catch up */ |
1082 | 0 | filledIp = ip; |
1083 | 0 | while (((ip>anchor) & (match > lowLimit)) && (unlikely(ip[-1]==match[-1]))) { ip--; match--; } |
1084 | | |
1085 | | /* Encode Literals */ |
1086 | 0 | { unsigned const litLength = (unsigned)(ip - anchor); |
1087 | 0 | token = op++; |
1088 | 0 | if ((outputDirective == limitedOutput) && /* Check output buffer overflow */ |
1089 | 0 | (unlikely(op + litLength + (2 + 1 + LASTLITERALS) + (litLength/255) > olimit)) ) { |
1090 | 0 | return 0; /* cannot compress within `dst` budget. Stored indexes in hash table are nonetheless fine */ |
1091 | 0 | } |
1092 | 0 | if ((outputDirective == fillOutput) && |
1093 | 0 | (unlikely(op + (litLength+240)/255 /* litlen */ + litLength /* literals */ + 2 /* offset */ + 1 /* token */ + MFLIMIT - MINMATCH /* min last literals so last match is <= end - MFLIMIT */ > olimit))) { |
1094 | 0 | op--; |
1095 | 0 | goto _last_literals; |
1096 | 0 | } |
1097 | 0 | if (litLength >= RUN_MASK) { |
1098 | 0 | int len = (int)(litLength - RUN_MASK); |
1099 | 0 | *token = (RUN_MASK<<ML_BITS); |
1100 | 0 | for(; len >= 255 ; len-=255) *op++ = 255; |
1101 | 0 | *op++ = (BYTE)len; |
1102 | 0 | } |
1103 | 0 | else *token = (BYTE)(litLength<<ML_BITS); |
1104 | | |
1105 | | /* Copy Literals */ |
1106 | 0 | LZ4_wildCopy8(op, anchor, op+litLength); |
1107 | 0 | op+=litLength; |
1108 | 0 | DEBUGLOG(6, "seq.start:%i, literals=%u, match.start:%i", |
1109 | 0 | (int)(anchor-(const BYTE*)source), litLength, (int)(ip-(const BYTE*)source)); |
1110 | 0 | } |
1111 | | |
1112 | 0 | _next_match: |
1113 | | /* at this stage, the following variables must be correctly set : |
1114 | | * - ip : at start of LZ operation |
1115 | | * - match : at start of previous pattern occurrence; can be within current prefix, or within extDict |
1116 | | * - offset : if maybe_ext_memSegment==1 (constant) |
1117 | | * - lowLimit : must be == dictionary to mean "match is within extDict"; must be == source otherwise |
1118 | | * - token and *token : position to write 4-bits for match length; higher 4-bits for literal length supposed already written |
1119 | | */ |
1120 | |
|
1121 | 0 | if ((outputDirective == fillOutput) && |
1122 | 0 | (op + 2 /* offset */ + 1 /* token */ + MFLIMIT - MINMATCH /* min last literals so last match is <= end - MFLIMIT */ > olimit)) { |
1123 | | /* the match was too close to the end, rewind and go to last literals */ |
1124 | 0 | op = token; |
1125 | 0 | goto _last_literals; |
1126 | 0 | } |
1127 | | |
1128 | | /* Encode Offset */ |
1129 | 0 | if (maybe_extMem) { /* static test */ |
1130 | 0 | DEBUGLOG(6, " with offset=%u (ext if > %i)", offset, (int)(ip - (const BYTE*)source)); |
1131 | 0 | assert(offset <= LZ4_DISTANCE_MAX && offset > 0); |
1132 | 0 | LZ4_writeLE16(op, (U16)offset); op+=2; |
1133 | 0 | } else { |
1134 | 0 | DEBUGLOG(6, " with offset=%u (same segment)", (U32)(ip - match)); |
1135 | 0 | assert(ip-match <= LZ4_DISTANCE_MAX); |
1136 | 0 | LZ4_writeLE16(op, (U16)(ip - match)); op+=2; |
1137 | 0 | } |
1138 | | |
1139 | | /* Encode MatchLength */ |
1140 | 0 | { unsigned matchCode; |
1141 | |
|
1142 | 0 | if ( (dictDirective==usingExtDict || dictDirective==usingDictCtx) |
1143 | 0 | && (lowLimit==dictionary) /* match within extDict */ ) { |
1144 | 0 | const BYTE* limit = ip + (dictEnd-match); |
1145 | 0 | assert(dictEnd > match); |
1146 | 0 | if (limit > matchlimit) limit = matchlimit; |
1147 | 0 | matchCode = LZ4_count(ip+MINMATCH, match+MINMATCH, limit); |
1148 | 0 | ip += (size_t)matchCode + MINMATCH; |
1149 | 0 | if (ip==limit) { |
1150 | 0 | unsigned const more = LZ4_count(limit, (const BYTE*)source, matchlimit); |
1151 | 0 | matchCode += more; |
1152 | 0 | ip += more; |
1153 | 0 | } |
1154 | 0 | DEBUGLOG(6, " with matchLength=%u starting in extDict", matchCode+MINMATCH); |
1155 | 0 | } else { |
1156 | 0 | matchCode = LZ4_count(ip+MINMATCH, match+MINMATCH, matchlimit); |
1157 | 0 | ip += (size_t)matchCode + MINMATCH; |
1158 | 0 | DEBUGLOG(6, " with matchLength=%u", matchCode+MINMATCH); |
1159 | 0 | } |
1160 | | |
1161 | 0 | if ((outputDirective) && /* Check output buffer overflow */ |
1162 | 0 | (unlikely(op + (1 + LASTLITERALS) + (matchCode+240)/255 > olimit)) ) { |
1163 | 0 | if (outputDirective == fillOutput) { |
1164 | | /* Match description too long : reduce it */ |
1165 | 0 | U32 newMatchCode = 15 /* in token */ - 1 /* to avoid needing a zero byte */ + ((U32)(olimit - op) - 1 - LASTLITERALS) * 255; |
1166 | 0 | ip -= matchCode - newMatchCode; |
1167 | 0 | assert(newMatchCode < matchCode); |
1168 | 0 | matchCode = newMatchCode; |
1169 | 0 | if (unlikely(ip <= filledIp)) { |
1170 | | /* We have already filled up to filledIp so if ip ends up less than filledIp |
1171 | | * we have positions in the hash table beyond the current position. This is |
1172 | | * a problem if we reuse the hash table. So we have to remove these positions |
1173 | | * from the hash table. |
1174 | | */ |
1175 | 0 | const BYTE* ptr; |
1176 | 0 | DEBUGLOG(5, "Clearing %u positions", (U32)(filledIp - ip)); |
1177 | 0 | for (ptr = ip; ptr <= filledIp; ++ptr) { |
1178 | 0 | U32 const h = LZ4_hashPosition(ptr, tableType); |
1179 | 0 | LZ4_clearHash(h, cctx->hashTable, tableType); |
1180 | 0 | } |
1181 | 0 | } |
1182 | 0 | } else { |
1183 | 0 | assert(outputDirective == limitedOutput); |
1184 | 0 | return 0; /* cannot compress within `dst` budget. Stored indexes in hash table are nonetheless fine */ |
1185 | 0 | } |
1186 | 0 | } |
1187 | 0 | if (matchCode >= ML_MASK) { |
1188 | 0 | *token += ML_MASK; |
1189 | 0 | matchCode -= ML_MASK; |
1190 | 0 | LZ4_write32(op, 0xFFFFFFFF); |
1191 | 0 | while (matchCode >= 4*255) { |
1192 | 0 | op+=4; |
1193 | 0 | LZ4_write32(op, 0xFFFFFFFF); |
1194 | 0 | matchCode -= 4*255; |
1195 | 0 | } |
1196 | 0 | op += matchCode / 255; |
1197 | 0 | *op++ = (BYTE)(matchCode % 255); |
1198 | 0 | } else |
1199 | 0 | *token += (BYTE)(matchCode); |
1200 | 0 | } |
1201 | | /* Ensure we have enough space for the last literals. */ |
1202 | 0 | assert(!(outputDirective == fillOutput && op + 1 + LASTLITERALS > olimit)); |
1203 | | |
1204 | 0 | anchor = ip; |
1205 | | |
1206 | | /* Test end of chunk */ |
1207 | 0 | if (ip >= mflimitPlusOne) break; |
1208 | | |
1209 | | /* Fill table */ |
1210 | 0 | { U32 const h = LZ4_hashPosition(ip-2, tableType); |
1211 | 0 | if (tableType == byPtr) { |
1212 | 0 | LZ4_putPositionOnHash(ip-2, h, cctx->hashTable, byPtr); |
1213 | 0 | } else { |
1214 | 0 | U32 const idx = (U32)((ip-2) - base); |
1215 | 0 | LZ4_putIndexOnHash(idx, h, cctx->hashTable, tableType); |
1216 | 0 | } } |
1217 | | |
1218 | | /* Test next position */ |
1219 | 0 | if (tableType == byPtr) { |
1220 | |
|
1221 | 0 | match = LZ4_getPosition(ip, cctx->hashTable, tableType); |
1222 | 0 | LZ4_putPosition(ip, cctx->hashTable, tableType); |
1223 | 0 | if ( (match+LZ4_DISTANCE_MAX >= ip) |
1224 | 0 | && (LZ4_read32(match) == LZ4_read32(ip)) ) |
1225 | 0 | { token=op++; *token=0; goto _next_match; } |
1226 | |
|
1227 | 0 | } else { /* byU32, byU16 */ |
1228 | |
|
1229 | 0 | U32 const h = LZ4_hashPosition(ip, tableType); |
1230 | 0 | U32 const current = (U32)(ip-base); |
1231 | 0 | U32 matchIndex = LZ4_getIndexOnHash(h, cctx->hashTable, tableType); |
1232 | 0 | assert(matchIndex < current); |
1233 | 0 | if (dictDirective == usingDictCtx) { |
1234 | 0 | if (matchIndex < startIndex) { |
1235 | | /* there was no match, try the dictionary */ |
1236 | 0 | assert(tableType == byU32); |
1237 | 0 | matchIndex = LZ4_getIndexOnHash(h, dictCtx->hashTable, byU32); |
1238 | 0 | match = dictBase + matchIndex; |
1239 | 0 | lowLimit = dictionary; /* required for match length counter */ |
1240 | 0 | matchIndex += dictDelta; |
1241 | 0 | } else { |
1242 | 0 | match = base + matchIndex; |
1243 | 0 | lowLimit = (const BYTE*)source; /* required for match length counter */ |
1244 | 0 | } |
1245 | 0 | } else if (dictDirective==usingExtDict) { |
1246 | 0 | if (matchIndex < startIndex) { |
1247 | 0 | assert(dictBase); |
1248 | 0 | match = dictBase + matchIndex; |
1249 | 0 | lowLimit = dictionary; /* required for match length counter */ |
1250 | 0 | } else { |
1251 | 0 | match = base + matchIndex; |
1252 | 0 | lowLimit = (const BYTE*)source; /* required for match length counter */ |
1253 | 0 | } |
1254 | 0 | } else { /* single memory segment */ |
1255 | 0 | match = base + matchIndex; |
1256 | 0 | } |
1257 | 0 | LZ4_putIndexOnHash(current, h, cctx->hashTable, tableType); |
1258 | 0 | assert(matchIndex < current); |
1259 | 0 | if ( ((dictIssue==dictSmall) ? (matchIndex >= prefixIdxLimit) : 1) |
1260 | 0 | && (((tableType==byU16) && (LZ4_DISTANCE_MAX == LZ4_DISTANCE_ABSOLUTE_MAX)) ? 1 : (matchIndex+LZ4_DISTANCE_MAX >= current)) |
1261 | 0 | && (LZ4_read32(match) == LZ4_read32(ip)) ) { |
1262 | 0 | token=op++; |
1263 | 0 | *token=0; |
1264 | 0 | if (maybe_extMem) offset = current - matchIndex; |
1265 | 0 | DEBUGLOG(6, "seq.start:%i, literals=%u, match.start:%i", |
1266 | 0 | (int)(anchor-(const BYTE*)source), 0, (int)(ip-(const BYTE*)source)); |
1267 | 0 | goto _next_match; |
1268 | 0 | } |
1269 | 0 | } |
1270 | | |
1271 | | /* Prepare next loop */ |
1272 | 0 | forwardH = LZ4_hashPosition(++ip, tableType); |
1273 | |
|
1274 | 0 | } |
1275 | | |
1276 | 0 | _last_literals: |
1277 | | /* Encode Last Literals */ |
1278 | 0 | { size_t lastRun = (size_t)(iend - anchor); |
1279 | 0 | if ( (outputDirective) && /* Check output buffer overflow */ |
1280 | 0 | (op + lastRun + 1 + ((lastRun+255-RUN_MASK)/255) > olimit)) { |
1281 | 0 | if (outputDirective == fillOutput) { |
1282 | | /* adapt lastRun to fill 'dst' */ |
1283 | 0 | assert(olimit >= op); |
1284 | 0 | lastRun = (size_t)(olimit-op) - 1/*token*/; |
1285 | 0 | lastRun -= (lastRun + 256 - RUN_MASK) / 256; /*additional length tokens*/ |
1286 | 0 | } else { |
1287 | 0 | assert(outputDirective == limitedOutput); |
1288 | 0 | return 0; /* cannot compress within `dst` budget. Stored indexes in hash table are nonetheless fine */ |
1289 | 0 | } |
1290 | 0 | } |
1291 | 0 | DEBUGLOG(6, "Final literal run : %i literals", (int)lastRun); |
1292 | 0 | if (lastRun >= RUN_MASK) { |
1293 | 0 | size_t accumulator = lastRun - RUN_MASK; |
1294 | 0 | *op++ = RUN_MASK << ML_BITS; |
1295 | 0 | for(; accumulator >= 255 ; accumulator-=255) *op++ = 255; |
1296 | 0 | *op++ = (BYTE) accumulator; |
1297 | 0 | } else { |
1298 | 0 | *op++ = (BYTE)(lastRun<<ML_BITS); |
1299 | 0 | } |
1300 | 0 | LZ4_memcpy(op, anchor, lastRun); |
1301 | 0 | ip = anchor + lastRun; |
1302 | 0 | op += lastRun; |
1303 | 0 | } |
1304 | | |
1305 | 0 | if (outputDirective == fillOutput) { |
1306 | 0 | *inputConsumed = (int) (((const char*)ip)-source); |
1307 | 0 | } |
1308 | 0 | result = (int)(((char*)op) - dest); |
1309 | 0 | assert(result > 0); |
1310 | 0 | DEBUGLOG(5, "LZ4_compress_generic: compressed %i bytes into %i bytes", inputSize, result); |
1311 | 0 | return result; |
1312 | 0 | } |
1313 | | |
1314 | | /** LZ4_compress_generic() : |
1315 | | * inlined, to ensure branches are decided at compilation time; |
1316 | | * takes care of src == (NULL, 0) |
1317 | | * and forward the rest to LZ4_compress_generic_validated */ |
1318 | | LZ4_FORCE_INLINE int LZ4_compress_generic( |
1319 | | LZ4_stream_t_internal* const cctx, |
1320 | | const char* const src, |
1321 | | char* const dst, |
1322 | | const int srcSize, |
1323 | | int *inputConsumed, /* only written when outputDirective == fillOutput */ |
1324 | | const int dstCapacity, |
1325 | | const limitedOutput_directive outputDirective, |
1326 | | const tableType_t tableType, |
1327 | | const dict_directive dictDirective, |
1328 | | const dictIssue_directive dictIssue, |
1329 | | const int acceleration) |
1330 | 0 | { |
1331 | 0 | DEBUGLOG(5, "LZ4_compress_generic: srcSize=%i, dstCapacity=%i", |
1332 | 0 | srcSize, dstCapacity); |
1333 | |
|
1334 | 0 | if ((U32)srcSize > (U32)LZ4_MAX_INPUT_SIZE) { return 0; } /* Unsupported srcSize, too large (or negative) */ |
1335 | 0 | if (srcSize == 0) { /* src == NULL supported if srcSize == 0 */ |
1336 | 0 | if (outputDirective != notLimited && dstCapacity <= 0) return 0; /* no output, can't write anything */ |
1337 | 0 | DEBUGLOG(5, "Generating an empty block"); |
1338 | 0 | assert(outputDirective == notLimited || dstCapacity >= 1); |
1339 | 0 | assert(dst != NULL); |
1340 | 0 | dst[0] = 0; |
1341 | 0 | if (outputDirective == fillOutput) { |
1342 | 0 | assert (inputConsumed != NULL); |
1343 | 0 | *inputConsumed = 0; |
1344 | 0 | } |
1345 | 0 | return 1; |
1346 | 0 | } |
1347 | 0 | assert(src != NULL); |
1348 | | |
1349 | 0 | return LZ4_compress_generic_validated(cctx, src, dst, srcSize, |
1350 | 0 | inputConsumed, /* only written into if outputDirective == fillOutput */ |
1351 | 0 | dstCapacity, outputDirective, |
1352 | 0 | tableType, dictDirective, dictIssue, acceleration); |
1353 | 0 | } |
1354 | | |
1355 | | |
1356 | | int LZ4_compress_fast_extState(void* state, const char* source, char* dest, int inputSize, int maxOutputSize, int acceleration) |
1357 | 0 | { |
1358 | 0 | LZ4_stream_t_internal* const ctx = & LZ4_initStream(state, sizeof(LZ4_stream_t)) -> internal_donotuse; |
1359 | 0 | assert(ctx != NULL); |
1360 | 0 | if (acceleration < 1) acceleration = LZ4_ACCELERATION_DEFAULT; |
1361 | 0 | if (acceleration > LZ4_ACCELERATION_MAX) acceleration = LZ4_ACCELERATION_MAX; |
1362 | 0 | if (maxOutputSize >= LZ4_compressBound(inputSize)) { |
1363 | 0 | if (inputSize < LZ4_64Klimit) { |
1364 | 0 | return LZ4_compress_generic(ctx, source, dest, inputSize, NULL, 0, notLimited, byU16, noDict, noDictIssue, acceleration); |
1365 | 0 | } else { |
1366 | 0 | const tableType_t tableType = ((sizeof(void*)==4) && ((uptrval)source > LZ4_DISTANCE_MAX)) ? byPtr : byU32; |
1367 | 0 | return LZ4_compress_generic(ctx, source, dest, inputSize, NULL, 0, notLimited, tableType, noDict, noDictIssue, acceleration); |
1368 | 0 | } |
1369 | 0 | } else { |
1370 | 0 | if (inputSize < LZ4_64Klimit) { |
1371 | 0 | return LZ4_compress_generic(ctx, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, byU16, noDict, noDictIssue, acceleration); |
1372 | 0 | } else { |
1373 | 0 | const tableType_t tableType = ((sizeof(void*)==4) && ((uptrval)source > LZ4_DISTANCE_MAX)) ? byPtr : byU32; |
1374 | 0 | return LZ4_compress_generic(ctx, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, noDict, noDictIssue, acceleration); |
1375 | 0 | } |
1376 | 0 | } |
1377 | 0 | } |
1378 | | |
1379 | | /** |
1380 | | * LZ4_compress_fast_extState_fastReset() : |
1381 | | * A variant of LZ4_compress_fast_extState(). |
1382 | | * |
1383 | | * Using this variant avoids an expensive initialization step. It is only safe |
1384 | | * to call if the state buffer is known to be correctly initialized already |
1385 | | * (see comment in lz4.h on LZ4_resetStream_fast() for a definition of |
1386 | | * "correctly initialized"). |
1387 | | */ |
1388 | | int LZ4_compress_fast_extState_fastReset(void* state, const char* src, char* dst, int srcSize, int dstCapacity, int acceleration) |
1389 | 0 | { |
1390 | 0 | LZ4_stream_t_internal* const ctx = &((LZ4_stream_t*)state)->internal_donotuse; |
1391 | 0 | if (acceleration < 1) acceleration = LZ4_ACCELERATION_DEFAULT; |
1392 | 0 | if (acceleration > LZ4_ACCELERATION_MAX) acceleration = LZ4_ACCELERATION_MAX; |
1393 | 0 | assert(ctx != NULL); |
1394 | | |
1395 | 0 | if (dstCapacity >= LZ4_compressBound(srcSize)) { |
1396 | 0 | if (srcSize < LZ4_64Klimit) { |
1397 | 0 | const tableType_t tableType = byU16; |
1398 | 0 | LZ4_prepareTable(ctx, srcSize, tableType); |
1399 | 0 | if (ctx->currentOffset) { |
1400 | 0 | return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, 0, notLimited, tableType, noDict, dictSmall, acceleration); |
1401 | 0 | } else { |
1402 | 0 | return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, 0, notLimited, tableType, noDict, noDictIssue, acceleration); |
1403 | 0 | } |
1404 | 0 | } else { |
1405 | 0 | const tableType_t tableType = ((sizeof(void*)==4) && ((uptrval)src > LZ4_DISTANCE_MAX)) ? byPtr : byU32; |
1406 | 0 | LZ4_prepareTable(ctx, srcSize, tableType); |
1407 | 0 | return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, 0, notLimited, tableType, noDict, noDictIssue, acceleration); |
1408 | 0 | } |
1409 | 0 | } else { |
1410 | 0 | if (srcSize < LZ4_64Klimit) { |
1411 | 0 | const tableType_t tableType = byU16; |
1412 | 0 | LZ4_prepareTable(ctx, srcSize, tableType); |
1413 | 0 | if (ctx->currentOffset) { |
1414 | 0 | return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, dstCapacity, limitedOutput, tableType, noDict, dictSmall, acceleration); |
1415 | 0 | } else { |
1416 | 0 | return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, dstCapacity, limitedOutput, tableType, noDict, noDictIssue, acceleration); |
1417 | 0 | } |
1418 | 0 | } else { |
1419 | 0 | const tableType_t tableType = ((sizeof(void*)==4) && ((uptrval)src > LZ4_DISTANCE_MAX)) ? byPtr : byU32; |
1420 | 0 | LZ4_prepareTable(ctx, srcSize, tableType); |
1421 | 0 | return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, dstCapacity, limitedOutput, tableType, noDict, noDictIssue, acceleration); |
1422 | 0 | } |
1423 | 0 | } |
1424 | 0 | } |
1425 | | |
1426 | | |
1427 | | int LZ4_compress_fast(const char* src, char* dest, int srcSize, int dstCapacity, int acceleration) |
1428 | 0 | { |
1429 | 0 | int result; |
1430 | | #if (LZ4_HEAPMODE) |
1431 | | LZ4_stream_t* const ctxPtr = (LZ4_stream_t*)ALLOC(sizeof(LZ4_stream_t)); /* malloc-calloc always properly aligned */ |
1432 | | if (ctxPtr == NULL) return 0; |
1433 | | #else |
1434 | 0 | LZ4_stream_t ctx; |
1435 | 0 | LZ4_stream_t* const ctxPtr = &ctx; |
1436 | 0 | #endif |
1437 | 0 | result = LZ4_compress_fast_extState(ctxPtr, src, dest, srcSize, dstCapacity, acceleration); |
1438 | |
|
1439 | | #if (LZ4_HEAPMODE) |
1440 | | FREEMEM(ctxPtr); |
1441 | | #endif |
1442 | 0 | return result; |
1443 | 0 | } |
1444 | | |
1445 | | |
1446 | | int LZ4_compress_default(const char* src, char* dst, int srcSize, int dstCapacity) |
1447 | 0 | { |
1448 | 0 | return LZ4_compress_fast(src, dst, srcSize, dstCapacity, 1); |
1449 | 0 | } |
1450 | | |
1451 | | |
1452 | | /* Note!: This function leaves the stream in an unclean/broken state! |
1453 | | * It is not safe to subsequently use the same state with a _fastReset() or |
1454 | | * _continue() call without resetting it. */ |
1455 | | static int LZ4_compress_destSize_extState (LZ4_stream_t* state, const char* src, char* dst, int* srcSizePtr, int targetDstSize) |
1456 | 0 | { |
1457 | 0 | void* const s = LZ4_initStream(state, sizeof (*state)); |
1458 | 0 | assert(s != NULL); (void)s; |
1459 | |
|
1460 | 0 | if (targetDstSize >= LZ4_compressBound(*srcSizePtr)) { /* compression success is guaranteed */ |
1461 | 0 | return LZ4_compress_fast_extState(state, src, dst, *srcSizePtr, targetDstSize, 1); |
1462 | 0 | } else { |
1463 | 0 | if (*srcSizePtr < LZ4_64Klimit) { |
1464 | 0 | return LZ4_compress_generic(&state->internal_donotuse, src, dst, *srcSizePtr, srcSizePtr, targetDstSize, fillOutput, byU16, noDict, noDictIssue, 1); |
1465 | 0 | } else { |
1466 | 0 | tableType_t const addrMode = ((sizeof(void*)==4) && ((uptrval)src > LZ4_DISTANCE_MAX)) ? byPtr : byU32; |
1467 | 0 | return LZ4_compress_generic(&state->internal_donotuse, src, dst, *srcSizePtr, srcSizePtr, targetDstSize, fillOutput, addrMode, noDict, noDictIssue, 1); |
1468 | 0 | } } |
1469 | 0 | } |
1470 | | |
1471 | | |
1472 | | int LZ4_compress_destSize(const char* src, char* dst, int* srcSizePtr, int targetDstSize) |
1473 | 0 | { |
1474 | | #if (LZ4_HEAPMODE) |
1475 | | LZ4_stream_t* const ctx = (LZ4_stream_t*)ALLOC(sizeof(LZ4_stream_t)); /* malloc-calloc always properly aligned */ |
1476 | | if (ctx == NULL) return 0; |
1477 | | #else |
1478 | 0 | LZ4_stream_t ctxBody; |
1479 | 0 | LZ4_stream_t* const ctx = &ctxBody; |
1480 | 0 | #endif |
1481 | |
|
1482 | 0 | int result = LZ4_compress_destSize_extState(ctx, src, dst, srcSizePtr, targetDstSize); |
1483 | |
|
1484 | | #if (LZ4_HEAPMODE) |
1485 | | FREEMEM(ctx); |
1486 | | #endif |
1487 | 0 | return result; |
1488 | 0 | } |
1489 | | |
1490 | | |
1491 | | |
1492 | | /*-****************************** |
1493 | | * Streaming functions |
1494 | | ********************************/ |
1495 | | |
1496 | | #if !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION) |
1497 | | LZ4_stream_t* LZ4_createStream(void) |
1498 | 0 | { |
1499 | 0 | LZ4_stream_t* const lz4s = (LZ4_stream_t*)ALLOC(sizeof(LZ4_stream_t)); |
1500 | 0 | LZ4_STATIC_ASSERT(sizeof(LZ4_stream_t) >= sizeof(LZ4_stream_t_internal)); |
1501 | 0 | DEBUGLOG(4, "LZ4_createStream %p", lz4s); |
1502 | 0 | if (lz4s == NULL) return NULL; |
1503 | 0 | LZ4_initStream(lz4s, sizeof(*lz4s)); |
1504 | 0 | return lz4s; |
1505 | 0 | } |
1506 | | #endif |
1507 | | |
1508 | | static size_t LZ4_stream_t_alignment(void) |
1509 | 0 | { |
1510 | 0 | #if LZ4_ALIGN_TEST |
1511 | 0 | typedef struct { char c; LZ4_stream_t t; } t_a; |
1512 | 0 | return sizeof(t_a) - sizeof(LZ4_stream_t); |
1513 | | #else |
1514 | | return 1; /* effectively disabled */ |
1515 | | #endif |
1516 | 0 | } |
1517 | | |
1518 | | LZ4_stream_t* LZ4_initStream (void* buffer, size_t size) |
1519 | 0 | { |
1520 | 0 | DEBUGLOG(5, "LZ4_initStream"); |
1521 | 0 | if (buffer == NULL) { return NULL; } |
1522 | 0 | if (size < sizeof(LZ4_stream_t)) { return NULL; } |
1523 | 0 | if (!LZ4_isAligned(buffer, LZ4_stream_t_alignment())) return NULL; |
1524 | 0 | MEM_INIT(buffer, 0, sizeof(LZ4_stream_t_internal)); |
1525 | 0 | return (LZ4_stream_t*)buffer; |
1526 | 0 | } |
1527 | | |
1528 | | /* resetStream is now deprecated, |
1529 | | * prefer initStream() which is more general */ |
1530 | | void LZ4_resetStream (LZ4_stream_t* LZ4_stream) |
1531 | 0 | { |
1532 | 0 | DEBUGLOG(5, "LZ4_resetStream (ctx:%p)", LZ4_stream); |
1533 | 0 | MEM_INIT(LZ4_stream, 0, sizeof(LZ4_stream_t_internal)); |
1534 | 0 | } |
1535 | | |
1536 | 0 | void LZ4_resetStream_fast(LZ4_stream_t* ctx) { |
1537 | 0 | LZ4_prepareTable(&(ctx->internal_donotuse), 0, byU32); |
1538 | 0 | } |
1539 | | |
1540 | | #if !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION) |
1541 | | int LZ4_freeStream (LZ4_stream_t* LZ4_stream) |
1542 | 0 | { |
1543 | 0 | if (!LZ4_stream) return 0; /* support free on NULL */ |
1544 | 0 | DEBUGLOG(5, "LZ4_freeStream %p", LZ4_stream); |
1545 | 0 | FREEMEM(LZ4_stream); |
1546 | 0 | return (0); |
1547 | 0 | } |
1548 | | #endif |
1549 | | |
1550 | | |
1551 | 0 | #define HASH_UNIT sizeof(reg_t) |
1552 | | int LZ4_loadDict (LZ4_stream_t* LZ4_dict, const char* dictionary, int dictSize) |
1553 | 0 | { |
1554 | 0 | LZ4_stream_t_internal* const dict = &LZ4_dict->internal_donotuse; |
1555 | 0 | const tableType_t tableType = byU32; |
1556 | 0 | const BYTE* p = (const BYTE*)dictionary; |
1557 | 0 | const BYTE* const dictEnd = p + dictSize; |
1558 | 0 | U32 idx32; |
1559 | |
|
1560 | 0 | DEBUGLOG(4, "LZ4_loadDict (%i bytes from %p into %p)", dictSize, dictionary, LZ4_dict); |
1561 | | |
1562 | | /* It's necessary to reset the context, |
1563 | | * and not just continue it with prepareTable() |
1564 | | * to avoid any risk of generating overflowing matchIndex |
1565 | | * when compressing using this dictionary */ |
1566 | 0 | LZ4_resetStream(LZ4_dict); |
1567 | | |
1568 | | /* We always increment the offset by 64 KB, since, if the dict is longer, |
1569 | | * we truncate it to the last 64k, and if it's shorter, we still want to |
1570 | | * advance by a whole window length so we can provide the guarantee that |
1571 | | * there are only valid offsets in the window, which allows an optimization |
1572 | | * in LZ4_compress_fast_continue() where it uses noDictIssue even when the |
1573 | | * dictionary isn't a full 64k. */ |
1574 | 0 | dict->currentOffset += 64 KB; |
1575 | |
|
1576 | 0 | if (dictSize < (int)HASH_UNIT) { |
1577 | 0 | return 0; |
1578 | 0 | } |
1579 | | |
1580 | 0 | if ((dictEnd - p) > 64 KB) p = dictEnd - 64 KB; |
1581 | 0 | dict->dictionary = p; |
1582 | 0 | dict->dictSize = (U32)(dictEnd - p); |
1583 | 0 | dict->tableType = (U32)tableType; |
1584 | 0 | idx32 = dict->currentOffset - dict->dictSize; |
1585 | |
|
1586 | 0 | while (p <= dictEnd-HASH_UNIT) { |
1587 | 0 | U32 const h = LZ4_hashPosition(p, tableType); |
1588 | 0 | LZ4_putIndexOnHash(idx32, h, dict->hashTable, tableType); |
1589 | 0 | p+=3; idx32+=3; |
1590 | 0 | } |
1591 | |
|
1592 | 0 | return (int)dict->dictSize; |
1593 | 0 | } |
1594 | | |
1595 | | void LZ4_attach_dictionary(LZ4_stream_t* workingStream, const LZ4_stream_t* dictionaryStream) |
1596 | 0 | { |
1597 | 0 | const LZ4_stream_t_internal* dictCtx = (dictionaryStream == NULL) ? NULL : |
1598 | 0 | &(dictionaryStream->internal_donotuse); |
1599 | |
|
1600 | 0 | DEBUGLOG(4, "LZ4_attach_dictionary (%p, %p, size %u)", |
1601 | 0 | workingStream, dictionaryStream, |
1602 | 0 | dictCtx != NULL ? dictCtx->dictSize : 0); |
1603 | |
|
1604 | 0 | if (dictCtx != NULL) { |
1605 | | /* If the current offset is zero, we will never look in the |
1606 | | * external dictionary context, since there is no value a table |
1607 | | * entry can take that indicate a miss. In that case, we need |
1608 | | * to bump the offset to something non-zero. |
1609 | | */ |
1610 | 0 | if (workingStream->internal_donotuse.currentOffset == 0) { |
1611 | 0 | workingStream->internal_donotuse.currentOffset = 64 KB; |
1612 | 0 | } |
1613 | | |
1614 | | /* Don't actually attach an empty dictionary. |
1615 | | */ |
1616 | 0 | if (dictCtx->dictSize == 0) { |
1617 | 0 | dictCtx = NULL; |
1618 | 0 | } |
1619 | 0 | } |
1620 | 0 | workingStream->internal_donotuse.dictCtx = dictCtx; |
1621 | 0 | } |
1622 | | |
1623 | | |
1624 | | static void LZ4_renormDictT(LZ4_stream_t_internal* LZ4_dict, int nextSize) |
1625 | 0 | { |
1626 | 0 | assert(nextSize >= 0); |
1627 | 0 | if (LZ4_dict->currentOffset + (unsigned)nextSize > 0x80000000) { /* potential ptrdiff_t overflow (32-bits mode) */ |
1628 | | /* rescale hash table */ |
1629 | 0 | U32 const delta = LZ4_dict->currentOffset - 64 KB; |
1630 | 0 | const BYTE* dictEnd = LZ4_dict->dictionary + LZ4_dict->dictSize; |
1631 | 0 | int i; |
1632 | 0 | DEBUGLOG(4, "LZ4_renormDictT"); |
1633 | 0 | for (i=0; i<LZ4_HASH_SIZE_U32; i++) { |
1634 | 0 | if (LZ4_dict->hashTable[i] < delta) LZ4_dict->hashTable[i]=0; |
1635 | 0 | else LZ4_dict->hashTable[i] -= delta; |
1636 | 0 | } |
1637 | 0 | LZ4_dict->currentOffset = 64 KB; |
1638 | 0 | if (LZ4_dict->dictSize > 64 KB) LZ4_dict->dictSize = 64 KB; |
1639 | 0 | LZ4_dict->dictionary = dictEnd - LZ4_dict->dictSize; |
1640 | 0 | } |
1641 | 0 | } |
1642 | | |
1643 | | |
1644 | | int LZ4_compress_fast_continue (LZ4_stream_t* LZ4_stream, |
1645 | | const char* source, char* dest, |
1646 | | int inputSize, int maxOutputSize, |
1647 | | int acceleration) |
1648 | 0 | { |
1649 | 0 | const tableType_t tableType = byU32; |
1650 | 0 | LZ4_stream_t_internal* const streamPtr = &LZ4_stream->internal_donotuse; |
1651 | 0 | const char* dictEnd = streamPtr->dictSize ? (const char*)streamPtr->dictionary + streamPtr->dictSize : NULL; |
1652 | |
|
1653 | 0 | DEBUGLOG(5, "LZ4_compress_fast_continue (inputSize=%i, dictSize=%u)", inputSize, streamPtr->dictSize); |
1654 | |
|
1655 | 0 | LZ4_renormDictT(streamPtr, inputSize); /* fix index overflow */ |
1656 | 0 | if (acceleration < 1) acceleration = LZ4_ACCELERATION_DEFAULT; |
1657 | 0 | if (acceleration > LZ4_ACCELERATION_MAX) acceleration = LZ4_ACCELERATION_MAX; |
1658 | | |
1659 | | /* invalidate tiny dictionaries */ |
1660 | 0 | if ( (streamPtr->dictSize < 4) /* tiny dictionary : not enough for a hash */ |
1661 | 0 | && (dictEnd != source) /* prefix mode */ |
1662 | 0 | && (inputSize > 0) /* tolerance : don't lose history, in case next invocation would use prefix mode */ |
1663 | 0 | && (streamPtr->dictCtx == NULL) /* usingDictCtx */ |
1664 | 0 | ) { |
1665 | 0 | DEBUGLOG(5, "LZ4_compress_fast_continue: dictSize(%u) at addr:%p is too small", streamPtr->dictSize, streamPtr->dictionary); |
1666 | | /* remove dictionary existence from history, to employ faster prefix mode */ |
1667 | 0 | streamPtr->dictSize = 0; |
1668 | 0 | streamPtr->dictionary = (const BYTE*)source; |
1669 | 0 | dictEnd = source; |
1670 | 0 | } |
1671 | | |
1672 | | /* Check overlapping input/dictionary space */ |
1673 | 0 | { const char* const sourceEnd = source + inputSize; |
1674 | 0 | if ((sourceEnd > (const char*)streamPtr->dictionary) && (sourceEnd < dictEnd)) { |
1675 | 0 | streamPtr->dictSize = (U32)(dictEnd - sourceEnd); |
1676 | 0 | if (streamPtr->dictSize > 64 KB) streamPtr->dictSize = 64 KB; |
1677 | 0 | if (streamPtr->dictSize < 4) streamPtr->dictSize = 0; |
1678 | 0 | streamPtr->dictionary = (const BYTE*)dictEnd - streamPtr->dictSize; |
1679 | 0 | } |
1680 | 0 | } |
1681 | | |
1682 | | /* prefix mode : source data follows dictionary */ |
1683 | 0 | if (dictEnd == source) { |
1684 | 0 | if ((streamPtr->dictSize < 64 KB) && (streamPtr->dictSize < streamPtr->currentOffset)) |
1685 | 0 | return LZ4_compress_generic(streamPtr, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, withPrefix64k, dictSmall, acceleration); |
1686 | 0 | else |
1687 | 0 | return LZ4_compress_generic(streamPtr, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, withPrefix64k, noDictIssue, acceleration); |
1688 | 0 | } |
1689 | | |
1690 | | /* external dictionary mode */ |
1691 | 0 | { int result; |
1692 | 0 | if (streamPtr->dictCtx) { |
1693 | | /* We depend here on the fact that dictCtx'es (produced by |
1694 | | * LZ4_loadDict) guarantee that their tables contain no references |
1695 | | * to offsets between dictCtx->currentOffset - 64 KB and |
1696 | | * dictCtx->currentOffset - dictCtx->dictSize. This makes it safe |
1697 | | * to use noDictIssue even when the dict isn't a full 64 KB. |
1698 | | */ |
1699 | 0 | if (inputSize > 4 KB) { |
1700 | | /* For compressing large blobs, it is faster to pay the setup |
1701 | | * cost to copy the dictionary's tables into the active context, |
1702 | | * so that the compression loop is only looking into one table. |
1703 | | */ |
1704 | 0 | LZ4_memcpy(streamPtr, streamPtr->dictCtx, sizeof(*streamPtr)); |
1705 | 0 | result = LZ4_compress_generic(streamPtr, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, usingExtDict, noDictIssue, acceleration); |
1706 | 0 | } else { |
1707 | 0 | result = LZ4_compress_generic(streamPtr, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, usingDictCtx, noDictIssue, acceleration); |
1708 | 0 | } |
1709 | 0 | } else { /* small data <= 4 KB */ |
1710 | 0 | if ((streamPtr->dictSize < 64 KB) && (streamPtr->dictSize < streamPtr->currentOffset)) { |
1711 | 0 | result = LZ4_compress_generic(streamPtr, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, usingExtDict, dictSmall, acceleration); |
1712 | 0 | } else { |
1713 | 0 | result = LZ4_compress_generic(streamPtr, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, usingExtDict, noDictIssue, acceleration); |
1714 | 0 | } |
1715 | 0 | } |
1716 | 0 | streamPtr->dictionary = (const BYTE*)source; |
1717 | 0 | streamPtr->dictSize = (U32)inputSize; |
1718 | 0 | return result; |
1719 | 0 | } |
1720 | 0 | } |
1721 | | |
1722 | | |
1723 | | /* Hidden debug function, to force-test external dictionary mode */ |
1724 | | int LZ4_compress_forceExtDict (LZ4_stream_t* LZ4_dict, const char* source, char* dest, int srcSize) |
1725 | 0 | { |
1726 | 0 | LZ4_stream_t_internal* const streamPtr = &LZ4_dict->internal_donotuse; |
1727 | 0 | int result; |
1728 | |
|
1729 | 0 | LZ4_renormDictT(streamPtr, srcSize); |
1730 | |
|
1731 | 0 | if ((streamPtr->dictSize < 64 KB) && (streamPtr->dictSize < streamPtr->currentOffset)) { |
1732 | 0 | result = LZ4_compress_generic(streamPtr, source, dest, srcSize, NULL, 0, notLimited, byU32, usingExtDict, dictSmall, 1); |
1733 | 0 | } else { |
1734 | 0 | result = LZ4_compress_generic(streamPtr, source, dest, srcSize, NULL, 0, notLimited, byU32, usingExtDict, noDictIssue, 1); |
1735 | 0 | } |
1736 | |
|
1737 | 0 | streamPtr->dictionary = (const BYTE*)source; |
1738 | 0 | streamPtr->dictSize = (U32)srcSize; |
1739 | |
|
1740 | 0 | return result; |
1741 | 0 | } |
1742 | | |
1743 | | |
1744 | | /*! LZ4_saveDict() : |
1745 | | * If previously compressed data block is not guaranteed to remain available at its memory location, |
1746 | | * save it into a safer place (char* safeBuffer). |
1747 | | * Note : no need to call LZ4_loadDict() afterwards, dictionary is immediately usable, |
1748 | | * one can therefore call LZ4_compress_fast_continue() right after. |
1749 | | * @return : saved dictionary size in bytes (necessarily <= dictSize), or 0 if error. |
1750 | | */ |
1751 | | int LZ4_saveDict (LZ4_stream_t* LZ4_dict, char* safeBuffer, int dictSize) |
1752 | 0 | { |
1753 | 0 | LZ4_stream_t_internal* const dict = &LZ4_dict->internal_donotuse; |
1754 | |
|
1755 | 0 | DEBUGLOG(5, "LZ4_saveDict : dictSize=%i, safeBuffer=%p", dictSize, safeBuffer); |
1756 | |
|
1757 | 0 | if ((U32)dictSize > 64 KB) { dictSize = 64 KB; } /* useless to define a dictionary > 64 KB */ |
1758 | 0 | if ((U32)dictSize > dict->dictSize) { dictSize = (int)dict->dictSize; } |
1759 | |
|
1760 | 0 | if (safeBuffer == NULL) assert(dictSize == 0); |
1761 | 0 | if (dictSize > 0) { |
1762 | 0 | const BYTE* const previousDictEnd = dict->dictionary + dict->dictSize; |
1763 | 0 | assert(dict->dictionary); |
1764 | 0 | LZ4_memmove(safeBuffer, previousDictEnd - dictSize, (size_t)dictSize); |
1765 | 0 | } |
1766 | | |
1767 | 0 | dict->dictionary = (const BYTE*)safeBuffer; |
1768 | 0 | dict->dictSize = (U32)dictSize; |
1769 | |
|
1770 | 0 | return dictSize; |
1771 | 0 | } |
1772 | | |
1773 | | |
1774 | | |
1775 | | /*-******************************* |
1776 | | * Decompression functions |
1777 | | ********************************/ |
1778 | | |
1779 | | typedef enum { decode_full_block = 0, partial_decode = 1 } earlyEnd_directive; |
1780 | | |
1781 | | #undef MIN |
1782 | 0 | #define MIN(a,b) ( (a) < (b) ? (a) : (b) ) |
1783 | | |
1784 | | |
1785 | | /* variant for decompress_unsafe() |
1786 | | * does not know end of input |
1787 | | * presumes input is well formed |
1788 | | * note : will consume at least one byte */ |
1789 | | static size_t read_long_length_no_check(const BYTE** pp) |
1790 | 0 | { |
1791 | 0 | size_t b, l = 0; |
1792 | 0 | do { b = **pp; (*pp)++; l += b; } while (b==255); |
1793 | 0 | DEBUGLOG(6, "read_long_length_no_check: +length=%zu using %zu input bytes", l, l/255 + 1) |
1794 | 0 | return l; |
1795 | 0 | } |
1796 | | |
1797 | | /* core decoder variant for LZ4_decompress_fast*() |
1798 | | * for legacy support only : these entry points are deprecated. |
1799 | | * - Presumes input is correctly formed (no defense vs malformed inputs) |
1800 | | * - Does not know input size (presume input buffer is "large enough") |
1801 | | * - Decompress a full block (only) |
1802 | | * @return : nb of bytes read from input. |
1803 | | * Note : this variant is not optimized for speed, just for maintenance. |
1804 | | * the goal is to remove support of decompress_fast*() variants by v2.0 |
1805 | | **/ |
1806 | | LZ4_FORCE_INLINE int |
1807 | | LZ4_decompress_unsafe_generic( |
1808 | | const BYTE* const istart, |
1809 | | BYTE* const ostart, |
1810 | | int decompressedSize, |
1811 | | |
1812 | | size_t prefixSize, |
1813 | | const BYTE* const dictStart, /* only if dict==usingExtDict */ |
1814 | | const size_t dictSize /* note: =0 if dictStart==NULL */ |
1815 | | ) |
1816 | 0 | { |
1817 | 0 | const BYTE* ip = istart; |
1818 | 0 | BYTE* op = (BYTE*)ostart; |
1819 | 0 | BYTE* const oend = ostart + decompressedSize; |
1820 | 0 | const BYTE* const prefixStart = ostart - prefixSize; |
1821 | |
|
1822 | 0 | DEBUGLOG(5, "LZ4_decompress_unsafe_generic"); |
1823 | 0 | if (dictStart == NULL) assert(dictSize == 0); |
1824 | | |
1825 | 0 | while (1) { |
1826 | | /* start new sequence */ |
1827 | 0 | unsigned token = *ip++; |
1828 | | |
1829 | | /* literals */ |
1830 | 0 | { size_t ll = token >> ML_BITS; |
1831 | 0 | if (ll==15) { |
1832 | | /* long literal length */ |
1833 | 0 | ll += read_long_length_no_check(&ip); |
1834 | 0 | } |
1835 | 0 | if ((size_t)(oend-op) < ll) return -1; /* output buffer overflow */ |
1836 | 0 | LZ4_memmove(op, ip, ll); /* support in-place decompression */ |
1837 | 0 | op += ll; |
1838 | 0 | ip += ll; |
1839 | 0 | if ((size_t)(oend-op) < MFLIMIT) { |
1840 | 0 | if (op==oend) break; /* end of block */ |
1841 | 0 | DEBUGLOG(5, "invalid: literals end at distance %zi from end of block", oend-op); |
1842 | | /* incorrect end of block : |
1843 | | * last match must start at least MFLIMIT==12 bytes before end of output block */ |
1844 | 0 | return -1; |
1845 | 0 | } } |
1846 | | |
1847 | | /* match */ |
1848 | 0 | { size_t ml = token & 15; |
1849 | 0 | size_t const offset = LZ4_readLE16(ip); |
1850 | 0 | ip+=2; |
1851 | |
|
1852 | 0 | if (ml==15) { |
1853 | | /* long literal length */ |
1854 | 0 | ml += read_long_length_no_check(&ip); |
1855 | 0 | } |
1856 | 0 | ml += MINMATCH; |
1857 | |
|
1858 | 0 | if ((size_t)(oend-op) < ml) return -1; /* output buffer overflow */ |
1859 | | |
1860 | 0 | { const BYTE* match = op - offset; |
1861 | | |
1862 | | /* out of range */ |
1863 | 0 | if (offset > (size_t)(op - prefixStart) + dictSize) { |
1864 | 0 | DEBUGLOG(6, "offset out of range"); |
1865 | 0 | return -1; |
1866 | 0 | } |
1867 | | |
1868 | | /* check special case : extDict */ |
1869 | 0 | if (offset > (size_t)(op - prefixStart)) { |
1870 | | /* extDict scenario */ |
1871 | 0 | const BYTE* const dictEnd = dictStart + dictSize; |
1872 | 0 | const BYTE* extMatch = dictEnd - (offset - (size_t)(op-prefixStart)); |
1873 | 0 | size_t const extml = (size_t)(dictEnd - extMatch); |
1874 | 0 | if (extml > ml) { |
1875 | | /* match entirely within extDict */ |
1876 | 0 | LZ4_memmove(op, extMatch, ml); |
1877 | 0 | op += ml; |
1878 | 0 | ml = 0; |
1879 | 0 | } else { |
1880 | | /* match split between extDict & prefix */ |
1881 | 0 | LZ4_memmove(op, extMatch, extml); |
1882 | 0 | op += extml; |
1883 | 0 | ml -= extml; |
1884 | 0 | } |
1885 | 0 | match = prefixStart; |
1886 | 0 | } |
1887 | | |
1888 | | /* match copy - slow variant, supporting overlap copy */ |
1889 | 0 | { size_t u; |
1890 | 0 | for (u=0; u<ml; u++) { |
1891 | 0 | op[u] = match[u]; |
1892 | 0 | } } } |
1893 | 0 | op += ml; |
1894 | 0 | if ((size_t)(oend-op) < LASTLITERALS) { |
1895 | 0 | DEBUGLOG(5, "invalid: match ends at distance %zi from end of block", oend-op); |
1896 | | /* incorrect end of block : |
1897 | | * last match must stop at least LASTLITERALS==5 bytes before end of output block */ |
1898 | 0 | return -1; |
1899 | 0 | } |
1900 | 0 | } /* match */ |
1901 | 0 | } /* main loop */ |
1902 | 0 | return (int)(ip - istart); |
1903 | 0 | } |
1904 | | |
1905 | | |
1906 | | /* Read the variable-length literal or match length. |
1907 | | * |
1908 | | * @ip : input pointer |
1909 | | * @ilimit : position after which if length is not decoded, the input is necessarily corrupted. |
1910 | | * @initial_check - check ip >= ipmax before start of loop. Returns initial_error if so. |
1911 | | * @error (output) - error code. Must be set to 0 before call. |
1912 | | **/ |
1913 | | typedef size_t Rvl_t; |
1914 | | static const Rvl_t rvl_error = (Rvl_t)(-1); |
1915 | | LZ4_FORCE_INLINE Rvl_t |
1916 | | read_variable_length(const BYTE** ip, const BYTE* ilimit, |
1917 | | int initial_check) |
1918 | 27.6k | { |
1919 | 27.6k | Rvl_t s, length = 0; |
1920 | 27.6k | assert(ip != NULL); |
1921 | 27.6k | assert(*ip != NULL); |
1922 | 27.6k | assert(ilimit != NULL); |
1923 | 27.6k | if (initial_check && unlikely((*ip) >= ilimit)) { /* read limit reached */ |
1924 | 0 | return rvl_error; |
1925 | 0 | } |
1926 | 139k | do { |
1927 | 139k | s = **ip; |
1928 | 139k | (*ip)++; |
1929 | 139k | length += s; |
1930 | 139k | if (unlikely((*ip) > ilimit)) { /* read limit reached */ |
1931 | 0 | return rvl_error; |
1932 | 0 | } |
1933 | | /* accumulator overflow detection (32-bit mode only) */ |
1934 | 139k | if ((sizeof(length)<8) && unlikely(length > ((Rvl_t)(-1)/2)) ) { |
1935 | 0 | return rvl_error; |
1936 | 0 | } |
1937 | 139k | } while (s==255); |
1938 | | |
1939 | 27.6k | return length; |
1940 | 27.6k | } |
1941 | | |
1942 | | /*! LZ4_decompress_generic() : |
1943 | | * This generic decompression function covers all use cases. |
1944 | | * It shall be instantiated several times, using different sets of directives. |
1945 | | * Note that it is important for performance that this function really get inlined, |
1946 | | * in order to remove useless branches during compilation optimization. |
1947 | | */ |
1948 | | LZ4_FORCE_INLINE int |
1949 | | LZ4_decompress_generic( |
1950 | | const char* const src, |
1951 | | char* const dst, |
1952 | | int srcSize, |
1953 | | int outputSize, /* If endOnInput==endOnInputSize, this value is `dstCapacity` */ |
1954 | | |
1955 | | earlyEnd_directive partialDecoding, /* full, partial */ |
1956 | | dict_directive dict, /* noDict, withPrefix64k, usingExtDict */ |
1957 | | const BYTE* const lowPrefix, /* always <= dst, == dst when no prefix */ |
1958 | | const BYTE* const dictStart, /* only if dict==usingExtDict */ |
1959 | | const size_t dictSize /* note : = 0 if noDict */ |
1960 | | ) |
1961 | 10.1k | { |
1962 | 10.1k | if ((src == NULL) || (outputSize < 0)) { return -1; } |
1963 | | |
1964 | 10.1k | { const BYTE* ip = (const BYTE*) src; |
1965 | 10.1k | const BYTE* const iend = ip + srcSize; |
1966 | | |
1967 | 10.1k | BYTE* op = (BYTE*) dst; |
1968 | 10.1k | BYTE* const oend = op + outputSize; |
1969 | 10.1k | BYTE* cpy; |
1970 | | |
1971 | 10.1k | const BYTE* const dictEnd = (dictStart == NULL) ? NULL : dictStart + dictSize; |
1972 | | |
1973 | 10.1k | const int checkOffset = (dictSize < (int)(64 KB)); |
1974 | | |
1975 | | |
1976 | | /* Set up the "end" pointers for the shortcut. */ |
1977 | 10.1k | const BYTE* const shortiend = iend - 14 /*maxLL*/ - 2 /*offset*/; |
1978 | 10.1k | const BYTE* const shortoend = oend - 14 /*maxLL*/ - 18 /*maxML*/; |
1979 | | |
1980 | 10.1k | const BYTE* match; |
1981 | 10.1k | size_t offset; |
1982 | 10.1k | unsigned token; |
1983 | 10.1k | size_t length; |
1984 | | |
1985 | | |
1986 | 10.1k | DEBUGLOG(5, "LZ4_decompress_generic (srcSize:%i, dstSize:%i)", srcSize, outputSize); |
1987 | | |
1988 | | /* Special cases */ |
1989 | 10.1k | assert(lowPrefix <= op); |
1990 | 10.1k | if (unlikely(outputSize==0)) { |
1991 | | /* Empty output buffer */ |
1992 | 0 | if (partialDecoding) return 0; |
1993 | 0 | return ((srcSize==1) && (*ip==0)) ? 0 : -1; |
1994 | 0 | } |
1995 | 10.1k | if (unlikely(srcSize==0)) { return -1; } |
1996 | | |
1997 | | /* LZ4_FAST_DEC_LOOP: |
1998 | | * designed for modern OoO performance cpus, |
1999 | | * where copying reliably 32-bytes is preferable to an unpredictable branch. |
2000 | | * note : fast loop may show a regression for some client arm chips. */ |
2001 | 10.1k | #if LZ4_FAST_DEC_LOOP |
2002 | 10.1k | if ((oend - op) < FASTLOOP_SAFE_DISTANCE) { |
2003 | 5.19k | DEBUGLOG(6, "skip fast decode loop"); |
2004 | 5.19k | goto safe_decode; |
2005 | 5.19k | } |
2006 | | |
2007 | | /* Fast loop : decode sequences as long as output < oend-FASTLOOP_SAFE_DISTANCE */ |
2008 | 4.96k | DEBUGLOG(6, "using fast decode loop"); |
2009 | 42.5k | while (1) { |
2010 | | /* Main fastloop assertion: We can always wildcopy FASTLOOP_SAFE_DISTANCE */ |
2011 | 42.5k | assert(oend - op >= FASTLOOP_SAFE_DISTANCE); |
2012 | 42.5k | assert(ip < iend); |
2013 | 42.5k | token = *ip++; |
2014 | 42.5k | length = token >> ML_BITS; /* literal length */ |
2015 | | |
2016 | | /* decode literal length */ |
2017 | 42.5k | if (length == RUN_MASK) { |
2018 | 1.36k | size_t const addl = read_variable_length(&ip, iend-RUN_MASK, 1); |
2019 | 1.36k | if (addl == rvl_error) { |
2020 | 0 | DEBUGLOG(6, "error reading long literal length"); |
2021 | 0 | goto _output_error; |
2022 | 0 | } |
2023 | 1.36k | length += addl; |
2024 | 1.36k | if (unlikely((uptrval)(op)+length<(uptrval)(op))) { goto _output_error; } /* overflow detection */ |
2025 | 1.36k | if (unlikely((uptrval)(ip)+length<(uptrval)(ip))) { goto _output_error; } /* overflow detection */ |
2026 | | |
2027 | | /* copy literals */ |
2028 | 1.36k | cpy = op+length; |
2029 | 1.36k | LZ4_STATIC_ASSERT(MFLIMIT >= WILDCOPYLENGTH); |
2030 | 1.36k | if ((cpy>oend-32) || (ip+length>iend-32)) { goto safe_literal_copy; } |
2031 | 709 | LZ4_wildCopy32(op, ip, cpy); |
2032 | 709 | ip += length; op = cpy; |
2033 | 41.2k | } else { |
2034 | 41.2k | cpy = op+length; |
2035 | 41.2k | DEBUGLOG(7, "copy %u bytes in a 16-bytes stripe", (unsigned)length); |
2036 | | /* We don't need to check oend, since we check it once for each loop below */ |
2037 | 41.2k | if (ip > iend-(16 + 1/*max lit + offset + nextToken*/)) { goto safe_literal_copy; } |
2038 | | /* Literals can only be <= 14, but hope compilers optimize better when copy by a register size */ |
2039 | 39.4k | LZ4_memcpy(op, ip, 16); |
2040 | 39.4k | ip += length; op = cpy; |
2041 | 39.4k | } |
2042 | | |
2043 | | /* get offset */ |
2044 | 40.1k | offset = LZ4_readLE16(ip); ip+=2; |
2045 | 40.1k | DEBUGLOG(6, " offset = %zu", offset); |
2046 | 40.1k | match = op - offset; |
2047 | 40.1k | assert(match <= op); /* overflow check */ |
2048 | | |
2049 | | /* get matchlength */ |
2050 | 40.1k | length = token & ML_MASK; |
2051 | | |
2052 | 40.1k | if (length == ML_MASK) { |
2053 | 22.9k | size_t const addl = read_variable_length(&ip, iend - LASTLITERALS + 1, 0); |
2054 | 22.9k | if (addl == rvl_error) { |
2055 | 0 | DEBUGLOG(6, "error reading long match length"); |
2056 | 0 | goto _output_error; |
2057 | 0 | } |
2058 | 22.9k | length += addl; |
2059 | 22.9k | length += MINMATCH; |
2060 | 22.9k | if (unlikely((uptrval)(op)+length<(uptrval)op)) { goto _output_error; } /* overflow detection */ |
2061 | 22.9k | if ((checkOffset) && (unlikely(match + dictSize < lowPrefix))) { |
2062 | 0 | DEBUGLOG(6, "Error : offset outside buffers"); |
2063 | 0 | goto _output_error; |
2064 | 0 | } |
2065 | 22.9k | if (op + length >= oend - FASTLOOP_SAFE_DISTANCE) { |
2066 | 1.67k | goto safe_match_copy; |
2067 | 1.67k | } |
2068 | 22.9k | } else { |
2069 | 17.1k | length += MINMATCH; |
2070 | 17.1k | if (op + length >= oend - FASTLOOP_SAFE_DISTANCE) { |
2071 | 851 | goto safe_match_copy; |
2072 | 851 | } |
2073 | | |
2074 | | /* Fastpath check: skip LZ4_wildCopy32 when true */ |
2075 | 16.3k | if ((dict == withPrefix64k) || (match >= lowPrefix)) { |
2076 | 16.3k | if (offset >= 8) { |
2077 | 8.89k | assert(match >= lowPrefix); |
2078 | 8.89k | assert(match <= op); |
2079 | 8.89k | assert(op + 18 <= oend); |
2080 | | |
2081 | 8.89k | LZ4_memcpy(op, match, 8); |
2082 | 8.89k | LZ4_memcpy(op+8, match+8, 8); |
2083 | 8.89k | LZ4_memcpy(op+16, match+16, 2); |
2084 | 8.89k | op += length; |
2085 | 8.89k | continue; |
2086 | 8.89k | } } } |
2087 | | |
2088 | 28.7k | if ( checkOffset && (unlikely(match + dictSize < lowPrefix)) ) { |
2089 | 0 | DEBUGLOG(6, "Error : pos=%zi, offset=%zi => outside buffers", op-lowPrefix, op-match); |
2090 | 0 | goto _output_error; |
2091 | 0 | } |
2092 | | /* match starting within external dictionary */ |
2093 | 28.7k | if ((dict==usingExtDict) && (match < lowPrefix)) { |
2094 | 0 | assert(dictEnd != NULL); |
2095 | 0 | if (unlikely(op+length > oend-LASTLITERALS)) { |
2096 | 0 | if (partialDecoding) { |
2097 | 0 | DEBUGLOG(7, "partialDecoding: dictionary match, close to dstEnd"); |
2098 | 0 | length = MIN(length, (size_t)(oend-op)); |
2099 | 0 | } else { |
2100 | 0 | DEBUGLOG(6, "end-of-block condition violated") |
2101 | 0 | goto _output_error; |
2102 | 0 | } } |
2103 | | |
2104 | 0 | if (length <= (size_t)(lowPrefix-match)) { |
2105 | | /* match fits entirely within external dictionary : just copy */ |
2106 | 0 | LZ4_memmove(op, dictEnd - (lowPrefix-match), length); |
2107 | 0 | op += length; |
2108 | 0 | } else { |
2109 | | /* match stretches into both external dictionary and current block */ |
2110 | 0 | size_t const copySize = (size_t)(lowPrefix - match); |
2111 | 0 | size_t const restSize = length - copySize; |
2112 | 0 | LZ4_memcpy(op, dictEnd - copySize, copySize); |
2113 | 0 | op += copySize; |
2114 | 0 | if (restSize > (size_t)(op - lowPrefix)) { /* overlap copy */ |
2115 | 0 | BYTE* const endOfMatch = op + restSize; |
2116 | 0 | const BYTE* copyFrom = lowPrefix; |
2117 | 0 | while (op < endOfMatch) { *op++ = *copyFrom++; } |
2118 | 0 | } else { |
2119 | 0 | LZ4_memcpy(op, lowPrefix, restSize); |
2120 | 0 | op += restSize; |
2121 | 0 | } } |
2122 | 0 | continue; |
2123 | 0 | } |
2124 | | |
2125 | | /* copy match within block */ |
2126 | 28.7k | cpy = op + length; |
2127 | | |
2128 | 28.7k | assert((op <= oend) && (oend-op >= 32)); |
2129 | 28.7k | if (unlikely(offset<16)) { |
2130 | 14.8k | LZ4_memcpy_using_offset(op, match, cpy, offset); |
2131 | 14.8k | } else { |
2132 | 13.8k | LZ4_wildCopy32(op, match, cpy); |
2133 | 13.8k | } |
2134 | | |
2135 | 28.7k | op = cpy; /* wildcopy correction */ |
2136 | 28.7k | } |
2137 | 5.19k | safe_decode: |
2138 | 5.19k | #endif |
2139 | | |
2140 | | /* Main Loop : decode remaining sequences where output < FASTLOOP_SAFE_DISTANCE */ |
2141 | 5.19k | DEBUGLOG(6, "using safe decode loop"); |
2142 | 24.2k | while (1) { |
2143 | 24.2k | assert(ip < iend); |
2144 | 24.2k | token = *ip++; |
2145 | 24.2k | length = token >> ML_BITS; /* literal length */ |
2146 | | |
2147 | | /* A two-stage shortcut for the most common case: |
2148 | | * 1) If the literal length is 0..14, and there is enough space, |
2149 | | * enter the shortcut and copy 16 bytes on behalf of the literals |
2150 | | * (in the fast mode, only 8 bytes can be safely copied this way). |
2151 | | * 2) Further if the match length is 4..18, copy 18 bytes in a similar |
2152 | | * manner; but we ensure that there's enough space in the output for |
2153 | | * those 18 bytes earlier, upon entering the shortcut (in other words, |
2154 | | * there is a combined check for both stages). |
2155 | | */ |
2156 | 24.2k | if ( (length != RUN_MASK) |
2157 | | /* strictly "less than" on input, to re-enter the loop with at least one byte */ |
2158 | 24.2k | && likely((ip < shortiend) & (op <= shortoend)) ) { |
2159 | | /* Copy the literals */ |
2160 | 4.62k | LZ4_memcpy(op, ip, 16); |
2161 | 4.62k | op += length; ip += length; |
2162 | | |
2163 | | /* The second stage: prepare for match copying, decode full info. |
2164 | | * If it doesn't work out, the info won't be wasted. */ |
2165 | 4.62k | length = token & ML_MASK; /* match length */ |
2166 | 4.62k | offset = LZ4_readLE16(ip); ip += 2; |
2167 | 4.62k | match = op - offset; |
2168 | 4.62k | assert(match <= op); /* check overflow */ |
2169 | | |
2170 | | /* Do not deal with overlapping matches. */ |
2171 | 4.62k | if ( (length != ML_MASK) |
2172 | 4.62k | && (offset >= 8) |
2173 | 4.62k | && (dict==withPrefix64k || match >= lowPrefix) ) { |
2174 | | /* Copy the match. */ |
2175 | 2.04k | LZ4_memcpy(op + 0, match + 0, 8); |
2176 | 2.04k | LZ4_memcpy(op + 8, match + 8, 8); |
2177 | 2.04k | LZ4_memcpy(op +16, match +16, 2); |
2178 | 2.04k | op += length + MINMATCH; |
2179 | | /* Both stages worked, load the next token. */ |
2180 | 2.04k | continue; |
2181 | 2.04k | } |
2182 | | |
2183 | | /* The second stage didn't work out, but the info is ready. |
2184 | | * Propel it right to the point of match copying. */ |
2185 | 2.57k | goto _copy_match; |
2186 | 4.62k | } |
2187 | | |
2188 | | /* decode literal length */ |
2189 | 19.6k | if (length == RUN_MASK) { |
2190 | 565 | size_t const addl = read_variable_length(&ip, iend-RUN_MASK, 1); |
2191 | 565 | if (addl == rvl_error) { goto _output_error; } |
2192 | 565 | length += addl; |
2193 | 565 | if (unlikely((uptrval)(op)+length<(uptrval)(op))) { goto _output_error; } /* overflow detection */ |
2194 | 565 | if (unlikely((uptrval)(ip)+length<(uptrval)(ip))) { goto _output_error; } /* overflow detection */ |
2195 | 565 | } |
2196 | | |
2197 | | /* copy literals */ |
2198 | 19.6k | cpy = op+length; |
2199 | 19.6k | #if LZ4_FAST_DEC_LOOP |
2200 | 22.0k | safe_literal_copy: |
2201 | 22.0k | #endif |
2202 | 22.0k | LZ4_STATIC_ASSERT(MFLIMIT >= WILDCOPYLENGTH); |
2203 | 22.0k | if ((cpy>oend-MFLIMIT) || (ip+length>iend-(2+1+LASTLITERALS))) { |
2204 | | /* We've either hit the input parsing restriction or the output parsing restriction. |
2205 | | * In the normal scenario, decoding a full block, it must be the last sequence, |
2206 | | * otherwise it's an error (invalid input or dimensions). |
2207 | | * In partialDecoding scenario, it's necessary to ensure there is no buffer overflow. |
2208 | | */ |
2209 | 10.1k | if (partialDecoding) { |
2210 | | /* Since we are partial decoding we may be in this block because of the output parsing |
2211 | | * restriction, which is not valid since the output buffer is allowed to be undersized. |
2212 | | */ |
2213 | 0 | DEBUGLOG(7, "partialDecoding: copying literals, close to input or output end") |
2214 | 0 | DEBUGLOG(7, "partialDecoding: literal length = %u", (unsigned)length); |
2215 | 0 | DEBUGLOG(7, "partialDecoding: remaining space in dstBuffer : %i", (int)(oend - op)); |
2216 | 0 | DEBUGLOG(7, "partialDecoding: remaining space in srcBuffer : %i", (int)(iend - ip)); |
2217 | | /* Finishing in the middle of a literals segment, |
2218 | | * due to lack of input. |
2219 | | */ |
2220 | 0 | if (ip+length > iend) { |
2221 | 0 | length = (size_t)(iend-ip); |
2222 | 0 | cpy = op + length; |
2223 | 0 | } |
2224 | | /* Finishing in the middle of a literals segment, |
2225 | | * due to lack of output space. |
2226 | | */ |
2227 | 0 | if (cpy > oend) { |
2228 | 0 | cpy = oend; |
2229 | 0 | assert(op<=oend); |
2230 | 0 | length = (size_t)(oend-op); |
2231 | 0 | } |
2232 | 10.1k | } else { |
2233 | | /* We must be on the last sequence (or invalid) because of the parsing limitations |
2234 | | * so check that we exactly consume the input and don't overrun the output buffer. |
2235 | | */ |
2236 | 10.1k | if ((ip+length != iend) || (cpy > oend)) { |
2237 | 0 | DEBUGLOG(6, "should have been last run of literals") |
2238 | 0 | DEBUGLOG(6, "ip(%p) + length(%i) = %p != iend (%p)", ip, (int)length, ip+length, iend); |
2239 | 0 | DEBUGLOG(6, "or cpy(%p) > oend(%p)", cpy, oend); |
2240 | 0 | goto _output_error; |
2241 | 0 | } |
2242 | 10.1k | } |
2243 | 10.1k | LZ4_memmove(op, ip, length); /* supports overlapping memory regions, for in-place decompression scenarios */ |
2244 | 10.1k | ip += length; |
2245 | 10.1k | op += length; |
2246 | | /* Necessarily EOF when !partialDecoding. |
2247 | | * When partialDecoding, it is EOF if we've either |
2248 | | * filled the output buffer or |
2249 | | * can't proceed with reading an offset for following match. |
2250 | | */ |
2251 | 10.1k | if (!partialDecoding || (cpy == oend) || (ip >= (iend-2))) { |
2252 | 10.1k | break; |
2253 | 10.1k | } |
2254 | 11.8k | } else { |
2255 | 11.8k | LZ4_wildCopy8(op, ip, cpy); /* can overwrite up to 8 bytes beyond cpy */ |
2256 | 11.8k | ip += length; op = cpy; |
2257 | 11.8k | } |
2258 | | |
2259 | | /* get offset */ |
2260 | 11.8k | offset = LZ4_readLE16(ip); ip+=2; |
2261 | 11.8k | match = op - offset; |
2262 | | |
2263 | | /* get matchlength */ |
2264 | 11.8k | length = token & ML_MASK; |
2265 | | |
2266 | 14.4k | _copy_match: |
2267 | 14.4k | if (length == ML_MASK) { |
2268 | 2.75k | size_t const addl = read_variable_length(&ip, iend - LASTLITERALS + 1, 0); |
2269 | 2.75k | if (addl == rvl_error) { goto _output_error; } |
2270 | 2.75k | length += addl; |
2271 | 2.75k | if (unlikely((uptrval)(op)+length<(uptrval)op)) goto _output_error; /* overflow detection */ |
2272 | 2.75k | } |
2273 | 14.4k | length += MINMATCH; |
2274 | | |
2275 | 14.4k | #if LZ4_FAST_DEC_LOOP |
2276 | 16.9k | safe_match_copy: |
2277 | 16.9k | #endif |
2278 | 16.9k | if ((checkOffset) && (unlikely(match + dictSize < lowPrefix))) goto _output_error; /* Error : offset outside buffers */ |
2279 | | /* match starting within external dictionary */ |
2280 | 16.9k | if ((dict==usingExtDict) && (match < lowPrefix)) { |
2281 | 0 | assert(dictEnd != NULL); |
2282 | 0 | if (unlikely(op+length > oend-LASTLITERALS)) { |
2283 | 0 | if (partialDecoding) length = MIN(length, (size_t)(oend-op)); |
2284 | 0 | else goto _output_error; /* doesn't respect parsing restriction */ |
2285 | 0 | } |
2286 | | |
2287 | 0 | if (length <= (size_t)(lowPrefix-match)) { |
2288 | | /* match fits entirely within external dictionary : just copy */ |
2289 | 0 | LZ4_memmove(op, dictEnd - (lowPrefix-match), length); |
2290 | 0 | op += length; |
2291 | 0 | } else { |
2292 | | /* match stretches into both external dictionary and current block */ |
2293 | 0 | size_t const copySize = (size_t)(lowPrefix - match); |
2294 | 0 | size_t const restSize = length - copySize; |
2295 | 0 | LZ4_memcpy(op, dictEnd - copySize, copySize); |
2296 | 0 | op += copySize; |
2297 | 0 | if (restSize > (size_t)(op - lowPrefix)) { /* overlap copy */ |
2298 | 0 | BYTE* const endOfMatch = op + restSize; |
2299 | 0 | const BYTE* copyFrom = lowPrefix; |
2300 | 0 | while (op < endOfMatch) *op++ = *copyFrom++; |
2301 | 0 | } else { |
2302 | 0 | LZ4_memcpy(op, lowPrefix, restSize); |
2303 | 0 | op += restSize; |
2304 | 0 | } } |
2305 | 0 | continue; |
2306 | 0 | } |
2307 | 16.9k | assert(match >= lowPrefix); |
2308 | | |
2309 | | /* copy match within block */ |
2310 | 16.9k | cpy = op + length; |
2311 | | |
2312 | | /* partialDecoding : may end anywhere within the block */ |
2313 | 16.9k | assert(op<=oend); |
2314 | 16.9k | if (partialDecoding && (cpy > oend-MATCH_SAFEGUARD_DISTANCE)) { |
2315 | 0 | size_t const mlen = MIN(length, (size_t)(oend-op)); |
2316 | 0 | const BYTE* const matchEnd = match + mlen; |
2317 | 0 | BYTE* const copyEnd = op + mlen; |
2318 | 0 | if (matchEnd > op) { /* overlap copy */ |
2319 | 0 | while (op < copyEnd) { *op++ = *match++; } |
2320 | 0 | } else { |
2321 | 0 | LZ4_memcpy(op, match, mlen); |
2322 | 0 | } |
2323 | 0 | op = copyEnd; |
2324 | 0 | if (op == oend) { break; } |
2325 | 0 | continue; |
2326 | 0 | } |
2327 | | |
2328 | 16.9k | if (unlikely(offset<8)) { |
2329 | 10.8k | LZ4_write32(op, 0); /* silence msan warning when offset==0 */ |
2330 | 10.8k | op[0] = match[0]; |
2331 | 10.8k | op[1] = match[1]; |
2332 | 10.8k | op[2] = match[2]; |
2333 | 10.8k | op[3] = match[3]; |
2334 | 10.8k | match += inc32table[offset]; |
2335 | 10.8k | LZ4_memcpy(op+4, match, 4); |
2336 | 10.8k | match -= dec64table[offset]; |
2337 | 10.8k | } else { |
2338 | 6.14k | LZ4_memcpy(op, match, 8); |
2339 | 6.14k | match += 8; |
2340 | 6.14k | } |
2341 | 16.9k | op += 8; |
2342 | | |
2343 | 16.9k | if (unlikely(cpy > oend-MATCH_SAFEGUARD_DISTANCE)) { |
2344 | 5.83k | BYTE* const oCopyLimit = oend - (WILDCOPYLENGTH-1); |
2345 | 5.83k | if (cpy > oend-LASTLITERALS) { goto _output_error; } /* Error : last LASTLITERALS bytes must be literals (uncompressed) */ |
2346 | 5.83k | if (op < oCopyLimit) { |
2347 | 3.02k | LZ4_wildCopy8(op, match, oCopyLimit); |
2348 | 3.02k | match += oCopyLimit - op; |
2349 | 3.02k | op = oCopyLimit; |
2350 | 3.02k | } |
2351 | 10.5k | while (op < cpy) { *op++ = *match++; } |
2352 | 11.1k | } else { |
2353 | 11.1k | LZ4_memcpy(op, match, 8); |
2354 | 11.1k | if (length > 16) { LZ4_wildCopy8(op+8, match+8, cpy); } |
2355 | 11.1k | } |
2356 | 16.9k | op = cpy; /* wildcopy correction */ |
2357 | 16.9k | } |
2358 | | |
2359 | | /* end of decoding */ |
2360 | 10.1k | DEBUGLOG(5, "decoded %i bytes", (int) (((char*)op)-dst)); |
2361 | 10.1k | return (int) (((char*)op)-dst); /* Nb of output bytes decoded */ |
2362 | | |
2363 | | /* Overflow error detected */ |
2364 | 0 | _output_error: |
2365 | 0 | return (int) (-(((const char*)ip)-src))-1; |
2366 | 5.19k | } |
2367 | 5.19k | } |
2368 | | |
2369 | | |
2370 | | /*===== Instantiate the API decoding functions. =====*/ |
2371 | | |
2372 | | LZ4_FORCE_O2 |
2373 | | int LZ4_decompress_safe(const char* source, char* dest, int compressedSize, int maxDecompressedSize) |
2374 | 10.1k | { |
2375 | 10.1k | return LZ4_decompress_generic(source, dest, compressedSize, maxDecompressedSize, |
2376 | 10.1k | decode_full_block, noDict, |
2377 | 10.1k | (BYTE*)dest, NULL, 0); |
2378 | 10.1k | } |
2379 | | |
2380 | | LZ4_FORCE_O2 |
2381 | | int LZ4_decompress_safe_partial(const char* src, char* dst, int compressedSize, int targetOutputSize, int dstCapacity) |
2382 | 0 | { |
2383 | 0 | dstCapacity = MIN(targetOutputSize, dstCapacity); |
2384 | 0 | return LZ4_decompress_generic(src, dst, compressedSize, dstCapacity, |
2385 | 0 | partial_decode, |
2386 | 0 | noDict, (BYTE*)dst, NULL, 0); |
2387 | 0 | } |
2388 | | |
2389 | | LZ4_FORCE_O2 |
2390 | | int LZ4_decompress_fast(const char* source, char* dest, int originalSize) |
2391 | 0 | { |
2392 | 0 | DEBUGLOG(5, "LZ4_decompress_fast"); |
2393 | 0 | return LZ4_decompress_unsafe_generic( |
2394 | 0 | (const BYTE*)source, (BYTE*)dest, originalSize, |
2395 | 0 | 0, NULL, 0); |
2396 | 0 | } |
2397 | | |
2398 | | /*===== Instantiate a few more decoding cases, used more than once. =====*/ |
2399 | | |
2400 | | LZ4_FORCE_O2 /* Exported, an obsolete API function. */ |
2401 | | int LZ4_decompress_safe_withPrefix64k(const char* source, char* dest, int compressedSize, int maxOutputSize) |
2402 | 0 | { |
2403 | 0 | return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, |
2404 | 0 | decode_full_block, withPrefix64k, |
2405 | 0 | (BYTE*)dest - 64 KB, NULL, 0); |
2406 | 0 | } |
2407 | | |
2408 | | LZ4_FORCE_O2 |
2409 | | static int LZ4_decompress_safe_partial_withPrefix64k(const char* source, char* dest, int compressedSize, int targetOutputSize, int dstCapacity) |
2410 | 0 | { |
2411 | 0 | dstCapacity = MIN(targetOutputSize, dstCapacity); |
2412 | 0 | return LZ4_decompress_generic(source, dest, compressedSize, dstCapacity, |
2413 | 0 | partial_decode, withPrefix64k, |
2414 | 0 | (BYTE*)dest - 64 KB, NULL, 0); |
2415 | 0 | } |
2416 | | |
2417 | | /* Another obsolete API function, paired with the previous one. */ |
2418 | | int LZ4_decompress_fast_withPrefix64k(const char* source, char* dest, int originalSize) |
2419 | 0 | { |
2420 | 0 | return LZ4_decompress_unsafe_generic( |
2421 | 0 | (const BYTE*)source, (BYTE*)dest, originalSize, |
2422 | 0 | 64 KB, NULL, 0); |
2423 | 0 | } |
2424 | | |
2425 | | LZ4_FORCE_O2 |
2426 | | static int LZ4_decompress_safe_withSmallPrefix(const char* source, char* dest, int compressedSize, int maxOutputSize, |
2427 | | size_t prefixSize) |
2428 | 0 | { |
2429 | 0 | return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, |
2430 | 0 | decode_full_block, noDict, |
2431 | 0 | (BYTE*)dest-prefixSize, NULL, 0); |
2432 | 0 | } |
2433 | | |
2434 | | LZ4_FORCE_O2 |
2435 | | static int LZ4_decompress_safe_partial_withSmallPrefix(const char* source, char* dest, int compressedSize, int targetOutputSize, int dstCapacity, |
2436 | | size_t prefixSize) |
2437 | 0 | { |
2438 | 0 | dstCapacity = MIN(targetOutputSize, dstCapacity); |
2439 | 0 | return LZ4_decompress_generic(source, dest, compressedSize, dstCapacity, |
2440 | 0 | partial_decode, noDict, |
2441 | 0 | (BYTE*)dest-prefixSize, NULL, 0); |
2442 | 0 | } |
2443 | | |
2444 | | LZ4_FORCE_O2 |
2445 | | int LZ4_decompress_safe_forceExtDict(const char* source, char* dest, |
2446 | | int compressedSize, int maxOutputSize, |
2447 | | const void* dictStart, size_t dictSize) |
2448 | 0 | { |
2449 | 0 | DEBUGLOG(5, "LZ4_decompress_safe_forceExtDict"); |
2450 | 0 | return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, |
2451 | 0 | decode_full_block, usingExtDict, |
2452 | 0 | (BYTE*)dest, (const BYTE*)dictStart, dictSize); |
2453 | 0 | } |
2454 | | |
2455 | | LZ4_FORCE_O2 |
2456 | | int LZ4_decompress_safe_partial_forceExtDict(const char* source, char* dest, |
2457 | | int compressedSize, int targetOutputSize, int dstCapacity, |
2458 | | const void* dictStart, size_t dictSize) |
2459 | 0 | { |
2460 | 0 | dstCapacity = MIN(targetOutputSize, dstCapacity); |
2461 | 0 | return LZ4_decompress_generic(source, dest, compressedSize, dstCapacity, |
2462 | 0 | partial_decode, usingExtDict, |
2463 | 0 | (BYTE*)dest, (const BYTE*)dictStart, dictSize); |
2464 | 0 | } |
2465 | | |
2466 | | LZ4_FORCE_O2 |
2467 | | static int LZ4_decompress_fast_extDict(const char* source, char* dest, int originalSize, |
2468 | | const void* dictStart, size_t dictSize) |
2469 | 0 | { |
2470 | 0 | return LZ4_decompress_unsafe_generic( |
2471 | 0 | (const BYTE*)source, (BYTE*)dest, originalSize, |
2472 | 0 | 0, (const BYTE*)dictStart, dictSize); |
2473 | 0 | } |
2474 | | |
2475 | | /* The "double dictionary" mode, for use with e.g. ring buffers: the first part |
2476 | | * of the dictionary is passed as prefix, and the second via dictStart + dictSize. |
2477 | | * These routines are used only once, in LZ4_decompress_*_continue(). |
2478 | | */ |
2479 | | LZ4_FORCE_INLINE |
2480 | | int LZ4_decompress_safe_doubleDict(const char* source, char* dest, int compressedSize, int maxOutputSize, |
2481 | | size_t prefixSize, const void* dictStart, size_t dictSize) |
2482 | 0 | { |
2483 | 0 | return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, |
2484 | 0 | decode_full_block, usingExtDict, |
2485 | 0 | (BYTE*)dest-prefixSize, (const BYTE*)dictStart, dictSize); |
2486 | 0 | } |
2487 | | |
2488 | | /*===== streaming decompression functions =====*/ |
2489 | | |
2490 | | #if !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION) |
2491 | | LZ4_streamDecode_t* LZ4_createStreamDecode(void) |
2492 | 0 | { |
2493 | 0 | LZ4_STATIC_ASSERT(sizeof(LZ4_streamDecode_t) >= sizeof(LZ4_streamDecode_t_internal)); |
2494 | 0 | return (LZ4_streamDecode_t*) ALLOC_AND_ZERO(sizeof(LZ4_streamDecode_t)); |
2495 | 0 | } |
2496 | | |
2497 | | int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream) |
2498 | 0 | { |
2499 | 0 | if (LZ4_stream == NULL) { return 0; } /* support free on NULL */ |
2500 | 0 | FREEMEM(LZ4_stream); |
2501 | 0 | return 0; |
2502 | 0 | } |
2503 | | #endif |
2504 | | |
2505 | | /*! LZ4_setStreamDecode() : |
2506 | | * Use this function to instruct where to find the dictionary. |
2507 | | * This function is not necessary if previous data is still available where it was decoded. |
2508 | | * Loading a size of 0 is allowed (same effect as no dictionary). |
2509 | | * @return : 1 if OK, 0 if error |
2510 | | */ |
2511 | | int LZ4_setStreamDecode (LZ4_streamDecode_t* LZ4_streamDecode, const char* dictionary, int dictSize) |
2512 | 0 | { |
2513 | 0 | LZ4_streamDecode_t_internal* lz4sd = &LZ4_streamDecode->internal_donotuse; |
2514 | 0 | lz4sd->prefixSize = (size_t)dictSize; |
2515 | 0 | if (dictSize) { |
2516 | 0 | assert(dictionary != NULL); |
2517 | 0 | lz4sd->prefixEnd = (const BYTE*) dictionary + dictSize; |
2518 | 0 | } else { |
2519 | 0 | lz4sd->prefixEnd = (const BYTE*) dictionary; |
2520 | 0 | } |
2521 | 0 | lz4sd->externalDict = NULL; |
2522 | 0 | lz4sd->extDictSize = 0; |
2523 | 0 | return 1; |
2524 | 0 | } |
2525 | | |
2526 | | /*! LZ4_decoderRingBufferSize() : |
2527 | | * when setting a ring buffer for streaming decompression (optional scenario), |
2528 | | * provides the minimum size of this ring buffer |
2529 | | * to be compatible with any source respecting maxBlockSize condition. |
2530 | | * Note : in a ring buffer scenario, |
2531 | | * blocks are presumed decompressed next to each other. |
2532 | | * When not enough space remains for next block (remainingSize < maxBlockSize), |
2533 | | * decoding resumes from beginning of ring buffer. |
2534 | | * @return : minimum ring buffer size, |
2535 | | * or 0 if there is an error (invalid maxBlockSize). |
2536 | | */ |
2537 | | int LZ4_decoderRingBufferSize(int maxBlockSize) |
2538 | 0 | { |
2539 | 0 | if (maxBlockSize < 0) return 0; |
2540 | 0 | if (maxBlockSize > LZ4_MAX_INPUT_SIZE) return 0; |
2541 | 0 | if (maxBlockSize < 16) maxBlockSize = 16; |
2542 | 0 | return LZ4_DECODER_RING_BUFFER_SIZE(maxBlockSize); |
2543 | 0 | } |
2544 | | |
2545 | | /* |
2546 | | *_continue() : |
2547 | | These decoding functions allow decompression of multiple blocks in "streaming" mode. |
2548 | | Previously decoded blocks must still be available at the memory position where they were decoded. |
2549 | | If it's not possible, save the relevant part of decoded data into a safe buffer, |
2550 | | and indicate where it stands using LZ4_setStreamDecode() |
2551 | | */ |
2552 | | LZ4_FORCE_O2 |
2553 | | int LZ4_decompress_safe_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* source, char* dest, int compressedSize, int maxOutputSize) |
2554 | 0 | { |
2555 | 0 | LZ4_streamDecode_t_internal* lz4sd = &LZ4_streamDecode->internal_donotuse; |
2556 | 0 | int result; |
2557 | |
|
2558 | 0 | if (lz4sd->prefixSize == 0) { |
2559 | | /* The first call, no dictionary yet. */ |
2560 | 0 | assert(lz4sd->extDictSize == 0); |
2561 | 0 | result = LZ4_decompress_safe(source, dest, compressedSize, maxOutputSize); |
2562 | 0 | if (result <= 0) return result; |
2563 | 0 | lz4sd->prefixSize = (size_t)result; |
2564 | 0 | lz4sd->prefixEnd = (BYTE*)dest + result; |
2565 | 0 | } else if (lz4sd->prefixEnd == (BYTE*)dest) { |
2566 | | /* They're rolling the current segment. */ |
2567 | 0 | if (lz4sd->prefixSize >= 64 KB - 1) |
2568 | 0 | result = LZ4_decompress_safe_withPrefix64k(source, dest, compressedSize, maxOutputSize); |
2569 | 0 | else if (lz4sd->extDictSize == 0) |
2570 | 0 | result = LZ4_decompress_safe_withSmallPrefix(source, dest, compressedSize, maxOutputSize, |
2571 | 0 | lz4sd->prefixSize); |
2572 | 0 | else |
2573 | 0 | result = LZ4_decompress_safe_doubleDict(source, dest, compressedSize, maxOutputSize, |
2574 | 0 | lz4sd->prefixSize, lz4sd->externalDict, lz4sd->extDictSize); |
2575 | 0 | if (result <= 0) return result; |
2576 | 0 | lz4sd->prefixSize += (size_t)result; |
2577 | 0 | lz4sd->prefixEnd += result; |
2578 | 0 | } else { |
2579 | | /* The buffer wraps around, or they're switching to another buffer. */ |
2580 | 0 | lz4sd->extDictSize = lz4sd->prefixSize; |
2581 | 0 | lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize; |
2582 | 0 | result = LZ4_decompress_safe_forceExtDict(source, dest, compressedSize, maxOutputSize, |
2583 | 0 | lz4sd->externalDict, lz4sd->extDictSize); |
2584 | 0 | if (result <= 0) return result; |
2585 | 0 | lz4sd->prefixSize = (size_t)result; |
2586 | 0 | lz4sd->prefixEnd = (BYTE*)dest + result; |
2587 | 0 | } |
2588 | | |
2589 | 0 | return result; |
2590 | 0 | } |
2591 | | |
2592 | | LZ4_FORCE_O2 int |
2593 | | LZ4_decompress_fast_continue (LZ4_streamDecode_t* LZ4_streamDecode, |
2594 | | const char* source, char* dest, int originalSize) |
2595 | 0 | { |
2596 | 0 | LZ4_streamDecode_t_internal* const lz4sd = |
2597 | 0 | (assert(LZ4_streamDecode!=NULL), &LZ4_streamDecode->internal_donotuse); |
2598 | 0 | int result; |
2599 | |
|
2600 | 0 | DEBUGLOG(5, "LZ4_decompress_fast_continue (toDecodeSize=%i)", originalSize); |
2601 | 0 | assert(originalSize >= 0); |
2602 | | |
2603 | 0 | if (lz4sd->prefixSize == 0) { |
2604 | 0 | DEBUGLOG(5, "first invocation : no prefix nor extDict"); |
2605 | 0 | assert(lz4sd->extDictSize == 0); |
2606 | 0 | result = LZ4_decompress_fast(source, dest, originalSize); |
2607 | 0 | if (result <= 0) return result; |
2608 | 0 | lz4sd->prefixSize = (size_t)originalSize; |
2609 | 0 | lz4sd->prefixEnd = (BYTE*)dest + originalSize; |
2610 | 0 | } else if (lz4sd->prefixEnd == (BYTE*)dest) { |
2611 | 0 | DEBUGLOG(5, "continue using existing prefix"); |
2612 | 0 | result = LZ4_decompress_unsafe_generic( |
2613 | 0 | (const BYTE*)source, (BYTE*)dest, originalSize, |
2614 | 0 | lz4sd->prefixSize, |
2615 | 0 | lz4sd->externalDict, lz4sd->extDictSize); |
2616 | 0 | if (result <= 0) return result; |
2617 | 0 | lz4sd->prefixSize += (size_t)originalSize; |
2618 | 0 | lz4sd->prefixEnd += originalSize; |
2619 | 0 | } else { |
2620 | 0 | DEBUGLOG(5, "prefix becomes extDict"); |
2621 | 0 | lz4sd->extDictSize = lz4sd->prefixSize; |
2622 | 0 | lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize; |
2623 | 0 | result = LZ4_decompress_fast_extDict(source, dest, originalSize, |
2624 | 0 | lz4sd->externalDict, lz4sd->extDictSize); |
2625 | 0 | if (result <= 0) return result; |
2626 | 0 | lz4sd->prefixSize = (size_t)originalSize; |
2627 | 0 | lz4sd->prefixEnd = (BYTE*)dest + originalSize; |
2628 | 0 | } |
2629 | | |
2630 | 0 | return result; |
2631 | 0 | } |
2632 | | |
2633 | | |
2634 | | /* |
2635 | | Advanced decoding functions : |
2636 | | *_usingDict() : |
2637 | | These decoding functions work the same as "_continue" ones, |
2638 | | the dictionary must be explicitly provided within parameters |
2639 | | */ |
2640 | | |
2641 | | int LZ4_decompress_safe_usingDict(const char* source, char* dest, int compressedSize, int maxOutputSize, const char* dictStart, int dictSize) |
2642 | 0 | { |
2643 | 0 | if (dictSize==0) |
2644 | 0 | return LZ4_decompress_safe(source, dest, compressedSize, maxOutputSize); |
2645 | 0 | if (dictStart+dictSize == dest) { |
2646 | 0 | if (dictSize >= 64 KB - 1) { |
2647 | 0 | return LZ4_decompress_safe_withPrefix64k(source, dest, compressedSize, maxOutputSize); |
2648 | 0 | } |
2649 | 0 | assert(dictSize >= 0); |
2650 | 0 | return LZ4_decompress_safe_withSmallPrefix(source, dest, compressedSize, maxOutputSize, (size_t)dictSize); |
2651 | 0 | } |
2652 | 0 | assert(dictSize >= 0); |
2653 | 0 | return LZ4_decompress_safe_forceExtDict(source, dest, compressedSize, maxOutputSize, dictStart, (size_t)dictSize); |
2654 | 0 | } |
2655 | | |
2656 | | int LZ4_decompress_safe_partial_usingDict(const char* source, char* dest, int compressedSize, int targetOutputSize, int dstCapacity, const char* dictStart, int dictSize) |
2657 | 0 | { |
2658 | 0 | if (dictSize==0) |
2659 | 0 | return LZ4_decompress_safe_partial(source, dest, compressedSize, targetOutputSize, dstCapacity); |
2660 | 0 | if (dictStart+dictSize == dest) { |
2661 | 0 | if (dictSize >= 64 KB - 1) { |
2662 | 0 | return LZ4_decompress_safe_partial_withPrefix64k(source, dest, compressedSize, targetOutputSize, dstCapacity); |
2663 | 0 | } |
2664 | 0 | assert(dictSize >= 0); |
2665 | 0 | return LZ4_decompress_safe_partial_withSmallPrefix(source, dest, compressedSize, targetOutputSize, dstCapacity, (size_t)dictSize); |
2666 | 0 | } |
2667 | 0 | assert(dictSize >= 0); |
2668 | 0 | return LZ4_decompress_safe_partial_forceExtDict(source, dest, compressedSize, targetOutputSize, dstCapacity, dictStart, (size_t)dictSize); |
2669 | 0 | } |
2670 | | |
2671 | | int LZ4_decompress_fast_usingDict(const char* source, char* dest, int originalSize, const char* dictStart, int dictSize) |
2672 | 0 | { |
2673 | 0 | if (dictSize==0 || dictStart+dictSize == dest) |
2674 | 0 | return LZ4_decompress_unsafe_generic( |
2675 | 0 | (const BYTE*)source, (BYTE*)dest, originalSize, |
2676 | 0 | (size_t)dictSize, NULL, 0); |
2677 | 0 | assert(dictSize >= 0); |
2678 | 0 | return LZ4_decompress_fast_extDict(source, dest, originalSize, dictStart, (size_t)dictSize); |
2679 | 0 | } |
2680 | | |
2681 | | |
2682 | | /*=************************************************* |
2683 | | * Obsolete Functions |
2684 | | ***************************************************/ |
2685 | | /* obsolete compression functions */ |
2686 | | int LZ4_compress_limitedOutput(const char* source, char* dest, int inputSize, int maxOutputSize) |
2687 | 0 | { |
2688 | 0 | return LZ4_compress_default(source, dest, inputSize, maxOutputSize); |
2689 | 0 | } |
2690 | | int LZ4_compress(const char* src, char* dest, int srcSize) |
2691 | 0 | { |
2692 | 0 | return LZ4_compress_default(src, dest, srcSize, LZ4_compressBound(srcSize)); |
2693 | 0 | } |
2694 | | int LZ4_compress_limitedOutput_withState (void* state, const char* src, char* dst, int srcSize, int dstSize) |
2695 | 0 | { |
2696 | 0 | return LZ4_compress_fast_extState(state, src, dst, srcSize, dstSize, 1); |
2697 | 0 | } |
2698 | | int LZ4_compress_withState (void* state, const char* src, char* dst, int srcSize) |
2699 | 0 | { |
2700 | 0 | return LZ4_compress_fast_extState(state, src, dst, srcSize, LZ4_compressBound(srcSize), 1); |
2701 | 0 | } |
2702 | | int LZ4_compress_limitedOutput_continue (LZ4_stream_t* LZ4_stream, const char* src, char* dst, int srcSize, int dstCapacity) |
2703 | 0 | { |
2704 | 0 | return LZ4_compress_fast_continue(LZ4_stream, src, dst, srcSize, dstCapacity, 1); |
2705 | 0 | } |
2706 | | int LZ4_compress_continue (LZ4_stream_t* LZ4_stream, const char* source, char* dest, int inputSize) |
2707 | 0 | { |
2708 | 0 | return LZ4_compress_fast_continue(LZ4_stream, source, dest, inputSize, LZ4_compressBound(inputSize), 1); |
2709 | 0 | } |
2710 | | |
2711 | | /* |
2712 | | These decompression functions are deprecated and should no longer be used. |
2713 | | They are only provided here for compatibility with older user programs. |
2714 | | - LZ4_uncompress is totally equivalent to LZ4_decompress_fast |
2715 | | - LZ4_uncompress_unknownOutputSize is totally equivalent to LZ4_decompress_safe |
2716 | | */ |
2717 | | int LZ4_uncompress (const char* source, char* dest, int outputSize) |
2718 | 0 | { |
2719 | 0 | return LZ4_decompress_fast(source, dest, outputSize); |
2720 | 0 | } |
2721 | | int LZ4_uncompress_unknownOutputSize (const char* source, char* dest, int isize, int maxOutputSize) |
2722 | 0 | { |
2723 | 0 | return LZ4_decompress_safe(source, dest, isize, maxOutputSize); |
2724 | 0 | } |
2725 | | |
2726 | | /* Obsolete Streaming functions */ |
2727 | | |
2728 | 0 | int LZ4_sizeofStreamState(void) { return sizeof(LZ4_stream_t); } |
2729 | | |
2730 | | int LZ4_resetStreamState(void* state, char* inputBuffer) |
2731 | 0 | { |
2732 | 0 | (void)inputBuffer; |
2733 | 0 | LZ4_resetStream((LZ4_stream_t*)state); |
2734 | 0 | return 0; |
2735 | 0 | } |
2736 | | |
2737 | | #if !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION) |
2738 | | void* LZ4_create (char* inputBuffer) |
2739 | 0 | { |
2740 | 0 | (void)inputBuffer; |
2741 | 0 | return LZ4_createStream(); |
2742 | 0 | } |
2743 | | #endif |
2744 | | |
2745 | | char* LZ4_slideInputBuffer (void* state) |
2746 | 0 | { |
2747 | | /* avoid const char * -> char * conversion warning */ |
2748 | 0 | return (char *)(uptrval)((LZ4_stream_t*)state)->internal_donotuse.dictionary; |
2749 | 0 | } |
2750 | | |
2751 | | #endif /* LZ4_COMMONDEFS_ONLY */ |