41 static void LZ4_wildCopy(
void* dstPtr,
const void* srcPtr,
void* dstEnd)
48 const size_t l2 = 8 - (((
size_t)d) & (
sizeof(
void*)-1));
62 #define WILDCOPYLENGTH 8
63 #define LASTLITERALS 5
64 #define MFLIMIT (WILDCOPYLENGTH+MINMATCH)
65 static const int LZ4_minLength = (MFLIMIT+1);
72 #define MAX_DISTANCE ((1 << MAXD_LOG) - 1)
75 #define ML_MASK ((1U<<ML_BITS)-1)
76 #define RUN_BITS (8-ML_BITS)
77 #define RUN_MASK ((1U<<RUN_BITS)-1)
83 typedef enum { noDict = 0, withPrefix64k, usingExtDict } dict_directive;
84 typedef enum { endOnOutputSize = 0, endOnInputSize = 1 } endCondition_directive;
85 typedef enum { full = 0, partial = 1 } earlyEnd_directive;
99 const char*
const source,
106 int targetOutputSize,
108 const BYTE*
const lowPrefix,
109 const BYTE*
const dictStart,
110 const size_t dictSize
114 const BYTE* ip = (
const BYTE*) source;
115 const BYTE*
const iend = ip + inputSize;
118 BYTE*
const oend = op + outputSize;
120 BYTE* oexit = op + targetOutputSize;
121 const BYTE*
const lowLimit = lowPrefix - dictSize;
123 const BYTE*
const dictEnd = (
const BYTE*)dictStart + dictSize;
124 const unsigned dec32table[] = {4, 1, 2, 1, 4, 4, 4, 4};
125 const int dec64table[] = {0, 0, 0, -1, 0, 1, 2, 3};
127 const int safeDecode = (endOnInput==endOnInputSize);
128 const int checkOffset = ((safeDecode) && (dictSize < (
int)(64 KB)));
129 const int inPlaceDecode = ((ip >= op) && (ip < oend));
133 if ((partialDecoding) && (oexit> oend-MFLIMIT)) oexit = oend-MFLIMIT;
134 if ((endOnInput) && (
unlikely(outputSize==0)))
return ((inputSize==1) && (*ip==0)) ? 0 : -1;
135 if ((!endOnInput) && (
unlikely(outputSize==0)))
return (*ip==0?1:-1);
146 if (
unlikely((inPlaceDecode) && (op + WILDCOPYLENGTH > ip)))
goto _output_error;
150 if ((
length=(token>>ML_BITS)) == RUN_MASK)
153 if ((endOnInput) &&
unlikely(ip>=iend-RUN_MASK))
goto _output_error;
159 while (
likely(endOnInput ? ip<iend-RUN_MASK : 1) && (
s==255) );
160 if ((safeDecode) &&
unlikely((
size_t)(op+
length)<(
size_t)(op)))
goto _output_error;
161 if ((safeDecode) &&
unlikely((
size_t)(ip+
length)<(
size_t)(ip)))
goto _output_error;
166 if (((endOnInput) && ((cpy>(partialDecoding?oexit:oend-MFLIMIT)) || (ip+
length>iend-(2+1+LASTLITERALS))) )
167 || ((!endOnInput) && (cpy>oend-WILDCOPYLENGTH)))
171 if (cpy > oend)
goto _output_error;
172 if ((endOnInput) && (ip+
length > iend))
goto _output_error;
176 if ((!endOnInput) && (cpy != oend))
goto _output_error;
177 if ((endOnInput) && ((ip+
length != iend) || (cpy > oend)))
goto _output_error;
184 LZ4_wildCopy(op, ip, cpy);
190 if ((checkOffset) && (
unlikely(match < lowLimit)))
goto _output_error;
199 if ((endOnInput) && (ip > iend-LASTLITERALS))
goto _output_error;
203 if ((safeDecode) &&
unlikely((
size_t)(op+
length)<(
size_t)op))
goto _output_error;
208 if ((dict==usingExtDict) && (match < lowPrefix))
212 if (
length <= (
size_t)(lowPrefix-match))
215 match = dictEnd - (lowPrefix-match);
221 size_t copySize = (
size_t)(lowPrefix-match);
222 memcpy(op, dictEnd - copySize, copySize);
224 copySize =
length - copySize;
225 if (copySize > (
size_t)(op-lowPrefix))
227 BYTE*
const endOfMatch = op + copySize;
228 const BYTE* copyFrom = lowPrefix;
229 while (op < endOfMatch) *op++ = *copyFrom++;
233 memcpy(op, lowPrefix, copySize);
244 const int dec64 = dec64table[
offset];
249 match += dec32table[
offset];
252 }
else {
LZ4_copy8(op, match); match+=8; }
257 BYTE*
const oCopyLimit = oend-(WILDCOPYLENGTH-1);
258 if (cpy > oend-LASTLITERALS)
goto _output_error;
261 LZ4_wildCopy(op, match, oCopyLimit);
262 match += oCopyLimit - op;
265 while (op<cpy) *op++ = *match++;
268 LZ4_wildCopy(op, match, cpy);
274 return (
int) (((
char*)op)-dest);
276 return (
int) (((
const char*)ip)-source);
280 return (
int) (-(((
const char*)ip)-source))-1;
void * memcpy(void *dest, const void *src, size_t n)
void * memmove(void *dest, const void *src, size_t n)
static uint16_t LZ4_readLE16(const void *src)
static void LZ4_copy8(void *dst, const void *src)
#define s(param, src_bits, pmcreg, dst_bits)