From a55cde02b0b3a7477ea40cb250a1f14549ed397f Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 18 Aug 2014 17:32:19 +0100 Subject: Added : lz4Framing.h, first example of Framing compression API --- lz4Frame.h | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 lz4Frame.h diff --git a/lz4Frame.h b/lz4Frame.h new file mode 100644 index 0000000..8fc5909 --- /dev/null +++ b/lz4Frame.h @@ -0,0 +1,74 @@ +/* + LZ4 Frame - Auto-framing for LZ4 + Header File + Copyright (C) 2011-2014, Yann Collet. + BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + You can contact the author at : + - LZ4 source repository : http://code.google.com/p/lz4/ + - LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c +*/ +#pragma once + +#if defined (__cplusplus) +extern "C" { +#endif + + + +/************************************** + Framing compression functions +**************************************/ + +typedef void LZ4F_compressionContext_t; + +typedef enum { default=0, max64KB=4, max256KB=5, max1MB=6, max4MB=7} maxBlockSize_t; +typedef enum { default=0, blockLinked, blockIndependent} blockMode_t; +typedef enum { default=0, contentChecksumEnabled, contentNoChecksum} contentChecksum_t; + +typedef struct { + maxBlockSize_t maxBlockSize; + blockMode_t blockMode; + contentChecksum_t contentChecksum; +} LZ4F_preferences_t; + + +LZ4F_compressionContext_t* LZ4F_createCompressionContext(void* dstBuffer, size_t dstMaxSize, const LZ4F_preferences_t* preferences); + +size_t LZ4F_compressBound(size_t srcSize, const LZ4F_preferences_t* preferences); + +size_t LZ4F_compress(void* dstBuffer, size_t dstMaxSize, const void* srcBuffer, size_t srcSize, LZ4F_compressionContext_t* compressionContext); + +size_t LZ4F_flush(void* dstBuffer, size_t dstMaxSize, LZ4F_compressionContext_t* compressionContext); + +void LZ4F_freeCompressionContext(LZ4F_compressionContext_t* compressionContext); + + + + +#if defined (__cplusplus) +} +#endif -- cgit v0.12 From c88999ad0d45421068ec6ee3df6f4d5dd18eefb4 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 19 Aug 2014 16:09:07 +0100 Subject: updated lz4frame API, following suggestions from Takayuki Matsuoka --- lz4Frame.h | 74 ---------------------------------------------------------- lz4frame.h | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 74 deletions(-) delete mode 100644 lz4Frame.h create mode 100644 lz4frame.h diff --git a/lz4Frame.h b/lz4Frame.h deleted file mode 100644 index 8fc5909..0000000 --- a/lz4Frame.h +++ /dev/null @@ -1,74 +0,0 @@ -/* - LZ4 Frame - Auto-framing for LZ4 - Header File - Copyright (C) 2011-2014, Yann Collet. - BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are - met: - - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above - copyright notice, this list of conditions and the following disclaimer - in the documentation and/or other materials provided with the - distribution. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - You can contact the author at : - - LZ4 source repository : http://code.google.com/p/lz4/ - - LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c -*/ -#pragma once - -#if defined (__cplusplus) -extern "C" { -#endif - - - -/************************************** - Framing compression functions -**************************************/ - -typedef void LZ4F_compressionContext_t; - -typedef enum { default=0, max64KB=4, max256KB=5, max1MB=6, max4MB=7} maxBlockSize_t; -typedef enum { default=0, blockLinked, blockIndependent} blockMode_t; -typedef enum { default=0, contentChecksumEnabled, contentNoChecksum} contentChecksum_t; - -typedef struct { - maxBlockSize_t maxBlockSize; - blockMode_t blockMode; - contentChecksum_t contentChecksum; -} LZ4F_preferences_t; - - -LZ4F_compressionContext_t* LZ4F_createCompressionContext(void* dstBuffer, size_t dstMaxSize, const LZ4F_preferences_t* preferences); - -size_t LZ4F_compressBound(size_t srcSize, const LZ4F_preferences_t* preferences); - -size_t LZ4F_compress(void* dstBuffer, size_t dstMaxSize, const void* srcBuffer, size_t srcSize, LZ4F_compressionContext_t* compressionContext); - -size_t LZ4F_flush(void* dstBuffer, size_t dstMaxSize, LZ4F_compressionContext_t* compressionContext); - -void LZ4F_freeCompressionContext(LZ4F_compressionContext_t* compressionContext); - - - - -#if defined (__cplusplus) -} -#endif diff --git a/lz4frame.h b/lz4frame.h new file mode 100644 index 0000000..899bb09 --- /dev/null +++ b/lz4frame.h @@ -0,0 +1,79 @@ +/* + LZ4 auto-framing library + Header File + Copyright (C) 2011-2014, Yann Collet. + BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + You can contact the author at : + - LZ4 source repository : http://code.google.com/p/lz4/ + - LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c +*/ +#pragma once + +#if defined (__cplusplus) +extern "C" { +#endif + + +/************************************** + Error management +**************************************/ +typedef enum { ERROR_GENERIC =-1U, ERROR_MIN = -2U } LZ4F_errorCode_t; +int LZ4F_isError(size_t code); + + +/************************************** + Framing compression functions +**************************************/ + +typedef void LZ4F_compressionContext_t; + +typedef enum { LZ4F_default=0, max64KB=4, max256KB=5, max1MB=6, max4MB=7} maxBlockSize_t; +typedef enum { LZ4F_default=0, blockLinked, blockIndependent} blockMode_t; +typedef enum { LZ4F_default=0, contentChecksumEnabled, contentNoChecksum} contentChecksum_t; + +typedef struct { + maxBlockSize_t maxBlockSize; + blockMode_t blockMode; + contentChecksum_t contentChecksum; +} LZ4F_preferences_t; + + +size_t LZ4F_createCompressionContext(LZ4F_compressionContext_t** compressionContextPtr, void* dstBuffer, size_t dstMaxSize, const LZ4F_preferences_t* preferences); + +size_t LZ4F_compressBound(size_t srcSize, const LZ4F_preferences_t* preferences); +size_t LZ4F_getMaxSrcSize(size_t maxDstSize, const LZ4F_preferences_t* preferences); + +size_t LZ4F_compress(LZ4F_compressionContext_t* compressionContext, void* dstBuffer, size_t dstMaxSize, const void* srcBuffer, size_t srcSize); + +size_t LZ4F_flush(LZ4F_compressionContext_t* compressionContext, void* dstBuffer, size_t dstMaxSize); + +void LZ4F_freeCompressionContext(LZ4F_compressionContext_t* compressionContext); + + +#if defined (__cplusplus) +} +#endif -- cgit v0.12 From 59b2c9df9d3c754295523ab79d2e99e7ba6d920b Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 19 Aug 2014 17:24:37 +0100 Subject: Added : lz4frame comments (doc) --- lz4frame.h | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 57 insertions(+), 7 deletions(-) diff --git a/lz4frame.h b/lz4frame.h index 899bb09..7638953 100644 --- a/lz4frame.h +++ b/lz4frame.h @@ -41,7 +41,15 @@ extern "C" { /************************************** Error management **************************************/ -typedef enum { ERROR_GENERIC =-1U, ERROR_MIN = -2U } LZ4F_errorCode_t; +typedef enum { + ERROR_GENERIC = -1U, + ERROR_maxDstSize_tooSmall = -2U, + ERROR_compressionLevel_invalid = -3U, + ERROR_maxBlockSize_invalid = -4U, + ERROR_blockMode_invalid = -5U, + ERROR_contentChecksumFlag_invalid = -6U, + ERROR_MIN = -7U + } LZ4F_errorCode_t; int LZ4F_isError(size_t code); @@ -56,22 +64,64 @@ typedef enum { LZ4F_default=0, blockLinked, blockIndependent} blockMode_t; typedef enum { LZ4F_default=0, contentChecksumEnabled, contentNoChecksum} contentChecksum_t; typedef struct { + int compressionLevel; /* valid values are >= 0 */ maxBlockSize_t maxBlockSize; blockMode_t blockMode; - contentChecksum_t contentChecksum; + contentChecksum_t contentChecksumFlag; } LZ4F_preferences_t; - -size_t LZ4F_createCompressionContext(LZ4F_compressionContext_t** compressionContextPtr, void* dstBuffer, size_t dstMaxSize, const LZ4F_preferences_t* preferences); +typedef struct { + int compressionLevel; /* default is compressionLevel value passed within preferences */ + int autoFlush; /* default is 0 = no autoflush */ +} LZ4F_compressOptions_t; size_t LZ4F_compressBound(size_t srcSize, const LZ4F_preferences_t* preferences); size_t LZ4F_getMaxSrcSize(size_t maxDstSize, const LZ4F_preferences_t* preferences); -size_t LZ4F_compress(LZ4F_compressionContext_t* compressionContext, void* dstBuffer, size_t dstMaxSize, const void* srcBuffer, size_t srcSize); + +/* Main compression functions */ + +size_t LZ4F_compressInit(LZ4F_compressionContext_t** compressionContextPtr, void* dstBuffer, size_t dstMaxSize, const LZ4F_preferences_t* preferences); +/* LZ4F_compress_Init() : + * The first thing to do is to create a compressionContext object. + * This is achieved using LZ4F_compressInit(), which takes as argument a dstBuffer and a LZ4F_preferences_t structure. + * The LZ4F_preferences_t structure is optional : you can provide NULL as argument, all preferences will then be set to default. + * The dstBuffer is required : LZ4F_compressInit() will write the frame header into it. + * dstBuffer must be large enough to accomodate a header (dstMaxSize). Maximum header size is 15 bytes. + * The result of the function is the number of bytes written into dstBuffer for the header, or an error code (can be tested using LZ4F_isError()) + */ + +size_t LZ4F_compress(LZ4F_compressionContext_t* compressionContext, void* dstBuffer, size_t dstMaxSize, const void* srcBuffer, size_t srcSize, const LZ4F_compressOptions_t* compressOptions); +/* LZ4F_compress() + * You can then call LZ4F_compress() repetitively to compress as much data as necessary. + * The most important rule to keep in mind is that dstBuffer must be large enough (dstMaxSize) to ensure compression completion. + * You can know the minimum size of dstMaxSize by using LZ4F_compressBound() + * Conversely, given a fixed dstMaxSize value, you can know the maximum srcSize authorized using LZ4F_getMaxSrcSize() + * If this condition is not respected, LZ4F_compress() will fail (result is an errorCode) + * The LZ4F_compressOptions_t structure is optional : you can provide NULL as argument. + * The result of the function is the number of bytes written into dstBuffer (it can be zero, meaning input data is just stored within compressionContext for a future block to complete) + * The function outputs an error code if it fails (can be tested using LZ4F_isError()) + */ -size_t LZ4F_flush(LZ4F_compressionContext_t* compressionContext, void* dstBuffer, size_t dstMaxSize); +size_t LZ4F_flush(LZ4F_compressionContext_t* compressionContext, void* dstBuffer, size_t dstMaxSize, const LZ4F_compressOptions_t* compressOptions); +/* LZ4F_flush() + * Should you need to create compressed data immediately, without waiting for a block to be filled, + * you can call LZ4_flush(), which will immediately compress any remaining data stored within compressionContext. + * The result of the function is the number of bytes written into dstBuffer (it can be zero there was no data left within compressionContext) + * The function outputs an error code if it fails (can be tested using LZ4F_isError()) + * The LZ4F_compressOptions_t structure is optional : you can provide NULL as argument. + */ -void LZ4F_freeCompressionContext(LZ4F_compressionContext_t* compressionContext); +size_t LZ4F_compressEnd(LZ4F_compressionContext_t* compressionContext, void* dstBuffer, size_t dstMaxSize, const LZ4F_compressOptions_t* compressOptions); +/* LZ4F_compressEnd() + * When you want to properly finish the compressed frame, just call LZ4F_compressEnd(). + * It will flush whatever data remained within compressionContext (like LZ4_flush()) + * but also properly finalize the frame, with an endMark and a checksum. + * It will also free compressionContext memory, so you can't call LZ4F_compress() anymore afterwards. + * The result of the function is the number of bytes written into dstBuffer (necessarily >= 4 (endMark size)) + * The function outputs an error code if it fails (can be tested using LZ4F_isError()) + * The LZ4F_compressOptions_t structure is optional : you can provide NULL as argument. + */ #if defined (__cplusplus) -- cgit v0.12 From 536a1236c272a7cb8e01f36e2841357449a5a7e1 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 19 Aug 2014 21:19:29 +0100 Subject: minor lz4frame API update --- lz4frame.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lz4frame.h b/lz4frame.h index 7638953..3caa937 100644 --- a/lz4frame.h +++ b/lz4frame.h @@ -57,7 +57,7 @@ int LZ4F_isError(size_t code); Framing compression functions **************************************/ -typedef void LZ4F_compressionContext_t; +typedef void* LZ4F_compressionContext_t; typedef enum { LZ4F_default=0, max64KB=4, max256KB=5, max1MB=6, max4MB=7} maxBlockSize_t; typedef enum { LZ4F_default=0, blockLinked, blockIndependent} blockMode_t; @@ -81,8 +81,8 @@ size_t LZ4F_getMaxSrcSize(size_t maxDstSize, const LZ4F_preferences_t* preferenc /* Main compression functions */ -size_t LZ4F_compressInit(LZ4F_compressionContext_t** compressionContextPtr, void* dstBuffer, size_t dstMaxSize, const LZ4F_preferences_t* preferences); -/* LZ4F_compress_Init() : +size_t LZ4F_compressInit(LZ4F_compressionContext_t* compressionContextPtr, void* dstBuffer, size_t dstMaxSize, const LZ4F_preferences_t* preferences); +/* LZ4F_compressInit() : * The first thing to do is to create a compressionContext object. * This is achieved using LZ4F_compressInit(), which takes as argument a dstBuffer and a LZ4F_preferences_t structure. * The LZ4F_preferences_t structure is optional : you can provide NULL as argument, all preferences will then be set to default. @@ -91,7 +91,7 @@ size_t LZ4F_compressInit(LZ4F_compressionContext_t** compressionContextPtr, void * The result of the function is the number of bytes written into dstBuffer for the header, or an error code (can be tested using LZ4F_isError()) */ -size_t LZ4F_compress(LZ4F_compressionContext_t* compressionContext, void* dstBuffer, size_t dstMaxSize, const void* srcBuffer, size_t srcSize, const LZ4F_compressOptions_t* compressOptions); +size_t LZ4F_compress(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const void* srcBuffer, size_t srcSize, const LZ4F_compressOptions_t* compressOptions); /* LZ4F_compress() * You can then call LZ4F_compress() repetitively to compress as much data as necessary. * The most important rule to keep in mind is that dstBuffer must be large enough (dstMaxSize) to ensure compression completion. @@ -103,7 +103,7 @@ size_t LZ4F_compress(LZ4F_compressionContext_t* compressionContext, void* dstBuf * The function outputs an error code if it fails (can be tested using LZ4F_isError()) */ -size_t LZ4F_flush(LZ4F_compressionContext_t* compressionContext, void* dstBuffer, size_t dstMaxSize, const LZ4F_compressOptions_t* compressOptions); +size_t LZ4F_flush(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const LZ4F_compressOptions_t* compressOptions); /* LZ4F_flush() * Should you need to create compressed data immediately, without waiting for a block to be filled, * you can call LZ4_flush(), which will immediately compress any remaining data stored within compressionContext. @@ -112,7 +112,7 @@ size_t LZ4F_flush(LZ4F_compressionContext_t* compressionContext, void* dstBuffer * The LZ4F_compressOptions_t structure is optional : you can provide NULL as argument. */ -size_t LZ4F_compressEnd(LZ4F_compressionContext_t* compressionContext, void* dstBuffer, size_t dstMaxSize, const LZ4F_compressOptions_t* compressOptions); +size_t LZ4F_compressEnd(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const LZ4F_compressOptions_t* compressOptions); /* LZ4F_compressEnd() * When you want to properly finish the compressed frame, just call LZ4F_compressEnd(). * It will flush whatever data remained within compressionContext (like LZ4_flush()) -- cgit v0.12 From 35946be11804d4fa46a60f4bb8326806f455eb5a Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sat, 23 Aug 2014 14:06:56 +0100 Subject: Updated LZ4F API : differentiate resource/compression functions --- lz4frame.h | 51 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 13 deletions(-) diff --git a/lz4frame.h b/lz4frame.h index 3caa937..9e75207 100644 --- a/lz4frame.h +++ b/lz4frame.h @@ -71,31 +71,55 @@ typedef struct { } LZ4F_preferences_t; typedef struct { - int compressionLevel; /* default is compressionLevel value passed within preferences */ int autoFlush; /* default is 0 = no autoflush */ + int stableSrc; /* unused for the time being, must be 0 */ } LZ4F_compressOptions_t; size_t LZ4F_compressBound(size_t srcSize, const LZ4F_preferences_t* preferences); -size_t LZ4F_getMaxSrcSize(size_t maxDstSize, const LZ4F_preferences_t* preferences); +size_t LZ4F_getMaxSrcSize(size_t dstMaxSize, const LZ4F_preferences_t* preferences); +/* LZ4F_compressBound() : gives the size of Dst buffer given a srcSize to handle worst case situations. + * LZ4F_getMaxSrcSize() : gives max allowed srcSize given dstMaxSize to handle worst case situations. + * You can use dstMaxSize==0 to know the "natural" srcSize instead (block size). + * The LZ4F_preferences_t structure is optional : you can provide NULL as argument, all preferences will then be set to default. + */ -/* Main compression functions */ +/* ********************************* + * Compression functions + * *********************************/ -size_t LZ4F_compressInit(LZ4F_compressionContext_t* compressionContextPtr, void* dstBuffer, size_t dstMaxSize, const LZ4F_preferences_t* preferences); -/* LZ4F_compressInit() : - * The first thing to do is to create a compressionContext object. - * This is achieved using LZ4F_compressInit(), which takes as argument a dstBuffer and a LZ4F_preferences_t structure. +/* Resource Management */ + +#define LZ4F_VERSION 100 +LZ4F_compressionContext_t LZ4F_createCompressionContext(int version, const LZ4F_preferences_t* preferences); +void LZ4F_freeCompressionContext(LZ4F_compressionContext_t* LZ4F_compressionContext); +/* LZ4F_createCompressionContext() : + * The first thing to do is to create a compressionContext object, which will be used in all compression operations. + * This is achieved using LZ4F_createCompressionContext(), which takes as argument a version and an LZ4F_preferences_t structure. + * The version provided MUST be LZ4F_VERSION. + * It is intended to track potential version differences between a program and an external dynamic library. * The LZ4F_preferences_t structure is optional : you can provide NULL as argument, all preferences will then be set to default. - * The dstBuffer is required : LZ4F_compressInit() will write the frame header into it. + * The result of the function is a pointer to the LZ4F_compressionContext_t object. + * If the pointer is NULL, there was an error during context creation. + * Object can release its memory using LZ4F_freeCompressionContext(); + */ + + +/* Compression */ + +size_t LZ4F_compressBegin(LZ4F_compressionContext_t* compressionContextPtr, void* dstBuffer, size_t dstMaxSize); +/* LZ4F_compressBegin() : + * will write the frame header into dstBuffer. * dstBuffer must be large enough to accomodate a header (dstMaxSize). Maximum header size is 15 bytes. - * The result of the function is the number of bytes written into dstBuffer for the header, or an error code (can be tested using LZ4F_isError()) + * The result of the function is the number of bytes written into dstBuffer for the header + * or an error code (can be tested using LZ4F_isError()) */ size_t LZ4F_compress(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const void* srcBuffer, size_t srcSize, const LZ4F_compressOptions_t* compressOptions); /* LZ4F_compress() * You can then call LZ4F_compress() repetitively to compress as much data as necessary. - * The most important rule to keep in mind is that dstBuffer must be large enough (dstMaxSize) to ensure compression completion. - * You can know the minimum size of dstMaxSize by using LZ4F_compressBound() + * The most important rule is that dstBuffer MUST be large enough (dstMaxSize) to ensure compression completion even in worst case. + * You can get the minimum value of dstMaxSize by using LZ4F_compressBound() * Conversely, given a fixed dstMaxSize value, you can know the maximum srcSize authorized using LZ4F_getMaxSrcSize() * If this condition is not respected, LZ4F_compress() will fail (result is an errorCode) * The LZ4F_compressOptions_t structure is optional : you can provide NULL as argument. @@ -107,7 +131,8 @@ size_t LZ4F_flush(LZ4F_compressionContext_t compressionContext, void* dstBuffer, /* LZ4F_flush() * Should you need to create compressed data immediately, without waiting for a block to be filled, * you can call LZ4_flush(), which will immediately compress any remaining data stored within compressionContext. - * The result of the function is the number of bytes written into dstBuffer (it can be zero there was no data left within compressionContext) + * The result of the function is the number of bytes written into dstBuffer + * (it can be zero, this means there was no data left within compressionContext) * The function outputs an error code if it fails (can be tested using LZ4F_isError()) * The LZ4F_compressOptions_t structure is optional : you can provide NULL as argument. */ @@ -117,10 +142,10 @@ size_t LZ4F_compressEnd(LZ4F_compressionContext_t compressionContext, void* dstB * When you want to properly finish the compressed frame, just call LZ4F_compressEnd(). * It will flush whatever data remained within compressionContext (like LZ4_flush()) * but also properly finalize the frame, with an endMark and a checksum. - * It will also free compressionContext memory, so you can't call LZ4F_compress() anymore afterwards. * The result of the function is the number of bytes written into dstBuffer (necessarily >= 4 (endMark size)) * The function outputs an error code if it fails (can be tested using LZ4F_isError()) * The LZ4F_compressOptions_t structure is optional : you can provide NULL as argument. + * compressionContext can then be used again, starting with LZ4F_compressBegin(). The preferences will remain the same. */ -- cgit v0.12 From 686d6327b3776a95a6941d3adbc317697adfd074 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 26 Aug 2014 00:56:54 +0100 Subject: LZ4F API update. Creating "simple function" (compression only) --- lz4frame.h | 47 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/lz4frame.h b/lz4frame.h index 9e75207..120be94 100644 --- a/lz4frame.h +++ b/lz4frame.h @@ -57,8 +57,6 @@ int LZ4F_isError(size_t code); Framing compression functions **************************************/ -typedef void* LZ4F_compressionContext_t; - typedef enum { LZ4F_default=0, max64KB=4, max256KB=5, max1MB=6, max4MB=7} maxBlockSize_t; typedef enum { LZ4F_default=0, blockLinked, blockIndependent} blockMode_t; typedef enum { LZ4F_default=0, contentChecksumEnabled, contentNoChecksum} contentChecksum_t; @@ -68,10 +66,37 @@ typedef struct { maxBlockSize_t maxBlockSize; blockMode_t blockMode; contentChecksum_t contentChecksumFlag; + int autoFlush; } LZ4F_preferences_t; + + +/********************************** + * Simple functions + * *********************************/ +size_t LZ4F_compressFrameBound(size_t srcSize, const LZ4F_preferences_t* preferences); + +size_t LZ4F_compressFrame(void* dstBuffer, size_t dstMaxSize, const void* srcBuffer, size_t srcSize, const LZ4F_preferences_t* preferences); +/* LZ4F_compressFrame() + * Compress an entire srcBuffer into a valid LZ4 frame, as defined by specification v1.4.1, in a single step. + * The most important rule is that dstBuffer MUST be large enough (dstMaxSize) to ensure compression completion even in worst case. + * You can get the minimum value of dstMaxSize by using LZ4F_compressFrameBound() + * If this condition is not respected, LZ4F_compressFrame() will fail (result is an errorCode) + * The LZ4F_preferences_t structure is optional : you can provide NULL as argument. All preferences will be set to default. + * The result of the function is the number of bytes written into dstBuffer. + * The function outputs an error code if it fails (can be tested using LZ4F_isError()) + */ + + + + +/********************************** + * Advanced functions + * *********************************/ + +typedef void* LZ4F_compressionContext_t; + typedef struct { - int autoFlush; /* default is 0 = no autoflush */ int stableSrc; /* unused for the time being, must be 0 */ } LZ4F_compressOptions_t; @@ -83,24 +108,18 @@ size_t LZ4F_getMaxSrcSize(size_t dstMaxSize, const LZ4F_preferences_t* preferenc * The LZ4F_preferences_t structure is optional : you can provide NULL as argument, all preferences will then be set to default. */ - -/* ********************************* - * Compression functions - * *********************************/ - /* Resource Management */ #define LZ4F_VERSION 100 -LZ4F_compressionContext_t LZ4F_createCompressionContext(int version, const LZ4F_preferences_t* preferences); -void LZ4F_freeCompressionContext(LZ4F_compressionContext_t* LZ4F_compressionContext); +LZ4F_errorCode_t LZ4F_createCompressionContext(LZ4F_compressionContext_t* LZ4F_compressionContext, int version, const LZ4F_preferences_t* preferences); +LZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_compressionContext_t LZ4F_compressionContext); /* LZ4F_createCompressionContext() : * The first thing to do is to create a compressionContext object, which will be used in all compression operations. * This is achieved using LZ4F_createCompressionContext(), which takes as argument a version and an LZ4F_preferences_t structure. - * The version provided MUST be LZ4F_VERSION. - * It is intended to track potential version differences between a program and an external dynamic library. + * The version provided MUST be LZ4F_VERSION. It is intended to track potential version differences between different binaries. * The LZ4F_preferences_t structure is optional : you can provide NULL as argument, all preferences will then be set to default. - * The result of the function is a pointer to the LZ4F_compressionContext_t object. - * If the pointer is NULL, there was an error during context creation. + * The function will provide a pointer to a fully allocated LZ4F_compressionContext_t object. + * If the result LZ4F_errorCode_t is not zero, there was an error during context creation. * Object can release its memory using LZ4F_freeCompressionContext(); */ -- cgit v0.12 From d2d3c4f10d6b5c3b73d86372deb189b0e6093669 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 26 Aug 2014 10:27:45 +0100 Subject: early LZ4F decompression API --- lz4frame.h | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/lz4frame.h b/lz4frame.h index 120be94..5bf153e 100644 --- a/lz4frame.h +++ b/lz4frame.h @@ -42,6 +42,7 @@ extern "C" { Error management **************************************/ typedef enum { + OK_NOERROR = 0, ERROR_GENERIC = -1U, ERROR_maxDstSize_tooSmall = -2U, ERROR_compressionLevel_invalid = -3U, @@ -72,7 +73,7 @@ typedef struct { /********************************** - * Simple functions + * Simple compression functions * *********************************/ size_t LZ4F_compressFrameBound(size_t srcSize, const LZ4F_preferences_t* preferences); @@ -89,9 +90,8 @@ size_t LZ4F_compressFrame(void* dstBuffer, size_t dstMaxSize, const void* srcBuf - /********************************** - * Advanced functions + * Advanced compression functions * *********************************/ typedef void* LZ4F_compressionContext_t; @@ -168,6 +168,39 @@ size_t LZ4F_compressEnd(LZ4F_compressionContext_t compressionContext, void* dstB */ +/********************************** + * Decompression functions + * *********************************/ + +typedef void* LZ4F_decompressionContext_t; + +typedef struct { + int stableDst; /* unused for the time being, must be 0 */ +} LZ4F_decompressOptions_t; + +LZ4F_errorCode_t LZ4F_createDecompressionContext(LZ4F_compressionContext_t* LZ4F_decompressionContext); +LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_compressionContext_t LZ4F_decompressionContext); + +LZ4F_errorCode_t LZ4F_decompressBegin(LZ4F_decompressionContext_t* decompressionContextPtr, void* srcBuffer, size_t* srcSize); +size_t LZ4F_getBlockSize(LZ4F_decompressionContext_t decompressionContext); +LZ4F_errorCode_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContext, void* dstBuffer, size_t* dstSize, const void* srcBuffer, size_t* srcSize, const LZ4F_decompressOptions_t* decompressOptions); +/* LZ4F_decompress() + * You can then call LZ4F_decompress() repetitively to regenerate as much data as necessary. + * The function will attempt to decode *srcSize from srcBuffer into dstBuffer, of maximum size *dstSize. + * + * The number of bytes generated into dstBuffer will be provided within *dstSize (necessarily <= original value). + * If dstBuffer is not large enough to hold at least one data block, the function may fail (result is an errorCode). + * You can know the size of one full block by using LZ4F_getBlockSize(); + * + * The number of bytes effectively read from srcBuffer will be provided within *srcSize (necessarily <= original value). + * If the number of bytes read is < number of bytes provided, then the function could not complete decompression operation. + * You will have call it again. + * + * The function result is an error code which can be tested using LZ4F_isError(). + */ + + + #if defined (__cplusplus) } #endif -- cgit v0.12 From 1174bec25b34e0c1b8eecbb6cc723ab351dbb3b4 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 27 Aug 2014 09:14:17 +0100 Subject: clarified comments within lz4frame API --- lz4frame.h | 58 ++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 48 insertions(+), 10 deletions(-) diff --git a/lz4frame.h b/lz4frame.h index 5bf153e..45fdae3 100644 --- a/lz4frame.h +++ b/lz4frame.h @@ -31,6 +31,13 @@ - LZ4 source repository : http://code.google.com/p/lz4/ - LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c */ + +/* LZ4F is a stand-alone API to create LZ4-compressed frames + * fully conformant to specification v1.4.1. + * All related operations, including memory management, are handled by the library. + * You don't need lz4.h when using lz4frame.h. + * */ + #pragma once #if defined (__cplusplus) @@ -60,20 +67,20 @@ int LZ4F_isError(size_t code); typedef enum { LZ4F_default=0, max64KB=4, max256KB=5, max1MB=6, max4MB=7} maxBlockSize_t; typedef enum { LZ4F_default=0, blockLinked, blockIndependent} blockMode_t; -typedef enum { LZ4F_default=0, contentChecksumEnabled, contentNoChecksum} contentChecksum_t; +typedef enum { LZ4F_default=0, contentChecksumEnabled, noContentChecksum} contentChecksum_t; typedef struct { - int compressionLevel; /* valid values are >= 0 */ + unsigned compressionLevel; maxBlockSize_t maxBlockSize; blockMode_t blockMode; contentChecksum_t contentChecksumFlag; - int autoFlush; + unsigned autoFlush; } LZ4F_preferences_t; /********************************** - * Simple compression functions + * Simple compression function * *********************************/ size_t LZ4F_compressFrameBound(size_t srcSize, const LZ4F_preferences_t* preferences); @@ -178,23 +185,54 @@ typedef struct { int stableDst; /* unused for the time being, must be 0 */ } LZ4F_decompressOptions_t; +/* Resource management */ + LZ4F_errorCode_t LZ4F_createDecompressionContext(LZ4F_compressionContext_t* LZ4F_decompressionContext); LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_compressionContext_t LZ4F_decompressionContext); +/* LZ4F_createDecompressionContext() : + * The first thing to do is to create a decompressionContext object, which will be used in all decompression operations. + * This is achieved using LZ4F_createDecompressionContext(). + * The function will provide a pointer to a fully allocated and initialised LZ4F_decompressionContext object. + * If the result LZ4F_errorCode_t is not zero, there was an error during context creation. + * Object can release its memory using LZ4F_freeDecompressionContext(); + */ -LZ4F_errorCode_t LZ4F_decompressBegin(LZ4F_decompressionContext_t* decompressionContextPtr, void* srcBuffer, size_t* srcSize); +/* Header information */ + +LZ4F_errorCode_t LZ4F_analyzeHeader(LZ4F_decompressionContext_t* decompressionContextPtr, void* srcBuffer, size_t* srcSize); +/* LZ4F_analyzeHeader() + * + * This function decodes header information, such as blockSize. + * LZ4F_analyzeHeader() is not compulsory. You could start instead by calling directly LZ4F_decompress. + * The objective is to only extract useful header information, without starting decompression. + * + * The number of bytes read from srcBuffer will be provided within *srcSize (necessarily <= original value). + * + * The function result is an error code which can be tested using LZ4F_isError(). + */ + size_t LZ4F_getBlockSize(LZ4F_decompressionContext_t decompressionContext); +/* LZ4F_getBlockSize() + * Provides the maximum size of a (decompressed) block within previously analyzed frame. + * Primarily useful for memory allocation purposes. + * Header must be previously analyzed, using LZ4F_analyzeHeader(). + * + * The result is the maximum size of a single block, + * or an error code which can be tested using LZ4F_isError() in case of failure. + */ + +/* Decompression */ + LZ4F_errorCode_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContext, void* dstBuffer, size_t* dstSize, const void* srcBuffer, size_t* srcSize, const LZ4F_decompressOptions_t* decompressOptions); /* LZ4F_decompress() - * You can then call LZ4F_decompress() repetitively to regenerate as much data as necessary. - * The function will attempt to decode *srcSize from srcBuffer into dstBuffer, of maximum size *dstSize. + * LZ4F_decompress() will be called repetitively to regenerate as much data as necessary. + * The function will attempt to decode *srcSize from srcBuffer, into dstBuffer of maximum size *dstSize. * * The number of bytes generated into dstBuffer will be provided within *dstSize (necessarily <= original value). - * If dstBuffer is not large enough to hold at least one data block, the function may fail (result is an errorCode). - * You can know the size of one full block by using LZ4F_getBlockSize(); * * The number of bytes effectively read from srcBuffer will be provided within *srcSize (necessarily <= original value). * If the number of bytes read is < number of bytes provided, then the function could not complete decompression operation. - * You will have call it again. + * You will have to call it again, using the same src arguments (but eventually different dst arguments). * * The function result is an error code which can be tested using LZ4F_isError(). */ -- cgit v0.12 From 3108eef8100c26d045275ab0ec9116a33c7f873a Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 28 Aug 2014 10:14:33 +0100 Subject: lz4frame API update : introducing LZ4F_getHeaderInfo() --- lz4frame.h | 60 ++++++++++++++++++++++++------------------------------------ 1 file changed, 24 insertions(+), 36 deletions(-) diff --git a/lz4frame.h b/lz4frame.h index 45fdae3..240a81d 100644 --- a/lz4frame.h +++ b/lz4frame.h @@ -49,7 +49,8 @@ extern "C" { Error management **************************************/ typedef enum { - OK_NOERROR = 0, + OK_NoError = 0, + OK_FrameEnd = 1, ERROR_GENERIC = -1U, ERROR_maxDstSize_tooSmall = -2U, ERROR_compressionLevel_invalid = -3U, @@ -70,11 +71,11 @@ typedef enum { LZ4F_default=0, blockLinked, blockIndependent} blockMode_t; typedef enum { LZ4F_default=0, contentChecksumEnabled, noContentChecksum} contentChecksum_t; typedef struct { - unsigned compressionLevel; - maxBlockSize_t maxBlockSize; - blockMode_t blockMode; - contentChecksum_t contentChecksumFlag; - unsigned autoFlush; + unsigned compressionLevel; /* from 0 to 16 */ + maxBlockSize_t maxBlockSize; /* max64KB, max256KB, max1MB, max4MB ; 0 == default */ + blockMode_t blockMode; /* blockLinked, blockIndependent ; 0 == default */ + contentChecksum_t contentChecksumFlag; /* contentChecksumEnabled, noContentChecksum ; 0 == default */ + unsigned autoFlush; /* 1 == automatic flush after each call to LZ4F_compress() */ } LZ4F_preferences_t; @@ -107,14 +108,6 @@ typedef struct { int stableSrc; /* unused for the time being, must be 0 */ } LZ4F_compressOptions_t; -size_t LZ4F_compressBound(size_t srcSize, const LZ4F_preferences_t* preferences); -size_t LZ4F_getMaxSrcSize(size_t dstMaxSize, const LZ4F_preferences_t* preferences); -/* LZ4F_compressBound() : gives the size of Dst buffer given a srcSize to handle worst case situations. - * LZ4F_getMaxSrcSize() : gives max allowed srcSize given dstMaxSize to handle worst case situations. - * You can use dstMaxSize==0 to know the "natural" srcSize instead (block size). - * The LZ4F_preferences_t structure is optional : you can provide NULL as argument, all preferences will then be set to default. - */ - /* Resource Management */ #define LZ4F_VERSION 100 @@ -141,6 +134,14 @@ size_t LZ4F_compressBegin(LZ4F_compressionContext_t* compressionContextPtr, void * or an error code (can be tested using LZ4F_isError()) */ +size_t LZ4F_compressBound(size_t srcSize, const LZ4F_preferences_t* preferences); +size_t LZ4F_getMaxSrcSize(size_t dstMaxSize, const LZ4F_preferences_t* preferences); +/* LZ4F_compressBound() : gives the size of Dst buffer given a srcSize to handle worst case situations. + * LZ4F_getMaxSrcSize() : gives max allowed srcSize given dstMaxSize to handle worst case situations. + * You can use dstMaxSize==0 to know the "natural" srcSize instead (block size). + * The LZ4F_preferences_t structure is optional : you can provide NULL as argument, all preferences will then be set to default. + */ + size_t LZ4F_compress(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const void* srcBuffer, size_t srcSize, const LZ4F_compressOptions_t* compressOptions); /* LZ4F_compress() * You can then call LZ4F_compress() repetitively to compress as much data as necessary. @@ -197,44 +198,31 @@ LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_compressionContext_t LZ4F_de * Object can release its memory using LZ4F_freeDecompressionContext(); */ -/* Header information */ +/* Decompression */ -LZ4F_errorCode_t LZ4F_analyzeHeader(LZ4F_decompressionContext_t* decompressionContextPtr, void* srcBuffer, size_t* srcSize); -/* LZ4F_analyzeHeader() - * +LZ4F_errorCode_t LZ4F_getHeaderInfo(LZ4F_preferences_t headerInfo, LZ4F_decompressionContext_t* decompressionContextPtr, void* srcBuffer, size_t* srcSize); +/* LZ4F_getHeaderInfo() * This function decodes header information, such as blockSize. - * LZ4F_analyzeHeader() is not compulsory. You could start instead by calling directly LZ4F_decompress. - * The objective is to only extract useful header information, without starting decompression. - * + * LZ4F_getHeaderInfo() is optional : you could start instead by calling directly LZ4F_decompress. + * The objective is to extract header information without starting decompression, typically for allocation purposes. + * LZ4F_getHeaderInfo() could also be used *after* starting decompression, on a valid frame decompression context. * The number of bytes read from srcBuffer will be provided within *srcSize (necessarily <= original value). - * * The function result is an error code which can be tested using LZ4F_isError(). */ -size_t LZ4F_getBlockSize(LZ4F_decompressionContext_t decompressionContext); -/* LZ4F_getBlockSize() - * Provides the maximum size of a (decompressed) block within previously analyzed frame. - * Primarily useful for memory allocation purposes. - * Header must be previously analyzed, using LZ4F_analyzeHeader(). - * - * The result is the maximum size of a single block, - * or an error code which can be tested using LZ4F_isError() in case of failure. - */ - -/* Decompression */ - LZ4F_errorCode_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContext, void* dstBuffer, size_t* dstSize, const void* srcBuffer, size_t* srcSize, const LZ4F_decompressOptions_t* decompressOptions); /* LZ4F_decompress() - * LZ4F_decompress() will be called repetitively to regenerate as much data as necessary. + * Call this function repetitively to regenerate data compressed within srcBuffer. * The function will attempt to decode *srcSize from srcBuffer, into dstBuffer of maximum size *dstSize. * * The number of bytes generated into dstBuffer will be provided within *dstSize (necessarily <= original value). * * The number of bytes effectively read from srcBuffer will be provided within *srcSize (necessarily <= original value). - * If the number of bytes read is < number of bytes provided, then the function could not complete decompression operation. + * If the number of bytes read is < number of bytes provided, then the decompression operation is not complete. * You will have to call it again, using the same src arguments (but eventually different dst arguments). * * The function result is an error code which can be tested using LZ4F_isError(). + * You'll be informed the frame is fully decoded on receiving result OK_FrameEnd(=1). */ -- cgit v0.12 From 2050756f0f8f2cb0d12229ed4a09095f74ddff21 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 29 Aug 2014 10:15:04 +0100 Subject: minor lz4frame API update : errorCodes & frameInfo --- lz4frame.h | 49 ++++++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/lz4frame.h b/lz4frame.h index 240a81d..dc6a93a 100644 --- a/lz4frame.h +++ b/lz4frame.h @@ -48,18 +48,17 @@ extern "C" { /************************************** Error management **************************************/ -typedef enum { - OK_NoError = 0, - OK_FrameEnd = 1, - ERROR_GENERIC = -1U, - ERROR_maxDstSize_tooSmall = -2U, - ERROR_compressionLevel_invalid = -3U, - ERROR_maxBlockSize_invalid = -4U, - ERROR_blockMode_invalid = -5U, - ERROR_contentChecksumFlag_invalid = -6U, - ERROR_MIN = -7U - } LZ4F_errorCode_t; -int LZ4F_isError(size_t code); +typedef size_t LZ4F_errorCode_t; +typedef enum { OK_NoError = 0, OK_FrameEnd = 1 } LZ4F_successCodes; +typedef enum { OK_NoError = 0, ERROR_GENERIC = 1, + ERROR_maxBlockSize_invalid, ERROR_blockMode_invalid, ERROR_contentChecksumFlag_invalid, + ERROR_compressionLevel_invalid, + ERROR_srcSize_tooLarge, ERROR_maxDstSize_tooSmall, + ERROR_maxCode + } LZ4F_errorCodes; /* error codes are negative unsigned values. + Compare function result to (-specificCode) */ + +int LZ4F_isError(LZ4F_errorCode_t code); /* Basically : code > -ERROR_maxCode */ /************************************** @@ -71,11 +70,15 @@ typedef enum { LZ4F_default=0, blockLinked, blockIndependent} blockMode_t; typedef enum { LZ4F_default=0, contentChecksumEnabled, noContentChecksum} contentChecksum_t; typedef struct { - unsigned compressionLevel; /* from 0 to 16 */ maxBlockSize_t maxBlockSize; /* max64KB, max256KB, max1MB, max4MB ; 0 == default */ blockMode_t blockMode; /* blockLinked, blockIndependent ; 0 == default */ contentChecksum_t contentChecksumFlag; /* contentChecksumEnabled, noContentChecksum ; 0 == default */ - unsigned autoFlush; /* 1 == automatic flush after each call to LZ4F_compress() */ +} LZ4F_frameInfo_t; + +typedef struct { + LZ4F_frameInfo_t frameInfo + unsigned compressionLevel; /* from 0 to 16 */ + unsigned autoFlush; /* 1 == automatic flush after each call to LZ4F_compress() */ } LZ4F_preferences_t; @@ -83,7 +86,7 @@ typedef struct { /********************************** * Simple compression function * *********************************/ -size_t LZ4F_compressFrameBound(size_t srcSize, const LZ4F_preferences_t* preferences); +size_t LZ4F_compressFrameBound(size_t srcSize, const LZ4F_frameInfo_t* frameInfo); size_t LZ4F_compressFrame(void* dstBuffer, size_t dstMaxSize, const void* srcBuffer, size_t srcSize, const LZ4F_preferences_t* preferences); /* LZ4F_compressFrame() @@ -134,8 +137,8 @@ size_t LZ4F_compressBegin(LZ4F_compressionContext_t* compressionContextPtr, void * or an error code (can be tested using LZ4F_isError()) */ -size_t LZ4F_compressBound(size_t srcSize, const LZ4F_preferences_t* preferences); -size_t LZ4F_getMaxSrcSize(size_t dstMaxSize, const LZ4F_preferences_t* preferences); +size_t LZ4F_compressBound(size_t srcSize, const LZ4F_frameInfo_t* frameInfo); +size_t LZ4F_getMaxSrcSize(size_t dstMaxSize, const LZ4F_frameInfo_t* frameInfo); /* LZ4F_compressBound() : gives the size of Dst buffer given a srcSize to handle worst case situations. * LZ4F_getMaxSrcSize() : gives max allowed srcSize given dstMaxSize to handle worst case situations. * You can use dstMaxSize==0 to know the "natural" srcSize instead (block size). @@ -200,12 +203,12 @@ LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_compressionContext_t LZ4F_de /* Decompression */ -LZ4F_errorCode_t LZ4F_getHeaderInfo(LZ4F_preferences_t headerInfo, LZ4F_decompressionContext_t* decompressionContextPtr, void* srcBuffer, size_t* srcSize); -/* LZ4F_getHeaderInfo() - * This function decodes header information, such as blockSize. - * LZ4F_getHeaderInfo() is optional : you could start instead by calling directly LZ4F_decompress. +LZ4F_errorCode_t LZ4F_getFrameInfo(LZ4F_frameInfo_t frameInfo, LZ4F_decompressionContext_t* decompressionContextPtr, void* srcBuffer, size_t* srcSize); +/* LZ4F_getFrameInfo() + * This function decodes frame header information, such as blockSize. + * It is optional : you could start by calling directly LZ4F_decompress() instead. * The objective is to extract header information without starting decompression, typically for allocation purposes. - * LZ4F_getHeaderInfo() could also be used *after* starting decompression, on a valid frame decompression context. + * LZ4F_getFrameInfo() can also be used *after* starting decompression, on a valid LZ4F_decompressionContext_t. * The number of bytes read from srcBuffer will be provided within *srcSize (necessarily <= original value). * The function result is an error code which can be tested using LZ4F_isError(). */ @@ -222,7 +225,7 @@ LZ4F_errorCode_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContex * You will have to call it again, using the same src arguments (but eventually different dst arguments). * * The function result is an error code which can be tested using LZ4F_isError(). - * You'll be informed the frame is fully decoded on receiving result OK_FrameEnd(=1). + * When the frame is fully decoded, the function result will be OK_FrameEnd(=1). */ -- cgit v0.12 From 47b70f4956528d2dbcaf25e4fd04a4f8844bcbf7 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 29 Aug 2014 16:35:13 +0100 Subject: lz4frame minor API fixes (pointers) lz4frame.c first example code (incomplete) --- lz4frame.c | 265 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lz4frame.h | 42 +++++----- 2 files changed, 289 insertions(+), 18 deletions(-) create mode 100644 lz4frame.c diff --git a/lz4frame.c b/lz4frame.c new file mode 100644 index 0000000..8af9c28 --- /dev/null +++ b/lz4frame.c @@ -0,0 +1,265 @@ +/* + LZ4 auto-framing library + Copyright (C) 2011-2014, Yann Collet. + BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + You can contact the author at : + - LZ4 source repository : http://code.google.com/p/lz4/ + - LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c +*/ + +/* LZ4F is a stand-alone API to create LZ4-compressed frames + * fully conformant to specification v1.4.1. + * All related operations, including memory management, are handled by the library. + * You don't need lz4.h when using lz4frame.h. + * */ + + +/************************************** + CPU Feature Detection +**************************************/ +/* 32 or 64 bits ? */ +static const int LZ4F_32bits = (sizeof(void*)==4); +static const int LZ4F_64bits = (sizeof(void*)==8); + +/* Little Endian or Big Endian ? */ +typedef union { + int num; + char bytes[4]; + } endian_t; +static const endian_t endianTest = { .num = 1 }; +#define LZ4F_isLittleEndian (endianTest.bytes[0]) + + +/************************************** + Compiler Options +**************************************/ +#ifdef _MSC_VER /* Visual Studio */ +# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ +#endif + +#define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) + + +/************************************** + Memory routines +**************************************/ +#include /* malloc, calloc, free */ +#define ALLOCATOR(n,s) calloc(n,s) +#define FREEMEM free +#include /* memset, memcpy */ +#define MEM_INIT memset + + +/************************************** + Includes +**************************************/ +#include "lz4frame.h" +#include "lz4.h" +#include "lz4hc.h" +#include "xxhash.h" + + +/************************************** + Basic Types +**************************************/ +#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */ +# include + typedef uint8_t BYTE; + typedef uint16_t U16; + typedef uint32_t U32; + typedef int32_t S32; + typedef uint64_t U64; +#else + typedef unsigned char BYTE; + typedef unsigned short U16; + typedef unsigned int U32; + typedef signed int S32; + typedef unsigned long long U64; +#endif + +#if defined(__GNUC__) +# define _PACKED __attribute__ ((packed)) +#else +# define _PACKED +#endif + +#if !defined(__GNUC__) +# if defined(__IBMC__) || defined(__SUNPRO_C) || defined(__SUNPRO_CC) +# pragma pack(1) +# else +# pragma pack(push, 1) +# endif +#endif + +typedef struct { U16 v; } _PACKED U16_S; +typedef struct { U32 v; } _PACKED U32_S; +typedef struct { U64 v; } _PACKED U64_S; +typedef struct {size_t v;} _PACKED size_t_S; + +#if !defined(__GNUC__) +# if defined(__SUNPRO_C) || defined(__SUNPRO_CC) +# pragma pack(0) +# else +# pragma pack(pop) +# endif +#endif + +#define A16(x) (((U16_S *)(x))->v) +#define A32(x) (((U32_S *)(x))->v) +#define A64(x) (((U64_S *)(x))->v) +#define AARCH(x) (((size_t_S *)(x))->v) + + +/************************************** + Constants +**************************************/ +#define KB *(1<<10) +#define MB *(1<<20) +#define GB *(1<<30) + + +/************************************** + Structures and local types +**************************************/ + + +/************************************** + Macros +**************************************/ + + +/************************************** + Private functions +**************************************/ +static size_t LZ4F_getBlockSize(unsigned blockSizeID) +{ + static const size_t blockSizes[4] = { 64 KB, 256 KB, 1 MB, 4 MB }; + + blockSizeID -= 4; + if (blockSizeID > 3) return ERROR_maxBlockSize_invalid; + return blockSizes[blockSizeID]; +} + + +/************************************** + Error management +**************************************/ +int LZ4F_isError(LZ4F_errorCode_t code) +{ + return (code > (LZ4F_errorCode_t)(-ERROR_maxCode)); +} + + +/************************************** + Compression functions +**************************************/ +size_t LZ4F_compressFrameBound(size_t srcSize, const LZ4F_frameInfo_t* frameInfoPtr) +{ + const LZ4F_frameInfo_t frameInfoNull = { 0 }; + size_t headerSize; + size_t blockInfoSize; + size_t blockSize; + unsigned nbBlocks; + size_t frameSuffixSize; + size_t totalBound; + + if (frameInfoPtr==NULL) frameInfoPtr = &frameInfoNull; /* all parameters set to default */ + + headerSize = 7; /* basic header size (no option) including magic number */ + blockInfoSize = 4; /* basic blockInfo size (no option) for one block */ + + blockSize = LZ4F_getBlockSize(frameInfoPtr->maxBlockSizeID); + nbBlocks = (srcSize + (blockSize-1)) / blockSize; + blockInfoSize *= nbBlocks; /* total block info size */ + + frameSuffixSize = 4; /* basic frameSuffixSize (no option) */ + if (frameInfoPtr->contentChecksumFlag == contentChecksumEnabled) frameSuffixSize += 4; + + totalBound = headerSize + srcSize + blockInfoSize + frameSuffixSize; + if (totalBound < srcSize) return ERROR_srcSize_tooLarge; /* overflow error */ + + return totalBound; +} + + +/* LZ4F_compressFrame() + * Compress an entire srcBuffer into a valid LZ4 frame, as defined by specification v1.4.1, in a single step. + * The most important rule is that dstBuffer MUST be large enough (dstMaxSize) to ensure compression completion even in worst case. + * You can get the minimum value of dstMaxSize by using LZ4F_compressFrameBound() + * If this condition is not respected, LZ4F_compressFrame() will fail (result is an errorCode) + * The LZ4F_preferences_t structure is optional : you can provide NULL as argument. All preferences will be set to default. + * The result of the function is the number of bytes written into dstBuffer. + * The function outputs an error code if it fails (can be tested using LZ4F_isError()) + */ +size_t LZ4F_compressFrame(void* dstBuffer, size_t dstMaxSize, const void* srcBuffer, size_t srcSize, const LZ4F_preferences_t* preferencesPtr) +{ + const LZ4F_frameInfo_t frameInfoNull = { 0 }; + const LZ4F_frameInfo_t* const frameInfoPtr = (preferencesPtr==NULL) ? &frameInfoNull : &(preferencesPtr->frameInfo); + LZ4F_compressionContext_t cctx; + LZ4F_errorCode_t errorCode; + BYTE* const dstStart = (BYTE*) dstBuffer; + BYTE* dstPtr = dstStart; + size_t blockSize = LZ4F_getBlockSize(frameInfoPtr->maxBlockSizeID); + unsigned nbBlocks = (srcSize + (blockSize-1)) / blockSize; + unsigned blockNb; + const BYTE* srcPtr = (const BYTE*) srcBuffer; + const size_t dstBlockSize = LZ4F_compressBound(blockSize, frameInfoPtr); + + + if (dstMaxSize < LZ4F_compressFrameBound(srcSize, frameInfoPtr)) + return ERROR_maxDstSize_tooSmall; + + errorCode = LZ4F_createCompressionContext(&cctx, LZ4F_VERSION, preferencesPtr); + if (LZ4F_isError(errorCode)) return errorCode; + + errorCode = LZ4F_compressBegin(cctx, dstBuffer, dstMaxSize); /* write header */ + if (LZ4F_isError(errorCode)) return errorCode; + dstPtr += errorCode; /* header size */ + + for (blockNb=1; blockNb /* size_t */ + + +/************************************** Error management **************************************/ typedef size_t LZ4F_errorCode_t; -typedef enum { OK_NoError = 0, OK_FrameEnd = 1 } LZ4F_successCodes; +typedef enum { OK_FrameEnd = 1 } LZ4F_successCodes; typedef enum { OK_NoError = 0, ERROR_GENERIC = 1, ERROR_maxBlockSize_invalid, ERROR_blockMode_invalid, ERROR_contentChecksumFlag_invalid, ERROR_compressionLevel_invalid, @@ -66,17 +72,17 @@ int LZ4F_isError(LZ4F_errorCode_t code); /* Basically : code > -ERROR_maxCode **************************************/ typedef enum { LZ4F_default=0, max64KB=4, max256KB=5, max1MB=6, max4MB=7} maxBlockSize_t; -typedef enum { LZ4F_default=0, blockLinked, blockIndependent} blockMode_t; -typedef enum { LZ4F_default=0, contentChecksumEnabled, noContentChecksum} contentChecksum_t; +typedef enum { blockLinked=1, blockIndependent} blockMode_t; +typedef enum { contentChecksumEnabled, noContentChecksum} contentChecksum_t; typedef struct { - maxBlockSize_t maxBlockSize; /* max64KB, max256KB, max1MB, max4MB ; 0 == default */ + maxBlockSize_t maxBlockSizeID; /* max64KB, max256KB, max1MB, max4MB ; 0 == default */ blockMode_t blockMode; /* blockLinked, blockIndependent ; 0 == default */ - contentChecksum_t contentChecksumFlag; /* contentChecksumEnabled, noContentChecksum ; 0 == default */ + contentChecksum_t contentChecksumFlag; /* contentChecksumEnabled (default), noContentChecksum ; */ } LZ4F_frameInfo_t; typedef struct { - LZ4F_frameInfo_t frameInfo + LZ4F_frameInfo_t frameInfo; unsigned compressionLevel; /* from 0 to 16 */ unsigned autoFlush; /* 1 == automatic flush after each call to LZ4F_compress() */ } LZ4F_preferences_t; @@ -86,9 +92,9 @@ typedef struct { /********************************** * Simple compression function * *********************************/ -size_t LZ4F_compressFrameBound(size_t srcSize, const LZ4F_frameInfo_t* frameInfo); +size_t LZ4F_compressFrameBound(size_t srcSize, const LZ4F_frameInfo_t* frameInfoPtr); -size_t LZ4F_compressFrame(void* dstBuffer, size_t dstMaxSize, const void* srcBuffer, size_t srcSize, const LZ4F_preferences_t* preferences); +size_t LZ4F_compressFrame(void* dstBuffer, size_t dstMaxSize, const void* srcBuffer, size_t srcSize, const LZ4F_preferences_t* preferencesPtr); /* LZ4F_compressFrame() * Compress an entire srcBuffer into a valid LZ4 frame, as defined by specification v1.4.1, in a single step. * The most important rule is that dstBuffer MUST be large enough (dstMaxSize) to ensure compression completion even in worst case. @@ -114,7 +120,7 @@ typedef struct { /* Resource Management */ #define LZ4F_VERSION 100 -LZ4F_errorCode_t LZ4F_createCompressionContext(LZ4F_compressionContext_t* LZ4F_compressionContext, int version, const LZ4F_preferences_t* preferences); +LZ4F_errorCode_t LZ4F_createCompressionContext(LZ4F_compressionContext_t* LZ4F_compressionContextPtr, int version, const LZ4F_preferences_t* preferencesPtr); LZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_compressionContext_t LZ4F_compressionContext); /* LZ4F_createCompressionContext() : * The first thing to do is to create a compressionContext object, which will be used in all compression operations. @@ -129,7 +135,7 @@ LZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_compressionContext_t LZ4F_comp /* Compression */ -size_t LZ4F_compressBegin(LZ4F_compressionContext_t* compressionContextPtr, void* dstBuffer, size_t dstMaxSize); +size_t LZ4F_compressBegin(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize); /* LZ4F_compressBegin() : * will write the frame header into dstBuffer. * dstBuffer must be large enough to accomodate a header (dstMaxSize). Maximum header size is 15 bytes. @@ -137,15 +143,15 @@ size_t LZ4F_compressBegin(LZ4F_compressionContext_t* compressionContextPtr, void * or an error code (can be tested using LZ4F_isError()) */ -size_t LZ4F_compressBound(size_t srcSize, const LZ4F_frameInfo_t* frameInfo); -size_t LZ4F_getMaxSrcSize(size_t dstMaxSize, const LZ4F_frameInfo_t* frameInfo); +size_t LZ4F_compressBound(size_t srcSize, const LZ4F_frameInfo_t* frameInfoPtr); +size_t LZ4F_getMaxSrcSize(size_t dstMaxSize, const LZ4F_frameInfo_t* frameInfoPtr); /* LZ4F_compressBound() : gives the size of Dst buffer given a srcSize to handle worst case situations. * LZ4F_getMaxSrcSize() : gives max allowed srcSize given dstMaxSize to handle worst case situations. * You can use dstMaxSize==0 to know the "natural" srcSize instead (block size). * The LZ4F_preferences_t structure is optional : you can provide NULL as argument, all preferences will then be set to default. */ -size_t LZ4F_compress(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const void* srcBuffer, size_t srcSize, const LZ4F_compressOptions_t* compressOptions); +size_t LZ4F_compress(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const void* srcBuffer, size_t srcSize, const LZ4F_compressOptions_t* compressOptionsPtr); /* LZ4F_compress() * You can then call LZ4F_compress() repetitively to compress as much data as necessary. * The most important rule is that dstBuffer MUST be large enough (dstMaxSize) to ensure compression completion even in worst case. @@ -157,7 +163,7 @@ size_t LZ4F_compress(LZ4F_compressionContext_t compressionContext, void* dstBuff * The function outputs an error code if it fails (can be tested using LZ4F_isError()) */ -size_t LZ4F_flush(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const LZ4F_compressOptions_t* compressOptions); +size_t LZ4F_flush(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const LZ4F_compressOptions_t* compressOptionsPtr); /* LZ4F_flush() * Should you need to create compressed data immediately, without waiting for a block to be filled, * you can call LZ4_flush(), which will immediately compress any remaining data stored within compressionContext. @@ -167,7 +173,7 @@ size_t LZ4F_flush(LZ4F_compressionContext_t compressionContext, void* dstBuffer, * The LZ4F_compressOptions_t structure is optional : you can provide NULL as argument. */ -size_t LZ4F_compressEnd(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const LZ4F_compressOptions_t* compressOptions); +size_t LZ4F_compressEnd(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const LZ4F_compressOptions_t* compressOptionsPtr); /* LZ4F_compressEnd() * When you want to properly finish the compressed frame, just call LZ4F_compressEnd(). * It will flush whatever data remained within compressionContext (like LZ4_flush()) @@ -191,7 +197,7 @@ typedef struct { /* Resource management */ -LZ4F_errorCode_t LZ4F_createDecompressionContext(LZ4F_compressionContext_t* LZ4F_decompressionContext); +LZ4F_errorCode_t LZ4F_createDecompressionContext(LZ4F_compressionContext_t* LZ4F_decompressionContextPtr); LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_compressionContext_t LZ4F_decompressionContext); /* LZ4F_createDecompressionContext() : * The first thing to do is to create a decompressionContext object, which will be used in all decompression operations. @@ -203,7 +209,7 @@ LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_compressionContext_t LZ4F_de /* Decompression */ -LZ4F_errorCode_t LZ4F_getFrameInfo(LZ4F_frameInfo_t frameInfo, LZ4F_decompressionContext_t* decompressionContextPtr, void* srcBuffer, size_t* srcSize); +LZ4F_errorCode_t LZ4F_getFrameInfo(LZ4F_decompressionContext_t decompressionContext, LZ4F_frameInfo_t* frameInfoPtr, const void* srcBuffer, size_t* srcSize); /* LZ4F_getFrameInfo() * This function decodes frame header information, such as blockSize. * It is optional : you could start by calling directly LZ4F_decompress() instead. @@ -213,7 +219,7 @@ LZ4F_errorCode_t LZ4F_getFrameInfo(LZ4F_frameInfo_t frameInfo, LZ4F_decompressio * The function result is an error code which can be tested using LZ4F_isError(). */ -LZ4F_errorCode_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContext, void* dstBuffer, size_t* dstSize, const void* srcBuffer, size_t* srcSize, const LZ4F_decompressOptions_t* decompressOptions); +LZ4F_errorCode_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContext, void* dstBuffer, size_t* dstSize, const void* srcBuffer, size_t* srcSize, const LZ4F_decompressOptions_t* decompressOptionsPtr); /* LZ4F_decompress() * Call this function repetitively to regenerate data compressed within srcBuffer. * The function will attempt to decode *srcSize from srcBuffer, into dstBuffer of maximum size *dstSize. -- cgit v0.12 From c39235f46e1e23b90a2bc6d6eec509c2c70f149f Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 29 Aug 2014 16:38:26 +0100 Subject: fix : lz4frame : added default blockSizeID --- lz4frame.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lz4frame.c b/lz4frame.c index 8af9c28..b3c36a5 100644 --- a/lz4frame.c +++ b/lz4frame.c @@ -141,6 +141,8 @@ typedef struct {size_t v;} _PACKED size_t_S; #define MB *(1<<20) #define GB *(1<<30) +#define LZ4F_BLOCKSIZEID_DEFAULT 4 + /************************************** Structures and local types @@ -159,6 +161,7 @@ static size_t LZ4F_getBlockSize(unsigned blockSizeID) { static const size_t blockSizes[4] = { 64 KB, 256 KB, 1 MB, 4 MB }; + if (blockSizeID == 0) blockSizeID = LZ4F_BLOCKSIZEID_DEFAULT; blockSizeID -= 4; if (blockSizeID > 3) return ERROR_maxBlockSize_invalid; return blockSizes[blockSizeID]; -- cgit v0.12 From 14a28c063319a8d7f5da59db6b6a6ef25336b726 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sat, 30 Aug 2014 12:32:09 +0100 Subject: minor fuzzer cleaning --- programs/fuzzer.c | 37 ++++++------------------------------- 1 file changed, 6 insertions(+), 31 deletions(-) diff --git a/programs/fuzzer.c b/programs/fuzzer.c index b302078..6dff4cb 100644 --- a/programs/fuzzer.c +++ b/programs/fuzzer.c @@ -85,9 +85,9 @@ -//************************************** -// Macros -//************************************** +/***************************************** + Macros +*****************************************/ #define DISPLAY(...) fprintf(stderr, __VA_ARGS__) #define DISPLAYLEVEL(l, ...) if (displayLevel>=l) { DISPLAY(__VA_ARGS__); } @@ -135,7 +135,7 @@ unsigned int FUZ_rand(unsigned int* src) #define FUZ_RAND15BITS ((FUZ_rand(seed) >> 3) & 32767) -#define FUZ_RANDLENGTH ( ((FUZ_rand(seed) >> 7) & 3) ? (FUZ_rand(seed) % 14) : (FUZ_rand(seed) & 511) + 15) +#define FUZ_RANDLENGTH ( ((FUZ_rand(seed) >> 7) & 3) ? (FUZ_rand(seed) % 15) : (FUZ_rand(seed) % 510) + 15) void FUZ_fillCompressibleNoiseBuffer(void* buffer, int bufferSize, double proba, U32* seed) { BYTE* BBuffer = (BYTE*)buffer; @@ -173,33 +173,9 @@ void FUZ_fillCompressibleNoiseBuffer(void* buffer, int bufferSize, double proba, } -// No longer useful; included into issue 134 -int FUZ_Issue52(void) -{ - char* output; - char* input; - int i, r; - - // Overflow test, by Ludwig Strigeus - printf("Overflow test (issue 52)..."); - input = (char*) malloc (20<<20); - output = (char*) malloc (20<<20); - input[0] = 0x0F; - input[1] = 0x00; - input[2] = 0x00; - for(i = 3; i < 16840000; i++) input[i] = 0xff; - r = LZ4_decompress_safe(input, output, 20<<20, 20<<20); - - free(input); - free(output); - printf(" Passed (return = %i < 0)\n",r); - return 0; -} - - #define MAX_NB_BUFF_I134 150 #define BLOCKSIZE_I134 (32 MB) -int FUZ_Issue134(void) +int FUZ_AddressOverflow(void) { char* buffers[MAX_NB_BUFF_I134+1] = {0}; int i, nbBuff=0; @@ -762,8 +738,7 @@ int main(int argc, char** argv) { printf("Seed = %u\n", seed); if (proba!=FUZ_COMPRESSIBILITY_DEFAULT) printf("Compressibility : %i%%\n", proba); - //FUZ_Issue52(); - FUZ_Issue134(); + FUZ_AddressOverflow(); if (nbTests<=0) nbTests=1; -- cgit v0.12 From 3b4e3f2b01df752d82a51d7b7afaa9a2fd5a5ff8 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sat, 30 Aug 2014 18:13:45 +0100 Subject: minor fuzzer code cleanup --- programs/fuzzer.c | 34 +++++++++++++++++----------------- programs/lz4io.c | 2 +- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/programs/fuzzer.c b/programs/fuzzer.c index 6dff4cb..f19382c 100644 --- a/programs/fuzzer.c +++ b/programs/fuzzer.c @@ -219,31 +219,31 @@ int FUZ_AddressOverflow(void) char* input = buffers[nbBuff-1]; char* output = buffers[nbBuff]; int r; - input[0] = 0xF0; // Literal length overflow - input[1] = 0xFF; - input[2] = 0xFF; - input[3] = 0xFF; - for(i = 4; i <= nbOf255+4; i++) input[i] = 0xff; + input[0] = (char)0xF0; // Literal length overflow + input[1] = (char)0xFF; + input[2] = (char)0xFF; + input[3] = (char)0xFF; + for(i = 4; i <= nbOf255+4; i++) input[i] = (char)0xff; r = LZ4_decompress_safe(input, output, nbOf255+64, BLOCKSIZE_I134); if (r>0) goto _overflowError; - input[0] = 0x1F; // Match length overflow - input[1] = 0x01; - input[2] = 0x01; - input[3] = 0x00; + input[0] = (char)0x1F; // Match length overflow + input[1] = (char)0x01; + input[2] = (char)0x01; + input[3] = (char)0x00; r = LZ4_decompress_safe(input, output, nbOf255+64, BLOCKSIZE_I134); if (r>0) goto _overflowError; output = buffers[nbBuff-2]; // Reverse in/out pointer order - input[0] = 0xF0; // Literal length overflow - input[1] = 0xFF; - input[2] = 0xFF; - input[3] = 0xFF; + input[0] = (char)0xF0; // Literal length overflow + input[1] = (char)0xFF; + input[2] = (char)0xFF; + input[3] = (char)0xFF; r = LZ4_decompress_safe(input, output, nbOf255+64, BLOCKSIZE_I134); if (r>0) goto _overflowError; - input[0] = 0x1F; // Match length overflow - input[1] = 0x01; - input[2] = 0x01; - input[3] = 0x00; + input[0] = (char)0x1F; // Match length overflow + input[1] = (char)0x01; + input[2] = (char)0x01; + input[3] = (char)0x00; r = LZ4_decompress_safe(input, output, nbOf255+64, BLOCKSIZE_I134); if (r>0) goto _overflowError; } diff --git a/programs/lz4io.c b/programs/lz4io.c index 2715972..20520d6 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -233,7 +233,7 @@ int LZ4IO_setNotificationLevel(int level) /* ************************************************************************ */ -/* ********************** LZ4 File / Stream compression ******************* */ +/* ********************** LZ4 File / Pipe compression ********************* */ /* ************************************************************************ */ static int LZ4S_GetBlockSize_FromBlockId (int id) { return (1 << (8 + (2 * id))); } -- cgit v0.12 From f66721d303a6141797ee1e04067a007aa0392daf Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sat, 30 Aug 2014 18:14:44 +0100 Subject: lz4frame compression API v1 completed Added : test program frametest --- lz4frame.c | 431 ++++++++++++++++++++++++++++++++++++++++++--------- lz4frame.h | 17 +- programs/Makefile | 12 +- programs/frametest.c | 327 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 703 insertions(+), 84 deletions(-) create mode 100644 programs/frametest.c diff --git a/lz4frame.c b/lz4frame.c index b3c36a5..39c9402 100644 --- a/lz4frame.c +++ b/lz4frame.c @@ -36,22 +36,6 @@ * All related operations, including memory management, are handled by the library. * You don't need lz4.h when using lz4frame.h. * */ - - -/************************************** - CPU Feature Detection -**************************************/ -/* 32 or 64 bits ? */ -static const int LZ4F_32bits = (sizeof(void*)==4); -static const int LZ4F_64bits = (sizeof(void*)==8); - -/* Little Endian or Big Endian ? */ -typedef union { - int num; - char bytes[4]; - } endian_t; -static const endian_t endianTest = { .num = 1 }; -#define LZ4F_isLittleEndian (endianTest.bytes[0]) /************************************** @@ -62,6 +46,10 @@ static const endian_t endianTest = { .num = 1 }; #endif #define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) +#ifdef __GNUC__ +# pragma GCC diagnostic ignored "-Wmissing-braces" /* GCC bug 53119 : doesn't accept { 0 } as initializer (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53119) */ +# pragma GCC diagnostic ignored "-Wmissing-field-initializers" /* GCC bug 53119 : doesn't accept { 0 } as initializer (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53119) */ +#endif /************************************** @@ -101,38 +89,6 @@ static const endian_t endianTest = { .num = 1 }; typedef unsigned long long U64; #endif -#if defined(__GNUC__) -# define _PACKED __attribute__ ((packed)) -#else -# define _PACKED -#endif - -#if !defined(__GNUC__) -# if defined(__IBMC__) || defined(__SUNPRO_C) || defined(__SUNPRO_CC) -# pragma pack(1) -# else -# pragma pack(push, 1) -# endif -#endif - -typedef struct { U16 v; } _PACKED U16_S; -typedef struct { U32 v; } _PACKED U32_S; -typedef struct { U64 v; } _PACKED U64_S; -typedef struct {size_t v;} _PACKED size_t_S; - -#if !defined(__GNUC__) -# if defined(__SUNPRO_C) || defined(__SUNPRO_CC) -# pragma pack(0) -# else -# pragma pack(pop) -# endif -#endif - -#define A16(x) (((U16_S *)(x))->v) -#define A32(x) (((U32_S *)(x))->v) -#define A64(x) (((U64_S *)(x))->v) -#define AARCH(x) (((size_t_S *)(x))->v) - /************************************** Constants @@ -141,12 +97,30 @@ typedef struct {size_t v;} _PACKED size_t_S; #define MB *(1<<20) #define GB *(1<<30) +#define _1BIT 0x01 +#define _2BITS 0x03 +#define _3BITS 0x07 +#define _4BITS 0x0F +#define _8BITS 0xFF + +#define LZ4F_MAGICNUMBER 0x184D2204U +#define LZ4F_BLOCKUNCOMPRESSED_FLAG 0x80000000U +#define LZ4F_MAXHEADERFRAME_SIZE 19 #define LZ4F_BLOCKSIZEID_DEFAULT 4 /************************************** Structures and local types **************************************/ +typedef struct { + LZ4F_preferences_t prefs; + unsigned version; + unsigned cStage; + size_t maxBlockSize; + XXH32_stateSpace_t xxh; + BYTE* tmpInputBuffer; + size_t tmpInputFilled; +} LZ4F_cctx_internal_t; /************************************** @@ -160,14 +134,32 @@ typedef struct {size_t v;} _PACKED size_t_S; static size_t LZ4F_getBlockSize(unsigned blockSizeID) { static const size_t blockSizes[4] = { 64 KB, 256 KB, 1 MB, 4 MB }; - + if (blockSizeID == 0) blockSizeID = LZ4F_BLOCKSIZEID_DEFAULT; blockSizeID -= 4; - if (blockSizeID > 3) return ERROR_maxBlockSize_invalid; + if (blockSizeID > 3) return -ERROR_maxBlockSize_invalid; return blockSizes[blockSizeID]; } +/* unoptimized version; solves endianess & alignment issues */ +static void LZ4F_writeLE32 (BYTE* dstPtr, U32 value32) +{ + dstPtr[0] = (BYTE)value32; + dstPtr[1] = (BYTE)(value32 >> 8); + dstPtr[2] = (BYTE)(value32 >> 16); + dstPtr[3] = (BYTE)(value32 >> 24); +} + + +static BYTE LZ4F_headerChecksum (BYTE* header, size_t length) +{ + U32 xxh = XXH32(header, length, 0); + return (BYTE)(xxh >> 8); +} + + + /************************************** Error management **************************************/ @@ -178,7 +170,7 @@ int LZ4F_isError(LZ4F_errorCode_t code) /************************************** - Compression functions + Simple compression functions **************************************/ size_t LZ4F_compressFrameBound(size_t srcSize, const LZ4F_frameInfo_t* frameInfoPtr) { @@ -189,22 +181,22 @@ size_t LZ4F_compressFrameBound(size_t srcSize, const LZ4F_frameInfo_t* frameInfo unsigned nbBlocks; size_t frameSuffixSize; size_t totalBound; - + if (frameInfoPtr==NULL) frameInfoPtr = &frameInfoNull; /* all parameters set to default */ - + headerSize = 7; /* basic header size (no option) including magic number */ blockInfoSize = 4; /* basic blockInfo size (no option) for one block */ - - blockSize = LZ4F_getBlockSize(frameInfoPtr->maxBlockSizeID); + + blockSize = LZ4F_getBlockSize(frameInfoPtr->blockSizeID); nbBlocks = (srcSize + (blockSize-1)) / blockSize; blockInfoSize *= nbBlocks; /* total block info size */ - + frameSuffixSize = 4; /* basic frameSuffixSize (no option) */ if (frameInfoPtr->contentChecksumFlag == contentChecksumEnabled) frameSuffixSize += 4; - + totalBound = headerSize + srcSize + blockInfoSize + frameSuffixSize; - if (totalBound < srcSize) return ERROR_srcSize_tooLarge; /* overflow error */ - + if (totalBound < srcSize) return -ERROR_srcSize_tooLarge; /* overflow error */ + return totalBound; } @@ -222,27 +214,27 @@ size_t LZ4F_compressFrame(void* dstBuffer, size_t dstMaxSize, const void* srcBuf { const LZ4F_frameInfo_t frameInfoNull = { 0 }; const LZ4F_frameInfo_t* const frameInfoPtr = (preferencesPtr==NULL) ? &frameInfoNull : &(preferencesPtr->frameInfo); - LZ4F_compressionContext_t cctx; + LZ4F_compressionContext_t cctx = NULL; LZ4F_errorCode_t errorCode; BYTE* const dstStart = (BYTE*) dstBuffer; BYTE* dstPtr = dstStart; - size_t blockSize = LZ4F_getBlockSize(frameInfoPtr->maxBlockSizeID); + size_t blockSize = LZ4F_getBlockSize(frameInfoPtr->blockSizeID); unsigned nbBlocks = (srcSize + (blockSize-1)) / blockSize; unsigned blockNb; const BYTE* srcPtr = (const BYTE*) srcBuffer; const size_t dstBlockSize = LZ4F_compressBound(blockSize, frameInfoPtr); - + if (dstMaxSize < LZ4F_compressFrameBound(srcSize, frameInfoPtr)) - return ERROR_maxDstSize_tooSmall; - + return -ERROR_dstMaxSize_tooSmall; + errorCode = LZ4F_createCompressionContext(&cctx, LZ4F_VERSION, preferencesPtr); if (LZ4F_isError(errorCode)) return errorCode; - + errorCode = LZ4F_compressBegin(cctx, dstBuffer, dstMaxSize); /* write header */ if (LZ4F_isError(errorCode)) return errorCode; dstPtr += errorCode; /* header size */ - + for (blockNb=1; blockNbprefs = *preferencesPtr; /* equivalent to memcpy() */ + cctxPtr->version = version; + cctxPtr->cStage = 0; /* Next stage : write header */ + if (cctxPtr->prefs.frameInfo.blockSizeID == 0) cctxPtr->prefs.frameInfo.blockSizeID = LZ4F_BLOCKSIZEID_DEFAULT; + cctxPtr->maxBlockSize = LZ4F_getBlockSize(cctxPtr->prefs.frameInfo.blockSizeID); + cctxPtr->tmpInputBuffer = malloc(cctxPtr->maxBlockSize); + if (cctxPtr->tmpInputBuffer == NULL) return -ERROR_allocation_failed; + cctxPtr->tmpInputFilled = 0; + XXH32_resetState(&(cctxPtr->xxh), 0); + + *LZ4F_compressionContextPtr = (LZ4F_compressionContext_t)cctxPtr; + + return OK_NoError; +} + + +LZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_compressionContext_t LZ4F_compressionContext) +{ + LZ4F_cctx_internal_t* cctxPtr = (LZ4F_cctx_internal_t*)LZ4F_compressionContext; + + free(cctxPtr->tmpInputBuffer); + free(LZ4F_compressionContext); + + return OK_NoError; +} + + +/* LZ4F_compressBegin() : + * will write the frame header into dstBuffer. + * dstBuffer must be large enough to accomodate a header (dstMaxSize). Maximum header size is LZ4F_MAXHEADERFRAME_SIZE(19) bytes. + * The result of the function is the number of bytes written into dstBuffer for the header + * or an error code (can be tested using LZ4F_isError()) + */ +size_t LZ4F_compressBegin(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize) +{ + LZ4F_cctx_internal_t* cctxPtr = (LZ4F_cctx_internal_t*)compressionContext; + BYTE* const dstStart = (BYTE*)dstBuffer; + BYTE* dstPtr = dstStart; + BYTE* headerStart; + + if (dstMaxSize < LZ4F_MAXHEADERFRAME_SIZE) return -ERROR_dstMaxSize_tooSmall; + if (cctxPtr->cStage != 0) return -ERROR_GENERIC; + + /* Magic Number */ + LZ4F_writeLE32(dstPtr, LZ4F_MAGICNUMBER); + dstPtr += 4; + headerStart = dstPtr; + + /* FLG Byte */ + *dstPtr = (1 & _2BITS) << 6; /* Version('01') */ + *dstPtr |= (1 & _1BIT ) << 5; /* Blocks independents */ + *dstPtr |= (1 & _1BIT ) << 2; /* Stream checksum */ + dstPtr++; + /* BD Byte */ + *dstPtr = (char)((cctxPtr->prefs.frameInfo.blockSizeID & _3BITS) << 4); + dstPtr++; + /* CRC Byte */ + *dstPtr = LZ4F_headerChecksum(headerStart, 2); + dstPtr++; + + cctxPtr->cStage = 1; /* header written */ + + return (dstPtr - dstStart); +} + + +/* LZ4F_compressBound() : gives the size of Dst buffer given a srcSize to handle worst case situations. + * The LZ4F_frameInfo_t structure is optional : + * you can provide NULL as argument, all preferences will then be set to default. + * */ +size_t LZ4F_compressBound(size_t srcSize, const LZ4F_frameInfo_t* frameInfoPtr) +{ + blockSizeID_t bid = (frameInfoPtr==NULL) ? LZ4F_BLOCKSIZEID_DEFAULT : frameInfoPtr->blockSizeID; + size_t blockSize = LZ4F_getBlockSize(bid); + size_t vSrcSize = srcSize + (blockSize-1); /* worst case : tmp buffer almost filled */ + unsigned nbBlocks = vSrcSize / blockSize; + size_t blockInfo = 4; /* default, without block CRC option */ + + return (blockSize + blockInfo) * nbBlocks; +} + +/* LZ4F_getMaxSrcSize() : gives max allowed srcSize given dstMaxSize to handle worst case situations. + * You can use dstMaxSize==0 to know the "natural" srcSize instead (block size). + * The LZ4F_frameInfo_t structure is optional : + * you can provide NULL as argument, all preferences will then be set to default. + * */ +size_t LZ4F_getMaxSrcSize(size_t dstMaxSize, const LZ4F_frameInfo_t* frameInfoPtr) +{ + blockSizeID_t bid = (frameInfoPtr==NULL) ? LZ4F_BLOCKSIZEID_DEFAULT : frameInfoPtr->blockSizeID; + size_t blockSize = LZ4F_getBlockSize(bid); + size_t worstCBlockSize = blockSize + 4; /* default, with no block CRC option */ + unsigned nbBlocks = dstMaxSize / worstCBlockSize; + size_t maxSrcSize = nbBlocks * blockSize; + + if (dstMaxSize == 0) return blockSize; + if (nbBlocks == 0) return -ERROR_dstMaxSize_tooSmall; /* can't even fit one block */ + + return maxSrcSize; +} + + +/* LZ4F_compress() + * You can then call LZ4F_compress() repetitively to compress as much data as necessary. + * The most important rule is that dstBuffer MUST be large enough (dstMaxSize) to ensure compression completion even in worst case. + * You can get the minimum value of dstMaxSize by using LZ4F_compressBound() + * Conversely, given a fixed dstMaxSize value, you can know the maximum srcSize authorized using LZ4F_getMaxSrcSize() + * If this condition is not respected, LZ4F_compress() will fail (result is an errorCode) + * The LZ4F_compressOptions_t structure is optional : you can provide NULL as argument. + * The result of the function is the number of bytes written into dstBuffer (it can be zero, meaning input data is just stored within compressionContext for a future block to complete) + * The function outputs an error code if it fails (can be tested using LZ4F_isError()) + */ +size_t LZ4F_compress(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const void* srcBuffer, size_t srcSize, const LZ4F_compressOptions_t* compressOptionsPtr) +{ + LZ4F_compressOptions_t cOptionsNull = { 0 }; + LZ4F_cctx_internal_t* cctxPtr = (LZ4F_cctx_internal_t*)compressionContext; + size_t blockSize = cctxPtr->maxBlockSize; + const BYTE* srcPtr = (const BYTE*)srcBuffer; + const BYTE* const srcEnd = srcPtr + srcSize; + BYTE* const dstStart = (BYTE*)dstBuffer; + BYTE* dstPtr = dstStart; + + + if (cctxPtr->cStage != 1) return -ERROR_GENERIC; + if (dstMaxSize < LZ4F_compressBound(srcSize, &(cctxPtr->prefs.frameInfo))) return -ERROR_dstMaxSize_tooSmall; + if (compressOptionsPtr == NULL) compressOptionsPtr = &cOptionsNull; + + /* complete tmp buffer */ + if (cctxPtr->tmpInputFilled > 0) + { + size_t sizeToCopy = blockSize - cctxPtr->tmpInputFilled; + if (sizeToCopy > srcSize) + { + /* add to tmp buffer */ + memcpy(cctxPtr->tmpInputBuffer + cctxPtr->tmpInputFilled, srcBuffer, srcSize); + srcPtr = srcEnd; + cctxPtr->tmpInputFilled += srcSize; + } + else + { + BYTE* cSizePtr = dstPtr; + U32 cSize; + memcpy(cctxPtr->tmpInputBuffer + cctxPtr->tmpInputFilled, srcBuffer, sizeToCopy); + srcPtr += sizeToCopy; + dstPtr += 4; /* space for cSizePtr */ + cSize = (U32)LZ4_compress_limitedOutput((const char*)cctxPtr->tmpInputBuffer, (char*)dstPtr, (int)(blockSize), (int)(blockSize-1)); + dstPtr += cSize; + LZ4F_writeLE32(cSizePtr, cSize); + if (cSize == 0) /* compression failed */ + { + cSize = blockSize + LZ4F_BLOCKUNCOMPRESSED_FLAG; + LZ4F_writeLE32(cSizePtr, cSize); + memcpy(dstPtr, cctxPtr->tmpInputBuffer, blockSize); + dstPtr += blockSize; + } + cctxPtr->tmpInputFilled = 0; + } + } + + while ((size_t)(srcEnd - srcPtr) >= blockSize) + { + /* compress one block */ + BYTE* cSizePtr = dstPtr; + U32 cSize; + dstPtr += 4; /* space for cSizePtr */ + cSize = (U32)LZ4_compress_limitedOutput((const char*)srcPtr, (char*)dstPtr, (int)(blockSize), (int)(blockSize-1)); + dstPtr += cSize; + LZ4F_writeLE32(cSizePtr, cSize); + if (cSize == 0) /* compression failed */ + { + cSize = blockSize + LZ4F_BLOCKUNCOMPRESSED_FLAG; + LZ4F_writeLE32(cSizePtr, cSize); + memcpy(dstPtr, srcPtr, blockSize); + dstPtr += blockSize; + } + srcPtr += blockSize; + } + + if (srcPtr < srcEnd) + { + /* fill tmp buffer */ + size_t sizeToCopy = srcEnd - srcPtr; + memcpy(cctxPtr->tmpInputBuffer, srcPtr, sizeToCopy); + cctxPtr->tmpInputFilled = sizeToCopy; + } + + if (cctxPtr->prefs.frameInfo.contentChecksumFlag == contentChecksumEnabled) + XXH32_update(&(cctxPtr->xxh), srcBuffer, (unsigned)srcSize); + + return dstPtr - dstStart; +} + + +/* LZ4F_flush() + * Should you need to create compressed data immediately, without waiting for a block to be filled, + * you can call LZ4_flush(), which will immediately compress any remaining data stored within compressionContext. + * The result of the function is the number of bytes written into dstBuffer + * (it can be zero, this means there was no data left within compressionContext) + * The function outputs an error code if it fails (can be tested using LZ4F_isError()) + * The LZ4F_compressOptions_t structure is optional : you can provide NULL as argument. + */ +size_t LZ4F_flush(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const LZ4F_compressOptions_t* compressOptionsPtr) +{ + LZ4F_compressOptions_t cOptionsNull = { 0 }; + LZ4F_cctx_internal_t* cctxPtr = (LZ4F_cctx_internal_t*)compressionContext; + BYTE* const dstStart = (BYTE*)dstBuffer; + BYTE* dstPtr = dstStart; + + + if (cctxPtr->tmpInputFilled == 0) return 0; /* nothing to flush */ + if (cctxPtr->cStage != 1) return -ERROR_GENERIC; + if (dstMaxSize < LZ4F_compressBound(1, &(cctxPtr->prefs.frameInfo))) return -ERROR_dstMaxSize_tooSmall; + if (compressOptionsPtr == NULL) compressOptionsPtr = &cOptionsNull; + + { + BYTE* cSizePtr = dstPtr; + U32 cSize; + dstPtr += 4; /* space for cSizePtr */ + cSize = (U32)LZ4_compress_limitedOutput((const char*)cctxPtr->tmpInputBuffer, (char*)dstPtr, (int)(cctxPtr->tmpInputFilled), (int)(cctxPtr->tmpInputFilled-1)); + dstPtr += cSize; + LZ4F_writeLE32(cSizePtr, cSize); + if (cSize == 0) /* compression failed */ + { + cSize = cctxPtr->tmpInputFilled + LZ4F_BLOCKUNCOMPRESSED_FLAG; + LZ4F_writeLE32(cSizePtr, cSize); + memcpy(dstPtr, cctxPtr->tmpInputBuffer, cctxPtr->tmpInputFilled); + dstPtr += cctxPtr->tmpInputFilled; + } + cctxPtr->tmpInputFilled = 0; + } + + return dstPtr - dstStart; +} + + +/* LZ4F_compressEnd() + * When you want to properly finish the compressed frame, just call LZ4F_compressEnd(). + * It will flush whatever data remained within compressionContext (like LZ4_flush()) + * but also properly finalize the frame, with an endMark and a checksum. + * The result of the function is the number of bytes written into dstBuffer (necessarily >= 4 (endMark size)) + * The function outputs an error code if it fails (can be tested using LZ4F_isError()) + * The LZ4F_compressOptions_t structure is optional : you can provide NULL as argument. + * compressionContext can then be used again, starting with LZ4F_compressBegin(). The preferences will remain the same. + */ +size_t LZ4F_compressEnd(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const LZ4F_compressOptions_t* compressOptionsPtr) +{ + LZ4F_cctx_internal_t* cctxPtr = (LZ4F_cctx_internal_t*)compressionContext; + BYTE* const dstStart = (BYTE*)dstBuffer; + BYTE* dstPtr = dstStart; + size_t errorCode; + + errorCode = LZ4F_flush(compressionContext, dstBuffer, dstMaxSize, compressOptionsPtr); + if (LZ4F_isError(errorCode)) return errorCode; + dstPtr += errorCode; + + LZ4F_writeLE32(dstPtr, 0); dstPtr+=4; /* endMark */ + + if (cctxPtr->prefs.frameInfo.contentChecksumFlag == contentChecksumEnabled) + { + U32 xxh = XXH32_intermediateDigest(&(cctxPtr->xxh)); + LZ4F_writeLE32(dstPtr, xxh); + dstPtr+=4; /* content Checksum */ + } + + cctxPtr->cStage = 0; /* state is now re-usable (with identical preferences) */ + + return dstPtr - dstStart; +} diff --git a/lz4frame.h b/lz4frame.h index c0fa4b4..851de45 100644 --- a/lz4frame.h +++ b/lz4frame.h @@ -58,8 +58,9 @@ typedef size_t LZ4F_errorCode_t; typedef enum { OK_FrameEnd = 1 } LZ4F_successCodes; typedef enum { OK_NoError = 0, ERROR_GENERIC = 1, ERROR_maxBlockSize_invalid, ERROR_blockMode_invalid, ERROR_contentChecksumFlag_invalid, + ERROR_srcSize_tooLarge, ERROR_dstMaxSize_tooSmall, + ERROR_allocation_failed, ERROR_compressionLevel_invalid, - ERROR_srcSize_tooLarge, ERROR_maxDstSize_tooSmall, ERROR_maxCode } LZ4F_errorCodes; /* error codes are negative unsigned values. Compare function result to (-specificCode) */ @@ -71,14 +72,14 @@ int LZ4F_isError(LZ4F_errorCode_t code); /* Basically : code > -ERROR_maxCode Framing compression functions **************************************/ -typedef enum { LZ4F_default=0, max64KB=4, max256KB=5, max1MB=6, max4MB=7} maxBlockSize_t; +typedef enum { LZ4F_default=0, max64KB=4, max256KB=5, max1MB=6, max4MB=7} blockSizeID_t; typedef enum { blockLinked=1, blockIndependent} blockMode_t; typedef enum { contentChecksumEnabled, noContentChecksum} contentChecksum_t; typedef struct { - maxBlockSize_t maxBlockSizeID; /* max64KB, max256KB, max1MB, max4MB ; 0 == default */ - blockMode_t blockMode; /* blockLinked, blockIndependent ; 0 == default */ - contentChecksum_t contentChecksumFlag; /* contentChecksumEnabled (default), noContentChecksum ; */ + blockSizeID_t blockSizeID; /* max64KB, max256KB, max1MB, max4MB ; 0 == default */ + blockMode_t blockMode; /* blockLinked, blockIndependent ; 0 == default */ + contentChecksum_t contentChecksumFlag; /* contentChecksumEnabled (default), noContentChecksum ; */ } LZ4F_frameInfo_t; typedef struct { @@ -89,7 +90,7 @@ typedef struct { -/********************************** +/*********************************** * Simple compression function * *********************************/ size_t LZ4F_compressFrameBound(size_t srcSize, const LZ4F_frameInfo_t* frameInfoPtr); @@ -138,7 +139,7 @@ LZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_compressionContext_t LZ4F_comp size_t LZ4F_compressBegin(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize); /* LZ4F_compressBegin() : * will write the frame header into dstBuffer. - * dstBuffer must be large enough to accomodate a header (dstMaxSize). Maximum header size is 15 bytes. + * dstBuffer must be large enough to accomodate a header (dstMaxSize). Maximum header size is 19 bytes. * The result of the function is the number of bytes written into dstBuffer for the header * or an error code (can be tested using LZ4F_isError()) */ @@ -148,7 +149,7 @@ size_t LZ4F_getMaxSrcSize(size_t dstMaxSize, const LZ4F_frameInfo_t* frameInfoPt /* LZ4F_compressBound() : gives the size of Dst buffer given a srcSize to handle worst case situations. * LZ4F_getMaxSrcSize() : gives max allowed srcSize given dstMaxSize to handle worst case situations. * You can use dstMaxSize==0 to know the "natural" srcSize instead (block size). - * The LZ4F_preferences_t structure is optional : you can provide NULL as argument, all preferences will then be set to default. + * The LZ4F_frameInfo_t structure is optional : you can provide NULL as argument, all preferences will then be set to default. */ size_t LZ4F_compress(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const void* srcBuffer, size_t srcSize, const LZ4F_compressOptions_t* compressOptionsPtr); diff --git a/programs/Makefile b/programs/Makefile index a04c323..441d538 100644 --- a/programs/Makefile +++ b/programs/Makefile @@ -30,13 +30,13 @@ # fullbench32: Same as fullbench, but forced to compile in 32-bits mode # ########################################################################## -RELEASE=r122 +RELEASE=r123 DESTDIR?= PREFIX ?= /usr CC := $(CC) CFLAGS ?= -O3 -CFLAGS += -std=c99 -Wall -Wextra -Wundef -Wshadow -Wstrict-prototypes -DLZ4_VERSION=\"$(RELEASE)\" +CFLAGS += -std=c99 -Wall -Wextra -Wundef -Wshadow -Wstrict-prototypes -Wpedantic -DLZ4_VERSION=\"$(RELEASE)\" FLAGS = -I.. $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) BINDIR=$(PREFIX)/bin @@ -68,7 +68,7 @@ endif default: lz4 lz4c -all: lz4 lz4c lz4c32 fullbench fullbench32 fuzzer fuzzer32 datagen +all: lz4 lz4c lz4c32 fullbench fullbench32 fuzzer fuzzer32 frametest datagen lz4: $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c bench.c xxhash.c lz4io.c lz4cli.c $(CC) $(FLAGS) -DDISABLE_LZ4C_LEGACY_OPTIONS $^ -o $@$(EXT) @@ -91,6 +91,9 @@ fuzzer : $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c xxhash.c fuzzer.c fuzzer32: $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c xxhash.c fuzzer.c $(CC) -m32 $(FLAGS) $^ -o $@$(EXT) +frametest: $(LZ4DIR)/lz4frame.c $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c xxhash.c frametest.c + $(CC) $(FLAGS) $^ -o $@$(EXT) + datagen : datagen.c $(CC) $(FLAGS) $^ -o $@$(EXT) @@ -99,7 +102,8 @@ clean: @rm -f core *.o \ lz4$(EXT) lz4c$(EXT) lz4c32$(EXT) \ fullbench$(EXT) fullbench32$(EXT) \ - fuzzer$(EXT) fuzzer32$(EXT) datagen$(EXT) + fuzzer$(EXT) fuzzer32$(EXT) \ + frametest$(EXT) datagen$(EXT) @echo Cleaning completed diff --git a/programs/frametest.c b/programs/frametest.c new file mode 100644 index 0000000..862ecf4 --- /dev/null +++ b/programs/frametest.c @@ -0,0 +1,327 @@ +/* + frameTest - test tool for lz4frame + Copyright (C) Yann Collet 2014 + GPL v2 License + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + You can contact the author at : + - LZ4 source repository : http://code.google.com/p/lz4/ + - LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c +*/ + +/************************************** + Compiler specific +**************************************/ +#define _CRT_SECURE_NO_WARNINGS // fgets +#ifdef _MSC_VER /* Visual Studio */ +# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ +# pragma warning(disable : 4146) /* disable: C4146: minus unsigned expression */ +#endif + + +/************************************** + Includes +**************************************/ +#include +#include // fgets, sscanf +#include // timeb +#include // strcmp +#include "lz4frame.h" + + +/************************************** + Basic Types +**************************************/ +#if defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */ +# include + typedef uint8_t BYTE; + typedef uint16_t U16; + typedef uint32_t U32; + typedef int32_t S32; + typedef uint64_t U64; +#else + typedef unsigned char BYTE; + typedef unsigned short U16; + typedef unsigned int U32; + typedef signed int S32; + typedef unsigned long long U64; +#endif + + +/************************************** + Constants +**************************************/ +#ifndef LZ4_VERSION +# define LZ4_VERSION "" +#endif + +#define NB_ATTEMPTS (1<<16) +#define COMPRESSIBLE_NOISE_LENGTH (1 << 21) +#define FUZ_MAX_BLOCK_SIZE (1 << 17) +#define FUZ_MAX_DICT_SIZE (1 << 15) +#define FUZ_COMPRESSIBILITY_DEFAULT 50 +#define PRIME1 2654435761U +#define PRIME2 2246822519U +#define PRIME3 3266489917U + +#define KB *(1U<<10) +#define MB *(1U<<20) +#define GB *(1U<<30) + + +/************************************** + Macros +**************************************/ +#define DISPLAY(...) fprintf(stderr, __VA_ARGS__) +#define DISPLAYLEVEL(l, ...) if (displayLevel>=l) { DISPLAY(__VA_ARGS__); } + + +/***************************************** + Local Parameters +*****************************************/ +static int no_prompt = 0; +static char* programName; +static int displayLevel = 2; + + +/********************************************************* + Fuzzer functions +*********************************************************/ +static int FUZ_GetMilliStart(void) +{ + struct timeb tb; + int nCount; + ftime( &tb ); + nCount = (int) (tb.millitm + (tb.time & 0xfffff) * 1000); + return nCount; +} + + +static int FUZ_GetMilliSpan( int nTimeStart ) +{ + int nSpan = FUZ_GetMilliStart() - nTimeStart; + if ( nSpan < 0 ) + nSpan += 0x100000 * 1000; + return nSpan; +} + + +# define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r))) +unsigned int FUZ_rand(unsigned int* src) +{ + U32 rand32 = *src; + rand32 *= PRIME1; + rand32 += PRIME2; + rand32 = FUZ_rotl32(rand32, 13); + *src = rand32; + return rand32 >> 3; +} + + +#define FUZ_RAND15BITS ((FUZ_rand(seed) >> 3) & 32767) +#define FUZ_RANDLENGTH ( ((FUZ_rand(seed) >> 7) & 3) ? (FUZ_rand(seed) % 14) : (FUZ_rand(seed) & 511) + 15) +void FUZ_fillCompressibleNoiseBuffer(void* buffer, int bufferSize, double proba, U32* seed) +{ + BYTE* BBuffer = (BYTE*)buffer; + int pos = 0; + U32 P32 = (U32)(32768 * proba); + + // First Byte + BBuffer[pos++] = (BYTE)(FUZ_rand(seed)); + + while (pos < bufferSize) + { + // Select : Literal (noise) or copy (within 64K) + if (FUZ_RAND15BITS < P32) + { + // Copy (within 64K) + int ref, d; + int length = FUZ_RANDLENGTH + 4; + int offset = FUZ_RAND15BITS + 1; + if (offset > pos) offset = pos; + if (pos + length > bufferSize) length = bufferSize - pos; + ref = pos - offset; + d = pos + length; + while (pos < d) BBuffer[pos++] = BBuffer[ref++]; + } + else + { + // Literal (noise) + int d; + int length = FUZ_RANDLENGTH; + if (pos + length > bufferSize) length = bufferSize - pos; + d = pos + length; + while (pos < d) BBuffer[pos++] = (BYTE)(FUZ_rand(seed) >> 5); + } + } +} + + + +#define FUZ_MAX(a,b) (a>b?a:b) + +int frameTest(U32 seed, int nbCycles, int startCycle, double compressibility) +{ + int testResult = 0; + void* CNBuffer; + void* compressedBuffer; + void* decodedBuffer; + U32 randState = seed; + size_t cSize; + + (void)nbCycles; (void)startCycle; + // Create compressible test buffer + CNBuffer = malloc(COMPRESSIBLE_NOISE_LENGTH); + FUZ_fillCompressibleNoiseBuffer(CNBuffer, COMPRESSIBLE_NOISE_LENGTH, compressibility, &randState); + compressedBuffer = malloc(LZ4F_compressFrameBound(COMPRESSIBLE_NOISE_LENGTH, NULL)); + decodedBuffer = malloc(COMPRESSIBLE_NOISE_LENGTH); + + // Trivial test : one-step frame, all default + cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(64 KB, NULL), CNBuffer, 64 KB, NULL); + if (LZ4F_isError(cSize)) goto _output_error; + DISPLAY("Compressed %i bytes into a %i bytes frame \n", 64 KB, (int)cSize); + +_end: + free(CNBuffer); + free(compressedBuffer); + free(decodedBuffer); + return testResult; + +_output_error: + testResult = 1; + if(!no_prompt) getchar(); + goto _end; +} + + +int FUZ_usage(void) +{ + DISPLAY( "Usage :\n"); + DISPLAY( " %s [args]\n", programName); + DISPLAY( "\n"); + DISPLAY( "Arguments :\n"); + DISPLAY( " -i# : Nb of tests (default:%i) \n", NB_ATTEMPTS); + DISPLAY( " -s# : Select seed (default:prompt user)\n"); + DISPLAY( " -t# : Select starting test number (default:0)\n"); + DISPLAY( " -p# : Select compressibility in %% (default:%i%%)\n", FUZ_COMPRESSIBILITY_DEFAULT); + DISPLAY( " -v : verbose\n"); + DISPLAY( " -h : display help and exit\n"); + return 0; +} + + +int main(int argc, char** argv) { + U32 timestamp = FUZ_GetMilliStart(); + U32 seed=0; + int seedset=0; + int argNb; + int nbTests = NB_ATTEMPTS; + int testNb = 0; + int proba = FUZ_COMPRESSIBILITY_DEFAULT; + + // Check command line + programName = argv[0]; + for(argNb=1; argNb='0') && (*argument<='9')) + { + nbTests *= 10; + nbTests += *argument - '0'; + argument++; + } + break; + case 's': + argument++; + seed=0; seedset=1; + while ((*argument>='0') && (*argument<='9')) + { + seed *= 10; + seed += *argument - '0'; + argument++; + } + break; + case 't': + argument++; + testNb=0; + while ((*argument>='0') && (*argument<='9')) + { + testNb *= 10; + testNb += *argument - '0'; + argument++; + } + break; + case 'p': + argument++; + proba=0; + while ((*argument>='0') && (*argument<='9')) + { + proba *= 10; + proba += *argument - '0'; + argument++; + } + if (proba<0) proba=0; + if (proba>100) proba=100; + break; + default: ; + } + } + } + } + + // Get Seed + printf("Starting lz4frame tester (%i-bits, %s)\n", (int)(sizeof(size_t)*8), LZ4_VERSION); + + if (!seedset) + { + char userInput[50] = {0}; + printf("Select an Initialisation number (default : random) : "); + fflush(stdout); + if ( no_prompt || fgets(userInput, sizeof userInput, stdin) ) + { + if ( sscanf(userInput, "%u", &seed) == 1 ) {} + else seed = FUZ_GetMilliSpan(timestamp); + } + } + printf("Seed = %u\n", seed); + if (proba!=FUZ_COMPRESSIBILITY_DEFAULT) printf("Compressibility : %i%%\n", proba); + + if (nbTests<=0) nbTests=1; + + return frameTest(seed, nbTests, testNb, ((double)proba) / 100); +} -- cgit v0.12 From 2f33c77d53793cc025ed9484870c9a2b0c879261 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sun, 31 Aug 2014 23:14:20 +0100 Subject: upgraded lz4frame compression tests --- lz4frame.c | 2 +- programs/Makefile | 2 +- programs/frametest.c | 94 ++++++++++++++++++++++++++++++++++++++++++---------- 3 files changed, 79 insertions(+), 19 deletions(-) diff --git a/lz4frame.c b/lz4frame.c index 39c9402..a094fd0 100644 --- a/lz4frame.c +++ b/lz4frame.c @@ -252,7 +252,7 @@ size_t LZ4F_compressFrame(void* dstBuffer, size_t dstMaxSize, const void* srcBuf dstPtr += errorCode; } - errorCode = LZ4F_compressEnd(cctx, dstPtr, dstMaxSize, NULL); /* flush last block, and generate suffix */ + errorCode = LZ4F_compressEnd(cctx, dstPtr, dstBlockSize, NULL); /* flush last block, and generate suffix */ if (LZ4F_isError(errorCode)) return errorCode; dstPtr += errorCode; diff --git a/programs/Makefile b/programs/Makefile index 441d538..21c7514 100644 --- a/programs/Makefile +++ b/programs/Makefile @@ -36,7 +36,7 @@ DESTDIR?= PREFIX ?= /usr CC := $(CC) CFLAGS ?= -O3 -CFLAGS += -std=c99 -Wall -Wextra -Wundef -Wshadow -Wstrict-prototypes -Wpedantic -DLZ4_VERSION=\"$(RELEASE)\" +CFLAGS += -std=c99 -Wall -Wextra -Wundef -Wshadow -Wstrict-prototypes -DLZ4_VERSION=\"$(RELEASE)\" FLAGS = -I.. $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) BINDIR=$(PREFIX)/bin diff --git a/programs/frametest.c b/programs/frametest.c index 862ecf4..ad93182 100644 --- a/programs/frametest.c +++ b/programs/frametest.c @@ -30,6 +30,11 @@ # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ # pragma warning(disable : 4146) /* disable: C4146: minus unsigned expression */ #endif +#define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) +#ifdef __GNUC__ +# pragma GCC diagnostic ignored "-Wmissing-braces" /* GCC bug 53119 : doesn't accept { 0 } as initializer (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53119) */ +# pragma GCC diagnostic ignored "-Wmissing-field-initializers" /* GCC bug 53119 : doesn't accept { 0 } as initializer (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53119) */ +#endif /************************************** @@ -132,11 +137,11 @@ unsigned int FUZ_rand(unsigned int* src) #define FUZ_RAND15BITS ((FUZ_rand(seed) >> 3) & 32767) -#define FUZ_RANDLENGTH ( ((FUZ_rand(seed) >> 7) & 3) ? (FUZ_rand(seed) % 14) : (FUZ_rand(seed) & 511) + 15) -void FUZ_fillCompressibleNoiseBuffer(void* buffer, int bufferSize, double proba, U32* seed) +#define FUZ_RANDLENGTH ( ((FUZ_rand(seed) >> 7) & 3) ? (FUZ_rand(seed) % 15) : (FUZ_rand(seed) % 510) + 15) +static void FUZ_fillCompressibleNoiseBuffer(void* buffer, unsigned bufferSize, double proba, U32* seed) { BYTE* BBuffer = (BYTE*)buffer; - int pos = 0; + unsigned pos = 0; U32 P32 = (U32)(32768 * proba); // First Byte @@ -148,23 +153,23 @@ void FUZ_fillCompressibleNoiseBuffer(void* buffer, int bufferSize, double proba, if (FUZ_RAND15BITS < P32) { // Copy (within 64K) - int ref, d; - int length = FUZ_RANDLENGTH + 4; - int offset = FUZ_RAND15BITS + 1; + unsigned match, end; + unsigned length = FUZ_RANDLENGTH + 4; + unsigned offset = FUZ_RAND15BITS + 1; if (offset > pos) offset = pos; if (pos + length > bufferSize) length = bufferSize - pos; - ref = pos - offset; - d = pos + length; - while (pos < d) BBuffer[pos++] = BBuffer[ref++]; + match = pos - offset; + end = pos + length; + while (pos < end) BBuffer[pos++] = BBuffer[match++]; } else { // Literal (noise) - int d; - int length = FUZ_RANDLENGTH; + unsigned end; + unsigned length = FUZ_RANDLENGTH; if (pos + length > bufferSize) length = bufferSize - pos; - d = pos + length; - while (pos < d) BBuffer[pos++] = (BYTE)(FUZ_rand(seed) >> 5); + end = pos + length; + while (pos < end) BBuffer[pos++] = (BYTE)(FUZ_rand(seed) >> 5); } } } @@ -180,7 +185,8 @@ int frameTest(U32 seed, int nbCycles, int startCycle, double compressibility) void* compressedBuffer; void* decodedBuffer; U32 randState = seed; - size_t cSize; + size_t cSize, testSize; + LZ4F_preferences_t prefs = { 0 }; (void)nbCycles; (void)startCycle; // Create compressible test buffer @@ -189,10 +195,64 @@ int frameTest(U32 seed, int nbCycles, int startCycle, double compressibility) compressedBuffer = malloc(LZ4F_compressFrameBound(COMPRESSIBLE_NOISE_LENGTH, NULL)); decodedBuffer = malloc(COMPRESSIBLE_NOISE_LENGTH); - // Trivial test : one-step frame, all default - cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(64 KB, NULL), CNBuffer, 64 KB, NULL); + // Trivial tests : one-step frame + testSize = COMPRESSIBLE_NOISE_LENGTH; + DISPLAY("Using NULL preferences : \n"); + cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, NULL), CNBuffer, testSize, NULL); + if (LZ4F_isError(cSize)) goto _output_error; + DISPLAY("Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); + + DISPLAY("Using 64 KB block : \n"); + prefs.frameInfo.blockSizeID = max64KB; + cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &(prefs.frameInfo)), CNBuffer, testSize, &prefs); + if (LZ4F_isError(cSize)) goto _output_error; + DISPLAY("Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); + + DISPLAY("without checksum : \n"); + prefs.frameInfo.contentChecksumFlag = noContentChecksum; + cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &(prefs.frameInfo)), CNBuffer, testSize, &prefs); + if (LZ4F_isError(cSize)) goto _output_error; + DISPLAY("Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); + + DISPLAY("Using 256 KB block : \n"); + prefs.frameInfo.blockSizeID = max256KB; + prefs.frameInfo.contentChecksumFlag = contentChecksumEnabled; + cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &(prefs.frameInfo)), CNBuffer, testSize, &prefs); + if (LZ4F_isError(cSize)) goto _output_error; + DISPLAY("Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); + + DISPLAY("without checksum : \n"); + prefs.frameInfo.contentChecksumFlag = noContentChecksum; + cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &(prefs.frameInfo)), CNBuffer, testSize, &prefs); if (LZ4F_isError(cSize)) goto _output_error; - DISPLAY("Compressed %i bytes into a %i bytes frame \n", 64 KB, (int)cSize); + DISPLAY("Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); + + DISPLAY("Using 1 MB block : \n"); + prefs.frameInfo.blockSizeID = max1MB; + prefs.frameInfo.contentChecksumFlag = contentChecksumEnabled; + cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &(prefs.frameInfo)), CNBuffer, testSize, &prefs); + if (LZ4F_isError(cSize)) goto _output_error; + DISPLAY("Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); + + DISPLAY("without checksum : \n"); + prefs.frameInfo.contentChecksumFlag = noContentChecksum; + cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &(prefs.frameInfo)), CNBuffer, testSize, &prefs); + if (LZ4F_isError(cSize)) goto _output_error; + DISPLAY("Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); + + DISPLAY("Using 4 MB block : \n"); + prefs.frameInfo.blockSizeID = max4MB; + prefs.frameInfo.contentChecksumFlag = contentChecksumEnabled; + cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &(prefs.frameInfo)), CNBuffer, testSize, &prefs); + if (LZ4F_isError(cSize)) goto _output_error; + DISPLAY("Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); + + DISPLAY("without checksum : \n"); + prefs.frameInfo.contentChecksumFlag = noContentChecksum; + cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &(prefs.frameInfo)), CNBuffer, testSize, &prefs); + if (LZ4F_isError(cSize)) goto _output_error; + DISPLAY("Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); + _end: free(CNBuffer); -- cgit v0.12 From f7f67e778c9261fa5843811068f3eb3cc4e70509 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 1 Sep 2014 22:44:02 +0100 Subject: Added : preliminary frame decompression function --- Makefile | 2 +- lz4frame.c | 244 ++++++++++++++++++++++++++++++++++++++++++++++++++- lz4frame.h | 24 ++--- programs/Makefile | 5 +- programs/frametest.c | 36 +++----- 5 files changed, 273 insertions(+), 38 deletions(-) diff --git a/Makefile b/Makefile index 30ecbb7..b21ae2f 100644 --- a/Makefile +++ b/Makefile @@ -31,7 +31,7 @@ # ################################################################ # Version numbers -VERSION=122 +VERSION=123 export RELEASE=r$(VERSION) LIBVER_MAJOR=`sed -n '/define LZ4_VERSION_MAJOR/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < lz4.h` LIBVER_MINOR=`sed -n '/define LZ4_VERSION_MINOR/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < lz4.h` diff --git a/lz4frame.c b/lz4frame.c index a094fd0..c604866 100644 --- a/lz4frame.c +++ b/lz4frame.c @@ -151,8 +151,17 @@ static void LZ4F_writeLE32 (BYTE* dstPtr, U32 value32) dstPtr[3] = (BYTE)(value32 >> 24); } +static U32 LZ4F_readLE32 (const BYTE* srcPtr) +{ + U32 value32 = srcPtr[0]; + value32 += (srcPtr[1]<<8); + value32 += (srcPtr[2]<<16); + value32 += (srcPtr[3]<<24); + return value32; +} -static BYTE LZ4F_headerChecksum (BYTE* header, size_t length) + +static BYTE LZ4F_headerChecksum (const BYTE* header, size_t length) { U32 xxh = XXH32(header, length, 0); return (BYTE)(xxh >> 8); @@ -553,3 +562,236 @@ size_t LZ4F_compressEnd(LZ4F_compressionContext_t compressionContext, void* dstB return dstPtr - dstStart; } + + +/*********************************** + * Decompression functions + * *********************************/ + +typedef struct { + LZ4F_frameInfo_t frameInfo; + unsigned dStage; + BYTE* tmpInputBuffer; + size_t tmpInputFilled; + size_t tmpInputTarget; +} LZ4F_dctx_internal_t; + + + +/* Resource management */ + +/* LZ4F_createDecompressionContext() : + * The first thing to do is to create a decompressionContext object, which will be used in all decompression operations. + * This is achieved using LZ4F_createDecompressionContext(). + * The function will provide a pointer to a fully allocated and initialised LZ4F_decompressionContext object. + * If the result LZ4F_errorCode_t is not zero, there was an error during context creation. + * Object can release its memory using LZ4F_freeDecompressionContext(); + */ +LZ4F_errorCode_t LZ4F_createDecompressionContext(LZ4F_compressionContext_t* LZ4F_decompressionContextPtr); +LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_compressionContext_t LZ4F_decompressionContext); + +/* Decompression */ + +static size_t LZ4F_decodeHeader(LZ4F_frameInfo_t* frameInfoPtr, const BYTE* srcPtr, size_t srcSize) +{ + BYTE FLG, BD, HC; + unsigned version, blockMode, blockChecksumFlag, contentSizeFlag, contentChecksumFlag, dictFlag, blockSizeID; + + /* need to decode header to get frameInfo */ + if (srcSize < 7) return -ERROR_GENERIC; /* minimal decodable size */ + + /* control magic number */ + if (LZ4F_readLE32(srcPtr) != LZ4F_MAGICNUMBER) return -ERROR_GENERIC; + srcPtr += 4; + + /* Flags */ + FLG = srcPtr[0]; + version = (FLG>>6)&_2BITS; + blockMode = (FLG>>5) & _1BIT; + blockChecksumFlag = (FLG>>4) & _1BIT; + contentSizeFlag = (FLG>>3) & _1BIT; + contentChecksumFlag = (FLG>>2) & _1BIT; + dictFlag = (FLG>>0) & _1BIT; + BD = srcPtr[1]; + blockSizeID = (BD>>4) & _3BITS; + + /* check */ + HC = LZ4F_headerChecksum(srcPtr, 2); + if (HC != srcPtr[2]) return -ERROR_GENERIC; /* Bad header checksum error */ + + /* validate */ + if (version != 1) return -ERROR_GENERIC; /* Version Number, only supported value */ + if (blockMode != blockIndependent) return -ERROR_GENERIC; /* Only supported blockMode for the time being */ + if (blockChecksumFlag != 0) return -ERROR_GENERIC; /* Only supported value for the time being */ + if (contentSizeFlag != 0) return -ERROR_GENERIC; /* Only supported value for the time being */ + if (((FLG>>1)&_1BIT) != 0) return -ERROR_GENERIC; /* Reserved bit */ + if (dictFlag != 0) return -ERROR_GENERIC; /* Only supported value for the time being */ + if (((BD>>7)&_1BIT) != 0) return -ERROR_GENERIC; /* Reserved bit */ + if (blockSizeID < 4) return -ERROR_GENERIC; /* Only supported values for the time being */ + if (((BD>>0)&_4BITS) != 0) return -ERROR_GENERIC; /* Reserved bits */ + + /* save */ + frameInfoPtr->blockMode = blockMode; + frameInfoPtr->contentChecksumFlag = contentChecksumFlag; + frameInfoPtr->blockSizeID = blockSizeID; + + return 7; +} + + +/* LZ4F_getFrameInfo() + * This function decodes frame header information, such as blockSize. + * It is optional : you could start by calling directly LZ4F_decompress() instead. + * The objective is to extract header information without starting decompression, typically for allocation purposes. + * LZ4F_getFrameInfo() can also be used *after* starting decompression, on a valid LZ4F_decompressionContext_t. + * The number of bytes read from srcBuffer will be provided within *srcSize (necessarily <= original value). + * The function result is an error code which can be tested using LZ4F_isError(). + */ +LZ4F_errorCode_t LZ4F_getFrameInfo(LZ4F_decompressionContext_t decompressionContext, LZ4F_frameInfo_t* frameInfoPtr, const void* srcBuffer, size_t* srcSize) +{ + LZ4F_dctx_internal_t* dctxPtr = (LZ4F_dctx_internal_t*)decompressionContext; + + if (dctxPtr->dStage==0) + { + LZ4F_errorCode_t errorCode = LZ4F_decodeHeader(frameInfoPtr, srcBuffer, *srcSize); + if (LZ4F_isError(errorCode)) return errorCode; + *srcSize = errorCode; + return OK_NoError; + } + + /* frameInfo already decoded */ + *srcSize = 0; + *frameInfoPtr = dctxPtr->frameInfo; + return OK_NoError; +} + +/* LZ4F_decompress() + * Call this function repetitively to regenerate data compressed within srcBuffer. + * The function will attempt to decode *srcSize from srcBuffer, into dstBuffer of maximum size *dstSize. + * + * The number of bytes generated into dstBuffer will be provided within *dstSize (necessarily <= original value). + * + * The number of bytes effectively read from srcBuffer will be provided within *srcSize (necessarily <= original value). + * If the number of bytes read is < number of bytes provided, then the decompression operation is not complete. + * You will have to call it again, using the same src arguments (but eventually different dst arguments). + * + * The function result is an error code which can be tested using LZ4F_isError(). + * When the frame is fully decoded, the function result will be OK_FrameEnd(=1). + */ +LZ4F_errorCode_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContext, void* dstBuffer, size_t* dstSize, const void* srcBuffer, size_t* srcSize, const LZ4F_decompressOptions_t* decompressOptionsPtr) +{ + LZ4F_dctx_internal_t* dctxPtr = (LZ4F_dctx_internal_t*)decompressionContext; + LZ4F_decompressOptions_t optionsNull = { 0 }; + const BYTE* const srcStart = (const BYTE*)srcBuffer; + const BYTE* const srcEnd = srcStart + *srcSize; + const BYTE* srcPtr = srcStart; + BYTE* const dstStart = (BYTE*)dstBuffer; + BYTE* const dstEnd = dstStart + *dstSize; + BYTE* dstPtr = dstStart; + size_t nextCBlockSize=0; + unsigned nextBlockUncompressedFlag=0; + + if (decompressOptionsPtr==NULL) decompressOptionsPtr = &optionsNull; + *srcSize = 0; + + if (dctxPtr->dStage == 0) /* header must be decoded */ + { + LZ4F_errorCode_t errorCode = LZ4F_decodeHeader(&(dctxPtr->frameInfo), srcBuffer, *srcSize); + if (LZ4F_isError(errorCode)) return errorCode; + srcPtr += errorCode; + dctxPtr->dStage = 1; + } + + if (dctxPtr->tmpInputTarget > 0) /* finalize what's already saved*/ + { + size_t sizeToCopy = dctxPtr->tmpInputTarget - dctxPtr->tmpInputFilled; + if (sizeToCopy < (size_t)(srcEnd - srcPtr)) sizeToCopy = srcEnd - srcPtr; + memcpy(dctxPtr->tmpInputBuffer + dctxPtr->tmpInputFilled, srcPtr, sizeToCopy); + srcPtr += sizeToCopy; + if (dctxPtr->tmpInputFilled < dctxPtr->tmpInputTarget) goto _end; + + switch (dctxPtr->dStage) + { + default: + return -ERROR_GENERIC; /* impossible case */ + case 1: + nextCBlockSize = LZ4F_readLE32(dctxPtr->tmpInputBuffer) & 0x7FFFFFFFU; + nextBlockUncompressedFlag = LZ4F_readLE32(dctxPtr->tmpInputBuffer) & 0x80000000U; + dctxPtr->tmpInputFilled = 0; + dctxPtr->tmpInputTarget = 0; + dctxPtr->dStage = 2; + break; + case 2: + if (nextBlockUncompressedFlag) + { + memcpy(dstPtr, dctxPtr->tmpInputBuffer, nextCBlockSize); + srcPtr += nextCBlockSize; + dstPtr += nextCBlockSize; + } + else + { + int origSize = LZ4_decompress_safe((const char*)(dctxPtr->tmpInputBuffer), (char*)dstPtr, (int)nextCBlockSize, (int)(dstEnd - dstPtr)); /* blindly attempt to decompress, in case there is enough space left */ + srcPtr += nextCBlockSize; + dstPtr += origSize; + } + dctxPtr->tmpInputFilled = 0; + dctxPtr->tmpInputTarget = 0; + dctxPtr->dStage = 1; + break; + } + } + + while (srcPtr < srcEnd) /* can still read */ + { + if (dctxPtr->dStage == 1) /* read next block size */ + { + if ((srcEnd - srcPtr) < 4) + { + size_t nbBytesToCopy = (srcEnd - srcPtr); + memcpy(dctxPtr->tmpInputBuffer, srcPtr, nbBytesToCopy); + dctxPtr->tmpInputFilled = nbBytesToCopy; + dctxPtr->tmpInputTarget = 4; + srcPtr = srcEnd; + break; + } + nextCBlockSize = LZ4F_readLE32(srcPtr) & 0x7FFFFFFFU; + nextBlockUncompressedFlag = LZ4F_readLE32(srcPtr) & 0x80000000U; + srcPtr += 4; + dctxPtr->dStage = 2; + } + + if (dctxPtr->dStage == 2) /* compressed block */ + { + if ((size_t)(srcEnd - srcPtr) < nextCBlockSize) + { + memcpy(dctxPtr->tmpInputBuffer, srcPtr, srcEnd-srcPtr); + dctxPtr->tmpInputFilled = srcEnd - srcPtr; + dctxPtr->tmpInputTarget = nextCBlockSize; + break; + } + if (nextBlockUncompressedFlag) + { + memcpy(dstPtr, srcPtr, nextCBlockSize); + srcPtr += nextCBlockSize; + dstPtr += nextCBlockSize; + } + else + { + int origSize = LZ4_decompress_safe((const char*)srcPtr, (char*)dstPtr, (int)nextCBlockSize, (int)(dstEnd - dstPtr)); /* blindly attempt to decompress, in case there is enough space left */ + srcPtr += nextCBlockSize; + dstPtr += origSize; + } + } + } + + if (srcPtr < srcEnd) + { + dctxPtr->tmpInputFilled = srcEnd-srcPtr; + memcpy(dctxPtr->tmpInputBuffer, srcPtr, dctxPtr->tmpInputFilled); /* save remaining input */ + } + +_end: + *srcSize = (srcPtr - srcStart); + *dstSize = (dstPtr - dstStart); + return OK_NoError; +} diff --git a/lz4frame.h b/lz4frame.h index 851de45..ae44a3a 100644 --- a/lz4frame.h +++ b/lz4frame.h @@ -37,7 +37,7 @@ * All related operations, including memory management, are handled by the library. * You don't need lz4.h when using lz4frame.h. * */ - + #pragma once #if defined (__cplusplus) @@ -56,13 +56,13 @@ extern "C" { **************************************/ typedef size_t LZ4F_errorCode_t; typedef enum { OK_FrameEnd = 1 } LZ4F_successCodes; -typedef enum { OK_NoError = 0, ERROR_GENERIC = 1, +typedef enum { OK_NoError = 0, ERROR_GENERIC = 1, ERROR_maxBlockSize_invalid, ERROR_blockMode_invalid, ERROR_contentChecksumFlag_invalid, ERROR_srcSize_tooLarge, ERROR_dstMaxSize_tooSmall, ERROR_allocation_failed, ERROR_compressionLevel_invalid, ERROR_maxCode - } LZ4F_errorCodes; /* error codes are negative unsigned values. + } LZ4F_errorCodes; /* error codes are negative unsigned values. Compare function result to (-specificCode) */ int LZ4F_isError(LZ4F_errorCode_t code); /* Basically : code > -ERROR_maxCode */ @@ -109,7 +109,7 @@ size_t LZ4F_compressFrame(void* dstBuffer, size_t dstMaxSize, const void* srcBuf /********************************** - * Advanced compression functions + * Advanced compression functions * *********************************/ typedef void* LZ4F_compressionContext_t; @@ -163,17 +163,17 @@ size_t LZ4F_compress(LZ4F_compressionContext_t compressionContext, void* dstBuff * The result of the function is the number of bytes written into dstBuffer (it can be zero, meaning input data is just stored within compressionContext for a future block to complete) * The function outputs an error code if it fails (can be tested using LZ4F_isError()) */ - + size_t LZ4F_flush(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const LZ4F_compressOptions_t* compressOptionsPtr); /* LZ4F_flush() * Should you need to create compressed data immediately, without waiting for a block to be filled, * you can call LZ4_flush(), which will immediately compress any remaining data stored within compressionContext. - * The result of the function is the number of bytes written into dstBuffer + * The result of the function is the number of bytes written into dstBuffer * (it can be zero, this means there was no data left within compressionContext) * The function outputs an error code if it fails (can be tested using LZ4F_isError()) * The LZ4F_compressOptions_t structure is optional : you can provide NULL as argument. */ - + size_t LZ4F_compressEnd(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const LZ4F_compressOptions_t* compressOptionsPtr); /* LZ4F_compressEnd() * When you want to properly finish the compressed frame, just call LZ4F_compressEnd(). @@ -186,8 +186,8 @@ size_t LZ4F_compressEnd(LZ4F_compressionContext_t compressionContext, void* dstB */ -/********************************** - * Decompression functions +/*********************************** + * Decompression functions * *********************************/ typedef void* LZ4F_decompressionContext_t; @@ -207,7 +207,7 @@ LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_compressionContext_t LZ4F_de * If the result LZ4F_errorCode_t is not zero, there was an error during context creation. * Object can release its memory using LZ4F_freeDecompressionContext(); */ - + /* Decompression */ LZ4F_errorCode_t LZ4F_getFrameInfo(LZ4F_decompressionContext_t decompressionContext, LZ4F_frameInfo_t* frameInfoPtr, const void* srcBuffer, size_t* srcSize); @@ -226,11 +226,11 @@ LZ4F_errorCode_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContex * The function will attempt to decode *srcSize from srcBuffer, into dstBuffer of maximum size *dstSize. * * The number of bytes generated into dstBuffer will be provided within *dstSize (necessarily <= original value). - * + * * The number of bytes effectively read from srcBuffer will be provided within *srcSize (necessarily <= original value). * If the number of bytes read is < number of bytes provided, then the decompression operation is not complete. * You will have to call it again, using the same src arguments (but eventually different dst arguments). - * + * * The function result is an error code which can be tested using LZ4F_isError(). * When the frame is fully decoded, the function result will be OK_FrameEnd(=1). */ diff --git a/programs/Makefile b/programs/Makefile index 21c7514..82a5e66 100644 --- a/programs/Makefile +++ b/programs/Makefile @@ -132,7 +132,7 @@ uninstall: [ -f $(DESTDIR)$(MANDIR)/lz4cat.1 ] && rm -f $(DESTDIR)$(MANDIR)/lz4cat.1 @echo lz4 successfully uninstalled -test-native: test-lz4 test-lz4c test-fullbench test-fuzzer test-mem +test-native: test-lz4 test-lz4c test-frame test-fullbench test-fuzzer test-mem test-force32: test-lz4c32 test-fullbench32 test-fuzzer32 test-mem32 @@ -169,6 +169,9 @@ test-fuzzer: fuzzer test-fuzzer32: fuzzer32 ./fuzzer32 --no-prompt +test-frame: frametest + ./frametest + test-mem: lz4 datagen ./datagen -g16KB > tmp valgrind ./lz4 -9 -BD -f tmp /dev/null diff --git a/programs/frametest.c b/programs/frametest.c index ad93182..e61205a 100644 --- a/programs/frametest.c +++ b/programs/frametest.c @@ -73,18 +73,19 @@ # define LZ4_VERSION "" #endif -#define NB_ATTEMPTS (1<<16) -#define COMPRESSIBLE_NOISE_LENGTH (1 << 21) -#define FUZ_MAX_BLOCK_SIZE (1 << 17) -#define FUZ_MAX_DICT_SIZE (1 << 15) +#define KB *(1U<<10) +#define MB *(1U<<20) +#define GB *(1U<<30) + +#define NB_ATTEMPTS (64 KB) +#define COMPRESSIBLE_NOISE_LENGTH (2 MB) +#define FUZ_MAX_BLOCK_SIZE (128 KB) +#define FUZ_MAX_DICT_SIZE (32 KB) #define FUZ_COMPRESSIBILITY_DEFAULT 50 #define PRIME1 2654435761U #define PRIME2 2246822519U #define PRIME3 3266489917U -#define KB *(1U<<10) -#define MB *(1U<<20) -#define GB *(1U<<30) /************************************** @@ -114,7 +115,7 @@ static int FUZ_GetMilliStart(void) return nCount; } - +/* static int FUZ_GetMilliSpan( int nTimeStart ) { int nSpan = FUZ_GetMilliStart() - nTimeStart; @@ -122,6 +123,7 @@ static int FUZ_GetMilliSpan( int nTimeStart ) nSpan += 0x100000 * 1000; return nSpan; } +*/ # define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r))) @@ -176,8 +178,6 @@ static void FUZ_fillCompressibleNoiseBuffer(void* buffer, unsigned bufferSize, d -#define FUZ_MAX(a,b) (a>b?a:b) - int frameTest(U32 seed, int nbCycles, int startCycle, double compressibility) { int testResult = 0; @@ -283,8 +283,8 @@ int FUZ_usage(void) } -int main(int argc, char** argv) { - U32 timestamp = FUZ_GetMilliStart(); +int main(int argc, char** argv) +{ U32 seed=0; int seedset=0; int argNb; @@ -367,17 +367,7 @@ int main(int argc, char** argv) { // Get Seed printf("Starting lz4frame tester (%i-bits, %s)\n", (int)(sizeof(size_t)*8), LZ4_VERSION); - if (!seedset) - { - char userInput[50] = {0}; - printf("Select an Initialisation number (default : random) : "); - fflush(stdout); - if ( no_prompt || fgets(userInput, sizeof userInput, stdin) ) - { - if ( sscanf(userInput, "%u", &seed) == 1 ) {} - else seed = FUZ_GetMilliSpan(timestamp); - } - } + if (!seedset) seed = FUZ_GetMilliStart() % 10000; printf("Seed = %u\n", seed); if (proba!=FUZ_COMPRESSIBILITY_DEFAULT) printf("Compressibility : %i%%\n", proba); -- cgit v0.12 From e619cfe8525f16d382fad1c569c1b71b25ac3d1d Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 3 Sep 2014 19:49:59 +0100 Subject: Completed first version of lz4frame decompress Added a first decompression test --- lz4.h | 13 +- lz4frame.c | 377 ++++++++++++++++++++++++++++++++++++--------------- lz4frame.h | 12 +- programs/frametest.c | 15 ++ 4 files changed, 295 insertions(+), 122 deletions(-) diff --git a/lz4.h b/lz4.h index 377e075..3400f50 100644 --- a/lz4.h +++ b/lz4.h @@ -37,6 +37,11 @@ extern "C" { #endif +/* + * lz4.h provides raw compression format functions, for optimal performance and integration into programs. + * If you need to generate data using an inter-operable format (respecting the framing specification), + * please use lz4frame.h instead. +*/ /************************************** Version @@ -142,7 +147,7 @@ LZ4_decompress_fast() : Destination buffer must be already allocated. Its size must be a minimum of 'originalSize' bytes. note : This function fully respect memory boundaries for properly formed compressed data. It is a bit faster than LZ4_decompress_safe(). - However, it does not provide any protection against intentionnally modified data stream (malicious input). + However, it does not provide any protection against intentionally modified data stream (malicious input). Use this function in trusted environment only (data to decode comes from a trusted source). */ int LZ4_decompress_fast (const char* source, char* dest, int originalSize); @@ -178,8 +183,7 @@ typedef struct { unsigned int table[LZ4_STREAMSIZE_U32]; } LZ4_stream_t; /* * LZ4_resetStream - * Use this function to init a newly allocated LZ4_stream_t structure - * You can also reset an existing LZ4_stream_t structure + * Use this function to init an allocated LZ4_stream_t structure */ void LZ4_resetStream (LZ4_stream_t* LZ4_stream); @@ -273,8 +277,7 @@ Advanced decoding functions : *_usingDict() : These decoding functions work the same as a combination of LZ4_setDictDecode() followed by LZ4_decompress_x_continue() - all together into a single function call. - It doesn't use nor update an LZ4_streamDecode_t structure. + They don't use nor update an LZ4_streamDecode_t structure. */ int LZ4_decompress_safe_usingDict (const char* source, char* dest, int compressedSize, int maxOutputSize, const char* dictStart, int dictSize); int LZ4_decompress_fast_usingDict (const char* source, char* dest, int originalSize, const char* dictStart, int dictSize); diff --git a/lz4frame.c b/lz4frame.c index c604866..80e744e 100644 --- a/lz4frame.c +++ b/lz4frame.c @@ -56,7 +56,7 @@ Memory routines **************************************/ #include /* malloc, calloc, free */ -#define ALLOCATOR(n,s) calloc(n,s) +#define ALLOCATOR(s) calloc(1,s) #define FREEMEM free #include /* memset, memcpy */ #define MEM_INIT memset @@ -118,8 +118,8 @@ typedef struct { unsigned cStage; size_t maxBlockSize; XXH32_stateSpace_t xxh; - BYTE* tmpInputBuffer; - size_t tmpInputFilled; + BYTE* tmpIn; + size_t tmpInSize; } LZ4F_cctx_internal_t; @@ -272,7 +272,7 @@ size_t LZ4F_compressFrame(void* dstBuffer, size_t dstMaxSize, const void* srcBuf } -/********************************** +/*********************************** * Advanced compression functions * *********************************/ @@ -292,7 +292,7 @@ LZ4F_errorCode_t LZ4F_createCompressionContext(LZ4F_compressionContext_t* LZ4F_c if (preferencesPtr == NULL) preferencesPtr = &prefNull; - cctxPtr = malloc(sizeof(LZ4F_cctx_internal_t)); + cctxPtr = ALLOCATOR(sizeof(LZ4F_cctx_internal_t)); if (cctxPtr==NULL) return -ERROR_allocation_failed; cctxPtr->prefs = *preferencesPtr; /* equivalent to memcpy() */ @@ -300,9 +300,9 @@ LZ4F_errorCode_t LZ4F_createCompressionContext(LZ4F_compressionContext_t* LZ4F_c cctxPtr->cStage = 0; /* Next stage : write header */ if (cctxPtr->prefs.frameInfo.blockSizeID == 0) cctxPtr->prefs.frameInfo.blockSizeID = LZ4F_BLOCKSIZEID_DEFAULT; cctxPtr->maxBlockSize = LZ4F_getBlockSize(cctxPtr->prefs.frameInfo.blockSizeID); - cctxPtr->tmpInputBuffer = malloc(cctxPtr->maxBlockSize); - if (cctxPtr->tmpInputBuffer == NULL) return -ERROR_allocation_failed; - cctxPtr->tmpInputFilled = 0; + cctxPtr->tmpIn = ALLOCATOR(cctxPtr->maxBlockSize); + if (cctxPtr->tmpIn == NULL) return -ERROR_allocation_failed; + cctxPtr->tmpInSize = 0; XXH32_resetState(&(cctxPtr->xxh), 0); *LZ4F_compressionContextPtr = (LZ4F_compressionContext_t)cctxPtr; @@ -315,8 +315,8 @@ LZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_compressionContext_t LZ4F_comp { LZ4F_cctx_internal_t* cctxPtr = (LZ4F_cctx_internal_t*)LZ4F_compressionContext; - free(cctxPtr->tmpInputBuffer); - free(LZ4F_compressionContext); + FREEMEM(cctxPtr->tmpIn); + FREEMEM(LZ4F_compressionContext); return OK_NoError; } @@ -324,7 +324,7 @@ LZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_compressionContext_t LZ4F_comp /* LZ4F_compressBegin() : * will write the frame header into dstBuffer. - * dstBuffer must be large enough to accomodate a header (dstMaxSize). Maximum header size is LZ4F_MAXHEADERFRAME_SIZE(19) bytes. + * dstBuffer must be large enough to accommodate a header (dstMaxSize). Maximum header size is LZ4F_MAXHEADERFRAME_SIZE(19) bytes. * The result of the function is the number of bytes written into dstBuffer for the header * or an error code (can be tested using LZ4F_isError()) */ @@ -346,7 +346,7 @@ size_t LZ4F_compressBegin(LZ4F_compressionContext_t compressionContext, void* ds /* FLG Byte */ *dstPtr = (1 & _2BITS) << 6; /* Version('01') */ *dstPtr |= (1 & _1BIT ) << 5; /* Blocks independents */ - *dstPtr |= (1 & _1BIT ) << 2; /* Stream checksum */ + *dstPtr |= (char)((cctxPtr->prefs.frameInfo.contentChecksumFlag & _1BIT ) << 2); /* Stream checksum */ dstPtr++; /* BD Byte */ *dstPtr = (char)((cctxPtr->prefs.frameInfo.blockSizeID & _3BITS) << 4); @@ -372,8 +372,12 @@ size_t LZ4F_compressBound(size_t srcSize, const LZ4F_frameInfo_t* frameInfoPtr) size_t vSrcSize = srcSize + (blockSize-1); /* worst case : tmp buffer almost filled */ unsigned nbBlocks = vSrcSize / blockSize; size_t blockInfo = 4; /* default, without block CRC option */ + size_t frameEnd = 4 + frameInfoPtr->contentChecksumFlag*4; + size_t lastBlockSize = blockInfo + (blockSize-1) + frameEnd; + size_t result = (blockSize + blockInfo) * nbBlocks; - return (blockSize + blockInfo) * nbBlocks; + if (result < lastBlockSize) result = lastBlockSize; + return result; } /* LZ4F_getMaxSrcSize() : gives max allowed srcSize given dstMaxSize to handle worst case situations. @@ -422,34 +426,34 @@ size_t LZ4F_compress(LZ4F_compressionContext_t compressionContext, void* dstBuff if (compressOptionsPtr == NULL) compressOptionsPtr = &cOptionsNull; /* complete tmp buffer */ - if (cctxPtr->tmpInputFilled > 0) + if (cctxPtr->tmpInSize > 0) { - size_t sizeToCopy = blockSize - cctxPtr->tmpInputFilled; + size_t sizeToCopy = blockSize - cctxPtr->tmpInSize; if (sizeToCopy > srcSize) { /* add to tmp buffer */ - memcpy(cctxPtr->tmpInputBuffer + cctxPtr->tmpInputFilled, srcBuffer, srcSize); + memcpy(cctxPtr->tmpIn + cctxPtr->tmpInSize, srcBuffer, srcSize); srcPtr = srcEnd; - cctxPtr->tmpInputFilled += srcSize; + cctxPtr->tmpInSize += srcSize; } else { BYTE* cSizePtr = dstPtr; U32 cSize; - memcpy(cctxPtr->tmpInputBuffer + cctxPtr->tmpInputFilled, srcBuffer, sizeToCopy); + memcpy(cctxPtr->tmpIn + cctxPtr->tmpInSize, srcBuffer, sizeToCopy); srcPtr += sizeToCopy; dstPtr += 4; /* space for cSizePtr */ - cSize = (U32)LZ4_compress_limitedOutput((const char*)cctxPtr->tmpInputBuffer, (char*)dstPtr, (int)(blockSize), (int)(blockSize-1)); + cSize = (U32)LZ4_compress_limitedOutput((const char*)cctxPtr->tmpIn, (char*)dstPtr, (int)(blockSize), (int)(blockSize-1)); dstPtr += cSize; LZ4F_writeLE32(cSizePtr, cSize); if (cSize == 0) /* compression failed */ { cSize = blockSize + LZ4F_BLOCKUNCOMPRESSED_FLAG; LZ4F_writeLE32(cSizePtr, cSize); - memcpy(dstPtr, cctxPtr->tmpInputBuffer, blockSize); + memcpy(dstPtr, cctxPtr->tmpIn, blockSize); dstPtr += blockSize; } - cctxPtr->tmpInputFilled = 0; + cctxPtr->tmpInSize = 0; } } @@ -476,8 +480,8 @@ size_t LZ4F_compress(LZ4F_compressionContext_t compressionContext, void* dstBuff { /* fill tmp buffer */ size_t sizeToCopy = srcEnd - srcPtr; - memcpy(cctxPtr->tmpInputBuffer, srcPtr, sizeToCopy); - cctxPtr->tmpInputFilled = sizeToCopy; + memcpy(cctxPtr->tmpIn, srcPtr, sizeToCopy); + cctxPtr->tmpInSize = sizeToCopy; } if (cctxPtr->prefs.frameInfo.contentChecksumFlag == contentChecksumEnabled) @@ -503,7 +507,7 @@ size_t LZ4F_flush(LZ4F_compressionContext_t compressionContext, void* dstBuffer, BYTE* dstPtr = dstStart; - if (cctxPtr->tmpInputFilled == 0) return 0; /* nothing to flush */ + if (cctxPtr->tmpInSize == 0) return 0; /* nothing to flush */ if (cctxPtr->cStage != 1) return -ERROR_GENERIC; if (dstMaxSize < LZ4F_compressBound(1, &(cctxPtr->prefs.frameInfo))) return -ERROR_dstMaxSize_tooSmall; if (compressOptionsPtr == NULL) compressOptionsPtr = &cOptionsNull; @@ -512,17 +516,17 @@ size_t LZ4F_flush(LZ4F_compressionContext_t compressionContext, void* dstBuffer, BYTE* cSizePtr = dstPtr; U32 cSize; dstPtr += 4; /* space for cSizePtr */ - cSize = (U32)LZ4_compress_limitedOutput((const char*)cctxPtr->tmpInputBuffer, (char*)dstPtr, (int)(cctxPtr->tmpInputFilled), (int)(cctxPtr->tmpInputFilled-1)); + cSize = (U32)LZ4_compress_limitedOutput((const char*)cctxPtr->tmpIn, (char*)dstPtr, (int)(cctxPtr->tmpInSize), (int)(cctxPtr->tmpInSize-1)); dstPtr += cSize; LZ4F_writeLE32(cSizePtr, cSize); if (cSize == 0) /* compression failed */ { - cSize = cctxPtr->tmpInputFilled + LZ4F_BLOCKUNCOMPRESSED_FLAG; + cSize = cctxPtr->tmpInSize + LZ4F_BLOCKUNCOMPRESSED_FLAG; LZ4F_writeLE32(cSizePtr, cSize); - memcpy(dstPtr, cctxPtr->tmpInputBuffer, cctxPtr->tmpInputFilled); - dstPtr += cctxPtr->tmpInputFilled; + memcpy(dstPtr, cctxPtr->tmpIn, cctxPtr->tmpInSize); + dstPtr += cctxPtr->tmpInSize; } - cctxPtr->tmpInputFilled = 0; + cctxPtr->tmpInSize = 0; } return dstPtr - dstStart; @@ -571,24 +575,48 @@ size_t LZ4F_compressEnd(LZ4F_compressionContext_t compressionContext, void* dstB typedef struct { LZ4F_frameInfo_t frameInfo; unsigned dStage; - BYTE* tmpInputBuffer; - size_t tmpInputFilled; - size_t tmpInputTarget; + size_t maxBlockSize; + size_t sizeToDecode; + const BYTE* srcExpect; + size_t srcConsumed; + BYTE* tmpIn; + size_t tmpInSize; + size_t tmpInTarget; + BYTE* tmpOut; + size_t tmpOutSize; + size_t tmpOutStart; } LZ4F_dctx_internal_t; - /* Resource management */ /* LZ4F_createDecompressionContext() : * The first thing to do is to create a decompressionContext object, which will be used in all decompression operations. * This is achieved using LZ4F_createDecompressionContext(). - * The function will provide a pointer to a fully allocated and initialised LZ4F_decompressionContext object. + * The function will provide a pointer to a fully allocated and initialized LZ4F_decompressionContext object. * If the result LZ4F_errorCode_t is not zero, there was an error during context creation. * Object can release its memory using LZ4F_freeDecompressionContext(); */ -LZ4F_errorCode_t LZ4F_createDecompressionContext(LZ4F_compressionContext_t* LZ4F_decompressionContextPtr); -LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_compressionContext_t LZ4F_decompressionContext); +LZ4F_errorCode_t LZ4F_createDecompressionContext(LZ4F_compressionContext_t* LZ4F_decompressionContextPtr) +{ + LZ4F_dctx_internal_t* dctxPtr; + + dctxPtr = ALLOCATOR(sizeof(LZ4F_dctx_internal_t)); + if (dctxPtr==NULL) return -ERROR_GENERIC; + + *LZ4F_decompressionContextPtr = (LZ4F_compressionContext_t)dctxPtr; + return OK_NoError; +} + +LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_compressionContext_t LZ4F_decompressionContext) +{ + LZ4F_dctx_internal_t* dctxPtr = (LZ4F_dctx_internal_t*)LZ4F_decompressionContext; + FREEMEM(dctxPtr->tmpIn); + FREEMEM(dctxPtr->tmpOut); + FREEMEM(dctxPtr); + return OK_NoError; +} + /* Decompression */ @@ -598,7 +626,7 @@ static size_t LZ4F_decodeHeader(LZ4F_frameInfo_t* frameInfoPtr, const BYTE* srcP unsigned version, blockMode, blockChecksumFlag, contentSizeFlag, contentChecksumFlag, dictFlag, blockSizeID; /* need to decode header to get frameInfo */ - if (srcSize < 7) return -ERROR_GENERIC; /* minimal decodable size */ + if (srcSize < 7) return -ERROR_GENERIC; /* minimal header size */ /* control magic number */ if (LZ4F_readLE32(srcPtr) != LZ4F_MAGICNUMBER) return -ERROR_GENERIC; @@ -638,6 +666,29 @@ static size_t LZ4F_decodeHeader(LZ4F_frameInfo_t* frameInfoPtr, const BYTE* srcP return 7; } +static LZ4F_errorCode_t LZ4F_checkBuffer(LZ4F_dctx_internal_t* dctxPtr) +{ + size_t newBlockSize = LZ4F_getBlockSize(dctxPtr->frameInfo.blockSizeID); + if (newBlockSize != dctxPtr->maxBlockSize) /* tmp buffers already allocated ; consider > test instead */ + { + FREEMEM(dctxPtr->tmpIn); + FREEMEM(dctxPtr->tmpOut); + dctxPtr->tmpIn = ALLOCATOR(newBlockSize); + if (dctxPtr->tmpIn == NULL) return -ERROR_GENERIC; + dctxPtr->tmpOut= ALLOCATOR(newBlockSize); + if (dctxPtr->tmpOut== NULL) return -ERROR_GENERIC; + dctxPtr->maxBlockSize = newBlockSize; + } + return OK_NoError; +} + + +typedef enum { dstage_header=0, + dstage_getCBlockSize, dstage_storeCBlockSize, dstage_decodeCBlockSize, + dstage_copyDirect, + dstage_getCBlock, dstage_storeCBlock, dstage_decodeCBlock, dstage_flushOut, + dstage_getSuffix, dstage_storeSuffix, dstage_checkSuffix } dStage_t; + /* LZ4F_getFrameInfo() * This function decodes frame header information, such as blockSize. @@ -656,6 +707,9 @@ LZ4F_errorCode_t LZ4F_getFrameInfo(LZ4F_decompressionContext_t decompressionCont LZ4F_errorCode_t errorCode = LZ4F_decodeHeader(frameInfoPtr, srcBuffer, *srcSize); if (LZ4F_isError(errorCode)) return errorCode; *srcSize = errorCode; + errorCode = LZ4F_checkBuffer(dctxPtr); + if (LZ4F_isError(errorCode)) return errorCode; + dctxPtr->dStage = dstage_getCBlockSize; return OK_NoError; } @@ -665,6 +719,7 @@ LZ4F_errorCode_t LZ4F_getFrameInfo(LZ4F_decompressionContext_t decompressionCont return OK_NoError; } + /* LZ4F_decompress() * Call this function repetitively to regenerate data compressed within srcBuffer. * The function will attempt to decode *srcSize from srcBuffer, into dstBuffer of maximum size *dstSize. @@ -689,109 +744,207 @@ LZ4F_errorCode_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContex BYTE* const dstEnd = dstStart + *dstSize; BYTE* dstPtr = dstStart; size_t nextCBlockSize=0; - unsigned nextBlockUncompressedFlag=0; + const BYTE* cBlockSizePtr=NULL; + const BYTE* cBlockPtr=NULL; + LZ4F_errorCode_t goodResult = OK_NoError; + if (decompressOptionsPtr==NULL) decompressOptionsPtr = &optionsNull; - *srcSize = 0; + *srcSize = 0; *dstSize = 0; - if (dctxPtr->dStage == 0) /* header must be decoded */ + /* expect same src buffer as previous function call */ + if (dctxPtr->srcExpect != NULL) { - LZ4F_errorCode_t errorCode = LZ4F_decodeHeader(&(dctxPtr->frameInfo), srcBuffer, *srcSize); - if (LZ4F_isError(errorCode)) return errorCode; - srcPtr += errorCode; - dctxPtr->dStage = 1; + if (srcStart != dctxPtr->srcExpect) return -ERROR_GENERIC; + srcPtr = srcStart + dctxPtr->srcConsumed; } - if (dctxPtr->tmpInputTarget > 0) /* finalize what's already saved*/ + while (srcPtr < srcEnd) { - size_t sizeToCopy = dctxPtr->tmpInputTarget - dctxPtr->tmpInputFilled; - if (sizeToCopy < (size_t)(srcEnd - srcPtr)) sizeToCopy = srcEnd - srcPtr; - memcpy(dctxPtr->tmpInputBuffer + dctxPtr->tmpInputFilled, srcPtr, sizeToCopy); - srcPtr += sizeToCopy; - if (dctxPtr->tmpInputFilled < dctxPtr->tmpInputTarget) goto _end; - - switch (dctxPtr->dStage) + //printf("src : %7i \n", (int)(srcPtr-srcStart)); + switch(dctxPtr->dStage) { - default: - return -ERROR_GENERIC; /* impossible case */ - case 1: - nextCBlockSize = LZ4F_readLE32(dctxPtr->tmpInputBuffer) & 0x7FFFFFFFU; - nextBlockUncompressedFlag = LZ4F_readLE32(dctxPtr->tmpInputBuffer) & 0x80000000U; - dctxPtr->tmpInputFilled = 0; - dctxPtr->tmpInputTarget = 0; - dctxPtr->dStage = 2; - break; - case 2: - if (nextBlockUncompressedFlag) + case dstage_header: { - memcpy(dstPtr, dctxPtr->tmpInputBuffer, nextCBlockSize); - srcPtr += nextCBlockSize; - dstPtr += nextCBlockSize; + LZ4F_errorCode_t errorCode = LZ4F_decodeHeader(&(dctxPtr->frameInfo), srcBuffer, (srcEnd - srcPtr)); + if (LZ4F_isError(errorCode)) return errorCode; + srcPtr += errorCode; + errorCode = LZ4F_checkBuffer(dctxPtr); + if (LZ4F_isError(errorCode)) return errorCode; + dctxPtr->dStage = dstage_getCBlockSize; + /* break; no need to break : dstage_getCBlockSize is next stage */ } - else + case dstage_getCBlockSize: { - int origSize = LZ4_decompress_safe((const char*)(dctxPtr->tmpInputBuffer), (char*)dstPtr, (int)nextCBlockSize, (int)(dstEnd - dstPtr)); /* blindly attempt to decompress, in case there is enough space left */ - srcPtr += nextCBlockSize; - dstPtr += origSize; + if ((srcEnd - srcPtr) < 4) /* not enough input to read cBlockSize */ + { + dctxPtr->tmpInSize = 0; + dctxPtr->dStage = dstage_storeCBlockSize; + break; + } + cBlockSizePtr = srcPtr; + srcPtr += 4; + dctxPtr->dStage = dstage_decodeCBlockSize; + break; } - dctxPtr->tmpInputFilled = 0; - dctxPtr->tmpInputTarget = 0; - dctxPtr->dStage = 1; - break; - } - } - - while (srcPtr < srcEnd) /* can still read */ - { - if (dctxPtr->dStage == 1) /* read next block size */ - { - if ((srcEnd - srcPtr) < 4) + case dstage_storeCBlockSize: { - size_t nbBytesToCopy = (srcEnd - srcPtr); - memcpy(dctxPtr->tmpInputBuffer, srcPtr, nbBytesToCopy); - dctxPtr->tmpInputFilled = nbBytesToCopy; - dctxPtr->tmpInputTarget = 4; - srcPtr = srcEnd; + size_t sizeToCopy = 4 - dctxPtr->tmpInSize; + if (sizeToCopy < (size_t)(srcEnd - srcPtr)) sizeToCopy = srcEnd - srcPtr; + memcpy(dctxPtr->tmpIn + dctxPtr->tmpInSize, srcPtr, sizeToCopy); + srcPtr += sizeToCopy; + dctxPtr->tmpInSize += sizeToCopy; + if (dctxPtr->tmpInSize < 4) break; /* not enough input to read CBlockSize */ + cBlockSizePtr = dctxPtr->tmpIn; + dctxPtr->dStage = dstage_decodeCBlockSize; break; } - nextCBlockSize = LZ4F_readLE32(srcPtr) & 0x7FFFFFFFU; - nextBlockUncompressedFlag = LZ4F_readLE32(srcPtr) & 0x80000000U; - srcPtr += 4; - dctxPtr->dStage = 2; - } - - if (dctxPtr->dStage == 2) /* compressed block */ - { - if ((size_t)(srcEnd - srcPtr) < nextCBlockSize) + case dstage_decodeCBlockSize: { - memcpy(dctxPtr->tmpInputBuffer, srcPtr, srcEnd-srcPtr); - dctxPtr->tmpInputFilled = srcEnd - srcPtr; - dctxPtr->tmpInputTarget = nextCBlockSize; + nextCBlockSize = LZ4F_readLE32(cBlockSizePtr) & 0x7FFFFFFFU; + if (nextCBlockSize==0) /* no more CBlock */ + { + dctxPtr->dStage = dstage_getSuffix; + break; + } + if (nextCBlockSize > dctxPtr->maxBlockSize) return -ERROR_GENERIC; + dctxPtr->sizeToDecode = nextCBlockSize; + if (LZ4F_readLE32(cBlockSizePtr) & 0x80000000U) /* uncompressed flag */ + { + dctxPtr->dStage = dstage_copyDirect; + break; + } + dctxPtr->dStage = dstage_getCBlock; break; } - if (nextBlockUncompressedFlag) + case dstage_copyDirect: { - memcpy(dstPtr, srcPtr, nextCBlockSize); - srcPtr += nextCBlockSize; - dstPtr += nextCBlockSize; + size_t sizeToCopy = dctxPtr->sizeToDecode; + if ((size_t)(srcEnd-srcPtr) < sizeToCopy) sizeToCopy = srcEnd-srcPtr; /* not enough input to read full block */ + if ((size_t)(dstEnd-dstPtr) < sizeToCopy) sizeToCopy = dstEnd - dstPtr; + memcpy(dstPtr, srcPtr, sizeToCopy); + srcPtr += sizeToCopy; + dstPtr += sizeToCopy; + if (sizeToCopy == dctxPtr->sizeToDecode) /* all copied */ + { + dctxPtr->dStage = dstage_getCBlockSize; + break; + } + dctxPtr->sizeToDecode -= sizeToCopy; /* still need to copy more */ + goto _end; /* either In or Out have reached end */ } - else + case dstage_getCBlock: { - int origSize = LZ4_decompress_safe((const char*)srcPtr, (char*)dstPtr, (int)nextCBlockSize, (int)(dstEnd - dstPtr)); /* blindly attempt to decompress, in case there is enough space left */ + if ((size_t)(srcEnd-srcPtr) < nextCBlockSize) + { + dctxPtr->tmpInTarget = nextCBlockSize; + dctxPtr->tmpInSize = 0; + dctxPtr->dStage = dstage_storeCBlock; + break; + } + cBlockPtr = srcPtr; srcPtr += nextCBlockSize; - dstPtr += origSize; + dctxPtr->dStage = dstage_decodeCBlock; + break; + } + case dstage_storeCBlock: + { + size_t sizeToCopy = dctxPtr->tmpInTarget - dctxPtr->tmpInSize; + if (sizeToCopy > (size_t)(srcEnd-srcPtr)) sizeToCopy = srcEnd-srcPtr; + memcpy(dctxPtr->tmpIn + dctxPtr->tmpInSize, srcPtr, sizeToCopy); + dctxPtr->tmpInSize += sizeToCopy; + srcPtr += sizeToCopy; + if (dctxPtr->tmpInSize < dctxPtr->tmpInTarget) break; /* need to read more */ + cBlockPtr = dctxPtr->tmpIn; + dctxPtr->dStage = dstage_decodeCBlock; + break; + } + case dstage_decodeCBlock: + { + int decodedSize; + if ((size_t)(dstEnd-dstPtr) < dctxPtr->maxBlockSize) /* not enough room : decode into tmpOut */ + { + decodedSize = LZ4_decompress_safe((const char*)cBlockPtr, (char*)dctxPtr->tmpOut, (int)dctxPtr->sizeToDecode, (int)dctxPtr->maxBlockSize); + if (decodedSize < 0) return -ERROR_GENERIC; /* decompression failed */ + dctxPtr->tmpOutSize = decodedSize; + dctxPtr->tmpOutStart = 0; + dctxPtr->dStage = dstage_flushOut; + break; + } + decodedSize = LZ4_decompress_safe((const char*)cBlockPtr, (char*)dstPtr, (int)dctxPtr->sizeToDecode, (int)dctxPtr->maxBlockSize); + if (decodedSize < 0) return -ERROR_GENERIC; /* decompression failed */ + dstPtr += decodedSize; + dctxPtr->dStage = dstage_getCBlockSize; + break; + } + case dstage_flushOut: + { + size_t sizeToCopy = dctxPtr->tmpOutSize - dctxPtr->tmpOutStart; + if (sizeToCopy > (size_t)(dstEnd-dstPtr)) sizeToCopy = dstEnd-dstPtr; + memcpy(dstPtr, dctxPtr->tmpOut + dctxPtr->tmpOutStart, sizeToCopy); + dctxPtr->tmpOutStart += sizeToCopy; + dstPtr += sizeToCopy; + if (dctxPtr->tmpOutStart < dctxPtr->tmpOutSize) goto _end; /* need to write more */ + dctxPtr->dStage = dstage_getCBlockSize; + break; + } + case dstage_getSuffix: + { + size_t suffixSize = dctxPtr->frameInfo.contentChecksumFlag * 4; + if (suffixSize == 0) /* frame completed */ + { + goodResult = OK_FrameEnd; + dctxPtr->dStage = dstage_header; + goto _end; + } + if ((srcEnd - srcPtr) >= 4) /* CRC present */ + { + cBlockPtr = srcPtr; + srcPtr += 4; + dctxPtr->dStage = dstage_checkSuffix; + break; + } + dctxPtr->tmpInSize = 0; + dctxPtr->dStage = dstage_storeSuffix; + break; + } + case dstage_storeSuffix: + { + size_t sizeToCopy = 4 - dctxPtr->tmpInSize; + if (sizeToCopy < (size_t)(srcEnd - srcPtr)) sizeToCopy = srcEnd - srcPtr; + memcpy(dctxPtr->tmpIn + dctxPtr->tmpInSize, srcPtr, sizeToCopy); + srcPtr += sizeToCopy; + dctxPtr->tmpInSize += sizeToCopy; + if (dctxPtr->tmpInSize < 4) break; /* not enough input to read suffix */ + cBlockPtr = dctxPtr->tmpIn; + dctxPtr->dStage = dstage_checkSuffix; + break; + } + case dstage_checkSuffix: + { + /* To do */ + goodResult = OK_FrameEnd; + dctxPtr->dStage = dstage_header; + goto _end; } } } - if (srcPtr < srcEnd) - { - dctxPtr->tmpInputFilled = srcEnd-srcPtr; - memcpy(dctxPtr->tmpInputBuffer, srcPtr, dctxPtr->tmpInputFilled); /* save remaining input */ - } + /* input fully read */ _end: + + if (srcPtrsrcExpect = srcStart; + dctxPtr->srcConsumed = srcPtr - srcStart; + } + else + { + dctxPtr->srcExpect = NULL; + dctxPtr->srcConsumed = 0; + } *srcSize = (srcPtr - srcStart); *dstSize = (dstPtr - dstStart); - return OK_NoError; + return goodResult; } diff --git a/lz4frame.h b/lz4frame.h index ae44a3a..1b4ab41 100644 --- a/lz4frame.h +++ b/lz4frame.h @@ -73,7 +73,7 @@ int LZ4F_isError(LZ4F_errorCode_t code); /* Basically : code > -ERROR_maxCode **************************************/ typedef enum { LZ4F_default=0, max64KB=4, max256KB=5, max1MB=6, max4MB=7} blockSizeID_t; -typedef enum { blockLinked=1, blockIndependent} blockMode_t; +typedef enum { blockLinked=0, blockIndependent} blockMode_t; typedef enum { contentChecksumEnabled, noContentChecksum} contentChecksum_t; typedef struct { @@ -203,7 +203,7 @@ LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_compressionContext_t LZ4F_de /* LZ4F_createDecompressionContext() : * The first thing to do is to create a decompressionContext object, which will be used in all decompression operations. * This is achieved using LZ4F_createDecompressionContext(). - * The function will provide a pointer to a fully allocated and initialised LZ4F_decompressionContext object. + * The function will provide a pointer to a fully allocated and initialized LZ4F_decompressionContext object. * If the result LZ4F_errorCode_t is not zero, there was an error during context creation. * Object can release its memory using LZ4F_freeDecompressionContext(); */ @@ -227,12 +227,14 @@ LZ4F_errorCode_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContex * * The number of bytes generated into dstBuffer will be provided within *dstSize (necessarily <= original value). * - * The number of bytes effectively read from srcBuffer will be provided within *srcSize (necessarily <= original value). + * The number of bytes effectively used from srcBuffer will be provided within *srcSize (necessarily <= original value). * If the number of bytes read is < number of bytes provided, then the decompression operation is not complete. - * You will have to call it again, using the same src arguments (but eventually different dst arguments). + * This typically happens when dstBuffer is not large enough to contain all decoded data. + * LZ4F_decompress() will have to be called again, using the same src arguments (but eventually different dst arguments). + * dstBuffer is supposed to be flushed between calls to the function, since its content will be rewritten. * * The function result is an error code which can be tested using LZ4F_isError(). - * When the frame is fully decoded, the function result will be OK_FrameEnd(=1). + * When a frame is fully decoded, the function result will be OK_FrameEnd(=1). */ diff --git a/programs/frametest.c b/programs/frametest.c index e61205a..2b52190 100644 --- a/programs/frametest.c +++ b/programs/frametest.c @@ -187,6 +187,7 @@ int frameTest(U32 seed, int nbCycles, int startCycle, double compressibility) U32 randState = seed; size_t cSize, testSize; LZ4F_preferences_t prefs = { 0 }; + LZ4F_decompressionContext_t dCtx; (void)nbCycles; (void)startCycle; // Create compressible test buffer @@ -202,6 +203,19 @@ int frameTest(U32 seed, int nbCycles, int startCycle, double compressibility) if (LZ4F_isError(cSize)) goto _output_error; DISPLAY("Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); + DISPLAY("Decompression test : \n"); + { + LZ4F_errorCode_t errorCode = LZ4F_createDecompressionContext(&dCtx); + size_t decodedBufferSize = COMPRESSIBLE_NOISE_LENGTH; + size_t compressedBufferSize = cSize; + if (LZ4F_isError(errorCode)) goto _output_error; + errorCode = LZ4F_decompress(dCtx, decodedBuffer, &decodedBufferSize, compressedBuffer, &compressedBufferSize, NULL); + if (LZ4F_isError(errorCode)) goto _output_error; + DISPLAY("Regenerated %i bytes \n", (int)decodedBufferSize); + errorCode = LZ4F_freeDecompressionContext(dCtx); + if (LZ4F_isError(errorCode)) goto _output_error; + } + DISPLAY("Using 64 KB block : \n"); prefs.frameInfo.blockSizeID = max64KB; cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &(prefs.frameInfo)), CNBuffer, testSize, &prefs); @@ -262,6 +276,7 @@ _end: _output_error: testResult = 1; + DISPLAY("Error detected ! \n"); if(!no_prompt) getchar(); goto _end; } -- cgit v0.12 From b03f8f0e6f40d17740685e50332b8eedb5117cec Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 4 Sep 2014 22:56:51 +0100 Subject: fixed : LZ4F_decompress() more decompress tests --- lz4frame.c | 101 +++++++++++++++++++++++++++++++-------------------- lz4frame.h | 14 ++++--- programs/frametest.c | 22 ++++++++++- 3 files changed, 91 insertions(+), 46 deletions(-) diff --git a/lz4frame.c b/lz4frame.c index 80e744e..f8facdb 100644 --- a/lz4frame.c +++ b/lz4frame.c @@ -683,7 +683,7 @@ static LZ4F_errorCode_t LZ4F_checkBuffer(LZ4F_dctx_internal_t* dctxPtr) } -typedef enum { dstage_header=0, +typedef enum { dstage_getHeader=0, dstage_storeHeader, dstage_decodeHeader, dstage_getCBlockSize, dstage_storeCBlockSize, dstage_decodeCBlockSize, dstage_copyDirect, dstage_getCBlock, dstage_storeCBlock, dstage_decodeCBlock, dstage_flushOut, @@ -744,78 +744,101 @@ LZ4F_errorCode_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContex BYTE* const dstEnd = dstStart + *dstSize; BYTE* dstPtr = dstStart; size_t nextCBlockSize=0; - const BYTE* cBlockSizePtr=NULL; - const BYTE* cBlockPtr=NULL; + const BYTE* selectedIn=NULL; LZ4F_errorCode_t goodResult = OK_NoError; if (decompressOptionsPtr==NULL) decompressOptionsPtr = &optionsNull; *srcSize = 0; *dstSize = 0; - /* expect same src buffer as previous function call */ + /* expect to continue decoding src buffer where it left previously */ if (dctxPtr->srcExpect != NULL) { if (srcStart != dctxPtr->srcExpect) return -ERROR_GENERIC; - srcPtr = srcStart + dctxPtr->srcConsumed; } while (srcPtr < srcEnd) { - //printf("src : %7i \n", (int)(srcPtr-srcStart)); switch(dctxPtr->dStage) { - case dstage_header: + case dstage_getHeader: { - LZ4F_errorCode_t errorCode = LZ4F_decodeHeader(&(dctxPtr->frameInfo), srcBuffer, (srcEnd - srcPtr)); + if ((srcEnd-srcPtr)>7) + { + selectedIn = srcPtr; + srcPtr += 7; + dctxPtr->dStage = dstage_decodeHeader; + goto goto_decodeHeader; /* break would risk leaving the while loop */ + } + dctxPtr->tmpInSize = 0; + dctxPtr->dStage = dstage_storeHeader; + /* break; break is useles, since storeHeader follows */ + } + case dstage_storeHeader: + { + size_t sizeToCopy = 7 - dctxPtr->tmpInSize; + if (sizeToCopy > (size_t)(srcEnd - srcPtr)) sizeToCopy = srcEnd - srcPtr; + memcpy(dctxPtr->tmpIn + dctxPtr->tmpInSize, srcPtr, sizeToCopy); + dctxPtr->tmpInSize += sizeToCopy; + srcPtr += sizeToCopy; + if (dctxPtr->tmpInSize < 7) break; /* src completed; come back later for more */ + selectedIn = dctxPtr->tmpIn; + dctxPtr->dStage = dstage_decodeHeader; + /* break; useless because it follows */ + } + case dstage_decodeHeader: +goto_decodeHeader: + { + LZ4F_errorCode_t errorCode = LZ4F_decodeHeader(&(dctxPtr->frameInfo), selectedIn, 7); if (LZ4F_isError(errorCode)) return errorCode; - srcPtr += errorCode; errorCode = LZ4F_checkBuffer(dctxPtr); if (LZ4F_isError(errorCode)) return errorCode; - dctxPtr->dStage = dstage_getCBlockSize; - /* break; no need to break : dstage_getCBlockSize is next stage */ + /* dctxPtr->dStage = dstage_getCBlockSize; break; no need to change stage nor break : dstage_getCBlockSize is next stage, and stage will be modified */ } case dstage_getCBlockSize: { - if ((srcEnd - srcPtr) < 4) /* not enough input to read cBlockSize */ + if ((srcEnd - srcPtr) >= 4) { - dctxPtr->tmpInSize = 0; - dctxPtr->dStage = dstage_storeCBlockSize; - break; + selectedIn = srcPtr; + srcPtr += 4; + dctxPtr->dStage = dstage_decodeCBlockSize; + goto goto_decodeCBlockSize; /* required : a break could leave while loop */ } - cBlockSizePtr = srcPtr; - srcPtr += 4; - dctxPtr->dStage = dstage_decodeCBlockSize; - break; + /* not enough input to read cBlockSize */ + dctxPtr->tmpInSize = 0; + dctxPtr->dStage = dstage_storeCBlockSize; + /* break; No need to break : dstage_storeCBlockSize is next block */ } case dstage_storeCBlockSize: { size_t sizeToCopy = 4 - dctxPtr->tmpInSize; - if (sizeToCopy < (size_t)(srcEnd - srcPtr)) sizeToCopy = srcEnd - srcPtr; + if (sizeToCopy > (size_t)(srcEnd - srcPtr)) sizeToCopy = srcEnd - srcPtr; memcpy(dctxPtr->tmpIn + dctxPtr->tmpInSize, srcPtr, sizeToCopy); srcPtr += sizeToCopy; dctxPtr->tmpInSize += sizeToCopy; if (dctxPtr->tmpInSize < 4) break; /* not enough input to read CBlockSize */ - cBlockSizePtr = dctxPtr->tmpIn; + selectedIn = dctxPtr->tmpIn; dctxPtr->dStage = dstage_decodeCBlockSize; - break; + /* break; No need to break : dstage_decodeCBlockSize is next block */ } case dstage_decodeCBlockSize: +goto_decodeCBlockSize: { - nextCBlockSize = LZ4F_readLE32(cBlockSizePtr) & 0x7FFFFFFFU; + nextCBlockSize = LZ4F_readLE32(selectedIn) & 0x7FFFFFFFU; if (nextCBlockSize==0) /* no more CBlock */ { dctxPtr->dStage = dstage_getSuffix; - break; + goto goto_getSuffix; /* required : a break could leave the while loop */ } if (nextCBlockSize > dctxPtr->maxBlockSize) return -ERROR_GENERIC; dctxPtr->sizeToDecode = nextCBlockSize; - if (LZ4F_readLE32(cBlockSizePtr) & 0x80000000U) /* uncompressed flag */ + if (LZ4F_readLE32(selectedIn) & 0x80000000U) /* uncompressed flag */ { dctxPtr->dStage = dstage_copyDirect; break; } dctxPtr->dStage = dstage_getCBlock; - break; + goto goto_getCBlock; /* break risk leaving while loop */ } case dstage_copyDirect: { @@ -834,6 +857,7 @@ LZ4F_errorCode_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContex goto _end; /* either In or Out have reached end */ } case dstage_getCBlock: +goto_getCBlock: { if ((size_t)(srcEnd-srcPtr) < nextCBlockSize) { @@ -842,7 +866,7 @@ LZ4F_errorCode_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContex dctxPtr->dStage = dstage_storeCBlock; break; } - cBlockPtr = srcPtr; + selectedIn = srcPtr; srcPtr += nextCBlockSize; dctxPtr->dStage = dstage_decodeCBlock; break; @@ -855,23 +879,23 @@ LZ4F_errorCode_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContex dctxPtr->tmpInSize += sizeToCopy; srcPtr += sizeToCopy; if (dctxPtr->tmpInSize < dctxPtr->tmpInTarget) break; /* need to read more */ - cBlockPtr = dctxPtr->tmpIn; + selectedIn = dctxPtr->tmpIn; dctxPtr->dStage = dstage_decodeCBlock; - break; + /* break; break unnecessary because it follows */ } case dstage_decodeCBlock: { int decodedSize; if ((size_t)(dstEnd-dstPtr) < dctxPtr->maxBlockSize) /* not enough room : decode into tmpOut */ { - decodedSize = LZ4_decompress_safe((const char*)cBlockPtr, (char*)dctxPtr->tmpOut, (int)dctxPtr->sizeToDecode, (int)dctxPtr->maxBlockSize); + decodedSize = LZ4_decompress_safe((const char*)selectedIn, (char*)dctxPtr->tmpOut, (int)dctxPtr->sizeToDecode, (int)dctxPtr->maxBlockSize); if (decodedSize < 0) return -ERROR_GENERIC; /* decompression failed */ dctxPtr->tmpOutSize = decodedSize; dctxPtr->tmpOutStart = 0; dctxPtr->dStage = dstage_flushOut; break; } - decodedSize = LZ4_decompress_safe((const char*)cBlockPtr, (char*)dstPtr, (int)dctxPtr->sizeToDecode, (int)dctxPtr->maxBlockSize); + decodedSize = LZ4_decompress_safe((const char*)selectedIn, (char*)dstPtr, (int)dctxPtr->sizeToDecode, (int)dctxPtr->maxBlockSize); if (decodedSize < 0) return -ERROR_GENERIC; /* decompression failed */ dstPtr += decodedSize; dctxPtr->dStage = dstage_getCBlockSize; @@ -889,17 +913,18 @@ LZ4F_errorCode_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContex break; } case dstage_getSuffix: +goto_getSuffix: { size_t suffixSize = dctxPtr->frameInfo.contentChecksumFlag * 4; if (suffixSize == 0) /* frame completed */ { goodResult = OK_FrameEnd; - dctxPtr->dStage = dstage_header; + dctxPtr->dStage = dstage_getHeader; goto _end; } if ((srcEnd - srcPtr) >= 4) /* CRC present */ { - cBlockPtr = srcPtr; + selectedIn = srcPtr; srcPtr += 4; dctxPtr->dStage = dstage_checkSuffix; break; @@ -916,7 +941,7 @@ LZ4F_errorCode_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContex srcPtr += sizeToCopy; dctxPtr->tmpInSize += sizeToCopy; if (dctxPtr->tmpInSize < 4) break; /* not enough input to read suffix */ - cBlockPtr = dctxPtr->tmpIn; + selectedIn = dctxPtr->tmpIn; dctxPtr->dStage = dstage_checkSuffix; break; } @@ -924,7 +949,7 @@ LZ4F_errorCode_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContex { /* To do */ goodResult = OK_FrameEnd; - dctxPtr->dStage = dstage_header; + dctxPtr->dStage = dstage_getHeader; goto _end; } } @@ -934,15 +959,13 @@ LZ4F_errorCode_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContex _end: - if (srcPtrsrcExpect = srcStart; - dctxPtr->srcConsumed = srcPtr - srcStart; + dctxPtr->srcExpect = srcPtr; } else { dctxPtr->srcExpect = NULL; - dctxPtr->srcConsumed = 0; } *srcSize = (srcPtr - srcStart); *dstSize = (dstPtr - dstStart); diff --git a/lz4frame.h b/lz4frame.h index 1b4ab41..ee2290b 100644 --- a/lz4frame.h +++ b/lz4frame.h @@ -74,7 +74,7 @@ int LZ4F_isError(LZ4F_errorCode_t code); /* Basically : code > -ERROR_maxCode typedef enum { LZ4F_default=0, max64KB=4, max256KB=5, max1MB=6, max4MB=7} blockSizeID_t; typedef enum { blockLinked=0, blockIndependent} blockMode_t; -typedef enum { contentChecksumEnabled, noContentChecksum} contentChecksum_t; +typedef enum { noContentChecksum=0, contentChecksumEnabled } contentChecksum_t; typedef struct { blockSizeID_t blockSizeID; /* max64KB, max256KB, max1MB, max4MB ; 0 == default */ @@ -220,18 +220,20 @@ LZ4F_errorCode_t LZ4F_getFrameInfo(LZ4F_decompressionContext_t decompressionCont * The function result is an error code which can be tested using LZ4F_isError(). */ -LZ4F_errorCode_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContext, void* dstBuffer, size_t* dstSize, const void* srcBuffer, size_t* srcSize, const LZ4F_decompressOptions_t* decompressOptionsPtr); +LZ4F_errorCode_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContext, void* dstBuffer, size_t* dstSizePtr, const void* srcBuffer, size_t* srcSizePtr, const LZ4F_decompressOptions_t* decompressOptionsPtr); /* LZ4F_decompress() * Call this function repetitively to regenerate data compressed within srcBuffer. - * The function will attempt to decode *srcSize from srcBuffer, into dstBuffer of maximum size *dstSize. + * The function will attempt to decode *srcSizePtr bytes from srcBuffer, into dstBuffer of maximum size *dstSizePtr. * - * The number of bytes generated into dstBuffer will be provided within *dstSize (necessarily <= original value). + * The number of bytes generated into dstBuffer will be provided within *dstSizePtr (necessarily <= original value). * - * The number of bytes effectively used from srcBuffer will be provided within *srcSize (necessarily <= original value). + * The number of bytes effectively used from srcBuffer will be provided within *srcSizePtr (necessarily <= original value). * If the number of bytes read is < number of bytes provided, then the decompression operation is not complete. * This typically happens when dstBuffer is not large enough to contain all decoded data. - * LZ4F_decompress() will have to be called again, using the same src arguments (but eventually different dst arguments). + * LZ4F_decompress() will have to be called again, starting from where it stopped (srcBuffer + *srcSizePtr) + * The function will check this condition, and refuse to continue if it is not respected. * dstBuffer is supposed to be flushed between calls to the function, since its content will be rewritten. + * Different dst arguments can be used. * * The function result is an error code which can be tested using LZ4F_isError(). * When a frame is fully decoded, the function result will be OK_FrameEnd(=1). diff --git a/programs/frametest.c b/programs/frametest.c index 2b52190..7742110 100644 --- a/programs/frametest.c +++ b/programs/frametest.c @@ -205,13 +205,33 @@ int frameTest(U32 seed, int nbCycles, int startCycle, double compressibility) DISPLAY("Decompression test : \n"); { - LZ4F_errorCode_t errorCode = LZ4F_createDecompressionContext(&dCtx); size_t decodedBufferSize = COMPRESSIBLE_NOISE_LENGTH; size_t compressedBufferSize = cSize; + BYTE* op = (BYTE*)decodedBuffer; + BYTE* const oend = (BYTE*)decodedBuffer + COMPRESSIBLE_NOISE_LENGTH; + BYTE* ip = (BYTE*)compressedBuffer; + BYTE* const iend = (BYTE*)compressedBuffer + cSize; + LZ4F_errorCode_t errorCode = LZ4F_createDecompressionContext(&dCtx); if (LZ4F_isError(errorCode)) goto _output_error; + + DISPLAY("Single Block : \n"); errorCode = LZ4F_decompress(dCtx, decodedBuffer, &decodedBufferSize, compressedBuffer, &compressedBufferSize, NULL); if (LZ4F_isError(errorCode)) goto _output_error; DISPLAY("Regenerated %i bytes \n", (int)decodedBufferSize); + + DISPLAY("Byte after byte : \n"); + while (ip < iend) + { + size_t oSize = oend-op; + size_t iSize = 1; + //DISPLAY("%7i \n", (int)(ip-(BYTE*)compressedBuffer)); + errorCode = LZ4F_decompress(dCtx, op, &oSize, ip, &iSize, NULL); + if (LZ4F_isError(errorCode)) goto _output_error; + op += oSize; + ip += iSize; + } + DISPLAY("Regenerated %i bytes \n", (int)decodedBufferSize); + errorCode = LZ4F_freeDecompressionContext(dCtx); if (LZ4F_isError(errorCode)) goto _output_error; } -- cgit v0.12 From 9732cf4d40fb63a44336cf9bf9b37ad47e9e3284 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 4 Sep 2014 22:57:32 +0100 Subject: Added : xxhash.h at root, for compilation --- xxhash.h | 168 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 168 insertions(+) create mode 100644 xxhash.h diff --git a/xxhash.h b/xxhash.h new file mode 100644 index 0000000..4311485 --- /dev/null +++ b/xxhash.h @@ -0,0 +1,168 @@ +/* + xxHash - Extremely Fast Hash algorithm + Header File + Copyright (C) 2012-2014, Yann Collet. + BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + You can contact the author at : + - xxHash source repository : http://code.google.com/p/xxhash/ +*/ + +/* Notice extracted from xxHash homepage : + +xxHash is an extremely fast Hash algorithm, running at RAM speed limits. +It also successfully passes all tests from the SMHasher suite. + +Comparison (single thread, Windows Seven 32 bits, using SMHasher on a Core 2 Duo @3GHz) + +Name Speed Q.Score Author +xxHash 5.4 GB/s 10 +CrapWow 3.2 GB/s 2 Andrew +MumurHash 3a 2.7 GB/s 10 Austin Appleby +SpookyHash 2.0 GB/s 10 Bob Jenkins +SBox 1.4 GB/s 9 Bret Mulvey +Lookup3 1.2 GB/s 9 Bob Jenkins +SuperFastHash 1.2 GB/s 1 Paul Hsieh +CityHash64 1.05 GB/s 10 Pike & Alakuijala +FNV 0.55 GB/s 5 Fowler, Noll, Vo +CRC32 0.43 GB/s 9 +MD5-32 0.33 GB/s 10 Ronald L. Rivest +SHA1-32 0.28 GB/s 10 + +Q.Score is a measure of quality of the hash function. +It depends on successfully passing SMHasher test set. +10 is a perfect score. +*/ + +#pragma once + +#if defined (__cplusplus) +extern "C" { +#endif + + +/***************************** + Type +*****************************/ +typedef enum { XXH_OK=0, XXH_ERROR } XXH_errorcode; + + + +/***************************** + Simple Hash Functions +*****************************/ + +unsigned int XXH32 (const void* input, unsigned int len, unsigned int seed); +unsigned long long XXH64 (const void* input, unsigned int len, unsigned long long seed); + +/* +XXH32() : + Calculate the 32-bits hash of sequence of length "len" stored at memory address "input". + The memory between input & input+len must be valid (allocated and read-accessible). + "seed" can be used to alter the result predictably. + This function successfully passes all SMHasher tests. + Speed on Core 2 Duo @ 3 GHz (single thread, SMHasher benchmark) : 5.4 GB/s + Note that "len" is type "int", which means it is limited to 2^31-1. + If your data is larger, use the advanced functions below. +XXH64() : + Calculate the 64-bits hash of sequence of length "len" stored at memory address "input". +*/ + + + +/***************************** + Advanced Hash Functions +*****************************/ + +void* XXH32_init (unsigned int seed); +XXH_errorcode XXH32_update (void* state, const void* input, unsigned int len); +unsigned int XXH32_digest (void* state); + +void* XXH64_init (unsigned long long seed); +XXH_errorcode XXH64_update (void* state, const void* input, unsigned int len); +unsigned long long XXH64_digest (void* state); + +/* +These functions calculate the xxhash of an input provided in several small packets, +as opposed to an input provided as a single block. + +It must be started with : +void* XXHnn_init() +The function returns a pointer which holds the state of calculation. +If the pointer is NULL, allocation has failed, so no state can be tracked. + +The state pointer must be provided as "void* state" parameter for XXHnn_update(). +XXHnn_update() can be called as many times as necessary. +The user must provide a valid (allocated) input. +The function returns an error code, with 0 meaning OK, and any other value meaning there is an error. +Note that "len" is type "int", which means it is limited to 2^31-1. +If your data is larger, it is recommended to chunk your data into blocks +of size for example 2^30 (1GB) to avoid any "int" overflow issue. + +Finally, you can end the calculation anytime, by using XXHnn_digest(). +This function returns the final nn-bits hash. +You must provide the same "void* state" parameter created by XXHnn_init(). +Memory will be freed by XXHnn_digest(). +*/ + + +int XXH32_sizeofState(void); +XXH_errorcode XXH32_resetState(void* state, unsigned int seed); + +#define XXH32_SIZEOFSTATE 48 +typedef struct { long long ll[(XXH32_SIZEOFSTATE+(sizeof(long long)-1))/sizeof(long long)]; } XXH32_stateSpace_t; + +int XXH64_sizeofState(void); +XXH_errorcode XXH64_resetState(void* state, unsigned long long seed); + +#define XXH64_SIZEOFSTATE 88 +typedef struct { long long ll[(XXH64_SIZEOFSTATE+(sizeof(long long)-1))/sizeof(long long)]; } XXH64_stateSpace_t; + +/* +These functions allow user application to make its own allocation for state. + +XXHnn_sizeofState() is used to know how much space must be allocated for the xxHash nn-bits state. +Note that the state must be aligned to access 'long long' fields. Memory must be allocated and referenced by a pointer. +This pointer must then be provided as 'state' into XXHnn_resetState(), which initializes the state. + +For static allocation purposes (such as allocation on stack, or freestanding systems without malloc()), +use the structure XXHnn_stateSpace_t, which will ensure that memory space is large enough and correctly aligned to access 'long long' fields. +*/ + + +unsigned int XXH32_intermediateDigest (void* state); +unsigned long long XXH64_intermediateDigest (void* state); +/* +These functions do the same as XXHnn_digest(), generating a nn-bit hash, +but preserve memory context. +This way, it becomes possible to generate intermediate hashes, and then continue feeding data with XXHnn_update(). +To free memory context, use XXHnn_digest(), or free(). +*/ + + +#if defined (__cplusplus) +} +#endif -- cgit v0.12 From 28f38c328d0974c1020e2c3a5a2ba9e085da5280 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 5 Sep 2014 15:50:06 +0100 Subject: Implemented Content Checksum Verification within LZ4F_decompress() --- lz4frame.c | 89 ++++++++++++++++++++++++++++++++------------------------------ lz4frame.h | 10 ++++--- 2 files changed, 53 insertions(+), 46 deletions(-) diff --git a/lz4frame.c b/lz4frame.c index f8facdb..ef9c053 100644 --- a/lz4frame.c +++ b/lz4frame.c @@ -122,6 +122,23 @@ typedef struct { size_t tmpInSize; } LZ4F_cctx_internal_t; +typedef struct { + LZ4F_frameInfo_t frameInfo; + unsigned version; + unsigned dStage; + size_t maxBlockSize; + XXH32_stateSpace_t xxh; + size_t sizeToDecode; + const BYTE* srcExpect; + BYTE* tmpIn; + size_t tmpInSize; + size_t tmpInTarget; + BYTE* tmpOut; + size_t tmpOutSize; + size_t tmpOutStart; + BYTE header[7]; +} LZ4F_dctx_internal_t; + /************************************** Macros @@ -572,22 +589,6 @@ size_t LZ4F_compressEnd(LZ4F_compressionContext_t compressionContext, void* dstB * Decompression functions * *********************************/ -typedef struct { - LZ4F_frameInfo_t frameInfo; - unsigned dStage; - size_t maxBlockSize; - size_t sizeToDecode; - const BYTE* srcExpect; - size_t srcConsumed; - BYTE* tmpIn; - size_t tmpInSize; - size_t tmpInTarget; - BYTE* tmpOut; - size_t tmpOutSize; - size_t tmpOutStart; -} LZ4F_dctx_internal_t; - - /* Resource management */ /* LZ4F_createDecompressionContext() : @@ -597,13 +598,14 @@ typedef struct { * If the result LZ4F_errorCode_t is not zero, there was an error during context creation. * Object can release its memory using LZ4F_freeDecompressionContext(); */ -LZ4F_errorCode_t LZ4F_createDecompressionContext(LZ4F_compressionContext_t* LZ4F_decompressionContextPtr) +LZ4F_errorCode_t LZ4F_createDecompressionContext(LZ4F_compressionContext_t* LZ4F_decompressionContextPtr, unsigned versionNumber) { LZ4F_dctx_internal_t* dctxPtr; dctxPtr = ALLOCATOR(sizeof(LZ4F_dctx_internal_t)); if (dctxPtr==NULL) return -ERROR_GENERIC; + dctxPtr->version = versionNumber; *LZ4F_decompressionContextPtr = (LZ4F_compressionContext_t)dctxPtr; return OK_NoError; } @@ -620,7 +622,7 @@ LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_compressionContext_t LZ4F_de /* Decompression */ -static size_t LZ4F_decodeHeader(LZ4F_frameInfo_t* frameInfoPtr, const BYTE* srcPtr, size_t srcSize) +static size_t LZ4F_decodeHeader(LZ4F_dctx_internal_t* dctxPtr, const BYTE* srcPtr, size_t srcSize) { BYTE FLG, BD, HC; unsigned version, blockMode, blockChecksumFlag, contentSizeFlag, contentChecksumFlag, dictFlag, blockSizeID; @@ -659,27 +661,25 @@ static size_t LZ4F_decodeHeader(LZ4F_frameInfo_t* frameInfoPtr, const BYTE* srcP if (((BD>>0)&_4BITS) != 0) return -ERROR_GENERIC; /* Reserved bits */ /* save */ - frameInfoPtr->blockMode = blockMode; - frameInfoPtr->contentChecksumFlag = contentChecksumFlag; - frameInfoPtr->blockSizeID = blockSizeID; + dctxPtr->frameInfo.blockMode = blockMode; + dctxPtr->frameInfo.contentChecksumFlag = contentChecksumFlag; + dctxPtr->frameInfo.blockSizeID = blockSizeID; - return 7; -} + /* init */ + if (contentChecksumFlag) XXH32_resetState(&(dctxPtr->xxh), 0); -static LZ4F_errorCode_t LZ4F_checkBuffer(LZ4F_dctx_internal_t* dctxPtr) -{ - size_t newBlockSize = LZ4F_getBlockSize(dctxPtr->frameInfo.blockSizeID); - if (newBlockSize != dctxPtr->maxBlockSize) /* tmp buffers already allocated ; consider > test instead */ + if (LZ4F_getBlockSize(blockSizeID) > dctxPtr->maxBlockSize) /* tmp buffers too small */ { FREEMEM(dctxPtr->tmpIn); FREEMEM(dctxPtr->tmpOut); - dctxPtr->tmpIn = ALLOCATOR(newBlockSize); + dctxPtr->tmpIn = ALLOCATOR(LZ4F_getBlockSize(blockSizeID)); if (dctxPtr->tmpIn == NULL) return -ERROR_GENERIC; - dctxPtr->tmpOut= ALLOCATOR(newBlockSize); + dctxPtr->tmpOut= ALLOCATOR(LZ4F_getBlockSize(blockSizeID)); if (dctxPtr->tmpOut== NULL) return -ERROR_GENERIC; - dctxPtr->maxBlockSize = newBlockSize; + dctxPtr->maxBlockSize = LZ4F_getBlockSize(blockSizeID); } - return OK_NoError; + + return 7; } @@ -704,11 +704,9 @@ LZ4F_errorCode_t LZ4F_getFrameInfo(LZ4F_decompressionContext_t decompressionCont if (dctxPtr->dStage==0) { - LZ4F_errorCode_t errorCode = LZ4F_decodeHeader(frameInfoPtr, srcBuffer, *srcSize); + LZ4F_errorCode_t errorCode = LZ4F_decodeHeader(dctxPtr, srcBuffer, *srcSize); if (LZ4F_isError(errorCode)) return errorCode; *srcSize = errorCode; - errorCode = LZ4F_checkBuffer(dctxPtr); - if (LZ4F_isError(errorCode)) return errorCode; dctxPtr->dStage = dstage_getCBlockSize; return OK_NoError; } @@ -763,7 +761,7 @@ LZ4F_errorCode_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContex { case dstage_getHeader: { - if ((srcEnd-srcPtr)>7) + if (srcEnd-srcPtr >= 7) { selectedIn = srcPtr; srcPtr += 7; @@ -778,20 +776,18 @@ LZ4F_errorCode_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContex { size_t sizeToCopy = 7 - dctxPtr->tmpInSize; if (sizeToCopy > (size_t)(srcEnd - srcPtr)) sizeToCopy = srcEnd - srcPtr; - memcpy(dctxPtr->tmpIn + dctxPtr->tmpInSize, srcPtr, sizeToCopy); + memcpy(dctxPtr->header + dctxPtr->tmpInSize, srcPtr, sizeToCopy); dctxPtr->tmpInSize += sizeToCopy; srcPtr += sizeToCopy; if (dctxPtr->tmpInSize < 7) break; /* src completed; come back later for more */ - selectedIn = dctxPtr->tmpIn; + selectedIn = dctxPtr->header; dctxPtr->dStage = dstage_decodeHeader; /* break; useless because it follows */ } case dstage_decodeHeader: goto_decodeHeader: { - LZ4F_errorCode_t errorCode = LZ4F_decodeHeader(&(dctxPtr->frameInfo), selectedIn, 7); - if (LZ4F_isError(errorCode)) return errorCode; - errorCode = LZ4F_checkBuffer(dctxPtr); + LZ4F_errorCode_t errorCode = LZ4F_decodeHeader(dctxPtr, selectedIn, 7); if (LZ4F_isError(errorCode)) return errorCode; /* dctxPtr->dStage = dstage_getCBlockSize; break; no need to change stage nor break : dstage_getCBlockSize is next stage, and stage will be modified */ } @@ -890,6 +886,8 @@ goto_getCBlock: { decodedSize = LZ4_decompress_safe((const char*)selectedIn, (char*)dctxPtr->tmpOut, (int)dctxPtr->sizeToDecode, (int)dctxPtr->maxBlockSize); if (decodedSize < 0) return -ERROR_GENERIC; /* decompression failed */ + if (dctxPtr->frameInfo.contentChecksumFlag) + XXH32_update(&(dctxPtr->xxh), dctxPtr->tmpOut, decodedSize); dctxPtr->tmpOutSize = decodedSize; dctxPtr->tmpOutStart = 0; dctxPtr->dStage = dstage_flushOut; @@ -897,6 +895,8 @@ goto_getCBlock: } decodedSize = LZ4_decompress_safe((const char*)selectedIn, (char*)dstPtr, (int)dctxPtr->sizeToDecode, (int)dctxPtr->maxBlockSize); if (decodedSize < 0) return -ERROR_GENERIC; /* decompression failed */ + if (dctxPtr->frameInfo.contentChecksumFlag) + XXH32_update(&(dctxPtr->xxh), dstPtr, decodedSize); dstPtr += decodedSize; dctxPtr->dStage = dstage_getCBlockSize; break; @@ -927,11 +927,11 @@ goto_getSuffix: selectedIn = srcPtr; srcPtr += 4; dctxPtr->dStage = dstage_checkSuffix; - break; + goto goto_checkSuffix; /* break risks leaving the while loop */ } dctxPtr->tmpInSize = 0; dctxPtr->dStage = dstage_storeSuffix; - break; + /* break; useless, it follow */ } case dstage_storeSuffix: { @@ -946,8 +946,11 @@ goto_getSuffix: break; } case dstage_checkSuffix: +goto_checkSuffix: { - /* To do */ + U32 readCRC = LZ4F_readLE32(selectedIn); + U32 resultCRC = XXH32_intermediateDigest(&(dctxPtr->xxh)); + if (readCRC != resultCRC) return -ERROR_GENERIC; goodResult = OK_FrameEnd; dctxPtr->dStage = dstage_getHeader; goto _end; diff --git a/lz4frame.h b/lz4frame.h index ee2290b..b4a067e 100644 --- a/lz4frame.h +++ b/lz4frame.h @@ -80,12 +80,14 @@ typedef struct { blockSizeID_t blockSizeID; /* max64KB, max256KB, max1MB, max4MB ; 0 == default */ blockMode_t blockMode; /* blockLinked, blockIndependent ; 0 == default */ contentChecksum_t contentChecksumFlag; /* contentChecksumEnabled (default), noContentChecksum ; */ + unsigned reserved[3]; } LZ4F_frameInfo_t; typedef struct { LZ4F_frameInfo_t frameInfo; unsigned compressionLevel; /* from 0 to 16 */ unsigned autoFlush; /* 1 == automatic flush after each call to LZ4F_compress() */ + unsigned reserved[4]; } LZ4F_preferences_t; @@ -115,7 +117,8 @@ size_t LZ4F_compressFrame(void* dstBuffer, size_t dstMaxSize, const void* srcBuf typedef void* LZ4F_compressionContext_t; typedef struct { - int stableSrc; /* unused for the time being, must be 0 */ + unsigned stableSrc; /* unused for the time being, must be 0 */ + unsigned reserved[5]; } LZ4F_compressOptions_t; /* Resource Management */ @@ -193,12 +196,13 @@ size_t LZ4F_compressEnd(LZ4F_compressionContext_t compressionContext, void* dstB typedef void* LZ4F_decompressionContext_t; typedef struct { - int stableDst; /* unused for the time being, must be 0 */ + unsigned stableDst; /* unused for the time being, must be 0 */ + unsigned reserved[5]; } LZ4F_decompressOptions_t; /* Resource management */ -LZ4F_errorCode_t LZ4F_createDecompressionContext(LZ4F_compressionContext_t* LZ4F_decompressionContextPtr); +LZ4F_errorCode_t LZ4F_createDecompressionContext(LZ4F_compressionContext_t* LZ4F_decompressionContextPtr, unsigned versionNumber); LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_compressionContext_t LZ4F_decompressionContext); /* LZ4F_createDecompressionContext() : * The first thing to do is to create a decompressionContext object, which will be used in all decompression operations. -- cgit v0.12 From fd8665320fe03f9f61d438a3cc81502406859c2a Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 5 Sep 2014 16:32:04 +0100 Subject: Added some more tests --- lz4frame.c | 2 +- programs/frametest.c | 186 +++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 152 insertions(+), 36 deletions(-) diff --git a/lz4frame.c b/lz4frame.c index ef9c053..ec2af04 100644 --- a/lz4frame.c +++ b/lz4frame.c @@ -105,7 +105,7 @@ #define LZ4F_MAGICNUMBER 0x184D2204U #define LZ4F_BLOCKUNCOMPRESSED_FLAG 0x80000000U -#define LZ4F_MAXHEADERFRAME_SIZE 19 +#define LZ4F_MAXHEADERFRAME_SIZE 7 #define LZ4F_BLOCKSIZEID_DEFAULT 4 diff --git a/programs/frametest.c b/programs/frametest.c index 7742110..7d26bee 100644 --- a/programs/frametest.c +++ b/programs/frametest.c @@ -45,6 +45,7 @@ #include // timeb #include // strcmp #include "lz4frame.h" +#include "xxhash.h" // XXH64 /************************************** @@ -91,8 +92,10 @@ /************************************** Macros **************************************/ -#define DISPLAY(...) fprintf(stderr, __VA_ARGS__) -#define DISPLAYLEVEL(l, ...) if (displayLevel>=l) { DISPLAY(__VA_ARGS__); } +#define DISPLAY(...) fprintf(stderr, __VA_ARGS__) +#define DISPLAYLEVEL(l, ...) if (displayLevel>=l) { DISPLAY(__VA_ARGS__); } +#define DISPLAYUPDATE(l, ...) if (displayLevel>=l) { if (FUZ_GetMilliStart() > g_time + 200) { g_time = FUZ_GetMilliStart(); DISPLAY(__VA_ARGS__); } } +static int g_time = 0; /***************************************** @@ -100,7 +103,7 @@ *****************************************/ static int no_prompt = 0; static char* programName; -static int displayLevel = 2; +static int displayLevel = 3; /********************************************************* @@ -134,12 +137,12 @@ unsigned int FUZ_rand(unsigned int* src) rand32 += PRIME2; rand32 = FUZ_rotl32(rand32, 13); *src = rand32; - return rand32 >> 3; + return rand32 >> 5; } -#define FUZ_RAND15BITS ((FUZ_rand(seed) >> 3) & 32767) -#define FUZ_RANDLENGTH ( ((FUZ_rand(seed) >> 7) & 3) ? (FUZ_rand(seed) % 15) : (FUZ_rand(seed) % 510) + 15) +#define FUZ_RAND15BITS (FUZ_rand(seed) & 0x7FFF) +#define FUZ_RANDLENGTH ( (FUZ_rand(seed) & 3) ? (FUZ_rand(seed) % 15) : (FUZ_rand(seed) % 510) + 15) static void FUZ_fillCompressibleNoiseBuffer(void* buffer, unsigned bufferSize, double proba, U32* seed) { BYTE* BBuffer = (BYTE*)buffer; @@ -177,8 +180,16 @@ static void FUZ_fillCompressibleNoiseBuffer(void* buffer, unsigned bufferSize, d } +static unsigned FUZ_highbit(U32 v32) +{ + unsigned nbBits = 0; + if (v32==0) return 0; + while (v32) { v32 >>= 1; nbBits ++; } + return nbBits; +} -int frameTest(U32 seed, int nbCycles, int startCycle, double compressibility) + +int basicTests(U32 seed, int nbCycles, int startCycle, double compressibility) { int testResult = 0; void* CNBuffer; @@ -188,22 +199,24 @@ int frameTest(U32 seed, int nbCycles, int startCycle, double compressibility) size_t cSize, testSize; LZ4F_preferences_t prefs = { 0 }; LZ4F_decompressionContext_t dCtx; + U64 crcOrig; (void)nbCycles; (void)startCycle; // Create compressible test buffer CNBuffer = malloc(COMPRESSIBLE_NOISE_LENGTH); - FUZ_fillCompressibleNoiseBuffer(CNBuffer, COMPRESSIBLE_NOISE_LENGTH, compressibility, &randState); compressedBuffer = malloc(LZ4F_compressFrameBound(COMPRESSIBLE_NOISE_LENGTH, NULL)); decodedBuffer = malloc(COMPRESSIBLE_NOISE_LENGTH); + FUZ_fillCompressibleNoiseBuffer(CNBuffer, COMPRESSIBLE_NOISE_LENGTH, compressibility, &randState); + crcOrig = XXH64(CNBuffer, COMPRESSIBLE_NOISE_LENGTH, 1); // Trivial tests : one-step frame testSize = COMPRESSIBLE_NOISE_LENGTH; - DISPLAY("Using NULL preferences : \n"); + DISPLAYLEVEL(3, "Using NULL preferences : \n"); cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, NULL), CNBuffer, testSize, NULL); if (LZ4F_isError(cSize)) goto _output_error; - DISPLAY("Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); + DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); - DISPLAY("Decompression test : \n"); + DISPLAYLEVEL(3, "Decompression test : \n"); { size_t decodedBufferSize = COMPRESSIBLE_NOISE_LENGTH; size_t compressedBufferSize = cSize; @@ -211,15 +224,18 @@ int frameTest(U32 seed, int nbCycles, int startCycle, double compressibility) BYTE* const oend = (BYTE*)decodedBuffer + COMPRESSIBLE_NOISE_LENGTH; BYTE* ip = (BYTE*)compressedBuffer; BYTE* const iend = (BYTE*)compressedBuffer + cSize; - LZ4F_errorCode_t errorCode = LZ4F_createDecompressionContext(&dCtx); + U64 crcDest; + + LZ4F_errorCode_t errorCode = LZ4F_createDecompressionContext(&dCtx, LZ4F_VERSION); if (LZ4F_isError(errorCode)) goto _output_error; - DISPLAY("Single Block : \n"); + DISPLAYLEVEL(3, "Single Block : \n"); errorCode = LZ4F_decompress(dCtx, decodedBuffer, &decodedBufferSize, compressedBuffer, &compressedBufferSize, NULL); - if (LZ4F_isError(errorCode)) goto _output_error; - DISPLAY("Regenerated %i bytes \n", (int)decodedBufferSize); + crcDest = XXH64(decodedBuffer, COMPRESSIBLE_NOISE_LENGTH, 1); + if (crcDest != crcOrig) goto _output_error; + DISPLAYLEVEL(3, "Regenerated %i bytes \n", (int)decodedBufferSize); - DISPLAY("Byte after byte : \n"); + DISPLAYLEVEL(3, "Byte after byte : \n"); while (ip < iend) { size_t oSize = oend-op; @@ -230,63 +246,99 @@ int frameTest(U32 seed, int nbCycles, int startCycle, double compressibility) op += oSize; ip += iSize; } - DISPLAY("Regenerated %i bytes \n", (int)decodedBufferSize); + crcDest = XXH64(decodedBuffer, COMPRESSIBLE_NOISE_LENGTH, 1); + if (crcDest != crcOrig) goto _output_error; + DISPLAYLEVEL(3, "Regenerated %i bytes \n", (int)decodedBufferSize); errorCode = LZ4F_freeDecompressionContext(dCtx); if (LZ4F_isError(errorCode)) goto _output_error; } - DISPLAY("Using 64 KB block : \n"); + DISPLAYLEVEL(3, "Using 64 KB block : \n"); prefs.frameInfo.blockSizeID = max64KB; + prefs.frameInfo.contentChecksumFlag = contentChecksumEnabled; cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &(prefs.frameInfo)), CNBuffer, testSize, &prefs); if (LZ4F_isError(cSize)) goto _output_error; - DISPLAY("Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); + DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); - DISPLAY("without checksum : \n"); + DISPLAYLEVEL(3, "without checksum : \n"); prefs.frameInfo.contentChecksumFlag = noContentChecksum; cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &(prefs.frameInfo)), CNBuffer, testSize, &prefs); if (LZ4F_isError(cSize)) goto _output_error; - DISPLAY("Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); + DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); - DISPLAY("Using 256 KB block : \n"); + DISPLAYLEVEL(3, "Using 256 KB block : \n"); prefs.frameInfo.blockSizeID = max256KB; prefs.frameInfo.contentChecksumFlag = contentChecksumEnabled; cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &(prefs.frameInfo)), CNBuffer, testSize, &prefs); if (LZ4F_isError(cSize)) goto _output_error; - DISPLAY("Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); + DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); + + DISPLAYLEVEL(3, "Decompression test : \n"); + { + size_t decodedBufferSize = COMPRESSIBLE_NOISE_LENGTH; + unsigned maxBits = FUZ_highbit(decodedBufferSize); + BYTE* op = (BYTE*)decodedBuffer; + BYTE* const oend = (BYTE*)decodedBuffer + COMPRESSIBLE_NOISE_LENGTH; + BYTE* ip = (BYTE*)compressedBuffer; + BYTE* const iend = (BYTE*)compressedBuffer + cSize; + U64 crcDest; + + LZ4F_errorCode_t errorCode = LZ4F_createDecompressionContext(&dCtx, LZ4F_VERSION); + if (LZ4F_isError(errorCode)) goto _output_error; + + DISPLAYLEVEL(3, "random segment sizes : \n"); + while (ip < iend) + { + unsigned nbBits = FUZ_rand(&randState) % maxBits; + size_t iSize = (FUZ_rand(&randState) & ((1< (size_t)(iend-ip)) iSize = iend-ip; + //DISPLAY("%7i : + %6i\n", (int)(ip-(BYTE*)compressedBuffer), (int)iSize); + errorCode = LZ4F_decompress(dCtx, op, &oSize, ip, &iSize, NULL); + if (LZ4F_isError(errorCode)) goto _output_error; + op += oSize; + ip += iSize; + } + crcDest = XXH64(decodedBuffer, COMPRESSIBLE_NOISE_LENGTH, 1); + if (crcDest != crcOrig) goto _output_error; + DISPLAYLEVEL(3, "Regenerated %i bytes \n", (int)decodedBufferSize); + + errorCode = LZ4F_freeDecompressionContext(dCtx); + if (LZ4F_isError(errorCode)) goto _output_error; + } - DISPLAY("without checksum : \n"); + DISPLAYLEVEL(3, "without checksum : \n"); prefs.frameInfo.contentChecksumFlag = noContentChecksum; cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &(prefs.frameInfo)), CNBuffer, testSize, &prefs); if (LZ4F_isError(cSize)) goto _output_error; - DISPLAY("Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); + DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); - DISPLAY("Using 1 MB block : \n"); + DISPLAYLEVEL(3, "Using 1 MB block : \n"); prefs.frameInfo.blockSizeID = max1MB; prefs.frameInfo.contentChecksumFlag = contentChecksumEnabled; cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &(prefs.frameInfo)), CNBuffer, testSize, &prefs); if (LZ4F_isError(cSize)) goto _output_error; - DISPLAY("Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); + DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); - DISPLAY("without checksum : \n"); + DISPLAYLEVEL(3, "without checksum : \n"); prefs.frameInfo.contentChecksumFlag = noContentChecksum; cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &(prefs.frameInfo)), CNBuffer, testSize, &prefs); if (LZ4F_isError(cSize)) goto _output_error; - DISPLAY("Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); + DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); - DISPLAY("Using 4 MB block : \n"); + DISPLAYLEVEL(3, "Using 4 MB block : \n"); prefs.frameInfo.blockSizeID = max4MB; prefs.frameInfo.contentChecksumFlag = contentChecksumEnabled; cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &(prefs.frameInfo)), CNBuffer, testSize, &prefs); if (LZ4F_isError(cSize)) goto _output_error; - DISPLAY("Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); + DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); - DISPLAY("without checksum : \n"); + DISPLAYLEVEL(3, "without checksum : \n"); prefs.frameInfo.contentChecksumFlag = noContentChecksum; cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &(prefs.frameInfo)), CNBuffer, testSize, &prefs); if (LZ4F_isError(cSize)) goto _output_error; - DISPLAY("Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); - + DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); _end: free(CNBuffer); @@ -302,6 +354,65 @@ _output_error: } +static const U32 srcDataLength = 4 MB; + +int fuzzerTests(U32 seed, unsigned nbTests, int startCycle, double compressibility) +{ + unsigned testResult = 0; + unsigned testNb = 0; + void* srcBuffer; + void* compressedBuffer; + void* decodedBuffer; + U32 randState = seed; + LZ4F_preferences_t prefs = { 0 }; + LZ4F_decompressionContext_t dCtx; + + (void)nbTests; (void)startCycle; (void)prefs; + // Create compressible test buffer + LZ4F_createDecompressionContext(&dCtx, LZ4F_VERSION); + srcBuffer = malloc(srcDataLength); + compressedBuffer = malloc(LZ4F_compressFrameBound(srcDataLength, NULL)); + decodedBuffer = malloc(srcDataLength); + FUZ_fillCompressibleNoiseBuffer(srcBuffer, srcDataLength, compressibility, &randState); + + // Select components of compression test + for (testNb=0; testNb < nbTests; testNb++) + { + unsigned nbBits = (FUZ_rand(&randState) % (FUZ_highbit(srcDataLength-1) - 1)) + 1; + size_t srcSize = (FUZ_rand(&randState) & ((1< Date: Sat, 6 Sep 2014 09:47:28 +0100 Subject: More tests (variation of blocksize & checksum) fixed : checksum error on dealing with uncompressed blocks --- lz4frame.c | 1 + programs/frametest.c | 11 +++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lz4frame.c b/lz4frame.c index ec2af04..866c922 100644 --- a/lz4frame.c +++ b/lz4frame.c @@ -842,6 +842,7 @@ goto_decodeCBlockSize: if ((size_t)(srcEnd-srcPtr) < sizeToCopy) sizeToCopy = srcEnd-srcPtr; /* not enough input to read full block */ if ((size_t)(dstEnd-dstPtr) < sizeToCopy) sizeToCopy = dstEnd - dstPtr; memcpy(dstPtr, srcPtr, sizeToCopy); + if (dctxPtr->frameInfo.contentChecksumFlag) XXH32_update(&(dctxPtr->xxh), srcPtr, sizeToCopy); srcPtr += sizeToCopy; dstPtr += sizeToCopy; if (sizeToCopy == dctxPtr->sizeToDecode) /* all copied */ diff --git a/programs/frametest.c b/programs/frametest.c index 7d26bee..59666a3 100644 --- a/programs/frametest.c +++ b/programs/frametest.c @@ -103,7 +103,7 @@ static int g_time = 0; *****************************************/ static int no_prompt = 0; static char* programName; -static int displayLevel = 3; +static int displayLevel = 2; /********************************************************* @@ -340,6 +340,7 @@ int basicTests(U32 seed, int nbCycles, int startCycle, double compressibility) if (LZ4F_isError(cSize)) goto _output_error; DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); + DISPLAY("Basic tests completed \n"); _end: free(CNBuffer); free(compressedBuffer); @@ -364,10 +365,9 @@ int fuzzerTests(U32 seed, unsigned nbTests, int startCycle, double compressibili void* compressedBuffer; void* decodedBuffer; U32 randState = seed; - LZ4F_preferences_t prefs = { 0 }; LZ4F_decompressionContext_t dCtx; - (void)nbTests; (void)startCycle; (void)prefs; + (void)startCycle; // Create compressible test buffer LZ4F_createDecompressionContext(&dCtx, LZ4F_VERSION); srcBuffer = malloc(srcDataLength); @@ -378,10 +378,13 @@ int fuzzerTests(U32 seed, unsigned nbTests, int startCycle, double compressibili // Select components of compression test for (testNb=0; testNb < nbTests; testNb++) { + unsigned CCflag = FUZ_rand(&randState) & 1; + unsigned BSId = 4 + (FUZ_rand(&randState) & 3); + LZ4F_preferences_t prefs = { { BSId, 0, CCflag, 0,0,0 }, 0,0, 0,0,0,0 }; unsigned nbBits = (FUZ_rand(&randState) % (FUZ_highbit(srcDataLength-1) - 1)) + 1; size_t srcSize = (FUZ_rand(&randState) & ((1< Date: Sat, 6 Sep 2014 12:19:26 +0100 Subject: minor test code refactoring --- programs/frametest.c | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/programs/frametest.c b/programs/frametest.c index 59666a3..c9ffb5d 100644 --- a/programs/frametest.c +++ b/programs/frametest.c @@ -366,6 +366,7 @@ int fuzzerTests(U32 seed, unsigned nbTests, int startCycle, double compressibili void* decodedBuffer; U32 randState = seed; LZ4F_decompressionContext_t dCtx; + size_t result; (void)startCycle; // Create compressible test buffer @@ -384,20 +385,21 @@ int fuzzerTests(U32 seed, unsigned nbTests, int startCycle, double compressibili unsigned nbBits = (FUZ_rand(&randState) % (FUZ_highbit(srcDataLength-1) - 1)) + 1; size_t srcSize = (FUZ_rand(&randState) & ((1< Date: Sat, 6 Sep 2014 13:06:28 +0100 Subject: minor frametest display improvement --- programs/frametest.c | 45 +++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/programs/frametest.c b/programs/frametest.c index c9ffb5d..facff30 100644 --- a/programs/frametest.c +++ b/programs/frametest.c @@ -94,39 +94,44 @@ **************************************/ #define DISPLAY(...) fprintf(stderr, __VA_ARGS__) #define DISPLAYLEVEL(l, ...) if (displayLevel>=l) { DISPLAY(__VA_ARGS__); } -#define DISPLAYUPDATE(l, ...) if (displayLevel>=l) { if (FUZ_GetMilliStart() > g_time + 200) { g_time = FUZ_GetMilliStart(); DISPLAY(__VA_ARGS__); } } -static int g_time = 0; +#define DISPLAYUPDATE(l, ...) if (displayLevel>=l) { \ + if ((FUZ_GetMilliSpan(g_time) > refreshRate) || (displayLevel>=4)) \ + { g_time = FUZ_GetMilliStart(); DISPLAY(__VA_ARGS__); \ + if (displayLevel>=4) fflush(stdout); } } +static const U32 refreshRate = 150; +static U32 g_time = 0; /***************************************** Local Parameters *****************************************/ -static int no_prompt = 0; +static U32 no_prompt = 0; static char* programName; -static int displayLevel = 2; +static U32 displayLevel = 2; /********************************************************* Fuzzer functions *********************************************************/ -static int FUZ_GetMilliStart(void) +static U32 FUZ_GetMilliStart(void) { struct timeb tb; - int nCount; + U32 nCount; ftime( &tb ); - nCount = (int) (tb.millitm + (tb.time & 0xfffff) * 1000); + nCount = (U32) (((tb.time & 0xfffff) * 1000) + tb.millitm); return nCount; } -/* -static int FUZ_GetMilliSpan( int nTimeStart ) + +static U32 FUZ_GetMilliSpan(U32 nTimeStart) { - int nSpan = FUZ_GetMilliStart() - nTimeStart; - if ( nSpan < 0 ) + U32 nCurrent = FUZ_GetMilliStart(); + U32 nSpan = nCurrent - nTimeStart; + if (nTimeStart > nCurrent) nSpan += 0x100000 * 1000; return nSpan; } -*/ + # define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r))) @@ -366,7 +371,8 @@ int fuzzerTests(U32 seed, unsigned nbTests, int startCycle, double compressibili void* decodedBuffer; U32 randState = seed; LZ4F_decompressionContext_t dCtx; - size_t result; +# define CHECK(cond, ...) if (cond) { DISPLAY("Error => "); DISPLAY(__VA_ARGS__); \ + DISPLAY(" (seed %u, test nb %i) \n", seed, testNb); goto _output_error; } (void)startCycle; // Create compressible test buffer @@ -387,24 +393,25 @@ int fuzzerTests(U32 seed, unsigned nbTests, int startCycle, double compressibili size_t srcStart = FUZ_rand(&randState) % (srcDataLength - srcSize); size_t cSize, decodedSize; U64 crcOrig, crcDecoded; + size_t result; DISPLAYUPDATE(2, "%5i \r", testNb); result = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(srcSize, &(prefs.frameInfo)), srcBuffer+srcStart, srcSize, &prefs ); - if (LZ4F_isError(result)) goto _output_error; + CHECK(LZ4F_isError(result), "Compression failed"); crcOrig = XXH64(srcBuffer+srcStart, srcSize, 1); cSize = result; decodedSize = srcDataLength; result = LZ4F_decompress(dCtx, decodedBuffer, &decodedSize, compressedBuffer, &cSize, NULL); - if (LZ4F_isError(result)) goto _output_error; - if (decodedSize != srcSize) goto _output_error; - crcDecoded = XXH64(decodedBuffer, srcSize, 1); - if (crcDecoded != crcOrig) goto _output_error; + CHECK(result!=OK_FrameEnd, "Decompression failed"); + crcDecoded = XXH64(decodedBuffer, decodedSize, 1); + CHECK(crcDecoded != crcOrig, "Decompression corruption"); } DISPLAYLEVEL(2, "All tests completed \n"); _end: + LZ4F_freeDecompressionContext(dCtx); free(srcBuffer); free(compressedBuffer); free(decodedBuffer); @@ -412,8 +419,6 @@ _end: _output_error: testResult = 1; - DISPLAY("Error detected ! \n"); - if(!no_prompt) getchar(); goto _end; } -- cgit v0.12 From 04f5b17b7227bbc3ee788f869b55641f6c776b08 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sat, 6 Sep 2014 22:48:03 +0100 Subject: Improved frame decompression test Fixed : frame decompression bug --- lz4frame.c | 7 ++++--- programs/frametest.c | 33 ++++++++++++++++++++++++++++----- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/lz4frame.c b/lz4frame.c index 866c922..19b228c 100644 --- a/lz4frame.c +++ b/lz4frame.c @@ -866,7 +866,7 @@ goto_getCBlock: selectedIn = srcPtr; srcPtr += nextCBlockSize; dctxPtr->dStage = dstage_decodeCBlock; - break; + goto goto_decodeCBlock; /* break risks leaving the while loop */ } case dstage_storeCBlock: { @@ -881,6 +881,7 @@ goto_getCBlock: /* break; break unnecessary because it follows */ } case dstage_decodeCBlock: +goto_decodeCBlock: { int decodedSize; if ((size_t)(dstEnd-dstPtr) < dctxPtr->maxBlockSize) /* not enough room : decode into tmpOut */ @@ -937,14 +938,14 @@ goto_getSuffix: case dstage_storeSuffix: { size_t sizeToCopy = 4 - dctxPtr->tmpInSize; - if (sizeToCopy < (size_t)(srcEnd - srcPtr)) sizeToCopy = srcEnd - srcPtr; + if (sizeToCopy > (size_t)(srcEnd - srcPtr)) sizeToCopy = srcEnd - srcPtr; memcpy(dctxPtr->tmpIn + dctxPtr->tmpInSize, srcPtr, sizeToCopy); srcPtr += sizeToCopy; dctxPtr->tmpInSize += sizeToCopy; if (dctxPtr->tmpInSize < 4) break; /* not enough input to read suffix */ selectedIn = dctxPtr->tmpIn; dctxPtr->dStage = dstage_checkSuffix; - break; + /* break; useless, it follows; would need a goto anyway */ } case dstage_checkSuffix: goto_checkSuffix: diff --git a/programs/frametest.c b/programs/frametest.c index facff30..6462d14 100644 --- a/programs/frametest.c +++ b/programs/frametest.c @@ -362,7 +362,7 @@ _output_error: static const U32 srcDataLength = 4 MB; -int fuzzerTests(U32 seed, unsigned nbTests, int startCycle, double compressibility) +int fuzzerTests(U32 seed, unsigned nbTests, int startTest, double compressibility) { unsigned testResult = 0; unsigned testNb = 0; @@ -374,7 +374,7 @@ int fuzzerTests(U32 seed, unsigned nbTests, int startCycle, double compressibili # define CHECK(cond, ...) if (cond) { DISPLAY("Error => "); DISPLAY(__VA_ARGS__); \ DISPLAY(" (seed %u, test nb %i) \n", seed, testNb); goto _output_error; } - (void)startCycle; + (void)startTest; // Create compressible test buffer LZ4F_createDecompressionContext(&dCtx, LZ4F_VERSION); srcBuffer = malloc(srcDataLength); @@ -395,17 +395,40 @@ int fuzzerTests(U32 seed, unsigned nbTests, int startCycle, double compressibili U64 crcOrig, crcDecoded; size_t result; - DISPLAYUPDATE(2, "%5i \r", testNb); + DISPLAYUPDATE(2, "\r%5i ", testNb); result = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(srcSize, &(prefs.frameInfo)), srcBuffer+srcStart, srcSize, &prefs ); - CHECK(LZ4F_isError(result), "Compression failed"); + CHECK(LZ4F_isError(result), "Compression failed (error %i)", (int)result); crcOrig = XXH64(srcBuffer+srcStart, srcSize, 1); cSize = result; decodedSize = srcDataLength; result = LZ4F_decompress(dCtx, decodedBuffer, &decodedSize, compressedBuffer, &cSize, NULL); - CHECK(result!=OK_FrameEnd, "Decompression failed"); + CHECK(result!=OK_FrameEnd, "Decompression failed (error %i)", (int)result); crcDecoded = XXH64(decodedBuffer, decodedSize, 1); CHECK(crcDecoded != crcOrig, "Decompression corruption"); + + { + const BYTE* ip = compressedBuffer; + const BYTE* const iend = ip + cSize; + BYTE* op = decodedBuffer; + BYTE* const oend = op + srcDataLength; + unsigned segRand = randState ^ PRIME1; + unsigned maxBits = FUZ_highbit(decodedSize); + while (ip < iend) + { + unsigned nbBitsSeg = FUZ_rand(&segRand) % maxBits; + size_t iSize = (FUZ_rand(&segRand) & ((1< (size_t)(iend-ip)) iSize = iend-ip; + result = LZ4F_decompress(dCtx, op, &oSize, ip, &iSize, NULL); + CHECK(LZ4F_isError(result), "Decompression failed (error %i)", (int)result); + op += oSize; + ip += iSize; + } + CHECK(result != OK_FrameEnd, "Frame decompression failed (error %i)", (int)result); + crcDecoded = XXH64(decodedBuffer, decodedSize, 1); + CHECK(crcDecoded != crcOrig, "Decompression corruption"); + } } DISPLAYLEVEL(2, "All tests completed \n"); -- cgit v0.12 From 535120bbe3ebedb5ad5f53bb179242624e7c2828 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sun, 7 Sep 2014 09:13:02 +0100 Subject: More complex compression tests --- programs/frametest.c | 67 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 45 insertions(+), 22 deletions(-) diff --git a/programs/frametest.c b/programs/frametest.c index 6462d14..e582e4a 100644 --- a/programs/frametest.c +++ b/programs/frametest.c @@ -78,14 +78,11 @@ #define MB *(1U<<20) #define GB *(1U<<30) -#define NB_ATTEMPTS (64 KB) +static const U32 nbTestsDefault = 128 KB; #define COMPRESSIBLE_NOISE_LENGTH (2 MB) -#define FUZ_MAX_BLOCK_SIZE (128 KB) -#define FUZ_MAX_DICT_SIZE (32 KB) #define FUZ_COMPRESSIBILITY_DEFAULT 50 -#define PRIME1 2654435761U -#define PRIME2 2246822519U -#define PRIME3 3266489917U +static const U32 prime1 = 2654435761U; +static const U32 prime2 = 2246822519U; @@ -118,7 +115,7 @@ static U32 FUZ_GetMilliStart(void) struct timeb tb; U32 nCount; ftime( &tb ); - nCount = (U32) (((tb.time & 0xfffff) * 1000) + tb.millitm); + nCount = (U32) (((tb.time & 0xFFFFF) * 1000) + tb.millitm); return nCount; } @@ -138,8 +135,8 @@ static U32 FUZ_GetMilliSpan(U32 nTimeStart) unsigned int FUZ_rand(unsigned int* src) { U32 rand32 = *src; - rand32 *= PRIME1; - rand32 += PRIME2; + rand32 *= prime1; + rand32 += prime2; rand32 = FUZ_rotl32(rand32, 13); *src = rand32; return rand32 >> 5; @@ -391,29 +388,55 @@ int fuzzerTests(U32 seed, unsigned nbTests, int startTest, double compressibilit unsigned nbBits = (FUZ_rand(&randState) % (FUZ_highbit(srcDataLength-1) - 1)) + 1; size_t srcSize = (FUZ_rand(&randState) & ((1< (size_t)(iend-ip)) iSize = iend-ip; + result = LZ4F_compress(cctx, op, oSize, ip, iSize, NULL); + CHECK(LZ4F_isError(result), "Compression failed (error %i)", (int)result); + op += result; + ip += iSize; + } + result = LZ4F_compressEnd(cctx, op, oend-op, NULL); + CHECK(LZ4F_isError(result), "Compression completion failed (error %i)", (int)result); + op += result; + result = LZ4F_freeCompressionContext(cctx); + CHECK(LZ4F_isError(result), "deallocation failed (error %i)", (int)result); + } { const BYTE* ip = compressedBuffer; const BYTE* const iend = ip + cSize; BYTE* op = decodedBuffer; BYTE* const oend = op + srcDataLength; - unsigned segRand = randState ^ PRIME1; - unsigned maxBits = FUZ_highbit(decodedSize); + U32 segRand = randState ^ prime1; + unsigned maxBits = FUZ_highbit(cSize); while (ip < iend) { unsigned nbBitsSeg = FUZ_rand(&segRand) % maxBits; @@ -426,12 +449,12 @@ int fuzzerTests(U32 seed, unsigned nbTests, int startTest, double compressibilit ip += iSize; } CHECK(result != OK_FrameEnd, "Frame decompression failed (error %i)", (int)result); - crcDecoded = XXH64(decodedBuffer, decodedSize, 1); + crcDecoded = XXH64(decodedBuffer, op-(BYTE*)decodedBuffer, 1); CHECK(crcDecoded != crcOrig, "Decompression corruption"); } } - DISPLAYLEVEL(2, "All tests completed \n"); + DISPLAYLEVEL(2, "\rAll tests completed \n"); _end: LZ4F_freeDecompressionContext(dCtx); @@ -452,7 +475,7 @@ int FUZ_usage(void) DISPLAY( " %s [args]\n", programName); DISPLAY( "\n"); DISPLAY( "Arguments :\n"); - DISPLAY( " -i# : Nb of tests (default:%i) \n", NB_ATTEMPTS); + DISPLAY( " -i# : Nb of tests (default:%i) \n", nbTestsDefault); DISPLAY( " -s# : Select seed (default:prompt user)\n"); DISPLAY( " -t# : Select starting test number (default:0)\n"); DISPLAY( " -p# : Select compressibility in %% (default:%i%%)\n", FUZ_COMPRESSIBILITY_DEFAULT); @@ -467,7 +490,7 @@ int main(int argc, char** argv) U32 seed=0; int seedset=0; int argNb; - int nbTests = NB_ATTEMPTS; + int nbTests = nbTestsDefault; int testNb = 0; int proba = FUZ_COMPRESSIBILITY_DEFAULT; -- cgit v0.12 From cdececa38dbc45df93a71007781988af789108e8 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sun, 7 Sep 2014 11:04:29 +0100 Subject: Added : variable dstBuffer size decompression test --- programs/frametest.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/programs/frametest.c b/programs/frametest.c index e582e4a..acd2c1d 100644 --- a/programs/frametest.c +++ b/programs/frametest.c @@ -393,11 +393,7 @@ int fuzzerTests(U32 seed, unsigned nbTests, int startTest, double compressibilit size_t result; DISPLAYUPDATE(2, "\r%5i ", testNb); - - result = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(srcSize, &(prefs.frameInfo)), srcBuffer+srcStart, srcSize, &prefs ); - CHECK(LZ4F_isError(result), "Compression failed (error %i)", (int)result); crcOrig = XXH64(srcBuffer+srcStart, srcSize, 1); - cSize = result; { const BYTE* ip = srcBuffer + srcStart; @@ -428,6 +424,7 @@ int fuzzerTests(U32 seed, unsigned nbTests, int startTest, double compressibilit op += result; result = LZ4F_freeCompressionContext(cctx); CHECK(LZ4F_isError(result), "deallocation failed (error %i)", (int)result); + cSize = op-(BYTE*)compressedBuffer; } { @@ -439,10 +436,12 @@ int fuzzerTests(U32 seed, unsigned nbTests, int startTest, double compressibilit unsigned maxBits = FUZ_highbit(cSize); while (ip < iend) { - unsigned nbBitsSeg = FUZ_rand(&segRand) % maxBits; - size_t iSize = (FUZ_rand(&segRand) & ((1< (size_t)(iend-ip)) iSize = iend-ip; + if (oSize > (size_t)(oend-op)) oSize = oend-op; result = LZ4F_decompress(dCtx, op, &oSize, ip, &iSize, NULL); CHECK(LZ4F_isError(result), "Decompression failed (error %i)", (int)result); op += oSize; -- cgit v0.12 From cf28c27809df4ce4d6822d8fbb503922b4cfc9a8 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sun, 7 Sep 2014 12:12:46 +0100 Subject: Added : fuzzer : ability to jump to specified testNb --- programs/frametest.c | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/programs/frametest.c b/programs/frametest.c index acd2c1d..6807f4c 100644 --- a/programs/frametest.c +++ b/programs/frametest.c @@ -359,14 +359,14 @@ _output_error: static const U32 srcDataLength = 4 MB; -int fuzzerTests(U32 seed, unsigned nbTests, int startTest, double compressibility) +int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressibility) { unsigned testResult = 0; unsigned testNb = 0; void* srcBuffer; void* compressedBuffer; void* decodedBuffer; - U32 randState = seed; + U32 coreRand = seed; LZ4F_decompressionContext_t dCtx; # define CHECK(cond, ...) if (cond) { DISPLAY("Error => "); DISPLAY(__VA_ARGS__); \ DISPLAY(" (seed %u, test nb %i) \n", seed, testNb); goto _output_error; } @@ -377,11 +377,15 @@ int fuzzerTests(U32 seed, unsigned nbTests, int startTest, double compressibilit srcBuffer = malloc(srcDataLength); compressedBuffer = malloc(LZ4F_compressFrameBound(srcDataLength, NULL)); decodedBuffer = malloc(srcDataLength); - FUZ_fillCompressibleNoiseBuffer(srcBuffer, srcDataLength, compressibility, &randState); + FUZ_fillCompressibleNoiseBuffer(srcBuffer, srcDataLength, compressibility, &coreRand); + + // jump to requested testNb + for (testNb =0; testNb < startTest; testNb++) (void)FUZ_rand(&coreRand); // sync randomizer - // Select components of compression test - for (testNb=0; testNb < nbTests; testNb++) + // main fuzzer loop + for ( ; testNb < nbTests; testNb++) { + U32 randState = coreRand ^ prime1; unsigned CCflag = FUZ_rand(&randState) & 1; unsigned BSId = 4 + (FUZ_rand(&randState) & 3); LZ4F_preferences_t prefs = { { BSId, 0, CCflag, 0,0,0 }, 0,0, 0,0,0,0 }; @@ -400,7 +404,6 @@ int fuzzerTests(U32 seed, unsigned nbTests, int startTest, double compressibilit const BYTE* const iend = ip + srcSize; BYTE* op = compressedBuffer; BYTE* const oend = op + LZ4F_compressFrameBound(srcDataLength, NULL); - U32 segRand = randState ^ prime2; unsigned maxBits = FUZ_highbit(srcSize); LZ4F_compressionContext_t cctx; result = LZ4F_createCompressionContext(&cctx, LZ4F_VERSION, &prefs); @@ -410,8 +413,8 @@ int fuzzerTests(U32 seed, unsigned nbTests, int startTest, double compressibilit op += result; while (ip < iend) { - unsigned nbBitsSeg = FUZ_rand(&segRand) % maxBits; - size_t iSize = (FUZ_rand(&segRand) & ((1< (size_t)(iend-ip)) iSize = iend-ip; result = LZ4F_compress(cctx, op, oSize, ip, iSize, NULL); @@ -432,14 +435,13 @@ int fuzzerTests(U32 seed, unsigned nbTests, int startTest, double compressibilit const BYTE* const iend = ip + cSize; BYTE* op = decodedBuffer; BYTE* const oend = op + srcDataLength; - U32 segRand = randState ^ prime1; unsigned maxBits = FUZ_highbit(cSize); while (ip < iend) { - unsigned nbBitsI = FUZ_rand(&segRand) % maxBits; - unsigned nbBitsO = FUZ_rand(&segRand) % maxBits; - size_t iSize = (FUZ_rand(&segRand) & ((1< (size_t)(iend-ip)) iSize = iend-ip; if (oSize > (size_t)(oend-op)) oSize = oend-op; result = LZ4F_decompress(dCtx, op, &oSize, ip, &iSize, NULL); @@ -451,6 +453,8 @@ int fuzzerTests(U32 seed, unsigned nbTests, int startTest, double compressibilit crcDecoded = XXH64(decodedBuffer, op-(BYTE*)decodedBuffer, 1); CHECK(crcDecoded != crcOrig, "Decompression corruption"); } + + (void)FUZ_rand(&coreRand); // update rand seed } DISPLAYLEVEL(2, "\rAll tests completed \n"); -- cgit v0.12 From d1d1f8835de315ce401c8d1309ba648539878a64 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sun, 7 Sep 2014 12:57:09 +0100 Subject: API change : compressionContext more easily reusable --- lz4frame.c | 39 ++++++++++++++++++++++----------------- lz4frame.h | 14 +++++++------- programs/frametest.c | 32 +++++++++++++++++--------------- 3 files changed, 46 insertions(+), 39 deletions(-) diff --git a/lz4frame.c b/lz4frame.c index 19b228c..eaa8408 100644 --- a/lz4frame.c +++ b/lz4frame.c @@ -117,6 +117,7 @@ typedef struct { unsigned version; unsigned cStage; size_t maxBlockSize; + size_t maxBufferSize; XXH32_stateSpace_t xxh; BYTE* tmpIn; size_t tmpInSize; @@ -254,10 +255,10 @@ size_t LZ4F_compressFrame(void* dstBuffer, size_t dstMaxSize, const void* srcBuf if (dstMaxSize < LZ4F_compressFrameBound(srcSize, frameInfoPtr)) return -ERROR_dstMaxSize_tooSmall; - errorCode = LZ4F_createCompressionContext(&cctx, LZ4F_VERSION, preferencesPtr); + errorCode = LZ4F_createCompressionContext(&cctx, LZ4F_VERSION); if (LZ4F_isError(errorCode)) return errorCode; - errorCode = LZ4F_compressBegin(cctx, dstBuffer, dstMaxSize); /* write header */ + errorCode = LZ4F_compressBegin(cctx, dstBuffer, dstMaxSize, preferencesPtr); /* write header */ if (LZ4F_isError(errorCode)) return errorCode; dstPtr += errorCode; /* header size */ @@ -297,30 +298,19 @@ size_t LZ4F_compressFrame(void* dstBuffer, size_t dstMaxSize, const void* srcBuf * The first thing to do is to create a compressionContext object, which will be used in all compression operations. * This is achieved using LZ4F_createCompressionContext(), which takes as argument a version and an LZ4F_preferences_t structure. * The version provided MUST be LZ4F_VERSION. It is intended to track potential version differences between different binaries. - * The LZ4F_preferences_t structure is optional : you can provide NULL as argument, all preferences will then be set to default. - * The function will provide a pointer to a fully allocated LZ4F_compressionContext_t object. - * If the result LZ4F_errorCode_t is not zero, there was an error during context creation. + * The function will provide a pointer to an allocated LZ4F_compressionContext_t object. + * If the result LZ4F_errorCode_t is not OK_NoError, there was an error during context creation. * Object can release its memory using LZ4F_freeCompressionContext(); */ -LZ4F_errorCode_t LZ4F_createCompressionContext(LZ4F_compressionContext_t* LZ4F_compressionContextPtr, int version, const LZ4F_preferences_t* preferencesPtr) +LZ4F_errorCode_t LZ4F_createCompressionContext(LZ4F_compressionContext_t* LZ4F_compressionContextPtr, unsigned version) { - const LZ4F_preferences_t prefNull = { 0 }; LZ4F_cctx_internal_t* cctxPtr; - if (preferencesPtr == NULL) preferencesPtr = &prefNull; - cctxPtr = ALLOCATOR(sizeof(LZ4F_cctx_internal_t)); if (cctxPtr==NULL) return -ERROR_allocation_failed; - cctxPtr->prefs = *preferencesPtr; /* equivalent to memcpy() */ cctxPtr->version = version; cctxPtr->cStage = 0; /* Next stage : write header */ - if (cctxPtr->prefs.frameInfo.blockSizeID == 0) cctxPtr->prefs.frameInfo.blockSizeID = LZ4F_BLOCKSIZEID_DEFAULT; - cctxPtr->maxBlockSize = LZ4F_getBlockSize(cctxPtr->prefs.frameInfo.blockSizeID); - cctxPtr->tmpIn = ALLOCATOR(cctxPtr->maxBlockSize); - if (cctxPtr->tmpIn == NULL) return -ERROR_allocation_failed; - cctxPtr->tmpInSize = 0; - XXH32_resetState(&(cctxPtr->xxh), 0); *LZ4F_compressionContextPtr = (LZ4F_compressionContext_t)cctxPtr; @@ -345,8 +335,9 @@ LZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_compressionContext_t LZ4F_comp * The result of the function is the number of bytes written into dstBuffer for the header * or an error code (can be tested using LZ4F_isError()) */ -size_t LZ4F_compressBegin(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize) +size_t LZ4F_compressBegin(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const LZ4F_preferences_t* preferencesPtr) { + LZ4F_preferences_t prefNull = { 0 }; LZ4F_cctx_internal_t* cctxPtr = (LZ4F_cctx_internal_t*)compressionContext; BYTE* const dstStart = (BYTE*)dstBuffer; BYTE* dstPtr = dstStart; @@ -354,6 +345,20 @@ size_t LZ4F_compressBegin(LZ4F_compressionContext_t compressionContext, void* ds if (dstMaxSize < LZ4F_MAXHEADERFRAME_SIZE) return -ERROR_dstMaxSize_tooSmall; if (cctxPtr->cStage != 0) return -ERROR_GENERIC; + if (preferencesPtr == NULL) preferencesPtr = &prefNull; + + /* Buffer Management */ + cctxPtr->prefs = *preferencesPtr; + if (cctxPtr->prefs.frameInfo.blockSizeID == 0) cctxPtr->prefs.frameInfo.blockSizeID = LZ4F_BLOCKSIZEID_DEFAULT; + cctxPtr->maxBlockSize = LZ4F_getBlockSize(cctxPtr->prefs.frameInfo.blockSizeID); + if (cctxPtr->maxBufferSize < cctxPtr->maxBlockSize) + { + cctxPtr->maxBufferSize = cctxPtr->maxBlockSize; + cctxPtr->tmpIn = ALLOCATOR(cctxPtr->maxBlockSize); + if (cctxPtr->tmpIn == NULL) return -ERROR_allocation_failed; + } + cctxPtr->tmpInSize = 0; + XXH32_resetState(&(cctxPtr->xxh), 0); /* Magic Number */ LZ4F_writeLE32(dstPtr, LZ4F_MAGICNUMBER); diff --git a/lz4frame.h b/lz4frame.h index b4a067e..179271a 100644 --- a/lz4frame.h +++ b/lz4frame.h @@ -124,13 +124,12 @@ typedef struct { /* Resource Management */ #define LZ4F_VERSION 100 -LZ4F_errorCode_t LZ4F_createCompressionContext(LZ4F_compressionContext_t* LZ4F_compressionContextPtr, int version, const LZ4F_preferences_t* preferencesPtr); +LZ4F_errorCode_t LZ4F_createCompressionContext(LZ4F_compressionContext_t* LZ4F_compressionContextPtr, unsigned version); LZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_compressionContext_t LZ4F_compressionContext); /* LZ4F_createCompressionContext() : * The first thing to do is to create a compressionContext object, which will be used in all compression operations. * This is achieved using LZ4F_createCompressionContext(), which takes as argument a version and an LZ4F_preferences_t structure. * The version provided MUST be LZ4F_VERSION. It is intended to track potential version differences between different binaries. - * The LZ4F_preferences_t structure is optional : you can provide NULL as argument, all preferences will then be set to default. * The function will provide a pointer to a fully allocated LZ4F_compressionContext_t object. * If the result LZ4F_errorCode_t is not zero, there was an error during context creation. * Object can release its memory using LZ4F_freeCompressionContext(); @@ -139,10 +138,11 @@ LZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_compressionContext_t LZ4F_comp /* Compression */ -size_t LZ4F_compressBegin(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize); +size_t LZ4F_compressBegin(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const LZ4F_preferences_t* preferencesPtr); /* LZ4F_compressBegin() : * will write the frame header into dstBuffer. - * dstBuffer must be large enough to accomodate a header (dstMaxSize). Maximum header size is 19 bytes. + * dstBuffer must be large enough to accommodate a header (dstMaxSize). Maximum header size is 19 bytes. + * The LZ4F_preferences_t structure is optional : you can provide NULL as argument, all preferences will then be set to default. * The result of the function is the number of bytes written into dstBuffer for the header * or an error code (can be tested using LZ4F_isError()) */ @@ -163,7 +163,7 @@ size_t LZ4F_compress(LZ4F_compressionContext_t compressionContext, void* dstBuff * Conversely, given a fixed dstMaxSize value, you can know the maximum srcSize authorized using LZ4F_getMaxSrcSize() * If this condition is not respected, LZ4F_compress() will fail (result is an errorCode) * The LZ4F_compressOptions_t structure is optional : you can provide NULL as argument. - * The result of the function is the number of bytes written into dstBuffer (it can be zero, meaning input data is just stored within compressionContext for a future block to complete) + * The result of the function is the number of bytes written into dstBuffer : it can be zero, meaning input data was just stored within compressionContext * The function outputs an error code if it fails (can be tested using LZ4F_isError()) */ @@ -171,10 +171,10 @@ size_t LZ4F_flush(LZ4F_compressionContext_t compressionContext, void* dstBuffer, /* LZ4F_flush() * Should you need to create compressed data immediately, without waiting for a block to be filled, * you can call LZ4_flush(), which will immediately compress any remaining data stored within compressionContext. + * The LZ4F_compressOptions_t structure is optional : you can provide NULL as argument. * The result of the function is the number of bytes written into dstBuffer * (it can be zero, this means there was no data left within compressionContext) * The function outputs an error code if it fails (can be tested using LZ4F_isError()) - * The LZ4F_compressOptions_t structure is optional : you can provide NULL as argument. */ size_t LZ4F_compressEnd(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const LZ4F_compressOptions_t* compressOptionsPtr); @@ -202,7 +202,7 @@ typedef struct { /* Resource management */ -LZ4F_errorCode_t LZ4F_createDecompressionContext(LZ4F_compressionContext_t* LZ4F_decompressionContextPtr, unsigned versionNumber); +LZ4F_errorCode_t LZ4F_createDecompressionContext(LZ4F_compressionContext_t* LZ4F_decompressionContextPtr, unsigned version); LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_compressionContext_t LZ4F_decompressionContext); /* LZ4F_createDecompressionContext() : * The first thing to do is to create a decompressionContext object, which will be used in all decompression operations. diff --git a/programs/frametest.c b/programs/frametest.c index 6807f4c..c33e1ac 100644 --- a/programs/frametest.c +++ b/programs/frametest.c @@ -363,20 +363,27 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi { unsigned testResult = 0; unsigned testNb = 0; - void* srcBuffer; - void* compressedBuffer; - void* decodedBuffer; + void* srcBuffer = NULL; + void* compressedBuffer = NULL; + void* decodedBuffer = NULL; U32 coreRand = seed; LZ4F_decompressionContext_t dCtx; + LZ4F_compressionContext_t cCtx; + size_t result; # define CHECK(cond, ...) if (cond) { DISPLAY("Error => "); DISPLAY(__VA_ARGS__); \ DISPLAY(" (seed %u, test nb %i) \n", seed, testNb); goto _output_error; } - (void)startTest; - // Create compressible test buffer - LZ4F_createDecompressionContext(&dCtx, LZ4F_VERSION); + // Create buffers + result = LZ4F_createDecompressionContext(&dCtx, LZ4F_VERSION); + CHECK(LZ4F_isError(result), "Allocation failed (error %i)", (int)result); + result = LZ4F_createCompressionContext(&cCtx, LZ4F_VERSION); + CHECK(LZ4F_isError(result), "Allocation failed (error %i)", (int)result); srcBuffer = malloc(srcDataLength); + CHECK(srcBuffer==NULL, "srcBuffer Allocation failed"); compressedBuffer = malloc(LZ4F_compressFrameBound(srcDataLength, NULL)); + CHECK(compressedBuffer==NULL, "compressedBuffer Allocation failed"); decodedBuffer = malloc(srcDataLength); + CHECK(decodedBuffer==NULL, "decodedBuffer Allocation failed"); FUZ_fillCompressibleNoiseBuffer(srcBuffer, srcDataLength, compressibility, &coreRand); // jump to requested testNb @@ -394,7 +401,6 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi size_t srcStart = FUZ_rand(&randState) % (srcDataLength - srcSize); size_t cSize; U64 crcOrig, crcDecoded; - size_t result; DISPLAYUPDATE(2, "\r%5i ", testNb); crcOrig = XXH64(srcBuffer+srcStart, srcSize, 1); @@ -405,10 +411,7 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi BYTE* op = compressedBuffer; BYTE* const oend = op + LZ4F_compressFrameBound(srcDataLength, NULL); unsigned maxBits = FUZ_highbit(srcSize); - LZ4F_compressionContext_t cctx; - result = LZ4F_createCompressionContext(&cctx, LZ4F_VERSION, &prefs); - CHECK(LZ4F_isError(result), "Allocation for compression failed (error %i)", (int)result); - result = LZ4F_compressBegin(cctx, op, oend-op); + result = LZ4F_compressBegin(cCtx, op, oend-op, &prefs); CHECK(LZ4F_isError(result), "Compression header failed (error %i)", (int)result); op += result; while (ip < iend) @@ -417,16 +420,14 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi size_t iSize = (FUZ_rand(&randState) & ((1< (size_t)(iend-ip)) iSize = iend-ip; - result = LZ4F_compress(cctx, op, oSize, ip, iSize, NULL); + result = LZ4F_compress(cCtx, op, oSize, ip, iSize, NULL); CHECK(LZ4F_isError(result), "Compression failed (error %i)", (int)result); op += result; ip += iSize; } - result = LZ4F_compressEnd(cctx, op, oend-op, NULL); + result = LZ4F_compressEnd(cCtx, op, oend-op, NULL); CHECK(LZ4F_isError(result), "Compression completion failed (error %i)", (int)result); op += result; - result = LZ4F_freeCompressionContext(cctx); - CHECK(LZ4F_isError(result), "deallocation failed (error %i)", (int)result); cSize = op-(BYTE*)compressedBuffer; } @@ -461,6 +462,7 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi _end: LZ4F_freeDecompressionContext(dCtx); + LZ4F_freeCompressionContext(cCtx); free(srcBuffer); free(compressedBuffer); free(decodedBuffer); -- cgit v0.12 From d1f479fe3af5ff35431f1c3a0dc1fcbcd7071839 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sun, 7 Sep 2014 13:29:04 +0100 Subject: minor allocation correction (compressBegin) --- lz4frame.c | 1 + 1 file changed, 1 insertion(+) diff --git a/lz4frame.c b/lz4frame.c index eaa8408..91d278b 100644 --- a/lz4frame.c +++ b/lz4frame.c @@ -354,6 +354,7 @@ size_t LZ4F_compressBegin(LZ4F_compressionContext_t compressionContext, void* ds if (cctxPtr->maxBufferSize < cctxPtr->maxBlockSize) { cctxPtr->maxBufferSize = cctxPtr->maxBlockSize; + FREEMEM(cctxPtr->tmpIn); cctxPtr->tmpIn = ALLOCATOR(cctxPtr->maxBlockSize); if (cctxPtr->tmpIn == NULL) return -ERROR_allocation_failed; } -- cgit v0.12 From 6b407c65a443e88c6cdd63dd738fbf94b9d6a886 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sun, 7 Sep 2014 14:15:33 +0100 Subject: Added : valgrind verification for frametest --- programs/Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/programs/Makefile b/programs/Makefile index 82a5e66..b28c42c 100644 --- a/programs/Makefile +++ b/programs/Makefile @@ -172,7 +172,7 @@ test-fuzzer32: fuzzer32 test-frame: frametest ./frametest -test-mem: lz4 datagen +test-mem: lz4 datagen frametest ./datagen -g16KB > tmp valgrind ./lz4 -9 -BD -f tmp /dev/null ./datagen -g16MB > tmp @@ -180,6 +180,7 @@ test-mem: lz4 datagen ./datagen -g256MB > tmp valgrind ./lz4 -B4D -f tmp /dev/null rm tmp + valgrind ./frametest -i100 test-mem32: lz4c32 datagen # unfortunately, valgrind doesn't seem to work with non-native binary. If someone knows how to do a valgrind-test on a 32-bits exe with a 64-bits system... -- cgit v0.12 From eac83cd850d2f69a82ef9af344be9b7f3925681a Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 9 Sep 2014 23:54:22 +0100 Subject: Added : streaming mode --- lz4.c | 2 +- lz4.h | 12 ++-- lz4frame.c | 156 +++++++++++++++++++++++++++++++++++++++------------ programs/frametest.c | 8 ++- 4 files changed, 132 insertions(+), 46 deletions(-) diff --git a/lz4.c b/lz4.c index 435f1ea..ecb8a79 100644 --- a/lz4.c +++ b/lz4.c @@ -860,7 +860,7 @@ int LZ4_saveDict (LZ4_stream_t* LZ4_dict, char* safeBuffer, int dictSize) dict->dictionary = (const BYTE*)safeBuffer; dict->dictSize = (U32)dictSize; - return 1; + return dictSize; } diff --git a/lz4.h b/lz4.h index 3400f50..9e192e6 100644 --- a/lz4.h +++ b/lz4.h @@ -185,7 +185,7 @@ typedef struct { unsigned int table[LZ4_STREAMSIZE_U32]; } LZ4_stream_t; * LZ4_resetStream * Use this function to init an allocated LZ4_stream_t structure */ -void LZ4_resetStream (LZ4_stream_t* LZ4_stream); +void LZ4_resetStream (LZ4_stream_t* LZ4_streamPtr); /* * If you prefer dynamic allocation methods, @@ -221,10 +221,10 @@ int LZ4_compress_limitedOutput_continue (LZ4_stream_t* LZ4_stream, const char* s /* * LZ4_saveDict * If previously compressed data block is not guaranteed to remain available at its memory location - * save it into a safe place (char* safeBuffer) + * save it into a safer place (char* safeBuffer) * Note : you don't need to call LZ4_loadDict() afterwards, * dictionary is immediately usable, you can therefore call again LZ4_compress_continue() - * Return : 1 if OK, 0 if error + * Return : dictionary size in bytes, or 0 if error * Note : any dictSize > 64 KB will be interpreted as 64KB. */ int LZ4_saveDict (LZ4_stream_t* LZ4_stream, char* safeBuffer, int dictSize); @@ -266,9 +266,9 @@ int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream); These decoding functions allow decompression of multiple blocks in "streaming" mode. Previously decoded blocks must still be available at the memory position where they were decoded. If it's not possible, save the relevant part of decoded data into a safe buffer, - and indicate where its new address using LZ4_setDictDecode() + and indicate where its new address using LZ4_setStreamDecode() */ -int LZ4_decompress_safe_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* source, char* dest, int compressedSize, int maxOutputSize); +int LZ4_decompress_safe_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* source, char* dest, int compressedSize, int maxDecompressedSize); int LZ4_decompress_fast_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* source, char* dest, int originalSize); @@ -279,7 +279,7 @@ Advanced decoding functions : a combination of LZ4_setDictDecode() followed by LZ4_decompress_x_continue() They don't use nor update an LZ4_streamDecode_t structure. */ -int LZ4_decompress_safe_usingDict (const char* source, char* dest, int compressedSize, int maxOutputSize, const char* dictStart, int dictSize); +int LZ4_decompress_safe_usingDict (const char* source, char* dest, int compressedSize, int maxDecompressedSize, const char* dictStart, int dictSize); int LZ4_decompress_fast_usingDict (const char* source, char* dest, int originalSize, const char* dictStart, int dictSize); diff --git a/lz4frame.c b/lz4frame.c index 91d278b..4871d5b 100644 --- a/lz4frame.c +++ b/lz4frame.c @@ -118,9 +118,13 @@ typedef struct { unsigned cStage; size_t maxBlockSize; size_t maxBufferSize; - XXH32_stateSpace_t xxh; - BYTE* tmpIn; + BYTE* tmpBuff; + BYTE* tmpDict; + size_t tmpDictSize; + BYTE* tmpIn; size_t tmpInSize; + XXH32_stateSpace_t xxh; + LZ4_stream_t lz4ctx; } LZ4F_cctx_internal_t; typedef struct { @@ -128,16 +132,21 @@ typedef struct { unsigned version; unsigned dStage; size_t maxBlockSize; - XXH32_stateSpace_t xxh; + size_t maxBufferSize; size_t sizeToDecode; const BYTE* srcExpect; BYTE* tmpIn; size_t tmpInSize; size_t tmpInTarget; + BYTE* tmpOutBuffer; + BYTE* dict; + size_t dictSize; BYTE* tmpOut; size_t tmpOutSize; size_t tmpOutStart; - BYTE header[7]; + XXH32_stateSpace_t xxh; + LZ4_streamDecode_t lz4ctx; + BYTE header[8]; } LZ4F_dctx_internal_t; @@ -322,7 +331,7 @@ LZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_compressionContext_t LZ4F_comp { LZ4F_cctx_internal_t* cctxPtr = (LZ4F_cctx_internal_t*)LZ4F_compressionContext; - FREEMEM(cctxPtr->tmpIn); + FREEMEM(cctxPtr->tmpBuff); FREEMEM(LZ4F_compressionContext); return OK_NoError; @@ -351,15 +360,21 @@ size_t LZ4F_compressBegin(LZ4F_compressionContext_t compressionContext, void* ds cctxPtr->prefs = *preferencesPtr; if (cctxPtr->prefs.frameInfo.blockSizeID == 0) cctxPtr->prefs.frameInfo.blockSizeID = LZ4F_BLOCKSIZEID_DEFAULT; cctxPtr->maxBlockSize = LZ4F_getBlockSize(cctxPtr->prefs.frameInfo.blockSizeID); - if (cctxPtr->maxBufferSize < cctxPtr->maxBlockSize) + if (cctxPtr->maxBufferSize < cctxPtr->maxBlockSize + (cctxPtr->prefs.frameInfo.blockMode == blockLinked)) { cctxPtr->maxBufferSize = cctxPtr->maxBlockSize; - FREEMEM(cctxPtr->tmpIn); - cctxPtr->tmpIn = ALLOCATOR(cctxPtr->maxBlockSize); - if (cctxPtr->tmpIn == NULL) return -ERROR_allocation_failed; + if (cctxPtr->prefs.frameInfo.blockMode == blockLinked) cctxPtr->maxBufferSize += 64 KB; + FREEMEM(cctxPtr->tmpBuff); + cctxPtr->tmpBuff = ALLOCATOR(cctxPtr->maxBufferSize); + if (cctxPtr->tmpBuff == NULL) return -ERROR_allocation_failed; + cctxPtr->tmpDict = cctxPtr->tmpBuff; + cctxPtr->tmpIn = cctxPtr->tmpBuff; + if (cctxPtr->prefs.frameInfo.blockMode == blockLinked) cctxPtr->tmpIn += 64 KB; } + cctxPtr->tmpDictSize = 0; cctxPtr->tmpInSize = 0; XXH32_resetState(&(cctxPtr->xxh), 0); + LZ4_resetStream(&(cctxPtr->lz4ctx)); /* Magic Number */ LZ4F_writeLE32(dstPtr, LZ4F_MAGICNUMBER); @@ -367,18 +382,16 @@ size_t LZ4F_compressBegin(LZ4F_compressionContext_t compressionContext, void* ds headerStart = dstPtr; /* FLG Byte */ - *dstPtr = (1 & _2BITS) << 6; /* Version('01') */ - *dstPtr |= (1 & _1BIT ) << 5; /* Blocks independents */ - *dstPtr |= (char)((cctxPtr->prefs.frameInfo.contentChecksumFlag & _1BIT ) << 2); /* Stream checksum */ - dstPtr++; + //cctxPtr->prefs.frameInfo.blockMode = 1; // <============ debug + *dstPtr++ = ((1 & _2BITS) << 6) /* Version('01') */ + + ((cctxPtr->prefs.frameInfo.blockMode & _1BIT ) << 5) /* Block mode */ + + (char)((cctxPtr->prefs.frameInfo.contentChecksumFlag & _1BIT ) << 2); /* Stream checksum */ /* BD Byte */ - *dstPtr = (char)((cctxPtr->prefs.frameInfo.blockSizeID & _3BITS) << 4); - dstPtr++; + *dstPtr++ = (char)((cctxPtr->prefs.frameInfo.blockSizeID & _3BITS) << 4); /* CRC Byte */ - *dstPtr = LZ4F_headerChecksum(headerStart, 2); - dstPtr++; + *dstPtr++ = LZ4F_headerChecksum(headerStart, 2); - cctxPtr->cStage = 1; /* header written */ + cctxPtr->cStage = 1; /* header written, wait for data block */ return (dstPtr - dstStart); } @@ -442,34 +455,44 @@ size_t LZ4F_compress(LZ4F_compressionContext_t compressionContext, void* dstBuff const BYTE* const srcEnd = srcPtr + srcSize; BYTE* const dstStart = (BYTE*)dstBuffer; BYTE* dstPtr = dstStart; + U32 lastBlockCompressed = 0; + int (*compress)(void*, const char*, char*, int, int); + if (cctxPtr->cStage != 1) return -ERROR_GENERIC; if (dstMaxSize < LZ4F_compressBound(srcSize, &(cctxPtr->prefs.frameInfo))) return -ERROR_dstMaxSize_tooSmall; if (compressOptionsPtr == NULL) compressOptionsPtr = &cOptionsNull; + /* select compression function */ + compress = (cctxPtr->prefs.frameInfo.blockMode == blockLinked) ? + (int(*)(void*,const char*,char*,int,int))LZ4_compress_limitedOutput_continue : + LZ4_compress_limitedOutput_withState; + /* complete tmp buffer */ - if (cctxPtr->tmpInSize > 0) + if (cctxPtr->tmpInSize > 0) /* some data already within tmp buffer */ { size_t sizeToCopy = blockSize - cctxPtr->tmpInSize; if (sizeToCopy > srcSize) { - /* add to tmp buffer */ + /* add src to tmpIn buffer */ memcpy(cctxPtr->tmpIn + cctxPtr->tmpInSize, srcBuffer, srcSize); srcPtr = srcEnd; cctxPtr->tmpInSize += srcSize; } else { + /* complete tmpIn block and then compress it */ BYTE* cSizePtr = dstPtr; U32 cSize; + lastBlockCompressed = 2; memcpy(cctxPtr->tmpIn + cctxPtr->tmpInSize, srcBuffer, sizeToCopy); srcPtr += sizeToCopy; - dstPtr += 4; /* space for cSizePtr */ - cSize = (U32)LZ4_compress_limitedOutput((const char*)cctxPtr->tmpIn, (char*)dstPtr, (int)(blockSize), (int)(blockSize-1)); + dstPtr += 4; /* space for cSize */ + cSize = (U32)compress(&(cctxPtr->lz4ctx), (const char*)cctxPtr->tmpIn, (char*)dstPtr, (int)(blockSize), (int)(blockSize-1)); dstPtr += cSize; LZ4F_writeLE32(cSizePtr, cSize); - if (cSize == 0) /* compression failed */ + if (cSize == 0) /* compression failed : non compressible assumed */ { cSize = blockSize + LZ4F_BLOCKUNCOMPRESSED_FLAG; LZ4F_writeLE32(cSizePtr, cSize); @@ -485,8 +508,9 @@ size_t LZ4F_compress(LZ4F_compressionContext_t compressionContext, void* dstBuff /* compress one block */ BYTE* cSizePtr = dstPtr; U32 cSize; + lastBlockCompressed = 1; dstPtr += 4; /* space for cSizePtr */ - cSize = (U32)LZ4_compress_limitedOutput((const char*)srcPtr, (char*)dstPtr, (int)(blockSize), (int)(blockSize-1)); + cSize = (U32)compress(&(cctxPtr->lz4ctx), (const char*)srcPtr, (char*)dstPtr, (int)(blockSize), (int)(blockSize-1)); dstPtr += cSize; LZ4F_writeLE32(cSizePtr, cSize); if (cSize == 0) /* compression failed */ @@ -499,7 +523,16 @@ size_t LZ4F_compress(LZ4F_compressionContext_t compressionContext, void* dstBuff srcPtr += blockSize; } - if (srcPtr < srcEnd) + if ((cctxPtr->prefs.frameInfo.blockMode == blockLinked) && (lastBlockCompressed)) + { + /* last 64 KB of input become dictionary */ + /* assumption : previous block size was at least 64 KB */ + int result = LZ4_saveDict (&(cctxPtr->lz4ctx), (char*)(cctxPtr->tmpDict), 64 KB); + if (!result) return ERROR_GENERIC; + cctxPtr->tmpIn = cctxPtr->tmpDict + result; + } + + if (srcPtr < srcEnd) /* some input data left */ { /* fill tmp buffer */ size_t sizeToCopy = srcEnd - srcPtr; @@ -528,6 +561,7 @@ size_t LZ4F_flush(LZ4F_compressionContext_t compressionContext, void* dstBuffer, LZ4F_cctx_internal_t* cctxPtr = (LZ4F_cctx_internal_t*)compressionContext; BYTE* const dstStart = (BYTE*)dstBuffer; BYTE* dstPtr = dstStart; + int (*compress)(void*, const char*, char*, int, int); if (cctxPtr->tmpInSize == 0) return 0; /* nothing to flush */ @@ -535,11 +569,16 @@ size_t LZ4F_flush(LZ4F_compressionContext_t compressionContext, void* dstBuffer, if (dstMaxSize < LZ4F_compressBound(1, &(cctxPtr->prefs.frameInfo))) return -ERROR_dstMaxSize_tooSmall; if (compressOptionsPtr == NULL) compressOptionsPtr = &cOptionsNull; + /* select compression function */ + compress = (cctxPtr->prefs.frameInfo.blockMode == blockLinked) ? + (int(*)(void*,const char*,char*,int,int))LZ4_compress_limitedOutput_continue : + LZ4_compress_limitedOutput_withState; + { BYTE* cSizePtr = dstPtr; U32 cSize; dstPtr += 4; /* space for cSizePtr */ - cSize = (U32)LZ4_compress_limitedOutput((const char*)cctxPtr->tmpIn, (char*)dstPtr, (int)(cctxPtr->tmpInSize), (int)(cctxPtr->tmpInSize-1)); + cSize = (U32)compress(&(cctxPtr->lz4ctx), (const char*)cctxPtr->tmpIn, (char*)dstPtr, (int)(cctxPtr->tmpInSize), (int)(cctxPtr->tmpInSize-1)); dstPtr += cSize; LZ4F_writeLE32(cSizePtr, cSize); if (cSize == 0) /* compression failed */ @@ -552,6 +591,14 @@ size_t LZ4F_flush(LZ4F_compressionContext_t compressionContext, void* dstBuffer, cctxPtr->tmpInSize = 0; } + if (cctxPtr->prefs.frameInfo.blockMode == blockLinked) + { + /* last 64 KB of input become dictionary */ + int result = LZ4_saveDict (&(cctxPtr->lz4ctx), (char*)(cctxPtr->tmpDict), 64 KB); + if (!result) return ERROR_GENERIC; + cctxPtr->tmpIn = cctxPtr->tmpDict + result; + } + return dstPtr - dstStart; } @@ -620,7 +667,7 @@ LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_compressionContext_t LZ4F_de { LZ4F_dctx_internal_t* dctxPtr = (LZ4F_dctx_internal_t*)LZ4F_decompressionContext; FREEMEM(dctxPtr->tmpIn); - FREEMEM(dctxPtr->tmpOut); + FREEMEM(dctxPtr->tmpOutBuffer); FREEMEM(dctxPtr); return OK_NoError; } @@ -657,7 +704,7 @@ static size_t LZ4F_decodeHeader(LZ4F_dctx_internal_t* dctxPtr, const BYTE* srcPt /* validate */ if (version != 1) return -ERROR_GENERIC; /* Version Number, only supported value */ - if (blockMode != blockIndependent) return -ERROR_GENERIC; /* Only supported blockMode for the time being */ + //if (blockMode != blockIndependent) return -ERROR_GENERIC; /* Only supported blockMode for the time being */ if (blockChecksumFlag != 0) return -ERROR_GENERIC; /* Only supported value for the time being */ if (contentSizeFlag != 0) return -ERROR_GENERIC; /* Only supported value for the time being */ if (((FLG>>1)&_1BIT) != 0) return -ERROR_GENERIC; /* Reserved bit */ @@ -673,16 +720,23 @@ static size_t LZ4F_decodeHeader(LZ4F_dctx_internal_t* dctxPtr, const BYTE* srcPt /* init */ if (contentChecksumFlag) XXH32_resetState(&(dctxPtr->xxh), 0); + if (blockMode==blockLinked) LZ4_setStreamDecode(&(dctxPtr->lz4ctx), NULL, 0); + dctxPtr->dictSize = 0; - if (LZ4F_getBlockSize(blockSizeID) > dctxPtr->maxBlockSize) /* tmp buffers too small */ + if (LZ4F_getBlockSize(blockSizeID) > dctxPtr->maxBufferSize) /* tmp buffers too small */ { FREEMEM(dctxPtr->tmpIn); - FREEMEM(dctxPtr->tmpOut); - dctxPtr->tmpIn = ALLOCATOR(LZ4F_getBlockSize(blockSizeID)); - if (dctxPtr->tmpIn == NULL) return -ERROR_GENERIC; - dctxPtr->tmpOut= ALLOCATOR(LZ4F_getBlockSize(blockSizeID)); - if (dctxPtr->tmpOut== NULL) return -ERROR_GENERIC; + FREEMEM(dctxPtr->tmpOutBuffer); dctxPtr->maxBlockSize = LZ4F_getBlockSize(blockSizeID); + dctxPtr->maxBufferSize = dctxPtr->maxBlockSize; + if (dctxPtr->frameInfo.blockMode==blockLinked) dctxPtr->maxBufferSize += 64 KB; + dctxPtr->tmpIn = ALLOCATOR(dctxPtr->maxBlockSize); + if (dctxPtr->tmpIn == NULL) return -ERROR_GENERIC; + dctxPtr->tmpOutBuffer= ALLOCATOR(dctxPtr->maxBufferSize); + if (dctxPtr->tmpOutBuffer== NULL) return -ERROR_GENERIC; + dctxPtr->tmpOut = dctxPtr->tmpOutBuffer; + if (dctxPtr->frameInfo.blockMode==blockLinked) dctxPtr->tmpOut += 64 KB; + dctxPtr->dict = dctxPtr->tmpOut - dctxPtr->dictSize; } return 7; @@ -724,6 +778,27 @@ LZ4F_errorCode_t LZ4F_getFrameInfo(LZ4F_decompressionContext_t decompressionCont } +static void LZ4F_saveDict(LZ4F_dctx_internal_t* dctxPtr, BYTE* decoded, size_t decodedSize) +{ + size_t newDictSize = decodedSize; + size_t preserveDictSize; + if (newDictSize > 64 KB) newDictSize = 64 KB; + preserveDictSize = 64 KB - newDictSize; + memmove(dctxPtr->tmpOutBuffer, dctxPtr->tmpOutBuffer + newDictSize, preserveDictSize); + memcpy(dctxPtr->tmpOutBuffer + preserveDictSize, decoded + decodedSize - newDictSize, newDictSize); + dctxPtr->dictSize += newDictSize; + if (dctxPtr->dictSize > 64 KB) dctxPtr->dictSize = 64 KB; + dctxPtr->dict = dctxPtr->tmpOut - dctxPtr->dictSize; +} + + +static int LZ4F_decompress_safe (const char* source, char* dest, int compressedSize, int maxDecompressedSize, const char* dictStart, int dictSize) +{ + (void)dictStart; (void)dictSize; + return LZ4_decompress_safe (source, dest, compressedSize, maxDecompressedSize); +} + + /* LZ4F_decompress() * Call this function repetitively to regenerate data compressed within srcBuffer. * The function will attempt to decode *srcSize from srcBuffer, into dstBuffer of maximum size *dstSize. @@ -750,6 +825,7 @@ LZ4F_errorCode_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContex size_t nextCBlockSize=0; const BYTE* selectedIn=NULL; LZ4F_errorCode_t goodResult = OK_NoError; + int (*decoder)(const char*, char*, int, int, const char*, int); if (decompressOptionsPtr==NULL) decompressOptionsPtr = &optionsNull; @@ -890,21 +966,29 @@ goto_getCBlock: goto_decodeCBlock: { int decodedSize; + if (dctxPtr->frameInfo.blockMode == blockLinked) + decoder = LZ4_decompress_safe_usingDict; + else + decoder = LZ4F_decompress_safe; if ((size_t)(dstEnd-dstPtr) < dctxPtr->maxBlockSize) /* not enough room : decode into tmpOut */ { - decodedSize = LZ4_decompress_safe((const char*)selectedIn, (char*)dctxPtr->tmpOut, (int)dctxPtr->sizeToDecode, (int)dctxPtr->maxBlockSize); + decodedSize = decoder((const char*)selectedIn, (char*)dctxPtr->tmpOut, (int)dctxPtr->sizeToDecode, (int)dctxPtr->maxBlockSize, (const char*)dctxPtr->dict, (int)dctxPtr->dictSize); if (decodedSize < 0) return -ERROR_GENERIC; /* decompression failed */ if (dctxPtr->frameInfo.contentChecksumFlag) XXH32_update(&(dctxPtr->xxh), dctxPtr->tmpOut, decodedSize); + if (dctxPtr->frameInfo.blockMode==blockLinked) + LZ4F_saveDict(dctxPtr, dctxPtr->tmpOut, decodedSize); dctxPtr->tmpOutSize = decodedSize; dctxPtr->tmpOutStart = 0; dctxPtr->dStage = dstage_flushOut; break; } - decodedSize = LZ4_decompress_safe((const char*)selectedIn, (char*)dstPtr, (int)dctxPtr->sizeToDecode, (int)dctxPtr->maxBlockSize); + decodedSize = decoder((const char*)selectedIn, (char*)dstPtr, (int)dctxPtr->sizeToDecode, (int)dctxPtr->maxBlockSize, (const char*)dctxPtr->dict, (int)dctxPtr->dictSize); if (decodedSize < 0) return -ERROR_GENERIC; /* decompression failed */ if (dctxPtr->frameInfo.contentChecksumFlag) XXH32_update(&(dctxPtr->xxh), dstPtr, decodedSize); + if (dctxPtr->frameInfo.blockMode==blockLinked) + LZ4F_saveDict(dctxPtr, dstPtr, decodedSize); dstPtr += decodedSize; dctxPtr->dStage = dstage_getCBlockSize; break; diff --git a/programs/frametest.c b/programs/frametest.c index c33e1ac..9db2379 100644 --- a/programs/frametest.c +++ b/programs/frametest.c @@ -352,7 +352,6 @@ _end: _output_error: testResult = 1; DISPLAY("Error detected ! \n"); - if(!no_prompt) getchar(); goto _end; } @@ -395,7 +394,8 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi U32 randState = coreRand ^ prime1; unsigned CCflag = FUZ_rand(&randState) & 1; unsigned BSId = 4 + (FUZ_rand(&randState) & 3); - LZ4F_preferences_t prefs = { { BSId, 0, CCflag, 0,0,0 }, 0,0, 0,0,0,0 }; + unsigned BMId = FUZ_rand(&randState) & 1; + LZ4F_preferences_t prefs = { { BSId, BMId, CCflag, 0,0,0 }, 0,0, 0,0,0,0 }; unsigned nbBits = (FUZ_rand(&randState) % (FUZ_highbit(srcDataLength-1) - 1)) + 1; size_t srcSize = (FUZ_rand(&randState) & ((1< Date: Wed, 10 Sep 2014 13:00:39 +0100 Subject: Fix : streaming mode bug (re-using context & buffers) --- lz4.c | 6 +++--- lz4.h | 2 +- lz4frame.c | 25 +++++++++++++------------ lz4frame.h | 1 + programs/frametest.c | 3 ++- programs/fullbench.c | 4 +--- 6 files changed, 21 insertions(+), 20 deletions(-) diff --git a/lz4.c b/lz4.c index ecb8a79..4e2026e 100644 --- a/lz4.c +++ b/lz4.c @@ -855,7 +855,7 @@ int LZ4_saveDict (LZ4_stream_t* LZ4_dict, char* safeBuffer, int dictSize) if ((U32)dictSize > 64 KB) dictSize = 64 KB; /* useless to define a dictionary > 64 KB */ if ((U32)dictSize > dict->dictSize) dictSize = dict->dictSize; - memcpy(safeBuffer, previousDictEnd - dictSize, dictSize); + memmove(safeBuffer, previousDictEnd - dictSize, dictSize); dict->dictionary = (const BYTE*)safeBuffer; dict->dictSize = (U32)dictSize; @@ -870,9 +870,9 @@ int LZ4_saveDict (LZ4_stream_t* LZ4_dict, char* safeBuffer, int dictSize) ****************************/ /* * This generic decompression function cover all use cases. - * It shall be instanciated several times, using different sets of directives + * It shall be instantiated several times, using different sets of directives * Note that it is essential this generic function is really inlined, - * in order to remove useless branches during compilation optimisation. + * in order to remove useless branches during compilation optimization. */ FORCE_INLINE int LZ4_decompress_generic( const char* source, diff --git a/lz4.h b/lz4.h index 9e192e6..44ada14 100644 --- a/lz4.h +++ b/lz4.h @@ -48,7 +48,7 @@ extern "C" { **************************************/ #define LZ4_VERSION_MAJOR 1 /* for major interface/format changes */ #define LZ4_VERSION_MINOR 3 /* for minor interface/format changes */ -#define LZ4_VERSION_RELEASE 0 /* for tweaks, bug-fixes, or development */ +#define LZ4_VERSION_RELEASE 1 /* for tweaks, bug-fixes, or development */ #define LZ4_VERSION_NUMBER (LZ4_VERSION_MAJOR *100*100 + LZ4_VERSION_MINOR *100 + LZ4_VERSION_RELEASE) int LZ4_versionNumber (void); diff --git a/lz4frame.c b/lz4frame.c index 4871d5b..071fb9e 100644 --- a/lz4frame.c +++ b/lz4frame.c @@ -145,7 +145,6 @@ typedef struct { size_t tmpOutSize; size_t tmpOutStart; XXH32_stateSpace_t xxh; - LZ4_streamDecode_t lz4ctx; BYTE header[8]; } LZ4F_dctx_internal_t; @@ -485,7 +484,7 @@ size_t LZ4F_compress(LZ4F_compressionContext_t compressionContext, void* dstBuff /* complete tmpIn block and then compress it */ BYTE* cSizePtr = dstPtr; U32 cSize; - lastBlockCompressed = 2; + lastBlockCompressed = 1; memcpy(cctxPtr->tmpIn + cctxPtr->tmpInSize, srcBuffer, sizeToCopy); srcPtr += sizeToCopy; dstPtr += 4; /* space for cSize */ @@ -717,27 +716,27 @@ static size_t LZ4F_decodeHeader(LZ4F_dctx_internal_t* dctxPtr, const BYTE* srcPt dctxPtr->frameInfo.blockMode = blockMode; dctxPtr->frameInfo.contentChecksumFlag = contentChecksumFlag; dctxPtr->frameInfo.blockSizeID = blockSizeID; + dctxPtr->maxBlockSize = LZ4F_getBlockSize(blockSizeID); /* init */ if (contentChecksumFlag) XXH32_resetState(&(dctxPtr->xxh), 0); - if (blockMode==blockLinked) LZ4_setStreamDecode(&(dctxPtr->lz4ctx), NULL, 0); - dctxPtr->dictSize = 0; - if (LZ4F_getBlockSize(blockSizeID) > dctxPtr->maxBufferSize) /* tmp buffers too small */ + /* alloc */ + if (dctxPtr->maxBlockSize + (dctxPtr->frameInfo.blockMode==blockLinked) > dctxPtr->maxBufferSize) /* tmp buffers too small */ { FREEMEM(dctxPtr->tmpIn); FREEMEM(dctxPtr->tmpOutBuffer); - dctxPtr->maxBlockSize = LZ4F_getBlockSize(blockSizeID); dctxPtr->maxBufferSize = dctxPtr->maxBlockSize; if (dctxPtr->frameInfo.blockMode==blockLinked) dctxPtr->maxBufferSize += 64 KB; dctxPtr->tmpIn = ALLOCATOR(dctxPtr->maxBlockSize); if (dctxPtr->tmpIn == NULL) return -ERROR_GENERIC; dctxPtr->tmpOutBuffer= ALLOCATOR(dctxPtr->maxBufferSize); if (dctxPtr->tmpOutBuffer== NULL) return -ERROR_GENERIC; - dctxPtr->tmpOut = dctxPtr->tmpOutBuffer; - if (dctxPtr->frameInfo.blockMode==blockLinked) dctxPtr->tmpOut += 64 KB; - dctxPtr->dict = dctxPtr->tmpOut - dctxPtr->dictSize; } + dctxPtr->tmpOut = dctxPtr->tmpOutBuffer; + if (dctxPtr->frameInfo.blockMode==blockLinked) dctxPtr->tmpOut += 64 KB; + dctxPtr->dictSize = 0; + dctxPtr->dict = dctxPtr->tmpOut; return 7; } @@ -778,7 +777,7 @@ LZ4F_errorCode_t LZ4F_getFrameInfo(LZ4F_decompressionContext_t decompressionCont } -static void LZ4F_saveDict(LZ4F_dctx_internal_t* dctxPtr, BYTE* decoded, size_t decodedSize) +static void LZ4F_saveDict(LZ4F_dctx_internal_t* dctxPtr, const BYTE* decoded, size_t decodedSize) { size_t newDictSize = decodedSize; size_t preserveDictSize; @@ -925,6 +924,8 @@ goto_decodeCBlockSize: if ((size_t)(dstEnd-dstPtr) < sizeToCopy) sizeToCopy = dstEnd - dstPtr; memcpy(dstPtr, srcPtr, sizeToCopy); if (dctxPtr->frameInfo.contentChecksumFlag) XXH32_update(&(dctxPtr->xxh), srcPtr, sizeToCopy); + if (dctxPtr->frameInfo.blockMode==blockLinked) + LZ4F_saveDict(dctxPtr, srcPtr, sizeToCopy); srcPtr += sizeToCopy; dstPtr += sizeToCopy; if (sizeToCopy == dctxPtr->sizeToDecode) /* all copied */ @@ -1023,7 +1024,7 @@ goto_getSuffix: } dctxPtr->tmpInSize = 0; dctxPtr->dStage = dstage_storeSuffix; - /* break; useless, it follow */ + /* break; useless, it follows */ } case dstage_storeSuffix: { @@ -1042,7 +1043,7 @@ goto_checkSuffix: { U32 readCRC = LZ4F_readLE32(selectedIn); U32 resultCRC = XXH32_intermediateDigest(&(dctxPtr->xxh)); - if (readCRC != resultCRC) return -ERROR_GENERIC; + if (readCRC != resultCRC) return -ERROR_checksum_invalid; goodResult = OK_FrameEnd; dctxPtr->dStage = dstage_getHeader; goto _end; diff --git a/lz4frame.h b/lz4frame.h index 179271a..039ab14 100644 --- a/lz4frame.h +++ b/lz4frame.h @@ -61,6 +61,7 @@ typedef enum { OK_NoError = 0, ERROR_GENERIC = 1, ERROR_srcSize_tooLarge, ERROR_dstMaxSize_tooSmall, ERROR_allocation_failed, ERROR_compressionLevel_invalid, + ERROR_checksum_invalid, ERROR_maxCode } LZ4F_errorCodes; /* error codes are negative unsigned values. Compare function result to (-specificCode) */ diff --git a/programs/frametest.c b/programs/frametest.c index 9db2379..efe1883 100644 --- a/programs/frametest.c +++ b/programs/frametest.c @@ -392,9 +392,9 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi for ( ; testNb < nbTests; testNb++) { U32 randState = coreRand ^ prime1; - unsigned CCflag = FUZ_rand(&randState) & 1; unsigned BSId = 4 + (FUZ_rand(&randState) & 3); unsigned BMId = FUZ_rand(&randState) & 1; + unsigned CCflag = FUZ_rand(&randState) & 1; LZ4F_preferences_t prefs = { { BSId, BMId, CCflag, 0,0,0 }, 0,0, 0,0,0,0 }; unsigned nbBits = (FUZ_rand(&randState) % (FUZ_highbit(srcDataLength-1) - 1)) + 1; size_t srcSize = (FUZ_rand(&randState) & ((1< (size_t)(iend-ip)) iSize = iend-ip; if (oSize > (size_t)(oend-op)) oSize = oend-op; + oSize = oend-op; result = LZ4F_decompress(dCtx, op, &oSize, ip, &iSize, NULL); CHECK(LZ4F_isError(result), "Decompression failed (error %i)", (int)result); op += oSize; diff --git a/programs/fullbench.c b/programs/fullbench.c index 3d39458..f1d3cc1 100644 --- a/programs/fullbench.c +++ b/programs/fullbench.c @@ -60,10 +60,8 @@ #endif #include "lz4.h" -#define COMPRESSOR0 LZ4_compress #include "lz4hc.h" -#define COMPRESSOR1 LZ4_compressHC -#define DEFAULTCOMPRESSOR COMPRESSOR0 +#include "lz4frame.h" #include "xxhash.h" -- cgit v0.12 From ed4a6bf2cb8a46979e2c9f0eab99052844b21f03 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 10 Sep 2014 13:53:42 +0100 Subject: More tests : random flushes --- lz4frame.h | 10 +++++++--- programs/frametest.c | 9 ++++++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/lz4frame.h b/lz4frame.h index 039ab14..a8f8544 100644 --- a/lz4frame.h +++ b/lz4frame.h @@ -44,6 +44,10 @@ extern "C" { #endif +/**************************************** + Note : experimental API. + Not yet integrated within lz4 library. +****************************************/ /************************************** Includes @@ -58,9 +62,9 @@ typedef size_t LZ4F_errorCode_t; typedef enum { OK_FrameEnd = 1 } LZ4F_successCodes; typedef enum { OK_NoError = 0, ERROR_GENERIC = 1, ERROR_maxBlockSize_invalid, ERROR_blockMode_invalid, ERROR_contentChecksumFlag_invalid, - ERROR_srcSize_tooLarge, ERROR_dstMaxSize_tooSmall, - ERROR_allocation_failed, ERROR_compressionLevel_invalid, + ERROR_allocation_failed, + ERROR_srcSize_tooLarge, ERROR_dstMaxSize_tooSmall, ERROR_checksum_invalid, ERROR_maxCode } LZ4F_errorCodes; /* error codes are negative unsigned values. @@ -80,7 +84,7 @@ typedef enum { noContentChecksum=0, contentChecksumEnabled } contentChecksum_t; typedef struct { blockSizeID_t blockSizeID; /* max64KB, max256KB, max1MB, max4MB ; 0 == default */ blockMode_t blockMode; /* blockLinked, blockIndependent ; 0 == default */ - contentChecksum_t contentChecksumFlag; /* contentChecksumEnabled (default), noContentChecksum ; */ + contentChecksum_t contentChecksumFlag; /* noContentChecksum, contentChecksumEnabled ; 0 == default */ unsigned reserved[3]; } LZ4F_frameInfo_t; diff --git a/programs/frametest.c b/programs/frametest.c index efe1883..2c45f85 100644 --- a/programs/frametest.c +++ b/programs/frametest.c @@ -356,7 +356,7 @@ _output_error: } -static const U32 srcDataLength = 4 MB; +static const U32 srcDataLength = 9 MB; /* needs to be > 2x4MB to test large blocks */ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressibility) { @@ -419,11 +419,18 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi unsigned nbBitsSeg = FUZ_rand(&randState) % maxBits; size_t iSize = (FUZ_rand(&randState) & ((1< (size_t)(iend-ip)) iSize = iend-ip; result = LZ4F_compress(cCtx, op, oSize, ip, iSize, NULL); CHECK(LZ4F_isError(result), "Compression failed (error %i)", (int)result); op += result; ip += iSize; + if (forceFlush) + { + result = LZ4F_flush(cCtx, op, oend-op, NULL); + CHECK(LZ4F_isError(result), "Compression failed (error %i)", (int)result); + op += result; + } } result = LZ4F_compressEnd(cCtx, op, oend-op, NULL); CHECK(LZ4F_isError(result), "Compression completion failed (error %i)", (int)result); -- cgit v0.12 From c71de79688ee778c284c5367388b8c45546da25b Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 10 Sep 2014 22:17:03 +0100 Subject: Improved lz4frame compression speed Added : fullbench test -c14 (LZ4F_compressFrame) --- lz4frame.c | 135 ++++++++++++++++++--------------------------------- lz4frame.h | 12 ++--- programs/Makefile | 4 +- programs/frametest.c | 8 +++ programs/fullbench.c | 8 ++- 5 files changed, 68 insertions(+), 99 deletions(-) diff --git a/lz4frame.c b/lz4frame.c index 071fb9e..55da791 100644 --- a/lz4frame.c +++ b/lz4frame.c @@ -58,7 +58,7 @@ #include /* malloc, calloc, free */ #define ALLOCATOR(s) calloc(1,s) #define FREEMEM free -#include /* memset, memcpy */ +#include /* memset, memcpy, memmove */ #define MEM_INIT memset @@ -119,8 +119,6 @@ typedef struct { size_t maxBlockSize; size_t maxBufferSize; BYTE* tmpBuff; - BYTE* tmpDict; - size_t tmpDictSize; BYTE* tmpIn; size_t tmpInSize; XXH32_stateSpace_t xxh; @@ -209,30 +207,16 @@ int LZ4F_isError(LZ4F_errorCode_t code) **************************************/ size_t LZ4F_compressFrameBound(size_t srcSize, const LZ4F_frameInfo_t* frameInfoPtr) { - const LZ4F_frameInfo_t frameInfoNull = { 0 }; + LZ4F_preferences_t prefs = { 0 }; size_t headerSize; - size_t blockInfoSize; - size_t blockSize; - unsigned nbBlocks; - size_t frameSuffixSize; - size_t totalBound; + size_t streamSize; - if (frameInfoPtr==NULL) frameInfoPtr = &frameInfoNull; /* all parameters set to default */ + if (frameInfoPtr!=NULL) prefs.frameInfo = *frameInfoPtr; headerSize = 7; /* basic header size (no option) including magic number */ - blockInfoSize = 4; /* basic blockInfo size (no option) for one block */ - - blockSize = LZ4F_getBlockSize(frameInfoPtr->blockSizeID); - nbBlocks = (srcSize + (blockSize-1)) / blockSize; - blockInfoSize *= nbBlocks; /* total block info size */ - - frameSuffixSize = 4; /* basic frameSuffixSize (no option) */ - if (frameInfoPtr->contentChecksumFlag == contentChecksumEnabled) frameSuffixSize += 4; - - totalBound = headerSize + srcSize + blockInfoSize + frameSuffixSize; - if (totalBound < srcSize) return -ERROR_srcSize_tooLarge; /* overflow error */ + streamSize = LZ4F_compressBound(srcSize, &prefs); - return totalBound; + return headerSize + streamSize; } @@ -253,11 +237,7 @@ size_t LZ4F_compressFrame(void* dstBuffer, size_t dstMaxSize, const void* srcBuf LZ4F_errorCode_t errorCode; BYTE* const dstStart = (BYTE*) dstBuffer; BYTE* dstPtr = dstStart; - size_t blockSize = LZ4F_getBlockSize(frameInfoPtr->blockSizeID); - unsigned nbBlocks = (srcSize + (blockSize-1)) / blockSize; - unsigned blockNb; - const BYTE* srcPtr = (const BYTE*) srcBuffer; - const size_t dstBlockSize = LZ4F_compressBound(blockSize, frameInfoPtr); + BYTE* const dstEnd = dstStart + dstMaxSize; if (dstMaxSize < LZ4F_compressFrameBound(srcSize, frameInfoPtr)) @@ -270,24 +250,12 @@ size_t LZ4F_compressFrame(void* dstBuffer, size_t dstMaxSize, const void* srcBuf if (LZ4F_isError(errorCode)) return errorCode; dstPtr += errorCode; /* header size */ - for (blockNb=1; blockNbmaxBufferSize < cctxPtr->maxBlockSize + (cctxPtr->prefs.frameInfo.blockMode == blockLinked)) { cctxPtr->maxBufferSize = cctxPtr->maxBlockSize; - if (cctxPtr->prefs.frameInfo.blockMode == blockLinked) cctxPtr->maxBufferSize += 64 KB; + if (cctxPtr->prefs.frameInfo.blockMode == blockLinked) cctxPtr->maxBufferSize += 128 KB; FREEMEM(cctxPtr->tmpBuff); cctxPtr->tmpBuff = ALLOCATOR(cctxPtr->maxBufferSize); if (cctxPtr->tmpBuff == NULL) return -ERROR_allocation_failed; - cctxPtr->tmpDict = cctxPtr->tmpBuff; cctxPtr->tmpIn = cctxPtr->tmpBuff; - if (cctxPtr->prefs.frameInfo.blockMode == blockLinked) cctxPtr->tmpIn += 64 KB; } - cctxPtr->tmpDictSize = 0; cctxPtr->tmpInSize = 0; XXH32_resetState(&(cctxPtr->xxh), 0); LZ4_resetStream(&(cctxPtr->lz4ctx)); @@ -400,40 +365,21 @@ size_t LZ4F_compressBegin(LZ4F_compressionContext_t compressionContext, void* ds * The LZ4F_frameInfo_t structure is optional : * you can provide NULL as argument, all preferences will then be set to default. * */ -size_t LZ4F_compressBound(size_t srcSize, const LZ4F_frameInfo_t* frameInfoPtr) +size_t LZ4F_compressBound(size_t srcSize, const LZ4F_preferences_t* preferencesPtr) { + LZ4F_frameInfo_t* frameInfoPtr = (LZ4F_frameInfo_t*)preferencesPtr; /* works because prefs starts with frameInfo */ blockSizeID_t bid = (frameInfoPtr==NULL) ? LZ4F_BLOCKSIZEID_DEFAULT : frameInfoPtr->blockSizeID; size_t blockSize = LZ4F_getBlockSize(bid); - size_t vSrcSize = srcSize + (blockSize-1); /* worst case : tmp buffer almost filled */ - unsigned nbBlocks = vSrcSize / blockSize; + unsigned bufferize = !(preferencesPtr->autoFlush); + unsigned nbBlocks = (srcSize / blockSize) + 1; + size_t lastBlockSize = bufferize ? blockSize : srcSize % blockSize; size_t blockInfo = 4; /* default, without block CRC option */ - size_t frameEnd = 4 + frameInfoPtr->contentChecksumFlag*4; - size_t lastBlockSize = blockInfo + (blockSize-1) + frameEnd; - size_t result = (blockSize + blockInfo) * nbBlocks; + size_t frameEnd = 4 + (frameInfoPtr->contentChecksumFlag*4); + size_t result = (blockInfo * nbBlocks) + (blockSize * (nbBlocks-1)) + lastBlockSize + frameEnd; - if (result < lastBlockSize) result = lastBlockSize; return result; } -/* LZ4F_getMaxSrcSize() : gives max allowed srcSize given dstMaxSize to handle worst case situations. - * You can use dstMaxSize==0 to know the "natural" srcSize instead (block size). - * The LZ4F_frameInfo_t structure is optional : - * you can provide NULL as argument, all preferences will then be set to default. - * */ -size_t LZ4F_getMaxSrcSize(size_t dstMaxSize, const LZ4F_frameInfo_t* frameInfoPtr) -{ - blockSizeID_t bid = (frameInfoPtr==NULL) ? LZ4F_BLOCKSIZEID_DEFAULT : frameInfoPtr->blockSizeID; - size_t blockSize = LZ4F_getBlockSize(bid); - size_t worstCBlockSize = blockSize + 4; /* default, with no block CRC option */ - unsigned nbBlocks = dstMaxSize / worstCBlockSize; - size_t maxSrcSize = nbBlocks * blockSize; - - if (dstMaxSize == 0) return blockSize; - if (nbBlocks == 0) return -ERROR_dstMaxSize_tooSmall; /* can't even fit one block */ - - return maxSrcSize; -} - /* LZ4F_compress() * You can then call LZ4F_compress() repetitively to compress as much data as necessary. @@ -460,7 +406,7 @@ size_t LZ4F_compress(LZ4F_compressionContext_t compressionContext, void* dstBuff if (cctxPtr->cStage != 1) return -ERROR_GENERIC; - if (dstMaxSize < LZ4F_compressBound(srcSize, &(cctxPtr->prefs.frameInfo))) return -ERROR_dstMaxSize_tooSmall; + if (dstMaxSize < LZ4F_compressBound(srcSize, &(cctxPtr->prefs))) return -ERROR_dstMaxSize_tooSmall; if (compressOptionsPtr == NULL) compressOptionsPtr = &cOptionsNull; /* select compression function */ @@ -507,7 +453,7 @@ size_t LZ4F_compress(LZ4F_compressionContext_t compressionContext, void* dstBuff /* compress one block */ BYTE* cSizePtr = dstPtr; U32 cSize; - lastBlockCompressed = 1; + lastBlockCompressed = 2; dstPtr += 4; /* space for cSizePtr */ cSize = (U32)compress(&(cctxPtr->lz4ctx), (const char*)srcPtr, (char*)dstPtr, (int)(blockSize), (int)(blockSize-1)); dstPtr += cSize; @@ -524,11 +470,20 @@ size_t LZ4F_compress(LZ4F_compressionContext_t compressionContext, void* dstBuff if ((cctxPtr->prefs.frameInfo.blockMode == blockLinked) && (lastBlockCompressed)) { - /* last 64 KB of input become dictionary */ - /* assumption : previous block size was at least 64 KB */ - int result = LZ4_saveDict (&(cctxPtr->lz4ctx), (char*)(cctxPtr->tmpDict), 64 KB); - if (!result) return ERROR_GENERIC; - cctxPtr->tmpIn = cctxPtr->tmpDict + result; + /* last compressed input up to 64 KB become dictionary */ + if (0 && (lastBlockCompressed==1) && + (cctxPtr->tmpBuff + cctxPtr->maxBufferSize > cctxPtr->tmpIn + cctxPtr->tmpInSize + cctxPtr->maxBlockSize)) + { + /* in theory, no need to "save", everything is properly stacked and tracked, so where is the problem ? */ + cctxPtr->tmpIn += cctxPtr->tmpInSize; + } + else + { + int result; + result = LZ4_saveDict (&(cctxPtr->lz4ctx), (char*)(cctxPtr->tmpBuff), 64 KB); + if (result==0) return -ERROR_GENERIC; + cctxPtr->tmpIn = cctxPtr->tmpBuff + result; + } } if (srcPtr < srcEnd) /* some input data left */ @@ -565,7 +520,7 @@ size_t LZ4F_flush(LZ4F_compressionContext_t compressionContext, void* dstBuffer, if (cctxPtr->tmpInSize == 0) return 0; /* nothing to flush */ if (cctxPtr->cStage != 1) return -ERROR_GENERIC; - if (dstMaxSize < LZ4F_compressBound(1, &(cctxPtr->prefs.frameInfo))) return -ERROR_dstMaxSize_tooSmall; + if (dstMaxSize < (cctxPtr->tmpInSize + 16)) return -ERROR_dstMaxSize_tooSmall; if (compressOptionsPtr == NULL) compressOptionsPtr = &cOptionsNull; /* select compression function */ @@ -590,12 +545,13 @@ size_t LZ4F_flush(LZ4F_compressionContext_t compressionContext, void* dstBuffer, cctxPtr->tmpInSize = 0; } - if (cctxPtr->prefs.frameInfo.blockMode == blockLinked) + if ((cctxPtr->prefs.frameInfo.blockMode == blockLinked) + )//&& (cctxPtr->maxBufferSize < (cctxPtr->tmpIn - cctxPtr->tmpDict) + cctxPtr->tmpInSize + cctxPtr->maxBlockSize )) { /* last 64 KB of input become dictionary */ - int result = LZ4_saveDict (&(cctxPtr->lz4ctx), (char*)(cctxPtr->tmpDict), 64 KB); + int result = LZ4_saveDict (&(cctxPtr->lz4ctx), (char*)(cctxPtr->tmpBuff), 64 KB); if (!result) return ERROR_GENERIC; - cctxPtr->tmpIn = cctxPtr->tmpDict + result; + cctxPtr->tmpIn = cctxPtr->tmpBuff + result; } return dstPtr - dstStart; @@ -678,6 +634,7 @@ static size_t LZ4F_decodeHeader(LZ4F_dctx_internal_t* dctxPtr, const BYTE* srcPt { BYTE FLG, BD, HC; unsigned version, blockMode, blockChecksumFlag, contentSizeFlag, contentChecksumFlag, dictFlag, blockSizeID; + size_t bufferNeeded; /* need to decode header to get frameInfo */ if (srcSize < 7) return -ERROR_GENERIC; /* minimal header size */ @@ -722,12 +679,12 @@ static size_t LZ4F_decodeHeader(LZ4F_dctx_internal_t* dctxPtr, const BYTE* srcPt if (contentChecksumFlag) XXH32_resetState(&(dctxPtr->xxh), 0); /* alloc */ - if (dctxPtr->maxBlockSize + (dctxPtr->frameInfo.blockMode==blockLinked) > dctxPtr->maxBufferSize) /* tmp buffers too small */ + bufferNeeded = dctxPtr->maxBlockSize + ((dctxPtr->frameInfo.blockMode==blockLinked) * 64 KB); + if (bufferNeeded > dctxPtr->maxBufferSize) /* tmp buffers too small */ { FREEMEM(dctxPtr->tmpIn); FREEMEM(dctxPtr->tmpOutBuffer); - dctxPtr->maxBufferSize = dctxPtr->maxBlockSize; - if (dctxPtr->frameInfo.blockMode==blockLinked) dctxPtr->maxBufferSize += 64 KB; + dctxPtr->maxBufferSize = bufferNeeded; dctxPtr->tmpIn = ALLOCATOR(dctxPtr->maxBlockSize); if (dctxPtr->tmpIn == NULL) return -ERROR_GENERIC; dctxPtr->tmpOutBuffer= ALLOCATOR(dctxPtr->maxBufferSize); diff --git a/lz4frame.h b/lz4frame.h index a8f8544..bd55a0a 100644 --- a/lz4frame.h +++ b/lz4frame.h @@ -91,7 +91,7 @@ typedef struct { typedef struct { LZ4F_frameInfo_t frameInfo; unsigned compressionLevel; /* from 0 to 16 */ - unsigned autoFlush; /* 1 == automatic flush after each call to LZ4F_compress() */ + unsigned autoFlush; /* 1 == always flush; reduce need for tmp buffer */ unsigned reserved[4]; } LZ4F_preferences_t; @@ -152,12 +152,10 @@ size_t LZ4F_compressBegin(LZ4F_compressionContext_t compressionContext, void* ds * or an error code (can be tested using LZ4F_isError()) */ -size_t LZ4F_compressBound(size_t srcSize, const LZ4F_frameInfo_t* frameInfoPtr); -size_t LZ4F_getMaxSrcSize(size_t dstMaxSize, const LZ4F_frameInfo_t* frameInfoPtr); -/* LZ4F_compressBound() : gives the size of Dst buffer given a srcSize to handle worst case situations. - * LZ4F_getMaxSrcSize() : gives max allowed srcSize given dstMaxSize to handle worst case situations. - * You can use dstMaxSize==0 to know the "natural" srcSize instead (block size). - * The LZ4F_frameInfo_t structure is optional : you can provide NULL as argument, all preferences will then be set to default. +size_t LZ4F_compressBound(size_t srcSize, const LZ4F_preferences_t* preferencesPtr); +/* LZ4F_compressBound() : + * Provides the minimum size of Dst buffer given srcSize to handle worst case situations. + * preferencesPtr is optional : you can provide NULL as argument, all preferences will then be set to default. */ size_t LZ4F_compress(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const void* srcBuffer, size_t srcSize, const LZ4F_compressOptions_t* compressOptionsPtr); diff --git a/programs/Makefile b/programs/Makefile index b28c42c..cc139f1 100644 --- a/programs/Makefile +++ b/programs/Makefile @@ -79,10 +79,10 @@ lz4c : $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c bench.c xxhash.c lz4io.c lz4cli.c lz4c32: $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c bench.c xxhash.c lz4io.c lz4cli.c $(CC) -m32 $(FLAGS) $^ -o $@$(EXT) -fullbench : $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c xxhash.c fullbench.c +fullbench : $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c $(LZ4DIR)/lz4frame.c xxhash.c fullbench.c $(CC) $(FLAGS) $^ -o $@$(EXT) -fullbench32: $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c xxhash.c fullbench.c +fullbench32: $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c $(LZ4DIR)/lz4frame.c xxhash.c fullbench.c $(CC) -m32 $(FLAGS) $^ -o $@$(EXT) fuzzer : $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c xxhash.c fuzzer.c diff --git a/programs/frametest.c b/programs/frametest.c index 2c45f85..9c332d3 100644 --- a/programs/frametest.c +++ b/programs/frametest.c @@ -454,6 +454,14 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi if (oSize > (size_t)(oend-op)) oSize = oend-op; oSize = oend-op; result = LZ4F_decompress(dCtx, op, &oSize, ip, &iSize, NULL); + if (result == (size_t)-ERROR_checksum_invalid) + { + int p=0; + BYTE* b1=(BYTE*)srcBuffer+srcStart; + BYTE* b2=(BYTE*)decodedBuffer; + while (b1[p]==b2[p]) p++; + printf("Error at pos %i : %02X != %02X \n", p, b1[p], b2[p]); + } CHECK(LZ4F_isError(result), "Decompression failed (error %i)", (int)result); op += oSize; ip += iSize; diff --git a/programs/fullbench.c b/programs/fullbench.c index f1d3cc1..b6a1c02 100644 --- a/programs/fullbench.c +++ b/programs/fullbench.c @@ -321,6 +321,11 @@ static int local_LZ4_compressHC_limitedOutput_continue(const char* in, char* out return LZ4_compressHC_limitedOutput_continue(ctx, in, out, inSize, LZ4_compressBound(inSize)); } +static int local_LZ4F_compressFrame(const char* in, char* out, int inSize) +{ + return LZ4F_compressFrame(out, 2*inSize, in, inSize, NULL); +} + static int local_LZ4_decompress_fast(const char* in, char* out, int inSize, int outSize) { (void)inSize; @@ -358,7 +363,7 @@ int fullSpeedBench(char** fileNamesTable, int nbFiles) { int fileIdx=0; char* orig_buff; -# define NB_COMPRESSION_ALGORITHMS 13 +# define NB_COMPRESSION_ALGORITHMS 14 # define MINCOMPRESSIONCHAR '0' double totalCTime[NB_COMPRESSION_ALGORITHMS+1] = {0}; double totalCSize[NB_COMPRESSION_ALGORITHMS+1] = {0}; @@ -494,6 +499,7 @@ int fullSpeedBench(char** fileNamesTable, int nbFiles) case 11: compressionFunction = local_LZ4_compressHC_continue; initFunction = LZ4_createHC; compressorName = "LZ4_compressHC_continue"; break; case 12: compressionFunction = local_LZ4_compressHC_limitedOutput_continue; initFunction = LZ4_createHC; compressorName = "LZ4_compressHC_limitedOutput_continue"; break; case 13: compressionFunction = local_LZ4_compress_forceDict; initFunction = local_LZ4_resetDictT; compressorName = "LZ4_compress_forceDict"; break; + case 14: compressionFunction = local_LZ4F_compressFrame; compressorName = "LZ4F_compressFrame"; break; default : DISPLAY("ERROR ! Bad algorithm Id !! \n"); free(chunkP); return 1; } -- cgit v0.12 From b1d022fa72b75af8fb373e26ac8d2f0f14a9e6fe Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 11 Sep 2014 22:27:14 +0100 Subject: slightly improved frame compression speed --- lz4frame.c | 24 +++++++++++------------- programs/frametest.c | 30 ++++++++++++++++-------------- programs/fullbench.c | 2 +- 3 files changed, 28 insertions(+), 28 deletions(-) diff --git a/lz4frame.c b/lz4frame.c index 55da791..728e070 100644 --- a/lz4frame.c +++ b/lz4frame.c @@ -318,6 +318,7 @@ size_t LZ4F_compressBegin(LZ4F_compressionContext_t compressionContext, void* ds BYTE* const dstStart = (BYTE*)dstBuffer; BYTE* dstPtr = dstStart; BYTE* headerStart; + size_t requiredBuffSize; if (dstMaxSize < LZ4F_MAXHEADERFRAME_SIZE) return -ERROR_dstMaxSize_tooSmall; if (cctxPtr->cStage != 0) return -ERROR_GENERIC; @@ -327,15 +328,15 @@ size_t LZ4F_compressBegin(LZ4F_compressionContext_t compressionContext, void* ds cctxPtr->prefs = *preferencesPtr; if (cctxPtr->prefs.frameInfo.blockSizeID == 0) cctxPtr->prefs.frameInfo.blockSizeID = LZ4F_BLOCKSIZEID_DEFAULT; cctxPtr->maxBlockSize = LZ4F_getBlockSize(cctxPtr->prefs.frameInfo.blockSizeID); - if (cctxPtr->maxBufferSize < cctxPtr->maxBlockSize + (cctxPtr->prefs.frameInfo.blockMode == blockLinked)) + requiredBuffSize = cctxPtr->maxBlockSize + ((cctxPtr->prefs.frameInfo.blockMode == blockLinked) * 128 KB); + if (cctxPtr->maxBufferSize < requiredBuffSize) { - cctxPtr->maxBufferSize = cctxPtr->maxBlockSize; - if (cctxPtr->prefs.frameInfo.blockMode == blockLinked) cctxPtr->maxBufferSize += 128 KB; + cctxPtr->maxBufferSize = requiredBuffSize; FREEMEM(cctxPtr->tmpBuff); cctxPtr->tmpBuff = ALLOCATOR(cctxPtr->maxBufferSize); if (cctxPtr->tmpBuff == NULL) return -ERROR_allocation_failed; - cctxPtr->tmpIn = cctxPtr->tmpBuff; } + cctxPtr->tmpIn = cctxPtr->tmpBuff; cctxPtr->tmpInSize = 0; XXH32_resetState(&(cctxPtr->xxh), 0); LZ4_resetStream(&(cctxPtr->lz4ctx)); @@ -444,6 +445,7 @@ size_t LZ4F_compress(LZ4F_compressionContext_t compressionContext, void* dstBuff memcpy(dstPtr, cctxPtr->tmpIn, blockSize); dstPtr += blockSize; } + if (cctxPtr->prefs.frameInfo.blockMode==blockLinked) cctxPtr->tmpIn += blockSize; cctxPtr->tmpInSize = 0; } } @@ -471,13 +473,8 @@ size_t LZ4F_compress(LZ4F_compressionContext_t compressionContext, void* dstBuff if ((cctxPtr->prefs.frameInfo.blockMode == blockLinked) && (lastBlockCompressed)) { /* last compressed input up to 64 KB become dictionary */ - if (0 && (lastBlockCompressed==1) && - (cctxPtr->tmpBuff + cctxPtr->maxBufferSize > cctxPtr->tmpIn + cctxPtr->tmpInSize + cctxPtr->maxBlockSize)) - { - /* in theory, no need to "save", everything is properly stacked and tracked, so where is the problem ? */ - cctxPtr->tmpIn += cctxPtr->tmpInSize; - } - else + if ((lastBlockCompressed==2) || + ((cctxPtr->tmpBuff + cctxPtr->maxBufferSize) < (cctxPtr->tmpIn + cctxPtr->maxBlockSize))) { int result; result = LZ4_saveDict (&(cctxPtr->lz4ctx), (char*)(cctxPtr->tmpBuff), 64 KB); @@ -486,7 +483,7 @@ size_t LZ4F_compress(LZ4F_compressionContext_t compressionContext, void* dstBuff } } - if (srcPtr < srcEnd) /* some input data left */ + if (srcPtr < srcEnd) /* some input data left, necessarily < blockSize */ { /* fill tmp buffer */ size_t sizeToCopy = srcEnd - srcPtr; @@ -542,11 +539,12 @@ size_t LZ4F_flush(LZ4F_compressionContext_t compressionContext, void* dstBuffer, memcpy(dstPtr, cctxPtr->tmpIn, cctxPtr->tmpInSize); dstPtr += cctxPtr->tmpInSize; } + if (cctxPtr->prefs.frameInfo.blockMode==blockLinked) cctxPtr->tmpIn += cctxPtr->tmpInSize; cctxPtr->tmpInSize = 0; } if ((cctxPtr->prefs.frameInfo.blockMode == blockLinked) - )//&& (cctxPtr->maxBufferSize < (cctxPtr->tmpIn - cctxPtr->tmpDict) + cctxPtr->tmpInSize + cctxPtr->maxBlockSize )) + && ((cctxPtr->tmpBuff + cctxPtr->maxBufferSize) < (cctxPtr->tmpIn + cctxPtr->maxBlockSize))) { /* last 64 KB of input become dictionary */ int result = LZ4_saveDict (&(cctxPtr->lz4ctx), (char*)(cctxPtr->tmpBuff), 64 KB); diff --git a/programs/frametest.c b/programs/frametest.c index 9c332d3..c70cc2f 100644 --- a/programs/frametest.c +++ b/programs/frametest.c @@ -191,7 +191,7 @@ static unsigned FUZ_highbit(U32 v32) } -int basicTests(U32 seed, int nbCycles, int startCycle, double compressibility) +int basicTests(U32 seed, double compressibility) { int testResult = 0; void* CNBuffer; @@ -203,7 +203,6 @@ int basicTests(U32 seed, int nbCycles, int startCycle, double compressibility) LZ4F_decompressionContext_t dCtx; U64 crcOrig; - (void)nbCycles; (void)startCycle; // Create compressible test buffer CNBuffer = malloc(COMPRESSIBLE_NOISE_LENGTH); compressedBuffer = malloc(LZ4F_compressFrameBound(COMPRESSIBLE_NOISE_LENGTH, NULL)); @@ -356,6 +355,16 @@ _output_error: } +static void locateBuffDiff(const void* buff1, const void* buff2) +{ + int p=0; + BYTE* b1=(BYTE*)buff1; + BYTE* b2=(BYTE*)buff2; + while (b1[p]==b2[p]) p++; + printf("Error at pos %i : %02X != %02X \n", p, b1[p], b2[p]); + } + + static const U32 srcDataLength = 9 MB; /* needs to be > 2x4MB to test large blocks */ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressibility) @@ -403,10 +412,10 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi U64 crcOrig, crcDecoded; DISPLAYUPDATE(2, "\r%5i ", testNb); - crcOrig = XXH64(srcBuffer+srcStart, srcSize, 1); + crcOrig = XXH64((BYTE*)srcBuffer+srcStart, srcSize, 1); { - const BYTE* ip = srcBuffer + srcStart; + const BYTE* ip = (const BYTE*)srcBuffer + srcStart; const BYTE* const iend = ip + srcSize; BYTE* op = compressedBuffer; BYTE* const oend = op + LZ4F_compressFrameBound(srcDataLength, NULL); @@ -454,14 +463,7 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi if (oSize > (size_t)(oend-op)) oSize = oend-op; oSize = oend-op; result = LZ4F_decompress(dCtx, op, &oSize, ip, &iSize, NULL); - if (result == (size_t)-ERROR_checksum_invalid) - { - int p=0; - BYTE* b1=(BYTE*)srcBuffer+srcStart; - BYTE* b2=(BYTE*)decodedBuffer; - while (b1[p]==b2[p]) p++; - printf("Error at pos %i : %02X != %02X \n", p, b1[p], b2[p]); - } + if (result == (size_t)-ERROR_checksum_invalid) locateBuffDiff((BYTE*)srcBuffer+srcStart, decodedBuffer); CHECK(LZ4F_isError(result), "Decompression failed (error %i)", (int)result); op += oSize; ip += iSize; @@ -514,7 +516,7 @@ int main(int argc, char** argv) int nbTests = nbTestsDefault; int testNb = 0; int proba = FUZ_COMPRESSIBILITY_DEFAULT; - int result; + int result=0; // Check command line programName = argv[0]; @@ -601,7 +603,7 @@ int main(int argc, char** argv) if (nbTests<=0) nbTests=1; - result = basicTests(seed, nbTests, testNb, ((double)proba) / 100); + if (testNb==0) result = basicTests(seed, ((double)proba) / 100); if (result) return 1; return fuzzerTests(seed, nbTests, testNb, ((double)proba) / 100); } diff --git a/programs/fullbench.c b/programs/fullbench.c index b6a1c02..96120e3 100644 --- a/programs/fullbench.c +++ b/programs/fullbench.c @@ -323,7 +323,7 @@ static int local_LZ4_compressHC_limitedOutput_continue(const char* in, char* out static int local_LZ4F_compressFrame(const char* in, char* out, int inSize) { - return LZ4F_compressFrame(out, 2*inSize, in, inSize, NULL); + return LZ4F_compressFrame(out, 2*inSize + (4<<20), in, inSize, NULL); } static int local_LZ4_decompress_fast(const char* in, char* out, int inSize, int outSize) -- cgit v0.12 From a586208597f28b7b4df0913f5b95cefbd46e2a31 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 12 Sep 2014 19:28:44 +0100 Subject: lz4frame : autoflush mode : saves memory, and improves compression speed LZ4F_getFrameInfo() fix, thanks to Christopher Jackson --- lz4frame.c | 81 ++++++++++++++++++++++++++++++++++------------------ lz4frame.h | 19 ++++++------ programs/frametest.c | 21 +++++++------- programs/fullbench.c | 39 +++++++++++++------------ 4 files changed, 95 insertions(+), 65 deletions(-) diff --git a/lz4frame.c b/lz4frame.c index 728e070..4c63227 100644 --- a/lz4frame.c +++ b/lz4frame.c @@ -205,13 +205,14 @@ int LZ4F_isError(LZ4F_errorCode_t code) /************************************** Simple compression functions **************************************/ -size_t LZ4F_compressFrameBound(size_t srcSize, const LZ4F_frameInfo_t* frameInfoPtr) +size_t LZ4F_compressFrameBound(size_t srcSize, const LZ4F_preferences_t* preferencesPtr) { LZ4F_preferences_t prefs = { 0 }; size_t headerSize; size_t streamSize; - if (frameInfoPtr!=NULL) prefs.frameInfo = *frameInfoPtr; + if (preferencesPtr!=NULL) prefs = *preferencesPtr; + prefs.autoFlush = 1; headerSize = 7; /* basic header size (no option) including magic number */ streamSize = LZ4F_compressBound(srcSize, &prefs); @@ -231,8 +232,7 @@ size_t LZ4F_compressFrameBound(size_t srcSize, const LZ4F_frameInfo_t* frameInfo */ size_t LZ4F_compressFrame(void* dstBuffer, size_t dstMaxSize, const void* srcBuffer, size_t srcSize, const LZ4F_preferences_t* preferencesPtr) { - const LZ4F_frameInfo_t frameInfoNull = { 0 }; - const LZ4F_frameInfo_t* const frameInfoPtr = (preferencesPtr==NULL) ? &frameInfoNull : &(preferencesPtr->frameInfo); + LZ4F_preferences_t prefs = { 0 }; LZ4F_compressionContext_t cctx = NULL; LZ4F_errorCode_t errorCode; BYTE* const dstStart = (BYTE*) dstBuffer; @@ -240,18 +240,21 @@ size_t LZ4F_compressFrame(void* dstBuffer, size_t dstMaxSize, const void* srcBuf BYTE* const dstEnd = dstStart + dstMaxSize; - if (dstMaxSize < LZ4F_compressFrameBound(srcSize, frameInfoPtr)) + if (preferencesPtr!=NULL) prefs = *preferencesPtr; + prefs.autoFlush = 1; + + if (dstMaxSize < LZ4F_compressFrameBound(srcSize, &prefs)) return -ERROR_dstMaxSize_tooSmall; errorCode = LZ4F_createCompressionContext(&cctx, LZ4F_VERSION); if (LZ4F_isError(errorCode)) return errorCode; - errorCode = LZ4F_compressBegin(cctx, dstBuffer, dstMaxSize, preferencesPtr); /* write header */ + errorCode = LZ4F_compressBegin(cctx, dstBuffer, dstMaxSize, &prefs); /* write header */ if (LZ4F_isError(errorCode)) return errorCode; dstPtr += errorCode; /* header size */ dstMaxSize -= errorCode; - errorCode = LZ4F_compress(cctx, dstPtr, dstMaxSize, srcBuffer, srcSize, NULL); + errorCode = LZ4F_compressUpdate(cctx, dstPtr, dstMaxSize, srcBuffer, srcSize, NULL); if (LZ4F_isError(errorCode)) return errorCode; dstPtr += errorCode; @@ -328,12 +331,16 @@ size_t LZ4F_compressBegin(LZ4F_compressionContext_t compressionContext, void* ds cctxPtr->prefs = *preferencesPtr; if (cctxPtr->prefs.frameInfo.blockSizeID == 0) cctxPtr->prefs.frameInfo.blockSizeID = LZ4F_BLOCKSIZEID_DEFAULT; cctxPtr->maxBlockSize = LZ4F_getBlockSize(cctxPtr->prefs.frameInfo.blockSizeID); + requiredBuffSize = cctxPtr->maxBlockSize + ((cctxPtr->prefs.frameInfo.blockMode == blockLinked) * 128 KB); + if (preferencesPtr->autoFlush) + requiredBuffSize = (cctxPtr->prefs.frameInfo.blockMode == blockLinked) * 64 KB; /* just needs dict */ + if (cctxPtr->maxBufferSize < requiredBuffSize) { cctxPtr->maxBufferSize = requiredBuffSize; FREEMEM(cctxPtr->tmpBuff); - cctxPtr->tmpBuff = ALLOCATOR(cctxPtr->maxBufferSize); + cctxPtr->tmpBuff = ALLOCATOR(requiredBuffSize); if (cctxPtr->tmpBuff == NULL) return -ERROR_allocation_failed; } cctxPtr->tmpIn = cctxPtr->tmpBuff; @@ -371,9 +378,8 @@ size_t LZ4F_compressBound(size_t srcSize, const LZ4F_preferences_t* preferencesP LZ4F_frameInfo_t* frameInfoPtr = (LZ4F_frameInfo_t*)preferencesPtr; /* works because prefs starts with frameInfo */ blockSizeID_t bid = (frameInfoPtr==NULL) ? LZ4F_BLOCKSIZEID_DEFAULT : frameInfoPtr->blockSizeID; size_t blockSize = LZ4F_getBlockSize(bid); - unsigned bufferize = !(preferencesPtr->autoFlush); unsigned nbBlocks = (srcSize / blockSize) + 1; - size_t lastBlockSize = bufferize ? blockSize : srcSize % blockSize; + size_t lastBlockSize = preferencesPtr->autoFlush ? srcSize % blockSize : blockSize; size_t blockInfo = 4; /* default, without block CRC option */ size_t frameEnd = 4 + (frameInfoPtr->contentChecksumFlag*4); size_t result = (blockInfo * nbBlocks) + (blockSize * (nbBlocks-1)) + lastBlockSize + frameEnd; @@ -382,17 +388,16 @@ size_t LZ4F_compressBound(size_t srcSize, const LZ4F_preferences_t* preferencesP } -/* LZ4F_compress() - * You can then call LZ4F_compress() repetitively to compress as much data as necessary. +/* LZ4F_compressUpdate() + * LZ4F_compressUpdate() can be called repetitively to compress as much data as necessary. * The most important rule is that dstBuffer MUST be large enough (dstMaxSize) to ensure compression completion even in worst case. - * You can get the minimum value of dstMaxSize by using LZ4F_compressBound() - * Conversely, given a fixed dstMaxSize value, you can know the maximum srcSize authorized using LZ4F_getMaxSrcSize() * If this condition is not respected, LZ4F_compress() will fail (result is an errorCode) + * You can get the minimum value of dstMaxSize by using LZ4F_compressBound() * The LZ4F_compressOptions_t structure is optional : you can provide NULL as argument. - * The result of the function is the number of bytes written into dstBuffer (it can be zero, meaning input data is just stored within compressionContext for a future block to complete) + * The result of the function is the number of bytes written into dstBuffer : it can be zero, meaning input data was just buffered. * The function outputs an error code if it fails (can be tested using LZ4F_isError()) */ -size_t LZ4F_compress(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const void* srcBuffer, size_t srcSize, const LZ4F_compressOptions_t* compressOptionsPtr) +size_t LZ4F_compressUpdate(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const void* srcBuffer, size_t srcSize, const LZ4F_compressOptions_t* compressOptionsPtr) { LZ4F_compressOptions_t cOptionsNull = { 0 }; LZ4F_cctx_internal_t* cctxPtr = (LZ4F_cctx_internal_t*)compressionContext; @@ -470,6 +475,27 @@ size_t LZ4F_compress(LZ4F_compressionContext_t compressionContext, void* dstBuff srcPtr += blockSize; } + if ((cctxPtr->prefs.autoFlush) && (srcPtr < srcEnd)) + { + /* compress remaining input */ + BYTE* cSizePtr = dstPtr; + U32 cSize; + size_t iSize = srcEnd - srcPtr; + lastBlockCompressed = 2; + dstPtr += 4; /* space for cSizePtr */ + cSize = (U32)compress(&(cctxPtr->lz4ctx), (const char*)srcPtr, (char*)dstPtr, (int)(iSize), (int)(iSize-1)); + dstPtr += cSize; + LZ4F_writeLE32(cSizePtr, cSize); + if (cSize == 0) /* compression failed */ + { + cSize = iSize + LZ4F_BLOCKUNCOMPRESSED_FLAG; + LZ4F_writeLE32(cSizePtr, cSize); + memcpy(dstPtr, srcPtr, iSize); + dstPtr += iSize; + } + srcPtr += iSize; + } + if ((cctxPtr->prefs.frameInfo.blockMode == blockLinked) && (lastBlockCompressed)) { /* last compressed input up to 64 KB become dictionary */ @@ -484,12 +510,12 @@ size_t LZ4F_compress(LZ4F_compressionContext_t compressionContext, void* dstBuff } if (srcPtr < srcEnd) /* some input data left, necessarily < blockSize */ - { - /* fill tmp buffer */ - size_t sizeToCopy = srcEnd - srcPtr; - memcpy(cctxPtr->tmpIn, srcPtr, sizeToCopy); - cctxPtr->tmpInSize = sizeToCopy; - } + { + /* fill tmp buffer */ + size_t sizeToCopy = srcEnd - srcPtr; + memcpy(cctxPtr->tmpIn, srcPtr, sizeToCopy); + cctxPtr->tmpInSize = sizeToCopy; + } if (cctxPtr->prefs.frameInfo.contentChecksumFlag == contentChecksumEnabled) XXH32_update(&(cctxPtr->xxh), srcBuffer, (unsigned)srcSize); @@ -709,24 +735,25 @@ typedef enum { dstage_getHeader=0, dstage_storeHeader, dstage_decodeHeader, * It is optional : you could start by calling directly LZ4F_decompress() instead. * The objective is to extract header information without starting decompression, typically for allocation purposes. * LZ4F_getFrameInfo() can also be used *after* starting decompression, on a valid LZ4F_decompressionContext_t. - * The number of bytes read from srcBuffer will be provided within *srcSize (necessarily <= original value). + * The number of bytes read from srcBuffer will be provided within *srcSizePtr (necessarily <= original value). * The function result is an error code which can be tested using LZ4F_isError(). */ -LZ4F_errorCode_t LZ4F_getFrameInfo(LZ4F_decompressionContext_t decompressionContext, LZ4F_frameInfo_t* frameInfoPtr, const void* srcBuffer, size_t* srcSize) +LZ4F_errorCode_t LZ4F_getFrameInfo(LZ4F_decompressionContext_t decompressionContext, LZ4F_frameInfo_t* frameInfoPtr, const void* srcBuffer, size_t* srcSizePtr) { LZ4F_dctx_internal_t* dctxPtr = (LZ4F_dctx_internal_t*)decompressionContext; if (dctxPtr->dStage==0) { - LZ4F_errorCode_t errorCode = LZ4F_decodeHeader(dctxPtr, srcBuffer, *srcSize); + LZ4F_errorCode_t errorCode = LZ4F_decodeHeader(dctxPtr, srcBuffer, *srcSizePtr); if (LZ4F_isError(errorCode)) return errorCode; - *srcSize = errorCode; + *srcSizePtr = errorCode; + *frameInfoPtr = dctxPtr->frameInfo; dctxPtr->dStage = dstage_getCBlockSize; return OK_NoError; } /* frameInfo already decoded */ - *srcSize = 0; + *srcSizePtr = 0; *frameInfoPtr = dctxPtr->frameInfo; return OK_NoError; } diff --git a/lz4frame.h b/lz4frame.h index bd55a0a..25cf043 100644 --- a/lz4frame.h +++ b/lz4frame.h @@ -100,7 +100,7 @@ typedef struct { /*********************************** * Simple compression function * *********************************/ -size_t LZ4F_compressFrameBound(size_t srcSize, const LZ4F_frameInfo_t* frameInfoPtr); +size_t LZ4F_compressFrameBound(size_t srcSize, const LZ4F_preferences_t* preferencesPtr); size_t LZ4F_compressFrame(void* dstBuffer, size_t dstMaxSize, const void* srcBuffer, size_t srcSize, const LZ4F_preferences_t* preferencesPtr); /* LZ4F_compressFrame() @@ -158,22 +158,21 @@ size_t LZ4F_compressBound(size_t srcSize, const LZ4F_preferences_t* preferencesP * preferencesPtr is optional : you can provide NULL as argument, all preferences will then be set to default. */ -size_t LZ4F_compress(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const void* srcBuffer, size_t srcSize, const LZ4F_compressOptions_t* compressOptionsPtr); -/* LZ4F_compress() - * You can then call LZ4F_compress() repetitively to compress as much data as necessary. +size_t LZ4F_compressUpdate(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const void* srcBuffer, size_t srcSize, const LZ4F_compressOptions_t* compressOptionsPtr); +/* LZ4F_compressUpdate() + * LZ4F_compressUpdate() can be called repetitively to compress as much data as necessary. * The most important rule is that dstBuffer MUST be large enough (dstMaxSize) to ensure compression completion even in worst case. - * You can get the minimum value of dstMaxSize by using LZ4F_compressBound() - * Conversely, given a fixed dstMaxSize value, you can know the maximum srcSize authorized using LZ4F_getMaxSrcSize() * If this condition is not respected, LZ4F_compress() will fail (result is an errorCode) + * You can get the minimum value of dstMaxSize by using LZ4F_compressBound() * The LZ4F_compressOptions_t structure is optional : you can provide NULL as argument. - * The result of the function is the number of bytes written into dstBuffer : it can be zero, meaning input data was just stored within compressionContext + * The result of the function is the number of bytes written into dstBuffer : it can be zero, meaning input data was just buffered. * The function outputs an error code if it fails (can be tested using LZ4F_isError()) */ size_t LZ4F_flush(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const LZ4F_compressOptions_t* compressOptionsPtr); /* LZ4F_flush() * Should you need to create compressed data immediately, without waiting for a block to be filled, - * you can call LZ4_flush(), which will immediately compress any remaining data stored within compressionContext. + * you can call LZ4_flush(), which will immediately compress any remaining data buffered within compressionContext. * The LZ4F_compressOptions_t structure is optional : you can provide NULL as argument. * The result of the function is the number of bytes written into dstBuffer * (it can be zero, this means there was no data left within compressionContext) @@ -217,13 +216,13 @@ LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_compressionContext_t LZ4F_de /* Decompression */ -LZ4F_errorCode_t LZ4F_getFrameInfo(LZ4F_decompressionContext_t decompressionContext, LZ4F_frameInfo_t* frameInfoPtr, const void* srcBuffer, size_t* srcSize); +LZ4F_errorCode_t LZ4F_getFrameInfo(LZ4F_decompressionContext_t decompressionContext, LZ4F_frameInfo_t* frameInfoPtr, const void* srcBuffer, size_t* srcSizePtr); /* LZ4F_getFrameInfo() * This function decodes frame header information, such as blockSize. * It is optional : you could start by calling directly LZ4F_decompress() instead. * The objective is to extract header information without starting decompression, typically for allocation purposes. * LZ4F_getFrameInfo() can also be used *after* starting decompression, on a valid LZ4F_decompressionContext_t. - * The number of bytes read from srcBuffer will be provided within *srcSize (necessarily <= original value). + * The number of bytes read from srcBuffer will be provided within *srcSizePtr (necessarily <= original value). * The function result is an error code which can be tested using LZ4F_isError(). */ diff --git a/programs/frametest.c b/programs/frametest.c index c70cc2f..c3662b4 100644 --- a/programs/frametest.c +++ b/programs/frametest.c @@ -258,20 +258,20 @@ int basicTests(U32 seed, double compressibility) DISPLAYLEVEL(3, "Using 64 KB block : \n"); prefs.frameInfo.blockSizeID = max64KB; prefs.frameInfo.contentChecksumFlag = contentChecksumEnabled; - cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &(prefs.frameInfo)), CNBuffer, testSize, &prefs); + cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &prefs), CNBuffer, testSize, &prefs); if (LZ4F_isError(cSize)) goto _output_error; DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); DISPLAYLEVEL(3, "without checksum : \n"); prefs.frameInfo.contentChecksumFlag = noContentChecksum; - cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &(prefs.frameInfo)), CNBuffer, testSize, &prefs); + cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &prefs), CNBuffer, testSize, &prefs); if (LZ4F_isError(cSize)) goto _output_error; DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); DISPLAYLEVEL(3, "Using 256 KB block : \n"); prefs.frameInfo.blockSizeID = max256KB; prefs.frameInfo.contentChecksumFlag = contentChecksumEnabled; - cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &(prefs.frameInfo)), CNBuffer, testSize, &prefs); + cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &prefs), CNBuffer, testSize, &prefs); if (LZ4F_isError(cSize)) goto _output_error; DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); @@ -311,33 +311,33 @@ int basicTests(U32 seed, double compressibility) DISPLAYLEVEL(3, "without checksum : \n"); prefs.frameInfo.contentChecksumFlag = noContentChecksum; - cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &(prefs.frameInfo)), CNBuffer, testSize, &prefs); + cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &prefs), CNBuffer, testSize, &prefs); if (LZ4F_isError(cSize)) goto _output_error; DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); DISPLAYLEVEL(3, "Using 1 MB block : \n"); prefs.frameInfo.blockSizeID = max1MB; prefs.frameInfo.contentChecksumFlag = contentChecksumEnabled; - cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &(prefs.frameInfo)), CNBuffer, testSize, &prefs); + cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &prefs), CNBuffer, testSize, &prefs); if (LZ4F_isError(cSize)) goto _output_error; DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); DISPLAYLEVEL(3, "without checksum : \n"); prefs.frameInfo.contentChecksumFlag = noContentChecksum; - cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &(prefs.frameInfo)), CNBuffer, testSize, &prefs); + cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &prefs), CNBuffer, testSize, &prefs); if (LZ4F_isError(cSize)) goto _output_error; DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); DISPLAYLEVEL(3, "Using 4 MB block : \n"); prefs.frameInfo.blockSizeID = max4MB; prefs.frameInfo.contentChecksumFlag = contentChecksumEnabled; - cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &(prefs.frameInfo)), CNBuffer, testSize, &prefs); + cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &prefs), CNBuffer, testSize, &prefs); if (LZ4F_isError(cSize)) goto _output_error; DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); DISPLAYLEVEL(3, "without checksum : \n"); prefs.frameInfo.contentChecksumFlag = noContentChecksum; - cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &(prefs.frameInfo)), CNBuffer, testSize, &prefs); + cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &prefs), CNBuffer, testSize, &prefs); if (LZ4F_isError(cSize)) goto _output_error; DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); @@ -404,7 +404,8 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi unsigned BSId = 4 + (FUZ_rand(&randState) & 3); unsigned BMId = FUZ_rand(&randState) & 1; unsigned CCflag = FUZ_rand(&randState) & 1; - LZ4F_preferences_t prefs = { { BSId, BMId, CCflag, 0,0,0 }, 0,0, 0,0,0,0 }; + unsigned autoflush = (FUZ_rand(&randState) & 3) == 2; + LZ4F_preferences_t prefs = { { BSId, BMId, CCflag, 0,0,0 }, 0,autoflush, 0,0,0,0 }; unsigned nbBits = (FUZ_rand(&randState) % (FUZ_highbit(srcDataLength-1) - 1)) + 1; size_t srcSize = (FUZ_rand(&randState) & ((1< (size_t)(iend-ip)) iSize = iend-ip; - result = LZ4F_compress(cCtx, op, oSize, ip, iSize, NULL); + result = LZ4F_compressUpdate(cCtx, op, oSize, ip, iSize, NULL); CHECK(LZ4F_isError(result), "Compression failed (error %i)", (int)result); op += result; ip += iSize; diff --git a/programs/fullbench.c b/programs/fullbench.c index 96120e3..794b5b2 100644 --- a/programs/fullbench.c +++ b/programs/fullbench.c @@ -323,7 +323,7 @@ static int local_LZ4_compressHC_limitedOutput_continue(const char* in, char* out static int local_LZ4F_compressFrame(const char* in, char* out, int inSize) { - return LZ4F_compressFrame(out, 2*inSize + (4<<20), in, inSize, NULL); + return LZ4F_compressFrame(out, 2*inSize + 16, in, inSize, NULL); } static int local_LZ4_decompress_fast(const char* in, char* out, int inSize, int outSize) @@ -431,22 +431,6 @@ int fullSpeedBench(char** fileNamesTable, int nbFiles) return 12; } - // Init chunks data - { - int i; - size_t remaining = benchedSize; - char* in = orig_buff; - char* out = compressed_buff; - for (i=0; i chunkSize) { chunkP[i].origSize = chunkSize; remaining -= chunkSize; } else { chunkP[i].origSize = (int)remaining; remaining = 0; } - chunkP[i].compressedBuffer = out; out += maxCompressedChunkSize; - chunkP[i].compressedSize = 0; - } - } - // Fill input buffer DISPLAY("Loading %s... \r", inFileName); readSize = fread(orig_buff, 1, benchedSize, inFile); @@ -484,6 +468,23 @@ int fullSpeedBench(char** fileNamesTable, int nbFiles) if ((compressionAlgo != ALL_COMPRESSORS) && (compressionAlgo != cAlgNb)) continue; + // Init chunks data + { + int i; + size_t remaining = benchedSize; + char* in = orig_buff; + char* out = compressed_buff; + nbChunks = (int) ((int)benchedSize / chunkSize) + 1; + for (i=0; i chunkSize) { chunkP[i].origSize = chunkSize; remaining -= chunkSize; } else { chunkP[i].origSize = (int)remaining; remaining = 0; } + chunkP[i].compressedBuffer = out; out += maxCompressedChunkSize; + chunkP[i].compressedSize = 0; + } + } + switch(cAlgNb) { case 1 : compressionFunction = LZ4_compress; compressorName = "LZ4_compress"; break; @@ -499,7 +500,9 @@ int fullSpeedBench(char** fileNamesTable, int nbFiles) case 11: compressionFunction = local_LZ4_compressHC_continue; initFunction = LZ4_createHC; compressorName = "LZ4_compressHC_continue"; break; case 12: compressionFunction = local_LZ4_compressHC_limitedOutput_continue; initFunction = LZ4_createHC; compressorName = "LZ4_compressHC_limitedOutput_continue"; break; case 13: compressionFunction = local_LZ4_compress_forceDict; initFunction = local_LZ4_resetDictT; compressorName = "LZ4_compress_forceDict"; break; - case 14: compressionFunction = local_LZ4F_compressFrame; compressorName = "LZ4F_compressFrame"; break; + case 14: compressionFunction = local_LZ4F_compressFrame; compressorName = "LZ4F_compressFrame"; + chunkP[0].origSize = benchedSize; nbChunks=1; + break; default : DISPLAY("ERROR ! Bad algorithm Id !! \n"); free(chunkP); return 1; } -- cgit v0.12 From bd704cf70ad7b935db0d1baaee902b28e6394bba Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sat, 13 Sep 2014 10:08:55 +0100 Subject: lz4frame : implemented option stableSrc Improved LZ4_compressFrame() speed --- lz4frame.c | 24 +++++++++++------------- lz4frame.h | 8 ++++---- programs/frametest.c | 17 +++++++++++++---- 3 files changed, 28 insertions(+), 21 deletions(-) diff --git a/lz4frame.c b/lz4frame.c index 4c63227..7290972 100644 --- a/lz4frame.c +++ b/lz4frame.c @@ -232,8 +232,9 @@ size_t LZ4F_compressFrameBound(size_t srcSize, const LZ4F_preferences_t* prefere */ size_t LZ4F_compressFrame(void* dstBuffer, size_t dstMaxSize, const void* srcBuffer, size_t srcSize, const LZ4F_preferences_t* preferencesPtr) { - LZ4F_preferences_t prefs = { 0 }; - LZ4F_compressionContext_t cctx = NULL; + LZ4F_cctx_internal_t cctxI = { 0 }; /* works because no allocation */ + LZ4F_preferences_t prefs = { 0 }; + LZ4F_compressOptions_t options = { 0 }; LZ4F_errorCode_t errorCode; BYTE* const dstStart = (BYTE*) dstBuffer; BYTE* dstPtr = dstStart; @@ -241,30 +242,27 @@ size_t LZ4F_compressFrame(void* dstBuffer, size_t dstMaxSize, const void* srcBuf if (preferencesPtr!=NULL) prefs = *preferencesPtr; + cctxI.version = LZ4F_VERSION; + cctxI.maxBufferSize = 64 KB; /* mess with real buffer size, to prevent allocation; works because autoflush==1 & stableSrc==1 */ prefs.autoFlush = 1; + options.stableSrc = 1; if (dstMaxSize < LZ4F_compressFrameBound(srcSize, &prefs)) return -ERROR_dstMaxSize_tooSmall; - errorCode = LZ4F_createCompressionContext(&cctx, LZ4F_VERSION); - if (LZ4F_isError(errorCode)) return errorCode; - - errorCode = LZ4F_compressBegin(cctx, dstBuffer, dstMaxSize, &prefs); /* write header */ + errorCode = LZ4F_compressBegin(&cctxI, dstBuffer, dstMaxSize, &prefs); /* write header */ if (LZ4F_isError(errorCode)) return errorCode; dstPtr += errorCode; /* header size */ dstMaxSize -= errorCode; - errorCode = LZ4F_compressUpdate(cctx, dstPtr, dstMaxSize, srcBuffer, srcSize, NULL); + errorCode = LZ4F_compressUpdate(&cctxI, dstPtr, dstMaxSize, srcBuffer, srcSize, &options); if (LZ4F_isError(errorCode)) return errorCode; dstPtr += errorCode; - errorCode = LZ4F_compressEnd(cctx, dstPtr, dstEnd-dstPtr, NULL); /* flush last block, and generate suffix */ + errorCode = LZ4F_compressEnd(&cctxI, dstPtr, dstEnd-dstPtr, &options); /* flush last block, and generate suffix */ if (LZ4F_isError(errorCode)) return errorCode; dstPtr += errorCode; - errorCode = LZ4F_freeCompressionContext(cctx); - if (LZ4F_isError(errorCode)) return errorCode; - return (dstPtr - dstStart); } @@ -496,9 +494,9 @@ size_t LZ4F_compressUpdate(LZ4F_compressionContext_t compressionContext, void* d srcPtr += iSize; } - if ((cctxPtr->prefs.frameInfo.blockMode == blockLinked) && (lastBlockCompressed)) + /* save last input up to 64 KB for dictionary */ + if ((cctxPtr->prefs.frameInfo.blockMode == blockLinked) && (lastBlockCompressed) && (!compressOptionsPtr->stableSrc)) { - /* last compressed input up to 64 KB become dictionary */ if ((lastBlockCompressed==2) || ((cctxPtr->tmpBuff + cctxPtr->maxBufferSize) < (cctxPtr->tmpIn + cctxPtr->maxBlockSize))) { diff --git a/lz4frame.h b/lz4frame.h index 25cf043..d75d462 100644 --- a/lz4frame.h +++ b/lz4frame.h @@ -85,7 +85,7 @@ typedef struct { blockSizeID_t blockSizeID; /* max64KB, max256KB, max1MB, max4MB ; 0 == default */ blockMode_t blockMode; /* blockLinked, blockIndependent ; 0 == default */ contentChecksum_t contentChecksumFlag; /* noContentChecksum, contentChecksumEnabled ; 0 == default */ - unsigned reserved[3]; + unsigned reserved[5]; } LZ4F_frameInfo_t; typedef struct { @@ -122,8 +122,8 @@ size_t LZ4F_compressFrame(void* dstBuffer, size_t dstMaxSize, const void* srcBuf typedef void* LZ4F_compressionContext_t; typedef struct { - unsigned stableSrc; /* unused for the time being, must be 0 */ - unsigned reserved[5]; + unsigned stableSrc; /* 1 == src content will remain available on future calls to LZ4F_compress(); avoid saving src content within tmp buffer as future dictionary */ + unsigned reserved[3]; } LZ4F_compressOptions_t; /* Resource Management */ @@ -199,7 +199,7 @@ typedef void* LZ4F_decompressionContext_t; typedef struct { unsigned stableDst; /* unused for the time being, must be 0 */ - unsigned reserved[5]; + unsigned reserved[3]; } LZ4F_decompressOptions_t; /* Resource management */ diff --git a/programs/frametest.c b/programs/frametest.c index c3662b4..73d6373 100644 --- a/programs/frametest.c +++ b/programs/frametest.c @@ -405,13 +405,19 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi unsigned BMId = FUZ_rand(&randState) & 1; unsigned CCflag = FUZ_rand(&randState) & 1; unsigned autoflush = (FUZ_rand(&randState) & 3) == 2; - LZ4F_preferences_t prefs = { { BSId, BMId, CCflag, 0,0,0 }, 0,autoflush, 0,0,0,0 }; + LZ4F_preferences_t prefs = { 0 }; + LZ4F_compressOptions_t options = { 0 }; unsigned nbBits = (FUZ_rand(&randState) % (FUZ_highbit(srcDataLength-1) - 1)) + 1; size_t srcSize = (FUZ_rand(&randState) & ((1< (size_t)(iend-ip)) iSize = iend-ip; - result = LZ4F_compressUpdate(cCtx, op, oSize, ip, iSize, NULL); + options.stableSrc = ((FUZ_rand(&randState) && 3) == 2); + + result = LZ4F_compressUpdate(cCtx, op, oSize, ip, iSize, &options); CHECK(LZ4F_isError(result), "Compression failed (error %i)", (int)result); op += result; ip += iSize; + if (forceFlush) { - result = LZ4F_flush(cCtx, op, oend-op, NULL); + result = LZ4F_flush(cCtx, op, oend-op, &options); CHECK(LZ4F_isError(result), "Compression failed (error %i)", (int)result); op += result; } } - result = LZ4F_compressEnd(cCtx, op, oend-op, NULL); + result = LZ4F_compressEnd(cCtx, op, oend-op, &options); CHECK(LZ4F_isError(result), "Compression completion failed (error %i)", (int)result); op += result; cSize = op-(BYTE*)compressedBuffer; -- cgit v0.12 From 658ab6cfca598b65b017e1ab784703add927ae0d Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sat, 13 Sep 2014 12:15:54 +0100 Subject: LZ4F_decompressFrame : implemented srcSizeHint as function result --- lz4frame.c | 208 ++++++++++++++++++++++++++++++--------------------- lz4frame.h | 18 +++-- programs/frametest.c | 2 +- 3 files changed, 134 insertions(+), 94 deletions(-) diff --git a/lz4frame.c b/lz4frame.c index 7290972..1be6cff 100644 --- a/lz4frame.c +++ b/lz4frame.c @@ -131,7 +131,6 @@ typedef struct { unsigned dStage; size_t maxBlockSize; size_t maxBufferSize; - size_t sizeToDecode; const BYTE* srcExpect; BYTE* tmpIn; size_t tmpInSize; @@ -740,7 +739,7 @@ LZ4F_errorCode_t LZ4F_getFrameInfo(LZ4F_decompressionContext_t decompressionCont { LZ4F_dctx_internal_t* dctxPtr = (LZ4F_dctx_internal_t*)decompressionContext; - if (dctxPtr->dStage==0) + if (dctxPtr->dStage == dstage_getHeader) { LZ4F_errorCode_t errorCode = LZ4F_decodeHeader(dctxPtr, srcBuffer, *srcSizePtr); if (LZ4F_isError(errorCode)) return errorCode; @@ -780,35 +779,41 @@ static int LZ4F_decompress_safe (const char* source, char* dest, int compressedS /* LZ4F_decompress() * Call this function repetitively to regenerate data compressed within srcBuffer. - * The function will attempt to decode *srcSize from srcBuffer, into dstBuffer of maximum size *dstSize. + * The function will attempt to decode *srcSizePtr from srcBuffer, into dstBuffer of maximum size *dstSizePtr. * - * The number of bytes generated into dstBuffer will be provided within *dstSize (necessarily <= original value). + * The number of bytes regenerated into dstBuffer will be provided within *dstSizePtr (necessarily <= original value). * - * The number of bytes effectively read from srcBuffer will be provided within *srcSize (necessarily <= original value). + * The number of bytes effectively read from srcBuffer will be provided within *srcSizePtr (necessarily <= original value). * If the number of bytes read is < number of bytes provided, then the decompression operation is not complete. - * You will have to call it again, using the same src arguments (but eventually different dst arguments). + * You will have to call it again, continuing from where it stopped. * - * The function result is an error code which can be tested using LZ4F_isError(). - * When the frame is fully decoded, the function result will be OK_FrameEnd(=1). + * The function result is an hint of the better srcSize to use for next call to LZ4F_decompress. + * Basically, it's the size of the current (or remaining) compressed block + header of next block. + * Respecting the hint provides some boost to performance, since it allows less buffer shuffling. + * Note that this is just a hint, you can always provide any srcSize you want. + * When a frame is fully decoded, the function result will be 0. + * If decompression failed, function result is an error code which can be tested using LZ4F_isError(). */ -LZ4F_errorCode_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContext, void* dstBuffer, size_t* dstSize, const void* srcBuffer, size_t* srcSize, const LZ4F_decompressOptions_t* decompressOptionsPtr) +size_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContext, + void* dstBuffer, size_t* dstSizePtr, + const void* srcBuffer, size_t* srcSizePtr, + const LZ4F_decompressOptions_t* decompressOptionsPtr) { LZ4F_dctx_internal_t* dctxPtr = (LZ4F_dctx_internal_t*)decompressionContext; LZ4F_decompressOptions_t optionsNull = { 0 }; const BYTE* const srcStart = (const BYTE*)srcBuffer; - const BYTE* const srcEnd = srcStart + *srcSize; + const BYTE* const srcEnd = srcStart + *srcSizePtr; const BYTE* srcPtr = srcStart; BYTE* const dstStart = (BYTE*)dstBuffer; - BYTE* const dstEnd = dstStart + *dstSize; + BYTE* const dstEnd = dstStart + *dstSizePtr; BYTE* dstPtr = dstStart; - size_t nextCBlockSize=0; const BYTE* selectedIn=NULL; - LZ4F_errorCode_t goodResult = OK_NoError; - int (*decoder)(const char*, char*, int, int, const char*, int); + unsigned doAnotherStage = 1; + size_t nextSrcSizeHint = 1; if (decompressOptionsPtr==NULL) decompressOptionsPtr = &optionsNull; - *srcSize = 0; *dstSize = 0; + *srcSizePtr = 0; *dstSizePtr = 0; /* expect to continue decoding src buffer where it left previously */ if (dctxPtr->srcExpect != NULL) @@ -816,10 +821,14 @@ LZ4F_errorCode_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContex if (srcStart != dctxPtr->srcExpect) return -ERROR_GENERIC; } - while (srcPtr < srcEnd) + /* programmed as a state machine */ + + while (doAnotherStage) { + switch(dctxPtr->dStage) { + case dstage_getHeader: { if (srcEnd-srcPtr >= 7) @@ -827,12 +836,13 @@ LZ4F_errorCode_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContex selectedIn = srcPtr; srcPtr += 7; dctxPtr->dStage = dstage_decodeHeader; - goto goto_decodeHeader; /* break would risk leaving the while loop */ + break; } dctxPtr->tmpInSize = 0; dctxPtr->dStage = dstage_storeHeader; - /* break; break is useles, since storeHeader follows */ + break; } + case dstage_storeHeader: { size_t sizeToCopy = 7 - dctxPtr->tmpInSize; @@ -840,18 +850,25 @@ LZ4F_errorCode_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContex memcpy(dctxPtr->header + dctxPtr->tmpInSize, srcPtr, sizeToCopy); dctxPtr->tmpInSize += sizeToCopy; srcPtr += sizeToCopy; - if (dctxPtr->tmpInSize < 7) break; /* src completed; come back later for more */ + if (dctxPtr->tmpInSize < 7) + { + nextSrcSizeHint = (7 - dctxPtr->tmpInSize) + 4; + doAnotherStage = 0; /* no enough src, wait to get some more */ + break; + } selectedIn = dctxPtr->header; dctxPtr->dStage = dstage_decodeHeader; - /* break; useless because it follows */ + break; } + case dstage_decodeHeader: -goto_decodeHeader: { LZ4F_errorCode_t errorCode = LZ4F_decodeHeader(dctxPtr, selectedIn, 7); if (LZ4F_isError(errorCode)) return errorCode; - /* dctxPtr->dStage = dstage_getCBlockSize; break; no need to change stage nor break : dstage_getCBlockSize is next stage, and stage will be modified */ + dctxPtr->dStage = dstage_getCBlockSize; + break; } + case dstage_getCBlockSize: { if ((srcEnd - srcPtr) >= 4) @@ -859,13 +876,14 @@ goto_decodeHeader: selectedIn = srcPtr; srcPtr += 4; dctxPtr->dStage = dstage_decodeCBlockSize; - goto goto_decodeCBlockSize; /* required : a break could leave while loop */ + break; } - /* not enough input to read cBlockSize */ + /* not enough input to read cBlockSize field */ dctxPtr->tmpInSize = 0; dctxPtr->dStage = dstage_storeCBlockSize; - /* break; No need to break : dstage_storeCBlockSize is next block */ + break; } + case dstage_storeCBlockSize: { size_t sizeToCopy = 4 - dctxPtr->tmpInSize; @@ -873,64 +891,71 @@ goto_decodeHeader: memcpy(dctxPtr->tmpIn + dctxPtr->tmpInSize, srcPtr, sizeToCopy); srcPtr += sizeToCopy; dctxPtr->tmpInSize += sizeToCopy; - if (dctxPtr->tmpInSize < 4) break; /* not enough input to read CBlockSize */ + if (dctxPtr->tmpInSize < 4) /* not enough input to get full cBlockSize; wait for more */ + { + nextSrcSizeHint = 4 - dctxPtr->tmpInSize; + doAnotherStage=0; + break; + } selectedIn = dctxPtr->tmpIn; dctxPtr->dStage = dstage_decodeCBlockSize; - /* break; No need to break : dstage_decodeCBlockSize is next block */ + break; } + case dstage_decodeCBlockSize: -goto_decodeCBlockSize: { - nextCBlockSize = LZ4F_readLE32(selectedIn) & 0x7FFFFFFFU; - if (nextCBlockSize==0) /* no more CBlock */ + size_t nextCBlockSize = LZ4F_readLE32(selectedIn) & 0x7FFFFFFFU; + if (nextCBlockSize==0) /* frameEnd signal, no more CBlock */ { dctxPtr->dStage = dstage_getSuffix; - goto goto_getSuffix; /* required : a break could leave the while loop */ + break; } - if (nextCBlockSize > dctxPtr->maxBlockSize) return -ERROR_GENERIC; - dctxPtr->sizeToDecode = nextCBlockSize; - if (LZ4F_readLE32(selectedIn) & 0x80000000U) /* uncompressed flag */ + if (nextCBlockSize > dctxPtr->maxBlockSize) return -ERROR_GENERIC; /* invalid cBlockSize */ + dctxPtr->tmpInTarget = nextCBlockSize; + if (LZ4F_readLE32(selectedIn) & LZ4F_BLOCKUNCOMPRESSED_FLAG) { dctxPtr->dStage = dstage_copyDirect; break; } dctxPtr->dStage = dstage_getCBlock; - goto goto_getCBlock; /* break risk leaving while loop */ + break; } - case dstage_copyDirect: + + case dstage_copyDirect: /* uncompressed block */ { - size_t sizeToCopy = dctxPtr->sizeToDecode; - if ((size_t)(srcEnd-srcPtr) < sizeToCopy) sizeToCopy = srcEnd-srcPtr; /* not enough input to read full block */ + size_t sizeToCopy = dctxPtr->tmpInTarget; + if ((size_t)(srcEnd-srcPtr) < sizeToCopy) sizeToCopy = srcEnd - srcPtr; /* not enough input to read full block */ if ((size_t)(dstEnd-dstPtr) < sizeToCopy) sizeToCopy = dstEnd - dstPtr; memcpy(dstPtr, srcPtr, sizeToCopy); if (dctxPtr->frameInfo.contentChecksumFlag) XXH32_update(&(dctxPtr->xxh), srcPtr, sizeToCopy); - if (dctxPtr->frameInfo.blockMode==blockLinked) - LZ4F_saveDict(dctxPtr, srcPtr, sizeToCopy); + if (dctxPtr->frameInfo.blockMode==blockLinked) LZ4F_saveDict(dctxPtr, srcPtr, sizeToCopy); srcPtr += sizeToCopy; dstPtr += sizeToCopy; - if (sizeToCopy == dctxPtr->sizeToDecode) /* all copied */ + if (sizeToCopy == dctxPtr->tmpInTarget) /* all copied */ { dctxPtr->dStage = dstage_getCBlockSize; break; } - dctxPtr->sizeToDecode -= sizeToCopy; /* still need to copy more */ - goto _end; /* either In or Out have reached end */ + dctxPtr->tmpInTarget -= sizeToCopy; /* still need to copy more */ + nextSrcSizeHint = dctxPtr->tmpInTarget + 4; + doAnotherStage = 0; + break; } - case dstage_getCBlock: -goto_getCBlock: + + case dstage_getCBlock: /* entry from dstage_decodeCBlockSize */ { - if ((size_t)(srcEnd-srcPtr) < nextCBlockSize) + if ((size_t)(srcEnd-srcPtr) < dctxPtr->tmpInTarget) { - dctxPtr->tmpInTarget = nextCBlockSize; dctxPtr->tmpInSize = 0; dctxPtr->dStage = dstage_storeCBlock; break; } selectedIn = srcPtr; - srcPtr += nextCBlockSize; + srcPtr += dctxPtr->tmpInTarget; dctxPtr->dStage = dstage_decodeCBlock; - goto goto_decodeCBlock; /* break risks leaving the while loop */ + break; } + case dstage_storeCBlock: { size_t sizeToCopy = dctxPtr->tmpInTarget - dctxPtr->tmpInSize; @@ -938,74 +963,86 @@ goto_getCBlock: memcpy(dctxPtr->tmpIn + dctxPtr->tmpInSize, srcPtr, sizeToCopy); dctxPtr->tmpInSize += sizeToCopy; srcPtr += sizeToCopy; - if (dctxPtr->tmpInSize < dctxPtr->tmpInTarget) break; /* need to read more */ + if (dctxPtr->tmpInSize < dctxPtr->tmpInTarget) /* need more input */ + { + nextSrcSizeHint = (dctxPtr->tmpInTarget - dctxPtr->tmpInSize) + 4; + doAnotherStage=0; + break; + } selectedIn = dctxPtr->tmpIn; dctxPtr->dStage = dstage_decodeCBlock; - /* break; break unnecessary because it follows */ + break; } + case dstage_decodeCBlock: -goto_decodeCBlock: { + int (*decoder)(const char*, char*, int, int, const char*, int); int decodedSize; + if (dctxPtr->frameInfo.blockMode == blockLinked) decoder = LZ4_decompress_safe_usingDict; else decoder = LZ4F_decompress_safe; - if ((size_t)(dstEnd-dstPtr) < dctxPtr->maxBlockSize) /* not enough room : decode into tmpOut */ + + if ((size_t)(dstEnd-dstPtr) < dctxPtr->maxBlockSize) /* not enough dst room : decode into tmpOut */ { - decodedSize = decoder((const char*)selectedIn, (char*)dctxPtr->tmpOut, (int)dctxPtr->sizeToDecode, (int)dctxPtr->maxBlockSize, (const char*)dctxPtr->dict, (int)dctxPtr->dictSize); + decodedSize = decoder((const char*)selectedIn, (char*)dctxPtr->tmpOut, (int)dctxPtr->tmpInTarget, (int)dctxPtr->maxBlockSize, (const char*)dctxPtr->dict, (int)dctxPtr->dictSize); if (decodedSize < 0) return -ERROR_GENERIC; /* decompression failed */ - if (dctxPtr->frameInfo.contentChecksumFlag) - XXH32_update(&(dctxPtr->xxh), dctxPtr->tmpOut, decodedSize); - if (dctxPtr->frameInfo.blockMode==blockLinked) - LZ4F_saveDict(dctxPtr, dctxPtr->tmpOut, decodedSize); + if (dctxPtr->frameInfo.contentChecksumFlag) XXH32_update(&(dctxPtr->xxh), dctxPtr->tmpOut, decodedSize); + if (dctxPtr->frameInfo.blockMode==blockLinked) LZ4F_saveDict(dctxPtr, dctxPtr->tmpOut, decodedSize); dctxPtr->tmpOutSize = decodedSize; dctxPtr->tmpOutStart = 0; dctxPtr->dStage = dstage_flushOut; break; } - decodedSize = decoder((const char*)selectedIn, (char*)dstPtr, (int)dctxPtr->sizeToDecode, (int)dctxPtr->maxBlockSize, (const char*)dctxPtr->dict, (int)dctxPtr->dictSize); + decodedSize = decoder((const char*)selectedIn, (char*)dstPtr, (int)dctxPtr->tmpInTarget, (int)dctxPtr->maxBlockSize, (const char*)dctxPtr->dict, (int)dctxPtr->dictSize); if (decodedSize < 0) return -ERROR_GENERIC; /* decompression failed */ - if (dctxPtr->frameInfo.contentChecksumFlag) - XXH32_update(&(dctxPtr->xxh), dstPtr, decodedSize); - if (dctxPtr->frameInfo.blockMode==blockLinked) - LZ4F_saveDict(dctxPtr, dstPtr, decodedSize); + if (dctxPtr->frameInfo.contentChecksumFlag) XXH32_update(&(dctxPtr->xxh), dstPtr, decodedSize); + if (dctxPtr->frameInfo.blockMode==blockLinked) LZ4F_saveDict(dctxPtr, dstPtr, decodedSize); dstPtr += decodedSize; dctxPtr->dStage = dstage_getCBlockSize; break; } - case dstage_flushOut: + + case dstage_flushOut: /* flush decoded data from tmpOut to dstBuffer */ { size_t sizeToCopy = dctxPtr->tmpOutSize - dctxPtr->tmpOutStart; if (sizeToCopy > (size_t)(dstEnd-dstPtr)) sizeToCopy = dstEnd-dstPtr; memcpy(dstPtr, dctxPtr->tmpOut + dctxPtr->tmpOutStart, sizeToCopy); dctxPtr->tmpOutStart += sizeToCopy; dstPtr += sizeToCopy; - if (dctxPtr->tmpOutStart < dctxPtr->tmpOutSize) goto _end; /* need to write more */ - dctxPtr->dStage = dstage_getCBlockSize; + if (dctxPtr->tmpOutStart == dctxPtr->tmpOutSize) + { + dctxPtr->dStage = dstage_getCBlockSize; + break; + } + nextSrcSizeHint = 4; + doAnotherStage = 0; /* still some data to flush */ break; } + case dstage_getSuffix: -goto_getSuffix: { size_t suffixSize = dctxPtr->frameInfo.contentChecksumFlag * 4; if (suffixSize == 0) /* frame completed */ { - goodResult = OK_FrameEnd; + nextSrcSizeHint = 0; dctxPtr->dStage = dstage_getHeader; - goto _end; + doAnotherStage = 0; + break; } if ((srcEnd - srcPtr) >= 4) /* CRC present */ { selectedIn = srcPtr; srcPtr += 4; dctxPtr->dStage = dstage_checkSuffix; - goto goto_checkSuffix; /* break risks leaving the while loop */ + break; } dctxPtr->tmpInSize = 0; dctxPtr->dStage = dstage_storeSuffix; - /* break; useless, it follows */ + break; } + case dstage_storeSuffix: { size_t sizeToCopy = 4 - dctxPtr->tmpInSize; @@ -1013,37 +1050,36 @@ goto_getSuffix: memcpy(dctxPtr->tmpIn + dctxPtr->tmpInSize, srcPtr, sizeToCopy); srcPtr += sizeToCopy; dctxPtr->tmpInSize += sizeToCopy; - if (dctxPtr->tmpInSize < 4) break; /* not enough input to read suffix */ + if (dctxPtr->tmpInSize < 4) /* not enough input to read complete suffix */ + { + nextSrcSizeHint = 4 - dctxPtr->tmpInSize; + doAnotherStage=0; + break; + } selectedIn = dctxPtr->tmpIn; dctxPtr->dStage = dstage_checkSuffix; - /* break; useless, it follows; would need a goto anyway */ + break; } + case dstage_checkSuffix: -goto_checkSuffix: { U32 readCRC = LZ4F_readLE32(selectedIn); U32 resultCRC = XXH32_intermediateDigest(&(dctxPtr->xxh)); if (readCRC != resultCRC) return -ERROR_checksum_invalid; - goodResult = OK_FrameEnd; + nextSrcSizeHint = 0; dctxPtr->dStage = dstage_getHeader; - goto _end; + doAnotherStage = 0; + break; } } } - /* input fully read */ - -_end: if (srcPtrsrcExpect = srcPtr; - } else - { dctxPtr->srcExpect = NULL; - } - *srcSize = (srcPtr - srcStart); - *dstSize = (dstPtr - dstStart); - return goodResult; + *srcSizePtr = (srcPtr - srcStart); + *dstSizePtr = (dstPtr - dstStart); + return nextSrcSizeHint; } diff --git a/lz4frame.h b/lz4frame.h index d75d462..4c8dd86 100644 --- a/lz4frame.h +++ b/lz4frame.h @@ -210,13 +210,13 @@ LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_compressionContext_t LZ4F_de * The first thing to do is to create a decompressionContext object, which will be used in all decompression operations. * This is achieved using LZ4F_createDecompressionContext(). * The function will provide a pointer to a fully allocated and initialized LZ4F_decompressionContext object. - * If the result LZ4F_errorCode_t is not zero, there was an error during context creation. + * If the result LZ4F_errorCode_t is not OK_NoError, there was an error during context creation. * Object can release its memory using LZ4F_freeDecompressionContext(); */ /* Decompression */ -LZ4F_errorCode_t LZ4F_getFrameInfo(LZ4F_decompressionContext_t decompressionContext, LZ4F_frameInfo_t* frameInfoPtr, const void* srcBuffer, size_t* srcSizePtr); +size_t LZ4F_getFrameInfo(LZ4F_decompressionContext_t decompressionContext, LZ4F_frameInfo_t* frameInfoPtr, const void* srcBuffer, size_t* srcSizePtr); /* LZ4F_getFrameInfo() * This function decodes frame header information, such as blockSize. * It is optional : you could start by calling directly LZ4F_decompress() instead. @@ -226,12 +226,12 @@ LZ4F_errorCode_t LZ4F_getFrameInfo(LZ4F_decompressionContext_t decompressionCont * The function result is an error code which can be tested using LZ4F_isError(). */ -LZ4F_errorCode_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContext, void* dstBuffer, size_t* dstSizePtr, const void* srcBuffer, size_t* srcSizePtr, const LZ4F_decompressOptions_t* decompressOptionsPtr); +size_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContext, void* dstBuffer, size_t* dstSizePtr, const void* srcBuffer, size_t* srcSizePtr, const LZ4F_decompressOptions_t* decompressOptionsPtr); /* LZ4F_decompress() * Call this function repetitively to regenerate data compressed within srcBuffer. * The function will attempt to decode *srcSizePtr bytes from srcBuffer, into dstBuffer of maximum size *dstSizePtr. * - * The number of bytes generated into dstBuffer will be provided within *dstSizePtr (necessarily <= original value). + * The number of bytes regenerated into dstBuffer will be provided within *dstSizePtr (necessarily <= original value). * * The number of bytes effectively used from srcBuffer will be provided within *srcSizePtr (necessarily <= original value). * If the number of bytes read is < number of bytes provided, then the decompression operation is not complete. @@ -239,10 +239,14 @@ LZ4F_errorCode_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContex * LZ4F_decompress() will have to be called again, starting from where it stopped (srcBuffer + *srcSizePtr) * The function will check this condition, and refuse to continue if it is not respected. * dstBuffer is supposed to be flushed between calls to the function, since its content will be rewritten. - * Different dst arguments can be used. + * Different dst arguments can be used between each calls. * - * The function result is an error code which can be tested using LZ4F_isError(). - * When a frame is fully decoded, the function result will be OK_FrameEnd(=1). + * The function result is an hint of the better srcSize to use for next call to LZ4F_decompress. + * Basically, it's the size of the current (or remaining) compressed block + header of next block. + * Respecting the hint provides some boost to performance, since it allows less buffer shuffling. + * Note that this is just a hint, you can always provide any srcSize you want. + * When a frame is fully decoded, the function result will be 0. + * If decompression failed, function result is an error code which can be tested using LZ4F_isError(). */ diff --git a/programs/frametest.c b/programs/frametest.c index 73d6373..caa4956 100644 --- a/programs/frametest.c +++ b/programs/frametest.c @@ -478,7 +478,7 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi op += oSize; ip += iSize; } - CHECK(result != OK_FrameEnd, "Frame decompression failed (error %i)", (int)result); + CHECK(result != 0, "Frame decompression failed (error %i)", (int)result); crcDecoded = XXH64(decodedBuffer, op-(BYTE*)decodedBuffer, 1); CHECK(crcDecoded != crcOrig, "Decompression corruption"); } -- cgit v0.12 From 6bede08aa42acd3391bb3a2bee4a0fded9363be5 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sat, 13 Sep 2014 14:40:35 +0100 Subject: LZ4F_getFrameInfo() : provides srcSize hint like LZ4F_decompress() --- lz4frame.c | 9 ++++++--- lz4frame.h | 4 +++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lz4frame.c b/lz4frame.c index 1be6cff..4996d28 100644 --- a/lz4frame.c +++ b/lz4frame.c @@ -733,7 +733,9 @@ typedef enum { dstage_getHeader=0, dstage_storeHeader, dstage_decodeHeader, * The objective is to extract header information without starting decompression, typically for allocation purposes. * LZ4F_getFrameInfo() can also be used *after* starting decompression, on a valid LZ4F_decompressionContext_t. * The number of bytes read from srcBuffer will be provided within *srcSizePtr (necessarily <= original value). - * The function result is an error code which can be tested using LZ4F_isError(). + * You are expected to resume decompression from where it stopped (srcBuffer + *srcSizePtr) + * The function result is an hint of the better srcSize to use for next call to LZ4F_decompress. + * or an error code which can be tested using LZ4F_isError(). */ LZ4F_errorCode_t LZ4F_getFrameInfo(LZ4F_decompressionContext_t decompressionContext, LZ4F_frameInfo_t* frameInfoPtr, const void* srcBuffer, size_t* srcSizePtr) { @@ -745,14 +747,15 @@ LZ4F_errorCode_t LZ4F_getFrameInfo(LZ4F_decompressionContext_t decompressionCont if (LZ4F_isError(errorCode)) return errorCode; *srcSizePtr = errorCode; *frameInfoPtr = dctxPtr->frameInfo; + dctxPtr->srcExpect = (BYTE*)srcBuffer + errorCode; dctxPtr->dStage = dstage_getCBlockSize; - return OK_NoError; + return 4; } /* frameInfo already decoded */ *srcSizePtr = 0; *frameInfoPtr = dctxPtr->frameInfo; - return OK_NoError; + return 0; } diff --git a/lz4frame.h b/lz4frame.h index 4c8dd86..40f5e3f 100644 --- a/lz4frame.h +++ b/lz4frame.h @@ -223,7 +223,9 @@ size_t LZ4F_getFrameInfo(LZ4F_decompressionContext_t decompressionContext, LZ4F_ * The objective is to extract header information without starting decompression, typically for allocation purposes. * LZ4F_getFrameInfo() can also be used *after* starting decompression, on a valid LZ4F_decompressionContext_t. * The number of bytes read from srcBuffer will be provided within *srcSizePtr (necessarily <= original value). - * The function result is an error code which can be tested using LZ4F_isError(). + * You are expected to resume decompression from where it stopped (srcBuffer + *srcSizePtr) + * The function result is an hint of the better srcSize to use for next call to LZ4F_decompress. + * or an error code which can be tested using LZ4F_isError(). */ size_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContext, void* dstBuffer, size_t* dstSizePtr, const void* srcBuffer, size_t* srcSizePtr, const LZ4F_decompressOptions_t* decompressOptionsPtr); -- cgit v0.12 From 38912f55e3c3b782529c2dd1e682d6af7c8bd052 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sat, 13 Sep 2014 15:24:16 +0100 Subject: fullbench : added benchmark for LZ4F_decompress() --- lz4frame.c | 2 +- lz4frame.h | 2 +- programs/fullbench.c | 95 +++++++++++++++++++++++++++++++++------------------- 3 files changed, 62 insertions(+), 37 deletions(-) diff --git a/lz4frame.c b/lz4frame.c index 4996d28..0b54840 100644 --- a/lz4frame.c +++ b/lz4frame.c @@ -734,7 +734,7 @@ typedef enum { dstage_getHeader=0, dstage_storeHeader, dstage_decodeHeader, * LZ4F_getFrameInfo() can also be used *after* starting decompression, on a valid LZ4F_decompressionContext_t. * The number of bytes read from srcBuffer will be provided within *srcSizePtr (necessarily <= original value). * You are expected to resume decompression from where it stopped (srcBuffer + *srcSizePtr) - * The function result is an hint of the better srcSize to use for next call to LZ4F_decompress. + * The function result is an hint of the better srcSize to use for next call to LZ4F_decompress, * or an error code which can be tested using LZ4F_isError(). */ LZ4F_errorCode_t LZ4F_getFrameInfo(LZ4F_decompressionContext_t decompressionContext, LZ4F_frameInfo_t* frameInfoPtr, const void* srcBuffer, size_t* srcSizePtr) diff --git a/lz4frame.h b/lz4frame.h index 40f5e3f..0769f6a 100644 --- a/lz4frame.h +++ b/lz4frame.h @@ -224,7 +224,7 @@ size_t LZ4F_getFrameInfo(LZ4F_decompressionContext_t decompressionContext, LZ4F_ * LZ4F_getFrameInfo() can also be used *after* starting decompression, on a valid LZ4F_decompressionContext_t. * The number of bytes read from srcBuffer will be provided within *srcSizePtr (necessarily <= original value). * You are expected to resume decompression from where it stopped (srcBuffer + *srcSizePtr) - * The function result is an hint of the better srcSize to use for next call to LZ4F_decompress. + * The function result is an hint of the better srcSize to use for next call to LZ4F_decompress, * or an error code which can be tested using LZ4F_isError(). */ diff --git a/programs/fullbench.c b/programs/fullbench.c index 794b5b2..f87b857 100644 --- a/programs/fullbench.c +++ b/programs/fullbench.c @@ -359,21 +359,37 @@ static int local_LZ4_decompress_safe_partial(const char* in, char* out, int inSi return LZ4_decompress_safe_partial(in, out, inSize, outSize - 5, outSize); } +static LZ4F_decompressionContext_t g_dCtx; + +static int local_LZ4F_decompress(const char* in, char* out, int inSize, int outSize) +{ + size_t srcSize = inSize; + size_t dstSize = outSize; + size_t result; + result = LZ4F_decompress(g_dCtx, out, &dstSize, in, &srcSize, NULL); + if (result!=0) { DISPLAY("Error decompressing frame : unfinished frame\n"); exit(8); } + if (srcSize != (size_t)inSize) { DISPLAY("Error decompressing frame : read size incorrect\n"); exit(9); } + return dstSize; +} + + int fullSpeedBench(char** fileNamesTable, int nbFiles) { int fileIdx=0; char* orig_buff; # define NB_COMPRESSION_ALGORITHMS 14 -# define MINCOMPRESSIONCHAR '0' double totalCTime[NB_COMPRESSION_ALGORITHMS+1] = {0}; double totalCSize[NB_COMPRESSION_ALGORITHMS+1] = {0}; -# define NB_DECOMPRESSION_ALGORITHMS 7 -# define MINDECOMPRESSIONCHAR '0' -# define MAXDECOMPRESSIONCHAR (MINDECOMPRESSIONCHAR + NB_DECOMPRESSION_ALGORITHMS) - static char* decompressionNames[] = { "LZ4_decompress_fast", "LZ4_decompress_fast_withPrefix64k", "LZ4_decompress_fast_usingDict", - "LZ4_decompress_safe", "LZ4_decompress_safe_withPrefix64k", "LZ4_decompress_safe_usingDict", "LZ4_decompress_safe_partial" }; +# define NB_DECOMPRESSION_ALGORITHMS 8 double totalDTime[NB_DECOMPRESSION_ALGORITHMS+1] = {0}; + size_t errorCode; + errorCode = LZ4F_createDecompressionContext(&g_dCtx, LZ4F_VERSION); + if (LZ4F_isError(errorCode)) + { + DISPLAY("dctx allocation issue \n"); + return 10; + } // Loop for each file while (fileIdx chunkSize) { chunkP[i].origSize = chunkSize; remaining -= chunkSize; } else { chunkP[i].origSize = (int)remaining; remaining = 0; } + chunkP[i].compressedBuffer = out; out += maxCompressedChunkSize; + chunkP[i].compressedSize = 0; + } + } + // Compression Algorithms for (cAlgNb=1; (cAlgNb <= NB_COMPRESSION_ALGORITHMS) && (compressionTest); cAlgNb++) { - char* compressorName; + const char* compressorName; int (*compressionFunction)(const char*, char*, int); void* (*initFunction)(const char*) = NULL; double bestTime = 100000000.; if ((compressionAlgo != ALL_COMPRESSORS) && (compressionAlgo != cAlgNb)) continue; - // Init chunks data - { - int i; - size_t remaining = benchedSize; - char* in = orig_buff; - char* out = compressed_buff; - nbChunks = (int) ((int)benchedSize / chunkSize) + 1; - for (i=0; i chunkSize) { chunkP[i].origSize = chunkSize; remaining -= chunkSize; } else { chunkP[i].origSize = (int)remaining; remaining = 0; } - chunkP[i].compressedBuffer = out; out += maxCompressedChunkSize; - chunkP[i].compressedSize = 0; - } - } - switch(cAlgNb) { case 1 : compressionFunction = LZ4_compress; compressorName = "LZ4_compress"; break; @@ -553,29 +569,38 @@ int fullSpeedBench(char** fileNamesTable, int nbFiles) chunkP[chunkNb].compressedSize = LZ4_compress(chunkP[chunkNb].origBuffer, chunkP[chunkNb].compressedBuffer, chunkP[chunkNb].origSize); if (chunkP[chunkNb].compressedSize==0) DISPLAY("ERROR ! %s() = 0 !! \n", "LZ4_compress"), exit(1); } - { size_t i; for (i=0; i Date: Sat, 13 Sep 2014 19:49:01 +0100 Subject: Frame decompression speed optimization --- lz4.c | 19 ++++++++++++++-- lz4frame.c | 64 ++++++++++++++++++++++++++++++++++++++++------------ programs/frametest.c | 9 ++++---- programs/fullbench.c | 14 ++++++++++-- 4 files changed, 83 insertions(+), 23 deletions(-) diff --git a/lz4.c b/lz4.c index 4e2026e..b39a91d 100644 --- a/lz4.c +++ b/lz4.c @@ -1153,14 +1153,29 @@ Advanced decoding functions : the dictionary must be explicitly provided within parameters */ +FORCE_INLINE int LZ4_decompress_usingDict_generic(const char* source, char* dest, int compressedSize, int maxOutputSize, int safe, const char* dictStart, int dictSize) +{ + if ((dictStart+dictSize == source) && (dictSize >= (int)(64 KB - 1))) + return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, safe, full, 0, withPrefix64k, NULL, 64 KB); + return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, safe, full, 0, usingExtDict, dictStart, dictSize); +} + int LZ4_decompress_safe_usingDict(const char* source, char* dest, int compressedSize, int maxOutputSize, const char* dictStart, int dictSize) { - return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, endOnInputSize, full, 0, usingExtDict, dictStart, dictSize); + //return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, endOnInputSize, full, 0, usingExtDict, dictStart, dictSize); + return LZ4_decompress_usingDict_generic(source, dest, compressedSize, maxOutputSize, 1, dictStart, dictSize); } int LZ4_decompress_fast_usingDict(const char* source, char* dest, int originalSize, const char* dictStart, int dictSize) { - return LZ4_decompress_generic(source, dest, 0, originalSize, endOnOutputSize, full, 0, usingExtDict, dictStart, dictSize); + //return LZ4_decompress_generic(source, dest, 0, originalSize, endOnOutputSize, full, 0, usingExtDict, dictStart, dictSize); + return LZ4_decompress_usingDict_generic(source, dest, 0, originalSize, 0, dictStart, dictSize); +} + +/* debug function */ +int LZ4_decompress_safe_forceExtDict(const char* source, char* dest, int compressedSize, int maxOutputSize, const char* dictStart, int dictSize) +{ + return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, endOnInputSize, full, 0, usingExtDict, dictStart, dictSize); } diff --git a/lz4frame.c b/lz4frame.c index 0b54840..390af0e 100644 --- a/lz4frame.c +++ b/lz4frame.c @@ -407,7 +407,6 @@ size_t LZ4F_compressUpdate(LZ4F_compressionContext_t compressionContext, void* d int (*compress)(void*, const char*, char*, int, int); - if (cctxPtr->cStage != 1) return -ERROR_GENERIC; if (dstMaxSize < LZ4F_compressBound(srcSize, &(cctxPtr->prefs))) return -ERROR_dstMaxSize_tooSmall; if (compressOptionsPtr == NULL) compressOptionsPtr = &cOptionsNull; @@ -681,7 +680,6 @@ static size_t LZ4F_decodeHeader(LZ4F_dctx_internal_t* dctxPtr, const BYTE* srcPt /* validate */ if (version != 1) return -ERROR_GENERIC; /* Version Number, only supported value */ - //if (blockMode != blockIndependent) return -ERROR_GENERIC; /* Only supported blockMode for the time being */ if (blockChecksumFlag != 0) return -ERROR_GENERIC; /* Only supported value for the time being */ if (contentSizeFlag != 0) return -ERROR_GENERIC; /* Only supported value for the time being */ if (((FLG>>1)&_1BIT) != 0) return -ERROR_GENERIC; /* Reserved bit */ @@ -700,7 +698,7 @@ static size_t LZ4F_decodeHeader(LZ4F_dctx_internal_t* dctxPtr, const BYTE* srcPt if (contentChecksumFlag) XXH32_resetState(&(dctxPtr->xxh), 0); /* alloc */ - bufferNeeded = dctxPtr->maxBlockSize + ((dctxPtr->frameInfo.blockMode==blockLinked) * 64 KB); + bufferNeeded = dctxPtr->maxBlockSize + ((dctxPtr->frameInfo.blockMode==blockLinked) * 128 KB); if (bufferNeeded > dctxPtr->maxBufferSize) /* tmp buffers too small */ { FREEMEM(dctxPtr->tmpIn); @@ -711,10 +709,9 @@ static size_t LZ4F_decodeHeader(LZ4F_dctx_internal_t* dctxPtr, const BYTE* srcPt dctxPtr->tmpOutBuffer= ALLOCATOR(dctxPtr->maxBufferSize); if (dctxPtr->tmpOutBuffer== NULL) return -ERROR_GENERIC; } - dctxPtr->tmpOut = dctxPtr->tmpOutBuffer; - if (dctxPtr->frameInfo.blockMode==blockLinked) dctxPtr->tmpOut += 64 KB; + dctxPtr->dict = dctxPtr->tmpOutBuffer; dctxPtr->dictSize = 0; - dctxPtr->dict = dctxPtr->tmpOut; + dctxPtr->tmpOut = dctxPtr->tmpOutBuffer; return 7; } @@ -765,11 +762,46 @@ static void LZ4F_saveDict(LZ4F_dctx_internal_t* dctxPtr, const BYTE* decoded, si size_t preserveDictSize; if (newDictSize > 64 KB) newDictSize = 64 KB; preserveDictSize = 64 KB - newDictSize; - memmove(dctxPtr->tmpOutBuffer, dctxPtr->tmpOutBuffer + newDictSize, preserveDictSize); - memcpy(dctxPtr->tmpOutBuffer + preserveDictSize, decoded + decodedSize - newDictSize, newDictSize); - dctxPtr->dictSize += newDictSize; - if (dctxPtr->dictSize > 64 KB) dctxPtr->dictSize = 64 KB; - dctxPtr->dict = dctxPtr->tmpOut - dctxPtr->dictSize; + if (preserveDictSize > dctxPtr->dictSize) preserveDictSize = dctxPtr->dictSize; + + memmove(dctxPtr->tmpOutBuffer, dctxPtr->dict + dctxPtr->dictSize - preserveDictSize, preserveDictSize); + memmove(dctxPtr->tmpOutBuffer + preserveDictSize, decoded + decodedSize - newDictSize, newDictSize); + + dctxPtr->dict = dctxPtr->tmpOutBuffer; + dctxPtr->dictSize = preserveDictSize + newDictSize; + dctxPtr->tmpOut = dctxPtr->tmpOutBuffer + dctxPtr->dictSize; +} + + +static void LZ4F_pointDict(LZ4F_dctx_internal_t* dctxPtr, const BYTE* decoded, size_t decodedSize) +{ + /* large decoded block */ + if (decodedSize >= (64 KB - 1)) + { + dctxPtr->dict = (BYTE*)decoded; + dctxPtr->dictSize = decodedSize; + dctxPtr->tmpOut = dctxPtr->tmpOutBuffer + 64 KB; + return; + } + + /* decoded block in the continuity of dictionary */ + if (dctxPtr->dict + dctxPtr->dictSize == decoded) + { + dctxPtr->dictSize += decodedSize; + if (dctxPtr->dict == dctxPtr->tmpOutBuffer) /* extended tmp buffer, don't go beyond 128 KB == maxDictSize */ + { + if (dctxPtr->dictSize > 128 KB) + { + memcpy(dctxPtr->tmpOutBuffer, dctxPtr->tmpOutBuffer + dctxPtr->dictSize - 64 KB, 64 KB); + dctxPtr->dictSize = 64 KB; + } + dctxPtr->tmpOut = dctxPtr->tmpOutBuffer + dctxPtr->dictSize; + } + return; + } + + /* small block, and not contiguous : let's save that */ + LZ4F_saveDict(dctxPtr, decoded, decodedSize); } @@ -931,7 +963,7 @@ size_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContext, if ((size_t)(dstEnd-dstPtr) < sizeToCopy) sizeToCopy = dstEnd - dstPtr; memcpy(dstPtr, srcPtr, sizeToCopy); if (dctxPtr->frameInfo.contentChecksumFlag) XXH32_update(&(dctxPtr->xxh), srcPtr, sizeToCopy); - if (dctxPtr->frameInfo.blockMode==blockLinked) LZ4F_saveDict(dctxPtr, srcPtr, sizeToCopy); + if (dctxPtr->frameInfo.blockMode==blockLinked) LZ4F_pointDict(dctxPtr, srcPtr, sizeToCopy); srcPtr += sizeToCopy; dstPtr += sizeToCopy; if (sizeToCopy == dctxPtr->tmpInTarget) /* all copied */ @@ -987,12 +1019,11 @@ size_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContext, else decoder = LZ4F_decompress_safe; - if ((size_t)(dstEnd-dstPtr) < dctxPtr->maxBlockSize) /* not enough dst room : decode into tmpOut */ + if ((size_t)(dstEnd-dstPtr) < dctxPtr->maxBlockSize) /* not enough place into dst : decode into tmpOut */ { decodedSize = decoder((const char*)selectedIn, (char*)dctxPtr->tmpOut, (int)dctxPtr->tmpInTarget, (int)dctxPtr->maxBlockSize, (const char*)dctxPtr->dict, (int)dctxPtr->dictSize); if (decodedSize < 0) return -ERROR_GENERIC; /* decompression failed */ if (dctxPtr->frameInfo.contentChecksumFlag) XXH32_update(&(dctxPtr->xxh), dctxPtr->tmpOut, decodedSize); - if (dctxPtr->frameInfo.blockMode==blockLinked) LZ4F_saveDict(dctxPtr, dctxPtr->tmpOut, decodedSize); dctxPtr->tmpOutSize = decodedSize; dctxPtr->tmpOutStart = 0; dctxPtr->dStage = dstage_flushOut; @@ -1001,7 +1032,7 @@ size_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContext, decodedSize = decoder((const char*)selectedIn, (char*)dstPtr, (int)dctxPtr->tmpInTarget, (int)dctxPtr->maxBlockSize, (const char*)dctxPtr->dict, (int)dctxPtr->dictSize); if (decodedSize < 0) return -ERROR_GENERIC; /* decompression failed */ if (dctxPtr->frameInfo.contentChecksumFlag) XXH32_update(&(dctxPtr->xxh), dstPtr, decodedSize); - if (dctxPtr->frameInfo.blockMode==blockLinked) LZ4F_saveDict(dctxPtr, dstPtr, decodedSize); + if (dctxPtr->frameInfo.blockMode==blockLinked) LZ4F_pointDict(dctxPtr, dstPtr, decodedSize); dstPtr += decodedSize; dctxPtr->dStage = dstage_getCBlockSize; break; @@ -1016,6 +1047,7 @@ size_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContext, dstPtr += sizeToCopy; if (dctxPtr->tmpOutStart == dctxPtr->tmpOutSize) { + if (dctxPtr->frameInfo.blockMode==blockLinked) LZ4F_pointDict(dctxPtr, dctxPtr->tmpOut, dctxPtr->tmpOutSize); dctxPtr->dStage = dstage_getCBlockSize; break; } @@ -1077,6 +1109,8 @@ size_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContext, } } + if ((dctxPtr->frameInfo.blockMode==blockLinked) && (dctxPtr->dict != dctxPtr->tmpOutBuffer)) + LZ4F_saveDict(dctxPtr, NULL, 0); if (srcPtrsrcExpect = srcPtr; diff --git a/programs/frametest.c b/programs/frametest.c index caa4956..8be7752 100644 --- a/programs/frametest.c +++ b/programs/frametest.c @@ -355,13 +355,13 @@ _output_error: } -static void locateBuffDiff(const void* buff1, const void* buff2) +static void locateBuffDiff(const void* buff1, const void* buff2, size_t size) { int p=0; BYTE* b1=(BYTE*)buff1; BYTE* b2=(BYTE*)buff2; while (b1[p]==b2[p]) p++; - printf("Error at pos %i : %02X != %02X \n", p, b1[p], b2[p]); + printf("Error at pos %i/%i : %02X != %02X \n", p, (int)size, b1[p], b2[p]); } @@ -473,13 +473,14 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi if (oSize > (size_t)(oend-op)) oSize = oend-op; oSize = oend-op; result = LZ4F_decompress(dCtx, op, &oSize, ip, &iSize, NULL); - if (result == (size_t)-ERROR_checksum_invalid) locateBuffDiff((BYTE*)srcBuffer+srcStart, decodedBuffer); + if (result == (size_t)-ERROR_checksum_invalid) locateBuffDiff((BYTE*)srcBuffer+srcStart, decodedBuffer, srcSize); CHECK(LZ4F_isError(result), "Decompression failed (error %i)", (int)result); op += oSize; ip += iSize; } CHECK(result != 0, "Frame decompression failed (error %i)", (int)result); crcDecoded = XXH64(decodedBuffer, op-(BYTE*)decodedBuffer, 1); + if (crcDecoded != crcOrig) locateBuffDiff((BYTE*)srcBuffer+srcStart, decodedBuffer, srcSize); CHECK(crcDecoded != crcOrig, "Decompression corruption"); } @@ -613,7 +614,7 @@ int main(int argc, char** argv) if (nbTests<=0) nbTests=1; - if (testNb==0) result = basicTests(seed, ((double)proba) / 100); + //if (testNb==0) result = basicTests(seed, ((double)proba) / 100); if (result) return 1; return fuzzerTests(seed, nbTests, testNb, ((double)proba) / 100); } diff --git a/programs/fullbench.c b/programs/fullbench.c index f87b857..9292f20 100644 --- a/programs/fullbench.c +++ b/programs/fullbench.c @@ -354,6 +354,15 @@ static int local_LZ4_decompress_safe_usingDict(const char* in, char* out, int in return outSize; } +extern int LZ4_decompress_safe_forceExtDict(const char* in, char* out, int inSize, int outSize, const char* dict, int dictSize); + +static int local_LZ4_decompress_safe_forceExtDict(const char* in, char* out, int inSize, int outSize) +{ + (void)inSize; + LZ4_decompress_safe_forceExtDict(in, out, inSize, outSize, in - 65536, 65536); + return outSize; +} + static int local_LZ4_decompress_safe_partial(const char* in, char* out, int inSize, int outSize) { return LZ4_decompress_safe_partial(in, out, inSize, outSize - 5, outSize); @@ -380,7 +389,7 @@ int fullSpeedBench(char** fileNamesTable, int nbFiles) # define NB_COMPRESSION_ALGORITHMS 14 double totalCTime[NB_COMPRESSION_ALGORITHMS+1] = {0}; double totalCSize[NB_COMPRESSION_ALGORITHMS+1] = {0}; -# define NB_DECOMPRESSION_ALGORITHMS 8 +# define NB_DECOMPRESSION_ALGORITHMS 9 double totalDTime[NB_DECOMPRESSION_ALGORITHMS+1] = {0}; size_t errorCode; @@ -589,7 +598,8 @@ int fullSpeedBench(char** fileNamesTable, int nbFiles) case 5: decompressionFunction = LZ4_decompress_safe_withPrefix64k; dName = "LZ4_decompress_safe_withPrefix64k"; break; case 6: decompressionFunction = local_LZ4_decompress_safe_usingDict; dName = "LZ4_decompress_safe_usingDict"; break; case 7: decompressionFunction = local_LZ4_decompress_safe_partial; dName = "LZ4_decompress_safe_partial"; break; - case 8: decompressionFunction = local_LZ4F_decompress; dName = "LZ4F_decompress"; + case 8: decompressionFunction = local_LZ4_decompress_safe_forceExtDict; dName = "LZ4_decompress_safe_forceExtDict"; break; + case 9: decompressionFunction = local_LZ4F_decompress; dName = "LZ4F_decompress"; errorCode = LZ4F_compressFrame(compressed_buff, compressedBuffSize, orig_buff, benchedSize, NULL); if (LZ4F_isError(errorCode)) { DISPLAY("Preparation error compressing frame\n"); return 1; } chunkP[0].origSize = benchedSize; -- cgit v0.12 From 457dc35e6a56aab12e060af608efd1cbb0eec8a9 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sat, 13 Sep 2014 21:21:41 +0100 Subject: Restored variable output size fuzzer test Quickfix frame decompression Small speed optimization frame decompression --- lz4frame.c | 23 ++++++++++++++--------- programs/frametest.c | 5 ++--- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/lz4frame.c b/lz4frame.c index 390af0e..7bc2319 100644 --- a/lz4frame.c +++ b/lz4frame.c @@ -775,15 +775,6 @@ static void LZ4F_saveDict(LZ4F_dctx_internal_t* dctxPtr, const BYTE* decoded, si static void LZ4F_pointDict(LZ4F_dctx_internal_t* dctxPtr, const BYTE* decoded, size_t decodedSize) { - /* large decoded block */ - if (decodedSize >= (64 KB - 1)) - { - dctxPtr->dict = (BYTE*)decoded; - dctxPtr->dictSize = decodedSize; - dctxPtr->tmpOut = dctxPtr->tmpOutBuffer + 64 KB; - return; - } - /* decoded block in the continuity of dictionary */ if (dctxPtr->dict + dctxPtr->dictSize == decoded) { @@ -800,6 +791,15 @@ static void LZ4F_pointDict(LZ4F_dctx_internal_t* dctxPtr, const BYTE* decoded, s return; } + /* large decoded block */ + if (decodedSize >= (64 KB - 1)) + { + dctxPtr->dict = (BYTE*)decoded; + dctxPtr->dictSize = decodedSize; + dctxPtr->tmpOut = dctxPtr->tmpOutBuffer + 64 KB; + return; + } + /* small block, and not contiguous : let's save that */ LZ4F_saveDict(dctxPtr, decoded, decodedSize); } @@ -953,6 +953,11 @@ size_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContext, break; } dctxPtr->dStage = dstage_getCBlock; + if (dstPtr==dstEnd) + { + nextSrcSizeHint = nextCBlockSize + 4; + doAnotherStage = 0; + } break; } diff --git a/programs/frametest.c b/programs/frametest.c index 8be7752..ba24fe1 100644 --- a/programs/frametest.c +++ b/programs/frametest.c @@ -466,12 +466,11 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi while (ip < iend) { unsigned nbBitsI = (FUZ_rand(&randState) % (maxBits-1)) + 1; - unsigned nbBitsO = (FUZ_rand(&randState) % (maxBits-1)) + 1; + unsigned nbBitsO = (FUZ_rand(&randState) % (maxBits)) + 1; size_t iSize = (FUZ_rand(&randState) & ((1< (size_t)(iend-ip)) iSize = iend-ip; if (oSize > (size_t)(oend-op)) oSize = oend-op; - oSize = oend-op; result = LZ4F_decompress(dCtx, op, &oSize, ip, &iSize, NULL); if (result == (size_t)-ERROR_checksum_invalid) locateBuffDiff((BYTE*)srcBuffer+srcStart, decodedBuffer, srcSize); CHECK(LZ4F_isError(result), "Decompression failed (error %i)", (int)result); -- cgit v0.12 From 7716c03dbfa82e1e8d0ab372ab13f5d39f1f27bb Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sat, 13 Sep 2014 23:44:07 +0100 Subject: changed xxhash directory changed makefile --- programs/Makefile | 16 +- programs/frametest.c | 2 +- programs/xxhash.c | 806 --------------------------------------------------- programs/xxhash.h | 168 ----------- xxhash.c | 806 +++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 815 insertions(+), 983 deletions(-) delete mode 100644 programs/xxhash.c delete mode 100644 programs/xxhash.h create mode 100644 xxhash.c diff --git a/programs/Makefile b/programs/Makefile index cc139f1..c653ec6 100644 --- a/programs/Makefile +++ b/programs/Makefile @@ -70,28 +70,28 @@ default: lz4 lz4c all: lz4 lz4c lz4c32 fullbench fullbench32 fuzzer fuzzer32 frametest datagen -lz4: $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c bench.c xxhash.c lz4io.c lz4cli.c +lz4: $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c $(LZ4DIR)/xxhash.c bench.c lz4io.c lz4cli.c $(CC) $(FLAGS) -DDISABLE_LZ4C_LEGACY_OPTIONS $^ -o $@$(EXT) -lz4c : $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c bench.c xxhash.c lz4io.c lz4cli.c +lz4c : $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c $(LZ4DIR)/xxhash.c bench.c lz4io.c lz4cli.c $(CC) $(FLAGS) $^ -o $@$(EXT) -lz4c32: $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c bench.c xxhash.c lz4io.c lz4cli.c +lz4c32: $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c $(LZ4DIR)/xxhash.c bench.c lz4io.c lz4cli.c $(CC) -m32 $(FLAGS) $^ -o $@$(EXT) -fullbench : $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c $(LZ4DIR)/lz4frame.c xxhash.c fullbench.c +fullbench : $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c $(LZ4DIR)/lz4frame.c $(LZ4DIR)/xxhash.c fullbench.c $(CC) $(FLAGS) $^ -o $@$(EXT) -fullbench32: $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c $(LZ4DIR)/lz4frame.c xxhash.c fullbench.c +fullbench32: $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c $(LZ4DIR)/lz4frame.c $(LZ4DIR)/xxhash.c fullbench.c $(CC) -m32 $(FLAGS) $^ -o $@$(EXT) -fuzzer : $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c xxhash.c fuzzer.c +fuzzer : $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c $(LZ4DIR)/xxhash.c fuzzer.c $(CC) $(FLAGS) $^ -o $@$(EXT) -fuzzer32: $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c xxhash.c fuzzer.c +fuzzer32: $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c $(LZ4DIR)/xxhash.c fuzzer.c $(CC) -m32 $(FLAGS) $^ -o $@$(EXT) -frametest: $(LZ4DIR)/lz4frame.c $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c xxhash.c frametest.c +frametest: $(LZ4DIR)/lz4frame.c $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c $(LZ4DIR)/xxhash.c frametest.c $(CC) $(FLAGS) $^ -o $@$(EXT) datagen : datagen.c diff --git a/programs/frametest.c b/programs/frametest.c index ba24fe1..4d09411 100644 --- a/programs/frametest.c +++ b/programs/frametest.c @@ -404,7 +404,7 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi unsigned BSId = 4 + (FUZ_rand(&randState) & 3); unsigned BMId = FUZ_rand(&randState) & 1; unsigned CCflag = FUZ_rand(&randState) & 1; - unsigned autoflush = (FUZ_rand(&randState) & 3) == 2; + unsigned autoflush = (FUZ_rand(&randState) & 7) == 2; LZ4F_preferences_t prefs = { 0 }; LZ4F_compressOptions_t options = { 0 }; unsigned nbBits = (FUZ_rand(&randState) % (FUZ_highbit(srcDataLength-1) - 1)) + 1; diff --git a/programs/xxhash.c b/programs/xxhash.c deleted file mode 100644 index be48370..0000000 --- a/programs/xxhash.c +++ /dev/null @@ -1,806 +0,0 @@ -/* -xxHash - Fast Hash algorithm -Copyright (C) 2012-2014, Yann Collet. -BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are -met: - -* Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. -* Redistributions in binary form must reproduce the above -copyright notice, this list of conditions and the following disclaimer -in the documentation and/or other materials provided with the -distribution. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -You can contact the author at : -- xxHash source repository : http://code.google.com/p/xxhash/ -*/ - - -//************************************** -// Tuning parameters -//************************************** -// Unaligned memory access is automatically enabled for "common" CPU, such as x86. -// For others CPU, the compiler will be more cautious, and insert extra code to ensure aligned access is respected. -// If you know your target CPU supports unaligned memory access, you want to force this option manually to improve performance. -// You can also enable this parameter if you know your input data will always be aligned (boundaries of 4, for U32). -#if defined(__ARM_FEATURE_UNALIGNED) || defined(__i386) || defined(_M_IX86) || defined(__x86_64__) || defined(_M_X64) -# define XXH_USE_UNALIGNED_ACCESS 1 -#endif - -// XXH_ACCEPT_NULL_INPUT_POINTER : -// If the input pointer is a null pointer, xxHash default behavior is to trigger a memory access error, since it is a bad pointer. -// When this option is enabled, xxHash output for null input pointers will be the same as a null-length input. -// This option has a very small performance cost (only measurable on small inputs). -// By default, this option is disabled. To enable it, uncomment below define : -// #define XXH_ACCEPT_NULL_INPUT_POINTER 1 - -// XXH_FORCE_NATIVE_FORMAT : -// By default, xxHash library provides endian-independant Hash values, based on little-endian convention. -// Results are therefore identical for little-endian and big-endian CPU. -// This comes at a performance cost for big-endian CPU, since some swapping is required to emulate little-endian format. -// Should endian-independance be of no importance for your application, you may set the #define below to 1. -// It will improve speed for Big-endian CPU. -// This option has no impact on Little_Endian CPU. -#define XXH_FORCE_NATIVE_FORMAT 0 - -//************************************** -// Compiler Specific Options -//************************************** -// Disable some Visual warning messages -#ifdef _MSC_VER // Visual Studio -# pragma warning(disable : 4127) // disable: C4127: conditional expression is constant -#endif - -#ifdef _MSC_VER // Visual Studio -# define FORCE_INLINE static __forceinline -#else -# ifdef __GNUC__ -# define FORCE_INLINE static inline __attribute__((always_inline)) -# else -# define FORCE_INLINE static inline -# endif -#endif - -//************************************** -// Includes & Memory related functions -//************************************** -#include "xxhash.h" -// Modify the local functions below should you wish to use some other memory related routines -// for malloc(), free() -#include -FORCE_INLINE void* XXH_malloc(size_t s) { return malloc(s); } -FORCE_INLINE void XXH_free (void* p) { free(p); } -// for memcpy() -#include -FORCE_INLINE void* XXH_memcpy(void* dest, const void* src, size_t size) { return memcpy(dest,src,size); } - - -//************************************** -// Basic Types -//************************************** -#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L // C99 -# include - typedef uint8_t BYTE; - typedef uint16_t U16; - typedef uint32_t U32; - typedef int32_t S32; - typedef uint64_t U64; -#else - typedef unsigned char BYTE; - typedef unsigned short U16; - typedef unsigned int U32; - typedef signed int S32; - typedef unsigned long long U64; -#endif - -#if defined(__GNUC__) && !defined(XXH_USE_UNALIGNED_ACCESS) -# define _PACKED __attribute__ ((packed)) -#else -# define _PACKED -#endif - -#if !defined(XXH_USE_UNALIGNED_ACCESS) && !defined(__GNUC__) -# ifdef __IBMC__ -# pragma pack(1) -# else -# pragma pack(push, 1) -# endif -#endif - -typedef struct _U32_S { U32 v; } _PACKED U32_S; -typedef struct _U64_S { U64 v; } _PACKED U64_S; - -#if !defined(XXH_USE_UNALIGNED_ACCESS) && !defined(__GNUC__) -# pragma pack(pop) -#endif - -#define A32(x) (((U32_S *)(x))->v) -#define A64(x) (((U64_S *)(x))->v) - - -//*************************************** -// Compiler-specific Functions and Macros -//*************************************** -#define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) - -// Note : although _rotl exists for minGW (GCC under windows), performance seems poor -#if defined(_MSC_VER) -# define XXH_rotl32(x,r) _rotl(x,r) -# define XXH_rotl64(x,r) _rotl64(x,r) -#else -# define XXH_rotl32(x,r) ((x << r) | (x >> (32 - r))) -# define XXH_rotl64(x,r) ((x << r) | (x >> (64 - r))) -#endif - -#if defined(_MSC_VER) // Visual Studio -# define XXH_swap32 _byteswap_ulong -# define XXH_swap64 _byteswap_uint64 -#elif GCC_VERSION >= 403 -# define XXH_swap32 __builtin_bswap32 -# define XXH_swap64 __builtin_bswap64 -#else -static inline U32 XXH_swap32 (U32 x) { - return ((x << 24) & 0xff000000 ) | - ((x << 8) & 0x00ff0000 ) | - ((x >> 8) & 0x0000ff00 ) | - ((x >> 24) & 0x000000ff );} -static inline U64 XXH_swap64 (U64 x) { - return ((x << 56) & 0xff00000000000000ULL) | - ((x << 40) & 0x00ff000000000000ULL) | - ((x << 24) & 0x0000ff0000000000ULL) | - ((x << 8) & 0x000000ff00000000ULL) | - ((x >> 8) & 0x00000000ff000000ULL) | - ((x >> 24) & 0x0000000000ff0000ULL) | - ((x >> 40) & 0x000000000000ff00ULL) | - ((x >> 56) & 0x00000000000000ffULL);} -#endif - - -//************************************** -// Constants -//************************************** -#define PRIME32_1 2654435761U -#define PRIME32_2 2246822519U -#define PRIME32_3 3266489917U -#define PRIME32_4 668265263U -#define PRIME32_5 374761393U - -#define PRIME64_1 11400714785074694791ULL -#define PRIME64_2 14029467366897019727ULL -#define PRIME64_3 1609587929392839161ULL -#define PRIME64_4 9650029242287828579ULL -#define PRIME64_5 2870177450012600261ULL - -//************************************** -// Architecture Macros -//************************************** -typedef enum { XXH_bigEndian=0, XXH_littleEndian=1 } XXH_endianess; -#ifndef XXH_CPU_LITTLE_ENDIAN // It is possible to define XXH_CPU_LITTLE_ENDIAN externally, for example using a compiler switch - static const int one = 1; -# define XXH_CPU_LITTLE_ENDIAN (*(char*)(&one)) -#endif - - -//************************************** -// Macros -//************************************** -#define XXH_STATIC_ASSERT(c) { enum { XXH_static_assert = 1/(!!(c)) }; } // use only *after* variable declarations - - -//**************************** -// Memory reads -//**************************** -typedef enum { XXH_aligned, XXH_unaligned } XXH_alignment; - -FORCE_INLINE U32 XXH_readLE32_align(const U32* ptr, XXH_endianess endian, XXH_alignment align) -{ - if (align==XXH_unaligned) - return endian==XXH_littleEndian ? A32(ptr) : XXH_swap32(A32(ptr)); - else - return endian==XXH_littleEndian ? *ptr : XXH_swap32(*ptr); -} - -FORCE_INLINE U32 XXH_readLE32(const U32* ptr, XXH_endianess endian) { return XXH_readLE32_align(ptr, endian, XXH_unaligned); } - -FORCE_INLINE U64 XXH_readLE64_align(const U64* ptr, XXH_endianess endian, XXH_alignment align) -{ - if (align==XXH_unaligned) - return endian==XXH_littleEndian ? A64(ptr) : XXH_swap64(A64(ptr)); - else - return endian==XXH_littleEndian ? *ptr : XXH_swap64(*ptr); -} - -FORCE_INLINE U64 XXH_readLE64(const U64* ptr, XXH_endianess endian) { return XXH_readLE64_align(ptr, endian, XXH_unaligned); } - - -//**************************** -// Simple Hash Functions -//**************************** -FORCE_INLINE U32 XXH32_endian_align(const void* input, unsigned int len, U32 seed, XXH_endianess endian, XXH_alignment align) -{ - const BYTE* p = (const BYTE*)input; - const BYTE* bEnd = p + len; - U32 h32; -#define XXH_get32bits(p) XXH_readLE32_align((const U32*)p, endian, align) - -#ifdef XXH_ACCEPT_NULL_INPUT_POINTER - if (p==NULL) { len=0; bEnd=p=(const BYTE*)(size_t)16; } -#endif - - if (len>=16) - { - const BYTE* const limit = bEnd - 16; - U32 v1 = seed + PRIME32_1 + PRIME32_2; - U32 v2 = seed + PRIME32_2; - U32 v3 = seed + 0; - U32 v4 = seed - PRIME32_1; - - do - { - v1 += XXH_get32bits(p) * PRIME32_2; v1 = XXH_rotl32(v1, 13); v1 *= PRIME32_1; p+=4; - v2 += XXH_get32bits(p) * PRIME32_2; v2 = XXH_rotl32(v2, 13); v2 *= PRIME32_1; p+=4; - v3 += XXH_get32bits(p) * PRIME32_2; v3 = XXH_rotl32(v3, 13); v3 *= PRIME32_1; p+=4; - v4 += XXH_get32bits(p) * PRIME32_2; v4 = XXH_rotl32(v4, 13); v4 *= PRIME32_1; p+=4; - } while (p<=limit); - - h32 = XXH_rotl32(v1, 1) + XXH_rotl32(v2, 7) + XXH_rotl32(v3, 12) + XXH_rotl32(v4, 18); - } - else - { - h32 = seed + PRIME32_5; - } - - h32 += (U32) len; - - while (p+4<=bEnd) - { - h32 += XXH_get32bits(p) * PRIME32_3; - h32 = XXH_rotl32(h32, 17) * PRIME32_4 ; - p+=4; - } - - while (p> 15; - h32 *= PRIME32_2; - h32 ^= h32 >> 13; - h32 *= PRIME32_3; - h32 ^= h32 >> 16; - - return h32; -} - - -U32 XXH32(const void* input, unsigned int len, U32 seed) -{ -#if 0 - // Simple version, good for code maintenance, but unfortunately slow for small inputs - void* state = XXH32_init(seed); - XXH32_update(state, input, len); - return XXH32_digest(state); -#else - XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN; - -# if !defined(XXH_USE_UNALIGNED_ACCESS) - if ((((size_t)input) & 3) == 0) // Input is aligned, let's leverage the speed advantage - { - if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT) - return XXH32_endian_align(input, len, seed, XXH_littleEndian, XXH_aligned); - else - return XXH32_endian_align(input, len, seed, XXH_bigEndian, XXH_aligned); - } -# endif - - if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT) - return XXH32_endian_align(input, len, seed, XXH_littleEndian, XXH_unaligned); - else - return XXH32_endian_align(input, len, seed, XXH_bigEndian, XXH_unaligned); -#endif -} - -FORCE_INLINE U64 XXH64_endian_align(const void* input, unsigned int len, U64 seed, XXH_endianess endian, XXH_alignment align) -{ - const BYTE* p = (const BYTE*)input; - const BYTE* bEnd = p + len; - U64 h64; -#define XXH_get64bits(p) XXH_readLE64_align((const U64*)p, endian, align) - -#ifdef XXH_ACCEPT_NULL_INPUT_POINTER - if (p==NULL) { len=0; bEnd=p=(const BYTE*)(size_t)32; } -#endif - - if (len>=32) - { - const BYTE* const limit = bEnd - 32; - U64 v1 = seed + PRIME64_1 + PRIME64_2; - U64 v2 = seed + PRIME64_2; - U64 v3 = seed + 0; - U64 v4 = seed - PRIME64_1; - - do - { - v1 += XXH_get64bits(p) * PRIME64_2; p+=8; v1 = XXH_rotl64(v1, 31); v1 *= PRIME64_1; - v2 += XXH_get64bits(p) * PRIME64_2; p+=8; v2 = XXH_rotl64(v2, 31); v2 *= PRIME64_1; - v3 += XXH_get64bits(p) * PRIME64_2; p+=8; v3 = XXH_rotl64(v3, 31); v3 *= PRIME64_1; - v4 += XXH_get64bits(p) * PRIME64_2; p+=8; v4 = XXH_rotl64(v4, 31); v4 *= PRIME64_1; - } while (p<=limit); - - h64 = XXH_rotl64(v1, 1) + XXH_rotl64(v2, 7) + XXH_rotl64(v3, 12) + XXH_rotl64(v4, 18); - - v1 *= PRIME64_2; v1 = XXH_rotl64(v1, 31); v1 *= PRIME64_1; h64 ^= v1; - h64 = h64 * PRIME64_1 + PRIME64_4; - - v2 *= PRIME64_2; v2 = XXH_rotl64(v2, 31); v2 *= PRIME64_1; h64 ^= v2; - h64 = h64 * PRIME64_1 + PRIME64_4; - - v3 *= PRIME64_2; v3 = XXH_rotl64(v3, 31); v3 *= PRIME64_1; h64 ^= v3; - h64 = h64 * PRIME64_1 + PRIME64_4; - - v4 *= PRIME64_2; v4 = XXH_rotl64(v4, 31); v4 *= PRIME64_1; h64 ^= v4; - h64 = h64 * PRIME64_1 + PRIME64_4; - } - else - { - h64 = seed + PRIME64_5; - } - - h64 += (U64) len; - - while (p+8<=bEnd) - { - U64 k1 = XXH_get64bits(p); - k1 *= PRIME64_2; k1 = XXH_rotl64(k1,31); k1 *= PRIME64_1; h64 ^= k1; - h64 = XXH_rotl64(h64,27) * PRIME64_1 + PRIME64_4; - p+=8; - } - - if (p+4<=bEnd) - { - h64 ^= (U64)(XXH_get32bits(p)) * PRIME64_1; - h64 = XXH_rotl64(h64, 23) * PRIME64_2 + PRIME64_3; - p+=4; - } - - while (p> 33; - h64 *= PRIME64_2; - h64 ^= h64 >> 29; - h64 *= PRIME64_3; - h64 ^= h64 >> 32; - - return h64; -} - - -unsigned long long XXH64(const void* input, unsigned int len, unsigned long long seed) -{ - XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN; - -# if !defined(XXH_USE_UNALIGNED_ACCESS) - if ((((size_t)input) & 7)==0) // Input is aligned, let's leverage the speed advantage - { - if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT) - return XXH64_endian_align(input, len, seed, XXH_littleEndian, XXH_aligned); - else - return XXH64_endian_align(input, len, seed, XXH_bigEndian, XXH_aligned); - } -# endif - - if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT) - return XXH64_endian_align(input, len, seed, XXH_littleEndian, XXH_unaligned); - else - return XXH64_endian_align(input, len, seed, XXH_bigEndian, XXH_unaligned); -} - -//**************************** -// Advanced Hash Functions -//**************************** - -struct XXH_state32_t -{ - U64 total_len; - U32 seed; - U32 v1; - U32 v2; - U32 v3; - U32 v4; - int memsize; - char memory[16]; -}; - -struct XXH_state64_t -{ - U64 total_len; - U64 seed; - U64 v1; - U64 v2; - U64 v3; - U64 v4; - int memsize; - char memory[32]; -}; - - -int XXH32_sizeofState(void) -{ - XXH_STATIC_ASSERT(XXH32_SIZEOFSTATE >= sizeof(struct XXH_state32_t)); // A compilation error here means XXH32_SIZEOFSTATE is not large enough - return sizeof(struct XXH_state32_t); -} - -int XXH64_sizeofState(void) -{ - XXH_STATIC_ASSERT(XXH64_SIZEOFSTATE >= sizeof(struct XXH_state64_t)); // A compilation error here means XXH64_SIZEOFSTATE is not large enough - return sizeof(struct XXH_state64_t); -} - - -XXH_errorcode XXH32_resetState(void* state_in, U32 seed) -{ - struct XXH_state32_t * state = (struct XXH_state32_t *) state_in; - state->seed = seed; - state->v1 = seed + PRIME32_1 + PRIME32_2; - state->v2 = seed + PRIME32_2; - state->v3 = seed + 0; - state->v4 = seed - PRIME32_1; - state->total_len = 0; - state->memsize = 0; - return XXH_OK; -} - -XXH_errorcode XXH64_resetState(void* state_in, unsigned long long seed) -{ - struct XXH_state64_t * state = (struct XXH_state64_t *) state_in; - state->seed = seed; - state->v1 = seed + PRIME64_1 + PRIME64_2; - state->v2 = seed + PRIME64_2; - state->v3 = seed + 0; - state->v4 = seed - PRIME64_1; - state->total_len = 0; - state->memsize = 0; - return XXH_OK; -} - - -void* XXH32_init (U32 seed) -{ - void* state = XXH_malloc (sizeof(struct XXH_state32_t)); - if (state != NULL) XXH32_resetState(state, seed); - return state; -} - -void* XXH64_init (unsigned long long seed) -{ - void* state = XXH_malloc (sizeof(struct XXH_state64_t)); - if (state != NULL) XXH64_resetState(state, seed); - return state; -} - - -FORCE_INLINE XXH_errorcode XXH32_update_endian (void* state_in, const void* input, int len, XXH_endianess endian) -{ - struct XXH_state32_t * state = (struct XXH_state32_t *) state_in; - const BYTE* p = (const BYTE*)input; - const BYTE* const bEnd = p + len; - -#ifdef XXH_ACCEPT_NULL_INPUT_POINTER - if (input==NULL) return XXH_ERROR; -#endif - - state->total_len += len; - - if (state->memsize + len < 16) // fill in tmp buffer - { - XXH_memcpy(state->memory + state->memsize, input, len); - state->memsize += len; - return XXH_OK; - } - - if (state->memsize) // some data left from previous update - { - XXH_memcpy(state->memory + state->memsize, input, 16-state->memsize); - { - const U32* p32 = (const U32*)state->memory; - state->v1 += XXH_readLE32(p32, endian) * PRIME32_2; state->v1 = XXH_rotl32(state->v1, 13); state->v1 *= PRIME32_1; p32++; - state->v2 += XXH_readLE32(p32, endian) * PRIME32_2; state->v2 = XXH_rotl32(state->v2, 13); state->v2 *= PRIME32_1; p32++; - state->v3 += XXH_readLE32(p32, endian) * PRIME32_2; state->v3 = XXH_rotl32(state->v3, 13); state->v3 *= PRIME32_1; p32++; - state->v4 += XXH_readLE32(p32, endian) * PRIME32_2; state->v4 = XXH_rotl32(state->v4, 13); state->v4 *= PRIME32_1; p32++; - } - p += 16-state->memsize; - state->memsize = 0; - } - - if (p <= bEnd-16) - { - const BYTE* const limit = bEnd - 16; - U32 v1 = state->v1; - U32 v2 = state->v2; - U32 v3 = state->v3; - U32 v4 = state->v4; - - do - { - v1 += XXH_readLE32((const U32*)p, endian) * PRIME32_2; v1 = XXH_rotl32(v1, 13); v1 *= PRIME32_1; p+=4; - v2 += XXH_readLE32((const U32*)p, endian) * PRIME32_2; v2 = XXH_rotl32(v2, 13); v2 *= PRIME32_1; p+=4; - v3 += XXH_readLE32((const U32*)p, endian) * PRIME32_2; v3 = XXH_rotl32(v3, 13); v3 *= PRIME32_1; p+=4; - v4 += XXH_readLE32((const U32*)p, endian) * PRIME32_2; v4 = XXH_rotl32(v4, 13); v4 *= PRIME32_1; p+=4; - } while (p<=limit); - - state->v1 = v1; - state->v2 = v2; - state->v3 = v3; - state->v4 = v4; - } - - if (p < bEnd) - { - XXH_memcpy(state->memory, p, bEnd-p); - state->memsize = (int)(bEnd-p); - } - - return XXH_OK; -} - -XXH_errorcode XXH32_update (void* state_in, const void* input, unsigned int len) -{ - XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN; - - if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT) - return XXH32_update_endian(state_in, input, len, XXH_littleEndian); - else - return XXH32_update_endian(state_in, input, len, XXH_bigEndian); -} - - - -FORCE_INLINE U32 XXH32_intermediateDigest_endian (void* state_in, XXH_endianess endian) -{ - struct XXH_state32_t * state = (struct XXH_state32_t *) state_in; - const BYTE * p = (const BYTE*)state->memory; - BYTE* bEnd = (BYTE*)state->memory + state->memsize; - U32 h32; - - if (state->total_len >= 16) - { - h32 = XXH_rotl32(state->v1, 1) + XXH_rotl32(state->v2, 7) + XXH_rotl32(state->v3, 12) + XXH_rotl32(state->v4, 18); - } - else - { - h32 = state->seed + PRIME32_5; - } - - h32 += (U32) state->total_len; - - while (p+4<=bEnd) - { - h32 += XXH_readLE32((const U32*)p, endian) * PRIME32_3; - h32 = XXH_rotl32(h32, 17) * PRIME32_4; - p+=4; - } - - while (p> 15; - h32 *= PRIME32_2; - h32 ^= h32 >> 13; - h32 *= PRIME32_3; - h32 ^= h32 >> 16; - - return h32; -} - - -U32 XXH32_intermediateDigest (void* state_in) -{ - XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN; - - if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT) - return XXH32_intermediateDigest_endian(state_in, XXH_littleEndian); - else - return XXH32_intermediateDigest_endian(state_in, XXH_bigEndian); -} - - -U32 XXH32_digest (void* state_in) -{ - U32 h32 = XXH32_intermediateDigest(state_in); - - XXH_free(state_in); - - return h32; -} - - -FORCE_INLINE XXH_errorcode XXH64_update_endian (void* state_in, const void* input, int len, XXH_endianess endian) -{ - struct XXH_state64_t * state = (struct XXH_state64_t *) state_in; - const BYTE* p = (const BYTE*)input; - const BYTE* const bEnd = p + len; - -#ifdef XXH_ACCEPT_NULL_INPUT_POINTER - if (input==NULL) return XXH_ERROR; -#endif - - state->total_len += len; - - if (state->memsize + len < 32) // fill in tmp buffer - { - XXH_memcpy(state->memory + state->memsize, input, len); - state->memsize += len; - return XXH_OK; - } - - if (state->memsize) // some data left from previous update - { - XXH_memcpy(state->memory + state->memsize, input, 32-state->memsize); - { - const U64* p64 = (const U64*)state->memory; - state->v1 += XXH_readLE64(p64, endian) * PRIME64_2; state->v1 = XXH_rotl64(state->v1, 31); state->v1 *= PRIME64_1; p64++; - state->v2 += XXH_readLE64(p64, endian) * PRIME64_2; state->v2 = XXH_rotl64(state->v2, 31); state->v2 *= PRIME64_1; p64++; - state->v3 += XXH_readLE64(p64, endian) * PRIME64_2; state->v3 = XXH_rotl64(state->v3, 31); state->v3 *= PRIME64_1; p64++; - state->v4 += XXH_readLE64(p64, endian) * PRIME64_2; state->v4 = XXH_rotl64(state->v4, 31); state->v4 *= PRIME64_1; p64++; - } - p += 32-state->memsize; - state->memsize = 0; - } - - if (p+32 <= bEnd) - { - const BYTE* const limit = bEnd - 32; - U64 v1 = state->v1; - U64 v2 = state->v2; - U64 v3 = state->v3; - U64 v4 = state->v4; - - do - { - v1 += XXH_readLE64((const U64*)p, endian) * PRIME64_2; v1 = XXH_rotl64(v1, 31); v1 *= PRIME64_1; p+=8; - v2 += XXH_readLE64((const U64*)p, endian) * PRIME64_2; v2 = XXH_rotl64(v2, 31); v2 *= PRIME64_1; p+=8; - v3 += XXH_readLE64((const U64*)p, endian) * PRIME64_2; v3 = XXH_rotl64(v3, 31); v3 *= PRIME64_1; p+=8; - v4 += XXH_readLE64((const U64*)p, endian) * PRIME64_2; v4 = XXH_rotl64(v4, 31); v4 *= PRIME64_1; p+=8; - } while (p<=limit); - - state->v1 = v1; - state->v2 = v2; - state->v3 = v3; - state->v4 = v4; - } - - if (p < bEnd) - { - XXH_memcpy(state->memory, p, bEnd-p); - state->memsize = (int)(bEnd-p); - } - - return XXH_OK; -} - -XXH_errorcode XXH64_update (void* state_in, const void* input, unsigned int len) -{ - XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN; - - if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT) - return XXH64_update_endian(state_in, input, len, XXH_littleEndian); - else - return XXH64_update_endian(state_in, input, len, XXH_bigEndian); -} - - - -FORCE_INLINE U64 XXH64_intermediateDigest_endian (void* state_in, XXH_endianess endian) -{ - struct XXH_state64_t * state = (struct XXH_state64_t *) state_in; - const BYTE * p = (const BYTE*)state->memory; - BYTE* bEnd = (BYTE*)state->memory + state->memsize; - U64 h64; - - if (state->total_len >= 32) - { - U64 v1 = state->v1; - U64 v2 = state->v2; - U64 v3 = state->v3; - U64 v4 = state->v4; - - h64 = XXH_rotl64(v1, 1) + XXH_rotl64(v2, 7) + XXH_rotl64(v3, 12) + XXH_rotl64(v4, 18); - - v1 *= PRIME64_2; v1 = XXH_rotl64(v1, 31); v1 *= PRIME64_1; h64 ^= v1; - h64 = h64*PRIME64_1 + PRIME64_4; - - v2 *= PRIME64_2; v2 = XXH_rotl64(v2, 31); v2 *= PRIME64_1; h64 ^= v2; - h64 = h64*PRIME64_1 + PRIME64_4; - - v3 *= PRIME64_2; v3 = XXH_rotl64(v3, 31); v3 *= PRIME64_1; h64 ^= v3; - h64 = h64*PRIME64_1 + PRIME64_4; - - v4 *= PRIME64_2; v4 = XXH_rotl64(v4, 31); v4 *= PRIME64_1; h64 ^= v4; - h64 = h64*PRIME64_1 + PRIME64_4; - } - else - { - h64 = state->seed + PRIME64_5; - } - - h64 += (U64) state->total_len; - - while (p+8<=bEnd) - { - U64 k1 = XXH_readLE64((const U64*)p, endian); - k1 *= PRIME64_2; k1 = XXH_rotl64(k1,31); k1 *= PRIME64_1; h64 ^= k1; - h64 = XXH_rotl64(h64,27) * PRIME64_1 + PRIME64_4; - p+=8; - } - - if (p+4<=bEnd) - { - h64 ^= (U64)(XXH_readLE32((const U32*)p, endian)) * PRIME64_1; - h64 = XXH_rotl64(h64, 23) * PRIME64_2 + PRIME64_3; - p+=4; - } - - while (p> 33; - h64 *= PRIME64_2; - h64 ^= h64 >> 29; - h64 *= PRIME64_3; - h64 ^= h64 >> 32; - - return h64; -} - - -unsigned long long XXH64_intermediateDigest (void* state_in) -{ - XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN; - - if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT) - return XXH64_intermediateDigest_endian(state_in, XXH_littleEndian); - else - return XXH64_intermediateDigest_endian(state_in, XXH_bigEndian); -} - - -unsigned long long XXH64_digest (void* state_in) -{ - U64 h64 = XXH64_intermediateDigest(state_in); - - XXH_free(state_in); - - return h64; -} - diff --git a/programs/xxhash.h b/programs/xxhash.h deleted file mode 100644 index 4311485..0000000 --- a/programs/xxhash.h +++ /dev/null @@ -1,168 +0,0 @@ -/* - xxHash - Extremely Fast Hash algorithm - Header File - Copyright (C) 2012-2014, Yann Collet. - BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are - met: - - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above - copyright notice, this list of conditions and the following disclaimer - in the documentation and/or other materials provided with the - distribution. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - You can contact the author at : - - xxHash source repository : http://code.google.com/p/xxhash/ -*/ - -/* Notice extracted from xxHash homepage : - -xxHash is an extremely fast Hash algorithm, running at RAM speed limits. -It also successfully passes all tests from the SMHasher suite. - -Comparison (single thread, Windows Seven 32 bits, using SMHasher on a Core 2 Duo @3GHz) - -Name Speed Q.Score Author -xxHash 5.4 GB/s 10 -CrapWow 3.2 GB/s 2 Andrew -MumurHash 3a 2.7 GB/s 10 Austin Appleby -SpookyHash 2.0 GB/s 10 Bob Jenkins -SBox 1.4 GB/s 9 Bret Mulvey -Lookup3 1.2 GB/s 9 Bob Jenkins -SuperFastHash 1.2 GB/s 1 Paul Hsieh -CityHash64 1.05 GB/s 10 Pike & Alakuijala -FNV 0.55 GB/s 5 Fowler, Noll, Vo -CRC32 0.43 GB/s 9 -MD5-32 0.33 GB/s 10 Ronald L. Rivest -SHA1-32 0.28 GB/s 10 - -Q.Score is a measure of quality of the hash function. -It depends on successfully passing SMHasher test set. -10 is a perfect score. -*/ - -#pragma once - -#if defined (__cplusplus) -extern "C" { -#endif - - -/***************************** - Type -*****************************/ -typedef enum { XXH_OK=0, XXH_ERROR } XXH_errorcode; - - - -/***************************** - Simple Hash Functions -*****************************/ - -unsigned int XXH32 (const void* input, unsigned int len, unsigned int seed); -unsigned long long XXH64 (const void* input, unsigned int len, unsigned long long seed); - -/* -XXH32() : - Calculate the 32-bits hash of sequence of length "len" stored at memory address "input". - The memory between input & input+len must be valid (allocated and read-accessible). - "seed" can be used to alter the result predictably. - This function successfully passes all SMHasher tests. - Speed on Core 2 Duo @ 3 GHz (single thread, SMHasher benchmark) : 5.4 GB/s - Note that "len" is type "int", which means it is limited to 2^31-1. - If your data is larger, use the advanced functions below. -XXH64() : - Calculate the 64-bits hash of sequence of length "len" stored at memory address "input". -*/ - - - -/***************************** - Advanced Hash Functions -*****************************/ - -void* XXH32_init (unsigned int seed); -XXH_errorcode XXH32_update (void* state, const void* input, unsigned int len); -unsigned int XXH32_digest (void* state); - -void* XXH64_init (unsigned long long seed); -XXH_errorcode XXH64_update (void* state, const void* input, unsigned int len); -unsigned long long XXH64_digest (void* state); - -/* -These functions calculate the xxhash of an input provided in several small packets, -as opposed to an input provided as a single block. - -It must be started with : -void* XXHnn_init() -The function returns a pointer which holds the state of calculation. -If the pointer is NULL, allocation has failed, so no state can be tracked. - -The state pointer must be provided as "void* state" parameter for XXHnn_update(). -XXHnn_update() can be called as many times as necessary. -The user must provide a valid (allocated) input. -The function returns an error code, with 0 meaning OK, and any other value meaning there is an error. -Note that "len" is type "int", which means it is limited to 2^31-1. -If your data is larger, it is recommended to chunk your data into blocks -of size for example 2^30 (1GB) to avoid any "int" overflow issue. - -Finally, you can end the calculation anytime, by using XXHnn_digest(). -This function returns the final nn-bits hash. -You must provide the same "void* state" parameter created by XXHnn_init(). -Memory will be freed by XXHnn_digest(). -*/ - - -int XXH32_sizeofState(void); -XXH_errorcode XXH32_resetState(void* state, unsigned int seed); - -#define XXH32_SIZEOFSTATE 48 -typedef struct { long long ll[(XXH32_SIZEOFSTATE+(sizeof(long long)-1))/sizeof(long long)]; } XXH32_stateSpace_t; - -int XXH64_sizeofState(void); -XXH_errorcode XXH64_resetState(void* state, unsigned long long seed); - -#define XXH64_SIZEOFSTATE 88 -typedef struct { long long ll[(XXH64_SIZEOFSTATE+(sizeof(long long)-1))/sizeof(long long)]; } XXH64_stateSpace_t; - -/* -These functions allow user application to make its own allocation for state. - -XXHnn_sizeofState() is used to know how much space must be allocated for the xxHash nn-bits state. -Note that the state must be aligned to access 'long long' fields. Memory must be allocated and referenced by a pointer. -This pointer must then be provided as 'state' into XXHnn_resetState(), which initializes the state. - -For static allocation purposes (such as allocation on stack, or freestanding systems without malloc()), -use the structure XXHnn_stateSpace_t, which will ensure that memory space is large enough and correctly aligned to access 'long long' fields. -*/ - - -unsigned int XXH32_intermediateDigest (void* state); -unsigned long long XXH64_intermediateDigest (void* state); -/* -These functions do the same as XXHnn_digest(), generating a nn-bit hash, -but preserve memory context. -This way, it becomes possible to generate intermediate hashes, and then continue feeding data with XXHnn_update(). -To free memory context, use XXHnn_digest(), or free(). -*/ - - -#if defined (__cplusplus) -} -#endif diff --git a/xxhash.c b/xxhash.c new file mode 100644 index 0000000..be48370 --- /dev/null +++ b/xxhash.c @@ -0,0 +1,806 @@ +/* +xxHash - Fast Hash algorithm +Copyright (C) 2012-2014, Yann Collet. +BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + +* Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. +* Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with the +distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +You can contact the author at : +- xxHash source repository : http://code.google.com/p/xxhash/ +*/ + + +//************************************** +// Tuning parameters +//************************************** +// Unaligned memory access is automatically enabled for "common" CPU, such as x86. +// For others CPU, the compiler will be more cautious, and insert extra code to ensure aligned access is respected. +// If you know your target CPU supports unaligned memory access, you want to force this option manually to improve performance. +// You can also enable this parameter if you know your input data will always be aligned (boundaries of 4, for U32). +#if defined(__ARM_FEATURE_UNALIGNED) || defined(__i386) || defined(_M_IX86) || defined(__x86_64__) || defined(_M_X64) +# define XXH_USE_UNALIGNED_ACCESS 1 +#endif + +// XXH_ACCEPT_NULL_INPUT_POINTER : +// If the input pointer is a null pointer, xxHash default behavior is to trigger a memory access error, since it is a bad pointer. +// When this option is enabled, xxHash output for null input pointers will be the same as a null-length input. +// This option has a very small performance cost (only measurable on small inputs). +// By default, this option is disabled. To enable it, uncomment below define : +// #define XXH_ACCEPT_NULL_INPUT_POINTER 1 + +// XXH_FORCE_NATIVE_FORMAT : +// By default, xxHash library provides endian-independant Hash values, based on little-endian convention. +// Results are therefore identical for little-endian and big-endian CPU. +// This comes at a performance cost for big-endian CPU, since some swapping is required to emulate little-endian format. +// Should endian-independance be of no importance for your application, you may set the #define below to 1. +// It will improve speed for Big-endian CPU. +// This option has no impact on Little_Endian CPU. +#define XXH_FORCE_NATIVE_FORMAT 0 + +//************************************** +// Compiler Specific Options +//************************************** +// Disable some Visual warning messages +#ifdef _MSC_VER // Visual Studio +# pragma warning(disable : 4127) // disable: C4127: conditional expression is constant +#endif + +#ifdef _MSC_VER // Visual Studio +# define FORCE_INLINE static __forceinline +#else +# ifdef __GNUC__ +# define FORCE_INLINE static inline __attribute__((always_inline)) +# else +# define FORCE_INLINE static inline +# endif +#endif + +//************************************** +// Includes & Memory related functions +//************************************** +#include "xxhash.h" +// Modify the local functions below should you wish to use some other memory related routines +// for malloc(), free() +#include +FORCE_INLINE void* XXH_malloc(size_t s) { return malloc(s); } +FORCE_INLINE void XXH_free (void* p) { free(p); } +// for memcpy() +#include +FORCE_INLINE void* XXH_memcpy(void* dest, const void* src, size_t size) { return memcpy(dest,src,size); } + + +//************************************** +// Basic Types +//************************************** +#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L // C99 +# include + typedef uint8_t BYTE; + typedef uint16_t U16; + typedef uint32_t U32; + typedef int32_t S32; + typedef uint64_t U64; +#else + typedef unsigned char BYTE; + typedef unsigned short U16; + typedef unsigned int U32; + typedef signed int S32; + typedef unsigned long long U64; +#endif + +#if defined(__GNUC__) && !defined(XXH_USE_UNALIGNED_ACCESS) +# define _PACKED __attribute__ ((packed)) +#else +# define _PACKED +#endif + +#if !defined(XXH_USE_UNALIGNED_ACCESS) && !defined(__GNUC__) +# ifdef __IBMC__ +# pragma pack(1) +# else +# pragma pack(push, 1) +# endif +#endif + +typedef struct _U32_S { U32 v; } _PACKED U32_S; +typedef struct _U64_S { U64 v; } _PACKED U64_S; + +#if !defined(XXH_USE_UNALIGNED_ACCESS) && !defined(__GNUC__) +# pragma pack(pop) +#endif + +#define A32(x) (((U32_S *)(x))->v) +#define A64(x) (((U64_S *)(x))->v) + + +//*************************************** +// Compiler-specific Functions and Macros +//*************************************** +#define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) + +// Note : although _rotl exists for minGW (GCC under windows), performance seems poor +#if defined(_MSC_VER) +# define XXH_rotl32(x,r) _rotl(x,r) +# define XXH_rotl64(x,r) _rotl64(x,r) +#else +# define XXH_rotl32(x,r) ((x << r) | (x >> (32 - r))) +# define XXH_rotl64(x,r) ((x << r) | (x >> (64 - r))) +#endif + +#if defined(_MSC_VER) // Visual Studio +# define XXH_swap32 _byteswap_ulong +# define XXH_swap64 _byteswap_uint64 +#elif GCC_VERSION >= 403 +# define XXH_swap32 __builtin_bswap32 +# define XXH_swap64 __builtin_bswap64 +#else +static inline U32 XXH_swap32 (U32 x) { + return ((x << 24) & 0xff000000 ) | + ((x << 8) & 0x00ff0000 ) | + ((x >> 8) & 0x0000ff00 ) | + ((x >> 24) & 0x000000ff );} +static inline U64 XXH_swap64 (U64 x) { + return ((x << 56) & 0xff00000000000000ULL) | + ((x << 40) & 0x00ff000000000000ULL) | + ((x << 24) & 0x0000ff0000000000ULL) | + ((x << 8) & 0x000000ff00000000ULL) | + ((x >> 8) & 0x00000000ff000000ULL) | + ((x >> 24) & 0x0000000000ff0000ULL) | + ((x >> 40) & 0x000000000000ff00ULL) | + ((x >> 56) & 0x00000000000000ffULL);} +#endif + + +//************************************** +// Constants +//************************************** +#define PRIME32_1 2654435761U +#define PRIME32_2 2246822519U +#define PRIME32_3 3266489917U +#define PRIME32_4 668265263U +#define PRIME32_5 374761393U + +#define PRIME64_1 11400714785074694791ULL +#define PRIME64_2 14029467366897019727ULL +#define PRIME64_3 1609587929392839161ULL +#define PRIME64_4 9650029242287828579ULL +#define PRIME64_5 2870177450012600261ULL + +//************************************** +// Architecture Macros +//************************************** +typedef enum { XXH_bigEndian=0, XXH_littleEndian=1 } XXH_endianess; +#ifndef XXH_CPU_LITTLE_ENDIAN // It is possible to define XXH_CPU_LITTLE_ENDIAN externally, for example using a compiler switch + static const int one = 1; +# define XXH_CPU_LITTLE_ENDIAN (*(char*)(&one)) +#endif + + +//************************************** +// Macros +//************************************** +#define XXH_STATIC_ASSERT(c) { enum { XXH_static_assert = 1/(!!(c)) }; } // use only *after* variable declarations + + +//**************************** +// Memory reads +//**************************** +typedef enum { XXH_aligned, XXH_unaligned } XXH_alignment; + +FORCE_INLINE U32 XXH_readLE32_align(const U32* ptr, XXH_endianess endian, XXH_alignment align) +{ + if (align==XXH_unaligned) + return endian==XXH_littleEndian ? A32(ptr) : XXH_swap32(A32(ptr)); + else + return endian==XXH_littleEndian ? *ptr : XXH_swap32(*ptr); +} + +FORCE_INLINE U32 XXH_readLE32(const U32* ptr, XXH_endianess endian) { return XXH_readLE32_align(ptr, endian, XXH_unaligned); } + +FORCE_INLINE U64 XXH_readLE64_align(const U64* ptr, XXH_endianess endian, XXH_alignment align) +{ + if (align==XXH_unaligned) + return endian==XXH_littleEndian ? A64(ptr) : XXH_swap64(A64(ptr)); + else + return endian==XXH_littleEndian ? *ptr : XXH_swap64(*ptr); +} + +FORCE_INLINE U64 XXH_readLE64(const U64* ptr, XXH_endianess endian) { return XXH_readLE64_align(ptr, endian, XXH_unaligned); } + + +//**************************** +// Simple Hash Functions +//**************************** +FORCE_INLINE U32 XXH32_endian_align(const void* input, unsigned int len, U32 seed, XXH_endianess endian, XXH_alignment align) +{ + const BYTE* p = (const BYTE*)input; + const BYTE* bEnd = p + len; + U32 h32; +#define XXH_get32bits(p) XXH_readLE32_align((const U32*)p, endian, align) + +#ifdef XXH_ACCEPT_NULL_INPUT_POINTER + if (p==NULL) { len=0; bEnd=p=(const BYTE*)(size_t)16; } +#endif + + if (len>=16) + { + const BYTE* const limit = bEnd - 16; + U32 v1 = seed + PRIME32_1 + PRIME32_2; + U32 v2 = seed + PRIME32_2; + U32 v3 = seed + 0; + U32 v4 = seed - PRIME32_1; + + do + { + v1 += XXH_get32bits(p) * PRIME32_2; v1 = XXH_rotl32(v1, 13); v1 *= PRIME32_1; p+=4; + v2 += XXH_get32bits(p) * PRIME32_2; v2 = XXH_rotl32(v2, 13); v2 *= PRIME32_1; p+=4; + v3 += XXH_get32bits(p) * PRIME32_2; v3 = XXH_rotl32(v3, 13); v3 *= PRIME32_1; p+=4; + v4 += XXH_get32bits(p) * PRIME32_2; v4 = XXH_rotl32(v4, 13); v4 *= PRIME32_1; p+=4; + } while (p<=limit); + + h32 = XXH_rotl32(v1, 1) + XXH_rotl32(v2, 7) + XXH_rotl32(v3, 12) + XXH_rotl32(v4, 18); + } + else + { + h32 = seed + PRIME32_5; + } + + h32 += (U32) len; + + while (p+4<=bEnd) + { + h32 += XXH_get32bits(p) * PRIME32_3; + h32 = XXH_rotl32(h32, 17) * PRIME32_4 ; + p+=4; + } + + while (p> 15; + h32 *= PRIME32_2; + h32 ^= h32 >> 13; + h32 *= PRIME32_3; + h32 ^= h32 >> 16; + + return h32; +} + + +U32 XXH32(const void* input, unsigned int len, U32 seed) +{ +#if 0 + // Simple version, good for code maintenance, but unfortunately slow for small inputs + void* state = XXH32_init(seed); + XXH32_update(state, input, len); + return XXH32_digest(state); +#else + XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN; + +# if !defined(XXH_USE_UNALIGNED_ACCESS) + if ((((size_t)input) & 3) == 0) // Input is aligned, let's leverage the speed advantage + { + if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT) + return XXH32_endian_align(input, len, seed, XXH_littleEndian, XXH_aligned); + else + return XXH32_endian_align(input, len, seed, XXH_bigEndian, XXH_aligned); + } +# endif + + if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT) + return XXH32_endian_align(input, len, seed, XXH_littleEndian, XXH_unaligned); + else + return XXH32_endian_align(input, len, seed, XXH_bigEndian, XXH_unaligned); +#endif +} + +FORCE_INLINE U64 XXH64_endian_align(const void* input, unsigned int len, U64 seed, XXH_endianess endian, XXH_alignment align) +{ + const BYTE* p = (const BYTE*)input; + const BYTE* bEnd = p + len; + U64 h64; +#define XXH_get64bits(p) XXH_readLE64_align((const U64*)p, endian, align) + +#ifdef XXH_ACCEPT_NULL_INPUT_POINTER + if (p==NULL) { len=0; bEnd=p=(const BYTE*)(size_t)32; } +#endif + + if (len>=32) + { + const BYTE* const limit = bEnd - 32; + U64 v1 = seed + PRIME64_1 + PRIME64_2; + U64 v2 = seed + PRIME64_2; + U64 v3 = seed + 0; + U64 v4 = seed - PRIME64_1; + + do + { + v1 += XXH_get64bits(p) * PRIME64_2; p+=8; v1 = XXH_rotl64(v1, 31); v1 *= PRIME64_1; + v2 += XXH_get64bits(p) * PRIME64_2; p+=8; v2 = XXH_rotl64(v2, 31); v2 *= PRIME64_1; + v3 += XXH_get64bits(p) * PRIME64_2; p+=8; v3 = XXH_rotl64(v3, 31); v3 *= PRIME64_1; + v4 += XXH_get64bits(p) * PRIME64_2; p+=8; v4 = XXH_rotl64(v4, 31); v4 *= PRIME64_1; + } while (p<=limit); + + h64 = XXH_rotl64(v1, 1) + XXH_rotl64(v2, 7) + XXH_rotl64(v3, 12) + XXH_rotl64(v4, 18); + + v1 *= PRIME64_2; v1 = XXH_rotl64(v1, 31); v1 *= PRIME64_1; h64 ^= v1; + h64 = h64 * PRIME64_1 + PRIME64_4; + + v2 *= PRIME64_2; v2 = XXH_rotl64(v2, 31); v2 *= PRIME64_1; h64 ^= v2; + h64 = h64 * PRIME64_1 + PRIME64_4; + + v3 *= PRIME64_2; v3 = XXH_rotl64(v3, 31); v3 *= PRIME64_1; h64 ^= v3; + h64 = h64 * PRIME64_1 + PRIME64_4; + + v4 *= PRIME64_2; v4 = XXH_rotl64(v4, 31); v4 *= PRIME64_1; h64 ^= v4; + h64 = h64 * PRIME64_1 + PRIME64_4; + } + else + { + h64 = seed + PRIME64_5; + } + + h64 += (U64) len; + + while (p+8<=bEnd) + { + U64 k1 = XXH_get64bits(p); + k1 *= PRIME64_2; k1 = XXH_rotl64(k1,31); k1 *= PRIME64_1; h64 ^= k1; + h64 = XXH_rotl64(h64,27) * PRIME64_1 + PRIME64_4; + p+=8; + } + + if (p+4<=bEnd) + { + h64 ^= (U64)(XXH_get32bits(p)) * PRIME64_1; + h64 = XXH_rotl64(h64, 23) * PRIME64_2 + PRIME64_3; + p+=4; + } + + while (p> 33; + h64 *= PRIME64_2; + h64 ^= h64 >> 29; + h64 *= PRIME64_3; + h64 ^= h64 >> 32; + + return h64; +} + + +unsigned long long XXH64(const void* input, unsigned int len, unsigned long long seed) +{ + XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN; + +# if !defined(XXH_USE_UNALIGNED_ACCESS) + if ((((size_t)input) & 7)==0) // Input is aligned, let's leverage the speed advantage + { + if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT) + return XXH64_endian_align(input, len, seed, XXH_littleEndian, XXH_aligned); + else + return XXH64_endian_align(input, len, seed, XXH_bigEndian, XXH_aligned); + } +# endif + + if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT) + return XXH64_endian_align(input, len, seed, XXH_littleEndian, XXH_unaligned); + else + return XXH64_endian_align(input, len, seed, XXH_bigEndian, XXH_unaligned); +} + +//**************************** +// Advanced Hash Functions +//**************************** + +struct XXH_state32_t +{ + U64 total_len; + U32 seed; + U32 v1; + U32 v2; + U32 v3; + U32 v4; + int memsize; + char memory[16]; +}; + +struct XXH_state64_t +{ + U64 total_len; + U64 seed; + U64 v1; + U64 v2; + U64 v3; + U64 v4; + int memsize; + char memory[32]; +}; + + +int XXH32_sizeofState(void) +{ + XXH_STATIC_ASSERT(XXH32_SIZEOFSTATE >= sizeof(struct XXH_state32_t)); // A compilation error here means XXH32_SIZEOFSTATE is not large enough + return sizeof(struct XXH_state32_t); +} + +int XXH64_sizeofState(void) +{ + XXH_STATIC_ASSERT(XXH64_SIZEOFSTATE >= sizeof(struct XXH_state64_t)); // A compilation error here means XXH64_SIZEOFSTATE is not large enough + return sizeof(struct XXH_state64_t); +} + + +XXH_errorcode XXH32_resetState(void* state_in, U32 seed) +{ + struct XXH_state32_t * state = (struct XXH_state32_t *) state_in; + state->seed = seed; + state->v1 = seed + PRIME32_1 + PRIME32_2; + state->v2 = seed + PRIME32_2; + state->v3 = seed + 0; + state->v4 = seed - PRIME32_1; + state->total_len = 0; + state->memsize = 0; + return XXH_OK; +} + +XXH_errorcode XXH64_resetState(void* state_in, unsigned long long seed) +{ + struct XXH_state64_t * state = (struct XXH_state64_t *) state_in; + state->seed = seed; + state->v1 = seed + PRIME64_1 + PRIME64_2; + state->v2 = seed + PRIME64_2; + state->v3 = seed + 0; + state->v4 = seed - PRIME64_1; + state->total_len = 0; + state->memsize = 0; + return XXH_OK; +} + + +void* XXH32_init (U32 seed) +{ + void* state = XXH_malloc (sizeof(struct XXH_state32_t)); + if (state != NULL) XXH32_resetState(state, seed); + return state; +} + +void* XXH64_init (unsigned long long seed) +{ + void* state = XXH_malloc (sizeof(struct XXH_state64_t)); + if (state != NULL) XXH64_resetState(state, seed); + return state; +} + + +FORCE_INLINE XXH_errorcode XXH32_update_endian (void* state_in, const void* input, int len, XXH_endianess endian) +{ + struct XXH_state32_t * state = (struct XXH_state32_t *) state_in; + const BYTE* p = (const BYTE*)input; + const BYTE* const bEnd = p + len; + +#ifdef XXH_ACCEPT_NULL_INPUT_POINTER + if (input==NULL) return XXH_ERROR; +#endif + + state->total_len += len; + + if (state->memsize + len < 16) // fill in tmp buffer + { + XXH_memcpy(state->memory + state->memsize, input, len); + state->memsize += len; + return XXH_OK; + } + + if (state->memsize) // some data left from previous update + { + XXH_memcpy(state->memory + state->memsize, input, 16-state->memsize); + { + const U32* p32 = (const U32*)state->memory; + state->v1 += XXH_readLE32(p32, endian) * PRIME32_2; state->v1 = XXH_rotl32(state->v1, 13); state->v1 *= PRIME32_1; p32++; + state->v2 += XXH_readLE32(p32, endian) * PRIME32_2; state->v2 = XXH_rotl32(state->v2, 13); state->v2 *= PRIME32_1; p32++; + state->v3 += XXH_readLE32(p32, endian) * PRIME32_2; state->v3 = XXH_rotl32(state->v3, 13); state->v3 *= PRIME32_1; p32++; + state->v4 += XXH_readLE32(p32, endian) * PRIME32_2; state->v4 = XXH_rotl32(state->v4, 13); state->v4 *= PRIME32_1; p32++; + } + p += 16-state->memsize; + state->memsize = 0; + } + + if (p <= bEnd-16) + { + const BYTE* const limit = bEnd - 16; + U32 v1 = state->v1; + U32 v2 = state->v2; + U32 v3 = state->v3; + U32 v4 = state->v4; + + do + { + v1 += XXH_readLE32((const U32*)p, endian) * PRIME32_2; v1 = XXH_rotl32(v1, 13); v1 *= PRIME32_1; p+=4; + v2 += XXH_readLE32((const U32*)p, endian) * PRIME32_2; v2 = XXH_rotl32(v2, 13); v2 *= PRIME32_1; p+=4; + v3 += XXH_readLE32((const U32*)p, endian) * PRIME32_2; v3 = XXH_rotl32(v3, 13); v3 *= PRIME32_1; p+=4; + v4 += XXH_readLE32((const U32*)p, endian) * PRIME32_2; v4 = XXH_rotl32(v4, 13); v4 *= PRIME32_1; p+=4; + } while (p<=limit); + + state->v1 = v1; + state->v2 = v2; + state->v3 = v3; + state->v4 = v4; + } + + if (p < bEnd) + { + XXH_memcpy(state->memory, p, bEnd-p); + state->memsize = (int)(bEnd-p); + } + + return XXH_OK; +} + +XXH_errorcode XXH32_update (void* state_in, const void* input, unsigned int len) +{ + XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN; + + if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT) + return XXH32_update_endian(state_in, input, len, XXH_littleEndian); + else + return XXH32_update_endian(state_in, input, len, XXH_bigEndian); +} + + + +FORCE_INLINE U32 XXH32_intermediateDigest_endian (void* state_in, XXH_endianess endian) +{ + struct XXH_state32_t * state = (struct XXH_state32_t *) state_in; + const BYTE * p = (const BYTE*)state->memory; + BYTE* bEnd = (BYTE*)state->memory + state->memsize; + U32 h32; + + if (state->total_len >= 16) + { + h32 = XXH_rotl32(state->v1, 1) + XXH_rotl32(state->v2, 7) + XXH_rotl32(state->v3, 12) + XXH_rotl32(state->v4, 18); + } + else + { + h32 = state->seed + PRIME32_5; + } + + h32 += (U32) state->total_len; + + while (p+4<=bEnd) + { + h32 += XXH_readLE32((const U32*)p, endian) * PRIME32_3; + h32 = XXH_rotl32(h32, 17) * PRIME32_4; + p+=4; + } + + while (p> 15; + h32 *= PRIME32_2; + h32 ^= h32 >> 13; + h32 *= PRIME32_3; + h32 ^= h32 >> 16; + + return h32; +} + + +U32 XXH32_intermediateDigest (void* state_in) +{ + XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN; + + if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT) + return XXH32_intermediateDigest_endian(state_in, XXH_littleEndian); + else + return XXH32_intermediateDigest_endian(state_in, XXH_bigEndian); +} + + +U32 XXH32_digest (void* state_in) +{ + U32 h32 = XXH32_intermediateDigest(state_in); + + XXH_free(state_in); + + return h32; +} + + +FORCE_INLINE XXH_errorcode XXH64_update_endian (void* state_in, const void* input, int len, XXH_endianess endian) +{ + struct XXH_state64_t * state = (struct XXH_state64_t *) state_in; + const BYTE* p = (const BYTE*)input; + const BYTE* const bEnd = p + len; + +#ifdef XXH_ACCEPT_NULL_INPUT_POINTER + if (input==NULL) return XXH_ERROR; +#endif + + state->total_len += len; + + if (state->memsize + len < 32) // fill in tmp buffer + { + XXH_memcpy(state->memory + state->memsize, input, len); + state->memsize += len; + return XXH_OK; + } + + if (state->memsize) // some data left from previous update + { + XXH_memcpy(state->memory + state->memsize, input, 32-state->memsize); + { + const U64* p64 = (const U64*)state->memory; + state->v1 += XXH_readLE64(p64, endian) * PRIME64_2; state->v1 = XXH_rotl64(state->v1, 31); state->v1 *= PRIME64_1; p64++; + state->v2 += XXH_readLE64(p64, endian) * PRIME64_2; state->v2 = XXH_rotl64(state->v2, 31); state->v2 *= PRIME64_1; p64++; + state->v3 += XXH_readLE64(p64, endian) * PRIME64_2; state->v3 = XXH_rotl64(state->v3, 31); state->v3 *= PRIME64_1; p64++; + state->v4 += XXH_readLE64(p64, endian) * PRIME64_2; state->v4 = XXH_rotl64(state->v4, 31); state->v4 *= PRIME64_1; p64++; + } + p += 32-state->memsize; + state->memsize = 0; + } + + if (p+32 <= bEnd) + { + const BYTE* const limit = bEnd - 32; + U64 v1 = state->v1; + U64 v2 = state->v2; + U64 v3 = state->v3; + U64 v4 = state->v4; + + do + { + v1 += XXH_readLE64((const U64*)p, endian) * PRIME64_2; v1 = XXH_rotl64(v1, 31); v1 *= PRIME64_1; p+=8; + v2 += XXH_readLE64((const U64*)p, endian) * PRIME64_2; v2 = XXH_rotl64(v2, 31); v2 *= PRIME64_1; p+=8; + v3 += XXH_readLE64((const U64*)p, endian) * PRIME64_2; v3 = XXH_rotl64(v3, 31); v3 *= PRIME64_1; p+=8; + v4 += XXH_readLE64((const U64*)p, endian) * PRIME64_2; v4 = XXH_rotl64(v4, 31); v4 *= PRIME64_1; p+=8; + } while (p<=limit); + + state->v1 = v1; + state->v2 = v2; + state->v3 = v3; + state->v4 = v4; + } + + if (p < bEnd) + { + XXH_memcpy(state->memory, p, bEnd-p); + state->memsize = (int)(bEnd-p); + } + + return XXH_OK; +} + +XXH_errorcode XXH64_update (void* state_in, const void* input, unsigned int len) +{ + XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN; + + if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT) + return XXH64_update_endian(state_in, input, len, XXH_littleEndian); + else + return XXH64_update_endian(state_in, input, len, XXH_bigEndian); +} + + + +FORCE_INLINE U64 XXH64_intermediateDigest_endian (void* state_in, XXH_endianess endian) +{ + struct XXH_state64_t * state = (struct XXH_state64_t *) state_in; + const BYTE * p = (const BYTE*)state->memory; + BYTE* bEnd = (BYTE*)state->memory + state->memsize; + U64 h64; + + if (state->total_len >= 32) + { + U64 v1 = state->v1; + U64 v2 = state->v2; + U64 v3 = state->v3; + U64 v4 = state->v4; + + h64 = XXH_rotl64(v1, 1) + XXH_rotl64(v2, 7) + XXH_rotl64(v3, 12) + XXH_rotl64(v4, 18); + + v1 *= PRIME64_2; v1 = XXH_rotl64(v1, 31); v1 *= PRIME64_1; h64 ^= v1; + h64 = h64*PRIME64_1 + PRIME64_4; + + v2 *= PRIME64_2; v2 = XXH_rotl64(v2, 31); v2 *= PRIME64_1; h64 ^= v2; + h64 = h64*PRIME64_1 + PRIME64_4; + + v3 *= PRIME64_2; v3 = XXH_rotl64(v3, 31); v3 *= PRIME64_1; h64 ^= v3; + h64 = h64*PRIME64_1 + PRIME64_4; + + v4 *= PRIME64_2; v4 = XXH_rotl64(v4, 31); v4 *= PRIME64_1; h64 ^= v4; + h64 = h64*PRIME64_1 + PRIME64_4; + } + else + { + h64 = state->seed + PRIME64_5; + } + + h64 += (U64) state->total_len; + + while (p+8<=bEnd) + { + U64 k1 = XXH_readLE64((const U64*)p, endian); + k1 *= PRIME64_2; k1 = XXH_rotl64(k1,31); k1 *= PRIME64_1; h64 ^= k1; + h64 = XXH_rotl64(h64,27) * PRIME64_1 + PRIME64_4; + p+=8; + } + + if (p+4<=bEnd) + { + h64 ^= (U64)(XXH_readLE32((const U32*)p, endian)) * PRIME64_1; + h64 = XXH_rotl64(h64, 23) * PRIME64_2 + PRIME64_3; + p+=4; + } + + while (p> 33; + h64 *= PRIME64_2; + h64 ^= h64 >> 29; + h64 *= PRIME64_3; + h64 ^= h64 >> 32; + + return h64; +} + + +unsigned long long XXH64_intermediateDigest (void* state_in) +{ + XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN; + + if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT) + return XXH64_intermediateDigest_endian(state_in, XXH_littleEndian); + else + return XXH64_intermediateDigest_endian(state_in, XXH_bigEndian); +} + + +unsigned long long XXH64_digest (void* state_in) +{ + U64 h64 = XXH64_intermediateDigest(state_in); + + XXH_free(state_in); + + return h64; +} + -- cgit v0.12 From 562b34f660062f624535e8dff30da7dd0862869f Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sat, 13 Sep 2014 23:49:45 +0100 Subject: changed : lz4 test mode (-t) to no longer ask for confirmation, as suggested by Nguyen Thary --- programs/lz4cli.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/programs/lz4cli.c b/programs/lz4cli.c index 1bbeda0..70f0760 100644 --- a/programs/lz4cli.c +++ b/programs/lz4cli.c @@ -377,7 +377,7 @@ int main(int argc, char** argv) case 'c': forceStdout=1; output_filename=stdoutmark; displayLevel=1; break; // Test - case 't': decode=1; output_filename=nulmark; break; + case 't': decode=1; LZ4IO_setOverwrite(1); output_filename=nulmark; break; // Overwrite case 'f': LZ4IO_setOverwrite(1); break; -- cgit v0.12 From d71b9e25b729e92871cd6a9791170e334d5199d1 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 15 Sep 2014 00:59:30 +0100 Subject: small improvements to lz4frame compression --- NEWS | 4 ++ lz4frame.c | 158 +++++++++++++++++++++++++-------------------------- lz4frame.h | 2 +- programs/frametest.c | 14 +++-- 4 files changed, 90 insertions(+), 88 deletions(-) diff --git a/NEWS b/NEWS index 69e5c4e..ee4e40b 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,7 @@ +r123: +Added : experimental lz4frame API, thanks to Takayuki Matsuoka and Christopher Jackson for testings +Fix : test mode (-t) no longer requires confirmation, thanks to Thary Nguyen + r122: Fix : AIX & AIX64 support (SamG) Fix : mips 64-bits support (lew van) diff --git a/lz4frame.c b/lz4frame.c index 7bc2319..77b96c9 100644 --- a/lz4frame.c +++ b/lz4frame.c @@ -245,6 +245,7 @@ size_t LZ4F_compressFrame(void* dstBuffer, size_t dstMaxSize, const void* srcBuf cctxI.maxBufferSize = 64 KB; /* mess with real buffer size, to prevent allocation; works because autoflush==1 & stableSrc==1 */ prefs.autoFlush = 1; options.stableSrc = 1; + if (srcSize <= 64 KB) prefs.frameInfo.blockMode = blockIndependent; /* no need for linked blocks */ if (dstMaxSize < LZ4F_compressFrameBound(srcSize, &prefs)) return -ERROR_dstMaxSize_tooSmall; @@ -385,6 +386,27 @@ size_t LZ4F_compressBound(size_t srcSize, const LZ4F_preferences_t* preferencesP } +typedef int (*compressFunc_t)(void*, const char*, char*, int, int); + +static size_t LZ4F_compressBlock(void* dst, const void* src, size_t srcSize, compressFunc_t compress, void* lz4ctx) +{ + /* compress one block */ + BYTE* cSizePtr = (BYTE*)dst; + U32 cSize; + cSize = (U32)compress(lz4ctx, (const char*)src, (char*)(cSizePtr+4), (int)(srcSize), (int)(srcSize-1)); + LZ4F_writeLE32(cSizePtr, cSize); + if (cSize == 0) /* compression failed */ + { + cSize = srcSize; + LZ4F_writeLE32(cSizePtr, srcSize + LZ4F_BLOCKUNCOMPRESSED_FLAG); + memcpy(cSizePtr+4, src, srcSize); + } + return cSize + 4; +} + + +typedef enum { notDone, fromTmpBuffer, fromSrcBuffer } LZ4F_lastBlockStatus; + /* LZ4F_compressUpdate() * LZ4F_compressUpdate() can be called repetitively to compress as much data as necessary. * The most important rule is that dstBuffer MUST be large enough (dstMaxSize) to ensure compression completion even in worst case. @@ -403,8 +425,8 @@ size_t LZ4F_compressUpdate(LZ4F_compressionContext_t compressionContext, void* d const BYTE* const srcEnd = srcPtr + srcSize; BYTE* const dstStart = (BYTE*)dstBuffer; BYTE* dstPtr = dstStart; - U32 lastBlockCompressed = 0; - int (*compress)(void*, const char*, char*, int, int); + LZ4F_lastBlockStatus lastBlockCompressed = notDone; + compressFunc_t compress; if (cctxPtr->cStage != 1) return -ERROR_GENERIC; @@ -426,26 +448,17 @@ size_t LZ4F_compressUpdate(LZ4F_compressionContext_t compressionContext, void* d memcpy(cctxPtr->tmpIn + cctxPtr->tmpInSize, srcBuffer, srcSize); srcPtr = srcEnd; cctxPtr->tmpInSize += srcSize; + /* still needs some CRC */ } else { /* complete tmpIn block and then compress it */ - BYTE* cSizePtr = dstPtr; - U32 cSize; - lastBlockCompressed = 1; + lastBlockCompressed = fromTmpBuffer; memcpy(cctxPtr->tmpIn + cctxPtr->tmpInSize, srcBuffer, sizeToCopy); srcPtr += sizeToCopy; - dstPtr += 4; /* space for cSize */ - cSize = (U32)compress(&(cctxPtr->lz4ctx), (const char*)cctxPtr->tmpIn, (char*)dstPtr, (int)(blockSize), (int)(blockSize-1)); - dstPtr += cSize; - LZ4F_writeLE32(cSizePtr, cSize); - if (cSize == 0) /* compression failed : non compressible assumed */ - { - cSize = blockSize + LZ4F_BLOCKUNCOMPRESSED_FLAG; - LZ4F_writeLE32(cSizePtr, cSize); - memcpy(dstPtr, cctxPtr->tmpIn, blockSize); - dstPtr += blockSize; - } + + dstPtr += LZ4F_compressBlock(dstPtr, cctxPtr->tmpIn, blockSize, compress, &(cctxPtr->lz4ctx)); + if (cctxPtr->prefs.frameInfo.blockMode==blockLinked) cctxPtr->tmpIn += blockSize; cctxPtr->tmpInSize = 0; } @@ -453,59 +466,45 @@ size_t LZ4F_compressUpdate(LZ4F_compressionContext_t compressionContext, void* d while ((size_t)(srcEnd - srcPtr) >= blockSize) { - /* compress one block */ - BYTE* cSizePtr = dstPtr; - U32 cSize; - lastBlockCompressed = 2; - dstPtr += 4; /* space for cSizePtr */ - cSize = (U32)compress(&(cctxPtr->lz4ctx), (const char*)srcPtr, (char*)dstPtr, (int)(blockSize), (int)(blockSize-1)); - dstPtr += cSize; - LZ4F_writeLE32(cSizePtr, cSize); - if (cSize == 0) /* compression failed */ - { - cSize = blockSize + LZ4F_BLOCKUNCOMPRESSED_FLAG; - LZ4F_writeLE32(cSizePtr, cSize); - memcpy(dstPtr, srcPtr, blockSize); - dstPtr += blockSize; - } + /* compress full block */ + lastBlockCompressed = fromSrcBuffer; + dstPtr += LZ4F_compressBlock(dstPtr, srcPtr, blockSize, compress, &(cctxPtr->lz4ctx)); srcPtr += blockSize; } if ((cctxPtr->prefs.autoFlush) && (srcPtr < srcEnd)) { - /* compress remaining input */ - BYTE* cSizePtr = dstPtr; - U32 cSize; - size_t iSize = srcEnd - srcPtr; - lastBlockCompressed = 2; - dstPtr += 4; /* space for cSizePtr */ - cSize = (U32)compress(&(cctxPtr->lz4ctx), (const char*)srcPtr, (char*)dstPtr, (int)(iSize), (int)(iSize-1)); - dstPtr += cSize; - LZ4F_writeLE32(cSizePtr, cSize); - if (cSize == 0) /* compression failed */ - { - cSize = iSize + LZ4F_BLOCKUNCOMPRESSED_FLAG; - LZ4F_writeLE32(cSizePtr, cSize); - memcpy(dstPtr, srcPtr, iSize); - dstPtr += iSize; - } - srcPtr += iSize; + /* compress remaining input < blockSize */ + lastBlockCompressed = fromSrcBuffer; + dstPtr += LZ4F_compressBlock(dstPtr, srcPtr, srcEnd - srcPtr, compress, &(cctxPtr->lz4ctx)); + srcPtr = srcEnd; } - /* save last input up to 64 KB for dictionary */ - if ((cctxPtr->prefs.frameInfo.blockMode == blockLinked) && (lastBlockCompressed) && (!compressOptionsPtr->stableSrc)) + /* preserve dictionary if necessary */ + if ((cctxPtr->prefs.frameInfo.blockMode==blockLinked) && (lastBlockCompressed==fromSrcBuffer)) { - if ((lastBlockCompressed==2) || - ((cctxPtr->tmpBuff + cctxPtr->maxBufferSize) < (cctxPtr->tmpIn + cctxPtr->maxBlockSize))) + if (compressOptionsPtr->stableSrc) { - int result; - result = LZ4_saveDict (&(cctxPtr->lz4ctx), (char*)(cctxPtr->tmpBuff), 64 KB); - if (result==0) return -ERROR_GENERIC; - cctxPtr->tmpIn = cctxPtr->tmpBuff + result; + cctxPtr->tmpIn = cctxPtr->tmpBuff; } + else + { + int realDictSize; + realDictSize = LZ4_saveDict (&(cctxPtr->lz4ctx), (char*)(cctxPtr->tmpBuff), 64 KB); + if (realDictSize==0) return -ERROR_GENERIC; + cctxPtr->tmpIn = cctxPtr->tmpBuff + realDictSize; + } + } + + /* keep tmpIn within limits */ + if ((cctxPtr->tmpIn + blockSize) > (cctxPtr->tmpBuff + cctxPtr->maxBufferSize)) /* necessarily blockLinked && lastBlockCompressed==fromTmpBuffer */ + { + LZ4_saveDict (&(cctxPtr->lz4ctx), (char*)(cctxPtr->tmpBuff), 64 KB); + cctxPtr->tmpIn = cctxPtr->tmpBuff + 64 KB; } - if (srcPtr < srcEnd) /* some input data left, necessarily < blockSize */ + /* some input data left, necessarily < blockSize */ + if (srcPtr < srcEnd) { /* fill tmp buffer */ size_t sizeToCopy = srcEnd - srcPtr; @@ -547,31 +546,16 @@ size_t LZ4F_flush(LZ4F_compressionContext_t compressionContext, void* dstBuffer, (int(*)(void*,const char*,char*,int,int))LZ4_compress_limitedOutput_continue : LZ4_compress_limitedOutput_withState; - { - BYTE* cSizePtr = dstPtr; - U32 cSize; - dstPtr += 4; /* space for cSizePtr */ - cSize = (U32)compress(&(cctxPtr->lz4ctx), (const char*)cctxPtr->tmpIn, (char*)dstPtr, (int)(cctxPtr->tmpInSize), (int)(cctxPtr->tmpInSize-1)); - dstPtr += cSize; - LZ4F_writeLE32(cSizePtr, cSize); - if (cSize == 0) /* compression failed */ - { - cSize = cctxPtr->tmpInSize + LZ4F_BLOCKUNCOMPRESSED_FLAG; - LZ4F_writeLE32(cSizePtr, cSize); - memcpy(dstPtr, cctxPtr->tmpIn, cctxPtr->tmpInSize); - dstPtr += cctxPtr->tmpInSize; - } - if (cctxPtr->prefs.frameInfo.blockMode==blockLinked) cctxPtr->tmpIn += cctxPtr->tmpInSize; - cctxPtr->tmpInSize = 0; - } + /* compress tmp buffer */ + dstPtr += LZ4F_compressBlock(dstPtr, cctxPtr->tmpIn, cctxPtr->tmpInSize, compress, &(cctxPtr->lz4ctx)); + if (cctxPtr->prefs.frameInfo.blockMode==blockLinked) cctxPtr->tmpIn += cctxPtr->tmpInSize; + cctxPtr->tmpInSize = 0; - if ((cctxPtr->prefs.frameInfo.blockMode == blockLinked) - && ((cctxPtr->tmpBuff + cctxPtr->maxBufferSize) < (cctxPtr->tmpIn + cctxPtr->maxBlockSize))) + /* keep tmpIn within limits */ + if ((cctxPtr->tmpIn + cctxPtr->maxBlockSize) > (cctxPtr->tmpBuff + cctxPtr->maxBufferSize)) /* necessarily blockLinked */ { - /* last 64 KB of input become dictionary */ - int result = LZ4_saveDict (&(cctxPtr->lz4ctx), (char*)(cctxPtr->tmpBuff), 64 KB); - if (!result) return ERROR_GENERIC; - cctxPtr->tmpIn = cctxPtr->tmpBuff + result; + LZ4_saveDict (&(cctxPtr->lz4ctx), (char*)(cctxPtr->tmpBuff), 64 KB); + cctxPtr->tmpIn = cctxPtr->tmpBuff + 64 KB; } return dstPtr - dstStart; @@ -1114,8 +1098,20 @@ size_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContext, } } - if ((dctxPtr->frameInfo.blockMode==blockLinked) && (dctxPtr->dict != dctxPtr->tmpOutBuffer)) + if ( (dctxPtr->frameInfo.blockMode==blockLinked) + &&(dctxPtr->dict != dctxPtr->tmpOutBuffer) + ) LZ4F_saveDict(dctxPtr, NULL, 0); + //(!decompressOptionsPtr->stableDst + 1) ) + /*{ + size_t newDictSize = dctxPtr->dictSize; + BYTE* oldDictEnd = dctxPtr->dict + dctxPtr->dictSize; + if ((newDictSize) > 64 KB) newDictSize = 64 KB; + memcpy(dctxPtr->tmpOutBuffer, oldDictEnd - newDictSize, newDictSize); + dctxPtr->dict = dctxPtr->tmpOutBuffer; + dctxPtr->dictSize = newDictSize; + dctxPtr->tmpOut = dctxPtr->tmpOutBuffer + newDictSize; + }*/ if (srcPtrsrcExpect = srcPtr; diff --git a/lz4frame.h b/lz4frame.h index 0769f6a..4bc8c59 100644 --- a/lz4frame.h +++ b/lz4frame.h @@ -187,7 +187,7 @@ size_t LZ4F_compressEnd(LZ4F_compressionContext_t compressionContext, void* dstB * The result of the function is the number of bytes written into dstBuffer (necessarily >= 4 (endMark size)) * The function outputs an error code if it fails (can be tested using LZ4F_isError()) * The LZ4F_compressOptions_t structure is optional : you can provide NULL as argument. - * compressionContext can then be used again, starting with LZ4F_compressBegin(). The preferences will remain the same. + * compressionContext can then be used again, starting with LZ4F_compressBegin(). */ diff --git a/programs/frametest.c b/programs/frametest.c index 4d09411..9f14bfb 100644 --- a/programs/frametest.c +++ b/programs/frametest.c @@ -406,7 +406,8 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi unsigned CCflag = FUZ_rand(&randState) & 1; unsigned autoflush = (FUZ_rand(&randState) & 7) == 2; LZ4F_preferences_t prefs = { 0 }; - LZ4F_compressOptions_t options = { 0 }; + LZ4F_compressOptions_t cOptions = { 0 }; + LZ4F_decompressOptions_t dOptions = { 0 }; unsigned nbBits = (FUZ_rand(&randState) % (FUZ_highbit(srcDataLength-1) - 1)) + 1; size_t srcSize = (FUZ_rand(&randState) & ((1< (size_t)(iend-ip)) iSize = iend-ip; - options.stableSrc = ((FUZ_rand(&randState) && 3) == 2); + cOptions.stableSrc = ((FUZ_rand(&randState) && 3) == 1); - result = LZ4F_compressUpdate(cCtx, op, oSize, ip, iSize, &options); + result = LZ4F_compressUpdate(cCtx, op, oSize, ip, iSize, &cOptions); CHECK(LZ4F_isError(result), "Compression failed (error %i)", (int)result); op += result; ip += iSize; if (forceFlush) { - result = LZ4F_flush(cCtx, op, oend-op, &options); + result = LZ4F_flush(cCtx, op, oend-op, &cOptions); CHECK(LZ4F_isError(result), "Compression failed (error %i)", (int)result); op += result; } } - result = LZ4F_compressEnd(cCtx, op, oend-op, &options); + result = LZ4F_compressEnd(cCtx, op, oend-op, &cOptions); CHECK(LZ4F_isError(result), "Compression completion failed (error %i)", (int)result); op += result; cSize = op-(BYTE*)compressedBuffer; @@ -471,7 +472,8 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi size_t oSize = (FUZ_rand(&randState) & ((1< (size_t)(iend-ip)) iSize = iend-ip; if (oSize > (size_t)(oend-op)) oSize = oend-op; - result = LZ4F_decompress(dCtx, op, &oSize, ip, &iSize, NULL); + dOptions.stableDst = FUZ_rand(&randState) & 1; + result = LZ4F_decompress(dCtx, op, &oSize, ip, &iSize, &dOptions); if (result == (size_t)-ERROR_checksum_invalid) locateBuffDiff((BYTE*)srcBuffer+srcStart, decodedBuffer, srcSize); CHECK(LZ4F_isError(result), "Decompression failed (error %i)", (int)result); op += oSize; -- cgit v0.12 From eeb9011467d4064eca754bd32e629e88fa5ec8f3 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sun, 21 Sep 2014 09:56:21 +0100 Subject: lz4frame : support stableDst option Multiple bugfixes within lz4frame decompression Small decompression speed improvements Improved fuzzer test, with more thorough and complex tests --- lz4.c | 4 +- lz4frame.c | 257 ++++++++++++++++++++++++++++++++++++--------------- lz4frame.h | 23 +++-- programs/frametest.c | 45 ++++++--- programs/fullbench.c | 6 +- 5 files changed, 235 insertions(+), 100 deletions(-) diff --git a/lz4.c b/lz4.c index b39a91d..1c8e416 100644 --- a/lz4.c +++ b/lz4.c @@ -1155,7 +1155,9 @@ Advanced decoding functions : FORCE_INLINE int LZ4_decompress_usingDict_generic(const char* source, char* dest, int compressedSize, int maxOutputSize, int safe, const char* dictStart, int dictSize) { - if ((dictStart+dictSize == source) && (dictSize >= (int)(64 KB - 1))) + if (dictSize==0) + return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, safe, full, 0, noDict, NULL, 64 KB); + if ((dictStart+dictSize == dest) && (dictSize >= (int)(64 KB - 1))) return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, safe, full, 0, withPrefix64k, NULL, 64 KB); return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, safe, full, 0, usingExtDict, dictStart, dictSize); } diff --git a/lz4frame.c b/lz4frame.c index 77b96c9..7e06a4e 100644 --- a/lz4frame.c +++ b/lz4frame.c @@ -152,6 +152,37 @@ typedef struct { /************************************** + Error management +**************************************/ +#define LZ4F_GENERATE_STRING(STRING) #STRING, +static const char* LZ4F_errorStrings[] = { LZ4F_LIST_ERRORS(LZ4F_GENERATE_STRING) }; + +/* +typedef enum { OK_NoError=0, ERROR_GENERIC = 1, + ERROR_maxBlockSize_invalid, ERROR_blockMode_invalid, ERROR_contentChecksumFlag_invalid, + ERROR_compressionLevel_invalid, + ERROR_allocation_failed, + ERROR_srcSize_tooLarge, ERROR_dstMaxSize_tooSmall, + ERROR_decompressionFailed, + ERROR_checksum_invalid, + ERROR_maxCode + } LZ4F_errorCodes; error codes are negative unsigned values. + Compare function result to (-specificCode) */ + +int LZ4F_isError(LZ4F_errorCode_t code) +{ + return (code > (LZ4F_errorCode_t)(-ERROR_maxCode)); +} + +const char* LZ4F_getErrorName(LZ4F_errorCode_t code) +{ + static const char* codeError = "Unspecified error code"; + if (LZ4F_isError(code)) return LZ4F_errorStrings[-code]; + return codeError; +} + + +/************************************** Private functions **************************************/ static size_t LZ4F_getBlockSize(unsigned blockSizeID) @@ -191,16 +222,6 @@ static BYTE LZ4F_headerChecksum (const BYTE* header, size_t length) } - -/************************************** - Error management -**************************************/ -int LZ4F_isError(LZ4F_errorCode_t code) -{ - return (code > (LZ4F_errorCode_t)(-ERROR_maxCode)); -} - - /************************************** Simple compression functions **************************************/ @@ -693,9 +714,13 @@ static size_t LZ4F_decodeHeader(LZ4F_dctx_internal_t* dctxPtr, const BYTE* srcPt dctxPtr->tmpOutBuffer= ALLOCATOR(dctxPtr->maxBufferSize); if (dctxPtr->tmpOutBuffer== NULL) return -ERROR_GENERIC; } + dctxPtr->tmpInSize = 0; + dctxPtr->tmpInTarget = 0; dctxPtr->dict = dctxPtr->tmpOutBuffer; dctxPtr->dictSize = 0; dctxPtr->tmpOut = dctxPtr->tmpOutBuffer; + dctxPtr->tmpOutStart = 0; + dctxPtr->tmpOutSize = 0; return 7; } @@ -704,7 +729,8 @@ static size_t LZ4F_decodeHeader(LZ4F_dctx_internal_t* dctxPtr, const BYTE* srcPt typedef enum { dstage_getHeader=0, dstage_storeHeader, dstage_decodeHeader, dstage_getCBlockSize, dstage_storeCBlockSize, dstage_decodeCBlockSize, dstage_copyDirect, - dstage_getCBlock, dstage_storeCBlock, dstage_decodeCBlock, dstage_flushOut, + dstage_getCBlock, dstage_storeCBlock, dstage_decodeCBlock, + dstage_decodeCBlock_intoDst, dstage_decodeCBlock_intoTmp, dstage_flushOut, dstage_getSuffix, dstage_storeSuffix, dstage_checkSuffix } dStage_t; @@ -740,62 +766,74 @@ LZ4F_errorCode_t LZ4F_getFrameInfo(LZ4F_decompressionContext_t decompressionCont } -static void LZ4F_saveDict(LZ4F_dctx_internal_t* dctxPtr, const BYTE* decoded, size_t decodedSize) +static int LZ4F_decompress_safe (const char* source, char* dest, int compressedSize, int maxDecompressedSize, const char* dictStart, int dictSize) { - size_t newDictSize = decodedSize; - size_t preserveDictSize; - if (newDictSize > 64 KB) newDictSize = 64 KB; - preserveDictSize = 64 KB - newDictSize; - if (preserveDictSize > dctxPtr->dictSize) preserveDictSize = dctxPtr->dictSize; - - memmove(dctxPtr->tmpOutBuffer, dctxPtr->dict + dctxPtr->dictSize - preserveDictSize, preserveDictSize); - memmove(dctxPtr->tmpOutBuffer + preserveDictSize, decoded + decodedSize - newDictSize, newDictSize); - - dctxPtr->dict = dctxPtr->tmpOutBuffer; - dctxPtr->dictSize = preserveDictSize + newDictSize; - dctxPtr->tmpOut = dctxPtr->tmpOutBuffer + dctxPtr->dictSize; + (void)dictStart; (void)dictSize; + return LZ4_decompress_safe (source, dest, compressedSize, maxDecompressedSize); } -static void LZ4F_pointDict(LZ4F_dctx_internal_t* dctxPtr, const BYTE* decoded, size_t decodedSize) + +static void LZ4F_updateDict(LZ4F_dctx_internal_t* dctxPtr, const BYTE* dstPtr, size_t dstSize, const BYTE* dstPtr0, unsigned withinTmp) { - /* decoded block in the continuity of dictionary */ - if (dctxPtr->dict + dctxPtr->dictSize == decoded) + if (dctxPtr->dictSize==0) + dctxPtr->dict = (BYTE*)dstPtr; /* priority to dictionary continuity */ + + if (dctxPtr->dict + dctxPtr->dictSize == dstPtr) /* dictionary continuity */ { - dctxPtr->dictSize += decodedSize; - if (dctxPtr->dict == dctxPtr->tmpOutBuffer) /* extended tmp buffer, don't go beyond 128 KB == maxDictSize */ - { - if (dctxPtr->dictSize > 128 KB) - { - memcpy(dctxPtr->tmpOutBuffer, dctxPtr->tmpOutBuffer + dctxPtr->dictSize - 64 KB, 64 KB); - dctxPtr->dictSize = 64 KB; - } - dctxPtr->tmpOut = dctxPtr->tmpOutBuffer + dctxPtr->dictSize; - } + dctxPtr->dictSize += dstSize; return; } - /* large decoded block */ - if (decodedSize >= (64 KB - 1)) + if (dstPtr - dstPtr0 + dstSize >= 64 KB) /* dstBuffer large enough to become dictionary */ { - dctxPtr->dict = (BYTE*)decoded; - dctxPtr->dictSize = decodedSize; - dctxPtr->tmpOut = dctxPtr->tmpOutBuffer + 64 KB; + dctxPtr->dict = (BYTE*)dstPtr0; + dctxPtr->dictSize = dstPtr - dstPtr0 + dstSize; return; } - /* small block, and not contiguous : let's save that */ - LZ4F_saveDict(dctxPtr, decoded, decodedSize); -} + if ((withinTmp) && (dctxPtr->dict == dctxPtr->tmpOutBuffer)) + { + /* assumption : dctxPtr->dict + dctxPtr->dictSize == dctxPtr->tmpOut + dctxPtr->tmpOutStart */ + dctxPtr->dictSize += dstSize; + return; + } + if (withinTmp) /* copy relevant dict portion in front of tmpOut within tmpOutBuffer */ + { + size_t savedDictSize = dctxPtr->tmpOut - dctxPtr->tmpOutBuffer; + memcpy(dctxPtr->tmpOutBuffer, dctxPtr->dict + dctxPtr->dictSize - dctxPtr->tmpOutStart- savedDictSize, savedDictSize); + dctxPtr->dict = dctxPtr->tmpOutBuffer; + dctxPtr->dictSize = savedDictSize + dctxPtr->tmpOutStart + dstSize; + return; + } -static int LZ4F_decompress_safe (const char* source, char* dest, int compressedSize, int maxDecompressedSize, const char* dictStart, int dictSize) -{ - (void)dictStart; (void)dictSize; - return LZ4_decompress_safe (source, dest, compressedSize, maxDecompressedSize); + if (dctxPtr->dict == dctxPtr->tmpOutBuffer) /* copy dst into tmp to complete dict */ + { + if (dctxPtr->dictSize + dstSize > dctxPtr->maxBufferSize) /* tmp buffer not large enough */ + { + size_t preserveSize = 64 KB - dstSize; /* note : dstSize < 64 KB */ + memcpy(dctxPtr->dict, dctxPtr->dict + dctxPtr->dictSize - preserveSize, preserveSize); + dctxPtr->dictSize = preserveSize; + } + memcpy(dctxPtr->dict + dctxPtr->dictSize, dstPtr, dstSize); + dctxPtr->dictSize += dstSize; + return; + } + + /* join dict & dest into tmp */ + { + size_t preserveSize = 64 KB - dstSize; /* note : dstSize < 64 KB */ + if (preserveSize > dctxPtr->dictSize) preserveSize = dctxPtr->dictSize; + memcpy(dctxPtr->tmpOutBuffer, dctxPtr->dict + dctxPtr->dictSize - preserveSize, preserveSize); + memcpy(dctxPtr->tmpOutBuffer + preserveSize, dstPtr, dstSize); + dctxPtr->dict = dctxPtr->tmpOutBuffer; + dctxPtr->dictSize = preserveSize + dstSize; + } } + /* LZ4F_decompress() * Call this function repetitively to regenerate data compressed within srcBuffer. * The function will attempt to decode *srcSizePtr from srcBuffer, into dstBuffer of maximum size *dstSizePtr. @@ -952,7 +990,11 @@ size_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContext, if ((size_t)(dstEnd-dstPtr) < sizeToCopy) sizeToCopy = dstEnd - dstPtr; memcpy(dstPtr, srcPtr, sizeToCopy); if (dctxPtr->frameInfo.contentChecksumFlag) XXH32_update(&(dctxPtr->xxh), srcPtr, sizeToCopy); - if (dctxPtr->frameInfo.blockMode==blockLinked) LZ4F_pointDict(dctxPtr, srcPtr, sizeToCopy); + + /* dictionary management */ + if (dctxPtr->frameInfo.blockMode==blockLinked) + LZ4F_updateDict(dctxPtr, dstPtr, sizeToCopy, dstStart, 0); + srcPtr += sizeToCopy; dstPtr += sizeToCopy; if (sizeToCopy == dctxPtr->tmpInTarget) /* all copied */ @@ -1000,6 +1042,15 @@ size_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContext, case dstage_decodeCBlock: { + if ((size_t)(dstEnd-dstPtr) < dctxPtr->maxBlockSize) /* not enough place into dst : decode into tmpOut */ + dctxPtr->dStage = dstage_decodeCBlock_intoTmp; + else + dctxPtr->dStage = dstage_decodeCBlock_intoDst; + break; + } + + case dstage_decodeCBlock_intoDst: + { int (*decoder)(const char*, char*, int, int, const char*, int); int decodedSize; @@ -1008,35 +1059,76 @@ size_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContext, else decoder = LZ4F_decompress_safe; - if ((size_t)(dstEnd-dstPtr) < dctxPtr->maxBlockSize) /* not enough place into dst : decode into tmpOut */ - { - decodedSize = decoder((const char*)selectedIn, (char*)dctxPtr->tmpOut, (int)dctxPtr->tmpInTarget, (int)dctxPtr->maxBlockSize, (const char*)dctxPtr->dict, (int)dctxPtr->dictSize); - if (decodedSize < 0) return -ERROR_GENERIC; /* decompression failed */ - if (dctxPtr->frameInfo.contentChecksumFlag) XXH32_update(&(dctxPtr->xxh), dctxPtr->tmpOut, decodedSize); - dctxPtr->tmpOutSize = decodedSize; - dctxPtr->tmpOutStart = 0; - dctxPtr->dStage = dstage_flushOut; - break; - } decodedSize = decoder((const char*)selectedIn, (char*)dstPtr, (int)dctxPtr->tmpInTarget, (int)dctxPtr->maxBlockSize, (const char*)dctxPtr->dict, (int)dctxPtr->dictSize); if (decodedSize < 0) return -ERROR_GENERIC; /* decompression failed */ if (dctxPtr->frameInfo.contentChecksumFlag) XXH32_update(&(dctxPtr->xxh), dstPtr, decodedSize); - if (dctxPtr->frameInfo.blockMode==blockLinked) LZ4F_pointDict(dctxPtr, dstPtr, decodedSize); + + /* dictionary management */ + if (dctxPtr->frameInfo.blockMode==blockLinked) + LZ4F_updateDict(dctxPtr, dstPtr, decodedSize, dstStart, 0); + dstPtr += decodedSize; dctxPtr->dStage = dstage_getCBlockSize; break; } + case dstage_decodeCBlock_intoTmp: + { + /* not enough place into dst : decode into tmpOut */ + int (*decoder)(const char*, char*, int, int, const char*, int); + int decodedSize; + + if (dctxPtr->frameInfo.blockMode == blockLinked) + decoder = LZ4_decompress_safe_usingDict; + else + decoder = LZ4F_decompress_safe; + + /* ensure enough place for tmpOut */ + if (dctxPtr->frameInfo.blockMode == blockLinked) + { + if (dctxPtr->dict == dctxPtr->tmpOutBuffer) + { + if (dctxPtr->dictSize > 128 KB) + { + memcpy(dctxPtr->dict, dctxPtr->dict + dctxPtr->dictSize - 64 KB, 64 KB); + dctxPtr->dictSize = 64 KB; + } + dctxPtr->tmpOut = dctxPtr->dict + dctxPtr->dictSize; + } + else /* dict not within tmp */ + { + size_t reservedDictSpace = dctxPtr->dictSize; + if (reservedDictSpace > 64 KB) reservedDictSpace = 64 KB; + dctxPtr->tmpOut = dctxPtr->tmpOutBuffer + reservedDictSpace; + } + } + + /* Decode */ + decodedSize = decoder((const char*)selectedIn, (char*)dctxPtr->tmpOut, (int)dctxPtr->tmpInTarget, (int)dctxPtr->maxBlockSize, (const char*)dctxPtr->dict, (int)dctxPtr->dictSize); + if (decodedSize < 0) return -ERROR_decompressionFailed; /* decompression failed */ + if (dctxPtr->frameInfo.contentChecksumFlag) XXH32_update(&(dctxPtr->xxh), dctxPtr->tmpOut, decodedSize); + dctxPtr->tmpOutSize = decodedSize; + dctxPtr->tmpOutStart = 0; + dctxPtr->dStage = dstage_flushOut; + break; + } + case dstage_flushOut: /* flush decoded data from tmpOut to dstBuffer */ { size_t sizeToCopy = dctxPtr->tmpOutSize - dctxPtr->tmpOutStart; if (sizeToCopy > (size_t)(dstEnd-dstPtr)) sizeToCopy = dstEnd-dstPtr; memcpy(dstPtr, dctxPtr->tmpOut + dctxPtr->tmpOutStart, sizeToCopy); + + /* dictionary management */ + if (dctxPtr->frameInfo.blockMode==blockLinked) + LZ4F_updateDict(dctxPtr, dstPtr, sizeToCopy, dstStart, 1); + dctxPtr->tmpOutStart += sizeToCopy; dstPtr += sizeToCopy; + + /* end of flush ? */ if (dctxPtr->tmpOutStart == dctxPtr->tmpOutSize) { - if (dctxPtr->frameInfo.blockMode==blockLinked) LZ4F_pointDict(dctxPtr, dctxPtr->tmpOut, dctxPtr->tmpOutSize); dctxPtr->dStage = dstage_getCBlockSize; break; } @@ -1045,7 +1137,7 @@ size_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContext, break; } - case dstage_getSuffix: + case dstage_getSuffix: { size_t suffixSize = dctxPtr->frameInfo.contentChecksumFlag * 4; if (suffixSize == 0) /* frame completed */ @@ -1098,20 +1190,35 @@ size_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContext, } } + /* preserve dictionary within tmp if necessary */ if ( (dctxPtr->frameInfo.blockMode==blockLinked) &&(dctxPtr->dict != dctxPtr->tmpOutBuffer) - ) - LZ4F_saveDict(dctxPtr, NULL, 0); - //(!decompressOptionsPtr->stableDst + 1) ) - /*{ - size_t newDictSize = dctxPtr->dictSize; - BYTE* oldDictEnd = dctxPtr->dict + dctxPtr->dictSize; - if ((newDictSize) > 64 KB) newDictSize = 64 KB; - memcpy(dctxPtr->tmpOutBuffer, oldDictEnd - newDictSize, newDictSize); - dctxPtr->dict = dctxPtr->tmpOutBuffer; - dctxPtr->dictSize = newDictSize; - dctxPtr->tmpOut = dctxPtr->tmpOutBuffer + newDictSize; - }*/ + &&(!decompressOptionsPtr->stableDst) + ) + { + if (dctxPtr->dStage == dstage_flushOut) + { + size_t preserveSize = dctxPtr->tmpOut - dctxPtr->tmpOutBuffer; + BYTE* oldDictEnd = dctxPtr->dict + dctxPtr->dictSize - dctxPtr->tmpOutStart; + + memcpy(dctxPtr->tmpOutBuffer, oldDictEnd - preserveSize, preserveSize); + + dctxPtr->dict = dctxPtr->tmpOutBuffer; + dctxPtr->dictSize = preserveSize + dctxPtr->tmpOutStart; + } + else + { + size_t newDictSize = dctxPtr->dictSize; + BYTE* oldDictEnd = dctxPtr->dict + dctxPtr->dictSize; + if ((newDictSize) > 64 KB) newDictSize = 64 KB; + + memcpy(dctxPtr->tmpOutBuffer, oldDictEnd - newDictSize, newDictSize); + + dctxPtr->dict = dctxPtr->tmpOutBuffer; + dctxPtr->dictSize = newDictSize; + dctxPtr->tmpOut = dctxPtr->tmpOutBuffer + newDictSize; + } + } if (srcPtrsrcExpect = srcPtr; diff --git a/lz4frame.h b/lz4frame.h index 4bc8c59..d24a824 100644 --- a/lz4frame.h +++ b/lz4frame.h @@ -59,18 +59,21 @@ extern "C" { Error management **************************************/ typedef size_t LZ4F_errorCode_t; -typedef enum { OK_FrameEnd = 1 } LZ4F_successCodes; -typedef enum { OK_NoError = 0, ERROR_GENERIC = 1, - ERROR_maxBlockSize_invalid, ERROR_blockMode_invalid, ERROR_contentChecksumFlag_invalid, - ERROR_compressionLevel_invalid, - ERROR_allocation_failed, - ERROR_srcSize_tooLarge, ERROR_dstMaxSize_tooSmall, - ERROR_checksum_invalid, - ERROR_maxCode - } LZ4F_errorCodes; /* error codes are negative unsigned values. - Compare function result to (-specificCode) */ +#define LZ4F_LIST_ERRORS(ITEM) \ + ITEM(OK_NoError) ITEM(ERROR_GENERIC) \ + ITEM(ERROR_maxBlockSize_invalid) ITEM(ERROR_blockMode_invalid) ITEM(ERROR_contentChecksumFlag_invalid) \ + ITEM(ERROR_compressionLevel_invalid) \ + ITEM(ERROR_allocation_failed) \ + ITEM(ERROR_srcSize_tooLarge) ITEM(ERROR_dstMaxSize_tooSmall) \ + ITEM(ERROR_decompressionFailed) \ + ITEM(ERROR_checksum_invalid) \ + ITEM(ERROR_maxCode) + +#define LZ4F_GENERATE_ENUM(ENUM) ENUM, +typedef enum { LZ4F_LIST_ERRORS(LZ4F_GENERATE_ENUM) } LZ4F_errorCodes; /* enum is exposed, to let programmer detect & handle specific errors */ int LZ4F_isError(LZ4F_errorCode_t code); /* Basically : code > -ERROR_maxCode */ +const char* LZ4F_getErrorName(LZ4F_errorCode_t code); /* return enum as string */ /************************************** diff --git a/programs/frametest.c b/programs/frametest.c index 9f14bfb..03b47e8 100644 --- a/programs/frametest.c +++ b/programs/frametest.c @@ -105,6 +105,7 @@ static U32 g_time = 0; static U32 no_prompt = 0; static char* programName; static U32 displayLevel = 2; +static U32 pause = 0; /********************************************************* @@ -355,13 +356,14 @@ _output_error: } -static void locateBuffDiff(const void* buff1, const void* buff2, size_t size) +static void locateBuffDiff(const void* buff1, const void* buff2, size_t size, unsigned nonContiguous) { int p=0; BYTE* b1=(BYTE*)buff1; BYTE* b2=(BYTE*)buff2; + if (nonContiguous) { DISPLAY("Non-contiguous output test (%i bytes)\n", (int)size); return; } while (b1[p]==b2[p]) p++; - printf("Error at pos %i/%i : %02X != %02X \n", p, (int)size, b1[p], b2[p]); + DISPLAY("Error at pos %i/%i : %02X != %02X \n", p, (int)size, b1[p], b2[p]); } @@ -378,6 +380,7 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi LZ4F_decompressionContext_t dCtx; LZ4F_compressionContext_t cCtx; size_t result; + XXH64_stateSpace_t xxh64; # define CHECK(cond, ...) if (cond) { DISPLAY("Error => "); DISPLAY(__VA_ARGS__); \ DISPLAY(" (seed %u, test nb %i) \n", seed, testNb); goto _output_error; } @@ -414,6 +417,7 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi size_t cSize; U64 crcOrig, crcDecoded; + (void)FUZ_rand(&coreRand); // update rand seed prefs.frameInfo.blockMode = BMId; prefs.frameInfo.blockSizeID = BSId; prefs.frameInfo.contentChecksumFlag = CCflag; @@ -464,6 +468,9 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi BYTE* op = decodedBuffer; BYTE* const oend = op + srcDataLength; unsigned maxBits = FUZ_highbit(cSize); + unsigned nonContiguousDst = (FUZ_rand(&randState) & 3) == 1; + nonContiguousDst += FUZ_rand(&randState) & nonContiguousDst; /* 0=>0; 1=>1,2 */ + XXH64_resetState(&xxh64, 1); while (ip < iend) { unsigned nbBitsI = (FUZ_rand(&randState) % (maxBits-1)) + 1; @@ -473,19 +480,24 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi if (iSize > (size_t)(iend-ip)) iSize = iend-ip; if (oSize > (size_t)(oend-op)) oSize = oend-op; dOptions.stableDst = FUZ_rand(&randState) & 1; + if (nonContiguousDst==2) dOptions.stableDst = 0; + //if (ip == compressedBuffer+62073) DISPLAY("oSize : %i : pos %i \n", (int)oSize, (int)(op-(BYTE*)decodedBuffer)); result = LZ4F_decompress(dCtx, op, &oSize, ip, &iSize, &dOptions); - if (result == (size_t)-ERROR_checksum_invalid) locateBuffDiff((BYTE*)srcBuffer+srcStart, decodedBuffer, srcSize); - CHECK(LZ4F_isError(result), "Decompression failed (error %i)", (int)result); + //if (op+oSize >= (BYTE*)decodedBuffer+94727) DISPLAY("iSize : %i : pos %i \n", (int)iSize, (int)(ip-(BYTE*)compressedBuffer)); + //if ((int)result<0) DISPLAY("iSize : %i : pos %i \n", (int)iSize, (int)(ip-(BYTE*)compressedBuffer)); + if (result == (size_t)-ERROR_checksum_invalid) locateBuffDiff((BYTE*)srcBuffer+srcStart, decodedBuffer, srcSize, nonContiguousDst); + CHECK(LZ4F_isError(result), "Decompression failed (error %i:%s)", (int)result, LZ4F_getErrorName((LZ4F_errorCode_t)result)); + XXH64_update(&xxh64, op, (U32)oSize); op += oSize; ip += iSize; + op += nonContiguousDst; + if (nonContiguousDst==2) op = decodedBuffer; // overwritten destination } CHECK(result != 0, "Frame decompression failed (error %i)", (int)result); - crcDecoded = XXH64(decodedBuffer, op-(BYTE*)decodedBuffer, 1); - if (crcDecoded != crcOrig) locateBuffDiff((BYTE*)srcBuffer+srcStart, decodedBuffer, srcSize); + crcDecoded = XXH64_intermediateDigest(&xxh64); + if (crcDecoded != crcOrig) locateBuffDiff((BYTE*)srcBuffer+srcStart, decodedBuffer, srcSize, nonContiguousDst); CHECK(crcDecoded != crcOrig, "Decompression corruption"); } - - (void)FUZ_rand(&coreRand); // update rand seed } DISPLAYLEVEL(2, "\rAll tests completed \n"); @@ -496,6 +508,12 @@ _end: free(srcBuffer); free(compressedBuffer); free(decodedBuffer); + + if (pause) + { + DISPLAY("press enter to finish \n"); + getchar(); + } return testResult; _output_error: @@ -542,10 +560,10 @@ int main(int argc, char** argv) if (argument[0]=='-') { if (!strcmp(argument, "--no-prompt")) { no_prompt=1; seedset=1; displayLevel=1; continue; } + argument++; - while (argument[1]!=0) + while (*argument!=0) { - argument++; switch(*argument) { case 'h': @@ -588,7 +606,7 @@ int main(int argc, char** argv) argument++; } break; - case 'p': + case 'p': /* compressibility % */ argument++; proba=0; while ((*argument>='0') && (*argument<='9')) @@ -600,7 +618,12 @@ int main(int argc, char** argv) if (proba<0) proba=0; if (proba>100) proba=100; break; + case 'P': /* pause at the end */ + argument++; + pause = 1; + break; default: ; + return FUZ_usage(); } } } diff --git a/programs/fullbench.c b/programs/fullbench.c index 9292f20..5ee1710 100644 --- a/programs/fullbench.c +++ b/programs/fullbench.c @@ -343,14 +343,14 @@ static int local_LZ4_decompress_fast_withPrefix64k(const char* in, char* out, in static int local_LZ4_decompress_fast_usingDict(const char* in, char* out, int inSize, int outSize) { (void)inSize; - LZ4_decompress_fast_usingDict(in, out, outSize, in - 65536, 65536); + LZ4_decompress_fast_usingDict(in, out, outSize, out - 65536, 65536); return outSize; } static int local_LZ4_decompress_safe_usingDict(const char* in, char* out, int inSize, int outSize) { (void)inSize; - LZ4_decompress_safe_usingDict(in, out, inSize, outSize, in - 65536, 65536); + LZ4_decompress_safe_usingDict(in, out, inSize, outSize, out - 65536, 65536); return outSize; } @@ -359,7 +359,7 @@ extern int LZ4_decompress_safe_forceExtDict(const char* in, char* out, int inSiz static int local_LZ4_decompress_safe_forceExtDict(const char* in, char* out, int inSize, int outSize) { (void)inSize; - LZ4_decompress_safe_forceExtDict(in, out, inSize, outSize, in - 65536, 65536); + LZ4_decompress_safe_forceExtDict(in, out, inSize, outSize, out - 65536, 65536); return outSize; } -- cgit v0.12 From 4e92d7e412760eda8edb0cc2159c9372e3a61889 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 22 Sep 2014 01:32:55 +0100 Subject: LZ4F_getFrameInfo() No longer requires to continue decoding at address after header Slightly improved LZ4F_decompress() speed (less dictionary copy) --- lz4frame.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/lz4frame.c b/lz4frame.c index 7e06a4e..d16a320 100644 --- a/lz4frame.c +++ b/lz4frame.c @@ -754,7 +754,7 @@ LZ4F_errorCode_t LZ4F_getFrameInfo(LZ4F_decompressionContext_t decompressionCont if (LZ4F_isError(errorCode)) return errorCode; *srcSizePtr = errorCode; *frameInfoPtr = dctxPtr->frameInfo; - dctxPtr->srcExpect = (BYTE*)srcBuffer + errorCode; + dctxPtr->srcExpect = NULL; dctxPtr->dStage = dstage_getCBlockSize; return 4; } @@ -801,11 +801,27 @@ static void LZ4F_updateDict(LZ4F_dctx_internal_t* dctxPtr, const BYTE* dstPtr, s if (withinTmp) /* copy relevant dict portion in front of tmpOut within tmpOutBuffer */ { +#if 0 size_t savedDictSize = dctxPtr->tmpOut - dctxPtr->tmpOutBuffer; memcpy(dctxPtr->tmpOutBuffer, dctxPtr->dict + dctxPtr->dictSize - dctxPtr->tmpOutStart- savedDictSize, savedDictSize); dctxPtr->dict = dctxPtr->tmpOutBuffer; dctxPtr->dictSize = savedDictSize + dctxPtr->tmpOutStart + dstSize; return; + +#else + + size_t preserveSize = dctxPtr->tmpOut - dctxPtr->tmpOutBuffer; + size_t copySize = 64 KB - dctxPtr->tmpOutSize; + BYTE* oldDictEnd = dctxPtr->dict + dctxPtr->dictSize - dctxPtr->tmpOutStart; + if (dctxPtr->tmpOutSize > 64 KB) copySize = 0; + if (copySize > preserveSize) copySize = preserveSize; + + memcpy(dctxPtr->tmpOutBuffer + preserveSize - copySize, oldDictEnd - copySize, copySize); + + dctxPtr->dict = dctxPtr->tmpOutBuffer; + dctxPtr->dictSize = preserveSize + dctxPtr->tmpOutStart + dstSize; + return; +#endif } if (dctxPtr->dict == dctxPtr->tmpOutBuffer) /* copy dst into tmp to complete dict */ @@ -1199,9 +1215,12 @@ size_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContext, if (dctxPtr->dStage == dstage_flushOut) { size_t preserveSize = dctxPtr->tmpOut - dctxPtr->tmpOutBuffer; + size_t copySize = 64 KB - dctxPtr->tmpOutSize; BYTE* oldDictEnd = dctxPtr->dict + dctxPtr->dictSize - dctxPtr->tmpOutStart; + if (dctxPtr->tmpOutSize > 64 KB) copySize = 0; + if (copySize > preserveSize) copySize = preserveSize; - memcpy(dctxPtr->tmpOutBuffer, oldDictEnd - preserveSize, preserveSize); + memcpy(dctxPtr->tmpOutBuffer + preserveSize - copySize, oldDictEnd - copySize, copySize); dctxPtr->dict = dctxPtr->tmpOutBuffer; dctxPtr->dictSize = preserveSize + dctxPtr->tmpOutStart; -- cgit v0.12 From ff670f659a25160d6fc396842db67b241ba6bd9e Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 22 Sep 2014 02:02:40 +0100 Subject: Slightly improved decompression speed (linkedBlockMode, small frames primarily) --- lz4frame.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lz4frame.c b/lz4frame.c index d16a320..898978f 100644 --- a/lz4frame.c +++ b/lz4frame.c @@ -873,7 +873,7 @@ size_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContext, const LZ4F_decompressOptions_t* decompressOptionsPtr) { LZ4F_dctx_internal_t* dctxPtr = (LZ4F_dctx_internal_t*)decompressionContext; - LZ4F_decompressOptions_t optionsNull = { 0 }; + static const LZ4F_decompressOptions_t optionsNull = { 0 }; const BYTE* const srcStart = (const BYTE*)srcBuffer; const BYTE* const srcEnd = srcStart + *srcSizePtr; const BYTE* srcPtr = srcStart; @@ -1210,6 +1210,7 @@ size_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContext, if ( (dctxPtr->frameInfo.blockMode==blockLinked) &&(dctxPtr->dict != dctxPtr->tmpOutBuffer) &&(!decompressOptionsPtr->stableDst) + &&((unsigned)(dctxPtr->dStage-1) < (unsigned)(dstage_getSuffix-1)) ) { if (dctxPtr->dStage == dstage_flushOut) -- cgit v0.12 From 0e6151b1379ed8e60c1ca345497c86f88e3967f6 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 22 Sep 2014 02:36:20 +0100 Subject: LZ4F_compressFrame() : auto-resize block size when unnecessarily large --- lz4frame.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/lz4frame.c b/lz4frame.c index 898978f..9209191 100644 --- a/lz4frame.c +++ b/lz4frame.c @@ -261,12 +261,29 @@ size_t LZ4F_compressFrame(void* dstBuffer, size_t dstMaxSize, const void* srcBuf BYTE* const dstEnd = dstStart + dstMaxSize; - if (preferencesPtr!=NULL) prefs = *preferencesPtr; cctxI.version = LZ4F_VERSION; - cctxI.maxBufferSize = 64 KB; /* mess with real buffer size, to prevent allocation; works because autoflush==1 & stableSrc==1 */ + cctxI.maxBufferSize = 64 KB; /* mess with real buffer size to prevent allocation; works because autoflush==1 & stableSrc==1 */ + + if (preferencesPtr!=NULL) prefs = *preferencesPtr; + { + blockSizeID_t proposedBSID = max64KB; + size_t maxBlockSize = 64 KB; + while (prefs.frameInfo.blockSizeID > proposedBSID) + { + if (srcSize <= maxBlockSize) + { + prefs.frameInfo.blockSizeID = proposedBSID; + break; + } + proposedBSID++; + maxBlockSize <<= 2; + } + } prefs.autoFlush = 1; + if (srcSize <= LZ4F_getBlockSize(prefs.frameInfo.blockSizeID)) + prefs.frameInfo.blockMode = blockIndependent; /* no need for linked blocks */ + options.stableSrc = 1; - if (srcSize <= 64 KB) prefs.frameInfo.blockMode = blockIndependent; /* no need for linked blocks */ if (dstMaxSize < LZ4F_compressFrameBound(srcSize, &prefs)) return -ERROR_dstMaxSize_tooSmall; -- cgit v0.12 From 6e1049872ae2606cfb3820464c850c694275ae59 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 22 Sep 2014 02:59:42 +0100 Subject: LZ4F_compressFrame : fix potential crash on selecting custom preferences frame fuzzer tests : new random tests using LZ4F_compressFrame --- lz4frame.c | 2 +- programs/frametest.c | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lz4frame.c b/lz4frame.c index 9209191..47ad655 100644 --- a/lz4frame.c +++ b/lz4frame.c @@ -262,7 +262,7 @@ size_t LZ4F_compressFrame(void* dstBuffer, size_t dstMaxSize, const void* srcBuf cctxI.version = LZ4F_VERSION; - cctxI.maxBufferSize = 64 KB; /* mess with real buffer size to prevent allocation; works because autoflush==1 & stableSrc==1 */ + cctxI.maxBufferSize = 5 MB; /* mess with real buffer size to prevent allocation; works because autoflush==1 & stableSrc==1 */ if (preferencesPtr!=NULL) prefs = *preferencesPtr; { diff --git a/programs/frametest.c b/programs/frametest.c index 03b47e8..aa1a727 100644 --- a/programs/frametest.c +++ b/programs/frametest.c @@ -426,6 +426,14 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi DISPLAYUPDATE(2, "\r%5i ", testNb); crcOrig = XXH64((BYTE*)srcBuffer+srcStart, srcSize, 1); + if ((FUZ_rand(&randState)&0xF) == 2) + { + LZ4F_preferences_t* framePrefs = &prefs; + if ((FUZ_rand(&randState)&7) == 1) framePrefs = NULL; + cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(srcSize, framePrefs), srcBuffer + srcStart, srcSize, framePrefs); + CHECK(LZ4F_isError(cSize), "LZ4F_compressFrame failed : error %i (%s)", (int)cSize, LZ4F_getErrorName(cSize)); + } + else { const BYTE* ip = (const BYTE*)srcBuffer + srcStart; const BYTE* const iend = ip + srcSize; -- cgit v0.12 From abb6f7806a9753504776a7d4a9c1bbb4841f91c8 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 22 Sep 2014 17:38:17 +0100 Subject: Added : suooirt for s390x, thanks to Nobuhiro Iwamatsu Multiple warning fixes for Visual Studio 2012 --- NEWS | 1 + lz4.c | 3 ++- lz4frame.c | 72 ++++++++++++++++++++++++++-------------------------- lz4hc.c | 3 ++- programs/frametest.c | 14 +++++----- programs/fullbench.c | 12 ++++----- programs/fuzzer.c | 1 + 7 files changed, 55 insertions(+), 51 deletions(-) diff --git a/NEWS b/NEWS index ee4e40b..41d5762 100644 --- a/NEWS +++ b/NEWS @@ -1,5 +1,6 @@ r123: Added : experimental lz4frame API, thanks to Takayuki Matsuoka and Christopher Jackson for testings +Fix : s390x support, thanks to Nobuhiro Iwamatsu Fix : test mode (-t) no longer requires confirmation, thanks to Thary Nguyen r122: diff --git a/lz4.c b/lz4.c index 1c8e416..39f176f 100644 --- a/lz4.c +++ b/lz4.c @@ -51,7 +51,8 @@ || defined(__powerpc64__) || defined(__powerpc64le__) \ || defined(__ppc64__) || defined(__ppc64le__) \ || defined(__PPC64__) || defined(__PPC64LE__) \ - || defined(__ia64) || defined(__itanium__) || defined(_M_IA64) ) /* Detects 64 bits mode */ + || defined(__ia64) || defined(__itanium__) || defined(_M_IA64) \ + || defined(__s390x__) ) /* Detects 64 bits mode */ # define LZ4_ARCH64 1 #else # define LZ4_ARCH64 0 diff --git a/lz4frame.c b/lz4frame.c index 47ad655..35e5b0b 100644 --- a/lz4frame.c +++ b/lz4frame.c @@ -177,7 +177,7 @@ int LZ4F_isError(LZ4F_errorCode_t code) const char* LZ4F_getErrorName(LZ4F_errorCode_t code) { static const char* codeError = "Unspecified error code"; - if (LZ4F_isError(code)) return LZ4F_errorStrings[-code]; + if (LZ4F_isError(code)) return LZ4F_errorStrings[-(int)(code)]; return codeError; } @@ -191,7 +191,7 @@ static size_t LZ4F_getBlockSize(unsigned blockSizeID) if (blockSizeID == 0) blockSizeID = LZ4F_BLOCKSIZEID_DEFAULT; blockSizeID -= 4; - if (blockSizeID > 3) return -ERROR_maxBlockSize_invalid; + if (blockSizeID > 3) return (size_t)-ERROR_maxBlockSize_invalid; return blockSizes[blockSizeID]; } @@ -217,7 +217,7 @@ static U32 LZ4F_readLE32 (const BYTE* srcPtr) static BYTE LZ4F_headerChecksum (const BYTE* header, size_t length) { - U32 xxh = XXH32(header, length, 0); + U32 xxh = XXH32(header, (U32)length, 0); return (BYTE)(xxh >> 8); } @@ -286,7 +286,7 @@ size_t LZ4F_compressFrame(void* dstBuffer, size_t dstMaxSize, const void* srcBuf options.stableSrc = 1; if (dstMaxSize < LZ4F_compressFrameBound(srcSize, &prefs)) - return -ERROR_dstMaxSize_tooSmall; + return (size_t)-ERROR_dstMaxSize_tooSmall; errorCode = LZ4F_compressBegin(&cctxI, dstBuffer, dstMaxSize, &prefs); /* write header */ if (LZ4F_isError(errorCode)) return errorCode; @@ -322,7 +322,7 @@ LZ4F_errorCode_t LZ4F_createCompressionContext(LZ4F_compressionContext_t* LZ4F_c LZ4F_cctx_internal_t* cctxPtr; cctxPtr = ALLOCATOR(sizeof(LZ4F_cctx_internal_t)); - if (cctxPtr==NULL) return -ERROR_allocation_failed; + if (cctxPtr==NULL) return (LZ4F_errorCode_t)-ERROR_allocation_failed; cctxPtr->version = version; cctxPtr->cStage = 0; /* Next stage : write header */ @@ -359,8 +359,8 @@ size_t LZ4F_compressBegin(LZ4F_compressionContext_t compressionContext, void* ds BYTE* headerStart; size_t requiredBuffSize; - if (dstMaxSize < LZ4F_MAXHEADERFRAME_SIZE) return -ERROR_dstMaxSize_tooSmall; - if (cctxPtr->cStage != 0) return -ERROR_GENERIC; + if (dstMaxSize < LZ4F_MAXHEADERFRAME_SIZE) return (size_t)-ERROR_dstMaxSize_tooSmall; + if (cctxPtr->cStage != 0) return (size_t)-ERROR_GENERIC; if (preferencesPtr == NULL) preferencesPtr = &prefNull; /* Buffer Management */ @@ -377,7 +377,7 @@ size_t LZ4F_compressBegin(LZ4F_compressionContext_t compressionContext, void* ds cctxPtr->maxBufferSize = requiredBuffSize; FREEMEM(cctxPtr->tmpBuff); cctxPtr->tmpBuff = ALLOCATOR(requiredBuffSize); - if (cctxPtr->tmpBuff == NULL) return -ERROR_allocation_failed; + if (cctxPtr->tmpBuff == NULL) return (size_t)-ERROR_allocation_failed; } cctxPtr->tmpIn = cctxPtr->tmpBuff; cctxPtr->tmpInSize = 0; @@ -414,7 +414,7 @@ size_t LZ4F_compressBound(size_t srcSize, const LZ4F_preferences_t* preferencesP LZ4F_frameInfo_t* frameInfoPtr = (LZ4F_frameInfo_t*)preferencesPtr; /* works because prefs starts with frameInfo */ blockSizeID_t bid = (frameInfoPtr==NULL) ? LZ4F_BLOCKSIZEID_DEFAULT : frameInfoPtr->blockSizeID; size_t blockSize = LZ4F_getBlockSize(bid); - unsigned nbBlocks = (srcSize / blockSize) + 1; + unsigned nbBlocks = (unsigned)(srcSize / blockSize) + 1; size_t lastBlockSize = preferencesPtr->autoFlush ? srcSize % blockSize : blockSize; size_t blockInfo = 4; /* default, without block CRC option */ size_t frameEnd = 4 + (frameInfoPtr->contentChecksumFlag*4); @@ -435,8 +435,8 @@ static size_t LZ4F_compressBlock(void* dst, const void* src, size_t srcSize, com LZ4F_writeLE32(cSizePtr, cSize); if (cSize == 0) /* compression failed */ { - cSize = srcSize; - LZ4F_writeLE32(cSizePtr, srcSize + LZ4F_BLOCKUNCOMPRESSED_FLAG); + cSize = (U32)srcSize; + LZ4F_writeLE32(cSizePtr, cSize + LZ4F_BLOCKUNCOMPRESSED_FLAG); memcpy(cSizePtr+4, src, srcSize); } return cSize + 4; @@ -467,8 +467,8 @@ size_t LZ4F_compressUpdate(LZ4F_compressionContext_t compressionContext, void* d compressFunc_t compress; - if (cctxPtr->cStage != 1) return -ERROR_GENERIC; - if (dstMaxSize < LZ4F_compressBound(srcSize, &(cctxPtr->prefs))) return -ERROR_dstMaxSize_tooSmall; + if (cctxPtr->cStage != 1) return (size_t)-ERROR_GENERIC; + if (dstMaxSize < LZ4F_compressBound(srcSize, &(cctxPtr->prefs))) return (size_t)-ERROR_dstMaxSize_tooSmall; if (compressOptionsPtr == NULL) compressOptionsPtr = &cOptionsNull; /* select compression function */ @@ -529,7 +529,7 @@ size_t LZ4F_compressUpdate(LZ4F_compressionContext_t compressionContext, void* d { int realDictSize; realDictSize = LZ4_saveDict (&(cctxPtr->lz4ctx), (char*)(cctxPtr->tmpBuff), 64 KB); - if (realDictSize==0) return -ERROR_GENERIC; + if (realDictSize==0) return (size_t)-ERROR_GENERIC; cctxPtr->tmpIn = cctxPtr->tmpBuff + realDictSize; } } @@ -575,8 +575,8 @@ size_t LZ4F_flush(LZ4F_compressionContext_t compressionContext, void* dstBuffer, if (cctxPtr->tmpInSize == 0) return 0; /* nothing to flush */ - if (cctxPtr->cStage != 1) return -ERROR_GENERIC; - if (dstMaxSize < (cctxPtr->tmpInSize + 16)) return -ERROR_dstMaxSize_tooSmall; + if (cctxPtr->cStage != 1) return (size_t)-ERROR_GENERIC; + if (dstMaxSize < (cctxPtr->tmpInSize + 16)) return (size_t)-ERROR_dstMaxSize_tooSmall; if (compressOptionsPtr == NULL) compressOptionsPtr = &cOptionsNull; /* select compression function */ @@ -653,7 +653,7 @@ LZ4F_errorCode_t LZ4F_createDecompressionContext(LZ4F_compressionContext_t* LZ4F LZ4F_dctx_internal_t* dctxPtr; dctxPtr = ALLOCATOR(sizeof(LZ4F_dctx_internal_t)); - if (dctxPtr==NULL) return -ERROR_GENERIC; + if (dctxPtr==NULL) return (LZ4F_errorCode_t)-ERROR_GENERIC; dctxPtr->version = versionNumber; *LZ4F_decompressionContextPtr = (LZ4F_compressionContext_t)dctxPtr; @@ -679,10 +679,10 @@ static size_t LZ4F_decodeHeader(LZ4F_dctx_internal_t* dctxPtr, const BYTE* srcPt size_t bufferNeeded; /* need to decode header to get frameInfo */ - if (srcSize < 7) return -ERROR_GENERIC; /* minimal header size */ + if (srcSize < 7) return (size_t)-ERROR_GENERIC; /* minimal header size */ /* control magic number */ - if (LZ4F_readLE32(srcPtr) != LZ4F_MAGICNUMBER) return -ERROR_GENERIC; + if (LZ4F_readLE32(srcPtr) != LZ4F_MAGICNUMBER) return (size_t)-ERROR_GENERIC; srcPtr += 4; /* Flags */ @@ -698,17 +698,17 @@ static size_t LZ4F_decodeHeader(LZ4F_dctx_internal_t* dctxPtr, const BYTE* srcPt /* check */ HC = LZ4F_headerChecksum(srcPtr, 2); - if (HC != srcPtr[2]) return -ERROR_GENERIC; /* Bad header checksum error */ + if (HC != srcPtr[2]) return (size_t)-ERROR_GENERIC; /* Bad header checksum error */ /* validate */ - if (version != 1) return -ERROR_GENERIC; /* Version Number, only supported value */ - if (blockChecksumFlag != 0) return -ERROR_GENERIC; /* Only supported value for the time being */ - if (contentSizeFlag != 0) return -ERROR_GENERIC; /* Only supported value for the time being */ - if (((FLG>>1)&_1BIT) != 0) return -ERROR_GENERIC; /* Reserved bit */ - if (dictFlag != 0) return -ERROR_GENERIC; /* Only supported value for the time being */ - if (((BD>>7)&_1BIT) != 0) return -ERROR_GENERIC; /* Reserved bit */ - if (blockSizeID < 4) return -ERROR_GENERIC; /* Only supported values for the time being */ - if (((BD>>0)&_4BITS) != 0) return -ERROR_GENERIC; /* Reserved bits */ + if (version != 1) return (size_t)-ERROR_GENERIC; /* Version Number, only supported value */ + if (blockChecksumFlag != 0) return (size_t)-ERROR_GENERIC; /* Only supported value for the time being */ + if (contentSizeFlag != 0) return (size_t)-ERROR_GENERIC; /* Only supported value for the time being */ + if (((FLG>>1)&_1BIT) != 0) return (size_t)-ERROR_GENERIC; /* Reserved bit */ + if (dictFlag != 0) return (size_t)-ERROR_GENERIC; /* Only supported value for the time being */ + if (((BD>>7)&_1BIT) != 0) return (size_t)-ERROR_GENERIC; /* Reserved bit */ + if (blockSizeID < 4) return (size_t)-ERROR_GENERIC; /* Only supported values for the time being */ + if (((BD>>0)&_4BITS) != 0) return (size_t)-ERROR_GENERIC; /* Reserved bits */ /* save */ dctxPtr->frameInfo.blockMode = blockMode; @@ -727,9 +727,9 @@ static size_t LZ4F_decodeHeader(LZ4F_dctx_internal_t* dctxPtr, const BYTE* srcPt FREEMEM(dctxPtr->tmpOutBuffer); dctxPtr->maxBufferSize = bufferNeeded; dctxPtr->tmpIn = ALLOCATOR(dctxPtr->maxBlockSize); - if (dctxPtr->tmpIn == NULL) return -ERROR_GENERIC; + if (dctxPtr->tmpIn == NULL) return (size_t)-ERROR_GENERIC; dctxPtr->tmpOutBuffer= ALLOCATOR(dctxPtr->maxBufferSize); - if (dctxPtr->tmpOutBuffer== NULL) return -ERROR_GENERIC; + if (dctxPtr->tmpOutBuffer== NULL) return (size_t)-ERROR_GENERIC; } dctxPtr->tmpInSize = 0; dctxPtr->tmpInTarget = 0; @@ -908,7 +908,7 @@ size_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContext, /* expect to continue decoding src buffer where it left previously */ if (dctxPtr->srcExpect != NULL) { - if (srcStart != dctxPtr->srcExpect) return -ERROR_GENERIC; + if (srcStart != dctxPtr->srcExpect) return (size_t)-ERROR_GENERIC; } /* programmed as a state machine */ @@ -1000,7 +1000,7 @@ size_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContext, dctxPtr->dStage = dstage_getSuffix; break; } - if (nextCBlockSize > dctxPtr->maxBlockSize) return -ERROR_GENERIC; /* invalid cBlockSize */ + if (nextCBlockSize > dctxPtr->maxBlockSize) return (size_t)-ERROR_GENERIC; /* invalid cBlockSize */ dctxPtr->tmpInTarget = nextCBlockSize; if (LZ4F_readLE32(selectedIn) & LZ4F_BLOCKUNCOMPRESSED_FLAG) { @@ -1022,7 +1022,7 @@ size_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContext, if ((size_t)(srcEnd-srcPtr) < sizeToCopy) sizeToCopy = srcEnd - srcPtr; /* not enough input to read full block */ if ((size_t)(dstEnd-dstPtr) < sizeToCopy) sizeToCopy = dstEnd - dstPtr; memcpy(dstPtr, srcPtr, sizeToCopy); - if (dctxPtr->frameInfo.contentChecksumFlag) XXH32_update(&(dctxPtr->xxh), srcPtr, sizeToCopy); + if (dctxPtr->frameInfo.contentChecksumFlag) XXH32_update(&(dctxPtr->xxh), srcPtr, (U32)sizeToCopy); /* dictionary management */ if (dctxPtr->frameInfo.blockMode==blockLinked) @@ -1093,7 +1093,7 @@ size_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContext, decoder = LZ4F_decompress_safe; decodedSize = decoder((const char*)selectedIn, (char*)dstPtr, (int)dctxPtr->tmpInTarget, (int)dctxPtr->maxBlockSize, (const char*)dctxPtr->dict, (int)dctxPtr->dictSize); - if (decodedSize < 0) return -ERROR_GENERIC; /* decompression failed */ + if (decodedSize < 0) return (size_t)-ERROR_GENERIC; /* decompression failed */ if (dctxPtr->frameInfo.contentChecksumFlag) XXH32_update(&(dctxPtr->xxh), dstPtr, decodedSize); /* dictionary management */ @@ -1138,7 +1138,7 @@ size_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContext, /* Decode */ decodedSize = decoder((const char*)selectedIn, (char*)dctxPtr->tmpOut, (int)dctxPtr->tmpInTarget, (int)dctxPtr->maxBlockSize, (const char*)dctxPtr->dict, (int)dctxPtr->dictSize); - if (decodedSize < 0) return -ERROR_decompressionFailed; /* decompression failed */ + if (decodedSize < 0) return (size_t)-ERROR_decompressionFailed; /* decompression failed */ if (dctxPtr->frameInfo.contentChecksumFlag) XXH32_update(&(dctxPtr->xxh), dctxPtr->tmpOut, decodedSize); dctxPtr->tmpOutSize = decodedSize; dctxPtr->tmpOutStart = 0; @@ -1214,7 +1214,7 @@ size_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContext, { U32 readCRC = LZ4F_readLE32(selectedIn); U32 resultCRC = XXH32_intermediateDigest(&(dctxPtr->xxh)); - if (readCRC != resultCRC) return -ERROR_checksum_invalid; + if (readCRC != resultCRC) return (size_t)-ERROR_checksum_invalid; nextSrcSizeHint = 0; dctxPtr->dStage = dstage_getHeader; doAnotherStage = 0; diff --git a/lz4hc.c b/lz4hc.c index 441a1d6..34a6173 100644 --- a/lz4hc.c +++ b/lz4hc.c @@ -58,7 +58,8 @@ || defined(__powerpc64__) || defined(__powerpc64le__) \ || defined(__ppc64__) || defined(__ppc64le__) \ || defined(__PPC64__) || defined(__PPC64LE__) \ - || defined(__ia64) || defined(__itanium__) || defined(_M_IA64) ) /* Detects 64 bits mode */ + || defined(__ia64) || defined(__itanium__) || defined(_M_IA64) \ + || defined(__s390x__) ) /* Detects 64 bits mode */ # define LZ4_ARCH64 1 #else # define LZ4_ARCH64 0 diff --git a/programs/frametest.c b/programs/frametest.c index aa1a727..19e8e82 100644 --- a/programs/frametest.c +++ b/programs/frametest.c @@ -279,7 +279,7 @@ int basicTests(U32 seed, double compressibility) DISPLAYLEVEL(3, "Decompression test : \n"); { size_t decodedBufferSize = COMPRESSIBLE_NOISE_LENGTH; - unsigned maxBits = FUZ_highbit(decodedBufferSize); + unsigned maxBits = FUZ_highbit((U32)decodedBufferSize); BYTE* op = (BYTE*)decodedBuffer; BYTE* const oend = (BYTE*)decodedBuffer + COMPRESSIBLE_NOISE_LENGTH; BYTE* ip = (BYTE*)compressedBuffer; @@ -377,8 +377,8 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi void* compressedBuffer = NULL; void* decodedBuffer = NULL; U32 coreRand = seed; - LZ4F_decompressionContext_t dCtx; - LZ4F_compressionContext_t cCtx; + LZ4F_decompressionContext_t dCtx = NULL; + LZ4F_compressionContext_t cCtx = NULL; size_t result; XXH64_stateSpace_t xxh64; # define CHECK(cond, ...) if (cond) { DISPLAY("Error => "); DISPLAY(__VA_ARGS__); \ @@ -424,13 +424,13 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi prefs.autoFlush = autoflush; DISPLAYUPDATE(2, "\r%5i ", testNb); - crcOrig = XXH64((BYTE*)srcBuffer+srcStart, srcSize, 1); + crcOrig = XXH64((BYTE*)srcBuffer+srcStart, (U32)srcSize, 1); if ((FUZ_rand(&randState)&0xF) == 2) { LZ4F_preferences_t* framePrefs = &prefs; if ((FUZ_rand(&randState)&7) == 1) framePrefs = NULL; - cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(srcSize, framePrefs), srcBuffer + srcStart, srcSize, framePrefs); + cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(srcSize, framePrefs), (char*)srcBuffer + srcStart, srcSize, framePrefs); CHECK(LZ4F_isError(cSize), "LZ4F_compressFrame failed : error %i (%s)", (int)cSize, LZ4F_getErrorName(cSize)); } else @@ -439,7 +439,7 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi const BYTE* const iend = ip + srcSize; BYTE* op = compressedBuffer; BYTE* const oend = op + LZ4F_compressFrameBound(srcDataLength, NULL); - unsigned maxBits = FUZ_highbit(srcSize); + unsigned maxBits = FUZ_highbit((U32)srcSize); result = LZ4F_compressBegin(cCtx, op, oend-op, &prefs); CHECK(LZ4F_isError(result), "Compression header failed (error %i)", (int)result); op += result; @@ -475,7 +475,7 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi const BYTE* const iend = ip + cSize; BYTE* op = decodedBuffer; BYTE* const oend = op + srcDataLength; - unsigned maxBits = FUZ_highbit(cSize); + unsigned maxBits = FUZ_highbit((U32)cSize); unsigned nonContiguousDst = (FUZ_rand(&randState) & 3) == 1; nonContiguousDst += FUZ_rand(&randState) & nonContiguousDst; /* 0=>0; 1=>1,2 */ XXH64_resetState(&xxh64, 1); diff --git a/programs/fullbench.c b/programs/fullbench.c index 5ee1710..f34c68c 100644 --- a/programs/fullbench.c +++ b/programs/fullbench.c @@ -268,7 +268,7 @@ static int local_LZ4_compress_limitedOutput_withState(const char* in, char* out, return LZ4_compress_limitedOutput_withState(stateLZ4, in, out, inSize, LZ4_compressBound(inSize)); } -static void* ctx; +static LZ4_stream_t* ctx; static int local_LZ4_compress_continue(const char* in, char* out, int inSize) { return LZ4_compress_continue(ctx, in, out, inSize); @@ -323,7 +323,7 @@ static int local_LZ4_compressHC_limitedOutput_continue(const char* in, char* out static int local_LZ4F_compressFrame(const char* in, char* out, int inSize) { - return LZ4F_compressFrame(out, 2*inSize + 16, in, inSize, NULL); + return (int)LZ4F_compressFrame(out, 2*inSize + 16, in, inSize, NULL); } static int local_LZ4_decompress_fast(const char* in, char* out, int inSize, int outSize) @@ -378,7 +378,7 @@ static int local_LZ4F_decompress(const char* in, char* out, int inSize, int outS result = LZ4F_decompress(g_dCtx, out, &dstSize, in, &srcSize, NULL); if (result!=0) { DISPLAY("Error decompressing frame : unfinished frame\n"); exit(8); } if (srcSize != (size_t)inSize) { DISPLAY("Error decompressing frame : read size incorrect\n"); exit(9); } - return dstSize; + return (int)dstSize; } @@ -526,7 +526,7 @@ int fullSpeedBench(char** fileNamesTable, int nbFiles) case 12: compressionFunction = local_LZ4_compressHC_limitedOutput_continue; initFunction = LZ4_createHC; compressorName = "LZ4_compressHC_limitedOutput_continue"; break; case 13: compressionFunction = local_LZ4_compress_forceDict; initFunction = local_LZ4_resetDictT; compressorName = "LZ4_compress_forceDict"; break; case 14: compressionFunction = local_LZ4F_compressFrame; compressorName = "LZ4F_compressFrame"; - chunkP[0].origSize = benchedSize; nbChunks=1; + chunkP[0].origSize = (int)benchedSize; nbChunks=1; break; default : DISPLAY("ERROR ! Bad algorithm Id !! \n"); free(chunkP); return 1; } @@ -602,8 +602,8 @@ int fullSpeedBench(char** fileNamesTable, int nbFiles) case 9: decompressionFunction = local_LZ4F_decompress; dName = "LZ4F_decompress"; errorCode = LZ4F_compressFrame(compressed_buff, compressedBuffSize, orig_buff, benchedSize, NULL); if (LZ4F_isError(errorCode)) { DISPLAY("Preparation error compressing frame\n"); return 1; } - chunkP[0].origSize = benchedSize; - chunkP[0].compressedSize = errorCode; + chunkP[0].origSize = (int)benchedSize; + chunkP[0].compressedSize = (int)errorCode; nbChunks = 1; break; default : DISPLAY("ERROR ! Bad decompression algorithm Id !! \n"); free(chunkP); return 1; diff --git a/programs/fuzzer.c b/programs/fuzzer.c index f19382c..c98333d 100644 --- a/programs/fuzzer.c +++ b/programs/fuzzer.c @@ -29,6 +29,7 @@ #ifdef _MSC_VER /* Visual Studio */ # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ # pragma warning(disable : 4146) /* disable: C4146: minus unsigned expression */ +# pragma warning(disable : 4310) /* disable: C4310: constant char value > 127 */ #endif -- cgit v0.12 From 8c1ae998379bce4a0e719aaac43a777713ab5c00 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 22 Sep 2014 18:42:00 +0100 Subject: fixed : clang warnings --- examples/blockStreaming_doubleBuffer.c | 6 +- examples/blockStreaming_lineByLine.c | 6 +- examples/blockStreaming_ringBuffer.c | 6 +- lz4frame.c | 897 +++++++++++++++++---------------- programs/frametest.c | 308 +++++------ programs/lz4cli.c | 6 +- 6 files changed, 628 insertions(+), 601 deletions(-) diff --git a/examples/blockStreaming_doubleBuffer.c b/examples/blockStreaming_doubleBuffer.c index bd87e77..0adf6ae 100644 --- a/examples/blockStreaming_doubleBuffer.c +++ b/examples/blockStreaming_doubleBuffer.c @@ -140,9 +140,9 @@ int main(int argc, char* argv[]) return 0; } - sprintf(inpFilename, "%s", argv[1]); - sprintf(lz4Filename, "%s.lz4s-%d", argv[1], BLOCK_BYTES); - sprintf(decFilename, "%s.lz4s-%d.dec", argv[1], BLOCK_BYTES); + snprintf(inpFilename, 256, "%s", argv[1]); + snprintf(lz4Filename, 256, "%s.lz4s-%d", argv[1], BLOCK_BYTES); + snprintf(decFilename, 256, "%s.lz4s-%d.dec", argv[1], BLOCK_BYTES); printf("inp = [%s]\n", inpFilename); printf("lz4 = [%s]\n", lz4Filename); diff --git a/examples/blockStreaming_lineByLine.c b/examples/blockStreaming_lineByLine.c index e236a44..6d14801 100644 --- a/examples/blockStreaming_lineByLine.c +++ b/examples/blockStreaming_lineByLine.c @@ -158,9 +158,9 @@ int main(int argc, char* argv[]) return 0; } - sprintf(inpFilename, "%s", argv[1]); - sprintf(lz4Filename, "%s.lz4s", argv[1]); - sprintf(decFilename, "%s.lz4s.dec", argv[1]); + snprintf(inpFilename, 256, "%s", argv[1]); + snprintf(lz4Filename, 256, "%s.lz4s", argv[1]); + snprintf(decFilename, 256, "%s.lz4s.dec", argv[1]); printf("inp = [%s]\n", inpFilename); printf("lz4 = [%s]\n", lz4Filename); diff --git a/examples/blockStreaming_ringBuffer.c b/examples/blockStreaming_ringBuffer.c index 725e375..bfdbed1 100644 --- a/examples/blockStreaming_ringBuffer.c +++ b/examples/blockStreaming_ringBuffer.c @@ -136,9 +136,9 @@ int main(int argc, char** argv) return 0; } - sprintf(inpFilename, "%s", argv[1]); - sprintf(lz4Filename, "%s.lz4s-%d", argv[1], 0); - sprintf(decFilename, "%s.lz4s-%d.dec", argv[1], 0); + snprintf(inpFilename, 256, "%s", argv[1]); + snprintf(lz4Filename, 256, "%s.lz4s-%d", argv[1], 0); + snprintf(decFilename, 256, "%s.lz4s-%d.dec", argv[1], 0); printf("inp = [%s]\n", inpFilename); printf("lz4 = [%s]\n", lz4Filename); diff --git a/lz4frame.c b/lz4frame.c index 35e5b0b..3a750c0 100644 --- a/lz4frame.c +++ b/lz4frame.c @@ -76,17 +76,17 @@ **************************************/ #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */ # include - typedef uint8_t BYTE; - typedef uint16_t U16; - typedef uint32_t U32; - typedef int32_t S32; - typedef uint64_t U64; +typedef uint8_t BYTE; +typedef uint16_t U16; +typedef uint32_t U32; +typedef int32_t S32; +typedef uint64_t U64; #else - typedef unsigned char BYTE; - typedef unsigned short U16; - typedef unsigned int U32; - typedef signed int S32; - typedef unsigned long long U64; +typedef unsigned char BYTE; +typedef unsigned short U16; +typedef unsigned int U32; +typedef signed int S32; +typedef unsigned long long U64; #endif @@ -112,37 +112,39 @@ /************************************** Structures and local types **************************************/ -typedef struct { - LZ4F_preferences_t prefs; - unsigned version; - unsigned cStage; - size_t maxBlockSize; - size_t maxBufferSize; - BYTE* tmpBuff; - BYTE* tmpIn; - size_t tmpInSize; - XXH32_stateSpace_t xxh; - LZ4_stream_t lz4ctx; +typedef struct +{ + LZ4F_preferences_t prefs; + unsigned version; + unsigned cStage; + size_t maxBlockSize; + size_t maxBufferSize; + BYTE* tmpBuff; + BYTE* tmpIn; + size_t tmpInSize; + XXH32_stateSpace_t xxh; + LZ4_stream_t lz4ctx; } LZ4F_cctx_internal_t; -typedef struct { +typedef struct +{ LZ4F_frameInfo_t frameInfo; - unsigned version; - unsigned dStage; - size_t maxBlockSize; - size_t maxBufferSize; - const BYTE* srcExpect; - BYTE* tmpIn; - size_t tmpInSize; - size_t tmpInTarget; - BYTE* tmpOutBuffer; - BYTE* dict; - size_t dictSize; - BYTE* tmpOut; - size_t tmpOutSize; - size_t tmpOutStart; - XXH32_stateSpace_t xxh; - BYTE header[8]; + unsigned version; + unsigned dStage; + size_t maxBlockSize; + size_t maxBufferSize; + const BYTE* srcExpect; + BYTE* tmpIn; + size_t tmpInSize; + size_t tmpInTarget; + BYTE* tmpOutBuffer; + BYTE* dict; + size_t dictSize; + BYTE* tmpOut; + size_t tmpOutSize; + size_t tmpOutStart; + XXH32_stateSpace_t xxh; + BYTE header[8]; } LZ4F_dctx_internal_t; @@ -171,7 +173,7 @@ typedef enum { OK_NoError=0, ERROR_GENERIC = 1, int LZ4F_isError(LZ4F_errorCode_t code) { - return (code > (LZ4F_errorCode_t)(-ERROR_maxCode)); + return (code > (LZ4F_errorCode_t)(-ERROR_maxCode)); } const char* LZ4F_getErrorName(LZ4F_errorCode_t code) @@ -187,22 +189,22 @@ const char* LZ4F_getErrorName(LZ4F_errorCode_t code) **************************************/ static size_t LZ4F_getBlockSize(unsigned blockSizeID) { - static const size_t blockSizes[4] = { 64 KB, 256 KB, 1 MB, 4 MB }; + static const size_t blockSizes[4] = { 64 KB, 256 KB, 1 MB, 4 MB }; - if (blockSizeID == 0) blockSizeID = LZ4F_BLOCKSIZEID_DEFAULT; - blockSizeID -= 4; - if (blockSizeID > 3) return (size_t)-ERROR_maxBlockSize_invalid; - return blockSizes[blockSizeID]; + if (blockSizeID == 0) blockSizeID = LZ4F_BLOCKSIZEID_DEFAULT; + blockSizeID -= 4; + if (blockSizeID > 3) return (size_t)-ERROR_maxBlockSize_invalid; + return blockSizes[blockSizeID]; } /* unoptimized version; solves endianess & alignment issues */ static void LZ4F_writeLE32 (BYTE* dstPtr, U32 value32) { - dstPtr[0] = (BYTE)value32; - dstPtr[1] = (BYTE)(value32 >> 8); - dstPtr[2] = (BYTE)(value32 >> 16); - dstPtr[3] = (BYTE)(value32 >> 24); + dstPtr[0] = (BYTE)value32; + dstPtr[1] = (BYTE)(value32 >> 8); + dstPtr[2] = (BYTE)(value32 >> 16); + dstPtr[3] = (BYTE)(value32 >> 24); } static U32 LZ4F_readLE32 (const BYTE* srcPtr) @@ -217,8 +219,8 @@ static U32 LZ4F_readLE32 (const BYTE* srcPtr) static BYTE LZ4F_headerChecksum (const BYTE* header, size_t length) { - U32 xxh = XXH32(header, (U32)length, 0); - return (BYTE)(xxh >> 8); + U32 xxh = XXH32(header, (U32)length, 0); + return (BYTE)(xxh >> 8); } @@ -227,17 +229,17 @@ static BYTE LZ4F_headerChecksum (const BYTE* header, size_t length) **************************************/ size_t LZ4F_compressFrameBound(size_t srcSize, const LZ4F_preferences_t* preferencesPtr) { - LZ4F_preferences_t prefs = { 0 }; - size_t headerSize; - size_t streamSize; + LZ4F_preferences_t prefs = { 0 }; + size_t headerSize; + size_t streamSize; - if (preferencesPtr!=NULL) prefs = *preferencesPtr; - prefs.autoFlush = 1; + if (preferencesPtr!=NULL) prefs = *preferencesPtr; + prefs.autoFlush = 1; - headerSize = 7; /* basic header size (no option) including magic number */ - streamSize = LZ4F_compressBound(srcSize, &prefs); + headerSize = 7; /* basic header size (no option) including magic number */ + streamSize = LZ4F_compressBound(srcSize, &prefs); - return headerSize + streamSize; + return headerSize + streamSize; } @@ -254,11 +256,11 @@ size_t LZ4F_compressFrame(void* dstBuffer, size_t dstMaxSize, const void* srcBuf { LZ4F_cctx_internal_t cctxI = { 0 }; /* works because no allocation */ LZ4F_preferences_t prefs = { 0 }; - LZ4F_compressOptions_t options = { 0 }; - LZ4F_errorCode_t errorCode; - BYTE* const dstStart = (BYTE*) dstBuffer; - BYTE* dstPtr = dstStart; - BYTE* const dstEnd = dstStart + dstMaxSize; + LZ4F_compressOptions_t options = { 0 }; + LZ4F_errorCode_t errorCode; + BYTE* const dstStart = (BYTE*) dstBuffer; + BYTE* dstPtr = dstStart; + BYTE* const dstEnd = dstStart + dstMaxSize; cctxI.version = LZ4F_VERSION; @@ -285,23 +287,23 @@ size_t LZ4F_compressFrame(void* dstBuffer, size_t dstMaxSize, const void* srcBuf options.stableSrc = 1; - if (dstMaxSize < LZ4F_compressFrameBound(srcSize, &prefs)) - return (size_t)-ERROR_dstMaxSize_tooSmall; + if (dstMaxSize < LZ4F_compressFrameBound(srcSize, &prefs)) + return (size_t)-ERROR_dstMaxSize_tooSmall; - errorCode = LZ4F_compressBegin(&cctxI, dstBuffer, dstMaxSize, &prefs); /* write header */ - if (LZ4F_isError(errorCode)) return errorCode; - dstPtr += errorCode; /* header size */ + errorCode = LZ4F_compressBegin(&cctxI, dstBuffer, dstMaxSize, &prefs); /* write header */ + if (LZ4F_isError(errorCode)) return errorCode; + dstPtr += errorCode; /* header size */ - dstMaxSize -= errorCode; + dstMaxSize -= errorCode; errorCode = LZ4F_compressUpdate(&cctxI, dstPtr, dstMaxSize, srcBuffer, srcSize, &options); - if (LZ4F_isError(errorCode)) return errorCode; - dstPtr += errorCode; + if (LZ4F_isError(errorCode)) return errorCode; + dstPtr += errorCode; - errorCode = LZ4F_compressEnd(&cctxI, dstPtr, dstEnd-dstPtr, &options); /* flush last block, and generate suffix */ - if (LZ4F_isError(errorCode)) return errorCode; - dstPtr += errorCode; + errorCode = LZ4F_compressEnd(&cctxI, dstPtr, dstEnd-dstPtr, &options); /* flush last block, and generate suffix */ + if (LZ4F_isError(errorCode)) return errorCode; + dstPtr += errorCode; - return (dstPtr - dstStart); + return (dstPtr - dstStart); } @@ -319,28 +321,28 @@ size_t LZ4F_compressFrame(void* dstBuffer, size_t dstMaxSize, const void* srcBuf */ LZ4F_errorCode_t LZ4F_createCompressionContext(LZ4F_compressionContext_t* LZ4F_compressionContextPtr, unsigned version) { - LZ4F_cctx_internal_t* cctxPtr; + LZ4F_cctx_internal_t* cctxPtr; - cctxPtr = ALLOCATOR(sizeof(LZ4F_cctx_internal_t)); - if (cctxPtr==NULL) return (LZ4F_errorCode_t)-ERROR_allocation_failed; + cctxPtr = ALLOCATOR(sizeof(LZ4F_cctx_internal_t)); + if (cctxPtr==NULL) return (LZ4F_errorCode_t)-ERROR_allocation_failed; - cctxPtr->version = version; - cctxPtr->cStage = 0; /* Next stage : write header */ + cctxPtr->version = version; + cctxPtr->cStage = 0; /* Next stage : write header */ - *LZ4F_compressionContextPtr = (LZ4F_compressionContext_t)cctxPtr; + *LZ4F_compressionContextPtr = (LZ4F_compressionContext_t)cctxPtr; - return OK_NoError; + return OK_NoError; } LZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_compressionContext_t LZ4F_compressionContext) { - LZ4F_cctx_internal_t* cctxPtr = (LZ4F_cctx_internal_t*)LZ4F_compressionContext; + LZ4F_cctx_internal_t* cctxPtr = (LZ4F_cctx_internal_t*)LZ4F_compressionContext; - FREEMEM(cctxPtr->tmpBuff); - FREEMEM(LZ4F_compressionContext); + FREEMEM(cctxPtr->tmpBuff); + FREEMEM(LZ4F_compressionContext); - return OK_NoError; + return OK_NoError; } @@ -353,26 +355,26 @@ LZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_compressionContext_t LZ4F_comp size_t LZ4F_compressBegin(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const LZ4F_preferences_t* preferencesPtr) { LZ4F_preferences_t prefNull = { 0 }; - LZ4F_cctx_internal_t* cctxPtr = (LZ4F_cctx_internal_t*)compressionContext; - BYTE* const dstStart = (BYTE*)dstBuffer; - BYTE* dstPtr = dstStart; - BYTE* headerStart; - size_t requiredBuffSize; - - if (dstMaxSize < LZ4F_MAXHEADERFRAME_SIZE) return (size_t)-ERROR_dstMaxSize_tooSmall; - if (cctxPtr->cStage != 0) return (size_t)-ERROR_GENERIC; - if (preferencesPtr == NULL) preferencesPtr = &prefNull; - - /* Buffer Management */ - cctxPtr->prefs = *preferencesPtr; - if (cctxPtr->prefs.frameInfo.blockSizeID == 0) cctxPtr->prefs.frameInfo.blockSizeID = LZ4F_BLOCKSIZEID_DEFAULT; - cctxPtr->maxBlockSize = LZ4F_getBlockSize(cctxPtr->prefs.frameInfo.blockSizeID); - - requiredBuffSize = cctxPtr->maxBlockSize + ((cctxPtr->prefs.frameInfo.blockMode == blockLinked) * 128 KB); - if (preferencesPtr->autoFlush) + LZ4F_cctx_internal_t* cctxPtr = (LZ4F_cctx_internal_t*)compressionContext; + BYTE* const dstStart = (BYTE*)dstBuffer; + BYTE* dstPtr = dstStart; + BYTE* headerStart; + size_t requiredBuffSize; + + if (dstMaxSize < LZ4F_MAXHEADERFRAME_SIZE) return (size_t)-ERROR_dstMaxSize_tooSmall; + if (cctxPtr->cStage != 0) return (size_t)-ERROR_GENERIC; + if (preferencesPtr == NULL) preferencesPtr = &prefNull; + + /* Buffer Management */ + cctxPtr->prefs = *preferencesPtr; + if (cctxPtr->prefs.frameInfo.blockSizeID == 0) cctxPtr->prefs.frameInfo.blockSizeID = LZ4F_BLOCKSIZEID_DEFAULT; + cctxPtr->maxBlockSize = LZ4F_getBlockSize(cctxPtr->prefs.frameInfo.blockSizeID); + + requiredBuffSize = cctxPtr->maxBlockSize + ((cctxPtr->prefs.frameInfo.blockMode == blockLinked) * 128 KB); + if (preferencesPtr->autoFlush) requiredBuffSize = (cctxPtr->prefs.frameInfo.blockMode == blockLinked) * 64 KB; /* just needs dict */ - if (cctxPtr->maxBufferSize < requiredBuffSize) + if (cctxPtr->maxBufferSize < requiredBuffSize) { cctxPtr->maxBufferSize = requiredBuffSize; FREEMEM(cctxPtr->tmpBuff); @@ -381,27 +383,27 @@ size_t LZ4F_compressBegin(LZ4F_compressionContext_t compressionContext, void* ds } cctxPtr->tmpIn = cctxPtr->tmpBuff; cctxPtr->tmpInSize = 0; - XXH32_resetState(&(cctxPtr->xxh), 0); - LZ4_resetStream(&(cctxPtr->lz4ctx)); + XXH32_resetState(&(cctxPtr->xxh), 0); + LZ4_resetStream(&(cctxPtr->lz4ctx)); - /* Magic Number */ - LZ4F_writeLE32(dstPtr, LZ4F_MAGICNUMBER); - dstPtr += 4; - headerStart = dstPtr; + /* Magic Number */ + LZ4F_writeLE32(dstPtr, LZ4F_MAGICNUMBER); + dstPtr += 4; + headerStart = dstPtr; - /* FLG Byte */ - //cctxPtr->prefs.frameInfo.blockMode = 1; // <============ debug + /* FLG Byte */ + //cctxPtr->prefs.frameInfo.blockMode = 1; // <============ debug *dstPtr++ = ((1 & _2BITS) << 6) /* Version('01') */ - + ((cctxPtr->prefs.frameInfo.blockMode & _1BIT ) << 5) /* Block mode */ - + (char)((cctxPtr->prefs.frameInfo.contentChecksumFlag & _1BIT ) << 2); /* Stream checksum */ - /* BD Byte */ + + ((cctxPtr->prefs.frameInfo.blockMode & _1BIT ) << 5) /* Block mode */ + + (char)((cctxPtr->prefs.frameInfo.contentChecksumFlag & _1BIT ) << 2); /* Stream checksum */ + /* BD Byte */ *dstPtr++ = (char)((cctxPtr->prefs.frameInfo.blockSizeID & _3BITS) << 4); - /* CRC Byte */ - *dstPtr++ = LZ4F_headerChecksum(headerStart, 2); + /* CRC Byte */ + *dstPtr++ = LZ4F_headerChecksum(headerStart, 2); - cctxPtr->cStage = 1; /* header written, wait for data block */ + cctxPtr->cStage = 1; /* header written, wait for data block */ - return (dstPtr - dstStart); + return (dstPtr - dstStart); } @@ -412,15 +414,15 @@ size_t LZ4F_compressBegin(LZ4F_compressionContext_t compressionContext, void* ds size_t LZ4F_compressBound(size_t srcSize, const LZ4F_preferences_t* preferencesPtr) { LZ4F_frameInfo_t* frameInfoPtr = (LZ4F_frameInfo_t*)preferencesPtr; /* works because prefs starts with frameInfo */ - blockSizeID_t bid = (frameInfoPtr==NULL) ? LZ4F_BLOCKSIZEID_DEFAULT : frameInfoPtr->blockSizeID; - size_t blockSize = LZ4F_getBlockSize(bid); - unsigned nbBlocks = (unsigned)(srcSize / blockSize) + 1; - size_t lastBlockSize = preferencesPtr->autoFlush ? srcSize % blockSize : blockSize; - size_t blockInfo = 4; /* default, without block CRC option */ - size_t frameEnd = 4 + (frameInfoPtr->contentChecksumFlag*4); - size_t result = (blockInfo * nbBlocks) + (blockSize * (nbBlocks-1)) + lastBlockSize + frameEnd; - - return result; + blockSizeID_t bid = (frameInfoPtr==NULL) ? LZ4F_BLOCKSIZEID_DEFAULT : frameInfoPtr->blockSizeID; + size_t blockSize = LZ4F_getBlockSize(bid); + unsigned nbBlocks = (unsigned)(srcSize / blockSize) + 1; + size_t lastBlockSize = preferencesPtr->autoFlush ? srcSize % blockSize : blockSize; + size_t blockInfo = 4; /* default, without block CRC option */ + size_t frameEnd = 4 + (frameInfoPtr->contentChecksumFlag*4); + size_t result = (blockInfo * nbBlocks) + (blockSize * (nbBlocks-1)) + lastBlockSize + frameEnd; + + return result; } @@ -456,66 +458,66 @@ typedef enum { notDone, fromTmpBuffer, fromSrcBuffer } LZ4F_lastBlockStatus; */ size_t LZ4F_compressUpdate(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const void* srcBuffer, size_t srcSize, const LZ4F_compressOptions_t* compressOptionsPtr) { - LZ4F_compressOptions_t cOptionsNull = { 0 }; - LZ4F_cctx_internal_t* cctxPtr = (LZ4F_cctx_internal_t*)compressionContext; - size_t blockSize = cctxPtr->maxBlockSize; - const BYTE* srcPtr = (const BYTE*)srcBuffer; - const BYTE* const srcEnd = srcPtr + srcSize; - BYTE* const dstStart = (BYTE*)dstBuffer; - BYTE* dstPtr = dstStart; - LZ4F_lastBlockStatus lastBlockCompressed = notDone; - compressFunc_t compress; - - - if (cctxPtr->cStage != 1) return (size_t)-ERROR_GENERIC; - if (dstMaxSize < LZ4F_compressBound(srcSize, &(cctxPtr->prefs))) return (size_t)-ERROR_dstMaxSize_tooSmall; - if (compressOptionsPtr == NULL) compressOptionsPtr = &cOptionsNull; - - /* select compression function */ - compress = (cctxPtr->prefs.frameInfo.blockMode == blockLinked) ? - (int(*)(void*,const char*,char*,int,int))LZ4_compress_limitedOutput_continue : - LZ4_compress_limitedOutput_withState; - - /* complete tmp buffer */ - if (cctxPtr->tmpInSize > 0) /* some data already within tmp buffer */ - { - size_t sizeToCopy = blockSize - cctxPtr->tmpInSize; - if (sizeToCopy > srcSize) - { - /* add src to tmpIn buffer */ - memcpy(cctxPtr->tmpIn + cctxPtr->tmpInSize, srcBuffer, srcSize); - srcPtr = srcEnd; - cctxPtr->tmpInSize += srcSize; - /* still needs some CRC */ - } - else - { - /* complete tmpIn block and then compress it */ - lastBlockCompressed = fromTmpBuffer; - memcpy(cctxPtr->tmpIn + cctxPtr->tmpInSize, srcBuffer, sizeToCopy); - srcPtr += sizeToCopy; - - dstPtr += LZ4F_compressBlock(dstPtr, cctxPtr->tmpIn, blockSize, compress, &(cctxPtr->lz4ctx)); - - if (cctxPtr->prefs.frameInfo.blockMode==blockLinked) cctxPtr->tmpIn += blockSize; - cctxPtr->tmpInSize = 0; - } - } - - while ((size_t)(srcEnd - srcPtr) >= blockSize) - { - /* compress full block */ + LZ4F_compressOptions_t cOptionsNull = { 0 }; + LZ4F_cctx_internal_t* cctxPtr = (LZ4F_cctx_internal_t*)compressionContext; + size_t blockSize = cctxPtr->maxBlockSize; + const BYTE* srcPtr = (const BYTE*)srcBuffer; + const BYTE* const srcEnd = srcPtr + srcSize; + BYTE* const dstStart = (BYTE*)dstBuffer; + BYTE* dstPtr = dstStart; + LZ4F_lastBlockStatus lastBlockCompressed = notDone; + compressFunc_t compress; + + + if (cctxPtr->cStage != 1) return (size_t)-ERROR_GENERIC; + if (dstMaxSize < LZ4F_compressBound(srcSize, &(cctxPtr->prefs))) return (size_t)-ERROR_dstMaxSize_tooSmall; + if (compressOptionsPtr == NULL) compressOptionsPtr = &cOptionsNull; + + /* select compression function */ + compress = (cctxPtr->prefs.frameInfo.blockMode == blockLinked) ? + (int(*)(void*,const char*,char*,int,int))LZ4_compress_limitedOutput_continue : + LZ4_compress_limitedOutput_withState; + + /* complete tmp buffer */ + if (cctxPtr->tmpInSize > 0) /* some data already within tmp buffer */ + { + size_t sizeToCopy = blockSize - cctxPtr->tmpInSize; + if (sizeToCopy > srcSize) + { + /* add src to tmpIn buffer */ + memcpy(cctxPtr->tmpIn + cctxPtr->tmpInSize, srcBuffer, srcSize); + srcPtr = srcEnd; + cctxPtr->tmpInSize += srcSize; + /* still needs some CRC */ + } + else + { + /* complete tmpIn block and then compress it */ + lastBlockCompressed = fromTmpBuffer; + memcpy(cctxPtr->tmpIn + cctxPtr->tmpInSize, srcBuffer, sizeToCopy); + srcPtr += sizeToCopy; + + dstPtr += LZ4F_compressBlock(dstPtr, cctxPtr->tmpIn, blockSize, compress, &(cctxPtr->lz4ctx)); + + if (cctxPtr->prefs.frameInfo.blockMode==blockLinked) cctxPtr->tmpIn += blockSize; + cctxPtr->tmpInSize = 0; + } + } + + while ((size_t)(srcEnd - srcPtr) >= blockSize) + { + /* compress full block */ lastBlockCompressed = fromSrcBuffer; dstPtr += LZ4F_compressBlock(dstPtr, srcPtr, blockSize, compress, &(cctxPtr->lz4ctx)); - srcPtr += blockSize; - } + srcPtr += blockSize; + } if ((cctxPtr->prefs.autoFlush) && (srcPtr < srcEnd)) { /* compress remaining input < blockSize */ lastBlockCompressed = fromSrcBuffer; dstPtr += LZ4F_compressBlock(dstPtr, srcPtr, srcEnd - srcPtr, compress, &(cctxPtr->lz4ctx)); - srcPtr = srcEnd; + srcPtr = srcEnd; } /* preserve dictionary if necessary */ @@ -542,7 +544,7 @@ size_t LZ4F_compressUpdate(LZ4F_compressionContext_t compressionContext, void* d } /* some input data left, necessarily < blockSize */ - if (srcPtr < srcEnd) + if (srcPtr < srcEnd) { /* fill tmp buffer */ size_t sizeToCopy = srcEnd - srcPtr; @@ -550,10 +552,10 @@ size_t LZ4F_compressUpdate(LZ4F_compressionContext_t compressionContext, void* d cctxPtr->tmpInSize = sizeToCopy; } - if (cctxPtr->prefs.frameInfo.contentChecksumFlag == contentChecksumEnabled) - XXH32_update(&(cctxPtr->xxh), srcBuffer, (unsigned)srcSize); + if (cctxPtr->prefs.frameInfo.contentChecksumFlag == contentChecksumEnabled) + XXH32_update(&(cctxPtr->xxh), srcBuffer, (unsigned)srcSize); - return dstPtr - dstStart; + return dstPtr - dstStart; } @@ -567,27 +569,28 @@ size_t LZ4F_compressUpdate(LZ4F_compressionContext_t compressionContext, void* d */ size_t LZ4F_flush(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const LZ4F_compressOptions_t* compressOptionsPtr) { - LZ4F_compressOptions_t cOptionsNull = { 0 }; - LZ4F_cctx_internal_t* cctxPtr = (LZ4F_cctx_internal_t*)compressionContext; - BYTE* const dstStart = (BYTE*)dstBuffer; - BYTE* dstPtr = dstStart; - int (*compress)(void*, const char*, char*, int, int); + LZ4F_compressOptions_t cOptionsNull = { 0 }; + LZ4F_cctx_internal_t* cctxPtr = (LZ4F_cctx_internal_t*)compressionContext; + BYTE* const dstStart = (BYTE*)dstBuffer; + BYTE* dstPtr = dstStart; + int (*compress)(void*, const char*, char*, int, int); - if (cctxPtr->tmpInSize == 0) return 0; /* nothing to flush */ - if (cctxPtr->cStage != 1) return (size_t)-ERROR_GENERIC; - if (dstMaxSize < (cctxPtr->tmpInSize + 16)) return (size_t)-ERROR_dstMaxSize_tooSmall; - if (compressOptionsPtr == NULL) compressOptionsPtr = &cOptionsNull; + if (cctxPtr->tmpInSize == 0) return 0; /* nothing to flush */ + if (cctxPtr->cStage != 1) return (size_t)-ERROR_GENERIC; + if (dstMaxSize < (cctxPtr->tmpInSize + 16)) return (size_t)-ERROR_dstMaxSize_tooSmall; + if (compressOptionsPtr == NULL) compressOptionsPtr = &cOptionsNull; + (void)compressOptionsPtr; /* not yet useful */ - /* select compression function */ - compress = (cctxPtr->prefs.frameInfo.blockMode == blockLinked) ? - (int(*)(void*,const char*,char*,int,int))LZ4_compress_limitedOutput_continue : - LZ4_compress_limitedOutput_withState; + /* select compression function */ + compress = (cctxPtr->prefs.frameInfo.blockMode == blockLinked) ? + (int(*)(void*,const char*,char*,int,int))LZ4_compress_limitedOutput_continue : + LZ4_compress_limitedOutput_withState; /* compress tmp buffer */ dstPtr += LZ4F_compressBlock(dstPtr, cctxPtr->tmpIn, cctxPtr->tmpInSize, compress, &(cctxPtr->lz4ctx)); if (cctxPtr->prefs.frameInfo.blockMode==blockLinked) cctxPtr->tmpIn += cctxPtr->tmpInSize; - cctxPtr->tmpInSize = 0; + cctxPtr->tmpInSize = 0; /* keep tmpIn within limits */ if ((cctxPtr->tmpIn + cctxPtr->maxBlockSize) > (cctxPtr->tmpBuff + cctxPtr->maxBufferSize)) /* necessarily blockLinked */ @@ -596,7 +599,7 @@ size_t LZ4F_flush(LZ4F_compressionContext_t compressionContext, void* dstBuffer, cctxPtr->tmpIn = cctxPtr->tmpBuff + 64 KB; } - return dstPtr - dstStart; + return dstPtr - dstStart; } @@ -611,27 +614,28 @@ size_t LZ4F_flush(LZ4F_compressionContext_t compressionContext, void* dstBuffer, */ size_t LZ4F_compressEnd(LZ4F_compressionContext_t compressionContext, void* dstBuffer, size_t dstMaxSize, const LZ4F_compressOptions_t* compressOptionsPtr) { - LZ4F_cctx_internal_t* cctxPtr = (LZ4F_cctx_internal_t*)compressionContext; - BYTE* const dstStart = (BYTE*)dstBuffer; - BYTE* dstPtr = dstStart; - size_t errorCode; + LZ4F_cctx_internal_t* cctxPtr = (LZ4F_cctx_internal_t*)compressionContext; + BYTE* const dstStart = (BYTE*)dstBuffer; + BYTE* dstPtr = dstStart; + size_t errorCode; - errorCode = LZ4F_flush(compressionContext, dstBuffer, dstMaxSize, compressOptionsPtr); - if (LZ4F_isError(errorCode)) return errorCode; - dstPtr += errorCode; + errorCode = LZ4F_flush(compressionContext, dstBuffer, dstMaxSize, compressOptionsPtr); + if (LZ4F_isError(errorCode)) return errorCode; + dstPtr += errorCode; - LZ4F_writeLE32(dstPtr, 0); dstPtr+=4; /* endMark */ + LZ4F_writeLE32(dstPtr, 0); + dstPtr+=4; /* endMark */ - if (cctxPtr->prefs.frameInfo.contentChecksumFlag == contentChecksumEnabled) - { - U32 xxh = XXH32_intermediateDigest(&(cctxPtr->xxh)); - LZ4F_writeLE32(dstPtr, xxh); - dstPtr+=4; /* content Checksum */ - } + if (cctxPtr->prefs.frameInfo.contentChecksumFlag == contentChecksumEnabled) + { + U32 xxh = XXH32_intermediateDigest(&(cctxPtr->xxh)); + LZ4F_writeLE32(dstPtr, xxh); + dstPtr+=4; /* content Checksum */ + } - cctxPtr->cStage = 0; /* state is now re-usable (with identical preferences) */ + cctxPtr->cStage = 0; /* state is now re-usable (with identical preferences) */ - return dstPtr - dstStart; + return dstPtr - dstStart; } @@ -748,7 +752,8 @@ typedef enum { dstage_getHeader=0, dstage_storeHeader, dstage_decodeHeader, dstage_copyDirect, dstage_getCBlock, dstage_storeCBlock, dstage_decodeCBlock, dstage_decodeCBlock_intoDst, dstage_decodeCBlock_intoTmp, dstage_flushOut, - dstage_getSuffix, dstage_storeSuffix, dstage_checkSuffix } dStage_t; + dstage_getSuffix, dstage_storeSuffix, dstage_checkSuffix + } dStage_t; /* LZ4F_getFrameInfo() @@ -785,7 +790,8 @@ LZ4F_errorCode_t LZ4F_getFrameInfo(LZ4F_decompressionContext_t decompressionCont static int LZ4F_decompress_safe (const char* source, char* dest, int compressedSize, int maxDecompressedSize, const char* dictStart, int dictSize) { - (void)dictStart; (void)dictSize; + (void)dictStart; + (void)dictSize; return LZ4_decompress_safe (source, dest, compressedSize, maxDecompressedSize); } @@ -903,7 +909,8 @@ size_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContext, if (decompressOptionsPtr==NULL) decompressOptionsPtr = &optionsNull; - *srcSizePtr = 0; *dstSizePtr = 0; + *srcSizePtr = 0; + *dstSizePtr = 0; /* expect to continue decoding src buffer where it left previously */ if (dctxPtr->srcExpect != NULL) @@ -920,314 +927,314 @@ size_t LZ4F_decompress(LZ4F_decompressionContext_t decompressionContext, { case dstage_getHeader: + { + if (srcEnd-srcPtr >= 7) { - if (srcEnd-srcPtr >= 7) - { - selectedIn = srcPtr; - srcPtr += 7; - dctxPtr->dStage = dstage_decodeHeader; - break; - } - dctxPtr->tmpInSize = 0; - dctxPtr->dStage = dstage_storeHeader; + selectedIn = srcPtr; + srcPtr += 7; + dctxPtr->dStage = dstage_decodeHeader; break; } + dctxPtr->tmpInSize = 0; + dctxPtr->dStage = dstage_storeHeader; + break; + } case dstage_storeHeader: + { + size_t sizeToCopy = 7 - dctxPtr->tmpInSize; + if (sizeToCopy > (size_t)(srcEnd - srcPtr)) sizeToCopy = srcEnd - srcPtr; + memcpy(dctxPtr->header + dctxPtr->tmpInSize, srcPtr, sizeToCopy); + dctxPtr->tmpInSize += sizeToCopy; + srcPtr += sizeToCopy; + if (dctxPtr->tmpInSize < 7) { - size_t sizeToCopy = 7 - dctxPtr->tmpInSize; - if (sizeToCopy > (size_t)(srcEnd - srcPtr)) sizeToCopy = srcEnd - srcPtr; - memcpy(dctxPtr->header + dctxPtr->tmpInSize, srcPtr, sizeToCopy); - dctxPtr->tmpInSize += sizeToCopy; - srcPtr += sizeToCopy; - if (dctxPtr->tmpInSize < 7) - { - nextSrcSizeHint = (7 - dctxPtr->tmpInSize) + 4; - doAnotherStage = 0; /* no enough src, wait to get some more */ - break; - } - selectedIn = dctxPtr->header; - dctxPtr->dStage = dstage_decodeHeader; + nextSrcSizeHint = (7 - dctxPtr->tmpInSize) + 4; + doAnotherStage = 0; /* no enough src, wait to get some more */ break; } + selectedIn = dctxPtr->header; + dctxPtr->dStage = dstage_decodeHeader; + break; + } case dstage_decodeHeader: - { - LZ4F_errorCode_t errorCode = LZ4F_decodeHeader(dctxPtr, selectedIn, 7); - if (LZ4F_isError(errorCode)) return errorCode; - dctxPtr->dStage = dstage_getCBlockSize; - break; - } + { + LZ4F_errorCode_t errorCode = LZ4F_decodeHeader(dctxPtr, selectedIn, 7); + if (LZ4F_isError(errorCode)) return errorCode; + dctxPtr->dStage = dstage_getCBlockSize; + break; + } case dstage_getCBlockSize: + { + if ((srcEnd - srcPtr) >= 4) { - if ((srcEnd - srcPtr) >= 4) - { - selectedIn = srcPtr; - srcPtr += 4; - dctxPtr->dStage = dstage_decodeCBlockSize; - break; - } - /* not enough input to read cBlockSize field */ - dctxPtr->tmpInSize = 0; - dctxPtr->dStage = dstage_storeCBlockSize; + selectedIn = srcPtr; + srcPtr += 4; + dctxPtr->dStage = dstage_decodeCBlockSize; break; } + /* not enough input to read cBlockSize field */ + dctxPtr->tmpInSize = 0; + dctxPtr->dStage = dstage_storeCBlockSize; + break; + } case dstage_storeCBlockSize: + { + size_t sizeToCopy = 4 - dctxPtr->tmpInSize; + if (sizeToCopy > (size_t)(srcEnd - srcPtr)) sizeToCopy = srcEnd - srcPtr; + memcpy(dctxPtr->tmpIn + dctxPtr->tmpInSize, srcPtr, sizeToCopy); + srcPtr += sizeToCopy; + dctxPtr->tmpInSize += sizeToCopy; + if (dctxPtr->tmpInSize < 4) /* not enough input to get full cBlockSize; wait for more */ { - size_t sizeToCopy = 4 - dctxPtr->tmpInSize; - if (sizeToCopy > (size_t)(srcEnd - srcPtr)) sizeToCopy = srcEnd - srcPtr; - memcpy(dctxPtr->tmpIn + dctxPtr->tmpInSize, srcPtr, sizeToCopy); - srcPtr += sizeToCopy; - dctxPtr->tmpInSize += sizeToCopy; - if (dctxPtr->tmpInSize < 4) /* not enough input to get full cBlockSize; wait for more */ - { - nextSrcSizeHint = 4 - dctxPtr->tmpInSize; - doAnotherStage=0; - break; - } - selectedIn = dctxPtr->tmpIn; - dctxPtr->dStage = dstage_decodeCBlockSize; + nextSrcSizeHint = 4 - dctxPtr->tmpInSize; + doAnotherStage=0; break; } + selectedIn = dctxPtr->tmpIn; + dctxPtr->dStage = dstage_decodeCBlockSize; + break; + } case dstage_decodeCBlockSize: + { + size_t nextCBlockSize = LZ4F_readLE32(selectedIn) & 0x7FFFFFFFU; + if (nextCBlockSize==0) /* frameEnd signal, no more CBlock */ { - size_t nextCBlockSize = LZ4F_readLE32(selectedIn) & 0x7FFFFFFFU; - if (nextCBlockSize==0) /* frameEnd signal, no more CBlock */ - { - dctxPtr->dStage = dstage_getSuffix; - break; - } - if (nextCBlockSize > dctxPtr->maxBlockSize) return (size_t)-ERROR_GENERIC; /* invalid cBlockSize */ - dctxPtr->tmpInTarget = nextCBlockSize; - if (LZ4F_readLE32(selectedIn) & LZ4F_BLOCKUNCOMPRESSED_FLAG) - { - dctxPtr->dStage = dstage_copyDirect; - break; - } - dctxPtr->dStage = dstage_getCBlock; - if (dstPtr==dstEnd) - { - nextSrcSizeHint = nextCBlockSize + 4; - doAnotherStage = 0; - } + dctxPtr->dStage = dstage_getSuffix; + break; + } + if (nextCBlockSize > dctxPtr->maxBlockSize) return (size_t)-ERROR_GENERIC; /* invalid cBlockSize */ + dctxPtr->tmpInTarget = nextCBlockSize; + if (LZ4F_readLE32(selectedIn) & LZ4F_BLOCKUNCOMPRESSED_FLAG) + { + dctxPtr->dStage = dstage_copyDirect; break; } + dctxPtr->dStage = dstage_getCBlock; + if (dstPtr==dstEnd) + { + nextSrcSizeHint = nextCBlockSize + 4; + doAnotherStage = 0; + } + break; + } case dstage_copyDirect: /* uncompressed block */ + { + size_t sizeToCopy = dctxPtr->tmpInTarget; + if ((size_t)(srcEnd-srcPtr) < sizeToCopy) sizeToCopy = srcEnd - srcPtr; /* not enough input to read full block */ + if ((size_t)(dstEnd-dstPtr) < sizeToCopy) sizeToCopy = dstEnd - dstPtr; + memcpy(dstPtr, srcPtr, sizeToCopy); + if (dctxPtr->frameInfo.contentChecksumFlag) XXH32_update(&(dctxPtr->xxh), srcPtr, (U32)sizeToCopy); + + /* dictionary management */ + if (dctxPtr->frameInfo.blockMode==blockLinked) + LZ4F_updateDict(dctxPtr, dstPtr, sizeToCopy, dstStart, 0); + + srcPtr += sizeToCopy; + dstPtr += sizeToCopy; + if (sizeToCopy == dctxPtr->tmpInTarget) /* all copied */ { - size_t sizeToCopy = dctxPtr->tmpInTarget; - if ((size_t)(srcEnd-srcPtr) < sizeToCopy) sizeToCopy = srcEnd - srcPtr; /* not enough input to read full block */ - if ((size_t)(dstEnd-dstPtr) < sizeToCopy) sizeToCopy = dstEnd - dstPtr; - memcpy(dstPtr, srcPtr, sizeToCopy); - if (dctxPtr->frameInfo.contentChecksumFlag) XXH32_update(&(dctxPtr->xxh), srcPtr, (U32)sizeToCopy); - - /* dictionary management */ - if (dctxPtr->frameInfo.blockMode==blockLinked) - LZ4F_updateDict(dctxPtr, dstPtr, sizeToCopy, dstStart, 0); - - srcPtr += sizeToCopy; - dstPtr += sizeToCopy; - if (sizeToCopy == dctxPtr->tmpInTarget) /* all copied */ - { - dctxPtr->dStage = dstage_getCBlockSize; - break; - } - dctxPtr->tmpInTarget -= sizeToCopy; /* still need to copy more */ - nextSrcSizeHint = dctxPtr->tmpInTarget + 4; - doAnotherStage = 0; + dctxPtr->dStage = dstage_getCBlockSize; break; } + dctxPtr->tmpInTarget -= sizeToCopy; /* still need to copy more */ + nextSrcSizeHint = dctxPtr->tmpInTarget + 4; + doAnotherStage = 0; + break; + } case dstage_getCBlock: /* entry from dstage_decodeCBlockSize */ + { + if ((size_t)(srcEnd-srcPtr) < dctxPtr->tmpInTarget) { - if ((size_t)(srcEnd-srcPtr) < dctxPtr->tmpInTarget) - { - dctxPtr->tmpInSize = 0; - dctxPtr->dStage = dstage_storeCBlock; - break; - } - selectedIn = srcPtr; - srcPtr += dctxPtr->tmpInTarget; - dctxPtr->dStage = dstage_decodeCBlock; + dctxPtr->tmpInSize = 0; + dctxPtr->dStage = dstage_storeCBlock; break; } + selectedIn = srcPtr; + srcPtr += dctxPtr->tmpInTarget; + dctxPtr->dStage = dstage_decodeCBlock; + break; + } case dstage_storeCBlock: + { + size_t sizeToCopy = dctxPtr->tmpInTarget - dctxPtr->tmpInSize; + if (sizeToCopy > (size_t)(srcEnd-srcPtr)) sizeToCopy = srcEnd-srcPtr; + memcpy(dctxPtr->tmpIn + dctxPtr->tmpInSize, srcPtr, sizeToCopy); + dctxPtr->tmpInSize += sizeToCopy; + srcPtr += sizeToCopy; + if (dctxPtr->tmpInSize < dctxPtr->tmpInTarget) /* need more input */ { - size_t sizeToCopy = dctxPtr->tmpInTarget - dctxPtr->tmpInSize; - if (sizeToCopy > (size_t)(srcEnd-srcPtr)) sizeToCopy = srcEnd-srcPtr; - memcpy(dctxPtr->tmpIn + dctxPtr->tmpInSize, srcPtr, sizeToCopy); - dctxPtr->tmpInSize += sizeToCopy; - srcPtr += sizeToCopy; - if (dctxPtr->tmpInSize < dctxPtr->tmpInTarget) /* need more input */ - { - nextSrcSizeHint = (dctxPtr->tmpInTarget - dctxPtr->tmpInSize) + 4; - doAnotherStage=0; - break; - } - selectedIn = dctxPtr->tmpIn; - dctxPtr->dStage = dstage_decodeCBlock; + nextSrcSizeHint = (dctxPtr->tmpInTarget - dctxPtr->tmpInSize) + 4; + doAnotherStage=0; break; } + selectedIn = dctxPtr->tmpIn; + dctxPtr->dStage = dstage_decodeCBlock; + break; + } case dstage_decodeCBlock: - { - if ((size_t)(dstEnd-dstPtr) < dctxPtr->maxBlockSize) /* not enough place into dst : decode into tmpOut */ - dctxPtr->dStage = dstage_decodeCBlock_intoTmp; - else - dctxPtr->dStage = dstage_decodeCBlock_intoDst; - break; - } + { + if ((size_t)(dstEnd-dstPtr) < dctxPtr->maxBlockSize) /* not enough place into dst : decode into tmpOut */ + dctxPtr->dStage = dstage_decodeCBlock_intoTmp; + else + dctxPtr->dStage = dstage_decodeCBlock_intoDst; + break; + } case dstage_decodeCBlock_intoDst: - { - int (*decoder)(const char*, char*, int, int, const char*, int); - int decodedSize; + { + int (*decoder)(const char*, char*, int, int, const char*, int); + int decodedSize; - if (dctxPtr->frameInfo.blockMode == blockLinked) - decoder = LZ4_decompress_safe_usingDict; - else - decoder = LZ4F_decompress_safe; + if (dctxPtr->frameInfo.blockMode == blockLinked) + decoder = LZ4_decompress_safe_usingDict; + else + decoder = LZ4F_decompress_safe; - decodedSize = decoder((const char*)selectedIn, (char*)dstPtr, (int)dctxPtr->tmpInTarget, (int)dctxPtr->maxBlockSize, (const char*)dctxPtr->dict, (int)dctxPtr->dictSize); - if (decodedSize < 0) return (size_t)-ERROR_GENERIC; /* decompression failed */ - if (dctxPtr->frameInfo.contentChecksumFlag) XXH32_update(&(dctxPtr->xxh), dstPtr, decodedSize); + decodedSize = decoder((const char*)selectedIn, (char*)dstPtr, (int)dctxPtr->tmpInTarget, (int)dctxPtr->maxBlockSize, (const char*)dctxPtr->dict, (int)dctxPtr->dictSize); + if (decodedSize < 0) return (size_t)-ERROR_GENERIC; /* decompression failed */ + if (dctxPtr->frameInfo.contentChecksumFlag) XXH32_update(&(dctxPtr->xxh), dstPtr, decodedSize); - /* dictionary management */ - if (dctxPtr->frameInfo.blockMode==blockLinked) - LZ4F_updateDict(dctxPtr, dstPtr, decodedSize, dstStart, 0); + /* dictionary management */ + if (dctxPtr->frameInfo.blockMode==blockLinked) + LZ4F_updateDict(dctxPtr, dstPtr, decodedSize, dstStart, 0); - dstPtr += decodedSize; - dctxPtr->dStage = dstage_getCBlockSize; - break; - } + dstPtr += decodedSize; + dctxPtr->dStage = dstage_getCBlockSize; + break; + } case dstage_decodeCBlock_intoTmp: - { - /* not enough place into dst : decode into tmpOut */ - int (*decoder)(const char*, char*, int, int, const char*, int); - int decodedSize; + { + /* not enough place into dst : decode into tmpOut */ + int (*decoder)(const char*, char*, int, int, const char*, int); + int decodedSize; - if (dctxPtr->frameInfo.blockMode == blockLinked) - decoder = LZ4_decompress_safe_usingDict; - else - decoder = LZ4F_decompress_safe; + if (dctxPtr->frameInfo.blockMode == blockLinked) + decoder = LZ4_decompress_safe_usingDict; + else + decoder = LZ4F_decompress_safe; - /* ensure enough place for tmpOut */ - if (dctxPtr->frameInfo.blockMode == blockLinked) + /* ensure enough place for tmpOut */ + if (dctxPtr->frameInfo.blockMode == blockLinked) + { + if (dctxPtr->dict == dctxPtr->tmpOutBuffer) { - if (dctxPtr->dict == dctxPtr->tmpOutBuffer) - { - if (dctxPtr->dictSize > 128 KB) - { - memcpy(dctxPtr->dict, dctxPtr->dict + dctxPtr->dictSize - 64 KB, 64 KB); - dctxPtr->dictSize = 64 KB; - } - dctxPtr->tmpOut = dctxPtr->dict + dctxPtr->dictSize; - } - else /* dict not within tmp */ + if (dctxPtr->dictSize > 128 KB) { - size_t reservedDictSpace = dctxPtr->dictSize; - if (reservedDictSpace > 64 KB) reservedDictSpace = 64 KB; - dctxPtr->tmpOut = dctxPtr->tmpOutBuffer + reservedDictSpace; + memcpy(dctxPtr->dict, dctxPtr->dict + dctxPtr->dictSize - 64 KB, 64 KB); + dctxPtr->dictSize = 64 KB; } + dctxPtr->tmpOut = dctxPtr->dict + dctxPtr->dictSize; + } + else /* dict not within tmp */ + { + size_t reservedDictSpace = dctxPtr->dictSize; + if (reservedDictSpace > 64 KB) reservedDictSpace = 64 KB; + dctxPtr->tmpOut = dctxPtr->tmpOutBuffer + reservedDictSpace; } - - /* Decode */ - decodedSize = decoder((const char*)selectedIn, (char*)dctxPtr->tmpOut, (int)dctxPtr->tmpInTarget, (int)dctxPtr->maxBlockSize, (const char*)dctxPtr->dict, (int)dctxPtr->dictSize); - if (decodedSize < 0) return (size_t)-ERROR_decompressionFailed; /* decompression failed */ - if (dctxPtr->frameInfo.contentChecksumFlag) XXH32_update(&(dctxPtr->xxh), dctxPtr->tmpOut, decodedSize); - dctxPtr->tmpOutSize = decodedSize; - dctxPtr->tmpOutStart = 0; - dctxPtr->dStage = dstage_flushOut; - break; } + /* Decode */ + decodedSize = decoder((const char*)selectedIn, (char*)dctxPtr->tmpOut, (int)dctxPtr->tmpInTarget, (int)dctxPtr->maxBlockSize, (const char*)dctxPtr->dict, (int)dctxPtr->dictSize); + if (decodedSize < 0) return (size_t)-ERROR_decompressionFailed; /* decompression failed */ + if (dctxPtr->frameInfo.contentChecksumFlag) XXH32_update(&(dctxPtr->xxh), dctxPtr->tmpOut, decodedSize); + dctxPtr->tmpOutSize = decodedSize; + dctxPtr->tmpOutStart = 0; + dctxPtr->dStage = dstage_flushOut; + break; + } + case dstage_flushOut: /* flush decoded data from tmpOut to dstBuffer */ - { - size_t sizeToCopy = dctxPtr->tmpOutSize - dctxPtr->tmpOutStart; - if (sizeToCopy > (size_t)(dstEnd-dstPtr)) sizeToCopy = dstEnd-dstPtr; - memcpy(dstPtr, dctxPtr->tmpOut + dctxPtr->tmpOutStart, sizeToCopy); + { + size_t sizeToCopy = dctxPtr->tmpOutSize - dctxPtr->tmpOutStart; + if (sizeToCopy > (size_t)(dstEnd-dstPtr)) sizeToCopy = dstEnd-dstPtr; + memcpy(dstPtr, dctxPtr->tmpOut + dctxPtr->tmpOutStart, sizeToCopy); - /* dictionary management */ - if (dctxPtr->frameInfo.blockMode==blockLinked) - LZ4F_updateDict(dctxPtr, dstPtr, sizeToCopy, dstStart, 1); + /* dictionary management */ + if (dctxPtr->frameInfo.blockMode==blockLinked) + LZ4F_updateDict(dctxPtr, dstPtr, sizeToCopy, dstStart, 1); - dctxPtr->tmpOutStart += sizeToCopy; - dstPtr += sizeToCopy; + dctxPtr->tmpOutStart += sizeToCopy; + dstPtr += sizeToCopy; - /* end of flush ? */ - if (dctxPtr->tmpOutStart == dctxPtr->tmpOutSize) - { - dctxPtr->dStage = dstage_getCBlockSize; - break; - } - nextSrcSizeHint = 4; - doAnotherStage = 0; /* still some data to flush */ + /* end of flush ? */ + if (dctxPtr->tmpOutStart == dctxPtr->tmpOutSize) + { + dctxPtr->dStage = dstage_getCBlockSize; break; } + nextSrcSizeHint = 4; + doAnotherStage = 0; /* still some data to flush */ + break; + } - case dstage_getSuffix: + case dstage_getSuffix: + { + size_t suffixSize = dctxPtr->frameInfo.contentChecksumFlag * 4; + if (suffixSize == 0) /* frame completed */ { - size_t suffixSize = dctxPtr->frameInfo.contentChecksumFlag * 4; - if (suffixSize == 0) /* frame completed */ - { - nextSrcSizeHint = 0; - dctxPtr->dStage = dstage_getHeader; - doAnotherStage = 0; - break; - } - if ((srcEnd - srcPtr) >= 4) /* CRC present */ - { - selectedIn = srcPtr; - srcPtr += 4; - dctxPtr->dStage = dstage_checkSuffix; - break; - } - dctxPtr->tmpInSize = 0; - dctxPtr->dStage = dstage_storeSuffix; + nextSrcSizeHint = 0; + dctxPtr->dStage = dstage_getHeader; + doAnotherStage = 0; break; } - - case dstage_storeSuffix: + if ((srcEnd - srcPtr) >= 4) /* CRC present */ { - size_t sizeToCopy = 4 - dctxPtr->tmpInSize; - if (sizeToCopy > (size_t)(srcEnd - srcPtr)) sizeToCopy = srcEnd - srcPtr; - memcpy(dctxPtr->tmpIn + dctxPtr->tmpInSize, srcPtr, sizeToCopy); - srcPtr += sizeToCopy; - dctxPtr->tmpInSize += sizeToCopy; - if (dctxPtr->tmpInSize < 4) /* not enough input to read complete suffix */ - { - nextSrcSizeHint = 4 - dctxPtr->tmpInSize; - doAnotherStage=0; - break; - } - selectedIn = dctxPtr->tmpIn; + selectedIn = srcPtr; + srcPtr += 4; dctxPtr->dStage = dstage_checkSuffix; break; } + dctxPtr->tmpInSize = 0; + dctxPtr->dStage = dstage_storeSuffix; + break; + } - case dstage_checkSuffix: + case dstage_storeSuffix: + { + size_t sizeToCopy = 4 - dctxPtr->tmpInSize; + if (sizeToCopy > (size_t)(srcEnd - srcPtr)) sizeToCopy = srcEnd - srcPtr; + memcpy(dctxPtr->tmpIn + dctxPtr->tmpInSize, srcPtr, sizeToCopy); + srcPtr += sizeToCopy; + dctxPtr->tmpInSize += sizeToCopy; + if (dctxPtr->tmpInSize < 4) /* not enough input to read complete suffix */ { - U32 readCRC = LZ4F_readLE32(selectedIn); - U32 resultCRC = XXH32_intermediateDigest(&(dctxPtr->xxh)); - if (readCRC != resultCRC) return (size_t)-ERROR_checksum_invalid; - nextSrcSizeHint = 0; - dctxPtr->dStage = dstage_getHeader; - doAnotherStage = 0; + nextSrcSizeHint = 4 - dctxPtr->tmpInSize; + doAnotherStage=0; break; } + selectedIn = dctxPtr->tmpIn; + dctxPtr->dStage = dstage_checkSuffix; + break; + } + + case dstage_checkSuffix: + { + U32 readCRC = LZ4F_readLE32(selectedIn); + U32 resultCRC = XXH32_intermediateDigest(&(dctxPtr->xxh)); + if (readCRC != resultCRC) return (size_t)-ERROR_checksum_invalid; + nextSrcSizeHint = 0; + dctxPtr->dStage = dstage_getHeader; + doAnotherStage = 0; + break; + } } } /* preserve dictionary within tmp if necessary */ if ( (dctxPtr->frameInfo.blockMode==blockLinked) - &&(dctxPtr->dict != dctxPtr->tmpOutBuffer) - &&(!decompressOptionsPtr->stableDst) - &&((unsigned)(dctxPtr->dStage-1) < (unsigned)(dstage_getSuffix-1)) + &&(dctxPtr->dict != dctxPtr->tmpOutBuffer) + &&(!decompressOptionsPtr->stableDst) + &&((unsigned)(dctxPtr->dStage-1) < (unsigned)(dstage_getSuffix-1)) ) { if (dctxPtr->dStage == dstage_flushOut) diff --git a/programs/frametest.c b/programs/frametest.c index 19e8e82..3d19ef4 100644 --- a/programs/frametest.c +++ b/programs/frametest.c @@ -53,17 +53,17 @@ **************************************/ #if defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */ # include - typedef uint8_t BYTE; - typedef uint16_t U16; - typedef uint32_t U32; - typedef int32_t S32; - typedef uint64_t U64; +typedef uint8_t BYTE; +typedef uint16_t U16; +typedef uint32_t U32; +typedef int32_t S32; +typedef uint64_t U64; #else - typedef unsigned char BYTE; - typedef unsigned short U16; - typedef unsigned int U32; - typedef signed int S32; - typedef unsigned long long U64; +typedef unsigned char BYTE; +typedef unsigned short U16; +typedef unsigned int U32; +typedef signed int S32; +typedef unsigned long long U64; #endif @@ -113,21 +113,21 @@ static U32 pause = 0; *********************************************************/ static U32 FUZ_GetMilliStart(void) { - struct timeb tb; - U32 nCount; - ftime( &tb ); - nCount = (U32) (((tb.time & 0xFFFFF) * 1000) + tb.millitm); - return nCount; + struct timeb tb; + U32 nCount; + ftime( &tb ); + nCount = (U32) (((tb.time & 0xFFFFF) * 1000) + tb.millitm); + return nCount; } static U32 FUZ_GetMilliSpan(U32 nTimeStart) { - U32 nCurrent = FUZ_GetMilliStart(); - U32 nSpan = nCurrent - nTimeStart; - if (nTimeStart > nCurrent) - nSpan += 0x100000 * 1000; - return nSpan; + U32 nCurrent = FUZ_GetMilliStart(); + U32 nSpan = nCurrent - nTimeStart; + if (nTimeStart > nCurrent) + nSpan += 0x100000 * 1000; + return nSpan; } @@ -187,39 +187,43 @@ static unsigned FUZ_highbit(U32 v32) { unsigned nbBits = 0; if (v32==0) return 0; - while (v32) { v32 >>= 1; nbBits ++; } + while (v32) + { + v32 >>= 1; + nbBits ++; + } return nbBits; } int basicTests(U32 seed, double compressibility) { - int testResult = 0; - void* CNBuffer; - void* compressedBuffer; - void* decodedBuffer; - U32 randState = seed; - size_t cSize, testSize; - LZ4F_preferences_t prefs = { 0 }; - LZ4F_decompressionContext_t dCtx; - U64 crcOrig; - - // Create compressible test buffer - CNBuffer = malloc(COMPRESSIBLE_NOISE_LENGTH); - compressedBuffer = malloc(LZ4F_compressFrameBound(COMPRESSIBLE_NOISE_LENGTH, NULL)); - decodedBuffer = malloc(COMPRESSIBLE_NOISE_LENGTH); - FUZ_fillCompressibleNoiseBuffer(CNBuffer, COMPRESSIBLE_NOISE_LENGTH, compressibility, &randState); - crcOrig = XXH64(CNBuffer, COMPRESSIBLE_NOISE_LENGTH, 1); - - // Trivial tests : one-step frame - testSize = COMPRESSIBLE_NOISE_LENGTH; - DISPLAYLEVEL(3, "Using NULL preferences : \n"); - cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, NULL), CNBuffer, testSize, NULL); - if (LZ4F_isError(cSize)) goto _output_error; - DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); - - DISPLAYLEVEL(3, "Decompression test : \n"); - { + int testResult = 0; + void* CNBuffer; + void* compressedBuffer; + void* decodedBuffer; + U32 randState = seed; + size_t cSize, testSize; + LZ4F_preferences_t prefs = { 0 }; + LZ4F_decompressionContext_t dCtx; + U64 crcOrig; + + // Create compressible test buffer + CNBuffer = malloc(COMPRESSIBLE_NOISE_LENGTH); + compressedBuffer = malloc(LZ4F_compressFrameBound(COMPRESSIBLE_NOISE_LENGTH, NULL)); + decodedBuffer = malloc(COMPRESSIBLE_NOISE_LENGTH); + FUZ_fillCompressibleNoiseBuffer(CNBuffer, COMPRESSIBLE_NOISE_LENGTH, compressibility, &randState); + crcOrig = XXH64(CNBuffer, COMPRESSIBLE_NOISE_LENGTH, 1); + + // Trivial tests : one-step frame + testSize = COMPRESSIBLE_NOISE_LENGTH; + DISPLAYLEVEL(3, "Using NULL preferences : \n"); + cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, NULL), CNBuffer, testSize, NULL); + if (LZ4F_isError(cSize)) goto _output_error; + DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); + + DISPLAYLEVEL(3, "Decompression test : \n"); + { size_t decodedBufferSize = COMPRESSIBLE_NOISE_LENGTH; size_t compressedBufferSize = cSize; BYTE* op = (BYTE*)decodedBuffer; @@ -254,30 +258,30 @@ int basicTests(U32 seed, double compressibility) errorCode = LZ4F_freeDecompressionContext(dCtx); if (LZ4F_isError(errorCode)) goto _output_error; - } - - DISPLAYLEVEL(3, "Using 64 KB block : \n"); - prefs.frameInfo.blockSizeID = max64KB; - prefs.frameInfo.contentChecksumFlag = contentChecksumEnabled; - cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &prefs), CNBuffer, testSize, &prefs); - if (LZ4F_isError(cSize)) goto _output_error; - DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); - - DISPLAYLEVEL(3, "without checksum : \n"); - prefs.frameInfo.contentChecksumFlag = noContentChecksum; - cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &prefs), CNBuffer, testSize, &prefs); - if (LZ4F_isError(cSize)) goto _output_error; - DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); - - DISPLAYLEVEL(3, "Using 256 KB block : \n"); - prefs.frameInfo.blockSizeID = max256KB; - prefs.frameInfo.contentChecksumFlag = contentChecksumEnabled; - cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &prefs), CNBuffer, testSize, &prefs); - if (LZ4F_isError(cSize)) goto _output_error; - DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); - - DISPLAYLEVEL(3, "Decompression test : \n"); - { + } + + DISPLAYLEVEL(3, "Using 64 KB block : \n"); + prefs.frameInfo.blockSizeID = max64KB; + prefs.frameInfo.contentChecksumFlag = contentChecksumEnabled; + cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &prefs), CNBuffer, testSize, &prefs); + if (LZ4F_isError(cSize)) goto _output_error; + DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); + + DISPLAYLEVEL(3, "without checksum : \n"); + prefs.frameInfo.contentChecksumFlag = noContentChecksum; + cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &prefs), CNBuffer, testSize, &prefs); + if (LZ4F_isError(cSize)) goto _output_error; + DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); + + DISPLAYLEVEL(3, "Using 256 KB block : \n"); + prefs.frameInfo.blockSizeID = max256KB; + prefs.frameInfo.contentChecksumFlag = contentChecksumEnabled; + cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &prefs), CNBuffer, testSize, &prefs); + if (LZ4F_isError(cSize)) goto _output_error; + DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); + + DISPLAYLEVEL(3, "Decompression test : \n"); + { size_t decodedBufferSize = COMPRESSIBLE_NOISE_LENGTH; unsigned maxBits = FUZ_highbit((U32)decodedBufferSize); BYTE* op = (BYTE*)decodedBuffer; @@ -308,51 +312,51 @@ int basicTests(U32 seed, double compressibility) errorCode = LZ4F_freeDecompressionContext(dCtx); if (LZ4F_isError(errorCode)) goto _output_error; - } - - DISPLAYLEVEL(3, "without checksum : \n"); - prefs.frameInfo.contentChecksumFlag = noContentChecksum; - cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &prefs), CNBuffer, testSize, &prefs); - if (LZ4F_isError(cSize)) goto _output_error; - DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); - - DISPLAYLEVEL(3, "Using 1 MB block : \n"); - prefs.frameInfo.blockSizeID = max1MB; - prefs.frameInfo.contentChecksumFlag = contentChecksumEnabled; - cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &prefs), CNBuffer, testSize, &prefs); - if (LZ4F_isError(cSize)) goto _output_error; - DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); - - DISPLAYLEVEL(3, "without checksum : \n"); - prefs.frameInfo.contentChecksumFlag = noContentChecksum; - cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &prefs), CNBuffer, testSize, &prefs); - if (LZ4F_isError(cSize)) goto _output_error; - DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); - - DISPLAYLEVEL(3, "Using 4 MB block : \n"); - prefs.frameInfo.blockSizeID = max4MB; - prefs.frameInfo.contentChecksumFlag = contentChecksumEnabled; - cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &prefs), CNBuffer, testSize, &prefs); - if (LZ4F_isError(cSize)) goto _output_error; - DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); - - DISPLAYLEVEL(3, "without checksum : \n"); - prefs.frameInfo.contentChecksumFlag = noContentChecksum; - cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &prefs), CNBuffer, testSize, &prefs); - if (LZ4F_isError(cSize)) goto _output_error; - DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); - - DISPLAY("Basic tests completed \n"); + } + + DISPLAYLEVEL(3, "without checksum : \n"); + prefs.frameInfo.contentChecksumFlag = noContentChecksum; + cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &prefs), CNBuffer, testSize, &prefs); + if (LZ4F_isError(cSize)) goto _output_error; + DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); + + DISPLAYLEVEL(3, "Using 1 MB block : \n"); + prefs.frameInfo.blockSizeID = max1MB; + prefs.frameInfo.contentChecksumFlag = contentChecksumEnabled; + cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &prefs), CNBuffer, testSize, &prefs); + if (LZ4F_isError(cSize)) goto _output_error; + DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); + + DISPLAYLEVEL(3, "without checksum : \n"); + prefs.frameInfo.contentChecksumFlag = noContentChecksum; + cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &prefs), CNBuffer, testSize, &prefs); + if (LZ4F_isError(cSize)) goto _output_error; + DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); + + DISPLAYLEVEL(3, "Using 4 MB block : \n"); + prefs.frameInfo.blockSizeID = max4MB; + prefs.frameInfo.contentChecksumFlag = contentChecksumEnabled; + cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &prefs), CNBuffer, testSize, &prefs); + if (LZ4F_isError(cSize)) goto _output_error; + DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); + + DISPLAYLEVEL(3, "without checksum : \n"); + prefs.frameInfo.contentChecksumFlag = noContentChecksum; + cSize = LZ4F_compressFrame(compressedBuffer, LZ4F_compressFrameBound(testSize, &prefs), CNBuffer, testSize, &prefs); + if (LZ4F_isError(cSize)) goto _output_error; + DISPLAYLEVEL(3, "Compressed %i bytes into a %i bytes frame \n", (int)testSize, (int)cSize); + + DISPLAY("Basic tests completed \n"); _end: - free(CNBuffer); - free(compressedBuffer); - free(decodedBuffer); - return testResult; + free(CNBuffer); + free(compressedBuffer); + free(decodedBuffer); + return testResult; _output_error: - testResult = 1; - DISPLAY("Error detected ! \n"); - goto _end; + testResult = 1; + DISPLAY("Error detected ! \n"); + goto _end; } @@ -361,44 +365,48 @@ static void locateBuffDiff(const void* buff1, const void* buff2, size_t size, un int p=0; BYTE* b1=(BYTE*)buff1; BYTE* b2=(BYTE*)buff2; - if (nonContiguous) { DISPLAY("Non-contiguous output test (%i bytes)\n", (int)size); return; } + if (nonContiguous) + { + DISPLAY("Non-contiguous output test (%i bytes)\n", (int)size); + return; + } while (b1[p]==b2[p]) p++; DISPLAY("Error at pos %i/%i : %02X != %02X \n", p, (int)size, b1[p], b2[p]); - } +} static const U32 srcDataLength = 9 MB; /* needs to be > 2x4MB to test large blocks */ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressibility) { - unsigned testResult = 0; - unsigned testNb = 0; - void* srcBuffer = NULL; - void* compressedBuffer = NULL; - void* decodedBuffer = NULL; - U32 coreRand = seed; - LZ4F_decompressionContext_t dCtx = NULL; - LZ4F_compressionContext_t cCtx = NULL; + unsigned testResult = 0; + unsigned testNb = 0; + void* srcBuffer = NULL; + void* compressedBuffer = NULL; + void* decodedBuffer = NULL; + U32 coreRand = seed; + LZ4F_decompressionContext_t dCtx = NULL; + LZ4F_compressionContext_t cCtx = NULL; size_t result; XXH64_stateSpace_t xxh64; # define CHECK(cond, ...) if (cond) { DISPLAY("Error => "); DISPLAY(__VA_ARGS__); \ - DISPLAY(" (seed %u, test nb %i) \n", seed, testNb); goto _output_error; } + DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); goto _output_error; } - // Create buffers - result = LZ4F_createDecompressionContext(&dCtx, LZ4F_VERSION); + // Create buffers + result = LZ4F_createDecompressionContext(&dCtx, LZ4F_VERSION); CHECK(LZ4F_isError(result), "Allocation failed (error %i)", (int)result); - result = LZ4F_createCompressionContext(&cCtx, LZ4F_VERSION); + result = LZ4F_createCompressionContext(&cCtx, LZ4F_VERSION); CHECK(LZ4F_isError(result), "Allocation failed (error %i)", (int)result); - srcBuffer = malloc(srcDataLength); + srcBuffer = malloc(srcDataLength); CHECK(srcBuffer==NULL, "srcBuffer Allocation failed"); - compressedBuffer = malloc(LZ4F_compressFrameBound(srcDataLength, NULL)); + compressedBuffer = malloc(LZ4F_compressFrameBound(srcDataLength, NULL)); CHECK(compressedBuffer==NULL, "compressedBuffer Allocation failed"); - decodedBuffer = malloc(srcDataLength); + decodedBuffer = malloc(srcDataLength); CHECK(decodedBuffer==NULL, "decodedBuffer Allocation failed"); - FUZ_fillCompressibleNoiseBuffer(srcBuffer, srcDataLength, compressibility, &coreRand); + FUZ_fillCompressibleNoiseBuffer(srcBuffer, srcDataLength, compressibility, &coreRand); // jump to requested testNb - for (testNb =0; testNb < startTest; testNb++) (void)FUZ_rand(&coreRand); // sync randomizer + for (testNb =0; testNb < startTest; testNb++) (void)FUZ_rand(&coreRand); // sync randomizer // main fuzzer loop for ( ; testNb < nbTests; testNb++) @@ -423,7 +431,7 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi prefs.frameInfo.contentChecksumFlag = CCflag; prefs.autoFlush = autoflush; - DISPLAYUPDATE(2, "\r%5i ", testNb); + DISPLAYUPDATE(2, "\r%5u ", testNb); crcOrig = XXH64((BYTE*)srcBuffer+srcStart, (U32)srcSize, 1); if ((FUZ_rand(&randState)&0xF) == 2) @@ -450,7 +458,7 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi size_t oSize = oend-op; unsigned forceFlush = ((FUZ_rand(&randState) & 3) == 1); if (iSize > (size_t)(iend-ip)) iSize = iend-ip; - cOptions.stableSrc = ((FUZ_rand(&randState) && 3) == 1); + cOptions.stableSrc = ((FUZ_rand(&randState) & 3) == 1); result = LZ4F_compressUpdate(cCtx, op, oSize, ip, iSize, &cOptions); CHECK(LZ4F_isError(result), "Compression failed (error %i)", (int)result); @@ -508,25 +516,25 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi } } - DISPLAYLEVEL(2, "\rAll tests completed \n"); + DISPLAYLEVEL(2, "\rAll tests completed \n"); _end: LZ4F_freeDecompressionContext(dCtx); LZ4F_freeCompressionContext(cCtx); - free(srcBuffer); - free(compressedBuffer); - free(decodedBuffer); + free(srcBuffer); + free(compressedBuffer); + free(decodedBuffer); - if (pause) + if (pause) { DISPLAY("press enter to finish \n"); getchar(); } - return testResult; + return testResult; _output_error: - testResult = 1; - goto _end; + testResult = 1; + goto _end; } @@ -536,7 +544,7 @@ int FUZ_usage(void) DISPLAY( " %s [args]\n", programName); DISPLAY( "\n"); DISPLAY( "Arguments :\n"); - DISPLAY( " -i# : Nb of tests (default:%i) \n", nbTestsDefault); + DISPLAY( " -i# : Nb of tests (default:%u) \n", nbTestsDefault); DISPLAY( " -s# : Select seed (default:prompt user)\n"); DISPLAY( " -t# : Select starting test number (default:0)\n"); DISPLAY( " -p# : Select compressibility in %% (default:%i%%)\n", FUZ_COMPRESSIBILITY_DEFAULT); @@ -567,7 +575,13 @@ int main(int argc, char** argv) // Decode command (note : aggregated commands are allowed) if (argument[0]=='-') { - if (!strcmp(argument, "--no-prompt")) { no_prompt=1; seedset=1; displayLevel=1; continue; } + if (!strcmp(argument, "--no-prompt")) + { + no_prompt=1; + seedset=1; + displayLevel=1; + continue; + } argument++; while (*argument!=0) @@ -596,7 +610,8 @@ int main(int argc, char** argv) break; case 's': argument++; - seed=0; seedset=1; + seed=0; + seedset=1; while ((*argument>='0') && (*argument<='9')) { seed *= 10; @@ -630,7 +645,8 @@ int main(int argc, char** argv) argument++; pause = 1; break; - default: ; + default: + ; return FUZ_usage(); } } diff --git a/programs/lz4cli.c b/programs/lz4cli.c index 70f0760..2d612e7 100644 --- a/programs/lz4cli.c +++ b/programs/lz4cli.c @@ -48,6 +48,10 @@ # pragma warning(disable : 4127) // disable: C4127: conditional expression is constant #endif +#ifdef __clang__ +# pragma clang diagnostic ignored "-Wunused-const-variable" // const variable one is really used ! +#endif + #define _FILE_OFFSET_BITS 64 // Large file support on 32-bits unix #define _POSIX_SOURCE 1 // for fileno() within on unix @@ -91,7 +95,7 @@ #if defined(_MSC_VER) // Visual Studio # define swap32 _byteswap_ulong -#elif GCC_VERSION >= 403 +#elif (GCC_VERSION >= 403) || defined(__clang__) # define swap32 __builtin_bswap32 #else static inline unsigned int swap32(unsigned int x) -- cgit v0.12