45 #define LZ4_HEAPMODE 0
52 #define ACCELERATION_DEFAULT 1
76 #ifndef LZ4_FORCE_MEMORY_ACCESS
77 #if defined(__GNUC__) && \
78 (defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || \
79 defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6Z__) || \
80 defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_6T2__))
81 #define LZ4_FORCE_MEMORY_ACCESS 2
82 #elif (defined(__INTEL_COMPILER) && !defined(_WIN32)) || defined(__GNUC__)
83 #define LZ4_FORCE_MEMORY_ACCESS 1
92 #if defined(_MSC_VER) && \
95 #define LZ4_FORCE_SW_BITCOUNT
101 #define LZ4_STATIC_LINKING_ONLY
102 #define LZ4_DISABLE_DEPRECATE_WARNINGS
114 #pragma warning(disable : 4293)
118 #ifndef LZ4_FORCE_INLINE
120 #define LZ4_FORCE_INLINE static __forceinline
122 #if defined(__cplusplus) || \
123 defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
125 #define LZ4_FORCE_INLINE static inline __attribute__((always_inline))
127 #define LZ4_FORCE_INLINE static inline
130 #define LZ4_FORCE_INLINE static
149 #if defined(__PPC64__) && defined(__LITTLE_ENDIAN__) && defined(__GNUC__)
150 #define LZ4_FORCE_O2_GCC_PPC64LE __attribute__((optimize("O2")))
151 #define LZ4_FORCE_O2_INLINE_GCC_PPC64LE \
152 __attribute__((optimize("O2"))) LZ4_FORCE_INLINE
154 #define LZ4_FORCE_O2_GCC_PPC64LE
155 #define LZ4_FORCE_O2_INLINE_GCC_PPC64LE static
158 #if (defined(__GNUC__) && (__GNUC__ >= 3)) || \
159 (defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 800)) || \
161 #define expect(expr, value) (__builtin_expect((expr), (value)))
163 #define expect(expr, value) (expr)
167 #define likely(expr) expect((expr) != 0, 1)
170 #define unlikely(expr) expect((expr) != 0, 0)
177 #define ALLOC(s) malloc(s)
178 #define ALLOC_AND_ZERO(s) calloc(1, s)
179 #define FREEMEM(p) free(p)
181 #define MEM_INIT(p, v, s) memset((p), (v), (s))
186 #if defined(__cplusplus) || \
187 (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) )
189 typedef uint8_t
BYTE;
190 typedef uint16_t
U16;
191 typedef uint32_t
U32;
193 typedef uint64_t
U64;
197 typedef unsigned short U16;
200 typedef unsigned long long U64;
204 #if defined(__x86_64__)
207 typedef size_t reg_t;
213 static unsigned LZ4_isLittleEndian(
void)
222 #if defined(LZ4_FORCE_MEMORY_ACCESS) && (LZ4_FORCE_MEMORY_ACCESS == 2)
225 static U16 LZ4_read16(
const void *memPtr)
227 return *(
const U16 *)memPtr;
230 static U32 LZ4_read32(
const void *memPtr)
232 return *(
const U32 *)memPtr;
235 static reg_t LZ4_read_ARCH(
const void *memPtr)
237 return *(
const reg_t *)memPtr;
240 static void LZ4_write16(
void *memPtr,
U16 value)
242 *(
U16 *)memPtr = value;
245 static void LZ4_write32(
void *memPtr,
U32 value)
247 *(
U32 *)memPtr = value;
250 #elif defined(LZ4_FORCE_MEMORY_ACCESS) && (LZ4_FORCE_MEMORY_ACCESS == 1)
261 static U16 LZ4_read16(
const void *ptr)
263 return ((
const unalign *)ptr)->u16;
266 static U32 LZ4_read32(
const void *ptr)
268 return ((
const unalign *)ptr)->u32;
271 static reg_t LZ4_read_ARCH(
const void *ptr)
273 return ((
const unalign *)ptr)->uArch;
276 static void LZ4_write16(
void *memPtr,
U16 value)
278 ((unalign *)memPtr)->u16 = value;
281 static void LZ4_write32(
void *memPtr,
U32 value)
283 ((unalign *)memPtr)->u32 = value;
288 static U16 LZ4_read16(
const void *memPtr)
292 memcpy(&val, memPtr,
sizeof(val));
296 static U32 LZ4_read32(
const void *memPtr)
300 memcpy(&val, memPtr,
sizeof(val));
304 static reg_t LZ4_read_ARCH(
const void *memPtr)
308 memcpy(&val, memPtr,
sizeof(val));
312 static void LZ4_write16(
void *memPtr,
U16 value)
314 memcpy(memPtr, &value,
sizeof(value));
317 static void LZ4_write32(
void *memPtr,
U32 value)
319 memcpy(memPtr, &value,
sizeof(value));
324 static U16 LZ4_readLE16(
const void *memPtr)
326 if (LZ4_isLittleEndian()) {
327 return LZ4_read16(memPtr);
330 const BYTE *p = (
const BYTE *)memPtr;
332 return (
U16)((
U16)p[0] + (p[1] << 8));
336 static void LZ4_writeLE16(
void *memPtr,
U16 value)
338 if (LZ4_isLittleEndian()) {
339 LZ4_write16(memPtr, value);
345 p[1] = (
BYTE)(value >> 8);
352 void LZ4_wildCopy(
void *dstPtr,
const void *srcPtr,
void *dstEnd)
355 const BYTE *s = (
const BYTE *)srcPtr;
370 #define WILDCOPYLENGTH 8
371 #define LASTLITERALS 5
372 #define MFLIMIT (WILDCOPYLENGTH + MINMATCH)
373 static const int LZ4_minLength = (
MFLIMIT + 1);
375 #define KB *(1 << 10)
376 #define MB *(1 << 20)
377 #define GB *(1U << 30)
380 #define MAX_DISTANCE ((1 << MAXD_LOG) - 1)
383 #define ML_MASK ((1U << ML_BITS) - 1)
384 #define RUN_BITS (8 - ML_BITS)
385 #define RUN_MASK ((1U << RUN_BITS) - 1)
390 #if defined(LZ4_DEBUG) && (LZ4_DEBUG >= 1)
394 #define assert(condition) ((void)0)
398 #define LZ4_STATIC_ASSERT(c) \
400 enum { LZ4_static_assert = 1 / (int)(!!(c)) }; \
403 #if defined(LZ4_DEBUG) && (LZ4_DEBUG >= 2)
405 static int g_debuglog_enable = 1;
407 #define DEBUGLOG(l, ...) \
409 if ((g_debuglog_enable) && (l <= LZ4_DEBUG)) { \
410 fprintf(stderr, __FILE__ ": "); \
411 fprintf(stderr, __VA_ARGS__); \
412 fprintf(stderr, " \n"); \
416 #define DEBUGLOG(l, ...) \
424 static unsigned LZ4_NbCommonBytes(
reg_t val)
426 if (LZ4_isLittleEndian()) {
427 if (
sizeof(val) == 8) {
428 #if defined(_MSC_VER) && defined(_WIN64) && !defined(LZ4_FORCE_SW_BITCOUNT)
431 _BitScanForward64(&
r, (
U64)val);
432 return (
int)(
r >> 3);
433 #elif (defined(__clang__) || (defined(__GNUC__) && (__GNUC__ >= 3))) && \
434 !defined(LZ4_FORCE_SW_BITCOUNT)
435 return (__builtin_ctzll((
U64)val) >> 3);
437 static const int DeBruijnBytePos[64] = {
438 0, 0, 0, 0, 0, 1, 1, 2, 0, 3, 1, 3, 1, 4, 2, 7,
439 0, 2, 3, 6, 1, 5, 3, 5, 1, 3, 4, 4, 2, 5, 6, 7,
440 7, 0, 1, 2, 3, 3, 4, 6, 2, 6, 5, 5, 3, 4, 5, 6,
441 7, 1, 2, 4, 6, 4, 4, 5, 7, 2, 6, 5, 7, 6, 7, 7};
442 return DeBruijnBytePos[((
U64)((val & -(
long long)val) *
443 0x0218A392CDABBD3FULL)) >>
448 #if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
451 _BitScanForward(&
r, (
U32)val);
452 return (
int)(
r >> 3);
453 #elif (defined(__clang__) || (defined(__GNUC__) && (__GNUC__ >= 3))) && \
454 !defined(LZ4_FORCE_SW_BITCOUNT)
455 return (__builtin_ctz((
U32)val) >> 3);
457 static const int DeBruijnBytePos[32] = {
458 0, 0, 3, 0, 3, 1, 3, 0, 3, 2, 2, 1, 3, 2, 0, 1,
459 3, 3, 1, 2, 2, 2, 2, 0, 3, 1, 2, 0, 1, 0, 1, 1};
460 return DeBruijnBytePos[((
U32)((val & -(
S32)val) * 0x077CB531U)) >>
466 if (
sizeof(val) == 8) {
467 #if defined(_MSC_VER) && defined(_WIN64) && !defined(LZ4_FORCE_SW_BITCOUNT)
470 _BitScanReverse64(&
r, val);
471 return (
unsigned)(
r >> 3);
472 #elif (defined(__clang__) || (defined(__GNUC__) && (__GNUC__ >= 3))) && \
473 !defined(LZ4_FORCE_SW_BITCOUNT)
474 return (__builtin_clzll((
U64)val) >> 3);
476 static const U32 by32 =
484 if (!(val >> by32)) {
503 #if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
506 _BitScanReverse(&
r, (
unsigned long)val);
507 return (
unsigned)(
r >> 3);
508 #elif (defined(__clang__) || (defined(__GNUC__) && (__GNUC__ >= 3))) && \
509 !defined(LZ4_FORCE_SW_BITCOUNT)
510 return (__builtin_clz((
U32)val) >> 3);
529 #define STEPSIZE sizeof(reg_t)
533 const BYTE *
const pStart = pIn;
536 reg_t const diff = LZ4_read_ARCH(pMatch) ^ LZ4_read_ARCH(pIn);
543 return LZ4_NbCommonBytes(diff);
548 reg_t const diff = LZ4_read_ARCH(pMatch) ^ LZ4_read_ARCH(pIn);
555 pIn += LZ4_NbCommonBytes(diff);
556 return (
unsigned)(pIn - pStart);
559 if ((
STEPSIZE == 8) && (pIn < (pInLimit - 3)) &&
560 (LZ4_read32(pMatch) == LZ4_read32(pIn))) {
564 if ((pIn < (pInLimit - 1)) && (LZ4_read16(pMatch) == LZ4_read16(pIn))) {
568 if ((pIn < pInLimit) && (*pMatch == *pIn))
570 return (
unsigned)(pIn - pStart);
573 #ifndef LZ4_COMMONDEFS_ONLY
578 static const int LZ4_64Klimit = ((64
KB) + (
MFLIMIT - 1));
579 static const U32 LZ4_skipTrigger = 6;
654 if (tableType ==
byU16)
655 return ((sequence * 2654435761U) >>
663 static const U64 prime5bytes = 889523592379ULL;
664 static const U64 prime8bytes = 11400714785074694791ULL;
667 if (LZ4_isLittleEndian())
668 return (
U32)(((sequence << 24) * prime5bytes) >> (64 - hashLog));
670 return (
U32)(((sequence >> 24) * prime8bytes) >> (64 - hashLog));
676 if ((
sizeof(
reg_t) == 8) && (tableType !=
byU16))
677 return LZ4_hash5(LZ4_read_ARCH(p), tableType);
678 return LZ4_hash4(LZ4_read32(p), tableType);
681 static void LZ4_putIndexOnHash(
U32 idx,
U32 h,
void *tableBase,
692 U32 *hashTable = (
U32 *)tableBase;
698 U16 *hashTable = (
U16 *)tableBase;
701 hashTable[h] = (
U16)idx;
707 static void LZ4_putPositionOnHash(
const BYTE *p,
U32 h,
void *tableBase,
717 const BYTE **hashTable = (
const BYTE **)tableBase;
723 U32 *hashTable = (
U32 *)tableBase;
725 hashTable[h] = (
U32)(p - srcBase);
729 U16 *hashTable = (
U16 *)tableBase;
731 hashTable[h] = (
U16)(p - srcBase);
743 LZ4_putPositionOnHash(p, h, tableBase, tableType, srcBase);
752 static U32 LZ4_getIndexOnHash(
U32 h,
const void *tableBase,
756 if (tableType ==
byU32) {
757 const U32 *
const hashTable = (
const U32 *)tableBase;
762 if (tableType ==
byU16) {
763 const U16 *
const hashTable = (
const U16 *)tableBase;
772 static const BYTE *LZ4_getPositionOnHash(
U32 h,
const void *tableBase,
776 if (tableType ==
byPtr) {
777 const BYTE *
const *hashTable = (
const BYTE *
const *)tableBase;
781 if (tableType ==
byU32) {
782 const U32 *
const hashTable = (
const U32 *)tableBase;
784 return hashTable[h] + srcBase;
787 const U16 *
const hashTable = (
const U16 *)tableBase;
789 return hashTable[h] + srcBase;
794 const void *tableBase,
800 return LZ4_getPositionOnHash(h, tableBase, tableType, srcBase);
813 (tableType ==
byU16 &&
816 tableType ==
byPtr || inputSize >= 4
KB) {
817 DEBUGLOG(4,
"LZ4_prepareTable: Resetting table in %p", cctx);
823 DEBUGLOG(4,
"LZ4_prepareTable: Re-use hash table (no reset)");
832 DEBUGLOG(5,
"LZ4_prepareTable: adding 64KB to currentOffset");
846 char *
const dest,
const int inputSize,
852 const BYTE *ip = (
const BYTE *)source;
855 const BYTE *base = (
const BYTE *)source - startIndex;
856 const BYTE *lowLimit;
860 const BYTE *
const dictionary =
869 int const maybe_extMem =
871 U32 const prefixIdxLimit =
872 startIndex - dictSize;
873 const BYTE *
const dictEnd = dictionary + dictSize;
874 const BYTE *anchor = (
const BYTE *)source;
875 const BYTE *
const iend = ip + inputSize;
876 const BYTE *
const mflimitPlusOne = iend -
MFLIMIT + 1;
883 : dictionary + dictSize - startIndex;
886 BYTE *
const olimit = op + maxOutputSize;
891 DEBUGLOG(5,
"LZ4_compress_generic: srcSize=%i, tableType=%u", inputSize,
894 if (outputLimited ==
fillOutput && maxOutputSize < 1)
898 if ((tableType ==
byU16) && (inputSize >= LZ4_64Klimit))
900 if (tableType ==
byPtr)
903 assert(acceleration >= 1);
921 if (inputSize < LZ4_minLength)
936 if (tableType ==
byPtr) {
937 const BYTE *forwardIp = ip;
939 unsigned searchMatchNb = acceleration << LZ4_skipTrigger;
942 U32 const h = forwardH;
946 step = (searchMatchNb++ >> LZ4_skipTrigger);
948 if (
unlikely(forwardIp > mflimitPlusOne))
950 assert(ip < mflimitPlusOne);
953 LZ4_getPositionOnHash(h, cctx->
hashTable, tableType, base);
955 LZ4_putPositionOnHash(ip, h, cctx->
hashTable, tableType, base);
958 (LZ4_read32(match) != LZ4_read32(ip)));
962 const BYTE *forwardIp = ip;
964 unsigned searchMatchNb = acceleration << LZ4_skipTrigger;
967 U32 const h = forwardH;
968 U32 const current = (
U32)(forwardIp - base);
970 LZ4_getIndexOnHash(h, cctx->
hashTable, tableType);
971 assert(matchIndex <= current);
972 assert(forwardIp - base < (ptrdiff_t)(2
GB - 1));
975 step = (searchMatchNb++ >> LZ4_skipTrigger);
977 if (
unlikely(forwardIp > mflimitPlusOne))
979 assert(ip < mflimitPlusOne);
982 if (matchIndex < startIndex) {
987 match = dictBase + matchIndex;
991 lowLimit = dictionary;
994 match = base + matchIndex;
995 lowLimit = (
const BYTE *)source;
999 if (matchIndex < startIndex) {
1001 "extDict candidate: matchIndex=%5u < "
1003 matchIndex, startIndex);
1005 match = dictBase + matchIndex;
1006 lowLimit = dictionary;
1009 match = base + matchIndex;
1010 lowLimit = (
const BYTE *)source;
1014 match = base + matchIndex;
1017 LZ4_putIndexOnHash(current, h, cctx->
hashTable, tableType);
1019 if ((dictIssue ==
dictSmall) && (matchIndex < prefixIdxLimit))
1021 assert(matchIndex < current);
1022 if ((tableType !=
byU16) &&
1025 if (tableType ==
byU16)
1026 assert((current - matchIndex) <=
1030 if (LZ4_read32(match) == LZ4_read32(ip)) {
1032 offset = current - matchIndex;
1040 while (((ip > anchor) & (match > lowLimit)) &&
1048 unsigned const litLength = (unsigned)(ip - anchor);
1051 if ((outputLimited ==
1059 op + (litLength + 240) / 255 +
1065 goto _last_literals;
1068 int len = (int)litLength -
RUN_MASK;
1071 for (; len >= 255; len -= 255)
1081 DEBUGLOG(6,
"seq.start:%i, literals=%u, match.start:%i",
1082 (
int)(anchor - (
const BYTE *)source), litLength,
1083 (
int)(ip - (
const BYTE *)source));
1105 goto _last_literals;
1110 DEBUGLOG(6,
" with offset=%u (ext if > %i)", offset,
1111 (
int)(ip - (
const BYTE *)source));
1112 assert(offset <= MAX_DISTANCE && offset > 0);
1113 LZ4_writeLE16(op, (
U16)offset);
1117 DEBUGLOG(6,
" with offset=%u (same segment)",
1120 LZ4_writeLE16(op, (
U16)(ip - match));
1130 (lowLimit == dictionary) ) {
1131 const BYTE *limit = ip + (dictEnd - match);
1134 if (limit > matchlimit)
1139 unsigned const more =
1145 " with matchLength=%u starting in extDict",
1152 DEBUGLOG(6,
" with matchLength=%u",
1156 if ((outputLimited) &&
1167 ip -= matchCode - newMatchCode;
1168 matchCode = newMatchCode;
1174 LZ4_write32(op, 0xFFFFFFFF);
1175 while (matchCode >= 4 * 255) {
1177 LZ4_write32(op, 0xFFFFFFFF);
1178 matchCode -= 4 * 255;
1180 op += matchCode / 255;
1181 *op++ = (
BYTE)(matchCode % 255);
1184 *token += (
BYTE)(matchCode);
1190 if (ip >= mflimitPlusOne)
1197 if (tableType ==
byPtr) {
1202 (LZ4_read32(match) == LZ4_read32(ip))) {
1211 U32 const current = (
U32)(ip - base);
1212 U32 matchIndex = LZ4_getIndexOnHash(h, cctx->
hashTable, tableType);
1213 assert(matchIndex < current);
1215 if (matchIndex < startIndex) {
1219 match = dictBase + matchIndex;
1222 matchIndex += dictDelta;
1225 match = base + matchIndex;
1226 lowLimit = (
const BYTE *)
1231 if (matchIndex < startIndex) {
1232 match = dictBase + matchIndex;
1237 match = base + matchIndex;
1238 lowLimit = (
const BYTE *)
1243 match = base + matchIndex;
1245 LZ4_putIndexOnHash(current, h, cctx->
hashTable, tableType);
1246 assert(matchIndex < current);
1247 if (((dictIssue ==
dictSmall) ? (matchIndex >= prefixIdxLimit)
1249 ((tableType ==
byU16)
1252 (LZ4_read32(match) == LZ4_read32(ip))) {
1256 offset = current - matchIndex;
1257 DEBUGLOG(6,
"seq.start:%i, literals=%u, match.start:%i",
1258 (
int)(anchor - (
const BYTE *)source), 0,
1259 (
int)(ip - (
const BYTE *)source));
1271 size_t lastRun = (size_t)(iend - anchor);
1273 if ((outputLimited) &&
1274 (op + lastRun + 1 + ((lastRun + 255 -
RUN_MASK) / 255) > olimit)) {
1277 lastRun = (olimit - op) - 1;
1278 lastRun -= (lastRun + 240) / 255;
1284 size_t accumulator = lastRun -
RUN_MASK;
1287 for (; accumulator >= 255; accumulator -= 255)
1289 *op++ = (
BYTE)accumulator;
1294 memcpy(op, anchor, lastRun);
1295 ip = anchor + lastRun;
1300 *inputConsumed = (int)(((
const char *)ip) - source);
1302 DEBUGLOG(5,
"LZ4_compress_generic: compressed %i bytes into %i bytes",
1303 inputSize, (
int)(((
char *)op) - dest));
1304 return (
int)(((
char *)op) - dest);
1308 int inputSize,
int maxOutputSize,
1313 if (acceleration < 1)
1317 if (inputSize < LZ4_64Klimit) {
1333 if (inputSize < LZ4_64Klimit) {
1361 char *dst,
int srcSize,
1362 int dstCapacity,
int acceleration)
1366 if (acceleration < 1)
1370 if (srcSize < LZ4_64Klimit) {
1397 if (srcSize < LZ4_64Klimit) {
1426 int maxOutputSize,
int acceleration)
1441 maxOutputSize, acceleration);
1459 int maxOutputSize,
int acceleration)
1465 if (inputSize < LZ4_64Klimit)
1467 inputSize,
NULL, maxOutputSize,
1481 char *dst,
int *srcSizePtr,
1486 if (targetDstSize >=
1493 if (*srcSizePtr < LZ4_64Klimit) {
1495 &
state->internal_donotuse, src, dst, *srcSizePtr, srcSizePtr,
1504 &
state->internal_donotuse, src, dst, *srcSizePtr, srcSizePtr,
1524 int result = LZ4_compress_destSize_extState(ctx, src, dst, srcSizePtr,
1545 DEBUGLOG(4,
"LZ4_createStream %p", lz4s);
1554 DEBUGLOG(5,
"LZ4_resetStream (ctx:%p)", LZ4_stream);
1567 DEBUGLOG(5,
"LZ4_freeStream %p", LZ4_stream);
1572 #define HASH_UNIT sizeof(reg_t)
1577 const BYTE *p = (
const BYTE *)dictionary;
1578 const BYTE *
const dictEnd = p + dictSize;
1581 DEBUGLOG(4,
"LZ4_loadDict (%i bytes from %p into %p)", dictSize, dictionary,
1597 if ((dictEnd - p) > 64
KB)
1598 p = dictEnd - 64
KB;
1620 if (dictionary_stream !=
NULL) {
1661 char *dest,
int inputSize,
int maxOutputSize,
1668 DEBUGLOG(5,
"LZ4_compress_fast_continue (inputSize=%i)", inputSize);
1672 LZ4_renormDictT(streamPtr, inputSize);
1673 if (acceleration < 1)
1678 && (dictEnd != (
const BYTE *)source)) {
1681 "LZ4_compress_fast_continue: dictSize(%u) at addr:%p is too small",
1685 dictEnd = (
const BYTE *)source;
1690 const BYTE *sourceEnd = (
const BYTE *)source + inputSize;
1692 if ((sourceEnd > streamPtr->
dictionary) && (sourceEnd < dictEnd)) {
1703 if (dictEnd == (
const BYTE *)source) {
1728 if (inputSize > 4
KB) {
1735 streamPtr, source, dest, inputSize,
NULL, maxOutputSize,
1741 streamPtr, source, dest, inputSize,
NULL, maxOutputSize,
1750 streamPtr, source, dest, inputSize,
NULL, maxOutputSize,
1756 streamPtr, source, dest, inputSize,
NULL, maxOutputSize,
1769 char *dest,
int srcSize)
1774 LZ4_renormDictT(streamPtr, srcSize);
1806 if ((
U32)dictSize > 64
KB)
1811 memmove(safeBuffer, previousDictEnd - dictSize, dictSize);
1829 const char *
const src,
char *
const dst,
int srcSize,
1833 int partialDecoding,
1834 int targetOutputSize,
1836 const BYTE *
const lowPrefix,
1837 const BYTE *
const dictStart,
1838 const size_t dictSize
1841 const BYTE *ip = (
const BYTE *)src;
1842 const BYTE *
const iend = ip + srcSize;
1845 BYTE *
const oend = op + outputSize;
1847 BYTE *oexit = op + targetOutputSize;
1849 const BYTE *
const dictEnd = (
const BYTE *)dictStart + dictSize;
1850 const unsigned inc32table[8] = {0, 1, 2, 1, 0, 4, 4, 4};
1851 const int dec64table[8] = {0, 0, 0, -1, -4, 1, 2, 3};
1854 const int checkOffset = ((safeDecode) && (dictSize < (
int)(64
KB)));
1857 const BYTE *
const shortiend =
1858 iend - (endOnInput ? 14 : 8) - 2 ;
1859 const BYTE *
const shortoend =
1860 oend - (endOnInput ? 14 : 8) - 18 ;
1862 DEBUGLOG(5,
"LZ4_decompress_generic (srcSize:%i)", srcSize);
1865 if ((partialDecoding) && (oexit > oend -
MFLIMIT))
1869 if ((endOnInput) && (
unlikely(outputSize == 0)))
1870 return ((srcSize == 1) && (*ip == 0)) ? 0
1872 if ((!endOnInput) && (
unlikely(outputSize == 0)))
1873 return (*ip == 0 ? 1 : -1);
1874 if ((endOnInput) &&
unlikely(srcSize == 0))
1882 unsigned const token = *ip++;
1883 size_t length = token >>
ML_BITS;
1885 assert(!endOnInput || ip <= iend);
1896 if ((endOnInput ? length !=
RUN_MASK : length <= 8)
1899 &&
likely((endOnInput ? ip < shortiend : 1) & (op <= shortoend))) {
1901 memcpy(op, ip, endOnInput ? 16 : 8);
1908 offset = LZ4_readLE16(ip);
1910 match = op - offset;
1913 if ((length !=
ML_MASK) && (offset >= 8) &&
1916 memcpy(op + 0, match + 0, 8);
1917 memcpy(op + 8, match + 8, 8);
1918 memcpy(op + 16, match + 16, 2);
1950 if (((endOnInput) &&
1951 ((cpy > (partialDecoding ? oexit : oend -
MFLIMIT)) ||
1954 if (partialDecoding) {
1958 if ((endOnInput) && (ip + length > iend))
1963 if ((!endOnInput) && (cpy != oend))
1966 if ((endOnInput) && ((ip + length != iend) || (cpy > oend)))
1969 memcpy(op, ip, length);
1979 offset = LZ4_readLE16(ip);
1981 match = op - offset;
1987 if ((checkOffset) && (
unlikely(match + dictSize < lowPrefix)))
2013 if (length <= (
size_t)(lowPrefix - match)) {
2016 memmove(op, dictEnd - (lowPrefix - match), length);
2021 size_t const copySize = (size_t)(lowPrefix - match);
2022 size_t const restSize = length - copySize;
2024 memcpy(op, dictEnd - copySize, copySize);
2026 if (restSize > (
size_t)(op - lowPrefix)) {
2027 BYTE *
const endOfMatch = op + restSize;
2028 const BYTE *copyFrom = lowPrefix;
2030 while (op < endOfMatch)
2031 *op++ = *copyFrom++;
2034 memcpy(op, lowPrefix, restSize);
2048 match += inc32table[offset];
2049 memcpy(op + 4, match, 4);
2050 match -= dec64table[offset];
2053 memcpy(op, match, 8);
2064 if (op < oCopyLimit) {
2066 match += oCopyLimit - op;
2073 memcpy(op, match, 8);
2082 return (
int)(((
char *)op) - dst);
2084 return (
int)(((
const char *)ip) - src);
2088 return (
int)(-(((
const char *)ip) - src)) - 1;
2095 int maxDecompressedSize)
2104 int compressedSize,
int targetOutputSize,
2105 int maxDecompressedSize)
2108 source, dest, compressedSize, maxDecompressedSize,
endOnInputSize,
2125 int compressedSize,
int maxOutputSize)
2142 static int LZ4_decompress_safe_withSmallPrefix(
const char *source,
char *dest,
2149 (
BYTE *)dest - prefixSize,
NULL, 0);
2154 #define LZ4_decompress_safe_extDict LZ4_decompress_safe_forceExtDict
2157 int compressedSize,
int maxOutputSize,
2158 const void *dictStart,
size_t dictSize)
2166 static int LZ4_decompress_fast_extDict(
const char *source,
char *dest,
2167 int originalSize,
const void *dictStart,
2172 (
BYTE *)dest, (
const BYTE *)dictStart, dictSize);
2181 int compressedSize,
int maxOutputSize,
2182 size_t prefixSize,
const void *dictStart,
2187 (
BYTE *)dest - prefixSize,
2188 (
const BYTE *)dictStart, dictSize);
2193 int originalSize,
size_t prefixSize,
2194 const void *dictStart,
size_t dictSize)
2198 (
BYTE *)dest - prefixSize, (
const BYTE *)dictStart, dictSize);
2225 const char *dictionary,
int dictSize)
2249 if (maxBlockSize < 0)
2253 if (maxBlockSize < 16)
2268 const char *source,
char *dest,
2269 int compressedSize,
int maxOutputSize)
2288 source, dest, compressedSize, maxOutputSize);
2290 result = LZ4_decompress_safe_withSmallPrefix(
2291 source, dest, compressedSize, maxOutputSize, lz4sd->
prefixSize);
2294 source, dest, compressedSize, maxOutputSize, lz4sd->
prefixSize,
2319 const char *source,
char *dest,
2338 source, dest, originalSize, lz4sd->
prefixSize,
2348 result = LZ4_decompress_fast_extDict(source, dest, originalSize,
2368 int compressedSize,
int maxOutputSize,
2369 const char *dictStart,
int dictSize)
2373 if (dictStart + dictSize == dest) {
2374 if (dictSize >= 64
KB - 1)
2376 source, dest, compressedSize, maxOutputSize);
2377 return LZ4_decompress_safe_withSmallPrefix(source, dest, compressedSize,
2378 maxOutputSize, dictSize);
2381 maxOutputSize, dictStart, dictSize);
2385 int originalSize,
const char *dictStart,
2388 if (dictSize == 0 || dictStart + dictSize == dest)
2390 return LZ4_decompress_fast_extDict(source, dest, originalSize, dictStart,
2404 int LZ4_compress(
const char *source,
char *dest,
int inputSize)
2411 char *dst,
int srcSize,
int dstSize)
2423 const char *src,
char *dst,
int srcSize,
2431 char *dest,
int inputSize)
2444 int LZ4_uncompress(
const char *source,
char *dest,
int outputSize)
2479 ->internal_donotuse.dictionary;
int LZ4_compress_fast_extState(void *state, const char *source, char *dest, int inputSize, int maxOutputSize, int acceleration)
int LZ4_decompress_fast_usingDict(const char *source, char *dest, int originalSize, const char *dictStart, int dictSize)
const char * LZ4_versionString(void)
#define LZ4_STATIC_ASSERT(c)
#define ACCELERATION_DEFAULT
int LZ4_compressBound(int isize)
int LZ4_loadDict(LZ4_stream_t *LZ4_dict, const char *dictionary, int dictSize)
LZ4_stream_t * LZ4_createStream(void)
LZ4_streamDecode_t * LZ4_createStreamDecode(void)
int LZ4_compress_default(const char *source, char *dest, int inputSize, int maxOutputSize)
LZ4_FORCE_INLINE int LZ4_decompress_fast_doubleDict(const char *source, char *dest, int originalSize, size_t prefixSize, const void *dictStart, size_t dictSize)
int LZ4_decompress_safe_usingDict(const char *source, char *dest, int compressedSize, int maxOutputSize, const char *dictStart, int dictSize)
void * LZ4_create(char *inputBuffer)
LZ4_FORCE_INLINE int LZ4_compress_generic(LZ4_stream_t_internal *const cctx, const char *const source, char *const dest, const int inputSize, int *inputConsumed, const int maxOutputSize, const limitedOutput_directive outputLimited, const tableType_t tableType, const dict_directive dictDirective, const dictIssue_directive dictIssue, const U32 acceleration)
LZ4_FORCE_INLINE const BYTE * LZ4_getPosition(const BYTE *p, const void *tableBase, tableType_t tableType, const BYTE *srcBase)
int LZ4_compress_limitedOutput(const char *source, char *dest, int inputSize, int maxOutputSize)
int LZ4_decoderRingBufferSize(int maxBlockSize)
int LZ4_compress_limitedOutput_continue(LZ4_stream_t *LZ4_stream, const char *src, char *dst, int srcSize, int dstCapacity)
LZ4_FORCE_O2_GCC_PPC64LE int LZ4_decompress_safe_withPrefix64k(const char *source, char *dest, int compressedSize, int maxOutputSize)
int LZ4_compress_destSize(const char *src, char *dst, int *srcSizePtr, int targetDstSize)
#define LZ4_FORCE_O2_GCC_PPC64LE
int LZ4_sizeofState(void)
#define LZ4_FORCE_O2_INLINE_GCC_PPC64LE
LZ4_FORCE_O2_GCC_PPC64LE int LZ4_decompress_safe(const char *source, char *dest, int compressedSize, int maxDecompressedSize)
void LZ4_resetStream(LZ4_stream_t *LZ4_stream)
void LZ4_resetStream_fast(LZ4_stream_t *ctx)
LZ4_FORCE_O2_INLINE_GCC_PPC64LE void LZ4_wildCopy(void *dstPtr, const void *srcPtr, void *dstEnd)
int LZ4_freeStream(LZ4_stream_t *LZ4_stream)
LZ4_FORCE_INLINE void LZ4_putPosition(const BYTE *p, void *tableBase, tableType_t tableType, const BYTE *srcBase)
int LZ4_compress_fast(const char *source, char *dest, int inputSize, int maxOutputSize, int acceleration)
int LZ4_uncompress(const char *source, char *dest, int outputSize)
int LZ4_compress_continue(LZ4_stream_t *LZ4_stream, const char *source, char *dest, int inputSize)
int LZ4_compress(const char *source, char *dest, int inputSize)
int LZ4_uncompress_unknownOutputSize(const char *source, char *dest, int isize, int maxOutputSize)
LZ4_FORCE_INLINE void LZ4_prepareTable(LZ4_stream_t_internal *const cctx, const int inputSize, const tableType_t tableType)
LZ4_FORCE_INLINE int LZ4_decompress_safe_doubleDict(const char *source, char *dest, int compressedSize, int maxOutputSize, size_t prefixSize, const void *dictStart, size_t dictSize)
char * LZ4_slideInputBuffer(void *state)
int LZ4_sizeofStreamState(void)
LZ4_FORCE_INLINE unsigned LZ4_count(const BYTE *pIn, const BYTE *pMatch, const BYTE *pInLimit)
int LZ4_setStreamDecode(LZ4_streamDecode_t *LZ4_streamDecode, const char *dictionary, int dictSize)
int LZ4_versionNumber(void)
int LZ4_compress_withState(void *state, const char *src, char *dst, int srcSize)
int LZ4_compress_forceExtDict(LZ4_stream_t *LZ4_dict, const char *source, char *dest, int srcSize)
LZ4_FORCE_O2_GCC_PPC64LE int LZ4_decompress_safe_continue(LZ4_streamDecode_t *LZ4_streamDecode, const char *source, char *dest, int compressedSize, int maxOutputSize)
#define ALLOC_AND_ZERO(s)
#define MEM_INIT(p, v, s)
int LZ4_compress_limitedOutput_withState(void *state, const char *src, char *dst, int srcSize, int dstSize)
int LZ4_saveDict(LZ4_stream_t *LZ4_dict, char *safeBuffer, int dictSize)
int LZ4_compress_fast_continue(LZ4_stream_t *LZ4_stream, const char *source, char *dest, int inputSize, int maxOutputSize, int acceleration)
LZ4_FORCE_INLINE U32 LZ4_hashPosition(const void *const p, tableType_t const tableType)
int LZ4_resetStreamState(void *state, char *inputBuffer)
#define assert(condition)
void LZ4_attach_dictionary(LZ4_stream_t *working_stream, const LZ4_stream_t *dictionary_stream)
int LZ4_compress_fast_force(const char *source, char *dest, int inputSize, int maxOutputSize, int acceleration)
int LZ4_compress_fast_extState_fastReset(void *state, const char *src, char *dst, int srcSize, int dstCapacity, int acceleration)
LZ4_FORCE_O2_GCC_PPC64LE int LZ4_decompress_safe_partial(const char *source, char *dest, int compressedSize, int targetOutputSize, int maxDecompressedSize)
LZ4_FORCE_O2_GCC_PPC64LE LZ4_FORCE_INLINE int LZ4_decompress_generic(const char *const src, char *const dst, int srcSize, int outputSize, int endOnInput, int partialDecoding, int targetOutputSize, int dict, const BYTE *const lowPrefix, const BYTE *const dictStart, const size_t dictSize)
LZ4_FORCE_O2_GCC_PPC64LE int LZ4_decompress_fast_continue(LZ4_streamDecode_t *LZ4_streamDecode, const char *source, char *dest, int originalSize)
#define LZ4_decompress_safe_extDict
int LZ4_decompress_fast_withPrefix64k(const char *source, char *dest, int originalSize)
LZ4_FORCE_O2_GCC_PPC64LE int LZ4_decompress_fast(const char *source, char *dest, int originalSize)
int LZ4_freeStreamDecode(LZ4_streamDecode_t *LZ4_stream)
#define LZ4_HASHTABLESIZE
#define LZ4_COMPRESSBOUND(isize)
#define LZ4_VERSION_STRING
#define LZ4_HASH_SIZE_U32
#define LZ4_DECODER_RING_BUFFER_SIZE(mbs)
#define LZ4_MAX_INPUT_SIZE
#define LZ4_VERSION_NUMBER
const unsigned char * externalDict
const unsigned char * prefixEnd
const LZ4_stream_t_internal * dictCtx
unsigned int currentOffset
unsigned int hashTable[LZ4_HASH_SIZE_U32]
const unsigned char * dictionary
LZ4_streamDecode_t_internal internal_donotuse
LZ4_stream_t_internal internal_donotuse