summaryrefslogtreecommitdiff
path: root/src/static_libs
diff options
context:
space:
mode:
authorVincent Torri <vincent.torri@gmail.com>2012-12-02 08:15:06 +0000
committerVincent Torri <vincent.torri@gmail.com>2012-12-02 08:15:06 +0000
commit6f5a4a9a7cf612eec5d76e00af1edc6e55c77ec8 (patch)
treed110c66eb85e030ae15fa845e72056da488428a0 /src/static_libs
parent88053411c63278452a9685b2419d5df796e0db2d (diff)
Eet: update lz4 code. Fix especially compilation bug on OpenBSD
see lz4 homepage for a full description of the fixes SVN revision: 79975
Diffstat (limited to 'src/static_libs')
-rw-r--r--src/static_libs/lz4/README2
-rw-r--r--src/static_libs/lz4/lz4.c886
-rw-r--r--src/static_libs/lz4/lz4.h75
-rw-r--r--src/static_libs/lz4/lz4hc.c4
4 files changed, 488 insertions, 479 deletions
diff --git a/src/static_libs/lz4/README b/src/static_libs/lz4/README
index 0abb857..718f773 100644
--- a/src/static_libs/lz4/README
+++ b/src/static_libs/lz4/README
@@ -4,4 +4,4 @@ by:
4 yann.collet.73@gmail.com 4 yann.collet.73@gmail.com
5Copyright/licensing info in source files here. 5Copyright/licensing info in source files here.
6 6
7this was from revsion 66. 7this was from revision 84.
diff --git a/src/static_libs/lz4/lz4.c b/src/static_libs/lz4/lz4.c
index eeefa67..a651748 100644
--- a/src/static_libs/lz4/lz4.c
+++ b/src/static_libs/lz4/lz4.c
@@ -91,7 +91,7 @@
91//************************************** 91//**************************************
92// Compiler Options 92// Compiler Options
93//************************************** 93//**************************************
94#if __STDC_VERSION__ >= 199901L // C99 94#if __STDC_VERSION__ >= 199901L // C99
95/* "restrict" is a known keyword */ 95/* "restrict" is a known keyword */
96#else 96#else
97# define restrict // Disable restrict 97# define restrict // Disable restrict
@@ -100,7 +100,6 @@
100#define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) 100#define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
101 101
102#ifdef _MSC_VER // Visual Studio 102#ifdef _MSC_VER // Visual Studio
103# define inline __forceinline // Visual is not C99, but supports some kind of inline
104# include <intrin.h> // For Visual 2005 103# include <intrin.h> // For Visual 2005
105# if LZ4_ARCH64 // 64-bit 104# if LZ4_ARCH64 // 64-bit
106# pragma intrinsic(_BitScanForward64) // For Visual 2005 105# pragma intrinsic(_BitScanForward64) // For Visual 2005
@@ -190,8 +189,8 @@ typedef struct _U64_S { U64 v; } U64_S;
190#define MAXD_LOG 16 189#define MAXD_LOG 16
191#define MAX_DISTANCE ((1 << MAXD_LOG) - 1) 190#define MAX_DISTANCE ((1 << MAXD_LOG) - 1)
192 191
193#define ML_BITS 4 192#define ML_BITS 4
194#define ML_MASK ((1U<<ML_BITS)-1) 193#define ML_MASK ((1U<<ML_BITS)-1)
195#define RUN_BITS (8-ML_BITS) 194#define RUN_BITS (8-ML_BITS)
196#define RUN_MASK ((1U<<RUN_BITS)-1) 195#define RUN_MASK ((1U<<RUN_BITS)-1)
197 196
@@ -203,20 +202,20 @@ typedef struct _U64_S { U64 v; } U64_S;
203# define STEPSIZE 8 202# define STEPSIZE 8
204# define UARCH U64 203# define UARCH U64
205# define AARCH A64 204# define AARCH A64
206# define LZ4_COPYSTEP(s,d) A64(d) = A64(s); d+=8; s+=8; 205# define LZ4_COPYSTEP(s,d) A64(d) = A64(s); d+=8; s+=8;
207# define LZ4_COPYPACKET(s,d) LZ4_COPYSTEP(s,d) 206# define LZ4_COPYPACKET(s,d) LZ4_COPYSTEP(s,d)
208# define LZ4_SECURECOPY(s,d,e) if (d<e) LZ4_WILDCOPY(s,d,e) 207# define LZ4_SECURECOPY(s,d,e) if (d<e) LZ4_WILDCOPY(s,d,e)
209# define HTYPE U32 208# define HTYPE U32
210# define INITBASE(base) const BYTE* const base = ip 209# define INITBASE(base) const BYTE* const base = ip
211#else // 32-bit 210#else // 32-bit
212# define STEPSIZE 4 211# define STEPSIZE 4
213# define UARCH U32 212# define UARCH U32
214# define AARCH A32 213# define AARCH A32
215# define LZ4_COPYSTEP(s,d) A32(d) = A32(s); d+=4; s+=4; 214# define LZ4_COPYSTEP(s,d) A32(d) = A32(s); d+=4; s+=4;
216# define LZ4_COPYPACKET(s,d) LZ4_COPYSTEP(s,d); LZ4_COPYSTEP(s,d); 215# define LZ4_COPYPACKET(s,d) LZ4_COPYSTEP(s,d); LZ4_COPYSTEP(s,d);
217# define LZ4_SECURECOPY LZ4_WILDCOPY 216# define LZ4_SECURECOPY LZ4_WILDCOPY
218# define HTYPE const BYTE* 217# define HTYPE const BYTE*
219# define INITBASE(base) const int base = 0 218# define INITBASE(base) const int base = 0
220#endif 219#endif
221 220
222#if (defined(LZ4_BIG_ENDIAN) && !defined(BIG_ENDIAN_NATIVE_BUT_INCOMPATIBLE)) 221#if (defined(LZ4_BIG_ENDIAN) && !defined(BIG_ENDIAN_NATIVE_BUT_INCOMPATIBLE))
@@ -233,7 +232,7 @@ typedef struct _U64_S { U64 v; } U64_S;
233//************************************** 232//**************************************
234struct refTables 233struct refTables
235{ 234{
236 HTYPE hashTable[HASHTABLESIZE]; 235 HTYPE hashTable[HASHTABLESIZE];
237}; 236};
238 237
239 238
@@ -261,11 +260,11 @@ static inline int LZ4_NbCommonBytes (register U64 val)
261 #elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT) 260 #elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
262 return (__builtin_clzll(val) >> 3); 261 return (__builtin_clzll(val) >> 3);
263 #else 262 #else
264 int r; 263 int r;
265 if (!(val>>32)) { r=4; } else { r=0; val>>=32; } 264 if (!(val>>32)) { r=4; } else { r=0; val>>=32; }
266 if (!(val>>16)) { r+=2; val>>=8; } else { val>>=24; } 265 if (!(val>>16)) { r+=2; val>>=8; } else { val>>=24; }
267 r += (!val); 266 r += (!val);
268 return r; 267 return r;
269 #endif 268 #endif
270#else 269#else
271 #if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT) 270 #if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
@@ -275,8 +274,8 @@ static inline int LZ4_NbCommonBytes (register U64 val)
275 #elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT) 274 #elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
276 return (__builtin_ctzll(val) >> 3); 275 return (__builtin_ctzll(val) >> 3);
277 #else 276 #else
278 static const int DeBruijnBytePos[64] = { 0, 0, 0, 0, 0, 1, 1, 2, 0, 3, 1, 3, 1, 4, 2, 7, 0, 2, 3, 6, 1, 5, 3, 5, 1, 3, 4, 4, 2, 5, 6, 7, 7, 0, 1, 2, 3, 3, 4, 6, 2, 6, 5, 5, 3, 4, 5, 6, 7, 1, 2, 4, 6, 4, 4, 5, 7, 2, 6, 5, 7, 6, 7, 7 }; 277 static const int DeBruijnBytePos[64] = { 0, 0, 0, 0, 0, 1, 1, 2, 0, 3, 1, 3, 1, 4, 2, 7, 0, 2, 3, 6, 1, 5, 3, 5, 1, 3, 4, 4, 2, 5, 6, 7, 7, 0, 1, 2, 3, 3, 4, 6, 2, 6, 5, 5, 3, 4, 5, 6, 7, 1, 2, 4, 6, 4, 4, 5, 7, 2, 6, 5, 7, 6, 7, 7 };
279 return DeBruijnBytePos[((U64)((val & -val) * 0x0218A392CDABBD3F)) >> 58]; 278 return DeBruijnBytePos[((U64)((val & -val) * 0x0218A392CDABBD3F)) >> 58];
280 #endif 279 #endif
281#endif 280#endif
282} 281}
@@ -293,10 +292,10 @@ static inline int LZ4_NbCommonBytes (register U32 val)
293 #elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT) 292 #elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
294 return (__builtin_clz(val) >> 3); 293 return (__builtin_clz(val) >> 3);
295 #else 294 #else
296 int r; 295 int r;
297 if (!(val>>16)) { r=2; val>>=8; } else { r=0; val>>=24; } 296 if (!(val>>16)) { r=2; val>>=8; } else { r=0; val>>=24; }
298 r += (!val); 297 r += (!val);
299 return r; 298 return r;
300 #endif 299 #endif
301#else 300#else
302 #if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT) 301 #if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
@@ -306,8 +305,8 @@ static inline int LZ4_NbCommonBytes (register U32 val)
306 #elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT) 305 #elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
307 return (__builtin_ctz(val) >> 3); 306 return (__builtin_ctz(val) >> 3);
308 #else 307 #else
309 static const int DeBruijnBytePos[32] = { 0, 0, 3, 0, 3, 1, 3, 0, 3, 2, 2, 1, 3, 2, 0, 1, 3, 3, 1, 2, 2, 2, 2, 0, 3, 1, 2, 0, 1, 0, 1, 1 }; 308 static const int DeBruijnBytePos[32] = { 0, 0, 3, 0, 3, 1, 3, 0, 3, 2, 2, 1, 3, 2, 0, 1, 3, 3, 1, 2, 2, 2, 2, 0, 3, 1, 2, 0, 1, 0, 1, 1 };
310 return DeBruijnBytePos[((U32)((val & -(S32)val) * 0x077CB531U)) >> 27]; 309 return DeBruijnBytePos[((U32)((val & -(S32)val) * 0x077CB531U)) >> 27];
311 #endif 310 #endif
312#endif 311#endif
313} 312}
@@ -327,161 +326,161 @@ static inline int LZ4_NbCommonBytes (register U32 val)
327// return : the number of bytes written in buffer 'dest', or 0 if the compression fails 326// return : the number of bytes written in buffer 'dest', or 0 if the compression fails
328 327
329static inline int LZ4_compressCtx(void** ctx, 328static inline int LZ4_compressCtx(void** ctx,
330 const char* source, 329 const char* source,
331 char* dest, 330 char* dest,
332 int isize, 331 int isize,
333 int maxOutputSize) 332 int maxOutputSize)
334{ 333{
335#if HEAPMODE 334#if HEAPMODE
336 struct refTables *srt = (struct refTables *) (*ctx); 335 struct refTables *srt = (struct refTables *) (*ctx);
337 HTYPE* HashTable; 336 HTYPE* HashTable;
338#else 337#else
339 HTYPE HashTable[HASHTABLESIZE] = {0}; 338 HTYPE HashTable[HASHTABLESIZE] = {0};
340#endif 339#endif
341 340
342 const BYTE* ip = (BYTE*) source; 341 const BYTE* ip = (BYTE*) source;
343 INITBASE(base); 342 INITBASE(base);
344 const BYTE* anchor = ip; 343 const BYTE* anchor = ip;
345 const BYTE* const iend = ip + isize; 344 const BYTE* const iend = ip + isize;
346 const BYTE* const mflimit = iend - MFLIMIT; 345 const BYTE* const mflimit = iend - MFLIMIT;
347#define matchlimit (iend - LASTLITERALS) 346#define matchlimit (iend - LASTLITERALS)
348 347
349 BYTE* op = (BYTE*) dest; 348 BYTE* op = (BYTE*) dest;
350 BYTE* const oend = op + maxOutputSize; 349 BYTE* const oend = op + maxOutputSize;
351 350
352 int len, length; 351 int len, length;
353 const int skipStrength = SKIPSTRENGTH; 352 const int skipStrength = SKIPSTRENGTH;
354 U32 forwardH; 353 U32 forwardH;
355 354
356 355
357 // Init 356 // Init
358 if (isize<MINLENGTH) goto _last_literals; 357 if (isize<MINLENGTH) goto _last_literals;
359#if HEAPMODE 358#if HEAPMODE
360 if (*ctx == NULL) 359 if (*ctx == NULL)
361 { 360 {
362 srt = (struct refTables *) malloc ( sizeof(struct refTables) ); 361 srt = (struct refTables *) malloc ( sizeof(struct refTables) );
363 *ctx = (void*) srt; 362 *ctx = (void*) srt;
364 } 363 }
365 HashTable = (HTYPE*)(srt->hashTable); 364 HashTable = (HTYPE*)(srt->hashTable);
366 memset((void*)HashTable, 0, sizeof(srt->hashTable)); 365 memset((void*)HashTable, 0, sizeof(srt->hashTable));
367#else 366#else
368 (void) ctx; 367 (void) ctx;
369#endif 368#endif
370 369
371 370
372 // First Byte 371 // First Byte
373 HashTable[LZ4_HASH_VALUE(ip)] = ip - base; 372 HashTable[LZ4_HASH_VALUE(ip)] = ip - base;
374 ip++; forwardH = LZ4_HASH_VALUE(ip); 373 ip++; forwardH = LZ4_HASH_VALUE(ip);
375 374
376 // Main Loop 375 // Main Loop
377 for ( ; ; ) 376 for ( ; ; )
378 { 377 {
379 int findMatchAttempts = (1U << skipStrength) + 3; 378 int findMatchAttempts = (1U << skipStrength) + 3;
380 const BYTE* forwardIp = ip; 379 const BYTE* forwardIp = ip;
381 const BYTE* ref; 380 const BYTE* ref;
382 BYTE* token; 381 BYTE* token;
383 382
384 // Find a match 383 // Find a match
385 do { 384 do {
386 U32 h = forwardH; 385 U32 h = forwardH;
387 int step = findMatchAttempts++ >> skipStrength; 386 int step = findMatchAttempts++ >> skipStrength;
388 ip = forwardIp; 387 ip = forwardIp;
389 forwardIp = ip + step; 388 forwardIp = ip + step;
390 389
391 if unlikely(forwardIp > mflimit) { goto _last_literals; } 390 if unlikely(forwardIp > mflimit) { goto _last_literals; }
392 391
393 forwardH = LZ4_HASH_VALUE(forwardIp); 392 forwardH = LZ4_HASH_VALUE(forwardIp);
394 ref = base + HashTable[h]; 393 ref = base + HashTable[h];
395 HashTable[h] = ip - base; 394 HashTable[h] = ip - base;
396 395
397 } while ((ref < ip - MAX_DISTANCE) || (A32(ref) != A32(ip))); 396 } while ((ref < ip - MAX_DISTANCE) || (A32(ref) != A32(ip)));
398 397
399 // Catch up 398 // Catch up
400 while ((ip>anchor) && (ref>(BYTE*)source) && unlikely(ip[-1]==ref[-1])) { ip--; ref--; } 399 while ((ip>anchor) && (ref>(BYTE*)source) && unlikely(ip[-1]==ref[-1])) { ip--; ref--; }
401 400
402 // Encode Literal length 401 // Encode Literal length
403 length = (int)(ip - anchor); 402 length = (int)(ip - anchor);
404 token = op++; 403 token = op++;
405 if unlikely(op + length + (2 + 1 + LASTLITERALS) + (length>>8) >= oend) return 0; // Check output limit 404 if unlikely(op + length + (2 + 1 + LASTLITERALS) + (length>>8) > oend) return 0; // Check output limit
406#ifdef _MSC_VER 405#ifdef _MSC_VER
407 if (length>=(int)RUN_MASK) 406 if (length>=(int)RUN_MASK)
408 { 407 {
409 int len = length-RUN_MASK; 408 int len = length-RUN_MASK;
410 *token=(RUN_MASK<<ML_BITS); 409 *token=(RUN_MASK<<ML_BITS);
411 if (len>254) 410 if (len>254)
412 { 411 {
413 do { *op++ = 255; len -= 255; } while (len>254); 412 do { *op++ = 255; len -= 255; } while (len>254);
414 *op++ = (BYTE)len; 413 *op++ = (BYTE)len;
415 memcpy(op, anchor, length); 414 memcpy(op, anchor, length);
416 op += length; 415 op += length;
417 goto _next_match; 416 goto _next_match;
418 } 417 }
419 else 418 else
420 *op++ = (BYTE)len; 419 *op++ = (BYTE)len;
421 } 420 }
422 else *token = (length<<ML_BITS); 421 else *token = (length<<ML_BITS);
423#else 422#else
424 if (length>=(int)RUN_MASK) { *token=(RUN_MASK<<ML_BITS); len = length-RUN_MASK; for(; len > 254 ; len-=255) *op++ = 255; *op++ = (BYTE)len; } 423 if (length>=(int)RUN_MASK) { *token=(RUN_MASK<<ML_BITS); len = length-RUN_MASK; for(; len > 254 ; len-=255) *op++ = 255; *op++ = (BYTE)len; }
425 else *token = (length<<ML_BITS); 424 else *token = (length<<ML_BITS);
426#endif 425#endif
427 426
428 // Copy Literals 427 // Copy Literals
429 LZ4_BLINDCOPY(anchor, op, length); 428 LZ4_BLINDCOPY(anchor, op, length);
430 429
431_next_match: 430_next_match:
432 // Encode Offset 431 // Encode Offset
433 LZ4_WRITE_LITTLEENDIAN_16(op,(U16)(ip-ref)); 432 LZ4_WRITE_LITTLEENDIAN_16(op,(U16)(ip-ref));
434 433
435 // Start Counting 434 // Start Counting
436 ip+=MINMATCH; ref+=MINMATCH; // MinMatch verified 435 ip+=MINMATCH; ref+=MINMATCH; // MinMatch verified
437 anchor = ip; 436 anchor = ip;
438 while likely(ip<matchlimit-(STEPSIZE-1)) 437 while likely(ip<matchlimit-(STEPSIZE-1))
439 { 438 {
440 UARCH diff = AARCH(ref) ^ AARCH(ip); 439 UARCH diff = AARCH(ref) ^ AARCH(ip);
441 if (!diff) { ip+=STEPSIZE; ref+=STEPSIZE; continue; } 440 if (!diff) { ip+=STEPSIZE; ref+=STEPSIZE; continue; }
442 ip += LZ4_NbCommonBytes(diff); 441 ip += LZ4_NbCommonBytes(diff);
443 goto _endCount; 442 goto _endCount;
444 } 443 }
445 if (LZ4_ARCH64) if ((ip<(matchlimit-3)) && (A32(ref) == A32(ip))) { ip+=4; ref+=4; } 444 if (LZ4_ARCH64) if ((ip<(matchlimit-3)) && (A32(ref) == A32(ip))) { ip+=4; ref+=4; }
446 if ((ip<(matchlimit-1)) && (A16(ref) == A16(ip))) { ip+=2; ref+=2; } 445 if ((ip<(matchlimit-1)) && (A16(ref) == A16(ip))) { ip+=2; ref+=2; }
447 if ((ip<matchlimit) && (*ref == *ip)) ip++; 446 if ((ip<matchlimit) && (*ref == *ip)) ip++;
448_endCount: 447_endCount:
449 448
450 // Encode MatchLength 449 // Encode MatchLength
451 len = (int)(ip - anchor); 450 len = (int)(ip - anchor);
452 if unlikely(op + (1 + LASTLITERALS) + (len>>8) >= oend) return 0; // Check output limit 451 if unlikely(op + (1 + LASTLITERALS) + (len>>8) > oend) return 0; // Check output limit
453 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; } 452 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; }
454 else *token += len; 453 else *token += len;
455 454
456 // Test end of chunk 455 // Test end of chunk
457 if (ip > mflimit) { anchor = ip; break; } 456 if (ip > mflimit) { anchor = ip; break; }
458 457
459 // Fill table 458 // Fill table
460 HashTable[LZ4_HASH_VALUE(ip-2)] = ip - 2 - base; 459 HashTable[LZ4_HASH_VALUE(ip-2)] = ip - 2 - base;
461 460
462 // Test next position 461 // Test next position
463 ref = base + HashTable[LZ4_HASH_VALUE(ip)]; 462 ref = base + HashTable[LZ4_HASH_VALUE(ip)];
464 HashTable[LZ4_HASH_VALUE(ip)] = ip - base; 463 HashTable[LZ4_HASH_VALUE(ip)] = ip - base;
465 if ((ref > ip - (MAX_DISTANCE + 1)) && (A32(ref) == A32(ip))) { token = op++; *token=0; goto _next_match; } 464 if ((ref > ip - (MAX_DISTANCE + 1)) && (A32(ref) == A32(ip))) { token = op++; *token=0; goto _next_match; }
466 465
467 // Prepare next loop 466 // Prepare next loop
468 anchor = ip++; 467 anchor = ip++;
469 forwardH = LZ4_HASH_VALUE(ip); 468 forwardH = LZ4_HASH_VALUE(ip);
470 } 469 }
471 470
472_last_literals: 471_last_literals:
473 // Encode Last Literals 472 // Encode Last Literals
474 { 473 {
475 int lastRun = (int)(iend - anchor); 474 int lastRun = (int)(iend - anchor);
476 if (((char*)op - dest) + lastRun + 1 + ((lastRun-15)/255) >= maxOutputSize) return 0; 475 if (((char*)op - dest) + lastRun + 1 + ((lastRun+255-RUN_MASK)/255) > (U32)maxOutputSize) return 0;
477 if (lastRun>=(int)RUN_MASK) { *op++=(RUN_MASK<<ML_BITS); lastRun-=RUN_MASK; for(; lastRun > 254 ; lastRun-=255) *op++ = 255; *op++ = (BYTE) lastRun; } 476 if (lastRun>=(int)RUN_MASK) { *op++=(RUN_MASK<<ML_BITS); lastRun-=RUN_MASK; for(; lastRun > 254 ; lastRun-=255) *op++ = 255; *op++ = (BYTE) lastRun; }
478 else *op++ = (lastRun<<ML_BITS); 477 else *op++ = (lastRun<<ML_BITS);
479 memcpy(op, anchor, iend - anchor); 478 memcpy(op, anchor, iend - anchor);
480 op += iend-anchor; 479 op += iend-anchor;
481 } 480 }
482 481
483 // End 482 // End
484 return (int) (((char*)op)-dest); 483 return (int) (((char*)op)-dest);
485} 484}
486 485
487 486
@@ -493,188 +492,188 @@ _last_literals:
493#define LZ4_HASH64K_FUNCTION(i) (((i) * 2654435761U) >> ((MINMATCH*8)-HASHLOG64K)) 492#define LZ4_HASH64K_FUNCTION(i) (((i) * 2654435761U) >> ((MINMATCH*8)-HASHLOG64K))
494#define LZ4_HASH64K_VALUE(p) LZ4_HASH64K_FUNCTION(A32(p)) 493#define LZ4_HASH64K_VALUE(p) LZ4_HASH64K_FUNCTION(A32(p))
495static inline int LZ4_compress64kCtx(void** ctx, 494static inline int LZ4_compress64kCtx(void** ctx,
496 const char* source, 495 const char* source,
497 char* dest, 496 char* dest,
498 int isize, 497 int isize,
499 int maxOutputSize) 498 int maxOutputSize)
500{ 499{
501#if HEAPMODE 500#if HEAPMODE
502 struct refTables *srt = (struct refTables *) (*ctx); 501 struct refTables *srt = (struct refTables *) (*ctx);
503 U16* HashTable; 502 U16* HashTable;
504#else 503#else
505 U16 HashTable[HASH64KTABLESIZE] = {0}; 504 U16 HashTable[HASH64KTABLESIZE] = {0};
506#endif 505#endif
507 506
508 const BYTE* ip = (BYTE*) source; 507 const BYTE* ip = (BYTE*) source;
509 const BYTE* anchor = ip; 508 const BYTE* anchor = ip;
510 const BYTE* const base = ip; 509 const BYTE* const base = ip;
511 const BYTE* const iend = ip + isize; 510 const BYTE* const iend = ip + isize;
512 const BYTE* const mflimit = iend - MFLIMIT; 511 const BYTE* const mflimit = iend - MFLIMIT;
513#define matchlimit (iend - LASTLITERALS) 512#define matchlimit (iend - LASTLITERALS)
514 513
515 BYTE* op = (BYTE*) dest; 514 BYTE* op = (BYTE*) dest;
516 BYTE* const oend = op + maxOutputSize; 515 BYTE* const oend = op + maxOutputSize;
517 516
518 int len, length; 517 int len, length;
519 const int skipStrength = SKIPSTRENGTH; 518 const int skipStrength = SKIPSTRENGTH;
520 U32 forwardH; 519 U32 forwardH;
521 520
522 521
523 // Init 522 // Init
524 if (isize<MINLENGTH) goto _last_literals; 523 if (isize<MINLENGTH) goto _last_literals;
525#if HEAPMODE 524#if HEAPMODE
526 if (*ctx == NULL) 525 if (*ctx == NULL)
527 { 526 {
528 srt = (struct refTables *) malloc ( sizeof(struct refTables) ); 527 srt = (struct refTables *) malloc ( sizeof(struct refTables) );
529 *ctx = (void*) srt; 528 *ctx = (void*) srt;
530 } 529 }
531 HashTable = (U16*)(srt->hashTable); 530 HashTable = (U16*)(srt->hashTable);
532 memset((void*)HashTable, 0, sizeof(srt->hashTable)); 531 memset((void*)HashTable, 0, sizeof(srt->hashTable));
533#else 532#else
534 (void) ctx; 533 (void) ctx;
535#endif 534#endif
536 535
537 536
538 // First Byte 537 // First Byte
539 ip++; forwardH = LZ4_HASH64K_VALUE(ip); 538 ip++; forwardH = LZ4_HASH64K_VALUE(ip);
540 539
541 // Main Loop 540 // Main Loop
542 for ( ; ; ) 541 for ( ; ; )
543 { 542 {
544 int findMatchAttempts = (1U << skipStrength) + 3; 543 int findMatchAttempts = (1U << skipStrength) + 3;
545 const BYTE* forwardIp = ip; 544 const BYTE* forwardIp = ip;
546 const BYTE* ref; 545 const BYTE* ref;
547 BYTE* token; 546 BYTE* token;
548 547
549 // Find a match 548 // Find a match
550 do { 549 do {
551 U32 h = forwardH; 550 U32 h = forwardH;
552 int step = findMatchAttempts++ >> skipStrength; 551 int step = findMatchAttempts++ >> skipStrength;
553 ip = forwardIp; 552 ip = forwardIp;
554 forwardIp = ip + step; 553 forwardIp = ip + step;
555 554
556 if (forwardIp > mflimit) { goto _last_literals; } 555 if (forwardIp > mflimit) { goto _last_literals; }
557 556
558 forwardH = LZ4_HASH64K_VALUE(forwardIp); 557 forwardH = LZ4_HASH64K_VALUE(forwardIp);
559 ref = base + HashTable[h]; 558 ref = base + HashTable[h];
560 HashTable[h] = (U16)(ip - base); 559 HashTable[h] = (U16)(ip - base);
561 560
562 } while (A32(ref) != A32(ip)); 561 } while (A32(ref) != A32(ip));
563 562
564 // Catch up 563 // Catch up
565 while ((ip>anchor) && (ref>(BYTE*)source) && (ip[-1]==ref[-1])) { ip--; ref--; } 564 while ((ip>anchor) && (ref>(BYTE*)source) && (ip[-1]==ref[-1])) { ip--; ref--; }
566 565
567 // Encode Literal length 566 // Encode Literal length
568 length = (int)(ip - anchor); 567 length = (int)(ip - anchor);
569 token = op++; 568 token = op++;
570 if unlikely(op + length + (2 + 1 + LASTLITERALS) + (length>>8) >= oend) return 0; // Check output limit 569 if unlikely(op + length + (2 + 1 + LASTLITERALS) + (length>>8) > oend) return 0; // Check output limit
571#ifdef _MSC_VER 570#ifdef _MSC_VER
572 if (length>=(int)RUN_MASK) 571 if (length>=(int)RUN_MASK)
573 { 572 {
574 int len = length-RUN_MASK; 573 int len = length-RUN_MASK;
575 *token=(RUN_MASK<<ML_BITS); 574 *token=(RUN_MASK<<ML_BITS);
576 if (len>254) 575 if (len>254)
577 { 576 {
578 do { *op++ = 255; len -= 255; } while (len>254); 577 do { *op++ = 255; len -= 255; } while (len>254);
579 *op++ = (BYTE)len; 578 *op++ = (BYTE)len;
580 memcpy(op, anchor, length); 579 memcpy(op, anchor, length);
581 op += length; 580 op += length;
582 goto _next_match; 581 goto _next_match;
583 } 582 }
584 else 583 else
585 *op++ = (BYTE)len; 584 *op++ = (BYTE)len;
586 } 585 }
587 else *token = (length<<ML_BITS); 586 else *token = (length<<ML_BITS);
588#else 587#else
589 if (length>=(int)RUN_MASK) { *token=(RUN_MASK<<ML_BITS); len = length-RUN_MASK; for(; len > 254 ; len-=255) *op++ = 255; *op++ = (BYTE)len; } 588 if (length>=(int)RUN_MASK) { *token=(RUN_MASK<<ML_BITS); len = length-RUN_MASK; for(; len > 254 ; len-=255) *op++ = 255; *op++ = (BYTE)len; }
590 else *token = (length<<ML_BITS); 589 else *token = (length<<ML_BITS);
591#endif 590#endif
592 591
593 // Copy Literals 592 // Copy Literals
594 LZ4_BLINDCOPY(anchor, op, length); 593 LZ4_BLINDCOPY(anchor, op, length);
595 594
596_next_match: 595_next_match:
597 // Encode Offset 596 // Encode Offset
598 LZ4_WRITE_LITTLEENDIAN_16(op,(U16)(ip-ref)); 597 LZ4_WRITE_LITTLEENDIAN_16(op,(U16)(ip-ref));
599 598
600 // Start Counting 599 // Start Counting
601 ip+=MINMATCH; ref+=MINMATCH; // MinMatch verified 600 ip+=MINMATCH; ref+=MINMATCH; // MinMatch verified
602 anchor = ip; 601 anchor = ip;
603 while (ip<matchlimit-(STEPSIZE-1)) 602 while (ip<matchlimit-(STEPSIZE-1))
604 { 603 {
605 UARCH diff = AARCH(ref) ^ AARCH(ip); 604 UARCH diff = AARCH(ref) ^ AARCH(ip);
606 if (!diff) { ip+=STEPSIZE; ref+=STEPSIZE; continue; } 605 if (!diff) { ip+=STEPSIZE; ref+=STEPSIZE; continue; }
607 ip += LZ4_NbCommonBytes(diff); 606 ip += LZ4_NbCommonBytes(diff);
608 goto _endCount; 607 goto _endCount;
609 } 608 }
610 if (LZ4_ARCH64) if ((ip<(matchlimit-3)) && (A32(ref) == A32(ip))) { ip+=4; ref+=4; } 609 if (LZ4_ARCH64) if ((ip<(matchlimit-3)) && (A32(ref) == A32(ip))) { ip+=4; ref+=4; }
611 if ((ip<(matchlimit-1)) && (A16(ref) == A16(ip))) { ip+=2; ref+=2; } 610 if ((ip<(matchlimit-1)) && (A16(ref) == A16(ip))) { ip+=2; ref+=2; }
612 if ((ip<matchlimit) && (*ref == *ip)) ip++; 611 if ((ip<matchlimit) && (*ref == *ip)) ip++;
613_endCount: 612_endCount:
614 613
615 // Encode MatchLength 614 // Encode MatchLength
616 len = (int)(ip - anchor); 615 len = (int)(ip - anchor);
617 if unlikely(op + (1 + LASTLITERALS) + (len>>8) >= oend) return 0; // Check output limit 616 if unlikely(op + (1 + LASTLITERALS) + (len>>8) > oend) return 0; // Check output limit
618 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; } 617 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; }
619 else *token += len; 618 else *token += len;
620 619
621 // Test end of chunk 620 // Test end of chunk
622 if (ip > mflimit) { anchor = ip; break; } 621 if (ip > mflimit) { anchor = ip; break; }
623 622
624 // Fill table 623 // Fill table
625 HashTable[LZ4_HASH64K_VALUE(ip-2)] = (U16)(ip - 2 - base); 624 HashTable[LZ4_HASH64K_VALUE(ip-2)] = (U16)(ip - 2 - base);
626 625
627 // Test next position 626 // Test next position
628 ref = base + HashTable[LZ4_HASH64K_VALUE(ip)]; 627 ref = base + HashTable[LZ4_HASH64K_VALUE(ip)];
629 HashTable[LZ4_HASH64K_VALUE(ip)] = (U16)(ip - base); 628 HashTable[LZ4_HASH64K_VALUE(ip)] = (U16)(ip - base);
630 if (A32(ref) == A32(ip)) { token = op++; *token=0; goto _next_match; } 629 if (A32(ref) == A32(ip)) { token = op++; *token=0; goto _next_match; }
631 630
632 // Prepare next loop 631 // Prepare next loop
633 anchor = ip++; 632 anchor = ip++;
634 forwardH = LZ4_HASH64K_VALUE(ip); 633 forwardH = LZ4_HASH64K_VALUE(ip);
635 } 634 }
636 635
637_last_literals: 636_last_literals:
638 // Encode Last Literals 637 // Encode Last Literals
639 { 638 {
640 int lastRun = (int)(iend - anchor); 639 int lastRun = (int)(iend - anchor);
641 if (((char*)op - dest) + lastRun + 1 + ((lastRun)>>8) >= maxOutputSize) return 0; 640 if (op + lastRun + 1 + (lastRun-RUN_MASK+255)/255 > oend) return 0;
642 if (lastRun>=(int)RUN_MASK) { *op++=(RUN_MASK<<ML_BITS); lastRun-=RUN_MASK; for(; lastRun > 254 ; lastRun-=255) *op++ = 255; *op++ = (BYTE) lastRun; } 641 if (lastRun>=(int)RUN_MASK) { *op++=(RUN_MASK<<ML_BITS); lastRun-=RUN_MASK; for(; lastRun > 254 ; lastRun-=255) *op++ = 255; *op++ = (BYTE) lastRun; }
643 else *op++ = (lastRun<<ML_BITS); 642 else *op++ = (lastRun<<ML_BITS);
644 memcpy(op, anchor, iend - anchor); 643 memcpy(op, anchor, iend - anchor);
645 op += iend-anchor; 644 op += iend-anchor;
646 } 645 }
647 646
648 // End 647 // End
649 return (int) (((char*)op)-dest); 648 return (int) (((char*)op)-dest);
650} 649}
651 650
652 651
653int LZ4_compress_limitedOutput(const char* source, 652int LZ4_compress_limitedOutput(const char* source,
654 char* dest, 653 char* dest,
655 int isize, 654 int isize,
656 int maxOutputSize) 655 int maxOutputSize)
657{ 656{
658#if HEAPMODE 657#if HEAPMODE
659 void* ctx = malloc(sizeof(struct refTables)); 658 void* ctx = malloc(sizeof(struct refTables));
660 int result; 659 int result;
661 if (isize < LZ4_64KLIMIT) 660 if (isize < LZ4_64KLIMIT)
662 result = LZ4_compress64kCtx(&ctx, source, dest, isize, maxOutputSize); 661 result = LZ4_compress64kCtx(&ctx, source, dest, isize, maxOutputSize);
663 else result = LZ4_compressCtx(&ctx, source, dest, isize, maxOutputSize); 662 else result = LZ4_compressCtx(&ctx, source, dest, isize, maxOutputSize);
664 free(ctx); 663 free(ctx);
665 return result; 664 return result;
666#else 665#else
667 if (isize < (int)LZ4_64KLIMIT) return LZ4_compress64kCtx(NULL, source, dest, isize, maxOutputSize); 666 if (isize < (int)LZ4_64KLIMIT) return LZ4_compress64kCtx(NULL, source, dest, isize, maxOutputSize);
668 return LZ4_compressCtx(NULL, source, dest, isize, maxOutputSize); 667 return LZ4_compressCtx(NULL, source, dest, isize, maxOutputSize);
669#endif 668#endif
670} 669}
671 670
672 671
673int LZ4_compress(const char* source, 672int LZ4_compress(const char* source,
674 char* dest, 673 char* dest,
675 int isize) 674 int isize)
676{ 675{
677 return LZ4_compress_limitedOutput(source, dest, isize, LZ4_compressBound(isize)); 676 return LZ4_compress_limitedOutput(source, dest, isize, LZ4_compressBound(isize));
678} 677}
679 678
680 679
@@ -691,173 +690,172 @@ int LZ4_compress(const char* source,
691// A corrupted input will produce an error result, a negative int, indicating the position of the error within input stream. 690// A corrupted input will produce an error result, a negative int, indicating the position of the error within input stream.
692 691
693int LZ4_uncompress(const char* source, 692int LZ4_uncompress(const char* source,
694 char* dest, 693 char* dest,
695 int osize) 694 int osize)
696{ 695{
697 // Local Variables 696 // Local Variables
698 const BYTE* restrict ip = (const BYTE*) source; 697 const BYTE* restrict ip = (const BYTE*) source;
699 const BYTE* restrict ref; 698 const BYTE* ref;
700 699
701 BYTE* restrict op = (BYTE*) dest; 700 BYTE* op = (BYTE*) dest;
702 BYTE* const oend = op + osize; 701 BYTE* const oend = op + osize;
703 BYTE* cpy; 702 BYTE* cpy;
704 703
705 BYTE token; 704 BYTE token;
706 705
707 int len, length; 706 int len, length;
708 size_t dec[] ={0, 3, 2, 3, 0, 0, 0, 0}; 707 size_t dec[] ={0, 3, 2, 3, 0, 0, 0, 0};
709 708
710 709
711 // Main Loop 710 // Main Loop
712 while (1) 711 while (1)
713 { 712 {
714 // get runlength 713 // get runlength
715 token = *ip++; 714 token = *ip++;
716 if ((length=(token>>ML_BITS)) == RUN_MASK) { for (;(len=*ip++)==255;length+=255){} length += len; } 715 if ((length=(token>>ML_BITS)) == RUN_MASK) { for (;(len=*ip++)==255;length+=255){} length += len; }
717 716
718 // copy literals 717 // copy literals
719 cpy = op+length; 718 cpy = op+length;
720 if unlikely(cpy>oend-COPYLENGTH) 719 if unlikely(cpy>oend-COPYLENGTH)
721 { 720 {
722 if (cpy > oend) goto _output_error; // Error : request to write beyond destination buffer 721 if (cpy != oend) goto _output_error; // Error : we must necessarily stand at EOF
723 memcpy(op, ip, length); 722 memcpy(op, ip, length);
724 ip += length; 723 ip += length;
725 break; // Necessarily EOF 724 break; // EOF
726 } 725 }
727 LZ4_WILDCOPY(ip, op, cpy); ip -= (op-cpy); op = cpy; 726 LZ4_WILDCOPY(ip, op, cpy); ip -= (op-cpy); op = cpy;
728 727
729 // get offset 728 // get offset
730 LZ4_READ_LITTLEENDIAN_16(ref,cpy,ip); ip+=2; 729 LZ4_READ_LITTLEENDIAN_16(ref,cpy,ip); ip+=2;
731 if (ref < (BYTE* const)dest) goto _output_error; // Error : offset create reference outside destination buffer 730 if (ref < (BYTE* const)dest) goto _output_error; // Error : offset create reference outside destination buffer
732 731
733 // get matchlength 732 // get matchlength
734 if ((length=(token&ML_MASK)) == ML_MASK) { for (;*ip==255;length+=255) {ip++;} length += *ip++; } 733 if ((length=(token&ML_MASK)) == ML_MASK) { for (;*ip==255;length+=255) {ip++;} length += *ip++; }
735 734
736 // copy repeated sequence 735 // copy repeated sequence
737 if unlikely(op-ref<STEPSIZE) 736 if unlikely(op-ref<STEPSIZE)
738 { 737 {
739#if LZ4_ARCH64 738#if LZ4_ARCH64
740 size_t dec2table[]={0, 0, 0, -1, 0, 1, 2, 3}; 739 size_t dec2table[]={0, 0, 0, -1, 0, 1, 2, 3};
741 size_t dec2 = dec2table[op-ref]; 740 size_t dec2 = dec2table[op-ref];
742#else 741#else
743 const int dec2 = 0; 742 const int dec2 = 0;
744#endif 743#endif
745 *op++ = *ref++; 744 *op++ = *ref++;
746 *op++ = *ref++; 745 *op++ = *ref++;
747 *op++ = *ref++; 746 *op++ = *ref++;
748 *op++ = *ref++; 747 *op++ = *ref++;
749 ref -= dec[op-ref]; 748 ref -= dec[op-ref];
750 A32(op)=A32(ref); op += STEPSIZE-4; 749 A32(op)=A32(ref); op += STEPSIZE-4;
751 ref -= dec2; 750 ref -= dec2;
752 } else { LZ4_COPYSTEP(ref,op); } 751 } else { LZ4_COPYSTEP(ref,op); }
753 cpy = op + length - (STEPSIZE-4); 752 cpy = op + length - (STEPSIZE-4);
754 if (cpy>oend-COPYLENGTH) 753 if (cpy>oend-COPYLENGTH)
755 { 754 {
756 if (cpy > oend) goto _output_error; // Error : request to write beyond destination buffer 755 if (cpy > oend) goto _output_error; // Error : request to write beyond destination buffer
757 LZ4_SECURECOPY(ref, op, (oend-COPYLENGTH)); 756 LZ4_SECURECOPY(ref, op, (oend-COPYLENGTH));
758 while(op<cpy) *op++=*ref++; 757 while(op<cpy) *op++=*ref++;
759 op=cpy; 758 op=cpy;
760 if (op == oend) break; // Check EOF (should never happen, since last 5 bytes are supposed to be literals) 759 if (op == oend) goto _output_error; // Check EOF (should never happen, since last 5 bytes are supposed to be literals)
761 continue; 760 continue;
762 } 761 }
763 LZ4_SECURECOPY(ref, op, cpy); 762 LZ4_SECURECOPY(ref, op, cpy);
764 op=cpy; // correction 763 op=cpy; // correction
765 } 764 }
766 765
767 // end of decoding 766 // end of decoding
768 return (int) (((char*)ip)-source); 767 return (int) (((char*)ip)-source);
769 768
770 // write overflow error detected 769 // write overflow error detected
771_output_error: 770_output_error:
772 return (int) (-(((char*)ip)-source)); 771 return (int) (-(((char*)ip)-source));
773} 772}
774 773
775 774
776int LZ4_uncompress_unknownOutputSize( 775int LZ4_uncompress_unknownOutputSize(
777 const char* source, 776 const char* source,
778 char* dest, 777 char* dest,
779 int isize, 778 int isize,
780 int maxOutputSize) 779 int maxOutputSize)
781{ 780{
782 // Local Variables 781 // Local Variables
783 const BYTE* restrict ip = (const BYTE*) source; 782 const BYTE* restrict ip = (const BYTE*) source;
784 const BYTE* const iend = ip + isize; 783 const BYTE* const iend = ip + isize;
785 const BYTE* restrict ref; 784 const BYTE* ref;
786 785
787 BYTE* restrict op = (BYTE*) dest; 786 BYTE* op = (BYTE*) dest;
788 BYTE* const oend = op + maxOutputSize; 787 BYTE* const oend = op + maxOutputSize;
789 BYTE* cpy; 788 BYTE* cpy;
790 789
791 size_t dec[] ={0, 3, 2, 3, 0, 0, 0, 0}; 790 size_t dec[] ={0, 3, 2, 3, 0, 0, 0, 0};
792 791
793 792
794 // Main Loop 793 // Main Loop
795 while (ip<iend) 794 while (ip<iend)
796 { 795 {
797 BYTE token; 796 BYTE token;
798 int length; 797 int length;
799 798
800 // get runlength 799 // get runlength
801 token = *ip++; 800 token = *ip++;
802 if ((length=(token>>ML_BITS)) == RUN_MASK) { int s=255; while ((ip<iend) && (s==255)) { s=*ip++; length += s; } } 801 if ((length=(token>>ML_BITS)) == RUN_MASK) { int s=255; while ((ip<iend) && (s==255)) { s=*ip++; length += s; } }
803 802
804 // copy literals 803 // copy literals
805 cpy = op+length; 804 cpy = op+length;
806 if ((cpy>oend-COPYLENGTH) || (ip+length>iend-COPYLENGTH)) 805 if ((cpy>oend-COPYLENGTH) || (ip+length>iend-COPYLENGTH))
807 { 806 {
808 if (cpy > oend) goto _output_error; // Error : request to write beyond destination buffer 807 if (cpy > oend) goto _output_error; // Error : writes beyond output buffer
809 if (ip+length > iend) goto _output_error; // Error : request to read beyond source buffer 808 if (ip+length != iend) goto _output_error; // Error : LZ4 format requires to consume all input at this stage
810 memcpy(op, ip, length); 809 memcpy(op, ip, length);
811 op += length; 810 op += length;
812 ip += length; 811 ip = iend;
813 if (ip<iend) goto _output_error; // Error : LZ4 format violation 812 break; // Necessarily EOF, due to parsing restrictions
814 break; // Necessarily EOF, due to parsing restrictions 813 }
815 } 814 LZ4_WILDCOPY(ip, op, cpy); ip -= (op-cpy); op = cpy;
816 LZ4_WILDCOPY(ip, op, cpy); ip -= (op-cpy); op = cpy; 815
817 816 // get offset
818 // get offset 817 LZ4_READ_LITTLEENDIAN_16(ref,cpy,ip); ip+=2;
819 LZ4_READ_LITTLEENDIAN_16(ref,cpy,ip); ip+=2; 818 if (ref < (BYTE* const)dest) goto _output_error; // Error : offset creates reference outside of destination buffer
820 if (ref < (BYTE* const)dest) goto _output_error; // Error : offset creates reference outside of destination buffer 819
821 820 // get matchlength
822 // get matchlength 821 if ((length=(token&ML_MASK)) == ML_MASK) { while (ip<iend) { int s = *ip++; length +=s; if (s==255) continue; break; } }
823 if ((length=(token&ML_MASK)) == ML_MASK) { while (ip<iend) { int s = *ip++; length +=s; if (s==255) continue; break; } } 822
824 823 // copy repeated sequence
825 // copy repeated sequence 824 if unlikely(op-ref<STEPSIZE)
826 if unlikely(op-ref<STEPSIZE) 825 {
827 {
828#if LZ4_ARCH64 826#if LZ4_ARCH64
829 size_t dec2table[]={0, 0, 0, -1, 0, 1, 2, 3}; 827 size_t dec2table[]={0, 0, 0, -1, 0, 1, 2, 3};
830 size_t dec2 = dec2table[op-ref]; 828 size_t dec2 = dec2table[op-ref];
831#else 829#else
832 const int dec2 = 0; 830 const int dec2 = 0;
833#endif 831#endif
834 *op++ = *ref++; 832 *op++ = *ref++;
835 *op++ = *ref++; 833 *op++ = *ref++;
836 *op++ = *ref++; 834 *op++ = *ref++;
837 *op++ = *ref++; 835 *op++ = *ref++;
838 ref -= dec[op-ref]; 836 ref -= dec[op-ref];
839 A32(op)=A32(ref); op += STEPSIZE-4; 837 A32(op)=A32(ref); op += STEPSIZE-4;
840 ref -= dec2; 838 ref -= dec2;
841 } else { LZ4_COPYSTEP(ref,op); } 839 } else { LZ4_COPYSTEP(ref,op); }
842 cpy = op + length - (STEPSIZE-4); 840 cpy = op + length - (STEPSIZE-4);
843 if (cpy>oend-COPYLENGTH) 841 if (cpy>oend-COPYLENGTH)
844 { 842 {
845 if (cpy > oend) goto _output_error; // Error : request to write outside of destination buffer 843 if (cpy > oend) goto _output_error; // Error : request to write outside of destination buffer
846 LZ4_SECURECOPY(ref, op, (oend-COPYLENGTH)); 844 LZ4_SECURECOPY(ref, op, (oend-COPYLENGTH));
847 while(op<cpy) *op++=*ref++; 845 while(op<cpy) *op++=*ref++;
848 op=cpy; 846 op=cpy;
849 if (op == oend) break; // Check EOF (should never happen, since last 5 bytes are supposed to be literals) 847 if (op == oend) goto _output_error; // Check EOF (should never happen, since last 5 bytes are supposed to be literals)
850 continue; 848 continue;
851 } 849 }
852 LZ4_SECURECOPY(ref, op, cpy); 850 LZ4_SECURECOPY(ref, op, cpy);
853 op=cpy; // correction 851 op=cpy; // correction
854 } 852 }
855 853
856 // end of decoding 854 // end of decoding
857 return (int) (((char*)op)-dest); 855 return (int) (((char*)op)-dest);
858 856
859 // write overflow error detected 857 // write overflow error detected
860_output_error: 858_output_error:
861 return (int) (-(((char*)ip)-source)); 859 return (int) (-(((char*)ip)-source));
862} 860}
863 861
diff --git a/src/static_libs/lz4/lz4.h b/src/static_libs/lz4/lz4.h
index e3df7bd..3680121 100644
--- a/src/static_libs/lz4/lz4.h
+++ b/src/static_libs/lz4/lz4.h
@@ -38,6 +38,14 @@ extern "C" {
38#endif 38#endif
39 39
40 40
41//**************************************
42// Compiler Options
43//**************************************
44#ifdef _MSC_VER // Visual Studio
45# define inline __inline // Visual is not C99, but supports some kind of inline
46#endif
47
48
41//**************************** 49//****************************
42// Simple Functions 50// Simple Functions
43//**************************** 51//****************************
@@ -47,22 +55,22 @@ int LZ4_uncompress (const char* source, char* dest, int osize);
47 55
48/* 56/*
49LZ4_compress() : 57LZ4_compress() :
50 Compresses 'isize' bytes from 'source' into 'dest'. 58 Compresses 'isize' bytes from 'source' into 'dest'.
51 Destination buffer must be already allocated, 59 Destination buffer must be already allocated,
52 and must be sized to handle worst cases situations (input data not compressible) 60 and must be sized to handle worst cases situations (input data not compressible)
53 Worst case size evaluation is provided by macro LZ4_compressBound() 61 Worst case size evaluation is provided by function LZ4_compressBound()
54 62
55 isize : is the input size. Max supported value is ~1.9GB 63 isize : is the input size. Max supported value is ~1.9GB
56 return : the number of bytes written in buffer dest 64 return : the number of bytes written in buffer dest
57 65
58 66
59LZ4_uncompress() : 67LZ4_uncompress() :
60 osize : is the output size, therefore the original size 68 osize : is the output size, therefore the original size
61 return : the number of bytes read in the source buffer 69 return : the number of bytes read in the source buffer
62 If the source stream is malformed, the function will stop decoding and return a negative result, indicating the byte position of the faulty instruction 70 If the source stream is malformed, the function will stop decoding and return a negative result, indicating the byte position of the faulty instruction
63 This function never writes beyond dest + osize, and is therefore protected against malicious data packets 71 This function never writes outside of provided buffers, and never modifies input buffer.
64 note : destination buffer must be already allocated. 72 note : destination buffer must be already allocated.
65 its size must be a minimum of 'osize' bytes. 73 its size must be a minimum of 'osize' bytes.
66*/ 74*/
67 75
68 76
@@ -70,16 +78,19 @@ LZ4_uncompress() :
70// Advanced Functions 78// Advanced Functions
71//**************************** 79//****************************
72 80
73#define LZ4_compressBound(isize) (isize + (isize/255) + 16) 81static inline int LZ4_compressBound(int isize) { return ((isize) + ((isize)/255) + 16); }
82#define LZ4_COMPRESSBOUND( isize) ((isize) + ((isize)/255) + 16)
74 83
75/* 84/*
76LZ4_compressBound() : 85LZ4_compressBound() :
77 Provides the maximum size that LZ4 may output in a "worst case" scenario (input data not compressible) 86 Provides the maximum size that LZ4 may output in a "worst case" scenario (input data not compressible)
78 primarily useful for memory allocation of output buffer. 87 primarily useful for memory allocation of output buffer.
79 88 inline function is recommended for the general case,
80 isize : is the input size. Max supported value is ~1.9GB 89 but macro is also provided when results need to be evaluated at compile time (such as table size allocation).
81 return : maximum output size in a "worst case" scenario 90
82 note : this function is limited by "int" range (2^31-1) 91 isize : is the input size. Max supported value is ~1.9GB
92 return : maximum output size in a "worst case" scenario
93 note : this function is limited by "int" range (2^31-1)
83*/ 94*/
84 95
85 96
@@ -88,13 +99,13 @@ int LZ4_compress_limitedOutput (const char* source, char* dest, int isize, int
88/* 99/*
89LZ4_compress_limitedOutput() : 100LZ4_compress_limitedOutput() :
90 Compress 'isize' bytes from 'source' into an output buffer 'dest' of maximum size 'maxOutputSize'. 101 Compress 'isize' bytes from 'source' into an output buffer 'dest' of maximum size 'maxOutputSize'.
91 If it cannot achieve it, compression will stop, and result of the function will be zero. 102 If it cannot achieve it, compression will stop, and result of the function will be zero.
92 This function never writes outside of provided output buffer. 103 This function never writes outside of provided output buffer.
93 104
94 isize : is the input size. Max supported value is ~1.9GB 105 isize : is the input size. Max supported value is ~1.9GB
95 maxOutputSize : is the size of the destination buffer (which must be already allocated) 106 maxOutputSize : is the size of the destination buffer (which must be already allocated)
96 return : the number of bytes written in buffer 'dest' 107 return : the number of bytes written in buffer 'dest'
97 or 0 if the compression fails 108 or 0 if the compression fails
98*/ 109*/
99 110
100 111
@@ -102,13 +113,13 @@ int LZ4_uncompress_unknownOutputSize (const char* source, char* dest, int isize,
102 113
103/* 114/*
104LZ4_uncompress_unknownOutputSize() : 115LZ4_uncompress_unknownOutputSize() :
105 isize : is the input size, therefore the compressed size 116 isize : is the input size, therefore the compressed size
106 maxOutputSize : is the size of the destination buffer (which must be already allocated) 117 maxOutputSize : is the size of the destination buffer (which must be already allocated)
107 return : the number of bytes decoded in the destination buffer (necessarily <= maxOutputSize) 118 return : the number of bytes decoded in the destination buffer (necessarily <= maxOutputSize)
108 If the source stream is malformed, the function will stop decoding and return a negative result, indicating the byte position of the faulty instruction 119 If the source stream is malformed, the function will stop decoding and return a negative result, indicating the byte position of the faulty instruction
109 This function never writes beyond dest + maxOutputSize, and is therefore protected against malicious data packets 120 This function never writes beyond dest + maxOutputSize, and is therefore protected against malicious data packets
110 note : Destination buffer must be already allocated. 121 note : Destination buffer must be already allocated.
111 This version is slightly slower than LZ4_uncompress() 122 This version is slightly slower than LZ4_uncompress()
112*/ 123*/
113 124
114 125
diff --git a/src/static_libs/lz4/lz4hc.c b/src/static_libs/lz4/lz4hc.c
index 2ab507e..7cd8bb4 100644
--- a/src/static_libs/lz4/lz4hc.c
+++ b/src/static_libs/lz4/lz4hc.c
@@ -337,7 +337,7 @@ inline static int LZ4HC_InsertAndFindBestMatch (LZ4HC_Data_Structure* hc4, const
337 // HC4 match finder 337 // HC4 match finder
338 LZ4HC_Insert(hc4, ip); 338 LZ4HC_Insert(hc4, ip);
339 ref = HASH_POINTER(ip); 339 ref = HASH_POINTER(ip);
340 while ((ref > (ip-MAX_DISTANCE)) && (nbAttempts)) 340 while ((ref >= (ip-MAX_DISTANCE)) && (nbAttempts))
341 { 341 {
342 nbAttempts--; 342 nbAttempts--;
343 if (*(ref+ml) == *(ip+ml)) 343 if (*(ref+ml) == *(ip+ml))
@@ -380,7 +380,7 @@ inline static int LZ4HC_InsertAndGetWiderMatch (LZ4HC_Data_Structure* hc4, const
380 LZ4HC_Insert(hc4, ip); 380 LZ4HC_Insert(hc4, ip);
381 ref = HASH_POINTER(ip); 381 ref = HASH_POINTER(ip);
382 382
383 while ((ref > ip-MAX_DISTANCE) && (ref >= hc4->base) && (nbAttempts)) 383 while ((ref >= ip-MAX_DISTANCE) && (ref >= hc4->base) && (nbAttempts))
384 { 384 {
385 nbAttempts--; 385 nbAttempts--;
386 if (*(startLimit + longest) == *(ref - delta + longest)) 386 if (*(startLimit + longest) == *(ref - delta + longest))