Corrected issue 3 in compression function. Update is recommended.

git-svn-id: https://lz4.googlecode.com/svn/trunk@26 650e7d94-2a16-8b24-b05c-7c0b3f6821cd
This commit is contained in:
yann.collet.73@gmail.com 2011-09-22 13:11:17 +00:00
parent 90bd4616ca
commit 3071281340

14
lz4.c
View File

@ -66,6 +66,7 @@
// Constants // Constants
//************************************** //**************************************
#define MINMATCH 4 #define MINMATCH 4
#define MINLENGTH 6
#define SKIPSTRENGTH 6 #define SKIPSTRENGTH 6
#define STACKLIMIT 13 #define STACKLIMIT 13
#define HEAPMODE (HASH_LOG>STACKLIMIT) // Defines if memory is allocated into the stack (local variable), or into the heap (malloc()). #define HEAPMODE (HASH_LOG>STACKLIMIT) // Defines if memory is allocated into the stack (local variable), or into the heap (malloc()).
@ -118,7 +119,8 @@ int LZ4_compressCtx(void** ctx,
const BYTE* ip = (BYTE*) source; const BYTE* ip = (BYTE*) source;
const BYTE* anchor = ip; const BYTE* anchor = ip;
const BYTE* const iend = ip + isize; const BYTE* const iend = ip + isize;
const BYTE* const ilimit = iend - MINMATCH; const BYTE* const ilm = iend - 1;
const BYTE* const ilimit = iend - MINMATCH - 1;
BYTE* op = (BYTE*) dest; BYTE* op = (BYTE*) dest;
BYTE* token; BYTE* token;
@ -130,6 +132,7 @@ int LZ4_compressCtx(void** ctx,
// Init // Init
if (isize<MINLENGTH) goto _last_literals;
#if HEAPMODE #if HEAPMODE
if (*ctx == NULL) if (*ctx == NULL)
{ {
@ -190,15 +193,15 @@ _next_match:
// Start Counting // Start Counting
ip+=MINMATCH; ref+=MINMATCH; // MinMatch verified ip+=MINMATCH; ref+=MINMATCH; // MinMatch verified
anchor = ip; anchor = ip;
while (ip<(iend-3)) while (ip<(iend-4))
{ {
if (*(U32*)ref == *(U32*)ip) { ip+=4; ref+=4; continue; } if (*(U32*)ref == *(U32*)ip) { ip+=4; ref+=4; continue; }
if (*(U16*)ref == *(U16*)ip) { ip+=2; ref+=2; } if (*(U16*)ref == *(U16*)ip) { ip+=2; ref+=2; }
if (*ref == *ip) ip++; if (*ref == *ip) ip++;
goto _endCount; goto _endCount;
} }
if ((ip<(iend-1)) && (*(U16*)ref == *(U16*)ip)) { ip+=2; ref+=2; } if ((ip<(iend-2)) && (*(U16*)ref == *(U16*)ip)) { ip+=2; ref+=2; }
if ((ip<iend) && (*ref == *ip)) ip++; if ((ip<iend-1) && (*ref == *ip)) ip++;
_endCount: _endCount:
len = (ip - anchor); len = (ip - anchor);
@ -207,7 +210,7 @@ _endCount:
else *token += len; else *token += len;
// Test end of chunk // Test end of chunk
if (ip > ilimit) { anchor = ip; break; } if (ip > ilimit-1) { anchor = ip; break; }
// Test next position // Test next position
ref = HashTable[HASH_VALUE(ip)]; ref = HashTable[HASH_VALUE(ip)];
@ -221,7 +224,6 @@ _endCount:
_last_literals: _last_literals:
// Encode Last Literals // Encode Last Literals
if (anchor < iend)
{ {
int lastLitRun = iend - anchor; int lastLitRun = iend - anchor;
if (lastLitRun>=(int)RUN_MASK) { *op++=(RUN_MASK<<ML_BITS); lastLitRun-=RUN_MASK; for(; lastLitRun > 254 ; lastLitRun-=255) *op++ = 255; *op++ = (BYTE) lastLitRun; } if (lastLitRun>=(int)RUN_MASK) { *op++=(RUN_MASK<<ML_BITS); lastLitRun-=RUN_MASK; for(; lastLitRun > 254 ; lastLitRun-=255) *op++ = 255; *op++ = (BYTE) lastLitRun; }