70
83
* malloc(), calloc() and free()
71
84
* towards another library or solution of their choice
72
85
* by modifying below section.
88
#include <string.h> /* memset, memcpy, memmove */
89
#ifndef LZ4_SRC_INCLUDED /* avoid redefinition when sources are coalesced */
90
# define MEM_INIT(p,v,s) memset((p),(v),(s))
74
93
#ifndef LZ4_SRC_INCLUDED /* avoid redefinition when sources are coalesced */
75
94
# include <stdlib.h> /* malloc, calloc, free */
76
95
# define ALLOC(s) malloc(s)
78
97
# define FREEMEM(p) free(p)
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))
87
/*-************************************
88
* Library declarations
89
**************************************/
90
#define LZ4F_STATIC_LINKING_ONLY
92
#define LZ4_STATIC_LINKING_ONLY
94
#define LZ4_HC_STATIC_LINKING_ONLY
96
#define XXH_STATIC_LINKING_ONLY
100
static void* LZ4F_calloc(size_t s, LZ4F_CustomMem cmem)
102
/* custom calloc defined : use it */
103
if (cmem.customCalloc != NULL) {
104
return cmem.customCalloc(cmem.opaqueState, s);
106
/* nothing defined : use default <stdlib.h>'s calloc() */
107
if (cmem.customAlloc == NULL) {
108
return ALLOC_AND_ZERO(s);
110
/* only custom alloc defined : use it, and combine it with memset() */
111
{ void* const p = cmem.customAlloc(cmem.opaqueState, s);
112
if (p != NULL) MEM_INIT(p, 0, s);
116
static void* LZ4F_malloc(size_t s, LZ4F_CustomMem cmem)
118
/* custom malloc defined : use it */
119
if (cmem.customAlloc != NULL) {
120
return cmem.customAlloc(cmem.opaqueState, s);
122
/* nothing defined : use default <stdlib.h>'s malloc() */
126
static void LZ4F_free(void* p, LZ4F_CustomMem cmem)
128
/* custom malloc defined : use it */
129
if (cmem.customFree != NULL) {
130
cmem.customFree(cmem.opaqueState, p);
133
/* nothing defined : use default <stdlib.h>'s free() */
100
138
/*-************************************
220
256
/*-************************************
221
257
* Structures and local types
222
258
**************************************/
260
typedef enum { LZ4B_COMPRESSED, LZ4B_UNCOMPRESSED} LZ4F_blockCompression_t;
223
262
typedef struct LZ4F_cctx_s
225
265
LZ4F_preferences_t prefs;
228
268
const LZ4F_CDict* cdict;
229
269
size_t maxBlockSize;
230
270
size_t maxBufferSize;
271
BYTE* tmpBuff; /* internal buffer, for streaming */
272
BYTE* tmpIn; /* starting position of data compress within internal buffer (>= tmpBuff) */
273
size_t tmpInSize; /* amount of data to compress after tmpIn */
235
275
XXH32_state_t xxh;
237
277
U16 lz4CtxAlloc; /* sized for: 0 = none, 1 = lz4 ctx, 2 = lz4hc ctx */
238
278
U16 lz4CtxState; /* in use as: 0 = none, 1 = lz4 ctx, 2 = lz4hc ctx */
279
LZ4F_blockCompression_t blockCompression;
264
305
return (LZ4F_errorCodes)(-(ptrdiff_t)functionResult);
267
static LZ4F_errorCode_t err0r(LZ4F_errorCodes code)
308
static LZ4F_errorCode_t LZ4F_returnErrorCode(LZ4F_errorCodes code)
269
310
/* A compilation error here means sizeof(ptrdiff_t) is not large enough */
270
311
LZ4F_STATIC_ASSERT(sizeof(ptrdiff_t) >= sizeof(size_t));
271
312
return (LZ4F_errorCode_t)-(ptrdiff_t)code;
315
#define RETURN_ERROR(e) return LZ4F_returnErrorCode(LZ4F_ERROR_ ## e)
317
#define RETURN_ERROR_IF(c,e) if (c) RETURN_ERROR(e)
319
#define FORWARD_IF_ERROR(r) if (LZ4F_isError(r)) return (r)
274
321
unsigned LZ4F_getVersion(void) { return LZ4F_VERSION; }
276
323
int LZ4F_compressionLevel_max(void) { return LZ4HC_CLEVEL_MAX; }
278
size_t LZ4F_getBlockSize(unsigned blockSizeID)
325
size_t LZ4F_getBlockSize(LZ4F_blockSizeID_t blockSizeID)
280
327
static const size_t blockSizes[4] = { 64 KB, 256 KB, 1 MB, 4 MB };
282
329
if (blockSizeID == 0) blockSizeID = LZ4F_BLOCKSIZEID_DEFAULT;
283
330
if (blockSizeID < LZ4F_max64KB || blockSizeID > LZ4F_max4MB)
284
return err0r(LZ4F_ERROR_maxBlockSize_invalid);
285
blockSizeID -= LZ4F_max64KB;
286
return blockSizes[blockSizeID];
331
RETURN_ERROR(maxBlockSize_invalid);
332
{ int const blockSizeIdx = (int)blockSizeID - (int)LZ4F_max64KB;
333
return blockSizes[blockSizeIdx];
289
336
/*-************************************
290
337
* Private functions
397
444
MEM_INIT(&options, 0, sizeof(options));
398
445
options.stableSrc = 1;
400
if (dstCapacity < LZ4F_compressFrameBound(srcSize, &prefs)) /* condition to guarantee success */
401
return err0r(LZ4F_ERROR_dstMaxSize_tooSmall);
447
RETURN_ERROR_IF(dstCapacity < LZ4F_compressFrameBound(srcSize, &prefs), dstMaxSize_tooSmall);
403
449
{ size_t const headerSize = LZ4F_compressBegin_usingCDict(cctx, dstBuffer, dstCapacity, cdict, &prefs); /* write header */
404
if (LZ4F_isError(headerSize)) return headerSize;
450
FORWARD_IF_ERROR(headerSize);
405
451
dstPtr += headerSize; /* header size */ }
407
453
assert(dstEnd >= dstPtr);
408
454
{ size_t const cSize = LZ4F_compressUpdate(cctx, dstPtr, (size_t)(dstEnd-dstPtr), srcBuffer, srcSize, &options);
409
if (LZ4F_isError(cSize)) return cSize;
455
FORWARD_IF_ERROR(cSize);
410
456
dstPtr += cSize; }
412
458
assert(dstEnd >= dstPtr);
413
459
{ 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;
460
FORWARD_IF_ERROR(tailSize);
415
461
dstPtr += tailSize; }
417
463
assert(dstEnd >= dstStart);
434
480
#if (LZ4F_HEAPMODE)
435
LZ4F_cctx_t *cctxPtr;
481
LZ4F_cctx_t* cctxPtr;
436
482
result = LZ4F_createCompressionContext(&cctxPtr, LZ4F_VERSION);
437
if (LZ4F_isError(result)) return result;
483
FORWARD_IF_ERROR(result);
439
485
LZ4F_cctx_t cctx;
440
486
LZ4_stream_t lz4ctx;
441
LZ4F_cctx_t *cctxPtr = &cctx;
487
LZ4F_cctx_t* const cctxPtr = &cctx;
443
DEBUGLOG(4, "LZ4F_compressFrame");
444
489
MEM_INIT(&cctx, 0, sizeof(cctx));
445
490
cctx.version = LZ4F_VERSION;
446
491
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)
492
if ( preferencesPtr == NULL
493
|| preferencesPtr->compressionLevel < LZ4HC_CLEVEL_MIN ) {
450
494
LZ4_initStream(&lz4ctx, sizeof(lz4ctx));
451
495
cctxPtr->lz4CtxPtr = &lz4ctx;
452
496
cctxPtr->lz4CtxAlloc = 1;
453
497
cctxPtr->lz4CtxState = 1;
500
DEBUGLOG(4, "LZ4F_compressFrame");
457
502
result = LZ4F_compressFrame_usingCDict(cctxPtr, dstBuffer, dstCapacity,
458
503
srcBuffer, srcSize,
476
520
*****************************************************/
478
522
struct LZ4F_CDict_s {
479
524
void* dictContent;
480
525
LZ4_stream_t* fastCtx;
481
526
LZ4_streamHC_t* HCCtx;
482
527
}; /* typedef'd to LZ4F_CDict within lz4frame_static.h */
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 */
490
LZ4F_CDict* LZ4F_createCDict(const void* dictBuffer, size_t dictSize)
530
LZ4F_createCDict_advanced(LZ4F_CustomMem cmem, const void* dictBuffer, size_t dictSize)
492
532
const char* dictStart = (const char*)dictBuffer;
493
LZ4F_CDict* cdict = (LZ4F_CDict*) ALLOC(sizeof(*cdict));
494
DEBUGLOG(4, "LZ4F_createCDict");
533
LZ4F_CDict* const cdict = (LZ4F_CDict*)LZ4F_malloc(sizeof(*cdict), cmem);
534
DEBUGLOG(4, "LZ4F_createCDict_advanced");
495
535
if (!cdict) return NULL;
496
537
if (dictSize > 64 KB) {
497
538
dictStart += dictSize - 64 KB;
498
539
dictSize = 64 KB;
500
cdict->dictContent = ALLOC(dictSize);
501
cdict->fastCtx = LZ4_createStream();
502
cdict->HCCtx = LZ4_createStreamHC();
541
cdict->dictContent = LZ4F_malloc(dictSize, cmem);
542
cdict->fastCtx = (LZ4_stream_t*)LZ4F_malloc(sizeof(LZ4_stream_t), cmem);
544
LZ4_initStream(cdict->fastCtx, sizeof(LZ4_stream_t));
545
cdict->HCCtx = (LZ4_streamHC_t*)LZ4F_malloc(sizeof(LZ4_streamHC_t), cmem);
547
LZ4_initStream(cdict->HCCtx, sizeof(LZ4_streamHC_t));
503
548
if (!cdict->dictContent || !cdict->fastCtx || !cdict->HCCtx) {
504
549
LZ4F_freeCDict(cdict);
559
/*! LZ4F_createCDict() :
560
* When compressing multiple messages / blocks with the same dictionary, it's recommended to load it just once.
561
* LZ4F_createCDict() will create a digested dictionary, ready to start future compression operations without startup delay.
562
* LZ4F_CDict can be created once and shared by multiple threads concurrently, since its usage is read-only.
563
* @dictBuffer can be released after LZ4F_CDict creation, since its content is copied within CDict
564
* @return : digested dictionary for compression, or NULL if failed */
565
LZ4F_CDict* LZ4F_createCDict(const void* dictBuffer, size_t dictSize)
567
DEBUGLOG(4, "LZ4F_createCDict");
568
return LZ4F_createCDict_advanced(LZ4F_defaultCMem, dictBuffer, dictSize);
514
571
void LZ4F_freeCDict(LZ4F_CDict* cdict)
516
573
if (cdict==NULL) return; /* support free on NULL */
517
FREEMEM(cdict->dictContent);
518
LZ4_freeStream(cdict->fastCtx);
519
LZ4_freeStreamHC(cdict->HCCtx);
574
LZ4F_free(cdict->dictContent, cdict->cmem);
575
LZ4F_free(cdict->fastCtx, cdict->cmem);
576
LZ4F_free(cdict->HCCtx, cdict->cmem);
577
LZ4F_free(cdict, cdict->cmem);
525
582
* Advanced compression functions
526
583
***********************************/
586
LZ4F_createCompressionContext_advanced(LZ4F_CustomMem customMem, unsigned version)
588
LZ4F_cctx* const cctxPtr =
589
(LZ4F_cctx*)LZ4F_calloc(sizeof(LZ4F_cctx), customMem);
590
if (cctxPtr==NULL) return NULL;
592
cctxPtr->cmem = customMem;
593
cctxPtr->version = version;
594
cctxPtr->cStage = 0; /* Uninitialized. Next stage : init cctx */
528
599
/*! LZ4F_createCompressionContext() :
529
600
* The first thing to do is to create a compressionContext object, which will be used in all compression operations.
530
601
* This is achieved using LZ4F_createCompressionContext(), which takes as argument a version and an LZ4F_preferences_t structure.
532
603
* The function will provide a pointer to an allocated LZ4F_compressionContext_t object.
533
604
* If the result LZ4F_errorCode_t is not OK_NoError, there was an error during context creation.
534
605
* Object can release its memory using LZ4F_freeCompressionContext();
536
LZ4F_errorCode_t LZ4F_createCompressionContext(LZ4F_cctx** LZ4F_compressionContextPtr, unsigned version)
608
LZ4F_createCompressionContext(LZ4F_cctx** LZ4F_compressionContextPtr, unsigned version)
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);
541
cctxPtr->version = version;
542
cctxPtr->cStage = 0; /* Next stage : init stream */
544
*LZ4F_compressionContextPtr = cctxPtr;
610
assert(LZ4F_compressionContextPtr != NULL); /* considered a violation of narrow contract */
611
/* in case it nonetheless happen in production */
612
RETURN_ERROR_IF(LZ4F_compressionContextPtr == NULL, parameter_null);
614
*LZ4F_compressionContextPtr = LZ4F_createCompressionContext_advanced(LZ4F_defaultCMem, version);
615
RETURN_ERROR_IF(*LZ4F_compressionContextPtr==NULL, allocation_failed);
546
616
return LZ4F_OK_NoError;
660
static int ctxTypeID_to_size(int ctxTypeID) {
663
return LZ4_sizeofState();
665
return LZ4_sizeofStateHC();
592
671
/*! LZ4F_compressBegin_usingCDict() :
593
* init streaming compression and writes frame header into dstBuffer.
594
* dstBuffer must be >= LZ4F_HEADER_SIZE_MAX bytes.
595
* @return : number of bytes written into dstBuffer for the header
672
* init streaming compression AND writes frame header into @dstBuffer.
673
* @dstCapacity must be >= LZ4F_HEADER_SIZE_MAX bytes.
674
* @return : number of bytes written into @dstBuffer for the header
596
675
* or an error code (can be tested using LZ4F_isError())
598
677
size_t LZ4F_compressBegin_usingCDict(LZ4F_cctx* cctxPtr,
600
679
const LZ4F_CDict* cdict,
601
680
const LZ4F_preferences_t* preferencesPtr)
603
LZ4F_preferences_t prefNull;
682
LZ4F_preferences_t const prefNull = LZ4F_INIT_PREFERENCES;
604
683
BYTE* const dstStart = (BYTE*)dstBuffer;
605
684
BYTE* dstPtr = dstStart;
608
if (dstCapacity < maxFHSize) return err0r(LZ4F_ERROR_dstMaxSize_tooSmall);
609
MEM_INIT(&prefNull, 0, sizeof(prefNull));
686
RETURN_ERROR_IF(dstCapacity < maxFHSize, dstMaxSize_tooSmall);
610
687
if (preferencesPtr == NULL) preferencesPtr = &prefNull;
611
688
cctxPtr->prefs = *preferencesPtr;
690
/* cctx Management */
614
691
{ U16 const ctxTypeID = (cctxPtr->prefs.compressionLevel < LZ4HC_CLEVEL_MIN) ? 1 : 2;
615
if (cctxPtr->lz4CtxAlloc < ctxTypeID) {
616
FREEMEM(cctxPtr->lz4CtxPtr);
692
int requiredSize = ctxTypeID_to_size(ctxTypeID);
693
int allocatedSize = ctxTypeID_to_size(cctxPtr->lz4CtxAlloc);
694
if (allocatedSize < requiredSize) {
695
/* not enough space allocated */
696
LZ4F_free(cctxPtr->lz4CtxPtr, cctxPtr->cmem);
617
697
if (cctxPtr->prefs.compressionLevel < LZ4HC_CLEVEL_MIN) {
618
cctxPtr->lz4CtxPtr = LZ4_createStream();
698
/* must take ownership of memory allocation,
699
* in order to respect custom allocator contract */
700
cctxPtr->lz4CtxPtr = LZ4F_malloc(sizeof(LZ4_stream_t), cctxPtr->cmem);
701
if (cctxPtr->lz4CtxPtr)
702
LZ4_initStream(cctxPtr->lz4CtxPtr, sizeof(LZ4_stream_t));
620
cctxPtr->lz4CtxPtr = LZ4_createStreamHC();
704
cctxPtr->lz4CtxPtr = LZ4F_malloc(sizeof(LZ4_streamHC_t), cctxPtr->cmem);
705
if (cctxPtr->lz4CtxPtr)
706
LZ4_initStreamHC(cctxPtr->lz4CtxPtr, sizeof(LZ4_streamHC_t));
622
if (cctxPtr->lz4CtxPtr == NULL)
623
return err0r(LZ4F_ERROR_allocation_failed);
708
RETURN_ERROR_IF(cctxPtr->lz4CtxPtr == NULL, allocation_failed);
624
709
cctxPtr->lz4CtxAlloc = ctxTypeID;
625
710
cctxPtr->lz4CtxState = ctxTypeID;
626
711
} else if (cctxPtr->lz4CtxState != ctxTypeID) {
627
/* otherwise, a sufficient buffer is allocated, but we need to
628
* reset it to the correct context type */
712
/* otherwise, a sufficient buffer is already allocated,
713
* but we need to reset it to the correct context type */
629
714
if (cctxPtr->prefs.compressionLevel < LZ4HC_CLEVEL_MIN) {
630
LZ4_initStream((LZ4_stream_t *) cctxPtr->lz4CtxPtr, sizeof (LZ4_stream_t));
715
LZ4_initStream((LZ4_stream_t*)cctxPtr->lz4CtxPtr, sizeof(LZ4_stream_t));
632
LZ4_initStreamHC((LZ4_streamHC_t *) cctxPtr->lz4CtxPtr, sizeof(LZ4_streamHC_t));
633
LZ4_setCompressionLevel((LZ4_streamHC_t *) cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel);
717
LZ4_initStreamHC((LZ4_streamHC_t*)cctxPtr->lz4CtxPtr, sizeof(LZ4_streamHC_t));
718
LZ4_setCompressionLevel((LZ4_streamHC_t*)cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel);
635
720
cctxPtr->lz4CtxState = ctxTypeID;
639
723
/* Buffer Management */
640
724
if (cctxPtr->prefs.frameInfo.blockSizeID == 0)
648
732
if (cctxPtr->maxBufferSize < requiredBuffSize) {
649
733
cctxPtr->maxBufferSize = 0;
650
FREEMEM(cctxPtr->tmpBuff);
651
cctxPtr->tmpBuff = (BYTE*)ALLOC_AND_ZERO(requiredBuffSize);
652
if (cctxPtr->tmpBuff == NULL) return err0r(LZ4F_ERROR_allocation_failed);
734
LZ4F_free(cctxPtr->tmpBuff, cctxPtr->cmem);
735
cctxPtr->tmpBuff = (BYTE*)LZ4F_calloc(requiredBuffSize, cctxPtr->cmem);
736
RETURN_ERROR_IF(cctxPtr->tmpBuff == NULL, allocation_failed);
653
737
cctxPtr->maxBufferSize = requiredBuffSize;
655
739
cctxPtr->tmpIn = cctxPtr->tmpBuff;
669
753
/* Magic Number */
670
754
LZ4F_writeLE32(dstPtr, LZ4F_MAGICNUMBER);
672
headerStart = dstPtr;
756
{ BYTE* const headerStart = dstPtr;
675
*dstPtr++ = (BYTE)(((1 & _2BITS) << 6) /* Version('01') */
676
+ ((cctxPtr->prefs.frameInfo.blockMode & _1BIT ) << 5)
677
+ ((cctxPtr->prefs.frameInfo.blockChecksumFlag & _1BIT ) << 4)
678
+ ((unsigned)(cctxPtr->prefs.frameInfo.contentSize > 0) << 3)
679
+ ((cctxPtr->prefs.frameInfo.contentChecksumFlag & _1BIT ) << 2)
680
+ (cctxPtr->prefs.frameInfo.dictID > 0) );
682
*dstPtr++ = (BYTE)((cctxPtr->prefs.frameInfo.blockSizeID & _3BITS) << 4);
683
/* Optional Frame content size field */
684
if (cctxPtr->prefs.frameInfo.contentSize) {
685
LZ4F_writeLE64(dstPtr, cctxPtr->prefs.frameInfo.contentSize);
687
cctxPtr->totalInSize = 0;
689
/* Optional dictionary ID field */
690
if (cctxPtr->prefs.frameInfo.dictID) {
691
LZ4F_writeLE32(dstPtr, cctxPtr->prefs.frameInfo.dictID);
694
/* Header CRC Byte */
695
*dstPtr = LZ4F_headerChecksum(headerStart, (size_t)(dstPtr - headerStart));
759
*dstPtr++ = (BYTE)(((1 & _2BITS) << 6) /* Version('01') */
760
+ ((cctxPtr->prefs.frameInfo.blockMode & _1BIT ) << 5)
761
+ ((cctxPtr->prefs.frameInfo.blockChecksumFlag & _1BIT ) << 4)
762
+ ((unsigned)(cctxPtr->prefs.frameInfo.contentSize > 0) << 3)
763
+ ((cctxPtr->prefs.frameInfo.contentChecksumFlag & _1BIT ) << 2)
764
+ (cctxPtr->prefs.frameInfo.dictID > 0) );
766
*dstPtr++ = (BYTE)((cctxPtr->prefs.frameInfo.blockSizeID & _3BITS) << 4);
767
/* Optional Frame content size field */
768
if (cctxPtr->prefs.frameInfo.contentSize) {
769
LZ4F_writeLE64(dstPtr, cctxPtr->prefs.frameInfo.contentSize);
771
cctxPtr->totalInSize = 0;
773
/* Optional dictionary ID field */
774
if (cctxPtr->prefs.frameInfo.dictID) {
775
LZ4F_writeLE32(dstPtr, cctxPtr->prefs.frameInfo.dictID);
778
/* Header CRC Byte */
779
*dstPtr = LZ4F_headerChecksum(headerStart, (size_t)(dstPtr - headerStart));
698
783
cctxPtr->cStage = 1; /* header written, now request input data block */
699
784
return (size_t)(dstPtr - dstStart);
744
829
LZ4F_blockChecksum_t crcFlag)
746
831
BYTE* const cSizePtr = (BYTE*)dst;
747
U32 cSize = (U32)compress(lz4ctx, (const char*)src, (char*)(cSizePtr+BHSize),
748
(int)(srcSize), (int)(srcSize-1),
750
if (cSize == 0) { /* compression failed */
751
DEBUGLOG(5, "LZ4F_makeBlock: compression failed, creating a raw block (size %u)", (U32)srcSize);
833
assert(compress != NULL);
834
cSize = (U32)compress(lz4ctx, (const char*)src, (char*)(cSizePtr+BHSize),
835
(int)(srcSize), (int)(srcSize-1),
838
if (cSize == 0 || cSize >= srcSize) {
752
839
cSize = (U32)srcSize;
753
840
LZ4F_writeLE32(cSizePtr, cSize | LZ4F_BLOCKUNCOMPRESSED_FLAG);
754
841
memcpy(cSizePtr+BHSize, src, srcSize);
766
853
static int LZ4F_compressBlock(void* ctx, const char* src, char* dst, int srcSize, int dstCapacity, int level, const LZ4F_CDict* cdict)
768
855
int const acceleration = (level < 0) ? -level + 1 : 1;
856
DEBUGLOG(5, "LZ4F_compressBlock (srcSize=%i)", srcSize);
769
857
LZ4F_initStream(ctx, cdict, level, LZ4F_blockIndependent);
771
859
return LZ4_compress_fast_continue((LZ4_stream_t*)ctx, src, dst, srcSize, dstCapacity, acceleration);
796
885
return LZ4_compress_HC_continue((LZ4_streamHC_t*)ctx, src, dst, srcSize, dstCapacity);
799
static compressFunc_t LZ4F_selectCompression(LZ4F_blockMode_t blockMode, int level)
888
static int LZ4F_doNotCompressBlock(void* ctx, const char* src, char* dst, int srcSize, int dstCapacity, int level, const LZ4F_CDict* cdict)
890
(void)ctx; (void)src; (void)dst; (void)srcSize; (void)dstCapacity; (void)level; (void)cdict;
894
static compressFunc_t LZ4F_selectCompression(LZ4F_blockMode_t blockMode, int level, LZ4F_blockCompression_t compressMode)
896
if (compressMode == LZ4B_UNCOMPRESSED) return LZ4F_doNotCompressBlock;
801
897
if (level < LZ4HC_CLEVEL_MIN) {
802
898
if (blockMode == LZ4F_blockIndependent) return LZ4F_compressBlock;
803
899
return LZ4F_compressBlock_continue;
816
913
typedef enum { notDone, fromTmpBuffer, fromSrcBuffer } LZ4F_lastBlockStatus;
818
/*! LZ4F_compressUpdate() :
915
static const LZ4F_compressOptions_t k_cOptionsNull = { 0, { 0, 0, 0 } };
918
/*! LZ4F_compressUpdateImpl() :
819
919
* LZ4F_compressUpdate() can be called repetitively to compress as much data as necessary.
820
* dstBuffer MUST be >= LZ4F_compressBound(srcSize, preferencesPtr).
821
* LZ4F_compressOptions_t structure is optional : you can provide NULL as argument.
920
* When successful, the function always entirely consumes @srcBuffer.
921
* src data is either buffered or compressed into @dstBuffer.
922
* If the block compression does not match the compression of the previous block, the old data is flushed
923
* and operations continue with the new compression mode.
924
* @dstCapacity MUST be >= LZ4F_compressBound(srcSize, preferencesPtr) when block compression is turned on.
925
* @compressOptionsPtr is optional : provide NULL to mean "default".
822
926
* @return : the number of bytes written into dstBuffer. It can be zero, meaning input data was just buffered.
823
927
* or an error code if it fails (which can be tested using LZ4F_isError())
928
* After an error, the state is left in a UB state, and must be re-initialized.
825
size_t LZ4F_compressUpdate(LZ4F_cctx* cctxPtr,
826
void* dstBuffer, size_t dstCapacity,
930
static size_t LZ4F_compressUpdateImpl(LZ4F_cctx* cctxPtr,
931
void* dstBuffer, size_t dstCapacity,
827
932
const void* srcBuffer, size_t srcSize,
828
const LZ4F_compressOptions_t* compressOptionsPtr)
830
LZ4F_compressOptions_t cOptionsNull;
933
const LZ4F_compressOptions_t* compressOptionsPtr,
934
LZ4F_blockCompression_t blockCompression)
831
936
size_t const blockSize = cctxPtr->maxBlockSize;
832
937
const BYTE* srcPtr = (const BYTE*)srcBuffer;
833
938
const BYTE* const srcEnd = srcPtr + srcSize;
834
939
BYTE* const dstStart = (BYTE*)dstBuffer;
835
940
BYTE* dstPtr = dstStart;
836
941
LZ4F_lastBlockStatus lastBlockCompressed = notDone;
837
compressFunc_t const compress = LZ4F_selectCompression(cctxPtr->prefs.frameInfo.blockMode, cctxPtr->prefs.compressionLevel);
942
compressFunc_t const compress = LZ4F_selectCompression(cctxPtr->prefs.frameInfo.blockMode, cctxPtr->prefs.compressionLevel, blockCompression);
839
944
DEBUGLOG(4, "LZ4F_compressUpdate (srcSize=%zu)", srcSize);
841
if (cctxPtr->cStage != 1) return err0r(LZ4F_ERROR_GENERIC);
946
RETURN_ERROR_IF(cctxPtr->cStage != 1, compressionState_uninitialized); /* state must be initialized and waiting for next block */
842
947
if (dstCapacity < LZ4F_compressBound_internal(srcSize, &(cctxPtr->prefs), cctxPtr->tmpInSize))
843
return err0r(LZ4F_ERROR_dstMaxSize_tooSmall);
844
MEM_INIT(&cOptionsNull, 0, sizeof(cOptionsNull));
845
if (compressOptionsPtr == NULL) compressOptionsPtr = &cOptionsNull;
948
RETURN_ERROR(dstMaxSize_tooSmall);
950
if (blockCompression == LZ4B_UNCOMPRESSED && dstCapacity < srcSize)
951
RETURN_ERROR(dstMaxSize_tooSmall);
953
/* flush currently written block, to continue with new block compression */
954
if (cctxPtr->blockCompression != blockCompression) {
955
bytesWritten = LZ4F_flush(cctxPtr, dstBuffer, dstCapacity, compressOptionsPtr);
956
dstPtr += bytesWritten;
957
cctxPtr->blockCompression = blockCompression;
960
if (compressOptionsPtr == NULL) compressOptionsPtr = &k_cOptionsNull;
847
962
/* complete tmp buffer */
848
963
if (cctxPtr->tmpInSize > 0) { /* some data already within tmp buffer */
849
964
size_t const sizeToCopy = blockSize - cctxPtr->tmpInSize;
965
assert(blockSize > cctxPtr->tmpInSize);
850
966
if (sizeToCopy > srcSize) {
851
967
/* add src to tmpIn buffer */
852
968
memcpy(cctxPtr->tmpIn + cctxPtr->tmpInSize, srcBuffer, srcSize);
884
998
if ((cctxPtr->prefs.autoFlush) && (srcPtr < srcEnd)) {
885
/* compress remaining input < blockSize */
999
/* autoFlush : remaining input (< blockSize) is compressed */
886
1000
lastBlockCompressed = fromSrcBuffer;
887
1001
dstPtr += LZ4F_makeBlock(dstPtr,
888
1002
srcPtr, (size_t)(srcEnd - srcPtr),
889
1003
compress, cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel,
891
1005
cctxPtr->prefs.frameInfo.blockChecksumFlag);
895
/* preserve dictionary if necessary */
1009
/* preserve dictionary within @tmpBuff whenever necessary */
896
1010
if ((cctxPtr->prefs.frameInfo.blockMode==LZ4F_blockLinked) && (lastBlockCompressed==fromSrcBuffer)) {
1011
/* linked blocks are only supported in compressed mode, see LZ4F_uncompressedUpdate */
1012
assert(blockCompression == LZ4B_COMPRESSED);
897
1013
if (compressOptionsPtr->stableSrc) {
898
cctxPtr->tmpIn = cctxPtr->tmpBuff;
1014
cctxPtr->tmpIn = cctxPtr->tmpBuff; /* src is stable : dictionary remains in src across invocations */
900
1016
int const realDictSize = LZ4F_localSaveDict(cctxPtr);
901
if (realDictSize==0) return err0r(LZ4F_ERROR_GENERIC);
1017
assert(0 <= realDictSize && realDictSize <= 64 KB);
902
1018
cctxPtr->tmpIn = cctxPtr->tmpBuff + realDictSize;
906
1022
/* keep tmpIn within limits */
907
if ((cctxPtr->tmpIn + blockSize) > (cctxPtr->tmpBuff + cctxPtr->maxBufferSize) /* necessarily LZ4F_blockLinked && lastBlockCompressed==fromTmpBuffer */
908
&& !(cctxPtr->prefs.autoFlush))
1023
if (!(cctxPtr->prefs.autoFlush) /* no autoflush : there may be some data left within internal buffer */
1024
&& (cctxPtr->tmpIn + blockSize) > (cctxPtr->tmpBuff + cctxPtr->maxBufferSize) ) /* not enough room to store next block */
1026
/* only preserve 64KB within internal buffer. Ensures there is enough room for next block.
1027
* note: this situation necessarily implies lastBlockCompressed==fromTmpBuffer */
910
1028
int const realDictSize = LZ4F_localSaveDict(cctxPtr);
911
1029
cctxPtr->tmpIn = cctxPtr->tmpBuff + realDictSize;
1030
assert((cctxPtr->tmpIn + blockSize) <= (cctxPtr->tmpBuff + cctxPtr->maxBufferSize));
914
1033
/* some input data left, necessarily < blockSize */
926
1045
return (size_t)(dstPtr - dstStart);
1048
/*! LZ4F_compressUpdate() :
1049
* LZ4F_compressUpdate() can be called repetitively to compress as much data as necessary.
1050
* When successful, the function always entirely consumes @srcBuffer.
1051
* src data is either buffered or compressed into @dstBuffer.
1052
* If previously an uncompressed block was written, buffered data is flushed
1053
* before appending compressed data is continued.
1054
* @dstCapacity MUST be >= LZ4F_compressBound(srcSize, preferencesPtr).
1055
* @compressOptionsPtr is optional : provide NULL to mean "default".
1056
* @return : the number of bytes written into dstBuffer. It can be zero, meaning input data was just buffered.
1057
* or an error code if it fails (which can be tested using LZ4F_isError())
1058
* After an error, the state is left in a UB state, and must be re-initialized.
1060
size_t LZ4F_compressUpdate(LZ4F_cctx* cctxPtr,
1061
void* dstBuffer, size_t dstCapacity,
1062
const void* srcBuffer, size_t srcSize,
1063
const LZ4F_compressOptions_t* compressOptionsPtr)
1065
return LZ4F_compressUpdateImpl(cctxPtr,
1066
dstBuffer, dstCapacity,
1068
compressOptionsPtr, LZ4B_COMPRESSED);
1071
/*! LZ4F_compressUpdate() :
1072
* LZ4F_compressUpdate() can be called repetitively to compress as much data as necessary.
1073
* When successful, the function always entirely consumes @srcBuffer.
1074
* src data is either buffered or compressed into @dstBuffer.
1075
* If previously an uncompressed block was written, buffered data is flushed
1076
* before appending compressed data is continued.
1077
* This is only supported when LZ4F_blockIndependent is used
1078
* @dstCapacity MUST be >= LZ4F_compressBound(srcSize, preferencesPtr).
1079
* @compressOptionsPtr is optional : provide NULL to mean "default".
1080
* @return : the number of bytes written into dstBuffer. It can be zero, meaning input data was just buffered.
1081
* or an error code if it fails (which can be tested using LZ4F_isError())
1082
* After an error, the state is left in a UB state, and must be re-initialized.
1084
size_t LZ4F_uncompressedUpdate(LZ4F_cctx* cctxPtr,
1085
void* dstBuffer, size_t dstCapacity,
1086
const void* srcBuffer, size_t srcSize,
1087
const LZ4F_compressOptions_t* compressOptionsPtr) {
1088
RETURN_ERROR_IF(cctxPtr->prefs.frameInfo.blockMode != LZ4F_blockIndependent, blockMode_invalid);
1089
return LZ4F_compressUpdateImpl(cctxPtr,
1090
dstBuffer, dstCapacity,
1092
compressOptionsPtr, LZ4B_UNCOMPRESSED);
930
1096
/*! LZ4F_flush() :
931
1097
* When compressed data must be sent immediately, without waiting for a block to be filled,
944
1110
compressFunc_t compress;
946
1112
if (cctxPtr->tmpInSize == 0) return 0; /* nothing to flush */
947
if (cctxPtr->cStage != 1) return err0r(LZ4F_ERROR_GENERIC);
948
if (dstCapacity < (cctxPtr->tmpInSize + BHSize + BFSize))
949
return err0r(LZ4F_ERROR_dstMaxSize_tooSmall);
950
(void)compressOptionsPtr; /* not yet useful */
1113
RETURN_ERROR_IF(cctxPtr->cStage != 1, compressionState_uninitialized);
1114
RETURN_ERROR_IF(dstCapacity < (cctxPtr->tmpInSize + BHSize + BFSize), dstMaxSize_tooSmall);
1115
(void)compressOptionsPtr; /* not useful (yet) */
952
1117
/* select compression function */
953
compress = LZ4F_selectCompression(cctxPtr->prefs.frameInfo.blockMode, cctxPtr->prefs.compressionLevel);
1118
compress = LZ4F_selectCompression(cctxPtr->prefs.frameInfo.blockMode, cctxPtr->prefs.compressionLevel, cctxPtr->blockCompression);
955
1120
/* compress tmp buffer */
956
1121
dstPtr += LZ4F_makeBlock(dstPtr,
993
1158
size_t const flushSize = LZ4F_flush(cctxPtr, dstBuffer, dstCapacity, compressOptionsPtr);
994
1159
DEBUGLOG(5,"LZ4F_compressEnd: dstCapacity=%u", (unsigned)dstCapacity);
995
if (LZ4F_isError(flushSize)) return flushSize;
1160
FORWARD_IF_ERROR(flushSize);
996
1161
dstPtr += flushSize;
998
1163
assert(flushSize <= dstCapacity);
999
1164
dstCapacity -= flushSize;
1001
if (dstCapacity < 4) return err0r(LZ4F_ERROR_dstMaxSize_tooSmall);
1166
RETURN_ERROR_IF(dstCapacity < 4, dstMaxSize_tooSmall);
1002
1167
LZ4F_writeLE32(dstPtr, 0);
1003
1168
dstPtr += 4; /* endMark */
1005
1170
if (cctxPtr->prefs.frameInfo.contentChecksumFlag == LZ4F_contentChecksumEnabled) {
1006
1171
U32 const xxh = XXH32_digest(&(cctxPtr->xxh));
1007
if (dstCapacity < 8) return err0r(LZ4F_ERROR_dstMaxSize_tooSmall);
1172
RETURN_ERROR_IF(dstCapacity < 8, dstMaxSize_tooSmall);
1008
1173
DEBUGLOG(5,"Writing 32-bit content checksum");
1009
1174
LZ4F_writeLE32(dstPtr, xxh);
1010
1175
dstPtr+=4; /* content Checksum */
1056
1222
size_t tmpOutStart;
1057
1223
XXH32_state_t xxh;
1058
1224
XXH32_state_t blockChecksum;
1059
1226
BYTE header[LZ4F_HEADER_SIZE_MAX];
1060
1227
}; /* typedef'd to LZ4F_dctx in lz4frame.h */
1230
LZ4F_dctx* LZ4F_createDecompressionContext_advanced(LZ4F_CustomMem customMem, unsigned version)
1232
LZ4F_dctx* const dctx = (LZ4F_dctx*)LZ4F_calloc(sizeof(LZ4F_dctx), customMem);
1233
if (dctx == NULL) return NULL;
1235
dctx->cmem = customMem;
1236
dctx->version = version;
1063
1240
/*! LZ4F_createDecompressionContext() :
1064
1241
* Create a decompressionContext object, which will track all decompression operations.
1065
1242
* Provides a pointer to a fully allocated and initialized LZ4F_decompressionContext object.
1066
1243
* Object can later be released using LZ4F_freeDecompressionContext().
1067
1244
* @return : if != 0, there was an error during context creation.
1069
LZ4F_errorCode_t LZ4F_createDecompressionContext(LZ4F_dctx** LZ4F_decompressionContextPtr, unsigned versionNumber)
1247
LZ4F_createDecompressionContext(LZ4F_dctx** LZ4F_decompressionContextPtr, unsigned versionNumber)
1071
LZ4F_dctx* const dctx = (LZ4F_dctx*)ALLOC_AND_ZERO(sizeof(LZ4F_dctx));
1072
if (dctx == NULL) { /* failed allocation */
1073
*LZ4F_decompressionContextPtr = NULL;
1074
return err0r(LZ4F_ERROR_allocation_failed);
1249
assert(LZ4F_decompressionContextPtr != NULL); /* violation of narrow contract */
1250
RETURN_ERROR_IF(LZ4F_decompressionContextPtr == NULL, parameter_null); /* in case it nonetheless happen in production */
1252
*LZ4F_decompressionContextPtr = LZ4F_createDecompressionContext_advanced(LZ4F_defaultCMem, versionNumber);
1253
if (*LZ4F_decompressionContextPtr == NULL) { /* failed allocation */
1254
RETURN_ERROR(allocation_failed);
1077
dctx->version = versionNumber;
1078
*LZ4F_decompressionContextPtr = dctx;
1079
1256
return LZ4F_OK_NoError;
1173
1350
{ U32 const BD = srcPtr[5];
1174
1351
blockSizeID = (BD>>4) & _3BITS;
1176
if (((BD>>7)&_1BIT) != 0) return err0r(LZ4F_ERROR_reservedFlag_set); /* Reserved bit */
1177
if (blockSizeID < 4) return err0r(LZ4F_ERROR_maxBlockSize_invalid); /* 4-7 only supported values for the time being */
1178
if (((BD>>0)&_4BITS) != 0) return err0r(LZ4F_ERROR_reservedFlag_set); /* Reserved bits */
1353
if (((BD>>7)&_1BIT) != 0) RETURN_ERROR(reservedFlag_set); /* Reserved bit */
1354
if (blockSizeID < 4) RETURN_ERROR(maxBlockSize_invalid); /* 4-7 only supported values for the time being */
1355
if (((BD>>0)&_4BITS) != 0) RETURN_ERROR(reservedFlag_set); /* Reserved bits */
1181
1358
/* check header */
1182
1359
assert(frameHeaderSize > 5);
1183
1360
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
1184
1361
{ BYTE const HC = LZ4F_headerChecksum(srcPtr+4, frameHeaderSize-5);
1185
if (HC != srcPtr[frameHeaderSize-1])
1186
return err0r(LZ4F_ERROR_headerChecksum_invalid);
1362
RETURN_ERROR_IF(HC != srcPtr[frameHeaderSize-1], headerChecksum_invalid);
1266
1441
if (dctx->dStage == dstage_storeFrameHeader) {
1267
1442
/* frame decoding already started, in the middle of header => automatic fail */
1268
1443
*srcSizePtr = 0;
1269
return err0r(LZ4F_ERROR_frameDecoding_alreadyStarted);
1444
RETURN_ERROR(frameDecoding_alreadyStarted);
1271
1446
size_t const hSize = LZ4F_headerSize(srcBuffer, *srcSizePtr);
1272
1447
if (LZ4F_isError(hSize)) { *srcSizePtr=0; return hSize; }
1273
1448
if (*srcSizePtr < hSize) {
1275
return err0r(LZ4F_ERROR_frameHeader_incomplete);
1450
RETURN_ERROR(frameHeader_incomplete);
1278
1453
{ size_t decodeResult = LZ4F_decodeHeader(dctx, srcBuffer, hSize);
1291
1466
/* LZ4F_updateDict() :
1292
1467
* only used for LZ4F_blockLinked mode
1293
* Condition : dstPtr != NULL
1468
* Condition : @dstPtr != NULL
1295
1470
static void LZ4F_updateDict(LZ4F_dctx* dctx,
1296
1471
const BYTE* dstPtr, size_t dstSize, const BYTE* dstBufferStart,
1297
1472
unsigned withinTmp)
1299
1474
assert(dstPtr != NULL);
1300
if (dctx->dictSize==0) {
1301
dctx->dict = (const BYTE*)dstPtr; /* priority to prefix mode */
1475
if (dctx->dictSize==0) dctx->dict = (const BYTE*)dstPtr; /* will lead to prefix mode */
1303
1476
assert(dctx->dict != NULL);
1305
1478
if (dctx->dict + dctx->dictSize == dstPtr) { /* prefix mode, everything within dstBuffer */
1453
1624
+ ((dctx->frameInfo.blockMode==LZ4F_blockLinked) ? 128 KB : 0);
1454
1625
if (bufferNeeded > dctx->maxBufferSize) { /* tmp buffers too small */
1455
1626
dctx->maxBufferSize = 0; /* ensure allocation will be re-attempted on next entry*/
1456
FREEMEM(dctx->tmpIn);
1457
dctx->tmpIn = (BYTE*)ALLOC(dctx->maxBlockSize + BFSize /* block checksum */);
1458
if (dctx->tmpIn == NULL)
1459
return err0r(LZ4F_ERROR_allocation_failed);
1460
FREEMEM(dctx->tmpOutBuffer);
1461
dctx->tmpOutBuffer= (BYTE*)ALLOC(bufferNeeded);
1462
if (dctx->tmpOutBuffer== NULL)
1463
return err0r(LZ4F_ERROR_allocation_failed);
1627
LZ4F_free(dctx->tmpIn, dctx->cmem);
1628
dctx->tmpIn = (BYTE*)LZ4F_malloc(dctx->maxBlockSize + BFSize /* block checksum */, dctx->cmem);
1629
RETURN_ERROR_IF(dctx->tmpIn == NULL, allocation_failed);
1630
LZ4F_free(dctx->tmpOutBuffer, dctx->cmem);
1631
dctx->tmpOutBuffer= (BYTE*)LZ4F_malloc(bufferNeeded, dctx->cmem);
1632
RETURN_ERROR_IF(dctx->tmpOutBuffer== NULL, allocation_failed);
1464
1633
dctx->maxBufferSize = bufferNeeded;
1466
1635
dctx->tmpInSize = 0;
1540
1709
size_t const minBuffSize = MIN((size_t)(srcEnd-srcPtr), (size_t)(dstEnd-dstPtr));
1541
1710
sizeToCopy = MIN(dctx->tmpInTarget, minBuffSize);
1542
1711
memcpy(dstPtr, srcPtr, sizeToCopy);
1543
if (dctx->frameInfo.blockChecksumFlag) {
1544
(void)XXH32_update(&dctx->blockChecksum, srcPtr, sizeToCopy);
1712
if (!dctx->skipChecksum) {
1713
if (dctx->frameInfo.blockChecksumFlag) {
1714
(void)XXH32_update(&dctx->blockChecksum, srcPtr, sizeToCopy);
1716
if (dctx->frameInfo.contentChecksumFlag)
1717
(void)XXH32_update(&dctx->xxh, srcPtr, sizeToCopy);
1546
if (dctx->frameInfo.contentChecksumFlag)
1547
(void)XXH32_update(&dctx->xxh, srcPtr, sizeToCopy);
1548
1719
if (dctx->frameInfo.contentSize)
1549
1720
dctx->frameRemainingSize -= sizeToCopy;
1639
1811
/* At this stage, input is large enough to decode a block */
1813
/* First, decode and control block checksum if it exists */
1640
1814
if (dctx->frameInfo.blockChecksumFlag) {
1815
assert(dctx->tmpInTarget >= 4);
1641
1816
dctx->tmpInTarget -= 4;
1642
1817
assert(selectedIn != NULL); /* selectedIn is defined at this stage (either srcPtr, or dctx->tmpIn) */
1643
1818
{ U32 const readBlockCrc = LZ4F_readLE32(selectedIn + dctx->tmpInTarget);
1644
1819
U32 const calcBlockCrc = XXH32(selectedIn, dctx->tmpInTarget, 0);
1645
1820
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
1646
if (readBlockCrc != calcBlockCrc)
1647
return err0r(LZ4F_ERROR_blockChecksum_invalid);
1821
RETURN_ERROR_IF(readBlockCrc != calcBlockCrc, blockChecksum_invalid);
1649
1823
(void)readBlockCrc;
1650
1824
(void)calcBlockCrc;
1654
if ((size_t)(dstEnd-dstPtr) >= dctx->maxBlockSize) {
1828
/* decode directly into destination buffer if there is enough room */
1829
if ( ((size_t)(dstEnd-dstPtr) >= dctx->maxBlockSize)
1830
/* unless the dictionary is stored in tmpOut:
1831
* in which case it's faster to decode within tmpOut
1832
* to benefit from prefix speedup */
1833
&& !(dctx->dict!= NULL && (const BYTE*)dctx->dict + dctx->dictSize == dctx->tmpOut) )
1655
1835
const char* dict = (const char*)dctx->dict;
1656
1836
size_t dictSize = dctx->dictSize;
1657
1837
int decodedSize;
1658
1838
assert(dstPtr != NULL);
1659
1839
if (dict && dictSize > 1 GB) {
1660
/* the dictSize param is an int, avoid truncation / sign issues */
1840
/* overflow control : dctx->dictSize is an int, avoid truncation / sign issues */
1661
1841
dict += dictSize - 64 KB;
1662
1842
dictSize = 64 KB;
1664
/* enough capacity in `dst` to decompress directly there */
1665
1844
decodedSize = LZ4_decompress_safe_usingDict(
1666
1845
(const char*)selectedIn, (char*)dstPtr,
1667
1846
(int)dctx->tmpInTarget, (int)dctx->maxBlockSize,
1668
1847
dict, (int)dictSize);
1669
if (decodedSize < 0) return err0r(LZ4F_ERROR_GENERIC); /* decompression failed */
1670
if (dctx->frameInfo.contentChecksumFlag)
1848
RETURN_ERROR_IF(decodedSize < 0, decompressionFailed);
1849
if ((dctx->frameInfo.contentChecksumFlag) && (!dctx->skipChecksum))
1671
1850
XXH32_update(&(dctx->xxh), dstPtr, (size_t)decodedSize);
1672
1851
if (dctx->frameInfo.contentSize)
1673
1852
dctx->frameRemainingSize -= (size_t)decodedSize;
1680
1859
dstPtr += decodedSize;
1681
dctx->dStage = dstage_getBlockHeader;
1860
dctx->dStage = dstage_getBlockHeader; /* end of block, let's get another one */
1685
1864
/* not enough place into dst : decode into tmpOut */
1686
/* ensure enough place for tmpOut */
1866
/* manage dictionary */
1687
1867
if (dctx->frameInfo.blockMode == LZ4F_blockLinked) {
1688
1868
if (dctx->dict == dctx->tmpOutBuffer) {
1869
/* truncate dictionary to 64 KB if too big */
1689
1870
if (dctx->dictSize > 128 KB) {
1690
1871
memcpy(dctx->tmpOutBuffer, dctx->dict + dctx->dictSize - 64 KB, 64 KB);
1691
1872
dctx->dictSize = 64 KB;
1693
1874
dctx->tmpOut = dctx->tmpOutBuffer + dctx->dictSize;
1694
} else { /* dict not within tmp */
1875
} else { /* dict not within tmpOut */
1695
1876
size_t const reservedDictSpace = MIN(dctx->dictSize, 64 KB);
1696
1877
dctx->tmpOut = dctx->tmpOutBuffer + reservedDictSpace;
1880
/* Decode block into tmpOut */
1700
1881
{ const char* dict = (const char*)dctx->dict;
1701
1882
size_t dictSize = dctx->dictSize;
1702
1883
int decodedSize;
1709
1890
(const char*)selectedIn, (char*)dctx->tmpOut,
1710
1891
(int)dctx->tmpInTarget, (int)dctx->maxBlockSize,
1711
1892
dict, (int)dictSize);
1712
if (decodedSize < 0) /* decompression failed */
1713
return err0r(LZ4F_ERROR_decompressionFailed);
1714
if (dctx->frameInfo.contentChecksumFlag)
1893
RETURN_ERROR_IF(decodedSize < 0, decompressionFailed);
1894
if (dctx->frameInfo.contentChecksumFlag && !dctx->skipChecksum)
1715
1895
XXH32_update(&(dctx->xxh), dctx->tmpOut, (size_t)decodedSize);
1716
1896
if (dctx->frameInfo.contentSize)
1717
1897
dctx->frameRemainingSize -= (size_t)decodedSize;
1777
1956
} /* if (dctx->dStage == dstage_storeSuffix) */
1779
1958
/* case dstage_checkSuffix: */ /* no direct entry, avoid initialization risks */
1780
{ U32 const readCRC = LZ4F_readLE32(selectedIn);
1959
if (!dctx->skipChecksum) {
1960
U32 const readCRC = LZ4F_readLE32(selectedIn);
1781
1961
U32 const resultCRC = XXH32_digest(&(dctx->xxh));
1782
1962
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
1783
if (readCRC != resultCRC)
1784
return err0r(LZ4F_ERROR_contentChecksum_invalid);
1963
RETURN_ERROR_IF(readCRC != resultCRC, contentChecksum_invalid);
1787
1966
(void)resultCRC;
1789
nextSrcSizeHint = 0;
1790
LZ4F_resetDecompressionContext(dctx);
1969
nextSrcSizeHint = 0;
1970
LZ4F_resetDecompressionContext(dctx);
1795
1974
case dstage_getSFrameSize:
1796
1975
if ((srcEnd - srcPtr) >= 4) {