/src/mozilla-central/mfbt/lz4.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | LZ4 - Fast LZ compression algorithm |
3 | | Copyright (C) 2011-2017, 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 | | /*-************************************ |
37 | | * Tuning parameters |
38 | | **************************************/ |
39 | | /* |
40 | | * LZ4_HEAPMODE : |
41 | | * Select how default compression functions will 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 | | * ACCELERATION_DEFAULT : |
50 | | * Select "acceleration" for LZ4_compress_fast() when parameter value <= 0 |
51 | | */ |
52 | 0 | #define ACCELERATION_DEFAULT 1 |
53 | | |
54 | | |
55 | | /*-************************************ |
56 | | * CPU Feature Detection |
57 | | **************************************/ |
58 | | /* LZ4_FORCE_MEMORY_ACCESS |
59 | | * By default, access to unaligned memory is controlled by `memcpy()`, which is safe and portable. |
60 | | * Unfortunately, on some target/compiler combinations, the generated assembly is sub-optimal. |
61 | | * The below switch allow to select different access method for improved performance. |
62 | | * Method 0 (default) : use `memcpy()`. Safe and portable. |
63 | | * Method 1 : `__packed` statement. It depends on compiler extension (ie, not portable). |
64 | | * This method is safe if your compiler supports it, and *generally* as fast or faster than `memcpy`. |
65 | | * Method 2 : direct access. This method is portable but violate C standard. |
66 | | * It can generate buggy code on targets which assembly generation depends on alignment. |
67 | | * But in some circumstances, it's the only known way to get the most performance (ie GCC + ARMv6) |
68 | | * See https://fastcompression.blogspot.fr/2015/08/accessing-unaligned-memory.html for details. |
69 | | * Prefer these methods in priority order (0 > 1 > 2) |
70 | | */ |
71 | | #ifndef LZ4_FORCE_MEMORY_ACCESS /* can be defined externally */ |
72 | | # if defined(__GNUC__) && \ |
73 | | ( defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) \ |
74 | | || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_6T2__) ) |
75 | | # define LZ4_FORCE_MEMORY_ACCESS 2 |
76 | | # elif (defined(__INTEL_COMPILER) && !defined(_WIN32)) || defined(__GNUC__) |
77 | | # define LZ4_FORCE_MEMORY_ACCESS 1 |
78 | | # endif |
79 | | #endif |
80 | | |
81 | | /* |
82 | | * LZ4_FORCE_SW_BITCOUNT |
83 | | * Define this parameter if your target system or compiler does not support hardware bit count |
84 | | */ |
85 | | #if defined(_MSC_VER) && defined(_WIN32_WCE) /* Visual Studio for WinCE doesn't support Hardware bit count */ |
86 | | # define LZ4_FORCE_SW_BITCOUNT |
87 | | #endif |
88 | | |
89 | | |
90 | | |
91 | | /*-************************************ |
92 | | * Dependency |
93 | | **************************************/ |
94 | | #define LZ4_STATIC_LINKING_ONLY |
95 | | #define LZ4_DISABLE_DEPRECATE_WARNINGS /* due to LZ4_decompress_safe_withPrefix64k */ |
96 | | #include "lz4.h" |
97 | | /* see also "memory routines" below */ |
98 | | |
99 | | |
100 | | /*-************************************ |
101 | | * Compiler Options |
102 | | **************************************/ |
103 | | #ifdef _MSC_VER /* Visual Studio */ |
104 | | # include <intrin.h> |
105 | | # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ |
106 | | # pragma warning(disable : 4293) /* disable: C4293: too large shift (32-bits) */ |
107 | | #endif /* _MSC_VER */ |
108 | | |
109 | | #ifndef LZ4_FORCE_INLINE |
110 | | # ifdef _MSC_VER /* Visual Studio */ |
111 | | # define LZ4_FORCE_INLINE static __forceinline |
112 | | # else |
113 | | # if defined (__cplusplus) || defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */ |
114 | | # ifdef __GNUC__ |
115 | | # define LZ4_FORCE_INLINE static inline __attribute__((always_inline)) |
116 | | # else |
117 | | # define LZ4_FORCE_INLINE static inline |
118 | | # endif |
119 | | # else |
120 | | # define LZ4_FORCE_INLINE static |
121 | | # endif /* __STDC_VERSION__ */ |
122 | | # endif /* _MSC_VER */ |
123 | | #endif /* LZ4_FORCE_INLINE */ |
124 | | |
125 | | /* LZ4_FORCE_O2_GCC_PPC64LE and LZ4_FORCE_O2_INLINE_GCC_PPC64LE |
126 | | * Gcc on ppc64le generates an unrolled SIMDized loop for LZ4_wildCopy, |
127 | | * together with a simple 8-byte copy loop as a fall-back path. |
128 | | * However, this optimization hurts the decompression speed by >30%, |
129 | | * because the execution does not go to the optimized loop |
130 | | * for typical compressible data, and all of the preamble checks |
131 | | * before going to the fall-back path become useless overhead. |
132 | | * This optimization happens only with the -O3 flag, and -O2 generates |
133 | | * a simple 8-byte copy loop. |
134 | | * With gcc on ppc64le, all of the LZ4_decompress_* and LZ4_wildCopy |
135 | | * functions are annotated with __attribute__((optimize("O2"))), |
136 | | * and also LZ4_wildCopy is forcibly inlined, so that the O2 attribute |
137 | | * of LZ4_wildCopy does not affect the compression speed. |
138 | | */ |
139 | | #if defined(__PPC64__) && defined(__LITTLE_ENDIAN__) && defined(__GNUC__) |
140 | | # define LZ4_FORCE_O2_GCC_PPC64LE __attribute__((optimize("O2"))) |
141 | | # define LZ4_FORCE_O2_INLINE_GCC_PPC64LE __attribute__((optimize("O2"))) LZ4_FORCE_INLINE |
142 | | #else |
143 | | # define LZ4_FORCE_O2_GCC_PPC64LE |
144 | | # define LZ4_FORCE_O2_INLINE_GCC_PPC64LE static |
145 | | #endif |
146 | | |
147 | | #if (defined(__GNUC__) && (__GNUC__ >= 3)) || (defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 800)) || defined(__clang__) |
148 | 0 | # define expect(expr,value) (__builtin_expect ((expr),(value)) ) |
149 | | #else |
150 | | # define expect(expr,value) (expr) |
151 | | #endif |
152 | | |
153 | | #ifndef likely |
154 | 0 | #define likely(expr) expect((expr) != 0, 1) |
155 | | #endif |
156 | | #ifndef unlikely |
157 | 0 | #define unlikely(expr) expect((expr) != 0, 0) |
158 | | #endif |
159 | | |
160 | | |
161 | | /*-************************************ |
162 | | * Memory routines |
163 | | **************************************/ |
164 | | #include <stdlib.h> /* malloc, calloc, free */ |
165 | 0 | #define ALLOC(s) malloc(s) |
166 | 0 | #define ALLOC_AND_ZERO(s) calloc(1,s) |
167 | 0 | #define FREEMEM(p) free(p) |
168 | | #include <string.h> /* memset, memcpy */ |
169 | 0 | #define MEM_INIT(p,v,s) memset((p),(v),(s)) |
170 | | |
171 | | |
172 | | /*-************************************ |
173 | | * Basic Types |
174 | | **************************************/ |
175 | | #if defined(__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) |
176 | | # include <stdint.h> |
177 | | typedef uint8_t BYTE; |
178 | | typedef uint16_t U16; |
179 | | typedef uint32_t U32; |
180 | | typedef int32_t S32; |
181 | | typedef uint64_t U64; |
182 | | typedef uintptr_t uptrval; |
183 | | #else |
184 | | typedef unsigned char BYTE; |
185 | | typedef unsigned short U16; |
186 | | typedef unsigned int U32; |
187 | | typedef signed int S32; |
188 | | typedef unsigned long long U64; |
189 | | typedef size_t uptrval; /* generally true, except OpenVMS-64 */ |
190 | | #endif |
191 | | |
192 | | #if defined(__x86_64__) |
193 | | typedef U64 reg_t; /* 64-bits in x32 mode */ |
194 | | #else |
195 | | typedef size_t reg_t; /* 32-bits in x32 mode */ |
196 | | #endif |
197 | | |
198 | | /*-************************************ |
199 | | * Reading and writing into memory |
200 | | **************************************/ |
201 | | static unsigned LZ4_isLittleEndian(void) |
202 | 0 | { |
203 | 0 | const union { U32 u; BYTE c[4]; } one = { 1 }; /* don't use static : performance detrimental */ |
204 | 0 | return one.c[0]; |
205 | 0 | } |
206 | | |
207 | | |
208 | | #if defined(LZ4_FORCE_MEMORY_ACCESS) && (LZ4_FORCE_MEMORY_ACCESS==2) |
209 | | /* lie to the compiler about data alignment; use with caution */ |
210 | | |
211 | | static U16 LZ4_read16(const void* memPtr) { return *(const U16*) memPtr; } |
212 | | static U32 LZ4_read32(const void* memPtr) { return *(const U32*) memPtr; } |
213 | | static reg_t LZ4_read_ARCH(const void* memPtr) { return *(const reg_t*) memPtr; } |
214 | | |
215 | | static void LZ4_write16(void* memPtr, U16 value) { *(U16*)memPtr = value; } |
216 | | static void LZ4_write32(void* memPtr, U32 value) { *(U32*)memPtr = value; } |
217 | | |
218 | | #elif defined(LZ4_FORCE_MEMORY_ACCESS) && (LZ4_FORCE_MEMORY_ACCESS==1) |
219 | | |
220 | | /* __pack instructions are safer, but compiler specific, hence potentially problematic for some compilers */ |
221 | | /* currently only defined for gcc and icc */ |
222 | | typedef union { U16 u16; U32 u32; reg_t uArch; } __attribute__((packed)) unalign; |
223 | | |
224 | 0 | static U16 LZ4_read16(const void* ptr) { return ((const unalign*)ptr)->u16; } |
225 | 0 | static U32 LZ4_read32(const void* ptr) { return ((const unalign*)ptr)->u32; } |
226 | 0 | static reg_t LZ4_read_ARCH(const void* ptr) { return ((const unalign*)ptr)->uArch; } |
227 | | |
228 | 0 | static void LZ4_write16(void* memPtr, U16 value) { ((unalign*)memPtr)->u16 = value; } |
229 | 0 | static void LZ4_write32(void* memPtr, U32 value) { ((unalign*)memPtr)->u32 = value; } |
230 | | |
231 | | #else /* safe and portable access through memcpy() */ |
232 | | |
233 | | static U16 LZ4_read16(const void* memPtr) |
234 | | { |
235 | | U16 val; memcpy(&val, memPtr, sizeof(val)); return val; |
236 | | } |
237 | | |
238 | | static U32 LZ4_read32(const void* memPtr) |
239 | | { |
240 | | U32 val; memcpy(&val, memPtr, sizeof(val)); return val; |
241 | | } |
242 | | |
243 | | static reg_t LZ4_read_ARCH(const void* memPtr) |
244 | | { |
245 | | reg_t val; memcpy(&val, memPtr, sizeof(val)); return val; |
246 | | } |
247 | | |
248 | | static void LZ4_write16(void* memPtr, U16 value) |
249 | | { |
250 | | memcpy(memPtr, &value, sizeof(value)); |
251 | | } |
252 | | |
253 | | static void LZ4_write32(void* memPtr, U32 value) |
254 | | { |
255 | | memcpy(memPtr, &value, sizeof(value)); |
256 | | } |
257 | | |
258 | | #endif /* LZ4_FORCE_MEMORY_ACCESS */ |
259 | | |
260 | | |
261 | | static U16 LZ4_readLE16(const void* memPtr) |
262 | 0 | { |
263 | 0 | if (LZ4_isLittleEndian()) { |
264 | 0 | return LZ4_read16(memPtr); |
265 | 0 | } else { |
266 | 0 | const BYTE* p = (const BYTE*)memPtr; |
267 | 0 | return (U16)((U16)p[0] + (p[1]<<8)); |
268 | 0 | } |
269 | 0 | } |
270 | | |
271 | | static void LZ4_writeLE16(void* memPtr, U16 value) |
272 | 0 | { |
273 | 0 | if (LZ4_isLittleEndian()) { |
274 | 0 | LZ4_write16(memPtr, value); |
275 | 0 | } else { |
276 | 0 | BYTE* p = (BYTE*)memPtr; |
277 | 0 | p[0] = (BYTE) value; |
278 | 0 | p[1] = (BYTE)(value>>8); |
279 | 0 | } |
280 | 0 | } |
281 | | |
282 | | /* customized variant of memcpy, which can overwrite up to 8 bytes beyond dstEnd */ |
283 | | LZ4_FORCE_O2_INLINE_GCC_PPC64LE |
284 | | void LZ4_wildCopy(void* dstPtr, const void* srcPtr, void* dstEnd) |
285 | 0 | { |
286 | 0 | BYTE* d = (BYTE*)dstPtr; |
287 | 0 | const BYTE* s = (const BYTE*)srcPtr; |
288 | 0 | BYTE* const e = (BYTE*)dstEnd; |
289 | 0 |
|
290 | 0 | do { memcpy(d,s,8); d+=8; s+=8; } while (d<e); |
291 | 0 | } |
292 | | |
293 | | |
294 | | /*-************************************ |
295 | | * Common Constants |
296 | | **************************************/ |
297 | 0 | #define MINMATCH 4 |
298 | | |
299 | 0 | #define WILDCOPYLENGTH 8 |
300 | 0 | #define LASTLITERALS 5 |
301 | 0 | #define MFLIMIT (WILDCOPYLENGTH+MINMATCH) |
302 | | static const int LZ4_minLength = (MFLIMIT+1); |
303 | | |
304 | 0 | #define KB *(1 <<10) |
305 | | #define MB *(1 <<20) |
306 | 0 | #define GB *(1U<<30) |
307 | | |
308 | 0 | #define MAXD_LOG 16 |
309 | 0 | #define MAX_DISTANCE ((1 << MAXD_LOG) - 1) |
310 | | |
311 | 0 | #define ML_BITS 4 |
312 | 0 | #define ML_MASK ((1U<<ML_BITS)-1) |
313 | 0 | #define RUN_BITS (8-ML_BITS) |
314 | 0 | #define RUN_MASK ((1U<<RUN_BITS)-1) |
315 | | |
316 | | |
317 | | /*-************************************ |
318 | | * Error detection |
319 | | **************************************/ |
320 | | #if defined(LZ4_DEBUG) && (LZ4_DEBUG>=1) |
321 | | # include <assert.h> |
322 | | #else |
323 | | # ifndef assert |
324 | 0 | # define assert(condition) ((void)0) |
325 | | # endif |
326 | | #endif |
327 | | |
328 | 0 | #define LZ4_STATIC_ASSERT(c) { enum { LZ4_static_assert = 1/(int)(!!(c)) }; } /* use after variable declarations */ |
329 | | |
330 | | #if defined(LZ4_DEBUG) && (LZ4_DEBUG>=2) |
331 | | # include <stdio.h> |
332 | | static int g_debuglog_enable = 1; |
333 | | # define DEBUGLOG(l, ...) { \ |
334 | | if ((g_debuglog_enable) && (l<=LZ4_DEBUG)) { \ |
335 | | fprintf(stderr, __FILE__ ": "); \ |
336 | | fprintf(stderr, __VA_ARGS__); \ |
337 | | fprintf(stderr, " \n"); \ |
338 | | } } |
339 | | #else |
340 | 0 | # define DEBUGLOG(l, ...) {} /* disabled */ |
341 | | #endif |
342 | | |
343 | | |
344 | | /*-************************************ |
345 | | * Common functions |
346 | | **************************************/ |
347 | | static unsigned LZ4_NbCommonBytes (reg_t val) |
348 | 0 | { |
349 | 0 | if (LZ4_isLittleEndian()) { |
350 | 0 | if (sizeof(val)==8) { |
351 | | # if defined(_MSC_VER) && defined(_WIN64) && !defined(LZ4_FORCE_SW_BITCOUNT) |
352 | | unsigned long r = 0; |
353 | | _BitScanForward64( &r, (U64)val ); |
354 | | return (int)(r>>3); |
355 | | # elif (defined(__clang__) || (defined(__GNUC__) && (__GNUC__>=3))) && !defined(LZ4_FORCE_SW_BITCOUNT) |
356 | | return (__builtin_ctzll((U64)val) >> 3); |
357 | | # else |
358 | | static const int DeBruijnBytePos[64] = { 0, 0, 0, 0, 0, 1, 1, 2, |
359 | | 0, 3, 1, 3, 1, 4, 2, 7, |
360 | | 0, 2, 3, 6, 1, 5, 3, 5, |
361 | | 1, 3, 4, 4, 2, 5, 6, 7, |
362 | | 7, 0, 1, 2, 3, 3, 4, 6, |
363 | | 2, 6, 5, 5, 3, 4, 5, 6, |
364 | | 7, 1, 2, 4, 6, 4, 4, 5, |
365 | | 7, 2, 6, 5, 7, 6, 7, 7 }; |
366 | | return DeBruijnBytePos[((U64)((val & -(long long)val) * 0x0218A392CDABBD3FULL)) >> 58]; |
367 | | # endif |
368 | 0 | } else /* 32 bits */ { |
369 | | # if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT) |
370 | | unsigned long r; |
371 | | _BitScanForward( &r, (U32)val ); |
372 | | return (int)(r>>3); |
373 | | # elif (defined(__clang__) || (defined(__GNUC__) && (__GNUC__>=3))) && !defined(LZ4_FORCE_SW_BITCOUNT) |
374 | | return (__builtin_ctz((U32)val) >> 3); |
375 | | # else |
376 | | static const int DeBruijnBytePos[32] = { 0, 0, 3, 0, 3, 1, 3, 0, |
377 | | 3, 2, 2, 1, 3, 2, 0, 1, |
378 | | 3, 3, 1, 2, 2, 2, 2, 0, |
379 | | 3, 1, 2, 0, 1, 0, 1, 1 }; |
380 | | return DeBruijnBytePos[((U32)((val & -(S32)val) * 0x077CB531U)) >> 27]; |
381 | | # endif |
382 | | } |
383 | 0 | } else /* Big Endian CPU */ { |
384 | 0 | if (sizeof(val)==8) { /* 64-bits */ |
385 | | # if defined(_MSC_VER) && defined(_WIN64) && !defined(LZ4_FORCE_SW_BITCOUNT) |
386 | | unsigned long r = 0; |
387 | | _BitScanReverse64( &r, val ); |
388 | | return (unsigned)(r>>3); |
389 | | # elif (defined(__clang__) || (defined(__GNUC__) && (__GNUC__>=3))) && !defined(LZ4_FORCE_SW_BITCOUNT) |
390 | | return (__builtin_clzll((U64)val) >> 3); |
391 | | # else |
392 | | static const U32 by32 = sizeof(val)*4; /* 32 on 64 bits (goal), 16 on 32 bits. |
393 | | Just to avoid some static analyzer complaining about shift by 32 on 32-bits target. |
394 | | Note that this code path is never triggered in 32-bits mode. */ |
395 | | unsigned r; |
396 | | if (!(val>>by32)) { r=4; } else { r=0; val>>=by32; } |
397 | | if (!(val>>16)) { r+=2; val>>=8; } else { val>>=24; } |
398 | | r += (!val); |
399 | | return r; |
400 | | # endif |
401 | 0 | } else /* 32 bits */ { |
402 | | # if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT) |
403 | | unsigned long r = 0; |
404 | | _BitScanReverse( &r, (unsigned long)val ); |
405 | | return (unsigned)(r>>3); |
406 | | # elif (defined(__clang__) || (defined(__GNUC__) && (__GNUC__>=3))) && !defined(LZ4_FORCE_SW_BITCOUNT) |
407 | | return (__builtin_clz((U32)val) >> 3); |
408 | | # else |
409 | | unsigned r; |
410 | | if (!(val>>16)) { r=2; val>>=8; } else { r=0; val>>=24; } |
411 | | r += (!val); |
412 | | return r; |
413 | | # endif |
414 | | } |
415 | 0 | } |
416 | 0 | } |
417 | | |
418 | 0 | #define STEPSIZE sizeof(reg_t) |
419 | | LZ4_FORCE_INLINE |
420 | | unsigned LZ4_count(const BYTE* pIn, const BYTE* pMatch, const BYTE* pInLimit) |
421 | 0 | { |
422 | 0 | const BYTE* const pStart = pIn; |
423 | 0 |
|
424 | 0 | if (likely(pIn < pInLimit-(STEPSIZE-1))) { |
425 | 0 | reg_t const diff = LZ4_read_ARCH(pMatch) ^ LZ4_read_ARCH(pIn); |
426 | 0 | if (!diff) { |
427 | 0 | pIn+=STEPSIZE; pMatch+=STEPSIZE; |
428 | 0 | } else { |
429 | 0 | return LZ4_NbCommonBytes(diff); |
430 | 0 | } } |
431 | 0 | |
432 | 0 | while (likely(pIn < pInLimit-(STEPSIZE-1))) { |
433 | 0 | reg_t const diff = LZ4_read_ARCH(pMatch) ^ LZ4_read_ARCH(pIn); |
434 | 0 | if (!diff) { pIn+=STEPSIZE; pMatch+=STEPSIZE; continue; } |
435 | 0 | pIn += LZ4_NbCommonBytes(diff); |
436 | 0 | return (unsigned)(pIn - pStart); |
437 | 0 | } |
438 | 0 |
|
439 | 0 | if ((STEPSIZE==8) && (pIn<(pInLimit-3)) && (LZ4_read32(pMatch) == LZ4_read32(pIn))) { pIn+=4; pMatch+=4; } |
440 | 0 | if ((pIn<(pInLimit-1)) && (LZ4_read16(pMatch) == LZ4_read16(pIn))) { pIn+=2; pMatch+=2; } |
441 | 0 | if ((pIn<pInLimit) && (*pMatch == *pIn)) pIn++; |
442 | 0 | return (unsigned)(pIn - pStart); |
443 | 0 | } |
444 | | |
445 | | |
446 | | #ifndef LZ4_COMMONDEFS_ONLY |
447 | | /*-************************************ |
448 | | * Local Constants |
449 | | **************************************/ |
450 | | static const int LZ4_64Klimit = ((64 KB) + (MFLIMIT-1)); |
451 | | static const U32 LZ4_skipTrigger = 6; /* Increase this value ==> compression run slower on incompressible data */ |
452 | | |
453 | | |
454 | | /*-************************************ |
455 | | * Local Structures and types |
456 | | **************************************/ |
457 | | typedef enum { notLimited = 0, limitedOutput = 1, fillOutput = 2 } limitedOutput_directive; |
458 | | typedef enum { clearedTable = 0, byPtr, byU32, byU16 } tableType_t; |
459 | | |
460 | | /** |
461 | | * This enum distinguishes several different modes of accessing previous |
462 | | * content in the stream. |
463 | | * |
464 | | * - noDict : There is no preceding content. |
465 | | * - withPrefix64k : Table entries up to ctx->dictSize before the current blob |
466 | | * blob being compressed are valid and refer to the preceding |
467 | | * content (of length ctx->dictSize), which is available |
468 | | * contiguously preceding in memory the content currently |
469 | | * being compressed. |
470 | | * - usingExtDict : Like withPrefix64k, but the preceding content is somewhere |
471 | | * else in memory, starting at ctx->dictionary with length |
472 | | * ctx->dictSize. |
473 | | * - usingDictCtx : Like usingExtDict, but everything concerning the preceding |
474 | | * content is in a separate context, pointed to by |
475 | | * ctx->dictCtx. ctx->dictionary, ctx->dictSize, and table |
476 | | * entries in the current context that refer to positions |
477 | | * preceding the beginning of the current compression are |
478 | | * ignored. Instead, ctx->dictCtx->dictionary and ctx->dictCtx |
479 | | * ->dictSize describe the location and size of the preceding |
480 | | * content, and matches are found by looking in the ctx |
481 | | * ->dictCtx->hashTable. |
482 | | */ |
483 | | typedef enum { noDict = 0, withPrefix64k, usingExtDict, usingDictCtx } dict_directive; |
484 | | typedef enum { noDictIssue = 0, dictSmall } dictIssue_directive; |
485 | | |
486 | | typedef enum { endOnOutputSize = 0, endOnInputSize = 1 } endCondition_directive; |
487 | | typedef enum { full = 0, partial = 1 } earlyEnd_directive; |
488 | | |
489 | | |
490 | | /*-************************************ |
491 | | * Local Utils |
492 | | **************************************/ |
493 | 0 | int LZ4_versionNumber (void) { return LZ4_VERSION_NUMBER; } |
494 | 0 | const char* LZ4_versionString(void) { return LZ4_VERSION_STRING; } |
495 | 0 | int LZ4_compressBound(int isize) { return LZ4_COMPRESSBOUND(isize); } |
496 | 0 | int LZ4_sizeofState() { return LZ4_STREAMSIZE; } |
497 | | |
498 | | |
499 | | /*-****************************** |
500 | | * Compression functions |
501 | | ********************************/ |
502 | | static U32 LZ4_hash4(U32 sequence, tableType_t const tableType) |
503 | 0 | { |
504 | 0 | if (tableType == byU16) |
505 | 0 | return ((sequence * 2654435761U) >> ((MINMATCH*8)-(LZ4_HASHLOG+1))); |
506 | 0 | else |
507 | 0 | return ((sequence * 2654435761U) >> ((MINMATCH*8)-LZ4_HASHLOG)); |
508 | 0 | } |
509 | | |
510 | | static U32 LZ4_hash5(U64 sequence, tableType_t const tableType) |
511 | 0 | { |
512 | 0 | static const U64 prime5bytes = 889523592379ULL; |
513 | 0 | static const U64 prime8bytes = 11400714785074694791ULL; |
514 | 0 | const U32 hashLog = (tableType == byU16) ? LZ4_HASHLOG+1 : LZ4_HASHLOG; |
515 | 0 | if (LZ4_isLittleEndian()) |
516 | 0 | return (U32)(((sequence << 24) * prime5bytes) >> (64 - hashLog)); |
517 | 0 | else |
518 | 0 | return (U32)(((sequence >> 24) * prime8bytes) >> (64 - hashLog)); |
519 | 0 | } |
520 | | |
521 | | LZ4_FORCE_INLINE U32 LZ4_hashPosition(const void* const p, tableType_t const tableType) |
522 | 0 | { |
523 | 0 | if ((sizeof(reg_t)==8) && (tableType != byU16)) return LZ4_hash5(LZ4_read_ARCH(p), tableType); |
524 | 0 | return LZ4_hash4(LZ4_read32(p), tableType); |
525 | 0 | } |
526 | | |
527 | | static void LZ4_putIndexOnHash(U32 idx, U32 h, void* tableBase, tableType_t const tableType) |
528 | 0 | { |
529 | 0 | switch (tableType) |
530 | 0 | { |
531 | 0 | default: /* fallthrough */ |
532 | 0 | case clearedTable: /* fallthrough */ |
533 | 0 | case byPtr: { /* illegal! */ assert(0); return; } |
534 | 0 | case byU32: { U32* hashTable = (U32*) tableBase; hashTable[h] = idx; return; } |
535 | 0 | case byU16: { U16* hashTable = (U16*) tableBase; assert(idx < 65536); hashTable[h] = (U16)idx; return; } |
536 | 0 | } |
537 | 0 | } |
538 | | |
539 | | static void LZ4_putPositionOnHash(const BYTE* p, U32 h, |
540 | | void* tableBase, tableType_t const tableType, |
541 | | const BYTE* srcBase) |
542 | 0 | { |
543 | 0 | switch (tableType) |
544 | 0 | { |
545 | 0 | case clearedTable: { /* illegal! */ assert(0); return; } |
546 | 0 | case byPtr: { const BYTE** hashTable = (const BYTE**)tableBase; hashTable[h] = p; return; } |
547 | 0 | case byU32: { U32* hashTable = (U32*) tableBase; hashTable[h] = (U32)(p-srcBase); return; } |
548 | 0 | case byU16: { U16* hashTable = (U16*) tableBase; hashTable[h] = (U16)(p-srcBase); return; } |
549 | 0 | } |
550 | 0 | } |
551 | | |
552 | | LZ4_FORCE_INLINE void LZ4_putPosition(const BYTE* p, void* tableBase, tableType_t tableType, const BYTE* srcBase) |
553 | 0 | { |
554 | 0 | U32 const h = LZ4_hashPosition(p, tableType); |
555 | 0 | LZ4_putPositionOnHash(p, h, tableBase, tableType, srcBase); |
556 | 0 | } |
557 | | |
558 | | /* LZ4_getIndexOnHash() : |
559 | | * Index of match position registered in hash table. |
560 | | * hash position must be calculated by using base+index, or dictBase+index. |
561 | | * Assumption 1 : only valid if tableType == byU32 or byU16. |
562 | | * Assumption 2 : h is presumed valid (within limits of hash table) |
563 | | */ |
564 | | static U32 LZ4_getIndexOnHash(U32 h, const void* tableBase, tableType_t tableType) |
565 | 0 | { |
566 | 0 | LZ4_STATIC_ASSERT(LZ4_MEMORY_USAGE > 2); |
567 | 0 | if (tableType == byU32) { |
568 | 0 | const U32* const hashTable = (const U32*) tableBase; |
569 | 0 | assert(h < (1U << (LZ4_MEMORY_USAGE-2))); |
570 | 0 | return hashTable[h]; |
571 | 0 | } |
572 | 0 | if (tableType == byU16) { |
573 | 0 | const U16* const hashTable = (const U16*) tableBase; |
574 | 0 | assert(h < (1U << (LZ4_MEMORY_USAGE-1))); |
575 | 0 | return hashTable[h]; |
576 | 0 | } |
577 | 0 | assert(0); return 0; /* forbidden case */ |
578 | 0 | } |
579 | | |
580 | | static const BYTE* LZ4_getPositionOnHash(U32 h, const void* tableBase, tableType_t tableType, const BYTE* srcBase) |
581 | 0 | { |
582 | 0 | if (tableType == byPtr) { const BYTE* const* hashTable = (const BYTE* const*) tableBase; return hashTable[h]; } |
583 | 0 | if (tableType == byU32) { const U32* const hashTable = (const U32*) tableBase; return hashTable[h] + srcBase; } |
584 | 0 | { const U16* const hashTable = (const U16*) tableBase; return hashTable[h] + srcBase; } /* default, to ensure a return */ |
585 | 0 | } |
586 | | |
587 | | LZ4_FORCE_INLINE const BYTE* LZ4_getPosition(const BYTE* p, |
588 | | const void* tableBase, tableType_t tableType, |
589 | | const BYTE* srcBase) |
590 | 0 | { |
591 | 0 | U32 const h = LZ4_hashPosition(p, tableType); |
592 | 0 | return LZ4_getPositionOnHash(h, tableBase, tableType, srcBase); |
593 | 0 | } |
594 | | |
595 | | LZ4_FORCE_INLINE void LZ4_prepareTable( |
596 | | LZ4_stream_t_internal* const cctx, |
597 | | const int inputSize, |
598 | 0 | const tableType_t tableType) { |
599 | 0 | /* If the table hasn't been used, it's guaranteed to be zeroed out, and is |
600 | 0 | * therefore safe to use no matter what mode we're in. Otherwise, we figure |
601 | 0 | * out if it's safe to leave as is or whether it needs to be reset. |
602 | 0 | */ |
603 | 0 | if (cctx->tableType != clearedTable) { |
604 | 0 | if (cctx->tableType != tableType |
605 | 0 | || (tableType == byU16 && cctx->currentOffset + inputSize >= 0xFFFFU) |
606 | 0 | || (tableType == byU32 && cctx->currentOffset > 1 GB) |
607 | 0 | || tableType == byPtr |
608 | 0 | || inputSize >= 4 KB) |
609 | 0 | { |
610 | 0 | DEBUGLOG(4, "LZ4_prepareTable: Resetting table in %p", cctx); |
611 | 0 | MEM_INIT(cctx->hashTable, 0, LZ4_HASHTABLESIZE); |
612 | 0 | cctx->currentOffset = 0; |
613 | 0 | cctx->tableType = clearedTable; |
614 | 0 | } else { |
615 | 0 | DEBUGLOG(4, "LZ4_prepareTable: Re-use hash table (no reset)"); |
616 | 0 | } |
617 | 0 | } |
618 | 0 |
|
619 | 0 | /* Adding a gap, so all previous entries are > MAX_DISTANCE back, is faster |
620 | 0 | * than compressing without a gap. However, compressing with |
621 | 0 | * currentOffset == 0 is faster still, so we preserve that case. |
622 | 0 | */ |
623 | 0 | if (cctx->currentOffset != 0 && tableType == byU32) { |
624 | 0 | DEBUGLOG(5, "LZ4_prepareTable: adding 64KB to currentOffset"); |
625 | 0 | cctx->currentOffset += 64 KB; |
626 | 0 | } |
627 | 0 |
|
628 | 0 | /* Finally, clear history */ |
629 | 0 | cctx->dictCtx = NULL; |
630 | 0 | cctx->dictionary = NULL; |
631 | 0 | cctx->dictSize = 0; |
632 | 0 | } |
633 | | |
634 | | /** LZ4_compress_generic() : |
635 | | inlined, to ensure branches are decided at compilation time */ |
636 | | LZ4_FORCE_INLINE int LZ4_compress_generic( |
637 | | LZ4_stream_t_internal* const cctx, |
638 | | const char* const source, |
639 | | char* const dest, |
640 | | const int inputSize, |
641 | | int *inputConsumed, /* only written when outputLimited == fillOutput */ |
642 | | const int maxOutputSize, |
643 | | const limitedOutput_directive outputLimited, |
644 | | const tableType_t tableType, |
645 | | const dict_directive dictDirective, |
646 | | const dictIssue_directive dictIssue, |
647 | | const U32 acceleration) |
648 | 0 | { |
649 | 0 | const BYTE* ip = (const BYTE*) source; |
650 | 0 |
|
651 | 0 | U32 const startIndex = cctx->currentOffset; |
652 | 0 | const BYTE* base = (const BYTE*) source - startIndex; |
653 | 0 | const BYTE* lowLimit; |
654 | 0 |
|
655 | 0 | const LZ4_stream_t_internal* dictCtx = (const LZ4_stream_t_internal*) cctx->dictCtx; |
656 | 0 | const BYTE* const dictionary = |
657 | 0 | dictDirective == usingDictCtx ? dictCtx->dictionary : cctx->dictionary; |
658 | 0 | const U32 dictSize = |
659 | 0 | dictDirective == usingDictCtx ? dictCtx->dictSize : cctx->dictSize; |
660 | 0 | const U32 dictDelta = (dictDirective == usingDictCtx) ? startIndex - dictCtx->currentOffset : 0; /* make indexes in dictCtx comparable with index in current context */ |
661 | 0 |
|
662 | 0 | int const maybe_extMem = (dictDirective == usingExtDict) || (dictDirective == usingDictCtx); |
663 | 0 | U32 const prefixIdxLimit = startIndex - dictSize; /* used when dictDirective == dictSmall */ |
664 | 0 | const BYTE* const dictEnd = dictionary + dictSize; |
665 | 0 | const BYTE* anchor = (const BYTE*) source; |
666 | 0 | const BYTE* const iend = ip + inputSize; |
667 | 0 | const BYTE* const mflimitPlusOne = iend - MFLIMIT + 1; |
668 | 0 | const BYTE* const matchlimit = iend - LASTLITERALS; |
669 | 0 |
|
670 | 0 | /* the dictCtx currentOffset is indexed on the start of the dictionary, |
671 | 0 | * while a dictionary in the current context precedes the currentOffset */ |
672 | 0 | const BYTE* dictBase = dictDirective == usingDictCtx ? |
673 | 0 | dictionary + dictSize - dictCtx->currentOffset : |
674 | 0 | dictionary + dictSize - startIndex; |
675 | 0 |
|
676 | 0 | BYTE* op = (BYTE*) dest; |
677 | 0 | BYTE* const olimit = op + maxOutputSize; |
678 | 0 |
|
679 | 0 | U32 offset = 0; |
680 | 0 | U32 forwardH; |
681 | 0 |
|
682 | 0 | DEBUGLOG(5, "LZ4_compress_generic: srcSize=%i, tableType=%u", inputSize, tableType); |
683 | 0 | /* Init conditions */ |
684 | 0 | if (outputLimited == fillOutput && maxOutputSize < 1) return 0; /* Impossible to store anything */ |
685 | 0 | if ((U32)inputSize > (U32)LZ4_MAX_INPUT_SIZE) return 0; /* Unsupported inputSize, too large (or negative) */ |
686 | 0 | if ((tableType == byU16) && (inputSize>=LZ4_64Klimit)) return 0; /* Size too large (not within 64K limit) */ |
687 | 0 | if (tableType==byPtr) assert(dictDirective==noDict); /* only supported use case with byPtr */ |
688 | 0 | assert(acceleration >= 1); |
689 | 0 |
|
690 | 0 | lowLimit = (const BYTE*)source - (dictDirective == withPrefix64k ? dictSize : 0); |
691 | 0 |
|
692 | 0 | /* Update context state */ |
693 | 0 | if (dictDirective == usingDictCtx) { |
694 | 0 | /* Subsequent linked blocks can't use the dictionary. */ |
695 | 0 | /* Instead, they use the block we just compressed. */ |
696 | 0 | cctx->dictCtx = NULL; |
697 | 0 | cctx->dictSize = (U32)inputSize; |
698 | 0 | } else { |
699 | 0 | cctx->dictSize += (U32)inputSize; |
700 | 0 | } |
701 | 0 | cctx->currentOffset += (U32)inputSize; |
702 | 0 | cctx->tableType = tableType; |
703 | 0 |
|
704 | 0 | if (inputSize<LZ4_minLength) goto _last_literals; /* Input too small, no compression (all literals) */ |
705 | 0 | |
706 | 0 | /* First Byte */ |
707 | 0 | LZ4_putPosition(ip, cctx->hashTable, tableType, base); |
708 | 0 | ip++; forwardH = LZ4_hashPosition(ip, tableType); |
709 | 0 |
|
710 | 0 | /* Main Loop */ |
711 | 0 | for ( ; ; ) { |
712 | 0 | const BYTE* match; |
713 | 0 | BYTE* token; |
714 | 0 |
|
715 | 0 | /* Find a match */ |
716 | 0 | if (tableType == byPtr) { |
717 | 0 | const BYTE* forwardIp = ip; |
718 | 0 | unsigned step = 1; |
719 | 0 | unsigned searchMatchNb = acceleration << LZ4_skipTrigger; |
720 | 0 | do { |
721 | 0 | U32 const h = forwardH; |
722 | 0 | ip = forwardIp; |
723 | 0 | forwardIp += step; |
724 | 0 | step = (searchMatchNb++ >> LZ4_skipTrigger); |
725 | 0 |
|
726 | 0 | if (unlikely(forwardIp > mflimitPlusOne)) goto _last_literals; |
727 | 0 | assert(ip < mflimitPlusOne); |
728 | 0 |
|
729 | 0 | match = LZ4_getPositionOnHash(h, cctx->hashTable, tableType, base); |
730 | 0 | forwardH = LZ4_hashPosition(forwardIp, tableType); |
731 | 0 | LZ4_putPositionOnHash(ip, h, cctx->hashTable, tableType, base); |
732 | 0 |
|
733 | 0 | } while ( (match+MAX_DISTANCE < ip) |
734 | 0 | || (LZ4_read32(match) != LZ4_read32(ip)) ); |
735 | 0 |
|
736 | 0 | } else { /* byU32, byU16 */ |
737 | 0 |
|
738 | 0 | const BYTE* forwardIp = ip; |
739 | 0 | unsigned step = 1; |
740 | 0 | unsigned searchMatchNb = acceleration << LZ4_skipTrigger; |
741 | 0 | do { |
742 | 0 | U32 const h = forwardH; |
743 | 0 | U32 const current = (U32)(forwardIp - base); |
744 | 0 | U32 matchIndex = LZ4_getIndexOnHash(h, cctx->hashTable, tableType); |
745 | 0 | assert(matchIndex <= current); |
746 | 0 | assert(forwardIp - base < (ptrdiff_t)(2 GB - 1)); |
747 | 0 | ip = forwardIp; |
748 | 0 | forwardIp += step; |
749 | 0 | step = (searchMatchNb++ >> LZ4_skipTrigger); |
750 | 0 |
|
751 | 0 | if (unlikely(forwardIp > mflimitPlusOne)) goto _last_literals; |
752 | 0 | assert(ip < mflimitPlusOne); |
753 | 0 |
|
754 | 0 | if (dictDirective == usingDictCtx) { |
755 | 0 | if (matchIndex < startIndex) { |
756 | 0 | /* there was no match, try the dictionary */ |
757 | 0 | assert(tableType == byU32); |
758 | 0 | matchIndex = LZ4_getIndexOnHash(h, dictCtx->hashTable, byU32); |
759 | 0 | match = dictBase + matchIndex; |
760 | 0 | matchIndex += dictDelta; /* make dictCtx index comparable with current context */ |
761 | 0 | lowLimit = dictionary; |
762 | 0 | } else { |
763 | 0 | match = base + matchIndex; |
764 | 0 | lowLimit = (const BYTE*)source; |
765 | 0 | } |
766 | 0 | } else if (dictDirective==usingExtDict) { |
767 | 0 | if (matchIndex < startIndex) { |
768 | 0 | DEBUGLOG(7, "extDict candidate: matchIndex=%5u < startIndex=%5u", matchIndex, startIndex); |
769 | 0 | assert(startIndex - matchIndex >= MINMATCH); |
770 | 0 | match = dictBase + matchIndex; |
771 | 0 | lowLimit = dictionary; |
772 | 0 | } else { |
773 | 0 | match = base + matchIndex; |
774 | 0 | lowLimit = (const BYTE*)source; |
775 | 0 | } |
776 | 0 | } else { /* single continuous memory segment */ |
777 | 0 | match = base + matchIndex; |
778 | 0 | } |
779 | 0 | forwardH = LZ4_hashPosition(forwardIp, tableType); |
780 | 0 | LZ4_putIndexOnHash(current, h, cctx->hashTable, tableType); |
781 | 0 |
|
782 | 0 | if ((dictIssue == dictSmall) && (matchIndex < prefixIdxLimit)) continue; /* match outside of valid area */ |
783 | 0 | assert(matchIndex < current); |
784 | 0 | if ((tableType != byU16) && (matchIndex+MAX_DISTANCE < current)) continue; /* too far */ |
785 | 0 | if (tableType == byU16) assert((current - matchIndex) <= MAX_DISTANCE); /* too_far presumed impossible with byU16 */ |
786 | 0 |
|
787 | 0 | if (LZ4_read32(match) == LZ4_read32(ip)) { |
788 | 0 | if (maybe_extMem) offset = current - matchIndex; |
789 | 0 | break; /* match found */ |
790 | 0 | } |
791 | 0 |
|
792 | 0 | } while(1); |
793 | 0 | } |
794 | 0 |
|
795 | 0 | /* Catch up */ |
796 | 0 | while (((ip>anchor) & (match > lowLimit)) && (unlikely(ip[-1]==match[-1]))) { ip--; match--; } |
797 | 0 |
|
798 | 0 | /* Encode Literals */ |
799 | 0 | { unsigned const litLength = (unsigned)(ip - anchor); |
800 | 0 | token = op++; |
801 | 0 | if ((outputLimited == limitedOutput) && /* Check output buffer overflow */ |
802 | 0 | (unlikely(op + litLength + (2 + 1 + LASTLITERALS) + (litLength/255) > olimit))) |
803 | 0 | return 0; |
804 | 0 | if ((outputLimited == fillOutput) && |
805 | 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))) { |
806 | 0 | op--; |
807 | 0 | goto _last_literals; |
808 | 0 | } |
809 | 0 | if (litLength >= RUN_MASK) { |
810 | 0 | int len = (int)litLength-RUN_MASK; |
811 | 0 | *token = (RUN_MASK<<ML_BITS); |
812 | 0 | for(; len >= 255 ; len-=255) *op++ = 255; |
813 | 0 | *op++ = (BYTE)len; |
814 | 0 | } |
815 | 0 | else *token = (BYTE)(litLength<<ML_BITS); |
816 | 0 |
|
817 | 0 | /* Copy Literals */ |
818 | 0 | LZ4_wildCopy(op, anchor, op+litLength); |
819 | 0 | op+=litLength; |
820 | 0 | DEBUGLOG(6, "seq.start:%i, literals=%u, match.start:%i", |
821 | 0 | (int)(anchor-(const BYTE*)source), litLength, (int)(ip-(const BYTE*)source)); |
822 | 0 | } |
823 | 0 |
|
824 | 0 | _next_match: |
825 | 0 | /* at this stage, the following variables must be correctly set : |
826 | 0 | * - ip : at start of LZ operation |
827 | 0 | * - match : at start of previous pattern occurence; can be within current prefix, or within extDict |
828 | 0 | * - offset : if maybe_ext_memSegment==1 (constant) |
829 | 0 | * - lowLimit : must be == dictionary to mean "match is within extDict"; must be == source otherwise |
830 | 0 | * - token and *token : position to write 4-bits for match length; higher 4-bits for literal length supposed already written |
831 | 0 | */ |
832 | 0 |
|
833 | 0 | if ((outputLimited == fillOutput) && |
834 | 0 | (op + 2 /* offset */ + 1 /* token */ + MFLIMIT - MINMATCH /* min last literals so last match is <= end - MFLIMIT */ > olimit)) { |
835 | 0 | /* the match was too close to the end, rewind and go to last literals */ |
836 | 0 | op = token; |
837 | 0 | goto _last_literals; |
838 | 0 | } |
839 | 0 | |
840 | 0 | /* Encode Offset */ |
841 | 0 | if (maybe_extMem) { /* static test */ |
842 | 0 | DEBUGLOG(6, " with offset=%u (ext if > %i)", offset, (int)(ip - (const BYTE*)source)); |
843 | 0 | assert(offset <= MAX_DISTANCE && offset > 0); |
844 | 0 | LZ4_writeLE16(op, (U16)offset); op+=2; |
845 | 0 | } else { |
846 | 0 | DEBUGLOG(6, " with offset=%u (same segment)", (U32)(ip - match)); |
847 | 0 | assert(ip-match <= MAX_DISTANCE); |
848 | 0 | LZ4_writeLE16(op, (U16)(ip - match)); op+=2; |
849 | 0 | } |
850 | 0 |
|
851 | 0 | /* Encode MatchLength */ |
852 | 0 | { unsigned matchCode; |
853 | 0 |
|
854 | 0 | if ( (dictDirective==usingExtDict || dictDirective==usingDictCtx) |
855 | 0 | && (lowLimit==dictionary) /* match within extDict */ ) { |
856 | 0 | const BYTE* limit = ip + (dictEnd-match); |
857 | 0 | assert(dictEnd > match); |
858 | 0 | if (limit > matchlimit) limit = matchlimit; |
859 | 0 | matchCode = LZ4_count(ip+MINMATCH, match+MINMATCH, limit); |
860 | 0 | ip += MINMATCH + matchCode; |
861 | 0 | if (ip==limit) { |
862 | 0 | unsigned const more = LZ4_count(limit, (const BYTE*)source, matchlimit); |
863 | 0 | matchCode += more; |
864 | 0 | ip += more; |
865 | 0 | } |
866 | 0 | DEBUGLOG(6, " with matchLength=%u starting in extDict", matchCode+MINMATCH); |
867 | 0 | } else { |
868 | 0 | matchCode = LZ4_count(ip+MINMATCH, match+MINMATCH, matchlimit); |
869 | 0 | ip += MINMATCH + matchCode; |
870 | 0 | DEBUGLOG(6, " with matchLength=%u", matchCode+MINMATCH); |
871 | 0 | } |
872 | 0 |
|
873 | 0 | if ((outputLimited) && /* Check output buffer overflow */ |
874 | 0 | (unlikely(op + (1 + LASTLITERALS) + (matchCode>>8) > olimit)) ) { |
875 | 0 | if (outputLimited == limitedOutput) |
876 | 0 | return 0; |
877 | 0 | if (outputLimited == fillOutput) { |
878 | 0 | /* Match description too long : reduce it */ |
879 | 0 | U32 newMatchCode = 15 /* in token */ - 1 /* to avoid needing a zero byte */ + ((U32)(olimit - op) - 2 - 1 - LASTLITERALS) * 255; |
880 | 0 | ip -= matchCode - newMatchCode; |
881 | 0 | matchCode = newMatchCode; |
882 | 0 | } |
883 | 0 | } |
884 | 0 | if (matchCode >= ML_MASK) { |
885 | 0 | *token += ML_MASK; |
886 | 0 | matchCode -= ML_MASK; |
887 | 0 | LZ4_write32(op, 0xFFFFFFFF); |
888 | 0 | while (matchCode >= 4*255) { |
889 | 0 | op+=4; |
890 | 0 | LZ4_write32(op, 0xFFFFFFFF); |
891 | 0 | matchCode -= 4*255; |
892 | 0 | } |
893 | 0 | op += matchCode / 255; |
894 | 0 | *op++ = (BYTE)(matchCode % 255); |
895 | 0 | } else |
896 | 0 | *token += (BYTE)(matchCode); |
897 | 0 | } |
898 | 0 |
|
899 | 0 | anchor = ip; |
900 | 0 |
|
901 | 0 | /* Test end of chunk */ |
902 | 0 | if (ip >= mflimitPlusOne) break; |
903 | 0 | |
904 | 0 | /* Fill table */ |
905 | 0 | LZ4_putPosition(ip-2, cctx->hashTable, tableType, base); |
906 | 0 |
|
907 | 0 | /* Test next position */ |
908 | 0 | if (tableType == byPtr) { |
909 | 0 |
|
910 | 0 | match = LZ4_getPosition(ip, cctx->hashTable, tableType, base); |
911 | 0 | LZ4_putPosition(ip, cctx->hashTable, tableType, base); |
912 | 0 | if ( (match+MAX_DISTANCE >= ip) |
913 | 0 | && (LZ4_read32(match) == LZ4_read32(ip)) ) |
914 | 0 | { token=op++; *token=0; goto _next_match; } |
915 | 0 | |
916 | 0 | } else { /* byU32, byU16 */ |
917 | 0 |
|
918 | 0 | U32 const h = LZ4_hashPosition(ip, tableType); |
919 | 0 | U32 const current = (U32)(ip-base); |
920 | 0 | U32 matchIndex = LZ4_getIndexOnHash(h, cctx->hashTable, tableType); |
921 | 0 | assert(matchIndex < current); |
922 | 0 | if (dictDirective == usingDictCtx) { |
923 | 0 | if (matchIndex < startIndex) { |
924 | 0 | /* there was no match, try the dictionary */ |
925 | 0 | matchIndex = LZ4_getIndexOnHash(h, dictCtx->hashTable, byU32); |
926 | 0 | match = dictBase + matchIndex; |
927 | 0 | lowLimit = dictionary; /* required for match length counter */ |
928 | 0 | matchIndex += dictDelta; |
929 | 0 | } else { |
930 | 0 | match = base + matchIndex; |
931 | 0 | lowLimit = (const BYTE*)source; /* required for match length counter */ |
932 | 0 | } |
933 | 0 | } else if (dictDirective==usingExtDict) { |
934 | 0 | if (matchIndex < startIndex) { |
935 | 0 | match = dictBase + matchIndex; |
936 | 0 | lowLimit = dictionary; /* required for match length counter */ |
937 | 0 | } else { |
938 | 0 | match = base + matchIndex; |
939 | 0 | lowLimit = (const BYTE*)source; /* required for match length counter */ |
940 | 0 | } |
941 | 0 | } else { /* single memory segment */ |
942 | 0 | match = base + matchIndex; |
943 | 0 | } |
944 | 0 | LZ4_putIndexOnHash(current, h, cctx->hashTable, tableType); |
945 | 0 | assert(matchIndex < current); |
946 | 0 | if ( ((dictIssue==dictSmall) ? (matchIndex >= prefixIdxLimit) : 1) |
947 | 0 | && ((tableType==byU16) ? 1 : (matchIndex+MAX_DISTANCE >= current)) |
948 | 0 | && (LZ4_read32(match) == LZ4_read32(ip)) ) { |
949 | 0 | token=op++; |
950 | 0 | *token=0; |
951 | 0 | if (maybe_extMem) offset = current - matchIndex; |
952 | 0 | DEBUGLOG(6, "seq.start:%i, literals=%u, match.start:%i", |
953 | 0 | (int)(anchor-(const BYTE*)source), 0, (int)(ip-(const BYTE*)source)); |
954 | 0 | goto _next_match; |
955 | 0 | } |
956 | 0 | } |
957 | 0 |
|
958 | 0 | /* Prepare next loop */ |
959 | 0 | forwardH = LZ4_hashPosition(++ip, tableType); |
960 | 0 |
|
961 | 0 | } |
962 | 0 |
|
963 | 0 | _last_literals: |
964 | 0 | /* Encode Last Literals */ |
965 | 0 | { size_t lastRun = (size_t)(iend - anchor); |
966 | 0 | if ( (outputLimited) && /* Check output buffer overflow */ |
967 | 0 | (op + lastRun + 1 + ((lastRun+255-RUN_MASK)/255) > olimit)) { |
968 | 0 | if (outputLimited == fillOutput) { |
969 | 0 | /* adapt lastRun to fill 'dst' */ |
970 | 0 | lastRun = (olimit-op) - 1; |
971 | 0 | lastRun -= (lastRun+240)/255; |
972 | 0 | } |
973 | 0 | if (outputLimited == limitedOutput) |
974 | 0 | return 0; |
975 | 0 | } |
976 | 0 | if (lastRun >= RUN_MASK) { |
977 | 0 | size_t accumulator = lastRun - RUN_MASK; |
978 | 0 | *op++ = RUN_MASK << ML_BITS; |
979 | 0 | for(; accumulator >= 255 ; accumulator-=255) *op++ = 255; |
980 | 0 | *op++ = (BYTE) accumulator; |
981 | 0 | } else { |
982 | 0 | *op++ = (BYTE)(lastRun<<ML_BITS); |
983 | 0 | } |
984 | 0 | memcpy(op, anchor, lastRun); |
985 | 0 | ip = anchor + lastRun; |
986 | 0 | op += lastRun; |
987 | 0 | } |
988 | 0 |
|
989 | 0 | if (outputLimited == fillOutput) { |
990 | 0 | *inputConsumed = (int) (((const char*)ip)-source); |
991 | 0 | } |
992 | 0 | DEBUGLOG(5, "LZ4_compress_generic: compressed %i bytes into %i bytes", inputSize, (int)(((char*)op) - dest)); |
993 | 0 | return (int)(((char*)op) - dest); |
994 | 0 | } |
995 | | |
996 | | |
997 | | int LZ4_compress_fast_extState(void* state, const char* source, char* dest, int inputSize, int maxOutputSize, int acceleration) |
998 | 0 | { |
999 | 0 | LZ4_stream_t_internal* ctx = &((LZ4_stream_t*)state)->internal_donotuse; |
1000 | 0 | if (acceleration < 1) acceleration = ACCELERATION_DEFAULT; |
1001 | 0 | LZ4_resetStream((LZ4_stream_t*)state); |
1002 | 0 | if (maxOutputSize >= LZ4_compressBound(inputSize)) { |
1003 | 0 | if (inputSize < LZ4_64Klimit) { |
1004 | 0 | return LZ4_compress_generic(ctx, source, dest, inputSize, NULL, 0, notLimited, byU16, noDict, noDictIssue, acceleration); |
1005 | 0 | } else { |
1006 | 0 | const tableType_t tableType = ((sizeof(void*)==4) && ((uptrval)source > MAX_DISTANCE)) ? byPtr : byU32; |
1007 | 0 | return LZ4_compress_generic(ctx, source, dest, inputSize, NULL, 0, notLimited, tableType, noDict, noDictIssue, acceleration); |
1008 | 0 | } |
1009 | 0 | } else { |
1010 | 0 | if (inputSize < LZ4_64Klimit) {; |
1011 | 0 | return LZ4_compress_generic(ctx, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, byU16, noDict, noDictIssue, acceleration); |
1012 | 0 | } else { |
1013 | 0 | const tableType_t tableType = ((sizeof(void*)==4) && ((uptrval)source > MAX_DISTANCE)) ? byPtr : byU32; |
1014 | 0 | return LZ4_compress_generic(ctx, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, noDict, noDictIssue, acceleration); |
1015 | 0 | } |
1016 | 0 | } |
1017 | 0 | } |
1018 | | |
1019 | | /** |
1020 | | * LZ4_compress_fast_extState_fastReset() : |
1021 | | * A variant of LZ4_compress_fast_extState(). |
1022 | | * |
1023 | | * Using this variant avoids an expensive initialization step. It is only safe |
1024 | | * to call if the state buffer is known to be correctly initialized already |
1025 | | * (see comment in lz4.h on LZ4_resetStream_fast() for a definition of |
1026 | | * "correctly initialized"). |
1027 | | */ |
1028 | | int LZ4_compress_fast_extState_fastReset(void* state, const char* src, char* dst, int srcSize, int dstCapacity, int acceleration) |
1029 | 0 | { |
1030 | 0 | LZ4_stream_t_internal* ctx = &((LZ4_stream_t*)state)->internal_donotuse; |
1031 | 0 | if (acceleration < 1) acceleration = ACCELERATION_DEFAULT; |
1032 | 0 |
|
1033 | 0 | if (dstCapacity >= LZ4_compressBound(srcSize)) { |
1034 | 0 | if (srcSize < LZ4_64Klimit) { |
1035 | 0 | const tableType_t tableType = byU16; |
1036 | 0 | LZ4_prepareTable(ctx, srcSize, tableType); |
1037 | 0 | if (ctx->currentOffset) { |
1038 | 0 | return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, 0, notLimited, tableType, noDict, dictSmall, acceleration); |
1039 | 0 | } else { |
1040 | 0 | return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, 0, notLimited, tableType, noDict, noDictIssue, acceleration); |
1041 | 0 | } |
1042 | 0 | } else { |
1043 | 0 | const tableType_t tableType = ((sizeof(void*)==4) && ((uptrval)src > MAX_DISTANCE)) ? byPtr : byU32; |
1044 | 0 | LZ4_prepareTable(ctx, srcSize, tableType); |
1045 | 0 | return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, 0, notLimited, tableType, noDict, noDictIssue, acceleration); |
1046 | 0 | } |
1047 | 0 | } else { |
1048 | 0 | if (srcSize < LZ4_64Klimit) { |
1049 | 0 | const tableType_t tableType = byU16; |
1050 | 0 | LZ4_prepareTable(ctx, srcSize, tableType); |
1051 | 0 | if (ctx->currentOffset) { |
1052 | 0 | return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, dstCapacity, limitedOutput, tableType, noDict, dictSmall, acceleration); |
1053 | 0 | } else { |
1054 | 0 | return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, dstCapacity, limitedOutput, tableType, noDict, noDictIssue, acceleration); |
1055 | 0 | } |
1056 | 0 | } else { |
1057 | 0 | const tableType_t tableType = ((sizeof(void*)==4) && ((uptrval)src > MAX_DISTANCE)) ? byPtr : byU32; |
1058 | 0 | LZ4_prepareTable(ctx, srcSize, tableType); |
1059 | 0 | return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, dstCapacity, limitedOutput, tableType, noDict, noDictIssue, acceleration); |
1060 | 0 | } |
1061 | 0 | } |
1062 | 0 | } |
1063 | | |
1064 | | |
1065 | | int LZ4_compress_fast(const char* source, char* dest, int inputSize, int maxOutputSize, int acceleration) |
1066 | 0 | { |
1067 | 0 | int result; |
1068 | | #if (LZ4_HEAPMODE) |
1069 | | LZ4_stream_t* ctxPtr = ALLOC(sizeof(LZ4_stream_t)); /* malloc-calloc always properly aligned */ |
1070 | | if (ctxPtr == NULL) return 0; |
1071 | | #else |
1072 | | LZ4_stream_t ctx; |
1073 | 0 | LZ4_stream_t* const ctxPtr = &ctx; |
1074 | 0 | #endif |
1075 | 0 | result = LZ4_compress_fast_extState(ctxPtr, source, dest, inputSize, maxOutputSize, acceleration); |
1076 | 0 |
|
1077 | | #if (LZ4_HEAPMODE) |
1078 | | FREEMEM(ctxPtr); |
1079 | | #endif |
1080 | | return result; |
1081 | 0 | } |
1082 | | |
1083 | | |
1084 | | int LZ4_compress_default(const char* source, char* dest, int inputSize, int maxOutputSize) |
1085 | 0 | { |
1086 | 0 | return LZ4_compress_fast(source, dest, inputSize, maxOutputSize, 1); |
1087 | 0 | } |
1088 | | |
1089 | | |
1090 | | /* hidden debug function */ |
1091 | | /* strangely enough, gcc generates faster code when this function is uncommented, even if unused */ |
1092 | | int LZ4_compress_fast_force(const char* source, char* dest, int inputSize, int maxOutputSize, int acceleration) |
1093 | 0 | { |
1094 | 0 | LZ4_stream_t ctx; |
1095 | 0 | LZ4_resetStream(&ctx); |
1096 | 0 |
|
1097 | 0 | if (inputSize < LZ4_64Klimit) |
1098 | 0 | return LZ4_compress_generic(&ctx.internal_donotuse, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, byU16, noDict, noDictIssue, acceleration); |
1099 | 0 | else |
1100 | 0 | return LZ4_compress_generic(&ctx.internal_donotuse, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, sizeof(void*)==8 ? byU32 : byPtr, noDict, noDictIssue, acceleration); |
1101 | 0 | } |
1102 | | |
1103 | | |
1104 | | /* Note!: This function leaves the stream in an unclean/broken state! |
1105 | | * It is not safe to subsequently use the same state with a _fastReset() or |
1106 | | * _continue() call without resetting it. */ |
1107 | | static int LZ4_compress_destSize_extState (LZ4_stream_t* state, const char* src, char* dst, int* srcSizePtr, int targetDstSize) |
1108 | 0 | { |
1109 | 0 | LZ4_resetStream(state); |
1110 | 0 |
|
1111 | 0 | if (targetDstSize >= LZ4_compressBound(*srcSizePtr)) { /* compression success is guaranteed */ |
1112 | 0 | return LZ4_compress_fast_extState(state, src, dst, *srcSizePtr, targetDstSize, 1); |
1113 | 0 | } else { |
1114 | 0 | if (*srcSizePtr < LZ4_64Klimit) { |
1115 | 0 | return LZ4_compress_generic(&state->internal_donotuse, src, dst, *srcSizePtr, srcSizePtr, targetDstSize, fillOutput, byU16, noDict, noDictIssue, 1); |
1116 | 0 | } else { |
1117 | 0 | tableType_t const tableType = ((sizeof(void*)==4) && ((uptrval)src > MAX_DISTANCE)) ? byPtr : byU32; |
1118 | 0 | return LZ4_compress_generic(&state->internal_donotuse, src, dst, *srcSizePtr, srcSizePtr, targetDstSize, fillOutput, tableType, noDict, noDictIssue, 1); |
1119 | 0 | } } |
1120 | 0 | } |
1121 | | |
1122 | | |
1123 | | int LZ4_compress_destSize(const char* src, char* dst, int* srcSizePtr, int targetDstSize) |
1124 | 0 | { |
1125 | | #if (LZ4_HEAPMODE) |
1126 | | LZ4_stream_t* ctx = (LZ4_stream_t*)ALLOC(sizeof(LZ4_stream_t)); /* malloc-calloc always properly aligned */ |
1127 | | if (ctx == NULL) return 0; |
1128 | | #else |
1129 | | LZ4_stream_t ctxBody; |
1130 | 0 | LZ4_stream_t* ctx = &ctxBody; |
1131 | 0 | #endif |
1132 | 0 |
|
1133 | 0 | int result = LZ4_compress_destSize_extState(ctx, src, dst, srcSizePtr, targetDstSize); |
1134 | 0 |
|
1135 | | #if (LZ4_HEAPMODE) |
1136 | | FREEMEM(ctx); |
1137 | | #endif |
1138 | | return result; |
1139 | 0 | } |
1140 | | |
1141 | | |
1142 | | |
1143 | | /*-****************************** |
1144 | | * Streaming functions |
1145 | | ********************************/ |
1146 | | |
1147 | | LZ4_stream_t* LZ4_createStream(void) |
1148 | 0 | { |
1149 | 0 | LZ4_stream_t* lz4s = (LZ4_stream_t*)ALLOC(sizeof(LZ4_stream_t)); |
1150 | 0 | LZ4_STATIC_ASSERT(LZ4_STREAMSIZE >= sizeof(LZ4_stream_t_internal)); /* A compilation error here means LZ4_STREAMSIZE is not large enough */ |
1151 | 0 | DEBUGLOG(4, "LZ4_createStream %p", lz4s); |
1152 | 0 | if (lz4s == NULL) return NULL; |
1153 | 0 | LZ4_resetStream(lz4s); |
1154 | 0 | return lz4s; |
1155 | 0 | } |
1156 | | |
1157 | | void LZ4_resetStream (LZ4_stream_t* LZ4_stream) |
1158 | 0 | { |
1159 | 0 | DEBUGLOG(5, "LZ4_resetStream (ctx:%p)", LZ4_stream); |
1160 | 0 | MEM_INIT(LZ4_stream, 0, sizeof(LZ4_stream_t)); |
1161 | 0 | } |
1162 | | |
1163 | 0 | void LZ4_resetStream_fast(LZ4_stream_t* ctx) { |
1164 | 0 | LZ4_prepareTable(&(ctx->internal_donotuse), 0, byU32); |
1165 | 0 | } |
1166 | | |
1167 | | int LZ4_freeStream (LZ4_stream_t* LZ4_stream) |
1168 | 0 | { |
1169 | 0 | if (!LZ4_stream) return 0; /* support free on NULL */ |
1170 | 0 | DEBUGLOG(5, "LZ4_freeStream %p", LZ4_stream); |
1171 | 0 | FREEMEM(LZ4_stream); |
1172 | 0 | return (0); |
1173 | 0 | } |
1174 | | |
1175 | | |
1176 | 0 | #define HASH_UNIT sizeof(reg_t) |
1177 | | int LZ4_loadDict (LZ4_stream_t* LZ4_dict, const char* dictionary, int dictSize) |
1178 | 0 | { |
1179 | 0 | LZ4_stream_t_internal* dict = &LZ4_dict->internal_donotuse; |
1180 | 0 | const tableType_t tableType = byU32; |
1181 | 0 | const BYTE* p = (const BYTE*)dictionary; |
1182 | 0 | const BYTE* const dictEnd = p + dictSize; |
1183 | 0 | const BYTE* base; |
1184 | 0 |
|
1185 | 0 | DEBUGLOG(4, "LZ4_loadDict (%i bytes from %p into %p)", dictSize, dictionary, LZ4_dict); |
1186 | 0 |
|
1187 | 0 | /* It's necessary to reset the context, |
1188 | 0 | * and not just continue it with prepareTable() |
1189 | 0 | * to avoid any risk of generating overflowing matchIndex |
1190 | 0 | * when compressing using this dictionary */ |
1191 | 0 | LZ4_resetStream(LZ4_dict); |
1192 | 0 |
|
1193 | 0 | /* We always increment the offset by 64 KB, since, if the dict is longer, |
1194 | 0 | * we truncate it to the last 64k, and if it's shorter, we still want to |
1195 | 0 | * advance by a whole window length so we can provide the guarantee that |
1196 | 0 | * there are only valid offsets in the window, which allows an optimization |
1197 | 0 | * in LZ4_compress_fast_continue() where it uses noDictIssue even when the |
1198 | 0 | * dictionary isn't a full 64k. */ |
1199 | 0 |
|
1200 | 0 | if ((dictEnd - p) > 64 KB) p = dictEnd - 64 KB; |
1201 | 0 | base = dictEnd - 64 KB - dict->currentOffset; |
1202 | 0 | dict->dictionary = p; |
1203 | 0 | dict->dictSize = (U32)(dictEnd - p); |
1204 | 0 | dict->currentOffset += 64 KB; |
1205 | 0 | dict->tableType = tableType; |
1206 | 0 |
|
1207 | 0 | if (dictSize < (int)HASH_UNIT) { |
1208 | 0 | return 0; |
1209 | 0 | } |
1210 | 0 | |
1211 | 0 | while (p <= dictEnd-HASH_UNIT) { |
1212 | 0 | LZ4_putPosition(p, dict->hashTable, tableType, base); |
1213 | 0 | p+=3; |
1214 | 0 | } |
1215 | 0 |
|
1216 | 0 | return dict->dictSize; |
1217 | 0 | } |
1218 | | |
1219 | 0 | void LZ4_attach_dictionary(LZ4_stream_t *working_stream, const LZ4_stream_t *dictionary_stream) { |
1220 | 0 | if (dictionary_stream != NULL) { |
1221 | 0 | /* If the current offset is zero, we will never look in the |
1222 | 0 | * external dictionary context, since there is no value a table |
1223 | 0 | * entry can take that indicate a miss. In that case, we need |
1224 | 0 | * to bump the offset to something non-zero. |
1225 | 0 | */ |
1226 | 0 | if (working_stream->internal_donotuse.currentOffset == 0) { |
1227 | 0 | working_stream->internal_donotuse.currentOffset = 64 KB; |
1228 | 0 | } |
1229 | 0 | working_stream->internal_donotuse.dictCtx = &(dictionary_stream->internal_donotuse); |
1230 | 0 | } else { |
1231 | 0 | working_stream->internal_donotuse.dictCtx = NULL; |
1232 | 0 | } |
1233 | 0 | } |
1234 | | |
1235 | | |
1236 | | static void LZ4_renormDictT(LZ4_stream_t_internal* LZ4_dict, int nextSize) |
1237 | 0 | { |
1238 | 0 | if (LZ4_dict->currentOffset + nextSize > 0x80000000) { /* potential ptrdiff_t overflow (32-bits mode) */ |
1239 | 0 | /* rescale hash table */ |
1240 | 0 | U32 const delta = LZ4_dict->currentOffset - 64 KB; |
1241 | 0 | const BYTE* dictEnd = LZ4_dict->dictionary + LZ4_dict->dictSize; |
1242 | 0 | int i; |
1243 | 0 | DEBUGLOG(4, "LZ4_renormDictT"); |
1244 | 0 | for (i=0; i<LZ4_HASH_SIZE_U32; i++) { |
1245 | 0 | if (LZ4_dict->hashTable[i] < delta) LZ4_dict->hashTable[i]=0; |
1246 | 0 | else LZ4_dict->hashTable[i] -= delta; |
1247 | 0 | } |
1248 | 0 | LZ4_dict->currentOffset = 64 KB; |
1249 | 0 | if (LZ4_dict->dictSize > 64 KB) LZ4_dict->dictSize = 64 KB; |
1250 | 0 | LZ4_dict->dictionary = dictEnd - LZ4_dict->dictSize; |
1251 | 0 | } |
1252 | 0 | } |
1253 | | |
1254 | | |
1255 | | int LZ4_compress_fast_continue (LZ4_stream_t* LZ4_stream, const char* source, char* dest, int inputSize, int maxOutputSize, int acceleration) |
1256 | 0 | { |
1257 | 0 | const tableType_t tableType = byU32; |
1258 | 0 | LZ4_stream_t_internal* streamPtr = &LZ4_stream->internal_donotuse; |
1259 | 0 | const BYTE* dictEnd = streamPtr->dictionary + streamPtr->dictSize; |
1260 | 0 |
|
1261 | 0 | DEBUGLOG(5, "LZ4_compress_fast_continue (inputSize=%i)", inputSize); |
1262 | 0 |
|
1263 | 0 | if (streamPtr->initCheck) return 0; /* Uninitialized structure detected */ |
1264 | 0 | LZ4_renormDictT(streamPtr, inputSize); /* avoid index overflow */ |
1265 | 0 | if (acceleration < 1) acceleration = ACCELERATION_DEFAULT; |
1266 | 0 |
|
1267 | 0 | /* invalidate tiny dictionaries */ |
1268 | 0 | if ( (streamPtr->dictSize-1 < 4) /* intentional underflow */ |
1269 | 0 | && (dictEnd != (const BYTE*)source) ) { |
1270 | 0 | DEBUGLOG(5, "LZ4_compress_fast_continue: dictSize(%u) at addr:%p is too small", streamPtr->dictSize, streamPtr->dictionary); |
1271 | 0 | streamPtr->dictSize = 0; |
1272 | 0 | streamPtr->dictionary = (const BYTE*)source; |
1273 | 0 | dictEnd = (const BYTE*)source; |
1274 | 0 | } |
1275 | 0 |
|
1276 | 0 | /* Check overlapping input/dictionary space */ |
1277 | 0 | { const BYTE* sourceEnd = (const BYTE*) source + inputSize; |
1278 | 0 | if ((sourceEnd > streamPtr->dictionary) && (sourceEnd < dictEnd)) { |
1279 | 0 | streamPtr->dictSize = (U32)(dictEnd - sourceEnd); |
1280 | 0 | if (streamPtr->dictSize > 64 KB) streamPtr->dictSize = 64 KB; |
1281 | 0 | if (streamPtr->dictSize < 4) streamPtr->dictSize = 0; |
1282 | 0 | streamPtr->dictionary = dictEnd - streamPtr->dictSize; |
1283 | 0 | } |
1284 | 0 | } |
1285 | 0 |
|
1286 | 0 | /* prefix mode : source data follows dictionary */ |
1287 | 0 | if (dictEnd == (const BYTE*)source) { |
1288 | 0 | if ((streamPtr->dictSize < 64 KB) && (streamPtr->dictSize < streamPtr->currentOffset)) |
1289 | 0 | return LZ4_compress_generic(streamPtr, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, withPrefix64k, dictSmall, acceleration); |
1290 | 0 | else |
1291 | 0 | return LZ4_compress_generic(streamPtr, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, withPrefix64k, noDictIssue, acceleration); |
1292 | 0 | } |
1293 | 0 | |
1294 | 0 | /* external dictionary mode */ |
1295 | 0 | { int result; |
1296 | 0 | if (streamPtr->dictCtx) { |
1297 | 0 | /* We depend here on the fact that dictCtx'es (produced by |
1298 | 0 | * LZ4_loadDict) guarantee that their tables contain no references |
1299 | 0 | * to offsets between dictCtx->currentOffset - 64 KB and |
1300 | 0 | * dictCtx->currentOffset - dictCtx->dictSize. This makes it safe |
1301 | 0 | * to use noDictIssue even when the dict isn't a full 64 KB. |
1302 | 0 | */ |
1303 | 0 | if (inputSize > 4 KB) { |
1304 | 0 | /* For compressing large blobs, it is faster to pay the setup |
1305 | 0 | * cost to copy the dictionary's tables into the active context, |
1306 | 0 | * so that the compression loop is only looking into one table. |
1307 | 0 | */ |
1308 | 0 | memcpy(streamPtr, streamPtr->dictCtx, sizeof(LZ4_stream_t)); |
1309 | 0 | result = LZ4_compress_generic(streamPtr, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, usingExtDict, noDictIssue, acceleration); |
1310 | 0 | } else { |
1311 | 0 | result = LZ4_compress_generic(streamPtr, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, usingDictCtx, noDictIssue, acceleration); |
1312 | 0 | } |
1313 | 0 | } else { |
1314 | 0 | if ((streamPtr->dictSize < 64 KB) && (streamPtr->dictSize < streamPtr->currentOffset)) { |
1315 | 0 | result = LZ4_compress_generic(streamPtr, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, usingExtDict, dictSmall, acceleration); |
1316 | 0 | } else { |
1317 | 0 | result = LZ4_compress_generic(streamPtr, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, usingExtDict, noDictIssue, acceleration); |
1318 | 0 | } |
1319 | 0 | } |
1320 | 0 | streamPtr->dictionary = (const BYTE*)source; |
1321 | 0 | streamPtr->dictSize = (U32)inputSize; |
1322 | 0 | return result; |
1323 | 0 | } |
1324 | 0 | } |
1325 | | |
1326 | | |
1327 | | /* Hidden debug function, to force-test external dictionary mode */ |
1328 | | int LZ4_compress_forceExtDict (LZ4_stream_t* LZ4_dict, const char* source, char* dest, int srcSize) |
1329 | 0 | { |
1330 | 0 | LZ4_stream_t_internal* streamPtr = &LZ4_dict->internal_donotuse; |
1331 | 0 | int result; |
1332 | 0 |
|
1333 | 0 | LZ4_renormDictT(streamPtr, srcSize); |
1334 | 0 |
|
1335 | 0 | if ((streamPtr->dictSize < 64 KB) && (streamPtr->dictSize < streamPtr->currentOffset)) { |
1336 | 0 | result = LZ4_compress_generic(streamPtr, source, dest, srcSize, NULL, 0, notLimited, byU32, usingExtDict, dictSmall, 1); |
1337 | 0 | } else { |
1338 | 0 | result = LZ4_compress_generic(streamPtr, source, dest, srcSize, NULL, 0, notLimited, byU32, usingExtDict, noDictIssue, 1); |
1339 | 0 | } |
1340 | 0 |
|
1341 | 0 | streamPtr->dictionary = (const BYTE*)source; |
1342 | 0 | streamPtr->dictSize = (U32)srcSize; |
1343 | 0 |
|
1344 | 0 | return result; |
1345 | 0 | } |
1346 | | |
1347 | | |
1348 | | /*! LZ4_saveDict() : |
1349 | | * If previously compressed data block is not guaranteed to remain available at its memory location, |
1350 | | * save it into a safer place (char* safeBuffer). |
1351 | | * Note : you don't need to call LZ4_loadDict() afterwards, |
1352 | | * dictionary is immediately usable, you can therefore call LZ4_compress_fast_continue(). |
1353 | | * Return : saved dictionary size in bytes (necessarily <= dictSize), or 0 if error. |
1354 | | */ |
1355 | | int LZ4_saveDict (LZ4_stream_t* LZ4_dict, char* safeBuffer, int dictSize) |
1356 | 0 | { |
1357 | 0 | LZ4_stream_t_internal* const dict = &LZ4_dict->internal_donotuse; |
1358 | 0 | const BYTE* const previousDictEnd = dict->dictionary + dict->dictSize; |
1359 | 0 |
|
1360 | 0 | if ((U32)dictSize > 64 KB) dictSize = 64 KB; /* useless to define a dictionary > 64 KB */ |
1361 | 0 | if ((U32)dictSize > dict->dictSize) dictSize = dict->dictSize; |
1362 | 0 |
|
1363 | 0 | memmove(safeBuffer, previousDictEnd - dictSize, dictSize); |
1364 | 0 |
|
1365 | 0 | dict->dictionary = (const BYTE*)safeBuffer; |
1366 | 0 | dict->dictSize = (U32)dictSize; |
1367 | 0 |
|
1368 | 0 | return dictSize; |
1369 | 0 | } |
1370 | | |
1371 | | |
1372 | | |
1373 | | /*-***************************** |
1374 | | * Decompression functions |
1375 | | *******************************/ |
1376 | | /*! LZ4_decompress_generic() : |
1377 | | * This generic decompression function covers all use cases. |
1378 | | * It shall be instantiated several times, using different sets of directives. |
1379 | | * Note that it is important for performance that this function really get inlined, |
1380 | | * in order to remove useless branches during compilation optimization. |
1381 | | */ |
1382 | | LZ4_FORCE_O2_GCC_PPC64LE |
1383 | | LZ4_FORCE_INLINE int LZ4_decompress_generic( |
1384 | | const char* const src, |
1385 | | char* const dst, |
1386 | | int srcSize, |
1387 | | int outputSize, /* If endOnInput==endOnInputSize, this value is `dstCapacity` */ |
1388 | | |
1389 | | int endOnInput, /* endOnOutputSize, endOnInputSize */ |
1390 | | int partialDecoding, /* full, partial */ |
1391 | | int targetOutputSize, /* only used if partialDecoding==partial */ |
1392 | | int dict, /* noDict, withPrefix64k, usingExtDict */ |
1393 | | const BYTE* const lowPrefix, /* always <= dst, == dst when no prefix */ |
1394 | | const BYTE* const dictStart, /* only if dict==usingExtDict */ |
1395 | | const size_t dictSize /* note : = 0 if noDict */ |
1396 | | ) |
1397 | 0 | { |
1398 | 0 | const BYTE* ip = (const BYTE*) src; |
1399 | 0 | const BYTE* const iend = ip + srcSize; |
1400 | 0 |
|
1401 | 0 | BYTE* op = (BYTE*) dst; |
1402 | 0 | BYTE* const oend = op + outputSize; |
1403 | 0 | BYTE* cpy; |
1404 | 0 | BYTE* oexit = op + targetOutputSize; |
1405 | 0 |
|
1406 | 0 | const BYTE* const dictEnd = (const BYTE*)dictStart + dictSize; |
1407 | 0 | const unsigned inc32table[8] = {0, 1, 2, 1, 0, 4, 4, 4}; |
1408 | 0 | const int dec64table[8] = {0, 0, 0, -1, -4, 1, 2, 3}; |
1409 | 0 |
|
1410 | 0 | const int safeDecode = (endOnInput==endOnInputSize); |
1411 | 0 | const int checkOffset = ((safeDecode) && (dictSize < (int)(64 KB))); |
1412 | 0 |
|
1413 | 0 | /* Set up the "end" pointers for the shortcut. */ |
1414 | 0 | const BYTE* const shortiend = iend - (endOnInput ? 14 : 8) /*maxLL*/ - 2 /*offset*/; |
1415 | 0 | const BYTE* const shortoend = oend - (endOnInput ? 14 : 8) /*maxLL*/ - 18 /*maxML*/; |
1416 | 0 |
|
1417 | 0 | DEBUGLOG(5, "LZ4_decompress_generic (srcSize:%i)", srcSize); |
1418 | 0 |
|
1419 | 0 | /* Special cases */ |
1420 | 0 | if ((partialDecoding) && (oexit > oend-MFLIMIT)) oexit = oend-MFLIMIT; /* targetOutputSize too high => just decode everything */ |
1421 | 0 | if ((endOnInput) && (unlikely(outputSize==0))) return ((srcSize==1) && (*ip==0)) ? 0 : -1; /* Empty output buffer */ |
1422 | 0 | if ((!endOnInput) && (unlikely(outputSize==0))) return (*ip==0?1:-1); |
1423 | 0 | if ((endOnInput) && unlikely(srcSize==0)) return -1; |
1424 | 0 | |
1425 | 0 | /* Main Loop : decode sequences */ |
1426 | 0 | while (1) { |
1427 | 0 | const BYTE* match; |
1428 | 0 | size_t offset; |
1429 | 0 |
|
1430 | 0 | unsigned const token = *ip++; |
1431 | 0 | size_t length = token >> ML_BITS; /* literal length */ |
1432 | 0 |
|
1433 | 0 | assert(!endOnInput || ip <= iend); /* ip < iend before the increment */ |
1434 | 0 |
|
1435 | 0 | /* A two-stage shortcut for the most common case: |
1436 | 0 | * 1) If the literal length is 0..14, and there is enough space, |
1437 | 0 | * enter the shortcut and copy 16 bytes on behalf of the literals |
1438 | 0 | * (in the fast mode, only 8 bytes can be safely copied this way). |
1439 | 0 | * 2) Further if the match length is 4..18, copy 18 bytes in a similar |
1440 | 0 | * manner; but we ensure that there's enough space in the output for |
1441 | 0 | * those 18 bytes earlier, upon entering the shortcut (in other words, |
1442 | 0 | * there is a combined check for both stages). |
1443 | 0 | */ |
1444 | 0 | if ( (endOnInput ? length != RUN_MASK : length <= 8) |
1445 | 0 | /* strictly "less than" on input, to re-enter the loop with at least one byte */ |
1446 | 0 | && likely((endOnInput ? ip < shortiend : 1) & (op <= shortoend)) ) { |
1447 | 0 | /* Copy the literals */ |
1448 | 0 | memcpy(op, ip, endOnInput ? 16 : 8); |
1449 | 0 | op += length; ip += length; |
1450 | 0 |
|
1451 | 0 | /* The second stage: prepare for match copying, decode full info. |
1452 | 0 | * If it doesn't work out, the info won't be wasted. */ |
1453 | 0 | length = token & ML_MASK; /* match length */ |
1454 | 0 | offset = LZ4_readLE16(ip); ip += 2; |
1455 | 0 | match = op - offset; |
1456 | 0 |
|
1457 | 0 | /* Do not deal with overlapping matches. */ |
1458 | 0 | if ( (length != ML_MASK) |
1459 | 0 | && (offset >= 8) |
1460 | 0 | && (dict==withPrefix64k || match >= lowPrefix) ) { |
1461 | 0 | /* Copy the match. */ |
1462 | 0 | memcpy(op + 0, match + 0, 8); |
1463 | 0 | memcpy(op + 8, match + 8, 8); |
1464 | 0 | memcpy(op +16, match +16, 2); |
1465 | 0 | op += length + MINMATCH; |
1466 | 0 | /* Both stages worked, load the next token. */ |
1467 | 0 | continue; |
1468 | 0 | } |
1469 | 0 |
|
1470 | 0 | /* The second stage didn't work out, but the info is ready. |
1471 | 0 | * Propel it right to the point of match copying. */ |
1472 | 0 | goto _copy_match; |
1473 | 0 | } |
1474 | 0 | |
1475 | 0 | /* decode literal length */ |
1476 | 0 | if (length == RUN_MASK) { |
1477 | 0 | unsigned s; |
1478 | 0 | if (unlikely(endOnInput ? ip >= iend-RUN_MASK : 0)) goto _output_error; /* overflow detection */ |
1479 | 0 | do { |
1480 | 0 | s = *ip++; |
1481 | 0 | length += s; |
1482 | 0 | } while ( likely(endOnInput ? ip<iend-RUN_MASK : 1) & (s==255) ); |
1483 | 0 | if ((safeDecode) && unlikely((uptrval)(op)+length<(uptrval)(op))) goto _output_error; /* overflow detection */ |
1484 | 0 | if ((safeDecode) && unlikely((uptrval)(ip)+length<(uptrval)(ip))) goto _output_error; /* overflow detection */ |
1485 | 0 | } |
1486 | 0 | |
1487 | 0 | /* copy literals */ |
1488 | 0 | cpy = op+length; |
1489 | 0 | if ( ((endOnInput) && ((cpy>(partialDecoding?oexit:oend-MFLIMIT)) || (ip+length>iend-(2+1+LASTLITERALS))) ) |
1490 | 0 | || ((!endOnInput) && (cpy>oend-WILDCOPYLENGTH)) ) |
1491 | 0 | { |
1492 | 0 | if (partialDecoding) { |
1493 | 0 | if (cpy > oend) goto _output_error; /* Error : write attempt beyond end of output buffer */ |
1494 | 0 | if ((endOnInput) && (ip+length > iend)) goto _output_error; /* Error : read attempt beyond end of input buffer */ |
1495 | 0 | } else { |
1496 | 0 | if ((!endOnInput) && (cpy != oend)) goto _output_error; /* Error : block decoding must stop exactly there */ |
1497 | 0 | if ((endOnInput) && ((ip+length != iend) || (cpy > oend))) goto _output_error; /* Error : input must be consumed */ |
1498 | 0 | } |
1499 | 0 | memcpy(op, ip, length); |
1500 | 0 | ip += length; |
1501 | 0 | op += length; |
1502 | 0 | break; /* Necessarily EOF, due to parsing restrictions */ |
1503 | 0 | } |
1504 | 0 | LZ4_wildCopy(op, ip, cpy); |
1505 | 0 | ip += length; op = cpy; |
1506 | 0 |
|
1507 | 0 | /* get offset */ |
1508 | 0 | offset = LZ4_readLE16(ip); ip+=2; |
1509 | 0 | match = op - offset; |
1510 | 0 |
|
1511 | 0 | /* get matchlength */ |
1512 | 0 | length = token & ML_MASK; |
1513 | 0 |
|
1514 | 0 | _copy_match: |
1515 | 0 | if ((checkOffset) && (unlikely(match + dictSize < lowPrefix))) goto _output_error; /* Error : offset outside buffers */ |
1516 | 0 | LZ4_write32(op, (U32)offset); /* costs ~1%; silence an msan warning when offset==0 */ |
1517 | 0 |
|
1518 | 0 | if (length == ML_MASK) { |
1519 | 0 | unsigned s; |
1520 | 0 | do { |
1521 | 0 | s = *ip++; |
1522 | 0 | if ((endOnInput) && (ip > iend-LASTLITERALS)) goto _output_error; |
1523 | 0 | length += s; |
1524 | 0 | } while (s==255); |
1525 | 0 | if ((safeDecode) && unlikely((uptrval)(op)+length<(uptrval)op)) goto _output_error; /* overflow detection */ |
1526 | 0 | } |
1527 | 0 | length += MINMATCH; |
1528 | 0 |
|
1529 | 0 | /* check external dictionary */ |
1530 | 0 | if ((dict==usingExtDict) && (match < lowPrefix)) { |
1531 | 0 | if (unlikely(op+length > oend-LASTLITERALS)) goto _output_error; /* doesn't respect parsing restriction */ |
1532 | 0 | |
1533 | 0 | if (length <= (size_t)(lowPrefix-match)) { |
1534 | 0 | /* match can be copied as a single segment from external dictionary */ |
1535 | 0 | memmove(op, dictEnd - (lowPrefix-match), length); |
1536 | 0 | op += length; |
1537 | 0 | } else { |
1538 | 0 | /* match encompass external dictionary and current block */ |
1539 | 0 | size_t const copySize = (size_t)(lowPrefix-match); |
1540 | 0 | size_t const restSize = length - copySize; |
1541 | 0 | memcpy(op, dictEnd - copySize, copySize); |
1542 | 0 | op += copySize; |
1543 | 0 | if (restSize > (size_t)(op-lowPrefix)) { /* overlap copy */ |
1544 | 0 | BYTE* const endOfMatch = op + restSize; |
1545 | 0 | const BYTE* copyFrom = lowPrefix; |
1546 | 0 | while (op < endOfMatch) *op++ = *copyFrom++; |
1547 | 0 | } else { |
1548 | 0 | memcpy(op, lowPrefix, restSize); |
1549 | 0 | op += restSize; |
1550 | 0 | } } |
1551 | 0 | continue; |
1552 | 0 | } |
1553 | 0 |
|
1554 | 0 | /* copy match within block */ |
1555 | 0 | cpy = op + length; |
1556 | 0 | if (unlikely(offset<8)) { |
1557 | 0 | op[0] = match[0]; |
1558 | 0 | op[1] = match[1]; |
1559 | 0 | op[2] = match[2]; |
1560 | 0 | op[3] = match[3]; |
1561 | 0 | match += inc32table[offset]; |
1562 | 0 | memcpy(op+4, match, 4); |
1563 | 0 | match -= dec64table[offset]; |
1564 | 0 | } else { memcpy(op, match, 8); match+=8; } |
1565 | 0 | op += 8; |
1566 | 0 |
|
1567 | 0 | if (unlikely(cpy>oend-12)) { |
1568 | 0 | BYTE* const oCopyLimit = oend-(WILDCOPYLENGTH-1); |
1569 | 0 | if (cpy > oend-LASTLITERALS) goto _output_error; /* Error : last LASTLITERALS bytes must be literals (uncompressed) */ |
1570 | 0 | if (op < oCopyLimit) { |
1571 | 0 | LZ4_wildCopy(op, match, oCopyLimit); |
1572 | 0 | match += oCopyLimit - op; |
1573 | 0 | op = oCopyLimit; |
1574 | 0 | } |
1575 | 0 | while (op<cpy) *op++ = *match++; |
1576 | 0 | } else { |
1577 | 0 | memcpy(op, match, 8); |
1578 | 0 | if (length>16) LZ4_wildCopy(op+8, match+8, cpy); |
1579 | 0 | } |
1580 | 0 | op = cpy; /* correction */ |
1581 | 0 | } |
1582 | 0 |
|
1583 | 0 | /* end of decoding */ |
1584 | 0 | if (endOnInput) |
1585 | 0 | return (int) (((char*)op)-dst); /* Nb of output bytes decoded */ |
1586 | 0 | else |
1587 | 0 | return (int) (((const char*)ip)-src); /* Nb of input bytes read */ |
1588 | 0 | |
1589 | 0 | /* Overflow error detected */ |
1590 | 0 | _output_error: |
1591 | 0 | return (int) (-(((const char*)ip)-src))-1; |
1592 | 0 | } |
1593 | | |
1594 | | |
1595 | | /*===== Instantiate the API decoding functions. =====*/ |
1596 | | |
1597 | | LZ4_FORCE_O2_GCC_PPC64LE |
1598 | | int LZ4_decompress_safe(const char* source, char* dest, int compressedSize, int maxDecompressedSize) |
1599 | 0 | { |
1600 | 0 | return LZ4_decompress_generic(source, dest, compressedSize, maxDecompressedSize, |
1601 | 0 | endOnInputSize, full, 0, noDict, |
1602 | 0 | (BYTE*)dest, NULL, 0); |
1603 | 0 | } |
1604 | | |
1605 | | LZ4_FORCE_O2_GCC_PPC64LE |
1606 | | int LZ4_decompress_safe_partial(const char* source, char* dest, int compressedSize, int targetOutputSize, int maxDecompressedSize) |
1607 | 0 | { |
1608 | 0 | return LZ4_decompress_generic(source, dest, compressedSize, maxDecompressedSize, |
1609 | 0 | endOnInputSize, partial, targetOutputSize, |
1610 | 0 | noDict, (BYTE*)dest, NULL, 0); |
1611 | 0 | } |
1612 | | |
1613 | | LZ4_FORCE_O2_GCC_PPC64LE |
1614 | | int LZ4_decompress_fast(const char* source, char* dest, int originalSize) |
1615 | 0 | { |
1616 | 0 | return LZ4_decompress_generic(source, dest, 0, originalSize, |
1617 | 0 | endOnOutputSize, full, 0, withPrefix64k, |
1618 | 0 | (BYTE*)dest - 64 KB, NULL, 0); |
1619 | 0 | } |
1620 | | |
1621 | | /*===== Instantiate a few more decoding cases, used more than once. =====*/ |
1622 | | |
1623 | | LZ4_FORCE_O2_GCC_PPC64LE /* Exported, an obsolete API function. */ |
1624 | | int LZ4_decompress_safe_withPrefix64k(const char* source, char* dest, int compressedSize, int maxOutputSize) |
1625 | 0 | { |
1626 | 0 | return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, |
1627 | 0 | endOnInputSize, full, 0, withPrefix64k, |
1628 | 0 | (BYTE*)dest - 64 KB, NULL, 0); |
1629 | 0 | } |
1630 | | |
1631 | | /* Another obsolete API function, paired with the previous one. */ |
1632 | | int LZ4_decompress_fast_withPrefix64k(const char* source, char* dest, int originalSize) |
1633 | 0 | { |
1634 | 0 | /* LZ4_decompress_fast doesn't validate match offsets, |
1635 | 0 | * and thus serves well with any prefixed dictionary. */ |
1636 | 0 | return LZ4_decompress_fast(source, dest, originalSize); |
1637 | 0 | } |
1638 | | |
1639 | | LZ4_FORCE_O2_GCC_PPC64LE |
1640 | | static int LZ4_decompress_safe_withSmallPrefix(const char* source, char* dest, int compressedSize, int maxOutputSize, |
1641 | | size_t prefixSize) |
1642 | 0 | { |
1643 | 0 | return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, |
1644 | 0 | endOnInputSize, full, 0, noDict, |
1645 | 0 | (BYTE*)dest-prefixSize, NULL, 0); |
1646 | 0 | } |
1647 | | |
1648 | | LZ4_FORCE_O2_GCC_PPC64LE /* Exported under another name, for tests/fullbench.c */ |
1649 | 0 | #define LZ4_decompress_safe_extDict LZ4_decompress_safe_forceExtDict |
1650 | | int LZ4_decompress_safe_extDict(const char* source, char* dest, int compressedSize, int maxOutputSize, |
1651 | | const void* dictStart, size_t dictSize) |
1652 | 0 | { |
1653 | 0 | return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, |
1654 | 0 | endOnInputSize, full, 0, usingExtDict, |
1655 | 0 | (BYTE*)dest, (const BYTE*)dictStart, dictSize); |
1656 | 0 | } |
1657 | | |
1658 | | LZ4_FORCE_O2_GCC_PPC64LE |
1659 | | static int LZ4_decompress_fast_extDict(const char* source, char* dest, int originalSize, |
1660 | | const void* dictStart, size_t dictSize) |
1661 | 0 | { |
1662 | 0 | return LZ4_decompress_generic(source, dest, 0, originalSize, |
1663 | 0 | endOnOutputSize, full, 0, usingExtDict, |
1664 | 0 | (BYTE*)dest, (const BYTE*)dictStart, dictSize); |
1665 | 0 | } |
1666 | | |
1667 | | /* The "double dictionary" mode, for use with e.g. ring buffers: the first part |
1668 | | * of the dictionary is passed as prefix, and the second via dictStart + dictSize. |
1669 | | * These routines are used only once, in LZ4_decompress_*_continue(). |
1670 | | */ |
1671 | | LZ4_FORCE_INLINE |
1672 | | int LZ4_decompress_safe_doubleDict(const char* source, char* dest, int compressedSize, int maxOutputSize, |
1673 | | size_t prefixSize, const void* dictStart, size_t dictSize) |
1674 | 0 | { |
1675 | 0 | return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, |
1676 | 0 | endOnInputSize, full, 0, usingExtDict, |
1677 | 0 | (BYTE*)dest-prefixSize, (const BYTE*)dictStart, dictSize); |
1678 | 0 | } |
1679 | | |
1680 | | LZ4_FORCE_INLINE |
1681 | | int LZ4_decompress_fast_doubleDict(const char* source, char* dest, int originalSize, |
1682 | | size_t prefixSize, const void* dictStart, size_t dictSize) |
1683 | 0 | { |
1684 | 0 | return LZ4_decompress_generic(source, dest, 0, originalSize, |
1685 | 0 | endOnOutputSize, full, 0, usingExtDict, |
1686 | 0 | (BYTE*)dest-prefixSize, (const BYTE*)dictStart, dictSize); |
1687 | 0 | } |
1688 | | |
1689 | | /*===== streaming decompression functions =====*/ |
1690 | | |
1691 | | LZ4_streamDecode_t* LZ4_createStreamDecode(void) |
1692 | 0 | { |
1693 | 0 | LZ4_streamDecode_t* lz4s = (LZ4_streamDecode_t*) ALLOC_AND_ZERO(sizeof(LZ4_streamDecode_t)); |
1694 | 0 | return lz4s; |
1695 | 0 | } |
1696 | | |
1697 | | int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream) |
1698 | 0 | { |
1699 | 0 | if (!LZ4_stream) return 0; /* support free on NULL */ |
1700 | 0 | FREEMEM(LZ4_stream); |
1701 | 0 | return 0; |
1702 | 0 | } |
1703 | | |
1704 | | /*! LZ4_setStreamDecode() : |
1705 | | * Use this function to instruct where to find the dictionary. |
1706 | | * This function is not necessary if previous data is still available where it was decoded. |
1707 | | * Loading a size of 0 is allowed (same effect as no dictionary). |
1708 | | * @return : 1 if OK, 0 if error |
1709 | | */ |
1710 | | int LZ4_setStreamDecode (LZ4_streamDecode_t* LZ4_streamDecode, const char* dictionary, int dictSize) |
1711 | 0 | { |
1712 | 0 | LZ4_streamDecode_t_internal* lz4sd = &LZ4_streamDecode->internal_donotuse; |
1713 | 0 | lz4sd->prefixSize = (size_t) dictSize; |
1714 | 0 | lz4sd->prefixEnd = (const BYTE*) dictionary + dictSize; |
1715 | 0 | lz4sd->externalDict = NULL; |
1716 | 0 | lz4sd->extDictSize = 0; |
1717 | 0 | return 1; |
1718 | 0 | } |
1719 | | |
1720 | | /*! LZ4_decoderRingBufferSize() : |
1721 | | * when setting a ring buffer for streaming decompression (optional scenario), |
1722 | | * provides the minimum size of this ring buffer |
1723 | | * to be compatible with any source respecting maxBlockSize condition. |
1724 | | * Note : in a ring buffer scenario, |
1725 | | * blocks are presumed decompressed next to each other. |
1726 | | * When not enough space remains for next block (remainingSize < maxBlockSize), |
1727 | | * decoding resumes from beginning of ring buffer. |
1728 | | * @return : minimum ring buffer size, |
1729 | | * or 0 if there is an error (invalid maxBlockSize). |
1730 | | */ |
1731 | | int LZ4_decoderRingBufferSize(int maxBlockSize) |
1732 | 0 | { |
1733 | 0 | if (maxBlockSize < 0) return 0; |
1734 | 0 | if (maxBlockSize > LZ4_MAX_INPUT_SIZE) return 0; |
1735 | 0 | if (maxBlockSize < 16) maxBlockSize = 16; |
1736 | 0 | return LZ4_DECODER_RING_BUFFER_SIZE(maxBlockSize); |
1737 | 0 | } |
1738 | | |
1739 | | /* |
1740 | | *_continue() : |
1741 | | These decoding functions allow decompression of multiple blocks in "streaming" mode. |
1742 | | Previously decoded blocks must still be available at the memory position where they were decoded. |
1743 | | If it's not possible, save the relevant part of decoded data into a safe buffer, |
1744 | | and indicate where it stands using LZ4_setStreamDecode() |
1745 | | */ |
1746 | | LZ4_FORCE_O2_GCC_PPC64LE |
1747 | | int LZ4_decompress_safe_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* source, char* dest, int compressedSize, int maxOutputSize) |
1748 | 0 | { |
1749 | 0 | LZ4_streamDecode_t_internal* lz4sd = &LZ4_streamDecode->internal_donotuse; |
1750 | 0 | int result; |
1751 | 0 |
|
1752 | 0 | if (lz4sd->prefixSize == 0) { |
1753 | 0 | /* The first call, no dictionary yet. */ |
1754 | 0 | assert(lz4sd->extDictSize == 0); |
1755 | 0 | result = LZ4_decompress_safe(source, dest, compressedSize, maxOutputSize); |
1756 | 0 | if (result <= 0) return result; |
1757 | 0 | lz4sd->prefixSize = result; |
1758 | 0 | lz4sd->prefixEnd = (BYTE*)dest + result; |
1759 | 0 | } else if (lz4sd->prefixEnd == (BYTE*)dest) { |
1760 | 0 | /* They're rolling the current segment. */ |
1761 | 0 | if (lz4sd->prefixSize >= 64 KB - 1) |
1762 | 0 | result = LZ4_decompress_safe_withPrefix64k(source, dest, compressedSize, maxOutputSize); |
1763 | 0 | else if (lz4sd->extDictSize == 0) |
1764 | 0 | result = LZ4_decompress_safe_withSmallPrefix(source, dest, compressedSize, maxOutputSize, |
1765 | 0 | lz4sd->prefixSize); |
1766 | 0 | else |
1767 | 0 | result = LZ4_decompress_safe_doubleDict(source, dest, compressedSize, maxOutputSize, |
1768 | 0 | lz4sd->prefixSize, lz4sd->externalDict, lz4sd->extDictSize); |
1769 | 0 | if (result <= 0) return result; |
1770 | 0 | lz4sd->prefixSize += result; |
1771 | 0 | lz4sd->prefixEnd += result; |
1772 | 0 | } else { |
1773 | 0 | /* The buffer wraps around, or they're switching to another buffer. */ |
1774 | 0 | lz4sd->extDictSize = lz4sd->prefixSize; |
1775 | 0 | lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize; |
1776 | 0 | result = LZ4_decompress_safe_extDict(source, dest, compressedSize, maxOutputSize, |
1777 | 0 | lz4sd->externalDict, lz4sd->extDictSize); |
1778 | 0 | if (result <= 0) return result; |
1779 | 0 | lz4sd->prefixSize = result; |
1780 | 0 | lz4sd->prefixEnd = (BYTE*)dest + result; |
1781 | 0 | } |
1782 | 0 |
|
1783 | 0 | return result; |
1784 | 0 | } |
1785 | | |
1786 | | LZ4_FORCE_O2_GCC_PPC64LE |
1787 | | int LZ4_decompress_fast_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* source, char* dest, int originalSize) |
1788 | 0 | { |
1789 | 0 | LZ4_streamDecode_t_internal* lz4sd = &LZ4_streamDecode->internal_donotuse; |
1790 | 0 | int result; |
1791 | 0 |
|
1792 | 0 | if (lz4sd->prefixSize == 0) { |
1793 | 0 | assert(lz4sd->extDictSize == 0); |
1794 | 0 | result = LZ4_decompress_fast(source, dest, originalSize); |
1795 | 0 | if (result <= 0) return result; |
1796 | 0 | lz4sd->prefixSize = originalSize; |
1797 | 0 | lz4sd->prefixEnd = (BYTE*)dest + originalSize; |
1798 | 0 | } else if (lz4sd->prefixEnd == (BYTE*)dest) { |
1799 | 0 | if (lz4sd->prefixSize >= 64 KB - 1 || lz4sd->extDictSize == 0) |
1800 | 0 | result = LZ4_decompress_fast(source, dest, originalSize); |
1801 | 0 | else |
1802 | 0 | result = LZ4_decompress_fast_doubleDict(source, dest, originalSize, |
1803 | 0 | lz4sd->prefixSize, lz4sd->externalDict, lz4sd->extDictSize); |
1804 | 0 | if (result <= 0) return result; |
1805 | 0 | lz4sd->prefixSize += originalSize; |
1806 | 0 | lz4sd->prefixEnd += originalSize; |
1807 | 0 | } else { |
1808 | 0 | lz4sd->extDictSize = lz4sd->prefixSize; |
1809 | 0 | lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize; |
1810 | 0 | result = LZ4_decompress_fast_extDict(source, dest, originalSize, |
1811 | 0 | lz4sd->externalDict, lz4sd->extDictSize); |
1812 | 0 | if (result <= 0) return result; |
1813 | 0 | lz4sd->prefixSize = originalSize; |
1814 | 0 | lz4sd->prefixEnd = (BYTE*)dest + originalSize; |
1815 | 0 | } |
1816 | 0 |
|
1817 | 0 | return result; |
1818 | 0 | } |
1819 | | |
1820 | | |
1821 | | /* |
1822 | | Advanced decoding functions : |
1823 | | *_usingDict() : |
1824 | | These decoding functions work the same as "_continue" ones, |
1825 | | the dictionary must be explicitly provided within parameters |
1826 | | */ |
1827 | | |
1828 | | int LZ4_decompress_safe_usingDict(const char* source, char* dest, int compressedSize, int maxOutputSize, const char* dictStart, int dictSize) |
1829 | 0 | { |
1830 | 0 | if (dictSize==0) |
1831 | 0 | return LZ4_decompress_safe(source, dest, compressedSize, maxOutputSize); |
1832 | 0 | if (dictStart+dictSize == dest) { |
1833 | 0 | if (dictSize >= 64 KB - 1) |
1834 | 0 | return LZ4_decompress_safe_withPrefix64k(source, dest, compressedSize, maxOutputSize); |
1835 | 0 | return LZ4_decompress_safe_withSmallPrefix(source, dest, compressedSize, maxOutputSize, dictSize); |
1836 | 0 | } |
1837 | 0 | return LZ4_decompress_safe_extDict(source, dest, compressedSize, maxOutputSize, dictStart, dictSize); |
1838 | 0 | } |
1839 | | |
1840 | | int LZ4_decompress_fast_usingDict(const char* source, char* dest, int originalSize, const char* dictStart, int dictSize) |
1841 | 0 | { |
1842 | 0 | if (dictSize==0 || dictStart+dictSize == dest) |
1843 | 0 | return LZ4_decompress_fast(source, dest, originalSize); |
1844 | 0 | return LZ4_decompress_fast_extDict(source, dest, originalSize, dictStart, dictSize); |
1845 | 0 | } |
1846 | | |
1847 | | |
1848 | | /*=************************************************* |
1849 | | * Obsolete Functions |
1850 | | ***************************************************/ |
1851 | | /* obsolete compression functions */ |
1852 | | int LZ4_compress_limitedOutput(const char* source, char* dest, int inputSize, int maxOutputSize) |
1853 | 0 | { |
1854 | 0 | return LZ4_compress_default(source, dest, inputSize, maxOutputSize); |
1855 | 0 | } |
1856 | | int LZ4_compress(const char* source, char* dest, int inputSize) |
1857 | 0 | { |
1858 | 0 | return LZ4_compress_default(source, dest, inputSize, LZ4_compressBound(inputSize)); |
1859 | 0 | } |
1860 | | int LZ4_compress_limitedOutput_withState (void* state, const char* src, char* dst, int srcSize, int dstSize) |
1861 | 0 | { |
1862 | 0 | return LZ4_compress_fast_extState(state, src, dst, srcSize, dstSize, 1); |
1863 | 0 | } |
1864 | | int LZ4_compress_withState (void* state, const char* src, char* dst, int srcSize) |
1865 | 0 | { |
1866 | 0 | return LZ4_compress_fast_extState(state, src, dst, srcSize, LZ4_compressBound(srcSize), 1); |
1867 | 0 | } |
1868 | | int LZ4_compress_limitedOutput_continue (LZ4_stream_t* LZ4_stream, const char* src, char* dst, int srcSize, int dstCapacity) |
1869 | 0 | { |
1870 | 0 | return LZ4_compress_fast_continue(LZ4_stream, src, dst, srcSize, dstCapacity, 1); |
1871 | 0 | } |
1872 | | int LZ4_compress_continue (LZ4_stream_t* LZ4_stream, const char* source, char* dest, int inputSize) |
1873 | 0 | { |
1874 | 0 | return LZ4_compress_fast_continue(LZ4_stream, source, dest, inputSize, LZ4_compressBound(inputSize), 1); |
1875 | 0 | } |
1876 | | |
1877 | | /* |
1878 | | These decompression functions are deprecated and should no longer be used. |
1879 | | They are only provided here for compatibility with older user programs. |
1880 | | - LZ4_uncompress is totally equivalent to LZ4_decompress_fast |
1881 | | - LZ4_uncompress_unknownOutputSize is totally equivalent to LZ4_decompress_safe |
1882 | | */ |
1883 | | int LZ4_uncompress (const char* source, char* dest, int outputSize) |
1884 | 0 | { |
1885 | 0 | return LZ4_decompress_fast(source, dest, outputSize); |
1886 | 0 | } |
1887 | | int LZ4_uncompress_unknownOutputSize (const char* source, char* dest, int isize, int maxOutputSize) |
1888 | 0 | { |
1889 | 0 | return LZ4_decompress_safe(source, dest, isize, maxOutputSize); |
1890 | 0 | } |
1891 | | |
1892 | | /* Obsolete Streaming functions */ |
1893 | | |
1894 | 0 | int LZ4_sizeofStreamState() { return LZ4_STREAMSIZE; } |
1895 | | |
1896 | | int LZ4_resetStreamState(void* state, char* inputBuffer) |
1897 | 0 | { |
1898 | 0 | (void)inputBuffer; |
1899 | 0 | LZ4_resetStream((LZ4_stream_t*)state); |
1900 | 0 | return 0; |
1901 | 0 | } |
1902 | | |
1903 | | void* LZ4_create (char* inputBuffer) |
1904 | 0 | { |
1905 | 0 | (void)inputBuffer; |
1906 | 0 | return LZ4_createStream(); |
1907 | 0 | } |
1908 | | |
1909 | | char* LZ4_slideInputBuffer (void* state) |
1910 | 0 | { |
1911 | 0 | /* avoid const char * -> char * conversion warning */ |
1912 | 0 | return (char *)(uptrval)((LZ4_stream_t*)state)->internal_donotuse.dictionary; |
1913 | 0 | } |
1914 | | |
1915 | | #endif /* LZ4_COMMONDEFS_ONLY */ |