lz4: Update to version 1.9.4
Signed-off-by: Andrzej Perczak <linux@andrzejperczak.com> Change-Id: I2361395079194edad4bf78ae2506bbd83206514d Signed-off-by: Tashfin Shakeer Rhythm <tashfinshakeerrhythm@gmail.com> (cherry picked from commit d5cb2d20e3246fb549066d0759f8a78c15bbcf5a) Signed-off-by: TogoFire <togofire@mailfence.com> Signed-off-by: Kanishk <kanishkthederp@gmail.com> Signed-off-by: Sorayukii <sorayukii69@gmail.com>
This commit is contained in:
parent
6ac6e18ba0
commit
b4a06d01f2
3 changed files with 302 additions and 203 deletions
|
|
@ -58,9 +58,9 @@
|
|||
#define LZ4_MEMORY_USAGE 14
|
||||
|
||||
#define LZ4_MAX_INPUT_SIZE 0x7E000000 /* 2 113 929 216 bytes */
|
||||
#define LZ4_COMPRESSBOUND(isize) \
|
||||
((unsigned int)(isize) > (unsigned int)LZ4_MAX_INPUT_SIZE ? \
|
||||
0 : \
|
||||
#define LZ4_COMPRESSBOUND(isize) \
|
||||
((unsigned int)(isize) > (unsigned int)LZ4_MAX_INPUT_SIZE ? \
|
||||
0 : \
|
||||
(isize) + ((isize) / 255) + 16)
|
||||
|
||||
#define LZ4_ACCELERATION_DEFAULT 1
|
||||
|
|
@ -83,32 +83,33 @@
|
|||
/*-************************************************************************
|
||||
* STREAMING CONSTANTS AND STRUCTURES
|
||||
**************************************************************************/
|
||||
#define LZ4_STREAMSIZE \
|
||||
((1UL << LZ4_MEMORY_USAGE) + \
|
||||
#define LZ4_STREAM_MINSIZE \
|
||||
((1UL << LZ4_MEMORY_USAGE) + \
|
||||
32) /* static size, for inter-version compatibility */
|
||||
#define LZ4_STREAMSIZE_VOIDP (LZ4_STREAMSIZE / sizeof(void *))
|
||||
|
||||
#define LZ4_STREAMDECODESIZE_U64 \
|
||||
(4 + ((sizeof(void *) == 16) ? 2 : 0) /*AS-400*/)
|
||||
#define LZ4_STREAMDECODESIZE \
|
||||
(LZ4_STREAMDECODESIZE_U64 * sizeof(unsigned long long))
|
||||
|
||||
#define LZ4_STREAMHCSIZE_SIZET (262192 / sizeof(size_t))
|
||||
|
||||
/*! LZ4_stream_t :
|
||||
* Never ever use below internal definitions directly !
|
||||
* These definitions are not API/ABI safe, and may change in future versions.
|
||||
* If you need static allocation, declare or allocate an LZ4_stream_t object.
|
||||
**/
|
||||
|
||||
/*
|
||||
* LZ4_stream_t - information structure to track an LZ4 stream.
|
||||
*/
|
||||
typedef struct LZ4_stream_t_internal LZ4_stream_t_internal;
|
||||
struct LZ4_stream_t_internal {
|
||||
uint32_t hashTable[LZ4_HASH_SIZE_U32];
|
||||
uint32_t currentOffset;
|
||||
uint32_t tableType;
|
||||
const uint8_t *dictionary;
|
||||
const LZ4_stream_t_internal *dictCtx;
|
||||
uint32_t currentOffset;
|
||||
uint32_t tableType;
|
||||
uint32_t dictSize;
|
||||
/* Implicit padding to ensure structure is aligned */
|
||||
};
|
||||
typedef union {
|
||||
void *table[LZ4_STREAMSIZE_VOIDP];
|
||||
char minStateSize[LZ4_STREAM_MINSIZE];
|
||||
LZ4_stream_t_internal internal_donotuse;
|
||||
} LZ4_stream_t;
|
||||
|
||||
|
|
@ -143,21 +144,27 @@ typedef union {
|
|||
*
|
||||
* init this structure using LZ4_setStreamDecode (or memset()) before first use
|
||||
*/
|
||||
/*! LZ4_streamDecode_t :
|
||||
* Never ever use below internal definitions directly !
|
||||
* These definitions are not API/ABI safe, and may change in future versions.
|
||||
* If you need static allocation, declare or allocate an LZ4_streamDecode_t object.
|
||||
**/
|
||||
typedef struct {
|
||||
const uint8_t *externalDict;
|
||||
size_t extDictSize;
|
||||
const uint8_t *prefixEnd;
|
||||
size_t extDictSize;
|
||||
size_t prefixSize;
|
||||
} LZ4_streamDecode_t_internal;
|
||||
#define LZ4_STREAMDECODE_MINSIZE 32
|
||||
typedef union {
|
||||
unsigned long long table[LZ4_STREAMDECODESIZE_U64];
|
||||
char minStateSize[LZ4_STREAMDECODE_MINSIZE];
|
||||
LZ4_streamDecode_t_internal internal_donotuse;
|
||||
} LZ4_streamDecode_t;
|
||||
|
||||
/*-************************************************************************
|
||||
* SIZE OF STATE
|
||||
**************************************************************************/
|
||||
#define LZ4_MEM_COMPRESS LZ4_STREAMSIZE
|
||||
#define LZ4_MEM_COMPRESS sizeof(LZ4_stream_t)
|
||||
#define LZ4HC_MEM_COMPRESS LZ4_STREAMHCSIZE
|
||||
|
||||
/*-************************************************************************
|
||||
|
|
|
|||
|
|
@ -1068,7 +1068,8 @@ int LZ4_saveDict(LZ4_stream_t *LZ4_dict, char *safeBuffer, int dictSize)
|
|||
const BYTE *const previousDictEnd =
|
||||
dict->dictionary + dict->dictSize;
|
||||
assert(dict->dictionary);
|
||||
LZ4_memmove(safeBuffer, previousDictEnd - dictSize, dictSize);
|
||||
LZ4_memmove(safeBuffer, previousDictEnd - dictSize,
|
||||
(size_t)dictSize);
|
||||
}
|
||||
|
||||
dict->dictionary = (const BYTE *)safeBuffer;
|
||||
|
|
|
|||
|
|
@ -134,38 +134,185 @@ static FORCE_INLINE void LZ4_memcpy_using_offset(BYTE *dstPtr,
|
|||
}
|
||||
#endif
|
||||
|
||||
/* variant for decompress_unsafe()
|
||||
* does not know end of input
|
||||
* presumes input is well formed
|
||||
* note : will consume at least one byte */
|
||||
size_t read_long_length_no_check(const BYTE **pp)
|
||||
{
|
||||
size_t b, l = 0;
|
||||
do {
|
||||
b = **pp;
|
||||
(*pp)++;
|
||||
l += b;
|
||||
} while (b == 255);
|
||||
DEBUGLOG(6,
|
||||
"read_long_length_no_check: +length=%zu using %zu input bytes",
|
||||
l, l / 255 + 1)
|
||||
return l;
|
||||
}
|
||||
|
||||
/* core decoder variant for LZ4_decompress_fast*()
|
||||
* for legacy support only : these entry points are deprecated.
|
||||
* - Presumes input is correctly formed (no defense vs malformed inputs)
|
||||
* - Does not know input size (presume input buffer is "large enough")
|
||||
* - Decompress a full block (only)
|
||||
* @return : nb of bytes read from input.
|
||||
* Note : this variant is not optimized for speed, just for maintenance.
|
||||
* the goal is to remove support of decompress_fast*() variants by v2.0
|
||||
**/
|
||||
FORCE_INLINE int LZ4_decompress_unsafe_generic(
|
||||
const BYTE *const istart, BYTE *const ostart, int decompressedSize,
|
||||
size_t prefixSize,
|
||||
const BYTE *const dictStart, /* only if dict==usingExtDict */
|
||||
const size_t dictSize /* note: =0 if dictStart==NULL */
|
||||
)
|
||||
{
|
||||
const BYTE *ip = istart;
|
||||
BYTE *op = (BYTE *)ostart;
|
||||
BYTE *const oend = ostart + decompressedSize;
|
||||
const BYTE *const prefixStart = ostart - prefixSize;
|
||||
|
||||
DEBUGLOG(5, "LZ4_decompress_unsafe_generic");
|
||||
if (dictStart == NULL)
|
||||
assert(dictSize == 0);
|
||||
|
||||
while (1) {
|
||||
/* start new sequence */
|
||||
unsigned token = *ip++;
|
||||
|
||||
/* literals */
|
||||
{
|
||||
size_t ll = token >> ML_BITS;
|
||||
if (ll == 15) {
|
||||
/* long literal length */
|
||||
ll += read_long_length_no_check(&ip);
|
||||
}
|
||||
if ((size_t)(oend - op) < ll)
|
||||
return -1; /* output buffer overflow */
|
||||
LZ4_memmove(op, ip,
|
||||
ll); /* support in-place decompression */
|
||||
op += ll;
|
||||
ip += ll;
|
||||
if ((size_t)(oend - op) < MFLIMIT) {
|
||||
if (op == oend)
|
||||
break; /* end of block */
|
||||
DEBUGLOG(
|
||||
5,
|
||||
"invalid: literals end at distance %zi from end of block",
|
||||
oend - op);
|
||||
/* incorrect end of block :
|
||||
* last match must start at least MFLIMIT==12 bytes before end of output block */
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/* match */
|
||||
{
|
||||
size_t ml = token & 15;
|
||||
size_t const offset = LZ4_readLE16(ip);
|
||||
ip += 2;
|
||||
|
||||
if (ml == 15) {
|
||||
/* long literal length */
|
||||
ml += read_long_length_no_check(&ip);
|
||||
}
|
||||
ml += MINMATCH;
|
||||
|
||||
if ((size_t)(oend - op) < ml)
|
||||
return -1; /* output buffer overflow */
|
||||
|
||||
{
|
||||
const BYTE *match = op - offset;
|
||||
|
||||
/* out of range */
|
||||
if (offset >
|
||||
(size_t)(op - prefixStart) + dictSize) {
|
||||
DEBUGLOG(6, "offset out of range");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* check special case : extDict */
|
||||
if (offset > (size_t)(op - prefixStart)) {
|
||||
/* extDict scenario */
|
||||
const BYTE *const dictEnd =
|
||||
dictStart + dictSize;
|
||||
const BYTE *extMatch =
|
||||
dictEnd -
|
||||
(offset -
|
||||
(size_t)(op - prefixStart));
|
||||
size_t const extml =
|
||||
(size_t)(dictEnd - extMatch);
|
||||
if (extml > ml) {
|
||||
/* match entirely within extDict */
|
||||
LZ4_memmove(op, extMatch, ml);
|
||||
op += ml;
|
||||
ml = 0;
|
||||
} else {
|
||||
/* match split between extDict & prefix */
|
||||
LZ4_memmove(op, extMatch,
|
||||
extml);
|
||||
op += extml;
|
||||
ml -= extml;
|
||||
}
|
||||
match = prefixStart;
|
||||
}
|
||||
|
||||
/* match copy - slow variant, supporting overlap copy */
|
||||
{
|
||||
size_t u;
|
||||
for (u = 0; u < ml; u++) {
|
||||
op[u] = match[u];
|
||||
}
|
||||
}
|
||||
}
|
||||
op += ml;
|
||||
if ((size_t)(oend - op) < LASTLITERALS) {
|
||||
DEBUGLOG(
|
||||
5,
|
||||
"invalid: match ends at distance %zi from end of block",
|
||||
oend - op);
|
||||
/* incorrect end of block :
|
||||
* last match must stop at least LASTLITERALS==5 bytes before end of output block */
|
||||
return -1;
|
||||
}
|
||||
} /* match */
|
||||
} /* main loop */
|
||||
return (int)(ip - istart);
|
||||
}
|
||||
|
||||
/* Read the variable-length literal or match length.
|
||||
*
|
||||
* ip - pointer to use as input.
|
||||
* lencheck - end ip. Return an error if ip advances >= lencheck.
|
||||
* loop_check - check ip >= lencheck in body of loop. Returns loop_error if so.
|
||||
* initial_check - check ip >= lencheck before start of loop. Returns initial_error if so.
|
||||
* error (output) - error code. Should be set to 0 before call.
|
||||
*/
|
||||
typedef enum {
|
||||
loop_error = -2,
|
||||
initial_error = -1,
|
||||
ok = 0
|
||||
} variable_length_error;
|
||||
static FORCE_INLINE unsigned
|
||||
read_variable_length(const BYTE **ip, const BYTE *lencheck, int loop_check,
|
||||
int initial_check, variable_length_error *error)
|
||||
* @ip : input pointer
|
||||
* @ilimit : position after which if length is not decoded, the input is necessarily corrupted.
|
||||
* @initial_check - check ip >= ipmax before start of loop. Returns initial_error if so.
|
||||
* @error (output) - error code. Must be set to 0 before call.
|
||||
**/
|
||||
typedef size_t Rvl_t;
|
||||
static const Rvl_t rvl_error = (Rvl_t)(-1);
|
||||
static FORCE_INLINE Rvl_t read_variable_length(const BYTE **ip,
|
||||
const BYTE *ilimit,
|
||||
int initial_check)
|
||||
{
|
||||
U32 length = 0;
|
||||
U32 s;
|
||||
Rvl_t s, length = 0;
|
||||
assert(ip != NULL);
|
||||
assert(*ip != NULL);
|
||||
assert(ilimit != NULL);
|
||||
if (initial_check &&
|
||||
unlikely((*ip) >= lencheck)) { /* overflow detection */
|
||||
*error = initial_error;
|
||||
return length;
|
||||
unlikely((*ip) >= ilimit)) { /* read limit reached */
|
||||
return rvl_error;
|
||||
}
|
||||
do {
|
||||
s = **ip;
|
||||
(*ip)++;
|
||||
length += s;
|
||||
if (loop_check &&
|
||||
unlikely((*ip) >= lencheck)) { /* overflow detection */
|
||||
*error = loop_error;
|
||||
return length;
|
||||
if (unlikely((*ip) > ilimit)) { /* read limit reached */
|
||||
return rvl_error;
|
||||
}
|
||||
/* accumulator overflow detection (32-bit mode only) */
|
||||
if ((sizeof(length) < 8) &&
|
||||
unlikely(length > ((Rvl_t)(-1) / 2))) {
|
||||
return rvl_error;
|
||||
}
|
||||
} while (s == 255);
|
||||
|
||||
|
|
@ -187,8 +334,6 @@ LZ4_decompress_generic(const char *const src, char *const dst, int srcSize,
|
|||
*/
|
||||
int outputSize,
|
||||
/* endOnOutputSize, endOnInputSize */
|
||||
endCondition_directive endOnInput,
|
||||
/* full, partial */
|
||||
earlyEnd_directive partialDecoding,
|
||||
/* noDict, withPrefix64k, usingExtDict */
|
||||
dict_directive dict,
|
||||
|
|
@ -214,15 +359,13 @@ LZ4_decompress_generic(const char *const src, char *const dst, int srcSize,
|
|||
const BYTE *const dictEnd =
|
||||
(dictStart == NULL) ? NULL : dictStart + dictSize;
|
||||
|
||||
const int safeDecode = (endOnInput == endOnInputSize);
|
||||
const int checkOffset =
|
||||
((safeDecode) && (dictSize < (int)(64 * KB)));
|
||||
const int checkOffset = (dictSize < (int)(64 * KB));
|
||||
|
||||
/* Set up the "end" pointers for the shortcut. */
|
||||
const BYTE *const shortiend =
|
||||
iend - (endOnInput ? 14 : 8) /*maxLL*/ - 2 /*offset*/;
|
||||
iend - 14 /*maxLL*/ - 2 /*offset*/;
|
||||
const BYTE *const shortoend =
|
||||
oend - (endOnInput ? 14 : 8) /*maxLL*/ - 18 /*maxML*/;
|
||||
oend - 14 /*maxLL*/ - 18 /*maxML*/;
|
||||
|
||||
const BYTE *match;
|
||||
size_t offset;
|
||||
|
|
@ -234,55 +377,47 @@ LZ4_decompress_generic(const char *const src, char *const dst, int srcSize,
|
|||
|
||||
/* Special cases */
|
||||
assert(lowPrefix <= op);
|
||||
if ((endOnInput) && (unlikely(outputSize == 0))) {
|
||||
if (unlikely(outputSize == 0)) {
|
||||
/* Empty output buffer */
|
||||
if (partialDecoding)
|
||||
return 0;
|
||||
return ((srcSize == 1) && (*ip == 0)) ? 0 : -1;
|
||||
}
|
||||
if ((!endOnInput) && (unlikely(outputSize == 0))) {
|
||||
return (*ip == 0 ? 1 : -1);
|
||||
}
|
||||
if ((endOnInput) && unlikely(srcSize == 0)) {
|
||||
if (unlikely(srcSize == 0)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Currently the fast loop shows a regression on qualcomm arm chips. */
|
||||
/* LZ4_FAST_DEC_LOOP:
|
||||
* designed for modern OoO performance cpus,
|
||||
* where copying reliably 32-bytes is preferable to an unpredictable branch.
|
||||
* note : fast loop may show a regression for some client arm chips. */
|
||||
#if LZ4_FAST_DEC_LOOP
|
||||
if ((oend - op) < FASTLOOP_SAFE_DISTANCE) {
|
||||
DEBUGLOG(6, "skip fast decode loop");
|
||||
goto safe_decode;
|
||||
}
|
||||
|
||||
/* Fast loop : decode sequences as long as output < iend-FASTLOOP_SAFE_DISTANCE */
|
||||
/* Fast loop : decode sequences as long as output < oend-FASTLOOP_SAFE_DISTANCE */
|
||||
while (1) {
|
||||
/* Main fastloop assertion: We can always wildcopy FASTLOOP_SAFE_DISTANCE */
|
||||
assert(oend - op >= FASTLOOP_SAFE_DISTANCE);
|
||||
if (endOnInput) {
|
||||
assert(ip < iend);
|
||||
}
|
||||
assert(ip < iend);
|
||||
token = *ip++;
|
||||
length = token >> ML_BITS; /* literal length */
|
||||
|
||||
assert(!endOnInput ||
|
||||
ip <= iend); /* ip < iend before the increment */
|
||||
|
||||
/* decode literal length */
|
||||
if (length == RUN_MASK) {
|
||||
variable_length_error error = ok;
|
||||
length += read_variable_length(
|
||||
&ip, iend - RUN_MASK, (int)endOnInput,
|
||||
(int)endOnInput, &error);
|
||||
if (error == initial_error) {
|
||||
size_t const addl = read_variable_length(
|
||||
&ip, iend - RUN_MASK, 1);
|
||||
if (addl == rvl_error) {
|
||||
goto _output_error;
|
||||
}
|
||||
if ((safeDecode) &&
|
||||
unlikely((uptrval)(op) + length <
|
||||
length += addl;
|
||||
if (unlikely((uptrval)(op) + length <
|
||||
(uptrval)(op))) {
|
||||
goto _output_error;
|
||||
} /* overflow detection */
|
||||
if ((safeDecode) &&
|
||||
unlikely((uptrval)(ip) + length <
|
||||
if (unlikely((uptrval)(ip) + length <
|
||||
(uptrval)(ip))) {
|
||||
goto _output_error;
|
||||
} /* overflow detection */
|
||||
|
|
@ -290,46 +425,26 @@ LZ4_decompress_generic(const char *const src, char *const dst, int srcSize,
|
|||
/* copy literals */
|
||||
cpy = op + length;
|
||||
LZ4_STATIC_ASSERT(MFLIMIT >= WILDCOPYLENGTH);
|
||||
if (endOnInput) { /* LZ4_decompress_safe() */
|
||||
if ((cpy > oend - 32) ||
|
||||
(ip + length > iend - 32)) {
|
||||
goto safe_literal_copy;
|
||||
}
|
||||
LZ4_wildCopy32(op, ip, cpy);
|
||||
} else { /* LZ4_decompress_fast() */
|
||||
if (cpy > oend - 8) {
|
||||
goto safe_literal_copy;
|
||||
}
|
||||
LZ4_wildCopy8(
|
||||
op, ip,
|
||||
cpy); /* LZ4_decompress_fast() cannot copy more than 8 bytes at a time :
|
||||
* it doesn't know input length, and only relies on end-of-block properties */
|
||||
if ((cpy > oend - 32) ||
|
||||
(ip + length > iend - 32)) {
|
||||
goto safe_literal_copy;
|
||||
}
|
||||
LZ4_wildCopy32(op, ip, cpy);
|
||||
ip += length;
|
||||
op = cpy;
|
||||
} else {
|
||||
cpy = op + length;
|
||||
if (endOnInput) { /* LZ4_decompress_safe() */
|
||||
DEBUGLOG(
|
||||
7,
|
||||
"copy %u bytes in a 16-bytes stripe",
|
||||
(unsigned)length);
|
||||
/* We don't need to check oend, since we check it once for each loop below */
|
||||
if (ip >
|
||||
iend - (16 +
|
||||
1 /*max lit + offset + nextToken*/)) {
|
||||
goto safe_literal_copy;
|
||||
}
|
||||
/* Literals can only be 14, but hope compilers optimize if we copy by a register size */
|
||||
LZ4_memcpy(op, ip, 16);
|
||||
} else { /* LZ4_decompress_fast() */
|
||||
/* LZ4_decompress_fast() cannot copy more than 8 bytes at a time :
|
||||
* it doesn't know input length, and relies on end-of-block properties */
|
||||
LZ4_memcpy(op, ip, 8);
|
||||
if (length > 8) {
|
||||
LZ4_memcpy(op + 8, ip + 8, 8);
|
||||
}
|
||||
DEBUGLOG(7,
|
||||
"copy %u bytes in a 16-bytes stripe",
|
||||
(unsigned)length);
|
||||
/* We don't need to check oend, since we check it once for each loop below */
|
||||
if (ip >
|
||||
iend - (16 +
|
||||
1 /*max lit + offset + nextToken*/)) {
|
||||
goto safe_literal_copy;
|
||||
}
|
||||
/* Literals can only be <= 14, but hope compilers optimize better when copy by a register size */
|
||||
LZ4_memcpy(op, ip, 16);
|
||||
ip += length;
|
||||
op = cpy;
|
||||
}
|
||||
|
|
@ -338,29 +453,27 @@ LZ4_decompress_generic(const char *const src, char *const dst, int srcSize,
|
|||
offset = LZ4_readLE16(ip);
|
||||
ip += 2;
|
||||
match = op - offset;
|
||||
assert(match <= op);
|
||||
assert(match <= op); /* overflow check */
|
||||
|
||||
/* get matchlength */
|
||||
length = token & ML_MASK;
|
||||
|
||||
if (length == ML_MASK) {
|
||||
variable_length_error error = ok;
|
||||
size_t const addl = read_variable_length(
|
||||
&ip, iend - LASTLITERALS + 1, 0);
|
||||
if (addl == rvl_error) {
|
||||
goto _output_error;
|
||||
}
|
||||
length += addl;
|
||||
length += MINMATCH;
|
||||
if (unlikely((uptrval)(op) + length <
|
||||
(uptrval)op)) {
|
||||
goto _output_error;
|
||||
} /* overflow detection */
|
||||
if ((checkOffset) &&
|
||||
(unlikely(match + dictSize < lowPrefix))) {
|
||||
goto _output_error;
|
||||
} /* Error : offset outside buffers */
|
||||
length += read_variable_length(
|
||||
&ip, iend - LASTLITERALS + 1,
|
||||
(int)endOnInput, 0, &error);
|
||||
if (error != ok) {
|
||||
goto _output_error;
|
||||
}
|
||||
if ((safeDecode) &&
|
||||
unlikely((uptrval)(op) + length <
|
||||
(uptrval)op)) {
|
||||
goto _output_error;
|
||||
} /* overflow detection */
|
||||
length += MINMATCH;
|
||||
if (op + length >=
|
||||
oend - FASTLOOP_SAFE_DISTANCE) {
|
||||
goto safe_match_copy;
|
||||
|
|
@ -372,7 +485,7 @@ LZ4_decompress_generic(const char *const src, char *const dst, int srcSize,
|
|||
goto safe_match_copy;
|
||||
}
|
||||
|
||||
/* Fastpath check: Avoids a branch in LZ4_wildCopy32 if true */
|
||||
/* Fastpath check: skip LZ4_wildCopy32 when true */
|
||||
if ((dict == withPrefix64k) ||
|
||||
(match >= lowPrefix)) {
|
||||
if (offset >= 8) {
|
||||
|
|
@ -397,6 +510,7 @@ LZ4_decompress_generic(const char *const src, char *const dst, int srcSize,
|
|||
} /* Error : offset outside buffers */
|
||||
/* match starting within external dictionary */
|
||||
if ((dict == usingExtDict) && (match < lowPrefix)) {
|
||||
assert(dictEnd != NULL);
|
||||
if (unlikely(op + length >
|
||||
oend - LASTLITERALS)) {
|
||||
if (partialDecoding) {
|
||||
|
|
@ -463,12 +577,10 @@ safe_decode:
|
|||
|
||||
/* Main Loop : decode remaining sequences where output < FASTLOOP_SAFE_DISTANCE */
|
||||
while (1) {
|
||||
assert(ip < iend);
|
||||
token = *ip++;
|
||||
length = token >> ML_BITS; /* literal length */
|
||||
|
||||
assert(!endOnInput ||
|
||||
ip <= iend); /* ip < iend before the increment */
|
||||
|
||||
/* A two-stage shortcut for the most common case:
|
||||
* 1) If the literal length is 0..14, and there is enough space,
|
||||
* enter the shortcut and copy 16 bytes on behalf of the literals
|
||||
|
|
@ -478,12 +590,11 @@ safe_decode:
|
|||
* those 18 bytes earlier, upon entering the shortcut (in other words,
|
||||
* there is a combined check for both stages).
|
||||
*/
|
||||
if ((endOnInput ? length != RUN_MASK : length <= 8)
|
||||
if ((length != RUN_MASK)
|
||||
/* strictly "less than" on input, to re-enter the loop with at least one byte */
|
||||
&& likely((endOnInput ? ip < shortiend : 1) &
|
||||
(op <= shortoend))) {
|
||||
&& likely((ip < shortiend) & (op <= shortoend))) {
|
||||
/* Copy the literals */
|
||||
LZ4_memcpy(op, ip, endOnInput ? 16 : 8);
|
||||
LZ4_memcpy(op, ip, 16);
|
||||
op += length;
|
||||
ip += length;
|
||||
|
||||
|
|
@ -515,20 +626,17 @@ safe_decode:
|
|||
|
||||
/* decode literal length */
|
||||
if (length == RUN_MASK) {
|
||||
variable_length_error error = ok;
|
||||
length += read_variable_length(
|
||||
&ip, iend - RUN_MASK, (int)endOnInput,
|
||||
(int)endOnInput, &error);
|
||||
if (error == initial_error) {
|
||||
size_t const addl = read_variable_length(
|
||||
&ip, iend - RUN_MASK, 1);
|
||||
if (addl == rvl_error) {
|
||||
goto _output_error;
|
||||
}
|
||||
if ((safeDecode) &&
|
||||
unlikely((uptrval)(op) + length <
|
||||
length += addl;
|
||||
if (unlikely((uptrval)(op) + length <
|
||||
(uptrval)(op))) {
|
||||
goto _output_error;
|
||||
} /* overflow detection */
|
||||
if ((safeDecode) &&
|
||||
unlikely((uptrval)(ip) + length <
|
||||
if (unlikely((uptrval)(ip) + length <
|
||||
(uptrval)(ip))) {
|
||||
goto _output_error;
|
||||
} /* overflow detection */
|
||||
|
|
@ -540,10 +648,8 @@ safe_decode:
|
|||
safe_literal_copy:
|
||||
#endif
|
||||
LZ4_STATIC_ASSERT(MFLIMIT >= WILDCOPYLENGTH);
|
||||
if (((endOnInput) &&
|
||||
((cpy > oend - MFLIMIT) ||
|
||||
(ip + length > iend - (2 + 1 + LASTLITERALS)))) ||
|
||||
((!endOnInput) && (cpy > oend - WILDCOPYLENGTH))) {
|
||||
if ((cpy > oend - MFLIMIT) ||
|
||||
(ip + length > iend - (2 + 1 + LASTLITERALS))) {
|
||||
/* We've either hit the input parsing restriction or the output parsing restriction.
|
||||
* In the normal scenario, decoding a full block, it must be the last sequence,
|
||||
* otherwise it's an error (invalid input or dimensions).
|
||||
|
|
@ -553,7 +659,6 @@ safe_literal_copy:
|
|||
/* Since we are partial decoding we may be in this block because of the output parsing
|
||||
* restriction, which is not valid since the output buffer is allowed to be undersized.
|
||||
*/
|
||||
assert(endOnInput);
|
||||
DEBUGLOG(
|
||||
7,
|
||||
"partialDecoding: copying literals, close to input or output end")
|
||||
|
|
@ -585,18 +690,11 @@ safe_literal_copy:
|
|||
length = (size_t)(oend - op);
|
||||
}
|
||||
} else {
|
||||
/* We must be on the last sequence because of the parsing limitations so check
|
||||
* that we exactly regenerate the original size (must be exact when !endOnInput).
|
||||
*/
|
||||
if ((!endOnInput) && (cpy != oend)) {
|
||||
goto _output_error;
|
||||
}
|
||||
/* We must be on the last sequence (or invalid) because of the parsing limitations
|
||||
* so check that we exactly consume the input and don't overrun the output buffer.
|
||||
*/
|
||||
if ((endOnInput) &&
|
||||
((ip + length != iend) ||
|
||||
(cpy > oend))) {
|
||||
if ((ip + length != iend) ||
|
||||
(cpy > oend)) {
|
||||
DEBUGLOG(
|
||||
6,
|
||||
"should have been last run of literals")
|
||||
|
|
@ -614,7 +712,7 @@ safe_literal_copy:
|
|||
}
|
||||
LZ4_memmove(
|
||||
op, ip,
|
||||
length); /* supports overlapping memory regions; only matters for in-place decompression scenarios */
|
||||
length); /* supports overlapping memory regions, for in-place decompression scenarios */
|
||||
ip += length;
|
||||
op += length;
|
||||
/* Necessarily EOF when !partialDecoding.
|
||||
|
|
@ -629,7 +727,7 @@ safe_literal_copy:
|
|||
} else {
|
||||
LZ4_wildCopy8(
|
||||
op, ip,
|
||||
cpy); /* may overwrite up to WILDCOPYLENGTH beyond cpy */
|
||||
cpy); /* can overwrite up to 8 bytes beyond cpy */
|
||||
ip += length;
|
||||
op = cpy;
|
||||
}
|
||||
|
|
@ -644,14 +742,13 @@ safe_literal_copy:
|
|||
|
||||
_copy_match:
|
||||
if (length == ML_MASK) {
|
||||
variable_length_error error = ok;
|
||||
length += read_variable_length(
|
||||
&ip, iend - LASTLITERALS + 1,
|
||||
(int)endOnInput, 0, &error);
|
||||
if (error != ok)
|
||||
size_t const addl = read_variable_length(
|
||||
&ip, iend - LASTLITERALS + 1, 0);
|
||||
if (addl == rvl_error) {
|
||||
goto _output_error;
|
||||
if ((safeDecode) &&
|
||||
unlikely((uptrval)(op) + length <
|
||||
}
|
||||
length += addl;
|
||||
if (unlikely((uptrval)(op) + length <
|
||||
(uptrval)op))
|
||||
goto _output_error; /* overflow detection */
|
||||
}
|
||||
|
|
@ -665,6 +762,7 @@ safe_match_copy:
|
|||
goto _output_error; /* Error : offset outside buffers */
|
||||
/* match starting within external dictionary */
|
||||
if ((dict == usingExtDict) && (match < lowPrefix)) {
|
||||
assert(dictEnd != NULL);
|
||||
if (unlikely(op + length >
|
||||
oend - LASTLITERALS)) {
|
||||
if (partialDecoding)
|
||||
|
|
@ -776,15 +874,9 @@ safe_match_copy:
|
|||
}
|
||||
|
||||
/* end of decoding */
|
||||
if (endOnInput) {
|
||||
DEBUGLOG(5, "decoded %i bytes",
|
||||
(int)(((char *)op) - dst));
|
||||
return (int)(((char *)op) -
|
||||
dst); /* Nb of output bytes decoded */
|
||||
} else {
|
||||
return (int)(((const char *)ip) -
|
||||
src); /* Nb of input bytes read */
|
||||
}
|
||||
DEBUGLOG(5, "decoded %i bytes", (int)(((char *)op) - dst));
|
||||
return (int)(((char *)op) -
|
||||
dst); /* Nb of output bytes decoded */
|
||||
|
||||
/* Overflow error detected */
|
||||
_output_error:
|
||||
|
|
@ -796,9 +888,8 @@ int LZ4_decompress_safe(const char *source, char *dest, int compressedSize,
|
|||
int maxDecompressedSize)
|
||||
{
|
||||
return LZ4_decompress_generic(source, dest, compressedSize,
|
||||
maxDecompressedSize, endOnInputSize,
|
||||
decode_full_block, noDict, (BYTE *)dest,
|
||||
NULL, 0);
|
||||
maxDecompressedSize, decode_full_block,
|
||||
noDict, (BYTE *)dest, NULL, 0);
|
||||
}
|
||||
|
||||
int LZ4_decompress_safe_partial(const char *src, char *dst, int compressedSize,
|
||||
|
|
@ -806,16 +897,14 @@ int LZ4_decompress_safe_partial(const char *src, char *dst, int compressedSize,
|
|||
{
|
||||
dstCapacity = min(targetOutputSize, dstCapacity);
|
||||
return LZ4_decompress_generic(src, dst, compressedSize, dstCapacity,
|
||||
endOnInputSize, partial_decode, noDict,
|
||||
(BYTE *)dst, NULL, 0);
|
||||
partial_decode, noDict, (BYTE *)dst, NULL,
|
||||
0);
|
||||
}
|
||||
|
||||
int LZ4_decompress_fast(const char *source, char *dest, int originalSize)
|
||||
{
|
||||
return LZ4_decompress_generic(source, dest, 0, originalSize,
|
||||
endOnOutputSize, decode_full_block,
|
||||
withPrefix64k, (BYTE *)dest - 64 * KB,
|
||||
NULL, 0);
|
||||
return LZ4_decompress_unsafe_generic((const BYTE *)source, (BYTE *)dest,
|
||||
originalSize, 0, NULL, 0);
|
||||
}
|
||||
|
||||
/* ===== Instantiate a few more decoding cases, used more than once. ===== */
|
||||
|
|
@ -824,9 +913,9 @@ static int LZ4_decompress_safe_withPrefix64k(const char *source, char *dest,
|
|||
int compressedSize, int maxOutputSize)
|
||||
{
|
||||
return LZ4_decompress_generic(source, dest, compressedSize,
|
||||
maxOutputSize, endOnInputSize,
|
||||
decode_full_block, withPrefix64k,
|
||||
(BYTE *)dest - 64 * KB, NULL, 0);
|
||||
maxOutputSize, decode_full_block,
|
||||
withPrefix64k, (BYTE *)dest - 64 * KB,
|
||||
NULL, 0);
|
||||
}
|
||||
|
||||
static int LZ4_decompress_safe_withSmallPrefix(const char *source, char *dest,
|
||||
|
|
@ -835,8 +924,7 @@ static int LZ4_decompress_safe_withSmallPrefix(const char *source, char *dest,
|
|||
size_t prefixSize)
|
||||
{
|
||||
return LZ4_decompress_generic(source, dest, compressedSize,
|
||||
maxOutputSize, endOnInputSize,
|
||||
decode_full_block, noDict,
|
||||
maxOutputSize, decode_full_block, noDict,
|
||||
(BYTE *)dest - prefixSize, NULL, 0);
|
||||
}
|
||||
|
||||
|
|
@ -845,20 +933,18 @@ int LZ4_decompress_safe_forceExtDict(const char *source, char *dest,
|
|||
const void *dictStart, size_t dictSize)
|
||||
{
|
||||
return LZ4_decompress_generic(source, dest, compressedSize,
|
||||
maxOutputSize, endOnInputSize,
|
||||
decode_full_block, usingExtDict,
|
||||
(BYTE *)dest, (const BYTE *)dictStart,
|
||||
dictSize);
|
||||
maxOutputSize, decode_full_block,
|
||||
usingExtDict, (BYTE *)dest,
|
||||
(const BYTE *)dictStart, dictSize);
|
||||
}
|
||||
|
||||
static int LZ4_decompress_fast_extDict(const char *source, char *dest,
|
||||
int originalSize, const void *dictStart,
|
||||
size_t dictSize)
|
||||
{
|
||||
return LZ4_decompress_generic(source, dest, 0, originalSize,
|
||||
endOnOutputSize, decode_full_block,
|
||||
usingExtDict, (BYTE *)dest,
|
||||
(const BYTE *)dictStart, dictSize);
|
||||
return LZ4_decompress_unsafe_generic((const BYTE *)source, (BYTE *)dest,
|
||||
originalSize, 0,
|
||||
(const BYTE *)dictStart, dictSize);
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -871,9 +957,8 @@ static FORCE_INLINE int LZ4_decompress_safe_doubleDict(
|
|||
size_t prefixSize, const void *dictStart, size_t dictSize)
|
||||
{
|
||||
return LZ4_decompress_generic(source, dest, compressedSize,
|
||||
maxOutputSize, endOnInputSize,
|
||||
decode_full_block, usingExtDict,
|
||||
(BYTE *)dest - prefixSize,
|
||||
maxOutputSize, decode_full_block,
|
||||
usingExtDict, (BYTE *)dest - prefixSize,
|
||||
(const BYTE *)dictStart, dictSize);
|
||||
}
|
||||
|
||||
|
|
@ -883,8 +968,8 @@ LZ4_decompress_fast_doubleDict(const char *source, char *dest, int originalSize,
|
|||
size_t dictSize)
|
||||
{
|
||||
return LZ4_decompress_generic(source, dest, 0, originalSize,
|
||||
endOnOutputSize, decode_full_block,
|
||||
usingExtDict, (BYTE *)dest - prefixSize,
|
||||
decode_full_block, usingExtDict,
|
||||
(BYTE *)dest - prefixSize,
|
||||
(const BYTE *)dictStart, dictSize);
|
||||
}
|
||||
|
||||
|
|
@ -968,12 +1053,17 @@ int LZ4_decompress_fast_continue(LZ4_streamDecode_t *LZ4_streamDecode,
|
|||
const char *source, char *dest,
|
||||
int originalSize)
|
||||
{
|
||||
LZ4_streamDecode_t_internal *lz4sd =
|
||||
&LZ4_streamDecode->internal_donotuse;
|
||||
LZ4_streamDecode_t_internal *const lz4sd =
|
||||
(assert(LZ4_streamDecode != NULL),
|
||||
&LZ4_streamDecode->internal_donotuse);
|
||||
int result;
|
||||
|
||||
DEBUGLOG(5, "LZ4_decompress_fast_continue (toDecodeSize=%i)",
|
||||
originalSize);
|
||||
assert(originalSize >= 0);
|
||||
|
||||
if (lz4sd->prefixSize == 0) {
|
||||
DEBUGLOG(5, "first invocation : no prefix nor extDict");
|
||||
assert(lz4sd->extDictSize == 0);
|
||||
result = LZ4_decompress_fast(source, dest, originalSize);
|
||||
if (result <= 0)
|
||||
|
|
@ -981,18 +1071,17 @@ int LZ4_decompress_fast_continue(LZ4_streamDecode_t *LZ4_streamDecode,
|
|||
lz4sd->prefixSize = (size_t)originalSize;
|
||||
lz4sd->prefixEnd = (BYTE *)dest + originalSize;
|
||||
} else if (lz4sd->prefixEnd == (BYTE *)dest) {
|
||||
if (lz4sd->prefixSize >= 64 * KB - 1 || lz4sd->extDictSize == 0)
|
||||
result =
|
||||
LZ4_decompress_fast(source, dest, originalSize);
|
||||
else
|
||||
result = LZ4_decompress_fast_doubleDict(
|
||||
source, dest, originalSize, lz4sd->prefixSize,
|
||||
lz4sd->externalDict, lz4sd->extDictSize);
|
||||
DEBUGLOG(5, "continue using existing prefix");
|
||||
result = LZ4_decompress_unsafe_generic(
|
||||
(const BYTE *)source, (BYTE *)dest, originalSize,
|
||||
lz4sd->prefixSize, lz4sd->externalDict,
|
||||
lz4sd->extDictSize);
|
||||
if (result <= 0)
|
||||
return result;
|
||||
lz4sd->prefixSize += (size_t)originalSize;
|
||||
lz4sd->prefixEnd += originalSize;
|
||||
} else {
|
||||
DEBUGLOG(5, "prefix becomes extDict");
|
||||
lz4sd->extDictSize = lz4sd->prefixSize;
|
||||
lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize;
|
||||
result = LZ4_decompress_fast_extDict(source, dest, originalSize,
|
||||
|
|
@ -1031,7 +1120,9 @@ int LZ4_decompress_fast_usingDict(const char *source, char *dest,
|
|||
int dictSize)
|
||||
{
|
||||
if (dictSize == 0 || dictStart + dictSize == dest)
|
||||
return LZ4_decompress_fast(source, dest, originalSize);
|
||||
return LZ4_decompress_unsafe_generic((const BYTE *)source,
|
||||
(BYTE *)dest, originalSize,
|
||||
(size_t)dictSize, NULL, 0);
|
||||
|
||||
return LZ4_decompress_fast_extDict(source, dest, originalSize,
|
||||
dictStart, dictSize);
|
||||
|
|
|
|||
Loading…
Reference in a new issue