From 0d073d4d287b08659eada92c614bec30fa277cd0 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 17 Mar 2017 15:11:09 -0700 Subject: added `extern C` for lz4.h static section should make the file more compatible with C++ compiler, such as Visual or g++ --- lib/lz4.h | 9 ++++----- lib/lz4frame.c | 43 ++++++++++++++++++++++--------------------- lib/lz4frame.h | 25 ++++++++++++------------- lib/lz4hc.h | 2 +- tests/fuzzer.c | 2 +- 5 files changed, 40 insertions(+), 41 deletions(-) diff --git a/lib/lz4.h b/lib/lz4.h index 88d56d0..588de22 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -32,13 +32,13 @@ - LZ4 homepage : http://www.lz4.org - LZ4 source repository : https://github.com/lz4/lz4 */ -#ifndef LZ4_H_2983827168210 -#define LZ4_H_2983827168210 - #if defined (__cplusplus) extern "C" { #endif +#ifndef LZ4_H_2983827168210 +#define LZ4_H_2983827168210 + /* --- Dependency --- */ #include /* size_t */ @@ -456,9 +456,8 @@ LZ4LIB_API LZ4_DEPRECATED("use LZ4_saveDict() instead") char* LZ4_slideInput LZ4LIB_API LZ4_DEPRECATED("use LZ4_decompress_safe_usingDict() instead") int LZ4_decompress_safe_withPrefix64k (const char* src, char* dst, int compressedSize, int maxDstSize); LZ4LIB_API LZ4_DEPRECATED("use LZ4_decompress_fast_usingDict() instead") int LZ4_decompress_fast_withPrefix64k (const char* src, char* dst, int originalSize); +#endif /* LZ4_H_2983827168210 */ #if defined (__cplusplus) } #endif - -#endif /* LZ4_H_2983827168210 */ diff --git a/lib/lz4frame.c b/lib/lz4frame.c index a0a625b..119dbee 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -819,13 +819,12 @@ static size_t LZ4F_headerSize(const void* src, size_t srcSize) output : set internal values of dctx, such as dctxPtr->frameInfo and dctxPtr->dStage. Also allocates internal buffers. - @return : nb Bytes read from srcVoidPtr (necessarily <= srcSize) + @return : nb Bytes read from src (necessarily <= srcSize) or an error code (testable with LZ4F_isError()) */ static size_t LZ4F_decodeHeader(LZ4F_dctx* dctxPtr, const void* src, size_t srcSize) { - BYTE FLG, BD; - unsigned version, blockMode, blockChecksumFlag, contentSizeFlag, contentChecksumFlag, blockSizeID; + unsigned blockMode, contentSizeFlag, contentChecksumFlag, blockSizeID; size_t frameHeaderSize; const BYTE* srcPtr = (const BYTE*)src; @@ -852,12 +851,17 @@ static size_t LZ4F_decodeHeader(LZ4F_dctx* dctxPtr, const void* src, size_t srcS dctxPtr->frameInfo.frameType = LZ4F_frame; /* Flags */ - FLG = srcPtr[4]; - version = (FLG>>6) & _2BITS; - blockMode = (FLG>>5) & _1BIT; - blockChecksumFlag = (FLG>>4) & _1BIT; - contentSizeFlag = (FLG>>3) & _1BIT; - contentChecksumFlag = (FLG>>2) & _1BIT; + { U32 const FLG = srcPtr[4]; + U32 const version = (FLG>>6) & _2BITS; + U32 const blockChecksumFlag = (FLG>>4) & _1BIT; + blockMode = (FLG>>5) & _1BIT; + contentSizeFlag = (FLG>>3) & _1BIT; + contentChecksumFlag = (FLG>>2) & _1BIT; + /* validate */ + if (((FLG>>0)&_2BITS) != 0) return err0r(LZ4F_ERROR_reservedFlag_set); /* Reserved bits */ + if (version != 1) return err0r(LZ4F_ERROR_headerVersion_wrong); /* Version Number, only supported value */ + if (blockChecksumFlag != 0) return err0r(LZ4F_ERROR_blockChecksum_unsupported); /* Not supported for the time being */ + } /* Frame Header Size */ frameHeaderSize = contentSizeFlag ? maxFHSize : minFHSize; @@ -872,16 +876,13 @@ static size_t LZ4F_decodeHeader(LZ4F_dctx* dctxPtr, const void* src, size_t srcS return srcSize; } - BD = srcPtr[5]; - blockSizeID = (BD>>4) & _3BITS; - - /* validate */ - if (version != 1) return err0r(LZ4F_ERROR_headerVersion_wrong); /* Version Number, only supported value */ - if (blockChecksumFlag != 0) return err0r(LZ4F_ERROR_blockChecksum_unsupported); /* Not supported for the time being */ - if (((FLG>>0)&_2BITS) != 0) return err0r(LZ4F_ERROR_reservedFlag_set); /* Reserved bits */ - if (((BD>>7)&_1BIT) != 0) return err0r(LZ4F_ERROR_reservedFlag_set); /* Reserved bit */ - if (blockSizeID < 4) return err0r(LZ4F_ERROR_maxBlockSize_invalid); /* 4-7 only supported values for the time being */ - if (((BD>>0)&_4BITS) != 0) return err0r(LZ4F_ERROR_reservedFlag_set); /* Reserved bits */ + { U32 const BD = srcPtr[5]; + blockSizeID = (BD>>4) & _3BITS; + /* validate */ + if (((BD>>7)&_1BIT) != 0) return err0r(LZ4F_ERROR_reservedFlag_set); /* Reserved bit */ + if (blockSizeID < 4) return err0r(LZ4F_ERROR_maxBlockSize_invalid); /* 4-7 only supported values for the time being */ + if (((BD>>0)&_4BITS) != 0) return err0r(LZ4F_ERROR_reservedFlag_set); /* Reserved bits */ + } /* check header */ { BYTE const HC = LZ4F_headerChecksum(srcPtr+4, frameHeaderSize-5); @@ -901,11 +902,11 @@ static size_t LZ4F_decodeHeader(LZ4F_dctx* dctxPtr, const void* src, size_t srcS /* internal buffers allocation */ { size_t const bufferNeeded = dctxPtr->maxBlockSize + ((dctxPtr->frameInfo.blockMode==LZ4F_blockLinked) * 128 KB); if (bufferNeeded > dctxPtr->maxBufferSize) { /* tmp buffers too small */ + dctxPtr->maxBufferSize = 0; /* ensure allocation will be re-attempted on next entry*/ FREEMEM(dctxPtr->tmpIn); dctxPtr->tmpIn = (BYTE*)ALLOCATOR(dctxPtr->maxBlockSize); if (dctxPtr->tmpIn == NULL) return err0r(LZ4F_ERROR_allocation_failed); FREEMEM(dctxPtr->tmpOutBuffer); - dctxPtr->maxBufferSize = 0; dctxPtr->tmpOutBuffer= (BYTE*)ALLOCATOR(bufferNeeded); if (dctxPtr->tmpOutBuffer== NULL) return err0r(LZ4F_ERROR_allocation_failed); dctxPtr->maxBufferSize = bufferNeeded; @@ -1072,7 +1073,7 @@ size_t LZ4F_decompress(LZ4F_dctx* dctxPtr, case dstage_getHeader: if ((size_t)(srcEnd-srcPtr) >= maxFHSize) { /* enough to decode - shortcut */ - LZ4F_errorCode_t const hSize = LZ4F_decodeHeader(dctxPtr, srcPtr, srcEnd-srcPtr); + LZ4F_errorCode_t const hSize = LZ4F_decodeHeader(dctxPtr, srcPtr, srcEnd-srcPtr); /* will change dStage appropriately */ if (LZ4F_isError(hSize)) return hSize; srcPtr += hSize; break; diff --git a/lib/lz4frame.h b/lib/lz4frame.h index 7b750b6..c1acba5 100644 --- a/lib/lz4frame.h +++ b/lib/lz4frame.h @@ -57,15 +57,14 @@ extern "C" { of encoding standard metadata alongside LZ4-compressed blocks. */ -/*^*************************************************************** -* Compiler specifics -*****************************************************************/ -/* -* LZ4_DLL_EXPORT : -* Enable exporting of functions when building a Windows DLL -* LZ4FLIB_API : -* Control library symbols visibility. -*/ +/*-*************************************************************** + * Compiler specifics + *****************************************************************/ +/* LZ4_DLL_EXPORT : + * Enable exporting of functions when building a Windows DLL + * LZ4FLIB_API : + * Control library symbols visibility. + */ #if defined(LZ4_DLL_EXPORT) && (LZ4_DLL_EXPORT==1) # define LZ4FLIB_API __declspec(dllexport) #elif defined(LZ4_DLL_IMPORT) && (LZ4_DLL_IMPORT==1) @@ -86,8 +85,8 @@ extern "C" { /*-************************************ -* Error management -**************************************/ + * Error management + **************************************/ typedef size_t LZ4F_errorCode_t; LZ4FLIB_API unsigned LZ4F_isError(LZ4F_errorCode_t code); /**< tells if a `LZ4F_errorCode_t` function result is an error code */ @@ -207,7 +206,7 @@ typedef struct { unsigned reserved[3]; } LZ4F_compressOptions_t; -/* Resource Management */ +/*--- Resource Management ---*/ #define LZ4F_VERSION 100 LZ4FLIB_API unsigned LZ4F_getVersion(void); @@ -223,7 +222,7 @@ LZ4FLIB_API LZ4F_errorCode_t LZ4F_createCompressionContext(LZ4F_cctx** cctxPtr, LZ4FLIB_API LZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_cctx* cctx); -/* Compression */ +/*---- Compression ----*/ #define LZ4F_HEADER_SIZE_MAX 15 /*! LZ4F_compressBegin() : diff --git a/lib/lz4hc.h b/lib/lz4hc.h index 09a648c..c96273b 100644 --- a/lib/lz4hc.h +++ b/lib/lz4hc.h @@ -257,7 +257,7 @@ LZ4LIB_API int LZ4_compress_HC_destSize(void* LZ4HC_Data, * Result is provided in 2 parts : * @return : the number of bytes written into 'dst' * or 0 if compression fails. - * `sourceSizePtr` : value will be updated to indicate how much bytes were read from `source` + * `srcSizePtr` : value will be updated to indicate how much bytes were read from `src` * Important : due to limitations, this prototype only works well up to cLevel < LZ4HC_CLEVEL_OPT_MIN * beyond that level, compression performance will be much reduced due to internal incompatibilities */ diff --git a/tests/fuzzer.c b/tests/fuzzer.c index a36d20b..2e22912 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -664,7 +664,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c LZ4_resetStreamHC (&LZ4dictHC, compressionLevel); LZ4_loadDictHC(&LZ4dictHC, dict, dictSize); blockContinueCompressedSize = LZ4_compress_HC_continue_destSize(&LZ4dictHC, block, compressedBuffer, &consumedSize, availableSpace); - DISPLAYLEVEL(3, " LZ4_compress_HC_continue_destSize : compressed %6i/%6i into %6i/%6i at cLevel=%i\n", consumedSize, blockSize, blockContinueCompressedSize, availableSpace, compressionLevel); + DISPLAYLEVEL(5, " LZ4_compress_HC_continue_destSize : compressed %6i/%6i into %6i/%6i at cLevel=%i\n", consumedSize, blockSize, blockContinueCompressedSize, availableSpace, compressionLevel); FUZ_CHECKTEST(blockContinueCompressedSize==0, "LZ4_compress_HC_continue_destSize failed"); FUZ_CHECKTEST(blockContinueCompressedSize > availableSpace, "LZ4_compress_HC_continue_destSize write overflow"); FUZ_CHECKTEST(consumedSize > blockSize, "LZ4_compress_HC_continue_destSize read overflow"); -- cgit v0.12