// Constants\r
//**************************************\r
#define MINMATCH 4\r
-#define INCOMPRESSIBLE 128\r
+#define SKIPSTRENGTH 6\r
\r
#define MAXD_LOG 16\r
#define MAX_DISTANCE ((1 << MAXD_LOG) - 1)\r
//**************************************\r
struct refTables\r
{\r
- BYTE* hashTable[HASHTABLESIZE];\r
+ const BYTE* hashTable[HASHTABLESIZE];\r
};\r
\r
\r
int isize)\r
{ \r
struct refTables *srt = (struct refTables *) (*ctx);\r
- BYTE** HashTable;\r
+ const BYTE** HashTable;\r
\r
- BYTE *ip = (BYTE*) source, \r
- *anchor = ip,\r
- *incompressible = anchor + INCOMPRESSIBLE,\r
- *iend = ip + isize,\r
- *ilimit = iend - MINMATCH;\r
+ const BYTE* ip = (BYTE*) source; \r
+ const BYTE* anchor = ip;\r
+ const BYTE* ref;\r
+ const BYTE* const iend = ip + isize;\r
+ const BYTE* const ilimit = iend - MINMATCH;\r
\r
- BYTE *op = (BYTE*) dest, \r
- *ref,\r
- *orun, *l_end;\r
+ BYTE* op = (BYTE*) dest;\r
+ BYTE* orun;\r
+ BYTE* l_end;\r
\r
- int len, length;\r
- U32 step=1;\r
+ int len, length;\r
+ const int skipStrength = SKIPSTRENGTH;\r
+ U32 skipped = 1U << skipStrength;\r
\r
\r
// Init \r
memset(HashTable, 0, sizeof(srt->hashTable));\r
\r
// Main Loop\r
- while (ip < ilimit)\r
+ while (ip < ilimit+1)\r
{\r
ref = HashTable[HASH_VALUE(ip)];\r
HashTable[HASH_VALUE(ip)] = ip;\r
\r
// Min Match\r
- if ((ref < ip - MAX_DISTANCE) || (*(U32*)ref != *(U32*)ip))\r
- { \r
- if (ip>incompressible) { incompressible += INCOMPRESSIBLE << (step >> 1); step++; }\r
- ip+=step; \r
- continue; \r
- }\r
- step=1;\r
+ if ((ref < ip - MAX_DISTANCE) || (*(U32*)ref != *(U32*)ip)) { ip += (skipped++) >> skipStrength ; continue; }\r
+ skipped = (1U << skipStrength) + 3;\r
\r
// Catch up\r
while ((ip>anchor) && (ref>(BYTE*)source) && (ip[-1]==ref[-1])) { ip--; ref--; } \r
do { *(U32*)op = *(U32*)anchor; op+=4; anchor+=4; } while (op<l_end) ;\r
op = l_end;\r
\r
+_next_match:\r
// Encode Offset\r
*(U16*)op = (ip-ref); op+=2;\r
\r
\r
// Encode MatchLength\r
if (len>=(int)ML_MASK) { *orun+=ML_MASK; len-=ML_MASK; for(; len > 509 ; len-=510) { *op++ = 255; *op++ = 255; } if (len > 254) { len-=255; *op++ = 255; } *op++ = (BYTE)len; } \r
- else *orun += len; \r
+ else *orun += len; \r
+\r
+ // Test end of chunk\r
+ if (ip > ilimit) { anchor = ip; break; }\r
+\r
+ // Test next position\r
+ ref = HashTable[HASH_VALUE(ip)];\r
+ HashTable[HASH_VALUE(ip)] = ip;\r
+ if ((ref > ip - (MAX_DISTANCE + 1)) && (*(U32*)ref == *(U32*)ip)) { orun = op++; *orun=0; goto _next_match; }\r
\r
// Prepare next loop\r
- anchor = ip; \r
- incompressible = anchor + INCOMPRESSIBLE;\r
+ anchor = ip++; \r
}\r
\r
// Encode Last Literals\r
}\r
\r
\r
+\r
int LZ4_compress(char* source, \r
char* dest,\r
int isize)\r