lz4.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564
  1. /*
  2. LZ4 - Fast LZ compression algorithm
  3. Copyright (C) 2011-2015, 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 source repository : https://github.com/Cyan4973/lz4
  27. - LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c
  28. */
  29. /**************************************
  30. * Tuning parameters
  31. **************************************/
  32. /*
  33. * HEAPMODE :
  34. * Select how default compression functions will allocate memory for their hash table,
  35. * in memory stack (0:default, fastest), or in memory heap (1:requires malloc()).
  36. */
  37. #define HEAPMODE 0
  38. /*
  39. * ACCELERATION_DEFAULT :
  40. * Select "acceleration" for LZ4_compress_fast() when parameter value <= 0
  41. */
  42. #define ACCELERATION_DEFAULT 1
  43. /**************************************
  44. * CPU Feature Detection
  45. **************************************/
  46. /* LZ4_FORCE_MEMORY_ACCESS
  47. * By default, access to unaligned memory is controlled by `memcpy()`, which is safe and portable.
  48. * Unfortunately, on some target/compiler combinations, the generated assembly is sub-optimal.
  49. * The below switch allow to select different access method for improved performance.
  50. * Method 0 (default) : use `memcpy()`. Safe and portable.
  51. * Method 1 : `__packed` statement. It depends on compiler extension (ie, not portable).
  52. * This method is safe if your compiler supports it, and *generally* as fast or faster than `memcpy`.
  53. * Method 2 : direct access. This method is portable but violate C standard.
  54. * It can generate buggy code on targets which generate assembly depending on alignment.
  55. * But in some circumstances, it's the only known way to get the most performance (ie GCC + ARMv6)
  56. * See http://fastcompression.blogspot.fr/2015/08/accessing-unaligned-memory.html for details.
  57. * Prefer these methods in priority order (0 > 1 > 2)
  58. */
  59. #ifndef LZ4_FORCE_MEMORY_ACCESS /* can be defined externally, on command line for example */
  60. # if defined(__GNUC__) && ( defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_6T2__) )
  61. # define LZ4_FORCE_MEMORY_ACCESS 2
  62. # elif defined(__INTEL_COMPILER) || \
  63. (defined(__GNUC__) && ( defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7S__) ))
  64. # define LZ4_FORCE_MEMORY_ACCESS 1
  65. # endif
  66. #endif
  67. /*
  68. * LZ4_FORCE_SW_BITCOUNT
  69. * Define this parameter if your target system or compiler does not support hardware bit count
  70. */
  71. #if defined(_MSC_VER) && defined(_WIN32_WCE) /* Visual Studio for Windows CE does not support Hardware bit count */
  72. # define LZ4_FORCE_SW_BITCOUNT
  73. #endif
  74. /**************************************
  75. * Includes
  76. **************************************/
  77. #include "lz4.h"
  78. /**************************************
  79. * Compiler Options
  80. **************************************/
  81. #ifdef _MSC_VER /* Visual Studio */
  82. # define FORCE_INLINE static __forceinline
  83. # include <intrin.h>
  84. # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
  85. # pragma warning(disable : 4293) /* disable: C4293: too large shift (32-bits) */
  86. #else
  87. # if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */
  88. # if defined(__GNUC__) || defined(__clang__)
  89. # define FORCE_INLINE static inline __attribute__((always_inline))
  90. # else
  91. # define FORCE_INLINE static inline
  92. # endif
  93. # else
  94. # define FORCE_INLINE static
  95. # endif /* __STDC_VERSION__ */
  96. #endif /* _MSC_VER */
  97. /* LZ4_GCC_VERSION is defined into lz4.h */
  98. #if (LZ4_GCC_VERSION >= 302) || (__INTEL_COMPILER >= 800) || defined(__clang__)
  99. # define expect(expr,value) (__builtin_expect ((expr),(value)) )
  100. #else
  101. # define expect(expr,value) (expr)
  102. #endif
  103. #define likely(expr) expect((expr) != 0, 1)
  104. #define unlikely(expr) expect((expr) != 0, 0)
  105. /**************************************
  106. * Memory routines
  107. **************************************/
  108. #include <stdlib.h> /* malloc, calloc, free */
  109. #define ALLOCATOR(n,s) calloc(n,s)
  110. #define FREEMEM free
  111. #include <string.h> /* memset, memcpy */
  112. #define MEM_INIT memset
  113. /**************************************
  114. * Basic Types
  115. **************************************/
  116. #if defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */
  117. # include <stdint.h>
  118. typedef uint8_t BYTE;
  119. typedef uint16_t U16;
  120. typedef uint32_t U32;
  121. typedef int32_t S32;
  122. typedef uint64_t U64;
  123. #else
  124. typedef unsigned char BYTE;
  125. typedef unsigned short U16;
  126. typedef unsigned int U32;
  127. typedef signed int S32;
  128. typedef unsigned long long U64;
  129. #endif
  130. /**************************************
  131. * Reading and writing into memory
  132. **************************************/
  133. #define STEPSIZE sizeof(size_t)
  134. static unsigned LZ4_64bits(void) { return sizeof(void*)==8; }
  135. static unsigned LZ4_isLittleEndian(void)
  136. {
  137. const union { U32 i; BYTE c[4]; } one = { 1 }; // don't use static : performance detrimental
  138. return one.c[0];
  139. }
  140. #if defined(LZ4_FORCE_MEMORY_ACCESS) && (LZ4_FORCE_MEMORY_ACCESS==2)
  141. static U16 LZ4_read16(const void* memPtr) { return *(const U16*) memPtr; }
  142. static U32 LZ4_read32(const void* memPtr) { return *(const U32*) memPtr; }
  143. static size_t LZ4_read_ARCH(const void* memPtr) { return *(const size_t*) memPtr; }
  144. static void LZ4_write16(void* memPtr, U16 value) { *(U16*)memPtr = value; }
  145. #elif defined(LZ4_FORCE_MEMORY_ACCESS) && (LZ4_FORCE_MEMORY_ACCESS==1)
  146. /* __pack instructions are safer, but compiler specific, hence potentially problematic for some compilers */
  147. /* currently only defined for gcc and icc */
  148. typedef union { U16 u16; U32 u32; size_t uArch; } __attribute__((packed)) unalign;
  149. static U16 LZ4_read16(const void* ptr) { return ((const unalign*)ptr)->u16; }
  150. static U32 LZ4_read32(const void* ptr) { return ((const unalign*)ptr)->u32; }
  151. static size_t LZ4_read_ARCH(const void* ptr) { return ((const unalign*)ptr)->uArch; }
  152. static void LZ4_write16(void* memPtr, U16 value) { ((unalign*)memPtr)->u16 = value; }
  153. #else
  154. static U16 LZ4_read16(const void* memPtr)
  155. {
  156. U16 val; memcpy(&val, memPtr, sizeof(val)); return val;
  157. }
  158. static U32 LZ4_read32(const void* memPtr)
  159. {
  160. U32 val; memcpy(&val, memPtr, sizeof(val)); return val;
  161. }
  162. static size_t LZ4_read_ARCH(const void* memPtr)
  163. {
  164. size_t val; memcpy(&val, memPtr, sizeof(val)); return val;
  165. }
  166. static void LZ4_write16(void* memPtr, U16 value)
  167. {
  168. memcpy(memPtr, &value, sizeof(value));
  169. }
  170. #endif // LZ4_FORCE_MEMORY_ACCESS
  171. static U16 LZ4_readLE16(const void* memPtr)
  172. {
  173. if (LZ4_isLittleEndian())
  174. {
  175. return LZ4_read16(memPtr);
  176. }
  177. else
  178. {
  179. const BYTE* p = (const BYTE*)memPtr;
  180. return (U16)((U16)p[0] + (p[1]<<8));
  181. }
  182. }
  183. static void LZ4_writeLE16(void* memPtr, U16 value)
  184. {
  185. if (LZ4_isLittleEndian())
  186. {
  187. LZ4_write16(memPtr, value);
  188. }
  189. else
  190. {
  191. BYTE* p = (BYTE*)memPtr;
  192. p[0] = (BYTE) value;
  193. p[1] = (BYTE)(value>>8);
  194. }
  195. }
  196. static void LZ4_copy8(void* dst, const void* src)
  197. {
  198. memcpy(dst,src,8);
  199. }
  200. /* customized variant of memcpy, which can overwrite up to 7 bytes beyond dstEnd */
  201. static void LZ4_wildCopy(void* dstPtr, const void* srcPtr, void* dstEnd)
  202. {
  203. BYTE* d = (BYTE*)dstPtr;
  204. const BYTE* s = (const BYTE*)srcPtr;
  205. BYTE* const e = (BYTE*)dstEnd;
  206. #if 0
  207. const size_t l2 = 8 - (((size_t)d) & (sizeof(void*)-1));
  208. LZ4_copy8(d,s); if (d>e-9) return;
  209. d+=l2; s+=l2;
  210. #endif /* join to align */
  211. do { LZ4_copy8(d,s); d+=8; s+=8; } while (d<e);
  212. }
  213. /**************************************
  214. * Common Constants
  215. **************************************/
  216. #define MINMATCH 4
  217. #define WILDCOPYLENGTH 8
  218. #define LASTLITERALS 5
  219. #define MFLIMIT (WILDCOPYLENGTH+MINMATCH)
  220. static const int LZ4_minLength = (MFLIMIT+1);
  221. #define KB *(1 <<10)
  222. #define MB *(1 <<20)
  223. #define GB *(1U<<30)
  224. #define MAXD_LOG 16
  225. #define MAX_DISTANCE ((1 << MAXD_LOG) - 1)
  226. #define ML_BITS 4
  227. #define ML_MASK ((1U<<ML_BITS)-1)
  228. #define RUN_BITS (8-ML_BITS)
  229. #define RUN_MASK ((1U<<RUN_BITS)-1)
  230. /**************************************
  231. * Common Utils
  232. **************************************/
  233. #define LZ4_STATIC_ASSERT(c) { enum { LZ4_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */
  234. /**************************************
  235. * Common functions
  236. **************************************/
  237. static unsigned LZ4_NbCommonBytes (register size_t val)
  238. {
  239. if (LZ4_isLittleEndian())
  240. {
  241. if (LZ4_64bits())
  242. {
  243. # if defined(_MSC_VER) && defined(_WIN64) && !defined(LZ4_FORCE_SW_BITCOUNT)
  244. unsigned long r = 0;
  245. _BitScanForward64( &r, (U64)val );
  246. return (int)(r>>3);
  247. # elif (defined(__clang__) || (LZ4_GCC_VERSION >= 304)) && !defined(LZ4_FORCE_SW_BITCOUNT)
  248. return (__builtin_ctzll((U64)val) >> 3);
  249. # else
  250. 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 };
  251. return DeBruijnBytePos[((U64)((val & -(long long)val) * 0x0218A392CDABBD3FULL)) >> 58];
  252. # endif
  253. }
  254. else /* 32 bits */
  255. {
  256. # if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
  257. unsigned long r;
  258. _BitScanForward( &r, (U32)val );
  259. return (int)(r>>3);
  260. # elif (defined(__clang__) || (LZ4_GCC_VERSION >= 304)) && !defined(LZ4_FORCE_SW_BITCOUNT)
  261. return (__builtin_ctz((U32)val) >> 3);
  262. # else
  263. 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 };
  264. return DeBruijnBytePos[((U32)((val & -(S32)val) * 0x077CB531U)) >> 27];
  265. # endif
  266. }
  267. }
  268. else /* Big Endian CPU */
  269. {
  270. if (LZ4_64bits())
  271. {
  272. # if defined(_MSC_VER) && defined(_WIN64) && !defined(LZ4_FORCE_SW_BITCOUNT)
  273. unsigned long r = 0;
  274. _BitScanReverse64( &r, val );
  275. return (unsigned)(r>>3);
  276. # elif (defined(__clang__) || (LZ4_GCC_VERSION >= 304)) && !defined(LZ4_FORCE_SW_BITCOUNT)
  277. return (__builtin_clzll((U64)val) >> 3);
  278. # else
  279. unsigned r;
  280. if (!(val>>32)) { r=4; } else { r=0; val>>=32; }
  281. if (!(val>>16)) { r+=2; val>>=8; } else { val>>=24; }
  282. r += (!val);
  283. return r;
  284. # endif
  285. }
  286. else /* 32 bits */
  287. {
  288. # if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
  289. unsigned long r = 0;
  290. _BitScanReverse( &r, (unsigned long)val );
  291. return (unsigned)(r>>3);
  292. # elif (defined(__clang__) || (LZ4_GCC_VERSION >= 304)) && !defined(LZ4_FORCE_SW_BITCOUNT)
  293. return (__builtin_clz((U32)val) >> 3);
  294. # else
  295. unsigned r;
  296. if (!(val>>16)) { r=2; val>>=8; } else { r=0; val>>=24; }
  297. r += (!val);
  298. return r;
  299. # endif
  300. }
  301. }
  302. }
  303. static unsigned LZ4_count(const BYTE* pIn, const BYTE* pMatch, const BYTE* pInLimit)
  304. {
  305. const BYTE* const pStart = pIn;
  306. while (likely(pIn<pInLimit-(STEPSIZE-1)))
  307. {
  308. size_t diff = LZ4_read_ARCH(pMatch) ^ LZ4_read_ARCH(pIn);
  309. if (!diff) { pIn+=STEPSIZE; pMatch+=STEPSIZE; continue; }
  310. pIn += LZ4_NbCommonBytes(diff);
  311. return (unsigned)(pIn - pStart);
  312. }
  313. if (LZ4_64bits()) if ((pIn<(pInLimit-3)) && (LZ4_read32(pMatch) == LZ4_read32(pIn))) { pIn+=4; pMatch+=4; }
  314. if ((pIn<(pInLimit-1)) && (LZ4_read16(pMatch) == LZ4_read16(pIn))) { pIn+=2; pMatch+=2; }
  315. if ((pIn<pInLimit) && (*pMatch == *pIn)) pIn++;
  316. return (unsigned)(pIn - pStart);
  317. }
  318. #ifndef LZ4_COMMONDEFS_ONLY
  319. /**************************************
  320. * Local Constants
  321. **************************************/
  322. #define LZ4_HASHLOG (LZ4_MEMORY_USAGE-2)
  323. #define HASHTABLESIZE (1 << LZ4_MEMORY_USAGE)
  324. #define HASH_SIZE_U32 (1 << LZ4_HASHLOG) /* required as macro for static allocation */
  325. static const int LZ4_64Klimit = ((64 KB) + (MFLIMIT-1));
  326. static const U32 LZ4_skipTrigger = 6; /* Increase this value ==> compression run slower on incompressible data */
  327. /**************************************
  328. * Local Structures and types
  329. **************************************/
  330. typedef struct {
  331. U32 hashTable[HASH_SIZE_U32];
  332. U32 currentOffset;
  333. U32 initCheck;
  334. const BYTE* dictionary;
  335. BYTE* bufferStart; /* obsolete, used for slideInputBuffer */
  336. U32 dictSize;
  337. } LZ4_stream_t_internal;
  338. typedef enum { notLimited = 0, limitedOutput = 1 } limitedOutput_directive;
  339. typedef enum { byPtr, byU32, byU16 } tableType_t;
  340. typedef enum { noDict = 0, withPrefix64k, usingExtDict } dict_directive;
  341. typedef enum { noDictIssue = 0, dictSmall } dictIssue_directive;
  342. typedef enum { endOnOutputSize = 0, endOnInputSize = 1 } endCondition_directive;
  343. typedef enum { full = 0, partial = 1 } earlyEnd_directive;
  344. /**************************************
  345. * Local Utils
  346. **************************************/
  347. int LZ4_versionNumber (void) { return LZ4_VERSION_NUMBER; }
  348. int LZ4_compressBound(int isize) { return LZ4_COMPRESSBOUND(isize); }
  349. int LZ4_sizeofState() { return LZ4_STREAMSIZE; }
  350. /********************************
  351. * Compression functions
  352. ********************************/
  353. static U32 LZ4_hashSequence(U32 sequence, tableType_t const tableType)
  354. {
  355. if (tableType == byU16)
  356. return (((sequence) * 2654435761U) >> ((MINMATCH*8)-(LZ4_HASHLOG+1)));
  357. else
  358. return (((sequence) * 2654435761U) >> ((MINMATCH*8)-LZ4_HASHLOG));
  359. }
  360. static const U64 prime5bytes = 889523592379ULL;
  361. static U32 LZ4_hashSequence64(size_t sequence, tableType_t const tableType)
  362. {
  363. const U32 hashLog = (tableType == byU16) ? LZ4_HASHLOG+1 : LZ4_HASHLOG;
  364. const U32 hashMask = (1<<hashLog) - 1;
  365. return ((sequence * prime5bytes) >> (40 - hashLog)) & hashMask;
  366. }
  367. static U32 LZ4_hashSequenceT(size_t sequence, tableType_t const tableType)
  368. {
  369. if (LZ4_64bits())
  370. return LZ4_hashSequence64(sequence, tableType);
  371. return LZ4_hashSequence((U32)sequence, tableType);
  372. }
  373. static U32 LZ4_hashPosition(const void* p, tableType_t tableType) { return LZ4_hashSequenceT(LZ4_read_ARCH(p), tableType); }
  374. static void LZ4_putPositionOnHash(const BYTE* p, U32 h, void* tableBase, tableType_t const tableType, const BYTE* srcBase)
  375. {
  376. switch (tableType)
  377. {
  378. case byPtr: { const BYTE** hashTable = (const BYTE**)tableBase; hashTable[h] = p; return; }
  379. case byU32: { U32* hashTable = (U32*) tableBase; hashTable[h] = (U32)(p-srcBase); return; }
  380. case byU16: { U16* hashTable = (U16*) tableBase; hashTable[h] = (U16)(p-srcBase); return; }
  381. }
  382. }
  383. static void LZ4_putPosition(const BYTE* p, void* tableBase, tableType_t tableType, const BYTE* srcBase)
  384. {
  385. U32 h = LZ4_hashPosition(p, tableType);
  386. LZ4_putPositionOnHash(p, h, tableBase, tableType, srcBase);
  387. }
  388. static const BYTE* LZ4_getPositionOnHash(U32 h, void* tableBase, tableType_t tableType, const BYTE* srcBase)
  389. {
  390. if (tableType == byPtr) { const BYTE** hashTable = (const BYTE**) tableBase; return hashTable[h]; }
  391. if (tableType == byU32) { U32* hashTable = (U32*) tableBase; return hashTable[h] + srcBase; }
  392. { U16* hashTable = (U16*) tableBase; return hashTable[h] + srcBase; } /* default, to ensure a return */
  393. }
  394. static const BYTE* LZ4_getPosition(const BYTE* p, void* tableBase, tableType_t tableType, const BYTE* srcBase)
  395. {
  396. U32 h = LZ4_hashPosition(p, tableType);
  397. return LZ4_getPositionOnHash(h, tableBase, tableType, srcBase);
  398. }
  399. FORCE_INLINE int LZ4_compress_generic(
  400. void* const ctx,
  401. const char* const source,
  402. char* const dest,
  403. const int inputSize,
  404. const int maxOutputSize,
  405. const limitedOutput_directive outputLimited,
  406. const tableType_t tableType,
  407. const dict_directive dict,
  408. const dictIssue_directive dictIssue,
  409. const U32 acceleration)
  410. {
  411. LZ4_stream_t_internal* const dictPtr = (LZ4_stream_t_internal*)ctx;
  412. const BYTE* ip = (const BYTE*) source;
  413. const BYTE* base;
  414. const BYTE* lowLimit;
  415. const BYTE* const lowRefLimit = ip - dictPtr->dictSize;
  416. const BYTE* const dictionary = dictPtr->dictionary;
  417. const BYTE* const dictEnd = dictionary + dictPtr->dictSize;
  418. const size_t dictDelta = dictEnd - (const BYTE*)source;
  419. const BYTE* anchor = (const BYTE*) source;
  420. const BYTE* const iend = ip + inputSize;
  421. const BYTE* const mflimit = iend - MFLIMIT;
  422. const BYTE* const matchlimit = iend - LASTLITERALS;
  423. BYTE* op = (BYTE*) dest;
  424. BYTE* const olimit = op + maxOutputSize;
  425. U32 forwardH;
  426. size_t refDelta=0;
  427. /* Init conditions */
  428. if ((U32)inputSize > (U32)LZ4_MAX_INPUT_SIZE) return 0; /* Unsupported input size, too large (or negative) */
  429. switch(dict)
  430. {
  431. case noDict:
  432. default:
  433. base = (const BYTE*)source;
  434. lowLimit = (const BYTE*)source;
  435. break;
  436. case withPrefix64k:
  437. base = (const BYTE*)source - dictPtr->currentOffset;
  438. lowLimit = (const BYTE*)source - dictPtr->dictSize;
  439. break;
  440. case usingExtDict:
  441. base = (const BYTE*)source - dictPtr->currentOffset;
  442. lowLimit = (const BYTE*)source;
  443. break;
  444. }
  445. if ((tableType == byU16) && (inputSize>=LZ4_64Klimit)) return 0; /* Size too large (not within 64K limit) */
  446. if (inputSize<LZ4_minLength) goto _last_literals; /* Input too small, no compression (all literals) */
  447. /* First Byte */
  448. LZ4_putPosition(ip, ctx, tableType, base);
  449. ip++; forwardH = LZ4_hashPosition(ip, tableType);
  450. /* Main Loop */
  451. for ( ; ; )
  452. {
  453. const BYTE* match;
  454. BYTE* token;
  455. {
  456. const BYTE* forwardIp = ip;
  457. unsigned step = 1;
  458. unsigned searchMatchNb = acceleration << LZ4_skipTrigger;
  459. /* Find a match */
  460. do {
  461. U32 h = forwardH;
  462. ip = forwardIp;
  463. forwardIp += step;
  464. step = (searchMatchNb++ >> LZ4_skipTrigger);
  465. if (unlikely(forwardIp > mflimit)) goto _last_literals;
  466. match = LZ4_getPositionOnHash(h, ctx, tableType, base);
  467. if (dict==usingExtDict)
  468. {
  469. if (match<(const BYTE*)source)
  470. {
  471. refDelta = dictDelta;
  472. lowLimit = dictionary;
  473. }
  474. else
  475. {
  476. refDelta = 0;
  477. lowLimit = (const BYTE*)source;
  478. }
  479. }
  480. forwardH = LZ4_hashPosition(forwardIp, tableType);
  481. LZ4_putPositionOnHash(ip, h, ctx, tableType, base);
  482. } while ( ((dictIssue==dictSmall) ? (match < lowRefLimit) : 0)
  483. || ((tableType==byU16) ? 0 : (match + MAX_DISTANCE < ip))
  484. || (LZ4_read32(match+refDelta) != LZ4_read32(ip)) );
  485. }
  486. /* Catch up */
  487. while ((ip>anchor) && (match+refDelta > lowLimit) && (unlikely(ip[-1]==match[refDelta-1]))) { ip--; match--; }
  488. {
  489. /* Encode Literal length */
  490. unsigned litLength = (unsigned)(ip - anchor);
  491. token = op++;
  492. if ((outputLimited) && (unlikely(op + litLength + (2 + 1 + LASTLITERALS) + (litLength/255) > olimit)))
  493. return 0; /* Check output limit */
  494. if (litLength>=RUN_MASK)
  495. {
  496. int len = (int)litLength-RUN_MASK;
  497. *token=(RUN_MASK<<ML_BITS);
  498. for(; len >= 255 ; len-=255) *op++ = 255;
  499. *op++ = (BYTE)len;
  500. }
  501. else *token = (BYTE)(litLength<<ML_BITS);
  502. /* Copy Literals */
  503. LZ4_wildCopy(op, anchor, op+litLength);
  504. op+=litLength;
  505. }
  506. _next_match:
  507. /* Encode Offset */
  508. LZ4_writeLE16(op, (U16)(ip-match)); op+=2;
  509. /* Encode MatchLength */
  510. {
  511. unsigned matchLength;
  512. if ((dict==usingExtDict) && (lowLimit==dictionary))
  513. {
  514. const BYTE* limit;
  515. match += refDelta;
  516. limit = ip + (dictEnd-match);
  517. if (limit > matchlimit) limit = matchlimit;
  518. matchLength = LZ4_count(ip+MINMATCH, match+MINMATCH, limit);
  519. ip += MINMATCH + matchLength;
  520. if (ip==limit)
  521. {
  522. unsigned more = LZ4_count(ip, (const BYTE*)source, matchlimit);
  523. matchLength += more;
  524. ip += more;
  525. }
  526. }
  527. else
  528. {
  529. matchLength = LZ4_count(ip+MINMATCH, match+MINMATCH, matchlimit);
  530. ip += MINMATCH + matchLength;
  531. }
  532. if ((outputLimited) && (unlikely(op + (1 + LASTLITERALS) + (matchLength>>8) > olimit)))
  533. return 0; /* Check output limit */
  534. if (matchLength>=ML_MASK)
  535. {
  536. *token += ML_MASK;
  537. matchLength -= ML_MASK;
  538. for (; matchLength >= 510 ; matchLength-=510) { *op++ = 255; *op++ = 255; }
  539. if (matchLength >= 255) { matchLength-=255; *op++ = 255; }
  540. *op++ = (BYTE)matchLength;
  541. }
  542. else *token += (BYTE)(matchLength);
  543. }
  544. anchor = ip;
  545. /* Test end of chunk */
  546. if (ip > mflimit) break;
  547. /* Fill table */
  548. LZ4_putPosition(ip-2, ctx, tableType, base);
  549. /* Test next position */
  550. match = LZ4_getPosition(ip, ctx, tableType, base);
  551. if (dict==usingExtDict)
  552. {
  553. if (match<(const BYTE*)source)
  554. {
  555. refDelta = dictDelta;
  556. lowLimit = dictionary;
  557. }
  558. else
  559. {
  560. refDelta = 0;
  561. lowLimit = (const BYTE*)source;
  562. }
  563. }
  564. LZ4_putPosition(ip, ctx, tableType, base);
  565. if ( ((dictIssue==dictSmall) ? (match>=lowRefLimit) : 1)
  566. && (match+MAX_DISTANCE>=ip)
  567. && (LZ4_read32(match+refDelta)==LZ4_read32(ip)) )
  568. { token=op++; *token=0; goto _next_match; }
  569. /* Prepare next loop */
  570. forwardH = LZ4_hashPosition(++ip, tableType);
  571. }
  572. _last_literals:
  573. /* Encode Last Literals */
  574. {
  575. const size_t lastRun = (size_t)(iend - anchor);
  576. if ((outputLimited) && ((op - (BYTE*)dest) + lastRun + 1 + ((lastRun+255-RUN_MASK)/255) > (U32)maxOutputSize))
  577. return 0; /* Check output limit */
  578. if (lastRun >= RUN_MASK)
  579. {
  580. size_t accumulator = lastRun - RUN_MASK;
  581. *op++ = RUN_MASK << ML_BITS;
  582. for(; accumulator >= 255 ; accumulator-=255) *op++ = 255;
  583. *op++ = (BYTE) accumulator;
  584. }
  585. else
  586. {
  587. *op++ = (BYTE)(lastRun<<ML_BITS);
  588. }
  589. memcpy(op, anchor, lastRun);
  590. op += lastRun;
  591. }
  592. /* End */
  593. return (int) (((char*)op)-dest);
  594. }
  595. int LZ4_compress_fast_extState(void* state, const char* source, char* dest, int inputSize, int maxOutputSize, int acceleration)
  596. {
  597. LZ4_resetStream((LZ4_stream_t*)state);
  598. if (acceleration < 1) acceleration = ACCELERATION_DEFAULT;
  599. if (maxOutputSize >= LZ4_compressBound(inputSize))
  600. {
  601. if (inputSize < LZ4_64Klimit)
  602. return LZ4_compress_generic(state, source, dest, inputSize, 0, notLimited, byU16, noDict, noDictIssue, acceleration);
  603. else
  604. return LZ4_compress_generic(state, source, dest, inputSize, 0, notLimited, LZ4_64bits() ? byU32 : byPtr, noDict, noDictIssue, acceleration);
  605. }
  606. else
  607. {
  608. if (inputSize < LZ4_64Klimit)
  609. return LZ4_compress_generic(state, source, dest, inputSize, maxOutputSize, limitedOutput, byU16, noDict, noDictIssue, acceleration);
  610. else
  611. return LZ4_compress_generic(state, source, dest, inputSize, maxOutputSize, limitedOutput, LZ4_64bits() ? byU32 : byPtr, noDict, noDictIssue, acceleration);
  612. }
  613. }
  614. int LZ4_compress_fast(const char* source, char* dest, int inputSize, int maxOutputSize, int acceleration)
  615. {
  616. #if (HEAPMODE)
  617. void* ctxPtr = ALLOCATOR(1, sizeof(LZ4_stream_t)); /* malloc-calloc always properly aligned */
  618. #else
  619. LZ4_stream_t ctx;
  620. void* ctxPtr = &ctx;
  621. #endif
  622. int result = LZ4_compress_fast_extState(ctxPtr, source, dest, inputSize, maxOutputSize, acceleration);
  623. #if (HEAPMODE)
  624. FREEMEM(ctxPtr);
  625. #endif
  626. return result;
  627. }
  628. int LZ4_compress_default(const char* source, char* dest, int inputSize, int maxOutputSize)
  629. {
  630. return LZ4_compress_fast(source, dest, inputSize, maxOutputSize, 1);
  631. }
  632. /* hidden debug function */
  633. /* strangely enough, gcc generates faster code when this function is uncommented, even if unused */
  634. int LZ4_compress_fast_force(const char* source, char* dest, int inputSize, int maxOutputSize, int acceleration)
  635. {
  636. LZ4_stream_t ctx;
  637. LZ4_resetStream(&ctx);
  638. if (inputSize < LZ4_64Klimit)
  639. return LZ4_compress_generic(&ctx, source, dest, inputSize, maxOutputSize, limitedOutput, byU16, noDict, noDictIssue, acceleration);
  640. else
  641. return LZ4_compress_generic(&ctx, source, dest, inputSize, maxOutputSize, limitedOutput, LZ4_64bits() ? byU32 : byPtr, noDict, noDictIssue, acceleration);
  642. }
  643. /********************************
  644. * destSize variant
  645. ********************************/
  646. static int LZ4_compress_destSize_generic(
  647. void* const ctx,
  648. const char* const src,
  649. char* const dst,
  650. int* const srcSizePtr,
  651. const int targetDstSize,
  652. const tableType_t tableType)
  653. {
  654. const BYTE* ip = (const BYTE*) src;
  655. const BYTE* base = (const BYTE*) src;
  656. const BYTE* lowLimit = (const BYTE*) src;
  657. const BYTE* anchor = ip;
  658. const BYTE* const iend = ip + *srcSizePtr;
  659. const BYTE* const mflimit = iend - MFLIMIT;
  660. const BYTE* const matchlimit = iend - LASTLITERALS;
  661. BYTE* op = (BYTE*) dst;
  662. BYTE* const oend = op + targetDstSize;
  663. BYTE* const oMaxLit = op + targetDstSize - 2 /* offset */ - 8 /* because 8+MINMATCH==MFLIMIT */ - 1 /* token */;
  664. BYTE* const oMaxMatch = op + targetDstSize - (LASTLITERALS + 1 /* token */);
  665. BYTE* const oMaxSeq = oMaxLit - 1 /* token */;
  666. U32 forwardH;
  667. /* Init conditions */
  668. if (targetDstSize < 1) return 0; /* Impossible to store anything */
  669. if ((U32)*srcSizePtr > (U32)LZ4_MAX_INPUT_SIZE) return 0; /* Unsupported input size, too large (or negative) */
  670. if ((tableType == byU16) && (*srcSizePtr>=LZ4_64Klimit)) return 0; /* Size too large (not within 64K limit) */
  671. if (*srcSizePtr<LZ4_minLength) goto _last_literals; /* Input too small, no compression (all literals) */
  672. /* First Byte */
  673. *srcSizePtr = 0;
  674. LZ4_putPosition(ip, ctx, tableType, base);
  675. ip++; forwardH = LZ4_hashPosition(ip, tableType);
  676. /* Main Loop */
  677. for ( ; ; )
  678. {
  679. const BYTE* match;
  680. BYTE* token;
  681. {
  682. const BYTE* forwardIp = ip;
  683. unsigned step = 1;
  684. unsigned searchMatchNb = 1 << LZ4_skipTrigger;
  685. /* Find a match */
  686. do {
  687. U32 h = forwardH;
  688. ip = forwardIp;
  689. forwardIp += step;
  690. step = (searchMatchNb++ >> LZ4_skipTrigger);
  691. if (unlikely(forwardIp > mflimit))
  692. goto _last_literals;
  693. match = LZ4_getPositionOnHash(h, ctx, tableType, base);
  694. forwardH = LZ4_hashPosition(forwardIp, tableType);
  695. LZ4_putPositionOnHash(ip, h, ctx, tableType, base);
  696. } while ( ((tableType==byU16) ? 0 : (match + MAX_DISTANCE < ip))
  697. || (LZ4_read32(match) != LZ4_read32(ip)) );
  698. }
  699. /* Catch up */
  700. while ((ip>anchor) && (match > lowLimit) && (unlikely(ip[-1]==match[-1]))) { ip--; match--; }
  701. {
  702. /* Encode Literal length */
  703. unsigned litLength = (unsigned)(ip - anchor);
  704. token = op++;
  705. if (op + ((litLength+240)/255) + litLength > oMaxLit)
  706. {
  707. /* Not enough space for a last match */
  708. op--;
  709. goto _last_literals;
  710. }
  711. if (litLength>=RUN_MASK)
  712. {
  713. unsigned len = litLength - RUN_MASK;
  714. *token=(RUN_MASK<<ML_BITS);
  715. for(; len >= 255 ; len-=255) *op++ = 255;
  716. *op++ = (BYTE)len;
  717. }
  718. else *token = (BYTE)(litLength<<ML_BITS);
  719. /* Copy Literals */
  720. LZ4_wildCopy(op, anchor, op+litLength);
  721. op += litLength;
  722. }
  723. _next_match:
  724. /* Encode Offset */
  725. LZ4_writeLE16(op, (U16)(ip-match)); op+=2;
  726. /* Encode MatchLength */
  727. {
  728. size_t matchLength;
  729. matchLength = LZ4_count(ip+MINMATCH, match+MINMATCH, matchlimit);
  730. if (op + ((matchLength+240)/255) > oMaxMatch)
  731. {
  732. /* Match description too long : reduce it */
  733. matchLength = (15-1) + (oMaxMatch-op) * 255;
  734. }
  735. ip += MINMATCH + matchLength;
  736. if (matchLength>=ML_MASK)
  737. {
  738. *token += ML_MASK;
  739. matchLength -= ML_MASK;
  740. while (matchLength >= 255) { matchLength-=255; *op++ = 255; }
  741. *op++ = (BYTE)matchLength;
  742. }
  743. else *token += (BYTE)(matchLength);
  744. }
  745. anchor = ip;
  746. /* Test end of block */
  747. if (ip > mflimit) break;
  748. if (op > oMaxSeq) break;
  749. /* Fill table */
  750. LZ4_putPosition(ip-2, ctx, tableType, base);
  751. /* Test next position */
  752. match = LZ4_getPosition(ip, ctx, tableType, base);
  753. LZ4_putPosition(ip, ctx, tableType, base);
  754. if ( (match+MAX_DISTANCE>=ip)
  755. && (LZ4_read32(match)==LZ4_read32(ip)) )
  756. { token=op++; *token=0; goto _next_match; }
  757. /* Prepare next loop */
  758. forwardH = LZ4_hashPosition(++ip, tableType);
  759. }
  760. _last_literals:
  761. /* Encode Last Literals */
  762. {
  763. size_t lastRunSize = (size_t)(iend - anchor);
  764. if (op + 1 /* token */ + ((lastRunSize+240)/255) /* litLength */ + lastRunSize /* literals */ > oend)
  765. {
  766. /* adapt lastRunSize to fill 'dst' */
  767. lastRunSize = (oend-op) - 1;
  768. lastRunSize -= (lastRunSize+240)/255;
  769. }
  770. ip = anchor + lastRunSize;
  771. if (lastRunSize >= RUN_MASK)
  772. {
  773. size_t accumulator = lastRunSize - RUN_MASK;
  774. *op++ = RUN_MASK << ML_BITS;
  775. for(; accumulator >= 255 ; accumulator-=255) *op++ = 255;
  776. *op++ = (BYTE) accumulator;
  777. }
  778. else
  779. {
  780. *op++ = (BYTE)(lastRunSize<<ML_BITS);
  781. }
  782. memcpy(op, anchor, lastRunSize);
  783. op += lastRunSize;
  784. }
  785. /* End */
  786. *srcSizePtr = (int) (((const char*)ip)-src);
  787. return (int) (((char*)op)-dst);
  788. }
  789. static int LZ4_compress_destSize_extState (void* state, const char* src, char* dst, int* srcSizePtr, int targetDstSize)
  790. {
  791. LZ4_resetStream((LZ4_stream_t*)state);
  792. if (targetDstSize >= LZ4_compressBound(*srcSizePtr)) /* compression success is guaranteed */
  793. {
  794. return LZ4_compress_fast_extState(state, src, dst, *srcSizePtr, targetDstSize, 1);
  795. }
  796. else
  797. {
  798. if (*srcSizePtr < LZ4_64Klimit)
  799. return LZ4_compress_destSize_generic(state, src, dst, srcSizePtr, targetDstSize, byU16);
  800. else
  801. return LZ4_compress_destSize_generic(state, src, dst, srcSizePtr, targetDstSize, LZ4_64bits() ? byU32 : byPtr);
  802. }
  803. }
  804. int LZ4_compress_destSize(const char* src, char* dst, int* srcSizePtr, int targetDstSize)
  805. {
  806. #if (HEAPMODE)
  807. void* ctx = ALLOCATOR(1, sizeof(LZ4_stream_t)); /* malloc-calloc always properly aligned */
  808. #else
  809. LZ4_stream_t ctxBody;
  810. void* ctx = &ctxBody;
  811. #endif
  812. int result = LZ4_compress_destSize_extState(ctx, src, dst, srcSizePtr, targetDstSize);
  813. #if (HEAPMODE)
  814. FREEMEM(ctx);
  815. #endif
  816. return result;
  817. }
  818. /********************************
  819. * Streaming functions
  820. ********************************/
  821. LZ4_stream_t* LZ4_createStream(void)
  822. {
  823. LZ4_stream_t* lz4s = (LZ4_stream_t*)ALLOCATOR(8, LZ4_STREAMSIZE_U64);
  824. LZ4_STATIC_ASSERT(LZ4_STREAMSIZE >= sizeof(LZ4_stream_t_internal)); /* A compilation error here means LZ4_STREAMSIZE is not large enough */
  825. LZ4_resetStream(lz4s);
  826. return lz4s;
  827. }
  828. void LZ4_resetStream (LZ4_stream_t* LZ4_stream)
  829. {
  830. MEM_INIT(LZ4_stream, 0, sizeof(LZ4_stream_t));
  831. }
  832. int LZ4_freeStream (LZ4_stream_t* LZ4_stream)
  833. {
  834. FREEMEM(LZ4_stream);
  835. return (0);
  836. }
  837. #define HASH_UNIT sizeof(size_t)
  838. int LZ4_loadDict (LZ4_stream_t* LZ4_dict, const char* dictionary, int dictSize)
  839. {
  840. LZ4_stream_t_internal* dict = (LZ4_stream_t_internal*) LZ4_dict;
  841. const BYTE* p = (const BYTE*)dictionary;
  842. const BYTE* const dictEnd = p + dictSize;
  843. const BYTE* base;
  844. if ((dict->initCheck) || (dict->currentOffset > 1 GB)) /* Uninitialized structure, or reuse overflow */
  845. LZ4_resetStream(LZ4_dict);
  846. if (dictSize < (int)HASH_UNIT)
  847. {
  848. dict->dictionary = NULL;
  849. dict->dictSize = 0;
  850. return 0;
  851. }
  852. if ((dictEnd - p) > 64 KB) p = dictEnd - 64 KB;
  853. dict->currentOffset += 64 KB;
  854. base = p - dict->currentOffset;
  855. dict->dictionary = p;
  856. dict->dictSize = (U32)(dictEnd - p);
  857. dict->currentOffset += dict->dictSize;
  858. while (p <= dictEnd-HASH_UNIT)
  859. {
  860. LZ4_putPosition(p, dict->hashTable, byU32, base);
  861. p+=3;
  862. }
  863. return dict->dictSize;
  864. }
  865. static void LZ4_renormDictT(LZ4_stream_t_internal* LZ4_dict, const BYTE* src)
  866. {
  867. if ((LZ4_dict->currentOffset > 0x80000000) ||
  868. ((size_t)LZ4_dict->currentOffset > (size_t)src)) /* address space overflow */
  869. {
  870. /* rescale hash table */
  871. U32 delta = LZ4_dict->currentOffset - 64 KB;
  872. const BYTE* dictEnd = LZ4_dict->dictionary + LZ4_dict->dictSize;
  873. int i;
  874. for (i=0; i<HASH_SIZE_U32; i++)
  875. {
  876. if (LZ4_dict->hashTable[i] < delta) LZ4_dict->hashTable[i]=0;
  877. else LZ4_dict->hashTable[i] -= delta;
  878. }
  879. LZ4_dict->currentOffset = 64 KB;
  880. if (LZ4_dict->dictSize > 64 KB) LZ4_dict->dictSize = 64 KB;
  881. LZ4_dict->dictionary = dictEnd - LZ4_dict->dictSize;
  882. }
  883. }
  884. int LZ4_compress_fast_continue (LZ4_stream_t* LZ4_stream, const char* source, char* dest, int inputSize, int maxOutputSize, int acceleration)
  885. {
  886. LZ4_stream_t_internal* streamPtr = (LZ4_stream_t_internal*)LZ4_stream;
  887. const BYTE* const dictEnd = streamPtr->dictionary + streamPtr->dictSize;
  888. const BYTE* smallest = (const BYTE*) source;
  889. if (streamPtr->initCheck) return 0; /* Uninitialized structure detected */
  890. if ((streamPtr->dictSize>0) && (smallest>dictEnd)) smallest = dictEnd;
  891. LZ4_renormDictT(streamPtr, smallest);
  892. if (acceleration < 1) acceleration = ACCELERATION_DEFAULT;
  893. /* Check overlapping input/dictionary space */
  894. {
  895. const BYTE* sourceEnd = (const BYTE*) source + inputSize;
  896. if ((sourceEnd > streamPtr->dictionary) && (sourceEnd < dictEnd))
  897. {
  898. streamPtr->dictSize = (U32)(dictEnd - sourceEnd);
  899. if (streamPtr->dictSize > 64 KB) streamPtr->dictSize = 64 KB;
  900. if (streamPtr->dictSize < 4) streamPtr->dictSize = 0;
  901. streamPtr->dictionary = dictEnd - streamPtr->dictSize;
  902. }
  903. }
  904. /* prefix mode : source data follows dictionary */
  905. if (dictEnd == (const BYTE*)source)
  906. {
  907. int result;
  908. if ((streamPtr->dictSize < 64 KB) && (streamPtr->dictSize < streamPtr->currentOffset))
  909. result = LZ4_compress_generic(LZ4_stream, source, dest, inputSize, maxOutputSize, limitedOutput, byU32, withPrefix64k, dictSmall, acceleration);
  910. else
  911. result = LZ4_compress_generic(LZ4_stream, source, dest, inputSize, maxOutputSize, limitedOutput, byU32, withPrefix64k, noDictIssue, acceleration);
  912. streamPtr->dictSize += (U32)inputSize;
  913. streamPtr->currentOffset += (U32)inputSize;
  914. return result;
  915. }
  916. /* external dictionary mode */
  917. {
  918. int result;
  919. if ((streamPtr->dictSize < 64 KB) && (streamPtr->dictSize < streamPtr->currentOffset))
  920. result = LZ4_compress_generic(LZ4_stream, source, dest, inputSize, maxOutputSize, limitedOutput, byU32, usingExtDict, dictSmall, acceleration);
  921. else
  922. result = LZ4_compress_generic(LZ4_stream, source, dest, inputSize, maxOutputSize, limitedOutput, byU32, usingExtDict, noDictIssue, acceleration);
  923. streamPtr->dictionary = (const BYTE*)source;
  924. streamPtr->dictSize = (U32)inputSize;
  925. streamPtr->currentOffset += (U32)inputSize;
  926. return result;
  927. }
  928. }
  929. /* Hidden debug function, to force external dictionary mode */
  930. int LZ4_compress_forceExtDict (LZ4_stream_t* LZ4_dict, const char* source, char* dest, int inputSize)
  931. {
  932. LZ4_stream_t_internal* streamPtr = (LZ4_stream_t_internal*)LZ4_dict;
  933. int result;
  934. const BYTE* const dictEnd = streamPtr->dictionary + streamPtr->dictSize;
  935. const BYTE* smallest = dictEnd;
  936. if (smallest > (const BYTE*) source) smallest = (const BYTE*) source;
  937. LZ4_renormDictT((LZ4_stream_t_internal*)LZ4_dict, smallest);
  938. result = LZ4_compress_generic(LZ4_dict, source, dest, inputSize, 0, notLimited, byU32, usingExtDict, noDictIssue, 1);
  939. streamPtr->dictionary = (const BYTE*)source;
  940. streamPtr->dictSize = (U32)inputSize;
  941. streamPtr->currentOffset += (U32)inputSize;
  942. return result;
  943. }
  944. int LZ4_saveDict (LZ4_stream_t* LZ4_dict, char* safeBuffer, int dictSize)
  945. {
  946. LZ4_stream_t_internal* dict = (LZ4_stream_t_internal*) LZ4_dict;
  947. const BYTE* previousDictEnd = dict->dictionary + dict->dictSize;
  948. if ((U32)dictSize > 64 KB) dictSize = 64 KB; /* useless to define a dictionary > 64 KB */
  949. if ((U32)dictSize > dict->dictSize) dictSize = dict->dictSize;
  950. memmove(safeBuffer, previousDictEnd - dictSize, dictSize);
  951. dict->dictionary = (const BYTE*)safeBuffer;
  952. dict->dictSize = (U32)dictSize;
  953. return dictSize;
  954. }
  955. /*******************************
  956. * Decompression functions
  957. *******************************/
  958. /*
  959. * This generic decompression function cover all use cases.
  960. * It shall be instantiated several times, using different sets of directives
  961. * Note that it is essential this generic function is really inlined,
  962. * in order to remove useless branches during compilation optimization.
  963. */
  964. FORCE_INLINE int LZ4_decompress_generic(
  965. const char* const source,
  966. char* const dest,
  967. int inputSize,
  968. int outputSize, /* If endOnInput==endOnInputSize, this value is the max size of Output Buffer. */
  969. int endOnInput, /* endOnOutputSize, endOnInputSize */
  970. int partialDecoding, /* full, partial */
  971. int targetOutputSize, /* only used if partialDecoding==partial */
  972. int dict, /* noDict, withPrefix64k, usingExtDict */
  973. const BYTE* const lowPrefix, /* == dest if dict == noDict */
  974. const BYTE* const dictStart, /* only if dict==usingExtDict */
  975. const size_t dictSize /* note : = 0 if noDict */
  976. )
  977. {
  978. /* Local Variables */
  979. const BYTE* ip = (const BYTE*) source;
  980. const BYTE* const iend = ip + inputSize;
  981. BYTE* op = (BYTE*) dest;
  982. BYTE* const oend = op + outputSize;
  983. BYTE* cpy;
  984. BYTE* oexit = op + targetOutputSize;
  985. const BYTE* const lowLimit = lowPrefix - dictSize;
  986. const BYTE* const dictEnd = (const BYTE*)dictStart + dictSize;
  987. const unsigned dec32table[] = {4, 1, 2, 1, 4, 4, 4, 4};
  988. const int dec64table[] = {0, 0, 0, -1, 0, 1, 2, 3};
  989. const int safeDecode = (endOnInput==endOnInputSize);
  990. const int checkOffset = ((safeDecode) && (dictSize < (int)(64 KB)));
  991. /* Special cases */
  992. if ((partialDecoding) && (oexit> oend-MFLIMIT)) oexit = oend-MFLIMIT; /* targetOutputSize too high => decode everything */
  993. if ((endOnInput) && (unlikely(outputSize==0))) return ((inputSize==1) && (*ip==0)) ? 0 : -1; /* Empty output buffer */
  994. if ((!endOnInput) && (unlikely(outputSize==0))) return (*ip==0?1:-1);
  995. /* Main Loop */
  996. while (1)
  997. {
  998. unsigned token;
  999. size_t length;
  1000. const BYTE* match;
  1001. size_t offset;
  1002. /* get literal length */
  1003. token = *ip++;
  1004. if ((length=(token>>ML_BITS)) == RUN_MASK)
  1005. {
  1006. unsigned s;
  1007. do
  1008. {
  1009. s = *ip++;
  1010. length += s;
  1011. }
  1012. while ( likely(endOnInput ? ip<iend-RUN_MASK : 1) && (s==255) );
  1013. if ((safeDecode) && unlikely((size_t)(op+length)<(size_t)(op))) goto _output_error; /* overflow detection */
  1014. if ((safeDecode) && unlikely((size_t)(ip+length)<(size_t)(ip))) goto _output_error; /* overflow detection */
  1015. }
  1016. /* copy literals */
  1017. cpy = op+length;
  1018. if (((endOnInput) && ((cpy>(partialDecoding?oexit:oend-MFLIMIT)) || (ip+length>iend-(2+1+LASTLITERALS))) )
  1019. || ((!endOnInput) && (cpy>oend-WILDCOPYLENGTH)))
  1020. {
  1021. if (partialDecoding)
  1022. {
  1023. if (cpy > oend) goto _output_error; /* Error : write attempt beyond end of output buffer */
  1024. if ((endOnInput) && (ip+length > iend)) goto _output_error; /* Error : read attempt beyond end of input buffer */
  1025. }
  1026. else
  1027. {
  1028. if ((!endOnInput) && (cpy != oend)) goto _output_error; /* Error : block decoding must stop exactly there */
  1029. if ((endOnInput) && ((ip+length != iend) || (cpy > oend))) goto _output_error; /* Error : input must be consumed */
  1030. }
  1031. memcpy(op, ip, length);
  1032. ip += length;
  1033. op += length;
  1034. break; /* Necessarily EOF, due to parsing restrictions */
  1035. }
  1036. LZ4_wildCopy(op, ip, cpy);
  1037. ip += length; op = cpy;
  1038. /* get offset */
  1039. offset = LZ4_readLE16(ip); ip+=2;
  1040. match = op - offset;
  1041. if ((checkOffset) && (unlikely(match < lowLimit))) goto _output_error; /* Error : offset outside buffers */
  1042. /* get matchlength */
  1043. length = token & ML_MASK;
  1044. if (length == ML_MASK)
  1045. {
  1046. unsigned s;
  1047. do
  1048. {
  1049. if ((endOnInput) && (ip > iend-LASTLITERALS)) goto _output_error;
  1050. s = *ip++;
  1051. length += s;
  1052. } while (s==255);
  1053. if ((safeDecode) && unlikely((size_t)(op+length)<(size_t)op)) goto _output_error; /* overflow detection */
  1054. }
  1055. length += MINMATCH;
  1056. /* check external dictionary */
  1057. if ((dict==usingExtDict) && (match < lowPrefix))
  1058. {
  1059. if (unlikely(op+length > oend-LASTLITERALS)) goto _output_error; /* doesn't respect parsing restriction */
  1060. if (length <= (size_t)(lowPrefix-match))
  1061. {
  1062. /* match can be copied as a single segment from external dictionary */
  1063. match = dictEnd - (lowPrefix-match);
  1064. memmove(op, match, length); op += length;
  1065. }
  1066. else
  1067. {
  1068. /* match encompass external dictionary and current block */
  1069. size_t copySize = (size_t)(lowPrefix-match);
  1070. memcpy(op, dictEnd - copySize, copySize);
  1071. op += copySize;
  1072. copySize = length - copySize;
  1073. if (copySize > (size_t)(op-lowPrefix)) /* overlap copy */
  1074. {
  1075. BYTE* const endOfMatch = op + copySize;
  1076. const BYTE* copyFrom = lowPrefix;
  1077. while (op < endOfMatch) *op++ = *copyFrom++;
  1078. }
  1079. else
  1080. {
  1081. memcpy(op, lowPrefix, copySize);
  1082. op += copySize;
  1083. }
  1084. }
  1085. continue;
  1086. }
  1087. /* copy match within block */
  1088. cpy = op + length;
  1089. if (unlikely(offset<8))
  1090. {
  1091. const int dec64 = dec64table[offset];
  1092. op[0] = match[0];
  1093. op[1] = match[1];
  1094. op[2] = match[2];
  1095. op[3] = match[3];
  1096. match += dec32table[offset];
  1097. memcpy(op+4, match, 4);
  1098. match -= dec64;
  1099. } else { LZ4_copy8(op, match); match+=8; }
  1100. op += 8;
  1101. if (unlikely(cpy>oend-12))
  1102. {
  1103. BYTE* const oCopyLimit = oend-(WILDCOPYLENGTH-1);
  1104. if (cpy > oend-LASTLITERALS) goto _output_error; /* Error : last LASTLITERALS bytes must be literals (uncompressed) */
  1105. if (op < oCopyLimit)
  1106. {
  1107. LZ4_wildCopy(op, match, oCopyLimit);
  1108. match += oCopyLimit - op;
  1109. op = oCopyLimit;
  1110. }
  1111. while (op<cpy) *op++ = *match++;
  1112. }
  1113. else
  1114. LZ4_wildCopy(op, match, cpy);
  1115. op=cpy; /* correction */
  1116. }
  1117. /* end of decoding */
  1118. if (endOnInput)
  1119. return (int) (((char*)op)-dest); /* Nb of output bytes decoded */
  1120. else
  1121. return (int) (((const char*)ip)-source); /* Nb of input bytes read */
  1122. /* Overflow error detected */
  1123. _output_error:
  1124. return (int) (-(((const char*)ip)-source))-1;
  1125. }
  1126. int LZ4_decompress_safe(const char* source, char* dest, int compressedSize, int maxDecompressedSize)
  1127. {
  1128. return LZ4_decompress_generic(source, dest, compressedSize, maxDecompressedSize, endOnInputSize, full, 0, noDict, (BYTE*)dest, NULL, 0);
  1129. }
  1130. int LZ4_decompress_safe_partial(const char* source, char* dest, int compressedSize, int targetOutputSize, int maxDecompressedSize)
  1131. {
  1132. return LZ4_decompress_generic(source, dest, compressedSize, maxDecompressedSize, endOnInputSize, partial, targetOutputSize, noDict, (BYTE*)dest, NULL, 0);
  1133. }
  1134. int LZ4_decompress_fast(const char* source, char* dest, int originalSize)
  1135. {
  1136. return LZ4_decompress_generic(source, dest, 0, originalSize, endOnOutputSize, full, 0, withPrefix64k, (BYTE*)(dest - 64 KB), NULL, 64 KB);
  1137. }
  1138. /* streaming decompression functions */
  1139. typedef struct
  1140. {
  1141. const BYTE* externalDict;
  1142. size_t extDictSize;
  1143. const BYTE* prefixEnd;
  1144. size_t prefixSize;
  1145. } LZ4_streamDecode_t_internal;
  1146. /*
  1147. * If you prefer dynamic allocation methods,
  1148. * LZ4_createStreamDecode()
  1149. * provides a pointer (void*) towards an initialized LZ4_streamDecode_t structure.
  1150. */
  1151. LZ4_streamDecode_t* LZ4_createStreamDecode(void)
  1152. {
  1153. LZ4_streamDecode_t* lz4s = (LZ4_streamDecode_t*) ALLOCATOR(1, sizeof(LZ4_streamDecode_t));
  1154. return lz4s;
  1155. }
  1156. int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream)
  1157. {
  1158. FREEMEM(LZ4_stream);
  1159. return 0;
  1160. }
  1161. /*
  1162. * LZ4_setStreamDecode
  1163. * Use this function to instruct where to find the dictionary
  1164. * This function is not necessary if previous data is still available where it was decoded.
  1165. * Loading a size of 0 is allowed (same effect as no dictionary).
  1166. * Return : 1 if OK, 0 if error
  1167. */
  1168. int LZ4_setStreamDecode (LZ4_streamDecode_t* LZ4_streamDecode, const char* dictionary, int dictSize)
  1169. {
  1170. LZ4_streamDecode_t_internal* lz4sd = (LZ4_streamDecode_t_internal*) LZ4_streamDecode;
  1171. lz4sd->prefixSize = (size_t) dictSize;
  1172. lz4sd->prefixEnd = (const BYTE*) dictionary + dictSize;
  1173. lz4sd->externalDict = NULL;
  1174. lz4sd->extDictSize = 0;
  1175. return 1;
  1176. }
  1177. /*
  1178. *_continue() :
  1179. These decoding functions allow decompression of multiple blocks in "streaming" mode.
  1180. Previously decoded blocks must still be available at the memory position where they were decoded.
  1181. If it's not possible, save the relevant part of decoded data into a safe buffer,
  1182. and indicate where it stands using LZ4_setStreamDecode()
  1183. */
  1184. int LZ4_decompress_safe_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* source, char* dest, int compressedSize, int maxOutputSize)
  1185. {
  1186. LZ4_streamDecode_t_internal* lz4sd = (LZ4_streamDecode_t_internal*) LZ4_streamDecode;
  1187. int result;
  1188. if (lz4sd->prefixEnd == (BYTE*)dest)
  1189. {
  1190. result = LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize,
  1191. endOnInputSize, full, 0,
  1192. usingExtDict, lz4sd->prefixEnd - lz4sd->prefixSize, lz4sd->externalDict, lz4sd->extDictSize);
  1193. if (result <= 0) return result;
  1194. lz4sd->prefixSize += result;
  1195. lz4sd->prefixEnd += result;
  1196. }
  1197. else
  1198. {
  1199. lz4sd->extDictSize = lz4sd->prefixSize;
  1200. lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize;
  1201. result = LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize,
  1202. endOnInputSize, full, 0,
  1203. usingExtDict, (BYTE*)dest, lz4sd->externalDict, lz4sd->extDictSize);
  1204. if (result <= 0) return result;
  1205. lz4sd->prefixSize = result;
  1206. lz4sd->prefixEnd = (BYTE*)dest + result;
  1207. }
  1208. return result;
  1209. }
  1210. int LZ4_decompress_fast_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* source, char* dest, int originalSize)
  1211. {
  1212. LZ4_streamDecode_t_internal* lz4sd = (LZ4_streamDecode_t_internal*) LZ4_streamDecode;
  1213. int result;
  1214. if (lz4sd->prefixEnd == (BYTE*)dest)
  1215. {
  1216. result = LZ4_decompress_generic(source, dest, 0, originalSize,
  1217. endOnOutputSize, full, 0,
  1218. usingExtDict, lz4sd->prefixEnd - lz4sd->prefixSize, lz4sd->externalDict, lz4sd->extDictSize);
  1219. if (result <= 0) return result;
  1220. lz4sd->prefixSize += originalSize;
  1221. lz4sd->prefixEnd += originalSize;
  1222. }
  1223. else
  1224. {
  1225. lz4sd->extDictSize = lz4sd->prefixSize;
  1226. lz4sd->externalDict = (BYTE*)dest - lz4sd->extDictSize;
  1227. result = LZ4_decompress_generic(source, dest, 0, originalSize,
  1228. endOnOutputSize, full, 0,
  1229. usingExtDict, (BYTE*)dest, lz4sd->externalDict, lz4sd->extDictSize);
  1230. if (result <= 0) return result;
  1231. lz4sd->prefixSize = originalSize;
  1232. lz4sd->prefixEnd = (BYTE*)dest + originalSize;
  1233. }
  1234. return result;
  1235. }
  1236. /*
  1237. Advanced decoding functions :
  1238. *_usingDict() :
  1239. These decoding functions work the same as "_continue" ones,
  1240. the dictionary must be explicitly provided within parameters
  1241. */
  1242. FORCE_INLINE int LZ4_decompress_usingDict_generic(const char* source, char* dest, int compressedSize, int maxOutputSize, int safe, const char* dictStart, int dictSize)
  1243. {
  1244. if (dictSize==0)
  1245. return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, safe, full, 0, noDict, (BYTE*)dest, NULL, 0);
  1246. if (dictStart+dictSize == dest)
  1247. {
  1248. if (dictSize >= (int)(64 KB - 1))
  1249. return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, safe, full, 0, withPrefix64k, (BYTE*)dest-64 KB, NULL, 0);
  1250. return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, safe, full, 0, noDict, (BYTE*)dest-dictSize, NULL, 0);
  1251. }
  1252. return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, safe, full, 0, usingExtDict, (BYTE*)dest, (const BYTE*)dictStart, dictSize);
  1253. }
  1254. int LZ4_decompress_safe_usingDict(const char* source, char* dest, int compressedSize, int maxOutputSize, const char* dictStart, int dictSize)
  1255. {
  1256. return LZ4_decompress_usingDict_generic(source, dest, compressedSize, maxOutputSize, 1, dictStart, dictSize);
  1257. }
  1258. int LZ4_decompress_fast_usingDict(const char* source, char* dest, int originalSize, const char* dictStart, int dictSize)
  1259. {
  1260. return LZ4_decompress_usingDict_generic(source, dest, 0, originalSize, 0, dictStart, dictSize);
  1261. }
  1262. /* debug function */
  1263. int LZ4_decompress_safe_forceExtDict(const char* source, char* dest, int compressedSize, int maxOutputSize, const char* dictStart, int dictSize)
  1264. {
  1265. return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, endOnInputSize, full, 0, usingExtDict, (BYTE*)dest, (const BYTE*)dictStart, dictSize);
  1266. }
  1267. /***************************************************
  1268. * Obsolete Functions
  1269. ***************************************************/
  1270. /* obsolete compression functions */
  1271. int LZ4_compress_limitedOutput(const char* source, char* dest, int inputSize, int maxOutputSize) { return LZ4_compress_default(source, dest, inputSize, maxOutputSize); }
  1272. int LZ4_compress(const char* source, char* dest, int inputSize) { return LZ4_compress_default(source, dest, inputSize, LZ4_compressBound(inputSize)); }
  1273. int LZ4_compress_limitedOutput_withState (void* state, const char* src, char* dst, int srcSize, int dstSize) { return LZ4_compress_fast_extState(state, src, dst, srcSize, dstSize, 1); }
  1274. int LZ4_compress_withState (void* state, const char* src, char* dst, int srcSize) { return LZ4_compress_fast_extState(state, src, dst, srcSize, LZ4_compressBound(srcSize), 1); }
  1275. int LZ4_compress_limitedOutput_continue (LZ4_stream_t* LZ4_stream, const char* src, char* dst, int srcSize, int maxDstSize) { return LZ4_compress_fast_continue(LZ4_stream, src, dst, srcSize, maxDstSize, 1); }
  1276. int LZ4_compress_continue (LZ4_stream_t* LZ4_stream, const char* source, char* dest, int inputSize) { return LZ4_compress_fast_continue(LZ4_stream, source, dest, inputSize, LZ4_compressBound(inputSize), 1); }
  1277. /*
  1278. These function names are deprecated and should no longer be used.
  1279. They are only provided here for compatibility with older user programs.
  1280. - LZ4_uncompress is totally equivalent to LZ4_decompress_fast
  1281. - LZ4_uncompress_unknownOutputSize is totally equivalent to LZ4_decompress_safe
  1282. */
  1283. int LZ4_uncompress (const char* source, char* dest, int outputSize) { return LZ4_decompress_fast(source, dest, outputSize); }
  1284. int LZ4_uncompress_unknownOutputSize (const char* source, char* dest, int isize, int maxOutputSize) { return LZ4_decompress_safe(source, dest, isize, maxOutputSize); }
  1285. /* Obsolete Streaming functions */
  1286. int LZ4_sizeofStreamState() { return LZ4_STREAMSIZE; }
  1287. static void LZ4_init(LZ4_stream_t_internal* lz4ds, BYTE* base)
  1288. {
  1289. MEM_INIT(lz4ds, 0, LZ4_STREAMSIZE);
  1290. lz4ds->bufferStart = base;
  1291. }
  1292. int LZ4_resetStreamState(void* state, char* inputBuffer)
  1293. {
  1294. if ((((size_t)state) & 3) != 0) return 1; /* Error : pointer is not aligned on 4-bytes boundary */
  1295. LZ4_init((LZ4_stream_t_internal*)state, (BYTE*)inputBuffer);
  1296. return 0;
  1297. }
  1298. void* LZ4_create (char* inputBuffer)
  1299. {
  1300. void* lz4ds = ALLOCATOR(8, LZ4_STREAMSIZE_U64);
  1301. LZ4_init ((LZ4_stream_t_internal*)lz4ds, (BYTE*)inputBuffer);
  1302. return lz4ds;
  1303. }
  1304. char* LZ4_slideInputBuffer (void* LZ4_Data)
  1305. {
  1306. LZ4_stream_t_internal* ctx = (LZ4_stream_t_internal*)LZ4_Data;
  1307. int dictSize = LZ4_saveDict((LZ4_stream_t*)LZ4_Data, (char*)ctx->bufferStart, 64 KB);
  1308. return (char*)(ctx->bufferStart + dictSize);
  1309. }
  1310. /* Obsolete streaming decompression functions */
  1311. int LZ4_decompress_safe_withPrefix64k(const char* source, char* dest, int compressedSize, int maxOutputSize)
  1312. {
  1313. return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, endOnInputSize, full, 0, withPrefix64k, (BYTE*)dest - 64 KB, NULL, 64 KB);
  1314. }
  1315. int LZ4_decompress_fast_withPrefix64k(const char* source, char* dest, int originalSize)
  1316. {
  1317. return LZ4_decompress_generic(source, dest, 0, originalSize, endOnOutputSize, full, 0, withPrefix64k, (BYTE*)dest - 64 KB, NULL, 64 KB);
  1318. }
  1319. #endif /* LZ4_COMMONDEFS_ONLY */