Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
lz4.c
Go to the documentation of this file.
1 /*
2  LZ4 - Fast LZ compression algorithm
3  Copyright (C) 2011-2013, Yann Collet.
4  BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
5 
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions are
8  met:
9 
10  * Redistributions of source code must retain the above copyright
11  notice, this list of conditions and the following disclaimer.
12  * Redistributions in binary form must reproduce the above
13  copyright notice, this list of conditions and the following disclaimer
14  in the documentation and/or other materials provided with the
15  distribution.
16 
17  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29  You can contact the author at :
30  - LZ4 homepage : http://fastcompression.blogspot.com/p/lz4.html
31  - LZ4 source repository : http://code.google.com/p/lz4/
32 */
33 
34 #ifndef LZ4_CS_ADAPTER
35 //**************************************
36 // Tuning parameters
37 //**************************************
38 // MEMORY_USAGE :
39 // Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.)
40 // Increasing memory usage improves compression ratio
41 // Reduced memory usage can improve speed, due to cache effect
42 // Default value is 14, for 16KB, which nicely fits into Intel x86 L1 cache
43 #define MEMORY_USAGE 14
44 
45 // BIG_ENDIAN_NATIVE_BUT_INCOMPATIBLE :
46 // This will provide a small boost to performance for big endian cpu, but the resulting compressed stream will be incompatible with little-endian CPU.
47 // You can set this option to 1 in situations where data will remain within closed environment
48 // This option is useless on Little_Endian CPU (such as x86)
49 //#define BIG_ENDIAN_NATIVE_BUT_INCOMPATIBLE 1
50 
51 //**************************************
52 // CPU Feature Detection
53 //**************************************
54 // 32 or 64 bits ?
55 #ifndef LZ4_ARCH64
56 
57 #if (defined(__x86_64__) || defined(__x86_64) || defined(__amd64__) || defined(__amd64) || defined(__ppc64__) || defined(_WIN64) || defined(__LP64__) || defined(_LP64) ) // Detects 64 bits mode
58 # define LZ4_ARCH64 1
59 #else
60 # define LZ4_ARCH64 0
61 #endif
62 
63 #endif
64 
65 // Little Endian or Big Endian ?
66 // Overwrite the #define below if you know your architecture endianess
67 #if defined (__GLIBC__)
68 # include <endian.h>
69 # if (__BYTE_ORDER == __BIG_ENDIAN)
70 # define LZ4_BIG_ENDIAN 1
71 # endif
72 #elif (defined(__BIG_ENDIAN__) || defined(__BIG_ENDIAN) || defined(_BIG_ENDIAN)) && !(defined(__LITTLE_ENDIAN__) || defined(__LITTLE_ENDIAN) || defined(_LITTLE_ENDIAN))
73 # define LZ4_BIG_ENDIAN 1
74 #elif defined(__sparc) || defined(__sparc__) \
75  || defined(__ppc__) || defined(_POWER) || defined(__powerpc__) || defined(_ARCH_PPC) || defined(__PPC__) || defined(__PPC) || defined(PPC) || defined(__powerpc__) || defined(__powerpc) || defined(powerpc) \
76  || defined(__hpux) || defined(__hppa) \
77  || defined(_MIPSEB) || defined(__s390__)
78 # define LZ4_BIG_ENDIAN 1
79 #else
80 // Little Endian assumed. PDP Endian and other very rare endian format are unsupported.
81 #endif
82 
83 // Unaligned memory access is automatically enabled for "common" CPU, such as x86.
84 // For others CPU, the compiler will be more cautious, and insert extra code to ensure aligned access is respected
85 // If you know your target CPU supports unaligned memory access, you want to force this option manually to improve performance
86 #if defined(__ARM_FEATURE_UNALIGNED)
87 # define LZ4_FORCE_UNALIGNED_ACCESS 1
88 #endif
89 
90 // Define this parameter if your target system or compiler does not support hardware bit count
91 #if defined(_MSC_VER) && defined(_WIN32_WCE) // Visual Studio for Windows CE does not support Hardware bit count
92 # define LZ4_FORCE_SW_BITCOUNT
93 #endif
94 
95 //**************************************
96 // Compiler Options
97 //**************************************
98 #if __STDC_VERSION__ >= 199901L // C99
99 /* "restrict" is a known keyword */
100 #else
101 # define restrict // Disable restrict
102 #endif
103 
104 #define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
105 
106 #if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT) // Visual Studio
107 # include <intrin.h> // For Visual 2005
108 # if LZ4_ARCH64 && defined(_M_X64) // 64-bit
109 # pragma intrinsic(_BitScanForward64) // For Visual 2005
110 # pragma intrinsic(_BitScanReverse64) // For Visual 2005
111 # else
112 # pragma intrinsic(_BitScanForward) // For Visual 2005
113 # pragma intrinsic(_BitScanReverse) // For Visual 2005
114 # endif
115 # pragma warning(disable : 4127) // disable: C4127: conditional expression is constant
116 #endif
117 
118 #ifdef _MSC_VER
119 # define lz4_bswap16(x) _byteswap_ushort(x)
120 #else
121 # define lz4_bswap16(x) ((unsigned short int) ((((x) >> 8) & 0xffu) | (((x) & 0xffu) << 8)))
122 #endif
123 
124 #if (GCC_VERSION >= 302) || (__INTEL_COMPILER >= 800) || defined(__clang__)
125 # define expect(expr,value) (__builtin_expect ((expr),(value)) )
126 #else
127 # define expect(expr,value) (expr)
128 #endif
129 
130 #define likely(expr) expect((expr) != 0, 1)
131 #define unlikely(expr) expect((expr) != 0, 0)
132 
133 
134 //**************************************
135 // Includes
136 //**************************************
137 #include <stdlib.h> // for malloc
138 #include <string.h> // for memset
139 #include "lz4.h"
140 
141 
142 //**************************************
143 // Basic Types
144 //**************************************
145 #if defined(_MSC_VER) // Visual Studio does not support 'stdint' natively
146 # define BYTE unsigned __int8
147 # define U16 unsigned __int16
148 # define U32 unsigned __int32
149 # define S32 __int32
150 # define U64 unsigned __int64
151 # define S64 __int64
152 #else
153 # include <stdint.h>
154 # define BYTE uint8_t
155 # define U16 uint16_t
156 # define U32 uint32_t
157 # define S32 int32_t
158 # define U64 uint64_t
159 # define S64 int64_t
160 #endif
161 
162 #ifndef LZ4_FORCE_UNALIGNED_ACCESS
163 # pragma pack(push, 1)
164 #endif
165 
166 typedef struct _U16_S { U16 v; } U16_S;
167 typedef struct _U32_S { U32 v; } U32_S;
168 typedef struct _U64_S { U64 v; } U64_S;
169 
170 #ifndef LZ4_FORCE_UNALIGNED_ACCESS
171 # pragma pack(pop)
172 #endif
173 
174 #define A64(x) (((U64_S *)(x))->v)
175 #define A32(x) (((U32_S *)(x))->v)
176 #define A16(x) (((U16_S *)(x))->v)
177 
178 
179 //**************************************
180 // Constants
181 //**************************************
182 #define MINMATCH 4
183 
184 #define HASH_LOG (MEMORY_USAGE-2)
185 #define HASHTABLESIZE (1 << HASH_LOG)
186 #define HASH_MASK (HASHTABLESIZE - 1)
187 
188 #define NOTCOMPRESSIBLE_DETECTIONLEVEL 6 // Increasing this value will make the algorithm slower on incompressible data
189 #define SKIPSTRENGTH (NOTCOMPRESSIBLE_DETECTIONLEVEL>2?NOTCOMPRESSIBLE_DETECTIONLEVEL:2)
190 #define STACKLIMIT 13
191 #define HEAPMODE (HASH_LOG>STACKLIMIT) // Defines if memory is allocated into the stack (local variable), or into the heap (malloc()).
192 #define COPYLENGTH 8
193 #define LASTLITERALS 5
194 #define MFLIMIT (COPYLENGTH+MINMATCH)
195 #define MINLENGTH (MFLIMIT+1)
196 
197 #define MAXD_LOG 16
198 #define MAX_DISTANCE ((1 << MAXD_LOG) - 1)
199 
200 #define ML_BITS 4
201 #define ML_MASK ((1U<<ML_BITS)-1)
202 #define RUN_BITS (8-ML_BITS)
203 #define RUN_MASK ((1U<<RUN_BITS)-1)
204 
205 
206 //**************************************
207 // Architecture-specific macros
208 //**************************************
209 #if LZ4_ARCH64 // 64-bit
210 # define STEPSIZE 8
211 # define UARCH U64
212 # define AARCH A64
213 # define LZ4_COPYSTEP(s,d) A64(d) = A64(s); d+=8; s+=8;
214 # define LZ4_COPYPACKET(s,d) LZ4_COPYSTEP(s,d)
215 # define LZ4_SECURECOPY(s,d,e) if (d<e) LZ4_WILDCOPY(s,d,e)
216 # define HTYPE U32
217 # define INITBASE(base) const BYTE* const base = ip
218 #else // 32-bit
219 # define STEPSIZE 4
220 # define UARCH U32
221 # define AARCH A32
222 # define LZ4_COPYSTEP(s,d) A32(d) = A32(s); d+=4; s+=4;
223 # define LZ4_COPYPACKET(s,d) LZ4_COPYSTEP(s,d); LZ4_COPYSTEP(s,d);
224 # define LZ4_SECURECOPY LZ4_WILDCOPY
225 # define HTYPE const BYTE*
226 # define INITBASE(base) const int base = 0
227 #endif
228 
229 #if (defined(LZ4_BIG_ENDIAN) && !defined(BIG_ENDIAN_NATIVE_BUT_INCOMPATIBLE))
230 # define LZ4_READ_LITTLEENDIAN_16(d,s,p) { U16 v = A16(p); v = lz4_bswap16(v); d = (s) - v; }
231 # define LZ4_WRITE_LITTLEENDIAN_16(p,i) { U16 v = (U16)(i); v = lz4_bswap16(v); A16(p) = v; p+=2; }
232 #else // Little Endian
233 # define LZ4_READ_LITTLEENDIAN_16(d,s,p) { d = (s) - A16(p); }
234 # define LZ4_WRITE_LITTLEENDIAN_16(p,v) { A16(p) = v; p+=2; }
235 #endif
236 
237 //**************************************
238 // Local structures
239 //**************************************
240 struct refTables
241 {
243 };
244 
245 //**************************************
246 // Macros
247 //**************************************
248 #define LZ4_HASH_FUNCTION(i) (((i) * 2654435761U) >> ((MINMATCH*8)-HASH_LOG))
249 #define LZ4_HASH_VALUE(p) LZ4_HASH_FUNCTION(A32(p))
250 #define LZ4_WILDCOPY(s,d,e) do { LZ4_COPYPACKET(s,d) } while (d<e);
251 #define LZ4_BLINDCOPY(s,d,l) { BYTE* e=(d)+l; LZ4_WILDCOPY(s,d,e); d=e; }
252 
253 #endif // LZ4_CS_ADAPTER
254 
255 //****************************
256 // Private functions
257 //****************************
258 #ifndef LZ4_CS_ADAPTER
259 
260 #if LZ4_ARCH64
261 
262 static inline int LZ4_NbCommonBytes (register U64 val)
263 {
264 #if defined(LZ4_BIG_ENDIAN)
265  #if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT) && defined(_M_X64)
266  unsigned long r = 0;
267  _BitScanReverse64( &r, val );
268  return (int)(r>>3);
269  #elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
270  return (__builtin_clzll(val) >> 3);
271  #else
272  int r;
273  if (!(val>>32)) { r=4; } else { r=0; val>>=32; }
274  if (!(val>>16)) { r+=2; val>>=8; } else { val>>=24; }
275  r += (!val);
276  return r;
277  #endif
278 #else
279  #if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT) && defined(_M_X64)
280  unsigned long r = 0;
281  _BitScanForward64( &r, val );
282  return (int)(r>>3);
283  #elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
284  return (__builtin_ctzll(val) >> 3);
285  #else
286  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 };
287 #ifdef LZ4_MK_OPT
288  return DeBruijnBytePos[((U64)((U64)((S64)val & -(S64)val) * 0x0218A392CDABBD3F)) >> 58];
289 #else
290  return DeBruijnBytePos[((U64)((val & -val) * 0x0218A392CDABBD3F)) >> 58];
291 #endif
292  #endif
293 #endif
294 }
295 
296 #else
297 
298 static inline int LZ4_NbCommonBytes (register U32 val)
299 {
300 #if defined(LZ4_BIG_ENDIAN)
301  #if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
302  unsigned long r = 0;
303  _BitScanReverse( &r, val );
304  return (int)(r>>3);
305  #elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
306  return (__builtin_clz(val) >> 3);
307  #else
308  int r;
309  if (!(val>>16)) { r=2; val>>=8; } else { r=0; val>>=24; }
310  r += (!val);
311  return r;
312  #endif
313 #else
314  #if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
315  unsigned long r;
316  _BitScanForward( &r, val );
317  return (int)(r>>3);
318  #elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
319  return (__builtin_ctz(val) >> 3);
320  #else
321  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 };
322 #ifdef LZ4_MK_OPT
323  return DeBruijnBytePos[((U32)((U32)((S32)val & -(S32)val) * 0x077CB531U)) >> 27];
324 #else
325  return DeBruijnBytePos[((U32)((val & -(S32)val) * 0x077CB531U)) >> 27];
326 #endif
327  #endif
328 #endif
329 }
330 
331 #endif
332 
333 #endif // LZ4_CS_ADAPTER
334 
335 //******************************
336 // Compression functions
337 //******************************
338 
339 // LZ4_compressCtx :
340 // -----------------
341 // Compress 'isize' bytes from 'source' into an output buffer 'dest' of maximum size 'maxOutputSize'.
342 // If it cannot achieve it, compression will stop, and result of the function will be zero.
343 // return : the number of bytes written in buffer 'dest', or 0 if the compression fails
344 
345 static inline int LZ4_compressCtx(void** ctx,
346  const char* source,
347  char* dest,
348  int inputSize,
349  int maxOutputSize)
350 {
351 #if HEAPMODE
352  struct refTables *srt = (struct refTables *) (*ctx);
353  HTYPE* HashTable;
354 #else
355  HTYPE HashTable[HASHTABLESIZE] = {0};
356 #endif
357 
358  const BYTE* ip = (BYTE*) source;
359  INITBASE(base);
360  const BYTE* anchor = ip;
361  const BYTE* const iend = ip + inputSize;
362  const BYTE* const mflimit = iend - MFLIMIT;
363 
364  BYTE* op = (BYTE*) dest;
365  BYTE* const oend = op + maxOutputSize;
366 
367 #ifdef LZ4_MK_OPT
368  const BYTE* matchlimit = iend - LASTLITERALS;
369  const BYTE* matchlimit_1 = matchlimit - 1;
370  #if (LZ4_ARCH64 == 1)
371  const BYTE* matchlimit_3 = matchlimit - 3;
372  #endif
373  const BYTE* matchlimit_STEPSIZE_1 = matchlimit - (STEPSIZE - 1);
374  const BYTE* oend_LASTLITERALS_1 = oend - (1 + LASTLITERALS);
375  const BYTE* oend_LASTLITERALS_3 = oend - (2 + 1 + LASTLITERALS);
376 #else
377  #define matchlimit (iend - LASTLITERALS)
378 #endif
379 
380  int length;
381 #ifndef LZ4_CS_ADAPTER
382  const int skipStrength = SKIPSTRENGTH;
383 #endif
384  U32 forwardH;
385 
386  // Init
387  if (inputSize<MINLENGTH) goto _last_literals;
388 #ifndef LZ4_CS_ADAPTER
389 #if HEAPMODE
390  if (*ctx == NULL)
391  {
392  srt = (struct refTables *) malloc ( sizeof(struct refTables) );
393  *ctx = (void*) srt;
394  }
395  HashTable = (HTYPE*)(srt->hashTable);
396  memset((void*)HashTable, 0, sizeof(srt->hashTable));
397 #else
398  (void) ctx;
399 #endif
400 #endif
401 
402  // First Byte
403  HashTable[LZ4_HASH_VALUE(ip)] = (HTYPE)(ip - base);
404  ip++; forwardH = LZ4_HASH_VALUE(ip);
405 
406  // Main Loop
407  while (1)
408  {
409  int findMatchAttempts = (1U << skipStrength) + 3;
410  const BYTE* forwardIp = ip;
411  const BYTE* ref;
412  BYTE* token;
413 
414  // Find a match
415  do {
416  U32 h = forwardH;
417  int step = findMatchAttempts++ >> skipStrength;
418  ip = forwardIp;
419  forwardIp = ip + step;
420 
421  if unlikely(forwardIp > mflimit) goto _last_literals;
422 
423  forwardH = LZ4_HASH_VALUE(forwardIp);
424  ref = base + HashTable[h];
425  HashTable[h] = (HTYPE)(ip - base);
426 
427  } while ((ref < ip - MAX_DISTANCE) || (A32(ref) != A32(ip)));
428 
429  // Catch up
430  while ((ip>anchor) && (ref>(BYTE*)source) && unlikely(ip[-1]==ref[-1])) { ip--; ref--; }
431 
432  // Encode Literal length
433  length = (int)(ip - anchor);
434  token = op++;
435 #ifdef LZ4_MK_OPT
436  if unlikely(op + length + (length>>8) > oend_LASTLITERALS_3) return 0; // Check output limit
437 #else
438  if unlikely(op + length + (2 + 1 + LASTLITERALS) + (length>>8) > oend) return 0; // Check output limit
439 #endif
440 #ifdef _MSC_VER
441  if (length>=(int)RUN_MASK)
442  {
443  int len = length-RUN_MASK;
444  *token=(RUN_MASK<<ML_BITS);
445  if (len>254)
446  {
447  do { *op++ = 255; len -= 255; } while (len>254);
448  *op++ = (BYTE)len;
449  memcpy(op, anchor, length);
450  op += length;
451  goto _next_match;
452  }
453  else
454  *op++ = (BYTE)len;
455  }
456  else *token = (BYTE)(length<<ML_BITS);
457 #else
458  if (length>=(int)RUN_MASK)
459  {
460  int len;
461  *token=(RUN_MASK<<ML_BITS);
462  len = length-RUN_MASK;
463  for(; len > 254 ; len-=255) *op++ = 255;
464  *op++ = (BYTE)len;
465  }
466  else *token = (length<<ML_BITS);
467 #endif
468 
469  // Copy Literals
470  LZ4_BLINDCOPY(anchor, op, length);
471 
472 _next_match:
473  // Encode Offset
474  LZ4_WRITE_LITTLEENDIAN_16(op,(U16)(ip-ref));
475 
476  // Start Counting
477  ip+=MINMATCH; ref+=MINMATCH; // MinMatch already verified
478  anchor = ip;
479 #ifdef LZ4_MK_OPT
480  while likely(ip < matchlimit_STEPSIZE_1)
481 #else
482  while likely(ip<matchlimit-(STEPSIZE-1))
483 #endif
484  {
485  UARCH diff = AARCH(ref) ^ AARCH(ip);
486  if (!diff) { ip += STEPSIZE; ref += STEPSIZE; continue; }
487  ip += LZ4_NbCommonBytes(diff);
488  goto _endCount;
489  }
490 #ifdef LZ4_MK_OPT
491  #if (LZ4_ARCH64 == 1)
492  if ((ip<matchlimit_3) && (A32(ref) == A32(ip))) { ip+=4; ref+=4; }
493  #endif
494  if ((ip<matchlimit_1) && (A16(ref) == A16(ip))) { ip+=2; ref+=2; }
495  if ((ip<matchlimit) && (*ref == *ip)) ip++;
496 #else
497  if (LZ4_ARCH64) if ((ip<(matchlimit-3)) && (A32(ref) == A32(ip))) { ip+=4; ref+=4; }
498  if ((ip<(matchlimit-1)) && (A16(ref) == A16(ip))) { ip+=2; ref+=2; }
499  if ((ip<matchlimit) && (*ref == *ip)) ip++;
500 #endif
501 
502 _endCount:
503 
504  // Encode MatchLength
505  length = (int)(ip - anchor);
506 #ifdef LZ4_MK_OPT
507  if unlikely(op + (length>>8) > oend_LASTLITERALS_1) return 0; // Check output limit
508 #else
509  if unlikely(op + (1 + LASTLITERALS) + (length>>8) > oend) return 0; // Check output limit
510 #endif
511 
512  if (length>=(int)ML_MASK)
513  {
514  *token+=ML_MASK;
515  length -= ML_MASK;
516  for (; length > 509 ; length-=510) { *op++ = 255; *op++ = 255; }
517  if (length > 254) { length-=255; *op++ = 255; }
518  *op++ = (BYTE)length;
519  }
520  else *token += (BYTE)length;
521 
522  // Test end of chunk
523  if (ip > mflimit) { anchor = ip; break; }
524 
525  // Fill table
526  HashTable[LZ4_HASH_VALUE(ip-2)] = (HTYPE)(ip - 2 - base);
527 
528  // Test next position
529 #ifdef LZ4_MK_OPT
530  U32 h = LZ4_HASH_VALUE(ip);
531  ref = base + HashTable[h];
532  HashTable[h] = (HTYPE)(ip - base);
533 #else
534  ref = base + HashTable[LZ4_HASH_VALUE(ip)];
535  HashTable[LZ4_HASH_VALUE(ip)] = (HTYPE)(ip - base);
536 #endif
537 
538  if ((ref > ip - (MAX_DISTANCE + 1)) && (A32(ref) == A32(ip))) { token = op++; *token=0; goto _next_match; }
539 
540  // Prepare next loop
541  anchor = ip++;
542  forwardH = LZ4_HASH_VALUE(ip);
543  }
544 
545 _last_literals:
546  // Encode Last Literals
547  {
548  int lastRun = (int)(iend - anchor);
549 #ifdef LZ4_MK_OPT
550  if ((BYTE*)op + lastRun + 1 + ((lastRun+255-RUN_MASK)/255) > oend) return 0;
551 #else
552  if (((char*)op - dest) + lastRun + 1 + ((lastRun+255-RUN_MASK)/255) > (U32)maxOutputSize) return 0; // Check output limit
553 #endif
554  if (lastRun>=(int)RUN_MASK) { *op++=(RUN_MASK<<ML_BITS); lastRun-=RUN_MASK; for(; lastRun > 254 ; lastRun-=255) *op++ = 255; *op++ = (BYTE) lastRun; }
555  else *op++ = (BYTE)(lastRun<<ML_BITS);
556  memcpy(op, anchor, iend - anchor);
557  op += iend-anchor;
558  }
559 
560  // End
561  return (int) (((char*)op)-dest);
562 }
563 
564 // Note : this function is valid only if isize < LZ4_64KLIMIT
565 #ifndef LZ4_CS_ADAPTER
566 #define LZ4_64KLIMIT ((1<<16) + (MFLIMIT-1))
567 #define HASHLOG64K (HASH_LOG+1)
568 #define HASH64KTABLESIZE (1U<<HASHLOG64K)
569 #define LZ4_HASH64K_FUNCTION(i) (((i) * 2654435761U) >> ((MINMATCH*8)-HASHLOG64K))
570 #define LZ4_HASH64K_VALUE(p) LZ4_HASH64K_FUNCTION(A32(p))
571 #endif
572 
573 static inline int LZ4_compress64kCtx(void** ctx,
574  const char* source,
575  char* dest,
576  int isize,
577  int maxOutputSize)
578 {
579 #if HEAPMODE
580  struct refTables *srt = (struct refTables *) (*ctx);
581  U16* HashTable;
582 #else
583  U16 HashTable[HASH64KTABLESIZE] = {0};
584 #endif
585 
586  const BYTE* ip = (BYTE*) source;
587  const BYTE* anchor = ip;
588  const BYTE* const base = ip;
589  const BYTE* const iend = ip + isize;
590  const BYTE* const mflimit = iend - MFLIMIT;
591 
592  BYTE* op = (BYTE*) dest;
593  BYTE* const oend = op + maxOutputSize;
594 
595 #ifdef LZ4_MK_OPT
596  const BYTE* matchlimit = iend - LASTLITERALS;
597  const BYTE* matchlimit_1 = matchlimit - 1;
598  #if (LZ4_ARCH64 == 1)
599  const BYTE* matchlimit_3 = matchlimit - 3;
600  #endif
601  const BYTE* matchlimit_STEPSIZE_1 = matchlimit - (STEPSIZE - 1);
602  const BYTE* oend_LASTLITERALS_1 = oend - (1 + LASTLITERALS);
603  const BYTE* oend_LASTLITERALS_3 = oend - (2 + 1 + LASTLITERALS);
604 #else
605  #define matchlimit (iend - LASTLITERALS)
606 #endif
607 
608  int len, length;
609 #ifndef LZ4_CS_ADAPTER
610  const int skipStrength = SKIPSTRENGTH;
611 #endif
612  U32 forwardH;
613 
614  // Init
615  if (isize < MINLENGTH) goto _last_literals;
616 #ifndef LZ4_CS_ADAPTER
617 #if HEAPMODE
618  if (*ctx == NULL)
619  {
620  srt = (struct refTables *) malloc ( sizeof(struct refTables) );
621  *ctx = (void*) srt;
622  }
623  HashTable = (U16*)(srt->hashTable);
624  memset((void*)HashTable, 0, sizeof(srt->hashTable));
625 #else
626  (void) ctx;
627 #endif
628 #endif
629 
630  // First Byte
631  ip++; forwardH = LZ4_HASH64K_VALUE(ip);
632 
633  // Main Loop
634  while (1)
635  {
636  int findMatchAttempts = (1U << skipStrength) + 3;
637  const BYTE* forwardIp = ip;
638  const BYTE* ref;
639  BYTE* token;
640 
641  // Find a match
642  do {
643  U32 h = forwardH;
644  int step = findMatchAttempts++ >> skipStrength;
645  ip = forwardIp;
646  forwardIp = ip + step;
647 
648  if (forwardIp > mflimit) goto _last_literals;
649 
650  forwardH = LZ4_HASH64K_VALUE(forwardIp);
651  ref = base + HashTable[h];
652  HashTable[h] = (U16)(ip - base);
653 
654  } while (A32(ref) != A32(ip));
655 
656  // Catch up
657  while ((ip>anchor) && (ref>(BYTE*)source) && (ip[-1]==ref[-1])) { ip--; ref--; }
658 
659  // Encode Literal length
660  length = (int)(ip - anchor);
661  token = op++;
662 #ifdef LZ4_MK_OPT
663  if unlikely(op + length + (length>>8) > oend_LASTLITERALS_3) return 0; // Check output limit
664 #else
665  if unlikely(op + length + (2 + 1 + LASTLITERALS) + (length>>8) > oend) return 0; // Check output limit
666 #endif
667 
668 #ifdef _MSC_VER
669  if (length>=(int)RUN_MASK)
670  {
671  int len = length-RUN_MASK;
672  *token=(RUN_MASK<<ML_BITS);
673  if (len>254)
674  {
675  do { *op++ = 255; len -= 255; } while (len>254);
676  *op++ = (BYTE)len;
677  memcpy(op, anchor, length);
678  op += length;
679  goto _next_match;
680  }
681  else
682  *op++ = (BYTE)len;
683  }
684  else *token = (BYTE)(length<<ML_BITS);
685 #else
686  if (length>=(int)RUN_MASK) { *token=(RUN_MASK<<ML_BITS); len = length-RUN_MASK; for(; len > 254 ; len-=255) *op++ = 255; *op++ = (BYTE)len; }
687  else *token = (length<<ML_BITS);
688 #endif
689 
690  // Copy Literals
691  LZ4_BLINDCOPY(anchor, op, length);
692 
693 _next_match:
694  // Encode Offset
695  LZ4_WRITE_LITTLEENDIAN_16(op,(U16)(ip-ref));
696 
697  // Start Counting
698  ip+=MINMATCH; ref+=MINMATCH; // MinMatch verified
699  anchor = ip;
700 #ifdef LZ4_MK_OPT
701  while (ip<matchlimit_STEPSIZE_1)
702 #else
703  while (ip<matchlimit-(STEPSIZE-1))
704 #endif
705  {
706  UARCH diff = AARCH(ref) ^ AARCH(ip);
707  if (!diff) { ip+=STEPSIZE; ref+=STEPSIZE; continue; }
708  ip += LZ4_NbCommonBytes(diff);
709  goto _endCount;
710  }
711 #ifdef LZ4_MK_OPT
712  #if (LZ4_ARCH64 == 1)
713  if ((ip<matchlimit_3) && (A32(ref) == A32(ip))) { ip+=4; ref+=4; }
714  #endif
715  if ((ip<matchlimit_1) && (A16(ref) == A16(ip))) { ip+=2; ref+=2; }
716  if ((ip<matchlimit) && (*ref == *ip)) ip++;
717 #else
718  if (LZ4_ARCH64) if ((ip<(matchlimit-3)) && (A32(ref) == A32(ip))) { ip+=4; ref+=4; }
719  if ((ip<(matchlimit-1)) && (A16(ref) == A16(ip))) { ip+=2; ref+=2; }
720  if ((ip<matchlimit) && (*ref == *ip)) ip++;
721 #endif
722 
723 _endCount:
724 
725  // Encode MatchLength
726  len = (int)(ip - anchor);
727 #ifdef LZ4_MK_OPT
728  if unlikely(op + (len>>8) > oend_LASTLITERALS_1) return 0; // Check output limit
729 #else
730  if unlikely(op + (1 + LASTLITERALS) + (len>>8) > oend) return 0; // Check output limit
731 #endif
732  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; }
733  else *token += (BYTE)len;
734 
735  // Test end of chunk
736  if (ip > mflimit) { anchor = ip; break; }
737 
738  // Fill table
739  HashTable[LZ4_HASH64K_VALUE(ip-2)] = (U16)(ip - 2 - base);
740 
741  // Test next position
742 #ifdef LZ4_MK_OPT
743  U32 h = LZ4_HASH64K_VALUE(ip);
744  ref = base + HashTable[h];
745  HashTable[h] = (U16)(ip - base);
746 #else
747  ref = base + HashTable[LZ4_HASH64K_VALUE(ip)];
748  HashTable[LZ4_HASH64K_VALUE(ip)] = (U16)(ip - base);
749 #endif
750 
751  if (A32(ref) == A32(ip)) { token = op++; *token=0; goto _next_match; }
752 
753  // Prepare next loop
754  anchor = ip++;
755  forwardH = LZ4_HASH64K_VALUE(ip);
756  }
757 
758 _last_literals:
759  // Encode Last Literals
760  {
761  int lastRun = (int)(iend - anchor);
762  if (op + lastRun + 1 + (lastRun-RUN_MASK+255)/255 > oend) return 0;
763  if (lastRun>=(int)RUN_MASK) { *op++=(RUN_MASK<<ML_BITS); lastRun-=RUN_MASK; for(; lastRun > 254 ; lastRun-=255) *op++ = 255; *op++ = (BYTE) lastRun; }
764  else *op++ = (BYTE)(lastRun<<ML_BITS);
765  memcpy(op, anchor, iend - anchor);
766  op += iend-anchor;
767  }
768 
769  // End
770  return (int) (((char*)op)-dest);
771 }
772 
773 
774 CORE_EXPORT( int ) LZ4_compress_limitedOutput(const char* source,
775  char* dest,
776  int isize,
777  int maxOutputSize)
778 {
779 #if HEAPMODE
780  void* ctx = malloc(sizeof(struct refTables));
781  int result;
782  if (ctx == NULL) return 0; // Failed allocation => compression not done
783  if (isize < LZ4_64KLIMIT)
784  result = LZ4_compress64kCtx(&ctx, source, dest, isize, maxOutputSize);
785  else result = LZ4_compressCtx(&ctx, source, dest, isize, maxOutputSize);
786  free(ctx);
787  return result;
788 #else
789  if (isize < (int)LZ4_64KLIMIT) return LZ4_compress64kCtx(NULL, source, dest, isize, maxOutputSize);
790  return LZ4_compressCtx(NULL, source, dest, isize, maxOutputSize);
791 #endif
792 }
793 
794 CORE_EXPORT( int ) LZ4_compress(const char* source,
795  char* dest,
796  int isize)
797 {
798  return LZ4_compress_limitedOutput(source, dest, isize, LZ4_compressBound(isize));
799 }
800 
801 //****************************
802 // Decompression functions
803 //****************************
804 
805 // Note : The decoding functions LZ4_uncompress() and LZ4_uncompress_unknownOutputSize()
806 // are safe against "buffer overflow" attack type.
807 // They will never write nor read outside of the provided output buffers.
808 // LZ4_uncompress_unknownOutputSize() also insures that it will never read outside of the input buffer.
809 // LZ4_uncompress() guarantees that it will never read before source, nor beyond source + LZ4_compressBound(osize)
810 // A corrupted input will produce an error result, a negative int, indicating the position of the error within input stream.
811 
812 CORE_EXPORT( int ) LZ4_uncompress(const char* source,
813  char* dest,
814  int osize)
815 {
816  // Local Variables
817  const BYTE* restrict ip = (const BYTE*) source;
818  const BYTE* ref;
819 
820  BYTE* op = (BYTE*) dest;
821  BYTE* const oend = op + osize;
822  BYTE* cpy;
823 
824  #ifdef LZ4_MK_OPT
825  const BYTE* oend_LASTLITERALS = oend - LASTLITERALS;
826  const BYTE* oend_COPYLENGTH = oend - COPYLENGTH;
827  const BYTE* oend_COPYLENGTH_STEPSIZE_4 = oend - COPYLENGTH - (STEPSIZE - 4);
828  #endif
829 
830  unsigned token;
831 
832  size_t dec32table[] = {0, 3, 2, 3, 0, 0, 0, 0};
833 #if LZ4_ARCH64
834  size_t dec64table[] = {0, 0, 0, (size_t)-1, 0, 1, 2, 3};
835 #endif
836 
837  // Main Loop
838  while (1)
839  {
840  size_t length;
841 
842  // get runlength
843  token = *ip++;
844  if ((length=(token>>ML_BITS)) == RUN_MASK) { size_t len; for (;(len=*ip++)==255;length+=255) { } length += len; }
845 
846  // copy literals
847  cpy = op+length;
848 #ifdef LZ4_MK_OPT
849  if (cpy>oend_COPYLENGTH)
850 #else
851  if (cpy>oend-COPYLENGTH)
852 #endif
853  {
854  if (cpy != oend) goto _output_error; // Error : not enough place for another match (min 4) + 5 literals
855  memcpy(op, ip, length);
856  ip += length;
857  break; // EOF
858  }
859  LZ4_WILDCOPY(ip, op, cpy); ip -= (op-cpy); op = cpy;
860 
861  // get offset
862  LZ4_READ_LITTLEENDIAN_16(ref,cpy,ip); ip+=2;
863  if unlikely(ref < (BYTE* const)dest) goto _output_error; // Error : offset outside destination buffer
864 
865  // get matchlength
866  if ((length=(token&ML_MASK)) == ML_MASK) { for (;*ip==255;length+=255) {ip++;} length += *ip++; }
867 
868  // copy repeated sequence
869  if unlikely((op-ref)<STEPSIZE)
870  {
871 #if LZ4_ARCH64
872  size_t dec64 = dec64table[op-ref];
873 #else
874  const int dec64 = 0;
875 #endif
876  op[0] = ref[0];
877  op[1] = ref[1];
878  op[2] = ref[2];
879  op[3] = ref[3];
880  op += 4; ref += 4; ref -= dec32table[op-ref];
881  A32(op) = A32(ref);
882  op += STEPSIZE-4; ref -= dec64;
883  } else { LZ4_COPYSTEP(ref,op); }
884  cpy = op + length - (STEPSIZE-4);
885 
886 #ifdef LZ4_MK_OPT
887  if unlikely(cpy > oend_COPYLENGTH_STEPSIZE_4)
888 #else
889  if unlikely(cpy>oend-(COPYLENGTH)-(STEPSIZE-4))
890 #endif
891  {
892 #ifdef LZ4_MK_OPT
893  if (cpy > oend_LASTLITERALS) goto _output_error; // Error : last 5 bytes must be literals
894  LZ4_SECURECOPY(ref, op, oend_COPYLENGTH);
895 #else
896  if (cpy > oend-LASTLITERALS) goto _output_error; // Error : last 5 bytes must be literals
897  LZ4_SECURECOPY(ref, op, (oend-COPYLENGTH));
898 #endif
899  while(op<cpy) *op++=*ref++;
900  op=cpy;
901  continue;
902  }
903 
904  LZ4_WILDCOPY(ref, op, cpy);
905  op = cpy; // correction
906  }
907 
908  // end of decoding
909  return (int) (((char*)ip)-source);
910 
911  // write overflow error detected
912 _output_error:
913  return (int) (-(((char*)ip)-source));
914 }
915 
916 CORE_EXPORT( int ) LZ4_uncompress_unknownOutputSize(
917  const char* source,
918  char* dest,
919  int isize,
920  int maxOutputSize)
921 {
922  // Local Variables
923  const BYTE* restrict ip = (const BYTE*) source;
924  const BYTE* const iend = ip + isize;
925  const BYTE* ref;
926 
927  BYTE* op = (BYTE*) dest;
928  BYTE* const oend = op + maxOutputSize;
929  BYTE* cpy;
930 
931 #ifdef LZ4_MK_OPT
932  const BYTE* iend_LASTLITERALS_3 = (iend-(2+1+LASTLITERALS));
933  const BYTE* iend_LASTLITERALS_1 = (iend-(LASTLITERALS+1));
934  const BYTE* oend_COPYLENGTH = (oend-COPYLENGTH);
935  const BYTE* oend_COPYLENGTH_STEPSIZE_4 = (oend-(COPYLENGTH+(STEPSIZE-4)));
936  const BYTE* oend_LASTLITERALS = (oend - LASTLITERALS);
937  const BYTE* oend_MFLIMIT = (oend - MFLIMIT);
938 #endif
939 
940  size_t dec32table[] = {0, 3, 2, 3, 0, 0, 0, 0};
941 #if LZ4_ARCH64
942  size_t dec64table[] = {0, 0, 0, (size_t)-1, 0, 1, 2, 3};
943 #endif
944 
945  // Special case
946  if unlikely(ip==iend) goto _output_error; // A correctly formed null-compressed LZ4 must have at least one byte (token=0)
947 
948  // Main Loop
949  while (1)
950  {
951  unsigned token;
952  size_t length;
953 
954  // get runlength
955  token = *ip++;
956  if ((length=(token>>ML_BITS)) == RUN_MASK)
957  {
958  int s=255;
959  while (likely(ip<iend) && (s==255)) { s=*ip++; length += s; }
960  }
961 
962  // copy literals
963  cpy = op+length;
964 #ifdef LZ4_MK_OPT
965  if ((cpy>oend_MFLIMIT) || (ip+length>iend_LASTLITERALS_3))
966 #else
967  if ((cpy>oend-MFLIMIT) || (ip+length>iend-(2+1+LASTLITERALS)))
968 #endif
969  {
970  if (cpy > oend) goto _output_error; // Error : writes beyond output buffer
971  if (ip+length != iend) goto _output_error; // Error : LZ4 format requires to consume all input at this stage (no match within the last 11 bytes, and at least 8 remaining input bytes for another match+literals)
972  memcpy(op, ip, length);
973  op += length;
974  break; // Necessarily EOF, due to parsing restrictions
975  }
976  LZ4_WILDCOPY(ip, op, cpy); ip -= (op-cpy); op = cpy;
977 
978  // get offset
979  LZ4_READ_LITTLEENDIAN_16(ref,cpy,ip); ip+=2;
980  if unlikely(ref < (BYTE* const)dest) goto _output_error; // Error : offset outside of destination buffer
981 
982  // get matchlength
983  if ((length=(token&ML_MASK)) == ML_MASK)
984  {
985 #ifdef LZ4_MK_OPT
986  while likely(ip<iend_LASTLITERALS_1) // Error : a minimum input bytes must remain for LASTLITERALS + token
987 #else
988  while likely(ip<iend-(LASTLITERALS+1)) // Error : a minimum input bytes must remain for LASTLITERALS + token
989 #endif
990  {
991  int s = *ip++;
992  length +=s;
993  if (s==255) continue;
994  break;
995  }
996  }
997 
998  // copy repeated sequence
999  if unlikely(op-ref<STEPSIZE)
1000  {
1001 #if LZ4_ARCH64
1002  size_t dec64 = dec64table[op-ref];
1003 #else
1004  const int dec64 = 0;
1005 #endif
1006  op[0] = ref[0];
1007  op[1] = ref[1];
1008  op[2] = ref[2];
1009  op[3] = ref[3];
1010  op += 4; ref += 4; ref -= dec32table[op-ref];
1011  A32(op) = A32(ref);
1012  op += STEPSIZE-4; ref -= dec64;
1013  } else { LZ4_COPYSTEP(ref,op); }
1014  cpy = op + length - (STEPSIZE-4);
1015 
1016 #ifdef LZ4_MK_OPT
1017  if unlikely(cpy>oend_COPYLENGTH_STEPSIZE_4)
1018 #else
1019  if unlikely(cpy>oend-(COPYLENGTH+(STEPSIZE-4)))
1020 #endif
1021  {
1022 #ifdef LZ4_MK_OPT
1023  if (cpy > oend_LASTLITERALS) goto _output_error; // Error : last 5 bytes must be literals
1024  LZ4_SECURECOPY(ref, op, oend_COPYLENGTH);
1025 #else
1026  if (cpy > oend-LASTLITERALS) goto _output_error; // Error : last 5 bytes must be literals
1027  LZ4_SECURECOPY(ref, op, (oend-COPYLENGTH));
1028 #endif
1029  while(op<cpy) *op++=*ref++;
1030  op=cpy;
1031  continue;
1032  }
1033 
1034  LZ4_WILDCOPY(ref, op, cpy);
1035  op=cpy; // correction
1036  }
1037 
1038  // end of decoding
1039  return (int) (((char*)op)-dest);
1040 
1041  // write overflow error detected
1042 _output_error:
1043  return (int) (-(((char*)ip)-source));
1044 }
#define LZ4_HASH_VALUE(p)
Definition: lz4.c:249
Definition: lz4.c:167
#define unlikely(expr)
Definition: lz4.c:131
#define MINMATCH
Definition: lz4.c:182
#define UARCH
Definition: lz4.c:220
#define INITBASE(base)
Definition: lz4.c:226
#define LZ4_READ_LITTLEENDIAN_16(d, s, p)
Definition: lz4.c:233
#define LASTLITERALS
Definition: lz4.c:193
HTYPE hashTable[HASHTABLESIZE]
Definition: lz4.c:242
#define COPYLENGTH
Definition: lz4.c:192
#define U16
Definition: lz4.c:155
struct _U64_S U64_S
U32 v
Definition: lz4.c:167
#define S64
Definition: lz4.c:159
#define MAX_DISTANCE
Definition: lz4.c:198
U16 v
Definition: lz4.c:166
#define LZ4_WRITE_LITTLEENDIAN_16(p, v)
Definition: lz4.c:234
static int LZ4_NbCommonBytes(register U32 val)
Definition: lz4.c:298
#define HASH64KTABLESIZE
Definition: lz4.c:568
CORE_EXPORT(int)
Definition: lz4.c:774
char int isize
Definition: lz4.h:103
#define HTYPE
Definition: lz4.c:225
#define U32
Definition: lz4.c:156
Definition: lz4.c:240
char int int maxOutputSize
Definition: lz4.h:103
#define LZ4_WILDCOPY(s, d, e)
Definition: lz4.c:250
struct _U16_S U16_S
#define ML_BITS
Definition: lz4.c:200
#define matchlimit
Definition: lz4.c:166
#define SKIPSTRENGTH
Definition: lz4.c:189
#define U64
Definition: lz4.c:158
#define STEPSIZE
Definition: lz4.c:219
#define LZ4_COPYSTEP(s, d)
Definition: lz4.c:222
#define restrict
Definition: lz4.c:101
U64 v
Definition: lz4.c:168
function s(a)
#define A32(x)
Definition: lz4.c:175
#define MINLENGTH
Definition: lz4.c:195
#define BYTE
Definition: lz4.c:154
#define LZ4_SECURECOPY
Definition: lz4.c:224
static int LZ4_compressBound(int isize)
Definition: lz4.h:87
Definition: lz4.c:168
#define HASHTABLESIZE
Definition: lz4.c:185
#define ML_MASK
Definition: lz4.c:201
#define AARCH
Definition: lz4.c:221
char * dest
Definition: lz4.h:61
#define S32
Definition: lz4.c:157
#define LZ4_ARCH64
Definition: lz4.c:60
#define LZ4_HASH64K_VALUE(p)
Definition: lz4.c:570
static int LZ4_compressCtx(void **ctx, const char *source, char *dest, int inputSize, int maxOutputSize)
Definition: lz4.c:345
static int LZ4_compress64kCtx(void **ctx, const char *source, char *dest, int isize, int maxOutputSize)
Definition: lz4.c:573
struct _U32_S U32_S
#define RUN_MASK
Definition: lz4.c:203
#define LZ4_64KLIMIT
Definition: lz4.c:566
#define A16(x)
Definition: lz4.c:176
#define LZ4_BLINDCOPY(s, d, l)
Definition: lz4.c:251
char int inputSize
Definition: lz4.h:61
#define likely(expr)
Definition: lz4.c:130
#define MFLIMIT
Definition: lz4.c:194