lz4frame.c 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838
  1. /*
  2. * LZ4 auto-framing library
  3. * Copyright (C) 2011-2016, Yann Collet.
  4. *
  5. * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are
  9. * met:
  10. *
  11. * - Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * - Redistributions in binary form must reproduce the above
  14. * copyright notice, this list of conditions and the following disclaimer
  15. * in the documentation and/or other materials provided with the
  16. * distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. * You can contact the author at :
  31. * - LZ4 homepage : http://www.lz4.org
  32. * - LZ4 source repository : https://github.com/lz4/lz4
  33. */
  34. /* LZ4F is a stand-alone API to create LZ4-compressed Frames
  35. * in full conformance with specification v1.6.1 .
  36. * This library rely upon memory management capabilities (malloc, free)
  37. * provided either by <stdlib.h>,
  38. * or redirected towards another library of user's choice
  39. * (see Memory Routines below).
  40. */
  41. /*-************************************
  42. * Compiler Options
  43. **************************************/
  44. #ifdef _MSC_VER /* Visual Studio */
  45. # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
  46. #endif
  47. /*-************************************
  48. * Tuning parameters
  49. **************************************/
  50. /*
  51. * LZ4F_HEAPMODE :
  52. * Select how default compression functions will allocate memory for their hash table,
  53. * in memory stack (0:default, fastest), or in memory heap (1:requires malloc()).
  54. */
  55. #ifndef LZ4F_HEAPMODE
  56. # define LZ4F_HEAPMODE 0
  57. #endif
  58. /*-************************************
  59. * Memory routines
  60. **************************************/
  61. /*
  62. * User may redirect invocations of
  63. * malloc(), calloc() and free()
  64. * towards another library or solution of their choice
  65. * by modifying below section.
  66. */
  67. #include <stdlib.h> /* malloc, calloc, free */
  68. #ifndef LZ4_SRC_INCLUDED /* avoid redefinition when sources are coalesced */
  69. # define ALLOC(s) malloc(s)
  70. # define ALLOC_AND_ZERO(s) calloc(1,(s))
  71. # define FREEMEM(p) free(p)
  72. #endif
  73. #include <string.h> /* memset, memcpy, memmove */
  74. #ifndef LZ4_SRC_INCLUDED /* avoid redefinition when sources are coalesced */
  75. # define MEM_INIT(p,v,s) memset((p),(v),(s))
  76. #endif
  77. /*-************************************
  78. * Library declarations
  79. **************************************/
  80. #define LZ4F_STATIC_LINKING_ONLY
  81. #include "lz4frame.h"
  82. #define LZ4_STATIC_LINKING_ONLY
  83. #include "lz4.h"
  84. #define LZ4_HC_STATIC_LINKING_ONLY
  85. #include "lz4hc.h"
  86. #define XXH_STATIC_LINKING_ONLY
  87. #include "xxhash.h"
  88. /*-************************************
  89. * Debug
  90. **************************************/
  91. #if defined(LZ4_DEBUG) && (LZ4_DEBUG>=1)
  92. # include <assert.h>
  93. #else
  94. # ifndef assert
  95. # define assert(condition) ((void)0)
  96. # endif
  97. #endif
  98. #define LZ4F_STATIC_ASSERT(c) { enum { LZ4F_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */
  99. #if defined(LZ4_DEBUG) && (LZ4_DEBUG>=2) && !defined(DEBUGLOG)
  100. # include <stdio.h>
  101. static int g_debuglog_enable = 1;
  102. # define DEBUGLOG(l, ...) { \
  103. if ((g_debuglog_enable) && (l<=LZ4_DEBUG)) { \
  104. fprintf(stderr, __FILE__ ": "); \
  105. fprintf(stderr, __VA_ARGS__); \
  106. fprintf(stderr, " \n"); \
  107. } }
  108. #else
  109. # define DEBUGLOG(l, ...) {} /* disabled */
  110. #endif
  111. /*-************************************
  112. * Basic Types
  113. **************************************/
  114. #if !defined (__VMS) && (defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
  115. # include <stdint.h>
  116. typedef uint8_t BYTE;
  117. typedef uint16_t U16;
  118. typedef uint32_t U32;
  119. typedef int32_t S32;
  120. typedef uint64_t U64;
  121. #else
  122. typedef unsigned char BYTE;
  123. typedef unsigned short U16;
  124. typedef unsigned int U32;
  125. typedef signed int S32;
  126. typedef unsigned long long U64;
  127. #endif
  128. /* unoptimized version; solves endianess & alignment issues */
  129. static U32 LZ4F_readLE32 (const void* src)
  130. {
  131. const BYTE* const srcPtr = (const BYTE*)src;
  132. U32 value32 = srcPtr[0];
  133. value32 += ((U32)srcPtr[1])<< 8;
  134. value32 += ((U32)srcPtr[2])<<16;
  135. value32 += ((U32)srcPtr[3])<<24;
  136. return value32;
  137. }
  138. static void LZ4F_writeLE32 (void* dst, U32 value32)
  139. {
  140. BYTE* const dstPtr = (BYTE*)dst;
  141. dstPtr[0] = (BYTE)value32;
  142. dstPtr[1] = (BYTE)(value32 >> 8);
  143. dstPtr[2] = (BYTE)(value32 >> 16);
  144. dstPtr[3] = (BYTE)(value32 >> 24);
  145. }
  146. static U64 LZ4F_readLE64 (const void* src)
  147. {
  148. const BYTE* const srcPtr = (const BYTE*)src;
  149. U64 value64 = srcPtr[0];
  150. value64 += ((U64)srcPtr[1]<<8);
  151. value64 += ((U64)srcPtr[2]<<16);
  152. value64 += ((U64)srcPtr[3]<<24);
  153. value64 += ((U64)srcPtr[4]<<32);
  154. value64 += ((U64)srcPtr[5]<<40);
  155. value64 += ((U64)srcPtr[6]<<48);
  156. value64 += ((U64)srcPtr[7]<<56);
  157. return value64;
  158. }
  159. static void LZ4F_writeLE64 (void* dst, U64 value64)
  160. {
  161. BYTE* const dstPtr = (BYTE*)dst;
  162. dstPtr[0] = (BYTE)value64;
  163. dstPtr[1] = (BYTE)(value64 >> 8);
  164. dstPtr[2] = (BYTE)(value64 >> 16);
  165. dstPtr[3] = (BYTE)(value64 >> 24);
  166. dstPtr[4] = (BYTE)(value64 >> 32);
  167. dstPtr[5] = (BYTE)(value64 >> 40);
  168. dstPtr[6] = (BYTE)(value64 >> 48);
  169. dstPtr[7] = (BYTE)(value64 >> 56);
  170. }
  171. /*-************************************
  172. * Constants
  173. **************************************/
  174. #ifndef LZ4_SRC_INCLUDED /* avoid double definition */
  175. # define KB *(1<<10)
  176. # define MB *(1<<20)
  177. # define GB *(1<<30)
  178. #endif
  179. #define _1BIT 0x01
  180. #define _2BITS 0x03
  181. #define _3BITS 0x07
  182. #define _4BITS 0x0F
  183. #define _8BITS 0xFF
  184. #define LZ4F_MAGIC_SKIPPABLE_START 0x184D2A50U
  185. #define LZ4F_MAGICNUMBER 0x184D2204U
  186. #define LZ4F_BLOCKUNCOMPRESSED_FLAG 0x80000000U
  187. #define LZ4F_BLOCKSIZEID_DEFAULT LZ4F_max64KB
  188. static const size_t minFHSize = LZ4F_HEADER_SIZE_MIN; /* 7 */
  189. static const size_t maxFHSize = LZ4F_HEADER_SIZE_MAX; /* 19 */
  190. static const size_t BHSize = 4; /* block header : size, and compress flag */
  191. static const size_t BFSize = 4; /* block footer : checksum (optional) */
  192. /*-************************************
  193. * Structures and local types
  194. **************************************/
  195. typedef struct LZ4F_cctx_s
  196. {
  197. LZ4F_preferences_t prefs;
  198. U32 version;
  199. U32 cStage;
  200. const LZ4F_CDict* cdict;
  201. size_t maxBlockSize;
  202. size_t maxBufferSize;
  203. BYTE* tmpBuff;
  204. BYTE* tmpIn;
  205. size_t tmpInSize;
  206. U64 totalInSize;
  207. XXH32_state_t xxh;
  208. void* lz4CtxPtr;
  209. U16 lz4CtxAlloc; /* sized for: 0 = none, 1 = lz4 ctx, 2 = lz4hc ctx */
  210. U16 lz4CtxState; /* in use as: 0 = none, 1 = lz4 ctx, 2 = lz4hc ctx */
  211. } LZ4F_cctx_t;
  212. /*-************************************
  213. * Error management
  214. **************************************/
  215. #define LZ4F_GENERATE_STRING(STRING) #STRING,
  216. static const char* LZ4F_errorStrings[] = { LZ4F_LIST_ERRORS(LZ4F_GENERATE_STRING) };
  217. unsigned LZ4F_isError(LZ4F_errorCode_t code)
  218. {
  219. return (code > (LZ4F_errorCode_t)(-LZ4F_ERROR_maxCode));
  220. }
  221. const char* LZ4F_getErrorName(LZ4F_errorCode_t code)
  222. {
  223. static const char* codeError = "Unspecified error code";
  224. if (LZ4F_isError(code)) return LZ4F_errorStrings[-(int)(code)];
  225. return codeError;
  226. }
  227. LZ4F_errorCodes LZ4F_getErrorCode(size_t functionResult)
  228. {
  229. if (!LZ4F_isError(functionResult)) return LZ4F_OK_NoError;
  230. return (LZ4F_errorCodes)(-(ptrdiff_t)functionResult);
  231. }
  232. static LZ4F_errorCode_t err0r(LZ4F_errorCodes code)
  233. {
  234. /* A compilation error here means sizeof(ptrdiff_t) is not large enough */
  235. LZ4F_STATIC_ASSERT(sizeof(ptrdiff_t) >= sizeof(size_t));
  236. return (LZ4F_errorCode_t)-(ptrdiff_t)code;
  237. }
  238. unsigned LZ4F_getVersion(void) { return LZ4F_VERSION; }
  239. int LZ4F_compressionLevel_max(void) { return LZ4HC_CLEVEL_MAX; }
  240. size_t LZ4F_getBlockSize(unsigned blockSizeID)
  241. {
  242. static const size_t blockSizes[4] = { 64 KB, 256 KB, 1 MB, 4 MB };
  243. if (blockSizeID == 0) blockSizeID = LZ4F_BLOCKSIZEID_DEFAULT;
  244. if (blockSizeID < LZ4F_max64KB || blockSizeID > LZ4F_max4MB)
  245. return err0r(LZ4F_ERROR_maxBlockSize_invalid);
  246. blockSizeID -= LZ4F_max64KB;
  247. return blockSizes[blockSizeID];
  248. }
  249. /*-************************************
  250. * Private functions
  251. **************************************/
  252. #define MIN(a,b) ( (a) < (b) ? (a) : (b) )
  253. static BYTE LZ4F_headerChecksum (const void* header, size_t length)
  254. {
  255. U32 const xxh = XXH32(header, length, 0);
  256. return (BYTE)(xxh >> 8);
  257. }
  258. /*-************************************
  259. * Simple-pass compression functions
  260. **************************************/
  261. static LZ4F_blockSizeID_t LZ4F_optimalBSID(const LZ4F_blockSizeID_t requestedBSID,
  262. const size_t srcSize)
  263. {
  264. LZ4F_blockSizeID_t proposedBSID = LZ4F_max64KB;
  265. size_t maxBlockSize = 64 KB;
  266. while (requestedBSID > proposedBSID) {
  267. if (srcSize <= maxBlockSize)
  268. return proposedBSID;
  269. proposedBSID = (LZ4F_blockSizeID_t)((int)proposedBSID + 1);
  270. maxBlockSize <<= 2;
  271. }
  272. return requestedBSID;
  273. }
  274. /*! LZ4F_compressBound_internal() :
  275. * Provides dstCapacity given a srcSize to guarantee operation success in worst case situations.
  276. * prefsPtr is optional : if NULL is provided, preferences will be set to cover worst case scenario.
  277. * @return is always the same for a srcSize and prefsPtr, so it can be relied upon to size reusable buffers.
  278. * When srcSize==0, LZ4F_compressBound() provides an upper bound for LZ4F_flush() and LZ4F_compressEnd() operations.
  279. */
  280. static size_t LZ4F_compressBound_internal(size_t srcSize,
  281. const LZ4F_preferences_t* preferencesPtr,
  282. size_t alreadyBuffered)
  283. {
  284. LZ4F_preferences_t prefsNull = LZ4F_INIT_PREFERENCES;
  285. prefsNull.frameInfo.contentChecksumFlag = LZ4F_contentChecksumEnabled; /* worst case */
  286. { const LZ4F_preferences_t* const prefsPtr = (preferencesPtr==NULL) ? &prefsNull : preferencesPtr;
  287. U32 const flush = prefsPtr->autoFlush | (srcSize==0);
  288. LZ4F_blockSizeID_t const blockID = prefsPtr->frameInfo.blockSizeID;
  289. size_t const blockSize = LZ4F_getBlockSize(blockID);
  290. size_t const maxBuffered = blockSize - 1;
  291. size_t const bufferedSize = MIN(alreadyBuffered, maxBuffered);
  292. size_t const maxSrcSize = srcSize + bufferedSize;
  293. unsigned const nbFullBlocks = (unsigned)(maxSrcSize / blockSize);
  294. size_t const partialBlockSize = maxSrcSize & (blockSize-1);
  295. size_t const lastBlockSize = flush ? partialBlockSize : 0;
  296. unsigned const nbBlocks = nbFullBlocks + (lastBlockSize>0);
  297. size_t const blockCRCSize = BFSize * prefsPtr->frameInfo.blockChecksumFlag;
  298. size_t const frameEnd = BHSize + (prefsPtr->frameInfo.contentChecksumFlag*BFSize);
  299. return ((BHSize + blockCRCSize) * nbBlocks) +
  300. (blockSize * nbFullBlocks) + lastBlockSize + frameEnd;
  301. }
  302. }
  303. size_t LZ4F_compressFrameBound(size_t srcSize, const LZ4F_preferences_t* preferencesPtr)
  304. {
  305. LZ4F_preferences_t prefs;
  306. size_t const headerSize = maxFHSize; /* max header size, including optional fields */
  307. if (preferencesPtr!=NULL) prefs = *preferencesPtr;
  308. else MEM_INIT(&prefs, 0, sizeof(prefs));
  309. prefs.autoFlush = 1;
  310. return headerSize + LZ4F_compressBound_internal(srcSize, &prefs, 0);;
  311. }
  312. /*! LZ4F_compressFrame_usingCDict() :
  313. * Compress srcBuffer using a dictionary, in a single step.
  314. * cdict can be NULL, in which case, no dictionary is used.
  315. * dstBuffer MUST be >= LZ4F_compressFrameBound(srcSize, preferencesPtr).
  316. * The LZ4F_preferences_t structure is optional : you may provide NULL as argument,
  317. * however, it's the only way to provide a dictID, so it's not recommended.
  318. * @return : number of bytes written into dstBuffer,
  319. * or an error code if it fails (can be tested using LZ4F_isError())
  320. */
  321. size_t LZ4F_compressFrame_usingCDict(LZ4F_cctx* cctx,
  322. void* dstBuffer, size_t dstCapacity,
  323. const void* srcBuffer, size_t srcSize,
  324. const LZ4F_CDict* cdict,
  325. const LZ4F_preferences_t* preferencesPtr)
  326. {
  327. LZ4F_preferences_t prefs;
  328. LZ4F_compressOptions_t options;
  329. BYTE* const dstStart = (BYTE*) dstBuffer;
  330. BYTE* dstPtr = dstStart;
  331. BYTE* const dstEnd = dstStart + dstCapacity;
  332. if (preferencesPtr!=NULL)
  333. prefs = *preferencesPtr;
  334. else
  335. MEM_INIT(&prefs, 0, sizeof(prefs));
  336. if (prefs.frameInfo.contentSize != 0)
  337. prefs.frameInfo.contentSize = (U64)srcSize; /* auto-correct content size if selected (!=0) */
  338. prefs.frameInfo.blockSizeID = LZ4F_optimalBSID(prefs.frameInfo.blockSizeID, srcSize);
  339. prefs.autoFlush = 1;
  340. if (srcSize <= LZ4F_getBlockSize(prefs.frameInfo.blockSizeID))
  341. prefs.frameInfo.blockMode = LZ4F_blockIndependent; /* only one block => no need for inter-block link */
  342. MEM_INIT(&options, 0, sizeof(options));
  343. options.stableSrc = 1;
  344. if (dstCapacity < LZ4F_compressFrameBound(srcSize, &prefs)) /* condition to guarantee success */
  345. return err0r(LZ4F_ERROR_dstMaxSize_tooSmall);
  346. { size_t const headerSize = LZ4F_compressBegin_usingCDict(cctx, dstBuffer, dstCapacity, cdict, &prefs); /* write header */
  347. if (LZ4F_isError(headerSize)) return headerSize;
  348. dstPtr += headerSize; /* header size */ }
  349. assert(dstEnd >= dstPtr);
  350. { size_t const cSize = LZ4F_compressUpdate(cctx, dstPtr, (size_t)(dstEnd-dstPtr), srcBuffer, srcSize, &options);
  351. if (LZ4F_isError(cSize)) return cSize;
  352. dstPtr += cSize; }
  353. assert(dstEnd >= dstPtr);
  354. { size_t const tailSize = LZ4F_compressEnd(cctx, dstPtr, (size_t)(dstEnd-dstPtr), &options); /* flush last block, and generate suffix */
  355. if (LZ4F_isError(tailSize)) return tailSize;
  356. dstPtr += tailSize; }
  357. assert(dstEnd >= dstStart);
  358. return (size_t)(dstPtr - dstStart);
  359. }
  360. /*! LZ4F_compressFrame() :
  361. * Compress an entire srcBuffer into a valid LZ4 frame, in a single step.
  362. * dstBuffer MUST be >= LZ4F_compressFrameBound(srcSize, preferencesPtr).
  363. * The LZ4F_preferences_t structure is optional : you can provide NULL as argument. All preferences will be set to default.
  364. * @return : number of bytes written into dstBuffer.
  365. * or an error code if it fails (can be tested using LZ4F_isError())
  366. */
  367. size_t LZ4F_compressFrame(void* dstBuffer, size_t dstCapacity,
  368. const void* srcBuffer, size_t srcSize,
  369. const LZ4F_preferences_t* preferencesPtr)
  370. {
  371. size_t result;
  372. #if (LZ4F_HEAPMODE)
  373. LZ4F_cctx_t *cctxPtr;
  374. result = LZ4F_createCompressionContext(&cctxPtr, LZ4F_VERSION);
  375. if (LZ4F_isError(result)) return result;
  376. #else
  377. LZ4F_cctx_t cctx;
  378. LZ4_stream_t lz4ctx;
  379. LZ4F_cctx_t *cctxPtr = &cctx;
  380. DEBUGLOG(4, "LZ4F_compressFrame");
  381. MEM_INIT(&cctx, 0, sizeof(cctx));
  382. cctx.version = LZ4F_VERSION;
  383. cctx.maxBufferSize = 5 MB; /* mess with real buffer size to prevent dynamic allocation; works only because autoflush==1 & stableSrc==1 */
  384. if (preferencesPtr == NULL ||
  385. preferencesPtr->compressionLevel < LZ4HC_CLEVEL_MIN)
  386. {
  387. LZ4_initStream(&lz4ctx, sizeof(lz4ctx));
  388. cctxPtr->lz4CtxPtr = &lz4ctx;
  389. cctxPtr->lz4CtxAlloc = 1;
  390. cctxPtr->lz4CtxState = 1;
  391. }
  392. #endif
  393. result = LZ4F_compressFrame_usingCDict(cctxPtr, dstBuffer, dstCapacity,
  394. srcBuffer, srcSize,
  395. NULL, preferencesPtr);
  396. #if (LZ4F_HEAPMODE)
  397. LZ4F_freeCompressionContext(cctxPtr);
  398. #else
  399. if (preferencesPtr != NULL &&
  400. preferencesPtr->compressionLevel >= LZ4HC_CLEVEL_MIN)
  401. {
  402. FREEMEM(cctxPtr->lz4CtxPtr);
  403. }
  404. #endif
  405. return result;
  406. }
  407. /*-***************************************************
  408. * Dictionary compression
  409. *****************************************************/
  410. struct LZ4F_CDict_s {
  411. void* dictContent;
  412. LZ4_stream_t* fastCtx;
  413. LZ4_streamHC_t* HCCtx;
  414. }; /* typedef'd to LZ4F_CDict within lz4frame_static.h */
  415. /*! LZ4F_createCDict() :
  416. * When compressing multiple messages / blocks with the same dictionary, it's recommended to load it just once.
  417. * LZ4F_createCDict() will create a digested dictionary, ready to start future compression operations without startup delay.
  418. * LZ4F_CDict can be created once and shared by multiple threads concurrently, since its usage is read-only.
  419. * `dictBuffer` can be released after LZ4F_CDict creation, since its content is copied within CDict
  420. * @return : digested dictionary for compression, or NULL if failed */
  421. LZ4F_CDict* LZ4F_createCDict(const void* dictBuffer, size_t dictSize)
  422. {
  423. const char* dictStart = (const char*)dictBuffer;
  424. LZ4F_CDict* cdict = (LZ4F_CDict*) ALLOC(sizeof(*cdict));
  425. DEBUGLOG(4, "LZ4F_createCDict");
  426. if (!cdict) return NULL;
  427. if (dictSize > 64 KB) {
  428. dictStart += dictSize - 64 KB;
  429. dictSize = 64 KB;
  430. }
  431. cdict->dictContent = ALLOC(dictSize);
  432. cdict->fastCtx = LZ4_createStream();
  433. cdict->HCCtx = LZ4_createStreamHC();
  434. if (!cdict->dictContent || !cdict->fastCtx || !cdict->HCCtx) {
  435. LZ4F_freeCDict(cdict);
  436. return NULL;
  437. }
  438. memcpy(cdict->dictContent, dictStart, dictSize);
  439. LZ4_loadDict (cdict->fastCtx, (const char*)cdict->dictContent, (int)dictSize);
  440. LZ4_setCompressionLevel(cdict->HCCtx, LZ4HC_CLEVEL_DEFAULT);
  441. LZ4_loadDictHC(cdict->HCCtx, (const char*)cdict->dictContent, (int)dictSize);
  442. return cdict;
  443. }
  444. void LZ4F_freeCDict(LZ4F_CDict* cdict)
  445. {
  446. if (cdict==NULL) return; /* support free on NULL */
  447. FREEMEM(cdict->dictContent);
  448. LZ4_freeStream(cdict->fastCtx);
  449. LZ4_freeStreamHC(cdict->HCCtx);
  450. FREEMEM(cdict);
  451. }
  452. /*-*********************************
  453. * Advanced compression functions
  454. ***********************************/
  455. /*! LZ4F_createCompressionContext() :
  456. * The first thing to do is to create a compressionContext object, which will be used in all compression operations.
  457. * This is achieved using LZ4F_createCompressionContext(), which takes as argument a version and an LZ4F_preferences_t structure.
  458. * The version provided MUST be LZ4F_VERSION. It is intended to track potential incompatible differences between different binaries.
  459. * The function will provide a pointer to an allocated LZ4F_compressionContext_t object.
  460. * If the result LZ4F_errorCode_t is not OK_NoError, there was an error during context creation.
  461. * Object can release its memory using LZ4F_freeCompressionContext();
  462. */
  463. LZ4F_errorCode_t LZ4F_createCompressionContext(LZ4F_compressionContext_t* LZ4F_compressionContextPtr, unsigned version)
  464. {
  465. LZ4F_cctx_t* const cctxPtr = (LZ4F_cctx_t*)ALLOC_AND_ZERO(sizeof(LZ4F_cctx_t));
  466. if (cctxPtr==NULL) return err0r(LZ4F_ERROR_allocation_failed);
  467. cctxPtr->version = version;
  468. cctxPtr->cStage = 0; /* Next stage : init stream */
  469. *LZ4F_compressionContextPtr = (LZ4F_compressionContext_t)cctxPtr;
  470. return LZ4F_OK_NoError;
  471. }
  472. LZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_compressionContext_t LZ4F_compressionContext)
  473. {
  474. LZ4F_cctx_t* const cctxPtr = (LZ4F_cctx_t*)LZ4F_compressionContext;
  475. if (cctxPtr != NULL) { /* support free on NULL */
  476. FREEMEM(cctxPtr->lz4CtxPtr); /* works because LZ4_streamHC_t and LZ4_stream_t are simple POD types */
  477. FREEMEM(cctxPtr->tmpBuff);
  478. FREEMEM(LZ4F_compressionContext);
  479. }
  480. return LZ4F_OK_NoError;
  481. }
  482. /**
  483. * This function prepares the internal LZ4(HC) stream for a new compression,
  484. * resetting the context and attaching the dictionary, if there is one.
  485. *
  486. * It needs to be called at the beginning of each independent compression
  487. * stream (i.e., at the beginning of a frame in blockLinked mode, or at the
  488. * beginning of each block in blockIndependent mode).
  489. */
  490. static void LZ4F_initStream(void* ctx,
  491. const LZ4F_CDict* cdict,
  492. int level,
  493. LZ4F_blockMode_t blockMode) {
  494. if (level < LZ4HC_CLEVEL_MIN) {
  495. if (cdict != NULL || blockMode == LZ4F_blockLinked) {
  496. /* In these cases, we will call LZ4_compress_fast_continue(),
  497. * which needs an already reset context. Otherwise, we'll call a
  498. * one-shot API. The non-continued APIs internally perform their own
  499. * resets at the beginning of their calls, where they know what
  500. * tableType they need the context to be in. So in that case this
  501. * would be misguided / wasted work. */
  502. LZ4_resetStream_fast((LZ4_stream_t*)ctx);
  503. }
  504. LZ4_attach_dictionary((LZ4_stream_t *)ctx, cdict ? cdict->fastCtx : NULL);
  505. } else {
  506. LZ4_resetStreamHC_fast((LZ4_streamHC_t*)ctx, level);
  507. LZ4_attach_HC_dictionary((LZ4_streamHC_t *)ctx, cdict ? cdict->HCCtx : NULL);
  508. }
  509. }
  510. /*! LZ4F_compressBegin_usingCDict() :
  511. * init streaming compression and writes frame header into dstBuffer.
  512. * dstBuffer must be >= LZ4F_HEADER_SIZE_MAX bytes.
  513. * @return : number of bytes written into dstBuffer for the header
  514. * or an error code (can be tested using LZ4F_isError())
  515. */
  516. size_t LZ4F_compressBegin_usingCDict(LZ4F_cctx* cctxPtr,
  517. void* dstBuffer, size_t dstCapacity,
  518. const LZ4F_CDict* cdict,
  519. const LZ4F_preferences_t* preferencesPtr)
  520. {
  521. LZ4F_preferences_t prefNull;
  522. BYTE* const dstStart = (BYTE*)dstBuffer;
  523. BYTE* dstPtr = dstStart;
  524. BYTE* headerStart;
  525. if (dstCapacity < maxFHSize) return err0r(LZ4F_ERROR_dstMaxSize_tooSmall);
  526. MEM_INIT(&prefNull, 0, sizeof(prefNull));
  527. if (preferencesPtr == NULL) preferencesPtr = &prefNull;
  528. cctxPtr->prefs = *preferencesPtr;
  529. /* Ctx Management */
  530. { U16 const ctxTypeID = (cctxPtr->prefs.compressionLevel < LZ4HC_CLEVEL_MIN) ? 1 : 2;
  531. if (cctxPtr->lz4CtxAlloc < ctxTypeID) {
  532. FREEMEM(cctxPtr->lz4CtxPtr);
  533. if (cctxPtr->prefs.compressionLevel < LZ4HC_CLEVEL_MIN) {
  534. cctxPtr->lz4CtxPtr = LZ4_createStream();
  535. } else {
  536. cctxPtr->lz4CtxPtr = LZ4_createStreamHC();
  537. }
  538. if (cctxPtr->lz4CtxPtr == NULL)
  539. return err0r(LZ4F_ERROR_allocation_failed);
  540. cctxPtr->lz4CtxAlloc = ctxTypeID;
  541. cctxPtr->lz4CtxState = ctxTypeID;
  542. } else if (cctxPtr->lz4CtxState != ctxTypeID) {
  543. /* otherwise, a sufficient buffer is allocated, but we need to
  544. * reset it to the correct context type */
  545. if (cctxPtr->prefs.compressionLevel < LZ4HC_CLEVEL_MIN) {
  546. LZ4_initStream((LZ4_stream_t *) cctxPtr->lz4CtxPtr, sizeof (LZ4_stream_t));
  547. } else {
  548. LZ4_initStreamHC((LZ4_streamHC_t *) cctxPtr->lz4CtxPtr, sizeof(LZ4_streamHC_t));
  549. LZ4_setCompressionLevel((LZ4_streamHC_t *) cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel);
  550. }
  551. cctxPtr->lz4CtxState = ctxTypeID;
  552. }
  553. }
  554. /* Buffer Management */
  555. if (cctxPtr->prefs.frameInfo.blockSizeID == 0)
  556. cctxPtr->prefs.frameInfo.blockSizeID = LZ4F_BLOCKSIZEID_DEFAULT;
  557. cctxPtr->maxBlockSize = LZ4F_getBlockSize(cctxPtr->prefs.frameInfo.blockSizeID);
  558. { size_t const requiredBuffSize = preferencesPtr->autoFlush ?
  559. ((cctxPtr->prefs.frameInfo.blockMode == LZ4F_blockLinked) ? 64 KB : 0) : /* only needs past data up to window size */
  560. cctxPtr->maxBlockSize + ((cctxPtr->prefs.frameInfo.blockMode == LZ4F_blockLinked) ? 128 KB : 0);
  561. if (cctxPtr->maxBufferSize < requiredBuffSize) {
  562. cctxPtr->maxBufferSize = 0;
  563. FREEMEM(cctxPtr->tmpBuff);
  564. cctxPtr->tmpBuff = (BYTE*)ALLOC_AND_ZERO(requiredBuffSize);
  565. if (cctxPtr->tmpBuff == NULL) return err0r(LZ4F_ERROR_allocation_failed);
  566. cctxPtr->maxBufferSize = requiredBuffSize;
  567. } }
  568. cctxPtr->tmpIn = cctxPtr->tmpBuff;
  569. cctxPtr->tmpInSize = 0;
  570. (void)XXH32_reset(&(cctxPtr->xxh), 0);
  571. /* context init */
  572. cctxPtr->cdict = cdict;
  573. if (cctxPtr->prefs.frameInfo.blockMode == LZ4F_blockLinked) {
  574. /* frame init only for blockLinked : blockIndependent will be init at each block */
  575. LZ4F_initStream(cctxPtr->lz4CtxPtr, cdict, cctxPtr->prefs.compressionLevel, LZ4F_blockLinked);
  576. }
  577. if (preferencesPtr->compressionLevel >= LZ4HC_CLEVEL_MIN) {
  578. LZ4_favorDecompressionSpeed((LZ4_streamHC_t*)cctxPtr->lz4CtxPtr, (int)preferencesPtr->favorDecSpeed);
  579. }
  580. /* Magic Number */
  581. LZ4F_writeLE32(dstPtr, LZ4F_MAGICNUMBER);
  582. dstPtr += 4;
  583. headerStart = dstPtr;
  584. /* FLG Byte */
  585. *dstPtr++ = (BYTE)(((1 & _2BITS) << 6) /* Version('01') */
  586. + ((cctxPtr->prefs.frameInfo.blockMode & _1BIT ) << 5)
  587. + ((cctxPtr->prefs.frameInfo.blockChecksumFlag & _1BIT ) << 4)
  588. + ((unsigned)(cctxPtr->prefs.frameInfo.contentSize > 0) << 3)
  589. + ((cctxPtr->prefs.frameInfo.contentChecksumFlag & _1BIT ) << 2)
  590. + (cctxPtr->prefs.frameInfo.dictID > 0) );
  591. /* BD Byte */
  592. *dstPtr++ = (BYTE)((cctxPtr->prefs.frameInfo.blockSizeID & _3BITS) << 4);
  593. /* Optional Frame content size field */
  594. if (cctxPtr->prefs.frameInfo.contentSize) {
  595. LZ4F_writeLE64(dstPtr, cctxPtr->prefs.frameInfo.contentSize);
  596. dstPtr += 8;
  597. cctxPtr->totalInSize = 0;
  598. }
  599. /* Optional dictionary ID field */
  600. if (cctxPtr->prefs.frameInfo.dictID) {
  601. LZ4F_writeLE32(dstPtr, cctxPtr->prefs.frameInfo.dictID);
  602. dstPtr += 4;
  603. }
  604. /* Header CRC Byte */
  605. *dstPtr = LZ4F_headerChecksum(headerStart, (size_t)(dstPtr - headerStart));
  606. dstPtr++;
  607. cctxPtr->cStage = 1; /* header written, now request input data block */
  608. return (size_t)(dstPtr - dstStart);
  609. }
  610. /*! LZ4F_compressBegin() :
  611. * init streaming compression and writes frame header into dstBuffer.
  612. * dstBuffer must be >= LZ4F_HEADER_SIZE_MAX bytes.
  613. * preferencesPtr can be NULL, in which case default parameters are selected.
  614. * @return : number of bytes written into dstBuffer for the header
  615. * or an error code (can be tested using LZ4F_isError())
  616. */
  617. size_t LZ4F_compressBegin(LZ4F_cctx* cctxPtr,
  618. void* dstBuffer, size_t dstCapacity,
  619. const LZ4F_preferences_t* preferencesPtr)
  620. {
  621. return LZ4F_compressBegin_usingCDict(cctxPtr, dstBuffer, dstCapacity,
  622. NULL, preferencesPtr);
  623. }
  624. /* LZ4F_compressBound() :
  625. * @return minimum capacity of dstBuffer for a given srcSize to handle worst case scenario.
  626. * LZ4F_preferences_t structure is optional : if NULL, preferences will be set to cover worst case scenario.
  627. * This function cannot fail.
  628. */
  629. size_t LZ4F_compressBound(size_t srcSize, const LZ4F_preferences_t* preferencesPtr)
  630. {
  631. return LZ4F_compressBound_internal(srcSize, preferencesPtr, (size_t)-1);
  632. }
  633. typedef int (*compressFunc_t)(void* ctx, const char* src, char* dst, int srcSize, int dstSize, int level, const LZ4F_CDict* cdict);
  634. /*! LZ4F_makeBlock():
  635. * compress a single block, add header and optional checksum.
  636. * assumption : dst buffer capacity is >= BHSize + srcSize + crcSize
  637. */
  638. static size_t LZ4F_makeBlock(void* dst,
  639. const void* src, size_t srcSize,
  640. compressFunc_t compress, void* lz4ctx, int level,
  641. const LZ4F_CDict* cdict,
  642. LZ4F_blockChecksum_t crcFlag)
  643. {
  644. BYTE* const cSizePtr = (BYTE*)dst;
  645. U32 cSize = (U32)compress(lz4ctx, (const char*)src, (char*)(cSizePtr+BHSize),
  646. (int)(srcSize), (int)(srcSize-1),
  647. level, cdict);
  648. if (cSize == 0) { /* compression failed */
  649. cSize = (U32)srcSize;
  650. LZ4F_writeLE32(cSizePtr, cSize | LZ4F_BLOCKUNCOMPRESSED_FLAG);
  651. memcpy(cSizePtr+BHSize, src, srcSize);
  652. } else {
  653. LZ4F_writeLE32(cSizePtr, cSize);
  654. }
  655. if (crcFlag) {
  656. U32 const crc32 = XXH32(cSizePtr+BHSize, cSize, 0); /* checksum of compressed data */
  657. LZ4F_writeLE32(cSizePtr+BHSize+cSize, crc32);
  658. }
  659. return BHSize + cSize + ((U32)crcFlag)*BFSize;
  660. }
  661. static int LZ4F_compressBlock(void* ctx, const char* src, char* dst, int srcSize, int dstCapacity, int level, const LZ4F_CDict* cdict)
  662. {
  663. int const acceleration = (level < 0) ? -level + 1 : 1;
  664. LZ4F_initStream(ctx, cdict, level, LZ4F_blockIndependent);
  665. if (cdict) {
  666. return LZ4_compress_fast_continue((LZ4_stream_t*)ctx, src, dst, srcSize, dstCapacity, acceleration);
  667. } else {
  668. return LZ4_compress_fast_extState_fastReset(ctx, src, dst, srcSize, dstCapacity, acceleration);
  669. }
  670. }
  671. static int LZ4F_compressBlock_continue(void* ctx, const char* src, char* dst, int srcSize, int dstCapacity, int level, const LZ4F_CDict* cdict)
  672. {
  673. int const acceleration = (level < 0) ? -level + 1 : 1;
  674. (void)cdict; /* init once at beginning of frame */
  675. return LZ4_compress_fast_continue((LZ4_stream_t*)ctx, src, dst, srcSize, dstCapacity, acceleration);
  676. }
  677. static int LZ4F_compressBlockHC(void* ctx, const char* src, char* dst, int srcSize, int dstCapacity, int level, const LZ4F_CDict* cdict)
  678. {
  679. LZ4F_initStream(ctx, cdict, level, LZ4F_blockIndependent);
  680. if (cdict) {
  681. return LZ4_compress_HC_continue((LZ4_streamHC_t*)ctx, src, dst, srcSize, dstCapacity);
  682. }
  683. return LZ4_compress_HC_extStateHC_fastReset(ctx, src, dst, srcSize, dstCapacity, level);
  684. }
  685. static int LZ4F_compressBlockHC_continue(void* ctx, const char* src, char* dst, int srcSize, int dstCapacity, int level, const LZ4F_CDict* cdict)
  686. {
  687. (void)level; (void)cdict; /* init once at beginning of frame */
  688. return LZ4_compress_HC_continue((LZ4_streamHC_t*)ctx, src, dst, srcSize, dstCapacity);
  689. }
  690. static compressFunc_t LZ4F_selectCompression(LZ4F_blockMode_t blockMode, int level)
  691. {
  692. if (level < LZ4HC_CLEVEL_MIN) {
  693. if (blockMode == LZ4F_blockIndependent) return LZ4F_compressBlock;
  694. return LZ4F_compressBlock_continue;
  695. }
  696. if (blockMode == LZ4F_blockIndependent) return LZ4F_compressBlockHC;
  697. return LZ4F_compressBlockHC_continue;
  698. }
  699. static int LZ4F_localSaveDict(LZ4F_cctx_t* cctxPtr)
  700. {
  701. if (cctxPtr->prefs.compressionLevel < LZ4HC_CLEVEL_MIN)
  702. return LZ4_saveDict ((LZ4_stream_t*)(cctxPtr->lz4CtxPtr), (char*)(cctxPtr->tmpBuff), 64 KB);
  703. return LZ4_saveDictHC ((LZ4_streamHC_t*)(cctxPtr->lz4CtxPtr), (char*)(cctxPtr->tmpBuff), 64 KB);
  704. }
  705. typedef enum { notDone, fromTmpBuffer, fromSrcBuffer } LZ4F_lastBlockStatus;
  706. /*! LZ4F_compressUpdate() :
  707. * LZ4F_compressUpdate() can be called repetitively to compress as much data as necessary.
  708. * dstBuffer MUST be >= LZ4F_compressBound(srcSize, preferencesPtr).
  709. * LZ4F_compressOptions_t structure is optional : you can provide NULL as argument.
  710. * @return : the number of bytes written into dstBuffer. It can be zero, meaning input data was just buffered.
  711. * or an error code if it fails (which can be tested using LZ4F_isError())
  712. */
  713. size_t LZ4F_compressUpdate(LZ4F_cctx* cctxPtr,
  714. void* dstBuffer, size_t dstCapacity,
  715. const void* srcBuffer, size_t srcSize,
  716. const LZ4F_compressOptions_t* compressOptionsPtr)
  717. {
  718. LZ4F_compressOptions_t cOptionsNull;
  719. size_t const blockSize = cctxPtr->maxBlockSize;
  720. const BYTE* srcPtr = (const BYTE*)srcBuffer;
  721. const BYTE* const srcEnd = srcPtr + srcSize;
  722. BYTE* const dstStart = (BYTE*)dstBuffer;
  723. BYTE* dstPtr = dstStart;
  724. LZ4F_lastBlockStatus lastBlockCompressed = notDone;
  725. compressFunc_t const compress = LZ4F_selectCompression(cctxPtr->prefs.frameInfo.blockMode, cctxPtr->prefs.compressionLevel);
  726. DEBUGLOG(4, "LZ4F_compressUpdate (srcSize=%zu)", srcSize);
  727. if (cctxPtr->cStage != 1) return err0r(LZ4F_ERROR_GENERIC);
  728. if (dstCapacity < LZ4F_compressBound_internal(srcSize, &(cctxPtr->prefs), cctxPtr->tmpInSize))
  729. return err0r(LZ4F_ERROR_dstMaxSize_tooSmall);
  730. MEM_INIT(&cOptionsNull, 0, sizeof(cOptionsNull));
  731. if (compressOptionsPtr == NULL) compressOptionsPtr = &cOptionsNull;
  732. /* complete tmp buffer */
  733. if (cctxPtr->tmpInSize > 0) { /* some data already within tmp buffer */
  734. size_t const sizeToCopy = blockSize - cctxPtr->tmpInSize;
  735. if (sizeToCopy > srcSize) {
  736. /* add src to tmpIn buffer */
  737. memcpy(cctxPtr->tmpIn + cctxPtr->tmpInSize, srcBuffer, srcSize);
  738. srcPtr = srcEnd;
  739. cctxPtr->tmpInSize += srcSize;
  740. /* still needs some CRC */
  741. } else {
  742. /* complete tmpIn block and then compress it */
  743. lastBlockCompressed = fromTmpBuffer;
  744. memcpy(cctxPtr->tmpIn + cctxPtr->tmpInSize, srcBuffer, sizeToCopy);
  745. srcPtr += sizeToCopy;
  746. dstPtr += LZ4F_makeBlock(dstPtr,
  747. cctxPtr->tmpIn, blockSize,
  748. compress, cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel,
  749. cctxPtr->cdict,
  750. cctxPtr->prefs.frameInfo.blockChecksumFlag);
  751. if (cctxPtr->prefs.frameInfo.blockMode==LZ4F_blockLinked) cctxPtr->tmpIn += blockSize;
  752. cctxPtr->tmpInSize = 0;
  753. }
  754. }
  755. while ((size_t)(srcEnd - srcPtr) >= blockSize) {
  756. /* compress full blocks */
  757. lastBlockCompressed = fromSrcBuffer;
  758. dstPtr += LZ4F_makeBlock(dstPtr,
  759. srcPtr, blockSize,
  760. compress, cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel,
  761. cctxPtr->cdict,
  762. cctxPtr->prefs.frameInfo.blockChecksumFlag);
  763. srcPtr += blockSize;
  764. }
  765. if ((cctxPtr->prefs.autoFlush) && (srcPtr < srcEnd)) {
  766. /* compress remaining input < blockSize */
  767. lastBlockCompressed = fromSrcBuffer;
  768. dstPtr += LZ4F_makeBlock(dstPtr,
  769. srcPtr, (size_t)(srcEnd - srcPtr),
  770. compress, cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel,
  771. cctxPtr->cdict,
  772. cctxPtr->prefs.frameInfo.blockChecksumFlag);
  773. srcPtr = srcEnd;
  774. }
  775. /* preserve dictionary if necessary */
  776. if ((cctxPtr->prefs.frameInfo.blockMode==LZ4F_blockLinked) && (lastBlockCompressed==fromSrcBuffer)) {
  777. if (compressOptionsPtr->stableSrc) {
  778. cctxPtr->tmpIn = cctxPtr->tmpBuff;
  779. } else {
  780. int const realDictSize = LZ4F_localSaveDict(cctxPtr);
  781. if (realDictSize==0) return err0r(LZ4F_ERROR_GENERIC);
  782. cctxPtr->tmpIn = cctxPtr->tmpBuff + realDictSize;
  783. }
  784. }
  785. /* keep tmpIn within limits */
  786. if ((cctxPtr->tmpIn + blockSize) > (cctxPtr->tmpBuff + cctxPtr->maxBufferSize) /* necessarily LZ4F_blockLinked && lastBlockCompressed==fromTmpBuffer */
  787. && !(cctxPtr->prefs.autoFlush))
  788. {
  789. int const realDictSize = LZ4F_localSaveDict(cctxPtr);
  790. cctxPtr->tmpIn = cctxPtr->tmpBuff + realDictSize;
  791. }
  792. /* some input data left, necessarily < blockSize */
  793. if (srcPtr < srcEnd) {
  794. /* fill tmp buffer */
  795. size_t const sizeToCopy = (size_t)(srcEnd - srcPtr);
  796. memcpy(cctxPtr->tmpIn, srcPtr, sizeToCopy);
  797. cctxPtr->tmpInSize = sizeToCopy;
  798. }
  799. if (cctxPtr->prefs.frameInfo.contentChecksumFlag == LZ4F_contentChecksumEnabled)
  800. (void)XXH32_update(&(cctxPtr->xxh), srcBuffer, srcSize);
  801. cctxPtr->totalInSize += srcSize;
  802. return (size_t)(dstPtr - dstStart);
  803. }
  804. /*! LZ4F_flush() :
  805. * When compressed data must be sent immediately, without waiting for a block to be filled,
  806. * invoke LZ4_flush(), which will immediately compress any remaining data stored within LZ4F_cctx.
  807. * The result of the function is the number of bytes written into dstBuffer.
  808. * It can be zero, this means there was no data left within LZ4F_cctx.
  809. * The function outputs an error code if it fails (can be tested using LZ4F_isError())
  810. * LZ4F_compressOptions_t* is optional. NULL is a valid argument.
  811. */
  812. size_t LZ4F_flush(LZ4F_cctx* cctxPtr,
  813. void* dstBuffer, size_t dstCapacity,
  814. const LZ4F_compressOptions_t* compressOptionsPtr)
  815. {
  816. BYTE* const dstStart = (BYTE*)dstBuffer;
  817. BYTE* dstPtr = dstStart;
  818. compressFunc_t compress;
  819. if (cctxPtr->tmpInSize == 0) return 0; /* nothing to flush */
  820. if (cctxPtr->cStage != 1) return err0r(LZ4F_ERROR_GENERIC);
  821. if (dstCapacity < (cctxPtr->tmpInSize + BHSize + BFSize))
  822. return err0r(LZ4F_ERROR_dstMaxSize_tooSmall);
  823. (void)compressOptionsPtr; /* not yet useful */
  824. /* select compression function */
  825. compress = LZ4F_selectCompression(cctxPtr->prefs.frameInfo.blockMode, cctxPtr->prefs.compressionLevel);
  826. /* compress tmp buffer */
  827. dstPtr += LZ4F_makeBlock(dstPtr,
  828. cctxPtr->tmpIn, cctxPtr->tmpInSize,
  829. compress, cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel,
  830. cctxPtr->cdict,
  831. cctxPtr->prefs.frameInfo.blockChecksumFlag);
  832. assert(((void)"flush overflows dstBuffer!", (size_t)(dstPtr - dstStart) <= dstCapacity));
  833. if (cctxPtr->prefs.frameInfo.blockMode == LZ4F_blockLinked)
  834. cctxPtr->tmpIn += cctxPtr->tmpInSize;
  835. cctxPtr->tmpInSize = 0;
  836. /* keep tmpIn within limits */
  837. if ((cctxPtr->tmpIn + cctxPtr->maxBlockSize) > (cctxPtr->tmpBuff + cctxPtr->maxBufferSize)) { /* necessarily LZ4F_blockLinked */
  838. int const realDictSize = LZ4F_localSaveDict(cctxPtr);
  839. cctxPtr->tmpIn = cctxPtr->tmpBuff + realDictSize;
  840. }
  841. return (size_t)(dstPtr - dstStart);
  842. }
  843. /*! LZ4F_compressEnd() :
  844. * When you want to properly finish the compressed frame, just call LZ4F_compressEnd().
  845. * It will flush whatever data remained within compressionContext (like LZ4_flush())
  846. * but also properly finalize the frame, with an endMark and an (optional) checksum.
  847. * LZ4F_compressOptions_t structure is optional : you can provide NULL as argument.
  848. * @return: the number of bytes written into dstBuffer (necessarily >= 4 (endMark size))
  849. * or an error code if it fails (can be tested using LZ4F_isError())
  850. * The context can then be used again to compress a new frame, starting with LZ4F_compressBegin().
  851. */
  852. size_t LZ4F_compressEnd(LZ4F_cctx* cctxPtr,
  853. void* dstBuffer, size_t dstCapacity,
  854. const LZ4F_compressOptions_t* compressOptionsPtr)
  855. {
  856. BYTE* const dstStart = (BYTE*)dstBuffer;
  857. BYTE* dstPtr = dstStart;
  858. size_t const flushSize = LZ4F_flush(cctxPtr, dstBuffer, dstCapacity, compressOptionsPtr);
  859. if (LZ4F_isError(flushSize)) return flushSize;
  860. dstPtr += flushSize;
  861. assert(flushSize <= dstCapacity);
  862. dstCapacity -= flushSize;
  863. if (dstCapacity < 4) return err0r(LZ4F_ERROR_dstMaxSize_tooSmall);
  864. LZ4F_writeLE32(dstPtr, 0);
  865. dstPtr += 4; /* endMark */
  866. if (cctxPtr->prefs.frameInfo.contentChecksumFlag == LZ4F_contentChecksumEnabled) {
  867. U32 const xxh = XXH32_digest(&(cctxPtr->xxh));
  868. if (dstCapacity < 8) return err0r(LZ4F_ERROR_dstMaxSize_tooSmall);
  869. LZ4F_writeLE32(dstPtr, xxh);
  870. dstPtr+=4; /* content Checksum */
  871. }
  872. cctxPtr->cStage = 0; /* state is now re-usable (with identical preferences) */
  873. cctxPtr->maxBufferSize = 0; /* reuse HC context */
  874. if (cctxPtr->prefs.frameInfo.contentSize) {
  875. if (cctxPtr->prefs.frameInfo.contentSize != cctxPtr->totalInSize)
  876. return err0r(LZ4F_ERROR_frameSize_wrong);
  877. }
  878. return (size_t)(dstPtr - dstStart);
  879. }
  880. /*-***************************************************
  881. * Frame Decompression
  882. *****************************************************/
  883. typedef enum {
  884. dstage_getFrameHeader=0, dstage_storeFrameHeader,
  885. dstage_init,
  886. dstage_getBlockHeader, dstage_storeBlockHeader,
  887. dstage_copyDirect, dstage_getBlockChecksum,
  888. dstage_getCBlock, dstage_storeCBlock,
  889. dstage_flushOut,
  890. dstage_getSuffix, dstage_storeSuffix,
  891. dstage_getSFrameSize, dstage_storeSFrameSize,
  892. dstage_skipSkippable
  893. } dStage_t;
  894. struct LZ4F_dctx_s {
  895. LZ4F_frameInfo_t frameInfo;
  896. U32 version;
  897. dStage_t dStage;
  898. U64 frameRemainingSize;
  899. size_t maxBlockSize;
  900. size_t maxBufferSize;
  901. BYTE* tmpIn;
  902. size_t tmpInSize;
  903. size_t tmpInTarget;
  904. BYTE* tmpOutBuffer;
  905. const BYTE* dict;
  906. size_t dictSize;
  907. BYTE* tmpOut;
  908. size_t tmpOutSize;
  909. size_t tmpOutStart;
  910. XXH32_state_t xxh;
  911. XXH32_state_t blockChecksum;
  912. BYTE header[LZ4F_HEADER_SIZE_MAX];
  913. }; /* typedef'd to LZ4F_dctx in lz4frame.h */
  914. /*! LZ4F_createDecompressionContext() :
  915. * Create a decompressionContext object, which will track all decompression operations.
  916. * Provides a pointer to a fully allocated and initialized LZ4F_decompressionContext object.
  917. * Object can later be released using LZ4F_freeDecompressionContext().
  918. * @return : if != 0, there was an error during context creation.
  919. */
  920. LZ4F_errorCode_t LZ4F_createDecompressionContext(LZ4F_dctx** LZ4F_decompressionContextPtr, unsigned versionNumber)
  921. {
  922. LZ4F_dctx* const dctx = (LZ4F_dctx*)ALLOC_AND_ZERO(sizeof(LZ4F_dctx));
  923. if (dctx == NULL) { /* failed allocation */
  924. *LZ4F_decompressionContextPtr = NULL;
  925. return err0r(LZ4F_ERROR_allocation_failed);
  926. }
  927. dctx->version = versionNumber;
  928. *LZ4F_decompressionContextPtr = dctx;
  929. return LZ4F_OK_NoError;
  930. }
  931. LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_dctx* dctx)
  932. {
  933. LZ4F_errorCode_t result = LZ4F_OK_NoError;
  934. if (dctx != NULL) { /* can accept NULL input, like free() */
  935. result = (LZ4F_errorCode_t)dctx->dStage;
  936. FREEMEM(dctx->tmpIn);
  937. FREEMEM(dctx->tmpOutBuffer);
  938. FREEMEM(dctx);
  939. }
  940. return result;
  941. }
  942. /*==--- Streaming Decompression operations ---==*/
  943. void LZ4F_resetDecompressionContext(LZ4F_dctx* dctx)
  944. {
  945. dctx->dStage = dstage_getFrameHeader;
  946. dctx->dict = NULL;
  947. dctx->dictSize = 0;
  948. }
  949. /*! LZ4F_decodeHeader() :
  950. * input : `src` points at the **beginning of the frame**
  951. * output : set internal values of dctx, such as
  952. * dctx->frameInfo and dctx->dStage.
  953. * Also allocates internal buffers.
  954. * @return : nb Bytes read from src (necessarily <= srcSize)
  955. * or an error code (testable with LZ4F_isError())
  956. */
  957. static size_t LZ4F_decodeHeader(LZ4F_dctx* dctx, const void* src, size_t srcSize)
  958. {
  959. unsigned blockMode, blockChecksumFlag, contentSizeFlag, contentChecksumFlag, dictIDFlag, blockSizeID;
  960. size_t frameHeaderSize;
  961. const BYTE* srcPtr = (const BYTE*)src;
  962. /* need to decode header to get frameInfo */
  963. if (srcSize < minFHSize) return err0r(LZ4F_ERROR_frameHeader_incomplete); /* minimal frame header size */
  964. MEM_INIT(&(dctx->frameInfo), 0, sizeof(dctx->frameInfo));
  965. /* special case : skippable frames */
  966. if ((LZ4F_readLE32(srcPtr) & 0xFFFFFFF0U) == LZ4F_MAGIC_SKIPPABLE_START) {
  967. dctx->frameInfo.frameType = LZ4F_skippableFrame;
  968. if (src == (void*)(dctx->header)) {
  969. dctx->tmpInSize = srcSize;
  970. dctx->tmpInTarget = 8;
  971. dctx->dStage = dstage_storeSFrameSize;
  972. return srcSize;
  973. } else {
  974. dctx->dStage = dstage_getSFrameSize;
  975. return 4;
  976. }
  977. }
  978. /* control magic number */
  979. if (LZ4F_readLE32(srcPtr) != LZ4F_MAGICNUMBER)
  980. return err0r(LZ4F_ERROR_frameType_unknown);
  981. dctx->frameInfo.frameType = LZ4F_frame;
  982. /* Flags */
  983. { U32 const FLG = srcPtr[4];
  984. U32 const version = (FLG>>6) & _2BITS;
  985. blockChecksumFlag = (FLG>>4) & _1BIT;
  986. blockMode = (FLG>>5) & _1BIT;
  987. contentSizeFlag = (FLG>>3) & _1BIT;
  988. contentChecksumFlag = (FLG>>2) & _1BIT;
  989. dictIDFlag = FLG & _1BIT;
  990. /* validate */
  991. if (((FLG>>1)&_1BIT) != 0) return err0r(LZ4F_ERROR_reservedFlag_set); /* Reserved bit */
  992. if (version != 1) return err0r(LZ4F_ERROR_headerVersion_wrong); /* Version Number, only supported value */
  993. }
  994. /* Frame Header Size */
  995. frameHeaderSize = minFHSize + (contentSizeFlag?8:0) + (dictIDFlag?4:0);
  996. if (srcSize < frameHeaderSize) {
  997. /* not enough input to fully decode frame header */
  998. if (srcPtr != dctx->header)
  999. memcpy(dctx->header, srcPtr, srcSize);
  1000. dctx->tmpInSize = srcSize;
  1001. dctx->tmpInTarget = frameHeaderSize;
  1002. dctx->dStage = dstage_storeFrameHeader;
  1003. return srcSize;
  1004. }
  1005. { U32 const BD = srcPtr[5];
  1006. blockSizeID = (BD>>4) & _3BITS;
  1007. /* validate */
  1008. if (((BD>>7)&_1BIT) != 0) return err0r(LZ4F_ERROR_reservedFlag_set); /* Reserved bit */
  1009. if (blockSizeID < 4) return err0r(LZ4F_ERROR_maxBlockSize_invalid); /* 4-7 only supported values for the time being */
  1010. if (((BD>>0)&_4BITS) != 0) return err0r(LZ4F_ERROR_reservedFlag_set); /* Reserved bits */
  1011. }
  1012. /* check header */
  1013. assert(frameHeaderSize > 5);
  1014. { BYTE const HC = LZ4F_headerChecksum(srcPtr+4, frameHeaderSize-5);
  1015. if (HC != srcPtr[frameHeaderSize-1])
  1016. return err0r(LZ4F_ERROR_headerChecksum_invalid);
  1017. }
  1018. /* save */
  1019. dctx->frameInfo.blockMode = (LZ4F_blockMode_t)blockMode;
  1020. dctx->frameInfo.blockChecksumFlag = (LZ4F_blockChecksum_t)blockChecksumFlag;
  1021. dctx->frameInfo.contentChecksumFlag = (LZ4F_contentChecksum_t)contentChecksumFlag;
  1022. dctx->frameInfo.blockSizeID = (LZ4F_blockSizeID_t)blockSizeID;
  1023. dctx->maxBlockSize = LZ4F_getBlockSize(blockSizeID);
  1024. if (contentSizeFlag)
  1025. dctx->frameRemainingSize =
  1026. dctx->frameInfo.contentSize = LZ4F_readLE64(srcPtr+6);
  1027. if (dictIDFlag)
  1028. dctx->frameInfo.dictID = LZ4F_readLE32(srcPtr + frameHeaderSize - 5);
  1029. dctx->dStage = dstage_init;
  1030. return frameHeaderSize;
  1031. }
  1032. /*! LZ4F_headerSize() :
  1033. * @return : size of frame header
  1034. * or an error code, which can be tested using LZ4F_isError()
  1035. */
  1036. size_t LZ4F_headerSize(const void* src, size_t srcSize)
  1037. {
  1038. if (src == NULL) return err0r(LZ4F_ERROR_srcPtr_wrong);
  1039. /* minimal srcSize to determine header size */
  1040. if (srcSize < LZ4F_MIN_SIZE_TO_KNOW_HEADER_LENGTH)
  1041. return err0r(LZ4F_ERROR_frameHeader_incomplete);
  1042. /* special case : skippable frames */
  1043. if ((LZ4F_readLE32(src) & 0xFFFFFFF0U) == LZ4F_MAGIC_SKIPPABLE_START)
  1044. return 8;
  1045. /* control magic number */
  1046. if (LZ4F_readLE32(src) != LZ4F_MAGICNUMBER)
  1047. return err0r(LZ4F_ERROR_frameType_unknown);
  1048. /* Frame Header Size */
  1049. { BYTE const FLG = ((const BYTE*)src)[4];
  1050. U32 const contentSizeFlag = (FLG>>3) & _1BIT;
  1051. U32 const dictIDFlag = FLG & _1BIT;
  1052. return minFHSize + (contentSizeFlag?8:0) + (dictIDFlag?4:0);
  1053. }
  1054. }
  1055. /*! LZ4F_getFrameInfo() :
  1056. * This function extracts frame parameters (max blockSize, frame checksum, etc.).
  1057. * Usage is optional. Objective is to provide relevant information for allocation purposes.
  1058. * This function works in 2 situations :
  1059. * - At the beginning of a new frame, in which case it will decode this information from `srcBuffer`, and start the decoding process.
  1060. * Amount of input data provided must be large enough to successfully decode the frame header.
  1061. * A header size is variable, but is guaranteed to be <= LZ4F_HEADER_SIZE_MAX bytes. It's possible to provide more input data than this minimum.
  1062. * - After decoding has been started. In which case, no input is read, frame parameters are extracted from dctx.
  1063. * The number of bytes consumed from srcBuffer will be updated within *srcSizePtr (necessarily <= original value).
  1064. * Decompression must resume from (srcBuffer + *srcSizePtr).
  1065. * @return : an hint about how many srcSize bytes LZ4F_decompress() expects for next call,
  1066. * or an error code which can be tested using LZ4F_isError()
  1067. * note 1 : in case of error, dctx is not modified. Decoding operations can resume from where they stopped.
  1068. * note 2 : frame parameters are *copied into* an already allocated LZ4F_frameInfo_t structure.
  1069. */
  1070. LZ4F_errorCode_t LZ4F_getFrameInfo(LZ4F_dctx* dctx,
  1071. LZ4F_frameInfo_t* frameInfoPtr,
  1072. const void* srcBuffer, size_t* srcSizePtr)
  1073. {
  1074. LZ4F_STATIC_ASSERT(dstage_getFrameHeader < dstage_storeFrameHeader);
  1075. if (dctx->dStage > dstage_storeFrameHeader) {
  1076. /* frameInfo already decoded */
  1077. size_t o=0, i=0;
  1078. *srcSizePtr = 0;
  1079. *frameInfoPtr = dctx->frameInfo;
  1080. /* returns : recommended nb of bytes for LZ4F_decompress() */
  1081. return LZ4F_decompress(dctx, NULL, &o, NULL, &i, NULL);
  1082. } else {
  1083. if (dctx->dStage == dstage_storeFrameHeader) {
  1084. /* frame decoding already started, in the middle of header => automatic fail */
  1085. *srcSizePtr = 0;
  1086. return err0r(LZ4F_ERROR_frameDecoding_alreadyStarted);
  1087. } else {
  1088. size_t const hSize = LZ4F_headerSize(srcBuffer, *srcSizePtr);
  1089. if (LZ4F_isError(hSize)) { *srcSizePtr=0; return hSize; }
  1090. if (*srcSizePtr < hSize) {
  1091. *srcSizePtr=0;
  1092. return err0r(LZ4F_ERROR_frameHeader_incomplete);
  1093. }
  1094. { size_t decodeResult = LZ4F_decodeHeader(dctx, srcBuffer, hSize);
  1095. if (LZ4F_isError(decodeResult)) {
  1096. *srcSizePtr = 0;
  1097. } else {
  1098. *srcSizePtr = decodeResult;
  1099. decodeResult = BHSize; /* block header size */
  1100. }
  1101. *frameInfoPtr = dctx->frameInfo;
  1102. return decodeResult;
  1103. } } }
  1104. }
  1105. /* LZ4F_updateDict() :
  1106. * only used for LZ4F_blockLinked mode */
  1107. static void LZ4F_updateDict(LZ4F_dctx* dctx,
  1108. const BYTE* dstPtr, size_t dstSize, const BYTE* dstBufferStart,
  1109. unsigned withinTmp)
  1110. {
  1111. if (dctx->dictSize==0)
  1112. dctx->dict = (const BYTE*)dstPtr; /* priority to dictionary continuity */
  1113. if (dctx->dict + dctx->dictSize == dstPtr) { /* dictionary continuity, directly within dstBuffer */
  1114. dctx->dictSize += dstSize;
  1115. return;
  1116. }
  1117. assert(dstPtr >= dstBufferStart);
  1118. if ((size_t)(dstPtr - dstBufferStart) + dstSize >= 64 KB) { /* history in dstBuffer becomes large enough to become dictionary */
  1119. dctx->dict = (const BYTE*)dstBufferStart;
  1120. dctx->dictSize = (size_t)(dstPtr - dstBufferStart) + dstSize;
  1121. return;
  1122. }
  1123. assert(dstSize < 64 KB); /* if dstSize >= 64 KB, dictionary would be set into dstBuffer directly */
  1124. /* dstBuffer does not contain whole useful history (64 KB), so it must be saved within tmpOut */
  1125. if ((withinTmp) && (dctx->dict == dctx->tmpOutBuffer)) { /* continue history within tmpOutBuffer */
  1126. /* withinTmp expectation : content of [dstPtr,dstSize] is same as [dict+dictSize,dstSize], so we just extend it */
  1127. assert(dctx->dict + dctx->dictSize == dctx->tmpOut + dctx->tmpOutStart);
  1128. dctx->dictSize += dstSize;
  1129. return;
  1130. }
  1131. if (withinTmp) { /* copy relevant dict portion in front of tmpOut within tmpOutBuffer */
  1132. size_t const preserveSize = (size_t)(dctx->tmpOut - dctx->tmpOutBuffer);
  1133. size_t copySize = 64 KB - dctx->tmpOutSize;
  1134. const BYTE* const oldDictEnd = dctx->dict + dctx->dictSize - dctx->tmpOutStart;
  1135. if (dctx->tmpOutSize > 64 KB) copySize = 0;
  1136. if (copySize > preserveSize) copySize = preserveSize;
  1137. memcpy(dctx->tmpOutBuffer + preserveSize - copySize, oldDictEnd - copySize, copySize);
  1138. dctx->dict = dctx->tmpOutBuffer;
  1139. dctx->dictSize = preserveSize + dctx->tmpOutStart + dstSize;
  1140. return;
  1141. }
  1142. if (dctx->dict == dctx->tmpOutBuffer) { /* copy dst into tmp to complete dict */
  1143. if (dctx->dictSize + dstSize > dctx->maxBufferSize) { /* tmp buffer not large enough */
  1144. size_t const preserveSize = 64 KB - dstSize;
  1145. memcpy(dctx->tmpOutBuffer, dctx->dict + dctx->dictSize - preserveSize, preserveSize);
  1146. dctx->dictSize = preserveSize;
  1147. }
  1148. memcpy(dctx->tmpOutBuffer + dctx->dictSize, dstPtr, dstSize);
  1149. dctx->dictSize += dstSize;
  1150. return;
  1151. }
  1152. /* join dict & dest into tmp */
  1153. { size_t preserveSize = 64 KB - dstSize;
  1154. if (preserveSize > dctx->dictSize) preserveSize = dctx->dictSize;
  1155. memcpy(dctx->tmpOutBuffer, dctx->dict + dctx->dictSize - preserveSize, preserveSize);
  1156. memcpy(dctx->tmpOutBuffer + preserveSize, dstPtr, dstSize);
  1157. dctx->dict = dctx->tmpOutBuffer;
  1158. dctx->dictSize = preserveSize + dstSize;
  1159. }
  1160. }
  1161. /*! LZ4F_decompress() :
  1162. * Call this function repetitively to regenerate compressed data in srcBuffer.
  1163. * The function will attempt to decode up to *srcSizePtr bytes from srcBuffer
  1164. * into dstBuffer of capacity *dstSizePtr.
  1165. *
  1166. * The number of bytes regenerated into dstBuffer will be provided within *dstSizePtr (necessarily <= original value).
  1167. *
  1168. * The number of bytes effectively read from srcBuffer will be provided within *srcSizePtr (necessarily <= original value).
  1169. * If number of bytes read is < number of bytes provided, then decompression operation is not complete.
  1170. * Remaining data will have to be presented again in a subsequent invocation.
  1171. *
  1172. * The function result is an hint of the better srcSize to use for next call to LZ4F_decompress.
  1173. * Schematically, it's the size of the current (or remaining) compressed block + header of next block.
  1174. * Respecting the hint provides a small boost to performance, since it allows less buffer shuffling.
  1175. * Note that this is just a hint, and it's always possible to any srcSize value.
  1176. * When a frame is fully decoded, @return will be 0.
  1177. * If decompression failed, @return is an error code which can be tested using LZ4F_isError().
  1178. */
  1179. size_t LZ4F_decompress(LZ4F_dctx* dctx,
  1180. void* dstBuffer, size_t* dstSizePtr,
  1181. const void* srcBuffer, size_t* srcSizePtr,
  1182. const LZ4F_decompressOptions_t* decompressOptionsPtr)
  1183. {
  1184. LZ4F_decompressOptions_t optionsNull;
  1185. const BYTE* const srcStart = (const BYTE*)srcBuffer;
  1186. const BYTE* const srcEnd = srcStart + *srcSizePtr;
  1187. const BYTE* srcPtr = srcStart;
  1188. BYTE* const dstStart = (BYTE*)dstBuffer;
  1189. BYTE* const dstEnd = dstStart + *dstSizePtr;
  1190. BYTE* dstPtr = dstStart;
  1191. const BYTE* selectedIn = NULL;
  1192. unsigned doAnotherStage = 1;
  1193. size_t nextSrcSizeHint = 1;
  1194. MEM_INIT(&optionsNull, 0, sizeof(optionsNull));
  1195. if (decompressOptionsPtr==NULL) decompressOptionsPtr = &optionsNull;
  1196. *srcSizePtr = 0;
  1197. *dstSizePtr = 0;
  1198. /* behaves as a state machine */
  1199. while (doAnotherStage) {
  1200. switch(dctx->dStage)
  1201. {
  1202. case dstage_getFrameHeader:
  1203. if ((size_t)(srcEnd-srcPtr) >= maxFHSize) { /* enough to decode - shortcut */
  1204. size_t const hSize = LZ4F_decodeHeader(dctx, srcPtr, (size_t)(srcEnd-srcPtr)); /* will update dStage appropriately */
  1205. if (LZ4F_isError(hSize)) return hSize;
  1206. srcPtr += hSize;
  1207. break;
  1208. }
  1209. dctx->tmpInSize = 0;
  1210. if (srcEnd-srcPtr == 0) return minFHSize; /* 0-size input */
  1211. dctx->tmpInTarget = minFHSize; /* minimum size to decode header */
  1212. dctx->dStage = dstage_storeFrameHeader;
  1213. /* fall-through */
  1214. case dstage_storeFrameHeader:
  1215. { size_t const sizeToCopy = MIN(dctx->tmpInTarget - dctx->tmpInSize, (size_t)(srcEnd - srcPtr));
  1216. memcpy(dctx->header + dctx->tmpInSize, srcPtr, sizeToCopy);
  1217. dctx->tmpInSize += sizeToCopy;
  1218. srcPtr += sizeToCopy;
  1219. }
  1220. if (dctx->tmpInSize < dctx->tmpInTarget) {
  1221. nextSrcSizeHint = (dctx->tmpInTarget - dctx->tmpInSize) + BHSize; /* rest of header + nextBlockHeader */
  1222. doAnotherStage = 0; /* not enough src data, ask for some more */
  1223. break;
  1224. }
  1225. { size_t const hSize = LZ4F_decodeHeader(dctx, dctx->header, dctx->tmpInTarget); /* will update dStage appropriately */
  1226. if (LZ4F_isError(hSize)) return hSize;
  1227. }
  1228. break;
  1229. case dstage_init:
  1230. if (dctx->frameInfo.contentChecksumFlag) (void)XXH32_reset(&(dctx->xxh), 0);
  1231. /* internal buffers allocation */
  1232. { size_t const bufferNeeded = dctx->maxBlockSize
  1233. + ((dctx->frameInfo.blockMode==LZ4F_blockLinked) ? 128 KB : 0);
  1234. if (bufferNeeded > dctx->maxBufferSize) { /* tmp buffers too small */
  1235. dctx->maxBufferSize = 0; /* ensure allocation will be re-attempted on next entry*/
  1236. FREEMEM(dctx->tmpIn);
  1237. dctx->tmpIn = (BYTE*)ALLOC(dctx->maxBlockSize + BFSize /* block checksum */);
  1238. if (dctx->tmpIn == NULL)
  1239. return err0r(LZ4F_ERROR_allocation_failed);
  1240. FREEMEM(dctx->tmpOutBuffer);
  1241. dctx->tmpOutBuffer= (BYTE*)ALLOC(bufferNeeded);
  1242. if (dctx->tmpOutBuffer== NULL)
  1243. return err0r(LZ4F_ERROR_allocation_failed);
  1244. dctx->maxBufferSize = bufferNeeded;
  1245. } }
  1246. dctx->tmpInSize = 0;
  1247. dctx->tmpInTarget = 0;
  1248. dctx->tmpOut = dctx->tmpOutBuffer;
  1249. dctx->tmpOutStart = 0;
  1250. dctx->tmpOutSize = 0;
  1251. dctx->dStage = dstage_getBlockHeader;
  1252. /* fall-through */
  1253. case dstage_getBlockHeader:
  1254. if ((size_t)(srcEnd - srcPtr) >= BHSize) {
  1255. selectedIn = srcPtr;
  1256. srcPtr += BHSize;
  1257. } else {
  1258. /* not enough input to read cBlockSize field */
  1259. dctx->tmpInSize = 0;
  1260. dctx->dStage = dstage_storeBlockHeader;
  1261. }
  1262. if (dctx->dStage == dstage_storeBlockHeader) /* can be skipped */
  1263. case dstage_storeBlockHeader:
  1264. { size_t const remainingInput = (size_t)(srcEnd - srcPtr);
  1265. size_t const wantedData = BHSize - dctx->tmpInSize;
  1266. size_t const sizeToCopy = MIN(wantedData, remainingInput);
  1267. memcpy(dctx->tmpIn + dctx->tmpInSize, srcPtr, sizeToCopy);
  1268. srcPtr += sizeToCopy;
  1269. dctx->tmpInSize += sizeToCopy;
  1270. if (dctx->tmpInSize < BHSize) { /* not enough input for cBlockSize */
  1271. nextSrcSizeHint = BHSize - dctx->tmpInSize;
  1272. doAnotherStage = 0;
  1273. break;
  1274. }
  1275. selectedIn = dctx->tmpIn;
  1276. } /* if (dctx->dStage == dstage_storeBlockHeader) */
  1277. /* decode block header */
  1278. { size_t const nextCBlockSize = LZ4F_readLE32(selectedIn) & 0x7FFFFFFFU;
  1279. size_t const crcSize = dctx->frameInfo.blockChecksumFlag * BFSize;
  1280. if (nextCBlockSize==0) { /* frameEnd signal, no more block */
  1281. dctx->dStage = dstage_getSuffix;
  1282. break;
  1283. }
  1284. if (nextCBlockSize > dctx->maxBlockSize)
  1285. return err0r(LZ4F_ERROR_maxBlockSize_invalid);
  1286. if (LZ4F_readLE32(selectedIn) & LZ4F_BLOCKUNCOMPRESSED_FLAG) {
  1287. /* next block is uncompressed */
  1288. dctx->tmpInTarget = nextCBlockSize;
  1289. if (dctx->frameInfo.blockChecksumFlag) {
  1290. (void)XXH32_reset(&dctx->blockChecksum, 0);
  1291. }
  1292. dctx->dStage = dstage_copyDirect;
  1293. break;
  1294. }
  1295. /* next block is a compressed block */
  1296. dctx->tmpInTarget = nextCBlockSize + crcSize;
  1297. dctx->dStage = dstage_getCBlock;
  1298. if (dstPtr==dstEnd) {
  1299. nextSrcSizeHint = BHSize + nextCBlockSize + crcSize;
  1300. doAnotherStage = 0;
  1301. }
  1302. break;
  1303. }
  1304. case dstage_copyDirect: /* uncompressed block */
  1305. { size_t const minBuffSize = MIN((size_t)(srcEnd-srcPtr), (size_t)(dstEnd-dstPtr));
  1306. size_t const sizeToCopy = MIN(dctx->tmpInTarget, minBuffSize);
  1307. memcpy(dstPtr, srcPtr, sizeToCopy);
  1308. if (dctx->frameInfo.blockChecksumFlag) {
  1309. (void)XXH32_update(&dctx->blockChecksum, srcPtr, sizeToCopy);
  1310. }
  1311. if (dctx->frameInfo.contentChecksumFlag)
  1312. (void)XXH32_update(&dctx->xxh, srcPtr, sizeToCopy);
  1313. if (dctx->frameInfo.contentSize)
  1314. dctx->frameRemainingSize -= sizeToCopy;
  1315. /* history management (linked blocks only)*/
  1316. if (dctx->frameInfo.blockMode == LZ4F_blockLinked)
  1317. LZ4F_updateDict(dctx, dstPtr, sizeToCopy, dstStart, 0);
  1318. srcPtr += sizeToCopy;
  1319. dstPtr += sizeToCopy;
  1320. if (sizeToCopy == dctx->tmpInTarget) { /* all done */
  1321. if (dctx->frameInfo.blockChecksumFlag) {
  1322. dctx->tmpInSize = 0;
  1323. dctx->dStage = dstage_getBlockChecksum;
  1324. } else
  1325. dctx->dStage = dstage_getBlockHeader; /* new block */
  1326. break;
  1327. }
  1328. dctx->tmpInTarget -= sizeToCopy; /* need to copy more */
  1329. nextSrcSizeHint = dctx->tmpInTarget +
  1330. +(dctx->frameInfo.blockChecksumFlag ? BFSize : 0)
  1331. + BHSize /* next header size */;
  1332. doAnotherStage = 0;
  1333. break;
  1334. }
  1335. /* check block checksum for recently transferred uncompressed block */
  1336. case dstage_getBlockChecksum:
  1337. { const void* crcSrc;
  1338. if ((srcEnd-srcPtr >= 4) && (dctx->tmpInSize==0)) {
  1339. crcSrc = srcPtr;
  1340. srcPtr += 4;
  1341. } else {
  1342. size_t const stillToCopy = 4 - dctx->tmpInSize;
  1343. size_t const sizeToCopy = MIN(stillToCopy, (size_t)(srcEnd-srcPtr));
  1344. memcpy(dctx->header + dctx->tmpInSize, srcPtr, sizeToCopy);
  1345. dctx->tmpInSize += sizeToCopy;
  1346. srcPtr += sizeToCopy;
  1347. if (dctx->tmpInSize < 4) { /* all input consumed */
  1348. doAnotherStage = 0;
  1349. break;
  1350. }
  1351. crcSrc = dctx->header;
  1352. }
  1353. { U32 const readCRC = LZ4F_readLE32(crcSrc);
  1354. U32 const calcCRC = XXH32_digest(&dctx->blockChecksum);
  1355. if (readCRC != calcCRC)
  1356. return err0r(LZ4F_ERROR_blockChecksum_invalid);
  1357. } }
  1358. dctx->dStage = dstage_getBlockHeader; /* new block */
  1359. break;
  1360. case dstage_getCBlock:
  1361. if ((size_t)(srcEnd-srcPtr) < dctx->tmpInTarget) {
  1362. dctx->tmpInSize = 0;
  1363. dctx->dStage = dstage_storeCBlock;
  1364. break;
  1365. }
  1366. /* input large enough to read full block directly */
  1367. selectedIn = srcPtr;
  1368. srcPtr += dctx->tmpInTarget;
  1369. if (0) /* jump over next block */
  1370. case dstage_storeCBlock:
  1371. { size_t const wantedData = dctx->tmpInTarget - dctx->tmpInSize;
  1372. size_t const inputLeft = (size_t)(srcEnd-srcPtr);
  1373. size_t const sizeToCopy = MIN(wantedData, inputLeft);
  1374. memcpy(dctx->tmpIn + dctx->tmpInSize, srcPtr, sizeToCopy);
  1375. dctx->tmpInSize += sizeToCopy;
  1376. srcPtr += sizeToCopy;
  1377. if (dctx->tmpInSize < dctx->tmpInTarget) { /* need more input */
  1378. nextSrcSizeHint = (dctx->tmpInTarget - dctx->tmpInSize)
  1379. + (dctx->frameInfo.blockChecksumFlag ? BFSize : 0)
  1380. + BHSize /* next header size */;
  1381. doAnotherStage = 0;
  1382. break;
  1383. }
  1384. selectedIn = dctx->tmpIn;
  1385. }
  1386. /* At this stage, input is large enough to decode a block */
  1387. if (dctx->frameInfo.blockChecksumFlag) {
  1388. dctx->tmpInTarget -= 4;
  1389. assert(selectedIn != NULL); /* selectedIn is defined at this stage (either srcPtr, or dctx->tmpIn) */
  1390. { U32 const readBlockCrc = LZ4F_readLE32(selectedIn + dctx->tmpInTarget);
  1391. U32 const calcBlockCrc = XXH32(selectedIn, dctx->tmpInTarget, 0);
  1392. if (readBlockCrc != calcBlockCrc)
  1393. return err0r(LZ4F_ERROR_blockChecksum_invalid);
  1394. } }
  1395. if ((size_t)(dstEnd-dstPtr) >= dctx->maxBlockSize) {
  1396. const char* dict = (const char*)dctx->dict;
  1397. size_t dictSize = dctx->dictSize;
  1398. int decodedSize;
  1399. if (dict && dictSize > 1 GB) {
  1400. /* the dictSize param is an int, avoid truncation / sign issues */
  1401. dict += dictSize - 64 KB;
  1402. dictSize = 64 KB;
  1403. }
  1404. /* enough capacity in `dst` to decompress directly there */
  1405. decodedSize = LZ4_decompress_safe_usingDict(
  1406. (const char*)selectedIn, (char*)dstPtr,
  1407. (int)dctx->tmpInTarget, (int)dctx->maxBlockSize,
  1408. dict, (int)dictSize);
  1409. if (decodedSize < 0) return err0r(LZ4F_ERROR_GENERIC); /* decompression failed */
  1410. if (dctx->frameInfo.contentChecksumFlag)
  1411. XXH32_update(&(dctx->xxh), dstPtr, (size_t)decodedSize);
  1412. if (dctx->frameInfo.contentSize)
  1413. dctx->frameRemainingSize -= (size_t)decodedSize;
  1414. /* dictionary management */
  1415. if (dctx->frameInfo.blockMode==LZ4F_blockLinked)
  1416. LZ4F_updateDict(dctx, dstPtr, (size_t)decodedSize, dstStart, 0);
  1417. dstPtr += decodedSize;
  1418. dctx->dStage = dstage_getBlockHeader;
  1419. break;
  1420. }
  1421. /* not enough place into dst : decode into tmpOut */
  1422. /* ensure enough place for tmpOut */
  1423. if (dctx->frameInfo.blockMode == LZ4F_blockLinked) {
  1424. if (dctx->dict == dctx->tmpOutBuffer) {
  1425. if (dctx->dictSize > 128 KB) {
  1426. memcpy(dctx->tmpOutBuffer, dctx->dict + dctx->dictSize - 64 KB, 64 KB);
  1427. dctx->dictSize = 64 KB;
  1428. }
  1429. dctx->tmpOut = dctx->tmpOutBuffer + dctx->dictSize;
  1430. } else { /* dict not within tmp */
  1431. size_t const reservedDictSpace = MIN(dctx->dictSize, 64 KB);
  1432. dctx->tmpOut = dctx->tmpOutBuffer + reservedDictSpace;
  1433. } }
  1434. /* Decode block */
  1435. { const char* dict = (const char*)dctx->dict;
  1436. size_t dictSize = dctx->dictSize;
  1437. int decodedSize;
  1438. if (dict && dictSize > 1 GB) {
  1439. /* the dictSize param is an int, avoid truncation / sign issues */
  1440. dict += dictSize - 64 KB;
  1441. dictSize = 64 KB;
  1442. }
  1443. decodedSize = LZ4_decompress_safe_usingDict(
  1444. (const char*)selectedIn, (char*)dctx->tmpOut,
  1445. (int)dctx->tmpInTarget, (int)dctx->maxBlockSize,
  1446. dict, (int)dictSize);
  1447. if (decodedSize < 0) /* decompression failed */
  1448. return err0r(LZ4F_ERROR_decompressionFailed);
  1449. if (dctx->frameInfo.contentChecksumFlag)
  1450. XXH32_update(&(dctx->xxh), dctx->tmpOut, (size_t)decodedSize);
  1451. if (dctx->frameInfo.contentSize)
  1452. dctx->frameRemainingSize -= (size_t)decodedSize;
  1453. dctx->tmpOutSize = (size_t)decodedSize;
  1454. dctx->tmpOutStart = 0;
  1455. dctx->dStage = dstage_flushOut;
  1456. }
  1457. /* fall-through */
  1458. case dstage_flushOut: /* flush decoded data from tmpOut to dstBuffer */
  1459. { size_t const sizeToCopy = MIN(dctx->tmpOutSize - dctx->tmpOutStart, (size_t)(dstEnd-dstPtr));
  1460. memcpy(dstPtr, dctx->tmpOut + dctx->tmpOutStart, sizeToCopy);
  1461. /* dictionary management */
  1462. if (dctx->frameInfo.blockMode == LZ4F_blockLinked)
  1463. LZ4F_updateDict(dctx, dstPtr, sizeToCopy, dstStart, 1 /*withinTmp*/);
  1464. dctx->tmpOutStart += sizeToCopy;
  1465. dstPtr += sizeToCopy;
  1466. if (dctx->tmpOutStart == dctx->tmpOutSize) { /* all flushed */
  1467. dctx->dStage = dstage_getBlockHeader; /* get next block */
  1468. break;
  1469. }
  1470. /* could not flush everything : stop there, just request a block header */
  1471. doAnotherStage = 0;
  1472. nextSrcSizeHint = BHSize;
  1473. break;
  1474. }
  1475. case dstage_getSuffix:
  1476. if (dctx->frameRemainingSize)
  1477. return err0r(LZ4F_ERROR_frameSize_wrong); /* incorrect frame size decoded */
  1478. if (!dctx->frameInfo.contentChecksumFlag) { /* no checksum, frame is completed */
  1479. nextSrcSizeHint = 0;
  1480. LZ4F_resetDecompressionContext(dctx);
  1481. doAnotherStage = 0;
  1482. break;
  1483. }
  1484. if ((srcEnd - srcPtr) < 4) { /* not enough size for entire CRC */
  1485. dctx->tmpInSize = 0;
  1486. dctx->dStage = dstage_storeSuffix;
  1487. } else {
  1488. selectedIn = srcPtr;
  1489. srcPtr += 4;
  1490. }
  1491. if (dctx->dStage == dstage_storeSuffix) /* can be skipped */
  1492. case dstage_storeSuffix:
  1493. { size_t const remainingInput = (size_t)(srcEnd - srcPtr);
  1494. size_t const wantedData = 4 - dctx->tmpInSize;
  1495. size_t const sizeToCopy = MIN(wantedData, remainingInput);
  1496. memcpy(dctx->tmpIn + dctx->tmpInSize, srcPtr, sizeToCopy);
  1497. srcPtr += sizeToCopy;
  1498. dctx->tmpInSize += sizeToCopy;
  1499. if (dctx->tmpInSize < 4) { /* not enough input to read complete suffix */
  1500. nextSrcSizeHint = 4 - dctx->tmpInSize;
  1501. doAnotherStage=0;
  1502. break;
  1503. }
  1504. selectedIn = dctx->tmpIn;
  1505. } /* if (dctx->dStage == dstage_storeSuffix) */
  1506. /* case dstage_checkSuffix: */ /* no direct entry, avoid initialization risks */
  1507. { U32 const readCRC = LZ4F_readLE32(selectedIn);
  1508. U32 const resultCRC = XXH32_digest(&(dctx->xxh));
  1509. if (readCRC != resultCRC)
  1510. return err0r(LZ4F_ERROR_contentChecksum_invalid);
  1511. nextSrcSizeHint = 0;
  1512. LZ4F_resetDecompressionContext(dctx);
  1513. doAnotherStage = 0;
  1514. break;
  1515. }
  1516. case dstage_getSFrameSize:
  1517. if ((srcEnd - srcPtr) >= 4) {
  1518. selectedIn = srcPtr;
  1519. srcPtr += 4;
  1520. } else {
  1521. /* not enough input to read cBlockSize field */
  1522. dctx->tmpInSize = 4;
  1523. dctx->tmpInTarget = 8;
  1524. dctx->dStage = dstage_storeSFrameSize;
  1525. }
  1526. if (dctx->dStage == dstage_storeSFrameSize)
  1527. case dstage_storeSFrameSize:
  1528. { size_t const sizeToCopy = MIN(dctx->tmpInTarget - dctx->tmpInSize,
  1529. (size_t)(srcEnd - srcPtr) );
  1530. memcpy(dctx->header + dctx->tmpInSize, srcPtr, sizeToCopy);
  1531. srcPtr += sizeToCopy;
  1532. dctx->tmpInSize += sizeToCopy;
  1533. if (dctx->tmpInSize < dctx->tmpInTarget) {
  1534. /* not enough input to get full sBlockSize; wait for more */
  1535. nextSrcSizeHint = dctx->tmpInTarget - dctx->tmpInSize;
  1536. doAnotherStage = 0;
  1537. break;
  1538. }
  1539. selectedIn = dctx->header + 4;
  1540. } /* if (dctx->dStage == dstage_storeSFrameSize) */
  1541. /* case dstage_decodeSFrameSize: */ /* no direct entry */
  1542. { size_t const SFrameSize = LZ4F_readLE32(selectedIn);
  1543. dctx->frameInfo.contentSize = SFrameSize;
  1544. dctx->tmpInTarget = SFrameSize;
  1545. dctx->dStage = dstage_skipSkippable;
  1546. break;
  1547. }
  1548. case dstage_skipSkippable:
  1549. { size_t const skipSize = MIN(dctx->tmpInTarget, (size_t)(srcEnd-srcPtr));
  1550. srcPtr += skipSize;
  1551. dctx->tmpInTarget -= skipSize;
  1552. doAnotherStage = 0;
  1553. nextSrcSizeHint = dctx->tmpInTarget;
  1554. if (nextSrcSizeHint) break; /* still more to skip */
  1555. /* frame fully skipped : prepare context for a new frame */
  1556. LZ4F_resetDecompressionContext(dctx);
  1557. break;
  1558. }
  1559. } /* switch (dctx->dStage) */
  1560. } /* while (doAnotherStage) */
  1561. /* preserve history within tmp whenever necessary */
  1562. LZ4F_STATIC_ASSERT((unsigned)dstage_init == 2);
  1563. if ( (dctx->frameInfo.blockMode==LZ4F_blockLinked) /* next block will use up to 64KB from previous ones */
  1564. && (dctx->dict != dctx->tmpOutBuffer) /* dictionary is not already within tmp */
  1565. && (!decompressOptionsPtr->stableDst) /* cannot rely on dst data to remain there for next call */
  1566. && ((unsigned)(dctx->dStage)-2 < (unsigned)(dstage_getSuffix)-2) ) /* valid stages : [init ... getSuffix[ */
  1567. {
  1568. if (dctx->dStage == dstage_flushOut) {
  1569. size_t const preserveSize = (size_t)(dctx->tmpOut - dctx->tmpOutBuffer);
  1570. size_t copySize = 64 KB - dctx->tmpOutSize;
  1571. const BYTE* oldDictEnd = dctx->dict + dctx->dictSize - dctx->tmpOutStart;
  1572. if (dctx->tmpOutSize > 64 KB) copySize = 0;
  1573. if (copySize > preserveSize) copySize = preserveSize;
  1574. if (copySize > 0)
  1575. memcpy(dctx->tmpOutBuffer + preserveSize - copySize, oldDictEnd - copySize, copySize);
  1576. dctx->dict = dctx->tmpOutBuffer;
  1577. dctx->dictSize = preserveSize + dctx->tmpOutStart;
  1578. } else {
  1579. const BYTE* const oldDictEnd = dctx->dict + dctx->dictSize;
  1580. size_t const newDictSize = MIN(dctx->dictSize, 64 KB);
  1581. if (newDictSize > 0)
  1582. memcpy(dctx->tmpOutBuffer, oldDictEnd - newDictSize, newDictSize);
  1583. dctx->dict = dctx->tmpOutBuffer;
  1584. dctx->dictSize = newDictSize;
  1585. dctx->tmpOut = dctx->tmpOutBuffer + newDictSize;
  1586. }
  1587. }
  1588. *srcSizePtr = (size_t)(srcPtr - srcStart);
  1589. *dstSizePtr = (size_t)(dstPtr - dstStart);
  1590. return nextSrcSizeHint;
  1591. }
  1592. /*! LZ4F_decompress_usingDict() :
  1593. * Same as LZ4F_decompress(), using a predefined dictionary.
  1594. * Dictionary is used "in place", without any preprocessing.
  1595. * It must remain accessible throughout the entire frame decoding.
  1596. */
  1597. size_t LZ4F_decompress_usingDict(LZ4F_dctx* dctx,
  1598. void* dstBuffer, size_t* dstSizePtr,
  1599. const void* srcBuffer, size_t* srcSizePtr,
  1600. const void* dict, size_t dictSize,
  1601. const LZ4F_decompressOptions_t* decompressOptionsPtr)
  1602. {
  1603. if (dctx->dStage <= dstage_init) {
  1604. dctx->dict = (const BYTE*)dict;
  1605. dctx->dictSize = dictSize;
  1606. }
  1607. return LZ4F_decompress(dctx, dstBuffer, dstSizePtr,
  1608. srcBuffer, srcSizePtr,
  1609. decompressOptionsPtr);
  1610. }