Eet: update lz4 code. Fix especially compilation bug on OpenBSD

see lz4 homepage for a full description of the fixes


SVN revision: 79975
This commit is contained in:
Vincent Torri 2012-12-02 08:15:06 +00:00
parent 88053411c6
commit 6f5a4a9a7c
4 changed files with 449 additions and 440 deletions

View File

@ -4,4 +4,4 @@ by:
yann.collet.73@gmail.com
Copyright/licensing info in source files here.
this was from revsion 66.
this was from revision 84.

View File

@ -100,7 +100,6 @@
#define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
#ifdef _MSC_VER // Visual Studio
# define inline __forceinline // Visual is not C99, but supports some kind of inline
# include <intrin.h> // For Visual 2005
# if LZ4_ARCH64 // 64-bit
# pragma intrinsic(_BitScanForward64) // For Visual 2005
@ -402,7 +401,7 @@ static inline int LZ4_compressCtx(void** ctx,
// Encode Literal length
length = (int)(ip - anchor);
token = op++;
if unlikely(op + length + (2 + 1 + LASTLITERALS) + (length>>8) >= oend) return 0; // Check output limit
if unlikely(op + length + (2 + 1 + LASTLITERALS) + (length>>8) > oend) return 0; // Check output limit
#ifdef _MSC_VER
if (length>=(int)RUN_MASK)
{
@ -449,7 +448,7 @@ _endCount:
// Encode MatchLength
len = (int)(ip - anchor);
if unlikely(op + (1 + LASTLITERALS) + (len>>8) >= oend) return 0; // Check output limit
if unlikely(op + (1 + LASTLITERALS) + (len>>8) > oend) return 0; // Check output limit
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; }
else *token += len;
@ -473,7 +472,7 @@ _last_literals:
// Encode Last Literals
{
int lastRun = (int)(iend - anchor);
if (((char*)op - dest) + lastRun + 1 + ((lastRun-15)/255) >= maxOutputSize) return 0;
if (((char*)op - dest) + lastRun + 1 + ((lastRun+255-RUN_MASK)/255) > (U32)maxOutputSize) return 0;
if (lastRun>=(int)RUN_MASK) { *op++=(RUN_MASK<<ML_BITS); lastRun-=RUN_MASK; for(; lastRun > 254 ; lastRun-=255) *op++ = 255; *op++ = (BYTE) lastRun; }
else *op++ = (lastRun<<ML_BITS);
memcpy(op, anchor, iend - anchor);
@ -567,7 +566,7 @@ static inline int LZ4_compress64kCtx(void** ctx,
// Encode Literal length
length = (int)(ip - anchor);
token = op++;
if unlikely(op + length + (2 + 1 + LASTLITERALS) + (length>>8) >= oend) return 0; // Check output limit
if unlikely(op + length + (2 + 1 + LASTLITERALS) + (length>>8) > oend) return 0; // Check output limit
#ifdef _MSC_VER
if (length>=(int)RUN_MASK)
{
@ -614,7 +613,7 @@ _endCount:
// Encode MatchLength
len = (int)(ip - anchor);
if unlikely(op + (1 + LASTLITERALS) + (len>>8) >= oend) return 0; // Check output limit
if unlikely(op + (1 + LASTLITERALS) + (len>>8) > oend) return 0; // Check output limit
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; }
else *token += len;
@ -638,7 +637,7 @@ _last_literals:
// Encode Last Literals
{
int lastRun = (int)(iend - anchor);
if (((char*)op - dest) + lastRun + 1 + ((lastRun)>>8) >= maxOutputSize) return 0;
if (op + lastRun + 1 + (lastRun-RUN_MASK+255)/255 > oend) return 0;
if (lastRun>=(int)RUN_MASK) { *op++=(RUN_MASK<<ML_BITS); lastRun-=RUN_MASK; for(; lastRun > 254 ; lastRun-=255) *op++ = 255; *op++ = (BYTE) lastRun; }
else *op++ = (lastRun<<ML_BITS);
memcpy(op, anchor, iend - anchor);
@ -696,9 +695,9 @@ int LZ4_uncompress(const char* source,
{
// Local Variables
const BYTE* restrict ip = (const BYTE*) source;
const BYTE* restrict ref;
const BYTE* ref;
BYTE* restrict op = (BYTE*) dest;
BYTE* op = (BYTE*) dest;
BYTE* const oend = op + osize;
BYTE* cpy;
@ -719,10 +718,10 @@ int LZ4_uncompress(const char* source,
cpy = op+length;
if unlikely(cpy>oend-COPYLENGTH)
{
if (cpy > oend) goto _output_error; // Error : request to write beyond destination buffer
if (cpy != oend) goto _output_error; // Error : we must necessarily stand at EOF
memcpy(op, ip, length);
ip += length;
break; // Necessarily EOF
break; // EOF
}
LZ4_WILDCOPY(ip, op, cpy); ip -= (op-cpy); op = cpy;
@ -757,7 +756,7 @@ int LZ4_uncompress(const char* source,
LZ4_SECURECOPY(ref, op, (oend-COPYLENGTH));
while(op<cpy) *op++=*ref++;
op=cpy;
if (op == oend) break; // Check EOF (should never happen, since last 5 bytes are supposed to be literals)
if (op == oend) goto _output_error; // Check EOF (should never happen, since last 5 bytes are supposed to be literals)
continue;
}
LZ4_SECURECOPY(ref, op, cpy);
@ -782,9 +781,9 @@ int LZ4_uncompress_unknownOutputSize(
// Local Variables
const BYTE* restrict ip = (const BYTE*) source;
const BYTE* const iend = ip + isize;
const BYTE* restrict ref;
const BYTE* ref;
BYTE* restrict op = (BYTE*) dest;
BYTE* op = (BYTE*) dest;
BYTE* const oend = op + maxOutputSize;
BYTE* cpy;
@ -805,12 +804,11 @@ int LZ4_uncompress_unknownOutputSize(
cpy = op+length;
if ((cpy>oend-COPYLENGTH) || (ip+length>iend-COPYLENGTH))
{
if (cpy > oend) goto _output_error; // Error : request to write beyond destination buffer
if (ip+length > iend) goto _output_error; // Error : request to read beyond source buffer
if (cpy > oend) goto _output_error; // Error : writes beyond output buffer
if (ip+length != iend) goto _output_error; // Error : LZ4 format requires to consume all input at this stage
memcpy(op, ip, length);
op += length;
ip += length;
if (ip<iend) goto _output_error; // Error : LZ4 format violation
ip = iend;
break; // Necessarily EOF, due to parsing restrictions
}
LZ4_WILDCOPY(ip, op, cpy); ip -= (op-cpy); op = cpy;
@ -846,7 +844,7 @@ int LZ4_uncompress_unknownOutputSize(
LZ4_SECURECOPY(ref, op, (oend-COPYLENGTH));
while(op<cpy) *op++=*ref++;
op=cpy;
if (op == oend) break; // Check EOF (should never happen, since last 5 bytes are supposed to be literals)
if (op == oend) goto _output_error; // Check EOF (should never happen, since last 5 bytes are supposed to be literals)
continue;
}
LZ4_SECURECOPY(ref, op, cpy);

View File

@ -38,6 +38,14 @@ extern "C" {
#endif
//**************************************
// Compiler Options
//**************************************
#ifdef _MSC_VER // Visual Studio
# define inline __inline // Visual is not C99, but supports some kind of inline
#endif
//****************************
// Simple Functions
//****************************
@ -50,7 +58,7 @@ LZ4_compress() :
Compresses 'isize' bytes from 'source' into 'dest'.
Destination buffer must be already allocated,
and must be sized to handle worst cases situations (input data not compressible)
Worst case size evaluation is provided by macro LZ4_compressBound()
Worst case size evaluation is provided by function LZ4_compressBound()
isize : is the input size. Max supported value is ~1.9GB
return : the number of bytes written in buffer dest
@ -60,7 +68,7 @@ LZ4_uncompress() :
osize : is the output size, therefore the original size
return : the number of bytes read in the source buffer
If the source stream is malformed, the function will stop decoding and return a negative result, indicating the byte position of the faulty instruction
This function never writes beyond dest + osize, and is therefore protected against malicious data packets
This function never writes outside of provided buffers, and never modifies input buffer.
note : destination buffer must be already allocated.
its size must be a minimum of 'osize' bytes.
*/
@ -70,12 +78,15 @@ LZ4_uncompress() :
// Advanced Functions
//****************************
#define LZ4_compressBound(isize) (isize + (isize/255) + 16)
static inline int LZ4_compressBound(int isize) { return ((isize) + ((isize)/255) + 16); }
#define LZ4_COMPRESSBOUND( isize) ((isize) + ((isize)/255) + 16)
/*
LZ4_compressBound() :
Provides the maximum size that LZ4 may output in a "worst case" scenario (input data not compressible)
primarily useful for memory allocation of output buffer.
inline function is recommended for the general case,
but macro is also provided when results need to be evaluated at compile time (such as table size allocation).
isize : is the input size. Max supported value is ~1.9GB
return : maximum output size in a "worst case" scenario

View File

@ -337,7 +337,7 @@ inline static int LZ4HC_InsertAndFindBestMatch (LZ4HC_Data_Structure* hc4, const
// HC4 match finder
LZ4HC_Insert(hc4, ip);
ref = HASH_POINTER(ip);
while ((ref > (ip-MAX_DISTANCE)) && (nbAttempts))
while ((ref >= (ip-MAX_DISTANCE)) && (nbAttempts))
{
nbAttempts--;
if (*(ref+ml) == *(ip+ml))
@ -380,7 +380,7 @@ inline static int LZ4HC_InsertAndGetWiderMatch (LZ4HC_Data_Structure* hc4, const
LZ4HC_Insert(hc4, ip);
ref = HASH_POINTER(ip);
while ((ref > ip-MAX_DISTANCE) && (ref >= hc4->base) && (nbAttempts))
while ((ref >= ip-MAX_DISTANCE) && (ref >= hc4->base) && (nbAttempts))
{
nbAttempts--;
if (*(startLimit + longest) == *(ref - delta + longest))