80 #if defined(CPU_HAS_EFFICIENT_UNALIGNED_MEMORY_ACCESS) \
81 || defined(__ARM_FEATURE_UNALIGNED) \
82 || defined(__i386__) || defined(__x86_64__) \
83 || defined(_M_IX86) || defined(_M_X64) \
84 || defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_8__) \
85 || (defined(_M_ARM) && (_M_ARM >= 7))
86 # define LZ4_UNALIGNED_ACCESS 1
88 # define LZ4_UNALIGNED_ACCESS 0
95 #if defined(_MSC_VER) && defined(_WIN32_WCE)
96 # define LZ4_FORCE_SW_BITCOUNT
103 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
110 # define FORCE_INLINE static __forceinline
112 # pragma warning(disable : 4127)
113 # pragma warning(disable : 4293)
115 # if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
117 # define FORCE_INLINE static inline __attribute__((always_inline))
119 # define FORCE_INLINE static inline
122 # define FORCE_INLINE static
126 #define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
128 #if (GCC_VERSION >= 302) || (__INTEL_COMPILER >= 800) || defined(__clang__)
129 # define expect(expr,value) (__builtin_expect ((expr),(value)) )
131 # define expect(expr,value) (expr)
134 #define likely(expr) expect((expr) != 0, 1)
135 #define unlikely(expr) expect((expr) != 0, 0)
142 #define ALLOCATOR(n,s) calloc(n,s)
145 #define MEM_INIT memset
157 #if defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
166 typedef unsigned short U16;
169 typedef unsigned long long U64;
176 #define STEPSIZE sizeof(size_t)
178 static unsigned LZ4_64bits(
void) {
return sizeof(
void*)==8; }
180 static unsigned LZ4_isLittleEndian(
void)
182 const union {
U32 i;
BYTE c[4]; } one = { 1 };
187 static U16 LZ4_readLE16(
const void* memPtr)
190 return *(
U16*)memPtr;
193 const BYTE* p = memPtr;
194 return (
U16)((
U16)p[0] + (p[1]<<8));
198 static void LZ4_writeLE16(
void* memPtr,
U16 value)
202 *(
U16*)memPtr = value;
209 p[1] = (
BYTE)(value>>8);
214 static U16 LZ4_read16(
const void* memPtr)
217 return *(
U16*)memPtr;
221 memcpy(&val16, memPtr, 2);
226 static U32 LZ4_read32(
const void* memPtr)
229 return *(
U32*)memPtr;
233 memcpy(&val32, memPtr, 4);
238 static U64 LZ4_read64(
const void* memPtr)
241 return *(
U64*)memPtr;
245 memcpy(&val64, memPtr, 8);
250 static size_t LZ4_read_ARCH(
const void* p)
253 return (
size_t)LZ4_read64(p);
255 return (
size_t)LZ4_read32(p);
259 static void LZ4_copy4(
void* dstPtr,
const void* srcPtr)
263 *(
U32*)dstPtr = *(
U32*)srcPtr;
266 memcpy(dstPtr, srcPtr, 4);
269 static void LZ4_copy8(
void* dstPtr,
const void* srcPtr)
275 *(
U64*)dstPtr = *(
U64*)srcPtr;
277 ((
U32*)dstPtr)[0] = ((
U32*)srcPtr)[0],
278 ((
U32*)dstPtr)[1] = ((
U32*)srcPtr)[1];
282 memcpy(dstPtr, srcPtr, 8);
286 static void LZ4_wildCopy(
void* dstPtr,
const void* srcPtr,
void* dstEnd)
289 const BYTE* s = srcPtr;
291 do { LZ4_copy8(d,s); d+=8; s+=8; }
while (d<e);
301 #define LASTLITERALS 5
302 #define MFLIMIT (COPYLENGTH+MINMATCH)
303 static const int LZ4_minLength = (
MFLIMIT+1);
310 #define MAX_DISTANCE ((1 << MAXD_LOG) - 1)
313 #define ML_MASK ((1U<<ML_BITS)-1)
314 #define RUN_BITS (8-ML_BITS)
315 #define RUN_MASK ((1U<<RUN_BITS)-1)
321 #define LZ4_STATIC_ASSERT(c) { enum { LZ4_static_assert = 1/(int)(!!(c)) }; }
327 static unsigned LZ4_NbCommonBytes (
register size_t val)
329 if (LZ4_isLittleEndian())
333 # if defined(_MSC_VER) && defined(_WIN64) && !defined(LZ4_FORCE_SW_BITCOUNT)
335 _BitScanForward64( &r, (
U64)val );
337 # elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
338 return (__builtin_ctzll((
U64)val) >> 3);
340 static const int DeBruijnBytePos[64] = { 0, 0, 0, 0, 0, 1, 1, 2, 0, 3, 1, 3, 1, 4, 2, 7, 0, 2, 3, 6, 1, 5, 3, 5, 1, 3, 4, 4, 2, 5, 6, 7, 7, 0, 1, 2, 3, 3, 4, 6, 2, 6, 5, 5, 3, 4, 5, 6, 7, 1, 2, 4, 6, 4, 4, 5, 7, 2, 6, 5, 7, 6, 7, 7 };
341 return DeBruijnBytePos[((
U64)((val & -(
long long)val) * 0x0218A392CDABBD3FULL)) >> 58];
346 # if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
348 _BitScanForward( &r, (
U32)val );
350 # elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
351 return (__builtin_ctz((
U32)val) >> 3);
353 static const int DeBruijnBytePos[32] = { 0, 0, 3, 0, 3, 1, 3, 0, 3, 2, 2, 1, 3, 2, 0, 1, 3, 3, 1, 2, 2, 2, 2, 0, 3, 1, 2, 0, 1, 0, 1, 1 };
354 return DeBruijnBytePos[((
U32)((val & -(
S32)val) * 0x077CB531U)) >> 27];
362 # if defined(_MSC_VER) && defined(_WIN64) && !defined(LZ4_FORCE_SW_BITCOUNT)
364 _BitScanReverse64( &r, val );
365 return (
unsigned)(r>>3);
366 # elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
367 return (__builtin_clzll(val) >> 3);
370 if (!(val>>32)) { r=4; }
else { r=0; val>>=32; }
371 if (!(val>>16)) { r+=2; val>>=8; }
else { val>>=24; }
378 # if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
380 _BitScanReverse( &r, (
unsigned long)val );
381 return (
unsigned)(r>>3);
382 # elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
383 return (__builtin_clz(val) >> 3);
386 if (!(val>>16)) { r=2; val>>=8; }
else { r=0; val>>=24; }
394 static unsigned LZ4_count(
const BYTE* pIn,
const BYTE* pMatch,
const BYTE* pInLimit)
396 const BYTE*
const pStart = pIn;
400 size_t diff = LZ4_read_ARCH(pMatch) ^ LZ4_read_ARCH(pIn);
402 pIn += LZ4_NbCommonBytes(diff);
403 return (
unsigned)(pIn - pStart);
406 if (LZ4_64bits())
if ((pIn<(pInLimit-3)) && (LZ4_read32(pMatch) == LZ4_read32(pIn))) { pIn+=4; pMatch+=4; }
407 if ((pIn<(pInLimit-1)) && (LZ4_read16(pMatch) == LZ4_read16(pIn))) { pIn+=2; pMatch+=2; }
408 if ((pIn<pInLimit) && (*pMatch == *pIn)) pIn++;
409 return (
unsigned)(pIn - pStart);
413 #ifndef LZ4_COMMONDEFS_ONLY
417 #define LZ4_HASHLOG (LZ4_MEMORY_USAGE-2)
418 #define HASHTABLESIZE (1 << LZ4_MEMORY_USAGE)
419 #define HASH_SIZE_U32 (1 << LZ4_HASHLOG)
421 static const int LZ4_64Klimit = ((64
KB) + (
MFLIMIT-1));
422 static const U32 LZ4_skipTrigger = 6;
461 if (tableType ==
byU16)
467 static U32 LZ4_hashPosition(
const BYTE* p,
tableType_t tableType) {
return LZ4_hashSequence(LZ4_read32(p), tableType); }
469 static void LZ4_putPositionOnHash(
const BYTE* p,
U32 h,
void* tableBase,
tableType_t tableType,
const BYTE* srcBase)
473 case byPtr: {
const BYTE** hashTable = (
const BYTE**)tableBase; hashTable[h] = p;
return; }
474 case byU32: {
U32* hashTable = (
U32*) tableBase; hashTable[h] = (
U32)(p-srcBase);
return; }
475 case byU16: {
U16* hashTable = (
U16*) tableBase; hashTable[h] = (
U16)(p-srcBase);
return; }
479 static void LZ4_putPosition(
const BYTE* p,
void* tableBase,
tableType_t tableType,
const BYTE* srcBase)
481 U32 h = LZ4_hashPosition(p, tableType);
482 LZ4_putPositionOnHash(p, h, tableBase, tableType, srcBase);
485 static const BYTE* LZ4_getPositionOnHash(
U32 h,
void* tableBase,
tableType_t tableType,
const BYTE* srcBase)
487 if (tableType ==
byPtr) {
const BYTE** hashTable = (
const BYTE**) tableBase;
return hashTable[h]; }
488 if (tableType ==
byU32) {
U32* hashTable = (
U32*) tableBase;
return hashTable[h] + srcBase; }
489 {
U16* hashTable = (
U16*) tableBase;
return hashTable[h] + srcBase; }
494 U32 h = LZ4_hashPosition(p, tableType);
495 return LZ4_getPositionOnHash(h, tableBase, tableType, srcBase);
498 static int LZ4_compress_generic(
511 const BYTE* ip = (
const BYTE*) source;
513 const BYTE* lowLimit;
516 const BYTE*
const dictEnd = dictionary + dictPtr->
dictSize;
517 const size_t dictDelta = dictEnd - (
const BYTE*)source;
518 const BYTE* anchor = (
const BYTE*) source;
519 const BYTE*
const iend = ip + inputSize;
524 BYTE*
const olimit = op + maxOutputSize;
535 base = (
const BYTE*)source;
536 lowLimit = (
const BYTE*)source;
544 lowLimit = (
const BYTE*)source;
547 if ((tableType ==
byU16) && (inputSize>=LZ4_64Klimit))
return 0;
548 if (inputSize<LZ4_minLength)
goto _last_literals;
551 LZ4_putPosition(ip, ctx, tableType, base);
552 ip++; forwardH = LZ4_hashPosition(ip, tableType);
560 const BYTE* forwardIp = ip;
562 unsigned searchMatchNb = (1U << LZ4_skipTrigger);
569 step = searchMatchNb++ >> LZ4_skipTrigger;
571 if (
unlikely(forwardIp > mflimit))
goto _last_literals;
573 match = LZ4_getPositionOnHash(h, ctx, tableType, base);
576 if (match<(
const BYTE*)source)
578 refDelta = dictDelta;
579 lowLimit = dictionary;
584 lowLimit = (
const BYTE*)source;
587 forwardH = LZ4_hashPosition(forwardIp, tableType);
588 LZ4_putPositionOnHash(ip, h, ctx, tableType, base);
590 }
while ( ((dictIssue==
dictSmall) ? (match < lowRefLimit) : 0)
592 || (LZ4_read32(match+refDelta) != LZ4_read32(ip)) );
596 while ((ip>anchor) && (match+refDelta > lowLimit) && (
unlikely(ip[-1]==match[refDelta-1]))) { ip--; match--; }
600 unsigned litLength = (unsigned)(ip - anchor);
602 if ((outputLimited) && (
unlikely(op + litLength + (2 + 1 + LASTLITERALS) + (litLength/255) > olimit)))
608 for(; len >= 255 ; len-=255) *op++ = 255;
614 LZ4_wildCopy(op, anchor, op+litLength);
620 LZ4_writeLE16(op, (
U16)(ip-match)); op+=2;
624 unsigned matchLength;
630 limit = ip + (dictEnd-
match);
631 if (limit > matchlimit) limit = matchlimit;
636 unsigned more = LZ4_count(ip, (
const BYTE*)source, matchlimit);
647 if ((outputLimited) && (
unlikely(op + (1 + LASTLITERALS) + (matchLength>>8) > olimit)))
653 for (; matchLength >= 510 ; matchLength-=510) { *op++ = 255; *op++ = 255; }
654 if (matchLength >= 255) { matchLength-=255; *op++ = 255; }
655 *op++ = (
BYTE)matchLength;
657 else *token += (
BYTE)(matchLength);
663 if (ip > mflimit)
break;
666 LZ4_putPosition(ip-2, ctx, tableType, base);
669 match = LZ4_getPosition(ip, ctx, tableType, base);
672 if (match<(
const BYTE*)source)
674 refDelta = dictDelta;
675 lowLimit = dictionary;
680 lowLimit = (
const BYTE*)source;
683 LZ4_putPosition(ip, ctx, tableType, base);
684 if ( ((dictIssue==
dictSmall) ? (match>=lowRefLimit) : 1)
686 && (LZ4_read32(match+refDelta)==LZ4_read32(ip)) )
687 { token=op++; *token=0;
goto _next_match; }
690 forwardH = LZ4_hashPosition(++ip, tableType);
696 int lastRun = (int)(iend - anchor);
697 if ((outputLimited) && (((
char*)op - dest) + lastRun + 1 + ((lastRun+255-
RUN_MASK)/255) > (
U32)maxOutputSize))
701 memcpy(op, anchor, iend - anchor);
706 return (
int) (((
char*)op)-dest);
719 if (inputSize < LZ4_64Klimit)
739 if (inputSize < LZ4_64Klimit)
783 const BYTE* p = (
const BYTE*)dictionary;
784 const BYTE*
const dictEnd = p + dictSize;
796 if (p <= dictEnd - 64
KB) p = dictEnd - 64
KB;
804 LZ4_putPosition(p, dict,
byU32, base);
839 const BYTE* smallest = (
const BYTE*) source;
841 if ((streamPtr->
dictSize>0) && (smallest>dictEnd)) smallest = dictEnd;
842 LZ4_renormDictT(streamPtr, smallest);
846 const BYTE* sourceEnd = (
const BYTE*) source + inputSize;
847 if ((sourceEnd > streamPtr->
dictionary) && (sourceEnd < dictEnd))
857 if (dictEnd == (
const BYTE*)source)
901 const BYTE* smallest = dictEnd;
902 if (smallest > (
const BYTE*) source) smallest = (
const BYTE*) source;
920 if ((
U32)dictSize > 64
KB) dictSize = 64
KB;
923 memmove(safeBuffer, previousDictEnd - dictSize, dictSize);
943 const char*
const source,
950 int targetOutputSize,
952 const BYTE*
const lowPrefix,
953 const BYTE*
const dictStart,
954 const size_t dictSize
959 const BYTE*
const iend = ip + inputSize;
962 BYTE*
const oend = op + outputSize;
964 BYTE* oexit = op + targetOutputSize;
965 const BYTE*
const lowLimit = lowPrefix - dictSize;
967 const BYTE*
const dictEnd = (
const BYTE*)dictStart + dictSize;
968 const size_t dec32table[] = {4, 1, 2, 1, 4, 4, 4, 4};
969 const size_t dec64table[] = {0, 0, 0, (size_t)-1, 0, 1, 2, 3};
972 const int checkOffset = ((safeDecode) && (dictSize < (
int)(64
KB)));
976 if ((partialDecoding) && (oexit> oend-
MFLIMIT)) oexit = oend-
MFLIMIT;
977 if ((endOnInput) && (
unlikely(outputSize==0)))
return ((inputSize==1) && (*ip==0)) ? 0 : -1;
978 if ((!endOnInput) && (
unlikely(outputSize==0)))
return (*ip==0?1:-1);
999 if ((safeDecode) &&
unlikely((
size_t)(op+length)<(
size_t)(op)))
goto _output_error;
1000 if ((safeDecode) &&
unlikely((
size_t)(ip+length)<(
size_t)(ip)))
goto _output_error;
1005 if (((endOnInput) && ((cpy>(partialDecoding?oexit:oend-MFLIMIT)) || (ip+length>iend-(2+1+LASTLITERALS))) )
1008 if (partialDecoding)
1010 if (cpy > oend)
goto _output_error;
1011 if ((endOnInput) && (ip+length > iend))
goto _output_error;
1015 if ((!endOnInput) && (cpy != oend))
goto _output_error;
1016 if ((endOnInput) && ((ip+length != iend) || (cpy > oend)))
goto _output_error;
1018 memcpy(op, ip, length);
1023 LZ4_wildCopy(op, ip, cpy);
1027 match = cpy - LZ4_readLE16(ip); ip+=2;
1028 if ((checkOffset) && (
unlikely(match < lowLimit)))
goto _output_error;
1032 if (length == ML_MASK)
1037 if ((endOnInput) && (ip > iend-LASTLITERALS))
goto _output_error;
1041 if ((safeDecode) &&
unlikely((
size_t)(op+length)<(
size_t)op))
goto _output_error;
1048 if (
unlikely(op+length > oend-LASTLITERALS))
goto _output_error;
1050 if (length <= (
size_t)(lowPrefix-match))
1053 match = dictEnd - (lowPrefix-
match);
1054 memcpy(op, match, length);
1060 size_t copySize = (size_t)(lowPrefix-match);
1061 memcpy(op, dictEnd - copySize, copySize);
1063 copySize = length - copySize;
1064 if (copySize > (
size_t)(op-lowPrefix))
1066 BYTE*
const endOfMatch = op + copySize;
1067 const BYTE* copyFrom = lowPrefix;
1068 while (op < endOfMatch) *op++ = *copyFrom++;
1072 memcpy(op, lowPrefix, copySize);
1083 const size_t dec64 = dec64table[op-
match];
1088 match += dec32table[op-
match];
1089 LZ4_copy4(op+4, match);
1090 op += 8; match -= dec64;
1091 }
else { LZ4_copy8(op, match); op+=8; match+=8; }
1095 if (cpy > oend-LASTLITERALS)
goto _output_error;
1098 LZ4_wildCopy(op, match, oend-8);
1099 match += (oend-8) - op;
1102 while (op<cpy) *op++ = *match++;
1105 LZ4_wildCopy(op, match, cpy);
1111 return (
int) (((
char*)op)-dest);
1113 return (
int) (((
char*)ip)-source);
1117 return (
int) (-(((
char*)ip)-source))-1;
1123 return LZ4_decompress_generic(source, dest, compressedSize, maxDecompressedSize,
endOnInputSize,
full, 0,
noDict, (
BYTE*)dest, NULL, 0);
1128 return LZ4_decompress_generic(source, dest, compressedSize, maxDecompressedSize,
endOnInputSize,
partial, targetOutputSize,
noDict, (
BYTE*)dest, NULL, 0);
1133 return LZ4_decompress_generic(source, dest, 0, originalSize,
endOnOutputSize,
full, 0,
withPrefix64k, (
BYTE*)(dest - 64
KB), NULL, 64
KB);
1198 if (result <= 0)
return result;
1209 if (result <= 0)
return result;
1227 if (result <= 0)
return result;
1238 if (result <= 0)
return result;
1257 return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, safe,
full, 0,
noDict, (
BYTE*)dest, NULL, 0);
1258 if (dictStart+dictSize == dest)
1260 if (dictSize >= (
int)(64
KB - 1))
1261 return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, safe,
full, 0,
withPrefix64k, (
BYTE*)dest-64
KB, NULL, 0);
1262 return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, safe,
full, 0,
noDict, (
BYTE*)dest-dictSize, NULL, 0);
1264 return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, safe,
full, 0,
usingExtDict, (
BYTE*)dest, (
BYTE*)dictStart, dictSize);
1280 return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize,
endOnInputSize,
full, 0,
usingExtDict, (
BYTE*)dest, (
BYTE*)dictStart, dictSize);
1309 if ((((
size_t)state) & 3) != 0)
return 1;
1334 if (((
size_t)(state)&3) != 0)
return 0;
1337 if (inputSize < LZ4_64Klimit)
1345 if (((
size_t)(state)&3) != 0)
return 0;
1348 if (inputSize < LZ4_64Klimit)
1358 return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize,
endOnInputSize,
full, 0,
withPrefix64k, (
BYTE*)dest - 64
KB, NULL, 64
KB);
1363 return LZ4_decompress_generic(source, dest, 0, originalSize,
endOnOutputSize,
full, 0,
withPrefix64k, (
BYTE*)dest - 64
KB, NULL, 64
KB);