34 #ifndef LZ4_CS_ADAPTER
43 #define MEMORY_USAGE 14
57 #if (defined(__x86_64__) || defined(__x86_64) || defined(__amd64__) || defined(__amd64) || defined(__ppc64__) || defined(_WIN64) || defined(__LP64__) || defined(_LP64) ) // Detects 64 bits mode
67 #if defined (__GLIBC__)
69 # if (__BYTE_ORDER == __BIG_ENDIAN)
70 # define LZ4_BIG_ENDIAN 1
72 #elif (defined(__BIG_ENDIAN__) || defined(__BIG_ENDIAN) || defined(_BIG_ENDIAN)) && !(defined(__LITTLE_ENDIAN__) || defined(__LITTLE_ENDIAN) || defined(_LITTLE_ENDIAN))
73 # define LZ4_BIG_ENDIAN 1
74 #elif defined(__sparc) || defined(__sparc__) \
75 || defined(__ppc__) || defined(_POWER) || defined(__powerpc__) || defined(_ARCH_PPC) || defined(__PPC__) || defined(__PPC) || defined(PPC) || defined(__powerpc__) || defined(__powerpc) || defined(powerpc) \
76 || defined(__hpux) || defined(__hppa) \
77 || defined(_MIPSEB) || defined(__s390__)
78 # define LZ4_BIG_ENDIAN 1
86 #if defined(__ARM_FEATURE_UNALIGNED)
87 # define LZ4_FORCE_UNALIGNED_ACCESS 1
91 #if defined(_MSC_VER) && defined(_WIN32_WCE) // Visual Studio for Windows CE does not support Hardware bit count
92 # define LZ4_FORCE_SW_BITCOUNT
98 #if __STDC_VERSION__ >= 199901L // C99
101 # define restrict // Disable restrict
104 #define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
106 #if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT) // Visual Studio
108 # if LZ4_ARCH64 && defined(_M_X64) // 64-bit
109 # pragma intrinsic(_BitScanForward64) // For Visual 2005
110 # pragma intrinsic(_BitScanReverse64) // For Visual 2005
112 # pragma intrinsic(_BitScanForward) // For Visual 2005
113 # pragma intrinsic(_BitScanReverse) // For Visual 2005
115 # pragma warning(disable : 4127) // disable: C4127: conditional expression is constant
119 # define lz4_bswap16(x) _byteswap_ushort(x)
121 # define lz4_bswap16(x) ((unsigned short int) ((((x) >> 8) & 0xffu) | (((x) & 0xffu) << 8)))
124 #if (GCC_VERSION >= 302) || (__INTEL_COMPILER >= 800) || defined(__clang__)
125 # define expect(expr,value) (__builtin_expect ((expr),(value)) )
127 # define expect(expr,value) (expr)
130 #define likely(expr) expect((expr) != 0, 1)
131 #define unlikely(expr) expect((expr) != 0, 0)
145 #if defined(_MSC_VER) // Visual Studio does not support 'stdint' natively
146 # define BYTE unsigned __int8
147 # define U16 unsigned __int16
148 # define U32 unsigned __int32
150 # define U64 unsigned __int64
154 # define BYTE uint8_t
155 # define U16 uint16_t
156 # define U32 uint32_t
158 # define U64 uint64_t
162 #ifndef LZ4_FORCE_UNALIGNED_ACCESS
163 # pragma pack(push, 1)
170 #ifndef LZ4_FORCE_UNALIGNED_ACCESS
174 #define A64(x) (((U64_S *)(x))->v)
175 #define A32(x) (((U32_S *)(x))->v)
176 #define A16(x) (((U16_S *)(x))->v)
184 #define HASH_LOG (MEMORY_USAGE-2)
185 #define HASHTABLESIZE (1 << HASH_LOG)
186 #define HASH_MASK (HASHTABLESIZE - 1)
188 #define NOTCOMPRESSIBLE_DETECTIONLEVEL 6 // Increasing this value will make the algorithm slower on incompressible data
189 #define SKIPSTRENGTH (NOTCOMPRESSIBLE_DETECTIONLEVEL>2?NOTCOMPRESSIBLE_DETECTIONLEVEL:2)
190 #define STACKLIMIT 13
191 #define HEAPMODE (HASH_LOG>STACKLIMIT) // Defines if memory is allocated into the stack (local variable), or into the heap (malloc()).
193 #define LASTLITERALS 5
194 #define MFLIMIT (COPYLENGTH+MINMATCH)
195 #define MINLENGTH (MFLIMIT+1)
198 #define MAX_DISTANCE ((1 << MAXD_LOG) - 1)
201 #define ML_MASK ((1U<<ML_BITS)-1)
202 #define RUN_BITS (8-ML_BITS)
203 #define RUN_MASK ((1U<<RUN_BITS)-1)
209 #if LZ4_ARCH64 // 64-bit
213 # define LZ4_COPYSTEP(s,d) A64(d) = A64(s); d+=8; s+=8;
214 # define LZ4_COPYPACKET(s,d) LZ4_COPYSTEP(s,d)
215 # define LZ4_SECURECOPY(s,d,e) if (d<e) LZ4_WILDCOPY(s,d,e)
217 # define INITBASE(base) const BYTE* const base = ip
222 # define LZ4_COPYSTEP(s,d) A32(d) = A32(s); d+=4; s+=4;
223 # define LZ4_COPYPACKET(s,d) LZ4_COPYSTEP(s,d); LZ4_COPYSTEP(s,d);
224 # define LZ4_SECURECOPY LZ4_WILDCOPY
225 # define HTYPE const BYTE*
226 # define INITBASE(base) const int base = 0
229 #if (defined(LZ4_BIG_ENDIAN) && !defined(BIG_ENDIAN_NATIVE_BUT_INCOMPATIBLE))
230 # define LZ4_READ_LITTLEENDIAN_16(d,s,p) { U16 v = A16(p); v = lz4_bswap16(v); d = (s) - v; }
231 # define LZ4_WRITE_LITTLEENDIAN_16(p,i) { U16 v = (U16)(i); v = lz4_bswap16(v); A16(p) = v; p+=2; }
232 #else // Little Endian
233 # define LZ4_READ_LITTLEENDIAN_16(d,s,p) { d = (s) - A16(p); }
234 # define LZ4_WRITE_LITTLEENDIAN_16(p,v) { A16(p) = v; p+=2; }
248 #define LZ4_HASH_FUNCTION(i) (((i) * 2654435761U) >> ((MINMATCH*8)-HASH_LOG))
249 #define LZ4_HASH_VALUE(p) LZ4_HASH_FUNCTION(A32(p))
250 #define LZ4_WILDCOPY(s,d,e) do { LZ4_COPYPACKET(s,d) } while (d<e);
251 #define LZ4_BLINDCOPY(s,d,l) { BYTE* e=(d)+l; LZ4_WILDCOPY(s,d,e); d=e; }
253 #endif // LZ4_CS_ADAPTER
258 #ifndef LZ4_CS_ADAPTER
264 #if defined(LZ4_BIG_ENDIAN)
265 #if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT) && defined(_M_X64)
267 _BitScanReverse64( &r, val );
269 #elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
270 return (__builtin_clzll(val) >> 3);
273 if (!(val>>32)) { r=4; }
else { r=0; val>>=32; }
274 if (!(val>>16)) { r+=2; val>>=8; }
else { val>>=24; }
279 #if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT) && defined(_M_X64)
281 _BitScanForward64( &r, val );
283 #elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
284 return (__builtin_ctzll(val) >> 3);
286 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 };
288 return DeBruijnBytePos[((
U64)((
U64)((
S64)val & -(
S64)val) * 0x0218A392CDABBD3F)) >> 58];
290 return DeBruijnBytePos[((
U64)((val & -val) * 0x0218A392CDABBD3F)) >> 58];
300 #if defined(LZ4_BIG_ENDIAN)
301 #if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
303 _BitScanReverse( &r, val );
305 #elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
306 return (__builtin_clz(val) >> 3);
309 if (!(val>>16)) { r=2; val>>=8; }
else { r=0; val>>=24; }
314 #if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
316 _BitScanForward( &r, val );
318 #elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
319 return (__builtin_ctz(val) >> 3);
321 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 };
323 return DeBruijnBytePos[((
U32)((
U32)((
S32)val & -(
S32)val) * 0x077CB531U)) >> 27];
325 return DeBruijnBytePos[((
U32)((val & -(
S32)val) * 0x077CB531U)) >> 27];
333 #endif // LZ4_CS_ADAPTER
360 const BYTE* anchor = ip;
369 const BYTE* matchlimit_1 = matchlimit - 1;
370 #if (LZ4_ARCH64 == 1)
371 const BYTE* matchlimit_3 = matchlimit - 3;
373 const BYTE* matchlimit_STEPSIZE_1 = matchlimit - (
STEPSIZE - 1);
377 #define matchlimit (iend - LASTLITERALS)
381 #ifndef LZ4_CS_ADAPTER
387 if (inputSize<
MINLENGTH)
goto _last_literals;
388 #ifndef LZ4_CS_ADAPTER
396 memset((
void*)HashTable, 0,
sizeof(srt->
hashTable));
409 int findMatchAttempts = (1U << skipStrength) + 3;
410 const BYTE* forwardIp = ip;
417 int step = findMatchAttempts++ >> skipStrength;
419 forwardIp = ip + step;
421 if unlikely(forwardIp > mflimit)
goto _last_literals;
424 ref = base + HashTable[h];
425 HashTable[h] = (
HTYPE)(ip - base);
430 while ((ip>anchor) && (ref>(
BYTE*)source) &&
unlikely(ip[-1]==ref[-1])) { ip--; ref--; }
433 length = (int)(ip - anchor);
436 if unlikely(op + length + (length>>8) > oend_LASTLITERALS_3)
return 0;
438 if unlikely(op + length + (2 + 1 + LASTLITERALS) + (length>>8) > oend)
return 0;
447 do { *op++ = 255; len -= 255; }
while (len>254);
449 memcpy(op, anchor, length);
463 for(; len > 254 ; len-=255) *op++ = 255;
466 else *token = (length<<
ML_BITS);
480 while likely(ip < matchlimit_STEPSIZE_1)
491 #if (LZ4_ARCH64 == 1)
492 if ((ip<matchlimit_3) && (
A32(ref) ==
A32(ip))) { ip+=4; ref+=4; }
494 if ((ip<matchlimit_1) && (
A16(ref) ==
A16(ip))) { ip+=2; ref+=2; }
495 if ((ip<matchlimit) && (*ref == *ip)) ip++;
497 if (
LZ4_ARCH64)
if ((ip<(matchlimit-3)) && (
A32(ref) ==
A32(ip))) { ip+=4; ref+=4; }
498 if ((ip<(matchlimit-1)) && (
A16(ref) ==
A16(ip))) { ip+=2; ref+=2; }
499 if ((ip<matchlimit) && (*ref == *ip)) ip++;
505 length = (int)(ip - anchor);
507 if unlikely(op + (length>>8) > oend_LASTLITERALS_1)
return 0;
509 if unlikely(op + (1 + LASTLITERALS) + (length>>8) > oend)
return 0;
516 for (; length > 509 ; length-=510) { *op++ = 255; *op++ = 255; }
517 if (length > 254) { length-=255; *op++ = 255; }
518 *op++ = (
BYTE)length;
520 else *token += (
BYTE)length;
523 if (ip > mflimit) { anchor = ip;
break; }
531 ref = base + HashTable[h];
532 HashTable[h] = (
HTYPE)(ip - base);
538 if ((ref > ip - (
MAX_DISTANCE + 1)) && (
A32(ref) ==
A32(ip))) { token = op++; *token=0;
goto _next_match; }
548 int lastRun = (int)(iend - anchor);
550 if ((
BYTE*)op + lastRun + 1 + ((lastRun+255-
RUN_MASK)/255) > oend)
return 0;
556 memcpy(op, anchor, iend - anchor);
561 return (
int) (((
char*)op)-
dest);
565 #ifndef LZ4_CS_ADAPTER
566 #define LZ4_64KLIMIT ((1<<16) + (MFLIMIT-1))
567 #define HASHLOG64K (HASH_LOG+1)
568 #define HASH64KTABLESIZE (1U<<HASHLOG64K)
569 #define LZ4_HASH64K_FUNCTION(i) (((i) * 2654435761U) >> ((MINMATCH*8)-HASHLOG64K))
570 #define LZ4_HASH64K_VALUE(p) LZ4_HASH64K_FUNCTION(A32(p))
587 const BYTE* anchor = ip;
588 const BYTE*
const base = ip;
597 const BYTE* matchlimit_1 = matchlimit - 1;
598 #if (LZ4_ARCH64 == 1)
599 const BYTE* matchlimit_3 = matchlimit - 3;
601 const BYTE* matchlimit_STEPSIZE_1 = matchlimit - (
STEPSIZE - 1);
605 #define matchlimit (iend - LASTLITERALS)
609 #ifndef LZ4_CS_ADAPTER
615 if (isize <
MINLENGTH)
goto _last_literals;
616 #ifndef LZ4_CS_ADAPTER
624 memset((
void*)HashTable, 0,
sizeof(srt->
hashTable));
636 int findMatchAttempts = (1U << skipStrength) + 3;
637 const BYTE* forwardIp = ip;
644 int step = findMatchAttempts++ >> skipStrength;
646 forwardIp = ip + step;
648 if (forwardIp > mflimit)
goto _last_literals;
651 ref = base + HashTable[h];
652 HashTable[h] = (
U16)(ip - base);
654 }
while (
A32(ref) !=
A32(ip));
657 while ((ip>anchor) && (ref>(
BYTE*)source) && (ip[-1]==ref[-1])) { ip--; ref--; }
660 length = (int)(ip - anchor);
663 if unlikely(op + length + (length>>8) > oend_LASTLITERALS_3)
return 0;
665 if unlikely(op + length + (2 + 1 + LASTLITERALS) + (length>>8) > oend)
return 0;
675 do { *op++ = 255; len -= 255; }
while (len>254);
677 memcpy(op, anchor, length);
686 if (length>=(
int)
RUN_MASK) { *token=(RUN_MASK<<
ML_BITS); len = length-
RUN_MASK;
for(; len > 254 ; len-=255) *op++ = 255; *op++ = (
BYTE)len; }
687 else *token = (length<<
ML_BITS);
701 while (ip<matchlimit_STEPSIZE_1)
712 #if (LZ4_ARCH64 == 1)
713 if ((ip<matchlimit_3) && (
A32(ref) ==
A32(ip))) { ip+=4; ref+=4; }
715 if ((ip<matchlimit_1) && (
A16(ref) ==
A16(ip))) { ip+=2; ref+=2; }
716 if ((ip<matchlimit) && (*ref == *ip)) ip++;
718 if (
LZ4_ARCH64)
if ((ip<(matchlimit-3)) && (
A32(ref) ==
A32(ip))) { ip+=4; ref+=4; }
719 if ((ip<(matchlimit-1)) && (
A16(ref) ==
A16(ip))) { ip+=2; ref+=2; }
720 if ((ip<matchlimit) && (*ref == *ip)) ip++;
726 len = (int)(ip - anchor);
728 if unlikely(op + (len>>8) > oend_LASTLITERALS_1)
return 0;
730 if unlikely(op + (1 + LASTLITERALS) + (len>>8) > oend)
return 0;
732 if (len>=(
int)
ML_MASK) { *token+=
ML_MASK; len-=
ML_MASK;
for(; len > 509 ; len-=510) { *op++ = 255; *op++ = 255; }
if (len > 254) { len-=255; *op++ = 255; } *op++ = (
BYTE)len; }
733 else *token += (
BYTE)len;
736 if (ip > mflimit) { anchor = ip;
break; }
744 ref = base + HashTable[h];
745 HashTable[h] = (
U16)(ip - base);
751 if (
A32(ref) ==
A32(ip)) { token = op++; *token=0;
goto _next_match; }
761 int lastRun = (int)(iend - anchor);
762 if (op + lastRun + 1 + (lastRun-
RUN_MASK+255)/255 > oend)
return 0;
765 memcpy(op, anchor, iend - anchor);
770 return (
int) (((
char*)op)-
dest);
780 void* ctx = malloc(
sizeof(
struct refTables));
782 if (ctx == NULL)
return 0;
785 else result =
LZ4_compressCtx(&ctx, source, dest, isize, maxOutputSize);
794 CORE_EXPORT(
int ) LZ4_compress(const
char* source,
798 return LZ4_compress_limitedOutput(source, dest, isize,
LZ4_compressBound(isize));
812 CORE_EXPORT(
int ) LZ4_uncompress(const
char* source,
821 BYTE*
const oend = op + osize;
827 const BYTE* oend_COPYLENGTH_STEPSIZE_4 = oend - COPYLENGTH - (
STEPSIZE - 4);
832 size_t dec32table[] = {0, 3, 2, 3, 0, 0, 0, 0};
834 size_t dec64table[] = {0, 0, 0, (size_t)-1, 0, 1, 2, 3};
844 if ((length=(token>>
ML_BITS)) ==
RUN_MASK) {
size_t len;
for (;(len=*ip++)==255;length+=255) { } length += len; }
849 if (cpy>oend_COPYLENGTH)
851 if (cpy>oend-COPYLENGTH)
854 if (cpy != oend)
goto _output_error;
855 memcpy(op, ip, length);
863 if unlikely(ref < (
BYTE*
const)dest)
goto _output_error;
866 if ((length=(token&
ML_MASK)) == ML_MASK) {
for (;*ip==255;length+=255) {ip++;} length += *ip++; }
872 size_t dec64 = dec64table[op-ref];
880 op += 4; ref += 4; ref -= dec32table[op-ref];
887 if unlikely(cpy > oend_COPYLENGTH_STEPSIZE_4)
893 if (cpy > oend_LASTLITERALS)
goto _output_error;
896 if (cpy > oend-LASTLITERALS)
goto _output_error;
899 while(op<cpy) *op++=*ref++;
909 return (
int) (((
char*)ip)-source);
913 return (
int) (-(((
char*)ip)-source));
916 CORE_EXPORT(
int ) LZ4_uncompress_unknownOutputSize(
933 const BYTE* iend_LASTLITERALS_1 = (iend-(LASTLITERALS+1));
935 const BYTE* oend_COPYLENGTH_STEPSIZE_4 = (oend-(COPYLENGTH+(
STEPSIZE-4)));
940 size_t dec32table[] = {0, 3, 2, 3, 0, 0, 0, 0};
942 size_t dec64table[] = {0, 0, 0, (size_t)-1, 0, 1, 2, 3};
946 if unlikely(ip==iend) goto _output_error;
959 while (
likely(ip<iend) && (s==255)) { s=*ip++; length +=
s; }
965 if ((cpy>oend_MFLIMIT) || (ip+length>iend_LASTLITERALS_3))
967 if ((cpy>oend-
MFLIMIT) || (ip+length>iend-(2+1+LASTLITERALS)))
970 if (cpy > oend)
goto _output_error;
971 if (ip+length != iend)
goto _output_error;
972 memcpy(op, ip, length);
980 if unlikely(ref < (
BYTE*
const)dest)
goto _output_error;
983 if ((length=(token&ML_MASK)) == ML_MASK)
986 while likely(ip<iend_LASTLITERALS_1)
988 while likely(ip<iend-(LASTLITERALS+1))
993 if (s==255)
continue;
1002 size_t dec64 = dec64table[op-ref];
1004 const int dec64 = 0;
1010 op += 4; ref += 4; ref -= dec32table[op-ref];
1017 if unlikely(cpy>oend_COPYLENGTH_STEPSIZE_4)
1023 if (cpy > oend_LASTLITERALS)
goto _output_error;
1026 if (cpy > oend-LASTLITERALS)
goto _output_error;
1029 while(op<cpy) *op++=*ref++;
1039 return (
int) (((
char*)op)-
dest);
1043 return (
int) (-(((
char*)ip)-source));
#define LZ4_HASH_VALUE(p)
#define LZ4_READ_LITTLEENDIAN_16(d, s, p)
HTYPE hashTable[HASHTABLESIZE]
#define LZ4_WRITE_LITTLEENDIAN_16(p, v)
static int LZ4_NbCommonBytes(register U32 val)
char int int maxOutputSize
#define LZ4_WILDCOPY(s, d, e)
#define LZ4_COPYSTEP(s, d)
static int LZ4_compressBound(int isize)
#define LZ4_HASH64K_VALUE(p)
static int LZ4_compressCtx(void **ctx, const char *source, char *dest, int inputSize, int maxOutputSize)
static int LZ4_compress64kCtx(void **ctx, const char *source, char *dest, int isize, int maxOutputSize)
#define LZ4_BLINDCOPY(s, d, l)