Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
lz4hc.c
Go to the documentation of this file.
1 /*
2  LZ4 HC - High Compression Mode of LZ4
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 
35 #ifndef LZ4_CS_ADAPTER
36 //**************************************
37 // CPU Feature Detection
38 //**************************************
39 // 32 or 64 bits ?
40 #ifndef LZ4_ARCH64
41 
42 #if (defined(__x86_64__) || defined(__x86_64) || defined(__amd64__) || defined(__amd64) || defined(__ppc64__) || defined(_WIN64) || defined(__LP64__) || defined(_LP64) ) // Detects 64 bits mode
43 #define LZ4_ARCH64 1
44 #else
45 #define LZ4_ARCH64 0
46 #endif
47 
48 #endif
49 
50 // Little Endian or Big Endian ?
51 // Overwrite the #define below if you know your architecture endianess
52 #if defined (__GLIBC__)
53 # include <endian.h>
54 # if (__BYTE_ORDER == __BIG_ENDIAN)
55 # define LZ4_BIG_ENDIAN 1
56 # endif
57 #elif (defined(__BIG_ENDIAN__) || defined(__BIG_ENDIAN) || defined(_BIG_ENDIAN)) && !(defined(__LITTLE_ENDIAN__) || defined(__LITTLE_ENDIAN) || defined(_LITTLE_ENDIAN))
58 # define LZ4_BIG_ENDIAN 1
59 #elif defined(__sparc) || defined(__sparc__) \
60  || defined(__ppc__) || defined(_POWER) || defined(__powerpc__) || defined(_ARCH_PPC) || defined(__PPC__) || defined(__PPC) || defined(PPC) || defined(__powerpc__) || defined(__powerpc) || defined(powerpc) \
61  || defined(__hpux) || defined(__hppa) \
62  || defined(_MIPSEB) || defined(__s390__)
63 #define LZ4_BIG_ENDIAN 1
64 #else
65 // Little Endian assumed. PDP Endian and other very rare endian format are unsupported.
66 #endif
67 
68 // Unaligned memory access is automatically enabled for "common" CPU, such as x86.
69 // For others CPU, the compiler will be more cautious, and insert extra code to ensure aligned access is respected
70 // If you know your target CPU supports unaligned memory access, you want to force this option manually to improve performance
71 #if defined(__ARM_FEATURE_UNALIGNED)
72 #define LZ4_FORCE_UNALIGNED_ACCESS 1
73 #endif
74 
75 // Define this parameter if your target system or compiler does not support hardware bit count
76 #if defined(_MSC_VER) && defined(_WIN32_WCE) // Visual Studio for Windows CE does not support Hardware bit count
77 # define LZ4_FORCE_SW_BITCOUNT
78 #endif
79 
80 
81 //**************************************
82 // Compiler Options
83 //**************************************
84 #if __STDC_VERSION__ >= 199901L // C99
85  /* "restrict" is a known keyword */
86 #else
87 #define restrict // Disable restrict
88 #endif
89 
90 #ifdef _MSC_VER
91  #define inline __inline // Visual is not C99, but supports some kind of inline
92  #define forceinline __forceinline
93 #else
94 # ifdef __GNUC__
95 # define forceinline inline __attribute__((always_inline))
96 # else
97 # define forceinline inline
98 # endif
99 #endif
100 
101 #if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT) // Visual Studio
102 #include <intrin.h> // For Visual 2005
103 # if LZ4_ARCH64 && defined(_M_X64) // 64-bit
104 # pragma intrinsic(_BitScanForward64) // For Visual 2005
105 # pragma intrinsic(_BitScanReverse64) // For Visual 2005
106 # else
107 # pragma intrinsic(_BitScanForward) // For Visual 2005
108 # pragma intrinsic(_BitScanReverse) // For Visual 2005
109 # endif
110 # pragma warning(disable : 4127) // disable: C4127: conditional expression is constant
111 # pragma warning(disable : 4701) // disable: C4701: potentially uninitialized local variable used
112 #endif
113 
114 #ifdef _MSC_VER // Visual Studio
115 #define lz4_bswap16(x) _byteswap_ushort(x)
116 #else
117 #define lz4_bswap16(x) ((unsigned short int) ((((x) >> 8) & 0xffu) | (((x) & 0xffu) << 8)))
118 #endif
119 
120 
121 //**************************************
122 // Includes
123 //**************************************
124 #include <stdlib.h> // calloc, free
125 #include <string.h> // memset, memcpy
126 #include "lz4hc.h"
127 
128 #define ALLOCATOR(s) calloc(1,s)
129 #define FREEMEM free
130 #define MEM_INIT memset
131 
132 
133 //**************************************
134 // Basic Types
135 //**************************************
136 #if defined(_MSC_VER) // Visual Studio does not support 'stdint' natively
137 #define BYTE unsigned __int8
138 #define U16 unsigned __int16
139 #define U32 unsigned __int32
140 #define S32 __int32
141 #define U64 unsigned __int64
142 #define S64 __int64
143 #else
144 #include <stdint.h>
145 #define BYTE uint8_t
146 #define U16 uint16_t
147 #define U32 uint32_t
148 #define S32 int32_t
149 #define U64 uint64_t
150 #define S64 int64_t
151 #endif
152 
153 #ifndef LZ4_FORCE_UNALIGNED_ACCESS
154 #pragma pack(push, 1)
155 #endif
156 
157 typedef struct _U16_S { U16 v; } U16_S;
158 typedef struct _U32_S { U32 v; } U32_S;
159 typedef struct _U64_S { U64 v; } U64_S;
160 
161 #ifndef LZ4_FORCE_UNALIGNED_ACCESS
162 #pragma pack(pop)
163 #endif
164 
165 #define A64(x) (((U64_S *)(x))->v)
166 #define A32(x) (((U32_S *)(x))->v)
167 #define A16(x) (((U16_S *)(x))->v)
168 
169 
170 //**************************************
171 // Constants
172 //**************************************
173 #define MINMATCH 4
174 
175 #define DICTIONARY_LOGSIZE 16
176 #define MAXD (1<<DICTIONARY_LOGSIZE)
177 #define MAXD_MASK ((U32)(MAXD - 1))
178 #define MAX_DISTANCE (MAXD - 1)
179 
180 #define HASH_LOG (DICTIONARY_LOGSIZE-1)
181 #define HASHTABLESIZE (1 << HASH_LOG)
182 #define HASH_MASK (HASHTABLESIZE - 1)
183 
184 #define MAX_NB_ATTEMPTS 256
185 
186 #define ML_BITS 4
187 #define ML_MASK (size_t)((1U<<ML_BITS)-1)
188 #define RUN_BITS (8-ML_BITS)
189 #define RUN_MASK ((1U<<RUN_BITS)-1)
190 
191 #define COPYLENGTH 8
192 #define LASTLITERALS 5
193 #define MFLIMIT (COPYLENGTH+MINMATCH)
194 #define MINLENGTH (MFLIMIT+1)
195 #define OPTIMAL_ML (int)((ML_MASK-1)+MINMATCH)
196 
197 
198 //**************************************
199 // Architecture-specific macros
200 //**************************************
201 #if LZ4_ARCH64 // 64-bit
202 #define STEPSIZE 8
203 #define LZ4_COPYSTEP(s,d) A64(d) = A64(s); d+=8; s+=8;
204 #define LZ4_COPYPACKET(s,d) LZ4_COPYSTEP(s,d)
205 #define UARCH U64
206 #define AARCH A64
207 #define HTYPE U32
208 #define INITBASE(b,s) const BYTE* const b = s
209 #else // 32-bit
210 #define STEPSIZE 4
211 #define LZ4_COPYSTEP(s,d) A32(d) = A32(s); d+=4; s+=4;
212 #define LZ4_COPYPACKET(s,d) LZ4_COPYSTEP(s,d); LZ4_COPYSTEP(s,d);
213 #define UARCH U32
214 #define AARCH A32
215 #define HTYPE const BYTE*
216 #define INITBASE(b,s) const int b = 0
217 #endif
218 
219 #if defined(LZ4_BIG_ENDIAN)
220 #define LZ4_READ_LITTLEENDIAN_16(d,s,p) { U16 v = A16(p); v = lz4_bswap16(v); d = (s) - v; }
221 #define LZ4_WRITE_LITTLEENDIAN_16(p,i) { U16 v = (U16)(i); v = lz4_bswap16(v); A16(p) = v; p+=2; }
222 #else // Little Endian
223 #define LZ4_READ_LITTLEENDIAN_16(d,s,p) { d = (s) - A16(p); }
224 #define LZ4_WRITE_LITTLEENDIAN_16(p,v) { A16(p) = v; p+=2; }
225 #endif
226 
227 
228 //************************************************************
229 // Local Types
230 //************************************************************
231 typedef struct
232 {
233  const BYTE* base;
234  HTYPE hashTable[HASHTABLESIZE];
235  U16 chainTable[MAXD];
238 
239 
240 //**************************************
241 // Macros
242 //**************************************
243 #define LZ4_WILDCOPY(s,d,e) do { LZ4_COPYPACKET(s,d) } while (d<e);
244 #define LZ4_BLINDCOPY(s,d,l) { BYTE* e=d+l; LZ4_WILDCOPY(s,d,e); d=e; }
245 #define HASH_FUNCTION(i) (((i) * 2654435761U) >> ((MINMATCH*8)-HASH_LOG))
246 #define HASH_VALUE(p) HASH_FUNCTION(A32(p))
247 #define HASH_POINTER(p) (HashTable[HASH_VALUE(p)] + base)
248 #define DELTANEXT(p) chainTable[(size_t)(p) & MAXD_MASK]
249 #define GETNEXT(p) ((p) - (size_t)DELTANEXT(p))
250 
251 
252 //**************************************
253 // Private functions
254 //**************************************
255 #if LZ4_ARCH64
256 
257 inline static int LZ4_NbCommonBytes (register U64 val)
258 {
259 #if defined(LZ4_BIG_ENDIAN)
260  #if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT) && defined(_M_X64)
261  unsigned long r = 0;
262  _BitScanReverse64( &r, val );
263  return (int)(r>>3);
264  #elif defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
265  return (__builtin_clzll(val) >> 3);
266  #else
267  int r;
268  if (!(val>>32)) { r=4; } else { r=0; val>>=32; }
269  if (!(val>>16)) { r+=2; val>>=8; } else { val>>=24; }
270  r += (!val);
271  return r;
272  #endif
273 #else
274  #if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT) && defined(_M_X64)
275  unsigned long r = 0;
276  _BitScanForward64( &r, val );
277  return (int)(r>>3);
278  #elif defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
279  return (__builtin_ctzll(val) >> 3);
280  #else
281  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 };
282 #ifdef LZ4_MK_OPT
283  return DeBruijnBytePos[((U64)((U64)((S64)val & -(S64)val) * 0x0218A392CDABBD3F)) >> 58];
284 #else
285  return DeBruijnBytePos[((U64)((val & -val) * 0x0218A392CDABBD3F)) >> 58];
286 #endif
287  #endif
288 #endif
289 }
290 
291 #else
292 
293 inline static int LZ4_NbCommonBytes (register U32 val)
294 {
295 #if defined(LZ4_BIG_ENDIAN)
296  #if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
297  unsigned long r;
298  _BitScanReverse( &r, val );
299  return (int)(r>>3);
300  #elif defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
301  return (__builtin_clz(val) >> 3);
302  #else
303  int r;
304  if (!(val>>16)) { r=2; val>>=8; } else { r=0; val>>=24; }
305  r += (!val);
306  return r;
307  #endif
308 #else
309  #if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
310  unsigned long r;
311  _BitScanForward( &r, val );
312  return (int)(r>>3);
313  #elif defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
314  return (__builtin_ctz(val) >> 3);
315  #else
316  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 };
317 #ifdef LZ4_MK_OPT
318  return DeBruijnBytePos[((U32)((U32)((S32)val & -(S32)val) * 0x077CB531U)) >> 27];
319 #else
320  return DeBruijnBytePos[((U32)((val & -(S32)val) * 0x077CB531U)) >> 27];
321 #endif
322  #endif
323 #endif
324 }
325 
326 #endif
327 
328 #endif // LZ4_CS_ADAPTER
329 
330 inline static int LZ4HC_Init (LZ4HC_Data_Structure* hc4, const BYTE* base)
331 {
332  MEM_INIT((void*)hc4->hashTable, 0, sizeof(hc4->hashTable));
333  MEM_INIT(hc4->chainTable, 0xFF, sizeof(hc4->chainTable));
334  hc4->nextToUpdate = base + LZ4_ARCH64;
335  hc4->base = base;
336  return 1;
337 }
338 
339 
340 inline static void* LZ4HC_Create (const BYTE* base)
341 {
342  void* hc4 = ALLOCATOR(sizeof(LZ4HC_Data_Structure));
343 
344  LZ4HC_Init ((LZ4HC_Data_Structure*)hc4, base);
345  return hc4;
346 }
347 
348 
349 inline static int LZ4HC_Free (void** LZ4HC_Data)
350 {
351  FREEMEM(*LZ4HC_Data);
352  *LZ4HC_Data = NULL;
353  return (1);
354 }
355 
356 
357 // Update chains up to ip (excluded)
358 forceinline static void LZ4HC_Insert (LZ4HC_Data_Structure* hc4, const BYTE* ip)
359 {
360  U16* chainTable = hc4->chainTable;
361  HTYPE* HashTable = hc4->hashTable;
362  INITBASE(base,hc4->base);
363 
364  while(hc4->nextToUpdate < ip)
365  {
366  const BYTE* p = hc4->nextToUpdate;
367  size_t delta = (p) - HASH_POINTER(p);
368  if (delta>MAX_DISTANCE) delta = MAX_DISTANCE;
369  DELTANEXT(p) = (U16)delta;
370  HashTable[HASH_VALUE(p)] = (HTYPE)((p) - base);
371  hc4->nextToUpdate++;
372  }
373 }
374 
375 
376 forceinline static size_t LZ4HC_CommonLength (const BYTE* p1, const BYTE* p2, const BYTE* const matchlimit)
377 {
378  const BYTE* p1t = p1;
379 
380  while (p1t<matchlimit-(STEPSIZE-1))
381  {
382  UARCH diff = AARCH(p2) ^ AARCH(p1t);
383  if (!diff) { p1t+=STEPSIZE; p2+=STEPSIZE; continue; }
384  p1t += LZ4_NbCommonBytes(diff);
385  return (p1t - p1);
386  }
387  if (LZ4_ARCH64) if ((p1t<(matchlimit-3)) && (A32(p2) == A32(p1t))) { p1t+=4; p2+=4; }
388  if ((p1t<(matchlimit-1)) && (A16(p2) == A16(p1t))) { p1t+=2; p2+=2; }
389  if ((p1t<matchlimit) && (*p2 == *p1t)) p1t++;
390  return (p1t - p1);
391 }
392 
393 
394 forceinline static int LZ4HC_InsertAndFindBestMatch (LZ4HC_Data_Structure* hc4, const BYTE* ip, const BYTE* const matchlimit, const BYTE** matchpos)
395 {
396  U16* const chainTable = hc4->chainTable;
397  HTYPE* const HashTable = hc4->hashTable;
398  const BYTE* ref;
399  INITBASE(base,hc4->base);
400  int nbAttempts=MAX_NB_ATTEMPTS;
401  size_t repl=0, ml=0;
402  U16 delta;
403 
404  // HC4 match finder
405  LZ4HC_Insert(hc4, ip);
406  ref = HASH_POINTER(ip);
407 
408 #define REPEAT_OPTIMIZATION
409 #ifdef REPEAT_OPTIMIZATION
410  // Detect repetitive sequences of length <= 4
411  if (ref >= ip-4) // potential repetition
412  {
413  if (A32(ref) == A32(ip)) // confirmed
414  {
415  delta = (U16)(ip-ref);
416  repl = ml = LZ4HC_CommonLength(ip+MINMATCH, ref+MINMATCH, matchlimit) + MINMATCH;
417  *matchpos = ref;
418  }
419  ref = GETNEXT(ref);
420  }
421 #endif
422 
423  while ((ref >= ip-MAX_DISTANCE) && (nbAttempts))
424  {
425  nbAttempts--;
426  if (*(ref+ml) == *(ip+ml))
427  if (A32(ref) == A32(ip))
428  {
429  size_t mlt = LZ4HC_CommonLength(ip+MINMATCH, ref+MINMATCH, matchlimit) + MINMATCH;
430  if (mlt > ml) { ml = mlt; *matchpos = ref; }
431  }
432  ref = GETNEXT(ref);
433  }
434 
435 #ifdef REPEAT_OPTIMIZATION
436  // Complete table
437  if (repl)
438  {
439  const BYTE* ptr = ip;
440  const BYTE* end;
441 
442  end = ip + repl - (MINMATCH-1);
443  while(ptr < end-delta)
444  {
445  DELTANEXT(ptr) = delta; // Pre-Load
446  ptr++;
447  }
448  do
449  {
450  DELTANEXT(ptr) = delta;
451  HashTable[HASH_VALUE(ptr)] = (HTYPE)((ptr) - base); // Head of chain
452  ptr++;
453  } while(ptr < end);
454  hc4->nextToUpdate = end;
455  }
456 #endif
457 
458  return (int)ml;
459 }
460 
461 
462 forceinline static int LZ4HC_InsertAndGetWiderMatch (LZ4HC_Data_Structure* hc4, const BYTE* ip, const BYTE* startLimit, const BYTE* matchlimit, int longest, const BYTE** matchpos, const BYTE** startpos)
463 {
464  U16* const chainTable = hc4->chainTable;
465  HTYPE* const HashTable = hc4->hashTable;
466  INITBASE(base,hc4->base);
467  const BYTE* ref;
468  int nbAttempts = MAX_NB_ATTEMPTS;
469  int delta = (int)(ip-startLimit);
470 
471  // First Match
472  LZ4HC_Insert(hc4, ip);
473  ref = HASH_POINTER(ip);
474 
475  while ((ref >= ip-MAX_DISTANCE) && (nbAttempts))
476  {
477  nbAttempts--;
478  if (*(startLimit + longest) == *(ref - delta + longest))
479  if (A32(ref) == A32(ip))
480  {
481 #if 1
482  const BYTE* reft = ref+MINMATCH;
483  const BYTE* ipt = ip+MINMATCH;
484  const BYTE* startt = ip;
485 
486  while (ipt<matchlimit-(STEPSIZE-1))
487  {
488  UARCH diff = AARCH(reft) ^ AARCH(ipt);
489  if (!diff) { ipt+=STEPSIZE; reft+=STEPSIZE; continue; }
490  ipt += LZ4_NbCommonBytes(diff);
491  goto _endCount;
492  }
493  if (LZ4_ARCH64) if ((ipt<(matchlimit-3)) && (A32(reft) == A32(ipt))) { ipt+=4; reft+=4; }
494  if ((ipt<(matchlimit-1)) && (A16(reft) == A16(ipt))) { ipt+=2; reft+=2; }
495  if ((ipt<matchlimit) && (*reft == *ipt)) ipt++;
496 _endCount:
497  reft = ref;
498 #else
499  // Easier for code maintenance, but unfortunately slower too
500  const BYTE* startt = ip;
501  const BYTE* reft = ref;
502  const BYTE* ipt = ip + MINMATCH + LZ4HC_CommonLength(ip+MINMATCH, ref+MINMATCH, matchlimit);
503 #endif
504 
505  while ((startt>startLimit) && (reft > hc4->base) && (startt[-1] == reft[-1])) {startt--; reft--;}
506 
507  if ((ipt-startt) > longest)
508  {
509  longest = (int)(ipt-startt);
510  *matchpos = reft;
511  *startpos = startt;
512  }
513  }
514  ref = GETNEXT(ref);
515  }
516 
517  return longest;
518 }
519 
520 
521 forceinline static int LZ4_encodeSequence(const BYTE** ip, BYTE** op, const BYTE** anchor, int matchLength, const BYTE* ref, BYTE* oend)
522 {
523  int length, len;
524  BYTE* token;
525 
526  // Encode Literal length
527  length = (int)(*ip - *anchor);
528  token = (*op)++;
529  if ((*op + length + (2 + 1 + LASTLITERALS) + (length>>8)) > oend) return 1; // Check output limit
530  if (length>=(int)RUN_MASK) { *token=(RUN_MASK<<ML_BITS); len = length-RUN_MASK; for(; len > 254 ; len-=255) *(*op)++ = 255; *(*op)++ = (BYTE)len; }
531  else *token = (BYTE)(length<<ML_BITS);
532 
533  // Copy Literals
534  LZ4_BLINDCOPY(*anchor, *op, length);
535 
536  // Encode Offset
537  LZ4_WRITE_LITTLEENDIAN_16(*op,(U16)(*ip-ref));
538 
539  // Encode MatchLength
540  len = (int)(matchLength-MINMATCH);
541  if (*op + (1 + LASTLITERALS) + (length>>8) > oend) return 1; // Check output limit
542  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; }
543  else *token += (BYTE)len;
544 
545  // Prepare next loop
546  *ip += matchLength;
547  *anchor = *ip;
548 
549  return 0;
550 }
551 
552 
553 //****************************
554 // Compression CODE
555 //****************************
556 
558  const char* source,
559  char* dest,
560  int inputSize,
561  int maxOutputSize)
562 {
563  const BYTE* ip = (const BYTE*) source;
564  const BYTE* anchor = ip;
565  const BYTE* const iend = ip + inputSize;
566  const BYTE* const mflimit = iend - MFLIMIT;
567  const BYTE* const matchlimit = (iend - LASTLITERALS);
568 
569  BYTE* op = (BYTE*) dest;
570  BYTE* const oend = op + maxOutputSize;
571 
572  int ml, ml2, ml3, ml0;
573  const BYTE* ref=NULL;
574  const BYTE* start2=NULL;
575  const BYTE* ref2=NULL;
576  const BYTE* start3=NULL;
577  const BYTE* ref3=NULL;
578  const BYTE* start0;
579  const BYTE* ref0;
580 
581  ip++;
582 
583  // Main Loop
584  while (ip < mflimit)
585  {
586  ml = LZ4HC_InsertAndFindBestMatch (ctx, ip, matchlimit, (&ref));
587  if (!ml) { ip++; continue; }
588 
589  // saved, in case we would skip too much
590  start0 = ip;
591  ref0 = ref;
592  ml0 = ml;
593 
594 _Search2:
595  if (ip+ml < mflimit)
596  ml2 = LZ4HC_InsertAndGetWiderMatch(ctx, ip + ml - 2, ip + 1, matchlimit, ml, &ref2, &start2);
597  else ml2=ml;
598 
599  if (ml2 == ml) // No better match
600  {
601  if (LZ4_encodeSequence(&ip, &op, &anchor, ml, ref, oend)) return 0;
602  continue;
603  }
604 
605  if (start0 < ip)
606  {
607  if (start2 < ip + ml0) // empirical
608  {
609  ip = start0;
610  ref = ref0;
611  ml = ml0;
612  }
613  }
614 
615  // Here, start0==ip
616  if ((start2 - ip) < 3) // First Match too small : removed
617  {
618  ml = ml2;
619  ip = start2;
620  ref =ref2;
621  goto _Search2;
622  }
623 
624 _Search3:
625  // Currently we have :
626  // ml2 > ml1, and
627  // ip1+3 <= ip2 (usually < ip1+ml1)
628  if ((start2 - ip) < OPTIMAL_ML)
629  {
630  int correction;
631  int new_ml = ml;
632  if (new_ml > OPTIMAL_ML) new_ml = OPTIMAL_ML;
633  if (ip+new_ml > start2 + ml2 - MINMATCH) new_ml = (int)(start2 - ip) + ml2 - MINMATCH;
634  correction = new_ml - (int)(start2 - ip);
635  if (correction > 0)
636  {
637  start2 += correction;
638  ref2 += correction;
639  ml2 -= correction;
640  }
641  }
642  // Now, we have start2 = ip+new_ml, with new_ml=min(ml, OPTIMAL_ML=18)
643 
644  if (start2 + ml2 < mflimit)
645  ml3 = LZ4HC_InsertAndGetWiderMatch(ctx, start2 + ml2 - 3, start2, matchlimit, ml2, &ref3, &start3);
646  else ml3=ml2;
647 
648  if (ml3 == ml2) // No better match : 2 sequences to encode
649  {
650  // ip & ref are known; Now for ml
651  if (start2 < ip+ml) ml = (int)(start2 - ip);
652  // Now, encode 2 sequences
653  if (LZ4_encodeSequence(&ip, &op, &anchor, ml, ref, oend)) return 0;
654  ip = start2;
655  if (LZ4_encodeSequence(&ip, &op, &anchor, ml2, ref2, oend)) return 0;
656  continue;
657  }
658 
659  if (start3 < ip+ml+3) // Not enough space for match 2 : remove it
660  {
661  if (start3 >= (ip+ml)) // can write Seq1 immediately ==> Seq2 is removed, so Seq3 becomes Seq1
662  {
663  if (start2 < ip+ml)
664  {
665  int correction = (int)(ip+ml - start2);
666  start2 += correction;
667  ref2 += correction;
668  ml2 -= correction;
669  if (ml2 < MINMATCH)
670  {
671  start2 = start3;
672  ref2 = ref3;
673  ml2 = ml3;
674  }
675  }
676 
677  if (LZ4_encodeSequence(&ip, &op, &anchor, ml, ref, oend)) return 0;
678  ip = start3;
679  ref = ref3;
680  ml = ml3;
681 
682  start0 = start2;
683  ref0 = ref2;
684  ml0 = ml2;
685  goto _Search2;
686  }
687 
688  start2 = start3;
689  ref2 = ref3;
690  ml2 = ml3;
691  goto _Search3;
692  }
693 
694  // OK, now we have 3 ascending matches; let's write at least the first one
695  // ip & ref are known; Now for ml
696  if (start2 < ip+ml)
697  {
698  if ((start2 - ip) < (int)ML_MASK)
699  {
700  int correction;
701  if (ml > OPTIMAL_ML) ml = OPTIMAL_ML;
702  if (ip + ml > start2 + ml2 - MINMATCH) ml = (int)(start2 - ip) + ml2 - MINMATCH;
703  correction = ml - (int)(start2 - ip);
704  if (correction > 0)
705  {
706  start2 += correction;
707  ref2 += correction;
708  ml2 -= correction;
709  }
710  }
711  else
712  {
713  ml = (int)(start2 - ip);
714  }
715  }
716  if (LZ4_encodeSequence(&ip, &op, &anchor, ml, ref, oend)) return 0;
717 
718  ip = start2;
719  ref = ref2;
720  ml = ml2;
721 
722  start2 = start3;
723  ref2 = ref3;
724  ml2 = ml3;
725 
726  goto _Search3;
727 
728  }
729 
730  // Encode Last Literals
731  {
732  int lastRun = (int)(iend - anchor);
733  if (((char*)op - dest) + lastRun + 1 + ((lastRun+255-RUN_MASK)/255) > (U32)maxOutputSize) return 0; // Check output limit
734  if (lastRun>=(int)RUN_MASK) { *op++=(RUN_MASK<<ML_BITS); lastRun-=RUN_MASK; for(; lastRun > 254 ; lastRun-=255) *op++ = 255; *op++ = (BYTE) lastRun; }
735  else *op++ = (BYTE)(lastRun<<ML_BITS);
736  memcpy(op, anchor, iend - anchor);
737  op += iend-anchor;
738  }
739 
740  // End
741  return (int) (((char*)op)-dest);
742 }
743 
744 
745 CORE_EXPORT( int ) LZ4_compressHC_limitedOutput(const char* source,
746  char* dest,
747  int inputSize,
748  int maxOutputSize)
749 {
750  void* ctx = LZ4HC_Create((const BYTE*)source);
751  int result = LZ4_compressHCCtx((LZ4HC_Data_Structure*)ctx, source, dest, inputSize, maxOutputSize);
752  LZ4HC_Free (&ctx);
753 
754  return result;
755 }
756 
757 
758 CORE_EXPORT( int ) LZ4_compressHC(const char* source,
759  char* dest,
760  int inputSize)
761 {
762  return LZ4_compressHC_limitedOutput(source, dest, inputSize, LZ4_compressBound(inputSize)+1);
763 }
764 
#define GETNEXT(p)
Definition: lz4hc.c:249
#define MAXD
Definition: lz4hc.c:176
#define MFLIMIT
Definition: lz4hc.c:193
Definition: lz4.c:167
#define forceinline
Definition: lz4hc.c:97
HTYPE hashTable[HASHTABLESIZE]
Definition: lz4hc.c:234
#define MEM_INIT
Definition: lz4hc.c:130
#define LASTLITERALS
Definition: lz4hc.c:192
static void * LZ4HC_Create(const BYTE *base)
Definition: lz4hc.c:340
#define MINMATCH
Definition: lz4hc.c:173
#define U16
Definition: lz4hc.c:146
#define BYTE
Definition: lz4hc.c:145
#define FREEMEM
Definition: lz4hc.c:129
#define STEPSIZE
Definition: lz4hc.c:210
U32 v
Definition: lz4.c:167
static forceinline int LZ4HC_InsertAndFindBestMatch(LZ4HC_Data_Structure *hc4, const BYTE *ip, const BYTE *const matchlimit, const BYTE **matchpos)
Definition: lz4hc.c:394
#define AARCH
Definition: lz4hc.c:214
U16 v
Definition: lz4.c:166
#define MAX_NB_ATTEMPTS
Definition: lz4hc.c:184
CORE_EXPORT(int)
Definition: lz4hc.c:745
#define UARCH
Definition: lz4hc.c:213
#define MAX_DISTANCE
Definition: lz4hc.c:178
struct _U64_S U64_S
#define U64
Definition: lz4hc.c:149
#define S64
Definition: lz4hc.c:150
static int LZ4HC_Free(void **LZ4HC_Data)
Definition: lz4hc.c:349
#define DELTANEXT(p)
Definition: lz4hc.c:248
struct _U16_S U16_S
char int int maxOutputSize
Definition: lz4.h:103
#define A16(x)
Definition: lz4hc.c:167
#define ML_BITS
Definition: lz4hc.c:186
#define HASHTABLESIZE
Definition: lz4hc.c:181
U16 chainTable[MAXD]
Definition: lz4hc.c:235
#define matchlimit
static forceinline int LZ4_encodeSequence(const BYTE **ip, BYTE **op, const BYTE **anchor, int matchLength, const BYTE *ref, BYTE *oend)
Definition: lz4hc.c:521
static forceinline void LZ4HC_Insert(LZ4HC_Data_Structure *hc4, const BYTE *ip)
Definition: lz4hc.c:358
Definition: lz4.c:166
#define HASH_VALUE(p)
Definition: lz4hc.c:246
const BYTE * base
Definition: lz4hc.c:233
static forceinline int LZ4HC_InsertAndGetWiderMatch(LZ4HC_Data_Structure *hc4, const BYTE *ip, const BYTE *startLimit, const BYTE *matchlimit, int longest, const BYTE **matchpos, const BYTE **startpos)
Definition: lz4hc.c:462
#define ML_MASK
Definition: lz4hc.c:187
static int LZ4HC_Init(LZ4HC_Data_Structure *hc4, const BYTE *base)
Definition: lz4hc.c:330
#define U32
Definition: lz4hc.c:147
U64 v
Definition: lz4.c:168
const BYTE * nextToUpdate
Definition: lz4hc.c:236
static int LZ4_compressBound(int isize)
Definition: lz4.h:87
Definition: lz4.c:168
#define A32(x)
Definition: lz4hc.c:166
#define LZ4_ARCH64
Definition: lz4hc.c:45
char * dest
Definition: lz4.h:61
static forceinline size_t LZ4HC_CommonLength(const BYTE *p1, const BYTE *p2, const BYTE *const matchlimit)
Definition: lz4hc.c:376
#define INITBASE(b, s)
Definition: lz4hc.c:216
#define HTYPE
Definition: lz4hc.c:215
#define LZ4_WRITE_LITTLEENDIAN_16(p, v)
Definition: lz4hc.c:224
#define RUN_MASK
Definition: lz4hc.c:189
struct _U32_S U32_S
char int inputSize
Definition: lz4.h:61
#define S32
Definition: lz4hc.c:148
#define LZ4_BLINDCOPY(s, d, l)
Definition: lz4hc.c:244
static int LZ4_NbCommonBytes(register U32 val)
Definition: lz4hc.c:293
int LZ4_compressHCCtx(LZ4HC_Data_Structure *ctx, const char *source, char *dest, int inputSize, int maxOutputSize)
Definition: lz4hc.c:557
#define OPTIMAL_ML
Definition: lz4hc.c:195
#define ALLOCATOR(s)
Definition: lz4hc.c:128
#define HASH_POINTER(p)
Definition: lz4hc.c:247