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