CMSIS2000  0.0.7
 Указатель Структуры данных Файлы Функции Переменные Определения типов Перечисления Элементы перечислений Макросы Группы Страницы
lz4.c
См. документацию.
1 /*
2  LZ4 - Fast LZ compression algorithm
3  Copyright (C) 2011-2012, 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 //**************************************
35 // Tuning parameters
36 //**************************************
37 // MEMORY_USAGE :
38 // Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.)
39 // Increasing memory usage improves compression ratio
40 // Reduced memory usage can improve speed, due to cache effect
41 // Default value is 14, for 16KB, which nicely fits into Intel x86 L1 cache
42 #define MEMORY_USAGE 14
43 
44 // BIG_ENDIAN_NATIVE_BUT_INCOMPATIBLE :
45 // This will provide a small boost to performance for big endian cpu, but the resulting compressed stream will be incompatible with little-endian CPU.
46 // You can set this option to 1 in situations where data will remain within closed environment
47 // This option is useless on Little_Endian CPU (such as x86)
48 //#define BIG_ENDIAN_NATIVE_BUT_INCOMPATIBLE 1
49 
50 
51 
52 //**************************************
53 // CPU Feature Detection
54 //**************************************
55 // 32 or 64 bits ?
56 #if (defined(__x86_64__) || defined(__x86_64) || defined(__amd64__) || defined(__amd64) || defined(__ppc64__) || defined(_WIN64) || defined(__LP64__) || defined(_LP64) ) // Detects 64 bits mode
57 # define LZ4_ARCH64 1
58 #else
59 # define LZ4_ARCH64 0
60 #endif
61 
62 // Little Endian or Big Endian ?
63 // Overwrite the #define below if you know your architecture endianess
64 #if defined (__GLIBC__)
65 # include <endian.h>
66 # if (__BYTE_ORDER == __BIG_ENDIAN)
67 # define LZ4_BIG_ENDIAN 1
68 # endif
69 #elif (defined(__BIG_ENDIAN__) || defined(__BIG_ENDIAN) || defined(_BIG_ENDIAN)) && !(defined(__LITTLE_ENDIAN__) || defined(__LITTLE_ENDIAN) || defined(_LITTLE_ENDIAN))
70 # define LZ4_BIG_ENDIAN 1
71 #elif defined(__sparc) || defined(__sparc__) \
72  || defined(__ppc__) || defined(_POWER) || defined(__powerpc__) || defined(_ARCH_PPC) || defined(__PPC__) || defined(__PPC) || defined(PPC) || defined(__powerpc__) || defined(__powerpc) || defined(powerpc) \
73  || defined(__hpux) || defined(__hppa) \
74  || defined(_MIPSEB) || defined(__s390__)
75 # define LZ4_BIG_ENDIAN 1
76 #else
77 // Little Endian assumed. PDP Endian and other very rare endian format are unsupported.
78 #endif
79 
80 // Unaligned memory access is automatically enabled for "common" CPU, such as x86.
81 // For others CPU, the compiler will be more cautious, and insert extra code to ensure aligned access is respected
82 // If you know your target CPU supports unaligned memory access, you want to force this option manually to improve performance
83 #if defined(__ARM_FEATURE_UNALIGNED)
84 # define LZ4_FORCE_UNALIGNED_ACCESS 1
85 #endif
86 
87 // Define this parameter if your target system or compiler does not support hardware bit count
88 #if defined(_MSC_VER) && defined(_WIN32_WCE) // Visual Studio for Windows CE does not support Hardware bit count
89 # define LZ4_FORCE_SW_BITCOUNT
90 #endif
91 
92 
93 //**************************************
94 // Compiler Options
95 //**************************************
96 #if __STDC_VERSION__ >= 199901L // C99
97 /* "restrict" is a known keyword */
98 #else
99 # define restrict // Disable restrict
100 #endif
101 
102 #define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
103 
104 #ifdef _MSC_VER // Visual Studio
105 # include <intrin.h> // For Visual 2005
106 # if LZ4_ARCH64 // 64-bit
107 # pragma intrinsic(_BitScanForward64) // For Visual 2005
108 # pragma intrinsic(_BitScanReverse64) // For Visual 2005
109 # else
110 # pragma intrinsic(_BitScanForward) // For Visual 2005
111 # pragma intrinsic(_BitScanReverse) // For Visual 2005
112 # endif
113 #endif
114 
115 #ifdef _MSC_VER
116 # define lz4_bswap16(x) _byteswap_ushort(x)
117 #else
118 # define lz4_bswap16(x) ((unsigned short int) ((((x) >> 8) & 0xffu) | (((x) & 0xffu) << 8)))
119 #endif
120 
121 #if (GCC_VERSION >= 302) || (__INTEL_COMPILER >= 800) || defined(__clang__)
122 # define expect(expr,value) (__builtin_expect ((expr),(value)) )
123 #else
124 # define expect(expr,value) (expr)
125 #endif
126 
127 #define likely(expr) expect((expr) != 0, 1)
128 #define unlikely(expr) expect((expr) != 0, 0)
129 
130 
131 //**************************************
132 // Includes
133 //**************************************
134 #include <stdlib.h> // for malloc
135 #include <string.h> // for memset
136 #include "lz4.h"
137 
138 
139 //**************************************
140 // Basic Types
141 //**************************************
142 #if defined(_MSC_VER) // Visual Studio does not support 'stdint' natively
143 # define BYTE unsigned __int8
144 # define U16 unsigned __int16
145 # define U32 unsigned __int32
146 # define S32 __int32
147 # define U64 unsigned __int64
148 #else
149 # include <stdint.h>
150 # define BYTE uint8_t
151 # define U16 uint16_t
152 # define U32 uint32_t
153 # define S32 int32_t
154 # define U64 uint64_t
155 #endif
156 
157 #ifndef LZ4_FORCE_UNALIGNED_ACCESS
158 # pragma pack(push, 1)
159 #endif
160 
161 typedef struct _U16_S { U16 v; } U16_S;
162 typedef struct _U32_S { U32 v; } U32_S;
163 typedef struct _U64_S { U64 v; } U64_S;
164 
165 #ifndef LZ4_FORCE_UNALIGNED_ACCESS
166 # pragma pack(pop)
167 #endif
168 
169 #define A64(x) (((U64_S *)(x))->v)
170 #define A32(x) (((U32_S *)(x))->v)
171 #define A16(x) (((U16_S *)(x))->v)
172 
173 
174 //**************************************
175 // Constants
176 //**************************************
177 #define MINMATCH 4
178 
179 #define HASH_LOG (MEMORY_USAGE-2)
180 #define HASHTABLESIZE (1 << HASH_LOG)
181 #define HASH_MASK (HASHTABLESIZE - 1)
182 
183 // NOTCOMPRESSIBLE_DETECTIONLEVEL :
184 // Decreasing this value will make the algorithm skip faster data segments considered "incompressible"
185 // This may decrease compression ratio dramatically, but will be faster on incompressible data
186 // Increasing this value will make the algorithm search more before declaring a segment "incompressible"
187 // This could improve compression a bit, but will be slower on incompressible data
188 // The default value (6) is recommended
189 #define NOTCOMPRESSIBLE_DETECTIONLEVEL 6
190 #define SKIPSTRENGTH (NOTCOMPRESSIBLE_DETECTIONLEVEL>2?NOTCOMPRESSIBLE_DETECTIONLEVEL:2)
191 #define STACKLIMIT 13
192 #define HEAPMODE (HASH_LOG>STACKLIMIT) // Defines if memory is allocated into the stack (local variable), or into the heap (malloc()).
193 #define COPYLENGTH 8
194 #define LASTLITERALS 5
195 #define MFLIMIT (COPYLENGTH+MINMATCH)
196 #define MINLENGTH (MFLIMIT+1)
197 
198 #define MAXD_LOG 16
199 #define MAX_DISTANCE ((1 << MAXD_LOG) - 1)
200 
201 #define ML_BITS 4
202 #define ML_MASK ((1U<<ML_BITS)-1)
203 #define RUN_BITS (8-ML_BITS)
204 #define RUN_MASK ((1U<<RUN_BITS)-1)
205 
206 
207 //**************************************
208 // Architecture-specific macros
209 //**************************************
210 #if LZ4_ARCH64 // 64-bit
211 # define STEPSIZE 8
212 # define UARCH U64
213 # define AARCH A64
214 # define LZ4_COPYSTEP(s,d) A64(d) = A64(s); d+=8; s+=8;
215 # define LZ4_COPYPACKET(s,d) LZ4_COPYSTEP(s,d)
216 # define LZ4_SECURECOPY(s,d,e) if (d<e) LZ4_WILDCOPY(s,d,e)
217 # define HTYPE U32
218 # define INITBASE(base) const BYTE* const base = ip
219 #else // 32-bit
220 # define STEPSIZE 4
221 # define UARCH U32
222 # define AARCH A32
223 # define LZ4_COPYSTEP(s,d) A32(d) = A32(s); d+=4; s+=4;
224 # define LZ4_COPYPACKET(s,d) LZ4_COPYSTEP(s,d); LZ4_COPYSTEP(s,d);
225 # define LZ4_SECURECOPY LZ4_WILDCOPY
226 # define HTYPE const BYTE*
227 # define INITBASE(base) const int base = 0
228 #endif
229 
230 #if (defined(LZ4_BIG_ENDIAN) && !defined(BIG_ENDIAN_NATIVE_BUT_INCOMPATIBLE))
231 # define LZ4_READ_LITTLEENDIAN_16(d,s,p) { U16 v = A16(p); v = lz4_bswap16(v); d = (s) - v; }
232 # define LZ4_WRITE_LITTLEENDIAN_16(p,i) { U16 v = (U16)(i); v = lz4_bswap16(v); A16(p) = v; p+=2; }
233 #else // Little Endian
234 # define LZ4_READ_LITTLEENDIAN_16(d,s,p) { d = (s) - A16(p); }
235 # define LZ4_WRITE_LITTLEENDIAN_16(p,v) { A16(p) = v; p+=2; }
236 #endif
237 
238 
239 //**************************************
240 // Local structures
241 //**************************************
242 struct refTables
243 {
245 };
246 
247 
248 //**************************************
249 // Macros
250 //**************************************
251 #define LZ4_HASH_FUNCTION(i) (((i) * 2654435761U) >> ((MINMATCH*8)-HASH_LOG))
252 #define LZ4_HASH_VALUE(p) LZ4_HASH_FUNCTION(A32(p))
253 #define LZ4_WILDCOPY(s,d,e) do { LZ4_COPYPACKET(s,d) } while (d<e);
254 #define LZ4_BLINDCOPY(s,d,l) { BYTE* e=(d)+l; LZ4_WILDCOPY(s,d,e); d=e; }
255 
256 
257 //****************************
258 // Private functions
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)
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)
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  return DeBruijnBytePos[((U64)((val & -val) * 0x0218A392CDABBD3F)) >> 58];
288  #endif
289 #endif
290 }
291 
292 #else
293 
294 static inline int LZ4_NbCommonBytes (register U32 val)
295 {
296 #if defined(LZ4_BIG_ENDIAN)
297  #if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
298  unsigned long r = 0;
299  _BitScanReverse( &r, val );
300  return (int)(r>>3);
301  #elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
302  return (__builtin_clz(val) >> 3);
303  #else
304  int r;
305  if (!(val>>16)) { r=2; val>>=8; } else { r=0; val>>=24; }
306  r += (!val);
307  return r;
308  #endif
309 #else
310  #if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
311  unsigned long r;
312  _BitScanForward( &r, val );
313  return (int)(r>>3);
314  #elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
315  return (__builtin_ctz(val) >> 3);
316  #else
317  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 };
318  return DeBruijnBytePos[((U32)((val & -(S32)val) * 0x077CB531U)) >> 27];
319  #endif
320 #endif
321 }
322 
323 #endif
324 
325 
326 
327 //******************************
328 // Compression functions
329 //******************************
330 
331 // LZ4_compressCtx :
332 // -----------------
333 // Compress 'isize' bytes from 'source' into an output buffer 'dest' of maximum size 'maxOutputSize'.
334 // If it cannot achieve it, compression will stop, and result of the function will be zero.
335 // return : the number of bytes written in buffer 'dest', or 0 if the compression fails
336 
337 static inline int LZ4_compressCtx(void** ctx,
338  const char* source,
339  char* dest,
340  int isize,
341  int maxOutputSize)
342 {
343 #if HEAPMODE
344  struct refTables *srt = (struct refTables *) (*ctx);
345  HTYPE* HashTable;
346 #else
347  HTYPE HashTable[HASHTABLESIZE] = {0};
348 #endif
349 
350  const BYTE* ip = (BYTE*) source;
351  INITBASE(base);
352  const BYTE* anchor = ip;
353  const BYTE* const iend = ip + isize;
354  const BYTE* const mflimit = iend - MFLIMIT;
355 #define matchlimit (iend - LASTLITERALS)
356 
357  BYTE* op = (BYTE*) dest;
358  BYTE* const oend = op + maxOutputSize;
359 
360  int length;
361  const int skipStrength = SKIPSTRENGTH;
362  U32 forwardH;
363 
364 
365  // Init
366  if (isize<MINLENGTH) goto _last_literals;
367 #if HEAPMODE
368  if (*ctx == NULL)
369  {
370  srt = (struct refTables *) malloc ( sizeof(struct refTables) );
371  *ctx = (void*) srt;
372  }
373  HashTable = (HTYPE*)(srt->hashTable);
374  memset((void*)HashTable, 0, sizeof(srt->hashTable));
375 #else
376  (void) ctx;
377 #endif
378 
379 
380  // First Byte
381  HashTable[LZ4_HASH_VALUE(ip)] = ip - base;
382  ip++; forwardH = LZ4_HASH_VALUE(ip);
383 
384  // Main Loop
385  for ( ; ; )
386  {
387  int findMatchAttempts = (1U << skipStrength) + 3;
388  const BYTE* forwardIp = ip;
389  const BYTE* ref;
390  BYTE* token;
391 
392  // Find a match
393  do {
394  U32 h = forwardH;
395  int step = findMatchAttempts++ >> skipStrength;
396  ip = forwardIp;
397  forwardIp = ip + step;
398 
399  if unlikely(forwardIp > mflimit) { goto _last_literals; }
400 
401  forwardH = LZ4_HASH_VALUE(forwardIp);
402  ref = base + HashTable[h];
403  HashTable[h] = ip - base;
404 
405  } while ((ref < ip - MAX_DISTANCE) || (A32(ref) != A32(ip)));
406 
407  // Catch up
408  while ((ip>anchor) && (ref>(BYTE*)source) && unlikely(ip[-1]==ref[-1])) { ip--; ref--; }
409 
410  // Encode Literal length
411  length = (int)(ip - anchor);
412  token = op++;
413  if unlikely(op + length + (2 + 1 + LASTLITERALS) + (length>>8) > oend) return 0; // Check output limit
414 #ifdef _MSC_VER
415  if (length>=(int)RUN_MASK)
416  {
417  int len = length-RUN_MASK;
418  *token=(RUN_MASK<<ML_BITS);
419  if (len>254)
420  {
421  do { *op++ = 255; len -= 255; } while (len>254);
422  *op++ = (BYTE)len;
423  memcpy(op, anchor, length);
424  op += length;
425  goto _next_match;
426  }
427  else
428  *op++ = (BYTE)len;
429  }
430  else *token = (length<<ML_BITS);
431 #else
432  if (length>=(int)RUN_MASK)
433  {
434  int len;
435  *token=(RUN_MASK<<ML_BITS);
436  len = length-RUN_MASK;
437  for(; len > 254 ; len-=255) *op++ = 255;
438  *op++ = (BYTE)len;
439  }
440  else *token = (length<<ML_BITS);
441 #endif
442 
443  // Copy Literals
444  LZ4_BLINDCOPY(anchor, op, length);
445 
446 _next_match:
447  // Encode Offset
448  LZ4_WRITE_LITTLEENDIAN_16(op,(U16)(ip-ref));
449 
450  // Start Counting
451  ip+=MINMATCH; ref+=MINMATCH; // MinMatch already verified
452  anchor = ip;
453  while likely(ip<matchlimit-(STEPSIZE-1))
454  {
455  UARCH diff = AARCH(ref) ^ AARCH(ip);
456  if (!diff) { ip+=STEPSIZE; ref+=STEPSIZE; continue; }
457  ip += LZ4_NbCommonBytes(diff);
458  goto _endCount;
459  }
460  if (LZ4_ARCH64) if ((ip<(matchlimit-3)) && (A32(ref) == A32(ip))) { ip+=4; ref+=4; }
461  if ((ip<(matchlimit-1)) && (A16(ref) == A16(ip))) { ip+=2; ref+=2; }
462  if ((ip<matchlimit) && (*ref == *ip)) ip++;
463 _endCount:
464 
465  // Encode MatchLength
466  length = (int)(ip - anchor);
467  if unlikely(op + (1 + LASTLITERALS) + (length>>8) > oend) return 0; // Check output limit
468  if (length>=(int)ML_MASK)
469  {
470  *token += ML_MASK;
471  length -= ML_MASK;
472  for (; length > 509 ; length-=510) { *op++ = 255; *op++ = 255; }
473  if (length > 254) { length-=255; *op++ = 255; }
474  *op++ = (BYTE)length;
475  }
476  else *token += length;
477 
478  // Test end of chunk
479  if (ip > mflimit) { anchor = ip; break; }
480 
481  // Fill table
482  HashTable[LZ4_HASH_VALUE(ip-2)] = ip - 2 - base;
483 
484  // Test next position
485  ref = base + HashTable[LZ4_HASH_VALUE(ip)];
486  HashTable[LZ4_HASH_VALUE(ip)] = ip - base;
487  if ((ref > ip - (MAX_DISTANCE + 1)) && (A32(ref) == A32(ip))) { token = op++; *token=0; goto _next_match; }
488 
489  // Prepare next loop
490  anchor = ip++;
491  forwardH = LZ4_HASH_VALUE(ip);
492  }
493 
494 _last_literals:
495  // Encode Last Literals
496  {
497  int lastRun = (int)(iend - anchor);
498  if (((char*)op - dest) + lastRun + 1 + ((lastRun+255-RUN_MASK)/255) > (U32)maxOutputSize) return 0;
499  if (lastRun>=(int)RUN_MASK) { *op++=(RUN_MASK<<ML_BITS); lastRun-=RUN_MASK; for(; lastRun > 254 ; lastRun-=255) *op++ = 255; *op++ = (BYTE) lastRun; }
500  else *op++ = (lastRun<<ML_BITS);
501  memcpy(op, anchor, iend - anchor);
502  op += iend-anchor;
503  }
504 
505  // End
506  return (int) (((char*)op)-dest);
507 }
508 
509 
510 
511 // Note : this function is valid only if isize < LZ4_64KLIMIT
512 #define LZ4_64KLIMIT ((1<<16) + (MFLIMIT-1))
513 #define HASHLOG64K (HASH_LOG+1)
514 #define HASH64KTABLESIZE (1U<<HASHLOG64K)
515 #define LZ4_HASH64K_FUNCTION(i) (((i) * 2654435761U) >> ((MINMATCH*8)-HASHLOG64K))
516 #define LZ4_HASH64K_VALUE(p) LZ4_HASH64K_FUNCTION(A32(p))
517 static inline int LZ4_compress64kCtx(void** ctx,
518  const char* source,
519  char* dest,
520  int isize,
521  int maxOutputSize)
522 {
523 #if HEAPMODE
524  struct refTables *srt = (struct refTables *) (*ctx);
525  U16* HashTable;
526 #else
527  U16 HashTable[HASH64KTABLESIZE] = {0};
528 #endif
529 
530  const BYTE* ip = (BYTE*) source;
531  const BYTE* anchor = ip;
532  const BYTE* const base = ip;
533  const BYTE* const iend = ip + isize;
534  const BYTE* const mflimit = iend - MFLIMIT;
535 #define matchlimit (iend - LASTLITERALS)
536 
537  BYTE* op = (BYTE*) dest;
538  BYTE* const oend = op + maxOutputSize;
539 
540  int len, length;
541  const int skipStrength = SKIPSTRENGTH;
542  U32 forwardH;
543 
544 
545  // Init
546  if (isize<MINLENGTH) goto _last_literals;
547 #if HEAPMODE
548  if (*ctx == NULL)
549  {
550  srt = (struct refTables *) malloc ( sizeof(struct refTables) );
551  *ctx = (void*) srt;
552  }
553  HashTable = (U16*)(srt->hashTable);
554  memset((void*)HashTable, 0, sizeof(srt->hashTable));
555 #else
556  (void) ctx;
557 #endif
558 
559 
560  // First Byte
561  ip++; forwardH = LZ4_HASH64K_VALUE(ip);
562 
563  // Main Loop
564  for ( ; ; )
565  {
566  int findMatchAttempts = (1U << skipStrength) + 3;
567  const BYTE* forwardIp = ip;
568  const BYTE* ref;
569  BYTE* token;
570 
571  // Find a match
572  do {
573  U32 h = forwardH;
574  int step = findMatchAttempts++ >> skipStrength;
575  ip = forwardIp;
576  forwardIp = ip + step;
577 
578  if (forwardIp > mflimit) { goto _last_literals; }
579 
580  forwardH = LZ4_HASH64K_VALUE(forwardIp);
581  ref = base + HashTable[h];
582  HashTable[h] = (U16)(ip - base);
583 
584  } while (A32(ref) != A32(ip));
585 
586  // Catch up
587  while ((ip>anchor) && (ref>(BYTE*)source) && (ip[-1]==ref[-1])) { ip--; ref--; }
588 
589  // Encode Literal length
590  length = (int)(ip - anchor);
591  token = op++;
592  if unlikely(op + length + (2 + 1 + LASTLITERALS) + (length>>8) > oend) return 0; // Check output limit
593 #ifdef _MSC_VER
594  if (length>=(int)RUN_MASK)
595  {
596  int len = length-RUN_MASK;
597  *token=(RUN_MASK<<ML_BITS);
598  if (len>254)
599  {
600  do { *op++ = 255; len -= 255; } while (len>254);
601  *op++ = (BYTE)len;
602  memcpy(op, anchor, length);
603  op += length;
604  goto _next_match;
605  }
606  else
607  *op++ = (BYTE)len;
608  }
609  else *token = (length<<ML_BITS);
610 #else
611  if (length>=(int)RUN_MASK) { *token=(RUN_MASK<<ML_BITS); len = length-RUN_MASK; for(; len > 254 ; len-=255) *op++ = 255; *op++ = (BYTE)len; }
612  else *token = (length<<ML_BITS);
613 #endif
614 
615  // Copy Literals
616  LZ4_BLINDCOPY(anchor, op, length);
617 
618 _next_match:
619  // Encode Offset
620  LZ4_WRITE_LITTLEENDIAN_16(op,(U16)(ip-ref));
621 
622  // Start Counting
623  ip+=MINMATCH; ref+=MINMATCH; // MinMatch verified
624  anchor = ip;
625  while (ip<matchlimit-(STEPSIZE-1))
626  {
627  UARCH diff = AARCH(ref) ^ AARCH(ip);
628  if (!diff) { ip+=STEPSIZE; ref+=STEPSIZE; continue; }
629  ip += LZ4_NbCommonBytes(diff);
630  goto _endCount;
631  }
632  if (LZ4_ARCH64) if ((ip<(matchlimit-3)) && (A32(ref) == A32(ip))) { ip+=4; ref+=4; }
633  if ((ip<(matchlimit-1)) && (A16(ref) == A16(ip))) { ip+=2; ref+=2; }
634  if ((ip<matchlimit) && (*ref == *ip)) ip++;
635 _endCount:
636 
637  // Encode MatchLength
638  len = (int)(ip - anchor);
639  if unlikely(op + (1 + LASTLITERALS) + (len>>8) > oend) return 0; // Check output limit
640  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; }
641  else *token += len;
642 
643  // Test end of chunk
644  if (ip > mflimit) { anchor = ip; break; }
645 
646  // Fill table
647  HashTable[LZ4_HASH64K_VALUE(ip-2)] = (U16)(ip - 2 - base);
648 
649  // Test next position
650  ref = base + HashTable[LZ4_HASH64K_VALUE(ip)];
651  HashTable[LZ4_HASH64K_VALUE(ip)] = (U16)(ip - base);
652  if (A32(ref) == A32(ip)) { token = op++; *token=0; goto _next_match; }
653 
654  // Prepare next loop
655  anchor = ip++;
656  forwardH = LZ4_HASH64K_VALUE(ip);
657  }
658 
659 _last_literals:
660  // Encode Last Literals
661  {
662  int lastRun = (int)(iend - anchor);
663  if (op + lastRun + 1 + (lastRun-RUN_MASK+255)/255 > oend) return 0;
664  if (lastRun>=(int)RUN_MASK) { *op++=(RUN_MASK<<ML_BITS); lastRun-=RUN_MASK; for(; lastRun > 254 ; lastRun-=255) *op++ = 255; *op++ = (BYTE) lastRun; }
665  else *op++ = (lastRun<<ML_BITS);
666  memcpy(op, anchor, iend - anchor);
667  op += iend-anchor;
668  }
669 
670  // End
671  return (int) (((char*)op)-dest);
672 }
673 
674 
675 int LZ4_compress_limitedOutput(const char* source,
676  char* dest,
677  int isize,
678  int maxOutputSize)
679 {
680 #if HEAPMODE
681  void* ctx = malloc(sizeof(struct refTables));
682  int result;
683  if (isize < LZ4_64KLIMIT)
684  result = LZ4_compress64kCtx(&ctx, source, dest, isize, maxOutputSize);
685  else result = LZ4_compressCtx(&ctx, source, dest, isize, maxOutputSize);
686  free(ctx);
687  return result;
688 #else
689  if (isize < (int)LZ4_64KLIMIT) return LZ4_compress64kCtx(NULL, source, dest, isize, maxOutputSize);
690  return LZ4_compressCtx(NULL, source, dest, isize, maxOutputSize);
691 #endif
692 }
693 
694 
695 int LZ4_compress(const char* source,
696  char* dest,
697  int isize)
698 {
699  return LZ4_compress_limitedOutput(source, dest, isize, LZ4_compressBound(isize));
700 }
701 
702 
703 
704 
705 //****************************
706 // Decompression functions
707 //****************************
708 
709 // Note : The decoding functions LZ4_uncompress() and LZ4_uncompress_unknownOutputSize()
710 // are safe against "buffer overflow" attack type.
711 // They will never write nor read outside of the provided output buffers.
712 // LZ4_uncompress_unknownOutputSize() also insures that it will never read outside of the input buffer.
713 // A corrupted input will produce an error result, a negative int, indicating the position of the error within input stream.
714 
715 int LZ4_uncompress(const char* source,
716  char* dest,
717  int osize)
718 {
719  // Local Variables
720  const BYTE* restrict ip = (const BYTE*) source;
721  const BYTE* ref;
722 
723  BYTE* op = (BYTE*) dest;
724  BYTE* const oend = op + osize;
725  BYTE* cpy;
726 
727  unsigned token;
728 
729  size_t dec32table[] = {0, 3, 2, 3, 0, 0, 0, 0};
730 #if LZ4_ARCH64
731  size_t dec64table[] = {0, 0, 0, -1, 0, 1, 2, 3};
732 #endif
733 
734 
735  // Main Loop
736  while (1)
737  {
738  size_t length;
739 
740  // get runlength
741  token = *ip++;
742  if ((length=(token>>ML_BITS)) == RUN_MASK) { size_t len; for (;(len=*ip++)==255;length+=255){} length += len; }
743 
744  // copy literals
745  cpy = op+length;
746  if (cpy>oend-COPYLENGTH)
747  {
748  if (cpy != oend) goto _output_error; // Error : not enough place for another match (min 4) + 5 literals
749  memcpy(op, ip, length);
750  ip += length;
751  break; // EOF
752  }
753  LZ4_WILDCOPY(ip, op, cpy); ip -= (op-cpy); op = cpy;
754 
755  // get offset
756  LZ4_READ_LITTLEENDIAN_16(ref,cpy,ip); ip+=2;
757  if unlikely(ref < (BYTE* const)dest) goto _output_error; // Error : offset outside destination buffer
758 
759  // get matchlength
760  if ((length=(token&ML_MASK)) == ML_MASK) { for (;*ip==255;length+=255) {ip++;} length += *ip++; }
761 
762  // copy repeated sequence
763  if unlikely((op-ref)<STEPSIZE)
764  {
765 #if LZ4_ARCH64
766  size_t dec64 = dec64table[op-ref];
767 #else
768  const int dec64 = 0;
769 #endif
770  op[0] = ref[0];
771  op[1] = ref[1];
772  op[2] = ref[2];
773  op[3] = ref[3];
774  op += 4, ref += 4; ref -= dec32table[op-ref];
775  A32(op) = A32(ref);
776  op += STEPSIZE-4; ref -= dec64;
777  } else { LZ4_COPYSTEP(ref,op); }
778  cpy = op + length - (STEPSIZE-4);
779 
780  if unlikely(cpy>oend-(COPYLENGTH)-(STEPSIZE-4))
781  {
782  if (cpy > oend-LASTLITERALS) goto _output_error; // Error : last 5 bytes must be literals
783  LZ4_SECURECOPY(ref, op, (oend-COPYLENGTH));
784  while(op<cpy) *op++=*ref++;
785  op=cpy;
786  continue;
787  }
788 
789  LZ4_WILDCOPY(ref, op, cpy);
790  op=cpy; // correction
791  }
792 
793  // end of decoding
794  return (int) (((char*)ip)-source);
795 
796  // write overflow error detected
797 _output_error:
798  return (int) (-(((char*)ip)-source));
799 }
800 
801 
803  const char* source,
804  char* dest,
805  int isize,
806  int maxOutputSize)
807 {
808  // Local Variables
809  const BYTE* restrict ip = (const BYTE*) source;
810  const BYTE* const iend = ip + isize;
811  const BYTE* ref;
812 
813  BYTE* op = (BYTE*) dest;
814  BYTE* const oend = op + maxOutputSize;
815  BYTE* cpy;
816 
817  size_t dec32table[] = {0, 3, 2, 3, 0, 0, 0, 0};
818 #if LZ4_ARCH64
819  size_t dec64table[] = {0, 0, 0, -1, 0, 1, 2, 3};
820 #endif
821 
822 
823  // Special case
824  if unlikely(ip==iend) goto _output_error; // A correctly formed null-compressed LZ4 must have at least one byte (token=0)
825 
826  // Main Loop
827  while (1)
828  {
829  unsigned token;
830  size_t length;
831 
832  // get runlength
833  token = *ip++;
834  if ((length=(token>>ML_BITS)) == RUN_MASK)
835  {
836  int s=255;
837  while (likely(ip<iend) && (s==255)) { s=*ip++; length += s; }
838  }
839 
840  // copy literals
841  cpy = op+length;
842  if ((cpy>oend-MFLIMIT) || (ip+length>iend-(2+1+LASTLITERALS)))
843  {
844  if (cpy > oend) goto _output_error; // Error : writes beyond output buffer
845  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)
846  memcpy(op, ip, length);
847  op += length;
848  break; // Necessarily EOF, due to parsing restrictions
849  }
850  LZ4_WILDCOPY(ip, op, cpy); ip -= (op-cpy); op = cpy;
851 
852  // get offset
853  LZ4_READ_LITTLEENDIAN_16(ref,cpy,ip); ip+=2;
854  if unlikely(ref < (BYTE* const)dest) goto _output_error; // Error : offset outside of destination buffer
855 
856  // get matchlength
857  if ((length=(token&ML_MASK)) == ML_MASK)
858  {
859  while likely(ip<iend-(LASTLITERALS+1)) // Error : a minimum input bytes must remain for LASTLITERALS + token
860  {
861  int s = *ip++;
862  length +=s;
863  if (s==255) continue;
864  break;
865  }
866  }
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  if unlikely(cpy>oend-(COPYLENGTH+(STEPSIZE-4)))
887  {
888  if (cpy > oend-LASTLITERALS) goto _output_error; // Error : last 5 bytes must be literals
889  LZ4_SECURECOPY(ref, op, (oend-COPYLENGTH));
890  while(op<cpy) *op++=*ref++;
891  op=cpy;
892  continue;
893  }
894 
895  LZ4_WILDCOPY(ref, op, cpy);
896  op=cpy; // correction
897  }
898 
899  // end of decoding
900  return (int) (((char*)op)-dest);
901 
902  // write overflow error detected
903 _output_error:
904  return (int) (-(((char*)ip)-source));
905 }
906