lz4.c 56 KB

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