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