Corrected issue 3 in compression function. Update is recommended.
authoryann.collet.73@gmail.com <yann.collet.73@gmail.com@650e7d94-2a16-8b24-b05c-7c0b3f6821cd>
Thu, 22 Sep 2011 13:11:17 +0000 (13:11 +0000)
committeryann.collet.73@gmail.com <yann.collet.73@gmail.com@650e7d94-2a16-8b24-b05c-7c0b3f6821cd>
Thu, 22 Sep 2011 13:11:17 +0000 (13:11 +0000)
git-svn-id: https://lz4.googlecode.com/svn/trunk@26 650e7d94-2a16-8b24-b05c-7c0b3f6821cd

lz4.c

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