lz4.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  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. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions are
  7. met:
  8. * Redistributions of source code must retain the above copyright
  9. notice, this list of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above
  11. copyright notice, this list of conditions and the following disclaimer
  12. in the documentation and/or other materials provided with the
  13. distribution.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  18. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  20. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. You can contact the author at :
  26. - LZ4 homepage : http://fastcompression.blogspot.com/p/lz4.html
  27. - LZ4 source repository : http://code.google.com/p/lz4/
  28. */
  29. //**************************************
  30. // Tuning parameters
  31. //**************************************
  32. // MEMORY_USAGE :
  33. // Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.)
  34. // Increasing memory usage improves compression ratio
  35. // Reduced memory usage can improve speed, due to cache effect
  36. // Default value is 14, for 16KB, which nicely fits into Intel x86 L1 cache
  37. #define MEMORY_USAGE 14
  38. // BIG_ENDIAN_NATIVE_BUT_INCOMPATIBLE :
  39. // This will provide a small boost to performance for big endian cpu, but the resulting compressed stream will be incompatible with little-endian CPU.
  40. // You can set this option to 1 in situations where data will remain within closed environment
  41. // This option is useless on Little_Endian CPU (such as x86)
  42. //#define BIG_ENDIAN_NATIVE_BUT_INCOMPATIBLE 1
  43. //**************************************
  44. // CPU Feature Detection
  45. //**************************************
  46. // 32 or 64 bits ?
  47. #if (defined(__x86_64__) || defined(__x86_64) || defined(__amd64__) || defined(__amd64) || defined(__ppc64__) || defined(_WIN64) || defined(__LP64__) || defined(_LP64) ) // Detects 64 bits mode
  48. # define LZ4_ARCH64 1
  49. #else
  50. # define LZ4_ARCH64 0
  51. #endif
  52. // Little Endian or Big Endian ?
  53. // Overwrite the #define below if you know your architecture endianess
  54. #if defined (__GLIBC__)
  55. # include <endian.h>
  56. # if (__BYTE_ORDER == __BIG_ENDIAN)
  57. # define LZ4_BIG_ENDIAN 1
  58. # endif
  59. #elif (defined(__BIG_ENDIAN__) || defined(__BIG_ENDIAN) || defined(_BIG_ENDIAN)) && !(defined(__LITTLE_ENDIAN__) || defined(__LITTLE_ENDIAN) || defined(_LITTLE_ENDIAN))
  60. # define LZ4_BIG_ENDIAN 1
  61. #elif defined(__sparc) || defined(__sparc__) \
  62. || defined(__ppc__) || defined(_POWER) || defined(__powerpc__) || defined(_ARCH_PPC) || defined(__PPC__) || defined(__PPC) || defined(PPC) || defined(__powerpc__) || defined(__powerpc) || defined(powerpc) \
  63. || defined(__hpux) || defined(__hppa) \
  64. || defined(_MIPSEB) || defined(__s390__)
  65. # define LZ4_BIG_ENDIAN 1
  66. #else
  67. // Little Endian assumed. PDP Endian and other very rare endian format are unsupported.
  68. #endif
  69. // Unaligned memory access is automatically enabled for "common" CPU, such as x86.
  70. // For others CPU, the compiler will be more cautious, and insert extra code to ensure aligned access is respected
  71. // If you know your target CPU supports unaligned memory access, you want to force this option manually to improve performance
  72. #if defined(__ARM_FEATURE_UNALIGNED)
  73. # define LZ4_FORCE_UNALIGNED_ACCESS 1
  74. #endif
  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. // Compiler Options
  81. //**************************************
  82. #if __STDC_VERSION__ >= 199901L // C99
  83. /* "restrict" is a known keyword */
  84. #else
  85. # define restrict // Disable restrict
  86. #endif
  87. #define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
  88. #ifdef _MSC_VER // Visual Studio
  89. # include <intrin.h> // For Visual 2005
  90. # if LZ4_ARCH64 // 64-bit
  91. # pragma intrinsic(_BitScanForward64) // For Visual 2005
  92. # pragma intrinsic(_BitScanReverse64) // For Visual 2005
  93. # else
  94. # pragma intrinsic(_BitScanForward) // For Visual 2005
  95. # pragma intrinsic(_BitScanReverse) // For Visual 2005
  96. # endif
  97. #endif
  98. #ifdef _MSC_VER
  99. # define lz4_bswap16(x) _byteswap_ushort(x)
  100. #else
  101. # define lz4_bswap16(x) ((unsigned short int) ((((x) >> 8) & 0xffu) | (((x) & 0xffu) << 8)))
  102. #endif
  103. #if (GCC_VERSION >= 302) || (__INTEL_COMPILER >= 800) || defined(__clang__)
  104. # define expect(expr,value) (__builtin_expect ((expr),(value)) )
  105. #else
  106. # define expect(expr,value) (expr)
  107. #endif
  108. #define likely(expr) expect((expr) != 0, 1)
  109. #define unlikely(expr) expect((expr) != 0, 0)
  110. //**************************************
  111. // Includes
  112. //**************************************
  113. #include <stdlib.h> // for malloc
  114. #include <string.h> // for memset
  115. #include "lz4.h"
  116. //**************************************
  117. // Basic Types
  118. //**************************************
  119. #if defined(_MSC_VER) // Visual Studio does not support 'stdint' natively
  120. # define BYTE unsigned __int8
  121. # define U16 unsigned __int16
  122. # define U32 unsigned __int32
  123. # define S32 __int32
  124. # define U64 unsigned __int64
  125. #else
  126. # include <stdint.h>
  127. # define BYTE uint8_t
  128. # define U16 uint16_t
  129. # define U32 uint32_t
  130. # define S32 int32_t
  131. # define U64 uint64_t
  132. #endif
  133. #ifndef LZ4_FORCE_UNALIGNED_ACCESS
  134. # pragma pack(push, 1)
  135. #endif
  136. typedef struct _U16_S { U16 v; } U16_S;
  137. typedef struct _U32_S { U32 v; } U32_S;
  138. typedef struct _U64_S { U64 v; } U64_S;
  139. #ifndef LZ4_FORCE_UNALIGNED_ACCESS
  140. # pragma pack(pop)
  141. #endif
  142. #define A64(x) (((U64_S *)(x))->v)
  143. #define A32(x) (((U32_S *)(x))->v)
  144. #define A16(x) (((U16_S *)(x))->v)
  145. //**************************************
  146. // Constants
  147. //**************************************
  148. #define MINMATCH 4
  149. #define HASH_LOG (MEMORY_USAGE-2)
  150. #define HASHTABLESIZE (1 << HASH_LOG)
  151. #define HASH_MASK (HASHTABLESIZE - 1)
  152. // NOTCOMPRESSIBLE_DETECTIONLEVEL :
  153. // Decreasing this value will make the algorithm skip faster data segments considered "incompressible"
  154. // This may decrease compression ratio dramatically, but will be faster on incompressible data
  155. // Increasing this value will make the algorithm search more before declaring a segment "incompressible"
  156. // This could improve compression a bit, but will be slower on incompressible data
  157. // The default value (6) is recommended
  158. #define NOTCOMPRESSIBLE_DETECTIONLEVEL 6
  159. #define SKIPSTRENGTH (NOTCOMPRESSIBLE_DETECTIONLEVEL>2?NOTCOMPRESSIBLE_DETECTIONLEVEL:2)
  160. #define STACKLIMIT 13
  161. #define HEAPMODE (HASH_LOG>STACKLIMIT) // Defines if memory is allocated into the stack (local variable), or into the heap (malloc()).
  162. #define COPYLENGTH 8
  163. #define LASTLITERALS 5
  164. #define MFLIMIT (COPYLENGTH+MINMATCH)
  165. #define MINLENGTH (MFLIMIT+1)
  166. #define MAXD_LOG 16
  167. #define MAX_DISTANCE ((1 << MAXD_LOG) - 1)
  168. #define ML_BITS 4
  169. #define ML_MASK ((1U<<ML_BITS)-1)
  170. #define RUN_BITS (8-ML_BITS)
  171. #define RUN_MASK ((1U<<RUN_BITS)-1)
  172. //**************************************
  173. // Architecture-specific macros
  174. //**************************************
  175. #if LZ4_ARCH64 // 64-bit
  176. # define STEPSIZE 8
  177. # define UARCH U64
  178. # define AARCH A64
  179. # define LZ4_COPYSTEP(s,d) A64(d) = A64(s); d+=8; s+=8;
  180. # define LZ4_COPYPACKET(s,d) LZ4_COPYSTEP(s,d)
  181. # define LZ4_SECURECOPY(s,d,e) if (d<e) LZ4_WILDCOPY(s,d,e)
  182. # define HTYPE U32
  183. # define INITBASE(base) const BYTE* const base = ip
  184. #else // 32-bit
  185. # define STEPSIZE 4
  186. # define UARCH U32
  187. # define AARCH A32
  188. # define LZ4_COPYSTEP(s,d) A32(d) = A32(s); d+=4; s+=4;
  189. # define LZ4_COPYPACKET(s,d) LZ4_COPYSTEP(s,d); LZ4_COPYSTEP(s,d);
  190. # define LZ4_SECURECOPY LZ4_WILDCOPY
  191. # define HTYPE const BYTE*
  192. # define INITBASE(base) const int base = 0
  193. #endif
  194. #if (defined(LZ4_BIG_ENDIAN) && !defined(BIG_ENDIAN_NATIVE_BUT_INCOMPATIBLE))
  195. # define LZ4_READ_LITTLEENDIAN_16(d,s,p) { U16 v = A16(p); v = lz4_bswap16(v); d = (s) - v; }
  196. # define LZ4_WRITE_LITTLEENDIAN_16(p,i) { U16 v = (U16)(i); v = lz4_bswap16(v); A16(p) = v; p+=2; }
  197. #else // Little Endian
  198. # define LZ4_READ_LITTLEENDIAN_16(d,s,p) { d = (s) - A16(p); }
  199. # define LZ4_WRITE_LITTLEENDIAN_16(p,v) { A16(p) = v; p+=2; }
  200. #endif
  201. //**************************************
  202. // Local structures
  203. //**************************************
  204. struct refTables
  205. {
  206. HTYPE hashTable[HASHTABLESIZE];
  207. };
  208. //**************************************
  209. // Macros
  210. //**************************************
  211. #define LZ4_HASH_FUNCTION(i) (((i) * 2654435761U) >> ((MINMATCH*8)-HASH_LOG))
  212. #define LZ4_HASH_VALUE(p) LZ4_HASH_FUNCTION(A32(p))
  213. #define LZ4_WILDCOPY(s,d,e) do { LZ4_COPYPACKET(s,d) } while (d<e);
  214. #define LZ4_BLINDCOPY(s,d,l) { BYTE* e=(d)+l; LZ4_WILDCOPY(s,d,e); d=e; }
  215. //****************************
  216. // Private functions
  217. //****************************
  218. #if LZ4_ARCH64
  219. static inline int LZ4_NbCommonBytes (register U64 val)
  220. {
  221. #if defined(LZ4_BIG_ENDIAN)
  222. #if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
  223. unsigned long r = 0;
  224. _BitScanReverse64( &r, val );
  225. return (int)(r>>3);
  226. #elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
  227. return (__builtin_clzll(val) >> 3);
  228. #else
  229. int r;
  230. if (!(val>>32)) { r=4; } else { r=0; val>>=32; }
  231. if (!(val>>16)) { r+=2; val>>=8; } else { val>>=24; }
  232. r += (!val);
  233. return r;
  234. #endif
  235. #else
  236. #if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
  237. unsigned long r = 0;
  238. _BitScanForward64( &r, val );
  239. return (int)(r>>3);
  240. #elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
  241. return (__builtin_ctzll(val) >> 3);
  242. #else
  243. 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 };
  244. return DeBruijnBytePos[((U64)((val & -val) * 0x0218A392CDABBD3F)) >> 58];
  245. #endif
  246. #endif
  247. }
  248. #else
  249. static inline int LZ4_NbCommonBytes (register U32 val)
  250. {
  251. #if defined(LZ4_BIG_ENDIAN)
  252. #if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
  253. unsigned long r = 0;
  254. _BitScanReverse( &r, val );
  255. return (int)(r>>3);
  256. #elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
  257. return (__builtin_clz(val) >> 3);
  258. #else
  259. int r;
  260. if (!(val>>16)) { r=2; val>>=8; } else { r=0; val>>=24; }
  261. r += (!val);
  262. return r;
  263. #endif
  264. #else
  265. #if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
  266. unsigned long r;
  267. _BitScanForward( &r, val );
  268. return (int)(r>>3);
  269. #elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
  270. return (__builtin_ctz(val) >> 3);
  271. #else
  272. 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 };
  273. return DeBruijnBytePos[((U32)((val & -(S32)val) * 0x077CB531U)) >> 27];
  274. #endif
  275. #endif
  276. }
  277. #endif
  278. //******************************
  279. // Compression functions
  280. //******************************
  281. // LZ4_compressCtx :
  282. // -----------------
  283. // Compress 'isize' bytes from 'source' into an output buffer 'dest' of maximum size 'maxOutputSize'.
  284. // If it cannot achieve it, compression will stop, and result of the function will be zero.
  285. // return : the number of bytes written in buffer 'dest', or 0 if the compression fails
  286. static inline int LZ4_compressCtx(void** ctx,
  287. const char* source,
  288. char* dest,
  289. int isize,
  290. int maxOutputSize)
  291. {
  292. #if HEAPMODE
  293. struct refTables *srt = (struct refTables *) (*ctx);
  294. HTYPE* HashTable;
  295. #else
  296. HTYPE HashTable[HASHTABLESIZE] = {0};
  297. #endif
  298. const BYTE* ip = (BYTE*) source;
  299. INITBASE(base);
  300. const BYTE* anchor = ip;
  301. const BYTE* const iend = ip + isize;
  302. const BYTE* const mflimit = iend - MFLIMIT;
  303. #define matchlimit (iend - LASTLITERALS)
  304. BYTE* op = (BYTE*) dest;
  305. BYTE* const oend = op + maxOutputSize;
  306. int length;
  307. const int skipStrength = SKIPSTRENGTH;
  308. U32 forwardH;
  309. // Init
  310. if (isize<MINLENGTH) goto _last_literals;
  311. #if HEAPMODE
  312. if (*ctx == NULL)
  313. {
  314. srt = (struct refTables *) malloc ( sizeof(struct refTables) );
  315. *ctx = (void*) srt;
  316. }
  317. HashTable = (HTYPE*)(srt->hashTable);
  318. memset((void*)HashTable, 0, sizeof(srt->hashTable));
  319. #else
  320. (void) ctx;
  321. #endif
  322. // First Byte
  323. HashTable[LZ4_HASH_VALUE(ip)] = ip - base;
  324. ip++; forwardH = LZ4_HASH_VALUE(ip);
  325. // Main Loop
  326. for ( ; ; )
  327. {
  328. int findMatchAttempts = (1U << skipStrength) + 3;
  329. const BYTE* forwardIp = ip;
  330. const BYTE* ref;
  331. BYTE* token;
  332. // Find a match
  333. do {
  334. U32 h = forwardH;
  335. int step = findMatchAttempts++ >> skipStrength;
  336. ip = forwardIp;
  337. forwardIp = ip + step;
  338. if unlikely(forwardIp > mflimit) { goto _last_literals; }
  339. forwardH = LZ4_HASH_VALUE(forwardIp);
  340. ref = base + HashTable[h];
  341. HashTable[h] = ip - base;
  342. } while ((ref < ip - MAX_DISTANCE) || (A32(ref) != A32(ip)));
  343. // Catch up
  344. while ((ip>anchor) && (ref>(BYTE*)source) && unlikely(ip[-1]==ref[-1])) { ip--; ref--; }
  345. // Encode Literal length
  346. length = (int)(ip - anchor);
  347. token = op++;
  348. if unlikely(op + length + (2 + 1 + LASTLITERALS) + (length>>8) > oend) return 0; // Check output limit
  349. #ifdef _MSC_VER
  350. if (length>=(int)RUN_MASK)
  351. {
  352. int len = length-RUN_MASK;
  353. *token=(RUN_MASK<<ML_BITS);
  354. if (len>254)
  355. {
  356. do { *op++ = 255; len -= 255; } while (len>254);
  357. *op++ = (BYTE)len;
  358. memcpy(op, anchor, length);
  359. op += length;
  360. goto _next_match;
  361. }
  362. else
  363. *op++ = (BYTE)len;
  364. }
  365. else *token = (length<<ML_BITS);
  366. #else
  367. if (length>=(int)RUN_MASK)
  368. {
  369. int len;
  370. *token=(RUN_MASK<<ML_BITS);
  371. len = length-RUN_MASK;
  372. for(; len > 254 ; len-=255) *op++ = 255;
  373. *op++ = (BYTE)len;
  374. }
  375. else *token = (length<<ML_BITS);
  376. #endif
  377. // Copy Literals
  378. LZ4_BLINDCOPY(anchor, op, length);
  379. _next_match:
  380. // Encode Offset
  381. LZ4_WRITE_LITTLEENDIAN_16(op,(U16)(ip-ref));
  382. // Start Counting
  383. ip+=MINMATCH; ref+=MINMATCH; // MinMatch already verified
  384. anchor = ip;
  385. while likely(ip<matchlimit-(STEPSIZE-1))
  386. {
  387. UARCH diff = AARCH(ref) ^ AARCH(ip);
  388. if (!diff) { ip+=STEPSIZE; ref+=STEPSIZE; continue; }
  389. ip += LZ4_NbCommonBytes(diff);
  390. goto _endCount;
  391. }
  392. if (LZ4_ARCH64) if ((ip<(matchlimit-3)) && (A32(ref) == A32(ip))) { ip+=4; ref+=4; }
  393. if ((ip<(matchlimit-1)) && (A16(ref) == A16(ip))) { ip+=2; ref+=2; }
  394. if ((ip<matchlimit) && (*ref == *ip)) ip++;
  395. _endCount:
  396. // Encode MatchLength
  397. length = (int)(ip - anchor);
  398. if unlikely(op + (1 + LASTLITERALS) + (length>>8) > oend) return 0; // Check output limit
  399. if (length>=(int)ML_MASK)
  400. {
  401. *token += ML_MASK;
  402. length -= ML_MASK;
  403. for (; length > 509 ; length-=510) { *op++ = 255; *op++ = 255; }
  404. if (length > 254) { length-=255; *op++ = 255; }
  405. *op++ = (BYTE)length;
  406. }
  407. else *token += length;
  408. // Test end of chunk
  409. if (ip > mflimit) { anchor = ip; break; }
  410. // Fill table
  411. HashTable[LZ4_HASH_VALUE(ip-2)] = ip - 2 - base;
  412. // Test next position
  413. ref = base + HashTable[LZ4_HASH_VALUE(ip)];
  414. HashTable[LZ4_HASH_VALUE(ip)] = ip - base;
  415. if ((ref > ip - (MAX_DISTANCE + 1)) && (A32(ref) == A32(ip))) { token = op++; *token=0; goto _next_match; }
  416. // Prepare next loop
  417. anchor = ip++;
  418. forwardH = LZ4_HASH_VALUE(ip);
  419. }
  420. _last_literals:
  421. // Encode Last Literals
  422. {
  423. int lastRun = (int)(iend - anchor);
  424. if (((char*)op - dest) + lastRun + 1 + ((lastRun+255-RUN_MASK)/255) > (U32)maxOutputSize) return 0;
  425. if (lastRun>=(int)RUN_MASK) { *op++=(RUN_MASK<<ML_BITS); lastRun-=RUN_MASK; for(; lastRun > 254 ; lastRun-=255) *op++ = 255; *op++ = (BYTE) lastRun; }
  426. else *op++ = (lastRun<<ML_BITS);
  427. memcpy(op, anchor, iend - anchor);
  428. op += iend-anchor;
  429. }
  430. // End
  431. return (int) (((char*)op)-dest);
  432. }
  433. // Note : this function is valid only if isize < LZ4_64KLIMIT
  434. #define LZ4_64KLIMIT ((1<<16) + (MFLIMIT-1))
  435. #define HASHLOG64K (HASH_LOG+1)
  436. #define HASH64KTABLESIZE (1U<<HASHLOG64K)
  437. #define LZ4_HASH64K_FUNCTION(i) (((i) * 2654435761U) >> ((MINMATCH*8)-HASHLOG64K))
  438. #define LZ4_HASH64K_VALUE(p) LZ4_HASH64K_FUNCTION(A32(p))
  439. static inline int LZ4_compress64kCtx(void** ctx,
  440. const char* source,
  441. char* dest,
  442. int isize,
  443. int maxOutputSize)
  444. {
  445. #if HEAPMODE
  446. struct refTables *srt = (struct refTables *) (*ctx);
  447. U16* HashTable;
  448. #else
  449. U16 HashTable[HASH64KTABLESIZE] = {0};
  450. #endif
  451. const BYTE* ip = (BYTE*) source;
  452. const BYTE* anchor = ip;
  453. const BYTE* const base = ip;
  454. const BYTE* const iend = ip + isize;
  455. const BYTE* const mflimit = iend - MFLIMIT;
  456. #define matchlimit (iend - LASTLITERALS)
  457. BYTE* op = (BYTE*) dest;
  458. BYTE* const oend = op + maxOutputSize;
  459. int len, length;
  460. const int skipStrength = SKIPSTRENGTH;
  461. U32 forwardH;
  462. // Init
  463. if (isize<MINLENGTH) goto _last_literals;
  464. #if HEAPMODE
  465. if (*ctx == NULL)
  466. {
  467. srt = (struct refTables *) malloc ( sizeof(struct refTables) );
  468. *ctx = (void*) srt;
  469. }
  470. HashTable = (U16*)(srt->hashTable);
  471. memset((void*)HashTable, 0, sizeof(srt->hashTable));
  472. #else
  473. (void) ctx;
  474. #endif
  475. // First Byte
  476. ip++; forwardH = LZ4_HASH64K_VALUE(ip);
  477. // Main Loop
  478. for ( ; ; )
  479. {
  480. int findMatchAttempts = (1U << skipStrength) + 3;
  481. const BYTE* forwardIp = ip;
  482. const BYTE* ref;
  483. BYTE* token;
  484. // Find a match
  485. do {
  486. U32 h = forwardH;
  487. int step = findMatchAttempts++ >> skipStrength;
  488. ip = forwardIp;
  489. forwardIp = ip + step;
  490. if (forwardIp > mflimit) { goto _last_literals; }
  491. forwardH = LZ4_HASH64K_VALUE(forwardIp);
  492. ref = base + HashTable[h];
  493. HashTable[h] = (U16)(ip - base);
  494. } while (A32(ref) != A32(ip));
  495. // Catch up
  496. while ((ip>anchor) && (ref>(BYTE*)source) && (ip[-1]==ref[-1])) { ip--; ref--; }
  497. // Encode Literal length
  498. length = (int)(ip - anchor);
  499. token = op++;
  500. if unlikely(op + length + (2 + 1 + LASTLITERALS) + (length>>8) > oend) return 0; // Check output limit
  501. #ifdef _MSC_VER
  502. if (length>=(int)RUN_MASK)
  503. {
  504. int len = length-RUN_MASK;
  505. *token=(RUN_MASK<<ML_BITS);
  506. if (len>254)
  507. {
  508. do { *op++ = 255; len -= 255; } while (len>254);
  509. *op++ = (BYTE)len;
  510. memcpy(op, anchor, length);
  511. op += length;
  512. goto _next_match;
  513. }
  514. else
  515. *op++ = (BYTE)len;
  516. }
  517. else *token = (length<<ML_BITS);
  518. #else
  519. if (length>=(int)RUN_MASK) { *token=(RUN_MASK<<ML_BITS); len = length-RUN_MASK; for(; len > 254 ; len-=255) *op++ = 255; *op++ = (BYTE)len; }
  520. else *token = (length<<ML_BITS);
  521. #endif
  522. // Copy Literals
  523. LZ4_BLINDCOPY(anchor, op, length);
  524. _next_match:
  525. // Encode Offset
  526. LZ4_WRITE_LITTLEENDIAN_16(op,(U16)(ip-ref));
  527. // Start Counting
  528. ip+=MINMATCH; ref+=MINMATCH; // MinMatch verified
  529. anchor = ip;
  530. while (ip<matchlimit-(STEPSIZE-1))
  531. {
  532. UARCH diff = AARCH(ref) ^ AARCH(ip);
  533. if (!diff) { ip+=STEPSIZE; ref+=STEPSIZE; continue; }
  534. ip += LZ4_NbCommonBytes(diff);
  535. goto _endCount;
  536. }
  537. if (LZ4_ARCH64) if ((ip<(matchlimit-3)) && (A32(ref) == A32(ip))) { ip+=4; ref+=4; }
  538. if ((ip<(matchlimit-1)) && (A16(ref) == A16(ip))) { ip+=2; ref+=2; }
  539. if ((ip<matchlimit) && (*ref == *ip)) ip++;
  540. _endCount:
  541. // Encode MatchLength
  542. len = (int)(ip - anchor);
  543. if unlikely(op + (1 + LASTLITERALS) + (len>>8) > oend) return 0; // Check output limit
  544. 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; }
  545. else *token += len;
  546. // Test end of chunk
  547. if (ip > mflimit) { anchor = ip; break; }
  548. // Fill table
  549. HashTable[LZ4_HASH64K_VALUE(ip-2)] = (U16)(ip - 2 - base);
  550. // Test next position
  551. ref = base + HashTable[LZ4_HASH64K_VALUE(ip)];
  552. HashTable[LZ4_HASH64K_VALUE(ip)] = (U16)(ip - base);
  553. if (A32(ref) == A32(ip)) { token = op++; *token=0; goto _next_match; }
  554. // Prepare next loop
  555. anchor = ip++;
  556. forwardH = LZ4_HASH64K_VALUE(ip);
  557. }
  558. _last_literals:
  559. // Encode Last Literals
  560. {
  561. int lastRun = (int)(iend - anchor);
  562. if (op + lastRun + 1 + (lastRun-RUN_MASK+255)/255 > oend) return 0;
  563. if (lastRun>=(int)RUN_MASK) { *op++=(RUN_MASK<<ML_BITS); lastRun-=RUN_MASK; for(; lastRun > 254 ; lastRun-=255) *op++ = 255; *op++ = (BYTE) lastRun; }
  564. else *op++ = (lastRun<<ML_BITS);
  565. memcpy(op, anchor, iend - anchor);
  566. op += iend-anchor;
  567. }
  568. // End
  569. return (int) (((char*)op)-dest);
  570. }
  571. int LZ4_compress_limitedOutput(const char* source,
  572. char* dest,
  573. int isize,
  574. int maxOutputSize)
  575. {
  576. #if HEAPMODE
  577. void* ctx = malloc(sizeof(struct refTables));
  578. int result;
  579. if (isize < LZ4_64KLIMIT)
  580. result = LZ4_compress64kCtx(&ctx, source, dest, isize, maxOutputSize);
  581. else result = LZ4_compressCtx(&ctx, source, dest, isize, maxOutputSize);
  582. free(ctx);
  583. return result;
  584. #else
  585. if (isize < (int)LZ4_64KLIMIT) return LZ4_compress64kCtx(NULL, source, dest, isize, maxOutputSize);
  586. return LZ4_compressCtx(NULL, source, dest, isize, maxOutputSize);
  587. #endif
  588. }
  589. int LZ4_compress(const char* source,
  590. char* dest,
  591. int isize)
  592. {
  593. return LZ4_compress_limitedOutput(source, dest, isize, LZ4_compressBound(isize));
  594. }
  595. //****************************
  596. // Decompression functions
  597. //****************************
  598. // Note : The decoding functions LZ4_uncompress() and LZ4_uncompress_unknownOutputSize()
  599. // are safe against "buffer overflow" attack type.
  600. // They will never write nor read outside of the provided output buffers.
  601. // LZ4_uncompress_unknownOutputSize() also insures that it will never read outside of the input buffer.
  602. // A corrupted input will produce an error result, a negative int, indicating the position of the error within input stream.
  603. int LZ4_uncompress(const char* source,
  604. char* dest,
  605. int osize)
  606. {
  607. // Local Variables
  608. const BYTE* restrict ip = (const BYTE*) source;
  609. const BYTE* ref;
  610. BYTE* op = (BYTE*) dest;
  611. BYTE* const oend = op + osize;
  612. BYTE* cpy;
  613. unsigned token;
  614. size_t dec32table[] = {0, 3, 2, 3, 0, 0, 0, 0};
  615. #if LZ4_ARCH64
  616. size_t dec64table[] = {0, 0, 0, -1, 0, 1, 2, 3};
  617. #endif
  618. // Main Loop
  619. while (1)
  620. {
  621. size_t length;
  622. // get runlength
  623. token = *ip++;
  624. if ((length=(token>>ML_BITS)) == RUN_MASK) { size_t len; for (;(len=*ip++)==255;length+=255){} length += len; }
  625. // copy literals
  626. cpy = op+length;
  627. if (cpy>oend-COPYLENGTH)
  628. {
  629. if (cpy != oend) goto _output_error; // Error : not enough place for another match (min 4) + 5 literals
  630. memcpy(op, ip, length);
  631. ip += length;
  632. break; // EOF
  633. }
  634. LZ4_WILDCOPY(ip, op, cpy); ip -= (op-cpy); op = cpy;
  635. // get offset
  636. LZ4_READ_LITTLEENDIAN_16(ref,cpy,ip); ip+=2;
  637. if unlikely(ref < (BYTE* const)dest) goto _output_error; // Error : offset outside destination buffer
  638. // get matchlength
  639. if ((length=(token&ML_MASK)) == ML_MASK) { for (;*ip==255;length+=255) {ip++;} length += *ip++; }
  640. // copy repeated sequence
  641. if unlikely((op-ref)<STEPSIZE)
  642. {
  643. #if LZ4_ARCH64
  644. size_t dec64 = dec64table[op-ref];
  645. #else
  646. const int dec64 = 0;
  647. #endif
  648. op[0] = ref[0];
  649. op[1] = ref[1];
  650. op[2] = ref[2];
  651. op[3] = ref[3];
  652. op += 4, ref += 4; ref -= dec32table[op-ref];
  653. A32(op) = A32(ref);
  654. op += STEPSIZE-4; ref -= dec64;
  655. } else { LZ4_COPYSTEP(ref,op); }
  656. cpy = op + length - (STEPSIZE-4);
  657. if unlikely(cpy>oend-(COPYLENGTH)-(STEPSIZE-4))
  658. {
  659. if (cpy > oend-LASTLITERALS) goto _output_error; // Error : last 5 bytes must be literals
  660. LZ4_SECURECOPY(ref, op, (oend-COPYLENGTH));
  661. while(op<cpy) *op++=*ref++;
  662. op=cpy;
  663. continue;
  664. }
  665. LZ4_WILDCOPY(ref, op, cpy);
  666. op=cpy; // correction
  667. }
  668. // end of decoding
  669. return (int) (((char*)ip)-source);
  670. // write overflow error detected
  671. _output_error:
  672. return (int) (-(((char*)ip)-source));
  673. }
  674. int LZ4_uncompress_unknownOutputSize(
  675. const char* source,
  676. char* dest,
  677. int isize,
  678. int maxOutputSize)
  679. {
  680. // Local Variables
  681. const BYTE* restrict ip = (const BYTE*) source;
  682. const BYTE* const iend = ip + isize;
  683. const BYTE* ref;
  684. BYTE* op = (BYTE*) dest;
  685. BYTE* const oend = op + maxOutputSize;
  686. BYTE* cpy;
  687. size_t dec32table[] = {0, 3, 2, 3, 0, 0, 0, 0};
  688. #if LZ4_ARCH64
  689. size_t dec64table[] = {0, 0, 0, -1, 0, 1, 2, 3};
  690. #endif
  691. // Special case
  692. if unlikely(ip==iend) goto _output_error; // A correctly formed null-compressed LZ4 must have at least one byte (token=0)
  693. // Main Loop
  694. while (1)
  695. {
  696. unsigned token;
  697. size_t length;
  698. // get runlength
  699. token = *ip++;
  700. if ((length=(token>>ML_BITS)) == RUN_MASK)
  701. {
  702. int s=255;
  703. while (likely(ip<iend) && (s==255)) { s=*ip++; length += s; }
  704. }
  705. // copy literals
  706. cpy = op+length;
  707. if ((cpy>oend-MFLIMIT) || (ip+length>iend-(2+1+LASTLITERALS)))
  708. {
  709. if (cpy > oend) goto _output_error; // Error : writes beyond output buffer
  710. 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)
  711. memcpy(op, ip, length);
  712. op += length;
  713. break; // Necessarily EOF, due to parsing restrictions
  714. }
  715. LZ4_WILDCOPY(ip, op, cpy); ip -= (op-cpy); op = cpy;
  716. // get offset
  717. LZ4_READ_LITTLEENDIAN_16(ref,cpy,ip); ip+=2;
  718. if unlikely(ref < (BYTE* const)dest) goto _output_error; // Error : offset outside of destination buffer
  719. // get matchlength
  720. if ((length=(token&ML_MASK)) == ML_MASK)
  721. {
  722. while likely(ip<iend-(LASTLITERALS+1)) // Error : a minimum input bytes must remain for LASTLITERALS + token
  723. {
  724. int s = *ip++;
  725. length +=s;
  726. if (s==255) continue;
  727. break;
  728. }
  729. }
  730. // copy repeated sequence
  731. if unlikely(op-ref<STEPSIZE)
  732. {
  733. #if LZ4_ARCH64
  734. size_t dec64 = dec64table[op-ref];
  735. #else
  736. const int dec64 = 0;
  737. #endif
  738. op[0] = ref[0];
  739. op[1] = ref[1];
  740. op[2] = ref[2];
  741. op[3] = ref[3];
  742. op += 4, ref += 4; ref -= dec32table[op-ref];
  743. A32(op) = A32(ref);
  744. op += STEPSIZE-4; ref -= dec64;
  745. } else { LZ4_COPYSTEP(ref,op); }
  746. cpy = op + length - (STEPSIZE-4);
  747. if unlikely(cpy>oend-(COPYLENGTH+(STEPSIZE-4)))
  748. {
  749. if (cpy > oend-LASTLITERALS) goto _output_error; // Error : last 5 bytes must be literals
  750. LZ4_SECURECOPY(ref, op, (oend-COPYLENGTH));
  751. while(op<cpy) *op++=*ref++;
  752. op=cpy;
  753. continue;
  754. }
  755. LZ4_WILDCOPY(ref, op, cpy);
  756. op=cpy; // correction
  757. }
  758. // end of decoding
  759. return (int) (((char*)op)-dest);
  760. // write overflow error detected
  761. _output_error:
  762. return (int) (-(((char*)ip)-source));
  763. }