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