From de4667011a077d403b3b9722ed021097005e68e3 Mon Sep 17 00:00:00 2001 From: Lucas Holt Date: Tue, 11 Sep 2018 21:18:43 -0400 Subject: FIx pkgconfig file installation on MidnightBSD --- lib/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Makefile b/lib/Makefile index 88d9b4f..d7c8cb4 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -143,7 +143,7 @@ libdir ?= $(LIBDIR) INCLUDEDIR ?= $(prefix)/include includedir ?= $(INCLUDEDIR) -ifneq (,$(filter $(OS),OpenBSD FreeBSD NetBSD DragonFly)) +ifneq (,$(filter $(OS),OpenBSD FreeBSD NetBSD DragonFly MidnightBSD)) PKGCONFIGDIR ?= $(prefix)/libdata/pkgconfig else PKGCONFIGDIR ?= $(libdir)/pkgconfig -- cgit v1.2.3 From 879f3dae115e4795674dbfbdbc91ccc801187726 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 12 Sep 2018 10:48:19 -0700 Subject: fixed NEWS for v1.8.3 --- NEWS | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS b/NEWS index 13a9a1c..57a75cf 100644 --- a/NEWS +++ b/NEWS @@ -2,6 +2,7 @@ v1.8.3 perf: minor decompression speed improvement (~+2%) with gcc fix : corruption in v1.8.2 at level 9 for files > 64KB under rare conditions (#560) cli : new command --fast, by @jennifermliu +cli : fixed elapsed time, and added cpu load indicator (on -vv) (#555) api : LZ4_decompress_safe_partial() now decodes exactly the nb of bytes requested (feature request #566) build : added Haiku target, by @fbrosson, and MidnightBSD, by @laffer1 doc : updated documentation regarding dictionary compression -- cgit v1.2.3 From 4a84836c132781e12bafd5f68923c2647b34e2b7 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 12 Sep 2018 10:50:37 -0700 Subject: removed image from repository used for v1.8.2 announcement --- doc/images/usingCDict_1_8_2.png | Bin 81858 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 doc/images/usingCDict_1_8_2.png diff --git a/doc/images/usingCDict_1_8_2.png b/doc/images/usingCDict_1_8_2.png deleted file mode 100644 index 9434198..0000000 Binary files a/doc/images/usingCDict_1_8_2.png and /dev/null differ -- cgit v1.2.3 From 86023f01f207f3561766bf33a0c3d935786ae5f9 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 13 Sep 2018 14:29:41 -0700 Subject: avoid final trailing comma for enum lists as detected in #485 by @JoachimSchneider. Refactored the c_standards tests so that these issues get automatically detected in CI tests. --- Makefile | 14 +++++++------- doc/lz4_manual.html | 6 +++--- doc/lz4frame_manual.html | 17 ++++++++++------- examples/frameCompress.c | 8 ++++---- lib/lz4frame.c | 10 +++++----- lib/lz4frame.h | 20 ++++++++++---------- lib/lz4hc.c | 2 +- 7 files changed, 40 insertions(+), 37 deletions(-) diff --git a/Makefile b/Makefile index 69a34b7..e8e6473 100644 --- a/Makefile +++ b/Makefile @@ -50,10 +50,10 @@ endif default: lib-release lz4-release .PHONY: all -all: allmost manuals +all: allmost examples manuals .PHONY: allmost -allmost: lib lz4 examples +allmost: lib lz4 .PHONY: lib lib-release liblz4.a lib: liblz4.a @@ -181,10 +181,10 @@ ctocpptest: clean CC=$(TESTCC) $(MAKE) -C $(TESTDIR) CFLAGS="$(CFLAGS)" all c_standards: clean - CFLAGS="-std=c90 -Werror" $(MAKE) clean allmost - CFLAGS="-std=gnu90 -Werror" $(MAKE) clean allmost - CFLAGS="-std=c99 -Werror" $(MAKE) clean allmost - CFLAGS="-std=gnu99 -Werror" $(MAKE) clean allmost - CFLAGS="-std=c11 -Werror" $(MAKE) clean allmost + $(MAKE) clean; CFLAGS="-std=c90 -Werror -pedantic -Wno-long-long -Wno-variadic-macros" $(MAKE) allmost + $(MAKE) clean; CFLAGS="-std=gnu90 -Werror -pedantic -Wno-long-long -Wno-variadic-macros" $(MAKE) allmost + $(MAKE) clean; CFLAGS="-std=c99 -Werror -pedantic" $(MAKE) all + $(MAKE) clean; CFLAGS="-std=gnu99 -Werror -pedantic" $(MAKE) all + $(MAKE) clean; CFLAGS="-std=c11 -Werror" $(MAKE) all endif diff --git a/doc/lz4_manual.html b/doc/lz4_manual.html index 6ebf8d2..c91ae10 100644 --- a/doc/lz4_manual.html +++ b/doc/lz4_manual.html @@ -21,7 +21,7 @@

Introduction

-  LZ4 is lossless compression algorithm, providing compression speed at 400 MB/s per core,
+  LZ4 is lossless compression algorithm, providing compression speed at 500 MB/s per core,
   scalable with multi-cores CPU. It features an extremely fast decoder, with speed in
   multiple GB/s per core, typically reaching RAM speed limits on multi-core systems.
 
@@ -37,8 +37,8 @@
 
   An additional format, called LZ4 frame specification (doc/lz4_Frame_format.md),
   take care of encoding standard metadata alongside LZ4-compressed blocks.
-  If your application requires interoperability, it's recommended to use it.
-  A library is provided to take care of it, see lz4frame.h.
+  Frame format is required for interoperability.
+  It is delivered through a companion API, declared in lz4frame.h.
 

Version


diff --git a/doc/lz4frame_manual.html b/doc/lz4frame_manual.html
index fb8e0ce..72e6782 100644
--- a/doc/lz4frame_manual.html
+++ b/doc/lz4frame_manual.html
@@ -84,19 +84,21 @@
   LZ4F_blockChecksum_t   blockChecksumFlag;   /* 1: each block followed by a checksum of block's compressed data; 0: disabled (default) */
 } LZ4F_frameInfo_t;
 

makes it possible to set or read frame parameters. - It's not required to set all fields, as long as the structure was initially memset() to zero. - For all fields, 0 sets it to default value + Structure must be first init to 0, using memset() or LZ4F_INIT_FRAMEINFO, + setting all parameters to default. + It's then possible to update selectively some parameters


typedef struct {
   LZ4F_frameInfo_t frameInfo;
   int      compressionLevel;    /* 0: default (fast mode); values > LZ4HC_CLEVEL_MAX count as LZ4HC_CLEVEL_MAX; values < 0 trigger "fast acceleration" */
-  unsigned autoFlush;           /* 1: always flush, to reduce usage of internal buffers */
-  unsigned favorDecSpeed;       /* 1: parser favors decompression speed vs compression ratio. Only works for high compression modes (>= LZ4LZ4HC_CLEVEL_OPT_MIN) */  /* >= v1.8.2 */
+  unsigned autoFlush;           /* 1: always flush; reduces usage of internal buffers */
+  unsigned favorDecSpeed;       /* 1: parser favors decompression speed vs compression ratio. Only works for high compression modes (>= LZ4HC_CLEVEL_OPT_MIN) */  /* v1.8.2+ */
   unsigned reserved[3];         /* must be zero for forward compatibility */
 } LZ4F_preferences_t;
-

makes it possible to supply detailed compression parameters to the stream interface. - Structure is presumed initially memset() to zero, representing default settings. +

makes it possible to supply advanced compression instructions to streaming interface. + Structure must be first init to 0, using memset() or LZ4F_INIT_PREFERENCES, + setting all parameters to default. All reserved fields must be set to zero.


@@ -295,7 +297,8 @@ LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_dctx* dctx); and start a new one using same context resources.


-
typedef enum { LZ4F_LIST_ERRORS(LZ4F_GENERATE_ENUM) } LZ4F_errorCodes;
+
typedef enum { LZ4F_LIST_ERRORS(LZ4F_GENERATE_ENUM)
+              _LZ4F_dummy_error_enum_for_c89_never_used } LZ4F_errorCodes;
 

Bulk processing dictionary API


 
diff --git a/examples/frameCompress.c b/examples/frameCompress.c
index a0c5d3d..2cc4649 100644
--- a/examples/frameCompress.c
+++ b/examples/frameCompress.c
@@ -184,8 +184,8 @@ decompress_file_internal(FILE* f_in, FILE* f_out,
     while (ret != 0) {
         /* Load more input */
         size_t readSize = firstChunk ? filled : fread(src, 1, srcCapacity, f_in); firstChunk=0;
-        const void* srcPtr = src + alreadyConsumed; alreadyConsumed=0;
-        const void* const srcEnd = srcPtr + readSize;
+        const void* srcPtr = (const char*)src + alreadyConsumed; alreadyConsumed=0;
+        const void* const srcEnd = (const char*)srcPtr + readSize;
         if (readSize == 0 || ferror(f_in)) {
             printf("Decompress: not enough input or error reading file\n");
             return 1;
@@ -198,7 +198,7 @@ decompress_file_internal(FILE* f_in, FILE* f_out,
         while (srcPtr < srcEnd && ret != 0) {
             /* Any data within dst has been flushed at this stage */
             size_t dstSize = dstCapacity;
-            size_t srcSize = srcEnd - srcPtr;
+            size_t srcSize = (const char*)srcEnd - (const char*)srcPtr;
             ret = LZ4F_decompress(dctx, dst, &dstSize, srcPtr, &srcSize, /* LZ4F_decompressOptions_t */ NULL);
             if (LZ4F_isError(ret)) {
                 printf("Decompression error: %s\n", LZ4F_getErrorName(ret));
@@ -207,7 +207,7 @@ decompress_file_internal(FILE* f_in, FILE* f_out,
             /* Flush output */
             if (dstSize != 0) safe_fwrite(dst, 1, dstSize, f_out);
             /* Update input */
-            srcPtr += srcSize;
+            srcPtr = (const char*)srcPtr + srcSize;
         }
 
         assert(srcPtr <= srcEnd);
diff --git a/lib/lz4frame.c b/lib/lz4frame.c
index 08bf0fa..a30de48 100644
--- a/lib/lz4frame.c
+++ b/lib/lz4frame.c
@@ -33,8 +33,8 @@ You can contact the author at :
 */
 
 /* LZ4F is a stand-alone API to create LZ4-compressed Frames
-*  in full conformance with specification v1.5.0
-*  All related operations, including memory management, are handled by the library.
+*  in full conformance with specification v1.6.1 .
+*  This library rely upon memory management capabilities.
 * */
 
 
@@ -63,9 +63,9 @@ You can contact the author at :
 *  Memory routines
 **************************************/
 #include    /* malloc, calloc, free */
-#define ALLOC(s)   malloc(s)
-#define ALLOC_AND_ZERO(s)   calloc(1,s)
-#define FREEMEM        free
+#define ALLOC(s)       malloc(s)
+#define ALLOC_AND_ZERO(s)  calloc(1,(s))
+#define FREEMEM(p)     free(p)
 #include    /* memset, memcpy, memmove */
 #define MEM_INIT       memset
 
diff --git a/lib/lz4frame.h b/lib/lz4frame.h
index 75f1fd9..fc30f6f 100644
--- a/lib/lz4frame.h
+++ b/lib/lz4frame.h
@@ -427,15 +427,15 @@ LZ4FLIB_API void LZ4F_resetDecompressionContext(LZ4F_dctx* dctx);   /* always su
 extern "C" {
 #endif
 
-/* These declarations are not stable and may change in the future. They are
- * therefore only safe to depend on when the caller is statically linked
- * against the library. To access their declarations, define
- * LZ4F_STATIC_LINKING_ONLY.
+/* These declarations are not stable and may change in the future.
+ * They are therefore only safe to depend on
+ * when the caller is statically linked against the library.
+ * To access their declarations, define LZ4F_STATIC_LINKING_ONLY.
  *
- * There is a further protection mechanism where these symbols aren't published
- * into shared/dynamic libraries. You can override this behavior and force
- * them to be published by defining LZ4F_PUBLISH_STATIC_FUNCTIONS. Use at
- * your own risk.
+ * By default, these symbols aren't published into shared/dynamic libraries.
+ * You can override this behavior and force them to be published
+ * by defining LZ4F_PUBLISH_STATIC_FUNCTIONS.
+ * Use at your own risk.
  */
 #ifdef LZ4F_PUBLISH_STATIC_FUNCTIONS
 #define LZ4FLIB_STATIC_API LZ4FLIB_API
@@ -471,12 +471,12 @@ extern "C" {
 #define LZ4F_GENERATE_ENUM(ENUM) LZ4F_##ENUM,
 
 /* enum list is exposed, to handle specific errors */
-typedef enum { LZ4F_LIST_ERRORS(LZ4F_GENERATE_ENUM) } LZ4F_errorCodes;
+typedef enum { LZ4F_LIST_ERRORS(LZ4F_GENERATE_ENUM)
+              _LZ4F_dummy_error_enum_for_c89_never_used } LZ4F_errorCodes;
 
 LZ4FLIB_STATIC_API LZ4F_errorCodes LZ4F_getErrorCode(size_t functionResult);
 
 
-
 /**********************************
  *  Bulk processing dictionary API
  *********************************/
diff --git a/lib/lz4hc.c b/lib/lz4hc.c
index e913ee7..87f4cdb 100644
--- a/lib/lz4hc.c
+++ b/lib/lz4hc.c
@@ -400,7 +400,7 @@ int LZ4HC_InsertAndFindBestMatch(LZ4HC_CCtx_internal* const hc4,   /* Index tabl
 typedef enum {
     noLimit = 0,
     limitedOutput = 1,
-    limitedDestSize = 2,
+    limitedDestSize = 2
 } limitedOutput_directive;
 
 /* LZ4HC_encodeSequence() :
-- 
cgit v1.2.3


From a803230f67af4649680446b9f1daaef4730a811e Mon Sep 17 00:00:00 2001
From: Yann Collet 
Date: Thu, 13 Sep 2018 16:02:11 -0700
Subject: unpublish static-only function

these functions are now unpublished in dll by default.
One needs to opt-in, using macro LZ4_PUBLISH_STATIC_FUNCTIONS.

used this opportunity to update a bunch of api comments in lz4.h
---
 doc/lz4_manual.html      | 131 +++++++++++++++++++--------------------
 doc/lz4frame_manual.html |  14 +++--
 lib/lz4.h                | 155 +++++++++++++++++++++++++----------------------
 3 files changed, 152 insertions(+), 148 deletions(-)

diff --git a/doc/lz4_manual.html b/doc/lz4_manual.html
index 6ebf8d2..39a48f3 100644
--- a/doc/lz4_manual.html
+++ b/doc/lz4_manual.html
@@ -21,7 +21,7 @@
 
 

Introduction

-  LZ4 is lossless compression algorithm, providing compression speed at 400 MB/s per core,
+  LZ4 is lossless compression algorithm, providing compression speed at 500 MB/s per core,
   scalable with multi-cores CPU. It features an extremely fast decoder, with speed in
   multiple GB/s per core, typically reaching RAM speed limits on multi-core systems.
 
@@ -37,15 +37,15 @@
 
   An additional format, called LZ4 frame specification (doc/lz4_Frame_format.md),
   take care of encoding standard metadata alongside LZ4-compressed blocks.
-  If your application requires interoperability, it's recommended to use it.
-  A library is provided to take care of it, see lz4frame.h.
+  Frame format is required for interoperability.
+  It is delivered through a companion API, declared in lz4frame.h.
 

Version


 
 
int LZ4_versionNumber (void);  /**< library version number; useful to check dll version */
 

-
const char* LZ4_versionString (void);   /**< library version string; unseful to check dll version */
+
const char* LZ4_versionString (void);   /**< library version string; useful to check dll version */
 

Tuning parameter


 
@@ -53,8 +53,8 @@
 # define LZ4_MEMORY_USAGE 14
 #endif
 

Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.) - Increasing memory usage improves compression ratio - Reduced memory usage may improve speed, thanks to cache effect + Increasing memory usage improves compression ratio. + Reduced memory usage may improve speed, thanks to better cache locality. Default value is 14, for 16KB, which nicely fits into Intel x86 L1 cache


@@ -68,21 +68,21 @@ It also runs faster, so it's a recommended setting. If the function cannot compress 'src' into a more limited 'dst' budget, compression stops *immediately*, and the function result is zero. - Note : as a consequence, 'dst' content is not valid. - Note 2 : This function is protected against buffer overflow scenarios (never writes outside 'dst' buffer, nor read outside 'source' buffer). + In which case, 'dst' content is undefined (invalid). srcSize : max supported value is LZ4_MAX_INPUT_SIZE. dstCapacity : size of buffer 'dst' (which must be already allocated) - return : the number of bytes written into buffer 'dst' (necessarily <= dstCapacity) - or 0 if compression fails + @return : the number of bytes written into buffer 'dst' (necessarily <= dstCapacity) + or 0 if compression fails + Note : This function is protected against buffer overflow scenarios (never writes outside 'dst' buffer, nor read outside 'source' buffer).


int LZ4_decompress_safe (const char* src, char* dst, int compressedSize, int dstCapacity);
 

compressedSize : is the exact complete size of the compressed block. dstCapacity : is the size of destination buffer, which must be already allocated. - return : the number of bytes decompressed into destination buffer (necessarily <= dstCapacity) + @return : the number of bytes decompressed into destination buffer (necessarily <= dstCapacity) If destination buffer is not large enough, decoding will stop and output an error code (negative value). If the source stream is detected malformed, the function will stop decoding and return a negative result. - This function is protected against malicious data packets. + Note : This function is protected against malicious data packets (never writes outside 'dst' buffer, nor read outside 'source' buffer).


Advanced Functions


@@ -107,10 +107,11 @@
 
 
int LZ4_sizeofState(void);
 int LZ4_compress_fast_extState (void* state, const char* src, char* dst, int srcSize, int dstCapacity, int acceleration);
-

Same compression function, just using an externally allocated memory space to store compression state. - Use LZ4_sizeofState() to know how much memory must be allocated, - and allocate it on 8-bytes boundaries (using malloc() typically). - Then, provide this buffer as 'void* state' to compression function. +

Same as LZ4_compress_fast(), using an externally allocated memory space for its state. + Use LZ4_sizeofState() to know how much memory must be allocated, + and allocate it on 8-bytes boundaries (using `malloc()` typically). + Then, provide this buffer as `void* state` to compression function. +


int LZ4_compress_destSize (const char* src, char* dst, int* srcSizePtr, int targetDstSize);
@@ -132,7 +133,7 @@ int LZ4_compress_fast_extState (void* state, const char* src, char* dst, int src
   and now `LZ4_decompress_safe()` can be as fast and sometimes faster than `LZ4_decompress_fast()`.
   Moreover, LZ4_decompress_fast() is not protected vs malformed input, as it doesn't perform full validation of compressed data.
   As a consequence, this function is no longer recommended, and may be deprecated in future versions.
-  It's only remaining specificity is that it can decompress data without knowing its compressed size.
+  It's last remaining specificity is that it can decompress data without knowing its compressed size.
 
   originalSize : is the uncompressed size to regenerate.
                  `dst` must be already allocated, its size must be >= 'originalSize' bytes.
@@ -175,13 +176,6 @@ int LZ4_compress_fast_extState (void* state, const char* src, char* dst, int src
 
 

Streaming Compression Functions


 
-
LZ4_stream_t* LZ4_createStream(void);
-int           LZ4_freeStream (LZ4_stream_t* streamPtr);
-

LZ4_createStream() will allocate and initialize an `LZ4_stream_t` structure. - LZ4_freeStream() releases its memory. - -


-
void LZ4_resetStream (LZ4_stream_t* streamPtr);
 

An LZ4_stream_t structure can be allocated once and re-used multiple times. Use this function to start compressing a new stream. @@ -198,7 +192,7 @@ int LZ4_freeStream (LZ4_stream_t* streamPtr);

int LZ4_compress_fast_continue (LZ4_stream_t* streamPtr, const char* src, char* dst, int srcSize, int dstCapacity, int acceleration);
 

Compress 'src' content using data from previously compressed blocks, for better compression ratio. - 'dst' buffer must be already allocated. + 'dst' buffer must be already allocated. If dstCapacity >= LZ4_compressBound(srcSize), compression is guaranteed to succeed, and runs faster. @return : size of compressed block @@ -206,10 +200,10 @@ int LZ4_freeStream (LZ4_stream_t* streamPtr); Note 1 : Each invocation to LZ4_compress_fast_continue() generates a new block. Each block has precise boundaries. + Each block must be decompressed separately, calling LZ4_decompress_*() with relevant metadata. It's not possible to append blocks together and expect a single invocation of LZ4_decompress_*() to decompress them together. - Each block must be decompressed separately, calling LZ4_decompress_*() with associated metadata. - Note 2 : The previous 64KB of source data is __assumed__ to remain present, unmodified, at same address in memory! + Note 2 : The previous 64KB of source data is __assumed__ to remain present, unmodified, at same address in memory ! Note 3 : When input is structured as a double-buffer, each buffer can have any size, including < 64 KB. Make sure that buffers are separated, by at least one byte. @@ -217,7 +211,7 @@ int LZ4_freeStream (LZ4_stream_t* streamPtr); Note 4 : If input buffer is a ring-buffer, it can have any size, including < 64 KB. - Note 5 : After an error, the stream status is invalid, it can only be reset or freed. + Note 5 : After an error, the stream status is undefined (invalid), it can only be reset or freed.


@@ -250,7 +244,7 @@ int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream);


int LZ4_decoderRingBufferSize(int maxBlockSize);
-#define LZ4_DECODER_RING_BUFFER_SIZE(mbs) (65536 + 14 + (mbs))  /* for static allocation; mbs presumed valid */
+#define LZ4_DECODER_RING_BUFFER_SIZE(maxBlockSize) (65536 + 14 + (maxBlockSize))  /* for static allocation; maxBlockSize presumed valid */
 

Note : in a ring buffer scenario (optional), blocks are presumed decompressed next to each other up to the moment there is not enough remaining space for next block (remainingSize < maxBlockSize), @@ -295,32 +289,34 @@ int LZ4_decompress_fast_usingDict (const char* src, char* dst, int originalSize,

These decoding functions work the same as a combination of LZ4_setStreamDecode() followed by LZ4_decompress_*_continue() They are stand-alone, and don't need an LZ4_streamDecode_t structure. - Dictionary is presumed stable : it must remain accessible and unmodified during next decompression. + Dictionary is presumed stable : it must remain accessible and unmodified during decompression. + Performance tip : Decompression speed can be substantially increased + when dst == dictStart + dictSize.


Unstable declarations

- Declarations in this section should be considered unstable.
- Use at your own peril, etc., etc.
- They may be removed in the future.
- Their signatures may change.
+ Declarations in this section must be considered unstable.
+ Their signatures may change, or may be removed in the future.
+ They are therefore only safe to depend on
+ when the caller is statically linked against the library.
+ To access their declarations, define LZ4_STATIC_LINKING_ONLY.
 
-
void LZ4_resetStream_fast (LZ4_stream_t* streamPtr);
+
LZ4LIB_STATIC_API void LZ4_resetStream_fast (LZ4_stream_t* streamPtr);
 

Use this, like LZ4_resetStream(), to prepare a context for a new chain of calls to a streaming API (e.g., LZ4_compress_fast_continue()). Note: - Using this in advance of a non- streaming-compression function is redundant, - and potentially bad for performance, since they all perform their own custom - reset internally. + Using this in advance of a non-streaming-compression function is redundant, + since they all perform their own custom reset internally. Differences from LZ4_resetStream(): When an LZ4_stream_t is known to be in a internally coherent state, - it can often be prepared for a new compression with almost no work, only - sometimes falling back to the full, expensive reset that is always required - when the stream is in an indeterminate state (i.e., the reset performed by - LZ4_resetStream()). + it can often be prepared for a new compression with almost no work, + only sometimes falling back to the full, expensive reset + that is always required when the stream is in an indeterminate state + (i.e., the reset performed by LZ4_resetStream()). LZ4_streams are guaranteed to be in a valid state when: - returned from LZ4_createStream() @@ -339,22 +335,21 @@ int LZ4_decompress_fast_usingDict (const char* src, char* dst, int originalSize,


-
int LZ4_compress_fast_extState_fastReset (void* state, const char* src, char* dst, int srcSize, int dstCapacity, int acceleration);
+
LZ4LIB_STATIC_API int LZ4_compress_fast_extState_fastReset (void* state, const char* src, char* dst, int srcSize, int dstCapacity, int acceleration);
 

A variant of LZ4_compress_fast_extState(). - Using this variant avoids an expensive initialization step. It is only safe - to call if the state buffer is known to be correctly initialized already - (see above comment on LZ4_resetStream_fast() for a definition of "correctly - initialized"). From a high level, the difference is that this function - initializes the provided state with a call to something like - LZ4_resetStream_fast() while LZ4_compress_fast_extState() starts with a - call to LZ4_resetStream(). + Using this variant avoids an expensive initialization step. + It is only safe to call if the state buffer is known to be correctly initialized already + (see above comment on LZ4_resetStream_fast() for a definition of "correctly initialized"). + From a high level, the difference is that + this function initializes the provided state with a call to something like LZ4_resetStream_fast() + while LZ4_compress_fast_extState() starts with a call to LZ4_resetStream().


-
void LZ4_attach_dictionary(LZ4_stream_t *working_stream, const LZ4_stream_t *dictionary_stream);
-

This is an experimental API that allows for the efficient use of a - static dictionary many times. +

LZ4LIB_STATIC_API void LZ4_attach_dictionary(LZ4_stream_t* workingStream, const LZ4_stream_t* dictionaryStream);
+

This is an experimental API that allows + efficient use of a static dictionary many times. Rather than re-loading the dictionary buffer into a working context before each compression, or copying a pre-loaded dictionary's LZ4_stream_t into a @@ -365,8 +360,8 @@ int LZ4_decompress_fast_usingDict (const char* src, char* dst, int originalSize, Currently, only streams which have been prepared by LZ4_loadDict() should be expected to work. - Alternatively, the provided dictionary stream pointer may be NULL, in which - case any existing dictionary stream is unset. + Alternatively, the provided dictionaryStream may be NULL, + in which case any existing dictionary stream is unset. If a dictionary is provided, it replaces any pre-existing stream history. The dictionary contents are the only history that can be referenced and @@ -381,9 +376,9 @@ int LZ4_decompress_fast_usingDict (const char* src, char* dst, int originalSize,


Private definitions

- Do not use these definitions.
- They are exposed to allow static allocation of `LZ4_stream_t` and `LZ4_streamDecode_t`.
- Using these definitions will expose code to API and/or ABI break in future versions of the library.
+ Do not use these definitions directly.
+ They are only exposed to allow static allocation of `LZ4_stream_t` and `LZ4_streamDecode_t`.
+ Accessing members will expose code to API and/or ABI break in future versions of the library.
 
typedef struct {
@@ -406,11 +401,11 @@ union LZ4_stream_u {
     unsigned long long table[LZ4_STREAMSIZE_U64];
     LZ4_stream_t_internal internal_donotuse;
 } ;  /* previously typedef'd to LZ4_stream_t */
-

information structure to track an LZ4 stream. - init this structure before first use. - note : only use in association with static linking ! - this definition is not API/ABI safe, - it may change in a future version ! +

information structure to track an LZ4 stream. + init this structure with LZ4_resetStream() before first use. + note : only use in association with static linking ! + this definition is not API/ABI safe, + it may change in a future version !


@@ -420,11 +415,11 @@ union LZ4_streamDecode_u { unsigned long long table[LZ4_STREAMDECODESIZE_U64]; LZ4_streamDecode_t_internal internal_donotuse; } ;
/* previously typedef'd to LZ4_streamDecode_t */ -

information structure to track an LZ4 stream during decompression. - init this structure using LZ4_setStreamDecode (or memset()) before first use - note : only use in association with static linking ! - this definition is not API/ABI safe, - and may change in a future version ! +

information structure to track an LZ4 stream during decompression. + init this structure using LZ4_setStreamDecode() before first use. + note : only use in association with static linking ! + this definition is not API/ABI safe, + and may change in a future version !


diff --git a/doc/lz4frame_manual.html b/doc/lz4frame_manual.html index fb8e0ce..af1853e 100644 --- a/doc/lz4frame_manual.html +++ b/doc/lz4frame_manual.html @@ -84,19 +84,21 @@ LZ4F_blockChecksum_t blockChecksumFlag; /* 1: each block followed by a checksum of block's compressed data; 0: disabled (default) */ } LZ4F_frameInfo_t;

makes it possible to set or read frame parameters. - It's not required to set all fields, as long as the structure was initially memset() to zero. - For all fields, 0 sets it to default value + Structure must be first init to 0, using memset() or LZ4F_INIT_FRAMEINFO, + setting all parameters to default. + It's then possible to update selectively some parameters


typedef struct {
   LZ4F_frameInfo_t frameInfo;
   int      compressionLevel;    /* 0: default (fast mode); values > LZ4HC_CLEVEL_MAX count as LZ4HC_CLEVEL_MAX; values < 0 trigger "fast acceleration" */
-  unsigned autoFlush;           /* 1: always flush, to reduce usage of internal buffers */
-  unsigned favorDecSpeed;       /* 1: parser favors decompression speed vs compression ratio. Only works for high compression modes (>= LZ4LZ4HC_CLEVEL_OPT_MIN) */  /* >= v1.8.2 */
+  unsigned autoFlush;           /* 1: always flush; reduces usage of internal buffers */
+  unsigned favorDecSpeed;       /* 1: parser favors decompression speed vs compression ratio. Only works for high compression modes (>= LZ4HC_CLEVEL_OPT_MIN) */  /* v1.8.2+ */
   unsigned reserved[3];         /* must be zero for forward compatibility */
 } LZ4F_preferences_t;
-

makes it possible to supply detailed compression parameters to the stream interface. - Structure is presumed initially memset() to zero, representing default settings. +

makes it possible to supply advanced compression instructions to streaming interface. + Structure must be first init to 0, using memset() or LZ4F_INIT_PREFERENCES, + setting all parameters to default. All reserved fields must be set to zero.


diff --git a/lib/lz4.h b/lib/lz4.h index 059ef7c..6d67bfc 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -103,7 +103,7 @@ extern "C" { #define LZ4_VERSION_STRING LZ4_EXPAND_AND_QUOTE(LZ4_LIB_VERSION) LZ4LIB_API int LZ4_versionNumber (void); /**< library version number; useful to check dll version */ -LZ4LIB_API const char* LZ4_versionString (void); /**< library version string; unseful to check dll version */ +LZ4LIB_API const char* LZ4_versionString (void); /**< library version string; useful to check dll version */ /*-************************************ @@ -112,14 +112,15 @@ LZ4LIB_API const char* LZ4_versionString (void); /**< library version string; /*! * LZ4_MEMORY_USAGE : * Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.) - * Increasing memory usage improves compression ratio - * Reduced memory usage may improve speed, thanks to cache effect + * Increasing memory usage improves compression ratio. + * Reduced memory usage may improve speed, thanks to better cache locality. * Default value is 14, for 16KB, which nicely fits into Intel x86 L1 cache */ #ifndef LZ4_MEMORY_USAGE # define LZ4_MEMORY_USAGE 14 #endif + /*-************************************ * Simple Functions **************************************/ @@ -130,21 +131,22 @@ LZ4LIB_API const char* LZ4_versionString (void); /**< library version string; It also runs faster, so it's a recommended setting. If the function cannot compress 'src' into a more limited 'dst' budget, compression stops *immediately*, and the function result is zero. - Note : as a consequence, 'dst' content is not valid. - Note 2 : This function is protected against buffer overflow scenarios (never writes outside 'dst' buffer, nor read outside 'source' buffer). + In which case, 'dst' content is undefined (invalid). srcSize : max supported value is LZ4_MAX_INPUT_SIZE. dstCapacity : size of buffer 'dst' (which must be already allocated) - return : the number of bytes written into buffer 'dst' (necessarily <= dstCapacity) - or 0 if compression fails */ + @return : the number of bytes written into buffer 'dst' (necessarily <= dstCapacity) + or 0 if compression fails + Note : This function is protected against buffer overflow scenarios (never writes outside 'dst' buffer, nor read outside 'source' buffer). +*/ LZ4LIB_API int LZ4_compress_default(const char* src, char* dst, int srcSize, int dstCapacity); /*! LZ4_decompress_safe() : compressedSize : is the exact complete size of the compressed block. dstCapacity : is the size of destination buffer, which must be already allocated. - return : the number of bytes decompressed into destination buffer (necessarily <= dstCapacity) + @return : the number of bytes decompressed into destination buffer (necessarily <= dstCapacity) If destination buffer is not large enough, decoding will stop and output an error code (negative value). If the source stream is detected malformed, the function will stop decoding and return a negative result. - This function is protected against malicious data packets. + Note : This function is protected against malicious data packets (never writes outside 'dst' buffer, nor read outside 'source' buffer). */ LZ4LIB_API int LZ4_decompress_safe (const char* src, char* dst, int compressedSize, int dstCapacity); @@ -155,8 +157,7 @@ LZ4LIB_API int LZ4_decompress_safe (const char* src, char* dst, int compressedSi #define LZ4_MAX_INPUT_SIZE 0x7E000000 /* 2 113 929 216 bytes */ #define LZ4_COMPRESSBOUND(isize) ((unsigned)(isize) > (unsigned)LZ4_MAX_INPUT_SIZE ? 0 : (isize) + ((isize)/255) + 16) -/*! -LZ4_compressBound() : +/*! LZ4_compressBound() : Provides the maximum size that LZ4 compression may output in a "worst case" scenario (input data not compressible) This function is primarily useful for memory allocation purposes (destination buffer size). Macro LZ4_COMPRESSBOUND() is also provided for compilation-time evaluation (stack memory allocation for example). @@ -167,8 +168,7 @@ LZ4_compressBound() : */ LZ4LIB_API int LZ4_compressBound(int inputSize); -/*! -LZ4_compress_fast() : +/*! LZ4_compress_fast() : Same as LZ4_compress_default(), but allows selection of "acceleration" factor. The larger the acceleration value, the faster the algorithm, but also the lesser the compression. It's a trade-off. It can be fine tuned, with each successive value providing roughly +~3% to speed. @@ -178,13 +178,12 @@ LZ4_compress_fast() : LZ4LIB_API int LZ4_compress_fast (const char* src, char* dst, int srcSize, int dstCapacity, int acceleration); -/*! -LZ4_compress_fast_extState() : - Same compression function, just using an externally allocated memory space to store compression state. - Use LZ4_sizeofState() to know how much memory must be allocated, - and allocate it on 8-bytes boundaries (using malloc() typically). - Then, provide this buffer as 'void* state' to compression function. -*/ +/*! LZ4_compress_fast_extState() : + * Same as LZ4_compress_fast(), using an externally allocated memory space for its state. + * Use LZ4_sizeofState() to know how much memory must be allocated, + * and allocate it on 8-bytes boundaries (using `malloc()` typically). + * Then, provide this buffer as `void* state` to compression function. + */ LZ4LIB_API int LZ4_sizeofState(void); LZ4LIB_API int LZ4_compress_fast_extState (void* state, const char* src, char* dst, int srcSize, int dstCapacity, int acceleration); @@ -210,7 +209,7 @@ LZ4LIB_API int LZ4_compress_destSize (const char* src, char* dst, int* srcSizePt * and now `LZ4_decompress_safe()` can be as fast and sometimes faster than `LZ4_decompress_fast()`. * Moreover, LZ4_decompress_fast() is not protected vs malformed input, as it doesn't perform full validation of compressed data. * As a consequence, this function is no longer recommended, and may be deprecated in future versions. - * It's only remaining specificity is that it can decompress data without knowing its compressed size. + * It's last remaining specificity is that it can decompress data without knowing its compressed size. * * originalSize : is the uncompressed size to regenerate. * `dst` must be already allocated, its size must be >= 'originalSize' bytes. @@ -257,10 +256,6 @@ LZ4LIB_API int LZ4_decompress_safe_partial (const char* src, char* dst, int srcS ***********************************************/ typedef union LZ4_stream_u LZ4_stream_t; /* incomplete type (defined later) */ -/*! LZ4_createStream() and LZ4_freeStream() : - * LZ4_createStream() will allocate and initialize an `LZ4_stream_t` structure. - * LZ4_freeStream() releases its memory. - */ LZ4LIB_API LZ4_stream_t* LZ4_createStream(void); LZ4LIB_API int LZ4_freeStream (LZ4_stream_t* streamPtr); @@ -280,7 +275,7 @@ LZ4LIB_API int LZ4_loadDict (LZ4_stream_t* streamPtr, const char* dictionary, in /*! LZ4_compress_fast_continue() : * Compress 'src' content using data from previously compressed blocks, for better compression ratio. - * 'dst' buffer must be already allocated. + * 'dst' buffer must be already allocated. * If dstCapacity >= LZ4_compressBound(srcSize), compression is guaranteed to succeed, and runs faster. * * @return : size of compressed block @@ -288,10 +283,10 @@ LZ4LIB_API int LZ4_loadDict (LZ4_stream_t* streamPtr, const char* dictionary, in * * Note 1 : Each invocation to LZ4_compress_fast_continue() generates a new block. * Each block has precise boundaries. + * Each block must be decompressed separately, calling LZ4_decompress_*() with relevant metadata. * It's not possible to append blocks together and expect a single invocation of LZ4_decompress_*() to decompress them together. - * Each block must be decompressed separately, calling LZ4_decompress_*() with associated metadata. * - * Note 2 : The previous 64KB of source data is __assumed__ to remain present, unmodified, at same address in memory! + * Note 2 : The previous 64KB of source data is __assumed__ to remain present, unmodified, at same address in memory ! * * Note 3 : When input is structured as a double-buffer, each buffer can have any size, including < 64 KB. * Make sure that buffers are separated, by at least one byte. @@ -299,7 +294,7 @@ LZ4LIB_API int LZ4_loadDict (LZ4_stream_t* streamPtr, const char* dictionary, in * * Note 4 : If input buffer is a ring-buffer, it can have any size, including < 64 KB. * - * Note 5 : After an error, the stream status is invalid, it can only be reset or freed. + * Note 5 : After an error, the stream status is undefined (invalid), it can only be reset or freed. */ LZ4LIB_API int LZ4_compress_fast_continue (LZ4_stream_t* streamPtr, const char* src, char* dst, int srcSize, int dstCapacity, int acceleration); @@ -335,7 +330,7 @@ LZ4LIB_API int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_str */ LZ4LIB_API int LZ4_setStreamDecode (LZ4_streamDecode_t* LZ4_streamDecode, const char* dictionary, int dictSize); -/*! LZ4_decoderRingBufferSize() : v1.8.2 +/*! LZ4_decoderRingBufferSize() : v1.8.2+ * Note : in a ring buffer scenario (optional), * blocks are presumed decompressed next to each other * up to the moment there is not enough remaining space for next block (remainingSize < maxBlockSize), @@ -347,7 +342,7 @@ LZ4LIB_API int LZ4_setStreamDecode (LZ4_streamDecode_t* LZ4_streamDecode, const * or 0 if there is an error (invalid maxBlockSize). */ LZ4LIB_API int LZ4_decoderRingBufferSize(int maxBlockSize); -#define LZ4_DECODER_RING_BUFFER_SIZE(mbs) (65536 + 14 + (mbs)) /* for static allocation; mbs presumed valid */ +#define LZ4_DECODER_RING_BUFFER_SIZE(maxBlockSize) (65536 + 14 + (maxBlockSize)) /* for static allocation; maxBlockSize presumed valid */ /*! LZ4_decompress_*_continue() : * These decoding functions allow decompression of consecutive blocks in "streaming" mode. @@ -382,7 +377,9 @@ LZ4LIB_API int LZ4_decompress_fast_continue (LZ4_streamDecode_t* LZ4_streamDecod * These decoding functions work the same as * a combination of LZ4_setStreamDecode() followed by LZ4_decompress_*_continue() * They are stand-alone, and don't need an LZ4_streamDecode_t structure. - * Dictionary is presumed stable : it must remain accessible and unmodified during next decompression. + * Dictionary is presumed stable : it must remain accessible and unmodified during decompression. + * Performance tip : Decompression speed can be substantially increased + * when dst == dictStart + dictSize. */ LZ4LIB_API int LZ4_decompress_safe_usingDict (const char* src, char* dst, int srcSize, int dstCapcity, const char* dictStart, int dictSize); LZ4LIB_API int LZ4_decompress_fast_usingDict (const char* src, char* dst, int originalSize, const char* dictStart, int dictSize); @@ -395,29 +392,41 @@ LZ4LIB_API int LZ4_decompress_fast_usingDict (const char* src, char* dst, int or /*-************************************ * Unstable declarations ************************************** - * Declarations in this section should be considered unstable. - * Use at your own peril, etc., etc. - * They may be removed in the future. - * Their signatures may change. + * Declarations in this section must be considered unstable. + * Their signatures may change, or may be removed in the future. + * They are therefore only safe to depend on + * when the caller is statically linked against the library. + * To access their declarations, define LZ4_STATIC_LINKING_ONLY. **************************************/ #ifdef LZ4_STATIC_LINKING_ONLY +/* By default, symbols in this section aren't published into shared/dynamic libraries. + * You can override this behavior and force them to be published + * by defining LZ4_PUBLISH_STATIC_FUNCTIONS. + * Use at your own risk. + */ + +#ifdef LZ4_PUBLISH_STATIC_FUNCTIONS +#define LZ4LIB_STATIC_API LZ4LIB_API +#else +#define LZ4LIB_STATIC_API +#endif + /*! LZ4_resetStream_fast() : * Use this, like LZ4_resetStream(), to prepare a context for a new chain of * calls to a streaming API (e.g., LZ4_compress_fast_continue()). * * Note: - * Using this in advance of a non- streaming-compression function is redundant, - * and potentially bad for performance, since they all perform their own custom - * reset internally. + * Using this in advance of a non-streaming-compression function is redundant, + * since they all perform their own custom reset internally. * * Differences from LZ4_resetStream(): * When an LZ4_stream_t is known to be in a internally coherent state, - * it can often be prepared for a new compression with almost no work, only - * sometimes falling back to the full, expensive reset that is always required - * when the stream is in an indeterminate state (i.e., the reset performed by - * LZ4_resetStream()). + * it can often be prepared for a new compression with almost no work, + * only sometimes falling back to the full, expensive reset + * that is always required when the stream is in an indeterminate state + * (i.e., the reset performed by LZ4_resetStream()). * * LZ4_streams are guaranteed to be in a valid state when: * - returned from LZ4_createStream() @@ -434,24 +443,23 @@ LZ4LIB_API int LZ4_decompress_fast_usingDict (const char* src, char* dst, int or * any fastReset or streaming function. It must first be cleansed by the full * LZ4_resetStream(). */ -LZ4LIB_API void LZ4_resetStream_fast (LZ4_stream_t* streamPtr); +LZ4LIB_STATIC_API void LZ4_resetStream_fast (LZ4_stream_t* streamPtr); /*! LZ4_compress_fast_extState_fastReset() : * A variant of LZ4_compress_fast_extState(). * - * Using this variant avoids an expensive initialization step. It is only safe - * to call if the state buffer is known to be correctly initialized already - * (see above comment on LZ4_resetStream_fast() for a definition of "correctly - * initialized"). From a high level, the difference is that this function - * initializes the provided state with a call to something like - * LZ4_resetStream_fast() while LZ4_compress_fast_extState() starts with a - * call to LZ4_resetStream(). + * Using this variant avoids an expensive initialization step. + * It is only safe to call if the state buffer is known to be correctly initialized already + * (see above comment on LZ4_resetStream_fast() for a definition of "correctly initialized"). + * From a high level, the difference is that + * this function initializes the provided state with a call to something like LZ4_resetStream_fast() + * while LZ4_compress_fast_extState() starts with a call to LZ4_resetStream(). */ -LZ4LIB_API int LZ4_compress_fast_extState_fastReset (void* state, const char* src, char* dst, int srcSize, int dstCapacity, int acceleration); +LZ4LIB_STATIC_API int LZ4_compress_fast_extState_fastReset (void* state, const char* src, char* dst, int srcSize, int dstCapacity, int acceleration); /*! LZ4_attach_dictionary() : - * This is an experimental API that allows for the efficient use of a - * static dictionary many times. + * This is an experimental API that allows + * efficient use of a static dictionary many times. * * Rather than re-loading the dictionary buffer into a working context before * each compression, or copying a pre-loaded dictionary's LZ4_stream_t into a @@ -462,8 +470,8 @@ LZ4LIB_API int LZ4_compress_fast_extState_fastReset (void* state, const char* sr * Currently, only streams which have been prepared by LZ4_loadDict() should * be expected to work. * - * Alternatively, the provided dictionary stream pointer may be NULL, in which - * case any existing dictionary stream is unset. + * Alternatively, the provided dictionaryStream may be NULL, + * in which case any existing dictionary stream is unset. * * If a dictionary is provided, it replaces any pre-existing stream history. * The dictionary contents are the only history that can be referenced and @@ -475,16 +483,17 @@ LZ4LIB_API int LZ4_compress_fast_extState_fastReset (void* state, const char* sr * stream (and source buffer) must remain in-place / accessible / unchanged * through the completion of the first compression call on the stream. */ -LZ4LIB_API void LZ4_attach_dictionary(LZ4_stream_t *working_stream, const LZ4_stream_t *dictionary_stream); +LZ4LIB_STATIC_API void LZ4_attach_dictionary(LZ4_stream_t* workingStream, const LZ4_stream_t* dictionaryStream); #endif + /*-************************************ * Private definitions ************************************** - * Do not use these definitions. - * They are exposed to allow static allocation of `LZ4_stream_t` and `LZ4_streamDecode_t`. - * Using these definitions will expose code to API and/or ABI break in future versions of the library. + * Do not use these definitions directly. + * They are only exposed to allow static allocation of `LZ4_stream_t` and `LZ4_streamDecode_t`. + * Accessing members will expose code to API and/or ABI break in future versions of the library. **************************************/ #define LZ4_HASHLOG (LZ4_MEMORY_USAGE-2) #define LZ4_HASHTABLESIZE (1 << LZ4_MEMORY_USAGE) @@ -533,13 +542,12 @@ typedef struct { #endif -/*! - * LZ4_stream_t : - * information structure to track an LZ4 stream. - * init this structure before first use. - * note : only use in association with static linking ! - * this definition is not API/ABI safe, - * it may change in a future version ! +/*! LZ4_stream_t : + * information structure to track an LZ4 stream. + * init this structure with LZ4_resetStream() before first use. + * note : only use in association with static linking ! + * this definition is not API/ABI safe, + * it may change in a future version ! */ #define LZ4_STREAMSIZE_U64 ((1 << (LZ4_MEMORY_USAGE-3)) + 4) #define LZ4_STREAMSIZE (LZ4_STREAMSIZE_U64 * sizeof(unsigned long long)) @@ -549,13 +557,12 @@ union LZ4_stream_u { } ; /* previously typedef'd to LZ4_stream_t */ -/*! - * LZ4_streamDecode_t : - * information structure to track an LZ4 stream during decompression. - * init this structure using LZ4_setStreamDecode (or memset()) before first use - * note : only use in association with static linking ! - * this definition is not API/ABI safe, - * and may change in a future version ! +/*! LZ4_streamDecode_t : + * information structure to track an LZ4 stream during decompression. + * init this structure using LZ4_setStreamDecode() before first use. + * note : only use in association with static linking ! + * this definition is not API/ABI safe, + * and may change in a future version ! */ #define LZ4_STREAMDECODESIZE_U64 4 #define LZ4_STREAMDECODESIZE (LZ4_STREAMDECODESIZE_U64 * sizeof(unsigned long long)) -- cgit v1.2.3 From e8b08f9fbd8556ff9ca0cddfe9b505dbfadcc0e6 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 13 Sep 2018 16:14:00 -0700 Subject: updated build doc --- lib/README.md | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/README.md b/lib/README.md index 7082fe3..8b3b424 100644 --- a/lib/README.md +++ b/lib/README.md @@ -16,10 +16,10 @@ They generate and decode data using [LZ4 block format]. For more compression ratio at the cost of compression speed, the High Compression variant called **lz4hc** is available. Add files **`lz4hc.c`** and **`lz4hc.h`**. -The variant still depends on regular `lib/lz4.*` source files. +This variant also depends on regular `lib/lz4.*` source files. -#### Frame variant, for interoperability +#### Frame support, for interoperability In order to produce compressed data compatible with `lz4` command line utility, it's necessary to encode lz4-compressed blocks using the [official interoperable frame format]. @@ -32,9 +32,14 @@ So it's necessary to include all `*.c` and `*.h` files present in `/lib`. #### Advanced / Experimental API -A complex API defined in `lz4frame_static.h` contains definitions -which are not guaranteed to remain stable in future versions. -As a consequence, it must be used with static linking ***only***. +Definitions which are not guaranteed to remain stable in future versions, +are protected behind macros, such as `LZ4_STATIC_LINKING_ONLY`. +As the name implies, these definitions can only be invoked +in the context of static linking ***only***. +Otherwise, dependent application may fail on API or ABI break in the future. +The associated symbols are also not present in dynamic library by default. +Should they be nonetheless needed, it's possible to force their publication +by using build macro `LZ4_PUBLISH_STATIC_FUNCTIONS`. #### Windows : using MinGW+MSYS to create DLL @@ -48,7 +53,7 @@ The dynamic library has to be added to linking options. It means that if a project that uses LZ4 consists of a single `test-dll.c` file it should be linked with `dll\liblz4.dll`. For example: ``` - gcc $(CFLAGS) -Iinclude/ test-dll.c -o test-dll dll\liblz4.dll + $(CC) $(CFLAGS) -Iinclude/ test-dll.c -o test-dll dll\liblz4.dll ``` The compiled executable will require LZ4 DLL which is available at `dll\liblz4.dll`. -- cgit v1.2.3 From 6103b4c9b4ebc29e3de0fcb08b3ccb64400abf5b Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 14 Sep 2018 14:58:16 -0700 Subject: use byU32 mode for any pointer > 32-bit including 128-bit, like IBM AS-400 --- lib/lz4.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 4046102..66a0898 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1094,23 +1094,25 @@ int LZ4_compress_fast(const char* source, char* dest, int inputSize, int maxOutp } -int LZ4_compress_default(const char* source, char* dest, int inputSize, int maxOutputSize) +int LZ4_compress_default(const char* src, char* dst, int srcSize, int maxOutputSize) { - return LZ4_compress_fast(source, dest, inputSize, maxOutputSize, 1); + return LZ4_compress_fast(src, dst, srcSize, maxOutputSize, 1); } /* hidden debug function */ /* strangely enough, gcc generates faster code when this function is uncommented, even if unused */ -int LZ4_compress_fast_force(const char* source, char* dest, int inputSize, int maxOutputSize, int acceleration) +int LZ4_compress_fast_force(const char* src, char* dst, int srcSize, int dstCapacity, int acceleration) { LZ4_stream_t ctx; LZ4_resetStream(&ctx); - if (inputSize < LZ4_64Klimit) - return LZ4_compress_generic(&ctx.internal_donotuse, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, byU16, noDict, noDictIssue, acceleration); - else - return LZ4_compress_generic(&ctx.internal_donotuse, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, sizeof(void*)==8 ? byU32 : byPtr, noDict, noDictIssue, acceleration); + if (srcSize < LZ4_64Klimit) { + return LZ4_compress_generic(&ctx.internal_donotuse, src, dst, srcSize, NULL, dstCapacity, limitedOutput, byU16, noDict, noDictIssue, acceleration); + } else { + tableType_t const addrMode = (sizeof(void*) > 4) ? byU32 : byPtr; + return LZ4_compress_generic(&ctx.internal_donotuse, src, dst, srcSize, NULL, dstCapacity, limitedOutput, addrMode, noDict, noDictIssue, acceleration); + } } @@ -1127,8 +1129,8 @@ static int LZ4_compress_destSize_extState (LZ4_stream_t* state, const char* src, if (*srcSizePtr < LZ4_64Klimit) { return LZ4_compress_generic(&state->internal_donotuse, src, dst, *srcSizePtr, srcSizePtr, targetDstSize, fillOutput, byU16, noDict, noDictIssue, 1); } else { - tableType_t const tableType = ((sizeof(void*)==4) && ((uptrval)src > MAX_DISTANCE)) ? byPtr : byU32; - return LZ4_compress_generic(&state->internal_donotuse, src, dst, *srcSizePtr, srcSizePtr, targetDstSize, fillOutput, tableType, noDict, noDictIssue, 1); + tableType_t const addrMode = ((sizeof(void*)==4) && ((uptrval)src > MAX_DISTANCE)) ? byPtr : byU32; + return LZ4_compress_generic(&state->internal_donotuse, src, dst, *srcSizePtr, srcSizePtr, targetDstSize, fillOutput, addrMode, noDict, noDictIssue, 1); } } } @@ -1159,7 +1161,7 @@ int LZ4_compress_destSize(const char* src, char* dst, int* srcSizePtr, int targe LZ4_stream_t* LZ4_createStream(void) { - LZ4_stream_t* lz4s = (LZ4_stream_t*)ALLOC(sizeof(LZ4_stream_t)); + LZ4_stream_t* const lz4s = (LZ4_stream_t*)ALLOC(sizeof(LZ4_stream_t)); LZ4_STATIC_ASSERT(LZ4_STREAMSIZE >= sizeof(LZ4_stream_t_internal)); /* A compilation error here means LZ4_STREAMSIZE is not large enough */ DEBUGLOG(4, "LZ4_createStream %p", lz4s); if (lz4s == NULL) return NULL; -- cgit v1.2.3 From cb2fb479efaf4bc74ea2c979646e711c94940b95 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 17 Sep 2018 17:05:17 -0700 Subject: increase lz4hc context size as constant for 128-bit systems --- lib/lz4hc.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/lz4hc.h b/lib/lz4hc.h index 970fa39..ab796ae 100644 --- a/lib/lz4hc.h +++ b/lib/lz4hc.h @@ -123,12 +123,12 @@ LZ4LIB_API int LZ4_saveDictHC (LZ4_streamHC_t* streamHCPtr, char* safeBuffer, in */ -/*-************************************************************** +/*-****************************************************************** * PRIVATE DEFINITIONS : - * Do not use these definitions. - * They are exposed to allow static allocation of `LZ4_streamHC_t`. - * Using these definitions makes the code vulnerable to potential API break when upgrading LZ4 - ****************************************************************/ + * Do not use these definitions directly. + * They are merely exposed to allow static allocation of `LZ4_streamHC_t`. + * Using these definitions makes the code vulnerable to potential API/ABI break when upgrading LZ4. + ********************************************************************/ #define LZ4HC_DICTIONARY_LOGSIZE 16 #define LZ4HC_MAXD (1< Date: Mon, 17 Sep 2018 17:22:07 -0700 Subject: increase size of LZ4 contexts for 128-bit systems --- lib/lz4.c | 3 ++- lib/lz4.h | 30 +++++++++++++++--------------- lib/lz4hc.h | 30 ++++++++++++++++++++---------- 3 files changed, 37 insertions(+), 26 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 66a0898..89be891 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1747,12 +1747,13 @@ int LZ4_decompress_fast_doubleDict(const char* source, char* dest, int originalS LZ4_streamDecode_t* LZ4_createStreamDecode(void) { LZ4_streamDecode_t* lz4s = (LZ4_streamDecode_t*) ALLOC_AND_ZERO(sizeof(LZ4_streamDecode_t)); + LZ4_STATIC_ASSERT(LZ4_STREAMDECODESIZE >= sizeof(LZ4_streamDecode_t_internal)); /* A compilation error here means LZ4_STREAMDECODESIZE is not large enough */ return lz4s; } int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream) { - if (!LZ4_stream) return 0; /* support free on NULL */ + if (LZ4_stream == NULL) return 0; /* support free on NULL */ FREEMEM(LZ4_stream); return 0; } diff --git a/lib/lz4.h b/lib/lz4.h index 6d67bfc..639797a 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -488,13 +488,13 @@ LZ4LIB_STATIC_API void LZ4_attach_dictionary(LZ4_stream_t* workingStream, const #endif -/*-************************************ - * Private definitions - ************************************** +/*-************************************************************ + * PRIVATE DEFINITIONS + ************************************************************** * Do not use these definitions directly. * They are only exposed to allow static allocation of `LZ4_stream_t` and `LZ4_streamDecode_t`. * Accessing members will expose code to API and/or ABI break in future versions of the library. - **************************************/ + **************************************************************/ #define LZ4_HASHLOG (LZ4_MEMORY_USAGE-2) #define LZ4_HASHTABLESIZE (1 << LZ4_MEMORY_USAGE) #define LZ4_HASH_SIZE_U32 (1 << LZ4_HASHLOG) /* required as macro for static allocation */ @@ -549,7 +549,7 @@ typedef struct { * this definition is not API/ABI safe, * it may change in a future version ! */ -#define LZ4_STREAMSIZE_U64 ((1 << (LZ4_MEMORY_USAGE-3)) + 4) +#define LZ4_STREAMSIZE_U64 ((1 << (LZ4_MEMORY_USAGE-3)) + 4 + ((sizeof(void*)==16) ? 4 : 0) /*AS-400*/ ) #define LZ4_STREAMSIZE (LZ4_STREAMSIZE_U64 * sizeof(unsigned long long)) union LZ4_stream_u { unsigned long long table[LZ4_STREAMSIZE_U64]; @@ -564,7 +564,7 @@ union LZ4_stream_u { * this definition is not API/ABI safe, * and may change in a future version ! */ -#define LZ4_STREAMDECODESIZE_U64 4 +#define LZ4_STREAMDECODESIZE_U64 (4 + ((sizeof(void*)==16) ? 2 : 0) /*AS-400*/ ) #define LZ4_STREAMDECODESIZE (LZ4_STREAMDECODESIZE_U64 * sizeof(unsigned long long)) union LZ4_streamDecode_u { unsigned long long table[LZ4_STREAMDECODESIZE_U64]; @@ -577,11 +577,11 @@ union LZ4_streamDecode_u { **************************************/ /*! Deprecation warnings - Should deprecation warnings be a problem, - it is generally possible to disable them, - typically with -Wno-deprecated-declarations for gcc - or _CRT_SECURE_NO_WARNINGS in Visual. - Otherwise, it's also possible to define LZ4_DISABLE_DEPRECATE_WARNINGS */ + * Should deprecation warnings be a problem, + * it is generally possible to disable them, + * typically with -Wno-deprecated-declarations for gcc + * or _CRT_SECURE_NO_WARNINGS in Visual. + * Otherwise, it's also possible to define LZ4_DISABLE_DEPRECATE_WARNINGS */ #ifdef LZ4_DISABLE_DEPRECATE_WARNINGS # define LZ4_DEPRECATED(message) /* disable deprecation warnings */ #else @@ -601,8 +601,8 @@ union LZ4_streamDecode_u { #endif /* LZ4_DISABLE_DEPRECATE_WARNINGS */ /* Obsolete compression functions */ -LZ4_DEPRECATED("use LZ4_compress_default() instead") LZ4LIB_API int LZ4_compress (const char* source, char* dest, int sourceSize); -LZ4_DEPRECATED("use LZ4_compress_default() instead") LZ4LIB_API int LZ4_compress_limitedOutput (const char* source, char* dest, int sourceSize, int maxOutputSize); +LZ4_DEPRECATED("use LZ4_compress_default() instead") LZ4LIB_API int LZ4_compress (const char* source, char* dest, int sourceSize); +LZ4_DEPRECATED("use LZ4_compress_default() instead") LZ4LIB_API int LZ4_compress_limitedOutput (const char* source, char* dest, int sourceSize, int maxOutputSize); LZ4_DEPRECATED("use LZ4_compress_fast_extState() instead") LZ4LIB_API int LZ4_compress_withState (void* state, const char* source, char* dest, int inputSize); LZ4_DEPRECATED("use LZ4_compress_fast_extState() instead") LZ4LIB_API int LZ4_compress_limitedOutput_withState (void* state, const char* source, char* dest, int inputSize, int maxOutputSize); LZ4_DEPRECATED("use LZ4_compress_fast_continue() instead") LZ4LIB_API int LZ4_compress_continue (LZ4_stream_t* LZ4_streamPtr, const char* source, char* dest, int inputSize); @@ -623,8 +623,8 @@ LZ4_DEPRECATED("use LZ4_decompress_safe() instead") LZ4LIB_API int LZ4_uncompres */ LZ4_DEPRECATED("Use LZ4_createStream() instead") LZ4LIB_API void* LZ4_create (char* inputBuffer); LZ4_DEPRECATED("Use LZ4_createStream() instead") LZ4LIB_API int LZ4_sizeofStreamState(void); -LZ4_DEPRECATED("Use LZ4_resetStream() instead") LZ4LIB_API int LZ4_resetStreamState(void* state, char* inputBuffer); -LZ4_DEPRECATED("Use LZ4_saveDict() instead") LZ4LIB_API char* LZ4_slideInputBuffer (void* state); +LZ4_DEPRECATED("Use LZ4_resetStream() instead") LZ4LIB_API int LZ4_resetStreamState(void* state, char* inputBuffer); +LZ4_DEPRECATED("Use LZ4_saveDict() instead") LZ4LIB_API char* LZ4_slideInputBuffer (void* state); /* Obsolete streaming decoding functions */ LZ4_DEPRECATED("use LZ4_decompress_safe_usingDict() instead") LZ4LIB_API int LZ4_decompress_safe_withPrefix64k (const char* src, char* dst, int compressedSize, int maxDstSize); diff --git a/lib/lz4hc.h b/lib/lz4hc.h index ab796ae..4c7ba5a 100644 --- a/lib/lz4hc.h +++ b/lib/lz4hc.h @@ -123,12 +123,20 @@ LZ4LIB_API int LZ4_saveDictHC (LZ4_streamHC_t* streamHCPtr, char* safeBuffer, in */ + + +/*^********************************************** + * !!!!!! STATIC LINKING ONLY !!!!!! + ***********************************************/ + /*-****************************************************************** * PRIVATE DEFINITIONS : * Do not use these definitions directly. * They are merely exposed to allow static allocation of `LZ4_streamHC_t`. - * Using these definitions makes the code vulnerable to potential API/ABI break when upgrading LZ4. + * Declare an `LZ4_streamHC_t` directly, rather than any type below. + * Even then, only do so in the context of static linking, as definitions may change between versions. ********************************************************************/ + #define LZ4HC_DICTIONARY_LOGSIZE 16 #define LZ4HC_MAXD (1< Date: Tue, 18 Sep 2018 12:14:26 -0700 Subject: updated xxhash to latest version --- lib/xxhash.c | 614 ++++++++++++++++++++++++++++++++++++----------------------- lib/xxhash.h | 221 ++++++++++++--------- 2 files changed, 503 insertions(+), 332 deletions(-) diff --git a/lib/xxhash.c b/lib/xxhash.c index 3fc97fd..ff28749 100644 --- a/lib/xxhash.c +++ b/lib/xxhash.c @@ -50,20 +50,26 @@ * Prefer these methods in priority order (0 > 1 > 2) */ #ifndef XXH_FORCE_MEMORY_ACCESS /* can be defined externally, on command line for example */ -# if defined(__GNUC__) && ( defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_6T2__) ) +# if defined(__GNUC__) && ( defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) \ + || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6Z__) \ + || defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_6T2__) ) # define XXH_FORCE_MEMORY_ACCESS 2 # elif (defined(__INTEL_COMPILER) && !defined(_WIN32)) || \ - (defined(__GNUC__) && ( defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7S__) )) + (defined(__GNUC__) && ( defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) \ + || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__) \ + || defined(__ARM_ARCH_7S__) )) # define XXH_FORCE_MEMORY_ACCESS 1 # endif #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. - * By default, this option is disabled. To enable it, uncomment below define : + * If input pointer is NULL, xxHash default behavior is to dereference it, triggering a segfault. + * When this macro is enabled, xxHash actively checks input for null pointer. + * It it is, result for null input pointers is the same as a null-length input. */ -/* #define XXH_ACCEPT_NULL_INPUT_POINTER 1 */ +#ifndef XXH_ACCEPT_NULL_INPUT_POINTER /* can be defined externally */ +# define XXH_ACCEPT_NULL_INPUT_POINTER 0 +#endif /*!XXH_FORCE_NATIVE_FORMAT : * By default, xxHash library provides endian-independent Hash values, based on little-endian convention. @@ -80,8 +86,9 @@ /*!XXH_FORCE_ALIGN_CHECK : * This is a minor performance trick, only useful with lots of very small keys. * It means : check for aligned/unaligned input. - * The check costs one initial branch per hash; set to 0 when the input data - * is guaranteed to be aligned. + * The check costs one initial branch per hash; + * set it to 0 when the input is guaranteed to be aligned, + * or when alignment doesn't matter for performance. */ #ifndef XXH_FORCE_ALIGN_CHECK /* can be defined externally */ # if defined(__i386) || defined(_M_IX86) || defined(__x86_64__) || defined(_M_X64) @@ -104,6 +111,8 @@ static void XXH_free (void* p) { free(p); } #include static void* XXH_memcpy(void* dest, const void* src, size_t size) { return memcpy(dest,src,size); } +#include /* assert */ + #define XXH_STATIC_LINKING_ONLY #include "xxhash.h" @@ -113,40 +122,35 @@ static void* XXH_memcpy(void* dest, const void* src, size_t size) { return memcp ***************************************/ #ifdef _MSC_VER /* Visual Studio */ # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ -#endif - -#ifndef XXH_FORCE_INLINE -# ifdef _MSC_VER /* Visual Studio */ -# define XXH_FORCE_INLINE static __forceinline -# else -# if defined (__cplusplus) || defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */ -# ifdef __GNUC__ -# define XXH_FORCE_INLINE static inline __attribute__((always_inline)) -# else -# define XXH_FORCE_INLINE static inline -# endif +# define FORCE_INLINE static __forceinline +#else +# if defined (__cplusplus) || defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */ +# ifdef __GNUC__ +# define FORCE_INLINE static inline __attribute__((always_inline)) # else -# define XXH_FORCE_INLINE static -# endif /* __STDC_VERSION__ */ -# endif /* _MSC_VER */ -#endif /* XXH_FORCE_INLINE */ +# define FORCE_INLINE static inline +# endif +# else +# define FORCE_INLINE static +# endif /* __STDC_VERSION__ */ +#endif /* ************************************* * Basic Types ***************************************/ #ifndef MEM_MODULE -# if !defined (__VMS) && (defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) ) +# if !defined (__VMS) \ + && (defined (__cplusplus) \ + || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) ) # include typedef uint8_t BYTE; typedef uint16_t U16; typedef uint32_t U32; - typedef int32_t S32; # else typedef unsigned char BYTE; typedef unsigned short U16; typedef unsigned int U32; - typedef signed int S32; # endif #endif @@ -213,8 +217,12 @@ typedef enum { XXH_bigEndian=0, XXH_littleEndian=1 } XXH_endianess; /* XXH_CPU_LITTLE_ENDIAN can be defined externally, for example on the compiler command line */ #ifndef XXH_CPU_LITTLE_ENDIAN - static const int g_one = 1; -# define XXH_CPU_LITTLE_ENDIAN (*(const char*)(&g_one)) +static int XXH_isLittleEndian(void) +{ + const union { U32 u; BYTE c[4]; } one = { 1 }; /* don't use static : performance detrimental */ + return one.c[0]; +} +# define XXH_CPU_LITTLE_ENDIAN XXH_isLittleEndian() #endif @@ -223,7 +231,7 @@ typedef enum { XXH_bigEndian=0, XXH_littleEndian=1 } XXH_endianess; *****************************/ typedef enum { XXH_aligned, XXH_unaligned } XXH_alignment; -XXH_FORCE_INLINE U32 XXH_readLE32_align(const void* ptr, XXH_endianess endian, XXH_alignment align) +FORCE_INLINE U32 XXH_readLE32_align(const void* ptr, XXH_endianess endian, XXH_alignment align) { if (align==XXH_unaligned) return endian==XXH_littleEndian ? XXH_read32(ptr) : XXH_swap32(XXH_read32(ptr)); @@ -231,7 +239,7 @@ XXH_FORCE_INLINE U32 XXH_readLE32_align(const void* ptr, XXH_endianess endian, X return endian==XXH_littleEndian ? *(const U32*)ptr : XXH_swap32(*(const U32*)ptr); } -XXH_FORCE_INLINE U32 XXH_readLE32(const void* ptr, XXH_endianess endian) +FORCE_INLINE U32 XXH_readLE32(const void* ptr, XXH_endianess endian) { return XXH_readLE32_align(ptr, endian, XXH_unaligned); } @@ -245,12 +253,12 @@ static U32 XXH_readBE32(const void* ptr) /* ************************************* * Macros ***************************************/ -#define XXH_STATIC_ASSERT(c) { enum { XXH_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */ +#define XXH_STATIC_ASSERT(c) { enum { XXH_sa = 1/(int)(!!(c)) }; } /* use after variable declarations */ XXH_PUBLIC_API unsigned XXH_versionNumber (void) { return XXH_VERSION_NUMBER; } /* ******************************************************************* -* 32-bits hash functions +* 32-bit hash functions *********************************************************************/ static const U32 PRIME32_1 = 2654435761U; static const U32 PRIME32_2 = 2246822519U; @@ -266,14 +274,89 @@ static U32 XXH32_round(U32 seed, U32 input) return seed; } -XXH_FORCE_INLINE U32 XXH32_endian_align(const void* input, size_t len, U32 seed, XXH_endianess endian, XXH_alignment align) +/* mix all bits */ +static U32 XXH32_avalanche(U32 h32) +{ + h32 ^= h32 >> 15; + h32 *= PRIME32_2; + h32 ^= h32 >> 13; + h32 *= PRIME32_3; + h32 ^= h32 >> 16; + return(h32); +} + +#define XXH_get32bits(p) XXH_readLE32_align(p, endian, align) + +static U32 +XXH32_finalize(U32 h32, const void* ptr, size_t len, + XXH_endianess endian, XXH_alignment align) + +{ + const BYTE* p = (const BYTE*)ptr; + +#define PROCESS1 \ + h32 += (*p++) * PRIME32_5; \ + h32 = XXH_rotl32(h32, 11) * PRIME32_1 ; + +#define PROCESS4 \ + h32 += XXH_get32bits(p) * PRIME32_3; \ + p+=4; \ + h32 = XXH_rotl32(h32, 17) * PRIME32_4 ; + + switch(len&15) /* or switch(bEnd - p) */ + { + case 12: PROCESS4; + /* fallthrough */ + case 8: PROCESS4; + /* fallthrough */ + case 4: PROCESS4; + return XXH32_avalanche(h32); + + case 13: PROCESS4; + /* fallthrough */ + case 9: PROCESS4; + /* fallthrough */ + case 5: PROCESS4; + PROCESS1; + return XXH32_avalanche(h32); + + case 14: PROCESS4; + /* fallthrough */ + case 10: PROCESS4; + /* fallthrough */ + case 6: PROCESS4; + PROCESS1; + PROCESS1; + return XXH32_avalanche(h32); + + case 15: PROCESS4; + /* fallthrough */ + case 11: PROCESS4; + /* fallthrough */ + case 7: PROCESS4; + /* fallthrough */ + case 3: PROCESS1; + /* fallthrough */ + case 2: PROCESS1; + /* fallthrough */ + case 1: PROCESS1; + /* fallthrough */ + case 0: return XXH32_avalanche(h32); + } + assert(0); + return h32; /* reaching this point is deemed impossible */ +} + + +FORCE_INLINE U32 +XXH32_endian_align(const void* input, size_t 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(p, endian, align) -#ifdef XXH_ACCEPT_NULL_INPUT_POINTER +#if defined(XXH_ACCEPT_NULL_INPUT_POINTER) && (XXH_ACCEPT_NULL_INPUT_POINTER>=1) if (p==NULL) { len=0; bEnd=p=(const BYTE*)(size_t)16; @@ -281,7 +364,7 @@ XXH_FORCE_INLINE U32 XXH32_endian_align(const void* input, size_t len, U32 seed, #endif if (len>=16) { - const BYTE* const limit = bEnd - 16; + const BYTE* const limit = bEnd - 15; U32 v1 = seed + PRIME32_1 + PRIME32_2; U32 v2 = seed + PRIME32_2; U32 v3 = seed + 0; @@ -292,34 +375,17 @@ XXH_FORCE_INLINE U32 XXH32_endian_align(const void* input, size_t len, U32 seed, v2 = XXH32_round(v2, XXH_get32bits(p)); p+=4; v3 = XXH32_round(v3, XXH_get32bits(p)); p+=4; v4 = XXH32_round(v4, XXH_get32bits(p)); p+=4; - } while (p<=limit); + } while (p < limit); - h32 = XXH_rotl32(v1, 1) + XXH_rotl32(v2, 7) + XXH_rotl32(v3, 12) + XXH_rotl32(v4, 18); + 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; + h32 += (U32)len; - return h32; + return XXH32_finalize(h32, p, len&15, endian, align); } @@ -371,74 +437,81 @@ XXH_PUBLIC_API void XXH32_copyState(XXH32_state_t* dstState, const XXH32_state_t XXH_PUBLIC_API XXH_errorcode XXH32_reset(XXH32_state_t* statePtr, unsigned int seed) { XXH32_state_t state; /* using a local state to memcpy() in order to avoid strict-aliasing warnings */ - memset(&state, 0, sizeof(state)-4); /* do not write into reserved, for future removal */ + memset(&state, 0, sizeof(state)); state.v1 = seed + PRIME32_1 + PRIME32_2; state.v2 = seed + PRIME32_2; state.v3 = seed + 0; state.v4 = seed - PRIME32_1; - memcpy(statePtr, &state, sizeof(state)); + /* do not write into reserved, planned to be removed in a future version */ + memcpy(statePtr, &state, sizeof(state) - sizeof(state.reserved)); return XXH_OK; } -XXH_FORCE_INLINE XXH_errorcode XXH32_update_endian (XXH32_state_t* state, const void* input, size_t len, XXH_endianess endian) +FORCE_INLINE XXH_errorcode +XXH32_update_endian(XXH32_state_t* state, const void* input, size_t len, XXH_endianess endian) { - const BYTE* p = (const BYTE*)input; - const BYTE* const bEnd = p + len; - -#ifdef XXH_ACCEPT_NULL_INPUT_POINTER - if (input==NULL) return XXH_ERROR; + if (input==NULL) +#if defined(XXH_ACCEPT_NULL_INPUT_POINTER) && (XXH_ACCEPT_NULL_INPUT_POINTER>=1) + return XXH_OK; +#else + return XXH_ERROR; #endif - state->total_len_32 += (unsigned)len; - state->large_len |= (len>=16) | (state->total_len_32>=16); + { const BYTE* p = (const BYTE*)input; + const BYTE* const bEnd = p + len; - if (state->memsize + len < 16) { /* fill in tmp buffer */ - XXH_memcpy((BYTE*)(state->mem32) + state->memsize, input, len); - state->memsize += (unsigned)len; - return XXH_OK; - } + state->total_len_32 += (unsigned)len; + state->large_len |= (len>=16) | (state->total_len_32>=16); - if (state->memsize) { /* some data left from previous update */ - XXH_memcpy((BYTE*)(state->mem32) + state->memsize, input, 16-state->memsize); - { const U32* p32 = state->mem32; - state->v1 = XXH32_round(state->v1, XXH_readLE32(p32, endian)); p32++; - state->v2 = XXH32_round(state->v2, XXH_readLE32(p32, endian)); p32++; - state->v3 = XXH32_round(state->v3, XXH_readLE32(p32, endian)); p32++; - state->v4 = XXH32_round(state->v4, XXH_readLE32(p32, endian)); p32++; + if (state->memsize + len < 16) { /* fill in tmp buffer */ + XXH_memcpy((BYTE*)(state->mem32) + state->memsize, input, len); + state->memsize += (unsigned)len; + return XXH_OK; } - 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 = XXH32_round(v1, XXH_readLE32(p, endian)); p+=4; - v2 = XXH32_round(v2, XXH_readLE32(p, endian)); p+=4; - v3 = XXH32_round(v3, XXH_readLE32(p, endian)); p+=4; - v4 = XXH32_round(v4, XXH_readLE32(p, endian)); p+=4; - } while (p<=limit); + if (state->memsize) { /* some data left from previous update */ + XXH_memcpy((BYTE*)(state->mem32) + state->memsize, input, 16-state->memsize); + { const U32* p32 = state->mem32; + state->v1 = XXH32_round(state->v1, XXH_readLE32(p32, endian)); p32++; + state->v2 = XXH32_round(state->v2, XXH_readLE32(p32, endian)); p32++; + state->v3 = XXH32_round(state->v3, XXH_readLE32(p32, endian)); p32++; + state->v4 = XXH32_round(state->v4, XXH_readLE32(p32, endian)); + } + p += 16-state->memsize; + state->memsize = 0; + } - state->v1 = v1; - state->v2 = v2; - state->v3 = v3; - state->v4 = v4; - } + 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 = XXH32_round(v1, XXH_readLE32(p, endian)); p+=4; + v2 = XXH32_round(v2, XXH_readLE32(p, endian)); p+=4; + v3 = XXH32_round(v3, XXH_readLE32(p, endian)); p+=4; + v4 = XXH32_round(v4, XXH_readLE32(p, endian)); p+=4; + } while (p<=limit); + + state->v1 = v1; + state->v2 = v2; + state->v3 = v3; + state->v4 = v4; + } - if (p < bEnd) { - XXH_memcpy(state->mem32, p, (size_t)(bEnd-p)); - state->memsize = (unsigned)(bEnd-p); + if (p < bEnd) { + XXH_memcpy(state->mem32, p, (size_t)(bEnd-p)); + state->memsize = (unsigned)(bEnd-p); + } } return XXH_OK; } + XXH_PUBLIC_API XXH_errorcode XXH32_update (XXH32_state_t* state_in, const void* input, size_t len) { XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN; @@ -450,40 +523,23 @@ XXH_PUBLIC_API XXH_errorcode XXH32_update (XXH32_state_t* state_in, const void* } - -XXH_FORCE_INLINE U32 XXH32_digest_endian (const XXH32_state_t* state, XXH_endianess endian) +FORCE_INLINE U32 +XXH32_digest_endian (const XXH32_state_t* state, XXH_endianess endian) { - const BYTE * p = (const BYTE*)state->mem32; - const BYTE* const bEnd = (const BYTE*)(state->mem32) + state->memsize; U32 h32; if (state->large_len) { - h32 = XXH_rotl32(state->v1, 1) + XXH_rotl32(state->v2, 7) + XXH_rotl32(state->v3, 12) + XXH_rotl32(state->v4, 18); + h32 = XXH_rotl32(state->v1, 1) + + XXH_rotl32(state->v2, 7) + + XXH_rotl32(state->v3, 12) + + XXH_rotl32(state->v4, 18); } else { h32 = state->v3 /* == seed */ + PRIME32_5; } h32 += state->total_len_32; - while (p+4<=bEnd) { - h32 += XXH_readLE32(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; + return XXH32_finalize(h32, state->mem32, state->memsize, endian, XXH_aligned); } @@ -503,7 +559,7 @@ XXH_PUBLIC_API unsigned int XXH32_digest (const XXH32_state_t* state_in) /*! Default XXH result types are basic unsigned 32 and 64 bits. * The canonical representation follows human-readable write convention, aka big-endian (large digits first). * These functions allow transformation of hash result into and from its canonical format. -* This way, hash values can be written into a file or buffer, and remain comparable across different systems and programs. +* This way, hash values can be written into a file or buffer, remaining comparable across different systems. */ XXH_PUBLIC_API void XXH32_canonicalFromHash(XXH32_canonical_t* dst, XXH32_hash_t hash) @@ -522,18 +578,21 @@ XXH_PUBLIC_API XXH32_hash_t XXH32_hashFromCanonical(const XXH32_canonical_t* src #ifndef XXH_NO_LONG_LONG /* ******************************************************************* -* 64-bits hash functions +* 64-bit hash functions *********************************************************************/ /*====== Memory access ======*/ #ifndef MEM_MODULE # define MEM_MODULE -# if !defined (__VMS) && (defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) ) +# if !defined (__VMS) \ + && (defined (__cplusplus) \ + || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) ) # include typedef uint64_t U64; # else - typedef unsigned long long U64; /* if your compiler doesn't support unsigned long long, replace by another 64-bit type here. Note that xxhash.h will also need to be updated. */ + /* if compiler doesn't support unsigned long long, replace by another 64-bit type */ + typedef unsigned long long U64; # endif #endif @@ -583,7 +642,7 @@ static U64 XXH_swap64 (U64 x) } #endif -XXH_FORCE_INLINE U64 XXH_readLE64_align(const void* ptr, XXH_endianess endian, XXH_alignment align) +FORCE_INLINE U64 XXH_readLE64_align(const void* ptr, XXH_endianess endian, XXH_alignment align) { if (align==XXH_unaligned) return endian==XXH_littleEndian ? XXH_read64(ptr) : XXH_swap64(XXH_read64(ptr)); @@ -591,7 +650,7 @@ XXH_FORCE_INLINE U64 XXH_readLE64_align(const void* ptr, XXH_endianess endian, X return endian==XXH_littleEndian ? *(const U64*)ptr : XXH_swap64(*(const U64*)ptr); } -XXH_FORCE_INLINE U64 XXH_readLE64(const void* ptr, XXH_endianess endian) +FORCE_INLINE U64 XXH_readLE64(const void* ptr, XXH_endianess endian) { return XXH_readLE64_align(ptr, endian, XXH_unaligned); } @@ -626,14 +685,137 @@ static U64 XXH64_mergeRound(U64 acc, U64 val) return acc; } -XXH_FORCE_INLINE U64 XXH64_endian_align(const void* input, size_t len, U64 seed, XXH_endianess endian, XXH_alignment align) +static U64 XXH64_avalanche(U64 h64) +{ + h64 ^= h64 >> 33; + h64 *= PRIME64_2; + h64 ^= h64 >> 29; + h64 *= PRIME64_3; + h64 ^= h64 >> 32; + return h64; +} + + +#define XXH_get64bits(p) XXH_readLE64_align(p, endian, align) + +static U64 +XXH64_finalize(U64 h64, const void* ptr, size_t len, + XXH_endianess endian, XXH_alignment align) +{ + const BYTE* p = (const BYTE*)ptr; + +#define PROCESS1_64 \ + h64 ^= (*p++) * PRIME64_5; \ + h64 = XXH_rotl64(h64, 11) * PRIME64_1; + +#define PROCESS4_64 \ + h64 ^= (U64)(XXH_get32bits(p)) * PRIME64_1; \ + p+=4; \ + h64 = XXH_rotl64(h64, 23) * PRIME64_2 + PRIME64_3; + +#define PROCESS8_64 { \ + U64 const k1 = XXH64_round(0, XXH_get64bits(p)); \ + p+=8; \ + h64 ^= k1; \ + h64 = XXH_rotl64(h64,27) * PRIME64_1 + PRIME64_4; \ +} + + switch(len&31) { + case 24: PROCESS8_64; + /* fallthrough */ + case 16: PROCESS8_64; + /* fallthrough */ + case 8: PROCESS8_64; + return XXH64_avalanche(h64); + + case 28: PROCESS8_64; + /* fallthrough */ + case 20: PROCESS8_64; + /* fallthrough */ + case 12: PROCESS8_64; + /* fallthrough */ + case 4: PROCESS4_64; + return XXH64_avalanche(h64); + + case 25: PROCESS8_64; + /* fallthrough */ + case 17: PROCESS8_64; + /* fallthrough */ + case 9: PROCESS8_64; + PROCESS1_64; + return XXH64_avalanche(h64); + + case 29: PROCESS8_64; + /* fallthrough */ + case 21: PROCESS8_64; + /* fallthrough */ + case 13: PROCESS8_64; + /* fallthrough */ + case 5: PROCESS4_64; + PROCESS1_64; + return XXH64_avalanche(h64); + + case 26: PROCESS8_64; + /* fallthrough */ + case 18: PROCESS8_64; + /* fallthrough */ + case 10: PROCESS8_64; + PROCESS1_64; + PROCESS1_64; + return XXH64_avalanche(h64); + + case 30: PROCESS8_64; + /* fallthrough */ + case 22: PROCESS8_64; + /* fallthrough */ + case 14: PROCESS8_64; + /* fallthrough */ + case 6: PROCESS4_64; + PROCESS1_64; + PROCESS1_64; + return XXH64_avalanche(h64); + + case 27: PROCESS8_64; + /* fallthrough */ + case 19: PROCESS8_64; + /* fallthrough */ + case 11: PROCESS8_64; + PROCESS1_64; + PROCESS1_64; + PROCESS1_64; + return XXH64_avalanche(h64); + + case 31: PROCESS8_64; + /* fallthrough */ + case 23: PROCESS8_64; + /* fallthrough */ + case 15: PROCESS8_64; + /* fallthrough */ + case 7: PROCESS4_64; + /* fallthrough */ + case 3: PROCESS1_64; + /* fallthrough */ + case 2: PROCESS1_64; + /* fallthrough */ + case 1: PROCESS1_64; + /* fallthrough */ + case 0: return XXH64_avalanche(h64); + } + + /* impossible to reach */ + assert(0); + return 0; /* unreachable, but some compilers complain without it */ +} + +FORCE_INLINE U64 +XXH64_endian_align(const void* input, size_t len, U64 seed, + XXH_endianess endian, XXH_alignment align) { const BYTE* p = (const BYTE*)input; - const BYTE* const bEnd = p + len; + const BYTE* bEnd = p + len; U64 h64; -#define XXH_get64bits(p) XXH_readLE64_align(p, endian, align) -#ifdef XXH_ACCEPT_NULL_INPUT_POINTER +#if defined(XXH_ACCEPT_NULL_INPUT_POINTER) && (XXH_ACCEPT_NULL_INPUT_POINTER>=1) if (p==NULL) { len=0; bEnd=p=(const BYTE*)(size_t)32; @@ -666,32 +848,7 @@ XXH_FORCE_INLINE U64 XXH64_endian_align(const void* input, size_t len, U64 seed, h64 += (U64) len; - while (p+8<=bEnd) { - U64 const k1 = XXH64_round(0, XXH_get64bits(p)); - 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; + return XXH64_finalize(h64, p, len, endian, align); } @@ -741,65 +898,71 @@ XXH_PUBLIC_API void XXH64_copyState(XXH64_state_t* dstState, const XXH64_state_t XXH_PUBLIC_API XXH_errorcode XXH64_reset(XXH64_state_t* statePtr, unsigned long long seed) { XXH64_state_t state; /* using a local state to memcpy() in order to avoid strict-aliasing warnings */ - memset(&state, 0, sizeof(state)-8); /* do not write into reserved, for future removal */ + memset(&state, 0, sizeof(state)); state.v1 = seed + PRIME64_1 + PRIME64_2; state.v2 = seed + PRIME64_2; state.v3 = seed + 0; state.v4 = seed - PRIME64_1; - memcpy(statePtr, &state, sizeof(state)); + /* do not write into reserved, planned to be removed in a future version */ + memcpy(statePtr, &state, sizeof(state) - sizeof(state.reserved)); return XXH_OK; } -XXH_FORCE_INLINE XXH_errorcode XXH64_update_endian (XXH64_state_t* state, const void* input, size_t len, XXH_endianess endian) +FORCE_INLINE XXH_errorcode +XXH64_update_endian (XXH64_state_t* state, const void* input, size_t len, XXH_endianess endian) { - const BYTE* p = (const BYTE*)input; - const BYTE* const bEnd = p + len; - -#ifdef XXH_ACCEPT_NULL_INPUT_POINTER - if (input==NULL) return XXH_ERROR; + if (input==NULL) +#if defined(XXH_ACCEPT_NULL_INPUT_POINTER) && (XXH_ACCEPT_NULL_INPUT_POINTER>=1) + return XXH_OK; +#else + return XXH_ERROR; #endif - state->total_len += len; - - if (state->memsize + len < 32) { /* fill in tmp buffer */ - XXH_memcpy(((BYTE*)state->mem64) + state->memsize, input, len); - state->memsize += (U32)len; - return XXH_OK; - } + { const BYTE* p = (const BYTE*)input; + const BYTE* const bEnd = p + len; - if (state->memsize) { /* tmp buffer is full */ - XXH_memcpy(((BYTE*)state->mem64) + state->memsize, input, 32-state->memsize); - state->v1 = XXH64_round(state->v1, XXH_readLE64(state->mem64+0, endian)); - state->v2 = XXH64_round(state->v2, XXH_readLE64(state->mem64+1, endian)); - state->v3 = XXH64_round(state->v3, XXH_readLE64(state->mem64+2, endian)); - state->v4 = XXH64_round(state->v4, XXH_readLE64(state->mem64+3, endian)); - p += 32-state->memsize; - state->memsize = 0; - } + state->total_len += len; - 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; + if (state->memsize + len < 32) { /* fill in tmp buffer */ + XXH_memcpy(((BYTE*)state->mem64) + state->memsize, input, len); + state->memsize += (U32)len; + return XXH_OK; + } - do { - v1 = XXH64_round(v1, XXH_readLE64(p, endian)); p+=8; - v2 = XXH64_round(v2, XXH_readLE64(p, endian)); p+=8; - v3 = XXH64_round(v3, XXH_readLE64(p, endian)); p+=8; - v4 = XXH64_round(v4, XXH_readLE64(p, endian)); p+=8; - } while (p<=limit); + if (state->memsize) { /* tmp buffer is full */ + XXH_memcpy(((BYTE*)state->mem64) + state->memsize, input, 32-state->memsize); + state->v1 = XXH64_round(state->v1, XXH_readLE64(state->mem64+0, endian)); + state->v2 = XXH64_round(state->v2, XXH_readLE64(state->mem64+1, endian)); + state->v3 = XXH64_round(state->v3, XXH_readLE64(state->mem64+2, endian)); + state->v4 = XXH64_round(state->v4, XXH_readLE64(state->mem64+3, endian)); + p += 32-state->memsize; + state->memsize = 0; + } - state->v1 = v1; - state->v2 = v2; - state->v3 = v3; - state->v4 = v4; - } + 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 = XXH64_round(v1, XXH_readLE64(p, endian)); p+=8; + v2 = XXH64_round(v2, XXH_readLE64(p, endian)); p+=8; + v3 = XXH64_round(v3, XXH_readLE64(p, endian)); p+=8; + v4 = XXH64_round(v4, XXH_readLE64(p, endian)); p+=8; + } while (p<=limit); + + state->v1 = v1; + state->v2 = v2; + state->v3 = v3; + state->v4 = v4; + } - if (p < bEnd) { - XXH_memcpy(state->mem64, p, (size_t)(bEnd-p)); - state->memsize = (unsigned)(bEnd-p); + if (p < bEnd) { + XXH_memcpy(state->mem64, p, (size_t)(bEnd-p)); + state->memsize = (unsigned)(bEnd-p); + } } return XXH_OK; @@ -815,10 +978,8 @@ XXH_PUBLIC_API XXH_errorcode XXH64_update (XXH64_state_t* state_in, const void* return XXH64_update_endian(state_in, input, len, XXH_bigEndian); } -XXH_FORCE_INLINE U64 XXH64_digest_endian (const XXH64_state_t* state, XXH_endianess endian) +FORCE_INLINE U64 XXH64_digest_endian (const XXH64_state_t* state, XXH_endianess endian) { - const BYTE * p = (const BYTE*)state->mem64; - const BYTE* const bEnd = (const BYTE*)state->mem64 + state->memsize; U64 h64; if (state->total_len >= 32) { @@ -833,37 +994,12 @@ XXH_FORCE_INLINE U64 XXH64_digest_endian (const XXH64_state_t* state, XXH_endian h64 = XXH64_mergeRound(h64, v3); h64 = XXH64_mergeRound(h64, v4); } else { - h64 = state->v3 + PRIME64_5; + h64 = state->v3 /*seed*/ + PRIME64_5; } h64 += (U64) state->total_len; - while (p+8<=bEnd) { - U64 const k1 = XXH64_round(0, XXH_readLE64(p, endian)); - h64 ^= k1; - h64 = XXH_rotl64(h64,27) * PRIME64_1 + PRIME64_4; - p+=8; - } - - if (p+4<=bEnd) { - h64 ^= (U64)(XXH_readLE32(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; + return XXH64_finalize(h64, state->mem64, (size_t)state->total_len, endian, XXH_aligned); } XXH_PUBLIC_API unsigned long long XXH64_digest (const XXH64_state_t* state_in) diff --git a/lib/xxhash.h b/lib/xxhash.h index 870a6d9..d6bad94 100644 --- a/lib/xxhash.h +++ b/lib/xxhash.h @@ -57,8 +57,8 @@ Q.Score is a measure of quality of the hash function. It depends on successfully passing SMHasher test set. 10 is a perfect score. -A 64-bits version, named XXH64, is available since r35. -It offers much better speed, but for 64-bits applications only. +A 64-bit version, named XXH64, is available since r35. +It offers much better speed, but for 64-bit applications only. Name Speed on 64 bits Speed on 32 bits XXH64 13.8 GB/s 1.9 GB/s XXH32 6.8 GB/s 6.0 GB/s @@ -80,18 +80,19 @@ typedef enum { XXH_OK=0, XXH_ERROR } XXH_errorcode; /* **************************** -* API modifier -******************************/ -/** XXH_PRIVATE_API -* This is useful to include xxhash functions in `static` mode -* in order to inline them, and remove their symbol from the public list. -* Methodology : -* #define XXH_PRIVATE_API -* #include "xxhash.h" -* `xxhash.c` is automatically included. -* It's not useful to compile and link it as a separate module. -*/ -#ifdef XXH_PRIVATE_API + * API modifier + ******************************/ +/** XXH_INLINE_ALL (and XXH_PRIVATE_API) + * This is useful to include xxhash functions in `static` mode + * in order to inline them, and remove their symbol from the public list. + * Inlining can offer dramatic performance improvement on small keys. + * Methodology : + * #define XXH_INLINE_ALL + * #include "xxhash.h" + * `xxhash.c` is automatically included. + * It's not useful to compile and link it as a separate module. + */ +#if defined(XXH_INLINE_ALL) || defined(XXH_PRIVATE_API) # ifndef XXH_STATIC_LINKING_ONLY # define XXH_STATIC_LINKING_ONLY # endif @@ -102,23 +103,24 @@ typedef enum { XXH_OK=0, XXH_ERROR } XXH_errorcode; # elif defined(_MSC_VER) # define XXH_PUBLIC_API static __inline # else -# define XXH_PUBLIC_API static /* this version may generate warnings for unused static functions; disable the relevant warning */ + /* this version may generate warnings for unused static functions */ +# define XXH_PUBLIC_API static # endif #else # define XXH_PUBLIC_API /* do nothing */ -#endif /* XXH_PRIVATE_API */ - -/*!XXH_NAMESPACE, aka Namespace Emulation : - -If you want to include _and expose_ xxHash functions from within your own library, -but also want to avoid symbol collisions with other libraries which may also include xxHash, - -you can use XXH_NAMESPACE, to automatically prefix any public symbol from xxhash library -with the value of XXH_NAMESPACE (therefore, avoid NULL and numeric values). - -Note that no change is required within the calling program as long as it includes `xxhash.h` : -regular symbol name will be automatically translated by this header. -*/ +#endif /* XXH_INLINE_ALL || XXH_PRIVATE_API */ + +/*! XXH_NAMESPACE, aka Namespace Emulation : + * + * If you want to include _and expose_ xxHash functions from within your own library, + * but also want to avoid symbol collisions with other libraries which may also include xxHash, + * + * you can use XXH_NAMESPACE, to automatically prefix any public symbol from xxhash library + * with the value of XXH_NAMESPACE (therefore, avoid NULL and numeric values). + * + * Note that no change is required within the calling program as long as it includes `xxhash.h` : + * regular symbol name will be automatically translated by this header. + */ #ifdef XXH_NAMESPACE # define XXH_CAT(A,B) A##B # define XXH_NAME2(A,B) XXH_CAT(A,B) @@ -149,18 +151,18 @@ regular symbol name will be automatically translated by this header. ***************************************/ #define XXH_VERSION_MAJOR 0 #define XXH_VERSION_MINOR 6 -#define XXH_VERSION_RELEASE 2 +#define XXH_VERSION_RELEASE 5 #define XXH_VERSION_NUMBER (XXH_VERSION_MAJOR *100*100 + XXH_VERSION_MINOR *100 + XXH_VERSION_RELEASE) XXH_PUBLIC_API unsigned XXH_versionNumber (void); /*-********************************************************************** -* 32-bits hash +* 32-bit hash ************************************************************************/ -typedef unsigned int XXH32_hash_t; +typedef unsigned int XXH32_hash_t; /*! XXH32() : - Calculate the 32-bits hash of sequence "length" bytes stored at memory address "input". + Calculate the 32-bit hash of sequence "length" bytes stored at memory address "input". The memory between input & input+length must be valid (allocated and read-accessible). "seed" can be used to alter the result predictably. Speed on Core 2 Duo @ 3 GHz (single thread, SMHasher benchmark) : 5.4 GB/s */ @@ -177,26 +179,25 @@ XXH_PUBLIC_API XXH_errorcode XXH32_update (XXH32_state_t* statePtr, const void* XXH_PUBLIC_API XXH32_hash_t XXH32_digest (const XXH32_state_t* statePtr); /* -These functions generate the xxHash of an input provided in multiple segments. -Note that, for small input, they are slower than single-call functions, due to state management. -For small input, prefer `XXH32()` and `XXH64()` . - -XXH state must first be allocated, using XXH*_createState() . - -Start a new hash by initializing state with a seed, using XXH*_reset(). - -Then, feed the hash state by calling XXH*_update() as many times as necessary. -Obviously, input must be allocated and read accessible. -The function returns an error code, with 0 meaning OK, and any other value meaning there is an error. - -Finally, a hash value can be produced anytime, by using XXH*_digest(). -This function returns the nn-bits hash as an int or long long. - -It's still possible to continue inserting input into the hash state after a digest, -and generate some new hashes later on, by calling again XXH*_digest(). - -When done, free XXH state space if it was allocated dynamically. -*/ + * Streaming functions generate the xxHash of an input provided in multiple segments. + * Note that, for small input, they are slower than single-call functions, due to state management. + * For small inputs, prefer `XXH32()` and `XXH64()`, which are better optimized. + * + * XXH state must first be allocated, using XXH*_createState() . + * + * Start a new hash by initializing state with a seed, using XXH*_reset(). + * + * Then, feed the hash state by calling XXH*_update() as many times as necessary. + * The function returns an error code, with 0 meaning OK, and any other value meaning there is an error. + * + * Finally, a hash value can be produced anytime, by using XXH*_digest(). + * This function returns the nn-bits hash as an int or long long. + * + * It's still possible to continue inserting input into the hash state after a digest, + * and generate some new hashes later on, by calling again XXH*_digest(). + * + * When done, free XXH state space if it was allocated dynamically. + */ /*====== Canonical representation ======*/ @@ -205,22 +206,22 @@ XXH_PUBLIC_API void XXH32_canonicalFromHash(XXH32_canonical_t* dst, XXH32_hash_t XXH_PUBLIC_API XXH32_hash_t XXH32_hashFromCanonical(const XXH32_canonical_t* src); /* Default result type for XXH functions are primitive unsigned 32 and 64 bits. -* The canonical representation uses human-readable write convention, aka big-endian (large digits first). -* These functions allow transformation of hash result into and from its canonical format. -* This way, hash values can be written into a file / memory, and remain comparable on different systems and programs. -*/ + * The canonical representation uses human-readable write convention, aka big-endian (large digits first). + * These functions allow transformation of hash result into and from its canonical format. + * This way, hash values can be written into a file / memory, and remain comparable on different systems and programs. + */ #ifndef XXH_NO_LONG_LONG /*-********************************************************************** -* 64-bits hash +* 64-bit hash ************************************************************************/ typedef unsigned long long XXH64_hash_t; /*! XXH64() : - Calculate the 64-bits hash of sequence of length "len" stored at memory address "input". + Calculate the 64-bit hash of sequence of length "len" stored at memory address "input". "seed" can be used to alter the result predictably. - This function runs faster on 64-bits systems, but slower on 32-bits systems (see benchmark). + This function runs faster on 64-bit systems, but slower on 32-bit systems (see benchmark). */ XXH_PUBLIC_API XXH64_hash_t XXH64 (const void* input, size_t length, unsigned long long seed); @@ -241,48 +242,82 @@ XXH_PUBLIC_API XXH64_hash_t XXH64_hashFromCanonical(const XXH64_canonical_t* src #endif /* XXH_NO_LONG_LONG */ + #ifdef XXH_STATIC_LINKING_ONLY /* ================================================================================================ - This section contains definitions which are not guaranteed to remain stable. + This section contains declarations which are not guaranteed to remain stable. They may change in future versions, becoming incompatible with a different version of the library. - They shall only be used with static linking. - Never use these definitions in association with dynamic linking ! + These declarations should only be used with static linking. + Never use them in association with dynamic linking ! =================================================================================================== */ -/* These definitions are only meant to allow allocation of XXH state - statically, on stack, or in a struct for example. - Do not use members directly. */ - - struct XXH32_state_s { - unsigned total_len_32; - unsigned large_len; - unsigned v1; - unsigned v2; - unsigned v3; - unsigned v4; - unsigned mem32[4]; /* buffer defined as U32 for alignment */ - unsigned memsize; - unsigned reserved; /* never read nor write, will be removed in a future version */ - }; /* typedef'd to XXH32_state_t */ - -#ifndef XXH_NO_LONG_LONG - struct XXH64_state_s { - unsigned long long total_len; - unsigned long long v1; - unsigned long long v2; - unsigned long long v3; - unsigned long long v4; - unsigned long long mem64[4]; /* buffer defined as U64 for alignment */ - unsigned memsize; - unsigned reserved[2]; /* never read nor write, will be removed in a future version */ - }; /* typedef'd to XXH64_state_t */ +/* These definitions are only present to allow + * static allocation of XXH state, on stack or in a struct for example. + * Never **ever** use members directly. */ + +#if !defined (__VMS) \ + && (defined (__cplusplus) \ + || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) ) +# include + +struct XXH32_state_s { + uint32_t total_len_32; + uint32_t large_len; + uint32_t v1; + uint32_t v2; + uint32_t v3; + uint32_t v4; + uint32_t mem32[4]; + uint32_t memsize; + uint32_t reserved; /* never read nor write, might be removed in a future version */ +}; /* typedef'd to XXH32_state_t */ + +struct XXH64_state_s { + uint64_t total_len; + uint64_t v1; + uint64_t v2; + uint64_t v3; + uint64_t v4; + uint64_t mem64[4]; + uint32_t memsize; + uint32_t reserved[2]; /* never read nor write, might be removed in a future version */ +}; /* typedef'd to XXH64_state_t */ + +# else + +struct XXH32_state_s { + unsigned total_len_32; + unsigned large_len; + unsigned v1; + unsigned v2; + unsigned v3; + unsigned v4; + unsigned mem32[4]; + unsigned memsize; + unsigned reserved; /* never read nor write, might be removed in a future version */ +}; /* typedef'd to XXH32_state_t */ + +# ifndef XXH_NO_LONG_LONG /* remove 64-bit support */ +struct XXH64_state_s { + unsigned long long total_len; + unsigned long long v1; + unsigned long long v2; + unsigned long long v3; + unsigned long long v4; + unsigned long long mem64[4]; + unsigned memsize; + unsigned reserved[2]; /* never read nor write, might be removed in a future version */ +}; /* typedef'd to XXH64_state_t */ +# endif + +# endif + + +#if defined(XXH_INLINE_ALL) || defined(XXH_PRIVATE_API) +# include "xxhash.c" /* include xxhash function bodies as `static`, for inlining */ #endif -# ifdef XXH_PRIVATE_API -# include "xxhash.c" /* include xxhash function bodies as `static`, for inlining */ -# endif - #endif /* XXH_STATIC_LINKING_ONLY */ -- cgit v1.2.3 From 8bea19d57c0db6d3d812c7acc3d1834762bce297 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 18 Sep 2018 15:51:26 -0700 Subject: fixed minor cppcheck warnings in lib --- lib/lz4.c | 398 +++++++++++++++++++++++++++++---------------------------- lib/lz4frame.c | 14 +- lib/lz4hc.c | 3 +- 3 files changed, 208 insertions(+), 207 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 4046102..df7a2c7 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1414,234 +1414,236 @@ LZ4_decompress_generic( const size_t dictSize /* note : = 0 if noDict */ ) { - const BYTE* ip = (const BYTE*) src; - const BYTE* const iend = ip + srcSize; + if (src == NULL) return -1; - BYTE* op = (BYTE*) dst; - BYTE* const oend = op + outputSize; - BYTE* cpy; + { const BYTE* ip = (const BYTE*) src; + const BYTE* const iend = ip + srcSize; - const BYTE* const dictEnd = (const BYTE*)dictStart + dictSize; - const unsigned inc32table[8] = {0, 1, 2, 1, 0, 4, 4, 4}; - const int dec64table[8] = {0, 0, 0, -1, -4, 1, 2, 3}; + BYTE* op = (BYTE*) dst; + BYTE* const oend = op + outputSize; + BYTE* cpy; - const int safeDecode = (endOnInput==endOnInputSize); - const int checkOffset = ((safeDecode) && (dictSize < (int)(64 KB))); + const BYTE* const dictEnd = (dictStart == NULL) ? NULL : dictStart + dictSize; + const unsigned inc32table[8] = {0, 1, 2, 1, 0, 4, 4, 4}; + const int dec64table[8] = {0, 0, 0, -1, -4, 1, 2, 3}; - /* Set up the "end" pointers for the shortcut. */ - const BYTE* const shortiend = iend - (endOnInput ? 14 : 8) /*maxLL*/ - 2 /*offset*/; - const BYTE* const shortoend = oend - (endOnInput ? 14 : 8) /*maxLL*/ - 18 /*maxML*/; + const int safeDecode = (endOnInput==endOnInputSize); + const int checkOffset = ((safeDecode) && (dictSize < (int)(64 KB))); - DEBUGLOG(5, "LZ4_decompress_generic (srcSize:%i, dstSize:%i)", srcSize, outputSize); + /* Set up the "end" pointers for the shortcut. */ + const BYTE* const shortiend = iend - (endOnInput ? 14 : 8) /*maxLL*/ - 2 /*offset*/; + const BYTE* const shortoend = oend - (endOnInput ? 14 : 8) /*maxLL*/ - 18 /*maxML*/; - /* Special cases */ - assert(lowPrefix <= op); - assert(src != NULL); - if ((endOnInput) && (unlikely(outputSize==0))) return ((srcSize==1) && (*ip==0)) ? 0 : -1; /* Empty output buffer */ - if ((!endOnInput) && (unlikely(outputSize==0))) return (*ip==0 ? 1 : -1); - if ((endOnInput) && unlikely(srcSize==0)) return -1; + DEBUGLOG(5, "LZ4_decompress_generic (srcSize:%i, dstSize:%i)", srcSize, outputSize); - /* Main Loop : decode sequences */ - while (1) { - const BYTE* match; - size_t offset; - - unsigned const token = *ip++; - size_t length = token >> ML_BITS; /* literal length */ + /* Special cases */ + assert(lowPrefix <= op); + if ((endOnInput) && (unlikely(outputSize==0))) return ((srcSize==1) && (*ip==0)) ? 0 : -1; /* Empty output buffer */ + if ((!endOnInput) && (unlikely(outputSize==0))) return (*ip==0 ? 1 : -1); + if ((endOnInput) && unlikely(srcSize==0)) return -1; - assert(!endOnInput || ip <= iend); /* ip < iend before the increment */ + /* Main Loop : decode sequences */ + while (1) { + const BYTE* match; + size_t offset; - /* A two-stage shortcut for the most common case: - * 1) If the literal length is 0..14, and there is enough space, - * enter the shortcut and copy 16 bytes on behalf of the literals - * (in the fast mode, only 8 bytes can be safely copied this way). - * 2) Further if the match length is 4..18, copy 18 bytes in a similar - * manner; but we ensure that there's enough space in the output for - * those 18 bytes earlier, upon entering the shortcut (in other words, - * there is a combined check for both stages). - */ - if ( (endOnInput ? length != RUN_MASK : length <= 8) - /* strictly "less than" on input, to re-enter the loop with at least one byte */ - && likely((endOnInput ? ip < shortiend : 1) & (op <= shortoend)) ) { - /* Copy the literals */ - memcpy(op, ip, endOnInput ? 16 : 8); - op += length; ip += length; - - /* The second stage: prepare for match copying, decode full info. - * If it doesn't work out, the info won't be wasted. */ - length = token & ML_MASK; /* match length */ - offset = LZ4_readLE16(ip); ip += 2; - match = op - offset; - assert(match <= op); /* check overflow */ - - /* Do not deal with overlapping matches. */ - if ( (length != ML_MASK) - && (offset >= 8) - && (dict==withPrefix64k || match >= lowPrefix) ) { - /* Copy the match. */ - memcpy(op + 0, match + 0, 8); - memcpy(op + 8, match + 8, 8); - memcpy(op +16, match +16, 2); - op += length + MINMATCH; - /* Both stages worked, load the next token. */ - continue; - } + unsigned const token = *ip++; + size_t length = token >> ML_BITS; /* literal length */ - /* The second stage didn't work out, but the info is ready. - * Propel it right to the point of match copying. */ - goto _copy_match; - } + assert(!endOnInput || ip <= iend); /* ip < iend before the increment */ - /* decode literal length */ - if (length == RUN_MASK) { - unsigned s; - if (unlikely(endOnInput ? ip >= iend-RUN_MASK : 0)) goto _output_error; /* overflow detection */ - do { - s = *ip++; - length += s; - } while ( likely(endOnInput ? ip= 8) + && (dict==withPrefix64k || match >= lowPrefix) ) { + /* Copy the match. */ + memcpy(op + 0, match + 0, 8); + memcpy(op + 8, match + 8, 8); + memcpy(op +16, match +16, 2); + op += length + MINMATCH; + /* Both stages worked, load the next token. */ + continue; + } - /* copy literals */ - cpy = op+length; - LZ4_STATIC_ASSERT(MFLIMIT >= WILDCOPYLENGTH); - if ( ((endOnInput) && ((cpy>oend-MFLIMIT) || (ip+length>iend-(2+1+LASTLITERALS))) ) - || ((!endOnInput) && (cpy>oend-WILDCOPYLENGTH)) ) - { - if (partialDecoding) { - if (cpy > oend) { cpy = oend; length = oend-op; } /* Partial decoding : stop in the middle of literal segment */ - if ((endOnInput) && (ip+length > iend)) goto _output_error; /* Error : read attempt beyond end of input buffer */ - } else { - if ((!endOnInput) && (cpy != oend)) goto _output_error; /* Error : block decoding must stop exactly there */ - if ((endOnInput) && ((ip+length != iend) || (cpy > oend))) goto _output_error; /* Error : input must be consumed */ + /* The second stage didn't work out, but the info is ready. + * Propel it right to the point of match copying. */ + goto _copy_match; } - memcpy(op, ip, length); - ip += length; - op += length; - if (!partialDecoding || (cpy == oend)) { - /* Necessarily EOF, due to parsing restrictions */ - break; + + /* decode literal length */ + if (length == RUN_MASK) { + unsigned s; + if (unlikely(endOnInput ? ip >= iend-RUN_MASK : 0)) goto _output_error; /* overflow detection */ + do { + s = *ip++; + length += s; + } while ( likely(endOnInput ? ip= WILDCOPYLENGTH); + if ( ((endOnInput) && ((cpy>oend-MFLIMIT) || (ip+length>iend-(2+1+LASTLITERALS))) ) + || ((!endOnInput) && (cpy>oend-WILDCOPYLENGTH)) ) + { + if (partialDecoding) { + if (cpy > oend) { cpy = oend; length = oend-op; } /* Partial decoding : stop in the middle of literal segment */ + if ((endOnInput) && (ip+length > iend)) goto _output_error; /* Error : read attempt beyond end of input buffer */ + } else { + if ((!endOnInput) && (cpy != oend)) goto _output_error; /* Error : block decoding must stop exactly there */ + if ((endOnInput) && ((ip+length != iend) || (cpy > oend))) goto _output_error; /* Error : input must be consumed */ + } + memcpy(op, ip, length); + ip += length; + op += length; + if (!partialDecoding || (cpy == oend)) { + /* Necessarily EOF, due to parsing restrictions */ + break; + } - /* get offset */ - offset = LZ4_readLE16(ip); ip+=2; - match = op - offset; + } else { + LZ4_wildCopy(op, ip, cpy); /* may overwrite up to WILDCOPYLENGTH beyond cpy */ + ip += length; op = cpy; + } - /* get matchlength */ - length = token & ML_MASK; + /* get offset */ + offset = LZ4_readLE16(ip); ip+=2; + match = op - offset; -_copy_match: - if ((checkOffset) && (unlikely(match + dictSize < lowPrefix))) goto _output_error; /* Error : offset outside buffers */ - if (!partialDecoding) { - assert(oend > op); - assert(oend - op >= 4); - LZ4_write32(op, 0); /* silence an msan warning when offset==0; costs <1%; */ - } /* note : when partialDecoding, there is no guarantee that at least 4 bytes remain available in output buffer */ + /* get matchlength */ + length = token & ML_MASK; + + _copy_match: + if ((checkOffset) && (unlikely(match + dictSize < lowPrefix))) goto _output_error; /* Error : offset outside buffers */ + if (!partialDecoding) { + assert(oend > op); + assert(oend - op >= 4); + LZ4_write32(op, 0); /* silence an msan warning when offset==0; costs <1%; */ + } /* note : when partialDecoding, there is no guarantee that at least 4 bytes remain available in output buffer */ + + if (length == ML_MASK) { + unsigned s; + do { + s = *ip++; + if ((endOnInput) && (ip > iend-LASTLITERALS)) goto _output_error; + length += s; + } while (s==255); + if ((safeDecode) && unlikely((uptrval)(op)+length<(uptrval)op)) goto _output_error; /* overflow detection */ + } + length += MINMATCH; - if (length == ML_MASK) { - unsigned s; - do { - s = *ip++; - if ((endOnInput) && (ip > iend-LASTLITERALS)) goto _output_error; - length += s; - } while (s==255); - if ((safeDecode) && unlikely((uptrval)(op)+length<(uptrval)op)) goto _output_error; /* overflow detection */ - } - length += MINMATCH; + /* match starting within external dictionary */ + if ((dict==usingExtDict) && (match < lowPrefix)) { + if (unlikely(op+length > oend-LASTLITERALS)) { + if (partialDecoding) length = MIN(length, (size_t)(oend-op)); + else goto _output_error; /* doesn't respect parsing restriction */ + } - /* match starting within external dictionary */ - if ((dict==usingExtDict) && (match < lowPrefix)) { - if (unlikely(op+length > oend-LASTLITERALS)) { - if (partialDecoding) length = MIN(length, (size_t)(oend-op)); - else goto _output_error; /* doesn't respect parsing restriction */ + if (length <= (size_t)(lowPrefix-match)) { + /* match fits entirely within external dictionary : just copy */ + memmove(op, dictEnd - (lowPrefix-match), length); + op += length; + } else { + /* match stretches into both external dictionary and current block */ + size_t const copySize = (size_t)(lowPrefix - match); + size_t const restSize = length - copySize; + memcpy(op, dictEnd - copySize, copySize); + op += copySize; + if (restSize > (size_t)(op - lowPrefix)) { /* overlap copy */ + BYTE* const endOfMatch = op + restSize; + const BYTE* copyFrom = lowPrefix; + while (op < endOfMatch) *op++ = *copyFrom++; + } else { + memcpy(op, lowPrefix, restSize); + op += restSize; + } } + continue; } - if (length <= (size_t)(lowPrefix-match)) { - /* match fits entirely within external dictionary : just copy */ - memmove(op, dictEnd - (lowPrefix-match), length); - op += length; - } else { - /* match stretches into both external dictionary and current block */ - size_t const copySize = (size_t)(lowPrefix - match); - size_t const restSize = length - copySize; - memcpy(op, dictEnd - copySize, copySize); - op += copySize; - if (restSize > (size_t)(op - lowPrefix)) { /* overlap copy */ - BYTE* const endOfMatch = op + restSize; - const BYTE* copyFrom = lowPrefix; - while (op < endOfMatch) *op++ = *copyFrom++; + /* copy match within block */ + cpy = op + length; + + /* partialDecoding : may not respect endBlock parsing restrictions */ + assert(op<=oend); + if (partialDecoding && (cpy > oend-MATCH_SAFEGUARD_DISTANCE)) { + size_t const mlen = MIN(length, (size_t)(oend-op)); + const BYTE* const matchEnd = match + mlen; + BYTE* const copyEnd = op + mlen; + if (matchEnd > op) { /* overlap copy */ + while (op < copyEnd) *op++ = *match++; } else { - memcpy(op, lowPrefix, restSize); - op += restSize; - } } - continue; - } + memcpy(op, match, mlen); + } + op = copyEnd; + if (op==oend) break; + continue; + } - /* copy match within block */ - cpy = op + length; - - /* partialDecoding : may not respect endBlock parsing restrictions */ - assert(op<=oend); - if (partialDecoding && (cpy > oend-MATCH_SAFEGUARD_DISTANCE)) { - size_t const mlen = MIN(length, (size_t)(oend-op)); - const BYTE* const matchEnd = match + mlen; - BYTE* const copyEnd = op + mlen; - if (matchEnd > op) { /* overlap copy */ - while (op < copyEnd) *op++ = *match++; + if (unlikely(offset<8)) { + op[0] = match[0]; + op[1] = match[1]; + op[2] = match[2]; + op[3] = match[3]; + match += inc32table[offset]; + memcpy(op+4, match, 4); + match -= dec64table[offset]; } else { - memcpy(op, match, mlen); + memcpy(op, match, 8); + match += 8; } - op = copyEnd; - if (op==oend) break; - continue; - } - - if (unlikely(offset<8)) { - op[0] = match[0]; - op[1] = match[1]; - op[2] = match[2]; - op[3] = match[3]; - match += inc32table[offset]; - memcpy(op+4, match, 4); - match -= dec64table[offset]; - } else { - memcpy(op, match, 8); - match += 8; - } - op += 8; - - if (unlikely(cpy > oend-MATCH_SAFEGUARD_DISTANCE)) { - BYTE* const oCopyLimit = oend - (WILDCOPYLENGTH-1); - if (cpy > oend-LASTLITERALS) goto _output_error; /* Error : last LASTLITERALS bytes must be literals (uncompressed) */ - if (op < oCopyLimit) { - LZ4_wildCopy(op, match, oCopyLimit); - match += oCopyLimit - op; - op = oCopyLimit; + op += 8; + + if (unlikely(cpy > oend-MATCH_SAFEGUARD_DISTANCE)) { + BYTE* const oCopyLimit = oend - (WILDCOPYLENGTH-1); + if (cpy > oend-LASTLITERALS) goto _output_error; /* Error : last LASTLITERALS bytes must be literals (uncompressed) */ + if (op < oCopyLimit) { + LZ4_wildCopy(op, match, oCopyLimit); + match += oCopyLimit - op; + op = oCopyLimit; + } + while (op < cpy) *op++ = *match++; + } else { + memcpy(op, match, 8); + if (length > 16) LZ4_wildCopy(op+8, match+8, cpy); } - while (op < cpy) *op++ = *match++; - } else { - memcpy(op, match, 8); - if (length > 16) LZ4_wildCopy(op+8, match+8, cpy); + op = cpy; /* wildcopy correction */ } - op = cpy; /* wildcopy correction */ - } - /* end of decoding */ - if (endOnInput) - return (int) (((char*)op)-dst); /* Nb of output bytes decoded */ - else - return (int) (((const char*)ip)-src); /* Nb of input bytes read */ + /* end of decoding */ + if (endOnInput) + return (int) (((char*)op)-dst); /* Nb of output bytes decoded */ + else + return (int) (((const char*)ip)-src); /* Nb of input bytes read */ - /* Overflow error detected */ -_output_error: - return (int) (-(((const char*)ip)-src))-1; + /* Overflow error detected */ + _output_error: + return (int) (-(((const char*)ip)-src))-1; + } } diff --git a/lib/lz4frame.c b/lib/lz4frame.c index a30de48..11e0975 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -635,7 +635,7 @@ size_t LZ4F_compressBegin_usingCDict(LZ4F_cctx* cctxPtr, } } cctxPtr->tmpIn = cctxPtr->tmpBuff; cctxPtr->tmpInSize = 0; - XXH32_reset(&(cctxPtr->xxh), 0); + (void)XXH32_reset(&(cctxPtr->xxh), 0); /* context init */ cctxPtr->cdict = cdict; @@ -644,7 +644,7 @@ size_t LZ4F_compressBegin_usingCDict(LZ4F_cctx* cctxPtr, LZ4F_initStream(cctxPtr->lz4CtxPtr, cdict, cctxPtr->prefs.compressionLevel, LZ4F_blockLinked); } if (preferencesPtr->compressionLevel >= LZ4HC_CLEVEL_MIN) { - LZ4_favorDecompressionSpeed((LZ4_streamHC_t*)cctxPtr->lz4CtxPtr, (int)preferencesPtr->favorDecSpeed); + LZ4_favorDecompressionSpeed((LZ4_streamHC_t*)cctxPtr->lz4CtxPtr, (int)preferencesPtr->favorDecSpeed); } /* Magic Number */ @@ -887,7 +887,7 @@ size_t LZ4F_compressUpdate(LZ4F_cctx* cctxPtr, } if (cctxPtr->prefs.frameInfo.contentChecksumFlag == LZ4F_contentChecksumEnabled) - XXH32_update(&(cctxPtr->xxh), srcBuffer, srcSize); + (void)XXH32_update(&(cctxPtr->xxh), srcBuffer, srcSize); cctxPtr->totalInSize += srcSize; return dstPtr - dstStart; @@ -1366,7 +1366,7 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, break; case dstage_init: - if (dctx->frameInfo.contentChecksumFlag) XXH32_reset(&(dctx->xxh), 0); + if (dctx->frameInfo.contentChecksumFlag) (void)XXH32_reset(&(dctx->xxh), 0); /* internal buffers allocation */ { size_t const bufferNeeded = dctx->maxBlockSize + ((dctx->frameInfo.blockMode==LZ4F_blockLinked) * 128 KB); @@ -1431,7 +1431,7 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, /* next block is uncompressed */ dctx->tmpInTarget = nextCBlockSize; if (dctx->frameInfo.blockChecksumFlag) { - XXH32_reset(&dctx->blockChecksum, 0); + (void)XXH32_reset(&dctx->blockChecksum, 0); } dctx->dStage = dstage_copyDirect; break; @@ -1451,10 +1451,10 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, size_t const sizeToCopy = MIN(dctx->tmpInTarget, minBuffSize); memcpy(dstPtr, srcPtr, sizeToCopy); if (dctx->frameInfo.blockChecksumFlag) { - XXH32_update(&dctx->blockChecksum, srcPtr, sizeToCopy); + (void)XXH32_update(&dctx->blockChecksum, srcPtr, sizeToCopy); } if (dctx->frameInfo.contentChecksumFlag) - XXH32_update(&dctx->xxh, srcPtr, sizeToCopy); + (void)XXH32_update(&dctx->xxh, srcPtr, sizeToCopy); if (dctx->frameInfo.contentSize) dctx->frameRemainingSize -= sizeToCopy; diff --git a/lib/lz4hc.c b/lib/lz4hc.c index 87f4cdb..714ab62 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -231,7 +231,6 @@ LZ4HC_InsertAndGetWiderMatch ( int matchChainPos = 0; U32 const pattern = LZ4_read32(ip); U32 matchIndex; - U32 dictMatchIndex; repeat_state_e repeat = rep_untested; size_t srcPatternLength = 0; @@ -349,8 +348,8 @@ LZ4HC_InsertAndGetWiderMatch ( if (dict == usingDictCtx && nbAttempts && ipIndex - lowestMatchIndex < MAX_DISTANCE) { size_t const dictEndOffset = dictCtx->end - dictCtx->base; + U32 dictMatchIndex = dictCtx->hashTable[LZ4HC_hashPtr(ip)]; assert(dictEndOffset <= 1 GB); - dictMatchIndex = dictCtx->hashTable[LZ4HC_hashPtr(ip)]; matchIndex = dictMatchIndex + lowestMatchIndex - (U32)dictEndOffset; while (ipIndex - matchIndex <= MAX_DISTANCE && nbAttempts--) { const BYTE* const matchPtr = dictCtx->base + dictMatchIndex; -- cgit v1.2.3 From e34a27d2910c48e2d7501f5eb0eaefe91c558763 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 18 Sep 2018 16:08:17 -0700 Subject: fixed minor cppcheck warnings in programs --- programs/lz4cli.c | 2 +- programs/util.h | 48 ++++++++++++++++++++++++++++-------------------- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/programs/lz4cli.c b/programs/lz4cli.c index 26a8089..46a95cd 100644 --- a/programs/lz4cli.c +++ b/programs/lz4cli.c @@ -141,7 +141,7 @@ static int usage_advanced(const char* exeName) DISPLAY( "--content-size : compressed frame includes original size (default:not present)\n"); DISPLAY( "--[no-]sparse : sparse mode (default:enabled on file, disabled on stdout)\n"); DISPLAY( "--favor-decSpeed: compressed files decompress faster, but are less compressed \n"); - DISPLAY( "--fast[=#]: switch to ultra fast compression level (default: %u)\n", 1); + DISPLAY( "--fast[=#]: switch to ultra fast compression level (default: %i)\n", 1); DISPLAY( "Benchmark arguments : \n"); DISPLAY( " -b# : benchmark file(s), using # compression level (default : 1) \n"); DISPLAY( " -e# : test all compression levels from -bX to # (default : 1)\n"); diff --git a/programs/util.h b/programs/util.h index d74db0d..4aba6a3 100644 --- a/programs/util.h +++ b/programs/util.h @@ -34,6 +34,7 @@ extern "C" { #include /* malloc */ #include /* strlen, strncpy */ #include /* fprintf */ +#include #include /* stat, utime */ #include /* stat */ #if defined(_MSC_VER) @@ -375,9 +376,9 @@ UTIL_STATIC U64 UTIL_getTotalFileSize(const char** fileNamesTable, unsigned nbFi * A modified version of realloc(). * If UTIL_realloc() fails the original block is freed. */ -UTIL_STATIC void *UTIL_realloc(void *ptr, size_t size) +UTIL_STATIC void* UTIL_realloc(void* ptr, size_t size) { - void *newptr = realloc(ptr, size); + void* newptr = realloc(ptr, size); if (newptr) return newptr; free(ptr); return NULL; @@ -387,10 +388,10 @@ UTIL_STATIC void *UTIL_realloc(void *ptr, size_t size) #ifdef _WIN32 # define UTIL_HAS_CREATEFILELIST -UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd) +UTIL_STATIC int UTIL_prepareFileList(const char* dirName, char** bufStart, size_t* pos, char** bufEnd) { char* path; - int dirLength, fnameLength, pathLength, nbFiles = 0; + int dirLength, nbFiles = 0; WIN32_FIND_DATAA cFile; HANDLE hFile; @@ -411,7 +412,8 @@ UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_ free(path); do { - fnameLength = (int)strlen(cFile.cFileName); + int pathLength; + int const fnameLength = (int)strlen(cFile.cFileName); path = (char*) malloc(dirLength + fnameLength + 2); if (!path) { FindClose(hFile); return 0; } memcpy(path, dirName, dirLength); @@ -451,12 +453,11 @@ UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_ # include /* opendir, readdir */ # include /* strerror, memcpy */ -UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd) +UTIL_STATIC int UTIL_prepareFileList(const char* dirName, char** bufStart, size_t* pos, char** bufEnd) { - DIR *dir; - struct dirent *entry; - char* path; - int dirLength, fnameLength, pathLength, nbFiles = 0; + DIR* dir; + struct dirent * entry; + int dirLength, nbFiles = 0; if (!(dir = opendir(dirName))) { fprintf(stderr, "Cannot open directory '%s': %s\n", dirName, strerror(errno)); @@ -466,6 +467,8 @@ UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_ dirLength = (int)strlen(dirName); errno = 0; while ((entry = readdir(dir)) != NULL) { + char* path; + int fnameLength, pathLength; if (strcmp (entry->d_name, "..") == 0 || strcmp (entry->d_name, ".") == 0) continue; fnameLength = (int)strlen(entry->d_name); @@ -508,7 +511,7 @@ UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_ #else -UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd) +UTIL_STATIC int UTIL_prepareFileList(const char* dirName, char** bufStart, size_t* pos, char** bufEnd) { (void)bufStart; (void)bufEnd; (void)pos; fprintf(stderr, "Directory %s ignored (compiled without _WIN32 or _POSIX_C_SOURCE)\n", dirName); @@ -523,12 +526,13 @@ UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_ * After finishing usage of the list the structures should be freed with UTIL_freeFileList(params: return value, allocatedBuffer) * In case of error UTIL_createFileList returns NULL and UTIL_freeFileList should not be called. */ -UTIL_STATIC const char** UTIL_createFileList(const char **inputNames, unsigned inputNamesNb, char** allocatedBuffer, unsigned* allocatedNamesNb) +UTIL_STATIC const char** +UTIL_createFileList(const char** inputNames, unsigned inputNamesNb, char** allocatedBuffer, unsigned* allocatedNamesNb) { size_t pos; unsigned i, nbFiles; char* buf = (char*)malloc(LIST_SIZE_INCREASE); - char* bufend = buf + LIST_SIZE_INCREASE; + size_t bufSize = LIST_SIZE_INCREASE; const char** fileTable; if (!buf) return NULL; @@ -536,20 +540,23 @@ UTIL_STATIC const char** UTIL_createFileList(const char **inputNames, unsigned i for (i=0, pos=0, nbFiles=0; i= bufend) { - ptrdiff_t newListSize = (bufend - buf) + LIST_SIZE_INCREASE; + if (pos + len >= bufSize) { + size_t newListSize = bufSize + LIST_SIZE_INCREASE; buf = (char*)UTIL_realloc(buf, newListSize); - bufend = buf + newListSize; + bufSize = newListSize; if (!buf) return NULL; } - if (buf + pos + len < bufend) { - strncpy(buf + pos, inputNames[i], bufend - (buf + pos)); + if (pos + len < bufSize) { + strncpy(buf + pos, inputNames[i], bufSize - pos); pos += len + 1; nbFiles++; } } else { + char* bufend = buf + bufSize; nbFiles += UTIL_prepareFileList(inputNames[i], &buf, &pos, &bufend); if (buf == NULL) return NULL; + assert(bufend > buf); + bufSize = bufend - buf; } } if (nbFiles == 0) { free(buf); return NULL; } @@ -562,7 +569,7 @@ UTIL_STATIC const char** UTIL_createFileList(const char **inputNames, unsigned i pos += strlen(fileTable[i]) + 1; } - if (buf + pos > bufend) { free(buf); free((void*)fileTable); return NULL; } + if (pos > bufSize) { free(buf); free((void*)fileTable); return NULL; } /* can this happen ? */ *allocatedBuffer = buf; *allocatedNamesNb = nbFiles; @@ -571,7 +578,8 @@ UTIL_STATIC const char** UTIL_createFileList(const char **inputNames, unsigned i } -UTIL_STATIC void UTIL_freeFileList(const char** filenameTable, char* allocatedBuffer) +UTIL_STATIC void +UTIL_freeFileList(const char** filenameTable, char* allocatedBuffer) { if (allocatedBuffer) free(allocatedBuffer); if (filenameTable) free((void*)filenameTable); -- cgit v1.2.3 From 697bd904b256e9927b65d23e187adb5dddf39399 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 18 Sep 2018 16:30:31 -0700 Subject: fixed minor cppcheck warnings in examples --- contrib/gen_manual/gen_manual.cpp | 30 ++++++++++++++++-------------- examples/blockStreaming_lineByLine.c | 2 +- examples/compress_functions.c | 25 ++++++++++++------------- examples/simple_buffer.c | 5 ++--- 4 files changed, 31 insertions(+), 31 deletions(-) diff --git a/contrib/gen_manual/gen_manual.cpp b/contrib/gen_manual/gen_manual.cpp index 65abd3a..e12273f 100644 --- a/contrib/gen_manual/gen_manual.cpp +++ b/contrib/gen_manual/gen_manual.cpp @@ -44,7 +44,7 @@ void trim(string& s, string characters) { size_t p = s.find_first_not_of(characters); s.erase(0, p); - + p = s.find_last_not_of(characters); if (string::npos != p) s.erase(p+1); @@ -67,14 +67,13 @@ vector get_lines(vector& input, int& linenum, string terminator) { vector out; string line; - size_t epos; while ((size_t)linenum < input.size()) { line = input[linenum]; if (terminator.empty() && line.empty()) { linenum--; break; } - - epos = line.find(terminator); + + size_t const epos = line.find(terminator); if (!terminator.empty() && epos!=string::npos) { out.push_back(line); break; @@ -152,8 +151,9 @@ int main(int argc, char *argv[]) { continue; } - /* comments of type /**< and /*!< are detected and only function declaration is highlighted (bold) */ - if ((line.find("/**<")!=string::npos || line.find("/*!<")!=string::npos) && line.find("*/")!=string::npos) { + /* comments of type / * * < and / * ! < are detected, and only function declaration is highlighted (bold) */ + if ((line.find("/**<")!=string::npos || line.find("/*!<")!=string::npos) + && line.find("*/")!=string::npos) { sout << "
";
             print_line(sout, line);
             sout << "

" << endl; @@ -177,16 +177,19 @@ int main(int argc, char *argv[]) { comments = get_lines(input, linenum, "*/"); if (!comments.empty()) comments[0] = line.substr(spos+3); - if (!comments.empty()) comments[comments.size()-1] = comments[comments.size()-1].substr(0, comments[comments.size()-1].find("*/")); + if (!comments.empty()) + comments[comments.size()-1] = comments[comments.size()-1].substr(0, comments[comments.size()-1].find("*/")); for (l=0; l"; for (l=0; l

"; @@ -202,7 +204,7 @@ int main(int argc, char *argv[]) { print_line(sout, comments[l]); } sout << "


" << endl << endl; - } else if (exclam == '=') { /* comments of type /*= and /**= mean: use a

header and show also all functions until first empty line */ + } else if (exclam == '=') { /* comments of type / * = and / * * = mean: use a

header and show also all functions until first empty line */ trim(comments[0], " "); sout << "

" << comments[0] << "

";
             for (l=1; l
" << endl; - } else { /* comments of type /** and /*- mean: this is a comment; use a

header for the first line */ + } else { /* comments of type / * * and / * - mean: this is a comment; use a

header for the first line */ if (comments.empty()) continue; trim(comments[0], " "); @@ -244,4 +246,4 @@ int main(int argc, char *argv[]) { ostream << "" << endl << "" << endl; return 0; -} \ No newline at end of file +} diff --git a/examples/blockStreaming_lineByLine.c b/examples/blockStreaming_lineByLine.c index 677c426..19c3345 100644 --- a/examples/blockStreaming_lineByLine.c +++ b/examples/blockStreaming_lineByLine.c @@ -99,7 +99,7 @@ static void test_decompress( uint16_t cmpBytes = 0; if (read_uint16(inpFp, &cmpBytes) != 1) break; - if (cmpBytes <= 0) break; + if (cmpBytes == 0) break; if (read_bin(inpFp, cmpBuf, cmpBytes) != cmpBytes) break; { diff --git a/examples/compress_functions.c b/examples/compress_functions.c index d0dca13..41d3c8c 100644 --- a/examples/compress_functions.c +++ b/examples/compress_functions.c @@ -88,7 +88,6 @@ void run_screaming(const char *message, const int code) { printf("%s\n", message); exit(code); - return; } @@ -343,19 +342,19 @@ int main(int argc, char **argv) { printf("%s", separator); printf(header_format, "Source", "Function Benchmarked", "Total Seconds", "Iterations/sec", "ns/Iteration", "% of default"); printf("%s", separator); - printf(format, "Normal Text", "LZ4_compress_default()", (double)time_taken__default / BILLION, (int)(iterations / ((double)time_taken__default /BILLION)), time_taken__default / iterations, (double)time_taken__default * 100 / time_taken__default); - printf(format, "Normal Text", "LZ4_compress_fast()", (double)time_taken__fast / BILLION, (int)(iterations / ((double)time_taken__fast /BILLION)), time_taken__fast / iterations, (double)time_taken__fast * 100 / time_taken__default); - printf(format, "Normal Text", "LZ4_compress_fast_extState()", (double)time_taken__fast_extstate / BILLION, (int)(iterations / ((double)time_taken__fast_extstate /BILLION)), time_taken__fast_extstate / iterations, (double)time_taken__fast_extstate * 100 / time_taken__default); - //printf(format, "Normal Text", "LZ4_compress_generic()", (double)time_taken__generic / BILLION, (int)(iterations / ((double)time_taken__generic /BILLION)), time_taken__generic / iterations, (double)time_taken__generic * 100 / time_taken__default); - printf(format, "Normal Text", "LZ4_decompress_safe()", (double)time_taken__decomp_safe / BILLION, (int)(iterations / ((double)time_taken__decomp_safe /BILLION)), time_taken__decomp_safe / iterations, (double)time_taken__decomp_safe * 100 / time_taken__default); - printf(format, "Normal Text", "LZ4_decompress_fast()", (double)time_taken__decomp_fast / BILLION, (int)(iterations / ((double)time_taken__decomp_fast /BILLION)), time_taken__decomp_fast / iterations, (double)time_taken__decomp_fast * 100 / time_taken__default); + printf(format, "Normal Text", "LZ4_compress_default()", (double)time_taken__default / BILLION, (int)(iterations / ((double)time_taken__default /BILLION)), (int)time_taken__default / iterations, (double)time_taken__default * 100 / time_taken__default); + printf(format, "Normal Text", "LZ4_compress_fast()", (double)time_taken__fast / BILLION, (int)(iterations / ((double)time_taken__fast /BILLION)), (int)time_taken__fast / iterations, (double)time_taken__fast * 100 / time_taken__default); + printf(format, "Normal Text", "LZ4_compress_fast_extState()", (double)time_taken__fast_extstate / BILLION, (int)(iterations / ((double)time_taken__fast_extstate /BILLION)), (int)time_taken__fast_extstate / iterations, (double)time_taken__fast_extstate * 100 / time_taken__default); + //printf(format, "Normal Text", "LZ4_compress_generic()", (double)time_taken__generic / BILLION, (int)(iterations / ((double)time_taken__generic /BILLION)), (int)time_taken__generic / iterations, (double)time_taken__generic * 100 / time_taken__default); + printf(format, "Normal Text", "LZ4_decompress_safe()", (double)time_taken__decomp_safe / BILLION, (int)(iterations / ((double)time_taken__decomp_safe /BILLION)), (int)time_taken__decomp_safe / iterations, (double)time_taken__decomp_safe * 100 / time_taken__default); + printf(format, "Normal Text", "LZ4_decompress_fast()", (double)time_taken__decomp_fast / BILLION, (int)(iterations / ((double)time_taken__decomp_fast /BILLION)), (int)time_taken__decomp_fast / iterations, (double)time_taken__decomp_fast * 100 / time_taken__default); printf(header_format, "", "", "", "", "", ""); - printf(format, "Compressible", "LZ4_compress_default()", (double)time_taken_hc__default / BILLION, (int)(iterations / ((double)time_taken_hc__default /BILLION)), time_taken_hc__default / iterations, (double)time_taken_hc__default * 100 / time_taken_hc__default); - printf(format, "Compressible", "LZ4_compress_fast()", (double)time_taken_hc__fast / BILLION, (int)(iterations / ((double)time_taken_hc__fast /BILLION)), time_taken_hc__fast / iterations, (double)time_taken_hc__fast * 100 / time_taken_hc__default); - printf(format, "Compressible", "LZ4_compress_fast_extState()", (double)time_taken_hc__fast_extstate / BILLION, (int)(iterations / ((double)time_taken_hc__fast_extstate /BILLION)), time_taken_hc__fast_extstate / iterations, (double)time_taken_hc__fast_extstate * 100 / time_taken_hc__default); - //printf(format, "Compressible", "LZ4_compress_generic()", (double)time_taken_hc__generic / BILLION, (int)(iterations / ((double)time_taken_hc__generic /BILLION)), time_taken_hc__generic / iterations, (double)time_taken_hc__generic * 100 / time_taken_hc__default); - printf(format, "Compressible", "LZ4_decompress_safe()", (double)time_taken_hc__decomp_safe / BILLION, (int)(iterations / ((double)time_taken_hc__decomp_safe /BILLION)), time_taken_hc__decomp_safe / iterations, (double)time_taken_hc__decomp_safe * 100 / time_taken_hc__default); - printf(format, "Compressible", "LZ4_decompress_fast()", (double)time_taken_hc__decomp_fast / BILLION, (int)(iterations / ((double)time_taken_hc__decomp_fast /BILLION)), time_taken_hc__decomp_fast / iterations, (double)time_taken_hc__decomp_fast * 100 / time_taken_hc__default); + printf(format, "Compressible", "LZ4_compress_default()", (double)time_taken_hc__default / BILLION, (int)(iterations / ((double)time_taken_hc__default /BILLION)), (int)time_taken_hc__default / iterations, (double)time_taken_hc__default * 100 / time_taken_hc__default); + printf(format, "Compressible", "LZ4_compress_fast()", (double)time_taken_hc__fast / BILLION, (int)(iterations / ((double)time_taken_hc__fast /BILLION)), (int)time_taken_hc__fast / iterations, (double)time_taken_hc__fast * 100 / time_taken_hc__default); + printf(format, "Compressible", "LZ4_compress_fast_extState()", (double)time_taken_hc__fast_extstate / BILLION, (int)(iterations / ((double)time_taken_hc__fast_extstate /BILLION)), (int)time_taken_hc__fast_extstate / iterations, (double)time_taken_hc__fast_extstate * 100 / time_taken_hc__default); + //printf(format, "Compressible", "LZ4_compress_generic()", (double)time_taken_hc__generic / BILLION, (int)(iterations / ((double)time_taken_hc__generic /BILLION)), (int)time_taken_hc__generic / iterations, (double)time_taken_hc__generic * 100 / time_taken_hc__default); + printf(format, "Compressible", "LZ4_decompress_safe()", (double)time_taken_hc__decomp_safe / BILLION, (int)(iterations / ((double)time_taken_hc__decomp_safe /BILLION)), (int)time_taken_hc__decomp_safe / iterations, (double)time_taken_hc__decomp_safe * 100 / time_taken_hc__default); + printf(format, "Compressible", "LZ4_decompress_fast()", (double)time_taken_hc__decomp_fast / BILLION, (int)(iterations / ((double)time_taken_hc__decomp_fast /BILLION)), (int)time_taken_hc__decomp_fast / iterations, (double)time_taken_hc__decomp_fast * 100 / time_taken_hc__default); printf("%s", separator); printf("\n"); printf("All done, ran %d iterations per test.\n", iterations); diff --git a/examples/simple_buffer.c b/examples/simple_buffer.c index 403d9e8..54e542a 100644 --- a/examples/simple_buffer.c +++ b/examples/simple_buffer.c @@ -16,10 +16,9 @@ /* * Easy show-error-and-bail function. */ -void run_screaming(const char *message, const int code) { - printf("%s\n", message); +void run_screaming(const char* message, const int code) { + printf("%s \n", message); exit(code); - return; } -- cgit v1.2.3 From 5a10ed2f7bb6fc9bffbabb2035ff1515e21bf25c Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 18 Sep 2018 16:52:02 -0700 Subject: added cppcheck as Makefile target and Travis CI test. Fixed last cppcheck warnings in tests and examples --- .travis.yml | 3 ++- Makefile | 5 +++++ contrib/gen_manual/gen_manual.cpp | 4 ++-- tests/frametest.c | 4 ++-- tests/fullbench.c | 4 ++-- 5 files changed, 13 insertions(+), 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index de6875b..5b0c6ca 100644 --- a/.travis.yml +++ b/.travis.yml @@ -129,13 +129,14 @@ matrix: - qemu-user-static - gcc-powerpc-linux-gnu - - env: Ubu=14.04 Cmd='make staticAnalyze' COMPILER=clang + - env: Ubu=14.04 Cmd='make staticAnalyze && make cppcheck' COMPILER=clang dist: trusty sudo: required addons: apt: packages: - clang + - cppcheck - env: Ubu=14.04 Cmd='make clean all CC=gcc-4.4 MOREFLAGS=-Werror && make clean && CFLAGS=-fPIC LDFLAGS="-pie -fPIE -D_FORTIFY_SOURCE=2" make -C programs' COMPILER=gcc-4.4 dist: trusty diff --git a/Makefile b/Makefile index e8e6473..2f8b85c 100644 --- a/Makefile +++ b/Makefile @@ -148,9 +148,14 @@ usan: clean usan32: clean CFLAGS="-m32 -O3 -g -fsanitize=undefined" $(MAKE) test FUZZER_TIME="-T30s" NB_LOOPS=-i1 +.PHONY: staticAnalyze staticAnalyze: clean CFLAGS=-g scan-build --status-bugs -v $(MAKE) all +.PHONY: cppcheck +cppcheck: + cppcheck . --force --enable=warning,portability,performance,style --error-exitcode=1 > /dev/null + platformTest: clean @echo "\n ---- test lz4 with $(CC) compiler ----" @$(CC) -v diff --git a/contrib/gen_manual/gen_manual.cpp b/contrib/gen_manual/gen_manual.cpp index e12273f..bedef94 100644 --- a/contrib/gen_manual/gen_manual.cpp +++ b/contrib/gen_manual/gen_manual.cpp @@ -180,9 +180,9 @@ int main(int argc, char *argv[]) { if (!comments.empty()) comments[comments.size()-1] = comments[comments.size()-1].substr(0, comments[comments.size()-1].find("*/")); for (l=0; l Date: Tue, 18 Sep 2018 18:08:59 -0700 Subject: tried to clean another bunch of cppcheck warnings so "funny" thing with cppcheck is that no 2 versions give the same list of warnings. On Mac, I'm using v1.81, which had all warnings fixed. On Travis CI, it's v1.61, and it complains about a dozen more/different things. On Linux, it's v1.72, and it finds a completely different list of a half dozen warnings. Some of these seems to be bugs/limitations in cppcheck itself. The TravisCI version v1.61 seems unable to understand %zu correctly, and seems to assume it means %u. --- examples/frameCompress.c | 13 +++++++------ lib/lz4.c | 9 +++++---- programs/lz4io.c | 40 ++++++++++++++++++++++++---------------- tests/frametest.c | 11 ++++++----- 4 files changed, 42 insertions(+), 31 deletions(-) diff --git a/examples/frameCompress.c b/examples/frameCompress.c index 2cc4649..a189329 100644 --- a/examples/frameCompress.c +++ b/examples/frameCompress.c @@ -70,11 +70,12 @@ compress_file_internal(FILE* f_in, FILE* f_out, /* write frame header */ { size_t const headerSize = LZ4F_compressBegin(ctx, outBuff, outCapacity, &kPrefs); if (LZ4F_isError(headerSize)) { - printf("Failed to start compression: error %zu\n", headerSize); + printf("Failed to start compression: error %u \n", (unsigned)headerSize); return result; } count_out = headerSize; - printf("Buffer size is %zu bytes, header size %zu bytes\n", outCapacity, headerSize); + printf("Buffer size is %u bytes, header size %u bytes \n", + (unsigned)outCapacity, (unsigned)headerSize); safe_fwrite(outBuff, 1, headerSize, f_out); } @@ -89,11 +90,11 @@ compress_file_internal(FILE* f_in, FILE* f_out, inBuff, readSize, NULL); if (LZ4F_isError(compressedSize)) { - printf("Compression failed: error %zu\n", compressedSize); + printf("Compression failed: error %u \n", (unsigned)compressedSize); return result; } - printf("Writing %zu bytes\n", compressedSize); + printf("Writing %u bytes\n", (unsigned)compressedSize); safe_fwrite(outBuff, 1, compressedSize, f_out); count_out += compressedSize; } @@ -103,11 +104,11 @@ compress_file_internal(FILE* f_in, FILE* f_out, outBuff, outCapacity, NULL); if (LZ4F_isError(compressedSize)) { - printf("Failed to end compression: error %zu\n", compressedSize); + printf("Failed to end compression: error %u \n", (unsigned)compressedSize); return result; } - printf("Writing %zu bytes\n", compressedSize); + printf("Writing %u bytes \n", (unsigned)compressedSize); safe_fwrite(outBuff, 1, compressedSize, f_out); count_out += compressedSize; } diff --git a/lib/lz4.c b/lib/lz4.c index df7a2c7..3c7467a 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -522,13 +522,14 @@ static U32 LZ4_hash4(U32 sequence, tableType_t const tableType) static U32 LZ4_hash5(U64 sequence, tableType_t const tableType) { - static const U64 prime5bytes = 889523592379ULL; - static const U64 prime8bytes = 11400714785074694791ULL; const U32 hashLog = (tableType == byU16) ? LZ4_HASHLOG+1 : LZ4_HASHLOG; - if (LZ4_isLittleEndian()) + if (LZ4_isLittleEndian()) { + const U64 prime5bytes = 889523592379ULL; return (U32)(((sequence << 24) * prime5bytes) >> (64 - hashLog)); - else + } else { + const U64 prime8bytes = 11400714785074694791ULL; return (U32)(((sequence >> 24) * prime8bytes) >> (64 - hashLog)); + } } LZ4_FORCE_INLINE U32 LZ4_hashPosition(const void* const p, tableType_t const tableType) diff --git a/programs/lz4io.c b/programs/lz4io.c index 28d6537..cdc4c27 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -331,42 +331,48 @@ static int LZ4IO_LZ4_compress(const char* src, char* dst, int srcSize, int dstSi * It generates compressed streams using the old 'legacy' format */ int LZ4IO_compressFilename_Legacy(const char* input_filename, const char* output_filename, int compressionlevel) { - int (*compressionFunction)(const char* src, char* dst, int srcSize, int dstSize, int cLevel); + typedef int (*compress_f)(const char* src, char* dst, int srcSize, int dstSize, int cLevel); + compress_f const compressionFunction = (compressionlevel < 3) ? LZ4IO_LZ4_compress : LZ4_compress_HC; unsigned long long filesize = 0; unsigned long long compressedfilesize = MAGICNUMBER_SIZE; char* in_buff; char* out_buff; const int outBuffSize = LZ4_compressBound(LEGACY_BLOCKSIZE); - FILE* finput; + FILE* const finput = LZ4IO_openSrcFile(input_filename); FILE* foutput; clock_t clockEnd; /* Init */ clock_t const clockStart = clock(); - compressionFunction = (compressionlevel < 3) ? LZ4IO_LZ4_compress : LZ4_compress_HC; + if (finput == NULL) + EXM_THROW(20, "%s : open file error ", input_filename); - finput = LZ4IO_openSrcFile(input_filename); - if (finput == NULL) EXM_THROW(20, "%s : open file error ", input_filename); foutput = LZ4IO_openDstFile(output_filename); - if (foutput == NULL) { fclose(finput); EXM_THROW(20, "%s : open file error ", input_filename); } + if (foutput == NULL) { + fclose(finput); + EXM_THROW(20, "%s : open file error ", input_filename); + } /* Allocate Memory */ in_buff = (char*)malloc(LEGACY_BLOCKSIZE); - out_buff = (char*)malloc(outBuffSize); - if (!in_buff || !out_buff) EXM_THROW(21, "Allocation error : not enough memory"); + out_buff = (char*)malloc(outBuffSize + 4); + if (!in_buff || !out_buff) + EXM_THROW(21, "Allocation error : not enough memory"); /* Write Archive Header */ LZ4IO_writeLE32(out_buff, LEGACY_MAGICNUMBER); - { size_t const sizeCheck = fwrite(out_buff, 1, MAGICNUMBER_SIZE, foutput); - if (sizeCheck != MAGICNUMBER_SIZE) EXM_THROW(22, "Write error : cannot write header"); } + { size_t const writeSize = fwrite(out_buff, 1, MAGICNUMBER_SIZE, foutput); + if (writeSize != MAGICNUMBER_SIZE) + EXM_THROW(22, "Write error : cannot write header"); + } /* Main Loop */ while (1) { - unsigned int outSize; + int outSize; /* Read Block */ - size_t const inSize = (int) fread(in_buff, (size_t)1, (size_t)LEGACY_BLOCKSIZE, finput); + size_t const inSize = fread(in_buff, (size_t)1, (size_t)LEGACY_BLOCKSIZE, finput); + assert(inSize <= LEGACY_BLOCKSIZE); if (inSize == 0) break; - if (inSize > LEGACY_BLOCKSIZE) EXM_THROW(23, "Read error : wrong fread() size report "); /* should be impossible */ filesize += inSize; /* Compress Block */ @@ -376,9 +382,11 @@ int LZ4IO_compressFilename_Legacy(const char* input_filename, const char* output (int)(filesize>>20), (double)compressedfilesize/filesize*100); /* Write Block */ - LZ4IO_writeLE32(out_buff, outSize); - { size_t const sizeCheck = fwrite(out_buff, 1, outSize+4, foutput); - if (sizeCheck!=(size_t)(outSize+4)) + assert(outSize > 0); + assert(outSize < outBuffSize); + LZ4IO_writeLE32(out_buff, (unsigned)outSize); + { size_t const writeSize = fwrite(out_buff, 1, outSize+4, foutput); + if (writeSize != (size_t)(outSize+4)) EXM_THROW(24, "Write error : cannot write compressed block"); } } if (ferror(finput)) EXM_THROW(25, "Error while reading %s ", input_filename); diff --git a/tests/frametest.c b/tests/frametest.c index 5714732..f8498b7 100644 --- a/tests/frametest.c +++ b/tests/frametest.c @@ -343,9 +343,10 @@ int basicTests(U32 seed, double compressibility) op += oSize; ip += iSize; } - { U64 const crcDest = XXH64(decodedBuffer, COMPRESSIBLE_NOISE_LENGTH, 1); - if (crcDest != crcOrig) goto _output_error; } - DISPLAYLEVEL(3, "Regenerated %u/%u bytes \n", (unsigned)(op-ostart), COMPRESSIBLE_NOISE_LENGTH); + { U64 const crcDest = XXH64(decodedBuffer, COMPRESSIBLE_NOISE_LENGTH, 1); + if (crcDest != crcOrig) goto _output_error; + } + DISPLAYLEVEL(3, "Regenerated %u/%u bytes \n", (unsigned)(op-ostart), (unsigned)COMPRESSIBLE_NOISE_LENGTH); } } @@ -840,8 +841,8 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi size_t const iSize = MIN(sampleMax, (size_t)(iend-ip)); size_t const oSize = LZ4F_compressBound(iSize, prefsPtr); cOptions.stableSrc = ((FUZ_rand(&randState) & 3) == 1); - DISPLAYLEVEL(6, "Sending %zu bytes to compress (stableSrc:%u) \n", - iSize, cOptions.stableSrc); + DISPLAYLEVEL(6, "Sending %u bytes to compress (stableSrc:%u) \n", + (unsigned)iSize, cOptions.stableSrc); result = LZ4F_compressUpdate(cCtx, op, oSize, ip, iSize, &cOptions); CHECK(LZ4F_isError(result), "Compression failed (error %i : %s)", (int)result, LZ4F_getErrorName(result)); -- cgit v1.2.3 From c4c19c74b8c7208bff29048b1a0d35b06accd8ac Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 25 Sep 2018 14:43:19 -0700 Subject: changed LZ4_streamDecode member order to reduce memory usage on 128-bits systems --- doc/lz4_manual.html | 20 ++++++++++---------- lib/lz4.h | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/doc/lz4_manual.html b/doc/lz4_manual.html index 39a48f3..5e04263 100644 --- a/doc/lz4_manual.html +++ b/doc/lz4_manual.html @@ -16,7 +16,7 @@
  • Streaming Compression Functions
  • Streaming Decompression Functions
  • Unstable declarations
  • -
  • Private definitions
  • +
  • PRIVATE DEFINITIONS
  • Obsolete Functions

  • @@ -375,7 +375,7 @@ int LZ4_decompress_fast_usingDict (const char* src, char* dst, int originalSize,


    -

    Private definitions

    +

    PRIVATE DEFINITIONS

      Do not use these definitions directly.
      They are only exposed to allow static allocation of `LZ4_stream_t` and `LZ4_streamDecode_t`.
      Accessing members will expose code to API and/or ABI break in future versions of the library.
    @@ -390,12 +390,12 @@ int LZ4_decompress_fast_usingDict (const char* src, char* dst, int originalSize,
     

    typedef struct {
         const unsigned char* externalDict;
    -    size_t extDictSize;
         const unsigned char* prefixEnd;
    +    size_t extDictSize;
         size_t prefixSize;
     } LZ4_streamDecode_t_internal;
     

    -
    #define LZ4_STREAMSIZE_U64 ((1 << (LZ4_MEMORY_USAGE-3)) + 4)
    +
    #define LZ4_STREAMSIZE_U64 ((1 << (LZ4_MEMORY_USAGE-3)) + 4 + ((sizeof(void*)==16) ? 4 : 0) /*AS-400*/ )
     #define LZ4_STREAMSIZE     (LZ4_STREAMSIZE_U64 * sizeof(unsigned long long))
     union LZ4_stream_u {
         unsigned long long table[LZ4_STREAMSIZE_U64];
    @@ -409,7 +409,7 @@ union LZ4_stream_u {
      
     


    -
    #define LZ4_STREAMDECODESIZE_U64  4
    +
    #define LZ4_STREAMDECODESIZE_U64 (4 + ((sizeof(void*)==16) ? 2 : 0) /*AS-400*/ )
     #define LZ4_STREAMDECODESIZE     (LZ4_STREAMDECODESIZE_U64 * sizeof(unsigned long long))
     union LZ4_streamDecode_u {
         unsigned long long table[LZ4_STREAMDECODESIZE_U64];
    @@ -442,11 +442,11 @@ union LZ4_streamDecode_u {
     #    define LZ4_DEPRECATED(message)
     #  endif
     #endif /* LZ4_DISABLE_DEPRECATE_WARNINGS */
    -

    Should deprecation warnings be a problem, - it is generally possible to disable them, - typically with -Wno-deprecated-declarations for gcc - or _CRT_SECURE_NO_WARNINGS in Visual. - Otherwise, it's also possible to define LZ4_DISABLE_DEPRECATE_WARNINGS +

    Should deprecation warnings be a problem, + it is generally possible to disable them, + typically with -Wno-deprecated-declarations for gcc + or _CRT_SECURE_NO_WARNINGS in Visual. + Otherwise, it's also possible to define LZ4_DISABLE_DEPRECATE_WARNINGS


    diff --git a/lib/lz4.h b/lib/lz4.h index 639797a..4d30857 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -535,8 +535,8 @@ struct LZ4_stream_t_internal { typedef struct { const unsigned char* externalDict; - size_t extDictSize; const unsigned char* prefixEnd; + size_t extDictSize; size_t prefixSize; } LZ4_streamDecode_t_internal; -- cgit v1.2.3 From 64ad395d27ad4f935cbcf9a5ed21699fbf1ce89c Mon Sep 17 00:00:00 2001 From: Blaise Sanouillet <43636048+blezsan@users.noreply.github.com> Date: Fri, 28 Sep 2018 08:02:49 -0700 Subject: support custom block sizes --- programs/lz4cli.c | 14 +- programs/lz4io.c | 25 +++- programs/lz4io.h | 4 + tests/Makefile | 8 +- tests/checkFrame.c | 269 +++++++++++++++++++++++++++++++++++++++ tests/test_custom_block_sizes.sh | 72 +++++++++++ 6 files changed, 380 insertions(+), 12 deletions(-) create mode 100644 tests/checkFrame.c create mode 100755 tests/test_custom_block_sizes.sh diff --git a/programs/lz4cli.c b/programs/lz4cli.c index 46a95cd..759f336 100644 --- a/programs/lz4cli.c +++ b/programs/lz4cli.c @@ -134,7 +134,8 @@ static int usage_advanced(const char* exeName) DISPLAY( " -r : operate recursively on directories (sets also -m) \n"); #endif DISPLAY( " -l : compress using Legacy format (Linux kernel compression)\n"); - DISPLAY( " -B# : Block size [4-7] (default : 7) \n"); + DISPLAY( " -B# : cut file into independent blocks of size # bytes [32+] \n"); + DISPLAY( " or predefined block size [4-7] (default: 7) \n"); DISPLAY( " -BD : Block dependency (improve compression ratio) \n"); DISPLAY( " -BX : enable block checksum (default:disabled) \n"); DISPLAY( "--no-frame-crc : disable stream checksum (default:enabled) \n"); @@ -146,8 +147,6 @@ static int usage_advanced(const char* exeName) DISPLAY( " -b# : benchmark file(s), using # compression level (default : 1) \n"); DISPLAY( " -e# : test all compression levels from -bX to # (default : 1)\n"); DISPLAY( " -i# : minimum evaluation time in seconds (default : 3s) \n"); - DISPLAY( " -B# : cut file into independent blocks of size # bytes [32+] \n"); - DISPLAY( " or predefined block size [4-7] (default: 7) \n"); if (g_lz4c_legacy_commands) { DISPLAY( "Legacy arguments : \n"); DISPLAY( " -c0 : fast compression \n"); @@ -497,11 +496,12 @@ int main(int argc, const char** argv) DISPLAYLEVEL(2, "using blocks of size %u KB \n", (U32)(blockSize>>10)); } else { if (B < 32) badusage(exeName); - BMK_setBlockSize(B); - if (B >= 1024) { - DISPLAYLEVEL(2, "bench: using blocks of size %u KB \n", (U32)(B>>10)); + blockSize = LZ4IO_setBlockSize(B); + BMK_setBlockSize(blockSize); + if (blockSize >= 1024) { + DISPLAYLEVEL(2, "using blocks of size %u KB \n", (U32)(blockSize>>10)); } else { - DISPLAYLEVEL(2, "bench: using blocks of size %u bytes \n", (U32)(B)); + DISPLAYLEVEL(2, "using blocks of size %u bytes \n", (U32)(blockSize)); } } break; diff --git a/programs/lz4io.c b/programs/lz4io.c index cdc4c27..17ff9a1 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -110,6 +110,7 @@ static clock_t g_time = 0; static int g_overwrite = 1; static int g_testMode = 0; static int g_blockSizeId = LZ4IO_BLOCKSIZEID_DEFAULT; +static int g_blockSize = 0; static int g_blockChecksum = 0; static int g_streamChecksum = 1; static int g_blockIndependence = 1; @@ -178,7 +179,25 @@ size_t LZ4IO_setBlockSizeID(unsigned bsid) static const unsigned maxBlockSizeID = 7; if ((bsid < minBlockSizeID) || (bsid > maxBlockSizeID)) return 0; g_blockSizeId = bsid; - return blockSizeTable[g_blockSizeId-minBlockSizeID]; + g_blockSize = blockSizeTable[g_blockSizeId-minBlockSizeID]; + return g_blockSize; +} + +size_t LZ4IO_setBlockSize(size_t blockSize) +{ + static const size_t minBlockSize = 32; + static const size_t maxBlockSize = 4 MB; + unsigned bsid = 0; + if (blockSize < minBlockSize) blockSize = minBlockSize; + if (blockSize > maxBlockSize) blockSize = maxBlockSize; + g_blockSize = blockSize; + blockSize--; + /* find which of { 64k, 256k, 1MB, 4MB } is closest to blockSize */ + while (blockSize >>= 2) + bsid++; + if (bsid < 7) bsid = 7; + g_blockSizeId = bsid-3; + return g_blockSize; } int LZ4IO_setBlockMode(LZ4IO_blockMode_t blockMode) @@ -499,7 +518,7 @@ static LZ4F_CDict* LZ4IO_createCDict(void) { static cRess_t LZ4IO_createCResources(void) { - const size_t blockSize = (size_t)LZ4IO_GetBlockSize_FromBlockId (g_blockSizeId); + const size_t blockSize = g_blockSize; cRess_t ress; LZ4F_errorCode_t const errorCode = LZ4F_createCompressionContext(&(ress.ctx), LZ4F_VERSION); @@ -543,7 +562,7 @@ static int LZ4IO_compressFilename_extRess(cRess_t ress, const char* srcFileName, void* const srcBuffer = ress.srcBuffer; void* const dstBuffer = ress.dstBuffer; const size_t dstBufferSize = ress.dstBufferSize; - const size_t blockSize = (size_t)LZ4IO_GetBlockSize_FromBlockId (g_blockSizeId); + const size_t blockSize = g_blockSize; size_t readSize; LZ4F_compressionContext_t ctx = ress.ctx; /* just a pointer */ LZ4F_preferences_t prefs; diff --git a/programs/lz4io.h b/programs/lz4io.h index 22c5e3e..33de41f 100644 --- a/programs/lz4io.h +++ b/programs/lz4io.h @@ -78,6 +78,10 @@ int LZ4IO_setTestMode(int yes); return : 0 if error, blockSize if OK */ size_t LZ4IO_setBlockSizeID(unsigned blockSizeID); +/* blockSize : valid values : 32 -> 4MB + return : 0 if error, actual blocksize if OK */ +size_t LZ4IO_setBlockSize(size_t blockSize); + /* Default setting : independent blocks */ typedef enum { LZ4IO_blockLinked=0, LZ4IO_blockIndependent} LZ4IO_blockMode_t; int LZ4IO_setBlockMode(LZ4IO_blockMode_t blockMode); diff --git a/tests/Makefile b/tests/Makefile index 3de111b..989ef85 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -63,7 +63,7 @@ NB_LOOPS ?= -i1 default: all -all: fullbench fuzzer frametest roundTripTest datagen +all: fullbench fuzzer frametest roundTripTest datagen checkFrame all32: CFLAGS+=-m32 all32: all @@ -109,6 +109,9 @@ roundTripTest : lz4.o lz4hc.o xxhash.o roundTripTest.c datagen : $(PRGDIR)/datagen.c datagencli.c $(CC) $(FLAGS) -I$(PRGDIR) $^ -o $@$(EXT) +checkFrame : lz4frame.o lz4.o lz4hc.o xxhash.o checkFrame.c + $(CC) $(FLAGS) $^ -o $@$(EXT) + clean: @$(MAKE) -C $(LZ4DIR) $@ > $(VOID) @$(MAKE) -C $(PRGDIR) $@ > $(VOID) @@ -118,7 +121,8 @@ clean: fuzzer$(EXT) fuzzer32$(EXT) \ frametest$(EXT) frametest32$(EXT) \ fasttest$(EXT) roundTripTest$(EXT) \ - datagen$(EXT) checkTag$(EXT) + datagen$(EXT) checkTag$(EXT) \ + frameTest$(EXT) @rm -fR $(TESTDIR) @echo Cleaning completed diff --git a/tests/checkFrame.c b/tests/checkFrame.c new file mode 100644 index 0000000..ace51c7 --- /dev/null +++ b/tests/checkFrame.c @@ -0,0 +1,269 @@ +/* + checkFrame - verify frame headers + Copyright (C) Yann Collet 2014-2016 + + 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 homepage : http://www.lz4.org + - LZ4 source repository : https://github.com/lz4/lz4 +*/ + +/*-************************************ +* Compiler specific +**************************************/ +#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 "util.h" /* U32 */ +#include /* malloc, free */ +#include /* fprintf */ +#include /* strcmp */ +#include /* clock_t, clock(), CLOCKS_PER_SEC */ +#include +#include "lz4frame.h" /* include multiple times to test correctness/safety */ +#include "lz4frame.h" +#define LZ4F_STATIC_LINKING_ONLY +#include "lz4frame.h" +#include "lz4frame.h" +#include "lz4.h" /* LZ4_VERSION_STRING */ +#define XXH_STATIC_LINKING_ONLY +#include "xxhash.h" /* XXH64 */ + + +/*-************************************ +* Constants +**************************************/ +#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__); } + +/************************************** +* Exceptions +***************************************/ +#ifndef DEBUG +# define DEBUG 0 +#endif +#define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__); +#define EXM_THROW(error, ...) \ +{ \ + DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \ + DISPLAYLEVEL(1, "Error %i : ", error); \ + DISPLAYLEVEL(1, __VA_ARGS__); \ + DISPLAYLEVEL(1, " \n"); \ + exit(error); \ +} + + + +/*-*************************************** +* Local Parameters +*****************************************/ +static U32 no_prompt = 0; +static U32 displayLevel = 2; +static U32 use_pause = 0; + + +/*-******************************************************* +* Fuzzer functions +*********************************************************/ +#define MIN(a,b) ( (a) < (b) ? (a) : (b) ) +#define MAX(a,b) ( (a) > (b) ? (a) : (b) ) + +void frameCheck(const char *srcFileName, unsigned bsid, size_t blockSize) +{ + FILE *srcFile; + LZ4F_decompressionContext_t dctx = NULL; + size_t srcBufferSize = 4 MB; + void *srcBuffer = malloc(srcBufferSize); + size_t dstBufferSize = 4 MB; + void *dstBuffer = malloc(dstBufferSize); + LZ4F_errorCode_t nextToLoad = 0; + size_t curblocksize = 0; + int partialBlock = 0; + + if (!srcBuffer || !dstBuffer) EXM_THROW(31, "Allocation error : not enough memory"); + srcFile = fopen(srcFileName, "rb"); + if ( srcFile==NULL ) EXM_THROW(1, "%s: %s \n", srcFileName, strerror(errno)); + + if (LZ4F_isError( LZ4F_createDecompressionContext(&dctx, LZ4F_VERSION) )) { + EXM_THROW(32, "Unable to create decompression context"); + } + /* Main Loop */ + for (;;) { + size_t readSize; + size_t pos = 0; + size_t decodedBytes = dstBufferSize; + size_t remaining; + LZ4F_frameInfo_t frameInfo; + + /* Read input */ + readSize = fread(srcBuffer, 1, srcBufferSize, srcFile); + if (!readSize) break; /* reached end of file or stream */ + + while (pos < readSize) { /* still to read */ + /* Decode Input (at least partially) */ + if (!nextToLoad) { + /* LZ4F_decompress returned 0 : starting new frame */ + curblocksize = 0; + remaining = readSize - pos; + nextToLoad = LZ4F_getFrameInfo(dctx, &frameInfo, (char*)(srcBuffer)+pos, &remaining); + if (LZ4F_isError(nextToLoad)) EXM_THROW(66, "Error getting frame info: %s", LZ4F_getErrorName(nextToLoad)); /* XXX */ + if (frameInfo.blockSizeID != bsid) EXM_THROW(66, "Block size ID %u != expected %u", frameInfo.blockSizeID, bsid); + pos += remaining; + /* nextToLoad should be block header size */ + remaining = nextToLoad; + decodedBytes = dstBufferSize; + nextToLoad = LZ4F_decompress(dctx, dstBuffer, &decodedBytes, (char*)(srcBuffer)+pos, &remaining, NULL); + if (LZ4F_isError(nextToLoad)) EXM_THROW(66, "Decompression error : %s", LZ4F_getErrorName(nextToLoad)); + pos += remaining; + } + decodedBytes = dstBufferSize; + /* nextToLoad should be just enough to cover the next block */ + if (nextToLoad > (readSize - pos)) { + /* block is not fully contained in current buffer */ + partialBlock = 1; + remaining = readSize - pos; + } else { + if (partialBlock) { + partialBlock = 0; + } + remaining = nextToLoad; + } + nextToLoad = LZ4F_decompress(dctx, dstBuffer, &decodedBytes, (char*)(srcBuffer)+pos, &remaining, NULL); + if (LZ4F_isError(nextToLoad)) EXM_THROW(66, "Decompression error : %s", LZ4F_getErrorName(nextToLoad)); + curblocksize += decodedBytes; + pos += remaining; + if (!partialBlock) { + /* detect small block due to end of frame; the final 4-byte frame checksum could be left in the buffer */ + if ((curblocksize != 0) && (nextToLoad > 4)) { + /* allow 4-byte fudge */ + if ((MAX(curblocksize, blockSize) - MIN(curblocksize, blockSize)) > 4) + EXM_THROW(66, "Block size %zu != expected %zu", curblocksize, blockSize); + } + curblocksize = 0; + } + } + } + /* can be out because readSize == 0, which could be an fread() error */ + if (ferror(srcFile)) EXM_THROW(67, "Read error"); + + if (nextToLoad!=0) EXM_THROW(68, "Unfinished stream"); + +} + +int FUZ_usage(const char* programName) +{ + DISPLAY( "Usage :\n"); + DISPLAY( " %s [args] filename\n", programName); + DISPLAY( "\n"); + DISPLAY( "Arguments :\n"); + DISPLAY( " -b# : expected blocksizeID [4-7] (required)\n"); + DISPLAY( " -B# : expected blocksize [4-7] (required)\n"); + DISPLAY( " -v : verbose\n"); + DISPLAY( " -h : display help and exit\n"); + return 0; +} + + +int main(int argc, const char** argv) +{ + int argNb; + int bsid=0; + size_t blockSize=0; + const char* const programName = argv[0]; + + /* Check command line */ + for (argNb=1; argNb='0') && (*argument<='9')) { + bsid *= 10; + bsid += *argument - '0'; + argument++; + } + break; + + case 'B': + argument++; + blockSize=0; + while ((*argument>='0') && (*argument<='9')) { + blockSize *= 10; + blockSize += *argument - '0'; + argument++; + } + break; + + default: + ; + return FUZ_usage(programName); + } + } + } else { + if (bsid == 0 || blockSize == 0) + return FUZ_usage(programName); + DISPLAY("Starting frame checker (%i-bits, %s)\n", (int)(sizeof(size_t)*8), LZ4_VERSION_STRING); + frameCheck(argument, bsid, blockSize); + } + } + return 0; +} diff --git a/tests/test_custom_block_sizes.sh b/tests/test_custom_block_sizes.sh new file mode 100755 index 0000000..7317b65 --- /dev/null +++ b/tests/test_custom_block_sizes.sh @@ -0,0 +1,72 @@ +#/usr/bin/env sh +set -e + +LZ4=../lz4 +CHECKFRAME=./checkFrame +DATAGEN=./datagen + +failures="" + +TMPFILE=/tmp/test_custom_block_sizes.$$ +TMPFILE1=/tmp/test_custom_block_sizes1.$$ +TMPFILE2=/tmp/test_custom_block_sizes2.$$ +$DATAGEN -g12345678 > $TMPFILE1 +$DATAGEN -g12345678 > $TMPFILE2 + +echo Testing -B32 +$LZ4 -f -B32 $TMPFILE && failures="31 (should fail) " + +for blocksize in 512 65535 65536 +do + echo Testing -B$blocksize + $LZ4 -f -B$blocksize $TMPFILE1 + $LZ4 -f -B$blocksize $TMPFILE2 + cat $TMPFILE1.lz4 $TMPFILE2.lz4 > $TMPFILE.lz4 + $CHECKFRAME -B$blocksize -b4 $TMPFILE.lz4 || failures="$failures $blocksize " +done + +for blocksize in 65537 262143 262144 +do + echo Testing -B$blocksize + $LZ4 -f -B$blocksize $TMPFILE1 + $LZ4 -f -B$blocksize $TMPFILE2 + cat $TMPFILE1.lz4 $TMPFILE2.lz4 > $TMPFILE.lz4 + $CHECKFRAME -B$blocksize -b5 $TMPFILE.lz4 || failures="$failures $blocksize " +done + +for blocksize in 262145 1048575 1048576 +do + echo Testing -B$blocksize + $LZ4 -f -B$blocksize $TMPFILE1 + $LZ4 -f -B$blocksize $TMPFILE2 + cat $TMPFILE1.lz4 $TMPFILE2.lz4 > $TMPFILE.lz4 + $CHECKFRAME -B$blocksize -b6 $TMPFILE.lz4 || failures="$failures $blocksize " +done + +for blocksize in 1048577 4194303 4194304 +do + echo Testing -B$blocksize + $LZ4 -f -B$blocksize $TMPFILE1 + $LZ4 -f -B$blocksize $TMPFILE2 + cat $TMPFILE1.lz4 $TMPFILE2.lz4 > $TMPFILE.lz4 + $CHECKFRAME -B$blocksize -b7 $TMPFILE.lz4 || failures="$failures $blocksize " +done + +for blocksize in 4194305 10485760 +do + echo Testing -B$blocksize + $LZ4 -f -B$blocksize $TMPFILE1 + $LZ4 -f -B$blocksize $TMPFILE2 + cat $TMPFILE1.lz4 $TMPFILE2.lz4 > $TMPFILE.lz4 + $CHECKFRAME -B4194304 -b7 $TMPFILE.lz4 || failures="$failures $blocksize " +done + +rm $TMPFILE.lz4 $TMPFILE1 $TMPFILE1.lz4 $TMPFILE2 $TMPFILE2.lz4 +if [ "$failures" == "" ] +then + echo ---- All tests passed + exit 0 +else + echo ---- The following tests had failures: $failures + exit 1 +fi -- cgit v1.2.3 From 718d16ef29315b98236ef5da9496f0477c152f5d Mon Sep 17 00:00:00 2001 From: Blaise Sanouillet <43636048+blezsan@users.noreply.github.com> Date: Fri, 28 Sep 2018 09:02:36 -0700 Subject: support custom block sizes - fix types --- programs/lz4io.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/programs/lz4io.c b/programs/lz4io.c index 17ff9a1..bec3ae4 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -110,7 +110,7 @@ static clock_t g_time = 0; static int g_overwrite = 1; static int g_testMode = 0; static int g_blockSizeId = LZ4IO_BLOCKSIZEID_DEFAULT; -static int g_blockSize = 0; +static size_t g_blockSize = 0; static int g_blockChecksum = 0; static int g_streamChecksum = 1; static int g_blockIndependence = 1; -- cgit v1.2.3 From 32501603c71fa61bc14ba38e5646c8ba16c27e6e Mon Sep 17 00:00:00 2001 From: Blaise Sanouillet <43636048+blezsan@users.noreply.github.com> Date: Fri, 28 Sep 2018 09:53:14 -0700 Subject: support custom block sizes: fix usage message & remove unused functions --- programs/lz4cli.c | 2 +- programs/lz4io.c | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/programs/lz4cli.c b/programs/lz4cli.c index 759f336..3709f50 100644 --- a/programs/lz4cli.c +++ b/programs/lz4cli.c @@ -134,7 +134,7 @@ static int usage_advanced(const char* exeName) DISPLAY( " -r : operate recursively on directories (sets also -m) \n"); #endif DISPLAY( " -l : compress using Legacy format (Linux kernel compression)\n"); - DISPLAY( " -B# : cut file into independent blocks of size # bytes [32+] \n"); + DISPLAY( " -B# : cut file into blocks of size # bytes [32+] \n"); DISPLAY( " or predefined block size [4-7] (default: 7) \n"); DISPLAY( " -BD : Block dependency (improve compression ratio) \n"); DISPLAY( " -BX : enable block checksum (default:disabled) \n"); diff --git a/programs/lz4io.c b/programs/lz4io.c index bec3ae4..a35928d 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -256,7 +256,6 @@ void LZ4IO_setRemoveSrcFile(unsigned flag) { g_removeSrcFile = (flag>0); } ** ********************** LZ4 File / Pipe compression ********************* ** ** ************************************************************************ */ -static int LZ4IO_GetBlockSize_FromBlockId (int id) { return (1 << (8 + (2 * id))); } static int LZ4IO_isSkippableMagicNumber(unsigned int magic) { return (magic & LZ4IO_SKIPPABLEMASK) == LZ4IO_SKIPPABLE0; } -- cgit v1.2.3 From 4d9435f1ee0d21072314fc34e37d990941ced7e3 Mon Sep 17 00:00:00 2001 From: Blaise Sanouillet <43636048+blezsan@users.noreply.github.com> Date: Fri, 28 Sep 2018 10:00:38 -0700 Subject: support custom block sizes: fix const --- tests/checkFrame.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/checkFrame.c b/tests/checkFrame.c index ace51c7..f32570c 100644 --- a/tests/checkFrame.c +++ b/tests/checkFrame.c @@ -101,9 +101,9 @@ void frameCheck(const char *srcFileName, unsigned bsid, size_t blockSize) { FILE *srcFile; LZ4F_decompressionContext_t dctx = NULL; - size_t srcBufferSize = 4 MB; + const size_t srcBufferSize = 4 MB; void *srcBuffer = malloc(srcBufferSize); - size_t dstBufferSize = 4 MB; + const size_t dstBufferSize = 4 MB; void *dstBuffer = malloc(dstBufferSize); LZ4F_errorCode_t nextToLoad = 0; size_t curblocksize = 0; -- cgit v1.2.3 From f2ae385c2f57b692e3283ead684c54b46067d966 Mon Sep 17 00:00:00 2001 From: Oleg Khabinov Date: Tue, 18 Sep 2018 11:29:31 -0700 Subject: Rename initCheck to dirtyContext and use it in LZ4_resetStream_fast() to check if full reset is needed. --- lib/lz4.c | 40 ++++++++++++++++++++++++++++++++-------- lib/lz4.h | 29 +++++++++++++++++------------ tests/fuzzer.c | 11 ++++++++--- 3 files changed, 57 insertions(+), 23 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index d0fa3e8..aed2bb2 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -610,6 +610,15 @@ LZ4_FORCE_INLINE void LZ4_prepareTable( LZ4_stream_t_internal* const cctx, const int inputSize, const tableType_t tableType) { + /* If compression failed during the previous step, then the context + * is marked as dirty, therefore, it has to be fully reset. + */ + if (cctx->dirtyContext) { + DEBUGLOG(5, "LZ4_prepareTable: Full reset for %p", cctx); + MEM_INIT(cctx, 0, sizeof(LZ4_stream_t_internal)); + return; + } + /* If the table hasn't been used, it's guaranteed to be zeroed out, and is * therefore safe to use no matter what mode we're in. Otherwise, we figure * out if it's safe to leave as is or whether it needs to be reset. @@ -660,6 +669,7 @@ LZ4_FORCE_INLINE int LZ4_compress_generic( const dictIssue_directive dictIssue, const U32 acceleration) { + int result; const BYTE* ip = (const BYTE*) source; U32 const startIndex = cctx->currentOffset; @@ -695,9 +705,9 @@ LZ4_FORCE_INLINE int LZ4_compress_generic( DEBUGLOG(5, "LZ4_compress_generic: srcSize=%i, tableType=%u", inputSize, tableType); /* Init conditions */ - if (outputLimited == fillOutput && maxOutputSize < 1) return 0; /* Impossible to store anything */ - if ((U32)inputSize > (U32)LZ4_MAX_INPUT_SIZE) return 0; /* Unsupported inputSize, too large (or negative) */ - if ((tableType == byU16) && (inputSize>=LZ4_64Klimit)) return 0; /* Size too large (not within 64K limit) */ + if (outputLimited == fillOutput && maxOutputSize < 1) goto _failure; /* Impossible to store anything */ + if ((U32)inputSize > (U32)LZ4_MAX_INPUT_SIZE) goto _failure; /* Unsupported inputSize, too large (or negative) */ + if ((tableType == byU16) && (inputSize>=LZ4_64Klimit)) goto _failure; /* Size too large (not within 64K limit) */ if (tableType==byPtr) assert(dictDirective==noDict); /* only supported use case with byPtr */ assert(acceleration >= 1); @@ -814,7 +824,8 @@ LZ4_FORCE_INLINE int LZ4_compress_generic( token = op++; if ((outputLimited == limitedOutput) && /* Check output buffer overflow */ (unlikely(op + litLength + (2 + 1 + LASTLITERALS) + (litLength/255) > olimit))) - return 0; + goto _failure; + if ((outputLimited == fillOutput) && (unlikely(op + (litLength+240)/255 /* litlen */ + litLength /* literals */ + 2 /* offset */ + 1 /* token */ + MFLIMIT - MINMATCH /* min last literals so last match is <= end - MFLIMIT */ > olimit))) { op--; @@ -887,7 +898,7 @@ _next_match: if ((outputLimited) && /* Check output buffer overflow */ (unlikely(op + (1 + LASTLITERALS) + (matchCode>>8) > olimit)) ) { if (outputLimited == limitedOutput) - return 0; + goto _failure; if (outputLimited == fillOutput) { /* Match description too long : reduce it */ U32 newMatchCode = 15 /* in token */ - 1 /* to avoid needing a zero byte */ + ((U32)(olimit - op) - 2 - 1 - LASTLITERALS) * 255; @@ -985,7 +996,7 @@ _last_literals: lastRun -= (lastRun+240)/255; } if (outputLimited == limitedOutput) - return 0; + goto _failure; } if (lastRun >= RUN_MASK) { size_t accumulator = lastRun - RUN_MASK; @@ -1004,7 +1015,14 @@ _last_literals: *inputConsumed = (int) (((const char*)ip)-source); } DEBUGLOG(5, "LZ4_compress_generic: compressed %i bytes into %i bytes", inputSize, (int)(((char*)op) - dest)); - return (int)(((char*)op) - dest); + result = (int)(((char*)op) - dest); + assert(result > 0); + return result; + +_failure: + /* Mark stream as having dirty context, so, it has to be fully reset */ + cctx->dirtyContext = 1; + return 0; } @@ -1233,6 +1251,12 @@ int LZ4_loadDict (LZ4_stream_t* LZ4_dict, const char* dictionary, int dictSize) } void LZ4_attach_dictionary(LZ4_stream_t *working_stream, const LZ4_stream_t *dictionary_stream) { + /* Calling LZ4_resetStream_fast() here makes sure that changes will not be + * erased by subsequent calls to LZ4_resetStream_fast() in case stream was + * marked as having dirty context, e.g. requiring full reset. + */ + LZ4_resetStream_fast(working_stream); + if (dictionary_stream != NULL) { /* If the current offset is zero, we will never look in the * external dictionary context, since there is no value a table @@ -1276,7 +1300,7 @@ int LZ4_compress_fast_continue (LZ4_stream_t* LZ4_stream, const char* source, ch DEBUGLOG(5, "LZ4_compress_fast_continue (inputSize=%i)", inputSize); - if (streamPtr->initCheck) return 0; /* Uninitialized structure detected */ + if (streamPtr->dirtyContext) return 0; /* Uninitialized structure detected */ LZ4_renormDictT(streamPtr, inputSize); /* avoid index overflow */ if (acceleration < 1) acceleration = ACCELERATION_DEFAULT; diff --git a/lib/lz4.h b/lib/lz4.h index 4d30857..346f0dc 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -414,19 +414,22 @@ LZ4LIB_API int LZ4_decompress_fast_usingDict (const char* src, char* dst, int or #endif /*! LZ4_resetStream_fast() : - * Use this, like LZ4_resetStream(), to prepare a context for a new chain of - * calls to a streaming API (e.g., LZ4_compress_fast_continue()). + * Use this to prepare a context for a new chain of calls to a streaming API + * (e.g., LZ4_compress_fast_continue()). + * + * Note: + * To stay on the safe side, when LZ4_stream_t is used for the first time, + * it should be either created using LZ4_createStream() or + * initialized using LZ4_resetStream(). * * Note: * Using this in advance of a non-streaming-compression function is redundant, * since they all perform their own custom reset internally. * * Differences from LZ4_resetStream(): - * When an LZ4_stream_t is known to be in a internally coherent state, - * it can often be prepared for a new compression with almost no work, - * only sometimes falling back to the full, expensive reset - * that is always required when the stream is in an indeterminate state - * (i.e., the reset performed by LZ4_resetStream()). + * When an LZ4_stream_t is known to be in an internally coherent state, + * it will be prepared for a new compression with almost no work. + * Otherwise, it will fall back to the full, expensive reset. * * LZ4_streams are guaranteed to be in a valid state when: * - returned from LZ4_createStream() @@ -439,9 +442,11 @@ LZ4LIB_API int LZ4_decompress_fast_usingDict (const char* src, char* dst, int or * call that fully reset the state (e.g., LZ4_compress_fast_extState()) and * that returned success * - * When a stream isn't known to be in a valid state, it is not safe to pass to - * any fastReset or streaming function. It must first be cleansed by the full - * LZ4_resetStream(). + * Note: + * A stream that was used in a compression call that did not return success + * (e.g., LZ4_compress_fast_continue()), can still be passed to this function, + * however, it's history is not preserved because of previous compression + * failure. */ LZ4LIB_STATIC_API void LZ4_resetStream_fast (LZ4_stream_t* streamPtr); @@ -506,7 +511,7 @@ typedef struct LZ4_stream_t_internal LZ4_stream_t_internal; struct LZ4_stream_t_internal { uint32_t hashTable[LZ4_HASH_SIZE_U32]; uint32_t currentOffset; - uint16_t initCheck; + uint16_t dirtyContext; uint16_t tableType; const uint8_t* dictionary; const LZ4_stream_t_internal* dictCtx; @@ -526,7 +531,7 @@ typedef struct LZ4_stream_t_internal LZ4_stream_t_internal; struct LZ4_stream_t_internal { unsigned int hashTable[LZ4_HASH_SIZE_U32]; unsigned int currentOffset; - unsigned short initCheck; + unsigned short dirtyContext; unsigned short tableType; const unsigned char* dictionary; const LZ4_stream_t_internal* dictCtx; diff --git a/tests/fuzzer.c b/tests/fuzzer.c index b29e82e..de434f9 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -744,6 +744,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c LZ4_attach_dictionary(&LZ4_stream, &LZ4dict); blockContinueCompressedSize = LZ4_compress_fast_continue(&LZ4_stream, block, compressedBuffer, blockSize, (int)compressedBufferSize, 1); FUZ_CHECKTEST(blockContinueCompressedSize==0, "LZ4_compress_fast_continue using extDictCtx failed"); + FUZ_CHECKTEST(LZ4_stream.internal_donotuse.dirtyContext, "context should be good"); /* In the future, it might be desirable to let extDictCtx mode's * output diverge from the output generated by regular extDict mode. @@ -754,19 +755,21 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c FUZ_CHECKTEST(XXH32(compressedBuffer, blockContinueCompressedSize, 0) != expectedCrc, "LZ4_compress_fast_continue using extDictCtx produced different output"); FUZ_DISPLAYTEST("LZ4_compress_fast_continue() after LZ4_attach_dictionary(), but output buffer is 1 byte too short"); - LZ4_resetStream(&LZ4_stream); + LZ4_resetStream_fast(&LZ4_stream); LZ4_attach_dictionary(&LZ4_stream, &LZ4dict); ret = LZ4_compress_fast_continue(&LZ4_stream, block, compressedBuffer, blockSize, blockContinueCompressedSize-1, 1); FUZ_CHECKTEST(ret>0, "LZ4_compress_fast_continue using extDictCtx should fail : one missing byte for output buffer : %i written, %i buffer", ret, blockContinueCompressedSize); + FUZ_CHECKTEST(!LZ4_stream.internal_donotuse.dirtyContext, "context should be dirty"); FUZ_DISPLAYTEST(); - LZ4_resetStream(&LZ4_stream); + LZ4_resetStream_fast(&LZ4_stream); LZ4_attach_dictionary(&LZ4_stream, &LZ4dict); ret = LZ4_compress_fast_continue(&LZ4_stream, block, compressedBuffer, blockSize, blockContinueCompressedSize, 1); FUZ_CHECKTEST(ret!=blockContinueCompressedSize, "LZ4_compress_limitedOutput_compressed size is different (%i != %i)", ret, blockContinueCompressedSize); FUZ_CHECKTEST(ret<=0, "LZ4_compress_fast_continue using extDictCtx should work : enough size available within output buffer"); FUZ_CHECKTEST(ret != expectedSize, "LZ4_compress_fast_continue using extDictCtx produced different-sized output"); FUZ_CHECKTEST(XXH32(compressedBuffer, ret, 0) != expectedCrc, "LZ4_compress_fast_continue using extDictCtx produced different output"); + FUZ_CHECKTEST(LZ4_stream.internal_donotuse.dirtyContext, "context should be good"); FUZ_DISPLAYTEST(); LZ4_resetStream_fast(&LZ4_stream); @@ -776,6 +779,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c FUZ_CHECKTEST(ret<=0, "LZ4_compress_fast_continue using extDictCtx with re-used context should work : enough size available within output buffer"); FUZ_CHECKTEST(ret != expectedSize, "LZ4_compress_fast_continue using extDictCtx produced different-sized output"); FUZ_CHECKTEST(XXH32(compressedBuffer, ret, 0) != expectedCrc, "LZ4_compress_fast_continue using extDictCtx produced different output"); + FUZ_CHECKTEST(LZ4_stream.internal_donotuse.dirtyContext, "context should be good"); } /* Decompress with dictionary as external */ @@ -990,6 +994,7 @@ static void FUZ_unitTests(int compressionLevel) LZ4_resetStream(&streamingState); result = LZ4_compress_fast_continue(&streamingState, testInput, testCompressed, testCompressedSize, testCompressedSize-1, 1); FUZ_CHECKTEST(result==0, "LZ4_compress_fast_continue() compression failed!"); + FUZ_CHECKTEST(streamingState.internal_donotuse.dirtyContext, "dirtyContext flag is set") result = LZ4_decompress_safe(testCompressed, testVerify, result, testCompressedSize); FUZ_CHECKTEST(result!=(int)testCompressedSize, "LZ4_decompress_safe() decompression failed"); @@ -1012,7 +1017,7 @@ static void FUZ_unitTests(int compressionLevel) XXH64_reset(&xxhOrig, 0); XXH64_reset(&xxhNewSafe, 0); XXH64_reset(&xxhNewFast, 0); - LZ4_resetStream(&streamingState); + LZ4_resetStream_fast(&streamingState); LZ4_setStreamDecode(&decodeStateSafe, NULL, 0); LZ4_setStreamDecode(&decodeStateFast, NULL, 0); -- cgit v1.2.3 From 21120549a7f35f924a66c7c071d0f5c69cf7e08a Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 28 Sep 2018 14:57:50 -0700 Subject: fixed improper hint when LZ4F_decompress() decodes an uncompressed block, it provides an incorrect hint for next block when frame checksum is enabled and block checksum is not. Impact is low : the hint is just an hint, the decoder works whatever the amount of input provided. But the assumption that each call to LZ4F_decompress() would generate just one complete block if input size hint was respected was broken by this error. --- lib/lz4frame.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/lz4frame.c b/lib/lz4frame.c index 11e0975..547afa3 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -1474,7 +1474,7 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, } dctx->tmpInTarget -= sizeToCopy; /* need to copy more */ nextSrcSizeHint = dctx->tmpInTarget + - + dctx->frameInfo.contentChecksumFlag * 4 /* block checksum */ + + dctx->frameInfo.blockChecksumFlag * 4 /* size for block checksum */ + BHSize /* next header size */; doAnotherStage = 0; break; @@ -1525,8 +1525,10 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, dctx->tmpInSize += sizeToCopy; srcPtr += sizeToCopy; if (dctx->tmpInSize < dctx->tmpInTarget) { /* need more input */ - nextSrcSizeHint = (dctx->tmpInTarget - dctx->tmpInSize) + BHSize; - doAnotherStage=0; + nextSrcSizeHint = (dctx->tmpInTarget - dctx->tmpInSize) + + dctx->frameInfo.blockChecksumFlag * 4 /* size for block checksum */ + + BHSize /* next header size */; + doAnotherStage = 0; break; } selectedIn = dctx->tmpIn; -- cgit v1.2.3 From d4703852b1f1e1b3defcfc677cabec77672e171d Mon Sep 17 00:00:00 2001 From: Blaise Sanouillet <43636048+blezsan@users.noreply.github.com> Date: Mon, 1 Oct 2018 05:22:50 -0700 Subject: support custom block sizes: remove unneeded fudging, fix leaked filehandles, other nits --- tests/checkFrame.c | 193 ++++++++++++++++++++------------------- tests/test_custom_block_sizes.sh | 6 +- 2 files changed, 101 insertions(+), 98 deletions(-) diff --git a/tests/checkFrame.c b/tests/checkFrame.c index f32570c..385bc8d 100644 --- a/tests/checkFrame.c +++ b/tests/checkFrame.c @@ -1,84 +1,84 @@ -/* - checkFrame - verify frame headers - Copyright (C) Yann Collet 2014-2016 - - 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 homepage : http://www.lz4.org - - LZ4 source repository : https://github.com/lz4/lz4 -*/ - -/*-************************************ -* Compiler specific -**************************************/ -#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 "util.h" /* U32 */ -#include /* malloc, free */ -#include /* fprintf */ -#include /* strcmp */ -#include /* clock_t, clock(), CLOCKS_PER_SEC */ -#include -#include "lz4frame.h" /* include multiple times to test correctness/safety */ -#include "lz4frame.h" -#define LZ4F_STATIC_LINKING_ONLY -#include "lz4frame.h" -#include "lz4frame.h" -#include "lz4.h" /* LZ4_VERSION_STRING */ -#define XXH_STATIC_LINKING_ONLY -#include "xxhash.h" /* XXH64 */ - - -/*-************************************ -* Constants -**************************************/ -#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__); } - -/************************************** -* Exceptions -***************************************/ -#ifndef DEBUG -# define DEBUG 0 -#endif -#define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__); -#define EXM_THROW(error, ...) \ + /* + checkFrame - verify frame headers + Copyright (C) Yann Collet 2014-2016 + + 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 homepage : http://www.lz4.org + - LZ4 source repository : https://github.com/lz4/lz4 + */ + + /*-************************************ + * Compiler specific + **************************************/ + #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 "util.h" /* U32 */ + #include /* malloc, free */ + #include /* fprintf */ + #include /* strcmp */ + #include /* clock_t, clock(), CLOCKS_PER_SEC */ + #include + #include "lz4frame.h" /* include multiple times to test correctness/safety */ + #include "lz4frame.h" + #define LZ4F_STATIC_LINKING_ONLY + #include "lz4frame.h" + #include "lz4frame.h" + #include "lz4.h" /* LZ4_VERSION_STRING */ + #define XXH_STATIC_LINKING_ONLY + #include "xxhash.h" /* XXH64 */ + + + /*-************************************ + * Constants + **************************************/ + #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__); } + + /************************************** + * Exceptions + ***************************************/ + #ifndef DEBUG + # define DEBUG 0 + #endif + #define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__); + #define EXM_THROW(error, ...) \ { \ DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \ DISPLAYLEVEL(1, "Error %i : ", error); \ DISPLAYLEVEL(1, __VA_ARGS__); \ DISPLAYLEVEL(1, " \n"); \ - exit(error); \ + return(error); \ } @@ -97,24 +97,21 @@ static U32 use_pause = 0; #define MIN(a,b) ( (a) < (b) ? (a) : (b) ) #define MAX(a,b) ( (a) > (b) ? (a) : (b) ) -void frameCheck(const char *srcFileName, unsigned bsid, size_t blockSize) +int frameCheck(FILE* const srcFile, unsigned bsid, size_t blockSize) { - FILE *srcFile; LZ4F_decompressionContext_t dctx = NULL; const size_t srcBufferSize = 4 MB; - void *srcBuffer = malloc(srcBufferSize); + void* const srcBuffer = malloc(srcBufferSize); const size_t dstBufferSize = 4 MB; - void *dstBuffer = malloc(dstBufferSize); + void* const dstBuffer = malloc(dstBufferSize); LZ4F_errorCode_t nextToLoad = 0; size_t curblocksize = 0; int partialBlock = 0; - if (!srcBuffer || !dstBuffer) EXM_THROW(31, "Allocation error : not enough memory"); - srcFile = fopen(srcFileName, "rb"); - if ( srcFile==NULL ) EXM_THROW(1, "%s: %s \n", srcFileName, strerror(errno)); + if (!srcBuffer || !dstBuffer) EXM_THROW(20, "Allocation error : not enough memory"); if (LZ4F_isError( LZ4F_createDecompressionContext(&dctx, LZ4F_VERSION) )) { - EXM_THROW(32, "Unable to create decompression context"); + EXM_THROW(21, "Unable to create decompression context"); } /* Main Loop */ for (;;) { @@ -135,14 +132,14 @@ void frameCheck(const char *srcFileName, unsigned bsid, size_t blockSize) curblocksize = 0; remaining = readSize - pos; nextToLoad = LZ4F_getFrameInfo(dctx, &frameInfo, (char*)(srcBuffer)+pos, &remaining); - if (LZ4F_isError(nextToLoad)) EXM_THROW(66, "Error getting frame info: %s", LZ4F_getErrorName(nextToLoad)); /* XXX */ - if (frameInfo.blockSizeID != bsid) EXM_THROW(66, "Block size ID %u != expected %u", frameInfo.blockSizeID, bsid); + if (LZ4F_isError(nextToLoad)) EXM_THROW(22, "Error getting frame info: %s", LZ4F_getErrorName(nextToLoad)); /* XXX */ + if (frameInfo.blockSizeID != bsid) EXM_THROW(23, "Block size ID %u != expected %u", frameInfo.blockSizeID, bsid); pos += remaining; /* nextToLoad should be block header size */ remaining = nextToLoad; decodedBytes = dstBufferSize; nextToLoad = LZ4F_decompress(dctx, dstBuffer, &decodedBytes, (char*)(srcBuffer)+pos, &remaining, NULL); - if (LZ4F_isError(nextToLoad)) EXM_THROW(66, "Decompression error : %s", LZ4F_getErrorName(nextToLoad)); + if (LZ4F_isError(nextToLoad)) EXM_THROW(24, "Decompression error : %s", LZ4F_getErrorName(nextToLoad)); pos += remaining; } decodedBytes = dstBufferSize; @@ -158,25 +155,25 @@ void frameCheck(const char *srcFileName, unsigned bsid, size_t blockSize) remaining = nextToLoad; } nextToLoad = LZ4F_decompress(dctx, dstBuffer, &decodedBytes, (char*)(srcBuffer)+pos, &remaining, NULL); - if (LZ4F_isError(nextToLoad)) EXM_THROW(66, "Decompression error : %s", LZ4F_getErrorName(nextToLoad)); + if (LZ4F_isError(nextToLoad)) EXM_THROW(24, "Decompression error : %s", LZ4F_getErrorName(nextToLoad)); curblocksize += decodedBytes; pos += remaining; if (!partialBlock) { /* detect small block due to end of frame; the final 4-byte frame checksum could be left in the buffer */ if ((curblocksize != 0) && (nextToLoad > 4)) { - /* allow 4-byte fudge */ - if ((MAX(curblocksize, blockSize) - MIN(curblocksize, blockSize)) > 4) - EXM_THROW(66, "Block size %zu != expected %zu", curblocksize, blockSize); + if (curblocksize != blockSize) + EXM_THROW(25, "Block size %zu != expected %zu, pos %zu\n", curblocksize, blockSize, pos); } curblocksize = 0; } } } /* can be out because readSize == 0, which could be an fread() error */ - if (ferror(srcFile)) EXM_THROW(67, "Read error"); + if (ferror(srcFile)) EXM_THROW(26, "Read error"); - if (nextToLoad!=0) EXM_THROW(68, "Unfinished stream"); + if (nextToLoad!=0) EXM_THROW(27, "Unfinished stream"); + return 0; } int FUZ_usage(const char* programName) @@ -186,7 +183,7 @@ int FUZ_usage(const char* programName) DISPLAY( "\n"); DISPLAY( "Arguments :\n"); DISPLAY( " -b# : expected blocksizeID [4-7] (required)\n"); - DISPLAY( " -B# : expected blocksize [4-7] (required)\n"); + DISPLAY( " -B# : expected blocksize [32-4194304] (required)\n"); DISPLAY( " -v : verbose\n"); DISPLAY( " -h : display help and exit\n"); return 0; @@ -196,6 +193,7 @@ int FUZ_usage(const char* programName) int main(int argc, const char** argv) { int argNb; + int err; int bsid=0; size_t blockSize=0; const char* const programName = argv[0]; @@ -259,10 +257,15 @@ int main(int argc, const char** argv) } } } else { + FILE *srcFile; if (bsid == 0 || blockSize == 0) return FUZ_usage(programName); DISPLAY("Starting frame checker (%i-bits, %s)\n", (int)(sizeof(size_t)*8), LZ4_VERSION_STRING); - frameCheck(argument, bsid, blockSize); + srcFile = fopen(argument, "rb"); + if ( srcFile==NULL ) EXM_THROW(1, "%s: %s \n", argument, strerror(errno)); + err = frameCheck(srcFile, bsid, blockSize); + fclose(srcFile); + return (err); } } return 0; diff --git a/tests/test_custom_block_sizes.sh b/tests/test_custom_block_sizes.sh index 7317b65..aba6733 100755 --- a/tests/test_custom_block_sizes.sh +++ b/tests/test_custom_block_sizes.sh @@ -13,10 +13,10 @@ TMPFILE2=/tmp/test_custom_block_sizes2.$$ $DATAGEN -g12345678 > $TMPFILE1 $DATAGEN -g12345678 > $TMPFILE2 -echo Testing -B32 -$LZ4 -f -B32 $TMPFILE && failures="31 (should fail) " +echo Testing -B31 +$LZ4 -f -B31 $TMPFILE1 && failures="31 (should fail) " -for blocksize in 512 65535 65536 +for blocksize in 32 65535 65536 do echo Testing -B$blocksize $LZ4 -f -B$blocksize $TMPFILE1 -- cgit v1.2.3 From 28eb88d9885810018cb79421ca7ce0462fd61df2 Mon Sep 17 00:00:00 2001 From: Oleg Khabinov Date: Mon, 1 Oct 2018 15:19:45 -0700 Subject: Some followups and renamings --- lib/lz4.c | 15 ++++++++------- lib/lz4.h | 4 ++-- tests/fuzzer.c | 10 +++++----- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index aed2bb2..1521aa2 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -613,7 +613,7 @@ LZ4_FORCE_INLINE void LZ4_prepareTable( /* If compression failed during the previous step, then the context * is marked as dirty, therefore, it has to be fully reset. */ - if (cctx->dirtyContext) { + if (cctx->dirty) { DEBUGLOG(5, "LZ4_prepareTable: Full reset for %p", cctx); MEM_INIT(cctx, 0, sizeof(LZ4_stream_t_internal)); return; @@ -704,10 +704,11 @@ LZ4_FORCE_INLINE int LZ4_compress_generic( U32 forwardH; DEBUGLOG(5, "LZ4_compress_generic: srcSize=%i, tableType=%u", inputSize, tableType); - /* Init conditions */ - if (outputLimited == fillOutput && maxOutputSize < 1) goto _failure; /* Impossible to store anything */ - if ((U32)inputSize > (U32)LZ4_MAX_INPUT_SIZE) goto _failure; /* Unsupported inputSize, too large (or negative) */ - if ((tableType == byU16) && (inputSize>=LZ4_64Klimit)) goto _failure; /* Size too large (not within 64K limit) */ + /* If init conditions are not met, we don't have to mark stream + * as having dirty context, since no action was taken yet */ + if (outputLimited == fillOutput && maxOutputSize < 1) return 0; /* Impossible to store anything */ + if ((U32)inputSize > (U32)LZ4_MAX_INPUT_SIZE) return 0; /* Unsupported inputSize, too large (or negative) */ + if ((tableType == byU16) && (inputSize>=LZ4_64Klimit)) return 0; /* Size too large (not within 64K limit) */ if (tableType==byPtr) assert(dictDirective==noDict); /* only supported use case with byPtr */ assert(acceleration >= 1); @@ -1021,7 +1022,7 @@ _last_literals: _failure: /* Mark stream as having dirty context, so, it has to be fully reset */ - cctx->dirtyContext = 1; + cctx->dirty = 1; return 0; } @@ -1300,7 +1301,7 @@ int LZ4_compress_fast_continue (LZ4_stream_t* LZ4_stream, const char* source, ch DEBUGLOG(5, "LZ4_compress_fast_continue (inputSize=%i)", inputSize); - if (streamPtr->dirtyContext) return 0; /* Uninitialized structure detected */ + if (streamPtr->dirty) return 0; /* Uninitialized structure detected */ LZ4_renormDictT(streamPtr, inputSize); /* avoid index overflow */ if (acceleration < 1) acceleration = ACCELERATION_DEFAULT; diff --git a/lib/lz4.h b/lib/lz4.h index 346f0dc..7e89d5f 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -511,7 +511,7 @@ typedef struct LZ4_stream_t_internal LZ4_stream_t_internal; struct LZ4_stream_t_internal { uint32_t hashTable[LZ4_HASH_SIZE_U32]; uint32_t currentOffset; - uint16_t dirtyContext; + uint16_t dirty; uint16_t tableType; const uint8_t* dictionary; const LZ4_stream_t_internal* dictCtx; @@ -531,7 +531,7 @@ typedef struct LZ4_stream_t_internal LZ4_stream_t_internal; struct LZ4_stream_t_internal { unsigned int hashTable[LZ4_HASH_SIZE_U32]; unsigned int currentOffset; - unsigned short dirtyContext; + unsigned short dirty; unsigned short tableType; const unsigned char* dictionary; const LZ4_stream_t_internal* dictCtx; diff --git a/tests/fuzzer.c b/tests/fuzzer.c index de434f9..95cb092 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -744,7 +744,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c LZ4_attach_dictionary(&LZ4_stream, &LZ4dict); blockContinueCompressedSize = LZ4_compress_fast_continue(&LZ4_stream, block, compressedBuffer, blockSize, (int)compressedBufferSize, 1); FUZ_CHECKTEST(blockContinueCompressedSize==0, "LZ4_compress_fast_continue using extDictCtx failed"); - FUZ_CHECKTEST(LZ4_stream.internal_donotuse.dirtyContext, "context should be good"); + FUZ_CHECKTEST(LZ4_stream.internal_donotuse.dirty, "context should be good"); /* In the future, it might be desirable to let extDictCtx mode's * output diverge from the output generated by regular extDict mode. @@ -759,7 +759,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c LZ4_attach_dictionary(&LZ4_stream, &LZ4dict); ret = LZ4_compress_fast_continue(&LZ4_stream, block, compressedBuffer, blockSize, blockContinueCompressedSize-1, 1); FUZ_CHECKTEST(ret>0, "LZ4_compress_fast_continue using extDictCtx should fail : one missing byte for output buffer : %i written, %i buffer", ret, blockContinueCompressedSize); - FUZ_CHECKTEST(!LZ4_stream.internal_donotuse.dirtyContext, "context should be dirty"); + FUZ_CHECKTEST(!LZ4_stream.internal_donotuse.dirty, "context should be dirty"); FUZ_DISPLAYTEST(); LZ4_resetStream_fast(&LZ4_stream); @@ -769,7 +769,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c FUZ_CHECKTEST(ret<=0, "LZ4_compress_fast_continue using extDictCtx should work : enough size available within output buffer"); FUZ_CHECKTEST(ret != expectedSize, "LZ4_compress_fast_continue using extDictCtx produced different-sized output"); FUZ_CHECKTEST(XXH32(compressedBuffer, ret, 0) != expectedCrc, "LZ4_compress_fast_continue using extDictCtx produced different output"); - FUZ_CHECKTEST(LZ4_stream.internal_donotuse.dirtyContext, "context should be good"); + FUZ_CHECKTEST(LZ4_stream.internal_donotuse.dirty, "context should be good"); FUZ_DISPLAYTEST(); LZ4_resetStream_fast(&LZ4_stream); @@ -779,7 +779,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c FUZ_CHECKTEST(ret<=0, "LZ4_compress_fast_continue using extDictCtx with re-used context should work : enough size available within output buffer"); FUZ_CHECKTEST(ret != expectedSize, "LZ4_compress_fast_continue using extDictCtx produced different-sized output"); FUZ_CHECKTEST(XXH32(compressedBuffer, ret, 0) != expectedCrc, "LZ4_compress_fast_continue using extDictCtx produced different output"); - FUZ_CHECKTEST(LZ4_stream.internal_donotuse.dirtyContext, "context should be good"); + FUZ_CHECKTEST(LZ4_stream.internal_donotuse.dirty, "context should be good"); } /* Decompress with dictionary as external */ @@ -994,7 +994,7 @@ static void FUZ_unitTests(int compressionLevel) LZ4_resetStream(&streamingState); result = LZ4_compress_fast_continue(&streamingState, testInput, testCompressed, testCompressedSize, testCompressedSize-1, 1); FUZ_CHECKTEST(result==0, "LZ4_compress_fast_continue() compression failed!"); - FUZ_CHECKTEST(streamingState.internal_donotuse.dirtyContext, "dirtyContext flag is set") + FUZ_CHECKTEST(streamingState.internal_donotuse.dirty, "context should be clean") result = LZ4_decompress_safe(testCompressed, testVerify, result, testCompressedSize); FUZ_CHECKTEST(result!=(int)testCompressedSize, "LZ4_decompress_safe() decompression failed"); -- cgit v1.2.3 From 294293d8990b8f25ebc8660ef95dd9b1485c5d37 Mon Sep 17 00:00:00 2001 From: Blaise Sanouillet <43636048+blezsan@users.noreply.github.com> Date: Tue, 2 Oct 2018 05:38:30 -0700 Subject: support custom block sizes: fix memory leak to make CI builds happy --- tests/checkFrame.c | 76 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 55 insertions(+), 21 deletions(-) diff --git a/tests/checkFrame.c b/tests/checkFrame.c index 385bc8d..f7e7a50 100644 --- a/tests/checkFrame.c +++ b/tests/checkFrame.c @@ -97,32 +97,59 @@ static U32 use_pause = 0; #define MIN(a,b) ( (a) < (b) ? (a) : (b) ) #define MAX(a,b) ( (a) > (b) ? (a) : (b) ) -int frameCheck(FILE* const srcFile, unsigned bsid, size_t blockSize) +typedef struct { + void* srcBuffer; + size_t srcBufferSize; + void* dstBuffer; + size_t dstBufferSize; + LZ4F_decompressionContext_t ctx; +} cRess_t; + +static int createCResources(cRess_t *ress) +{ + ress->srcBufferSize = 4 MB; + ress->srcBuffer = malloc(ress->srcBufferSize); + ress->dstBufferSize = 4 MB; + ress->dstBuffer = malloc(ress->dstBufferSize); + + if (!ress->srcBuffer || !ress->dstBuffer) { + free(ress->srcBuffer); + free(ress->dstBuffer); + EXM_THROW(20, "Allocation error : not enough memory"); + } + + if (LZ4F_isError( LZ4F_createDecompressionContext(&(ress->ctx), LZ4F_VERSION) )) { + free(ress->srcBuffer); + free(ress->dstBuffer); + EXM_THROW(21, "Unable to create decompression context"); + } + return 0; +} + +static void freeCResources(cRess_t ress) +{ + free(ress.srcBuffer); + free(ress.dstBuffer); + + (void) LZ4F_freeDecompressionContext(ress.ctx); +} + +int frameCheck(cRess_t ress, FILE* const srcFile, unsigned bsid, size_t blockSize) { - LZ4F_decompressionContext_t dctx = NULL; - const size_t srcBufferSize = 4 MB; - void* const srcBuffer = malloc(srcBufferSize); - const size_t dstBufferSize = 4 MB; - void* const dstBuffer = malloc(dstBufferSize); LZ4F_errorCode_t nextToLoad = 0; size_t curblocksize = 0; int partialBlock = 0; - if (!srcBuffer || !dstBuffer) EXM_THROW(20, "Allocation error : not enough memory"); - - if (LZ4F_isError( LZ4F_createDecompressionContext(&dctx, LZ4F_VERSION) )) { - EXM_THROW(21, "Unable to create decompression context"); - } /* Main Loop */ for (;;) { size_t readSize; size_t pos = 0; - size_t decodedBytes = dstBufferSize; + size_t decodedBytes = ress.dstBufferSize; size_t remaining; LZ4F_frameInfo_t frameInfo; /* Read input */ - readSize = fread(srcBuffer, 1, srcBufferSize, srcFile); + readSize = fread(ress.srcBuffer, 1, ress.srcBufferSize, srcFile); if (!readSize) break; /* reached end of file or stream */ while (pos < readSize) { /* still to read */ @@ -131,18 +158,18 @@ int frameCheck(FILE* const srcFile, unsigned bsid, size_t blockSize) /* LZ4F_decompress returned 0 : starting new frame */ curblocksize = 0; remaining = readSize - pos; - nextToLoad = LZ4F_getFrameInfo(dctx, &frameInfo, (char*)(srcBuffer)+pos, &remaining); + nextToLoad = LZ4F_getFrameInfo(ress.ctx, &frameInfo, (char*)(ress.srcBuffer)+pos, &remaining); if (LZ4F_isError(nextToLoad)) EXM_THROW(22, "Error getting frame info: %s", LZ4F_getErrorName(nextToLoad)); /* XXX */ if (frameInfo.blockSizeID != bsid) EXM_THROW(23, "Block size ID %u != expected %u", frameInfo.blockSizeID, bsid); pos += remaining; /* nextToLoad should be block header size */ remaining = nextToLoad; - decodedBytes = dstBufferSize; - nextToLoad = LZ4F_decompress(dctx, dstBuffer, &decodedBytes, (char*)(srcBuffer)+pos, &remaining, NULL); + decodedBytes = ress.dstBufferSize; + nextToLoad = LZ4F_decompress(ress.ctx, ress.dstBuffer, &decodedBytes, (char*)(ress.srcBuffer)+pos, &remaining, NULL); if (LZ4F_isError(nextToLoad)) EXM_THROW(24, "Decompression error : %s", LZ4F_getErrorName(nextToLoad)); pos += remaining; } - decodedBytes = dstBufferSize; + decodedBytes = ress.dstBufferSize; /* nextToLoad should be just enough to cover the next block */ if (nextToLoad > (readSize - pos)) { /* block is not fully contained in current buffer */ @@ -154,7 +181,7 @@ int frameCheck(FILE* const srcFile, unsigned bsid, size_t blockSize) } remaining = nextToLoad; } - nextToLoad = LZ4F_decompress(dctx, dstBuffer, &decodedBytes, (char*)(srcBuffer)+pos, &remaining, NULL); + nextToLoad = LZ4F_decompress(ress.ctx, ress.dstBuffer, &decodedBytes, (char*)(ress.srcBuffer)+pos, &remaining, NULL); if (LZ4F_isError(nextToLoad)) EXM_THROW(24, "Decompression error : %s", LZ4F_getErrorName(nextToLoad)); curblocksize += decodedBytes; pos += remaining; @@ -193,7 +220,6 @@ int FUZ_usage(const char* programName) int main(int argc, const char** argv) { int argNb; - int err; int bsid=0; size_t blockSize=0; const char* const programName = argv[0]; @@ -257,13 +283,21 @@ int main(int argc, const char** argv) } } } else { + int err; FILE *srcFile; + cRess_t ress; if (bsid == 0 || blockSize == 0) return FUZ_usage(programName); DISPLAY("Starting frame checker (%i-bits, %s)\n", (int)(sizeof(size_t)*8), LZ4_VERSION_STRING); + err = createCResources(&ress); + if (err) return (err); srcFile = fopen(argument, "rb"); - if ( srcFile==NULL ) EXM_THROW(1, "%s: %s \n", argument, strerror(errno)); - err = frameCheck(srcFile, bsid, blockSize); + if ( srcFile==NULL ) { + freeCResources(ress); + EXM_THROW(1, "%s: %s \n", argument, strerror(errno)); + } + err = frameCheck(ress, srcFile, bsid, blockSize); + freeCResources(ress); fclose(srcFile); return (err); } -- cgit v1.2.3 From e07a37d712c87b6d47d043b018e4ff86d31996b3 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 9 Oct 2018 14:25:18 -0700 Subject: added a test for LZ4F_compressEnd() which actively tries to make it write out of bound. For this scenario to be possible, it's necessary to set dstCapacity < LZ4F_compressBound() When a compression operation fails, the CCtx context is left in an undefined state, therefore compression cannot resume. As a consequence : - round trip tests must be aborted, since there is nothing valid to decompress - most users avoid this situation, by ensuring that dstCapacity >= LZ4F_compressBound() For these reasons, this use case was poorly tested up to now. --- lib/lz4frame.c | 25 ++++++++++++++----------- lib/lz4frame.h | 2 ++ tests/frametest.c | 14 ++++++++++++-- 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/lib/lz4frame.c b/lib/lz4frame.c index 547afa3..e688f72 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -686,7 +686,7 @@ size_t LZ4F_compressBegin_usingCDict(LZ4F_cctx* cctxPtr, * dstBuffer must be >= LZ4F_HEADER_SIZE_MAX bytes. * preferencesPtr can be NULL, in which case default parameters are selected. * @return : number of bytes written into dstBuffer for the header - * or an error code (can be tested using LZ4F_isError()) + * or an error code (can be tested using LZ4F_isError()) */ size_t LZ4F_compressBegin(LZ4F_cctx* cctxPtr, void* dstBuffer, size_t dstCapacity, @@ -934,25 +934,28 @@ size_t LZ4F_flush(LZ4F_cctx* cctxPtr, void* dstBuffer, size_t dstCapacity, const /*! 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. + * 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 an (optional) checksum. + * LZ4F_compressOptions_t structure is optional : you can provide NULL as argument. + * @return: the number of bytes written into dstBuffer (necessarily >= 4 (endMark size)) + * or an error code if it fails (can be tested using LZ4F_isError()) + * The context can then be used again to compress a new frame, starting with LZ4F_compressBegin(). */ -size_t LZ4F_compressEnd(LZ4F_cctx* cctxPtr, void* dstBuffer, size_t dstMaxSize, const LZ4F_compressOptions_t* compressOptionsPtr) +size_t LZ4F_compressEnd(LZ4F_cctx* cctxPtr, + void* dstBuffer, size_t dstCapacity, + const LZ4F_compressOptions_t* compressOptionsPtr) { BYTE* const dstStart = (BYTE*)dstBuffer; BYTE* dstPtr = dstStart; - size_t const flushSize = LZ4F_flush(cctxPtr, dstBuffer, dstMaxSize, compressOptionsPtr); + size_t const flushSize = LZ4F_flush(cctxPtr, dstBuffer, dstCapacity, compressOptionsPtr); if (LZ4F_isError(flushSize)) return flushSize; + assert(flushSize <= dstCapacity); dstPtr += flushSize; LZ4F_writeLE32(dstPtr, 0); - dstPtr+=4; /* endMark */ + dstPtr += 4; /* endMark */ if (cctxPtr->prefs.frameInfo.contentChecksumFlag == LZ4F_contentChecksumEnabled) { U32 const xxh = XXH32_digest(&(cctxPtr->xxh)); diff --git a/lib/lz4frame.h b/lib/lz4frame.h index fc30f6f..599f17e 100644 --- a/lib/lz4frame.h +++ b/lib/lz4frame.h @@ -295,6 +295,7 @@ LZ4FLIB_API size_t LZ4F_compressUpdate(LZ4F_cctx* cctx, * `cOptPtr` is optional : it's possible to provide NULL, all options will be set to default. * @return : nb of bytes written into dstBuffer (can be zero, when there is no data stored within cctx) * or an error code if it fails (which can be tested using LZ4F_isError()) + * Note : LZ4F_flush() is guaranteed to be successful when dstCapacity >= LZ4F_compressBound(0, prefsPtr). */ LZ4FLIB_API size_t LZ4F_flush(LZ4F_cctx* cctx, void* dstBuffer, size_t dstCapacity, @@ -307,6 +308,7 @@ LZ4FLIB_API size_t LZ4F_flush(LZ4F_cctx* cctx, * `cOptPtr` is optional : NULL can be provided, in which case all options will be set to default. * @return : nb of bytes written into dstBuffer, necessarily >= 4 (endMark), * or an error code if it fails (which can be tested using LZ4F_isError()) + * Note : LZ4F_compressEnd() is guaranteed to be successful when dstCapacity >= LZ4F_compressBound(0, prefsPtr). * A successful call to LZ4F_compressEnd() makes `cctx` available again for another compression task. */ LZ4FLIB_API size_t LZ4F_compressEnd(LZ4F_cctx* cctx, diff --git a/tests/frametest.c b/tests/frametest.c index f8498b7..6d2cdd0 100644 --- a/tests/frametest.c +++ b/tests/frametest.c @@ -857,8 +857,18 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi } } } CHECK(op>=oend, "LZ4F_compressFrameBound overflow"); - result = LZ4F_compressEnd(cCtx, op, oend-op, &cOptions); - CHECK(LZ4F_isError(result), "Compression completion failed (error %i : %s)", (int)result, LZ4F_getErrorName(result)); + { size_t const dstEndSafeSize = LZ4F_compressBound(0, prefsPtr); + int const tooSmallDstEnd = ((FUZ_rand(&randState) & 31) == 3); + size_t const dstEndTooSmallSize = (FUZ_rand(&randState) % dstEndSafeSize) + 1; + size_t const dstEndSize = tooSmallDstEnd ? dstEndTooSmallSize : dstEndSafeSize; + BYTE const canaryByte = (BYTE)(FUZ_rand(&randState) & 255); + op[dstEndSize] = canaryByte; + result = LZ4F_compressEnd(cCtx, op, dstEndSize, &cOptions); + CHECK(op[dstEndSize] != canaryByte, "LZ4F_compressEnd writes beyond dstCapacity !"); + if (LZ4F_isError(result)) { + if (tooSmallDstEnd) /* failure is allowed */ continue; + CHECK(1, "Compression completion failed (error %i : %s)", (int)result, LZ4F_getErrorName(result)); + } } op += result; cSize = op-(BYTE*)compressedBuffer; DISPLAYLEVEL(5, "\nCompressed %u bytes into %u \n", (U32)srcSize, (U32)cSize); -- cgit v1.2.3 From 6902fa48925ed22cd37bb4262205437feb8d2420 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 9 Oct 2018 14:37:51 -0700 Subject: fixed #589 following recommendations by @raggi. The fix is slightly different, but achieves the same goal, and is backed by a test tool which proves that it works (generates the error before the patch, no longer after the patch). --- lib/lz4frame.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/lz4frame.c b/lib/lz4frame.c index e688f72..357f962 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -951,14 +951,18 @@ size_t LZ4F_compressEnd(LZ4F_cctx* cctxPtr, size_t const flushSize = LZ4F_flush(cctxPtr, dstBuffer, dstCapacity, compressOptionsPtr); if (LZ4F_isError(flushSize)) return flushSize; - assert(flushSize <= dstCapacity); dstPtr += flushSize; + assert(flushSize <= dstCapacity); + dstCapacity -= flushSize; + + if (dstCapacity < 4) return err0r(LZ4F_ERROR_dstMaxSize_tooSmall); LZ4F_writeLE32(dstPtr, 0); dstPtr += 4; /* endMark */ if (cctxPtr->prefs.frameInfo.contentChecksumFlag == LZ4F_contentChecksumEnabled) { U32 const xxh = XXH32_digest(&(cctxPtr->xxh)); + if (dstCapacity < 8) return err0r(LZ4F_ERROR_dstMaxSize_tooSmall); LZ4F_writeLE32(dstPtr, xxh); dstPtr+=4; /* content Checksum */ } -- cgit v1.2.3 From f27ea0774e34b6680a08854af9b4de06406ba39e Mon Sep 17 00:00:00 2001 From: Oleg Khabinov Date: Thu, 20 Sep 2018 11:47:30 -0700 Subject: Adding information about dirty context for _HC_ family of functions --- lib/lz4hc.c | 35 +++++++++++++++++++++++------------ lib/lz4hc.h | 13 +++++++++++-- tests/fuzzer.c | 32 ++++++++++++++++++++++++-------- 3 files changed, 58 insertions(+), 22 deletions(-) diff --git a/lib/lz4hc.c b/lib/lz4hc.c index 714ab62..d4180e3 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -744,16 +744,22 @@ LZ4_FORCE_INLINE int LZ4HC_compress_generic_internal ( cLevel = MIN(LZ4HC_CLEVEL_MAX, cLevel); { cParams_t const cParam = clTable[cLevel]; HCfavor_e const favor = ctx->favorDecSpeed ? favorDecompressionSpeed : favorCompressionRatio; - if (cParam.strat == lz4hc) - return LZ4HC_compress_hashChain(ctx, + int result; + + if (cParam.strat == lz4hc) { + result = LZ4HC_compress_hashChain(ctx, src, dst, srcSizePtr, dstCapacity, cParam.nbSearches, limit, dict); - assert(cParam.strat == lz4opt); - return LZ4HC_compress_optimal(ctx, - src, dst, srcSizePtr, dstCapacity, - cParam.nbSearches, cParam.targetLength, limit, - cLevel == LZ4HC_CLEVEL_MAX, /* ultra mode */ - dict, favor); + } else { + assert(cParam.strat == lz4opt); + result = LZ4HC_compress_optimal(ctx, + src, dst, srcSizePtr, dstCapacity, + cParam.nbSearches, cParam.targetLength, limit, + cLevel == LZ4HC_CLEVEL_MAX, /* ultra mode */ + dict, favor); + } + if (result <= 0) ctx->dirty = 1; + return result; } } @@ -892,16 +898,21 @@ void LZ4_resetStreamHC (LZ4_streamHC_t* LZ4_streamHCPtr, int compressionLevel) LZ4_streamHCPtr->internal_donotuse.base = NULL; LZ4_streamHCPtr->internal_donotuse.dictCtx = NULL; LZ4_streamHCPtr->internal_donotuse.favorDecSpeed = 0; + LZ4_streamHCPtr->internal_donotuse.dirty = 0; LZ4_setCompressionLevel(LZ4_streamHCPtr, compressionLevel); } void LZ4_resetStreamHC_fast (LZ4_streamHC_t* LZ4_streamHCPtr, int compressionLevel) { DEBUGLOG(4, "LZ4_resetStreamHC_fast(%p, %d)", LZ4_streamHCPtr, compressionLevel); - LZ4_streamHCPtr->internal_donotuse.end -= (uptrval)LZ4_streamHCPtr->internal_donotuse.base; - LZ4_streamHCPtr->internal_donotuse.base = NULL; - LZ4_streamHCPtr->internal_donotuse.dictCtx = NULL; - LZ4_setCompressionLevel(LZ4_streamHCPtr, compressionLevel); + if (LZ4_streamHCPtr->internal_donotuse.dirty) { + LZ4_resetStreamHC(LZ4_streamHCPtr, compressionLevel); + } else { + LZ4_streamHCPtr->internal_donotuse.end -= (uptrval)LZ4_streamHCPtr->internal_donotuse.base; + LZ4_streamHCPtr->internal_donotuse.base = NULL; + LZ4_streamHCPtr->internal_donotuse.dictCtx = NULL; + LZ4_setCompressionLevel(LZ4_streamHCPtr, compressionLevel); + } } void LZ4_setCompressionLevel(LZ4_streamHC_t* LZ4_streamHCPtr, int compressionLevel) diff --git a/lib/lz4hc.h b/lib/lz4hc.h index 4c7ba5a..26919af 100644 --- a/lib/lz4hc.h +++ b/lib/lz4hc.h @@ -161,7 +161,9 @@ struct LZ4HC_CCtx_internal uint32_t lowLimit; /* below that point, no more dict */ uint32_t nextToUpdate; /* index from which to continue dictionary update */ short compressionLevel; - short favorDecSpeed; + int8_t favorDecSpeed; /* favor decompression speed if this flag set, + otherwise, favor compression ratio */ + int8_t dirty; /* stream has to be fully reset if this flag is set */ const LZ4HC_CCtx_internal* dictCtx; }; @@ -179,7 +181,9 @@ struct LZ4HC_CCtx_internal unsigned int lowLimit; /* below that point, no more dict */ unsigned int nextToUpdate; /* index from which to continue dictionary update */ short compressionLevel; - short favorDecSpeed; + char favorDecSpeed; /* favor decompression speed if this flag set, + otherwise, favor compression ratio */ + char dirty; /* stream has to be fully reset if this flag is set */ const LZ4HC_CCtx_internal* dictCtx; }; @@ -314,6 +318,11 @@ void LZ4_favorDecompressionSpeed(LZ4_streamHC_t* LZ4_streamHCPtr, int favor); * - the stream was in an indeterminate state and was used in a compression * call that fully reset the state (LZ4_compress_HC_extStateHC()) and that * returned success + * + * Note: + * A stream that was last used in a compression call that returned an error + * may be passed to this function. However, it will be fully reset, which will + * clear any existing history and settings from the context. */ void LZ4_resetStreamHC_fast(LZ4_streamHC_t* LZ4_streamHCPtr, int compressionLevel); diff --git a/tests/fuzzer.c b/tests/fuzzer.c index 95cb092..1d23e1a 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -832,17 +832,20 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c LZ4_setCompressionLevel(&LZ4dictHC, compressionLevel-1); blockContinueCompressedSize = LZ4_compress_HC_continue(&LZ4dictHC, block, compressedBuffer, blockSize, (int)compressedBufferSize); FUZ_CHECKTEST(blockContinueCompressedSize==0, "LZ4_compress_HC_continue failed"); + FUZ_CHECKTEST(LZ4dictHC.internal_donotuse.dirty, "Context should be clean"); FUZ_DISPLAYTEST(); LZ4_loadDictHC(&LZ4dictHC, dict, dictSize); ret = LZ4_compress_HC_continue(&LZ4dictHC, block, compressedBuffer, blockSize, blockContinueCompressedSize-1); FUZ_CHECKTEST(ret>0, "LZ4_compress_HC_continue using ExtDict should fail : one missing byte for output buffer (%i != %i)", ret, blockContinueCompressedSize); + FUZ_CHECKTEST(!LZ4dictHC.internal_donotuse.dirty, "Context should be dirty"); FUZ_DISPLAYTEST(); LZ4_loadDictHC(&LZ4dictHC, dict, dictSize); ret = LZ4_compress_HC_continue(&LZ4dictHC, block, compressedBuffer, blockSize, blockContinueCompressedSize); FUZ_CHECKTEST(ret!=blockContinueCompressedSize, "LZ4_compress_HC_continue size is different (%i != %i)", ret, blockContinueCompressedSize); FUZ_CHECKTEST(ret<=0, "LZ4_compress_HC_continue should work : enough size available within output buffer"); + FUZ_CHECKTEST(LZ4dictHC.internal_donotuse.dirty, "Context should be clean"); FUZ_DISPLAYTEST(); decodedBuffer[blockSize] = 0; @@ -865,19 +868,22 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c LZ4_attach_HC_dictionary(&LZ4_streamHC, &LZ4dictHC); blockContinueCompressedSize = LZ4_compress_HC_continue(&LZ4_streamHC, block, compressedBuffer, blockSize, (int)compressedBufferSize); FUZ_CHECKTEST(blockContinueCompressedSize==0, "LZ4_compress_HC_continue with ExtDictCtx failed"); + FUZ_CHECKTEST(LZ4_streamHC.internal_donotuse.dirty, "Context should be clean"); FUZ_DISPLAYTEST(); - LZ4_resetStreamHC (&LZ4_streamHC, compressionLevel); + LZ4_resetStreamHC_fast (&LZ4_streamHC, compressionLevel); LZ4_attach_HC_dictionary(&LZ4_streamHC, &LZ4dictHC); ret = LZ4_compress_HC_continue(&LZ4_streamHC, block, compressedBuffer, blockSize, blockContinueCompressedSize-1); FUZ_CHECKTEST(ret>0, "LZ4_compress_HC_continue using ExtDictCtx should fail : one missing byte for output buffer (%i != %i)", ret, blockContinueCompressedSize); + FUZ_CHECKTEST(!LZ4_streamHC.internal_donotuse.dirty, "Context should be dirty"); FUZ_DISPLAYTEST(); - LZ4_resetStreamHC (&LZ4_streamHC, compressionLevel); + LZ4_resetStreamHC_fast (&LZ4_streamHC, compressionLevel); LZ4_attach_HC_dictionary(&LZ4_streamHC, &LZ4dictHC); ret = LZ4_compress_HC_continue(&LZ4_streamHC, block, compressedBuffer, blockSize, blockContinueCompressedSize); FUZ_CHECKTEST(ret!=blockContinueCompressedSize, "LZ4_compress_HC_continue using ExtDictCtx size is different (%i != %i)", ret, blockContinueCompressedSize); FUZ_CHECKTEST(ret<=0, "LZ4_compress_HC_continue using ExtDictCtx should work : enough size available within output buffer"); + FUZ_CHECKTEST(LZ4_streamHC.internal_donotuse.dirty, "Context should be clean"); FUZ_DISPLAYTEST(); LZ4_resetStreamHC_fast (&LZ4_streamHC, compressionLevel); @@ -885,6 +891,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c ret = LZ4_compress_HC_continue(&LZ4_streamHC, block, compressedBuffer, blockSize, blockContinueCompressedSize); FUZ_CHECKTEST(ret!=blockContinueCompressedSize, "LZ4_compress_HC_continue using ExtDictCtx and fast reset size is different (%i != %i)", ret, blockContinueCompressedSize); FUZ_CHECKTEST(ret<=0, "LZ4_compress_HC_continue using ExtDictCtx and fast reset should work : enough size available within output buffer"); + FUZ_CHECKTEST(LZ4_streamHC.internal_donotuse.dirty, "Context should be clean"); FUZ_DISPLAYTEST(); decodedBuffer[blockSize] = 0; @@ -1070,6 +1077,7 @@ static void FUZ_unitTests(int compressionLevel) LZ4_resetStreamHC(&sHC, compressionLevel); result = LZ4_compress_HC_continue(&sHC, testInput, testCompressed, testCompressedSize, testCompressedSize-1); FUZ_CHECKTEST(result==0, "LZ4_compressHC_limitedOutput_continue() compression failed"); + FUZ_CHECKTEST(sHC.internal_donotuse.dirty, "Context should be clean"); result = LZ4_decompress_safe(testCompressed, testVerify, result, testCompressedSize); FUZ_CHECKTEST(result!=(int)testCompressedSize, "LZ4_decompress_safe() decompression failed"); @@ -1078,10 +1086,11 @@ static void FUZ_unitTests(int compressionLevel) /* simple dictionary HC compression test */ crcOrig = XXH64(testInput + 64 KB, testCompressedSize, 0); - LZ4_resetStreamHC(&sHC, compressionLevel); + LZ4_resetStreamHC_fast(&sHC, compressionLevel); LZ4_loadDictHC(&sHC, testInput, 64 KB); result = LZ4_compress_HC_continue(&sHC, testInput + 64 KB, testCompressed, testCompressedSize, testCompressedSize-1); FUZ_CHECKTEST(result==0, "LZ4_compressHC_limitedOutput_continue() dictionary compression failed : result = %i", result); + FUZ_CHECKTEST(sHC.internal_donotuse.dirty, "Context should be clean"); result = LZ4_decompress_safe_usingDict(testCompressed, testVerify, result, testCompressedSize, testInput, 64 KB); FUZ_CHECKTEST(result!=(int)testCompressedSize, "LZ4_decompress_safe() simple dictionary decompression test failed"); @@ -1092,12 +1101,14 @@ static void FUZ_unitTests(int compressionLevel) { int result1, result2; int segSize = testCompressedSize / 2; crcOrig = XXH64(testInput + segSize, testCompressedSize, 0); - LZ4_resetStreamHC(&sHC, compressionLevel); + LZ4_resetStreamHC_fast(&sHC, compressionLevel); LZ4_loadDictHC(&sHC, testInput, segSize); result1 = LZ4_compress_HC_continue(&sHC, testInput + segSize, testCompressed, segSize, segSize -1); FUZ_CHECKTEST(result1==0, "LZ4_compressHC_limitedOutput_continue() dictionary compression failed : result = %i", result1); + FUZ_CHECKTEST(sHC.internal_donotuse.dirty, "Context should be clean"); result2 = LZ4_compress_HC_continue(&sHC, testInput + 2*segSize, testCompressed+result1, segSize, segSize-1); FUZ_CHECKTEST(result2==0, "LZ4_compressHC_limitedOutput_continue() dictionary compression failed : result = %i", result2); + FUZ_CHECKTEST(sHC.internal_donotuse.dirty, "Context should be clean"); result = LZ4_decompress_safe_usingDict(testCompressed, testVerify, result1, segSize, testInput, segSize); FUZ_CHECKTEST(result!=segSize, "LZ4_decompress_safe() dictionary decompression part 1 failed"); @@ -1109,10 +1120,11 @@ static void FUZ_unitTests(int compressionLevel) /* remote dictionary HC compression test */ crcOrig = XXH64(testInput + 64 KB, testCompressedSize, 0); - LZ4_resetStreamHC(&sHC, compressionLevel); + LZ4_resetStreamHC_fast(&sHC, compressionLevel); LZ4_loadDictHC(&sHC, testInput, 32 KB); result = LZ4_compress_HC_continue(&sHC, testInput + 64 KB, testCompressed, testCompressedSize, testCompressedSize-1); FUZ_CHECKTEST(result==0, "LZ4_compressHC_limitedOutput_continue() remote dictionary failed : result = %i", result); + FUZ_CHECKTEST(sHC.internal_donotuse.dirty, "Context should be clean"); result = LZ4_decompress_safe_usingDict(testCompressed, testVerify, result, testCompressedSize, testInput, 32 KB); FUZ_CHECKTEST(result!=(int)testCompressedSize, "LZ4_decompress_safe_usingDict() decompression failed following remote dictionary HC compression test"); @@ -1130,7 +1142,7 @@ static void FUZ_unitTests(int compressionLevel) int segSize = (FUZ_rand(&randState) & 8191); int segNb = 1; - LZ4_resetStreamHC(&sHC, compressionLevel); + LZ4_resetStreamHC_fast(&sHC, compressionLevel); LZ4_loadDictHC(&sHC, dict, dictSize); XXH64_reset(&crcOrigState, 0); @@ -1141,6 +1153,7 @@ static void FUZ_unitTests(int compressionLevel) crcOrig = XXH64_digest(&crcOrigState); result = LZ4_compress_HC_continue(&sHC, testInput + segStart, testCompressed, segSize, LZ4_compressBound(segSize)); FUZ_CHECKTEST(result==0, "LZ4_compressHC_limitedOutput_continue() dictionary compression failed : result = %i", result); + FUZ_CHECKTEST(sHC.internal_donotuse.dirty, "Context should be clean"); result = LZ4_decompress_safe_usingDict(testCompressed, dst, result, segSize, dict, dictSize); FUZ_CHECKTEST(result!=segSize, "LZ4_decompress_safe_usingDict() dictionary decompression part %i failed", segNb); @@ -1177,7 +1190,7 @@ static void FUZ_unitTests(int compressionLevel) XXH64_reset(&xxhOrig, 0); XXH64_reset(&xxhNewSafe, 0); XXH64_reset(&xxhNewFast, 0); - LZ4_resetStreamHC(&sHC, compressionLevel); + LZ4_resetStreamHC_fast(&sHC, compressionLevel); LZ4_setStreamDecode(&decodeStateSafe, NULL, 0); LZ4_setStreamDecode(&decodeStateFast, NULL, 0); @@ -1188,6 +1201,7 @@ static void FUZ_unitTests(int compressionLevel) memcpy (ringBuffer + rNext, testInput + iNext, messageSize); compressedSize = LZ4_compress_HC_continue(&sHC, ringBuffer + rNext, testCompressed, messageSize, testCompressedSize-ringBufferSize); FUZ_CHECKTEST(compressedSize==0, "LZ4_compress_HC_continue() compression failed"); + FUZ_CHECKTEST(sHC.internal_donotuse.dirty, "Context should be clean"); result = LZ4_decompress_safe_continue(&decodeStateSafe, testCompressed, testVerify + dNext, compressedSize, messageSize); FUZ_CHECKTEST(result!=(int)messageSize, "ringBuffer : LZ4_decompress_safe_continue() test failed"); @@ -1238,7 +1252,7 @@ static void FUZ_unitTests(int compressionLevel) XXH64_reset(&xxhOrig, 0); XXH64_reset(&xxhNewSafe, 0); XXH64_reset(&xxhNewFast, 0); - LZ4_resetStreamHC(&sHC, compressionLevel); + LZ4_resetStreamHC_fast(&sHC, compressionLevel); LZ4_setStreamDecode(&decodeStateSafe, NULL, 0); LZ4_setStreamDecode(&decodeStateFast, NULL, 0); @@ -1251,6 +1265,7 @@ static void FUZ_unitTests(int compressionLevel) compressedSize = LZ4_compress_HC_continue(&sHC, testInput + iNext, testCompressed, messageSize, testCompressedSize-ringBufferSize); FUZ_CHECKTEST(compressedSize==0, "LZ4_compress_HC_continue() compression failed"); + FUZ_CHECKTEST(sHC.internal_donotuse.dirty, "Context should be clean"); result = LZ4_decompress_safe_continue(&decodeStateSafe, testCompressed, ringBufferSafe + dNext, compressedSize, messageSize); FUZ_CHECKTEST(result!=messageSize, "64K D.ringBuffer : LZ4_decompress_safe_continue() test failed"); @@ -1282,6 +1297,7 @@ static void FUZ_unitTests(int compressionLevel) compressedSize = LZ4_compress_HC_continue(&sHC, testInput + iNext, testCompressed, messageSize, testCompressedSize-ringBufferSize); FUZ_CHECKTEST(compressedSize==0, "LZ4_compress_HC_continue() compression failed"); + FUZ_CHECKTEST(sHC.internal_donotuse.dirty, "Context should be clean"); DISPLAYLEVEL(5, "compressed %i bytes to %i bytes \n", messageSize, compressedSize); /* test LZ4_decompress_safe_continue */ -- cgit v1.2.3 From bf9bf80f8d06aab75347ed6df62758511e91fbe3 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 15 Oct 2018 11:14:30 -0700 Subject: updated code documentation to clarify #589 --- doc/lz4_manual.html | 25 +++++++++++++++---------- doc/lz4frame_manual.html | 22 ++++++++++++++-------- lib/lz4frame.h | 21 +++++++++++++-------- 3 files changed, 42 insertions(+), 26 deletions(-) diff --git a/doc/lz4_manual.html b/doc/lz4_manual.html index 5e04263..4c4734a 100644 --- a/doc/lz4_manual.html +++ b/doc/lz4_manual.html @@ -304,19 +304,22 @@ int LZ4_decompress_fast_usingDict (const char* src, char* dst, int originalSize,
    LZ4LIB_STATIC_API void LZ4_resetStream_fast (LZ4_stream_t* streamPtr);
    -

    Use this, like LZ4_resetStream(), to prepare a context for a new chain of - calls to a streaming API (e.g., LZ4_compress_fast_continue()). +

    Use this to prepare a context for a new chain of calls to a streaming API + (e.g., LZ4_compress_fast_continue()). + + Note: + To stay on the safe side, when LZ4_stream_t is used for the first time, + it should be either created using LZ4_createStream() or + initialized using LZ4_resetStream(). Note: Using this in advance of a non-streaming-compression function is redundant, since they all perform their own custom reset internally. Differences from LZ4_resetStream(): - When an LZ4_stream_t is known to be in a internally coherent state, - it can often be prepared for a new compression with almost no work, - only sometimes falling back to the full, expensive reset - that is always required when the stream is in an indeterminate state - (i.e., the reset performed by LZ4_resetStream()). + When an LZ4_stream_t is known to be in an internally coherent state, + it will be prepared for a new compression with almost no work. + Otherwise, it will fall back to the full, expensive reset. LZ4_streams are guaranteed to be in a valid state when: - returned from LZ4_createStream() @@ -329,9 +332,11 @@ int LZ4_decompress_fast_usingDict (const char* src, char* dst, int originalSize, call that fully reset the state (e.g., LZ4_compress_fast_extState()) and that returned success - When a stream isn't known to be in a valid state, it is not safe to pass to - any fastReset or streaming function. It must first be cleansed by the full - LZ4_resetStream(). + Note: + A stream that was used in a compression call that did not return success + (e.g., LZ4_compress_fast_continue()), can still be passed to this function, + however, it's history is not preserved because of previous compression + failure.


    diff --git a/doc/lz4frame_manual.html b/doc/lz4frame_manual.html index 72e6782..2b16045 100644 --- a/doc/lz4frame_manual.html +++ b/doc/lz4frame_manual.html @@ -157,15 +157,19 @@ LZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_cctx* cctx);


    size_t LZ4F_compressBound(size_t srcSize, const LZ4F_preferences_t* prefsPtr);
    -

    Provides minimum dstCapacity required to guarantee compression success - given a srcSize and preferences, covering worst case scenario. +

    Provides minimum dstCapacity required to guarantee success of + LZ4F_compressUpdate(), given a srcSize and preferences, for a worst case scenario. + When srcSize==0, LZ4F_compressBound() provides an upper bound for LZ4F_flush() and LZ4F_compressEnd() instead. + Note that the result is only valid for a single invocation of LZ4F_compressUpdate(). + When invoking LZ4F_compressUpdate() multiple times, + if the output buffer is gradually filled up instead of emptied and re-used from its start, + one must check if there is enough remaining capacity before each invocation, using LZ4F_compressBound(). + @return is always the same for a srcSize and prefsPtr. prefsPtr is optional : when NULL is provided, preferences will be set to cover worst case scenario. - Estimation is valid for either LZ4F_compressUpdate(), LZ4F_flush() or LZ4F_compressEnd(), - Estimation includes the possibility that internal buffer might already be filled by up to (blockSize-1) bytes. - It also includes frame footer (ending + checksum), which would have to be generated by LZ4F_compressEnd(). - Estimation doesn't include frame header, as it was already generated by LZ4F_compressBegin(). - Result is always the same for a srcSize and prefsPtr, so it can be trusted to size reusable buffers. - When srcSize==0, LZ4F_compressBound() provides an upper bound for LZ4F_flush() and LZ4F_compressEnd() operations. + tech details : + @return includes the possibility that internal buffer might already be filled by up to (blockSize-1) bytes. + It also includes frame footer (ending + checksum), since it might be generated by LZ4F_compressEnd(). + @return doesn't include frame header, as it was already generated by LZ4F_compressBegin().


    @@ -194,6 +198,7 @@ LZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_cctx* cctx); `cOptPtr` is optional : it's possible to provide NULL, all options will be set to default. @return : nb of bytes written into dstBuffer (can be zero, when there is no data stored within cctx) or an error code if it fails (which can be tested using LZ4F_isError()) + Note : LZ4F_flush() is guaranteed to be successful when dstCapacity >= LZ4F_compressBound(0, prefsPtr).


    @@ -206,6 +211,7 @@ LZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_cctx* cctx); `cOptPtr` is optional : NULL can be provided, in which case all options will be set to default. @return : nb of bytes written into dstBuffer, necessarily >= 4 (endMark), or an error code if it fails (which can be tested using LZ4F_isError()) + Note : LZ4F_compressEnd() is guaranteed to be successful when dstCapacity >= LZ4F_compressBound(0, prefsPtr). A successful call to LZ4F_compressEnd() makes `cctx` available again for another compression task.


    diff --git a/lib/lz4frame.h b/lib/lz4frame.h index 599f17e..7c7c34e 100644 --- a/lib/lz4frame.h +++ b/lib/lz4frame.h @@ -248,6 +248,7 @@ LZ4FLIB_API LZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_cctx* cctx); /*---- Compression ----*/ #define LZ4F_HEADER_SIZE_MAX 19 /* LZ4 Frame header size can vary from 7 to 19 bytes */ + /*! LZ4F_compressBegin() : * will write the frame header into dstBuffer. * dstCapacity must be >= LZ4F_HEADER_SIZE_MAX bytes. @@ -260,15 +261,19 @@ LZ4FLIB_API size_t LZ4F_compressBegin(LZ4F_cctx* cctx, const LZ4F_preferences_t* prefsPtr); /*! LZ4F_compressBound() : - * Provides minimum dstCapacity required to guarantee compression success - * given a srcSize and preferences, covering worst case scenario. + * Provides minimum dstCapacity required to guarantee success of + * LZ4F_compressUpdate(), given a srcSize and preferences, for a worst case scenario. + * When srcSize==0, LZ4F_compressBound() provides an upper bound for LZ4F_flush() and LZ4F_compressEnd() instead. + * Note that the result is only valid for a single invocation of LZ4F_compressUpdate(). + * When invoking LZ4F_compressUpdate() multiple times, + * if the output buffer is gradually filled up instead of emptied and re-used from its start, + * one must check if there is enough remaining capacity before each invocation, using LZ4F_compressBound(). + * @return is always the same for a srcSize and prefsPtr. * prefsPtr is optional : when NULL is provided, preferences will be set to cover worst case scenario. - * Estimation is valid for either LZ4F_compressUpdate(), LZ4F_flush() or LZ4F_compressEnd(), - * Estimation includes the possibility that internal buffer might already be filled by up to (blockSize-1) bytes. - * It also includes frame footer (ending + checksum), which would have to be generated by LZ4F_compressEnd(). - * Estimation doesn't include frame header, as it was already generated by LZ4F_compressBegin(). - * Result is always the same for a srcSize and prefsPtr, so it can be trusted to size reusable buffers. - * When srcSize==0, LZ4F_compressBound() provides an upper bound for LZ4F_flush() and LZ4F_compressEnd() operations. + * tech details : + * @return includes the possibility that internal buffer might already be filled by up to (blockSize-1) bytes. + * It also includes frame footer (ending + checksum), since it might be generated by LZ4F_compressEnd(). + * @return doesn't include frame header, as it was already generated by LZ4F_compressBegin(). */ LZ4FLIB_API size_t LZ4F_compressBound(size_t srcSize, const LZ4F_preferences_t* prefsPtr); -- cgit v1.2.3 From 45dc195f8405a1c8dfbb96e597f021df26e193bc Mon Sep 17 00:00:00 2001 From: "W. Felix Handte" Date: Mon, 15 Oct 2018 17:22:37 -0700 Subject: Change Comment and Make LZ4LIB_STATIC_API Available to LZ4HC --- lib/lz4.h | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/lib/lz4.h b/lib/lz4.h index 346f0dc..3478831 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -385,27 +385,26 @@ LZ4LIB_API int LZ4_decompress_safe_usingDict (const char* src, char* dst, int sr LZ4LIB_API int LZ4_decompress_fast_usingDict (const char* src, char* dst, int originalSize, const char* dictStart, int dictSize); -/*^********************************************** +/*^************************************* * !!!!!! STATIC LINKING ONLY !!!!!! - ***********************************************/ + ***************************************/ -/*-************************************ - * Unstable declarations - ************************************** - * Declarations in this section must be considered unstable. - * Their signatures may change, or may be removed in the future. - * They are therefore only safe to depend on - * when the caller is statically linked against the library. - * To access their declarations, define LZ4_STATIC_LINKING_ONLY. - **************************************/ - -#ifdef LZ4_STATIC_LINKING_ONLY - -/* By default, symbols in this section aren't published into shared/dynamic libraries. - * You can override this behavior and force them to be published - * by defining LZ4_PUBLISH_STATIC_FUNCTIONS. - * Use at your own risk. - */ +/*-**************************************************************************** + * Symbols declared in this section must be considered unstable. Their + * signatures or semantics may change, or they may be removed altogether in the + * future. They are therefore only safe to depend on when the caller is + * statically linked against the library. + * + * To protect against unsafe usage, not only are the declarations guarded, the + * definitions are hidden by default when building LZ4 as a shared/dynamic + * library. + * + * In order to access these declarations, define LZ4_STATIC_LINKING_ONLY in + * your application before including LZ4's headers. + * + * In order to make their implementations accessible dynamically, you must + * define LZ4_PUBLISH_STATIC_FUNCTIONS when building the LZ4 library. + ******************************************************************************/ #ifdef LZ4_PUBLISH_STATIC_FUNCTIONS #define LZ4LIB_STATIC_API LZ4LIB_API @@ -413,6 +412,8 @@ LZ4LIB_API int LZ4_decompress_fast_usingDict (const char* src, char* dst, int or #define LZ4LIB_STATIC_API #endif +#ifdef LZ4_STATIC_LINKING_ONLY + /*! LZ4_resetStream_fast() : * Use this to prepare a context for a new chain of calls to a streaming API * (e.g., LZ4_compress_fast_continue()). -- cgit v1.2.3 From 6a2da13cb7adb8da42cbfd7c1d47ea5680009f27 Mon Sep 17 00:00:00 2001 From: "W. Felix Handte" Date: Mon, 15 Oct 2018 17:23:06 -0700 Subject: Make LZ4HC Experimental Functions Dynamically Publishable --- lib/lz4hc.h | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/lib/lz4hc.h b/lib/lz4hc.h index 4c7ba5a..6d9ac90 100644 --- a/lib/lz4hc.h +++ b/lib/lz4hc.h @@ -268,10 +268,11 @@ extern "C" { * or 0 if compression fails. * `srcSizePtr` : value will be updated to indicate how much bytes were read from `src` */ -int LZ4_compress_HC_destSize(void* LZ4HC_Data, - const char* src, char* dst, - int* srcSizePtr, int targetDstSize, - int compressionLevel); +LZ4LIB_STATIC_API int LZ4_compress_HC_destSize( + void* LZ4HC_Data, + const char* src, char* dst, + int* srcSizePtr, int targetDstSize, + int compressionLevel); /*! LZ4_compress_HC_continue_destSize() : v1.8.0 (experimental) * Similar as LZ4_compress_HC_continue(), @@ -282,20 +283,23 @@ int LZ4_compress_HC_destSize(void* LZ4HC_Data, * or 0 if compression fails. * `srcSizePtr` : value will be updated to indicate how much bytes were read from `src`. */ -int LZ4_compress_HC_continue_destSize(LZ4_streamHC_t* LZ4_streamHCPtr, - const char* src, char* dst, - int* srcSizePtr, int targetDstSize); +LZ4LIB_STATIC_API int LZ4_compress_HC_continue_destSize( + LZ4_streamHC_t* LZ4_streamHCPtr, + const char* src, char* dst, + int* srcSizePtr, int targetDstSize); /*! LZ4_setCompressionLevel() : v1.8.0 (experimental) * It's possible to change compression level between 2 invocations of LZ4_compress_HC_continue*() */ -void LZ4_setCompressionLevel(LZ4_streamHC_t* LZ4_streamHCPtr, int compressionLevel); +LZ4LIB_STATIC_API void LZ4_setCompressionLevel( + LZ4_streamHC_t* LZ4_streamHCPtr, int compressionLevel); /*! LZ4_favorDecompressionSpeed() : v1.8.2 (experimental) * Parser will select decisions favoring decompression over compression ratio. * Only work at highest compression settings (level >= LZ4HC_CLEVEL_OPT_MIN) */ -void LZ4_favorDecompressionSpeed(LZ4_streamHC_t* LZ4_streamHCPtr, int favor); +LZ4LIB_STATIC_API void LZ4_favorDecompressionSpeed( + LZ4_streamHC_t* LZ4_streamHCPtr, int favor); /*! LZ4_resetStreamHC_fast() : * When an LZ4_streamHC_t is known to be in a internally coherent state, @@ -315,7 +319,8 @@ void LZ4_favorDecompressionSpeed(LZ4_streamHC_t* LZ4_streamHCPtr, int favor); * call that fully reset the state (LZ4_compress_HC_extStateHC()) and that * returned success */ -void LZ4_resetStreamHC_fast(LZ4_streamHC_t* LZ4_streamHCPtr, int compressionLevel); +LZ4LIB_STATIC_API void LZ4_resetStreamHC_fast( + LZ4_streamHC_t* LZ4_streamHCPtr, int compressionLevel); /*! LZ4_compress_HC_extStateHC_fastReset() : * A variant of LZ4_compress_HC_extStateHC(). @@ -328,7 +333,11 @@ void LZ4_resetStreamHC_fast(LZ4_streamHC_t* LZ4_streamHCPtr, int compressionLeve * LZ4_resetStreamHC_fast() while LZ4_compress_HC_extStateHC() starts with a * call to LZ4_resetStreamHC(). */ -int LZ4_compress_HC_extStateHC_fastReset (void* state, const char* src, char* dst, int srcSize, int dstCapacity, int compressionLevel); +LZ4LIB_STATIC_API int LZ4_compress_HC_extStateHC_fastReset ( + void* state, + const char* src, char* dst, + int srcSize, int dstCapacity, + int compressionLevel); /*! LZ4_attach_HC_dictionary() : * This is an experimental API that allows for the efficient use of a @@ -355,7 +364,9 @@ int LZ4_compress_HC_extStateHC_fastReset (void* state, const char* src, char* ds * stream (and source buffer) must remain in-place / accessible / unchanged * through the lifetime of the stream session. */ -LZ4LIB_API void LZ4_attach_HC_dictionary(LZ4_streamHC_t *working_stream, const LZ4_streamHC_t *dictionary_stream); +LZ4LIB_STATIC_API void LZ4_attach_HC_dictionary( + LZ4_streamHC_t *working_stream, + const LZ4_streamHC_t *dictionary_stream); #if defined (__cplusplus) } -- cgit v1.2.3 From 17f5071e72095d93e146264577ed7621763c2b70 Mon Sep 17 00:00:00 2001 From: Bing Xu Date: Thu, 15 Nov 2018 22:24:25 -0800 Subject: Enable amalgamation of lz4hc.c and lz4.c --- lib/lz4.c | 15 ++++++++++++++- lib/lz4hc.c | 29 +++++++++++++++-------------- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 1521aa2..dd9edcc 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -32,6 +32,13 @@ - LZ4 source repository : https://github.com/lz4/lz4 */ +/* + * LZ4_SRC_INCLUDED: + * Amalgamation flag, whether lz4.c is included + */ +#ifndef LZ4_SRC_INCLUDED +# define LZ4_SRC_INCLUDED 1 +#endif /*-************************************ * Tuning parameters @@ -455,7 +462,13 @@ static const U32 LZ4_skipTrigger = 6; /* Increase this value ==> compression ru /*-************************************ * Local Structures and types **************************************/ -typedef enum { notLimited = 0, limitedOutput = 1, fillOutput = 2 } limitedOutput_directive; +typedef enum { + noLimit = 0, + notLimited = 1, + limitedOutput = 2, + fillOutput = 3, + limitedDestSize = 4 +} limitedOutput_directive; typedef enum { clearedTable = 0, byPtr, byU32, byU16 } tableType_t; /** diff --git a/lib/lz4hc.c b/lib/lz4hc.c index d4180e3..56c8f47 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -61,9 +61,21 @@ # pragma clang diagnostic ignored "-Wunused-function" #endif +/*=== Enums ===*/ +typedef enum { noDictCtx, usingDictCtxHc } dictCtx_directive; +#ifndef LZ4_SRC_INCLUDED +typedef enum { + noLimit = 0, + limitedOutput = 1, + limitedDestSize = 2 +} limitedOutput_directive; +#endif + + #define LZ4_COMMONDEFS_ONLY +#ifndef LZ4_SRC_INCLUDED #include "lz4.c" /* LZ4_count, constants, mem */ - +#endif /*=== Constants ===*/ #define OPTIMAL_ML (int)((ML_MASK-1)+MINMATCH) @@ -79,9 +91,6 @@ static U32 LZ4HC_hashPtr(const void* ptr) { return HASH_FUNCTION(LZ4_read32(ptr)); } -/*=== Enums ===*/ -typedef enum { noDictCtx, usingDictCtx } dictCtx_directive; - /************************************** * HC Compression @@ -346,7 +355,7 @@ LZ4HC_InsertAndGetWiderMatch ( } /* while ((matchIndex>=lowestMatchIndex) && (nbAttempts)) */ - if (dict == usingDictCtx && nbAttempts && ipIndex - lowestMatchIndex < MAX_DISTANCE) { + if (dict == usingDictCtxHc && nbAttempts && ipIndex - lowestMatchIndex < MAX_DISTANCE) { size_t const dictEndOffset = dictCtx->end - dictCtx->base; U32 dictMatchIndex = dictCtx->hashTable[LZ4HC_hashPtr(ip)]; assert(dictEndOffset <= 1 GB); @@ -394,14 +403,6 @@ int LZ4HC_InsertAndFindBestMatch(LZ4HC_CCtx_internal* const hc4, /* Index tabl return LZ4HC_InsertAndGetWiderMatch(hc4, ip, ip, iLimit, MINMATCH-1, matchpos, &uselessPtr, maxNbAttempts, patternAnalysis, 0 /*chainSwap*/, dict, favorCompressionRatio); } - - -typedef enum { - noLimit = 0, - limitedOutput = 1, - limitedDestSize = 2 -} limitedOutput_directive; - /* LZ4HC_encodeSequence() : * @return : 0 if ok, * 1 if buffer issue detected */ @@ -800,7 +801,7 @@ static int LZ4HC_compress_generic_dictCtx ( ctx->compressionLevel = (short)cLevel; return LZ4HC_compress_generic_noDictCtx(ctx, src, dst, srcSizePtr, dstCapacity, cLevel, limit); } else { - return LZ4HC_compress_generic_internal(ctx, src, dst, srcSizePtr, dstCapacity, cLevel, limit, usingDictCtx); + return LZ4HC_compress_generic_internal(ctx, src, dst, srcSizePtr, dstCapacity, cLevel, limit, usingDictCtxHc); } } -- cgit v1.2.3 From a7e8d394c0c76d952e3f02f6f33a1fc60be94de6 Mon Sep 17 00:00:00 2001 From: Bing Xu Date: Fri, 16 Nov 2018 09:12:26 -0800 Subject: [amalgamation] add test --- tests/Makefile | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/Makefile b/tests/Makefile index 989ef85..24dc581 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -152,11 +152,17 @@ endif DD:=dd -test: test-lz4 test-lz4c test-frametest test-fullbench test-fuzzer test-install +test: test-lz4 test-lz4c test-frametest test-fullbench test-fuzzer test-install test-amalgamation test32: CFLAGS+=-m32 test32: test +test-amalgamation: $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c + cat $(LZ4DIR)/lz4.c > lz4_all.c + cat $(LZ4DIR)/lz4hc.c >> lz4_all.c + $(CC) -I$(LZ4DIR) -c lz4_all.c + $(RM) lz4_all.c + test-install: lz4 lib liblz4.pc lz4_root=.. ./test_install.sh -- cgit v1.2.3 From 31ce8b56e5da76887ad2670d4dfe621aff39b688 Mon Sep 17 00:00:00 2001 From: Vincent Torri Date: Sat, 17 Nov 2018 07:44:23 +0100 Subject: Use / instead of \ when accessing files in the dll subdirectory. This allow cross-compilation for Windows on Linux --- lib/Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Makefile b/lib/Makefile index d7c8cb4..f7bd439 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -103,8 +103,8 @@ $(LIBLZ4): $(SRCFILES) ifeq ($(BUILD_SHARED),yes) # can be disabled on command line @echo compiling dynamic library $(LIBVER) ifneq (,$(filter Windows%,$(OS))) - $(Q)$(CC) $(FLAGS) -DLZ4_DLL_EXPORT=1 -shared $^ -o dll\$@.dll - dlltool -D dll\liblz4.dll -d dll\liblz4.def -l dll\liblz4.lib + $(Q)$(CC) $(FLAGS) -DLZ4_DLL_EXPORT=1 -shared $^ -o dll/$@.dll + dlltool -D dll/liblz4.dll -d dll/liblz4.def -l dll/liblz4.lib else $(Q)$(CC) $(FLAGS) -shared $^ -fPIC -fvisibility=hidden $(SONAME_FLAGS) -o $@ @echo creating versioned links -- cgit v1.2.3 From b5e106220ba253fb212670e37d873f7916a88f49 Mon Sep 17 00:00:00 2001 From: Vincent Torri Date: Sat, 17 Nov 2018 14:17:15 +0100 Subject: Add DLLTOOL variable so that one can override dlltool binary This fix cross compilation on linux for Windows --- lib/Makefile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/Makefile b/lib/Makefile index f7bd439..f535435 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -54,6 +54,8 @@ DEBUGFLAGS:= -Wall -Wextra -Wcast-qual -Wcast-align -Wshadow \ CFLAGS += $(DEBUGFLAGS) $(MOREFLAGS) FLAGS = $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) +DLLTOOL ?= dlltool + SRCFILES := $(sort $(wildcard *.c)) @@ -104,7 +106,7 @@ ifeq ($(BUILD_SHARED),yes) # can be disabled on command line @echo compiling dynamic library $(LIBVER) ifneq (,$(filter Windows%,$(OS))) $(Q)$(CC) $(FLAGS) -DLZ4_DLL_EXPORT=1 -shared $^ -o dll/$@.dll - dlltool -D dll/liblz4.dll -d dll/liblz4.def -l dll/liblz4.lib + $(DLLTOOL) -D dll/liblz4.dll -d dll/liblz4.def -l dll/liblz4.lib else $(Q)$(CC) $(FLAGS) -shared $^ -fPIC -fvisibility=hidden $(SONAME_FLAGS) -o $@ @echo creating versioned links -- cgit v1.2.3 From d966844a952d6fa2ff06c1bd920dca4b2e70e6a7 Mon Sep 17 00:00:00 2001 From: Vincent Torri Date: Sun, 18 Nov 2018 13:22:10 +0100 Subject: Add documentation about DLLTOOL variable --- lib/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/README.md b/lib/README.md index 8b3b424..66f1eed 100644 --- a/lib/README.md +++ b/lib/README.md @@ -64,6 +64,7 @@ Other files present in the directory are not source code. There are : - `LICENSE` : contains the BSD license text - `Makefile` : `make` script to compile and install lz4 library (static and dynamic) + - dlltool can be overrided by setting DLLTOOL variable (useful when cross-compiling on Linux for Windows) - `liblz4.pc.in` : for `pkg-config` (used in `make install`) - `README.md` : this file -- cgit v1.2.3 From e057e942157263ed3f55633daa944a757ceffe9d Mon Sep 17 00:00:00 2001 From: Vincent Torri Date: Sun, 18 Nov 2018 21:03:27 +0100 Subject: update lib/README.md --- lib/README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/README.md b/lib/README.md index 66f1eed..018ce40 100644 --- a/lib/README.md +++ b/lib/README.md @@ -46,6 +46,10 @@ by using build macro `LZ4_PUBLISH_STATIC_FUNCTIONS`. DLL can be created using MinGW+MSYS with the `make liblz4` command. This command creates `dll\liblz4.dll` and the import library `dll\liblz4.lib`. +To override the `dlltool` command when cross-compiling on Linux, just set the `DLLTOOL` variable. Example of cross compilation on Linux with mingw-w64 64 bits: +``` +make BUILD_STATIC=no CC=x86_64-w64-mingw32-gcc DLLTOOL=x86_64-w64-mingw32-dlltool OS=Windows_NT +``` The import library is only required with Visual C++. The header files `lz4.h`, `lz4hc.h`, `lz4frame.h` and the dynamic library `dll\liblz4.dll` are required to compile a project using gcc/MinGW. @@ -64,7 +68,6 @@ Other files present in the directory are not source code. There are : - `LICENSE` : contains the BSD license text - `Makefile` : `make` script to compile and install lz4 library (static and dynamic) - - dlltool can be overrided by setting DLLTOOL variable (useful when cross-compiling on Linux for Windows) - `liblz4.pc.in` : for `pkg-config` (used in `make install`) - `README.md` : this file -- cgit v1.2.3 From 0314027051fd931ef4bd06591fdf30ea03a0de3d Mon Sep 17 00:00:00 2001 From: Vincent Torri Date: Tue, 20 Nov 2018 21:08:23 +0100 Subject: Allow installation of lz4 for Windows (MSYS2 or when cross-compiling) --- Makefile | 2 +- lib/Makefile | 15 +++++++++++++-- programs/Makefile | 2 +- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 2f8b85c..091b256 100644 --- a/Makefile +++ b/Makefile @@ -89,7 +89,7 @@ clean: #----------------------------------------------------------------------------- # make install is validated only for Linux, OSX, BSD, Hurd and Solaris targets #----------------------------------------------------------------------------- -ifneq (,$(filter $(shell uname),Linux Darwin GNU/kFreeBSD GNU OpenBSD FreeBSD NetBSD DragonFly SunOS Haiku MidnightBSD)) +ifneq (,$(filter $(shell uname),Linux Darwin GNU/kFreeBSD GNU OpenBSD FreeBSD NetBSD DragonFly SunOS Haiku MidnightBSD MINGW32_NT-6.1 MINGW64_NT-6.1)) HOST_OS = POSIX .PHONY: install uninstall diff --git a/lib/Makefile b/lib/Makefile index f535435..fc96bbd 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -73,7 +73,11 @@ else SHARED_EXT_VER = $(SHARED_EXT).$(LIBVER) endif +ifneq (,$(filter Windows%,$(OS))) +LIBLZ4 = liblz4-$(LIBVER_MAJOR) +else LIBLZ4 = liblz4.$(SHARED_EXT_VER) +endif .PHONY: default default: lib-release @@ -126,7 +130,7 @@ clean: #----------------------------------------------------------------------------- # make install is validated only for Linux, OSX, BSD, Hurd and Solaris targets #----------------------------------------------------------------------------- -ifneq (,$(filter $(shell uname),Linux Darwin GNU/kFreeBSD GNU OpenBSD FreeBSD NetBSD DragonFly SunOS Haiku MidnightBSD)) +ifneq (,$(filter $(shell uname),Linux Darwin GNU/kFreeBSD GNU OpenBSD FreeBSD NetBSD DragonFly SunOS Haiku MidnightBSD MINGW32_NT-6.1 MINGW64_NT-6.1)) .PHONY: listL120 listL120: # extract lines >= 120 characters in *.{c,h}, by Takayuki Matsuoka (note : $$, for Makefile compatibility) @@ -140,6 +144,8 @@ PREFIX ?= /usr/local prefix ?= $(PREFIX) EXEC_PREFIX ?= $(prefix) exec_prefix ?= $(EXEC_PREFIX) +BINDIR ?= $(exec_prefix)/bin +bindir ?= $(BINDIR) LIBDIR ?= $(exec_prefix)/lib libdir ?= $(LIBDIR) INCLUDEDIR ?= $(prefix)/include @@ -170,7 +176,7 @@ liblz4.pc: liblz4.pc.in Makefile $< >$@ install: lib liblz4.pc - $(Q)$(INSTALL) -d -m 755 $(DESTDIR)$(pkgconfigdir)/ $(DESTDIR)$(includedir)/ $(DESTDIR)$(libdir)/ + $(Q)$(INSTALL) -d -m 755 $(DESTDIR)$(pkgconfigdir)/ $(DESTDIR)$(includedir)/ $(DESTDIR)$(libdir)/ $(DESTDIR)$(bindir)/ $(Q)$(INSTALL_DATA) liblz4.pc $(DESTDIR)$(pkgconfigdir)/ @echo Installing libraries ifeq ($(BUILD_STATIC),yes) @@ -178,9 +184,14 @@ ifeq ($(BUILD_STATIC),yes) $(Q)$(INSTALL_DATA) lz4frame_static.h $(DESTDIR)$(includedir)/lz4frame_static.h endif ifeq ($(BUILD_SHARED),yes) +ifneq (,$(filter Windows%,$(OS))) + $(Q)$(INSTALL_PROGRAM) dll/$(LIBLZ4).dll $(DESTDIR)$(bindir) + $(Q)$(INSTALL_PROGRAM) dll/liblz4.lib $(DESTDIR)$(libdir) +else $(Q)$(INSTALL_PROGRAM) liblz4.$(SHARED_EXT_VER) $(DESTDIR)$(libdir) $(Q)ln -sf liblz4.$(SHARED_EXT_VER) $(DESTDIR)$(libdir)/liblz4.$(SHARED_EXT_MAJOR) $(Q)ln -sf liblz4.$(SHARED_EXT_VER) $(DESTDIR)$(libdir)/liblz4.$(SHARED_EXT) +endif endif @echo Installing headers in $(includedir) $(Q)$(INSTALL_DATA) lz4.h $(DESTDIR)$(includedir)/lz4.h diff --git a/programs/Makefile b/programs/Makefile index bd33d9b..8a2ad65 100644 --- a/programs/Makefile +++ b/programs/Makefile @@ -109,7 +109,7 @@ clean: #----------------------------------------------------------------------------- # make install is validated only for Linux, OSX, BSD, Hurd and Solaris targets #----------------------------------------------------------------------------- -ifneq (,$(filter $(shell uname),Linux Darwin GNU/kFreeBSD GNU OpenBSD FreeBSD NetBSD DragonFly SunOS Haiku MidnightBSD)) +ifneq (,$(filter $(shell uname),Linux Darwin GNU/kFreeBSD GNU OpenBSD FreeBSD NetBSD DragonFly SunOS Haiku MidnightBSD MINGW32_NT-6.1 MINGW64_NT-6.1)) unlz4: lz4 ln -s lz4$(EXT) unlz4$(EXT) -- cgit v1.2.3 From 8e8b658bde8aa8d5f6c162d53193b4422554444c Mon Sep 17 00:00:00 2001 From: Vincent Torri Date: Wed, 21 Nov 2018 08:40:51 +0100 Subject: Add explanation for the installation of the DLL in the bin directory --- lib/Makefile | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/Makefile b/lib/Makefile index fc96bbd..74ddd59 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -184,6 +184,9 @@ ifeq ($(BUILD_STATIC),yes) $(Q)$(INSTALL_DATA) lz4frame_static.h $(DESTDIR)$(includedir)/lz4frame_static.h endif ifeq ($(BUILD_SHARED),yes) +# Traditionnally, one installs the DLLs in the bin directory as programs +# search them first in their directory. This allows to not pollute system +# directories (like c:/windows/system32), nor modify the PATH variable. ifneq (,$(filter Windows%,$(OS))) $(Q)$(INSTALL_PROGRAM) dll/$(LIBLZ4).dll $(DESTDIR)$(bindir) $(Q)$(INSTALL_PROGRAM) dll/liblz4.lib $(DESTDIR)$(libdir) -- cgit v1.2.3 From bd2b259760f08f606363732a1c21d7b9331e909e Mon Sep 17 00:00:00 2001 From: Vincent Torri Date: Wed, 21 Nov 2018 09:07:26 +0100 Subject: Uninstall DLL and import lib --- lib/Makefile | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/Makefile b/lib/Makefile index 74ddd59..8a8d618 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -204,9 +204,14 @@ endif uninstall: $(Q)$(RM) $(DESTDIR)$(pkgconfigdir)/liblz4.pc +ifneq (,$(filter Windows%,$(OS))) + $(Q)$(RM) $(DESTDIR)$(bindir)/$(LIBLZ4).dll + $(Q)$(RM) $(DESTDIR)$(libdir)/liblz4.lib +else $(Q)$(RM) $(DESTDIR)$(libdir)/liblz4.$(SHARED_EXT) $(Q)$(RM) $(DESTDIR)$(libdir)/liblz4.$(SHARED_EXT_MAJOR) $(Q)$(RM) $(DESTDIR)$(libdir)/liblz4.$(SHARED_EXT_VER) +endif $(Q)$(RM) $(DESTDIR)$(libdir)/liblz4.a $(Q)$(RM) $(DESTDIR)$(includedir)/lz4.h $(Q)$(RM) $(DESTDIR)$(includedir)/lz4hc.h -- cgit v1.2.3 From ca45db8d4fdb48062281bc309fed9fc1c53df28c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niklas=20Hamb=C3=BCchen?= Date: Sat, 24 Nov 2018 22:13:10 +0100 Subject: lz4_Frame_format.md: Fix typo HexaBytes -> Exabytes --- doc/lz4_Frame_format.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/lz4_Frame_format.md b/doc/lz4_Frame_format.md index 0c98df1..a8541f5 100644 --- a/doc/lz4_Frame_format.md +++ b/doc/lz4_Frame_format.md @@ -213,7 +213,7 @@ __Content Size__ This is the original (uncompressed) size. This information is optional, and only present if the associated flag is set. -Content size is provided using unsigned 8 Bytes, for a maximum of 16 HexaBytes. +Content size is provided using unsigned 8 Bytes, for a maximum of 16 Exabytes. Format is Little endian. This value is informational, typically for display or memory allocation. It can be skipped by a decoder, or used to validate content correctness. -- cgit v1.2.3 From b192c86ba43b3b762e705f4cdd509da3250e1c0c Mon Sep 17 00:00:00 2001 From: Bing Xu Date: Mon, 26 Nov 2018 11:30:15 -0800 Subject: [amalgamation] lz4frame.c --- lib/README.md | 13 +++++++++++++ lib/lz4frame.c | 7 +++++++ tests/Makefile | 1 + 3 files changed, 21 insertions(+) diff --git a/lib/README.md b/lib/README.md index 8b3b424..e3fdbf3 100644 --- a/lib/README.md +++ b/lib/README.md @@ -42,6 +42,19 @@ Should they be nonetheless needed, it's possible to force their publication by using build macro `LZ4_PUBLISH_STATIC_FUNCTIONS`. +#### Amalgamation + +lz4 code is able to be amalgamated into a single file. +We can combine all source code in `lz4_all.c` by using following command, +``` +cat lz4.c > lz4_all.c +cat lz4hc.c >> lz4_all.c +cat lz4frame.c >> lz4_all.c +``` +and compile `lz4_all.c`. +It's necessary to include all `*.h` files present in `/lib` together with `lz4_all.c`. + + #### Windows : using MinGW+MSYS to create DLL DLL can be created using MinGW+MSYS with the `make liblz4` command. diff --git a/lib/lz4frame.c b/lib/lz4frame.c index 357f962..705832d 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -64,10 +64,15 @@ You can contact the author at : **************************************/ #include /* malloc, calloc, free */ #define ALLOC(s) malloc(s) +#ifndef LZ4_SRC_INCLUDED #define ALLOC_AND_ZERO(s) calloc(1,(s)) +#endif #define FREEMEM(p) free(p) #include /* memset, memcpy, memmove */ +#ifndef LZ4_SRC_INCLUDED #define MEM_INIT memset +#endif + /*-************************************ @@ -180,9 +185,11 @@ static void LZ4F_writeLE64 (void* dst, U64 value64) /*-************************************ * Constants **************************************/ +#ifndef LZ4_SRC_INCLUDED #define KB *(1<<10) #define MB *(1<<20) #define GB *(1<<30) +#endif #define _1BIT 0x01 #define _2BITS 0x03 diff --git a/tests/Makefile b/tests/Makefile index 24dc581..8dcef6d 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -160,6 +160,7 @@ test32: test test-amalgamation: $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c cat $(LZ4DIR)/lz4.c > lz4_all.c cat $(LZ4DIR)/lz4hc.c >> lz4_all.c + cat $(LZ4DIR)/lz4frame.c >> lz4_all.c $(CC) -I$(LZ4DIR) -c lz4_all.c $(RM) lz4_all.c -- cgit v1.2.3 From 5bc119455fde697f132adbd7fd50f095954f703b Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Sat, 1 Dec 2018 12:15:53 +0700 Subject: Add meson build system --- contrib/meson/GetLz4LibraryVersion.py | 45 ++++++++ contrib/meson/InstallSymlink.py | 72 +++++++++++++ contrib/meson/contrib/gen_manual/meson.build | 42 ++++++++ contrib/meson/contrib/meson.build | 10 ++ contrib/meson/examples/meson.build | 49 +++++++++ contrib/meson/lib/meson.build | 52 ++++++++++ contrib/meson/meson.build | 147 +++++++++++++++++++++++++++ contrib/meson/meson_options.txt | 24 +++++ contrib/meson/programs/meson.build | 51 ++++++++++ contrib/meson/tests/meson.build | 81 +++++++++++++++ 10 files changed, 573 insertions(+) create mode 100644 contrib/meson/GetLz4LibraryVersion.py create mode 100644 contrib/meson/InstallSymlink.py create mode 100644 contrib/meson/contrib/gen_manual/meson.build create mode 100644 contrib/meson/contrib/meson.build create mode 100644 contrib/meson/examples/meson.build create mode 100644 contrib/meson/lib/meson.build create mode 100644 contrib/meson/meson.build create mode 100644 contrib/meson/meson_options.txt create mode 100644 contrib/meson/programs/meson.build create mode 100644 contrib/meson/tests/meson.build diff --git a/contrib/meson/GetLz4LibraryVersion.py b/contrib/meson/GetLz4LibraryVersion.py new file mode 100644 index 0000000..e929f95 --- /dev/null +++ b/contrib/meson/GetLz4LibraryVersion.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python3 +# ############################################################################# +# Copyright (c) 2018-present lzutao +# All rights reserved. +# +# This source code is licensed under both the BSD-style license (found in the +# LICENSE file in the root directory of this source tree) and the GPLv2 (found +# in the COPYING file in the root directory of this source tree). +# ############################################################################# +import re +import sys + + +def usage(): + print('usage: python3 GetLz4LibraryVersion.py ') + sys.exit(1) + + +def find_version(filepath): + version_file_data = None + with open(filepath) as fd: + version_file_data = fd.read() + + patterns = r"""#\s*define\s+LZ4_VERSION_MAJOR\s+([0-9]+).*$ +#\s*define\s+LZ4_VERSION_MINOR\s+([0-9]+).*$ +#\s*define\s+LZ4_VERSION_RELEASE\s+([0-9]+).*$ +""" + regex = re.compile(patterns, re.MULTILINE) + version_match = regex.search(version_file_data) + if version_match: + return version_match.groups() + raise Exception("Unable to find version string.") + + +def main(): + if len(sys.argv) < 2: + usage() + + filepath = sys.argv[1] + version_tup = find_version(filepath) + print('.'.join(version_tup)) + + +if __name__ == '__main__': + main() diff --git a/contrib/meson/InstallSymlink.py b/contrib/meson/InstallSymlink.py new file mode 100644 index 0000000..d7b1e5a --- /dev/null +++ b/contrib/meson/InstallSymlink.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python3 +# ############################################################################# +# Copyright (c) 2018-present lzutao +# All rights reserved. +# +# This source code is licensed under both the BSD-style license (found in the +# LICENSE file in the root directory of this source tree) and the GPLv2 (found +# in the COPYING file in the root directory of this source tree). +# ############################################################################# +import errno +import os + + +def mkdir_p(path, dir_mode=0o777): + try: + os.makedirs(path, mode=dir_mode) + except OSError as exc: # Python >2.5 + if exc.errno == errno.EEXIST and os.path.isdir(path): + pass + else: + raise + + +def InstallSymlink(src, dst, install_dir, dst_is_dir=False, dir_mode=0o777): + if not os.path.exists(install_dir): + mkdir_p(install_dir, dir_mode) + if not os.path.isdir(install_dir): + raise NotADirectoryError(install_dir) + + new_dst = os.path.join(install_dir, dst) + if os.path.islink(new_dst) and os.readlink(new_dst) == src: + print('File exists: %r -> %r' % (dst, src)) + return + print('Installing symlink %r -> %r' % (new_dst, src)) + os.symlink(src, new_dst, dst_is_dir) + + +def main(): + import argparse + parser = argparse.ArgumentParser(description='Install a symlink.\n', + usage='usage: InstallSymlink.py [-h] [-d] [-m MODE] src dst ' + 'install_dir\n\n' + 'example:\n' + '\tInstallSymlink.py libcrypto.so.1.0.0 libcrypt.so ' + '/usr/lib/x86_64-linux-gnu False') + parser.add_argument('src', help='target to link') + parser.add_argument('dst', help='link name') + parser.add_argument('install_dir', help='installation directory') + parser.add_argument('-d', '--isdir', + action='store_true', + help='dst is a directory') + parser.add_argument('-m', '--mode', + help='directory mode on creating if not exist', + default='0o777') + args = parser.parse_args() + + src = args.src + dst = args.dst + install_dir = args.install_dir + dst_is_dir = args.isdir + dir_mode = int(args.mode, 8) + + DESTDIR = os.environ.get('DESTDIR') + if DESTDIR: + install_dir = DESTDIR + install_dir if os.path.isabs(install_dir) \ + else os.path.join(DESTDIR, install_dir) + + InstallSymlink(src, dst, install_dir, dst_is_dir, dir_mode) + + +if __name__ == '__main__': + main() diff --git a/contrib/meson/contrib/gen_manual/meson.build b/contrib/meson/contrib/gen_manual/meson.build new file mode 100644 index 0000000..6233cdc --- /dev/null +++ b/contrib/meson/contrib/gen_manual/meson.build @@ -0,0 +1,42 @@ +# ############################################################################# +# Copyright (c) 2018-present lzutao +# All rights reserved. +# +# This source code is licensed under both the BSD-style license (found in the +# LICENSE file in the root directory of this source tree) and the GPLv2 (found +# in the COPYING file in the root directory of this source tree). +# ############################################################################# + +lz4_root_dir = '../../../..' + +add_languages('cpp') +cxx = meson.get_compiler('cpp') + +gen_manual_includes = include_directories(join_paths(lz4_root_dir, 'contrib/gen_manual')) + +gen_manual_cppflags = cxx.get_supported_arguments(['-Wextra', '-Wcast-qual', + '-Wcast-align', '-Wshadow', '-Wstrict-aliasing=1', '-Wswitch-enum', + '-Wno-comment']) + +gen_manual = executable('gen_manual', + join_paths(lz4_root_dir, 'contrib/gen_manual/gen_manual.cpp'), + cpp_args: gen_manual_cppflags, + include_directories: gen_manual_includes, + install: false) + +# Update lz4 manual +lz4_manual_html = custom_target('lz4_manual.html', + output : 'lz4_manual.html', + command : [gen_manual, + lz4_version, + join_paths(meson.current_source_dir(), lz4_root_dir, 'lib/lz4.h'), + '@OUTPUT@'], + install : false) +# Update lz4frame manual +lz4_manual_html = custom_target('lz4frame_manual.html', + output : 'lz4frame_manual.html', + command : [gen_manual, + lz4_version, + join_paths(meson.current_source_dir(), lz4_root_dir, 'lib/lz4frame.h'), + '@OUTPUT@'], + install : false) diff --git a/contrib/meson/contrib/meson.build b/contrib/meson/contrib/meson.build new file mode 100644 index 0000000..5249a4c --- /dev/null +++ b/contrib/meson/contrib/meson.build @@ -0,0 +1,10 @@ +# ############################################################################# +# Copyright (c) 2018-present lzutao +# All rights reserved. +# +# This source code is licensed under both the BSD-style license (found in the +# LICENSE file in the root directory of this source tree) and the GPLv2 (found +# in the COPYING file in the root directory of this source tree). +# ############################################################################# + +subdir('gen_manual') diff --git a/contrib/meson/examples/meson.build b/contrib/meson/examples/meson.build new file mode 100644 index 0000000..3c13214 --- /dev/null +++ b/contrib/meson/examples/meson.build @@ -0,0 +1,49 @@ +# ############################################################################# +# Copyright (c) 2018-present lzutao +# All rights reserved. +# +# This source code is licensed under both the BSD-style license (found in the +# LICENSE file in the root directory of this source tree) and the GPLv2 (found +# in the COPYING file in the root directory of this source tree). +# ############################################################################# + +lz4_root_dir = '../../..' + +#examples_c_args = ['-Wextra', '-Wundef', '-Wshadow', '-Wcast-align', '-Wstrict-prototypes'] + +printVersion = executable('printVersion', + join_paths(lz4_root_dir, 'examples/printVersion.c'), + dependencies: liblz4_dep, + install: false) +doubleBuffer = executable('doubleBuffer', + join_paths(lz4_root_dir, 'examples/blockStreaming_doubleBuffer.c'), + dependencies: liblz4_dep, + install: false) +dictionaryRandomAccess = executable('dictionaryRandomAccess', + join_paths(lz4_root_dir, 'examples/dictionaryRandomAccess.c'), + dependencies: liblz4_dep, + install: false) +ringBuffer = executable('ringBuffer', + join_paths(lz4_root_dir, 'examples/blockStreaming_ringBuffer.c'), + dependencies: liblz4_dep, + install: false) +ringBufferHC = executable('ringBufferHC', + join_paths(lz4_root_dir, 'examples/HCStreaming_ringBuffer.c'), + dependencies: liblz4_dep, + install: false) +lineCompress = executable('lineCompress', + join_paths(lz4_root_dir, 'examples/blockStreaming_lineByLine.c'), + dependencies: liblz4_dep, + install: false) +frameCompress = executable('frameCompress', + join_paths(lz4_root_dir, 'examples/frameCompress.c'), + dependencies: liblz4_dep, + install: false) +compressFunctions = executable('compressFunctions', + join_paths(lz4_root_dir, 'examples/compress_functions.c'), + dependencies: liblz4_dep, + install: false) +simpleBuffer = executable('simpleBuffer', + join_paths(lz4_root_dir, 'examples/simple_buffer.c'), + dependencies: liblz4_dep, + install: false) diff --git a/contrib/meson/lib/meson.build b/contrib/meson/lib/meson.build new file mode 100644 index 0000000..3810601 --- /dev/null +++ b/contrib/meson/lib/meson.build @@ -0,0 +1,52 @@ +# ############################################################################# +# Copyright (c) 2018-present lzutao +# All rights reserved. +# +# This source code is licensed under both the BSD-style license (found in the +# LICENSE file in the root directory of this source tree) and the GPLv2 (found +# in the COPYING file in the root directory of this source tree). +# ############################################################################# + +lz4_root_dir = '../../..' + +liblz4_includes = [include_directories(join_paths(lz4_root_dir, 'lib'))] +liblz4_sources = [join_paths(lz4_root_dir, 'lib/lz4.c'), + join_paths(lz4_root_dir, 'lib/lz4frame.c'), + join_paths(lz4_root_dir, 'lib/lz4hc.c'), + join_paths(lz4_root_dir, 'lib/xxhash.c')] +liblz4_c_args = [] + +liblz4_debug_cflags = [] +if use_debug + liblz4_c_args += '-DLZ4_DEBUG=@0@'.format(debug_level) + if [compiler_gcc, compiler_clang].contains(cc_id) + liblz4_debug_cflags = ['-Wextra', '-Wcast-qual', '-Wcast-align', '-Wshadow', + '-Wswitch-enum', '-Wdeclaration-after-statement', '-Wstrict-prototypes', + '-Wundef', '-Wpointer-arith', '-Wstrict-aliasing=1'] + endif +endif +liblz4_c_args += cc.get_supported_arguments(liblz4_debug_cflags) + +liblz4 = library('lz4', + liblz4_sources, + include_directories: liblz4_includes, + c_args: liblz4_c_args, + install: true, + soversion: lz4_libversion) + +liblz4_dep = declare_dependency(link_with: liblz4, + include_directories: liblz4_includes) + +pkgconfig.generate(name: 'lz4', + filebase: 'lz4', + libraries: [liblz4], + description: 'extremely fast lossless compression algorithm library', + version: lz4_libversion, + url: 'http://www.lz4.org/') + +install_headers(join_paths(lz4_root_dir, 'lib/lz4.h'), + join_paths(lz4_root_dir, 'lib/lz4hc.h'), + join_paths(lz4_root_dir, 'lib/lz4frame.h')) +if get_option('default_library') != 'shared' + install_headers(join_paths(lz4_root_dir, 'lib/lz4frame_static.h')) +endif diff --git a/contrib/meson/meson.build b/contrib/meson/meson.build new file mode 100644 index 0000000..f1ca503 --- /dev/null +++ b/contrib/meson/meson.build @@ -0,0 +1,147 @@ +# ############################################################################# +# Copyright (c) 2018-present lzutao +# All rights reserved. +# +# This source code is licensed under both the BSD-style license (found in the +# LICENSE file in the root directory of this source tree) and the GPLv2 (found +# in the COPYING file in the root directory of this source tree). +# ############################################################################# + +project('lz4', ['c'], + license: ['BSD', 'GPLv2'], + default_options : ['c_std=c99', + 'buildtype=release'], + version: '1.8.3', + meson_version: '>=0.47.0') + +cc = meson.get_compiler('c') +pkgconfig = import('pkgconfig') +python3 = import('python').find_installation() + +host_machine_os = host_machine.system() +os_windows = 'windows' +os_linux = 'linux' +os_darwin = 'darwin' +os_freebsd = 'freebsd' +os_sun = 'sunos' + +cc_id = cc.get_id() +compiler_gcc = 'gcc' +compiler_clang = 'clang' +compiler_msvc = 'msvc' + +lz4_version = meson.project_version() +lz4_libversion = '' + +c_std = get_option('c_std') + +# ============================================================================= +# Installation directories +# ============================================================================= + +if host_machine_os == os_windows + lz4_prefix = '.' + lz4_bindir = 'bin' + lz4_datadir = 'share' + lz4_mandir = join_paths(lz4_datadir, 'man') +else + lz4_prefix = get_option('prefix') + lz4_bindir = join_paths(lz4_prefix, get_option('bindir')) + lz4_datadir = join_paths(lz4_prefix, get_option('datadir')) + lz4_mandir = join_paths(lz4_prefix, get_option('mandir')) +endif + +lz4_docdir = join_paths(lz4_datadir, 'doc', meson.project_name()) + +# ============================================================================= +# Project options +# ============================================================================= + +buildtype = get_option('buildtype') + +# Built-in options +use_debug = get_option('debug') + +# Custom options +debug_level = get_option('debug_level') +use_backtrace = get_option('backtrace') + +build_programs = get_option('build_programs') +build_contrib = get_option('build_contrib') +build_tests = get_option('build_tests') +build_examples = get_option('build_examples') +#feature_multi_thread = get_option('multi_thread') + +# ============================================================================= +# Helper scripts for Meson +# ============================================================================= + +GetLz4LibraryVersion_py = files('GetLz4LibraryVersion.py') + +# ============================================================================= +# Getting project version from lz4.h +# ============================================================================= + +lz4_h_file = join_paths(meson.current_source_dir(), 'lib/lz4.h') +r = run_command(python3, GetLz4LibraryVersion_py, lz4_h_file) +if r.returncode() == 0 + output = r.stdout().strip() + if output.version_compare('>@0@'.format(lz4_version)) + lz4_version = output + message('Project version is now: @0@'.format(lz4_version)) + endif +endif + +if host_machine_os != os_windows + lz4_libversion = lz4_version +endif + +# ============================================================================= +# Dependencies +# ============================================================================= + +#libm_dep = cc.find_library('m', required: build_tests) +#thread_dep = dependency('threads', required: feature_multi_thread) +#use_multi_thread = thread_dep.found() + +# ============================================================================= +# Compiler flags +# ============================================================================= + +add_project_arguments(['-DXXH_NAMESPACE=LZ4_'], language: 'c') + +if [compiler_gcc, compiler_clang].contains(cc_id) + common_warning_flags = [] + if buildtype == 'release' + common_warning_flags = ['-Werror'] + endif + if c_std == 'c89' or c_std == 'gnu89' + common_warning_flags += ['-pedantic', '-Wno-long-long', '-Wno-variadic-macros'] + elif c_std == 'c99' or c_std == 'gnu99' + common_warning_flags += ['-pedantic'] + endif + cc_compile_flags = cc.get_supported_arguments(common_warning_flags) + add_project_arguments(cc_compile_flags, language: 'c') +endif + +# ============================================================================= +# Subdirs +# ============================================================================= + +subdir('lib') + +if build_programs + subdir('programs') +endif + +if build_tests + subdir('tests') +endif + +if build_contrib + subdir('contrib') +endif + +if build_examples + subdir('examples') +endif diff --git a/contrib/meson/meson_options.txt b/contrib/meson/meson_options.txt new file mode 100644 index 0000000..f6a4ae7 --- /dev/null +++ b/contrib/meson/meson_options.txt @@ -0,0 +1,24 @@ +# ############################################################################# +# Copyright (c) 2018-present lzutao +# All rights reserved. +# +# This source code is licensed under both the BSD-style license (found in the +# LICENSE file in the root directory of this source tree) and the GPLv2 (found +# in the COPYING file in the root directory of this source tree). +# ############################################################################# + +# Read guidelines from https://wiki.gnome.org/Initiatives/GnomeGoals/MesonPorting + +option('debug_level', type: 'integer', min: 0, max: 7, value: 1, + description: 'Enable run-time debug. See lib/lz4hc.c') +option('backtrace', type: 'boolean', value: false, + description: 'Display a stack backtrace when execution generates a runtime exception') + +option('build_programs', type: 'boolean', value: false, + description: 'Enable programs build') +option('build_tests', type: 'boolean', value: false, + description: 'Enable tests build') +option('build_contrib', type: 'boolean', value: false, + description: 'Enable contrib build') +option('build_examples', type: 'boolean', value: false, + description: 'Enable examples build') diff --git a/contrib/meson/programs/meson.build b/contrib/meson/programs/meson.build new file mode 100644 index 0000000..b5c3228 --- /dev/null +++ b/contrib/meson/programs/meson.build @@ -0,0 +1,51 @@ +# ############################################################################# +# Copyright (c) 2018-present lzutao +# All rights reserved. +# +# This source code is licensed under both the BSD-style license (found in the +# LICENSE file in the root directory of this source tree) and the GPLv2 (found +# in the COPYING file in the root directory of this source tree). +# ############################################################################# + +lz4_root_dir = '../../..' + +lz4_includes = include_directories(join_paths(lz4_root_dir, 'programs')) +lz4_sources = [join_paths(lz4_root_dir, 'programs/bench.c'), + join_paths(lz4_root_dir, 'programs/datagen.c'), + join_paths(lz4_root_dir, 'programs/lz4cli.c'), + join_paths(lz4_root_dir, 'programs/lz4io.c')] +lz4_c_args = [] + +export_dynamic_on_windows = false +# explicit backtrace enable/disable for Linux & Darwin +if not use_backtrace + lz4_c_args += '-DBACKTRACE_ENABLE=0' +elif use_debug and host_machine_os == os_windows # MinGW target + lz4_c_args += '-DBACKTRACE_ENABLE=1' + export_dynamic_on_windows = true +endif + +lz4_deps = [ liblz4_dep ] + +lz4 = executable('lz4', + lz4_sources, + include_directories: lz4_includes, + c_args: lz4_c_args, + dependencies: lz4_deps, + export_dynamic: export_dynamic_on_windows, # Since Meson 0.45.0 + install: true) + +# ============================================================================= +# Programs and manpages installing +# ============================================================================= + +install_man(join_paths(lz4_root_dir, 'programs/lz4.1')) + +InstallSymlink_py = '../InstallSymlink.py' +lz4_man1_dir = join_paths(lz4_mandir, 'man1') +man1_EXT = host_machine_os != os_windows ? '.1.gz' : '.1' + +foreach f : ['lz4c', 'lz4cat', 'unlz4'] + meson.add_install_script(InstallSymlink_py, 'lz4', f, lz4_bindir) + meson.add_install_script(InstallSymlink_py, 'lz4' + man1_EXT, f + man1_EXT, lz4_man1_dir) +endforeach diff --git a/contrib/meson/tests/meson.build b/contrib/meson/tests/meson.build new file mode 100644 index 0000000..82e2813 --- /dev/null +++ b/contrib/meson/tests/meson.build @@ -0,0 +1,81 @@ +# ############################################################################# +# Copyright (c) 2018-present lzutao +# All rights reserved. +# +# This source code is licensed under both the BSD-style license (found in the +# LICENSE file in the root directory of this source tree) and the GPLv2 (found +# in the COPYING file in the root directory of this source tree). +# ############################################################################# + +lz4_root_dir = '../../..' +programs_dir_inc = include_directories(join_paths(lz4_root_dir, 'programs')) +lib_dir_inc = include_directories(join_paths(lz4_root_dir, 'lib')) + +# ============================================================================= +# Test flags +# ============================================================================= + +TEST_FILES = join_paths(lz4_root_dir, 'tests/COPYING') +FUZZER_TIME = '-T90s' +NB_LOOPS = '-i1' + +# ============================================================================= +# Executables +# ============================================================================= + +fullbench_sources = [join_paths(lz4_root_dir, 'tests/fullbench.c')] +fullbench = executable('fullbench', + fullbench_sources, + include_directories: programs_dir_inc, + dependencies: liblz4_dep, + install: false) + +fuzzer_sources = [join_paths(lz4_root_dir, 'tests/fuzzer.c')] +fuzzer = executable('fuzzer', + fuzzer_sources, + c_args: ['-D_DEFAULT_SOURCE', '-D_BSD_SOURCE'], # since glibc 2.19 + include_directories: programs_dir_inc, + dependencies: liblz4_dep, + install: false) + +frametest_sources = [join_paths(lz4_root_dir, 'tests/frametest.c')] +frametest = executable('frametest', + frametest_sources, + include_directories: programs_dir_inc, + dependencies: liblz4_dep, + install: false) + +roundTripTest_sources = [join_paths(lz4_root_dir, 'tests/roundTripTest.c')] +roundTripTest = executable('roundTripTest', + roundTripTest_sources, + dependencies: [ liblz4_dep ], + install: false) + +datagen_sources = [join_paths(lz4_root_dir, 'tests/datagencli.c')] +datagen = executable('datagen', + datagen_sources, + objects: lz4.extract_objects(join_paths(lz4_root_dir, 'programs/datagen.c')), + include_directories: lz4_includes, + dependencies: [ liblz4_dep ], + install: false) + +checkFrame_sources = [join_paths(lz4_root_dir, 'tests/checkFrame.c')] +checkFrame = executable('checkFrame', + checkFrame_sources, + include_directories: programs_dir_inc, + dependencies: [ liblz4_dep ], + install: false) + +checkTag_sources = [join_paths(lz4_root_dir, 'tests/checkTag.c')] +checkTag = executable('checkTag', + checkTag_sources, + include_directories: lib_dir_inc, + install: false) + +# ============================================================================= +# Tests (Use "meson test --list" to list all tests) +# ============================================================================= + +test('test-fullbench', fullbench, args: ['--no-prompt', NB_LOOPS, TEST_FILES]) +test('test-fuzzer', fuzzer, args: [FUZZER_TIME]) +test('test-frametest', frametest, args: [FUZZER_TIME]) -- cgit v1.2.3 From 9f87365de5efbab5d2aee0b7c87bbece71955696 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Sat, 1 Dec 2018 13:12:51 +0700 Subject: Add Travis meson build --- .travis.yml | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/.travis.yml b/.travis.yml index 5b0c6ca..043f932 100644 --- a/.travis.yml +++ b/.travis.yml @@ -156,8 +156,26 @@ matrix: sudo: false env: Cmd="make -C tests checkTag && tests/checkTag $TRAVIS_BRANCH " COMPILER=cc + - dist: xenial + sudo: required + env: BUILD_SYSTEM='meson' + allow_failures: + - env: BUILD_SYSTEM='meson' script: + - if [ "${BUILD_SYSTEM}" = meson ]; then + sudo apt-get install -qq python3 tree + && curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py" + && python3 get-pip.py --user && rm get-pip.py + && pip3 install --user meson ninja + && meson --buildtype=debug -Dauto_features=enabled -Ddefault_library=both + -Dbuild_{programs,contrib,tests,examples}=true contrib/meson build + && cd "$_" + && ninja + && DESTDIR=./staging ninja install + && tree ./staging; + travis_terminate "$?"; + fi - uname -a - echo Cmd=$Cmd - $COMPILER -v -- cgit v1.2.3 From 0241755dbfce0293fa5d7c5d88ef0ad5365e8362 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Sun, 2 Dec 2018 20:12:24 +0700 Subject: Fix clang warnings: -Wformat-pedantic and -Werror=overflow --- tests/fuzzer.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/fuzzer.c b/tests/fuzzer.c index 1d23e1a..9ed3ae9 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -508,7 +508,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c /* Test decoding with a one byte input */ FUZ_DISPLAYTEST("LZ4_decompress_safe() with one byte input"); - { char const tmp = 0xFF; + { char const tmp = (char)0xFF; LZ4_decompress_safe(&tmp, decodedBuffer, 1, blockSize); } @@ -518,7 +518,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c /* 14 bytes of literals, followed by a 14 byte match. * Should not read beyond the end of the buffer. * See https://github.com/lz4/lz4/issues/508. */ - *tmp = 0xEE; + *tmp = (char)0xEE; memset(tmp + 1, 0, 14); tmp[15] = 14; tmp[16] = 0; @@ -678,7 +678,8 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c FUZ_CHECKTEST(ret>0, "LZ4_compress_fast_continue using ExtDict should fail : one missing byte for output buffer : %i written, %i buffer", ret, blockContinueCompressedSize); FUZ_DISPLAYTEST("test LZ4_compress_fast_continue() with dictionary loaded with LZ4_loadDict()"); - DISPLAYLEVEL(5, " compress %i bytes from buffer(%p) into dst(%p) using dict(%p) of size %i \n", blockSize, block, decodedBuffer, dict, dictSize); + DISPLAYLEVEL(5, " compress %i bytes from buffer(%p) into dst(%p) using dict(%p) of size %i \n", + blockSize, (void *)block, (void *)decodedBuffer, (void *)dict, dictSize); LZ4_loadDict(&LZ4dict, dict, dictSize); ret = LZ4_compress_fast_continue(&LZ4dict, block, compressedBuffer, blockSize, blockContinueCompressedSize, 1); FUZ_CHECKTEST(ret!=blockContinueCompressedSize, "LZ4_compress_limitedOutput_compressed size is different (%i != %i)", ret, blockContinueCompressedSize); @@ -686,7 +687,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c /* Decompress with dictionary as external */ FUZ_DISPLAYTEST("test LZ4_decompress_fast_usingDict() with dictionary as extDict"); - DISPLAYLEVEL(5, " decoding %i bytes from buffer(%p) using dict(%p) of size %i \n", blockSize, decodedBuffer, dict, dictSize); + DISPLAYLEVEL(5, " decoding %i bytes from buffer(%p) using dict(%p) of size %i \n", blockSize, (void *)decodedBuffer, (void *)dict, dictSize); decodedBuffer[blockSize] = 0; ret = LZ4_decompress_fast_usingDict(compressedBuffer, decodedBuffer, blockSize, dict, dictSize); FUZ_CHECKTEST(ret!=blockContinueCompressedSize, "LZ4_decompress_fast_usingDict did not read all compressed block input"); -- cgit v1.2.3 From b9d3080d75589d5efa70654ac16c83035a756d71 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Sun, 2 Dec 2018 20:30:25 +0700 Subject: clang: Fix -Wcast-qual --- tests/fuzzer.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/fuzzer.c b/tests/fuzzer.c index 9ed3ae9..fab1fce 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -679,7 +679,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c FUZ_DISPLAYTEST("test LZ4_compress_fast_continue() with dictionary loaded with LZ4_loadDict()"); DISPLAYLEVEL(5, " compress %i bytes from buffer(%p) into dst(%p) using dict(%p) of size %i \n", - blockSize, (void *)block, (void *)decodedBuffer, (void *)dict, dictSize); + blockSize, (const void *)block, (void *)decodedBuffer, (const void *)dict, dictSize); LZ4_loadDict(&LZ4dict, dict, dictSize); ret = LZ4_compress_fast_continue(&LZ4dict, block, compressedBuffer, blockSize, blockContinueCompressedSize, 1); FUZ_CHECKTEST(ret!=blockContinueCompressedSize, "LZ4_compress_limitedOutput_compressed size is different (%i != %i)", ret, blockContinueCompressedSize); @@ -687,7 +687,8 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c /* Decompress with dictionary as external */ FUZ_DISPLAYTEST("test LZ4_decompress_fast_usingDict() with dictionary as extDict"); - DISPLAYLEVEL(5, " decoding %i bytes from buffer(%p) using dict(%p) of size %i \n", blockSize, (void *)decodedBuffer, (void *)dict, dictSize); + DISPLAYLEVEL(5, " decoding %i bytes from buffer(%p) using dict(%p) of size %i \n", + blockSize, (void *)decodedBuffer, (const void *)dict, dictSize); decodedBuffer[blockSize] = 0; ret = LZ4_decompress_fast_usingDict(compressedBuffer, decodedBuffer, blockSize, dict, dictSize); FUZ_CHECKTEST(ret!=blockContinueCompressedSize, "LZ4_decompress_fast_usingDict did not read all compressed block input"); -- cgit v1.2.3 From 49073aca8606131bb73ca1471219d63b27d2602f Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Sun, 2 Dec 2018 20:39:47 +0700 Subject: clang: Fix -Wcomma --- tests/frametest.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/frametest.c b/tests/frametest.c index 6d2cdd0..b93f4fe 100644 --- a/tests/frametest.c +++ b/tests/frametest.c @@ -160,7 +160,7 @@ 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; } -- cgit v1.2.3 From b03714dc80772daeff5a71762fc4018156a258ff Mon Sep 17 00:00:00 2001 From: Vincent Torri Date: Sun, 2 Dec 2018 18:50:18 +0100 Subject: Windows: create import library with gcc directly, remove now useless def file --- lib/Makefile | 5 +---- lib/dll/liblz4.def | 62 ------------------------------------------------------ 2 files changed, 1 insertion(+), 66 deletions(-) delete mode 100644 lib/dll/liblz4.def diff --git a/lib/Makefile b/lib/Makefile index 8a8d618..0e760b4 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -54,8 +54,6 @@ DEBUGFLAGS:= -Wall -Wextra -Wcast-qual -Wcast-align -Wshadow \ CFLAGS += $(DEBUGFLAGS) $(MOREFLAGS) FLAGS = $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -DLLTOOL ?= dlltool - SRCFILES := $(sort $(wildcard *.c)) @@ -109,8 +107,7 @@ $(LIBLZ4): $(SRCFILES) ifeq ($(BUILD_SHARED),yes) # can be disabled on command line @echo compiling dynamic library $(LIBVER) ifneq (,$(filter Windows%,$(OS))) - $(Q)$(CC) $(FLAGS) -DLZ4_DLL_EXPORT=1 -shared $^ -o dll/$@.dll - $(DLLTOOL) -D dll/liblz4.dll -d dll/liblz4.def -l dll/liblz4.lib + $(Q)$(CC) $(FLAGS) -DLZ4_DLL_EXPORT=1 -shared $^ -o dll/$@.dll -Wl,--out-implib,dll/liblz4.lib else $(Q)$(CC) $(FLAGS) -shared $^ -fPIC -fvisibility=hidden $(SONAME_FLAGS) -o $@ @echo creating versioned links diff --git a/lib/dll/liblz4.def b/lib/dll/liblz4.def deleted file mode 100644 index 0ace223..0000000 --- a/lib/dll/liblz4.def +++ /dev/null @@ -1,62 +0,0 @@ -LIBRARY liblz4.dll -EXPORTS - LZ4F_compressBegin - LZ4F_compressBound - LZ4F_compressEnd - LZ4F_compressFrame - LZ4F_compressFrameBound - LZ4F_compressUpdate - LZ4F_createCompressionContext - LZ4F_createDecompressionContext - LZ4F_decompress - LZ4F_flush - LZ4F_freeCompressionContext - LZ4F_freeDecompressionContext - LZ4F_getErrorName - LZ4F_getFrameInfo - LZ4F_getVersion - LZ4F_isError - LZ4_compress - LZ4_compressBound - LZ4_compressHC - LZ4_compressHC_continue - LZ4_compressHC_limitedOutput - LZ4_compressHC_limitedOutput_continue - LZ4_compressHC_limitedOutput_withStateHC - LZ4_compressHC_withStateHC - LZ4_compress_HC - LZ4_compress_HC_continue - LZ4_compress_HC_extStateHC - LZ4_compress_continue - LZ4_compress_default - LZ4_compress_destSize - LZ4_compress_fast - LZ4_compress_fast_continue - LZ4_compress_fast_extState - LZ4_compress_limitedOutput - LZ4_compress_limitedOutput_continue - LZ4_compress_limitedOutput_withState - LZ4_compress_withState - LZ4_createStream - LZ4_createStreamDecode - LZ4_createStreamHC - LZ4_decompress_fast - LZ4_decompress_fast_continue - LZ4_decompress_fast_usingDict - LZ4_decompress_safe - LZ4_decompress_safe_continue - LZ4_decompress_safe_partial - LZ4_decompress_safe_usingDict - LZ4_freeStream - LZ4_freeStreamDecode - LZ4_freeStreamHC - LZ4_loadDict - LZ4_loadDictHC - LZ4_resetStream - LZ4_resetStreamHC - LZ4_saveDict - LZ4_saveDictHC - LZ4_setStreamDecode - LZ4_sizeofState - LZ4_sizeofStateHC - LZ4_versionNumber -- cgit v1.2.3 From 5699432d386062b9088fda27bb0c597062efa666 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Sun, 2 Dec 2018 21:36:09 +0700 Subject: meson test: Update timeout and tests that use COPYING --- contrib/meson/tests/meson.build | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/contrib/meson/tests/meson.build b/contrib/meson/tests/meson.build index 82e2813..392bcf2 100644 --- a/contrib/meson/tests/meson.build +++ b/contrib/meson/tests/meson.build @@ -15,7 +15,7 @@ lib_dir_inc = include_directories(join_paths(lz4_root_dir, 'lib')) # Test flags # ============================================================================= -TEST_FILES = join_paths(lz4_root_dir, 'tests/COPYING') +TEST_FILES = join_paths(meson.current_source_dir(), lz4_root_dir, 'tests/COPYING') FUZZER_TIME = '-T90s' NB_LOOPS = '-i1' @@ -76,6 +76,18 @@ checkTag = executable('checkTag', # Tests (Use "meson test --list" to list all tests) # ============================================================================= -test('test-fullbench', fullbench, args: ['--no-prompt', NB_LOOPS, TEST_FILES]) -test('test-fuzzer', fuzzer, args: [FUZZER_TIME]) -test('test-frametest', frametest, args: [FUZZER_TIME]) +# XXX: (Need TEST) These timeouts (in seconds) when running on a HDD should be +# at least six times bigger than on a SSD + +test('test-fullbench', + fullbench, + args: ['--no-prompt', NB_LOOPS, TEST_FILES], + timeout: 420) # Should enough when running on HDD +test('test-fuzzer', + fuzzer, + args: [FUZZER_TIME], + timeout: 100) +test('test-frametest', + frametest, + args: [FUZZER_TIME], + timeout: 100) -- cgit v1.2.3 From 4fc4b735869bf2cab0e777867a860363d6db5632 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Sun, 2 Dec 2018 22:19:48 +0700 Subject: meson: Use --werror build option instead of passing -Werror [skip ci] --- contrib/meson/meson.build | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/contrib/meson/meson.build b/contrib/meson/meson.build index f1ca503..3c39034 100644 --- a/contrib/meson/meson.build +++ b/contrib/meson/meson.build @@ -112,9 +112,8 @@ add_project_arguments(['-DXXH_NAMESPACE=LZ4_'], language: 'c') if [compiler_gcc, compiler_clang].contains(cc_id) common_warning_flags = [] - if buildtype == 'release' - common_warning_flags = ['-Werror'] - endif + # Should use Meson's own --werror build option + #common_warning_flags += ['-Werror'] if c_std == 'c89' or c_std == 'gnu89' common_warning_flags += ['-pedantic', '-Wno-long-long', '-Wno-variadic-macros'] elif c_std == 'c99' or c_std == 'gnu99' -- cgit v1.2.3 From a9e96d5c5a110e95bef9613ea41fcf26c4254546 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Sun, 2 Dec 2018 22:29:32 +0700 Subject: meson: Fix soversion and generated pkg-config file [skip ci] --- contrib/meson/lib/meson.build | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/contrib/meson/lib/meson.build b/contrib/meson/lib/meson.build index 3810601..7a1b5d5 100644 --- a/contrib/meson/lib/meson.build +++ b/contrib/meson/lib/meson.build @@ -32,13 +32,14 @@ liblz4 = library('lz4', include_directories: liblz4_includes, c_args: liblz4_c_args, install: true, - soversion: lz4_libversion) + version: lz4_libversion, + soversion: '1') liblz4_dep = declare_dependency(link_with: liblz4, include_directories: liblz4_includes) pkgconfig.generate(name: 'lz4', - filebase: 'lz4', + filebase: 'liblz4', libraries: [liblz4], description: 'extremely fast lossless compression algorithm library', version: lz4_libversion, -- cgit v1.2.3 From 4fe675af1cc8eb65f843e06962763dab8c920ce5 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Sun, 2 Dec 2018 22:42:14 +0700 Subject: Use argparse instead of manually parsing [skip ci] --- contrib/meson/GetLz4LibraryVersion.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/contrib/meson/GetLz4LibraryVersion.py b/contrib/meson/GetLz4LibraryVersion.py index e929f95..80b09c9 100644 --- a/contrib/meson/GetLz4LibraryVersion.py +++ b/contrib/meson/GetLz4LibraryVersion.py @@ -11,11 +11,6 @@ import re import sys -def usage(): - print('usage: python3 GetLz4LibraryVersion.py ') - sys.exit(1) - - def find_version(filepath): version_file_data = None with open(filepath) as fd: @@ -33,10 +28,11 @@ def find_version(filepath): def main(): - if len(sys.argv) < 2: - usage() - - filepath = sys.argv[1] + import argparse + parser = argparse.ArgumentParser(description='Print lz4 version from lib/lz4.h') + parser.add_argument('file', help='path to lib/lz4.h') + args = parser.parse_args() + filepath = args.file version_tup = find_version(filepath) print('.'.join(version_tup)) -- cgit v1.2.3 From e2fc6bcd1c098f55ca92467edacbb27b99942b6f Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Mon, 3 Dec 2018 11:02:56 +0700 Subject: meson: Update usage of InstallSymlink helper --- contrib/meson/InstallSymlink.py | 64 +++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 31 deletions(-) diff --git a/contrib/meson/InstallSymlink.py b/contrib/meson/InstallSymlink.py index d7b1e5a..a0858eb 100644 --- a/contrib/meson/InstallSymlink.py +++ b/contrib/meson/InstallSymlink.py @@ -7,42 +7,49 @@ # LICENSE file in the root directory of this source tree) and the GPLv2 (found # in the COPYING file in the root directory of this source tree). # ############################################################################# -import errno + import os +import pathlib # since Python 3.4 + +def prepend_destdir(path): + """prepend_destdir(path) -> Path -def mkdir_p(path, dir_mode=0o777): - try: - os.makedirs(path, mode=dir_mode) - except OSError as exc: # Python >2.5 - if exc.errno == errno.EEXIST and os.path.isdir(path): - pass - else: - raise + Needed because pathlib.Path.joinpath() discards the first path if the + second one is absolute, which is usually the case here. + """ + path = pathlib.Path(path) + DESTDIR = os.environ.get('DESTDIR') + + if DESTDIR: + if not path.is_absolute(): + raise Exception('{!r} must be an absolute path when DESTDIR is set'.format(path)) + path = pathlib.Path(DESTDIR).joinpath(*path.resolve().parts[1:]) + return path -def InstallSymlink(src, dst, install_dir, dst_is_dir=False, dir_mode=0o777): - if not os.path.exists(install_dir): - mkdir_p(install_dir, dir_mode) - if not os.path.isdir(install_dir): - raise NotADirectoryError(install_dir) - new_dst = os.path.join(install_dir, dst) - if os.path.islink(new_dst) and os.readlink(new_dst) == src: - print('File exists: %r -> %r' % (dst, src)) +def install_symlink(src, dst, install_dir, dst_is_dir=False, dir_mode=0o777): + if not install_dir.exists(): + install_dir.mkdir(mode=dir_mode, parents=True, exist_ok=True) + if not install_dir.is_dir(): + raise NotADirectoryError(install_dir) + + new_dst = install_dir.joinpath(dst) + if new_dst.is_symlink() and os.readlink(new_dst) == src: + print('File exists: {!r} -> {!r}'.format(new_dst, src)) return - print('Installing symlink %r -> %r' % (new_dst, src)) - os.symlink(src, new_dst, dst_is_dir) + print('Installing symlink {!r} -> {!r}'.format(new_dst, src)) + new_dst.symlink_to(src, target_is_directory=dst_is_dir) def main(): import argparse - parser = argparse.ArgumentParser(description='Install a symlink.\n', - usage='usage: InstallSymlink.py [-h] [-d] [-m MODE] src dst ' - 'install_dir\n\n' + parser = argparse.ArgumentParser(description='Install a symlink', + usage='InstallSymlink.py [-h] [-d] [-m MODE] src dst install_dir\n\n' 'example:\n' - '\tInstallSymlink.py libcrypto.so.1.0.0 libcrypt.so ' - '/usr/lib/x86_64-linux-gnu False') + '\tInstallSymlink.py dash sh /bin\n' + '\tDESTDIR=./staging InstallSymlink.py dash sh /bin') parser.add_argument('src', help='target to link') parser.add_argument('dst', help='link name') parser.add_argument('install_dir', help='installation directory') @@ -56,16 +63,11 @@ def main(): src = args.src dst = args.dst - install_dir = args.install_dir dst_is_dir = args.isdir dir_mode = int(args.mode, 8) - DESTDIR = os.environ.get('DESTDIR') - if DESTDIR: - install_dir = DESTDIR + install_dir if os.path.isabs(install_dir) \ - else os.path.join(DESTDIR, install_dir) - - InstallSymlink(src, dst, install_dir, dst_is_dir, dir_mode) + install_dir = prepend_destdir(args.install_dir) + install_symlink(src, dst, install_dir, dst_is_dir, dir_mode) if __name__ == '__main__': -- cgit v1.2.3 From 7b6ccfe52c2c21cf7d969e5b4148645b253e2854 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Sun, 2 Dec 2018 22:57:18 +0700 Subject: meson: Use clang for faster build "ninja install" automatically builds the project, no need to use "ninja" --- .travis.yml | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 043f932..66da011 100644 --- a/.travis.yml +++ b/.travis.yml @@ -168,10 +168,14 @@ script: && curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py" && python3 get-pip.py --user && rm get-pip.py && pip3 install --user meson ninja - && meson --buildtype=debug -Dauto_features=enabled -Ddefault_library=both - -Dbuild_{programs,contrib,tests,examples}=true contrib/meson build + && export CC=clang CXX=clang++ + && meson --buildtype=debug + -Db_lundef=false + -Dauto_features=enabled + -Ddefault_library=both + -Dbuild_{programs,contrib,tests,examples}=true + contrib/meson build && cd "$_" - && ninja && DESTDIR=./staging ninja install && tree ./staging; travis_terminate "$?"; -- cgit v1.2.3 From 0caeec939fafd12dd8278f80928c86f496ffc3aa Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Mon, 3 Dec 2018 22:48:25 +0700 Subject: meson: Correct support for Windows build --- contrib/meson/lib/meson.build | 2 +- contrib/meson/meson.build | 57 +++++++++++++------------------------- contrib/meson/programs/meson.build | 5 ++-- 3 files changed, 23 insertions(+), 41 deletions(-) diff --git a/contrib/meson/lib/meson.build b/contrib/meson/lib/meson.build index 7a1b5d5..f4b873e 100644 --- a/contrib/meson/lib/meson.build +++ b/contrib/meson/lib/meson.build @@ -33,7 +33,7 @@ liblz4 = library('lz4', c_args: liblz4_c_args, install: true, version: lz4_libversion, - soversion: '1') + soversion: lz4_libsoversion) liblz4_dep = declare_dependency(link_with: liblz4, include_directories: liblz4_includes) diff --git a/contrib/meson/meson.build b/contrib/meson/meson.build index 3c39034..0047444 100644 --- a/contrib/meson/meson.build +++ b/contrib/meson/meson.build @@ -17,6 +17,7 @@ project('lz4', ['c'], cc = meson.get_compiler('c') pkgconfig = import('pkgconfig') python3 = import('python').find_installation() +c_std = get_option('c_std') host_machine_os = host_machine.system() os_windows = 'windows' @@ -31,26 +32,30 @@ compiler_clang = 'clang' compiler_msvc = 'msvc' lz4_version = meson.project_version() -lz4_libversion = '' -c_std = get_option('c_std') +lz4_h_file = join_paths(meson.current_source_dir(), 'lib/lz4.h') +GetLz4LibraryVersion_py = files('GetLz4LibraryVersion.py') +r = run_command(python3, GetLz4LibraryVersion_py, lz4_h_file) +if r.returncode() == 0 + output = r.stdout().strip() + if output.version_compare('>@0@'.format(lz4_version)) + lz4_version = output + message('Project version is now: @0@'.format(lz4_version)) + endif +endif + +lz4_version_array = lz4_version.split('.') +lz4_libversion = lz4_version +lz4_libsoversion = lz4_version_array[0] # ============================================================================= # Installation directories # ============================================================================= -if host_machine_os == os_windows - lz4_prefix = '.' - lz4_bindir = 'bin' - lz4_datadir = 'share' - lz4_mandir = join_paths(lz4_datadir, 'man') -else - lz4_prefix = get_option('prefix') - lz4_bindir = join_paths(lz4_prefix, get_option('bindir')) - lz4_datadir = join_paths(lz4_prefix, get_option('datadir')) - lz4_mandir = join_paths(lz4_prefix, get_option('mandir')) -endif - +lz4_prefix = get_option('prefix') +lz4_bindir = join_paths(lz4_prefix, get_option('bindir')) +lz4_datadir = join_paths(lz4_prefix, get_option('datadir')) +lz4_mandir = join_paths(lz4_prefix, get_option('mandir')) lz4_docdir = join_paths(lz4_datadir, 'doc', meson.project_name()) # ============================================================================= @@ -72,30 +77,6 @@ build_tests = get_option('build_tests') build_examples = get_option('build_examples') #feature_multi_thread = get_option('multi_thread') -# ============================================================================= -# Helper scripts for Meson -# ============================================================================= - -GetLz4LibraryVersion_py = files('GetLz4LibraryVersion.py') - -# ============================================================================= -# Getting project version from lz4.h -# ============================================================================= - -lz4_h_file = join_paths(meson.current_source_dir(), 'lib/lz4.h') -r = run_command(python3, GetLz4LibraryVersion_py, lz4_h_file) -if r.returncode() == 0 - output = r.stdout().strip() - if output.version_compare('>@0@'.format(lz4_version)) - lz4_version = output - message('Project version is now: @0@'.format(lz4_version)) - endif -endif - -if host_machine_os != os_windows - lz4_libversion = lz4_version -endif - # ============================================================================= # Dependencies # ============================================================================= diff --git a/contrib/meson/programs/meson.build b/contrib/meson/programs/meson.build index b5c3228..2d39b59 100644 --- a/contrib/meson/programs/meson.build +++ b/contrib/meson/programs/meson.build @@ -43,9 +43,10 @@ install_man(join_paths(lz4_root_dir, 'programs/lz4.1')) InstallSymlink_py = '../InstallSymlink.py' lz4_man1_dir = join_paths(lz4_mandir, 'man1') -man1_EXT = host_machine_os != os_windows ? '.1.gz' : '.1' +bin_EXT = host_machine_os == os_windows ? '.exe' : '' +man1_EXT = '.1.gz' # Meson automatically compresses manpages foreach f : ['lz4c', 'lz4cat', 'unlz4'] - meson.add_install_script(InstallSymlink_py, 'lz4', f, lz4_bindir) + meson.add_install_script(InstallSymlink_py, 'lz4' + bin_EXT, f + bin_EXT, lz4_bindir) meson.add_install_script(InstallSymlink_py, 'lz4' + man1_EXT, f + man1_EXT, lz4_man1_dir) endforeach -- cgit v1.2.3 From 2491af993b9baf1ba23c71ef172b10bd4fcf6441 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Tue, 4 Dec 2018 00:56:10 +0700 Subject: meson: Fix wrong path to lib/lz4.h --- contrib/meson/meson.build | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/meson/meson.build b/contrib/meson/meson.build index 0047444..7416a1e 100644 --- a/contrib/meson/meson.build +++ b/contrib/meson/meson.build @@ -33,7 +33,7 @@ compiler_msvc = 'msvc' lz4_version = meson.project_version() -lz4_h_file = join_paths(meson.current_source_dir(), 'lib/lz4.h') +lz4_h_file = join_paths(meson.current_source_dir(), '../../lib/lz4.h') GetLz4LibraryVersion_py = files('GetLz4LibraryVersion.py') r = run_command(python3, GetLz4LibraryVersion_py, lz4_h_file) if r.returncode() == 0 -- cgit v1.2.3 From b4ab8875f9b0dc47444cb1e87b3a31e7157cfc1b Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Tue, 4 Dec 2018 00:59:06 +0700 Subject: meson: Add fallback message when not found lz4.h --- contrib/meson/meson.build | 2 ++ 1 file changed, 2 insertions(+) diff --git a/contrib/meson/meson.build b/contrib/meson/meson.build index 7416a1e..cbabbc6 100644 --- a/contrib/meson/meson.build +++ b/contrib/meson/meson.build @@ -42,6 +42,8 @@ if r.returncode() == 0 lz4_version = output message('Project version is now: @0@'.format(lz4_version)) endif +else + message('Cannot find project version in @0@'.format(lz4_h_file)) endif lz4_version_array = lz4_version.split('.') -- cgit v1.2.3 From 4c43d14891a286affbf2a5b4d57415451410048e Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Tue, 4 Dec 2018 10:18:01 +0700 Subject: Use MESON_INSTALL_DESTDIR_PREFIX instead of DESTDIR --- contrib/meson/InstallSymlink.py | 25 +++++-------------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/contrib/meson/InstallSymlink.py b/contrib/meson/InstallSymlink.py index a0858eb..c1f13f5 100644 --- a/contrib/meson/InstallSymlink.py +++ b/contrib/meson/InstallSymlink.py @@ -12,23 +12,6 @@ import os import pathlib # since Python 3.4 -def prepend_destdir(path): - """prepend_destdir(path) -> Path - - Needed because pathlib.Path.joinpath() discards the first path if the - second one is absolute, which is usually the case here. - """ - path = pathlib.Path(path) - DESTDIR = os.environ.get('DESTDIR') - - if DESTDIR: - if not path.is_absolute(): - raise Exception('{!r} must be an absolute path when DESTDIR is set'.format(path)) - - path = pathlib.Path(DESTDIR).joinpath(*path.resolve().parts[1:]) - return path - - def install_symlink(src, dst, install_dir, dst_is_dir=False, dir_mode=0o777): if not install_dir.exists(): install_dir.mkdir(mode=dir_mode, parents=True, exist_ok=True) @@ -48,8 +31,7 @@ def main(): parser = argparse.ArgumentParser(description='Install a symlink', usage='InstallSymlink.py [-h] [-d] [-m MODE] src dst install_dir\n\n' 'example:\n' - '\tInstallSymlink.py dash sh /bin\n' - '\tDESTDIR=./staging InstallSymlink.py dash sh /bin') + '\tInstallSymlink.py dash sh /bin') parser.add_argument('src', help='target to link') parser.add_argument('dst', help='link name') parser.add_argument('install_dir', help='installation directory') @@ -65,8 +47,11 @@ def main(): dst = args.dst dst_is_dir = args.isdir dir_mode = int(args.mode, 8) + install_dir = pathlib.Path(args.install_dir) - install_dir = prepend_destdir(args.install_dir) + meson_destdir = os.environ.get('MESON_INSTALL_DESTDIR_PREFIX') + if meson_destdir: + install_dir = pathlib.Path(meson_destdir).joinpath(install_dir) install_symlink(src, dst, install_dir, dst_is_dir, dir_mode) -- cgit v1.2.3 From fbc42958c087626edb24592a5ad1eb73bb6ae5cd Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Tue, 4 Dec 2018 10:18:54 +0700 Subject: No need to join prefix and let soversion base on version --- contrib/meson/lib/meson.build | 3 +-- contrib/meson/meson.build | 8 +++----- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/contrib/meson/lib/meson.build b/contrib/meson/lib/meson.build index f4b873e..ff7a703 100644 --- a/contrib/meson/lib/meson.build +++ b/contrib/meson/lib/meson.build @@ -32,8 +32,7 @@ liblz4 = library('lz4', include_directories: liblz4_includes, c_args: liblz4_c_args, install: true, - version: lz4_libversion, - soversion: lz4_libsoversion) + version: lz4_libversion) liblz4_dep = declare_dependency(link_with: liblz4, include_directories: liblz4_includes) diff --git a/contrib/meson/meson.build b/contrib/meson/meson.build index cbabbc6..d618eb4 100644 --- a/contrib/meson/meson.build +++ b/contrib/meson/meson.build @@ -46,18 +46,16 @@ else message('Cannot find project version in @0@'.format(lz4_h_file)) endif -lz4_version_array = lz4_version.split('.') lz4_libversion = lz4_version -lz4_libsoversion = lz4_version_array[0] # ============================================================================= # Installation directories # ============================================================================= lz4_prefix = get_option('prefix') -lz4_bindir = join_paths(lz4_prefix, get_option('bindir')) -lz4_datadir = join_paths(lz4_prefix, get_option('datadir')) -lz4_mandir = join_paths(lz4_prefix, get_option('mandir')) +lz4_bindir = get_option('bindir') +lz4_datadir = get_option('datadir') +lz4_mandir = get_option('mandir') lz4_docdir = join_paths(lz4_datadir, 'doc', meson.project_name()) # ============================================================================= -- cgit v1.2.3 From 535636ff5cca702e4b5eb8e602c0ce70bfcde2c1 Mon Sep 17 00:00:00 2001 From: "W. Felix Handte" Date: Tue, 4 Dec 2018 14:05:11 -0800 Subject: Don't Attach Very Small Dictionaries Fixes a mismatch in behavior between loading into the context (via `LZ4_loadDict()`) a very small (<= 4 bytes) non-contiguous dictionary, versus attaching it with `LZ4_attach_dictionary()`. Before this patch, this divergence could be reproduced by running ``` make -C tests fuzzer MOREFLAGS="-m32" tests/fuzzer -v -s1239 -t3146 ``` Making sure these two paths behave exactly identically is an easy way to test the correctness of the attach path, so it's desirable that this remain an unpolluted, high signal test. --- lib/lz4.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/lz4.c b/lib/lz4.c index dd9edcc..87cbb99 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1271,7 +1271,9 @@ void LZ4_attach_dictionary(LZ4_stream_t *working_stream, const LZ4_stream_t *dic */ LZ4_resetStream_fast(working_stream); - if (dictionary_stream != NULL) { + if (dictionary_stream != NULL + && dictionary_stream->internal_donotuse.dictSize - 1 >= 4 + /* intentional underflow */) { /* If the current offset is zero, we will never look in the * external dictionary context, since there is no value a table * entry can take that indicate a miss. In that case, we need -- cgit v1.2.3 From 4e3accccb221c2e5dac90efec823a4b13fed70ae Mon Sep 17 00:00:00 2001 From: "W. Felix Handte" Date: Wed, 5 Dec 2018 11:24:33 -0800 Subject: Fix Dict Size Test in `LZ4_compress_fast_continue()` Dictionaries don't need to be > 4 bytes, they need to be >= 4 bytes. This test was overly conservative. Also removes the test in `LZ4_attach_dictionary()`. --- lib/lz4.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 87cbb99..53eff2e 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1271,9 +1271,7 @@ void LZ4_attach_dictionary(LZ4_stream_t *working_stream, const LZ4_stream_t *dic */ LZ4_resetStream_fast(working_stream); - if (dictionary_stream != NULL - && dictionary_stream->internal_donotuse.dictSize - 1 >= 4 - /* intentional underflow */) { + if (dictionary_stream != NULL) { /* If the current offset is zero, we will never look in the * external dictionary context, since there is no value a table * entry can take that indicate a miss. In that case, we need @@ -1321,7 +1319,7 @@ int LZ4_compress_fast_continue (LZ4_stream_t* LZ4_stream, const char* source, ch if (acceleration < 1) acceleration = ACCELERATION_DEFAULT; /* invalidate tiny dictionaries */ - if ( (streamPtr->dictSize-1 < 4) /* intentional underflow */ + if ( (streamPtr->dictSize-1 < 4-1) /* intentional underflow */ && (dictEnd != (const BYTE*)source) ) { DEBUGLOG(5, "LZ4_compress_fast_continue: dictSize(%u) at addr:%p is too small", streamPtr->dictSize, streamPtr->dictionary); streamPtr->dictSize = 0; -- cgit v1.2.3 From 58d8055da65f07972bf73162a5477229428b96bc Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Sun, 9 Dec 2018 18:57:49 +0700 Subject: meson: Add README.md for newcomers [skip ci] --- contrib/meson/README.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 contrib/meson/README.md diff --git a/contrib/meson/README.md b/contrib/meson/README.md new file mode 100644 index 0000000..4e55ab9 --- /dev/null +++ b/contrib/meson/README.md @@ -0,0 +1,34 @@ +Meson build system for lz4 +========================== + +Meson is a build system designed to optimize programmer productivity. +It aims to do this by providing simple, out-of-the-box support for +modern software development tools and practices, such as unit tests, +coverage reports, Valgrind, CCache and the like. + +This Meson build system is provided with no guarantee. + +## How to build + +`cd` to this meson directory (`contrib/meson`) + +```sh +meson --buildtype=release -Ddefault_library=shared -Dbuild_programs=true builddir +cd builddir +ninja # to build +ninja install # to install +``` + +You might want to install it in staging directory: + +```sh +DESTDIR=./staging ninja install +``` + +To configure build options, use: + +```sh +meson configure +``` + +See [man meson(1)](https://manpages.debian.org/testing/meson/meson.1.en.html). -- cgit v1.2.3 From bd5869a5aa40e1fb17213ad6f8156d7de923e20a Mon Sep 17 00:00:00 2001 From: Edward Hope-Morley Date: Tue, 11 Dec 2018 17:54:45 +0000 Subject: Add snap packaging support This commit adds a snapcraft.yaml file to allow lz4 to be distributed as a snap (see https://snapcraft.io/ for more info on snaps). Building the Snap ----------------- To build the snap simply install snapcraft and build the snap e.g. on Ubuntu by doing: $ sudo apt install snapcraft $ cd contrib; snapcraft This will build a snap that can be published [1] to e.g. https://snapcraft.io/lz4 If you want to test the snap before publishing you can do: $ snap install .snap --dangerous And that will install it on your local host. You will then have lz4 as a command to run. Installing the Snap ------------------- Once published and released in the snapstore your snap can be installed on any platform that supports snaps by doing: snap install lz4 [1] https://docs.snapcraft.io/releasing-to-the-snap-store --- contrib/snap/README.md | 29 +++++++++++++++++++++++++++++ contrib/snap/snapcraft.yaml | 31 +++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 contrib/snap/README.md create mode 100644 contrib/snap/snapcraft.yaml diff --git a/contrib/snap/README.md b/contrib/snap/README.md new file mode 100644 index 0000000..612d6d7 --- /dev/null +++ b/contrib/snap/README.md @@ -0,0 +1,29 @@ +Snap Packaging +-------------- + +This directory contains the config required to generate a snap package +of lz4. Snaps are universal Linux packages that allow you to easily +build your application from any source and ship it to any Linux +distribution by publishing it to https://snapcraft.io/. A key attribute +of a snap package is that it is (ideally) confined such that it +executes within a controlled environmenti with all its dependencies +bundled with it and does not share dependencies with of from any other +package on the system (with a couple of minor exceptions). + +The basic anatomy and workflow is: + + * ensure snap.snapcraft.yaml is up-to-date e.g. with version info + + * build the snap by installing the snapcraft package and running it + + * push snap/* changes to the repo (excluding any crud generated by a build of course) + + * register yourself as owner of lz4 name in snapstore + + * publish new snap to the snap store + + * install snap by doing 'snap install lz4' on any Linux distro + + * all installed copies of lz4 will be automatically updated to your new version + +For more information on Snaps see https://docs.snapcraft.io and https://forum.snapcraft.io/ diff --git a/contrib/snap/snapcraft.yaml b/contrib/snap/snapcraft.yaml new file mode 100644 index 0000000..2793c0e --- /dev/null +++ b/contrib/snap/snapcraft.yaml @@ -0,0 +1,31 @@ +name: lz4 +version: 1.8.4 +summary: Extremely Fast Compression algorithm +description: > + LZ4 is lossless compression algorithm, providing compression + speed > 500 MB/s per core, scalable with multi-cores CPU. It features an + extremely fast decoder, with speed in multiple GB/s per core, typically + reaching RAM speed limits on multi-core systems. + . + Speed can be tuned dynamically, selecting an "acceleration" factor which + trades compression ratio for faster speed. On the other end, a high + compression derivative, LZ4_HC, is also provided, trading CPU time for + improved compression ratio. All versions feature the same decompression + speed. + . + LZ4 is also compatible with dictionary compression, and can ingest any + input file as dictionary, including those created by Zstandard Dictionary + Builder. (note: only the final 64KB are used). + . + LZ4 library is provided as open-source software using BSD 2-Clause license. +confinement: strict +grade: stable + +apps: + lz4: + command: usr/local/bin/lz4 + plugs: [home] +parts: + lz4: + source: ../ + plugin: make -- cgit v1.2.3 From 54dabaa16c516af67f7d82ddf96d709f6a04f57a Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Thu, 13 Dec 2018 14:27:13 +0700 Subject: meson: Update InstallSymlink.py usage Change default directory mode to 755. --- contrib/meson/InstallSymlink.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/contrib/meson/InstallSymlink.py b/contrib/meson/InstallSymlink.py index c1f13f5..d0f5151 100644 --- a/contrib/meson/InstallSymlink.py +++ b/contrib/meson/InstallSymlink.py @@ -1,12 +1,13 @@ #!/usr/bin/env python3 # ############################################################################# -# Copyright (c) 2018-present lzutao +# Copyright (c) 2018-present lzutao # All rights reserved. # # This source code is licensed under both the BSD-style license (found in the # LICENSE file in the root directory of this source tree) and the GPLv2 (found # in the COPYING file in the root directory of this source tree). # ############################################################################# +# This file should be synced with https://github.com/lzutao/meson-symlink import os import pathlib # since Python 3.4 @@ -29,30 +30,27 @@ def install_symlink(src, dst, install_dir, dst_is_dir=False, dir_mode=0o777): def main(): import argparse parser = argparse.ArgumentParser(description='Install a symlink', - usage='InstallSymlink.py [-h] [-d] [-m MODE] src dst install_dir\n\n' + usage='{0} [-h] [-d] [-m MODE] source dest install_dir\n\n' 'example:\n' - '\tInstallSymlink.py dash sh /bin') - parser.add_argument('src', help='target to link') - parser.add_argument('dst', help='link name') + ' {0} dash sh /bin'.format(pathlib.Path(__file__).name)) + parser.add_argument('source', help='target to link') + parser.add_argument('dest', help='link name') parser.add_argument('install_dir', help='installation directory') parser.add_argument('-d', '--isdir', action='store_true', - help='dst is a directory') + help='dest is a directory') parser.add_argument('-m', '--mode', help='directory mode on creating if not exist', - default='0o777') + default='0o755') args = parser.parse_args() - src = args.src - dst = args.dst - dst_is_dir = args.isdir dir_mode = int(args.mode, 8) install_dir = pathlib.Path(args.install_dir) meson_destdir = os.environ.get('MESON_INSTALL_DESTDIR_PREFIX') if meson_destdir: install_dir = pathlib.Path(meson_destdir).joinpath(install_dir) - install_symlink(src, dst, install_dir, dst_is_dir, dir_mode) + install_symlink(args.source, args.dest, install_dir, args.isdir, dir_mode) if __name__ == '__main__': -- cgit v1.2.3 From 7f8b0175a9edccd8da7b75d298a3a200df6cea5b Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Thu, 13 Dec 2018 14:52:51 +0700 Subject: meson: Update man1 extension in meson 0.49.0 --- contrib/meson/programs/meson.build | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/meson/programs/meson.build b/contrib/meson/programs/meson.build index 2d39b59..df64eb0 100644 --- a/contrib/meson/programs/meson.build +++ b/contrib/meson/programs/meson.build @@ -44,7 +44,7 @@ install_man(join_paths(lz4_root_dir, 'programs/lz4.1')) InstallSymlink_py = '../InstallSymlink.py' lz4_man1_dir = join_paths(lz4_mandir, 'man1') bin_EXT = host_machine_os == os_windows ? '.exe' : '' -man1_EXT = '.1.gz' # Meson automatically compresses manpages +man1_EXT = meson.version().version_compare('>=0.49.0') ? '.1' : '.1.gz' foreach f : ['lz4c', 'lz4cat', 'unlz4'] meson.add_install_script(InstallSymlink_py, 'lz4' + bin_EXT, f + bin_EXT, lz4_bindir) -- cgit v1.2.3 From 34dcc5e16d281a11dbccdb238cefdee678d075d5 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Thu, 13 Dec 2018 18:08:01 +0700 Subject: Simplify logic by setting default value for MESON_INSTALL_DESTDIR_PREFIX --- contrib/meson/InstallSymlink.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/contrib/meson/InstallSymlink.py b/contrib/meson/InstallSymlink.py index d0f5151..3f2998c 100644 --- a/contrib/meson/InstallSymlink.py +++ b/contrib/meson/InstallSymlink.py @@ -45,11 +45,9 @@ def main(): args = parser.parse_args() dir_mode = int(args.mode, 8) - install_dir = pathlib.Path(args.install_dir) - meson_destdir = os.environ.get('MESON_INSTALL_DESTDIR_PREFIX') - if meson_destdir: - install_dir = pathlib.Path(meson_destdir).joinpath(install_dir) + meson_destdir = os.environ.get('MESON_INSTALL_DESTDIR_PREFIX', default='') + install_dir = pathlib.Path(meson_destdir, args.install_dir) install_symlink(args.source, args.dest, install_dir, args.isdir, dir_mode) -- cgit v1.2.3 From e23d0fb908c6180088eb69b76ea27a486df77e2c Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Fri, 14 Dec 2018 11:12:22 +0700 Subject: meson: Remove unused sys import --- contrib/meson/GetLz4LibraryVersion.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/contrib/meson/GetLz4LibraryVersion.py b/contrib/meson/GetLz4LibraryVersion.py index 80b09c9..d8abfcb 100644 --- a/contrib/meson/GetLz4LibraryVersion.py +++ b/contrib/meson/GetLz4LibraryVersion.py @@ -8,10 +8,9 @@ # in the COPYING file in the root directory of this source tree). # ############################################################################# import re -import sys -def find_version(filepath): +def find_version_tuple(filepath): version_file_data = None with open(filepath) as fd: version_file_data = fd.read() @@ -32,9 +31,8 @@ def main(): parser = argparse.ArgumentParser(description='Print lz4 version from lib/lz4.h') parser.add_argument('file', help='path to lib/lz4.h') args = parser.parse_args() - filepath = args.file - version_tup = find_version(filepath) - print('.'.join(version_tup)) + version_tuple = find_version_tuple(args.file) + print('.'.join(version_tuple)) if __name__ == '__main__': -- cgit v1.2.3 From 7a4e04e6a683cab6dd8961c898a7164d3b2ecc7c Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 2 Jan 2019 14:36:12 -0800 Subject: updated LZ4 block format rewording the end of block conditions for clarity and answering related questions. --- doc/lz4_Block_format.md | 69 +++++++++++++++++++++++-------------------------- 1 file changed, 33 insertions(+), 36 deletions(-) diff --git a/doc/lz4_Block_format.md b/doc/lz4_Block_format.md index 5438730..2fb4c19 100644 --- a/doc/lz4_Block_format.md +++ b/doc/lz4_Block_format.md @@ -1,6 +1,6 @@ LZ4 Block Format Description ============================ -Last revised: 2018-04-25. +Last revised: 2018-12-30. Author : Yann Collet @@ -10,7 +10,8 @@ using any programming language. LZ4 is an LZ77-type compressor with a fixed, byte-oriented encoding. There is no entropy encoder back-end nor framing layer. -The latter is assumed to be handled by other parts of the system (see [LZ4 Frame format]). +The latter is assumed to be handled by other parts of the system +(see [LZ4 Frame format]). This design is assumed to favor simplicity and speed. It helps later on for optimizations, compactness, and features. @@ -104,45 +105,41 @@ A common case is an offset of 1, meaning the last byte is repeated `matchlength` times. -Parsing restrictions +End of block restrictions ----------------------- -There are specific parsing rules to respect in order to remain compatible -with assumptions made by the decoder : - -1. The last 5 bytes are always literals. In other words, the last five bytes - from the uncompressed input (or all bytes, if the input has less than five - bytes) must be encoded as literals on behalf of the last sequence. - The last sequence is incomplete, and stops right after the literals. -2. The last match must start at least 12 bytes before end of block. - The last match is part of the penultimate sequence, - since the last sequence stops right after literals. +There are specific rules required to terminate a block. + +1. The last sequence only contains literals. The block ends right after them. +1. The last 5 bytes of input are always literals. + Therefore, the last sequence contains at least 5 bytes, + or all input bytes if input is smaller than 5 bytes + (empty input can be represented with a zero byte, + interpreted as a token without literal and without a match). +2. The last match must start at least 12 bytes before the end of block. + The last match is part of the penultimate sequence. + It is followed by the last sequence, which only contains literals. Note that, as a consequence, blocks < 13 bytes cannot be compressed. -These rules are in place to ensure that the decoder -can speculatively execute copy instructions -without ever reading nor writing beyond provided I/O buffers. - -1. To copy literals from a non-last sequence, an 8-byte copy instruction - can always be safely issued (without reading past the input), - because literals are followed by a 2-byte offset, - and last sequence is at least 1+5 bytes long. -2. Similarly, a match operation can speculatively copy up to 12 bytes - while remaining within output buffer boundaries. - -Empty inputs can be represented with a zero byte, -interpreted as a token without literals and without a match. +These rules are in place to ensure that a compatible decoder +can be designed for speed, issuing speculatively instructions, +while never reading nor writing beyond provided I/O buffers. Additional notes ----------------------- -There is no assumption nor limits to the way the compressor +If the decoder will decompress data from an external source, +it is recommended to ensure that the decoder will not be vulnerable to +buffer overflow manipulations. +Always ensure that read and write operations +remain within the limits of provided buffers. +Test the decoder with fuzzers +to ensure it's resilient to improbable combinations. + +The format makes no assumption nor limits to the way the compressor searches and selects matches within the source data block. -It could be a fast scan, a multi-probe, a full search using BST, -standard hash chains or MMC, well whatever. - -Advanced parsing strategies can also be implemented, such as lazy match, -or full optimal parsing. - -All these trade-off offer distinctive speed/memory/compression advantages. -Whatever the method used by the compressor, its result will be decodable -by any LZ4 decoder if it follows the format specification described above. +Multiple techniques can be considered, +featuring distinct time / performance trade offs. +As long as the format is respected, +the result will be compatible and decodable by any compliant decoder. +An upper compression limit can be reached, +using a technique called "full optimal parsing", at high cpu cost. -- cgit v1.2.3 From ec735ac53e6b216efcbcd411d496dc90b058c570 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 2 Jan 2019 15:02:22 -0800 Subject: updated frame format re-wording non-full blocks, for clarity. --- doc/lz4_Frame_format.md | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/doc/lz4_Frame_format.md b/doc/lz4_Frame_format.md index a8541f5..a0514e0 100644 --- a/doc/lz4_Frame_format.md +++ b/doc/lz4_Frame_format.md @@ -265,20 +265,23 @@ The highest bit is “1” if data in the block is uncompressed. The highest bit is “0” if data in the block is compressed by LZ4. -All other bits give the size, in bytes, of the following data block -(the size does not include the block checksum if present). +All other bits give the size, in bytes, of the following data block. +The size does not include the block checksum if present. Block Size shall never be larger than Block Maximum Size. -Such a thing could happen for incompressible source data. -In such case, such a data block shall be passed in uncompressed format. +Such a thing could potentially happen for non-compressible sources. +In such a case, such data block shall be passed using uncompressed format. __Data__ Where the actual data to decode stands. It might be compressed or not, depending on previous field indications. -Uncompressed size of Data can be any size, up to “block maximum size”. -Note that data block is not necessarily full : -an arbitrary “flush” may happen anytime. Any block can be “partially filled”. + +When compressed, the data must respect the [LZ4 block format specification](https://github.com/lz4/lz4/blob/master/doc/lz4_Block_format.md). + +Note that the block is not necessarily full. +Uncompressed size of data can be any size, up to "Block Maximum Size”, +so it may contain less data than the maximum block size. __Block checksum__ -- cgit v1.2.3 From 660d21272e4c8a0f49db5fc1e6853f08713dff82 Mon Sep 17 00:00:00 2001 From: qiuyangs Date: Sun, 6 Jan 2019 16:29:30 +0800 Subject: lz4hc.c: change (length >> 8) to (length / 255) Every 0xff byte in the compressed block corresponds to a length of 255 (not 256) in the input data. For long repeating sequences, using (length >> 8) may generate bad compressed blocks. --- lib/lz4hc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/lz4hc.c b/lib/lz4hc.c index 56c8f47..86db155 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -437,7 +437,7 @@ LZ4_FORCE_INLINE int LZ4HC_encodeSequence ( /* Encode Literal length */ length = (size_t)(*ip - *anchor); - if ((limit) && ((*op + (length >> 8) + length + (2 + 1 + LASTLITERALS)) > oend)) return 1; /* Check output limit */ + if ((limit) && ((*op + (length / 255) + length + (2 + 1 + LASTLITERALS)) > oend)) return 1; /* Check output limit */ if (length >= RUN_MASK) { size_t len = length - RUN_MASK; *token = (RUN_MASK << ML_BITS); @@ -458,7 +458,7 @@ LZ4_FORCE_INLINE int LZ4HC_encodeSequence ( /* Encode MatchLength */ assert(matchLength >= MINMATCH); length = (size_t)(matchLength - MINMATCH); - if ((limit) && (*op + (length >> 8) + (1 + LASTLITERALS) > oend)) return 1; /* Check output limit */ + if ((limit) && (*op + (length / 255) + (1 + LASTLITERALS) > oend)) return 1; /* Check output limit */ if (length >= ML_MASK) { *token += ML_MASK; length -= ML_MASK; -- cgit v1.2.3 From e6905b581249cfe5ae4df6d9157c757f49686824 Mon Sep 17 00:00:00 2001 From: "Ruben O. Chiavone" Date: Tue, 8 Jan 2019 22:56:04 -0300 Subject: Fix lz4 extension in input filename not causing decompression --- programs/lz4cli.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/programs/lz4cli.c b/programs/lz4cli.c index 3709f50..84499d0 100644 --- a/programs/lz4cli.c +++ b/programs/lz4cli.c @@ -287,6 +287,19 @@ static unsigned longCommandWArg(const char** stringPtr, const char* longCommand) typedef enum { om_auto, om_compress, om_decompress, om_test, om_bench } operationMode_e; +/** determineOpMode() : + * auto-determine operation mode, based on input filename extension + * @return `om_decompress` if input filename has .lz4 extension and `om_compress` otherwise. + */ +static operationMode_e determineOpMode(const char* inputFilename) +{ + size_t const inSize = strlen(inputFilename); + size_t const extSize = strlen(LZ4_EXTENSION); + size_t const extStart= (inSize > extSize) ? inSize-extSize : 0; + if (!strcmp(inputFilename+extStart, LZ4_EXTENSION)) return om_decompress; + else return om_compress; +} + int main(int argc, const char** argv) { int i, @@ -633,11 +646,7 @@ int main(int argc, const char** argv) while ((!output_filename) && (multiple_inputs==0)) { if (!IS_CONSOLE(stdout)) { output_filename=stdoutmark; break; } /* Default to stdout whenever possible (i.e. not a console) */ if (mode == om_auto) { /* auto-determine compression or decompression, based on file extension */ - size_t const inSize = strlen(input_filename); - size_t const extSize = strlen(LZ4_EXTENSION); - size_t const extStart= (inSize > extSize) ? inSize-extSize : 0; - if (!strcmp(input_filename+extStart, LZ4_EXTENSION)) mode = om_decompress; - else mode = om_compress; + mode = determineOpMode(input_filename); } if (mode == om_compress) { /* compression to file */ size_t const l = strlen(input_filename); @@ -675,6 +684,11 @@ int main(int argc, const char** argv) if (!strcmp(output_filename,stdoutmark) && (displayLevel==2)) displayLevel=1; if ((multiple_inputs) && (displayLevel==2)) displayLevel=1; + /* Auto-determine compression or decompression, based on file extension */ + if (mode == om_auto) { + mode = determineOpMode(input_filename); + } + /* IO Stream/File */ LZ4IO_setNotificationLevel(displayLevel); if (ifnIdx == 0) multiple_inputs = 0; -- cgit v1.2.3 From 4c953b46ef0526b4342a9a0890508e0c84208e8d Mon Sep 17 00:00:00 2001 From: "Ruben O. Chiavone" Date: Wed, 9 Jan 2019 01:51:40 -0300 Subject: Add test to cover issue #596 --- tests/Makefile | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/Makefile b/tests/Makefile index 8dcef6d..760fc32 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -284,6 +284,11 @@ test-lz4-basic: lz4 datagen unlz4 lz4cat test "$(shell ./datagen -g20KB | $(LZ4) -c --fast=1 | wc -c)" -eq "$(shell ./datagen -g20KB| $(LZ4) -c --fast| wc -c)" # checks default fast compression is -1 ! $(LZ4) -c --fast=0 tmp-tlb-dg20K # lz4 should fail when fast=0 ! $(LZ4) -c --fast=-1 tmp-tlb-dg20K # lz4 should fail when fast=-1 + # Test for #596 + @echo "TEST" > tmp-tlb-test + $(LZ4) tmp-tlb-test + $(LZ4) tmp-tlb-test.lz4 tmp-tlb-test2 + $(DIFF) -q tmp-tlb-test tmp-tlb-test2 @$(RM) tmp-tlb* -- cgit v1.2.3 From 81937422514f5b6847d1a99cd5315449ec59566e Mon Sep 17 00:00:00 2001 From: Tim Zakian <2895723+tzakian@users.noreply.github.com> Date: Wed, 9 Jan 2019 10:49:49 -0800 Subject: Make LZ4F_getBlockSize public and publis in experimental section --- lib/lz4frame.c | 15 +++++++-------- lib/lz4frame.h | 1 + tests/frametest.c | 22 ++++++++++++++++++++++ 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/lib/lz4frame.c b/lib/lz4frame.c index 705832d..3f81ef0 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -265,22 +265,21 @@ unsigned LZ4F_getVersion(void) { return LZ4F_VERSION; } int LZ4F_compressionLevel_max(void) { return LZ4HC_CLEVEL_MAX; } - -/*-************************************ -* Private functions -**************************************/ -#define MIN(a,b) ( (a) < (b) ? (a) : (b) ) - -static size_t LZ4F_getBlockSize(unsigned blockSizeID) +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; + if (blockSizeID < 4 || blockSizeID > 7) return err0r(LZ4F_ERROR_maxBlockSize_invalid); blockSizeID -= 4; - if (blockSizeID > 3) return err0r(LZ4F_ERROR_maxBlockSize_invalid); return blockSizes[blockSizeID]; } +/*-************************************ +* Private functions +**************************************/ +#define MIN(a,b) ( (a) < (b) ? (a) : (b) ) + static BYTE LZ4F_headerChecksum (const void* header, size_t length) { U32 const xxh = XXH32(header, length, 0); diff --git a/lib/lz4frame.h b/lib/lz4frame.h index 7c7c34e..68f4118 100644 --- a/lib/lz4frame.h +++ b/lib/lz4frame.h @@ -483,6 +483,7 @@ typedef enum { LZ4F_LIST_ERRORS(LZ4F_GENERATE_ENUM) LZ4FLIB_STATIC_API LZ4F_errorCodes LZ4F_getErrorCode(size_t functionResult); +LZ4FLIB_STATIC_API size_t LZ4F_getBlockSize(unsigned); /********************************** * Bulk processing dictionary API diff --git a/tests/frametest.c b/tests/frametest.c index b93f4fe..75fd062 100644 --- a/tests/frametest.c +++ b/tests/frametest.c @@ -658,6 +658,28 @@ int basicTests(U32 seed, double compressibility) CHECK( LZ4F_freeCompressionContext(cctx) ); cctx = NULL; } + DISPLAYLEVEL(3, "getBlockSize test: \n"); + { size_t result; + for (unsigned blockSizeID = 4; blockSizeID < 8; ++blockSizeID) { + result = LZ4F_getBlockSize(blockSizeID); + CHECK(result); + DISPLAYLEVEL(3, "Returned block size of %zu bytes for blockID %u \n", + result, blockSizeID); + } + + /* Test an invalid input that's too large */ + result = LZ4F_getBlockSize(8); + if(!LZ4F_isError(result) || + LZ4F_getErrorCode(result) != LZ4F_ERROR_maxBlockSize_invalid) + goto _output_error; + + /* Test an invalid input that's too small */ + result = LZ4F_getBlockSize(3); + if(!LZ4F_isError(result) || + LZ4F_getErrorCode(result) != LZ4F_ERROR_maxBlockSize_invalid) + goto _output_error; + } + DISPLAYLEVEL(3, "Skippable frame test : \n"); { size_t decodedBufferSize = COMPRESSIBLE_NOISE_LENGTH; -- cgit v1.2.3 From 4ec29b0fab4661d33c50cd7c6cd9da930b302c27 Mon Sep 17 00:00:00 2001 From: Tim Zakian <2895723+tzakian@users.noreply.github.com> Date: Wed, 9 Jan 2019 11:17:46 -0800 Subject: Fix C90 compatibility issue --- tests/frametest.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/frametest.c b/tests/frametest.c index 75fd062..14906a0 100644 --- a/tests/frametest.c +++ b/tests/frametest.c @@ -660,7 +660,8 @@ int basicTests(U32 seed, double compressibility) DISPLAYLEVEL(3, "getBlockSize test: \n"); { size_t result; - for (unsigned blockSizeID = 4; blockSizeID < 8; ++blockSizeID) { + unsigned blockSizeID; + for (blockSizeID = 4; blockSizeID < 8; ++blockSizeID) { result = LZ4F_getBlockSize(blockSizeID); CHECK(result); DISPLAYLEVEL(3, "Returned block size of %zu bytes for blockID %u \n", -- cgit v1.2.3 From 7741c60f9834cb96be26051221236d3b36b525e2 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 9 Jan 2019 12:09:52 -0800 Subject: add a test to check long sequences (#631) the test fails, as intended, since #631 is not merged yet in this branch. --- tests/fuzzer.c | 116 +++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 76 insertions(+), 40 deletions(-) diff --git a/tests/fuzzer.c b/tests/fuzzer.c index fab1fce..33e4753 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -48,6 +48,7 @@ #include /* strcmp */ #include /* clock_t, clock, CLOCKS_PER_SEC */ #include +#include /* INT_MAX */ #if defined(__unix__) && defined(_AIX) # include /* mmap */ #endif @@ -1064,45 +1065,79 @@ static void FUZ_unitTests(int compressionLevel) } /* LZ4 HC streaming tests */ - { LZ4_streamHC_t* sp; - LZ4_streamHC_t sHC; + { LZ4_streamHC_t sHC; /* statically allocated */ U64 crcOrig; int result; /* Allocation test */ - sp = LZ4_createStreamHC(); - FUZ_CHECKTEST(sp==NULL, "LZ4_createStreamHC() allocation failed"); - LZ4_freeStreamHC(sp); + DISPLAYLEVEL(3, " Basic HC allocation : "); + { LZ4_streamHC_t* const sp = LZ4_createStreamHC(); + FUZ_CHECKTEST(sp==NULL, "LZ4_createStreamHC() allocation failed"); + LZ4_freeStreamHC(sp); + } + DISPLAYLEVEL(3, " OK \n"); /* simple HC compression test */ - crcOrig = XXH64(testInput, testCompressedSize, 0); - LZ4_resetStreamHC(&sHC, compressionLevel); - result = LZ4_compress_HC_continue(&sHC, testInput, testCompressed, testCompressedSize, testCompressedSize-1); - FUZ_CHECKTEST(result==0, "LZ4_compressHC_limitedOutput_continue() compression failed"); - FUZ_CHECKTEST(sHC.internal_donotuse.dirty, "Context should be clean"); + DISPLAYLEVEL(3, " Simple HC round-trip : "); + { U64 const crc64 = XXH64(testInput, testCompressedSize, 0); + LZ4_resetStreamHC(&sHC, compressionLevel); + result = LZ4_compress_HC_continue(&sHC, testInput, testCompressed, testCompressedSize, testCompressedSize-1); + FUZ_CHECKTEST(result==0, "LZ4_compressHC_limitedOutput_continue() compression failed"); + FUZ_CHECKTEST(sHC.internal_donotuse.dirty, "Context should be clean"); - result = LZ4_decompress_safe(testCompressed, testVerify, result, testCompressedSize); - FUZ_CHECKTEST(result!=(int)testCompressedSize, "LZ4_decompress_safe() decompression failed"); - { U64 const crcNew = XXH64(testVerify, testCompressedSize, 0); - FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe() decompression corruption"); } + result = LZ4_decompress_safe(testCompressed, testVerify, result, testCompressedSize); + FUZ_CHECKTEST(result!=(int)testCompressedSize, "LZ4_decompress_safe() decompression failed"); + { U64 const crcNew = XXH64(testVerify, testCompressedSize, 0); + FUZ_CHECKTEST(crc64!=crcNew, "LZ4_decompress_safe() decompression corruption"); + } } + DISPLAYLEVEL(3, " OK \n"); + + /* long sequence test */ + DISPLAYLEVEL(3, " Long sequence HC test : "); + { size_t const blockSize = 1 MB; + size_t const targetSize = 4116; /* size carefully selected to trigger an overflow */ + void* const block = malloc(blockSize); + void* const dstBlock = malloc(targetSize+1); + BYTE const sentinel = 101; + int srcSize; + + assert(block != NULL); assert(dstBlock != NULL); + memset(block, 0, blockSize); + ((char*)dstBlock)[targetSize] = sentinel; + + LZ4_resetStreamHC(&sHC, 3); + assert(blockSize < INT_MAX); + srcSize = (int)blockSize; + result = LZ4_compress_HC_destSize(&sHC, block, dstBlock, &srcSize, targetSize, 3); + FUZ_CHECKTEST(result!=0, "LZ4_compress_HC_destSize() : compression must fail !"); + FUZ_CHECKTEST(((char*)dstBlock)[targetSize] != sentinel, "LZ4_compress_HC_destSize()") + + LZ4_resetStreamHC(&sHC, 3); /* make sure the context is clean after the test */ + free(block); + free(dstBlock); + } + DISPLAYLEVEL(3, " OK \n"); /* simple dictionary HC compression test */ - crcOrig = XXH64(testInput + 64 KB, testCompressedSize, 0); - LZ4_resetStreamHC_fast(&sHC, compressionLevel); - LZ4_loadDictHC(&sHC, testInput, 64 KB); - result = LZ4_compress_HC_continue(&sHC, testInput + 64 KB, testCompressed, testCompressedSize, testCompressedSize-1); - FUZ_CHECKTEST(result==0, "LZ4_compressHC_limitedOutput_continue() dictionary compression failed : result = %i", result); - FUZ_CHECKTEST(sHC.internal_donotuse.dirty, "Context should be clean"); - - result = LZ4_decompress_safe_usingDict(testCompressed, testVerify, result, testCompressedSize, testInput, 64 KB); - FUZ_CHECKTEST(result!=(int)testCompressedSize, "LZ4_decompress_safe() simple dictionary decompression test failed"); - { U64 const crcNew = XXH64(testVerify, testCompressedSize, 0); - FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe() simple dictionary decompression test : corruption"); } + DISPLAYLEVEL(3, " HC dictionary compression test : "); + { U64 const crc64 = XXH64(testInput + 64 KB, testCompressedSize, 0); + LZ4_resetStreamHC_fast(&sHC, compressionLevel); + LZ4_loadDictHC(&sHC, testInput, 64 KB); + result = LZ4_compress_HC_continue(&sHC, testInput + 64 KB, testCompressed, testCompressedSize, testCompressedSize-1); + FUZ_CHECKTEST(result==0, "LZ4_compressHC_limitedOutput_continue() dictionary compression failed : result = %i", result); + FUZ_CHECKTEST(sHC.internal_donotuse.dirty, "Context should be clean"); + + result = LZ4_decompress_safe_usingDict(testCompressed, testVerify, result, testCompressedSize, testInput, 64 KB); + FUZ_CHECKTEST(result!=(int)testCompressedSize, "LZ4_decompress_safe() simple dictionary decompression test failed"); + { U64 const crcNew = XXH64(testVerify, testCompressedSize, 0); + FUZ_CHECKTEST(crc64!=crcNew, "LZ4_decompress_safe() simple dictionary decompression test : corruption"); + } } + DISPLAYLEVEL(3, " OK \n"); /* multiple HC compression test with dictionary */ { int result1, result2; int segSize = testCompressedSize / 2; - crcOrig = XXH64(testInput + segSize, testCompressedSize, 0); + U64 const crc64 = XXH64(testInput + segSize, testCompressedSize, 0); LZ4_resetStreamHC_fast(&sHC, compressionLevel); LZ4_loadDictHC(&sHC, testInput, segSize); result1 = LZ4_compress_HC_continue(&sHC, testInput + segSize, testCompressed, segSize, segSize -1); @@ -1116,22 +1151,23 @@ static void FUZ_unitTests(int compressionLevel) FUZ_CHECKTEST(result!=segSize, "LZ4_decompress_safe() dictionary decompression part 1 failed"); result = LZ4_decompress_safe_usingDict(testCompressed+result1, testVerify+segSize, result2, segSize, testInput, 2*segSize); FUZ_CHECKTEST(result!=segSize, "LZ4_decompress_safe() dictionary decompression part 2 failed"); - { U64 const crcNew = XXH64(testVerify, testCompressedSize, 0); - FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe() dictionary decompression corruption"); } - } + { U64 const crcNew = XXH64(testVerify, testCompressedSize, 0); + FUZ_CHECKTEST(crc64!=crcNew, "LZ4_decompress_safe() dictionary decompression corruption"); + } } /* remote dictionary HC compression test */ - crcOrig = XXH64(testInput + 64 KB, testCompressedSize, 0); - LZ4_resetStreamHC_fast(&sHC, compressionLevel); - LZ4_loadDictHC(&sHC, testInput, 32 KB); - result = LZ4_compress_HC_continue(&sHC, testInput + 64 KB, testCompressed, testCompressedSize, testCompressedSize-1); - FUZ_CHECKTEST(result==0, "LZ4_compressHC_limitedOutput_continue() remote dictionary failed : result = %i", result); - FUZ_CHECKTEST(sHC.internal_donotuse.dirty, "Context should be clean"); - - result = LZ4_decompress_safe_usingDict(testCompressed, testVerify, result, testCompressedSize, testInput, 32 KB); - FUZ_CHECKTEST(result!=(int)testCompressedSize, "LZ4_decompress_safe_usingDict() decompression failed following remote dictionary HC compression test"); - { U64 const crcNew = XXH64(testVerify, testCompressedSize, 0); - FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe_usingDict() decompression corruption"); } + { U64 const crc64 = XXH64(testInput + 64 KB, testCompressedSize, 0); + LZ4_resetStreamHC_fast(&sHC, compressionLevel); + LZ4_loadDictHC(&sHC, testInput, 32 KB); + result = LZ4_compress_HC_continue(&sHC, testInput + 64 KB, testCompressed, testCompressedSize, testCompressedSize-1); + FUZ_CHECKTEST(result==0, "LZ4_compressHC_limitedOutput_continue() remote dictionary failed : result = %i", result); + FUZ_CHECKTEST(sHC.internal_donotuse.dirty, "Context should be clean"); + + result = LZ4_decompress_safe_usingDict(testCompressed, testVerify, result, testCompressedSize, testInput, 32 KB); + FUZ_CHECKTEST(result!=(int)testCompressedSize, "LZ4_decompress_safe_usingDict() decompression failed following remote dictionary HC compression test"); + { U64 const crcNew = XXH64(testVerify, testCompressedSize, 0); + FUZ_CHECKTEST(crc64!=crcNew, "LZ4_decompress_safe_usingDict() decompression corruption"); + } } /* multiple HC compression with ext. dictionary */ { XXH64_state_t crcOrigState; -- cgit v1.2.3 From fbebf0345dde5855837460593df0f84931b20fc4 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 9 Jan 2019 12:41:03 -0800 Subject: minor explicit cast warning --- tests/fuzzer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/fuzzer.c b/tests/fuzzer.c index 33e4753..56535b7 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -1108,7 +1108,7 @@ static void FUZ_unitTests(int compressionLevel) LZ4_resetStreamHC(&sHC, 3); assert(blockSize < INT_MAX); srcSize = (int)blockSize; - result = LZ4_compress_HC_destSize(&sHC, block, dstBlock, &srcSize, targetSize, 3); + result = LZ4_compress_HC_destSize(&sHC, block, dstBlock, &srcSize, (int)targetSize, 3); FUZ_CHECKTEST(result!=0, "LZ4_compress_HC_destSize() : compression must fail !"); FUZ_CHECKTEST(((char*)dstBlock)[targetSize] != sentinel, "LZ4_compress_HC_destSize()") -- cgit v1.2.3 From baed01a9c7703a6d8a68f45b2948a1e84de555e1 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 9 Jan 2019 13:38:33 -0800 Subject: fixed long sequence overflow test --- tests/fuzzer.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/fuzzer.c b/tests/fuzzer.c index 56535b7..573703c 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -1108,8 +1108,10 @@ static void FUZ_unitTests(int compressionLevel) LZ4_resetStreamHC(&sHC, 3); assert(blockSize < INT_MAX); srcSize = (int)blockSize; + assert(targetSize < INT_MAX); result = LZ4_compress_HC_destSize(&sHC, block, dstBlock, &srcSize, (int)targetSize, 3); - FUZ_CHECKTEST(result!=0, "LZ4_compress_HC_destSize() : compression must fail !"); + DISPLAYLEVEL(4, "cSize=%i; readSize=%i; ", result, srcSize); + FUZ_CHECKTEST(result!=4116, "LZ4_compress_HC_destSize() : compression must fill dstBuffer completely, but no more !"); FUZ_CHECKTEST(((char*)dstBlock)[targetSize] != sentinel, "LZ4_compress_HC_destSize()") LZ4_resetStreamHC(&sHC, 3); /* make sure the context is clean after the test */ -- cgit v1.2.3 From 81441e246223e91ccc3ce1e146df8874ec9c526e Mon Sep 17 00:00:00 2001 From: Tim Zakian <2895723+tzakian@users.noreply.github.com> Date: Wed, 9 Jan 2019 13:42:12 -0800 Subject: Make fact that certain variables that are passed into LZ4HC_encodeSequence are changed by the function call --- lib/lz4hc.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/lz4hc.c b/lib/lz4hc.c index 86db155..129bf0c 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -88,6 +88,8 @@ typedef enum { #define HASH_FUNCTION(i) (((i) * 2654435761U) >> ((MINMATCH*8)-LZ4HC_HASH_LOG)) #define DELTANEXTMAXD(p) chainTable[(p) & LZ4HC_MAXD_MASK] /* flexible, LZ4HC_MAXD dependent */ #define DELTANEXTU16(table, pos) table[(U16)(pos)] /* faster */ +/* Make fields passed to, and updated by LZ4HC_encodeSequence explicit */ +#define UPDATABLE(ip, op, anchor) &ip, &op, &anchor static U32 LZ4HC_hashPtr(const void* ptr) { return HASH_FUNCTION(LZ4_read32(ptr)); } @@ -533,7 +535,7 @@ _Search2: if (ml2 == ml) { /* No better match => encode ML1 */ optr = op; - if (LZ4HC_encodeSequence(&ip, &op, &anchor, ml, ref, limit, oend)) goto _dest_overflow; + if (LZ4HC_encodeSequence(UPDATABLE(ip, op, anchor), ml, ref, limit, oend)) goto _dest_overflow; continue; } @@ -581,10 +583,10 @@ _Search3: if (start2 < ip+ml) ml = (int)(start2 - ip); /* Now, encode 2 sequences */ optr = op; - if (LZ4HC_encodeSequence(&ip, &op, &anchor, ml, ref, limit, oend)) goto _dest_overflow; + if (LZ4HC_encodeSequence(UPDATABLE(ip, op, anchor), ml, ref, limit, oend)) goto _dest_overflow; ip = start2; optr = op; - if (LZ4HC_encodeSequence(&ip, &op, &anchor, ml2, ref2, limit, oend)) goto _dest_overflow; + if (LZ4HC_encodeSequence(UPDATABLE(ip, op, anchor), ml2, ref2, limit, oend)) goto _dest_overflow; continue; } @@ -603,7 +605,7 @@ _Search3: } optr = op; - if (LZ4HC_encodeSequence(&ip, &op, &anchor, ml, ref, limit, oend)) goto _dest_overflow; + if (LZ4HC_encodeSequence(UPDATABLE(ip, op, anchor), ml, ref, limit, oend)) goto _dest_overflow; ip = start3; ref = ref3; ml = ml3; @@ -641,7 +643,7 @@ _Search3: } } optr = op; - if (LZ4HC_encodeSequence(&ip, &op, &anchor, ml, ref, limit, oend)) goto _dest_overflow; + if (LZ4HC_encodeSequence(UPDATABLE(ip, op, anchor), ml, ref, limit, oend)) goto _dest_overflow; /* ML2 becomes ML1 */ ip = start2; ref = ref2; ml = ml2; @@ -1206,7 +1208,7 @@ static int LZ4HC_compress_optimal ( LZ4HC_CCtx_internal* ctx, int const firstML = firstMatch.len; const BYTE* const matchPos = ip - firstMatch.off; opSaved = op; - if ( LZ4HC_encodeSequence(&ip, &op, &anchor, firstML, matchPos, limit, oend) ) /* updates ip, op and anchor */ + if ( LZ4HC_encodeSequence(UPDATABLE(ip, op, anchor), firstML, matchPos, limit, oend) ) /* updates ip, op and anchor */ goto _dest_overflow; continue; } @@ -1378,7 +1380,7 @@ static int LZ4HC_compress_optimal ( LZ4HC_CCtx_internal* ctx, assert(ml >= MINMATCH); assert((offset >= 1) && (offset <= MAX_DISTANCE)); opSaved = op; - if ( LZ4HC_encodeSequence(&ip, &op, &anchor, ml, ip - offset, limit, oend) ) /* updates ip, op and anchor */ + if ( LZ4HC_encodeSequence(UPDATABLE(ip, op, anchor), ml, ip - offset, limit, oend) ) /* updates ip, op and anchor */ goto _dest_overflow; } } } /* while (ip <= mflimit) */ -- cgit v1.2.3 From 186015a5d247d5e48586db1ab51a59f0348a3861 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 9 Jan 2019 13:45:42 -0800 Subject: fixed strict C++ compilation --- tests/.gitignore | 1 + tests/fuzzer.c | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/.gitignore b/tests/.gitignore index 9aa42a0..c4f9092 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -10,6 +10,7 @@ fuzzer32 fasttest roundTripTest checkTag +checkFrame # test artefacts tmp* diff --git a/tests/fuzzer.c b/tests/fuzzer.c index 573703c..893ffc1 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -1109,7 +1109,7 @@ static void FUZ_unitTests(int compressionLevel) assert(blockSize < INT_MAX); srcSize = (int)blockSize; assert(targetSize < INT_MAX); - result = LZ4_compress_HC_destSize(&sHC, block, dstBlock, &srcSize, (int)targetSize, 3); + result = LZ4_compress_HC_destSize(&sHC, (const char*)block, (char*)dstBlock, &srcSize, (int)targetSize, 3); DISPLAYLEVEL(4, "cSize=%i; readSize=%i; ", result, srcSize); FUZ_CHECKTEST(result!=4116, "LZ4_compress_HC_destSize() : compression must fill dstBuffer completely, but no more !"); FUZ_CHECKTEST(((char*)dstBlock)[targetSize] != sentinel, "LZ4_compress_HC_destSize()") -- cgit v1.2.3 From 9028682e7ac1e621299b52065d47ba9f74c5c111 Mon Sep 17 00:00:00 2001 From: Tim Zakian <2895723+tzakian@users.noreply.github.com> Date: Thu, 10 Jan 2019 10:20:17 -0800 Subject: Fix pass-through mode --- programs/lz4cli.c | 7 ++++++- programs/lz4io.c | 11 ++++++++++- programs/lz4io.h | 4 ++++ tests/Makefile | 9 ++++++--- 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/programs/lz4cli.c b/programs/lz4cli.c index 84499d0..c6b3cc1 100644 --- a/programs/lz4cli.c +++ b/programs/lz4cli.c @@ -340,6 +340,7 @@ int main(int argc, const char** argv) if (exeNameMatch(exeName, LZ4CAT)) { mode = om_decompress; LZ4IO_setOverwrite(1); + LZ4IO_setPassThrough(1); LZ4IO_setRemoveSrcFile(0); forceStdout=1; output_filename=stdoutmark; @@ -468,7 +469,11 @@ int main(int argc, const char** argv) case 'd': mode = om_decompress; break; /* Force stdout, even if stdout==console */ - case 'c': forceStdout=1; output_filename=stdoutmark; break; + case 'c': + forceStdout=1; + output_filename=stdoutmark; + LZ4IO_setPassThrough(1); + break; /* Test integrity */ case 't': mode = om_test; break; diff --git a/programs/lz4io.c b/programs/lz4io.c index a35928d..f7adb89 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -107,6 +107,7 @@ static clock_t g_time = 0; /************************************** * Local Parameters **************************************/ +static int g_passThrough = 0; static int g_overwrite = 1; static int g_testMode = 0; static int g_blockSizeId = LZ4IO_BLOCKSIZEID_DEFAULT; @@ -157,6 +158,14 @@ int LZ4IO_setDictionaryFilename(const char* dictionaryFilename) { return g_useDictionary; } +/* Default setting : passThrough = 0; return : passThrough mode (0/1) */ +int LZ4IO_setPassThrough(int yes) +{ + g_passThrough = (yes!=0); + return g_passThrough; +} + + /* Default setting : overwrite = 1; return : overwrite mode (0/1) */ int LZ4IO_setOverwrite(int yes) { @@ -1070,7 +1079,7 @@ static unsigned long long selectDecoder(dRess_t ress, FILE* finput, FILE* foutpu default: if (nbFrames == 1) { /* just started */ /* Wrong magic number at the beginning of 1st stream */ - if (!g_testMode && g_overwrite) { + if (!g_testMode && g_overwrite && g_passThrough) { nbFrames = 0; return LZ4IO_passThrough(finput, foutput, MNstore); } diff --git a/programs/lz4io.h b/programs/lz4io.h index 33de41f..3a62869 100644 --- a/programs/lz4io.h +++ b/programs/lz4io.h @@ -66,6 +66,10 @@ int LZ4IO_decompressMultipleFilenames(const char** inFileNamesTable, int ifntSiz int LZ4IO_setDictionaryFilename(const char* dictionaryFilename); +/* Default setting : passThrough = 0; + return : passThrough mode (0/1) */ +int LZ4IO_setPassThrough(int yes); + /* Default setting : overwrite = 1; return : overwrite mode (0/1) */ int LZ4IO_setOverwrite(int yes); diff --git a/tests/Makefile b/tests/Makefile index 760fc32..7d49b31 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -337,8 +337,11 @@ test-lz4-testmode: lz4 datagen ! ./datagen | $(LZ4) -t ! ./datagen | $(LZ4) -tf @echo "\n ---- pass-through mode ----" - ! ./datagen | $(LZ4) -d > $(VOID) - ./datagen | $(LZ4) -df > $(VOID) + @echo "Why hello there " > tmp-tlt2.lz4 + ! $(LZ4) -f tmp-tlt2.lz4 > $(VOID) + ! ./datagen | $(LZ4) -dc > $(VOID) + ! ./datagen | $(LZ4) -df > $(VOID) + ./datagen | $(LZ4) -dcf > $(VOID) @echo "Hello World !" > tmp-tlt1 $(LZ4) -dcf tmp-tlt1 @echo "from underground..." > tmp-tlt2 @@ -347,7 +350,7 @@ test-lz4-testmode: lz4 datagen ! $(LZ4) file-does-not-exist ! $(LZ4) -f file-does-not-exist ! $(LZ4) -fm file1-dne file2-dne - @$(RM) tmp-tlt + @$(RM) tmp-tlt tmp-tlt1 tmp-tlt2 tmp-tlt2.lz4 test-lz4-opt-parser: lz4 datagen @echo "\n ---- test opt-parser ----" -- cgit v1.2.3 From b3b22b9660796f25de2b77b666517a835cfc7e9e Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Fri, 11 Jan 2019 02:32:39 +0700 Subject: meson: Explicit use `meson setup` to setup a builddir --- contrib/meson/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/meson/README.md b/contrib/meson/README.md index 4e55ab9..fa18493 100644 --- a/contrib/meson/README.md +++ b/contrib/meson/README.md @@ -13,7 +13,7 @@ This Meson build system is provided with no guarantee. `cd` to this meson directory (`contrib/meson`) ```sh -meson --buildtype=release -Ddefault_library=shared -Dbuild_programs=true builddir +meson setup --buildtype=release -Ddefault_library=shared -Dbuild_programs=true builddir cd builddir ninja # to build ninja install # to install -- cgit v1.2.3 From 4765ad88bd4e0abc6f4b45146a47cfd9cd956ef5 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Fri, 11 Jan 2019 02:33:27 +0700 Subject: meson: Use libray as required argument in `pkgconfig` --- contrib/meson/lib/meson.build | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contrib/meson/lib/meson.build b/contrib/meson/lib/meson.build index ff7a703..f1deb58 100644 --- a/contrib/meson/lib/meson.build +++ b/contrib/meson/lib/meson.build @@ -37,9 +37,9 @@ liblz4 = library('lz4', liblz4_dep = declare_dependency(link_with: liblz4, include_directories: liblz4_includes) -pkgconfig.generate(name: 'lz4', +pkgconfig.generate(liblz4, + name: 'lz4', filebase: 'liblz4', - libraries: [liblz4], description: 'extremely fast lossless compression algorithm library', version: lz4_libversion, url: 'http://www.lz4.org/') -- cgit v1.2.3 From d2288d2cc0ed47b3531d5ff97eea2e74a6612aa3 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Fri, 11 Jan 2019 02:34:16 +0700 Subject: meson: Favor warning if cannot find version string --- contrib/meson/meson.build | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/meson/meson.build b/contrib/meson/meson.build index d618eb4..e25e7c0 100644 --- a/contrib/meson/meson.build +++ b/contrib/meson/meson.build @@ -43,7 +43,7 @@ if r.returncode() == 0 message('Project version is now: @0@'.format(lz4_version)) endif else - message('Cannot find project version in @0@'.format(lz4_h_file)) + warning('Cannot find project version in @0@'.format(lz4_h_file)) endif lz4_libversion = lz4_version -- cgit v1.2.3 From 7fe378fc70476fdc385658cb08791942b1077934 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Fri, 11 Jan 2019 01:20:16 +0700 Subject: travis: Prefer `script` field than `Cmd` env --- .travis.yml | 186 +++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 108 insertions(+), 78 deletions(-) diff --git a/.travis.yml b/.travis.yml index 66da011..9e968fe 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,90 +1,116 @@ language: c + matrix: fast_finish: true include: # OS X Mavericks - - os: osx + - name: (macOS) General Test + os: osx install: - export CC=clang - env: Ubu=OS_X_Mavericks Cmd='make -C tests test-lz4 MOREFLAGS="-Werror -Wconversion -Wno-sign-conversion" && CFLAGS=-m32 make -C tests clean test-lz4-contentSize' COMPILER=clang + script: + - make -C tests test-lz4 MOREFLAGS='-Werror -Wconversion -Wno-sign-conversion' + - CFLAGS=-m32 make -C tests clean test-lz4-contentSize # Container-based 12.04 LTS Server Edition 64 bit (doesn't support 32-bit includes) - - os: linux - sudo: false - env: Ubu=12.04cont Cmd='make -C tests test-lz4 test-lz4c test-fullbench' COMPILER=cc + - name: (Precise) benchmark test + dist: precise + script: + - make -C tests test-lz4 test-lz4c test-fullbench - - os: linux - sudo: required - env: Ubu=12.04cont Cmd='sudo sysctl -w vm.mmap_min_addr="4096" && make -C tests test-frametest test-fuzzer' COMPILER=cc + - name: (Precise) frame and fuzzer test + dist: precise + install: + - sudo sysctl -w vm.mmap_min_addr=4096 + script: + - make -C tests test-frametest test-fuzzer - - os: linux - sudo: false - env: Ubu=12.04cont Cmd="make gpptest && make clean && make examples && make clean cmake && make clean travis-install && make clean clangtest" COMPILER=cc + - name: (Precise) g++ and clang CMake test + dist: precise + script: + - make gpptest + - make clean + - make examples + - make clean cmake + - make clean travis-install + - make clean clangtest # 14.04 LTS Server Edition 64 bit - - env: Ubu=14.04 Cmd='make -C tests test MOREFLAGS=-mx32' COMPILER=cc + - name: (Trusty) i386 gcc test dist: trusty - sudo: required addons: apt: packages: - libc6-dev-i386 - gcc-multilib + script: + - make -C tests test MOREFLAGS=-mx32 # presume clang >= v3.9.0 - - env: Ubu=14.04 Cmd='make usan MOREFLAGS=-Wcomma -Werror' COMPILER=clang + - name: (Trusty) USan test dist: trusty - sudo: required addons: apt: packages: - clang + script: + - make usan MOREFLAGS=-Wcomma -Werror - - env: Ubu=14.04 Cmd='make c_standards && make -C tests test-lz4 test-mem' COMPILER=cc + - name: (Trusty) valgrind test dist: trusty - sudo: required addons: apt: packages: - valgrind + script: + - make c_standards + - make -C tests test-lz4 test-mem - - env: Ubu=14.04 Cmd='make ctocpptest' COMPILER=cc + - name: (Trusty) c-to-c++ test dist: trusty - sudo: false + script: + - make ctocpptest - - env: Ubu=14.04 Cmd='make -C tests test-lz4c32 test-fullbench32 versionsTest' COMPILER=cc + - name: (Trusty) i386 benchmark + version test dist: trusty - sudo: required addons: apt: packages: - python3 - libc6-dev-i386 - gcc-multilib + script: + - make -C tests test-lz4c32 test-fullbench32 versionsTest - - env: Ubu=14.04 Cmd='sudo sysctl -w vm.mmap_min_addr="4096" && make -C tests test-frametest32 test-fuzzer32' COMPILER=cc + - name: (Trusty) i386 frame + fuzzer test dist: trusty - sudo: required addons: apt: packages: - libc6-dev-i386 - gcc-multilib + install: + - sudo sysctl -w vm.mmap_min_addr="4096" + script: + - make -C tests test-frametest32 test-fuzzer32 - - env: Ubu=14.04 Cmd='make c_standards CC=gcc-6 && make -C tests test-lz4 CC=gcc-6 MOREFLAGS=-Werror' COMPILER=gcc-6 + - name: (Trusty) gcc-6 standard C compilation dist: trusty - sudo: required addons: apt: sources: - ubuntu-toolchain-r-test packages: - gcc-6 + install: + - export CC=gcc-6 + script: + - make c_standards + - make -C tests test-lz4 MOREFLAGS=-Werror - - env: Ubu=14.04 Cmd='make platformTest CC=arm-linux-gnueabi-gcc QEMU_SYS=qemu-arm-static && make platformTest CC=aarch64-linux-gnu-gcc QEMU_SYS=qemu-aarch64-static' COMPILER=arm-linux-gnueabi-gcc + - name: (Trusty) arm + aarch64 compilation dist: trusty - sudo: required addons: apt: packages: @@ -94,23 +120,19 @@ matrix: - libc6-dev-armel-cross - gcc-aarch64-linux-gnu - libc6-dev-arm64-cross + script: + - make platformTest CC=arm-linux-gnueabi-gcc QEMU_SYS=qemu-arm-static + - make platformTest CC=aarch64-linux-gnu-gcc QEMU_SYS=qemu-aarch64-static - - env: Ubu=14.04 Cmd='make -C tests test-lz4 clean test-lz4c32 CC=gcc-5 MOREFLAGS=-Werror' COMPILER=gcc-5 - dist: trusty - sudo: required - addons: - apt: - sources: - - ubuntu-toolchain-r-test - packages: - - libc6-dev-i386 - - gcc-multilib - - gcc-5 - - gcc-5-multilib + - name: (Xenial) gcc-5 compilation + dist: xenial + install: + - export CC=gcc-5 + script: + - make -C tests test-lz4 clean test-lz4c32 MOREFLAGS=-Werror - - env: Ubu=14.04 Cmd='make -C tests test-lz4 CC=clang-3.8' COMPILER=clang-3.8 + - name: (Trusty) clang-3.8 compilation dist: trusty - sudo: required addons: apt: sources: @@ -118,29 +140,34 @@ matrix: - llvm-toolchain-precise-3.8 packages: - clang-3.8 + script: + - make -C tests test-lz4 CC=clang-3.8 - - env: Ubu=14.04 Cmd='make platformTest CC=powerpc-linux-gnu-gcc QEMU_SYS=qemu-ppc-static && make platformTest CC=powerpc-linux-gnu-gcc QEMU_SYS=qemu-ppc64-static MOREFLAGS=-m64' COMPILER=powerpc-linux-gnu-gcc + - name: (Trusty) PowerPC + PPC64 compilation dist: trusty - sudo: required addons: apt: packages: - qemu-system-ppc - qemu-user-static - gcc-powerpc-linux-gnu + script: + - make platformTest CC=powerpc-linux-gnu-gcc QEMU_SYS=qemu-ppc-static + - make platformTest CC=powerpc-linux-gnu-gcc QEMU_SYS=qemu-ppc64-static MOREFLAGS=-m64 - - env: Ubu=14.04 Cmd='make staticAnalyze && make cppcheck' COMPILER=clang + - name: (Trusty) scan-build + cppcheck dist: trusty - sudo: required addons: apt: packages: - clang - cppcheck + script: + - make staticAnalyze + - make cppcheck - - env: Ubu=14.04 Cmd='make clean all CC=gcc-4.4 MOREFLAGS=-Werror && make clean && CFLAGS=-fPIC LDFLAGS="-pie -fPIE -D_FORTIFY_SOURCE=2" make -C programs' COMPILER=gcc-4.4 + - name: (Trusty) gcc-4.4 compilation dist: trusty - sudo: required addons: apt: sources: @@ -149,38 +176,41 @@ matrix: - libc6-dev-i386 - gcc-multilib - gcc-4.4 + script: + - make clean all CC=gcc-4.4 MOREFLAGS=-Werror + - make clean + - CFLAGS=-fPIC LDFLAGS='-pie -fPIE -D_FORTIFY_SOURCE=2' make -C programs # tag-specific test - - if: tag =~ ^v[0-9]\.[0-9] + - name: tag build + if: tag =~ ^v[0-9]\.[0-9] os: linux - sudo: false - env: Cmd="make -C tests checkTag && tests/checkTag $TRAVIS_BRANCH " COMPILER=cc - - - dist: xenial - sudo: required - env: BUILD_SYSTEM='meson' + script: + - make -C tests checkTag + - tests/checkTag "$TRAVIS_BRANCH" + + - name: (Xenial) Meson + clang build + env: ALLOW_FAILURES=true + dist: xenial + language: cpp + compiler: clang + install: + - sudo apt-get install -qq python3 tree + - curl -o ~/ninja.zip -L 'https://github.com/ninja-build/ninja/releases/download/v1.8.2/ninja-linux.zip' + && unzip ~/ninja.zip -d ~/.local/bin + - curl -o ~/get-pip.py 'https://bootstrap.pypa.io/get-pip.py' + && python3 ~/get-pip.py --user + && pip3 install --user meson + script: + - meson + --buildtype=debug + -Db_lundef=false + -Dauto_features=enabled + -Ddefault_library=both + -Dbuild_{programs,contrib,tests,examples}=true + contrib/meson build + - cd build + - DESTDIR=./staging ninja install + - tree ./staging allow_failures: - - env: BUILD_SYSTEM='meson' - -script: - - if [ "${BUILD_SYSTEM}" = meson ]; then - sudo apt-get install -qq python3 tree - && curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py" - && python3 get-pip.py --user && rm get-pip.py - && pip3 install --user meson ninja - && export CC=clang CXX=clang++ - && meson --buildtype=debug - -Db_lundef=false - -Dauto_features=enabled - -Ddefault_library=both - -Dbuild_{programs,contrib,tests,examples}=true - contrib/meson build - && cd "$_" - && DESTDIR=./staging ninja install - && tree ./staging; - travis_terminate "$?"; - fi - - uname -a - - echo Cmd=$Cmd - - $COMPILER -v - - sh -c "$Cmd" + - env: ALLOW_FAILURES=true -- cgit v1.2.3 From c99b64af86894056891aedda4a366bfaab461db5 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Fri, 11 Jan 2019 01:39:34 +0700 Subject: travis: Prefer `apt-get` in `install` field than `addons-apt-sources` --- .travis.yml | 71 ++++++++++++++++++++++--------------------------------------- 1 file changed, 25 insertions(+), 46 deletions(-) diff --git a/.travis.yml b/.travis.yml index 9e968fe..301d294 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,8 +6,7 @@ matrix: # OS X Mavericks - name: (macOS) General Test os: osx - install: - - export CC=clang + compiler: clang script: - make -C tests test-lz4 MOREFLAGS='-Werror -Wconversion -Wno-sign-conversion' - CFLAGS=-m32 make -C tests clean test-lz4-contentSize @@ -50,19 +49,14 @@ matrix: # presume clang >= v3.9.0 - name: (Trusty) USan test dist: trusty - addons: - apt: - packages: - - clang + compiler: clang script: - make usan MOREFLAGS=-Wcomma -Werror - name: (Trusty) valgrind test dist: trusty - addons: - apt: - packages: - - valgrind + install: + - sudo apt-get install -qq valgrind script: - make c_standards - make -C tests test-lz4 test-mem @@ -74,24 +68,16 @@ matrix: - name: (Trusty) i386 benchmark + version test dist: trusty - addons: - apt: - packages: - - python3 - - libc6-dev-i386 - - gcc-multilib + install: + - sudo apt-get install -qq python3 libc6-dev-i386 gcc-multilib script: - make -C tests test-lz4c32 test-fullbench32 versionsTest - name: (Trusty) i386 frame + fuzzer test dist: trusty - addons: - apt: - packages: - - libc6-dev-i386 - - gcc-multilib install: - - sudo sysctl -w vm.mmap_min_addr="4096" + - sudo apt-get install -qq libc6-dev-i386 gcc-multilib + - sudo sysctl -w vm.mmap_min_addr=4096 script: - make -C tests test-frametest32 test-fuzzer32 @@ -103,23 +89,22 @@ matrix: - ubuntu-toolchain-r-test packages: - gcc-6 - install: - - export CC=gcc-6 + env: + - CC=gcc-6 script: - make c_standards - make -C tests test-lz4 MOREFLAGS=-Werror - name: (Trusty) arm + aarch64 compilation dist: trusty - addons: - apt: - packages: - - qemu-system-arm - - qemu-user-static - - gcc-arm-linux-gnueabi - - libc6-dev-armel-cross - - gcc-aarch64-linux-gnu - - libc6-dev-arm64-cross + install: + - sudo apt-get install -qq + qemu-system-arm + qemu-user-static + gcc-arm-linux-gnueabi + libc6-dev-armel-cross + gcc-aarch64-linux-gnu + libc6-dev-arm64-cross script: - make platformTest CC=arm-linux-gnueabi-gcc QEMU_SYS=qemu-arm-static - make platformTest CC=aarch64-linux-gnu-gcc QEMU_SYS=qemu-aarch64-static @@ -127,7 +112,7 @@ matrix: - name: (Xenial) gcc-5 compilation dist: xenial install: - - export CC=gcc-5 + - sudo apt-get install -qq libc6-dev-i386 gcc-multilib script: - make -C tests test-lz4 clean test-lz4c32 MOREFLAGS=-Werror @@ -145,23 +130,17 @@ matrix: - name: (Trusty) PowerPC + PPC64 compilation dist: trusty - addons: - apt: - packages: - - qemu-system-ppc - - qemu-user-static - - gcc-powerpc-linux-gnu + install: + - sudo apt-get install -qq qemu-system-ppc qemu-user-static gcc-powerpc-linux-gnu script: - make platformTest CC=powerpc-linux-gnu-gcc QEMU_SYS=qemu-ppc-static - make platformTest CC=powerpc-linux-gnu-gcc QEMU_SYS=qemu-ppc64-static MOREFLAGS=-m64 - name: (Trusty) scan-build + cppcheck dist: trusty - addons: - apt: - packages: - - clang - - cppcheck + compiler: clang + install: + - sudo apt-get install -qq cppcheck script: - make staticAnalyze - make cppcheck @@ -202,7 +181,7 @@ matrix: && python3 ~/get-pip.py --user && pip3 install --user meson script: - - meson + - meson setup --buildtype=debug -Db_lundef=false -Dauto_features=enabled -- cgit v1.2.3 From 5822e667ccf2f7ea89029d2536d4a49c05c327ae Mon Sep 17 00:00:00 2001 From: Tim Zakian <2895723+tzakian@users.noreply.github.com> Date: Thu, 10 Jan 2019 15:27:47 -0800 Subject: Remove a bunch of global variables that tracked settings for the IO module, and move them in to a struct --- programs/lz4cli.c | 66 +++++++------- programs/lz4io.c | 267 ++++++++++++++++++++++++++++++------------------------ programs/lz4io.h | 42 +++++---- 3 files changed, 209 insertions(+), 166 deletions(-) diff --git a/programs/lz4cli.c b/programs/lz4cli.c index c6b3cc1..d956b87 100644 --- a/programs/lz4cli.c +++ b/programs/lz4cli.c @@ -92,7 +92,7 @@ static unsigned displayLevel = 2; /* 0 : no display ; 1: errors only ; 2 : dow ***************************************/ #define DEFAULT_COMPRESSOR LZ4IO_compressFilename #define DEFAULT_DECOMPRESSOR LZ4IO_decompressFilename -int LZ4IO_compressFilename_Legacy(const char* input_filename, const char* output_filename, int compressionlevel); /* hidden function */ +int LZ4IO_compressFilename_Legacy(LZ4IO_prefs_t* prefs, const char* input_filename, const char* output_filename, int compressionlevel); /* hidden function */ /*-*************************** @@ -318,9 +318,10 @@ int main(int argc, const char** argv) char* dynNameSpace = NULL; const char** inFileNames = (const char**) calloc(argc, sizeof(char*)); unsigned ifnIdx=0; + LZ4IO_prefs_t* prefs = LZ4IO_defaultPreferences(); const char nullOutput[] = NULL_OUTPUT; const char extension[] = LZ4_EXTENSION; - size_t blockSize = LZ4IO_setBlockSizeID(LZ4_BLOCKSIZEID_DEFAULT); + size_t blockSize = LZ4IO_setBlockSizeID(prefs, LZ4_BLOCKSIZEID_DEFAULT); const char* const exeName = lastNameFromPath(argv[0]); #ifdef UTIL_HAS_CREATEFILELIST const char** extendedFileList = NULL; @@ -334,14 +335,14 @@ int main(int argc, const char** argv) return 1; } inFileNames[0] = stdinmark; - LZ4IO_setOverwrite(0); + LZ4IO_setOverwrite(prefs, 0); /* predefined behaviors, based on binary/link name */ if (exeNameMatch(exeName, LZ4CAT)) { mode = om_decompress; - LZ4IO_setOverwrite(1); - LZ4IO_setPassThrough(1); - LZ4IO_setRemoveSrcFile(0); + LZ4IO_setOverwrite(prefs, 1); + LZ4IO_setPassThrough(prefs, 1); + LZ4IO_setRemoveSrcFile(prefs, 0); forceStdout=1; output_filename=stdoutmark; displayLevel=1; @@ -373,23 +374,23 @@ int main(int argc, const char** argv) || (!strcmp(argument, "--uncompress"))) { mode = om_decompress; continue; } if (!strcmp(argument, "--multiple")) { multiple_inputs = 1; continue; } if (!strcmp(argument, "--test")) { mode = om_test; continue; } - if (!strcmp(argument, "--force")) { LZ4IO_setOverwrite(1); continue; } - if (!strcmp(argument, "--no-force")) { LZ4IO_setOverwrite(0); continue; } + if (!strcmp(argument, "--force")) { LZ4IO_setOverwrite(prefs, 1); continue; } + if (!strcmp(argument, "--no-force")) { LZ4IO_setOverwrite(prefs, 0); continue; } if ((!strcmp(argument, "--stdout")) || (!strcmp(argument, "--to-stdout"))) { forceStdout=1; output_filename=stdoutmark; continue; } - if (!strcmp(argument, "--frame-crc")) { LZ4IO_setStreamChecksumMode(1); continue; } - if (!strcmp(argument, "--no-frame-crc")) { LZ4IO_setStreamChecksumMode(0); continue; } - if (!strcmp(argument, "--content-size")) { LZ4IO_setContentSize(1); continue; } - if (!strcmp(argument, "--no-content-size")) { LZ4IO_setContentSize(0); continue; } - if (!strcmp(argument, "--sparse")) { LZ4IO_setSparseFile(2); continue; } - if (!strcmp(argument, "--no-sparse")) { LZ4IO_setSparseFile(0); continue; } - if (!strcmp(argument, "--favor-decSpeed")) { LZ4IO_favorDecSpeed(1); continue; } + if (!strcmp(argument, "--frame-crc")) { LZ4IO_setStreamChecksumMode(prefs, 1); continue; } + if (!strcmp(argument, "--no-frame-crc")) { LZ4IO_setStreamChecksumMode(prefs, 0); continue; } + if (!strcmp(argument, "--content-size")) { LZ4IO_setContentSize(prefs, 1); continue; } + if (!strcmp(argument, "--no-content-size")) { LZ4IO_setContentSize(prefs, 0); continue; } + if (!strcmp(argument, "--sparse")) { LZ4IO_setSparseFile(prefs, 2); continue; } + if (!strcmp(argument, "--no-sparse")) { LZ4IO_setSparseFile(prefs, 0); continue; } + if (!strcmp(argument, "--favor-decSpeed")) { LZ4IO_favorDecSpeed(prefs, 1); continue; } if (!strcmp(argument, "--verbose")) { displayLevel++; continue; } if (!strcmp(argument, "--quiet")) { if (displayLevel) displayLevel--; continue; } if (!strcmp(argument, "--version")) { DISPLAY(WELCOME_MESSAGE); return 0; } if (!strcmp(argument, "--help")) { usage_advanced(exeName); goto _cleanup; } - if (!strcmp(argument, "--keep")) { LZ4IO_setRemoveSrcFile(0); continue; } /* keep source file (default) */ - if (!strcmp(argument, "--rm")) { LZ4IO_setRemoveSrcFile(1); continue; } + if (!strcmp(argument, "--keep")) { LZ4IO_setRemoveSrcFile(prefs, 0); continue; } /* keep source file (default) */ + if (!strcmp(argument, "--rm")) { LZ4IO_setRemoveSrcFile(prefs, 1); continue; } if (longCommandWArg(&argument, "--fast")) { /* Parse optional acceleration factor */ if (*argument == '=') { @@ -420,7 +421,7 @@ int main(int argc, const char** argv) if (!strcmp(argument, "c1")) { cLevel=9; argument++; continue; } /* -c1 (high compression) */ if (!strcmp(argument, "c2")) { cLevel=12; argument++; continue; } /* -c2 (very high compression) */ if (!strcmp(argument, "hc")) { cLevel=12; argument++; continue; } /* -hc (very high compression) */ - if (!strcmp(argument, "y")) { LZ4IO_setOverwrite(1); continue; } /* -y (answer 'yes' to overwrite permission) */ + if (!strcmp(argument, "y")) { LZ4IO_setOverwrite(prefs, 1); continue; } /* -y (answer 'yes' to overwrite permission) */ } if ((*argument>='0') && (*argument<='9')) { @@ -472,14 +473,14 @@ int main(int argc, const char** argv) case 'c': forceStdout=1; output_filename=stdoutmark; - LZ4IO_setPassThrough(1); + LZ4IO_setPassThrough(prefs, 1); break; /* Test integrity */ case 't': mode = om_test; break; /* Overwrite */ - case 'f': LZ4IO_setOverwrite(1); break; + case 'f': LZ4IO_setOverwrite(prefs, 1); break; /* Verbose mode */ case 'v': displayLevel++; break; @@ -488,7 +489,7 @@ int main(int argc, const char** argv) case 'q': if (displayLevel) displayLevel--; break; /* keep source file (default anyway, so useless) (for xz/lzma compatibility) */ - case 'k': LZ4IO_setRemoveSrcFile(0); break; + case 'k': LZ4IO_setRemoveSrcFile(prefs, 0); break; /* Modify Block Properties */ case 'B': @@ -496,8 +497,8 @@ int main(int argc, const char** argv) int exitBlockProperties=0; switch(argument[1]) { - case 'D': LZ4IO_setBlockMode(LZ4IO_blockLinked); argument++; break; - case 'X': LZ4IO_setBlockChecksumMode(1); argument ++; break; /* disabled by default */ + case 'D': LZ4IO_setBlockMode(prefs, LZ4IO_blockLinked); argument++; break; + case 'X': LZ4IO_setBlockChecksumMode(prefs, 1); argument ++; break; /* disabled by default */ default : if (argument[1] < '0' || argument[1] > '9') { exitBlockProperties=1; @@ -509,12 +510,12 @@ int main(int argc, const char** argv) argument--; if (B < 4) badusage(exeName); if (B <= 7) { - blockSize = LZ4IO_setBlockSizeID(B); + blockSize = LZ4IO_setBlockSizeID(prefs, B); BMK_setBlockSize(blockSize); DISPLAYLEVEL(2, "using blocks of size %u KB \n", (U32)(blockSize>>10)); } else { if (B < 32) badusage(exeName); - blockSize = LZ4IO_setBlockSize(B); + blockSize = LZ4IO_setBlockSize(prefs, B); BMK_setBlockSize(blockSize); if (blockSize >= 1024) { DISPLAYLEVEL(2, "using blocks of size %u KB \n", (U32)(blockSize>>10)); @@ -623,7 +624,7 @@ int main(int argc, const char** argv) } if (mode == om_test) { - LZ4IO_setTestMode(1); + LZ4IO_setTestMode(prefs, 1); output_filename = nulmark; mode = om_decompress; /* defer to decompress */ } @@ -633,7 +634,7 @@ int main(int argc, const char** argv) DISPLAYLEVEL(1, "refusing to read from a console\n"); exit(1); } - LZ4IO_setDictionaryFilename(dictionary_filename); + LZ4IO_setDictionaryFilename(prefs, dictionary_filename); } /* compress or decompress */ @@ -699,18 +700,18 @@ int main(int argc, const char** argv) if (ifnIdx == 0) multiple_inputs = 0; if (mode == om_decompress) { if (multiple_inputs) - operationResult = LZ4IO_decompressMultipleFilenames(inFileNames, ifnIdx, !strcmp(output_filename,stdoutmark) ? stdoutmark : LZ4_EXTENSION); + operationResult = LZ4IO_decompressMultipleFilenames(prefs, inFileNames, ifnIdx, !strcmp(output_filename,stdoutmark) ? stdoutmark : LZ4_EXTENSION); else - operationResult = DEFAULT_DECOMPRESSOR(input_filename, output_filename); + operationResult = DEFAULT_DECOMPRESSOR(prefs, input_filename, output_filename); } else { /* compression is default action */ if (legacy_format) { DISPLAYLEVEL(3, "! Generating LZ4 Legacy format (deprecated) ! \n"); - LZ4IO_compressFilename_Legacy(input_filename, output_filename, cLevel); + LZ4IO_compressFilename_Legacy(prefs, input_filename, output_filename, cLevel); } else { if (multiple_inputs) - operationResult = LZ4IO_compressMultipleFilenames(inFileNames, ifnIdx, LZ4_EXTENSION, cLevel); + operationResult = LZ4IO_compressMultipleFilenames(prefs, inFileNames, ifnIdx, LZ4_EXTENSION, cLevel); else - operationResult = DEFAULT_COMPRESSOR(input_filename, output_filename, cLevel); + operationResult = DEFAULT_COMPRESSOR(prefs, input_filename, output_filename, cLevel); } } @@ -723,6 +724,7 @@ _cleanup: inFileNames = NULL; } #endif + LZ4IO_freePreferences(prefs); free((void*)inFileNames); return operationResult; } diff --git a/programs/lz4io.c b/programs/lz4io.c index f7adb89..cff961b 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -107,20 +107,23 @@ static clock_t g_time = 0; /************************************** * Local Parameters **************************************/ -static int g_passThrough = 0; -static int g_overwrite = 1; -static int g_testMode = 0; -static int g_blockSizeId = LZ4IO_BLOCKSIZEID_DEFAULT; -static size_t g_blockSize = 0; -static int g_blockChecksum = 0; -static int g_streamChecksum = 1; -static int g_blockIndependence = 1; -static int g_sparseFileSupport = 1; -static int g_contentSizeFlag = 0; -static int g_useDictionary = 0; -static unsigned g_favorDecSpeed = 0; -static const char* g_dictionaryFilename = NULL; +struct LZ4IO_prefs_s { + int passThrough; + int overwrite; + int testMode; + int blockSizeId; + size_t blockSize; + int blockChecksum; + int streamChecksum; + int blockIndependence; + int sparseFileSupport; + int contentSizeFlag; + int useDictionary; + unsigned favorDecSpeed; + char const* dictionaryFilename; + U32 removeSrcFile; +}; /************************************** * Exceptions @@ -152,81 +155,108 @@ static const char* g_dictionaryFilename = NULL; /* ****************** Parameters ******************** */ /* ************************************************** */ -int LZ4IO_setDictionaryFilename(const char* dictionaryFilename) { - g_dictionaryFilename = dictionaryFilename; - g_useDictionary = dictionaryFilename != NULL; - return g_useDictionary; +LZ4IO_prefs_t* LZ4IO_defaultPreferences(void) +{ + LZ4IO_prefs_t* ret = malloc(sizeof(LZ4IO_prefs_t)); + ret->passThrough = 0; + ret->overwrite = 1; + ret->testMode = 0; + ret->blockSizeId = LZ4IO_BLOCKSIZEID_DEFAULT; + ret->blockSize = 0; + ret->blockChecksum = 0; + ret->streamChecksum = 1; + ret->blockIndependence = 1; + ret->sparseFileSupport = 1; + ret->contentSizeFlag = 0; + ret->useDictionary = 0; + ret->favorDecSpeed = 0; + ret->dictionaryFilename = NULL; + ret->removeSrcFile = 0; + return ret; +} + +void LZ4IO_freePreferences(LZ4IO_prefs_t* prefs) +{ + free(prefs); +} + + +int LZ4IO_setDictionaryFilename(LZ4IO_prefs_t* prefs, const char* dictionaryFilename) +{ + prefs->dictionaryFilename = dictionaryFilename; + prefs->useDictionary = dictionaryFilename != NULL; + return prefs->useDictionary; } /* Default setting : passThrough = 0; return : passThrough mode (0/1) */ -int LZ4IO_setPassThrough(int yes) +int LZ4IO_setPassThrough(LZ4IO_prefs_t* prefs, int yes) { - g_passThrough = (yes!=0); - return g_passThrough; + prefs->passThrough = (yes!=0); + return prefs->passThrough; } /* Default setting : overwrite = 1; return : overwrite mode (0/1) */ -int LZ4IO_setOverwrite(int yes) +int LZ4IO_setOverwrite(LZ4IO_prefs_t* prefs, int yes) { - g_overwrite = (yes!=0); - return g_overwrite; + prefs->overwrite = (yes!=0); + return prefs->overwrite; } /* Default setting : testMode = 0; return : testMode (0/1) */ -int LZ4IO_setTestMode(int yes) +int LZ4IO_setTestMode(LZ4IO_prefs_t* prefs, int yes) { - g_testMode = (yes!=0); - return g_testMode; + prefs->testMode = (yes!=0); + return prefs->testMode; } /* blockSizeID : valid values : 4-5-6-7 */ -size_t LZ4IO_setBlockSizeID(unsigned bsid) +size_t LZ4IO_setBlockSizeID(LZ4IO_prefs_t* prefs, unsigned bsid) { static const size_t blockSizeTable[] = { 64 KB, 256 KB, 1 MB, 4 MB }; static const unsigned minBlockSizeID = 4; static const unsigned maxBlockSizeID = 7; if ((bsid < minBlockSizeID) || (bsid > maxBlockSizeID)) return 0; - g_blockSizeId = bsid; - g_blockSize = blockSizeTable[g_blockSizeId-minBlockSizeID]; - return g_blockSize; + prefs->blockSizeId = bsid; + prefs->blockSize = blockSizeTable[prefs->blockSizeId-minBlockSizeID]; + return prefs->blockSize; } -size_t LZ4IO_setBlockSize(size_t blockSize) +size_t LZ4IO_setBlockSize(LZ4IO_prefs_t* prefs, size_t blockSize) { static const size_t minBlockSize = 32; static const size_t maxBlockSize = 4 MB; unsigned bsid = 0; if (blockSize < minBlockSize) blockSize = minBlockSize; if (blockSize > maxBlockSize) blockSize = maxBlockSize; - g_blockSize = blockSize; + prefs->blockSize = blockSize; blockSize--; /* find which of { 64k, 256k, 1MB, 4MB } is closest to blockSize */ while (blockSize >>= 2) bsid++; if (bsid < 7) bsid = 7; - g_blockSizeId = bsid-3; - return g_blockSize; + prefs->blockSizeId = bsid-3; + return prefs->blockSize; } -int LZ4IO_setBlockMode(LZ4IO_blockMode_t blockMode) +int LZ4IO_setBlockMode(LZ4IO_prefs_t* prefs, LZ4IO_blockMode_t blockMode) { - g_blockIndependence = (blockMode == LZ4IO_blockIndependent); - return g_blockIndependence; + prefs->blockIndependence = (blockMode == LZ4IO_blockIndependent); + return prefs->blockIndependence; } /* Default setting : no block checksum */ -int LZ4IO_setBlockChecksumMode(int enable) +int LZ4IO_setBlockChecksumMode(LZ4IO_prefs_t* prefs, int enable) { - g_blockChecksum = (enable != 0); - return g_blockChecksum; + prefs->blockChecksum = (enable != 0); + return prefs->blockChecksum; } /* Default setting : checksum enabled */ -int LZ4IO_setStreamChecksumMode(int enable) +int LZ4IO_setStreamChecksumMode(LZ4IO_prefs_t* prefs, int enable) { - g_streamChecksum = (enable != 0); - return g_streamChecksum; + prefs->streamChecksum = (enable != 0); + return prefs->streamChecksum; } /* Default setting : 0 (no notification) */ @@ -237,27 +267,29 @@ int LZ4IO_setNotificationLevel(int level) } /* Default setting : 0 (disabled) */ -int LZ4IO_setSparseFile(int enable) +int LZ4IO_setSparseFile(LZ4IO_prefs_t* prefs, int enable) { - g_sparseFileSupport = (enable!=0); - return g_sparseFileSupport; + prefs->sparseFileSupport = (enable!=0); + return prefs->sparseFileSupport; } /* Default setting : 0 (disabled) */ -int LZ4IO_setContentSize(int enable) +int LZ4IO_setContentSize(LZ4IO_prefs_t* prefs, int enable) { - g_contentSizeFlag = (enable!=0); - return g_contentSizeFlag; + prefs->contentSizeFlag = (enable!=0); + return prefs->contentSizeFlag; } /* Default setting : 0 (disabled) */ -void LZ4IO_favorDecSpeed(int favor) +void LZ4IO_favorDecSpeed(LZ4IO_prefs_t* prefs, int favor) { - g_favorDecSpeed = (favor!=0); + prefs->favorDecSpeed = (favor!=0); } -static U32 g_removeSrcFile = 0; -void LZ4IO_setRemoveSrcFile(unsigned flag) { g_removeSrcFile = (flag>0); } +void LZ4IO_setRemoveSrcFile(LZ4IO_prefs_t* prefs, unsigned flag) +{ + prefs->removeSrcFile = (flag>0); +} @@ -292,7 +324,7 @@ static FILE* LZ4IO_openSrcFile(const char* srcFileName) /** FIO_openDstFile() : * condition : `dstFileName` must be non-NULL. * @result : FILE* to `dstFileName`, or NULL if it fails */ -static FILE* LZ4IO_openDstFile(const char* dstFileName) +static FILE* LZ4IO_openDstFile(LZ4IO_prefs_t* prefs, const char* dstFileName) { FILE* f; @@ -300,12 +332,12 @@ static FILE* LZ4IO_openDstFile(const char* dstFileName) DISPLAYLEVEL(4,"Using stdout for output\n"); f = stdout; SET_BINARY_MODE(stdout); - if (g_sparseFileSupport==1) { - g_sparseFileSupport = 0; + if (prefs->sparseFileSupport==1) { + prefs->sparseFileSupport = 0; DISPLAYLEVEL(4, "Sparse File Support is automatically disabled on stdout ; try --sparse \n"); } } else { - if (!g_overwrite && strcmp (dstFileName, nulmark)) { /* Check if destination file already exists */ + if (!prefs->overwrite && strcmp (dstFileName, nulmark)) { /* Check if destination file already exists */ f = fopen( dstFileName, "rb" ); if (f != NULL) { /* dest exists, prompt for overwrite authorization */ fclose(f); @@ -326,7 +358,7 @@ static FILE* LZ4IO_openDstFile(const char* dstFileName) } /* sparse file */ - if (f && g_sparseFileSupport) { SET_SPARSE_FILE_MODE(f); } + if (f && prefs->sparseFileSupport) { SET_SPARSE_FILE_MODE(f); } return f; } @@ -356,7 +388,7 @@ static int LZ4IO_LZ4_compress(const char* src, char* dst, int srcSize, int dstSi /* LZ4IO_compressFilename_Legacy : * This function is intentionally "hidden" (not published in .h) * It generates compressed streams using the old 'legacy' format */ -int LZ4IO_compressFilename_Legacy(const char* input_filename, const char* output_filename, int compressionlevel) +int LZ4IO_compressFilename_Legacy(LZ4IO_prefs_t* prefs, const char* input_filename, const char* output_filename, int compressionlevel) { typedef int (*compress_f)(const char* src, char* dst, int srcSize, int dstSize, int cLevel); compress_f const compressionFunction = (compressionlevel < 3) ? LZ4IO_LZ4_compress : LZ4_compress_HC; @@ -374,7 +406,7 @@ int LZ4IO_compressFilename_Legacy(const char* input_filename, const char* output if (finput == NULL) EXM_THROW(20, "%s : open file error ", input_filename); - foutput = LZ4IO_openDstFile(output_filename); + foutput = LZ4IO_openDstFile(prefs, output_filename); if (foutput == NULL) { fclose(finput); EXM_THROW(20, "%s : open file error ", input_filename); @@ -453,7 +485,7 @@ typedef struct { LZ4F_CDict* cdict; } cRess_t; -static void* LZ4IO_createDict(const char* dictFilename, size_t *dictSize) { +static void* LZ4IO_createDict(LZ4IO_prefs_t* prefs, size_t *dictSize) { size_t readSize; size_t dictEnd = 0; size_t dictLen = 0; @@ -461,6 +493,7 @@ static void* LZ4IO_createDict(const char* dictFilename, size_t *dictSize) { size_t circularBufSize = LZ4_MAX_DICT_SIZE; char* circularBuf; char* dictBuf; + const char* dictFilename = prefs->dictionaryFilename; FILE* dictFile; if (!dictFilename) EXM_THROW(25, "Dictionary error : no filename provided"); @@ -510,23 +543,23 @@ static void* LZ4IO_createDict(const char* dictFilename, size_t *dictSize) { return dictBuf; } -static LZ4F_CDict* LZ4IO_createCDict(void) { +static LZ4F_CDict* LZ4IO_createCDict(LZ4IO_prefs_t* prefs) { size_t dictionarySize; void* dictionaryBuffer; LZ4F_CDict* cdict; - if (!g_useDictionary) { + if (!prefs->useDictionary) { return NULL; } - dictionaryBuffer = LZ4IO_createDict(g_dictionaryFilename, &dictionarySize); + dictionaryBuffer = LZ4IO_createDict(prefs, &dictionarySize); if (!dictionaryBuffer) EXM_THROW(25, "Dictionary error : could not create dictionary"); cdict = LZ4F_createCDict(dictionaryBuffer, dictionarySize); free(dictionaryBuffer); return cdict; } -static cRess_t LZ4IO_createCResources(void) +static cRess_t LZ4IO_createCResources(LZ4IO_prefs_t* prefs) { - const size_t blockSize = g_blockSize; + const size_t blockSize = prefs->blockSize; cRess_t ress; LZ4F_errorCode_t const errorCode = LZ4F_createCompressionContext(&(ress.ctx), LZ4F_VERSION); @@ -539,7 +572,7 @@ static cRess_t LZ4IO_createCResources(void) ress.dstBuffer = malloc(ress.dstBufferSize); if (!ress.srcBuffer || !ress.dstBuffer) EXM_THROW(31, "Allocation error : not enough memory"); - ress.cdict = LZ4IO_createCDict(); + ress.cdict = LZ4IO_createCDict(prefs); return ress; } @@ -561,7 +594,7 @@ static void LZ4IO_freeCResources(cRess_t ress) * result : 0 : compression completed correctly * 1 : missing or pb opening srcFileName */ -static int LZ4IO_compressFilename_extRess(cRess_t ress, const char* srcFileName, const char* dstFileName, int compressionLevel) +static int LZ4IO_compressFilename_extRess(LZ4IO_prefs_t* io_prefs, cRess_t ress, const char* srcFileName, const char* dstFileName, int compressionLevel) { unsigned long long filesize = 0; unsigned long long compressedfilesize = 0; @@ -570,7 +603,7 @@ static int LZ4IO_compressFilename_extRess(cRess_t ress, const char* srcFileName, void* const srcBuffer = ress.srcBuffer; void* const dstBuffer = ress.dstBuffer; const size_t dstBufferSize = ress.dstBufferSize; - const size_t blockSize = g_blockSize; + const size_t blockSize = io_prefs->blockSize; size_t readSize; LZ4F_compressionContext_t ctx = ress.ctx; /* just a pointer */ LZ4F_preferences_t prefs; @@ -578,7 +611,7 @@ static int LZ4IO_compressFilename_extRess(cRess_t ress, const char* srcFileName, /* Init */ srcFile = LZ4IO_openSrcFile(srcFileName); if (srcFile == NULL) return 1; - dstFile = LZ4IO_openDstFile(dstFileName); + dstFile = LZ4IO_openDstFile(io_prefs, dstFileName); if (dstFile == NULL) { fclose(srcFile); return 1; } memset(&prefs, 0, sizeof(prefs)); @@ -586,12 +619,12 @@ static int LZ4IO_compressFilename_extRess(cRess_t ress, const char* srcFileName, /* Set compression parameters */ prefs.autoFlush = 1; prefs.compressionLevel = compressionLevel; - prefs.frameInfo.blockMode = (LZ4F_blockMode_t)g_blockIndependence; - prefs.frameInfo.blockSizeID = (LZ4F_blockSizeID_t)g_blockSizeId; - prefs.frameInfo.blockChecksumFlag = (LZ4F_blockChecksum_t)g_blockChecksum; - prefs.frameInfo.contentChecksumFlag = (LZ4F_contentChecksum_t)g_streamChecksum; - prefs.favorDecSpeed = g_favorDecSpeed; - if (g_contentSizeFlag) { + prefs.frameInfo.blockMode = (LZ4F_blockMode_t)io_prefs->blockIndependence; + prefs.frameInfo.blockSizeID = (LZ4F_blockSizeID_t)io_prefs->blockSizeId; + prefs.frameInfo.blockChecksumFlag = (LZ4F_blockChecksum_t)io_prefs->blockChecksum; + prefs.frameInfo.contentChecksumFlag = (LZ4F_contentChecksum_t)io_prefs->streamChecksum; + prefs.favorDecSpeed = io_prefs->favorDecSpeed; + if (io_prefs->contentSizeFlag) { U64 const fileSize = UTIL_getFileSize(srcFileName); prefs.frameInfo.contentSize = fileSize; /* == 0 if input == stdin */ if (fileSize==0) @@ -670,7 +703,7 @@ static int LZ4IO_compressFilename_extRess(cRess_t ress, const char* srcFileName, UTIL_setFileStat(dstFileName, &statbuf); } } - if (g_removeSrcFile) { /* remove source file : --rm */ + if (io_prefs->removeSrcFile) { /* remove source file : --rm */ if (remove(srcFileName)) EXM_THROW(40, "Remove error : %s: %s", srcFileName, strerror(errno)); } @@ -685,13 +718,13 @@ static int LZ4IO_compressFilename_extRess(cRess_t ress, const char* srcFileName, } -int LZ4IO_compressFilename(const char* srcFileName, const char* dstFileName, int compressionLevel) +int LZ4IO_compressFilename(LZ4IO_prefs_t* prefs, const char* srcFileName, const char* dstFileName, int compressionLevel) { UTIL_time_t const timeStart = UTIL_getTime(); clock_t const cpuStart = clock(); - cRess_t const ress = LZ4IO_createCResources(); + cRess_t const ress = LZ4IO_createCResources(prefs); - int const result = LZ4IO_compressFilename_extRess(ress, srcFileName, dstFileName, compressionLevel); + int const result = LZ4IO_compressFilename_extRess(prefs, ress, srcFileName, dstFileName, compressionLevel); /* Free resources */ LZ4IO_freeCResources(ress); @@ -710,7 +743,7 @@ int LZ4IO_compressFilename(const char* srcFileName, const char* dstFileName, int #define FNSPACE 30 -int LZ4IO_compressMultipleFilenames(const char** inFileNamesTable, int ifntSize, const char* suffix, int compressionLevel) +int LZ4IO_compressMultipleFilenames(LZ4IO_prefs_t* prefs, const char** inFileNamesTable, int ifntSize, const char* suffix, int compressionLevel) { int i; int missed_files = 0; @@ -720,7 +753,7 @@ int LZ4IO_compressMultipleFilenames(const char** inFileNamesTable, int ifntSize, cRess_t ress; if (dstFileName == NULL) return ifntSize; /* not enough memory */ - ress = LZ4IO_createCResources(); + ress = LZ4IO_createCResources(prefs); /* loop on each file */ for (i=0; isparseFileSupport) { /* normal write */ size_t const sizeCheck = fwrite(buffer, 1, bufferSize, file); if (sizeCheck != bufferSize) EXM_THROW(70, "Write error : cannot write decoded block"); return 0; @@ -834,7 +867,7 @@ static void LZ4IO_fwriteSparseEnd(FILE* file, unsigned storedSkips) static unsigned g_magicRead = 0; /* out-parameter of LZ4IO_decodeLegacyStream() */ -static unsigned long long LZ4IO_decodeLegacyStream(FILE* finput, FILE* foutput) +static unsigned long long LZ4IO_decodeLegacyStream(LZ4IO_prefs_t* prefs, FILE* finput, FILE* foutput) { unsigned long long streamSize = 0; unsigned storedSkips = 0; @@ -868,7 +901,7 @@ static unsigned long long LZ4IO_decodeLegacyStream(FILE* finput, FILE* foutput) if (decodeSize < 0) EXM_THROW(53, "Decoding Failed ! Corrupted input detected !"); streamSize += decodeSize; /* Write Block */ - storedSkips = LZ4IO_fwriteSparse(foutput, out_buff, decodeSize, storedSkips); /* success or die */ + storedSkips = LZ4IO_fwriteSparse(prefs, foutput, out_buff, decodeSize, storedSkips); /* success or die */ } } if (ferror(finput)) EXM_THROW(54, "Read error : ferror"); @@ -894,19 +927,19 @@ typedef struct { size_t dictBufferSize; } dRess_t; -static void LZ4IO_loadDDict(dRess_t* ress) { - if (!g_useDictionary) { +static void LZ4IO_loadDDict(LZ4IO_prefs_t* prefs, dRess_t* ress) { + if (!prefs->useDictionary) { ress->dictBuffer = NULL; ress->dictBufferSize = 0; return; } - ress->dictBuffer = LZ4IO_createDict(g_dictionaryFilename, &ress->dictBufferSize); + ress->dictBuffer = LZ4IO_createDict(prefs, &ress->dictBufferSize); if (!ress->dictBuffer) EXM_THROW(25, "Dictionary error : could not create dictionary"); } static const size_t LZ4IO_dBufferSize = 64 KB; -static dRess_t LZ4IO_createDResources(void) +static dRess_t LZ4IO_createDResources(LZ4IO_prefs_t* prefs) { dRess_t ress; @@ -921,7 +954,7 @@ static dRess_t LZ4IO_createDResources(void) ress.dstBuffer = malloc(ress.dstBufferSize); if (!ress.srcBuffer || !ress.dstBuffer) EXM_THROW(61, "Allocation error : not enough memory"); - LZ4IO_loadDDict(&ress); + LZ4IO_loadDDict(prefs, &ress); ress.dstFile = NULL; return ress; @@ -937,7 +970,7 @@ static void LZ4IO_freeDResources(dRess_t ress) } -static unsigned long long LZ4IO_decompressLZ4F(dRess_t ress, FILE* srcFile, FILE* dstFile) +static unsigned long long LZ4IO_decompressLZ4F(LZ4IO_prefs_t* prefs, dRess_t ress, FILE* srcFile, FILE* dstFile) { unsigned long long filesize = 0; LZ4F_errorCode_t nextToLoad; @@ -972,8 +1005,8 @@ static unsigned long long LZ4IO_decompressLZ4F(dRess_t ress, FILE* srcFile, FILE /* Write Block */ if (decodedBytes) { - if (!g_testMode) - storedSkips = LZ4IO_fwriteSparse(dstFile, ress.dstBuffer, decodedBytes, storedSkips); + if (!prefs->testMode) + storedSkips = LZ4IO_fwriteSparse(prefs, dstFile, ress.dstBuffer, decodedBytes, storedSkips); filesize += decodedBytes; DISPLAYUPDATE(2, "\rDecompressed : %u MB ", (unsigned)(filesize>>20)); } @@ -984,7 +1017,7 @@ static unsigned long long LZ4IO_decompressLZ4F(dRess_t ress, FILE* srcFile, FILE /* can be out because readSize == 0, which could be an fread() error */ if (ferror(srcFile)) EXM_THROW(67, "Read error"); - if (!g_testMode) LZ4IO_fwriteSparseEnd(dstFile, storedSkips); + if (!prefs->testMode) LZ4IO_fwriteSparseEnd(dstFile, storedSkips); if (nextToLoad!=0) EXM_THROW(68, "Unfinished stream"); return filesize; @@ -993,7 +1026,7 @@ static unsigned long long LZ4IO_decompressLZ4F(dRess_t ress, FILE* srcFile, FILE #define PTSIZE (64 KB) #define PTSIZET (PTSIZE / sizeof(size_t)) -static unsigned long long LZ4IO_passThrough(FILE* finput, FILE* foutput, unsigned char MNstore[MAGICNUMBER_SIZE]) +static unsigned long long LZ4IO_passThrough(LZ4IO_prefs_t* prefs, FILE* finput, FILE* foutput, unsigned char MNstore[MAGICNUMBER_SIZE]) { size_t buffer[PTSIZET]; size_t readBytes = 1; @@ -1006,7 +1039,7 @@ static unsigned long long LZ4IO_passThrough(FILE* finput, FILE* foutput, unsigne while (readBytes) { readBytes = fread(buffer, 1, PTSIZE, finput); total += readBytes; - storedSkips = LZ4IO_fwriteSparse(foutput, buffer, readBytes, storedSkips); + storedSkips = LZ4IO_fwriteSparse(prefs, foutput, buffer, readBytes, storedSkips); } if (ferror(finput)) EXM_THROW(51, "Read Error"); @@ -1033,7 +1066,7 @@ static int fseek_u32(FILE *fp, unsigned offset, int where) } #define ENDOFSTREAM ((unsigned long long)-1) -static unsigned long long selectDecoder(dRess_t ress, FILE* finput, FILE* foutput) +static unsigned long long selectDecoder(LZ4IO_prefs_t* prefs, dRess_t ress, FILE* finput, FILE* foutput) { unsigned char MNstore[MAGICNUMBER_SIZE]; unsigned magicNumber; @@ -1059,10 +1092,10 @@ static unsigned long long selectDecoder(dRess_t ress, FILE* finput, FILE* foutpu switch(magicNumber) { case LZ4IO_MAGICNUMBER: - return LZ4IO_decompressLZ4F(ress, finput, foutput); + return LZ4IO_decompressLZ4F(prefs, ress, finput, foutput); case LEGACY_MAGICNUMBER: DISPLAYLEVEL(4, "Detected : Legacy format \n"); - return LZ4IO_decodeLegacyStream(finput, foutput); + return LZ4IO_decodeLegacyStream(prefs, finput, foutput); case LZ4IO_SKIPPABLE0: DISPLAYLEVEL(4, "Skipping detected skippable area \n"); { size_t const nbReadBytes = fread(MNstore, 1, 4, finput); @@ -1079,9 +1112,9 @@ static unsigned long long selectDecoder(dRess_t ress, FILE* finput, FILE* foutpu default: if (nbFrames == 1) { /* just started */ /* Wrong magic number at the beginning of 1st stream */ - if (!g_testMode && g_overwrite && g_passThrough) { + if (!prefs->testMode && prefs->overwrite && prefs->passThrough) { nbFrames = 0; - return LZ4IO_passThrough(finput, foutput, MNstore); + return LZ4IO_passThrough(prefs, finput, foutput, MNstore); } EXM_THROW(44,"Unrecognized header : file cannot be decoded"); } @@ -1096,7 +1129,7 @@ static unsigned long long selectDecoder(dRess_t ress, FILE* finput, FILE* foutpu } -static int LZ4IO_decompressSrcFile(dRess_t ress, const char* input_filename, const char* output_filename) +static int LZ4IO_decompressSrcFile(LZ4IO_prefs_t* prefs, dRess_t ress, const char* input_filename, const char* output_filename) { FILE* const foutput = ress.dstFile; unsigned long long filesize = 0; @@ -1108,14 +1141,14 @@ static int LZ4IO_decompressSrcFile(dRess_t ress, const char* input_filename, con /* Loop over multiple streams */ for ( ; ; ) { /* endless loop, see break condition */ unsigned long long const decodedSize = - selectDecoder(ress, finput, foutput); + selectDecoder(prefs, ress, finput, foutput); if (decodedSize == ENDOFSTREAM) break; filesize += decodedSize; } /* Close input */ fclose(finput); - if (g_removeSrcFile) { /* --rm */ + if (prefs->removeSrcFile) { /* --rm */ if (remove(input_filename)) EXM_THROW(45, "Remove error : %s: %s", input_filename, strerror(errno)); } @@ -1129,11 +1162,11 @@ static int LZ4IO_decompressSrcFile(dRess_t ress, const char* input_filename, con } -static int LZ4IO_decompressDstFile(dRess_t ress, const char* input_filename, const char* output_filename) +static int LZ4IO_decompressDstFile(LZ4IO_prefs_t* prefs, dRess_t ress, const char* input_filename, const char* output_filename) { stat_t statbuf; int stat_result = 0; - FILE* const foutput = LZ4IO_openDstFile(output_filename); + FILE* const foutput = LZ4IO_openDstFile(prefs, output_filename); if (foutput==NULL) return 1; /* failure */ if ( strcmp(input_filename, stdinmark) @@ -1141,7 +1174,7 @@ static int LZ4IO_decompressDstFile(dRess_t ress, const char* input_filename, con stat_result = 1; ress.dstFile = foutput; - LZ4IO_decompressSrcFile(ress, input_filename, output_filename); + LZ4IO_decompressSrcFile(prefs, ress, input_filename, output_filename); fclose(foutput); @@ -1157,12 +1190,12 @@ static int LZ4IO_decompressDstFile(dRess_t ress, const char* input_filename, con } -int LZ4IO_decompressFilename(const char* input_filename, const char* output_filename) +int LZ4IO_decompressFilename(LZ4IO_prefs_t* prefs, const char* input_filename, const char* output_filename) { - dRess_t const ress = LZ4IO_createDResources(); + dRess_t const ress = LZ4IO_createDResources(prefs); clock_t const start = clock(); - int const missingFiles = LZ4IO_decompressDstFile(ress, input_filename, output_filename); + int const missingFiles = LZ4IO_decompressDstFile(prefs, ress, input_filename, output_filename); clock_t const end = clock(); double const seconds = (double)(end - start) / CLOCKS_PER_SEC; @@ -1173,7 +1206,7 @@ int LZ4IO_decompressFilename(const char* input_filename, const char* output_file } -int LZ4IO_decompressMultipleFilenames(const char** inFileNamesTable, int ifntSize, const char* suffix) +int LZ4IO_decompressMultipleFilenames(LZ4IO_prefs_t* prefs, const char** inFileNamesTable, int ifntSize, const char* suffix) { int i; int skippedFiles = 0; @@ -1181,16 +1214,16 @@ int LZ4IO_decompressMultipleFilenames(const char** inFileNamesTable, int ifntSiz char* outFileName = (char*)malloc(FNSPACE); size_t ofnSize = FNSPACE; size_t const suffixSize = strlen(suffix); - dRess_t ress = LZ4IO_createDResources(); + dRess_t ress = LZ4IO_createDResources(prefs); if (outFileName==NULL) return ifntSize; /* not enough memory */ - ress.dstFile = LZ4IO_openDstFile(stdoutmark); + ress.dstFile = LZ4IO_openDstFile(prefs, stdoutmark); for (i=0; i 4MB return : 0 if error, actual blocksize if OK */ -size_t LZ4IO_setBlockSize(size_t blockSize); +size_t LZ4IO_setBlockSize(LZ4IO_prefs_t* prefs, size_t blockSize); /* Default setting : independent blocks */ typedef enum { LZ4IO_blockLinked=0, LZ4IO_blockIndependent} LZ4IO_blockMode_t; -int LZ4IO_setBlockMode(LZ4IO_blockMode_t blockMode); +int LZ4IO_setBlockMode(LZ4IO_prefs_t* prefs, LZ4IO_blockMode_t blockMode); /* Default setting : no block checksum */ -int LZ4IO_setBlockChecksumMode(int xxhash); +int LZ4IO_setBlockChecksumMode(LZ4IO_prefs_t* prefs, int xxhash); /* Default setting : stream checksum enabled */ -int LZ4IO_setStreamChecksumMode(int xxhash); +int LZ4IO_setStreamChecksumMode(LZ4IO_prefs_t* prefs, int xxhash); /* Default setting : 0 (no notification) */ int LZ4IO_setNotificationLevel(int level); /* Default setting : 0 (disabled) */ -int LZ4IO_setSparseFile(int enable); +int LZ4IO_setSparseFile(LZ4IO_prefs_t* prefs, int enable); /* Default setting : 0 == no content size present in frame header */ -int LZ4IO_setContentSize(int enable); +int LZ4IO_setContentSize(LZ4IO_prefs_t* prefs, int enable); /* Default setting : 0 == src file preserved */ -void LZ4IO_setRemoveSrcFile(unsigned flag); +void LZ4IO_setRemoveSrcFile(LZ4IO_prefs_t* prefs, unsigned flag); /* Default setting : 0 == favor compression ratio * Note : 1 only works for high compression levels (10+) */ -void LZ4IO_favorDecSpeed(int favor); +void LZ4IO_favorDecSpeed(LZ4IO_prefs_t* prefs, int favor); #endif /* LZ4IO_H_237902873 */ -- cgit v1.2.3 From 416916146f049e8f9fc5c3aa586b5e5e8f784ce5 Mon Sep 17 00:00:00 2001 From: Tim Zakian <2895723+tzakian@users.noreply.github.com> Date: Thu, 10 Jan 2019 20:40:00 -0800 Subject: Add constant pointer annotations --- programs/lz4cli.c | 4 ++-- programs/lz4io.c | 69 ++++++++++++++++++++++++++++--------------------------- programs/lz4io.h | 36 ++++++++++++++--------------- 3 files changed, 55 insertions(+), 54 deletions(-) diff --git a/programs/lz4cli.c b/programs/lz4cli.c index d956b87..464e43b 100644 --- a/programs/lz4cli.c +++ b/programs/lz4cli.c @@ -92,7 +92,7 @@ static unsigned displayLevel = 2; /* 0 : no display ; 1: errors only ; 2 : dow ***************************************/ #define DEFAULT_COMPRESSOR LZ4IO_compressFilename #define DEFAULT_DECOMPRESSOR LZ4IO_decompressFilename -int LZ4IO_compressFilename_Legacy(LZ4IO_prefs_t* prefs, const char* input_filename, const char* output_filename, int compressionlevel); /* hidden function */ +int LZ4IO_compressFilename_Legacy(LZ4IO_prefs_t* const prefs, const char* input_filename, const char* output_filename, int compressionlevel); /* hidden function */ /*-*************************** @@ -318,7 +318,7 @@ int main(int argc, const char** argv) char* dynNameSpace = NULL; const char** inFileNames = (const char**) calloc(argc, sizeof(char*)); unsigned ifnIdx=0; - LZ4IO_prefs_t* prefs = LZ4IO_defaultPreferences(); + LZ4IO_prefs_t* const prefs = LZ4IO_defaultPreferences(); const char nullOutput[] = NULL_OUTPUT; const char extension[] = LZ4_EXTENSION; size_t blockSize = LZ4IO_setBlockSizeID(prefs, LZ4_BLOCKSIZEID_DEFAULT); diff --git a/programs/lz4io.c b/programs/lz4io.c index cff961b..2eeb154 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -157,7 +157,8 @@ struct LZ4IO_prefs_s { LZ4IO_prefs_t* LZ4IO_defaultPreferences(void) { - LZ4IO_prefs_t* ret = malloc(sizeof(LZ4IO_prefs_t)); + LZ4IO_prefs_t* const ret = malloc(sizeof(LZ4IO_prefs_t)); + if (!ret) EXM_THROW(21, "Allocation error : not enough memory"); ret->passThrough = 0; ret->overwrite = 1; ret->testMode = 0; @@ -175,13 +176,13 @@ LZ4IO_prefs_t* LZ4IO_defaultPreferences(void) return ret; } -void LZ4IO_freePreferences(LZ4IO_prefs_t* prefs) +void LZ4IO_freePreferences(LZ4IO_prefs_t* const prefs) { free(prefs); } -int LZ4IO_setDictionaryFilename(LZ4IO_prefs_t* prefs, const char* dictionaryFilename) +int LZ4IO_setDictionaryFilename(LZ4IO_prefs_t* const prefs, const char* dictionaryFilename) { prefs->dictionaryFilename = dictionaryFilename; prefs->useDictionary = dictionaryFilename != NULL; @@ -189,7 +190,7 @@ int LZ4IO_setDictionaryFilename(LZ4IO_prefs_t* prefs, const char* dictionaryFile } /* Default setting : passThrough = 0; return : passThrough mode (0/1) */ -int LZ4IO_setPassThrough(LZ4IO_prefs_t* prefs, int yes) +int LZ4IO_setPassThrough(LZ4IO_prefs_t* const prefs, int yes) { prefs->passThrough = (yes!=0); return prefs->passThrough; @@ -197,21 +198,21 @@ int LZ4IO_setPassThrough(LZ4IO_prefs_t* prefs, int yes) /* Default setting : overwrite = 1; return : overwrite mode (0/1) */ -int LZ4IO_setOverwrite(LZ4IO_prefs_t* prefs, int yes) +int LZ4IO_setOverwrite(LZ4IO_prefs_t* const prefs, int yes) { prefs->overwrite = (yes!=0); return prefs->overwrite; } /* Default setting : testMode = 0; return : testMode (0/1) */ -int LZ4IO_setTestMode(LZ4IO_prefs_t* prefs, int yes) +int LZ4IO_setTestMode(LZ4IO_prefs_t* const prefs, int yes) { prefs->testMode = (yes!=0); return prefs->testMode; } /* blockSizeID : valid values : 4-5-6-7 */ -size_t LZ4IO_setBlockSizeID(LZ4IO_prefs_t* prefs, unsigned bsid) +size_t LZ4IO_setBlockSizeID(LZ4IO_prefs_t* const prefs, unsigned bsid) { static const size_t blockSizeTable[] = { 64 KB, 256 KB, 1 MB, 4 MB }; static const unsigned minBlockSizeID = 4; @@ -222,7 +223,7 @@ size_t LZ4IO_setBlockSizeID(LZ4IO_prefs_t* prefs, unsigned bsid) return prefs->blockSize; } -size_t LZ4IO_setBlockSize(LZ4IO_prefs_t* prefs, size_t blockSize) +size_t LZ4IO_setBlockSize(LZ4IO_prefs_t* const prefs, size_t blockSize) { static const size_t minBlockSize = 32; static const size_t maxBlockSize = 4 MB; @@ -239,21 +240,21 @@ size_t LZ4IO_setBlockSize(LZ4IO_prefs_t* prefs, size_t blockSize) return prefs->blockSize; } -int LZ4IO_setBlockMode(LZ4IO_prefs_t* prefs, LZ4IO_blockMode_t blockMode) +int LZ4IO_setBlockMode(LZ4IO_prefs_t* const prefs, LZ4IO_blockMode_t blockMode) { prefs->blockIndependence = (blockMode == LZ4IO_blockIndependent); return prefs->blockIndependence; } /* Default setting : no block checksum */ -int LZ4IO_setBlockChecksumMode(LZ4IO_prefs_t* prefs, int enable) +int LZ4IO_setBlockChecksumMode(LZ4IO_prefs_t* const prefs, int enable) { prefs->blockChecksum = (enable != 0); return prefs->blockChecksum; } /* Default setting : checksum enabled */ -int LZ4IO_setStreamChecksumMode(LZ4IO_prefs_t* prefs, int enable) +int LZ4IO_setStreamChecksumMode(LZ4IO_prefs_t* const prefs, int enable) { prefs->streamChecksum = (enable != 0); return prefs->streamChecksum; @@ -267,26 +268,26 @@ int LZ4IO_setNotificationLevel(int level) } /* Default setting : 0 (disabled) */ -int LZ4IO_setSparseFile(LZ4IO_prefs_t* prefs, int enable) +int LZ4IO_setSparseFile(LZ4IO_prefs_t* const prefs, int enable) { prefs->sparseFileSupport = (enable!=0); return prefs->sparseFileSupport; } /* Default setting : 0 (disabled) */ -int LZ4IO_setContentSize(LZ4IO_prefs_t* prefs, int enable) +int LZ4IO_setContentSize(LZ4IO_prefs_t* const prefs, int enable) { prefs->contentSizeFlag = (enable!=0); return prefs->contentSizeFlag; } /* Default setting : 0 (disabled) */ -void LZ4IO_favorDecSpeed(LZ4IO_prefs_t* prefs, int favor) +void LZ4IO_favorDecSpeed(LZ4IO_prefs_t* const prefs, int favor) { prefs->favorDecSpeed = (favor!=0); } -void LZ4IO_setRemoveSrcFile(LZ4IO_prefs_t* prefs, unsigned flag) +void LZ4IO_setRemoveSrcFile(LZ4IO_prefs_t* const prefs, unsigned flag) { prefs->removeSrcFile = (flag>0); } @@ -324,7 +325,7 @@ static FILE* LZ4IO_openSrcFile(const char* srcFileName) /** FIO_openDstFile() : * condition : `dstFileName` must be non-NULL. * @result : FILE* to `dstFileName`, or NULL if it fails */ -static FILE* LZ4IO_openDstFile(LZ4IO_prefs_t* prefs, const char* dstFileName) +static FILE* LZ4IO_openDstFile(LZ4IO_prefs_t* const prefs, const char* dstFileName) { FILE* f; @@ -388,7 +389,7 @@ static int LZ4IO_LZ4_compress(const char* src, char* dst, int srcSize, int dstSi /* LZ4IO_compressFilename_Legacy : * This function is intentionally "hidden" (not published in .h) * It generates compressed streams using the old 'legacy' format */ -int LZ4IO_compressFilename_Legacy(LZ4IO_prefs_t* prefs, const char* input_filename, const char* output_filename, int compressionlevel) +int LZ4IO_compressFilename_Legacy(LZ4IO_prefs_t* const prefs, const char* input_filename, const char* output_filename, int compressionlevel) { typedef int (*compress_f)(const char* src, char* dst, int srcSize, int dstSize, int cLevel); compress_f const compressionFunction = (compressionlevel < 3) ? LZ4IO_LZ4_compress : LZ4_compress_HC; @@ -485,7 +486,7 @@ typedef struct { LZ4F_CDict* cdict; } cRess_t; -static void* LZ4IO_createDict(LZ4IO_prefs_t* prefs, size_t *dictSize) { +static void* LZ4IO_createDict(LZ4IO_prefs_t* const prefs, size_t *dictSize) { size_t readSize; size_t dictEnd = 0; size_t dictLen = 0; @@ -543,7 +544,7 @@ static void* LZ4IO_createDict(LZ4IO_prefs_t* prefs, size_t *dictSize) { return dictBuf; } -static LZ4F_CDict* LZ4IO_createCDict(LZ4IO_prefs_t* prefs) { +static LZ4F_CDict* LZ4IO_createCDict(LZ4IO_prefs_t* const prefs) { size_t dictionarySize; void* dictionaryBuffer; LZ4F_CDict* cdict; @@ -557,7 +558,7 @@ static LZ4F_CDict* LZ4IO_createCDict(LZ4IO_prefs_t* prefs) { return cdict; } -static cRess_t LZ4IO_createCResources(LZ4IO_prefs_t* prefs) +static cRess_t LZ4IO_createCResources(LZ4IO_prefs_t* const prefs) { const size_t blockSize = prefs->blockSize; cRess_t ress; @@ -594,7 +595,7 @@ static void LZ4IO_freeCResources(cRess_t ress) * result : 0 : compression completed correctly * 1 : missing or pb opening srcFileName */ -static int LZ4IO_compressFilename_extRess(LZ4IO_prefs_t* io_prefs, cRess_t ress, const char* srcFileName, const char* dstFileName, int compressionLevel) +static int LZ4IO_compressFilename_extRess(LZ4IO_prefs_t* const io_prefs, cRess_t ress, const char* srcFileName, const char* dstFileName, int compressionLevel) { unsigned long long filesize = 0; unsigned long long compressedfilesize = 0; @@ -718,7 +719,7 @@ static int LZ4IO_compressFilename_extRess(LZ4IO_prefs_t* io_prefs, cRess_t ress, } -int LZ4IO_compressFilename(LZ4IO_prefs_t* prefs, const char* srcFileName, const char* dstFileName, int compressionLevel) +int LZ4IO_compressFilename(LZ4IO_prefs_t* const prefs, const char* srcFileName, const char* dstFileName, int compressionLevel) { UTIL_time_t const timeStart = UTIL_getTime(); clock_t const cpuStart = clock(); @@ -743,7 +744,7 @@ int LZ4IO_compressFilename(LZ4IO_prefs_t* prefs, const char* srcFileName, const #define FNSPACE 30 -int LZ4IO_compressMultipleFilenames(LZ4IO_prefs_t* prefs, const char** inFileNamesTable, int ifntSize, const char* suffix, int compressionLevel) +int LZ4IO_compressMultipleFilenames(LZ4IO_prefs_t* const prefs, const char** inFileNamesTable, int ifntSize, const char* suffix, int compressionLevel) { int i; int missed_files = 0; @@ -788,7 +789,7 @@ static unsigned LZ4IO_readLE32 (const void* s) } -static unsigned LZ4IO_fwriteSparse(LZ4IO_prefs_t* prefs, FILE* file, const void* buffer, size_t bufferSize, unsigned storedSkips) +static unsigned LZ4IO_fwriteSparse(LZ4IO_prefs_t* const prefs, FILE* file, const void* buffer, size_t bufferSize, unsigned storedSkips) { const size_t sizeT = sizeof(size_t); const size_t maskT = sizeT -1 ; @@ -867,7 +868,7 @@ static void LZ4IO_fwriteSparseEnd(FILE* file, unsigned storedSkips) static unsigned g_magicRead = 0; /* out-parameter of LZ4IO_decodeLegacyStream() */ -static unsigned long long LZ4IO_decodeLegacyStream(LZ4IO_prefs_t* prefs, FILE* finput, FILE* foutput) +static unsigned long long LZ4IO_decodeLegacyStream(LZ4IO_prefs_t* const prefs, FILE* finput, FILE* foutput) { unsigned long long streamSize = 0; unsigned storedSkips = 0; @@ -927,7 +928,7 @@ typedef struct { size_t dictBufferSize; } dRess_t; -static void LZ4IO_loadDDict(LZ4IO_prefs_t* prefs, dRess_t* ress) { +static void LZ4IO_loadDDict(LZ4IO_prefs_t* const prefs, dRess_t* ress) { if (!prefs->useDictionary) { ress->dictBuffer = NULL; ress->dictBufferSize = 0; @@ -939,7 +940,7 @@ static void LZ4IO_loadDDict(LZ4IO_prefs_t* prefs, dRess_t* ress) { } static const size_t LZ4IO_dBufferSize = 64 KB; -static dRess_t LZ4IO_createDResources(LZ4IO_prefs_t* prefs) +static dRess_t LZ4IO_createDResources(LZ4IO_prefs_t* const prefs) { dRess_t ress; @@ -970,7 +971,7 @@ static void LZ4IO_freeDResources(dRess_t ress) } -static unsigned long long LZ4IO_decompressLZ4F(LZ4IO_prefs_t* prefs, dRess_t ress, FILE* srcFile, FILE* dstFile) +static unsigned long long LZ4IO_decompressLZ4F(LZ4IO_prefs_t* const prefs, dRess_t ress, FILE* srcFile, FILE* dstFile) { unsigned long long filesize = 0; LZ4F_errorCode_t nextToLoad; @@ -1026,7 +1027,7 @@ static unsigned long long LZ4IO_decompressLZ4F(LZ4IO_prefs_t* prefs, dRess_t res #define PTSIZE (64 KB) #define PTSIZET (PTSIZE / sizeof(size_t)) -static unsigned long long LZ4IO_passThrough(LZ4IO_prefs_t* prefs, FILE* finput, FILE* foutput, unsigned char MNstore[MAGICNUMBER_SIZE]) +static unsigned long long LZ4IO_passThrough(LZ4IO_prefs_t* const prefs, FILE* finput, FILE* foutput, unsigned char MNstore[MAGICNUMBER_SIZE]) { size_t buffer[PTSIZET]; size_t readBytes = 1; @@ -1066,7 +1067,7 @@ static int fseek_u32(FILE *fp, unsigned offset, int where) } #define ENDOFSTREAM ((unsigned long long)-1) -static unsigned long long selectDecoder(LZ4IO_prefs_t* prefs, dRess_t ress, FILE* finput, FILE* foutput) +static unsigned long long selectDecoder(LZ4IO_prefs_t* const prefs, dRess_t ress, FILE* finput, FILE* foutput) { unsigned char MNstore[MAGICNUMBER_SIZE]; unsigned magicNumber; @@ -1129,7 +1130,7 @@ static unsigned long long selectDecoder(LZ4IO_prefs_t* prefs, dRess_t ress, FILE } -static int LZ4IO_decompressSrcFile(LZ4IO_prefs_t* prefs, dRess_t ress, const char* input_filename, const char* output_filename) +static int LZ4IO_decompressSrcFile(LZ4IO_prefs_t* const prefs, dRess_t ress, const char* input_filename, const char* output_filename) { FILE* const foutput = ress.dstFile; unsigned long long filesize = 0; @@ -1162,7 +1163,7 @@ static int LZ4IO_decompressSrcFile(LZ4IO_prefs_t* prefs, dRess_t ress, const cha } -static int LZ4IO_decompressDstFile(LZ4IO_prefs_t* prefs, dRess_t ress, const char* input_filename, const char* output_filename) +static int LZ4IO_decompressDstFile(LZ4IO_prefs_t* const prefs, dRess_t ress, const char* input_filename, const char* output_filename) { stat_t statbuf; int stat_result = 0; @@ -1190,7 +1191,7 @@ static int LZ4IO_decompressDstFile(LZ4IO_prefs_t* prefs, dRess_t ress, const cha } -int LZ4IO_decompressFilename(LZ4IO_prefs_t* prefs, const char* input_filename, const char* output_filename) +int LZ4IO_decompressFilename(LZ4IO_prefs_t* const prefs, const char* input_filename, const char* output_filename) { dRess_t const ress = LZ4IO_createDResources(prefs); clock_t const start = clock(); @@ -1206,7 +1207,7 @@ int LZ4IO_decompressFilename(LZ4IO_prefs_t* prefs, const char* input_filename, c } -int LZ4IO_decompressMultipleFilenames(LZ4IO_prefs_t* prefs, const char** inFileNamesTable, int ifntSize, const char* suffix) +int LZ4IO_decompressMultipleFilenames(LZ4IO_prefs_t* const prefs, const char** inFileNamesTable, int ifntSize, const char* suffix) { int i; int skippedFiles = 0; diff --git a/programs/lz4io.h b/programs/lz4io.h index 4b4740a..0c28784 100644 --- a/programs/lz4io.h +++ b/programs/lz4io.h @@ -55,70 +55,70 @@ static const char nulmark[] = "/dev/null"; typedef struct LZ4IO_prefs_s LZ4IO_prefs_t; LZ4IO_prefs_t* LZ4IO_defaultPreferences(void); -void LZ4IO_freePreferences(LZ4IO_prefs_t* prefs); +void LZ4IO_freePreferences(LZ4IO_prefs_t* const prefs); /* ************************************************** */ /* ****************** Functions ********************* */ /* ************************************************** */ -int LZ4IO_compressFilename (LZ4IO_prefs_t* prefs, const char* input_filename, const char* output_filename, int compressionlevel); -int LZ4IO_decompressFilename(LZ4IO_prefs_t* prefs, const char* input_filename, const char* output_filename); +int LZ4IO_compressFilename(LZ4IO_prefs_t* const prefs, const char* input_filename, const char* output_filename, int compressionlevel); +int LZ4IO_decompressFilename(LZ4IO_prefs_t* const prefs, const char* input_filename, const char* output_filename); -int LZ4IO_compressMultipleFilenames(LZ4IO_prefs_t* prefs, const char** inFileNamesTable, int ifntSize, const char* suffix, int compressionlevel); -int LZ4IO_decompressMultipleFilenames(LZ4IO_prefs_t* prefs, const char** inFileNamesTable, int ifntSize, const char* suffix); +int LZ4IO_compressMultipleFilenames(LZ4IO_prefs_t* const prefs, const char** inFileNamesTable, int ifntSize, const char* suffix, int compressionlevel); +int LZ4IO_decompressMultipleFilenames(LZ4IO_prefs_t* const prefs, const char** inFileNamesTable, int ifntSize, const char* suffix); /* ************************************************** */ /* ****************** Parameters ******************** */ /* ************************************************** */ -int LZ4IO_setDictionaryFilename(LZ4IO_prefs_t* prefs, const char* dictionaryFilename); +int LZ4IO_setDictionaryFilename(LZ4IO_prefs_t* const prefs, const char* dictionaryFilename); /* Default setting : passThrough = 0; return : passThrough mode (0/1) */ -int LZ4IO_setPassThrough(LZ4IO_prefs_t* prefs, int yes); +int LZ4IO_setPassThrough(LZ4IO_prefs_t* const prefs, int yes); /* Default setting : overwrite = 1; return : overwrite mode (0/1) */ -int LZ4IO_setOverwrite(LZ4IO_prefs_t* prefs, int yes); +int LZ4IO_setOverwrite(LZ4IO_prefs_t* const prefs, int yes); /* Default setting : testMode = 0; return : testMode (0/1) */ -int LZ4IO_setTestMode(LZ4IO_prefs_t* prefs, int yes); +int LZ4IO_setTestMode(LZ4IO_prefs_t* const prefs, int yes); /* blockSizeID : valid values : 4-5-6-7 return : 0 if error, blockSize if OK */ -size_t LZ4IO_setBlockSizeID(LZ4IO_prefs_t* prefs, unsigned blockSizeID); +size_t LZ4IO_setBlockSizeID(LZ4IO_prefs_t* const prefs, unsigned blockSizeID); /* blockSize : valid values : 32 -> 4MB return : 0 if error, actual blocksize if OK */ -size_t LZ4IO_setBlockSize(LZ4IO_prefs_t* prefs, size_t blockSize); +size_t LZ4IO_setBlockSize(LZ4IO_prefs_t* const prefs, size_t blockSize); /* Default setting : independent blocks */ typedef enum { LZ4IO_blockLinked=0, LZ4IO_blockIndependent} LZ4IO_blockMode_t; -int LZ4IO_setBlockMode(LZ4IO_prefs_t* prefs, LZ4IO_blockMode_t blockMode); +int LZ4IO_setBlockMode(LZ4IO_prefs_t* const prefs, LZ4IO_blockMode_t blockMode); /* Default setting : no block checksum */ -int LZ4IO_setBlockChecksumMode(LZ4IO_prefs_t* prefs, int xxhash); +int LZ4IO_setBlockChecksumMode(LZ4IO_prefs_t* const prefs, int xxhash); /* Default setting : stream checksum enabled */ -int LZ4IO_setStreamChecksumMode(LZ4IO_prefs_t* prefs, int xxhash); +int LZ4IO_setStreamChecksumMode(LZ4IO_prefs_t* const prefs, int xxhash); /* Default setting : 0 (no notification) */ int LZ4IO_setNotificationLevel(int level); /* Default setting : 0 (disabled) */ -int LZ4IO_setSparseFile(LZ4IO_prefs_t* prefs, int enable); +int LZ4IO_setSparseFile(LZ4IO_prefs_t* const prefs, int enable); /* Default setting : 0 == no content size present in frame header */ -int LZ4IO_setContentSize(LZ4IO_prefs_t* prefs, int enable); +int LZ4IO_setContentSize(LZ4IO_prefs_t* const prefs, int enable); /* Default setting : 0 == src file preserved */ -void LZ4IO_setRemoveSrcFile(LZ4IO_prefs_t* prefs, unsigned flag); +void LZ4IO_setRemoveSrcFile(LZ4IO_prefs_t* const prefs, unsigned flag); /* Default setting : 0 == favor compression ratio * Note : 1 only works for high compression levels (10+) */ -void LZ4IO_favorDecSpeed(LZ4IO_prefs_t* prefs, int favor); +void LZ4IO_favorDecSpeed(LZ4IO_prefs_t* const prefs, int favor); #endif /* LZ4IO_H_237902873 */ -- cgit v1.2.3 From c1610690b12407e1cd4470b0ef3908cf78646294 Mon Sep 17 00:00:00 2001 From: Tim Zakian <2895723+tzakian@users.noreply.github.com> Date: Fri, 11 Jan 2019 09:49:26 -0800 Subject: Add cast around malloc --- programs/lz4io.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/programs/lz4io.c b/programs/lz4io.c index 2eeb154..6bb6c48 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -157,7 +157,7 @@ struct LZ4IO_prefs_s { LZ4IO_prefs_t* LZ4IO_defaultPreferences(void) { - LZ4IO_prefs_t* const ret = malloc(sizeof(LZ4IO_prefs_t)); + LZ4IO_prefs_t* const ret = (LZ4IO_prefs_t*)malloc(sizeof(LZ4IO_prefs_t)); if (!ret) EXM_THROW(21, "Allocation error : not enough memory"); ret->passThrough = 0; ret->overwrite = 1; -- cgit v1.2.3 From 929dbbcddf780882d9f6b4175a533712379ffeb2 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Wed, 23 Jan 2019 15:40:26 +0700 Subject: meson: Add -DLZ4_DLL_EXPORT=1 to build dynamic lib on Windows Thanks @nacho for pointing it out. --- contrib/meson/lib/meson.build | 7 ++++++- contrib/meson/meson.build | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/contrib/meson/lib/meson.build b/contrib/meson/lib/meson.build index f1deb58..e782334 100644 --- a/contrib/meson/lib/meson.build +++ b/contrib/meson/lib/meson.build @@ -27,6 +27,10 @@ if use_debug endif liblz4_c_args += cc.get_supported_arguments(liblz4_debug_cflags) +if host_machine_os == os_windows and default_library != 'static' + liblz4_c_args += '-DLZ4_DLL_EXPORT=1' +endif + liblz4 = library('lz4', liblz4_sources, include_directories: liblz4_includes, @@ -47,6 +51,7 @@ pkgconfig.generate(liblz4, install_headers(join_paths(lz4_root_dir, 'lib/lz4.h'), join_paths(lz4_root_dir, 'lib/lz4hc.h'), join_paths(lz4_root_dir, 'lib/lz4frame.h')) -if get_option('default_library') != 'shared' + +if default_library != 'shared' install_headers(join_paths(lz4_root_dir, 'lib/lz4frame_static.h')) endif diff --git a/contrib/meson/meson.build b/contrib/meson/meson.build index e25e7c0..bf30eae 100644 --- a/contrib/meson/meson.build +++ b/contrib/meson/meson.build @@ -18,6 +18,7 @@ cc = meson.get_compiler('c') pkgconfig = import('pkgconfig') python3 = import('python').find_installation() c_std = get_option('c_std') +default_library = get_option('default_library') host_machine_os = host_machine.system() os_windows = 'windows' -- cgit v1.2.3 From 26e7635a0e3898bafb9e2299b8c95dd571ebe7b1 Mon Sep 17 00:00:00 2001 From: Jeremy Maitin-Shepard Date: Mon, 4 Feb 2019 12:22:56 -0800 Subject: Eliminate optimize attribute warning with clang on PPC64LE --- lib/lz4.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/lz4.c b/lib/lz4.c index 53eff2e..884a9b4 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -143,7 +143,7 @@ * and also LZ4_wildCopy is forcibly inlined, so that the O2 attribute * of LZ4_wildCopy does not affect the compression speed. */ -#if defined(__PPC64__) && defined(__LITTLE_ENDIAN__) && defined(__GNUC__) +#if defined(__PPC64__) && defined(__LITTLE_ENDIAN__) && defined(__GNUC__) && !defined(__clang__) # define LZ4_FORCE_O2_GCC_PPC64LE __attribute__((optimize("O2"))) # define LZ4_FORCE_O2_INLINE_GCC_PPC64LE __attribute__((optimize("O2"))) LZ4_FORCE_INLINE #else -- cgit v1.2.3 From 71ec7fde1e7e93abf1157c6a25185b33efa916ad Mon Sep 17 00:00:00 2001 From: Areg Melik-Adamyan Date: Wed, 6 Feb 2019 22:29:31 -0600 Subject: Build fixed by removing unavailable project --- visual/VS2017/lz4.sln | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/visual/VS2017/lz4.sln b/visual/VS2017/lz4.sln index 78f223b..72e98fc 100644 --- a/visual/VS2017/lz4.sln +++ b/visual/VS2017/lz4.sln @@ -1,7 +1,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Express 2012 for Windows Desktop -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "lz4", "lz4\lz4.vcxproj", "{E30329AC-0057-4FE0-8FDA-7F650D398C4C}" -EndProject +# Visual Studio 15 +VisualStudioVersion = 15.0.28307.271 +MinimumVisualStudioVersion = 10.0.40219.1 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "liblz4-dll", "liblz4-dll\liblz4-dll.vcxproj", "{9800039D-4AAA-43A4-BB78-FEF6F4836927}" EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "liblz4", "liblz4\liblz4.vcxproj", "{9092C5CC-3E71-41B3-BF68-4A7BDD8A5476}" @@ -27,14 +27,6 @@ Global Release|x64 = Release|x64 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {E30329AC-0057-4FE0-8FDA-7F650D398C4C}.Debug|Win32.ActiveCfg = Debug|Win32 - {E30329AC-0057-4FE0-8FDA-7F650D398C4C}.Debug|Win32.Build.0 = Debug|Win32 - {E30329AC-0057-4FE0-8FDA-7F650D398C4C}.Debug|x64.ActiveCfg = Debug|x64 - {E30329AC-0057-4FE0-8FDA-7F650D398C4C}.Debug|x64.Build.0 = Debug|x64 - {E30329AC-0057-4FE0-8FDA-7F650D398C4C}.Release|Win32.ActiveCfg = Release|Win32 - {E30329AC-0057-4FE0-8FDA-7F650D398C4C}.Release|Win32.Build.0 = Release|Win32 - {E30329AC-0057-4FE0-8FDA-7F650D398C4C}.Release|x64.ActiveCfg = Release|x64 - {E30329AC-0057-4FE0-8FDA-7F650D398C4C}.Release|x64.Build.0 = Release|x64 {9800039D-4AAA-43A4-BB78-FEF6F4836927}.Debug|Win32.ActiveCfg = Debug|Win32 {9800039D-4AAA-43A4-BB78-FEF6F4836927}.Debug|Win32.Build.0 = Debug|Win32 {9800039D-4AAA-43A4-BB78-FEF6F4836927}.Debug|x64.ActiveCfg = Debug|x64 @@ -95,4 +87,7 @@ Global GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {BBC259B2-BABF-47CD-8A6A-7B8318A803AC} + EndGlobalSection EndGlobal -- cgit v1.2.3 From 4da336062e5b295c0a54bb3683cdc6ca9ba23ca3 Mon Sep 17 00:00:00 2001 From: Dave Watson Date: Thu, 24 Jan 2019 13:07:57 -0800 Subject: decompress_generic: Refactor variable length fields Make a helper function to read variable lengths for literals and match length. --- lib/lz4.c | 47 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 53eff2e..4a063eb 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1434,6 +1434,35 @@ typedef enum { decode_full_block = 0, partial_decode = 1 } earlyEnd_directive; #undef MIN #define MIN(a,b) ( (a) < (b) ? (a) : (b) ) +/* Read the variable-length literal or match length. + * + * ip - pointer to use as input. + * lencheck - end ip. Return an error if ip advances >= lencheck. + * loop_check - check ip >= lencheck in body of loop. Returns loop_error if so. + * initial_check - check ip >= lencheck before start of loop. Returns initial_error if so. + * error (output) - error code. Should be set to 0 before call. + */ +typedef enum { loop_error = -2, initial_error = -1, ok = 0} variable_length_error; +LZ4_FORCE_INLINE unsigned read_variable_length(const BYTE**ip, const BYTE* lencheck, int loop_check, int initial_check, variable_length_error* error) { + unsigned length = 0; + unsigned s; + if (initial_check && unlikely((*ip) >= lencheck)) { /* overflow detection */ + *error = initial_error; + return length; + } + do { + s = **ip; + (*ip)++; + length += s; + if (loop_check && unlikely((*ip) >= lencheck)) { /* overflow detection */ + *error = loop_error; + return length; + } + } while (s==255); + + return length; +} + /*! LZ4_decompress_generic() : * This generic decompression function covers all use cases. * It shall be instantiated several times, using different sets of directives. @@ -1536,12 +1565,9 @@ LZ4_decompress_generic( /* decode literal length */ if (length == RUN_MASK) { - unsigned s; - if (unlikely(endOnInput ? ip >= iend-RUN_MASK : 0)) goto _output_error; /* overflow detection */ - do { - s = *ip++; - length += s; - } while ( likely(endOnInput ? ip iend-LASTLITERALS)) goto _output_error; - length += s; - } while (s==255); + variable_length_error error = ok; + length += read_variable_length(&ip, iend - LASTLITERALS + 1, endOnInput, 0, &error); + if (error != ok) goto _output_error; if ((safeDecode) && unlikely((uptrval)(op)+length<(uptrval)op)) goto _output_error; /* overflow detection */ } length += MINMATCH; -- cgit v1.2.3 From 28356e02ad6f6dac529302cedf707712c5b628fe Mon Sep 17 00:00:00 2001 From: Dave Watson Date: Thu, 24 Jan 2019 13:42:25 -0800 Subject: decompress_generic: Add a loop fastpath Copy the main loop, and change checks such that op is always less than oend-SAFE_DISTANCE. Currently these are added for the literal copy length check, and for the match copy length check. Otherwise the first loop is exactly the same as the second. Follow on diffs will optimize the first copy loop based on this new requirement. I also tried instead making a separate inlineable function for the copy loop (similar to existing partialDecode flags, etc), but I think the changes might be significant enough to warrent doubling the code, instead pulling out common functionality to separate functions. This is the basic transformation that will allow several following optimisations. --- lib/lz4.c | 158 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 153 insertions(+), 5 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 4a063eb..a2b49e9 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -307,6 +307,7 @@ void LZ4_wildCopy(void* dstPtr, const void* srcPtr, void* dstEnd) #define LASTLITERALS 5 /* see ../doc/lz4_Block_format.md#parsing-restrictions */ #define MFLIMIT 12 /* see ../doc/lz4_Block_format.md#parsing-restrictions */ #define MATCH_SAFEGUARD_DISTANCE ((2*WILDCOPYLENGTH) - MINMATCH) /* ensure it's possible to write 2 x wildcopyLength without overflowing output buffer */ +#define FASTLOOP_SAFE_DISTANCE 32 static const int LZ4_minLength = (MFLIMIT+1); #define KB *(1 <<10) @@ -1500,10 +1501,17 @@ LZ4_decompress_generic( const int safeDecode = (endOnInput==endOnInputSize); const int checkOffset = ((safeDecode) && (dictSize < (int)(64 KB))); + /* Set up the "end" pointers for the shortcut. */ const BYTE* const shortiend = iend - (endOnInput ? 14 : 8) /*maxLL*/ - 2 /*offset*/; const BYTE* const shortoend = oend - (endOnInput ? 14 : 8) /*maxLL*/ - 18 /*maxML*/; + const BYTE* match; + size_t offset; + unsigned token; + size_t length; + + DEBUGLOG(5, "LZ4_decompress_generic (srcSize:%i, dstSize:%i)", srcSize, outputSize); /* Special cases */ @@ -1512,13 +1520,149 @@ LZ4_decompress_generic( if ((!endOnInput) && (unlikely(outputSize==0))) return (*ip==0 ? 1 : -1); if ((endOnInput) && unlikely(srcSize==0)) return -1; - /* Main Loop : decode sequences */ + if ((oend - op) < FASTLOOP_SAFE_DISTANCE) + goto safe_decode; + + /* Fast loop : decode sequences as long as output < iend-FASTLOOP_SAFE_DISTANCE */ while (1) { - const BYTE* match; - size_t offset; + /* Main fastloop assertion: We can always wildcopy FASTLOOP_SAFE_DISTANCE */ + assert(oend - op >= FASTLOOP_SAFE_DISTANCE); + + token = *ip++; + length = token >> ML_BITS; /* literal length */ + + assert(!endOnInput || ip <= iend); /* ip < iend before the increment */ + + /* decode literal length */ + if (length == RUN_MASK) { + variable_length_error error = ok; + length += read_variable_length(&ip, iend-RUN_MASK, endOnInput, endOnInput, &error); + if (error == initial_error) goto _output_error; + if ((safeDecode) && unlikely((uptrval)(op)+length<(uptrval)(op))) goto _output_error; /* overflow detection */ + if ((safeDecode) && unlikely((uptrval)(ip)+length<(uptrval)(ip))) goto _output_error; /* overflow detection */ + } + + /* copy literals */ + cpy = op+length; + LZ4_STATIC_ASSERT(MFLIMIT >= WILDCOPYLENGTH); + if ( ((endOnInput) && ((cpy>oend-FASTLOOP_SAFE_DISTANCE) || (ip+length>iend-(2+1+LASTLITERALS))) ) + || ((!endOnInput) && (cpy>oend-FASTLOOP_SAFE_DISTANCE)) ) + { + goto safe_literal_copy; + } else { + LZ4_wildCopy(op, ip, cpy); /* may overwrite up to WILDCOPYLENGTH beyond cpy */ + ip += length; op = cpy; + } + + /* get offset */ + offset = LZ4_readLE16(ip); ip+=2; + match = op - offset; + + /* get matchlength */ + length = token & ML_MASK; - unsigned const token = *ip++; - size_t length = token >> ML_BITS; /* literal length */ + if ((checkOffset) && (unlikely(match + dictSize < lowPrefix))) goto _output_error; /* Error : offset outside buffers */ + if (!partialDecoding) { + assert(oend > op); + assert(oend - op >= 4); + LZ4_write32(op, 0); /* silence an msan warning when offset==0; costs <1%; */ + } /* note : when partialDecoding, there is no guarantee that at least 4 bytes remain available in output buffer */ + + if (length == ML_MASK) { + variable_length_error error = ok; + length += read_variable_length(&ip, iend - LASTLITERALS + 1, endOnInput, 0, &error); + if (error != ok) goto _output_error; + if ((safeDecode) && unlikely((uptrval)(op)+length<(uptrval)op)) goto _output_error; /* overflow detection */ + } + length += MINMATCH; + + if (op + length >= oend - FASTLOOP_SAFE_DISTANCE) { + goto safe_match_copy; + } + + /* match starting within external dictionary */ + if ((dict==usingExtDict) && (match < lowPrefix)) { + if (unlikely(op+length > oend-LASTLITERALS)) { + if (partialDecoding) length = MIN(length, (size_t)(oend-op)); + else goto _output_error; /* doesn't respect parsing restriction */ + } + + if (length <= (size_t)(lowPrefix-match)) { + /* match fits entirely within external dictionary : just copy */ + memmove(op, dictEnd - (lowPrefix-match), length); + op += length; + } else { + /* match stretches into both external dictionary and current block */ + size_t const copySize = (size_t)(lowPrefix - match); + size_t const restSize = length - copySize; + memcpy(op, dictEnd - copySize, copySize); + op += copySize; + if (restSize > (size_t)(op - lowPrefix)) { /* overlap copy */ + BYTE* const endOfMatch = op + restSize; + const BYTE* copyFrom = lowPrefix; + while (op < endOfMatch) *op++ = *copyFrom++; + } else { + memcpy(op, lowPrefix, restSize); + op += restSize; + } } + continue; + } + + /* copy match within block */ + cpy = op + length; + + /* partialDecoding : may not respect endBlock parsing restrictions */ + assert(op<=oend); + if (partialDecoding && (cpy > oend-MATCH_SAFEGUARD_DISTANCE)) { + size_t const mlen = MIN(length, (size_t)(oend-op)); + const BYTE* const matchEnd = match + mlen; + BYTE* const copyEnd = op + mlen; + if (matchEnd > op) { /* overlap copy */ + while (op < copyEnd) *op++ = *match++; + } else { + memcpy(op, match, mlen); + } + op = copyEnd; + if (op==oend) goto decode_done; + continue; + } + + if (unlikely(offset<8)) { + op[0] = match[0]; + op[1] = match[1]; + op[2] = match[2]; + op[3] = match[3]; + match += inc32table[offset]; + memcpy(op+4, match, 4); + match -= dec64table[offset]; + } else { + memcpy(op, match, 8); + match += 8; + } + op += 8; + + if (unlikely(cpy > oend-MATCH_SAFEGUARD_DISTANCE)) { + BYTE* const oCopyLimit = oend - (WILDCOPYLENGTH-1); + if (cpy > oend-LASTLITERALS) goto _output_error; /* Error : last LASTLITERALS bytes must be literals (uncompressed) */ + if (op < oCopyLimit) { + LZ4_wildCopy(op, match, oCopyLimit); + match += oCopyLimit - op; + op = oCopyLimit; + } + while (op < cpy) *op++ = *match++; + } else { + memcpy(op, match, 8); + if (length > 16) LZ4_wildCopy(op+8, match+8, cpy); + } + op = cpy; /* wildcopy correction */ + } + + safe_decode: + + /* Main Loop : decode remaining sequences where output < FASTLOOP_SAFE_DISTANCE */ + while (1) { + token = *ip++; + length = token >> ML_BITS; /* literal length */ assert(!endOnInput || ip <= iend); /* ip < iend before the increment */ @@ -1574,6 +1718,7 @@ LZ4_decompress_generic( /* copy literals */ cpy = op+length; + safe_literal_copy: LZ4_STATIC_ASSERT(MFLIMIT >= WILDCOPYLENGTH); if ( ((endOnInput) && ((cpy>oend-MFLIMIT) || (ip+length>iend-(2+1+LASTLITERALS))) ) || ((!endOnInput) && (cpy>oend-WILDCOPYLENGTH)) ) @@ -1621,6 +1766,8 @@ LZ4_decompress_generic( } length += MINMATCH; + safe_match_copy: + /* match starting within external dictionary */ if ((dict==usingExtDict) && (match < lowPrefix)) { if (unlikely(op+length > oend-LASTLITERALS)) { @@ -1697,6 +1844,7 @@ LZ4_decompress_generic( } op = cpy; /* wildcopy correction */ } + decode_done: /* end of decoding */ if (endOnInput) -- cgit v1.2.3 From 5dfa7d422ba6c184a7c7694f56bcd36e38e5ed1a Mon Sep 17 00:00:00 2001 From: Dave Watson Date: Thu, 24 Jan 2019 14:17:24 -0800 Subject: decompress_generic: optimize match copy Add an LZ4_wildCopy16, that will wildcopy, potentially smashing up to 16 bytes, and use it for match copy. On x64, this avoids many blocked loads due to store forwarding, similar to issue #411. --- lib/lz4.c | 51 ++++++++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index a2b49e9..1e938d0 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -297,6 +297,16 @@ void LZ4_wildCopy(void* dstPtr, const void* srcPtr, void* dstEnd) do { memcpy(d,s,8); d+=8; s+=8; } while (d oend-MATCH_SAFEGUARD_DISTANCE)) { - BYTE* const oCopyLimit = oend - (WILDCOPYLENGTH-1); - if (cpy > oend-LASTLITERALS) goto _output_error; /* Error : last LASTLITERALS bytes must be literals (uncompressed) */ - if (op < oCopyLimit) { - LZ4_wildCopy(op, match, oCopyLimit); - match += oCopyLimit - op; - op = oCopyLimit; + if (unlikely(offset<16)) { + if (offset < 8) { + op[0] = match[0]; + op[1] = match[1]; + op[2] = match[2]; + op[3] = match[3]; + match += inc32table[offset]; + memcpy(op+4, match, 4); + match -= dec64table[offset]; + op += 8; + } else { + memcpy(op, match, 8); + op += 8; + match += 8; } - while (op < cpy) *op++ = *match++; - } else { + memcpy(op, match, 8); if (length > 16) LZ4_wildCopy(op+8, match+8, cpy); + } else { + LZ4_wildCopy16(op, match, cpy); } + op = cpy; /* wildcopy correction */ } -- cgit v1.2.3 From 59332a3026eaff40e4c6d3c627bdb51bfd26fea1 Mon Sep 17 00:00:00 2001 From: Dave Watson Date: Fri, 25 Jan 2019 14:40:27 -0800 Subject: decompress_generic: Optimize literal copies Use LZ4_wildCopy16 for variable-length literals. For literal counts that fit in the flag byte, copy directly. We can also omit oend checks for roughly the same reason as the previous shortcut: We check once that both match length and literal length fit in FASTLOOP_SAFE_DISTANCE, including wildcopy distance. --- lib/lz4.c | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 1e938d0..978a957 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1545,22 +1545,31 @@ LZ4_decompress_generic( /* decode literal length */ if (length == RUN_MASK) { - variable_length_error error = ok; - length += read_variable_length(&ip, iend-RUN_MASK, endOnInput, endOnInput, &error); - if (error == initial_error) goto _output_error; + variable_length_error error = ok; + length += read_variable_length(&ip, iend-RUN_MASK, endOnInput, endOnInput, &error); + if (error == initial_error) goto _output_error; if ((safeDecode) && unlikely((uptrval)(op)+length<(uptrval)(op))) goto _output_error; /* overflow detection */ if ((safeDecode) && unlikely((uptrval)(ip)+length<(uptrval)(ip))) goto _output_error; /* overflow detection */ - } - /* copy literals */ - cpy = op+length; - LZ4_STATIC_ASSERT(MFLIMIT >= WILDCOPYLENGTH); - if ( ((endOnInput) && ((cpy>oend-FASTLOOP_SAFE_DISTANCE) || (ip+length>iend-(2+1+LASTLITERALS))) ) - || ((!endOnInput) && (cpy>oend-FASTLOOP_SAFE_DISTANCE)) ) - { - goto safe_literal_copy; + /* copy literals */ + cpy = op+length; + LZ4_STATIC_ASSERT(MFLIMIT >= WILDCOPYLENGTH); + if ( ((endOnInput) && ((cpy>oend-FASTLOOP_SAFE_DISTANCE) || (ip+length>iend-(2+1+LASTLITERALS))) ) + || ((!endOnInput) && (cpy>oend-FASTLOOP_SAFE_DISTANCE)) ) + { + goto safe_literal_copy; + } + LZ4_wildCopy16(op, ip, cpy); + ip += length; op = cpy; } else { - LZ4_wildCopy(op, ip, cpy); /* may overwrite up to WILDCOPYLENGTH beyond cpy */ + cpy = op+length; + /* We don't need to check oend, since we check it once for each loop below */ + if ( ((endOnInput) && (ip+16>iend-(2+1+LASTLITERALS)))) + { + goto safe_literal_copy; + } + /* Literals can only be 14, but hope compilers optimize if we copy by a register size */ + memcpy(op, ip, 16); ip += length; op = cpy; } -- cgit v1.2.3 From 232f1e261fce421542e0cf7549f25e3064006bf0 Mon Sep 17 00:00:00 2001 From: Dave Watson Date: Fri, 25 Jan 2019 14:47:15 -0800 Subject: decompress_generic: drop partial copy check in fast loop We've already checked that we are more than FASTLOOP_SAFE_DISTANCE away from the end, so this branch can never be true, we will have already jumped to the second decode loop. --- lib/lz4.c | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 978a957..915ee74 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1632,20 +1632,6 @@ LZ4_decompress_generic( /* partialDecoding : may not respect endBlock parsing restrictions */ assert(op<=oend); - if (partialDecoding && (cpy > oend-MATCH_SAFEGUARD_DISTANCE)) { - size_t const mlen = MIN(length, (size_t)(oend-op)); - const BYTE* const matchEnd = match + mlen; - BYTE* const copyEnd = op + mlen; - if (matchEnd > op) { /* overlap copy */ - while (op < copyEnd) *op++ = *match++; - } else { - memcpy(op, match, mlen); - } - op = copyEnd; - if (op==oend) goto decode_done; - continue; - } - if (unlikely(offset<16)) { if (offset < 8) { op[0] = match[0]; @@ -1858,7 +1844,6 @@ LZ4_decompress_generic( } op = cpy; /* wildcopy correction */ } - decode_done: /* end of decoding */ if (endOnInput) -- cgit v1.2.3 From 28b824921d94c8263cb51dfdee0a149490009f8f Mon Sep 17 00:00:00 2001 From: Dave Watson Date: Fri, 25 Jan 2019 16:01:09 -0800 Subject: decompress_generic: re-add fastpath This is the remaineder of the original 'shortcut'. If true, we can avoid the loop in LZ4_wildCopy, and directly copy instead. --- lib/lz4.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 915ee74..a18401a 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1592,11 +1592,26 @@ LZ4_decompress_generic( length += read_variable_length(&ip, iend - LASTLITERALS + 1, endOnInput, 0, &error); if (error != ok) goto _output_error; if ((safeDecode) && unlikely((uptrval)(op)+length<(uptrval)op)) goto _output_error; /* overflow detection */ - } - length += MINMATCH; + length += MINMATCH; + if (op + length >= oend - FASTLOOP_SAFE_DISTANCE) { + goto safe_match_copy; + } + } else { + length += MINMATCH; + if (op + length >= oend - FASTLOOP_SAFE_DISTANCE) { + goto safe_match_copy; + } - if (op + length >= oend - FASTLOOP_SAFE_DISTANCE) { - goto safe_match_copy; + /* Fastpath check: Avoids a branch in LZ4_wildCopy16 if true */ + if (!(dict == usingExtDict) || (match >= lowPrefix)) { + if (offset >= 8) { + memcpy(op, match, 8); + memcpy(op+8, match+8, 8); + memcpy(op+16, match+16, 2); + op += length; + continue; + } + } } /* match starting within external dictionary */ -- cgit v1.2.3 From 1fbaf843066caec21489d178fc348dbe62939a90 Mon Sep 17 00:00:00 2001 From: Dave Watson Date: Fri, 25 Jan 2019 16:06:55 -0800 Subject: decompress_generic: remove msan write This store is also causing load-blocked-by-store issues, remove it. The msan warning will have to be fixed another way if it is still an issue. --- lib/lz4.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index a18401a..614df2b 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1581,11 +1581,6 @@ LZ4_decompress_generic( length = token & ML_MASK; if ((checkOffset) && (unlikely(match + dictSize < lowPrefix))) goto _output_error; /* Error : offset outside buffers */ - if (!partialDecoding) { - assert(oend > op); - assert(oend - op >= 4); - LZ4_write32(op, 0); /* silence an msan warning when offset==0; costs <1%; */ - } /* note : when partialDecoding, there is no guarantee that at least 4 bytes remain available in output buffer */ if (length == ML_MASK) { variable_length_error error = ok; -- cgit v1.2.3 From faac110e20bef7c5715c068294d1a6950450fad6 Mon Sep 17 00:00:00 2001 From: Dave Watson Date: Fri, 25 Jan 2019 16:19:05 -0800 Subject: decompress_generic: Unroll loops a bit more Generally we want our wildcopy loops to look like the memcpy loops from our libc, but without the final byte copy checks. We can unroll a bit to make long copies even faster. The only catch is that this affects the value of FASTLOOP_SAFE_DISTANCE. --- lib/lz4.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 614df2b..993d746 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -297,15 +297,15 @@ void LZ4_wildCopy(void* dstPtr, const void* srcPtr, void* dstEnd) do { memcpy(d,s,8); d+=8; s+=8; } while (d= lowPrefix)) { if (offset >= 8) { memcpy(op, match, 8); @@ -1661,7 +1661,7 @@ LZ4_decompress_generic( memcpy(op, match, 8); if (length > 16) LZ4_wildCopy(op+8, match+8, cpy); } else { - LZ4_wildCopy16(op, match, cpy); + LZ4_wildCopy32(op, match, cpy); } op = cpy; /* wildcopy correction */ -- cgit v1.2.3 From 75fb878a901900d0addd6950b4c9e4ab2bd3d1f2 Mon Sep 17 00:00:00 2001 From: Dave Watson Date: Mon, 28 Jan 2019 09:36:25 -0800 Subject: decompress_generic: Add fastpath for small offsets For small offsets of size 1, 2, 4 and 8, we can set a single uint64_t, and then use it to do a memset() variation. In particular, this makes the somewhat-common RLE (offset 1) about 2-4x faster than the previous implementation - we avoid not only the load blocked by store, but also avoid the loads entirely. --- lib/lz4.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 59 insertions(+), 19 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 993d746..066d535 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -297,6 +297,30 @@ void LZ4_wildCopy(void* dstPtr, const void* srcPtr, void* dstEnd) do { memcpy(d,s,8); d+=8; s+=8; } while (d 16) LZ4_wildCopy(op+8, match+8, cpy); + LZ4_memcpy_using_offset(op, match, cpy, offset); } else { LZ4_wildCopy32(op, match, cpy); } -- cgit v1.2.3 From 5d7d1166cb681cda79a3ac4bda774f06a13356ad Mon Sep 17 00:00:00 2001 From: Dave Watson Date: Fri, 8 Feb 2019 13:57:43 -0800 Subject: decompress_generic: Limit fastpath to x86 New fastpath currently shows a regression on qualcomm arm chips. Restrict it to x86 for now --- lib/lz4.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 066d535..48423f6 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -301,6 +301,7 @@ static const unsigned inc32table[8] = {0, 1, 2, 1, 0, 4, 4, 4}; static const int dec64table[8] = {0, 0, 0, -1, -4, 1, 2, 3}; +#if defined(__i386__) || defined(__x86_64__) LZ4_FORCE_O2_INLINE_GCC_PPC64LE void LZ4_memcpy_using_offset_base(BYTE* dstPtr, const BYTE* srcPtr, BYTE* dstEnd, const size_t offset) { if (offset < 8) { @@ -365,7 +366,7 @@ void LZ4_memcpy_using_offset(BYTE* dstPtr, const BYTE* srcPtr, BYTE* dstEnd, con dstPtr += 8; } } - +#endif /*-************************************ * Common Constants **************************************/ @@ -1586,6 +1587,8 @@ LZ4_decompress_generic( if ((!endOnInput) && (unlikely(outputSize==0))) return (*ip==0 ? 1 : -1); if ((endOnInput) && unlikely(srcSize==0)) return -1; + /* Currently the fast loop shows a regression on qualcomm arm chips. */ +#if defined(__i386__) || defined(__x86_64__) if ((oend - op) < FASTLOOP_SAFE_DISTANCE) goto safe_decode; @@ -1706,8 +1709,8 @@ LZ4_decompress_generic( op = cpy; /* wildcopy correction */ } - safe_decode: +#endif /* Main Loop : decode remaining sequences where output < FASTLOOP_SAFE_DISTANCE */ while (1) { @@ -1768,7 +1771,9 @@ LZ4_decompress_generic( /* copy literals */ cpy = op+length; +#if defined(__i386__) || defined(__x86_64__) safe_literal_copy: +#endif LZ4_STATIC_ASSERT(MFLIMIT >= WILDCOPYLENGTH); if ( ((endOnInput) && ((cpy>oend-MFLIMIT) || (ip+length>iend-(2+1+LASTLITERALS))) ) || ((!endOnInput) && (cpy>oend-WILDCOPYLENGTH)) ) @@ -1816,8 +1821,9 @@ LZ4_decompress_generic( } length += MINMATCH; +#if defined(__i386__) || defined(__x86_64__) safe_match_copy: - +#endif /* match starting within external dictionary */ if ((dict==usingExtDict) && (match < lowPrefix)) { if (unlikely(op+length > oend-LASTLITERALS)) { -- cgit v1.2.3 From 0360981f681b06e47c0516223165e96e309c6af6 Mon Sep 17 00:00:00 2001 From: Vincent Torri Date: Sun, 3 Mar 2019 22:06:38 +0100 Subject: Allow installation of lz4 for Windows 10 with MSYS2 --- Makefile | 2 +- lib/Makefile | 2 +- programs/Makefile | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 091b256..df54c73 100644 --- a/Makefile +++ b/Makefile @@ -89,7 +89,7 @@ clean: #----------------------------------------------------------------------------- # make install is validated only for Linux, OSX, BSD, Hurd and Solaris targets #----------------------------------------------------------------------------- -ifneq (,$(filter $(shell uname),Linux Darwin GNU/kFreeBSD GNU OpenBSD FreeBSD NetBSD DragonFly SunOS Haiku MidnightBSD MINGW32_NT-6.1 MINGW64_NT-6.1)) +ifneq (,$(filter $(shell uname),Linux Darwin GNU/kFreeBSD GNU OpenBSD FreeBSD NetBSD DragonFly SunOS Haiku MidnightBSD MINGW32_NT-6.1 MINGW64_NT-6.1 MINGW32_NT-10.0 MINGW64_NT-10.0)) HOST_OS = POSIX .PHONY: install uninstall diff --git a/lib/Makefile b/lib/Makefile index 0e760b4..cb1571c 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -127,7 +127,7 @@ clean: #----------------------------------------------------------------------------- # make install is validated only for Linux, OSX, BSD, Hurd and Solaris targets #----------------------------------------------------------------------------- -ifneq (,$(filter $(shell uname),Linux Darwin GNU/kFreeBSD GNU OpenBSD FreeBSD NetBSD DragonFly SunOS Haiku MidnightBSD MINGW32_NT-6.1 MINGW64_NT-6.1)) +ifneq (,$(filter $(shell uname),Linux Darwin GNU/kFreeBSD GNU OpenBSD FreeBSD NetBSD DragonFly SunOS Haiku MidnightBSD MINGW32_NT-6.1 MINGW64_NT-6.1 MINGW32_NT-10.0 MINGW64_NT-10.0)) .PHONY: listL120 listL120: # extract lines >= 120 characters in *.{c,h}, by Takayuki Matsuoka (note : $$, for Makefile compatibility) diff --git a/programs/Makefile b/programs/Makefile index 8a2ad65..af461fe 100644 --- a/programs/Makefile +++ b/programs/Makefile @@ -109,7 +109,7 @@ clean: #----------------------------------------------------------------------------- # make install is validated only for Linux, OSX, BSD, Hurd and Solaris targets #----------------------------------------------------------------------------- -ifneq (,$(filter $(shell uname),Linux Darwin GNU/kFreeBSD GNU OpenBSD FreeBSD NetBSD DragonFly SunOS Haiku MidnightBSD MINGW32_NT-6.1 MINGW64_NT-6.1)) +ifneq (,$(filter $(shell uname),Linux Darwin GNU/kFreeBSD GNU OpenBSD FreeBSD NetBSD DragonFly SunOS Haiku MidnightBSD MINGW32_NT-6.1 MINGW64_NT-6.1 MINGW32_NT-10.0 MINGW64_NT-10.0)) unlz4: lz4 ln -s lz4$(EXT) unlz4$(EXT) -- cgit v1.2.3 From 7d9d00f4df14f02a59f9c605a08af52a97032262 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 2 Apr 2019 16:06:37 -0700 Subject: fixed a few minor conversion warnings --- lib/lz4.c | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index cecfa52..de744c5 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -336,15 +336,15 @@ LZ4_FORCE_O2_INLINE_GCC_PPC64LE void LZ4_memcpy_using_offset(BYTE* dstPtr, const BYTE* srcPtr, BYTE* dstEnd, const size_t offset) { BYTE v[8]; switch(offset) { - case 1: + case 1: memset(v, *srcPtr, 8); goto copy_loop; - case 2: + case 2: memcpy(v, srcPtr, 2); memcpy(&v[2], srcPtr, 2); memcpy(&v[4], &v[0], 4); goto copy_loop; - case 4: + case 4: memcpy(v, srcPtr, 4); memcpy(&v[4], srcPtr, 4); goto copy_loop; @@ -353,7 +353,7 @@ void LZ4_memcpy_using_offset(BYTE* dstPtr, const BYTE* srcPtr, BYTE* dstEnd, con case 6: case 7: case 8: - default: + default: LZ4_memcpy_using_offset_base(dstPtr, srcPtr, dstEnd, offset); return; } @@ -532,7 +532,7 @@ static const U32 LZ4_skipTrigger = 6; /* Increase this value ==> compression ru /*-************************************ * Local Structures and types **************************************/ -typedef enum { +typedef enum { noLimit = 0, notLimited = 1, limitedOutput = 2, @@ -750,7 +750,7 @@ LZ4_FORCE_INLINE int LZ4_compress_generic( const tableType_t tableType, const dict_directive dictDirective, const dictIssue_directive dictIssue, - const U32 acceleration) + const int acceleration) { int result; const BYTE* ip = (const BYTE*) source; @@ -823,8 +823,8 @@ LZ4_FORCE_INLINE int LZ4_compress_generic( /* Find a match */ if (tableType == byPtr) { const BYTE* forwardIp = ip; - unsigned step = 1; - unsigned searchMatchNb = acceleration << LZ4_skipTrigger; + int step = 1; + int searchMatchNb = acceleration << LZ4_skipTrigger; do { U32 const h = forwardH; ip = forwardIp; @@ -844,8 +844,8 @@ LZ4_FORCE_INLINE int LZ4_compress_generic( } else { /* byU32, byU16 */ const BYTE* forwardIp = ip; - unsigned step = 1; - unsigned searchMatchNb = acceleration << LZ4_skipTrigger; + int step = 1; + int searchMatchNb = acceleration << LZ4_skipTrigger; do { U32 const h = forwardH; U32 const current = (U32)(forwardIp - base); @@ -916,7 +916,7 @@ LZ4_FORCE_INLINE int LZ4_compress_generic( goto _last_literals; } if (litLength >= RUN_MASK) { - int len = (int)litLength-RUN_MASK; + int len = (int)(litLength - RUN_MASK); *token = (RUN_MASK<= 255 ; len-=255) *op++ = 255; *op++ = (BYTE)len; @@ -1076,7 +1076,8 @@ _last_literals: (op + lastRun + 1 + ((lastRun+255-RUN_MASK)/255) > olimit)) { if (outputLimited == fillOutput) { /* adapt lastRun to fill 'dst' */ - lastRun = (olimit-op) - 1; + assert(olimit >= op); + lastRun = (size_t)(olimit-op) - 1; lastRun -= (lastRun+240)/255; } if (outputLimited == limitedOutput) @@ -1331,7 +1332,7 @@ int LZ4_loadDict (LZ4_stream_t* LZ4_dict, const char* dictionary, int dictSize) p+=3; } - return dict->dictSize; + return (int)dict->dictSize; } void LZ4_attach_dictionary(LZ4_stream_t *working_stream, const LZ4_stream_t *dictionary_stream) { @@ -1359,7 +1360,8 @@ void LZ4_attach_dictionary(LZ4_stream_t *working_stream, const LZ4_stream_t *dic static void LZ4_renormDictT(LZ4_stream_t_internal* LZ4_dict, int nextSize) { - if (LZ4_dict->currentOffset + nextSize > 0x80000000) { /* potential ptrdiff_t overflow (32-bits mode) */ + assert(nextSize >= 0); + if (LZ4_dict->currentOffset + (unsigned)nextSize > 0x80000000) { /* potential ptrdiff_t overflow (32-bits mode) */ /* rescale hash table */ U32 const delta = LZ4_dict->currentOffset - 64 KB; const BYTE* dictEnd = LZ4_dict->dictionary + LZ4_dict->dictSize; @@ -1482,7 +1484,7 @@ int LZ4_saveDict (LZ4_stream_t* LZ4_dict, char* safeBuffer, int dictSize) const BYTE* const previousDictEnd = dict->dictionary + dict->dictSize; if ((U32)dictSize > 64 KB) dictSize = 64 KB; /* useless to define a dictionary > 64 KB */ - if ((U32)dictSize > dict->dictSize) dictSize = dict->dictSize; + if ((U32)dictSize > dict->dictSize) dictSize = (int)dict->dictSize; memmove(safeBuffer, previousDictEnd - dictSize, dictSize); @@ -1510,7 +1512,7 @@ typedef enum { decode_full_block = 0, partial_decode = 1 } earlyEnd_directive; * lencheck - end ip. Return an error if ip advances >= lencheck. * loop_check - check ip >= lencheck in body of loop. Returns loop_error if so. * initial_check - check ip >= lencheck before start of loop. Returns initial_error if so. - * error (output) - error code. Should be set to 0 before call. + * error (output) - error code. Should be set to 0 before call. */ typedef enum { loop_error = -2, initial_error = -1, ok = 0} variable_length_error; LZ4_FORCE_INLINE unsigned read_variable_length(const BYTE**ip, const BYTE* lencheck, int loop_check, int initial_check, variable_length_error* error) { @@ -1596,7 +1598,7 @@ LZ4_decompress_generic( while (1) { /* Main fastloop assertion: We can always wildcopy FASTLOOP_SAFE_DISTANCE */ assert(oend - op >= FASTLOOP_SAFE_DISTANCE); - + token = *ip++; length = token >> ML_BITS; /* literal length */ @@ -1706,12 +1708,12 @@ LZ4_decompress_generic( } else { LZ4_wildCopy32(op, match, cpy); } - + op = cpy; /* wildcopy correction */ } safe_decode: #endif - + /* Main Loop : decode remaining sequences where output < FASTLOOP_SAFE_DISTANCE */ while (1) { token = *ip++; @@ -1779,7 +1781,7 @@ LZ4_decompress_generic( || ((!endOnInput) && (cpy>oend-WILDCOPYLENGTH)) ) { if (partialDecoding) { - if (cpy > oend) { cpy = oend; length = oend-op; } /* Partial decoding : stop in the middle of literal segment */ + if (cpy > oend) { cpy = oend; assert(op<=oend); length = (size_t)(oend-op); } /* Partial decoding : stop in the middle of literal segment */ if ((endOnInput) && (ip+length > iend)) goto _output_error; /* Error : read attempt beyond end of input buffer */ } else { if ((!endOnInput) && (cpy != oend)) goto _output_error; /* Error : block decoding must stop exactly there */ -- cgit v1.2.3 From 2589c4424ff56a9e6bb37b2be394e5e0c376e7a5 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 2 Apr 2019 16:22:11 -0700 Subject: created LZ4_FAST_DEC_LOOP build macro --- lib/README.md | 19 +++++++++++++++---- lib/lz4.c | 21 ++++++++++++++++----- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/lib/README.md b/lib/README.md index a705de6..e9b221f 100644 --- a/lib/README.md +++ b/lib/README.md @@ -42,17 +42,28 @@ Should they be nonetheless needed, it's possible to force their publication by using build macro `LZ4_PUBLISH_STATIC_FUNCTIONS`. +#### Build macros + +The following build macro can be determined at compilation time : + +- `LZ4_FAST_DEC_LOOP` : this triggers the optimized decompression loop. + This loops works great on x86/x64 cpus, and is automatically enabled on this platform. + It's possible to enable or disable it manually, by passing `LZ4_FAST_DEC_LOOP=1` or `0` to the preprocessor. + Typically with `gcc` : `-DLZ4_FAST_DEC_LOOP=1`, + and with `make` : `CPPFLAGS+=-DLZ4_FAST_DEC_LOOP=1 make lz4`. + + #### Amalgamation -lz4 code is able to be amalgamated into a single file. -We can combine all source code in `lz4_all.c` by using following command, +lz4 source code can be amalgamated into a single file. +One can combine all source code into `lz4_all.c` by using following command: ``` cat lz4.c > lz4_all.c cat lz4hc.c >> lz4_all.c cat lz4frame.c >> lz4_all.c ``` -and compile `lz4_all.c`. -It's necessary to include all `*.h` files present in `/lib` together with `lz4_all.c`. +(`cat` file order is important) then compile `lz4_all.c`. +All `*.h` files present in `/lib` remain necessary to compile `lz4_all.c`. #### Windows : using MinGW+MSYS to create DLL diff --git a/lib/lz4.c b/lib/lz4.c index de744c5..dafd972 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -202,6 +202,7 @@ typedef size_t reg_t; /* 32-bits in x32 mode */ #endif + /*-************************************ * Reading and writing into memory **************************************/ @@ -235,7 +236,7 @@ static reg_t LZ4_read_ARCH(const void* ptr) { return ((const unalign*)ptr)->uArc static void LZ4_write16(void* memPtr, U16 value) { ((unalign*)memPtr)->u16 = value; } static void LZ4_write32(void* memPtr, U32 value) { ((unalign*)memPtr)->u32 = value; } -#else /* safe and portable access through memcpy() */ +#else /* safe and portable access using memcpy() */ static U16 LZ4_read16(const void* memPtr) { @@ -301,7 +302,15 @@ static const unsigned inc32table[8] = {0, 1, 2, 1, 0, 4, 4, 4}; static const int dec64table[8] = {0, 0, 0, -1, -4, 1, 2, 3}; -#if defined(__i386__) || defined(__x86_64__) +#ifndef LZ4_FAST_DEC_LOOP +# if defined(__i386__) || defined(__x86_64__) +# define LZ4_FAST_DEC_LOOP 1 +# else +# define LZ4_FAST_DEC_LOOP 0 +# endif +#endif + +#if LZ4_FAST_DEC_LOOP LZ4_FORCE_O2_INLINE_GCC_PPC64LE void LZ4_memcpy_using_offset_base(BYTE* dstPtr, const BYTE* srcPtr, BYTE* dstEnd, const size_t offset) { if (offset < 8) { @@ -367,6 +376,8 @@ void LZ4_memcpy_using_offset(BYTE* dstPtr, const BYTE* srcPtr, BYTE* dstEnd, con } } #endif + + /*-************************************ * Common Constants **************************************/ @@ -1590,7 +1601,7 @@ LZ4_decompress_generic( if ((endOnInput) && unlikely(srcSize==0)) return -1; /* Currently the fast loop shows a regression on qualcomm arm chips. */ -#if defined(__i386__) || defined(__x86_64__) +#if LZ4_FAST_DEC_LOOP if ((oend - op) < FASTLOOP_SAFE_DISTANCE) goto safe_decode; @@ -1773,7 +1784,7 @@ LZ4_decompress_generic( /* copy literals */ cpy = op+length; -#if defined(__i386__) || defined(__x86_64__) +#if LZ4_FAST_DEC_LOOP safe_literal_copy: #endif LZ4_STATIC_ASSERT(MFLIMIT >= WILDCOPYLENGTH); @@ -1823,7 +1834,7 @@ LZ4_decompress_generic( } length += MINMATCH; -#if defined(__i386__) || defined(__x86_64__) +#if LZ4_FAST_DEC_LOOP safe_match_copy: #endif /* match starting within external dictionary */ -- cgit v1.2.3 From 753076bfa419fe8029b427193c2dd02d5adca08b Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 2 Apr 2019 17:16:43 -0700 Subject: fixed minor conversion warnings --- lib/lz4.c | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index dafd972..46f5565 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -341,6 +341,7 @@ void LZ4_wildCopy32(void* dstPtr, const void* srcPtr, void* dstEnd) do { memcpy(d,s,16); memcpy(d+16,s+16,16); d+=32; s+=32; } while (d= 32)); if (unlikely(offset<16)) { LZ4_memcpy_using_offset(op, match, cpy, offset); } else { @@ -1868,7 +1863,7 @@ LZ4_decompress_generic( /* copy match within block */ cpy = op + length; - /* partialDecoding : may not respect endBlock parsing restrictions */ + /* partialDecoding : may end anywhere within the block */ assert(op<=oend); if (partialDecoding && (cpy > oend-MATCH_SAFEGUARD_DISTANCE)) { size_t const mlen = MIN(length, (size_t)(oend-op)); @@ -2091,7 +2086,7 @@ int LZ4_decompress_safe_continue (LZ4_streamDecode_t* LZ4_streamDecode, const ch assert(lz4sd->extDictSize == 0); result = LZ4_decompress_safe(source, dest, compressedSize, maxOutputSize); if (result <= 0) return result; - lz4sd->prefixSize = result; + lz4sd->prefixSize = (size_t)result; lz4sd->prefixEnd = (BYTE*)dest + result; } else if (lz4sd->prefixEnd == (BYTE*)dest) { /* They're rolling the current segment. */ @@ -2104,7 +2099,7 @@ int LZ4_decompress_safe_continue (LZ4_streamDecode_t* LZ4_streamDecode, const ch result = LZ4_decompress_safe_doubleDict(source, dest, compressedSize, maxOutputSize, lz4sd->prefixSize, lz4sd->externalDict, lz4sd->extDictSize); if (result <= 0) return result; - lz4sd->prefixSize += result; + lz4sd->prefixSize += (size_t)result; lz4sd->prefixEnd += result; } else { /* The buffer wraps around, or they're switching to another buffer. */ @@ -2113,7 +2108,7 @@ int LZ4_decompress_safe_continue (LZ4_streamDecode_t* LZ4_streamDecode, const ch result = LZ4_decompress_safe_forceExtDict(source, dest, compressedSize, maxOutputSize, lz4sd->externalDict, lz4sd->extDictSize); if (result <= 0) return result; - lz4sd->prefixSize = result; + lz4sd->prefixSize = (size_t)result; lz4sd->prefixEnd = (BYTE*)dest + result; } @@ -2125,12 +2120,13 @@ int LZ4_decompress_fast_continue (LZ4_streamDecode_t* LZ4_streamDecode, const ch { LZ4_streamDecode_t_internal* lz4sd = &LZ4_streamDecode->internal_donotuse; int result; + assert(originalSize >= 0); if (lz4sd->prefixSize == 0) { assert(lz4sd->extDictSize == 0); result = LZ4_decompress_fast(source, dest, originalSize); if (result <= 0) return result; - lz4sd->prefixSize = originalSize; + lz4sd->prefixSize = (size_t)originalSize; lz4sd->prefixEnd = (BYTE*)dest + originalSize; } else if (lz4sd->prefixEnd == (BYTE*)dest) { if (lz4sd->prefixSize >= 64 KB - 1 || lz4sd->extDictSize == 0) @@ -2139,7 +2135,7 @@ int LZ4_decompress_fast_continue (LZ4_streamDecode_t* LZ4_streamDecode, const ch result = LZ4_decompress_fast_doubleDict(source, dest, originalSize, lz4sd->prefixSize, lz4sd->externalDict, lz4sd->extDictSize); if (result <= 0) return result; - lz4sd->prefixSize += originalSize; + lz4sd->prefixSize += (size_t)originalSize; lz4sd->prefixEnd += originalSize; } else { lz4sd->extDictSize = lz4sd->prefixSize; @@ -2147,7 +2143,7 @@ int LZ4_decompress_fast_continue (LZ4_streamDecode_t* LZ4_streamDecode, const ch result = LZ4_decompress_fast_extDict(source, dest, originalSize, lz4sd->externalDict, lz4sd->extDictSize); if (result <= 0) return result; - lz4sd->prefixSize = originalSize; + lz4sd->prefixSize = (size_t)originalSize; lz4sd->prefixEnd = (BYTE*)dest + originalSize; } -- cgit v1.2.3 From f2755c9887907272fa2d09445b50a07e8dea6b02 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 3 Apr 2019 08:59:29 -0700 Subject: minor comments and reformatting --- lib/lz4.c | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 46f5565..df92162 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -311,8 +311,10 @@ static const int dec64table[8] = {0, 0, 0, -1, -4, 1, 2, 3}; #endif #if LZ4_FAST_DEC_LOOP -LZ4_FORCE_O2_INLINE_GCC_PPC64LE -void LZ4_memcpy_using_offset_base(BYTE* dstPtr, const BYTE* srcPtr, BYTE* dstEnd, const size_t offset) { + +LZ4_FORCE_O2_INLINE_GCC_PPC64LE void +LZ4_memcpy_using_offset_base(BYTE* dstPtr, const BYTE* srcPtr, BYTE* dstEnd, const size_t offset) +{ if (offset < 8) { dstPtr[0] = srcPtr[0]; dstPtr[1] = srcPtr[1]; @@ -331,9 +333,11 @@ void LZ4_memcpy_using_offset_base(BYTE* dstPtr, const BYTE* srcPtr, BYTE* dstEnd LZ4_wildCopy(dstPtr, srcPtr, dstEnd); } -/* customized variant of memcpy, which can overwrite up to 32 bytes beyond dstEnd */ -LZ4_FORCE_O2_INLINE_GCC_PPC64LE -void LZ4_wildCopy32(void* dstPtr, const void* srcPtr, void* dstEnd) +/* customized variant of memcpy, which can overwrite up to 32 bytes beyond dstEnd + * this version copies two times 16 bytes (instead of one time 32 bytes) + * because it must be compatible with offsets >= 16. */ +LZ4_FORCE_O2_INLINE_GCC_PPC64LE void +LZ4_wildCopy32(void* dstPtr, const void* srcPtr, void* dstEnd) { BYTE* d = (BYTE*)dstPtr; const BYTE* s = (const BYTE*)srcPtr; @@ -342,8 +346,9 @@ void LZ4_wildCopy32(void* dstPtr, const void* srcPtr, void* dstEnd) do { memcpy(d,s,16); memcpy(d+16,s+16,16); d+=32; s+=32; } while (d= lencheck before start of loop. Returns initial_error if so. * error (output) - error code. Should be set to 0 before call. */ -typedef enum { loop_error = -2, initial_error = -1, ok = 0} variable_length_error; -LZ4_FORCE_INLINE unsigned read_variable_length(const BYTE**ip, const BYTE* lencheck, int loop_check, int initial_check, variable_length_error* error) { +typedef enum { loop_error = -2, initial_error = -1, ok = 0 } variable_length_error; +LZ4_FORCE_INLINE unsigned +read_variable_length(const BYTE**ip, const BYTE* lencheck, int loop_check, int initial_check, variable_length_error* error) +{ unsigned length = 0; unsigned s; if (initial_check && unlikely((*ip) >= lencheck)) { /* overflow detection */ @@ -1673,9 +1680,7 @@ LZ4_decompress_generic( memcpy(op+16, match+16, 2); op += length; continue; - } - } - } + } } } /* match starting within external dictionary */ if ((dict==usingExtDict) && (match < lowPrefix)) { -- cgit v1.2.3 From 25da6cc967419f5f322df2f34a37a1d7c4f597cf Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 3 Apr 2019 14:18:00 -0700 Subject: moved _destSize() into "stable API" status as requested in #642 --- doc/lz4_manual.html | 21 +++++++++----- lib/lz4hc.h | 82 ++++++++++++++++++++++++++--------------------------- 2 files changed, 55 insertions(+), 48 deletions(-) diff --git a/doc/lz4_manual.html b/doc/lz4_manual.html index 4c4734a..f98e8bb 100644 --- a/doc/lz4_manual.html +++ b/doc/lz4_manual.html @@ -15,7 +15,7 @@
  • Advanced Functions
  • Streaming Compression Functions
  • Streaming Decompression Functions
  • -
  • Unstable declarations
  • +
  • Symbols declared in this section must be considered unstable. Their
  • PRIVATE DEFINITIONS
  • Obsolete Functions
  • @@ -295,12 +295,19 @@ int LZ4_decompress_fast_usingDict (const char* src, char* dst, int originalSize,


    -

    Unstable declarations

    - Declarations in this section must be considered unstable.
    - Their signatures may change, or may be removed in the future.
    - They are therefore only safe to depend on
    - when the caller is statically linked against the library.
    - To access their declarations, define LZ4_STATIC_LINKING_ONLY.
    +

    Symbols declared in this section must be considered unstable. Their

     signatures or semantics may change, or they may be removed altogether in the
    + future. They are therefore only safe to depend on when the caller is
    + statically linked against the library.
    +
    + To protect against unsafe usage, not only are the declarations guarded, the
    + definitions are hidden by default when building LZ4 as a shared/dynamic
    + library.
    +
    + In order to access these declarations, define LZ4_STATIC_LINKING_ONLY in
    + your application before including LZ4's headers.
    +
    + In order to make their implementations accessible dynamically, you must
    + define LZ4_PUBLISH_STATIC_FUNCTIONS when building the LZ4 library.
     
    LZ4LIB_STATIC_API void LZ4_resetStream_fast (LZ4_stream_t* streamPtr);
    diff --git a/lib/lz4hc.h b/lib/lz4hc.h
    index d3fb594..3324f13 100644
    --- a/lib/lz4hc.h
    +++ b/lib/lz4hc.h
    @@ -54,7 +54,7 @@ extern "C" {
      *  Block Compression
      **************************************/
     /*! LZ4_compress_HC() :
    - *  Compress data from `src` into `dst`, using the more powerful but slower "HC" algorithm.
    + *  Compress data from `src` into `dst`, using the powerful but slower "HC" algorithm.
      * `dst` must be already allocated.
      *  Compression is guaranteed to succeed if `dstCapacity >= LZ4_compressBound(srcSize)` (see "lz4.h")
      *  Max supported `srcSize` value is LZ4_MAX_INPUT_SIZE (see "lz4.h")
    @@ -77,7 +77,21 @@ LZ4LIB_API int LZ4_compress_HC (const char* src, char* dst, int srcSize, int dst
      *  Memory segment must be aligned on 8-bytes boundaries (which a normal malloc() should do properly).
      */
     LZ4LIB_API int LZ4_sizeofStateHC(void);
    -LZ4LIB_API int LZ4_compress_HC_extStateHC(void* state, const char* src, char* dst, int srcSize, int maxDstSize, int compressionLevel);
    +LZ4LIB_API int LZ4_compress_HC_extStateHC(void* stateHC, const char* src, char* dst, int srcSize, int maxDstSize, int compressionLevel);
    +
    +
    +/*! LZ4_compress_HC_destSize() : v1.9.0+
    + *  Will compress as much data as possible from `src`
    + *  to fit into `targetDstSize` budget.
    + *  Result is provided in 2 parts :
    + * @return : the number of bytes written into 'dst'
    + *           or 0 if compression fails.
    + * `srcSizePtr` : value will be updated to indicate how much bytes were read from `src`
    + */
    +LZ4LIB_API int LZ4_compress_HC_destSize(void* stateHC,
    +                                  const char* src, char* dst,
    +                                        int* srcSizePtr, int targetDstSize,
    +                                        int compressionLevel);
     
     
     /*-************************************
    @@ -95,13 +109,6 @@ LZ4LIB_API int LZ4_compress_HC_extStateHC(void* state, const char* src, char* ds
     LZ4LIB_API LZ4_streamHC_t* LZ4_createStreamHC(void);
     LZ4LIB_API int             LZ4_freeStreamHC (LZ4_streamHC_t* streamHCPtr);
     
    -LZ4LIB_API void LZ4_resetStreamHC (LZ4_streamHC_t* streamHCPtr, int compressionLevel);
    -LZ4LIB_API int  LZ4_loadDictHC (LZ4_streamHC_t* streamHCPtr, const char* dictionary, int dictSize);
    -
    -LZ4LIB_API int LZ4_compress_HC_continue (LZ4_streamHC_t* streamHCPtr, const char* src, char* dst, int srcSize, int maxDstSize);
    -
    -LZ4LIB_API int LZ4_saveDictHC (LZ4_streamHC_t* streamHCPtr, char* safeBuffer, int maxDictSize);
    -
     /*
       These functions compress data in successive blocks of any size, using previous blocks as dictionary.
       One key assumption is that previous blocks (up to 64 KB) remain read-accessible while compressing next blocks.
    @@ -114,14 +121,35 @@ LZ4LIB_API int LZ4_saveDictHC (LZ4_streamHC_t* streamHCPtr, char* safeBuffer, in
       Then, use LZ4_compress_HC_continue() to compress each successive block.
       Previous memory blocks (including initial dictionary when present) must remain accessible and unmodified during compression.
       'dst' buffer should be sized to handle worst case scenarios (see LZ4_compressBound()), to ensure operation success.
    -  Because in case of failure, the API does not guarantee context recovery, and context will have to be reset.
    -  If `dst` buffer budget cannot be >= LZ4_compressBound(), consider using LZ4_compress_HC_continue_destSize() instead.
    +  In case of failure, the API does not guarantee context recovery, therefore context must be reset, using `LZ4_resetStreamHC()`.
    +  If `dst` buffer size cannot be made >= LZ4_compressBound(), consider using LZ4_compress_HC_continue_destSize() instead.
     
       If, for any reason, previous data block can't be preserved unmodified in memory for next compression block,
       you can save it to a more stable memory space, using LZ4_saveDictHC().
       Return value of LZ4_saveDictHC() is the size of dictionary effectively saved into 'safeBuffer'.
     */
     
    +LZ4LIB_API void LZ4_resetStreamHC (LZ4_streamHC_t* streamHCPtr, int compressionLevel);
    +LZ4LIB_API int  LZ4_loadDictHC (LZ4_streamHC_t* streamHCPtr, const char* dictionary, int dictSize);
    +
    +LZ4LIB_API int LZ4_compress_HC_continue (LZ4_streamHC_t* streamHCPtr,
    +                                   const char* src, char* dst,
    +                                         int srcSize, int maxDstSize);
    +
    +LZ4LIB_API int LZ4_saveDictHC (LZ4_streamHC_t* streamHCPtr, char* safeBuffer, int maxDictSize);
    +
    +/*! LZ4_compress_HC_continue_destSize() : v1.9.0+
    + *  Similar as LZ4_compress_HC_continue(),
    + *  but will read as much data as possible from `src`
    + *  to fit into `targetDstSize` budget.
    + *  Result is provided into 2 parts :
    + * @return : the number of bytes written into 'dst' (necessarily <= targetDstSize)
    + *           or 0 if compression fails.
    + * `srcSizePtr` : value will be updated to indicate how much bytes were read from `src`.
    + */
    +LZ4LIB_API int LZ4_compress_HC_continue_destSize(LZ4_streamHC_t* LZ4_streamHCPtr,
    +                                           const char* src, char* dst,
    +                                                 int* srcSizePtr, int targetDstSize);
     
     
     
    @@ -264,41 +292,13 @@ LZ4_DEPRECATED("use LZ4_resetStreamHC() instead") LZ4LIB_API  int   LZ4_resetStr
     extern "C" {
     #endif
     
    -/*! LZ4_compress_HC_destSize() : v1.8.0 (experimental)
    - *  Will try to compress as much data from `src` as possible
    - *  that can fit into `targetDstSize` budget.
    - *  Result is provided in 2 parts :
    - * @return : the number of bytes written into 'dst'
    - *           or 0 if compression fails.
    - * `srcSizePtr` : value will be updated to indicate how much bytes were read from `src`
    - */
    -LZ4LIB_STATIC_API int LZ4_compress_HC_destSize(
    -    void* LZ4HC_Data,
    -    const char* src, char* dst,
    -    int* srcSizePtr, int targetDstSize,
    -    int compressionLevel);
    -
    -/*! LZ4_compress_HC_continue_destSize() : v1.8.0 (experimental)
    - *  Similar as LZ4_compress_HC_continue(),
    - *  but will read a variable nb of bytes from `src`
    - *  to fit into `targetDstSize` budget.
    - *  Result is provided in 2 parts :
    - * @return : the number of bytes written into 'dst'
    - *           or 0 if compression fails.
    - * `srcSizePtr` : value will be updated to indicate how much bytes were read from `src`.
    - */
    -LZ4LIB_STATIC_API int LZ4_compress_HC_continue_destSize(
    -    LZ4_streamHC_t* LZ4_streamHCPtr,
    -    const char* src, char* dst,
    -    int* srcSizePtr, int targetDstSize);
    -
    -/*! LZ4_setCompressionLevel() : v1.8.0 (experimental)
    +/*! LZ4_setCompressionLevel() : v1.8.0+ (experimental)
      *  It's possible to change compression level between 2 invocations of LZ4_compress_HC_continue*()
      */
     LZ4LIB_STATIC_API void LZ4_setCompressionLevel(
         LZ4_streamHC_t* LZ4_streamHCPtr, int compressionLevel);
     
    -/*! LZ4_favorDecompressionSpeed() : v1.8.2 (experimental)
    +/*! LZ4_favorDecompressionSpeed() : v1.8.2+ (experimental)
      *  Parser will select decisions favoring decompression over compression ratio.
      *  Only work at highest compression settings (level >= LZ4HC_CLEVEL_OPT_MIN)
      */
    -- 
    cgit v1.2.3
    
    
    From 50f01d881d0bfe0accd8f7c70937be99c403b6ef Mon Sep 17 00:00:00 2001
    From: Yann Collet 
    Date: Wed, 3 Apr 2019 14:27:21 -0700
    Subject: fixed doc
    
    and bumped version number fo v1.9.0
    ---
     doc/lz4_manual.html | 21 ++++++++++++---------
     lib/lz4.h           | 17 ++++++++++-------
     2 files changed, 22 insertions(+), 16 deletions(-)
    
    diff --git a/doc/lz4_manual.html b/doc/lz4_manual.html
    index f98e8bb..f25aa79 100644
    --- a/doc/lz4_manual.html
    +++ b/doc/lz4_manual.html
    @@ -1,10 +1,10 @@
     
     
     
    -1.8.3 Manual
    +1.9.0 Manual
     
     
    -

    1.8.3 Manual

    +

    1.9.0 Manual


    Contents

      @@ -15,7 +15,7 @@
    1. Advanced Functions
    2. Streaming Compression Functions
    3. Streaming Decompression Functions
    4. -
    5. Symbols declared in this section must be considered unstable. Their
    6. +
    7. Experimental section
    8. PRIVATE DEFINITIONS
    9. Obsolete Functions
    @@ -295,16 +295,19 @@ int LZ4_decompress_fast_usingDict (const char* src, char* dst, int originalSize,


    -

    Symbols declared in this section must be considered unstable. Their

     signatures or semantics may change, or they may be removed altogether in the
    +

    Experimental section

    + Symbols declared in this section must be considered unstable. Their
    + signatures or semantics may change, or they may be removed altogether in the
      future. They are therefore only safe to depend on when the caller is
      statically linked against the library.
     
    - To protect against unsafe usage, not only are the declarations guarded, the
    - definitions are hidden by default when building LZ4 as a shared/dynamic
    - library.
    + To protect against unsafe usage, not only are the declarations guarded,
    + the definitions are hidden by default
    + when building LZ4 as a shared/dynamic library.
     
    - In order to access these declarations, define LZ4_STATIC_LINKING_ONLY in
    - your application before including LZ4's headers.
    + In order to access these declarations,
    + define LZ4_STATIC_LINKING_ONLY in your application
    + before including LZ4's headers.
     
      In order to make their implementations accessible dynamically, you must
      define LZ4_PUBLISH_STATIC_FUNCTIONS when building the LZ4 library.
    diff --git a/lib/lz4.h b/lib/lz4.h
    index c78f123..878aaf0 100644
    --- a/lib/lz4.h
    +++ b/lib/lz4.h
    @@ -92,8 +92,8 @@ extern "C" {
     
     /*------   Version   ------*/
     #define LZ4_VERSION_MAJOR    1    /* for breaking interface changes  */
    -#define LZ4_VERSION_MINOR    8    /* for new (non-breaking) interface capabilities */
    -#define LZ4_VERSION_RELEASE  3    /* for tweaks, bug-fixes, or development */
    +#define LZ4_VERSION_MINOR    9    /* for new (non-breaking) interface capabilities */
    +#define LZ4_VERSION_RELEASE  0    /* for tweaks, bug-fixes, or development */
     
     #define LZ4_VERSION_NUMBER (LZ4_VERSION_MAJOR *100*100 + LZ4_VERSION_MINOR *100 + LZ4_VERSION_RELEASE)
     
    @@ -390,17 +390,20 @@ LZ4LIB_API int LZ4_decompress_fast_usingDict (const char* src, char* dst, int or
      ***************************************/
     
     /*-****************************************************************************
    + * Experimental section
    + *
      * Symbols declared in this section must be considered unstable. Their
      * signatures or semantics may change, or they may be removed altogether in the
      * future. They are therefore only safe to depend on when the caller is
      * statically linked against the library.
      *
    - * To protect against unsafe usage, not only are the declarations guarded, the
    - * definitions are hidden by default when building LZ4 as a shared/dynamic
    - * library.
    + * To protect against unsafe usage, not only are the declarations guarded,
    + * the definitions are hidden by default
    + * when building LZ4 as a shared/dynamic library.
      *
    - * In order to access these declarations, define LZ4_STATIC_LINKING_ONLY in
    - * your application before including LZ4's headers.
    + * In order to access these declarations,
    + * define LZ4_STATIC_LINKING_ONLY in your application
    + * before including LZ4's headers.
      *
      * In order to make their implementations accessible dynamically, you must
      * define LZ4_PUBLISH_STATIC_FUNCTIONS when building the LZ4 library.
    -- 
    cgit v1.2.3
    
    
    From b5bedefabe513c810e77a64c448b00377e0a8bf0 Mon Sep 17 00:00:00 2001
    From: Yann Collet 
    Date: Wed, 3 Apr 2019 16:28:42 -0700
    Subject: fixed an old bug in LZ4F_flush()
    
    which remained undetected so far,
    as it requires a fairly large number of conditions to be triggered,
    starting with enabling Block checksum, which is disabled by default,
    and which usage is known to be extremely rare.
    ---
     lib/lz4frame.c | 114 +++++++++++++++++++++++++++++++++------------------------
     lib/lz4hc.h    |  61 ++++++++++++++++++++----------
     2 files changed, 109 insertions(+), 66 deletions(-)
    
    diff --git a/lib/lz4frame.c b/lib/lz4frame.c
    index 3f81ef0..2164470 100644
    --- a/lib/lz4frame.c
    +++ b/lib/lz4frame.c
    @@ -74,7 +74,6 @@ You can contact the author at :
     #endif
     
     
    -
     /*-************************************
     *  Includes
     **************************************/
    @@ -139,8 +138,8 @@ static U32 LZ4F_readLE32 (const void* src)
     {
         const BYTE* const srcPtr = (const BYTE*)src;
         U32 value32 = srcPtr[0];
    -    value32 += (srcPtr[1]<<8);
    -    value32 += (srcPtr[2]<<16);
    +    value32 += ((U32)srcPtr[1])<< 8;
    +    value32 += ((U32)srcPtr[2])<<16;
         value32 += ((U32)srcPtr[3])<<24;
         return value32;
     }
    @@ -204,7 +203,8 @@ static void LZ4F_writeLE64 (void* dst, U64 value64)
     
     static const size_t minFHSize = 7;
     static const size_t maxFHSize = LZ4F_HEADER_SIZE_MAX;   /* 19 */
    -static const size_t BHSize = 4;
    +static const size_t BHSize = 4;  /* block header : size, and compress flag */
    +static const size_t BFSize = 4;  /* block footer : checksum (optional) */
     
     
     /*-************************************
    @@ -329,11 +329,10 @@ static size_t LZ4F_compressBound_internal(size_t srcSize,
             size_t const lastBlockSize = flush ? partialBlockSize : 0;
             unsigned const nbBlocks = nbFullBlocks + (lastBlockSize>0);
     
    -        size_t const blockHeaderSize = 4;
    -        size_t const blockCRCSize = 4 * prefsPtr->frameInfo.blockChecksumFlag;
    -        size_t const frameEnd = 4 + (prefsPtr->frameInfo.contentChecksumFlag*4);
    +        size_t const blockCRCSize = BFSize * prefsPtr->frameInfo.blockChecksumFlag;
    +        size_t const frameEnd = BHSize + (prefsPtr->frameInfo.contentChecksumFlag*BFSize);
     
    -        return ((blockHeaderSize + blockCRCSize) * nbBlocks) +
    +        return ((BHSize + blockCRCSize) * nbBlocks) +
                    (blockSize * nbFullBlocks) + lastBlockSize + frameEnd;
         }
     }
    @@ -394,15 +393,18 @@ size_t LZ4F_compressFrame_usingCDict(LZ4F_cctx* cctx,
           if (LZ4F_isError(headerSize)) return headerSize;
           dstPtr += headerSize;   /* header size */ }
     
    -    { size_t const cSize = LZ4F_compressUpdate(cctx, dstPtr, dstEnd-dstPtr, srcBuffer, srcSize, &options);
    +    assert(dstEnd >= dstPtr);
    +    { size_t const cSize = LZ4F_compressUpdate(cctx, dstPtr, (size_t)(dstEnd-dstPtr), srcBuffer, srcSize, &options);
           if (LZ4F_isError(cSize)) return cSize;
           dstPtr += cSize; }
     
    -    { size_t const tailSize = LZ4F_compressEnd(cctx, dstPtr, dstEnd-dstPtr, &options);   /* flush last block, and generate suffix */
    +    assert(dstEnd >= dstPtr);
    +    { size_t const tailSize = LZ4F_compressEnd(cctx, dstPtr, (size_t)(dstEnd-dstPtr), &options);   /* flush last block, and generate suffix */
           if (LZ4F_isError(tailSize)) return tailSize;
           dstPtr += tailSize; }
     
    -    return (dstPtr - dstStart);
    +    assert(dstEnd >= dstStart);
    +    return (size_t)(dstPtr - dstStart);
     }
     
     
    @@ -662,7 +664,7 @@ size_t LZ4F_compressBegin_usingCDict(LZ4F_cctx* cctxPtr,
         *dstPtr++ = (BYTE)(((1 & _2BITS) << 6)    /* Version('01') */
             + ((cctxPtr->prefs.frameInfo.blockMode & _1BIT ) << 5)
             + ((cctxPtr->prefs.frameInfo.blockChecksumFlag & _1BIT ) << 4)
    -        + ((cctxPtr->prefs.frameInfo.contentSize > 0) << 3)
    +        + ((unsigned)(cctxPtr->prefs.frameInfo.contentSize > 0) << 3)
             + ((cctxPtr->prefs.frameInfo.contentChecksumFlag & _1BIT ) << 2)
             +  (cctxPtr->prefs.frameInfo.dictID > 0) );
         /* BD Byte */
    @@ -679,11 +681,11 @@ size_t LZ4F_compressBegin_usingCDict(LZ4F_cctx* cctxPtr,
             dstPtr += 4;
         }
         /* Header CRC Byte */
    -    *dstPtr = LZ4F_headerChecksum(headerStart, dstPtr - headerStart);
    +    *dstPtr = LZ4F_headerChecksum(headerStart, (size_t)(dstPtr - headerStart));
         dstPtr++;
     
         cctxPtr->cStage = 1;   /* header written, now request input data block */
    -    return (dstPtr - dstStart);
    +    return (size_t)(dstPtr - dstStart);
     }
     
     
    @@ -718,27 +720,31 @@ typedef int (*compressFunc_t)(void* ctx, const char* src, char* dst, int srcSize
     
     
     /*! LZ4F_makeBlock():
    - *  compress a single block, add header and checksum
    - *  assumption : dst buffer capacity is >= srcSize */
    -static size_t LZ4F_makeBlock(void* dst, const void* src, size_t srcSize,
    + *  compress a single block, add header and optional checksum.
    + *  assumption : dst buffer capacity is >= BHSize + srcSize + crcSize
    + */
    +static size_t LZ4F_makeBlock(void* dst,
    +                       const void* src, size_t srcSize,
                                  compressFunc_t compress, void* lz4ctx, int level,
    -                             const LZ4F_CDict* cdict, LZ4F_blockChecksum_t crcFlag)
    +                       const LZ4F_CDict* cdict,
    +                             LZ4F_blockChecksum_t crcFlag)
     {
         BYTE* const cSizePtr = (BYTE*)dst;
    -    U32 cSize = (U32)compress(lz4ctx, (const char*)src, (char*)(cSizePtr+4),
    +    U32 cSize = (U32)compress(lz4ctx, (const char*)src, (char*)(cSizePtr+BHSize),
                                           (int)(srcSize), (int)(srcSize-1),
                                           level, cdict);
    -    LZ4F_writeLE32(cSizePtr, cSize);
         if (cSize == 0) {  /* compression failed */
             cSize = (U32)srcSize;
             LZ4F_writeLE32(cSizePtr, cSize | LZ4F_BLOCKUNCOMPRESSED_FLAG);
    -        memcpy(cSizePtr+4, src, srcSize);
    +        memcpy(cSizePtr+BHSize, src, srcSize);
    +    } else {
    +        LZ4F_writeLE32(cSizePtr, cSize);
         }
         if (crcFlag) {
    -        U32 const crc32 = XXH32(cSizePtr+4, cSize, 0);  /* checksum of compressed data */
    -        LZ4F_writeLE32(cSizePtr+4+cSize, crc32);
    +        U32 const crc32 = XXH32(cSizePtr+BHSize, cSize, 0);  /* checksum of compressed data */
    +        LZ4F_writeLE32(cSizePtr+BHSize+cSize, crc32);
         }
    -    return 4 + cSize + ((U32)crcFlag)*4;
    +    return BHSize + cSize + ((U32)crcFlag)*BFSize;
     }
     
     
    @@ -838,9 +844,11 @@ size_t LZ4F_compressUpdate(LZ4F_cctx* cctxPtr,
                 memcpy(cctxPtr->tmpIn + cctxPtr->tmpInSize, srcBuffer, sizeToCopy);
                 srcPtr += sizeToCopy;
     
    -            dstPtr += LZ4F_makeBlock(dstPtr, cctxPtr->tmpIn, blockSize,
    +            dstPtr += LZ4F_makeBlock(dstPtr,
    +                                     cctxPtr->tmpIn, blockSize,
                                          compress, cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel,
    -                                     cctxPtr->cdict, cctxPtr->prefs.frameInfo.blockChecksumFlag);
    +                                     cctxPtr->cdict,
    +                                     cctxPtr->prefs.frameInfo.blockChecksumFlag);
     
                 if (cctxPtr->prefs.frameInfo.blockMode==LZ4F_blockLinked) cctxPtr->tmpIn += blockSize;
                 cctxPtr->tmpInSize = 0;
    @@ -850,18 +858,22 @@ size_t LZ4F_compressUpdate(LZ4F_cctx* cctxPtr,
         while ((size_t)(srcEnd - srcPtr) >= blockSize) {
             /* compress full blocks */
             lastBlockCompressed = fromSrcBuffer;
    -        dstPtr += LZ4F_makeBlock(dstPtr, srcPtr, blockSize,
    +        dstPtr += LZ4F_makeBlock(dstPtr,
    +                                 srcPtr, blockSize,
                                      compress, cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel,
    -                                 cctxPtr->cdict, cctxPtr->prefs.frameInfo.blockChecksumFlag);
    +                                 cctxPtr->cdict,
    +                                 cctxPtr->prefs.frameInfo.blockChecksumFlag);
             srcPtr += blockSize;
         }
     
         if ((cctxPtr->prefs.autoFlush) && (srcPtr < srcEnd)) {
             /* compress remaining input < blockSize */
             lastBlockCompressed = fromSrcBuffer;
    -        dstPtr += LZ4F_makeBlock(dstPtr, srcPtr, srcEnd - srcPtr,
    +        dstPtr += LZ4F_makeBlock(dstPtr,
    +                                 srcPtr, (size_t)(srcEnd - srcPtr),
                                      compress, cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel,
    -                                 cctxPtr->cdict, cctxPtr->prefs.frameInfo.blockChecksumFlag);
    +                                 cctxPtr->cdict,
    +                                 cctxPtr->prefs.frameInfo.blockChecksumFlag);
             srcPtr  = srcEnd;
         }
     
    @@ -887,7 +899,7 @@ size_t LZ4F_compressUpdate(LZ4F_cctx* cctxPtr,
         /* some input data left, necessarily < blockSize */
         if (srcPtr < srcEnd) {
             /* fill tmp buffer */
    -        size_t const sizeToCopy = srcEnd - srcPtr;
    +        size_t const sizeToCopy = (size_t)(srcEnd - srcPtr);
             memcpy(cctxPtr->tmpIn, srcPtr, sizeToCopy);
             cctxPtr->tmpInSize = sizeToCopy;
         }
    @@ -896,19 +908,21 @@ size_t LZ4F_compressUpdate(LZ4F_cctx* cctxPtr,
             (void)XXH32_update(&(cctxPtr->xxh), srcBuffer, srcSize);
     
         cctxPtr->totalInSize += srcSize;
    -    return dstPtr - dstStart;
    +    return (size_t)(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)
    + *  When compressed data must be sent immediately, without waiting for a block to be filled,
    + *  invoke LZ4_flush(), which will immediately compress any remaining data stored within LZ4F_cctx.
    + *  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 LZ4F_cctx.
      *  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.
    + *  LZ4F_compressOptions_t* is optional. NULL is a valid argument.
      */
    -size_t LZ4F_flush(LZ4F_cctx* cctxPtr, void* dstBuffer, size_t dstCapacity, const LZ4F_compressOptions_t* compressOptionsPtr)
    +size_t LZ4F_flush(LZ4F_cctx* cctxPtr,
    +                  void* dstBuffer, size_t dstCapacity,
    +            const LZ4F_compressOptions_t* compressOptionsPtr)
     {
         BYTE* const dstStart = (BYTE*)dstBuffer;
         BYTE* dstPtr = dstStart;
    @@ -916,26 +930,32 @@ size_t LZ4F_flush(LZ4F_cctx* cctxPtr, void* dstBuffer, size_t dstCapacity, const
     
         if (cctxPtr->tmpInSize == 0) return 0;   /* nothing to flush */
         if (cctxPtr->cStage != 1) return err0r(LZ4F_ERROR_GENERIC);
    -    if (dstCapacity < (cctxPtr->tmpInSize + 4)) return err0r(LZ4F_ERROR_dstMaxSize_tooSmall);   /* +4 : block header(4)  */
    +    if (dstCapacity < (cctxPtr->tmpInSize + BHSize + BFSize))
    +        return err0r(LZ4F_ERROR_dstMaxSize_tooSmall);
         (void)compressOptionsPtr;   /* not yet useful */
     
         /* select compression function */
         compress = LZ4F_selectCompression(cctxPtr->prefs.frameInfo.blockMode, cctxPtr->prefs.compressionLevel);
     
         /* compress tmp buffer */
    -    dstPtr += LZ4F_makeBlock(dstPtr, cctxPtr->tmpIn, cctxPtr->tmpInSize,
    +    dstPtr += LZ4F_makeBlock(dstPtr,
    +                             cctxPtr->tmpIn, cctxPtr->tmpInSize,
                                  compress, cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel,
    -                             cctxPtr->cdict, cctxPtr->prefs.frameInfo.blockChecksumFlag);
    -    if (cctxPtr->prefs.frameInfo.blockMode==LZ4F_blockLinked) cctxPtr->tmpIn += cctxPtr->tmpInSize;
    +                             cctxPtr->cdict,
    +                             cctxPtr->prefs.frameInfo.blockChecksumFlag);
    +    assert(((void)"flush overflows dstBuffer!", (size_t)(dstPtr - dstStart) < dstCapacity));
    +
    +    if (cctxPtr->prefs.frameInfo.blockMode == LZ4F_blockLinked)
    +        cctxPtr->tmpIn += cctxPtr->tmpInSize;
         cctxPtr->tmpInSize = 0;
     
         /* keep tmpIn within limits */
         if ((cctxPtr->tmpIn + cctxPtr->maxBlockSize) > (cctxPtr->tmpBuff + cctxPtr->maxBufferSize)) {  /* necessarily LZ4F_blockLinked */
    -        int realDictSize = LZ4F_localSaveDict(cctxPtr);
    +        int const realDictSize = LZ4F_localSaveDict(cctxPtr);
             cctxPtr->tmpIn = cctxPtr->tmpBuff + realDictSize;
         }
     
    -    return dstPtr - dstStart;
    +    return (size_t)(dstPtr - dstStart);
     }
     
     
    @@ -981,7 +1001,7 @@ size_t LZ4F_compressEnd(LZ4F_cctx* cctxPtr,
                 return err0r(LZ4F_ERROR_frameSize_wrong);
         }
     
    -    return dstPtr - dstStart;
    +    return (size_t)(dstPtr - dstStart);
     }
     
     
    @@ -1433,7 +1453,7 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx,
     
             /* decode block header */
                 {   size_t const nextCBlockSize = LZ4F_readLE32(selectedIn) & 0x7FFFFFFFU;
    -                size_t const crcSize = dctx->frameInfo.blockChecksumFlag * 4;
    +                size_t const crcSize = dctx->frameInfo.blockChecksumFlag * BFSize;
                     if (nextCBlockSize==0) {  /* frameEnd signal, no more block */
                         dctx->dStage = dstage_getSuffix;
                         break;
    @@ -1453,7 +1473,7 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx,
                     dctx->tmpInTarget = nextCBlockSize + crcSize;
                     dctx->dStage = dstage_getCBlock;
                     if (dstPtr==dstEnd) {
    -                    nextSrcSizeHint = nextCBlockSize + crcSize + BHSize;
    +                    nextSrcSizeHint = BHSize + nextCBlockSize + crcSize;
                         doAnotherStage = 0;
                     }
                     break;
    diff --git a/lib/lz4hc.h b/lib/lz4hc.h
    index 3324f13..5e7cb11 100644
    --- a/lib/lz4hc.h
    +++ b/lib/lz4hc.h
    @@ -110,23 +110,43 @@ LZ4LIB_API LZ4_streamHC_t* LZ4_createStreamHC(void);
     LZ4LIB_API int             LZ4_freeStreamHC (LZ4_streamHC_t* streamHCPtr);
     
     /*
    -  These functions compress data in successive blocks of any size, using previous blocks as dictionary.
    +  These functions compress data in successive blocks of any size,
    +  using previous blocks as dictionary, to improve compression ratio.
       One key assumption is that previous blocks (up to 64 KB) remain read-accessible while compressing next blocks.
       There is an exception for ring buffers, which can be smaller than 64 KB.
    -  Ring buffers scenario is automatically detected and handled by LZ4_compress_HC_continue().
    +  Ring-buffer scenario is automatically detected and handled within LZ4_compress_HC_continue().
     
    -  Before starting compression, state must be properly initialized, using LZ4_resetStreamHC().
    -  A first "fictional block" can then be designated as initial dictionary, using LZ4_loadDictHC() (Optional).
    +  Before starting compression, state must be allocated and properly initialized.
    +  LZ4_createStreamHC() does both, though compression level is set to LZ4HC_CLEVEL_DEFAULT.
     
    -  Then, use LZ4_compress_HC_continue() to compress each successive block.
    -  Previous memory blocks (including initial dictionary when present) must remain accessible and unmodified during compression.
    -  'dst' buffer should be sized to handle worst case scenarios (see LZ4_compressBound()), to ensure operation success.
    -  In case of failure, the API does not guarantee context recovery, therefore context must be reset, using `LZ4_resetStreamHC()`.
    -  If `dst` buffer size cannot be made >= LZ4_compressBound(), consider using LZ4_compress_HC_continue_destSize() instead.
    +  Selecting the compression level can be done with LZ4_resetStreamHC_fast() (starts a new stream)
    +  or LZ4_setCompressionLevel() (anytime, between blocks in the same stream) (experimental).
    +  LZ4_resetStreamHC_fast() only works on states which have been properly initialized at least once.
     
    -  If, for any reason, previous data block can't be preserved unmodified in memory for next compression block,
    -  you can save it to a more stable memory space, using LZ4_saveDictHC().
    -  Return value of LZ4_saveDictHC() is the size of dictionary effectively saved into 'safeBuffer'.
    +  If state space is provided manually with no guarantee of its content, for example allocated on stack,
    +  it must be fully initialized, using LZ4_resetStreamHC().
    +  LZ4_resetStreamHC() is heavier, and it's guaranteed to succeed on any valid memory segment.
    +  In contrast, LZ4_resetStreamHC_fast() only works on states which have been properly initialized at least once.
    +
    +  After reset, a first "fictional block" can be designated as initial dictionary,
    +  using LZ4_loadDictHC() (Optional).
    +
    +  Invoke LZ4_compress_HC_continue() to compress each successive block.
    +  The number of blocks is unlimited.
    +  Previous input blocks (including initial dictionary when present) must remain accessible and unmodified during compression.
    +
    +  'dst' buffer should be sized to handle worst case scenarios (see LZ4_compressBound()), ensuring compression success.
    +  In case of failure, the API does not guarantee recovery, so the state _must_ be reset.
    +  Whenever `dst` buffer size cannot be made >= LZ4_compressBound(),
    +  consider using LZ4_compress_HC_continue_destSize() to ensure success.
    +
    +  Whenever previous input blocks can't be preserved unmodified in-place during compression of next blocks,
    +  it's possible to copy the last blocks into a more stable memory space, using LZ4_saveDictHC().
    +  Return value of LZ4_saveDictHC() is the size of dictionary effectively saved into 'safeBuffer' (<= 64 KB)
    +
    +  After completing a streaming compression,
    +  it's possible to start a new stream and re-use the LZ4_streamHC_t state 
    +  by resetting it, using LZ4_resetStreamHC_fast().
     */
     
     LZ4LIB_API void LZ4_resetStreamHC (LZ4_streamHC_t* streamHCPtr, int compressionLevel);
    @@ -136,21 +156,22 @@ LZ4LIB_API int LZ4_compress_HC_continue (LZ4_streamHC_t* streamHCPtr,
                                        const char* src, char* dst,
                                              int srcSize, int maxDstSize);
     
    -LZ4LIB_API int LZ4_saveDictHC (LZ4_streamHC_t* streamHCPtr, char* safeBuffer, int maxDictSize);
    -
     /*! LZ4_compress_HC_continue_destSize() : v1.9.0+
    - *  Similar as LZ4_compress_HC_continue(),
    + *  Similar to LZ4_compress_HC_continue(),
      *  but will read as much data as possible from `src`
      *  to fit into `targetDstSize` budget.
      *  Result is provided into 2 parts :
      * @return : the number of bytes written into 'dst' (necessarily <= targetDstSize)
      *           or 0 if compression fails.
    - * `srcSizePtr` : value will be updated to indicate how much bytes were read from `src`.
    + * `srcSizePtr` : on success, *srcSizePtr will be updated to indicate how much bytes were read from `src`.
    + *           Note that this function may not consume the entire input.
      */
     LZ4LIB_API int LZ4_compress_HC_continue_destSize(LZ4_streamHC_t* LZ4_streamHCPtr,
                                                const char* src, char* dst,
                                                      int* srcSizePtr, int targetDstSize);
     
    +LZ4LIB_API int LZ4_saveDictHC (LZ4_streamHC_t* streamHCPtr, char* safeBuffer, int maxDictSize);
    +
     
     
     /*^**********************************************
    @@ -293,14 +314,16 @@ extern "C" {
     #endif
     
     /*! LZ4_setCompressionLevel() : v1.8.0+ (experimental)
    - *  It's possible to change compression level between 2 invocations of LZ4_compress_HC_continue*()
    + *  It's possible to change compression level
    + *  between successive invocations of LZ4_compress_HC_continue*()
    + *  for dynamic adaptation.
      */
     LZ4LIB_STATIC_API void LZ4_setCompressionLevel(
         LZ4_streamHC_t* LZ4_streamHCPtr, int compressionLevel);
     
     /*! LZ4_favorDecompressionSpeed() : v1.8.2+ (experimental)
    - *  Parser will select decisions favoring decompression over compression ratio.
    - *  Only work at highest compression settings (level >= LZ4HC_CLEVEL_OPT_MIN)
    + *  Opt. Parser will favor decompression speed over compression ratio.
    + *  Only applicable to levels >= LZ4HC_CLEVEL_OPT_MIN.
      */
     LZ4LIB_STATIC_API void LZ4_favorDecompressionSpeed(
         LZ4_streamHC_t* LZ4_streamHCPtr, int favor);
    -- 
    cgit v1.2.3
    
    
    From ab913005093cb77d2efc430ba50636c2b8dea8d0 Mon Sep 17 00:00:00 2001
    From: Yann Collet 
    Date: Thu, 4 Apr 2019 12:24:46 -0700
    Subject: moved LZ4_decompress_fast*() into deprecated section
    
    ---
     lib/lz4.h   | 52 +++++++++++++++++++++++++++++-----------------------
     lib/lz4hc.h |  2 +-
     2 files changed, 30 insertions(+), 24 deletions(-)
    
    diff --git a/lib/lz4.h b/lib/lz4.h
    index 878aaf0..c848f5f 100644
    --- a/lib/lz4.h
    +++ b/lib/lz4.h
    @@ -203,27 +203,6 @@ LZ4LIB_API int LZ4_compress_fast_extState (void* state, const char* src, char* d
     LZ4LIB_API int LZ4_compress_destSize (const char* src, char* dst, int* srcSizePtr, int targetDstSize);
     
     
    -/*! LZ4_decompress_fast() : **unsafe!**
    - *  This function used to be a bit faster than LZ4_decompress_safe(),
    - *  though situation has changed in recent versions,
    - *  and now `LZ4_decompress_safe()` can be as fast and sometimes faster than `LZ4_decompress_fast()`.
    - *  Moreover, LZ4_decompress_fast() is not protected vs malformed input, as it doesn't perform full validation of compressed data.
    - *  As a consequence, this function is no longer recommended, and may be deprecated in future versions.
    - *  It's last remaining specificity is that it can decompress data without knowing its compressed size.
    - *
    - *  originalSize : is the uncompressed size to regenerate.
    - *                 `dst` must be already allocated, its size must be >= 'originalSize' bytes.
    - * @return : number of bytes read from source buffer (== compressed size).
    - *           If the source stream is detected malformed, the function stops decoding and returns a negative result.
    - *  note : This function requires uncompressed originalSize to be known in advance.
    - *         The function never writes past the output buffer.
    - *         However, since it doesn't know its 'src' size, it may read past the intended input.
    - *         Also, because match offsets are not validated during decoding,
    - *         reads from 'src' may underflow.
    - *         Use this function in trusted environment **only**.
    - */
    -LZ4LIB_API int LZ4_decompress_fast (const char* src, char* dst, int originalSize);
    -
     /*! LZ4_decompress_safe_partial() :
      *  Decompress an LZ4 compressed block, of size 'srcSize' at position 'src',
      *  into destination buffer 'dst' of size 'dstCapacity'.
    @@ -370,7 +349,6 @@ LZ4LIB_API int LZ4_decoderRingBufferSize(int maxBlockSize);
      *  then indicate where this data is saved using LZ4_setStreamDecode(), before decompressing next block.
     */
     LZ4LIB_API int LZ4_decompress_safe_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* src, char* dst, int srcSize, int dstCapacity);
    -LZ4LIB_API int LZ4_decompress_fast_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* src, char* dst, int originalSize);
     
     
     /*! LZ4_decompress_*_usingDict() :
    @@ -382,7 +360,6 @@ LZ4LIB_API int LZ4_decompress_fast_continue (LZ4_streamDecode_t* LZ4_streamDecod
      *                    when dst == dictStart + dictSize.
      */
     LZ4LIB_API int LZ4_decompress_safe_usingDict (const char* src, char* dst, int srcSize, int dstCapcity, const char* dictStart, int dictSize);
    -LZ4LIB_API int LZ4_decompress_fast_usingDict (const char* src, char* dst, int originalSize, const char* dictStart, int dictSize);
     
     
     /*^*************************************
    @@ -639,6 +616,35 @@ LZ4_DEPRECATED("Use LZ4_saveDict() instead")     LZ4LIB_API char* LZ4_slideInput
     LZ4_DEPRECATED("use LZ4_decompress_safe_usingDict() instead") LZ4LIB_API int LZ4_decompress_safe_withPrefix64k (const char* src, char* dst, int compressedSize, int maxDstSize);
     LZ4_DEPRECATED("use LZ4_decompress_fast_usingDict() instead") LZ4LIB_API int LZ4_decompress_fast_withPrefix64k (const char* src, char* dst, int originalSize);
     
    +/*! LZ4_decompress_fast() : **unsafe!**
    + *  These functions used to be a bit faster than LZ4_decompress_safe(),
    + *  but situation has changed in recent versions.
    + *  Now, `LZ4_decompress_safe()` is as fast and sometimes even faster than `LZ4_decompress_fast()`.
    + *  Moreover, LZ4_decompress_safe() is protected vs malformed input, while `LZ4_decompress_fast()` is not, making it a security liability.
    + *  As a consequence, LZ4_decompress_fast() is strongly discouraged, and deprecated.
    + *
    + *  Last LZ4_decompress_fast() specificity is that it can decompress a block without knowing its compressed size.
    + *  Note that even that functionality could be achieved in a more secure manner if need be,
    + *  though it would require new prototypes, and adaptation of the implementation to this new use case.
    + *
    + *  Parameters:
    + *  originalSize : is the uncompressed size to regenerate.
    + *                 `dst` must be already allocated, its size must be >= 'originalSize' bytes.
    + * @return : number of bytes read from source buffer (== compressed size).
    + *           The function expects to finish at block's end exactly.
    + *           If the source stream is detected malformed, the function stops decoding and returns a negative result.
    + *  note : LZ4_decompress_fast*() requires originalSize. Thanks to this information, it never writes past the output buffer.
    + *         However, since it doesn't know its 'src' size, it may read an unknown amount of input, and overflow input buffer.
    + *         Also, since match offsets are not validated, match reads from 'src' may underflow.
    + *         These issues never happen if input data is correct.
    + *         But they may happen if input data is invalid (error or intentional tampering).
    + *         As a consequence, use these functions in trusted environments with trusted data **only**.
    + */
    +LZ4LIB_API int LZ4_decompress_fast (const char* src, char* dst, int originalSize);
    +LZ4LIB_API int LZ4_decompress_fast_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* src, char* dst, int originalSize);
    +LZ4LIB_API int LZ4_decompress_fast_usingDict (const char* src, char* dst, int originalSize, const char* dictStart, int dictSize);
    +
    +
     #endif /* LZ4_H_2983827168210 */
     
     
    diff --git a/lib/lz4hc.h b/lib/lz4hc.h
    index 5e7cb11..d1772f3 100644
    --- a/lib/lz4hc.h
    +++ b/lib/lz4hc.h
    @@ -145,7 +145,7 @@ LZ4LIB_API int             LZ4_freeStreamHC (LZ4_streamHC_t* streamHCPtr);
       Return value of LZ4_saveDictHC() is the size of dictionary effectively saved into 'safeBuffer' (<= 64 KB)
     
       After completing a streaming compression,
    -  it's possible to start a new stream and re-use the LZ4_streamHC_t state 
    +  it's possible to start a new stream of blocks, and re-use the same LZ4_streamHC_t state 
       by resetting it, using LZ4_resetStreamHC_fast().
     */
     
    -- 
    cgit v1.2.3
    
    
    From 7a39fb8fb69a47486b91810708bbe796331b26a2 Mon Sep 17 00:00:00 2001
    From: Yann Collet 
    Date: Thu, 4 Apr 2019 12:47:36 -0700
    Subject: make `_fast*()` decoder generate a deprecation warning
    
    updated modification
    ---
     doc/lz4_manual.html           | 65 ++++++++++++++++++++++++++-----------------
     doc/lz4frame_manual.html      |  4 +--
     examples/compress_functions.c |  1 +
     lib/README.md                 | 14 ++++++++--
     lib/lz4.h                     | 20 +++++++++----
     tests/fullbench.c             |  1 +
     tests/fuzzer.c                |  4 +--
     7 files changed, 70 insertions(+), 39 deletions(-)
    
    diff --git a/doc/lz4_manual.html b/doc/lz4_manual.html
    index f25aa79..b738c8d 100644
    --- a/doc/lz4_manual.html
    +++ b/doc/lz4_manual.html
    @@ -127,27 +127,6 @@ int LZ4_compress_fast_extState (void* state, const char* src, char* dst, int src
                or 0 if compression fails.
     


    -
    int LZ4_decompress_fast (const char* src, char* dst, int originalSize);
    -

    This function used to be a bit faster than LZ4_decompress_safe(), - though situation has changed in recent versions, - and now `LZ4_decompress_safe()` can be as fast and sometimes faster than `LZ4_decompress_fast()`. - Moreover, LZ4_decompress_fast() is not protected vs malformed input, as it doesn't perform full validation of compressed data. - As a consequence, this function is no longer recommended, and may be deprecated in future versions. - It's last remaining specificity is that it can decompress data without knowing its compressed size. - - originalSize : is the uncompressed size to regenerate. - `dst` must be already allocated, its size must be >= 'originalSize' bytes. - @return : number of bytes read from source buffer (== compressed size). - If the source stream is detected malformed, the function stops decoding and returns a negative result. - note : This function requires uncompressed originalSize to be known in advance. - The function never writes past the output buffer. - However, since it doesn't know its 'src' size, it may read past the intended input. - Also, because match offsets are not validated during decoding, - reads from 'src' may underflow. - Use this function in trusted environment **only**. - -


    -
    int LZ4_decompress_safe_partial (const char* src, char* dst, int srcSize, int targetOutputSize, int dstCapacity);
     

    Decompress an LZ4 compressed block, of size 'srcSize' at position 'src', into destination buffer 'dst' of size 'dstCapacity'. @@ -258,7 +237,6 @@ int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream);


    int LZ4_decompress_safe_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* src, char* dst, int srcSize, int dstCapacity);
    -int LZ4_decompress_fast_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* src, char* dst, int originalSize);
     

    These decoding functions allow decompression of consecutive blocks in "streaming" mode. A block is an unsplittable entity, it must be presented entirely to a decompression function. Decompression functions only accepts one block at a time. @@ -285,7 +263,6 @@ int LZ4_decompress_fast_continue (LZ4_streamDecode_t* LZ4_streamDecode, const ch


    int LZ4_decompress_safe_usingDict (const char* src, char* dst, int srcSize, int dstCapcity, const char* dictStart, int dictSize);
    -int LZ4_decompress_fast_usingDict (const char* src, char* dst, int originalSize, const char* dictStart, int dictSize);
     

    These decoding functions work the same as a combination of LZ4_setStreamDecode() followed by LZ4_decompress_*_continue() They are stand-alone, and don't need an LZ4_streamDecode_t structure. @@ -457,11 +434,47 @@ union LZ4_streamDecode_u { # define LZ4_DEPRECATED(message) # endif #endif /* LZ4_DISABLE_DEPRECATE_WARNINGS */ -

    Should deprecation warnings be a problem, - it is generally possible to disable them, +

    + Deprecated functions make the compiler generate a warning when invoked. + This is meant to invite users to update their source code. + Should deprecation warnings be a problem, it is generally possible to disable them, typically with -Wno-deprecated-declarations for gcc or _CRT_SECURE_NO_WARNINGS in Visual. - Otherwise, it's also possible to define LZ4_DISABLE_DEPRECATE_WARNINGS + + Another method is to define LZ4_DISABLE_DEPRECATE_WARNINGS + before including the header file. + +


    + +
    LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe() instead") LZ4LIB_API
    +int LZ4_decompress_fast (const char* src, char* dst, int originalSize);
    +LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe_continue() instead") LZ4LIB_API
    +int LZ4_decompress_fast_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* src, char* dst, int originalSize);
    +LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe_usingDict() instead") LZ4LIB_API
    +int LZ4_decompress_fast_usingDict (const char* src, char* dst, int originalSize, const char* dictStart, int dictSize);
    +

    These functions used to be a bit faster than LZ4_decompress_safe(), + but situation has changed in recent versions. + Now, `LZ4_decompress_safe()` is as fast and sometimes even faster than `LZ4_decompress_fast()`. + Moreover, LZ4_decompress_safe() is protected vs malformed input, while `LZ4_decompress_fast()` is not, making it a security liability. + As a consequence, LZ4_decompress_fast() is strongly discouraged, and deprecated. + + Last LZ4_decompress_fast() specificity is that it can decompress a block without knowing its compressed size. + Note that even that functionality could be achieved in a more secure manner if need be, + though it would require new prototypes, and adaptation of the implementation to this new use case. + + Parameters: + originalSize : is the uncompressed size to regenerate. + `dst` must be already allocated, its size must be >= 'originalSize' bytes. + @return : number of bytes read from source buffer (== compressed size). + The function expects to finish at block's end exactly. + If the source stream is detected malformed, the function stops decoding and returns a negative result. + note : LZ4_decompress_fast*() requires originalSize. Thanks to this information, it never writes past the output buffer. + However, since it doesn't know its 'src' size, it may read an unknown amount of input, and overflow input buffer. + Also, since match offsets are not validated, match reads from 'src' may underflow. + These issues never happen if input data is correct. + But they may happen if input data is invalid (error or intentional tampering). + As a consequence, use these functions in trusted environments with trusted data **only**. +


    diff --git a/doc/lz4frame_manual.html b/doc/lz4frame_manual.html index 2b16045..4277c3c 100644 --- a/doc/lz4frame_manual.html +++ b/doc/lz4frame_manual.html @@ -1,10 +1,10 @@ -1.8.3 Manual +1.9.0 Manual -

    1.8.3 Manual

    +

    1.9.0 Manual


    Contents

      diff --git a/examples/compress_functions.c b/examples/compress_functions.c index 41d3c8c..7fd6775 100644 --- a/examples/compress_functions.c +++ b/examples/compress_functions.c @@ -60,6 +60,7 @@ #define _POSIX_C_SOURCE 199309L /* Includes, for Power! */ +#define LZ4_DISABLE_DEPRECATE_WARNINGS /* LZ4_decompress_fast */ #include "lz4.h" #include /* for printf() */ #include /* for exit() */ diff --git a/lib/README.md b/lib/README.md index e9b221f..be8eba0 100644 --- a/lib/README.md +++ b/lib/README.md @@ -7,8 +7,8 @@ not all of them are necessary. #### Minimal LZ4 build The minimum required is **`lz4.c`** and **`lz4.h`**, -which provides the fast compression and decompression algorithm. -They generate and decode data using [LZ4 block format]. +which provides the fast compression and decompression algorithms. +They generate and decode data using the [LZ4 block format]. #### High Compression variant @@ -49,9 +49,17 @@ The following build macro can be determined at compilation time : - `LZ4_FAST_DEC_LOOP` : this triggers the optimized decompression loop. This loops works great on x86/x64 cpus, and is automatically enabled on this platform. It's possible to enable or disable it manually, by passing `LZ4_FAST_DEC_LOOP=1` or `0` to the preprocessor. - Typically with `gcc` : `-DLZ4_FAST_DEC_LOOP=1`, + For example, with `gcc` : `-DLZ4_FAST_DEC_LOOP=1`, and with `make` : `CPPFLAGS+=-DLZ4_FAST_DEC_LOOP=1 make lz4`. +- `LZ4_DISABLE_DEPRECATE_WARNINGS` : invoking a deprecated function will make the compiler generate a warning. + This is meant to invite users to update their source code. + Should this be a problem, it's generally to make the compiler ignore these warnings, + for example with `-Wno-deprecated-declarations` on `gcc`, + or `_CRT_SECURE_NO_WARNINGS` for Visual Studio. + Another method is to define `LZ4_DISABLE_DEPRECATE_WARNINGS` + before including the LZ4 header files. + #### Amalgamation diff --git a/lib/lz4.h b/lib/lz4.h index c848f5f..1dc8e00 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -563,11 +563,16 @@ union LZ4_streamDecode_u { **************************************/ /*! Deprecation warnings - * Should deprecation warnings be a problem, - * it is generally possible to disable them, + * + * Deprecated functions make the compiler generate a warning when invoked. + * This is meant to invite users to update their source code. + * Should deprecation warnings be a problem, it is generally possible to disable them, * typically with -Wno-deprecated-declarations for gcc * or _CRT_SECURE_NO_WARNINGS in Visual. - * Otherwise, it's also possible to define LZ4_DISABLE_DEPRECATE_WARNINGS */ + * + * Another method is to define LZ4_DISABLE_DEPRECATE_WARNINGS + * before including the header file. + */ #ifdef LZ4_DISABLE_DEPRECATE_WARNINGS # define LZ4_DEPRECATED(message) /* disable deprecation warnings */ #else @@ -640,9 +645,12 @@ LZ4_DEPRECATED("use LZ4_decompress_fast_usingDict() instead") LZ4LIB_API int LZ4 * But they may happen if input data is invalid (error or intentional tampering). * As a consequence, use these functions in trusted environments with trusted data **only**. */ -LZ4LIB_API int LZ4_decompress_fast (const char* src, char* dst, int originalSize); -LZ4LIB_API int LZ4_decompress_fast_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* src, char* dst, int originalSize); -LZ4LIB_API int LZ4_decompress_fast_usingDict (const char* src, char* dst, int originalSize, const char* dictStart, int dictSize); +LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe() instead") LZ4LIB_API +int LZ4_decompress_fast (const char* src, char* dst, int originalSize); +LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe_continue() instead") LZ4LIB_API +int LZ4_decompress_fast_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* src, char* dst, int originalSize); +LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe_usingDict() instead") LZ4LIB_API +int LZ4_decompress_fast_usingDict (const char* src, char* dst, int originalSize, const char* dictStart, int dictSize); #endif /* LZ4_H_2983827168210 */ diff --git a/tests/fullbench.c b/tests/fullbench.c index 4b4e921..e08947d 100644 --- a/tests/fullbench.c +++ b/tests/fullbench.c @@ -42,6 +42,7 @@ #include /* strcmp */ #include /* clock_t, clock(), CLOCKS_PER_SEC */ +#define LZ4_DISABLE_DEPRECATE_WARNINGS /* LZ4_decompress_fast */ #include "lz4.h" #include "lz4hc.h" #include "lz4frame.h" diff --git a/tests/fuzzer.c b/tests/fuzzer.c index 893ffc1..69c763b 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -32,8 +32,6 @@ # pragma warning(disable : 4310) /* disable: C4310: constant char value > 127 */ #endif -#define LZ4_DISABLE_DEPRECATE_WARNINGS - /*-************************************ * Dependencies @@ -53,7 +51,9 @@ # include /* mmap */ #endif +#define LZ4_DISABLE_DEPRECATE_WARNINGS /* LZ4_decompress_fast */ #define LZ4_STATIC_LINKING_ONLY +#include "lz4.h" #define LZ4_HC_STATIC_LINKING_ONLY #include "lz4hc.h" #define XXH_STATIC_LINKING_ONLY -- cgit v1.2.3 From c491df54eca484654d14b3608406c35ab7366ec6 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 4 Apr 2019 17:03:05 -0700 Subject: created LZ4_initStreamHC() - promoted LZ4_resetStreamHC_fast() to stable - moved LZ4_resetStreamHC() to deprecated (but do not generate a warning yet) - Updated doc, to highlight difference between init and reset - switched all invocations of LZ4_resetStreamHC() onto LZ4_initStreamHC() - misc: ensure `make all` also builds /tests --- Makefile | 6 ++- lib/lz4frame.c | 126 ++++++++++++++++++++++++++++----------------------- lib/lz4hc.c | 132 +++++++++++++++++++++++++++++------------------------- lib/lz4hc.h | 75 ++++++++++++++++++++----------- tests/fullbench.c | 15 ++++--- tests/fuzzer.c | 42 ++++++++--------- 6 files changed, 225 insertions(+), 171 deletions(-) diff --git a/Makefile b/Makefile index df54c73..f3c6ce2 100644 --- a/Makefile +++ b/Makefile @@ -50,7 +50,7 @@ endif default: lib-release lz4-release .PHONY: all -all: allmost examples manuals +all: allmost examples manuals build_tests .PHONY: allmost allmost: lib lz4 @@ -75,6 +75,10 @@ examples: liblz4.a manuals: @$(MAKE) -C contrib/gen_manual $@ +.PHONY: build_tests +build_tests: + @$(MAKE) -C $(TESTDIR) all + .PHONY: clean clean: @$(MAKE) -C $(LZ4DIR) $@ > $(VOID) diff --git a/lib/lz4frame.c b/lib/lz4frame.c index 2164470..f076204 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -1,41 +1,44 @@ /* -LZ4 auto-framing library -Copyright (C) 2011-2016, 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 homepage : http://www.lz4.org -- LZ4 source repository : https://github.com/lz4/lz4 -*/ + * LZ4 auto-framing library + * Copyright (C) 2011-2016, 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 homepage : http://www.lz4.org + * - LZ4 source repository : https://github.com/lz4/lz4 + */ /* LZ4F is a stand-alone API to create LZ4-compressed Frames -* in full conformance with specification v1.6.1 . -* This library rely upon memory management capabilities. -* */ + * in full conformance with specification v1.6.1 . + * This library rely upon memory management capabilities (malloc, free) + * provided either by , + * or redirected towards another library of user's choice + * (see Memory Routines below). + */ /*-************************************ @@ -62,20 +65,27 @@ You can contact the author at : /*-************************************ * Memory routines **************************************/ +/* + * User may redirect invocations of + * malloc(), calloc() and free() + * towards another library or solution of their choice + * by modifying below section. + */ #include /* malloc, calloc, free */ #define ALLOC(s) malloc(s) -#ifndef LZ4_SRC_INCLUDED -#define ALLOC_AND_ZERO(s) calloc(1,(s)) +#ifndef LZ4_SRC_INCLUDED /* avoid redefinition when sources are coalesced */ +# define ALLOC_AND_ZERO(s) calloc(1,(s)) #endif #define FREEMEM(p) free(p) + #include /* memset, memcpy, memmove */ -#ifndef LZ4_SRC_INCLUDED -#define MEM_INIT memset +#ifndef LZ4_SRC_INCLUDED /* avoid redefinition when sources are coalesced */ +# define MEM_INIT memset #endif /*-************************************ -* Includes +* Library declarations **************************************/ #define LZ4F_STATIC_LINKING_ONLY #include "lz4frame.h" @@ -606,11 +616,12 @@ size_t LZ4F_compressBegin_usingCDict(LZ4F_cctx* cctxPtr, if (cctxPtr->lz4CtxAlloc < ctxTypeID) { FREEMEM(cctxPtr->lz4CtxPtr); if (cctxPtr->prefs.compressionLevel < LZ4HC_CLEVEL_MIN) { - cctxPtr->lz4CtxPtr = (void*)LZ4_createStream(); + cctxPtr->lz4CtxPtr = LZ4_createStream(); } else { - cctxPtr->lz4CtxPtr = (void*)LZ4_createStreamHC(); + cctxPtr->lz4CtxPtr = LZ4_createStreamHC(); } - if (cctxPtr->lz4CtxPtr == NULL) return err0r(LZ4F_ERROR_allocation_failed); + if (cctxPtr->lz4CtxPtr == NULL) + return err0r(LZ4F_ERROR_allocation_failed); cctxPtr->lz4CtxAlloc = ctxTypeID; cctxPtr->lz4CtxState = ctxTypeID; } else if (cctxPtr->lz4CtxState != ctxTypeID) { @@ -619,7 +630,7 @@ size_t LZ4F_compressBegin_usingCDict(LZ4F_cctx* cctxPtr, if (cctxPtr->prefs.compressionLevel < LZ4HC_CLEVEL_MIN) { LZ4_resetStream((LZ4_stream_t *) cctxPtr->lz4CtxPtr); } else { - LZ4_resetStreamHC((LZ4_streamHC_t *) cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel); + LZ4_initStreamHC((LZ4_streamHC_t *) cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel); } cctxPtr->lz4CtxState = ctxTypeID; } @@ -1268,9 +1279,10 @@ static void LZ4F_updateDict(LZ4F_dctx* dctx, return; } - if (dstPtr - dstBufferStart + dstSize >= 64 KB) { /* history in dstBuffer becomes large enough to become dictionary */ + assert(dstPtr >= dstBufferStart); + if ((size_t)(dstPtr - dstBufferStart) + dstSize >= 64 KB) { /* history in dstBuffer becomes large enough to become dictionary */ dctx->dict = (const BYTE*)dstBufferStart; - dctx->dictSize = dstPtr - dstBufferStart + dstSize; + dctx->dictSize = (size_t)(dstPtr - dstBufferStart) + dstSize; return; } @@ -1286,7 +1298,7 @@ static void LZ4F_updateDict(LZ4F_dctx* dctx, } if (withinTmp) { /* copy relevant dict portion in front of tmpOut within tmpOutBuffer */ - size_t const preserveSize = dctx->tmpOut - dctx->tmpOutBuffer; + size_t const preserveSize = (size_t)(dctx->tmpOut - dctx->tmpOutBuffer); size_t copySize = 64 KB - dctx->tmpOutSize; const BYTE* const oldDictEnd = dctx->dict + dctx->dictSize - dctx->tmpOutStart; if (dctx->tmpOutSize > 64 KB) copySize = 0; @@ -1371,7 +1383,7 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, case dstage_getFrameHeader: if ((size_t)(srcEnd-srcPtr) >= maxFHSize) { /* enough to decode - shortcut */ - size_t const hSize = LZ4F_decodeHeader(dctx, srcPtr, srcEnd-srcPtr); /* will update dStage appropriately */ + size_t const hSize = LZ4F_decodeHeader(dctx, srcPtr, (size_t)(srcEnd-srcPtr)); /* will update dStage appropriately */ if (LZ4F_isError(hSize)) return hSize; srcPtr += hSize; break; @@ -1593,13 +1605,13 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, dict, (int)dictSize); if (decodedSize < 0) return err0r(LZ4F_ERROR_GENERIC); /* decompression failed */ if (dctx->frameInfo.contentChecksumFlag) - XXH32_update(&(dctx->xxh), dstPtr, decodedSize); + XXH32_update(&(dctx->xxh), dstPtr, (size_t)decodedSize); if (dctx->frameInfo.contentSize) - dctx->frameRemainingSize -= decodedSize; + dctx->frameRemainingSize -= (size_t)decodedSize; /* dictionary management */ if (dctx->frameInfo.blockMode==LZ4F_blockLinked) - LZ4F_updateDict(dctx, dstPtr, decodedSize, dstStart, 0); + LZ4F_updateDict(dctx, dstPtr, (size_t)decodedSize, dstStart, 0); dstPtr += decodedSize; dctx->dStage = dstage_getBlockHeader; @@ -1636,10 +1648,10 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, if (decodedSize < 0) /* decompression failed */ return err0r(LZ4F_ERROR_decompressionFailed); if (dctx->frameInfo.contentChecksumFlag) - XXH32_update(&(dctx->xxh), dctx->tmpOut, decodedSize); + XXH32_update(&(dctx->xxh), dctx->tmpOut, (size_t)decodedSize); if (dctx->frameInfo.contentSize) - dctx->frameRemainingSize -= decodedSize; - dctx->tmpOutSize = decodedSize; + dctx->frameRemainingSize -= (size_t)decodedSize; + dctx->tmpOutSize = (size_t)decodedSize; dctx->tmpOutStart = 0; dctx->dStage = dstage_flushOut; } @@ -1767,7 +1779,7 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, && ((unsigned)(dctx->dStage)-2 < (unsigned)(dstage_getSuffix)-2) ) /* valid stages : [init ... getSuffix[ */ { if (dctx->dStage == dstage_flushOut) { - size_t const preserveSize = dctx->tmpOut - dctx->tmpOutBuffer; + size_t const preserveSize = (size_t)(dctx->tmpOut - dctx->tmpOutBuffer); size_t copySize = 64 KB - dctx->tmpOutSize; const BYTE* oldDictEnd = dctx->dict + dctx->dictSize - dctx->tmpOutStart; if (dctx->tmpOutSize > 64 KB) copySize = 0; @@ -1791,8 +1803,8 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, } } - *srcSizePtr = (srcPtr - srcStart); - *dstSizePtr = (dstPtr - dstStart); + *srcSizePtr = (size_t)(srcPtr - srcStart); + *dstSizePtr = (size_t)(dstPtr - dstStart); return nextSrcSizeHint; } diff --git a/lib/lz4hc.c b/lib/lz4hc.c index 129bf0c..644b8eb 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -105,7 +105,7 @@ static void LZ4HC_clearTables (LZ4HC_CCtx_internal* hc4) static void LZ4HC_init (LZ4HC_CCtx_internal* hc4, const BYTE* start) { - uptrval startingOffset = hc4->end - hc4->base; + uptrval startingOffset = (uptrval)(hc4->end - hc4->base); if (startingOffset > 1 GB) { LZ4HC_clearTables(hc4); startingOffset = 0; @@ -266,7 +266,7 @@ LZ4HC_InsertAndGetWiderMatch ( if (LZ4_read16(iLowLimit + longest - 1) == LZ4_read16(matchPtr - lookBackLength + longest - 1)) { if (LZ4_read32(matchPtr) == pattern) { int const back = lookBackLength ? LZ4HC_countBack(ip, matchPtr, iLowLimit, lowPrefixPtr) : 0; - matchLength = MINMATCH + LZ4_count(ip+MINMATCH, matchPtr+MINMATCH, iHighLimit); + matchLength = MINMATCH + (int)LZ4_count(ip+MINMATCH, matchPtr+MINMATCH, iHighLimit); matchLength -= back; if (matchLength > longest) { longest = matchLength; @@ -280,7 +280,7 @@ LZ4HC_InsertAndGetWiderMatch ( int back = 0; const BYTE* vLimit = ip + (dictLimit - matchIndex); if (vLimit > iHighLimit) vLimit = iHighLimit; - matchLength = LZ4_count(ip+MINMATCH, matchPtr+MINMATCH, vLimit) + MINMATCH; + matchLength = (int)LZ4_count(ip+MINMATCH, matchPtr+MINMATCH, vLimit) + MINMATCH; if ((ip+matchLength == vLimit) && (vLimit < iHighLimit)) matchLength += LZ4_count(ip+matchLength, lowPrefixPtr, iHighLimit); back = lookBackLength ? LZ4HC_countBack(ip, matchPtr, iLowLimit, dictStart) : 0; @@ -293,11 +293,11 @@ LZ4HC_InsertAndGetWiderMatch ( if (chainSwap && matchLength==longest) { /* better match => select a better chain */ assert(lookBackLength==0); /* search forward only */ - if (matchIndex + longest <= ipIndex) { + if (matchIndex + (U32)longest <= ipIndex) { U32 distanceToNextMatch = 1; int pos; for (pos = 0; pos <= longest - MINMATCH; pos++) { - U32 const candidateDist = DELTANEXTU16(chainTable, matchIndex + pos); + U32 const candidateDist = DELTANEXTU16(chainTable, matchIndex + (U32)pos); if (candidateDist > distanceToNextMatch) { distanceToNextMatch = candidateDist; matchChainPos = pos; @@ -353,12 +353,14 @@ LZ4HC_InsertAndGetWiderMatch ( } } /* PA optimization */ /* follow current chain */ - matchIndex -= DELTANEXTU16(chainTable, matchIndex+matchChainPos); + matchIndex -= DELTANEXTU16(chainTable, matchIndex + matchChainPos); } /* while ((matchIndex>=lowestMatchIndex) && (nbAttempts)) */ - if (dict == usingDictCtxHc && nbAttempts && ipIndex - lowestMatchIndex < MAX_DISTANCE) { - size_t const dictEndOffset = dictCtx->end - dictCtx->base; + if ( dict == usingDictCtxHc + && nbAttempts + && ipIndex - lowestMatchIndex < MAX_DISTANCE) { + size_t const dictEndOffset = (size_t)(dictCtx->end - dictCtx->base); U32 dictMatchIndex = dictCtx->hashTable[LZ4HC_hashPtr(ip)]; assert(dictEndOffset <= 1 GB); matchIndex = dictMatchIndex + lowestMatchIndex - (U32)dictEndOffset; @@ -370,22 +372,19 @@ LZ4HC_InsertAndGetWiderMatch ( int back = 0; const BYTE* vLimit = ip + (dictEndOffset - dictMatchIndex); if (vLimit > iHighLimit) vLimit = iHighLimit; - mlt = LZ4_count(ip+MINMATCH, matchPtr+MINMATCH, vLimit) + MINMATCH; + mlt = (int)LZ4_count(ip+MINMATCH, matchPtr+MINMATCH, vLimit) + MINMATCH; back = lookBackLength ? LZ4HC_countBack(ip, matchPtr, iLowLimit, dictCtx->base + dictCtx->dictLimit) : 0; mlt -= back; if (mlt > longest) { longest = mlt; *matchpos = base + matchIndex + back; *startpos = ip + back; - } - } + } } { U32 const nextOffset = DELTANEXTU16(dictCtx->chainTable, dictMatchIndex); dictMatchIndex -= nextOffset; matchIndex -= nextOffset; - } - } - } + } } } return longest; } @@ -518,7 +517,7 @@ LZ4_FORCE_INLINE int LZ4HC_compress_hashChain ( /* Main Loop */ while (ip <= mflimit) { - ml = LZ4HC_InsertAndFindBestMatch (ctx, ip, matchlimit, &ref, maxNbAttempts, patternAnalysis, dict); + ml = LZ4HC_InsertAndFindBestMatch(ctx, ip, matchlimit, &ref, maxNbAttempts, patternAnalysis, dict); if (mldictCtx == NULL); return LZ4HC_compress_generic_internal(ctx, src, dst, srcSizePtr, dstCapacity, cLevel, limit, noDictCtx); } -static int LZ4HC_compress_generic_dictCtx ( - LZ4HC_CCtx_internal* const ctx, - const char* const src, - char* const dst, - int* const srcSizePtr, - int const dstCapacity, - int cLevel, - limitedOutput_directive limit - ) +static int +LZ4HC_compress_generic_dictCtx ( + LZ4HC_CCtx_internal* const ctx, + const char* const src, + char* const dst, + int* const srcSizePtr, + int const dstCapacity, + int cLevel, + limitedOutput_directive limit + ) { const size_t position = ctx->end - ctx->base - ctx->lowLimit; assert(ctx->dictCtx != NULL); @@ -807,15 +808,16 @@ static int LZ4HC_compress_generic_dictCtx ( } } -static int LZ4HC_compress_generic ( - LZ4HC_CCtx_internal* const ctx, - const char* const src, - char* const dst, - int* const srcSizePtr, - int const dstCapacity, - int cLevel, - limitedOutput_directive limit - ) +static int +LZ4HC_compress_generic ( + LZ4HC_CCtx_internal* const ctx, + const char* const src, + char* const dst, + int* const srcSizePtr, + int const dstCapacity, + int cLevel, + limitedOutput_directive limit + ) { if (ctx->dictCtx == NULL) { return LZ4HC_compress_generic_noDictCtx(ctx, src, dst, srcSizePtr, dstCapacity, cLevel, limit); @@ -842,7 +844,7 @@ int LZ4_compress_HC_extStateHC_fastReset (void* state, const char* src, char* ds int LZ4_compress_HC_extStateHC (void* state, const char* src, char* dst, int srcSize, int dstCapacity, int compressionLevel) { if (((size_t)(state)&(sizeof(void*)-1)) != 0) return 0; /* Error : state is not aligned for pointers (32 or 64 bits) */ - LZ4_resetStreamHC ((LZ4_streamHC_t*)state, compressionLevel); + LZ4_initStreamHC (state, compressionLevel); /* full initialization, as there is no guarantee on state's content (could be freshly malloc'ed) */ return LZ4_compress_HC_extStateHC_fastReset(state, src, dst, srcSize, dstCapacity, compressionLevel); } @@ -861,12 +863,10 @@ int LZ4_compress_HC(const char* src, char* dst, int srcSize, int dstCapacity, in return cSize; } -/* LZ4_compress_HC_destSize() : - * only compatible with regular HC parser */ int LZ4_compress_HC_destSize(void* LZ4HC_Data, const char* source, char* dest, int* sourceSizePtr, int targetDestSize, int cLevel) { LZ4HC_CCtx_internal* const ctx = &((LZ4_streamHC_t*)LZ4HC_Data)->internal_donotuse; - LZ4_resetStreamHC((LZ4_streamHC_t*)LZ4HC_Data, cLevel); + LZ4_initStreamHC(LZ4HC_Data, cLevel); /* full initialization, as there is no guarantee on state's content (could be freshly malloc'ed) */ LZ4HC_init(ctx, (const BYTE*) source); return LZ4HC_compress_generic(ctx, source, dest, sourceSizePtr, targetDestSize, cLevel, limitedDestSize); } @@ -877,14 +877,16 @@ int LZ4_compress_HC_destSize(void* LZ4HC_Data, const char* source, char* dest, i * Streaming Functions **************************************/ /* allocation */ -LZ4_streamHC_t* LZ4_createStreamHC(void) { +LZ4_streamHC_t* LZ4_createStreamHC(void) +{ LZ4_streamHC_t* const LZ4_streamHCPtr = (LZ4_streamHC_t*)ALLOC(sizeof(LZ4_streamHC_t)); if (LZ4_streamHCPtr==NULL) return NULL; - LZ4_resetStreamHC(LZ4_streamHCPtr, LZ4HC_CLEVEL_DEFAULT); + LZ4_initStreamHC(LZ4_streamHCPtr, LZ4HC_CLEVEL_DEFAULT); /* full initialization, malloc'ed buffer can be full of garbage */ return LZ4_streamHCPtr; } -int LZ4_freeStreamHC (LZ4_streamHC_t* LZ4_streamHCPtr) { +int LZ4_freeStreamHC (LZ4_streamHC_t* LZ4_streamHCPtr) +{ DEBUGLOG(4, "LZ4_freeStreamHC(%p)", LZ4_streamHCPtr); if (!LZ4_streamHCPtr) return 0; /* support free on NULL */ free(LZ4_streamHCPtr); @@ -893,9 +895,10 @@ int LZ4_freeStreamHC (LZ4_streamHC_t* LZ4_streamHCPtr) { /* initialization */ -void LZ4_resetStreamHC (LZ4_streamHC_t* LZ4_streamHCPtr, int compressionLevel) +void LZ4_initStreamHC (void* state, int compressionLevel) { - LZ4_STATIC_ASSERT(sizeof(LZ4HC_CCtx_internal) <= sizeof(size_t) * LZ4_STREAMHCSIZE_SIZET); /* if compilation fails here, LZ4_STREAMHCSIZE must be increased */ + LZ4_streamHC_t* const LZ4_streamHCPtr = (LZ4_streamHC_t*)state; + LZ4_STATIC_ASSERT(sizeof(LZ4HC_CCtx_internal) <= LZ4_STREAMHCSIZE); /* if compilation fails here, LZ4_STREAMHCSIZE must be increased */ DEBUGLOG(4, "LZ4_resetStreamHC(%p, %d)", LZ4_streamHCPtr, compressionLevel); LZ4_streamHCPtr->internal_donotuse.end = (const BYTE *)(ptrdiff_t)-1; LZ4_streamHCPtr->internal_donotuse.base = NULL; @@ -905,11 +908,17 @@ void LZ4_resetStreamHC (LZ4_streamHC_t* LZ4_streamHCPtr, int compressionLevel) LZ4_setCompressionLevel(LZ4_streamHCPtr, compressionLevel); } +/* just a stub */ +void LZ4_resetStreamHC (LZ4_streamHC_t* LZ4_streamHCPtr, int compressionLevel) +{ + return LZ4_initStreamHC(LZ4_streamHCPtr, compressionLevel); +} + void LZ4_resetStreamHC_fast (LZ4_streamHC_t* LZ4_streamHCPtr, int compressionLevel) { DEBUGLOG(4, "LZ4_resetStreamHC_fast(%p, %d)", LZ4_streamHCPtr, compressionLevel); if (LZ4_streamHCPtr->internal_donotuse.dirty) { - LZ4_resetStreamHC(LZ4_streamHCPtr, compressionLevel); + LZ4_initStreamHC(LZ4_streamHCPtr, compressionLevel); } else { LZ4_streamHCPtr->internal_donotuse.end -= (uptrval)LZ4_streamHCPtr->internal_donotuse.base; LZ4_streamHCPtr->internal_donotuse.base = NULL; @@ -938,7 +947,7 @@ int LZ4_loadDictHC (LZ4_streamHC_t* LZ4_streamHCPtr, const char* dictionary, int dictionary += dictSize - 64 KB; dictSize = 64 KB; } - LZ4_resetStreamHC(LZ4_streamHCPtr, ctxPtr->compressionLevel); + LZ4_initStreamHC(LZ4_streamHCPtr, ctxPtr->compressionLevel); LZ4HC_init (ctxPtr, (const BYTE*)dictionary); ctxPtr->end = (const BYTE*)dictionary + dictSize; if (dictSize >= 4) LZ4HC_Insert (ctxPtr, ctxPtr->end-3); @@ -1029,8 +1038,8 @@ int LZ4_saveDictHC (LZ4_streamHC_t* LZ4_streamHCPtr, char* safeBuffer, int dictS { U32 const endIndex = (U32)(streamPtr->end - streamPtr->base); streamPtr->end = (const BYTE*)safeBuffer + dictSize; streamPtr->base = streamPtr->end - endIndex; - streamPtr->dictLimit = endIndex - dictSize; - streamPtr->lowLimit = endIndex - dictSize; + streamPtr->dictLimit = endIndex - (U32)dictSize; + streamPtr->lowLimit = endIndex - (U32)dictSize; if (streamPtr->nextToUpdate < streamPtr->dictLimit) streamPtr->nextToUpdate = streamPtr->dictLimit; } return dictSize; @@ -1061,7 +1070,7 @@ int LZ4_resetStreamStateHC(void* state, char* inputBuffer) { LZ4HC_CCtx_internal *ctx = &((LZ4_streamHC_t*)state)->internal_donotuse; if ((((size_t)state) & (sizeof(void*)-1)) != 0) return 1; /* Error : pointer is not aligned for pointer (32 or 64 bits) */ - LZ4_resetStreamHC((LZ4_streamHC_t*)state, ((LZ4_streamHC_t*)state)->internal_donotuse.compressionLevel); + LZ4_initStreamHC((LZ4_streamHC_t*)state, ((LZ4_streamHC_t*)state)->internal_donotuse.compressionLevel); LZ4HC_init(ctx, (const BYTE*)inputBuffer); return 0; } @@ -1070,7 +1079,7 @@ void* LZ4_createHC (const char* inputBuffer) { LZ4_streamHC_t* hc4 = (LZ4_streamHC_t*)ALLOC(sizeof(LZ4_streamHC_t)); if (hc4 == NULL) return NULL; /* not enough memory */ - LZ4_resetStreamHC(hc4, 0 /* compressionLevel */); + LZ4_initStreamHC(hc4, 0 /* compressionLevel */); LZ4HC_init (&hc4->internal_donotuse, (const BYTE*)inputBuffer); return hc4; } @@ -1102,7 +1111,7 @@ char* LZ4_slideInputBufferHC(void* LZ4HC_Data) /* ================================================ - * LZ4 Optimal parser (levels 10-12) + * LZ4 Optimal parser (levels [LZ4HC_CLEVEL_OPT_MIN - LZ4HC_CLEVEL_MAX]) * ===============================================*/ typedef struct { int price; @@ -1115,8 +1124,9 @@ typedef struct { LZ4_FORCE_INLINE int LZ4HC_literalsPrice(int const litlen) { int price = litlen; + assert(litlen >= 0); if (litlen >= (int)RUN_MASK) - price += 1 + (litlen-RUN_MASK)/255; + price += 1 + ((litlen-(int)RUN_MASK) / 255); return price; } @@ -1125,11 +1135,13 @@ LZ4_FORCE_INLINE int LZ4HC_literalsPrice(int const litlen) LZ4_FORCE_INLINE int LZ4HC_sequencePrice(int litlen, int mlen) { int price = 1 + 2 ; /* token + 16-bit offset */ + assert(litlen >= 0); + assert(mlen >= MINMATCH); price += LZ4HC_literalsPrice(litlen); if (mlen >= (int)(ML_MASK+MINMATCH)) - price += 1 + (mlen-(ML_MASK+MINMATCH))/255; + price += 1 + ((mlen-(int)(ML_MASK+MINMATCH)) / 255); return price; } diff --git a/lib/lz4hc.h b/lib/lz4hc.h index d1772f3..91b14ee 100644 --- a/lib/lz4hc.h +++ b/lib/lz4hc.h @@ -84,9 +84,9 @@ LZ4LIB_API int LZ4_compress_HC_extStateHC(void* stateHC, const char* src, char* * Will compress as much data as possible from `src` * to fit into `targetDstSize` budget. * Result is provided in 2 parts : - * @return : the number of bytes written into 'dst' + * @return : the number of bytes written into 'dst' (necessarily <= targetDstSize) * or 0 if compression fails. - * `srcSizePtr` : value will be updated to indicate how much bytes were read from `src` + * `srcSizePtr` : on success, *srcSizePtr is updated to indicate how much bytes were read from `src` */ LZ4LIB_API int LZ4_compress_HC_destSize(void* stateHC, const char* src, char* dst, @@ -103,8 +103,8 @@ LZ4LIB_API int LZ4_compress_HC_destSize(void* stateHC, /*! LZ4_createStreamHC() and LZ4_freeStreamHC() : * These functions create and release memory for LZ4 HC streaming state. * Newly created states are automatically initialized. - * Existing states can be re-used several times, using LZ4_resetStreamHC(). - * These methods are API and ABI stable, they can be used in combination with a DLL. + * A same state can be used multiple times consecutively, + * starting with LZ4_resetStreamHC_fast() to start a new stream of blocks. */ LZ4LIB_API LZ4_streamHC_t* LZ4_createStreamHC(void); LZ4LIB_API int LZ4_freeStreamHC (LZ4_streamHC_t* streamHCPtr); @@ -121,35 +121,38 @@ LZ4LIB_API int LZ4_freeStreamHC (LZ4_streamHC_t* streamHCPtr); Selecting the compression level can be done with LZ4_resetStreamHC_fast() (starts a new stream) or LZ4_setCompressionLevel() (anytime, between blocks in the same stream) (experimental). - LZ4_resetStreamHC_fast() only works on states which have been properly initialized at least once. - - If state space is provided manually with no guarantee of its content, for example allocated on stack, - it must be fully initialized, using LZ4_resetStreamHC(). - LZ4_resetStreamHC() is heavier, and it's guaranteed to succeed on any valid memory segment. - In contrast, LZ4_resetStreamHC_fast() only works on states which have been properly initialized at least once. + LZ4_resetStreamHC_fast() only works on states which have been properly initialized at least once, + which is automatically the case when state is created using LZ4_createStreamHC(). After reset, a first "fictional block" can be designated as initial dictionary, using LZ4_loadDictHC() (Optional). Invoke LZ4_compress_HC_continue() to compress each successive block. The number of blocks is unlimited. - Previous input blocks (including initial dictionary when present) must remain accessible and unmodified during compression. + Previous input blocks, including initial dictionary when present, + must remain accessible and unmodified during compression. + + It's allowed to update compression level anytime between blocks, + using LZ4_setCompressionLevel() (experimental). - 'dst' buffer should be sized to handle worst case scenarios (see LZ4_compressBound()), ensuring compression success. - In case of failure, the API does not guarantee recovery, so the state _must_ be reset. - Whenever `dst` buffer size cannot be made >= LZ4_compressBound(), - consider using LZ4_compress_HC_continue_destSize() to ensure success. + 'dst' buffer should be sized to handle worst case scenarios + (see LZ4_compressBound(), it ensures compression success). + In case of failure, the API does not guarantee recovery, + so the state _must_ be reset. + To ensure compression success + whenever `dst` buffer size cannot be made >= LZ4_compressBound(), + consider using LZ4_compress_HC_continue_destSize(). Whenever previous input blocks can't be preserved unmodified in-place during compression of next blocks, it's possible to copy the last blocks into a more stable memory space, using LZ4_saveDictHC(). Return value of LZ4_saveDictHC() is the size of dictionary effectively saved into 'safeBuffer' (<= 64 KB) After completing a streaming compression, - it's possible to start a new stream of blocks, and re-use the same LZ4_streamHC_t state - by resetting it, using LZ4_resetStreamHC_fast(). + it's possible to start a new stream of blocks, using the same LZ4_streamHC_t state, + just by resetting it, using LZ4_resetStreamHC_fast(). */ -LZ4LIB_API void LZ4_resetStreamHC (LZ4_streamHC_t* streamHCPtr, int compressionLevel); +LZ4LIB_API void LZ4_resetStreamHC_fast(LZ4_streamHC_t* streamHCPtr, int compressionLevel); LZ4LIB_API int LZ4_loadDictHC (LZ4_streamHC_t* streamHCPtr, const char* dictionary, int dictSize); LZ4LIB_API int LZ4_compress_HC_continue (LZ4_streamHC_t* streamHCPtr, @@ -239,22 +242,31 @@ struct LZ4HC_CCtx_internal #endif -/* do not use these definitions directly. - * allocate an LZ4_streamHC_t instead. */ +/* Do not use these definitions directly ! + * Declare or allocate an LZ4_streamHC_t instead. + */ #define LZ4_STREAMHCSIZE (4*LZ4HC_HASHTABLESIZE + 2*LZ4HC_MAXD + 56 + ((sizeof(void*)==16) ? 56 : 0) /* AS400*/ ) /* 262200 or 262256*/ #define LZ4_STREAMHCSIZE_SIZET (LZ4_STREAMHCSIZE / sizeof(size_t)) union LZ4_streamHC_u { size_t table[LZ4_STREAMHCSIZE_SIZET]; LZ4HC_CCtx_internal internal_donotuse; }; /* previously typedef'd to LZ4_streamHC_t */ + /* LZ4_streamHC_t : * This structure allows static allocation of LZ4 HC streaming state. - * State must be initialized using LZ4_resetStreamHC() before first use. + * This can be used to allocate statically, on state, or as part of a larger structure. + * + * Such state **must** be initialized using LZ4_initStreamHC() before first use. + * + * Note that invoking LZ4_initStreamHC() is not required when + * the state was created using LZ4_createStreamHC() (which is recommended). + * Using the normal builder, a newly created state is automatically initialized. * * Static allocation shall only be used in combination with static linking. - * When invoking LZ4 from a DLL, use create/free functions instead, which are API and ABI stable. */ +LZ4LIB_API void LZ4_initStreamHC (void* streamHCPtr, int compressionLevel); + /*-************************************ * Deprecated Functions @@ -264,11 +276,11 @@ union LZ4_streamHC_u { /* deprecated compression functions */ LZ4_DEPRECATED("use LZ4_compress_HC() instead") LZ4LIB_API int LZ4_compressHC (const char* source, char* dest, int inputSize); LZ4_DEPRECATED("use LZ4_compress_HC() instead") LZ4LIB_API int LZ4_compressHC_limitedOutput (const char* source, char* dest, int inputSize, int maxOutputSize); -LZ4_DEPRECATED("use LZ4_compress_HC() instead") LZ4LIB_API int LZ4_compressHC2 (const char* source, char* dest, int inputSize, int compressionLevel); -LZ4_DEPRECATED("use LZ4_compress_HC() instead") LZ4LIB_API int LZ4_compressHC2_limitedOutput (const char* source, char* dest, int inputSize, int maxOutputSize, int compressionLevel); +LZ4_DEPRECATED("use LZ4_compress_HC() instead") LZ4LIB_API int LZ4_compressHC2 (const char* source, char* dest, int inputSize, int compressionLevel); +LZ4_DEPRECATED("use LZ4_compress_HC() instead") LZ4LIB_API int LZ4_compressHC2_limitedOutput(const char* source, char* dest, int inputSize, int maxOutputSize, int compressionLevel); LZ4_DEPRECATED("use LZ4_compress_HC_extStateHC() instead") LZ4LIB_API int LZ4_compressHC_withStateHC (void* state, const char* source, char* dest, int inputSize); LZ4_DEPRECATED("use LZ4_compress_HC_extStateHC() instead") LZ4LIB_API int LZ4_compressHC_limitedOutput_withStateHC (void* state, const char* source, char* dest, int inputSize, int maxOutputSize); -LZ4_DEPRECATED("use LZ4_compress_HC_extStateHC() instead") LZ4LIB_API int LZ4_compressHC2_withStateHC (void* state, const char* source, char* dest, int inputSize, int compressionLevel); +LZ4_DEPRECATED("use LZ4_compress_HC_extStateHC() instead") LZ4LIB_API int LZ4_compressHC2_withStateHC (void* state, const char* source, char* dest, int inputSize, int compressionLevel); LZ4_DEPRECATED("use LZ4_compress_HC_extStateHC() instead") LZ4LIB_API int LZ4_compressHC2_limitedOutput_withStateHC(void* state, const char* source, char* dest, int inputSize, int maxOutputSize, int compressionLevel); LZ4_DEPRECATED("use LZ4_compress_HC_continue() instead") LZ4LIB_API int LZ4_compressHC_continue (LZ4_streamHC_t* LZ4_streamHCPtr, const char* source, char* dest, int inputSize); LZ4_DEPRECATED("use LZ4_compress_HC_continue() instead") LZ4LIB_API int LZ4_compressHC_limitedOutput_continue (LZ4_streamHC_t* LZ4_streamHCPtr, const char* source, char* dest, int inputSize, int maxOutputSize); @@ -284,12 +296,23 @@ LZ4_DEPRECATED("use LZ4_compress_HC_continue() instead") LZ4LIB_API int LZ4_comp LZ4_DEPRECATED("use LZ4_createStreamHC() instead") LZ4LIB_API void* LZ4_createHC (const char* inputBuffer); LZ4_DEPRECATED("use LZ4_saveDictHC() instead") LZ4LIB_API char* LZ4_slideInputBufferHC (void* LZ4HC_Data); LZ4_DEPRECATED("use LZ4_freeStreamHC() instead") LZ4LIB_API int LZ4_freeHC (void* LZ4HC_Data); -LZ4_DEPRECATED("use LZ4_compress_HC_continue() instead") LZ4LIB_API int LZ4_compressHC2_continue (void* LZ4HC_Data, const char* source, char* dest, int inputSize, int compressionLevel); +LZ4_DEPRECATED("use LZ4_compress_HC_continue() instead") LZ4LIB_API int LZ4_compressHC2_continue (void* LZ4HC_Data, const char* source, char* dest, int inputSize, int compressionLevel); LZ4_DEPRECATED("use LZ4_compress_HC_continue() instead") LZ4LIB_API int LZ4_compressHC2_limitedOutput_continue (void* LZ4HC_Data, const char* source, char* dest, int inputSize, int maxOutputSize, int compressionLevel); LZ4_DEPRECATED("use LZ4_createStreamHC() instead") LZ4LIB_API int LZ4_sizeofStreamStateHC(void); LZ4_DEPRECATED("use LZ4_resetStreamHC() instead") LZ4LIB_API int LZ4_resetStreamStateHC(void* state, char* inputBuffer); +/* LZ4_resetStreamHC() is now replaced by LZ4_initStreamHC(). + * The intention is to emphasize the difference with LZ4_resetStreamHC_fast(), + * which is now the recommended function to start a new stream of blocks, + * but cannot be used to initialize a memory segment containing arbitrary garbage data. + * + * It is recommended to switch to LZ4_initStreamHC(). + * LZ4_resetStreamHC() will generate deprecation warnings in a future version. + */ +LZ4LIB_API void LZ4_resetStreamHC (LZ4_streamHC_t* streamHCPtr, int compressionLevel); + + #if defined (__cplusplus) } #endif diff --git a/tests/fullbench.c b/tests/fullbench.c index e08947d..faa50cb 100644 --- a/tests/fullbench.c +++ b/tests/fullbench.c @@ -243,7 +243,7 @@ static int local_LZ4_compress_forceDict(const char* in, char* out, int inSize) LZ4_streamHC_t LZ4_streamHC; static void local_LZ4_resetStreamHC(void) { - LZ4_resetStreamHC(&LZ4_streamHC, 0); + LZ4_initStreamHC(&LZ4_streamHC, 0); } static int local_LZ4_saveDictHC(const char* in, char* out, int inSize) @@ -327,16 +327,19 @@ static int local_LZ4_decompress_safe_partial(const char* in, char* out, int inSi /* frame functions */ static int local_LZ4F_compressFrame(const char* in, char* out, int inSize) { - return (int)LZ4F_compressFrame(out, LZ4F_compressFrameBound(inSize, NULL), in, inSize, NULL); + assert(inSize >= 0); + return (int)LZ4F_compressFrame(out, LZ4F_compressFrameBound((size_t)inSize, NULL), in, (size_t)inSize, NULL); } 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 srcSize = (size_t)inSize; + size_t dstSize = (size_t)outSize; size_t result; + assert(inSize >= 0); + assert(outSize >= 0); 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); } @@ -439,7 +442,7 @@ int fullSpeedBench(const char** fileNamesTable, int nbFiles) char* out = compressed_buff; nbChunks = (int) (((int)benchedSize + (g_chunkSize-1))/ g_chunkSize); for (i=0; i g_chunkSize) { chunkP[i].origSize = g_chunkSize; remaining -= g_chunkSize; } else { chunkP[i].origSize = (int)remaining; remaining = 0; } chunkP[i].compressedBuffer = out; out += maxCompressedChunkSize; @@ -611,7 +614,7 @@ int fullSpeedBench(const char** fileNamesTable, int nbFiles) PROGRESS("%2i-%-34.34s :%10i -> %7.1f MB/s\r", loopNb, dName, (int)benchedSize, (double)benchedSize / bestTime / 1000000); /* CRC Checking */ - crcDecoded = XXH32(orig_buff, (int)benchedSize, 0); + crcDecoded = XXH32(orig_buff, benchedSize, 0); if (checkResult && (crcOriginal!=crcDecoded)) { DISPLAY("\n!!! WARNING !!! %14s : Invalid Checksum : %x != %x\n", inFileName, (unsigned)crcOriginal, (unsigned)crcDecoded); diff --git a/tests/fuzzer.c b/tests/fuzzer.c index 69c763b..6ebbf20 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -309,13 +309,13 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c unsigned long long hcbytes = 0; unsigned long long ccbytes = 0; void* const CNBuffer = malloc(COMPRESSIBLE_NOISE_LENGTH); - size_t const compressedBufferSize = LZ4_compressBound(FUZ_MAX_BLOCK_SIZE); + size_t const compressedBufferSize = (size_t)LZ4_compressBound(FUZ_MAX_BLOCK_SIZE); char* const compressedBuffer = (char*)malloc(compressedBufferSize); char* const decodedBuffer = (char*)malloc(FUZ_MAX_DICT_SIZE + FUZ_MAX_BLOCK_SIZE); size_t const labSize = 96 KB; void* const lowAddrBuffer = FUZ_createLowAddr(labSize); - void* const stateLZ4 = malloc(LZ4_sizeofState()); - void* const stateLZ4HC = malloc(LZ4_sizeofStateHC()); + void* const stateLZ4 = malloc((size_t)LZ4_sizeofState()); + void* const stateLZ4HC = malloc((size_t)LZ4_sizeofStateHC()); LZ4_stream_t LZ4dict; LZ4_streamHC_t LZ4dictHC; U32 coreRandState = seed; @@ -370,7 +370,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c const char* dict = block - dictSize; int compressedSize, HCcompressedSize; int blockContinueCompressedSize; - U32 const crcOrig = XXH32(block, blockSize, 0); + U32 const crcOrig = XXH32(block, (size_t)blockSize, 0); int ret; FUZ_displayUpdate(cycleNb); @@ -395,7 +395,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c DISPLAYLEVEL(5, "destSize : %7i/%7i; content%7i/%7i ", ret, targetSize, srcSize, blockSize); if (targetSize>0) { /* check correctness */ - U32 const crcBase = XXH32(block, srcSize, 0); + U32 const crcBase = XXH32(block, (size_t)srcSize, 0); char const canary = FUZ_rand(&randState) & 255; FUZ_CHECKTEST((ret==0), "LZ4_compress_destSize() compression failed"); FUZ_DISPLAYTEST(); @@ -430,7 +430,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c FUZ_CHECKTEST(srcSize > blockSize, "LZ4_compress_HC_destSize() fed more than src buffer !"); if (targetSize>0) { /* check correctness */ - U32 const crcBase = XXH32(block, srcSize, 0); + U32 const crcBase = XXH32(block, (size_t)srcSize, 0); char const canary = FUZ_rand(&randState) & 255; FUZ_CHECKTEST((ret==0), "LZ4_compress_HC_destSize() compression failed"); FUZ_DISPLAYTEST(); @@ -547,7 +547,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c FUZ_CHECKTEST(ret<0, "LZ4_decompress_safe failed despite amply sufficient space"); FUZ_CHECKTEST(ret!=blockSize, "LZ4_decompress_safe did not regenerate original data"); FUZ_CHECKTEST(decodedBuffer[blockSize+1], "LZ4_decompress_safe overrun specified output buffer size"); - { U32 const crcCheck = XXH32(decodedBuffer, blockSize, 0); + { U32 const crcCheck = XXH32(decodedBuffer, (size_t)blockSize, 0); FUZ_CHECKTEST(crcCheck!=crcOrig, "LZ4_decompress_safe corrupted decoded data"); } @@ -582,7 +582,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c /* Test partial decoding => must work */ FUZ_DISPLAYTEST("test LZ4_decompress_safe_partial"); { size_t const missingBytes = FUZ_rand(&randState) % blockSize; - int const targetSize = (int)(blockSize - missingBytes); + int const targetSize = (int)((size_t)blockSize - missingBytes); char const sentinel = decodedBuffer[targetSize] = block[targetSize] ^ 0x5A; int const decResult = LZ4_decompress_safe_partial(compressedBuffer, decodedBuffer, compressedSize, targetSize, blockSize); FUZ_CHECKTEST(decResult<0, "LZ4_decompress_safe_partial failed despite valid input data (error:%i)", decResult); @@ -653,7 +653,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c memcpy(decodedBuffer, dict, dictSize); ret = LZ4_decompress_fast_usingDict(compressedBuffer, decodedBuffer+dictSize, blockSize, decodedBuffer, dictSize); FUZ_CHECKTEST(ret!=blockContinueCompressedSize, "LZ4_decompress_fast_usingDict did not read all compressed block input"); - { U32 const crcCheck = XXH32(decodedBuffer+dictSize, blockSize, 0); + { U32 const crcCheck = XXH32(decodedBuffer+dictSize, (size_t)blockSize, 0); if (crcCheck!=crcOrig) FUZ_findDiff(block, decodedBuffer); FUZ_CHECKTEST(crcCheck!=crcOrig, "LZ4_decompress_fast_usingDict corrupted decoded data (dict %i)", dictSize); } @@ -661,7 +661,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c FUZ_DISPLAYTEST("test LZ4_decompress_safe_usingDict()"); ret = LZ4_decompress_safe_usingDict(compressedBuffer, decodedBuffer+dictSize, blockContinueCompressedSize, blockSize, decodedBuffer, dictSize); FUZ_CHECKTEST(ret!=blockSize, "LZ4_decompress_safe_usingDict did not regenerate original data"); - { U32 const crcCheck = XXH32(decodedBuffer+dictSize, blockSize, 0); + { U32 const crcCheck = XXH32(decodedBuffer+dictSize, (size_t)blockSize, 0); FUZ_CHECKTEST(crcCheck!=crcOrig, "LZ4_decompress_safe_usingDict corrupted decoded data"); } @@ -830,7 +830,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c FUZ_DISPLAYTEST("LZ4_compress_HC_continue with an external dictionary"); dict -= (FUZ_rand(&randState) & 7); /* even bigger separation */ if (dict < (char*)CNBuffer) dict = (char*)CNBuffer; - LZ4_resetStreamHC (&LZ4dictHC, compressionLevel); + LZ4_initStreamHC (&LZ4dictHC, compressionLevel); LZ4_loadDictHC(&LZ4dictHC, dict, dictSize); LZ4_setCompressionLevel(&LZ4dictHC, compressionLevel-1); blockContinueCompressedSize = LZ4_compress_HC_continue(&LZ4dictHC, block, compressedBuffer, blockSize, (int)compressedBufferSize); @@ -855,7 +855,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c ret = LZ4_decompress_safe_usingDict(compressedBuffer, decodedBuffer, blockContinueCompressedSize, blockSize, dict, dictSize); FUZ_CHECKTEST(ret!=blockSize, "LZ4_decompress_safe_usingDict did not regenerate original data"); FUZ_CHECKTEST(decodedBuffer[blockSize], "LZ4_decompress_safe_usingDict overrun specified output buffer size"); - { U32 const crcCheck = XXH32(decodedBuffer, blockSize, 0); + { U32 const crcCheck = XXH32(decodedBuffer, (size_t)blockSize, 0); if (crcCheck!=crcOrig) FUZ_findDiff(block, decodedBuffer); FUZ_CHECKTEST(crcCheck!=crcOrig, "LZ4_decompress_safe_usingDict corrupted decoded data"); } @@ -865,9 +865,9 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c { LZ4_streamHC_t LZ4_streamHC; - LZ4_resetStreamHC (&LZ4dictHC, compressionLevel); + LZ4_initStreamHC (&LZ4dictHC, compressionLevel); LZ4_loadDictHC(&LZ4dictHC, dict, dictSize); - LZ4_resetStreamHC (&LZ4_streamHC, compressionLevel); + LZ4_initStreamHC (&LZ4_streamHC, compressionLevel); LZ4_attach_HC_dictionary(&LZ4_streamHC, &LZ4dictHC); blockContinueCompressedSize = LZ4_compress_HC_continue(&LZ4_streamHC, block, compressedBuffer, blockSize, (int)compressedBufferSize); FUZ_CHECKTEST(blockContinueCompressedSize==0, "LZ4_compress_HC_continue with ExtDictCtx failed"); @@ -901,7 +901,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c ret = LZ4_decompress_safe_usingDict(compressedBuffer, decodedBuffer, blockContinueCompressedSize, blockSize, dict, dictSize); FUZ_CHECKTEST(ret!=blockSize, "LZ4_decompress_safe_usingDict did not regenerate original data"); FUZ_CHECKTEST(decodedBuffer[blockSize], "LZ4_decompress_safe_usingDict overrun specified output buffer size"); - { U32 const crcCheck = XXH32(decodedBuffer, blockSize, 0); + { U32 const crcCheck = XXH32(decodedBuffer, (size_t)blockSize, 0); if (crcCheck!=crcOrig) FUZ_findDiff(block, decodedBuffer); FUZ_CHECKTEST(crcCheck!=crcOrig, "LZ4_decompress_safe_usingDict corrupted decoded data"); } @@ -912,7 +912,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c { int const availableSpace = (FUZ_rand(&randState) % blockSize) + 5; int consumedSize = blockSize; FUZ_DISPLAYTEST(); - LZ4_resetStreamHC (&LZ4dictHC, compressionLevel); + LZ4_initStreamHC (&LZ4dictHC, compressionLevel); LZ4_loadDictHC(&LZ4dictHC, dict, dictSize); blockContinueCompressedSize = LZ4_compress_HC_continue_destSize(&LZ4dictHC, block, compressedBuffer, &consumedSize, availableSpace); DISPLAYLEVEL(5, " LZ4_compress_HC_continue_destSize : compressed %6i/%6i into %6i/%6i at cLevel=%i\n", consumedSize, blockSize, blockContinueCompressedSize, availableSpace, compressionLevel); @@ -925,8 +925,8 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c ret = LZ4_decompress_safe_usingDict(compressedBuffer, decodedBuffer, blockContinueCompressedSize, consumedSize, dict, dictSize); FUZ_CHECKTEST(ret!=consumedSize, "LZ4_decompress_safe_usingDict did not regenerate original data"); FUZ_CHECKTEST(decodedBuffer[consumedSize], "LZ4_decompress_safe_usingDict overrun specified output buffer size") - { U32 const crcSrc = XXH32(block, consumedSize, 0); - U32 const crcDst = XXH32(decodedBuffer, consumedSize, 0); + { U32 const crcSrc = XXH32(block, (size_t)consumedSize, 0); + U32 const crcDst = XXH32(decodedBuffer, (size_t)consumedSize, 0); if (crcSrc!=crcDst) FUZ_findDiff(block, decodedBuffer); FUZ_CHECKTEST(crcSrc!=crcDst, "LZ4_decompress_safe_usingDict corrupted decoded data"); } @@ -1080,7 +1080,7 @@ static void FUZ_unitTests(int compressionLevel) /* simple HC compression test */ DISPLAYLEVEL(3, " Simple HC round-trip : "); { U64 const crc64 = XXH64(testInput, testCompressedSize, 0); - LZ4_resetStreamHC(&sHC, compressionLevel); + LZ4_initStreamHC(&sHC, compressionLevel); result = LZ4_compress_HC_continue(&sHC, testInput, testCompressed, testCompressedSize, testCompressedSize-1); FUZ_CHECKTEST(result==0, "LZ4_compressHC_limitedOutput_continue() compression failed"); FUZ_CHECKTEST(sHC.internal_donotuse.dirty, "Context should be clean"); @@ -1105,7 +1105,7 @@ static void FUZ_unitTests(int compressionLevel) memset(block, 0, blockSize); ((char*)dstBlock)[targetSize] = sentinel; - LZ4_resetStreamHC(&sHC, 3); + LZ4_initStreamHC(&sHC, 3); assert(blockSize < INT_MAX); srcSize = (int)blockSize; assert(targetSize < INT_MAX); @@ -1114,7 +1114,7 @@ static void FUZ_unitTests(int compressionLevel) FUZ_CHECKTEST(result!=4116, "LZ4_compress_HC_destSize() : compression must fill dstBuffer completely, but no more !"); FUZ_CHECKTEST(((char*)dstBlock)[targetSize] != sentinel, "LZ4_compress_HC_destSize()") - LZ4_resetStreamHC(&sHC, 3); /* make sure the context is clean after the test */ + LZ4_resetStreamHC_fast(&sHC, 3); /* make sure the context is clean after the test */ free(block); free(dstBlock); } -- cgit v1.2.3 From c3f8928f879f5ce89d9c32996ea991312e6c69ec Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 5 Apr 2019 10:41:16 -0700 Subject: fixed strict iso C90 --- lib/lz4hc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/lz4hc.c b/lib/lz4hc.c index 644b8eb..9e8b77e 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -911,7 +911,7 @@ void LZ4_initStreamHC (void* state, int compressionLevel) /* just a stub */ void LZ4_resetStreamHC (LZ4_streamHC_t* LZ4_streamHCPtr, int compressionLevel) { - return LZ4_initStreamHC(LZ4_streamHCPtr, compressionLevel); + LZ4_initStreamHC(LZ4_streamHCPtr, compressionLevel); } void LZ4_resetStreamHC_fast (LZ4_streamHC_t* LZ4_streamHCPtr, int compressionLevel) -- cgit v1.2.3 From a4f1635b0a29580628c22fbccfd68bda122453bb Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 5 Apr 2019 11:47:06 -0700 Subject: fuzzer: fixed strict c99 with mmap+MAP_ANONYMOUS --- tests/frametest.c | 9 ++++----- tests/fuzzer.c | 4 +--- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/tests/frametest.c b/tests/frametest.c index 14906a0..fa005db 100644 --- a/tests/frametest.c +++ b/tests/frametest.c @@ -270,7 +270,7 @@ int basicTests(U32 seed, double compressibility) if (decResult != 0) goto _output_error; /* should finish now */ op += oSize; if (op>oend) { DISPLAY("decompression write overflow \n"); goto _output_error; } - { U64 const crcDest = XXH64(decodedBuffer, op-ostart, 1); + { U64 const crcDest = XXH64(decodedBuffer, (size_t)(op-ostart), 1); if (crcDest != crcOrig) goto _output_error; } } @@ -309,7 +309,6 @@ int basicTests(U32 seed, double compressibility) iSize = 15 - iSize; CHECK( LZ4F_getFrameInfo(dCtx, &fi, ip, &iSize) ); DISPLAYLEVEL(3, " correctly decoded \n"); - ip += iSize; } DISPLAYLEVEL(3, "Decode a buggy input : "); @@ -337,7 +336,7 @@ int basicTests(U32 seed, double compressibility) const BYTE* ip = (const BYTE*) compressedBuffer; const BYTE* const iend = ip + cSize; while (ip < iend) { - size_t oSize = oend-op; + size_t oSize = (size_t)(oend-op); size_t iSize = 1; CHECK( LZ4F_decompress(dCtx, op, &oSize, ip, &iSize, NULL) ); op += oSize; @@ -380,8 +379,8 @@ int basicTests(U32 seed, double compressibility) while (ip < iend) { unsigned const nbBits = FUZ_rand(&randState) % maxBits; size_t iSize = (FUZ_rand(&randState) & ((1< (size_t)(iend-ip)) iSize = iend-ip; + size_t oSize = (size_t)(oend-op); + if (iSize > (size_t)(iend-ip)) iSize = (size_t)(iend-ip); CHECK( LZ4F_decompress(dCtx, op, &oSize, ip, &iSize, NULL) ); op += oSize; ip += iSize; diff --git a/tests/fuzzer.c b/tests/fuzzer.c index 6ebbf20..4cc80fa 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -37,6 +37,7 @@ * Dependencies **************************************/ #if defined(__unix__) && !defined(_AIX) /* must be included before platform.h for MAP_ANONYMOUS */ +# define _GNU_SOURCE /* MAP_ANONYMOUS even in -std=c99 mode */ # include /* mmap */ #endif #include "platform.h" /* _CRT_SECURE_NO_WARNINGS */ @@ -47,9 +48,6 @@ #include /* clock_t, clock, CLOCKS_PER_SEC */ #include #include /* INT_MAX */ -#if defined(__unix__) && defined(_AIX) -# include /* mmap */ -#endif #define LZ4_DISABLE_DEPRECATE_WARNINGS /* LZ4_decompress_fast */ #define LZ4_STATIC_LINKING_ONLY -- cgit v1.2.3 From 2a94faf462c53feb864c4ae4e857c4870a948da0 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 5 Apr 2019 11:55:34 -0700 Subject: fixed _GNU_SOURCE duplicate --- tests/fuzzer.c | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/fuzzer.c b/tests/fuzzer.c index 4cc80fa..1efe322 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -37,6 +37,7 @@ * Dependencies **************************************/ #if defined(__unix__) && !defined(_AIX) /* must be included before platform.h for MAP_ANONYMOUS */ +# undef _GNU_SOURCE /* in case it's already defined */ # define _GNU_SOURCE /* MAP_ANONYMOUS even in -std=c99 mode */ # include /* mmap */ #endif -- cgit v1.2.3 From 2ece0d83809849e68e9c194ff3d340982b527256 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 5 Apr 2019 12:54:13 -0700 Subject: created LZ4_initStream() - promoted LZ4_resetStream_fast() to stable - moved LZ4_resetStream() into deprecate, but without triggering a compiler warning - update all sources to no longer rely on LZ4_resetStream() note : LZ4_initStream() proposal is slightly different : it's able to initialize any buffer, provided that it's large enough. To this end, it accepts a void*, and returns an LZ4_stream_t*. --- doc/lz4_manual.html | 87 +++++++++++++++++---------------- examples/blockStreaming_doubleBuffer.c | 2 +- examples/dictionaryRandomAccess.c | 4 +- lib/lz4.c | 17 +++++-- lib/lz4.h | 88 +++++++++++++++++----------------- lib/lz4frame.c | 4 +- lib/lz4hc.h | 4 +- tests/fullbench.c | 4 +- tests/fuzzer.c | 6 +-- 9 files changed, 111 insertions(+), 105 deletions(-) diff --git a/doc/lz4_manual.html b/doc/lz4_manual.html index b738c8d..5db3ec9 100644 --- a/doc/lz4_manual.html +++ b/doc/lz4_manual.html @@ -155,9 +155,23 @@ int LZ4_compress_fast_extState (void* state, const char* src, char* dst, int src

      Streaming Compression Functions

      
       
      -
      void LZ4_resetStream (LZ4_stream_t* streamPtr);
      -

      An LZ4_stream_t structure can be allocated once and re-used multiple times. - Use this function to start compressing a new stream. +

      void LZ4_resetStream_fast (LZ4_stream_t* streamPtr);
      +

      Use this to prepare an LZ4_stream_t for a new chain of dependent blocks + (e.g., LZ4_compress_fast_continue()). + + An LZ4_stream_t must be initialized once. + This is automatically done when created by LZ4_createStream(). + However, should the LZ4_stream_t be simply declared on stack (for example), + it's necessary to initialize first using LZ4_initStream(). + + After that, start any new stream with LZ4_resetStream_fast(). + A same LZ4_stream_t can be re-used multiple times consecutively + and compress multiple streams, + provided that it starts each new stream with LZ4_resetStream_fast(). + + LZ4_resetStream_fast() is much faster than LZ4_initStream(), + but is not compatible with memory regions containing garbage data. + For this reason, LZ4_stream_t must be initialized at least once,


      @@ -290,43 +304,6 @@ int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream); define LZ4_PUBLISH_STATIC_FUNCTIONS when building the LZ4 library.
      -
      LZ4LIB_STATIC_API void LZ4_resetStream_fast (LZ4_stream_t* streamPtr);
      -

      Use this to prepare a context for a new chain of calls to a streaming API - (e.g., LZ4_compress_fast_continue()). - - Note: - To stay on the safe side, when LZ4_stream_t is used for the first time, - it should be either created using LZ4_createStream() or - initialized using LZ4_resetStream(). - - Note: - Using this in advance of a non-streaming-compression function is redundant, - since they all perform their own custom reset internally. - - Differences from LZ4_resetStream(): - When an LZ4_stream_t is known to be in an internally coherent state, - it will be prepared for a new compression with almost no work. - Otherwise, it will fall back to the full, expensive reset. - - LZ4_streams are guaranteed to be in a valid state when: - - returned from LZ4_createStream() - - reset by LZ4_resetStream() - - memset(stream, 0, sizeof(LZ4_stream_t)), though this is discouraged - - the stream was in a valid state and was reset by LZ4_resetStream_fast() - - the stream was in a valid state and was then used in any compression call - that returned success - - the stream was in an indeterminate state and was used in a compression - call that fully reset the state (e.g., LZ4_compress_fast_extState()) and - that returned success - - Note: - A stream that was used in a compression call that did not return success - (e.g., LZ4_compress_fast_continue()), can still be passed to this function, - however, it's history is not preserved because of previous compression - failure. - -


      -
      LZ4LIB_STATIC_API int LZ4_compress_fast_extState_fastReset (void* state, const char* src, char* dst, int srcSize, int dstCapacity, int acceleration);
       

      A variant of LZ4_compress_fast_extState(). @@ -394,10 +371,24 @@ union LZ4_stream_u { LZ4_stream_t_internal internal_donotuse; } ; /* previously typedef'd to LZ4_stream_t */

      information structure to track an LZ4 stream. - init this structure with LZ4_resetStream() before first use. - note : only use in association with static linking ! - this definition is not API/ABI safe, - it may change in a future version ! + LZ4_stream_t can also be created using LZ4_createStream(), which is recommended. + The structure definition can be convenient for static allocation + (on stack, or as part of larger structure). + Init this structure with LZ4_initStream() before first use. + note : only use this definition in association with static linking ! + this definition is not API/ABI safe, and may change in a future version. + +


      + +
      LZ4_stream_t* LZ4_initStream (void* buffer, size_t size);
      +

      An LZ4_stream_t structure must be initialized at least once. + While this is automatically done when invoking LZ4_createStream(), + it's not when the structure is simply declared on stack (for example). + Use this function to properly initialize a newly declared LZ4_stream_t. + It can also accept any arbitrary buffer of sufficient size as input, + and will return a pointer of proper type upon initialization. + Note : initialization can fail if size < sizeof(LZ4_stream_t). + In which case, the function will @return NULL.


      @@ -477,5 +468,13 @@ int LZ4_decompress_fast_usingDict (const char* src, char* dst, int originalSize,


    +
    //LZ4LIB_API void LZ4_resetStream (LZ4_stream_t* streamPtr);
    +

    An LZ4_stream_t structure must be initialized at least once. + This is done with LZ4_initStream(), or LZ4_resetStream(). + Consider switching to LZ4_initStream(), + invoking LZ4_resetStream() will trigger deprecation warnings in the future. + +


    + diff --git a/examples/blockStreaming_doubleBuffer.c b/examples/blockStreaming_doubleBuffer.c index acb3455..3f719d3 100644 --- a/examples/blockStreaming_doubleBuffer.c +++ b/examples/blockStreaming_doubleBuffer.c @@ -44,7 +44,7 @@ void test_compress(FILE* outFp, FILE* inpFp) char inpBuf[2][BLOCK_BYTES]; int inpBufIndex = 0; - LZ4_resetStream(lz4Stream); + LZ4_initStream(lz4Stream, sizeof (*lz4Stream)); for(;;) { char* const inpPtr = inpBuf[inpBufIndex]; diff --git a/examples/dictionaryRandomAccess.c b/examples/dictionaryRandomAccess.c index 291fd08..ecb3b2d 100644 --- a/examples/dictionaryRandomAccess.c +++ b/examples/dictionaryRandomAccess.c @@ -11,7 +11,7 @@ #include #include -#define MIN(x, y) (x) < (y) ? (x) : (y) +#define MIN(x, y) ((x) < (y) ? (x) : (y)) enum { BLOCK_BYTES = 1024, /* 1 KiB of uncompressed data in a block */ @@ -63,7 +63,7 @@ void test_compress(FILE* outFp, FILE* inpFp, void *dict, int dictSize) int *offsetsEnd = offsets; - LZ4_resetStream(lz4Stream); + LZ4_initStream(lz4Stream, sizeof(*lz4Stream)); /* Write header magic */ write_bin(outFp, kTestMagic, sizeof(kTestMagic)); diff --git a/lib/lz4.c b/lib/lz4.c index df92162..e0fcf0f 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1125,9 +1125,8 @@ _failure: int LZ4_compress_fast_extState(void* state, const char* source, char* dest, int inputSize, int maxOutputSize, int acceleration) { - LZ4_stream_t_internal* ctx = &((LZ4_stream_t*)state)->internal_donotuse; + LZ4_stream_t_internal* const ctx = & LZ4_initStream(state, sizeof(LZ4_stream_t)) -> internal_donotuse; if (acceleration < 1) acceleration = ACCELERATION_DEFAULT; - LZ4_resetStream((LZ4_stream_t*)state); if (maxOutputSize >= LZ4_compressBound(inputSize)) { if (inputSize < LZ4_64Klimit) { return LZ4_compress_generic(ctx, source, dest, inputSize, NULL, 0, notLimited, byU16, noDict, noDictIssue, acceleration); @@ -1221,7 +1220,7 @@ int LZ4_compress_default(const char* src, char* dst, int srcSize, int maxOutputS int LZ4_compress_fast_force(const char* src, char* dst, int srcSize, int dstCapacity, int acceleration) { LZ4_stream_t ctx; - LZ4_resetStream(&ctx); + LZ4_initStream(&ctx, sizeof(ctx)); if (srcSize < LZ4_64Klimit) { return LZ4_compress_generic(&ctx.internal_donotuse, src, dst, srcSize, NULL, dstCapacity, limitedOutput, byU16, noDict, noDictIssue, acceleration); @@ -1237,7 +1236,7 @@ int LZ4_compress_fast_force(const char* src, char* dst, int srcSize, int dstCapa * _continue() call without resetting it. */ static int LZ4_compress_destSize_extState (LZ4_stream_t* state, const char* src, char* dst, int* srcSizePtr, int targetDstSize) { - LZ4_resetStream(state); + LZ4_initStream(state, sizeof (*state)); if (targetDstSize >= LZ4_compressBound(*srcSizePtr)) { /* compression success is guaranteed */ return LZ4_compress_fast_extState(state, src, dst, *srcSizePtr, targetDstSize, 1); @@ -1281,10 +1280,18 @@ LZ4_stream_t* LZ4_createStream(void) LZ4_STATIC_ASSERT(LZ4_STREAMSIZE >= sizeof(LZ4_stream_t_internal)); /* A compilation error here means LZ4_STREAMSIZE is not large enough */ DEBUGLOG(4, "LZ4_createStream %p", lz4s); if (lz4s == NULL) return NULL; - LZ4_resetStream(lz4s); + LZ4_initStream(lz4s, sizeof(*lz4s)); return lz4s; } +LZ4_stream_t* LZ4_initStream (void* buffer, size_t size) +{ + DEBUGLOG(5, "LZ4_initStream"); + if (size < sizeof(LZ4_stream_t)) return NULL; + MEM_INIT(buffer, 0, sizeof(LZ4_stream_t)); + return (LZ4_stream_t*)buffer; +} + void LZ4_resetStream (LZ4_stream_t* LZ4_stream) { DEBUGLOG(5, "LZ4_resetStream (ctx:%p)", LZ4_stream); diff --git a/lib/lz4.h b/lib/lz4.h index 1dc8e00..3fc67bb 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -238,11 +238,25 @@ typedef union LZ4_stream_u LZ4_stream_t; /* incomplete type (defined later) */ LZ4LIB_API LZ4_stream_t* LZ4_createStream(void); LZ4LIB_API int LZ4_freeStream (LZ4_stream_t* streamPtr); -/*! LZ4_resetStream() : - * An LZ4_stream_t structure can be allocated once and re-used multiple times. - * Use this function to start compressing a new stream. +/*! LZ4_resetStream_fast() : v1.9.0+ + * Use this to prepare an LZ4_stream_t for a new chain of dependent blocks + * (e.g., LZ4_compress_fast_continue()). + * + * An LZ4_stream_t must be initialized once. + * This is automatically done when created by LZ4_createStream(). + * However, should the LZ4_stream_t be simply declared on stack (for example), + * it's necessary to initialize first using LZ4_initStream(). + * + * After that, start any new stream with LZ4_resetStream_fast(). + * A same LZ4_stream_t can be re-used multiple times consecutively + * and compress multiple streams, + * provided that it starts each new stream with LZ4_resetStream_fast(). + * + * LZ4_resetStream_fast() is much faster than LZ4_initStream(), + * but is not compatible with memory regions containing garbage data. + * For this reason, LZ4_stream_t must be initialized at least once, */ -LZ4LIB_API void LZ4_resetStream (LZ4_stream_t* streamPtr); +LZ4LIB_API void LZ4_resetStream_fast (LZ4_stream_t* streamPtr); /*! LZ4_loadDict() : * Use this function to load a static dictionary into LZ4_stream_t. @@ -394,42 +408,6 @@ LZ4LIB_API int LZ4_decompress_safe_usingDict (const char* src, char* dst, int sr #ifdef LZ4_STATIC_LINKING_ONLY -/*! LZ4_resetStream_fast() : - * Use this to prepare a context for a new chain of calls to a streaming API - * (e.g., LZ4_compress_fast_continue()). - * - * Note: - * To stay on the safe side, when LZ4_stream_t is used for the first time, - * it should be either created using LZ4_createStream() or - * initialized using LZ4_resetStream(). - * - * Note: - * Using this in advance of a non-streaming-compression function is redundant, - * since they all perform their own custom reset internally. - * - * Differences from LZ4_resetStream(): - * When an LZ4_stream_t is known to be in an internally coherent state, - * it will be prepared for a new compression with almost no work. - * Otherwise, it will fall back to the full, expensive reset. - * - * LZ4_streams are guaranteed to be in a valid state when: - * - returned from LZ4_createStream() - * - reset by LZ4_resetStream() - * - memset(stream, 0, sizeof(LZ4_stream_t)), though this is discouraged - * - the stream was in a valid state and was reset by LZ4_resetStream_fast() - * - the stream was in a valid state and was then used in any compression call - * that returned success - * - the stream was in an indeterminate state and was used in a compression - * call that fully reset the state (e.g., LZ4_compress_fast_extState()) and - * that returned success - * - * Note: - * A stream that was used in a compression call that did not return success - * (e.g., LZ4_compress_fast_continue()), can still be passed to this function, - * however, it's history is not preserved because of previous compression - * failure. - */ -LZ4LIB_STATIC_API void LZ4_resetStream_fast (LZ4_stream_t* streamPtr); /*! LZ4_compress_fast_extState_fastReset() : * A variant of LZ4_compress_fast_extState(). @@ -530,10 +508,12 @@ typedef struct { /*! LZ4_stream_t : * information structure to track an LZ4 stream. - * init this structure with LZ4_resetStream() before first use. - * note : only use in association with static linking ! - * this definition is not API/ABI safe, - * it may change in a future version ! + * LZ4_stream_t can also be created using LZ4_createStream(), which is recommended. + * The structure definition can be convenient for static allocation + * (on stack, or as part of larger structure). + * Init this structure with LZ4_initStream() before first use. + * note : only use this definition in association with static linking ! + * this definition is not API/ABI safe, and may change in a future version. */ #define LZ4_STREAMSIZE_U64 ((1 << (LZ4_MEMORY_USAGE-3)) + 4 + ((sizeof(void*)==16) ? 4 : 0) /*AS-400*/ ) #define LZ4_STREAMSIZE (LZ4_STREAMSIZE_U64 * sizeof(unsigned long long)) @@ -542,6 +522,18 @@ union LZ4_stream_u { LZ4_stream_t_internal internal_donotuse; } ; /* previously typedef'd to LZ4_stream_t */ +/*! LZ4_initStream() : + * An LZ4_stream_t structure must be initialized at least once. + * While this is automatically done when invoking LZ4_createStream(), + * it's not when the structure is simply declared on stack (for example). + * Use this function to properly initialize a newly declared LZ4_stream_t. + * It can also accept any arbitrary buffer of sufficient size as input, + * and will return a pointer of proper type upon initialization. + * Note : initialization can fail if size < sizeof(LZ4_stream_t). + * In which case, the function will @return NULL. + */ +LZ4LIB_API LZ4_stream_t* LZ4_initStream (void* buffer, size_t size); + /*! LZ4_streamDecode_t : * information structure to track an LZ4 stream during decompression. @@ -652,6 +644,14 @@ int LZ4_decompress_fast_continue (LZ4_streamDecode_t* LZ4_streamDecode, const ch LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe_usingDict() instead") LZ4LIB_API int LZ4_decompress_fast_usingDict (const char* src, char* dst, int originalSize, const char* dictStart, int dictSize); +/*! LZ4_resetStream() : + * An LZ4_stream_t structure must be initialized at least once. + * This is done with LZ4_initStream(), or LZ4_resetStream(). + * Consider switching to LZ4_initStream(), + * invoking LZ4_resetStream() will trigger deprecation warnings in the future. + */ +LZ4LIB_API void LZ4_resetStream (LZ4_stream_t* streamPtr); + #endif /* LZ4_H_2983827168210 */ diff --git a/lib/lz4frame.c b/lib/lz4frame.c index f076204..3b4fcee 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -446,7 +446,7 @@ size_t LZ4F_compressFrame(void* dstBuffer, size_t dstCapacity, if (preferencesPtr == NULL || preferencesPtr->compressionLevel < LZ4HC_CLEVEL_MIN) { - LZ4_resetStream(&lz4ctx); + LZ4_initStream(&lz4ctx, sizeof(lz4ctx)); cctxPtr->lz4CtxPtr = &lz4ctx; cctxPtr->lz4CtxAlloc = 1; cctxPtr->lz4CtxState = 1; @@ -628,7 +628,7 @@ size_t LZ4F_compressBegin_usingCDict(LZ4F_cctx* cctxPtr, /* otherwise, a sufficient buffer is allocated, but we need to * reset it to the correct context type */ if (cctxPtr->prefs.compressionLevel < LZ4HC_CLEVEL_MIN) { - LZ4_resetStream((LZ4_stream_t *) cctxPtr->lz4CtxPtr); + LZ4_initStream((LZ4_stream_t *) cctxPtr->lz4CtxPtr, sizeof (LZ4_stream_t)); } else { LZ4_initStreamHC((LZ4_streamHC_t *) cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel); } diff --git a/lib/lz4hc.h b/lib/lz4hc.h index 91b14ee..fbf4518 100644 --- a/lib/lz4hc.h +++ b/lib/lz4hc.h @@ -152,7 +152,7 @@ LZ4LIB_API int LZ4_freeStreamHC (LZ4_streamHC_t* streamHCPtr); just by resetting it, using LZ4_resetStreamHC_fast(). */ -LZ4LIB_API void LZ4_resetStreamHC_fast(LZ4_streamHC_t* streamHCPtr, int compressionLevel); +LZ4LIB_API void LZ4_resetStreamHC_fast(LZ4_streamHC_t* streamHCPtr, int compressionLevel); /* v1.9.0+ */ LZ4LIB_API int LZ4_loadDictHC (LZ4_streamHC_t* streamHCPtr, const char* dictionary, int dictSize); LZ4LIB_API int LZ4_compress_HC_continue (LZ4_streamHC_t* streamHCPtr, @@ -265,7 +265,7 @@ union LZ4_streamHC_u { * Static allocation shall only be used in combination with static linking. */ -LZ4LIB_API void LZ4_initStreamHC (void* streamHCPtr, int compressionLevel); +LZ4LIB_API void LZ4_initStreamHC (void* streamHCPtr, int compressionLevel); /* v1.9.0+ */ /*-************************************ diff --git a/tests/fullbench.c b/tests/fullbench.c index faa50cb..61d6b78 100644 --- a/tests/fullbench.c +++ b/tests/fullbench.c @@ -161,12 +161,12 @@ static size_t BMK_findMaxMem(U64 requiredMem) static LZ4_stream_t LZ4_stream; static void local_LZ4_resetDictT(void) { - LZ4_resetStream(&LZ4_stream); + LZ4_initStream(&LZ4_stream, sizeof(LZ4_stream)); } static void local_LZ4_createStream(void) { - LZ4_resetStream(&LZ4_stream); + LZ4_initStream(&LZ4_stream, sizeof(LZ4_stream)); } static int local_LZ4_saveDict(const char* in, char* out, int inSize) diff --git a/tests/fuzzer.c b/tests/fuzzer.c index 1efe322..d899cef 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -641,7 +641,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c /* Compress using dictionary */ FUZ_DISPLAYTEST("test LZ4_compress_fast_continue() with dictionary of size %i", dictSize); { LZ4_stream_t LZ4_stream; - LZ4_resetStream(&LZ4_stream); + LZ4_initStream(&LZ4_stream, sizeof(LZ4_stream)); LZ4_compress_fast_continue (&LZ4_stream, dict, compressedBuffer, dictSize, (int)compressedBufferSize, 1); /* Just to fill hash tables */ blockContinueCompressedSize = LZ4_compress_fast_continue (&LZ4_stream, block, compressedBuffer, blockSize, (int)compressedBufferSize, 1); FUZ_CHECKTEST(blockContinueCompressedSize==0, "LZ4_compress_fast_continue failed"); @@ -742,7 +742,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c FUZ_DISPLAYTEST("LZ4_compress_fast_continue() after LZ4_attach_dictionary()"); LZ4_loadDict(&LZ4dict, dict, dictSize); - LZ4_resetStream(&LZ4_stream); + LZ4_initStream(&LZ4_stream, sizeof(LZ4_stream)); LZ4_attach_dictionary(&LZ4_stream, &LZ4dict); blockContinueCompressedSize = LZ4_compress_fast_continue(&LZ4_stream, block, compressedBuffer, blockSize, (int)compressedBufferSize, 1); FUZ_CHECKTEST(blockContinueCompressedSize==0, "LZ4_compress_fast_continue using extDictCtx failed"); @@ -1000,7 +1000,7 @@ static void FUZ_unitTests(int compressionLevel) /* simple compression test */ crcOrig = XXH64(testInput, testCompressedSize, 0); - LZ4_resetStream(&streamingState); + LZ4_initStream(&streamingState, sizeof(streamingState)); result = LZ4_compress_fast_continue(&streamingState, testInput, testCompressed, testCompressedSize, testCompressedSize-1, 1); FUZ_CHECKTEST(result==0, "LZ4_compress_fast_continue() compression failed!"); FUZ_CHECKTEST(streamingState.internal_donotuse.dirty, "context should be clean") -- cgit v1.2.3 From 34f0004a5ed5ca5a113432bf2e433966d545133e Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 5 Apr 2019 15:35:19 -0700 Subject: added comment on initStream + _extState_ as suggested by @felixhandte --- lib/lz4.h | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/lz4.h b/lib/lz4.h index 3fc67bb..5aa4229 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -242,19 +242,23 @@ LZ4LIB_API int LZ4_freeStream (LZ4_stream_t* streamPtr); * Use this to prepare an LZ4_stream_t for a new chain of dependent blocks * (e.g., LZ4_compress_fast_continue()). * - * An LZ4_stream_t must be initialized once. + * An LZ4_stream_t must be initialized once before usage. * This is automatically done when created by LZ4_createStream(). * However, should the LZ4_stream_t be simply declared on stack (for example), - * it's necessary to initialize first using LZ4_initStream(). + * it's necessary to initialize it first, using LZ4_initStream(). * - * After that, start any new stream with LZ4_resetStream_fast(). + * After init, start any new stream with LZ4_resetStream_fast(). * A same LZ4_stream_t can be re-used multiple times consecutively * and compress multiple streams, * provided that it starts each new stream with LZ4_resetStream_fast(). * * LZ4_resetStream_fast() is much faster than LZ4_initStream(), * but is not compatible with memory regions containing garbage data. - * For this reason, LZ4_stream_t must be initialized at least once, + * + * Note: it's only useful to call LZ4_resetStream_fast() + * in the context of streaming compression. + * The *extState* functions perform their own resets. + * Invoking LZ4_resetStream_fast() before is redundant, and even counterproductive. */ LZ4LIB_API void LZ4_resetStream_fast (LZ4_stream_t* streamPtr); -- cgit v1.2.3 From c198a39a663d5a36d2d564a2a4140485e42404bb Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 8 Apr 2019 12:49:54 -0700 Subject: LZ4_initStream() checks alignment restriction updated associated documentation --- lib/lz4.c | 7 +++++++ lib/lz4.h | 17 ++++++++++------- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index e0fcf0f..a2e58c0 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1284,10 +1284,17 @@ LZ4_stream_t* LZ4_createStream(void) return lz4s; } +static size_t LZ4_stream_t_alignment(void) +{ + struct { char c; LZ4_stream_t t; } t_a; + return sizeof(t_a) - sizeof(t_a.t); +} + LZ4_stream_t* LZ4_initStream (void* buffer, size_t size) { DEBUGLOG(5, "LZ4_initStream"); if (size < sizeof(LZ4_stream_t)) return NULL; + if (((size_t)buffer) & (LZ4_stream_t_alignment() - 1)) return NULL; /* alignment check */ MEM_INIT(buffer, 0, sizeof(LZ4_stream_t)); return (LZ4_stream_t*)buffer; } diff --git a/lib/lz4.h b/lib/lz4.h index 5aa4229..e55e1c6 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -528,13 +528,16 @@ union LZ4_stream_u { /*! LZ4_initStream() : * An LZ4_stream_t structure must be initialized at least once. - * While this is automatically done when invoking LZ4_createStream(), - * it's not when the structure is simply declared on stack (for example). - * Use this function to properly initialize a newly declared LZ4_stream_t. - * It can also accept any arbitrary buffer of sufficient size as input, - * and will return a pointer of proper type upon initialization. - * Note : initialization can fail if size < sizeof(LZ4_stream_t). - * In which case, the function will @return NULL. + * This is automatically done when invoking LZ4_createStream(), + * but it's not when the structure is simply declared on stack (for example). + * + * Use LZ4_initStream() to properly initialize a newly declared LZ4_stream_t. + * It can also initialize any arbitrary buffer of sufficient size, + * and will @return a pointer of proper type upon initialization. + * + * Note : initialization fails if size and alignment conditions are not respected. + * In which case, the function will @return NULL. + * Note2: An LZ4_stream_t structure guarantees correct alignment and size. */ LZ4LIB_API LZ4_stream_t* LZ4_initStream (void* buffer, size_t size); -- cgit v1.2.3 From 5db9a2f8b6476b588bae84205718bdb7f3a1967e Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 8 Apr 2019 13:45:01 -0700 Subject: added make list target to tests --- tests/Makefile | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/Makefile b/tests/Makefile index 7d49b31..bc88ece 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -151,12 +151,18 @@ endif DD:=dd +.PHONY: list +list: + @$(MAKE) -pRrq -f $(lastword $(MAKEFILE_LIST)) : 2>/dev/null | awk -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' | sort | egrep -v -e '^[^[:alnum:]]' -e '^$@$$' | xargs +.PHONY: test test: test-lz4 test-lz4c test-frametest test-fullbench test-fuzzer test-install test-amalgamation +.PHONY: test32 test32: CFLAGS+=-m32 test32: test +.PHONY: test-amalgamation test-amalgamation: $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c cat $(LZ4DIR)/lz4.c > lz4_all.c cat $(LZ4DIR)/lz4hc.c >> lz4_all.c -- cgit v1.2.3 From 249703ae2e4e6cce5fc098b3e1443deb6ad42e29 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 8 Apr 2019 13:49:26 -0700 Subject: slightly faster huge file test level down 5->4 size down 6G->5G --- tests/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Makefile b/tests/Makefile index bc88ece..5495f48 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -327,7 +327,7 @@ test-lz4-dict: lz4 datagen test-lz4-hugefile: lz4 datagen @echo "\n ---- test huge files compression/decompression ----" ./datagen -g6GB | $(LZ4) -vB5D | $(LZ4) -qt - ./datagen -g6GB | $(LZ4) -v5BD | $(LZ4) -qt + ./datagen -g5GB | $(LZ4) -v4BD | $(LZ4) -qt # test large file size [2-4] GB @./datagen -g3G -P100 | $(LZ4) -vv | $(LZ4) --decompress --force --sparse - tmphf1 @ls -ls tmphf1 -- cgit v1.2.3 From 3f5f10c63e257c2e4b056f9a69d068c47f1c85f0 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 8 Apr 2019 14:01:32 -0700 Subject: fixed cleaning tmp directory --- tests/Makefile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Makefile b/tests/Makefile index 5495f48..792c22c 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -115,15 +115,15 @@ checkFrame : lz4frame.o lz4.o lz4hc.o xxhash.o checkFrame.c clean: @$(MAKE) -C $(LZ4DIR) $@ > $(VOID) @$(MAKE) -C $(PRGDIR) $@ > $(VOID) - @$(RM) core *.o *.test tmp* \ + @$(RM) -rf core *.o *.test tmp* \ fullbench-dll$(EXT) fullbench-lib$(EXT) \ fullbench$(EXT) fullbench32$(EXT) \ fuzzer$(EXT) fuzzer32$(EXT) \ frametest$(EXT) frametest32$(EXT) \ fasttest$(EXT) roundTripTest$(EXT) \ datagen$(EXT) checkTag$(EXT) \ - frameTest$(EXT) - @rm -fR $(TESTDIR) + frameTest$(EXT) + @$(RM) -rf $(TESTDIR) @echo Cleaning completed .PHONY: versionsTest -- cgit v1.2.3 From b92681f3c0cf0b054e6f235f338ee2dcb92ec37b Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 8 Apr 2019 14:10:59 -0700 Subject: fullbench: assert lz4_stream_t initialization --- tests/fullbench.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/fullbench.c b/tests/fullbench.c index 61d6b78..34c9b7b 100644 --- a/tests/fullbench.c +++ b/tests/fullbench.c @@ -161,12 +161,14 @@ static size_t BMK_findMaxMem(U64 requiredMem) static LZ4_stream_t LZ4_stream; static void local_LZ4_resetDictT(void) { - LZ4_initStream(&LZ4_stream, sizeof(LZ4_stream)); + void* const r = LZ4_initStream(&LZ4_stream, sizeof(LZ4_stream)); + assert(r != NULL); } static void local_LZ4_createStream(void) { - LZ4_initStream(&LZ4_stream, sizeof(LZ4_stream)); + void* const r = LZ4_initStream(&LZ4_stream, sizeof(LZ4_stream)); + assert(r != NULL); } static int local_LZ4_saveDict(const char* in, char* out, int inSize) -- cgit v1.2.3 From da19cc79da6eb7aba915e6deae57ca5c7ac6930f Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 8 Apr 2019 14:21:09 -0700 Subject: checkFrame: removed %zu not liked by mingw --- tests/checkFrame.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/tests/checkFrame.c b/tests/checkFrame.c index f7e7a50..50c0405 100644 --- a/tests/checkFrame.c +++ b/tests/checkFrame.c @@ -1,6 +1,6 @@ /* checkFrame - verify frame headers - Copyright (C) Yann Collet 2014-2016 + Copyright (C) Yann Collet 2014-present GPL v2 License @@ -159,8 +159,12 @@ int frameCheck(cRess_t ress, FILE* const srcFile, unsigned bsid, size_t blockSiz curblocksize = 0; remaining = readSize - pos; nextToLoad = LZ4F_getFrameInfo(ress.ctx, &frameInfo, (char*)(ress.srcBuffer)+pos, &remaining); - if (LZ4F_isError(nextToLoad)) EXM_THROW(22, "Error getting frame info: %s", LZ4F_getErrorName(nextToLoad)); /* XXX */ - if (frameInfo.blockSizeID != bsid) EXM_THROW(23, "Block size ID %u != expected %u", frameInfo.blockSizeID, bsid); + if (LZ4F_isError(nextToLoad)) + EXM_THROW(22, "Error getting frame info: %s", + LZ4F_getErrorName(nextToLoad)); + if (frameInfo.blockSizeID != bsid) + EXM_THROW(23, "Block size ID %u != expected %u", + frameInfo.blockSizeID, bsid); pos += remaining; /* nextToLoad should be block header size */ remaining = nextToLoad; @@ -189,7 +193,8 @@ int frameCheck(cRess_t ress, FILE* const srcFile, unsigned bsid, size_t blockSiz /* detect small block due to end of frame; the final 4-byte frame checksum could be left in the buffer */ if ((curblocksize != 0) && (nextToLoad > 4)) { if (curblocksize != blockSize) - EXM_THROW(25, "Block size %zu != expected %zu, pos %zu\n", curblocksize, blockSize, pos); + EXM_THROW(25, "Block size %u != expected %u, pos %u\n", + (unsigned)curblocksize, (unsigned)blockSize, (unsigned)pos); } curblocksize = 0; } @@ -220,7 +225,7 @@ int FUZ_usage(const char* programName) int main(int argc, const char** argv) { int argNb; - int bsid=0; + unsigned bsid=0; size_t blockSize=0; const char* const programName = argv[0]; @@ -262,7 +267,7 @@ int main(int argc, const char** argv) bsid=0; while ((*argument>='0') && (*argument<='9')) { bsid *= 10; - bsid += *argument - '0'; + bsid += (unsigned)(*argument - '0'); argument++; } break; @@ -272,7 +277,7 @@ int main(int argc, const char** argv) blockSize=0; while ((*argument>='0') && (*argument<='9')) { blockSize *= 10; - blockSize += *argument - '0'; + blockSize += (size_t)(*argument - '0'); argument++; } break; -- cgit v1.2.3 From 111df0fa45729538cc8ec526c71e854d0f15dc2e Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 8 Apr 2019 16:47:21 -0700 Subject: removed LZ4_stream_t alignment test on Visual it fails on x86 32-bit mode : Visual reports an alignment of 8-bytes (even with alignof()) but actually only align LZ4_stream_t on 4 bytes. The alignment check then fails, resulting in missed initialization. --- lib/lz4.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/lz4.c b/lib/lz4.c index a2e58c0..04a30e7 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1284,17 +1284,25 @@ LZ4_stream_t* LZ4_createStream(void) return lz4s; } +#ifndef _MSC_VER /* for some reason, Visual fails the aligment test on 32-bit x86 : + it reports an aligment of 8-bytes, + while actually aligning LZ4_stream_t on 4 bytes. */ static size_t LZ4_stream_t_alignment(void) { struct { char c; LZ4_stream_t t; } t_a; return sizeof(t_a) - sizeof(t_a.t); } +#endif LZ4_stream_t* LZ4_initStream (void* buffer, size_t size) { DEBUGLOG(5, "LZ4_initStream"); if (size < sizeof(LZ4_stream_t)) return NULL; +#ifndef _MSC_VER /* for some reason, Visual fails the aligment test on 32-bit x86 : + it reports an aligment of 8-bytes, + while actually aligning LZ4_stream_t on 4 bytes. */ if (((size_t)buffer) & (LZ4_stream_t_alignment() - 1)) return NULL; /* alignment check */ +#endif MEM_INIT(buffer, 0, sizeof(LZ4_stream_t)); return (LZ4_stream_t*)buffer; } -- cgit v1.2.3 From 5ef4f3ce913dae19f6bcd065a2b231c5461e4233 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 8 Apr 2019 16:51:22 -0700 Subject: check some more initialization result ensure it's not NULL. --- lib/lz4.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/lz4.c b/lib/lz4.c index 04a30e7..5fbc7c7 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1126,6 +1126,7 @@ _failure: int LZ4_compress_fast_extState(void* state, const char* source, char* dest, int inputSize, int maxOutputSize, int acceleration) { LZ4_stream_t_internal* const ctx = & LZ4_initStream(state, sizeof(LZ4_stream_t)) -> internal_donotuse; + assert(ctx != NULL); if (acceleration < 1) acceleration = ACCELERATION_DEFAULT; if (maxOutputSize >= LZ4_compressBound(inputSize)) { if (inputSize < LZ4_64Klimit) { @@ -1236,7 +1237,8 @@ int LZ4_compress_fast_force(const char* src, char* dst, int srcSize, int dstCapa * _continue() call without resetting it. */ static int LZ4_compress_destSize_extState (LZ4_stream_t* state, const char* src, char* dst, int* srcSizePtr, int targetDstSize) { - LZ4_initStream(state, sizeof (*state)); + void* const s = LZ4_initStream(state, sizeof (*state)); + assert(s != NULL); (void)s; if (targetDstSize >= LZ4_compressBound(*srcSizePtr)) { /* compression success is guaranteed */ return LZ4_compress_fast_extState(state, src, dst, *srcSizePtr, targetDstSize, 1); @@ -1307,6 +1309,8 @@ LZ4_stream_t* LZ4_initStream (void* buffer, size_t size) return (LZ4_stream_t*)buffer; } +/* resetStream is now deprecated, + * prefer initStream() which is more general */ void LZ4_resetStream (LZ4_stream_t* LZ4_stream) { DEBUGLOG(5, "LZ4_resetStream (ctx:%p)", LZ4_stream); -- cgit v1.2.3 From 14c71dfa9cc7161e1283e27bc688fd2bbeb637a2 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 9 Apr 2019 13:55:42 -0700 Subject: modified LZ4_initStreamHC() to look like LZ4_initStream() it is now a pure initializer, for statically allocated states. It can initialize any memory area, and because of this, requires size. --- doc/lz4_manual.html | 31 +++++++++------ lib/lz4.c | 1 + lib/lz4frame.c | 3 +- lib/lz4hc.c | 109 +++++++++++++++++++++++++++++++++++----------------- lib/lz4hc.h | 6 +-- tests/fullbench.c | 2 +- tests/fuzzer.c | 39 ++++++++++--------- 7 files changed, 119 insertions(+), 72 deletions(-) diff --git a/doc/lz4_manual.html b/doc/lz4_manual.html index 5db3ec9..1c6dba7 100644 --- a/doc/lz4_manual.html +++ b/doc/lz4_manual.html @@ -159,19 +159,23 @@ int LZ4_compress_fast_extState (void* state, const char* src, char* dst, int src

    Use this to prepare an LZ4_stream_t for a new chain of dependent blocks (e.g., LZ4_compress_fast_continue()). - An LZ4_stream_t must be initialized once. + An LZ4_stream_t must be initialized once before usage. This is automatically done when created by LZ4_createStream(). However, should the LZ4_stream_t be simply declared on stack (for example), - it's necessary to initialize first using LZ4_initStream(). + it's necessary to initialize it first, using LZ4_initStream(). - After that, start any new stream with LZ4_resetStream_fast(). + After init, start any new stream with LZ4_resetStream_fast(). A same LZ4_stream_t can be re-used multiple times consecutively and compress multiple streams, provided that it starts each new stream with LZ4_resetStream_fast(). LZ4_resetStream_fast() is much faster than LZ4_initStream(), but is not compatible with memory regions containing garbage data. - For this reason, LZ4_stream_t must be initialized at least once, + + Note: it's only useful to call LZ4_resetStream_fast() + in the context of streaming compression. + The *extState* functions perform their own resets. + Invoking LZ4_resetStream_fast() before is redundant, and even counterproductive.


    @@ -382,13 +386,16 @@ union LZ4_stream_u {
    LZ4_stream_t* LZ4_initStream (void* buffer, size_t size);
     

    An LZ4_stream_t structure must be initialized at least once. - While this is automatically done when invoking LZ4_createStream(), - it's not when the structure is simply declared on stack (for example). - Use this function to properly initialize a newly declared LZ4_stream_t. - It can also accept any arbitrary buffer of sufficient size as input, - and will return a pointer of proper type upon initialization. - Note : initialization can fail if size < sizeof(LZ4_stream_t). - In which case, the function will @return NULL. + This is automatically done when invoking LZ4_createStream(), + but it's not when the structure is simply declared on stack (for example). + + Use LZ4_initStream() to properly initialize a newly declared LZ4_stream_t. + It can also initialize any arbitrary buffer of sufficient size, + and will @return a pointer of proper type upon initialization. + + Note : initialization fails if size and alignment conditions are not respected. + In which case, the function will @return NULL. + Note2: An LZ4_stream_t structure guarantees correct alignment and size.


    @@ -468,7 +475,7 @@ int LZ4_decompress_fast_usingDict (const char* src, char* dst, int originalSize,


    -
    //LZ4LIB_API void LZ4_resetStream (LZ4_stream_t* streamPtr);
    +
    void LZ4_resetStream (LZ4_stream_t* streamPtr);
     

    An LZ4_stream_t structure must be initialized at least once. This is done with LZ4_initStream(), or LZ4_resetStream(). Consider switching to LZ4_initStream(), diff --git a/lib/lz4.c b/lib/lz4.c index 5fbc7c7..34587ce 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1299,6 +1299,7 @@ static size_t LZ4_stream_t_alignment(void) LZ4_stream_t* LZ4_initStream (void* buffer, size_t size) { DEBUGLOG(5, "LZ4_initStream"); + if (buffer == NULL) return NULL; if (size < sizeof(LZ4_stream_t)) return NULL; #ifndef _MSC_VER /* for some reason, Visual fails the aligment test on 32-bit x86 : it reports an aligment of 8-bytes, diff --git a/lib/lz4frame.c b/lib/lz4frame.c index 3b4fcee..efe38da 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -630,7 +630,8 @@ size_t LZ4F_compressBegin_usingCDict(LZ4F_cctx* cctxPtr, if (cctxPtr->prefs.compressionLevel < LZ4HC_CLEVEL_MIN) { LZ4_initStream((LZ4_stream_t *) cctxPtr->lz4CtxPtr, sizeof (LZ4_stream_t)); } else { - LZ4_initStreamHC((LZ4_streamHC_t *) cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel); + LZ4_initStreamHC((LZ4_streamHC_t *) cctxPtr->lz4CtxPtr, sizeof(LZ4_streamHC_t)); + LZ4_setCompressionLevel((LZ4_streamHC_t *) cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel); } cctxPtr->lz4CtxState = ctxTypeID; } diff --git a/lib/lz4hc.c b/lib/lz4hc.c index 9e8b77e..693337a 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -103,7 +103,7 @@ static void LZ4HC_clearTables (LZ4HC_CCtx_internal* hc4) MEM_INIT(hc4->chainTable, 0xFF, sizeof(hc4->chainTable)); } -static void LZ4HC_init (LZ4HC_CCtx_internal* hc4, const BYTE* start) +static void LZ4HC_init_internal (LZ4HC_CCtx_internal* hc4, const BYTE* start) { uptrval startingOffset = (uptrval)(hc4->end - hc4->base); if (startingOffset > 1 GB) { @@ -239,7 +239,7 @@ LZ4HC_InsertAndGetWiderMatch ( const BYTE* const dictBase = hc4->dictBase; int const lookBackLength = (int)(ip-iLowLimit); int nbAttempts = maxNbAttempts; - int matchChainPos = 0; + U32 matchChainPos = 0; U32 const pattern = LZ4_read32(ip); U32 matchIndex; repeat_state_e repeat = rep_untested; @@ -300,7 +300,7 @@ LZ4HC_InsertAndGetWiderMatch ( U32 const candidateDist = DELTANEXTU16(chainTable, matchIndex + (U32)pos); if (candidateDist > distanceToNextMatch) { distanceToNextMatch = candidateDist; - matchChainPos = pos; + matchChainPos = (U32)pos; } } if (distanceToNextMatch > 1) { if (distanceToNextMatch > matchIndex) break; /* avoid overflow */ @@ -793,7 +793,7 @@ LZ4HC_compress_generic_dictCtx ( limitedOutput_directive limit ) { - const size_t position = ctx->end - ctx->base - ctx->lowLimit; + const size_t position = (size_t)(ctx->end - ctx->base) - ctx->lowLimit; assert(ctx->dictCtx != NULL); if (position >= 64 KB) { ctx->dictCtx = NULL; @@ -827,14 +827,31 @@ LZ4HC_compress_generic ( } -int LZ4_sizeofStateHC(void) { return sizeof(LZ4_streamHC_t); } +int LZ4_sizeofStateHC(void) { return (int)sizeof(LZ4_streamHC_t); } +#ifndef _MSC_VER /* for some reason, Visual fails the aligment test on 32-bit x86 : + * it reports an aligment of 8-bytes, + * while actually aligning LZ4_streamHC_t on 4 bytes. */ +static size_t LZ4_streamHC_t_alignment(void) +{ + struct { char c; LZ4_streamHC_t t; } t_a; + return sizeof(t_a) - sizeof(t_a.t); +} +#endif + +/* state is presumed correctly initialized, + * in which case its size and alignment have already been validate */ int LZ4_compress_HC_extStateHC_fastReset (void* state, const char* src, char* dst, int srcSize, int dstCapacity, int compressionLevel) { LZ4HC_CCtx_internal* const ctx = &((LZ4_streamHC_t*)state)->internal_donotuse; +#ifndef _MSC_VER /* for some reason, Visual fails the aligment test on 32-bit x86 : + * it reports an aligment of 8-bytes, + * while actually aligning LZ4_streamHC_t on 4 bytes. */ + assert(((size_t)state & (LZ4_streamHC_t_alignment() - 1)) == 0); /* check alignment */ +#endif if (((size_t)(state)&(sizeof(void*)-1)) != 0) return 0; /* Error : state is not aligned for pointers (32 or 64 bits) */ LZ4_resetStreamHC_fast((LZ4_streamHC_t*)state, compressionLevel); - LZ4HC_init (ctx, (const BYTE*)src); + LZ4HC_init_internal (ctx, (const BYTE*)src); if (dstCapacity < LZ4_compressBound(srcSize)) return LZ4HC_compress_generic (ctx, src, dst, &srcSize, dstCapacity, compressionLevel, limitedOutput); else @@ -843,8 +860,8 @@ int LZ4_compress_HC_extStateHC_fastReset (void* state, const char* src, char* ds int LZ4_compress_HC_extStateHC (void* state, const char* src, char* dst, int srcSize, int dstCapacity, int compressionLevel) { - if (((size_t)(state)&(sizeof(void*)-1)) != 0) return 0; /* Error : state is not aligned for pointers (32 or 64 bits) */ - LZ4_initStreamHC (state, compressionLevel); /* full initialization, as there is no guarantee on state's content (could be freshly malloc'ed) */ + LZ4_streamHC_t* const ctx = LZ4_initStreamHC(state, sizeof(*ctx)); + if (ctx==NULL) return 0; /* init failure */ return LZ4_compress_HC_extStateHC_fastReset(state, src, dst, srcSize, dstCapacity, compressionLevel); } @@ -863,12 +880,14 @@ int LZ4_compress_HC(const char* src, char* dst, int srcSize, int dstCapacity, in return cSize; } -int LZ4_compress_HC_destSize(void* LZ4HC_Data, const char* source, char* dest, int* sourceSizePtr, int targetDestSize, int cLevel) +/* state is presumed sized correctly (>= sizeof(LZ4_streamHC_t)) */ +int LZ4_compress_HC_destSize(void* state, const char* source, char* dest, int* sourceSizePtr, int targetDestSize, int cLevel) { - LZ4HC_CCtx_internal* const ctx = &((LZ4_streamHC_t*)LZ4HC_Data)->internal_donotuse; - LZ4_initStreamHC(LZ4HC_Data, cLevel); /* full initialization, as there is no guarantee on state's content (could be freshly malloc'ed) */ - LZ4HC_init(ctx, (const BYTE*) source); - return LZ4HC_compress_generic(ctx, source, dest, sourceSizePtr, targetDestSize, cLevel, limitedDestSize); + LZ4_streamHC_t* const ctx = LZ4_initStreamHC(state, sizeof(*ctx)); + if (ctx==NULL) return 0; /* init failure */ + LZ4HC_init_internal(&ctx->internal_donotuse, (const BYTE*) source); + LZ4_setCompressionLevel(ctx, cLevel); + return LZ4HC_compress_generic(&ctx->internal_donotuse, source, dest, sourceSizePtr, targetDestSize, cLevel, limitedDestSize); } @@ -881,7 +900,7 @@ LZ4_streamHC_t* LZ4_createStreamHC(void) { LZ4_streamHC_t* const LZ4_streamHCPtr = (LZ4_streamHC_t*)ALLOC(sizeof(LZ4_streamHC_t)); if (LZ4_streamHCPtr==NULL) return NULL; - LZ4_initStreamHC(LZ4_streamHCPtr, LZ4HC_CLEVEL_DEFAULT); /* full initialization, malloc'ed buffer can be full of garbage */ + LZ4_initStreamHC(LZ4_streamHCPtr, sizeof(*LZ4_streamHCPtr)); /* full initialization, malloc'ed buffer can be full of garbage */ return LZ4_streamHCPtr; } @@ -894,37 +913,48 @@ int LZ4_freeStreamHC (LZ4_streamHC_t* LZ4_streamHCPtr) } -/* initialization */ -void LZ4_initStreamHC (void* state, int compressionLevel) +LZ4_streamHC_t* LZ4_initStreamHC (void* buffer, size_t size) { - LZ4_streamHC_t* const LZ4_streamHCPtr = (LZ4_streamHC_t*)state; - LZ4_STATIC_ASSERT(sizeof(LZ4HC_CCtx_internal) <= LZ4_STREAMHCSIZE); /* if compilation fails here, LZ4_STREAMHCSIZE must be increased */ + LZ4_streamHC_t* const LZ4_streamHCPtr = (LZ4_streamHC_t*)buffer; + if (buffer == NULL) return NULL; + if (size < sizeof(LZ4_streamHC_t)) return NULL; +#ifndef _MSC_VER /* for some reason, Visual fails the aligment test on 32-bit x86 : + * it reports an aligment of 8-bytes, + * while actually aligning LZ4_streamHC_t on 4 bytes. */ + if (((size_t)buffer) & (LZ4_streamHC_t_alignment() - 1)) return NULL; /* alignment check */ +#endif + /* if compilation fails here, LZ4_STREAMHCSIZE must be increased */ + LZ4_STATIC_ASSERT(sizeof(LZ4HC_CCtx_internal) <= LZ4_STREAMHCSIZE); DEBUGLOG(4, "LZ4_resetStreamHC(%p, %d)", LZ4_streamHCPtr, compressionLevel); + /* end-base will trigger a clearTable on starting compression */ LZ4_streamHCPtr->internal_donotuse.end = (const BYTE *)(ptrdiff_t)-1; LZ4_streamHCPtr->internal_donotuse.base = NULL; LZ4_streamHCPtr->internal_donotuse.dictCtx = NULL; LZ4_streamHCPtr->internal_donotuse.favorDecSpeed = 0; LZ4_streamHCPtr->internal_donotuse.dirty = 0; - LZ4_setCompressionLevel(LZ4_streamHCPtr, compressionLevel); + LZ4_setCompressionLevel(LZ4_streamHCPtr, LZ4HC_CLEVEL_DEFAULT); + return LZ4_streamHCPtr; } /* just a stub */ void LZ4_resetStreamHC (LZ4_streamHC_t* LZ4_streamHCPtr, int compressionLevel) { - LZ4_initStreamHC(LZ4_streamHCPtr, compressionLevel); + LZ4_initStreamHC(LZ4_streamHCPtr, sizeof(*LZ4_streamHCPtr)); + LZ4_setCompressionLevel(LZ4_streamHCPtr, compressionLevel); } void LZ4_resetStreamHC_fast (LZ4_streamHC_t* LZ4_streamHCPtr, int compressionLevel) { DEBUGLOG(4, "LZ4_resetStreamHC_fast(%p, %d)", LZ4_streamHCPtr, compressionLevel); if (LZ4_streamHCPtr->internal_donotuse.dirty) { - LZ4_initStreamHC(LZ4_streamHCPtr, compressionLevel); + LZ4_initStreamHC(LZ4_streamHCPtr, sizeof(*LZ4_streamHCPtr)); } else { + /* preserve end - base : can trigger clearTable's threshold */ LZ4_streamHCPtr->internal_donotuse.end -= (uptrval)LZ4_streamHCPtr->internal_donotuse.base; LZ4_streamHCPtr->internal_donotuse.base = NULL; LZ4_streamHCPtr->internal_donotuse.dictCtx = NULL; - LZ4_setCompressionLevel(LZ4_streamHCPtr, compressionLevel); } + LZ4_setCompressionLevel(LZ4_streamHCPtr, compressionLevel); } void LZ4_setCompressionLevel(LZ4_streamHC_t* LZ4_streamHCPtr, int compressionLevel) @@ -939,16 +969,19 @@ void LZ4_favorDecompressionSpeed(LZ4_streamHC_t* LZ4_streamHCPtr, int favor) LZ4_streamHCPtr->internal_donotuse.favorDecSpeed = (favor!=0); } +/* LZ4_loadDictHC() : + * LZ4_streamHCPtr is presumed properly initialized */ int LZ4_loadDictHC (LZ4_streamHC_t* LZ4_streamHCPtr, const char* dictionary, int dictSize) { LZ4HC_CCtx_internal* const ctxPtr = &LZ4_streamHCPtr->internal_donotuse; DEBUGLOG(4, "LZ4_loadDictHC(%p, %p, %d)", LZ4_streamHCPtr, dictionary, dictSize); + assert(LZ4_streamHCPtr != NULL); if (dictSize > 64 KB) { dictionary += dictSize - 64 KB; dictSize = 64 KB; } - LZ4_initStreamHC(LZ4_streamHCPtr, ctxPtr->compressionLevel); - LZ4HC_init (ctxPtr, (const BYTE*)dictionary); + LZ4_resetStreamHC_fast(LZ4_streamHCPtr, ctxPtr->compressionLevel); + LZ4HC_init_internal (ctxPtr, (const BYTE*)dictionary); ctxPtr->end = (const BYTE*)dictionary + dictSize; if (dictSize >= 4) LZ4HC_Insert (ctxPtr, ctxPtr->end-3); return dictSize; @@ -982,8 +1015,9 @@ static int LZ4_compressHC_continue_generic (LZ4_streamHC_t* LZ4_streamHCPtr, { LZ4HC_CCtx_internal* const ctxPtr = &LZ4_streamHCPtr->internal_donotuse; DEBUGLOG(4, "LZ4_compressHC_continue_generic(%p, %p, %d)", LZ4_streamHCPtr, src, *srcSizePtr); + assert(ctxPtr != NULL); /* auto-init if forgotten */ - if (ctxPtr->base == NULL) LZ4HC_init (ctxPtr, (const BYTE*) src); + if (ctxPtr->base == NULL) LZ4HC_init_internal (ctxPtr, (const BYTE*) src); /* Check overflow */ if ((size_t)(ctxPtr->end - ctxPtr->base) > 2 GB) { @@ -1046,11 +1080,13 @@ int LZ4_saveDictHC (LZ4_streamHC_t* LZ4_streamHCPtr, char* safeBuffer, int dictS } -/*********************************** +/*************************************************** * Deprecated Functions -***********************************/ +***************************************************/ + /* These functions currently generate deprecation warnings */ -/* Deprecated compression functions */ + +/* Wrappers for deprecated compression functions */ int LZ4_compressHC(const char* src, char* dst, int srcSize) { return LZ4_compress_HC (src, dst, srcSize, LZ4_compressBound(srcSize), 0); } int LZ4_compressHC_limitedOutput(const char* src, char* dst, int srcSize, int maxDstSize) { return LZ4_compress_HC(src, dst, srcSize, maxDstSize, 0); } int LZ4_compressHC2(const char* src, char* dst, int srcSize, int cLevel) { return LZ4_compress_HC (src, dst, srcSize, LZ4_compressBound(srcSize), cLevel); } @@ -1066,25 +1102,26 @@ int LZ4_compressHC_limitedOutput_continue (LZ4_streamHC_t* ctx, const char* src, /* Deprecated streaming functions */ int LZ4_sizeofStreamStateHC(void) { return LZ4_STREAMHCSIZE; } +/* state is presumed correctly sized, aka >= sizeof(LZ4_streamHC_t) + * @return : 0 on success, !=0 if error */ int LZ4_resetStreamStateHC(void* state, char* inputBuffer) { - LZ4HC_CCtx_internal *ctx = &((LZ4_streamHC_t*)state)->internal_donotuse; - if ((((size_t)state) & (sizeof(void*)-1)) != 0) return 1; /* Error : pointer is not aligned for pointer (32 or 64 bits) */ - LZ4_initStreamHC((LZ4_streamHC_t*)state, ((LZ4_streamHC_t*)state)->internal_donotuse.compressionLevel); - LZ4HC_init(ctx, (const BYTE*)inputBuffer); + LZ4_streamHC_t* const hc4 = LZ4_initStreamHC(state, sizeof(*hc4)); + if (hc4 == NULL) return 1; /* init failed */ + LZ4HC_init_internal (&hc4->internal_donotuse, (const BYTE*)inputBuffer); return 0; } void* LZ4_createHC (const char* inputBuffer) { - LZ4_streamHC_t* hc4 = (LZ4_streamHC_t*)ALLOC(sizeof(LZ4_streamHC_t)); + LZ4_streamHC_t* const hc4 = LZ4_createStreamHC(); if (hc4 == NULL) return NULL; /* not enough memory */ - LZ4_initStreamHC(hc4, 0 /* compressionLevel */); - LZ4HC_init (&hc4->internal_donotuse, (const BYTE*)inputBuffer); + LZ4HC_init_internal (&hc4->internal_donotuse, (const BYTE*)inputBuffer); return hc4; } -int LZ4_freeHC (void* LZ4HC_Data) { +int LZ4_freeHC (void* LZ4HC_Data) +{ if (!LZ4HC_Data) return 0; /* support free on NULL */ FREEMEM(LZ4HC_Data); return 0; diff --git a/lib/lz4hc.h b/lib/lz4hc.h index fbf4518..445c126 100644 --- a/lib/lz4hc.h +++ b/lib/lz4hc.h @@ -265,7 +265,7 @@ union LZ4_streamHC_u { * Static allocation shall only be used in combination with static linking. */ -LZ4LIB_API void LZ4_initStreamHC (void* streamHCPtr, int compressionLevel); /* v1.9.0+ */ +LZ4LIB_API LZ4_streamHC_t* LZ4_initStreamHC (void* buffer, size_t size); /*-************************************ @@ -299,7 +299,7 @@ LZ4_DEPRECATED("use LZ4_freeStreamHC() instead") LZ4LIB_API int LZ4_freeHC ( LZ4_DEPRECATED("use LZ4_compress_HC_continue() instead") LZ4LIB_API int LZ4_compressHC2_continue (void* LZ4HC_Data, const char* source, char* dest, int inputSize, int compressionLevel); LZ4_DEPRECATED("use LZ4_compress_HC_continue() instead") LZ4LIB_API int LZ4_compressHC2_limitedOutput_continue (void* LZ4HC_Data, const char* source, char* dest, int inputSize, int maxOutputSize, int compressionLevel); LZ4_DEPRECATED("use LZ4_createStreamHC() instead") LZ4LIB_API int LZ4_sizeofStreamStateHC(void); -LZ4_DEPRECATED("use LZ4_resetStreamHC() instead") LZ4LIB_API int LZ4_resetStreamStateHC(void* state, char* inputBuffer); +LZ4_DEPRECATED("use LZ4_initStreamHC() instead") LZ4LIB_API int LZ4_resetStreamStateHC(void* state, char* inputBuffer); /* LZ4_resetStreamHC() is now replaced by LZ4_initStreamHC(). @@ -310,7 +310,7 @@ LZ4_DEPRECATED("use LZ4_resetStreamHC() instead") LZ4LIB_API int LZ4_resetStr * It is recommended to switch to LZ4_initStreamHC(). * LZ4_resetStreamHC() will generate deprecation warnings in a future version. */ -LZ4LIB_API void LZ4_resetStreamHC (LZ4_streamHC_t* streamHCPtr, int compressionLevel); +//LZ4LIB_API void LZ4_resetStreamHC (LZ4_streamHC_t* streamHCPtr, int compressionLevel); #if defined (__cplusplus) diff --git a/tests/fullbench.c b/tests/fullbench.c index 34c9b7b..456c916 100644 --- a/tests/fullbench.c +++ b/tests/fullbench.c @@ -245,7 +245,7 @@ static int local_LZ4_compress_forceDict(const char* in, char* out, int inSize) LZ4_streamHC_t LZ4_streamHC; static void local_LZ4_resetStreamHC(void) { - LZ4_initStreamHC(&LZ4_streamHC, 0); + LZ4_initStreamHC(&LZ4_streamHC, sizeof(LZ4_streamHC)); } static int local_LZ4_saveDictHC(const char* in, char* out, int inSize) diff --git a/tests/fuzzer.c b/tests/fuzzer.c index d899cef..8828c06 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -345,7 +345,8 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c DISPLAY("Not enough memory to start fuzzer tests"); goto _output_error; } - memset(&LZ4dict, 0, sizeof(LZ4dict)); + if ( LZ4_initStream(&LZ4dict, sizeof(LZ4dict)) == NULL) abort(); + if ( LZ4_initStreamHC(&LZ4dictHC, sizeof(LZ4dictHC)) == NULL) abort(); { U32 randState = coreRandState ^ PRIME3; FUZ_fillCompressibleNoiseBuffer(CNBuffer, COMPRESSIBLE_NOISE_LENGTH, compressibility, &randState); } @@ -800,7 +801,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c ret = LZ4_decompress_safe_usingDict(compressedBuffer, decodedBuffer, blockContinueCompressedSize, blockSize, dict, dictSize); FUZ_CHECKTEST(ret!=blockSize, "LZ4_decompress_safe_usingDict did not regenerate original data"); FUZ_CHECKTEST(decodedBuffer[blockSize], "LZ4_decompress_safe_usingDict overrun specified output buffer size"); - { U32 const crcCheck = XXH32(decodedBuffer, blockSize, 0); + { U32 const crcCheck = XXH32(decodedBuffer, (size_t)blockSize, 0); FUZ_CHECKTEST(crcCheck!=crcOrig, "LZ4_decompress_safe_usingDict corrupted decoded data"); } @@ -829,9 +830,8 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c FUZ_DISPLAYTEST("LZ4_compress_HC_continue with an external dictionary"); dict -= (FUZ_rand(&randState) & 7); /* even bigger separation */ if (dict < (char*)CNBuffer) dict = (char*)CNBuffer; - LZ4_initStreamHC (&LZ4dictHC, compressionLevel); LZ4_loadDictHC(&LZ4dictHC, dict, dictSize); - LZ4_setCompressionLevel(&LZ4dictHC, compressionLevel-1); + LZ4_setCompressionLevel (&LZ4dictHC, compressionLevel); blockContinueCompressedSize = LZ4_compress_HC_continue(&LZ4dictHC, block, compressedBuffer, blockSize, (int)compressedBufferSize); FUZ_CHECKTEST(blockContinueCompressedSize==0, "LZ4_compress_HC_continue failed"); FUZ_CHECKTEST(LZ4dictHC.internal_donotuse.dirty, "Context should be clean"); @@ -863,11 +863,11 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c FUZ_DISPLAYTEST(); { LZ4_streamHC_t LZ4_streamHC; + LZ4_initStreamHC(&LZ4_streamHC, sizeof(LZ4_streamHC)); - LZ4_initStreamHC (&LZ4dictHC, compressionLevel); LZ4_loadDictHC(&LZ4dictHC, dict, dictSize); - LZ4_initStreamHC (&LZ4_streamHC, compressionLevel); LZ4_attach_HC_dictionary(&LZ4_streamHC, &LZ4dictHC); + LZ4_setCompressionLevel (&LZ4_streamHC, compressionLevel); blockContinueCompressedSize = LZ4_compress_HC_continue(&LZ4_streamHC, block, compressedBuffer, blockSize, (int)compressedBufferSize); FUZ_CHECKTEST(blockContinueCompressedSize==0, "LZ4_compress_HC_continue with ExtDictCtx failed"); FUZ_CHECKTEST(LZ4_streamHC.internal_donotuse.dirty, "Context should be clean"); @@ -894,25 +894,25 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c FUZ_CHECKTEST(ret!=blockContinueCompressedSize, "LZ4_compress_HC_continue using ExtDictCtx and fast reset size is different (%i != %i)", ret, blockContinueCompressedSize); FUZ_CHECKTEST(ret<=0, "LZ4_compress_HC_continue using ExtDictCtx and fast reset should work : enough size available within output buffer"); FUZ_CHECKTEST(LZ4_streamHC.internal_donotuse.dirty, "Context should be clean"); + } - FUZ_DISPLAYTEST(); - decodedBuffer[blockSize] = 0; - ret = LZ4_decompress_safe_usingDict(compressedBuffer, decodedBuffer, blockContinueCompressedSize, blockSize, dict, dictSize); - FUZ_CHECKTEST(ret!=blockSize, "LZ4_decompress_safe_usingDict did not regenerate original data"); - FUZ_CHECKTEST(decodedBuffer[blockSize], "LZ4_decompress_safe_usingDict overrun specified output buffer size"); - { U32 const crcCheck = XXH32(decodedBuffer, (size_t)blockSize, 0); - if (crcCheck!=crcOrig) FUZ_findDiff(block, decodedBuffer); - FUZ_CHECKTEST(crcCheck!=crcOrig, "LZ4_decompress_safe_usingDict corrupted decoded data"); - } + FUZ_DISPLAYTEST(); + decodedBuffer[blockSize] = 0; + ret = LZ4_decompress_safe_usingDict(compressedBuffer, decodedBuffer, blockContinueCompressedSize, blockSize, dict, dictSize); + FUZ_CHECKTEST(ret!=blockSize, "LZ4_decompress_safe_usingDict did not regenerate original data"); + FUZ_CHECKTEST(decodedBuffer[blockSize], "LZ4_decompress_safe_usingDict overrun specified output buffer size"); + { U32 const crcCheck = XXH32(decodedBuffer, (size_t)blockSize, 0); + if (crcCheck!=crcOrig) FUZ_findDiff(block, decodedBuffer); + FUZ_CHECKTEST(crcCheck!=crcOrig, "LZ4_decompress_safe_usingDict corrupted decoded data"); } /* Compress HC continue destSize */ FUZ_DISPLAYTEST(); - { int const availableSpace = (FUZ_rand(&randState) % blockSize) + 5; + { int const availableSpace = (int)(FUZ_rand(&randState) % blockSize) + 5; int consumedSize = blockSize; FUZ_DISPLAYTEST(); - LZ4_initStreamHC (&LZ4dictHC, compressionLevel); LZ4_loadDictHC(&LZ4dictHC, dict, dictSize); + LZ4_setCompressionLevel(&LZ4dictHC, compressionLevel); blockContinueCompressedSize = LZ4_compress_HC_continue_destSize(&LZ4dictHC, block, compressedBuffer, &consumedSize, availableSpace); DISPLAYLEVEL(5, " LZ4_compress_HC_continue_destSize : compressed %6i/%6i into %6i/%6i at cLevel=%i\n", consumedSize, blockSize, blockContinueCompressedSize, availableSpace, compressionLevel); FUZ_CHECKTEST(blockContinueCompressedSize==0, "LZ4_compress_HC_continue_destSize failed"); @@ -1067,6 +1067,7 @@ static void FUZ_unitTests(int compressionLevel) { LZ4_streamHC_t sHC; /* statically allocated */ U64 crcOrig; int result; + LZ4_initStreamHC(&sHC, sizeof(sHC)); /* Allocation test */ DISPLAYLEVEL(3, " Basic HC allocation : "); @@ -1079,7 +1080,7 @@ static void FUZ_unitTests(int compressionLevel) /* simple HC compression test */ DISPLAYLEVEL(3, " Simple HC round-trip : "); { U64 const crc64 = XXH64(testInput, testCompressedSize, 0); - LZ4_initStreamHC(&sHC, compressionLevel); + LZ4_setCompressionLevel(&sHC, compressionLevel); result = LZ4_compress_HC_continue(&sHC, testInput, testCompressed, testCompressedSize, testCompressedSize-1); FUZ_CHECKTEST(result==0, "LZ4_compressHC_limitedOutput_continue() compression failed"); FUZ_CHECKTEST(sHC.internal_donotuse.dirty, "Context should be clean"); @@ -1104,7 +1105,7 @@ static void FUZ_unitTests(int compressionLevel) memset(block, 0, blockSize); ((char*)dstBlock)[targetSize] = sentinel; - LZ4_initStreamHC(&sHC, 3); + LZ4_resetStreamHC_fast(&sHC, 3); assert(blockSize < INT_MAX); srcSize = (int)blockSize; assert(targetSize < INT_MAX); -- cgit v1.2.3 From 887e8a4d012ac7b2034df9a27b1422ae98fee3a9 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 9 Apr 2019 14:00:30 -0700 Subject: re-enable LZ4_resetStreamHC() towards deprecation, but still available and fully supported --- lib/lz4hc.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/lz4hc.h b/lib/lz4hc.h index 445c126..ea90230 100644 --- a/lib/lz4hc.h +++ b/lib/lz4hc.h @@ -310,7 +310,7 @@ LZ4_DEPRECATED("use LZ4_initStreamHC() instead") LZ4LIB_API int LZ4_resetStre * It is recommended to switch to LZ4_initStreamHC(). * LZ4_resetStreamHC() will generate deprecation warnings in a future version. */ -//LZ4LIB_API void LZ4_resetStreamHC (LZ4_streamHC_t* streamHCPtr, int compressionLevel); +LZ4LIB_API void LZ4_resetStreamHC (LZ4_streamHC_t* streamHCPtr, int compressionLevel); #if defined (__cplusplus) -- cgit v1.2.3 From d8d5f14138109f81d0f5dba60ac179cfe16405c5 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 9 Apr 2019 15:37:59 -0700 Subject: fixed loadDictHC by making a full initialization instead of a fast reset. --- lib/lz4hc.c | 28 ++++++++++++++++++---------- tests/fuzzer.c | 8 ++++---- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/lib/lz4hc.c b/lib/lz4hc.c index 693337a..411b6cc 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -736,10 +736,10 @@ LZ4_FORCE_INLINE int LZ4HC_compress_generic_internal ( { lz4opt,16384,LZ4_OPT_NUM }, /* 12==LZ4HC_CLEVEL_MAX */ }; - DEBUGLOG(4, "LZ4HC_compress_generic(%p, %p, %d)", ctx, src, *srcSizePtr); + DEBUGLOG(4, "LZ4HC_compress_generic(ctx=%p, src=%p, srcSize=%d)", ctx, src, *srcSizePtr); - if (limit == limitedDestSize && dstCapacity < 1) return 0; /* Impossible to store anything */ - if ((U32)*srcSizePtr > (U32)LZ4_MAX_INPUT_SIZE) return 0; /* Unsupported input size (too large or negative) */ + if (limit == limitedDestSize && dstCapacity < 1) return 0; /* Impossible to store anything */ + if ((U32)*srcSizePtr > (U32)LZ4_MAX_INPUT_SIZE) return 0; /* Unsupported input size (too large or negative) */ ctx->end += *srcSizePtr; if (cLevel < 1) cLevel = LZ4HC_CLEVEL_DEFAULT; /* note : convention is different from lz4frame, maybe something to review */ @@ -756,7 +756,7 @@ LZ4_FORCE_INLINE int LZ4HC_compress_generic_internal ( assert(cParam.strat == lz4opt); result = LZ4HC_compress_optimal(ctx, src, dst, srcSizePtr, dstCapacity, - cParam.nbSearches, cParam.targetLength, limit, + (int)cParam.nbSearches, cParam.targetLength, limit, cLevel == LZ4HC_CLEVEL_MAX, /* ultra mode */ dict, favor); } @@ -925,7 +925,7 @@ LZ4_streamHC_t* LZ4_initStreamHC (void* buffer, size_t size) #endif /* if compilation fails here, LZ4_STREAMHCSIZE must be increased */ LZ4_STATIC_ASSERT(sizeof(LZ4HC_CCtx_internal) <= LZ4_STREAMHCSIZE); - DEBUGLOG(4, "LZ4_resetStreamHC(%p, %d)", LZ4_streamHCPtr, compressionLevel); + DEBUGLOG(4, "LZ4_initStreamHC(%p, %u)", LZ4_streamHCPtr, (unsigned)size); /* end-base will trigger a clearTable on starting compression */ LZ4_streamHCPtr->internal_donotuse.end = (const BYTE *)(ptrdiff_t)-1; LZ4_streamHCPtr->internal_donotuse.base = NULL; @@ -959,6 +959,7 @@ void LZ4_resetStreamHC_fast (LZ4_streamHC_t* LZ4_streamHCPtr, int compressionLev void LZ4_setCompressionLevel(LZ4_streamHC_t* LZ4_streamHCPtr, int compressionLevel) { + DEBUGLOG(5, "LZ4_setCompressionLevel(%p, %d)", LZ4_streamHCPtr, compressionLevel); if (compressionLevel < 1) compressionLevel = LZ4HC_CLEVEL_DEFAULT; if (compressionLevel > LZ4HC_CLEVEL_MAX) compressionLevel = LZ4HC_CLEVEL_MAX; LZ4_streamHCPtr->internal_donotuse.compressionLevel = (short)compressionLevel; @@ -971,7 +972,8 @@ void LZ4_favorDecompressionSpeed(LZ4_streamHC_t* LZ4_streamHCPtr, int favor) /* LZ4_loadDictHC() : * LZ4_streamHCPtr is presumed properly initialized */ -int LZ4_loadDictHC (LZ4_streamHC_t* LZ4_streamHCPtr, const char* dictionary, int dictSize) +int LZ4_loadDictHC (LZ4_streamHC_t* LZ4_streamHCPtr, + const char* dictionary, int dictSize) { LZ4HC_CCtx_internal* const ctxPtr = &LZ4_streamHCPtr->internal_donotuse; DEBUGLOG(4, "LZ4_loadDictHC(%p, %p, %d)", LZ4_streamHCPtr, dictionary, dictSize); @@ -980,7 +982,11 @@ int LZ4_loadDictHC (LZ4_streamHC_t* LZ4_streamHCPtr, const char* dictionary, int dictionary += dictSize - 64 KB; dictSize = 64 KB; } - LZ4_resetStreamHC_fast(LZ4_streamHCPtr, ctxPtr->compressionLevel); + /* need a full initialization, there are bad side-effects when using resetFast() */ + { int const cLevel = ctxPtr->compressionLevel; + LZ4_initStreamHC(LZ4_streamHCPtr, sizeof(*LZ4_streamHCPtr)); + LZ4_setCompressionLevel(LZ4_streamHCPtr, cLevel); + } LZ4HC_init_internal (ctxPtr, (const BYTE*)dictionary); ctxPtr->end = (const BYTE*)dictionary + dictSize; if (dictSize >= 4) LZ4HC_Insert (ctxPtr, ctxPtr->end-3); @@ -1014,7 +1020,8 @@ static int LZ4_compressHC_continue_generic (LZ4_streamHC_t* LZ4_streamHCPtr, limitedOutput_directive limit) { LZ4HC_CCtx_internal* const ctxPtr = &LZ4_streamHCPtr->internal_donotuse; - DEBUGLOG(4, "LZ4_compressHC_continue_generic(%p, %p, %d)", LZ4_streamHCPtr, src, *srcSizePtr); + DEBUGLOG(4, "LZ4_compressHC_continue_generic(ctx=%p, src=%p, srcSize=%d)", + LZ4_streamHCPtr, src, *srcSizePtr); assert(ctxPtr != NULL); /* auto-init if forgotten */ if (ctxPtr->base == NULL) LZ4HC_init_internal (ctxPtr, (const BYTE*) src); @@ -1027,7 +1034,8 @@ static int LZ4_compressHC_continue_generic (LZ4_streamHC_t* LZ4_streamHCPtr, } /* Check if blocks follow each other */ - if ((const BYTE*)src != ctxPtr->end) LZ4HC_setExternalDict(ctxPtr, (const BYTE*)src); + if ((const BYTE*)src != ctxPtr->end) + LZ4HC_setExternalDict(ctxPtr, (const BYTE*)src); /* Check overlapping input/dictionary space */ { const BYTE* sourceEnd = (const BYTE*) src + *srcSizePtr; @@ -1237,7 +1245,7 @@ static int LZ4HC_compress_optimal ( LZ4HC_CCtx_internal* ctx, BYTE* oend = op + dstCapacity; /* init */ - DEBUGLOG(5, "LZ4HC_compress_optimal"); + DEBUGLOG(5, "LZ4HC_compress_optimal(dst=%p, dstCapa=%u)", dst, (unsigned)dstCapacity); *srcSizePtr = 0; if (limit == limitedDestSize) oend -= LASTLITERALS; /* Hack for support LZ4 format restriction */ if (sufficient_len >= LZ4_OPT_NUM) sufficient_len = LZ4_OPT_NUM-1; diff --git a/tests/fuzzer.c b/tests/fuzzer.c index 8828c06..27d8d9c 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -836,16 +836,16 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c FUZ_CHECKTEST(blockContinueCompressedSize==0, "LZ4_compress_HC_continue failed"); FUZ_CHECKTEST(LZ4dictHC.internal_donotuse.dirty, "Context should be clean"); - FUZ_DISPLAYTEST(); + FUZ_DISPLAYTEST("LZ4_compress_HC_continue with same external dictionary, but output buffer 1 byte too short"); LZ4_loadDictHC(&LZ4dictHC, dict, dictSize); ret = LZ4_compress_HC_continue(&LZ4dictHC, block, compressedBuffer, blockSize, blockContinueCompressedSize-1); - FUZ_CHECKTEST(ret>0, "LZ4_compress_HC_continue using ExtDict should fail : one missing byte for output buffer (%i != %i)", ret, blockContinueCompressedSize); + FUZ_CHECKTEST(ret>0, "LZ4_compress_HC_continue using ExtDict should fail : one missing byte for output buffer (expected %i, but result=%i)", blockContinueCompressedSize, ret); FUZ_CHECKTEST(!LZ4dictHC.internal_donotuse.dirty, "Context should be dirty"); - FUZ_DISPLAYTEST(); + FUZ_DISPLAYTEST("LZ4_compress_HC_continue with same external dictionary, and output buffer exactly the right size"); LZ4_loadDictHC(&LZ4dictHC, dict, dictSize); ret = LZ4_compress_HC_continue(&LZ4dictHC, block, compressedBuffer, blockSize, blockContinueCompressedSize); - FUZ_CHECKTEST(ret!=blockContinueCompressedSize, "LZ4_compress_HC_continue size is different (%i != %i)", ret, blockContinueCompressedSize); + FUZ_CHECKTEST(ret!=blockContinueCompressedSize, "LZ4_compress_HC_continue size is different : ret(%i) != expected(%i)", ret, blockContinueCompressedSize); FUZ_CHECKTEST(ret<=0, "LZ4_compress_HC_continue should work : enough size available within output buffer"); FUZ_CHECKTEST(LZ4dictHC.internal_donotuse.dirty, "Context should be clean"); -- cgit v1.2.3 From 9e501702bd1137b4049d06edc60ec366ec725d34 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 9 Apr 2019 18:10:02 -0700 Subject: made LZ4F_getHeaderSize() public --- lib/lz4frame.c | 90 +++++++++++++++++++++++++++++-------------------------- lib/lz4frame.h | 66 ++++++++++++++++++++++++++++++---------- tests/frametest.c | 11 +++++-- 3 files changed, 107 insertions(+), 60 deletions(-) diff --git a/lib/lz4frame.c b/lib/lz4frame.c index efe38da..2b31a0d 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -211,7 +211,7 @@ static void LZ4F_writeLE64 (void* dst, U64 value64) #define LZ4F_BLOCKUNCOMPRESSED_FLAG 0x80000000U #define LZ4F_BLOCKSIZEID_DEFAULT LZ4F_max64KB -static const size_t minFHSize = 7; +static const size_t minFHSize = LZ4F_HEADER_SIZE_MIN; /* 7 */ static const size_t maxFHSize = LZ4F_HEADER_SIZE_MAX; /* 19 */ static const size_t BHSize = 4; /* block header : size, and compress flag */ static const size_t BFSize = 4; /* block footer : checksum (optional) */ @@ -280,8 +280,9 @@ 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; - if (blockSizeID < 4 || blockSizeID > 7) return err0r(LZ4F_ERROR_maxBlockSize_invalid); - blockSizeID -= 4; + if (blockSizeID < LZ4F_max64KB || blockSizeID > LZ4F_max4MB) + return err0r(LZ4F_ERROR_maxBlockSize_invalid); + blockSizeID -= LZ4F_max64KB; return blockSizes[blockSizeID]; } @@ -1094,31 +1095,6 @@ void LZ4F_resetDecompressionContext(LZ4F_dctx* dctx) } -/*! LZ4F_headerSize() : - * @return : size of frame header - * or an error code, which can be tested using LZ4F_isError() - */ -static size_t LZ4F_headerSize(const void* src, size_t srcSize) -{ - /* minimal srcSize to determine header size */ - if (srcSize < 5) return err0r(LZ4F_ERROR_frameHeader_incomplete); - - /* special case : skippable frames */ - if ((LZ4F_readLE32(src) & 0xFFFFFFF0U) == LZ4F_MAGIC_SKIPPABLE_START) return 8; - - /* control magic number */ - if (LZ4F_readLE32(src) != LZ4F_MAGICNUMBER) - return err0r(LZ4F_ERROR_frameType_unknown); - - /* Frame Header Size */ - { BYTE const FLG = ((const BYTE*)src)[4]; - U32 const contentSizeFlag = (FLG>>3) & _1BIT; - U32 const dictIDFlag = FLG & _1BIT; - return minFHSize + (contentSizeFlag*8) + (dictIDFlag*4); - } -} - - /*! LZ4F_decodeHeader() : * input : `src` points at the **beginning of the frame** * output : set internal values of dctx, such as @@ -1191,6 +1167,7 @@ static size_t LZ4F_decodeHeader(LZ4F_dctx* dctx, const void* src, size_t srcSize } /* check header */ + assert(frameHeaderSize > 5); { BYTE const HC = LZ4F_headerChecksum(srcPtr+4, frameHeaderSize-5); if (HC != srcPtr[frameHeaderSize-1]) return err0r(LZ4F_ERROR_headerChecksum_invalid); @@ -1214,6 +1191,34 @@ static size_t LZ4F_decodeHeader(LZ4F_dctx* dctx, const void* src, size_t srcSize } +/*! LZ4F_headerSize() : + * @return : size of frame header + * or an error code, which can be tested using LZ4F_isError() + */ +size_t LZ4F_headerSize(const void* src, size_t srcSize) +{ + if (src == NULL) return err0r(LZ4F_ERROR_srcPtr_wrong); + + /* minimal srcSize to determine header size */ + if (srcSize < LZ4F_MIN_SIZE_TO_KNOW_HEADER_LENGTH) + return err0r(LZ4F_ERROR_frameHeader_incomplete); + + /* special case : skippable frames */ + if ((LZ4F_readLE32(src) & 0xFFFFFFF0U) == LZ4F_MAGIC_SKIPPABLE_START) + return 8; + + /* control magic number */ + if (LZ4F_readLE32(src) != LZ4F_MAGICNUMBER) + return err0r(LZ4F_ERROR_frameType_unknown); + + /* Frame Header Size */ + { BYTE const FLG = ((const BYTE*)src)[4]; + U32 const contentSizeFlag = (FLG>>3) & _1BIT; + U32 const dictIDFlag = FLG & _1BIT; + return minFHSize + (contentSizeFlag*8) + (dictIDFlag*4); + } +} + /*! LZ4F_getFrameInfo() : * This function extracts frame parameters (max blockSize, frame checksum, etc.). * Usage is optional. Objective is to provide relevant information for allocation purposes. @@ -1229,10 +1234,12 @@ static size_t LZ4F_decodeHeader(LZ4F_dctx* dctx, const void* src, size_t srcSize * note 1 : in case of error, dctx is not modified. Decoding operations can resume from where they stopped. * note 2 : frame parameters are *copied into* an already allocated LZ4F_frameInfo_t structure. */ -LZ4F_errorCode_t LZ4F_getFrameInfo(LZ4F_dctx* dctx, LZ4F_frameInfo_t* frameInfoPtr, - const void* srcBuffer, size_t* srcSizePtr) +LZ4F_errorCode_t LZ4F_getFrameInfo(LZ4F_dctx* dctx, + LZ4F_frameInfo_t* frameInfoPtr, + const void* srcBuffer, size_t* srcSizePtr) { - if (dctx->dStage > dstage_storeFrameHeader) { /* assumption : dstage_* header enum at beginning of range */ + LZ4F_STATIC_ASSERT(dstage_getFrameHeader < dstage_storeFrameHeader); + if (dctx->dStage > dstage_storeFrameHeader) { /* frameInfo already decoded */ size_t o=0, i=0; *srcSizePtr = 0; @@ -1245,7 +1252,6 @@ LZ4F_errorCode_t LZ4F_getFrameInfo(LZ4F_dctx* dctx, LZ4F_frameInfo_t* frameInfoP *srcSizePtr = 0; return err0r(LZ4F_ERROR_frameDecoding_alreadyStarted); } else { - size_t decodeResult; size_t const hSize = LZ4F_headerSize(srcBuffer, *srcSizePtr); if (LZ4F_isError(hSize)) { *srcSizePtr=0; return hSize; } if (*srcSizePtr < hSize) { @@ -1253,16 +1259,16 @@ LZ4F_errorCode_t LZ4F_getFrameInfo(LZ4F_dctx* dctx, LZ4F_frameInfo_t* frameInfoP return err0r(LZ4F_ERROR_frameHeader_incomplete); } - decodeResult = LZ4F_decodeHeader(dctx, srcBuffer, hSize); - if (LZ4F_isError(decodeResult)) { - *srcSizePtr = 0; - } else { - *srcSizePtr = decodeResult; - decodeResult = BHSize; /* block header size */ - } - *frameInfoPtr = dctx->frameInfo; - return decodeResult; - } } + { size_t decodeResult = LZ4F_decodeHeader(dctx, srcBuffer, hSize); + if (LZ4F_isError(decodeResult)) { + *srcSizePtr = 0; + } else { + *srcSizePtr = decodeResult; + decodeResult = BHSize; /* block header size */ + } + *frameInfoPtr = dctx->frameInfo; + return decodeResult; + } } } } diff --git a/lib/lz4frame.h b/lib/lz4frame.h index 68f4118..2ada8b8 100644 --- a/lib/lz4frame.h +++ b/lib/lz4frame.h @@ -247,7 +247,8 @@ LZ4FLIB_API LZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_cctx* cctx); /*---- Compression ----*/ -#define LZ4F_HEADER_SIZE_MAX 19 /* LZ4 Frame header size can vary from 7 to 19 bytes */ +#define LZ4F_HEADER_SIZE_MIN 7 /* LZ4 Frame header size can vary, depending on selected paramaters */ +#define LZ4F_HEADER_SIZE_MAX 19 /*! LZ4F_compressBegin() : * will write the frame header into dstBuffer. @@ -352,23 +353,58 @@ LZ4FLIB_API LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_dctx* dctx); * Streaming decompression functions *************************************/ +#define LZ4F_MIN_SIZE_TO_KNOW_HEADER_LENGTH 5 + +/*! LZ4F_headerSize() : v1.9.0+ + * Provide the header size of a frame starting at `src`. + * `srcSize` must be >= LZ4F_MIN_SIZE_TO_KNOW_HEADER_LENGTH, + * which is enough to decode the header length. + * @return : size of frame header + * or an error code, which can be tested using LZ4F_isError() + * note : Frame header size is variable, but is guaranteed to be + * >= LZ4F_HEADER_SIZE_MIN bytes, and <= LZ4F_HEADER_SIZE_MAX bytes. + */ +size_t LZ4F_headerSize(const void* src, size_t srcSize); + /*! LZ4F_getFrameInfo() : * This function extracts frame parameters (max blockSize, dictID, etc.). - * Its usage is optional. - * Extracted information is typically useful for allocation and dictionary. - * This function works in 2 situations : - * - At the beginning of a new frame, in which case - * it will decode information from `srcBuffer`, starting the decoding process. - * Input size must be large enough to successfully decode the entire frame header. - * Frame header size is variable, but is guaranteed to be <= LZ4F_HEADER_SIZE_MAX bytes. - * It's allowed to provide more input data than this minimum. - * - After decoding has been started. - * In which case, no input is read, frame parameters are extracted from dctx. - * - If decoding has barely started, but not yet extracted information from header, + * Its usage is optional: user can call LZ4F_decompress() directly. + * + * Extracted information will fill an existing LZ4F_frameInfo_t structure. + * This can be useful for allocation and dictionary identification purposes. + * + * LZ4F_getFrameInfo() can work in the following situations : + * + * 1) At the beginning of a new frame, before any invocation of LZ4F_decompress(). + * It will decode header from `srcBuffer`, + * consuming the header and starting the decoding process. + * + * Input size must be large enough to contain the full frame header. + * Frame header size can be known beforehand by LZ4F_headerSize(). + * Frame header size is variable, but is guaranteed to be >= LZ4F_HEADER_SIZE_MIN bytes, + * and not more than <= LZ4F_HEADER_SIZE_MAX bytes. + * Hence, blindly providing LZ4F_HEADER_SIZE_MAX bytes or more will always work. + * It's allowed to provide more input data than the header size, + * LZ4F_getFrameInfo() will only consume the header. + * + * If input size is not large enough, + * aka if it's smaller than header size, + * function will fail and return an error code. + * + * 2) After decoding has been started, + * it's possible to invoke LZ4F_getFrameInfo() anytime + * to extract already decoded frame parameters stored within dctx. + * + * Note that, if decoding has barely started, + * and not yet read enough information to decode the header, * LZ4F_getFrameInfo() will fail. - * The number of bytes consumed from srcBuffer will be updated within *srcSizePtr (necessarily <= original value). - * Decompression must resume from (srcBuffer + *srcSizePtr). - * @return : an hint about how many srcSize bytes LZ4F_decompress() expects for next call, + * + * The number of bytes consumed from srcBuffer will be updated in *srcSizePtr (necessarily <= original value). + * LZ4F_getFrameInfo() only consumes bytes when decoding has not yet started, + * and when decoding the header has been successful. + * Decompression must then resume from (srcBuffer + *srcSizePtr). + * + * @return : a hint about how many srcSize bytes LZ4F_decompress() expects for next call, * or an error code which can be tested using LZ4F_isError(). * note 1 : in case of error, dctx is not modified. Decoding operation can resume from beginning safely. * note 2 : frame parameters are *copied into* an already allocated LZ4F_frameInfo_t structure. diff --git a/tests/frametest.c b/tests/frametest.c index fa005db..59e866c 100644 --- a/tests/frametest.c +++ b/tests/frametest.c @@ -211,9 +211,13 @@ int basicTests(U32 seed, double compressibility) CHECK ( LZ4F_createDecompressionContext(&dCtx, LZ4F_VERSION) ); DISPLAYLEVEL(3, "LZ4F_getFrameInfo on null-content frame (#157) \n"); - { size_t avail_in = cSize; - LZ4F_frameInfo_t frame_info; + assert(cSize >= LZ4F_MIN_SIZE_TO_KNOW_HEADER_LENGTH); + { LZ4F_frameInfo_t frame_info; + size_t const fhs = LZ4F_headerSize(compressedBuffer, LZ4F_MIN_SIZE_TO_KNOW_HEADER_LENGTH); + size_t avail_in = fhs; + CHECK( fhs ); CHECK( LZ4F_getFrameInfo(dCtx, &frame_info, compressedBuffer, &avail_in) ); + if (avail_in != fhs) goto _output_error; /* must consume all, since header size is supposed to be exact */ } DISPLAYLEVEL(3, "LZ4F_freeDecompressionContext \n"); @@ -306,7 +310,8 @@ int basicTests(U32 seed, double compressibility) } DISPLAYLEVEL(3, "LZ4F_getFrameInfo on enough input : "); - iSize = 15 - iSize; + iSize = LZ4F_headerSize(ip, LZ4F_MIN_SIZE_TO_KNOW_HEADER_LENGTH); + CHECK( iSize ); CHECK( LZ4F_getFrameInfo(dCtx, &fi, ip, &iSize) ); DISPLAYLEVEL(3, " correctly decoded \n"); } -- cgit v1.2.3 From db60ed19b131e59485f0a716865bd59381815145 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 9 Apr 2019 18:23:32 -0700 Subject: added versions in comments --- lib/lz4.h | 3 ++- lib/lz4hc.h | 6 +++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/lz4.h b/lib/lz4.h index e55e1c6..b86417a 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -526,7 +526,7 @@ union LZ4_stream_u { LZ4_stream_t_internal internal_donotuse; } ; /* previously typedef'd to LZ4_stream_t */ -/*! LZ4_initStream() : +/*! LZ4_initStream() : v1.9.0+ * An LZ4_stream_t structure must be initialized at least once. * This is automatically done when invoking LZ4_createStream(), * but it's not when the structure is simply declared on stack (for example). @@ -538,6 +538,7 @@ union LZ4_stream_u { * Note : initialization fails if size and alignment conditions are not respected. * In which case, the function will @return NULL. * Note2: An LZ4_stream_t structure guarantees correct alignment and size. + * Note3: Before v1.9.0, use LZ4_resetStream() instead */ LZ4LIB_API LZ4_stream_t* LZ4_initStream (void* buffer, size_t size); diff --git a/lib/lz4hc.h b/lib/lz4hc.h index ea90230..cdc6d89 100644 --- a/lib/lz4hc.h +++ b/lib/lz4hc.h @@ -265,6 +265,10 @@ union LZ4_streamHC_u { * Static allocation shall only be used in combination with static linking. */ +/* LZ4_initStreamHC() : v1.9.0+ + * Required before first use of a statically allocated LZ4_streamHC_t. + * Before v1.9.0 : use LZ4_resetStreamHC() instead + */ LZ4LIB_API LZ4_streamHC_t* LZ4_initStreamHC (void* buffer, size_t size); @@ -351,7 +355,7 @@ LZ4LIB_STATIC_API void LZ4_setCompressionLevel( LZ4LIB_STATIC_API void LZ4_favorDecompressionSpeed( LZ4_streamHC_t* LZ4_streamHCPtr, int favor); -/*! LZ4_resetStreamHC_fast() : +/*! LZ4_resetStreamHC_fast() : v1.9.0+ * When an LZ4_streamHC_t is known to be in a internally coherent state, * it can often be prepared for a new compression with almost no work, only * sometimes falling back to the full, expensive reset that is always required -- cgit v1.2.3 From 013fee5665cbf03113c1c2e78d5b50fa9663b306 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 11 Apr 2019 13:55:35 -0700 Subject: updated block format with more details on MF_LIMIT aka distance from end of block for the last match. --- doc/lz4_Block_format.md | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/doc/lz4_Block_format.md b/doc/lz4_Block_format.md index 2fb4c19..4344e9b 100644 --- a/doc/lz4_Block_format.md +++ b/doc/lz4_Block_format.md @@ -1,6 +1,6 @@ LZ4 Block Format Description ============================ -Last revised: 2018-12-30. +Last revised: 2019-03-30. Author : Yann Collet @@ -109,18 +109,29 @@ End of block restrictions ----------------------- There are specific rules required to terminate a block. -1. The last sequence only contains literals. The block ends right after them. -1. The last 5 bytes of input are always literals. - Therefore, the last sequence contains at least 5 bytes, - or all input bytes if input is smaller than 5 bytes - (empty input can be represented with a zero byte, - interpreted as a token without literal and without a match). -2. The last match must start at least 12 bytes before the end of block. +1. The last sequence contains only literals. + The block ends right after them. +2. The last 5 bytes of input are always literals. + Therefore, the last sequence contains at least 5 bytes. + - Special : if input is smaller than 5 bytes, + there is only one sequence, it contains the whole input as literals. + Empty input can be represented with a zero byte, + interpreted as a final token without literal and without a match. +3. The last match must start at least 12 bytes before the end of block. The last match is part of the penultimate sequence. - It is followed by the last sequence, which only contains literals. - Note that, as a consequence, blocks < 13 bytes cannot be compressed. - -These rules are in place to ensure that a compatible decoder + It is followed by the last sequence, which contains only literals. + - Note that, as a consequence, + an independent block < 13 bytes cannot be compressed, + because the match must copy "something", + so it needs at least one prior byte. + - When a block can reference data from another block, + it can start immediately with a match and no literal, + so a block of 12 bytes can be compressed. + +When a block does not respect these end conditions, +a conformant decoder is allowed to reject the block as incorrect. + +These rules are in place to ensure that a conformant decoder can be designed for speed, issuing speculatively instructions, while never reading nor writing beyond provided I/O buffers. -- cgit v1.2.3 From 8d76c8a44a15cc7c0c1f345ba750e44edac7abb7 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 11 Apr 2019 14:15:33 -0700 Subject: introduce LZ4_DISTANCE_MAX build macro make it possible to generate LZ4-compressed block with a controlled maximum offset (necessarily <= 65535). This could be useful for compatibility with decoders using a very limited memory budget (<64 KB). Answer #154 --- doc/lz4_manual.html | 1 + doc/lz4frame_manual.html | 61 +++++++++++++++++++++++++++++++++++++----------- lib/README.md | 7 ++++++ lib/lz4.c | 35 +++++++++++++++------------ lib/lz4hc.c | 16 ++++++------- programs/Makefile | 2 +- 6 files changed, 84 insertions(+), 38 deletions(-) diff --git a/doc/lz4_manual.html b/doc/lz4_manual.html index 1c6dba7..ef1a8b5 100644 --- a/doc/lz4_manual.html +++ b/doc/lz4_manual.html @@ -396,6 +396,7 @@ union LZ4_stream_u { Note : initialization fails if size and alignment conditions are not respected. In which case, the function will @return NULL. Note2: An LZ4_stream_t structure guarantees correct alignment and size. + Note3: Before v1.9.0, use LZ4_resetStream() instead


    diff --git a/doc/lz4frame_manual.html b/doc/lz4frame_manual.html index 4277c3c..d5496a1 100644 --- a/doc/lz4frame_manual.html +++ b/doc/lz4frame_manual.html @@ -237,25 +237,58 @@ LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_dctx* dctx);

    Streaming decompression functions

    
     
    +
    size_t LZ4F_headerSize(const void* src, size_t srcSize);
    +

    Provide the header size of a frame starting at `src`. + `srcSize` must be >= LZ4F_MIN_SIZE_TO_KNOW_HEADER_LENGTH, + which is enough to decode the header length. + @return : size of frame header + or an error code, which can be tested using LZ4F_isError() + note : Frame header size is variable, but is guaranteed to be + >= LZ4F_HEADER_SIZE_MIN bytes, and <= LZ4F_HEADER_SIZE_MAX bytes. + +


    +
    size_t LZ4F_getFrameInfo(LZ4F_dctx* dctx,
                                          LZ4F_frameInfo_t* frameInfoPtr,
                                          const void* srcBuffer, size_t* srcSizePtr);
     

    This function extracts frame parameters (max blockSize, dictID, etc.). - Its usage is optional. - Extracted information is typically useful for allocation and dictionary. - This function works in 2 situations : - - At the beginning of a new frame, in which case - it will decode information from `srcBuffer`, starting the decoding process. - Input size must be large enough to successfully decode the entire frame header. - Frame header size is variable, but is guaranteed to be <= LZ4F_HEADER_SIZE_MAX bytes. - It's allowed to provide more input data than this minimum. - - After decoding has been started. - In which case, no input is read, frame parameters are extracted from dctx. - - If decoding has barely started, but not yet extracted information from header, + Its usage is optional: user can call LZ4F_decompress() directly. + + Extracted information will fill an existing LZ4F_frameInfo_t structure. + This can be useful for allocation and dictionary identification purposes. + + LZ4F_getFrameInfo() can work in the following situations : + + 1) At the beginning of a new frame, before any invocation of LZ4F_decompress(). + It will decode header from `srcBuffer`, + consuming the header and starting the decoding process. + + Input size must be large enough to contain the full frame header. + Frame header size can be known beforehand by LZ4F_headerSize(). + Frame header size is variable, but is guaranteed to be >= LZ4F_HEADER_SIZE_MIN bytes, + and not more than <= LZ4F_HEADER_SIZE_MAX bytes. + Hence, blindly providing LZ4F_HEADER_SIZE_MAX bytes or more will always work. + It's allowed to provide more input data than the header size, + LZ4F_getFrameInfo() will only consume the header. + + If input size is not large enough, + aka if it's smaller than header size, + function will fail and return an error code. + + 2) After decoding has been started, + it's possible to invoke LZ4F_getFrameInfo() anytime + to extract already decoded frame parameters stored within dctx. + + Note that, if decoding has barely started, + and not yet read enough information to decode the header, LZ4F_getFrameInfo() will fail. - The number of bytes consumed from srcBuffer will be updated within *srcSizePtr (necessarily <= original value). - Decompression must resume from (srcBuffer + *srcSizePtr). - @return : an hint about how many srcSize bytes LZ4F_decompress() expects for next call, + + The number of bytes consumed from srcBuffer will be updated in *srcSizePtr (necessarily <= original value). + LZ4F_getFrameInfo() only consumes bytes when decoding has not yet started, + and when decoding the header has been successful. + Decompression must then resume from (srcBuffer + *srcSizePtr). + + @return : a hint about how many srcSize bytes LZ4F_decompress() expects for next call, or an error code which can be tested using LZ4F_isError(). note 1 : in case of error, dctx is not modified. Decoding operation can resume from beginning safely. note 2 : frame parameters are *copied into* an already allocated LZ4F_frameInfo_t structure. diff --git a/lib/README.md b/lib/README.md index be8eba0..c6daaea 100644 --- a/lib/README.md +++ b/lib/README.md @@ -52,6 +52,13 @@ The following build macro can be determined at compilation time : For example, with `gcc` : `-DLZ4_FAST_DEC_LOOP=1`, and with `make` : `CPPFLAGS+=-DLZ4_FAST_DEC_LOOP=1 make lz4`. +- `LZ4_DISTANCE_MAX` : control the maximum offset that the compressor will allow. + Set to 65535 by default, which is the maximum value supported by lz4 format. + Reducing maximum distance will reduce opportunities for LZ4 to find matches, + hence will produce worse the compression ratio. + However, a smaller max distance may allow compatibility with specific decoders using limited memory budget. + This build macro only influences the compressed output of the compressor. + - `LZ4_DISABLE_DEPRECATE_WARNINGS` : invoking a deprecated function will make the compiler generate a warning. This is meant to invite users to update their source code. Should this be a problem, it's generally to make the compiler ignore these warnings, diff --git a/lib/lz4.c b/lib/lz4.c index 34587ce..ca3684f 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -395,8 +395,13 @@ static const int LZ4_minLength = (MFLIMIT+1); #define MB *(1 <<20) #define GB *(1U<<30) -#define MAXD_LOG 16 -#define MAX_DISTANCE ((1 << MAXD_LOG) - 1) +#ifndef LZ4_DISTANCE_MAX /* can be user - defined at compile time */ +# define LZ4_DISTANCE_MAX 65535 +#endif + +#if (LZ4_DISTANCE_MAX > 65535) /* max supported by LZ4 format */ +# error "LZ4_DISTANCE_MAX is too big : must be <= 65535" +#endif #define ML_BITS 4 #define ML_MASK ((1U< MAX_DISTANCE back, is faster + /* Adding a gap, so all previous entries are > LZ4_DISTANCE_MAX back, is faster * than compressing without a gap. However, compressing with * currentOffset == 0 is faster still, so we preserve that case. */ @@ -850,7 +855,7 @@ LZ4_FORCE_INLINE int LZ4_compress_generic( forwardH = LZ4_hashPosition(forwardIp, tableType); LZ4_putPositionOnHash(ip, h, cctx->hashTable, tableType, base); - } while ( (match+MAX_DISTANCE < ip) + } while ( (match+LZ4_DISTANCE_MAX < ip) || (LZ4_read32(match) != LZ4_read32(ip)) ); } else { /* byU32, byU16 */ @@ -901,8 +906,8 @@ LZ4_FORCE_INLINE int LZ4_compress_generic( if ((dictIssue == dictSmall) && (matchIndex < prefixIdxLimit)) continue; /* match outside of valid area */ assert(matchIndex < current); - if ((tableType != byU16) && (matchIndex+MAX_DISTANCE < current)) continue; /* too far */ - if (tableType == byU16) assert((current - matchIndex) <= MAX_DISTANCE); /* too_far presumed impossible with byU16 */ + if ((tableType != byU16) && (matchIndex+LZ4_DISTANCE_MAX < current)) continue; /* too far */ + if (tableType == byU16) assert((current - matchIndex) <= LZ4_DISTANCE_MAX); /* too_far presumed impossible with byU16 */ if (LZ4_read32(match) == LZ4_read32(ip)) { if (maybe_extMem) offset = current - matchIndex; @@ -961,11 +966,11 @@ _next_match: /* Encode Offset */ if (maybe_extMem) { /* static test */ DEBUGLOG(6, " with offset=%u (ext if > %i)", offset, (int)(ip - (const BYTE*)source)); - assert(offset <= MAX_DISTANCE && offset > 0); + assert(offset <= LZ4_DISTANCE_MAX && offset > 0); LZ4_writeLE16(op, (U16)offset); op+=2; } else { DEBUGLOG(6, " with offset=%u (same segment)", (U32)(ip - match)); - assert(ip-match <= MAX_DISTANCE); + assert(ip-match <= LZ4_DISTANCE_MAX); LZ4_writeLE16(op, (U16)(ip - match)); op+=2; } @@ -1030,7 +1035,7 @@ _next_match: match = LZ4_getPosition(ip, cctx->hashTable, tableType, base); LZ4_putPosition(ip, cctx->hashTable, tableType, base); - if ( (match+MAX_DISTANCE >= ip) + if ( (match+LZ4_DISTANCE_MAX >= ip) && (LZ4_read32(match) == LZ4_read32(ip)) ) { token=op++; *token=0; goto _next_match; } @@ -1065,7 +1070,7 @@ _next_match: LZ4_putIndexOnHash(current, h, cctx->hashTable, tableType); assert(matchIndex < current); if ( ((dictIssue==dictSmall) ? (matchIndex >= prefixIdxLimit) : 1) - && ((tableType==byU16) ? 1 : (matchIndex+MAX_DISTANCE >= current)) + && ((tableType==byU16) ? 1 : (matchIndex+LZ4_DISTANCE_MAX >= current)) && (LZ4_read32(match) == LZ4_read32(ip)) ) { token=op++; *token=0; @@ -1132,14 +1137,14 @@ int LZ4_compress_fast_extState(void* state, const char* source, char* dest, int if (inputSize < LZ4_64Klimit) { return LZ4_compress_generic(ctx, source, dest, inputSize, NULL, 0, notLimited, byU16, noDict, noDictIssue, acceleration); } else { - const tableType_t tableType = ((sizeof(void*)==4) && ((uptrval)source > MAX_DISTANCE)) ? byPtr : byU32; + const tableType_t tableType = ((sizeof(void*)==4) && ((uptrval)source > LZ4_DISTANCE_MAX)) ? byPtr : byU32; return LZ4_compress_generic(ctx, source, dest, inputSize, NULL, 0, notLimited, tableType, noDict, noDictIssue, acceleration); } } else { if (inputSize < LZ4_64Klimit) {; return LZ4_compress_generic(ctx, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, byU16, noDict, noDictIssue, acceleration); } else { - const tableType_t tableType = ((sizeof(void*)==4) && ((uptrval)source > MAX_DISTANCE)) ? byPtr : byU32; + const tableType_t tableType = ((sizeof(void*)==4) && ((uptrval)source > LZ4_DISTANCE_MAX)) ? byPtr : byU32; return LZ4_compress_generic(ctx, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, noDict, noDictIssue, acceleration); } } @@ -1169,7 +1174,7 @@ int LZ4_compress_fast_extState_fastReset(void* state, const char* src, char* dst return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, 0, notLimited, tableType, noDict, noDictIssue, acceleration); } } else { - const tableType_t tableType = ((sizeof(void*)==4) && ((uptrval)src > MAX_DISTANCE)) ? byPtr : byU32; + const tableType_t tableType = ((sizeof(void*)==4) && ((uptrval)src > LZ4_DISTANCE_MAX)) ? byPtr : byU32; LZ4_prepareTable(ctx, srcSize, tableType); return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, 0, notLimited, tableType, noDict, noDictIssue, acceleration); } @@ -1183,7 +1188,7 @@ int LZ4_compress_fast_extState_fastReset(void* state, const char* src, char* dst return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, dstCapacity, limitedOutput, tableType, noDict, noDictIssue, acceleration); } } else { - const tableType_t tableType = ((sizeof(void*)==4) && ((uptrval)src > MAX_DISTANCE)) ? byPtr : byU32; + const tableType_t tableType = ((sizeof(void*)==4) && ((uptrval)src > LZ4_DISTANCE_MAX)) ? byPtr : byU32; LZ4_prepareTable(ctx, srcSize, tableType); return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, dstCapacity, limitedOutput, tableType, noDict, noDictIssue, acceleration); } @@ -1246,7 +1251,7 @@ static int LZ4_compress_destSize_extState (LZ4_stream_t* state, const char* src, if (*srcSizePtr < LZ4_64Klimit) { return LZ4_compress_generic(&state->internal_donotuse, src, dst, *srcSizePtr, srcSizePtr, targetDstSize, fillOutput, byU16, noDict, noDictIssue, 1); } else { - tableType_t const addrMode = ((sizeof(void*)==4) && ((uptrval)src > MAX_DISTANCE)) ? byPtr : byU32; + tableType_t const addrMode = ((sizeof(void*)==4) && ((uptrval)src > LZ4_DISTANCE_MAX)) ? byPtr : byU32; return LZ4_compress_generic(&state->internal_donotuse, src, dst, *srcSizePtr, srcSizePtr, targetDstSize, fillOutput, addrMode, noDict, noDictIssue, 1); } } } diff --git a/lib/lz4hc.c b/lib/lz4hc.c index 411b6cc..a6dc7a2 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -132,7 +132,7 @@ LZ4_FORCE_INLINE void LZ4HC_Insert (LZ4HC_CCtx_internal* hc4, const BYTE* ip) while (idx < target) { U32 const h = LZ4HC_hashPtr(base+idx); size_t delta = idx - hashTable[h]; - if (delta>MAX_DISTANCE) delta = MAX_DISTANCE; + if (delta>LZ4_DISTANCE_MAX) delta = LZ4_DISTANCE_MAX; DELTANEXTU16(chainTable, idx) = (U16)delta; hashTable[h] = idx; idx++; @@ -235,7 +235,7 @@ LZ4HC_InsertAndGetWiderMatch ( const U32 dictLimit = hc4->dictLimit; const BYTE* const lowPrefixPtr = base + dictLimit; const U32 ipIndex = (U32)(ip - base); - const U32 lowestMatchIndex = (hc4->lowLimit + 64 KB > ipIndex) ? hc4->lowLimit : ipIndex - MAX_DISTANCE; + const U32 lowestMatchIndex = (hc4->lowLimit + 64 KB > ipIndex) ? hc4->lowLimit : ipIndex - LZ4_DISTANCE_MAX; const BYTE* const dictBase = hc4->dictBase; int const lookBackLength = (int)(ip-iLowLimit); int nbAttempts = maxNbAttempts; @@ -325,7 +325,7 @@ LZ4HC_InsertAndGetWiderMatch ( const BYTE* const matchPtr = base + matchCandidateIdx; if (LZ4_read32(matchPtr) == pattern) { /* good candidate */ size_t const forwardPatternLength = LZ4HC_countPattern(matchPtr+sizeof(pattern), iHighLimit, pattern) + sizeof(pattern); - const BYTE* const lowestMatchPtr = (lowPrefixPtr + MAX_DISTANCE >= ip) ? lowPrefixPtr : ip - MAX_DISTANCE; + const BYTE* const lowestMatchPtr = (lowPrefixPtr + LZ4_DISTANCE_MAX >= ip) ? lowPrefixPtr : ip - LZ4_DISTANCE_MAX; size_t const backLength = LZ4HC_reverseCountPattern(matchPtr, lowestMatchPtr, pattern); size_t const currentSegmentLength = backLength + forwardPatternLength; @@ -338,7 +338,7 @@ LZ4HC_InsertAndGetWiderMatch ( size_t const maxML = MIN(currentSegmentLength, srcPatternLength); if ((size_t)longest < maxML) { assert(base + matchIndex < ip); - if (ip - (base+matchIndex) > MAX_DISTANCE) break; + if (ip - (base+matchIndex) > LZ4_DISTANCE_MAX) break; assert(maxML < 2 GB); longest = (int)maxML; *matchpos = base + matchIndex; /* virtual pos, relative to ip, to retrieve offset */ @@ -359,12 +359,12 @@ LZ4HC_InsertAndGetWiderMatch ( if ( dict == usingDictCtxHc && nbAttempts - && ipIndex - lowestMatchIndex < MAX_DISTANCE) { + && ipIndex - lowestMatchIndex < LZ4_DISTANCE_MAX) { size_t const dictEndOffset = (size_t)(dictCtx->end - dictCtx->base); U32 dictMatchIndex = dictCtx->hashTable[LZ4HC_hashPtr(ip)]; assert(dictEndOffset <= 1 GB); matchIndex = dictMatchIndex + lowestMatchIndex - (U32)dictEndOffset; - while (ipIndex - matchIndex <= MAX_DISTANCE && nbAttempts--) { + while (ipIndex - matchIndex <= LZ4_DISTANCE_MAX && nbAttempts--) { const BYTE* const matchPtr = dictCtx->base + dictMatchIndex; if (LZ4_read32(matchPtr) == pattern) { @@ -453,7 +453,7 @@ LZ4_FORCE_INLINE int LZ4HC_encodeSequence ( *op += length; /* Encode Offset */ - assert( (*ip - match) <= MAX_DISTANCE ); /* note : consider providing offset as a value, rather than as a pointer difference */ + assert( (*ip - match) <= LZ4_DISTANCE_MAX ); /* note : consider providing offset as a value, rather than as a pointer difference */ LZ4_writeLE16(*op, (U16)(*ip-match)); *op += 2; /* Encode MatchLength */ @@ -1435,7 +1435,7 @@ static int LZ4HC_compress_optimal ( LZ4HC_CCtx_internal* ctx, if (ml == 1) { ip++; rPos++; continue; } /* literal; note: can end up with several literals, in which case, skip them */ rPos += ml; assert(ml >= MINMATCH); - assert((offset >= 1) && (offset <= MAX_DISTANCE)); + assert((offset >= 1) && (offset <= LZ4_DISTANCE_MAX)); opSaved = op; if ( LZ4HC_encodeSequence(UPDATABLE(ip, op, anchor), ml, ip - offset, limit, oend) ) /* updates ip, op and anchor */ goto _dest_overflow; diff --git a/programs/Makefile b/programs/Makefile index af461fe..92fd683 100644 --- a/programs/Makefile +++ b/programs/Makefile @@ -94,7 +94,7 @@ lz4.1: lz4.1.md $(LIBVER_SRC) man: lz4.1 clean-man: - rm lz4.1 + $(RM) lz4.1 preview-man: clean-man man man ./lz4.1 -- cgit v1.2.3 From bcb26565a732062759a2428e167e27ad3c2f6e96 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 11 Apr 2019 15:19:48 -0700 Subject: improved documentation for LZ4 dictionary compression --- README.md | 8 +++++--- doc/lz4_manual.html | 11 ++++++++--- doc/lz4frame_manual.html | 2 +- lib/lz4.h | 11 ++++++++--- lib/lz4frame.h | 20 +++++++++++++++++++- programs/README.md | 13 +++++++++---- 6 files changed, 50 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index e64020d..4e139e3 100644 --- a/README.md +++ b/README.md @@ -15,9 +15,11 @@ trading CPU time for improved compression ratio. All versions feature the same decompression speed. LZ4 is also compatible with [dictionary compression](https://github.com/facebook/zstd#the-case-for-small-data-compression), -and can ingest any input file as dictionary, -including those created by [Zstandard Dictionary Builder](https://github.com/facebook/zstd/blob/v1.3.5/programs/zstd.1.md#dictionary-builder). -(note: only the final 64KB are used). +both at [API](https://github.com/lz4/lz4/blob/v1.8.3/lib/lz4frame.h#L481) and [CLI](https://github.com/lz4/lz4/blob/v1.8.3/programs/lz4.1.md#operation-modifiers) levels. +It can ingest any input file as dictionary, though only the final 64KB are used. +This capability can be combined with the [Zstandard Dictionary Builder](https://github.com/facebook/zstd/blob/v1.3.5/programs/zstd.1.md#dictionary-builder), +in order to drastically improve compression performance on small files. + LZ4 library is provided as open-source software using BSD 2-Clause license. diff --git a/doc/lz4_manual.html b/doc/lz4_manual.html index ef1a8b5..4cd21fc 100644 --- a/doc/lz4_manual.html +++ b/doc/lz4_manual.html @@ -180,10 +180,15 @@ int LZ4_compress_fast_extState (void* state, const char* src, char* dst, int src


    int LZ4_loadDict (LZ4_stream_t* streamPtr, const char* dictionary, int dictSize);
    -

    Use this function to load a static dictionary into LZ4_stream_t. - Any previous data will be forgotten, only 'dictionary' will remain in memory. +

    Use this function to reference a static dictionary into LZ4_stream_t. + The dictionary must remain available during compression. + LZ4_loadDict() triggers a reset, so any previous data will be forgotten. + The same dictionary will have to be loaded on decompression side for successful decoding. + Dictionary are useful for better compression of small data (KB range). + While LZ4 accept any input as dictionary, + results are generally better when using Zstandard's Dictionary Builder. Loading a size of 0 is allowed, and is the same as reset. - @return : dictionary size, in bytes (necessarily <= 64 KB) + @return : loaded dictionary size, in bytes (necessarily <= 64 KB)


    diff --git a/doc/lz4frame_manual.html b/doc/lz4frame_manual.html index d5496a1..914405f 100644 --- a/doc/lz4frame_manual.html +++ b/doc/lz4frame_manual.html @@ -343,7 +343,7 @@ LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_dctx* dctx);
    LZ4FLIB_STATIC_API LZ4F_CDict* LZ4F_createCDict(const void* dictBuffer, size_t dictSize);
     LZ4FLIB_STATIC_API void        LZ4F_freeCDict(LZ4F_CDict* CDict);
    -

    When compressing multiple messages / blocks with the same dictionary, it's recommended to load it just once. +

    When compressing multiple messages / blocks using the same dictionary, it's recommended to load it just once. LZ4_createCDict() will create a digested dictionary, ready to start future compression operations without startup delay. LZ4_CDict can be created once and shared by multiple threads concurrently, since its usage is read-only. `dictBuffer` can be released after LZ4_CDict creation, since its content is copied within CDict diff --git a/lib/lz4.h b/lib/lz4.h index b86417a..935b55f 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -263,10 +263,15 @@ LZ4LIB_API int LZ4_freeStream (LZ4_stream_t* streamPtr); LZ4LIB_API void LZ4_resetStream_fast (LZ4_stream_t* streamPtr); /*! LZ4_loadDict() : - * Use this function to load a static dictionary into LZ4_stream_t. - * Any previous data will be forgotten, only 'dictionary' will remain in memory. + * Use this function to reference a static dictionary into LZ4_stream_t. + * The dictionary must remain available during compression. + * LZ4_loadDict() triggers a reset, so any previous data will be forgotten. + * The same dictionary will have to be loaded on decompression side for successful decoding. + * Dictionary are useful for better compression of small data (KB range). + * While LZ4 accept any input as dictionary, + * results are generally better when using Zstandard's Dictionary Builder. * Loading a size of 0 is allowed, and is the same as reset. - * @return : dictionary size, in bytes (necessarily <= 64 KB) + * @return : loaded dictionary size, in bytes (necessarily <= 64 KB) */ LZ4LIB_API int LZ4_loadDict (LZ4_stream_t* streamPtr, const char* dictionary, int dictSize); diff --git a/lib/lz4frame.h b/lib/lz4frame.h index 2ada8b8..5c68628 100644 --- a/lib/lz4frame.h +++ b/lib/lz4frame.h @@ -524,10 +524,28 @@ LZ4FLIB_STATIC_API size_t LZ4F_getBlockSize(unsigned); /********************************** * Bulk processing dictionary API *********************************/ + +/* A Dictionary is useful for the compression of small messages (KB range). + * It dramatically improves compression efficiency. + * + * LZ4 can ingest any input as dictionary, though only the last 64 KB are useful. + * Best results are generally achieved by using Zstandard's Dictionary Builder + * to generate a high-quality dictionary from a set of samples. + * + * Loading a dictionary has a cost, since it involves construction of tables. + * The Bulk processing dictionary API makes it possible to share this cost + * over an arbitrary number of compression jobs, even concurrently, + * markedly improving compression latency for these cases. + * + * The same dictionary will have to be used on the decompression side + * for decoding to be successful. + * To help identify the correct dictionary at decoding stage, + * the frame header allows optional embedding of a dictID field. + */ typedef struct LZ4F_CDict_s LZ4F_CDict; /*! LZ4_createCDict() : - * When compressing multiple messages / blocks with the same dictionary, it's recommended to load it just once. + * When compressing multiple messages / blocks using the same dictionary, it's recommended to load it just once. * LZ4_createCDict() will create a digested dictionary, ready to start future compression operations without startup delay. * LZ4_CDict can be created once and shared by multiple threads concurrently, since its usage is read-only. * `dictBuffer` can be released after LZ4_CDict creation, since its content is copied within CDict */ diff --git a/programs/README.md b/programs/README.md index 2ad0449..23c944b 100644 --- a/programs/README.md +++ b/programs/README.md @@ -38,7 +38,9 @@ Arguments : -9 : High compression -d : decompression (default for .lz4 extension) -z : force compression + -D FILE: use FILE as dictionary -f : overwrite output without prompting + -k : preserve source files(s) (default) --rm : remove source file(s) after successful de/compression -h/-H : display help/long help and exit @@ -51,17 +53,20 @@ Advanced arguments : -m : multiple input files (implies automatic output filenames) -r : operate recursively on directories (sets also -m) -l : compress using Legacy format (Linux kernel compression) - -B# : Block size [4-7] (default : 7) + -B# : cut file into blocks of size # bytes [32+] + or predefined block size [4-7] (default: 7) -BD : Block dependency (improve compression ratio) + -BX : enable block checksum (default:disabled) --no-frame-crc : disable stream checksum (default:enabled) --content-size : compressed frame includes original size (default:not present) --[no-]sparse : sparse mode (default:enabled on file, disabled on stdout) +--favor-decSpeed: compressed files decompress faster, but are less compressed +--fast[=#]: switch to ultra fast compression level (default: 1) + Benchmark arguments : -b# : benchmark file(s), using # compression level (default : 1) -e# : test all compression levels from -bX to # (default : 1) - -i# : minimum evaluation time in seconds (default : 3s) - -B# : cut file into independent blocks of size # bytes [32+] - or predefined block size [4-7] (default: 7) + -i# : minimum evaluation time in seconds (default : 3s)``` ``` #### License -- cgit v1.2.3 From 21ff1a839afcf18e68ee7dbb0ec0d6cccb4c1be0 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 12 Apr 2019 11:27:44 -0700 Subject: updated doc to underline difference between block and frame as this is a very frequent source of confusion for new users. --- doc/lz4_manual.html | 18 +++++++++++------- lib/README.md | 9 +++++---- lib/lz4.h | 18 +++++++++++------- lib/lz4frame.h | 13 ++++++++----- programs/README.md | 14 +++++++++++--- 5 files changed, 46 insertions(+), 26 deletions(-) diff --git a/doc/lz4_manual.html b/doc/lz4_manual.html index 4cd21fc..356a60d 100644 --- a/doc/lz4_manual.html +++ b/doc/lz4_manual.html @@ -26,19 +26,23 @@ multiple GB/s per core, typically reaching RAM speed limits on multi-core systems. The LZ4 compression library provides in-memory compression and decompression functions. + It gives full buffer control to user. Compression can be done in: - a single step (described as Simple Functions) - a single step, reusing a context (described in Advanced Functions) - unbounded multiple steps (described as Streaming compression) - lz4.h provides block compression functions. It gives full buffer control to user. - Decompressing an lz4-compressed block also requires metadata (such as compressed size). - Each application is free to encode such metadata in whichever way it wants. + lz4.h generates and decodes LZ4-compressed blocks (doc/lz4_Block_format.md). + Decompressing a block requires additional metadata, such as its compressed size. + Each application is free to encode and pass such metadata in whichever way it wants. - An additional format, called LZ4 frame specification (doc/lz4_Frame_format.md), - take care of encoding standard metadata alongside LZ4-compressed blocks. - Frame format is required for interoperability. - It is delivered through a companion API, declared in lz4frame.h. + lz4.h only handle blocks, it can not generate Frames. + + Blocks are different from Frames (doc/lz4_Frame_format.md). + Frames bundle both blocks and metadata in a specified manner. + This are required for compressed data to be self-contained and portable. + Frame format is delivered through a companion API, declared in lz4frame.h. + Note that the `lz4` CLI can only manage frames.

    Version

    
    diff --git a/lib/README.md b/lib/README.md
    index c6daaea..b753195 100644
    --- a/lib/README.md
    +++ b/lib/README.md
    @@ -16,13 +16,14 @@ They generate and decode data using the [LZ4 block format].
     For more compression ratio at the cost of compression speed,
     the High Compression variant called **lz4hc** is available.
     Add files **`lz4hc.c`** and **`lz4hc.h`**.
    -This variant also depends on regular `lib/lz4.*` source files.
    +This variant also compresses data using the [LZ4 block format],
    +and depends on regular `lib/lz4.*` source files.
     
     
     #### Frame support, for interoperability
     
     In order to produce compressed data compatible with `lz4` command line utility,
    -it's necessary to encode lz4-compressed blocks using the [official interoperable frame format].
    +it's necessary to use the [official interoperable frame format].
     This format is generated and decoded automatically by the **lz4frame** library.
     Its public API is described in `lib/lz4frame.h`.
     In order to work properly, lz4frame needs all other modules present in `/lib`,
    @@ -44,7 +45,7 @@ by using build macro `LZ4_PUBLISH_STATIC_FUNCTIONS`.
     
     #### Build macros
     
    -The following build macro can be determined at compilation time :
    +The following build macro can be selected at compilation time :
     
     - `LZ4_FAST_DEC_LOOP` : this triggers the optimized decompression loop.
       This loops works great on x86/x64 cpus, and is automatically enabled on this platform.
    @@ -61,7 +62,7 @@ The following build macro can be determined at compilation time :
     
     - `LZ4_DISABLE_DEPRECATE_WARNINGS` : invoking a deprecated function will make the compiler generate a warning.
       This is meant to invite users to update their source code.
    -  Should this be a problem, it's generally to make the compiler ignore these warnings,
    +  Should this be a problem, it's generally possible to make the compiler ignore these warnings,
       for example with `-Wno-deprecated-declarations` on `gcc`,
       or `_CRT_SECURE_NO_WARNINGS` for Visual Studio.
       Another method is to define `LZ4_DISABLE_DEPRECATE_WARNINGS`
    diff --git a/lib/lz4.h b/lib/lz4.h
    index 935b55f..23b5ac5 100644
    --- a/lib/lz4.h
    +++ b/lib/lz4.h
    @@ -51,19 +51,23 @@ extern "C" {
       multiple GB/s per core, typically reaching RAM speed limits on multi-core systems.
     
       The LZ4 compression library provides in-memory compression and decompression functions.
    +  It gives full buffer control to user.
       Compression can be done in:
         - a single step (described as Simple Functions)
         - a single step, reusing a context (described in Advanced Functions)
         - unbounded multiple steps (described as Streaming compression)
     
    -  lz4.h provides block compression functions. It gives full buffer control to user.
    -  Decompressing an lz4-compressed block also requires metadata (such as compressed size).
    -  Each application is free to encode such metadata in whichever way it wants.
    +  lz4.h generates and decodes LZ4-compressed blocks (doc/lz4_Block_format.md).
    +  Decompressing a block requires additional metadata, such as its compressed size.
    +  Each application is free to encode and pass such metadata in whichever way it wants.
     
    -  An additional format, called LZ4 frame specification (doc/lz4_Frame_format.md),
    -  take care of encoding standard metadata alongside LZ4-compressed blocks.
    -  Frame format is required for interoperability.
    -  It is delivered through a companion API, declared in lz4frame.h.
    +  lz4.h only handle blocks, it can not generate Frames.
    +
    +  Blocks are different from Frames (doc/lz4_Frame_format.md).
    +  Frames bundle both blocks and metadata in a specified manner.
    +  This are required for compressed data to be self-contained and portable.
    +  Frame format is delivered through a companion API, declared in lz4frame.h.
    +  Note that the `lz4` CLI can only manage frames.
     */
     
     /*^***************************************************************
    diff --git a/lib/lz4frame.h b/lib/lz4frame.h
    index 5c68628..ca20dc9 100644
    --- a/lib/lz4frame.h
    +++ b/lib/lz4frame.h
    @@ -32,11 +32,14 @@
        - LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c
     */
     
    -/* LZ4F is a stand-alone API to create LZ4-compressed frames
    - * conformant with specification v1.6.1.
    - * It also offers streaming capabilities.
    +/* LZ4F is a stand-alone API able to create and decode LZ4 frames
    + * conformant with specification v1.6.1 in doc/lz4_Frame_format.md .
    + * Generated frames are compatible with `lz4` CLI.
    + *
    + * LZ4F also offers streaming capabilities.
    + *
      * lz4.h is not required when using lz4frame.h,
    - * except to get constant such as LZ4_VERSION_NUMBER.
    + * except to extract common constant such as LZ4_VERSION_NUMBER.
      * */
     
     #ifndef LZ4F_H_09782039843
    @@ -195,7 +198,7 @@ typedef struct {
     *  Simple compression function
     ***********************************/
     
    -LZ4FLIB_API int LZ4F_compressionLevel_max(void);
    +LZ4FLIB_API int LZ4F_compressionLevel_max(void);   /* v1.8.0+ */
     
     /*! LZ4F_compressFrameBound() :
      *  Returns the maximum possible compressed size with LZ4F_compressFrame() given srcSize and preferences.
    diff --git a/programs/README.md b/programs/README.md
    index 23c944b..c1995af 100644
    --- a/programs/README.md
    +++ b/programs/README.md
    @@ -1,18 +1,26 @@
     Command Line Interface for LZ4 library
     ============================================
     
    -Command Line Interface (CLI) can be created using the `make` command without any additional parameters.
    -There are also multiple targets that create different variations of CLI:
    +### Build
    +The Command Line Interface (CLI) can be generated
    +using the `make` command without any additional parameters.
    +
    +The `Makefile` script supports all [standard conventions](https://www.gnu.org/prep/standards/html_node/Makefile-Conventions.html),
    +including standard targets (`all`, `install`, `clean`, etc.)
    +and standard variables (`CC`, `CFLAGS`, `CPPFLAGS`, etc.).
    +
    +For advanced use cases, there are targets to different variations of the CLI:
     - `lz4` : default CLI, with a command line syntax close to gzip
     - `lz4c` : Same as `lz4` with additional support legacy lz4 commands (incompatible with gzip)
     - `lz4c32` : Same as `lz4c`, but forced to compile in 32-bits mode
     
    +The CLI generates and decodes [LZ4-compressed frames](../doc/lz4_Frame_format.md).
    +
     
     #### Aggregation of parameters
     CLI supports aggregation of parameters i.e. `-b1`, `-e18`, and `-i1` can be joined into `-b1e18i1`.
     
     
    -
     #### Benchmark in Command Line Interface
     CLI includes in-memory compression benchmark module for lz4.
     The benchmark is conducted using a given filename.
    -- 
    cgit v1.2.3
    
    
    From 352bbdb98ef6d8c5518b6c7ec7b48a40245c3a9c Mon Sep 17 00:00:00 2001
    From: Yann Collet 
    Date: Fri, 12 Apr 2019 13:15:05 -0700
    Subject: updated man page
    
    with --favor-decSpeed
    ---
     programs/lz4.1    | 14 +++++++++-----
     programs/lz4.1.md |  8 +++++++-
     2 files changed, 16 insertions(+), 6 deletions(-)
    
    diff --git a/programs/lz4.1 b/programs/lz4.1
    index f35e29d..d3406a5 100644
    --- a/programs/lz4.1
    +++ b/programs/lz4.1
    @@ -1,5 +1,5 @@
     .
    -.TH "LZ4" "1" "September 2018" "lz4 1.8.3" "User Commands"
    +.TH "LZ4" "1" "April 2019" "lz4 1.9.0" "User Commands"
     .
     .SH "NAME"
     \fBlz4\fR \- lz4, unlz4, lz4cat \- Compress or decompress \.lz4 files
    @@ -118,6 +118,14 @@ Benchmark mode, using \fB#\fR compression level\.
     Compression level, with # being any value from 1 to 12\. Higher values trade compression speed for compression ratio\. Values above 12 are considered the same as 12\. Recommended values are 1 for fast compression (default), and 9 for high compression\. Speed/compression trade\-off will vary depending on data to compress\. Decompression speed remains fast at all settings\.
     .
     .TP
    +\fB\-\-fast[=#]\fR
    +Switch to ultra\-fast compression levels\. The higher the value, the faster the compression speed, at the cost of some compression ratio\. If \fB=#\fR is not present, it defaults to \fB1\fR\. This setting overrides compression level if one was set previously\. Similarly, if a compression level is set after \fB\-\-fast\fR, it overrides it\.
    +.
    +.TP
    +\fB\-\-favor\-decSpeed\fR
    +Generate compressed data optimized for decompression speed\. Compressed data will be larger as a consequence (typically by ~0\.5%), while decompression speed will be improved by 5\-20%, depending on use cases\. This option only works in combination with very high compression levels (>=10)\.
    +.
    +.TP
     \fB\-D dictionaryName\fR
     Compress, decompress or benchmark using dictionary \fIdictionaryName\fR\. Compression and decompression must use the same dictionary to be compatible\. Using a different dictionary during decompression will either abort due to decompression error, or generate a checksum error\.
     .
    @@ -155,10 +163,6 @@ Block size [4\-7](default : 7)
     Block Dependency (improves compression ratio on small blocks)
     .
     .TP
    -\fB\-\-fast[=#]\fR
    -switch to ultra\-fast compression levels\. If \fB=#\fR is not present, it defaults to \fB1\fR\. The higher the value, the faster the compression speed, at the cost of some compression ratio\. This setting overwrites compression level if one was set previously\. Similarly, if a compression level is set after \fB\-\-fast\fR, it overrides it\.
    -.
    -.TP
     \fB\-\-[no\-]frame\-crc\fR
     Select frame checksum (default:enabled)
     .
    diff --git a/programs/lz4.1.md b/programs/lz4.1.md
    index 12b8e29..2ee40aa 100644
    --- a/programs/lz4.1.md
    +++ b/programs/lz4.1.md
    @@ -126,12 +126,18 @@ only the latest one will be applied.
       Decompression speed remains fast at all settings.
     
     * `--fast[=#]`:
    -  switch to ultra-fast compression levels.
    +  Switch to ultra-fast compression levels.
       The higher the value, the faster the compression speed, at the cost of some compression ratio.
       If `=#` is not present, it defaults to `1`.
       This setting overrides compression level if one was set previously.
       Similarly, if a compression level is set after `--fast`, it overrides it.
     
    +* `--favor-decSpeed`:
    +  Generate compressed data optimized for decompression speed.
    +  Compressed data will be larger as a consequence (typically by ~0.5%),
    +  while decompression speed will be improved by 5-20%, depending on use cases.
    +  This option only works in combination with very high compression levels (>=10).
    +
     * `-D dictionaryName`:
       Compress, decompress or benchmark using dictionary _dictionaryName_.
       Compression and decompression must use the same dictionary to be compatible.
    -- 
    cgit v1.2.3
    
    
    From 20c2a5fc946c2ad4a765c57cb9502ba7a8f87116 Mon Sep 17 00:00:00 2001
    From: Yann Collet 
    Date: Fri, 12 Apr 2019 13:27:11 -0700
    Subject: added command -BI for "Independent Blocks"
    
    This is the reverse of `-BD`, and the current default.
    
    This command can be useful to reverse a previous `-BD` command.
    It may in the future be more important
    if `lz4` switches to generating dependent blocks by default.
    ---
     programs/lz4.1    |  6 +++++-
     programs/lz4.1.md |  5 ++++-
     programs/lz4cli.c | 24 +++++++++++++-----------
     tests/Makefile    |  2 +-
     4 files changed, 23 insertions(+), 14 deletions(-)
    
    diff --git a/programs/lz4.1 b/programs/lz4.1
    index d3406a5..1576e45 100644
    --- a/programs/lz4.1
    +++ b/programs/lz4.1
    @@ -159,8 +159,12 @@ Block size [4\-7](default : 7)
     \fB\-B4\fR= 64KB ; \fB\-B5\fR= 256KB ; \fB\-B6\fR= 1MB ; \fB\-B7\fR= 4MB
     .
     .TP
    +\fB\-BI\fR
    +Produce independent blocks (default)
    +.
    +.TP
     \fB\-BD\fR
    -Block Dependency (improves compression ratio on small blocks)
    +Blocks depend on predecessors (improves compression ratio, more noticeable on small blocks)
     .
     .TP
     \fB\-\-[no\-]frame\-crc\fR
    diff --git a/programs/lz4.1.md b/programs/lz4.1.md
    index 2ee40aa..10449a0 100644
    --- a/programs/lz4.1.md
    +++ b/programs/lz4.1.md
    @@ -172,8 +172,11 @@ only the latest one will be applied.
       Block size \[4-7\](default : 7)
    `-B4`= 64KB ; `-B5`= 256KB ; `-B6`= 1MB ; `-B7`= 4MB +* `-BI`: + Produce independent blocks (default) + * `-BD`: - Block Dependency (improves compression ratio on small blocks) + Blocks depend on predecessors (improves compression ratio, more noticeable on small blocks) * `--[no-]frame-crc`: Select frame checksum (default:enabled) diff --git a/programs/lz4cli.c b/programs/lz4cli.c index 464e43b..9a7aeb4 100644 --- a/programs/lz4cli.c +++ b/programs/lz4cli.c @@ -136,7 +136,8 @@ static int usage_advanced(const char* exeName) DISPLAY( " -l : compress using Legacy format (Linux kernel compression)\n"); DISPLAY( " -B# : cut file into blocks of size # bytes [32+] \n"); DISPLAY( " or predefined block size [4-7] (default: 7) \n"); - DISPLAY( " -BD : Block dependency (improve compression ratio) \n"); + DISPLAY( " -BI : Block Independence (default) \n"); + DISPLAY( " -BD : Block dependency (improves compression ratio) \n"); DISPLAY( " -BX : enable block checksum (default:disabled) \n"); DISPLAY( "--no-frame-crc : disable stream checksum (default:enabled) \n"); DISPLAY( "--content-size : compressed frame includes original size (default:not present)\n"); @@ -250,16 +251,16 @@ static int exeNameMatch(const char* exeName, const char* test) } /*! readU32FromChar() : - @return : unsigned integer value read from input in `char` format - allows and interprets K, KB, KiB, M, MB and MiB suffix. - Will also modify `*stringPtr`, advancing it to position where it stopped reading. - Note : function result can overflow if digit string > MAX_UINT */ + * @return : unsigned integer value read from input in `char` format + * allows and interprets K, KB, KiB, M, MB and MiB suffix. + * Will also modify `*stringPtr`, advancing it to position where it stopped reading. + * Note : function result can overflow if digit string > MAX_UINT */ static unsigned readU32FromChar(const char** stringPtr) { unsigned result = 0; while ((**stringPtr >='0') && (**stringPtr <='9')) { result *= 10; - result += **stringPtr - '0'; + result += (unsigned)(**stringPtr - '0'); (*stringPtr)++ ; } if ((**stringPtr=='K') || (**stringPtr=='M')) { @@ -277,7 +278,7 @@ static unsigned readU32FromChar(const char** stringPtr) * If yes, @return 1 and advances *stringPtr to the position which immediately follows longCommand. * @return 0 and doesn't modify *stringPtr otherwise. */ -static unsigned longCommandWArg(const char** stringPtr, const char* longCommand) +static int longCommandWArg(const char** stringPtr, const char* longCommand) { size_t const comSize = strlen(longCommand); int const result = !strncmp(*stringPtr, longCommand, comSize); @@ -316,7 +317,7 @@ int main(int argc, const char** argv) const char* output_filename= NULL; const char* dictionary_filename = NULL; char* dynNameSpace = NULL; - const char** inFileNames = (const char**) calloc(argc, sizeof(char*)); + const char** inFileNames = (const char**)calloc((size_t)argc, sizeof(char*)); unsigned ifnIdx=0; LZ4IO_prefs_t* const prefs = LZ4IO_defaultPreferences(); const char nullOutput[] = NULL_OUTPUT; @@ -425,7 +426,7 @@ int main(int argc, const char** argv) } if ((*argument>='0') && (*argument<='9')) { - cLevel = readU32FromChar(&argument); + cLevel = (int)readU32FromChar(&argument); argument--; continue; } @@ -440,7 +441,7 @@ int main(int argc, const char** argv) case 'e': argument++; - cLevelLast = readU32FromChar(&argument); + cLevelLast = (int)readU32FromChar(&argument); argument--; break; @@ -498,6 +499,7 @@ int main(int argc, const char** argv) switch(argument[1]) { case 'D': LZ4IO_setBlockMode(prefs, LZ4IO_blockLinked); argument++; break; + case 'I': LZ4IO_setBlockMode(prefs, LZ4IO_blockIndependent); argument++; break; case 'X': LZ4IO_setBlockChecksumMode(prefs, 1); argument ++; break; /* disabled by default */ default : if (argument[1] < '0' || argument[1] > '9') { @@ -696,7 +698,7 @@ int main(int argc, const char** argv) } /* IO Stream/File */ - LZ4IO_setNotificationLevel(displayLevel); + LZ4IO_setNotificationLevel((int)displayLevel); if (ifnIdx == 0) multiple_inputs = 0; if (mode == om_decompress) { if (multiple_inputs) diff --git a/tests/Makefile b/tests/Makefile index 792c22c..8e154f7 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -248,7 +248,7 @@ test-lz4-basic: lz4 datagen unlz4 lz4cat $(DIFF) -q tmp-tlb-dg20k tmp-tlb-dec $(LZ4) --no-frame-crc < tmp-tlb-dg20k | $(LZ4) -d > tmp-tlb-dec $(DIFF) -q tmp-tlb-dg20k tmp-tlb-dec - ./datagen | $(LZ4) | $(LZ4) -t + ./datagen | $(LZ4) -BI | $(LZ4) -t ./datagen -g6M -P99 | $(LZ4) -9BD | $(LZ4) -t ./datagen -g17M | $(LZ4) -9v | $(LZ4) -qt ./datagen -g33M | $(LZ4) --no-frame-crc | $(LZ4) -t -- cgit v1.2.3 From e865a3e7ec36ca812d8ebb920796732eaceb5135 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 12 Apr 2019 15:17:48 -0700 Subject: fixed lz4 -m -c can compress multiple files into stdout --- programs/lz4cli.c | 2 +- programs/lz4io.c | 42 ++++++++++++++++++++++++++++++------------ programs/lz4io.h | 3 +++ tests/Makefile | 15 ++++++++++++--- 4 files changed, 46 insertions(+), 16 deletions(-) diff --git a/programs/lz4cli.c b/programs/lz4cli.c index 9a7aeb4..8bd7042 100644 --- a/programs/lz4cli.c +++ b/programs/lz4cli.c @@ -711,7 +711,7 @@ int main(int argc, const char** argv) LZ4IO_compressFilename_Legacy(prefs, input_filename, output_filename, cLevel); } else { if (multiple_inputs) - operationResult = LZ4IO_compressMultipleFilenames(prefs, inFileNames, ifnIdx, LZ4_EXTENSION, cLevel); + operationResult = LZ4IO_compressMultipleFilenames(prefs, inFileNames, ifnIdx, !strcmp(output_filename,stdoutmark) ? stdoutmark : LZ4_EXTENSION, cLevel); else operationResult = DEFAULT_COMPRESSOR(prefs, input_filename, output_filename, cLevel); } diff --git a/programs/lz4io.c b/programs/lz4io.c index 6bb6c48..bf412da 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -121,8 +121,8 @@ struct LZ4IO_prefs_s { int contentSizeFlag; int useDictionary; unsigned favorDecSpeed; - char const* dictionaryFilename; - U32 removeSrcFile; + const char* dictionaryFilename; + int removeSrcFile; }; /************************************** @@ -323,11 +323,12 @@ static FILE* LZ4IO_openSrcFile(const char* srcFileName) } /** FIO_openDstFile() : - * condition : `dstFileName` must be non-NULL. + * condition : `dstFileName` must be non-NULL. * @result : FILE* to `dstFileName`, or NULL if it fails */ static FILE* LZ4IO_openDstFile(LZ4IO_prefs_t* const prefs, const char* dstFileName) { FILE* f; + assert(dstFileName != NULL); if (!strcmp (dstFileName, stdoutmark)) { DISPLAYLEVEL(4,"Using stdout for output\n"); @@ -595,7 +596,10 @@ static void LZ4IO_freeCResources(cRess_t ress) * result : 0 : compression completed correctly * 1 : missing or pb opening srcFileName */ -static int LZ4IO_compressFilename_extRess(LZ4IO_prefs_t* const io_prefs, cRess_t ress, const char* srcFileName, const char* dstFileName, int compressionLevel) +static int +LZ4IO_compressFilename_extRess(LZ4IO_prefs_t* const io_prefs, cRess_t ress, + const char* srcFileName, const char* dstFileName, + int compressionLevel) { unsigned long long filesize = 0; unsigned long long compressedfilesize = 0; @@ -691,9 +695,9 @@ static int LZ4IO_compressFilename_extRess(LZ4IO_prefs_t* const io_prefs, cRess_t compressedfilesize += headerSize; } - /* Release files */ + /* Release file handlers */ fclose (srcFile); - fclose (dstFile); + if (strcmp(dstFileName,stdoutmark)) fclose (dstFile); /* do not close stdout */ /* Copy owner, file permissions and modification time */ { stat_t statbuf; @@ -744,7 +748,10 @@ int LZ4IO_compressFilename(LZ4IO_prefs_t* const prefs, const char* srcFileName, #define FNSPACE 30 -int LZ4IO_compressMultipleFilenames(LZ4IO_prefs_t* const prefs, const char** inFileNamesTable, int ifntSize, const char* suffix, int compressionLevel) +int LZ4IO_compressMultipleFilenames(LZ4IO_prefs_t* const prefs, + const char** inFileNamesTable, int ifntSize, + const char* suffix, + int compressionLevel) { int i; int missed_files = 0; @@ -759,11 +766,21 @@ int LZ4IO_compressMultipleFilenames(LZ4IO_prefs_t* const prefs, const char** inF /* loop on each file */ for (i=0; i= 4 */ static unsigned LZ4IO_readLE32 (const void* s) { const unsigned char* const srcPtr = (const unsigned char*)s; unsigned value32 = srcPtr[0]; - value32 += (srcPtr[1]<<8); - value32 += (srcPtr[2]<<16); - value32 += ((unsigned)srcPtr[3])<<24; + value32 += (unsigned)srcPtr[1] << 8; + value32 += (unsigned)srcPtr[2] << 16; + value32 += (unsigned)srcPtr[3] << 24; return value32; } diff --git a/programs/lz4io.h b/programs/lz4io.h index 0c28784..54d49be 100644 --- a/programs/lz4io.h +++ b/programs/lz4io.h @@ -57,13 +57,16 @@ typedef struct LZ4IO_prefs_s LZ4IO_prefs_t; LZ4IO_prefs_t* LZ4IO_defaultPreferences(void); void LZ4IO_freePreferences(LZ4IO_prefs_t* const prefs); + /* ************************************************** */ /* ****************** Functions ********************* */ /* ************************************************** */ +/* if output_filename == stdoutmark, writes to stdout */ int LZ4IO_compressFilename(LZ4IO_prefs_t* const prefs, const char* input_filename, const char* output_filename, int compressionlevel); int LZ4IO_decompressFilename(LZ4IO_prefs_t* const prefs, const char* input_filename, const char* output_filename); +/* if suffix == stdoutmark, writes to stdout */ int LZ4IO_compressMultipleFilenames(LZ4IO_prefs_t* const prefs, const char** inFileNamesTable, int ifntSize, const char* suffix, int compressionlevel); int LZ4IO_decompressMultipleFilenames(LZ4IO_prefs_t* const prefs, const char** inFileNamesTable, int ifntSize, const char* suffix); diff --git a/tests/Makefile b/tests/Makefile index 8e154f7..8442ab8 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -232,11 +232,20 @@ test-lz4-multiple: lz4 datagen @./datagen -s2 -g100K > tmp-tlm2 2> $(VOID) @./datagen -s3 -g1M > tmp-tlm3 2> $(VOID) $(LZ4) -f -m tmp-tlm* - ls -ls tmp-tlm* + test -f tmp-tlm1.lz4 + test -f tmp-tlm2.lz4 + test -f tmp-tlm3.lz4 @$(RM) tmp-tlm1 tmp-tlm2 tmp-tlm3 $(LZ4) -df -m tmp-tlm*.lz4 - ls -ls tmp-tlm* - $(LZ4) -f -m tmp-tlm1 notHere tmp-tlm2; echo $$? + test -f tmp-tlm1 + test -f tmp-tlm2 + test -f tmp-tlm3 + cat tmp-tlm1.lz4 tmp-tlm2.lz4 tmp-tlm3.lz4 > tmp-tlm-concat1 + $(RM) *.lz4 + $(LZ4) -f -m tmp-tlm* -c > tmp-tlm-concat2 + test ! -f tmp-tlm1.lz4 # must not create .lz4 artefact + sdiff tmp-tlm-concat1 tmp-tlm-concat2 # must be equivalent + ! $(LZ4) -f -m tmp-tlm1 notHere tmp-tlm2 # must fail : notHere not present @$(RM) tmp-tlm* test-lz4-basic: lz4 datagen unlz4 lz4cat -- cgit v1.2.3 From 9c49e3ca07673949c58d9cffb23034ad0865fefb Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 12 Apr 2019 15:55:38 -0700 Subject: added tests for -d -m -c seems to work properly --- .gitignore | 3 +++ programs/lz4io.c | 11 ++++++++--- tests/Makefile | 34 ++++++++++++++++++++++++---------- 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index 829270b..2a59a7d 100644 --- a/.gitignore +++ b/.gitignore @@ -33,3 +33,6 @@ bin/ # Windows / Msys nul ld.exe* + +# test files +*.lz4 diff --git a/programs/lz4io.c b/programs/lz4io.c index bf412da..121bd44 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -766,6 +766,12 @@ int LZ4IO_compressMultipleFilenames(LZ4IO_prefs_t* const prefs, /* loop on each file */ for (i=0; i tmp-lfc-nonempty.lz4 cat tmp-lfc-nonempty.lz4 tmp-lfc-empty.lz4 tmp-lfc-nonempty.lz4 > tmp-lfc-concat.lz4 $(LZ4) -d tmp-lfc-concat.lz4 > tmp-lfc-result - sdiff tmp-lfc-src tmp-lfc-result + cmp tmp-lfc-src tmp-lfc-result @$(RM) tmp-lfc-* @echo frame concatenation test completed @@ -230,22 +230,36 @@ test-lz4-multiple: lz4 datagen @echo "\n ---- test multiple files ----" @./datagen -s1 > tmp-tlm1 2> $(VOID) @./datagen -s2 -g100K > tmp-tlm2 2> $(VOID) - @./datagen -s3 -g1M > tmp-tlm3 2> $(VOID) + @./datagen -s3 -g200K > tmp-tlm3 2> $(VOID) + # compress multiple files : one .lz4 per source file $(LZ4) -f -m tmp-tlm* test -f tmp-tlm1.lz4 test -f tmp-tlm2.lz4 test -f tmp-tlm3.lz4 - @$(RM) tmp-tlm1 tmp-tlm2 tmp-tlm3 - $(LZ4) -df -m tmp-tlm*.lz4 - test -f tmp-tlm1 - test -f tmp-tlm2 - test -f tmp-tlm3 + # decompress multiple files : one output file per .lz4 + mv tmp-tlm1 tmp-tlm1-orig + mv tmp-tlm2 tmp-tlm2-orig + mv tmp-tlm3 tmp-tlm3-orig + $(LZ4) -d -f -m tmp-tlm*.lz4 + cmp tmp-tlm1 tmp-tlm1-orig # must be identical + cmp tmp-tlm2 tmp-tlm2-orig + cmp tmp-tlm3 tmp-tlm3-orig + # compress multiple files into stdout cat tmp-tlm1.lz4 tmp-tlm2.lz4 tmp-tlm3.lz4 > tmp-tlm-concat1 $(RM) *.lz4 - $(LZ4) -f -m tmp-tlm* -c > tmp-tlm-concat2 + $(LZ4) -m tmp-tlm1 tmp-tlm2 tmp-tlm3 -c > tmp-tlm-concat2 test ! -f tmp-tlm1.lz4 # must not create .lz4 artefact - sdiff tmp-tlm-concat1 tmp-tlm-concat2 # must be equivalent - ! $(LZ4) -f -m tmp-tlm1 notHere tmp-tlm2 # must fail : notHere not present + cmp tmp-tlm-concat1 tmp-tlm-concat2 # must be equivalent + # decompress multiple files into stdout + $(RM) tmp-tlm-concat1 tmp-tlm-concat2 + $(LZ4) -f -m tmp-tlm1 tmp-tlm2 tmp-tlm3 # generate .lz4 to decompress + cat tmp-tlm1 tmp-tlm2 tmp-tlm3 > tmp-tlm-concat1 # create concatenated reference + $(RM) tmp-tlm1 tmp-tlm2 tmp-tlm3 + $(LZ4) -d -m tmp-tlm1.lz4 tmp-tlm2.lz4 tmp-tlm3.lz4 -c > tmp-tlm-concat2 + test ! -f tmp-tlm1 # must not create file artefact + cmp tmp-tlm-concat1 tmp-tlm-concat2 # must be equivalent + # compress multiple files, one of which is absent (must fail) + ! $(LZ4) -f -m tmp-tlm-concat1 notHere tmp-tlm-concat2 # must fail : notHere not present @$(RM) tmp-tlm* test-lz4-basic: lz4 datagen unlz4 lz4cat -- cgit v1.2.3 From f8b760503445610c43cad813fb3eda29bb3fc763 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 12 Apr 2019 16:49:01 -0700 Subject: fixed minor Visual warnings since Visual 2017, worries about potential overflow, which are actually impossible. Replaced (c * a) by (c ? a : 0). Will likely replaced a * by a cmov. Probably harmless for performance. --- lib/lz4.c | 4 ++-- lib/lz4frame.c | 24 ++++++++++++------------ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index ca3684f..031f8c1 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -983,7 +983,7 @@ _next_match: assert(dictEnd > match); if (limit > matchlimit) limit = matchlimit; matchCode = LZ4_count(ip+MINMATCH, match+MINMATCH, limit); - ip += MINMATCH + matchCode; + ip += (size_t)matchCode + MINMATCH; if (ip==limit) { unsigned const more = LZ4_count(limit, (const BYTE*)source, matchlimit); matchCode += more; @@ -992,7 +992,7 @@ _next_match: DEBUGLOG(6, " with matchLength=%u starting in extDict", matchCode+MINMATCH); } else { matchCode = LZ4_count(ip+MINMATCH, match+MINMATCH, matchlimit); - ip += MINMATCH + matchCode; + ip += (size_t)matchCode + MINMATCH; DEBUGLOG(6, " with matchLength=%u", matchCode+MINMATCH); } diff --git a/lib/lz4frame.c b/lib/lz4frame.c index 2b31a0d..b6bc218 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -194,10 +194,10 @@ static void LZ4F_writeLE64 (void* dst, U64 value64) /*-************************************ * Constants **************************************/ -#ifndef LZ4_SRC_INCLUDED -#define KB *(1<<10) -#define MB *(1<<20) -#define GB *(1<<30) +#ifndef LZ4_SRC_INCLUDED /* avoid double definition */ +# define KB *(1<<10) +# define MB *(1<<20) +# define GB *(1<<30) #endif #define _1BIT 0x01 @@ -644,8 +644,8 @@ size_t LZ4F_compressBegin_usingCDict(LZ4F_cctx* cctxPtr, cctxPtr->maxBlockSize = LZ4F_getBlockSize(cctxPtr->prefs.frameInfo.blockSizeID); { size_t const requiredBuffSize = preferencesPtr->autoFlush ? - (cctxPtr->prefs.frameInfo.blockMode == LZ4F_blockLinked) * 64 KB : /* only needs windows size */ - cctxPtr->maxBlockSize + ((cctxPtr->prefs.frameInfo.blockMode == LZ4F_blockLinked) * 128 KB); + ((cctxPtr->prefs.frameInfo.blockMode == LZ4F_blockLinked) ? 64 KB : 0) : /* only needs past data up to window size */ + cctxPtr->maxBlockSize + ((cctxPtr->prefs.frameInfo.blockMode == LZ4F_blockLinked) ? 128 KB : 0); if (cctxPtr->maxBufferSize < requiredBuffSize) { cctxPtr->maxBufferSize = 0; @@ -1146,7 +1146,7 @@ static size_t LZ4F_decodeHeader(LZ4F_dctx* dctx, const void* src, size_t srcSize } /* Frame Header Size */ - frameHeaderSize = minFHSize + (contentSizeFlag*8) + (dictIDFlag*4); + frameHeaderSize = minFHSize + (contentSizeFlag?8:0) + (dictIDFlag?4:0); if (srcSize < frameHeaderSize) { /* not enough input to fully decode frame header */ @@ -1215,7 +1215,7 @@ size_t LZ4F_headerSize(const void* src, size_t srcSize) { BYTE const FLG = ((const BYTE*)src)[4]; U32 const contentSizeFlag = (FLG>>3) & _1BIT; U32 const dictIDFlag = FLG & _1BIT; - return minFHSize + (contentSizeFlag*8) + (dictIDFlag*4); + return minFHSize + (contentSizeFlag?8:0) + (dictIDFlag?4:0); } } @@ -1421,11 +1421,11 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, if (dctx->frameInfo.contentChecksumFlag) (void)XXH32_reset(&(dctx->xxh), 0); /* internal buffers allocation */ { size_t const bufferNeeded = dctx->maxBlockSize - + ((dctx->frameInfo.blockMode==LZ4F_blockLinked) * 128 KB); + + ((dctx->frameInfo.blockMode==LZ4F_blockLinked) ? 128 KB : 0); if (bufferNeeded > dctx->maxBufferSize) { /* tmp buffers too small */ dctx->maxBufferSize = 0; /* ensure allocation will be re-attempted on next entry*/ FREEMEM(dctx->tmpIn); - dctx->tmpIn = (BYTE*)ALLOC(dctx->maxBlockSize + 4 /* block checksum */); + dctx->tmpIn = (BYTE*)ALLOC(dctx->maxBlockSize + BFSize /* block checksum */); if (dctx->tmpIn == NULL) return err0r(LZ4F_ERROR_allocation_failed); FREEMEM(dctx->tmpOutBuffer); @@ -1526,7 +1526,7 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, } dctx->tmpInTarget -= sizeToCopy; /* need to copy more */ nextSrcSizeHint = dctx->tmpInTarget + - + dctx->frameInfo.blockChecksumFlag * 4 /* size for block checksum */ + +(dctx->frameInfo.blockChecksumFlag ? BFSize : 0) + BHSize /* next header size */; doAnotherStage = 0; break; @@ -1578,7 +1578,7 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, srcPtr += sizeToCopy; if (dctx->tmpInSize < dctx->tmpInTarget) { /* need more input */ nextSrcSizeHint = (dctx->tmpInTarget - dctx->tmpInSize) - + dctx->frameInfo.blockChecksumFlag * 4 /* size for block checksum */ + + (dctx->frameInfo.blockChecksumFlag ? BFSize : 0) + BHSize /* next header size */; doAnotherStage = 0; break; -- cgit v1.2.3 From dd43b913a292e5404aa8bc981ad11223f216d898 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 12 Apr 2019 16:56:22 -0700 Subject: fix minor visual warning yet some overly cautious overflow risk flag, while it's actually impossible, due to previous test just one line above. Changing the cast position, just to be please the thing. --- lib/lz4hc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/lz4hc.c b/lib/lz4hc.c index a6dc7a2..4e3573a 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -458,7 +458,7 @@ LZ4_FORCE_INLINE int LZ4HC_encodeSequence ( /* Encode MatchLength */ assert(matchLength >= MINMATCH); - length = (size_t)(matchLength - MINMATCH); + length = (size_t)matchLength - MINMATCH; if ((limit) && (*op + (length / 255) + (1 + LASTLITERALS) > oend)) return 1; /* Check output limit */ if (length >= ML_MASK) { *token += ML_MASK; @@ -979,7 +979,7 @@ int LZ4_loadDictHC (LZ4_streamHC_t* LZ4_streamHCPtr, DEBUGLOG(4, "LZ4_loadDictHC(%p, %p, %d)", LZ4_streamHCPtr, dictionary, dictSize); assert(LZ4_streamHCPtr != NULL); if (dictSize > 64 KB) { - dictionary += dictSize - 64 KB; + dictionary += (size_t)dictSize - 64 KB; dictSize = 64 KB; } /* need a full initialization, there are bad side-effects when using resetFast() */ -- cgit v1.2.3 From c7554c3004e11b3d706143917f33783b1a2e9a29 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 12 Apr 2019 17:03:28 -0700 Subject: fixed minor Visual conversion warnings --- programs/util.h | 12 ++++++------ tests/fullbench.c | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/programs/util.h b/programs/util.h index 4aba6a3..1385620 100644 --- a/programs/util.h +++ b/programs/util.h @@ -391,11 +391,11 @@ UTIL_STATIC void* UTIL_realloc(void* ptr, size_t size) UTIL_STATIC int UTIL_prepareFileList(const char* dirName, char** bufStart, size_t* pos, char** bufEnd) { char* path; - int dirLength, nbFiles = 0; + size_t dirLength, nbFiles = 0; WIN32_FIND_DATAA cFile; HANDLE hFile; - dirLength = (int)strlen(dirName); + dirLength = strlen(dirName); path = (char*) malloc(dirLength + 3); if (!path) return 0; @@ -412,7 +412,7 @@ UTIL_STATIC int UTIL_prepareFileList(const char* dirName, char** bufStart, size_ free(path); do { - int pathLength; + size_t pathLength; int const fnameLength = (int)strlen(cFile.cFileName); path = (char*) malloc(dirLength + fnameLength + 2); if (!path) { FindClose(hFile); return 0; } @@ -553,15 +553,15 @@ UTIL_createFileList(const char** inputNames, unsigned inputNamesNb, char** alloc } } else { char* bufend = buf + bufSize; - nbFiles += UTIL_prepareFileList(inputNames[i], &buf, &pos, &bufend); + nbFiles += (unsigned)UTIL_prepareFileList(inputNames[i], &buf, &pos, &bufend); if (buf == NULL) return NULL; assert(bufend > buf); - bufSize = bufend - buf; + bufSize = (size_t)(bufend - buf); } } if (nbFiles == 0) { free(buf); return NULL; } - fileTable = (const char**)malloc((nbFiles+1) * sizeof(const char*)); + fileTable = (const char**)malloc(((size_t)nbFiles+1) * sizeof(const char*)); if (!fileTable) { free(buf); return NULL; } for (i=0, pos=0; i Date: Fri, 12 Apr 2019 17:06:53 -0700 Subject: some more Visual conversion warning --- programs/util.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/programs/util.h b/programs/util.h index 1385620..85ac87f 100644 --- a/programs/util.h +++ b/programs/util.h @@ -45,6 +45,7 @@ extern "C" { # include /* utime */ #endif #include /* time */ +#include /* INT_MAX */ #include @@ -445,7 +446,8 @@ UTIL_STATIC int UTIL_prepareFileList(const char* dirName, char** bufStart, size_ } while (FindNextFileA(hFile, &cFile)); FindClose(hFile); - return nbFiles; + assert(nbFiles < INT_MAX); + return (int)nbFiles; } #elif defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L) /* opendir, readdir require POSIX.1-2001 */ -- cgit v1.2.3 From 63bfb64b80a6b9b310961c04cadd5242c81df0c8 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 12 Apr 2019 17:40:23 -0700 Subject: and even more visual conversion warnings --- programs/util.h | 6 +++++- tests/fuzzer.c | 20 +++++++++++--------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/programs/util.h b/programs/util.h index 85ac87f..6a35481 100644 --- a/programs/util.h +++ b/programs/util.h @@ -571,7 +571,11 @@ UTIL_createFileList(const char** inputNames, unsigned inputNamesNb, char** alloc pos += strlen(fileTable[i]) + 1; } - if (pos > bufSize) { free(buf); free((void*)fileTable); return NULL; } /* can this happen ? */ + if (pos > bufSize) { + free(buf); + free((void*)fileTable); + return NULL; + } /* can this happen ? */ *allocatedBuffer = buf; *allocatedNamesNb = nbFiles; diff --git a/tests/fuzzer.c b/tests/fuzzer.c index 27d8d9c..8e25615 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -145,10 +145,10 @@ static void FUZ_fillCompressibleNoiseBuffer(void* buffer, size_t bufferSize, dou /* Select : Literal (noise) or copy (within 64K) */ if (FUZ_RAND15BITS < P32) { /* Copy (within 64K) */ - size_t const length = FUZ_RANDLENGTH + 4; + size_t const length = (size_t)FUZ_RANDLENGTH + 4; size_t const d = MIN(pos+length, bufferSize); size_t match; - size_t offset = FUZ_RAND15BITS + 1; + size_t offset = (size_t)FUZ_RAND15BITS + 1; while (offset > pos) offset >>= 1; match = pos - offset; while (pos < d) BBuffer[pos++] = BBuffer[match++]; @@ -667,7 +667,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c /* Compress using External dictionary */ FUZ_DISPLAYTEST("test LZ4_compress_fast_continue(), with non-contiguous dictionary"); - dict -= (FUZ_rand(&randState) & 0xF) + 1; /* create space, so now dictionary is an ExtDict */ + dict -= (size_t)(FUZ_rand(&randState) & 0xF) + 1; /* create space, so now dictionary is an ExtDict */ if (dict < (char*)CNBuffer) dict = (char*)CNBuffer; LZ4_loadDict(&LZ4dict, dict, dictSize); blockContinueCompressedSize = LZ4_compress_fast_continue(&LZ4dict, block, compressedBuffer, blockSize, (int)compressedBufferSize, 1); @@ -1145,7 +1145,7 @@ static void FUZ_unitTests(int compressionLevel) result1 = LZ4_compress_HC_continue(&sHC, testInput + segSize, testCompressed, segSize, segSize -1); FUZ_CHECKTEST(result1==0, "LZ4_compressHC_limitedOutput_continue() dictionary compression failed : result = %i", result1); FUZ_CHECKTEST(sHC.internal_donotuse.dirty, "Context should be clean"); - result2 = LZ4_compress_HC_continue(&sHC, testInput + 2*segSize, testCompressed+result1, segSize, segSize-1); + result2 = LZ4_compress_HC_continue(&sHC, testInput + 2*(size_t)segSize, testCompressed+result1, segSize, segSize-1); FUZ_CHECKTEST(result2==0, "LZ4_compressHC_limitedOutput_continue() dictionary compression failed : result = %i", result2); FUZ_CHECKTEST(sHC.internal_donotuse.dirty, "Context should be clean"); @@ -1178,7 +1178,7 @@ static void FUZ_unitTests(int compressionLevel) int dictSize = (FUZ_rand(&randState) & 8191); char* dst = testVerify; - size_t segStart = dictSize + 7; + size_t segStart = (size_t)dictSize + 7; int segSize = (FUZ_rand(&randState) & 8191); int segNb = 1; @@ -1203,13 +1203,14 @@ static void FUZ_unitTests(int compressionLevel) FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe_usingDict() part %i corruption", segNb); } + assert(segSize >= 0); dict = dst; dictSize = segSize; - dst += segSize + 1; + dst += (size_t)segSize + 1; segNb ++; - segStart += segSize + (FUZ_rand(&randState) & 0xF) + 1; + segStart += (size_t)segSize + (FUZ_rand(&randState) & 0xF) + 1; segSize = (FUZ_rand(&randState) & 8191); } } @@ -1288,7 +1289,7 @@ static void FUZ_unitTests(int compressionLevel) int dNext = 0; int compressedSize; - assert((size_t)(dBufferSize + 1 + dBufferSize) < testVerifySize); /* space used by ringBufferSafe and ringBufferFast */ + assert((size_t)dBufferSize * 2 + 1 < testVerifySize); /* space used by ringBufferSafe and ringBufferFast */ XXH64_reset(&xxhOrig, 0); XXH64_reset(&xxhNewSafe, 0); XXH64_reset(&xxhNewFast, 0); @@ -1323,7 +1324,8 @@ static void FUZ_unitTests(int compressionLevel) /* prepare second message */ dNext += messageSize; - totalMessageSize += messageSize; + assert(messageSize >= 0); + totalMessageSize += (unsigned)messageSize; messageSize = maxMessageSize; iNext = BSIZE1+1; assert(BSIZE1 >= 65535); -- cgit v1.2.3 From 31763c59fca9a38bc697cd37f591d6ba385f7614 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 12 Apr 2019 18:00:43 -0700 Subject: updated benchmark table --- README.md | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 4e139e3..fd58573 100644 --- a/README.md +++ b/README.md @@ -50,8 +50,8 @@ Benchmarks ------------------------- The benchmark uses [lzbench], from @inikep -compiled with GCC v7.3.0 on Linux 64-bits (Debian 4.15.17-1). -The reference system uses a Core i7-6700K CPU @ 4.0GHz. +compiled with GCC v8.2.0 on Linux 64-bits (Ubuntu 4.18.0-17). +The reference system uses a Core i7-9700K CPU @ 4.9GHz. Benchmark evaluates the compression of reference [Silesia Corpus] in single-thread mode. @@ -60,16 +60,16 @@ in single-thread mode. | Compressor | Ratio | Compression | Decompression | | ---------- | ----- | ----------- | ------------- | -| memcpy | 1.000 |13100 MB/s | 13100 MB/s | -|**LZ4 default (v1.8.2)** |**2.101**|**730 MB/s** | **3900 MB/s** | -| LZO 2.09 | 2.108 | 630 MB/s | 800 MB/s | -| QuickLZ 1.5.0 | 2.238 | 530 MB/s | 720 MB/s | -| Snappy 1.1.4 | 2.091 | 525 MB/s | 1750 MB/s | -| [Zstandard] 1.3.4 -1 | 2.877 | 470 MB/s | 1380 MB/s | -| LZF v3.6 | 2.073 | 380 MB/s | 840 MB/s | -| [zlib] deflate 1.2.11 -1| 2.730 | 100 MB/s | 380 MB/s | -|**LZ4 HC -9 (v1.8.2)** |**2.721**| 40 MB/s | **3920 MB/s** | -| [zlib] deflate 1.2.11 -6| 3.099 | 34 MB/s | 410 MB/s | +| memcpy | 1.000 | 13700 MB/s | 13700 MB/s | +|**LZ4 default (v1.9.0)** |**2.101**| **780 MB/s**| **4900 MB/s** | +| LZO 2.09 | 2.108 | 670 MB/s | 860 MB/s | +| QuickLZ 1.5.0 | 2.238 | 575 MB/s | 780 MB/s | +| Snappy 1.1.4 | 2.091 | 565 MB/s | 1950 MB/s | +| [Zstandard] 1.4.0 -1 | 2.883 | 515 MB/s | 1380 MB/s | +| LZF v3.6 | 2.073 | 415 MB/s | 910 MB/s | +| [zlib] deflate 1.2.11 -1| 2.730 | 100 MB/s | 415 MB/s | +|**LZ4 HC -9 (v1.8.2)** |**2.721**| 39 MB/s | **4850 MB/s** | +| [zlib] deflate 1.2.11 -6| 3.099 | 36 MB/s | 445 MB/s | [zlib]: http://www.zlib.net/ [Zstandard]: http://www.zstd.net/ -- cgit v1.2.3 From 749c3400bd6ff6b49641fadaf0edb23d7a7b6413 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sat, 13 Apr 2019 15:55:06 -0700 Subject: fixed incorrect assertion condition output can use the full length of output buffer --- lib/lz4frame.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/lz4frame.c b/lib/lz4frame.c index 2b31a0d..4c5c1a8 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -956,7 +956,7 @@ size_t LZ4F_flush(LZ4F_cctx* cctxPtr, compress, cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel, cctxPtr->cdict, cctxPtr->prefs.frameInfo.blockChecksumFlag); - assert(((void)"flush overflows dstBuffer!", (size_t)(dstPtr - dstStart) < dstCapacity)); + assert(((void)"flush overflows dstBuffer!", (size_t)(dstPtr - dstStart) <= dstCapacity)); if (cctxPtr->prefs.frameInfo.blockMode == LZ4F_blockLinked) cctxPtr->tmpIn += cctxPtr->tmpInSize; -- cgit v1.2.3 From c222dd9010377efb7b2e87655fbec94a5b47f549 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 15 Apr 2019 01:54:40 -0700 Subject: fixed frametest One test could write a canary value out of bound in exceptional conditions involving multiple flushes triggered by -s3421 -t462948. --- tests/frametest.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/tests/frametest.c b/tests/frametest.c index 59e866c..e4205a3 100644 --- a/tests/frametest.c +++ b/tests/frametest.c @@ -787,7 +787,7 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi unsigned testNb = 0; size_t const srcDataLength = 9 MB; /* needs to be > 2x4MB to test large blocks */ void* srcBuffer = NULL; - size_t const compressedBufferSize = LZ4F_compressFrameBound(srcDataLength, NULL); + size_t const compressedBufferSize = LZ4F_compressFrameBound(srcDataLength, NULL) + 64 KB; /* needs some margin for some rare exceptional cases involving multiple flushes */ void* compressedBuffer = NULL; void* decodedBuffer = NULL; U32 coreRand = seed; @@ -801,10 +801,10 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); goto _output_error; } /* 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); + { size_t const creationStatus = LZ4F_createDecompressionContext(&dCtx, LZ4F_VERSION); + CHECK(LZ4F_isError(creationStatus), "Allocation failed (error %i)", (int)creationStatus); } + { size_t const creationStatus = LZ4F_createCompressionContext(&cCtx, LZ4F_VERSION); + CHECK(LZ4F_isError(creationStatus), "Allocation failed (error %i)", (int)creationStatus); } srcBuffer = malloc(srcDataLength); CHECK(srcBuffer==NULL, "srcBuffer Allocation failed"); compressedBuffer = malloc(compressedBufferSize); @@ -859,7 +859,7 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi unsigned const maxBits = FUZ_highbit((U32)srcSize); LZ4F_compressOptions_t cOptions; memset(&cOptions, 0, sizeof(cOptions)); - result = LZ4F_compressBegin(cCtx, op, oend-op, prefsPtr); + result = LZ4F_compressBegin(cCtx, op, (size_t)(oend-op), prefsPtr); CHECK(LZ4F_isError(result), "Compression header failed (error %i)", (int)result); op += result; while (ip < iend) { @@ -878,9 +878,10 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi { unsigned const forceFlush = neverFlush ? 0 : ((FUZ_rand(&randState) & 3) == 1); if (forceFlush) { - result = LZ4F_flush(cCtx, op, oend-op, &cOptions); - CHECK(LZ4F_isError(result), "Compression failed (error %i)", (int)result); - op += result; + size_t const flushSize = LZ4F_flush(cCtx, op, (size_t)(oend-op), &cOptions); + DISPLAYLEVEL(6,"flushing %u bytes \n", (unsigned)flushSize); + CHECK(LZ4F_isError(flushSize), "Compression failed (error %i)", (int)flushSize); + op += flushSize; } } } CHECK(op>=oend, "LZ4F_compressFrameBound overflow"); @@ -889,6 +890,9 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi size_t const dstEndTooSmallSize = (FUZ_rand(&randState) % dstEndSafeSize) + 1; size_t const dstEndSize = tooSmallDstEnd ? dstEndTooSmallSize : dstEndSafeSize; BYTE const canaryByte = (BYTE)(FUZ_rand(&randState) & 255); + DISPLAYLEVEL(7,"canaryByte at pos %u / %u \n", + (unsigned)((size_t)(op - (BYTE*)compressedBuffer) + dstEndSize), + (unsigned)compressedBufferSize); op[dstEndSize] = canaryByte; result = LZ4F_compressEnd(cCtx, op, dstEndSize, &cOptions); CHECK(op[dstEndSize] != canaryByte, "LZ4F_compressEnd writes beyond dstCapacity !"); @@ -897,7 +901,7 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi CHECK(1, "Compression completion failed (error %i : %s)", (int)result, LZ4F_getErrorName(result)); } } op += result; - cSize = op-(BYTE*)compressedBuffer; + cSize = (size_t)(op - (BYTE*)compressedBuffer); DISPLAYLEVEL(5, "\nCompressed %u bytes into %u \n", (U32)srcSize, (U32)cSize); } -- cgit v1.2.3 From 9c41e5804e4ac7675db94a8a3efa5b28454622fc Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 15 Apr 2019 02:11:59 -0700 Subject: assert() the failed test ensure canary remains within buffer limits --- tests/frametest.c | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/frametest.c b/tests/frametest.c index e4205a3..b1c7c02 100644 --- a/tests/frametest.c +++ b/tests/frametest.c @@ -893,6 +893,7 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi DISPLAYLEVEL(7,"canaryByte at pos %u / %u \n", (unsigned)((size_t)(op - (BYTE*)compressedBuffer) + dstEndSize), (unsigned)compressedBufferSize); + assert(op + dstEndSize < (BYTE*)compressedBuffer + compressedBufferSize); op[dstEndSize] = canaryByte; result = LZ4F_compressEnd(cCtx, op, dstEndSize, &cOptions); CHECK(op[dstEndSize] != canaryByte, "LZ4F_compressEnd writes beyond dstCapacity !"); -- cgit v1.2.3 From 3ab916c6a258434987b41ffbb58f792545415d8e Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 15 Apr 2019 10:14:36 -0700 Subject: frametest: removed uninitialized warning was a false positive, but better remove it anyway --- tests/frametest.c | 43 ++++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/tests/frametest.c b/tests/frametest.c index b1c7c02..1bf74ba 100644 --- a/tests/frametest.c +++ b/tests/frametest.c @@ -783,7 +783,7 @@ static void locateBuffDiff(const void* buff1, const void* buff2, size_t size, un int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressibility, U32 duration_s) { - unsigned testResult = 0; + int testResult = 0; unsigned testNb = 0; size_t const srcDataLength = 9 MB; /* needs to be > 2x4MB to test large blocks */ void* srcBuffer = NULL; @@ -793,7 +793,6 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi U32 coreRand = seed; LZ4F_decompressionContext_t dCtx = NULL; LZ4F_compressionContext_t cCtx = NULL; - size_t result; clock_t const startClock = clock(); clock_t const clockDuration = duration_s * CLOCKS_PER_SEC; # undef CHECK @@ -857,23 +856,27 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi BYTE* op = (BYTE*)compressedBuffer; BYTE* const oend = op + (neverFlush ? LZ4F_compressFrameBound(srcSize, prefsPtr) : compressedBufferSize); /* when flushes are possible, can't guarantee a max compressed size */ unsigned const maxBits = FUZ_highbit((U32)srcSize); + size_t cSegmentSize; LZ4F_compressOptions_t cOptions; memset(&cOptions, 0, sizeof(cOptions)); - result = LZ4F_compressBegin(cCtx, op, (size_t)(oend-op), prefsPtr); - CHECK(LZ4F_isError(result), "Compression header failed (error %i)", (int)result); - op += result; + cSegmentSize = LZ4F_compressBegin(cCtx, op, (size_t)(oend-op), prefsPtr); + CHECK(LZ4F_isError(cSegmentSize), "Compression header failed (error %i)", + (int)cSegmentSize); + op += cSegmentSize; while (ip < iend) { unsigned const nbBitsSeg = FUZ_rand(&randState) % maxBits; size_t const sampleMax = (FUZ_rand(&randState) & ((1< Date: Mon, 15 Apr 2019 10:28:36 -0700 Subject: fixed lz4frame with linked blocks when one block was not compressible, it would tag the context as `dirty`, resulting in compression automatically bailing out of all future blocks, making the rest of the frame uncompressible. --- lib/lz4.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 031f8c1..be6d50f 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -924,8 +924,8 @@ LZ4_FORCE_INLINE int LZ4_compress_generic( { unsigned const litLength = (unsigned)(ip - anchor); token = op++; if ((outputLimited == limitedOutput) && /* Check output buffer overflow */ - (unlikely(op + litLength + (2 + 1 + LASTLITERALS) + (litLength/255) > olimit))) - goto _failure; + (unlikely(op + litLength + (2 + 1 + LASTLITERALS) + (litLength/255) > olimit)) ) + return 0; /* cannot compress within `dst` budget. Stored indexes in hash table are nonetheless fine */ if ((outputLimited == fillOutput) && (unlikely(op + (litLength+240)/255 /* litlen */ + litLength /* literals */ + 2 /* offset */ + 1 /* token */ + MFLIMIT - MINMATCH /* min last literals so last match is <= end - MFLIMIT */ > olimit))) { @@ -998,14 +998,14 @@ _next_match: if ((outputLimited) && /* Check output buffer overflow */ (unlikely(op + (1 + LASTLITERALS) + (matchCode>>8) > olimit)) ) { - if (outputLimited == limitedOutput) - goto _failure; if (outputLimited == fillOutput) { /* Match description too long : reduce it */ U32 newMatchCode = 15 /* in token */ - 1 /* to avoid needing a zero byte */ + ((U32)(olimit - op) - 2 - 1 - LASTLITERALS) * 255; ip -= matchCode - newMatchCode; matchCode = newMatchCode; } + if (outputLimited == limitedOutput) + return 0; /* cannot compress within `dst` budget. Stored indexes in hash table are nonetheless fine */ } if (matchCode >= ML_MASK) { *token += ML_MASK; @@ -1098,7 +1098,7 @@ _last_literals: lastRun -= (lastRun+240)/255; } if (outputLimited == limitedOutput) - goto _failure; + return 0; /* cannot compress within `dst` budget. Stored indexes in hash table are nonetheless fine */ } if (lastRun >= RUN_MASK) { size_t accumulator = lastRun - RUN_MASK; @@ -1120,11 +1120,6 @@ _last_literals: result = (int)(((char*)op) - dest); assert(result > 0); return result; - -_failure: - /* Mark stream as having dirty context, so, it has to be fully reset */ - cctx->dirty = 1; - return 0; } @@ -1422,7 +1417,10 @@ static void LZ4_renormDictT(LZ4_stream_t_internal* LZ4_dict, int nextSize) } -int LZ4_compress_fast_continue (LZ4_stream_t* LZ4_stream, const char* source, char* dest, int inputSize, int maxOutputSize, int acceleration) +int LZ4_compress_fast_continue (LZ4_stream_t* LZ4_stream, + const char* source, char* dest, + int inputSize, int maxOutputSize, + int acceleration) { const tableType_t tableType = byU32; LZ4_stream_t_internal* streamPtr = &LZ4_stream->internal_donotuse; -- cgit v1.2.3 From 1f4a412646c82a7e0964b6c5b169d49ee072f475 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 15 Apr 2019 10:33:40 -0700 Subject: decompress*_fast() function do not generate deprecation warnings they are classified as deprecated in the API documentation (lz4.h) but do not yet trigger a warning, to give time to existing applications to move away. Also, the _fast() variants are still ~5% faster than the _safe() ones after Dave's patch. --- lib/lz4.h | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/lib/lz4.h b/lib/lz4.h index 23b5ac5..1589be9 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -631,11 +631,11 @@ LZ4_DEPRECATED("use LZ4_decompress_safe_usingDict() instead") LZ4LIB_API int LZ4 LZ4_DEPRECATED("use LZ4_decompress_fast_usingDict() instead") LZ4LIB_API int LZ4_decompress_fast_withPrefix64k (const char* src, char* dst, int originalSize); /*! LZ4_decompress_fast() : **unsafe!** - * These functions used to be a bit faster than LZ4_decompress_safe(), - * but situation has changed in recent versions. - * Now, `LZ4_decompress_safe()` is as fast and sometimes even faster than `LZ4_decompress_fast()`. - * Moreover, LZ4_decompress_safe() is protected vs malformed input, while `LZ4_decompress_fast()` is not, making it a security liability. + * These functions are generally slightly faster than LZ4_decompress_safe(), + * though the difference is small (generally ~5%). + * However, the real cost is a risk : LZ4_decompress_safe() is protected vs malformed input, while `LZ4_decompress_fast()` is not, making it a security liability. * As a consequence, LZ4_decompress_fast() is strongly discouraged, and deprecated. + * These functions will generate a deprecation warning in the future. * * Last LZ4_decompress_fast() specificity is that it can decompress a block without knowing its compressed size. * Note that even that functionality could be achieved in a more secure manner if need be, @@ -648,18 +648,19 @@ LZ4_DEPRECATED("use LZ4_decompress_fast_usingDict() instead") LZ4LIB_API int LZ4 * The function expects to finish at block's end exactly. * If the source stream is detected malformed, the function stops decoding and returns a negative result. * note : LZ4_decompress_fast*() requires originalSize. Thanks to this information, it never writes past the output buffer. - * However, since it doesn't know its 'src' size, it may read an unknown amount of input, and overflow input buffer. - * Also, since match offsets are not validated, match reads from 'src' may underflow. - * These issues never happen if input data is correct. + * However, since it doesn't know its 'src' size, it may read an unknown amount of input, past input buffer bounds. + * Also, since match offsets are not validated, match reads from 'src' may underflow too. + * These issues never happen if input (compressed) data is correct. * But they may happen if input data is invalid (error or intentional tampering). * As a consequence, use these functions in trusted environments with trusted data **only**. */ -LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe() instead") LZ4LIB_API -int LZ4_decompress_fast (const char* src, char* dst, int originalSize); -LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe_continue() instead") LZ4LIB_API -int LZ4_decompress_fast_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* src, char* dst, int originalSize); -LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe_usingDict() instead") LZ4LIB_API -int LZ4_decompress_fast_usingDict (const char* src, char* dst, int originalSize, const char* dictStart, int dictSize); + +/* LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe() instead") */ +LZ4LIB_API int LZ4_decompress_fast (const char* src, char* dst, int originalSize); +/* LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe_continue() instead") */ +LZ4LIB_API int LZ4_decompress_fast_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* src, char* dst, int originalSize); +/* LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe_usingDict() instead") */ +LZ4LIB_API int LZ4_decompress_fast_usingDict (const char* src, char* dst, int originalSize, const char* dictStart, int dictSize); /*! LZ4_resetStream() : * An LZ4_stream_t structure must be initialized at least once. -- cgit v1.2.3 From 98e5c0062fe80c98f5bc6231b5eb8bf9330b55be Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 15 Apr 2019 10:44:46 -0700 Subject: added more margin for canary in frametest --- tests/frametest.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/frametest.c b/tests/frametest.c index 1bf74ba..657a963 100644 --- a/tests/frametest.c +++ b/tests/frametest.c @@ -27,8 +27,8 @@ * Compiler specific **************************************/ #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 : 4127) /* disable: C4127: conditional expression is constant */ +# pragma warning(disable : 4146) /* disable: C4146: minus unsigned expression */ #endif @@ -55,7 +55,7 @@ static void FUZ_writeLE32 (void* dstVoidPtr, U32 value32) { BYTE* dstPtr = (BYTE*)dstVoidPtr; - dstPtr[0] = (BYTE)value32; + dstPtr[0] = (BYTE) value32; dstPtr[1] = (BYTE)(value32 >> 8); dstPtr[2] = (BYTE)(value32 >> 16); dstPtr[3] = (BYTE)(value32 >> 24); @@ -77,7 +77,6 @@ static const U32 prime1 = 2654435761U; static const U32 prime2 = 2246822519U; - /*-************************************ * Macros **************************************/ @@ -787,7 +786,7 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi unsigned testNb = 0; size_t const srcDataLength = 9 MB; /* needs to be > 2x4MB to test large blocks */ void* srcBuffer = NULL; - size_t const compressedBufferSize = LZ4F_compressFrameBound(srcDataLength, NULL) + 64 KB; /* needs some margin for some rare exceptional cases involving multiple flushes */ + size_t const compressedBufferSize = LZ4F_compressFrameBound(srcDataLength, NULL) + 4 MB; /* needs some margin */ void* compressedBuffer = NULL; void* decodedBuffer = NULL; U32 coreRand = seed; -- cgit v1.2.3 From 474c17cdc4f4704745e1337f88066e9bd88a3eb0 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 15 Apr 2019 11:09:56 -0700 Subject: unified limitedOutput_directive between lz4.c and lz4hc.c . was left in a strange state after the "amalgamation" patch. Now only 3 directives remain, same name across both implementations, single definition place. Might allow some light simplification due to reduced nb of states possible. --- lib/lz4.c | 31 +++++++++++++++---------------- lib/lz4hc.c | 31 ++++++++++++------------------- 2 files changed, 27 insertions(+), 35 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index be6d50f..5047fde 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -32,14 +32,6 @@ - LZ4 source repository : https://github.com/lz4/lz4 */ -/* - * LZ4_SRC_INCLUDED: - * Amalgamation flag, whether lz4.c is included - */ -#ifndef LZ4_SRC_INCLUDED -# define LZ4_SRC_INCLUDED 1 -#endif - /*-************************************ * Tuning parameters **************************************/ @@ -98,6 +90,14 @@ /*-************************************ * Dependency **************************************/ +/* + * LZ4_SRC_INCLUDED: + * Amalgamation flag, whether lz4.c is included + */ +#ifndef LZ4_SRC_INCLUDED +# define LZ4_SRC_INCLUDED 1 +#endif + #define LZ4_STATIC_LINKING_ONLY #define LZ4_DISABLE_DEPRECATE_WARNINGS /* due to LZ4_decompress_safe_withPrefix64k */ #include "lz4.h" @@ -177,7 +177,7 @@ /*-************************************ -* Basic Types +* Types **************************************/ #if defined(__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) # include @@ -202,6 +202,12 @@ typedef size_t reg_t; /* 32-bits in x32 mode */ #endif +typedef enum { + notLimited = 0, + limitedOutput = 1, + fillOutput = 2, +} limitedOutput_directive; + /*-************************************ * Reading and writing into memory @@ -549,13 +555,6 @@ static const U32 LZ4_skipTrigger = 6; /* Increase this value ==> compression ru /*-************************************ * Local Structures and types **************************************/ -typedef enum { - noLimit = 0, - notLimited = 1, - limitedOutput = 2, - fillOutput = 3, - limitedDestSize = 4 -} limitedOutput_directive; typedef enum { clearedTable = 0, byPtr, byU32, byU16 } tableType_t; /** diff --git a/lib/lz4hc.c b/lib/lz4hc.c index 4e3573a..f6ed779 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -63,13 +63,6 @@ /*=== Enums ===*/ typedef enum { noDictCtx, usingDictCtxHc } dictCtx_directive; -#ifndef LZ4_SRC_INCLUDED -typedef enum { - noLimit = 0, - limitedOutput = 1, - limitedDestSize = 2 -} limitedOutput_directive; -#endif #define LZ4_COMMONDEFS_ONLY @@ -512,7 +505,7 @@ LZ4_FORCE_INLINE int LZ4HC_compress_hashChain ( /* init */ *srcSizePtr = 0; - if (limit == limitedDestSize) oend -= LASTLITERALS; /* Hack for support LZ4 format restriction */ + if (limit == fillOutput) oend -= LASTLITERALS; /* Hack for support LZ4 format restriction */ if (inputSize < LZ4_minLength) goto _last_literals; /* Input too small, no compression (all literals) */ /* Main Loop */ @@ -659,7 +652,7 @@ _last_literals: { size_t lastRunSize = (size_t)(iend - anchor); /* literals */ size_t litLength = (lastRunSize + 255 - RUN_MASK) / 255; size_t const totalSize = 1 + litLength + lastRunSize; - if (limit == limitedDestSize) oend += LASTLITERALS; /* restore correct value */ + if (limit == fillOutput) oend += LASTLITERALS; /* restore correct value */ if (limit && (op + totalSize > oend)) { if (limit == limitedOutput) return 0; /* Check output limit */ /* adapt lastRunSize to fill 'dest' */ @@ -686,7 +679,7 @@ _last_literals: return (int) (((char*)op)-dest); _dest_overflow: - if (limit == limitedDestSize) { + if (limit == fillOutput) { op = optr; /* restore correct out pointer */ goto _last_literals; } @@ -738,7 +731,7 @@ LZ4_FORCE_INLINE int LZ4HC_compress_generic_internal ( DEBUGLOG(4, "LZ4HC_compress_generic(ctx=%p, src=%p, srcSize=%d)", ctx, src, *srcSizePtr); - if (limit == limitedDestSize && dstCapacity < 1) return 0; /* Impossible to store anything */ + if (limit == fillOutput && dstCapacity < 1) return 0; /* Impossible to store anything */ if ((U32)*srcSizePtr > (U32)LZ4_MAX_INPUT_SIZE) return 0; /* Unsupported input size (too large or negative) */ ctx->end += *srcSizePtr; @@ -855,7 +848,7 @@ int LZ4_compress_HC_extStateHC_fastReset (void* state, const char* src, char* ds if (dstCapacity < LZ4_compressBound(srcSize)) return LZ4HC_compress_generic (ctx, src, dst, &srcSize, dstCapacity, compressionLevel, limitedOutput); else - return LZ4HC_compress_generic (ctx, src, dst, &srcSize, dstCapacity, compressionLevel, noLimit); + return LZ4HC_compress_generic (ctx, src, dst, &srcSize, dstCapacity, compressionLevel, notLimited); } int LZ4_compress_HC_extStateHC (void* state, const char* src, char* dst, int srcSize, int dstCapacity, int compressionLevel) @@ -887,7 +880,7 @@ int LZ4_compress_HC_destSize(void* state, const char* source, char* dest, int* s if (ctx==NULL) return 0; /* init failure */ LZ4HC_init_internal(&ctx->internal_donotuse, (const BYTE*) source); LZ4_setCompressionLevel(ctx, cLevel); - return LZ4HC_compress_generic(&ctx->internal_donotuse, source, dest, sourceSizePtr, targetDestSize, cLevel, limitedDestSize); + return LZ4HC_compress_generic(&ctx->internal_donotuse, source, dest, sourceSizePtr, targetDestSize, cLevel, fillOutput); } @@ -1056,12 +1049,12 @@ int LZ4_compress_HC_continue (LZ4_streamHC_t* LZ4_streamHCPtr, const char* src, if (dstCapacity < LZ4_compressBound(srcSize)) return LZ4_compressHC_continue_generic (LZ4_streamHCPtr, src, dst, &srcSize, dstCapacity, limitedOutput); else - return LZ4_compressHC_continue_generic (LZ4_streamHCPtr, src, dst, &srcSize, dstCapacity, noLimit); + return LZ4_compressHC_continue_generic (LZ4_streamHCPtr, src, dst, &srcSize, dstCapacity, notLimited); } int LZ4_compress_HC_continue_destSize (LZ4_streamHC_t* LZ4_streamHCPtr, const char* src, char* dst, int* srcSizePtr, int targetDestSize) { - return LZ4_compressHC_continue_generic(LZ4_streamHCPtr, src, dst, srcSizePtr, targetDestSize, limitedDestSize); + return LZ4_compressHC_continue_generic(LZ4_streamHCPtr, src, dst, srcSizePtr, targetDestSize, fillOutput); } @@ -1137,7 +1130,7 @@ int LZ4_freeHC (void* LZ4HC_Data) int LZ4_compressHC2_continue (void* LZ4HC_Data, const char* src, char* dst, int srcSize, int cLevel) { - return LZ4HC_compress_generic (&((LZ4_streamHC_t*)LZ4HC_Data)->internal_donotuse, src, dst, &srcSize, 0, cLevel, noLimit); + return LZ4HC_compress_generic (&((LZ4_streamHC_t*)LZ4HC_Data)->internal_donotuse, src, dst, &srcSize, 0, cLevel, notLimited); } int LZ4_compressHC2_limitedOutput_continue (void* LZ4HC_Data, const char* src, char* dst, int srcSize, int dstCapacity, int cLevel) @@ -1247,7 +1240,7 @@ static int LZ4HC_compress_optimal ( LZ4HC_CCtx_internal* ctx, /* init */ DEBUGLOG(5, "LZ4HC_compress_optimal(dst=%p, dstCapa=%u)", dst, (unsigned)dstCapacity); *srcSizePtr = 0; - if (limit == limitedDestSize) oend -= LASTLITERALS; /* Hack for support LZ4 format restriction */ + if (limit == fillOutput) oend -= LASTLITERALS; /* Hack for support LZ4 format restriction */ if (sufficient_len >= LZ4_OPT_NUM) sufficient_len = LZ4_OPT_NUM-1; /* Main Loop */ @@ -1447,7 +1440,7 @@ static int LZ4HC_compress_optimal ( LZ4HC_CCtx_internal* ctx, { size_t lastRunSize = (size_t)(iend - anchor); /* literals */ size_t litLength = (lastRunSize + 255 - RUN_MASK) / 255; size_t const totalSize = 1 + litLength + lastRunSize; - if (limit == limitedDestSize) oend += LASTLITERALS; /* restore correct value */ + if (limit == fillOutput) oend += LASTLITERALS; /* restore correct value */ if (limit && (op + totalSize > oend)) { if (limit == limitedOutput) return 0; /* Check output limit */ /* adapt lastRunSize to fill 'dst' */ @@ -1474,7 +1467,7 @@ static int LZ4HC_compress_optimal ( LZ4HC_CCtx_internal* ctx, return (int) ((char*)op-dst); _dest_overflow: - if (limit == limitedDestSize) { + if (limit == fillOutput) { op = opSaved; /* restore correct out pointer */ goto _last_literals; } -- cgit v1.2.3 From ce0f3169891ecd0bb2822ec7d2ed322a41014b39 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 15 Apr 2019 11:12:54 -0700 Subject: fix fuzzer tests on dirty context context is no longer dirty after a failed compressed block. Actually, all indexes are fine. It remains compatible with continued streaming, and reset*_fast(). --- tests/fuzzer.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/fuzzer.c b/tests/fuzzer.c index 8e25615..3128e6d 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -762,7 +762,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c LZ4_attach_dictionary(&LZ4_stream, &LZ4dict); ret = LZ4_compress_fast_continue(&LZ4_stream, block, compressedBuffer, blockSize, blockContinueCompressedSize-1, 1); FUZ_CHECKTEST(ret>0, "LZ4_compress_fast_continue using extDictCtx should fail : one missing byte for output buffer : %i written, %i buffer", ret, blockContinueCompressedSize); - FUZ_CHECKTEST(!LZ4_stream.internal_donotuse.dirty, "context should be dirty"); + /* note : context is no longer dirty after a failed compressed block */ FUZ_DISPLAYTEST(); LZ4_resetStream_fast(&LZ4_stream); @@ -840,7 +840,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c LZ4_loadDictHC(&LZ4dictHC, dict, dictSize); ret = LZ4_compress_HC_continue(&LZ4dictHC, block, compressedBuffer, blockSize, blockContinueCompressedSize-1); FUZ_CHECKTEST(ret>0, "LZ4_compress_HC_continue using ExtDict should fail : one missing byte for output buffer (expected %i, but result=%i)", blockContinueCompressedSize, ret); - FUZ_CHECKTEST(!LZ4dictHC.internal_donotuse.dirty, "Context should be dirty"); + /* note : context is no longer dirty after a failed compressed block */ FUZ_DISPLAYTEST("LZ4_compress_HC_continue with same external dictionary, and output buffer exactly the right size"); LZ4_loadDictHC(&LZ4dictHC, dict, dictSize); @@ -877,7 +877,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c LZ4_attach_HC_dictionary(&LZ4_streamHC, &LZ4dictHC); ret = LZ4_compress_HC_continue(&LZ4_streamHC, block, compressedBuffer, blockSize, blockContinueCompressedSize-1); FUZ_CHECKTEST(ret>0, "LZ4_compress_HC_continue using ExtDictCtx should fail : one missing byte for output buffer (%i != %i)", ret, blockContinueCompressedSize); - FUZ_CHECKTEST(!LZ4_streamHC.internal_donotuse.dirty, "Context should be dirty"); + /* note : context is no longer dirty after a failed compressed block */ FUZ_DISPLAYTEST(); LZ4_resetStreamHC_fast (&LZ4_streamHC, compressionLevel); -- cgit v1.2.3 From ac5d2629c1689efb0864225a71465e8d27526bf3 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 15 Apr 2019 11:17:35 -0700 Subject: added a fake initialization to please an analyzer which is unable to understand that the variable is necessarily initialized in spite of an assert just before. --- tests/frametest.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/frametest.c b/tests/frametest.c index 657a963..a5197ff 100644 --- a/tests/frametest.c +++ b/tests/frametest.c @@ -920,7 +920,7 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi unsigned const maxBits = MAX(3, suggestedBits); unsigned const nonContiguousDst = FUZ_rand(&randState) % 3; /* 0 : contiguous; 1 : non-contiguous; 2 : dst overwritten */ size_t totalOut = 0; - size_t decSize; + size_t decSize = 0; XXH64_state_t xxh64; XXH64_reset(&xxh64, 1); assert(ip < iend); -- cgit v1.2.3 From 55f6f0dd7417832e163d367750bf4adf37842b4e Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 15 Apr 2019 11:22:25 -0700 Subject: fix comma for pedantic --- lib/lz4.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/lz4.c b/lib/lz4.c index 5047fde..218efc2 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -205,7 +205,7 @@ typedef enum { notLimited = 0, limitedOutput = 1, - fillOutput = 2, + fillOutput = 2 } limitedOutput_directive; -- cgit v1.2.3 From 920c988669663708bc5ac8db3ba18bd78eebe499 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 15 Apr 2019 14:13:10 -0700 Subject: simplified output_directive --- lib/lz4.c | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 218efc2..693bdaf 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -760,9 +760,9 @@ LZ4_FORCE_INLINE int LZ4_compress_generic( const char* const source, char* const dest, const int inputSize, - int *inputConsumed, /* only written when outputLimited == fillOutput */ + int *inputConsumed, /* only written when outputDirective == fillOutput */ const int maxOutputSize, - const limitedOutput_directive outputLimited, + const limitedOutput_directive outputDirective, const tableType_t tableType, const dict_directive dictDirective, const dictIssue_directive dictIssue, @@ -805,7 +805,7 @@ LZ4_FORCE_INLINE int LZ4_compress_generic( DEBUGLOG(5, "LZ4_compress_generic: srcSize=%i, tableType=%u", inputSize, tableType); /* If init conditions are not met, we don't have to mark stream * as having dirty context, since no action was taken yet */ - if (outputLimited == fillOutput && maxOutputSize < 1) return 0; /* Impossible to store anything */ + if (outputDirective == fillOutput && maxOutputSize < 1) return 0; /* Impossible to store anything */ if ((U32)inputSize > (U32)LZ4_MAX_INPUT_SIZE) return 0; /* Unsupported inputSize, too large (or negative) */ if ((tableType == byU16) && (inputSize>=LZ4_64Klimit)) return 0; /* Size too large (not within 64K limit) */ if (tableType==byPtr) assert(dictDirective==noDict); /* only supported use case with byPtr */ @@ -922,11 +922,11 @@ LZ4_FORCE_INLINE int LZ4_compress_generic( /* Encode Literals */ { unsigned const litLength = (unsigned)(ip - anchor); token = op++; - if ((outputLimited == limitedOutput) && /* Check output buffer overflow */ + if ((outputDirective == limitedOutput) && /* Check output buffer overflow */ (unlikely(op + litLength + (2 + 1 + LASTLITERALS) + (litLength/255) > olimit)) ) return 0; /* cannot compress within `dst` budget. Stored indexes in hash table are nonetheless fine */ - if ((outputLimited == fillOutput) && + if ((outputDirective == fillOutput) && (unlikely(op + (litLength+240)/255 /* litlen */ + litLength /* literals */ + 2 /* offset */ + 1 /* token */ + MFLIMIT - MINMATCH /* min last literals so last match is <= end - MFLIMIT */ > olimit))) { op--; goto _last_literals; @@ -955,7 +955,7 @@ _next_match: * - token and *token : position to write 4-bits for match length; higher 4-bits for literal length supposed already written */ - if ((outputLimited == fillOutput) && + if ((outputDirective == fillOutput) && (op + 2 /* offset */ + 1 /* token */ + MFLIMIT - MINMATCH /* min last literals so last match is <= end - MFLIMIT */ > olimit)) { /* the match was too close to the end, rewind and go to last literals */ op = token; @@ -995,16 +995,17 @@ _next_match: DEBUGLOG(6, " with matchLength=%u", matchCode+MINMATCH); } - if ((outputLimited) && /* Check output buffer overflow */ + if ((outputDirective) && /* Check output buffer overflow */ (unlikely(op + (1 + LASTLITERALS) + (matchCode>>8) > olimit)) ) { - if (outputLimited == fillOutput) { + if (outputDirective == fillOutput) { /* Match description too long : reduce it */ U32 newMatchCode = 15 /* in token */ - 1 /* to avoid needing a zero byte */ + ((U32)(olimit - op) - 2 - 1 - LASTLITERALS) * 255; ip -= matchCode - newMatchCode; matchCode = newMatchCode; - } - if (outputLimited == limitedOutput) + } else { + assert(outputDirective == limitedOutput); return 0; /* cannot compress within `dst` budget. Stored indexes in hash table are nonetheless fine */ + } } if (matchCode >= ML_MASK) { *token += ML_MASK; @@ -1088,16 +1089,17 @@ _next_match: _last_literals: /* Encode Last Literals */ { size_t lastRun = (size_t)(iend - anchor); - if ( (outputLimited) && /* Check output buffer overflow */ + if ( (outputDirective) && /* Check output buffer overflow */ (op + lastRun + 1 + ((lastRun+255-RUN_MASK)/255) > olimit)) { - if (outputLimited == fillOutput) { + if (outputDirective == fillOutput) { /* adapt lastRun to fill 'dst' */ assert(olimit >= op); lastRun = (size_t)(olimit-op) - 1; lastRun -= (lastRun+240)/255; - } - if (outputLimited == limitedOutput) + } else { + assert(outputDirective == limitedOutput); return 0; /* cannot compress within `dst` budget. Stored indexes in hash table are nonetheless fine */ + } } if (lastRun >= RUN_MASK) { size_t accumulator = lastRun - RUN_MASK; @@ -1112,7 +1114,7 @@ _last_literals: op += lastRun; } - if (outputLimited == fillOutput) { + if (outputDirective == fillOutput) { *inputConsumed = (int) (((const char*)ip)-source); } DEBUGLOG(5, "LZ4_compress_generic: compressed %i bytes into %i bytes", inputSize, (int)(((char*)op) - dest)); -- cgit v1.2.3 From fc3176f6aa6b8034e0a27598c23bdda559b5cf9c Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 15 Apr 2019 17:30:31 -0700 Subject: updated NEWS for v1.9.0 --- NEWS | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/NEWS b/NEWS index 57a75cf..6313142 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,15 @@ +v1.9.0 +perf: large decompression speed improvement on x86/x64 (~+20%) by @djwatson +api : changed : _destSize() compression variants are promoted to stable API +api : new : LZ4_initStream(HC), replacing LZ4_resetStream(HC) +api : changed : LZ4_resetStream(HC) as recommended reset function, for better performance on small data +cli : support custom block sizes, by @blezsan +build: source code can be amalgamated, by Bing Xu +build: added meson build, by @lzutao +build: new build macros : LZ4_DISTANCE_MAX, LZ4_FAST_DEC_LOOP +install: MidnightBSD, by @laffer1 +install: Windows 10 msys2, by @vtorri + v1.8.3 perf: minor decompression speed improvement (~+2%) with gcc fix : corruption in v1.8.2 at level 9 for files > 64KB under rare conditions (#560) -- cgit v1.2.3 From 108adfcb422ba5c2594a72b8fd5c9df38cc9ca6e Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 16 Apr 2019 11:20:31 -0700 Subject: minor news update for msys2 --- NEWS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS b/NEWS index 6313142..7a3a071 100644 --- a/NEWS +++ b/NEWS @@ -8,7 +8,7 @@ build: source code can be amalgamated, by Bing Xu build: added meson build, by @lzutao build: new build macros : LZ4_DISTANCE_MAX, LZ4_FAST_DEC_LOOP install: MidnightBSD, by @laffer1 -install: Windows 10 msys2, by @vtorri +install: msys2 on Windows 10, by @vtorri v1.8.3 perf: minor decompression speed improvement (~+2%) with gcc -- cgit v1.2.3 From 6fc763cd98eb9a487afc9c2577627c4028e43cef Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 16 Apr 2019 11:26:03 -0700 Subject: ensure consistent definition and usage of FREEMEM as suggested by @sloutsky in #671 --- lib/lz4frame.c | 8 ++++---- lib/lz4hc.c | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/lz4frame.c b/lib/lz4frame.c index 19efd0b..42124e9 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -72,15 +72,15 @@ * by modifying below section. */ #include /* malloc, calloc, free */ -#define ALLOC(s) malloc(s) #ifndef LZ4_SRC_INCLUDED /* avoid redefinition when sources are coalesced */ -# define ALLOC_AND_ZERO(s) calloc(1,(s)) +# define ALLOC(s) malloc(s) +# define ALLOC_AND_ZERO(s) calloc(1,(s)) +# define FREEMEM(p) free(p) #endif -#define FREEMEM(p) free(p) #include /* memset, memcpy, memmove */ #ifndef LZ4_SRC_INCLUDED /* avoid redefinition when sources are coalesced */ -# define MEM_INIT memset +# define MEM_INIT(p,v,s) memset((p),(v),(s)) #endif diff --git a/lib/lz4hc.c b/lib/lz4hc.c index f6ed779..d5f6743 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -868,7 +868,7 @@ int LZ4_compress_HC(const char* src, char* dst, int srcSize, int dstCapacity, in #endif int const cSize = LZ4_compress_HC_extStateHC(statePtr, src, dst, srcSize, dstCapacity, compressionLevel); #if defined(LZ4HC_HEAPMODE) && LZ4HC_HEAPMODE==1 - free(statePtr); + FREEMEM(statePtr); #endif return cSize; } @@ -901,7 +901,7 @@ int LZ4_freeStreamHC (LZ4_streamHC_t* LZ4_streamHCPtr) { DEBUGLOG(4, "LZ4_freeStreamHC(%p)", LZ4_streamHCPtr); if (!LZ4_streamHCPtr) return 0; /* support free on NULL */ - free(LZ4_streamHCPtr); + FREEMEM(LZ4_streamHCPtr); return 0; } -- cgit v1.2.3 From d25b6d640132bb25a3db9617a8708d31701402f5 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 16 Apr 2019 11:52:54 -0700 Subject: minor benchmark update was using wrong memory timing --- NEWS | 2 +- README.md | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/NEWS b/NEWS index 7a3a071..2d85607 100644 --- a/NEWS +++ b/NEWS @@ -1,5 +1,5 @@ v1.9.0 -perf: large decompression speed improvement on x86/x64 (~+20%) by @djwatson +perf: large decompression speed improvement on x86/x64 (up to +20%) by @djwatson api : changed : _destSize() compression variants are promoted to stable API api : new : LZ4_initStream(HC), replacing LZ4_resetStream(HC) api : changed : LZ4_resetStream(HC) as recommended reset function, for better performance on small data diff --git a/README.md b/README.md index fd58573..73554cd 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ Benchmarks The benchmark uses [lzbench], from @inikep compiled with GCC v8.2.0 on Linux 64-bits (Ubuntu 4.18.0-17). -The reference system uses a Core i7-9700K CPU @ 4.9GHz. +The reference system uses a Core i7-9700K CPU @ 4.9GHz (w/ turbo boost). Benchmark evaluates the compression of reference [Silesia Corpus] in single-thread mode. @@ -61,14 +61,14 @@ in single-thread mode. | Compressor | Ratio | Compression | Decompression | | ---------- | ----- | ----------- | ------------- | | memcpy | 1.000 | 13700 MB/s | 13700 MB/s | -|**LZ4 default (v1.9.0)** |**2.101**| **780 MB/s**| **4900 MB/s** | +|**LZ4 default (v1.9.0)** |**2.101**| **780 MB/s**| **4970 MB/s** | | LZO 2.09 | 2.108 | 670 MB/s | 860 MB/s | | QuickLZ 1.5.0 | 2.238 | 575 MB/s | 780 MB/s | | Snappy 1.1.4 | 2.091 | 565 MB/s | 1950 MB/s | | [Zstandard] 1.4.0 -1 | 2.883 | 515 MB/s | 1380 MB/s | | LZF v3.6 | 2.073 | 415 MB/s | 910 MB/s | | [zlib] deflate 1.2.11 -1| 2.730 | 100 MB/s | 415 MB/s | -|**LZ4 HC -9 (v1.8.2)** |**2.721**| 39 MB/s | **4850 MB/s** | +|**LZ4 HC -9 (v1.8.2)** |**2.721**| 41 MB/s | **4900 MB/s** | | [zlib] deflate 1.2.11 -6| 3.099 | 36 MB/s | 445 MB/s | [zlib]: http://www.zlib.net/ -- cgit v1.2.3 From 5dde00e5d33eb617673f62191781907468beadb7 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 16 Apr 2019 20:46:04 -0700 Subject: fixed version number for lz4hc --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 73554cd..607fc4e 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ in single-thread mode. | [Zstandard] 1.4.0 -1 | 2.883 | 515 MB/s | 1380 MB/s | | LZF v3.6 | 2.073 | 415 MB/s | 910 MB/s | | [zlib] deflate 1.2.11 -1| 2.730 | 100 MB/s | 415 MB/s | -|**LZ4 HC -9 (v1.8.2)** |**2.721**| 41 MB/s | **4900 MB/s** | +|**LZ4 HC -9 (v1.9.0)** |**2.721**| 41 MB/s | **4900 MB/s** | | [zlib] deflate 1.2.11 -6| 3.099 | 36 MB/s | 445 MB/s | [zlib]: http://www.zlib.net/ -- cgit v1.2.3 From 1848ea5cbd582e3728ea4fe1ff619468753debea Mon Sep 17 00:00:00 2001 From: Norm Green Date: Wed, 17 Apr 2019 09:20:09 -0700 Subject: Fix AIX errors/warnings --- lib/lz4.c | 6 ++++++ tests/fuzzer.c | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/lib/lz4.c b/lib/lz4.c index 693bdaf..bd8fa11 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -98,8 +98,14 @@ # define LZ4_SRC_INCLUDED 1 #endif +#ifndef LZ4_STATIC_LINKING_ONLY #define LZ4_STATIC_LINKING_ONLY +#endif + +#ifndef LZ4_DISABLE_DEPRECATE_WARNINGS #define LZ4_DISABLE_DEPRECATE_WARNINGS /* due to LZ4_decompress_safe_withPrefix64k */ +#endif + #include "lz4.h" /* see also "memory routines" below */ diff --git a/tests/fuzzer.c b/tests/fuzzer.c index 3128e6d..68e2232 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -50,6 +50,10 @@ #include #include /* INT_MAX */ +#if defined(_AIX) +# include /* mmap */ +#endif + #define LZ4_DISABLE_DEPRECATE_WARNINGS /* LZ4_decompress_fast */ #define LZ4_STATIC_LINKING_ONLY #include "lz4.h" -- cgit v1.2.3 From 1ed69691a1d1226a6d6e7c465eb1e8c2706833cb Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 17 Apr 2019 13:31:24 -0700 Subject: fuzzer: added tests to detect LZ4_decompress_fast() out of bound read --- tests/fuzzer.c | 46 +++++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/tests/fuzzer.c b/tests/fuzzer.c index 68e2232..0eece15 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -481,31 +481,39 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c /* Test compression */ FUZ_DISPLAYTEST("test LZ4_compress_default()"); ret = LZ4_compress_default(block, compressedBuffer, blockSize, (int)compressedBufferSize); - FUZ_CHECKTEST(ret==0, "LZ4_compress_default() failed"); + FUZ_CHECKTEST(ret<=0, "LZ4_compress_default() failed"); compressedSize = ret; /* Decompression tests */ - /* Test decoding with output size exactly correct => must work */ - FUZ_DISPLAYTEST("LZ4_decompress_fast() with exact output buffer"); - ret = LZ4_decompress_fast(compressedBuffer, decodedBuffer, blockSize); - FUZ_CHECKTEST(ret<0, "LZ4_decompress_fast failed despite correct space"); - FUZ_CHECKTEST(ret!=compressedSize, "LZ4_decompress_fast failed : did not fully read compressed data"); - { U32 const crcCheck = XXH32(decodedBuffer, blockSize, 0); - FUZ_CHECKTEST(crcCheck!=crcOrig, "LZ4_decompress_fast corrupted decoded data"); - } + /* Test decompress_fast() with input buffer size exactly correct => must not read out of bound */ + { char* const cBuffer_exact = malloc((size_t)compressedSize); + assert(cBuffer_exact != NULL); + memcpy(cBuffer_exact, compressedBuffer, compressedSize); + + /* Test decoding with output size exactly correct => must work */ + FUZ_DISPLAYTEST("LZ4_decompress_fast() with exact output buffer"); + ret = LZ4_decompress_fast(cBuffer_exact, decodedBuffer, blockSize); + FUZ_CHECKTEST(ret<0, "LZ4_decompress_fast failed despite correct space"); + FUZ_CHECKTEST(ret!=compressedSize, "LZ4_decompress_fast failed : did not fully read compressed data"); + { U32 const crcCheck = XXH32(decodedBuffer, (size_t)blockSize, 0); + FUZ_CHECKTEST(crcCheck!=crcOrig, "LZ4_decompress_fast corrupted decoded data"); + } - /* Test decoding with one byte missing => must fail */ - FUZ_DISPLAYTEST("LZ4_decompress_fast() with output buffer 1-byte too short"); - decodedBuffer[blockSize-1] = 0; - ret = LZ4_decompress_fast(compressedBuffer, decodedBuffer, blockSize-1); - FUZ_CHECKTEST(ret>=0, "LZ4_decompress_fast should have failed, due to Output Size being too small"); - FUZ_CHECKTEST(decodedBuffer[blockSize-1], "LZ4_decompress_fast overrun specified output buffer"); + /* Test decoding with one byte missing => must fail */ + FUZ_DISPLAYTEST("LZ4_decompress_fast() with output buffer 1-byte too short"); + decodedBuffer[blockSize-1] = 0; + ret = LZ4_decompress_fast(cBuffer_exact, decodedBuffer, blockSize-1); + FUZ_CHECKTEST(ret>=0, "LZ4_decompress_fast should have failed, due to Output Size being too small"); + FUZ_CHECKTEST(decodedBuffer[blockSize-1], "LZ4_decompress_fast overrun specified output buffer"); - /* Test decoding with one byte too much => must fail */ - FUZ_DISPLAYTEST(); - ret = LZ4_decompress_fast(compressedBuffer, decodedBuffer, blockSize+1); - FUZ_CHECKTEST(ret>=0, "LZ4_decompress_fast should have failed, due to Output Size being too large"); + /* Test decoding with one byte too much => must fail */ + FUZ_DISPLAYTEST(); + ret = LZ4_decompress_fast(cBuffer_exact, decodedBuffer, blockSize+1); + FUZ_CHECKTEST(ret>=0, "LZ4_decompress_fast should have failed, due to Output Size being too large"); + + free(cBuffer_exact); + } /* Test decoding with empty input */ FUZ_DISPLAYTEST("LZ4_decompress_safe() with empty input"); -- cgit v1.2.3 From 25d96f1e4d84b9fca8f754cd91b822fc32758e5c Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 17 Apr 2019 15:01:53 -0700 Subject: fix out-of-bound read within LZ4_decompress_fast() and deprecate LZ4_decompress_fast(), with deprecation warnings enabled by default. Note that, as a consequence of the fix, LZ4_decompress_fast is now __slower__ than LZ4_decompress_safe(). That's because, since it doesn't know the input buffer size, it must progress more cautiously into the input buffer to ensure to out-of-bound read. --- lib/lz4.c | 15 +++++++++++++-- lib/lz4.h | 19 ++++++++++--------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index bd8fa11..2f8aa04 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -135,6 +135,9 @@ # endif /* _MSC_VER */ #endif /* LZ4_FORCE_INLINE */ +#undef LZ4_FORCE_INLINE +#define LZ4_FORCE_INLINE static /* disable */ + /* LZ4_FORCE_O2_GCC_PPC64LE and LZ4_FORCE_O2_INLINE_GCC_PPC64LE * Gcc on ppc64le generates an unrolled SIMDized loop for LZ4_wildCopy, * together with a simple 8-byte copy loop as a fall-back path. @@ -1671,7 +1674,10 @@ LZ4_decompress_generic( { goto safe_literal_copy; } - LZ4_wildCopy32(op, ip, cpy); + if (endOnInput) + LZ4_wildCopy32(op, ip, cpy); + else + LZ4_wildCopy(op, ip, cpy); /* LZ4_decompress_fast() cannot copy more than 8 bytes at a time : it doesn't know input length, and only relies on end-of-block properties */ ip += length; op = cpy; } else { cpy = op+length; @@ -1681,7 +1687,12 @@ LZ4_decompress_generic( goto safe_literal_copy; } /* Literals can only be 14, but hope compilers optimize if we copy by a register size */ - memcpy(op, ip, 16); + if (endOnInput) + memcpy(op, ip, 16); + else { /* LZ4_decompress_fast() cannot copy more than 8 bytes at a time : it doesn't know input length, and only relies on end-of-block properties */ + memcpy(op, ip, 8); + if (length > 8) memcpy(op+8, ip+8, 8); + } ip += length; op = cpy; } diff --git a/lib/lz4.h b/lib/lz4.h index 1589be9..962f5e6 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -631,14 +631,15 @@ LZ4_DEPRECATED("use LZ4_decompress_safe_usingDict() instead") LZ4LIB_API int LZ4 LZ4_DEPRECATED("use LZ4_decompress_fast_usingDict() instead") LZ4LIB_API int LZ4_decompress_fast_withPrefix64k (const char* src, char* dst, int originalSize); /*! LZ4_decompress_fast() : **unsafe!** - * These functions are generally slightly faster than LZ4_decompress_safe(), - * though the difference is small (generally ~5%). - * However, the real cost is a risk : LZ4_decompress_safe() is protected vs malformed input, while `LZ4_decompress_fast()` is not, making it a security liability. + * These functions used to be faster than LZ4_decompress_safe(), + * but it has changed, and they are now slower than LZ4_decompress_safe(). + * This is because LZ4_decompress_fast() doesn't know the input size, + * and therefore must progress more cautiously in the input buffer to not read beyond the end of block. + * On top of that `LZ4_decompress_fast()` is not protected vs malformed or malicious inputs, making it a security liability. * As a consequence, LZ4_decompress_fast() is strongly discouraged, and deprecated. - * These functions will generate a deprecation warning in the future. * - * Last LZ4_decompress_fast() specificity is that it can decompress a block without knowing its compressed size. - * Note that even that functionality could be achieved in a more secure manner if need be, + * Only LZ4_decompress_fast() specificity is that it can decompress a block without knowing its compressed size. + * Even that functionality could be achieved in a more secure manner if need be, * though it would require new prototypes, and adaptation of the implementation to this new use case. * * Parameters: @@ -655,11 +656,11 @@ LZ4_DEPRECATED("use LZ4_decompress_fast_usingDict() instead") LZ4LIB_API int LZ4 * As a consequence, use these functions in trusted environments with trusted data **only**. */ -/* LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe() instead") */ +LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe() instead") LZ4LIB_API int LZ4_decompress_fast (const char* src, char* dst, int originalSize); -/* LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe_continue() instead") */ +LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe_continue() instead") LZ4LIB_API int LZ4_decompress_fast_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* src, char* dst, int originalSize); -/* LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe_usingDict() instead") */ +LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe_usingDict() instead") LZ4LIB_API int LZ4_decompress_fast_usingDict (const char* src, char* dst, int originalSize, const char* dictStart, int dictSize); /*! LZ4_resetStream() : -- cgit v1.2.3 From 5acfb15df066ab77301c32064b28ec9b3e571d67 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 17 Apr 2019 15:33:37 -0700 Subject: re-enable FORCE_INLINE was disabled for tests --- lib/lz4.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 2f8aa04..c38932e 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -135,9 +135,6 @@ # endif /* _MSC_VER */ #endif /* LZ4_FORCE_INLINE */ -#undef LZ4_FORCE_INLINE -#define LZ4_FORCE_INLINE static /* disable */ - /* LZ4_FORCE_O2_GCC_PPC64LE and LZ4_FORCE_O2_INLINE_GCC_PPC64LE * Gcc on ppc64le generates an unrolled SIMDized loop for LZ4_wildCopy, * together with a simple 8-byte copy loop as a fall-back path. -- cgit v1.2.3 From 8f0e7440633b956484626a432af3eae70c436419 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 17 Apr 2019 15:44:14 -0700 Subject: fuzzer: fix explicit cast for C++ --- tests/fuzzer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/fuzzer.c b/tests/fuzzer.c index 0eece15..234be7c 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -487,7 +487,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c /* Decompression tests */ /* Test decompress_fast() with input buffer size exactly correct => must not read out of bound */ - { char* const cBuffer_exact = malloc((size_t)compressedSize); + { char* const cBuffer_exact = (char*)malloc((size_t)compressedSize); assert(cBuffer_exact != NULL); memcpy(cBuffer_exact, compressedBuffer, compressedSize); -- cgit v1.2.3 From 3f558163ad74ee9553839aedb211c08a835f23a3 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 18 Apr 2019 10:38:51 -0700 Subject: fuzzer: fixed scan-build leak warning the program exit(), so there is no need to track and dealloc every buffer. --- tests/fuzzer.c | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/tests/fuzzer.c b/tests/fuzzer.c index 234be7c..14cd8a0 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -331,7 +331,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c if (cond) { \ printf("Test %u : ", testNb); printf(__VA_ARGS__); \ printf(" (seed %u, cycle %u) \n", seed, cycleNb); \ - goto _output_error; \ + exit(1); \ } # define FUZ_DISPLAYTEST(...) { \ @@ -347,7 +347,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c /* init */ if(!CNBuffer || !compressedBuffer || !decodedBuffer) { DISPLAY("Not enough memory to start fuzzer tests"); - goto _output_error; + exit(1); } if ( LZ4_initStream(&LZ4dict, sizeof(LZ4dict)) == NULL) abort(); if ( LZ4_initStreamHC(&LZ4dictHC, sizeof(LZ4dictHC)) == NULL) abort(); @@ -505,7 +505,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c decodedBuffer[blockSize-1] = 0; ret = LZ4_decompress_fast(cBuffer_exact, decodedBuffer, blockSize-1); FUZ_CHECKTEST(ret>=0, "LZ4_decompress_fast should have failed, due to Output Size being too small"); - FUZ_CHECKTEST(decodedBuffer[blockSize-1], "LZ4_decompress_fast overrun specified output buffer"); + FUZ_CHECKTEST(decodedBuffer[blockSize-1]!=0, "LZ4_decompress_fast overrun specified output buffer"); /* Test decoding with one byte too much => must fail */ FUZ_DISPLAYTEST(); @@ -960,20 +960,13 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c printf("ratio with dict: %0.3f%%\n", (double)ccbytes/bytes*100); /* release memory */ - { -_exit: - free(CNBuffer); - free(compressedBuffer); - free(decodedBuffer); - FUZ_freeLowAddr(lowAddrBuffer, labSize); - free(stateLZ4); - free(stateLZ4HC); - return result; - -_output_error: - result = 1; - goto _exit; - } + free(CNBuffer); + free(compressedBuffer); + free(decodedBuffer); + FUZ_freeLowAddr(lowAddrBuffer, labSize); + free(stateLZ4); + free(stateLZ4HC); + return result; } @@ -1388,8 +1381,6 @@ static void FUZ_unitTests(int compressionLevel) printf("All unit tests completed successfully compressionLevel=%d \n", compressionLevel); return; -_output_error: - exit(1); } -- cgit v1.2.3 From 4f6de46f60fc7b158e7fc5ce24eb06c39d8b6ce2 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 18 Apr 2019 12:05:50 -0700 Subject: fix several minor static analyzer warnings --- programs/bench.c | 12 +++++----- tests/checkFrame.c | 2 +- tests/frametest.c | 14 +++++++----- tests/fuzzer.c | 64 +++++++++++++++++++++++++++++++----------------------- 4 files changed, 52 insertions(+), 40 deletions(-) diff --git a/programs/bench.c b/programs/bench.c index 11bf044..5934935 100644 --- a/programs/bench.c +++ b/programs/bench.c @@ -209,7 +209,7 @@ static int BMK_benchMem(const void* srcBuffer, size_t srcSize, blockTable[nbBlocks].cPtr = cPtr; blockTable[nbBlocks].resPtr = resPtr; blockTable[nbBlocks].srcSize = thisBlockSize; - blockTable[nbBlocks].cRoom = LZ4_compressBound((int)thisBlockSize); + blockTable[nbBlocks].cRoom = (size_t)LZ4_compressBound((int)thisBlockSize); srcPtr += thisBlockSize; cPtr += blockTable[nbBlocks].cRoom; resPtr += thisBlockSize; @@ -257,8 +257,8 @@ static int BMK_benchMem(const void* srcBuffer, size_t srcSize, for (nbLoops=0; nbLoops < nbCompressionLoops; nbLoops++) { U32 blockNb; for (blockNb=0; blockNb 0) { diff --git a/tests/checkFrame.c b/tests/checkFrame.c index 50c0405..139a599 100644 --- a/tests/checkFrame.c +++ b/tests/checkFrame.c @@ -105,7 +105,7 @@ typedef struct { LZ4F_decompressionContext_t ctx; } cRess_t; -static int createCResources(cRess_t *ress) +static int createCResources(cRess_t* ress) { ress->srcBufferSize = 4 MB; ress->srcBuffer = malloc(ress->srcBufferSize); diff --git a/tests/frametest.c b/tests/frametest.c index a5197ff..9f7cb8d 100644 --- a/tests/frametest.c +++ b/tests/frametest.c @@ -167,7 +167,7 @@ static unsigned FUZ_highbit(U32 v32) /*-******************************************************* * Tests *********************************************************/ -#define CHECK_V(v,f) v = f; if (LZ4F_isError(v)) { fprintf(stderr, "%s\n", LZ4F_getErrorName(v)); goto _output_error; } +#define CHECK_V(v,f) v = f; if (LZ4F_isError(v)) { fprintf(stderr, "%s \n", LZ4F_getErrorName(v)); goto _output_error; } #define CHECK(f) { LZ4F_errorCode_t const CHECK_V(err_ , f); } int basicTests(U32 seed, double compressibility) @@ -795,8 +795,9 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi clock_t const startClock = clock(); clock_t const clockDuration = duration_s * CLOCKS_PER_SEC; # undef CHECK -# define CHECK(cond, ...) if (cond) { DISPLAY("Error => "); DISPLAY(__VA_ARGS__); \ - DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); goto _output_error; } +# define EXIT_MSG(...) { DISPLAY("Error => "); DISPLAY(__VA_ARGS__); \ + DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); goto _output_error; } +# define CHECK(cond, ...) { if (cond) { EXIT_MSG(__VA_ARGS__); } } /* Create buffers */ { size_t const creationStatus = LZ4F_createDecompressionContext(&dCtx, LZ4F_VERSION); @@ -950,9 +951,10 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi CHECK(decSize != 0, "Frame decompression failed (error %i)", (int)decSize); if (totalOut) { /* otherwise, it's a skippable frame */ U64 const crcDecoded = XXH64_digest(&xxh64); - if (crcDecoded != crcOrig) locateBuffDiff(srcStart, decodedBuffer, srcSize, nonContiguousDst); - CHECK(crcDecoded != crcOrig, "Decompression corruption"); - } + if (crcDecoded != crcOrig) { + locateBuffDiff(srcStart, decodedBuffer, srcSize, nonContiguousDst); + EXIT_MSG("Decompression corruption"); + } } } } diff --git a/tests/fuzzer.c b/tests/fuzzer.c index 14cd8a0..78f90a1 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -327,12 +327,13 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c int result = 0; unsigned cycleNb; -# define FUZ_CHECKTEST(cond, ...) \ - if (cond) { \ - printf("Test %u : ", testNb); printf(__VA_ARGS__); \ - printf(" (seed %u, cycle %u) \n", seed, cycleNb); \ - exit(1); \ - } +# define EXIT_MSG(...) { \ + printf("Test %u : ", testNb); printf(__VA_ARGS__); \ + printf(" (seed %u, cycle %u) \n", seed, cycleNb); \ + exit(1); \ +} + +# define FUZ_CHECKTEST(cond, ...) { if (cond) { EXIT_MSG(__VA_ARGS__) } } # define FUZ_DISPLAYTEST(...) { \ testNb++; \ @@ -666,8 +667,10 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c ret = LZ4_decompress_fast_usingDict(compressedBuffer, decodedBuffer+dictSize, blockSize, decodedBuffer, dictSize); FUZ_CHECKTEST(ret!=blockContinueCompressedSize, "LZ4_decompress_fast_usingDict did not read all compressed block input"); { U32 const crcCheck = XXH32(decodedBuffer+dictSize, (size_t)blockSize, 0); - if (crcCheck!=crcOrig) FUZ_findDiff(block, decodedBuffer); - FUZ_CHECKTEST(crcCheck!=crcOrig, "LZ4_decompress_fast_usingDict corrupted decoded data (dict %i)", dictSize); + if (crcCheck!=crcOrig) { + FUZ_findDiff(block, decodedBuffer); + EXIT_MSG("LZ4_decompress_fast_usingDict corrupted decoded data (dict %i)", dictSize); + } } FUZ_DISPLAYTEST("test LZ4_decompress_safe_usingDict()"); @@ -706,9 +709,11 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c ret = LZ4_decompress_fast_usingDict(compressedBuffer, decodedBuffer, blockSize, dict, dictSize); FUZ_CHECKTEST(ret!=blockContinueCompressedSize, "LZ4_decompress_fast_usingDict did not read all compressed block input"); FUZ_CHECKTEST(decodedBuffer[blockSize], "LZ4_decompress_fast_usingDict overrun specified output buffer size"); - { U32 const crcCheck = XXH32(decodedBuffer, blockSize, 0); - if (crcCheck!=crcOrig) FUZ_findDiff(block, decodedBuffer); - FUZ_CHECKTEST(crcCheck!=crcOrig, "LZ4_decompress_fast_usingDict corrupted decoded data (dict %i)", dictSize); + { U32 const crcCheck = XXH32(decodedBuffer, (size_t)blockSize, 0); + if (crcCheck!=crcOrig) { + FUZ_findDiff(block, decodedBuffer); + EXIT_MSG("LZ4_decompress_fast_usingDict corrupted decoded data (dict %i)", dictSize); + } } FUZ_DISPLAYTEST(); @@ -804,8 +809,10 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c FUZ_CHECKTEST(ret!=blockContinueCompressedSize, "LZ4_decompress_fast_usingDict did not read all compressed block input"); FUZ_CHECKTEST(decodedBuffer[blockSize], "LZ4_decompress_fast_usingDict overrun specified output buffer size"); { U32 const crcCheck = XXH32(decodedBuffer, blockSize, 0); - if (crcCheck!=crcOrig) FUZ_findDiff(block, decodedBuffer); - FUZ_CHECKTEST(crcCheck!=crcOrig, "LZ4_decompress_fast_usingDict corrupted decoded data (dict %i)", dictSize); + if (crcCheck!=crcOrig) { + FUZ_findDiff(block, decodedBuffer); + EXIT_MSG("LZ4_decompress_fast_usingDict corrupted decoded data (dict %i)", dictSize); + } } FUZ_DISPLAYTEST(); @@ -867,9 +874,10 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c FUZ_CHECKTEST(ret!=blockSize, "LZ4_decompress_safe_usingDict did not regenerate original data"); FUZ_CHECKTEST(decodedBuffer[blockSize], "LZ4_decompress_safe_usingDict overrun specified output buffer size"); { U32 const crcCheck = XXH32(decodedBuffer, (size_t)blockSize, 0); - if (crcCheck!=crcOrig) FUZ_findDiff(block, decodedBuffer); - FUZ_CHECKTEST(crcCheck!=crcOrig, "LZ4_decompress_safe_usingDict corrupted decoded data"); - } + if (crcCheck!=crcOrig) { + FUZ_findDiff(block, decodedBuffer); + EXIT_MSG("LZ4_decompress_safe_usingDict corrupted decoded data"); + } } /* Compress HC using external dictionary stream */ FUZ_DISPLAYTEST(); @@ -914,9 +922,10 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c FUZ_CHECKTEST(ret!=blockSize, "LZ4_decompress_safe_usingDict did not regenerate original data"); FUZ_CHECKTEST(decodedBuffer[blockSize], "LZ4_decompress_safe_usingDict overrun specified output buffer size"); { U32 const crcCheck = XXH32(decodedBuffer, (size_t)blockSize, 0); - if (crcCheck!=crcOrig) FUZ_findDiff(block, decodedBuffer); - FUZ_CHECKTEST(crcCheck!=crcOrig, "LZ4_decompress_safe_usingDict corrupted decoded data"); - } + if (crcCheck!=crcOrig) { + FUZ_findDiff(block, decodedBuffer); + EXIT_MSG("LZ4_decompress_safe_usingDict corrupted decoded data"); + } } /* Compress HC continue destSize */ FUZ_DISPLAYTEST(); @@ -938,9 +947,10 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c FUZ_CHECKTEST(decodedBuffer[consumedSize], "LZ4_decompress_safe_usingDict overrun specified output buffer size") { U32 const crcSrc = XXH32(block, (size_t)consumedSize, 0); U32 const crcDst = XXH32(decodedBuffer, (size_t)consumedSize, 0); - if (crcSrc!=crcDst) FUZ_findDiff(block, decodedBuffer); - FUZ_CHECKTEST(crcSrc!=crcDst, "LZ4_decompress_safe_usingDict corrupted decoded data"); - } + if (crcSrc!=crcDst) { + FUZ_findDiff(block, decodedBuffer); + EXIT_MSG("LZ4_decompress_safe_usingDict corrupted decoded data"); + } } } /* ***** End of tests *** */ @@ -981,9 +991,8 @@ static void FUZ_unitTests(int compressionLevel) const unsigned cycleNb= 0; char testInput[testInputSize]; char testCompressed[testCompressedSize]; - size_t const testVerifySize = testInputSize; char testVerify[testInputSize]; - char ringBuffer[ringBufferSize]; + char ringBuffer[ringBufferSize] = {0}; U32 randState = 1; /* Init */ @@ -1026,7 +1035,6 @@ static void FUZ_unitTests(int compressionLevel) U32 rNext = 0; U32 dNext = 0; const U32 dBufferSize = ringBufferSize + maxMessageSizeMask; - int compressedSize; XXH64_reset(&xxhOrig, 0); XXH64_reset(&xxhNewSafe, 0); @@ -1036,6 +1044,7 @@ static void FUZ_unitTests(int compressionLevel) LZ4_setStreamDecode(&decodeStateFast, NULL, 0); while (iNext + messageSize < testCompressedSize) { + int compressedSize; XXH64_update(&xxhOrig, testInput + iNext, messageSize); crcOrig = XXH64_digest(&xxhOrig); @@ -1231,7 +1240,6 @@ static void FUZ_unitTests(int compressionLevel) U32 rNext = 0; U32 dNext = 0; const U32 dBufferSize = ringBufferSize + maxMessageSizeMask; - int compressedSize; XXH64_reset(&xxhOrig, 0); XXH64_reset(&xxhNewSafe, 0); @@ -1241,6 +1249,7 @@ static void FUZ_unitTests(int compressionLevel) LZ4_setStreamDecode(&decodeStateFast, NULL, 0); while (iNext + messageSize < testCompressedSize) { + int compressedSize; XXH64_update(&xxhOrig, testInput + iNext, messageSize); crcOrig = XXH64_digest(&xxhOrig); @@ -1293,6 +1302,7 @@ static void FUZ_unitTests(int compressionLevel) int iNext = 0; int dNext = 0; int compressedSize; + size_t const testVerifySize = testInputSize; assert((size_t)dBufferSize * 2 + 1 < testVerifySize); /* space used by ringBufferSafe and ringBufferFast */ XXH64_reset(&xxhOrig, 0); @@ -1306,7 +1316,7 @@ static void FUZ_unitTests(int compressionLevel) /* first block */ messageSize = BSIZE1; /* note : we cheat a bit here, in theory no message should be > maxMessageSize. We just want to fill the decoding ring buffer once. */ - XXH64_update(&xxhOrig, testInput + iNext, messageSize); + XXH64_update(&xxhOrig, testInput + iNext, (size_t)messageSize); crcOrig = XXH64_digest(&xxhOrig); compressedSize = LZ4_compress_HC_continue(&sHC, testInput + iNext, testCompressed, messageSize, testCompressedSize-ringBufferSize); -- cgit v1.2.3 From 0b876db6d42ec22da0c635e97a0d690908f2104a Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 18 Apr 2019 16:06:02 -0700 Subject: address a few minor Visual warnings and created target cxx17build --- Makefile | 8 ++++++++ doc/lz4_manual.html | 29 +++++++++++++---------------- lib/lz4.h | 8 +++++--- lib/lz4hc.c | 1 + tests/frametest.c | 7 ++++--- tests/fullbench.c | 4 ++-- tests/fuzzer.c | 4 ++-- 7 files changed, 35 insertions(+), 26 deletions(-) diff --git a/Makefile b/Makefile index f3c6ce2..f3844a1 100644 --- a/Makefile +++ b/Makefile @@ -181,6 +181,14 @@ gpptest gpptest32: clean CC=$(CC) $(MAKE) -C $(PRGDIR) all CFLAGS="$(CFLAGS)" CC=$(CC) $(MAKE) -C $(TESTDIR) all CFLAGS="$(CFLAGS)" +cxx17build : CC = "$(CXX) -Wno-deprecated" +cxx17build : CFLAGS = -std=c++17 -Wall -Wextra -Wundef -Wshadow -Wcast-align -Werror +cxx17build : clean + $(CXX) -v + CC=$(CC) $(MAKE) -C $(LZ4DIR) all CFLAGS="$(CFLAGS)" + CC=$(CC) $(MAKE) -C $(PRGDIR) all CFLAGS="$(CFLAGS)" + CC=$(CC) $(MAKE) -C $(TESTDIR) all CFLAGS="$(CFLAGS)" + ctocpptest: LIBCC="$(CC)" ctocpptest: TESTCC="$(CXX)" ctocpptest: CFLAGS="" diff --git a/doc/lz4_manual.html b/doc/lz4_manual.html index 356a60d..ee43b8a 100644 --- a/doc/lz4_manual.html +++ b/doc/lz4_manual.html @@ -454,21 +454,18 @@ union LZ4_streamDecode_u {


    -
    LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe() instead") LZ4LIB_API
    -int LZ4_decompress_fast (const char* src, char* dst, int originalSize);
    -LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe_continue() instead") LZ4LIB_API
    -int LZ4_decompress_fast_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* src, char* dst, int originalSize);
    -LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe_usingDict() instead") LZ4LIB_API
    -int LZ4_decompress_fast_usingDict (const char* src, char* dst, int originalSize, const char* dictStart, int dictSize);
    -

    These functions used to be a bit faster than LZ4_decompress_safe(), - but situation has changed in recent versions. - Now, `LZ4_decompress_safe()` is as fast and sometimes even faster than `LZ4_decompress_fast()`. - Moreover, LZ4_decompress_safe() is protected vs malformed input, while `LZ4_decompress_fast()` is not, making it a security liability. +

    These functions used to be faster than LZ4_decompress_safe(), + but it has changed, and they are now slower than LZ4_decompress_safe(). + This is because LZ4_decompress_fast() doesn't know the input size, + and therefore must progress more cautiously in the input buffer to not read beyond the end of block. + On top of that `LZ4_decompress_fast()` is not protected vs malformed or malicious inputs, making it a security liability. As a consequence, LZ4_decompress_fast() is strongly discouraged, and deprecated. - Last LZ4_decompress_fast() specificity is that it can decompress a block without knowing its compressed size. - Note that even that functionality could be achieved in a more secure manner if need be, - though it would require new prototypes, and adaptation of the implementation to this new use case. + The last remaining LZ4_decompress_fast() specificity is that + it can decompress a block without knowing its compressed size. + Such functionality could be achieved in a more secure manner, + by also providing the maximum size of input buffer, + but it would require new prototypes, and adaptation of the implementation to this new use case. Parameters: originalSize : is the uncompressed size to regenerate. @@ -477,9 +474,9 @@ int LZ4_decompress_fast_usingDict (const char* src, char* dst, int originalSize, The function expects to finish at block's end exactly. If the source stream is detected malformed, the function stops decoding and returns a negative result. note : LZ4_decompress_fast*() requires originalSize. Thanks to this information, it never writes past the output buffer. - However, since it doesn't know its 'src' size, it may read an unknown amount of input, and overflow input buffer. - Also, since match offsets are not validated, match reads from 'src' may underflow. - These issues never happen if input data is correct. + However, since it doesn't know its 'src' size, it may read an unknown amount of input, past input buffer bounds. + Also, since match offsets are not validated, match reads from 'src' may underflow too. + These issues never happen if input (compressed) data is correct. But they may happen if input data is invalid (error or intentional tampering). As a consequence, use these functions in trusted environments with trusted data **only**. diff --git a/lib/lz4.h b/lib/lz4.h index 962f5e6..737a0c7 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -638,9 +638,11 @@ LZ4_DEPRECATED("use LZ4_decompress_fast_usingDict() instead") LZ4LIB_API int LZ4 * On top of that `LZ4_decompress_fast()` is not protected vs malformed or malicious inputs, making it a security liability. * As a consequence, LZ4_decompress_fast() is strongly discouraged, and deprecated. * - * Only LZ4_decompress_fast() specificity is that it can decompress a block without knowing its compressed size. - * Even that functionality could be achieved in a more secure manner if need be, - * though it would require new prototypes, and adaptation of the implementation to this new use case. + * The last remaining LZ4_decompress_fast() specificity is that + * it can decompress a block without knowing its compressed size. + * Such functionality could be achieved in a more secure manner, + * by also providing the maximum size of input buffer, + * but it would require new prototypes, and adaptation of the implementation to this new use case. * * Parameters: * originalSize : is the uncompressed size to regenerate. diff --git a/lib/lz4hc.c b/lib/lz4hc.c index d5f6743..031df8f 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -1396,6 +1396,7 @@ static int LZ4HC_compress_optimal ( LZ4HC_CCtx_internal* ctx, } } } /* for (cur = 1; cur <= last_match_pos; cur++) */ + assert(last_match_pos < LZ4_OPT_NUM + TRAILING_LITERALS); best_mlen = opt[last_match_pos].mlen; best_off = opt[last_match_pos].off; cur = last_match_pos - best_mlen; diff --git a/tests/frametest.c b/tests/frametest.c index 9f7cb8d..bf95beb 100644 --- a/tests/frametest.c +++ b/tests/frametest.c @@ -667,8 +667,8 @@ int basicTests(U32 seed, double compressibility) for (blockSizeID = 4; blockSizeID < 8; ++blockSizeID) { result = LZ4F_getBlockSize(blockSizeID); CHECK(result); - DISPLAYLEVEL(3, "Returned block size of %zu bytes for blockID %u \n", - result, blockSizeID); + DISPLAYLEVEL(3, "Returned block size of %u bytes for blockID %u \n", + (unsigned)result, blockSizeID); } /* Test an invalid input that's too large */ @@ -770,7 +770,8 @@ static void locateBuffDiff(const void* buff1, const void* buff2, size_t size, un const BYTE* b2=(const BYTE*)buff2; DISPLAY("locateBuffDiff: looking for error position \n"); if (nonContiguous) { - DISPLAY("mode %u: non-contiguous output (%zu bytes), cannot search \n", nonContiguous, size); + DISPLAY("mode %u: non-contiguous output (%u bytes), cannot search \n", + nonContiguous, (unsigned)size); return; } while (p < size && b1[p]==b2[p]) p++; diff --git a/tests/fullbench.c b/tests/fullbench.c index 1a52aab..d2af662 100644 --- a/tests/fullbench.c +++ b/tests/fullbench.c @@ -162,13 +162,13 @@ static LZ4_stream_t LZ4_stream; static void local_LZ4_resetDictT(void) { void* const r = LZ4_initStream(&LZ4_stream, sizeof(LZ4_stream)); - assert(r != NULL); + assert(r != NULL); (void)r; } static void local_LZ4_createStream(void) { void* const r = LZ4_initStream(&LZ4_stream, sizeof(LZ4_stream)); - assert(r != NULL); + assert(r != NULL); (void)r; } static int local_LZ4_saveDict(const char* in, char* out, int inSize) diff --git a/tests/fuzzer.c b/tests/fuzzer.c index 78f90a1..9908a7b 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -490,6 +490,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c /* Test decompress_fast() with input buffer size exactly correct => must not read out of bound */ { char* const cBuffer_exact = (char*)malloc((size_t)compressedSize); assert(cBuffer_exact != NULL); + assert(compressedSize <= compressedBufferSize); memcpy(cBuffer_exact, compressedBuffer, compressedSize); /* Test decoding with output size exactly correct => must work */ @@ -1302,9 +1303,8 @@ static void FUZ_unitTests(int compressionLevel) int iNext = 0; int dNext = 0; int compressedSize; - size_t const testVerifySize = testInputSize; - assert((size_t)dBufferSize * 2 + 1 < testVerifySize); /* space used by ringBufferSafe and ringBufferFast */ + assert((size_t)dBufferSize * 2 + 1 < testInputSize); /* space used by ringBufferSafe and ringBufferFast */ XXH64_reset(&xxhOrig, 0); XXH64_reset(&xxhNewSafe, 0); XXH64_reset(&xxhNewFast, 0); -- cgit v1.2.3 From 6cb084ed368ab8a4df3b48eb9683101c5e518418 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 18 Apr 2019 16:41:27 -0700 Subject: fuzzer: reduce stack usage to please Visual static analyzer --- tests/fuzzer.c | 124 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 66 insertions(+), 58 deletions(-) diff --git a/tests/fuzzer.c b/tests/fuzzer.c index 9908a7b..a99f2f3 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -319,8 +319,8 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c void* const lowAddrBuffer = FUZ_createLowAddr(labSize); void* const stateLZ4 = malloc((size_t)LZ4_sizeofState()); void* const stateLZ4HC = malloc((size_t)LZ4_sizeofStateHC()); - LZ4_stream_t LZ4dict; - LZ4_streamHC_t LZ4dictHC; + LZ4_stream_t LZ4dictBody; + LZ4_streamHC_t* LZ4dictHC = LZ4_createStreamHC(); U32 coreRandState = seed; clock_t const clockStart = clock(); clock_t const clockDuration = (clock_t)duration_s * CLOCKS_PER_SEC; @@ -346,12 +346,11 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c /* init */ - if(!CNBuffer || !compressedBuffer || !decodedBuffer) { + if(!CNBuffer || !compressedBuffer || !decodedBuffer || !LZ4dictHC) { DISPLAY("Not enough memory to start fuzzer tests"); exit(1); } - if ( LZ4_initStream(&LZ4dict, sizeof(LZ4dict)) == NULL) abort(); - if ( LZ4_initStreamHC(&LZ4dictHC, sizeof(LZ4dictHC)) == NULL) abort(); + if ( LZ4_initStream(&LZ4dictBody, sizeof(LZ4dictBody)) == NULL) abort(); { U32 randState = coreRandState ^ PRIME3; FUZ_fillCompressibleNoiseBuffer(CNBuffer, COMPRESSIBLE_NOISE_LENGTH, compressibility, &randState); } @@ -455,42 +454,42 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c /* Test compression HC */ FUZ_DISPLAYTEST("test LZ4_compress_HC()"); - ret = LZ4_compress_HC(block, compressedBuffer, blockSize, (int)compressedBufferSize, compressionLevel); - FUZ_CHECKTEST(ret==0, "LZ4_compress_HC() failed"); - HCcompressedSize = ret; + HCcompressedSize = LZ4_compress_HC(block, compressedBuffer, blockSize, (int)compressedBufferSize, compressionLevel); + FUZ_CHECKTEST(HCcompressedSize==0, "LZ4_compress_HC() failed"); /* Test compression HC using external state */ FUZ_DISPLAYTEST("test LZ4_compress_HC_extStateHC()"); - ret = LZ4_compress_HC_extStateHC(stateLZ4HC, block, compressedBuffer, blockSize, (int)compressedBufferSize, compressionLevel); - FUZ_CHECKTEST(ret==0, "LZ4_compress_HC_extStateHC() failed") + { int const r = LZ4_compress_HC_extStateHC(stateLZ4HC, block, compressedBuffer, blockSize, (int)compressedBufferSize, compressionLevel); + FUZ_CHECKTEST(r==0, "LZ4_compress_HC_extStateHC() failed") + } /* Test compression HC using fast reset external state */ FUZ_DISPLAYTEST("test LZ4_compress_HC_extStateHC_fastReset()"); - ret = LZ4_compress_HC_extStateHC_fastReset(stateLZ4HC, block, compressedBuffer, blockSize, (int)compressedBufferSize, compressionLevel); - FUZ_CHECKTEST(ret==0, "LZ4_compress_HC_extStateHC_fastReset() failed"); + { int const r = LZ4_compress_HC_extStateHC_fastReset(stateLZ4HC, block, compressedBuffer, blockSize, (int)compressedBufferSize, compressionLevel); + FUZ_CHECKTEST(r==0, "LZ4_compress_HC_extStateHC_fastReset() failed"); + } /* Test compression using external state */ FUZ_DISPLAYTEST("test LZ4_compress_fast_extState()"); - ret = LZ4_compress_fast_extState(stateLZ4, block, compressedBuffer, blockSize, (int)compressedBufferSize, 8); - FUZ_CHECKTEST(ret==0, "LZ4_compress_fast_extState() failed"); + { int const r = LZ4_compress_fast_extState(stateLZ4, block, compressedBuffer, blockSize, (int)compressedBufferSize, 8); + FUZ_CHECKTEST(r==0, "LZ4_compress_fast_extState() failed"); } /* Test compression using fast reset external state*/ FUZ_DISPLAYTEST(); - ret = LZ4_compress_fast_extState_fastReset(stateLZ4, block, compressedBuffer, blockSize, (int)compressedBufferSize, 8); - FUZ_CHECKTEST(ret==0, "LZ4_compress_fast_extState_fastReset() failed"); + { int const r = LZ4_compress_fast_extState_fastReset(stateLZ4, block, compressedBuffer, blockSize, (int)compressedBufferSize, 8); + FUZ_CHECKTEST(r==0, "LZ4_compress_fast_extState_fastReset() failed"); } /* Test compression */ FUZ_DISPLAYTEST("test LZ4_compress_default()"); - ret = LZ4_compress_default(block, compressedBuffer, blockSize, (int)compressedBufferSize); - FUZ_CHECKTEST(ret<=0, "LZ4_compress_default() failed"); - compressedSize = ret; + compressedSize = LZ4_compress_default(block, compressedBuffer, blockSize, (int)compressedBufferSize); + FUZ_CHECKTEST(compressedSize<=0, "LZ4_compress_default() failed"); /* Decompression tests */ /* Test decompress_fast() with input buffer size exactly correct => must not read out of bound */ { char* const cBuffer_exact = (char*)malloc((size_t)compressedSize); assert(cBuffer_exact != NULL); - assert(compressedSize <= compressedBufferSize); + assert(compressedSize <= (int)compressedBufferSize); memcpy(cBuffer_exact, compressedBuffer, compressedSize); /* Test decoding with output size exactly correct => must work */ @@ -685,20 +684,20 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c FUZ_DISPLAYTEST("test LZ4_compress_fast_continue(), with non-contiguous dictionary"); dict -= (size_t)(FUZ_rand(&randState) & 0xF) + 1; /* create space, so now dictionary is an ExtDict */ if (dict < (char*)CNBuffer) dict = (char*)CNBuffer; - LZ4_loadDict(&LZ4dict, dict, dictSize); - blockContinueCompressedSize = LZ4_compress_fast_continue(&LZ4dict, block, compressedBuffer, blockSize, (int)compressedBufferSize, 1); + LZ4_loadDict(&LZ4dictBody, dict, dictSize); + blockContinueCompressedSize = LZ4_compress_fast_continue(&LZ4dictBody, block, compressedBuffer, blockSize, (int)compressedBufferSize, 1); FUZ_CHECKTEST(blockContinueCompressedSize==0, "LZ4_compress_fast_continue failed"); FUZ_DISPLAYTEST("test LZ4_compress_fast_continue() with dictionary but with an output buffer too short by one byte"); - LZ4_loadDict(&LZ4dict, dict, dictSize); - ret = LZ4_compress_fast_continue(&LZ4dict, block, compressedBuffer, blockSize, blockContinueCompressedSize-1, 1); + LZ4_loadDict(&LZ4dictBody, dict, dictSize); + ret = LZ4_compress_fast_continue(&LZ4dictBody, block, compressedBuffer, blockSize, blockContinueCompressedSize-1, 1); FUZ_CHECKTEST(ret>0, "LZ4_compress_fast_continue using ExtDict should fail : one missing byte for output buffer : %i written, %i buffer", ret, blockContinueCompressedSize); FUZ_DISPLAYTEST("test LZ4_compress_fast_continue() with dictionary loaded with LZ4_loadDict()"); DISPLAYLEVEL(5, " compress %i bytes from buffer(%p) into dst(%p) using dict(%p) of size %i \n", blockSize, (const void *)block, (void *)decodedBuffer, (const void *)dict, dictSize); - LZ4_loadDict(&LZ4dict, dict, dictSize); - ret = LZ4_compress_fast_continue(&LZ4dict, block, compressedBuffer, blockSize, blockContinueCompressedSize, 1); + LZ4_loadDict(&LZ4dictBody, dict, dictSize); + ret = LZ4_compress_fast_continue(&LZ4dictBody, block, compressedBuffer, blockSize, blockContinueCompressedSize, 1); FUZ_CHECKTEST(ret!=blockContinueCompressedSize, "LZ4_compress_limitedOutput_compressed size is different (%i != %i)", ret, blockContinueCompressedSize); FUZ_CHECKTEST(ret<=0, "LZ4_compress_fast_continue should work : enough size available within output buffer"); @@ -754,15 +753,15 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c U32 expectedCrc; FUZ_DISPLAYTEST("LZ4_compress_fast_continue() after LZ4_loadDict()"); - LZ4_loadDict(&LZ4dict, dict, dictSize); - expectedSize = LZ4_compress_fast_continue(&LZ4dict, block, compressedBuffer, blockSize, (int)compressedBufferSize, 1); + LZ4_loadDict(&LZ4dictBody, dict, dictSize); + expectedSize = LZ4_compress_fast_continue(&LZ4dictBody, block, compressedBuffer, blockSize, (int)compressedBufferSize, 1); FUZ_CHECKTEST(expectedSize<=0, "LZ4_compress_fast_continue reference compression for extDictCtx should have succeeded"); expectedCrc = XXH32(compressedBuffer, expectedSize, 0); FUZ_DISPLAYTEST("LZ4_compress_fast_continue() after LZ4_attach_dictionary()"); - LZ4_loadDict(&LZ4dict, dict, dictSize); + LZ4_loadDict(&LZ4dictBody, dict, dictSize); LZ4_initStream(&LZ4_stream, sizeof(LZ4_stream)); - LZ4_attach_dictionary(&LZ4_stream, &LZ4dict); + LZ4_attach_dictionary(&LZ4_stream, &LZ4dictBody); blockContinueCompressedSize = LZ4_compress_fast_continue(&LZ4_stream, block, compressedBuffer, blockSize, (int)compressedBufferSize, 1); FUZ_CHECKTEST(blockContinueCompressedSize==0, "LZ4_compress_fast_continue using extDictCtx failed"); FUZ_CHECKTEST(LZ4_stream.internal_donotuse.dirty, "context should be good"); @@ -777,14 +776,14 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c FUZ_DISPLAYTEST("LZ4_compress_fast_continue() after LZ4_attach_dictionary(), but output buffer is 1 byte too short"); LZ4_resetStream_fast(&LZ4_stream); - LZ4_attach_dictionary(&LZ4_stream, &LZ4dict); + LZ4_attach_dictionary(&LZ4_stream, &LZ4dictBody); ret = LZ4_compress_fast_continue(&LZ4_stream, block, compressedBuffer, blockSize, blockContinueCompressedSize-1, 1); FUZ_CHECKTEST(ret>0, "LZ4_compress_fast_continue using extDictCtx should fail : one missing byte for output buffer : %i written, %i buffer", ret, blockContinueCompressedSize); /* note : context is no longer dirty after a failed compressed block */ FUZ_DISPLAYTEST(); LZ4_resetStream_fast(&LZ4_stream); - LZ4_attach_dictionary(&LZ4_stream, &LZ4dict); + LZ4_attach_dictionary(&LZ4_stream, &LZ4dictBody); ret = LZ4_compress_fast_continue(&LZ4_stream, block, compressedBuffer, blockSize, blockContinueCompressedSize, 1); FUZ_CHECKTEST(ret!=blockContinueCompressedSize, "LZ4_compress_limitedOutput_compressed size is different (%i != %i)", ret, blockContinueCompressedSize); FUZ_CHECKTEST(ret<=0, "LZ4_compress_fast_continue using extDictCtx should work : enough size available within output buffer"); @@ -794,7 +793,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c FUZ_DISPLAYTEST(); LZ4_resetStream_fast(&LZ4_stream); - LZ4_attach_dictionary(&LZ4_stream, &LZ4dict); + LZ4_attach_dictionary(&LZ4_stream, &LZ4dictBody); ret = LZ4_compress_fast_continue(&LZ4_stream, block, compressedBuffer, blockSize, blockContinueCompressedSize, 1); FUZ_CHECKTEST(ret!=blockContinueCompressedSize, "LZ4_compress_limitedOutput_compressed size is different (%i != %i)", ret, blockContinueCompressedSize); FUZ_CHECKTEST(ret<=0, "LZ4_compress_fast_continue using extDictCtx with re-used context should work : enough size available within output buffer"); @@ -850,24 +849,24 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c FUZ_DISPLAYTEST("LZ4_compress_HC_continue with an external dictionary"); dict -= (FUZ_rand(&randState) & 7); /* even bigger separation */ if (dict < (char*)CNBuffer) dict = (char*)CNBuffer; - LZ4_loadDictHC(&LZ4dictHC, dict, dictSize); - LZ4_setCompressionLevel (&LZ4dictHC, compressionLevel); - blockContinueCompressedSize = LZ4_compress_HC_continue(&LZ4dictHC, block, compressedBuffer, blockSize, (int)compressedBufferSize); + LZ4_loadDictHC(LZ4dictHC, dict, dictSize); + LZ4_setCompressionLevel (LZ4dictHC, compressionLevel); + blockContinueCompressedSize = LZ4_compress_HC_continue(LZ4dictHC, block, compressedBuffer, blockSize, (int)compressedBufferSize); FUZ_CHECKTEST(blockContinueCompressedSize==0, "LZ4_compress_HC_continue failed"); - FUZ_CHECKTEST(LZ4dictHC.internal_donotuse.dirty, "Context should be clean"); + FUZ_CHECKTEST(LZ4dictHC->internal_donotuse.dirty, "Context should be clean"); FUZ_DISPLAYTEST("LZ4_compress_HC_continue with same external dictionary, but output buffer 1 byte too short"); - LZ4_loadDictHC(&LZ4dictHC, dict, dictSize); - ret = LZ4_compress_HC_continue(&LZ4dictHC, block, compressedBuffer, blockSize, blockContinueCompressedSize-1); + LZ4_loadDictHC(LZ4dictHC, dict, dictSize); + ret = LZ4_compress_HC_continue(LZ4dictHC, block, compressedBuffer, blockSize, blockContinueCompressedSize-1); FUZ_CHECKTEST(ret>0, "LZ4_compress_HC_continue using ExtDict should fail : one missing byte for output buffer (expected %i, but result=%i)", blockContinueCompressedSize, ret); /* note : context is no longer dirty after a failed compressed block */ FUZ_DISPLAYTEST("LZ4_compress_HC_continue with same external dictionary, and output buffer exactly the right size"); - LZ4_loadDictHC(&LZ4dictHC, dict, dictSize); - ret = LZ4_compress_HC_continue(&LZ4dictHC, block, compressedBuffer, blockSize, blockContinueCompressedSize); + LZ4_loadDictHC(LZ4dictHC, dict, dictSize); + ret = LZ4_compress_HC_continue(LZ4dictHC, block, compressedBuffer, blockSize, blockContinueCompressedSize); FUZ_CHECKTEST(ret!=blockContinueCompressedSize, "LZ4_compress_HC_continue size is different : ret(%i) != expected(%i)", ret, blockContinueCompressedSize); FUZ_CHECKTEST(ret<=0, "LZ4_compress_HC_continue should work : enough size available within output buffer"); - FUZ_CHECKTEST(LZ4dictHC.internal_donotuse.dirty, "Context should be clean"); + FUZ_CHECKTEST(LZ4dictHC->internal_donotuse.dirty, "Context should be clean"); FUZ_DISPLAYTEST(); decodedBuffer[blockSize] = 0; @@ -886,8 +885,8 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c LZ4_streamHC_t LZ4_streamHC; LZ4_initStreamHC(&LZ4_streamHC, sizeof(LZ4_streamHC)); - LZ4_loadDictHC(&LZ4dictHC, dict, dictSize); - LZ4_attach_HC_dictionary(&LZ4_streamHC, &LZ4dictHC); + LZ4_loadDictHC(LZ4dictHC, dict, dictSize); + LZ4_attach_HC_dictionary(&LZ4_streamHC, LZ4dictHC); LZ4_setCompressionLevel (&LZ4_streamHC, compressionLevel); blockContinueCompressedSize = LZ4_compress_HC_continue(&LZ4_streamHC, block, compressedBuffer, blockSize, (int)compressedBufferSize); FUZ_CHECKTEST(blockContinueCompressedSize==0, "LZ4_compress_HC_continue with ExtDictCtx failed"); @@ -895,14 +894,14 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c FUZ_DISPLAYTEST(); LZ4_resetStreamHC_fast (&LZ4_streamHC, compressionLevel); - LZ4_attach_HC_dictionary(&LZ4_streamHC, &LZ4dictHC); + LZ4_attach_HC_dictionary(&LZ4_streamHC, LZ4dictHC); ret = LZ4_compress_HC_continue(&LZ4_streamHC, block, compressedBuffer, blockSize, blockContinueCompressedSize-1); FUZ_CHECKTEST(ret>0, "LZ4_compress_HC_continue using ExtDictCtx should fail : one missing byte for output buffer (%i != %i)", ret, blockContinueCompressedSize); /* note : context is no longer dirty after a failed compressed block */ FUZ_DISPLAYTEST(); LZ4_resetStreamHC_fast (&LZ4_streamHC, compressionLevel); - LZ4_attach_HC_dictionary(&LZ4_streamHC, &LZ4dictHC); + LZ4_attach_HC_dictionary(&LZ4_streamHC, LZ4dictHC); ret = LZ4_compress_HC_continue(&LZ4_streamHC, block, compressedBuffer, blockSize, blockContinueCompressedSize); FUZ_CHECKTEST(ret!=blockContinueCompressedSize, "LZ4_compress_HC_continue using ExtDictCtx size is different (%i != %i)", ret, blockContinueCompressedSize); FUZ_CHECKTEST(ret<=0, "LZ4_compress_HC_continue using ExtDictCtx should work : enough size available within output buffer"); @@ -910,7 +909,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c FUZ_DISPLAYTEST(); LZ4_resetStreamHC_fast (&LZ4_streamHC, compressionLevel); - LZ4_attach_HC_dictionary(&LZ4_streamHC, &LZ4dictHC); + LZ4_attach_HC_dictionary(&LZ4_streamHC, LZ4dictHC); ret = LZ4_compress_HC_continue(&LZ4_streamHC, block, compressedBuffer, blockSize, blockContinueCompressedSize); FUZ_CHECKTEST(ret!=blockContinueCompressedSize, "LZ4_compress_HC_continue using ExtDictCtx and fast reset size is different (%i != %i)", ret, blockContinueCompressedSize); FUZ_CHECKTEST(ret<=0, "LZ4_compress_HC_continue using ExtDictCtx and fast reset should work : enough size available within output buffer"); @@ -933,9 +932,9 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c { int const availableSpace = (int)(FUZ_rand(&randState) % blockSize) + 5; int consumedSize = blockSize; FUZ_DISPLAYTEST(); - LZ4_loadDictHC(&LZ4dictHC, dict, dictSize); - LZ4_setCompressionLevel(&LZ4dictHC, compressionLevel); - blockContinueCompressedSize = LZ4_compress_HC_continue_destSize(&LZ4dictHC, block, compressedBuffer, &consumedSize, availableSpace); + LZ4_loadDictHC(LZ4dictHC, dict, dictSize); + LZ4_setCompressionLevel(LZ4dictHC, compressionLevel); + blockContinueCompressedSize = LZ4_compress_HC_continue_destSize(LZ4dictHC, block, compressedBuffer, &consumedSize, availableSpace); DISPLAYLEVEL(5, " LZ4_compress_HC_continue_destSize : compressed %6i/%6i into %6i/%6i at cLevel=%i\n", consumedSize, blockSize, blockContinueCompressedSize, availableSpace, compressionLevel); FUZ_CHECKTEST(blockContinueCompressedSize==0, "LZ4_compress_HC_continue_destSize failed"); FUZ_CHECKTEST(blockContinueCompressedSize > availableSpace, "LZ4_compress_HC_continue_destSize write overflow"); @@ -975,6 +974,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c free(compressedBuffer); free(decodedBuffer); FUZ_freeLowAddr(lowAddrBuffer, labSize); + LZ4_freeStreamHC(LZ4dictHC); free(stateLZ4); free(stateLZ4HC); return result; @@ -990,28 +990,31 @@ static void FUZ_unitTests(int compressionLevel) const unsigned testNb = 0; const unsigned seed = 0; const unsigned cycleNb= 0; - char testInput[testInputSize]; - char testCompressed[testCompressedSize]; - char testVerify[testInputSize]; + char* testInput = (char*)malloc(testInputSize); + char* testCompressed = (char*)malloc(testCompressedSize); + char* testVerify = (char*)malloc(testInputSize); char ringBuffer[ringBufferSize] = {0}; U32 randState = 1; /* Init */ + if (!testInput || !testCompressed || !testVerify) { + EXIT_MSG("not enough memory for FUZ_unitTests"); + } FUZ_fillCompressibleNoiseBuffer(testInput, testInputSize, 0.50, &randState); /* 32-bits address space overflow test */ FUZ_AddressOverflow(); /* LZ4 streaming tests */ - { LZ4_stream_t* statePtr; - LZ4_stream_t streamingState; + { LZ4_stream_t streamingState; U64 crcOrig; int result; /* Allocation test */ - statePtr = LZ4_createStream(); - FUZ_CHECKTEST(statePtr==NULL, "LZ4_createStream() allocation failed"); - LZ4_freeStream(statePtr); + { LZ4_stream_t* const statePtr = LZ4_createStream(); + FUZ_CHECKTEST(statePtr==NULL, "LZ4_createStream() allocation failed"); + LZ4_freeStream(statePtr); + } /* simple compression test */ crcOrig = XXH64(testInput, testCompressedSize, 0); @@ -1389,6 +1392,11 @@ static void FUZ_unitTests(int compressionLevel) } } + /* clean up */ + free(testInput); + free(testCompressed); + free(testVerify); + printf("All unit tests completed successfully compressionLevel=%d \n", compressionLevel); return; } -- cgit v1.2.3 From 4790994568d4d3ba492857c4c567ea6517588a45 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 18 Apr 2019 17:12:14 -0700 Subject: fuzzer : reduced stack usage --- tests/fuzzer.c | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/tests/fuzzer.c b/tests/fuzzer.c index a99f2f3..ffbeefc 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -881,39 +881,39 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c /* Compress HC using external dictionary stream */ FUZ_DISPLAYTEST(); - { - LZ4_streamHC_t LZ4_streamHC; - LZ4_initStreamHC(&LZ4_streamHC, sizeof(LZ4_streamHC)); + { LZ4_streamHC_t* LZ4_streamHC = LZ4_createStreamHC(); LZ4_loadDictHC(LZ4dictHC, dict, dictSize); - LZ4_attach_HC_dictionary(&LZ4_streamHC, LZ4dictHC); - LZ4_setCompressionLevel (&LZ4_streamHC, compressionLevel); - blockContinueCompressedSize = LZ4_compress_HC_continue(&LZ4_streamHC, block, compressedBuffer, blockSize, (int)compressedBufferSize); + LZ4_attach_HC_dictionary(LZ4_streamHC, LZ4dictHC); + LZ4_setCompressionLevel (LZ4_streamHC, compressionLevel); + blockContinueCompressedSize = LZ4_compress_HC_continue(LZ4_streamHC, block, compressedBuffer, blockSize, (int)compressedBufferSize); FUZ_CHECKTEST(blockContinueCompressedSize==0, "LZ4_compress_HC_continue with ExtDictCtx failed"); - FUZ_CHECKTEST(LZ4_streamHC.internal_donotuse.dirty, "Context should be clean"); + FUZ_CHECKTEST(LZ4_streamHC->internal_donotuse.dirty, "Context should be clean"); FUZ_DISPLAYTEST(); - LZ4_resetStreamHC_fast (&LZ4_streamHC, compressionLevel); - LZ4_attach_HC_dictionary(&LZ4_streamHC, LZ4dictHC); - ret = LZ4_compress_HC_continue(&LZ4_streamHC, block, compressedBuffer, blockSize, blockContinueCompressedSize-1); + LZ4_resetStreamHC_fast (LZ4_streamHC, compressionLevel); + LZ4_attach_HC_dictionary(LZ4_streamHC, LZ4dictHC); + ret = LZ4_compress_HC_continue(LZ4_streamHC, block, compressedBuffer, blockSize, blockContinueCompressedSize-1); FUZ_CHECKTEST(ret>0, "LZ4_compress_HC_continue using ExtDictCtx should fail : one missing byte for output buffer (%i != %i)", ret, blockContinueCompressedSize); /* note : context is no longer dirty after a failed compressed block */ FUZ_DISPLAYTEST(); - LZ4_resetStreamHC_fast (&LZ4_streamHC, compressionLevel); - LZ4_attach_HC_dictionary(&LZ4_streamHC, LZ4dictHC); - ret = LZ4_compress_HC_continue(&LZ4_streamHC, block, compressedBuffer, blockSize, blockContinueCompressedSize); + LZ4_resetStreamHC_fast (LZ4_streamHC, compressionLevel); + LZ4_attach_HC_dictionary(LZ4_streamHC, LZ4dictHC); + ret = LZ4_compress_HC_continue(LZ4_streamHC, block, compressedBuffer, blockSize, blockContinueCompressedSize); FUZ_CHECKTEST(ret!=blockContinueCompressedSize, "LZ4_compress_HC_continue using ExtDictCtx size is different (%i != %i)", ret, blockContinueCompressedSize); FUZ_CHECKTEST(ret<=0, "LZ4_compress_HC_continue using ExtDictCtx should work : enough size available within output buffer"); - FUZ_CHECKTEST(LZ4_streamHC.internal_donotuse.dirty, "Context should be clean"); + FUZ_CHECKTEST(LZ4_streamHC->internal_donotuse.dirty, "Context should be clean"); FUZ_DISPLAYTEST(); - LZ4_resetStreamHC_fast (&LZ4_streamHC, compressionLevel); - LZ4_attach_HC_dictionary(&LZ4_streamHC, LZ4dictHC); - ret = LZ4_compress_HC_continue(&LZ4_streamHC, block, compressedBuffer, blockSize, blockContinueCompressedSize); + LZ4_resetStreamHC_fast (LZ4_streamHC, compressionLevel); + LZ4_attach_HC_dictionary(LZ4_streamHC, LZ4dictHC); + ret = LZ4_compress_HC_continue(LZ4_streamHC, block, compressedBuffer, blockSize, blockContinueCompressedSize); FUZ_CHECKTEST(ret!=blockContinueCompressedSize, "LZ4_compress_HC_continue using ExtDictCtx and fast reset size is different (%i != %i)", ret, blockContinueCompressedSize); FUZ_CHECKTEST(ret<=0, "LZ4_compress_HC_continue using ExtDictCtx and fast reset should work : enough size available within output buffer"); - FUZ_CHECKTEST(LZ4_streamHC.internal_donotuse.dirty, "Context should be clean"); + FUZ_CHECKTEST(LZ4_streamHC->internal_donotuse.dirty, "Context should be clean"); + + LZ4_freeStreamHC(LZ4_streamHC); } FUZ_DISPLAYTEST(); -- cgit v1.2.3 From 4e4f1ad623cca9ebd212400b5783c63fd76dc868 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 18 Apr 2019 17:26:01 -0700 Subject: ensure list of names is large enough --- programs/util.h | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/programs/util.h b/programs/util.h index 6a35481..1dd515c 100644 --- a/programs/util.h +++ b/programs/util.h @@ -379,7 +379,7 @@ UTIL_STATIC U64 UTIL_getTotalFileSize(const char** fileNamesTable, unsigned nbFi */ UTIL_STATIC void* UTIL_realloc(void* ptr, size_t size) { - void* newptr = realloc(ptr, size); + void* const newptr = realloc(ptr, size); if (newptr) return newptr; free(ptr); return NULL; @@ -529,7 +529,8 @@ UTIL_STATIC int UTIL_prepareFileList(const char* dirName, char** bufStart, size_ * In case of error UTIL_createFileList returns NULL and UTIL_freeFileList should not be called. */ UTIL_STATIC const char** -UTIL_createFileList(const char** inputNames, unsigned inputNamesNb, char** allocatedBuffer, unsigned* allocatedNamesNb) +UTIL_createFileList(const char** inputNames, unsigned inputNamesNb, + char** allocatedBuffer, unsigned* allocatedNamesNb) { size_t pos; unsigned i, nbFiles; @@ -543,16 +544,14 @@ UTIL_createFileList(const char** inputNames, unsigned inputNamesNb, char** alloc if (!UTIL_isDirectory(inputNames[i])) { size_t const len = strlen(inputNames[i]); if (pos + len >= bufSize) { - size_t newListSize = bufSize + LIST_SIZE_INCREASE; - buf = (char*)UTIL_realloc(buf, newListSize); - bufSize = newListSize; + while (pos + len >= bufSize) bufSize += LIST_SIZE_INCREASE; + buf = (char*)UTIL_realloc(buf, bufSize); if (!buf) return NULL; } - if (pos + len < bufSize) { - strncpy(buf + pos, inputNames[i], bufSize - pos); - pos += len + 1; - nbFiles++; - } + assert(pos + len < bufSize); + strncpy(buf + pos, inputNames[i], bufSize - pos); + pos += len + 1; + nbFiles++; } else { char* bufend = buf + bufSize; nbFiles += (unsigned)UTIL_prepareFileList(inputNames[i], &buf, &pos, &bufend); -- cgit v1.2.3 From ae199124e5aef12f6e467bc8cf30bffece55d55c Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 18 Apr 2019 18:50:51 -0700 Subject: fixed read-after input in LZ4_decompress_safe() --- lib/lz4.c | 57 ++++++++++----------- lib/lz4hc.c | 2 +- tests/fuzzer.c | 156 ++++++++++++++++++++++++++++++--------------------------- 3 files changed, 112 insertions(+), 103 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index c38932e..e614c45 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -136,7 +136,7 @@ #endif /* LZ4_FORCE_INLINE */ /* LZ4_FORCE_O2_GCC_PPC64LE and LZ4_FORCE_O2_INLINE_GCC_PPC64LE - * Gcc on ppc64le generates an unrolled SIMDized loop for LZ4_wildCopy, + * gcc on ppc64le generates an unrolled SIMDized loop for LZ4_wildCopy8, * together with a simple 8-byte copy loop as a fall-back path. * However, this optimization hurts the decompression speed by >30%, * because the execution does not go to the optimized loop @@ -144,10 +144,10 @@ * before going to the fall-back path become useless overhead. * This optimization happens only with the -O3 flag, and -O2 generates * a simple 8-byte copy loop. - * With gcc on ppc64le, all of the LZ4_decompress_* and LZ4_wildCopy + * With gcc on ppc64le, all of the LZ4_decompress_* and LZ4_wildCopy8 * functions are annotated with __attribute__((optimize("O2"))), - * and also LZ4_wildCopy is forcibly inlined, so that the O2 attribute - * of LZ4_wildCopy does not affect the compression speed. + * and also LZ4_wildCopy8 is forcibly inlined, so that the O2 attribute + * of LZ4_wildCopy8 does not affect the compression speed. */ #if defined(__PPC64__) && defined(__LITTLE_ENDIAN__) && defined(__GNUC__) && !defined(__clang__) # define LZ4_FORCE_O2_GCC_PPC64LE __attribute__((optimize("O2"))) @@ -301,7 +301,7 @@ static void LZ4_writeLE16(void* memPtr, U16 value) /* customized variant of memcpy, which can overwrite up to 8 bytes beyond dstEnd */ LZ4_FORCE_O2_INLINE_GCC_PPC64LE -void LZ4_wildCopy(void* dstPtr, const void* srcPtr, void* dstEnd) +void LZ4_wildCopy8(void* dstPtr, const void* srcPtr, void* dstEnd) { BYTE* d = (BYTE*)dstPtr; const BYTE* s = (const BYTE*)srcPtr; @@ -342,7 +342,7 @@ LZ4_memcpy_using_offset_base(BYTE* dstPtr, const BYTE* srcPtr, BYTE* dstEnd, con srcPtr += 8; } - LZ4_wildCopy(dstPtr, srcPtr, dstEnd); + LZ4_wildCopy8(dstPtr, srcPtr, dstEnd); } /* customized variant of memcpy, which can overwrite up to 32 bytes beyond dstEnd @@ -946,7 +946,7 @@ LZ4_FORCE_INLINE int LZ4_compress_generic( else *token = (BYTE)(litLength<= FASTLOOP_SAFE_DISTANCE); - + if (endOnInput) assert(ip < iend); token = *ip++; length = token >> ML_BITS; /* literal length */ @@ -1666,27 +1668,26 @@ LZ4_decompress_generic( /* copy literals */ cpy = op+length; LZ4_STATIC_ASSERT(MFLIMIT >= WILDCOPYLENGTH); - if ( ((endOnInput) && ((cpy>oend-FASTLOOP_SAFE_DISTANCE) || (ip+length>iend-(2+1+LASTLITERALS))) ) - || ((!endOnInput) && (cpy>oend-FASTLOOP_SAFE_DISTANCE)) ) - { - goto safe_literal_copy; - } - if (endOnInput) + if (endOnInput) { /* LZ4_decompress_safe() */ + if ((cpy>oend-32) || (ip+length>iend-32)) goto safe_literal_copy; LZ4_wildCopy32(op, ip, cpy); - else - LZ4_wildCopy(op, ip, cpy); /* LZ4_decompress_fast() cannot copy more than 8 bytes at a time : it doesn't know input length, and only relies on end-of-block properties */ + } else { /* LZ4_decompress_fast() */ + if (cpy>oend-8) goto safe_literal_copy; + LZ4_wildCopy8(op, ip, cpy); /* LZ4_decompress_fast() cannot copy more than 8 bytes at a time : + * it doesn't know input length, and only relies on end-of-block properties */ + } ip += length; op = cpy; } else { cpy = op+length; - /* We don't need to check oend, since we check it once for each loop below */ - if ( ((endOnInput) && (ip+16>iend-(2+1+LASTLITERALS)))) - { - goto safe_literal_copy; - } - /* Literals can only be 14, but hope compilers optimize if we copy by a register size */ - if (endOnInput) + if (endOnInput) { /* LZ4_decompress_safe() */ + DEBUGLOG(7, "copy %u bytes in a 16-bytes stripe", (unsigned)length); + /* We don't need to check oend, since we check it once for each loop below */ + if (ip > iend-(16 + 1/*max lit + offset + nextToken*/)) goto safe_literal_copy; + /* Literals can only be 14, but hope compilers optimize if we copy by a register size */ memcpy(op, ip, 16); - else { /* LZ4_decompress_fast() cannot copy more than 8 bytes at a time : it doesn't know input length, and only relies on end-of-block properties */ + } else { /* LZ4_decompress_fast() */ + /* LZ4_decompress_fast() cannot copy more than 8 bytes at a time : + * it doesn't know input length, and relies on end-of-block properties */ memcpy(op, ip, 8); if (length > 8) memcpy(op+8, ip+8, 8); } @@ -1852,7 +1853,7 @@ LZ4_decompress_generic( } } else { - LZ4_wildCopy(op, ip, cpy); /* may overwrite up to WILDCOPYLENGTH beyond cpy */ + LZ4_wildCopy8(op, ip, cpy); /* may overwrite up to WILDCOPYLENGTH beyond cpy */ ip += length; op = cpy; } @@ -1947,14 +1948,14 @@ LZ4_decompress_generic( BYTE* const oCopyLimit = oend - (WILDCOPYLENGTH-1); if (cpy > oend-LASTLITERALS) goto _output_error; /* Error : last LASTLITERALS bytes must be literals (uncompressed) */ if (op < oCopyLimit) { - LZ4_wildCopy(op, match, oCopyLimit); + LZ4_wildCopy8(op, match, oCopyLimit); match += oCopyLimit - op; op = oCopyLimit; } while (op < cpy) *op++ = *match++; } else { memcpy(op, match, 8); - if (length > 16) LZ4_wildCopy(op+8, match+8, cpy); + if (length > 16) LZ4_wildCopy8(op+8, match+8, cpy); } op = cpy; /* wildcopy correction */ } diff --git a/lib/lz4hc.c b/lib/lz4hc.c index 031df8f..936f739 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -442,7 +442,7 @@ LZ4_FORCE_INLINE int LZ4HC_encodeSequence ( } /* Copy Literals */ - LZ4_wildCopy(*op, *anchor, (*op) + length); + LZ4_wildCopy8(*op, *anchor, (*op) + length); *op += length; /* Encode Offset */ diff --git a/tests/fuzzer.c b/tests/fuzzer.c index ffbeefc..a5b5c93 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -494,9 +494,10 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c /* Test decoding with output size exactly correct => must work */ FUZ_DISPLAYTEST("LZ4_decompress_fast() with exact output buffer"); - ret = LZ4_decompress_fast(cBuffer_exact, decodedBuffer, blockSize); - FUZ_CHECKTEST(ret<0, "LZ4_decompress_fast failed despite correct space"); - FUZ_CHECKTEST(ret!=compressedSize, "LZ4_decompress_fast failed : did not fully read compressed data"); + { int const r = LZ4_decompress_fast(cBuffer_exact, decodedBuffer, blockSize); + FUZ_CHECKTEST(r<0, "LZ4_decompress_fast failed despite correct space"); + FUZ_CHECKTEST(r!=compressedSize, "LZ4_decompress_fast failed : did not fully read compressed data"); + } { U32 const crcCheck = XXH32(decodedBuffer, (size_t)blockSize, 0); FUZ_CHECKTEST(crcCheck!=crcOrig, "LZ4_decompress_fast corrupted decoded data"); } @@ -504,92 +505,75 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c /* Test decoding with one byte missing => must fail */ FUZ_DISPLAYTEST("LZ4_decompress_fast() with output buffer 1-byte too short"); decodedBuffer[blockSize-1] = 0; - ret = LZ4_decompress_fast(cBuffer_exact, decodedBuffer, blockSize-1); - FUZ_CHECKTEST(ret>=0, "LZ4_decompress_fast should have failed, due to Output Size being too small"); + { int const r = LZ4_decompress_fast(cBuffer_exact, decodedBuffer, blockSize-1); + FUZ_CHECKTEST(r>=0, "LZ4_decompress_fast should have failed, due to Output Size being too small"); + } FUZ_CHECKTEST(decodedBuffer[blockSize-1]!=0, "LZ4_decompress_fast overrun specified output buffer"); /* Test decoding with one byte too much => must fail */ FUZ_DISPLAYTEST(); - ret = LZ4_decompress_fast(cBuffer_exact, decodedBuffer, blockSize+1); - FUZ_CHECKTEST(ret>=0, "LZ4_decompress_fast should have failed, due to Output Size being too large"); - - free(cBuffer_exact); - } - - /* Test decoding with empty input */ - FUZ_DISPLAYTEST("LZ4_decompress_safe() with empty input"); - LZ4_decompress_safe(compressedBuffer, decodedBuffer, 0, blockSize); + { int const r = LZ4_decompress_fast(cBuffer_exact, decodedBuffer, blockSize+1); + FUZ_CHECKTEST(r>=0, "LZ4_decompress_fast should have failed, due to Output Size being too large"); + } - /* Test decoding with a one byte input */ - FUZ_DISPLAYTEST("LZ4_decompress_safe() with one byte input"); - { char const tmp = (char)0xFF; - LZ4_decompress_safe(&tmp, decodedBuffer, 1, blockSize); - } + /* Test decoding with output size exactly what's necessary => must work */ + FUZ_DISPLAYTEST(); + decodedBuffer[blockSize] = 0; + { int const r = LZ4_decompress_safe(cBuffer_exact, decodedBuffer, compressedSize, blockSize); + FUZ_CHECKTEST(r<0, "LZ4_decompress_safe failed despite sufficient space"); + FUZ_CHECKTEST(r!=blockSize, "LZ4_decompress_safe did not regenerate original data"); + } + FUZ_CHECKTEST(decodedBuffer[blockSize], "LZ4_decompress_safe overrun specified output buffer size"); + { U32 const crcCheck = XXH32(decodedBuffer, blockSize, 0); + FUZ_CHECKTEST(crcCheck!=crcOrig, "LZ4_decompress_safe corrupted decoded data"); + } - /* Test decoding shortcut edge case */ - FUZ_DISPLAYTEST("LZ4_decompress_safe() with shortcut edge case"); - { char tmp[17]; - /* 14 bytes of literals, followed by a 14 byte match. - * Should not read beyond the end of the buffer. - * See https://github.com/lz4/lz4/issues/508. */ - *tmp = (char)0xEE; - memset(tmp + 1, 0, 14); - tmp[15] = 14; - tmp[16] = 0; - ret = LZ4_decompress_safe(tmp, decodedBuffer, sizeof(tmp), blockSize); - FUZ_CHECKTEST(ret >= 0, "LZ4_decompress_safe() should fail"); - } + /* Test decoding with more than enough output size => must work */ + FUZ_DISPLAYTEST(); + decodedBuffer[blockSize] = 0; + decodedBuffer[blockSize+1] = 0; + { int const r = LZ4_decompress_safe(cBuffer_exact, decodedBuffer, compressedSize, blockSize+1); + FUZ_CHECKTEST(r<0, "LZ4_decompress_safe failed despite amply sufficient space"); + FUZ_CHECKTEST(r!=blockSize, "LZ4_decompress_safe did not regenerate original data"); + } + FUZ_CHECKTEST(decodedBuffer[blockSize+1], "LZ4_decompress_safe overrun specified output buffer size"); + { U32 const crcCheck = XXH32(decodedBuffer, (size_t)blockSize, 0); + FUZ_CHECKTEST(crcCheck!=crcOrig, "LZ4_decompress_safe corrupted decoded data"); + } + /* Test decoding with output size being one byte too short => must fail */ + FUZ_DISPLAYTEST(); + decodedBuffer[blockSize-1] = 0; + { int const r = LZ4_decompress_safe(cBuffer_exact, decodedBuffer, compressedSize, blockSize-1); + FUZ_CHECKTEST(r>=0, "LZ4_decompress_safe should have failed, due to Output Size being one byte too short"); + } + FUZ_CHECKTEST(decodedBuffer[blockSize-1], "LZ4_decompress_safe overrun specified output buffer size"); - /* Test decoding with output size exactly what's necessary => must work */ - FUZ_DISPLAYTEST(); - decodedBuffer[blockSize] = 0; - ret = LZ4_decompress_safe(compressedBuffer, decodedBuffer, compressedSize, blockSize); - FUZ_CHECKTEST(ret<0, "LZ4_decompress_safe failed despite sufficient space"); - FUZ_CHECKTEST(ret!=blockSize, "LZ4_decompress_safe did not regenerate original data"); - FUZ_CHECKTEST(decodedBuffer[blockSize], "LZ4_decompress_safe overrun specified output buffer size"); - { U32 const crcCheck = XXH32(decodedBuffer, blockSize, 0); - FUZ_CHECKTEST(crcCheck!=crcOrig, "LZ4_decompress_safe corrupted decoded data"); - } + /* Test decoding with output size being 10 bytes too short => must fail */ + FUZ_DISPLAYTEST(); + if (blockSize>10) { + decodedBuffer[blockSize-10] = 0; + { int const r = LZ4_decompress_safe(cBuffer_exact, decodedBuffer, compressedSize, blockSize-10); + FUZ_CHECKTEST(r>=0, "LZ4_decompress_safe should have failed, due to Output Size being 10 bytes too short"); + } + FUZ_CHECKTEST(decodedBuffer[blockSize-10], "LZ4_decompress_safe overrun specified output buffer size"); + } - // Test decoding with more than enough output size => must work - FUZ_DISPLAYTEST(); - decodedBuffer[blockSize] = 0; - decodedBuffer[blockSize+1] = 0; - ret = LZ4_decompress_safe(compressedBuffer, decodedBuffer, compressedSize, blockSize+1); - FUZ_CHECKTEST(ret<0, "LZ4_decompress_safe failed despite amply sufficient space"); - FUZ_CHECKTEST(ret!=blockSize, "LZ4_decompress_safe did not regenerate original data"); - FUZ_CHECKTEST(decodedBuffer[blockSize+1], "LZ4_decompress_safe overrun specified output buffer size"); - { U32 const crcCheck = XXH32(decodedBuffer, (size_t)blockSize, 0); - FUZ_CHECKTEST(crcCheck!=crcOrig, "LZ4_decompress_safe corrupted decoded data"); + free(cBuffer_exact); } - // Test decoding with output size being one byte too short => must fail + /* Test decoding with input size being one byte too short => must fail */ FUZ_DISPLAYTEST(); - decodedBuffer[blockSize-1] = 0; - ret = LZ4_decompress_safe(compressedBuffer, decodedBuffer, compressedSize, blockSize-1); - FUZ_CHECKTEST(ret>=0, "LZ4_decompress_safe should have failed, due to Output Size being one byte too short"); - FUZ_CHECKTEST(decodedBuffer[blockSize-1], "LZ4_decompress_safe overrun specified output buffer size"); - - // Test decoding with output size being 10 bytes too short => must fail - FUZ_DISPLAYTEST(); - if (blockSize>10) { - decodedBuffer[blockSize-10] = 0; - ret = LZ4_decompress_safe(compressedBuffer, decodedBuffer, compressedSize, blockSize-10); - FUZ_CHECKTEST(ret>=0, "LZ4_decompress_safe should have failed, due to Output Size being 10 bytes too short"); - FUZ_CHECKTEST(decodedBuffer[blockSize-10], "LZ4_decompress_safe overrun specified output buffer size"); + { int const r = LZ4_decompress_safe(compressedBuffer, decodedBuffer, compressedSize-1, blockSize); + FUZ_CHECKTEST(r>=0, "LZ4_decompress_safe should have failed, due to input size being one byte too short (blockSize=%i, ret=%i, compressedSize=%i)", blockSize, ret, compressedSize); } - // Test decoding with input size being one byte too short => must fail - FUZ_DISPLAYTEST(); - ret = LZ4_decompress_safe(compressedBuffer, decodedBuffer, compressedSize-1, blockSize); - FUZ_CHECKTEST(ret>=0, "LZ4_decompress_safe should have failed, due to input size being one byte too short (blockSize=%i, ret=%i, compressedSize=%i)", blockSize, ret, compressedSize); - - // Test decoding with input size being one byte too large => must fail + /* Test decoding with input size being one byte too large => must fail */ FUZ_DISPLAYTEST(); decodedBuffer[blockSize] = 0; - ret = LZ4_decompress_safe(compressedBuffer, decodedBuffer, compressedSize+1, blockSize); - FUZ_CHECKTEST(ret>=0, "LZ4_decompress_safe should have failed, due to input size being too large"); + { int const r = LZ4_decompress_safe(compressedBuffer, decodedBuffer, compressedSize+1, blockSize); + FUZ_CHECKTEST(r>=0, "LZ4_decompress_safe should have failed, due to input size being too large"); + } FUZ_CHECKTEST(decodedBuffer[blockSize], "LZ4_decompress_safe overrun specified output buffer size"); /* Test partial decoding => must work */ @@ -881,7 +865,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c /* Compress HC using external dictionary stream */ FUZ_DISPLAYTEST(); - { LZ4_streamHC_t* LZ4_streamHC = LZ4_createStreamHC(); + { LZ4_streamHC_t* const LZ4_streamHC = LZ4_createStreamHC(); LZ4_loadDictHC(LZ4dictHC, dict, dictSize); LZ4_attach_HC_dictionary(LZ4_streamHC, LZ4dictHC); @@ -1005,6 +989,30 @@ static void FUZ_unitTests(int compressionLevel) /* 32-bits address space overflow test */ FUZ_AddressOverflow(); + /* Test decoding with empty input */ + DISPLAYLEVEL(3, "LZ4_decompress_safe() with empty input"); + LZ4_decompress_safe(testCompressed, testVerify, 0, testInputSize); + + /* Test decoding with a one byte input */ + DISPLAYLEVEL(3, "LZ4_decompress_safe() with one byte input"); + { char const tmp = (char)0xFF; + LZ4_decompress_safe(&tmp, testVerify, 1, testInputSize); + } + + /* Test decoding shortcut edge case */ + DISPLAYLEVEL(3, "LZ4_decompress_safe() with shortcut edge case"); + { char tmp[17]; + /* 14 bytes of literals, followed by a 14 byte match. + * Should not read beyond the end of the buffer. + * See https://github.com/lz4/lz4/issues/508. */ + *tmp = (char)0xEE; + memset(tmp + 1, 0, 14); + tmp[15] = 14; + tmp[16] = 0; + { int const r = LZ4_decompress_safe(tmp, testVerify, sizeof(tmp), testInputSize); + FUZ_CHECKTEST(r >= 0, "LZ4_decompress_safe() should fail"); + } } + /* LZ4 streaming tests */ { LZ4_stream_t streamingState; U64 crcOrig; -- cgit v1.2.3 From af35920c81e388f7d5f8be5560d791a9f265a4f5 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 19 Apr 2019 10:23:50 -0700 Subject: lz4frame: initializers compatibility with C++ fix #679, reported by @degski --- Makefile | 2 +- lib/lz4frame.c | 8 +++++--- lib/lz4frame.h | 4 ++-- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index f3844a1..4edb47a 100644 --- a/Makefile +++ b/Makefile @@ -182,7 +182,7 @@ gpptest gpptest32: clean CC=$(CC) $(MAKE) -C $(TESTDIR) all CFLAGS="$(CFLAGS)" cxx17build : CC = "$(CXX) -Wno-deprecated" -cxx17build : CFLAGS = -std=c++17 -Wall -Wextra -Wundef -Wshadow -Wcast-align -Werror +cxx17build : CFLAGS = -std=c++17 -Wall -Wextra -Wundef -Wshadow -Wcast-align -Werror -pedantic cxx17build : clean $(CXX) -v CC=$(CC) $(MAKE) -C $(LZ4DIR) all CFLAGS="$(CFLAGS)" diff --git a/lib/lz4frame.c b/lib/lz4frame.c index 42124e9..a10e4af 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -325,8 +325,7 @@ static size_t LZ4F_compressBound_internal(size_t srcSize, const LZ4F_preferences_t* preferencesPtr, size_t alreadyBuffered) { - LZ4F_preferences_t prefsNull; - MEM_INIT(&prefsNull, 0, sizeof(prefsNull)); + LZ4F_preferences_t prefsNull = LZ4F_INIT_PREFERENCES; prefsNull.frameInfo.contentChecksumFlag = LZ4F_contentChecksumEnabled; /* worst case */ { const LZ4F_preferences_t* const prefsPtr = (preferencesPtr==NULL) ? &prefsNull : preferencesPtr; U32 const flush = prefsPtr->autoFlush | (srcSize==0); @@ -1065,7 +1064,10 @@ struct LZ4F_dctx_s { LZ4F_errorCode_t LZ4F_createDecompressionContext(LZ4F_dctx** LZ4F_decompressionContextPtr, unsigned versionNumber) { LZ4F_dctx* const dctx = (LZ4F_dctx*)ALLOC_AND_ZERO(sizeof(LZ4F_dctx)); - if (dctx==NULL) return err0r(LZ4F_ERROR_GENERIC); + if (dctx == NULL) { /* failed allocation */ + *LZ4F_decompressionContextPtr = NULL; + return err0r(LZ4F_ERROR_allocation_failed); + } dctx->version = versionNumber; *LZ4F_decompressionContextPtr = dctx; diff --git a/lib/lz4frame.h b/lib/lz4frame.h index ca20dc9..742c252 100644 --- a/lib/lz4frame.h +++ b/lib/lz4frame.h @@ -176,7 +176,7 @@ typedef struct { LZ4F_blockChecksum_t blockChecksumFlag; /* 1: each block followed by a checksum of block's compressed data; 0: disabled (default) */ } LZ4F_frameInfo_t; -#define LZ4F_INIT_FRAMEINFO { 0, 0, 0, 0, 0, 0, 0 } /* v1.8.3+ */ +#define LZ4F_INIT_FRAMEINFO { LZ4F_default, LZ4F_blockLinked, LZ4F_noContentChecksum, LZ4F_frame, 0ULL, 0U, LZ4F_noBlockChecksum } /* v1.8.3+ */ /*! LZ4F_preferences_t : * makes it possible to supply advanced compression instructions to streaming interface. @@ -191,7 +191,7 @@ typedef struct { unsigned reserved[3]; /* must be zero for forward compatibility */ } LZ4F_preferences_t; -#define LZ4F_INIT_PREFERENCES { LZ4F_INIT_FRAMEINFO, 0, 0, 0, { 0, 0, 0 } } /* v1.8.3+ */ +#define LZ4F_INIT_PREFERENCES { LZ4F_INIT_FRAMEINFO, 0, 0u, 0u, { 0u, 0u, 0u } } /* v1.8.3+ */ /*-********************************* -- cgit v1.2.3 From 7a4e3b1fac5cd9d4ec7c8d0091329ba107ec2131 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 19 Apr 2019 11:54:01 -0700 Subject: bumped version number to v1.9.1 --- NEWS | 5 +++++ doc/lz4_manual.html | 4 ++-- doc/lz4frame_manual.html | 4 ++-- lib/lz4.h | 2 +- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/NEWS b/NEWS index 2d85607..7722d73 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,8 @@ +v1.9.1 +fix : decompression functions were reading beyond input size (introduced in v1.9.0, reported by @ppodolsky and @danlark1) +api : fix : lz4frame initializers compatibility with c++, reported by @degski +build: AIX, by Norman Green + v1.9.0 perf: large decompression speed improvement on x86/x64 (up to +20%) by @djwatson api : changed : _destSize() compression variants are promoted to stable API diff --git a/doc/lz4_manual.html b/doc/lz4_manual.html index ee43b8a..3a9e0db 100644 --- a/doc/lz4_manual.html +++ b/doc/lz4_manual.html @@ -1,10 +1,10 @@ -1.9.0 Manual +1.9.1 Manual -

    1.9.0 Manual

    +

    1.9.1 Manual


    Contents

      diff --git a/doc/lz4frame_manual.html b/doc/lz4frame_manual.html index 914405f..2cf4f03 100644 --- a/doc/lz4frame_manual.html +++ b/doc/lz4frame_manual.html @@ -1,10 +1,10 @@ -1.9.0 Manual +1.9.1 Manual -

      1.9.0 Manual

      +

      1.9.1 Manual


      Contents

        diff --git a/lib/lz4.h b/lib/lz4.h index 737a0c7..a9c932c 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -97,7 +97,7 @@ extern "C" { /*------ Version ------*/ #define LZ4_VERSION_MAJOR 1 /* for breaking interface changes */ #define LZ4_VERSION_MINOR 9 /* for new (non-breaking) interface capabilities */ -#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) -- cgit v1.2.3 From 5f8ac02b7707c03f93ff10ac1362d2c618bd3c42 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 19 Apr 2019 17:08:40 -0700 Subject: cli: display a warning whenever default output is stdout while input != stdin This behavior has been preserved for compatibility with existing ecosystem. But it's problematic, as some environment start `lz4` without identifying stdout as console by default, leading to a change of behavior for a same line of script. A more sensible policy would be to default to stdout only when input is stdin. Soft change for the time being : keep the behavior, just print a warning message. User should prefer `-c` to explicitly select `stdout`. Also : updated tests in Makefile to explicitly select `stdout` with `-c`. --- programs/lz4cli.c | 36 +++++++++++++++++++++++++----------- tests/Makefile | 26 +++++++++++++------------- 2 files changed, 38 insertions(+), 24 deletions(-) diff --git a/programs/lz4cli.c b/programs/lz4cli.c index 8bd7042..279aaf1 100644 --- a/programs/lz4cli.c +++ b/programs/lz4cli.c @@ -652,7 +652,15 @@ int main(int argc, const char** argv) /* No output filename ==> try to select one automatically (when possible) */ while ((!output_filename) && (multiple_inputs==0)) { - if (!IS_CONSOLE(stdout)) { output_filename=stdoutmark; break; } /* Default to stdout whenever possible (i.e. not a console) */ + + if (!IS_CONSOLE(stdout)) { + /* Default to stdout whenever stdout is not the console. + * Note : this policy is likely to change in the future, don't rely on it ! + * prefer using an explicit `-c` flag */ + DISPLAYLEVEL(1, "Warning : using stdout as default output. Do not rely on this behavior: use explicit `-c` instead ! \n"); + output_filename=stdoutmark; + break; + } if (mode == om_auto) { /* auto-determine compression or decompression, based on file extension */ mode = determineOpMode(input_filename); } @@ -682,10 +690,14 @@ int main(int argc, const char** argv) break; } - /* Check if output is defined as console; trigger an error in this case */ + if (multiple_inputs==0) assert(output_filename); + /* when multiple_inputs==1, output_filename may simply be useless, + * however, output_filename must be !NULL for next strcmp() tests */ if (!output_filename) output_filename = "*\\dummy^!//"; + + /* Check if output is defined as console; trigger an error in this case */ if (!strcmp(output_filename,stdoutmark) && IS_CONSOLE(stdout) && !forceStdout) { - DISPLAYLEVEL(1, "refusing to write to console without -c\n"); + DISPLAYLEVEL(1, "refusing to write to console without -c \n"); exit(1); } /* Downgrade notification level in stdout and multiple file mode */ @@ -701,21 +713,23 @@ int main(int argc, const char** argv) LZ4IO_setNotificationLevel((int)displayLevel); if (ifnIdx == 0) multiple_inputs = 0; if (mode == om_decompress) { - if (multiple_inputs) - operationResult = LZ4IO_decompressMultipleFilenames(prefs, inFileNames, ifnIdx, !strcmp(output_filename,stdoutmark) ? stdoutmark : LZ4_EXTENSION); - else + if (multiple_inputs) { + assert(ifnIdx <= INT_MAX); + operationResult = LZ4IO_decompressMultipleFilenames(prefs, inFileNames, (int)ifnIdx, !strcmp(output_filename,stdoutmark) ? stdoutmark : LZ4_EXTENSION); + } else { operationResult = DEFAULT_DECOMPRESSOR(prefs, input_filename, output_filename); + } } else { /* compression is default action */ if (legacy_format) { DISPLAYLEVEL(3, "! Generating LZ4 Legacy format (deprecated) ! \n"); LZ4IO_compressFilename_Legacy(prefs, input_filename, output_filename, cLevel); } else { - if (multiple_inputs) - operationResult = LZ4IO_compressMultipleFilenames(prefs, inFileNames, ifnIdx, !strcmp(output_filename,stdoutmark) ? stdoutmark : LZ4_EXTENSION, cLevel); - else + if (multiple_inputs) { + assert(ifnIdx <= INT_MAX); + operationResult = LZ4IO_compressMultipleFilenames(prefs, inFileNames, (int)ifnIdx, !strcmp(output_filename,stdoutmark) ? stdoutmark : LZ4_EXTENSION, cLevel); + } else { operationResult = DEFAULT_COMPRESSOR(prefs, input_filename, output_filename, cLevel); - } - } + } } } _cleanup: if (main_pause) waitEnter(); diff --git a/tests/Makefile b/tests/Makefile index 70cae63..579999c 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -176,15 +176,15 @@ test-install: lz4 lib liblz4.pc test-lz4-sparse: lz4 datagen @echo "\n ---- test sparse file support ----" ./datagen -g5M -P100 > tmplsdg5M - $(LZ4) -B4D tmplsdg5M | $(LZ4) -dv --sparse > tmplscB4 + $(LZ4) -B4D tmplsdg5M -c | $(LZ4) -dv --sparse > tmplscB4 $(DIFF) -s tmplsdg5M tmplscB4 - $(LZ4) -B5D tmplsdg5M | $(LZ4) -dv --sparse > tmplscB5 + $(LZ4) -B5D tmplsdg5M -c | $(LZ4) -dv --sparse > tmplscB5 $(DIFF) -s tmplsdg5M tmplscB5 - $(LZ4) -B6D tmplsdg5M | $(LZ4) -dv --sparse > tmplscB6 + $(LZ4) -B6D tmplsdg5M -c | $(LZ4) -dv --sparse > tmplscB6 $(DIFF) -s tmplsdg5M tmplscB6 - $(LZ4) -B7D tmplsdg5M | $(LZ4) -dv --sparse > tmplscB7 + $(LZ4) -B7D tmplsdg5M -c | $(LZ4) -dv --sparse > tmplscB7 $(DIFF) -s tmplsdg5M tmplscB7 - $(LZ4) tmplsdg5M | $(LZ4) -dv --no-sparse > tmplsnosparse + $(LZ4) tmplsdg5M -c | $(LZ4) -dv --no-sparse > tmplsnosparse $(DIFF) -s tmplsdg5M tmplsnosparse ls -ls tmpls* ./datagen -s1 -g1200007 -P100 | $(LZ4) | $(LZ4) -dv --sparse > tmplsodd # Odd size file (to generate non-full last block) @@ -200,7 +200,7 @@ test-lz4-sparse: lz4 datagen cat tmplsdg1M tmplsdg1M > tmpls2M $(LZ4) -B5 -v tmplsdg1M tmplsc $(LZ4) -d -v tmplsc tmplsr - $(LZ4) -d -v tmplsc >> tmplsr + $(LZ4) -d -v tmplsc -c >> tmplsr ls -ls tmp* $(DIFF) tmpls2M tmplsr @$(RM) tmpls* @@ -208,8 +208,8 @@ test-lz4-sparse: lz4 datagen test-lz4-contentSize: lz4 datagen @echo "\n ---- test original size support ----" ./datagen -g15M > tmplc1 - $(LZ4) -v tmplc1 | $(LZ4) -t - $(LZ4) -v --content-size tmplc1 | $(LZ4) -d > tmplc2 + $(LZ4) -v tmplc1 -c | $(LZ4) -t + $(LZ4) -v --content-size tmplc1 -c | $(LZ4) -d > tmplc2 $(DIFF) -s tmplc1 tmplc2 @$(RM) tmplc* @@ -218,10 +218,10 @@ test-lz4-frame-concatenation: lz4 datagen @echo -n > tmp-lfc-empty @echo hi > tmp-lfc-nonempty cat tmp-lfc-nonempty tmp-lfc-empty tmp-lfc-nonempty > tmp-lfc-src - @$(LZ4) -zq tmp-lfc-empty > tmp-lfc-empty.lz4 - @$(LZ4) -zq tmp-lfc-nonempty > tmp-lfc-nonempty.lz4 + $(LZ4) -zq tmp-lfc-empty -c > tmp-lfc-empty.lz4 + $(LZ4) -zq tmp-lfc-nonempty -c > tmp-lfc-nonempty.lz4 cat tmp-lfc-nonempty.lz4 tmp-lfc-empty.lz4 tmp-lfc-nonempty.lz4 > tmp-lfc-concat.lz4 - $(LZ4) -d tmp-lfc-concat.lz4 > tmp-lfc-result + $(LZ4) -d tmp-lfc-concat.lz4 -c > tmp-lfc-result cmp tmp-lfc-src tmp-lfc-result @$(RM) tmp-lfc-* @echo frame concatenation test completed @@ -341,8 +341,8 @@ test-lz4-dict: lz4 datagen for l in 0 1 4 128 32767 32768 32769 65535 65536 65537 98303 98304 98305 131071 131072 131073; do \ ./datagen -g$$l > tmp-dict-$$l; \ $(DD) if=tmp-dict-$$l of=tmp-dict-$$l-tail bs=1 count=65536 skip=$$((l > 65536 ? l - 65536 : 0)); \ - < tmp-dict-$$l $(LZ4) -D stdin tmp-dict-data-128KB | $(LZ4) -dD tmp-dict-$$l-tail | $(DIFF) - tmp-dict-data-128KB; \ - < tmp-dict-$$l-tail $(LZ4) -D stdin tmp-dict-data-128KB | $(LZ4) -dD tmp-dict-$$l | $(DIFF) - tmp-dict-data-128KB; \ + < tmp-dict-$$l $(LZ4) -D stdin tmp-dict-data-128KB -c | $(LZ4) -dD tmp-dict-$$l-tail | $(DIFF) - tmp-dict-data-128KB; \ + < tmp-dict-$$l-tail $(LZ4) -D stdin tmp-dict-data-128KB -c | $(LZ4) -dD tmp-dict-$$l | $(DIFF) - tmp-dict-data-128KB; \ done @$(RM) tmp-dict* -- cgit v1.2.3 From 55ab3c48aef50fbf56203eb15fd37d84be5cbd46 Mon Sep 17 00:00:00 2001 From: gabrielstedman Date: Sat, 20 Apr 2019 22:06:10 +0100 Subject: Add --list option to display compressed file information. --- programs/lz4cli.c | 34 ++++++++++++++++++++++++++++++++-- programs/lz4io.c | 40 +++++++++++++++++++++++++++++++++++++++- programs/lz4io.h | 11 +++++++++++ 3 files changed, 82 insertions(+), 3 deletions(-) diff --git a/programs/lz4cli.c b/programs/lz4cli.c index 8bd7042..adfabc3 100644 --- a/programs/lz4cli.c +++ b/programs/lz4cli.c @@ -40,6 +40,7 @@ #include /* exit, calloc, free */ #include /* strcmp, strlen */ #include "bench.h" /* BMK_benchFile, BMK_SetNbIterations, BMK_SetBlocksize, BMK_SetPause */ +#include "lz4frame.h" #include "lz4io.h" /* LZ4IO_compressFilename, LZ4IO_decompressFilename, LZ4IO_compressMultipleFilenames */ #include "lz4hc.h" /* LZ4HC_CLEVEL_MAX */ #include "lz4.h" /* LZ4_VERSION_STRING */ @@ -141,6 +142,7 @@ static int usage_advanced(const char* exeName) DISPLAY( " -BX : enable block checksum (default:disabled) \n"); DISPLAY( "--no-frame-crc : disable stream checksum (default:enabled) \n"); DISPLAY( "--content-size : compressed frame includes original size (default:not present)\n"); + DISPLAY( "--list : list information about .lz4 files. Only useful if compressed with --content-size flag.\n"); DISPLAY( "--[no-]sparse : sparse mode (default:enabled on file, disabled on stdout)\n"); DISPLAY( "--favor-decSpeed: compressed files decompress faster, but are less compressed \n"); DISPLAY( "--fast[=#]: switch to ultra fast compression level (default: %i)\n", 1); @@ -286,7 +288,7 @@ static int longCommandWArg(const char** stringPtr, const char* longCommand) return result; } -typedef enum { om_auto, om_compress, om_decompress, om_test, om_bench } operationMode_e; +typedef enum { om_auto, om_compress, om_decompress, om_test, om_bench, om_list } operationMode_e; /** determineOpMode() : * auto-determine operation mode, based on input filename extension @@ -383,6 +385,7 @@ int main(int argc, const char** argv) if (!strcmp(argument, "--no-frame-crc")) { LZ4IO_setStreamChecksumMode(prefs, 0); continue; } if (!strcmp(argument, "--content-size")) { LZ4IO_setContentSize(prefs, 1); continue; } if (!strcmp(argument, "--no-content-size")) { LZ4IO_setContentSize(prefs, 0); continue; } + if (!strcmp(argument, "--list")) { mode = om_list; continue; } if (!strcmp(argument, "--sparse")) { LZ4IO_setSparseFile(prefs, 2); continue; } if (!strcmp(argument, "--no-sparse")) { LZ4IO_setSparseFile(prefs, 0); continue; } if (!strcmp(argument, "--favor-decSpeed")) { LZ4IO_favorDecSpeed(prefs, 1); continue; } @@ -705,7 +708,34 @@ int main(int argc, const char** argv) operationResult = LZ4IO_decompressMultipleFilenames(prefs, inFileNames, ifnIdx, !strcmp(output_filename,stdoutmark) ? stdoutmark : LZ4_EXTENSION); else operationResult = DEFAULT_DECOMPRESSOR(prefs, input_filename, output_filename); - } else { /* compression is default action */ + } else if (mode == om_list){ + LZ4F_compFileInfo_t cfinfo; + if(!multiple_inputs){ + inFileNames[ifnIdx++] = input_filename; + } + DISPLAY("%16s\t%-20s\t%-20s\t%-10s\t%s\n","BlockChecksumFlag","Compressed", "Uncompressed", "Ratio", "Filename"); + for(i=0; i /* clock */ #include /* stat64 */ #include /* stat64 */ -#include "lz4io.h" #include "lz4.h" /* still required for legacy format */ #include "lz4hc.h" /* still required for legacy format */ #define LZ4F_STATIC_LINKING_ONLY #include "lz4frame.h" +#include "lz4io.h" /***************************** @@ -1265,3 +1265,41 @@ int LZ4IO_decompressMultipleFilenames(LZ4IO_prefs_t* const prefs, const char** i free(outFileName); return missingFiles + skippedFiles; } + +size_t LZ4IO_getCompressedFileInfo(const char* input_filename, LZ4F_compFileInfo_t* cfinfo){ + /* Get file size */ + stat_t statbuf; + if (!UTIL_getFileStat(input_filename, &statbuf)){ + EXM_THROW(60, "Can't stat file : %s", input_filename); + } + cfinfo->fileSize = statbuf.st_size; + /* Get basename without extension */ + const char *b = strrchr(input_filename, '/'); + if (b && b != input_filename){ + b++; + } else{ + b=input_filename; + } + const char *e = strrchr(b, '.'); + cfinfo->fileName = malloc( (e-b+1) * sizeof(char)); + strncpy(cfinfo->fileName, b, (e-b)); + cfinfo->fileName[e-b] = '\0'; + /* init */ + dRess_t ress; + LZ4F_errorCode_t const errorCode = LZ4F_createDecompressionContext(&ress.dCtx, LZ4F_VERSION); + if (LZ4F_isError(errorCode)) EXM_THROW(60, "Can't create LZ4F context : %s", LZ4F_getErrorName(errorCode)); + FILE* const finput = LZ4IO_openSrcFile(input_filename); + if (finput==NULL) return 1; + /* Allocate Memory */ + ress.srcBuffer = malloc(LZ4IO_dBufferSize); + size_t readSize = LZ4F_HEADER_SIZE_MAX; + if (!fread(ress.srcBuffer, readSize, 1, finput)){ + EXM_THROW(30, "Error reading %s ", input_filename); + } + cfinfo->frameInfo = (LZ4F_frameInfo_t) LZ4F_INIT_FRAMEINFO; + LZ4F_getFrameInfo(ress.dCtx, &cfinfo->frameInfo, ress.srcBuffer, &readSize); + /* Close input/free resources */ + fclose(finput); + free(ress.srcBuffer); + return readSize; +} diff --git a/programs/lz4io.h b/programs/lz4io.h index 54d49be..bf1fa76 100644 --- a/programs/lz4io.h +++ b/programs/lz4io.h @@ -54,6 +54,14 @@ static const char nulmark[] = "/dev/null"; typedef struct LZ4IO_prefs_s LZ4IO_prefs_t; +typedef struct { + LZ4F_frameInfo_t frameInfo; + char* fileName; + unsigned long long fileSize; + double compressionRatio; +} LZ4F_compFileInfo_t; + + LZ4IO_prefs_t* LZ4IO_defaultPreferences(void); void LZ4IO_freePreferences(LZ4IO_prefs_t* const prefs); @@ -116,6 +124,9 @@ int LZ4IO_setSparseFile(LZ4IO_prefs_t* const prefs, int enable); /* Default setting : 0 == no content size present in frame header */ int LZ4IO_setContentSize(LZ4IO_prefs_t* const prefs, int enable); +/* */ +size_t LZ4IO_getCompressedFileInfo(const char* input_filename, LZ4F_compFileInfo_t* cfinfo); + /* Default setting : 0 == src file preserved */ void LZ4IO_setRemoveSrcFile(LZ4IO_prefs_t* const prefs, unsigned flag); -- cgit v1.2.3 From 55484191c40a2909bf08eb23754a136199c134c2 Mon Sep 17 00:00:00 2001 From: gabrielstedman Date: Sun, 21 Apr 2019 00:20:38 +0100 Subject: correctly use unisgned int for index --- programs/lz4cli.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/programs/lz4cli.c b/programs/lz4cli.c index adfabc3..54d93f1 100644 --- a/programs/lz4cli.c +++ b/programs/lz4cli.c @@ -714,9 +714,9 @@ int main(int argc, const char** argv) inFileNames[ifnIdx++] = input_filename; } DISPLAY("%16s\t%-20s\t%-20s\t%-10s\t%s\n","BlockChecksumFlag","Compressed", "Uncompressed", "Ratio", "Filename"); - for(i=0; i Date: Sun, 21 Apr 2019 17:43:57 +0100 Subject: FR #598 Improve initial design, test mallocs, support C90. --- programs/lz4.1.md | 6 +++ programs/lz4cli.c | 26 ++----------- programs/lz4io.c | 112 ++++++++++++++++++++++++++++++++++++++---------------- programs/lz4io.h | 10 +++-- 4 files changed, 94 insertions(+), 60 deletions(-) diff --git a/programs/lz4.1.md b/programs/lz4.1.md index 10449a0..7db9f19 100644 --- a/programs/lz4.1.md +++ b/programs/lz4.1.md @@ -114,6 +114,11 @@ only the latest one will be applied. * `-b#`: Benchmark mode, using `#` compression level. +* `--list`: + List mode. + Lists information about .lz4 files. + Useful if compressed with --content-size flag. + ### Operation modifiers * `-#`: @@ -161,6 +166,7 @@ only the latest one will be applied. Multiple input files. Compressed file names will be appended a `.lz4` suffix. This mode also reduces notification level. + Can also be used to list multiple files. `lz4 -m` has a behavior equivalent to `gzip -k` (it preserves source files by default). diff --git a/programs/lz4cli.c b/programs/lz4cli.c index 54d93f1..23c4be7 100644 --- a/programs/lz4cli.c +++ b/programs/lz4cli.c @@ -142,7 +142,7 @@ static int usage_advanced(const char* exeName) DISPLAY( " -BX : enable block checksum (default:disabled) \n"); DISPLAY( "--no-frame-crc : disable stream checksum (default:enabled) \n"); DISPLAY( "--content-size : compressed frame includes original size (default:not present)\n"); - DISPLAY( "--list : list information about .lz4 files. Only useful if compressed with --content-size flag.\n"); + DISPLAY( "--list : lists information about .lz4 files. Useful if compressed with --content-size flag.\n"); DISPLAY( "--[no-]sparse : sparse mode (default:enabled on file, disabled on stdout)\n"); DISPLAY( "--favor-decSpeed: compressed files decompress faster, but are less compressed \n"); DISPLAY( "--fast[=#]: switch to ultra fast compression level (default: %i)\n", 1); @@ -709,31 +709,11 @@ int main(int argc, const char** argv) else operationResult = DEFAULT_DECOMPRESSOR(prefs, input_filename, output_filename); } else if (mode == om_list){ - LZ4F_compFileInfo_t cfinfo; if(!multiple_inputs){ inFileNames[ifnIdx++] = input_filename; } - DISPLAY("%16s\t%-20s\t%-20s\t%-10s\t%s\n","BlockChecksumFlag","Compressed", "Uncompressed", "Ratio", "Filename"); - for(unsigned int j=0; jfileSize = statbuf.st_size; - /* Get basename without extension */ - const char *b = strrchr(input_filename, '/'); - if (b && b != input_filename){ - b++; - } else{ - b=input_filename; + if(cfinfo.frameInfo.contentSize){ + DISPLAY("%-16d\t%-20llu\t%-20llu\t%-8.4f\t%s\n",cfinfo.frameInfo.blockChecksumFlag,cfinfo.fileSize,cfinfo.frameInfo.contentSize, cfinfo.ratio, cfinfo.fileName); } - const char *e = strrchr(b, '.'); - cfinfo->fileName = malloc( (e-b+1) * sizeof(char)); - strncpy(cfinfo->fileName, b, (e-b)); - cfinfo->fileName[e-b] = '\0'; - /* init */ - dRess_t ress; - LZ4F_errorCode_t const errorCode = LZ4F_createDecompressionContext(&ress.dCtx, LZ4F_VERSION); - if (LZ4F_isError(errorCode)) EXM_THROW(60, "Can't create LZ4F context : %s", LZ4F_getErrorName(errorCode)); - FILE* const finput = LZ4IO_openSrcFile(input_filename); - if (finput==NULL) return 1; - /* Allocate Memory */ - ress.srcBuffer = malloc(LZ4IO_dBufferSize); - size_t readSize = LZ4F_HEADER_SIZE_MAX; - if (!fread(ress.srcBuffer, readSize, 1, finput)){ - EXM_THROW(30, "Error reading %s ", input_filename); + else{ + DISPLAY("%-16d\t%-20llu\t%-20s\t%-10s\t%s\n",cfinfo.frameInfo.blockChecksumFlag,cfinfo.fileSize, "-", "-", cfinfo.fileName); } - cfinfo->frameInfo = (LZ4F_frameInfo_t) LZ4F_INIT_FRAMEINFO; - LZ4F_getFrameInfo(ress.dCtx, &cfinfo->frameInfo, ress.srcBuffer, &readSize); - /* Close input/free resources */ - fclose(finput); - free(ress.srcBuffer); - return readSize; + } + return op_result; +} + +int LZ4IO_getCompressedFileInfo(const char* input_filename, LZ4F_compFileInfo_t* cfinfo){ + const char *b, + *e; + char *t; + stat_t statbuf; + size_t readSize = LZ4F_HEADER_SIZE_MAX; + LZ4F_errorCode_t errorCode; + dRess_t ress; + /* Open file */ + FILE* const finput = LZ4IO_openSrcFile(input_filename); + if (finput==NULL) return 0; + *cfinfo = (LZ4F_compFileInfo_t) LZ4F_INIT_FILEINFO; + /* Get file size */ + if (!UTIL_getFileStat(input_filename, &statbuf)){ + EXM_THROW(60, "Can't stat file : %s", input_filename); + } + cfinfo->fileSize = statbuf.st_size; + + /* Get basename without extension */ + b = strrchr(input_filename, '/'); + if (!b){ + b = strrchr(input_filename, '\\'); + } + if (b && b != input_filename){ + b++; + } else{ + b=input_filename; + } + e = strrchr(b, '.'); + /* Allocate Memory */ + t = malloc( (e-b+1) * sizeof(char)); + ress.srcBuffer = malloc(LZ4IO_dBufferSize); + if (!t || !ress.srcBuffer) + EXM_THROW(21, "Allocation error : not enough memory"); + strncpy(t, b, (e-b)); + t[e-b] = '\0'; + cfinfo->fileName = t; + + /* init */ + errorCode = LZ4F_createDecompressionContext(&ress.dCtx, LZ4F_VERSION); + if (LZ4F_isError(errorCode)) EXM_THROW(60, "Can't create LZ4F context : %s", LZ4F_getErrorName(errorCode)); + + if (!fread(ress.srcBuffer, readSize, 1, finput)){ + EXM_THROW(30, "Error reading %s ", input_filename); + } + // cfinfo->frameInfo = (LZ4F_frameInfo_t) LZ4F_INIT_FRAMEINFO; + LZ4F_getFrameInfo(ress.dCtx, &cfinfo->frameInfo, ress.srcBuffer, &readSize); + if(cfinfo->frameInfo.contentSize){ + cfinfo->ratio = (double)cfinfo->fileSize / cfinfo->frameInfo.contentSize; + } else { + cfinfo->ratio = -1; + } + + /* Close input/free resources */ + fclose(finput); + free(ress.srcBuffer); + return 0; } diff --git a/programs/lz4io.h b/programs/lz4io.h index bf1fa76..707f233 100644 --- a/programs/lz4io.h +++ b/programs/lz4io.h @@ -56,11 +56,12 @@ typedef struct LZ4IO_prefs_s LZ4IO_prefs_t; typedef struct { LZ4F_frameInfo_t frameInfo; - char* fileName; + const char* fileName; unsigned long long fileSize; - double compressionRatio; + double ratio; } LZ4F_compFileInfo_t; +#define LZ4F_INIT_FILEINFO { (LZ4F_frameInfo_t) LZ4F_INIT_FRAMEINFO, NULL, 0ULL, -1.f } LZ4IO_prefs_t* LZ4IO_defaultPreferences(void); void LZ4IO_freePreferences(LZ4IO_prefs_t* const prefs); @@ -124,8 +125,9 @@ int LZ4IO_setSparseFile(LZ4IO_prefs_t* const prefs, int enable); /* Default setting : 0 == no content size present in frame header */ int LZ4IO_setContentSize(LZ4IO_prefs_t* const prefs, int enable); -/* */ -size_t LZ4IO_getCompressedFileInfo(const char* input_filename, LZ4F_compFileInfo_t* cfinfo); +int LZ4IO_getCompressedFilesInfo(const char** inFileNames,const size_t ifnIdx); + +int LZ4IO_getCompressedFileInfo(const char* input_filename, LZ4F_compFileInfo_t* cfinfo); /* Default setting : 0 == src file preserved */ void LZ4IO_setRemoveSrcFile(LZ4IO_prefs_t* const prefs, unsigned flag); -- cgit v1.2.3 From 467e352de929fa664af03ece065c89e9997d1bf1 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sun, 21 Apr 2019 12:42:13 -0700 Subject: tests/Makefile : created CMP variable for potential redirection, if need be. note : should probably converge all comparison operations onto CMP --- tests/Makefile | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/tests/Makefile b/tests/Makefile index 579999c..209a530 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -144,6 +144,8 @@ ifneq (,$(filter $(shell uname), Darwin )) MD5:=md5 -r endif +# note : we should probably settle on a single compare utility +CMP:=cmp DIFF:=diff ifneq (,$(filter $(shell uname),SunOS)) DIFF:=gdiff @@ -222,7 +224,7 @@ test-lz4-frame-concatenation: lz4 datagen $(LZ4) -zq tmp-lfc-nonempty -c > tmp-lfc-nonempty.lz4 cat tmp-lfc-nonempty.lz4 tmp-lfc-empty.lz4 tmp-lfc-nonempty.lz4 > tmp-lfc-concat.lz4 $(LZ4) -d tmp-lfc-concat.lz4 -c > tmp-lfc-result - cmp tmp-lfc-src tmp-lfc-result + $(CMP) tmp-lfc-src tmp-lfc-result @$(RM) tmp-lfc-* @echo frame concatenation test completed @@ -241,15 +243,15 @@ test-lz4-multiple: lz4 datagen mv tmp-tlm2 tmp-tlm2-orig mv tmp-tlm3 tmp-tlm3-orig $(LZ4) -d -f -m tmp-tlm*.lz4 - cmp tmp-tlm1 tmp-tlm1-orig # must be identical - cmp tmp-tlm2 tmp-tlm2-orig - cmp tmp-tlm3 tmp-tlm3-orig + $(CMP) tmp-tlm1 tmp-tlm1-orig # must be identical + $(CMP) tmp-tlm2 tmp-tlm2-orig + $(CMP) tmp-tlm3 tmp-tlm3-orig # compress multiple files into stdout cat tmp-tlm1.lz4 tmp-tlm2.lz4 tmp-tlm3.lz4 > tmp-tlm-concat1 $(RM) *.lz4 $(LZ4) -m tmp-tlm1 tmp-tlm2 tmp-tlm3 -c > tmp-tlm-concat2 test ! -f tmp-tlm1.lz4 # must not create .lz4 artefact - cmp tmp-tlm-concat1 tmp-tlm-concat2 # must be equivalent + $(CMP) tmp-tlm-concat1 tmp-tlm-concat2 # must be equivalent # decompress multiple files into stdout $(RM) tmp-tlm-concat1 tmp-tlm-concat2 $(LZ4) -f -m tmp-tlm1 tmp-tlm2 tmp-tlm3 # generate .lz4 to decompress @@ -257,7 +259,7 @@ test-lz4-multiple: lz4 datagen $(RM) tmp-tlm1 tmp-tlm2 tmp-tlm3 $(LZ4) -d -m tmp-tlm1.lz4 tmp-tlm2.lz4 tmp-tlm3.lz4 -c > tmp-tlm-concat2 test ! -f tmp-tlm1 # must not create file artefact - cmp tmp-tlm-concat1 tmp-tlm-concat2 # must be equivalent + $(CMP) tmp-tlm-concat1 tmp-tlm-concat2 # must be equivalent # compress multiple files, one of which is absent (must fail) ! $(LZ4) -f -m tmp-tlm-concat1 notHere tmp-tlm-concat2 # must fail : notHere not present @$(RM) tmp-tlm* -- cgit v1.2.3 From 22fae16c6f4c4f118e93468ede884295d365fcef Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sun, 21 Apr 2019 17:01:03 -0700 Subject: ensure tests work when `stdout` is not the console ensure this case is continuously tested on travis. Update documentation on implicit output, invite to not rely on implicit output in scripts. --- .travis.yml | 2 +- programs/lz4.1.md | 27 +++++++++++++-------------- programs/lz4cli.c | 5 +++-- tests/Makefile | 2 +- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/.travis.yml b/.travis.yml index 301d294..2065478 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,7 +8,7 @@ matrix: os: osx compiler: clang script: - - make -C tests test-lz4 MOREFLAGS='-Werror -Wconversion -Wno-sign-conversion' + - make -C tests test-lz4 MOREFLAGS='-Werror -Wconversion -Wno-sign-conversion' | tee # test scenario where `stdout` is not the console - CFLAGS=-m32 make -C tests clean test-lz4-contentSize # Container-based 12.04 LTS Server Edition 64 bit (doesn't support 32-bit includes) diff --git a/programs/lz4.1.md b/programs/lz4.1.md index 10449a0..ffda3ff 100644 --- a/programs/lz4.1.md +++ b/programs/lz4.1.md @@ -31,29 +31,29 @@ The native file format is the `.lz4` format. `lz4` supports a command line syntax similar _but not identical_ to `gzip(1)`. Differences are : - * `lz4` preserves original files * `lz4` compresses a single file by default (see `-m` for multiple files) * `lz4 file1 file2` means : compress file1 _into_ file2 * `lz4 file.lz4` will default to decompression (use `-z` to force compression) + * `lz4` preserves original files * `lz4` shows real-time notification statistics during compression or decompression of a single file (use `-q` to silence them) - * If no destination name is provided, result is sent to `stdout` - _except if stdout is the console_. - * If no destination name is provided, __and__ if `stdout` is the console, - `file` is compressed into `file.lz4`. - * As a consequence of previous rules, note the following example : - `lz4 file | consumer` sends compressed data to `consumer` through `stdout`, - hence it does _not_ create `file.lz4`. - * Another consequence of those rules is that to run `lz4` under `nohup`, - you should provide a destination file: `nohup lz4 file file.lz4`, - because `nohup` writes the specified command's output to a file. + * When no destination is specified, result is sent on implicit output, + which depends on `stdout` status. + When `stdout` _is Not the console_, it becomes the implicit output. + Otherwise, if `stdout` is the console, the implicit output is `filename.lz4`. + * It is considered bad practice to rely on implicit output in scripts. + because the script's environment may change. + Always use explicit output in scripts. + `-c` ensures that output will be `stdout`. + Conversely, providing a destination name, or using `-m` + ensures that the output will be either the specified name, or `filename.lz4` respectively. Default behaviors can be modified by opt-in commands, detailed below. * `lz4 -m` makes it possible to provide multiple input filenames, which will be compressed into files using suffix `.lz4`. - Progress notifications are also disabled by default (use `-v` to enable them). + Progress notifications become disabled by default (use `-v` to enable them). This mode has a behavior which more closely mimics `gzip` command line, with the main remaining difference being that source files are preserved by default. * Similarly, `lz4 -m -d` can decompress multiple `*.lz4` files. @@ -81,8 +81,7 @@ In some cases, some options can be expressed using short command `-x` or long command `--long-word`. Short commands can be concatenated together. For example, `-d -c` is equivalent to `-dc`. -Long commands cannot be concatenated. -They must be clearly separated by a space. +Long commands cannot be concatenated. They must be clearly separated by a space. ### Multiple commands diff --git a/programs/lz4cli.c b/programs/lz4cli.c index 279aaf1..82f2fac 100644 --- a/programs/lz4cli.c +++ b/programs/lz4cli.c @@ -655,8 +655,9 @@ int main(int argc, const char** argv) if (!IS_CONSOLE(stdout)) { /* Default to stdout whenever stdout is not the console. - * Note : this policy is likely to change in the future, don't rely on it ! - * prefer using an explicit `-c` flag */ + * Note : this policy may change in the future, therefore don't rely on it ! + * To ensure `stdout` is explicitly selected, use `-c` command flag. + * Conversely, to ensure output will not become `stdout`, use `-m` command flag */ DISPLAYLEVEL(1, "Warning : using stdout as default output. Do not rely on this behavior: use explicit `-c` instead ! \n"); output_filename=stdoutmark; break; diff --git a/tests/Makefile b/tests/Makefile index 209a530..ddc6d1e 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -317,7 +317,7 @@ test-lz4-basic: lz4 datagen unlz4 lz4cat ! $(LZ4) -c --fast=-1 tmp-tlb-dg20K # lz4 should fail when fast=-1 # Test for #596 @echo "TEST" > tmp-tlb-test - $(LZ4) tmp-tlb-test + $(LZ4) -m tmp-tlb-test $(LZ4) tmp-tlb-test.lz4 tmp-tlb-test2 $(DIFF) -q tmp-tlb-test tmp-tlb-test2 @$(RM) tmp-tlb* -- cgit v1.2.3 From 2133366da080aaa2dbec2a33b52ad236cb493b01 Mon Sep 17 00:00:00 2001 From: gabrielstedman Date: Mon, 22 Apr 2019 09:00:20 +0100 Subject: FR #598 - Make LZ4IO_getCompressedFileInfo internal and reword func --- programs/lz4cli.c | 3 +- programs/lz4io.c | 132 ++++++++++++++++++++++++++---------------------------- programs/lz4io.h | 9 ++-- 3 files changed, 68 insertions(+), 76 deletions(-) diff --git a/programs/lz4cli.c b/programs/lz4cli.c index 23c4be7..9a74286 100644 --- a/programs/lz4cli.c +++ b/programs/lz4cli.c @@ -40,7 +40,6 @@ #include /* exit, calloc, free */ #include /* strcmp, strlen */ #include "bench.h" /* BMK_benchFile, BMK_SetNbIterations, BMK_SetBlocksize, BMK_SetPause */ -#include "lz4frame.h" #include "lz4io.h" /* LZ4IO_compressFilename, LZ4IO_decompressFilename, LZ4IO_compressMultipleFilenames */ #include "lz4hc.h" /* LZ4HC_CLEVEL_MAX */ #include "lz4.h" /* LZ4_VERSION_STRING */ @@ -712,7 +711,7 @@ int main(int argc, const char** argv) if(!multiple_inputs){ inFileNames[ifnIdx++] = input_filename; } - operationResult = LZ4IO_getCompressedFilesInfo(inFileNames, ifnIdx); + operationResult = LZ4IO_displayCompressedFilesInfo(inFileNames, ifnIdx); inFileNames=NULL; } else { /* compression is default action */ diff --git a/programs/lz4io.c b/programs/lz4io.c index 2e38d2f..61fa13f 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -1214,6 +1214,60 @@ static int LZ4IO_decompressDstFile(LZ4IO_prefs_t* const prefs, dRess_t ress, con } +static int LZ4IO_getCompressedFileInfo(const char* input_filename, LZ4F_compFileInfo_t* cfinfo){ + const char *b, + *e; + char *t; + size_t readSize = LZ4F_HEADER_SIZE_MAX; + LZ4F_errorCode_t errorCode; + dRess_t ress; + // LZ4F_compFileInfo_t cfinfo = (LZ4F_compFileInfo_t) LZ4F_INIT_FILEINFO; + /* Open file */ + FILE* const finput = LZ4IO_openSrcFile(input_filename); + if (finput==NULL) return 1; + + /* Get file size */ + if (!UTIL_getFileStat(input_filename, &cfinfo->fileStat)){ + EXM_THROW(60, "Can't stat file : %s", input_filename); + } + + /* Get basename without extension */ + b = strrchr(input_filename, '/'); + if (!b){ + b = strrchr(input_filename, '\\'); + } + if (b && b != input_filename){ + b++; + } else{ + b=input_filename; + } + e = strrchr(b, '.'); + + /* Allocate Memory */ + t = malloc( (e-b+1) * sizeof(char)); + ress.srcBuffer = malloc(LZ4IO_dBufferSize); + if (!t || !ress.srcBuffer) + EXM_THROW(21, "Allocation error : not enough memory"); + strncpy(t, b, (e-b)); + t[e-b] = '\0'; + cfinfo->fileName = t; + + /* init */ + errorCode = LZ4F_createDecompressionContext(&ress.dCtx, LZ4F_VERSION); + if (LZ4F_isError(errorCode)) EXM_THROW(60, "Can't create LZ4F context : %s", LZ4F_getErrorName(errorCode)); + + if (!fread(ress.srcBuffer, readSize, 1, finput)){ + EXM_THROW(30, "Error reading %s ", input_filename); + } + LZ4F_getFrameInfo(ress.dCtx, &cfinfo->frameInfo, ress.srcBuffer, &readSize); + + /* Close input/free resources */ + fclose(finput); + free(ress.srcBuffer); + return 0; +} + + int LZ4IO_decompressFilename(LZ4IO_prefs_t* const prefs, const char* input_filename, const char* output_filename) { dRess_t const ress = LZ4IO_createDResources(prefs); @@ -1266,86 +1320,28 @@ int LZ4IO_decompressMultipleFilenames(LZ4IO_prefs_t* const prefs, const char** i return missingFiles + skippedFiles; } -int LZ4IO_getCompressedFilesInfo(const char** inFileNames, const size_t ifnIdx){ + +int LZ4IO_displayCompressedFilesInfo(const char** inFileNames, const size_t ifnIdx){ size_t idx; int op_result=0; - LZ4F_compFileInfo_t cfinfo = (LZ4F_compFileInfo_t) LZ4F_INIT_FILEINFO; + double ratio; + LZ4F_compFileInfo_t cfinfo; DISPLAY("%16s\t%-20s\t%-20s\t%-10s\t%s\n","BlockChecksumFlag","Compressed", "Uncompressed", "Ratio", "Filename"); for(idx=0; idxfileSize = statbuf.st_size; - - /* Get basename without extension */ - b = strrchr(input_filename, '/'); - if (!b){ - b = strrchr(input_filename, '\\'); - } - if (b && b != input_filename){ - b++; - } else{ - b=input_filename; - } - e = strrchr(b, '.'); - /* Allocate Memory */ - t = malloc( (e-b+1) * sizeof(char)); - ress.srcBuffer = malloc(LZ4IO_dBufferSize); - if (!t || !ress.srcBuffer) - EXM_THROW(21, "Allocation error : not enough memory"); - strncpy(t, b, (e-b)); - t[e-b] = '\0'; - cfinfo->fileName = t; - - /* init */ - errorCode = LZ4F_createDecompressionContext(&ress.dCtx, LZ4F_VERSION); - if (LZ4F_isError(errorCode)) EXM_THROW(60, "Can't create LZ4F context : %s", LZ4F_getErrorName(errorCode)); - - if (!fread(ress.srcBuffer, readSize, 1, finput)){ - EXM_THROW(30, "Error reading %s ", input_filename); - } - // cfinfo->frameInfo = (LZ4F_frameInfo_t) LZ4F_INIT_FRAMEINFO; - LZ4F_getFrameInfo(ress.dCtx, &cfinfo->frameInfo, ress.srcBuffer, &readSize); - if(cfinfo->frameInfo.contentSize){ - cfinfo->ratio = (double)cfinfo->fileSize / cfinfo->frameInfo.contentSize; - } else { - cfinfo->ratio = -1; - } - - /* Close input/free resources */ - fclose(finput); - free(ress.srcBuffer); - return 0; -} diff --git a/programs/lz4io.h b/programs/lz4io.h index 707f233..3d37bd0 100644 --- a/programs/lz4io.h +++ b/programs/lz4io.h @@ -57,11 +57,10 @@ typedef struct LZ4IO_prefs_s LZ4IO_prefs_t; typedef struct { LZ4F_frameInfo_t frameInfo; const char* fileName; - unsigned long long fileSize; - double ratio; + stat_t fileStat; } LZ4F_compFileInfo_t; -#define LZ4F_INIT_FILEINFO { (LZ4F_frameInfo_t) LZ4F_INIT_FRAMEINFO, NULL, 0ULL, -1.f } +#define LZ4F_INIT_FILEINFO { (LZ4F_frameInfo_t) LZ4F_INIT_FRAMEINFO, NULL, stat_t() } LZ4IO_prefs_t* LZ4IO_defaultPreferences(void); void LZ4IO_freePreferences(LZ4IO_prefs_t* const prefs); @@ -125,9 +124,7 @@ int LZ4IO_setSparseFile(LZ4IO_prefs_t* const prefs, int enable); /* Default setting : 0 == no content size present in frame header */ int LZ4IO_setContentSize(LZ4IO_prefs_t* const prefs, int enable); -int LZ4IO_getCompressedFilesInfo(const char** inFileNames,const size_t ifnIdx); - -int LZ4IO_getCompressedFileInfo(const char* input_filename, LZ4F_compFileInfo_t* cfinfo); +int LZ4IO_displayCompressedFilesInfo(const char** inFileNames,const size_t ifnIdx); /* Default setting : 0 == src file preserved */ void LZ4IO_setRemoveSrcFile(LZ4IO_prefs_t* const prefs, unsigned flag); -- cgit v1.2.3 From 0acebbe53dd1b15071f2c48a7b060299b5d03622 Mon Sep 17 00:00:00 2001 From: gabrielstedman Date: Mon, 22 Apr 2019 09:58:26 +0100 Subject: FR #598 - Move LZ4F_compFileInfo_t def to lz4frame.h discard output_file assert if in om_list mode --- lib/lz4frame.h | 7 +++++++ programs/lz4cli.c | 13 ++++++++++--- programs/lz4io.c | 11 +++++++---- programs/lz4io.h | 8 -------- 4 files changed, 24 insertions(+), 15 deletions(-) diff --git a/lib/lz4frame.h b/lib/lz4frame.h index 742c252..5945647 100644 --- a/lib/lz4frame.h +++ b/lib/lz4frame.h @@ -193,6 +193,13 @@ typedef struct { #define LZ4F_INIT_PREFERENCES { LZ4F_INIT_FRAMEINFO, 0, 0u, 0u, { 0u, 0u, 0u } } /* v1.8.3+ */ +typedef struct { + LZ4F_frameInfo_t frameInfo; + const char* fileName; + unsigned long fileSize; +} LZ4F_compFileInfo_t; + +#define LZ4F_INIT_FILEINFO { (LZ4F_frameInfo_t) LZ4F_INIT_FRAMEINFO, NULL, 0UL } /*-********************************* * Simple compression function diff --git a/programs/lz4cli.c b/programs/lz4cli.c index 3315773..443e4b4 100644 --- a/programs/lz4cli.c +++ b/programs/lz4cli.c @@ -693,7 +693,7 @@ int main(int argc, const char** argv) break; } - if (multiple_inputs==0) assert(output_filename); + if (multiple_inputs==0 && mode != om_list) assert(output_filename); /* when multiple_inputs==1, output_filename may simply be useless, * however, output_filename must be !NULL for next strcmp() tests */ if (!output_filename) output_filename = "*\\dummy^!//"; @@ -721,7 +721,14 @@ int main(int argc, const char** argv) operationResult = LZ4IO_decompressMultipleFilenames(prefs, inFileNames, (int)ifnIdx, !strcmp(output_filename,stdoutmark) ? stdoutmark : LZ4_EXTENSION); } else { operationResult = DEFAULT_DECOMPRESSOR(prefs, input_filename, output_filename); - + } + } else if (mode == om_list){ + if(!multiple_inputs){ + inFileNames[ifnIdx++] = input_filename; + } + operationResult = LZ4IO_displayCompressedFilesInfo(inFileNames, ifnIdx); + inFileNames=NULL; + } else { /* compression is default action */ if (legacy_format) { DISPLAYLEVEL(3, "! Generating LZ4 Legacy format (deprecated) ! \n"); LZ4IO_compressFilename_Legacy(prefs, input_filename, output_filename, cLevel); @@ -745,4 +752,4 @@ _cleanup: LZ4IO_freePreferences(prefs); free((void*)inFileNames); return operationResult; -} +} \ No newline at end of file diff --git a/programs/lz4io.c b/programs/lz4io.c index 61fa13f..070e22a 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -1218,6 +1218,7 @@ static int LZ4IO_getCompressedFileInfo(const char* input_filename, LZ4F_compFil const char *b, *e; char *t; + stat_t statbuf; size_t readSize = LZ4F_HEADER_SIZE_MAX; LZ4F_errorCode_t errorCode; dRess_t ress; @@ -1227,10 +1228,12 @@ static int LZ4IO_getCompressedFileInfo(const char* input_filename, LZ4F_compFil if (finput==NULL) return 1; /* Get file size */ - if (!UTIL_getFileStat(input_filename, &cfinfo->fileStat)){ + if (!UTIL_getFileStat(input_filename, &statbuf)){ EXM_THROW(60, "Can't stat file : %s", input_filename); } + cfinfo->fileSize = statbuf.st_size; + /* Get basename without extension */ b = strrchr(input_filename, '/'); if (!b){ @@ -1336,11 +1339,11 @@ int LZ4IO_displayCompressedFilesInfo(const char** inFileNames, const size_t ifnI break; } if(cfinfo.frameInfo.contentSize){ - ratio = (double)cfinfo.fileStat.st_size / cfinfo.frameInfo.contentSize; - DISPLAY("%-16d\t%-20lu\t%-20llu\t%-8.4f\t%s\n",cfinfo.frameInfo.blockChecksumFlag,cfinfo.fileStat.st_size,cfinfo.frameInfo.contentSize, ratio, cfinfo.fileName); + ratio = (double)cfinfo.fileSize / cfinfo.frameInfo.contentSize; + DISPLAY("%-16d\t%-20lu\t%-20llu\t%-8.4f\t%s\n",cfinfo.frameInfo.blockChecksumFlag,cfinfo.fileSize,cfinfo.frameInfo.contentSize, ratio, cfinfo.fileName); } else{ - DISPLAY("%-16d\t%-20lu\t%-20s\t%-10s\t%s\n",cfinfo.frameInfo.blockChecksumFlag,cfinfo.fileStat.st_size, "-", "-", cfinfo.fileName); + DISPLAY("%-16d\t%-20lu\t%-20s\t%-10s\t%s\n",cfinfo.frameInfo.blockChecksumFlag,cfinfo.fileSize, "-", "-", cfinfo.fileName); } } return op_result; diff --git a/programs/lz4io.h b/programs/lz4io.h index fcda5f1..2008b55 100644 --- a/programs/lz4io.h +++ b/programs/lz4io.h @@ -52,14 +52,6 @@ static const char nulmark[] = "/dev/null"; /* ****************** Type Definitions ************** */ /* ************************************************** */ -typedef struct { - LZ4F_frameInfo_t frameInfo; - const char* fileName; - stat_t fileStat; -} LZ4F_compFileInfo_t; - -#define LZ4F_INIT_FILEINFO { (LZ4F_frameInfo_t) LZ4F_INIT_FRAMEINFO, NULL, stat_t() } - typedef struct LZ4IO_prefs_s LZ4IO_prefs_t; LZ4IO_prefs_t* LZ4IO_defaultPreferences(void); -- cgit v1.2.3 From e31b6dc03ca464de55ebb403fb0ef6470d99e1a0 Mon Sep 17 00:00:00 2001 From: gabrielstedman Date: Mon, 22 Apr 2019 10:35:29 +0100 Subject: FR #598 - Make fileSize unsigned long long --- lib/lz4frame.h | 4 ++-- programs/lz4io.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/lz4frame.h b/lib/lz4frame.h index 5945647..8c07e86 100644 --- a/lib/lz4frame.h +++ b/lib/lz4frame.h @@ -196,10 +196,10 @@ typedef struct { typedef struct { LZ4F_frameInfo_t frameInfo; const char* fileName; - unsigned long fileSize; + unsigned long long fileSize; } LZ4F_compFileInfo_t; -#define LZ4F_INIT_FILEINFO { (LZ4F_frameInfo_t) LZ4F_INIT_FRAMEINFO, NULL, 0UL } +#define LZ4F_INIT_FILEINFO { (LZ4F_frameInfo_t) LZ4F_INIT_FRAMEINFO, NULL, 0ULL } /*-********************************* * Simple compression function diff --git a/programs/lz4io.c b/programs/lz4io.c index 070e22a..9aa2b94 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -1340,10 +1340,10 @@ int LZ4IO_displayCompressedFilesInfo(const char** inFileNames, const size_t ifnI } if(cfinfo.frameInfo.contentSize){ ratio = (double)cfinfo.fileSize / cfinfo.frameInfo.contentSize; - DISPLAY("%-16d\t%-20lu\t%-20llu\t%-8.4f\t%s\n",cfinfo.frameInfo.blockChecksumFlag,cfinfo.fileSize,cfinfo.frameInfo.contentSize, ratio, cfinfo.fileName); + DISPLAY("%-16d\t%-20llu\t%-20llu\t%-8.4f\t%s\n",cfinfo.frameInfo.blockChecksumFlag,cfinfo.fileSize,cfinfo.frameInfo.contentSize, ratio, cfinfo.fileName); } else{ - DISPLAY("%-16d\t%-20lu\t%-20s\t%-10s\t%s\n",cfinfo.frameInfo.blockChecksumFlag,cfinfo.fileSize, "-", "-", cfinfo.fileName); + DISPLAY("%-16d\t%-20llu\t%-20s\t%-10s\t%s\n",cfinfo.frameInfo.blockChecksumFlag,cfinfo.fileSize, "-", "-", cfinfo.fileName); } } return op_result; -- cgit v1.2.3 From 84f978a2f3a787c7646ab3773fa7c41bddbb849e Mon Sep 17 00:00:00 2001 From: gabrielstedman Date: Mon, 22 Apr 2019 11:07:08 +0100 Subject: FR #598 - Correctly initialize cfinfo & cast malloc res to (char*) --- programs/lz4io.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/programs/lz4io.c b/programs/lz4io.c index 9aa2b94..175157e 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -1222,7 +1222,6 @@ static int LZ4IO_getCompressedFileInfo(const char* input_filename, LZ4F_compFil size_t readSize = LZ4F_HEADER_SIZE_MAX; LZ4F_errorCode_t errorCode; dRess_t ress; - // LZ4F_compFileInfo_t cfinfo = (LZ4F_compFileInfo_t) LZ4F_INIT_FILEINFO; /* Open file */ FILE* const finput = LZ4IO_openSrcFile(input_filename); if (finput==NULL) return 1; @@ -1247,7 +1246,7 @@ static int LZ4IO_getCompressedFileInfo(const char* input_filename, LZ4F_compFil e = strrchr(b, '.'); /* Allocate Memory */ - t = malloc( (e-b+1) * sizeof(char)); + t = (char*)malloc( (e-b+1) * sizeof(char)); ress.srcBuffer = malloc(LZ4IO_dBufferSize); if (!t || !ress.srcBuffer) EXM_THROW(21, "Allocation error : not enough memory"); @@ -1332,6 +1331,7 @@ int LZ4IO_displayCompressedFilesInfo(const char** inFileNames, const size_t ifnI DISPLAY("%16s\t%-20s\t%-20s\t%-10s\t%s\n","BlockChecksumFlag","Compressed", "Uncompressed", "Ratio", "Filename"); for(idx=0; idx Date: Mon, 22 Apr 2019 13:48:59 -0400 Subject: Initial commits from diff I submitted earlier --- Makefile | 53 +++++++++++++++++++++++++++++++++++++---- examples/Makefile | 47 ++++++++++++++++++++++++++++++++---- lib/Makefile | 42 +++++++++++++++++++++++--------- programs/Makefile | 41 ++++++++++++++++++++++++++++---- tests/Makefile | 71 +++++++++++++++++++++++++++++++++++++++++++++++++------ 5 files changed, 221 insertions(+), 33 deletions(-) diff --git a/Makefile b/Makefile index f3c6ce2..51b4216 100644 --- a/Makefile +++ b/Makefile @@ -36,15 +36,50 @@ TESTDIR = tests EXDIR = examples -# Define nul output -ifneq (,$(filter Windows%,$(OS))) +# Define *.exe as extension for targetting Windows systems +TARGET_OS ?= $(shell uname) +ifeq ($(TARGET_OS),) + TARGET_OS ?= $(OS) +endif + +ifneq (,$(filter Windows%,$(TARGET_OS))) +LIBLZ4 = liblz4-$(LIBVER_MAJOR) +EXT = .exe +else +EXT = .exe +ifneq (,$(filter MINGW%,$(TARGET_OS))) +EXT = .exe +else +ifneq (,$(filter MSYS%,$(TARGET_OS))) +EXT = .exe +else +ifneq (,$(filter CYGWIN%,$(TARGET_OS))) EXT = .exe -VOID = nul else EXT = -VOID = /dev/null +endif +endif +endif endif +#determine if dev/nul based on host environment +ifneq (,$(filter MINGW%,$(shell uname))) +VOID := /dev/null +else +ifneq (,$(filter MSYS%,$(shell uname))) +VOID := /dev/null +else +ifneq (,$(filter CYGWIN%,$(shell uname))) +VOID := /dev/null +else +ifneq (,$(filter Windows%,$(OS))) +VOID := nul +else +VOID := /dev/null +endif +endif +endif +endif .PHONY: default default: lib-release lz4-release @@ -93,7 +128,7 @@ clean: #----------------------------------------------------------------------------- # make install is validated only for Linux, OSX, BSD, Hurd and Solaris targets #----------------------------------------------------------------------------- -ifneq (,$(filter $(shell uname),Linux Darwin GNU/kFreeBSD GNU OpenBSD FreeBSD NetBSD DragonFly SunOS Haiku MidnightBSD MINGW32_NT-6.1 MINGW64_NT-6.1 MINGW32_NT-10.0 MINGW64_NT-10.0)) +ifneq (,$(filter Linux Darwin GNU/kFreeBSD GNU OpenBSD FreeBSD NetBSD DragonFly SunOS Haiku MidnightBSD MINGW32% MINGW64% CYGWIN% MSYS%,$(shell uname))) HOST_OS = POSIX .PHONY: install uninstall @@ -181,6 +216,14 @@ gpptest gpptest32: clean CC=$(CC) $(MAKE) -C $(PRGDIR) all CFLAGS="$(CFLAGS)" CC=$(CC) $(MAKE) -C $(TESTDIR) all CFLAGS="$(CFLAGS)" +cxx17build : CC = "$(CXX) -Wno-deprecated" +cxx17build : CFLAGS = -std=c++17 -Wall -Wextra -Wundef -Wshadow -Wcast-align -Werror -pedantic +cxx17build : clean + $(CXX) -v + CC=$(CC) $(MAKE) -C $(LZ4DIR) all CFLAGS="$(CFLAGS)" + CC=$(CC) $(MAKE) -C $(PRGDIR) all CFLAGS="$(CFLAGS)" + CC=$(CC) $(MAKE) -C $(TESTDIR) all CFLAGS="$(CFLAGS)" + ctocpptest: LIBCC="$(CC)" ctocpptest: TESTCC="$(CXX)" ctocpptest: CFLAGS="" diff --git a/examples/Makefile b/examples/Makefile index 103e7ec..26aaafd 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -38,14 +38,51 @@ LZ4 = ../programs/lz4 # Define *.exe as extension for Windows systems -ifneq (,$(filter Windows%,$(OS))) -EXT =.exe -VOID = nul + +# Define *.exe as extension for targetting Windows systems +TARGET_OS ?= $(shell uname) +ifeq ($(TARGET_OS),) + TARGET_OS ?= $(OS) +endif + +ifneq (,$(filter Windows%,$(TARGET_OS))) +LIBLZ4 = liblz4-$(LIBVER_MAJOR) +EXT = .exe +else +EXT = .exe +ifneq (,$(filter MINGW%,$(TARGET_OS))) +EXT = .exe else -EXT = -VOID = /dev/null +ifneq (,$(filter MSYS%,$(TARGET_OS))) +EXT = .exe +else +ifneq (,$(filter CYGWIN%,$(TARGET_OS))) +EXT = .exe +else +EXT = +endif +endif +endif endif +#determine if dev/nul based on host environment +ifneq (,$(filter MINGW%,$(shell uname))) +VOID := /dev/null +else +ifneq (,$(filter MSYS%,$(shell uname))) +VOID := /dev/null +else +ifneq (,$(filter CYGWIN%,$(shell uname))) +VOID := /dev/null +else +ifneq (,$(filter Windows%,$(OS))) +VOID := nul +else +VOID := /dev/null +endif +endif +endif +endif default: all diff --git a/lib/Makefile b/lib/Makefile index cb1571c..4e96b57 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -45,7 +45,10 @@ LIBVER := $(shell echo $(LIBVER_SCRIPT)) BUILD_SHARED:=yes BUILD_STATIC:=yes -OS ?= $(shell uname) +TARGET_OS ?= $(shell uname) +ifeq ($(TARGET_OS),) + TARGET_OS ?= $(OS) +endif CPPFLAGS+= -DXXH_NAMESPACE=LZ4_ CFLAGS ?= -O3 DEBUGFLAGS:= -Wall -Wextra -Wcast-qual -Wcast-align -Wshadow \ @@ -71,10 +74,26 @@ else SHARED_EXT_VER = $(SHARED_EXT).$(LIBVER) endif -ifneq (,$(filter Windows%,$(OS))) +WINBASED = yes +ifneq (,$(filter Windows%,$(TARGET_OS))) LIBLZ4 = liblz4-$(LIBVER_MAJOR) +LIBLZ4_EXP = liblz4.lib +else +LIBLZ4_EXP = liblz4.dll.a +ifneq (,$(filter MINGW%,$(TARGET_OS))) +LIBLZ4 = liblz4 +else +ifneq (,$(filter MSYS%,$(TARGET_OS))) +LIBLZ4 = msys-lz4-$(LIBVER_MAJOR) +else +ifneq (,$(filter CYGWIN%,$(TARGET_OS))) +LIBLZ4 = cyglz4-$(LIBVER_MAJOR) else LIBLZ4 = liblz4.$(SHARED_EXT_VER) +WINBASED = no +endif +endif +endif endif .PHONY: default @@ -106,8 +125,8 @@ endif $(LIBLZ4): $(SRCFILES) ifeq ($(BUILD_SHARED),yes) # can be disabled on command line @echo compiling dynamic library $(LIBVER) -ifneq (,$(filter Windows%,$(OS))) - $(Q)$(CC) $(FLAGS) -DLZ4_DLL_EXPORT=1 -shared $^ -o dll/$@.dll -Wl,--out-implib,dll/liblz4.lib +ifeq ($(WINBASED),yes) + $(Q)$(CC) $(FLAGS) -DLZ4_DLL_EXPORT=1 -shared $^ -o dll/$@.dll -Wl,--out-implib,dll/$(LIBLZ4_EXP) else $(Q)$(CC) $(FLAGS) -shared $^ -fPIC -fvisibility=hidden $(SONAME_FLAGS) -o $@ @echo creating versioned links @@ -116,18 +135,19 @@ else endif endif +ifeq (,$(filter MINGW%,$(TARGET_OS))) liblz4: $(LIBLZ4) +endif clean: - $(Q)$(RM) core *.o liblz4.pc dll/liblz4.dll dll/liblz4.lib + $(Q)$(RM) core *.o liblz4.pc dll/$(LIBLZ4).dll dll/$(LIBLZ4_EXP) $(Q)$(RM) *.a *.$(SHARED_EXT) *.$(SHARED_EXT_MAJOR) *.$(SHARED_EXT_VER) @echo Cleaning library completed - #----------------------------------------------------------------------------- # make install is validated only for Linux, OSX, BSD, Hurd and Solaris targets #----------------------------------------------------------------------------- -ifneq (,$(filter $(shell uname),Linux Darwin GNU/kFreeBSD GNU OpenBSD FreeBSD NetBSD DragonFly SunOS Haiku MidnightBSD MINGW32_NT-6.1 MINGW64_NT-6.1 MINGW32_NT-10.0 MINGW64_NT-10.0)) +ifneq (,$(filter Linux Darwin GNU/kFreeBSD GNU OpenBSD FreeBSD NetBSD DragonFly SunOS Haiku MidnightBSD MINGW32_NT% MINGW64_NT% MSYS% CYGWIN_NT%,$(shell uname))) .PHONY: listL120 listL120: # extract lines >= 120 characters in *.{c,h}, by Takayuki Matsuoka (note : $$, for Makefile compatibility) @@ -184,9 +204,9 @@ ifeq ($(BUILD_SHARED),yes) # Traditionnally, one installs the DLLs in the bin directory as programs # search them first in their directory. This allows to not pollute system # directories (like c:/windows/system32), nor modify the PATH variable. -ifneq (,$(filter Windows%,$(OS))) +ifeq ($(WINBASED),yes) $(Q)$(INSTALL_PROGRAM) dll/$(LIBLZ4).dll $(DESTDIR)$(bindir) - $(Q)$(INSTALL_PROGRAM) dll/liblz4.lib $(DESTDIR)$(libdir) + $(Q)$(INSTALL_PROGRAM) dll/$(LIBLZ4_EXP) $(DESTDIR)$(libdir) else $(Q)$(INSTALL_PROGRAM) liblz4.$(SHARED_EXT_VER) $(DESTDIR)$(libdir) $(Q)ln -sf liblz4.$(SHARED_EXT_VER) $(DESTDIR)$(libdir)/liblz4.$(SHARED_EXT_MAJOR) @@ -201,9 +221,9 @@ endif uninstall: $(Q)$(RM) $(DESTDIR)$(pkgconfigdir)/liblz4.pc -ifneq (,$(filter Windows%,$(OS))) +ifeq (WINBASED,1) $(Q)$(RM) $(DESTDIR)$(bindir)/$(LIBLZ4).dll - $(Q)$(RM) $(DESTDIR)$(libdir)/liblz4.lib + $(Q)$(RM) $(DESTDIR)$(libdir)/$(LIBLZ4_EXP) else $(Q)$(RM) $(DESTDIR)$(libdir)/liblz4.$(SHARED_EXT) $(Q)$(RM) $(DESTDIR)$(libdir)/liblz4.$(SHARED_EXT_MAJOR) diff --git a/programs/Makefile b/programs/Makefile index 92fd683..47912d8 100644 --- a/programs/Makefile +++ b/programs/Makefile @@ -56,17 +56,48 @@ LZ4_VERSION=$(LIBVER) MD2ROFF = ronn MD2ROFF_FLAGS = --roff --warnings --manual="User Commands" --organization="lz4 $(LZ4_VERSION)" +TARGET_OS ?= $(shell uname) +ifeq ($(TARGET_OS),) + TARGET_OS ?= $(OS) +endif # Define *.exe as extension for Windows systems -ifneq (,$(filter Windows%,$(OS))) +ifneq (,$(filter Windows%,$(TARGET_OS))) +EXT :=.exe +else +ifneq (,$(filter MINGW%,$(TARGET_OS))) +EXT :=.exe +else +ifneq (,$(filter MSYS%,$(TARGET_OS))) +EXT :=.exe +else +ifneq (,$(filter CYGWIN%,$(TARGET_OS))) EXT :=.exe -VOID := nul else EXT := -VOID := /dev/null +endif +endif +endif endif - +#determine if dev/nul based on host environment +ifneq (,$(filter MINGW%,$(shell uname))) +VOID := /dev/null +else +ifneq (,$(filter MSYS%,$(shell uname))) +VOID := /dev/null +else +ifneq (,$(filter CYGWIN%,$(shell uname))) +VOID := /dev/null +else +ifneq (,$(filter Windows%,$(OS))) +VOID := nul +else +VOID := /dev/null +endif +endif +endif +endif default: lz4-release @@ -109,7 +140,7 @@ clean: #----------------------------------------------------------------------------- # make install is validated only for Linux, OSX, BSD, Hurd and Solaris targets #----------------------------------------------------------------------------- -ifneq (,$(filter $(shell uname),Linux Darwin GNU/kFreeBSD GNU OpenBSD FreeBSD NetBSD DragonFly SunOS Haiku MidnightBSD MINGW32_NT-6.1 MINGW64_NT-6.1 MINGW32_NT-10.0 MINGW64_NT-10.0)) +ifneq (,$(filter Linux Darwin GNU/kFreeBSD GNU OpenBSD FreeBSD NetBSD DragonFly SunOS Haiku MidnightBSD MINGW32_NT% MINGW64_NT% MSYS% CYGWIN_NT%,$(shell uname))) unlz4: lz4 ln -s lz4$(EXT) unlz4$(EXT) diff --git a/tests/Makefile b/tests/Makefile index 70cae63..d8104de 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -43,15 +43,72 @@ CFLAGS += $(DEBUGFLAGS) $(MOREFLAGS) CPPFLAGS+= -I$(LZ4DIR) -I$(PRGDIR) -DXXH_NAMESPACE=LZ4_ FLAGS = $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) - # Define *.exe as extension for Windows systems +TARGET_OS ?= $(shell uname) +ifeq ($(TARGET_OS),) + TARGET_OS ?= $(OS) +endif + +ifneq (,$(filter Windows%,$(TARGET_OS))) +EXT :=.exe +else +ifneq (,$(filter MINGW%,$(TARGET_OS))) +EXT :=.exe +else +ifneq (,$(filter MSYS%,$(TARGET_OS))) +EXT :=.exe +else +ifneq (,$(filter CYGWIN%,$(TARGET_OS))) +EXT :=.exe +else +EXT := +endif +endif +endif +endif + +# determine name of .DLL for the target +WINBASED = yes +ifneq (,$(filter Windows%,$(TARGET_OS))) +LIBLZ4 = liblz4-$(LIBVER_MAJOR) +LIBLZ4_EXP = liblz4.lib +else +LIBLZ4_EXP = liblz4.dll.a +ifneq (,$(filter MINGW%,$(TARGET_OS))) +LIBLZ4 = liblz4 +else +ifneq (,$(filter MSYS%,$(TARGET_OS))) +LIBLZ4 = msys-lz4-$(LIBVER_MAJOR) +else +ifneq (,$(filter CYGWIN%,$(TARGET_OS))) +LIBLZ4 = cyglz4-$(LIBVER_MAJOR) +else +LIBLZ4 = liblz4.$(SHARED_EXT_VER) +WINBASED = no +endif +endif +endif +endif + +#determine if dev/nul based on host environment +ifneq (,$(filter MINGW%,$(shell uname))) +VOID := /dev/null +else +ifneq (,$(filter MSYS%,$(shell uname))) +VOID := /dev/null +else +ifneq (,$(filter CYGWIN%,$(shell uname))) +VOID := /dev/null +else ifneq (,$(filter Windows%,$(OS))) -EXT =.exe -VOID = nul +VOID := nul else -EXT = -VOID = /dev/null +VOID := /dev/null +endif endif +endif +endif + LZ4 := $(PRGDIR)/lz4$(EXT) @@ -95,7 +152,7 @@ fullbench-lib: fullbench.c $(LZ4DIR)/liblz4.a fullbench-dll: fullbench.c $(LZ4DIR)/xxhash.c $(MAKE) -C $(LZ4DIR) liblz4 - $(CC) $(FLAGS) $^ -o $@$(EXT) -DLZ4_DLL_IMPORT=1 $(LZ4DIR)/dll/liblz4.dll + $(CC) $(FLAGS) $^ -o $@$(EXT) -DLZ4_DLL_IMPORT=1 $(LZ4DIR)/dll/$(LIBLZ4).dll fuzzer : lz4.o lz4hc.o xxhash.o fuzzer.c $(CC) $(FLAGS) $^ -o $@$(EXT) @@ -137,7 +194,7 @@ checkTag: checkTag.c $(LZ4DIR)/lz4.h #----------------------------------------------------------------------------- # validated only for Linux, OSX, BSD, Hurd and Solaris targets #----------------------------------------------------------------------------- -ifneq (,$(filter $(shell uname),Linux Darwin GNU/kFreeBSD GNU OpenBSD FreeBSD NetBSD DragonFly SunOS Haiku MidnightBSD)) +ifneq (,$(filter Linux Darwin GNU/kFreeBSD GNU OpenBSD FreeBSD NetBSD DragonFly SunOS Haiku MidnightBSD MINGW32% MINGW64% CYGWIN% MSYS%,$(shell uname))) MD5:=md5sum ifneq (,$(filter $(shell uname), Darwin )) -- cgit v1.2.3 From ae5cea9112479df58d226f79ae34c07bc636a04d Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 22 Apr 2019 12:27:25 -0700 Subject: fixed C90 compliance re-structure code, have everything into a single section of lz4io.c --- lib/lz4frame.h | 7 --- programs/lz4cli.c | 2 +- programs/lz4io.c | 127 +++++++++++++++++++++++++++++------------------------- programs/lz4io.h | 6 ++- 4 files changed, 74 insertions(+), 68 deletions(-) diff --git a/lib/lz4frame.h b/lib/lz4frame.h index 8c07e86..742c252 100644 --- a/lib/lz4frame.h +++ b/lib/lz4frame.h @@ -193,13 +193,6 @@ typedef struct { #define LZ4F_INIT_PREFERENCES { LZ4F_INIT_FRAMEINFO, 0, 0u, 0u, { 0u, 0u, 0u } } /* v1.8.3+ */ -typedef struct { - LZ4F_frameInfo_t frameInfo; - const char* fileName; - unsigned long long fileSize; -} LZ4F_compFileInfo_t; - -#define LZ4F_INIT_FILEINFO { (LZ4F_frameInfo_t) LZ4F_INIT_FRAMEINFO, NULL, 0ULL } /*-********************************* * Simple compression function diff --git a/programs/lz4cli.c b/programs/lz4cli.c index 443e4b4..39ff1ea 100644 --- a/programs/lz4cli.c +++ b/programs/lz4cli.c @@ -752,4 +752,4 @@ _cleanup: LZ4IO_freePreferences(prefs); free((void*)inFileNames); return operationResult; -} \ No newline at end of file +} diff --git a/programs/lz4io.c b/programs/lz4io.c index 175157e..1e6efe1 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -1214,62 +1214,6 @@ static int LZ4IO_decompressDstFile(LZ4IO_prefs_t* const prefs, dRess_t ress, con } -static int LZ4IO_getCompressedFileInfo(const char* input_filename, LZ4F_compFileInfo_t* cfinfo){ - const char *b, - *e; - char *t; - stat_t statbuf; - size_t readSize = LZ4F_HEADER_SIZE_MAX; - LZ4F_errorCode_t errorCode; - dRess_t ress; - /* Open file */ - FILE* const finput = LZ4IO_openSrcFile(input_filename); - if (finput==NULL) return 1; - - /* Get file size */ - if (!UTIL_getFileStat(input_filename, &statbuf)){ - EXM_THROW(60, "Can't stat file : %s", input_filename); - } - - cfinfo->fileSize = statbuf.st_size; - - /* Get basename without extension */ - b = strrchr(input_filename, '/'); - if (!b){ - b = strrchr(input_filename, '\\'); - } - if (b && b != input_filename){ - b++; - } else{ - b=input_filename; - } - e = strrchr(b, '.'); - - /* Allocate Memory */ - t = (char*)malloc( (e-b+1) * sizeof(char)); - ress.srcBuffer = malloc(LZ4IO_dBufferSize); - if (!t || !ress.srcBuffer) - EXM_THROW(21, "Allocation error : not enough memory"); - strncpy(t, b, (e-b)); - t[e-b] = '\0'; - cfinfo->fileName = t; - - /* init */ - errorCode = LZ4F_createDecompressionContext(&ress.dCtx, LZ4F_VERSION); - if (LZ4F_isError(errorCode)) EXM_THROW(60, "Can't create LZ4F context : %s", LZ4F_getErrorName(errorCode)); - - if (!fread(ress.srcBuffer, readSize, 1, finput)){ - EXM_THROW(30, "Error reading %s ", input_filename); - } - LZ4F_getFrameInfo(ress.dCtx, &cfinfo->frameInfo, ress.srcBuffer, &readSize); - - /* Close input/free resources */ - fclose(finput); - free(ress.srcBuffer); - return 0; -} - - int LZ4IO_decompressFilename(LZ4IO_prefs_t* const prefs, const char* input_filename, const char* output_filename) { dRess_t const ress = LZ4IO_createDResources(prefs); @@ -1323,15 +1267,82 @@ int LZ4IO_decompressMultipleFilenames(LZ4IO_prefs_t* const prefs, const char** i } +/* ********************************************************************* */ +/* ********************** LZ4 --list command *********************** */ +/* ********************************************************************* */ + +typedef struct { + LZ4F_frameInfo_t frameInfo; + const char* fileName; + unsigned long long fileSize; +} LZ4F_compFileInfo_t; + +#define LZ4F_INIT_FILEINFO { LZ4F_INIT_FRAMEINFO, NULL, 0ULL } + + +static int LZ4IO_getCompressedFileInfo(const char* input_filename, LZ4F_compFileInfo_t* cfinfo){ + const char *b, + *e; + char *t; + stat_t statbuf; + size_t readSize = LZ4F_HEADER_SIZE_MAX; + LZ4F_errorCode_t errorCode; + dRess_t ress; + /* Open file */ + FILE* const finput = LZ4IO_openSrcFile(input_filename); + if (finput==NULL) return 1; + + /* Get file size */ + if (!UTIL_getFileStat(input_filename, &statbuf)){ + EXM_THROW(60, "Can't stat file : %s", input_filename); + } + + cfinfo->fileSize = (unsigned long long)statbuf.st_size; + + /* Get basename without extension */ + b = strrchr(input_filename, '/'); + if (!b){ + b = strrchr(input_filename, '\\'); + } + if (b && b != input_filename){ + b++; + } else{ + b=input_filename; + } + e = strrchr(b, '.'); + + /* Allocate Memory */ + t = (char*)malloc( (size_t)(e-b+1) * sizeof(char)); + ress.srcBuffer = malloc(LZ4IO_dBufferSize); + if (!t || !ress.srcBuffer) + EXM_THROW(21, "Allocation error : not enough memory"); + strncpy(t, b, (e-b)); + t[e-b] = '\0'; + cfinfo->fileName = t; + + /* init */ + errorCode = LZ4F_createDecompressionContext(&ress.dCtx, LZ4F_VERSION); + if (LZ4F_isError(errorCode)) EXM_THROW(60, "Can't create LZ4F context : %s", LZ4F_getErrorName(errorCode)); + + if (!fread(ress.srcBuffer, readSize, 1, finput)){ + EXM_THROW(30, "Error reading %s ", input_filename); + } + LZ4F_getFrameInfo(ress.dCtx, &cfinfo->frameInfo, ress.srcBuffer, &readSize); + + /* Close input/free resources */ + fclose(finput); + free(ress.srcBuffer); + return 0; +} + int LZ4IO_displayCompressedFilesInfo(const char** inFileNames, const size_t ifnIdx){ size_t idx; int op_result=0; double ratio; - LZ4F_compFileInfo_t cfinfo; DISPLAY("%16s\t%-20s\t%-20s\t%-10s\t%s\n","BlockChecksumFlag","Compressed", "Uncompressed", "Ratio", "Filename"); for(idx=0; idx Date: Mon, 22 Apr 2019 15:48:48 -0400 Subject: Try to put some tests I made in ONE place. I also moved a test for "install" in one place to try to isolate it. --- Makefile | 48 ++------------------------------------ Makefile.inc | 59 +++++++++++++++++++++++++++++++++++++++++++++++ examples/Makefile | 48 +------------------------------------- lib/Makefile | 28 ++-------------------- tests/Makefile | 69 ++----------------------------------------------------- 5 files changed, 66 insertions(+), 186 deletions(-) create mode 100644 Makefile.inc diff --git a/Makefile b/Makefile index 51b4216..b7926b7 100644 --- a/Makefile +++ b/Makefile @@ -35,51 +35,7 @@ PRGDIR = programs TESTDIR = tests EXDIR = examples - -# Define *.exe as extension for targetting Windows systems -TARGET_OS ?= $(shell uname) -ifeq ($(TARGET_OS),) - TARGET_OS ?= $(OS) -endif - -ifneq (,$(filter Windows%,$(TARGET_OS))) -LIBLZ4 = liblz4-$(LIBVER_MAJOR) -EXT = .exe -else -EXT = .exe -ifneq (,$(filter MINGW%,$(TARGET_OS))) -EXT = .exe -else -ifneq (,$(filter MSYS%,$(TARGET_OS))) -EXT = .exe -else -ifneq (,$(filter CYGWIN%,$(TARGET_OS))) -EXT = .exe -else -EXT = -endif -endif -endif -endif - -#determine if dev/nul based on host environment -ifneq (,$(filter MINGW%,$(shell uname))) -VOID := /dev/null -else -ifneq (,$(filter MSYS%,$(shell uname))) -VOID := /dev/null -else -ifneq (,$(filter CYGWIN%,$(shell uname))) -VOID := /dev/null -else -ifneq (,$(filter Windows%,$(OS))) -VOID := nul -else -VOID := /dev/null -endif -endif -endif -endif +include Makefile.inc .PHONY: default default: lib-release lz4-release @@ -128,7 +84,7 @@ clean: #----------------------------------------------------------------------------- # make install is validated only for Linux, OSX, BSD, Hurd and Solaris targets #----------------------------------------------------------------------------- -ifneq (,$(filter Linux Darwin GNU/kFreeBSD GNU OpenBSD FreeBSD NetBSD DragonFly SunOS Haiku MidnightBSD MINGW32% MINGW64% CYGWIN% MSYS%,$(shell uname))) +ifeq ($(POSIX_ENV),yes) HOST_OS = POSIX .PHONY: install uninstall diff --git a/Makefile.inc b/Makefile.inc new file mode 100644 index 0000000..d9dc891 --- /dev/null +++ b/Makefile.inc @@ -0,0 +1,59 @@ +TARGET_OS ?= $(shell uname) +ifeq ($(TARGET_OS),) + TARGET_OS ?= $(OS) +endif + +ifneq (,$(filter Windows%,$(TARGET_OS))) +LIBLZ4 = liblz4-$(LIBVER_MAJOR) +LIBLZ4_EXP = liblz4.lib +WINBASED = yes +else +LIBLZ4_EXP = liblz4.dll.a + ifneq (,$(filter MINGW%,$(TARGET_OS))) +LIBLZ4 = liblz4 +WINBASED = yes + else + ifneq (,$(filter MSYS%,$(TARGET_OS))) +LIBLZ4 = msys-lz4-$(LIBVER_MAJOR) +WINBASED = yes + else + ifneq (,$(filter CYGWIN%,$(TARGET_OS))) +LIBLZ4 = cyglz4-$(LIBVER_MAJOR) +WINBASED = yes + else +LIBLZ4 = liblz4.$(SHARED_EXT_VER) +WINBASED = no +EXT = + endif + endif + endif +endif + +ifeq ($(WINBASED),yes) +EXT = .exe +endif + +#determine if dev/nul based on host environment +ifneq (,$(filter MINGW%,$(shell uname))) +VOID := /dev/null +else + ifneq (,$(filter MSYS%,$(shell uname))) +VOID := /dev/null + else + ifneq (,$(filter CYGWIN%,$(shell uname))) +VOID := /dev/null + else + ifneq (,$(filter Windows%,$(OS))) +VOID := nul + else +VOID := /dev/null + endif + endif + endif +endif + +ifneq (,$(filter Linux Darwin GNU/kFreeBSD GNU OpenBSD FreeBSD NetBSD DragonFly SunOS Haiku MidnightBSD MINGW32% MINGW64% CYGWIN% MSYS%,$(shell uname))) +POSIX_ENV = Yes +else +POSIX_ENV = No +endif diff --git a/examples/Makefile b/examples/Makefile index 26aaafd..6a34b33 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -36,53 +36,7 @@ TESTFILE = Makefile LZ4DIR := ../lib LZ4 = ../programs/lz4 - -# Define *.exe as extension for Windows systems - -# Define *.exe as extension for targetting Windows systems -TARGET_OS ?= $(shell uname) -ifeq ($(TARGET_OS),) - TARGET_OS ?= $(OS) -endif - -ifneq (,$(filter Windows%,$(TARGET_OS))) -LIBLZ4 = liblz4-$(LIBVER_MAJOR) -EXT = .exe -else -EXT = .exe -ifneq (,$(filter MINGW%,$(TARGET_OS))) -EXT = .exe -else -ifneq (,$(filter MSYS%,$(TARGET_OS))) -EXT = .exe -else -ifneq (,$(filter CYGWIN%,$(TARGET_OS))) -EXT = .exe -else -EXT = -endif -endif -endif -endif - -#determine if dev/nul based on host environment -ifneq (,$(filter MINGW%,$(shell uname))) -VOID := /dev/null -else -ifneq (,$(filter MSYS%,$(shell uname))) -VOID := /dev/null -else -ifneq (,$(filter CYGWIN%,$(shell uname))) -VOID := /dev/null -else -ifneq (,$(filter Windows%,$(OS))) -VOID := nul -else -VOID := /dev/null -endif -endif -endif -endif +include ../Makefile.inc default: all diff --git a/lib/Makefile b/lib/Makefile index 4e96b57..0546031 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -45,10 +45,6 @@ LIBVER := $(shell echo $(LIBVER_SCRIPT)) BUILD_SHARED:=yes BUILD_STATIC:=yes -TARGET_OS ?= $(shell uname) -ifeq ($(TARGET_OS),) - TARGET_OS ?= $(OS) -endif CPPFLAGS+= -DXXH_NAMESPACE=LZ4_ CFLAGS ?= -O3 DEBUGFLAGS:= -Wall -Wextra -Wcast-qual -Wcast-align -Wshadow \ @@ -74,27 +70,7 @@ else SHARED_EXT_VER = $(SHARED_EXT).$(LIBVER) endif -WINBASED = yes -ifneq (,$(filter Windows%,$(TARGET_OS))) -LIBLZ4 = liblz4-$(LIBVER_MAJOR) -LIBLZ4_EXP = liblz4.lib -else -LIBLZ4_EXP = liblz4.dll.a -ifneq (,$(filter MINGW%,$(TARGET_OS))) -LIBLZ4 = liblz4 -else -ifneq (,$(filter MSYS%,$(TARGET_OS))) -LIBLZ4 = msys-lz4-$(LIBVER_MAJOR) -else -ifneq (,$(filter CYGWIN%,$(TARGET_OS))) -LIBLZ4 = cyglz4-$(LIBVER_MAJOR) -else -LIBLZ4 = liblz4.$(SHARED_EXT_VER) -WINBASED = no -endif -endif -endif -endif +include ../Makefile.inc .PHONY: default default: lib-release @@ -147,7 +123,7 @@ clean: #----------------------------------------------------------------------------- # make install is validated only for Linux, OSX, BSD, Hurd and Solaris targets #----------------------------------------------------------------------------- -ifneq (,$(filter Linux Darwin GNU/kFreeBSD GNU OpenBSD FreeBSD NetBSD DragonFly SunOS Haiku MidnightBSD MINGW32_NT% MINGW64_NT% MSYS% CYGWIN_NT%,$(shell uname))) +ifeq ($(POSIX_ENV),yes) .PHONY: listL120 listL120: # extract lines >= 120 characters in *.{c,h}, by Takayuki Matsuoka (note : $$, for Makefile compatibility) diff --git a/tests/Makefile b/tests/Makefile index d8104de..b9afb02 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -43,71 +43,7 @@ CFLAGS += $(DEBUGFLAGS) $(MOREFLAGS) CPPFLAGS+= -I$(LZ4DIR) -I$(PRGDIR) -DXXH_NAMESPACE=LZ4_ FLAGS = $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) -# Define *.exe as extension for Windows systems -TARGET_OS ?= $(shell uname) -ifeq ($(TARGET_OS),) - TARGET_OS ?= $(OS) -endif - -ifneq (,$(filter Windows%,$(TARGET_OS))) -EXT :=.exe -else -ifneq (,$(filter MINGW%,$(TARGET_OS))) -EXT :=.exe -else -ifneq (,$(filter MSYS%,$(TARGET_OS))) -EXT :=.exe -else -ifneq (,$(filter CYGWIN%,$(TARGET_OS))) -EXT :=.exe -else -EXT := -endif -endif -endif -endif - -# determine name of .DLL for the target -WINBASED = yes -ifneq (,$(filter Windows%,$(TARGET_OS))) -LIBLZ4 = liblz4-$(LIBVER_MAJOR) -LIBLZ4_EXP = liblz4.lib -else -LIBLZ4_EXP = liblz4.dll.a -ifneq (,$(filter MINGW%,$(TARGET_OS))) -LIBLZ4 = liblz4 -else -ifneq (,$(filter MSYS%,$(TARGET_OS))) -LIBLZ4 = msys-lz4-$(LIBVER_MAJOR) -else -ifneq (,$(filter CYGWIN%,$(TARGET_OS))) -LIBLZ4 = cyglz4-$(LIBVER_MAJOR) -else -LIBLZ4 = liblz4.$(SHARED_EXT_VER) -WINBASED = no -endif -endif -endif -endif - -#determine if dev/nul based on host environment -ifneq (,$(filter MINGW%,$(shell uname))) -VOID := /dev/null -else -ifneq (,$(filter MSYS%,$(shell uname))) -VOID := /dev/null -else -ifneq (,$(filter CYGWIN%,$(shell uname))) -VOID := /dev/null -else -ifneq (,$(filter Windows%,$(OS))) -VOID := nul -else -VOID := /dev/null -endif -endif -endif -endif +include ../Makefile.inc LZ4 := $(PRGDIR)/lz4$(EXT) @@ -117,7 +53,6 @@ TEST_FILES := COPYING FUZZER_TIME := -T90s NB_LOOPS ?= -i1 - default: all all: fullbench fuzzer frametest roundTripTest datagen checkFrame @@ -194,7 +129,7 @@ checkTag: checkTag.c $(LZ4DIR)/lz4.h #----------------------------------------------------------------------------- # validated only for Linux, OSX, BSD, Hurd and Solaris targets #----------------------------------------------------------------------------- -ifneq (,$(filter Linux Darwin GNU/kFreeBSD GNU OpenBSD FreeBSD NetBSD DragonFly SunOS Haiku MidnightBSD MINGW32% MINGW64% CYGWIN% MSYS%,$(shell uname))) +ifeq ($(POSIX_ENV),yes) MD5:=md5sum ifneq (,$(filter $(shell uname), Darwin )) -- cgit v1.2.3 From 2acddd9918bdef87179d63fd06dc31e9d9bc3f1e Mon Sep 17 00:00:00 2001 From: JPeterMugaas Date: Mon, 22 Apr 2019 16:06:04 -0400 Subject: Fix a test for mingw --- Makefile | 2 +- Makefile.inc | 2 +- lib/Makefile | 2 +- tests/Makefile | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index b7926b7..dd731eb 100644 --- a/Makefile +++ b/Makefile @@ -84,7 +84,7 @@ clean: #----------------------------------------------------------------------------- # make install is validated only for Linux, OSX, BSD, Hurd and Solaris targets #----------------------------------------------------------------------------- -ifeq ($(POSIX_ENV),yes) +ifeq ($(POSIX_ENV),Yes) HOST_OS = POSIX .PHONY: install uninstall diff --git a/Makefile.inc b/Makefile.inc index d9dc891..c09ba62 100644 --- a/Makefile.inc +++ b/Makefile.inc @@ -52,7 +52,7 @@ VOID := /dev/null endif endif -ifneq (,$(filter Linux Darwin GNU/kFreeBSD GNU OpenBSD FreeBSD NetBSD DragonFly SunOS Haiku MidnightBSD MINGW32% MINGW64% CYGWIN% MSYS%,$(shell uname))) +ifneq (,$(filter Linux Darwin GNU/kFreeBSD GNU OpenBSD FreeBSD NetBSD DragonFly SunOS Haiku MidnightBSD MINGW% CYGWIN% MSYS%,$(shell uname))) POSIX_ENV = Yes else POSIX_ENV = No diff --git a/lib/Makefile b/lib/Makefile index 0546031..28853df 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -123,7 +123,7 @@ clean: #----------------------------------------------------------------------------- # make install is validated only for Linux, OSX, BSD, Hurd and Solaris targets #----------------------------------------------------------------------------- -ifeq ($(POSIX_ENV),yes) +ifeq ($(POSIX_ENV),Yes) .PHONY: listL120 listL120: # extract lines >= 120 characters in *.{c,h}, by Takayuki Matsuoka (note : $$, for Makefile compatibility) diff --git a/tests/Makefile b/tests/Makefile index b9afb02..9b7ed8d 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -129,7 +129,7 @@ checkTag: checkTag.c $(LZ4DIR)/lz4.h #----------------------------------------------------------------------------- # validated only for Linux, OSX, BSD, Hurd and Solaris targets #----------------------------------------------------------------------------- -ifeq ($(POSIX_ENV),yes) +ifeq ($(POSIX_ENV),Yes) MD5:=md5sum ifneq (,$(filter $(shell uname), Darwin )) -- cgit v1.2.3 From 0d4c885abb793b3bdb7ad6c815671e5847b53e6e Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 22 Apr 2019 14:01:19 -0700 Subject: refactored --list function better handling of special conditions, better scoping of variables. Also : updated man page --- programs/lz4.1 | 24 ++++---- programs/lz4io.c | 176 ++++++++++++++++++++++++++++++------------------------- programs/lz4io.h | 3 +- tests/Makefile | 1 + 4 files changed, 111 insertions(+), 93 deletions(-) diff --git a/programs/lz4.1 b/programs/lz4.1 index 1576e45..eb82b68 100644 --- a/programs/lz4.1 +++ b/programs/lz4.1 @@ -1,5 +1,5 @@ . -.TH "LZ4" "1" "April 2019" "lz4 1.9.0" "User Commands" +.TH "LZ4" "1" "April 2019" "lz4 1.9.1" "User Commands" . .SH "NAME" \fBlz4\fR \- lz4, unlz4, lz4cat \- Compress or decompress \.lz4 files @@ -23,9 +23,6 @@ When writing scripts that need to decompress files, it is recommended to always \fBlz4\fR supports a command line syntax similar \fIbut not identical\fR to \fBgzip(1)\fR\. Differences are : . .IP "\(bu" 4 -\fBlz4\fR preserves original files -. -.IP "\(bu" 4 \fBlz4\fR compresses a single file by default (see \fB\-m\fR for multiple files) . .IP "\(bu" 4 @@ -35,19 +32,16 @@ When writing scripts that need to decompress files, it is recommended to always \fBlz4 file\.lz4\fR will default to decompression (use \fB\-z\fR to force compression) . .IP "\(bu" 4 -\fBlz4\fR shows real\-time notification statistics during compression or decompression of a single file (use \fB\-q\fR to silence them) -. -.IP "\(bu" 4 -If no destination name is provided, result is sent to \fBstdout\fR \fIexcept if stdout is the console\fR\. +\fBlz4\fR preserves original files . .IP "\(bu" 4 -If no destination name is provided, \fBand\fR if \fBstdout\fR is the console, \fBfile\fR is compressed into \fBfile\.lz4\fR\. +\fBlz4\fR shows real\-time notification statistics during compression or decompression of a single file (use \fB\-q\fR to silence them) . .IP "\(bu" 4 -As a consequence of previous rules, note the following example : \fBlz4 file | consumer\fR sends compressed data to \fBconsumer\fR through \fBstdout\fR, hence it does \fInot\fR create \fBfile\.lz4\fR\. +When no destination is specified, result is sent on implicit output, which depends on \fBstdout\fR status\. When \fBstdout\fR \fIis Not the console\fR, it becomes the implicit output\. Otherwise, if \fBstdout\fR is the console, the implicit output is \fBfilename\.lz4\fR\. . .IP "\(bu" 4 -Another consequence of those rules is that to run \fBlz4\fR under \fBnohup\fR, you should provide a destination file: \fBnohup lz4 file file\.lz4\fR, because \fBnohup\fR writes the specified command\'s output to a file\. +It is considered bad practice to rely on implicit output in scripts\. because the script\'s environment may change\. Always use explicit output in scripts\. \fB\-c\fR ensures that output will be \fBstdout\fR\. Conversely, providing a destination name, or using \fB\-m\fR ensures that the output will be either the specified name, or \fBfilename\.lz4\fR respectively\. . .IP "" 0 . @@ -55,7 +49,7 @@ Another consequence of those rules is that to run \fBlz4\fR under \fBnohup\fR, y Default behaviors can be modified by opt\-in commands, detailed below\. . .IP "\(bu" 4 -\fBlz4 \-m\fR makes it possible to provide multiple input filenames, which will be compressed into files using suffix \fB\.lz4\fR\. Progress notifications are also disabled by default (use \fB\-v\fR to enable them)\. This mode has a behavior which more closely mimics \fBgzip\fR command line, with the main remaining difference being that source files are preserved by default\. +\fBlz4 \-m\fR makes it possible to provide multiple input filenames, which will be compressed into files using suffix \fB\.lz4\fR\. Progress notifications become disabled by default (use \fB\-v\fR to enable them)\. This mode has a behavior which more closely mimics \fBgzip\fR command line, with the main remaining difference being that source files are preserved by default\. . .IP "\(bu" 4 Similarly, \fBlz4 \-m \-d\fR can decompress multiple \fB*\.lz4\fR files\. @@ -111,6 +105,10 @@ Test the integrity of compressed \fB\.lz4\fR files\. The decompressed data is di \fB\-b#\fR Benchmark mode, using \fB#\fR compression level\. . +.TP +\fB\-\-list\fR +List mode\. Lists information about \.lz4 files\. Useful if compressed with \-\-content\-size flag\. +. .SS "Operation modifiers" . .TP @@ -145,7 +143,7 @@ Force write to standard output, even if it is the console\. . .TP \fB\-m\fR \fB\-\-multiple\fR -Multiple input files\. Compressed file names will be appended a \fB\.lz4\fR suffix\. This mode also reduces notification level\. \fBlz4 \-m\fR has a behavior equivalent to \fBgzip \-k\fR (it preserves source files by default)\. +Multiple input files\. Compressed file names will be appended a \fB\.lz4\fR suffix\. This mode also reduces notification level\. Can also be used to list multiple files\. \fBlz4 \-m\fR has a behavior equivalent to \fBgzip \-k\fR (it preserves source files by default)\. . .TP \fB\-r\fR diff --git a/programs/lz4io.c b/programs/lz4io.c index 1e6efe1..e3eed93 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -1275,87 +1275,105 @@ typedef struct { LZ4F_frameInfo_t frameInfo; const char* fileName; unsigned long long fileSize; -} LZ4F_compFileInfo_t; - -#define LZ4F_INIT_FILEINFO { LZ4F_INIT_FRAMEINFO, NULL, 0ULL } - - -static int LZ4IO_getCompressedFileInfo(const char* input_filename, LZ4F_compFileInfo_t* cfinfo){ - const char *b, - *e; - char *t; - stat_t statbuf; - size_t readSize = LZ4F_HEADER_SIZE_MAX; - LZ4F_errorCode_t errorCode; - dRess_t ress; - /* Open file */ - FILE* const finput = LZ4IO_openSrcFile(input_filename); - if (finput==NULL) return 1; - - /* Get file size */ - if (!UTIL_getFileStat(input_filename, &statbuf)){ - EXM_THROW(60, "Can't stat file : %s", input_filename); - } - - cfinfo->fileSize = (unsigned long long)statbuf.st_size; - - /* Get basename without extension */ - b = strrchr(input_filename, '/'); - if (!b){ - b = strrchr(input_filename, '\\'); - } - if (b && b != input_filename){ - b++; - } else{ - b=input_filename; - } - e = strrchr(b, '.'); - - /* Allocate Memory */ - t = (char*)malloc( (size_t)(e-b+1) * sizeof(char)); - ress.srcBuffer = malloc(LZ4IO_dBufferSize); - if (!t || !ress.srcBuffer) - EXM_THROW(21, "Allocation error : not enough memory"); - strncpy(t, b, (e-b)); - t[e-b] = '\0'; - cfinfo->fileName = t; - - /* init */ - errorCode = LZ4F_createDecompressionContext(&ress.dCtx, LZ4F_VERSION); - if (LZ4F_isError(errorCode)) EXM_THROW(60, "Can't create LZ4F context : %s", LZ4F_getErrorName(errorCode)); - - if (!fread(ress.srcBuffer, readSize, 1, finput)){ - EXM_THROW(30, "Error reading %s ", input_filename); - } - LZ4F_getFrameInfo(ress.dCtx, &cfinfo->frameInfo, ress.srcBuffer, &readSize); - - /* Close input/free resources */ - fclose(finput); - free(ress.srcBuffer); - return 0; -} +} LZ4IO_cFileInfo_t; + +#define LZ4IO_INIT_CFILEINFO { LZ4F_INIT_FRAMEINFO, NULL, 0ULL } + + +/* This function is limited, + * it only works fine for a file consisting of a single valid frame. + * It will / may stop program execution if a single filename is wrong. + * It will not look at content beyond first frame header. + * + * Things to improve : + * - continue execution after an error, just report an error code, keep all memory clean + * - check the entire file for additional content after first frame + * + combine results from multiple frames, give total + * - Optional : + * + report nb of blocks, hence max. possible decompressed size (when not reported in header) + * + report block type (B4D, B7I, etc.) + */ +static int +LZ4IO_getCompressedFileInfo(LZ4IO_cFileInfo_t* cfinfo, const char* input_filename) +{ + /* Get file size */ + cfinfo->fileSize = UTIL_getFileSize(input_filename); /* returns 0 if cannot read information */ -int LZ4IO_displayCompressedFilesInfo(const char** inFileNames, const size_t ifnIdx){ - size_t idx; - int op_result=0; - double ratio; - DISPLAY("%16s\t%-20s\t%-20s\t%-10s\t%s\n","BlockChecksumFlag","Compressed", "Uncompressed", "Ratio", "Filename"); - for(idx=0; idxfileName = b; } - if(cfinfo.frameInfo.contentSize){ - ratio = (double)cfinfo.fileSize / cfinfo.frameInfo.contentSize; - DISPLAY("%-16d\t%-20llu\t%-20llu\t%-8.4f\t%s\n",cfinfo.frameInfo.blockChecksumFlag,cfinfo.fileSize,cfinfo.frameInfo.contentSize, ratio, cfinfo.fileName); + + /* Read file and extract header */ + { size_t readSize = LZ4F_HEADER_SIZE_MAX; + void* buffer = malloc(readSize); + LZ4F_dctx* dctx; + + if (!buffer) EXM_THROW(21, "Allocation error : not enough memory"); + { LZ4F_errorCode_t const errorCode = + LZ4F_createDecompressionContext(&dctx, LZ4F_VERSION); + if (LZ4F_isError(errorCode)) + EXM_THROW(60, "Can't create LZ4F context : %s", + LZ4F_getErrorName(errorCode)); + } + + { FILE* const finput = LZ4IO_openSrcFile(input_filename); + if (finput==NULL) return 1; + if (!fread(buffer, readSize, 1, finput)) { + EXM_THROW(30, "Error reading %s ", input_filename); + } + fclose(finput); + } + + { LZ4F_errorCode_t const errorCode = + LZ4F_getFrameInfo(dctx, &cfinfo->frameInfo, buffer, &readSize); + if (LZ4F_isError(errorCode)) + EXM_THROW(60, "Cannot interpret LZ4 frame : %s", + LZ4F_getErrorName(errorCode)); + } + + /* clean */ + free(buffer); + LZ4F_freeDecompressionContext(dctx); } - else{ - DISPLAY("%-16d\t%-20llu\t%-20s\t%-10s\t%s\n",cfinfo.frameInfo.blockChecksumFlag,cfinfo.fileSize, "-", "-", cfinfo.fileName); + + return 0; +} + +int LZ4IO_displayCompressedFilesInfo(const char** inFileNames, const size_t ifnIdx) +{ + size_t idx; + + DISPLAY("%16s\t%-20s\t%-20s\t%-10s\t%s\n", + "BlockChecksumFlag","Compressed", "Uncompressed", "Ratio", "Filename"); + + for (idx=0; idx> tmp-tlb-hw.lz4 $(LZ4) -f tmp-tlb-hw.lz4 # uncompress valid frame followed by invalid data $(LZ4) -BX tmp-tlb-hw -c -q | $(LZ4) -tv # test block checksum -- cgit v1.2.3 From 5e6807fd95284fcef48a976e2a28617f8ff134d7 Mon Sep 17 00:00:00 2001 From: JPeterMugaas Date: Mon, 22 Apr 2019 17:38:43 -0400 Subject: Make programs/Makefile use the includes. --- programs/Makefile | 43 +------------------------------------------ 1 file changed, 1 insertion(+), 42 deletions(-) diff --git a/programs/Makefile b/programs/Makefile index 47912d8..e055491 100644 --- a/programs/Makefile +++ b/programs/Makefile @@ -56,48 +56,7 @@ LZ4_VERSION=$(LIBVER) MD2ROFF = ronn MD2ROFF_FLAGS = --roff --warnings --manual="User Commands" --organization="lz4 $(LZ4_VERSION)" -TARGET_OS ?= $(shell uname) -ifeq ($(TARGET_OS),) - TARGET_OS ?= $(OS) -endif - -# Define *.exe as extension for Windows systems -ifneq (,$(filter Windows%,$(TARGET_OS))) -EXT :=.exe -else -ifneq (,$(filter MINGW%,$(TARGET_OS))) -EXT :=.exe -else -ifneq (,$(filter MSYS%,$(TARGET_OS))) -EXT :=.exe -else -ifneq (,$(filter CYGWIN%,$(TARGET_OS))) -EXT :=.exe -else -EXT := -endif -endif -endif -endif - -#determine if dev/nul based on host environment -ifneq (,$(filter MINGW%,$(shell uname))) -VOID := /dev/null -else -ifneq (,$(filter MSYS%,$(shell uname))) -VOID := /dev/null -else -ifneq (,$(filter CYGWIN%,$(shell uname))) -VOID := /dev/null -else -ifneq (,$(filter Windows%,$(OS))) -VOID := nul -else -VOID := /dev/null -endif -endif -endif -endif +include ../Makefile.inc default: lz4-release -- cgit v1.2.3 From f401f1de7eba98ee5acde5b8dd9681ff1aa76b56 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 22 Apr 2019 15:01:20 -0700 Subject: --list can resume after a failed file --- programs/lz4io.c | 103 ++++++++++++++++++++++++++++++++----------------------- programs/lz4io.h | 2 +- 2 files changed, 61 insertions(+), 44 deletions(-) diff --git a/programs/lz4io.c b/programs/lz4io.c index e3eed93..2a7b4b9 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -1230,7 +1230,9 @@ int LZ4IO_decompressFilename(LZ4IO_prefs_t* const prefs, const char* input_filen } -int LZ4IO_decompressMultipleFilenames(LZ4IO_prefs_t* const prefs, const char** inFileNamesTable, int ifntSize, const char* suffix) +int LZ4IO_decompressMultipleFilenames(LZ4IO_prefs_t* const prefs, + const char** inFileNamesTable, int ifntSize, + const char* suffix) { int i; int skippedFiles = 0; @@ -1250,7 +1252,12 @@ int LZ4IO_decompressMultipleFilenames(LZ4IO_prefs_t* const prefs, const char** i missingFiles += LZ4IO_decompressSrcFile(prefs, ress, inFileNamesTable[i], stdoutmark); continue; } - if (ofnSize <= ifnSize-suffixSize+1) { free(outFileName); ofnSize = ifnSize + 20; outFileName = (char*)malloc(ofnSize); if (outFileName==NULL) return ifntSize; } + if (ofnSize <= ifnSize-suffixSize+1) { + free(outFileName); + ofnSize = ifnSize + 20; + outFileName = (char*)malloc(ofnSize); + if (outFileName==NULL) return ifntSize; + } if (ifnSize <= suffixSize || strcmp(suffixPtr, suffix) != 0) { DISPLAYLEVEL(1, "File extension doesn't match expected LZ4_EXTENSION (%4s); will not process file: %s\n", suffix, inFileNamesTable[i]); skippedFiles++; @@ -1279,6 +1286,13 @@ typedef struct { #define LZ4IO_INIT_CFILEINFO { LZ4F_INIT_FRAMEINFO, NULL, 0ULL } +#define CHECK_Z_THROW(f) { \ + LZ4F_errorCode_t const ec = (f); \ + if (LZ4F_isError(ec)) \ + EXM_THROW(1, "LZ4F error : %s", LZ4F_getErrorName(ec)); \ +} + +typedef enum { LZ4IO_LZ4F_OK, LZ4IO_format_not_known, LZ4IO_not_a_file } LZ4IO_infoResult; /* This function is limited, * it only works fine for a file consisting of a single valid frame. @@ -1286,17 +1300,18 @@ typedef struct { * It will not look at content beyond first frame header. * * Things to improve : - * - continue execution after an error, just report an error code, keep all memory clean * - check the entire file for additional content after first frame * + combine results from multiple frames, give total * - Optional : * + report nb of blocks, hence max. possible decompressed size (when not reported in header) * + report block type (B4D, B7I, etc.) */ -static int +static LZ4IO_infoResult LZ4IO_getCompressedFileInfo(LZ4IO_cFileInfo_t* cfinfo, const char* input_filename) { - /* Get file size */ + LZ4IO_infoResult result = LZ4IO_format_not_known; /* default result (error) */ + + if (!UTIL_isRegFile(input_filename)) return LZ4IO_not_a_file; cfinfo->fileSize = UTIL_getFileSize(input_filename); /* returns 0 if cannot read information */ /* Get filename without path prefix */ @@ -1313,67 +1328,69 @@ LZ4IO_getCompressedFileInfo(LZ4IO_cFileInfo_t* cfinfo, const char* input_filenam } /* Read file and extract header */ - { size_t readSize = LZ4F_HEADER_SIZE_MAX; - void* buffer = malloc(readSize); - LZ4F_dctx* dctx; + { size_t const hSize = LZ4F_HEADER_SIZE_MAX; + size_t readSize=0; + void* const buffer = malloc(hSize); if (!buffer) EXM_THROW(21, "Allocation error : not enough memory"); - { LZ4F_errorCode_t const errorCode = - LZ4F_createDecompressionContext(&dctx, LZ4F_VERSION); - if (LZ4F_isError(errorCode)) - EXM_THROW(60, "Can't create LZ4F context : %s", - LZ4F_getErrorName(errorCode)); - } - { FILE* const finput = LZ4IO_openSrcFile(input_filename); - if (finput==NULL) return 1; - if (!fread(buffer, readSize, 1, finput)) { - EXM_THROW(30, "Error reading %s ", input_filename); - } - fclose(finput); - } + if (finput) { + readSize = fread(buffer, 1, hSize, finput); + fclose(finput); + } } - { LZ4F_errorCode_t const errorCode = - LZ4F_getFrameInfo(dctx, &cfinfo->frameInfo, buffer, &readSize); - if (LZ4F_isError(errorCode)) - EXM_THROW(60, "Cannot interpret LZ4 frame : %s", - LZ4F_getErrorName(errorCode)); + if (readSize > 0) { + LZ4F_dctx* dctx; + CHECK_Z_THROW(LZ4F_createDecompressionContext(&dctx, LZ4F_VERSION)); + if (!LZ4F_isError(LZ4F_getFrameInfo(dctx, &cfinfo->frameInfo, buffer, &readSize))) { + result = LZ4IO_LZ4F_OK; + } + LZ4F_freeDecompressionContext(dctx); } /* clean */ free(buffer); - LZ4F_freeDecompressionContext(dctx); } - return 0; + return result; } -int LZ4IO_displayCompressedFilesInfo(const char** inFileNames, const size_t ifnIdx) +int LZ4IO_displayCompressedFilesInfo(const char** inFileNames, size_t ifnIdx) { + int result = 0; size_t idx; - DISPLAY("%16s\t%-20s\t%-20s\t%-10s\t%s\n", - "BlockChecksumFlag","Compressed", "Uncompressed", "Ratio", "Filename"); + DISPLAY("%20s %20s %10s %7s %s\n", + "Compressed", "Uncompressed", "Ratio", "Check", "Filename"); for (idx=0; idx Date: Mon, 22 Apr 2019 15:14:53 -0700 Subject: --list gives block type --- programs/lz4.1 | 2 +- programs/lz4.1.md | 5 ++--- programs/lz4io.c | 26 +++++++++++++++++++++----- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/programs/lz4.1 b/programs/lz4.1 index eb82b68..ad0c12c 100644 --- a/programs/lz4.1 +++ b/programs/lz4.1 @@ -107,7 +107,7 @@ Benchmark mode, using \fB#\fR compression level\. . .TP \fB\-\-list\fR -List mode\. Lists information about \.lz4 files\. Useful if compressed with \-\-content\-size flag\. +List information about \.lz4 files\. note : current implementation is limited to single\-frame \.lz4 files\. . .SS "Operation modifiers" . diff --git a/programs/lz4.1.md b/programs/lz4.1.md index 62f672e..8874467 100644 --- a/programs/lz4.1.md +++ b/programs/lz4.1.md @@ -114,9 +114,8 @@ only the latest one will be applied. Benchmark mode, using `#` compression level. * `--list`: - List mode. - Lists information about .lz4 files. - Useful if compressed with --content-size flag. + List information about .lz4 files. + note : current implementation is limited to single-frame .lz4 files. ### Operation modifiers diff --git a/programs/lz4io.c b/programs/lz4io.c index 2a7b4b9..5393cd0 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -1304,7 +1304,6 @@ typedef enum { LZ4IO_LZ4F_OK, LZ4IO_format_not_known, LZ4IO_not_a_file } LZ4IO_i * + combine results from multiple frames, give total * - Optional : * + report nb of blocks, hence max. possible decompressed size (when not reported in header) - * + report block type (B4D, B7I, etc.) */ static LZ4IO_infoResult LZ4IO_getCompressedFileInfo(LZ4IO_cFileInfo_t* cfinfo, const char* input_filename) @@ -1355,13 +1354,26 @@ LZ4IO_getCompressedFileInfo(LZ4IO_cFileInfo_t* cfinfo, const char* input_filenam return result; } + +/* buffer : must be a valid memory area of at least 4 bytes */ +const char* LZ4IO_blockTypeID(int sizeID, int blockMode, char* buffer) +{ + buffer[0] = 'B'; + assert(sizeID >= 4); assert(sizeID <=7); + buffer[1] = (char)(sizeID + '0'); + buffer[2] = (blockMode == LZ4F_blockIndependent) ? 'I' : 'D'; + buffer[3] = 0; + return buffer; +} + + int LZ4IO_displayCompressedFilesInfo(const char** inFileNames, size_t ifnIdx) { int result = 0; size_t idx; - DISPLAY("%20s %20s %10s %7s %s\n", - "Compressed", "Uncompressed", "Ratio", "Check", "Filename"); + DISPLAY("%5s %20s %20s %10s %7s %s\n", + "Block", "Compressed", "Uncompressed", "Ratio", "Check", "Filename"); for (idx=0; idx Date: Mon, 22 Apr 2019 15:24:44 -0700 Subject: updated code comments --- programs/lz4io.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/programs/lz4io.c b/programs/lz4io.c index 5393cd0..105718d 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -1295,9 +1295,9 @@ typedef struct { typedef enum { LZ4IO_LZ4F_OK, LZ4IO_format_not_known, LZ4IO_not_a_file } LZ4IO_infoResult; /* This function is limited, - * it only works fine for a file consisting of a single valid frame. - * It will / may stop program execution if a single filename is wrong. + * it only works fine for a file consisting of a single valid frame using LZ4 Frame specification. * It will not look at content beyond first frame header. + * It's also unable to parse legacy frames, nor skippable ones. * * Things to improve : * - check the entire file for additional content after first frame @@ -1311,7 +1311,7 @@ LZ4IO_getCompressedFileInfo(LZ4IO_cFileInfo_t* cfinfo, const char* input_filenam LZ4IO_infoResult result = LZ4IO_format_not_known; /* default result (error) */ if (!UTIL_isRegFile(input_filename)) return LZ4IO_not_a_file; - cfinfo->fileSize = UTIL_getFileSize(input_filename); /* returns 0 if cannot read information */ + cfinfo->fileSize = UTIL_getFileSize(input_filename); /* Get filename without path prefix */ { const char* b = strrchr(input_filename, '/'); @@ -1329,9 +1329,10 @@ LZ4IO_getCompressedFileInfo(LZ4IO_cFileInfo_t* cfinfo, const char* input_filenam /* Read file and extract header */ { size_t const hSize = LZ4F_HEADER_SIZE_MAX; size_t readSize=0; - void* const buffer = malloc(hSize); + void* const buffer = malloc(hSize); if (!buffer) EXM_THROW(21, "Allocation error : not enough memory"); + { FILE* const finput = LZ4IO_openSrcFile(input_filename); if (finput) { readSize = fread(buffer, 1, hSize, finput); -- cgit v1.2.3 From 35b83a921f8030ee9b71cbb7324dc7aafaeb9878 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 22 Apr 2019 16:06:22 -0700 Subject: fix: no leak when LZ4F_dctx creation fails strange, because it previous implementation, it would `exit()`, so it should not matter ... --- programs/lz4io.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/programs/lz4io.c b/programs/lz4io.c index 105718d..960c451 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -1286,11 +1286,6 @@ typedef struct { #define LZ4IO_INIT_CFILEINFO { LZ4F_INIT_FRAMEINFO, NULL, 0ULL } -#define CHECK_Z_THROW(f) { \ - LZ4F_errorCode_t const ec = (f); \ - if (LZ4F_isError(ec)) \ - EXM_THROW(1, "LZ4F error : %s", LZ4F_getErrorName(ec)); \ -} typedef enum { LZ4IO_LZ4F_OK, LZ4IO_format_not_known, LZ4IO_not_a_file } LZ4IO_infoResult; @@ -1341,10 +1336,10 @@ LZ4IO_getCompressedFileInfo(LZ4IO_cFileInfo_t* cfinfo, const char* input_filenam if (readSize > 0) { LZ4F_dctx* dctx; - CHECK_Z_THROW(LZ4F_createDecompressionContext(&dctx, LZ4F_VERSION)); - if (!LZ4F_isError(LZ4F_getFrameInfo(dctx, &cfinfo->frameInfo, buffer, &readSize))) { - result = LZ4IO_LZ4F_OK; - } + if (!LZ4F_isError(LZ4F_createDecompressionContext(&dctx, LZ4F_VERSION))) { + if (!LZ4F_isError(LZ4F_getFrameInfo(dctx, &cfinfo->frameInfo, buffer, &readSize))) { + result = LZ4IO_LZ4F_OK; + } } LZ4F_freeDecompressionContext(dctx); } -- cgit v1.2.3 From 1e700b6f85bbc9e7052190f2f686041367321766 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 22 Apr 2019 17:45:24 -0700 Subject: updated NEWS for v1.9.1 --- NEWS | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/NEWS b/NEWS index 7722d73..860f15b 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,8 @@ v1.9.1 -fix : decompression functions were reading beyond input size (introduced in v1.9.0, reported by @ppodolsky and @danlark1) +fix : decompression functions were reading a few bytes beyond input size (introduced in v1.9.0, reported by @ppodolsky and @danlark1) api : fix : lz4frame initializers compatibility with c++, reported by @degski +cli : added command --list, based on a patch by @gabrielstedman +build: improved Windows build, by @JPeterMugaas build: AIX, by Norman Green v1.9.0 -- cgit v1.2.3 From 7136489d336fd2e584eb13cb858983caac5ad2e8 Mon Sep 17 00:00:00 2001 From: JPeterMugaas Date: Tue, 23 Apr 2019 07:44:00 -0400 Subject: More build imrpvements Moved a few other tests to Makefiles.inc. Other things might need to go there. Made a test for symlink appropriateness. Windows can NOT handle them the same way Unix-like operating systems do (if at all). This is mostly the same as the Visual C projects. embed version info into .dll and .exes that are redistributed. --- Makefile.inc | 50 +++++++++++++++++++++++++-------- lib/Makefile | 75 ++++++++++++++++++++++++++------------------------ lib/liblz4-dll.rc.in | 35 +++++++++++++++++++++++ programs/Makefile | 53 +++++++++++++++++++++-------------- programs/lz4-exe.rc.in | 27 ++++++++++++++++++ tests/Makefile | 2 +- 6 files changed, 173 insertions(+), 69 deletions(-) create mode 100644 lib/liblz4-dll.rc.in create mode 100644 programs/lz4-exe.rc.in diff --git a/Makefile.inc b/Makefile.inc index c09ba62..2d64405 100644 --- a/Makefile.inc +++ b/Makefile.inc @@ -1,3 +1,9 @@ +ifeq ($(V), 1) +Q = +else +Q = @ +endif + TARGET_OS ?= $(shell uname) ifeq ($(TARGET_OS),) TARGET_OS ?= $(OS) @@ -31,24 +37,17 @@ endif ifeq ($(WINBASED),yes) EXT = .exe +WINDRES = windres endif #determine if dev/nul based on host environment -ifneq (,$(filter MINGW%,$(shell uname))) +ifneq (,$(filter MINGW% MSYS% CYGWIN%,$(shell uname))) VOID := /dev/null else - ifneq (,$(filter MSYS%,$(shell uname))) -VOID := /dev/null - else - ifneq (,$(filter CYGWIN%,$(shell uname))) -VOID := /dev/null - else - ifneq (,$(filter Windows%,$(OS))) + ifneq (,$(filter Windows%,$(OS))) VOID := nul - else + else VOID := /dev/null - endif - endif endif endif @@ -57,3 +56,32 @@ POSIX_ENV = Yes else POSIX_ENV = No endif + +# Avoid symlinks when targetting Windows or building on a Windows host +ifeq ($(WINBASED),yes) +LN_S = cp -p +LN_SF = cp -p +else + ifneq (,$(filter MINGW% MSYS% CYGWIN%,$(shell uname))) +LN_S = cp -p +LN_SF = cp -p + else + ifneq (,$(filter Windows%,$(OS))) +LN_S = cp -p +LN_SF = cp -p + else +LN_S = ln -s +LN_SF = ln -sf + endif + endif +endif + +ifneq (,$(filter $(shell uname),SunOS)) +INSTALL ?= ginstall +else +INSTALL ?= install +endif + +INSTALL_PROGRAM ?= $(INSTALL) -m 755 +INSTALL_DATA ?= $(INSTALL) -m 644 +INSTALL_DIR ?= $(INSTALL) -d -m 755 diff --git a/lib/Makefile b/lib/Makefile index 28853df..330642a 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -85,12 +85,6 @@ all: lib all32: CFLAGS+=-m32 all32: all -ifeq ($(V), 1) -Q = -else -Q = @ -endif - liblz4.a: $(SRCFILES) ifeq ($(BUILD_STATIC),yes) # can be disabled on command line @echo compiling static library @@ -98,17 +92,32 @@ ifeq ($(BUILD_STATIC),yes) # can be disabled on command line $(Q)$(AR) rcs $@ *.o endif +ifeq ($(WINBASED),yes) +liblz4-dll.rc: liblz4-dll.rc.in + @echo creating library resource + $(Q)sed -e 's|@LIBLZ4@|$(LIBLZ4)|' \ + -e 's|@LIBVER_MAJOR@|$(LIBVER_MAJOR)|g' \ + -e 's|@LIBVER_MINOR@|$(LIBVER_MINOR)|g' \ + -e 's|@LIBVER_PATCH@|$(LIBVER_PATCH)|g' \ + $< >$@ + +liblz4-dll.o: liblz4-dll.rc + $(WINDRES) -i liblz4-dll.rc -o liblz4-dll.o + +$(LIBLZ4): $(SRCFILES) liblz4-dll.o +else $(LIBLZ4): $(SRCFILES) +endif ifeq ($(BUILD_SHARED),yes) # can be disabled on command line @echo compiling dynamic library $(LIBVER) -ifeq ($(WINBASED),yes) + ifeq ($(WINBASED),yes) $(Q)$(CC) $(FLAGS) -DLZ4_DLL_EXPORT=1 -shared $^ -o dll/$@.dll -Wl,--out-implib,dll/$(LIBLZ4_EXP) -else + else $(Q)$(CC) $(FLAGS) -shared $^ -fPIC -fvisibility=hidden $(SONAME_FLAGS) -o $@ @echo creating versioned links - $(Q)ln -sf $@ liblz4.$(SHARED_EXT_MAJOR) - $(Q)ln -sf $@ liblz4.$(SHARED_EXT) -endif + $(Q)$(LN_SF) $@ liblz4.$(SHARED_EXT_MAJOR) + $(Q)$(LN_SF) $@ liblz4.$(SHARED_EXT) + endif endif ifeq (,$(filter MINGW%,$(TARGET_OS))) @@ -116,6 +125,9 @@ liblz4: $(LIBLZ4) endif clean: +ifeq ($(WINBASED),yes) + $(Q)$(RM) *.rc +endif $(Q)$(RM) core *.o liblz4.pc dll/$(LIBLZ4).dll dll/$(LIBLZ4_EXP) $(Q)$(RM) *.a *.$(SHARED_EXT) *.$(SHARED_EXT_MAJOR) *.$(SHARED_EXT_VER) @echo Cleaning library completed @@ -144,22 +156,13 @@ libdir ?= $(LIBDIR) INCLUDEDIR ?= $(prefix)/include includedir ?= $(INCLUDEDIR) -ifneq (,$(filter $(OS),OpenBSD FreeBSD NetBSD DragonFly MidnightBSD)) + ifneq (,$(filter $(TARGET_OS),OpenBSD FreeBSD NetBSD DragonFly MidnightBSD)) PKGCONFIGDIR ?= $(prefix)/libdata/pkgconfig -else + else PKGCONFIGDIR ?= $(libdir)/pkgconfig -endif + endif pkgconfigdir ?= $(PKGCONFIGDIR) -ifneq (,$(filter $(OS),SunOS)) -INSTALL ?= ginstall -else -INSTALL ?= install -endif - -INSTALL_PROGRAM ?= $(INSTALL) -INSTALL_DATA ?= $(INSTALL) -m 644 - liblz4.pc: liblz4.pc.in Makefile @echo creating pkgconfig $(Q)sed -e 's|@PREFIX@|$(prefix)|' \ @@ -169,26 +172,26 @@ liblz4.pc: liblz4.pc.in Makefile $< >$@ install: lib liblz4.pc - $(Q)$(INSTALL) -d -m 755 $(DESTDIR)$(pkgconfigdir)/ $(DESTDIR)$(includedir)/ $(DESTDIR)$(libdir)/ $(DESTDIR)$(bindir)/ + $(Q)$(INSTALL_DIR) $(DESTDIR)$(pkgconfigdir)/ $(DESTDIR)$(includedir)/ $(DESTDIR)$(libdir)/ $(DESTDIR)$(bindir)/ $(Q)$(INSTALL_DATA) liblz4.pc $(DESTDIR)$(pkgconfigdir)/ @echo Installing libraries -ifeq ($(BUILD_STATIC),yes) + ifeq ($(BUILD_STATIC),yes) $(Q)$(INSTALL_DATA) liblz4.a $(DESTDIR)$(libdir)/liblz4.a $(Q)$(INSTALL_DATA) lz4frame_static.h $(DESTDIR)$(includedir)/lz4frame_static.h -endif -ifeq ($(BUILD_SHARED),yes) + endif + ifeq ($(BUILD_SHARED),yes) # Traditionnally, one installs the DLLs in the bin directory as programs # search them first in their directory. This allows to not pollute system # directories (like c:/windows/system32), nor modify the PATH variable. -ifeq ($(WINBASED),yes) + ifeq ($(WINBASED),yes) $(Q)$(INSTALL_PROGRAM) dll/$(LIBLZ4).dll $(DESTDIR)$(bindir) $(Q)$(INSTALL_PROGRAM) dll/$(LIBLZ4_EXP) $(DESTDIR)$(libdir) -else + else $(Q)$(INSTALL_PROGRAM) liblz4.$(SHARED_EXT_VER) $(DESTDIR)$(libdir) - $(Q)ln -sf liblz4.$(SHARED_EXT_VER) $(DESTDIR)$(libdir)/liblz4.$(SHARED_EXT_MAJOR) - $(Q)ln -sf liblz4.$(SHARED_EXT_VER) $(DESTDIR)$(libdir)/liblz4.$(SHARED_EXT) -endif -endif + $(Q)$(LN_SF) liblz4.$(SHARED_EXT_VER) $(DESTDIR)$(libdir)/liblz4.$(SHARED_EXT_MAJOR) + $(Q)$(LN_SF) liblz4.$(SHARED_EXT_VER) $(DESTDIR)$(libdir)/liblz4.$(SHARED_EXT) + endif + endif @echo Installing headers in $(includedir) $(Q)$(INSTALL_DATA) lz4.h $(DESTDIR)$(includedir)/lz4.h $(Q)$(INSTALL_DATA) lz4hc.h $(DESTDIR)$(includedir)/lz4hc.h @@ -197,14 +200,14 @@ endif uninstall: $(Q)$(RM) $(DESTDIR)$(pkgconfigdir)/liblz4.pc -ifeq (WINBASED,1) + ifeq (WINBASED,1) $(Q)$(RM) $(DESTDIR)$(bindir)/$(LIBLZ4).dll $(Q)$(RM) $(DESTDIR)$(libdir)/$(LIBLZ4_EXP) -else + else $(Q)$(RM) $(DESTDIR)$(libdir)/liblz4.$(SHARED_EXT) $(Q)$(RM) $(DESTDIR)$(libdir)/liblz4.$(SHARED_EXT_MAJOR) $(Q)$(RM) $(DESTDIR)$(libdir)/liblz4.$(SHARED_EXT_VER) -endif + endif $(Q)$(RM) $(DESTDIR)$(libdir)/liblz4.a $(Q)$(RM) $(DESTDIR)$(includedir)/lz4.h $(Q)$(RM) $(DESTDIR)$(includedir)/lz4hc.h diff --git a/lib/liblz4-dll.rc.in b/lib/liblz4-dll.rc.in new file mode 100644 index 0000000..bf9adf5 --- /dev/null +++ b/lib/liblz4-dll.rc.in @@ -0,0 +1,35 @@ +#include + +// DLL version information. +1 VERSIONINFO +FILEVERSION @LIBVER_MAJOR@,@LIBVER_MINOR@,@LIBVER_PATCH@,0 +PRODUCTVERSION @LIBVER_MAJOR@,@LIBVER_MINOR@,@LIBVER_PATCH@,0 +FILEFLAGSMASK VS_FFI_FILEFLAGSMASK +#ifdef _DEBUG + FILEFLAGS VS_FF_DEBUG | VS_FF_PRERELEASE +#else + FILEFLAGS 0 +#endif +FILEOS VOS_NT_WINDOWS32 +FILETYPE VFT_DLL +FILESUBTYPE VFT2_UNKNOWN +BEGIN + BLOCK "StringFileInfo" + BEGIN + BLOCK "040904B0" + BEGIN + VALUE "CompanyName", "Yann Collet" + VALUE "FileDescription", "Extremely fast compression" + VALUE "FileVersion", "@LIBVER_MAJOR@.@LIBVER_MINOR@.@LIBVER_PATCH@.0" + VALUE "InternalName", "@LIBLZ4@" + VALUE "LegalCopyright", "Copyright (C) 2013-2016, Yann Collet" + VALUE "OriginalFilename", "@LIBLZ4@.dll" + VALUE "ProductName", "LZ4" + VALUE "ProductVersion", "@LIBVER_MAJOR@.@LIBVER_MINOR@.@LIBVER_PATCH@.0" + END + END + BLOCK "VarFileInfo" + BEGIN + VALUE "Translation", 0x0409, 1200 + END +END diff --git a/programs/Makefile b/programs/Makefile index e055491..4994551 100644 --- a/programs/Makefile +++ b/programs/Makefile @@ -65,14 +65,32 @@ all: lz4 lz4c all32: CFLAGS+=-m32 all32: all +ifeq ($(WINBASED),yes) +lz4-exe.rc: lz4-exe.rc.in + @echo creating executable resource + $(Q)sed -e 's|@PROGNAME@|lz4|' \ + -e 's|@LIBVER_MAJOR@|$(LIBVER_MAJOR)|g' \ + -e 's|@LIBVER_MINOR@|$(LIBVER_MINOR)|g' \ + -e 's|@LIBVER_PATCH@|$(LIBVER_PATCH)|g' \ + -e 's|@EXT@|$(EXT)|g' \ + $< >$@ + +lz4-exe.o: lz4-exe.rc + $(WINDRES) -i lz4-exe.rc -o lz4-exe.o + +lz4: $(OBJFILES) lz4-exe.o + $(CC) $(FLAGS) $^ -o $@$(EXT) +else lz4: $(OBJFILES) $(CC) $(FLAGS) $^ -o $@$(EXT) +endif + lz4-release: DEBUGFLAGS= lz4-release: lz4 lz4c: lz4 - ln -s lz4$(EXT) lz4c$(EXT) + $(LN_SF) lz4$(EXT) lz4c$(EXT) lz4c32: CFLAGS += -m32 lz4c32 : $(SRCFILES) @@ -90,6 +108,9 @@ preview-man: clean-man man man ./lz4.1 clean: +ifeq ($(WINBASED),yes) + $(Q)$(RM) *.rc +endif @$(MAKE) -C $(LZ4DIR) $@ > $(VOID) @$(RM) core *.o *.test tmp* \ lz4$(EXT) lz4c$(EXT) lz4c32$(EXT) unlz4$(EXT) lz4cat$(EXT) @@ -99,13 +120,13 @@ clean: #----------------------------------------------------------------------------- # make install is validated only for Linux, OSX, BSD, Hurd and Solaris targets #----------------------------------------------------------------------------- -ifneq (,$(filter Linux Darwin GNU/kFreeBSD GNU OpenBSD FreeBSD NetBSD DragonFly SunOS Haiku MidnightBSD MINGW32_NT% MINGW64_NT% MSYS% CYGWIN_NT%,$(shell uname))) +ifeq ($(POSIX_ENV),Yes) unlz4: lz4 - ln -s lz4$(EXT) unlz4$(EXT) + $(LN_SF) lz4$(EXT) unlz4$(EXT) lz4cat: lz4 - ln -s lz4$(EXT) lz4cat$(EXT) + $(LN_SF) lz4$(EXT) lz4cat$(EXT) DESTDIR ?= # directory variables : GNU conventions prefer lowercase @@ -124,28 +145,18 @@ mandir ?= $(MANDIR) MAN1DIR ?= $(mandir)/man1 man1dir ?= $(MAN1DIR) -ifneq (,$(filter $(shell uname),SunOS)) -INSTALL ?= ginstall -else -INSTALL ?= install -endif - -INSTALL_PROGRAM ?= $(INSTALL) -m 755 -INSTALL_DATA ?= $(INSTALL) -m 644 - - install: lz4 @echo Installing binaries - @$(INSTALL) -d -m 755 $(DESTDIR)$(bindir)/ $(DESTDIR)$(man1dir)/ + @$(INSTALL_DIR) $(DESTDIR)$(bindir)/ $(DESTDIR)$(man1dir)/ @$(INSTALL_PROGRAM) lz4$(EXT) $(DESTDIR)$(bindir)/lz4$(EXT) - @ln -sf lz4$(EXT) $(DESTDIR)$(bindir)/lz4c$(EXT) - @ln -sf lz4$(EXT) $(DESTDIR)$(bindir)/lz4cat$(EXT) - @ln -sf lz4$(EXT) $(DESTDIR)$(bindir)/unlz4$(EXT) + @$(LN_S) lz4$(EXT) $(DESTDIR)$(bindir)/lz4c$(EXT) + @$(LN_S) lz4$(EXT) $(DESTDIR)$(bindir)/lz4cat$(EXT) + @$(LN_S) lz4$(EXT) $(DESTDIR)$(bindir)/unlz4$(EXT) @echo Installing man pages @$(INSTALL_DATA) lz4.1 $(DESTDIR)$(man1dir)/lz4.1 - @ln -sf lz4.1 $(DESTDIR)$(man1dir)/lz4c.1 - @ln -sf lz4.1 $(DESTDIR)$(man1dir)/lz4cat.1 - @ln -sf lz4.1 $(DESTDIR)$(man1dir)/unlz4.1 + @$(LN_SF) lz4.1 $(DESTDIR)$(man1dir)/lz4c.1 + @$(LN_SF) lz4.1 $(DESTDIR)$(man1dir)/lz4cat.1 + @$(LN_SF) lz4.1 $(DESTDIR)$(man1dir)/unlz4.1 @echo lz4 installation completed uninstall: diff --git a/programs/lz4-exe.rc.in b/programs/lz4-exe.rc.in new file mode 100644 index 0000000..7b81030 --- /dev/null +++ b/programs/lz4-exe.rc.in @@ -0,0 +1,27 @@ +1 VERSIONINFO +FILEVERSION @LIBVER_MAJOR@,@LIBVER_MINOR@,@LIBVER_PATCH@,0 +PRODUCTVERSION @LIBVER_MAJOR@,@LIBVER_MINOR@,@LIBVER_PATCH@,0 +FILEFLAGSMASK 0 +FILEOS 0x40000 +FILETYPE 1 +{ + BLOCK "StringFileInfo" + { + BLOCK "040904B0" + { + VALUE "CompanyName", "Yann Collet" + VALUE "FileDescription", "Extremely fast compression" + VALUE "FileVersion", "@LIBVER_MAJOR@.@LIBVER_MINOR@.@LIBVER_PATCH@.0" + VALUE "InternalName", "@PROGNAME@" + VALUE "LegalCopyright", "Copyright (C) 2013-2016, Yann Collet" + VALUE "OriginalFilename", "@PROGNAME@.@EXT@" + VALUE "ProductName", "LZ4" + VALUE "ProductVersion", "@LIBVER_MAJOR@.@LIBVER_MINOR@.@LIBVER_PATCH@.0" + } + } + BLOCK "VarFileInfo" + { + VALUE "Translation", 0x0409, 1200 + } +} + diff --git a/tests/Makefile b/tests/Makefile index 02c2ab6..67514e4 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -67,7 +67,7 @@ lib liblz4.pc: $(MAKE) -C $(LZ4DIR) $@ CFLAGS="$(CFLAGS)" lz4c unlz4 lz4cat: lz4 - ln -sf $(LZ4) $(PRGDIR)/$@ + $(LN_SF) $(LZ4) $(PRGDIR)/$@ lz4c32: # create a 32-bits version for 32/64 interop tests $(MAKE) -C $(PRGDIR) $@ CFLAGS="-m32 $(CFLAGS)" -- cgit v1.2.3 From 024216ef7394b6411eeaa5b52d0cec9953a44249 Mon Sep 17 00:00:00 2001 From: Chongyu Zhu Date: Wed, 24 Apr 2019 03:13:19 +0800 Subject: lib/Makefile: Fix detection of `Darwin`. --- lib/Makefile | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/Makefile b/lib/Makefile index 330642a..8f21d3d 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -55,10 +55,11 @@ FLAGS = $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) SRCFILES := $(sort $(wildcard *.c)) +include ../Makefile.inc # OS X linker doesn't support -soname, and use different extension # see : https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/DynamicLibraries/100-Articles/DynamicLibraryDesignGuidelines.html -ifeq ($(OS), Darwin) +ifeq ($(TARGET_OS), Darwin) SHARED_EXT = dylib SHARED_EXT_MAJOR = $(LIBVER_MAJOR).$(SHARED_EXT) SHARED_EXT_VER = $(LIBVER).$(SHARED_EXT) @@ -70,8 +71,6 @@ else SHARED_EXT_VER = $(SHARED_EXT).$(LIBVER) endif -include ../Makefile.inc - .PHONY: default default: lib-release -- cgit v1.2.3 From 2b68501ece2596bb40b27ffca1d1d8b54282d2fc Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 23 Apr 2019 12:29:39 -0700 Subject: added library build test on Mac OS-X --- .travis.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 2065478..c684b6c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,8 +8,11 @@ matrix: os: osx compiler: clang script: + - make # test library build + - make clean - make -C tests test-lz4 MOREFLAGS='-Werror -Wconversion -Wno-sign-conversion' | tee # test scenario where `stdout` is not the console - - CFLAGS=-m32 make -C tests clean test-lz4-contentSize + - make clean + - CFLAGS=-m32 make -C tests test-lz4-contentSize # Container-based 12.04 LTS Server Edition 64 bit (doesn't support 32-bit includes) - name: (Precise) benchmark test -- cgit v1.2.3 From 7937e862554ae4cc8722dbf66f53c3da2259e0c5 Mon Sep 17 00:00:00 2001 From: "Dmitry V. Levin" Date: Tue, 23 Apr 2019 21:18:11 +0000 Subject: test-amalgamation: fix the list of prerequisites Add $(LZ4DIR)/lz4frame.c to the list of prerequisites as the rule uses that file. Fixes: b192c86b ("[amalgamation] lz4frame.c") --- tests/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Makefile b/tests/Makefile index 67514e4..bcaf603 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -157,7 +157,7 @@ test32: CFLAGS+=-m32 test32: test .PHONY: test-amalgamation -test-amalgamation: $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c +test-amalgamation: $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c $(LZ4DIR)/lz4frame.c cat $(LZ4DIR)/lz4.c > lz4_all.c cat $(LZ4DIR)/lz4hc.c >> lz4_all.c cat $(LZ4DIR)/lz4frame.c >> lz4_all.c -- cgit v1.2.3 From 10726d4c56a54393ff105009077124472f0a369c Mon Sep 17 00:00:00 2001 From: "Dmitry V. Levin" Date: Tue, 23 Apr 2019 21:18:11 +0000 Subject: test-amalgamation: use a single cat command Use the list of prerequisites instead of listing those files manually, this way they will never fall out of sync. Also update the amalgamation example to use a single cat command. Fixes: a7e8d394 ("[amalgamation] add test") Fixes: b192c86b ("[amalgamation] lz4frame.c") --- lib/README.md | 4 +--- tests/Makefile | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/README.md b/lib/README.md index b753195..cf1505f 100644 --- a/lib/README.md +++ b/lib/README.md @@ -74,9 +74,7 @@ The following build macro can be selected at compilation time : lz4 source code can be amalgamated into a single file. One can combine all source code into `lz4_all.c` by using following command: ``` -cat lz4.c > lz4_all.c -cat lz4hc.c >> lz4_all.c -cat lz4frame.c >> lz4_all.c +cat lz4.c lz4hc.c lz4frame.c > lz4_all.c ``` (`cat` file order is important) then compile `lz4_all.c`. All `*.h` files present in `/lib` remain necessary to compile `lz4_all.c`. diff --git a/tests/Makefile b/tests/Makefile index bcaf603..f327535 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -158,9 +158,7 @@ test32: test .PHONY: test-amalgamation test-amalgamation: $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c $(LZ4DIR)/lz4frame.c - cat $(LZ4DIR)/lz4.c > lz4_all.c - cat $(LZ4DIR)/lz4hc.c >> lz4_all.c - cat $(LZ4DIR)/lz4frame.c >> lz4_all.c + cat $^ > lz4_all.c $(CC) -I$(LZ4DIR) -c lz4_all.c $(RM) lz4_all.c -- cgit v1.2.3 From 8069d2ae6ff88fbaeff1c8d5bf1e4d3eedca1d3c Mon Sep 17 00:00:00 2001 From: "Dmitry V. Levin" Date: Tue, 23 Apr 2019 21:18:11 +0000 Subject: test-amalgamation: fix compilation options Use the same compilation options to compile lz4_all.c and other object files. Fixes: a7e8d394 ("[amalgamation] add test") --- tests/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Makefile b/tests/Makefile index f327535..36cb26a 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -159,7 +159,7 @@ test32: test .PHONY: test-amalgamation test-amalgamation: $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c $(LZ4DIR)/lz4frame.c cat $^ > lz4_all.c - $(CC) -I$(LZ4DIR) -c lz4_all.c + $(CC) $(CFLAGS) $(CPPFLAGS) -c lz4_all.c $(RM) lz4_all.c test-install: lz4 lib liblz4.pc -- cgit v1.2.3 From 0d3f85df651cf298e0a60c646e119e3e5fd90c72 Mon Sep 17 00:00:00 2001 From: "Dmitry V. Levin" Date: Tue, 23 Apr 2019 21:18:11 +0000 Subject: test-amalgamation: split the rule Change test-amalgamation to follow each-rule-makes-a-single-target idiom. Fixes: a7e8d394 ("[amalgamation] add test") --- tests/Makefile | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/tests/Makefile b/tests/Makefile index 36cb26a..8f0dfd3 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -114,7 +114,7 @@ clean: frametest$(EXT) frametest32$(EXT) \ fasttest$(EXT) roundTripTest$(EXT) \ datagen$(EXT) checkTag$(EXT) \ - frameTest$(EXT) + frameTest$(EXT) lz4_all.c @$(RM) -rf $(TESTDIR) @echo Cleaning completed @@ -156,11 +156,13 @@ test: test-lz4 test-lz4c test-frametest test-fullbench test-fuzzer test-install test32: CFLAGS+=-m32 test32: test -.PHONY: test-amalgamation -test-amalgamation: $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c $(LZ4DIR)/lz4frame.c - cat $^ > lz4_all.c - $(CC) $(CFLAGS) $(CPPFLAGS) -c lz4_all.c - $(RM) lz4_all.c +test-amalgamation: lz4_all.o + +lz4_all.o: lz4_all.c + $(CC) $(CFLAGS) $(CPPFLAGS) -c $^ -o $@ + +lz4_all.c: $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c $(LZ4DIR)/lz4frame.c + cat $^ > $@ test-install: lz4 lib liblz4.pc lz4_root=.. ./test_install.sh -- cgit v1.2.3 From ba99eac4d0f12a152426d95fd4fe49ff2cedd566 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 24 Apr 2019 10:03:02 -0700 Subject: several minor style changes recommended by clang-tidy --- lib/lz4.c | 137 +++++++++++++++++++++++++++++++++----------------------------- lib/lz4.h | 4 +- 2 files changed, 75 insertions(+), 66 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index e614c45..69143c0 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -606,9 +606,11 @@ int LZ4_sizeofState() { return LZ4_STREAMSIZE; } extern "C" { #endif -int LZ4_compress_forceExtDict (LZ4_stream_t* LZ4_stream, const char* source, char* dest, int inputSize); +int LZ4_compress_forceExtDict (LZ4_stream_t* LZ4_dict, const char* source, char* dest, int srcSize); -int LZ4_decompress_safe_forceExtDict(const char* in, char* out, int inSize, int outSize, const void* dict, size_t dictSize); +int LZ4_decompress_safe_forceExtDict(const char* source, char* dest, + int compressedSize, int maxOutputSize, + const void* dictStart, size_t dictSize); #if defined (__cplusplus) } @@ -811,9 +813,9 @@ LZ4_FORCE_INLINE int LZ4_compress_generic( DEBUGLOG(5, "LZ4_compress_generic: srcSize=%i, tableType=%u", inputSize, tableType); /* If init conditions are not met, we don't have to mark stream * as having dirty context, since no action was taken yet */ - if (outputDirective == fillOutput && maxOutputSize < 1) return 0; /* Impossible to store anything */ - if ((U32)inputSize > (U32)LZ4_MAX_INPUT_SIZE) return 0; /* Unsupported inputSize, too large (or negative) */ - if ((tableType == byU16) && (inputSize>=LZ4_64Klimit)) return 0; /* Size too large (not within 64K limit) */ + if (outputDirective == fillOutput && maxOutputSize < 1) { return 0; } /* Impossible to store anything */ + if ((U32)inputSize > (U32)LZ4_MAX_INPUT_SIZE) { return 0; } /* Unsupported inputSize, too large (or negative) */ + if ((tableType == byU16) && (inputSize>=LZ4_64Klimit)) { return 0; } /* Size too large (not within 64K limit) */ if (tableType==byPtr) assert(dictDirective==noDict); /* only supported use case with byPtr */ assert(acceleration >= 1); @@ -909,10 +911,10 @@ LZ4_FORCE_INLINE int LZ4_compress_generic( forwardH = LZ4_hashPosition(forwardIp, tableType); LZ4_putIndexOnHash(current, h, cctx->hashTable, tableType); - if ((dictIssue == dictSmall) && (matchIndex < prefixIdxLimit)) continue; /* match outside of valid area */ + if ((dictIssue == dictSmall) && (matchIndex < prefixIdxLimit)) { continue; } /* match outside of valid area */ assert(matchIndex < current); - if ((tableType != byU16) && (matchIndex+LZ4_DISTANCE_MAX < current)) continue; /* too far */ - if (tableType == byU16) assert((current - matchIndex) <= LZ4_DISTANCE_MAX); /* too_far presumed impossible with byU16 */ + if ((tableType != byU16) && (matchIndex+LZ4_DISTANCE_MAX < current)) { continue; } /* too far */ + if (tableType == byU16) { assert((current - matchIndex) <= LZ4_DISTANCE_MAX); } /* too_far presumed impossible with byU16 */ if (LZ4_read32(match) == LZ4_read32(ip)) { if (maybe_extMem) offset = current - matchIndex; @@ -929,9 +931,9 @@ LZ4_FORCE_INLINE int LZ4_compress_generic( { unsigned const litLength = (unsigned)(ip - anchor); token = op++; if ((outputDirective == limitedOutput) && /* Check output buffer overflow */ - (unlikely(op + litLength + (2 + 1 + LASTLITERALS) + (litLength/255) > olimit)) ) + (unlikely(op + litLength + (2 + 1 + LASTLITERALS) + (litLength/255) > olimit)) ) { return 0; /* cannot compress within `dst` budget. Stored indexes in hash table are nonetheless fine */ - + } if ((outputDirective == fillOutput) && (unlikely(op + (litLength+240)/255 /* litlen */ + litLength /* literals */ + 2 /* offset */ + 1 /* token */ + MFLIMIT - MINMATCH /* min last literals so last match is <= end - MFLIMIT */ > olimit))) { op--; @@ -1306,12 +1308,12 @@ static size_t LZ4_stream_t_alignment(void) LZ4_stream_t* LZ4_initStream (void* buffer, size_t size) { DEBUGLOG(5, "LZ4_initStream"); - if (buffer == NULL) return NULL; - if (size < sizeof(LZ4_stream_t)) return NULL; + if (buffer == NULL) { return NULL; } + if (size < sizeof(LZ4_stream_t)) { return NULL; } #ifndef _MSC_VER /* for some reason, Visual fails the aligment test on 32-bit x86 : it reports an aligment of 8-bytes, while actually aligning LZ4_stream_t on 4 bytes. */ - if (((size_t)buffer) & (LZ4_stream_t_alignment() - 1)) return NULL; /* alignment check */ + if (((size_t)buffer) & (LZ4_stream_t_alignment() - 1)) { return NULL; } /* alignment check */ #endif MEM_INIT(buffer, 0, sizeof(LZ4_stream_t)); return (LZ4_stream_t*)buffer; @@ -1381,25 +1383,25 @@ int LZ4_loadDict (LZ4_stream_t* LZ4_dict, const char* dictionary, int dictSize) return (int)dict->dictSize; } -void LZ4_attach_dictionary(LZ4_stream_t *working_stream, const LZ4_stream_t *dictionary_stream) { +void LZ4_attach_dictionary(LZ4_stream_t* workingStream, const LZ4_stream_t* dictionaryStream) { /* Calling LZ4_resetStream_fast() here makes sure that changes will not be * erased by subsequent calls to LZ4_resetStream_fast() in case stream was * marked as having dirty context, e.g. requiring full reset. */ - LZ4_resetStream_fast(working_stream); + LZ4_resetStream_fast(workingStream); - if (dictionary_stream != NULL) { + if (dictionaryStream != NULL) { /* If the current offset is zero, we will never look in the * external dictionary context, since there is no value a table * entry can take that indicate a miss. In that case, we need * to bump the offset to something non-zero. */ - if (working_stream->internal_donotuse.currentOffset == 0) { - working_stream->internal_donotuse.currentOffset = 64 KB; + if (workingStream->internal_donotuse.currentOffset == 0) { + workingStream->internal_donotuse.currentOffset = 64 KB; } - working_stream->internal_donotuse.dictCtx = &(dictionary_stream->internal_donotuse); + workingStream->internal_donotuse.dictCtx = &(dictionaryStream->internal_donotuse); } else { - working_stream->internal_donotuse.dictCtx = NULL; + workingStream->internal_donotuse.dictCtx = NULL; } } @@ -1435,7 +1437,7 @@ int LZ4_compress_fast_continue (LZ4_stream_t* LZ4_stream, DEBUGLOG(5, "LZ4_compress_fast_continue (inputSize=%i)", inputSize); - if (streamPtr->dirty) return 0; /* Uninitialized structure detected */ + if (streamPtr->dirty) { return 0; } /* Uninitialized structure detected */ LZ4_renormDictT(streamPtr, inputSize); /* avoid index overflow */ if (acceleration < 1) acceleration = ACCELERATION_DEFAULT; @@ -1532,8 +1534,8 @@ int LZ4_saveDict (LZ4_stream_t* LZ4_dict, char* safeBuffer, int dictSize) LZ4_stream_t_internal* const dict = &LZ4_dict->internal_donotuse; const BYTE* const previousDictEnd = dict->dictionary + dict->dictSize; - if ((U32)dictSize > 64 KB) dictSize = 64 KB; /* useless to define a dictionary > 64 KB */ - if ((U32)dictSize > dict->dictSize) dictSize = (int)dict->dictSize; + if ((U32)dictSize > 64 KB) { dictSize = 64 KB; } /* useless to define a dictionary > 64 KB */ + if ((U32)dictSize > dict->dictSize) { dictSize = (int)dict->dictSize; } memmove(safeBuffer, previousDictEnd - dictSize, dictSize); @@ -1607,7 +1609,7 @@ LZ4_decompress_generic( const size_t dictSize /* note : = 0 if noDict */ ) { - if (src == NULL) return -1; + if (src == NULL) { return -1; } { const BYTE* ip = (const BYTE*) src; const BYTE* const iend = ip + srcSize; @@ -1636,9 +1638,9 @@ LZ4_decompress_generic( /* Special cases */ assert(lowPrefix <= op); - if ((endOnInput) && (unlikely(outputSize==0))) return ((srcSize==1) && (*ip==0)) ? 0 : -1; /* Empty output buffer */ - if ((!endOnInput) && (unlikely(outputSize==0))) return (*ip==0 ? 1 : -1); - if ((endOnInput) && unlikely(srcSize==0)) return -1; + if ((endOnInput) && (unlikely(outputSize==0))) { return ((srcSize==1) && (*ip==0)) ? 0 : -1; } /* Empty output buffer */ + if ((!endOnInput) && (unlikely(outputSize==0))) { return (*ip==0 ? 1 : -1); } + if ((endOnInput) && unlikely(srcSize==0)) { return -1; } /* Currently the fast loop shows a regression on qualcomm arm chips. */ #if LZ4_FAST_DEC_LOOP @@ -1651,7 +1653,7 @@ LZ4_decompress_generic( while (1) { /* Main fastloop assertion: We can always wildcopy FASTLOOP_SAFE_DISTANCE */ assert(oend - op >= FASTLOOP_SAFE_DISTANCE); - if (endOnInput) assert(ip < iend); + if (endOnInput) { assert(ip < iend); } token = *ip++; length = token >> ML_BITS; /* literal length */ @@ -1661,18 +1663,18 @@ LZ4_decompress_generic( if (length == RUN_MASK) { variable_length_error error = ok; length += read_variable_length(&ip, iend-RUN_MASK, endOnInput, endOnInput, &error); - if (error == initial_error) goto _output_error; - if ((safeDecode) && unlikely((uptrval)(op)+length<(uptrval)(op))) goto _output_error; /* overflow detection */ - if ((safeDecode) && unlikely((uptrval)(ip)+length<(uptrval)(ip))) goto _output_error; /* overflow detection */ + if (error == initial_error) { goto _output_error; } + if ((safeDecode) && unlikely((uptrval)(op)+length<(uptrval)(op))) { goto _output_error; } /* overflow detection */ + if ((safeDecode) && unlikely((uptrval)(ip)+length<(uptrval)(ip))) { goto _output_error; } /* overflow detection */ /* copy literals */ cpy = op+length; LZ4_STATIC_ASSERT(MFLIMIT >= WILDCOPYLENGTH); if (endOnInput) { /* LZ4_decompress_safe() */ - if ((cpy>oend-32) || (ip+length>iend-32)) goto safe_literal_copy; + if ((cpy>oend-32) || (ip+length>iend-32)) { goto safe_literal_copy; } LZ4_wildCopy32(op, ip, cpy); } else { /* LZ4_decompress_fast() */ - if (cpy>oend-8) goto safe_literal_copy; + if (cpy>oend-8) { goto safe_literal_copy; } LZ4_wildCopy8(op, ip, cpy); /* LZ4_decompress_fast() cannot copy more than 8 bytes at a time : * it doesn't know input length, and only relies on end-of-block properties */ } @@ -1682,14 +1684,14 @@ LZ4_decompress_generic( if (endOnInput) { /* LZ4_decompress_safe() */ DEBUGLOG(7, "copy %u bytes in a 16-bytes stripe", (unsigned)length); /* We don't need to check oend, since we check it once for each loop below */ - if (ip > iend-(16 + 1/*max lit + offset + nextToken*/)) goto safe_literal_copy; + if (ip > iend-(16 + 1/*max lit + offset + nextToken*/)) { goto safe_literal_copy; } /* Literals can only be 14, but hope compilers optimize if we copy by a register size */ memcpy(op, ip, 16); } else { /* LZ4_decompress_fast() */ /* LZ4_decompress_fast() cannot copy more than 8 bytes at a time : * it doesn't know input length, and relies on end-of-block properties */ memcpy(op, ip, 8); - if (length > 8) memcpy(op+8, ip+8, 8); + if (length > 8) { memcpy(op+8, ip+8, 8); } } ip += length; op = cpy; } @@ -1701,13 +1703,13 @@ LZ4_decompress_generic( /* get matchlength */ length = token & ML_MASK; - if ((checkOffset) && (unlikely(match + dictSize < lowPrefix))) goto _output_error; /* Error : offset outside buffers */ + if ((checkOffset) && (unlikely(match + dictSize < lowPrefix))) { goto _output_error; } /* Error : offset outside buffers */ if (length == ML_MASK) { variable_length_error error = ok; length += read_variable_length(&ip, iend - LASTLITERALS + 1, endOnInput, 0, &error); - if (error != ok) goto _output_error; - if ((safeDecode) && unlikely((uptrval)(op)+length<(uptrval)op)) goto _output_error; /* overflow detection */ + if (error != ok) { goto _output_error; } + if ((safeDecode) && unlikely((uptrval)(op)+length<(uptrval)op)) { goto _output_error; } /* overflow detection */ length += MINMATCH; if (op + length >= oend - FASTLOOP_SAFE_DISTANCE) { goto safe_match_copy; @@ -1731,9 +1733,11 @@ LZ4_decompress_generic( /* match starting within external dictionary */ if ((dict==usingExtDict) && (match < lowPrefix)) { if (unlikely(op+length > oend-LASTLITERALS)) { - if (partialDecoding) length = MIN(length, (size_t)(oend-op)); - else goto _output_error; /* doesn't respect parsing restriction */ - } + if (partialDecoding) { + length = MIN(length, (size_t)(oend-op)); /* reach end of buffer */ + } else { + goto _output_error; /* end-of-block condition violated */ + } } if (length <= (size_t)(lowPrefix-match)) { /* match fits entirely within external dictionary : just copy */ @@ -1748,7 +1752,7 @@ LZ4_decompress_generic( if (restSize > (size_t)(op - lowPrefix)) { /* overlap copy */ BYTE* const endOfMatch = op + restSize; const BYTE* copyFrom = lowPrefix; - while (op < endOfMatch) *op++ = *copyFrom++; + while (op < endOfMatch) { *op++ = *copyFrom++; } } else { memcpy(op, lowPrefix, restSize); op += restSize; @@ -1821,11 +1825,11 @@ LZ4_decompress_generic( /* decode literal length */ if (length == RUN_MASK) { - variable_length_error error = ok; - length += read_variable_length(&ip, iend-RUN_MASK, endOnInput, endOnInput, &error); - if (error == initial_error) goto _output_error; - if ((safeDecode) && unlikely((uptrval)(op)+length<(uptrval)(op))) goto _output_error; /* overflow detection */ - if ((safeDecode) && unlikely((uptrval)(ip)+length<(uptrval)(ip))) goto _output_error; /* overflow detection */ + variable_length_error error = ok; + length += read_variable_length(&ip, iend-RUN_MASK, endOnInput, endOnInput, &error); + if (error == initial_error) { goto _output_error; } + if ((safeDecode) && unlikely((uptrval)(op)+length<(uptrval)(op))) { goto _output_error; } /* overflow detection */ + if ((safeDecode) && unlikely((uptrval)(ip)+length<(uptrval)(ip))) { goto _output_error; } /* overflow detection */ } /* copy literals */ @@ -1839,10 +1843,10 @@ LZ4_decompress_generic( { if (partialDecoding) { if (cpy > oend) { cpy = oend; assert(op<=oend); length = (size_t)(oend-op); } /* Partial decoding : stop in the middle of literal segment */ - if ((endOnInput) && (ip+length > iend)) goto _output_error; /* Error : read attempt beyond end of input buffer */ + if ((endOnInput) && (ip+length > iend)) { goto _output_error; } /* Error : read attempt beyond end of input buffer */ } else { - if ((!endOnInput) && (cpy != oend)) goto _output_error; /* Error : block decoding must stop exactly there */ - if ((endOnInput) && ((ip+length != iend) || (cpy > oend))) goto _output_error; /* Error : input must be consumed */ + if ((!endOnInput) && (cpy != oend)) { goto _output_error; } /* Error : block decoding must stop exactly there */ + if ((endOnInput) && ((ip+length != iend) || (cpy > oend))) { goto _output_error; } /* Error : input must be consumed */ } memcpy(op, ip, length); ip += length; @@ -1921,12 +1925,12 @@ LZ4_decompress_generic( const BYTE* const matchEnd = match + mlen; BYTE* const copyEnd = op + mlen; if (matchEnd > op) { /* overlap copy */ - while (op < copyEnd) *op++ = *match++; + while (op < copyEnd) { *op++ = *match++; } } else { memcpy(op, match, mlen); } op = copyEnd; - if (op==oend) break; + if (op == oend) { break; } continue; } @@ -1946,25 +1950,26 @@ LZ4_decompress_generic( if (unlikely(cpy > oend-MATCH_SAFEGUARD_DISTANCE)) { BYTE* const oCopyLimit = oend - (WILDCOPYLENGTH-1); - if (cpy > oend-LASTLITERALS) goto _output_error; /* Error : last LASTLITERALS bytes must be literals (uncompressed) */ + if (cpy > oend-LASTLITERALS) { goto _output_error; } /* Error : last LASTLITERALS bytes must be literals (uncompressed) */ if (op < oCopyLimit) { LZ4_wildCopy8(op, match, oCopyLimit); match += oCopyLimit - op; op = oCopyLimit; } - while (op < cpy) *op++ = *match++; + while (op < cpy) { *op++ = *match++; } } else { memcpy(op, match, 8); - if (length > 16) LZ4_wildCopy8(op+8, match+8, cpy); + if (length > 16) { LZ4_wildCopy8(op+8, match+8, cpy); } } op = cpy; /* wildcopy correction */ } /* end of decoding */ - if (endOnInput) + if (endOnInput) { return (int) (((char*)op)-dst); /* Nb of output bytes decoded */ - else + } else { return (int) (((const char*)ip)-src); /* Nb of input bytes read */ + } /* Overflow error detected */ _output_error: @@ -2079,7 +2084,7 @@ LZ4_streamDecode_t* LZ4_createStreamDecode(void) int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream) { - if (LZ4_stream == NULL) return 0; /* support free on NULL */ + if (LZ4_stream == NULL) { return 0; } /* support free on NULL */ FREEMEM(LZ4_stream); return 0; } @@ -2214,18 +2219,22 @@ int LZ4_decompress_safe_usingDict(const char* source, char* dest, int compressed if (dictSize==0) return LZ4_decompress_safe(source, dest, compressedSize, maxOutputSize); if (dictStart+dictSize == dest) { - if (dictSize >= 64 KB - 1) + if (dictSize >= 64 KB - 1) { return LZ4_decompress_safe_withPrefix64k(source, dest, compressedSize, maxOutputSize); - return LZ4_decompress_safe_withSmallPrefix(source, dest, compressedSize, maxOutputSize, dictSize); + } + assert(dictSize >= 0); + return LZ4_decompress_safe_withSmallPrefix(source, dest, compressedSize, maxOutputSize, (size_t)dictSize); } - return LZ4_decompress_safe_forceExtDict(source, dest, compressedSize, maxOutputSize, dictStart, dictSize); + assert(dictSize >= 0); + return LZ4_decompress_safe_forceExtDict(source, dest, compressedSize, maxOutputSize, dictStart, (size_t)dictSize); } int LZ4_decompress_fast_usingDict(const char* source, char* dest, int originalSize, const char* dictStart, int dictSize) { if (dictSize==0 || dictStart+dictSize == dest) return LZ4_decompress_fast(source, dest, originalSize); - return LZ4_decompress_fast_extDict(source, dest, originalSize, dictStart, dictSize); + assert(dictSize >= 0); + return LZ4_decompress_fast_extDict(source, dest, originalSize, dictStart, (size_t)dictSize); } @@ -2237,9 +2246,9 @@ int LZ4_compress_limitedOutput(const char* source, char* dest, int inputSize, in { return LZ4_compress_default(source, dest, inputSize, maxOutputSize); } -int LZ4_compress(const char* source, char* dest, int inputSize) +int LZ4_compress(const char* src, char* dest, int srcSize) { - return LZ4_compress_default(source, dest, inputSize, LZ4_compressBound(inputSize)); + return LZ4_compress_default(src, dest, srcSize, LZ4_compressBound(srcSize)); } int LZ4_compress_limitedOutput_withState (void* state, const char* src, char* dst, int srcSize, int dstSize) { diff --git a/lib/lz4.h b/lib/lz4.h index a9c932c..6064967 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -601,8 +601,8 @@ union LZ4_streamDecode_u { #endif /* LZ4_DISABLE_DEPRECATE_WARNINGS */ /* Obsolete compression functions */ -LZ4_DEPRECATED("use LZ4_compress_default() instead") LZ4LIB_API int LZ4_compress (const char* source, char* dest, int sourceSize); -LZ4_DEPRECATED("use LZ4_compress_default() instead") LZ4LIB_API int LZ4_compress_limitedOutput (const char* source, char* dest, int sourceSize, int maxOutputSize); +LZ4_DEPRECATED("use LZ4_compress_default() instead") LZ4LIB_API int LZ4_compress (const char* src, char* dest, int srcSize); +LZ4_DEPRECATED("use LZ4_compress_default() instead") LZ4LIB_API int LZ4_compress_limitedOutput (const char* src, char* dest, int srcSize, int maxOutputSize); LZ4_DEPRECATED("use LZ4_compress_fast_extState() instead") LZ4LIB_API int LZ4_compress_withState (void* state, const char* source, char* dest, int inputSize); LZ4_DEPRECATED("use LZ4_compress_fast_extState() instead") LZ4LIB_API int LZ4_compress_limitedOutput_withState (void* state, const char* source, char* dest, int inputSize, int maxOutputSize); LZ4_DEPRECATED("use LZ4_compress_fast_continue() instead") LZ4LIB_API int LZ4_compress_continue (LZ4_stream_t* LZ4_streamPtr, const char* source, char* dest, int inputSize); -- cgit v1.2.3 From 9e056bc032c1f1b425bbfc6034be75f595d8cffe Mon Sep 17 00:00:00 2001 From: Brenden Eng Date: Thu, 25 Apr 2019 22:37:39 -0400 Subject: Include block checksum in worst case scenario calculation of dstCapacity --- lib/lz4frame.c | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/lz4frame.c b/lib/lz4frame.c index a10e4af..f131d9a 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -327,6 +327,7 @@ static size_t LZ4F_compressBound_internal(size_t srcSize, { LZ4F_preferences_t prefsNull = LZ4F_INIT_PREFERENCES; prefsNull.frameInfo.contentChecksumFlag = LZ4F_contentChecksumEnabled; /* worst case */ + prefsNull.frameInfo.blockChecksumFlag = LZ4F_blockChecksumEnabled; /* worst case */ { const LZ4F_preferences_t* const prefsPtr = (preferencesPtr==NULL) ? &prefsNull : preferencesPtr; U32 const flush = prefsPtr->autoFlush | (srcSize==0); LZ4F_blockSizeID_t const blockID = prefsPtr->frameInfo.blockSizeID; -- cgit v1.2.3 From dd54ea01a207a65d4819db4222066373f6170113 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Sat, 27 Apr 2019 01:41:03 +0700 Subject: contrib: Make Meson build non optional * Update ninja version to 1.9 in CI. * Update default project version in meson script. --- .travis.yml | 6 +++--- contrib/meson/meson.build | 4 +++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index c684b6c..8ebcdfa 100644 --- a/.travis.yml +++ b/.travis.yml @@ -172,13 +172,13 @@ matrix: - tests/checkTag "$TRAVIS_BRANCH" - name: (Xenial) Meson + clang build - env: ALLOW_FAILURES=true + #env: ALLOW_FAILURES=true dist: xenial language: cpp compiler: clang install: - sudo apt-get install -qq python3 tree - - curl -o ~/ninja.zip -L 'https://github.com/ninja-build/ninja/releases/download/v1.8.2/ninja-linux.zip' + - curl -o ~/ninja.zip -L 'https://github.com/ninja-build/ninja/releases/download/v1.9.0/ninja-linux.zip' && unzip ~/ninja.zip -d ~/.local/bin - curl -o ~/get-pip.py 'https://bootstrap.pypa.io/get-pip.py' && python3 ~/get-pip.py --user @@ -191,7 +191,7 @@ matrix: -Ddefault_library=both -Dbuild_{programs,contrib,tests,examples}=true contrib/meson build - - cd build + - pushd build - DESTDIR=./staging ninja install - tree ./staging allow_failures: diff --git a/contrib/meson/meson.build b/contrib/meson/meson.build index bf30eae..7e364eb 100644 --- a/contrib/meson/meson.build +++ b/contrib/meson/meson.build @@ -11,7 +11,9 @@ project('lz4', ['c'], license: ['BSD', 'GPLv2'], default_options : ['c_std=c99', 'buildtype=release'], - version: '1.8.3', + # Fall-back version in case of extracting version number from + # `lz4.h` failed. + version: '1.9.1', meson_version: '>=0.47.0') cc = meson.get_compiler('c') -- cgit v1.2.3 From cb1be75c84e9b49c82634fe9be5f968087c98df8 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Tue, 30 Apr 2019 12:25:40 +0700 Subject: meson: Rely only on extracted version in lz4.h So now instead of warning when failing to extract version number from lz4.h, we error and stop the build instead. --- .travis.yml | 4 ++-- contrib/meson/meson.build | 13 ++++--------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/.travis.yml b/.travis.yml index 8ebcdfa..ee643e5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -178,9 +178,9 @@ matrix: compiler: clang install: - sudo apt-get install -qq python3 tree - - curl -o ~/ninja.zip -L 'https://github.com/ninja-build/ninja/releases/download/v1.9.0/ninja-linux.zip' + - travis_retry curl -o ~/ninja.zip -L 'https://github.com/ninja-build/ninja/releases/download/v1.9.0/ninja-linux.zip' && unzip ~/ninja.zip -d ~/.local/bin - - curl -o ~/get-pip.py 'https://bootstrap.pypa.io/get-pip.py' + - travis_retry curl -o ~/get-pip.py 'https://bootstrap.pypa.io/get-pip.py' && python3 ~/get-pip.py --user && pip3 install --user meson script: diff --git a/contrib/meson/meson.build b/contrib/meson/meson.build index 7e364eb..c28d90a 100644 --- a/contrib/meson/meson.build +++ b/contrib/meson/meson.build @@ -11,9 +11,7 @@ project('lz4', ['c'], license: ['BSD', 'GPLv2'], default_options : ['c_std=c99', 'buildtype=release'], - # Fall-back version in case of extracting version number from - # `lz4.h` failed. - version: '1.9.1', + version: 'DUMMY', meson_version: '>=0.47.0') cc = meson.get_compiler('c') @@ -40,13 +38,10 @@ lz4_h_file = join_paths(meson.current_source_dir(), '../../lib/lz4.h') GetLz4LibraryVersion_py = files('GetLz4LibraryVersion.py') r = run_command(python3, GetLz4LibraryVersion_py, lz4_h_file) if r.returncode() == 0 - output = r.stdout().strip() - if output.version_compare('>@0@'.format(lz4_version)) - lz4_version = output - message('Project version is now: @0@'.format(lz4_version)) - endif + lz4_version = r.stdout().strip() + message('Project version is now: @0@'.format(lz4_version)) else - warning('Cannot find project version in @0@'.format(lz4_h_file)) + error('Cannot find project version in @0@'.format(lz4_h_file)) endif lz4_libversion = lz4_version -- cgit v1.2.3 From 605d811e6cc94736dd609c644404dd24c013fd6f Mon Sep 17 00:00:00 2001 From: George Prekas Date: Fri, 3 May 2019 11:44:56 -0500 Subject: enable LZ4_FAST_DEC_LOOP build macro on aarch64/GCC by default --- lib/lz4.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/lz4.c b/lib/lz4.c index 69143c0..070dd7e 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -317,6 +317,11 @@ static const int dec64table[8] = {0, 0, 0, -1, -4, 1, 2, 3}; #ifndef LZ4_FAST_DEC_LOOP # if defined(__i386__) || defined(__x86_64__) # define LZ4_FAST_DEC_LOOP 1 +# elif defined(__aarch64__) && !defined(__clang__) + /* On aarch64, we disable this optimization for clang because on certain + * mobile chipsets and clang, it reduces performance. For more information + * refer to https://github.com/lz4/lz4/pull/707. */ +# define LZ4_FAST_DEC_LOOP 1 # else # define LZ4_FAST_DEC_LOOP 0 # endif -- cgit v1.2.3 From 98a86c8ef6ea3202f8cb52772144d0f744bd5c73 Mon Sep 17 00:00:00 2001 From: gstedman Date: Fri, 10 May 2019 16:54:05 +0100 Subject: Add multiframe report to --list command --- lib/lz4frame.c | 4 +- lib/lz4frame.h | 9 ++ programs/lz4io.c | 377 +++++++++++++++++++++++++++++++++++++------------ programs/lz4io.h | 2 + tests/Makefile | 7 +- tests/test-lz4-list.py | 282 ++++++++++++++++++++++++++++++++++++ 6 files changed, 585 insertions(+), 96 deletions(-) create mode 100644 tests/test-lz4-list.py diff --git a/lib/lz4frame.c b/lib/lz4frame.c index f131d9a..95b8b8e 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -213,8 +213,8 @@ static void LZ4F_writeLE64 (void* dst, U64 value64) static const size_t minFHSize = LZ4F_HEADER_SIZE_MIN; /* 7 */ static const size_t maxFHSize = LZ4F_HEADER_SIZE_MAX; /* 19 */ -static const size_t BHSize = 4; /* block header : size, and compress flag */ -static const size_t BFSize = 4; /* block footer : checksum (optional) */ +static const size_t BHSize = LZ4F_BLOCK_HEADER_SIZE; /* block header : size, and compress flag */ +static const size_t BFSize = LZ4F_BLOCK_CHECKSUM_SIZE; /* block footer : checksum (optional) */ /*-************************************ diff --git a/lib/lz4frame.h b/lib/lz4frame.h index 742c252..391e484 100644 --- a/lib/lz4frame.h +++ b/lib/lz4frame.h @@ -253,6 +253,15 @@ LZ4FLIB_API LZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_cctx* cctx); #define LZ4F_HEADER_SIZE_MIN 7 /* LZ4 Frame header size can vary, depending on selected paramaters */ #define LZ4F_HEADER_SIZE_MAX 19 +/* Size in bytes of a block header in little-endian format. Highest bit indicates if block data is uncompressed */ +#define LZ4F_BLOCK_HEADER_SIZE 4 + +/* Size in bytes of a block checksum footer in little-endian format. */ +#define LZ4F_BLOCK_CHECKSUM_SIZE 4 + +/* Size in bytes of the content checksum. */ +#define LZ4F_CONTENT_CHECKSUM_SIZE 4 + /*! LZ4F_compressBegin() : * will write the frame header into dstBuffer. * dstCapacity must be >= LZ4F_HEADER_SIZE_MAX bytes. diff --git a/programs/lz4io.c b/programs/lz4io.c index 960c451..c27a0ad 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -1278,82 +1278,118 @@ int LZ4IO_decompressMultipleFilenames(LZ4IO_prefs_t* const prefs, /* ********************** LZ4 --list command *********************** */ /* ********************************************************************* */ +typedef enum +{ + lz4Frame = 0, + legacyFrame, + skippableFrame +} LZ4IO_frameType_t; + +typedef struct { + LZ4F_frameInfo_t lz4FrameInfo; + LZ4IO_frameType_t frameType; +} LZ4IO_frameInfo_t; + +#define LZ4IO_INIT_FRAMEINFO { LZ4F_INIT_FRAMEINFO, lz4Frame } + typedef struct { - LZ4F_frameInfo_t frameInfo; const char* fileName; unsigned long long fileSize; + unsigned long long frameCount; + LZ4IO_frameInfo_t frameSummary; + unsigned short eqFrameTypes; + unsigned short eqBlockTypes; + unsigned short allContentSize; } LZ4IO_cFileInfo_t; -#define LZ4IO_INIT_CFILEINFO { LZ4F_INIT_FRAMEINFO, NULL, 0ULL } - +#define LZ4IO_INIT_CFILEINFO { NULL, 0ULL, 0, LZ4IO_INIT_FRAMEINFO, 1, 1, 1 } typedef enum { LZ4IO_LZ4F_OK, LZ4IO_format_not_known, LZ4IO_not_a_file } LZ4IO_infoResult; -/* This function is limited, - * it only works fine for a file consisting of a single valid frame using LZ4 Frame specification. - * It will not look at content beyond first frame header. - * It's also unable to parse legacy frames, nor skippable ones. - * - * Things to improve : - * - check the entire file for additional content after first frame - * + combine results from multiple frames, give total - * - Optional : - * + report nb of blocks, hence max. possible decompressed size (when not reported in header) - */ -static LZ4IO_infoResult -LZ4IO_getCompressedFileInfo(LZ4IO_cFileInfo_t* cfinfo, const char* input_filename) -{ - LZ4IO_infoResult result = LZ4IO_format_not_known; /* default result (error) */ - - if (!UTIL_isRegFile(input_filename)) return LZ4IO_not_a_file; - cfinfo->fileSize = UTIL_getFileSize(input_filename); +static const char * LZ4IO_frameTypeNames[]={"LZ4Frame", "LegacyFrame", "SkippableFrame" }; - /* Get filename without path prefix */ - { const char* b = strrchr(input_filename, '/'); - if (!b) { - b = strrchr(input_filename, '\\'); - } - if (b && b != input_filename) { - b++; - } else { - b = input_filename; +/* Read block headers and skip block data + Return total blocks size for this frame including headers, + block checksums and content checksums. + returns 0 in case it can't succesfully skip block data. + Assumes SEEK_CUR after frame header. + */ +static unsigned long long LZ4IO_skipBlocksData(FILE* finput, + const LZ4F_blockChecksum_t blockChecksumFlag, + const LZ4F_contentChecksum_t contentChecksumFlag){ + unsigned char blockInfo[LZ4F_BLOCK_HEADER_SIZE]; + unsigned long long totalBlocksSize = 0; + for(;;){ + if (!fread(blockInfo, 1, LZ4F_BLOCK_HEADER_SIZE, finput)){ + if (feof(finput)) return totalBlocksSize; + return 0; + } + totalBlocksSize += LZ4F_BLOCK_HEADER_SIZE; + { + const unsigned long nextCBlockSize = LZ4IO_readLE32(&blockInfo) & 0x7FFFFFFFU; + const unsigned long nextBlock = nextCBlockSize + (blockChecksumFlag * LZ4F_BLOCK_CHECKSUM_SIZE); + if (nextCBlockSize == 0){ + /* Reached EndMark */ + if(contentChecksumFlag){ + /* Skip content checksum */ + if(fseek(finput, LZ4F_CONTENT_CHECKSUM_SIZE, SEEK_CUR) != 0){ + return 0; + } + totalBlocksSize += LZ4F_CONTENT_CHECKSUM_SIZE; } - cfinfo->fileName = b; + break; + } + totalBlocksSize += nextBlock; + /* skip to the next block */ + if (fseek(finput, nextBlock, SEEK_CUR) != 0){ + return 0; + } } + } + return totalBlocksSize; +} - /* Read file and extract header */ - { size_t const hSize = LZ4F_HEADER_SIZE_MAX; - size_t readSize=0; - - void* const buffer = malloc(hSize); - if (!buffer) EXM_THROW(21, "Allocation error : not enough memory"); - - { FILE* const finput = LZ4IO_openSrcFile(input_filename); - if (finput) { - readSize = fread(buffer, 1, hSize, finput); - fclose(finput); - } } - - if (readSize > 0) { - LZ4F_dctx* dctx; - if (!LZ4F_isError(LZ4F_createDecompressionContext(&dctx, LZ4F_VERSION))) { - if (!LZ4F_isError(LZ4F_getFrameInfo(dctx, &cfinfo->frameInfo, buffer, &readSize))) { - result = LZ4IO_LZ4F_OK; - } } - LZ4F_freeDecompressionContext(dctx); +/* For legacy frames only. + Read block headers and skip block data. + Return total blocks size for this frame including headers. + or 0 in case it can't succesfully skip block data. + This works as long as legacy block header size = magic number size. + Assumes SEEK_CUR after frame header. + */ +static unsigned long long LZ4IO_skipLegacyBlocksData(FILE* finput){ + unsigned char blockInfo[LZIO_LEGACY_BLOCK_HEADER_SIZE]; + unsigned long long totalBlocksSize = 0; + if(LZIO_LEGACY_BLOCK_HEADER_SIZE != MAGICNUMBER_SIZE){ + DISPLAYLEVEL(4, "Legacy block header size not equal to magic number size. Cannot skip blocks"); + return 0; + } + for(;;){ + if (!fread(blockInfo, 1, LZIO_LEGACY_BLOCK_HEADER_SIZE, finput)){ + if (feof(finput)) return totalBlocksSize; + return 0; + } + { const unsigned int nextCBlockSize = LZ4IO_readLE32(&blockInfo); + if( nextCBlockSize == LEGACY_MAGICNUMBER || + nextCBlockSize == LZ4IO_MAGICNUMBER || + LZ4IO_isSkippableMagicNumber(nextCBlockSize)){ + /* Rewind back. we want cursor at the begining of next frame.*/ + if (fseek(finput, -LZIO_LEGACY_BLOCK_HEADER_SIZE, SEEK_CUR) != 0){ + return 0; } - - /* clean */ - free(buffer); + break; + } + totalBlocksSize += LZIO_LEGACY_BLOCK_HEADER_SIZE + nextCBlockSize; + /* skip to the next block */ + if (fseek(finput, nextCBlockSize, SEEK_CUR) != 0){ + return 0; + } } - - return result; + } + return totalBlocksSize; } - /* buffer : must be a valid memory area of at least 4 bytes */ -const char* LZ4IO_blockTypeID(int sizeID, int blockMode, char* buffer) -{ +const char* LZ4IO_blockTypeID(int sizeID, int blockMode, char* buffer){ buffer[0] = 'B'; assert(sizeID >= 4); assert(sizeID <=7); buffer[1] = (char)(sizeID + '0'); @@ -1362,47 +1398,204 @@ const char* LZ4IO_blockTypeID(int sizeID, int blockMode, char* buffer) return buffer; } +/* buffer : must be valid memory area of at least 10 bytes */ +static const char* LZ4IO_toHuman(long double size, char *buf){ + const char units[] = {"\0KMGTPEZY"}; + size_t i = 0; + for(;size>=1024;i++) size /= 1024; + sprintf(buf, "%.2Lf%c", size, units[i]); + return buf; +} -int LZ4IO_displayCompressedFilesInfo(const char** inFileNames, size_t ifnIdx) +/* Get filename without path prefix */ +static const char* LZ4IO_baseName(const char* input_filename) { + const char* b = strrchr(input_filename, '/'); + if (!b) b = strrchr(input_filename, '\\'); + return b ? b + 1 : b; +} + +/* Report frame/s information in verbose mode. + * Will populate file info with fileName and contentSize where applicable. + * - TODO : + * + report nb of blocks, hence max. possible decompressed size (when not reported in header) + */ +static LZ4IO_infoResult +LZ4IO_getCompressedFileInfo(LZ4IO_cFileInfo_t* cfinfo, const char* input_filename) { - int result = 0; - size_t idx; + LZ4IO_infoResult result = LZ4IO_format_not_known; /* default result (error) */ + unsigned char buffer[LZ4F_HEADER_SIZE_MAX]; + FILE* const finput = LZ4IO_openSrcFile(input_filename); + cfinfo->fileSize = UTIL_getFileSize(input_filename); - DISPLAY("%5s %20s %20s %10s %7s %s\n", - "Block", "Compressed", "Uncompressed", "Ratio", "Check", "Filename"); - for (idx=0; idxframeSummary.frameType != lz4Frame) cfinfo->eqFrameTypes = 0; + /* Get frame info */ + { const size_t readBytes = fread(buffer+MAGICNUMBER_SIZE, 1, LZ4F_HEADER_SIZE_MIN-MAGICNUMBER_SIZE, finput); + if (!readBytes || ferror(finput)) EXM_THROW(71, "Error reading %s", input_filename); } + { size_t hSize = LZ4F_headerSize(&buffer, LZ4F_HEADER_SIZE_MIN); + if(!LZ4F_isError(hSize)){ + if(hSize > (LZ4F_HEADER_SIZE_MIN + MAGICNUMBER_SIZE)){ + /* We've already read LZ4F_HEADER_SIZE_MIN so read any extra until hSize*/ + const size_t readBytes = fread(buffer+LZ4F_HEADER_SIZE_MIN, 1, hSize-LZ4F_HEADER_SIZE_MIN, finput); + if (!readBytes || ferror(finput)) EXM_THROW(72, "Error reading %s", input_filename); + } + /* Create decompression context */ + { LZ4F_dctx* dctx; + if (!LZ4F_isError(LZ4F_createDecompressionContext(&dctx, LZ4F_VERSION))) { + if (!LZ4F_isError(LZ4F_getFrameInfo(dctx, &frameInfo.lz4FrameInfo, buffer, &hSize))) { + if ((cfinfo->frameSummary.lz4FrameInfo.blockSizeID != frameInfo.lz4FrameInfo.blockSizeID || + cfinfo->frameSummary.lz4FrameInfo.blockMode != frameInfo.lz4FrameInfo.blockMode) + && cfinfo->frameCount !=0) + cfinfo->eqBlockTypes = 0; + { const unsigned long long totalBlocksSize = LZ4IO_skipBlocksData(finput, + frameInfo.lz4FrameInfo.blockChecksumFlag, + frameInfo.lz4FrameInfo.contentChecksumFlag); + if(totalBlocksSize){ + char bTypeBuffer[5]; + LZ4IO_blockTypeID(frameInfo.lz4FrameInfo.blockSizeID, frameInfo.lz4FrameInfo.blockMode, bTypeBuffer); + DISPLAYLEVEL(3, " %6llu %14s %5s %8s", + cfinfo->frameCount + 1, + LZ4IO_frameTypeNames[frameInfo.frameType], + bTypeBuffer, + frameInfo.lz4FrameInfo.contentChecksumFlag ? "XXH32" : "-"); + if(frameInfo.lz4FrameInfo.contentSize){ + { double const ratio = (double)(totalBlocksSize + hSize) / frameInfo.lz4FrameInfo.contentSize * 100; + DISPLAYLEVEL(3, " %20llu %20llu %9.2f%%\n", + totalBlocksSize + hSize, + frameInfo.lz4FrameInfo.contentSize, + ratio); } + /* Now we've consumed frameInfo we can use it to store the total contentSize */ + frameInfo.lz4FrameInfo.contentSize += cfinfo->frameSummary.lz4FrameInfo.contentSize; + } + else{ + DISPLAYLEVEL(3, " %20llu %20s %9s \n", totalBlocksSize + hSize, "-", "-"); + cfinfo->allContentSize = 0; + } + result = LZ4IO_LZ4F_OK; + } } + } + } } + } } + break; + case LEGACY_MAGICNUMBER: + frameInfo.frameType = legacyFrame; + if (cfinfo->frameSummary.frameType != legacyFrame && cfinfo->frameCount !=0) cfinfo->eqFrameTypes = 0; + cfinfo->eqBlockTypes = 0; + cfinfo->allContentSize = 0; + { const unsigned long long totalBlocksSize = LZ4IO_skipLegacyBlocksData(finput); + if (totalBlocksSize){ + DISPLAYLEVEL(3, " %6llu %14s %5s %8s %20llu %20s %9s\n", + cfinfo->frameCount + 1, + LZ4IO_frameTypeNames[frameInfo.frameType], + "-", "-", + totalBlocksSize + 4, + "-", "-"); + result = LZ4IO_LZ4F_OK; + } } + break; + case LZ4IO_SKIPPABLE0: + frameInfo.frameType = skippableFrame; + if (cfinfo->frameSummary.frameType != skippableFrame && cfinfo->frameCount !=0) cfinfo->eqFrameTypes = 0; + cfinfo->eqBlockTypes = 0; + cfinfo->allContentSize = 0; + { nbReadBytes = fread(buffer, 1, 4, finput); + if (nbReadBytes != 4) + EXM_THROW(42, "Stream error : skippable size unreadable"); + } + { unsigned const size = LZ4IO_readLE32(buffer); + int const errorNb = fseek_u32(finput, size, SEEK_CUR); + if (errorNb != 0) + EXM_THROW(43, "Stream error : cannot skip skippable area"); + DISPLAYLEVEL(3, " %6llu %14s %5s %8s %20u %20s %9s\n", + cfinfo->frameCount + 1, + "SkippableFrame", + "-", "-", size + 8, "-", "-"); + + result = LZ4IO_LZ4F_OK; + } + break; + default: + { long int const position = ftell(finput); /* only works for files < 2 GB */ + DISPLAYLEVEL(3, "Stream followed by undecodable data "); + if (position != -1L) + DISPLAYLEVEL(3, "at position %i ", (int)position); + DISPLAYLEVEL(3, "\n"); + } + break; + } + if(result != LZ4IO_LZ4F_OK){ + break; + } + cfinfo->frameSummary = frameInfo; } + cfinfo->frameCount++; + } + fclose(finput); + return result; +} + + +int LZ4IO_displayCompressedFilesInfo(const char** inFileNames, size_t ifnIdx) +{ + int result = 0; + size_t idx = 0; + if(g_displayLevel < 3){ + DISPLAY("%10s %14s %5s %11s %13s %9s %s\n", + "Frames", "Type", "Block", "Compressed", "Uncompressed", "Ratio", "Filename"); + } + for (; idx 0: + self.nvinfo_list.append(NVerboseFileInfo(line)) + + def test_frames(self): + all_concat_frames = 0 + all_concat_index = None + for i, nvinfo in enumerate(self.nvinfo_list): + if "concat-all" in nvinfo.filename: + all_concat_index = i + elif "2f--content-size" in nvinfo.filename: + self.assertEqual("2", nvinfo.frames, nvinfo.line) + all_concat_frames += 2 + else: + self.assertEqual("1", nvinfo.frames, nvinfo.line) + all_concat_frames += 1 + self.assertNotEqual(None, all_concat_index, "Couldn't find concat-all file index.") + self.assertEqual(self.nvinfo_list[all_concat_index].frames, str(all_concat_frames), self.nvinfo_list[all_concat_index].line) + + def test_frame_types(self): + for nvinfo in self.nvinfo_list: + if "-lz4f-" in nvinfo.filename: + self.assertEqual(nvinfo.type, "LZ4Frame", nvinfo.line) + elif "-legc-" in nvinfo.filename: + self.assertEqual(nvinfo.type, "LegacyFrame", nvinfo.line) + elif "-skip-" in nvinfo.filename: + self.assertEqual(nvinfo.type, "SkippableFrame", nvinfo.line) + + def test_block(self): + for nvinfo in self.nvinfo_list: + # if "-leg" in nvinfo.filename or "-skip" in nvinfo.filename: + # self.assertEqual(nvinfo.block, "-", nvinfo.line) + if "--BD" in nvinfo.filename: + self.assertRegex(nvinfo.block, "^B[0-9]+D$", nvinfo.line) + elif "--BI" in nvinfo.filename: + self.assertRegex(nvinfo.block, "^B[0-9]+I$", nvinfo.line) + + def test_compressed_size(self): + for nvinfo in self.nvinfo_list: + self.assertEqual(nvinfo.compressed, to_human(nvinfo.exp_comp_size), nvinfo.line) + + def test_ratio(self): + for nvinfo in self.nvinfo_list: + if "--content-size" in nvinfo.filename: + self.assertEqual(nvinfo.ratio, "{:.2f}%".format(float(nvinfo.exp_comp_size) / float(nvinfo.exp_unc_size) * 100), nvinfo.line) + + def test_uncompressed_size(self): + for nvinfo in self.nvinfo_list: + if "--content-size" in nvinfo.filename: + self.assertEqual(nvinfo.uncompressed, to_human(nvinfo.exp_unc_size), nvinfo.line) + + +class VerboseFileInfo(object): + def __init__(self, lines): + # Parse lines + self.frame_list = [] + self.file_frame_map = [] + for i, line in enumerate(lines): + if i == 0: + self.filename = line + continue + elif i == 1: + # Skip header + continue + frame_info = dict(zip(["frame", "type", "block", "checksum", "compressed", "uncompressed", "ratio"], line.split())) + frame_info["line"] = line + self.frame_list.append(frame_info) + + +class TestVerbose(unittest.TestCase): + @classmethod + def setUpClass(self): + # Even do we're listing 2 files to test multiline working as expected. + # we're only really interested in testing the output of the concat-all file. + self.vinfo_list = [] + start = end = 0 + output = execute("{} --list -m -v {}/test_list_concat-all.lz4 {}/test_list_*M-lz4f-2f--content-size.lz4".format(LZ4, TEMP, TEMP), print_output=True) + for i, line in enumerate(output): + if line.startswith("test_list"): + if start != 0 and end != 0: + self.vinfo_list.append(VerboseFileInfo(output[start:end])) + start = i + if not line: + end = i + self.vinfo_list.append(VerboseFileInfo(output[start:end])) + # Populate file_frame_map as a reference of the expected info + concat_file_list = glob.glob("/tmp/test_list_[!concat]*.lz4") + # One of the files has 2 frames so duplicate it in this list to map each frame 1 to a single file + for i, filename in enumerate(concat_file_list): + if "2f--content-size" in filename: + concat_file_list.insert(i, filename) + break + self.cvinfo = self.vinfo_list[0] + self.cvinfo.file_frame_map = concat_file_list + self.cvinfo.compressed_size = os.path.getsize("{}/test_list_concat-all.lz4".format(TEMP)) + + def test_filename(self): + for i, vinfo in enumerate(self.vinfo_list): + self.assertRegex(vinfo.filename, "^test_list_.*({}/{})".format(i + 1, len(self.vinfo_list))) + + def test_frame_number(self): + for vinfo in self.vinfo_list: + for i, frame_info in enumerate(vinfo.frame_list): + self.assertEqual(frame_info["frame"], str(i + 1), frame_info["line"]) + + def test_frame_type(self): + for i, frame_info in enumerate(self.cvinfo.frame_list): + if "-lz4f-" in self.cvinfo.file_frame_map[i]: + self.assertEqual(self.cvinfo.frame_list[i]["type"], "LZ4Frame", self.cvinfo.frame_list[i]["line"]) + elif "-legc-" in self.cvinfo.file_frame_map[i]: + self.assertEqual(self.cvinfo.frame_list[i]["type"], "LegacyFrame", self.cvinfo.frame_list[i]["line"]) + elif "-skip-" in self.cvinfo.file_frame_map[i]: + self.assertEqual(self.cvinfo.frame_list[i]["type"], "SkippableFrame", self.cvinfo.frame_list[i]["line"]) + + def test_block(self): + for i, frame_info in enumerate(self.cvinfo.frame_list): + if "--BD" in self.cvinfo.file_frame_map[i]: + self.assertRegex(self.cvinfo.frame_list[i]["block"], "^B[0-9]+D$", self.cvinfo.frame_list[i]["line"]) + elif "--BI" in self.cvinfo.file_frame_map[i]: + self.assertEqual(self.cvinfo.frame_list[i]["block"], "^B[0-9]+I$", self.cvinfo.frame_list[i]["line"]) + + def test_checksum(self): + for i, frame_info in enumerate(self.cvinfo.frame_list): + if "-lz4f-" in self.cvinfo.file_frame_map[i] and "--no-frame-crc" not in self.cvinfo.file_frame_map[i]: + self.assertEqual(self.cvinfo.frame_list[i]["checksum"], "XXH32", self.cvinfo.frame_list[i]["line"]) + + def test_compressed(self): + total = 0 + for i, frame_info in enumerate(self.cvinfo.frame_list): + if "-2f-" not in self.cvinfo.file_frame_map[i]: + expected_size = os.path.getsize(self.cvinfo.file_frame_map[i]) + self.assertEqual(self.cvinfo.frame_list[i]["compressed"], str(expected_size), self.cvinfo.frame_list[i]["line"]) + total += int(self.cvinfo.frame_list[i]["compressed"]) + self.assertEqual(total, self.cvinfo.compressed_size, "Expected total sum ({}) to match {} filesize".format(total, self.cvinfo.filename)) + + def test_uncompressed(self): + for i, frame_info in enumerate(self.cvinfo.frame_list): + ffm = self.cvinfo.file_frame_map[i] + if "-2f-" not in ffm and "--content-size" in ffm: + expected_size_unc = int(ffm[ffm.rindex("_") + 1:ffm.index("M")]) * 1048576 + self.assertEqual(self.cvinfo.frame_list[i]["uncompressed"], str(expected_size_unc), self.cvinfo.frame_list[i]["line"]) + + def test_ratio(self): + for i, frame_info in enumerate(self.cvinfo.frame_list): + if "--content-size" in self.cvinfo.file_frame_map[i]: + self.assertEqual(self.cvinfo.frame_list[i]['ratio'], + "{:.2f}%".format(float(self.cvinfo.frame_list[i]['compressed']) / float(self.cvinfo.frame_list[i]['uncompressed']) * 100), + self.cvinfo.frame_list[i]["line"]) + + +def to_human(size): + for unit in ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y']: + if size < 1024.0: + break + size /= 1024.0 + return "{:.2f}{}".format(size, unit) + + +def log(text): + print(time.strftime("%Y/%m/%d %H:%M:%S") + ' - ' + text) + + +def errout(text, err=1): + log(text) + exit(err) + + +def execute(command, print_command=True, print_output=False, print_error=True, param_shell=True): + if os.environ.get('QEMU_SYS'): + command = "{} {}".format(os.environ['QEMU_SYS'], command) + if print_command: + log("> " + command) + popen = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=param_shell) + stdout_lines, stderr_lines = popen.communicate() + stderr_lines = stderr_lines.decode("utf-8") + stdout_lines = stdout_lines.decode("utf-8") + if print_output: + if stdout_lines: + print(stdout_lines) + if stderr_lines: + print(stderr_lines) + if popen.returncode is not None and popen.returncode != 0: + if stderr_lines and not print_output and print_error: + print(stderr_lines) + errout("Failed to run: {}\n".format(command, stdout_lines + stderr_lines)) + return (stdout_lines + stderr_lines).splitlines() + + +def cleanup(silent=False): + for f in glob.glob("{}/test_list*".format(TEMP)): + if not silent: + log("Deleting {}".format(f)) + os.unlink(f) + + +def datagen(file_name, size): + non_sparse_size = size // 2 + sparse_size = size - non_sparse_size + with open(file_name, "wb") as f: + f.seek(sparse_size) + f.write(os.urandom(non_sparse_size)) + + +def generate_files(): + # file format ~ test_list-f.lz4 ~ + # Generate LZ4Frames + for i in SIZES: + filename = "{}/test_list_{}M".format(TEMP, i) + log("Generating {}".format(filename)) + datagen(filename, i * MIB) + for j in ["--content-size", "-BI", "-BD", "-BX", "--no-frame-crc"]: + lz4file = "{}-lz4f-1f{}.lz4".format(filename, j) + execute("{} {} {} {}".format(LZ4, j, filename, lz4file)) + # Generate skippable frames + lz4file = "{}-skip-1f.lz4".format(filename) + skipsize = i * 1024 + skipbytes = bytes([80, 42, 77, 24]) + skipsize.to_bytes(4, byteorder='little', signed=False) + with open(lz4file, 'wb') as f: + f.write(skipbytes) + f.write(os.urandom(skipsize)) + # Generate legacy frames + lz4file = "{}-legc-1f.lz4".format(filename) + execute("{} -l {} {}".format(LZ4, filename, lz4file)) + + # Concatenate --content-size files + file_list = glob.glob("{}/test_list_*-lz4f-1f--content-size.lz4".format(TEMP)) + with open("{}/test_list_{}M-lz4f-2f--content-size.lz4".format(TEMP, sum(SIZES)), 'ab') as outfile: + for fname in file_list: + with open(fname, 'rb') as infile: + outfile.write(infile.read()) + + # Concatenate all files + file_list = glob.glob("{}/test_list_*.lz4".format(TEMP)) + with open("{}/test_list_concat-all.lz4".format(TEMP), 'ab') as outfile: + for fname in file_list: + with open(fname, 'rb') as infile: + outfile.write(infile.read()) + + +if __name__ == '__main__': + cleanup() + generate_files() + unittest.main(verbosity=2, exit=False) + cleanup(silent=True) -- cgit v1.2.3 From 81081f9b2d6e114b06f7a3eecc8e6a20bb88e035 Mon Sep 17 00:00:00 2001 From: gstedman Date: Thu, 16 May 2019 15:12:00 +0100 Subject: List mode improvements. Improve formatting Include static assert Use UTIL_fseek to handle potential fseek limitation Be explicit when refusing to read from stdin Properly free dctx after use Include valgrind tests --- programs/lz4cli.c | 21 ++- programs/lz4io.c | 372 +++++++++++++++++++++++++------------------------ tests/Makefile | 2 + tests/test-lz4-list.py | 4 +- 4 files changed, 208 insertions(+), 191 deletions(-) diff --git a/programs/lz4cli.c b/programs/lz4cli.c index 39ff1ea..c83b4cc 100644 --- a/programs/lz4cli.c +++ b/programs/lz4cli.c @@ -141,7 +141,7 @@ static int usage_advanced(const char* exeName) DISPLAY( " -BX : enable block checksum (default:disabled) \n"); DISPLAY( "--no-frame-crc : disable stream checksum (default:enabled) \n"); DISPLAY( "--content-size : compressed frame includes original size (default:not present)\n"); - DISPLAY( "--list : lists information about .lz4 files. Useful if compressed with --content-size flag.\n"); + DISPLAY( "--list FILE : lists information about .lz4 files (useful for files compressed with --content-size flag)\n"); DISPLAY( "--[no-]sparse : sparse mode (default:enabled on file, disabled on stdout)\n"); DISPLAY( "--favor-decSpeed: compressed files decompress faster, but are less compressed \n"); DISPLAY( "--fast[=#]: switch to ultra fast compression level (default: %i)\n", 1); @@ -654,7 +654,6 @@ int main(int argc, const char** argv) /* No output filename ==> try to select one automatically (when possible) */ while ((!output_filename) && (multiple_inputs==0)) { - if (!IS_CONSOLE(stdout)) { /* Default to stdout whenever stdout is not the console. * Note : this policy may change in the future, therefore don't rely on it ! @@ -693,7 +692,19 @@ int main(int argc, const char** argv) break; } - if (multiple_inputs==0 && mode != om_list) assert(output_filename); + if (mode == om_list){ + /* Exit if trying to read from stdin as this isn't supported in this mode */ + if(!strcmp(input_filename, stdinmark)){ + DISPLAYLEVEL(1, "refusing to read from standard input in --list mode\n"); + exit(1); + } + if(!multiple_inputs){ + inFileNames[ifnIdx++] = input_filename; + } + } + else{ + if (multiple_inputs==0) assert(output_filename); + } /* when multiple_inputs==1, output_filename may simply be useless, * however, output_filename must be !NULL for next strcmp() tests */ if (!output_filename) output_filename = "*\\dummy^!//"; @@ -723,11 +734,7 @@ int main(int argc, const char** argv) operationResult = DEFAULT_DECOMPRESSOR(prefs, input_filename, output_filename); } } else if (mode == om_list){ - if(!multiple_inputs){ - inFileNames[ifnIdx++] = input_filename; - } operationResult = LZ4IO_displayCompressedFilesInfo(inFileNames, ifnIdx); - inFileNames=NULL; } else { /* compression is default action */ if (legacy_format) { DISPLAYLEVEL(3, "! Generating LZ4 Legacy format (deprecated) ! \n"); diff --git a/programs/lz4io.c b/programs/lz4io.c index c27a0ad..d03aa6d 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -102,6 +102,7 @@ static int g_displayLevel = 0; /* 0 : no display ; 1: errors ; 2 : + result } } static const clock_t refreshRate = CLOCKS_PER_SEC / 6; static clock_t g_time = 0; +#define LZ4IO_STATIC_ASSERT(c) { enum { LZ4IO_static_assert = 1/(int)(!!(c)) }; } /* use after variable declarations */ /************************************** @@ -1286,112 +1287,109 @@ typedef enum } LZ4IO_frameType_t; typedef struct { - LZ4F_frameInfo_t lz4FrameInfo; - LZ4IO_frameType_t frameType; + LZ4F_frameInfo_t lz4FrameInfo; + LZ4IO_frameType_t frameType; } LZ4IO_frameInfo_t; #define LZ4IO_INIT_FRAMEINFO { LZ4F_INIT_FRAMEINFO, lz4Frame } typedef struct { - const char* fileName; - unsigned long long fileSize; - unsigned long long frameCount; - LZ4IO_frameInfo_t frameSummary; - unsigned short eqFrameTypes; - unsigned short eqBlockTypes; - unsigned short allContentSize; + const char* fileName; + unsigned long long fileSize; + unsigned long long frameCount; + LZ4IO_frameInfo_t frameSummary; + unsigned short eqFrameTypes; + unsigned short eqBlockTypes; + unsigned short allContentSize; } LZ4IO_cFileInfo_t; #define LZ4IO_INIT_CFILEINFO { NULL, 0ULL, 0, LZ4IO_INIT_FRAMEINFO, 1, 1, 1 } typedef enum { LZ4IO_LZ4F_OK, LZ4IO_format_not_known, LZ4IO_not_a_file } LZ4IO_infoResult; -static const char * LZ4IO_frameTypeNames[]={"LZ4Frame", "LegacyFrame", "SkippableFrame" }; +static const char * LZ4IO_frameTypeNames[] = {"LZ4Frame", "LegacyFrame", "SkippableFrame" }; /* Read block headers and skip block data - Return total blocks size for this frame including headers, + Return total blocks size for this frame including block headers, block checksums and content checksums. returns 0 in case it can't succesfully skip block data. Assumes SEEK_CUR after frame header. */ static unsigned long long LZ4IO_skipBlocksData(FILE* finput, - const LZ4F_blockChecksum_t blockChecksumFlag, - const LZ4F_contentChecksum_t contentChecksumFlag){ - unsigned char blockInfo[LZ4F_BLOCK_HEADER_SIZE]; - unsigned long long totalBlocksSize = 0; - for(;;){ - if (!fread(blockInfo, 1, LZ4F_BLOCK_HEADER_SIZE, finput)){ - if (feof(finput)) return totalBlocksSize; - return 0; - } - totalBlocksSize += LZ4F_BLOCK_HEADER_SIZE; - { - const unsigned long nextCBlockSize = LZ4IO_readLE32(&blockInfo) & 0x7FFFFFFFU; - const unsigned long nextBlock = nextCBlockSize + (blockChecksumFlag * LZ4F_BLOCK_CHECKSUM_SIZE); - if (nextCBlockSize == 0){ - /* Reached EndMark */ - if(contentChecksumFlag){ - /* Skip content checksum */ - if(fseek(finput, LZ4F_CONTENT_CHECKSUM_SIZE, SEEK_CUR) != 0){ + const LZ4F_blockChecksum_t blockChecksumFlag, + const LZ4F_contentChecksum_t contentChecksumFlag) { + unsigned char blockInfo[LZ4F_BLOCK_HEADER_SIZE]; + unsigned long long totalBlocksSize = 0; + for (;;) { + if (!fread(blockInfo, 1, LZ4F_BLOCK_HEADER_SIZE, finput)) { + if (feof(finput)) return totalBlocksSize; return 0; - } - totalBlocksSize += LZ4F_CONTENT_CHECKSUM_SIZE; } - break; - } - totalBlocksSize += nextBlock; - /* skip to the next block */ - if (fseek(finput, nextBlock, SEEK_CUR) != 0){ - return 0; - } + totalBlocksSize += LZ4F_BLOCK_HEADER_SIZE; + { + const unsigned long nextCBlockSize = LZ4IO_readLE32(&blockInfo) & 0x7FFFFFFFU; + const unsigned long nextBlock = nextCBlockSize + (blockChecksumFlag * LZ4F_BLOCK_CHECKSUM_SIZE); + if (nextCBlockSize == 0) { + /* Reached EndMark */ + if (contentChecksumFlag) { + /* Skip content checksum */ + if (UTIL_fseek(finput, LZ4F_CONTENT_CHECKSUM_SIZE, SEEK_CUR) != 0) { + return 0; + } + totalBlocksSize += LZ4F_CONTENT_CHECKSUM_SIZE; + } + break; + } + totalBlocksSize += nextBlock; + /* skip to the next block */ + if (UTIL_fseek(finput, nextBlock, SEEK_CUR) != 0) { + return 0; + } + } } - } - return totalBlocksSize; + return totalBlocksSize; } /* For legacy frames only. Read block headers and skip block data. - Return total blocks size for this frame including headers. + Return total blocks size for this frame including block headers. or 0 in case it can't succesfully skip block data. This works as long as legacy block header size = magic number size. Assumes SEEK_CUR after frame header. */ -static unsigned long long LZ4IO_skipLegacyBlocksData(FILE* finput){ - unsigned char blockInfo[LZIO_LEGACY_BLOCK_HEADER_SIZE]; - unsigned long long totalBlocksSize = 0; - if(LZIO_LEGACY_BLOCK_HEADER_SIZE != MAGICNUMBER_SIZE){ - DISPLAYLEVEL(4, "Legacy block header size not equal to magic number size. Cannot skip blocks"); - return 0; - } - for(;;){ - if (!fread(blockInfo, 1, LZIO_LEGACY_BLOCK_HEADER_SIZE, finput)){ - if (feof(finput)) return totalBlocksSize; - return 0; - } - { const unsigned int nextCBlockSize = LZ4IO_readLE32(&blockInfo); - if( nextCBlockSize == LEGACY_MAGICNUMBER || - nextCBlockSize == LZ4IO_MAGICNUMBER || - LZ4IO_isSkippableMagicNumber(nextCBlockSize)){ - /* Rewind back. we want cursor at the begining of next frame.*/ - if (fseek(finput, -LZIO_LEGACY_BLOCK_HEADER_SIZE, SEEK_CUR) != 0){ - return 0; +static unsigned long long LZ4IO_skipLegacyBlocksData(FILE* finput) { + unsigned char blockInfo[LZIO_LEGACY_BLOCK_HEADER_SIZE]; + unsigned long long totalBlocksSize = 0; + LZ4IO_STATIC_ASSERT(LZIO_LEGACY_BLOCK_HEADER_SIZE == MAGICNUMBER_SIZE); + for (;;) { + if (!fread(blockInfo, 1, LZIO_LEGACY_BLOCK_HEADER_SIZE, finput)) { + if (feof(finput)) return totalBlocksSize; + return 0; + } + { const unsigned int nextCBlockSize = LZ4IO_readLE32(&blockInfo); + if ( nextCBlockSize == LEGACY_MAGICNUMBER || + nextCBlockSize == LZ4IO_MAGICNUMBER || + LZ4IO_isSkippableMagicNumber(nextCBlockSize)) { + /* Rewind back. we want cursor at the begining of next frame.*/ + if (fseek(finput, -LZIO_LEGACY_BLOCK_HEADER_SIZE, SEEK_CUR) != 0) { + return 0; + } + break; + } + totalBlocksSize += LZIO_LEGACY_BLOCK_HEADER_SIZE + nextCBlockSize; + /* skip to the next block */ + if (UTIL_fseek(finput, nextCBlockSize, SEEK_CUR) != 0) { + return 0; + } } - break; - } - totalBlocksSize += LZIO_LEGACY_BLOCK_HEADER_SIZE + nextCBlockSize; - /* skip to the next block */ - if (fseek(finput, nextCBlockSize, SEEK_CUR) != 0){ - return 0; - } } - } - return totalBlocksSize; + return totalBlocksSize; } /* buffer : must be a valid memory area of at least 4 bytes */ -const char* LZ4IO_blockTypeID(int sizeID, int blockMode, char* buffer){ +const char* LZ4IO_blockTypeID(int sizeID, int blockMode, char* buffer) { buffer[0] = 'B'; - assert(sizeID >= 4); assert(sizeID <=7); + assert(sizeID >= 4); assert(sizeID <= 7); buffer[1] = (char)(sizeID + '0'); buffer[2] = (blockMode == LZ4F_blockIndependent) ? 'I' : 'D'; buffer[3] = 0; @@ -1399,23 +1397,24 @@ const char* LZ4IO_blockTypeID(int sizeID, int blockMode, char* buffer){ } /* buffer : must be valid memory area of at least 10 bytes */ -static const char* LZ4IO_toHuman(long double size, char *buf){ +static const char* LZ4IO_toHuman(long double size, char *buf) { const char units[] = {"\0KMGTPEZY"}; size_t i = 0; - for(;size>=1024;i++) size /= 1024; + for (; size >= 1024; i++) size /= 1024; sprintf(buf, "%.2Lf%c", size, units[i]); return buf; } /* Get filename without path prefix */ static const char* LZ4IO_baseName(const char* input_filename) { - const char* b = strrchr(input_filename, '/'); - if (!b) b = strrchr(input_filename, '\\'); - return b ? b + 1 : b; + const char* b = strrchr(input_filename, '/'); + if (!b) b = strrchr(input_filename, '\\'); + if (!b) return input_filename; + return b ? b + 1 : b; } /* Report frame/s information in verbose mode. - * Will populate file info with fileName and contentSize where applicable. + * Will populate file info with fileName and frameSummary where applicable. * - TODO : * + report nb of blocks, hence max. possible decompressed size (when not reported in header) */ @@ -1427,91 +1426,98 @@ LZ4IO_getCompressedFileInfo(LZ4IO_cFileInfo_t* cfinfo, const char* input_filenam FILE* const finput = LZ4IO_openSrcFile(input_filename); cfinfo->fileSize = UTIL_getFileSize(input_filename); - - while(!feof(finput)){ - { LZ4IO_frameInfo_t frameInfo = LZ4IO_INIT_FRAMEINFO; + while (!feof(finput)) { + LZ4IO_frameInfo_t frameInfo = LZ4IO_INIT_FRAMEINFO; unsigned magicNumber; /* Get MagicNumber */ size_t nbReadBytes = fread(buffer, 1, MAGICNUMBER_SIZE, finput); - if (nbReadBytes==0) { break; } /* EOF */ + if (nbReadBytes == 0) { break; } /* EOF */ result = LZ4IO_format_not_known; /* default result (error) */ if (nbReadBytes != MAGICNUMBER_SIZE) - EXM_THROW(40, "Unrecognized header : Magic Number unreadable"); + EXM_THROW(40, "Unrecognized header : Magic Number unreadable"); magicNumber = LZ4IO_readLE32(buffer); /* Little Endian format */ if (LZ4IO_isSkippableMagicNumber(magicNumber)) magicNumber = LZ4IO_SKIPPABLE0; /* fold skippable magic numbers */ - switch(magicNumber) - { + switch (magicNumber) { case LZ4IO_MAGICNUMBER: - if(cfinfo->frameSummary.frameType != lz4Frame) cfinfo->eqFrameTypes = 0; + if (cfinfo->frameSummary.frameType != lz4Frame) cfinfo->eqFrameTypes = 0; /* Get frame info */ - { const size_t readBytes = fread(buffer+MAGICNUMBER_SIZE, 1, LZ4F_HEADER_SIZE_MIN-MAGICNUMBER_SIZE, finput); - if (!readBytes || ferror(finput)) EXM_THROW(71, "Error reading %s", input_filename); } - { size_t hSize = LZ4F_headerSize(&buffer, LZ4F_HEADER_SIZE_MIN); - if(!LZ4F_isError(hSize)){ - if(hSize > (LZ4F_HEADER_SIZE_MIN + MAGICNUMBER_SIZE)){ - /* We've already read LZ4F_HEADER_SIZE_MIN so read any extra until hSize*/ - const size_t readBytes = fread(buffer+LZ4F_HEADER_SIZE_MIN, 1, hSize-LZ4F_HEADER_SIZE_MIN, finput); - if (!readBytes || ferror(finput)) EXM_THROW(72, "Error reading %s", input_filename); - } - /* Create decompression context */ - { LZ4F_dctx* dctx; - if (!LZ4F_isError(LZ4F_createDecompressionContext(&dctx, LZ4F_VERSION))) { - if (!LZ4F_isError(LZ4F_getFrameInfo(dctx, &frameInfo.lz4FrameInfo, buffer, &hSize))) { - if ((cfinfo->frameSummary.lz4FrameInfo.blockSizeID != frameInfo.lz4FrameInfo.blockSizeID || - cfinfo->frameSummary.lz4FrameInfo.blockMode != frameInfo.lz4FrameInfo.blockMode) - && cfinfo->frameCount !=0) - cfinfo->eqBlockTypes = 0; - { const unsigned long long totalBlocksSize = LZ4IO_skipBlocksData(finput, - frameInfo.lz4FrameInfo.blockChecksumFlag, - frameInfo.lz4FrameInfo.contentChecksumFlag); - if(totalBlocksSize){ - char bTypeBuffer[5]; - LZ4IO_blockTypeID(frameInfo.lz4FrameInfo.blockSizeID, frameInfo.lz4FrameInfo.blockMode, bTypeBuffer); - DISPLAYLEVEL(3, " %6llu %14s %5s %8s", - cfinfo->frameCount + 1, - LZ4IO_frameTypeNames[frameInfo.frameType], - bTypeBuffer, - frameInfo.lz4FrameInfo.contentChecksumFlag ? "XXH32" : "-"); - if(frameInfo.lz4FrameInfo.contentSize){ - { double const ratio = (double)(totalBlocksSize + hSize) / frameInfo.lz4FrameInfo.contentSize * 100; - DISPLAYLEVEL(3, " %20llu %20llu %9.2f%%\n", - totalBlocksSize + hSize, - frameInfo.lz4FrameInfo.contentSize, - ratio); } - /* Now we've consumed frameInfo we can use it to store the total contentSize */ - frameInfo.lz4FrameInfo.contentSize += cfinfo->frameSummary.lz4FrameInfo.contentSize; - } - else{ - DISPLAYLEVEL(3, " %20llu %20s %9s \n", totalBlocksSize + hSize, "-", "-"); - cfinfo->allContentSize = 0; + { const size_t readBytes = fread(buffer + MAGICNUMBER_SIZE, 1, LZ4F_HEADER_SIZE_MIN - MAGICNUMBER_SIZE, finput); + if (!readBytes || ferror(finput)) EXM_THROW(71, "Error reading %s", input_filename); + } + { size_t hSize = LZ4F_headerSize(&buffer, LZ4F_HEADER_SIZE_MIN); + if (!LZ4F_isError(hSize)) { + if (hSize > (LZ4F_HEADER_SIZE_MIN + MAGICNUMBER_SIZE)) { + /* We've already read LZ4F_HEADER_SIZE_MIN so read any extra until hSize*/ + const size_t readBytes = fread(buffer + LZ4F_HEADER_SIZE_MIN, 1, hSize - LZ4F_HEADER_SIZE_MIN, finput); + if (!readBytes || ferror(finput)) EXM_THROW(72, "Error reading %s", input_filename); + } + /* Create decompression context */ + { LZ4F_dctx* dctx; + unsigned isError = LZ4F_isError(LZ4F_createDecompressionContext(&dctx, LZ4F_VERSION)); + if (!isError) { + isError = LZ4F_isError(LZ4F_getFrameInfo(dctx, &frameInfo.lz4FrameInfo, buffer, &hSize)); + LZ4F_freeDecompressionContext(dctx); + if (!isError) { + if ((cfinfo->frameSummary.lz4FrameInfo.blockSizeID != frameInfo.lz4FrameInfo.blockSizeID || + cfinfo->frameSummary.lz4FrameInfo.blockMode != frameInfo.lz4FrameInfo.blockMode) + && cfinfo->frameCount != 0) + cfinfo->eqBlockTypes = 0; + { const unsigned long long totalBlocksSize = LZ4IO_skipBlocksData(finput, + frameInfo.lz4FrameInfo.blockChecksumFlag, + frameInfo.lz4FrameInfo.contentChecksumFlag); + if (totalBlocksSize) { + char bTypeBuffer[5]; + LZ4IO_blockTypeID(frameInfo.lz4FrameInfo.blockSizeID, frameInfo.lz4FrameInfo.blockMode, bTypeBuffer); + DISPLAYLEVEL(3, " %6llu %14s %5s %8s", + cfinfo->frameCount + 1, + LZ4IO_frameTypeNames[frameInfo.frameType], + bTypeBuffer, + frameInfo.lz4FrameInfo.contentChecksumFlag ? "XXH32" : "-"); + if (frameInfo.lz4FrameInfo.contentSize) { + { double const ratio = (double)(totalBlocksSize + hSize) / frameInfo.lz4FrameInfo.contentSize * 100; + DISPLAYLEVEL(3, " %20llu %20llu %9.2f%%\n", + totalBlocksSize + hSize, + frameInfo.lz4FrameInfo.contentSize, + ratio); + } + /* Now we've consumed frameInfo we can use it to store the total contentSize */ + frameInfo.lz4FrameInfo.contentSize += cfinfo->frameSummary.lz4FrameInfo.contentSize; + } + else { + DISPLAYLEVEL(3, " %20llu %20s %9s \n", totalBlocksSize + hSize, "-", "-"); + cfinfo->allContentSize = 0; + } + result = LZ4IO_LZ4F_OK; + } + } + } } - result = LZ4IO_LZ4F_OK; - } } - } - } } - } } + } + } + } break; case LEGACY_MAGICNUMBER: frameInfo.frameType = legacyFrame; - if (cfinfo->frameSummary.frameType != legacyFrame && cfinfo->frameCount !=0) cfinfo->eqFrameTypes = 0; + if (cfinfo->frameSummary.frameType != legacyFrame && cfinfo->frameCount != 0) cfinfo->eqFrameTypes = 0; cfinfo->eqBlockTypes = 0; cfinfo->allContentSize = 0; - { const unsigned long long totalBlocksSize = LZ4IO_skipLegacyBlocksData(finput); - if (totalBlocksSize){ - DISPLAYLEVEL(3, " %6llu %14s %5s %8s %20llu %20s %9s\n", - cfinfo->frameCount + 1, - LZ4IO_frameTypeNames[frameInfo.frameType], - "-", "-", - totalBlocksSize + 4, - "-", "-"); - result = LZ4IO_LZ4F_OK; - } } + { const unsigned long long totalBlocksSize = LZ4IO_skipLegacyBlocksData(finput); + if (totalBlocksSize) { + DISPLAYLEVEL(3, " %6llu %14s %5s %8s %20llu %20s %9s\n", + cfinfo->frameCount + 1, + LZ4IO_frameTypeNames[frameInfo.frameType], + "-", "-", + totalBlocksSize + 4, + "-", "-"); + result = LZ4IO_LZ4F_OK; + } + } break; case LZ4IO_SKIPPABLE0: frameInfo.frameType = skippableFrame; - if (cfinfo->frameSummary.frameType != skippableFrame && cfinfo->frameCount !=0) cfinfo->eqFrameTypes = 0; + if (cfinfo->frameSummary.frameType != skippableFrame && cfinfo->frameCount != 0) cfinfo->eqFrameTypes = 0; cfinfo->eqBlockTypes = 0; cfinfo->allContentSize = 0; { nbReadBytes = fread(buffer, 1, 4, finput); @@ -1522,10 +1528,10 @@ LZ4IO_getCompressedFileInfo(LZ4IO_cFileInfo_t* cfinfo, const char* input_filenam int const errorNb = fseek_u32(finput, size, SEEK_CUR); if (errorNb != 0) EXM_THROW(43, "Stream error : cannot skip skippable area"); - DISPLAYLEVEL(3, " %6llu %14s %5s %8s %20u %20s %9s\n", - cfinfo->frameCount + 1, - "SkippableFrame", - "-", "-", size + 8, "-", "-"); + DISPLAYLEVEL(3, " %6llu %14s %5s %8s %20u %20s %9s\n", + cfinfo->frameCount + 1, + "SkippableFrame", + "-", "-", size + 8, "-", "-"); result = LZ4IO_LZ4F_OK; } @@ -1537,13 +1543,13 @@ LZ4IO_getCompressedFileInfo(LZ4IO_cFileInfo_t* cfinfo, const char* input_filenam DISPLAYLEVEL(3, "at position %i ", (int)position); DISPLAYLEVEL(3, "\n"); } + break; + } + if (result != LZ4IO_LZ4F_OK) { break; } - if(result != LZ4IO_LZ4F_OK){ - break; - } - cfinfo->frameSummary = frameInfo; } - cfinfo->frameCount++; + cfinfo->frameSummary = frameInfo; + cfinfo->frameCount++; } fclose(finput); return result; @@ -1554,11 +1560,11 @@ int LZ4IO_displayCompressedFilesInfo(const char** inFileNames, size_t ifnIdx) { int result = 0; size_t idx = 0; - if(g_displayLevel < 3){ - DISPLAY("%10s %14s %5s %11s %13s %9s %s\n", - "Frames", "Type", "Block", "Compressed", "Uncompressed", "Ratio", "Filename"); + if (g_displayLevel < 3) { + DISPLAY("%10s %14s %5s %11s %13s %9s %s\n", + "Frames", "Type", "Block", "Compressed", "Uncompressed", "Ratio", "Filename"); } - for (; idx Date: Thu, 16 May 2019 16:46:16 -0700 Subject: updated tests - only play listTest with `make test`, not `make all` which is limited to build - update `clangtest`, so that it's possible to disable O3 optimization, for faster processing --- .circleci/config.yml | 4 ++-- Makefile | 11 +++++++---- tests/Makefile | 4 ++-- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 4c08cb2..a620cd1 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -10,7 +10,7 @@ version: 2 jobs: build: working_directory: ~/lz4/lz4 - parallelism: 1 + parallelism: 2 shell: /bin/bash --login # CircleCI 2.0 does not support environment variables that refer to each other the same way as 1.0 did. # If any of these refer to each other, rewrite them so that they don't or see https://circleci.com/docs/2.0/env-vars/#interpolating-environment-variables-to-set-other-environment-variables . @@ -71,7 +71,7 @@ jobs: # Test # This would typically be a build job when using workflows, possibly combined with build # This is based on your 1.0 configuration file or project settings - - run: clang -v; make clangtest && make clean + - run: CFLAGS= make clangtest && make clean - run: g++ -v; make gpptest && make clean - run: gcc -v; make c_standards && make clean - run: gcc -v; g++ -v; make ctocpptest && make clean diff --git a/Makefile b/Makefile index dd731eb..e24cec5 100644 --- a/Makefile +++ b/Makefile @@ -125,11 +125,14 @@ test: $(MAKE) -C $(TESTDIR) $@ $(MAKE) -C $(EXDIR) $@ +clangtest: CFLAGS ?= -O3 +clangtest: CFLAGS += -Werror -Wconversion -Wno-sign-conversion +clangtest: CC = clang clangtest: clean - clang -v - @CFLAGS="-O3 -Werror -Wconversion -Wno-sign-conversion" $(MAKE) -C $(LZ4DIR) all CC=clang - @CFLAGS="-O3 -Werror -Wconversion -Wno-sign-conversion" $(MAKE) -C $(PRGDIR) all CC=clang - @CFLAGS="-O3 -Werror -Wconversion -Wno-sign-conversion" $(MAKE) -C $(TESTDIR) all CC=clang + $(CC) -v + @CFLAGS="$(CFLAGS)" $(MAKE) -C $(LZ4DIR) all CC=$(CC) + @CFLAGS="$(CFLAGS)" $(MAKE) -C $(PRGDIR) all CC=$(CC) + @CFLAGS="$(CFLAGS)" $(MAKE) -C $(TESTDIR) all CC=$(CC) clangtest-native: clean clang -v diff --git a/tests/Makefile b/tests/Makefile index dbb7bd5..a38031a 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -55,7 +55,7 @@ NB_LOOPS ?= -i1 default: all -all: fullbench fuzzer frametest roundTripTest datagen checkFrame listTest +all: fullbench fuzzer frametest roundTripTest datagen checkFrame all32: CFLAGS+=-m32 all32: all @@ -153,7 +153,7 @@ list: @$(MAKE) -pRrq -f $(lastword $(MAKEFILE_LIST)) : 2>/dev/null | awk -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' | sort | egrep -v -e '^[^[:alnum:]]' -e '^$@$$' | xargs .PHONY: test -test: test-lz4 test-lz4c test-frametest test-fullbench test-fuzzer test-install test-amalgamation +test: test-lz4 test-lz4c test-frametest test-fullbench test-fuzzer test-install test-amalgamation listTest .PHONY: test32 test32: CFLAGS+=-m32 -- cgit v1.2.3 From 2be2fe43a8460521c90edcfbbb98517933429f4d Mon Sep 17 00:00:00 2001 From: Niko Dzhus Date: Fri, 24 May 2019 18:25:06 +0300 Subject: fix temporary buffer use when input size hint is respected --- lib/lz4frame.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/lz4frame.c b/lib/lz4frame.c index 95b8b8e..cc7f2d5 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -1494,7 +1494,7 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, /* next block is a compressed block */ dctx->tmpInTarget = nextCBlockSize + crcSize; dctx->dStage = dstage_getCBlock; - if (dstPtr==dstEnd) { + if (dstPtr==dstEnd || srcPtr==srcEnd) { nextSrcSizeHint = BHSize + nextCBlockSize + crcSize; doAnotherStage = 0; } -- cgit v1.2.3 From 729eef61a12d381f436da180329dba74b9892aa4 Mon Sep 17 00:00:00 2001 From: gabrielstedman Date: Sat, 25 May 2019 19:57:04 +0100 Subject: Handle file not existing case #704 --- programs/lz4cli.c | 13 ++++++++++--- tests/Makefile | 1 + 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/programs/lz4cli.c b/programs/lz4cli.c index c83b4cc..3619cd5 100644 --- a/programs/lz4cli.c +++ b/programs/lz4cli.c @@ -648,9 +648,16 @@ int main(int argc, const char** argv) DISPLAYLEVEL(1, "refusing to read from a console\n"); exit(1); } - /* if input==stdin and no output defined, stdout becomes default output */ - if (!strcmp(input_filename, stdinmark) && !output_filename) - output_filename = stdoutmark; + if (!strcmp(input_filename, stdinmark)) { + /* if input==stdin and no output defined, stdout becomes default output */ + if (!output_filename) output_filename = stdoutmark; + } + else{ + if (!recursive && !UTIL_isRegFile(input_filename)) { + DISPLAYLEVEL(1, "%s: is not a regular file \n", input_filename); + exit(1); + } + } /* No output filename ==> try to select one automatically (when possible) */ while ((!output_filename) && (multiple_inputs==0)) { diff --git a/tests/Makefile b/tests/Makefile index a38031a..65713ef 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -376,6 +376,7 @@ test-lz4-testmode: lz4 datagen @echo "\n ---- non-existing source ----" ! $(LZ4) file-does-not-exist ! $(LZ4) -f file-does-not-exist + ! $(LZ4) -t file-does-not-exist ! $(LZ4) -fm file1-dne file2-dne @$(RM) tmp-tlt tmp-tlt1 tmp-tlt2 tmp-tlt2.lz4 -- cgit v1.2.3 From 4fc6b485500ac85cd6abca04bf0fdd687ade8213 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 29 May 2019 11:19:10 -0700 Subject: added test case for in-place compression --- tests/fuzzer.c | 57 +++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 16 deletions(-) diff --git a/tests/fuzzer.c b/tests/fuzzer.c index a5b5c93..26e25eb 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -389,8 +389,8 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c /* Test compression destSize */ FUZ_DISPLAYTEST("test LZ4_compress_destSize()"); { int srcSize = blockSize; - int const targetSize = srcSize * ((FUZ_rand(&randState) & 127)+1) >> 7; - char endCheck = FUZ_rand(&randState) & 255; + int const targetSize = srcSize * (int)((FUZ_rand(&randState) & 127)+1) >> 7; + char endCheck = (char)(FUZ_rand(&randState) & 255); compressedBuffer[targetSize] = endCheck; ret = LZ4_compress_destSize(block, compressedBuffer, &srcSize, targetSize); FUZ_CHECKTEST(ret > targetSize, "LZ4_compress_destSize() result larger than dst buffer !"); @@ -400,7 +400,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c if (targetSize>0) { /* check correctness */ U32 const crcBase = XXH32(block, (size_t)srcSize, 0); - char const canary = FUZ_rand(&randState) & 255; + char const canary = (char)(FUZ_rand(&randState) & 255); FUZ_CHECKTEST((ret==0), "LZ4_compress_destSize() compression failed"); FUZ_DISPLAYTEST(); compressedSize = ret; @@ -409,7 +409,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c FUZ_CHECKTEST(ret<0, "LZ4_decompress_safe() failed on data compressed by LZ4_compress_destSize"); FUZ_CHECKTEST(ret!=srcSize, "LZ4_decompress_safe() failed : did not fully decompressed data"); FUZ_CHECKTEST(decodedBuffer[srcSize] != canary, "LZ4_decompress_safe() overwrite dst buffer !"); - { U32 const crcDec = XXH32(decodedBuffer, srcSize, 0); + { U32 const crcDec = XXH32(decodedBuffer, (size_t)srcSize, 0); FUZ_CHECKTEST(crcDec!=crcBase, "LZ4_decompress_safe() corrupted decoded data"); } DISPLAYLEVEL(5, " OK \n"); @@ -420,8 +420,8 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c /* Test compression HC destSize */ FUZ_DISPLAYTEST("test LZ4_compress_HC_destSize()"); { int srcSize = blockSize; - int const targetSize = srcSize * ((FUZ_rand(&randState) & 127)+1) >> 7; - char const endCheck = FUZ_rand(&randState) & 255; + int const targetSize = srcSize * (int)((FUZ_rand(&randState) & 127)+1) >> 7; + char const endCheck = (char)(FUZ_rand(&randState) & 255); void* ctx = LZ4_createHC(block); FUZ_CHECKTEST(ctx==NULL, "LZ4_createHC() allocation failed"); compressedBuffer[targetSize] = endCheck; @@ -435,7 +435,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c if (targetSize>0) { /* check correctness */ U32 const crcBase = XXH32(block, (size_t)srcSize, 0); - char const canary = FUZ_rand(&randState) & 255; + char const canary = (char)(FUZ_rand(&randState) & 255); FUZ_CHECKTEST((ret==0), "LZ4_compress_HC_destSize() compression failed"); FUZ_DISPLAYTEST(); compressedSize = ret; @@ -444,7 +444,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c FUZ_CHECKTEST(ret<0, "LZ4_decompress_safe() failed on data compressed by LZ4_compressHC_destSize"); FUZ_CHECKTEST(ret!=srcSize, "LZ4_decompress_safe() failed : did not fully decompressed data"); FUZ_CHECKTEST(decodedBuffer[srcSize] != canary, "LZ4_decompress_safe() overwrite dst buffer !"); - { U32 const crcDec = XXH32(decodedBuffer, srcSize, 0); + { U32 const crcDec = XXH32(decodedBuffer, (size_t)srcSize, 0); FUZ_CHECKTEST(crcDec!=crcBase, "LZ4_decompress_safe() corrupted decoded data"); } DISPLAYLEVEL(5, " OK \n"); @@ -524,7 +524,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c FUZ_CHECKTEST(r!=blockSize, "LZ4_decompress_safe did not regenerate original data"); } FUZ_CHECKTEST(decodedBuffer[blockSize], "LZ4_decompress_safe overrun specified output buffer size"); - { U32 const crcCheck = XXH32(decodedBuffer, blockSize, 0); + { U32 const crcCheck = XXH32(decodedBuffer, (size_t)blockSize, 0); FUZ_CHECKTEST(crcCheck!=crcOrig, "LZ4_decompress_safe corrupted decoded data"); } @@ -966,7 +966,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c #define testInputSize (192 KB) -#define testCompressedSize (128 KB) +#define testCompressedSize (130 KB) #define ringBufferSize (8 KB) static void FUZ_unitTests(int compressionLevel) @@ -990,17 +990,17 @@ static void FUZ_unitTests(int compressionLevel) FUZ_AddressOverflow(); /* Test decoding with empty input */ - DISPLAYLEVEL(3, "LZ4_decompress_safe() with empty input"); + DISPLAYLEVEL(3, "LZ4_decompress_safe() with empty input \n"); LZ4_decompress_safe(testCompressed, testVerify, 0, testInputSize); /* Test decoding with a one byte input */ - DISPLAYLEVEL(3, "LZ4_decompress_safe() with one byte input"); + DISPLAYLEVEL(3, "LZ4_decompress_safe() with one byte input \n"); { char const tmp = (char)0xFF; LZ4_decompress_safe(&tmp, testVerify, 1, testInputSize); } /* Test decoding shortcut edge case */ - DISPLAYLEVEL(3, "LZ4_decompress_safe() with shortcut edge case"); + DISPLAYLEVEL(3, "LZ4_decompress_safe() with shortcut edge case \n"); { char tmp[17]; /* 14 bytes of literals, followed by a 14 byte match. * Should not read beyond the end of the buffer. @@ -1013,6 +1013,31 @@ static void FUZ_unitTests(int compressionLevel) FUZ_CHECKTEST(r >= 0, "LZ4_decompress_safe() should fail"); } } + /* in-place compression test */ + DISPLAYLEVEL(3, "in-place compression using LZ4_compress_default() :"); + { size_t const sampleSize = 65 KB; + size_t const maxCSize = LZ4_COMPRESSBOUND(sampleSize); + size_t const margin = 64 KB; + size_t const outSize = maxCSize + margin; + size_t const startIndex = outSize - sampleSize; + char* const startInput = testCompressed + startIndex; + XXH32_hash_t const crcOrig = XXH32(testInput, sampleSize, 0); + int cSize; + assert(outSize < testCompressedSize); + memcpy(startInput, testInput, sampleSize); /* copy at end of buffer */ + /* compress in-place */ + cSize = LZ4_compress_default(startInput, testCompressed, sampleSize, maxCSize); + assert(cSize != 0); /* ensure compression is successful */ + assert(maxCSize < INT_MAX); + assert(cSize <= (int)maxCSize); + /* decompress and verify */ + { int const dSize = LZ4_decompress_safe(testCompressed, testVerify, cSize, testInputSize); + assert(dSize == sampleSize); /* correct size */ + { XXH32_hash_t const crcCheck = XXH32(testVerify, (size_t)dSize, 0); + assert(crcCheck == crcOrig); + } } } + DISPLAYLEVEL(3, " OK \n"); + /* LZ4 streaming tests */ { LZ4_stream_t streamingState; U64 crcOrig; @@ -1061,17 +1086,17 @@ static void FUZ_unitTests(int compressionLevel) crcOrig = XXH64_digest(&xxhOrig); memcpy (ringBuffer + rNext, testInput + iNext, messageSize); - compressedSize = LZ4_compress_fast_continue(&streamingState, ringBuffer + rNext, testCompressed, messageSize, testCompressedSize-ringBufferSize, 1); + compressedSize = LZ4_compress_fast_continue(&streamingState, ringBuffer + rNext, testCompressed, (int)messageSize, testCompressedSize-ringBufferSize, 1); FUZ_CHECKTEST(compressedSize==0, "LZ4_compress_fast_continue() compression failed"); - result = LZ4_decompress_safe_continue(&decodeStateSafe, testCompressed, testVerify + dNext, compressedSize, messageSize); + result = LZ4_decompress_safe_continue(&decodeStateSafe, testCompressed, testVerify + dNext, compressedSize, (int)messageSize); FUZ_CHECKTEST(result!=(int)messageSize, "ringBuffer : LZ4_decompress_safe_continue() test failed"); XXH64_update(&xxhNewSafe, testVerify + dNext, messageSize); { U64 const crcNew = XXH64_digest(&xxhNewSafe); FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe_continue() decompression corruption"); } - result = LZ4_decompress_fast_continue(&decodeStateFast, testCompressed, testVerify + dNext, messageSize); + result = LZ4_decompress_fast_continue(&decodeStateFast, testCompressed, testVerify + dNext, (int)messageSize); FUZ_CHECKTEST(result!=compressedSize, "ringBuffer : LZ4_decompress_fast_continue() test failed"); XXH64_update(&xxhNewFast, testVerify + dNext, messageSize); -- cgit v1.2.3 From b17f578a919b7e6b078cede2d52be29dd48c8e8c Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 29 May 2019 12:06:13 -0700 Subject: added comments and macros for in-place (de)compression --- doc/lz4_manual.html | 38 ++++++++++++++++++++++++++++++++++++-- lib/lz4.c | 4 ---- lib/lz4.h | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- tests/fuzzer.c | 3 +-- 4 files changed, 83 insertions(+), 10 deletions(-) diff --git a/doc/lz4_manual.html b/doc/lz4_manual.html index 3a9e0db..0530966 100644 --- a/doc/lz4_manual.html +++ b/doc/lz4_manual.html @@ -40,9 +40,9 @@ Blocks are different from Frames (doc/lz4_Frame_format.md). Frames bundle both blocks and metadata in a specified manner. - This are required for compressed data to be self-contained and portable. + Embedding metadata is required for compressed data to be self-contained and portable. Frame format is delivered through a companion API, declared in lz4frame.h. - Note that the `lz4` CLI can only manage frames. + The `lz4` CLI can only manage frames.

    Version

    
    @@ -357,6 +357,40 @@ int                 LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream);
      
     


    +

    + It's possible to have input and output sharing the same buffer, + for highly contrained memory environments. + In both cases, it requires input to lay at the end of the buffer, + and buffer must have some margin, hence be larger than final size. + + This technique is more useful for decompression, + since decompressed size is typically larger, + and margin is mostly required to avoid stripe overflow, so it's short. + + For compression though, margin must be able to cope with both + history preservation, requiring input data to remain unmodified up to LZ4_DISTANCE_MAX, + and data expansion, which can happen when input is not compressible. + As a consequence, buffer size requirements are much higher than average compressed size, + hence memory savings are limited. + + There are ways to limit this cost for compression : + - Reduce history size, by modifying LZ4_DISTANCE_MAX. + Lower values will also reduce compression ratio, except when input_size < LZ4_DISTANCE_MAX, + so it's a reasonable trick when inputs are known to be small. + - Require the compressor to deliver a "maximum compressed size". + When this size is < LZ4_COMPRESSBOUND(inputSize), then compression can fail, + in which case, the return code will be 0 (zero). + The caller must be ready for these cases to happen, + and typically design a backup scheme to send data uncompressed. + The combination of both techniques can significantly reduce + the amount of margin required for in-place compression. + +


    + +
    #define LZ4_DECOMPRESS_INPLACE_BUFFER_SIZE(decompressedSize)   ( (decompressedSize) + LZ4_DECOMPRESS_INPLACE_MARGIN)  /**< note: presumes that compressedSize < decompressedSize */
    +

    +
    #define LZ4_COMPRESS_INPLACE_BUFFER_SIZE(maxCompressedSize)   ( (maxCompressedSize) + LZ4_COMPRESS_INPLACE_MARGIN)  /**< maxCompressedSize is generally LZ4_COMPRESSBOUND(inputSize), but can be set to any lower value, with the risk that compression can fail (return code 0(zero)) */
    +

    PRIVATE DEFINITIONS

      Do not use these definitions directly.
      They are only exposed to allow static allocation of `LZ4_stream_t` and `LZ4_streamDecode_t`.
    diff --git a/lib/lz4.c b/lib/lz4.c
    index 070dd7e..19070c2 100644
    --- a/lib/lz4.c
    +++ b/lib/lz4.c
    @@ -412,10 +412,6 @@ static const int LZ4_minLength = (MFLIMIT+1);
     #define MB *(1 <<20)
     #define GB *(1U<<30)
     
    -#ifndef LZ4_DISTANCE_MAX   /* can be user - defined at compile time */
    -#  define LZ4_DISTANCE_MAX 65535
    -#endif
    -
     #if (LZ4_DISTANCE_MAX > 65535)   /* max supported by LZ4 format */
     #  error "LZ4_DISTANCE_MAX is too big : must be <= 65535"
     #endif
    diff --git a/lib/lz4.h b/lib/lz4.h
    index 6064967..ae7f52c 100644
    --- a/lib/lz4.h
    +++ b/lib/lz4.h
    @@ -65,9 +65,9 @@ extern "C" {
     
       Blocks are different from Frames (doc/lz4_Frame_format.md).
       Frames bundle both blocks and metadata in a specified manner.
    -  This are required for compressed data to be self-contained and portable.
    +  Embedding metadata is required for compressed data to be self-contained and portable.
       Frame format is delivered through a companion API, declared in lz4frame.h.
    -  Note that the `lz4` CLI can only manage frames.
    +  The `lz4` CLI can only manage frames.
     */
     
     /*^***************************************************************
    @@ -462,8 +462,51 @@ LZ4LIB_STATIC_API int LZ4_compress_fast_extState_fastReset (void* state, const c
      */
     LZ4LIB_STATIC_API void LZ4_attach_dictionary(LZ4_stream_t* workingStream, const LZ4_stream_t* dictionaryStream);
     
    +
    +/*! In-place compression and decompression
    + *
    + * It's possible to have input and output sharing the same buffer,
    + * for highly contrained memory environments.
    + * In both cases, it requires input to lay at the end of the buffer,
    + * and buffer must have some margin, hence be larger than final size.
    + *
    + * This technique is more useful for decompression,
    + * since decompressed size is typically larger,
    + * and margin is mostly required to avoid stripe overflow, so it's short.
    + *
    + * For compression though, margin must be able to cope with both
    + * history preservation, requiring input data to remain unmodified up to LZ4_DISTANCE_MAX,
    + * and data expansion, which can happen when input is not compressible.
    + * As a consequence, buffer size requirements are much higher than average compressed size,
    + * hence memory savings are limited.
    + *
    + * There are ways to limit this cost for compression :
    + * - Reduce history size, by modifying LZ4_DISTANCE_MAX.
    + *   Lower values will also reduce compression ratio, except when input_size < LZ4_DISTANCE_MAX,
    + *   so it's a reasonable trick when inputs are known to be small.
    + * - Require the compressor to deliver a "maximum compressed size".
    + *   When this size is < LZ4_COMPRESSBOUND(inputSize), then compression can fail,
    + *   in which case, the return code will be 0 (zero).
    + *   The caller must be ready for these cases to happen,
    + *   and typically design a backup scheme to send data uncompressed.
    + * The combination of both techniques can significantly reduce
    + * the amount of margin required for in-place compression.
    + */
    +
    +#define LZ4_DECOMPRESS_INPLACE_MARGIN 32
    +#define LZ4_DECOMPRESS_INPLACE_BUFFER_SIZE(decompressedSize)   ( (decompressedSize) + LZ4_DECOMPRESS_INPLACE_MARGIN)  /**< note: presumes that compressedSize < decompressedSize */
    +
    +#ifndef LZ4_DISTANCE_MAX   /* history window size; can be user-defined at compile time */
    +#  define LZ4_DISTANCE_MAX 65535   /* set to maximum value by default */
     #endif
     
    +#define LZ4_COMPRESS_INPLACE_MARGIN (LZ4_DISTANCE_MAX + 32)
    +#define LZ4_COMPRESS_INPLACE_BUFFER_SIZE(maxCompressedSize)   ( (maxCompressedSize) + LZ4_COMPRESS_INPLACE_MARGIN)  /**< maxCompressedSize is generally LZ4_COMPRESSBOUND(inputSize), but can be set to any lower value, with the risk that compression can fail (return code 0(zero)) */
    +
    +
    +#endif   /* LZ4_STATIC_LINKING_ONLY */
    +
    +
     
     /*-************************************************************
      *  PRIVATE DEFINITIONS
    @@ -567,6 +610,7 @@ union LZ4_streamDecode_u {
     } ;   /* previously typedef'd to LZ4_streamDecode_t */
     
     
    +
     /*-************************************
     *  Obsolete Functions
     **************************************/
    diff --git a/tests/fuzzer.c b/tests/fuzzer.c
    index 26e25eb..e04caa5 100644
    --- a/tests/fuzzer.c
    +++ b/tests/fuzzer.c
    @@ -1017,8 +1017,7 @@ static void FUZ_unitTests(int compressionLevel)
         DISPLAYLEVEL(3, "in-place compression using LZ4_compress_default() :");
         {   size_t const sampleSize = 65 KB;
             size_t const maxCSize = LZ4_COMPRESSBOUND(sampleSize);
    -        size_t const margin = 64 KB;
    -        size_t const outSize = maxCSize + margin;
    +        size_t const outSize = LZ4_COMPRESS_INPLACE_BUFFER_SIZE(maxCSize);
             size_t const startIndex = outSize - sampleSize;
             char*  const startInput = testCompressed + startIndex;
             XXH32_hash_t const crcOrig = XXH32(testInput, sampleSize, 0);
    -- 
    cgit v1.2.3
    
    
    From 444550defad26bcbe2e62209badbba76e803e521 Mon Sep 17 00:00:00 2001
    From: Yann Collet 
    Date: Wed, 29 May 2019 12:21:14 -0700
    Subject: ensure lz4.h can be included with or without LZ4_STATIC_LINKING_ONLY
     in any order
    
    ensure correct propagation of LZ4_DISTANCE_MAX
    ---
     lib/lz4.c   |  1 +
     lib/lz4.h   | 16 ++++++++++++----
     lib/lz4hc.h |  3 +++
     3 files changed, 16 insertions(+), 4 deletions(-)
    
    diff --git a/lib/lz4.c b/lib/lz4.c
    index 19070c2..a198a03 100644
    --- a/lib/lz4.c
    +++ b/lib/lz4.c
    @@ -106,6 +106,7 @@
     #define LZ4_DISABLE_DEPRECATE_WARNINGS /* due to LZ4_decompress_safe_withPrefix64k */
     #endif
     
    +#define LZ4_STATIC_LINKING_ONLY  /* LZ4_DISTANCE_MAX */
     #include "lz4.h"
     /* see also "memory routines" below */
     
    diff --git a/lib/lz4.h b/lib/lz4.h
    index ae7f52c..1e24ce9 100644
    --- a/lib/lz4.h
    +++ b/lib/lz4.h
    @@ -388,6 +388,8 @@ LZ4LIB_API int LZ4_decompress_safe_continue (LZ4_streamDecode_t* LZ4_streamDecod
      */
     LZ4LIB_API int LZ4_decompress_safe_usingDict (const char* src, char* dst, int srcSize, int dstCapcity, const char* dictStart, int dictSize);
     
    +#endif /* LZ4_H_2983827168210 */
    +
     
     /*^*************************************
      * !!!!!!   STATIC LINKING ONLY   !!!!!!
    @@ -413,14 +415,17 @@ LZ4LIB_API int LZ4_decompress_safe_usingDict (const char* src, char* dst, int sr
      * define LZ4_PUBLISH_STATIC_FUNCTIONS when building the LZ4 library.
      ******************************************************************************/
     
    +#ifdef LZ4_STATIC_LINKING_ONLY
    +
    +#ifndef LZ4_STATIC_3504398509
    +#define LZ4_STATIC_3504398509
    +
     #ifdef LZ4_PUBLISH_STATIC_FUNCTIONS
     #define LZ4LIB_STATIC_API LZ4LIB_API
     #else
     #define LZ4LIB_STATIC_API
     #endif
     
    -#ifdef LZ4_STATIC_LINKING_ONLY
    -
     
     /*! LZ4_compress_fast_extState_fastReset() :
      *  A variant of LZ4_compress_fast_extState().
    @@ -503,11 +508,14 @@ LZ4LIB_STATIC_API void LZ4_attach_dictionary(LZ4_stream_t* workingStream, const
     #define LZ4_COMPRESS_INPLACE_MARGIN (LZ4_DISTANCE_MAX + 32)
     #define LZ4_COMPRESS_INPLACE_BUFFER_SIZE(maxCompressedSize)   ( (maxCompressedSize) + LZ4_COMPRESS_INPLACE_MARGIN)  /**< maxCompressedSize is generally LZ4_COMPRESSBOUND(inputSize), but can be set to any lower value, with the risk that compression can fail (return code 0(zero)) */
     
    -
    +#endif   /* LZ4_STATIC_3504398509 */
     #endif   /* LZ4_STATIC_LINKING_ONLY */
     
     
     
    +#ifndef LZ4_H_98237428734687
    +#define LZ4_H_98237428734687
    +
     /*-************************************************************
      *  PRIVATE DEFINITIONS
      **************************************************************
    @@ -718,7 +726,7 @@ LZ4LIB_API int LZ4_decompress_fast_usingDict (const char* src, char* dst, int or
     LZ4LIB_API void LZ4_resetStream (LZ4_stream_t* streamPtr);
     
     
    -#endif /* LZ4_H_2983827168210 */
    +#endif /* LZ4_H_98237428734687 */
     
     
     #if defined (__cplusplus)
    diff --git a/lib/lz4hc.h b/lib/lz4hc.h
    index cdc6d89..44e35bb 100644
    --- a/lib/lz4hc.h
    +++ b/lib/lz4hc.h
    @@ -336,6 +336,9 @@ LZ4LIB_API void LZ4_resetStreamHC (LZ4_streamHC_t* streamHCPtr, int compressionL
     #ifndef LZ4_HC_SLO_098092834
     #define LZ4_HC_SLO_098092834
     
    +#define LZ4_STATIC_LINKING_ONLY   /* LZ4LIB_STATIC_API */
    +#include "lz4.h"
    +
     #if defined (__cplusplus)
     extern "C" {
     #endif
    -- 
    cgit v1.2.3
    
    
    From c5bcb4d68b44bd276523cd354424b89efe511b76 Mon Sep 17 00:00:00 2001
    From: Yann Collet 
    Date: Wed, 29 May 2019 12:56:27 -0700
    Subject: fixed minor conversion warning
    
    ---
     tests/fuzzer.c | 2 +-
     1 file changed, 1 insertion(+), 1 deletion(-)
    
    diff --git a/tests/fuzzer.c b/tests/fuzzer.c
    index e04caa5..8107cb8 100644
    --- a/tests/fuzzer.c
    +++ b/tests/fuzzer.c
    @@ -1031,7 +1031,7 @@ static void FUZ_unitTests(int compressionLevel)
             assert(cSize <= (int)maxCSize);
             /* decompress and verify */
             {   int const dSize = LZ4_decompress_safe(testCompressed, testVerify, cSize, testInputSize);
    -            assert(dSize == sampleSize);   /* correct size */
    +            assert(dSize == (int)sampleSize);   /* correct size */
                 {   XXH32_hash_t const crcCheck = XXH32(testVerify, (size_t)dSize, 0);
                     assert(crcCheck == crcOrig);
         }   }   }
    -- 
    cgit v1.2.3
    
    
    From 76116495bfd2096f06c923ebe865c59e42f32b07 Mon Sep 17 00:00:00 2001
    From: Yann Collet 
    Date: Wed, 29 May 2019 13:14:52 -0700
    Subject: some more minor conversion warnings fixes
    
    ---
     Makefile       |  2 +-
     lib/lz4.c      |  8 ++++----
     tests/fuzzer.c | 10 +++++-----
     3 files changed, 10 insertions(+), 10 deletions(-)
    
    diff --git a/Makefile b/Makefile
    index e24cec5..3e55046 100644
    --- a/Makefile
    +++ b/Makefile
    @@ -49,7 +49,7 @@ allmost: lib lz4
     .PHONY: lib lib-release liblz4.a
     lib: liblz4.a
     lib lib-release liblz4.a:
    -	@$(MAKE) -C $(LZ4DIR) $@
    +	@CFLAGS="$(CFLAGS)" $(MAKE) -C $(LZ4DIR) $@
     
     .PHONY: lz4 lz4-release
     lz4 : liblz4.a
    diff --git a/lib/lz4.c b/lib/lz4.c
    index a198a03..4451bfc 100644
    --- a/lib/lz4.c
    +++ b/lib/lz4.c
    @@ -462,7 +462,7 @@ static unsigned LZ4_NbCommonBytes (reg_t val)
                 _BitScanForward64( &r, (U64)val );
                 return (int)(r>>3);
     #       elif (defined(__clang__) || (defined(__GNUC__) && (__GNUC__>=3))) && !defined(LZ4_FORCE_SW_BITCOUNT)
    -            return (__builtin_ctzll((U64)val) >> 3);
    +            return (unsigned)__builtin_ctzll((U64)val) >> 3;
     #       else
                 static const int DeBruijnBytePos[64] = { 0, 0, 0, 0, 0, 1, 1, 2,
                                                          0, 3, 1, 3, 1, 4, 2, 7,
    @@ -480,7 +480,7 @@ static unsigned LZ4_NbCommonBytes (reg_t val)
                 _BitScanForward( &r, (U32)val );
                 return (int)(r>>3);
     #       elif (defined(__clang__) || (defined(__GNUC__) && (__GNUC__>=3))) && !defined(LZ4_FORCE_SW_BITCOUNT)
    -            return (__builtin_ctz((U32)val) >> 3);
    +            return (unsigned)__builtin_ctz((U32)val) >> 3;
     #       else
                 static const int DeBruijnBytePos[32] = { 0, 0, 3, 0, 3, 1, 3, 0,
                                                          3, 2, 2, 1, 3, 2, 0, 1,
    @@ -496,7 +496,7 @@ static unsigned LZ4_NbCommonBytes (reg_t val)
                 _BitScanReverse64( &r, val );
                 return (unsigned)(r>>3);
     #       elif (defined(__clang__) || (defined(__GNUC__) && (__GNUC__>=3))) && !defined(LZ4_FORCE_SW_BITCOUNT)
    -            return (__builtin_clzll((U64)val) >> 3);
    +            return (unsigned)__builtin_clzll((U64)val) >> 3;
     #       else
                 static const U32 by32 = sizeof(val)*4;  /* 32 on 64 bits (goal), 16 on 32 bits.
                     Just to avoid some static analyzer complaining about shift by 32 on 32-bits target.
    @@ -513,7 +513,7 @@ static unsigned LZ4_NbCommonBytes (reg_t val)
                 _BitScanReverse( &r, (unsigned long)val );
                 return (unsigned)(r>>3);
     #       elif (defined(__clang__) || (defined(__GNUC__) && (__GNUC__>=3))) && !defined(LZ4_FORCE_SW_BITCOUNT)
    -            return (__builtin_clz((U32)val) >> 3);
    +            return (unsigned)__builtin_clz((U32)val) >> 3;
     #       else
                 unsigned r;
                 if (!(val>>16)) { r=2; val>>=8; } else { r=0; val>>=24; }
    diff --git a/tests/fuzzer.c b/tests/fuzzer.c
    index 8107cb8..9225e5b 100644
    --- a/tests/fuzzer.c
    +++ b/tests/fuzzer.c
    @@ -1015,10 +1015,10 @@ static void FUZ_unitTests(int compressionLevel)
     
         /* in-place compression test */
         DISPLAYLEVEL(3, "in-place compression using LZ4_compress_default() :");
    -    {   size_t const sampleSize = 65 KB;
    -        size_t const maxCSize = LZ4_COMPRESSBOUND(sampleSize);
    -        size_t const outSize = LZ4_COMPRESS_INPLACE_BUFFER_SIZE(maxCSize);
    -        size_t const startIndex = outSize - sampleSize;
    +    {   int const sampleSize = 65 KB;
    +        int const maxCSize = LZ4_COMPRESSBOUND(sampleSize);
    +        int const outSize = LZ4_COMPRESS_INPLACE_BUFFER_SIZE(maxCSize);
    +        int const startIndex = outSize - sampleSize;
             char*  const startInput = testCompressed + startIndex;
             XXH32_hash_t const crcOrig = XXH32(testInput, sampleSize, 0);
             int cSize;
    @@ -1028,7 +1028,7 @@ static void FUZ_unitTests(int compressionLevel)
             cSize = LZ4_compress_default(startInput, testCompressed, sampleSize, maxCSize);
             assert(cSize != 0);  /* ensure compression is successful */
             assert(maxCSize < INT_MAX);
    -        assert(cSize <= (int)maxCSize);
    +        assert(cSize <= maxCSize);
             /* decompress and verify */
             {   int const dSize = LZ4_decompress_safe(testCompressed, testVerify, cSize, testInputSize);
                 assert(dSize == (int)sampleSize);   /* correct size */
    -- 
    cgit v1.2.3
    
    
    From 45eba5b0303c7a960b46fa07c3f578f5051826d8 Mon Sep 17 00:00:00 2001
    From: Yann Collet 
    Date: Wed, 29 May 2019 13:17:45 -0700
    Subject: one more conversion warning
    
    ---
     tests/fuzzer.c | 2 +-
     1 file changed, 1 insertion(+), 1 deletion(-)
    
    diff --git a/tests/fuzzer.c b/tests/fuzzer.c
    index 9225e5b..0cd651d 100644
    --- a/tests/fuzzer.c
    +++ b/tests/fuzzer.c
    @@ -1022,7 +1022,7 @@ static void FUZ_unitTests(int compressionLevel)
             char*  const startInput = testCompressed + startIndex;
             XXH32_hash_t const crcOrig = XXH32(testInput, sampleSize, 0);
             int cSize;
    -        assert(outSize < testCompressedSize);
    +        assert(outSize < (int)testCompressedSize);
             memcpy(startInput, testInput, sampleSize);  /* copy at end of buffer */
             /* compress in-place */
             cSize = LZ4_compress_default(startInput, testCompressed, sampleSize, maxCSize);
    -- 
    cgit v1.2.3
    
    
    From b2ba857a4f295af79f94207cab1b9febec14a380 Mon Sep 17 00:00:00 2001
    From: Yann Collet 
    Date: Wed, 29 May 2019 13:33:55 -0700
    Subject: fuzzer: changed internal buffer size
    
    to ensure no overflow during unit tests
    ---
     tests/fuzzer.c | 19 ++++++++++---------
     1 file changed, 10 insertions(+), 9 deletions(-)
    
    diff --git a/tests/fuzzer.c b/tests/fuzzer.c
    index 0cd651d..ba995e7 100644
    --- a/tests/fuzzer.c
    +++ b/tests/fuzzer.c
    @@ -965,7 +965,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c
     }
     
     
    -#define testInputSize (192 KB)
    +#define testInputSize (196 KB)
     #define testCompressedSize (130 KB)
     #define ringBufferSize (8 KB)
     
    @@ -1175,12 +1175,12 @@ static void FUZ_unitTests(int compressionLevel)
             {   U64 const crc64 = XXH64(testInput + 64 KB, testCompressedSize, 0);
                 LZ4_resetStreamHC_fast(&sHC, compressionLevel);
                 LZ4_loadDictHC(&sHC, testInput, 64 KB);
    -            result = LZ4_compress_HC_continue(&sHC, testInput + 64 KB, testCompressed, testCompressedSize, testCompressedSize-1);
    -            FUZ_CHECKTEST(result==0, "LZ4_compressHC_limitedOutput_continue() dictionary compression failed : result = %i", result);
    -            FUZ_CHECKTEST(sHC.internal_donotuse.dirty, "Context should be clean");
    -
    -            result = LZ4_decompress_safe_usingDict(testCompressed, testVerify, result, testCompressedSize, testInput, 64 KB);
    -            FUZ_CHECKTEST(result!=(int)testCompressedSize, "LZ4_decompress_safe() simple dictionary decompression test failed");
    +            {   int const cSize = LZ4_compress_HC_continue(&sHC, testInput + 64 KB, testCompressed, testCompressedSize, testCompressedSize-1);
    +                FUZ_CHECKTEST(cSize==0, "LZ4_compressHC_limitedOutput_continue() dictionary compression failed : @return = %i", cSize);
    +                FUZ_CHECKTEST(sHC.internal_donotuse.dirty, "Context should be clean");
    +                {   int const dSize = LZ4_decompress_safe_usingDict(testCompressed, testVerify, cSize, testCompressedSize, testInput, 64 KB);
    +                    FUZ_CHECKTEST(dSize!=(int)testCompressedSize, "LZ4_decompress_safe() simple dictionary decompression test failed");
    +            }   }
                 {   U64 const crcNew = XXH64(testVerify, testCompressedSize, 0);
                     FUZ_CHECKTEST(crc64!=crcNew, "LZ4_decompress_safe() simple dictionary decompression test : corruption");
             }   }
    @@ -1189,7 +1189,8 @@ static void FUZ_unitTests(int compressionLevel)
             /* multiple HC compression test with dictionary */
             {   int result1, result2;
                 int segSize = testCompressedSize / 2;
    -            U64 const crc64 = XXH64(testInput + segSize, testCompressedSize, 0);
    +            XXH64_hash_t const crc64 = ( (void)assert((unsigned)segSize + testCompressedSize < testInputSize) ,
    +                                        XXH64(testInput + segSize, testCompressedSize, 0) );
                 LZ4_resetStreamHC_fast(&sHC, compressionLevel);
                 LZ4_loadDictHC(&sHC, testInput, segSize);
                 result1 = LZ4_compress_HC_continue(&sHC, testInput + segSize, testCompressed, segSize, segSize -1);
    @@ -1203,7 +1204,7 @@ static void FUZ_unitTests(int compressionLevel)
                 FUZ_CHECKTEST(result!=segSize, "LZ4_decompress_safe() dictionary decompression part 1 failed");
                 result = LZ4_decompress_safe_usingDict(testCompressed+result1, testVerify+segSize, result2, segSize, testInput, 2*segSize);
                 FUZ_CHECKTEST(result!=segSize, "LZ4_decompress_safe() dictionary decompression part 2 failed");
    -            {   U64 const crcNew = XXH64(testVerify, testCompressedSize, 0);
    +            {   XXH64_hash_t const crcNew = XXH64(testVerify, testCompressedSize, 0);
                     FUZ_CHECKTEST(crc64!=crcNew, "LZ4_decompress_safe() dictionary decompression corruption");
             }   }
     
    -- 
    cgit v1.2.3
    
    
    From 22adbb176afa46ebe1b799f7758381da8461bfe4 Mon Sep 17 00:00:00 2001
    From: Yann Collet 
    Date: Thu, 30 May 2019 09:45:21 -0700
    Subject: add more doc on in-place (de)compression
    
    ---
     doc/lz4_manual.html | 30 +++++++++++++++++++++++++-----
     lib/lz4.h           | 30 +++++++++++++++++++++++++-----
     2 files changed, 50 insertions(+), 10 deletions(-)
    
    diff --git a/doc/lz4_manual.html b/doc/lz4_manual.html
    index 0530966..091f537 100644
    --- a/doc/lz4_manual.html
    +++ b/doc/lz4_manual.html
    @@ -361,21 +361,35 @@ int                 LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream);
      It's possible to have input and output sharing the same buffer,
      for highly contrained memory environments.
      In both cases, it requires input to lay at the end of the buffer,
    - and buffer must have some margin, hence be larger than final size.
    + and decompression to start at beginning of the buffer.
    + Buffer size must feature some margin, hence be larger than final size.
    +
    + |<------------------------buffer----------------------------------->|
    +                             |<------------compressed data---------->|
    + |<-----------decompressed size------------------>|
    +                                                  |<-----margin----->|
     
      This technique is more useful for decompression,
      since decompressed size is typically larger,
      and margin is mostly required to avoid stripe overflow, so it's short.
     
    - For compression though, margin must be able to cope with both
    + In-place decompression will work inside any buffer
    + which size is >= LZ4_DECOMPRESS_INPLACE_BUFFER_SIZE(decompressedSize).
    + This presumes that decompressedSize > compressedSize.
    + Otherwise, it means compression actually expanded data,
    + which can happen when data is not compressible (already compressed, or encrypted),
    + and it would be more efficient to store such data with a flag indicating it's not compressed.
    +
    + For compression, margin is larger, as it must be able to cope with both
      history preservation, requiring input data to remain unmodified up to LZ4_DISTANCE_MAX,
      and data expansion, which can happen when input is not compressible.
    - As a consequence, buffer size requirements are much higher than average compressed size,
    - hence memory savings are limited.
    + As a consequence, buffer size requirements are much higher,
    + and memory savings offered by in-place compression are more limited.
     
      There are ways to limit this cost for compression :
      - Reduce history size, by modifying LZ4_DISTANCE_MAX.
    -   Lower values will also reduce compression ratio, except when input_size < LZ4_DISTANCE_MAX,
    +   Note that it is a compile-time constant, so all future compression will apply this parameter.
    +   Lower values will reduce compression ratio, except when input_size < LZ4_DISTANCE_MAX,
        so it's a reasonable trick when inputs are known to be small.
      - Require the compressor to deliver a "maximum compressed size".
        When this size is < LZ4_COMPRESSBOUND(inputSize), then compression can fail,
    @@ -384,6 +398,12 @@ int                 LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream);
        and typically design a backup scheme to send data uncompressed.
      The combination of both techniques can significantly reduce
      the amount of margin required for in-place compression.
    +
    + In-place compression can work in any buffer
    + which size is >= LZ4_COMPRESS_INPLACE_BUFFER_SIZE(maxCompressedSize)
    + with maxCompressedSize == LZ4_COMPRESSBOUND(srcSize) for guaranteed compression success.
    + This macro depends on both maxCompressedSize and LZ4_DISTANCE_MAX,
    + so there are ways to reduce memory requirements by playing with them.
      
     


    diff --git a/lib/lz4.h b/lib/lz4.h index 1e24ce9..91dcf64 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -473,21 +473,35 @@ LZ4LIB_STATIC_API void LZ4_attach_dictionary(LZ4_stream_t* workingStream, const * It's possible to have input and output sharing the same buffer, * for highly contrained memory environments. * In both cases, it requires input to lay at the end of the buffer, - * and buffer must have some margin, hence be larger than final size. + * and decompression to start at beginning of the buffer. + * Buffer size must feature some margin, hence be larger than final size. + * + * |<------------------------buffer----------------------------------->| + * |<------------compressed data---------->| + * |<-----------decompressed size------------------>| + * |<-----margin----->| * * This technique is more useful for decompression, * since decompressed size is typically larger, * and margin is mostly required to avoid stripe overflow, so it's short. * - * For compression though, margin must be able to cope with both + * In-place decompression will work inside any buffer + * which size is >= LZ4_DECOMPRESS_INPLACE_BUFFER_SIZE(decompressedSize). + * This presumes that decompressedSize > compressedSize. + * Otherwise, it means compression actually expanded data, + * which can happen when data is not compressible (already compressed, or encrypted), + * and it would be more efficient to store such data with a flag indicating it's not compressed. + * + * For compression, margin is larger, as it must be able to cope with both * history preservation, requiring input data to remain unmodified up to LZ4_DISTANCE_MAX, * and data expansion, which can happen when input is not compressible. - * As a consequence, buffer size requirements are much higher than average compressed size, - * hence memory savings are limited. + * As a consequence, buffer size requirements are much higher, + * and memory savings offered by in-place compression are more limited. * * There are ways to limit this cost for compression : * - Reduce history size, by modifying LZ4_DISTANCE_MAX. - * Lower values will also reduce compression ratio, except when input_size < LZ4_DISTANCE_MAX, + * Note that it is a compile-time constant, so all future compression will apply this parameter. + * Lower values will reduce compression ratio, except when input_size < LZ4_DISTANCE_MAX, * so it's a reasonable trick when inputs are known to be small. * - Require the compressor to deliver a "maximum compressed size". * When this size is < LZ4_COMPRESSBOUND(inputSize), then compression can fail, @@ -496,6 +510,12 @@ LZ4LIB_STATIC_API void LZ4_attach_dictionary(LZ4_stream_t* workingStream, const * and typically design a backup scheme to send data uncompressed. * The combination of both techniques can significantly reduce * the amount of margin required for in-place compression. + * + * In-place compression can work in any buffer + * which size is >= LZ4_COMPRESS_INPLACE_BUFFER_SIZE(maxCompressedSize) + * with maxCompressedSize == LZ4_COMPRESSBOUND(srcSize) for guaranteed compression success. + * This macro depends on both maxCompressedSize and LZ4_DISTANCE_MAX, + * so there are ways to reduce memory requirements by playing with them. */ #define LZ4_DECOMPRESS_INPLACE_MARGIN 32 -- cgit v1.2.3 From 6c69ae6bd69ee66b9e5051b4f88d533999240553 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 30 May 2019 16:17:47 -0700 Subject: added test case for in-place decompression worst case, designed to make the decoder overwrite into input --- tests/fuzzer.c | 95 ++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 63 insertions(+), 32 deletions(-) diff --git a/tests/fuzzer.c b/tests/fuzzer.c index ba995e7..b45620b 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -207,7 +207,7 @@ static int FUZ_AddressOverflow(void) } { size_t const sizeToGenerateOverflow = (size_t)(- ((uintptr_t)buffers[nbBuff-1]) + 512); - unsigned const nbOf255 = (unsigned)((sizeToGenerateOverflow / 255) + 1); + int const nbOf255 = (int)((sizeToGenerateOverflow / 255) + 1); char* const input = buffers[nbBuff-1]; char* output = buffers[nbBuff]; int r; @@ -215,7 +215,7 @@ static int FUZ_AddressOverflow(void) input[1] = (char)0xFF; input[2] = (char)0xFF; input[3] = (char)0xFF; - { unsigned u; for(u = 4; u <= nbOf255+4; u++) input[u] = (char)0xff; } + { int u; for(u = 4; u <= nbOf255+4; u++) input[u] = (char)0xff; } r = LZ4_decompress_safe(input, output, nbOf255+64, BLOCKSIZE_I134); if (r>0) { DISPLAY("LZ4_decompress_safe = %i \n", r); goto _overflowError; } input[0] = (char)0x1F; /* Match length overflow */ @@ -366,7 +366,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c U32 testNb = 0; U32 randState = FUZ_rand(&coreRandState) ^ PRIME3; int const blockSize = (FUZ_rand(&randState) % (FUZ_MAX_BLOCK_SIZE-1)) + 1; - int const blockStart = (FUZ_rand(&randState) % (COMPRESSIBLE_NOISE_LENGTH - blockSize - 1)) + 1; + int const blockStart = (int)(FUZ_rand(&randState) % (COMPRESSIBLE_NOISE_LENGTH - blockSize - 1)) + 1; int const dictSizeRand = FUZ_rand(&randState) % FUZ_MAX_DICT_SIZE; int const dictSize = MIN(dictSizeRand, blockStart - 1); int const compressionLevel = FUZ_rand(&randState) % (LZ4HC_CLEVEL_MAX+1); @@ -578,7 +578,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c /* Test partial decoding => must work */ FUZ_DISPLAYTEST("test LZ4_decompress_safe_partial"); - { size_t const missingBytes = FUZ_rand(&randState) % blockSize; + { size_t const missingBytes = FUZ_rand(&randState) % (unsigned)blockSize; int const targetSize = (int)((size_t)blockSize - missingBytes); char const sentinel = decodedBuffer[targetSize] = block[targetSize] ^ 0x5A; int const decResult = LZ4_decompress_safe_partial(compressedBuffer, decodedBuffer, compressedSize, targetSize, blockSize); @@ -705,7 +705,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c ret = LZ4_decompress_safe_usingDict(compressedBuffer, decodedBuffer, blockContinueCompressedSize, blockSize, dict, dictSize); FUZ_CHECKTEST(ret!=blockSize, "LZ4_decompress_safe_usingDict did not regenerate original data"); FUZ_CHECKTEST(decodedBuffer[blockSize], "LZ4_decompress_safe_usingDict overrun specified output buffer size"); - { U32 const crcCheck = XXH32(decodedBuffer, blockSize, 0); + { U32 const crcCheck = XXH32(decodedBuffer, (size_t)blockSize, 0); FUZ_CHECKTEST(crcCheck!=crcOrig, "LZ4_decompress_safe_usingDict corrupted decoded data"); } @@ -792,12 +792,11 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c ret = LZ4_decompress_fast_usingDict(compressedBuffer, decodedBuffer, blockSize, dict, dictSize); FUZ_CHECKTEST(ret!=blockContinueCompressedSize, "LZ4_decompress_fast_usingDict did not read all compressed block input"); FUZ_CHECKTEST(decodedBuffer[blockSize], "LZ4_decompress_fast_usingDict overrun specified output buffer size"); - { U32 const crcCheck = XXH32(decodedBuffer, blockSize, 0); + { U32 const crcCheck = XXH32(decodedBuffer, (size_t)blockSize, 0); if (crcCheck!=crcOrig) { FUZ_findDiff(block, decodedBuffer); EXIT_MSG("LZ4_decompress_fast_usingDict corrupted decoded data (dict %i)", dictSize); - } - } + } } FUZ_DISPLAYTEST(); decodedBuffer[blockSize] = 0; @@ -1018,8 +1017,8 @@ static void FUZ_unitTests(int compressionLevel) { int const sampleSize = 65 KB; int const maxCSize = LZ4_COMPRESSBOUND(sampleSize); int const outSize = LZ4_COMPRESS_INPLACE_BUFFER_SIZE(maxCSize); - int const startIndex = outSize - sampleSize; - char* const startInput = testCompressed + startIndex; + int const startInputIndex = outSize - sampleSize; + char* const startInput = testCompressed + startInputIndex; XXH32_hash_t const crcOrig = XXH32(testInput, sampleSize, 0); int cSize; assert(outSize < (int)testCompressedSize); @@ -1031,12 +1030,36 @@ static void FUZ_unitTests(int compressionLevel) assert(cSize <= maxCSize); /* decompress and verify */ { int const dSize = LZ4_decompress_safe(testCompressed, testVerify, cSize, testInputSize); - assert(dSize == (int)sampleSize); /* correct size */ + assert(dSize == sampleSize); /* correct size */ { XXH32_hash_t const crcCheck = XXH32(testVerify, (size_t)dSize, 0); assert(crcCheck == crcOrig); } } } DISPLAYLEVEL(3, " OK \n"); + /* in-place decompression test */ + DISPLAYLEVEL(3, "in-place decompression, limit case:"); + { int const sampleSize = 65 KB; + + FUZ_fillCompressibleNoiseBuffer(testInput, sampleSize, 0.0, &randState); + memset(testInput, 0, 267); /* calculated exactly so that compressedSize == originalSize-1 */ + + { XXH64_hash_t const crcOrig = XXH64(testInput, sampleSize, 0); + int const cSize = LZ4_compress_default(testInput, testCompressed, sampleSize, testCompressedSize); + assert(cSize == sampleSize - 1); /* worst case for in-place decompression */ + + { int const bufferSize = LZ4_DECOMPRESS_INPLACE_BUFFER_SIZE(sampleSize); + int const startInputIndex = bufferSize - cSize; + char* const startInput = testVerify + startInputIndex; + memcpy(startInput, testCompressed, cSize); + + /* decompress and verify */ + { int const dSize = LZ4_decompress_safe(startInput, testVerify, cSize, sampleSize); + assert(dSize == sampleSize); /* correct size */ + { XXH64_hash_t const crcCheck = XXH64(testVerify, (size_t)dSize, 0); + assert(crcCheck == crcOrig); + } } } } } + DISPLAYLEVEL(3, " OK \n"); + /* LZ4 streaming tests */ { LZ4_stream_t streamingState; U64 crcOrig; @@ -1226,15 +1249,15 @@ static void FUZ_unitTests(int compressionLevel) { XXH64_state_t crcOrigState; XXH64_state_t crcNewState; const char* dict = testInput + 3; - int dictSize = (FUZ_rand(&randState) & 8191); + size_t dictSize = (FUZ_rand(&randState) & 8191); char* dst = testVerify; - size_t segStart = (size_t)dictSize + 7; - int segSize = (FUZ_rand(&randState) & 8191); + size_t segStart = dictSize + 7; + size_t segSize = (FUZ_rand(&randState) & 8191); int segNb = 1; LZ4_resetStreamHC_fast(&sHC, compressionLevel); - LZ4_loadDictHC(&sHC, dict, dictSize); + LZ4_loadDictHC(&sHC, dict, (int)dictSize); XXH64_reset(&crcOrigState, 0); XXH64_reset(&crcNewState, 0); @@ -1242,29 +1265,28 @@ static void FUZ_unitTests(int compressionLevel) while (segStart + segSize < testInputSize) { XXH64_update(&crcOrigState, testInput + segStart, segSize); crcOrig = XXH64_digest(&crcOrigState); - result = LZ4_compress_HC_continue(&sHC, testInput + segStart, testCompressed, segSize, LZ4_compressBound(segSize)); + assert(segSize <= INT_MAX); + result = LZ4_compress_HC_continue(&sHC, testInput + segStart, testCompressed, (int)segSize, LZ4_compressBound((int)segSize)); FUZ_CHECKTEST(result==0, "LZ4_compressHC_limitedOutput_continue() dictionary compression failed : result = %i", result); FUZ_CHECKTEST(sHC.internal_donotuse.dirty, "Context should be clean"); - result = LZ4_decompress_safe_usingDict(testCompressed, dst, result, segSize, dict, dictSize); - FUZ_CHECKTEST(result!=segSize, "LZ4_decompress_safe_usingDict() dictionary decompression part %i failed", segNb); + result = LZ4_decompress_safe_usingDict(testCompressed, dst, result, (int)segSize, dict, (int)dictSize); + FUZ_CHECKTEST(result!=(int)segSize, "LZ4_decompress_safe_usingDict() dictionary decompression part %i failed", (int)segNb); XXH64_update(&crcNewState, dst, segSize); { U64 const crcNew = XXH64_digest(&crcNewState); if (crcOrig != crcNew) FUZ_findDiff(dst, testInput+segStart); FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe_usingDict() part %i corruption", segNb); } - assert(segSize >= 0); dict = dst; dictSize = segSize; - dst += (size_t)segSize + 1; + dst += segSize + 1; segNb ++; - segStart += (size_t)segSize + (FUZ_rand(&randState) & 0xF) + 1; + segStart += segSize + (FUZ_rand(&randState) & 0xF) + 1; segSize = (FUZ_rand(&randState) & 8191); - } - } + } } /* ring buffer test */ { XXH64_state_t xxhOrig; @@ -1291,18 +1313,21 @@ static void FUZ_unitTests(int compressionLevel) crcOrig = XXH64_digest(&xxhOrig); memcpy (ringBuffer + rNext, testInput + iNext, messageSize); - compressedSize = LZ4_compress_HC_continue(&sHC, ringBuffer + rNext, testCompressed, messageSize, testCompressedSize-ringBufferSize); + assert(messageSize < INT_MAX); + compressedSize = LZ4_compress_HC_continue(&sHC, ringBuffer + rNext, testCompressed, (int)messageSize, testCompressedSize-ringBufferSize); FUZ_CHECKTEST(compressedSize==0, "LZ4_compress_HC_continue() compression failed"); FUZ_CHECKTEST(sHC.internal_donotuse.dirty, "Context should be clean"); - result = LZ4_decompress_safe_continue(&decodeStateSafe, testCompressed, testVerify + dNext, compressedSize, messageSize); + assert(messageSize < INT_MAX); + result = LZ4_decompress_safe_continue(&decodeStateSafe, testCompressed, testVerify + dNext, compressedSize, (int)messageSize); FUZ_CHECKTEST(result!=(int)messageSize, "ringBuffer : LZ4_decompress_safe_continue() test failed"); XXH64_update(&xxhNewSafe, testVerify + dNext, messageSize); { U64 const crcNew = XXH64_digest(&xxhNewSafe); FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe_continue() decompression corruption"); } - result = LZ4_decompress_fast_continue(&decodeStateFast, testCompressed, testVerify + dNext, messageSize); + assert(messageSize < INT_MAX); + result = LZ4_decompress_fast_continue(&decodeStateFast, testCompressed, testVerify + dNext, (int)messageSize); FUZ_CHECKTEST(result!=compressedSize, "ringBuffer : LZ4_decompress_fast_continue() test failed"); XXH64_update(&xxhNewFast, testVerify + dNext, messageSize); @@ -1362,14 +1387,14 @@ static void FUZ_unitTests(int compressionLevel) result = LZ4_decompress_safe_continue(&decodeStateSafe, testCompressed, ringBufferSafe + dNext, compressedSize, messageSize); FUZ_CHECKTEST(result!=messageSize, "64K D.ringBuffer : LZ4_decompress_safe_continue() test failed"); - XXH64_update(&xxhNewSafe, ringBufferSafe + dNext, messageSize); + XXH64_update(&xxhNewSafe, ringBufferSafe + dNext, (size_t)messageSize); { U64 const crcNew = XXH64_digest(&xxhNewSafe); FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe_continue() decompression corruption"); } result = LZ4_decompress_fast_continue(&decodeStateFast, testCompressed, ringBufferFast + dNext, messageSize); FUZ_CHECKTEST(result!=compressedSize, "64K D.ringBuffer : LZ4_decompress_fast_continue() test failed"); - XXH64_update(&xxhNewFast, ringBufferFast + dNext, messageSize); + XXH64_update(&xxhNewFast, ringBufferFast + dNext, (size_t)messageSize); { U64 const crcNew = XXH64_digest(&xxhNewFast); FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_fast_continue() decompression corruption"); } @@ -1385,7 +1410,7 @@ static void FUZ_unitTests(int compressionLevel) dNext = 0; while (totalMessageSize < 9 MB) { - XXH64_update(&xxhOrig, testInput + iNext, messageSize); + XXH64_update(&xxhOrig, testInput + iNext, (size_t)messageSize); crcOrig = XXH64_digest(&xxhOrig); compressedSize = LZ4_compress_HC_continue(&sHC, testInput + iNext, testCompressed, messageSize, testCompressedSize-ringBufferSize); @@ -1400,7 +1425,7 @@ static void FUZ_unitTests(int compressionLevel) testCompressed, ringBufferSafe + dNext, compressedSize, dBufferSize - dNext); /* works without knowing messageSize, under assumption that messageSize <= maxMessageSize */ FUZ_CHECKTEST(result!=messageSize, "D.ringBuffer : LZ4_decompress_safe_continue() test failed"); - XXH64_update(&xxhNewSafe, ringBufferSafe + dNext, messageSize); + XXH64_update(&xxhNewSafe, ringBufferSafe + dNext, (size_t)messageSize); { U64 const crcNew = XXH64_digest(&xxhNewSafe); if (crcOrig != crcNew) FUZ_findDiff(testInput + iNext, ringBufferSafe + dNext); FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe_continue() decompression corruption during D.ringBuffer test"); @@ -1409,7 +1434,7 @@ static void FUZ_unitTests(int compressionLevel) /* test LZ4_decompress_fast_continue in its own buffer ringBufferFast */ result = LZ4_decompress_fast_continue(&decodeStateFast, testCompressed, ringBufferFast + dNext, messageSize); FUZ_CHECKTEST(result!=compressedSize, "D.ringBuffer : LZ4_decompress_fast_continue() test failed"); - XXH64_update(&xxhNewFast, ringBufferFast + dNext, messageSize); + XXH64_update(&xxhNewFast, ringBufferFast + dNext, (size_t)messageSize); { U64 const crcNew = XXH64_digest(&xxhNewFast); if (crcOrig != crcNew) FUZ_findDiff(testInput + iNext, ringBufferFast + dNext); FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_fast_continue() decompression corruption during D.ringBuffer test"); @@ -1417,7 +1442,8 @@ static void FUZ_unitTests(int compressionLevel) /* prepare next message */ dNext += messageSize; - totalMessageSize += messageSize; + assert(messageSize >= 0); + totalMessageSize += (unsigned)messageSize; messageSize = (FUZ_rand(&randState) & maxMessageSizeMask) + 1; iNext = (FUZ_rand(&randState) & 65535); if (dNext + maxMessageSize > dBufferSize) dNext = 0; @@ -1435,6 +1461,11 @@ static void FUZ_unitTests(int compressionLevel) } + +/* ======================================= + * CLI + * ======================================= */ + static int FUZ_usage(const char* programName) { DISPLAY( "Usage :\n"); -- cgit v1.2.3 From 676d46df2723c89ca7d80fd71d3c2dc6e5be82ec Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 30 May 2019 16:19:30 -0700 Subject: updated LZ4_DECOMPRESS_INPLACE_MARGIN to pass worst case scenario. Now adds margin proportional to input size to counter local expansion. --- lib/lz4.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/lz4.h b/lib/lz4.h index 91dcf64..383c2db 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -518,8 +518,8 @@ LZ4LIB_STATIC_API void LZ4_attach_dictionary(LZ4_stream_t* workingStream, const * so there are ways to reduce memory requirements by playing with them. */ -#define LZ4_DECOMPRESS_INPLACE_MARGIN 32 -#define LZ4_DECOMPRESS_INPLACE_BUFFER_SIZE(decompressedSize) ( (decompressedSize) + LZ4_DECOMPRESS_INPLACE_MARGIN) /**< note: presumes that compressedSize < decompressedSize */ +#define LZ4_DECOMPRESS_INPLACE_MARGIN(decompressedSize) (((decompressedSize) >> 8) + 32) +#define LZ4_DECOMPRESS_INPLACE_BUFFER_SIZE(decompressedSize) ( (decompressedSize) + LZ4_DECOMPRESS_INPLACE_MARGIN(decompressedSize)) /**< note: presumes that compressedSize < decompressedSize */ #ifndef LZ4_DISTANCE_MAX /* history window size; can be user-defined at compile time */ # define LZ4_DISTANCE_MAX 65535 /* set to maximum value by default */ -- cgit v1.2.3 From 2f9d1736fbfe5ee92b8305a2a613ed123e03264f Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 30 May 2019 16:23:53 -0700 Subject: updated API manual --- doc/lz4_manual.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/lz4_manual.html b/doc/lz4_manual.html index 091f537..083211d 100644 --- a/doc/lz4_manual.html +++ b/doc/lz4_manual.html @@ -407,7 +407,7 @@ int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream);


    -
    #define LZ4_DECOMPRESS_INPLACE_BUFFER_SIZE(decompressedSize)   ( (decompressedSize) + LZ4_DECOMPRESS_INPLACE_MARGIN)  /**< note: presumes that compressedSize < decompressedSize */
    +
    #define LZ4_DECOMPRESS_INPLACE_BUFFER_SIZE(decompressedSize)   ( (decompressedSize) + LZ4_DECOMPRESS_INPLACE_MARGIN(decompressedSize))  /**< note: presumes that compressedSize < decompressedSize */
     

    #define LZ4_COMPRESS_INPLACE_BUFFER_SIZE(maxCompressedSize)   ( (maxCompressedSize) + LZ4_COMPRESS_INPLACE_MARGIN)  /**< maxCompressedSize is generally LZ4_COMPRESSBOUND(inputSize), but can be set to any lower value, with the risk that compression can fail (return code 0(zero)) */
     

    -- cgit v1.2.3 From 89c97d5ea682ba7e2481d610a8b7a2b057a80391 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 30 May 2019 17:29:51 -0700 Subject: fullbench: added test scenario LZ4F_decompress_followHint This emulates a streaming scenario, where the caller follows rigorously the srcSize hints provided as return value of LZ4F_decompress(). This is useful to show the issue in #714, where data is uselessly copied in a tmp buffer first. --- tests/fullbench.c | 64 ++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 51 insertions(+), 13 deletions(-) diff --git a/tests/fullbench.c b/tests/fullbench.c index d2af662..2488a54 100644 --- a/tests/fullbench.c +++ b/tests/fullbench.c @@ -343,11 +343,45 @@ static int local_LZ4F_decompress(const char* in, char* out, int inSize, int outS assert(inSize >= 0); assert(outSize >= 0); 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); } + 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 (int)dstSize; } +static int local_LZ4F_decompress_followHint(const char* src, char* dst, int srcSize, int dstSize) +{ + size_t totalInSize = (size_t)srcSize; + size_t maxOutSize = (size_t)dstSize; + + size_t inPos = 0; + size_t inSize = 0; + size_t outPos = 0; + size_t outRemaining = maxOutSize - outPos; + + do { + size_t const sizeHint = LZ4F_decompress(g_dCtx, dst+outPos, &outRemaining, src+inPos, &inSize, NULL); + assert(!LZ4F_isError(sizeHint)); + + inPos += inSize; + inSize = sizeHint; + + outPos += outRemaining; + outRemaining = maxOutSize - outPos; + + if (!sizeHint) break; + + } while(1); + + /* frame completed */ + if (inPos != totalInSize) { + DISPLAY("Error decompressing frame : must read (%u) full frame (%u) \n", + (unsigned)inPos, (unsigned)totalInSize); + exit(10); + } + return (int)outPos; + +} + #define NB_COMPRESSION_ALGORITHMS 100 #define NB_DECOMPRESSION_ALGORITHMS 100 @@ -446,7 +480,14 @@ int fullSpeedBench(const char** fileNamesTable, int nbFiles) for (i=0; i g_chunkSize) { chunkP[i].origSize = g_chunkSize; remaining -= g_chunkSize; } else { chunkP[i].origSize = (int)remaining; remaining = 0; } + assert(g_chunkSize > 0); + if (remaining > (size_t)g_chunkSize) { + chunkP[i].origSize = g_chunkSize; + remaining -= (size_t)g_chunkSize; + } else { + chunkP[i].origSize = (int)remaining; + remaining = 0; + } chunkP[i].compressedBuffer = out; out += maxCompressedChunkSize; chunkP[i].compressedSize = 0; } @@ -565,17 +606,14 @@ int fullSpeedBench(const char** fileNamesTable, int nbFiles) #ifndef LZ4_DLL_IMPORT case 8: decompressionFunction = local_LZ4_decompress_safe_forceExtDict; dName = "LZ4_decompress_safe_forceExtDict"; break; #endif - case 9: decompressionFunction = local_LZ4F_decompress; dName = "LZ4F_decompress"; - { size_t const errorCode = LZ4F_compressFrame(compressed_buff, compressedBuffSize, orig_buff, benchedSize, NULL); - if (LZ4F_isError(errorCode)) { - DISPLAY("Error while preparing compressed frame\n"); - free(orig_buff); - free(compressed_buff); - free(chunkP); - return 1; - } + case 10: + case 11: + if (dAlgNb == 10) { decompressionFunction = local_LZ4F_decompress; dName = "LZ4F_decompress"; } /* can be skipped */ + if (dAlgNb == 11) { decompressionFunction = local_LZ4F_decompress_followHint; dName = "LZ4F_decompress_followHint"; } /* can be skipped */ + { size_t const fcsize = LZ4F_compressFrame(compressed_buff, (size_t)compressedBuffSize, orig_buff, benchedSize, NULL); + assert(!LZ4F_isError(fcsize)); chunkP[0].origSize = (int)benchedSize; - chunkP[0].compressedSize = (int)errorCode; + chunkP[0].compressedSize = (int)fcsize; nbChunks = 1; break; } -- cgit v1.2.3 From 64b5917736396754a8f93c2913f5276361831b39 Mon Sep 17 00:00:00 2001 From: Chenxi Mao Date: Fri, 31 May 2019 08:36:13 +0800 Subject: FAST_DEC_LOOP: only did offset check in specific condition. When I did FAST_DEC_LOOP performance test, I found the offset check is much more than v1.8.3 You will see the condition check difference via lzbench with dickens test case. v1.8.3 34959 v.1.9.x 1055885 After investigate the code, we could see the difference. v.1.8.3 SKIP the condition check if if condition is true in: https://github.com/lz4/lz4/blob/v1.8.3/lib/lz4.c#L1463 AND below condition is true https://github.com/lz4/lz4/blob/v1.8.3/lib/lz4.c#L1478\ The offset check should be invoked. v1.9.3 The offset check code will be invoked in every loop which lead to downgrade. So the fix would be move this check to specific condition to avoid useless condition check. After this change, the call number is same as v1.8.3 --- lib/lz4.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 070dd7e..9e6999f 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1708,10 +1708,9 @@ LZ4_decompress_generic( /* get matchlength */ length = token & ML_MASK; - if ((checkOffset) && (unlikely(match + dictSize < lowPrefix))) { goto _output_error; } /* Error : offset outside buffers */ - if (length == ML_MASK) { variable_length_error error = ok; + if ((checkOffset) && (unlikely(match + dictSize < lowPrefix))) { goto _output_error; } /* Error : offset outside buffers */ length += read_variable_length(&ip, iend - LASTLITERALS + 1, endOnInput, 0, &error); if (error != ok) { goto _output_error; } if ((safeDecode) && unlikely((uptrval)(op)+length<(uptrval)op)) { goto _output_error; } /* overflow detection */ @@ -1735,6 +1734,7 @@ LZ4_decompress_generic( continue; } } } + if ((checkOffset) && (unlikely(match + dictSize < lowPrefix))) { goto _output_error; } /* Error : offset outside buffers */ /* match starting within external dictionary */ if ((dict==usingExtDict) && (match < lowPrefix)) { if (unlikely(op+length > oend-LASTLITERALS)) { -- cgit v1.2.3 From 4eec64e4d80140400ad138b18b7286439329cb08 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 31 May 2019 11:31:48 -0700 Subject: Makefile removed CFLAGS modifier which was removing `-O3` as a side effect --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 3e55046..e24cec5 100644 --- a/Makefile +++ b/Makefile @@ -49,7 +49,7 @@ allmost: lib lz4 .PHONY: lib lib-release liblz4.a lib: liblz4.a lib lib-release liblz4.a: - @CFLAGS="$(CFLAGS)" $(MAKE) -C $(LZ4DIR) $@ + @$(MAKE) -C $(LZ4DIR) $@ .PHONY: lz4 lz4-release lz4 : liblz4.a -- cgit v1.2.3 From 33cb8518ac385835cc17be9a770b27b40cd0e15b Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 31 May 2019 11:44:37 -0700 Subject: decompress: changed final memcpy() into memmove() for compatibility with in-place decompression scenarios. --- lib/lz4.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 4451bfc..a1d1860 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -120,6 +120,7 @@ # pragma warning(disable : 4293) /* disable: C4293: too large shift (32-bits) */ #endif /* _MSC_VER */ +#define LZ4_FORCE_INLINE static #ifndef LZ4_FORCE_INLINE # ifdef _MSC_VER /* Visual Studio */ # define LZ4_FORCE_INLINE static __forceinline @@ -707,18 +708,19 @@ static const BYTE* LZ4_getPositionOnHash(U32 h, const void* tableBase, tableType { const U16* const hashTable = (const U16*) tableBase; return hashTable[h] + srcBase; } /* default, to ensure a return */ } -LZ4_FORCE_INLINE const BYTE* LZ4_getPosition(const BYTE* p, - const void* tableBase, tableType_t tableType, - const BYTE* srcBase) +LZ4_FORCE_INLINE const BYTE* +LZ4_getPosition(const BYTE* p, + const void* tableBase, tableType_t tableType, + const BYTE* srcBase) { U32 const h = LZ4_hashPosition(p, tableType); return LZ4_getPositionOnHash(h, tableBase, tableType, srcBase); } -LZ4_FORCE_INLINE void LZ4_prepareTable( - LZ4_stream_t_internal* const cctx, - const int inputSize, - const tableType_t tableType) { +LZ4_FORCE_INLINE void +LZ4_prepareTable(LZ4_stream_t_internal* const cctx, + const int inputSize, + const tableType_t tableType) { /* If compression failed during the previous step, then the context * is marked as dirty, therefore, it has to be fully reset. */ @@ -733,9 +735,10 @@ LZ4_FORCE_INLINE void LZ4_prepareTable( * out if it's safe to leave as is or whether it needs to be reset. */ if (cctx->tableType != clearedTable) { + assert(inputSize >= 0); if (cctx->tableType != tableType - || (tableType == byU16 && cctx->currentOffset + inputSize >= 0xFFFFU) - || (tableType == byU32 && cctx->currentOffset > 1 GB) + || ((tableType == byU16) && cctx->currentOffset + (unsigned)inputSize >= 0xFFFFU) + || ((tableType == byU32) && cctx->currentOffset > 1 GB) || tableType == byPtr || inputSize >= 4 KB) { @@ -1850,7 +1853,7 @@ LZ4_decompress_generic( if ((!endOnInput) && (cpy != oend)) { goto _output_error; } /* Error : block decoding must stop exactly there */ if ((endOnInput) && ((ip+length != iend) || (cpy > oend))) { goto _output_error; } /* Error : input must be consumed */ } - memcpy(op, ip, length); + memmove(op, ip, length); /* supports overlapping memory regions, which only matters for in-place decompression scenarios */ ip += length; op += length; if (!partialDecoding || (cpy == oend)) { -- cgit v1.2.3 From 5997e139f53169fa3a1c1b4418d2452a90b01602 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 31 May 2019 11:56:59 -0700 Subject: added more details for in-place documentation --- doc/lz4_manual.html | 25 +++++++++++++------------ lib/lz4.h | 29 +++++++++++++++-------------- 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/doc/lz4_manual.html b/doc/lz4_manual.html index 083211d..1480089 100644 --- a/doc/lz4_manual.html +++ b/doc/lz4_manual.html @@ -364,23 +364,23 @@ int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream); and decompression to start at beginning of the buffer. Buffer size must feature some margin, hence be larger than final size. - |<------------------------buffer----------------------------------->| - |<------------compressed data---------->| + |<------------------------buffer--------------------------------->| + |<-----------compressed data--------->| |<-----------decompressed size------------------>| - |<-----margin----->| + |<----margin---->| This technique is more useful for decompression, since decompressed size is typically larger, - and margin is mostly required to avoid stripe overflow, so it's short. + and margin is short. In-place decompression will work inside any buffer which size is >= LZ4_DECOMPRESS_INPLACE_BUFFER_SIZE(decompressedSize). This presumes that decompressedSize > compressedSize. Otherwise, it means compression actually expanded data, - which can happen when data is not compressible (already compressed, or encrypted), and it would be more efficient to store such data with a flag indicating it's not compressed. + This can happen when data is not compressible (already compressed, or encrypted). - For compression, margin is larger, as it must be able to cope with both + For in-place compression, margin is larger, as it must be able to cope with both history preservation, requiring input data to remain unmodified up to LZ4_DISTANCE_MAX, and data expansion, which can happen when input is not compressible. As a consequence, buffer size requirements are much higher, @@ -388,10 +388,11 @@ int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream); There are ways to limit this cost for compression : - Reduce history size, by modifying LZ4_DISTANCE_MAX. - Note that it is a compile-time constant, so all future compression will apply this parameter. + Note that it is a compile-time constant, so all compressions will apply this limit. Lower values will reduce compression ratio, except when input_size < LZ4_DISTANCE_MAX, so it's a reasonable trick when inputs are known to be small. - Require the compressor to deliver a "maximum compressed size". + This is the `dstCapacity` parameter in `LZ4_compress*()`. When this size is < LZ4_COMPRESSBOUND(inputSize), then compression can fail, in which case, the return code will be 0 (zero). The caller must be ready for these cases to happen, @@ -400,16 +401,16 @@ int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream); the amount of margin required for in-place compression. In-place compression can work in any buffer - which size is >= LZ4_COMPRESS_INPLACE_BUFFER_SIZE(maxCompressedSize) + which size is >= (maxCompressedSize) with maxCompressedSize == LZ4_COMPRESSBOUND(srcSize) for guaranteed compression success. - This macro depends on both maxCompressedSize and LZ4_DISTANCE_MAX, - so there are ways to reduce memory requirements by playing with them. + LZ4_COMPRESS_INPLACE_BUFFER_SIZE() depends on both maxCompressedSize and LZ4_DISTANCE_MAX, + so it's possible to reduce memory requirements by playing with them.


    -
    #define LZ4_DECOMPRESS_INPLACE_BUFFER_SIZE(decompressedSize)   ( (decompressedSize) + LZ4_DECOMPRESS_INPLACE_MARGIN(decompressedSize))  /**< note: presumes that compressedSize < decompressedSize */
    +
    #define LZ4_DECOMPRESS_INPLACE_BUFFER_SIZE(decompressedSize)   ((decompressedSize) + LZ4_DECOMPRESS_INPLACE_MARGIN(decompressedSize))  /**< note: presumes that compressedSize < decompressedSize. note2: margin is overestimated a bit, since it could use compressedSize instead */
     

    -
    #define LZ4_COMPRESS_INPLACE_BUFFER_SIZE(maxCompressedSize)   ( (maxCompressedSize) + LZ4_COMPRESS_INPLACE_MARGIN)  /**< maxCompressedSize is generally LZ4_COMPRESSBOUND(inputSize), but can be set to any lower value, with the risk that compression can fail (return code 0(zero)) */
    +
    #define LZ4_COMPRESS_INPLACE_BUFFER_SIZE(maxCompressedSize)   ((maxCompressedSize) + LZ4_COMPRESS_INPLACE_MARGIN)  /**< maxCompressedSize is generally LZ4_COMPRESSBOUND(inputSize), but can be set to any lower value, with the risk that compression can fail (return code 0(zero)) */
     

    PRIVATE DEFINITIONS

      Do not use these definitions directly.
    diff --git a/lib/lz4.h b/lib/lz4.h
    index 383c2db..0dfa637 100644
    --- a/lib/lz4.h
    +++ b/lib/lz4.h
    @@ -476,23 +476,23 @@ LZ4LIB_STATIC_API void LZ4_attach_dictionary(LZ4_stream_t* workingStream, const
      * and decompression to start at beginning of the buffer.
      * Buffer size must feature some margin, hence be larger than final size.
      *
    - * |<------------------------buffer----------------------------------->|
    - *                             |<------------compressed data---------->|
    + * |<------------------------buffer--------------------------------->|
    + *                             |<-----------compressed data--------->|
      * |<-----------decompressed size------------------>|
    - *                                                  |<-----margin----->|
    + *                                                  |<----margin---->|
      *
      * This technique is more useful for decompression,
      * since decompressed size is typically larger,
    - * and margin is mostly required to avoid stripe overflow, so it's short.
    + * and margin is short.
      *
      * In-place decompression will work inside any buffer
      * which size is >= LZ4_DECOMPRESS_INPLACE_BUFFER_SIZE(decompressedSize).
      * This presumes that decompressedSize > compressedSize.
      * Otherwise, it means compression actually expanded data,
    - * which can happen when data is not compressible (already compressed, or encrypted),
      * and it would be more efficient to store such data with a flag indicating it's not compressed.
    + * This can happen when data is not compressible (already compressed, or encrypted).
      *
    - * For compression, margin is larger, as it must be able to cope with both
    + * For in-place compression, margin is larger, as it must be able to cope with both
      * history preservation, requiring input data to remain unmodified up to LZ4_DISTANCE_MAX,
      * and data expansion, which can happen when input is not compressible.
      * As a consequence, buffer size requirements are much higher,
    @@ -500,10 +500,11 @@ LZ4LIB_STATIC_API void LZ4_attach_dictionary(LZ4_stream_t* workingStream, const
      *
      * There are ways to limit this cost for compression :
      * - Reduce history size, by modifying LZ4_DISTANCE_MAX.
    - *   Note that it is a compile-time constant, so all future compression will apply this parameter.
    + *   Note that it is a compile-time constant, so all compressions will apply this limit.
      *   Lower values will reduce compression ratio, except when input_size < LZ4_DISTANCE_MAX,
      *   so it's a reasonable trick when inputs are known to be small.
      * - Require the compressor to deliver a "maximum compressed size".
    + *   This is the `dstCapacity` parameter in `LZ4_compress*()`.
      *   When this size is < LZ4_COMPRESSBOUND(inputSize), then compression can fail,
      *   in which case, the return code will be 0 (zero).
      *   The caller must be ready for these cases to happen,
    @@ -512,21 +513,21 @@ LZ4LIB_STATIC_API void LZ4_attach_dictionary(LZ4_stream_t* workingStream, const
      * the amount of margin required for in-place compression.
      *
      * In-place compression can work in any buffer
    - * which size is >= LZ4_COMPRESS_INPLACE_BUFFER_SIZE(maxCompressedSize)
    + * which size is >= (maxCompressedSize)
      * with maxCompressedSize == LZ4_COMPRESSBOUND(srcSize) for guaranteed compression success.
    - * This macro depends on both maxCompressedSize and LZ4_DISTANCE_MAX,
    - * so there are ways to reduce memory requirements by playing with them.
    + * LZ4_COMPRESS_INPLACE_BUFFER_SIZE() depends on both maxCompressedSize and LZ4_DISTANCE_MAX,
    + * so it's possible to reduce memory requirements by playing with them.
      */
     
    -#define LZ4_DECOMPRESS_INPLACE_MARGIN(decompressedSize)  (((decompressedSize) >> 8) + 32)
    -#define LZ4_DECOMPRESS_INPLACE_BUFFER_SIZE(decompressedSize)   ( (decompressedSize) + LZ4_DECOMPRESS_INPLACE_MARGIN(decompressedSize))  /**< note: presumes that compressedSize < decompressedSize */
    +#define LZ4_DECOMPRESS_INPLACE_MARGIN(compressedSize)          (((compressedSize) >> 8) + 32)
    +#define LZ4_DECOMPRESS_INPLACE_BUFFER_SIZE(decompressedSize)   ((decompressedSize) + LZ4_DECOMPRESS_INPLACE_MARGIN(decompressedSize))  /**< note: presumes that compressedSize < decompressedSize. note2: margin is overestimated a bit, since it could use compressedSize instead */
     
     #ifndef LZ4_DISTANCE_MAX   /* history window size; can be user-defined at compile time */
     #  define LZ4_DISTANCE_MAX 65535   /* set to maximum value by default */
     #endif
     
    -#define LZ4_COMPRESS_INPLACE_MARGIN (LZ4_DISTANCE_MAX + 32)
    -#define LZ4_COMPRESS_INPLACE_BUFFER_SIZE(maxCompressedSize)   ( (maxCompressedSize) + LZ4_COMPRESS_INPLACE_MARGIN)  /**< maxCompressedSize is generally LZ4_COMPRESSBOUND(inputSize), but can be set to any lower value, with the risk that compression can fail (return code 0(zero)) */
    +#define LZ4_COMPRESS_INPLACE_MARGIN                           (LZ4_DISTANCE_MAX + 32)   /* LZ4_DISTANCE_MAX can be safely replaced by srcSize when it's smaller */
    +#define LZ4_COMPRESS_INPLACE_BUFFER_SIZE(maxCompressedSize)   ((maxCompressedSize) + LZ4_COMPRESS_INPLACE_MARGIN)  /**< maxCompressedSize is generally LZ4_COMPRESSBOUND(inputSize), but can be set to any lower value, with the risk that compression can fail (return code 0(zero)) */
     
     #endif   /* LZ4_STATIC_3504398509 */
     #endif   /* LZ4_STATIC_LINKING_ONLY */
    -- 
    cgit v1.2.3
    
    
    From 99f1721ff5e3ab74fbe1fc8c68337ba87c1a8be9 Mon Sep 17 00:00:00 2001
    From: Yann Collet 
    Date: Fri, 31 May 2019 12:01:33 -0700
    Subject: replaced while(1)
    
    by for (;;)
    just to please Visual Studio C4127 .
    ---
     tests/fullbench.c | 5 ++---
     1 file changed, 2 insertions(+), 3 deletions(-)
    
    diff --git a/tests/fullbench.c b/tests/fullbench.c
    index 2488a54..dbdb02c 100644
    --- a/tests/fullbench.c
    +++ b/tests/fullbench.c
    @@ -358,7 +358,7 @@ static int local_LZ4F_decompress_followHint(const char* src, char* dst, int srcS
         size_t outPos = 0;
         size_t outRemaining = maxOutSize - outPos;
     
    -    do {
    +    for (;;) {
             size_t const sizeHint = LZ4F_decompress(g_dCtx, dst+outPos, &outRemaining, src+inPos, &inSize, NULL);
             assert(!LZ4F_isError(sizeHint));
     
    @@ -369,8 +369,7 @@ static int local_LZ4F_decompress_followHint(const char* src, char* dst, int srcS
             outRemaining = maxOutSize - outPos;
     
             if (!sizeHint) break;
    -
    -    } while(1);
    +    }
     
         /* frame completed */
         if (inPos != totalInSize) {
    -- 
    cgit v1.2.3
    
    
    From 33a04fb8bd19fb4450ea37dad6aa0fd0d7f4007c Mon Sep 17 00:00:00 2001
    From: Yann Collet 
    Date: Fri, 31 May 2019 13:25:12 -0700
    Subject: fullbench: ensure decompressionFunction and dName are initialized
    
    Visual Studio seems to miss that they are necessarily initialized in the switch() { case: }
    ---
     tests/fullbench.c | 18 ++++++++++++++----
     1 file changed, 14 insertions(+), 4 deletions(-)
    
    diff --git a/tests/fullbench.c b/tests/fullbench.c
    index dbdb02c..4609f13 100644
    --- a/tests/fullbench.c
    +++ b/tests/fullbench.c
    @@ -571,9 +571,15 @@ int fullSpeedBench(const char** fileNamesTable, int nbFiles)
     
               nbChunks = (int) (((int)benchedSize + (g_chunkSize-1))/ g_chunkSize);
               for (i=0; i g_chunkSize) { chunkP[i].origSize = g_chunkSize; remaining -= g_chunkSize; } else { chunkP[i].origSize = (int)remaining; remaining = 0; }
    +              if ((int)remaining > g_chunkSize) {
    +                  chunkP[i].origSize = g_chunkSize;
    +                  remaining -= (size_t)g_chunkSize;
    +              } else {
    +                  chunkP[i].origSize = (int)remaining;
    +                  remaining = 0;
    +              }
                   chunkP[i].compressedBuffer = out; out += maxCompressedChunkSize;
                   chunkP[i].compressedSize = 0;
               }
    @@ -586,8 +592,8 @@ int fullSpeedBench(const char** fileNamesTable, int nbFiles)
     
             /* Decompression Algorithms */
             for (dAlgNb=0; (dAlgNb <= NB_DECOMPRESSION_ALGORITHMS) && g_decompressionTest; dAlgNb++) {
    -            const char* dName;
    -            int (*decompressionFunction)(const char*, char*, int, int);
    +            const char* dName = NULL;
    +            int (*decompressionFunction)(const char*, char*, int, int) = NULL;
                 double bestTime = 100000000.;
                 int checkResult = 1;
     
    @@ -609,6 +615,7 @@ int fullSpeedBench(const char** fileNamesTable, int nbFiles)
                 case 11:
                     if (dAlgNb == 10) { decompressionFunction = local_LZ4F_decompress; dName = "LZ4F_decompress"; }  /* can be skipped */
                     if (dAlgNb == 11) { decompressionFunction = local_LZ4F_decompress_followHint; dName = "LZ4F_decompress_followHint"; }  /* can be skipped */
    +                /* prepare compressed data using frame format */
                     {   size_t const fcsize = LZ4F_compressFrame(compressed_buff, (size_t)compressedBuffSize, orig_buff, benchedSize, NULL);
                         assert(!LZ4F_isError(fcsize));
                         chunkP[0].origSize = (int)benchedSize;
    @@ -620,6 +627,9 @@ int fullSpeedBench(const char** fileNamesTable, int nbFiles)
                     continue;   /* skip if unknown ID */
                 }
     
    +            assert(decompressionFunction != NULL);
    +            assert(dName != NULL);
    +
                 { size_t i; for (i=0; i
    Date: Tue, 4 Jun 2019 14:04:49 -0700
    Subject: restored FORCE_INLINE
    
    ---
     lib/lz4.c | 1 -
     1 file changed, 1 deletion(-)
    
    diff --git a/lib/lz4.c b/lib/lz4.c
    index 076565a..cac3240 100644
    --- a/lib/lz4.c
    +++ b/lib/lz4.c
    @@ -120,7 +120,6 @@
     #  pragma warning(disable : 4293)        /* disable: C4293: too large shift (32-bits) */
     #endif  /* _MSC_VER */
     
    -#define LZ4_FORCE_INLINE static
     #ifndef LZ4_FORCE_INLINE
     #  ifdef _MSC_VER    /* Visual Studio */
     #    define LZ4_FORCE_INLINE static __forceinline
    -- 
    cgit v1.2.3
    
    
    From f7b52ecbbad2d2632e0ae08f105d38e3f49a3673 Mon Sep 17 00:00:00 2001
    From: Ephraim Park 
    Date: Tue, 4 Jun 2019 12:45:31 -0700
    Subject: circleci : use custom docker image with pre-installed dependencies
    
    ---
     .circleci/config.yml                | 33 +--------------------------------
     .circleci/images/primary/Dockerfile | 12 ++++++++++++
     2 files changed, 13 insertions(+), 32 deletions(-)
     create mode 100644 .circleci/images/primary/Dockerfile
    
    diff --git a/.circleci/config.yml b/.circleci/config.yml
    index a620cd1..3abcbc1 100644
    --- a/.circleci/config.yml
    +++ b/.circleci/config.yml
    @@ -27,8 +27,7 @@ jobs:
         # To see the list of pre-built images that CircleCI provides for most common languages see
         # https://circleci.com/docs/2.0/circleci-images/
         docker:
    -    - image: circleci/build-image:ubuntu-14.04-XXL-upstart-1189-5614f37
    -      command: /sbin/init
    +    - image: fbopensource/lz4-circleci-primary:0.0.4
         steps:
         # Machine Setup
         #   If you break your build into multiple jobs with workflows, you will probably want to do the parts of this that are relevant in each
    @@ -38,36 +37,6 @@ jobs:
         # In many cases you can simplify this from what is generated here.
         # 'See docs on artifact collection here https://circleci.com/docs/2.0/artifacts/'
         - run: mkdir -p $CIRCLE_ARTIFACTS $CIRCLE_TEST_REPORTS
    -    # Dependencies
    -    #   This would typically go in either a build or a build-and-test job when using workflows
    -    # Restore the dependency cache
    -    - restore_cache:
    -        keys:
    -        # This branch if available
    -        - v1-dep-{{ .Branch }}-
    -        # Default branch if not
    -        - v1-dep-dev-
    -        # Any branch if there are none on the default branch - this should be unnecessary if you have your default branch configured correctly
    -        - v1-dep-
    -    # This is based on your 1.0 configuration file or project settings
    -    - run: sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test; sudo apt-get -y -qq update
    -    - run: sudo apt-get -y install qemu-system-ppc qemu-user-static gcc-powerpc-linux-gnu
    -    - run: sudo apt-get -y install qemu-system-arm gcc-arm-linux-gnueabi libc6-dev-armel-cross gcc-aarch64-linux-gnu libc6-dev-arm64-cross
    -    - run: sudo apt-get -y install libc6-dev-i386 clang gcc-5 gcc-5-multilib gcc-6 valgrind
    -    # Save dependency cache
    -    - save_cache:
    -        key: v1-dep-{{ .Branch }}-{{ epoch }}
    -        paths:
    -        # This is a broad list of cache paths to include many possible development environments
    -        # You can probably delete some of these entries
    -        - vendor/bundle
    -        - ~/virtualenvs
    -        - ~/.m2
    -        - ~/.ivy2
    -        - ~/.bundle
    -        - ~/.go_workspace
    -        - ~/.gradle
    -        - ~/.cache/bower
         # Test
         #   This would typically be a build job when using workflows, possibly combined with build
         # This is based on your 1.0 configuration file or project settings
    diff --git a/.circleci/images/primary/Dockerfile b/.circleci/images/primary/Dockerfile
    new file mode 100644
    index 0000000..7767014
    --- /dev/null
    +++ b/.circleci/images/primary/Dockerfile
    @@ -0,0 +1,12 @@
    +FROM circleci/buildpack-deps:bionic
    +
    +RUN sudo apt-get -y -qq update
    +RUN sudo apt-get -y install software-properties-common
    +RUN sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
    +RUN sudo apt-get -y install cmake
    +RUN sudo apt-get -y install qemu-system-ppc qemu-user-static qemu-system-arm
    +RUN sudo apt-get -y install libc6-dev-armel-cross  libc6-dev-arm64-cross libc6-dev-i386
    +RUN sudo apt-get -y install clang clang-tools
    +RUN sudo apt-get -y install gcc-5 gcc-5-multilib gcc-6
    +RUN sudo apt-get -y install valgrind
    +RUN sudo apt-get -y install gcc-multilib-powerpc-linux-gnu gcc-powerpc-linux-gnu gcc-arm-linux-gnueabi gcc-aarch64-linux-gnu
    -- 
    cgit v1.2.3
    
    
    From 1d759576b92a54a29e549bb3a4d42f0660ab4b31 Mon Sep 17 00:00:00 2001
    From: Yann Collet 
    Date: Thu, 6 Jun 2019 13:20:30 -0700
    Subject: precise again that LZ4 decoder needs metadata
    
    and that such metadata must be provided / sent / saved by the application.
    ---
     doc/lz4_manual.html | 51 +++++++++++++++++++++++++++++++--------------------
     lib/lz4.h           | 53 +++++++++++++++++++++++++++++++----------------------
     2 files changed, 62 insertions(+), 42 deletions(-)
    
    diff --git a/doc/lz4_manual.html b/doc/lz4_manual.html
    index 1480089..18daa05 100644
    --- a/doc/lz4_manual.html
    +++ b/doc/lz4_manual.html
    @@ -21,7 +21,7 @@
     
     

    Introduction

    -  LZ4 is lossless compression algorithm, providing compression speed at 500 MB/s per core,
    +  LZ4 is lossless compression algorithm, providing compression speed >500 MB/s per core,
       scalable with multi-cores CPU. It features an extremely fast decoder, with speed in
       multiple GB/s per core, typically reaching RAM speed limits on multi-core systems.
     
    @@ -33,7 +33,10 @@
         - unbounded multiple steps (described as Streaming compression)
     
       lz4.h generates and decodes LZ4-compressed blocks (doc/lz4_Block_format.md).
    -  Decompressing a block requires additional metadata, such as its compressed size.
    +  Decompressing such a compressed block requires additional metadata.
    +  Exact metadata depends on exact decompression function.
    +  For the typical case of LZ4_decompress_safe(),
    +  metadata includes block's compressed size, and maximum bound of decompressed size.
       Each application is free to encode and pass such metadata in whichever way it wants.
     
       lz4.h only handle blocks, it can not generate Frames.
    @@ -66,27 +69,35 @@
     

    Simple Functions

    
     
     
    int LZ4_compress_default(const char* src, char* dst, int srcSize, int dstCapacity);
    -

    Compresses 'srcSize' bytes from buffer 'src' - into already allocated 'dst' buffer of size 'dstCapacity'. - Compression is guaranteed to succeed if 'dstCapacity' >= LZ4_compressBound(srcSize). - It also runs faster, so it's a recommended setting. - If the function cannot compress 'src' into a more limited 'dst' budget, - compression stops *immediately*, and the function result is zero. - In which case, 'dst' content is undefined (invalid). - srcSize : max supported value is LZ4_MAX_INPUT_SIZE. - dstCapacity : size of buffer 'dst' (which must be already allocated) - @return : the number of bytes written into buffer 'dst' (necessarily <= dstCapacity) - or 0 if compression fails - Note : This function is protected against buffer overflow scenarios (never writes outside 'dst' buffer, nor read outside 'source' buffer). +

    Compresses 'srcSize' bytes from buffer 'src' + into already allocated 'dst' buffer of size 'dstCapacity'. + Compression is guaranteed to succeed if 'dstCapacity' >= LZ4_compressBound(srcSize). + It also runs faster, so it's a recommended setting. + If the function cannot compress 'src' into a more limited 'dst' budget, + compression stops *immediately*, and the function result is zero. + In which case, 'dst' content is undefined (invalid). + srcSize : max supported value is LZ4_MAX_INPUT_SIZE. + dstCapacity : size of buffer 'dst' (which must be already allocated) + @return : the number of bytes written into buffer 'dst' (necessarily <= dstCapacity) + or 0 if compression fails + Note : This function is protected against buffer overflow scenarios (never writes outside 'dst' buffer, nor read outside 'source' buffer). +


    int LZ4_decompress_safe (const char* src, char* dst, int compressedSize, int dstCapacity);
    -

    compressedSize : is the exact complete size of the compressed block. - dstCapacity : is the size of destination buffer, which must be already allocated. - @return : the number of bytes decompressed into destination buffer (necessarily <= dstCapacity) - If destination buffer is not large enough, decoding will stop and output an error code (negative value). - If the source stream is detected malformed, the function will stop decoding and return a negative result. - Note : This function is protected against malicious data packets (never writes outside 'dst' buffer, nor read outside 'source' buffer). +

    compressedSize : is the exact complete size of the compressed block. + dstCapacity : is the size of destination buffer (which must be already allocated), presumed an upper bound of decompressed size. + @return : the number of bytes decompressed into destination buffer (necessarily <= dstCapacity) + If destination buffer is not large enough, decoding will stop and output an error code (negative value). + If the source stream is detected malformed, the function will stop decoding and return a negative result. + Note 1 : This function is protected against malicious data packets : + it will never writes outside 'dst' buffer, nor read outside 'source' buffer, + even if the compressed block is maliciously modified to order the decoder to do these actions. + In such case, the decoder stops immediately, and considers the compressed block malformed. + Note 2 : compressedSize and dstCapacity must be provided to the function, the compressed block does not contain them. + The implementation is free to send / store / derive this information in whichever way is most beneficial. + If there is a need for a different format which bundles together both compressed data and its metadata, consider looking at lz4frame.h instead. +


    Advanced Functions

    
    diff --git a/lib/lz4.h b/lib/lz4.h
    index 0dfa637..ad93d00 100644
    --- a/lib/lz4.h
    +++ b/lib/lz4.h
    @@ -46,7 +46,7 @@ extern "C" {
     /**
       Introduction
     
    -  LZ4 is lossless compression algorithm, providing compression speed at 500 MB/s per core,
    +  LZ4 is lossless compression algorithm, providing compression speed >500 MB/s per core,
       scalable with multi-cores CPU. It features an extremely fast decoder, with speed in
       multiple GB/s per core, typically reaching RAM speed limits on multi-core systems.
     
    @@ -58,7 +58,10 @@ extern "C" {
         - unbounded multiple steps (described as Streaming compression)
     
       lz4.h generates and decodes LZ4-compressed blocks (doc/lz4_Block_format.md).
    -  Decompressing a block requires additional metadata, such as its compressed size.
    +  Decompressing such a compressed block requires additional metadata.
    +  Exact metadata depends on exact decompression function.
    +  For the typical case of LZ4_decompress_safe(),
    +  metadata includes block's compressed size, and maximum bound of decompressed size.
       Each application is free to encode and pass such metadata in whichever way it wants.
     
       lz4.h only handle blocks, it can not generate Frames.
    @@ -129,29 +132,35 @@ LZ4LIB_API const char* LZ4_versionString (void);   /**< library version string;
     *  Simple Functions
     **************************************/
     /*! LZ4_compress_default() :
    -    Compresses 'srcSize' bytes from buffer 'src'
    -    into already allocated 'dst' buffer of size 'dstCapacity'.
    -    Compression is guaranteed to succeed if 'dstCapacity' >= LZ4_compressBound(srcSize).
    -    It also runs faster, so it's a recommended setting.
    -    If the function cannot compress 'src' into a more limited 'dst' budget,
    -    compression stops *immediately*, and the function result is zero.
    -    In which case, 'dst' content is undefined (invalid).
    -        srcSize : max supported value is LZ4_MAX_INPUT_SIZE.
    -        dstCapacity : size of buffer 'dst' (which must be already allocated)
    -       @return  : the number of bytes written into buffer 'dst' (necessarily <= dstCapacity)
    -                  or 0 if compression fails
    -    Note : This function is protected against buffer overflow scenarios (never writes outside 'dst' buffer, nor read outside 'source' buffer).
    -*/
    + *  Compresses 'srcSize' bytes from buffer 'src'
    + *  into already allocated 'dst' buffer of size 'dstCapacity'.
    + *  Compression is guaranteed to succeed if 'dstCapacity' >= LZ4_compressBound(srcSize).
    + *  It also runs faster, so it's a recommended setting.
    + *  If the function cannot compress 'src' into a more limited 'dst' budget,
    + *  compression stops *immediately*, and the function result is zero.
    + *  In which case, 'dst' content is undefined (invalid).
    + *      srcSize : max supported value is LZ4_MAX_INPUT_SIZE.
    + *      dstCapacity : size of buffer 'dst' (which must be already allocated)
    + *     @return  : the number of bytes written into buffer 'dst' (necessarily <= dstCapacity)
    + *                or 0 if compression fails
    + * Note : This function is protected against buffer overflow scenarios (never writes outside 'dst' buffer, nor read outside 'source' buffer).
    + */
     LZ4LIB_API int LZ4_compress_default(const char* src, char* dst, int srcSize, int dstCapacity);
     
     /*! LZ4_decompress_safe() :
    -    compressedSize : is the exact complete size of the compressed block.
    -    dstCapacity : is the size of destination buffer, which must be already allocated.
    -   @return : the number of bytes decompressed into destination buffer (necessarily <= dstCapacity)
    -             If destination buffer is not large enough, decoding will stop and output an error code (negative value).
    -             If the source stream is detected malformed, the function will stop decoding and return a negative result.
    -    Note : This function is protected against malicious data packets (never writes outside 'dst' buffer, nor read outside 'source' buffer).
    -*/
    + *  compressedSize : is the exact complete size of the compressed block.
    + *  dstCapacity : is the size of destination buffer (which must be already allocated), presumed an upper bound of decompressed size.
    + * @return : the number of bytes decompressed into destination buffer (necessarily <= dstCapacity)
    + *           If destination buffer is not large enough, decoding will stop and output an error code (negative value).
    + *           If the source stream is detected malformed, the function will stop decoding and return a negative result.
    + * Note 1 : This function is protected against malicious data packets :
    + *          it will never writes outside 'dst' buffer, nor read outside 'source' buffer,
    + *          even if the compressed block is maliciously modified to order the decoder to do these actions.
    + *          In such case, the decoder stops immediately, and considers the compressed block malformed.
    + * Note 2 : compressedSize and dstCapacity must be provided to the function, the compressed block does not contain them.
    + *          The implementation is free to send / store / derive this information in whichever way is most beneficial.
    + *          If there is a need for a different format which bundles together both compressed data and its metadata, consider looking at lz4frame.h instead.
    + */
     LZ4LIB_API int LZ4_decompress_safe (const char* src, char* dst, int compressedSize, int dstCapacity);
     
     
    -- 
    cgit v1.2.3
    
    
    From 798301b4e144fab5d25fc34566c1419685f5f1eb Mon Sep 17 00:00:00 2001
    From: Yann Collet 
    Date: Thu, 6 Jun 2019 14:17:44 -0700
    Subject: update simple_buffer example
    
    there were a few tiny inaccuracies, especially in error conditions.
    ---
     examples/simple_buffer.c | 45 +++++++++++++++++++++++++--------------------
     1 file changed, 25 insertions(+), 20 deletions(-)
    
    diff --git a/examples/simple_buffer.c b/examples/simple_buffer.c
    index 54e542a..027fc85 100644
    --- a/examples/simple_buffer.c
    +++ b/examples/simple_buffer.c
    @@ -3,18 +3,18 @@
      * Copyright  : Kyle Harper
      * License    : Follows same licensing as the lz4.c/lz4.h program at any given time.  Currently, BSD 2.
      * Description: Example program to demonstrate the basic usage of the compress/decompress functions within lz4.c/lz4.h.
    - *              The functions you'll likely want are LZ4_compress_default and LZ4_decompress_safe.  Both of these are documented in
    - *              the lz4.h header file; I recommend reading them.
    + *              The functions you'll likely want are LZ4_compress_default and LZ4_decompress_safe.
    + *              Both of these are documented in the lz4.h header file; I recommend reading them.
      */
     
    -/* Includes, for Power! */
    -#include "lz4.h"     // This is all that is required to expose the prototypes for basic compression and decompression.
    +/* Dependencies */
     #include    // For printf()
     #include   // For memcmp()
     #include   // For exit()
    +#include "lz4.h"     // This is all that is required to expose the prototypes for basic compression and decompression.
     
     /*
    - * Easy show-error-and-bail function.
    + * Simple show-error-and-bail function.
      */
     void run_screaming(const char* message, const int code) {
       printf("%s \n", message);
    @@ -32,9 +32,9 @@ int main(void) {
       //   1) The return codes of LZ4_ functions are important.
       //      Read lz4.h if you're unsure what a given code means.
       //   2) LZ4 uses char* pointers in all LZ4_ functions.
    -  //      This is baked into the API and probably not going to change.
    -  //      If your program uses pointers that are unsigned char*, void*, or otherwise different,
    -  //      you may need to do some casting or set the right -W compiler flags to ignore those warnings (e.g.: -Wno-pointer-sign).
    +  //      This is baked into the API and not going to change, for consistency.
    +  //      If your program uses different pointer types,
    +  //      you may need to do some casting or set the right -Wno compiler flags to ignore those warnings (e.g.: -Wno-pointer-sign).
     
       /* Compression */
       // We'll store some text into a variable pointed to by *src to be compressed later.
    @@ -44,7 +44,7 @@ int main(void) {
       // LZ4 provides a function that will tell you the maximum size of compressed output based on input data via LZ4_compressBound().
       const int max_dst_size = LZ4_compressBound(src_size);
       // We will use that size for our destination boundary when allocating space.
    -  char* compressed_data = malloc(max_dst_size);
    +  char* compressed_data = malloc((size_t)max_dst_size);
       if (compressed_data == NULL)
         run_screaming("Failed to allocate memory for *compressed_data.", 1);
       // That's all the information and preparation LZ4 needs to compress *src into *compressed_data.
    @@ -52,21 +52,26 @@ int main(void) {
       // Save the return value for error checking.
       const int compressed_data_size = LZ4_compress_default(src, compressed_data, src_size, max_dst_size);
       // Check return_value to determine what happened.
    -  if (compressed_data_size < 0)
    -    run_screaming("A negative result from LZ4_compress_default indicates a failure trying to compress the data.  See exit code (echo $?) for value returned.", compressed_data_size);
    -  if (compressed_data_size == 0)
    -    run_screaming("A result of 0 means compression worked, but was stopped because the destination buffer couldn't hold all the information.", 1);
    +  if (compressed_data_size <= 0)
    +    run_screaming("A 0 or negative result from LZ4_compress_default() indicates a failure trying to compress the data. ", 1);
       if (compressed_data_size > 0)
         printf("We successfully compressed some data!\n");
       // Not only does a positive return_value mean success, the value returned == the number of bytes required.
       // You can use this to realloc() *compress_data to free up memory, if desired.  We'll do so just to demonstrate the concept.
    -  compressed_data = (char *)realloc(compressed_data, compressed_data_size);
    +  compressed_data = (char *)realloc(compressed_data, (size_t)compressed_data_size);
       if (compressed_data == NULL)
         run_screaming("Failed to re-alloc memory for compressed_data.  Sad :(", 1);
     
    +
       /* Decompression */
    -  // Now that we've successfully compressed the information from *src to *compressed_data, let's do the opposite!  We'll create a
    -  // *new_src location of size src_size since we know that value.
    +  // Now that we've successfully compressed the information from *src to *compressed_data, let's do the opposite!
    +  // The decompression will need to know the compressed size, and an upper bound of the decompressed size.
    +  // In this example, we just re-use this information from previous section,
    +  // but in a real-world scenario, metadata must be transmitted to the decompression side.
    +  // Each implementation is in charge of this part. Oftentimes, it adds some header of its own.
    +  // Sometimes, the metadata can be extracted from the local context.
    +
    +  // First, let's create a *new_src location of size src_size since we know that value.
       char* const regen_buffer = malloc(src_size);
       if (regen_buffer == NULL)
         run_screaming("Failed to allocate memory for *regen_buffer.", 1);
    @@ -77,17 +82,17 @@ int main(void) {
       free(compressed_data);   /* no longer useful */
       if (decompressed_size < 0)
         run_screaming("A negative result from LZ4_decompress_safe indicates a failure trying to decompress the data.  See exit code (echo $?) for value returned.", decompressed_size);
    -  if (decompressed_size == 0)
    -    run_screaming("I'm not sure this function can ever return 0.  Documentation in lz4.h doesn't indicate so.", 1);
    -  if (decompressed_size > 0)
    +  if (decompressed_size >= 0)
         printf("We successfully decompressed some data!\n");
       // Not only does a positive return value mean success,
       // value returned == number of bytes regenerated from compressed_data stream.
    +  if (decompressed_size != src_size)
    +    run_screaming("Decompressed data is different from original! \n", 1);
     
       /* Validation */
       // We should be able to compare our original *src with our *new_src and be byte-for-byte identical.
       if (memcmp(src, regen_buffer, src_size) != 0)
         run_screaming("Validation failed.  *src and *new_src are not identical.", 1);
    -  printf("Validation done.  The string we ended up with is:\n%s\n", regen_buffer);
    +  printf("Validation done. The string we ended up with is:\n%s\n", regen_buffer);
       return 0;
     }
    -- 
    cgit v1.2.3
    
    
    From baf9b0e0434e1e516f468c4b96bde5bb38d4c737 Mon Sep 17 00:00:00 2001
    From: Yann Collet 
    Date: Mon, 24 Jun 2019 16:08:30 -0700
    Subject: fix #734 : --version should output to stdout
    
    instead of stderr
    ---
     programs/lz4cli.c | 5 +++--
     1 file changed, 3 insertions(+), 2 deletions(-)
    
    diff --git a/programs/lz4cli.c b/programs/lz4cli.c
    index 3619cd5..3b5d61e 100644
    --- a/programs/lz4cli.c
    +++ b/programs/lz4cli.c
    @@ -67,6 +67,7 @@ static int g_lz4c_legacy_commands = 0;
     /*-************************************
     *  Macros
     ***************************************/
    +#define DISPLAYOUT(...)        fprintf(stdout, __VA_ARGS__)
     #define DISPLAY(...)           fprintf(stderr, __VA_ARGS__)
     #define DISPLAYLEVEL(l, ...)   if (displayLevel>=l) { DISPLAY(__VA_ARGS__); }
     static unsigned displayLevel = 2;   /* 0 : no display ; 1: errors only ; 2 : downgradable normal ; 3 : non-downgradable normal; 4 : + information */
    @@ -390,7 +391,7 @@ int main(int argc, const char** argv)
                     if (!strcmp(argument,  "--favor-decSpeed")) { LZ4IO_favorDecSpeed(prefs, 1); continue; }
                     if (!strcmp(argument,  "--verbose")) { displayLevel++; continue; }
                     if (!strcmp(argument,  "--quiet")) { if (displayLevel) displayLevel--; continue; }
    -                if (!strcmp(argument,  "--version")) { DISPLAY(WELCOME_MESSAGE); return 0; }
    +                if (!strcmp(argument,  "--version")) { DISPLAYOUT(WELCOME_MESSAGE); return 0; }
                     if (!strcmp(argument,  "--help")) { usage_advanced(exeName); goto _cleanup; }
                     if (!strcmp(argument,  "--keep")) { LZ4IO_setRemoveSrcFile(prefs, 0); continue; }   /* keep source file (default) */
                     if (!strcmp(argument,  "--rm")) { LZ4IO_setRemoveSrcFile(prefs, 1); continue; }
    @@ -437,7 +438,7 @@ int main(int argc, const char** argv)
                     switch(argument[0])
                     {
                         /* Display help */
    -                case 'V': DISPLAY(WELCOME_MESSAGE); goto _cleanup;   /* Version */
    +                case 'V': DISPLAYOUT(WELCOME_MESSAGE); goto _cleanup;   /* Version */
                     case 'h': usage_advanced(exeName); goto _cleanup;
                     case 'H': usage_longhelp(exeName); goto _cleanup;
     
    -- 
    cgit v1.2.3
    
    
    From a5cf079d4dc9097c4e58f0eb7b0996b0a6d91696 Mon Sep 17 00:00:00 2001
    From: Max Dymond 
    Date: Tue, 25 Jun 2019 17:22:02 +0100
    Subject: Add a fuzzing target that compiles in the oss-fuzz environment
    
    ---
     .travis.yml                 |  6 ++++
     Makefile                    |  1 +
     ossfuzz/Makefile            | 54 +++++++++++++++++++++++++++++++++
     ossfuzz/compress_fuzzer.cc  | 22 ++++++++++++++
     ossfuzz/ossfuzz.sh          | 26 ++++++++++++++++
     ossfuzz/standaloneengine.cc | 74 +++++++++++++++++++++++++++++++++++++++++++++
     ossfuzz/testinput.h         |  3 ++
     ossfuzz/travisoss.sh        | 24 +++++++++++++++
     8 files changed, 210 insertions(+)
     create mode 100644 ossfuzz/Makefile
     create mode 100644 ossfuzz/compress_fuzzer.cc
     create mode 100755 ossfuzz/ossfuzz.sh
     create mode 100644 ossfuzz/standaloneengine.cc
     create mode 100644 ossfuzz/testinput.h
     create mode 100755 ossfuzz/travisoss.sh
    
    diff --git a/.travis.yml b/.travis.yml
    index ee643e5..4d45e89 100644
    --- a/.travis.yml
    +++ b/.travis.yml
    @@ -194,5 +194,11 @@ matrix:
             - pushd build
             - DESTDIR=./staging ninja install
             - tree ./staging
    +
    +    # oss-fuzz compilation test
    +    - name: Compile OSS-Fuzz targets
    +      script:
    +        - ./ossfuzz/travisoss.sh
    +
       allow_failures:
         - env: ALLOW_FAILURES=true
    diff --git a/Makefile b/Makefile
    index e24cec5..34835fd 100644
    --- a/Makefile
    +++ b/Makefile
    @@ -34,6 +34,7 @@ LZ4DIR  = lib
     PRGDIR  = programs
     TESTDIR = tests
     EXDIR   = examples
    +FUZZDIR = ossfuzz
     
     include Makefile.inc
     
    diff --git a/ossfuzz/Makefile b/ossfuzz/Makefile
    new file mode 100644
    index 0000000..94829b2
    --- /dev/null
    +++ b/ossfuzz/Makefile
    @@ -0,0 +1,54 @@
    +# ##########################################################################
    +# LZ4 oss fuzzer - Makefile
    +#
    +# 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 homepage : http://www.lz4.org
    +#  - LZ4 source repository : https://github.com/lz4/lz4
    +# ##########################################################################
    +# lz4_fuzzer : OSS Fuzz test tool
    +# ##########################################################################
    +
    +LZ4DIR  := ../lib
    +LIB_FUZZING_ENGINE ?= standaloneengine.o
    +
    +DEBUGLEVEL?= 1
    +DEBUGFLAGS = -g -DLZ4_DEBUG=$(DEBUGLEVEL)
    +
    +CFLAGS  += -I$(LZ4DIR) $(DEBUGFLAGS) $(MOREFLAGS)
    +CPPFLAGS+= -I$(LZ4DIR) -DXXH_NAMESPACE=LZ4_
    +FLAGS    = $(CFLAGS) $(CPPFLAGS) $(LDFLAGS)
    +
    +include ../Makefile.inc
    +
    +# Include a rule to build the static library if calling this target
    +# directly.
    +$(LZ4DIR)/liblz4.a:
    +	$(MAKE) -C $(LZ4DIR) CFLAGS="$(CFLAGS)" liblz4.a
    +
    +%.o: %.cc
    +	$(CXX) -c $(CFLAGS) $(CPPFLAGS) $< -o $@
    +
    +.PHONY: compress_fuzzer
    +compress_fuzzer: compress_fuzzer.o $(LZ4DIR)/liblz4.a
    +	# Compile the standalone code just in case. The OSS-Fuzz code might
    +	# override the LIB_FUZZING_ENGINE value to "-fsanitize=fuzzer"
    +	$(CXX) -c $(CFLAGS) $(CPPFLAGS) standaloneengine.cc -o standaloneengine.o
    +
    +	# Now compile the actual fuzzer.
    +	$(CXX) $(FLAGS) $(LIB_FUZZING_ENGINE) $^ -o $@$(EXT)
    diff --git a/ossfuzz/compress_fuzzer.cc b/ossfuzz/compress_fuzzer.cc
    new file mode 100644
    index 0000000..006a0ab
    --- /dev/null
    +++ b/ossfuzz/compress_fuzzer.cc
    @@ -0,0 +1,22 @@
    +#include 
    +#include 
    +#include 
    +#include "lz4.h"
    +
    +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
    +{
    +  size_t const compressed_dest_size = LZ4_compressBound(size);
    +  char *const dest_buffer = (char *)malloc(compressed_dest_size);
    +
    +  int result = LZ4_compress_default((const char*)data, dest_buffer,
    +                                    size, compressed_dest_size);
    +
    +  if (result == 0)
    +  {
    +    abort();
    +  }
    +
    +  free(dest_buffer);
    +
    +  return 0;
    +}
    diff --git a/ossfuzz/ossfuzz.sh b/ossfuzz/ossfuzz.sh
    new file mode 100755
    index 0000000..e0cb63c
    --- /dev/null
    +++ b/ossfuzz/ossfuzz.sh
    @@ -0,0 +1,26 @@
    +#!/bin/bash -eu
    +
    +# This script is called by the oss-fuzz main project when compiling the fuzz
    +# targets. This script is regression tested by travisoss.sh.
    +
    +# Save off the current folder as the build root.
    +export BUILD_ROOT=$PWD
    +
    +# lz4 uses CPPFLAGS rather than CXX flags.
    +export CPPFLAGS="${CXXFLAGS}"
    +
    +echo "CC: $CC"
    +echo "CXX: $CXX"
    +echo "LIB_FUZZING_ENGINE: $LIB_FUZZING_ENGINE"
    +echo "CFLAGS: $CFLAGS"
    +echo "CPPFLAGS: $CPPFLAGS"
    +echo "OUT: $OUT"
    +
    +export MAKEFLAGS+="-j$(nproc)"
    +
    +pushd ossfuzz
    +make V=1 compress_fuzzer
    +popd
    +
    +# Copy the fuzzers to the target directory.
    +cp -v ossfuzz/compress_fuzzer $OUT/
    diff --git a/ossfuzz/standaloneengine.cc b/ossfuzz/standaloneengine.cc
    new file mode 100644
    index 0000000..175360e
    --- /dev/null
    +++ b/ossfuzz/standaloneengine.cc
    @@ -0,0 +1,74 @@
    +#include 
    +#include 
    +#include 
    +
    +#include "testinput.h"
    +
    +/**
    + * Main procedure for standalone fuzzing engine.
    + *
    + * Reads filenames from the argument array. For each filename, read the file
    + * into memory and then call the fuzzing interface with the data.
    + */
    +int main(int argc, char **argv)
    +{
    +  int ii;
    +  for(ii = 1; ii < argc; ii++)
    +  {
    +    FILE *infile;
    +    printf("[%s] ", argv[ii]);
    +
    +    /* Try and open the file. */
    +    infile = fopen(argv[ii], "rb");
    +    if(infile)
    +    {
    +      uint8_t *buffer = NULL;
    +      size_t buffer_len;
    +
    +      printf("Opened.. ");
    +
    +      /* Get the length of the file. */
    +      fseek(infile, 0L, SEEK_END);
    +      buffer_len = ftell(infile);
    +
    +      /* Reset the file indicator to the beginning of the file. */
    +      fseek(infile, 0L, SEEK_SET);
    +
    +      /* Allocate a buffer for the file contents. */
    +      buffer = (uint8_t *)calloc(buffer_len, sizeof(uint8_t));
    +      if(buffer)
    +      {
    +        /* Read all the text from the file into the buffer. */
    +        fread(buffer, sizeof(uint8_t), buffer_len, infile);
    +        printf("Read %zu bytes, fuzzing.. ", buffer_len);
    +
    +        /* Call the fuzzer with the data. */
    +        LLVMFuzzerTestOneInput(buffer, buffer_len);
    +
    +        printf("complete !!");
    +
    +        /* Free the buffer as it's no longer needed. */
    +        free(buffer);
    +        buffer = NULL;
    +      }
    +      else
    +      {
    +        fprintf(stderr,
    +                "[%s] Failed to allocate %zu bytes \n",
    +                argv[ii],
    +                buffer_len);
    +      }
    +
    +      /* Close the file as it's no longer needed. */
    +      fclose(infile);
    +      infile = NULL;
    +    }
    +    else
    +    {
    +      /* Failed to open the file. Maybe wrong name or wrong permissions? */
    +      fprintf(stderr, "[%s] Open failed. \n", argv[ii]);
    +    }
    +
    +    printf("\n");
    +  }
    +}
    diff --git a/ossfuzz/testinput.h b/ossfuzz/testinput.h
    new file mode 100644
    index 0000000..6ab9b51
    --- /dev/null
    +++ b/ossfuzz/testinput.h
    @@ -0,0 +1,3 @@
    +#include 
    +
    +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
    diff --git a/ossfuzz/travisoss.sh b/ossfuzz/travisoss.sh
    new file mode 100755
    index 0000000..3b2f26f
    --- /dev/null
    +++ b/ossfuzz/travisoss.sh
    @@ -0,0 +1,24 @@
    +#!/bin/bash
    +
    +set -ex
    +
    +# Clone the oss-fuzz repository
    +git clone https://github.com/google/oss-fuzz.git /tmp/ossfuzz
    +
    +if [[ ! -d /tmp/ossfuzz/projects/lz4 ]]
    +then
    +    echo "Could not find the lz4 project in ossfuzz"
    +
    +    # Exit with a success code while the lz4 project is not expected to exist
    +    # on oss-fuzz.
    +    exit 0
    +fi
    +
    +# Modify the oss-fuzz Dockerfile so that we're checking out the current branch on travis.
    +sed -i "s@https://github.com/lz4/lz4.git@-b $TRAVIS_BRANCH https://github.com/lz4/lz4.git@" /tmp/ossfuzz/projects/lz4/Dockerfile
    +
    +# Try and build the fuzzers
    +pushd /tmp/ossfuzz
    +python infra/helper.py build_image --pull lz4
    +python infra/helper.py build_fuzzers lz4
    +popd
    -- 
    cgit v1.2.3
    
    
    From 88a7cfd7283ea9c51fd044b9f58aee47b9ed3d16 Mon Sep 17 00:00:00 2001
    From: Max Dymond 
    Date: Fri, 28 Jun 2019 20:54:46 +0100
    Subject: Code review markups:
    
    - Correct use of CPPFLAGS
    - Detect allocation failure
    - Add a CHECK macro for failure
    ---
     ossfuzz/Makefile           | 12 ++++++------
     ossfuzz/compress_fuzzer.cc | 18 +++++++++++-------
     ossfuzz/ossfuzz.sh         |  5 +----
     3 files changed, 18 insertions(+), 17 deletions(-)
    
    diff --git a/ossfuzz/Makefile b/ossfuzz/Makefile
    index 94829b2..2a7e439 100644
    --- a/ossfuzz/Makefile
    +++ b/ossfuzz/Makefile
    @@ -30,9 +30,9 @@ LIB_FUZZING_ENGINE ?= standaloneengine.o
     DEBUGLEVEL?= 1
     DEBUGFLAGS = -g -DLZ4_DEBUG=$(DEBUGLEVEL)
     
    -CFLAGS  += -I$(LZ4DIR) $(DEBUGFLAGS) $(MOREFLAGS)
    -CPPFLAGS+= -I$(LZ4DIR) -DXXH_NAMESPACE=LZ4_
    -FLAGS    = $(CFLAGS) $(CPPFLAGS) $(LDFLAGS)
    +CFLAGS   += -I$(LZ4DIR) $(DEBUGFLAGS) $(MOREFLAGS)
    +CXXFLAGS += -I$(LZ4DIR) $(DEBUGFLAGS) $(MOREFLAGS)
    +CPPFLAGS += -DXXH_NAMESPACE=LZ4_
     
     include ../Makefile.inc
     
    @@ -42,13 +42,13 @@ $(LZ4DIR)/liblz4.a:
     	$(MAKE) -C $(LZ4DIR) CFLAGS="$(CFLAGS)" liblz4.a
     
     %.o: %.cc
    -	$(CXX) -c $(CFLAGS) $(CPPFLAGS) $< -o $@
    +	$(CXX) -c $(CXXFLAGS) $(CPPFLAGS) $< -o $@
     
     .PHONY: compress_fuzzer
     compress_fuzzer: compress_fuzzer.o $(LZ4DIR)/liblz4.a
     	# Compile the standalone code just in case. The OSS-Fuzz code might
     	# override the LIB_FUZZING_ENGINE value to "-fsanitize=fuzzer"
    -	$(CXX) -c $(CFLAGS) $(CPPFLAGS) standaloneengine.cc -o standaloneengine.o
    +	$(CXX) -c $(CXXFLAGS) $(CPPFLAGS) standaloneengine.cc -o standaloneengine.o
     
     	# Now compile the actual fuzzer.
    -	$(CXX) $(FLAGS) $(LIB_FUZZING_ENGINE) $^ -o $@$(EXT)
    +	$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(LDFLAGS) $(LIB_FUZZING_ENGINE) $^ -o $@$(EXT)
    diff --git a/ossfuzz/compress_fuzzer.cc b/ossfuzz/compress_fuzzer.cc
    index 006a0ab..4a720e2 100644
    --- a/ossfuzz/compress_fuzzer.cc
    +++ b/ossfuzz/compress_fuzzer.cc
    @@ -3,20 +3,24 @@
     #include 
     #include "lz4.h"
     
    +#define CHECK(COND)   if (!(COND)) { abort(); }
    +
     extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
     {
       size_t const compressed_dest_size = LZ4_compressBound(size);
       char *const dest_buffer = (char *)malloc(compressed_dest_size);
     
    -  int result = LZ4_compress_default((const char*)data, dest_buffer,
    -                                    size, compressed_dest_size);
    -
    -  if (result == 0)
    +  if (dest_buffer != NULL)
       {
    -    abort();
    -  }
    +    // Allocation succeeded, try compressing the incoming data.
    +    int result = LZ4_compress_default((const char*)data,
    +                                      dest_buffer,
    +                                      size,
    +                                      compressed_dest_size);
    +    CHECK(result != 0);
     
    -  free(dest_buffer);
    +    free(dest_buffer);
    +  }
     
       return 0;
     }
    diff --git a/ossfuzz/ossfuzz.sh b/ossfuzz/ossfuzz.sh
    index e0cb63c..87bc213 100755
    --- a/ossfuzz/ossfuzz.sh
    +++ b/ossfuzz/ossfuzz.sh
    @@ -6,14 +6,11 @@
     # Save off the current folder as the build root.
     export BUILD_ROOT=$PWD
     
    -# lz4 uses CPPFLAGS rather than CXX flags.
    -export CPPFLAGS="${CXXFLAGS}"
    -
     echo "CC: $CC"
     echo "CXX: $CXX"
     echo "LIB_FUZZING_ENGINE: $LIB_FUZZING_ENGINE"
     echo "CFLAGS: $CFLAGS"
    -echo "CPPFLAGS: $CPPFLAGS"
    +echo "CXXFLAGS: $CXXFLAGS"
     echo "OUT: $OUT"
     
     export MAKEFLAGS+="-j$(nproc)"
    -- 
    cgit v1.2.3
    
    
    From 60d71dc20c5f9bb95e0b963ab6fb19212eb441a9 Mon Sep 17 00:00:00 2001
    From: Max Dymond 
    Date: Fri, 28 Jun 2019 22:19:27 +0100
    Subject: Write a simple decompress target as well
    
    ---
     ossfuzz/Makefile             |  7 ++++---
     ossfuzz/decompress_fuzzer.cc | 28 ++++++++++++++++++++++++++++
     ossfuzz/ossfuzz.sh           |  4 ++--
     3 files changed, 34 insertions(+), 5 deletions(-)
     create mode 100644 ossfuzz/decompress_fuzzer.cc
    
    diff --git a/ossfuzz/Makefile b/ossfuzz/Makefile
    index 2a7e439..1e7679b 100644
    --- a/ossfuzz/Makefile
    +++ b/ossfuzz/Makefile
    @@ -21,7 +21,8 @@
     #  - LZ4 homepage : http://www.lz4.org
     #  - LZ4 source repository : https://github.com/lz4/lz4
     # ##########################################################################
    -# lz4_fuzzer : OSS Fuzz test tool
    +# compress_fuzzer : OSS Fuzz test tool
    +# decompress_fuzzer : OSS Fuzz test tool
     # ##########################################################################
     
     LZ4DIR  := ../lib
    @@ -44,8 +45,8 @@ $(LZ4DIR)/liblz4.a:
     %.o: %.cc
     	$(CXX) -c $(CXXFLAGS) $(CPPFLAGS) $< -o $@
     
    -.PHONY: compress_fuzzer
    -compress_fuzzer: compress_fuzzer.o $(LZ4DIR)/liblz4.a
    +# Generic rule for generating fuzzers
    +%_fuzzer: %_fuzzer.o $(LZ4DIR)/liblz4.a
     	# Compile the standalone code just in case. The OSS-Fuzz code might
     	# override the LIB_FUZZING_ENGINE value to "-fsanitize=fuzzer"
     	$(CXX) -c $(CXXFLAGS) $(CPPFLAGS) standaloneengine.cc -o standaloneengine.o
    diff --git a/ossfuzz/decompress_fuzzer.cc b/ossfuzz/decompress_fuzzer.cc
    new file mode 100644
    index 0000000..594a5af
    --- /dev/null
    +++ b/ossfuzz/decompress_fuzzer.cc
    @@ -0,0 +1,28 @@
    +#include 
    +#include 
    +#include 
    +#include "lz4.h"
    +
    +#define CHECK(COND)   if (!(COND)) { abort(); }
    +
    +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
    +{
    +  size_t const buffer_size = 10 * 1024 * 1024;
    +  char *const dest_buffer = (char *)malloc(buffer_size);
    +
    +  if (dest_buffer != NULL)
    +  {
    +    // Allocation succeeded, try decompressing the incoming data.
    +    int result = LZ4_decompress_safe((const char*)data,
    +                                     dest_buffer,
    +                                     size,
    +                                     buffer_size);
    +
    +    // Ignore the result of decompression.
    +    (void)result;
    +
    +    free(dest_buffer);
    +  }
    +
    +  return 0;
    +}
    diff --git a/ossfuzz/ossfuzz.sh b/ossfuzz/ossfuzz.sh
    index 87bc213..a76b0d6 100755
    --- a/ossfuzz/ossfuzz.sh
    +++ b/ossfuzz/ossfuzz.sh
    @@ -16,8 +16,8 @@ echo "OUT: $OUT"
     export MAKEFLAGS+="-j$(nproc)"
     
     pushd ossfuzz
    -make V=1 compress_fuzzer
    +make V=1 compress_fuzzer decompress_fuzzer
     popd
     
     # Copy the fuzzers to the target directory.
    -cp -v ossfuzz/compress_fuzzer $OUT/
    +cp -v ossfuzz/compress_fuzzer ossfuzz/decompress_fuzzer $OUT/
    -- 
    cgit v1.2.3
    
    
    From e72d44230093f58be47c855e6b7d92493ce160db Mon Sep 17 00:00:00 2001
    From: Nick Terrell 
    Date: Fri, 28 Jun 2019 14:40:14 -0700
    Subject: Fix out-of-bounds read of up to 64 KB in the past
    
    ---
     lib/lz4.c | 10 ++++++++--
     1 file changed, 8 insertions(+), 2 deletions(-)
    
    diff --git a/lib/lz4.c b/lib/lz4.c
    index cac3240..d121e29 100644
    --- a/lib/lz4.c
    +++ b/lib/lz4.c
    @@ -1703,6 +1703,7 @@ LZ4_decompress_generic(
                 /* get offset */
                 offset = LZ4_readLE16(ip); ip+=2;
                 match = op - offset;
    +            assert(match <= op);
     
                 /* get matchlength */
                 length = token & ML_MASK;
    @@ -1724,8 +1725,12 @@ LZ4_decompress_generic(
                     }
     
                     /* Fastpath check: Avoids a branch in LZ4_wildCopy32 if true */
    -                if (!(dict == usingExtDict) || (match >= lowPrefix)) {
    +                if ((dict == withPrefix64k) || (match >= lowPrefix)) {
                         if (offset >= 8) {
    +                        assert(match >= lowPrefix);
    +                        assert(match <= op);
    +                        assert(op + 18 <= oend);
    +
                             memcpy(op, match, 8);
                             memcpy(op+8, match+8, 8);
                             memcpy(op+16, match+16, 2);
    @@ -1873,7 +1878,6 @@ LZ4_decompress_generic(
                 length = token & ML_MASK;
     
         _copy_match:
    -            if ((checkOffset) && (unlikely(match + dictSize < lowPrefix))) goto _output_error;   /* Error : offset outside buffers */
                 if (!partialDecoding) {
                     assert(oend > op);
                     assert(oend - op >= 4);
    @@ -1891,6 +1895,7 @@ LZ4_decompress_generic(
     #if LZ4_FAST_DEC_LOOP
             safe_match_copy:
     #endif
    +            if ((checkOffset) && (unlikely(match + dictSize < lowPrefix))) goto _output_error;   /* Error : offset outside buffers */
                 /* match starting within external dictionary */
                 if ((dict==usingExtDict) && (match < lowPrefix)) {
                     if (unlikely(op+length > oend-LASTLITERALS)) {
    @@ -1918,6 +1923,7 @@ LZ4_decompress_generic(
                     }   }
                     continue;
                 }
    +            assert(match >= lowPrefix);
     
                 /* copy match within block */
                 cpy = op + length;
    -- 
    cgit v1.2.3
    
    
    From 02b5b3c242fd4131983152f0dd422429e6702923 Mon Sep 17 00:00:00 2001
    From: Max Dymond 
    Date: Fri, 28 Jun 2019 23:48:33 +0100
    Subject: Move to using C rather than C++ for compilation
    
    ---
     Makefile                     |  1 +
     ossfuzz/Makefile             | 12 +++++--
     ossfuzz/compress_fuzzer.c    | 26 ++++++++++++++++
     ossfuzz/compress_fuzzer.cc   | 26 ----------------
     ossfuzz/decompress_fuzzer.c  | 28 +++++++++++++++++
     ossfuzz/decompress_fuzzer.cc | 28 -----------------
     ossfuzz/standaloneengine.c   | 74 ++++++++++++++++++++++++++++++++++++++++++++
     ossfuzz/standaloneengine.cc  | 74 --------------------------------------------
     ossfuzz/testinput.h          |  2 +-
     9 files changed, 139 insertions(+), 132 deletions(-)
     create mode 100644 ossfuzz/compress_fuzzer.c
     delete mode 100644 ossfuzz/compress_fuzzer.cc
     create mode 100644 ossfuzz/decompress_fuzzer.c
     delete mode 100644 ossfuzz/decompress_fuzzer.cc
     create mode 100644 ossfuzz/standaloneengine.c
     delete mode 100644 ossfuzz/standaloneengine.cc
    
    diff --git a/Makefile b/Makefile
    index 34835fd..f25f951 100644
    --- a/Makefile
    +++ b/Makefile
    @@ -77,6 +77,7 @@ clean:
     	@$(MAKE) -C $(PRGDIR) $@ > $(VOID)
     	@$(MAKE) -C $(TESTDIR) $@ > $(VOID)
     	@$(MAKE) -C $(EXDIR) $@ > $(VOID)
    +	@$(MAKE) -C $(FUZZDIR) $@ > $(VOID)
     	@$(MAKE) -C contrib/gen_manual $@ > $(VOID)
     	@$(RM) lz4$(EXT)
     	@echo Cleaning completed
    diff --git a/ossfuzz/Makefile b/ossfuzz/Makefile
    index 1e7679b..1480ccb 100644
    --- a/ossfuzz/Makefile
    +++ b/ossfuzz/Makefile
    @@ -42,14 +42,20 @@ include ../Makefile.inc
     $(LZ4DIR)/liblz4.a:
     	$(MAKE) -C $(LZ4DIR) CFLAGS="$(CFLAGS)" liblz4.a
     
    -%.o: %.cc
    -	$(CXX) -c $(CXXFLAGS) $(CPPFLAGS) $< -o $@
    +%.o: %.c
    +	$(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@
     
     # Generic rule for generating fuzzers
     %_fuzzer: %_fuzzer.o $(LZ4DIR)/liblz4.a
     	# Compile the standalone code just in case. The OSS-Fuzz code might
     	# override the LIB_FUZZING_ENGINE value to "-fsanitize=fuzzer"
    -	$(CXX) -c $(CXXFLAGS) $(CPPFLAGS) standaloneengine.cc -o standaloneengine.o
    +	$(CC) -c $(CFLAGS) $(CPPFLAGS) standaloneengine.c -o standaloneengine.o
     
     	# Now compile the actual fuzzer.
     	$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(LDFLAGS) $(LIB_FUZZING_ENGINE) $^ -o $@$(EXT)
    +
    +%_fuzzer_clean:
    +	$(RM) $*_fuzzer $*_fuzzer.o standaloneengine.o
    +
    +.PHONY: clean
    +clean: compress_fuzzer_clean decompress_fuzzer_clean
    diff --git a/ossfuzz/compress_fuzzer.c b/ossfuzz/compress_fuzzer.c
    new file mode 100644
    index 0000000..28610ad
    --- /dev/null
    +++ b/ossfuzz/compress_fuzzer.c
    @@ -0,0 +1,26 @@
    +#include 
    +#include 
    +#include 
    +#include "lz4.h"
    +
    +#define CHECK(COND)   if (!(COND)) { abort(); }
    +
    +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
    +{
    +  size_t const compressed_dest_size = LZ4_compressBound(size);
    +  char *const dest_buffer = (char *)malloc(compressed_dest_size);
    +
    +  if (dest_buffer != NULL)
    +  {
    +    // Allocation succeeded, try compressing the incoming data.
    +    int result = LZ4_compress_default((const char*)data,
    +                                      dest_buffer,
    +                                      size,
    +                                      compressed_dest_size);
    +    CHECK(result != 0);
    +
    +    free(dest_buffer);
    +  }
    +
    +  return 0;
    +}
    diff --git a/ossfuzz/compress_fuzzer.cc b/ossfuzz/compress_fuzzer.cc
    deleted file mode 100644
    index 4a720e2..0000000
    --- a/ossfuzz/compress_fuzzer.cc
    +++ /dev/null
    @@ -1,26 +0,0 @@
    -#include 
    -#include 
    -#include 
    -#include "lz4.h"
    -
    -#define CHECK(COND)   if (!(COND)) { abort(); }
    -
    -extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
    -{
    -  size_t const compressed_dest_size = LZ4_compressBound(size);
    -  char *const dest_buffer = (char *)malloc(compressed_dest_size);
    -
    -  if (dest_buffer != NULL)
    -  {
    -    // Allocation succeeded, try compressing the incoming data.
    -    int result = LZ4_compress_default((const char*)data,
    -                                      dest_buffer,
    -                                      size,
    -                                      compressed_dest_size);
    -    CHECK(result != 0);
    -
    -    free(dest_buffer);
    -  }
    -
    -  return 0;
    -}
    diff --git a/ossfuzz/decompress_fuzzer.c b/ossfuzz/decompress_fuzzer.c
    new file mode 100644
    index 0000000..1fa2b1a
    --- /dev/null
    +++ b/ossfuzz/decompress_fuzzer.c
    @@ -0,0 +1,28 @@
    +#include 
    +#include 
    +#include 
    +#include "lz4.h"
    +
    +#define CHECK(COND)   if (!(COND)) { abort(); }
    +
    +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
    +{
    +  size_t const buffer_size = 10 * 1024 * 1024;
    +  char *const dest_buffer = (char *)malloc(buffer_size);
    +
    +  if (dest_buffer != NULL)
    +  {
    +    // Allocation succeeded, try decompressing the incoming data.
    +    int result = LZ4_decompress_safe((const char*)data,
    +                                     dest_buffer,
    +                                     size,
    +                                     buffer_size);
    +
    +    // Ignore the result of decompression.
    +    (void)result;
    +
    +    free(dest_buffer);
    +  }
    +
    +  return 0;
    +}
    diff --git a/ossfuzz/decompress_fuzzer.cc b/ossfuzz/decompress_fuzzer.cc
    deleted file mode 100644
    index 594a5af..0000000
    --- a/ossfuzz/decompress_fuzzer.cc
    +++ /dev/null
    @@ -1,28 +0,0 @@
    -#include 
    -#include 
    -#include 
    -#include "lz4.h"
    -
    -#define CHECK(COND)   if (!(COND)) { abort(); }
    -
    -extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
    -{
    -  size_t const buffer_size = 10 * 1024 * 1024;
    -  char *const dest_buffer = (char *)malloc(buffer_size);
    -
    -  if (dest_buffer != NULL)
    -  {
    -    // Allocation succeeded, try decompressing the incoming data.
    -    int result = LZ4_decompress_safe((const char*)data,
    -                                     dest_buffer,
    -                                     size,
    -                                     buffer_size);
    -
    -    // Ignore the result of decompression.
    -    (void)result;
    -
    -    free(dest_buffer);
    -  }
    -
    -  return 0;
    -}
    diff --git a/ossfuzz/standaloneengine.c b/ossfuzz/standaloneengine.c
    new file mode 100644
    index 0000000..175360e
    --- /dev/null
    +++ b/ossfuzz/standaloneengine.c
    @@ -0,0 +1,74 @@
    +#include 
    +#include 
    +#include 
    +
    +#include "testinput.h"
    +
    +/**
    + * Main procedure for standalone fuzzing engine.
    + *
    + * Reads filenames from the argument array. For each filename, read the file
    + * into memory and then call the fuzzing interface with the data.
    + */
    +int main(int argc, char **argv)
    +{
    +  int ii;
    +  for(ii = 1; ii < argc; ii++)
    +  {
    +    FILE *infile;
    +    printf("[%s] ", argv[ii]);
    +
    +    /* Try and open the file. */
    +    infile = fopen(argv[ii], "rb");
    +    if(infile)
    +    {
    +      uint8_t *buffer = NULL;
    +      size_t buffer_len;
    +
    +      printf("Opened.. ");
    +
    +      /* Get the length of the file. */
    +      fseek(infile, 0L, SEEK_END);
    +      buffer_len = ftell(infile);
    +
    +      /* Reset the file indicator to the beginning of the file. */
    +      fseek(infile, 0L, SEEK_SET);
    +
    +      /* Allocate a buffer for the file contents. */
    +      buffer = (uint8_t *)calloc(buffer_len, sizeof(uint8_t));
    +      if(buffer)
    +      {
    +        /* Read all the text from the file into the buffer. */
    +        fread(buffer, sizeof(uint8_t), buffer_len, infile);
    +        printf("Read %zu bytes, fuzzing.. ", buffer_len);
    +
    +        /* Call the fuzzer with the data. */
    +        LLVMFuzzerTestOneInput(buffer, buffer_len);
    +
    +        printf("complete !!");
    +
    +        /* Free the buffer as it's no longer needed. */
    +        free(buffer);
    +        buffer = NULL;
    +      }
    +      else
    +      {
    +        fprintf(stderr,
    +                "[%s] Failed to allocate %zu bytes \n",
    +                argv[ii],
    +                buffer_len);
    +      }
    +
    +      /* Close the file as it's no longer needed. */
    +      fclose(infile);
    +      infile = NULL;
    +    }
    +    else
    +    {
    +      /* Failed to open the file. Maybe wrong name or wrong permissions? */
    +      fprintf(stderr, "[%s] Open failed. \n", argv[ii]);
    +    }
    +
    +    printf("\n");
    +  }
    +}
    diff --git a/ossfuzz/standaloneengine.cc b/ossfuzz/standaloneengine.cc
    deleted file mode 100644
    index 175360e..0000000
    --- a/ossfuzz/standaloneengine.cc
    +++ /dev/null
    @@ -1,74 +0,0 @@
    -#include 
    -#include 
    -#include 
    -
    -#include "testinput.h"
    -
    -/**
    - * Main procedure for standalone fuzzing engine.
    - *
    - * Reads filenames from the argument array. For each filename, read the file
    - * into memory and then call the fuzzing interface with the data.
    - */
    -int main(int argc, char **argv)
    -{
    -  int ii;
    -  for(ii = 1; ii < argc; ii++)
    -  {
    -    FILE *infile;
    -    printf("[%s] ", argv[ii]);
    -
    -    /* Try and open the file. */
    -    infile = fopen(argv[ii], "rb");
    -    if(infile)
    -    {
    -      uint8_t *buffer = NULL;
    -      size_t buffer_len;
    -
    -      printf("Opened.. ");
    -
    -      /* Get the length of the file. */
    -      fseek(infile, 0L, SEEK_END);
    -      buffer_len = ftell(infile);
    -
    -      /* Reset the file indicator to the beginning of the file. */
    -      fseek(infile, 0L, SEEK_SET);
    -
    -      /* Allocate a buffer for the file contents. */
    -      buffer = (uint8_t *)calloc(buffer_len, sizeof(uint8_t));
    -      if(buffer)
    -      {
    -        /* Read all the text from the file into the buffer. */
    -        fread(buffer, sizeof(uint8_t), buffer_len, infile);
    -        printf("Read %zu bytes, fuzzing.. ", buffer_len);
    -
    -        /* Call the fuzzer with the data. */
    -        LLVMFuzzerTestOneInput(buffer, buffer_len);
    -
    -        printf("complete !!");
    -
    -        /* Free the buffer as it's no longer needed. */
    -        free(buffer);
    -        buffer = NULL;
    -      }
    -      else
    -      {
    -        fprintf(stderr,
    -                "[%s] Failed to allocate %zu bytes \n",
    -                argv[ii],
    -                buffer_len);
    -      }
    -
    -      /* Close the file as it's no longer needed. */
    -      fclose(infile);
    -      infile = NULL;
    -    }
    -    else
    -    {
    -      /* Failed to open the file. Maybe wrong name or wrong permissions? */
    -      fprintf(stderr, "[%s] Open failed. \n", argv[ii]);
    -    }
    -
    -    printf("\n");
    -  }
    -}
    diff --git a/ossfuzz/testinput.h b/ossfuzz/testinput.h
    index 6ab9b51..8da6215 100644
    --- a/ossfuzz/testinput.h
    +++ b/ossfuzz/testinput.h
    @@ -1,3 +1,3 @@
     #include 
     
    -extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
    +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
    -- 
    cgit v1.2.3
    
    
    From e2a33f12e1198d3f5de374519d7a034a8736f6c8 Mon Sep 17 00:00:00 2001
    From: Max Dymond 
    Date: Sat, 29 Jun 2019 00:23:06 +0100
    Subject: More markups for style changes
    
    ---
     ossfuzz/Makefile            | 14 +++++++-------
     ossfuzz/compress_fuzzer.c   | 19 +++++++++----------
     ossfuzz/decompress_fuzzer.c | 22 +++++++++++-----------
     ossfuzz/testinput.h         | 12 ++++++++++++
     4 files changed, 39 insertions(+), 28 deletions(-)
    
    diff --git a/ossfuzz/Makefile b/ossfuzz/Makefile
    index 1480ccb..7812c41 100644
    --- a/ossfuzz/Makefile
    +++ b/ossfuzz/Makefile
    @@ -31,28 +31,28 @@ LIB_FUZZING_ENGINE ?= standaloneengine.o
     DEBUGLEVEL?= 1
     DEBUGFLAGS = -g -DLZ4_DEBUG=$(DEBUGLEVEL)
     
    -CFLAGS   += -I$(LZ4DIR) $(DEBUGFLAGS) $(MOREFLAGS)
    -CXXFLAGS += -I$(LZ4DIR) $(DEBUGFLAGS) $(MOREFLAGS)
    -CPPFLAGS += -DXXH_NAMESPACE=LZ4_
    +LZ4_CFLAGS  = $(CFLAGS) $(DEBUGFLAGS) $(MOREFLAGS)
    +LZ4_CXXFLAGS = $(CXXFLAGS) $(DEBUGFLAGS) $(MOREFLAGS)
    +LZ4_CPPFLAGS = $(CPPFLAGS) -I$(LZ4DIR) -DXXH_NAMESPACE=LZ4_
     
     include ../Makefile.inc
     
     # Include a rule to build the static library if calling this target
     # directly.
     $(LZ4DIR)/liblz4.a:
    -	$(MAKE) -C $(LZ4DIR) CFLAGS="$(CFLAGS)" liblz4.a
    +	$(MAKE) -C $(LZ4DIR) CFLAGS="$(LZ4_CFLAGS)" liblz4.a
     
     %.o: %.c
    -	$(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@
    +	$(CC) -c $(LZ4_CFLAGS) $(LZ4_CPPFLAGS) $< -o $@
     
     # Generic rule for generating fuzzers
     %_fuzzer: %_fuzzer.o $(LZ4DIR)/liblz4.a
     	# Compile the standalone code just in case. The OSS-Fuzz code might
     	# override the LIB_FUZZING_ENGINE value to "-fsanitize=fuzzer"
    -	$(CC) -c $(CFLAGS) $(CPPFLAGS) standaloneengine.c -o standaloneengine.o
    +	$(CC) -c $(LZ4_CFLAGS) $(LZ4_CPPFLAGS) standaloneengine.c -o standaloneengine.o
     
     	# Now compile the actual fuzzer.
    -	$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(LDFLAGS) $(LIB_FUZZING_ENGINE) $^ -o $@$(EXT)
    +	$(CXX) $(LZ4_CXXFLAGS) $(LZ4_CPPFLAGS) $(LDFLAGS) $(LIB_FUZZING_ENGINE) $^ -o $@$(EXT)
     
     %_fuzzer_clean:
     	$(RM) $*_fuzzer $*_fuzzer.o standaloneengine.o
    diff --git a/ossfuzz/compress_fuzzer.c b/ossfuzz/compress_fuzzer.c
    index 28610ad..3908534 100644
    --- a/ossfuzz/compress_fuzzer.c
    +++ b/ossfuzz/compress_fuzzer.c
    @@ -10,17 +10,16 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
       size_t const compressed_dest_size = LZ4_compressBound(size);
       char *const dest_buffer = (char *)malloc(compressed_dest_size);
     
    -  if (dest_buffer != NULL)
    -  {
    -    // Allocation succeeded, try compressing the incoming data.
    -    int result = LZ4_compress_default((const char*)data,
    -                                      dest_buffer,
    -                                      size,
    -                                      compressed_dest_size);
    -    CHECK(result != 0);
    +  CHECK(dest_buffer != NULL);
     
    -    free(dest_buffer);
    -  }
    +  // Allocation succeeded, try compressing the incoming data.
    +  int result = LZ4_compress_default((const char*)data,
    +                                    dest_buffer,
    +                                    size,
    +                                    compressed_dest_size);
    +  CHECK(result != 0);
    +
    +  free(dest_buffer);
     
       return 0;
     }
    diff --git a/ossfuzz/decompress_fuzzer.c b/ossfuzz/decompress_fuzzer.c
    index 1fa2b1a..e6e14c4 100644
    --- a/ossfuzz/decompress_fuzzer.c
    +++ b/ossfuzz/decompress_fuzzer.c
    @@ -7,22 +7,22 @@
     
     int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
     {
    +  // TODO: Size input buffer pseudo-randomly based on seed extracted from input
       size_t const buffer_size = 10 * 1024 * 1024;
       char *const dest_buffer = (char *)malloc(buffer_size);
     
    -  if (dest_buffer != NULL)
    -  {
    -    // Allocation succeeded, try decompressing the incoming data.
    -    int result = LZ4_decompress_safe((const char*)data,
    -                                     dest_buffer,
    -                                     size,
    -                                     buffer_size);
    +  CHECK(dest_buffer != NULL);
     
    -    // Ignore the result of decompression.
    -    (void)result;
    +  // Allocation succeeded, try decompressing the incoming data.
    +  int result = LZ4_decompress_safe((const char*)data,
    +                                   dest_buffer,
    +                                   size,
    +                                   buffer_size);
     
    -    free(dest_buffer);
    -  }
    +  // Ignore the result of decompression.
    +  (void)result;
    +
    +  free(dest_buffer);
     
       return 0;
     }
    diff --git a/ossfuzz/testinput.h b/ossfuzz/testinput.h
    index 8da6215..0e50a3c 100644
    --- a/ossfuzz/testinput.h
    +++ b/ossfuzz/testinput.h
    @@ -1,3 +1,15 @@
    +#ifndef TESTINPUT_H_INCLUDED
    +#define TESTINPUT_H_INCLUDED
    +
     #include 
     
    +#if defined (__cplusplus)
    +extern "C" {
    +#endif
    +
     int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
    +
    +#if defined(__cplusplus)
    +}
    +#endif
    +#endif
    -- 
    cgit v1.2.3
    
    
    From 4c60f25c6510f81a6bc2921a3fb7a77dc71f5d8a Mon Sep 17 00:00:00 2001
    From: Lzu Tao 
    Date: Sat, 29 Jun 2019 10:10:05 +0700
    Subject: meson: Fix deprecated warnings on build options
    
    Meson now reserves `build_` prefix options.
    ---
     contrib/meson/README.md         |  2 +-
     contrib/meson/meson.build       | 18 +++++++++---------
     contrib/meson/meson_options.txt |  8 ++++----
     3 files changed, 14 insertions(+), 14 deletions(-)
    
    diff --git a/contrib/meson/README.md b/contrib/meson/README.md
    index fa18493..a44850a 100644
    --- a/contrib/meson/README.md
    +++ b/contrib/meson/README.md
    @@ -13,7 +13,7 @@ This Meson build system is provided with no guarantee.
     `cd` to this meson directory (`contrib/meson`)
     
     ```sh
    -meson setup --buildtype=release -Ddefault_library=shared -Dbuild_programs=true builddir
    +meson setup --buildtype=release -Ddefault_library=shared -Dbin_programs=true builddir
     cd builddir
     ninja             # to build
     ninja install     # to install
    diff --git a/contrib/meson/meson.build b/contrib/meson/meson.build
    index c28d90a..65a4c26 100644
    --- a/contrib/meson/meson.build
    +++ b/contrib/meson/meson.build
    @@ -69,17 +69,17 @@ use_debug = get_option('debug')
     debug_level = get_option('debug_level')
     use_backtrace = get_option('backtrace')
     
    -build_programs = get_option('build_programs')
    -build_contrib = get_option('build_contrib')
    -build_tests = get_option('build_tests')
    -build_examples = get_option('build_examples')
    +bin_programs = get_option('bin_programs')
    +bin_contrib = get_option('bin_contrib')
    +bin_tests = get_option('bin_tests')
    +bin_examples = get_option('bin_examples')
     #feature_multi_thread = get_option('multi_thread')
     
     # =============================================================================
     # Dependencies
     # =============================================================================
     
    -#libm_dep = cc.find_library('m', required: build_tests)
    +#libm_dep = cc.find_library('m', required: bin_tests)
     #thread_dep = dependency('threads', required: feature_multi_thread)
     #use_multi_thread = thread_dep.found()
     
    @@ -108,18 +108,18 @@ endif
     
     subdir('lib')
     
    -if build_programs
    +if bin_programs
       subdir('programs')
     endif
     
    -if build_tests
    +if bin_tests
       subdir('tests')
     endif
     
    -if build_contrib
    +if bin_contrib
       subdir('contrib')
     endif
     
    -if build_examples
    +if bin_examples
       subdir('examples')
     endif
    diff --git a/contrib/meson/meson_options.txt b/contrib/meson/meson_options.txt
    index f6a4ae7..a409c2d 100644
    --- a/contrib/meson/meson_options.txt
    +++ b/contrib/meson/meson_options.txt
    @@ -14,11 +14,11 @@ option('debug_level', type: 'integer', min: 0, max: 7, value: 1,
     option('backtrace', type: 'boolean', value: false,
       description: 'Display a stack backtrace when execution generates a runtime exception')
     
    -option('build_programs', type: 'boolean', value: false,
    +option('bin_programs', type: 'boolean', value: false,
       description: 'Enable programs build')
    -option('build_tests', type: 'boolean', value: false,
    +option('bin_tests', type: 'boolean', value: false,
       description: 'Enable tests build')
    -option('build_contrib', type: 'boolean', value: false,
    +option('bin_contrib', type: 'boolean', value: false,
       description: 'Enable contrib build')
    -option('build_examples', type: 'boolean', value: false,
    +option('bin_examples', type: 'boolean', value: false,
       description: 'Enable examples build')
    -- 
    cgit v1.2.3
    
    
    From ff27a1572b830a82917eefc4008debc433f837d3 Mon Sep 17 00:00:00 2001
    From: Lzu Tao 
    Date: Sat, 29 Jun 2019 10:11:32 +0700
    Subject: meson: Always build gen_manual on build machine
    
    As gen_manual is using as a generator, not a binary target
    installed on host machine.
    ---
     contrib/meson/contrib/gen_manual/meson.build | 1 +
     1 file changed, 1 insertion(+)
    
    diff --git a/contrib/meson/contrib/gen_manual/meson.build b/contrib/meson/contrib/gen_manual/meson.build
    index 6233cdc..38180e9 100644
    --- a/contrib/meson/contrib/gen_manual/meson.build
    +++ b/contrib/meson/contrib/gen_manual/meson.build
    @@ -22,6 +22,7 @@ gen_manual = executable('gen_manual',
       join_paths(lz4_root_dir, 'contrib/gen_manual/gen_manual.cpp'),
       cpp_args: gen_manual_cppflags,
       include_directories: gen_manual_includes,
    +  native: true,
       install: false)
     
     # Update lz4 manual
    -- 
    cgit v1.2.3
    
    
    From 62f59d562b54e8329f585136d37d673f04933a19 Mon Sep 17 00:00:00 2001
    From: Yann Collet 
    Date: Fri, 28 Jun 2019 20:15:43 -0700
    Subject: fuzzer: added test to catch #738
    
    ---
     tests/fuzzer.c | 141 ++++++++++++++++++++++++++++++++++++---------------------
     1 file changed, 90 insertions(+), 51 deletions(-)
    
    diff --git a/tests/fuzzer.c b/tests/fuzzer.c
    index b45620b..db26b72 100644
    --- a/tests/fuzzer.c
    +++ b/tests/fuzzer.c
    @@ -122,6 +122,14 @@ static U32 FUZ_rotl32(U32 u32, U32 nbBits)
         return ((u32 << nbBits) | (u32 >> (32 - nbBits)));
     }
     
    +static U32 FUZ_highbit32(U32 v32)
    +{
    +    unsigned nbBits = 0;
    +    if (v32==0) return 0;
    +    while (v32) v32 >>= 1, nbBits++;
    +    return nbBits;
    +}
    +
     static U32 FUZ_rand(U32* src)
     {
         U32 rand32 = *src;
    @@ -366,7 +374,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c
             U32 testNb = 0;
             U32 randState = FUZ_rand(&coreRandState) ^ PRIME3;
             int const blockSize  = (FUZ_rand(&randState) % (FUZ_MAX_BLOCK_SIZE-1)) + 1;
    -        int const blockStart = (int)(FUZ_rand(&randState) % (COMPRESSIBLE_NOISE_LENGTH - blockSize - 1)) + 1;
    +        int const blockStart = (int)(FUZ_rand(&randState) % (U32)(COMPRESSIBLE_NOISE_LENGTH - blockSize - 1)) + 1;
             int const dictSizeRand = FUZ_rand(&randState) % FUZ_MAX_DICT_SIZE;
             int const dictSize = MIN(dictSizeRand, blockStart - 1);
             int const compressionLevel = FUZ_rand(&randState) % (LZ4HC_CLEVEL_MAX+1);
    @@ -388,69 +396,65 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c
     
             /* Test compression destSize */
             FUZ_DISPLAYTEST("test LZ4_compress_destSize()");
    -        {   int srcSize = blockSize;
    +        {   int cSize, srcSize = blockSize;
                 int const targetSize = srcSize * (int)((FUZ_rand(&randState) & 127)+1) >> 7;
    -            char endCheck = (char)(FUZ_rand(&randState) & 255);
    +            char const endCheck = (char)(FUZ_rand(&randState) & 255);
                 compressedBuffer[targetSize] = endCheck;
    -            ret = LZ4_compress_destSize(block, compressedBuffer, &srcSize, targetSize);
    -            FUZ_CHECKTEST(ret > targetSize, "LZ4_compress_destSize() result larger than dst buffer !");
    +            cSize = LZ4_compress_destSize(block, compressedBuffer, &srcSize, targetSize);
    +            FUZ_CHECKTEST(cSize > targetSize, "LZ4_compress_destSize() result larger than dst buffer !");
                 FUZ_CHECKTEST(compressedBuffer[targetSize] != endCheck, "LZ4_compress_destSize() overwrite dst buffer !");
    -            FUZ_CHECKTEST(srcSize > blockSize, "LZ4_compress_destSize() fed more than src buffer !");
    -            DISPLAYLEVEL(5, "destSize : %7i/%7i; content%7i/%7i ", ret, targetSize, srcSize, blockSize);
    +            FUZ_CHECKTEST(srcSize > blockSize, "LZ4_compress_destSize() read more than src buffer !");
    +            DISPLAYLEVEL(5, "destSize : %7i/%7i; content%7i/%7i ", cSize, targetSize, srcSize, blockSize);
                 if (targetSize>0) {
                     /* check correctness */
                     U32 const crcBase = XXH32(block, (size_t)srcSize, 0);
                     char const canary = (char)(FUZ_rand(&randState) & 255);
    -                FUZ_CHECKTEST((ret==0), "LZ4_compress_destSize() compression failed");
    +                FUZ_CHECKTEST((cSize==0), "LZ4_compress_destSize() compression failed");
                     FUZ_DISPLAYTEST();
    -                compressedSize = ret;
                     decodedBuffer[srcSize] = canary;
    -                ret = LZ4_decompress_safe(compressedBuffer, decodedBuffer, compressedSize, srcSize);
    -                FUZ_CHECKTEST(ret<0, "LZ4_decompress_safe() failed on data compressed by LZ4_compress_destSize");
    -                FUZ_CHECKTEST(ret!=srcSize, "LZ4_decompress_safe() failed : did not fully decompressed data");
    +                {   int const dSize = LZ4_decompress_safe(compressedBuffer, decodedBuffer, cSize, srcSize);
    +                    FUZ_CHECKTEST(dSize<0, "LZ4_decompress_safe() failed on data compressed by LZ4_compress_destSize");
    +                    FUZ_CHECKTEST(dSize!=srcSize, "LZ4_decompress_safe() failed : did not fully decompressed data");
    +                }
                     FUZ_CHECKTEST(decodedBuffer[srcSize] != canary, "LZ4_decompress_safe() overwrite dst buffer !");
    -                { U32 const crcDec = XXH32(decodedBuffer, (size_t)srcSize, 0);
    -                  FUZ_CHECKTEST(crcDec!=crcBase, "LZ4_decompress_safe() corrupted decoded data"); }
    -
    -                DISPLAYLEVEL(5, " OK \n");
    -            } else {
    -                DISPLAYLEVEL(5, " \n");
    -        }   }
    +                {   U32 const crcDec = XXH32(decodedBuffer, (size_t)srcSize, 0);
    +                    FUZ_CHECKTEST(crcDec!=crcBase, "LZ4_decompress_safe() corrupted decoded data");
    +            }   }
    +            DISPLAYLEVEL(5, " OK \n");
    +        }
     
             /* Test compression HC destSize */
             FUZ_DISPLAYTEST("test LZ4_compress_HC_destSize()");
    -        {   int srcSize = blockSize;
    +        {   int cSize, srcSize = blockSize;
                 int const targetSize = srcSize * (int)((FUZ_rand(&randState) & 127)+1) >> 7;
                 char const endCheck = (char)(FUZ_rand(&randState) & 255);
    -            void* ctx = LZ4_createHC(block);
    +            void* const ctx = LZ4_createHC(block);
                 FUZ_CHECKTEST(ctx==NULL, "LZ4_createHC() allocation failed");
                 compressedBuffer[targetSize] = endCheck;
    -            ret = LZ4_compress_HC_destSize(ctx, block, compressedBuffer, &srcSize, targetSize, compressionLevel);
    +            cSize = LZ4_compress_HC_destSize(ctx, block, compressedBuffer, &srcSize, targetSize, compressionLevel);
                 DISPLAYLEVEL(5, "LZ4_compress_HC_destSize(%i): destSize : %7i/%7i; content%7i/%7i ",
    -                            compressionLevel, ret, targetSize, srcSize, blockSize);
    +                            compressionLevel, cSize, targetSize, srcSize, blockSize);
                 LZ4_freeHC(ctx);
    -            FUZ_CHECKTEST(ret > targetSize, "LZ4_compress_HC_destSize() result larger than dst buffer !");
    +            FUZ_CHECKTEST(cSize > targetSize, "LZ4_compress_HC_destSize() result larger than dst buffer !");
                 FUZ_CHECKTEST(compressedBuffer[targetSize] != endCheck, "LZ4_compress_HC_destSize() overwrite dst buffer !");
                 FUZ_CHECKTEST(srcSize > blockSize, "LZ4_compress_HC_destSize() fed more than src buffer !");
                 if (targetSize>0) {
                     /* check correctness */
                     U32 const crcBase = XXH32(block, (size_t)srcSize, 0);
                     char const canary = (char)(FUZ_rand(&randState) & 255);
    -                FUZ_CHECKTEST((ret==0), "LZ4_compress_HC_destSize() compression failed");
    +                FUZ_CHECKTEST((cSize==0), "LZ4_compress_HC_destSize() compression failed");
                     FUZ_DISPLAYTEST();
    -                compressedSize = ret;
                     decodedBuffer[srcSize] = canary;
    -                ret = LZ4_decompress_safe(compressedBuffer, decodedBuffer, compressedSize, srcSize);
    -                FUZ_CHECKTEST(ret<0, "LZ4_decompress_safe() failed on data compressed by LZ4_compressHC_destSize");
    -                FUZ_CHECKTEST(ret!=srcSize, "LZ4_decompress_safe() failed : did not fully decompressed data");
    +                {   int const dSize = LZ4_decompress_safe(compressedBuffer, decodedBuffer, cSize, srcSize);
    +                    FUZ_CHECKTEST(dSize<0, "LZ4_decompress_safe() failed on data compressed by LZ4_compressHC_destSize");
    +                    FUZ_CHECKTEST(dSize!=srcSize, "LZ4_decompress_safe() failed : did not fully decompressed data");
    +                }
                     FUZ_CHECKTEST(decodedBuffer[srcSize] != canary, "LZ4_decompress_safe() overwrite dst buffer !");
                     {   U32 const crcDec = XXH32(decodedBuffer, (size_t)srcSize, 0);
                         FUZ_CHECKTEST(crcDec!=crcBase, "LZ4_decompress_safe() corrupted decoded data");
    -                }
    -                DISPLAYLEVEL(5, " OK \n");
    -            } else {
    -                DISPLAYLEVEL(5, " \n");
    -        }   }
    +            }   }
    +            DISPLAYLEVEL(5, " OK \n");
    +        }
     
             /* Test compression HC */
             FUZ_DISPLAYTEST("test LZ4_compress_HC()");
    @@ -565,7 +569,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c
             /* Test decoding with input size being one byte too short => must fail */
             FUZ_DISPLAYTEST();
             {   int const r = LZ4_decompress_safe(compressedBuffer, decodedBuffer, compressedSize-1, blockSize);
    -            FUZ_CHECKTEST(r>=0, "LZ4_decompress_safe should have failed, due to input size being one byte too short (blockSize=%i, ret=%i, compressedSize=%i)", blockSize, ret, compressedSize);
    +            FUZ_CHECKTEST(r>=0, "LZ4_decompress_safe should have failed, due to input size being one byte too short (blockSize=%i, result=%i, compressedSize=%i)", blockSize, r, compressedSize);
             }
     
             /* Test decoding with input size being one byte too large => must fail */
    @@ -587,6 +591,42 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c
                 FUZ_CHECKTEST(decodedBuffer[targetSize] != sentinel, "LZ4_decompress_safe_partial overwrite beyond requested size (though %i <= %i <= %i)", decResult, targetSize, blockSize);
             }
     
    +        /* noisy src decompression test */
    +        {
    +            /* insert noise into src */
    +            {   U32 const maxNbBits = FUZ_highbit32((U32)compressedSize);
    +                size_t pos = 0;
    +                for (;;) {
    +                    /* keep some original src */
    +                    {   U32 const nbBits = FUZ_rand(&randState) % maxNbBits;
    +                        size_t const mask = (1<= (size_t)compressedSize) break;
    +                    /* add noise */
    +                    {   U32 const nbBitsCodes = FUZ_rand(&randState) % maxNbBits;
    +                        U32 const nbBits = nbBitsCodes ? nbBitsCodes-1 : 0;
    +                        size_t const mask = (1< blockSize, "LZ4_decompress_safe on noisy src : result is too large : %u > %u (dst buffer)", (unsigned)decompressResult, (unsigned)blockSize);
    +                }
    +                {   U32 endCheck; memcpy(&endCheck, decodedBuffer+blockSize, sizeof(endCheck));
    +                    FUZ_CHECKTEST(endMark!=endCheck, "LZ4_decompress_safe on noisy src : dst buffer overflow");
    +        }   }   }   /* noisy src decompression test */
    +
             /* Test Compression with limited output size */
     
             /* Test compression with output size being exactly what's necessary (should work) */
    @@ -615,8 +655,9 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c
                 if (missingBytes >= compressedSize) missingBytes = compressedSize-1;
                 missingBytes += !missingBytes;   /* avoid special case missingBytes==0 */
                 compressedBuffer[compressedSize-missingBytes] = 0;
    -            ret = LZ4_compress_default(block, compressedBuffer, blockSize, compressedSize-missingBytes);
    -            FUZ_CHECKTEST(ret, "LZ4_compress_default should have failed (output buffer too small by %i byte)", missingBytes);
    +            {   int const cSize = LZ4_compress_default(block, compressedBuffer, blockSize, compressedSize-missingBytes);
    +                FUZ_CHECKTEST(cSize, "LZ4_compress_default should have failed (output buffer too small by %i byte)", missingBytes);
    +            }
                 FUZ_CHECKTEST(compressedBuffer[compressedSize-missingBytes], "LZ4_compress_default overran output buffer ! (%i missingBytes)", missingBytes)
             }
     
    @@ -626,8 +667,9 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c
                 if (missingBytes >= HCcompressedSize) missingBytes = HCcompressedSize-1;
                 missingBytes += !missingBytes;   /* avoid special case missingBytes==0 */
                 compressedBuffer[HCcompressedSize-missingBytes] = 0;
    -            ret = LZ4_compress_HC(block, compressedBuffer, blockSize, HCcompressedSize-missingBytes, compressionLevel);
    -            FUZ_CHECKTEST(ret, "LZ4_compress_HC should have failed (output buffer too small by %i byte)", missingBytes);
    +            {   int const hcSize = LZ4_compress_HC(block, compressedBuffer, blockSize, HCcompressedSize-missingBytes, compressionLevel);
    +                FUZ_CHECKTEST(hcSize, "LZ4_compress_HC should have failed (output buffer too small by %i byte)", missingBytes);
    +            }
                 FUZ_CHECKTEST(compressedBuffer[HCcompressedSize-missingBytes], "LZ4_compress_HC overran output buffer ! (%i missingBytes)", missingBytes)
             }
     
    @@ -654,8 +696,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c
                 if (crcCheck!=crcOrig) {
                     FUZ_findDiff(block, decodedBuffer);
                     EXIT_MSG("LZ4_decompress_fast_usingDict corrupted decoded data (dict %i)", dictSize);
    -            }
    -        }
    +        }   }
     
             FUZ_DISPLAYTEST("test LZ4_decompress_safe_usingDict()");
             ret = LZ4_decompress_safe_usingDict(compressedBuffer, decodedBuffer+dictSize, blockContinueCompressedSize, blockSize, decodedBuffer, dictSize);
    @@ -697,8 +738,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c
                 if (crcCheck!=crcOrig) {
                     FUZ_findDiff(block, decodedBuffer);
                     EXIT_MSG("LZ4_decompress_fast_usingDict corrupted decoded data (dict %i)", dictSize);
    -            }
    -        }
    +        }   }
     
             FUZ_DISPLAYTEST();
             decodedBuffer[blockSize] = 0;
    @@ -722,8 +762,8 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c
             FUZ_CHECKTEST(decodedBuffer[blockSize-1], "LZ4_decompress_safe_usingDict overrun specified output buffer size");
     
             FUZ_DISPLAYTEST();
    -        {   U32 const missingBytes = (FUZ_rand(&randState) & 0xF) + 2;
    -            if ((U32)blockSize > missingBytes) {
    +        {   int const missingBytes = (FUZ_rand(&randState) & 0xF) + 2;
    +            if (blockSize > missingBytes) {
                     decodedBuffer[blockSize-missingBytes] = 0;
                     ret = LZ4_decompress_safe_usingDict(compressedBuffer, decodedBuffer, blockContinueCompressedSize, blockSize-missingBytes, dict, dictSize);
                     FUZ_CHECKTEST(ret>=0, "LZ4_decompress_safe_usingDict should have failed : output buffer too small (-%u byte)", missingBytes);
    @@ -731,8 +771,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c
             }   }
     
             /* Compress using external dictionary stream */
    -        {
    -            LZ4_stream_t LZ4_stream;
    +        {   LZ4_stream_t LZ4_stream;
                 int expectedSize;
                 U32 expectedCrc;
     
    @@ -740,7 +779,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c
                 LZ4_loadDict(&LZ4dictBody, dict, dictSize);
                 expectedSize = LZ4_compress_fast_continue(&LZ4dictBody, block, compressedBuffer, blockSize, (int)compressedBufferSize, 1);
                 FUZ_CHECKTEST(expectedSize<=0, "LZ4_compress_fast_continue reference compression for extDictCtx should have succeeded");
    -            expectedCrc = XXH32(compressedBuffer, expectedSize, 0);
    +            expectedCrc = XXH32(compressedBuffer, (size_t)expectedSize, 0);
     
                 FUZ_DISPLAYTEST("LZ4_compress_fast_continue() after LZ4_attach_dictionary()");
                 LZ4_loadDict(&LZ4dictBody, dict, dictSize);
    @@ -756,7 +795,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c
                  * test.
                  */
                 FUZ_CHECKTEST(blockContinueCompressedSize != expectedSize, "LZ4_compress_fast_continue using extDictCtx produced different-sized output (%d expected vs %d actual)", expectedSize, blockContinueCompressedSize);
    -            FUZ_CHECKTEST(XXH32(compressedBuffer, blockContinueCompressedSize, 0) != expectedCrc, "LZ4_compress_fast_continue using extDictCtx produced different output");
    +            FUZ_CHECKTEST(XXH32(compressedBuffer, (size_t)blockContinueCompressedSize, 0) != expectedCrc, "LZ4_compress_fast_continue using extDictCtx produced different output");
     
                 FUZ_DISPLAYTEST("LZ4_compress_fast_continue() after LZ4_attach_dictionary(), but output buffer is 1 byte too short");
                 LZ4_resetStream_fast(&LZ4_stream);
    @@ -772,7 +811,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c
                 FUZ_CHECKTEST(ret!=blockContinueCompressedSize, "LZ4_compress_limitedOutput_compressed size is different (%i != %i)", ret, blockContinueCompressedSize);
                 FUZ_CHECKTEST(ret<=0, "LZ4_compress_fast_continue using extDictCtx should work : enough size available within output buffer");
                 FUZ_CHECKTEST(ret != expectedSize, "LZ4_compress_fast_continue using extDictCtx produced different-sized output");
    -            FUZ_CHECKTEST(XXH32(compressedBuffer, ret, 0) != expectedCrc, "LZ4_compress_fast_continue using extDictCtx produced different output");
    +            FUZ_CHECKTEST(XXH32(compressedBuffer, (size_t)ret, 0) != expectedCrc, "LZ4_compress_fast_continue using extDictCtx produced different output");
                 FUZ_CHECKTEST(LZ4_stream.internal_donotuse.dirty, "context should be good");
     
                 FUZ_DISPLAYTEST();
    @@ -782,7 +821,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c
                 FUZ_CHECKTEST(ret!=blockContinueCompressedSize, "LZ4_compress_limitedOutput_compressed size is different (%i != %i)", ret, blockContinueCompressedSize);
                 FUZ_CHECKTEST(ret<=0, "LZ4_compress_fast_continue using extDictCtx with re-used context should work : enough size available within output buffer");
                 FUZ_CHECKTEST(ret != expectedSize, "LZ4_compress_fast_continue using extDictCtx produced different-sized output");
    -            FUZ_CHECKTEST(XXH32(compressedBuffer, ret, 0) != expectedCrc, "LZ4_compress_fast_continue using extDictCtx produced different output");
    +            FUZ_CHECKTEST(XXH32(compressedBuffer, (size_t)ret, 0) != expectedCrc, "LZ4_compress_fast_continue using extDictCtx produced different output");
                 FUZ_CHECKTEST(LZ4_stream.internal_donotuse.dirty, "context should be good");
             }
     
    -- 
    cgit v1.2.3
    
    
    From 567b4e098baad783870bf6514a984c4b021d5056 Mon Sep 17 00:00:00 2001
    From: Yann Collet 
    Date: Fri, 28 Jun 2019 20:23:12 -0700
    Subject: moved noisy-src decoder test into cBuffer_exact
    
    so that it can also catch any potential read out-of-bound in the input buffer
    (none reported so far, just a precaution for the future).
    ---
     tests/fuzzer.c | 68 +++++++++++++++++++++++++++++-----------------------------
     1 file changed, 34 insertions(+), 34 deletions(-)
    
    diff --git a/tests/fuzzer.c b/tests/fuzzer.c
    index db26b72..368b28b 100644
    --- a/tests/fuzzer.c
    +++ b/tests/fuzzer.c
    @@ -563,36 +563,8 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c
                     FUZ_CHECKTEST(decodedBuffer[blockSize-10], "LZ4_decompress_safe overrun specified output buffer size");
                 }
     
    -            free(cBuffer_exact);
    -        }
    -
    -        /* Test decoding with input size being one byte too short => must fail */
    -        FUZ_DISPLAYTEST();
    -        {   int const r = LZ4_decompress_safe(compressedBuffer, decodedBuffer, compressedSize-1, blockSize);
    -            FUZ_CHECKTEST(r>=0, "LZ4_decompress_safe should have failed, due to input size being one byte too short (blockSize=%i, result=%i, compressedSize=%i)", blockSize, r, compressedSize);
    -        }
    -
    -        /* Test decoding with input size being one byte too large => must fail */
    -        FUZ_DISPLAYTEST();
    -        decodedBuffer[blockSize] = 0;
    -        {   int const r = LZ4_decompress_safe(compressedBuffer, decodedBuffer, compressedSize+1, blockSize);
    -            FUZ_CHECKTEST(r>=0, "LZ4_decompress_safe should have failed, due to input size being too large");
    -        }
    -        FUZ_CHECKTEST(decodedBuffer[blockSize], "LZ4_decompress_safe overrun specified output buffer size");
    -
    -        /* Test partial decoding => must work */
    -        FUZ_DISPLAYTEST("test LZ4_decompress_safe_partial");
    -        {   size_t const missingBytes = FUZ_rand(&randState) % (unsigned)blockSize;
    -            int const targetSize = (int)((size_t)blockSize - missingBytes);
    -            char const sentinel = decodedBuffer[targetSize] = block[targetSize] ^ 0x5A;
    -            int const decResult = LZ4_decompress_safe_partial(compressedBuffer, decodedBuffer, compressedSize, targetSize, blockSize);
    -            FUZ_CHECKTEST(decResult<0, "LZ4_decompress_safe_partial failed despite valid input data (error:%i)", decResult);
    -            FUZ_CHECKTEST(decResult != targetSize, "LZ4_decompress_safe_partial did not regenerated required amount of data (%i < %i <= %i)", decResult, targetSize, blockSize);
    -            FUZ_CHECKTEST(decodedBuffer[targetSize] != sentinel, "LZ4_decompress_safe_partial overwrite beyond requested size (though %i <= %i <= %i)", decResult, targetSize, blockSize);
    -        }
    +            /* noisy src decompression test */
     
    -        /* noisy src decompression test */
    -        {
                 /* insert noise into src */
                 {   U32 const maxNbBits = FUZ_highbit32((U32)compressedSize);
                     size_t pos = 0;
    @@ -611,21 +583,49 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c
                             size_t const rNoiseLength = (FUZ_rand(&randState) & mask) + 1;
                             size_t const noiseLength = MIN(rNoiseLength, (size_t)compressedSize-pos);
                             size_t const noiseStart = FUZ_rand(&randState) % (COMPRESSIBLE_NOISE_LENGTH - noiseLength);
    -                        memcpy(compressedBuffer + pos, (const char*)CNBuffer + noiseStart, noiseLength);
    +                        memcpy(cBuffer_exact + pos, (const char*)CNBuffer + noiseStart, noiseLength);
                             pos += noiseLength;
                 }   }   }
     
                 /* decompress noisy source */
    -            FUZ_DISPLAYTEST("decompress noisy source \n");
    +            FUZ_DISPLAYTEST("decompress noisy source ");
                 {   U32 const endMark = 0xA9B1C3D6;
                     memcpy(decodedBuffer+blockSize, &endMark, sizeof(endMark));
    -                {   int const decompressResult = LZ4_decompress_safe(compressedBuffer, decodedBuffer, compressedSize, blockSize);
    +                {   int const decompressResult = LZ4_decompress_safe(cBuffer_exact, decodedBuffer, compressedSize, blockSize);
                         /* result *may* be an unlikely success, but even then, it must strictly respect dst buffer boundaries */
                         FUZ_CHECKTEST(decompressResult > blockSize, "LZ4_decompress_safe on noisy src : result is too large : %u > %u (dst buffer)", (unsigned)decompressResult, (unsigned)blockSize);
                     }
                     {   U32 endCheck; memcpy(&endCheck, decodedBuffer+blockSize, sizeof(endCheck));
                         FUZ_CHECKTEST(endMark!=endCheck, "LZ4_decompress_safe on noisy src : dst buffer overflow");
    -        }   }   }   /* noisy src decompression test */
    +            }   }   /* noisy src decompression test */
    +
    +            free(cBuffer_exact);
    +        }
    +
    +        /* Test decoding with input size being one byte too short => must fail */
    +        FUZ_DISPLAYTEST();
    +        {   int const r = LZ4_decompress_safe(compressedBuffer, decodedBuffer, compressedSize-1, blockSize);
    +            FUZ_CHECKTEST(r>=0, "LZ4_decompress_safe should have failed, due to input size being one byte too short (blockSize=%i, result=%i, compressedSize=%i)", blockSize, r, compressedSize);
    +        }
    +
    +        /* Test decoding with input size being one byte too large => must fail */
    +        FUZ_DISPLAYTEST();
    +        decodedBuffer[blockSize] = 0;
    +        {   int const r = LZ4_decompress_safe(compressedBuffer, decodedBuffer, compressedSize+1, blockSize);
    +            FUZ_CHECKTEST(r>=0, "LZ4_decompress_safe should have failed, due to input size being too large");
    +        }
    +        FUZ_CHECKTEST(decodedBuffer[blockSize], "LZ4_decompress_safe overrun specified output buffer size");
    +
    +        /* Test partial decoding => must work */
    +        FUZ_DISPLAYTEST("test LZ4_decompress_safe_partial");
    +        {   size_t const missingBytes = FUZ_rand(&randState) % (unsigned)blockSize;
    +            int const targetSize = (int)((size_t)blockSize - missingBytes);
    +            char const sentinel = decodedBuffer[targetSize] = block[targetSize] ^ 0x5A;
    +            int const decResult = LZ4_decompress_safe_partial(compressedBuffer, decodedBuffer, compressedSize, targetSize, blockSize);
    +            FUZ_CHECKTEST(decResult<0, "LZ4_decompress_safe_partial failed despite valid input data (error:%i)", decResult);
    +            FUZ_CHECKTEST(decResult != targetSize, "LZ4_decompress_safe_partial did not regenerated required amount of data (%i < %i <= %i)", decResult, targetSize, blockSize);
    +            FUZ_CHECKTEST(decodedBuffer[targetSize] != sentinel, "LZ4_decompress_safe_partial overwrite beyond requested size (though %i <= %i <= %i)", decResult, targetSize, blockSize);
    +        }
     
             /* Test Compression with limited output size */
     
    @@ -713,7 +713,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c
             blockContinueCompressedSize = LZ4_compress_fast_continue(&LZ4dictBody, block, compressedBuffer, blockSize, (int)compressedBufferSize, 1);
             FUZ_CHECKTEST(blockContinueCompressedSize==0, "LZ4_compress_fast_continue failed");
     
    -        FUZ_DISPLAYTEST("test LZ4_compress_fast_continue() with dictionary but with an output buffer too short by one byte");
    +        FUZ_DISPLAYTEST("LZ4_compress_fast_continue() with dictionary and output buffer too short by one byte");
             LZ4_loadDict(&LZ4dictBody, dict, dictSize);
             ret = LZ4_compress_fast_continue(&LZ4dictBody, block, compressedBuffer, blockSize, blockContinueCompressedSize-1, 1);
             FUZ_CHECKTEST(ret>0, "LZ4_compress_fast_continue using ExtDict should fail : one missing byte for output buffer : %i written, %i buffer", ret, blockContinueCompressedSize);
    -- 
    cgit v1.2.3
    
    
    From 3b917ef6e6e9b18b15f18b6d691c4ea5033cfe41 Mon Sep 17 00:00:00 2001
    From: Yann Collet 
    Date: Fri, 28 Jun 2019 20:55:47 -0700
    Subject: travisCI: added ASAN fuzzer tests
    
    and fixed minor formatting warnings
    ---
     .travis.yml                       |  6 ++++++
     examples/HCStreaming_ringBuffer.c |  9 ++++++---
     tests/fullbench.c                 | 16 +++++++++-------
     tests/fuzzer.c                    |  6 +++---
     4 files changed, 24 insertions(+), 13 deletions(-)
    
    diff --git a/.travis.yml b/.travis.yml
    index ee643e5..b88d907 100644
    --- a/.travis.yml
    +++ b/.travis.yml
    @@ -27,6 +27,12 @@ matrix:
           script:
             - make -C tests test-frametest test-fuzzer
     
    +    - name: ASAN tests with fuzzer and frametest
    +      install:
    +        - sudo sysctl -w vm.mmap_min_addr=4096
    +      script:
    +        - CC=clang MOREFLAGS=-fsanitize=address make -C tests test-frametest test-fuzzer
    +
         - name: (Precise) g++ and clang CMake test
           dist: precise
           script:
    diff --git a/examples/HCStreaming_ringBuffer.c b/examples/HCStreaming_ringBuffer.c
    index a878577..bc8391e 100644
    --- a/examples/HCStreaming_ringBuffer.c
    +++ b/examples/HCStreaming_ringBuffer.c
    @@ -26,6 +26,7 @@
     #include 
     #include 
     #include 
    +#include 
     
     enum {
         MESSAGE_MAX_BYTES   = 1024,
    @@ -39,7 +40,8 @@ size_t write_int32(FILE* fp, int32_t i) {
     }
     
     size_t write_bin(FILE* fp, const void* array, int arrayBytes) {
    -    return fwrite(array, 1, arrayBytes, fp);
    +    assert(arrayBytes >= 0);
    +    return fwrite(array, 1, (size_t)arrayBytes, fp);
     }
     
     size_t read_int32(FILE* fp, int32_t* i) {
    @@ -47,7 +49,8 @@ size_t read_int32(FILE* fp, int32_t* i) {
     }
     
     size_t read_bin(FILE* fp, void* array, int arrayBytes) {
    -    return fread(array, 1, arrayBytes, fp);
    +    assert(arrayBytes >= 0);
    +    return fread(array, 1, (size_t)arrayBytes, fp);
     }
     
     
    @@ -174,7 +177,7 @@ int main(int argc, const char** argv)
             return 0;
         }
     
    -    if (!strcmp(argv[1], "-p")) pause = 1, fileID = 2;
    +    if (!strcmp(argv[1], "-p")) { pause = 1; fileID = 2; }
     
         snprintf(inpFilename, 256, "%s", argv[fileID]);
         snprintf(lz4Filename, 256, "%s.lz4s-%d", argv[fileID], 9);
    diff --git a/tests/fullbench.c b/tests/fullbench.c
    index 4609f13..7d74d3f 100644
    --- a/tests/fullbench.c
    +++ b/tests/fullbench.c
    @@ -541,9 +541,10 @@ int fullSpeedBench(const char** fileNamesTable, int nbFiles)
                         if (initFunction!=NULL) initFunction();
                         for (chunkNb=0; chunkNb%9i (%5.2f%%),%7.1f MB/s\r", loopNb, compressorName, (int)benchedSize, (int)cSize, ratio, (double)benchedSize / bestTime / 1000000);
                 }
    @@ -586,9 +587,10 @@ int fullSpeedBench(const char** fileNamesTable, int nbFiles)
             }
             for (chunkNb=0; chunkNb>= 1, nbBits++;
    +    while (v32) { v32 >>= 1; nbBits++; }
         return nbBits;
     }
     
    @@ -766,8 +766,8 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c
                 if (blockSize > missingBytes) {
                     decodedBuffer[blockSize-missingBytes] = 0;
                     ret = LZ4_decompress_safe_usingDict(compressedBuffer, decodedBuffer, blockContinueCompressedSize, blockSize-missingBytes, dict, dictSize);
    -                FUZ_CHECKTEST(ret>=0, "LZ4_decompress_safe_usingDict should have failed : output buffer too small (-%u byte)", missingBytes);
    -                FUZ_CHECKTEST(decodedBuffer[blockSize-missingBytes], "LZ4_decompress_safe_usingDict overrun specified output buffer size (-%u byte) (blockSize=%i)", missingBytes, blockSize);
    +                FUZ_CHECKTEST(ret>=0, "LZ4_decompress_safe_usingDict should have failed : output buffer too small (-%i byte)", missingBytes);
    +                FUZ_CHECKTEST(decodedBuffer[blockSize-missingBytes], "LZ4_decompress_safe_usingDict overrun specified output buffer size (-%i byte) (blockSize=%i)", missingBytes, blockSize);
             }   }
     
             /* Compress using external dictionary stream */
    -- 
    cgit v1.2.3
    
    
    From 84f98dd316e23fef5850645a0d7fc69ccff4c773 Mon Sep 17 00:00:00 2001
    From: Lzu Tao 
    Date: Sat, 29 Jun 2019 20:54:39 +0700
    Subject: meson: Rename options in travis config
    
    ---
     .travis.yml | 30 ++++++++++++++++++------------
     1 file changed, 18 insertions(+), 12 deletions(-)
    
    diff --git a/.travis.yml b/.travis.yml
    index ee643e5..ca5fff3 100644
    --- a/.travis.yml
    +++ b/.travis.yml
    @@ -178,18 +178,24 @@ matrix:
           compiler: clang
           install:
             - sudo apt-get install -qq python3 tree
    -        - travis_retry curl -o ~/ninja.zip -L 'https://github.com/ninja-build/ninja/releases/download/v1.9.0/ninja-linux.zip'
    -            && unzip ~/ninja.zip -d ~/.local/bin
    -        - travis_retry curl -o ~/get-pip.py 'https://bootstrap.pypa.io/get-pip.py'
    -            && python3 ~/get-pip.py --user
    -            && pip3 install --user meson
    -      script:
    -        - meson setup
    -            --buildtype=debug
    -            -Db_lundef=false
    -            -Dauto_features=enabled
    -            -Ddefault_library=both
    -            -Dbuild_{programs,contrib,tests,examples}=true
    +        - |
    +          travis_retry curl -o ~/ninja.zip -L 'https://github.com/ninja-build/ninja/releases/download/v1.9.0/ninja-linux.zip' &&
    +          unzip ~/ninja.zip -d ~/.local/bin
    +        - |
    +          travis_retry curl -o ~/get-pip.py 'https://bootstrap.pypa.io/get-pip.py' &&
    +          python3 ~/get-pip.py --user &&
    +          pip3 install --user meson
    +      script:
    +        - |
    +          meson setup \
    +            --buildtype=debug \
    +            -Db_lundef=false \
    +            -Dauto_features=enabled \
    +            -Ddefault_library=both \
    +            -Dbin_programs=true \
    +            -Dbin_contrib=true \
    +            -Dbin_tests=true \
    +            -Dbin_examples=true \
                 contrib/meson build
             - pushd build
             - DESTDIR=./staging ninja install
    -- 
    cgit v1.2.3
    
    
    From f3ec519f594ca78ba22001f416db84ea2da41fa1 Mon Sep 17 00:00:00 2001
    From: Max Dymond 
    Date: Sun, 30 Jun 2019 20:16:03 +0100
    Subject: Remove unnecessary call to Makefile.inc
    
    ---
     ossfuzz/Makefile | 2 --
     1 file changed, 2 deletions(-)
    
    diff --git a/ossfuzz/Makefile b/ossfuzz/Makefile
    index 7812c41..bd01123 100644
    --- a/ossfuzz/Makefile
    +++ b/ossfuzz/Makefile
    @@ -35,8 +35,6 @@ LZ4_CFLAGS  = $(CFLAGS) $(DEBUGFLAGS) $(MOREFLAGS)
     LZ4_CXXFLAGS = $(CXXFLAGS) $(DEBUGFLAGS) $(MOREFLAGS)
     LZ4_CPPFLAGS = $(CPPFLAGS) -I$(LZ4DIR) -DXXH_NAMESPACE=LZ4_
     
    -include ../Makefile.inc
    -
     # Include a rule to build the static library if calling this target
     # directly.
     $(LZ4DIR)/liblz4.a:
    -- 
    cgit v1.2.3
    
    
    From 4e87942529323e8db18707689651fa279b13ac82 Mon Sep 17 00:00:00 2001
    From: Yann Collet 
    Date: Sun, 30 Jun 2019 13:59:49 -0700
    Subject: frametest: added LZ4F decoder noise test
    
    ---
     tests/frametest.c | 265 +++++++++++++++++++++++++++++++++++++-----------------
     tests/fuzzer.c    |  12 +--
     2 files changed, 188 insertions(+), 89 deletions(-)
    
    diff --git a/tests/frametest.c b/tests/frametest.c
    index bf95beb..32a023c 100644
    --- a/tests/frametest.c
    +++ b/tests/frametest.c
    @@ -41,7 +41,7 @@
     #include      /* strcmp */
     #include        /* clock_t, clock(), CLOCKS_PER_SEC */
     #include 
    -#include "lz4frame.h"   /* include multiple times to test correctness/safety */
    +#include "lz4frame.h"   /* included multiple times to test correctness/safety */
     #include "lz4frame.h"
     #define LZ4F_STATIC_LINKING_ONLY
     #include "lz4frame.h"
    @@ -150,8 +150,7 @@ static void FUZ_fillCompressibleNoiseBuffer(void* buffer, size_t bufferSize, dou
                 size_t const length = MIN(lengthRand, bufferSize - pos);
                 size_t const end = pos + length;
                 while (pos < end) BBuffer[pos++] = (BYTE)(FUZ_rand(seed) >> 5);
    -        }
    -    }
    +    }   }
     }
     
     
    @@ -703,8 +702,8 @@ int basicTests(U32 seed, double compressibility)
             while (ip < iend) {
                 unsigned nbBits = FUZ_rand(&randState) % maxBits;
                 size_t iSize = (FUZ_rand(&randState) & ((1< (size_t)(iend-ip)) iSize = iend-ip;
    +            size_t oSize = (size_t)(oend-op);
    +            if (iSize > (size_t)(iend-ip)) iSize = (size_t)(iend-ip);
                 CHECK( LZ4F_decompress(dCtx, op, &oSize, ip, &iSize, NULL) );
                 op += oSize;
                 ip += iSize;
    @@ -722,8 +721,8 @@ int basicTests(U32 seed, double compressibility)
             while (ip < iend) {
                 unsigned const nbBits = FUZ_rand(&randState) % maxBits;
                 size_t iSize = (FUZ_rand(&randState) & ((1< (size_t)(iend-ip)) iSize = iend-ip;
    +            size_t oSize = (size_t)(oend-op);
    +            if (iSize > (size_t)(iend-ip)) iSize = (size_t)(iend-ip);
                 CHECK( LZ4F_decompress(dCtx, op, &oSize, ip, &iSize, NULL) );
                 op += oSize;
                 ip += iSize;
    @@ -739,7 +738,7 @@ int basicTests(U32 seed, double compressibility)
             while (ip < iend) {
                 size_t iSize = 10;
                 size_t oSize = 10;
    -            if (iSize > (size_t)(iend-ip)) iSize = iend-ip;
    +            if (iSize > (size_t)(iend-ip)) iSize = (size_t)(iend-ip);
                 CHECK( LZ4F_decompress(dCtx, op, &oSize, ip, &iSize, NULL) );
                 op += oSize;
                 ip += iSize;
    @@ -763,15 +762,17 @@ _output_error:
     }
     
     
    -static void locateBuffDiff(const void* buff1, const void* buff2, size_t size, unsigned nonContiguous)
    +typedef enum { o_contiguous, o_noncontiguous, o_overwrite } o_scenario_e;
    +
    +static void locateBuffDiff(const void* buff1, const void* buff2, size_t size, o_scenario_e o_scenario)
     {
         size_t p=0;
         const BYTE* b1=(const BYTE*)buff1;
         const BYTE* b2=(const BYTE*)buff2;
         DISPLAY("locateBuffDiff: looking for error position \n");
    -    if (nonContiguous) {
    -        DISPLAY("mode %u: non-contiguous output (%u bytes), cannot search \n",
    -                nonContiguous, (unsigned)size);
    +    if (o_scenario != o_contiguous) {
    +        DISPLAY("mode %i: non-contiguous output (%u bytes), cannot search \n",
    +                (int)o_scenario, (unsigned)size);
             return;
         }
         while (p < size && b1[p]==b2[p]) p++;
    @@ -780,38 +781,143 @@ static void locateBuffDiff(const void* buff1, const void* buff2, size_t size, un
         }
     }
     
    +#   define EXIT_MSG(...) { DISPLAY("Error => "); DISPLAY(__VA_ARGS__); \
    +                           DISPLAY(" (seed %u, test nb %u)  \n", seed, testNb); exit(1); }
    +#   undef CHECK
    +#   define CHECK(cond, ...) { if (cond) { EXIT_MSG(__VA_ARGS__); } }
    +
    +
    +size_t test_lz4f_decompression_wBuffers(
    +          const void* cSrc, size_t cSize,
    +                void* dst, size_t dstCapacity, o_scenario_e o_scenario,
    +          const void* srcRef, size_t decompressedSize,
    +                U64 crcOrig,
    +                U32* const randState,
    +                LZ4F_dctx* const dCtx,
    +                U32 seed, U32 testNb)
    +{
    +    const BYTE* ip = (const BYTE*)cSrc;
    +    const BYTE* const iend = ip + cSize;
    +
    +    BYTE* op = (BYTE*)dst;
    +    BYTE* const oend = op + dstCapacity;
    +
    +    unsigned const suggestedBits = FUZ_highbit((U32)cSize);
    +    unsigned const maxBits = MAX(3, suggestedBits);
    +    size_t totalOut = 0;
    +    size_t moreToFlush = 0;
    +    XXH64_state_t xxh64;
    +    XXH64_reset(&xxh64, 1);
    +    assert(ip < iend);
    +    while (ip < iend) {
    +        unsigned const nbBitsI = (FUZ_rand(randState) % (maxBits-1)) + 1;
    +        unsigned const nbBitsO = (FUZ_rand(randState) % (maxBits)) + 1;
    +        size_t const iSizeCand = (FUZ_rand(randState) & ((1< 2x4MB to test large blocks */
    -    void* srcBuffer = NULL;
    -    size_t const compressedBufferSize = LZ4F_compressFrameBound(srcDataLength, NULL) + 4 MB;  /* needs some margin */
    +    size_t const CNBufferLength = 9 MB;  /* needs to be > 2x4MB to test large blocks */
    +    void* CNBuffer = NULL;
    +    size_t const compressedBufferSize = LZ4F_compressFrameBound(CNBufferLength, NULL) + 4 MB;  /* needs some margin */
         void* compressedBuffer = NULL;
         void* decodedBuffer = NULL;
         U32 coreRand = seed;
         LZ4F_decompressionContext_t dCtx = NULL;
    +    LZ4F_decompressionContext_t dCtxNoise = NULL;
         LZ4F_compressionContext_t cCtx = NULL;
         clock_t const startClock = clock();
         clock_t const clockDuration = duration_s * CLOCKS_PER_SEC;
    -#   undef CHECK
    -#   define EXIT_MSG(...) { DISPLAY("Error => "); DISPLAY(__VA_ARGS__); \
    -                           DISPLAY(" (seed %u, test nb %u)  \n", seed, testNb); goto _output_error; }
    -#   define CHECK(cond, ...) { if (cond) { EXIT_MSG(__VA_ARGS__); } }
     
         /* Create buffers */
         {   size_t const creationStatus = LZ4F_createDecompressionContext(&dCtx, LZ4F_VERSION);
             CHECK(LZ4F_isError(creationStatus), "Allocation failed (error %i)", (int)creationStatus); }
    +    {   size_t const creationStatus = LZ4F_createDecompressionContext(&dCtxNoise, LZ4F_VERSION);
    +        CHECK(LZ4F_isError(creationStatus), "Allocation failed (error %i)", (int)creationStatus); }
         {   size_t const creationStatus = LZ4F_createCompressionContext(&cCtx, LZ4F_VERSION);
             CHECK(LZ4F_isError(creationStatus), "Allocation failed (error %i)", (int)creationStatus); }
    -    srcBuffer = malloc(srcDataLength);
    -    CHECK(srcBuffer==NULL, "srcBuffer Allocation failed");
    +    CNBuffer = malloc(CNBufferLength);
    +    CHECK(CNBuffer==NULL, "CNBuffer Allocation failed");
         compressedBuffer = malloc(compressedBufferSize);
         CHECK(compressedBuffer==NULL, "compressedBuffer Allocation failed");
    -    decodedBuffer = calloc(1, srcDataLength);   /* calloc avoids decodedBuffer being considered "garbage" by scan-build */
    +    decodedBuffer = calloc(1, CNBufferLength);   /* calloc avoids decodedBuffer being considered "garbage" by scan-build */
         CHECK(decodedBuffer==NULL, "decodedBuffer Allocation failed");
    -    FUZ_fillCompressibleNoiseBuffer(srcBuffer, srcDataLength, compressibility, &coreRand);
    +    FUZ_fillCompressibleNoiseBuffer(CNBuffer, CNBufferLength, compressibility, &coreRand);
     
         /* jump to requested testNb */
         for (testNb =0; (testNb < startTest); testNb++) (void)FUZ_rand(&coreRand);   /* sync randomizer */
    @@ -819,10 +925,10 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi
         /* main fuzzer test loop */
         for ( ; (testNb < nbTests) || (clockDuration > FUZ_GetClockSpan(startClock)) ; testNb++) {
             U32 randState = coreRand ^ prime1;
    -        unsigned const srcBits = (FUZ_rand(&randState) % (FUZ_highbit((U32)(srcDataLength-1)) - 1)) + 1;
    +        unsigned const srcBits = (FUZ_rand(&randState) % (FUZ_highbit((U32)(CNBufferLength-1)) - 1)) + 1;
             size_t const srcSize = (FUZ_rand(&randState) & ((1< %s)",
    +                                        (int)result, LZ4F_getErrorName(result));
             }
    -    }
    +
    +#if 1
    +        /* insert noise into src */
    +        {   U32 const maxNbBits = FUZ_highbit((U32)cSize);
    +            size_t pos = 0;
    +            for (;;) {
    +                /* keep some original src */
    +                {   U32 const nbBits = FUZ_rand(&randState) % maxNbBits;
    +                    size_t const mask = (1<= cSize) break;
    +                /* add noise */
    +                {   U32 const nbBitsCodes = FUZ_rand(&randState) % maxNbBits;
    +                    U32 const nbBits = nbBitsCodes ? nbBitsCodes-1 : 0;
    +                    size_t const mask = (1<='0') && (*argument<='9')) {
                             nbTests *= 10;
    -                        nbTests += *argument - '0';
    +                        nbTests += (unsigned)(*argument - '0');
                             argument++;
                         }
                         break;
    @@ -1071,7 +1170,7 @@ int main(int argc, const char** argv)
                                 case '6':
                                 case '7':
                                 case '8':
    -                            case '9': duration *= 10; duration += *argument++ - '0'; continue;
    +                            case '9': duration *= 10; duration += (U32)(*argument++ - '0'); continue;
                             }
                             break;
                         }
    @@ -1083,7 +1182,7 @@ int main(int argc, const char** argv)
                         seedset=1;
                         while ((*argument>='0') && (*argument<='9')) {
                             seed *= 10;
    -                        seed += *argument - '0';
    +                        seed += (U32)(*argument - '0');
                             argument++;
                         }
                         break;
    @@ -1092,7 +1191,7 @@ int main(int argc, const char** argv)
                         testNb=0;
                         while ((*argument>='0') && (*argument<='9')) {
                             testNb *= 10;
    -                        testNb += *argument - '0';
    +                        testNb += (unsigned)(*argument - '0');
                             argument++;
                         }
                         break;
    diff --git a/tests/fuzzer.c b/tests/fuzzer.c
    index 7b6e929..74bc5a0 100644
    --- a/tests/fuzzer.c
    +++ b/tests/fuzzer.c
    @@ -1528,8 +1528,8 @@ int main(int argc, const char** argv)
         U32 seed = 0;
         int seedset = 0;
         int argNb;
    -    int nbTests = NB_ATTEMPTS;
    -    int testNb = 0;
    +    unsigned nbTests = NB_ATTEMPTS;
    +    unsigned testNb = 0;
         int proba = FUZ_COMPRESSIBILITY_DEFAULT;
         int use_pause = 0;
         const char* programName = argv[0];
    @@ -1567,7 +1567,7 @@ int main(int argc, const char** argv)
                         nbTests = 0; duration = 0;
                         while ((*argument>='0') && (*argument<='9')) {
                             nbTests *= 10;
    -                        nbTests += *argument - '0';
    +                        nbTests += (unsigned)(*argument - '0');
                             argument++;
                         }
                         break;
    @@ -1590,7 +1590,7 @@ int main(int argc, const char** argv)
                                 case '6':
                                 case '7':
                                 case '8':
    -                            case '9': duration *= 10; duration += *argument++ - '0'; continue;
    +                            case '9': duration *= 10; duration += (U32)(*argument++ - '0'); continue;
                             }
                             break;
                         }
    @@ -1601,7 +1601,7 @@ int main(int argc, const char** argv)
                         seed=0; seedset=1;
                         while ((*argument>='0') && (*argument<='9')) {
                             seed *= 10;
    -                        seed += *argument - '0';
    +                        seed += (U32)(*argument - '0');
                             argument++;
                         }
                         break;
    @@ -1611,7 +1611,7 @@ int main(int argc, const char** argv)
                         testNb=0;
                         while ((*argument>='0') && (*argument<='9')) {
                             testNb *= 10;
    -                        testNb += *argument - '0';
    +                        testNb += (unsigned)(*argument - '0');
                             argument++;
                         }
                         break;
    -- 
    cgit v1.2.3
    
    
    From 89e96e55ffab100f9893f2b6c12b4b1e26def17e Mon Sep 17 00:00:00 2001
    From: Yann Collet 
    Date: Sun, 30 Jun 2019 14:57:12 -0700
    Subject: updated frametest
    
    so that noisy src decompression
    doesn't generate output
    nor fails when decompression fails (which is expected).
    ---
     tests/frametest.c | 43 +++++++++++++++++++++++++++----------------
     1 file changed, 27 insertions(+), 16 deletions(-)
    
    diff --git a/tests/frametest.c b/tests/frametest.c
    index 32a023c..6d690f2 100644
    --- a/tests/frametest.c
    +++ b/tests/frametest.c
    @@ -766,18 +766,20 @@ typedef enum { o_contiguous, o_noncontiguous, o_overwrite } o_scenario_e;
     
     static void locateBuffDiff(const void* buff1, const void* buff2, size_t size, o_scenario_e o_scenario)
     {
    -    size_t p=0;
    -    const BYTE* b1=(const BYTE*)buff1;
    -    const BYTE* b2=(const BYTE*)buff2;
    -    DISPLAY("locateBuffDiff: looking for error position \n");
    -    if (o_scenario != o_contiguous) {
    -        DISPLAY("mode %i: non-contiguous output (%u bytes), cannot search \n",
    -                (int)o_scenario, (unsigned)size);
    -        return;
    -    }
    -    while (p < size && b1[p]==b2[p]) p++;
    -    if (p != size) {
    -        DISPLAY("Error at pos %i/%i : %02X != %02X \n", (int)p, (int)size, b1[p], b2[p]);
    +    if (displayLevel >= 5) {
    +        size_t p=0;
    +        const BYTE* b1=(const BYTE*)buff1;
    +        const BYTE* b2=(const BYTE*)buff2;
    +        DISPLAY("locateBuffDiff: looking for error position \n");
    +        if (o_scenario != o_contiguous) {
    +            DISPLAY("mode %i: non-contiguous output (%u bytes), cannot search \n",
    +                    (int)o_scenario, (unsigned)size);
    +            return;
    +        }
    +        while (p < size && b1[p]==b2[p]) p++;
    +        if (p != size) {
    +            DISPLAY("Error at pos %i/%i : %02X != %02X \n", (int)p, (int)size, b1[p], b2[p]);
    +        }
         }
     }
     
    @@ -825,6 +827,8 @@ size_t test_lz4f_decompression_wBuffers(
             if (o_scenario == o_overwrite) dOptions.stableDst = 0;  /* overwrite mode */
             if (op + oSizeMax < oend) op[oSizeMax] = mark;
     
    +        DISPLAYLEVEL(7, "dstCapacity=%u,  presentedInput=%u \n", (unsigned)oSize, (unsigned)iSize);
    +
             /* read data from byte-exact buffer to catch out-of-bound reads */
             {   void* const iBuffer = malloc(iSizeMax);
                 assert(iBuffer != NULL);
    @@ -832,6 +836,7 @@ size_t test_lz4f_decompression_wBuffers(
                 moreToFlush = LZ4F_decompress(dCtx, op, &oSize, iBuffer, &iSize, &dOptions);
                 free(iBuffer);
             }
    +        DISPLAYLEVEL(7, "oSize=%u,  readSize=%u \n", (unsigned)oSize, (unsigned)iSize);
     
             if (op + oSizeMax < oend) {
                 CHECK(op[oSizeMax] != mark, "op[oSizeMax] = %02X != %02X : "
    @@ -846,8 +851,14 @@ size_t test_lz4f_decompression_wBuffers(
             totalOut += oSize;
             op += oSize;
             ip += iSize;
    -        op += (o_scenario == o_noncontiguous);  /* create a gap between consecutive output */
    +        if (o_scenario == o_noncontiguous) {
    +            if (op == oend) return LZ4F_ERROR_GENERIC;  /* can theoretically happen with bogus data */
    +            op++; /* create a gap between consecutive output */
    +        }
             if (o_scenario==o_overwrite) op = (BYTE*)dst;   /* overwrite destination */
    +        if ( (op == oend) /* no more room for output; can happen with bogus input */
    +          && (iSize == 0)) /* no input consumed */
    +            break;
         }
         if (moreToFlush != 0) return LZ4F_ERROR_decompressionFailed;
         if (totalOut) {  /* otherwise, it's a skippable frame */
    @@ -870,7 +881,7 @@ size_t test_lz4f_decompression(const void* cSrc, size_t cSize,
         o_scenario_e const o_scenario = (o_scenario_e)(FUZ_rand(randState) % 3);   /* 0 : contiguous; 1 : non-contiguous; 2 : dst overwritten */
         /* tighten dst buffer conditions */
         size_t const dstCapacity = (o_scenario == o_noncontiguous) ?
    -                               (decompressedSize * 2) + 64 :
    +                               (decompressedSize * 2) + 128 :
                                    decompressedSize;
         size_t result;
         void* const dstBuffer = malloc(dstCapacity);
    @@ -1023,7 +1034,7 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi
     
     
             /* multi-segments decompression */
    -        printf("normal decompression \n");
    +        DISPLAYLEVEL(6, "normal decompression \n");
             {   size_t result = test_lz4f_decompression(compressedBuffer, cSize, srcStart, srcSize, crcOrig, &randState, dCtx, seed, testNb);
                 CHECK (LZ4F_isError(result), "multi-segment decompression failed (error %i => %s)",
                                             (int)result, LZ4F_getErrorName(result));
    @@ -1053,7 +1064,7 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi
             }   }   }
     
             /* test decompression on noisy src */
    -        printf("noisy decompression \n");
    +        DISPLAYLEVEL(6, "noisy decompression \n");
             test_lz4f_decompression(compressedBuffer, cSize, srcStart, srcSize, crcOrig, &randState, dCtxNoise, seed, testNb);
             /* note : we don't analyze result here : it probably failed, which is expected.
              * We just check for potential out-of-bound reads and writes. */
    -- 
    cgit v1.2.3
    
    
    From 2cacdd21423010406dbafaad710dfa9c567f77c1 Mon Sep 17 00:00:00 2001
    From: Yann Collet 
    Date: Sun, 30 Jun 2019 15:36:32 -0700
    Subject: fix minor cppcheck warnings
    
    ---
     tests/frametest.c | 2 +-
     tests/fuzzer.c    | 4 ++--
     2 files changed, 3 insertions(+), 3 deletions(-)
    
    diff --git a/tests/frametest.c b/tests/frametest.c
    index 6d690f2..69dd5aa 100644
    --- a/tests/frametest.c
    +++ b/tests/frametest.c
    @@ -1236,7 +1236,7 @@ int main(int argc, const char** argv)
         DISPLAY("Seed = %u\n", seed);
         if (proba!=FUZ_COMPRESSIBILITY_DEFAULT) DISPLAY("Compressibility : %i%%\n", proba);
     
    -    if (nbTests<=0) nbTests=1;
    +    nbTests += (nbTests==0);  /* avoid zero */
     
         if (testNb==0) result = basicTests(seed, ((double)proba) / 100);
         if (result) return 1;
    diff --git a/tests/fuzzer.c b/tests/fuzzer.c
    index 74bc5a0..8a095c4 100644
    --- a/tests/fuzzer.c
    +++ b/tests/fuzzer.c
    @@ -861,7 +861,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c
             FUZ_DISPLAYTEST("LZ4_decompress_safe_usingDict with a too small output buffer");
             {   U32 const missingBytes = (FUZ_rand(&randState) & 0xF) + 2;
                 if ((U32)blockSize > missingBytes) {
    -                decodedBuffer[blockSize-missingBytes] = 0;
    +                decodedBuffer[(U32)blockSize-missingBytes] = 0;
                     ret = LZ4_decompress_safe_usingDict(compressedBuffer, decodedBuffer, blockContinueCompressedSize, blockSize-missingBytes, dict, dictSize);
                     FUZ_CHECKTEST(ret>=0, "LZ4_decompress_safe_usingDict should have failed : output buffer too small (-%u byte)", missingBytes);
                     FUZ_CHECKTEST(decodedBuffer[blockSize-missingBytes], "LZ4_decompress_safe_usingDict overrun specified output buffer size (-%u byte) (blockSize=%i)", missingBytes, blockSize);
    @@ -1646,7 +1646,7 @@ int main(int argc, const char** argv)
     
         if ((seedset==0) && (testNb==0)) { FUZ_unitTests(LZ4HC_CLEVEL_DEFAULT); FUZ_unitTests(LZ4HC_CLEVEL_OPT_MIN); }
     
    -    if (nbTests<=0) nbTests=1;
    +    nbTests += (nbTests==0);  /* avoid zero */
     
         {   int const result = FUZ_test(seed, nbTests, testNb, ((double)proba) / 100, duration);
             if (use_pause) {
    -- 
    cgit v1.2.3
    
    
    From bb5c34a875a892077e60582ac27898489bfd9d14 Mon Sep 17 00:00:00 2001
    From: Yann Collet 
    Date: Mon, 1 Jul 2019 09:01:43 -0700
    Subject: bumped version number to v1.9.2
    
    to reduce risks that future bug reports in `dev` branch report `v1.9.1` as the failing version.
    ---
     doc/lz4_manual.html      |  4 ++--
     doc/lz4frame_manual.html |  4 ++--
     lib/lz4.h                |  2 +-
     programs/lz4.1           |  2 +-
     programs/lz4io.c         | 16 ++++++++--------
     5 files changed, 14 insertions(+), 14 deletions(-)
    
    diff --git a/doc/lz4_manual.html b/doc/lz4_manual.html
    index 18daa05..a477584 100644
    --- a/doc/lz4_manual.html
    +++ b/doc/lz4_manual.html
    @@ -1,10 +1,10 @@
     
     
     
    -1.9.1 Manual
    +1.9.2 Manual
     
     
    -

    1.9.1 Manual

    +

    1.9.2 Manual


    Contents

      diff --git a/doc/lz4frame_manual.html b/doc/lz4frame_manual.html index 2cf4f03..72f27c8 100644 --- a/doc/lz4frame_manual.html +++ b/doc/lz4frame_manual.html @@ -1,10 +1,10 @@ -1.9.1 Manual +1.9.2 Manual -

      1.9.1 Manual

      +

      1.9.2 Manual


      Contents

        diff --git a/lib/lz4.h b/lib/lz4.h index ad93d00..32108e2 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -100,7 +100,7 @@ extern "C" { /*------ Version ------*/ #define LZ4_VERSION_MAJOR 1 /* for breaking interface changes */ #define LZ4_VERSION_MINOR 9 /* for new (non-breaking) interface capabilities */ -#define LZ4_VERSION_RELEASE 1 /* for tweaks, bug-fixes, or development */ +#define LZ4_VERSION_RELEASE 2 /* for tweaks, bug-fixes, or development */ #define LZ4_VERSION_NUMBER (LZ4_VERSION_MAJOR *100*100 + LZ4_VERSION_MINOR *100 + LZ4_VERSION_RELEASE) diff --git a/programs/lz4.1 b/programs/lz4.1 index ad0c12c..3ca017a 100644 --- a/programs/lz4.1 +++ b/programs/lz4.1 @@ -1,5 +1,5 @@ . -.TH "LZ4" "1" "April 2019" "lz4 1.9.1" "User Commands" +.TH "LZ4" "1" "July 2019" "lz4 1.9.2" "User Commands" . .SH "NAME" \fBlz4\fR \- lz4, unlz4, lz4cat \- Compress or decompress \.lz4 files diff --git a/programs/lz4io.c b/programs/lz4io.c index d03aa6d..d818535 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -219,8 +219,8 @@ size_t LZ4IO_setBlockSizeID(LZ4IO_prefs_t* const prefs, unsigned bsid) static const unsigned minBlockSizeID = 4; static const unsigned maxBlockSizeID = 7; if ((bsid < minBlockSizeID) || (bsid > maxBlockSizeID)) return 0; - prefs->blockSizeId = bsid; - prefs->blockSize = blockSizeTable[prefs->blockSizeId-minBlockSizeID]; + prefs->blockSizeId = (int)bsid; + prefs->blockSize = blockSizeTable[(unsigned)prefs->blockSizeId-minBlockSizeID]; return prefs->blockSize; } @@ -237,7 +237,7 @@ size_t LZ4IO_setBlockSize(LZ4IO_prefs_t* const prefs, size_t blockSize) while (blockSize >>= 2) bsid++; if (bsid < 7) bsid = 7; - prefs->blockSizeId = bsid-3; + prefs->blockSizeId = (int)(bsid-3); return prefs->blockSize; } @@ -417,7 +417,7 @@ int LZ4IO_compressFilename_Legacy(LZ4IO_prefs_t* const prefs, const char* input_ /* Allocate Memory */ in_buff = (char*)malloc(LEGACY_BLOCKSIZE); - out_buff = (char*)malloc(outBuffSize + 4); + out_buff = (char*)malloc((size_t)outBuffSize + 4); if (!in_buff || !out_buff) EXM_THROW(21, "Allocation error : not enough memory"); @@ -898,7 +898,7 @@ static unsigned long long LZ4IO_decodeLegacyStream(LZ4IO_prefs_t* const prefs, F unsigned storedSkips = 0; /* Allocate Memory */ - char* const in_buff = (char*)malloc(LZ4_compressBound(LEGACY_BLOCKSIZE)); + char* const in_buff = (char*)malloc((size_t)LZ4_compressBound(LEGACY_BLOCKSIZE)); char* const out_buff = (char*)malloc(LEGACY_BLOCKSIZE); if (!in_buff || !out_buff) EXM_THROW(51, "Allocation error : not enough memory"); @@ -922,11 +922,11 @@ static unsigned long long LZ4IO_decodeLegacyStream(LZ4IO_prefs_t* const prefs, F if (sizeCheck!=blockSize) EXM_THROW(52, "Read error : cannot access compressed block !"); } /* Decode Block */ - { int const decodeSize = LZ4_decompress_safe(in_buff, out_buff, blockSize, LEGACY_BLOCKSIZE); + { int const decodeSize = LZ4_decompress_safe(in_buff, out_buff, (int)blockSize, LEGACY_BLOCKSIZE); if (decodeSize < 0) EXM_THROW(53, "Decoding Failed ! Corrupted input detected !"); - streamSize += decodeSize; + streamSize += (unsigned long long)decodeSize; /* Write Block */ - storedSkips = LZ4IO_fwriteSparse(prefs, foutput, out_buff, decodeSize, storedSkips); /* success or die */ + storedSkips = LZ4IO_fwriteSparse(prefs, foutput, out_buff, (size_t)decodeSize, storedSkips); /* success or die */ } } if (ferror(finput)) EXM_THROW(54, "Read error : ferror"); -- cgit v1.2.3 From 9ea2835eaa2da7415e3c97a010585e12705d6220 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 1 Jul 2019 10:55:54 -0700 Subject: CircleCI : reduced test duration Ideally, we want to make CircleCI a "fast" test environment, with short feedback loop (~5mn). We are still far from this goal. This patch starts this process by removing "long" tasks which are non-essential and redundant with travisCI. It also acknowledges that parallelism is broken. The script should be more heavily updated to support parallelism, which might be important to support its goal. --- .circleci/config.yml | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 3abcbc1..7f03d1a 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -10,7 +10,10 @@ version: 2 jobs: build: working_directory: ~/lz4/lz4 - parallelism: 2 + # Parallelism is broken in this file : it just plays the same tests twice. + # The script will have to be modified to support parallelism properly + # In the meantime, set it to 1. + parallelism: 1 shell: /bin/bash --login # CircleCI 2.0 does not support environment variables that refer to each other the same way as 1.0 did. # If any of these refer to each other, rewrite them so that they don't or see https://circleci.com/docs/2.0/env-vars/#interpolating-environment-variables-to-set-other-environment-variables . @@ -42,25 +45,20 @@ jobs: # This is based on your 1.0 configuration file or project settings - run: CFLAGS= make clangtest && make clean - run: g++ -v; make gpptest && make clean - - run: gcc -v; make c_standards && make clean - run: gcc -v; g++ -v; make ctocpptest && make clean - run: gcc-5 -v; CC=gcc-5 CFLAGS="-O2 -Werror" make check && make clean - run: gcc-5 -v; CC=gcc-5 CFLAGS="-O2 -m32 -Werror" CPPFLAGS=-I/usr/include/x86_64-linux-gnu make check && make clean - - run: gcc-6 -v; CC=gcc-6 make c_standards && make clean - run: gcc-6 -v; CC=gcc-6 MOREFLAGS="-O2 -Werror" make check && make clean - run: make cmake && make clean - run: make -C tests test-lz4 - run: make -C tests test-lz4c - run: make -C tests test-frametest - - run: make -C tests test-fullbench - run: make -C tests test-fuzzer && make clean - run: make -C lib all && make clean - run: pyenv global 3.4.4; make versionsTest MOREFLAGS=-I/usr/include/x86_64-linux-gnu && make clean - run: make travis-install && make clean - run: gcc -v; CFLAGS="-O2 -m32 -Werror" CPPFLAGS=-I/usr/include/x86_64-linux-gnu make check && make clean - - run: make usan && make clean - run: clang -v; make staticAnalyze && make clean - - run: make -C tests test-mem && make clean - run: make platformTest CC=powerpc-linux-gnu-gcc QEMU_SYS=qemu-ppc-static && make clean - run: make platformTest CC=powerpc-linux-gnu-gcc QEMU_SYS=qemu-ppc64-static MOREFLAGS=-m64 && make clean - run: make platformTest CC=arm-linux-gnueabi-gcc QEMU_SYS=qemu-arm-static && make clean -- cgit v1.2.3 From 0e6ff83d091e151ad510845bb3b4ebb42314f6a3 Mon Sep 17 00:00:00 2001 From: Amine Choukir Date: Wed, 3 Jul 2019 11:50:38 +0200 Subject: Update blockStreaming_doubleBuffer.md --- examples/blockStreaming_doubleBuffer.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/blockStreaming_doubleBuffer.md b/examples/blockStreaming_doubleBuffer.md index 3027ea3..38dc2e8 100644 --- a/examples/blockStreaming_doubleBuffer.md +++ b/examples/blockStreaming_doubleBuffer.md @@ -1,7 +1,7 @@ # LZ4 Streaming API Example : Double Buffer by *Takayuki Matsuoka* -`blockStreaming_doubleBuffer.c` is LZ4 Straming API example which implements double buffer (de)compression. +`blockStreaming_doubleBuffer.c` is LZ4 Streaming API example which implements double buffer (de)compression. Please note : @@ -76,10 +76,10 @@ so it just compress the line without dependencies and generates compressed block After that, write {Out#1} to the file. Next, read second block to double buffer's second page. And compress it. -In this time, LZ4 can use dependency to Block#1 to improve compression ratio. +This time, LZ4 can use dependency to Block#1 to improve compression ratio. This dependency is called "Prefix mode". -Next, read third block to double buffer's *first* page. And compress it. +Next, read third block to double buffer's *first* page, and compress it. Also this time, LZ4 can use dependency to Block#2. This dependency is called "External Dictonaly mode". -- cgit v1.2.3 From 12e5841e762c69e9daa79d6f6d6a5f4302492479 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Thu, 4 Jul 2019 18:13:36 +0200 Subject: Remove an useless declaration --- lib/lz4.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/lz4.c b/lib/lz4.c index d121e29..38ad6ab 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1149,7 +1149,7 @@ int LZ4_compress_fast_extState(void* state, const char* source, char* dest, int return LZ4_compress_generic(ctx, source, dest, inputSize, NULL, 0, notLimited, tableType, noDict, noDictIssue, acceleration); } } else { - if (inputSize < LZ4_64Klimit) {; + if (inputSize < LZ4_64Klimit) { return LZ4_compress_generic(ctx, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, byU16, noDict, noDictIssue, acceleration); } else { const tableType_t tableType = ((sizeof(void*)==4) && ((uptrval)source > LZ4_DISTANCE_MAX)) ? byPtr : byU32; -- cgit v1.2.3 From 658ab8fca1c7bbc191d63c17ee751344a591f3ce Mon Sep 17 00:00:00 2001 From: Hamid Zare Date: Thu, 11 Jul 2019 14:34:52 -0700 Subject: changed the input text to something more compression friendly --- examples/simple_buffer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/simple_buffer.c b/examples/simple_buffer.c index 027fc85..62409d0 100644 --- a/examples/simple_buffer.c +++ b/examples/simple_buffer.c @@ -38,7 +38,7 @@ int main(void) { /* Compression */ // We'll store some text into a variable pointed to by *src to be compressed later. - const char* const src = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."; + const char* const src = "Lorem ipsum dolor sit amet, hhhhhhhhhhhhhhhhhhhhhhh"; // The compression function needs to know how many bytes exist. Since we're using a string, we can use strlen() + 1 (for \0). const int src_size = (int)(strlen(src) + 1); // LZ4 provides a function that will tell you the maximum size of compressed output based on input data via LZ4_compressBound(). -- cgit v1.2.3 From 771a7192d6aa46e5c136ded366a216af60df3583 Mon Sep 17 00:00:00 2001 From: Hamid Zare Date: Thu, 11 Jul 2019 14:39:29 -0700 Subject: print the compression ratio --- examples/simple_buffer.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/simple_buffer.c b/examples/simple_buffer.c index 62409d0..ea57022 100644 --- a/examples/simple_buffer.c +++ b/examples/simple_buffer.c @@ -55,7 +55,8 @@ int main(void) { if (compressed_data_size <= 0) run_screaming("A 0 or negative result from LZ4_compress_default() indicates a failure trying to compress the data. ", 1); if (compressed_data_size > 0) - printf("We successfully compressed some data!\n"); + printf("We successfully compressed some data! Ratio: %.2f\n", + (float) compressed_data_size/src_size); // Not only does a positive return_value mean success, the value returned == the number of bytes required. // You can use this to realloc() *compress_data to free up memory, if desired. We'll do so just to demonstrate the concept. compressed_data = (char *)realloc(compressed_data, (size_t)compressed_data_size); -- cgit v1.2.3 From f1e8e806e0dd753eb48e40272e620f747c7b723e Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 11 Jul 2019 17:29:16 -0700 Subject: keep the "lorem ipsum" topic of the example string but make it compressible --- examples/simple_buffer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/simple_buffer.c b/examples/simple_buffer.c index ea57022..6afc62a 100644 --- a/examples/simple_buffer.c +++ b/examples/simple_buffer.c @@ -38,7 +38,7 @@ int main(void) { /* Compression */ // We'll store some text into a variable pointed to by *src to be compressed later. - const char* const src = "Lorem ipsum dolor sit amet, hhhhhhhhhhhhhhhhhhhhhhh"; + const char* const src = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor site amat."; // The compression function needs to know how many bytes exist. Since we're using a string, we can use strlen() + 1 (for \0). const int src_size = (int)(strlen(src) + 1); // LZ4 provides a function that will tell you the maximum size of compressed output based on input data via LZ4_compressBound(). -- cgit v1.2.3 From 8ac954aa71edd5c13a0b9f6ab57eaece75efc28d Mon Sep 17 00:00:00 2001 From: Hitatm Date: Mon, 15 Jul 2019 22:53:46 +0800 Subject: bugfix: correctly control the offset < LZ4_DISTANCE_MAX,when change the value of LZ4_DISTANCE_MAX, --- lib/lz4hc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/lz4hc.c b/lib/lz4hc.c index 936f739..46c20bc 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -228,7 +228,7 @@ LZ4HC_InsertAndGetWiderMatch ( const U32 dictLimit = hc4->dictLimit; const BYTE* const lowPrefixPtr = base + dictLimit; const U32 ipIndex = (U32)(ip - base); - const U32 lowestMatchIndex = (hc4->lowLimit + 64 KB > ipIndex) ? hc4->lowLimit : ipIndex - LZ4_DISTANCE_MAX; + const U32 lowestMatchIndex = (hc4->lowLimit + (LZ4_DISTANCE_MAX + 1) > ipIndex) ? hc4->lowLimit : ipIndex - LZ4_DISTANCE_MAX; const BYTE* const dictBase = hc4->dictBase; int const lookBackLength = (int)(ip-iLowLimit); int nbAttempts = maxNbAttempts; -- cgit v1.2.3 From 6654c2cd3bd49dfc6b6bb3447bcc01799ea35ac3 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 15 Jul 2019 12:11:34 -0700 Subject: ensure conformance with custom LZ4_DISTANCE_MAX It's now possible to select a custom LZ4_DISTANCE_MAX at compile time, provided it's <= 65535. However, in some cases (when compressing in byU16 mode), the new distance wasn't respected, as it used to implied that it was necessarily within range. Added a distance check for this case. Also : added a new TravisCI test which ensures that custom LZ4_DISTANCE_MAX compiles correctly and compresses correctly (relying on `assert()` to find outsized offsets). --- .travis.yml | 4 ++++ lib/README.md | 4 ++-- lib/lz4.c | 13 +++++++++---- tests/Makefile | 2 +- tests/frametest.c | 3 ++- 5 files changed, 18 insertions(+), 8 deletions(-) diff --git a/.travis.yml b/.travis.yml index af5cf0f..bd29630 100644 --- a/.travis.yml +++ b/.travis.yml @@ -33,6 +33,10 @@ matrix: script: - CC=clang MOREFLAGS=-fsanitize=address make -C tests test-frametest test-fuzzer + - name: Custom LZ4_DISTANCE_MAX + script: + - MOREFLAGS=-DLZ4_DISTANCE_MAX=8000 make check + - name: (Precise) g++ and clang CMake test dist: precise script: diff --git a/lib/README.md b/lib/README.md index cf1505f..cba2c34 100644 --- a/lib/README.md +++ b/lib/README.md @@ -56,8 +56,8 @@ The following build macro can be selected at compilation time : - `LZ4_DISTANCE_MAX` : control the maximum offset that the compressor will allow. Set to 65535 by default, which is the maximum value supported by lz4 format. Reducing maximum distance will reduce opportunities for LZ4 to find matches, - hence will produce worse the compression ratio. - However, a smaller max distance may allow compatibility with specific decoders using limited memory budget. + hence will produce a worse compression ratio. + However, a smaller max distance can allow compatibility with specific decoders using limited memory budget. This build macro only influences the compressed output of the compressor. - `LZ4_DISABLE_DEPRECATE_WARNINGS` : invoking a deprecated function will make the compiler generate a warning. diff --git a/lib/lz4.c b/lib/lz4.c index 38ad6ab..abbdd97 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -413,7 +413,8 @@ static const int LZ4_minLength = (MFLIMIT+1); #define MB *(1 <<20) #define GB *(1U<<30) -#if (LZ4_DISTANCE_MAX > 65535) /* max supported by LZ4 format */ +#define LZ4_DISTANCE_ABSOLUTE_MAX 65535 +#if (LZ4_DISTANCE_MAX > LZ4_DISTANCE_ABSOLUTE_MAX) /* max supported by LZ4 format */ # error "LZ4_DISTANCE_MAX is too big : must be <= 65535" #endif @@ -915,10 +916,14 @@ LZ4_FORCE_INLINE int LZ4_compress_generic( forwardH = LZ4_hashPosition(forwardIp, tableType); LZ4_putIndexOnHash(current, h, cctx->hashTable, tableType); + DEBUGLOG(7, "candidate at pos=%u (offset=%u \n", matchIndex, current - matchIndex); if ((dictIssue == dictSmall) && (matchIndex < prefixIdxLimit)) { continue; } /* match outside of valid area */ assert(matchIndex < current); - if ((tableType != byU16) && (matchIndex+LZ4_DISTANCE_MAX < current)) { continue; } /* too far */ - if (tableType == byU16) { assert((current - matchIndex) <= LZ4_DISTANCE_MAX); } /* too_far presumed impossible with byU16 */ + if ( ((tableType != byU16) || (LZ4_DISTANCE_MAX < LZ4_DISTANCE_ABSOLUTE_MAX)) + && (matchIndex+LZ4_DISTANCE_MAX < current)) { + continue; + } /* too far */ + assert((current - matchIndex) <= LZ4_DISTANCE_MAX); /* match now expected within distance */ if (LZ4_read32(match) == LZ4_read32(ip)) { if (maybe_extMem) offset = current - matchIndex; @@ -1082,7 +1087,7 @@ _next_match: LZ4_putIndexOnHash(current, h, cctx->hashTable, tableType); assert(matchIndex < current); if ( ((dictIssue==dictSmall) ? (matchIndex >= prefixIdxLimit) : 1) - && ((tableType==byU16) ? 1 : (matchIndex+LZ4_DISTANCE_MAX >= current)) + && (((tableType==byU16) && (LZ4_DISTANCE_MAX == LZ4_DISTANCE_ABSOLUTE_MAX)) ? 1 : (matchIndex+LZ4_DISTANCE_MAX >= current)) && (LZ4_read32(match) == LZ4_read32(ip)) ) { token=op++; *token=0; diff --git a/tests/Makefile b/tests/Makefile index 65713ef..422baba 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -448,7 +448,7 @@ test-fuzzer32: CFLAGS += -m32 test-fuzzer32: test-fuzzer test-frametest: frametest - ./frametest $(FUZZER_TIME) + ./frametest -v $(FUZZER_TIME) test-frametest32: CFLAGS += -m32 test-frametest32: test-frametest diff --git a/tests/frametest.c b/tests/frametest.c index 69dd5aa..91813bb 100644 --- a/tests/frametest.c +++ b/tests/frametest.c @@ -46,6 +46,7 @@ #define LZ4F_STATIC_LINKING_ONLY #include "lz4frame.h" #include "lz4frame.h" +#define LZ4_STATIC_LINKING_ONLY /* LZ4_DISTANCE_MAX */ #include "lz4.h" /* LZ4_VERSION_STRING */ #define XXH_STATIC_LINKING_ONLY #include "xxhash.h" /* XXH64 */ @@ -540,7 +541,7 @@ int basicTests(U32 seed, double compressibility) cdict, NULL) ); DISPLAYLEVEL(3, "compressed %u bytes into %u bytes \n", (unsigned)dictSize, (unsigned)cSizeWithDict); - if (cSizeWithDict >= cSizeNoDict) goto _output_error; /* must be more efficient */ + if ((LZ4_DISTANCE_MAX > dictSize) && (cSizeWithDict >= cSizeNoDict)) goto _output_error; /* must be more efficient */ crcOrig = XXH64(CNBuffer, dictSize, 0); DISPLAYLEVEL(3, "LZ4F_decompress_usingDict : "); -- cgit v1.2.3 From 725cb0aafdf78b550c52618fe5cea1fadd278881 Mon Sep 17 00:00:00 2001 From: Nick Terrell Date: Fri, 12 Jul 2019 18:36:28 -0700 Subject: [lz4] Fix bugs in partial decoding * Partial decoding could read a few bytes beyond the end of the input * Partial decoding returned an error with an empty output buffer --- lib/lz4.c | 49 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 38ad6ab..7a4c6aa 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1642,7 +1642,11 @@ LZ4_decompress_generic( /* Special cases */ assert(lowPrefix <= op); - if ((endOnInput) && (unlikely(outputSize==0))) { return ((srcSize==1) && (*ip==0)) ? 0 : -1; } /* Empty output buffer */ + if ((endOnInput) && (unlikely(outputSize==0))) { + /* Empty output buffer */ + if (partialDecoding) return 0; + return ((srcSize==1) && (*ip==0)) ? 0 : -1; + } if ((!endOnInput) && (unlikely(outputSize==0))) { return (*ip==0 ? 1 : -1); } if ((endOnInput) && unlikely(srcSize==0)) { return -1; } @@ -1850,21 +1854,50 @@ LZ4_decompress_generic( if ( ((endOnInput) && ((cpy>oend-MFLIMIT) || (ip+length>iend-(2+1+LASTLITERALS))) ) || ((!endOnInput) && (cpy>oend-WILDCOPYLENGTH)) ) { + /* We've either hit the input parsing restriction or the output parsing restriction. + * If we've hit the input parsing condition then this must be the last sequence. + * If we've hit the output parsing condition then we are either using partialDecoding + * or we've hit the output parsing condition. + */ if (partialDecoding) { - if (cpy > oend) { cpy = oend; assert(op<=oend); length = (size_t)(oend-op); } /* Partial decoding : stop in the middle of literal segment */ - if ((endOnInput) && (ip+length > iend)) { goto _output_error; } /* Error : read attempt beyond end of input buffer */ + /* Since we are partial decoding we may be in this block because of the output parsing + * restriction, which is not valid since the output buffer is allowed to be undersized. + */ + assert(endOnInput); + /* If we're in this block because of the input parsing condition, then we must be on the + * last sequence (or invalid), so we must check that we exactly consume the input. + */ + if ((ip+length>iend-(2+1+LASTLITERALS)) && (ip+length != iend)) { goto _output_error; } + assert(ip+length <= iend); + /* We are finishing in the middle of a literals segment. + * Break after the copy. + */ + if (cpy > oend) { + cpy = oend; + assert(op<=oend); + length = (size_t)(oend-op); + } + assert(ip+length <= iend); } else { - if ((!endOnInput) && (cpy != oend)) { goto _output_error; } /* Error : block decoding must stop exactly there */ - if ((endOnInput) && ((ip+length != iend) || (cpy > oend))) { goto _output_error; } /* Error : input must be consumed */ + /* We must be on the last sequence because of the parsing limitations so check + * that we exactly regenerate the original size (must be exact when !endOnInput). + */ + if ((!endOnInput) && (cpy != oend)) { goto _output_error; } + /* We must be on the last sequence (or invalid) because of the parsing limitations + * so check that we exactly consume the input and don't overrun the output buffer. + */ + if ((endOnInput) && ((ip+length != iend) || (cpy > oend))) { goto _output_error; } } memmove(op, ip, length); /* supports overlapping memory regions, which only matters for in-place decompression scenarios */ ip += length; op += length; - if (!partialDecoding || (cpy == oend)) { - /* Necessarily EOF, due to parsing restrictions */ + /* Necessarily EOF when !partialDecoding. When partialDecoding + * it is EOF if we've either filled the output buffer or hit + * the input parsing restriction. + */ + if (!partialDecoding || (cpy == oend) || (ip == iend)) { break; } - } else { LZ4_wildCopy8(op, ip, cpy); /* may overwrite up to WILDCOPYLENGTH beyond cpy */ ip += length; op = cpy; -- cgit v1.2.3 From 3c40db8d258716b9efcfb46fa6dc29de6e43e616 Mon Sep 17 00:00:00 2001 From: Nick Terrell Date: Fri, 12 Jul 2019 19:27:00 -0700 Subject: [ossfuzz] Improve the fuzzers * Run more decompression variants * Round trip the compression fuzzer and do partial decompression as well * Add a compression fuzzer that compresses into a smaller output buffer and test the destSize variant These fuzzers caught 2 bugs that were fixed in the previous commit. * Input buffer over-read in partial decompress * Partial decompress fails if output size is 0 --- ossfuzz/Makefile | 1 + ossfuzz/compress_fuzzer.c | 52 +++++++++++++++++++------- ossfuzz/decompress_fuzzer.c | 60 ++++++++++++++++++++++-------- ossfuzz/fuzz.h | 48 ++++++++++++++++++++++++ ossfuzz/fuzz_helpers.h | 89 +++++++++++++++++++++++++++++++++++++++++++++ ossfuzz/round_trip_fuzzer.c | 50 +++++++++++++++++++++++++ ossfuzz/standaloneengine.c | 2 +- ossfuzz/testinput.h | 15 -------- 8 files changed, 273 insertions(+), 44 deletions(-) create mode 100644 ossfuzz/fuzz.h create mode 100644 ossfuzz/fuzz_helpers.h create mode 100644 ossfuzz/round_trip_fuzzer.c delete mode 100644 ossfuzz/testinput.h diff --git a/ossfuzz/Makefile b/ossfuzz/Makefile index bd01123..4d24944 100644 --- a/ossfuzz/Makefile +++ b/ossfuzz/Makefile @@ -57,3 +57,4 @@ $(LZ4DIR)/liblz4.a: .PHONY: clean clean: compress_fuzzer_clean decompress_fuzzer_clean + $(MAKE) -C $(LZ4DIR) clean diff --git a/ossfuzz/compress_fuzzer.c b/ossfuzz/compress_fuzzer.c index 3908534..7021624 100644 --- a/ossfuzz/compress_fuzzer.c +++ b/ossfuzz/compress_fuzzer.c @@ -1,25 +1,51 @@ +/** + * This fuzz target attempts to compress the fuzzed data with the simple + * compression function with an output buffer that may be too small to + * ensure that the compressor never crashes. + */ + #include #include #include -#include "lz4.h" +#include -#define CHECK(COND) if (!(COND)) { abort(); } +#include "fuzz_helpers.h" +#include "lz4.h" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { - size_t const compressed_dest_size = LZ4_compressBound(size); - char *const dest_buffer = (char *)malloc(compressed_dest_size); + uint32_t seed = FUZZ_seed(&data, &size); + size_t const dstCapacity = FUZZ_rand32(&seed, 0, LZ4_compressBound(size)); + char* const dst = (char*)malloc(dstCapacity); + char* const rt = (char*)malloc(size); + + FUZZ_ASSERT(dst); + FUZZ_ASSERT(rt); - CHECK(dest_buffer != NULL); + /* If compression succeeds it must round trip correctly. */ + { + int const dstSize = LZ4_compress_default((const char*)data, dst, + size, dstCapacity); + if (dstSize > 0) { + int const rtSize = LZ4_decompress_safe(dst, rt, dstSize, size); + FUZZ_ASSERT_MSG(rtSize == size, "Incorrect regenerated size"); + FUZZ_ASSERT_MSG(!memcmp(data, rt, size), "Corruption!"); + } + } - // Allocation succeeded, try compressing the incoming data. - int result = LZ4_compress_default((const char*)data, - dest_buffer, - size, - compressed_dest_size); - CHECK(result != 0); + if (dstCapacity > 0) { + /* Compression succeeds and must round trip correctly. */ + int compressedSize = size; + int const dstSize = LZ4_compress_destSize((const char*)data, dst, + &compressedSize, dstCapacity); + FUZZ_ASSERT(dstSize > 0); + int const rtSize = LZ4_decompress_safe(dst, rt, dstSize, size); + FUZZ_ASSERT_MSG(rtSize == compressedSize, "Incorrect regenerated size"); + FUZZ_ASSERT_MSG(!memcmp(data, rt, compressedSize), "Corruption!"); + } - free(dest_buffer); + free(dst); + free(rt); - return 0; + return 0; } diff --git a/ossfuzz/decompress_fuzzer.c b/ossfuzz/decompress_fuzzer.c index e6e14c4..0267c93 100644 --- a/ossfuzz/decompress_fuzzer.c +++ b/ossfuzz/decompress_fuzzer.c @@ -1,28 +1,58 @@ +/** + * This fuzz target attempts to decompress the fuzzed data with the simple + * decompression function to ensure the decompressor never crashes. + */ + #include #include #include -#include "lz4.h" +#include -#define CHECK(COND) if (!(COND)) { abort(); } +#include "fuzz_helpers.h" +#include "lz4.h" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { - // TODO: Size input buffer pseudo-randomly based on seed extracted from input - size_t const buffer_size = 10 * 1024 * 1024; - char *const dest_buffer = (char *)malloc(buffer_size); - CHECK(dest_buffer != NULL); + uint32_t seed = FUZZ_seed(&data, &size); + size_t const dstCapacity = FUZZ_rand32(&seed, 0, 4 * size); + size_t const smallDictSize = size + 1; + size_t const largeDictSize = 64 * 1024 - 1; + size_t const dictSize = MAX(smallDictSize, largeDictSize); + char* const dst = (char*)malloc(dstCapacity); + char* const dict = (char*)malloc(dictSize + size); + char* const largeDict = dict; + char* const dataAfterDict = dict + dictSize; + char* const smallDict = dataAfterDict - smallDictSize; - // Allocation succeeded, try decompressing the incoming data. - int result = LZ4_decompress_safe((const char*)data, - dest_buffer, - size, - buffer_size); + FUZZ_ASSERT(dst); + FUZZ_ASSERT(dict); - // Ignore the result of decompression. - (void)result; + /* Prepare the dictionary. The data doesn't matter for decompression. */ + memset(dict, 0, dictSize); + memcpy(dataAfterDict, data, size); - free(dest_buffer); + /* Decompress using each possible dictionary configuration. */ + /* No dictionary. */ + LZ4_decompress_safe_usingDict((char const*)data, dst, size, + dstCapacity, NULL, 0); + /* Small external dictonary. */ + LZ4_decompress_safe_usingDict((char const*)data, dst, size, + dstCapacity, smallDict, smallDictSize); + /* Large external dictionary. */ + LZ4_decompress_safe_usingDict((char const*)data, dst, size, + dstCapacity, largeDict, largeDictSize); + /* Small prefix. */ + LZ4_decompress_safe_usingDict((char const*)dataAfterDict, dst, size, + dstCapacity, smallDict, smallDictSize); + /* Large prefix. */ + LZ4_decompress_safe_usingDict((char const*)data, dst, size, + dstCapacity, largeDict, largeDictSize); + /* Partial decompression. */ + LZ4_decompress_safe_partial((char const*)data, dst, size, + dstCapacity, dstCapacity); + free(dst); + free(dict); - return 0; + return 0; } diff --git a/ossfuzz/fuzz.h b/ossfuzz/fuzz.h new file mode 100644 index 0000000..eefac63 --- /dev/null +++ b/ossfuzz/fuzz.h @@ -0,0 +1,48 @@ +/** + * Fuzz target interface. + * Fuzz targets have some common parameters passed as macros during compilation. + * Check the documentation for each individual fuzzer for more parameters. + * + * @param FUZZ_RNG_SEED_SIZE: + * The number of bytes of the source to look at when constructing a seed + * for the deterministic RNG. These bytes are discarded before passing + * the data to lz4 functions. Every fuzzer initializes the RNG exactly + * once before doing anything else, even if it is unused. + * Default: 4. + * @param LZ4_DEBUG: + * This is a parameter for the lz4 library. Defining `LZ4_DEBUG=1` + * enables assert() statements in the lz4 library. Higher levels enable + * logging, so aren't recommended. Defining `LZ4_DEBUG=1` is + * recommended. + * @param LZ4_FORCE_MEMORY_ACCESS: + * This flag controls how the zstd library accesses unaligned memory. + * It can be undefined, or 0 through 2. If it is undefined, it selects + * the method to use based on the compiler. If testing with UBSAN set + * MEM_FORCE_MEMORY_ACCESS=0 to use the standard compliant method. + * @param FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION + * This is the canonical flag to enable deterministic builds for fuzzing. + * Changes to zstd for fuzzing are gated behind this define. + * It is recommended to define this when building zstd for fuzzing. + */ + +#ifndef FUZZ_H +#define FUZZ_H + +#ifndef FUZZ_RNG_SEED_SIZE +# define FUZZ_RNG_SEED_SIZE 4 +#endif + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +int LLVMFuzzerTestOneInput(const uint8_t *src, size_t size); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/ossfuzz/fuzz_helpers.h b/ossfuzz/fuzz_helpers.h new file mode 100644 index 0000000..626f209 --- /dev/null +++ b/ossfuzz/fuzz_helpers.h @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2016-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under both the BSD-style license (found in the + * LICENSE file in the root directory of this source tree) and the GPLv2 (found + * in the COPYING file in the root directory of this source tree). + */ + +/** + * Helper functions for fuzzing. + */ + +#ifndef FUZZ_HELPERS_H +#define FUZZ_HELPERS_H + +#include "fuzz.h" +#include "xxhash.h" +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) + +#define FUZZ_QUOTE_IMPL(str) #str +#define FUZZ_QUOTE(str) FUZZ_QUOTE_IMPL(str) + +/** + * Asserts for fuzzing that are always enabled. + */ +#define FUZZ_ASSERT_MSG(cond, msg) \ + ((cond) ? (void)0 \ + : (fprintf(stderr, "%s: %u: Assertion: `%s' failed. %s\n", __FILE__, \ + __LINE__, FUZZ_QUOTE(cond), (msg)), \ + abort())) +#define FUZZ_ASSERT(cond) FUZZ_ASSERT_MSG((cond), ""); + +#if defined(__GNUC__) +#define FUZZ_STATIC static __inline __attribute__((unused)) +#elif defined(__cplusplus) || \ + (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) +#define FUZZ_STATIC static inline +#elif defined(_MSC_VER) +#define FUZZ_STATIC static __inline +#else +#define FUZZ_STATIC static +#endif + +/** + * Deterministically constructs a seed based on the fuzz input. + * Consumes up to the first FUZZ_RNG_SEED_SIZE bytes of the input. + */ +FUZZ_STATIC uint32_t FUZZ_seed(uint8_t const **src, size_t* size) { + uint8_t const *data = *src; + size_t const toHash = MIN(FUZZ_RNG_SEED_SIZE, *size); + *size -= toHash; + *src += toHash; + return XXH32(data, toHash, 0); +} + +#define FUZZ_rotl32(x, r) (((x) << (r)) | ((x) >> (32 - (r)))) + +FUZZ_STATIC uint32_t FUZZ_rand(uint32_t *state) { + static const uint32_t prime1 = 2654435761U; + static const uint32_t prime2 = 2246822519U; + uint32_t rand32 = *state; + rand32 *= prime1; + rand32 += prime2; + rand32 = FUZZ_rotl32(rand32, 13); + *state = rand32; + return rand32 >> 5; +} + +/* Returns a random numer in the range [min, max]. */ +FUZZ_STATIC uint32_t FUZZ_rand32(uint32_t *state, uint32_t min, uint32_t max) { + uint32_t random = FUZZ_rand(state); + return min + (random % (max - min + 1)); +} + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/ossfuzz/round_trip_fuzzer.c b/ossfuzz/round_trip_fuzzer.c new file mode 100644 index 0000000..3a66e80 --- /dev/null +++ b/ossfuzz/round_trip_fuzzer.c @@ -0,0 +1,50 @@ +/** + * This fuzz target performs a lz4 round-trip test (compress & decompress), + * compares the result with the original, and calls abort() on corruption. + */ + +#include +#include +#include +#include + +#include "fuzz_helpers.h" +#include "lz4.h" + +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + uint32_t seed = FUZZ_seed(&data, &size); + size_t const dstCapacity = LZ4_compressBound(size); + char* const dst = (char*)malloc(dstCapacity); + char* const rt = (char*)malloc(size); + + FUZZ_ASSERT(dst); + FUZZ_ASSERT(rt); + + /* Compression must succeed and round trip correctly. */ + int const dstSize = LZ4_compress_default((const char*)data, dst, + size, dstCapacity); + FUZZ_ASSERT(dstSize > 0); + + int const rtSize = LZ4_decompress_safe(dst, rt, dstSize, size); + FUZZ_ASSERT_MSG(rtSize == size, "Incorrect size"); + FUZZ_ASSERT_MSG(!memcmp(data, rt, size), "Corruption!"); + + /* Partial decompression must succeed. */ + { + size_t const partialCapacity = FUZZ_rand32(&seed, 0, size); + char* const partial = (char*)malloc(partialCapacity); + FUZZ_ASSERT(partial); + int const partialSize = LZ4_decompress_safe_partial( + dst, partial, dstSize, partialCapacity, partialCapacity); + FUZZ_ASSERT(partialSize >= 0); + FUZZ_ASSERT_MSG(partialSize == partialCapacity, "Incorrect size"); + FUZZ_ASSERT_MSG(!memcmp(data, partial, partialSize), "Corruption!"); + free(partial); + } + + free(dst); + free(rt); + + return 0; +} diff --git a/ossfuzz/standaloneengine.c b/ossfuzz/standaloneengine.c index 175360e..6afeffd 100644 --- a/ossfuzz/standaloneengine.c +++ b/ossfuzz/standaloneengine.c @@ -2,7 +2,7 @@ #include #include -#include "testinput.h" +#include "fuzz.h" /** * Main procedure for standalone fuzzing engine. diff --git a/ossfuzz/testinput.h b/ossfuzz/testinput.h deleted file mode 100644 index 0e50a3c..0000000 --- a/ossfuzz/testinput.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef TESTINPUT_H_INCLUDED -#define TESTINPUT_H_INCLUDED - -#include - -#if defined (__cplusplus) -extern "C" { -#endif - -int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size); - -#if defined(__cplusplus) -} -#endif -#endif -- cgit v1.2.3 From 690009e2c2f9e5dcb0d40e7c0c40610ce6006eda Mon Sep 17 00:00:00 2001 From: Nick Terrell Date: Wed, 17 Jul 2019 11:07:24 -0700 Subject: [LZ4_compress_destSize] Allow 2 more bytes of match length --- lib/lz4.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/lz4.c b/lib/lz4.c index 5b03e3d..1e80c98 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1016,7 +1016,7 @@ _next_match: (unlikely(op + (1 + LASTLITERALS) + (matchCode>>8) > olimit)) ) { if (outputDirective == fillOutput) { /* Match description too long : reduce it */ - U32 newMatchCode = 15 /* in token */ - 1 /* to avoid needing a zero byte */ + ((U32)(olimit - op) - 2 - 1 - LASTLITERALS) * 255; + U32 newMatchCode = 15 /* in token */ - 1 /* to avoid needing a zero byte */ + ((U32)(olimit - op) - 1 - LASTLITERALS) * 255; ip -= matchCode - newMatchCode; matchCode = newMatchCode; } else { -- cgit v1.2.3 From 6bc6f836a18d1f8fd05c8fc2b42f1d800bc25de1 Mon Sep 17 00:00:00 2001 From: Nick Terrell Date: Wed, 17 Jul 2019 11:28:38 -0700 Subject: [LZ4_compress_destSize] Fix rare data corruption bug --- lib/lz4.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/lib/lz4.c b/lib/lz4.c index 1e80c98..461644d 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -648,6 +648,18 @@ LZ4_FORCE_INLINE U32 LZ4_hashPosition(const void* const p, tableType_t const tab return LZ4_hash4(LZ4_read32(p), tableType); } +static void LZ4_clearHash(U32 h, void* tableBase, tableType_t const tableType) +{ + switch (tableType) + { + default: /* fallthrough */ + case clearedTable: { /* illegal! */ assert(0); return; } + case byPtr: { const BYTE** hashTable = (const BYTE**)tableBase; hashTable[h] = NULL; return; } + case byU32: { U32* hashTable = (U32*) tableBase; hashTable[h] = 0; return; } + case byU16: { U16* hashTable = (U16*) tableBase; hashTable[h] = 0; return; } + } +} + static void LZ4_putIndexOnHash(U32 idx, U32 h, void* tableBase, tableType_t const tableType) { switch (tableType) @@ -848,6 +860,7 @@ LZ4_FORCE_INLINE int LZ4_compress_generic( for ( ; ; ) { const BYTE* match; BYTE* token; + const BYTE* filledIp; /* Find a match */ if (tableType == byPtr) { @@ -934,6 +947,7 @@ LZ4_FORCE_INLINE int LZ4_compress_generic( } /* Catch up */ + filledIp = ip; while (((ip>anchor) & (match > lowLimit)) && (unlikely(ip[-1]==match[-1]))) { ip--; match--; } /* Encode Literals */ @@ -1018,7 +1032,21 @@ _next_match: /* Match description too long : reduce it */ U32 newMatchCode = 15 /* in token */ - 1 /* to avoid needing a zero byte */ + ((U32)(olimit - op) - 1 - LASTLITERALS) * 255; ip -= matchCode - newMatchCode; + assert(newMatchCode < matchCode); matchCode = newMatchCode; + if (unlikely(ip < filledIp)) { + /* We have already filled up to filledIp so if ip ends up less than filledIp + * we have positions in the hash table beyond the current position. This is + * a problem if we reuse the hash table. So we have to remove these positions + * from the hash table. + */ + const BYTE* ptr; + DEBUGLOG(5, "Clearing %u positions", (U32)(filledIp - ip)); + for (ptr = ip + 1; ptr <= filledIp; ++ptr) { + U32 const h = LZ4_hashPosition(ptr, tableType); + LZ4_clearHash(h, cctx->hashTable, tableType); + } + } } else { assert(outputDirective == limitedOutput); return 0; /* cannot compress within `dst` budget. Stored indexes in hash table are nonetheless fine */ @@ -1038,6 +1066,8 @@ _next_match: } else *token += (BYTE)(matchCode); } + /* Ensure we have enough space for the last literals. */ + assert(!(outputDirective == fillOutput && op + 1 + LASTLITERALS > olimit)); anchor = ip; -- cgit v1.2.3 From 13a2d9e34ffc4170720ce417c73e396d0ac1471a Mon Sep 17 00:00:00 2001 From: Nick Terrell Date: Wed, 17 Jul 2019 11:50:47 -0700 Subject: [LZ4_compress_destSize] Fix overflow condition --- lib/lz4.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/lz4.c b/lib/lz4.c index 461644d..74a9247 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1027,7 +1027,7 @@ _next_match: } if ((outputDirective) && /* Check output buffer overflow */ - (unlikely(op + (1 + LASTLITERALS) + (matchCode>>8) > olimit)) ) { + (unlikely(op + (1 + LASTLITERALS) + (matchCode+240)/255 > olimit)) ) { if (outputDirective == fillOutput) { /* Match description too long : reduce it */ U32 newMatchCode = 15 /* in token */ - 1 /* to avoid needing a zero byte */ + ((U32)(olimit - op) - 1 - LASTLITERALS) * 255; -- cgit v1.2.3 From 99d925f997923f24492d2c68d932bc958f98ace9 Mon Sep 17 00:00:00 2001 From: dooxe Date: Thu, 18 Jul 2019 11:25:43 +0200 Subject: Added BUNDLE DESTINATION in CMakeLists.txt so that it works with newer versions of cmake --- contrib/cmake_unofficial/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/contrib/cmake_unofficial/CMakeLists.txt b/contrib/cmake_unofficial/CMakeLists.txt index b09c4fb..42d92ea 100644 --- a/contrib/cmake_unofficial/CMakeLists.txt +++ b/contrib/cmake_unofficial/CMakeLists.txt @@ -172,6 +172,7 @@ if(NOT LZ4_BUNDLED_MODE) include(GNUInstallDirs) install(TARGETS ${LZ4_PROGRAMS_BUILT} + BUNDLE DESTINATION "${CMAKE_INSTALL_BINDIR}" RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}") install(TARGETS ${LZ4_LIBRARIES_BUILT} LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" -- cgit v1.2.3 From 369fb3900cbc73543f1bab276ca1b82abe402937 Mon Sep 17 00:00:00 2001 From: "W. Felix Handte" Date: Thu, 18 Jul 2019 12:41:12 -0400 Subject: Fix Data Corruption Bug when Streaming with an Attached Dict in HC Mode This diff fixes an issue in which we failed to clear the `dictCtx` in HC compression. The `dictCtx` is not supposed to be used when an `extDict` is present: matches found in the `dictCtx` do not account for the presence of an `extDict` segment, and their offsets are therefore miscalculated when one is present. This can lead to data corruption. This diff clears the `dictCtx` whenever setting an `extDict`. This issue was uncovered by @terrelln's fuzzing work. --- lib/lz4hc.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/lz4hc.c b/lib/lz4hc.c index 46c20bc..d9e55a0 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -998,6 +998,11 @@ static void LZ4HC_setExternalDict(LZ4HC_CCtx_internal* ctxPtr, const BYTE* newBl if (ctxPtr->end >= ctxPtr->base + ctxPtr->dictLimit + 4) LZ4HC_Insert (ctxPtr, ctxPtr->end-3); /* Referencing remaining dictionary content */ + /* cannot reference an extDict and a dictCtx at the same time */ + if (ctxPtr->dictCtx != NULL) { + ctxPtr->dictCtx = NULL; + } + /* Only one memory segment for extDict, so any previous extDict is lost at this stage */ ctxPtr->lowLimit = ctxPtr->dictLimit; ctxPtr->dictLimit = (U32)(ctxPtr->end - ctxPtr->base); -- cgit v1.2.3 From 40943ba0c90483dbe229299c39d048ae79f08246 Mon Sep 17 00:00:00 2001 From: "W. Felix Handte" Date: Thu, 18 Jul 2019 13:35:12 -0400 Subject: Unconditionally Clear `dictCtx` --- lib/lz4hc.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/lz4hc.c b/lib/lz4hc.c index d9e55a0..29288a5 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -998,11 +998,6 @@ static void LZ4HC_setExternalDict(LZ4HC_CCtx_internal* ctxPtr, const BYTE* newBl if (ctxPtr->end >= ctxPtr->base + ctxPtr->dictLimit + 4) LZ4HC_Insert (ctxPtr, ctxPtr->end-3); /* Referencing remaining dictionary content */ - /* cannot reference an extDict and a dictCtx at the same time */ - if (ctxPtr->dictCtx != NULL) { - ctxPtr->dictCtx = NULL; - } - /* Only one memory segment for extDict, so any previous extDict is lost at this stage */ ctxPtr->lowLimit = ctxPtr->dictLimit; ctxPtr->dictLimit = (U32)(ctxPtr->end - ctxPtr->base); @@ -1010,6 +1005,9 @@ static void LZ4HC_setExternalDict(LZ4HC_CCtx_internal* ctxPtr, const BYTE* newBl ctxPtr->base = newBlock - ctxPtr->dictLimit; ctxPtr->end = newBlock; ctxPtr->nextToUpdate = ctxPtr->dictLimit; /* match referencing will resume from there */ + + /* cannot reference an extDict and a dictCtx at the same time */ + ctxPtr->dictCtx = NULL; } static int LZ4_compressHC_continue_generic (LZ4_streamHC_t* LZ4_streamHCPtr, -- cgit v1.2.3 From 7c32101c655d93b61fc212dcd512b87119dd7333 Mon Sep 17 00:00:00 2001 From: Nick Terrell Date: Thu, 18 Jul 2019 12:20:29 -0700 Subject: [LZ4_compress_destSize] Fix off-by-one error in fix The next match is looking at the current ip, not the next ip, so it needs to be cleared as well. Credit to OSS-Fuzz --- lib/lz4.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/lz4.c b/lib/lz4.c index 74a9247..70424b9 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1042,7 +1042,7 @@ _next_match: */ const BYTE* ptr; DEBUGLOG(5, "Clearing %u positions", (U32)(filledIp - ip)); - for (ptr = ip + 1; ptr <= filledIp; ++ptr) { + for (ptr = ip; ptr <= filledIp; ++ptr) { U32 const h = LZ4_hashPosition(ptr, tableType); LZ4_clearHash(h, cctx->hashTable, tableType); } -- cgit v1.2.3 From 9b258abd93e19dece681ea59db9c31e1e27635ff Mon Sep 17 00:00:00 2001 From: Nick Terrell Date: Wed, 17 Jul 2019 17:33:31 -0700 Subject: [fuzz] Add a streaming round trip fuzzer --- ossfuzz/round_trip_stream_fuzzer.c | 217 +++++++++++++++++++++++++++++++++++++ 1 file changed, 217 insertions(+) create mode 100644 ossfuzz/round_trip_stream_fuzzer.c diff --git a/ossfuzz/round_trip_stream_fuzzer.c b/ossfuzz/round_trip_stream_fuzzer.c new file mode 100644 index 0000000..4facfc9 --- /dev/null +++ b/ossfuzz/round_trip_stream_fuzzer.c @@ -0,0 +1,217 @@ +/** + * This fuzz target performs a lz4 streaming round-trip test + * (compress & decompress), compares the result with the original, and calls + * abort() on corruption. + */ + +#include +#include +#include +#include + +#include "fuzz_helpers.h" +#define LZ4_STATIC_LINKING_ONLY +#include "lz4.h" + +typedef struct { + char const* buf; + size_t size; + size_t pos; +} const_cursor_t; + +typedef struct { + char* buf; + size_t size; + size_t pos; +} cursor_t; + +typedef struct { + LZ4_stream_t* cstream; + LZ4_streamDecode_t* dstream; + const_cursor_t data; + cursor_t compressed; + cursor_t roundTrip; + uint32_t seed; +} state_t; + +cursor_t cursor_create(size_t size) +{ + cursor_t cursor; + cursor.buf = (char*)malloc(size); + cursor.size = size; + cursor.pos = 0; + FUZZ_ASSERT(cursor.buf); + return cursor; +} + +void cursor_free(cursor_t cursor) +{ + free(cursor.buf); +} + +state_t state_create(char const* data, size_t size, uint32_t seed) +{ + state_t state; + + state.seed = seed; + + state.data.buf = (char const*)data; + state.data.size = size; + state.data.pos = 0; + + /* Extra margin because we are streaming. */ + state.compressed = cursor_create(1024 + 2 * LZ4_compressBound(size)); + state.roundTrip = cursor_create(size); + + state.cstream = LZ4_createStream(); + FUZZ_ASSERT(state.cstream); + state.dstream = LZ4_createStreamDecode(); + FUZZ_ASSERT(state.dstream); + + return state; +} + +void state_free(state_t state) +{ + cursor_free(state.compressed); + cursor_free(state.roundTrip); + LZ4_freeStream(state.cstream); + LZ4_freeStreamDecode(state.dstream); +} + +static void state_reset(state_t* state, uint32_t seed) +{ + LZ4_resetStream_fast(state->cstream); + LZ4_setStreamDecode(state->dstream, NULL, 0); + state->data.pos = 0; + state->compressed.pos = 0; + state->roundTrip.pos = 0; + state->seed = seed; +} + +static void state_decompress(state_t* state, char const* src, int srcSize) +{ + char* dst = state->roundTrip.buf + state->roundTrip.pos; + int const dstCapacity = state->roundTrip.size - state->roundTrip.pos; + int const dSize = LZ4_decompress_safe_continue(state->dstream, src, dst, + srcSize, dstCapacity); + FUZZ_ASSERT(dSize >= 0); + state->roundTrip.pos += dSize; +} + +static void state_checkRoundTrip(state_t const* state) +{ + char const* data = state->data.buf; + size_t const size = state->data.size; + FUZZ_ASSERT_MSG(size == state->roundTrip.pos, "Incorrect size!"); + FUZZ_ASSERT_MSG(!memcmp(data, state->roundTrip.buf, size), "Corruption!"); +} + +/** + * Picks a dictionary size and trims the dictionary off of the data. + */ +static size_t state_trimDict(state_t* state) +{ + /* 64 KB is the max dict size, allow slightly beyond that to test trim. */ + uint32_t maxDictSize = MIN(70 * 1024, state->data.size); + size_t const dictSize = FUZZ_rand32(&state->seed, 0, maxDictSize); + FUZZ_ASSERT(state->compressed.pos == 0); + state->compressed.pos += dictSize; + return dictSize; +} + +static void state_prefixRoundTrip(state_t* state) +{ + while (state->data.pos != state->data.size) { + char const* src = state->data.buf + state->data.pos; + char* dst = state->compressed.buf + state->compressed.pos; + int const srcRemaining = state->data.size - state->data.pos; + int const srcSize = FUZZ_rand32(&state->seed, 0, srcRemaining); + int const dstCapacity = state->compressed.size - state->compressed.pos; + int const cSize = LZ4_compress_fast_continue(state->cstream, src, dst, + srcSize, dstCapacity, 0); + FUZZ_ASSERT(cSize > 0); + state->data.pos += srcSize; + state->compressed.pos += cSize; + state_decompress(state, dst, cSize); + } +} + +static void state_extDictRoundTrip(state_t* state) +{ + int i = 0; + cursor_t data2 = cursor_create(state->data.size); + memcpy(data2.buf, state->data.buf, state->data.size); + while (state->data.pos != state->data.size) { + char const* data = (i++ & 1) ? state->data.buf : data2.buf; + char const* src = data + state->data.pos; + char* dst = state->compressed.buf + state->compressed.pos; + int const srcRemaining = state->data.size - state->data.pos; + int const srcSize = FUZZ_rand32(&state->seed, 0, srcRemaining); + int const dstCapacity = state->compressed.size - state->compressed.pos; + int const cSize = LZ4_compress_fast_continue(state->cstream, src, dst, + srcSize, dstCapacity, 0); + FUZZ_ASSERT(cSize > 0); + state->data.pos += srcSize; + state->compressed.pos += cSize; + state_decompress(state, dst, cSize); + } + cursor_free(data2); +} + +static void state_randomRoundTrip(state_t* state) +{ + if (FUZZ_rand32(&state->seed, 0, 1)) { + state_prefixRoundTrip(state); + } else { + state_extDictRoundTrip(state); + } +} + +static void state_loadDictRoundTrip(state_t* state) +{ + char const* dict = state->compressed.buf; + size_t const dictSize = state_trimDict(state); + LZ4_loadDict(state->cstream, dict, dictSize); + LZ4_setStreamDecode(state->dstream, dict, dictSize); + state_randomRoundTrip(state); +} + +static void state_attachDictRoundTrip(state_t* state) +{ + char const* dict = state->compressed.buf; + size_t const dictSize = state_trimDict(state); + LZ4_stream_t* dictStream = LZ4_createStream(); + LZ4_loadDict(dictStream, dict, dictSize); + LZ4_attach_dictionary(state->cstream, dictStream); + LZ4_setStreamDecode(state->dstream, dict, dictSize); + state_randomRoundTrip(state); + LZ4_freeStream(dictStream); +} + +typedef void (*round_trip_t)(state_t* state); + +round_trip_t roundTrips[] = { + &state_prefixRoundTrip, + &state_extDictRoundTrip, + &state_loadDictRoundTrip, + &state_attachDictRoundTrip, +}; + +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + uint32_t seed = FUZZ_seed(&data, &size); + state_t state = state_create((char const*)data, size, seed); + const int n = sizeof(roundTrips) / sizeof(round_trip_t); + int i; + + for (i = 0; i < n; ++i) { + roundTrips[i](&state); + state_checkRoundTrip(&state); + state_reset(&state, seed); + } + + state_free(state); + + return 0; +} -- cgit v1.2.3 From 399a80d48e4352aeae99eb5cbd799bcee6978a3d Mon Sep 17 00:00:00 2001 From: Nick Terrell Date: Wed, 17 Jul 2019 17:41:41 -0700 Subject: [fuzzer] Update scripts for new fuzzers --- ossfuzz/Makefile | 8 ++++++++ ossfuzz/ossfuzz.sh | 4 ++-- ossfuzz/travisoss.sh | 5 +---- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/ossfuzz/Makefile b/ossfuzz/Makefile index 4d24944..2bb40ec 100644 --- a/ossfuzz/Makefile +++ b/ossfuzz/Makefile @@ -35,6 +35,14 @@ LZ4_CFLAGS = $(CFLAGS) $(DEBUGFLAGS) $(MOREFLAGS) LZ4_CXXFLAGS = $(CXXFLAGS) $(DEBUGFLAGS) $(MOREFLAGS) LZ4_CPPFLAGS = $(CPPFLAGS) -I$(LZ4DIR) -DXXH_NAMESPACE=LZ4_ +FUZZERS := \ + compress_fuzzer \ + decompress_fuzzer \ + round_trip_fuzzer \ + round_trip_stream_fuzzer + +all: $(FUZZERS) + # Include a rule to build the static library if calling this target # directly. $(LZ4DIR)/liblz4.a: diff --git a/ossfuzz/ossfuzz.sh b/ossfuzz/ossfuzz.sh index a76b0d6..9782286 100755 --- a/ossfuzz/ossfuzz.sh +++ b/ossfuzz/ossfuzz.sh @@ -16,8 +16,8 @@ echo "OUT: $OUT" export MAKEFLAGS+="-j$(nproc)" pushd ossfuzz -make V=1 compress_fuzzer decompress_fuzzer +make V=1 all popd # Copy the fuzzers to the target directory. -cp -v ossfuzz/compress_fuzzer ossfuzz/decompress_fuzzer $OUT/ +cp -v ossfuzz/*_fuzzer $OUT/ diff --git a/ossfuzz/travisoss.sh b/ossfuzz/travisoss.sh index 3b2f26f..5ea884c 100755 --- a/ossfuzz/travisoss.sh +++ b/ossfuzz/travisoss.sh @@ -8,10 +8,7 @@ git clone https://github.com/google/oss-fuzz.git /tmp/ossfuzz if [[ ! -d /tmp/ossfuzz/projects/lz4 ]] then echo "Could not find the lz4 project in ossfuzz" - - # Exit with a success code while the lz4 project is not expected to exist - # on oss-fuzz. - exit 0 + exit 1 fi # Modify the oss-fuzz Dockerfile so that we're checking out the current branch on travis. -- cgit v1.2.3 From 675ef9a9fc9899667d0fe9b7b2a66e402e870a6d Mon Sep 17 00:00:00 2001 From: Nick Terrell Date: Wed, 17 Jul 2019 18:31:44 -0700 Subject: [fuzz] Add HC fuzzers for round trip, compress, and streaming --- ossfuzz/Makefile | 4 +- ossfuzz/compress_hc_fuzzer.c | 57 ++++++++++++++++++++ ossfuzz/fuzz_helpers.h | 9 +++- ossfuzz/round_trip_hc_fuzzer.c | 39 ++++++++++++++ ossfuzz/round_trip_stream_fuzzer.c | 107 +++++++++++++++++++++++++++++++++---- 5 files changed, 202 insertions(+), 14 deletions(-) create mode 100644 ossfuzz/compress_hc_fuzzer.c create mode 100644 ossfuzz/round_trip_hc_fuzzer.c diff --git a/ossfuzz/Makefile b/ossfuzz/Makefile index 2bb40ec..9974b81 100644 --- a/ossfuzz/Makefile +++ b/ossfuzz/Makefile @@ -39,7 +39,9 @@ FUZZERS := \ compress_fuzzer \ decompress_fuzzer \ round_trip_fuzzer \ - round_trip_stream_fuzzer + round_trip_stream_fuzzer \ + compress_hc_fuzzer \ + round_trip_hc_fuzzer all: $(FUZZERS) diff --git a/ossfuzz/compress_hc_fuzzer.c b/ossfuzz/compress_hc_fuzzer.c new file mode 100644 index 0000000..4841367 --- /dev/null +++ b/ossfuzz/compress_hc_fuzzer.c @@ -0,0 +1,57 @@ +/** + * This fuzz target attempts to compress the fuzzed data with the simple + * compression function with an output buffer that may be too small to + * ensure that the compressor never crashes. + */ + +#include +#include +#include +#include + +#include "fuzz_helpers.h" +#include "lz4.h" +#include "lz4hc.h" + +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + uint32_t seed = FUZZ_seed(&data, &size); + size_t const dstCapacity = FUZZ_rand32(&seed, 0, LZ4_compressBound(size)); + char* const dst = (char*)malloc(dstCapacity); + char* const rt = (char*)malloc(size); + int const level = FUZZ_rand32(&seed, LZ4HC_CLEVEL_MIN, LZ4HC_CLEVEL_MAX); + + FUZZ_ASSERT(dst); + FUZZ_ASSERT(rt); + + /* If compression succeeds it must round trip correctly. */ + { + int const dstSize = LZ4_compress_HC((const char*)data, dst, size, + dstCapacity, level); + if (dstSize > 0) { + int const rtSize = LZ4_decompress_safe(dst, rt, dstSize, size); + FUZZ_ASSERT_MSG(rtSize == size, "Incorrect regenerated size"); + FUZZ_ASSERT_MSG(!memcmp(data, rt, size), "Corruption!"); + } + } + + if (dstCapacity > 0) { + /* Compression succeeds and must round trip correctly. */ + void* state = malloc(LZ4_sizeofStateHC()); + FUZZ_ASSERT(state); + int compressedSize = size; + int const dstSize = LZ4_compress_HC_destSize(state, (const char*)data, + dst, &compressedSize, + dstCapacity, level); + FUZZ_ASSERT(dstSize > 0); + int const rtSize = LZ4_decompress_safe(dst, rt, dstSize, size); + FUZZ_ASSERT_MSG(rtSize == compressedSize, "Incorrect regenerated size"); + FUZZ_ASSERT_MSG(!memcmp(data, rt, compressedSize), "Corruption!"); + free(state); + } + + free(dst); + free(rt); + + return 0; +} diff --git a/ossfuzz/fuzz_helpers.h b/ossfuzz/fuzz_helpers.h index 626f209..c4a8645 100644 --- a/ossfuzz/fuzz_helpers.h +++ b/ossfuzz/fuzz_helpers.h @@ -24,8 +24,13 @@ extern "C" { #endif -#define MIN(a, b) ((a) < (b) ? (a) : (b)) -#define MAX(a, b) ((a) > (b) ? (a) : (b)) +#define LZ4_COMMONDEFS_ONLY +#ifndef LZ4_SRC_INCLUDED +#include "lz4.c" /* LZ4_count, constants, mem */ +#endif + +#define MIN(a,b) ( (a) < (b) ? (a) : (b) ) +#define MAX(a,b) ( (a) > (b) ? (a) : (b) ) #define FUZZ_QUOTE_IMPL(str) #str #define FUZZ_QUOTE(str) FUZZ_QUOTE_IMPL(str) diff --git a/ossfuzz/round_trip_hc_fuzzer.c b/ossfuzz/round_trip_hc_fuzzer.c new file mode 100644 index 0000000..325cdf0 --- /dev/null +++ b/ossfuzz/round_trip_hc_fuzzer.c @@ -0,0 +1,39 @@ +/** + * This fuzz target performs a lz4 round-trip test (compress & decompress), + * compares the result with the original, and calls abort() on corruption. + */ + +#include +#include +#include +#include + +#include "fuzz_helpers.h" +#include "lz4.h" +#include "lz4hc.h" + +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + uint32_t seed = FUZZ_seed(&data, &size); + size_t const dstCapacity = LZ4_compressBound(size); + char* const dst = (char*)malloc(dstCapacity); + char* const rt = (char*)malloc(size); + int const level = FUZZ_rand32(&seed, LZ4HC_CLEVEL_MIN, LZ4HC_CLEVEL_MAX); + + FUZZ_ASSERT(dst); + FUZZ_ASSERT(rt); + + /* Compression must succeed and round trip correctly. */ + int const dstSize = LZ4_compress_HC((const char*)data, dst, size, + dstCapacity, level); + FUZZ_ASSERT(dstSize > 0); + + int const rtSize = LZ4_decompress_safe(dst, rt, dstSize, size); + FUZZ_ASSERT_MSG(rtSize == size, "Incorrect size"); + FUZZ_ASSERT_MSG(!memcmp(data, rt, size), "Corruption!"); + + free(dst); + free(rt); + + return 0; +} diff --git a/ossfuzz/round_trip_stream_fuzzer.c b/ossfuzz/round_trip_stream_fuzzer.c index 4facfc9..abfcd2d 100644 --- a/ossfuzz/round_trip_stream_fuzzer.c +++ b/ossfuzz/round_trip_stream_fuzzer.c @@ -12,6 +12,8 @@ #include "fuzz_helpers.h" #define LZ4_STATIC_LINKING_ONLY #include "lz4.h" +#define LZ4_HC_STATIC_LINKING_ONLY +#include "lz4hc.h" typedef struct { char const* buf; @@ -27,11 +29,13 @@ typedef struct { typedef struct { LZ4_stream_t* cstream; + LZ4_streamHC_t* cstreamHC; LZ4_streamDecode_t* dstream; const_cursor_t data; cursor_t compressed; cursor_t roundTrip; uint32_t seed; + int level; } state_t; cursor_t cursor_create(size_t size) @@ -44,6 +48,8 @@ cursor_t cursor_create(size_t size) return cursor; } +typedef void (*round_trip_t)(state_t* state); + void cursor_free(cursor_t cursor) { free(cursor.buf); @@ -65,6 +71,8 @@ state_t state_create(char const* data, size_t size, uint32_t seed) state.cstream = LZ4_createStream(); FUZZ_ASSERT(state.cstream); + state.cstreamHC = LZ4_createStreamHC(); + FUZZ_ASSERT(state.cstream); state.dstream = LZ4_createStreamDecode(); FUZZ_ASSERT(state.dstream); @@ -76,12 +84,15 @@ void state_free(state_t state) cursor_free(state.compressed); cursor_free(state.roundTrip); LZ4_freeStream(state.cstream); + LZ4_freeStreamHC(state.cstreamHC); LZ4_freeStreamDecode(state.dstream); } static void state_reset(state_t* state, uint32_t seed) { + state->level = FUZZ_rand32(&seed, LZ4HC_CLEVEL_MIN, LZ4HC_CLEVEL_MAX); LZ4_resetStream_fast(state->cstream); + LZ4_resetStreamHC_fast(state->cstreamHC, state->level); LZ4_setStreamDecode(state->dstream, NULL, 0); state->data.pos = 0; state->compressed.pos = 0; @@ -109,14 +120,19 @@ static void state_checkRoundTrip(state_t const* state) /** * Picks a dictionary size and trims the dictionary off of the data. + * We copy the dictionary to the roundTrip so our validation passes. */ static size_t state_trimDict(state_t* state) { /* 64 KB is the max dict size, allow slightly beyond that to test trim. */ uint32_t maxDictSize = MIN(70 * 1024, state->data.size); size_t const dictSize = FUZZ_rand32(&state->seed, 0, maxDictSize); - FUZZ_ASSERT(state->compressed.pos == 0); - state->compressed.pos += dictSize; + DEBUGLOG(2, "dictSize = %zu", dictSize); + FUZZ_ASSERT(state->data.pos == 0); + FUZZ_ASSERT(state->roundTrip.pos == 0); + memcpy(state->roundTrip.buf, state->data.buf, dictSize); + state->data.pos += dictSize; + state->roundTrip.pos += dictSize; return dictSize; } @@ -159,43 +175,111 @@ static void state_extDictRoundTrip(state_t* state) cursor_free(data2); } -static void state_randomRoundTrip(state_t* state) +static void state_randomRoundTrip(state_t* state, round_trip_t rt0, + round_trip_t rt1) { if (FUZZ_rand32(&state->seed, 0, 1)) { - state_prefixRoundTrip(state); + rt0(state); } else { - state_extDictRoundTrip(state); + rt1(state); } } static void state_loadDictRoundTrip(state_t* state) { - char const* dict = state->compressed.buf; + char const* dict = state->data.buf; size_t const dictSize = state_trimDict(state); LZ4_loadDict(state->cstream, dict, dictSize); LZ4_setStreamDecode(state->dstream, dict, dictSize); - state_randomRoundTrip(state); + state_randomRoundTrip(state, state_prefixRoundTrip, state_extDictRoundTrip); } static void state_attachDictRoundTrip(state_t* state) { - char const* dict = state->compressed.buf; + char const* dict = state->data.buf; size_t const dictSize = state_trimDict(state); LZ4_stream_t* dictStream = LZ4_createStream(); LZ4_loadDict(dictStream, dict, dictSize); LZ4_attach_dictionary(state->cstream, dictStream); LZ4_setStreamDecode(state->dstream, dict, dictSize); - state_randomRoundTrip(state); + state_randomRoundTrip(state, state_prefixRoundTrip, state_extDictRoundTrip); LZ4_freeStream(dictStream); } -typedef void (*round_trip_t)(state_t* state); +static void state_prefixHCRoundTrip(state_t* state) +{ + while (state->data.pos != state->data.size) { + char const* src = state->data.buf + state->data.pos; + char* dst = state->compressed.buf + state->compressed.pos; + int const srcRemaining = state->data.size - state->data.pos; + int const srcSize = FUZZ_rand32(&state->seed, 0, srcRemaining); + int const dstCapacity = state->compressed.size - state->compressed.pos; + int const cSize = LZ4_compress_HC_continue(state->cstreamHC, src, dst, + srcSize, dstCapacity); + FUZZ_ASSERT(cSize > 0); + state->data.pos += srcSize; + state->compressed.pos += cSize; + state_decompress(state, dst, cSize); + } +} + +static void state_extDictHCRoundTrip(state_t* state) +{ + int i = 0; + cursor_t data2 = cursor_create(state->data.size); + DEBUGLOG(2, "extDictHC"); + memcpy(data2.buf, state->data.buf, state->data.size); + while (state->data.pos != state->data.size) { + char const* data = (i++ & 1) ? state->data.buf : data2.buf; + char const* src = data + state->data.pos; + char* dst = state->compressed.buf + state->compressed.pos; + int const srcRemaining = state->data.size - state->data.pos; + int const srcSize = FUZZ_rand32(&state->seed, 0, srcRemaining); + int const dstCapacity = state->compressed.size - state->compressed.pos; + int const cSize = LZ4_compress_HC_continue(state->cstreamHC, src, dst, + srcSize, dstCapacity); + FUZZ_ASSERT(cSize > 0); + DEBUGLOG(2, "srcSize = %d", srcSize); + state->data.pos += srcSize; + state->compressed.pos += cSize; + state_decompress(state, dst, cSize); + } + cursor_free(data2); +} + +static void state_loadDictHCRoundTrip(state_t* state) +{ + char const* dict = state->data.buf; + size_t const dictSize = state_trimDict(state); + LZ4_loadDictHC(state->cstreamHC, dict, dictSize); + LZ4_setStreamDecode(state->dstream, dict, dictSize); + state_randomRoundTrip(state, state_prefixHCRoundTrip, + state_extDictHCRoundTrip); +} + +static void state_attachDictHCRoundTrip(state_t* state) +{ + char const* dict = state->data.buf; + size_t const dictSize = state_trimDict(state); + LZ4_streamHC_t* dictStream = LZ4_createStreamHC(); + LZ4_setCompressionLevel(dictStream, state->level); + LZ4_loadDictHC(dictStream, dict, dictSize); + LZ4_attach_HC_dictionary(state->cstreamHC, dictStream); + LZ4_setStreamDecode(state->dstream, dict, dictSize); + state_randomRoundTrip(state, state_prefixHCRoundTrip, + state_extDictHCRoundTrip); + LZ4_freeStreamHC(dictStream); +} round_trip_t roundTrips[] = { &state_prefixRoundTrip, &state_extDictRoundTrip, &state_loadDictRoundTrip, &state_attachDictRoundTrip, + &state_prefixHCRoundTrip, + &state_extDictHCRoundTrip, + &state_loadDictHCRoundTrip, + &state_attachDictHCRoundTrip, }; int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) @@ -206,9 +290,10 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) int i; for (i = 0; i < n; ++i) { + DEBUGLOG(2, "Round trip %d", i); + state_reset(&state, seed); roundTrips[i](&state); state_checkRoundTrip(&state); - state_reset(&state, seed); } state_free(state); -- cgit v1.2.3 From 1f236e0790ab28a223e89bbb8e65d3ed067adc89 Mon Sep 17 00:00:00 2001 From: Nick Terrell Date: Wed, 17 Jul 2019 19:40:04 -0700 Subject: Fix LZ4_attach_dictionary with empty dictionary --- lib/lz4.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/lz4.c b/lib/lz4.c index 74a9247..81b3c10 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1429,7 +1429,7 @@ void LZ4_attach_dictionary(LZ4_stream_t* workingStream, const LZ4_stream_t* dict */ LZ4_resetStream_fast(workingStream); - if (dictionaryStream != NULL) { + if (dictionaryStream != NULL && dictionaryStream->internal_donotuse.dictSize > 0) { /* If the current offset is zero, we will never look in the * external dictionary context, since there is no value a table * entry can take that indicate a miss. In that case, we need -- cgit v1.2.3 From b487660309d4245eec87e3ada4712bc2a19df791 Mon Sep 17 00:00:00 2001 From: Nick Terrell Date: Thu, 18 Jul 2019 18:45:32 -0700 Subject: [lz4frame] Skip magic and checksums in fuzzing mode When `FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION` is defined we skip magic and checksum checks. This makes it easier to fuzz decompression. --- lib/lz4frame.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/lz4frame.c b/lib/lz4frame.c index cc7f2d5..13c1ae9 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -1131,8 +1131,10 @@ static size_t LZ4F_decodeHeader(LZ4F_dctx* dctx, const void* src, size_t srcSize } /* control magic number */ +#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION if (LZ4F_readLE32(srcPtr) != LZ4F_MAGICNUMBER) return err0r(LZ4F_ERROR_frameType_unknown); +#endif dctx->frameInfo.frameType = LZ4F_frame; /* Flags */ @@ -1171,10 +1173,12 @@ static size_t LZ4F_decodeHeader(LZ4F_dctx* dctx, const void* src, size_t srcSize /* check header */ assert(frameHeaderSize > 5); +#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION { BYTE const HC = LZ4F_headerChecksum(srcPtr+4, frameHeaderSize-5); if (HC != srcPtr[frameHeaderSize-1]) return err0r(LZ4F_ERROR_headerChecksum_invalid); } +#endif /* save */ dctx->frameInfo.blockMode = (LZ4F_blockMode_t)blockMode; @@ -1211,8 +1215,10 @@ size_t LZ4F_headerSize(const void* src, size_t srcSize) return 8; /* control magic number */ +#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION if (LZ4F_readLE32(src) != LZ4F_MAGICNUMBER) return err0r(LZ4F_ERROR_frameType_unknown); +#endif /* Frame Header Size */ { BYTE const FLG = ((const BYTE*)src)[4]; @@ -1555,8 +1561,10 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, } { U32 const readCRC = LZ4F_readLE32(crcSrc); U32 const calcCRC = XXH32_digest(&dctx->blockChecksum); +#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION if (readCRC != calcCRC) return err0r(LZ4F_ERROR_blockChecksum_invalid); +#endif } } dctx->dStage = dstage_getBlockHeader; /* new block */ break; @@ -1595,8 +1603,10 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, assert(selectedIn != NULL); /* selectedIn is defined at this stage (either srcPtr, or dctx->tmpIn) */ { U32 const readBlockCrc = LZ4F_readLE32(selectedIn + dctx->tmpInTarget); U32 const calcBlockCrc = XXH32(selectedIn, dctx->tmpInTarget, 0); +#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION if (readBlockCrc != calcBlockCrc) return err0r(LZ4F_ERROR_blockChecksum_invalid); +#endif } } if ((size_t)(dstEnd-dstPtr) >= dctx->maxBlockSize) { @@ -1724,8 +1734,10 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, /* case dstage_checkSuffix: */ /* no direct entry, avoid initialization risks */ { U32 const readCRC = LZ4F_readLE32(selectedIn); U32 const resultCRC = XXH32_digest(&(dctx->xxh)); +#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION if (readCRC != resultCRC) return err0r(LZ4F_ERROR_contentChecksum_invalid); +#endif nextSrcSizeHint = 0; LZ4F_resetDecompressionContext(dctx); doAnotherStage = 0; -- cgit v1.2.3 From d28159c025829fc70a295b727e32f899a9e0c7c5 Mon Sep 17 00:00:00 2001 From: Nick Terrell Date: Thu, 18 Jul 2019 18:49:40 -0700 Subject: [fuzz] Add LZ4 frame fuzzers * Round trip fuzzer * Compress fuzzer * Decompress fuzzer --- ossfuzz/Makefile | 10 ++++-- ossfuzz/compress_frame_fuzzer.c | 42 ++++++++++++++++++++++++ ossfuzz/decompress_frame_fuzzer.c | 67 +++++++++++++++++++++++++++++++++++++++ ossfuzz/lz4_helpers.c | 51 +++++++++++++++++++++++++++++ ossfuzz/lz4_helpers.h | 13 ++++++++ ossfuzz/round_trip_frame_fuzzer.c | 39 +++++++++++++++++++++++ 6 files changed, 219 insertions(+), 3 deletions(-) create mode 100644 ossfuzz/compress_frame_fuzzer.c create mode 100644 ossfuzz/decompress_frame_fuzzer.c create mode 100644 ossfuzz/lz4_helpers.c create mode 100644 ossfuzz/lz4_helpers.h create mode 100644 ossfuzz/round_trip_frame_fuzzer.c diff --git a/ossfuzz/Makefile b/ossfuzz/Makefile index 9974b81..6875eb6 100644 --- a/ossfuzz/Makefile +++ b/ossfuzz/Makefile @@ -33,7 +33,8 @@ DEBUGFLAGS = -g -DLZ4_DEBUG=$(DEBUGLEVEL) LZ4_CFLAGS = $(CFLAGS) $(DEBUGFLAGS) $(MOREFLAGS) LZ4_CXXFLAGS = $(CXXFLAGS) $(DEBUGFLAGS) $(MOREFLAGS) -LZ4_CPPFLAGS = $(CPPFLAGS) -I$(LZ4DIR) -DXXH_NAMESPACE=LZ4_ +LZ4_CPPFLAGS = $(CPPFLAGS) -I$(LZ4DIR) -DXXH_NAMESPACE=LZ4_ \ + -DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION FUZZERS := \ compress_fuzzer \ @@ -41,7 +42,10 @@ FUZZERS := \ round_trip_fuzzer \ round_trip_stream_fuzzer \ compress_hc_fuzzer \ - round_trip_hc_fuzzer + round_trip_hc_fuzzer \ + compress_frame_fuzzer \ + round_trip_frame_fuzzer \ + decompress_frame_fuzzer all: $(FUZZERS) @@ -54,7 +58,7 @@ $(LZ4DIR)/liblz4.a: $(CC) -c $(LZ4_CFLAGS) $(LZ4_CPPFLAGS) $< -o $@ # Generic rule for generating fuzzers -%_fuzzer: %_fuzzer.o $(LZ4DIR)/liblz4.a +%_fuzzer: %_fuzzer.o lz4_helpers.o $(LZ4DIR)/liblz4.a # Compile the standalone code just in case. The OSS-Fuzz code might # override the LIB_FUZZING_ENGINE value to "-fsanitize=fuzzer" $(CC) -c $(LZ4_CFLAGS) $(LZ4_CPPFLAGS) standaloneengine.c -o standaloneengine.o diff --git a/ossfuzz/compress_frame_fuzzer.c b/ossfuzz/compress_frame_fuzzer.c new file mode 100644 index 0000000..75c609f --- /dev/null +++ b/ossfuzz/compress_frame_fuzzer.c @@ -0,0 +1,42 @@ +/** + * This fuzz target attempts to compress the fuzzed data with the simple + * compression function with an output buffer that may be too small to + * ensure that the compressor never crashes. + */ + +#include +#include +#include +#include + +#include "fuzz_helpers.h" +#include "lz4.h" +#include "lz4frame.h" +#include "lz4_helpers.h" + +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + uint32_t seed = FUZZ_seed(&data, &size); + LZ4F_preferences_t const prefs = FUZZ_randomPreferences(&seed); + size_t const compressBound = LZ4F_compressFrameBound(size, &prefs); + size_t const dstCapacity = FUZZ_rand32(&seed, 0, compressBound); + char* const dst = (char*)malloc(dstCapacity); + char* const rt = (char*)malloc(size); + + FUZZ_ASSERT(dst); + FUZZ_ASSERT(rt); + + /* If compression succeeds it must round trip correctly. */ + size_t const dstSize = + LZ4F_compressFrame(dst, dstCapacity, data, size, &prefs); + if (!LZ4F_isError(dstSize)) { + size_t const rtSize = FUZZ_decompressFrame(rt, size, dst, dstSize); + FUZZ_ASSERT_MSG(rtSize == size, "Incorrect regenerated size"); + FUZZ_ASSERT_MSG(!memcmp(data, rt, size), "Corruption!"); + } + + free(dst); + free(rt); + + return 0; +} diff --git a/ossfuzz/decompress_frame_fuzzer.c b/ossfuzz/decompress_frame_fuzzer.c new file mode 100644 index 0000000..bda25b0 --- /dev/null +++ b/ossfuzz/decompress_frame_fuzzer.c @@ -0,0 +1,67 @@ +/** + * This fuzz target attempts to decompress the fuzzed data with the simple + * decompression function to ensure the decompressor never crashes. + */ + +#include +#include +#include +#include + +#include "fuzz_helpers.h" +#include "lz4.h" +#define LZ4F_STATIC_LINKING_ONLY +#include "lz4frame.h" +#include "lz4_helpers.h" + +static void decompress(LZ4F_dctx* dctx, void* dst, size_t dstCapacity, + const void* src, size_t srcSize, + const void* dict, size_t dictSize, + const LZ4F_decompressOptions_t* opts) +{ + LZ4F_resetDecompressionContext(dctx); + if (dictSize == 0) + LZ4F_decompress(dctx, dst, &dstCapacity, src, &srcSize, opts); + else + LZ4F_decompress_usingDict(dctx, dst, &dstCapacity, src, &srcSize, + dict, dictSize, opts); +} + +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + + uint32_t seed = FUZZ_seed(&data, &size); + size_t const dstCapacity = FUZZ_rand32(&seed, 0, 4 * size); + size_t const largeDictSize = 64 * 1024; + size_t const dictSize = FUZZ_rand32(&seed, 0, largeDictSize); + char* const dst = (char*)malloc(dstCapacity); + char* const dict = (char*)malloc(dictSize); + LZ4F_decompressOptions_t opts; + LZ4F_dctx* dctx; + LZ4F_createDecompressionContext(&dctx, LZ4F_VERSION); + + FUZZ_ASSERT(dctx); + FUZZ_ASSERT(dst); + FUZZ_ASSERT(dict); + + /* Prepare the dictionary. The data doesn't matter for decompression. */ + memset(dict, 0, dictSize); + + + /* Decompress using multiple configurations. */ + memset(&opts, 0, sizeof(opts)); + opts.stableDst = 0; + decompress(dctx, dst, dstCapacity, data, size, NULL, 0, &opts); + opts.stableDst = 1; + decompress(dctx, dst, dstCapacity, data, size, NULL, 0, &opts); + opts.stableDst = 0; + decompress(dctx, dst, dstCapacity, data, size, dict, dictSize, &opts); + opts.stableDst = 1; + decompress(dctx, dst, dstCapacity, data, size, dict, dictSize, &opts); + + LZ4F_freeDecompressionContext(dctx); + free(dst); + free(dict); + + return 0; +} diff --git a/ossfuzz/lz4_helpers.c b/ossfuzz/lz4_helpers.c new file mode 100644 index 0000000..9471630 --- /dev/null +++ b/ossfuzz/lz4_helpers.c @@ -0,0 +1,51 @@ +#include "fuzz_helpers.h" +#include "lz4_helpers.h" +#include "lz4hc.h" + +LZ4F_frameInfo_t FUZZ_randomFrameInfo(uint32_t* seed) +{ + LZ4F_frameInfo_t info = LZ4F_INIT_FRAMEINFO; + info.blockSizeID = FUZZ_rand32(seed, LZ4F_max64KB - 1, LZ4F_max4MB); + if (info.blockSizeID < LZ4F_max64KB) { + info.blockSizeID = LZ4F_default; + } + info.blockMode = FUZZ_rand32(seed, LZ4F_blockLinked, LZ4F_blockIndependent); + info.contentChecksumFlag = FUZZ_rand32(seed, LZ4F_noContentChecksum, + LZ4F_contentChecksumEnabled); + info.blockChecksumFlag = FUZZ_rand32(seed, LZ4F_noBlockChecksum, + LZ4F_blockChecksumEnabled); + return info; +} + +LZ4F_preferences_t FUZZ_randomPreferences(uint32_t* seed) +{ + LZ4F_preferences_t prefs = LZ4F_INIT_PREFERENCES; + prefs.frameInfo = FUZZ_randomFrameInfo(seed); + prefs.compressionLevel = FUZZ_rand32(seed, 0, LZ4HC_CLEVEL_MAX + 3) - 3; + prefs.autoFlush = FUZZ_rand32(seed, 0, 1); + prefs.favorDecSpeed = FUZZ_rand32(seed, 0, 1); + return prefs; +} + +size_t FUZZ_decompressFrame(void* dst, const size_t dstCapacity, + const void* src, const size_t srcSize) +{ + LZ4F_decompressOptions_t opts; + memset(&opts, 0, sizeof(opts)); + opts.stableDst = 1; + LZ4F_dctx* dctx; + LZ4F_createDecompressionContext(&dctx, LZ4F_VERSION); + FUZZ_ASSERT(dctx); + + size_t dstSize = dstCapacity; + size_t srcConsumed = srcSize; + size_t const rc = + LZ4F_decompress(dctx, dst, &dstSize, src, &srcConsumed, &opts); + FUZZ_ASSERT(!LZ4F_isError(rc)); + FUZZ_ASSERT(rc == 0); + FUZZ_ASSERT(srcConsumed == srcSize); + + LZ4F_freeDecompressionContext(dctx); + + return dstSize; +} diff --git a/ossfuzz/lz4_helpers.h b/ossfuzz/lz4_helpers.h new file mode 100644 index 0000000..c99fb01 --- /dev/null +++ b/ossfuzz/lz4_helpers.h @@ -0,0 +1,13 @@ +#ifndef LZ4_HELPERS +#define LZ4_HELPERS + +#include "lz4frame.h" + +LZ4F_frameInfo_t FUZZ_randomFrameInfo(uint32_t* seed); + +LZ4F_preferences_t FUZZ_randomPreferences(uint32_t* seed); + +size_t FUZZ_decompressFrame(void* dst, const size_t dstCapacity, + const void* src, const size_t srcSize); + +#endif /* LZ4_HELPERS */ diff --git a/ossfuzz/round_trip_frame_fuzzer.c b/ossfuzz/round_trip_frame_fuzzer.c new file mode 100644 index 0000000..1eea90c --- /dev/null +++ b/ossfuzz/round_trip_frame_fuzzer.c @@ -0,0 +1,39 @@ +/** + * This fuzz target performs a lz4 round-trip test (compress & decompress), + * compares the result with the original, and calls abort() on corruption. + */ + +#include +#include +#include +#include + +#include "fuzz_helpers.h" +#include "lz4.h" +#include "lz4frame.h" +#include "lz4_helpers.h" + +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + uint32_t seed = FUZZ_seed(&data, &size); + LZ4F_preferences_t const prefs = FUZZ_randomPreferences(&seed); + size_t const dstCapacity = LZ4F_compressFrameBound(size, &prefs); + char* const dst = (char*)malloc(dstCapacity); + char* const rt = (char*)malloc(size); + + FUZZ_ASSERT(dst); + FUZZ_ASSERT(rt); + + /* Compression must succeed and round trip correctly. */ + size_t const dstSize = + LZ4F_compressFrame(dst, dstCapacity, data, size, &prefs); + FUZZ_ASSERT(!LZ4F_isError(dstSize)); + size_t const rtSize = FUZZ_decompressFrame(rt, size, dst, dstSize); + FUZZ_ASSERT_MSG(rtSize == size, "Incorrect regenerated size"); + FUZZ_ASSERT_MSG(!memcmp(data, rt, size), "Corruption!"); + + free(dst); + free(rt); + + return 0; +} -- cgit v1.2.3 From 87e52f7d5d8e2e44c107ca266d91a5ff669325b1 Mon Sep 17 00:00:00 2001 From: Nick Terrell Date: Fri, 19 Jul 2019 14:44:06 -0700 Subject: [lz4frame] Fix unused variable warnings in fuzzing mode --- lib/lz4frame.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/lz4frame.c b/lib/lz4frame.c index 13c1ae9..c9f630d 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -1564,6 +1564,9 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION if (readCRC != calcCRC) return err0r(LZ4F_ERROR_blockChecksum_invalid); +#else + (void)readCRC; + (void)calcCRC; #endif } } dctx->dStage = dstage_getBlockHeader; /* new block */ @@ -1606,6 +1609,9 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION if (readBlockCrc != calcBlockCrc) return err0r(LZ4F_ERROR_blockChecksum_invalid); +#else + (void)readBlockCrc; + (void)calcBlockCrc; #endif } } @@ -1737,6 +1743,9 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION if (readCRC != resultCRC) return err0r(LZ4F_ERROR_contentChecksum_invalid); +#else + (void)readCRC; + (void)resultCRC; #endif nextSrcSizeHint = 0; LZ4F_resetDecompressionContext(dctx); -- cgit v1.2.3 From eee8cc79e79230a884884beb885ea92fe1b70928 Mon Sep 17 00:00:00 2001 From: WHR Date: Tue, 23 Jul 2019 13:37:11 +0800 Subject: lz4cli: add option '--best' as an alias of '-12' --- programs/lz4.1 | 4 ++++ programs/lz4.1.md | 3 +++ programs/lz4cli.c | 4 ++++ 3 files changed, 11 insertions(+) diff --git a/programs/lz4.1 b/programs/lz4.1 index 3ca017a..d758ed5 100644 --- a/programs/lz4.1 +++ b/programs/lz4.1 @@ -120,6 +120,10 @@ Compression level, with # being any value from 1 to 12\. Higher values trade com Switch to ultra\-fast compression levels\. The higher the value, the faster the compression speed, at the cost of some compression ratio\. If \fB=#\fR is not present, it defaults to \fB1\fR\. This setting overrides compression level if one was set previously\. Similarly, if a compression level is set after \fB\-\-fast\fR, it overrides it\. . .TP +\fB\-\-best\fR +Set highest compression level\. Same as -12\. +. +.TP \fB\-\-favor\-decSpeed\fR Generate compressed data optimized for decompression speed\. Compressed data will be larger as a consequence (typically by ~0\.5%), while decompression speed will be improved by 5\-20%, depending on use cases\. This option only works in combination with very high compression levels (>=10)\. . diff --git a/programs/lz4.1.md b/programs/lz4.1.md index 8874467..56c0053 100644 --- a/programs/lz4.1.md +++ b/programs/lz4.1.md @@ -135,6 +135,9 @@ only the latest one will be applied. This setting overrides compression level if one was set previously. Similarly, if a compression level is set after `--fast`, it overrides it. +* `--best`: + Set highest compression level. Same as -12. + * `--favor-decSpeed`: Generate compressed data optimized for decompression speed. Compressed data will be larger as a consequence (typically by ~0.5%), diff --git a/programs/lz4cli.c b/programs/lz4cli.c index 3b5d61e..5da7654 100644 --- a/programs/lz4cli.c +++ b/programs/lz4cli.c @@ -146,6 +146,7 @@ static int usage_advanced(const char* exeName) DISPLAY( "--[no-]sparse : sparse mode (default:enabled on file, disabled on stdout)\n"); DISPLAY( "--favor-decSpeed: compressed files decompress faster, but are less compressed \n"); DISPLAY( "--fast[=#]: switch to ultra fast compression level (default: %i)\n", 1); + DISPLAY( "--best : same as -%d\n", LZ4HC_CLEVEL_MAX); DISPLAY( "Benchmark arguments : \n"); DISPLAY( " -b# : benchmark file(s), using # compression level (default : 1) \n"); DISPLAY( " -e# : test all compression levels from -bX to # (default : 1)\n"); @@ -414,6 +415,9 @@ int main(int argc, const char** argv) } continue; } + + /* For gzip(1) compatibility */ + if (!strcmp(argument, "--best")) { cLevel=LZ4HC_CLEVEL_MAX; continue; } } while (argument[1]!=0) { -- cgit v1.2.3 From 4c1d4c437daddc36baf0f4616f2392f67ac320d4 Mon Sep 17 00:00:00 2001 From: Nick Terrell Date: Tue, 23 Jul 2019 17:54:09 -0700 Subject: [LZ4HC] Speed up pattern compression with external dictionary Fixes #761. --- lib/lz4hc.c | 74 +++++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 47 insertions(+), 27 deletions(-) diff --git a/lib/lz4hc.c b/lib/lz4hc.c index 29288a5..b8ff72a 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -151,6 +151,19 @@ int LZ4HC_countBack(const BYTE* const ip, const BYTE* const match, return back; } +#if defined(_MSC_VER) +# define LZ4HC_rotl32(x,r) _rotl(x,r) +#else +# define LZ4HC_rotl32(x,r) ((x << r) | (x >> (32 - r))) +#endif + + +static U32 LZ4HC_rotatePattern(size_t const length, U32 const pattern) +{ + size_t const bitsToRotate = (length & (sizeof(pattern) - 1)) << 3; + return LZ4HC_rotl32(pattern, (int)bitsToRotate); +} + /* LZ4HC_countPattern() : * pattern32 must be a sample of repetitive pattern of length 1, 2 or 4 (but not 3!) */ static unsigned @@ -313,34 +326,41 @@ LZ4HC_InsertAndGetWiderMatch ( } else { repeat = rep_not; } } - if ( (repeat == rep_confirmed) - && (matchCandidateIdx >= dictLimit) ) { /* same segment only */ - const BYTE* const matchPtr = base + matchCandidateIdx; + if ( (repeat == rep_confirmed) && (matchCandidateIdx >= lowestMatchIndex) ) { + const int extDict = matchCandidateIdx < dictLimit; + const BYTE* const matchPtr = (extDict ? dictBase : base) + matchCandidateIdx; if (LZ4_read32(matchPtr) == pattern) { /* good candidate */ - size_t const forwardPatternLength = LZ4HC_countPattern(matchPtr+sizeof(pattern), iHighLimit, pattern) + sizeof(pattern); - const BYTE* const lowestMatchPtr = (lowPrefixPtr + LZ4_DISTANCE_MAX >= ip) ? lowPrefixPtr : ip - LZ4_DISTANCE_MAX; - size_t const backLength = LZ4HC_reverseCountPattern(matchPtr, lowestMatchPtr, pattern); - size_t const currentSegmentLength = backLength + forwardPatternLength; - - if ( (currentSegmentLength >= srcPatternLength) /* current pattern segment large enough to contain full srcPatternLength */ - && (forwardPatternLength <= srcPatternLength) ) { /* haven't reached this position yet */ - matchIndex = matchCandidateIdx + (U32)forwardPatternLength - (U32)srcPatternLength; /* best position, full pattern, might be followed by more match */ - } else { - matchIndex = matchCandidateIdx - (U32)backLength; /* farthest position in current segment, will find a match of length currentSegmentLength + maybe some back */ - if (lookBackLength==0) { /* no back possible */ - size_t const maxML = MIN(currentSegmentLength, srcPatternLength); - if ((size_t)longest < maxML) { - assert(base + matchIndex < ip); - if (ip - (base+matchIndex) > LZ4_DISTANCE_MAX) break; - assert(maxML < 2 GB); - longest = (int)maxML; - *matchpos = base + matchIndex; /* virtual pos, relative to ip, to retrieve offset */ - *startpos = ip; - } - { U32 const distToNextPattern = DELTANEXTU16(chainTable, matchIndex); - if (distToNextPattern > matchIndex) break; /* avoid overflow */ - matchIndex -= distToNextPattern; - } } } + const BYTE* const dictStart = dictBase + hc4->lowLimit; + const BYTE* const iLimit = extDict ? dictBase + dictLimit : iHighLimit; + size_t forwardPatternLength = LZ4HC_countPattern(matchPtr+sizeof(pattern), iLimit, pattern) + sizeof(pattern); + if (extDict && ip + forwardPatternLength == iLimit) { + U32 const rotatedPattern = LZ4HC_rotatePattern(forwardPatternLength, pattern); + forwardPatternLength += LZ4HC_countPattern(lowPrefixPtr, iHighLimit, rotatedPattern); + } + { const BYTE* const lowestMatchPtr = extDict ? dictStart : lowPrefixPtr; + size_t const backLengthRaw = LZ4HC_reverseCountPattern(matchPtr, lowestMatchPtr, pattern); + size_t const backLength = matchCandidateIdx - MAX(matchCandidateIdx - (U32)backLengthRaw, lowestMatchIndex); + size_t const currentSegmentLength = backLength + forwardPatternLength; + + if ( (currentSegmentLength >= srcPatternLength) /* current pattern segment large enough to contain full srcPatternLength */ + && (forwardPatternLength <= srcPatternLength) ) { /* haven't reached this position yet */ + matchIndex = matchCandidateIdx + (U32)forwardPatternLength - (U32)srcPatternLength; /* best position, full pattern, might be followed by more match */ + } else { + matchIndex = matchCandidateIdx - (U32)backLength; /* farthest position in current segment, will find a match of length currentSegmentLength + maybe some back */ + if (lookBackLength==0) { /* no back possible */ + size_t const maxML = MIN(currentSegmentLength, srcPatternLength); + if ((size_t)longest < maxML) { + assert(base + matchIndex < ip); + if (ip - (base+matchIndex) > LZ4_DISTANCE_MAX) break; + assert(maxML < 2 GB); + longest = (int)maxML; + *matchpos = base + matchIndex; /* virtual pos, relative to ip, to retrieve offset */ + *startpos = ip; + } + { U32 const distToNextPattern = DELTANEXTU16(chainTable, matchIndex); + if (distToNextPattern > matchIndex) break; /* avoid overflow */ + matchIndex -= distToNextPattern; + } } } } continue; } } } } /* PA optimization */ -- cgit v1.2.3 From 7e97bf377d3c3a6d0d71a3ce9116e1f0143a8740 Mon Sep 17 00:00:00 2001 From: Nick Terrell Date: Tue, 30 Jul 2019 15:16:35 -0700 Subject: [lz4hc] Improve pattern detection in ext dict It is important to continue to look backwards if the current pattern reaches `lowPrefixPtr`. If the pattern detection doesn't go all the way to the beginning of the pattern, or the end of the pattern it slows down the search instead of speeding it up. The slow unit in `round_trip_stream_fuzzer` used to take 12 seconds to run with -O3, now it takes 0.2 seconds. Credit to OSS-Fuzz --- lib/lz4hc.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/lz4hc.c b/lib/lz4hc.c index b8ff72a..4122af4 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -158,9 +158,11 @@ int LZ4HC_countBack(const BYTE* const ip, const BYTE* const match, #endif -static U32 LZ4HC_rotatePattern(size_t const length, U32 const pattern) +static U32 LZ4HC_rotatePattern(size_t const rotate, U32 const pattern) { - size_t const bitsToRotate = (length & (sizeof(pattern) - 1)) << 3; + size_t const bitsToRotate = (rotate & (sizeof(pattern) - 1)) << 3; + if (bitsToRotate == 0) + return pattern; return LZ4HC_rotl32(pattern, (int)bitsToRotate); } @@ -338,9 +340,15 @@ LZ4HC_InsertAndGetWiderMatch ( forwardPatternLength += LZ4HC_countPattern(lowPrefixPtr, iHighLimit, rotatedPattern); } { const BYTE* const lowestMatchPtr = extDict ? dictStart : lowPrefixPtr; - size_t const backLengthRaw = LZ4HC_reverseCountPattern(matchPtr, lowestMatchPtr, pattern); - size_t const backLength = matchCandidateIdx - MAX(matchCandidateIdx - (U32)backLengthRaw, lowestMatchIndex); - size_t const currentSegmentLength = backLength + forwardPatternLength; + size_t backLength = LZ4HC_reverseCountPattern(matchPtr, lowestMatchPtr, pattern); + size_t currentSegmentLength; + if (!extDict && matchPtr - backLength == lowPrefixPtr && hc4->lowLimit < dictLimit) { + U32 const rotatedPattern = LZ4HC_rotatePattern((U32)(-(int)backLength), pattern); + backLength += LZ4HC_reverseCountPattern(dictBase + dictLimit, dictStart, rotatedPattern); + } + /* Limit backLength not go further than lowestMatchIndex */ + backLength = matchCandidateIdx - MAX(matchCandidateIdx - (U32)backLength, lowestMatchIndex); + currentSegmentLength = backLength + forwardPatternLength; if ( (currentSegmentLength >= srcPatternLength) /* current pattern segment large enough to contain full srcPatternLength */ && (forwardPatternLength <= srcPatternLength) ) { /* haven't reached this position yet */ -- cgit v1.2.3 From 58ea585878008db36fa20025703757f0b70836eb Mon Sep 17 00:00:00 2001 From: Nick Terrell Date: Tue, 30 Jul 2019 15:21:52 -0700 Subject: [lz4hc] Fix minor pessimization in extDict pattern matching We should be comparing `matchPtr` not `ip`. This bug just means that this branch was not taken, so we might miss some of the forward length. --- lib/lz4hc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/lz4hc.c b/lib/lz4hc.c index 4122af4..a0b8c48 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -335,7 +335,7 @@ LZ4HC_InsertAndGetWiderMatch ( const BYTE* const dictStart = dictBase + hc4->lowLimit; const BYTE* const iLimit = extDict ? dictBase + dictLimit : iHighLimit; size_t forwardPatternLength = LZ4HC_countPattern(matchPtr+sizeof(pattern), iLimit, pattern) + sizeof(pattern); - if (extDict && ip + forwardPatternLength == iLimit) { + if (extDict && matchPtr + forwardPatternLength == iLimit) { U32 const rotatedPattern = LZ4HC_rotatePattern(forwardPatternLength, pattern); forwardPatternLength += LZ4HC_countPattern(lowPrefixPtr, iHighLimit, rotatedPattern); } -- cgit v1.2.3 From be1738aa46326e86e9c3bb1029abaadce45b8e72 Mon Sep 17 00:00:00 2001 From: Nick Terrell Date: Tue, 30 Jul 2019 23:39:39 -0700 Subject: [lz4hc] Fix pattern detection end of dictionary The pattern detection in extDict mode could put `matchIndex` within the last 3 bytes of the dictionary. This would cause a read out of bounds. --- lib/lz4hc.c | 65 ++++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 45 insertions(+), 20 deletions(-) diff --git a/lib/lz4hc.c b/lib/lz4hc.c index a0b8c48..596888a 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -218,6 +218,16 @@ LZ4HC_reverseCountPattern(const BYTE* ip, const BYTE* const iLow, U32 pattern) return (unsigned)(iStart - ip); } +/* LZ4HC_protectDictEnd() : + * Checks if the match is in the last 3 bytes of the dictionary, so reading the + * 4 byte MINMATCH would overflow. + * @returns true if the match index is okay. + */ +static int LZ4HC_protectDictEnd(U32 const dictLimit, U32 const matchIndex) +{ + return ((U32)((dictLimit - 1) - matchIndex) >= 3); +} + typedef enum { rep_untested, rep_not, rep_confirmed } repeat_state_e; typedef enum { favorCompressionRatio=0, favorDecompressionSpeed } HCfavor_e; @@ -304,7 +314,7 @@ LZ4HC_InsertAndGetWiderMatch ( if (matchIndex + (U32)longest <= ipIndex) { U32 distanceToNextMatch = 1; int pos; - for (pos = 0; pos <= longest - MINMATCH; pos++) { + for (pos = 0; pos <= longest - MINMATCH; ++pos) { U32 const candidateDist = DELTANEXTU16(chainTable, matchIndex + (U32)pos); if (candidateDist > distanceToNextMatch) { distanceToNextMatch = candidateDist; @@ -314,7 +324,8 @@ LZ4HC_InsertAndGetWiderMatch ( if (distanceToNextMatch > matchIndex) break; /* avoid overflow */ matchIndex -= distanceToNextMatch; continue; - } } } + } + } } { U32 const distNextMatch = DELTANEXTU16(chainTable, matchIndex); if (patternAnalysis && distNextMatch==1 && matchChainPos==0) { @@ -328,7 +339,8 @@ LZ4HC_InsertAndGetWiderMatch ( } else { repeat = rep_not; } } - if ( (repeat == rep_confirmed) && (matchCandidateIdx >= lowestMatchIndex) ) { + if ( (repeat == rep_confirmed) && (matchCandidateIdx >= lowestMatchIndex) + && LZ4HC_protectDictEnd(dictLimit, matchCandidateIdx) ) { const int extDict = matchCandidateIdx < dictLimit; const BYTE* const matchPtr = (extDict ? dictBase : base) + matchCandidateIdx; if (LZ4_read32(matchPtr) == pattern) { /* good candidate */ @@ -348,27 +360,40 @@ LZ4HC_InsertAndGetWiderMatch ( } /* Limit backLength not go further than lowestMatchIndex */ backLength = matchCandidateIdx - MAX(matchCandidateIdx - (U32)backLength, lowestMatchIndex); + assert(matchCandidateIdx - backLength >= lowestMatchIndex); currentSegmentLength = backLength + forwardPatternLength; - + /* Adjust to end of pattern if the source pattern fits, otherwise the beginning of the pattern */ if ( (currentSegmentLength >= srcPatternLength) /* current pattern segment large enough to contain full srcPatternLength */ && (forwardPatternLength <= srcPatternLength) ) { /* haven't reached this position yet */ - matchIndex = matchCandidateIdx + (U32)forwardPatternLength - (U32)srcPatternLength; /* best position, full pattern, might be followed by more match */ + U32 const newMatchIndex = matchCandidateIdx + (U32)forwardPatternLength - (U32)srcPatternLength; /* best position, full pattern, might be followed by more match */ + if (LZ4HC_protectDictEnd(dictLimit, newMatchIndex)) + matchIndex = newMatchIndex; + else { + /* Can only happen if started in the prefix */ + assert(newMatchIndex >= dictLimit - 3 && newMatchIndex < dictLimit && !extDict); + matchIndex = dictLimit; + } } else { - matchIndex = matchCandidateIdx - (U32)backLength; /* farthest position in current segment, will find a match of length currentSegmentLength + maybe some back */ - if (lookBackLength==0) { /* no back possible */ - size_t const maxML = MIN(currentSegmentLength, srcPatternLength); - if ((size_t)longest < maxML) { - assert(base + matchIndex < ip); - if (ip - (base+matchIndex) > LZ4_DISTANCE_MAX) break; - assert(maxML < 2 GB); - longest = (int)maxML; - *matchpos = base + matchIndex; /* virtual pos, relative to ip, to retrieve offset */ - *startpos = ip; - } - { U32 const distToNextPattern = DELTANEXTU16(chainTable, matchIndex); - if (distToNextPattern > matchIndex) break; /* avoid overflow */ - matchIndex -= distToNextPattern; - } } } } + U32 const newMatchIndex = matchCandidateIdx - (U32)backLength; /* farthest position in current segment, will find a match of length currentSegmentLength + maybe some back */ + if (!LZ4HC_protectDictEnd(dictLimit, newMatchIndex)) { + assert(newMatchIndex >= dictLimit - 3 && newMatchIndex < dictLimit && !extDict); + matchIndex = dictLimit; + } else { + matchIndex = newMatchIndex; + if (lookBackLength==0) { /* no back possible */ + size_t const maxML = MIN(currentSegmentLength, srcPatternLength); + if ((size_t)longest < maxML) { + assert(base + matchIndex < ip); + if (ip - (base+matchIndex) > LZ4_DISTANCE_MAX) break; + assert(maxML < 2 GB); + longest = (int)maxML; + *matchpos = base + matchIndex; /* virtual pos, relative to ip, to retrieve offset */ + *startpos = ip; + } + { U32 const distToNextPattern = DELTANEXTU16(chainTable, matchIndex); + if (distToNextPattern > matchIndex) break; /* avoid overflow */ + matchIndex -= distToNextPattern; + } } } } } continue; } } } } /* PA optimization */ -- cgit v1.2.3 From 38c3945de300851757d0dd76182ee28aaf8253a4 Mon Sep 17 00:00:00 2001 From: Nick Terrell Date: Tue, 30 Jul 2019 23:40:58 -0700 Subject: [lz4hc] Only allow chain swapping forwards When the match is very long and found quickly, we can do matchLength * nbCompares iterations through the chain swapping, which can really slow down compression. --- lib/lz4hc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/lz4hc.c b/lib/lz4hc.c index 596888a..0608ec6 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -314,7 +314,7 @@ LZ4HC_InsertAndGetWiderMatch ( if (matchIndex + (U32)longest <= ipIndex) { U32 distanceToNextMatch = 1; int pos; - for (pos = 0; pos <= longest - MINMATCH; ++pos) { + for (pos = matchChainPos; pos <= longest - MINMATCH; ++pos) { U32 const candidateDist = DELTANEXTU16(chainTable, matchIndex + (U32)pos); if (candidateDist > distanceToNextMatch) { distanceToNextMatch = candidateDist; -- cgit v1.2.3 From 064adb2e8d95698168d436afc223e8ba44e56831 Mon Sep 17 00:00:00 2001 From: Nick Terrell Date: Wed, 31 Jul 2019 00:57:16 -0700 Subject: [lz4hc] Chain swap with acceleration --- lib/lz4hc.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/lz4hc.c b/lib/lz4hc.c index 0608ec6..5922ed7 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -312,20 +312,26 @@ LZ4HC_InsertAndGetWiderMatch ( if (chainSwap && matchLength==longest) { /* better match => select a better chain */ assert(lookBackLength==0); /* search forward only */ if (matchIndex + (U32)longest <= ipIndex) { + int const kTrigger = 4; U32 distanceToNextMatch = 1; + int const end = longest - MINMATCH + 1; + int step = 1; + int accel = 1 << kTrigger; int pos; - for (pos = matchChainPos; pos <= longest - MINMATCH; ++pos) { + for (pos = 0; pos < end; pos += step) { U32 const candidateDist = DELTANEXTU16(chainTable, matchIndex + (U32)pos); + step = (accel++ >> kTrigger); if (candidateDist > distanceToNextMatch) { distanceToNextMatch = candidateDist; matchChainPos = (U32)pos; - } } + accel = 1 << kTrigger; + } + } if (distanceToNextMatch > 1) { if (distanceToNextMatch > matchIndex) break; /* avoid overflow */ matchIndex -= distanceToNextMatch; continue; - } - } } + } } } { U32 const distNextMatch = DELTANEXTU16(chainTable, matchIndex); if (patternAnalysis && distNextMatch==1 && matchChainPos==0) { -- cgit v1.2.3 From e18fbd51c1529db7c78616a64a97ac23aa57c2fc Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 6 Aug 2019 14:46:31 +0200 Subject: silence msan warning when offset==0 --- lib/lz4.c | 129 ++++++++++++++++++++++++++++--------------------------- tests/.gitignore | 1 + 2 files changed, 66 insertions(+), 64 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index c8f49cc..0849505 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -183,6 +183,60 @@ #define MEM_INIT(p,v,s) memset((p),(v),(s)) +/*-************************************ +* Common Constants +**************************************/ +#define MINMATCH 4 + +#define WILDCOPYLENGTH 8 +#define LASTLITERALS 5 /* see ../doc/lz4_Block_format.md#parsing-restrictions */ +#define MFLIMIT 12 /* see ../doc/lz4_Block_format.md#parsing-restrictions */ +#define MATCH_SAFEGUARD_DISTANCE ((2*WILDCOPYLENGTH) - MINMATCH) /* ensure it's possible to write 2 x wildcopyLength without overflowing output buffer */ +#define FASTLOOP_SAFE_DISTANCE 64 +static const int LZ4_minLength = (MFLIMIT+1); + +#define KB *(1 <<10) +#define MB *(1 <<20) +#define GB *(1U<<30) + +#define LZ4_DISTANCE_ABSOLUTE_MAX 65535 +#if (LZ4_DISTANCE_MAX > LZ4_DISTANCE_ABSOLUTE_MAX) /* max supported by LZ4 format */ +# error "LZ4_DISTANCE_MAX is too big : must be <= 65535" +#endif + +#define ML_BITS 4 +#define ML_MASK ((1U<=1) +# include +#else +# ifndef assert +# define assert(condition) ((void)0) +# endif +#endif + +#define LZ4_STATIC_ASSERT(c) { enum { LZ4_static_assert = 1/(int)(!!(c)) }; } /* use after variable declarations */ + +#if defined(LZ4_DEBUG) && (LZ4_DEBUG>=2) +# include +static int g_debuglog_enable = 1; +# define DEBUGLOG(l, ...) { \ + if ((g_debuglog_enable) && (l<=LZ4_DEBUG)) { \ + fprintf(stderr, __FILE__ ": "); \ + fprintf(stderr, __VA_ARGS__); \ + fprintf(stderr, " \n"); \ + } } +#else +# define DEBUGLOG(l, ...) {} /* disabled */ +#endif + + /*-************************************ * Types **************************************/ @@ -364,29 +418,35 @@ LZ4_wildCopy32(void* dstPtr, const void* srcPtr, void* dstEnd) do { memcpy(d,s,16); memcpy(d+16,s+16,16); d+=32; s+=32; } while (d= dstPtr + MINMATCH + * - there is at least 8 bytes available to write after dstEnd */ LZ4_FORCE_O2_INLINE_GCC_PPC64LE void LZ4_memcpy_using_offset(BYTE* dstPtr, const BYTE* srcPtr, BYTE* dstEnd, const size_t offset) { BYTE v[8]; + + assert(dstEnd >= dstPtr + MINMATCH); + LZ4_write32(dstPtr, 0); /* silence an msan warning when offset==0 */ + switch(offset) { case 1: memset(v, *srcPtr, 8); - goto copy_loop; + break; case 2: memcpy(v, srcPtr, 2); memcpy(&v[2], srcPtr, 2); memcpy(&v[4], &v[0], 4); - goto copy_loop; + break; case 4: memcpy(v, srcPtr, 4); memcpy(&v[4], srcPtr, 4); - goto copy_loop; + break; default: LZ4_memcpy_using_offset_base(dstPtr, srcPtr, dstEnd, offset); return; } - copy_loop: memcpy(dstPtr, v, 8); dstPtr += 8; while (dstPtr < dstEnd) { @@ -397,60 +457,6 @@ LZ4_memcpy_using_offset(BYTE* dstPtr, const BYTE* srcPtr, BYTE* dstEnd, const si #endif -/*-************************************ -* Common Constants -**************************************/ -#define MINMATCH 4 - -#define WILDCOPYLENGTH 8 -#define LASTLITERALS 5 /* see ../doc/lz4_Block_format.md#parsing-restrictions */ -#define MFLIMIT 12 /* see ../doc/lz4_Block_format.md#parsing-restrictions */ -#define MATCH_SAFEGUARD_DISTANCE ((2*WILDCOPYLENGTH) - MINMATCH) /* ensure it's possible to write 2 x wildcopyLength without overflowing output buffer */ -#define FASTLOOP_SAFE_DISTANCE 64 -static const int LZ4_minLength = (MFLIMIT+1); - -#define KB *(1 <<10) -#define MB *(1 <<20) -#define GB *(1U<<30) - -#define LZ4_DISTANCE_ABSOLUTE_MAX 65535 -#if (LZ4_DISTANCE_MAX > LZ4_DISTANCE_ABSOLUTE_MAX) /* max supported by LZ4 format */ -# error "LZ4_DISTANCE_MAX is too big : must be <= 65535" -#endif - -#define ML_BITS 4 -#define ML_MASK ((1U<=1) -# include -#else -# ifndef assert -# define assert(condition) ((void)0) -# endif -#endif - -#define LZ4_STATIC_ASSERT(c) { enum { LZ4_static_assert = 1/(int)(!!(c)) }; } /* use after variable declarations */ - -#if defined(LZ4_DEBUG) && (LZ4_DEBUG>=2) -# include -static int g_debuglog_enable = 1; -# define DEBUGLOG(l, ...) { \ - if ((g_debuglog_enable) && (l<=LZ4_DEBUG)) { \ - fprintf(stderr, __FILE__ ": "); \ - fprintf(stderr, __VA_ARGS__); \ - fprintf(stderr, " \n"); \ - } } -#else -# define DEBUGLOG(l, ...) {} /* disabled */ -#endif - - /*-************************************ * Common functions **************************************/ @@ -1946,12 +1952,6 @@ LZ4_decompress_generic( length = token & ML_MASK; _copy_match: - if (!partialDecoding) { - assert(oend > op); - assert(oend - op >= 4); - LZ4_write32(op, 0); /* silence an msan warning when offset==0; costs <1%; */ - } /* note : when partialDecoding, there is no guarantee that at least 4 bytes remain available in output buffer */ - if (length == ML_MASK) { variable_length_error error = ok; length += read_variable_length(&ip, iend - LASTLITERALS + 1, endOnInput, 0, &error); @@ -2013,6 +2013,7 @@ LZ4_decompress_generic( } if (unlikely(offset<8)) { + LZ4_write32(op, 0); /* silence msan warning when offset==0 */ op[0] = match[0]; op[1] = match[1]; op[2] = match[2]; diff --git a/tests/.gitignore b/tests/.gitignore index c4f9092..0d13df8 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -15,6 +15,7 @@ checkFrame # test artefacts tmp* versionsTest +lz4_all.c # local tests afl -- cgit v1.2.3 From 918269a4e395d88364a517b889473922ac5521ec Mon Sep 17 00:00:00 2001 From: "W. Felix Handte" Date: Tue, 6 Aug 2019 15:24:51 -0400 Subject: Make Attaching an Empty Dict Behave the Same as Using it Directly When using an empty dictionary, we bail out of loading or attaching it in ways that leave the working context in potentially slightly different states. In particular, in some paths, we will cause the currentOffset to be non-zero, while in others we would allow it to remain 0. This difference in behavior is perfectly harmless, but in some situations, it can produce slight differences in the compressed output. For sanity's sake, we currently try to maintain a strict correspondence between the behavior of the dict attachment and the dict loading paths. This patch restores them to behaving identically. This shouldn't have any negative side-effects, as far as I can tell. When writing the dict attachment code, I tried to preserve zeroed currentOffsets when possible, since they benchmarked as very slightly faster. However, the case of attaching an empty dictionary is probably rare enought that it's acceptable to minisculely degrade performance in that corner case. --- lib/lz4.c | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 0849505..147a8d6 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1408,18 +1408,18 @@ int LZ4_loadDict (LZ4_stream_t* LZ4_dict, const char* dictionary, int dictSize) * there are only valid offsets in the window, which allows an optimization * in LZ4_compress_fast_continue() where it uses noDictIssue even when the * dictionary isn't a full 64k. */ - - if ((dictEnd - p) > 64 KB) p = dictEnd - 64 KB; - base = dictEnd - 64 KB - dict->currentOffset; - dict->dictionary = p; - dict->dictSize = (U32)(dictEnd - p); dict->currentOffset += 64 KB; - dict->tableType = tableType; if (dictSize < (int)HASH_UNIT) { return 0; } + if ((dictEnd - p) > 64 KB) p = dictEnd - 64 KB; + base = dictEnd - dict->currentOffset; + dict->dictionary = p; + dict->dictSize = (U32)(dictEnd - p); + dict->tableType = tableType; + while (p <= dictEnd-HASH_UNIT) { LZ4_putPosition(p, dict->hashTable, tableType, base); p+=3; @@ -1435,15 +1435,16 @@ void LZ4_attach_dictionary(LZ4_stream_t* workingStream, const LZ4_stream_t* dict */ LZ4_resetStream_fast(workingStream); + /* If the current offset is zero, we will never look in the + * external dictionary context, since there is no value a table + * entry can take that indicate a miss. In that case, we need + * to bump the offset to something non-zero. + */ + if (workingStream->internal_donotuse.currentOffset == 0) { + workingStream->internal_donotuse.currentOffset = 64 KB; + } + if (dictionaryStream != NULL && dictionaryStream->internal_donotuse.dictSize > 0) { - /* If the current offset is zero, we will never look in the - * external dictionary context, since there is no value a table - * entry can take that indicate a miss. In that case, we need - * to bump the offset to something non-zero. - */ - if (workingStream->internal_donotuse.currentOffset == 0) { - workingStream->internal_donotuse.currentOffset = 64 KB; - } workingStream->internal_donotuse.dictCtx = &(dictionaryStream->internal_donotuse); } else { workingStream->internal_donotuse.dictCtx = NULL; -- cgit v1.2.3 From 4f49d744e8de26b4d942c0c0e480f6a37dde41a5 Mon Sep 17 00:00:00 2001 From: "W. Felix Handte" Date: Tue, 6 Aug 2019 18:54:03 -0400 Subject: Add Attach Dict Debug Log --- lib/lz4.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/lz4.c b/lib/lz4.c index 147a8d6..07739a7 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1429,6 +1429,10 @@ int LZ4_loadDict (LZ4_stream_t* LZ4_dict, const char* dictionary, int dictSize) } void LZ4_attach_dictionary(LZ4_stream_t* workingStream, const LZ4_stream_t* dictionaryStream) { + DEBUGLOG(4, "LZ4_attach_dictionary (%p, %p, size %d)", + workingStream, dictionaryStream, dictionaryStream != NULL ? + dictionaryStream->internal_donotuse.dictSize : 0); + /* Calling LZ4_resetStream_fast() here makes sure that changes will not be * erased by subsequent calls to LZ4_resetStream_fast() in case stream was * marked as having dirty context, e.g. requiring full reset. -- cgit v1.2.3 From 4c580067199c4a7fdcacc6961842ee6d3832bb93 Mon Sep 17 00:00:00 2001 From: "W. Felix Handte" Date: Tue, 6 Aug 2019 19:08:41 -0400 Subject: Only Bump Offset When Attaching Non-Null Dictionary We do want to bump, even if the dictionary is empty, but we **don't** want to bump if the dictionary is null. --- lib/lz4.c | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 07739a7..877d14e 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1429,9 +1429,12 @@ int LZ4_loadDict (LZ4_stream_t* LZ4_dict, const char* dictionary, int dictSize) } void LZ4_attach_dictionary(LZ4_stream_t* workingStream, const LZ4_stream_t* dictionaryStream) { - DEBUGLOG(4, "LZ4_attach_dictionary (%p, %p, size %d)", - workingStream, dictionaryStream, dictionaryStream != NULL ? - dictionaryStream->internal_donotuse.dictSize : 0); + const LZ4_stream_t_internal* dictCtx = dictionaryStream == NULL ? NULL : + &(dictionaryStream->internal_donotuse); + + DEBUGLOG(4, "LZ4_attach_dictionary (%p, %p, size %u)", + workingStream, dictionaryStream, + dictCtx != NULL ? dictCtx->dictSize : 0); /* Calling LZ4_resetStream_fast() here makes sure that changes will not be * erased by subsequent calls to LZ4_resetStream_fast() in case stream was @@ -1439,20 +1442,23 @@ void LZ4_attach_dictionary(LZ4_stream_t* workingStream, const LZ4_stream_t* dict */ LZ4_resetStream_fast(workingStream); - /* If the current offset is zero, we will never look in the - * external dictionary context, since there is no value a table - * entry can take that indicate a miss. In that case, we need - * to bump the offset to something non-zero. - */ - if (workingStream->internal_donotuse.currentOffset == 0) { - workingStream->internal_donotuse.currentOffset = 64 KB; - } + if (dictCtx != NULL) { + /* If the current offset is zero, we will never look in the + * external dictionary context, since there is no value a table + * entry can take that indicate a miss. In that case, we need + * to bump the offset to something non-zero. + */ + if (workingStream->internal_donotuse.currentOffset == 0) { + workingStream->internal_donotuse.currentOffset = 64 KB; + } - if (dictionaryStream != NULL && dictionaryStream->internal_donotuse.dictSize > 0) { - workingStream->internal_donotuse.dictCtx = &(dictionaryStream->internal_donotuse); - } else { - workingStream->internal_donotuse.dictCtx = NULL; + /* Don't actually attach an empty dictionary. + */ + if (dictCtx->dictSize == 0) { + dictCtx = NULL; + } } + workingStream->internal_donotuse.dictCtx = dictCtx; } -- cgit v1.2.3 From d7cad81093cd805110291f84d64d385557d0ffba Mon Sep 17 00:00:00 2001 From: Nick Terrell Date: Fri, 9 Aug 2019 10:32:26 -0700 Subject: [LZ4_compress_destSize] Fix off-by-one error PR#756 fixed the data corruption bug, but didn't clear `ip`. PR#760 fixed that off-by-one error, but missed the case where `ip == filledIp`, which is harder for the fuzzers to find (it took 20 days not 1 day). Verified this fixed the issue reported by OSS-Fuzz. Credit to OSS-Fuzz. --- lib/lz4.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/lz4.c b/lib/lz4.c index 877d14e..9808d70 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1040,7 +1040,7 @@ _next_match: ip -= matchCode - newMatchCode; assert(newMatchCode < matchCode); matchCode = newMatchCode; - if (unlikely(ip < filledIp)) { + if (unlikely(ip <= filledIp)) { /* We have already filled up to filledIp so if ip ends up less than filledIp * we have positions in the hash table beyond the current position. This is * a problem if we reuse the hash table. So we have to remove these positions -- cgit v1.2.3 From fdf2ef5809ca875c454510610764d9125ef2ebbd Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 15 Aug 2019 13:59:59 +0200 Subject: fixed test error could trigger %0 on exceptional circumstances due to wrong buffer size parameter. --- tests/frametest.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/frametest.c b/tests/frametest.c index 91813bb..1b932e4 100644 --- a/tests/frametest.c +++ b/tests/frametest.c @@ -1059,7 +1059,7 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi size_t const mask = (1< Date: Fri, 16 Aug 2019 10:50:46 -0700 Subject: Adding fuzz data producer for uint32 and using in decompress_fuzzer Summary: Consuming bytes from the end of data instead of from the front to prevent "all-in-one" decisions. Test Plan: Reviewers: Subscribers: Tasks: Tags: --- ossfuzz/decompress_fuzzer.c | 5 ++--- ossfuzz/fuzz_data_producer.h | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 ossfuzz/fuzz_data_producer.h diff --git a/ossfuzz/decompress_fuzzer.c b/ossfuzz/decompress_fuzzer.c index 0267c93..b17783c 100644 --- a/ossfuzz/decompress_fuzzer.c +++ b/ossfuzz/decompress_fuzzer.c @@ -9,13 +9,12 @@ #include #include "fuzz_helpers.h" +#include "fuzz_data_producer.h" #include "lz4.h" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { - - uint32_t seed = FUZZ_seed(&data, &size); - size_t const dstCapacity = FUZZ_rand32(&seed, 0, 4 * size); + size_t const dstCapacity = FUZZ_produceUint32Range(data, size, 0, 4 * size); size_t const smallDictSize = size + 1; size_t const largeDictSize = 64 * 1024 - 1; size_t const dictSize = MAX(smallDictSize, largeDictSize); diff --git a/ossfuzz/fuzz_data_producer.h b/ossfuzz/fuzz_data_producer.h new file mode 100644 index 0000000..c41aaec --- /dev/null +++ b/ossfuzz/fuzz_data_producer.h @@ -0,0 +1,27 @@ +#include +#include +#include +#include + +FUZZ_STATIC uint32_t FUZZ_produceUint32Range(uint8_t *data, size_t size, + uint32_t min, uint32_t max) { + if (min > max) { + return 0; + } + + uint32_t range = max - min; + uint32_t rolling = range; + uint32_t result = 0; + + while (rolling > 0 && size > 0) { + uint8_t next = *(data + size - 1); + size -= 1; + result = (result << 8) | next; + } + + if (range == 0xffffffff) { + return result; + } + + return min + result % (range + 1); +} -- cgit v1.2.3 From a9ac05645644a0615b558f6ac655c4ae46c4a926 Mon Sep 17 00:00:00 2001 From: bimbashrestha Date: Fri, 16 Aug 2019 14:19:06 -0700 Subject: Created a data producer API and used in decompress_fuzzer --- ossfuzz/decompress_fuzzer.c | 7 ++++++- ossfuzz/fuzz_data_producer.h | 25 ++++++++++++++++++++----- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/ossfuzz/decompress_fuzzer.c b/ossfuzz/decompress_fuzzer.c index b17783c..49f71b0 100644 --- a/ossfuzz/decompress_fuzzer.c +++ b/ossfuzz/decompress_fuzzer.c @@ -14,7 +14,8 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { - size_t const dstCapacity = FUZZ_produceUint32Range(data, size, 0, 4 * size); + FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size); + size_t const dstCapacity = FUZZ_dataProducer_uint32(producer, 0, 4 * size); size_t const smallDictSize = size + 1; size_t const largeDictSize = 64 * 1024 - 1; size_t const dictSize = MAX(smallDictSize, largeDictSize); @@ -24,6 +25,9 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) char* const dataAfterDict = dict + dictSize; char* const smallDict = dataAfterDict - smallDictSize; + /* Restrict to remaining data from producer */ + size = producer->size; + FUZZ_ASSERT(dst); FUZZ_ASSERT(dict); @@ -52,6 +56,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) dstCapacity, dstCapacity); free(dst); free(dict); + FUZZ_dataProducer_free(producer); return 0; } diff --git a/ossfuzz/fuzz_data_producer.h b/ossfuzz/fuzz_data_producer.h index c41aaec..6c4ef8a 100644 --- a/ossfuzz/fuzz_data_producer.h +++ b/ossfuzz/fuzz_data_producer.h @@ -3,8 +3,22 @@ #include #include -FUZZ_STATIC uint32_t FUZZ_produceUint32Range(uint8_t *data, size_t size, - uint32_t min, uint32_t max) { +typedef struct { + const uint8_t *data; + size_t size; +} FUZZ_dataProducer_t; + +FUZZ_dataProducer_t *FUZZ_dataProducer_create(const uint8_t *data, size_t size) { + FUZZ_dataProducer_t *producer = malloc(sizeof(FUZZ_dataProducer_t)); + producer->data = data; + producer->size = size; + return producer; +} + +void FUZZ_dataProducer_free(FUZZ_dataProducer_t *producer) { free(producer); } + +uint32_t FUZZ_dataProducer_uint32(FUZZ_dataProducer_t *producer, uint32_t min, + uint32_t max) { if (min > max) { return 0; } @@ -13,10 +27,11 @@ FUZZ_STATIC uint32_t FUZZ_produceUint32Range(uint8_t *data, size_t size, uint32_t rolling = range; uint32_t result = 0; - while (rolling > 0 && size > 0) { - uint8_t next = *(data + size - 1); - size -= 1; + while (rolling > 0 && producer->size > 0) { + uint8_t next = *(producer->data + producer->size - 1); + producer->size -= 1; result = (result << 8) | next; + rolling >>= 8; } if (range == 0xffffffff) { -- cgit v1.2.3 From f839e9fe8a393117c64dff58196d36b741780ab0 Mon Sep 17 00:00:00 2001 From: bimbashrestha Date: Fri, 16 Aug 2019 16:43:28 -0700 Subject: Seperating fuzz data producer api impl and header, using data producer on the easy fuzzers --- ossfuzz/Makefile | 2 +- ossfuzz/compress_frame_fuzzer.c | 2 ++ ossfuzz/compress_fuzzer.c | 10 ++++++++-- ossfuzz/compress_hc_fuzzer.c | 13 ++++++++++--- ossfuzz/decompress_frame_fuzzer.c | 14 ++++++++++---- ossfuzz/decompress_fuzzer.c | 3 ++- ossfuzz/fuzz_data_producer.c | 32 ++++++++++++++++++++++++++++++++ ossfuzz/fuzz_data_producer.h | 34 +++++----------------------------- ossfuzz/round_trip_hc_fuzzer.c | 10 ++++++++-- 9 files changed, 78 insertions(+), 42 deletions(-) create mode 100644 ossfuzz/fuzz_data_producer.c diff --git a/ossfuzz/Makefile b/ossfuzz/Makefile index 6875eb6..7e043a1 100644 --- a/ossfuzz/Makefile +++ b/ossfuzz/Makefile @@ -58,7 +58,7 @@ $(LZ4DIR)/liblz4.a: $(CC) -c $(LZ4_CFLAGS) $(LZ4_CPPFLAGS) $< -o $@ # Generic rule for generating fuzzers -%_fuzzer: %_fuzzer.o lz4_helpers.o $(LZ4DIR)/liblz4.a +%_fuzzer: %_fuzzer.o lz4_helpers.o fuzz_data_producer.o $(LZ4DIR)/liblz4.a # Compile the standalone code just in case. The OSS-Fuzz code might # override the LIB_FUZZING_ENGINE value to "-fsanitize=fuzzer" $(CC) -c $(LZ4_CFLAGS) $(LZ4_CPPFLAGS) standaloneengine.c -o standaloneengine.o diff --git a/ossfuzz/compress_frame_fuzzer.c b/ossfuzz/compress_frame_fuzzer.c index 75c609f..344917a 100644 --- a/ossfuzz/compress_frame_fuzzer.c +++ b/ossfuzz/compress_frame_fuzzer.c @@ -10,12 +10,14 @@ #include #include "fuzz_helpers.h" +#include "fuzz_data_producer.h" #include "lz4.h" #include "lz4frame.h" #include "lz4_helpers.h" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { + FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size); uint32_t seed = FUZZ_seed(&data, &size); LZ4F_preferences_t const prefs = FUZZ_randomPreferences(&seed); size_t const compressBound = LZ4F_compressFrameBound(size, &prefs); diff --git a/ossfuzz/compress_fuzzer.c b/ossfuzz/compress_fuzzer.c index 7021624..42efbd3 100644 --- a/ossfuzz/compress_fuzzer.c +++ b/ossfuzz/compress_fuzzer.c @@ -10,15 +10,20 @@ #include #include "fuzz_helpers.h" +#include "fuzz_data_producer.h" #include "lz4.h" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { - uint32_t seed = FUZZ_seed(&data, &size); - size_t const dstCapacity = FUZZ_rand32(&seed, 0, LZ4_compressBound(size)); + FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size); + size_t const dstCapacity = FUZZ_dataProducer_uint32( + producer, 0, LZ4_compressBound(size)); char* const dst = (char*)malloc(dstCapacity); char* const rt = (char*)malloc(size); + /* Restrict to remaining data from producer */ + size = producer->size; + FUZZ_ASSERT(dst); FUZZ_ASSERT(rt); @@ -46,6 +51,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) free(dst); free(rt); + FUZZ_dataProducer_free(producer); return 0; } diff --git a/ossfuzz/compress_hc_fuzzer.c b/ossfuzz/compress_hc_fuzzer.c index 4841367..f06f6dd 100644 --- a/ossfuzz/compress_hc_fuzzer.c +++ b/ossfuzz/compress_hc_fuzzer.c @@ -10,16 +10,22 @@ #include #include "fuzz_helpers.h" +#include "fuzz_data_producer.h" #include "lz4.h" #include "lz4hc.h" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { - uint32_t seed = FUZZ_seed(&data, &size); - size_t const dstCapacity = FUZZ_rand32(&seed, 0, LZ4_compressBound(size)); + FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size); + size_t const dstCapacity = FUZZ_dataProducer_uint32( + producer, 0, LZ4_compressBound(size)); char* const dst = (char*)malloc(dstCapacity); char* const rt = (char*)malloc(size); - int const level = FUZZ_rand32(&seed, LZ4HC_CLEVEL_MIN, LZ4HC_CLEVEL_MAX); + int const level = FUZZ_dataProducer_uint32( + producer, LZ4HC_CLEVEL_MIN, LZ4HC_CLEVEL_MAX); + + /* Restrict to remaining data from producer */ + size = producer->size; FUZZ_ASSERT(dst); FUZZ_ASSERT(rt); @@ -52,6 +58,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) free(dst); free(rt); + FUZZ_dataProducer_free(producer); return 0; } diff --git a/ossfuzz/decompress_frame_fuzzer.c b/ossfuzz/decompress_frame_fuzzer.c index bda25b0..4c8ac39 100644 --- a/ossfuzz/decompress_frame_fuzzer.c +++ b/ossfuzz/decompress_frame_fuzzer.c @@ -9,6 +9,7 @@ #include #include "fuzz_helpers.h" +#include "fuzz_data_producer.h" #include "lz4.h" #define LZ4F_STATIC_LINKING_ONLY #include "lz4frame.h" @@ -29,17 +30,21 @@ static void decompress(LZ4F_dctx* dctx, void* dst, size_t dstCapacity, int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { - - uint32_t seed = FUZZ_seed(&data, &size); - size_t const dstCapacity = FUZZ_rand32(&seed, 0, 4 * size); + FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size); + size_t const dstCapacity = FUZZ_dataProducer_uint32( + producer, 0, 4 * size); size_t const largeDictSize = 64 * 1024; - size_t const dictSize = FUZZ_rand32(&seed, 0, largeDictSize); + size_t const dictSize = FUZZ_dataProducer_uint32( + producer, 0, largeDictSize); char* const dst = (char*)malloc(dstCapacity); char* const dict = (char*)malloc(dictSize); LZ4F_decompressOptions_t opts; LZ4F_dctx* dctx; LZ4F_createDecompressionContext(&dctx, LZ4F_VERSION); + /* Restrict to remaining data from producer */ + size = producer->size; + FUZZ_ASSERT(dctx); FUZZ_ASSERT(dst); FUZZ_ASSERT(dict); @@ -62,6 +67,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) LZ4F_freeDecompressionContext(dctx); free(dst); free(dict); + FUZZ_dataProducer_free(producer); return 0; } diff --git a/ossfuzz/decompress_fuzzer.c b/ossfuzz/decompress_fuzzer.c index 49f71b0..ae03ba9 100644 --- a/ossfuzz/decompress_fuzzer.c +++ b/ossfuzz/decompress_fuzzer.c @@ -15,7 +15,8 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size); - size_t const dstCapacity = FUZZ_dataProducer_uint32(producer, 0, 4 * size); + size_t const dstCapacity = FUZZ_dataProducer_uint32( + producer, 0, 4 * size); size_t const smallDictSize = size + 1; size_t const largeDictSize = 64 * 1024 - 1; size_t const dictSize = MAX(smallDictSize, largeDictSize); diff --git a/ossfuzz/fuzz_data_producer.c b/ossfuzz/fuzz_data_producer.c new file mode 100644 index 0000000..ae07575 --- /dev/null +++ b/ossfuzz/fuzz_data_producer.c @@ -0,0 +1,32 @@ +#include "fuzz_data_producer.h" + +FUZZ_dataProducer_t *FUZZ_dataProducer_create(const uint8_t *data, size_t size) { + FUZZ_dataProducer_t *producer = malloc(sizeof(FUZZ_dataProducer_t)); + producer->data = data; + producer->size = size; + return producer; +} + +void FUZZ_dataProducer_free(FUZZ_dataProducer_t *producer) { free(producer); } + +uint32_t FUZZ_dataProducer_uint32(FUZZ_dataProducer_t *producer, uint32_t min, + uint32_t max) { + FUZZ_ASSERT(min <= max); + + uint32_t range = max - min; + uint32_t rolling = range; + uint32_t result = 0; + + while (rolling > 0 && producer->size > 0) { + uint8_t next = *(producer->data + producer->size - 1); + producer->size -= 1; + result = (result << 8) | next; + rolling >>= 8; + } + + if (range == 0xffffffff) { + return result; + } + + return min + result % (range + 1); +} diff --git a/ossfuzz/fuzz_data_producer.h b/ossfuzz/fuzz_data_producer.h index 6c4ef8a..389ec2a 100644 --- a/ossfuzz/fuzz_data_producer.h +++ b/ossfuzz/fuzz_data_producer.h @@ -3,40 +3,16 @@ #include #include +#include "fuzz_helpers.h" + typedef struct { const uint8_t *data; size_t size; } FUZZ_dataProducer_t; -FUZZ_dataProducer_t *FUZZ_dataProducer_create(const uint8_t *data, size_t size) { - FUZZ_dataProducer_t *producer = malloc(sizeof(FUZZ_dataProducer_t)); - producer->data = data; - producer->size = size; - return producer; -} +FUZZ_dataProducer_t *FUZZ_dataProducer_create(const uint8_t *data, size_t size); -void FUZZ_dataProducer_free(FUZZ_dataProducer_t *producer) { free(producer); } +void FUZZ_dataProducer_free(FUZZ_dataProducer_t *producer); uint32_t FUZZ_dataProducer_uint32(FUZZ_dataProducer_t *producer, uint32_t min, - uint32_t max) { - if (min > max) { - return 0; - } - - uint32_t range = max - min; - uint32_t rolling = range; - uint32_t result = 0; - - while (rolling > 0 && producer->size > 0) { - uint8_t next = *(producer->data + producer->size - 1); - producer->size -= 1; - result = (result << 8) | next; - rolling >>= 8; - } - - if (range == 0xffffffff) { - return result; - } - - return min + result % (range + 1); -} + uint32_t max); diff --git a/ossfuzz/round_trip_hc_fuzzer.c b/ossfuzz/round_trip_hc_fuzzer.c index 325cdf0..22b5e8f 100644 --- a/ossfuzz/round_trip_hc_fuzzer.c +++ b/ossfuzz/round_trip_hc_fuzzer.c @@ -9,16 +9,21 @@ #include #include "fuzz_helpers.h" +#include "fuzz_data_producer.h" #include "lz4.h" #include "lz4hc.h" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { - uint32_t seed = FUZZ_seed(&data, &size); + FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size); size_t const dstCapacity = LZ4_compressBound(size); char* const dst = (char*)malloc(dstCapacity); char* const rt = (char*)malloc(size); - int const level = FUZZ_rand32(&seed, LZ4HC_CLEVEL_MIN, LZ4HC_CLEVEL_MAX); + int const level = FUZZ_dataProducer_uint32( + producer, LZ4HC_CLEVEL_MIN, LZ4HC_CLEVEL_MAX); + + /* Restrict to remaining data from producer */ + size = producer->size; FUZZ_ASSERT(dst); FUZZ_ASSERT(rt); @@ -34,6 +39,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) free(dst); free(rt); + FUZZ_dataProducer_free(producer); return 0; } -- cgit v1.2.3 From dc17d39c2fb962b591c73cf1467d89cf53b89156 Mon Sep 17 00:00:00 2001 From: bimbashrestha Date: Fri, 16 Aug 2019 17:14:47 -0700 Subject: Adding comments, fixing nit, and hiding the struct in data producer api --- ossfuzz/compress_frame_fuzzer.c | 2 -- ossfuzz/compress_fuzzer.c | 2 +- ossfuzz/compress_hc_fuzzer.c | 2 +- ossfuzz/decompress_frame_fuzzer.c | 2 +- ossfuzz/decompress_fuzzer.c | 2 +- ossfuzz/fuzz_data_producer.c | 12 ++++++++++++ ossfuzz/fuzz_data_producer.h | 12 ++++++++---- ossfuzz/round_trip_hc_fuzzer.c | 2 +- 8 files changed, 25 insertions(+), 11 deletions(-) diff --git a/ossfuzz/compress_frame_fuzzer.c b/ossfuzz/compress_frame_fuzzer.c index 344917a..75c609f 100644 --- a/ossfuzz/compress_frame_fuzzer.c +++ b/ossfuzz/compress_frame_fuzzer.c @@ -10,14 +10,12 @@ #include #include "fuzz_helpers.h" -#include "fuzz_data_producer.h" #include "lz4.h" #include "lz4frame.h" #include "lz4_helpers.h" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { - FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size); uint32_t seed = FUZZ_seed(&data, &size); LZ4F_preferences_t const prefs = FUZZ_randomPreferences(&seed); size_t const compressBound = LZ4F_compressFrameBound(size, &prefs); diff --git a/ossfuzz/compress_fuzzer.c b/ossfuzz/compress_fuzzer.c index 42efbd3..9d72e72 100644 --- a/ossfuzz/compress_fuzzer.c +++ b/ossfuzz/compress_fuzzer.c @@ -22,7 +22,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) char* const rt = (char*)malloc(size); /* Restrict to remaining data from producer */ - size = producer->size; + size = FUZZ_dataProducer_remainingBytes(producer); FUZZ_ASSERT(dst); FUZZ_ASSERT(rt); diff --git a/ossfuzz/compress_hc_fuzzer.c b/ossfuzz/compress_hc_fuzzer.c index f06f6dd..5f22104 100644 --- a/ossfuzz/compress_hc_fuzzer.c +++ b/ossfuzz/compress_hc_fuzzer.c @@ -25,7 +25,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) producer, LZ4HC_CLEVEL_MIN, LZ4HC_CLEVEL_MAX); /* Restrict to remaining data from producer */ - size = producer->size; + size = FUZZ_dataProducer_remainingBytes(producer); FUZZ_ASSERT(dst); FUZZ_ASSERT(rt); diff --git a/ossfuzz/decompress_frame_fuzzer.c b/ossfuzz/decompress_frame_fuzzer.c index 4c8ac39..60d2ea1 100644 --- a/ossfuzz/decompress_frame_fuzzer.c +++ b/ossfuzz/decompress_frame_fuzzer.c @@ -43,7 +43,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) LZ4F_createDecompressionContext(&dctx, LZ4F_VERSION); /* Restrict to remaining data from producer */ - size = producer->size; + size = FUZZ_dataProducer_remainingBytes(producer); FUZZ_ASSERT(dctx); FUZZ_ASSERT(dst); diff --git a/ossfuzz/decompress_fuzzer.c b/ossfuzz/decompress_fuzzer.c index ae03ba9..bc4190b 100644 --- a/ossfuzz/decompress_fuzzer.c +++ b/ossfuzz/decompress_fuzzer.c @@ -27,7 +27,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) char* const smallDict = dataAfterDict - smallDictSize; /* Restrict to remaining data from producer */ - size = producer->size; + size = FUZZ_dataProducer_remainingBytes(producer); FUZZ_ASSERT(dst); FUZZ_ASSERT(dict); diff --git a/ossfuzz/fuzz_data_producer.c b/ossfuzz/fuzz_data_producer.c index ae07575..992f5a7 100644 --- a/ossfuzz/fuzz_data_producer.c +++ b/ossfuzz/fuzz_data_producer.c @@ -1,7 +1,15 @@ #include "fuzz_data_producer.h" +struct FUZZ_dataProducer_s{ + const uint8_t *data; + size_t size; +}; + FUZZ_dataProducer_t *FUZZ_dataProducer_create(const uint8_t *data, size_t size) { FUZZ_dataProducer_t *producer = malloc(sizeof(FUZZ_dataProducer_t)); + + FUZZ_ASSERT(producer != NULL); + producer->data = data; producer->size = size; return producer; @@ -30,3 +38,7 @@ uint32_t FUZZ_dataProducer_uint32(FUZZ_dataProducer_t *producer, uint32_t min, return min + result % (range + 1); } + +size_t FUZZ_dataProducer_remainingBytes(FUZZ_dataProducer_t *producer){ + return producer->size; +} diff --git a/ossfuzz/fuzz_data_producer.h b/ossfuzz/fuzz_data_producer.h index 389ec2a..8df5257 100644 --- a/ossfuzz/fuzz_data_producer.h +++ b/ossfuzz/fuzz_data_producer.h @@ -5,14 +5,18 @@ #include "fuzz_helpers.h" -typedef struct { - const uint8_t *data; - size_t size; -} FUZZ_dataProducer_t; +/* Struct used for maintaining the state of the data */ +typedef struct FUZZ_dataProducer_s FUZZ_dataProducer_t; +/* Returns a data producer state struct. Use for producer initialization. */ FUZZ_dataProducer_t *FUZZ_dataProducer_create(const uint8_t *data, size_t size); +/* Frees the data producer */ void FUZZ_dataProducer_free(FUZZ_dataProducer_t *producer); +/* Returns value between [min, max] */ uint32_t FUZZ_dataProducer_uint32(FUZZ_dataProducer_t *producer, uint32_t min, uint32_t max); + +/* Returns the size of the remaining bytes of data in the producer */ +size_t FUZZ_dataProducer_remainingBytes(FUZZ_dataProducer_t *producer); diff --git a/ossfuzz/round_trip_hc_fuzzer.c b/ossfuzz/round_trip_hc_fuzzer.c index 22b5e8f..8406809 100644 --- a/ossfuzz/round_trip_hc_fuzzer.c +++ b/ossfuzz/round_trip_hc_fuzzer.c @@ -23,7 +23,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) producer, LZ4HC_CLEVEL_MIN, LZ4HC_CLEVEL_MAX); /* Restrict to remaining data from producer */ - size = producer->size; + size = FUZZ_dataProducer_remainingBytes(producer); FUZZ_ASSERT(dst); FUZZ_ASSERT(rt); -- cgit v1.2.3 From 28964f4bea7fa95dc76154c5190d77fc18b2f8c8 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 21 Aug 2019 13:44:24 +0200 Subject: fixed #778 fixed assert() when divisor == 0 --- examples/frameCompress.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/frameCompress.c b/examples/frameCompress.c index a189329..aac4a3b 100644 --- a/examples/frameCompress.c +++ b/examples/frameCompress.c @@ -32,12 +32,12 @@ static void safe_fwrite(void* buf, size_t eltSize, size_t nbElt, FILE* f) { size_t const writtenSize = fwrite(buf, eltSize, nbElt, f); size_t const expectedSize = eltSize * nbElt; - assert(expectedSize / nbElt == eltSize); /* check overflow */ + if (nbElt>0) assert(expectedSize / nbElt == eltSize); /* check overflow */ if (writtenSize < expectedSize) { if (ferror(f)) /* note : ferror() must follow fwrite */ fprintf(stderr, "Write failed \n"); else - fprintf(stderr, "Short write \n"); + fprintf(stderr, "Write too short \n"); exit(1); } } -- cgit v1.2.3 From 7d153a704d00a266c9601c947e07fd33e1cd6f4d Mon Sep 17 00:00:00 2001 From: Bimba Shrestha Date: Fri, 30 Aug 2019 10:27:42 -0700 Subject: Making fuzzers use dataProducer api instead of random seed for decisions --- ossfuzz/compress_frame_fuzzer.c | 11 ++++++++--- ossfuzz/fuzz_data_producer.c | 25 +++++++++++++++++++++++++ ossfuzz/fuzz_data_producer.h | 8 ++++++++ ossfuzz/round_trip_frame_fuzzer.c | 9 +++++++-- ossfuzz/round_trip_fuzzer.c | 10 ++++++++-- 5 files changed, 56 insertions(+), 7 deletions(-) diff --git a/ossfuzz/compress_frame_fuzzer.c b/ossfuzz/compress_frame_fuzzer.c index 75c609f..7fe09a1 100644 --- a/ossfuzz/compress_frame_fuzzer.c +++ b/ossfuzz/compress_frame_fuzzer.c @@ -13,19 +13,23 @@ #include "lz4.h" #include "lz4frame.h" #include "lz4_helpers.h" +#include "fuzz_data_producer.h" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { - uint32_t seed = FUZZ_seed(&data, &size); - LZ4F_preferences_t const prefs = FUZZ_randomPreferences(&seed); + FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, LZ4_compressBound(size)); + LZ4F_preferences_t const prefs = FUZZ_dataProducer_preferences(producer); size_t const compressBound = LZ4F_compressFrameBound(size, &prefs); - size_t const dstCapacity = FUZZ_rand32(&seed, 0, compressBound); + size_t const dstCapacity = FUZZ_dataProducer_uint32(producer, 0, compressBound); char* const dst = (char*)malloc(dstCapacity); char* const rt = (char*)malloc(size); FUZZ_ASSERT(dst); FUZZ_ASSERT(rt); + /* Restrict to remaining data from producer */ + size = FUZZ_dataProducer_remainingBytes(producer); + /* If compression succeeds it must round trip correctly. */ size_t const dstSize = LZ4F_compressFrame(dst, dstCapacity, data, size, &prefs); @@ -37,6 +41,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) free(dst); free(rt); + FUZZ_dataProducer_free(producer); return 0; } diff --git a/ossfuzz/fuzz_data_producer.c b/ossfuzz/fuzz_data_producer.c index 992f5a7..f35bd8a 100644 --- a/ossfuzz/fuzz_data_producer.c +++ b/ossfuzz/fuzz_data_producer.c @@ -39,6 +39,31 @@ uint32_t FUZZ_dataProducer_uint32(FUZZ_dataProducer_t *producer, uint32_t min, return min + result % (range + 1); } +LZ4F_frameInfo_t FUZZ_dataProducer_frameInfo(FUZZ_dataProducer_t* producer) +{ + LZ4F_frameInfo_t info = LZ4F_INIT_FRAMEINFO; + info.blockSizeID = FUZZ_dataProducer_uint32(producer, LZ4F_max64KB - 1, LZ4F_max4MB); + if (info.blockSizeID < LZ4F_max64KB) { + info.blockSizeID = LZ4F_default; + } + info.blockMode = FUZZ_dataProducer_uint32(producer, LZ4F_blockLinked, LZ4F_blockIndependent); + info.contentChecksumFlag = FUZZ_dataProducer_uint32(producer, LZ4F_noContentChecksum, + LZ4F_contentChecksumEnabled); + info.blockChecksumFlag = FUZZ_dataProducer_uint32(producer, LZ4F_noBlockChecksum, + LZ4F_blockChecksumEnabled); + return info; +} + +LZ4F_preferences_t FUZZ_dataProducer_preferences(FUZZ_dataProducer_t* producer) +{ + LZ4F_preferences_t prefs = LZ4F_INIT_PREFERENCES; + prefs.frameInfo = FUZZ_dataProducer_frameInfo(producer); + prefs.compressionLevel = FUZZ_dataProducer_uint32(producer, 0, LZ4HC_CLEVEL_MAX + 3) - 3; + prefs.autoFlush = FUZZ_dataProducer_uint32(producer, 0, 1); + prefs.favorDecSpeed = FUZZ_dataProducer_uint32(producer, 0, 1); + return prefs; +} + size_t FUZZ_dataProducer_remainingBytes(FUZZ_dataProducer_t *producer){ return producer->size; } diff --git a/ossfuzz/fuzz_data_producer.h b/ossfuzz/fuzz_data_producer.h index 8df5257..4c097a7 100644 --- a/ossfuzz/fuzz_data_producer.h +++ b/ossfuzz/fuzz_data_producer.h @@ -4,6 +4,8 @@ #include #include "fuzz_helpers.h" +#include "lz4frame.h" +#include "lz4hc.h" /* Struct used for maintaining the state of the data */ typedef struct FUZZ_dataProducer_s FUZZ_dataProducer_t; @@ -18,5 +20,11 @@ void FUZZ_dataProducer_free(FUZZ_dataProducer_t *producer); uint32_t FUZZ_dataProducer_uint32(FUZZ_dataProducer_t *producer, uint32_t min, uint32_t max); +/* Returns lz4 preferences */ +LZ4F_preferences_t FUZZ_dataProducer_preferences(FUZZ_dataProducer_t* producer); + +/* Returns lz4 frame info */ +LZ4F_frameInfo_t FUZZ_dataProducer_frameInfo(FUZZ_dataProducer_t* producer); + /* Returns the size of the remaining bytes of data in the producer */ size_t FUZZ_dataProducer_remainingBytes(FUZZ_dataProducer_t *producer); diff --git a/ossfuzz/round_trip_frame_fuzzer.c b/ossfuzz/round_trip_frame_fuzzer.c index 1eea90c..fe6fc77 100644 --- a/ossfuzz/round_trip_frame_fuzzer.c +++ b/ossfuzz/round_trip_frame_fuzzer.c @@ -12,11 +12,12 @@ #include "lz4.h" #include "lz4frame.h" #include "lz4_helpers.h" +#include "fuzz_data_producer.h" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { - uint32_t seed = FUZZ_seed(&data, &size); - LZ4F_preferences_t const prefs = FUZZ_randomPreferences(&seed); + FUZZ_dataProducer_t* producer = FUZZ_dataProducer_create(data, LZ4_compressBound(size)); + LZ4F_preferences_t const prefs = FUZZ_dataProducer_preferences(producer); size_t const dstCapacity = LZ4F_compressFrameBound(size, &prefs); char* const dst = (char*)malloc(dstCapacity); char* const rt = (char*)malloc(size); @@ -24,6 +25,9 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) FUZZ_ASSERT(dst); FUZZ_ASSERT(rt); + /* Restrict to remaining data from producer */ + size = FUZZ_dataProducer_remainingBytes(producer); + /* Compression must succeed and round trip correctly. */ size_t const dstSize = LZ4F_compressFrame(dst, dstCapacity, data, size, &prefs); @@ -34,6 +38,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) free(dst); free(rt); + FUZZ_dataProducer_free(producer); return 0; } diff --git a/ossfuzz/round_trip_fuzzer.c b/ossfuzz/round_trip_fuzzer.c index 3a66e80..e37a0a6 100644 --- a/ossfuzz/round_trip_fuzzer.c +++ b/ossfuzz/round_trip_fuzzer.c @@ -10,10 +10,12 @@ #include "fuzz_helpers.h" #include "lz4.h" +#include "fuzz_data_producer.h" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { - uint32_t seed = FUZZ_seed(&data, &size); + FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size); + size_t const partialCapacity = FUZZ_dataProducer_uint32(producer, 0, size); size_t const dstCapacity = LZ4_compressBound(size); char* const dst = (char*)malloc(dstCapacity); char* const rt = (char*)malloc(size); @@ -21,6 +23,9 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) FUZZ_ASSERT(dst); FUZZ_ASSERT(rt); + /* Restrict to remaining data from producer */ + size = FUZZ_dataProducer_remainingBytes(producer); + /* Compression must succeed and round trip correctly. */ int const dstSize = LZ4_compress_default((const char*)data, dst, size, dstCapacity); @@ -32,7 +37,6 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) /* Partial decompression must succeed. */ { - size_t const partialCapacity = FUZZ_rand32(&seed, 0, size); char* const partial = (char*)malloc(partialCapacity); FUZZ_ASSERT(partial); int const partialSize = LZ4_decompress_safe_partial( @@ -43,8 +47,10 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) free(partial); } + free(dst); free(rt); + FUZZ_dataProducer_free(producer); return 0; } -- cgit v1.2.3 From a55095ddd63431ecb79854f887f40b6a9fcd520c Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Tue, 30 Jul 2019 22:13:51 -0700 Subject: util.h: Remove deprecated utime for non-Windows utime was deprecated in POSIX 2008. --- programs/platform.h | 2 +- programs/util.h | 24 +++++++++++++++++++----- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/programs/platform.h b/programs/platform.h index c0b3840..7e2cb58 100644 --- a/programs/platform.h +++ b/programs/platform.h @@ -86,7 +86,7 @@ extern "C" { # else # if defined(__linux__) || defined(__linux) # ifndef _POSIX_C_SOURCE -# define _POSIX_C_SOURCE 200112L /* use feature test macro */ +# define _POSIX_C_SOURCE 200809L /* use feature test macro */ # endif # endif # include /* declares _POSIX_VERSION */ diff --git a/programs/util.h b/programs/util.h index 1dd515c..8e361ca 100644 --- a/programs/util.h +++ b/programs/util.h @@ -37,12 +37,17 @@ extern "C" { #include #include /* stat, utime */ #include /* stat */ -#if defined(_MSC_VER) +#if defined(_WIN32) # include /* utime */ # include /* _chmod */ #else # include /* chown, stat */ +# if PLATFORM_POSIX_VERSION < 200809L # include /* utime */ +# else +# include /* AT_FDCWD */ +# include /* for utimensat */ +# endif #endif #include /* time */ #include /* INT_MAX */ @@ -287,14 +292,23 @@ UTIL_STATIC int UTIL_isRegFile(const char* infilename); UTIL_STATIC int UTIL_setFileStat(const char *filename, stat_t *statbuf) { int res = 0; - struct utimbuf timebuf; if (!UTIL_isRegFile(filename)) return -1; - timebuf.actime = time(NULL); - timebuf.modtime = statbuf->st_mtime; - res += utime(filename, &timebuf); /* set access and modification times */ + { +#if defined(_WIN32) || (PLATFORM_POSIX_VERSION < 200809L) + struct utimbuf timebuf; + timebuf.actime = time(NULL); + timebuf.modtime = statbuf->st_mtime; + res += utime(filename, &timebuf); /* set access and modification times */ +#else + struct timespec timebuf[2] = {}; + timebuf[0].tv_nsec = UTIME_NOW; + timebuf[1].tv_sec = statbuf->st_mtime; + res += utimensat(AT_FDCWD, filename, timebuf, 0); /* set access and modification times */ +#endif + } #if !defined(_WIN32) res += chown(filename, statbuf->st_uid, statbuf->st_gid); /* Copy ownership */ -- cgit v1.2.3 From 208694297a308da8dae2b3bb104bdab486d1b683 Mon Sep 17 00:00:00 2001 From: Bimba Shrestha Date: Fri, 13 Sep 2019 14:07:52 -0700 Subject: Seperating the seed generation and use --- ossfuzz/compress_frame_fuzzer.c | 6 ++++-- ossfuzz/compress_fuzzer.c | 5 +++-- ossfuzz/compress_hc_fuzzer.c | 12 ++++++++---- ossfuzz/decompress_frame_fuzzer.c | 10 +++++++--- ossfuzz/decompress_fuzzer.c | 5 +++-- ossfuzz/fuzz_data_producer.c | 36 ++++++++++++++++++++++++------------ ossfuzz/fuzz_data_producer.h | 9 ++++++++- ossfuzz/round_trip_frame_fuzzer.c | 7 ++++--- ossfuzz/round_trip_fuzzer.c | 4 +++- ossfuzz/round_trip_hc_fuzzer.c | 7 ++++--- 10 files changed, 68 insertions(+), 33 deletions(-) diff --git a/ossfuzz/compress_frame_fuzzer.c b/ossfuzz/compress_frame_fuzzer.c index 7fe09a1..a99bb74 100644 --- a/ossfuzz/compress_frame_fuzzer.c +++ b/ossfuzz/compress_frame_fuzzer.c @@ -19,8 +19,10 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, LZ4_compressBound(size)); LZ4F_preferences_t const prefs = FUZZ_dataProducer_preferences(producer); - size_t const compressBound = LZ4F_compressFrameBound(size, &prefs); - size_t const dstCapacity = FUZZ_dataProducer_uint32(producer, 0, compressBound); + size_t const compressBound = LZ4F_compressFrameBound(FUZZ_dataProducer_remainingBytes(producer), &prefs); + size_t const dstCapacitySeed = FUZZ_dataProducer_uint32_seed(producer, 0, compressBound); + size_t const dstCapacity = FUZZ_dataProducer_uint32(dstCapacitySeed, + 0, FUZZ_dataProducer_remainingBytes(producer)); char* const dst = (char*)malloc(dstCapacity); char* const rt = (char*)malloc(size); diff --git a/ossfuzz/compress_fuzzer.c b/ossfuzz/compress_fuzzer.c index 9d72e72..76d227e 100644 --- a/ossfuzz/compress_fuzzer.c +++ b/ossfuzz/compress_fuzzer.c @@ -16,8 +16,9 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size); - size_t const dstCapacity = FUZZ_dataProducer_uint32( - producer, 0, LZ4_compressBound(size)); + size_t const dstCapacitySeed = FUZZ_dataProducer_uint32_seed(producer, 0, LZ4_compressBound(size)); + size_t const dstCapacity = FUZZ_dataProducer_uint32(dstCapacitySeed, + 0, LZ4_compressBound(FUZZ_dataProducer_remainingBytes(producer))); char* const dst = (char*)malloc(dstCapacity); char* const rt = (char*)malloc(size); diff --git a/ossfuzz/compress_hc_fuzzer.c b/ossfuzz/compress_hc_fuzzer.c index 5f22104..3e28f42 100644 --- a/ossfuzz/compress_hc_fuzzer.c +++ b/ossfuzz/compress_hc_fuzzer.c @@ -17,12 +17,16 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size); - size_t const dstCapacity = FUZZ_dataProducer_uint32( - producer, 0, LZ4_compressBound(size)); + size_t const dstCapacitySeed = FUZZ_dataProducer_uint32_seed(producer, + 0, LZ4_compressBound(size)); + size_t const levelSeed = FUZZ_dataProducer_uint32_seed(producer, + LZ4HC_CLEVEL_MIN, LZ4HC_CLEVEL_MAX); + size_t const dstCapacity = FUZZ_dataProducer_uint32(dstCapacitySeed, + 0, FUZZ_dataProducer_remainingBytes(producer)); + int const level = FUZZ_dataProducer_uint32( + levelSeed, LZ4HC_CLEVEL_MIN, LZ4HC_CLEVEL_MAX); char* const dst = (char*)malloc(dstCapacity); char* const rt = (char*)malloc(size); - int const level = FUZZ_dataProducer_uint32( - producer, LZ4HC_CLEVEL_MIN, LZ4HC_CLEVEL_MAX); /* Restrict to remaining data from producer */ size = FUZZ_dataProducer_remainingBytes(producer); diff --git a/ossfuzz/decompress_frame_fuzzer.c b/ossfuzz/decompress_frame_fuzzer.c index 60d2ea1..1308100 100644 --- a/ossfuzz/decompress_frame_fuzzer.c +++ b/ossfuzz/decompress_frame_fuzzer.c @@ -31,11 +31,15 @@ static void decompress(LZ4F_dctx* dctx, void* dst, size_t dstCapacity, int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size); - size_t const dstCapacity = FUZZ_dataProducer_uint32( - producer, 0, 4 * size); + size_t const dstCapacitySeed = FUZZ_dataProducer_uint32_seed(producer, + 0, 4 * size); size_t const largeDictSize = 64 * 1024; + size_t const dictSizeSeed = FUZZ_dataProducer_uint32_seed(producer, + 0, largeDictSize); + size_t const dstCapacity = FUZZ_dataProducer_uint32( + dstCapacitySeed, 0, 4 * FUZZ_dataProducer_remainingBytes(producer)); size_t const dictSize = FUZZ_dataProducer_uint32( - producer, 0, largeDictSize); + dictSizeSeed, 0, largeDictSize); char* const dst = (char*)malloc(dstCapacity); char* const dict = (char*)malloc(dictSize); LZ4F_decompressOptions_t opts; diff --git a/ossfuzz/decompress_fuzzer.c b/ossfuzz/decompress_fuzzer.c index bc4190b..111f5b5 100644 --- a/ossfuzz/decompress_fuzzer.c +++ b/ossfuzz/decompress_fuzzer.c @@ -15,8 +15,9 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size); - size_t const dstCapacity = FUZZ_dataProducer_uint32( - producer, 0, 4 * size); + size_t const dstCapacitySeed = FUZZ_dataProducer_uint32_seed(producer, 0, 4 * size); + size_t const dstCapacity = FUZZ_dataProducer_uint32(dstCapacitySeed, + 0, 4 * FUZZ_dataProducer_remainingBytes(producer)); size_t const smallDictSize = size + 1; size_t const largeDictSize = 64 * 1024 - 1; size_t const dictSize = MAX(smallDictSize, largeDictSize); diff --git a/ossfuzz/fuzz_data_producer.c b/ossfuzz/fuzz_data_producer.c index f35bd8a..70e42ea 100644 --- a/ossfuzz/fuzz_data_producer.c +++ b/ossfuzz/fuzz_data_producer.c @@ -17,7 +17,7 @@ FUZZ_dataProducer_t *FUZZ_dataProducer_create(const uint8_t *data, size_t size) void FUZZ_dataProducer_free(FUZZ_dataProducer_t *producer) { free(producer); } -uint32_t FUZZ_dataProducer_uint32(FUZZ_dataProducer_t *producer, uint32_t min, +uint32_t FUZZ_dataProducer_uint32_seed(FUZZ_dataProducer_t *producer, uint32_t min, uint32_t max) { FUZZ_ASSERT(min <= max); @@ -32,24 +32,36 @@ uint32_t FUZZ_dataProducer_uint32(FUZZ_dataProducer_t *producer, uint32_t min, rolling >>= 8; } - if (range == 0xffffffff) { - return result; - } + return result; +} - return min + result % (range + 1); +uint32_t FUZZ_dataProducer_uint32(uint32_t seed, uint32_t min, uint32_t max) +{ + uint32_t range = max - min; + if (range == 0xffffffff) { + return seed; + } + return min + seed % (range + 1); +} + +uint32_t FUZZ_dataProducer_uint32NonAdaptive(FUZZ_dataProducer_t* producer, + uint32_t min, uint32_t max) +{ + size_t const seed = FUZZ_dataProducer_uint32_seed(producer, min, max); + return FUZZ_dataProducer_uint32(seed, min, max); } LZ4F_frameInfo_t FUZZ_dataProducer_frameInfo(FUZZ_dataProducer_t* producer) { LZ4F_frameInfo_t info = LZ4F_INIT_FRAMEINFO; - info.blockSizeID = FUZZ_dataProducer_uint32(producer, LZ4F_max64KB - 1, LZ4F_max4MB); + info.blockSizeID = FUZZ_dataProducer_uint32NonAdaptive(producer, LZ4F_max64KB - 1, LZ4F_max4MB); if (info.blockSizeID < LZ4F_max64KB) { info.blockSizeID = LZ4F_default; } - info.blockMode = FUZZ_dataProducer_uint32(producer, LZ4F_blockLinked, LZ4F_blockIndependent); - info.contentChecksumFlag = FUZZ_dataProducer_uint32(producer, LZ4F_noContentChecksum, + info.blockMode = FUZZ_dataProducer_uint32NonAdaptive(producer, LZ4F_blockLinked, LZ4F_blockIndependent); + info.contentChecksumFlag = FUZZ_dataProducer_uint32NonAdaptive(producer, LZ4F_noContentChecksum, LZ4F_contentChecksumEnabled); - info.blockChecksumFlag = FUZZ_dataProducer_uint32(producer, LZ4F_noBlockChecksum, + info.blockChecksumFlag = FUZZ_dataProducer_uint32NonAdaptive(producer, LZ4F_noBlockChecksum, LZ4F_blockChecksumEnabled); return info; } @@ -58,9 +70,9 @@ LZ4F_preferences_t FUZZ_dataProducer_preferences(FUZZ_dataProducer_t* producer) { LZ4F_preferences_t prefs = LZ4F_INIT_PREFERENCES; prefs.frameInfo = FUZZ_dataProducer_frameInfo(producer); - prefs.compressionLevel = FUZZ_dataProducer_uint32(producer, 0, LZ4HC_CLEVEL_MAX + 3) - 3; - prefs.autoFlush = FUZZ_dataProducer_uint32(producer, 0, 1); - prefs.favorDecSpeed = FUZZ_dataProducer_uint32(producer, 0, 1); + prefs.compressionLevel = FUZZ_dataProducer_uint32NonAdaptive(producer, 0, LZ4HC_CLEVEL_MAX + 3) - 3; + prefs.autoFlush = FUZZ_dataProducer_uint32NonAdaptive(producer, 0, 1); + prefs.favorDecSpeed = FUZZ_dataProducer_uint32NonAdaptive(producer, 0, 1); return prefs; } diff --git a/ossfuzz/fuzz_data_producer.h b/ossfuzz/fuzz_data_producer.h index 4c097a7..e12f899 100644 --- a/ossfuzz/fuzz_data_producer.h +++ b/ossfuzz/fuzz_data_producer.h @@ -16,8 +16,15 @@ FUZZ_dataProducer_t *FUZZ_dataProducer_create(const uint8_t *data, size_t size); /* Frees the data producer */ void FUZZ_dataProducer_free(FUZZ_dataProducer_t *producer); +/* Returns a seed value for the function after this one to consume */ +uint32_t FUZZ_dataProducer_uint32_seed(FUZZ_dataProducer_t *producer, uint32_t min, + uint32_t max); + /* Returns value between [min, max] */ -uint32_t FUZZ_dataProducer_uint32(FUZZ_dataProducer_t *producer, uint32_t min, +uint32_t FUZZ_dataProducer_uint32(uint32_t seed, uint32_t min, uint32_t max); + +/* Combination of above two functions for non adaptive use cases. ie where size is not involved */ +uint32_t FUZZ_dataProducer_uint32NonAdaptive(FUZZ_dataProducer_t *producer, uint32_t min, uint32_t max); /* Returns lz4 preferences */ diff --git a/ossfuzz/round_trip_frame_fuzzer.c b/ossfuzz/round_trip_frame_fuzzer.c index fe6fc77..c34553d 100644 --- a/ossfuzz/round_trip_frame_fuzzer.c +++ b/ossfuzz/round_trip_frame_fuzzer.c @@ -16,11 +16,12 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { - FUZZ_dataProducer_t* producer = FUZZ_dataProducer_create(data, LZ4_compressBound(size)); + FUZZ_dataProducer_t* producer = FUZZ_dataProducer_create(data, size); LZ4F_preferences_t const prefs = FUZZ_dataProducer_preferences(producer); - size_t const dstCapacity = LZ4F_compressFrameBound(size, &prefs); + size_t const dstCapacity = LZ4F_compressFrameBound( + LZ4_compressBound(FUZZ_dataProducer_remainingBytes(producer)), &prefs); char* const dst = (char*)malloc(dstCapacity); - char* const rt = (char*)malloc(size); + char* const rt = (char*)malloc(FUZZ_dataProducer_remainingBytes(producer)); FUZZ_ASSERT(dst); FUZZ_ASSERT(rt); diff --git a/ossfuzz/round_trip_fuzzer.c b/ossfuzz/round_trip_fuzzer.c index e37a0a6..85774fe 100644 --- a/ossfuzz/round_trip_fuzzer.c +++ b/ossfuzz/round_trip_fuzzer.c @@ -15,7 +15,9 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size); - size_t const partialCapacity = FUZZ_dataProducer_uint32(producer, 0, size); + size_t const partialCapacitySeed = FUZZ_dataProducer_uint32_seed(producer, 0, size); + size_t const partialCapacity = FUZZ_dataProducer_uint32(partialCapacitySeed, + 0, FUZZ_dataProducer_remainingBytes(producer)); size_t const dstCapacity = LZ4_compressBound(size); char* const dst = (char*)malloc(dstCapacity); char* const rt = (char*)malloc(size); diff --git a/ossfuzz/round_trip_hc_fuzzer.c b/ossfuzz/round_trip_hc_fuzzer.c index 8406809..cc9c302 100644 --- a/ossfuzz/round_trip_hc_fuzzer.c +++ b/ossfuzz/round_trip_hc_fuzzer.c @@ -16,11 +16,12 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size); - size_t const dstCapacity = LZ4_compressBound(size); + int const level = FUZZ_dataProducer_uint32NonAdaptive(producer, + LZ4HC_CLEVEL_MIN, LZ4HC_CLEVEL_MAX); + size_t const dstCapacity = LZ4_compressBound( + FUZZ_dataProducer_remainingBytes(producer)); char* const dst = (char*)malloc(dstCapacity); char* const rt = (char*)malloc(size); - int const level = FUZZ_dataProducer_uint32( - producer, LZ4HC_CLEVEL_MIN, LZ4HC_CLEVEL_MAX); /* Restrict to remaining data from producer */ size = FUZZ_dataProducer_remainingBytes(producer); -- cgit v1.2.3 From 9cb73d69c4d92e255826dfee47e46a6815955ee4 Mon Sep 17 00:00:00 2001 From: Bimba Shrestha Date: Fri, 13 Sep 2019 16:04:48 -0700 Subject: Addressing naming nits and moving size modification up in all fuzzers --- ossfuzz/compress_frame_fuzzer.c | 15 ++++++++------- ossfuzz/compress_fuzzer.c | 11 +++++------ ossfuzz/compress_hc_fuzzer.c | 16 +++++++--------- ossfuzz/decompress_frame_fuzzer.c | 16 ++++++++-------- ossfuzz/decompress_fuzzer.c | 11 +++++------ ossfuzz/fuzz_data_producer.c | 24 ++++++++++++------------ ossfuzz/fuzz_data_producer.h | 6 +++--- ossfuzz/round_trip_frame_fuzzer.c | 9 ++++----- ossfuzz/round_trip_fuzzer.c | 12 ++++++------ ossfuzz/round_trip_hc_fuzzer.c | 11 +++++------ 10 files changed, 63 insertions(+), 68 deletions(-) diff --git a/ossfuzz/compress_frame_fuzzer.c b/ossfuzz/compress_frame_fuzzer.c index a99bb74..30f0448 100644 --- a/ossfuzz/compress_frame_fuzzer.c +++ b/ossfuzz/compress_frame_fuzzer.c @@ -19,19 +19,20 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, LZ4_compressBound(size)); LZ4F_preferences_t const prefs = FUZZ_dataProducer_preferences(producer); - size_t const compressBound = LZ4F_compressFrameBound(FUZZ_dataProducer_remainingBytes(producer), &prefs); - size_t const dstCapacitySeed = FUZZ_dataProducer_uint32_seed(producer, 0, compressBound); - size_t const dstCapacity = FUZZ_dataProducer_uint32(dstCapacitySeed, - 0, FUZZ_dataProducer_remainingBytes(producer)); + + size = FUZZ_dataProducer_remainingBytes(producer); + size_t const compressBound = LZ4F_compressFrameBound(size, &prefs); + size_t const dstCapacitySeed = FUZZ_dataProducer_uint32(producer, 0, compressBound); + + size = FUZZ_dataProducer_remainingBytes(producer); + size_t const dstCapacity = FUZZ_getRange_from_uint32(dstCapacitySeed, 0, size); + char* const dst = (char*)malloc(dstCapacity); char* const rt = (char*)malloc(size); FUZZ_ASSERT(dst); FUZZ_ASSERT(rt); - /* Restrict to remaining data from producer */ - size = FUZZ_dataProducer_remainingBytes(producer); - /* If compression succeeds it must round trip correctly. */ size_t const dstSize = LZ4F_compressFrame(dst, dstCapacity, data, size, &prefs); diff --git a/ossfuzz/compress_fuzzer.c b/ossfuzz/compress_fuzzer.c index 76d227e..fac7dab 100644 --- a/ossfuzz/compress_fuzzer.c +++ b/ossfuzz/compress_fuzzer.c @@ -16,14 +16,13 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size); - size_t const dstCapacitySeed = FUZZ_dataProducer_uint32_seed(producer, 0, LZ4_compressBound(size)); - size_t const dstCapacity = FUZZ_dataProducer_uint32(dstCapacitySeed, - 0, LZ4_compressBound(FUZZ_dataProducer_remainingBytes(producer))); - char* const dst = (char*)malloc(dstCapacity); - char* const rt = (char*)malloc(size); + size_t const dstCapacitySeed = FUZZ_dataProducer_uint32(producer, 0, LZ4_compressBound(size)); - /* Restrict to remaining data from producer */ size = FUZZ_dataProducer_remainingBytes(producer); + size_t const dstCapacity = FUZZ_getRange_from_uint32(dstCapacitySeed, 0, LZ4_compressBound(size)); + + char* const dst = (char*)malloc(dstCapacity); + char* const rt = (char*)malloc(size); FUZZ_ASSERT(dst); FUZZ_ASSERT(rt); diff --git a/ossfuzz/compress_hc_fuzzer.c b/ossfuzz/compress_hc_fuzzer.c index 3e28f42..fac5e6f 100644 --- a/ossfuzz/compress_hc_fuzzer.c +++ b/ossfuzz/compress_hc_fuzzer.c @@ -17,19 +17,17 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size); - size_t const dstCapacitySeed = FUZZ_dataProducer_uint32_seed(producer, + size_t const dstCapacitySeed = FUZZ_dataProducer_uint32(producer, 0, LZ4_compressBound(size)); - size_t const levelSeed = FUZZ_dataProducer_uint32_seed(producer, + size_t const levelSeed = FUZZ_dataProducer_uint32(producer, LZ4HC_CLEVEL_MIN, LZ4HC_CLEVEL_MAX); - size_t const dstCapacity = FUZZ_dataProducer_uint32(dstCapacitySeed, - 0, FUZZ_dataProducer_remainingBytes(producer)); - int const level = FUZZ_dataProducer_uint32( - levelSeed, LZ4HC_CLEVEL_MIN, LZ4HC_CLEVEL_MAX); - char* const dst = (char*)malloc(dstCapacity); - char* const rt = (char*)malloc(size); - /* Restrict to remaining data from producer */ size = FUZZ_dataProducer_remainingBytes(producer); + size_t const dstCapacity = FUZZ_getRange_from_uint32(dstCapacitySeed, 0, size); + int const level = FUZZ_getRange_from_uint32(levelSeed, LZ4HC_CLEVEL_MIN, LZ4HC_CLEVEL_MAX); + + char* const dst = (char*)malloc(dstCapacity); + char* const rt = (char*)malloc(size); FUZZ_ASSERT(dst); FUZZ_ASSERT(rt); diff --git a/ossfuzz/decompress_frame_fuzzer.c b/ossfuzz/decompress_frame_fuzzer.c index 1308100..cf88579 100644 --- a/ossfuzz/decompress_frame_fuzzer.c +++ b/ossfuzz/decompress_frame_fuzzer.c @@ -31,24 +31,24 @@ static void decompress(LZ4F_dctx* dctx, void* dst, size_t dstCapacity, int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size); - size_t const dstCapacitySeed = FUZZ_dataProducer_uint32_seed(producer, + size_t const dstCapacitySeed = FUZZ_dataProducer_uint32(producer, 0, 4 * size); size_t const largeDictSize = 64 * 1024; - size_t const dictSizeSeed = FUZZ_dataProducer_uint32_seed(producer, + size_t const dictSizeSeed = FUZZ_dataProducer_uint32(producer, 0, largeDictSize); - size_t const dstCapacity = FUZZ_dataProducer_uint32( - dstCapacitySeed, 0, 4 * FUZZ_dataProducer_remainingBytes(producer)); - size_t const dictSize = FUZZ_dataProducer_uint32( + + size = FUZZ_dataProducer_remainingBytes(producer); + size_t const dstCapacity = FUZZ_getRange_from_uint32( + dstCapacitySeed, 0, 4 * size); + size_t const dictSize = FUZZ_getRange_from_uint32( dictSizeSeed, 0, largeDictSize); + char* const dst = (char*)malloc(dstCapacity); char* const dict = (char*)malloc(dictSize); LZ4F_decompressOptions_t opts; LZ4F_dctx* dctx; LZ4F_createDecompressionContext(&dctx, LZ4F_VERSION); - /* Restrict to remaining data from producer */ - size = FUZZ_dataProducer_remainingBytes(producer); - FUZZ_ASSERT(dctx); FUZZ_ASSERT(dst); FUZZ_ASSERT(dict); diff --git a/ossfuzz/decompress_fuzzer.c b/ossfuzz/decompress_fuzzer.c index 111f5b5..c2595b0 100644 --- a/ossfuzz/decompress_fuzzer.c +++ b/ossfuzz/decompress_fuzzer.c @@ -15,9 +15,11 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size); - size_t const dstCapacitySeed = FUZZ_dataProducer_uint32_seed(producer, 0, 4 * size); - size_t const dstCapacity = FUZZ_dataProducer_uint32(dstCapacitySeed, - 0, 4 * FUZZ_dataProducer_remainingBytes(producer)); + size_t const dstCapacitySeed = FUZZ_dataProducer_uint32(producer, 0, 4 * size); + + size = FUZZ_dataProducer_remainingBytes(producer); + size_t const dstCapacity = FUZZ_getRange_from_uint32(dstCapacitySeed, 0, 4 * size); + size_t const smallDictSize = size + 1; size_t const largeDictSize = 64 * 1024 - 1; size_t const dictSize = MAX(smallDictSize, largeDictSize); @@ -27,9 +29,6 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) char* const dataAfterDict = dict + dictSize; char* const smallDict = dataAfterDict - smallDictSize; - /* Restrict to remaining data from producer */ - size = FUZZ_dataProducer_remainingBytes(producer); - FUZZ_ASSERT(dst); FUZZ_ASSERT(dict); diff --git a/ossfuzz/fuzz_data_producer.c b/ossfuzz/fuzz_data_producer.c index 70e42ea..9557f58 100644 --- a/ossfuzz/fuzz_data_producer.c +++ b/ossfuzz/fuzz_data_producer.c @@ -17,7 +17,7 @@ FUZZ_dataProducer_t *FUZZ_dataProducer_create(const uint8_t *data, size_t size) void FUZZ_dataProducer_free(FUZZ_dataProducer_t *producer) { free(producer); } -uint32_t FUZZ_dataProducer_uint32_seed(FUZZ_dataProducer_t *producer, uint32_t min, +uint32_t FUZZ_dataProducer_uint32(FUZZ_dataProducer_t *producer, uint32_t min, uint32_t max) { FUZZ_ASSERT(min <= max); @@ -35,7 +35,7 @@ uint32_t FUZZ_dataProducer_uint32_seed(FUZZ_dataProducer_t *producer, uint32_t m return result; } -uint32_t FUZZ_dataProducer_uint32(uint32_t seed, uint32_t min, uint32_t max) +uint32_t FUZZ_getRange_from_uint32(uint32_t seed, uint32_t min, uint32_t max) { uint32_t range = max - min; if (range == 0xffffffff) { @@ -44,24 +44,24 @@ uint32_t FUZZ_dataProducer_uint32(uint32_t seed, uint32_t min, uint32_t max) return min + seed % (range + 1); } -uint32_t FUZZ_dataProducer_uint32NonAdaptive(FUZZ_dataProducer_t* producer, +uint32_t FUZZ_dataProducer_range32(FUZZ_dataProducer_t* producer, uint32_t min, uint32_t max) { - size_t const seed = FUZZ_dataProducer_uint32_seed(producer, min, max); - return FUZZ_dataProducer_uint32(seed, min, max); + size_t const seed = FUZZ_dataProducer_uint32(producer, min, max); + return FUZZ_getRange_from_uint32(seed, min, max); } LZ4F_frameInfo_t FUZZ_dataProducer_frameInfo(FUZZ_dataProducer_t* producer) { LZ4F_frameInfo_t info = LZ4F_INIT_FRAMEINFO; - info.blockSizeID = FUZZ_dataProducer_uint32NonAdaptive(producer, LZ4F_max64KB - 1, LZ4F_max4MB); + info.blockSizeID = FUZZ_dataProducer_range32(producer, LZ4F_max64KB - 1, LZ4F_max4MB); if (info.blockSizeID < LZ4F_max64KB) { info.blockSizeID = LZ4F_default; } - info.blockMode = FUZZ_dataProducer_uint32NonAdaptive(producer, LZ4F_blockLinked, LZ4F_blockIndependent); - info.contentChecksumFlag = FUZZ_dataProducer_uint32NonAdaptive(producer, LZ4F_noContentChecksum, + info.blockMode = FUZZ_dataProducer_range32(producer, LZ4F_blockLinked, LZ4F_blockIndependent); + info.contentChecksumFlag = FUZZ_dataProducer_range32(producer, LZ4F_noContentChecksum, LZ4F_contentChecksumEnabled); - info.blockChecksumFlag = FUZZ_dataProducer_uint32NonAdaptive(producer, LZ4F_noBlockChecksum, + info.blockChecksumFlag = FUZZ_dataProducer_range32(producer, LZ4F_noBlockChecksum, LZ4F_blockChecksumEnabled); return info; } @@ -70,9 +70,9 @@ LZ4F_preferences_t FUZZ_dataProducer_preferences(FUZZ_dataProducer_t* producer) { LZ4F_preferences_t prefs = LZ4F_INIT_PREFERENCES; prefs.frameInfo = FUZZ_dataProducer_frameInfo(producer); - prefs.compressionLevel = FUZZ_dataProducer_uint32NonAdaptive(producer, 0, LZ4HC_CLEVEL_MAX + 3) - 3; - prefs.autoFlush = FUZZ_dataProducer_uint32NonAdaptive(producer, 0, 1); - prefs.favorDecSpeed = FUZZ_dataProducer_uint32NonAdaptive(producer, 0, 1); + prefs.compressionLevel = FUZZ_dataProducer_range32(producer, 0, LZ4HC_CLEVEL_MAX + 3) - 3; + prefs.autoFlush = FUZZ_dataProducer_range32(producer, 0, 1); + prefs.favorDecSpeed = FUZZ_dataProducer_range32(producer, 0, 1); return prefs; } diff --git a/ossfuzz/fuzz_data_producer.h b/ossfuzz/fuzz_data_producer.h index e12f899..db18fd2 100644 --- a/ossfuzz/fuzz_data_producer.h +++ b/ossfuzz/fuzz_data_producer.h @@ -17,14 +17,14 @@ FUZZ_dataProducer_t *FUZZ_dataProducer_create(const uint8_t *data, size_t size); void FUZZ_dataProducer_free(FUZZ_dataProducer_t *producer); /* Returns a seed value for the function after this one to consume */ -uint32_t FUZZ_dataProducer_uint32_seed(FUZZ_dataProducer_t *producer, uint32_t min, +uint32_t FUZZ_dataProducer_uint32(FUZZ_dataProducer_t *producer, uint32_t min, uint32_t max); /* Returns value between [min, max] */ -uint32_t FUZZ_dataProducer_uint32(uint32_t seed, uint32_t min, uint32_t max); +uint32_t FUZZ_getRange_from_uint32(uint32_t seed, uint32_t min, uint32_t max); /* Combination of above two functions for non adaptive use cases. ie where size is not involved */ -uint32_t FUZZ_dataProducer_uint32NonAdaptive(FUZZ_dataProducer_t *producer, uint32_t min, +uint32_t FUZZ_dataProducer_range32(FUZZ_dataProducer_t *producer, uint32_t min, uint32_t max); /* Returns lz4 preferences */ diff --git a/ossfuzz/round_trip_frame_fuzzer.c b/ossfuzz/round_trip_frame_fuzzer.c index c34553d..aea13bb 100644 --- a/ossfuzz/round_trip_frame_fuzzer.c +++ b/ossfuzz/round_trip_frame_fuzzer.c @@ -18,17 +18,16 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { FUZZ_dataProducer_t* producer = FUZZ_dataProducer_create(data, size); LZ4F_preferences_t const prefs = FUZZ_dataProducer_preferences(producer); - size_t const dstCapacity = LZ4F_compressFrameBound( - LZ4_compressBound(FUZZ_dataProducer_remainingBytes(producer)), &prefs); + + size = FUZZ_dataProducer_remainingBytes(producer); + size_t const dstCapacity = LZ4F_compressFrameBound(LZ4_compressBound(size), &prefs); + char* const dst = (char*)malloc(dstCapacity); char* const rt = (char*)malloc(FUZZ_dataProducer_remainingBytes(producer)); FUZZ_ASSERT(dst); FUZZ_ASSERT(rt); - /* Restrict to remaining data from producer */ - size = FUZZ_dataProducer_remainingBytes(producer); - /* Compression must succeed and round trip correctly. */ size_t const dstSize = LZ4F_compressFrame(dst, dstCapacity, data, size, &prefs); diff --git a/ossfuzz/round_trip_fuzzer.c b/ossfuzz/round_trip_fuzzer.c index 85774fe..80cd910 100644 --- a/ossfuzz/round_trip_fuzzer.c +++ b/ossfuzz/round_trip_fuzzer.c @@ -15,19 +15,19 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size); - size_t const partialCapacitySeed = FUZZ_dataProducer_uint32_seed(producer, 0, size); - size_t const partialCapacity = FUZZ_dataProducer_uint32(partialCapacitySeed, - 0, FUZZ_dataProducer_remainingBytes(producer)); + size_t const partialCapacitySeed = FUZZ_dataProducer_uint32(producer, 0, size); + + size = FUZZ_dataProducer_remainingBytes(producer); + size_t const partialCapacity = FUZZ_getRange_from_uint32(partialCapacitySeed, + 0, size); size_t const dstCapacity = LZ4_compressBound(size); + char* const dst = (char*)malloc(dstCapacity); char* const rt = (char*)malloc(size); FUZZ_ASSERT(dst); FUZZ_ASSERT(rt); - /* Restrict to remaining data from producer */ - size = FUZZ_dataProducer_remainingBytes(producer); - /* Compression must succeed and round trip correctly. */ int const dstSize = LZ4_compress_default((const char*)data, dst, size, dstCapacity); diff --git a/ossfuzz/round_trip_hc_fuzzer.c b/ossfuzz/round_trip_hc_fuzzer.c index cc9c302..75ca8ec 100644 --- a/ossfuzz/round_trip_hc_fuzzer.c +++ b/ossfuzz/round_trip_hc_fuzzer.c @@ -16,15 +16,14 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size); - int const level = FUZZ_dataProducer_uint32NonAdaptive(producer, + int const level = FUZZ_dataProducer_range32(producer, LZ4HC_CLEVEL_MIN, LZ4HC_CLEVEL_MAX); - size_t const dstCapacity = LZ4_compressBound( - FUZZ_dataProducer_remainingBytes(producer)); - char* const dst = (char*)malloc(dstCapacity); - char* const rt = (char*)malloc(size); - /* Restrict to remaining data from producer */ size = FUZZ_dataProducer_remainingBytes(producer); + size_t const dstCapacity = LZ4_compressBound(size); + + char* const dst = (char*)malloc(dstCapacity); + char* const rt = (char*)malloc(size); FUZZ_ASSERT(dst); FUZZ_ASSERT(rt); -- cgit v1.2.3 From 8edc5879d029459074a9c7bd9489dabf5b510bf6 Mon Sep 17 00:00:00 2001 From: Bimba Shrestha Date: Fri, 13 Sep 2019 18:08:58 -0700 Subject: Retreiving 32 bits from the end for fuzzer --- ossfuzz/compress_frame_fuzzer.c | 8 +++----- ossfuzz/compress_fuzzer.c | 7 ++++--- ossfuzz/compress_hc_fuzzer.c | 8 +++----- ossfuzz/decompress_frame_fuzzer.c | 10 ++++------ ossfuzz/decompress_fuzzer.c | 5 ++--- ossfuzz/fuzz_data_producer.c | 30 +++++++++++++----------------- ossfuzz/fuzz_data_producer.h | 5 ++--- ossfuzz/round_trip_frame_fuzzer.c | 3 +-- ossfuzz/round_trip_fuzzer.c | 7 +++---- ossfuzz/round_trip_hc_fuzzer.c | 3 +-- 10 files changed, 36 insertions(+), 50 deletions(-) diff --git a/ossfuzz/compress_frame_fuzzer.c b/ossfuzz/compress_frame_fuzzer.c index 30f0448..668d7c3 100644 --- a/ossfuzz/compress_frame_fuzzer.c +++ b/ossfuzz/compress_frame_fuzzer.c @@ -19,13 +19,11 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, LZ4_compressBound(size)); LZ4F_preferences_t const prefs = FUZZ_dataProducer_preferences(producer); - + size_t const dstCapacitySeed = FUZZ_dataProducer_retrieve32(producer); size = FUZZ_dataProducer_remainingBytes(producer); - size_t const compressBound = LZ4F_compressFrameBound(size, &prefs); - size_t const dstCapacitySeed = FUZZ_dataProducer_uint32(producer, 0, compressBound); - size = FUZZ_dataProducer_remainingBytes(producer); - size_t const dstCapacity = FUZZ_getRange_from_uint32(dstCapacitySeed, 0, size); + size_t const compressBound = LZ4F_compressFrameBound(size, &prefs); + size_t const dstCapacity = FUZZ_getRange_from_uint32(dstCapacitySeed, 0, compressBound); char* const dst = (char*)malloc(dstCapacity); char* const rt = (char*)malloc(size); diff --git a/ossfuzz/compress_fuzzer.c b/ossfuzz/compress_fuzzer.c index fac7dab..edc8aad 100644 --- a/ossfuzz/compress_fuzzer.c +++ b/ossfuzz/compress_fuzzer.c @@ -16,10 +16,11 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size); - size_t const dstCapacitySeed = FUZZ_dataProducer_uint32(producer, 0, LZ4_compressBound(size)); - + size_t const dstCapacitySeed = FUZZ_dataProducer_retrieve32(producer); size = FUZZ_dataProducer_remainingBytes(producer); - size_t const dstCapacity = FUZZ_getRange_from_uint32(dstCapacitySeed, 0, LZ4_compressBound(size)); + + size_t const compressBound = LZ4_compressBound(size); + size_t const dstCapacity = FUZZ_getRange_from_uint32(dstCapacitySeed, 0, compressBound); char* const dst = (char*)malloc(dstCapacity); char* const rt = (char*)malloc(size); diff --git a/ossfuzz/compress_hc_fuzzer.c b/ossfuzz/compress_hc_fuzzer.c index fac5e6f..7d8e45a 100644 --- a/ossfuzz/compress_hc_fuzzer.c +++ b/ossfuzz/compress_hc_fuzzer.c @@ -17,12 +17,10 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size); - size_t const dstCapacitySeed = FUZZ_dataProducer_uint32(producer, - 0, LZ4_compressBound(size)); - size_t const levelSeed = FUZZ_dataProducer_uint32(producer, - LZ4HC_CLEVEL_MIN, LZ4HC_CLEVEL_MAX); - + size_t const dstCapacitySeed = FUZZ_dataProducer_retrieve32(producer); + size_t const levelSeed = FUZZ_dataProducer_retrieve32(producer); size = FUZZ_dataProducer_remainingBytes(producer); + size_t const dstCapacity = FUZZ_getRange_from_uint32(dstCapacitySeed, 0, size); int const level = FUZZ_getRange_from_uint32(levelSeed, LZ4HC_CLEVEL_MIN, LZ4HC_CLEVEL_MAX); diff --git a/ossfuzz/decompress_frame_fuzzer.c b/ossfuzz/decompress_frame_fuzzer.c index cf88579..0fcbb16 100644 --- a/ossfuzz/decompress_frame_fuzzer.c +++ b/ossfuzz/decompress_frame_fuzzer.c @@ -31,15 +31,13 @@ static void decompress(LZ4F_dctx* dctx, void* dst, size_t dstCapacity, int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size); - size_t const dstCapacitySeed = FUZZ_dataProducer_uint32(producer, - 0, 4 * size); - size_t const largeDictSize = 64 * 1024; - size_t const dictSizeSeed = FUZZ_dataProducer_uint32(producer, - 0, largeDictSize); - + size_t const dstCapacitySeed = FUZZ_dataProducer_retrieve32(producer); + size_t const dictSizeSeed = FUZZ_dataProducer_retrieve32(producer); size = FUZZ_dataProducer_remainingBytes(producer); + size_t const dstCapacity = FUZZ_getRange_from_uint32( dstCapacitySeed, 0, 4 * size); + size_t const largeDictSize = 64 * 1024; size_t const dictSize = FUZZ_getRange_from_uint32( dictSizeSeed, 0, largeDictSize); diff --git a/ossfuzz/decompress_fuzzer.c b/ossfuzz/decompress_fuzzer.c index c2595b0..6f48e30 100644 --- a/ossfuzz/decompress_fuzzer.c +++ b/ossfuzz/decompress_fuzzer.c @@ -15,11 +15,10 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size); - size_t const dstCapacitySeed = FUZZ_dataProducer_uint32(producer, 0, 4 * size); - + size_t const dstCapacitySeed = FUZZ_dataProducer_retrieve32(producer); size = FUZZ_dataProducer_remainingBytes(producer); - size_t const dstCapacity = FUZZ_getRange_from_uint32(dstCapacitySeed, 0, 4 * size); + size_t const dstCapacity = FUZZ_getRange_from_uint32(dstCapacitySeed, 0, 4 * size); size_t const smallDictSize = size + 1; size_t const largeDictSize = 64 * 1024 - 1; size_t const dictSize = MAX(smallDictSize, largeDictSize); diff --git a/ossfuzz/fuzz_data_producer.c b/ossfuzz/fuzz_data_producer.c index 9557f58..cc06958 100644 --- a/ossfuzz/fuzz_data_producer.c +++ b/ossfuzz/fuzz_data_producer.c @@ -17,22 +17,18 @@ FUZZ_dataProducer_t *FUZZ_dataProducer_create(const uint8_t *data, size_t size) void FUZZ_dataProducer_free(FUZZ_dataProducer_t *producer) { free(producer); } -uint32_t FUZZ_dataProducer_uint32(FUZZ_dataProducer_t *producer, uint32_t min, - uint32_t max) { - FUZZ_ASSERT(min <= max); - - uint32_t range = max - min; - uint32_t rolling = range; - uint32_t result = 0; - - while (rolling > 0 && producer->size > 0) { - uint8_t next = *(producer->data + producer->size - 1); - producer->size -= 1; - result = (result << 8) | next; - rolling >>= 8; - } - - return result; +uint32_t FUZZ_dataProducer_retrieve32(FUZZ_dataProducer_t *producer) { + const uint8_t* data = producer->data; + const size_t size = producer->size; + if (size == 0) { + return 0; + } else if (size < 4) { + producer->size -= 1; + return (uint32_t)data[size - 1]; + } else { + producer->size -= 4; + return *(data + size - 4); + } } uint32_t FUZZ_getRange_from_uint32(uint32_t seed, uint32_t min, uint32_t max) @@ -47,7 +43,7 @@ uint32_t FUZZ_getRange_from_uint32(uint32_t seed, uint32_t min, uint32_t max) uint32_t FUZZ_dataProducer_range32(FUZZ_dataProducer_t* producer, uint32_t min, uint32_t max) { - size_t const seed = FUZZ_dataProducer_uint32(producer, min, max); + size_t const seed = FUZZ_dataProducer_retrieve32(producer); return FUZZ_getRange_from_uint32(seed, min, max); } diff --git a/ossfuzz/fuzz_data_producer.h b/ossfuzz/fuzz_data_producer.h index db18fd2..b96dcba 100644 --- a/ossfuzz/fuzz_data_producer.h +++ b/ossfuzz/fuzz_data_producer.h @@ -16,9 +16,8 @@ FUZZ_dataProducer_t *FUZZ_dataProducer_create(const uint8_t *data, size_t size); /* Frees the data producer */ void FUZZ_dataProducer_free(FUZZ_dataProducer_t *producer); -/* Returns a seed value for the function after this one to consume */ -uint32_t FUZZ_dataProducer_uint32(FUZZ_dataProducer_t *producer, uint32_t min, - uint32_t max); +/* Returns 32 bits from the end of data */ +uint32_t FUZZ_dataProducer_retrieve32(FUZZ_dataProducer_t *producer); /* Returns value between [min, max] */ uint32_t FUZZ_getRange_from_uint32(uint32_t seed, uint32_t min, uint32_t max); diff --git a/ossfuzz/round_trip_frame_fuzzer.c b/ossfuzz/round_trip_frame_fuzzer.c index aea13bb..149542d 100644 --- a/ossfuzz/round_trip_frame_fuzzer.c +++ b/ossfuzz/round_trip_frame_fuzzer.c @@ -18,10 +18,9 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { FUZZ_dataProducer_t* producer = FUZZ_dataProducer_create(data, size); LZ4F_preferences_t const prefs = FUZZ_dataProducer_preferences(producer); - size = FUZZ_dataProducer_remainingBytes(producer); - size_t const dstCapacity = LZ4F_compressFrameBound(LZ4_compressBound(size), &prefs); + size_t const dstCapacity = LZ4F_compressFrameBound(LZ4_compressBound(size), &prefs); char* const dst = (char*)malloc(dstCapacity); char* const rt = (char*)malloc(FUZZ_dataProducer_remainingBytes(producer)); diff --git a/ossfuzz/round_trip_fuzzer.c b/ossfuzz/round_trip_fuzzer.c index 80cd910..6307058 100644 --- a/ossfuzz/round_trip_fuzzer.c +++ b/ossfuzz/round_trip_fuzzer.c @@ -15,11 +15,10 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size); - size_t const partialCapacitySeed = FUZZ_dataProducer_uint32(producer, 0, size); - + size_t const partialCapacitySeed = FUZZ_dataProducer_retrieve32(producer); size = FUZZ_dataProducer_remainingBytes(producer); - size_t const partialCapacity = FUZZ_getRange_from_uint32(partialCapacitySeed, - 0, size); + + size_t const partialCapacity = FUZZ_getRange_from_uint32(partialCapacitySeed, 0, size); size_t const dstCapacity = LZ4_compressBound(size); char* const dst = (char*)malloc(dstCapacity); diff --git a/ossfuzz/round_trip_hc_fuzzer.c b/ossfuzz/round_trip_hc_fuzzer.c index 75ca8ec..7d03ee2 100644 --- a/ossfuzz/round_trip_hc_fuzzer.c +++ b/ossfuzz/round_trip_hc_fuzzer.c @@ -18,10 +18,9 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size); int const level = FUZZ_dataProducer_range32(producer, LZ4HC_CLEVEL_MIN, LZ4HC_CLEVEL_MAX); - size = FUZZ_dataProducer_remainingBytes(producer); - size_t const dstCapacity = LZ4_compressBound(size); + size_t const dstCapacity = LZ4_compressBound(size); char* const dst = (char*)malloc(dstCapacity); char* const rt = (char*)malloc(size); -- cgit v1.2.3 From c5a83c1a48722108460f8542d92703fd5f59e296 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Mon, 16 Sep 2019 09:20:02 +1000 Subject: Have read_variable_length use fixed size types Otherwise, the output from decoding LZ4-compressed input could be platform dependent. Also add a compile-time check to confirm the existing code's assumptions that, if isn't used, then sizeof(int) == 4. Updates #792 --- lib/lz4.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 9808d70..85c3322 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -249,6 +249,10 @@ static int g_debuglog_enable = 1; typedef uint64_t U64; typedef uintptr_t uptrval; #else +# include +# if UINT_MAX != 4294967295UL +# error "LZ4 code (when not C++ or C99) assumes that sizeof(int) == 4" +# endif typedef unsigned char BYTE; typedef unsigned short U16; typedef unsigned int U32; @@ -1625,8 +1629,8 @@ typedef enum { loop_error = -2, initial_error = -1, ok = 0 } variable_length_err LZ4_FORCE_INLINE unsigned read_variable_length(const BYTE**ip, const BYTE* lencheck, int loop_check, int initial_check, variable_length_error* error) { - unsigned length = 0; - unsigned s; + U32 length = 0; + U32 s; if (initial_check && unlikely((*ip) >= lencheck)) { /* overflow detection */ *error = initial_error; return length; -- cgit v1.2.3 From 192161e97e020b165a3cfc7821439e895ec194c8 Mon Sep 17 00:00:00 2001 From: Bimba Shrestha Date: Mon, 23 Sep 2019 11:54:56 -0700 Subject: Using size instead of LZ4_compressBound(size) <- causes heap overflow --- ossfuzz/compress_frame_fuzzer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ossfuzz/compress_frame_fuzzer.c b/ossfuzz/compress_frame_fuzzer.c index 668d7c3..bb14fc2 100644 --- a/ossfuzz/compress_frame_fuzzer.c +++ b/ossfuzz/compress_frame_fuzzer.c @@ -17,7 +17,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { - FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, LZ4_compressBound(size)); + FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size); LZ4F_preferences_t const prefs = FUZZ_dataProducer_preferences(producer); size_t const dstCapacitySeed = FUZZ_dataProducer_retrieve32(producer); size = FUZZ_dataProducer_remainingBytes(producer); -- cgit v1.2.3 From b6623e710d6bea36634d82547b0d89021b0d6549 Mon Sep 17 00:00:00 2001 From: Julius Michaelis Date: Thu, 26 Sep 2019 17:23:53 +0900 Subject: meson: move one layer deeper to allow easy construction of a wrap file --- contrib/meson/GetLz4LibraryVersion.py | 39 ------- contrib/meson/InstallSymlink.py | 55 ---------- contrib/meson/contrib/gen_manual/meson.build | 43 -------- contrib/meson/contrib/meson.build | 10 -- contrib/meson/examples/meson.build | 49 --------- contrib/meson/lib/meson.build | 57 ---------- contrib/meson/meson.build | 114 +------------------- contrib/meson/meson/GetLz4LibraryVersion.py | 39 +++++++ contrib/meson/meson/InstallSymlink.py | 55 ++++++++++ contrib/meson/meson/contrib/gen_manual/meson.build | 43 ++++++++ contrib/meson/meson/contrib/meson.build | 10 ++ contrib/meson/meson/examples/meson.build | 49 +++++++++ contrib/meson/meson/lib/meson.build | 57 ++++++++++ contrib/meson/meson/meson.build | 118 +++++++++++++++++++++ contrib/meson/meson/programs/meson.build | 52 +++++++++ contrib/meson/meson/tests/meson.build | 93 ++++++++++++++++ contrib/meson/programs/meson.build | 52 --------- contrib/meson/tests/meson.build | 93 ---------------- 18 files changed, 521 insertions(+), 507 deletions(-) delete mode 100644 contrib/meson/GetLz4LibraryVersion.py delete mode 100644 contrib/meson/InstallSymlink.py delete mode 100644 contrib/meson/contrib/gen_manual/meson.build delete mode 100644 contrib/meson/contrib/meson.build delete mode 100644 contrib/meson/examples/meson.build delete mode 100644 contrib/meson/lib/meson.build create mode 100644 contrib/meson/meson/GetLz4LibraryVersion.py create mode 100644 contrib/meson/meson/InstallSymlink.py create mode 100644 contrib/meson/meson/contrib/gen_manual/meson.build create mode 100644 contrib/meson/meson/contrib/meson.build create mode 100644 contrib/meson/meson/examples/meson.build create mode 100644 contrib/meson/meson/lib/meson.build create mode 100644 contrib/meson/meson/meson.build create mode 100644 contrib/meson/meson/programs/meson.build create mode 100644 contrib/meson/meson/tests/meson.build delete mode 100644 contrib/meson/programs/meson.build delete mode 100644 contrib/meson/tests/meson.build diff --git a/contrib/meson/GetLz4LibraryVersion.py b/contrib/meson/GetLz4LibraryVersion.py deleted file mode 100644 index d8abfcb..0000000 --- a/contrib/meson/GetLz4LibraryVersion.py +++ /dev/null @@ -1,39 +0,0 @@ -#!/usr/bin/env python3 -# ############################################################################# -# Copyright (c) 2018-present lzutao -# All rights reserved. -# -# This source code is licensed under both the BSD-style license (found in the -# LICENSE file in the root directory of this source tree) and the GPLv2 (found -# in the COPYING file in the root directory of this source tree). -# ############################################################################# -import re - - -def find_version_tuple(filepath): - version_file_data = None - with open(filepath) as fd: - version_file_data = fd.read() - - patterns = r"""#\s*define\s+LZ4_VERSION_MAJOR\s+([0-9]+).*$ -#\s*define\s+LZ4_VERSION_MINOR\s+([0-9]+).*$ -#\s*define\s+LZ4_VERSION_RELEASE\s+([0-9]+).*$ -""" - regex = re.compile(patterns, re.MULTILINE) - version_match = regex.search(version_file_data) - if version_match: - return version_match.groups() - raise Exception("Unable to find version string.") - - -def main(): - import argparse - parser = argparse.ArgumentParser(description='Print lz4 version from lib/lz4.h') - parser.add_argument('file', help='path to lib/lz4.h') - args = parser.parse_args() - version_tuple = find_version_tuple(args.file) - print('.'.join(version_tuple)) - - -if __name__ == '__main__': - main() diff --git a/contrib/meson/InstallSymlink.py b/contrib/meson/InstallSymlink.py deleted file mode 100644 index 3f2998c..0000000 --- a/contrib/meson/InstallSymlink.py +++ /dev/null @@ -1,55 +0,0 @@ -#!/usr/bin/env python3 -# ############################################################################# -# Copyright (c) 2018-present lzutao -# All rights reserved. -# -# This source code is licensed under both the BSD-style license (found in the -# LICENSE file in the root directory of this source tree) and the GPLv2 (found -# in the COPYING file in the root directory of this source tree). -# ############################################################################# -# This file should be synced with https://github.com/lzutao/meson-symlink - -import os -import pathlib # since Python 3.4 - - -def install_symlink(src, dst, install_dir, dst_is_dir=False, dir_mode=0o777): - if not install_dir.exists(): - install_dir.mkdir(mode=dir_mode, parents=True, exist_ok=True) - if not install_dir.is_dir(): - raise NotADirectoryError(install_dir) - - new_dst = install_dir.joinpath(dst) - if new_dst.is_symlink() and os.readlink(new_dst) == src: - print('File exists: {!r} -> {!r}'.format(new_dst, src)) - return - print('Installing symlink {!r} -> {!r}'.format(new_dst, src)) - new_dst.symlink_to(src, target_is_directory=dst_is_dir) - - -def main(): - import argparse - parser = argparse.ArgumentParser(description='Install a symlink', - usage='{0} [-h] [-d] [-m MODE] source dest install_dir\n\n' - 'example:\n' - ' {0} dash sh /bin'.format(pathlib.Path(__file__).name)) - parser.add_argument('source', help='target to link') - parser.add_argument('dest', help='link name') - parser.add_argument('install_dir', help='installation directory') - parser.add_argument('-d', '--isdir', - action='store_true', - help='dest is a directory') - parser.add_argument('-m', '--mode', - help='directory mode on creating if not exist', - default='0o755') - args = parser.parse_args() - - dir_mode = int(args.mode, 8) - - meson_destdir = os.environ.get('MESON_INSTALL_DESTDIR_PREFIX', default='') - install_dir = pathlib.Path(meson_destdir, args.install_dir) - install_symlink(args.source, args.dest, install_dir, args.isdir, dir_mode) - - -if __name__ == '__main__': - main() diff --git a/contrib/meson/contrib/gen_manual/meson.build b/contrib/meson/contrib/gen_manual/meson.build deleted file mode 100644 index 38180e9..0000000 --- a/contrib/meson/contrib/gen_manual/meson.build +++ /dev/null @@ -1,43 +0,0 @@ -# ############################################################################# -# Copyright (c) 2018-present lzutao -# All rights reserved. -# -# This source code is licensed under both the BSD-style license (found in the -# LICENSE file in the root directory of this source tree) and the GPLv2 (found -# in the COPYING file in the root directory of this source tree). -# ############################################################################# - -lz4_root_dir = '../../../..' - -add_languages('cpp') -cxx = meson.get_compiler('cpp') - -gen_manual_includes = include_directories(join_paths(lz4_root_dir, 'contrib/gen_manual')) - -gen_manual_cppflags = cxx.get_supported_arguments(['-Wextra', '-Wcast-qual', - '-Wcast-align', '-Wshadow', '-Wstrict-aliasing=1', '-Wswitch-enum', - '-Wno-comment']) - -gen_manual = executable('gen_manual', - join_paths(lz4_root_dir, 'contrib/gen_manual/gen_manual.cpp'), - cpp_args: gen_manual_cppflags, - include_directories: gen_manual_includes, - native: true, - install: false) - -# Update lz4 manual -lz4_manual_html = custom_target('lz4_manual.html', - output : 'lz4_manual.html', - command : [gen_manual, - lz4_version, - join_paths(meson.current_source_dir(), lz4_root_dir, 'lib/lz4.h'), - '@OUTPUT@'], - install : false) -# Update lz4frame manual -lz4_manual_html = custom_target('lz4frame_manual.html', - output : 'lz4frame_manual.html', - command : [gen_manual, - lz4_version, - join_paths(meson.current_source_dir(), lz4_root_dir, 'lib/lz4frame.h'), - '@OUTPUT@'], - install : false) diff --git a/contrib/meson/contrib/meson.build b/contrib/meson/contrib/meson.build deleted file mode 100644 index 5249a4c..0000000 --- a/contrib/meson/contrib/meson.build +++ /dev/null @@ -1,10 +0,0 @@ -# ############################################################################# -# Copyright (c) 2018-present lzutao -# All rights reserved. -# -# This source code is licensed under both the BSD-style license (found in the -# LICENSE file in the root directory of this source tree) and the GPLv2 (found -# in the COPYING file in the root directory of this source tree). -# ############################################################################# - -subdir('gen_manual') diff --git a/contrib/meson/examples/meson.build b/contrib/meson/examples/meson.build deleted file mode 100644 index 3c13214..0000000 --- a/contrib/meson/examples/meson.build +++ /dev/null @@ -1,49 +0,0 @@ -# ############################################################################# -# Copyright (c) 2018-present lzutao -# All rights reserved. -# -# This source code is licensed under both the BSD-style license (found in the -# LICENSE file in the root directory of this source tree) and the GPLv2 (found -# in the COPYING file in the root directory of this source tree). -# ############################################################################# - -lz4_root_dir = '../../..' - -#examples_c_args = ['-Wextra', '-Wundef', '-Wshadow', '-Wcast-align', '-Wstrict-prototypes'] - -printVersion = executable('printVersion', - join_paths(lz4_root_dir, 'examples/printVersion.c'), - dependencies: liblz4_dep, - install: false) -doubleBuffer = executable('doubleBuffer', - join_paths(lz4_root_dir, 'examples/blockStreaming_doubleBuffer.c'), - dependencies: liblz4_dep, - install: false) -dictionaryRandomAccess = executable('dictionaryRandomAccess', - join_paths(lz4_root_dir, 'examples/dictionaryRandomAccess.c'), - dependencies: liblz4_dep, - install: false) -ringBuffer = executable('ringBuffer', - join_paths(lz4_root_dir, 'examples/blockStreaming_ringBuffer.c'), - dependencies: liblz4_dep, - install: false) -ringBufferHC = executable('ringBufferHC', - join_paths(lz4_root_dir, 'examples/HCStreaming_ringBuffer.c'), - dependencies: liblz4_dep, - install: false) -lineCompress = executable('lineCompress', - join_paths(lz4_root_dir, 'examples/blockStreaming_lineByLine.c'), - dependencies: liblz4_dep, - install: false) -frameCompress = executable('frameCompress', - join_paths(lz4_root_dir, 'examples/frameCompress.c'), - dependencies: liblz4_dep, - install: false) -compressFunctions = executable('compressFunctions', - join_paths(lz4_root_dir, 'examples/compress_functions.c'), - dependencies: liblz4_dep, - install: false) -simpleBuffer = executable('simpleBuffer', - join_paths(lz4_root_dir, 'examples/simple_buffer.c'), - dependencies: liblz4_dep, - install: false) diff --git a/contrib/meson/lib/meson.build b/contrib/meson/lib/meson.build deleted file mode 100644 index e782334..0000000 --- a/contrib/meson/lib/meson.build +++ /dev/null @@ -1,57 +0,0 @@ -# ############################################################################# -# Copyright (c) 2018-present lzutao -# All rights reserved. -# -# This source code is licensed under both the BSD-style license (found in the -# LICENSE file in the root directory of this source tree) and the GPLv2 (found -# in the COPYING file in the root directory of this source tree). -# ############################################################################# - -lz4_root_dir = '../../..' - -liblz4_includes = [include_directories(join_paths(lz4_root_dir, 'lib'))] -liblz4_sources = [join_paths(lz4_root_dir, 'lib/lz4.c'), - join_paths(lz4_root_dir, 'lib/lz4frame.c'), - join_paths(lz4_root_dir, 'lib/lz4hc.c'), - join_paths(lz4_root_dir, 'lib/xxhash.c')] -liblz4_c_args = [] - -liblz4_debug_cflags = [] -if use_debug - liblz4_c_args += '-DLZ4_DEBUG=@0@'.format(debug_level) - if [compiler_gcc, compiler_clang].contains(cc_id) - liblz4_debug_cflags = ['-Wextra', '-Wcast-qual', '-Wcast-align', '-Wshadow', - '-Wswitch-enum', '-Wdeclaration-after-statement', '-Wstrict-prototypes', - '-Wundef', '-Wpointer-arith', '-Wstrict-aliasing=1'] - endif -endif -liblz4_c_args += cc.get_supported_arguments(liblz4_debug_cflags) - -if host_machine_os == os_windows and default_library != 'static' - liblz4_c_args += '-DLZ4_DLL_EXPORT=1' -endif - -liblz4 = library('lz4', - liblz4_sources, - include_directories: liblz4_includes, - c_args: liblz4_c_args, - install: true, - version: lz4_libversion) - -liblz4_dep = declare_dependency(link_with: liblz4, - include_directories: liblz4_includes) - -pkgconfig.generate(liblz4, - name: 'lz4', - filebase: 'liblz4', - description: 'extremely fast lossless compression algorithm library', - version: lz4_libversion, - url: 'http://www.lz4.org/') - -install_headers(join_paths(lz4_root_dir, 'lib/lz4.h'), - join_paths(lz4_root_dir, 'lib/lz4hc.h'), - join_paths(lz4_root_dir, 'lib/lz4frame.h')) - -if default_library != 'shared' - install_headers(join_paths(lz4_root_dir, 'lib/lz4frame_static.h')) -endif diff --git a/contrib/meson/meson.build b/contrib/meson/meson.build index 65a4c26..d1e97d9 100644 --- a/contrib/meson/meson.build +++ b/contrib/meson/meson.build @@ -7,6 +7,10 @@ # in the COPYING file in the root directory of this source tree). # ############################################################################# +# This is a dummy meson file. +# The intention is that it can be easily moved to the root of the project +# (together with meson_options.txt) and packaged for wrapdb. + project('lz4', ['c'], license: ['BSD', 'GPLv2'], default_options : ['c_std=c99', @@ -14,112 +18,4 @@ project('lz4', ['c'], version: 'DUMMY', meson_version: '>=0.47.0') -cc = meson.get_compiler('c') -pkgconfig = import('pkgconfig') -python3 = import('python').find_installation() -c_std = get_option('c_std') -default_library = get_option('default_library') - -host_machine_os = host_machine.system() -os_windows = 'windows' -os_linux = 'linux' -os_darwin = 'darwin' -os_freebsd = 'freebsd' -os_sun = 'sunos' - -cc_id = cc.get_id() -compiler_gcc = 'gcc' -compiler_clang = 'clang' -compiler_msvc = 'msvc' - -lz4_version = meson.project_version() - -lz4_h_file = join_paths(meson.current_source_dir(), '../../lib/lz4.h') -GetLz4LibraryVersion_py = files('GetLz4LibraryVersion.py') -r = run_command(python3, GetLz4LibraryVersion_py, lz4_h_file) -if r.returncode() == 0 - lz4_version = r.stdout().strip() - message('Project version is now: @0@'.format(lz4_version)) -else - error('Cannot find project version in @0@'.format(lz4_h_file)) -endif - -lz4_libversion = lz4_version - -# ============================================================================= -# Installation directories -# ============================================================================= - -lz4_prefix = get_option('prefix') -lz4_bindir = get_option('bindir') -lz4_datadir = get_option('datadir') -lz4_mandir = get_option('mandir') -lz4_docdir = join_paths(lz4_datadir, 'doc', meson.project_name()) - -# ============================================================================= -# Project options -# ============================================================================= - -buildtype = get_option('buildtype') - -# Built-in options -use_debug = get_option('debug') - -# Custom options -debug_level = get_option('debug_level') -use_backtrace = get_option('backtrace') - -bin_programs = get_option('bin_programs') -bin_contrib = get_option('bin_contrib') -bin_tests = get_option('bin_tests') -bin_examples = get_option('bin_examples') -#feature_multi_thread = get_option('multi_thread') - -# ============================================================================= -# Dependencies -# ============================================================================= - -#libm_dep = cc.find_library('m', required: bin_tests) -#thread_dep = dependency('threads', required: feature_multi_thread) -#use_multi_thread = thread_dep.found() - -# ============================================================================= -# Compiler flags -# ============================================================================= - -add_project_arguments(['-DXXH_NAMESPACE=LZ4_'], language: 'c') - -if [compiler_gcc, compiler_clang].contains(cc_id) - common_warning_flags = [] - # Should use Meson's own --werror build option - #common_warning_flags += ['-Werror'] - if c_std == 'c89' or c_std == 'gnu89' - common_warning_flags += ['-pedantic', '-Wno-long-long', '-Wno-variadic-macros'] - elif c_std == 'c99' or c_std == 'gnu99' - common_warning_flags += ['-pedantic'] - endif - cc_compile_flags = cc.get_supported_arguments(common_warning_flags) - add_project_arguments(cc_compile_flags, language: 'c') -endif - -# ============================================================================= -# Subdirs -# ============================================================================= - -subdir('lib') - -if bin_programs - subdir('programs') -endif - -if bin_tests - subdir('tests') -endif - -if bin_contrib - subdir('contrib') -endif - -if bin_examples - subdir('examples') -endif +subdir('meson') diff --git a/contrib/meson/meson/GetLz4LibraryVersion.py b/contrib/meson/meson/GetLz4LibraryVersion.py new file mode 100644 index 0000000..d8abfcb --- /dev/null +++ b/contrib/meson/meson/GetLz4LibraryVersion.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 +# ############################################################################# +# Copyright (c) 2018-present lzutao +# All rights reserved. +# +# This source code is licensed under both the BSD-style license (found in the +# LICENSE file in the root directory of this source tree) and the GPLv2 (found +# in the COPYING file in the root directory of this source tree). +# ############################################################################# +import re + + +def find_version_tuple(filepath): + version_file_data = None + with open(filepath) as fd: + version_file_data = fd.read() + + patterns = r"""#\s*define\s+LZ4_VERSION_MAJOR\s+([0-9]+).*$ +#\s*define\s+LZ4_VERSION_MINOR\s+([0-9]+).*$ +#\s*define\s+LZ4_VERSION_RELEASE\s+([0-9]+).*$ +""" + regex = re.compile(patterns, re.MULTILINE) + version_match = regex.search(version_file_data) + if version_match: + return version_match.groups() + raise Exception("Unable to find version string.") + + +def main(): + import argparse + parser = argparse.ArgumentParser(description='Print lz4 version from lib/lz4.h') + parser.add_argument('file', help='path to lib/lz4.h') + args = parser.parse_args() + version_tuple = find_version_tuple(args.file) + print('.'.join(version_tuple)) + + +if __name__ == '__main__': + main() diff --git a/contrib/meson/meson/InstallSymlink.py b/contrib/meson/meson/InstallSymlink.py new file mode 100644 index 0000000..3f2998c --- /dev/null +++ b/contrib/meson/meson/InstallSymlink.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python3 +# ############################################################################# +# Copyright (c) 2018-present lzutao +# All rights reserved. +# +# This source code is licensed under both the BSD-style license (found in the +# LICENSE file in the root directory of this source tree) and the GPLv2 (found +# in the COPYING file in the root directory of this source tree). +# ############################################################################# +# This file should be synced with https://github.com/lzutao/meson-symlink + +import os +import pathlib # since Python 3.4 + + +def install_symlink(src, dst, install_dir, dst_is_dir=False, dir_mode=0o777): + if not install_dir.exists(): + install_dir.mkdir(mode=dir_mode, parents=True, exist_ok=True) + if not install_dir.is_dir(): + raise NotADirectoryError(install_dir) + + new_dst = install_dir.joinpath(dst) + if new_dst.is_symlink() and os.readlink(new_dst) == src: + print('File exists: {!r} -> {!r}'.format(new_dst, src)) + return + print('Installing symlink {!r} -> {!r}'.format(new_dst, src)) + new_dst.symlink_to(src, target_is_directory=dst_is_dir) + + +def main(): + import argparse + parser = argparse.ArgumentParser(description='Install a symlink', + usage='{0} [-h] [-d] [-m MODE] source dest install_dir\n\n' + 'example:\n' + ' {0} dash sh /bin'.format(pathlib.Path(__file__).name)) + parser.add_argument('source', help='target to link') + parser.add_argument('dest', help='link name') + parser.add_argument('install_dir', help='installation directory') + parser.add_argument('-d', '--isdir', + action='store_true', + help='dest is a directory') + parser.add_argument('-m', '--mode', + help='directory mode on creating if not exist', + default='0o755') + args = parser.parse_args() + + dir_mode = int(args.mode, 8) + + meson_destdir = os.environ.get('MESON_INSTALL_DESTDIR_PREFIX', default='') + install_dir = pathlib.Path(meson_destdir, args.install_dir) + install_symlink(args.source, args.dest, install_dir, args.isdir, dir_mode) + + +if __name__ == '__main__': + main() diff --git a/contrib/meson/meson/contrib/gen_manual/meson.build b/contrib/meson/meson/contrib/gen_manual/meson.build new file mode 100644 index 0000000..a872bd6 --- /dev/null +++ b/contrib/meson/meson/contrib/gen_manual/meson.build @@ -0,0 +1,43 @@ +# ############################################################################# +# Copyright (c) 2018-present lzutao +# All rights reserved. +# +# This source code is licensed under both the BSD-style license (found in the +# LICENSE file in the root directory of this source tree) and the GPLv2 (found +# in the COPYING file in the root directory of this source tree). +# ############################################################################# + +lz4_root_dir = '../../../../..' + +add_languages('cpp') +cxx = meson.get_compiler('cpp') + +gen_manual_includes = include_directories(join_paths(lz4_root_dir, 'contrib/gen_manual')) + +gen_manual_cppflags = cxx.get_supported_arguments(['-Wextra', '-Wcast-qual', + '-Wcast-align', '-Wshadow', '-Wstrict-aliasing=1', '-Wswitch-enum', + '-Wno-comment']) + +gen_manual = executable('gen_manual', + join_paths(lz4_root_dir, 'contrib/gen_manual/gen_manual.cpp'), + cpp_args: gen_manual_cppflags, + include_directories: gen_manual_includes, + native: true, + install: false) + +# Update lz4 manual +lz4_manual_html = custom_target('lz4_manual.html', + output : 'lz4_manual.html', + command : [gen_manual, + lz4_version, + join_paths(meson.current_source_dir(), lz4_root_dir, 'lib/lz4.h'), + '@OUTPUT@'], + install : false) +# Update lz4frame manual +lz4_manual_html = custom_target('lz4frame_manual.html', + output : 'lz4frame_manual.html', + command : [gen_manual, + lz4_version, + join_paths(meson.current_source_dir(), lz4_root_dir, 'lib/lz4frame.h'), + '@OUTPUT@'], + install : false) diff --git a/contrib/meson/meson/contrib/meson.build b/contrib/meson/meson/contrib/meson.build new file mode 100644 index 0000000..5249a4c --- /dev/null +++ b/contrib/meson/meson/contrib/meson.build @@ -0,0 +1,10 @@ +# ############################################################################# +# Copyright (c) 2018-present lzutao +# All rights reserved. +# +# This source code is licensed under both the BSD-style license (found in the +# LICENSE file in the root directory of this source tree) and the GPLv2 (found +# in the COPYING file in the root directory of this source tree). +# ############################################################################# + +subdir('gen_manual') diff --git a/contrib/meson/meson/examples/meson.build b/contrib/meson/meson/examples/meson.build new file mode 100644 index 0000000..493049d --- /dev/null +++ b/contrib/meson/meson/examples/meson.build @@ -0,0 +1,49 @@ +# ############################################################################# +# Copyright (c) 2018-present lzutao +# All rights reserved. +# +# This source code is licensed under both the BSD-style license (found in the +# LICENSE file in the root directory of this source tree) and the GPLv2 (found +# in the COPYING file in the root directory of this source tree). +# ############################################################################# + +lz4_root_dir = '../../../..' + +#examples_c_args = ['-Wextra', '-Wundef', '-Wshadow', '-Wcast-align', '-Wstrict-prototypes'] + +printVersion = executable('printVersion', + join_paths(lz4_root_dir, 'examples/printVersion.c'), + dependencies: liblz4_dep, + install: false) +doubleBuffer = executable('doubleBuffer', + join_paths(lz4_root_dir, 'examples/blockStreaming_doubleBuffer.c'), + dependencies: liblz4_dep, + install: false) +dictionaryRandomAccess = executable('dictionaryRandomAccess', + join_paths(lz4_root_dir, 'examples/dictionaryRandomAccess.c'), + dependencies: liblz4_dep, + install: false) +ringBuffer = executable('ringBuffer', + join_paths(lz4_root_dir, 'examples/blockStreaming_ringBuffer.c'), + dependencies: liblz4_dep, + install: false) +ringBufferHC = executable('ringBufferHC', + join_paths(lz4_root_dir, 'examples/HCStreaming_ringBuffer.c'), + dependencies: liblz4_dep, + install: false) +lineCompress = executable('lineCompress', + join_paths(lz4_root_dir, 'examples/blockStreaming_lineByLine.c'), + dependencies: liblz4_dep, + install: false) +frameCompress = executable('frameCompress', + join_paths(lz4_root_dir, 'examples/frameCompress.c'), + dependencies: liblz4_dep, + install: false) +compressFunctions = executable('compressFunctions', + join_paths(lz4_root_dir, 'examples/compress_functions.c'), + dependencies: liblz4_dep, + install: false) +simpleBuffer = executable('simpleBuffer', + join_paths(lz4_root_dir, 'examples/simple_buffer.c'), + dependencies: liblz4_dep, + install: false) diff --git a/contrib/meson/meson/lib/meson.build b/contrib/meson/meson/lib/meson.build new file mode 100644 index 0000000..131edcb --- /dev/null +++ b/contrib/meson/meson/lib/meson.build @@ -0,0 +1,57 @@ +# ############################################################################# +# Copyright (c) 2018-present lzutao +# All rights reserved. +# +# This source code is licensed under both the BSD-style license (found in the +# LICENSE file in the root directory of this source tree) and the GPLv2 (found +# in the COPYING file in the root directory of this source tree). +# ############################################################################# + +lz4_root_dir = '../../../..' + +liblz4_includes = [include_directories(join_paths(lz4_root_dir, 'lib'))] +liblz4_sources = [join_paths(lz4_root_dir, 'lib/lz4.c'), + join_paths(lz4_root_dir, 'lib/lz4frame.c'), + join_paths(lz4_root_dir, 'lib/lz4hc.c'), + join_paths(lz4_root_dir, 'lib/xxhash.c')] +liblz4_c_args = [] + +liblz4_debug_cflags = [] +if use_debug + liblz4_c_args += '-DLZ4_DEBUG=@0@'.format(debug_level) + if [compiler_gcc, compiler_clang].contains(cc_id) + liblz4_debug_cflags = ['-Wextra', '-Wcast-qual', '-Wcast-align', '-Wshadow', + '-Wswitch-enum', '-Wdeclaration-after-statement', '-Wstrict-prototypes', + '-Wundef', '-Wpointer-arith', '-Wstrict-aliasing=1'] + endif +endif +liblz4_c_args += cc.get_supported_arguments(liblz4_debug_cflags) + +if host_machine_os == os_windows and default_library != 'static' + liblz4_c_args += '-DLZ4_DLL_EXPORT=1' +endif + +liblz4 = library('lz4', + liblz4_sources, + include_directories: liblz4_includes, + c_args: liblz4_c_args, + install: true, + version: lz4_libversion) + +liblz4_dep = declare_dependency(link_with: liblz4, + include_directories: liblz4_includes) + +pkgconfig.generate(liblz4, + name: 'lz4', + filebase: 'liblz4', + description: 'extremely fast lossless compression algorithm library', + version: lz4_libversion, + url: 'http://www.lz4.org/') + +install_headers(join_paths(lz4_root_dir, 'lib/lz4.h'), + join_paths(lz4_root_dir, 'lib/lz4hc.h'), + join_paths(lz4_root_dir, 'lib/lz4frame.h')) + +if default_library != 'shared' + install_headers(join_paths(lz4_root_dir, 'lib/lz4frame_static.h')) +endif diff --git a/contrib/meson/meson/meson.build b/contrib/meson/meson/meson.build new file mode 100644 index 0000000..387e7bd --- /dev/null +++ b/contrib/meson/meson/meson.build @@ -0,0 +1,118 @@ +# ############################################################################# +# Copyright (c) 2018-present lzutao +# All rights reserved. +# +# This source code is licensed under both the BSD-style license (found in the +# LICENSE file in the root directory of this source tree) and the GPLv2 (found +# in the COPYING file in the root directory of this source tree). +# ############################################################################# + +cc = meson.get_compiler('c') +pkgconfig = import('pkgconfig') +python3 = import('python').find_installation() +c_std = get_option('c_std') +default_library = get_option('default_library') + +host_machine_os = host_machine.system() +os_windows = 'windows' +os_linux = 'linux' +os_darwin = 'darwin' +os_freebsd = 'freebsd' +os_sun = 'sunos' + +cc_id = cc.get_id() +compiler_gcc = 'gcc' +compiler_clang = 'clang' +compiler_msvc = 'msvc' + +lz4_version = meson.project_version() + +lz4_h_file = join_paths(meson.current_source_dir(), '../../../lib/lz4.h') +GetLz4LibraryVersion_py = files('GetLz4LibraryVersion.py') +r = run_command(python3, GetLz4LibraryVersion_py, lz4_h_file) +if r.returncode() == 0 + lz4_version = r.stdout().strip() + message('Project version is now: @0@'.format(lz4_version)) +else + error('Cannot find project version in @0@'.format(lz4_h_file)) +endif + +lz4_libversion = lz4_version + +# ============================================================================= +# Installation directories +# ============================================================================= + +lz4_prefix = get_option('prefix') +lz4_bindir = get_option('bindir') +lz4_datadir = get_option('datadir') +lz4_mandir = get_option('mandir') +lz4_docdir = join_paths(lz4_datadir, 'doc', meson.project_name()) + +# ============================================================================= +# Project options +# ============================================================================= + +buildtype = get_option('buildtype') + +# Built-in options +use_debug = get_option('debug') + +# Custom options +debug_level = get_option('debug_level') +use_backtrace = get_option('backtrace') + +bin_programs = get_option('bin_programs') +bin_contrib = get_option('bin_contrib') +bin_tests = get_option('bin_tests') +bin_examples = get_option('bin_examples') +#feature_multi_thread = get_option('multi_thread') + +# ============================================================================= +# Dependencies +# ============================================================================= + +#libm_dep = cc.find_library('m', required: bin_tests) +#thread_dep = dependency('threads', required: feature_multi_thread) +#use_multi_thread = thread_dep.found() + +# ============================================================================= +# Compiler flags +# ============================================================================= + +add_project_arguments(['-DXXH_NAMESPACE=LZ4_'], language: 'c') + +if [compiler_gcc, compiler_clang].contains(cc_id) + common_warning_flags = [] + # Should use Meson's own --werror build option + #common_warning_flags += ['-Werror'] + if c_std == 'c89' or c_std == 'gnu89' + common_warning_flags += ['-pedantic', '-Wno-long-long', '-Wno-variadic-macros'] + elif c_std == 'c99' or c_std == 'gnu99' + common_warning_flags += ['-pedantic'] + endif + cc_compile_flags = cc.get_supported_arguments(common_warning_flags) + add_project_arguments(cc_compile_flags, language: 'c') +endif + +# ============================================================================= +# Subdirs +# ============================================================================= + +subdir('lib') + +if bin_programs + subdir('programs') +endif + +if bin_tests + subdir('tests') +endif + +if bin_contrib + subdir('contrib') +endif + +if bin_examples + subdir('examples') +endif diff --git a/contrib/meson/meson/programs/meson.build b/contrib/meson/meson/programs/meson.build new file mode 100644 index 0000000..705dbf5 --- /dev/null +++ b/contrib/meson/meson/programs/meson.build @@ -0,0 +1,52 @@ +# ############################################################################# +# Copyright (c) 2018-present lzutao +# All rights reserved. +# +# This source code is licensed under both the BSD-style license (found in the +# LICENSE file in the root directory of this source tree) and the GPLv2 (found +# in the COPYING file in the root directory of this source tree). +# ############################################################################# + +lz4_root_dir = '../../../..' + +lz4_includes = include_directories(join_paths(lz4_root_dir, 'programs')) +lz4_sources = [join_paths(lz4_root_dir, 'programs/bench.c'), + join_paths(lz4_root_dir, 'programs/datagen.c'), + join_paths(lz4_root_dir, 'programs/lz4cli.c'), + join_paths(lz4_root_dir, 'programs/lz4io.c')] +lz4_c_args = [] + +export_dynamic_on_windows = false +# explicit backtrace enable/disable for Linux & Darwin +if not use_backtrace + lz4_c_args += '-DBACKTRACE_ENABLE=0' +elif use_debug and host_machine_os == os_windows # MinGW target + lz4_c_args += '-DBACKTRACE_ENABLE=1' + export_dynamic_on_windows = true +endif + +lz4_deps = [ liblz4_dep ] + +lz4 = executable('lz4', + lz4_sources, + include_directories: lz4_includes, + c_args: lz4_c_args, + dependencies: lz4_deps, + export_dynamic: export_dynamic_on_windows, # Since Meson 0.45.0 + install: true) + +# ============================================================================= +# Programs and manpages installing +# ============================================================================= + +install_man(join_paths(lz4_root_dir, 'programs/lz4.1')) + +InstallSymlink_py = '../InstallSymlink.py' +lz4_man1_dir = join_paths(lz4_mandir, 'man1') +bin_EXT = host_machine_os == os_windows ? '.exe' : '' +man1_EXT = meson.version().version_compare('>=0.49.0') ? '.1' : '.1.gz' + +foreach f : ['lz4c', 'lz4cat', 'unlz4'] + meson.add_install_script(InstallSymlink_py, 'lz4' + bin_EXT, f + bin_EXT, lz4_bindir) + meson.add_install_script(InstallSymlink_py, 'lz4' + man1_EXT, f + man1_EXT, lz4_man1_dir) +endforeach diff --git a/contrib/meson/meson/tests/meson.build b/contrib/meson/meson/tests/meson.build new file mode 100644 index 0000000..7800475 --- /dev/null +++ b/contrib/meson/meson/tests/meson.build @@ -0,0 +1,93 @@ +# ############################################################################# +# Copyright (c) 2018-present lzutao +# All rights reserved. +# +# This source code is licensed under both the BSD-style license (found in the +# LICENSE file in the root directory of this source tree) and the GPLv2 (found +# in the COPYING file in the root directory of this source tree). +# ############################################################################# + +lz4_root_dir = '../../../..' +programs_dir_inc = include_directories(join_paths(lz4_root_dir, 'programs')) +lib_dir_inc = include_directories(join_paths(lz4_root_dir, 'lib')) + +# ============================================================================= +# Test flags +# ============================================================================= + +TEST_FILES = join_paths(meson.current_source_dir(), lz4_root_dir, 'tests/COPYING') +FUZZER_TIME = '-T90s' +NB_LOOPS = '-i1' + +# ============================================================================= +# Executables +# ============================================================================= + +fullbench_sources = [join_paths(lz4_root_dir, 'tests/fullbench.c')] +fullbench = executable('fullbench', + fullbench_sources, + include_directories: programs_dir_inc, + dependencies: liblz4_dep, + install: false) + +fuzzer_sources = [join_paths(lz4_root_dir, 'tests/fuzzer.c')] +fuzzer = executable('fuzzer', + fuzzer_sources, + c_args: ['-D_DEFAULT_SOURCE', '-D_BSD_SOURCE'], # since glibc 2.19 + include_directories: programs_dir_inc, + dependencies: liblz4_dep, + install: false) + +frametest_sources = [join_paths(lz4_root_dir, 'tests/frametest.c')] +frametest = executable('frametest', + frametest_sources, + include_directories: programs_dir_inc, + dependencies: liblz4_dep, + install: false) + +roundTripTest_sources = [join_paths(lz4_root_dir, 'tests/roundTripTest.c')] +roundTripTest = executable('roundTripTest', + roundTripTest_sources, + dependencies: [ liblz4_dep ], + install: false) + +datagen_sources = [join_paths(lz4_root_dir, 'tests/datagencli.c')] +datagen = executable('datagen', + datagen_sources, + objects: lz4.extract_objects(join_paths(lz4_root_dir, 'programs/datagen.c')), + include_directories: lz4_includes, + dependencies: [ liblz4_dep ], + install: false) + +checkFrame_sources = [join_paths(lz4_root_dir, 'tests/checkFrame.c')] +checkFrame = executable('checkFrame', + checkFrame_sources, + include_directories: programs_dir_inc, + dependencies: [ liblz4_dep ], + install: false) + +checkTag_sources = [join_paths(lz4_root_dir, 'tests/checkTag.c')] +checkTag = executable('checkTag', + checkTag_sources, + include_directories: lib_dir_inc, + install: false) + +# ============================================================================= +# Tests (Use "meson test --list" to list all tests) +# ============================================================================= + +# XXX: (Need TEST) These timeouts (in seconds) when running on a HDD should be +# at least six times bigger than on a SSD + +test('test-fullbench', + fullbench, + args: ['--no-prompt', NB_LOOPS, TEST_FILES], + timeout: 420) # Should enough when running on HDD +test('test-fuzzer', + fuzzer, + args: [FUZZER_TIME], + timeout: 100) +test('test-frametest', + frametest, + args: [FUZZER_TIME], + timeout: 100) diff --git a/contrib/meson/programs/meson.build b/contrib/meson/programs/meson.build deleted file mode 100644 index df64eb0..0000000 --- a/contrib/meson/programs/meson.build +++ /dev/null @@ -1,52 +0,0 @@ -# ############################################################################# -# Copyright (c) 2018-present lzutao -# All rights reserved. -# -# This source code is licensed under both the BSD-style license (found in the -# LICENSE file in the root directory of this source tree) and the GPLv2 (found -# in the COPYING file in the root directory of this source tree). -# ############################################################################# - -lz4_root_dir = '../../..' - -lz4_includes = include_directories(join_paths(lz4_root_dir, 'programs')) -lz4_sources = [join_paths(lz4_root_dir, 'programs/bench.c'), - join_paths(lz4_root_dir, 'programs/datagen.c'), - join_paths(lz4_root_dir, 'programs/lz4cli.c'), - join_paths(lz4_root_dir, 'programs/lz4io.c')] -lz4_c_args = [] - -export_dynamic_on_windows = false -# explicit backtrace enable/disable for Linux & Darwin -if not use_backtrace - lz4_c_args += '-DBACKTRACE_ENABLE=0' -elif use_debug and host_machine_os == os_windows # MinGW target - lz4_c_args += '-DBACKTRACE_ENABLE=1' - export_dynamic_on_windows = true -endif - -lz4_deps = [ liblz4_dep ] - -lz4 = executable('lz4', - lz4_sources, - include_directories: lz4_includes, - c_args: lz4_c_args, - dependencies: lz4_deps, - export_dynamic: export_dynamic_on_windows, # Since Meson 0.45.0 - install: true) - -# ============================================================================= -# Programs and manpages installing -# ============================================================================= - -install_man(join_paths(lz4_root_dir, 'programs/lz4.1')) - -InstallSymlink_py = '../InstallSymlink.py' -lz4_man1_dir = join_paths(lz4_mandir, 'man1') -bin_EXT = host_machine_os == os_windows ? '.exe' : '' -man1_EXT = meson.version().version_compare('>=0.49.0') ? '.1' : '.1.gz' - -foreach f : ['lz4c', 'lz4cat', 'unlz4'] - meson.add_install_script(InstallSymlink_py, 'lz4' + bin_EXT, f + bin_EXT, lz4_bindir) - meson.add_install_script(InstallSymlink_py, 'lz4' + man1_EXT, f + man1_EXT, lz4_man1_dir) -endforeach diff --git a/contrib/meson/tests/meson.build b/contrib/meson/tests/meson.build deleted file mode 100644 index 392bcf2..0000000 --- a/contrib/meson/tests/meson.build +++ /dev/null @@ -1,93 +0,0 @@ -# ############################################################################# -# Copyright (c) 2018-present lzutao -# All rights reserved. -# -# This source code is licensed under both the BSD-style license (found in the -# LICENSE file in the root directory of this source tree) and the GPLv2 (found -# in the COPYING file in the root directory of this source tree). -# ############################################################################# - -lz4_root_dir = '../../..' -programs_dir_inc = include_directories(join_paths(lz4_root_dir, 'programs')) -lib_dir_inc = include_directories(join_paths(lz4_root_dir, 'lib')) - -# ============================================================================= -# Test flags -# ============================================================================= - -TEST_FILES = join_paths(meson.current_source_dir(), lz4_root_dir, 'tests/COPYING') -FUZZER_TIME = '-T90s' -NB_LOOPS = '-i1' - -# ============================================================================= -# Executables -# ============================================================================= - -fullbench_sources = [join_paths(lz4_root_dir, 'tests/fullbench.c')] -fullbench = executable('fullbench', - fullbench_sources, - include_directories: programs_dir_inc, - dependencies: liblz4_dep, - install: false) - -fuzzer_sources = [join_paths(lz4_root_dir, 'tests/fuzzer.c')] -fuzzer = executable('fuzzer', - fuzzer_sources, - c_args: ['-D_DEFAULT_SOURCE', '-D_BSD_SOURCE'], # since glibc 2.19 - include_directories: programs_dir_inc, - dependencies: liblz4_dep, - install: false) - -frametest_sources = [join_paths(lz4_root_dir, 'tests/frametest.c')] -frametest = executable('frametest', - frametest_sources, - include_directories: programs_dir_inc, - dependencies: liblz4_dep, - install: false) - -roundTripTest_sources = [join_paths(lz4_root_dir, 'tests/roundTripTest.c')] -roundTripTest = executable('roundTripTest', - roundTripTest_sources, - dependencies: [ liblz4_dep ], - install: false) - -datagen_sources = [join_paths(lz4_root_dir, 'tests/datagencli.c')] -datagen = executable('datagen', - datagen_sources, - objects: lz4.extract_objects(join_paths(lz4_root_dir, 'programs/datagen.c')), - include_directories: lz4_includes, - dependencies: [ liblz4_dep ], - install: false) - -checkFrame_sources = [join_paths(lz4_root_dir, 'tests/checkFrame.c')] -checkFrame = executable('checkFrame', - checkFrame_sources, - include_directories: programs_dir_inc, - dependencies: [ liblz4_dep ], - install: false) - -checkTag_sources = [join_paths(lz4_root_dir, 'tests/checkTag.c')] -checkTag = executable('checkTag', - checkTag_sources, - include_directories: lib_dir_inc, - install: false) - -# ============================================================================= -# Tests (Use "meson test --list" to list all tests) -# ============================================================================= - -# XXX: (Need TEST) These timeouts (in seconds) when running on a HDD should be -# at least six times bigger than on a SSD - -test('test-fullbench', - fullbench, - args: ['--no-prompt', NB_LOOPS, TEST_FILES], - timeout: 420) # Should enough when running on HDD -test('test-fuzzer', - fuzzer, - args: [FUZZER_TIME], - timeout: 100) -test('test-frametest', - frametest, - args: [FUZZER_TIME], - timeout: 100) -- cgit v1.2.3 From 60d6533b9ebaf4d3b01437e3ace1f0c9389d0975 Mon Sep 17 00:00:00 2001 From: Bimba Shrestha Date: Fri, 4 Oct 2019 08:36:08 -0700 Subject: Adding cirrus config file for freebsd-12-0 --- .cirrus.yml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .cirrus.yml diff --git a/.cirrus.yml b/.cirrus.yml new file mode 100644 index 0000000..cb4b843 --- /dev/null +++ b/.cirrus.yml @@ -0,0 +1,5 @@ +freebsd_instance: + image_family: freebsd-12-0 + +task: + script: pkg install -y gmake && gmake test -- cgit v1.2.3 From b2c1fa8c5993b6d7f31ef580e343e23dea001865 Mon Sep 17 00:00:00 2001 From: Bimba Shrestha Date: Fri, 4 Oct 2019 08:40:46 -0700 Subject: Using gmake instead of make --- tests/test_install.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_install.sh b/tests/test_install.sh index f9de402..c400fb7 100755 --- a/tests/test_install.sh +++ b/tests/test_install.sh @@ -1,7 +1,7 @@ #/usr/bin/env sh set -e -make="make -C $lz4_root" +make="gmake -C $lz4_root" for cmd in install uninstall; do for upper in DUMMY PREFIX EXEC_PREFIX LIBDIR INCLUDEDIR PKGCONFIGDIR BINDIR MANDIR MAN1DIR ; do lower=$(echo $upper | tr '[:upper:]' '[:lower:]') -- cgit v1.2.3 From c0acce96c1c9034cddfe2fd82044b3023fa9de63 Mon Sep 17 00:00:00 2001 From: Bimba Shrestha Date: Fri, 4 Oct 2019 09:29:58 -0700 Subject: Using instead of gmake (to address the travis failure) --- tests/test_install.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_install.sh b/tests/test_install.sh index c400fb7..50567b7 100755 --- a/tests/test_install.sh +++ b/tests/test_install.sh @@ -1,7 +1,7 @@ #/usr/bin/env sh set -e -make="gmake -C $lz4_root" +make="$(MAKE) -C $lz4_root" for cmd in install uninstall; do for upper in DUMMY PREFIX EXEC_PREFIX LIBDIR INCLUDEDIR PKGCONFIGDIR BINDIR MANDIR MAN1DIR ; do lower=$(echo $upper | tr '[:upper:]' '[:lower:]') -- cgit v1.2.3 From 77e6ff11609caab0df61fc3f2a85e3f325a84806 Mon Sep 17 00:00:00 2001 From: Bimba Shrestha Date: Fri, 4 Oct 2019 10:12:22 -0700 Subject: Adding condition for FreeBSD and using gmake --- tests/test_install.sh | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/test_install.sh b/tests/test_install.sh index 50567b7..0cdfd3b 100755 --- a/tests/test_install.sh +++ b/tests/test_install.sh @@ -1,7 +1,14 @@ #/usr/bin/env sh set -e -make="$(MAKE) -C $lz4_root" + +make="make -C $lz4_root" +if [ "$unamestr" = 'Linux' ]; then + make="make -C $lz4_root" +elif [ "$unamestr" = 'FreeBSD' ]; then + make="gmake -C $lz4_root" +fi + for cmd in install uninstall; do for upper in DUMMY PREFIX EXEC_PREFIX LIBDIR INCLUDEDIR PKGCONFIGDIR BINDIR MANDIR MAN1DIR ; do lower=$(echo $upper | tr '[:upper:]' '[:lower:]') -- cgit v1.2.3 From dcdb9c73c02cdb5f6d4d7498bbf807cfd71aff84 Mon Sep 17 00:00:00 2001 From: Bimba Shrestha Date: Mon, 7 Oct 2019 08:12:42 -0700 Subject: Adding unamestr var --- tests/test_install.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_install.sh b/tests/test_install.sh index 0cdfd3b..ba87934 100755 --- a/tests/test_install.sh +++ b/tests/test_install.sh @@ -3,6 +3,7 @@ set -e make="make -C $lz4_root" +unamestr=$(uname) if [ "$unamestr" = 'Linux' ]; then make="make -C $lz4_root" elif [ "$unamestr" = 'FreeBSD' ]; then -- cgit v1.2.3 From 2c392c5f9a6a0178a1fb969e605862a54a505cd2 Mon Sep 17 00:00:00 2001 From: David A Kondrad Date: Fri, 25 Oct 2019 14:51:36 -0400 Subject: project: visual: fix VS2010/17 gitignore The `lz4` project was excluded from being checked in for VS2017. Add some fixups to explicitly allow this project directory. Also add an exclusion for VS2017 binaries and VS `ipch` directories. Signed-off-by: David A Kondrad --- visual/.gitignore | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/visual/.gitignore b/visual/.gitignore index 276f8f5..69e1111 100644 --- a/visual/.gitignore +++ b/visual/.gitignore @@ -8,3 +8,9 @@ *.user ver*/ VS2010/bin/ +VS2017/bin/ +ipch + +# Fixup for lz4 project directories +!VS2010/lz4 +!VS2017/lz4 -- cgit v1.2.3 From de4487788852e5e0537ac98a337d401d2eaf9eee Mon Sep 17 00:00:00 2001 From: David A Kondrad Date: Fri, 25 Oct 2019 15:04:27 -0400 Subject: project: VS2017: add lz4 project Add missing lz4 project to the VS2017 solution. The project was made from scratch rather than importing and updated to match the VS2010 options. Any new options not present in the old VS2010 project we pulled from other console binaries in the solution. Signed-off-by: David A Kondrad --- visual/VS2017/lz4.sln | 10 +++ visual/VS2017/lz4/lz4.rc | 51 +++++++++++++ visual/VS2017/lz4/lz4.vcxproj | 164 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 225 insertions(+) create mode 100644 visual/VS2017/lz4/lz4.rc create mode 100644 visual/VS2017/lz4/lz4.vcxproj diff --git a/visual/VS2017/lz4.sln b/visual/VS2017/lz4.sln index 72e98fc..6a2779f 100644 --- a/visual/VS2017/lz4.sln +++ b/visual/VS2017/lz4.sln @@ -19,6 +19,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "fullbench-dll", "fullbench- {9800039D-4AAA-43A4-BB78-FEF6F4836927} = {9800039D-4AAA-43A4-BB78-FEF6F4836927} EndProjectSection EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "lz4", "lz4\lz4.vcxproj", "{60A3115E-B988-41EE-8815-F4D4F253D866}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Win32 = Debug|Win32 @@ -83,6 +85,14 @@ Global {13992FD2-077E-4954-B065-A428198201A9}.Release|Win32.Build.0 = Release|Win32 {13992FD2-077E-4954-B065-A428198201A9}.Release|x64.ActiveCfg = Release|x64 {13992FD2-077E-4954-B065-A428198201A9}.Release|x64.Build.0 = Release|x64 + {60A3115E-B988-41EE-8815-F4D4F253D866}.Debug|Win32.ActiveCfg = Debug|Win32 + {60A3115E-B988-41EE-8815-F4D4F253D866}.Debug|Win32.Build.0 = Debug|Win32 + {60A3115E-B988-41EE-8815-F4D4F253D866}.Debug|x64.ActiveCfg = Debug|x64 + {60A3115E-B988-41EE-8815-F4D4F253D866}.Debug|x64.Build.0 = Debug|x64 + {60A3115E-B988-41EE-8815-F4D4F253D866}.Release|Win32.ActiveCfg = Release|Win32 + {60A3115E-B988-41EE-8815-F4D4F253D866}.Release|Win32.Build.0 = Release|Win32 + {60A3115E-B988-41EE-8815-F4D4F253D866}.Release|x64.ActiveCfg = Release|x64 + {60A3115E-B988-41EE-8815-F4D4F253D866}.Release|x64.Build.0 = Release|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/visual/VS2017/lz4/lz4.rc b/visual/VS2017/lz4/lz4.rc new file mode 100644 index 0000000..c593edf --- /dev/null +++ b/visual/VS2017/lz4/lz4.rc @@ -0,0 +1,51 @@ +// Microsoft Visual C++ generated resource script. +// + +#include "lz4.h" /* LZ4_VERSION_STRING */ +#define APSTUDIO_READONLY_SYMBOLS +#include "verrsrc.h" +#undef APSTUDIO_READONLY_SYMBOLS + + +#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU) +LANGUAGE 9, 1 + +///////////////////////////////////////////////////////////////////////////// +// +// Version +// + +VS_VERSION_INFO VERSIONINFO + FILEVERSION LZ4_VERSION_MAJOR,LZ4_VERSION_MINOR,LZ4_VERSION_RELEASE,0 + PRODUCTVERSION LZ4_VERSION_MAJOR,LZ4_VERSION_MINOR,LZ4_VERSION_RELEASE,0 + FILEFLAGSMASK VS_FFI_FILEFLAGSMASK +#ifdef _DEBUG + FILEFLAGS VS_FF_DEBUG +#else + FILEFLAGS 0x0L +#endif + FILEOS VOS_NT_WINDOWS32 + FILETYPE VFT_DLL + FILESUBTYPE VFT2_UNKNOWN +BEGIN + BLOCK "StringFileInfo" + BEGIN + BLOCK "040904B0" + BEGIN + VALUE "CompanyName", "Yann Collet" + VALUE "FileDescription", "Extremely fast compression" + VALUE "FileVersion", LZ4_VERSION_STRING + VALUE "InternalName", "lz4.exe" + VALUE "LegalCopyright", "Copyright (C) 2013-2016, Yann Collet" + VALUE "OriginalFilename", "lz4.exe" + VALUE "ProductName", "LZ4" + VALUE "ProductVersion", LZ4_VERSION_STRING + END + END + BLOCK "VarFileInfo" + BEGIN + VALUE "Translation", 0x0409, 1200 + END +END + +#endif diff --git a/visual/VS2017/lz4/lz4.vcxproj b/visual/VS2017/lz4/lz4.vcxproj new file mode 100644 index 0000000..b4fed24 --- /dev/null +++ b/visual/VS2017/lz4/lz4.vcxproj @@ -0,0 +1,164 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 15.0 + {60A3115E-B988-41EE-8815-F4D4F253D866} + lz4 + 8.1 + + + + Application + true + v141 + Unicode + + + Application + false + v141 + false + Unicode + + + Application + true + v141 + MultiByte + + + Application + false + v141 + true + MultiByte + + + + + + + + + + + + + + + + + + + + + $(SolutionDir)bin\$(Platform)_$(Configuration)\ + $(SolutionDir)bin\obj\$(RootNamespace)_$(Platform)_$(Configuration)\ + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + + + $(SolutionDir)bin\$(Platform)_$(Configuration)\ + $(SolutionDir)bin\obj\$(RootNamespace)_$(Platform)_$(Configuration)\ + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + false + + + + Level4 + Disabled + true + true + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + + + true + Console + false + false + + + + + Level3 + Disabled + true + true + + + + + Level3 + MaxSpeed + true + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + + + true + true + true + Console + + + + + Level3 + MaxSpeed + true + true + true + true + + + true + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file -- cgit v1.2.3 From cc91777c98d71194c9d29544f84880742ffdf86c Mon Sep 17 00:00:00 2001 From: Reto Koradi Date: Mon, 4 Nov 2019 11:51:41 -0800 Subject: Make benchmark compatible with dictionary compression Support the -D command line option for running benchmarks. The benchmark code was slightly restructured to factor out the calls that need to be different for each benchmark scenario. Since there are now 4 scenarios (all combinations of fast/HC and with/without dictionary), the logic was getting somewhat convoluted otherwise. This was done by extending the compressionParameters struct that previously contained just a single function pointer. It now contains 4 function pointers for init/reset/compress/cleanup, with the related state. The functions get a pointer to the structure as their first argument (inspired by C++), so that they can access the state values in the struct. --- programs/bench.c | 276 +++++++++++++++++++++++++++++++++++++++++++++--------- programs/bench.h | 3 +- programs/lz4cli.c | 18 ++-- 3 files changed, 244 insertions(+), 53 deletions(-) diff --git a/programs/bench.c b/programs/bench.c index 5934935..3357d14 100644 --- a/programs/bench.c +++ b/programs/bench.c @@ -45,17 +45,176 @@ #include "datagen.h" /* RDG_genBuffer */ #include "xxhash.h" +#include "bench.h" - +#define LZ4_STATIC_LINKING_ONLY #include "lz4.h" -#define COMPRESSOR0 LZ4_compress_local -static int LZ4_compress_local(const char* src, char* dst, int srcSize, int dstSize, int clevel) { - int const acceleration = (clevel < 0) ? -clevel + 1 : 1; - return LZ4_compress_fast(src, dst, srcSize, dstSize, acceleration); -} +#define LZ4_HC_STATIC_LINKING_ONLY #include "lz4hc.h" -#define COMPRESSOR1 LZ4_compress_HC -#define DEFAULTCOMPRESSOR COMPRESSOR0 + + +/* ************************************* +* Compression parameters and functions +***************************************/ + +struct compressionParameters +{ + int cLevel; + const char* dictBuf; + int dictSize; + + LZ4_stream_t* LZ4_stream; + LZ4_stream_t* LZ4_dictStream; + LZ4_streamHC_t* LZ4_streamHC; + LZ4_streamHC_t* LZ4_dictStreamHC; + + void (*initFunction)( + struct compressionParameters* pThis); + void (*resetFunction)( + const struct compressionParameters* pThis); + int (*blockFunction)( + const struct compressionParameters* pThis, + const char* src, char* dst, int srcSize, int dstSize); + void (*cleanupFunction)( + const struct compressionParameters* pThis); +}; + +static void LZ4_compressInitNoStream( + struct compressionParameters* pThis) +{ + pThis->LZ4_stream = NULL; + pThis->LZ4_dictStream = NULL; + pThis->LZ4_streamHC = NULL; + pThis->LZ4_dictStreamHC = NULL; +} + +static void LZ4_compressInitStream( + struct compressionParameters* pThis) +{ + pThis->LZ4_stream = LZ4_createStream(); + pThis->LZ4_dictStream = LZ4_createStream(); + pThis->LZ4_streamHC = NULL; + pThis->LZ4_dictStreamHC = NULL; + LZ4_loadDict(pThis->LZ4_dictStream, pThis->dictBuf, pThis->dictSize); +} + +static void LZ4_compressInitStreamHC( + struct compressionParameters* pThis) +{ + pThis->LZ4_stream = NULL; + pThis->LZ4_dictStream = NULL; + pThis->LZ4_streamHC = LZ4_createStreamHC(); + pThis->LZ4_dictStreamHC = LZ4_createStreamHC(); + LZ4_loadDictHC(pThis->LZ4_dictStreamHC, pThis->dictBuf, pThis->dictSize); +} + +static void LZ4_compressResetNoStream( + const struct compressionParameters* pThis) +{ + (void)pThis; +} + +static void LZ4_compressResetStream( + const struct compressionParameters* pThis) +{ + LZ4_resetStream_fast(pThis->LZ4_stream); + LZ4_attach_dictionary(pThis->LZ4_stream, pThis->LZ4_dictStream); +} + +static void LZ4_compressResetStreamHC( + const struct compressionParameters* pThis) +{ + LZ4_resetStreamHC_fast(pThis->LZ4_streamHC, pThis->cLevel); + LZ4_attach_HC_dictionary(pThis->LZ4_streamHC, pThis->LZ4_dictStreamHC); +} + +static int LZ4_compressBlockNoStream( + const struct compressionParameters* pThis, + const char* src, char* dst, + int srcSize, int dstSize) +{ + int const acceleration = (pThis->cLevel < 0) ? -pThis->cLevel + 1 : 1; + return LZ4_compress_fast(src, dst, srcSize, dstSize, acceleration); +} + +static int LZ4_compressBlockNoStreamHC( + const struct compressionParameters* pThis, + const char* src, char* dst, + int srcSize, int dstSize) +{ + return LZ4_compress_HC(src, dst, srcSize, dstSize, pThis->cLevel); +} + +static int LZ4_compressBlockStream( + const struct compressionParameters* pThis, + const char* src, char* dst, + int srcSize, int dstSize) +{ + int const acceleration = (pThis->cLevel < 0) ? -pThis->cLevel + 1 : 1; + return LZ4_compress_fast_continue(pThis->LZ4_stream, src, dst, srcSize, dstSize, acceleration); +} + +static int LZ4_compressBlockStreamHC( + const struct compressionParameters* pThis, + const char* src, char* dst, + int srcSize, int dstSize) +{ + return LZ4_compress_HC_continue(pThis->LZ4_streamHC, src, dst, srcSize, dstSize); +} + +static void LZ4_compressCleanupNoStream( + const struct compressionParameters* pThis) +{ + (void)pThis; +} + +static void LZ4_compressCleanupStream( + const struct compressionParameters* pThis) +{ + LZ4_freeStream(pThis->LZ4_stream); + LZ4_freeStream(pThis->LZ4_dictStream); +} + +static void LZ4_compressCleanupStreamHC( + const struct compressionParameters* pThis) +{ + LZ4_freeStreamHC(pThis->LZ4_streamHC); + LZ4_freeStreamHC(pThis->LZ4_dictStreamHC); +} + +static void LZ4_buildCompressionParameters( + struct compressionParameters* pParams, + int cLevel, const char* dictBuf, int dictSize) +{ + pParams->cLevel = cLevel; + pParams->dictBuf = dictBuf; + pParams->dictSize = dictSize; + + if (dictSize) { + if (cLevel < LZ4HC_CLEVEL_MIN) { + pParams->initFunction = LZ4_compressInitStream; + pParams->resetFunction = LZ4_compressResetStream; + pParams->blockFunction = LZ4_compressBlockStream; + pParams->cleanupFunction = LZ4_compressCleanupStream; + } else { + pParams->initFunction = LZ4_compressInitStreamHC; + pParams->resetFunction = LZ4_compressResetStreamHC; + pParams->blockFunction = LZ4_compressBlockStreamHC; + pParams->cleanupFunction = LZ4_compressCleanupStreamHC; + } + } else { + pParams->initFunction = LZ4_compressInitNoStream; + pParams->resetFunction = LZ4_compressResetNoStream; + pParams->cleanupFunction = LZ4_compressCleanupNoStream; + + if (cLevel < LZ4HC_CLEVEL_MIN) { + pParams->blockFunction = LZ4_compressBlockNoStream; + } else { + pParams->blockFunction = LZ4_compressBlockNoStreamHC; + } + } +} + #define LZ4_isError(errcode) (errcode==0) @@ -79,6 +238,8 @@ static int LZ4_compress_local(const char* src, char* dst, int srcSize, int dstSi #define MB *(1 <<20) #define GB *(1U<<30) +#define LZ4_MAX_DICT_SIZE (64 KB) + static const size_t maxMemory = (sizeof(size_t)==4) ? (2 GB - 64 MB) : (size_t)(1ULL << ((sizeof(size_t)*8)-31)); static U32 g_compressibilityDefault = 50; @@ -152,17 +313,13 @@ typedef struct { size_t resSize; } blockParam_t; -struct compressionParameters -{ - int (*compressionFunction)(const char* src, char* dst, int srcSize, int dstSize, int cLevel); -}; - #define MIN(a,b) ((a)<(b) ? (a) : (b)) #define MAX(a,b) ((a)>(b) ? (a) : (b)) static int BMK_benchMem(const void* srcBuffer, size_t srcSize, const char* displayName, int cLevel, - const size_t* fileSizes, U32 nbFiles) + const size_t* fileSizes, U32 nbFiles, + const char* dictBuf, int dictSize) { size_t const blockSize = (g_blockSize>=32 ? g_blockSize : srcSize) + (!srcSize) /* avoid div by 0 */ ; U32 const maxNbBlocks = (U32) ((srcSize + (blockSize-1)) / blockSize) + nbFiles; @@ -172,27 +329,16 @@ static int BMK_benchMem(const void* srcBuffer, size_t srcSize, void* const resultBuffer = malloc(srcSize); U32 nbBlocks; struct compressionParameters compP; - int cfunctionId; /* checks */ if (!compressedBuffer || !resultBuffer || !blockTable) EXM_THROW(31, "allocation error : not enough memory"); - /* init */ if (strlen(displayName)>17) displayName += strlen(displayName)-17; /* can only display 17 characters */ - /* Init */ - if (cLevel < LZ4HC_CLEVEL_MIN) cfunctionId = 0; else cfunctionId = 1; - switch (cfunctionId) - { -#ifdef COMPRESSOR0 - case 0 : compP.compressionFunction = COMPRESSOR0; break; -#endif -#ifdef COMPRESSOR1 - case 1 : compP.compressionFunction = COMPRESSOR1; break; -#endif - default : compP.compressionFunction = DEFAULTCOMPRESSOR; - } + /* init */ + LZ4_buildCompressionParameters(&compP, cLevel, dictBuf, dictSize); + compP.initFunction(&compP); /* Init blockTable data */ { const char* srcPtr = (const char*)srcBuffer; @@ -256,8 +402,12 @@ static int BMK_benchMem(const void* srcBuffer, size_t srcSize, U32 nbLoops; for (nbLoops=0; nbLoops < nbCompressionLoops; nbLoops++) { U32 blockNb; + compP.resetFunction(&compP); for (blockNb=0; blockNb 1) ? mfName : fileNamesTable[0]; BMK_benchCLevel(srcBuffer, benchedSize, displayName, cLevel, cLevelLast, - fileSizes, nbFiles); + fileSizes, nbFiles, + dictBuf, dictSize); } /* clean up */ @@ -497,7 +655,8 @@ static void BMK_benchFileTable(const char** fileNamesTable, unsigned nbFiles, } -static void BMK_syntheticTest(int cLevel, int cLevelLast, double compressibility) +static void BMK_syntheticTest(int cLevel, int cLevelLast, double compressibility, + const char* dictBuf, int dictSize) { char name[20] = {0}; size_t benchedSize = 10000000; @@ -511,7 +670,7 @@ static void BMK_syntheticTest(int cLevel, int cLevelLast, double compressibility /* Bench */ snprintf (name, sizeof(name), "Synthetic %2u%%", (unsigned)(compressibility*100)); - BMK_benchCLevel(srcBuffer, benchedSize, name, cLevel, cLevelLast, &benchedSize, 1); + BMK_benchCLevel(srcBuffer, benchedSize, name, cLevel, cLevelLast, &benchedSize, 1, dictBuf, dictSize); /* clean up */ free(srcBuffer); @@ -519,7 +678,8 @@ static void BMK_syntheticTest(int cLevel, int cLevelLast, double compressibility int BMK_benchFilesSeparately(const char** fileNamesTable, unsigned nbFiles, - int cLevel, int cLevelLast) + int cLevel, int cLevelLast, + const char* dictBuf, int dictSize) { unsigned fileNb; if (cLevel > LZ4HC_CLEVEL_MAX) cLevel = LZ4HC_CLEVEL_MAX; @@ -528,29 +688,59 @@ int BMK_benchFilesSeparately(const char** fileNamesTable, unsigned nbFiles, if (cLevelLast > cLevel) DISPLAYLEVEL(2, "Benchmarking levels from %d to %d\n", cLevel, cLevelLast); for (fileNb=0; fileNb LZ4HC_CLEVEL_MAX) cLevel = LZ4HC_CLEVEL_MAX; if (cLevelLast > LZ4HC_CLEVEL_MAX) cLevelLast = LZ4HC_CLEVEL_MAX; if (cLevelLast < cLevel) cLevelLast = cLevel; if (cLevelLast > cLevel) DISPLAYLEVEL(2, "Benchmarking levels from %d to %d\n", cLevel, cLevelLast); + if (dictFileName) { + FILE* dictFile = NULL; + U64 dictFileSize = UTIL_getFileSize(dictFileName); + if (!dictFileSize) EXM_THROW(25, "Dictionary error : could not stat dictionary file"); + + dictFile = fopen(dictFileName, "rb"); + if (!dictFile) EXM_THROW(25, "Dictionary error : could not open dictionary file"); + + if (dictFileSize > LZ4_MAX_DICT_SIZE) { + dictSize = LZ4_MAX_DICT_SIZE; + if (UTIL_fseek(dictFile, dictFileSize - dictSize, SEEK_SET)) + EXM_THROW(25, "Dictionary error : could not seek dictionary file"); + } else { + dictSize = (int)dictFileSize; + } + + dictBuf = (char *)malloc(dictSize); + if (!dictBuf) EXM_THROW(25, "Allocation error : not enough memory"); + + if (fread(dictBuf, 1, dictSize, dictFile) != (size_t)dictSize) + EXM_THROW(25, "Dictionary error : could not read dictionary file"); + + fclose(dictFile); + } + if (nbFiles == 0) - BMK_syntheticTest(cLevel, cLevelLast, compressibility); + BMK_syntheticTest(cLevel, cLevelLast, compressibility, dictBuf, dictSize); else { if (g_benchSeparately) - BMK_benchFilesSeparately(fileNamesTable, nbFiles, cLevel, cLevelLast); + BMK_benchFilesSeparately(fileNamesTable, nbFiles, cLevel, cLevelLast, dictBuf, dictSize); else - BMK_benchFileTable(fileNamesTable, nbFiles, cLevel, cLevelLast); + BMK_benchFileTable(fileNamesTable, nbFiles, cLevel, cLevelLast, dictBuf, dictSize); } + + free(dictBuf); return 0; } diff --git a/programs/bench.h b/programs/bench.h index bb67bee..22ebf60 100644 --- a/programs/bench.h +++ b/programs/bench.h @@ -26,7 +26,8 @@ #include int BMK_benchFiles(const char** fileNamesTable, unsigned nbFiles, - int cLevel, int cLevelLast); + int cLevel, int cLevelLast, + const char* dictFileName); /* Set Parameters */ void BMK_setNbSeconds(unsigned nbLoops); diff --git a/programs/lz4cli.c b/programs/lz4cli.c index 5da7654..e95050b 100644 --- a/programs/lz4cli.c +++ b/programs/lz4cli.c @@ -625,10 +625,18 @@ int main(int argc, const char** argv) #endif } + if (dictionary_filename) { + if (!strcmp(dictionary_filename, stdinmark) && IS_CONSOLE(stdin)) { + DISPLAYLEVEL(1, "refusing to read from a console\n"); + exit(1); + } + LZ4IO_setDictionaryFilename(prefs, dictionary_filename); + } + /* benchmark and test modes */ if (mode == om_bench) { BMK_setNotificationLevel(displayLevel); - operationResult = BMK_benchFiles(inFileNames, ifnIdx, cLevel, cLevelLast); + operationResult = BMK_benchFiles(inFileNames, ifnIdx, cLevel, cLevelLast, dictionary_filename); goto _cleanup; } @@ -638,14 +646,6 @@ int main(int argc, const char** argv) mode = om_decompress; /* defer to decompress */ } - if (dictionary_filename) { - if (!strcmp(dictionary_filename, stdinmark) && IS_CONSOLE(stdin)) { - DISPLAYLEVEL(1, "refusing to read from a console\n"); - exit(1); - } - LZ4IO_setDictionaryFilename(prefs, dictionary_filename); - } - /* compress or decompress */ if (!input_filename) input_filename = stdinmark; /* Check if input is defined as console; trigger an error in this case */ -- cgit v1.2.3 From 935b432be14bb97ddba14880640d69d98ea5ad57 Mon Sep 17 00:00:00 2001 From: Jerome Duval Date: Sun, 8 Sep 2019 13:36:26 +0200 Subject: bring support for Haiku up to date. --- programs/lz4cli.c | 4 ++++ programs/platform.h | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/programs/lz4cli.c b/programs/lz4cli.c index e95050b..94f466f 100644 --- a/programs/lz4cli.c +++ b/programs/lz4cli.c @@ -658,7 +658,11 @@ int main(int argc, const char** argv) if (!output_filename) output_filename = stdoutmark; } else{ +#ifdef UTIL_HAS_CREATEFILELIST if (!recursive && !UTIL_isRegFile(input_filename)) { +#else + if (!UTIL_isRegFile(input_filename)) { +#endif DISPLAYLEVEL(1, "%s: is not a regular file \n", input_filename); exit(1); } diff --git a/programs/platform.h b/programs/platform.h index 7e2cb58..bdbcb3b 100644 --- a/programs/platform.h +++ b/programs/platform.h @@ -81,7 +81,8 @@ extern "C" { #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)) /* UNIX-like OS */ \ || defined(__midipix__) || defined(__VMS)) # if (defined(__APPLE__) && defined(__MACH__)) || defined(__SVR4) || defined(_AIX) || defined(__hpux) /* POSIX.1–2001 (SUSv3) conformant */ \ - || defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__MidnightBSD__) /* BSD distros */ + || defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__MidnightBSD__) /* BSD distros */ \ + || defined(__HAIKU__) # define PLATFORM_POSIX_VERSION 200112L # else # if defined(__linux__) || defined(__linux) -- cgit v1.2.3 From 059682fb010167afdc6f1087b3b7f4bf3488a53f Mon Sep 17 00:00:00 2001 From: Andrew Martin Date: Sat, 30 Nov 2019 06:58:54 -0500 Subject: Fix typos in streaming_api_basics.md --- examples/streaming_api_basics.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/streaming_api_basics.md b/examples/streaming_api_basics.md index 90065e4..1ccc6e3 100644 --- a/examples/streaming_api_basics.md +++ b/examples/streaming_api_basics.md @@ -10,7 +10,7 @@ LZ4 has the following API sets : such as LZ4 command line utility, node-lz4, etc. - "Block" API : This is recommended for simple purpose. It compress single raw memory block to LZ4 memory block and vice versa. - - "Streaming" API : This is designed for complex thing. + - "Streaming" API : This is designed for complex things. For example, compress huge stream data in restricted memory environment. Basically, you should use "Auto Framing" API. @@ -19,13 +19,13 @@ But if you want to write advanced application, it's time to use Block or Streami ## What is difference between Block and Streaming API ? -Block API (de)compresses single contiguous memory block. -In other words, LZ4 library find redundancy from single contiguous memory block. -Streaming API does same thing but (de)compress multiple adjacent contiguous memory block. +Block API (de)compresses a single contiguous memory block. +In other words, LZ4 library finds redundancy from a single contiguous memory block. +Streaming API does same thing but (de)compresses multiple adjacent contiguous memory blocks. So LZ4 library could find more redundancy than Block API. The following figure shows difference between API and block sizes. -In these figures, original data is splitted to 4KiBytes contiguous chunks. +In these figures, the original data is split into 4KiBytes contiguous chunks. ``` Original Data @@ -81,7 +81,7 @@ This dependency improves compression ratio. ## Restriction of Streaming API -For the efficiency, Streaming API doesn't keep mirror copy of dependent (de)compressed memory. +For efficiency, Streaming API doesn't keep a mirror copy of dependent (de)compressed memory. This means users should keep these dependent (de)compressed memory explicitly. Usually, "Dependent memory" is previous adjacent contiguous memory up to 64KiBytes. LZ4 will not access further memories. -- cgit v1.2.3 From 0f6cbd996fbaade6715c7009c6f8dbb0f23f0b4b Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 2 Dec 2019 16:38:33 -0800 Subject: faster decoding speed with Visual by enabling the fast decoder path. Visual requires a different set of macro constants to detect x86 / x64. On my laptop, decoding speed on x64 went up from 3.12 to 3.45 GB/s. 32-bit is less impressive, though still favorable, with speed increasing from 2.55 to 2.60 GB/s. So both cases are now enabled. Suggested by Bartosz Taudul (@wolfpld). --- .gitignore | 3 +++ lib/lz4.c | 6 +++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 2a59a7d..d7ba96e 100644 --- a/.gitignore +++ b/.gitignore @@ -26,6 +26,9 @@ _codelite_lz4/ bin/ *.zip +# analyzers +infer-out + # Mac .DS_Store *.dSYM diff --git a/lib/lz4.c b/lib/lz4.c index 85c3322..866855b 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -374,12 +374,12 @@ static const int dec64table[8] = {0, 0, 0, -1, -4, 1, 2, 3}; #ifndef LZ4_FAST_DEC_LOOP -# if defined(__i386__) || defined(__x86_64__) +# if defined __i386__ || defined _M_IX86 || defined __x86_64__ || defined _M_X64 # define LZ4_FAST_DEC_LOOP 1 # elif defined(__aarch64__) && !defined(__clang__) /* On aarch64, we disable this optimization for clang because on certain - * mobile chipsets and clang, it reduces performance. For more information - * refer to https://github.com/lz4/lz4/pull/707. */ + * mobile chipsets, performance is reduced with clang. For information + * refer to https://github.com/lz4/lz4/pull/707 */ # define LZ4_FAST_DEC_LOOP 1 # else # define LZ4_FAST_DEC_LOOP 0 -- cgit v1.2.3 From d755f87f9fb7d625cdb9c9b54ab02e451cd80e9e Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 3 Dec 2019 14:49:22 -0800 Subject: fixed lz4hc assert error when src ptr is in very low memory area (< 64K), the virtual reference to data in dictionary might end up in a very high memory address. Since it's not a "real" memory address, just a virtual one, to calculate distance, it doesn't matter : only distance matters. The assert was to restrictive. Fixed. --- lib/lz4.c | 16 ++++++++-------- lib/lz4hc.c | 6 +++--- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 866855b..fc3fb88 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -225,15 +225,15 @@ static const int LZ4_minLength = (MFLIMIT+1); #if defined(LZ4_DEBUG) && (LZ4_DEBUG>=2) # include -static int g_debuglog_enable = 1; -# define DEBUGLOG(l, ...) { \ - if ((g_debuglog_enable) && (l<=LZ4_DEBUG)) { \ - fprintf(stderr, __FILE__ ": "); \ - fprintf(stderr, __VA_ARGS__); \ - fprintf(stderr, " \n"); \ - } } + static int g_debuglog_enable = 1; +# define DEBUGLOG(l, ...) { \ + if ((g_debuglog_enable) && (l<=LZ4_DEBUG)) { \ + fprintf(stderr, __FILE__ ": "); \ + fprintf(stderr, __VA_ARGS__); \ + fprintf(stderr, " \n"); \ + } } #else -# define DEBUGLOG(l, ...) {} /* disabled */ +# define DEBUGLOG(l, ...) {} /* disabled */ #endif diff --git a/lib/lz4hc.c b/lib/lz4hc.c index 5922ed7..afadb3f 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -389,8 +389,8 @@ LZ4HC_InsertAndGetWiderMatch ( if (lookBackLength==0) { /* no back possible */ size_t const maxML = MIN(currentSegmentLength, srcPatternLength); if ((size_t)longest < maxML) { - assert(base + matchIndex < ip); - if (ip - (base+matchIndex) > LZ4_DISTANCE_MAX) break; + assert(base + matchIndex != ip); + if ((size_t)(ip - base) - matchIndex > LZ4_DISTANCE_MAX) break; assert(maxML < 2 GB); longest = (int)maxML; *matchpos = base + matchIndex; /* virtual pos, relative to ip, to retrieve offset */ @@ -1028,7 +1028,7 @@ int LZ4_loadDictHC (LZ4_streamHC_t* LZ4_streamHCPtr, const char* dictionary, int dictSize) { LZ4HC_CCtx_internal* const ctxPtr = &LZ4_streamHCPtr->internal_donotuse; - DEBUGLOG(4, "LZ4_loadDictHC(%p, %p, %d)", LZ4_streamHCPtr, dictionary, dictSize); + DEBUGLOG(4, "LZ4_loadDictHC(ctx:%p, dict:%p, dictSize:%d)", LZ4_streamHCPtr, dictionary, dictSize); assert(LZ4_streamHCPtr != NULL); if (dictSize > 64 KB) { dictionary += (size_t)dictSize - 64 KB; -- cgit v1.2.3 From d28e49bf75dcd5f8668577a73d0a6690de1b9f24 Mon Sep 17 00:00:00 2001 From: NancyLi1013 Date: Thu, 16 Jan 2020 00:49:23 -0800 Subject: Add vcpkg installation instructions --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index 607fc4e..451238b 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,17 @@ It is compatible with parallel builds (`-j#`). [redirection]: https://www.gnu.org/prep/standards/html_node/Directory-Variables.html [command redefinition]: https://www.gnu.org/prep/standards/html_node/Utilities-in-Makefiles.html +Building LZ4 - Using vcpkg + +You can download and install LZ4 using the [vcpkg](https://github.com/Microsoft/vcpkg) dependency manager: + + git clone https://github.com/Microsoft/vcpkg.git + cd vcpkg + ./bootstrap-vcpkg.sh + ./vcpkg integrate install + vcpkg install lz4 + +The LZ4 port in vcpkg is kept up to date by Microsoft team members and community contributors. If the version is out of date, please [create an issue or pull request](https://github.com/Microsoft/vcpkg) on the vcpkg repository. Documentation ------------------------- -- cgit v1.2.3 From 7224f9bd5d97dbd2d0dfba297b6682e8f45b0ef0 Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Fri, 17 Jan 2020 00:37:47 +0100 Subject: Force inline small functions used by LZ4_compress_generic. --- lib/lz4.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index fc3fb88..82ab490 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -632,7 +632,7 @@ int LZ4_decompress_safe_forceExtDict(const char* source, char* dest, /*-****************************** * Compression functions ********************************/ -static U32 LZ4_hash4(U32 sequence, tableType_t const tableType) +LZ4_FORCE_INLINE U32 LZ4_hash4(U32 sequence, tableType_t const tableType) { if (tableType == byU16) return ((sequence * 2654435761U) >> ((MINMATCH*8)-(LZ4_HASHLOG+1))); @@ -640,7 +640,7 @@ static U32 LZ4_hash4(U32 sequence, tableType_t const tableType) return ((sequence * 2654435761U) >> ((MINMATCH*8)-LZ4_HASHLOG)); } -static U32 LZ4_hash5(U64 sequence, tableType_t const tableType) +LZ4_FORCE_INLINE U32 LZ4_hash5(U64 sequence, tableType_t const tableType) { const U32 hashLog = (tableType == byU16) ? LZ4_HASHLOG+1 : LZ4_HASHLOG; if (LZ4_isLittleEndian()) { @@ -658,7 +658,7 @@ LZ4_FORCE_INLINE U32 LZ4_hashPosition(const void* const p, tableType_t const tab return LZ4_hash4(LZ4_read32(p), tableType); } -static void LZ4_clearHash(U32 h, void* tableBase, tableType_t const tableType) +LZ4_FORCE_INLINE void LZ4_clearHash(U32 h, void* tableBase, tableType_t const tableType) { switch (tableType) { @@ -670,7 +670,7 @@ static void LZ4_clearHash(U32 h, void* tableBase, tableType_t const tableType) } } -static void LZ4_putIndexOnHash(U32 idx, U32 h, void* tableBase, tableType_t const tableType) +LZ4_FORCE_INLINE void LZ4_putIndexOnHash(U32 idx, U32 h, void* tableBase, tableType_t const tableType) { switch (tableType) { @@ -682,7 +682,7 @@ static void LZ4_putIndexOnHash(U32 idx, U32 h, void* tableBase, tableType_t cons } } -static void LZ4_putPositionOnHash(const BYTE* p, U32 h, +LZ4_FORCE_INLINE void LZ4_putPositionOnHash(const BYTE* p, U32 h, void* tableBase, tableType_t const tableType, const BYTE* srcBase) { @@ -707,7 +707,7 @@ LZ4_FORCE_INLINE void LZ4_putPosition(const BYTE* p, void* tableBase, tableType_ * Assumption 1 : only valid if tableType == byU32 or byU16. * Assumption 2 : h is presumed valid (within limits of hash table) */ -static U32 LZ4_getIndexOnHash(U32 h, const void* tableBase, tableType_t tableType) +LZ4_FORCE_INLINE U32 LZ4_getIndexOnHash(U32 h, const void* tableBase, tableType_t tableType) { LZ4_STATIC_ASSERT(LZ4_MEMORY_USAGE > 2); if (tableType == byU32) { -- cgit v1.2.3 From e55548b0bd553cc25d8c0994db6f5d49663fb3f5 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 27 Jan 2020 13:51:06 -0800 Subject: updated NEWS for v1.9.3 --- NEWS | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/NEWS b/NEWS index 860f15b..44c45be 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,21 @@ +v1.9.3 +perf: faster speed on Visual Studio, by @wolfpld + + +v1.9.2 +fix : out-of-bound read in exceptional circumstances when using decompress_partial(), by @terrelln +fix : slim opportunity for out-of-bound write with compress_fast() with a large enough input and when providing an output smaller than recommended (< LZ4_compressBound(inputSize)), by @terrelln +fix : rare data corruption bug with LZ4_compress_destSize(), by @terrelln +fix : data corruption bug when Streaming with an Attached Dict in HC Mode, by @felixhandte +perf: enable LZ4_FAST_DEC_LOOP on aarch64/GCC by default, by @prekageo +perf: improved lz4frame streaming API speed, by @dreambottle +perf: speed up lz4hc on slow patterns when using external dictionary, by @terrelln +api: better in-place decompression and compression support +cli : --list supports multi-frames files, by @gstedman +cli: --version outputs to stdout +cli : add option --best as an alias of -12 , by @Low-power +misc: Integration into oss-fuzz by @cmeister2, expanded list of scenarios by @terrelln + v1.9.1 fix : decompression functions were reading a few bytes beyond input size (introduced in v1.9.0, reported by @ppodolsky and @danlark1) api : fix : lz4frame initializers compatibility with c++, reported by @degski -- cgit v1.2.3 From 2553cd550b53532cc7d19831db520c9812cbb667 Mon Sep 17 00:00:00 2001 From: Yan Pashkovsky Date: Fri, 31 Jan 2020 19:44:56 +0300 Subject: fix: allocate LZ4HC_optimal_t opt on heap each time (#837) --- lib/lz4hc.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/lz4hc.c b/lib/lz4hc.c index afadb3f..8049840 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -1287,8 +1287,13 @@ static int LZ4HC_compress_optimal ( LZ4HC_CCtx_internal* ctx, const dictCtx_directive dict, const HCfavor_e favorDecSpeed) { + int retval = 0; #define TRAILING_LITERALS 3 +#ifdef LZ4HC_HEAPMODE + LZ4HC_optimal_t* opt = (LZ4HC_optimal_t*)malloc(sizeof(LZ4HC_optimal_t) * (LZ4_OPT_NUM + TRAILING_LITERALS)); +#else LZ4HC_optimal_t opt[LZ4_OPT_NUM + TRAILING_LITERALS]; /* ~64 KB, which is a bit large for stack... */ +#endif const BYTE* ip = (const BYTE*) source; const BYTE* anchor = ip; @@ -1505,7 +1510,10 @@ static int LZ4HC_compress_optimal ( LZ4HC_CCtx_internal* ctx, size_t const totalSize = 1 + litLength + lastRunSize; if (limit == fillOutput) oend += LASTLITERALS; /* restore correct value */ if (limit && (op + totalSize > oend)) { - if (limit == limitedOutput) return 0; /* Check output limit */ + if (limit == limitedOutput) { /* Check output limit */ + retval = 0; + goto _return_label; + } /* adapt lastRunSize to fill 'dst' */ lastRunSize = (size_t)(oend - op) - 1; litLength = (lastRunSize + 255 - RUN_MASK) / 255; @@ -1527,12 +1535,17 @@ static int LZ4HC_compress_optimal ( LZ4HC_CCtx_internal* ctx, /* End */ *srcSizePtr = (int) (((const char*)ip) - source); - return (int) ((char*)op-dst); + retval = (int) ((char*)op-dst); + goto _return_label; _dest_overflow: if (limit == fillOutput) { op = opSaved; /* restore correct out pointer */ goto _last_literals; } - return 0; + _return_label: +#ifdef LZ4HC_HEAPMODE + free(opt); +#endif + return retval; } -- cgit v1.2.3 From 4cc9d863a3ef97b41979dbb119178ebe1df46dd1 Mon Sep 17 00:00:00 2001 From: Yan Pashkovsky Date: Mon, 3 Feb 2020 16:50:02 +0300 Subject: fix malloc handling --- lib/lz4hc.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/lz4hc.c b/lib/lz4hc.c index 8049840..b75514f 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -1290,7 +1290,7 @@ static int LZ4HC_compress_optimal ( LZ4HC_CCtx_internal* ctx, int retval = 0; #define TRAILING_LITERALS 3 #ifdef LZ4HC_HEAPMODE - LZ4HC_optimal_t* opt = (LZ4HC_optimal_t*)malloc(sizeof(LZ4HC_optimal_t) * (LZ4_OPT_NUM + TRAILING_LITERALS)); + LZ4HC_optimal_t* const opt = (LZ4HC_optimal_t*)malloc(sizeof(LZ4HC_optimal_t) * (LZ4_OPT_NUM + TRAILING_LITERALS)); #else LZ4HC_optimal_t opt[LZ4_OPT_NUM + TRAILING_LITERALS]; /* ~64 KB, which is a bit large for stack... */ #endif @@ -1305,6 +1305,9 @@ static int LZ4HC_compress_optimal ( LZ4HC_CCtx_internal* ctx, BYTE* oend = op + dstCapacity; /* init */ +#ifdef LZ4HC_HEAPMODE + if (opt == NULL) goto _return_label; +#endif DEBUGLOG(5, "LZ4HC_compress_optimal(dst=%p, dstCapa=%u)", dst, (unsigned)dstCapacity); *srcSizePtr = 0; if (limit == fillOutput) oend -= LASTLITERALS; /* Hack for support LZ4 format restriction */ -- cgit v1.2.3 From ac4940cd98e55ae1fffd5f2f331d33c8cdb12b53 Mon Sep 17 00:00:00 2001 From: Filipe Calasans Date: Thu, 6 Feb 2020 21:39:54 -0800 Subject: Fix lz4cli --list option --- programs/lz4cli.c | 2 +- programs/lz4io.c | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/programs/lz4cli.c b/programs/lz4cli.c index 94f466f..aadccab 100644 --- a/programs/lz4cli.c +++ b/programs/lz4cli.c @@ -670,7 +670,7 @@ int main(int argc, const char** argv) /* No output filename ==> try to select one automatically (when possible) */ while ((!output_filename) && (multiple_inputs==0)) { - if (!IS_CONSOLE(stdout)) { + if (!IS_CONSOLE(stdout) && mode != om_list) { /* Default to stdout whenever stdout is not the console. * Note : this policy may change in the future, therefore don't rely on it ! * To ensure `stdout` is explicitly selected, use `-c` command flag. diff --git a/programs/lz4io.c b/programs/lz4io.c index d818535..ec6dc7f 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -90,6 +90,7 @@ * Macros **************************************/ #define DISPLAY(...) fprintf(stderr, __VA_ARGS__) +#define DISPLAYOUT(...) fprintf(stdout, __VA_ARGS__) #define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); } static int g_displayLevel = 0; /* 0 : no display ; 1: errors ; 2 : + result + interaction + warnings ; 3 : + progression; 4 : + information */ @@ -1561,7 +1562,7 @@ int LZ4IO_displayCompressedFilesInfo(const char** inFileNames, size_t ifnIdx) int result = 0; size_t idx = 0; if (g_displayLevel < 3) { - DISPLAY("%10s %14s %5s %11s %13s %9s %s\n", + DISPLAYOUT("%10s %14s %5s %11s %13s %9s %s\n", "Frames", "Type", "Block", "Compressed", "Uncompressed", "Ratio", "Filename"); } for (; idx < ifnIdx; idx++) { @@ -1586,7 +1587,7 @@ int LZ4IO_displayCompressedFilesInfo(const char** inFileNames, size_t ifnIdx) if (g_displayLevel < 3) { /* Display Summary */ { char buffers[3][10]; - DISPLAY("%10llu %14s %5s %11s %13s ", + DISPLAYOUT("%10llu %14s %5s %11s %13s ", cfinfo.frameCount, cfinfo.eqFrameTypes ? LZ4IO_frameTypeNames[cfinfo.frameSummary.frameType] : "-" , cfinfo.eqBlockTypes ? LZ4IO_blockTypeID(cfinfo.frameSummary.lz4FrameInfo.blockSizeID, @@ -1595,9 +1596,9 @@ int LZ4IO_displayCompressedFilesInfo(const char** inFileNames, size_t ifnIdx) cfinfo.allContentSize ? LZ4IO_toHuman((long double)cfinfo.frameSummary.lz4FrameInfo.contentSize, buffers[2]) : "-"); if (cfinfo.allContentSize) { double const ratio = (double)cfinfo.fileSize / cfinfo.frameSummary.lz4FrameInfo.contentSize * 100; - DISPLAY("%9.2f%% %s \n", ratio, cfinfo.fileName); + DISPLAYOUT("%9.2f%% %s \n", ratio, cfinfo.fileName); } else { - DISPLAY("%9s %s\n", + DISPLAYOUT("%9s %s\n", "-", cfinfo.fileName); } -- cgit v1.2.3 From 781417a36997548c54c445c0e2e9634e69b0e0c6 Mon Sep 17 00:00:00 2001 From: Filipe Calasans Date: Fri, 7 Feb 2020 14:52:25 -0800 Subject: Implement -m option with legacy format on cli --- programs/lz4cli.c | 11 +++++++++-- programs/lz4io.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/programs/lz4cli.c b/programs/lz4cli.c index aadccab..67dcaa1 100644 --- a/programs/lz4cli.c +++ b/programs/lz4cli.c @@ -94,7 +94,10 @@ static unsigned displayLevel = 2; /* 0 : no display ; 1: errors only ; 2 : dow #define DEFAULT_COMPRESSOR LZ4IO_compressFilename #define DEFAULT_DECOMPRESSOR LZ4IO_decompressFilename int LZ4IO_compressFilename_Legacy(LZ4IO_prefs_t* const prefs, const char* input_filename, const char* output_filename, int compressionlevel); /* hidden function */ - +int LZ4IO_compressMultipleFilenames_Legacy(LZ4IO_prefs_t* const prefs, + const char** inFileNamesTable, int ifntSize, + const char* suffix, + int compressionLevel); /*-*************************** * Functions @@ -754,7 +757,11 @@ int main(int argc, const char** argv) } else { /* compression is default action */ if (legacy_format) { DISPLAYLEVEL(3, "! Generating LZ4 Legacy format (deprecated) ! \n"); - LZ4IO_compressFilename_Legacy(prefs, input_filename, output_filename, cLevel); + if(multiple_inputs){ + LZ4IO_compressMultipleFilenames_Legacy(prefs, inFileNames, (int)ifnIdx, !strcmp(output_filename,stdoutmark) ? stdoutmark : LZ4_EXTENSION, cLevel); + } else { + LZ4IO_compressFilename_Legacy(prefs, input_filename, output_filename, cLevel); + } } else { if (multiple_inputs) { assert(ifnIdx <= INT_MAX); diff --git a/programs/lz4io.c b/programs/lz4io.c index ec6dc7f..7926b20 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -470,11 +470,59 @@ int LZ4IO_compressFilename_Legacy(LZ4IO_prefs_t* const prefs, const char* input_ free(in_buff); free(out_buff); fclose(finput); - fclose(foutput); + if (strcmp(output_filename,stdoutmark)) fclose(foutput); /* do not close stdout */ return 0; } +#define FNSPACE 30 +/* LZ4IO_compressMultipleFilenames_Legacy : + * This function is intentionally "hidden" (not published in .h) + * It generates multiple compressed streams using the old 'legacy' format */ +int LZ4IO_compressMultipleFilenames_Legacy(LZ4IO_prefs_t* const prefs, + const char** inFileNamesTable, int ifntSize, + const char* suffix, + int compressionLevel) +{ + int i; + int missed_files = 0; + char* dstFileName = (char*)malloc(FNSPACE); + size_t ofnSize = FNSPACE; + const size_t suffixSize = strlen(suffix); + + if (dstFileName == NULL) return ifntSize; /* not enough memory */ + + /* loop on each file */ + for (i=0; i Date: Fri, 7 Feb 2020 17:06:40 -0800 Subject: Add test scenario test-lz4-multiple-legacy --- tests/Makefile | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/tests/Makefile b/tests/Makefile index 422baba..1f8321c 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -259,6 +259,44 @@ test-lz4-multiple: lz4 datagen ! $(LZ4) -f -m tmp-tlm-concat1 notHere tmp-tlm-concat2 # must fail : notHere not present @$(RM) tmp-tlm* +test-lz4-multiple-legacy: lz4 datagen + @echo "\n ---- test multiple files (Legacy format) ----" + @./datagen -s1 > tmp-tlm1 2> $(VOID) + @./datagen -s2 -g100K > tmp-tlm2 2> $(VOID) + @./datagen -s3 -g200K > tmp-tlm3 2> $(VOID) + # compress multiple files using legacy format: one .lz4 per source file + $(LZ4) -f -l -m tmp-tlm* + test -f tmp-tlm1.lz4 + test -f tmp-tlm2.lz4 + test -f tmp-tlm3.lz4 + # decompress multiple files compressed using legacy format: one output file per .lz4 + mv tmp-tlm1 tmp-tlm1-orig + mv tmp-tlm2 tmp-tlm2-orig + mv tmp-tlm3 tmp-tlm3-orig + $(LZ4) -d -f -m tmp-tlm*.lz4 + $(LZ4) -l -d -f -m tmp-tlm*.lz4 # -l mustn't impact -d option + $(CMP) tmp-tlm1 tmp-tlm1-orig # must be identical + $(CMP) tmp-tlm2 tmp-tlm2-orig + $(CMP) tmp-tlm3 tmp-tlm3-orig + # compress multiple files into stdout using legacy format + cat tmp-tlm1.lz4 tmp-tlm2.lz4 tmp-tlm3.lz4 > tmp-tlm-concat1 + $(RM) *.lz4 + $(LZ4) -l -m tmp-tlm1 tmp-tlm2 tmp-tlm3 -c > tmp-tlm-concat2 + test ! -f tmp-tlm1.lz4 # must not create .lz4 artefact + $(CMP) tmp-tlm-concat1 tmp-tlm-concat2 # must be equivalent + # # # decompress multiple files into stdout using legacy format + $(RM) tmp-tlm-concat1 tmp-tlm-concat2 + $(LZ4) -l -f -m tmp-tlm1 tmp-tlm2 tmp-tlm3 # generate .lz4 to decompress + cat tmp-tlm1 tmp-tlm2 tmp-tlm3 > tmp-tlm-concat1 # create concatenated reference + $(RM) tmp-tlm1 tmp-tlm2 tmp-tlm3 + $(LZ4) -d -m tmp-tlm1.lz4 tmp-tlm2.lz4 tmp-tlm3.lz4 -c > tmp-tlm-concat2 + $(LZ4) -d -l -m tmp-tlm1.lz4 tmp-tlm2.lz4 tmp-tlm3.lz4 -c > tmp-tlm-concat2 # -l mustn't impact option -d + test ! -f tmp-tlm1 # must not create file artefact + $(CMP) tmp-tlm-concat1 tmp-tlm-concat2 # must be equivalent + # # # compress multiple files, one of which is absent (must fail) + ! $(LZ4) -f -l -m tmp-tlm-concat1 notHere-legacy tmp-tlm-concat2 # must fail : notHere-legacy not present + @$(RM) tmp-tlm* + test-lz4-basic: lz4 datagen unlz4 lz4cat @echo "\n ---- test lz4 basic compression/decompression ----" ./datagen -g0 | $(LZ4) -v | $(LZ4) -t @@ -393,7 +431,7 @@ test-lz4-opt-parser: lz4 datagen ./datagen -g16M -P90 | $(LZ4) -11B5 | $(LZ4) -t ./datagen -g32M -P10 | $(LZ4) -11B5D | $(LZ4) -t -test-lz4-essentials : lz4 datagen test-lz4-basic test-lz4-multiple \ +test-lz4-essentials : lz4 datagen test-lz4-basic test-lz4-multiple test-lz4-multiple-legacy \ test-lz4-frame-concatenation test-lz4-testmode \ test-lz4-contentSize test-lz4-dict @$(RM) tmp* -- cgit v1.2.3 From c5af6992f5d3af1bef7ff70f1a970841e5eb500c Mon Sep 17 00:00:00 2001 From: Bimba Shrestha Date: Tue, 3 Mar 2020 13:29:57 -0800 Subject: Adding issue templates --- .github/ISSUE_TEMPLATE/bug_report.md | 34 +++++++++++++++++++++++++++++++ .github/ISSUE_TEMPLATE/feature_request.md | 20 ++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 .github/ISSUE_TEMPLATE/bug_report.md create mode 100644 .github/ISSUE_TEMPLATE/feature_request.md diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md new file mode 100644 index 0000000..96d9476 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -0,0 +1,34 @@ +--- +name: Bug report +about: Create a report to help us improve +title: '' +labels: '' +assignees: '' + +--- + +**Describe the bug** +A clear and concise description of what the bug is. + +**To Reproduce** +Steps to reproduce the behavior: +1. Go to '...' +2. Click on '....' +3. Scroll down to '....' +4. See error + +**Expected behavior** +A clear and concise description of what you expected to happen. + +**Screenshots** +If applicable, add screenshots to help explain your problem. + +**Desktop (please complete the following information):** + - OS: [e.g. Mac] + - Version [e.g. 22] + - Compiler [e.g. gcc] + - Build System [e.g. Makefile] + - Other hardware specs [e.g Core 2 duo...] + +**Additional context** +Add any other context about the problem here. diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md new file mode 100644 index 0000000..bbcbbe7 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -0,0 +1,20 @@ +--- +name: Feature request +about: Suggest an idea for this project +title: '' +labels: '' +assignees: '' + +--- + +**Is your feature request related to a problem? Please describe.** +A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] + +**Describe the solution you'd like** +A clear and concise description of what you want to happen. + +**Describe alternatives you've considered** +A clear and concise description of any alternative solutions or features you've considered. + +**Additional context** +Add any other context or screenshots about the feature request here. -- cgit v1.2.3 From 8ec5ee30be6dc156f6aa4e2d66eef521f35dc1ee Mon Sep 17 00:00:00 2001 From: Indragie Karunaratne Date: Sun, 26 Apr 2020 14:49:34 -0700 Subject: Add LZ4FLIB_VISIBILITY for controlling library symbol visibility --- lib/lz4frame.h | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/lz4frame.h b/lib/lz4frame.h index 391e484..b6ac587 100644 --- a/lib/lz4frame.h +++ b/lib/lz4frame.h @@ -66,17 +66,22 @@ extern "C" { *****************************************************************/ /* LZ4_DLL_EXPORT : * Enable exporting of functions when building a Windows DLL - * LZ4FLIB_API : + * LZ4LIB_VISIBILITY : * Control library symbols visibility. */ +#ifndef LZ4FLIB_VISIBILITY +# if defined(__GNUC__) && (__GNUC__ >= 4) +# define LZ4FLIB_VISIBILITY __attribute__ ((visibility ("default"))) +# else +# define LZ4FLIB_VISIBILITY +# endif +#endif #if defined(LZ4_DLL_EXPORT) && (LZ4_DLL_EXPORT==1) -# define LZ4FLIB_API __declspec(dllexport) +# define LZ4FLIB_API __declspec(dllexport) LZ4FLIB_VISIBILITY #elif defined(LZ4_DLL_IMPORT) && (LZ4_DLL_IMPORT==1) -# define LZ4FLIB_API __declspec(dllimport) -#elif defined(__GNUC__) && (__GNUC__ >= 4) -# define LZ4FLIB_API __attribute__ ((__visibility__ ("default"))) +# define LZ4FLIB_API __declspec(dllimport) LZ4FLIB_VISIBILITY #else -# define LZ4FLIB_API +# define LZ4FLIB_API LZ4FLIB_VISIBILITY #endif #ifdef LZ4F_DISABLE_DEPRECATE_WARNINGS -- cgit v1.2.3 From 12001d6c1ac08e188587d67d888aa008b2e1ceba Mon Sep 17 00:00:00 2001 From: Andrew Deason Date: Wed, 6 May 2020 14:36:56 -0500 Subject: Avoid old-style function definitions Define 0-argument functions like foo(void) instead of foo(), in order to avoid a warning with -Wold-style-definition. This makes it easier to embed lz4.c in projects that compile with -Werror -Wold-style-definition. --- lib/lz4.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 9808d70..8900c40 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -605,7 +605,7 @@ typedef enum { noDictIssue = 0, dictSmall } dictIssue_directive; int LZ4_versionNumber (void) { return LZ4_VERSION_NUMBER; } const char* LZ4_versionString(void) { return LZ4_VERSION_STRING; } int LZ4_compressBound(int isize) { return LZ4_COMPRESSBOUND(isize); } -int LZ4_sizeofState() { return LZ4_STREAMSIZE; } +int LZ4_sizeofState(void) { return LZ4_STREAMSIZE; } /*-************************************ @@ -2374,7 +2374,7 @@ int LZ4_uncompress_unknownOutputSize (const char* source, char* dest, int isize, /* Obsolete Streaming functions */ -int LZ4_sizeofStreamState() { return LZ4_STREAMSIZE; } +int LZ4_sizeofStreamState(void) { return LZ4_STREAMSIZE; } int LZ4_resetStreamState(void* state, char* inputBuffer) { -- cgit v1.2.3 From 7a75b045bde89e77e3573f91821d0314251aac1d Mon Sep 17 00:00:00 2001 From: Aleksandr Kukuev Date: Mon, 11 May 2020 23:32:02 +0300 Subject: Reducing stack usage in _t_alignment checks --- lib/lz4.c | 4 ++-- lib/lz4hc.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 82ab490..fef9845 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1349,8 +1349,8 @@ LZ4_stream_t* LZ4_createStream(void) while actually aligning LZ4_stream_t on 4 bytes. */ static size_t LZ4_stream_t_alignment(void) { - struct { char c; LZ4_stream_t t; } t_a; - return sizeof(t_a) - sizeof(t_a.t); + typedef struct { char c; LZ4_stream_t t; } t_a; + return sizeof(t_a) - sizeof(LZ4_stream_t); } #endif diff --git a/lib/lz4hc.c b/lib/lz4hc.c index b75514f..687f87e 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -886,8 +886,8 @@ int LZ4_sizeofStateHC(void) { return (int)sizeof(LZ4_streamHC_t); } * while actually aligning LZ4_streamHC_t on 4 bytes. */ static size_t LZ4_streamHC_t_alignment(void) { - struct { char c; LZ4_streamHC_t t; } t_a; - return sizeof(t_a) - sizeof(t_a.t); + typedef struct { char c; LZ4_streamHC_t t; } t_a; + return sizeof(t_a) - sizeof(LZ4_streamHC_t); } #endif -- cgit v1.2.3 From 57c35511d4a0053576468a23837de43fc293d923 Mon Sep 17 00:00:00 2001 From: Christopher Harvie Date: Wed, 13 May 2020 21:48:12 -0400 Subject: fix LZ4F_compressBound when automatic flushing is enabled --- lib/lz4frame.c | 3 +++ lib/lz4frame.h | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/lz4frame.c b/lib/lz4frame.c index c9f630d..5d716ea 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -725,6 +725,9 @@ size_t LZ4F_compressBegin(LZ4F_cctx* cctxPtr, */ size_t LZ4F_compressBound(size_t srcSize, const LZ4F_preferences_t* preferencesPtr) { + if (preferencesPtr && preferencesPtr->autoFlush) { + return LZ4F_compressBound_internal(srcSize, preferencesPtr, 0); + } return LZ4F_compressBound_internal(srcSize, preferencesPtr, (size_t)-1); } diff --git a/lib/lz4frame.h b/lib/lz4frame.h index 391e484..87b0b2a 100644 --- a/lib/lz4frame.h +++ b/lib/lz4frame.h @@ -284,7 +284,7 @@ LZ4FLIB_API size_t LZ4F_compressBegin(LZ4F_cctx* cctx, * @return is always the same for a srcSize and prefsPtr. * prefsPtr is optional : when NULL is provided, preferences will be set to cover worst case scenario. * tech details : - * @return includes the possibility that internal buffer might already be filled by up to (blockSize-1) bytes. + * @return if automatic flushing is not enabled, includes the possibility that internal buffer might already be filled by up to (blockSize-1) bytes. * It also includes frame footer (ending + checksum), since it might be generated by LZ4F_compressEnd(). * @return doesn't include frame header, as it was already generated by LZ4F_compressBegin(). */ -- cgit v1.2.3 From 2a7203c05d58e3094df7c173226f5127e15970ad Mon Sep 17 00:00:00 2001 From: Christopher Harvie Date: Wed, 13 May 2020 23:01:43 -0400 Subject: add tests --- tests/frametest.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/frametest.c b/tests/frametest.c index 1b932e4..f891530 100644 --- a/tests/frametest.c +++ b/tests/frametest.c @@ -200,6 +200,24 @@ int basicTests(U32 seed, double compressibility) DISPLAYLEVEL(3, " %u \n", (U32)cBound); } + /* LZ4F_compressBound() : special case : automatic flushing enabled */ + DISPLAYLEVEL(3, "LZ4F_compressBound(1 KB, autoFlush=1) = "); + { size_t cBound; + LZ4F_preferences_t autoFlushPrefs; + memset(&autoFlushPrefs, 0, sizeof(autoFlushPrefs)); + autoFlushPrefs.autoFlush = 1; + cBound = LZ4F_compressBound(1 KB, &autoFlushPrefs); + if (cBound > 64 KB) goto _output_error; + DISPLAYLEVEL(3, " %u \n", (U32)cBound); + } + + /* LZ4F_compressBound() : special case : automatic flushing disabled */ + DISPLAYLEVEL(3, "LZ4F_compressBound(1 KB, autoFlush=0) = "); + { size_t const cBound = LZ4F_compressBound(1 KB, &prefs); + if (cBound < 64 KB) goto _output_error; + DISPLAYLEVEL(3, " %u \n", (U32)cBound); + } + /* Special case : null-content frame */ testSize = 0; DISPLAYLEVEL(3, "LZ4F_compressFrame, compress null content : "); -- cgit v1.2.3 From 7c578280967bef3ee7991aaefc1eabd4a0908678 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ketelaars?= Date: Sat, 30 May 2020 07:36:22 +0200 Subject: Fix tests on OpenBSD Add condition for OpenBSD, which uses gmake --- tests/test_install.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_install.sh b/tests/test_install.sh index ba87934..122bac5 100755 --- a/tests/test_install.sh +++ b/tests/test_install.sh @@ -6,7 +6,7 @@ make="make -C $lz4_root" unamestr=$(uname) if [ "$unamestr" = 'Linux' ]; then make="make -C $lz4_root" -elif [ "$unamestr" = 'FreeBSD' ]; then +elif [ "$unamestr" = 'FreeBSD' -o "$unamestr" = 'OpenBSD' ]; then make="gmake -C $lz4_root" fi -- cgit v1.2.3 From a05312d924052aa25721aab11fc557d77e817025 Mon Sep 17 00:00:00 2001 From: Bimba Shrestha Date: Fri, 5 Jun 2020 11:57:44 -0500 Subject: bsd upsate to 12-1 --- .cirrus.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.cirrus.yml b/.cirrus.yml index cb4b843..0c0e7a7 100644 --- a/.cirrus.yml +++ b/.cirrus.yml @@ -1,5 +1,5 @@ freebsd_instance: - image_family: freebsd-12-0 + image_family: freebsd-12-1 task: script: pkg install -y gmake && gmake test -- cgit v1.2.3 From 49b3ad4bd41311e92fc3a18eda571800bde5aa42 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sat, 13 Jun 2020 14:31:15 -0700 Subject: Update issue templates --- .github/ISSUE_TEMPLATE/bug_report.md | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md index 96d9476..86b7696 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -10,20 +10,18 @@ assignees: '' **Describe the bug** A clear and concise description of what the bug is. +**Expected behavior** +Please describe what you expected to happen. + **To Reproduce** Steps to reproduce the behavior: 1. Go to '...' 2. Click on '....' 3. Scroll down to '....' -4. See error - -**Expected behavior** -A clear and concise description of what you expected to happen. - -**Screenshots** +4. See error '...' If applicable, add screenshots to help explain your problem. -**Desktop (please complete the following information):** +**System (please complete the following information):** - OS: [e.g. Mac] - Version [e.g. 22] - Compiler [e.g. gcc] -- cgit v1.2.3 From e68c7d38780ada518e6c43a09a2d92421ea8111b Mon Sep 17 00:00:00 2001 From: Alexander Gallego Date: Wed, 8 Jul 2020 08:21:45 -0700 Subject: avoid computing 0 offsets from null pointers Similar work in the kernel: https://patchwork.kernel.org/patch/11351499/ UBsan (+clang-10) complains about doing pointer arithmetic (adding 0) to a nullpointer. This patch is tested with clang-10+ubsan --- lib/lz4.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 82ab490..2f7880a 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -819,7 +819,7 @@ LZ4_FORCE_INLINE int LZ4_compress_generic( int const maybe_extMem = (dictDirective == usingExtDict) || (dictDirective == usingDictCtx); U32 const prefixIdxLimit = startIndex - dictSize; /* used when dictDirective == dictSmall */ - const BYTE* const dictEnd = dictionary + dictSize; + const BYTE* const dictEnd = dictionary ? dictionary + dictSize : dictionary; const BYTE* anchor = (const BYTE*) source; const BYTE* const iend = ip + inputSize; const BYTE* const mflimitPlusOne = iend - MFLIMIT + 1; @@ -827,7 +827,7 @@ LZ4_FORCE_INLINE int LZ4_compress_generic( /* the dictCtx currentOffset is indexed on the start of the dictionary, * while a dictionary in the current context precedes the currentOffset */ - const BYTE* dictBase = (dictDirective == usingDictCtx) ? + const BYTE* dictBase = !dictionary ? NULL : (dictDirective == usingDictCtx) ? dictionary + dictSize - dictCtx->currentOffset : dictionary + dictSize - startIndex; -- cgit v1.2.3 From 4ebc792cab645d4fef2dbad3611a67daf4d10150 Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Tue, 21 Jul 2020 14:04:17 -0700 Subject: meson: remove build requirement for distutils Tested on a default installation of Debian 10. Same as https://github.com/facebook/zstd/pull/2197 Signed-off-by: Rosen Penev --- contrib/meson/meson/meson.build | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/contrib/meson/meson/meson.build b/contrib/meson/meson/meson.build index 387e7bd..b278b7c 100644 --- a/contrib/meson/meson/meson.build +++ b/contrib/meson/meson/meson.build @@ -9,7 +9,6 @@ cc = meson.get_compiler('c') pkgconfig = import('pkgconfig') -python3 = import('python').find_installation() c_std = get_option('c_std') default_library = get_option('default_library') @@ -28,8 +27,8 @@ compiler_msvc = 'msvc' lz4_version = meson.project_version() lz4_h_file = join_paths(meson.current_source_dir(), '../../../lib/lz4.h') -GetLz4LibraryVersion_py = files('GetLz4LibraryVersion.py') -r = run_command(python3, GetLz4LibraryVersion_py, lz4_h_file) +GetLz4LibraryVersion_py = find_program('GetLz4LibraryVersion.py', native : true) +r = run_command(GetLz4LibraryVersion_py, lz4_h_file) if r.returncode() == 0 lz4_version = r.stdout().strip() message('Project version is now: @0@'.format(lz4_version)) -- cgit v1.2.3 From e416cc9baa8f228108175b3551726c3f04aec3ee Mon Sep 17 00:00:00 2001 From: Nate <37554478+servusDei2018@users.noreply.github.com> Date: Thu, 23 Jul 2020 20:13:14 -0400 Subject: Optimized by replacing `endl` with `'\n'` --- contrib/gen_manual/gen_manual.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/contrib/gen_manual/gen_manual.cpp b/contrib/gen_manual/gen_manual.cpp index bedef94..d5fe702 100644 --- a/contrib/gen_manual/gen_manual.cpp +++ b/contrib/gen_manual/gen_manual.cpp @@ -96,10 +96,9 @@ void print_line(stringstream &sout, string line) epos = line.find("*/"); if (spos!=string::npos && epos!=string::npos) { sout << line.substr(0, spos); - sout << "" << line.substr(spos) << "" << endl; + sout << "" << line.substr(spos) << "" << '\n'; } else { - // fprintf(stderr, "lines=%s\n", line.c_str()); - sout << line << endl; + sout << line << '\n'; } } -- cgit v1.2.3 From fe2a1b3707d59db8bb5ad599cbed6507ffbd544e Mon Sep 17 00:00:00 2001 From: Nick Terrell Date: Mon, 3 Aug 2020 11:18:35 -0700 Subject: Call LZ4_memcpy() instead of memcpy() `LZ4_memcpy()` uses `__builtin_memcpy()` to ensure that clang/gcc can inline the `memcpy()` calls in freestanding mode. This is necessary for decompressing the Linux Kernel with LZ4. Without an analogous patch decompression ran at 77 MB/s, and with the patch it ran at 884 MB/s. --- lib/lz4.c | 87 +++++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 51 insertions(+), 36 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 59c6d4b..ae50d54 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -277,6 +277,21 @@ typedef enum { /*-************************************ * Reading and writing into memory **************************************/ + +/** + * LZ4 relies on memcpy with a constant size being inlined. In freestanding + * environments, the compiler can't assume the implementation of memcpy() is + * standard compliant, so it can't apply its specialized memcpy() inlining + * logic. When possible, use __builtin_memcpy() to tell the compiler to analyze + * memcpy() as if it were standard compliant, so it can inline it in freestanding + * environments. This is needed when decompressing the Linux Kernel, for example. + */ +#if defined(__GNUC__) && (__GNUC__ >= 4) +#define LZ4_memcpy(dst, src, size) __builtin_memcpy(dst, src, size) +#else +#define LZ4_memcpy(dst, src, size) memcpy(dst, src, size) +#endif + static unsigned LZ4_isLittleEndian(void) { const union { U32 u; BYTE c[4]; } one = { 1 }; /* don't use static : performance detrimental */ @@ -311,27 +326,27 @@ static void LZ4_write32(void* memPtr, U32 value) { ((unalign*)memPtr)->u32 = val static U16 LZ4_read16(const void* memPtr) { - U16 val; memcpy(&val, memPtr, sizeof(val)); return val; + U16 val; LZ4_memcpy(&val, memPtr, sizeof(val)); return val; } static U32 LZ4_read32(const void* memPtr) { - U32 val; memcpy(&val, memPtr, sizeof(val)); return val; + U32 val; LZ4_memcpy(&val, memPtr, sizeof(val)); return val; } static reg_t LZ4_read_ARCH(const void* memPtr) { - reg_t val; memcpy(&val, memPtr, sizeof(val)); return val; + reg_t val; LZ4_memcpy(&val, memPtr, sizeof(val)); return val; } static void LZ4_write16(void* memPtr, U16 value) { - memcpy(memPtr, &value, sizeof(value)); + LZ4_memcpy(memPtr, &value, sizeof(value)); } static void LZ4_write32(void* memPtr, U32 value) { - memcpy(memPtr, &value, sizeof(value)); + LZ4_memcpy(memPtr, &value, sizeof(value)); } #endif /* LZ4_FORCE_MEMORY_ACCESS */ @@ -366,7 +381,7 @@ void LZ4_wildCopy8(void* dstPtr, const void* srcPtr, void* dstEnd) const BYTE* s = (const BYTE*)srcPtr; BYTE* const e = (BYTE*)dstEnd; - do { memcpy(d,s,8); d+=8; s+=8; } while (ddictCtx, sizeof(LZ4_stream_t)); + LZ4_memcpy(streamPtr, streamPtr->dictCtx, sizeof(LZ4_stream_t)); result = LZ4_compress_generic(streamPtr, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, usingExtDict, noDictIssue, acceleration); } else { result = LZ4_compress_generic(streamPtr, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, usingDictCtx, noDictIssue, acceleration); @@ -1750,12 +1765,12 @@ LZ4_decompress_generic( /* We don't need to check oend, since we check it once for each loop below */ if (ip > iend-(16 + 1/*max lit + offset + nextToken*/)) { goto safe_literal_copy; } /* Literals can only be 14, but hope compilers optimize if we copy by a register size */ - memcpy(op, ip, 16); + LZ4_memcpy(op, ip, 16); } else { /* LZ4_decompress_fast() */ /* LZ4_decompress_fast() cannot copy more than 8 bytes at a time : * it doesn't know input length, and relies on end-of-block properties */ - memcpy(op, ip, 8); - if (length > 8) { memcpy(op+8, ip+8, 8); } + LZ4_memcpy(op, ip, 8); + if (length > 8) { LZ4_memcpy(op+8, ip+8, 8); } } ip += length; op = cpy; } @@ -1791,9 +1806,9 @@ LZ4_decompress_generic( assert(match <= op); assert(op + 18 <= oend); - memcpy(op, match, 8); - memcpy(op+8, match+8, 8); - memcpy(op+16, match+16, 2); + LZ4_memcpy(op, match, 8); + LZ4_memcpy(op+8, match+8, 8); + LZ4_memcpy(op+16, match+16, 2); op += length; continue; } } } @@ -1816,14 +1831,14 @@ LZ4_decompress_generic( /* match stretches into both external dictionary and current block */ size_t const copySize = (size_t)(lowPrefix - match); size_t const restSize = length - copySize; - memcpy(op, dictEnd - copySize, copySize); + LZ4_memcpy(op, dictEnd - copySize, copySize); op += copySize; if (restSize > (size_t)(op - lowPrefix)) { /* overlap copy */ BYTE* const endOfMatch = op + restSize; const BYTE* copyFrom = lowPrefix; while (op < endOfMatch) { *op++ = *copyFrom++; } } else { - memcpy(op, lowPrefix, restSize); + LZ4_memcpy(op, lowPrefix, restSize); op += restSize; } } continue; @@ -1864,7 +1879,7 @@ LZ4_decompress_generic( /* strictly "less than" on input, to re-enter the loop with at least one byte */ && likely((endOnInput ? ip < shortiend : 1) & (op <= shortoend)) ) { /* Copy the literals */ - memcpy(op, ip, endOnInput ? 16 : 8); + LZ4_memcpy(op, ip, endOnInput ? 16 : 8); op += length; ip += length; /* The second stage: prepare for match copying, decode full info. @@ -1879,9 +1894,9 @@ LZ4_decompress_generic( && (offset >= 8) && (dict==withPrefix64k || match >= lowPrefix) ) { /* Copy the match. */ - memcpy(op + 0, match + 0, 8); - memcpy(op + 8, match + 8, 8); - memcpy(op +16, match +16, 2); + LZ4_memcpy(op + 0, match + 0, 8); + LZ4_memcpy(op + 8, match + 8, 8); + LZ4_memcpy(op +16, match +16, 2); op += length + MINMATCH; /* Both stages worked, load the next token. */ continue; @@ -1994,14 +2009,14 @@ LZ4_decompress_generic( /* match stretches into both external dictionary and current block */ size_t const copySize = (size_t)(lowPrefix - match); size_t const restSize = length - copySize; - memcpy(op, dictEnd - copySize, copySize); + LZ4_memcpy(op, dictEnd - copySize, copySize); op += copySize; if (restSize > (size_t)(op - lowPrefix)) { /* overlap copy */ BYTE* const endOfMatch = op + restSize; const BYTE* copyFrom = lowPrefix; while (op < endOfMatch) *op++ = *copyFrom++; } else { - memcpy(op, lowPrefix, restSize); + LZ4_memcpy(op, lowPrefix, restSize); op += restSize; } } continue; @@ -2020,7 +2035,7 @@ LZ4_decompress_generic( if (matchEnd > op) { /* overlap copy */ while (op < copyEnd) { *op++ = *match++; } } else { - memcpy(op, match, mlen); + LZ4_memcpy(op, match, mlen); } op = copyEnd; if (op == oend) { break; } @@ -2034,10 +2049,10 @@ LZ4_decompress_generic( op[2] = match[2]; op[3] = match[3]; match += inc32table[offset]; - memcpy(op+4, match, 4); + LZ4_memcpy(op+4, match, 4); match -= dec64table[offset]; } else { - memcpy(op, match, 8); + LZ4_memcpy(op, match, 8); match += 8; } op += 8; @@ -2052,7 +2067,7 @@ LZ4_decompress_generic( } while (op < cpy) { *op++ = *match++; } } else { - memcpy(op, match, 8); + LZ4_memcpy(op, match, 8); if (length > 16) { LZ4_wildCopy8(op+8, match+8, cpy); } } op = cpy; /* wildcopy correction */ -- cgit v1.2.3 From d7399232a4d547d7183c193997e17d534d000f52 Mon Sep 17 00:00:00 2001 From: "W. Felix Handte" Date: Wed, 5 Aug 2020 12:46:32 -0400 Subject: Remove Extraneous Reset in LZ4_attach_dictionary() Nothing internally sets dirty anymore. The only way to get that is if you use an uninitialized context, in which case your warranty is void anyways. --- lib/lz4.c | 6 ------ 1 file changed, 6 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index ae50d54..46c8e11 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1455,12 +1455,6 @@ void LZ4_attach_dictionary(LZ4_stream_t* workingStream, const LZ4_stream_t* dict workingStream, dictionaryStream, dictCtx != NULL ? dictCtx->dictSize : 0); - /* Calling LZ4_resetStream_fast() here makes sure that changes will not be - * erased by subsequent calls to LZ4_resetStream_fast() in case stream was - * marked as having dirty context, e.g. requiring full reset. - */ - LZ4_resetStream_fast(workingStream); - if (dictCtx != NULL) { /* If the current offset is zero, we will never look in the * external dictionary context, since there is no value a table -- cgit v1.2.3 From ae1372690d3925d893cac44ab6e9a2d6aaff819f Mon Sep 17 00:00:00 2001 From: Indragie Karunaratne Date: Wed, 5 Aug 2020 15:34:07 -0600 Subject: LZ4LIB_VISIBILITY -> LZ4FLIB_VISIBILITY in comment --- lib/lz4frame.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/lz4frame.h b/lib/lz4frame.h index b6ac587..eba9c9e 100644 --- a/lib/lz4frame.h +++ b/lib/lz4frame.h @@ -66,7 +66,7 @@ extern "C" { *****************************************************************/ /* LZ4_DLL_EXPORT : * Enable exporting of functions when building a Windows DLL - * LZ4LIB_VISIBILITY : + * LZ4FLIB_VISIBILITY : * Control library symbols visibility. */ #ifndef LZ4FLIB_VISIBILITY -- cgit v1.2.3 From 9af86f084101b08e87e7c1509e19aa05d27271ed Mon Sep 17 00:00:00 2001 From: "W. Felix Handte" Date: Thu, 6 Aug 2020 16:06:40 -0400 Subject: Remove dirty Field From LZ4_stream_t --- lib/lz4.c | 10 ---------- lib/lz4.h | 6 ++---- tests/fuzzer.c | 4 ---- 3 files changed, 2 insertions(+), 18 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 46c8e11..148827e 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -758,15 +758,6 @@ LZ4_FORCE_INLINE void LZ4_prepareTable(LZ4_stream_t_internal* const cctx, const int inputSize, const tableType_t tableType) { - /* If compression failed during the previous step, then the context - * is marked as dirty, therefore, it has to be fully reset. - */ - if (cctx->dirty) { - DEBUGLOG(5, "LZ4_prepareTable: Full reset for %p", cctx); - MEM_INIT(cctx, 0, sizeof(LZ4_stream_t_internal)); - return; - } - /* If the table hasn't been used, it's guaranteed to be zeroed out, and is * therefore safe to use no matter what mode we're in. Otherwise, we figure * out if it's safe to leave as is or whether it needs to be reset. @@ -1506,7 +1497,6 @@ int LZ4_compress_fast_continue (LZ4_stream_t* LZ4_stream, DEBUGLOG(5, "LZ4_compress_fast_continue (inputSize=%i)", inputSize); - if (streamPtr->dirty) { return 0; } /* Uninitialized structure detected */ LZ4_renormDictT(streamPtr, inputSize); /* avoid index overflow */ if (acceleration < 1) acceleration = ACCELERATION_DEFAULT; diff --git a/lib/lz4.h b/lib/lz4.h index 32108e2..9b3d758 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -564,8 +564,7 @@ typedef struct LZ4_stream_t_internal LZ4_stream_t_internal; struct LZ4_stream_t_internal { uint32_t hashTable[LZ4_HASH_SIZE_U32]; uint32_t currentOffset; - uint16_t dirty; - uint16_t tableType; + uint32_t tableType; const uint8_t* dictionary; const LZ4_stream_t_internal* dictCtx; uint32_t dictSize; @@ -584,8 +583,7 @@ typedef struct LZ4_stream_t_internal LZ4_stream_t_internal; struct LZ4_stream_t_internal { unsigned int hashTable[LZ4_HASH_SIZE_U32]; unsigned int currentOffset; - unsigned short dirty; - unsigned short tableType; + unsigned int tableType; const unsigned char* dictionary; const LZ4_stream_t_internal* dictCtx; unsigned int dictSize; diff --git a/tests/fuzzer.c b/tests/fuzzer.c index 8a095c4..3d7456a 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -787,7 +787,6 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c LZ4_attach_dictionary(&LZ4_stream, &LZ4dictBody); blockContinueCompressedSize = LZ4_compress_fast_continue(&LZ4_stream, block, compressedBuffer, blockSize, (int)compressedBufferSize, 1); FUZ_CHECKTEST(blockContinueCompressedSize==0, "LZ4_compress_fast_continue using extDictCtx failed"); - FUZ_CHECKTEST(LZ4_stream.internal_donotuse.dirty, "context should be good"); /* In the future, it might be desirable to let extDictCtx mode's * output diverge from the output generated by regular extDict mode. @@ -812,7 +811,6 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c FUZ_CHECKTEST(ret<=0, "LZ4_compress_fast_continue using extDictCtx should work : enough size available within output buffer"); FUZ_CHECKTEST(ret != expectedSize, "LZ4_compress_fast_continue using extDictCtx produced different-sized output"); FUZ_CHECKTEST(XXH32(compressedBuffer, (size_t)ret, 0) != expectedCrc, "LZ4_compress_fast_continue using extDictCtx produced different output"); - FUZ_CHECKTEST(LZ4_stream.internal_donotuse.dirty, "context should be good"); FUZ_DISPLAYTEST(); LZ4_resetStream_fast(&LZ4_stream); @@ -822,7 +820,6 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c FUZ_CHECKTEST(ret<=0, "LZ4_compress_fast_continue using extDictCtx with re-used context should work : enough size available within output buffer"); FUZ_CHECKTEST(ret != expectedSize, "LZ4_compress_fast_continue using extDictCtx produced different-sized output"); FUZ_CHECKTEST(XXH32(compressedBuffer, (size_t)ret, 0) != expectedCrc, "LZ4_compress_fast_continue using extDictCtx produced different output"); - FUZ_CHECKTEST(LZ4_stream.internal_donotuse.dirty, "context should be good"); } /* Decompress with dictionary as external */ @@ -1115,7 +1112,6 @@ static void FUZ_unitTests(int compressionLevel) LZ4_initStream(&streamingState, sizeof(streamingState)); result = LZ4_compress_fast_continue(&streamingState, testInput, testCompressed, testCompressedSize, testCompressedSize-1, 1); FUZ_CHECKTEST(result==0, "LZ4_compress_fast_continue() compression failed!"); - FUZ_CHECKTEST(streamingState.internal_donotuse.dirty, "context should be clean") result = LZ4_decompress_safe(testCompressed, testVerify, result, testCompressedSize); FUZ_CHECKTEST(result!=(int)testCompressedSize, "LZ4_decompress_safe() decompression failed"); -- cgit v1.2.3 From a78235e6ad87049f2d1c5f98ecc0ccd16ca71f95 Mon Sep 17 00:00:00 2001 From: "W. Felix Handte" Date: Mon, 10 Aug 2020 13:46:31 -0400 Subject: Fix Enum Casts Fixes `-Wsign-compare` issues. --- lib/lz4.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 148827e..6322434 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -762,9 +762,9 @@ LZ4_prepareTable(LZ4_stream_t_internal* const cctx, * therefore safe to use no matter what mode we're in. Otherwise, we figure * out if it's safe to leave as is or whether it needs to be reset. */ - if (cctx->tableType != clearedTable) { + if ((tableType_t)cctx->tableType != clearedTable) { assert(inputSize >= 0); - if (cctx->tableType != tableType + if ((tableType_t)cctx->tableType != tableType || ((tableType == byU16) && cctx->currentOffset + (unsigned)inputSize >= 0xFFFFU) || ((tableType == byU32) && cctx->currentOffset > 1 GB) || tableType == byPtr @@ -773,7 +773,7 @@ LZ4_prepareTable(LZ4_stream_t_internal* const cctx, DEBUGLOG(4, "LZ4_prepareTable: Resetting table in %p", cctx); MEM_INIT(cctx->hashTable, 0, LZ4_HASHTABLESIZE); cctx->currentOffset = 0; - cctx->tableType = clearedTable; + cctx->tableType = (U32)clearedTable; } else { DEBUGLOG(4, "LZ4_prepareTable: Re-use hash table (no reset)"); } @@ -864,7 +864,7 @@ LZ4_FORCE_INLINE int LZ4_compress_generic( cctx->dictSize += (U32)inputSize; } cctx->currentOffset += (U32)inputSize; - cctx->tableType = (U16)tableType; + cctx->tableType = (U32)tableType; if (inputSizecurrentOffset; dict->dictionary = p; dict->dictSize = (U32)(dictEnd - p); - dict->tableType = tableType; + dict->tableType = (U32)tableType; while (p <= dictEnd-HASH_UNIT) { LZ4_putPosition(p, dict->hashTable, tableType, base); -- cgit v1.2.3 From b01c58000e39ee7208032317fb1f0f96832e9e06 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 10 Aug 2020 10:54:35 -0700 Subject: add test that breaks --fast with huge values, as expected from #876 Also : added target `check` --- tests/Makefile | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/Makefile b/tests/Makefile index 1f8321c..05cae15 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -152,6 +152,9 @@ DD:=dd list: @$(MAKE) -pRrq -f $(lastword $(MAKEFILE_LIST)) : 2>/dev/null | awk -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' | sort | egrep -v -e '^[^[:alnum:]]' -e '^$@$$' | xargs +.PHONY: check +check: test-lz4-essentials + .PHONY: test test: test-lz4 test-lz4c test-frametest test-fullbench test-fuzzer test-install test-amalgamation listTest @@ -349,6 +352,8 @@ test-lz4-basic: lz4 datagen unlz4 lz4cat test "$(shell ./datagen -g20KB | $(LZ4) -c --fast=1 | wc -c)" -eq "$(shell ./datagen -g20KB| $(LZ4) -c --fast| wc -c)" # checks default fast compression is -1 ! $(LZ4) -c --fast=0 tmp-tlb-dg20K # lz4 should fail when fast=0 ! $(LZ4) -c --fast=-1 tmp-tlb-dg20K # lz4 should fail when fast=-1 + # High --fast values can result in out-of-bound dereferences #876 + ./datagen -g1M | $(LZ4) -c --fast=999999999 > /dev/null # Test for #596 @echo "TEST" > tmp-tlb-test $(LZ4) -m tmp-tlb-test -- cgit v1.2.3 From 7b1b078dfcedd169332b0583d34daa65b2c585db Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 10 Aug 2020 11:03:27 -0700 Subject: fix #876 by introducing a max limit acceleration value --- lib/lz4.c | 19 ++++++++++++++----- lib/lz4.h | 3 ++- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index ae50d54..239a3f4 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -45,10 +45,16 @@ #endif /* - * ACCELERATION_DEFAULT : + * LZ4_ACCELERATION_DEFAULT : * Select "acceleration" for LZ4_compress_fast() when parameter value <= 0 */ -#define ACCELERATION_DEFAULT 1 +#define LZ4_ACCELERATION_DEFAULT 1 +/* + * LZ4_ACCELERATION_MAX : + * Any "acceleration" value higher than this threshold + * get treated as LZ4_ACCELERATION_MAX instead (fix #876) + */ +#define LZ4_ACCELERATION_MAX 65537 /*-************************************ @@ -1200,7 +1206,8 @@ int LZ4_compress_fast_extState(void* state, const char* source, char* dest, int { LZ4_stream_t_internal* const ctx = & LZ4_initStream(state, sizeof(LZ4_stream_t)) -> internal_donotuse; assert(ctx != NULL); - if (acceleration < 1) acceleration = ACCELERATION_DEFAULT; + if (acceleration < 1) acceleration = LZ4_ACCELERATION_DEFAULT; + if (acceleration > LZ4_ACCELERATION_MAX) acceleration = LZ4_ACCELERATION_MAX; if (maxOutputSize >= LZ4_compressBound(inputSize)) { if (inputSize < LZ4_64Klimit) { return LZ4_compress_generic(ctx, source, dest, inputSize, NULL, 0, notLimited, byU16, noDict, noDictIssue, acceleration); @@ -1230,7 +1237,8 @@ int LZ4_compress_fast_extState(void* state, const char* source, char* dest, int int LZ4_compress_fast_extState_fastReset(void* state, const char* src, char* dst, int srcSize, int dstCapacity, int acceleration) { LZ4_stream_t_internal* ctx = &((LZ4_stream_t*)state)->internal_donotuse; - if (acceleration < 1) acceleration = ACCELERATION_DEFAULT; + if (acceleration < 1) acceleration = LZ4_ACCELERATION_DEFAULT; + if (acceleration > LZ4_ACCELERATION_MAX) acceleration = LZ4_ACCELERATION_MAX; if (dstCapacity >= LZ4_compressBound(srcSize)) { if (srcSize < LZ4_64Klimit) { @@ -1514,7 +1522,8 @@ int LZ4_compress_fast_continue (LZ4_stream_t* LZ4_stream, if (streamPtr->dirty) { return 0; } /* Uninitialized structure detected */ LZ4_renormDictT(streamPtr, inputSize); /* avoid index overflow */ - if (acceleration < 1) acceleration = ACCELERATION_DEFAULT; + if (acceleration < 1) acceleration = LZ4_ACCELERATION_DEFAULT; + if (acceleration > LZ4_ACCELERATION_MAX) acceleration = LZ4_ACCELERATION_MAX; /* invalidate tiny dictionaries */ if ( (streamPtr->dictSize-1 < 4-1) /* intentional underflow */ diff --git a/lib/lz4.h b/lib/lz4.h index 32108e2..54ec3fb 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -186,7 +186,8 @@ LZ4LIB_API int LZ4_compressBound(int inputSize); The larger the acceleration value, the faster the algorithm, but also the lesser the compression. It's a trade-off. It can be fine tuned, with each successive value providing roughly +~3% to speed. An acceleration value of "1" is the same as regular LZ4_compress_default() - Values <= 0 will be replaced by ACCELERATION_DEFAULT (currently == 1, see lz4.c). + Values <= 0 will be replaced by LZ4_ACCELERATION_DEFAULT (currently == 1, see lz4.c). + Values > LZ4_ACCELERATION_MAX will be replaced by LZ4_ACCELERATION_MAX (currently == 65537, see lz4.c). */ LZ4LIB_API int LZ4_compress_fast (const char* src, char* dst, int srcSize, int dstCapacity, int acceleration); -- cgit v1.2.3 From a29ade17a0157350de851fcd72e4291d2142691c Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 10 Aug 2020 11:51:57 -0700 Subject: fix #832 does no longer rely on default 0-interpretation when __GNUC__ is not defined --- lib/lz4.h | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/lz4.h b/lib/lz4.h index 9b3d758..b8a898e 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -665,18 +665,17 @@ union LZ4_streamDecode_u { #ifdef LZ4_DISABLE_DEPRECATE_WARNINGS # define LZ4_DEPRECATED(message) /* disable deprecation warnings */ #else -# define LZ4_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) # if defined (__cplusplus) && (__cplusplus >= 201402) /* C++14 or greater */ # define LZ4_DEPRECATED(message) [[deprecated(message)]] -# elif (LZ4_GCC_VERSION >= 405) || defined(__clang__) -# define LZ4_DEPRECATED(message) __attribute__((deprecated(message))) -# elif (LZ4_GCC_VERSION >= 301) -# define LZ4_DEPRECATED(message) __attribute__((deprecated)) # elif defined(_MSC_VER) # define LZ4_DEPRECATED(message) __declspec(deprecated(message)) +# elif defined(__clang__) || (defined(__GNUC__) && (__GNUC__ >= 4) && (__GNUC_MINOR__ >= 5)) +# define LZ4_DEPRECATED(message) __attribute__((deprecated(message))) +# elif defined(__GNUC__) && (__GNUC__ >= 3) && (__GNUC_MINOR__ >= 1) +# define LZ4_DEPRECATED(message) __attribute__((deprecated)) # else -# pragma message("WARNING: You need to implement LZ4_DEPRECATED for this compiler") -# define LZ4_DEPRECATED(message) +# pragma message("WARNING: LZ4_DEPRECATED needs custom implementation for this compiler") +# define LZ4_DEPRECATED(message) /* disabled */ # endif #endif /* LZ4_DISABLE_DEPRECATE_WARNINGS */ -- cgit v1.2.3 From 28e5a4e0542820e81f8372c1a405310e9757cd7e Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 10 Aug 2020 17:11:49 -0700 Subject: fixed test of gnu c version --- lib/lz4.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/lz4.h b/lib/lz4.h index b8a898e..3d6ab55 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -669,9 +669,9 @@ union LZ4_streamDecode_u { # define LZ4_DEPRECATED(message) [[deprecated(message)]] # elif defined(_MSC_VER) # define LZ4_DEPRECATED(message) __declspec(deprecated(message)) -# elif defined(__clang__) || (defined(__GNUC__) && (__GNUC__ >= 4) && (__GNUC_MINOR__ >= 5)) +# elif defined(__clang__) || (defined(__GNUC__) && (__GNUC__ * 10 + __GNUC_MINOR__ >= 45)) # define LZ4_DEPRECATED(message) __attribute__((deprecated(message))) -# elif defined(__GNUC__) && (__GNUC__ >= 3) && (__GNUC_MINOR__ >= 1) +# elif defined(__GNUC__) && (__GNUC__ * 10 + __GNUC_MINOR__ >= 31) # define LZ4_DEPRECATED(message) __attribute__((deprecated)) # else # pragma message("WARNING: LZ4_DEPRECATED needs custom implementation for this compiler") -- cgit v1.2.3 From 3dd34df75185f132238451ef4fdb68b83f6fb91a Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 11 Aug 2020 14:03:20 -0700 Subject: added target lz4-wlib variant of lz4 linking to liblz4 dynamic library requires the dynamic library to expose static-only symbols (experimental API) Example for #888 --- .travis.yml | 4 +++- lib/README.md | 7 ++++--- lib/lz4frame.h | 6 +++--- ossfuzz/Makefile | 6 +++++- programs/.gitignore | 1 + programs/Makefile | 26 ++++++++++++++++++++------ 6 files changed, 36 insertions(+), 14 deletions(-) diff --git a/.travis.yml b/.travis.yml index bd29630..f2da894 100644 --- a/.travis.yml +++ b/.travis.yml @@ -33,9 +33,11 @@ matrix: script: - CC=clang MOREFLAGS=-fsanitize=address make -C tests test-frametest test-fuzzer - - name: Custom LZ4_DISTANCE_MAX + - name: Custom LZ4_DISTANCE_MAX ; Build lz4-wlib (CLI linked to dynamic library) script: - MOREFLAGS=-DLZ4_DISTANCE_MAX=8000 make check + - make clean + - make -C programs lz4-wlib - name: (Precise) g++ and clang CMake test dist: precise diff --git a/lib/README.md b/lib/README.md index cba2c34..707d777 100644 --- a/lib/README.md +++ b/lib/README.md @@ -35,12 +35,13 @@ So it's necessary to include all `*.c` and `*.h` files present in `/lib`. Definitions which are not guaranteed to remain stable in future versions, are protected behind macros, such as `LZ4_STATIC_LINKING_ONLY`. -As the name implies, these definitions can only be invoked +As the name strongly implies, these definitions should only be invoked in the context of static linking ***only***. Otherwise, dependent application may fail on API or ABI break in the future. -The associated symbols are also not present in dynamic library by default. +The associated symbols are also not exposed by the dynamic library by default. Should they be nonetheless needed, it's possible to force their publication -by using build macro `LZ4_PUBLISH_STATIC_FUNCTIONS`. +by using build macros `LZ4_PUBLISH_STATIC_FUNCTIONS` +and `LZ4F_PUBLISH_STATIC_FUNCTIONS`. #### Build macros diff --git a/lib/lz4frame.h b/lib/lz4frame.h index 77d682b..c669aec 100644 --- a/lib/lz4frame.h +++ b/lib/lz4frame.h @@ -381,7 +381,7 @@ LZ4FLIB_API LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_dctx* dctx); * note : Frame header size is variable, but is guaranteed to be * >= LZ4F_HEADER_SIZE_MIN bytes, and <= LZ4F_HEADER_SIZE_MAX bytes. */ -size_t LZ4F_headerSize(const void* src, size_t srcSize); +LZ4FLIB_API size_t LZ4F_headerSize(const void* src, size_t srcSize); /*! LZ4F_getFrameInfo() : * This function extracts frame parameters (max blockSize, dictID, etc.). @@ -498,9 +498,9 @@ extern "C" { * Use at your own risk. */ #ifdef LZ4F_PUBLISH_STATIC_FUNCTIONS -#define LZ4FLIB_STATIC_API LZ4FLIB_API +# define LZ4FLIB_STATIC_API LZ4FLIB_API #else -#define LZ4FLIB_STATIC_API +# define LZ4FLIB_STATIC_API #endif diff --git a/ossfuzz/Makefile b/ossfuzz/Makefile index 7e043a1..f247405 100644 --- a/ossfuzz/Makefile +++ b/ossfuzz/Makefile @@ -47,6 +47,7 @@ FUZZERS := \ round_trip_frame_fuzzer \ decompress_frame_fuzzer +.PHONY: all all: $(FUZZERS) # Include a rule to build the static library if calling this target @@ -70,5 +71,8 @@ $(LZ4DIR)/liblz4.a: $(RM) $*_fuzzer $*_fuzzer.o standaloneengine.o .PHONY: clean -clean: compress_fuzzer_clean decompress_fuzzer_clean +clean: compress_fuzzer_clean decompress_fuzzer_clean \ + compress_frame_fuzzer_clean compress_hc_fuzzer_clean \ + decompress_frame_fuzzer_clean round_trip_frame_fuzzer_clean \ + round_trip_fuzzer_clean round_trip_hc_fuzzer_clean round_trip_stream_fuzzer_clean $(MAKE) -C $(LZ4DIR) clean diff --git a/programs/.gitignore b/programs/.gitignore index daa7f14..9ffadd9 100644 --- a/programs/.gitignore +++ b/programs/.gitignore @@ -4,6 +4,7 @@ unlz4 lz4cat lz4c lz4c32 +lz4-wlib datagen frametest frametest32 diff --git a/programs/Makefile b/programs/Makefile index 4994551..4256ae8 100644 --- a/programs/Makefile +++ b/programs/Makefile @@ -41,12 +41,13 @@ LIBVER_MINOR := $(shell echo $(LIBVER_MINOR_SCRIPT)) LIBVER_PATCH := $(shell echo $(LIBVER_PATCH_SCRIPT)) LIBVER := $(shell echo $(LIBVER_SCRIPT)) -SRCFILES := $(sort $(wildcard $(LZ4DIR)/*.c) $(wildcard *.c)) -OBJFILES := $(SRCFILES:.c=.o) +LIBFILES = $(wildcard $(LZ4DIR)/*.c) +SRCFILES = $(sort $(LIBFILES) $(wildcard *.c)) +OBJFILES = $(SRCFILES:.c=.o) CPPFLAGS += -I$(LZ4DIR) -DXXH_NAMESPACE=LZ4_ CFLAGS ?= -O3 -DEBUGFLAGS:=-Wall -Wextra -Wundef -Wcast-qual -Wcast-align -Wshadow \ +DEBUGFLAGS= -Wall -Wextra -Wundef -Wcast-qual -Wcast-align -Wshadow \ -Wswitch-enum -Wdeclaration-after-statement -Wstrict-prototypes \ -Wpointer-arith -Wstrict-aliasing=1 CFLAGS += $(DEBUGFLAGS) $(MOREFLAGS) @@ -82,13 +83,25 @@ lz4: $(OBJFILES) lz4-exe.o $(CC) $(FLAGS) $^ -o $@$(EXT) else lz4: $(OBJFILES) - $(CC) $(FLAGS) $^ -o $@$(EXT) + $(CC) $(FLAGS) $(OBJFILES) -o $@$(EXT) $(LDLIBS) endif - +.PHONY: lz4-release lz4-release: DEBUGFLAGS= lz4-release: lz4 +lz4-wlib: LIBFILES = +lz4-wlib: SRCFILES+= $(LZ4DIR)/xxhash.c # benchmark unit needs XXH64() +lz4-wlib: LDFLAGS += -L $(LZ4DIR) +lz4-wlib: LDLIBS = -llz4 +lz4-wlib: liblz4 $(OBJFILES) + @echo WARNING: $@ must link to an extended variant of the dynamic library which also exposes unstable symbols + $(CC) $(FLAGS) $(OBJFILES) -o $@$(EXT) $(LDLIBS) + +.PHONY:liblz4 +liblz4: + CPPFLAGS="-DLZ4F_PUBLISH_STATIC_FUNCTIONS -DLZ4_PUBLISH_STATIC_FUNCTIONS" $(MAKE) -C $(LZ4DIR) liblz4 + lz4c: lz4 $(LN_SF) lz4$(EXT) lz4c$(EXT) @@ -113,7 +126,8 @@ ifeq ($(WINBASED),yes) endif @$(MAKE) -C $(LZ4DIR) $@ > $(VOID) @$(RM) core *.o *.test tmp* \ - lz4$(EXT) lz4c$(EXT) lz4c32$(EXT) unlz4$(EXT) lz4cat$(EXT) + lz4$(EXT) lz4c$(EXT) lz4c32$(EXT) lz4-wlib$(EXT) \ + unlz4$(EXT) lz4cat$(EXT) @echo Cleaning completed -- cgit v1.2.3 From e72897402df0b93978d9471979e6d5c62d278b2a Mon Sep 17 00:00:00 2001 From: aqrit Date: Tue, 11 Aug 2020 21:14:09 -0400 Subject: rejigger bit counting intrinsics Fix lz4/lz4#867 Optimize software fallback routines. Delete some faulty (and dead?) MSVC big endian code. --- lib/lz4.c | 91 ++++++++++++++++++++++++++++++++------------------------------- 1 file changed, 46 insertions(+), 45 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 1cd7322..5d0a2d2 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -487,75 +487,76 @@ LZ4_memcpy_using_offset(BYTE* dstPtr, const BYTE* srcPtr, BYTE* dstEnd, const si **************************************/ static unsigned LZ4_NbCommonBytes (reg_t val) { + assert(val != 0); if (LZ4_isLittleEndian()) { - if (sizeof(val)==8) { -# if defined(_MSC_VER) && defined(_WIN64) && !defined(LZ4_FORCE_SW_BITCOUNT) + if (sizeof(val) == 8) { +# if defined(_MSC_VER) && (_MSC_VER >= 1800) && defined(_M_AMD64) && !defined(LZ4_FORCE_SW_BITCOUNT) + /* x64 CPUS without BMI support interpret `TZCNT` as `REP BSF` */ + return (unsigned)_tzcnt_u64(val) >> 3; +# elif defined(_MSC_VER) && defined(_WIN64) && !defined(LZ4_FORCE_SW_BITCOUNT) unsigned long r = 0; - _BitScanForward64( &r, (U64)val ); - return (int)(r>>3); -# elif (defined(__clang__) || (defined(__GNUC__) && (__GNUC__>=3))) && !defined(LZ4_FORCE_SW_BITCOUNT) + _BitScanForward64(&r, (U64)val); + return (unsigned)r >> 3; +# elif (defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 3) || \ + ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))))) && \ + !defined(LZ4_FORCE_SW_BITCOUNT) return (unsigned)__builtin_ctzll((U64)val) >> 3; # else - static const int DeBruijnBytePos[64] = { 0, 0, 0, 0, 0, 1, 1, 2, - 0, 3, 1, 3, 1, 4, 2, 7, - 0, 2, 3, 6, 1, 5, 3, 5, - 1, 3, 4, 4, 2, 5, 6, 7, - 7, 0, 1, 2, 3, 3, 4, 6, - 2, 6, 5, 5, 3, 4, 5, 6, - 7, 1, 2, 4, 6, 4, 4, 5, - 7, 2, 6, 5, 7, 6, 7, 7 }; - return DeBruijnBytePos[((U64)((val & -(long long)val) * 0x0218A392CDABBD3FULL)) >> 58]; + const U64 m = 0x0101010101010101ULL; + val ^= val - 1; + return (unsigned)(((U64)((val & (m - 1)) * m)) >> 56); # endif } else /* 32 bits */ { # if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT) unsigned long r; - _BitScanForward( &r, (U32)val ); - return (int)(r>>3); -# elif (defined(__clang__) || (defined(__GNUC__) && (__GNUC__>=3))) && !defined(LZ4_FORCE_SW_BITCOUNT) + _BitScanForward(&r, (U32)val); + return (unsigned)r >> 3; +# elif (defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 3) || \ + ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))))) && \ + !defined(LZ4_FORCE_SW_BITCOUNT) return (unsigned)__builtin_ctz((U32)val) >> 3; # else - static const int DeBruijnBytePos[32] = { 0, 0, 3, 0, 3, 1, 3, 0, - 3, 2, 2, 1, 3, 2, 0, 1, - 3, 3, 1, 2, 2, 2, 2, 0, - 3, 1, 2, 0, 1, 0, 1, 1 }; - return DeBruijnBytePos[((U32)((val & -(S32)val) * 0x077CB531U)) >> 27]; + const U32 m = 0x01010101; + return (unsigned)((((val - 1) ^ val) & (m - 1)) * m) >> 24; # endif } } else /* Big Endian CPU */ { - if (sizeof(val)==8) { /* 64-bits */ -# if defined(_MSC_VER) && defined(_WIN64) && !defined(LZ4_FORCE_SW_BITCOUNT) - unsigned long r = 0; - _BitScanReverse64( &r, val ); - return (unsigned)(r>>3); -# elif (defined(__clang__) || (defined(__GNUC__) && (__GNUC__>=3))) && !defined(LZ4_FORCE_SW_BITCOUNT) + if (sizeof(val)==8) { +# if (defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 3) || \ + ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))))) && \ + !defined(LZ4_FORCE_SW_BITCOUNT) return (unsigned)__builtin_clzll((U64)val) >> 3; # else - static const U32 by32 = sizeof(val)*4; /* 32 on 64 bits (goal), 16 on 32 bits. - Just to avoid some static analyzer complaining about shift by 32 on 32-bits target. - Note that this code path is never triggered in 32-bits mode. */ - unsigned r; - if (!(val>>by32)) { r=4; } else { r=0; val>>=by32; } - if (!(val>>16)) { r+=2; val>>=8; } else { val>>=24; } - r += (!val); - return r; + static const unsigned char ctz7_tab[128] = { + 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, + 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, + 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, + 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, + 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, + 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, + 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, + 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, + }; + const U64 mask = 0x0101010101010101ULL; + val = (((val >> 8) - mask) | val) & mask; + return ctz7_tab[((U64)(val * 0x0080402010080402ULL)) >> 57]; # endif } else /* 32 bits */ { -# if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT) - unsigned long r = 0; - _BitScanReverse( &r, (unsigned long)val ); - return (unsigned)(r>>3); -# elif (defined(__clang__) || (defined(__GNUC__) && (__GNUC__>=3))) && !defined(LZ4_FORCE_SW_BITCOUNT) +# if (defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 3) || \ + ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))))) && \ + !defined(LZ4_FORCE_SW_BITCOUNT) return (unsigned)__builtin_clz((U32)val) >> 3; # else - unsigned r; - if (!(val>>16)) { r=2; val>>=8; } else { r=0; val>>=24; } - r += (!val); - return r; + val >>= 8; + val = ((((val + 0x00FFFF00) | 0x00FFFFFF) + val) | + (val + 0x00FF0000)) >> 24; + return (unsigned)val ^ 3; # endif } } } + #define STEPSIZE sizeof(reg_t) LZ4_FORCE_INLINE unsigned LZ4_count(const BYTE* pIn, const BYTE* pMatch, const BYTE* pInLimit) -- cgit v1.2.3 From f9f3fdfb1cb3b359a81247b9b72e08f435a1f5e3 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 12 Aug 2020 14:39:01 -0700 Subject: added test decompress-partial with extraneous input bytes fails currently, for investigation of #783 --- .travis.yml | 4 +--- tests/Makefile | 2 +- tests/fuzzer.c | 27 ++++++++++++++++++--------- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/.travis.yml b/.travis.yml index bd29630..20c1f89 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,9 +10,7 @@ matrix: script: - make # test library build - make clean - - make -C tests test-lz4 MOREFLAGS='-Werror -Wconversion -Wno-sign-conversion' | tee # test scenario where `stdout` is not the console - - make clean - - CFLAGS=-m32 make -C tests test-lz4-contentSize + - make test MOREFLAGS='-Werror -Wconversion -Wno-sign-conversion' | tee # test scenario where `stdout` is not the console # Container-based 12.04 LTS Server Edition 64 bit (doesn't support 32-bit includes) - name: (Precise) benchmark test diff --git a/tests/Makefile b/tests/Makefile index 05cae15..80b30f3 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -391,7 +391,7 @@ test-lz4-dict: lz4 datagen test-lz4-hugefile: lz4 datagen @echo "\n ---- test huge files compression/decompression ----" ./datagen -g6GB | $(LZ4) -vB5D | $(LZ4) -qt - ./datagen -g5GB | $(LZ4) -v4BD | $(LZ4) -qt + ./datagen -g4500MB | $(LZ4) -v3BD | $(LZ4) -qt # test large file size [2-4] GB @./datagen -g3G -P100 | $(LZ4) -vv | $(LZ4) --decompress --force --sparse - tmphf1 @ls -ls tmphf1 diff --git a/tests/fuzzer.c b/tests/fuzzer.c index 3d7456a..cbb53ca 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -620,11 +620,16 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c FUZ_DISPLAYTEST("test LZ4_decompress_safe_partial"); { size_t const missingBytes = FUZ_rand(&randState) % (unsigned)blockSize; int const targetSize = (int)((size_t)blockSize - missingBytes); + size_t const extraneousInBytes = FUZ_rand(&randState) % 2; + int const inCSize = (int)((size_t)compressedSize + extraneousInBytes); char const sentinel = decodedBuffer[targetSize] = block[targetSize] ^ 0x5A; - int const decResult = LZ4_decompress_safe_partial(compressedBuffer, decodedBuffer, compressedSize, targetSize, blockSize); + //DISPLAY("compressedSize=%i, inCSize=%i \n", compressedSize, inCSize); + //DISPLAY("decompressedSize=%i, targetDstSize=%i \n", blockSize, targetSize); + int const decResult = LZ4_decompress_safe_partial(compressedBuffer, decodedBuffer, inCSize, targetSize, blockSize); FUZ_CHECKTEST(decResult<0, "LZ4_decompress_safe_partial failed despite valid input data (error:%i)", decResult); FUZ_CHECKTEST(decResult != targetSize, "LZ4_decompress_safe_partial did not regenerated required amount of data (%i < %i <= %i)", decResult, targetSize, blockSize); FUZ_CHECKTEST(decodedBuffer[targetSize] != sentinel, "LZ4_decompress_safe_partial overwrite beyond requested size (though %i <= %i <= %i)", decResult, targetSize, blockSize); + FUZ_CHECKTEST(memcmp(block, decodedBuffer, (size_t)targetSize), "LZ4_decompress_safe_partial: corruption detected in regenerated data"); } /* Test Compression with limited output size */ @@ -856,9 +861,9 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c FUZ_CHECKTEST(decodedBuffer[blockSize-1], "LZ4_decompress_safe_usingDict overrun specified output buffer size"); FUZ_DISPLAYTEST("LZ4_decompress_safe_usingDict with a too small output buffer"); - { U32 const missingBytes = (FUZ_rand(&randState) & 0xF) + 2; - if ((U32)blockSize > missingBytes) { - decodedBuffer[(U32)blockSize-missingBytes] = 0; + { int const missingBytes = (FUZ_rand(&randState) & 0xF) + 2; + if (blockSize > missingBytes) { + decodedBuffer[blockSize-missingBytes] = 0; ret = LZ4_decompress_safe_usingDict(compressedBuffer, decodedBuffer, blockContinueCompressedSize, blockSize-missingBytes, dict, dictSize); FUZ_CHECKTEST(ret>=0, "LZ4_decompress_safe_usingDict should have failed : output buffer too small (-%u byte)", missingBytes); FUZ_CHECKTEST(decodedBuffer[blockSize-missingBytes], "LZ4_decompress_safe_usingDict overrun specified output buffer size (-%u byte) (blockSize=%i)", missingBytes, blockSize); @@ -948,7 +953,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c /* Compress HC continue destSize */ FUZ_DISPLAYTEST(); - { int const availableSpace = (int)(FUZ_rand(&randState) % blockSize) + 5; + { int const availableSpace = (int)(FUZ_rand(&randState) % (U32)blockSize) + 5; int consumedSize = blockSize; FUZ_DISPLAYTEST(); LZ4_loadDictHC(LZ4dictHC, dict, dictSize); @@ -974,10 +979,14 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c /* ***** End of tests *** */ /* Fill stats */ - bytes += blockSize; - cbytes += compressedSize; - hcbytes += HCcompressedSize; - ccbytes += blockContinueCompressedSize; + assert(blockSize >= 0); + bytes += (unsigned)blockSize; + assert(compressedSize >= 0); + cbytes += (unsigned)compressedSize; + assert(HCcompressedSize >= 0); + hcbytes += (unsigned)HCcompressedSize; + assert(blockContinueCompressedSize >= 0); + ccbytes += (unsigned)blockContinueCompressedSize; } if (nbCycles<=1) nbCycles = cycleNb; /* end by time */ -- cgit v1.2.3 From ab713923a24d52a6c3d0835627b3f0d715955467 Mon Sep 17 00:00:00 2001 From: BellaXlp <2273816832@qq.com> Date: Thu, 13 Aug 2020 05:42:10 +0800 Subject: fix issue #783 (#862) * fix issue #783 --- lib/lz4.c | 2 +- tests/Makefile | 14 ++++++++++--- tests/decompress-partial.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 tests/decompress-partial.c diff --git a/lib/lz4.c b/lib/lz4.c index 1cd7322..162a36a 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1931,7 +1931,7 @@ LZ4_decompress_generic( /* If we're in this block because of the input parsing condition, then we must be on the * last sequence (or invalid), so we must check that we exactly consume the input. */ - if ((ip+length>iend-(2+1+LASTLITERALS)) && (ip+length != iend)) { goto _output_error; } + if ((ip+length>iend-(2+1+LASTLITERALS)) && (ip+length != iend) && (cpy != oend)) { goto _output_error; } assert(ip+length <= iend); /* We are finishing in the middle of a literals segment. * Break after the copy. diff --git a/tests/Makefile b/tests/Makefile index 80b30f3..6404213 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -55,7 +55,7 @@ NB_LOOPS ?= -i1 default: all -all: fullbench fuzzer frametest roundTripTest datagen checkFrame +all: fullbench fuzzer frametest roundTripTest datagen checkFrame decompress-partial all32: CFLAGS+=-m32 all32: all @@ -104,6 +104,9 @@ datagen : $(PRGDIR)/datagen.c datagencli.c checkFrame : lz4frame.o lz4.o lz4hc.o xxhash.o checkFrame.c $(CC) $(FLAGS) $^ -o $@$(EXT) +decompress-partial: lz4.o decompress-partial.c + $(CC) $(FLAGS) $^ -o $@$(EXT) + clean: @$(MAKE) -C $(LZ4DIR) $@ > $(VOID) @$(MAKE) -C $(PRGDIR) $@ > $(VOID) @@ -114,7 +117,8 @@ clean: frametest$(EXT) frametest32$(EXT) \ fasttest$(EXT) roundTripTest$(EXT) \ datagen$(EXT) checkTag$(EXT) \ - frameTest$(EXT) lz4_all.c + frameTest$(EXT) decompress-partial$(EXT) \ + lz4_all.c @$(RM) -rf $(TESTDIR) @echo Cleaning completed @@ -156,7 +160,7 @@ list: check: test-lz4-essentials .PHONY: test -test: test-lz4 test-lz4c test-frametest test-fullbench test-fuzzer test-install test-amalgamation listTest +test: test-lz4 test-lz4c test-frametest test-fullbench test-fuzzer test-install test-amalgamation listTest test-decompress-partial .PHONY: test32 test32: CFLAGS+=-m32 @@ -519,4 +523,8 @@ test-mem: lz4 datagen fuzzer frametest fullbench test-mem32: lz4c32 datagen # unfortunately, valgrind doesn't seem to work with non-native binary... +test-decompress-partial : decompress-partial + @echo "\n ---- test decompress-partial ----" + ./decompress-partial$(EXT) + endif diff --git a/tests/decompress-partial.c b/tests/decompress-partial.c new file mode 100644 index 0000000..4e124b7 --- /dev/null +++ b/tests/decompress-partial.c @@ -0,0 +1,49 @@ +#include "stdio.h" +#include "string.h" +#include "lz4.h" + +const char source[] = + "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod\n" + "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim\n" + "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea\n" + "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate\n" + "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat\n" + "cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id\n" + "est laborum.\n" + "\n" + "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium\n" + "doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore\n" + "veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim\n" + "ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia\n" + "consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque\n" + "porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur,\n" + "adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore\n" + "et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis\n" + "nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid\n" + "ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea\n" + "voluptate velit esse quam nihil molestiae consequatur, vel illum qui\n" + "dolorem eum fugiat quo voluptas nulla pariatur?\n"; + +#define BUFFER_SIZE 2048 + +int main(void) +{ + int srcLen = (int)strlen(source); + char cmpBuffer[BUFFER_SIZE]; + char outBuffer[BUFFER_SIZE]; + int cmpSize; + int i; + + cmpSize = LZ4_compress_default(source, cmpBuffer, srcLen, BUFFER_SIZE); + + for (i = cmpSize; i < cmpSize + 10; ++i) { + int result = LZ4_decompress_safe_partial(cmpBuffer, outBuffer, i, srcLen, BUFFER_SIZE); + if ((result < 0) || (result != srcLen) || memcmp(source, outBuffer, srcLen)) { + printf("test decompress-partial error \n"); + return -1; + } + } + + printf("test decompress-partial OK \n"); + return 0; +} -- cgit v1.2.3 From e9cfa49d0ddde238a7f3dbe6320f11e3d1fe58ea Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 12 Aug 2020 17:27:33 -0700 Subject: Clarifies and fix EndMark EndMark, the 4-bytes value indicating the end of frame, must be `0x00000000`. Previously, it was just mentioned as a `0-size` block. But such definition could encompass uncompressed blocks of size 0, with a header of value `0x80000000`. But the intention was to also support uncompressed empty blocks. They could be used as a keep-alive signal. Note that compressed empty blocks are already supported, it's just that they have a size 1 instead of 0 (for the `0` token). Unfortunately, the decoder implementation was also wrong, and would also interpret a `0x80000000` block header as an endMark. This issue evaded detection so far simply because this situation never happens, as LZ4Frame always issues a clean 0x00000000 value as a endMark. It also does not flush empty blocks. This is fixed in this PR. The decoder can now deal with empty uncompressed blocks, and do not confuse them with EndMark. The specification is also clarified. Finally, FrameTest is updated to randomly insert empty blocks during fuzzing. --- doc/lz4_Frame_format.md | 47 +++++++++++++++++++++++++++++------------------ lib/lz4frame.c | 8 +++++--- tests/frametest.c | 30 +++++++++++++++++++++--------- 3 files changed, 55 insertions(+), 30 deletions(-) diff --git a/doc/lz4_Frame_format.md b/doc/lz4_Frame_format.md index a0514e0..4548698 100644 --- a/doc/lz4_Frame_format.md +++ b/doc/lz4_Frame_format.md @@ -16,7 +16,7 @@ Distribution of this document is unlimited. ### Version -1.6.1 (30/01/2018) +1.6.2 (12/08/2020) Introduction @@ -75,7 +75,7 @@ __Frame Descriptor__ 3 to 15 Bytes, to be detailed in its own paragraph, as it is the most important part of the spec. -The combined __Magic Number__ and __Frame Descriptor__ fields are sometimes +The combined _Magic_Number_ and _Frame_Descriptor_ fields are sometimes called ___LZ4 Frame Header___. Its size varies between 7 and 19 bytes. __Data Blocks__ @@ -85,14 +85,13 @@ That’s where compressed data is stored. __EndMark__ -The flow of blocks ends when the last data block has a size of “0”. -The size is expressed as a 32-bits value. +The flow of blocks ends when the last data block is announced with +an invalid size of “0”, expressed as a 32-bits value (exactly `0x00000000`). __Content Checksum__ -Content Checksum verify that the full content has been decoded correctly. -The content checksum is the result -of [xxh32() hash function](https://github.com/Cyan4973/xxHash) +_Content_Checksum_ verify that the full content has been decoded correctly. +The content checksum is the result of [xxHash-32 algorithm] digesting the original (decoded) data as input, and a seed of zero. Content checksum is only present when its associated flag is set in the frame descriptor. @@ -101,7 +100,7 @@ that all blocks were fully transmitted in the correct order and without error, and also that the encoding/decoding process itself generated no distortion. Its usage is recommended. -The combined __EndMark__ and __Content Checksum__ fields might sometimes be +The combined _EndMark_ and _Content_Checksum_ fields might sometimes be referred to as ___LZ4 Frame Footer___. Its size varies between 4 and 8 bytes. __Frame Concatenation__ @@ -261,16 +260,24 @@ __Block Size__ This field uses 4-bytes, format is little-endian. -The highest bit is “1” if data in the block is uncompressed. +If highest bit is set (`1`), the block is uncompressed. -The highest bit is “0” if data in the block is compressed by LZ4. +If highest bit is not set (`0`), the block is LZ4-compressed, +using the [LZ4 block format specification](https://github.com/lz4/lz4/blob/master/doc/lz4_Block_format.md). -All other bits give the size, in bytes, of the following data block. +All other bits give the size, in bytes, of the data section. The size does not include the block checksum if present. -Block Size shall never be larger than Block Maximum Size. -Such a thing could potentially happen for non-compressible sources. -In such a case, such data block shall be passed using uncompressed format. +_Block_Size_ shall never be larger than _Block_Maximum_Size_. +Such an outcome could potentially happen for non-compressible sources. +In such a case, such data block must be passed using uncompressed format. + +A value of `0x00000000` is invalid, and signifies an _EndMark_ instead. +Note that this is different from a value of `0x80000000` (highest bit set), +which is an uncompressed block of size 0 (empty), +which is valid, and therefore doesn't end a frame. +Note that, if _Block_checksum_ is enabled, +even an empty block must be followed by a 32-bit block checksum. __Data__ @@ -279,20 +286,22 @@ It might be compressed or not, depending on previous field indications. When compressed, the data must respect the [LZ4 block format specification](https://github.com/lz4/lz4/blob/master/doc/lz4_Block_format.md). -Note that the block is not necessarily full. -Uncompressed size of data can be any size, up to "Block Maximum Size”, +Note that a block is not necessarily full. +Uncompressed size of data can be any size __up to__ _Block_Maximum_Size_, so it may contain less data than the maximum block size. __Block checksum__ Only present if the associated flag is set. This is a 4-bytes checksum value, in little endian format, -calculated by using the xxHash-32 algorithm on the raw (undecoded) data block, +calculated by using the [xxHash-32 algorithm] on the __raw__ (undecoded) data block, and a seed of zero. The intention is to detect data corruption (storage or transmission errors) before decoding. -Block checksum is cumulative with Content checksum. +_Block_checksum_ can be cumulative with _Content_checksum_. + +[xxHash-32 algorithm]: https://github.com/Cyan4973/xxHash/blob/release/doc/xxhash_spec.md Skippable Frames @@ -389,6 +398,8 @@ and trigger an error if it does not fit within acceptable range. Version changes --------------- +1.6.2 : clarifies specification of _EndMark_ + 1.6.1 : introduced terms "LZ4 Frame Header" and "LZ4 Frame Footer" 1.6.0 : restored Dictionary ID field in Frame header diff --git a/lib/lz4frame.c b/lib/lz4frame.c index 5d716ea..e11f1c8 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -1483,14 +1483,16 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, } /* if (dctx->dStage == dstage_storeBlockHeader) */ /* decode block header */ - { size_t const nextCBlockSize = LZ4F_readLE32(selectedIn) & 0x7FFFFFFFU; + { U32 const blockHeader = LZ4F_readLE32(selectedIn); + size_t const nextCBlockSize = blockHeader & 0x7FFFFFFFU; size_t const crcSize = dctx->frameInfo.blockChecksumFlag * BFSize; - if (nextCBlockSize==0) { /* frameEnd signal, no more block */ + if (blockHeader==0) { /* frameEnd signal, no more block */ dctx->dStage = dstage_getSuffix; break; } - if (nextCBlockSize > dctx->maxBlockSize) + if (nextCBlockSize > dctx->maxBlockSize) { return err0r(LZ4F_ERROR_maxBlockSize_invalid); + } if (LZ4F_readLE32(selectedIn) & LZ4F_BLOCKUNCOMPRESSED_FLAG) { /* next block is uncompressed */ dctx->tmpInTarget = nextCBlockSize; diff --git a/tests/frametest.c b/tests/frametest.c index f891530..236a98c 100644 --- a/tests/frametest.c +++ b/tests/frametest.c @@ -995,13 +995,13 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi BYTE* op = (BYTE*)compressedBuffer; BYTE* const oend = op + (neverFlush ? LZ4F_compressFrameBound(srcSize, prefsPtr) : compressedBufferSize); /* when flushes are possible, can't guarantee a max compressed size */ unsigned const maxBits = FUZ_highbit((U32)srcSize); - size_t cSegmentSize; LZ4F_compressOptions_t cOptions; memset(&cOptions, 0, sizeof(cOptions)); - cSegmentSize = LZ4F_compressBegin(cCtx, op, (size_t)(oend-op), prefsPtr); - CHECK(LZ4F_isError(cSegmentSize), "Compression header failed (error %i)", - (int)cSegmentSize); - op += cSegmentSize; + { size_t const fhSize = LZ4F_compressBegin(cCtx, op, (size_t)(oend-op), prefsPtr); + CHECK(LZ4F_isError(fhSize), "Compression header failed (error %i)", + (int)fhSize); + op += fhSize; + } while (ip < iend) { unsigned const nbBitsSeg = FUZ_rand(&randState) % maxBits; size_t const sampleMax = (FUZ_rand(&randState) & ((1<frameInfo.blockChecksumFlag) { + U32 const bc32 = XXH32(op, 0, 0); + op[0] = (BYTE)bc32; /* little endian format */ + op[1] = (BYTE)(bc32>>8); + op[2] = (BYTE)(bc32>>16); + op[3] = (BYTE)(bc32>>24); + op += 4; + } } } } + } /* while (ip=oend, "LZ4F_compressFrameBound overflow"); { size_t const dstEndSafeSize = LZ4F_compressBound(0, prefsPtr); int const tooSmallDstEnd = ((FUZ_rand(&randState) & 31) == 3); @@ -1086,8 +1098,8 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi DISPLAYLEVEL(6, "noisy decompression \n"); test_lz4f_decompression(compressedBuffer, cSize, srcStart, srcSize, crcOrig, &randState, dCtxNoise, seed, testNb); /* note : we don't analyze result here : it probably failed, which is expected. - * We just check for potential out-of-bound reads and writes. */ - LZ4F_resetDecompressionContext(dCtxNoise); /* context must be reset after an error */ + * The sole purpose is to catch potential out-of-bound reads and writes. */ + LZ4F_resetDecompressionContext(dCtxNoise); /* context must be reset after an error */ #endif } /* for ( ; (testNb < nbTests) ; ) */ -- cgit v1.2.3 From 5ab7d22fa5622ab0a02bc627e6ec8742a8e3707c Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 14 Aug 2020 15:03:03 -0700 Subject: clarify endMark definition --- doc/lz4_Frame_format.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/doc/lz4_Frame_format.md b/doc/lz4_Frame_format.md index 4548698..e7cbdbf 100644 --- a/doc/lz4_Frame_format.md +++ b/doc/lz4_Frame_format.md @@ -85,8 +85,8 @@ That’s where compressed data is stored. __EndMark__ -The flow of blocks ends when the last data block is announced with -an invalid size of “0”, expressed as a 32-bits value (exactly `0x00000000`). +The flow of blocks ends when the last data block is followed by +the 32-bit value `0x00000000`. __Content Checksum__ @@ -260,9 +260,9 @@ __Block Size__ This field uses 4-bytes, format is little-endian. -If highest bit is set (`1`), the block is uncompressed. +If the highest bit is set (`1`), the block is uncompressed. -If highest bit is not set (`0`), the block is LZ4-compressed, +If the highest bit is not set (`0`), the block is LZ4-compressed, using the [LZ4 block format specification](https://github.com/lz4/lz4/blob/master/doc/lz4_Block_format.md). All other bits give the size, in bytes, of the data section. -- cgit v1.2.3 From e45defa8bdfe711e3e6c800970516038ca6d7143 Mon Sep 17 00:00:00 2001 From: aqrit Date: Mon, 17 Aug 2020 17:53:07 -0400 Subject: silence warning MSVC debug mode complains --- lib/lz4.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 5d0a2d2..98fc657 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -538,8 +538,8 @@ static unsigned LZ4_NbCommonBytes (reg_t val) 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, }; const U64 mask = 0x0101010101010101ULL; - val = (((val >> 8) - mask) | val) & mask; - return ctz7_tab[((U64)(val * 0x0080402010080402ULL)) >> 57]; + U64 t = (((val >> 8) - mask) | val) & mask; + return ctz7_tab[(t * 0x0080402010080402ULL) >> 57]; # endif } else /* 32 bits */ { # if (defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 3) || \ -- cgit v1.2.3 From 31d477db07c999afd639c3a37105894ecbf710fc Mon Sep 17 00:00:00 2001 From: Nick Terrell Date: Wed, 19 Aug 2020 18:06:49 -0700 Subject: [build] Move CMake and Visual build systems to build/ Fixes #852. --- Makefile | 2 +- NEWS | 2 + appveyor.yml | 10 +- build/.gitignore | 16 ++ build/README.md | 55 ++++++ build/VS2010/datagen/datagen.vcxproj | 169 ++++++++++++++++ build/VS2010/frametest/frametest.vcxproj | 176 +++++++++++++++++ build/VS2010/fullbench-dll/fullbench-dll.vcxproj | 180 +++++++++++++++++ build/VS2010/fullbench/fullbench.vcxproj | 176 +++++++++++++++++ build/VS2010/fuzzer/fuzzer.vcxproj | 173 ++++++++++++++++ build/VS2010/liblz4-dll/liblz4-dll.rc | 51 +++++ build/VS2010/liblz4-dll/liblz4-dll.vcxproj | 179 +++++++++++++++++ build/VS2010/liblz4/liblz4.vcxproj | 175 +++++++++++++++++ build/VS2010/lz4.sln | 98 ++++++++++ build/VS2010/lz4/lz4.rc | 51 +++++ build/VS2010/lz4/lz4.vcxproj | 189 ++++++++++++++++++ build/VS2017/datagen/datagen.vcxproj | 173 ++++++++++++++++ build/VS2017/frametest/frametest.vcxproj | 180 +++++++++++++++++ build/VS2017/fullbench-dll/fullbench-dll.vcxproj | 184 +++++++++++++++++ build/VS2017/fullbench/fullbench.vcxproj | 180 +++++++++++++++++ build/VS2017/fuzzer/fuzzer.vcxproj | 177 +++++++++++++++++ build/VS2017/liblz4-dll/liblz4-dll.rc | 51 +++++ build/VS2017/liblz4-dll/liblz4-dll.vcxproj | 183 +++++++++++++++++ build/VS2017/liblz4/liblz4.vcxproj | 179 +++++++++++++++++ build/VS2017/lz4.sln | 103 ++++++++++ build/VS2017/lz4/lz4.rc | 51 +++++ build/VS2017/lz4/lz4.vcxproj | 164 ++++++++++++++++ build/cmake/.gitignore | 9 + build/cmake/CMakeLists.txt | 228 ++++++++++++++++++++++ contrib/cmake_unofficial/.gitignore | 9 - contrib/cmake_unofficial/CMakeLists.txt | 228 ---------------------- contrib/debian/rules | 3 +- visual/.gitignore | 16 -- visual/README.md | 53 ----- visual/VS2010/datagen/datagen.vcxproj | 169 ---------------- visual/VS2010/frametest/frametest.vcxproj | 176 ----------------- visual/VS2010/fullbench-dll/fullbench-dll.vcxproj | 180 ----------------- visual/VS2010/fullbench/fullbench.vcxproj | 176 ----------------- visual/VS2010/fuzzer/fuzzer.vcxproj | 173 ---------------- visual/VS2010/liblz4-dll/liblz4-dll.rc | 51 ----- visual/VS2010/liblz4-dll/liblz4-dll.vcxproj | 179 ----------------- visual/VS2010/liblz4/liblz4.vcxproj | 175 ----------------- visual/VS2010/lz4.sln | 98 ---------- visual/VS2010/lz4/lz4.rc | 51 ----- visual/VS2010/lz4/lz4.vcxproj | 189 ------------------ visual/VS2017/datagen/datagen.vcxproj | 173 ---------------- visual/VS2017/frametest/frametest.vcxproj | 180 ----------------- visual/VS2017/fullbench-dll/fullbench-dll.vcxproj | 184 ----------------- visual/VS2017/fullbench/fullbench.vcxproj | 180 ----------------- visual/VS2017/fuzzer/fuzzer.vcxproj | 177 ----------------- visual/VS2017/liblz4-dll/liblz4-dll.rc | 51 ----- visual/VS2017/liblz4-dll/liblz4-dll.vcxproj | 183 ----------------- visual/VS2017/liblz4/liblz4.vcxproj | 179 ----------------- visual/VS2017/lz4.sln | 103 ---------- visual/VS2017/lz4/lz4.rc | 51 ----- visual/VS2017/lz4/lz4.vcxproj | 164 ---------------- 56 files changed, 3559 insertions(+), 3556 deletions(-) create mode 100644 build/.gitignore create mode 100644 build/README.md create mode 100644 build/VS2010/datagen/datagen.vcxproj create mode 100644 build/VS2010/frametest/frametest.vcxproj create mode 100644 build/VS2010/fullbench-dll/fullbench-dll.vcxproj create mode 100644 build/VS2010/fullbench/fullbench.vcxproj create mode 100644 build/VS2010/fuzzer/fuzzer.vcxproj create mode 100644 build/VS2010/liblz4-dll/liblz4-dll.rc create mode 100644 build/VS2010/liblz4-dll/liblz4-dll.vcxproj create mode 100644 build/VS2010/liblz4/liblz4.vcxproj create mode 100644 build/VS2010/lz4.sln create mode 100644 build/VS2010/lz4/lz4.rc create mode 100644 build/VS2010/lz4/lz4.vcxproj create mode 100644 build/VS2017/datagen/datagen.vcxproj create mode 100644 build/VS2017/frametest/frametest.vcxproj create mode 100644 build/VS2017/fullbench-dll/fullbench-dll.vcxproj create mode 100644 build/VS2017/fullbench/fullbench.vcxproj create mode 100644 build/VS2017/fuzzer/fuzzer.vcxproj create mode 100644 build/VS2017/liblz4-dll/liblz4-dll.rc create mode 100644 build/VS2017/liblz4-dll/liblz4-dll.vcxproj create mode 100644 build/VS2017/liblz4/liblz4.vcxproj create mode 100644 build/VS2017/lz4.sln create mode 100644 build/VS2017/lz4/lz4.rc create mode 100644 build/VS2017/lz4/lz4.vcxproj create mode 100644 build/cmake/.gitignore create mode 100644 build/cmake/CMakeLists.txt delete mode 100644 contrib/cmake_unofficial/.gitignore delete mode 100644 contrib/cmake_unofficial/CMakeLists.txt delete mode 100644 visual/.gitignore delete mode 100644 visual/README.md delete mode 100644 visual/VS2010/datagen/datagen.vcxproj delete mode 100644 visual/VS2010/frametest/frametest.vcxproj delete mode 100644 visual/VS2010/fullbench-dll/fullbench-dll.vcxproj delete mode 100644 visual/VS2010/fullbench/fullbench.vcxproj delete mode 100644 visual/VS2010/fuzzer/fuzzer.vcxproj delete mode 100644 visual/VS2010/liblz4-dll/liblz4-dll.rc delete mode 100644 visual/VS2010/liblz4-dll/liblz4-dll.vcxproj delete mode 100644 visual/VS2010/liblz4/liblz4.vcxproj delete mode 100644 visual/VS2010/lz4.sln delete mode 100644 visual/VS2010/lz4/lz4.rc delete mode 100644 visual/VS2010/lz4/lz4.vcxproj delete mode 100644 visual/VS2017/datagen/datagen.vcxproj delete mode 100644 visual/VS2017/frametest/frametest.vcxproj delete mode 100644 visual/VS2017/fullbench-dll/fullbench-dll.vcxproj delete mode 100644 visual/VS2017/fullbench/fullbench.vcxproj delete mode 100644 visual/VS2017/fuzzer/fuzzer.vcxproj delete mode 100644 visual/VS2017/liblz4-dll/liblz4-dll.rc delete mode 100644 visual/VS2017/liblz4-dll/liblz4-dll.vcxproj delete mode 100644 visual/VS2017/liblz4/liblz4.vcxproj delete mode 100644 visual/VS2017/lz4.sln delete mode 100644 visual/VS2017/lz4/lz4.rc delete mode 100644 visual/VS2017/lz4/lz4.vcxproj diff --git a/Makefile b/Makefile index f25f951..ab695ea 100644 --- a/Makefile +++ b/Makefile @@ -98,7 +98,7 @@ travis-install: $(MAKE) -j1 install DESTDIR=~/install_test_dir cmake: - @cd contrib/cmake_unofficial; cmake $(CMAKE_PARAMS) CMakeLists.txt; $(MAKE) + @cd build/cmake; cmake $(CMAKE_PARAMS) CMakeLists.txt; $(MAKE) endif diff --git a/NEWS b/NEWS index 44c45be..34ca486 100644 --- a/NEWS +++ b/NEWS @@ -1,5 +1,7 @@ v1.9.3 perf: faster speed on Visual Studio, by @wolfpld +build: contrib/cmake_unofficial/ moved to build/cmake/ +build: visual/* moved to build/ v1.9.2 diff --git a/appveyor.yml b/appveyor.yml index 056719a..57b6dda 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -82,20 +82,20 @@ build_script: ECHO *** && ECHO *** Building Visual Studio 2010 %PLATFORM%\%CONFIGURATION% && ECHO *** && - msbuild "visual\VS2010\lz4.sln" %ADDITIONALPARAM% /m /verbosity:minimal /property:PlatformToolset=v100 /t:Clean,Build /p:Platform=%PLATFORM% /p:Configuration=%CONFIGURATION% /p:EnableWholeProgramOptimization=true /logger:"C:\Program Files\AppVeyor\BuildAgent\Appveyor.MSBuildLogger.dll" && + msbuild "build\VS2010\lz4.sln" %ADDITIONALPARAM% /m /verbosity:minimal /property:PlatformToolset=v100 /t:Clean,Build /p:Platform=%PLATFORM% /p:Configuration=%CONFIGURATION% /p:EnableWholeProgramOptimization=true /logger:"C:\Program Files\AppVeyor\BuildAgent\Appveyor.MSBuildLogger.dll" && ECHO *** && ECHO *** Building Visual Studio 2012 %PLATFORM%\%CONFIGURATION% && ECHO *** && - msbuild "visual\VS2010\lz4.sln" /m /verbosity:minimal /property:PlatformToolset=v110 /t:Clean,Build /p:Platform=%PLATFORM% /p:Configuration=%CONFIGURATION% /logger:"C:\Program Files\AppVeyor\BuildAgent\Appveyor.MSBuildLogger.dll" && + msbuild "build\VS2010\lz4.sln" /m /verbosity:minimal /property:PlatformToolset=v110 /t:Clean,Build /p:Platform=%PLATFORM% /p:Configuration=%CONFIGURATION% /logger:"C:\Program Files\AppVeyor\BuildAgent\Appveyor.MSBuildLogger.dll" && ECHO *** && ECHO *** Building Visual Studio 2013 %PLATFORM%\%CONFIGURATION% && ECHO *** && - msbuild "visual\VS2010\lz4.sln" /m /verbosity:minimal /property:PlatformToolset=v120 /t:Clean,Build /p:Platform=%PLATFORM% /p:Configuration=%CONFIGURATION% /logger:"C:\Program Files\AppVeyor\BuildAgent\Appveyor.MSBuildLogger.dll" && + msbuild "build\VS2010\lz4.sln" /m /verbosity:minimal /property:PlatformToolset=v120 /t:Clean,Build /p:Platform=%PLATFORM% /p:Configuration=%CONFIGURATION% /logger:"C:\Program Files\AppVeyor\BuildAgent\Appveyor.MSBuildLogger.dll" && ECHO *** && ECHO *** Building Visual Studio 2015 %PLATFORM%\%CONFIGURATION% && ECHO *** && - msbuild "visual\VS2010\lz4.sln" /m /verbosity:minimal /property:PlatformToolset=v140 /t:Clean,Build /p:Platform=%PLATFORM% /p:Configuration=%CONFIGURATION% /logger:"C:\Program Files\AppVeyor\BuildAgent\Appveyor.MSBuildLogger.dll" && - COPY visual\VS2010\bin\%PLATFORM%_%CONFIGURATION%\*.exe programs\ + msbuild "build\VS2010\lz4.sln" /m /verbosity:minimal /property:PlatformToolset=v140 /t:Clean,Build /p:Platform=%PLATFORM% /p:Configuration=%CONFIGURATION% /logger:"C:\Program Files\AppVeyor\BuildAgent\Appveyor.MSBuildLogger.dll" && + COPY build\VS2010\bin\%PLATFORM%_%CONFIGURATION%\*.exe programs\ ) test_script: diff --git a/build/.gitignore b/build/.gitignore new file mode 100644 index 0000000..69e1111 --- /dev/null +++ b/build/.gitignore @@ -0,0 +1,16 @@ +# Visual C++ +.vs/ +*Copy +*.db +*.opensdf +*.sdf +*.suo +*.user +ver*/ +VS2010/bin/ +VS2017/bin/ +ipch + +# Fixup for lz4 project directories +!VS2010/lz4 +!VS2017/lz4 diff --git a/build/README.md b/build/README.md new file mode 100644 index 0000000..d416aeb --- /dev/null +++ b/build/README.md @@ -0,0 +1,55 @@ +Projects for various integrated development environments (IDE) +============================================================== + +#### Included projects + +The following projects are included with the lz4 distribution: +- `cmake` - CMake project +- `VS2010` - Visual Studio 2010 project (which also works well with Visual Studio 2012, 2013, 2015) +- `VS2017` - Visual Studio 2017 project + + +#### How to compile lz4 with Visual Studio + +1. Install Visual Studio e.g. VS 2015 Community Edition (it's free). +2. Download the latest version of lz4 from https://github.com/lz4/lz4/releases +3. Decompress ZIP archive. +4. Go to decompressed directory then to `build` then `VS2010` and open `lz4.sln` +5. Visual Studio will ask about converting VS2010 project to VS2015 and you should agree. +6. Change `Debug` to `Release` and if you have 64-bit Windows change also `Win32` to `x64`. +7. Press F7 on keyboard or select `BUILD` from the menu bar and choose `Build Solution`. +8. If compilation will be fine a compiled executable will be in `build\VS2010\bin\x64_Release\lz4.exe` + + +#### Projects available within lz4.sln + +The Visual Studio solution file `lz4.sln` contains many projects that will be compiled to the +`build\VS2010\bin\$(Platform)_$(Configuration)` directory. For example `lz4` set to `x64` and +`Release` will be compiled to `build\VS2010\bin\x64_Release\lz4.exe`. The solution file contains the +following projects: + +- `lz4` : Command Line Utility, supporting gzip-like arguments +- `datagen` : Synthetic and parametrable data generator, for tests +- `frametest` : Test tool that checks lz4frame integrity on target platform +- `fullbench` : Precisely measure speed for each lz4 inner functions +- `fuzzer` : Test tool, to check lz4 integrity on target platform +- `liblz4` : A static LZ4 library compiled to `liblz4_static.lib` +- `liblz4-dll` : A dynamic LZ4 library (DLL) compiled to `liblz4.dll` with the import library `liblz4.lib` +- `fullbench-dll` : The fullbench program compiled with the import library; the executable requires LZ4 DLL + + +#### Using LZ4 DLL with Microsoft Visual C++ project + +The header files `lib\lz4.h`, `lib\lz4hc.h`, `lib\lz4frame.h` and the import library +`build\VS2010\bin\$(Platform)_$(Configuration)\liblz4.lib` are required to +compile a project using Visual C++. + +1. The path to header files should be added to `Additional Include Directories` that can + be found in Project Properties of Visual Studio IDE in the `C/C++` Property Pages on the `General` page. +2. The import library has to be added to `Additional Dependencies` that can + be found in Project Properties in the `Linker` Property Pages on the `Input` page. + If one will provide only the name `liblz4.lib` without a full path to the library + then the directory has to be added to `Linker\General\Additional Library Directories`. + +The compiled executable will require LZ4 DLL which is available at +`build\VS2010\bin\$(Platform)_$(Configuration)\liblz4.dll`. diff --git a/build/VS2010/datagen/datagen.vcxproj b/build/VS2010/datagen/datagen.vcxproj new file mode 100644 index 0000000..e24f961 --- /dev/null +++ b/build/VS2010/datagen/datagen.vcxproj @@ -0,0 +1,169 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {D745AE2F-596A-403A-9B91-81A8C6779243} + Win32Proj + datagen + $(SolutionDir)bin\$(Platform)_$(Configuration)\ + $(SolutionDir)bin\obj\$(RootNamespace)_$(Platform)_$(Configuration)\ + + + + Application + true + Unicode + + + Application + true + Unicode + + + Application + false + Unicode + true + + + Application + false + Unicode + true + + + + + + + + + + + + + + + + + + + true + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\programs;$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + + + true + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\programs;$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + true + + + false + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\programs;$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + + + false + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\programs;$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + true + + + + + + Level4 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + false + MultiThreadedDebug + + + Console + true + + + + + + + Level4 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + true + /analyze:stacksize295252 %(AdditionalOptions) + MultiThreadedDebug + + + Console + true + + + + + Level4 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + false + false + MultiThreaded + + + Console + true + true + true + + + + + Level4 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + false + true + /analyze:stacksize295252 %(AdditionalOptions) + MultiThreaded + + + Console + true + true + true + + + + + + + + + + + + + diff --git a/build/VS2010/frametest/frametest.vcxproj b/build/VS2010/frametest/frametest.vcxproj new file mode 100644 index 0000000..3196768 --- /dev/null +++ b/build/VS2010/frametest/frametest.vcxproj @@ -0,0 +1,176 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {39AD6ECC-8BAD-4368-95E4-A1AA2F077BB7} + Win32Proj + frametest + $(SolutionDir)bin\$(Platform)_$(Configuration)\ + $(SolutionDir)bin\obj\$(RootNamespace)_$(Platform)_$(Configuration)\ + + + + Application + true + Unicode + + + Application + true + Unicode + + + Application + false + Unicode + true + + + Application + false + Unicode + true + + + + + + + + + + + + + + + + + + + true + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + + + true + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + true + + + false + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + + + false + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + true + + + + + + Level4 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + false + MultiThreadedDebug + + + Console + true + + + + + + + Level4 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + true + /analyze:stacksize295252 %(AdditionalOptions) + MultiThreadedDebug + + + Console + true + + + + + Level4 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + false + false + MultiThreaded + + + Console + true + true + true + + + + + Level4 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + false + true + /analyze:stacksize295252 %(AdditionalOptions) + MultiThreaded + + + Console + true + true + true + + + + + + + + + + + + + + + + + + + + diff --git a/build/VS2010/fullbench-dll/fullbench-dll.vcxproj b/build/VS2010/fullbench-dll/fullbench-dll.vcxproj new file mode 100644 index 0000000..8f503f5 --- /dev/null +++ b/build/VS2010/fullbench-dll/fullbench-dll.vcxproj @@ -0,0 +1,180 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {13992FD2-077E-4954-B065-A428198201A9} + Win32Proj + fullbench-dll + $(SolutionDir)bin\$(Platform)_$(Configuration)\ + $(SolutionDir)bin\obj\$(RootNamespace)_$(Platform)_$(Configuration)\ + + + + Application + true + Unicode + + + Application + true + Unicode + + + Application + false + Unicode + true + + + Application + false + Unicode + true + + + + + + + + + + + + + + + + + + + true + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + + + true + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + true + + + false + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + + + false + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + true + + + + + + Level4 + Disabled + WIN32;_DEBUG;_CONSOLE;LZ4_DLL_IMPORT=1;%(PreprocessorDefinitions) + true + false + MultiThreadedDebug + + + Console + true + $(SolutionDir)bin\$(Platform)_$(Configuration);%(AdditionalLibraryDirectories) + liblz4.lib;%(AdditionalDependencies) + + + + + + + Level4 + Disabled + WIN32;_DEBUG;_CONSOLE;LZ4_DLL_IMPORT=1;%(PreprocessorDefinitions) + true + true + /analyze:stacksize295252 %(AdditionalOptions) + MultiThreadedDebug + + + Console + true + $(SolutionDir)bin\$(Platform)_$(Configuration);%(AdditionalLibraryDirectories) + liblz4.lib;%(AdditionalDependencies) + + + + + Level4 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;LZ4_DLL_IMPORT=1;%(PreprocessorDefinitions) + false + false + MultiThreaded + + + Console + true + true + true + $(SolutionDir)bin\$(Platform)_$(Configuration);%(AdditionalLibraryDirectories) + liblz4.lib;%(AdditionalDependencies) + + + + + Level4 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;LZ4_DLL_IMPORT=1;%(PreprocessorDefinitions) + false + true + /analyze:stacksize295252 %(AdditionalOptions) + MultiThreaded + + + Console + true + true + true + $(SolutionDir)bin\$(Platform)_$(Configuration);%(AdditionalLibraryDirectories) + liblz4.lib;%(AdditionalDependencies) + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/build/VS2010/fullbench/fullbench.vcxproj b/build/VS2010/fullbench/fullbench.vcxproj new file mode 100644 index 0000000..aa67431 --- /dev/null +++ b/build/VS2010/fullbench/fullbench.vcxproj @@ -0,0 +1,176 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {6A4DF4EF-C77F-43C6-8901-DDCD20879E4E} + Win32Proj + fullbench + $(SolutionDir)bin\$(Platform)_$(Configuration)\ + $(SolutionDir)bin\obj\$(RootNamespace)_$(Platform)_$(Configuration)\ + + + + Application + true + Unicode + + + Application + true + Unicode + + + Application + false + Unicode + true + + + Application + false + Unicode + true + + + + + + + + + + + + + + + + + + + true + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + + + true + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + true + + + false + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + + + false + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + true + + + + + + Level4 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + false + MultiThreadedDebug + + + Console + true + + + + + + + Level4 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + true + /analyze:stacksize295252 %(AdditionalOptions) + MultiThreadedDebug + + + Console + true + + + + + Level4 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + false + false + MultiThreaded + + + Console + true + true + true + + + + + Level4 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + false + true + /analyze:stacksize295252 %(AdditionalOptions) + MultiThreaded + + + Console + true + true + true + + + + + + + + + + + + + + + + + + + + diff --git a/build/VS2010/fuzzer/fuzzer.vcxproj b/build/VS2010/fuzzer/fuzzer.vcxproj new file mode 100644 index 0000000..21cbf56 --- /dev/null +++ b/build/VS2010/fuzzer/fuzzer.vcxproj @@ -0,0 +1,173 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {18B9F1A7-9C66-4352-898B-30804DADE0FD} + Win32Proj + fuzzer + $(SolutionDir)bin\$(Platform)_$(Configuration)\ + $(SolutionDir)bin\obj\$(RootNamespace)_$(Platform)_$(Configuration)\ + + + + Application + true + Unicode + + + Application + true + Unicode + + + Application + false + Unicode + true + + + Application + false + Unicode + true + + + + + + + + + + + + + + + + + + + true + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + + + true + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + true + + + false + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + + + false + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + true + + + + + + Level4 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + false + MultiThreadedDebug + + + Console + true + + + + + + + Level4 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + true + /analyze:stacksize295252 %(AdditionalOptions) + MultiThreadedDebug + + + Console + true + + + + + Level4 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + false + false + MultiThreaded + + + Console + true + true + true + + + + + Level4 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + false + true + /analyze:stacksize295252 %(AdditionalOptions) + MultiThreaded + + + Console + true + true + true + + + + + + + + + + + + + + + + + diff --git a/build/VS2010/liblz4-dll/liblz4-dll.rc b/build/VS2010/liblz4-dll/liblz4-dll.rc new file mode 100644 index 0000000..b1871fe --- /dev/null +++ b/build/VS2010/liblz4-dll/liblz4-dll.rc @@ -0,0 +1,51 @@ +// Microsoft Visual C++ generated resource script. +// + +#include "lz4.h" /* LZ4_VERSION_STRING */ +#define APSTUDIO_READONLY_SYMBOLS +#include "verrsrc.h" +#undef APSTUDIO_READONLY_SYMBOLS + + +#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU) +LANGUAGE 9, 1 + +///////////////////////////////////////////////////////////////////////////// +// +// Version +// + +VS_VERSION_INFO VERSIONINFO + FILEVERSION LZ4_VERSION_MAJOR,LZ4_VERSION_MINOR,LZ4_VERSION_RELEASE,0 + PRODUCTVERSION LZ4_VERSION_MAJOR,LZ4_VERSION_MINOR,LZ4_VERSION_RELEASE,0 + FILEFLAGSMASK VS_FFI_FILEFLAGSMASK +#ifdef _DEBUG + FILEFLAGS VS_FF_DEBUG +#else + FILEFLAGS 0x0L +#endif + FILEOS VOS_NT_WINDOWS32 + FILETYPE VFT_DLL + FILESUBTYPE VFT2_UNKNOWN +BEGIN + BLOCK "StringFileInfo" + BEGIN + BLOCK "040904B0" + BEGIN + VALUE "CompanyName", "Yann Collet" + VALUE "FileDescription", "Extremely fast compression" + VALUE "FileVersion", LZ4_VERSION_STRING + VALUE "InternalName", "lz4.dll" + VALUE "LegalCopyright", "Copyright (C) 2013-2016, Yann Collet" + VALUE "OriginalFilename", "lz4.dll" + VALUE "ProductName", "LZ4" + VALUE "ProductVersion", LZ4_VERSION_STRING + END + END + BLOCK "VarFileInfo" + BEGIN + VALUE "Translation", 0x0409, 1200 + END +END + +#endif diff --git a/build/VS2010/liblz4-dll/liblz4-dll.vcxproj b/build/VS2010/liblz4-dll/liblz4-dll.vcxproj new file mode 100644 index 0000000..56ec3b9 --- /dev/null +++ b/build/VS2010/liblz4-dll/liblz4-dll.vcxproj @@ -0,0 +1,179 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {9800039D-4AAA-43A4-BB78-FEF6F4836927} + Win32Proj + liblz4-dll + $(SolutionDir)bin\$(Platform)_$(Configuration)\ + $(SolutionDir)bin\obj\$(RootNamespace)_$(Platform)_$(Configuration)\ + liblz4-dll + + + + DynamicLibrary + true + Unicode + + + DynamicLibrary + true + Unicode + + + DynamicLibrary + false + Unicode + true + + + DynamicLibrary + false + Unicode + true + + + + + + + + + + + + + + + + + + + true + liblz4 + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + + + true + liblz4 + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + true + + + false + liblz4 + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + + + false + liblz4 + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + true + + + + + + Level4 + Disabled + WIN32;_DEBUG;LZ4_DLL_EXPORT=1;%(PreprocessorDefinitions) + true + false + MultiThreadedDebug + + + true + + + + + + + Level4 + Disabled + WIN32;_DEBUG;LZ4_DLL_EXPORT=1;%(PreprocessorDefinitions) + true + true + /analyze:stacksize295252 %(AdditionalOptions) + MultiThreadedDebug + + + true + + + + + Level4 + + + MaxSpeed + true + true + WIN32;NDEBUG;LZ4_DLL_EXPORT=1;%(PreprocessorDefinitions) + false + false + MultiThreaded + + + true + true + true + + + + + Level4 + + + MaxSpeed + true + true + WIN32;NDEBUG;LZ4_DLL_EXPORT=1;%(PreprocessorDefinitions) + false + true + /analyze:stacksize295252 %(AdditionalOptions) + MultiThreaded + + + true + true + true + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/build/VS2010/liblz4/liblz4.vcxproj b/build/VS2010/liblz4/liblz4.vcxproj new file mode 100644 index 0000000..61ea159 --- /dev/null +++ b/build/VS2010/liblz4/liblz4.vcxproj @@ -0,0 +1,175 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {9092C5CC-3E71-41B3-BF68-4A7BDD8A5476} + Win32Proj + liblz4 + $(SolutionDir)bin\$(Platform)_$(Configuration)\ + $(SolutionDir)bin\obj\$(RootNamespace)_$(Platform)_$(Configuration)\ + + + + StaticLibrary + true + Unicode + + + StaticLibrary + true + Unicode + + + StaticLibrary + false + Unicode + true + + + StaticLibrary + false + Unicode + true + + + + + + + + + + + + + + + + + + + true + liblz4_static + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + + + true + liblz4_static + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + true + + + false + liblz4_static + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + + + false + liblz4_static + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + true + + + + + + Level4 + Disabled + WIN32;_DEBUG;LZ4_DLL_EXPORT=1;%(PreprocessorDefinitions) + true + false + MultiThreadedDebug + + + true + + + + + + + Level4 + Disabled + WIN32;_DEBUG;LZ4_DLL_EXPORT=1;%(PreprocessorDefinitions) + true + true + /analyze:stacksize295252 %(AdditionalOptions) + MultiThreadedDebug + + + true + + + + + Level4 + + + MaxSpeed + true + true + WIN32;NDEBUG;LZ4_DLL_EXPORT=1;%(PreprocessorDefinitions) + false + false + MultiThreaded + + + true + true + true + + + + + Level4 + + + MaxSpeed + true + true + WIN32;NDEBUG;LZ4_DLL_EXPORT=1;%(PreprocessorDefinitions) + false + true + /analyze:stacksize295252 %(AdditionalOptions) + MultiThreaded + + + true + true + true + + + + + + + + + + + + + + + + + + + diff --git a/build/VS2010/lz4.sln b/build/VS2010/lz4.sln new file mode 100644 index 0000000..78f223b --- /dev/null +++ b/build/VS2010/lz4.sln @@ -0,0 +1,98 @@ +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Express 2012 for Windows Desktop +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "lz4", "lz4\lz4.vcxproj", "{E30329AC-0057-4FE0-8FDA-7F650D398C4C}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "liblz4-dll", "liblz4-dll\liblz4-dll.vcxproj", "{9800039D-4AAA-43A4-BB78-FEF6F4836927}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "liblz4", "liblz4\liblz4.vcxproj", "{9092C5CC-3E71-41B3-BF68-4A7BDD8A5476}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "fuzzer", "fuzzer\fuzzer.vcxproj", "{18B9F1A7-9C66-4352-898B-30804DADE0FD}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "fullbench", "fullbench\fullbench.vcxproj", "{6A4DF4EF-C77F-43C6-8901-DDCD20879E4E}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "frametest", "frametest\frametest.vcxproj", "{39AD6ECC-8BAD-4368-95E4-A1AA2F077BB7}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "datagen", "datagen\datagen.vcxproj", "{D745AE2F-596A-403A-9B91-81A8C6779243}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "fullbench-dll", "fullbench-dll\fullbench-dll.vcxproj", "{13992FD2-077E-4954-B065-A428198201A9}" + ProjectSection(ProjectDependencies) = postProject + {9800039D-4AAA-43A4-BB78-FEF6F4836927} = {9800039D-4AAA-43A4-BB78-FEF6F4836927} + EndProjectSection +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Debug|x64 = Debug|x64 + Release|Win32 = Release|Win32 + Release|x64 = Release|x64 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {E30329AC-0057-4FE0-8FDA-7F650D398C4C}.Debug|Win32.ActiveCfg = Debug|Win32 + {E30329AC-0057-4FE0-8FDA-7F650D398C4C}.Debug|Win32.Build.0 = Debug|Win32 + {E30329AC-0057-4FE0-8FDA-7F650D398C4C}.Debug|x64.ActiveCfg = Debug|x64 + {E30329AC-0057-4FE0-8FDA-7F650D398C4C}.Debug|x64.Build.0 = Debug|x64 + {E30329AC-0057-4FE0-8FDA-7F650D398C4C}.Release|Win32.ActiveCfg = Release|Win32 + {E30329AC-0057-4FE0-8FDA-7F650D398C4C}.Release|Win32.Build.0 = Release|Win32 + {E30329AC-0057-4FE0-8FDA-7F650D398C4C}.Release|x64.ActiveCfg = Release|x64 + {E30329AC-0057-4FE0-8FDA-7F650D398C4C}.Release|x64.Build.0 = Release|x64 + {9800039D-4AAA-43A4-BB78-FEF6F4836927}.Debug|Win32.ActiveCfg = Debug|Win32 + {9800039D-4AAA-43A4-BB78-FEF6F4836927}.Debug|Win32.Build.0 = Debug|Win32 + {9800039D-4AAA-43A4-BB78-FEF6F4836927}.Debug|x64.ActiveCfg = Debug|x64 + {9800039D-4AAA-43A4-BB78-FEF6F4836927}.Debug|x64.Build.0 = Debug|x64 + {9800039D-4AAA-43A4-BB78-FEF6F4836927}.Release|Win32.ActiveCfg = Release|Win32 + {9800039D-4AAA-43A4-BB78-FEF6F4836927}.Release|Win32.Build.0 = Release|Win32 + {9800039D-4AAA-43A4-BB78-FEF6F4836927}.Release|x64.ActiveCfg = Release|x64 + {9800039D-4AAA-43A4-BB78-FEF6F4836927}.Release|x64.Build.0 = Release|x64 + {9092C5CC-3E71-41B3-BF68-4A7BDD8A5476}.Debug|Win32.ActiveCfg = Debug|Win32 + {9092C5CC-3E71-41B3-BF68-4A7BDD8A5476}.Debug|Win32.Build.0 = Debug|Win32 + {9092C5CC-3E71-41B3-BF68-4A7BDD8A5476}.Debug|x64.ActiveCfg = Debug|x64 + {9092C5CC-3E71-41B3-BF68-4A7BDD8A5476}.Debug|x64.Build.0 = Debug|x64 + {9092C5CC-3E71-41B3-BF68-4A7BDD8A5476}.Release|Win32.ActiveCfg = Release|Win32 + {9092C5CC-3E71-41B3-BF68-4A7BDD8A5476}.Release|Win32.Build.0 = Release|Win32 + {9092C5CC-3E71-41B3-BF68-4A7BDD8A5476}.Release|x64.ActiveCfg = Release|x64 + {9092C5CC-3E71-41B3-BF68-4A7BDD8A5476}.Release|x64.Build.0 = Release|x64 + {18B9F1A7-9C66-4352-898B-30804DADE0FD}.Debug|Win32.ActiveCfg = Debug|Win32 + {18B9F1A7-9C66-4352-898B-30804DADE0FD}.Debug|Win32.Build.0 = Debug|Win32 + {18B9F1A7-9C66-4352-898B-30804DADE0FD}.Debug|x64.ActiveCfg = Debug|x64 + {18B9F1A7-9C66-4352-898B-30804DADE0FD}.Debug|x64.Build.0 = Debug|x64 + {18B9F1A7-9C66-4352-898B-30804DADE0FD}.Release|Win32.ActiveCfg = Release|Win32 + {18B9F1A7-9C66-4352-898B-30804DADE0FD}.Release|Win32.Build.0 = Release|Win32 + {18B9F1A7-9C66-4352-898B-30804DADE0FD}.Release|x64.ActiveCfg = Release|x64 + {18B9F1A7-9C66-4352-898B-30804DADE0FD}.Release|x64.Build.0 = Release|x64 + {6A4DF4EF-C77F-43C6-8901-DDCD20879E4E}.Debug|Win32.ActiveCfg = Debug|Win32 + {6A4DF4EF-C77F-43C6-8901-DDCD20879E4E}.Debug|Win32.Build.0 = Debug|Win32 + {6A4DF4EF-C77F-43C6-8901-DDCD20879E4E}.Debug|x64.ActiveCfg = Debug|x64 + {6A4DF4EF-C77F-43C6-8901-DDCD20879E4E}.Debug|x64.Build.0 = Debug|x64 + {6A4DF4EF-C77F-43C6-8901-DDCD20879E4E}.Release|Win32.ActiveCfg = Release|Win32 + {6A4DF4EF-C77F-43C6-8901-DDCD20879E4E}.Release|Win32.Build.0 = Release|Win32 + {6A4DF4EF-C77F-43C6-8901-DDCD20879E4E}.Release|x64.ActiveCfg = Release|x64 + {6A4DF4EF-C77F-43C6-8901-DDCD20879E4E}.Release|x64.Build.0 = Release|x64 + {39AD6ECC-8BAD-4368-95E4-A1AA2F077BB7}.Debug|Win32.ActiveCfg = Debug|Win32 + {39AD6ECC-8BAD-4368-95E4-A1AA2F077BB7}.Debug|Win32.Build.0 = Debug|Win32 + {39AD6ECC-8BAD-4368-95E4-A1AA2F077BB7}.Debug|x64.ActiveCfg = Debug|x64 + {39AD6ECC-8BAD-4368-95E4-A1AA2F077BB7}.Debug|x64.Build.0 = Debug|x64 + {39AD6ECC-8BAD-4368-95E4-A1AA2F077BB7}.Release|Win32.ActiveCfg = Release|Win32 + {39AD6ECC-8BAD-4368-95E4-A1AA2F077BB7}.Release|Win32.Build.0 = Release|Win32 + {39AD6ECC-8BAD-4368-95E4-A1AA2F077BB7}.Release|x64.ActiveCfg = Release|x64 + {39AD6ECC-8BAD-4368-95E4-A1AA2F077BB7}.Release|x64.Build.0 = Release|x64 + {D745AE2F-596A-403A-9B91-81A8C6779243}.Debug|Win32.ActiveCfg = Debug|Win32 + {D745AE2F-596A-403A-9B91-81A8C6779243}.Debug|Win32.Build.0 = Debug|Win32 + {D745AE2F-596A-403A-9B91-81A8C6779243}.Debug|x64.ActiveCfg = Debug|x64 + {D745AE2F-596A-403A-9B91-81A8C6779243}.Debug|x64.Build.0 = Debug|x64 + {D745AE2F-596A-403A-9B91-81A8C6779243}.Release|Win32.ActiveCfg = Release|Win32 + {D745AE2F-596A-403A-9B91-81A8C6779243}.Release|Win32.Build.0 = Release|Win32 + {D745AE2F-596A-403A-9B91-81A8C6779243}.Release|x64.ActiveCfg = Release|x64 + {D745AE2F-596A-403A-9B91-81A8C6779243}.Release|x64.Build.0 = Release|x64 + {13992FD2-077E-4954-B065-A428198201A9}.Debug|Win32.ActiveCfg = Debug|Win32 + {13992FD2-077E-4954-B065-A428198201A9}.Debug|Win32.Build.0 = Debug|Win32 + {13992FD2-077E-4954-B065-A428198201A9}.Debug|x64.ActiveCfg = Debug|x64 + {13992FD2-077E-4954-B065-A428198201A9}.Debug|x64.Build.0 = Debug|x64 + {13992FD2-077E-4954-B065-A428198201A9}.Release|Win32.ActiveCfg = Release|Win32 + {13992FD2-077E-4954-B065-A428198201A9}.Release|Win32.Build.0 = Release|Win32 + {13992FD2-077E-4954-B065-A428198201A9}.Release|x64.ActiveCfg = Release|x64 + {13992FD2-077E-4954-B065-A428198201A9}.Release|x64.Build.0 = Release|x64 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/build/VS2010/lz4/lz4.rc b/build/VS2010/lz4/lz4.rc new file mode 100644 index 0000000..c593edf --- /dev/null +++ b/build/VS2010/lz4/lz4.rc @@ -0,0 +1,51 @@ +// Microsoft Visual C++ generated resource script. +// + +#include "lz4.h" /* LZ4_VERSION_STRING */ +#define APSTUDIO_READONLY_SYMBOLS +#include "verrsrc.h" +#undef APSTUDIO_READONLY_SYMBOLS + + +#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU) +LANGUAGE 9, 1 + +///////////////////////////////////////////////////////////////////////////// +// +// Version +// + +VS_VERSION_INFO VERSIONINFO + FILEVERSION LZ4_VERSION_MAJOR,LZ4_VERSION_MINOR,LZ4_VERSION_RELEASE,0 + PRODUCTVERSION LZ4_VERSION_MAJOR,LZ4_VERSION_MINOR,LZ4_VERSION_RELEASE,0 + FILEFLAGSMASK VS_FFI_FILEFLAGSMASK +#ifdef _DEBUG + FILEFLAGS VS_FF_DEBUG +#else + FILEFLAGS 0x0L +#endif + FILEOS VOS_NT_WINDOWS32 + FILETYPE VFT_DLL + FILESUBTYPE VFT2_UNKNOWN +BEGIN + BLOCK "StringFileInfo" + BEGIN + BLOCK "040904B0" + BEGIN + VALUE "CompanyName", "Yann Collet" + VALUE "FileDescription", "Extremely fast compression" + VALUE "FileVersion", LZ4_VERSION_STRING + VALUE "InternalName", "lz4.exe" + VALUE "LegalCopyright", "Copyright (C) 2013-2016, Yann Collet" + VALUE "OriginalFilename", "lz4.exe" + VALUE "ProductName", "LZ4" + VALUE "ProductVersion", LZ4_VERSION_STRING + END + END + BLOCK "VarFileInfo" + BEGIN + VALUE "Translation", 0x0409, 1200 + END +END + +#endif diff --git a/build/VS2010/lz4/lz4.vcxproj b/build/VS2010/lz4/lz4.vcxproj new file mode 100644 index 0000000..de7a714 --- /dev/null +++ b/build/VS2010/lz4/lz4.vcxproj @@ -0,0 +1,189 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {E30329AC-0057-4FE0-8FDA-7F650D398C4C} + Win32Proj + lz4 + $(SolutionDir)bin\$(Platform)_$(Configuration)\ + $(SolutionDir)bin\obj\$(RootNamespace)_$(Platform)_$(Configuration)\ + + + + Application + true + Unicode + + + Application + true + Unicode + + + Application + false + Unicode + true + + + Application + false + Unicode + true + + + + + + + + + + + + + + + + + + + true + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + + + true + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + true + + + false + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + + + false + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + true + + + + + + Level4 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + false + MultiThreadedDebug + + + Console + true + setargv.obj;%(AdditionalDependencies) + + + + + + + Level4 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + true + /analyze:stacksize295252 %(AdditionalOptions) + MultiThreadedDebug + + + Console + true + setargv.obj;%(AdditionalDependencies) + + + + + Level4 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + false + false + MultiThreaded + + + Console + true + true + true + setargv.obj;%(AdditionalDependencies) + + + + + Level4 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + false + true + /analyze:stacksize295252 %(AdditionalOptions) + MultiThreaded + + + Console + true + true + true + setargv.obj;%(AdditionalDependencies) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/build/VS2017/datagen/datagen.vcxproj b/build/VS2017/datagen/datagen.vcxproj new file mode 100644 index 0000000..30e159e --- /dev/null +++ b/build/VS2017/datagen/datagen.vcxproj @@ -0,0 +1,173 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {D745AE2F-596A-403A-9B91-81A8C6779243} + Win32Proj + datagen + $(SolutionDir)bin\$(Platform)_$(Configuration)\ + $(SolutionDir)bin\obj\$(RootNamespace)_$(Platform)_$(Configuration)\ + + + + Application + true + Unicode + v141 + + + Application + true + Unicode + v141 + + + Application + false + Unicode + true + v141 + + + Application + false + Unicode + true + v141 + + + + + + + + + + + + + + + + + + + true + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\programs;$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + + + true + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\programs;$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + true + + + false + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\programs;$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + + + false + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\programs;$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + true + + + + + + Level4 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + false + MultiThreadedDebug + + + Console + true + + + + + + + Level4 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + true + /analyze:stacksize295252 %(AdditionalOptions) + MultiThreadedDebug + + + Console + true + + + + + Level4 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + false + false + MultiThreaded + + + Console + true + true + true + + + + + Level4 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + false + true + /analyze:stacksize295252 %(AdditionalOptions) + MultiThreaded + + + Console + true + true + true + + + + + + + + + + + + + \ No newline at end of file diff --git a/build/VS2017/frametest/frametest.vcxproj b/build/VS2017/frametest/frametest.vcxproj new file mode 100644 index 0000000..a3a403d --- /dev/null +++ b/build/VS2017/frametest/frametest.vcxproj @@ -0,0 +1,180 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {39AD6ECC-8BAD-4368-95E4-A1AA2F077BB7} + Win32Proj + frametest + $(SolutionDir)bin\$(Platform)_$(Configuration)\ + $(SolutionDir)bin\obj\$(RootNamespace)_$(Platform)_$(Configuration)\ + + + + Application + true + Unicode + v141 + + + Application + true + Unicode + v141 + + + Application + false + Unicode + true + v141 + + + Application + false + Unicode + true + v141 + + + + + + + + + + + + + + + + + + + true + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + + + true + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + true + + + false + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + + + false + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + true + + + + + + Level4 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + false + MultiThreadedDebug + + + Console + true + + + + + + + Level4 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + true + /analyze:stacksize295252 %(AdditionalOptions) + MultiThreadedDebug + + + Console + true + + + + + Level4 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + false + false + MultiThreaded + + + Console + true + true + true + + + + + Level4 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + false + true + /analyze:stacksize295252 %(AdditionalOptions) + MultiThreaded + + + Console + true + true + true + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/build/VS2017/fullbench-dll/fullbench-dll.vcxproj b/build/VS2017/fullbench-dll/fullbench-dll.vcxproj new file mode 100644 index 0000000..d54a8d7 --- /dev/null +++ b/build/VS2017/fullbench-dll/fullbench-dll.vcxproj @@ -0,0 +1,184 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {13992FD2-077E-4954-B065-A428198201A9} + Win32Proj + fullbench-dll + $(SolutionDir)bin\$(Platform)_$(Configuration)\ + $(SolutionDir)bin\obj\$(RootNamespace)_$(Platform)_$(Configuration)\ + + + + Application + true + Unicode + v141 + + + Application + true + Unicode + v141 + + + Application + false + Unicode + true + v141 + + + Application + false + Unicode + true + v141 + + + + + + + + + + + + + + + + + + + true + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + + + true + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + true + + + false + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + + + false + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + true + + + + + + Level4 + Disabled + WIN32;_DEBUG;_CONSOLE;LZ4_DLL_IMPORT=1;%(PreprocessorDefinitions) + true + false + MultiThreadedDebug + + + Console + true + $(SolutionDir)bin\$(Platform)_$(Configuration);%(AdditionalLibraryDirectories) + liblz4.lib;%(AdditionalDependencies) + + + + + + + Level4 + Disabled + WIN32;_DEBUG;_CONSOLE;LZ4_DLL_IMPORT=1;%(PreprocessorDefinitions) + true + true + /analyze:stacksize295252 %(AdditionalOptions) + MultiThreadedDebug + + + Console + true + $(SolutionDir)bin\$(Platform)_$(Configuration);%(AdditionalLibraryDirectories) + liblz4.lib;%(AdditionalDependencies) + + + + + Level4 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;LZ4_DLL_IMPORT=1;%(PreprocessorDefinitions) + false + false + MultiThreaded + + + Console + true + true + true + $(SolutionDir)bin\$(Platform)_$(Configuration);%(AdditionalLibraryDirectories) + liblz4.lib;%(AdditionalDependencies) + + + + + Level4 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;LZ4_DLL_IMPORT=1;%(PreprocessorDefinitions) + false + true + /analyze:stacksize295252 %(AdditionalOptions) + MultiThreaded + + + Console + true + true + true + $(SolutionDir)bin\$(Platform)_$(Configuration);%(AdditionalLibraryDirectories) + liblz4.lib;%(AdditionalDependencies) + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/build/VS2017/fullbench/fullbench.vcxproj b/build/VS2017/fullbench/fullbench.vcxproj new file mode 100644 index 0000000..54c9743 --- /dev/null +++ b/build/VS2017/fullbench/fullbench.vcxproj @@ -0,0 +1,180 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {6A4DF4EF-C77F-43C6-8901-DDCD20879E4E} + Win32Proj + fullbench + $(SolutionDir)bin\$(Platform)_$(Configuration)\ + $(SolutionDir)bin\obj\$(RootNamespace)_$(Platform)_$(Configuration)\ + + + + Application + true + Unicode + v141 + + + Application + true + Unicode + v141 + + + Application + false + Unicode + true + v141 + + + Application + false + Unicode + true + v141 + + + + + + + + + + + + + + + + + + + true + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + + + true + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + true + + + false + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + + + false + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + true + + + + + + Level4 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + false + MultiThreadedDebug + + + Console + true + + + + + + + Level4 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + true + /analyze:stacksize295252 %(AdditionalOptions) + MultiThreadedDebug + + + Console + true + + + + + Level4 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + false + false + MultiThreaded + + + Console + true + true + true + + + + + Level4 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + false + true + /analyze:stacksize295252 %(AdditionalOptions) + MultiThreaded + + + Console + true + true + true + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/build/VS2017/fuzzer/fuzzer.vcxproj b/build/VS2017/fuzzer/fuzzer.vcxproj new file mode 100644 index 0000000..aa6fe42 --- /dev/null +++ b/build/VS2017/fuzzer/fuzzer.vcxproj @@ -0,0 +1,177 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {18B9F1A7-9C66-4352-898B-30804DADE0FD} + Win32Proj + fuzzer + $(SolutionDir)bin\$(Platform)_$(Configuration)\ + $(SolutionDir)bin\obj\$(RootNamespace)_$(Platform)_$(Configuration)\ + + + + Application + true + Unicode + v141 + + + Application + true + Unicode + v141 + + + Application + false + Unicode + true + v141 + + + Application + false + Unicode + true + v141 + + + + + + + + + + + + + + + + + + + true + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + + + true + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + true + + + false + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + + + false + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + true + + + + + + Level4 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + false + MultiThreadedDebug + + + Console + true + + + + + + + Level4 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + true + /analyze:stacksize295252 %(AdditionalOptions) + MultiThreadedDebug + + + Console + true + + + + + Level4 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + false + false + MultiThreaded + + + Console + true + true + true + + + + + Level4 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + false + true + /analyze:stacksize295252 %(AdditionalOptions) + MultiThreaded + + + Console + true + true + true + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/build/VS2017/liblz4-dll/liblz4-dll.rc b/build/VS2017/liblz4-dll/liblz4-dll.rc new file mode 100644 index 0000000..b1871fe --- /dev/null +++ b/build/VS2017/liblz4-dll/liblz4-dll.rc @@ -0,0 +1,51 @@ +// Microsoft Visual C++ generated resource script. +// + +#include "lz4.h" /* LZ4_VERSION_STRING */ +#define APSTUDIO_READONLY_SYMBOLS +#include "verrsrc.h" +#undef APSTUDIO_READONLY_SYMBOLS + + +#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU) +LANGUAGE 9, 1 + +///////////////////////////////////////////////////////////////////////////// +// +// Version +// + +VS_VERSION_INFO VERSIONINFO + FILEVERSION LZ4_VERSION_MAJOR,LZ4_VERSION_MINOR,LZ4_VERSION_RELEASE,0 + PRODUCTVERSION LZ4_VERSION_MAJOR,LZ4_VERSION_MINOR,LZ4_VERSION_RELEASE,0 + FILEFLAGSMASK VS_FFI_FILEFLAGSMASK +#ifdef _DEBUG + FILEFLAGS VS_FF_DEBUG +#else + FILEFLAGS 0x0L +#endif + FILEOS VOS_NT_WINDOWS32 + FILETYPE VFT_DLL + FILESUBTYPE VFT2_UNKNOWN +BEGIN + BLOCK "StringFileInfo" + BEGIN + BLOCK "040904B0" + BEGIN + VALUE "CompanyName", "Yann Collet" + VALUE "FileDescription", "Extremely fast compression" + VALUE "FileVersion", LZ4_VERSION_STRING + VALUE "InternalName", "lz4.dll" + VALUE "LegalCopyright", "Copyright (C) 2013-2016, Yann Collet" + VALUE "OriginalFilename", "lz4.dll" + VALUE "ProductName", "LZ4" + VALUE "ProductVersion", LZ4_VERSION_STRING + END + END + BLOCK "VarFileInfo" + BEGIN + VALUE "Translation", 0x0409, 1200 + END +END + +#endif diff --git a/build/VS2017/liblz4-dll/liblz4-dll.vcxproj b/build/VS2017/liblz4-dll/liblz4-dll.vcxproj new file mode 100644 index 0000000..8e7ee3b --- /dev/null +++ b/build/VS2017/liblz4-dll/liblz4-dll.vcxproj @@ -0,0 +1,183 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {9800039D-4AAA-43A4-BB78-FEF6F4836927} + Win32Proj + liblz4-dll + $(SolutionDir)bin\$(Platform)_$(Configuration)\ + $(SolutionDir)bin\obj\$(RootNamespace)_$(Platform)_$(Configuration)\ + liblz4-dll + + + + DynamicLibrary + true + Unicode + v141 + + + DynamicLibrary + true + Unicode + v141 + + + DynamicLibrary + false + Unicode + true + v141 + + + DynamicLibrary + false + Unicode + true + v141 + + + + + + + + + + + + + + + + + + + true + liblz4 + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + + + true + liblz4 + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + true + + + false + liblz4 + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + + + false + liblz4 + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + true + + + + + + Level4 + Disabled + WIN32;_DEBUG;LZ4_DLL_EXPORT=1;%(PreprocessorDefinitions) + true + false + MultiThreadedDebug + + + true + + + + + + + Level4 + Disabled + WIN32;_DEBUG;LZ4_DLL_EXPORT=1;%(PreprocessorDefinitions) + true + true + /analyze:stacksize295252 %(AdditionalOptions) + MultiThreadedDebug + + + true + + + + + Level4 + + + MaxSpeed + true + true + WIN32;NDEBUG;LZ4_DLL_EXPORT=1;%(PreprocessorDefinitions) + false + false + MultiThreaded + + + true + true + true + + + + + Level4 + + + MaxSpeed + true + true + WIN32;NDEBUG;LZ4_DLL_EXPORT=1;%(PreprocessorDefinitions) + false + true + /analyze:stacksize295252 %(AdditionalOptions) + MultiThreaded + + + true + true + true + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/build/VS2017/liblz4/liblz4.vcxproj b/build/VS2017/liblz4/liblz4.vcxproj new file mode 100644 index 0000000..948f7db --- /dev/null +++ b/build/VS2017/liblz4/liblz4.vcxproj @@ -0,0 +1,179 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {9092C5CC-3E71-41B3-BF68-4A7BDD8A5476} + Win32Proj + liblz4 + $(SolutionDir)bin\$(Platform)_$(Configuration)\ + $(SolutionDir)bin\obj\$(RootNamespace)_$(Platform)_$(Configuration)\ + + + + StaticLibrary + true + Unicode + v141 + + + StaticLibrary + true + Unicode + v141 + + + StaticLibrary + false + Unicode + true + v141 + + + StaticLibrary + false + Unicode + true + v141 + + + + + + + + + + + + + + + + + + + true + liblz4_static + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + + + true + liblz4_static + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + true + + + false + liblz4_static + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + + + false + liblz4_static + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + true + + + + + + Level4 + Disabled + WIN32;_DEBUG;LZ4_DLL_EXPORT=1;%(PreprocessorDefinitions) + true + false + MultiThreadedDebug + + + true + + + + + + + Level4 + Disabled + WIN32;_DEBUG;LZ4_DLL_EXPORT=1;%(PreprocessorDefinitions) + true + true + /analyze:stacksize295252 %(AdditionalOptions) + MultiThreadedDebug + + + true + + + + + Level4 + + + MaxSpeed + true + true + WIN32;NDEBUG;LZ4_DLL_EXPORT=1;%(PreprocessorDefinitions) + false + false + MultiThreaded + + + true + true + true + + + + + Level4 + + + MaxSpeed + true + true + WIN32;NDEBUG;LZ4_DLL_EXPORT=1;%(PreprocessorDefinitions) + false + true + /analyze:stacksize295252 %(AdditionalOptions) + MultiThreaded + + + true + true + true + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/build/VS2017/lz4.sln b/build/VS2017/lz4.sln new file mode 100644 index 0000000..6a2779f --- /dev/null +++ b/build/VS2017/lz4.sln @@ -0,0 +1,103 @@ +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28307.271 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "liblz4-dll", "liblz4-dll\liblz4-dll.vcxproj", "{9800039D-4AAA-43A4-BB78-FEF6F4836927}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "liblz4", "liblz4\liblz4.vcxproj", "{9092C5CC-3E71-41B3-BF68-4A7BDD8A5476}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "fuzzer", "fuzzer\fuzzer.vcxproj", "{18B9F1A7-9C66-4352-898B-30804DADE0FD}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "fullbench", "fullbench\fullbench.vcxproj", "{6A4DF4EF-C77F-43C6-8901-DDCD20879E4E}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "frametest", "frametest\frametest.vcxproj", "{39AD6ECC-8BAD-4368-95E4-A1AA2F077BB7}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "datagen", "datagen\datagen.vcxproj", "{D745AE2F-596A-403A-9B91-81A8C6779243}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "fullbench-dll", "fullbench-dll\fullbench-dll.vcxproj", "{13992FD2-077E-4954-B065-A428198201A9}" + ProjectSection(ProjectDependencies) = postProject + {9800039D-4AAA-43A4-BB78-FEF6F4836927} = {9800039D-4AAA-43A4-BB78-FEF6F4836927} + EndProjectSection +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "lz4", "lz4\lz4.vcxproj", "{60A3115E-B988-41EE-8815-F4D4F253D866}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Debug|x64 = Debug|x64 + Release|Win32 = Release|Win32 + Release|x64 = Release|x64 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {9800039D-4AAA-43A4-BB78-FEF6F4836927}.Debug|Win32.ActiveCfg = Debug|Win32 + {9800039D-4AAA-43A4-BB78-FEF6F4836927}.Debug|Win32.Build.0 = Debug|Win32 + {9800039D-4AAA-43A4-BB78-FEF6F4836927}.Debug|x64.ActiveCfg = Debug|x64 + {9800039D-4AAA-43A4-BB78-FEF6F4836927}.Debug|x64.Build.0 = Debug|x64 + {9800039D-4AAA-43A4-BB78-FEF6F4836927}.Release|Win32.ActiveCfg = Release|Win32 + {9800039D-4AAA-43A4-BB78-FEF6F4836927}.Release|Win32.Build.0 = Release|Win32 + {9800039D-4AAA-43A4-BB78-FEF6F4836927}.Release|x64.ActiveCfg = Release|x64 + {9800039D-4AAA-43A4-BB78-FEF6F4836927}.Release|x64.Build.0 = Release|x64 + {9092C5CC-3E71-41B3-BF68-4A7BDD8A5476}.Debug|Win32.ActiveCfg = Debug|Win32 + {9092C5CC-3E71-41B3-BF68-4A7BDD8A5476}.Debug|Win32.Build.0 = Debug|Win32 + {9092C5CC-3E71-41B3-BF68-4A7BDD8A5476}.Debug|x64.ActiveCfg = Debug|x64 + {9092C5CC-3E71-41B3-BF68-4A7BDD8A5476}.Debug|x64.Build.0 = Debug|x64 + {9092C5CC-3E71-41B3-BF68-4A7BDD8A5476}.Release|Win32.ActiveCfg = Release|Win32 + {9092C5CC-3E71-41B3-BF68-4A7BDD8A5476}.Release|Win32.Build.0 = Release|Win32 + {9092C5CC-3E71-41B3-BF68-4A7BDD8A5476}.Release|x64.ActiveCfg = Release|x64 + {9092C5CC-3E71-41B3-BF68-4A7BDD8A5476}.Release|x64.Build.0 = Release|x64 + {18B9F1A7-9C66-4352-898B-30804DADE0FD}.Debug|Win32.ActiveCfg = Debug|Win32 + {18B9F1A7-9C66-4352-898B-30804DADE0FD}.Debug|Win32.Build.0 = Debug|Win32 + {18B9F1A7-9C66-4352-898B-30804DADE0FD}.Debug|x64.ActiveCfg = Debug|x64 + {18B9F1A7-9C66-4352-898B-30804DADE0FD}.Debug|x64.Build.0 = Debug|x64 + {18B9F1A7-9C66-4352-898B-30804DADE0FD}.Release|Win32.ActiveCfg = Release|Win32 + {18B9F1A7-9C66-4352-898B-30804DADE0FD}.Release|Win32.Build.0 = Release|Win32 + {18B9F1A7-9C66-4352-898B-30804DADE0FD}.Release|x64.ActiveCfg = Release|x64 + {18B9F1A7-9C66-4352-898B-30804DADE0FD}.Release|x64.Build.0 = Release|x64 + {6A4DF4EF-C77F-43C6-8901-DDCD20879E4E}.Debug|Win32.ActiveCfg = Debug|Win32 + {6A4DF4EF-C77F-43C6-8901-DDCD20879E4E}.Debug|Win32.Build.0 = Debug|Win32 + {6A4DF4EF-C77F-43C6-8901-DDCD20879E4E}.Debug|x64.ActiveCfg = Debug|x64 + {6A4DF4EF-C77F-43C6-8901-DDCD20879E4E}.Debug|x64.Build.0 = Debug|x64 + {6A4DF4EF-C77F-43C6-8901-DDCD20879E4E}.Release|Win32.ActiveCfg = Release|Win32 + {6A4DF4EF-C77F-43C6-8901-DDCD20879E4E}.Release|Win32.Build.0 = Release|Win32 + {6A4DF4EF-C77F-43C6-8901-DDCD20879E4E}.Release|x64.ActiveCfg = Release|x64 + {6A4DF4EF-C77F-43C6-8901-DDCD20879E4E}.Release|x64.Build.0 = Release|x64 + {39AD6ECC-8BAD-4368-95E4-A1AA2F077BB7}.Debug|Win32.ActiveCfg = Debug|Win32 + {39AD6ECC-8BAD-4368-95E4-A1AA2F077BB7}.Debug|Win32.Build.0 = Debug|Win32 + {39AD6ECC-8BAD-4368-95E4-A1AA2F077BB7}.Debug|x64.ActiveCfg = Debug|x64 + {39AD6ECC-8BAD-4368-95E4-A1AA2F077BB7}.Debug|x64.Build.0 = Debug|x64 + {39AD6ECC-8BAD-4368-95E4-A1AA2F077BB7}.Release|Win32.ActiveCfg = Release|Win32 + {39AD6ECC-8BAD-4368-95E4-A1AA2F077BB7}.Release|Win32.Build.0 = Release|Win32 + {39AD6ECC-8BAD-4368-95E4-A1AA2F077BB7}.Release|x64.ActiveCfg = Release|x64 + {39AD6ECC-8BAD-4368-95E4-A1AA2F077BB7}.Release|x64.Build.0 = Release|x64 + {D745AE2F-596A-403A-9B91-81A8C6779243}.Debug|Win32.ActiveCfg = Debug|Win32 + {D745AE2F-596A-403A-9B91-81A8C6779243}.Debug|Win32.Build.0 = Debug|Win32 + {D745AE2F-596A-403A-9B91-81A8C6779243}.Debug|x64.ActiveCfg = Debug|x64 + {D745AE2F-596A-403A-9B91-81A8C6779243}.Debug|x64.Build.0 = Debug|x64 + {D745AE2F-596A-403A-9B91-81A8C6779243}.Release|Win32.ActiveCfg = Release|Win32 + {D745AE2F-596A-403A-9B91-81A8C6779243}.Release|Win32.Build.0 = Release|Win32 + {D745AE2F-596A-403A-9B91-81A8C6779243}.Release|x64.ActiveCfg = Release|x64 + {D745AE2F-596A-403A-9B91-81A8C6779243}.Release|x64.Build.0 = Release|x64 + {13992FD2-077E-4954-B065-A428198201A9}.Debug|Win32.ActiveCfg = Debug|Win32 + {13992FD2-077E-4954-B065-A428198201A9}.Debug|Win32.Build.0 = Debug|Win32 + {13992FD2-077E-4954-B065-A428198201A9}.Debug|x64.ActiveCfg = Debug|x64 + {13992FD2-077E-4954-B065-A428198201A9}.Debug|x64.Build.0 = Debug|x64 + {13992FD2-077E-4954-B065-A428198201A9}.Release|Win32.ActiveCfg = Release|Win32 + {13992FD2-077E-4954-B065-A428198201A9}.Release|Win32.Build.0 = Release|Win32 + {13992FD2-077E-4954-B065-A428198201A9}.Release|x64.ActiveCfg = Release|x64 + {13992FD2-077E-4954-B065-A428198201A9}.Release|x64.Build.0 = Release|x64 + {60A3115E-B988-41EE-8815-F4D4F253D866}.Debug|Win32.ActiveCfg = Debug|Win32 + {60A3115E-B988-41EE-8815-F4D4F253D866}.Debug|Win32.Build.0 = Debug|Win32 + {60A3115E-B988-41EE-8815-F4D4F253D866}.Debug|x64.ActiveCfg = Debug|x64 + {60A3115E-B988-41EE-8815-F4D4F253D866}.Debug|x64.Build.0 = Debug|x64 + {60A3115E-B988-41EE-8815-F4D4F253D866}.Release|Win32.ActiveCfg = Release|Win32 + {60A3115E-B988-41EE-8815-F4D4F253D866}.Release|Win32.Build.0 = Release|Win32 + {60A3115E-B988-41EE-8815-F4D4F253D866}.Release|x64.ActiveCfg = Release|x64 + {60A3115E-B988-41EE-8815-F4D4F253D866}.Release|x64.Build.0 = Release|x64 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {BBC259B2-BABF-47CD-8A6A-7B8318A803AC} + EndGlobalSection +EndGlobal diff --git a/build/VS2017/lz4/lz4.rc b/build/VS2017/lz4/lz4.rc new file mode 100644 index 0000000..c593edf --- /dev/null +++ b/build/VS2017/lz4/lz4.rc @@ -0,0 +1,51 @@ +// Microsoft Visual C++ generated resource script. +// + +#include "lz4.h" /* LZ4_VERSION_STRING */ +#define APSTUDIO_READONLY_SYMBOLS +#include "verrsrc.h" +#undef APSTUDIO_READONLY_SYMBOLS + + +#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU) +LANGUAGE 9, 1 + +///////////////////////////////////////////////////////////////////////////// +// +// Version +// + +VS_VERSION_INFO VERSIONINFO + FILEVERSION LZ4_VERSION_MAJOR,LZ4_VERSION_MINOR,LZ4_VERSION_RELEASE,0 + PRODUCTVERSION LZ4_VERSION_MAJOR,LZ4_VERSION_MINOR,LZ4_VERSION_RELEASE,0 + FILEFLAGSMASK VS_FFI_FILEFLAGSMASK +#ifdef _DEBUG + FILEFLAGS VS_FF_DEBUG +#else + FILEFLAGS 0x0L +#endif + FILEOS VOS_NT_WINDOWS32 + FILETYPE VFT_DLL + FILESUBTYPE VFT2_UNKNOWN +BEGIN + BLOCK "StringFileInfo" + BEGIN + BLOCK "040904B0" + BEGIN + VALUE "CompanyName", "Yann Collet" + VALUE "FileDescription", "Extremely fast compression" + VALUE "FileVersion", LZ4_VERSION_STRING + VALUE "InternalName", "lz4.exe" + VALUE "LegalCopyright", "Copyright (C) 2013-2016, Yann Collet" + VALUE "OriginalFilename", "lz4.exe" + VALUE "ProductName", "LZ4" + VALUE "ProductVersion", LZ4_VERSION_STRING + END + END + BLOCK "VarFileInfo" + BEGIN + VALUE "Translation", 0x0409, 1200 + END +END + +#endif diff --git a/build/VS2017/lz4/lz4.vcxproj b/build/VS2017/lz4/lz4.vcxproj new file mode 100644 index 0000000..b4fed24 --- /dev/null +++ b/build/VS2017/lz4/lz4.vcxproj @@ -0,0 +1,164 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 15.0 + {60A3115E-B988-41EE-8815-F4D4F253D866} + lz4 + 8.1 + + + + Application + true + v141 + Unicode + + + Application + false + v141 + false + Unicode + + + Application + true + v141 + MultiByte + + + Application + false + v141 + true + MultiByte + + + + + + + + + + + + + + + + + + + + + $(SolutionDir)bin\$(Platform)_$(Configuration)\ + $(SolutionDir)bin\obj\$(RootNamespace)_$(Platform)_$(Configuration)\ + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + + + $(SolutionDir)bin\$(Platform)_$(Configuration)\ + $(SolutionDir)bin\obj\$(RootNamespace)_$(Platform)_$(Configuration)\ + $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + false + + + + Level4 + Disabled + true + true + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + + + true + Console + false + false + + + + + Level3 + Disabled + true + true + + + + + Level3 + MaxSpeed + true + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + + + true + true + true + Console + + + + + Level3 + MaxSpeed + true + true + true + true + + + true + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/build/cmake/.gitignore b/build/cmake/.gitignore new file mode 100644 index 0000000..d39505d --- /dev/null +++ b/build/cmake/.gitignore @@ -0,0 +1,9 @@ +# cmake artefact + +CMakeCache.txt +CMakeFiles +*.cmake +Makefile +liblz4.pc +lz4c +install_manifest.txt diff --git a/build/cmake/CMakeLists.txt b/build/cmake/CMakeLists.txt new file mode 100644 index 0000000..42d92ea --- /dev/null +++ b/build/cmake/CMakeLists.txt @@ -0,0 +1,228 @@ +# CMake support for LZ4 +# +# To the extent possible under law, the author(s) have dedicated all +# copyright and related and neighboring rights to this software to +# the public domain worldwide. This software is distributed without +# any warranty. +# +# For details, see . +# +# LZ4's CMake support is maintained by Evan Nemerson; when filing +# bugs please mention @nemequ to make sure I see it. + +set(LZ4_TOP_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../..") + +option(LZ4_BUILD_LEGACY_LZ4C "Build lz4c progam with legacy argument support" ON) + +# Parse version information +file(STRINGS "${LZ4_TOP_SOURCE_DIR}/lib/lz4.h" LZ4_VERSION_MAJOR REGEX "^#define LZ4_VERSION_MAJOR +([0-9]+) +.*$") +string(REGEX REPLACE "^#define LZ4_VERSION_MAJOR +([0-9]+) +.*$" "\\1" LZ4_VERSION_MAJOR "${LZ4_VERSION_MAJOR}") +file(STRINGS "${LZ4_TOP_SOURCE_DIR}/lib/lz4.h" LZ4_VERSION_MINOR REGEX "^#define LZ4_VERSION_MINOR +([0-9]+) +.*$") +string(REGEX REPLACE "^#define LZ4_VERSION_MINOR +([0-9]+) +.*$" "\\1" LZ4_VERSION_MINOR "${LZ4_VERSION_MINOR}") +file(STRINGS "${LZ4_TOP_SOURCE_DIR}/lib/lz4.h" LZ4_VERSION_RELEASE REGEX "^#define LZ4_VERSION_RELEASE +([0-9]+) +.*$") +string(REGEX REPLACE "^#define LZ4_VERSION_RELEASE +([0-9]+) +.*$" "\\1" LZ4_VERSION_RELEASE "${LZ4_VERSION_RELEASE}") +set(LZ4_VERSION_STRING "${LZ4_VERSION_MAJOR}.${LZ4_VERSION_MINOR}.${LZ4_VERSION_RELEASE}") +mark_as_advanced(LZ4_VERSION_STRING LZ4_VERSION_MAJOR LZ4_VERSION_MINOR LZ4_VERSION_RELEASE) + +if("${CMAKE_VERSION}" VERSION_LESS "3.0") + project(LZ4 C) +else() + cmake_policy (SET CMP0048 NEW) + project(LZ4 + VERSION ${LZ4_VERSION_STRING} + LANGUAGES C) +endif() + +cmake_minimum_required (VERSION 2.8.6) + +# If LZ4 is being bundled in another project, we don't want to +# install anything. However, we want to let people override this, so +# we'll use the LZ4_BUNDLED_MODE variable to let them do that; just +# set it to OFF in your project before you add_subdirectory(lz4/contrib/cmake_unofficial). +get_directory_property(LZ4_PARENT_DIRECTORY PARENT_DIRECTORY) +if("${LZ4_BUNDLED_MODE}" STREQUAL "") + # Bundled mode hasn't been set one way or the other, set the default + # depending on whether or not we are the top-level project. + if("${LZ4_PARENT_DIRECTORY}" STREQUAL "") + set(LZ4_BUNDLED_MODE OFF) + else() + set(LZ4_BUNDLED_MODE ON) + endif() +endif() +mark_as_advanced(LZ4_BUNDLED_MODE) + +# CPack +if(NOT LZ4_BUNDLED_MODE AND NOT CPack_CMake_INCLUDED) + set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "LZ4 compression library") + set(CPACK_PACKAGE_DESCRIPTION_FILE "${LZ4_TOP_SOURCE_DIR}/README.md") + set(CPACK_RESOURCE_FILE_LICENSE "${LZ4_TOP_SOURCE_DIR}/LICENSE") + set(CPACK_PACKAGE_VERSION_MAJOR ${LZ4_VERSION_MAJOR}) + set(CPACK_PACKAGE_VERSION_MINOR ${LZ4_VERSION_MINOR}) + set(CPACK_PACKAGE_VERSION_PATCH ${LZ4_VERSION_RELEASE}) + include(CPack) +endif(NOT LZ4_BUNDLED_MODE AND NOT CPack_CMake_INCLUDED) + +# Allow people to choose whether to build shared or static libraries +# via the BUILD_SHARED_LIBS option unless we are in bundled mode, in +# which case we always use static libraries. +include(CMakeDependentOption) +CMAKE_DEPENDENT_OPTION(BUILD_SHARED_LIBS "Build shared libraries" ON "NOT LZ4_BUNDLED_MODE" OFF) +CMAKE_DEPENDENT_OPTION(BUILD_STATIC_LIBS "Build static libraries" OFF "BUILD_SHARED_LIBS" ON) + +if(NOT BUILD_SHARED_LIBS AND NOT BUILD_STATIC_LIBS) + message(FATAL_ERROR "Both BUILD_SHARED_LIBS and BUILD_STATIC_LIBS have been disabled") +endif() + +set(LZ4_LIB_SOURCE_DIR "${LZ4_TOP_SOURCE_DIR}/lib") +set(LZ4_PROG_SOURCE_DIR "${LZ4_TOP_SOURCE_DIR}/programs") + +include_directories("${LZ4_LIB_SOURCE_DIR}") + +# CLI sources +set(LZ4_SOURCES + "${LZ4_LIB_SOURCE_DIR}/lz4.c" + "${LZ4_LIB_SOURCE_DIR}/lz4hc.c" + "${LZ4_LIB_SOURCE_DIR}/lz4.h" + "${LZ4_LIB_SOURCE_DIR}/lz4hc.h" + "${LZ4_LIB_SOURCE_DIR}/lz4frame.c" + "${LZ4_LIB_SOURCE_DIR}/lz4frame.h" + "${LZ4_LIB_SOURCE_DIR}/xxhash.c") +set(LZ4_CLI_SOURCES + "${LZ4_PROG_SOURCE_DIR}/bench.c" + "${LZ4_PROG_SOURCE_DIR}/lz4cli.c" + "${LZ4_PROG_SOURCE_DIR}/lz4io.c" + "${LZ4_PROG_SOURCE_DIR}/datagen.c") + +# Whether to use position independent code for the static library. If +# we're building a shared library this is ignored and PIC is always +# used. +option(LZ4_POSITION_INDEPENDENT_LIB "Use position independent code for static library (if applicable)" ON) + +# liblz4 +set(LZ4_LIBRARIES_BUILT) +if(BUILD_SHARED_LIBS) + add_library(lz4_shared SHARED ${LZ4_SOURCES}) + set_target_properties(lz4_shared PROPERTIES + OUTPUT_NAME lz4 + SOVERSION "${LZ4_VERSION_MAJOR}" + VERSION "${LZ4_VERSION_STRING}") + list(APPEND LZ4_LIBRARIES_BUILT lz4_shared) +endif() +if(BUILD_STATIC_LIBS) + add_library(lz4_static STATIC ${LZ4_SOURCES}) + set_target_properties(lz4_static PROPERTIES + OUTPUT_NAME lz4 + POSITION_INDEPENDENT_CODE ${LZ4_POSITION_INDEPENDENT_LIB}) + list(APPEND LZ4_LIBRARIES_BUILT lz4_static) +endif() + +# link to shared whenever possible, to static otherwise +if(BUILD_SHARED_LIBS) + set(LZ4_LINK_LIBRARY lz4_shared) +else() + set(LZ4_LINK_LIBRARY lz4_static) +endif() + +# lz4 +set(LZ4_PROGRAMS_BUILT lz4cli) +add_executable(lz4cli ${LZ4_CLI_SOURCES}) +set_target_properties(lz4cli PROPERTIES OUTPUT_NAME lz4) +target_link_libraries(lz4cli ${LZ4_LINK_LIBRARY}) + +# lz4c +if (LZ4_BUILD_LEGACY_LZ4C) + list(APPEND LZ4_PROGRAMS_BUILT lz4c) + add_executable(lz4c ${LZ4_CLI_SOURCES}) + set_target_properties(lz4c PROPERTIES COMPILE_DEFINITIONS "ENABLE_LZ4C_LEGACY_OPTIONS") + target_link_libraries(lz4c ${LZ4_LINK_LIBRARY}) +endif() + +# Extra warning flags +include (CheckCCompilerFlag) +foreach (flag + # GCC-style + -Wall + -Wextra + -Wundef + -Wcast-qual + -Wcast-align + -Wshadow + -Wswitch-enum + -Wdeclaration-after-statement + -Wstrict-prototypes + -Wpointer-arith + + # MSVC-style + /W4) + # Because https://gcc.gnu.org/wiki/FAQ#wnowarning + string(REGEX REPLACE "\\-Wno\\-(.+)" "-W\\1" flag_to_test "${flag}") + string(REGEX REPLACE "[^a-zA-Z0-9]+" "_" test_name "CFLAG_${flag_to_test}") + + check_c_compiler_flag("${ADD_COMPILER_FLAGS_PREPEND} ${flag_to_test}" ${test_name}) + + if(${test_name}) + set(CMAKE_C_FLAGS "${flag} ${CMAKE_C_FLAGS}") + endif() + + unset(test_name) + unset(flag_to_test) +endforeach (flag) + +if(NOT LZ4_BUNDLED_MODE) + include(GNUInstallDirs) + + install(TARGETS ${LZ4_PROGRAMS_BUILT} + BUNDLE DESTINATION "${CMAKE_INSTALL_BINDIR}" + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}") + install(TARGETS ${LZ4_LIBRARIES_BUILT} + LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" + ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}") + install(FILES + "${LZ4_LIB_SOURCE_DIR}/lz4.h" + "${LZ4_LIB_SOURCE_DIR}/lz4frame.h" + "${LZ4_LIB_SOURCE_DIR}/lz4hc.h" + DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") + install(FILES "${LZ4_PROG_SOURCE_DIR}/lz4.1" + DESTINATION "${CMAKE_INSTALL_MANDIR}/man1") + install(FILES "${CMAKE_CURRENT_BINARY_DIR}/liblz4.pc" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") + + # install lz4cat and unlz4 symlinks on *nix + if(UNIX) + install(CODE " + foreach(f lz4cat unlz4) + set(dest \"\$ENV{DESTDIR}${CMAKE_INSTALL_FULL_BINDIR}/\${f}\") + message(STATUS \"Symlinking: \${dest} -> lz4\") + execute_process( + COMMAND \"${CMAKE_COMMAND}\" -E create_symlink lz4 \"\${dest}\") + endforeach() + ") + + # create manpage aliases + foreach(f lz4cat unlz4) + file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/${f}.1" ".so man1/lz4.1\n") + install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${f}.1" + DESTINATION "${CMAKE_INSTALL_MANDIR}/man1") + endforeach() + endif(UNIX) +endif(NOT LZ4_BUNDLED_MODE) + +# pkg-config +set(PREFIX "${CMAKE_INSTALL_PREFIX}") + +if("${CMAKE_INSTALL_FULL_LIBDIR}" STREQUAL "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}") + set(LIBDIR "\${prefix}/${CMAKE_INSTALL_LIBDIR}") +else() + set(LIBDIR "${CMAKE_INSTALL_FULL_LIBDIR}") +endif() + +if("${CMAKE_INSTALL_FULL_INCLUDEDIR}" STREQUAL "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}") + set(INCLUDEDIR "\${prefix}/${CMAKE_INSTALL_INCLUDEDIR}") +else() + set(INCLUDEDIR "${CMAKE_INSTALL_FULL_INCLUDEDIR}") +endif() + +# for liblz4.pc substitution +set(VERSION ${LZ4_VERSION_STRING}) +configure_file(${LZ4_LIB_SOURCE_DIR}/liblz4.pc.in liblz4.pc @ONLY) diff --git a/contrib/cmake_unofficial/.gitignore b/contrib/cmake_unofficial/.gitignore deleted file mode 100644 index d39505d..0000000 --- a/contrib/cmake_unofficial/.gitignore +++ /dev/null @@ -1,9 +0,0 @@ -# cmake artefact - -CMakeCache.txt -CMakeFiles -*.cmake -Makefile -liblz4.pc -lz4c -install_manifest.txt diff --git a/contrib/cmake_unofficial/CMakeLists.txt b/contrib/cmake_unofficial/CMakeLists.txt deleted file mode 100644 index 42d92ea..0000000 --- a/contrib/cmake_unofficial/CMakeLists.txt +++ /dev/null @@ -1,228 +0,0 @@ -# CMake support for LZ4 -# -# To the extent possible under law, the author(s) have dedicated all -# copyright and related and neighboring rights to this software to -# the public domain worldwide. This software is distributed without -# any warranty. -# -# For details, see . -# -# LZ4's CMake support is maintained by Evan Nemerson; when filing -# bugs please mention @nemequ to make sure I see it. - -set(LZ4_TOP_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../..") - -option(LZ4_BUILD_LEGACY_LZ4C "Build lz4c progam with legacy argument support" ON) - -# Parse version information -file(STRINGS "${LZ4_TOP_SOURCE_DIR}/lib/lz4.h" LZ4_VERSION_MAJOR REGEX "^#define LZ4_VERSION_MAJOR +([0-9]+) +.*$") -string(REGEX REPLACE "^#define LZ4_VERSION_MAJOR +([0-9]+) +.*$" "\\1" LZ4_VERSION_MAJOR "${LZ4_VERSION_MAJOR}") -file(STRINGS "${LZ4_TOP_SOURCE_DIR}/lib/lz4.h" LZ4_VERSION_MINOR REGEX "^#define LZ4_VERSION_MINOR +([0-9]+) +.*$") -string(REGEX REPLACE "^#define LZ4_VERSION_MINOR +([0-9]+) +.*$" "\\1" LZ4_VERSION_MINOR "${LZ4_VERSION_MINOR}") -file(STRINGS "${LZ4_TOP_SOURCE_DIR}/lib/lz4.h" LZ4_VERSION_RELEASE REGEX "^#define LZ4_VERSION_RELEASE +([0-9]+) +.*$") -string(REGEX REPLACE "^#define LZ4_VERSION_RELEASE +([0-9]+) +.*$" "\\1" LZ4_VERSION_RELEASE "${LZ4_VERSION_RELEASE}") -set(LZ4_VERSION_STRING "${LZ4_VERSION_MAJOR}.${LZ4_VERSION_MINOR}.${LZ4_VERSION_RELEASE}") -mark_as_advanced(LZ4_VERSION_STRING LZ4_VERSION_MAJOR LZ4_VERSION_MINOR LZ4_VERSION_RELEASE) - -if("${CMAKE_VERSION}" VERSION_LESS "3.0") - project(LZ4 C) -else() - cmake_policy (SET CMP0048 NEW) - project(LZ4 - VERSION ${LZ4_VERSION_STRING} - LANGUAGES C) -endif() - -cmake_minimum_required (VERSION 2.8.6) - -# If LZ4 is being bundled in another project, we don't want to -# install anything. However, we want to let people override this, so -# we'll use the LZ4_BUNDLED_MODE variable to let them do that; just -# set it to OFF in your project before you add_subdirectory(lz4/contrib/cmake_unofficial). -get_directory_property(LZ4_PARENT_DIRECTORY PARENT_DIRECTORY) -if("${LZ4_BUNDLED_MODE}" STREQUAL "") - # Bundled mode hasn't been set one way or the other, set the default - # depending on whether or not we are the top-level project. - if("${LZ4_PARENT_DIRECTORY}" STREQUAL "") - set(LZ4_BUNDLED_MODE OFF) - else() - set(LZ4_BUNDLED_MODE ON) - endif() -endif() -mark_as_advanced(LZ4_BUNDLED_MODE) - -# CPack -if(NOT LZ4_BUNDLED_MODE AND NOT CPack_CMake_INCLUDED) - set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "LZ4 compression library") - set(CPACK_PACKAGE_DESCRIPTION_FILE "${LZ4_TOP_SOURCE_DIR}/README.md") - set(CPACK_RESOURCE_FILE_LICENSE "${LZ4_TOP_SOURCE_DIR}/LICENSE") - set(CPACK_PACKAGE_VERSION_MAJOR ${LZ4_VERSION_MAJOR}) - set(CPACK_PACKAGE_VERSION_MINOR ${LZ4_VERSION_MINOR}) - set(CPACK_PACKAGE_VERSION_PATCH ${LZ4_VERSION_RELEASE}) - include(CPack) -endif(NOT LZ4_BUNDLED_MODE AND NOT CPack_CMake_INCLUDED) - -# Allow people to choose whether to build shared or static libraries -# via the BUILD_SHARED_LIBS option unless we are in bundled mode, in -# which case we always use static libraries. -include(CMakeDependentOption) -CMAKE_DEPENDENT_OPTION(BUILD_SHARED_LIBS "Build shared libraries" ON "NOT LZ4_BUNDLED_MODE" OFF) -CMAKE_DEPENDENT_OPTION(BUILD_STATIC_LIBS "Build static libraries" OFF "BUILD_SHARED_LIBS" ON) - -if(NOT BUILD_SHARED_LIBS AND NOT BUILD_STATIC_LIBS) - message(FATAL_ERROR "Both BUILD_SHARED_LIBS and BUILD_STATIC_LIBS have been disabled") -endif() - -set(LZ4_LIB_SOURCE_DIR "${LZ4_TOP_SOURCE_DIR}/lib") -set(LZ4_PROG_SOURCE_DIR "${LZ4_TOP_SOURCE_DIR}/programs") - -include_directories("${LZ4_LIB_SOURCE_DIR}") - -# CLI sources -set(LZ4_SOURCES - "${LZ4_LIB_SOURCE_DIR}/lz4.c" - "${LZ4_LIB_SOURCE_DIR}/lz4hc.c" - "${LZ4_LIB_SOURCE_DIR}/lz4.h" - "${LZ4_LIB_SOURCE_DIR}/lz4hc.h" - "${LZ4_LIB_SOURCE_DIR}/lz4frame.c" - "${LZ4_LIB_SOURCE_DIR}/lz4frame.h" - "${LZ4_LIB_SOURCE_DIR}/xxhash.c") -set(LZ4_CLI_SOURCES - "${LZ4_PROG_SOURCE_DIR}/bench.c" - "${LZ4_PROG_SOURCE_DIR}/lz4cli.c" - "${LZ4_PROG_SOURCE_DIR}/lz4io.c" - "${LZ4_PROG_SOURCE_DIR}/datagen.c") - -# Whether to use position independent code for the static library. If -# we're building a shared library this is ignored and PIC is always -# used. -option(LZ4_POSITION_INDEPENDENT_LIB "Use position independent code for static library (if applicable)" ON) - -# liblz4 -set(LZ4_LIBRARIES_BUILT) -if(BUILD_SHARED_LIBS) - add_library(lz4_shared SHARED ${LZ4_SOURCES}) - set_target_properties(lz4_shared PROPERTIES - OUTPUT_NAME lz4 - SOVERSION "${LZ4_VERSION_MAJOR}" - VERSION "${LZ4_VERSION_STRING}") - list(APPEND LZ4_LIBRARIES_BUILT lz4_shared) -endif() -if(BUILD_STATIC_LIBS) - add_library(lz4_static STATIC ${LZ4_SOURCES}) - set_target_properties(lz4_static PROPERTIES - OUTPUT_NAME lz4 - POSITION_INDEPENDENT_CODE ${LZ4_POSITION_INDEPENDENT_LIB}) - list(APPEND LZ4_LIBRARIES_BUILT lz4_static) -endif() - -# link to shared whenever possible, to static otherwise -if(BUILD_SHARED_LIBS) - set(LZ4_LINK_LIBRARY lz4_shared) -else() - set(LZ4_LINK_LIBRARY lz4_static) -endif() - -# lz4 -set(LZ4_PROGRAMS_BUILT lz4cli) -add_executable(lz4cli ${LZ4_CLI_SOURCES}) -set_target_properties(lz4cli PROPERTIES OUTPUT_NAME lz4) -target_link_libraries(lz4cli ${LZ4_LINK_LIBRARY}) - -# lz4c -if (LZ4_BUILD_LEGACY_LZ4C) - list(APPEND LZ4_PROGRAMS_BUILT lz4c) - add_executable(lz4c ${LZ4_CLI_SOURCES}) - set_target_properties(lz4c PROPERTIES COMPILE_DEFINITIONS "ENABLE_LZ4C_LEGACY_OPTIONS") - target_link_libraries(lz4c ${LZ4_LINK_LIBRARY}) -endif() - -# Extra warning flags -include (CheckCCompilerFlag) -foreach (flag - # GCC-style - -Wall - -Wextra - -Wundef - -Wcast-qual - -Wcast-align - -Wshadow - -Wswitch-enum - -Wdeclaration-after-statement - -Wstrict-prototypes - -Wpointer-arith - - # MSVC-style - /W4) - # Because https://gcc.gnu.org/wiki/FAQ#wnowarning - string(REGEX REPLACE "\\-Wno\\-(.+)" "-W\\1" flag_to_test "${flag}") - string(REGEX REPLACE "[^a-zA-Z0-9]+" "_" test_name "CFLAG_${flag_to_test}") - - check_c_compiler_flag("${ADD_COMPILER_FLAGS_PREPEND} ${flag_to_test}" ${test_name}) - - if(${test_name}) - set(CMAKE_C_FLAGS "${flag} ${CMAKE_C_FLAGS}") - endif() - - unset(test_name) - unset(flag_to_test) -endforeach (flag) - -if(NOT LZ4_BUNDLED_MODE) - include(GNUInstallDirs) - - install(TARGETS ${LZ4_PROGRAMS_BUILT} - BUNDLE DESTINATION "${CMAKE_INSTALL_BINDIR}" - RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}") - install(TARGETS ${LZ4_LIBRARIES_BUILT} - LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" - ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" - RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}") - install(FILES - "${LZ4_LIB_SOURCE_DIR}/lz4.h" - "${LZ4_LIB_SOURCE_DIR}/lz4frame.h" - "${LZ4_LIB_SOURCE_DIR}/lz4hc.h" - DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") - install(FILES "${LZ4_PROG_SOURCE_DIR}/lz4.1" - DESTINATION "${CMAKE_INSTALL_MANDIR}/man1") - install(FILES "${CMAKE_CURRENT_BINARY_DIR}/liblz4.pc" - DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") - - # install lz4cat and unlz4 symlinks on *nix - if(UNIX) - install(CODE " - foreach(f lz4cat unlz4) - set(dest \"\$ENV{DESTDIR}${CMAKE_INSTALL_FULL_BINDIR}/\${f}\") - message(STATUS \"Symlinking: \${dest} -> lz4\") - execute_process( - COMMAND \"${CMAKE_COMMAND}\" -E create_symlink lz4 \"\${dest}\") - endforeach() - ") - - # create manpage aliases - foreach(f lz4cat unlz4) - file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/${f}.1" ".so man1/lz4.1\n") - install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${f}.1" - DESTINATION "${CMAKE_INSTALL_MANDIR}/man1") - endforeach() - endif(UNIX) -endif(NOT LZ4_BUNDLED_MODE) - -# pkg-config -set(PREFIX "${CMAKE_INSTALL_PREFIX}") - -if("${CMAKE_INSTALL_FULL_LIBDIR}" STREQUAL "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}") - set(LIBDIR "\${prefix}/${CMAKE_INSTALL_LIBDIR}") -else() - set(LIBDIR "${CMAKE_INSTALL_FULL_LIBDIR}") -endif() - -if("${CMAKE_INSTALL_FULL_INCLUDEDIR}" STREQUAL "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}") - set(INCLUDEDIR "\${prefix}/${CMAKE_INSTALL_INCLUDEDIR}") -else() - set(INCLUDEDIR "${CMAKE_INSTALL_FULL_INCLUDEDIR}") -endif() - -# for liblz4.pc substitution -set(VERSION ${LZ4_VERSION_STRING}) -configure_file(${LZ4_LIB_SOURCE_DIR}/liblz4.pc.in liblz4.pc @ONLY) diff --git a/contrib/debian/rules b/contrib/debian/rules index 748e68d..c897bc5 100755 --- a/contrib/debian/rules +++ b/contrib/debian/rules @@ -4,5 +4,4 @@ include /usr/share/cdbs/1/rules/debhelper.mk include /usr/share/cdbs/1/class/cmake.mk -DEB_CMAKE_EXTRA_FLAGS := -DCMAKE_BUILD_TYPE=RelWithDebInfo ../cmake_unofficial - +DEB_CMAKE_EXTRA_FLAGS := -DCMAKE_BUILD_TYPE=RelWithDebInfo ../../build/cmake diff --git a/visual/.gitignore b/visual/.gitignore deleted file mode 100644 index 69e1111..0000000 --- a/visual/.gitignore +++ /dev/null @@ -1,16 +0,0 @@ -# Visual C++ -.vs/ -*Copy -*.db -*.opensdf -*.sdf -*.suo -*.user -ver*/ -VS2010/bin/ -VS2017/bin/ -ipch - -# Fixup for lz4 project directories -!VS2010/lz4 -!VS2017/lz4 diff --git a/visual/README.md b/visual/README.md deleted file mode 100644 index 216971f..0000000 --- a/visual/README.md +++ /dev/null @@ -1,53 +0,0 @@ -Projects for various integrated development environments (IDE) -============================================================== - -#### Included projects - -The following projects are included with the lz4 distribution: -- `VS2010` - Visual Studio 2010 project (which also works well with Visual Studio 2012, 2013, 2015) - - -#### How to compile lz4 with Visual Studio - -1. Install Visual Studio e.g. VS 2015 Community Edition (it's free). -2. Download the latest version of lz4 from https://github.com/lz4/lz4/releases -3. Decompress ZIP archive. -4. Go to decompressed directory then to `visual` then `VS2010` and open `lz4.sln` -5. Visual Studio will ask about converting VS2010 project to VS2015 and you should agree. -6. Change `Debug` to `Release` and if you have 64-bit Windows change also `Win32` to `x64`. -7. Press F7 on keyboard or select `BUILD` from the menu bar and choose `Build Solution`. -8. If compilation will be fine a compiled executable will be in `visual\VS2010\bin\x64_Release\lz4.exe` - - -#### Projects available within lz4.sln - -The Visual Studio solution file `lz4.sln` contains many projects that will be compiled to the -`visual\VS2010\bin\$(Platform)_$(Configuration)` directory. For example `lz4` set to `x64` and -`Release` will be compiled to `visual\VS2010\bin\x64_Release\lz4.exe`. The solution file contains the -following projects: - -- `lz4` : Command Line Utility, supporting gzip-like arguments -- `datagen` : Synthetic and parametrable data generator, for tests -- `frametest` : Test tool that checks lz4frame integrity on target platform -- `fullbench` : Precisely measure speed for each lz4 inner functions -- `fuzzer` : Test tool, to check lz4 integrity on target platform -- `liblz4` : A static LZ4 library compiled to `liblz4_static.lib` -- `liblz4-dll` : A dynamic LZ4 library (DLL) compiled to `liblz4.dll` with the import library `liblz4.lib` -- `fullbench-dll` : The fullbench program compiled with the import library; the executable requires LZ4 DLL - - -#### Using LZ4 DLL with Microsoft Visual C++ project - -The header files `lib\lz4.h`, `lib\lz4hc.h`, `lib\lz4frame.h` and the import library -`visual\VS2010\bin\$(Platform)_$(Configuration)\liblz4.lib` are required to compile a -project using Visual C++. - -1. The path to header files should be added to `Additional Include Directories` that can - be found in Project Properties of Visual Studio IDE in the `C/C++` Property Pages on the `General` page. -2. The import library has to be added to `Additional Dependencies` that can - be found in Project Properties in the `Linker` Property Pages on the `Input` page. - If one will provide only the name `liblz4.lib` without a full path to the library - then the directory has to be added to `Linker\General\Additional Library Directories`. - -The compiled executable will require LZ4 DLL which is available at -`visual\VS2010\bin\$(Platform)_$(Configuration)\liblz4.dll`. diff --git a/visual/VS2010/datagen/datagen.vcxproj b/visual/VS2010/datagen/datagen.vcxproj deleted file mode 100644 index e24f961..0000000 --- a/visual/VS2010/datagen/datagen.vcxproj +++ /dev/null @@ -1,169 +0,0 @@ - - - - - Debug - Win32 - - - Debug - x64 - - - Release - Win32 - - - Release - x64 - - - - {D745AE2F-596A-403A-9B91-81A8C6779243} - Win32Proj - datagen - $(SolutionDir)bin\$(Platform)_$(Configuration)\ - $(SolutionDir)bin\obj\$(RootNamespace)_$(Platform)_$(Configuration)\ - - - - Application - true - Unicode - - - Application - true - Unicode - - - Application - false - Unicode - true - - - Application - false - Unicode - true - - - - - - - - - - - - - - - - - - - true - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\programs;$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - - - true - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\programs;$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - true - - - false - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\programs;$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - - - false - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\programs;$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - true - - - - - - Level4 - Disabled - WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - false - MultiThreadedDebug - - - Console - true - - - - - - - Level4 - Disabled - WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - true - /analyze:stacksize295252 %(AdditionalOptions) - MultiThreadedDebug - - - Console - true - - - - - Level4 - - - MaxSpeed - true - true - WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) - false - false - MultiThreaded - - - Console - true - true - true - - - - - Level4 - - - MaxSpeed - true - true - WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) - false - true - /analyze:stacksize295252 %(AdditionalOptions) - MultiThreaded - - - Console - true - true - true - - - - - - - - - - - - - diff --git a/visual/VS2010/frametest/frametest.vcxproj b/visual/VS2010/frametest/frametest.vcxproj deleted file mode 100644 index 3196768..0000000 --- a/visual/VS2010/frametest/frametest.vcxproj +++ /dev/null @@ -1,176 +0,0 @@ - - - - - Debug - Win32 - - - Debug - x64 - - - Release - Win32 - - - Release - x64 - - - - {39AD6ECC-8BAD-4368-95E4-A1AA2F077BB7} - Win32Proj - frametest - $(SolutionDir)bin\$(Platform)_$(Configuration)\ - $(SolutionDir)bin\obj\$(RootNamespace)_$(Platform)_$(Configuration)\ - - - - Application - true - Unicode - - - Application - true - Unicode - - - Application - false - Unicode - true - - - Application - false - Unicode - true - - - - - - - - - - - - - - - - - - - true - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - - - true - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - true - - - false - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - - - false - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - true - - - - - - Level4 - Disabled - WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - false - MultiThreadedDebug - - - Console - true - - - - - - - Level4 - Disabled - WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - true - /analyze:stacksize295252 %(AdditionalOptions) - MultiThreadedDebug - - - Console - true - - - - - Level4 - - - MaxSpeed - true - true - WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) - false - false - MultiThreaded - - - Console - true - true - true - - - - - Level4 - - - MaxSpeed - true - true - WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) - false - true - /analyze:stacksize295252 %(AdditionalOptions) - MultiThreaded - - - Console - true - true - true - - - - - - - - - - - - - - - - - - - - diff --git a/visual/VS2010/fullbench-dll/fullbench-dll.vcxproj b/visual/VS2010/fullbench-dll/fullbench-dll.vcxproj deleted file mode 100644 index 8f503f5..0000000 --- a/visual/VS2010/fullbench-dll/fullbench-dll.vcxproj +++ /dev/null @@ -1,180 +0,0 @@ - - - - - Debug - Win32 - - - Debug - x64 - - - Release - Win32 - - - Release - x64 - - - - {13992FD2-077E-4954-B065-A428198201A9} - Win32Proj - fullbench-dll - $(SolutionDir)bin\$(Platform)_$(Configuration)\ - $(SolutionDir)bin\obj\$(RootNamespace)_$(Platform)_$(Configuration)\ - - - - Application - true - Unicode - - - Application - true - Unicode - - - Application - false - Unicode - true - - - Application - false - Unicode - true - - - - - - - - - - - - - - - - - - - true - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - - - true - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - true - - - false - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - - - false - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - true - - - - - - Level4 - Disabled - WIN32;_DEBUG;_CONSOLE;LZ4_DLL_IMPORT=1;%(PreprocessorDefinitions) - true - false - MultiThreadedDebug - - - Console - true - $(SolutionDir)bin\$(Platform)_$(Configuration);%(AdditionalLibraryDirectories) - liblz4.lib;%(AdditionalDependencies) - - - - - - - Level4 - Disabled - WIN32;_DEBUG;_CONSOLE;LZ4_DLL_IMPORT=1;%(PreprocessorDefinitions) - true - true - /analyze:stacksize295252 %(AdditionalOptions) - MultiThreadedDebug - - - Console - true - $(SolutionDir)bin\$(Platform)_$(Configuration);%(AdditionalLibraryDirectories) - liblz4.lib;%(AdditionalDependencies) - - - - - Level4 - - - MaxSpeed - true - true - WIN32;NDEBUG;_CONSOLE;LZ4_DLL_IMPORT=1;%(PreprocessorDefinitions) - false - false - MultiThreaded - - - Console - true - true - true - $(SolutionDir)bin\$(Platform)_$(Configuration);%(AdditionalLibraryDirectories) - liblz4.lib;%(AdditionalDependencies) - - - - - Level4 - - - MaxSpeed - true - true - WIN32;NDEBUG;_CONSOLE;LZ4_DLL_IMPORT=1;%(PreprocessorDefinitions) - false - true - /analyze:stacksize295252 %(AdditionalOptions) - MultiThreaded - - - Console - true - true - true - $(SolutionDir)bin\$(Platform)_$(Configuration);%(AdditionalLibraryDirectories) - liblz4.lib;%(AdditionalDependencies) - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/visual/VS2010/fullbench/fullbench.vcxproj b/visual/VS2010/fullbench/fullbench.vcxproj deleted file mode 100644 index aa67431..0000000 --- a/visual/VS2010/fullbench/fullbench.vcxproj +++ /dev/null @@ -1,176 +0,0 @@ - - - - - Debug - Win32 - - - Debug - x64 - - - Release - Win32 - - - Release - x64 - - - - {6A4DF4EF-C77F-43C6-8901-DDCD20879E4E} - Win32Proj - fullbench - $(SolutionDir)bin\$(Platform)_$(Configuration)\ - $(SolutionDir)bin\obj\$(RootNamespace)_$(Platform)_$(Configuration)\ - - - - Application - true - Unicode - - - Application - true - Unicode - - - Application - false - Unicode - true - - - Application - false - Unicode - true - - - - - - - - - - - - - - - - - - - true - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - - - true - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - true - - - false - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - - - false - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - true - - - - - - Level4 - Disabled - WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - false - MultiThreadedDebug - - - Console - true - - - - - - - Level4 - Disabled - WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - true - /analyze:stacksize295252 %(AdditionalOptions) - MultiThreadedDebug - - - Console - true - - - - - Level4 - - - MaxSpeed - true - true - WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) - false - false - MultiThreaded - - - Console - true - true - true - - - - - Level4 - - - MaxSpeed - true - true - WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) - false - true - /analyze:stacksize295252 %(AdditionalOptions) - MultiThreaded - - - Console - true - true - true - - - - - - - - - - - - - - - - - - - - diff --git a/visual/VS2010/fuzzer/fuzzer.vcxproj b/visual/VS2010/fuzzer/fuzzer.vcxproj deleted file mode 100644 index 21cbf56..0000000 --- a/visual/VS2010/fuzzer/fuzzer.vcxproj +++ /dev/null @@ -1,173 +0,0 @@ - - - - - Debug - Win32 - - - Debug - x64 - - - Release - Win32 - - - Release - x64 - - - - {18B9F1A7-9C66-4352-898B-30804DADE0FD} - Win32Proj - fuzzer - $(SolutionDir)bin\$(Platform)_$(Configuration)\ - $(SolutionDir)bin\obj\$(RootNamespace)_$(Platform)_$(Configuration)\ - - - - Application - true - Unicode - - - Application - true - Unicode - - - Application - false - Unicode - true - - - Application - false - Unicode - true - - - - - - - - - - - - - - - - - - - true - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - - - true - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - true - - - false - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - - - false - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - true - - - - - - Level4 - Disabled - WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - false - MultiThreadedDebug - - - Console - true - - - - - - - Level4 - Disabled - WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - true - /analyze:stacksize295252 %(AdditionalOptions) - MultiThreadedDebug - - - Console - true - - - - - Level4 - - - MaxSpeed - true - true - WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) - false - false - MultiThreaded - - - Console - true - true - true - - - - - Level4 - - - MaxSpeed - true - true - WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) - false - true - /analyze:stacksize295252 %(AdditionalOptions) - MultiThreaded - - - Console - true - true - true - - - - - - - - - - - - - - - - - diff --git a/visual/VS2010/liblz4-dll/liblz4-dll.rc b/visual/VS2010/liblz4-dll/liblz4-dll.rc deleted file mode 100644 index b1871fe..0000000 --- a/visual/VS2010/liblz4-dll/liblz4-dll.rc +++ /dev/null @@ -1,51 +0,0 @@ -// Microsoft Visual C++ generated resource script. -// - -#include "lz4.h" /* LZ4_VERSION_STRING */ -#define APSTUDIO_READONLY_SYMBOLS -#include "verrsrc.h" -#undef APSTUDIO_READONLY_SYMBOLS - - -#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU) -LANGUAGE 9, 1 - -///////////////////////////////////////////////////////////////////////////// -// -// Version -// - -VS_VERSION_INFO VERSIONINFO - FILEVERSION LZ4_VERSION_MAJOR,LZ4_VERSION_MINOR,LZ4_VERSION_RELEASE,0 - PRODUCTVERSION LZ4_VERSION_MAJOR,LZ4_VERSION_MINOR,LZ4_VERSION_RELEASE,0 - FILEFLAGSMASK VS_FFI_FILEFLAGSMASK -#ifdef _DEBUG - FILEFLAGS VS_FF_DEBUG -#else - FILEFLAGS 0x0L -#endif - FILEOS VOS_NT_WINDOWS32 - FILETYPE VFT_DLL - FILESUBTYPE VFT2_UNKNOWN -BEGIN - BLOCK "StringFileInfo" - BEGIN - BLOCK "040904B0" - BEGIN - VALUE "CompanyName", "Yann Collet" - VALUE "FileDescription", "Extremely fast compression" - VALUE "FileVersion", LZ4_VERSION_STRING - VALUE "InternalName", "lz4.dll" - VALUE "LegalCopyright", "Copyright (C) 2013-2016, Yann Collet" - VALUE "OriginalFilename", "lz4.dll" - VALUE "ProductName", "LZ4" - VALUE "ProductVersion", LZ4_VERSION_STRING - END - END - BLOCK "VarFileInfo" - BEGIN - VALUE "Translation", 0x0409, 1200 - END -END - -#endif diff --git a/visual/VS2010/liblz4-dll/liblz4-dll.vcxproj b/visual/VS2010/liblz4-dll/liblz4-dll.vcxproj deleted file mode 100644 index 56ec3b9..0000000 --- a/visual/VS2010/liblz4-dll/liblz4-dll.vcxproj +++ /dev/null @@ -1,179 +0,0 @@ - - - - - Debug - Win32 - - - Debug - x64 - - - Release - Win32 - - - Release - x64 - - - - {9800039D-4AAA-43A4-BB78-FEF6F4836927} - Win32Proj - liblz4-dll - $(SolutionDir)bin\$(Platform)_$(Configuration)\ - $(SolutionDir)bin\obj\$(RootNamespace)_$(Platform)_$(Configuration)\ - liblz4-dll - - - - DynamicLibrary - true - Unicode - - - DynamicLibrary - true - Unicode - - - DynamicLibrary - false - Unicode - true - - - DynamicLibrary - false - Unicode - true - - - - - - - - - - - - - - - - - - - true - liblz4 - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - - - true - liblz4 - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - true - - - false - liblz4 - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - - - false - liblz4 - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - true - - - - - - Level4 - Disabled - WIN32;_DEBUG;LZ4_DLL_EXPORT=1;%(PreprocessorDefinitions) - true - false - MultiThreadedDebug - - - true - - - - - - - Level4 - Disabled - WIN32;_DEBUG;LZ4_DLL_EXPORT=1;%(PreprocessorDefinitions) - true - true - /analyze:stacksize295252 %(AdditionalOptions) - MultiThreadedDebug - - - true - - - - - Level4 - - - MaxSpeed - true - true - WIN32;NDEBUG;LZ4_DLL_EXPORT=1;%(PreprocessorDefinitions) - false - false - MultiThreaded - - - true - true - true - - - - - Level4 - - - MaxSpeed - true - true - WIN32;NDEBUG;LZ4_DLL_EXPORT=1;%(PreprocessorDefinitions) - false - true - /analyze:stacksize295252 %(AdditionalOptions) - MultiThreaded - - - true - true - true - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/visual/VS2010/liblz4/liblz4.vcxproj b/visual/VS2010/liblz4/liblz4.vcxproj deleted file mode 100644 index 61ea159..0000000 --- a/visual/VS2010/liblz4/liblz4.vcxproj +++ /dev/null @@ -1,175 +0,0 @@ - - - - - Debug - Win32 - - - Debug - x64 - - - Release - Win32 - - - Release - x64 - - - - {9092C5CC-3E71-41B3-BF68-4A7BDD8A5476} - Win32Proj - liblz4 - $(SolutionDir)bin\$(Platform)_$(Configuration)\ - $(SolutionDir)bin\obj\$(RootNamespace)_$(Platform)_$(Configuration)\ - - - - StaticLibrary - true - Unicode - - - StaticLibrary - true - Unicode - - - StaticLibrary - false - Unicode - true - - - StaticLibrary - false - Unicode - true - - - - - - - - - - - - - - - - - - - true - liblz4_static - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - - - true - liblz4_static - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - true - - - false - liblz4_static - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - - - false - liblz4_static - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - true - - - - - - Level4 - Disabled - WIN32;_DEBUG;LZ4_DLL_EXPORT=1;%(PreprocessorDefinitions) - true - false - MultiThreadedDebug - - - true - - - - - - - Level4 - Disabled - WIN32;_DEBUG;LZ4_DLL_EXPORT=1;%(PreprocessorDefinitions) - true - true - /analyze:stacksize295252 %(AdditionalOptions) - MultiThreadedDebug - - - true - - - - - Level4 - - - MaxSpeed - true - true - WIN32;NDEBUG;LZ4_DLL_EXPORT=1;%(PreprocessorDefinitions) - false - false - MultiThreaded - - - true - true - true - - - - - Level4 - - - MaxSpeed - true - true - WIN32;NDEBUG;LZ4_DLL_EXPORT=1;%(PreprocessorDefinitions) - false - true - /analyze:stacksize295252 %(AdditionalOptions) - MultiThreaded - - - true - true - true - - - - - - - - - - - - - - - - - - - diff --git a/visual/VS2010/lz4.sln b/visual/VS2010/lz4.sln deleted file mode 100644 index 78f223b..0000000 --- a/visual/VS2010/lz4.sln +++ /dev/null @@ -1,98 +0,0 @@ -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Express 2012 for Windows Desktop -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "lz4", "lz4\lz4.vcxproj", "{E30329AC-0057-4FE0-8FDA-7F650D398C4C}" -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "liblz4-dll", "liblz4-dll\liblz4-dll.vcxproj", "{9800039D-4AAA-43A4-BB78-FEF6F4836927}" -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "liblz4", "liblz4\liblz4.vcxproj", "{9092C5CC-3E71-41B3-BF68-4A7BDD8A5476}" -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "fuzzer", "fuzzer\fuzzer.vcxproj", "{18B9F1A7-9C66-4352-898B-30804DADE0FD}" -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "fullbench", "fullbench\fullbench.vcxproj", "{6A4DF4EF-C77F-43C6-8901-DDCD20879E4E}" -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "frametest", "frametest\frametest.vcxproj", "{39AD6ECC-8BAD-4368-95E4-A1AA2F077BB7}" -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "datagen", "datagen\datagen.vcxproj", "{D745AE2F-596A-403A-9B91-81A8C6779243}" -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "fullbench-dll", "fullbench-dll\fullbench-dll.vcxproj", "{13992FD2-077E-4954-B065-A428198201A9}" - ProjectSection(ProjectDependencies) = postProject - {9800039D-4AAA-43A4-BB78-FEF6F4836927} = {9800039D-4AAA-43A4-BB78-FEF6F4836927} - EndProjectSection -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Win32 = Debug|Win32 - Debug|x64 = Debug|x64 - Release|Win32 = Release|Win32 - Release|x64 = Release|x64 - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {E30329AC-0057-4FE0-8FDA-7F650D398C4C}.Debug|Win32.ActiveCfg = Debug|Win32 - {E30329AC-0057-4FE0-8FDA-7F650D398C4C}.Debug|Win32.Build.0 = Debug|Win32 - {E30329AC-0057-4FE0-8FDA-7F650D398C4C}.Debug|x64.ActiveCfg = Debug|x64 - {E30329AC-0057-4FE0-8FDA-7F650D398C4C}.Debug|x64.Build.0 = Debug|x64 - {E30329AC-0057-4FE0-8FDA-7F650D398C4C}.Release|Win32.ActiveCfg = Release|Win32 - {E30329AC-0057-4FE0-8FDA-7F650D398C4C}.Release|Win32.Build.0 = Release|Win32 - {E30329AC-0057-4FE0-8FDA-7F650D398C4C}.Release|x64.ActiveCfg = Release|x64 - {E30329AC-0057-4FE0-8FDA-7F650D398C4C}.Release|x64.Build.0 = Release|x64 - {9800039D-4AAA-43A4-BB78-FEF6F4836927}.Debug|Win32.ActiveCfg = Debug|Win32 - {9800039D-4AAA-43A4-BB78-FEF6F4836927}.Debug|Win32.Build.0 = Debug|Win32 - {9800039D-4AAA-43A4-BB78-FEF6F4836927}.Debug|x64.ActiveCfg = Debug|x64 - {9800039D-4AAA-43A4-BB78-FEF6F4836927}.Debug|x64.Build.0 = Debug|x64 - {9800039D-4AAA-43A4-BB78-FEF6F4836927}.Release|Win32.ActiveCfg = Release|Win32 - {9800039D-4AAA-43A4-BB78-FEF6F4836927}.Release|Win32.Build.0 = Release|Win32 - {9800039D-4AAA-43A4-BB78-FEF6F4836927}.Release|x64.ActiveCfg = Release|x64 - {9800039D-4AAA-43A4-BB78-FEF6F4836927}.Release|x64.Build.0 = Release|x64 - {9092C5CC-3E71-41B3-BF68-4A7BDD8A5476}.Debug|Win32.ActiveCfg = Debug|Win32 - {9092C5CC-3E71-41B3-BF68-4A7BDD8A5476}.Debug|Win32.Build.0 = Debug|Win32 - {9092C5CC-3E71-41B3-BF68-4A7BDD8A5476}.Debug|x64.ActiveCfg = Debug|x64 - {9092C5CC-3E71-41B3-BF68-4A7BDD8A5476}.Debug|x64.Build.0 = Debug|x64 - {9092C5CC-3E71-41B3-BF68-4A7BDD8A5476}.Release|Win32.ActiveCfg = Release|Win32 - {9092C5CC-3E71-41B3-BF68-4A7BDD8A5476}.Release|Win32.Build.0 = Release|Win32 - {9092C5CC-3E71-41B3-BF68-4A7BDD8A5476}.Release|x64.ActiveCfg = Release|x64 - {9092C5CC-3E71-41B3-BF68-4A7BDD8A5476}.Release|x64.Build.0 = Release|x64 - {18B9F1A7-9C66-4352-898B-30804DADE0FD}.Debug|Win32.ActiveCfg = Debug|Win32 - {18B9F1A7-9C66-4352-898B-30804DADE0FD}.Debug|Win32.Build.0 = Debug|Win32 - {18B9F1A7-9C66-4352-898B-30804DADE0FD}.Debug|x64.ActiveCfg = Debug|x64 - {18B9F1A7-9C66-4352-898B-30804DADE0FD}.Debug|x64.Build.0 = Debug|x64 - {18B9F1A7-9C66-4352-898B-30804DADE0FD}.Release|Win32.ActiveCfg = Release|Win32 - {18B9F1A7-9C66-4352-898B-30804DADE0FD}.Release|Win32.Build.0 = Release|Win32 - {18B9F1A7-9C66-4352-898B-30804DADE0FD}.Release|x64.ActiveCfg = Release|x64 - {18B9F1A7-9C66-4352-898B-30804DADE0FD}.Release|x64.Build.0 = Release|x64 - {6A4DF4EF-C77F-43C6-8901-DDCD20879E4E}.Debug|Win32.ActiveCfg = Debug|Win32 - {6A4DF4EF-C77F-43C6-8901-DDCD20879E4E}.Debug|Win32.Build.0 = Debug|Win32 - {6A4DF4EF-C77F-43C6-8901-DDCD20879E4E}.Debug|x64.ActiveCfg = Debug|x64 - {6A4DF4EF-C77F-43C6-8901-DDCD20879E4E}.Debug|x64.Build.0 = Debug|x64 - {6A4DF4EF-C77F-43C6-8901-DDCD20879E4E}.Release|Win32.ActiveCfg = Release|Win32 - {6A4DF4EF-C77F-43C6-8901-DDCD20879E4E}.Release|Win32.Build.0 = Release|Win32 - {6A4DF4EF-C77F-43C6-8901-DDCD20879E4E}.Release|x64.ActiveCfg = Release|x64 - {6A4DF4EF-C77F-43C6-8901-DDCD20879E4E}.Release|x64.Build.0 = Release|x64 - {39AD6ECC-8BAD-4368-95E4-A1AA2F077BB7}.Debug|Win32.ActiveCfg = Debug|Win32 - {39AD6ECC-8BAD-4368-95E4-A1AA2F077BB7}.Debug|Win32.Build.0 = Debug|Win32 - {39AD6ECC-8BAD-4368-95E4-A1AA2F077BB7}.Debug|x64.ActiveCfg = Debug|x64 - {39AD6ECC-8BAD-4368-95E4-A1AA2F077BB7}.Debug|x64.Build.0 = Debug|x64 - {39AD6ECC-8BAD-4368-95E4-A1AA2F077BB7}.Release|Win32.ActiveCfg = Release|Win32 - {39AD6ECC-8BAD-4368-95E4-A1AA2F077BB7}.Release|Win32.Build.0 = Release|Win32 - {39AD6ECC-8BAD-4368-95E4-A1AA2F077BB7}.Release|x64.ActiveCfg = Release|x64 - {39AD6ECC-8BAD-4368-95E4-A1AA2F077BB7}.Release|x64.Build.0 = Release|x64 - {D745AE2F-596A-403A-9B91-81A8C6779243}.Debug|Win32.ActiveCfg = Debug|Win32 - {D745AE2F-596A-403A-9B91-81A8C6779243}.Debug|Win32.Build.0 = Debug|Win32 - {D745AE2F-596A-403A-9B91-81A8C6779243}.Debug|x64.ActiveCfg = Debug|x64 - {D745AE2F-596A-403A-9B91-81A8C6779243}.Debug|x64.Build.0 = Debug|x64 - {D745AE2F-596A-403A-9B91-81A8C6779243}.Release|Win32.ActiveCfg = Release|Win32 - {D745AE2F-596A-403A-9B91-81A8C6779243}.Release|Win32.Build.0 = Release|Win32 - {D745AE2F-596A-403A-9B91-81A8C6779243}.Release|x64.ActiveCfg = Release|x64 - {D745AE2F-596A-403A-9B91-81A8C6779243}.Release|x64.Build.0 = Release|x64 - {13992FD2-077E-4954-B065-A428198201A9}.Debug|Win32.ActiveCfg = Debug|Win32 - {13992FD2-077E-4954-B065-A428198201A9}.Debug|Win32.Build.0 = Debug|Win32 - {13992FD2-077E-4954-B065-A428198201A9}.Debug|x64.ActiveCfg = Debug|x64 - {13992FD2-077E-4954-B065-A428198201A9}.Debug|x64.Build.0 = Debug|x64 - {13992FD2-077E-4954-B065-A428198201A9}.Release|Win32.ActiveCfg = Release|Win32 - {13992FD2-077E-4954-B065-A428198201A9}.Release|Win32.Build.0 = Release|Win32 - {13992FD2-077E-4954-B065-A428198201A9}.Release|x64.ActiveCfg = Release|x64 - {13992FD2-077E-4954-B065-A428198201A9}.Release|x64.Build.0 = Release|x64 - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection -EndGlobal diff --git a/visual/VS2010/lz4/lz4.rc b/visual/VS2010/lz4/lz4.rc deleted file mode 100644 index c593edf..0000000 --- a/visual/VS2010/lz4/lz4.rc +++ /dev/null @@ -1,51 +0,0 @@ -// Microsoft Visual C++ generated resource script. -// - -#include "lz4.h" /* LZ4_VERSION_STRING */ -#define APSTUDIO_READONLY_SYMBOLS -#include "verrsrc.h" -#undef APSTUDIO_READONLY_SYMBOLS - - -#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU) -LANGUAGE 9, 1 - -///////////////////////////////////////////////////////////////////////////// -// -// Version -// - -VS_VERSION_INFO VERSIONINFO - FILEVERSION LZ4_VERSION_MAJOR,LZ4_VERSION_MINOR,LZ4_VERSION_RELEASE,0 - PRODUCTVERSION LZ4_VERSION_MAJOR,LZ4_VERSION_MINOR,LZ4_VERSION_RELEASE,0 - FILEFLAGSMASK VS_FFI_FILEFLAGSMASK -#ifdef _DEBUG - FILEFLAGS VS_FF_DEBUG -#else - FILEFLAGS 0x0L -#endif - FILEOS VOS_NT_WINDOWS32 - FILETYPE VFT_DLL - FILESUBTYPE VFT2_UNKNOWN -BEGIN - BLOCK "StringFileInfo" - BEGIN - BLOCK "040904B0" - BEGIN - VALUE "CompanyName", "Yann Collet" - VALUE "FileDescription", "Extremely fast compression" - VALUE "FileVersion", LZ4_VERSION_STRING - VALUE "InternalName", "lz4.exe" - VALUE "LegalCopyright", "Copyright (C) 2013-2016, Yann Collet" - VALUE "OriginalFilename", "lz4.exe" - VALUE "ProductName", "LZ4" - VALUE "ProductVersion", LZ4_VERSION_STRING - END - END - BLOCK "VarFileInfo" - BEGIN - VALUE "Translation", 0x0409, 1200 - END -END - -#endif diff --git a/visual/VS2010/lz4/lz4.vcxproj b/visual/VS2010/lz4/lz4.vcxproj deleted file mode 100644 index de7a714..0000000 --- a/visual/VS2010/lz4/lz4.vcxproj +++ /dev/null @@ -1,189 +0,0 @@ - - - - - Debug - Win32 - - - Debug - x64 - - - Release - Win32 - - - Release - x64 - - - - {E30329AC-0057-4FE0-8FDA-7F650D398C4C} - Win32Proj - lz4 - $(SolutionDir)bin\$(Platform)_$(Configuration)\ - $(SolutionDir)bin\obj\$(RootNamespace)_$(Platform)_$(Configuration)\ - - - - Application - true - Unicode - - - Application - true - Unicode - - - Application - false - Unicode - true - - - Application - false - Unicode - true - - - - - - - - - - - - - - - - - - - true - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - - - true - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - true - - - false - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - - - false - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - true - - - - - - Level4 - Disabled - WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - false - MultiThreadedDebug - - - Console - true - setargv.obj;%(AdditionalDependencies) - - - - - - - Level4 - Disabled - WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - true - /analyze:stacksize295252 %(AdditionalOptions) - MultiThreadedDebug - - - Console - true - setargv.obj;%(AdditionalDependencies) - - - - - Level4 - - - MaxSpeed - true - true - WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) - false - false - MultiThreaded - - - Console - true - true - true - setargv.obj;%(AdditionalDependencies) - - - - - Level4 - - - MaxSpeed - true - true - WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) - false - true - /analyze:stacksize295252 %(AdditionalOptions) - MultiThreaded - - - Console - true - true - true - setargv.obj;%(AdditionalDependencies) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/visual/VS2017/datagen/datagen.vcxproj b/visual/VS2017/datagen/datagen.vcxproj deleted file mode 100644 index 30e159e..0000000 --- a/visual/VS2017/datagen/datagen.vcxproj +++ /dev/null @@ -1,173 +0,0 @@ - - - - - Debug - Win32 - - - Debug - x64 - - - Release - Win32 - - - Release - x64 - - - - {D745AE2F-596A-403A-9B91-81A8C6779243} - Win32Proj - datagen - $(SolutionDir)bin\$(Platform)_$(Configuration)\ - $(SolutionDir)bin\obj\$(RootNamespace)_$(Platform)_$(Configuration)\ - - - - Application - true - Unicode - v141 - - - Application - true - Unicode - v141 - - - Application - false - Unicode - true - v141 - - - Application - false - Unicode - true - v141 - - - - - - - - - - - - - - - - - - - true - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\programs;$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - - - true - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\programs;$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - true - - - false - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\programs;$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - - - false - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\programs;$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - true - - - - - - Level4 - Disabled - WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - false - MultiThreadedDebug - - - Console - true - - - - - - - Level4 - Disabled - WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - true - /analyze:stacksize295252 %(AdditionalOptions) - MultiThreadedDebug - - - Console - true - - - - - Level4 - - - MaxSpeed - true - true - WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) - false - false - MultiThreaded - - - Console - true - true - true - - - - - Level4 - - - MaxSpeed - true - true - WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) - false - true - /analyze:stacksize295252 %(AdditionalOptions) - MultiThreaded - - - Console - true - true - true - - - - - - - - - - - - - \ No newline at end of file diff --git a/visual/VS2017/frametest/frametest.vcxproj b/visual/VS2017/frametest/frametest.vcxproj deleted file mode 100644 index a3a403d..0000000 --- a/visual/VS2017/frametest/frametest.vcxproj +++ /dev/null @@ -1,180 +0,0 @@ - - - - - Debug - Win32 - - - Debug - x64 - - - Release - Win32 - - - Release - x64 - - - - {39AD6ECC-8BAD-4368-95E4-A1AA2F077BB7} - Win32Proj - frametest - $(SolutionDir)bin\$(Platform)_$(Configuration)\ - $(SolutionDir)bin\obj\$(RootNamespace)_$(Platform)_$(Configuration)\ - - - - Application - true - Unicode - v141 - - - Application - true - Unicode - v141 - - - Application - false - Unicode - true - v141 - - - Application - false - Unicode - true - v141 - - - - - - - - - - - - - - - - - - - true - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - - - true - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - true - - - false - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - - - false - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - true - - - - - - Level4 - Disabled - WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - false - MultiThreadedDebug - - - Console - true - - - - - - - Level4 - Disabled - WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - true - /analyze:stacksize295252 %(AdditionalOptions) - MultiThreadedDebug - - - Console - true - - - - - Level4 - - - MaxSpeed - true - true - WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) - false - false - MultiThreaded - - - Console - true - true - true - - - - - Level4 - - - MaxSpeed - true - true - WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) - false - true - /analyze:stacksize295252 %(AdditionalOptions) - MultiThreaded - - - Console - true - true - true - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/visual/VS2017/fullbench-dll/fullbench-dll.vcxproj b/visual/VS2017/fullbench-dll/fullbench-dll.vcxproj deleted file mode 100644 index d54a8d7..0000000 --- a/visual/VS2017/fullbench-dll/fullbench-dll.vcxproj +++ /dev/null @@ -1,184 +0,0 @@ - - - - - Debug - Win32 - - - Debug - x64 - - - Release - Win32 - - - Release - x64 - - - - {13992FD2-077E-4954-B065-A428198201A9} - Win32Proj - fullbench-dll - $(SolutionDir)bin\$(Platform)_$(Configuration)\ - $(SolutionDir)bin\obj\$(RootNamespace)_$(Platform)_$(Configuration)\ - - - - Application - true - Unicode - v141 - - - Application - true - Unicode - v141 - - - Application - false - Unicode - true - v141 - - - Application - false - Unicode - true - v141 - - - - - - - - - - - - - - - - - - - true - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - - - true - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - true - - - false - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - - - false - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - true - - - - - - Level4 - Disabled - WIN32;_DEBUG;_CONSOLE;LZ4_DLL_IMPORT=1;%(PreprocessorDefinitions) - true - false - MultiThreadedDebug - - - Console - true - $(SolutionDir)bin\$(Platform)_$(Configuration);%(AdditionalLibraryDirectories) - liblz4.lib;%(AdditionalDependencies) - - - - - - - Level4 - Disabled - WIN32;_DEBUG;_CONSOLE;LZ4_DLL_IMPORT=1;%(PreprocessorDefinitions) - true - true - /analyze:stacksize295252 %(AdditionalOptions) - MultiThreadedDebug - - - Console - true - $(SolutionDir)bin\$(Platform)_$(Configuration);%(AdditionalLibraryDirectories) - liblz4.lib;%(AdditionalDependencies) - - - - - Level4 - - - MaxSpeed - true - true - WIN32;NDEBUG;_CONSOLE;LZ4_DLL_IMPORT=1;%(PreprocessorDefinitions) - false - false - MultiThreaded - - - Console - true - true - true - $(SolutionDir)bin\$(Platform)_$(Configuration);%(AdditionalLibraryDirectories) - liblz4.lib;%(AdditionalDependencies) - - - - - Level4 - - - MaxSpeed - true - true - WIN32;NDEBUG;_CONSOLE;LZ4_DLL_IMPORT=1;%(PreprocessorDefinitions) - false - true - /analyze:stacksize295252 %(AdditionalOptions) - MultiThreaded - - - Console - true - true - true - $(SolutionDir)bin\$(Platform)_$(Configuration);%(AdditionalLibraryDirectories) - liblz4.lib;%(AdditionalDependencies) - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/visual/VS2017/fullbench/fullbench.vcxproj b/visual/VS2017/fullbench/fullbench.vcxproj deleted file mode 100644 index 54c9743..0000000 --- a/visual/VS2017/fullbench/fullbench.vcxproj +++ /dev/null @@ -1,180 +0,0 @@ - - - - - Debug - Win32 - - - Debug - x64 - - - Release - Win32 - - - Release - x64 - - - - {6A4DF4EF-C77F-43C6-8901-DDCD20879E4E} - Win32Proj - fullbench - $(SolutionDir)bin\$(Platform)_$(Configuration)\ - $(SolutionDir)bin\obj\$(RootNamespace)_$(Platform)_$(Configuration)\ - - - - Application - true - Unicode - v141 - - - Application - true - Unicode - v141 - - - Application - false - Unicode - true - v141 - - - Application - false - Unicode - true - v141 - - - - - - - - - - - - - - - - - - - true - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - - - true - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - true - - - false - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - - - false - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - true - - - - - - Level4 - Disabled - WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - false - MultiThreadedDebug - - - Console - true - - - - - - - Level4 - Disabled - WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - true - /analyze:stacksize295252 %(AdditionalOptions) - MultiThreadedDebug - - - Console - true - - - - - Level4 - - - MaxSpeed - true - true - WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) - false - false - MultiThreaded - - - Console - true - true - true - - - - - Level4 - - - MaxSpeed - true - true - WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) - false - true - /analyze:stacksize295252 %(AdditionalOptions) - MultiThreaded - - - Console - true - true - true - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/visual/VS2017/fuzzer/fuzzer.vcxproj b/visual/VS2017/fuzzer/fuzzer.vcxproj deleted file mode 100644 index aa6fe42..0000000 --- a/visual/VS2017/fuzzer/fuzzer.vcxproj +++ /dev/null @@ -1,177 +0,0 @@ - - - - - Debug - Win32 - - - Debug - x64 - - - Release - Win32 - - - Release - x64 - - - - {18B9F1A7-9C66-4352-898B-30804DADE0FD} - Win32Proj - fuzzer - $(SolutionDir)bin\$(Platform)_$(Configuration)\ - $(SolutionDir)bin\obj\$(RootNamespace)_$(Platform)_$(Configuration)\ - - - - Application - true - Unicode - v141 - - - Application - true - Unicode - v141 - - - Application - false - Unicode - true - v141 - - - Application - false - Unicode - true - v141 - - - - - - - - - - - - - - - - - - - true - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - - - true - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - true - - - false - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - - - false - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - true - - - - - - Level4 - Disabled - WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - false - MultiThreadedDebug - - - Console - true - - - - - - - Level4 - Disabled - WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - true - /analyze:stacksize295252 %(AdditionalOptions) - MultiThreadedDebug - - - Console - true - - - - - Level4 - - - MaxSpeed - true - true - WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) - false - false - MultiThreaded - - - Console - true - true - true - - - - - Level4 - - - MaxSpeed - true - true - WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) - false - true - /analyze:stacksize295252 %(AdditionalOptions) - MultiThreaded - - - Console - true - true - true - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/visual/VS2017/liblz4-dll/liblz4-dll.rc b/visual/VS2017/liblz4-dll/liblz4-dll.rc deleted file mode 100644 index b1871fe..0000000 --- a/visual/VS2017/liblz4-dll/liblz4-dll.rc +++ /dev/null @@ -1,51 +0,0 @@ -// Microsoft Visual C++ generated resource script. -// - -#include "lz4.h" /* LZ4_VERSION_STRING */ -#define APSTUDIO_READONLY_SYMBOLS -#include "verrsrc.h" -#undef APSTUDIO_READONLY_SYMBOLS - - -#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU) -LANGUAGE 9, 1 - -///////////////////////////////////////////////////////////////////////////// -// -// Version -// - -VS_VERSION_INFO VERSIONINFO - FILEVERSION LZ4_VERSION_MAJOR,LZ4_VERSION_MINOR,LZ4_VERSION_RELEASE,0 - PRODUCTVERSION LZ4_VERSION_MAJOR,LZ4_VERSION_MINOR,LZ4_VERSION_RELEASE,0 - FILEFLAGSMASK VS_FFI_FILEFLAGSMASK -#ifdef _DEBUG - FILEFLAGS VS_FF_DEBUG -#else - FILEFLAGS 0x0L -#endif - FILEOS VOS_NT_WINDOWS32 - FILETYPE VFT_DLL - FILESUBTYPE VFT2_UNKNOWN -BEGIN - BLOCK "StringFileInfo" - BEGIN - BLOCK "040904B0" - BEGIN - VALUE "CompanyName", "Yann Collet" - VALUE "FileDescription", "Extremely fast compression" - VALUE "FileVersion", LZ4_VERSION_STRING - VALUE "InternalName", "lz4.dll" - VALUE "LegalCopyright", "Copyright (C) 2013-2016, Yann Collet" - VALUE "OriginalFilename", "lz4.dll" - VALUE "ProductName", "LZ4" - VALUE "ProductVersion", LZ4_VERSION_STRING - END - END - BLOCK "VarFileInfo" - BEGIN - VALUE "Translation", 0x0409, 1200 - END -END - -#endif diff --git a/visual/VS2017/liblz4-dll/liblz4-dll.vcxproj b/visual/VS2017/liblz4-dll/liblz4-dll.vcxproj deleted file mode 100644 index 8e7ee3b..0000000 --- a/visual/VS2017/liblz4-dll/liblz4-dll.vcxproj +++ /dev/null @@ -1,183 +0,0 @@ - - - - - Debug - Win32 - - - Debug - x64 - - - Release - Win32 - - - Release - x64 - - - - {9800039D-4AAA-43A4-BB78-FEF6F4836927} - Win32Proj - liblz4-dll - $(SolutionDir)bin\$(Platform)_$(Configuration)\ - $(SolutionDir)bin\obj\$(RootNamespace)_$(Platform)_$(Configuration)\ - liblz4-dll - - - - DynamicLibrary - true - Unicode - v141 - - - DynamicLibrary - true - Unicode - v141 - - - DynamicLibrary - false - Unicode - true - v141 - - - DynamicLibrary - false - Unicode - true - v141 - - - - - - - - - - - - - - - - - - - true - liblz4 - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - - - true - liblz4 - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - true - - - false - liblz4 - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - - - false - liblz4 - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - true - - - - - - Level4 - Disabled - WIN32;_DEBUG;LZ4_DLL_EXPORT=1;%(PreprocessorDefinitions) - true - false - MultiThreadedDebug - - - true - - - - - - - Level4 - Disabled - WIN32;_DEBUG;LZ4_DLL_EXPORT=1;%(PreprocessorDefinitions) - true - true - /analyze:stacksize295252 %(AdditionalOptions) - MultiThreadedDebug - - - true - - - - - Level4 - - - MaxSpeed - true - true - WIN32;NDEBUG;LZ4_DLL_EXPORT=1;%(PreprocessorDefinitions) - false - false - MultiThreaded - - - true - true - true - - - - - Level4 - - - MaxSpeed - true - true - WIN32;NDEBUG;LZ4_DLL_EXPORT=1;%(PreprocessorDefinitions) - false - true - /analyze:stacksize295252 %(AdditionalOptions) - MultiThreaded - - - true - true - true - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/visual/VS2017/liblz4/liblz4.vcxproj b/visual/VS2017/liblz4/liblz4.vcxproj deleted file mode 100644 index 948f7db..0000000 --- a/visual/VS2017/liblz4/liblz4.vcxproj +++ /dev/null @@ -1,179 +0,0 @@ - - - - - Debug - Win32 - - - Debug - x64 - - - Release - Win32 - - - Release - x64 - - - - {9092C5CC-3E71-41B3-BF68-4A7BDD8A5476} - Win32Proj - liblz4 - $(SolutionDir)bin\$(Platform)_$(Configuration)\ - $(SolutionDir)bin\obj\$(RootNamespace)_$(Platform)_$(Configuration)\ - - - - StaticLibrary - true - Unicode - v141 - - - StaticLibrary - true - Unicode - v141 - - - StaticLibrary - false - Unicode - true - v141 - - - StaticLibrary - false - Unicode - true - v141 - - - - - - - - - - - - - - - - - - - true - liblz4_static - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - - - true - liblz4_static - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - true - - - false - liblz4_static - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - - - false - liblz4_static - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - true - - - - - - Level4 - Disabled - WIN32;_DEBUG;LZ4_DLL_EXPORT=1;%(PreprocessorDefinitions) - true - false - MultiThreadedDebug - - - true - - - - - - - Level4 - Disabled - WIN32;_DEBUG;LZ4_DLL_EXPORT=1;%(PreprocessorDefinitions) - true - true - /analyze:stacksize295252 %(AdditionalOptions) - MultiThreadedDebug - - - true - - - - - Level4 - - - MaxSpeed - true - true - WIN32;NDEBUG;LZ4_DLL_EXPORT=1;%(PreprocessorDefinitions) - false - false - MultiThreaded - - - true - true - true - - - - - Level4 - - - MaxSpeed - true - true - WIN32;NDEBUG;LZ4_DLL_EXPORT=1;%(PreprocessorDefinitions) - false - true - /analyze:stacksize295252 %(AdditionalOptions) - MultiThreaded - - - true - true - true - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/visual/VS2017/lz4.sln b/visual/VS2017/lz4.sln deleted file mode 100644 index 6a2779f..0000000 --- a/visual/VS2017/lz4.sln +++ /dev/null @@ -1,103 +0,0 @@ -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.28307.271 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "liblz4-dll", "liblz4-dll\liblz4-dll.vcxproj", "{9800039D-4AAA-43A4-BB78-FEF6F4836927}" -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "liblz4", "liblz4\liblz4.vcxproj", "{9092C5CC-3E71-41B3-BF68-4A7BDD8A5476}" -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "fuzzer", "fuzzer\fuzzer.vcxproj", "{18B9F1A7-9C66-4352-898B-30804DADE0FD}" -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "fullbench", "fullbench\fullbench.vcxproj", "{6A4DF4EF-C77F-43C6-8901-DDCD20879E4E}" -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "frametest", "frametest\frametest.vcxproj", "{39AD6ECC-8BAD-4368-95E4-A1AA2F077BB7}" -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "datagen", "datagen\datagen.vcxproj", "{D745AE2F-596A-403A-9B91-81A8C6779243}" -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "fullbench-dll", "fullbench-dll\fullbench-dll.vcxproj", "{13992FD2-077E-4954-B065-A428198201A9}" - ProjectSection(ProjectDependencies) = postProject - {9800039D-4AAA-43A4-BB78-FEF6F4836927} = {9800039D-4AAA-43A4-BB78-FEF6F4836927} - EndProjectSection -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "lz4", "lz4\lz4.vcxproj", "{60A3115E-B988-41EE-8815-F4D4F253D866}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Win32 = Debug|Win32 - Debug|x64 = Debug|x64 - Release|Win32 = Release|Win32 - Release|x64 = Release|x64 - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {9800039D-4AAA-43A4-BB78-FEF6F4836927}.Debug|Win32.ActiveCfg = Debug|Win32 - {9800039D-4AAA-43A4-BB78-FEF6F4836927}.Debug|Win32.Build.0 = Debug|Win32 - {9800039D-4AAA-43A4-BB78-FEF6F4836927}.Debug|x64.ActiveCfg = Debug|x64 - {9800039D-4AAA-43A4-BB78-FEF6F4836927}.Debug|x64.Build.0 = Debug|x64 - {9800039D-4AAA-43A4-BB78-FEF6F4836927}.Release|Win32.ActiveCfg = Release|Win32 - {9800039D-4AAA-43A4-BB78-FEF6F4836927}.Release|Win32.Build.0 = Release|Win32 - {9800039D-4AAA-43A4-BB78-FEF6F4836927}.Release|x64.ActiveCfg = Release|x64 - {9800039D-4AAA-43A4-BB78-FEF6F4836927}.Release|x64.Build.0 = Release|x64 - {9092C5CC-3E71-41B3-BF68-4A7BDD8A5476}.Debug|Win32.ActiveCfg = Debug|Win32 - {9092C5CC-3E71-41B3-BF68-4A7BDD8A5476}.Debug|Win32.Build.0 = Debug|Win32 - {9092C5CC-3E71-41B3-BF68-4A7BDD8A5476}.Debug|x64.ActiveCfg = Debug|x64 - {9092C5CC-3E71-41B3-BF68-4A7BDD8A5476}.Debug|x64.Build.0 = Debug|x64 - {9092C5CC-3E71-41B3-BF68-4A7BDD8A5476}.Release|Win32.ActiveCfg = Release|Win32 - {9092C5CC-3E71-41B3-BF68-4A7BDD8A5476}.Release|Win32.Build.0 = Release|Win32 - {9092C5CC-3E71-41B3-BF68-4A7BDD8A5476}.Release|x64.ActiveCfg = Release|x64 - {9092C5CC-3E71-41B3-BF68-4A7BDD8A5476}.Release|x64.Build.0 = Release|x64 - {18B9F1A7-9C66-4352-898B-30804DADE0FD}.Debug|Win32.ActiveCfg = Debug|Win32 - {18B9F1A7-9C66-4352-898B-30804DADE0FD}.Debug|Win32.Build.0 = Debug|Win32 - {18B9F1A7-9C66-4352-898B-30804DADE0FD}.Debug|x64.ActiveCfg = Debug|x64 - {18B9F1A7-9C66-4352-898B-30804DADE0FD}.Debug|x64.Build.0 = Debug|x64 - {18B9F1A7-9C66-4352-898B-30804DADE0FD}.Release|Win32.ActiveCfg = Release|Win32 - {18B9F1A7-9C66-4352-898B-30804DADE0FD}.Release|Win32.Build.0 = Release|Win32 - {18B9F1A7-9C66-4352-898B-30804DADE0FD}.Release|x64.ActiveCfg = Release|x64 - {18B9F1A7-9C66-4352-898B-30804DADE0FD}.Release|x64.Build.0 = Release|x64 - {6A4DF4EF-C77F-43C6-8901-DDCD20879E4E}.Debug|Win32.ActiveCfg = Debug|Win32 - {6A4DF4EF-C77F-43C6-8901-DDCD20879E4E}.Debug|Win32.Build.0 = Debug|Win32 - {6A4DF4EF-C77F-43C6-8901-DDCD20879E4E}.Debug|x64.ActiveCfg = Debug|x64 - {6A4DF4EF-C77F-43C6-8901-DDCD20879E4E}.Debug|x64.Build.0 = Debug|x64 - {6A4DF4EF-C77F-43C6-8901-DDCD20879E4E}.Release|Win32.ActiveCfg = Release|Win32 - {6A4DF4EF-C77F-43C6-8901-DDCD20879E4E}.Release|Win32.Build.0 = Release|Win32 - {6A4DF4EF-C77F-43C6-8901-DDCD20879E4E}.Release|x64.ActiveCfg = Release|x64 - {6A4DF4EF-C77F-43C6-8901-DDCD20879E4E}.Release|x64.Build.0 = Release|x64 - {39AD6ECC-8BAD-4368-95E4-A1AA2F077BB7}.Debug|Win32.ActiveCfg = Debug|Win32 - {39AD6ECC-8BAD-4368-95E4-A1AA2F077BB7}.Debug|Win32.Build.0 = Debug|Win32 - {39AD6ECC-8BAD-4368-95E4-A1AA2F077BB7}.Debug|x64.ActiveCfg = Debug|x64 - {39AD6ECC-8BAD-4368-95E4-A1AA2F077BB7}.Debug|x64.Build.0 = Debug|x64 - {39AD6ECC-8BAD-4368-95E4-A1AA2F077BB7}.Release|Win32.ActiveCfg = Release|Win32 - {39AD6ECC-8BAD-4368-95E4-A1AA2F077BB7}.Release|Win32.Build.0 = Release|Win32 - {39AD6ECC-8BAD-4368-95E4-A1AA2F077BB7}.Release|x64.ActiveCfg = Release|x64 - {39AD6ECC-8BAD-4368-95E4-A1AA2F077BB7}.Release|x64.Build.0 = Release|x64 - {D745AE2F-596A-403A-9B91-81A8C6779243}.Debug|Win32.ActiveCfg = Debug|Win32 - {D745AE2F-596A-403A-9B91-81A8C6779243}.Debug|Win32.Build.0 = Debug|Win32 - {D745AE2F-596A-403A-9B91-81A8C6779243}.Debug|x64.ActiveCfg = Debug|x64 - {D745AE2F-596A-403A-9B91-81A8C6779243}.Debug|x64.Build.0 = Debug|x64 - {D745AE2F-596A-403A-9B91-81A8C6779243}.Release|Win32.ActiveCfg = Release|Win32 - {D745AE2F-596A-403A-9B91-81A8C6779243}.Release|Win32.Build.0 = Release|Win32 - {D745AE2F-596A-403A-9B91-81A8C6779243}.Release|x64.ActiveCfg = Release|x64 - {D745AE2F-596A-403A-9B91-81A8C6779243}.Release|x64.Build.0 = Release|x64 - {13992FD2-077E-4954-B065-A428198201A9}.Debug|Win32.ActiveCfg = Debug|Win32 - {13992FD2-077E-4954-B065-A428198201A9}.Debug|Win32.Build.0 = Debug|Win32 - {13992FD2-077E-4954-B065-A428198201A9}.Debug|x64.ActiveCfg = Debug|x64 - {13992FD2-077E-4954-B065-A428198201A9}.Debug|x64.Build.0 = Debug|x64 - {13992FD2-077E-4954-B065-A428198201A9}.Release|Win32.ActiveCfg = Release|Win32 - {13992FD2-077E-4954-B065-A428198201A9}.Release|Win32.Build.0 = Release|Win32 - {13992FD2-077E-4954-B065-A428198201A9}.Release|x64.ActiveCfg = Release|x64 - {13992FD2-077E-4954-B065-A428198201A9}.Release|x64.Build.0 = Release|x64 - {60A3115E-B988-41EE-8815-F4D4F253D866}.Debug|Win32.ActiveCfg = Debug|Win32 - {60A3115E-B988-41EE-8815-F4D4F253D866}.Debug|Win32.Build.0 = Debug|Win32 - {60A3115E-B988-41EE-8815-F4D4F253D866}.Debug|x64.ActiveCfg = Debug|x64 - {60A3115E-B988-41EE-8815-F4D4F253D866}.Debug|x64.Build.0 = Debug|x64 - {60A3115E-B988-41EE-8815-F4D4F253D866}.Release|Win32.ActiveCfg = Release|Win32 - {60A3115E-B988-41EE-8815-F4D4F253D866}.Release|Win32.Build.0 = Release|Win32 - {60A3115E-B988-41EE-8815-F4D4F253D866}.Release|x64.ActiveCfg = Release|x64 - {60A3115E-B988-41EE-8815-F4D4F253D866}.Release|x64.Build.0 = Release|x64 - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {BBC259B2-BABF-47CD-8A6A-7B8318A803AC} - EndGlobalSection -EndGlobal diff --git a/visual/VS2017/lz4/lz4.rc b/visual/VS2017/lz4/lz4.rc deleted file mode 100644 index c593edf..0000000 --- a/visual/VS2017/lz4/lz4.rc +++ /dev/null @@ -1,51 +0,0 @@ -// Microsoft Visual C++ generated resource script. -// - -#include "lz4.h" /* LZ4_VERSION_STRING */ -#define APSTUDIO_READONLY_SYMBOLS -#include "verrsrc.h" -#undef APSTUDIO_READONLY_SYMBOLS - - -#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU) -LANGUAGE 9, 1 - -///////////////////////////////////////////////////////////////////////////// -// -// Version -// - -VS_VERSION_INFO VERSIONINFO - FILEVERSION LZ4_VERSION_MAJOR,LZ4_VERSION_MINOR,LZ4_VERSION_RELEASE,0 - PRODUCTVERSION LZ4_VERSION_MAJOR,LZ4_VERSION_MINOR,LZ4_VERSION_RELEASE,0 - FILEFLAGSMASK VS_FFI_FILEFLAGSMASK -#ifdef _DEBUG - FILEFLAGS VS_FF_DEBUG -#else - FILEFLAGS 0x0L -#endif - FILEOS VOS_NT_WINDOWS32 - FILETYPE VFT_DLL - FILESUBTYPE VFT2_UNKNOWN -BEGIN - BLOCK "StringFileInfo" - BEGIN - BLOCK "040904B0" - BEGIN - VALUE "CompanyName", "Yann Collet" - VALUE "FileDescription", "Extremely fast compression" - VALUE "FileVersion", LZ4_VERSION_STRING - VALUE "InternalName", "lz4.exe" - VALUE "LegalCopyright", "Copyright (C) 2013-2016, Yann Collet" - VALUE "OriginalFilename", "lz4.exe" - VALUE "ProductName", "LZ4" - VALUE "ProductVersion", LZ4_VERSION_STRING - END - END - BLOCK "VarFileInfo" - BEGIN - VALUE "Translation", 0x0409, 1200 - END -END - -#endif diff --git a/visual/VS2017/lz4/lz4.vcxproj b/visual/VS2017/lz4/lz4.vcxproj deleted file mode 100644 index b4fed24..0000000 --- a/visual/VS2017/lz4/lz4.vcxproj +++ /dev/null @@ -1,164 +0,0 @@ - - - - - Debug - Win32 - - - Release - Win32 - - - Debug - x64 - - - Release - x64 - - - - 15.0 - {60A3115E-B988-41EE-8815-F4D4F253D866} - lz4 - 8.1 - - - - Application - true - v141 - Unicode - - - Application - false - v141 - false - Unicode - - - Application - true - v141 - MultiByte - - - Application - false - v141 - true - MultiByte - - - - - - - - - - - - - - - - - - - - - $(SolutionDir)bin\$(Platform)_$(Configuration)\ - $(SolutionDir)bin\obj\$(RootNamespace)_$(Platform)_$(Configuration)\ - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - - - $(SolutionDir)bin\$(Platform)_$(Configuration)\ - $(SolutionDir)bin\obj\$(RootNamespace)_$(Platform)_$(Configuration)\ - $(IncludePath);$(UniversalCRT_IncludePath);$(SolutionDir)..\..\lib;$(SolutionDir)..\..\programs;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - false - - - - Level4 - Disabled - true - true - true - WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) - - - true - Console - false - false - - - - - Level3 - Disabled - true - true - - - - - Level3 - MaxSpeed - true - true - true - true - WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) - - - true - true - true - Console - - - - - Level3 - MaxSpeed - true - true - true - true - - - true - true - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file -- cgit v1.2.3 From e97ff296272799d827b9a2dc0c9a2710b3a5b85c Mon Sep 17 00:00:00 2001 From: Nick Terrell Date: Wed, 19 Aug 2020 18:19:11 -0700 Subject: [CI][Fuzz] Fix Travis-CI fuzzer tests Fixes #781 --- ossfuzz/travisoss.sh | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ossfuzz/travisoss.sh b/ossfuzz/travisoss.sh index 5ea884c..218f4d6 100755 --- a/ossfuzz/travisoss.sh +++ b/ossfuzz/travisoss.sh @@ -12,7 +12,12 @@ then fi # Modify the oss-fuzz Dockerfile so that we're checking out the current branch on travis. -sed -i "s@https://github.com/lz4/lz4.git@-b $TRAVIS_BRANCH https://github.com/lz4/lz4.git@" /tmp/ossfuzz/projects/lz4/Dockerfile +if [ "x${TRAVIS_PULL_REQUEST}" -eq "xfalse" ] +then + sed -i "s@https://github.com/lz4/lz4.git@-b ${TRAVIS_BRANCH} https://github.com/lz4/lz4.git@" /tmp/ossfuzz/projects/lz4/Dockerfile +else + sed -i "s@https://github.com/lz4/lz4.git@-b ${TRAVIS_PULL_REQUEST_BRANCH} https://github.com/${TRAVIS_PULL_REQUEST_SLUG}.git@" /tmp/ossfuzz/projects/lz4/Dockerfile +fi # Try and build the fuzzers pushd /tmp/ossfuzz -- cgit v1.2.3 From 184a487d0d8249989885ef421c529b5ea64e7e2b Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 21 Aug 2020 18:12:13 -0700 Subject: added target aarch64, ppc64lw and s390x to TravisCI more portability tests --- .travis.yml | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/.travis.yml b/.travis.yml index f2da894..1474fad 100644 --- a/.travis.yml +++ b/.travis.yml @@ -124,6 +124,21 @@ matrix: - make platformTest CC=arm-linux-gnueabi-gcc QEMU_SYS=qemu-arm-static - make platformTest CC=aarch64-linux-gnu-gcc QEMU_SYS=qemu-aarch64-static + - name: aarch64 real-hw tests + arch: arm64 + script: + - make test + + - name: PPC64LE real-hw tests + arch: ppc64le + script: + - make test + + - name: IBM s390x real-hw tests + arch: s390x + script: + - make test + - name: (Xenial) gcc-5 compilation dist: xenial install: -- cgit v1.2.3 From 0002563be823ed65149ea7aa64654f3ce13c0e5b Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 21 Aug 2020 19:23:49 -0700 Subject: removed LZ4_compress_fast_force() which serves no more purpose. The comment implies that the simple presence of this unused function was affecting performance, and that's the reason why it was not removed earlier. This is likely another side effect of instruction alignment. It's obviously unreliable to rely on it in this way, meaning that the impact will be different, positive of negative, with any minor code change, and any minor compiler version change, even parameter change. --- lib/lz4.c | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 1cd7322..70324e2 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1288,22 +1288,6 @@ int LZ4_compress_default(const char* src, char* dst, int srcSize, int maxOutputS } -/* hidden debug function */ -/* strangely enough, gcc generates faster code when this function is uncommented, even if unused */ -int LZ4_compress_fast_force(const char* src, char* dst, int srcSize, int dstCapacity, int acceleration) -{ - LZ4_stream_t ctx; - LZ4_initStream(&ctx, sizeof(ctx)); - - if (srcSize < LZ4_64Klimit) { - return LZ4_compress_generic(&ctx.internal_donotuse, src, dst, srcSize, NULL, dstCapacity, limitedOutput, byU16, noDict, noDictIssue, acceleration); - } else { - tableType_t const addrMode = (sizeof(void*) > 4) ? byU32 : byPtr; - return LZ4_compress_generic(&ctx.internal_donotuse, src, dst, srcSize, NULL, dstCapacity, limitedOutput, addrMode, noDict, noDictIssue, acceleration); - } -} - - /* Note!: This function leaves the stream in an unclean/broken state! * It is not safe to subsequently use the same state with a _fastReset() or * _continue() call without resetting it. */ -- cgit v1.2.3 From 9a7658070aeca2c0a6c68e5025a80f9e4b847ad7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niklas=20Hamb=C3=BCchen?= Date: Mon, 24 Aug 2020 06:23:51 +0200 Subject: Use fstat() to determine file size. This allows us to get the file size even when the input file is passed via stdin. This fixes `--content-size` not working in situations like $ lz4 -v --content-size < /tmp/test > /tmp/test.lz4 Warning : cannot determine input content size With this change, it works. Also helps with #904. --- programs/lz4io.c | 4 ++-- programs/util.h | 27 ++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/programs/lz4io.c b/programs/lz4io.c index 7926b20..0f3507e 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -680,7 +680,7 @@ LZ4IO_compressFilename_extRess(LZ4IO_prefs_t* const io_prefs, cRess_t ress, prefs.frameInfo.contentChecksumFlag = (LZ4F_contentChecksum_t)io_prefs->streamChecksum; prefs.favorDecSpeed = io_prefs->favorDecSpeed; if (io_prefs->contentSizeFlag) { - U64 const fileSize = UTIL_getFileSize(srcFileName); + U64 const fileSize = UTIL_getOpenFileSize(srcFile); prefs.frameInfo.contentSize = fileSize; /* == 0 if input == stdin */ if (fileSize==0) DISPLAYLEVEL(3, "Warning : cannot determine input content size \n"); @@ -1472,7 +1472,7 @@ LZ4IO_getCompressedFileInfo(LZ4IO_cFileInfo_t* cfinfo, const char* input_filenam LZ4IO_infoResult result = LZ4IO_format_not_known; /* default result (error) */ unsigned char buffer[LZ4F_HEADER_SIZE_MAX]; FILE* const finput = LZ4IO_openSrcFile(input_filename); - cfinfo->fileSize = UTIL_getFileSize(input_filename); + cfinfo->fileSize = (finput == NULL) ? 0 : UTIL_getOpenFileSize(finput); while (!feof(finput)) { LZ4IO_frameInfo_t frameInfo = LZ4IO_INIT_FRAMEINFO; diff --git a/programs/util.h b/programs/util.h index 8e361ca..4b2c09a 100644 --- a/programs/util.h +++ b/programs/util.h @@ -33,7 +33,7 @@ extern "C" { #include /* size_t, ptrdiff_t */ #include /* malloc */ #include /* strlen, strncpy */ -#include /* fprintf */ +#include /* fprintf, fileno */ #include #include /* stat, utime */ #include /* stat */ @@ -357,6 +357,31 @@ UTIL_STATIC U32 UTIL_isDirectory(const char* infilename) } +UTIL_STATIC U64 UTIL_getOpenFileSize(FILE* file) +{ + int r; + int fd = fileno(file); + if (fd < 0) { + perror("fileno"); + exit(1); + } +#if defined(_MSC_VER) + struct __stat64 statbuf; + r = _fstat64(fd, &statbuf); + if (r || !(statbuf.st_mode & S_IFREG)) return 0; /* No good... */ +#elif defined(__MINGW32__) && defined (__MSVCRT__) + struct _stati64 statbuf; + r = _fstati64(fd, &statbuf); + if (r || !(statbuf.st_mode & S_IFREG)) return 0; /* No good... */ +#else + struct stat statbuf; + r = fstat(fd, &statbuf); + if (r || !S_ISREG(statbuf.st_mode)) return 0; /* No good... */ +#endif + return (U64)statbuf.st_size; +} + + UTIL_STATIC U64 UTIL_getFileSize(const char* infilename) { int r; -- cgit v1.2.3 From f803995b77379e420d3af21a25903da7c6bf5fbd Mon Sep 17 00:00:00 2001 From: Nick Terrell Date: Mon, 24 Aug 2020 11:22:57 -0700 Subject: [CI][ossfuzz] Fix string equal --- ossfuzz/travisoss.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ossfuzz/travisoss.sh b/ossfuzz/travisoss.sh index 218f4d6..eae9a80 100755 --- a/ossfuzz/travisoss.sh +++ b/ossfuzz/travisoss.sh @@ -12,7 +12,7 @@ then fi # Modify the oss-fuzz Dockerfile so that we're checking out the current branch on travis. -if [ "x${TRAVIS_PULL_REQUEST}" -eq "xfalse" ] +if [ "x${TRAVIS_PULL_REQUEST}" = "xfalse" ] then sed -i "s@https://github.com/lz4/lz4.git@-b ${TRAVIS_BRANCH} https://github.com/lz4/lz4.git@" /tmp/ossfuzz/projects/lz4/Dockerfile else -- cgit v1.2.3 From 86edeb6784c25d5a952a9e5d33b25846f444375c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niklas=20Hamb=C3=BCchen?= Date: Tue, 25 Aug 2020 03:05:01 +0200 Subject: fixup: C90 compliance, clean up stat() types/calls --- programs/util.h | 58 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/programs/util.h b/programs/util.h index 4b2c09a..6658bf9 100644 --- a/programs/util.h +++ b/programs/util.h @@ -122,6 +122,27 @@ extern "C" { #endif +/*-**************************************** +* stat() functions +******************************************/ +#if defined(_MSC_VER) +# define UTIL_TYPE_stat __stat64 +# define UTIL_stat _stat64 +# define UTIL_fstat _fstat64 +# define UTIL_STAT_MODE_ISREG(st_mode) ((st_mode) & S_IFREG) +#elif defined(__MINGW32__) && defined (__MSVCRT__) +# define UTIL_TYPE_stat _stati64 +# define UTIL_stat _stati64 +# define UTIL_fstat _fstati64 +# define UTIL_STAT_MODE_ISREG(st_mode) ((st_mode) & S_IFREG) +#else +# define UTIL_TYPE_stat stat +# define UTIL_stat stat +# define UTIL_fstat fstat +# define UTIL_STAT_MODE_ISREG(st_mode) (S_ISREG(st_mode)) +#endif + + /* ************************************* * Constants ***************************************/ @@ -360,24 +381,16 @@ UTIL_STATIC U32 UTIL_isDirectory(const char* infilename) UTIL_STATIC U64 UTIL_getOpenFileSize(FILE* file) { int r; - int fd = fileno(file); + int fd; + struct UTIL_TYPE_stat statbuf; + + fd = fileno(file); if (fd < 0) { perror("fileno"); exit(1); } -#if defined(_MSC_VER) - struct __stat64 statbuf; - r = _fstat64(fd, &statbuf); - if (r || !(statbuf.st_mode & S_IFREG)) return 0; /* No good... */ -#elif defined(__MINGW32__) && defined (__MSVCRT__) - struct _stati64 statbuf; - r = _fstati64(fd, &statbuf); - if (r || !(statbuf.st_mode & S_IFREG)) return 0; /* No good... */ -#else - struct stat statbuf; - r = fstat(fd, &statbuf); - if (r || !S_ISREG(statbuf.st_mode)) return 0; /* No good... */ -#endif + r = UTIL_fstat(fd, &statbuf); + if (r || !UTIL_STAT_MODE_ISREG(statbuf.st_mode)) return 0; /* No good... */ return (U64)statbuf.st_size; } @@ -385,19 +398,10 @@ UTIL_STATIC U64 UTIL_getOpenFileSize(FILE* file) UTIL_STATIC U64 UTIL_getFileSize(const char* infilename) { int r; -#if defined(_MSC_VER) - struct __stat64 statbuf; - r = _stat64(infilename, &statbuf); - if (r || !(statbuf.st_mode & S_IFREG)) return 0; /* No good... */ -#elif defined(__MINGW32__) && defined (__MSVCRT__) - struct _stati64 statbuf; - r = _stati64(infilename, &statbuf); - if (r || !(statbuf.st_mode & S_IFREG)) return 0; /* No good... */ -#else - struct stat statbuf; - r = stat(infilename, &statbuf); - if (r || !S_ISREG(statbuf.st_mode)) return 0; /* No good... */ -#endif + struct UTIL_TYPE_stat statbuf; + + r = UTIL_stat(infilename, &statbuf); + if (r || !UTIL_STAT_MODE_ISREG(statbuf.st_mode)) return 0; /* No good... */ return (U64)statbuf.st_size; } -- cgit v1.2.3 From a58b7ac6ce71819b712d0663f1d37bfb3cdd161c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niklas=20Hamb=C3=BCchen?= Date: Tue, 25 Aug 2020 03:37:41 +0200 Subject: fixup: Portable fileno() --- programs/util.h | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/programs/util.h b/programs/util.h index 6658bf9..2f3d0a8 100644 --- a/programs/util.h +++ b/programs/util.h @@ -143,6 +143,15 @@ extern "C" { #endif +/*-**************************************** +* fileno() function +******************************************/ +#if defined(_MSC_VER) +# define UTIL_fileno _fileno +#else +# define UTIL_fileno fileno +#endif + /* ************************************* * Constants ***************************************/ @@ -384,7 +393,7 @@ UTIL_STATIC U64 UTIL_getOpenFileSize(FILE* file) int fd; struct UTIL_TYPE_stat statbuf; - fd = fileno(file); + fd = UTIL_fileno(file); if (fd < 0) { perror("fileno"); exit(1); -- cgit v1.2.3 From 65f081ccaffce6753ec02c53c7b6413deddf2eb9 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 25 Aug 2020 14:25:51 -0700 Subject: use variables for program invocation --- tests/Makefile | 162 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 82 insertions(+), 80 deletions(-) diff --git a/tests/Makefile b/tests/Makefile index 05cae15..6f29020 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -146,7 +146,9 @@ ifneq (,$(filter $(shell uname),SunOS)) DIFF:=gdiff endif +CAT:=cat DD:=dd +DATAGEN:=./datagen .PHONY: list list: @@ -168,14 +170,14 @@ lz4_all.o: lz4_all.c $(CC) $(CFLAGS) $(CPPFLAGS) -c $^ -o $@ lz4_all.c: $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c $(LZ4DIR)/lz4frame.c - cat $^ > $@ + $(CAT) $^ > $@ test-install: lz4 lib liblz4.pc lz4_root=.. ./test_install.sh test-lz4-sparse: lz4 datagen @echo "\n ---- test sparse file support ----" - ./datagen -g5M -P100 > tmplsdg5M + $(DATAGEN) -g5M -P100 > tmplsdg5M $(LZ4) -B4D tmplsdg5M -c | $(LZ4) -dv --sparse > tmplscB4 $(DIFF) -s tmplsdg5M tmplscB4 $(LZ4) -B5D tmplsdg5M -c | $(LZ4) -dv --sparse > tmplscB5 @@ -187,17 +189,17 @@ test-lz4-sparse: lz4 datagen $(LZ4) tmplsdg5M -c | $(LZ4) -dv --no-sparse > tmplsnosparse $(DIFF) -s tmplsdg5M tmplsnosparse ls -ls tmpls* - ./datagen -s1 -g1200007 -P100 | $(LZ4) | $(LZ4) -dv --sparse > tmplsodd # Odd size file (to generate non-full last block) - ./datagen -s1 -g1200007 -P100 | $(DIFF) -s - tmplsodd + $(DATAGEN) -s1 -g1200007 -P100 | $(LZ4) | $(LZ4) -dv --sparse > tmplsodd # Odd size file (to generate non-full last block) + $(DATAGEN) -s1 -g1200007 -P100 | $(DIFF) -s - tmplsodd ls -ls tmplsodd @$(RM) tmpls* @echo "\n Compatibility with Console :" echo "Hello World 1 !" | $(LZ4) | $(LZ4) -d -c - echo "Hello World 2 !" | $(LZ4) | $(LZ4) -d | cat + echo "Hello World 2 !" | $(LZ4) | $(LZ4) -d | $(CAT) echo "Hello World 3 !" | $(LZ4) --no-frame-crc | $(LZ4) -d -c @echo "\n Compatibility with Append :" - ./datagen -P100 -g1M > tmplsdg1M - cat tmplsdg1M tmplsdg1M > tmpls2M + $(DATAGEN) -P100 -g1M > tmplsdg1M + $(CAT) tmplsdg1M tmplsdg1M > tmpls2M $(LZ4) -B5 -v tmplsdg1M tmplsc $(LZ4) -d -v tmplsc tmplsr $(LZ4) -d -v tmplsc -c >> tmplsr @@ -207,7 +209,7 @@ test-lz4-sparse: lz4 datagen test-lz4-contentSize: lz4 datagen @echo "\n ---- test original size support ----" - ./datagen -g15M > tmplc1 + $(DATAGEN) -g15M > tmplc1 $(LZ4) -v tmplc1 -c | $(LZ4) -t $(LZ4) -v --content-size tmplc1 -c | $(LZ4) -d > tmplc2 $(DIFF) -s tmplc1 tmplc2 @@ -217,10 +219,10 @@ test-lz4-frame-concatenation: lz4 datagen @echo "\n ---- test frame concatenation ----" @echo -n > tmp-lfc-empty @echo hi > tmp-lfc-nonempty - cat tmp-lfc-nonempty tmp-lfc-empty tmp-lfc-nonempty > tmp-lfc-src + $(CAT) tmp-lfc-nonempty tmp-lfc-empty tmp-lfc-nonempty > tmp-lfc-src $(LZ4) -zq tmp-lfc-empty -c > tmp-lfc-empty.lz4 $(LZ4) -zq tmp-lfc-nonempty -c > tmp-lfc-nonempty.lz4 - cat tmp-lfc-nonempty.lz4 tmp-lfc-empty.lz4 tmp-lfc-nonempty.lz4 > tmp-lfc-concat.lz4 + $(CAT) tmp-lfc-nonempty.lz4 tmp-lfc-empty.lz4 tmp-lfc-nonempty.lz4 > tmp-lfc-concat.lz4 $(LZ4) -d tmp-lfc-concat.lz4 -c > tmp-lfc-result $(CMP) tmp-lfc-src tmp-lfc-result @$(RM) tmp-lfc-* @@ -228,9 +230,9 @@ test-lz4-frame-concatenation: lz4 datagen test-lz4-multiple: lz4 datagen @echo "\n ---- test multiple files ----" - @./datagen -s1 > tmp-tlm1 2> $(VOID) - @./datagen -s2 -g100K > tmp-tlm2 2> $(VOID) - @./datagen -s3 -g200K > tmp-tlm3 2> $(VOID) + @$(DATAGEN) -s1 > tmp-tlm1 2> $(VOID) + @$(DATAGEN) -s2 -g100K > tmp-tlm2 2> $(VOID) + @$(DATAGEN) -s3 -g200K > tmp-tlm3 2> $(VOID) # compress multiple files : one .lz4 per source file $(LZ4) -f -m tmp-tlm* test -f tmp-tlm1.lz4 @@ -245,7 +247,7 @@ test-lz4-multiple: lz4 datagen $(CMP) tmp-tlm2 tmp-tlm2-orig $(CMP) tmp-tlm3 tmp-tlm3-orig # compress multiple files into stdout - cat tmp-tlm1.lz4 tmp-tlm2.lz4 tmp-tlm3.lz4 > tmp-tlm-concat1 + $(CAT) tmp-tlm1.lz4 tmp-tlm2.lz4 tmp-tlm3.lz4 > tmp-tlm-concat1 $(RM) *.lz4 $(LZ4) -m tmp-tlm1 tmp-tlm2 tmp-tlm3 -c > tmp-tlm-concat2 test ! -f tmp-tlm1.lz4 # must not create .lz4 artefact @@ -253,7 +255,7 @@ test-lz4-multiple: lz4 datagen # decompress multiple files into stdout $(RM) tmp-tlm-concat1 tmp-tlm-concat2 $(LZ4) -f -m tmp-tlm1 tmp-tlm2 tmp-tlm3 # generate .lz4 to decompress - cat tmp-tlm1 tmp-tlm2 tmp-tlm3 > tmp-tlm-concat1 # create concatenated reference + $(CAT) tmp-tlm1 tmp-tlm2 tmp-tlm3 > tmp-tlm-concat1 # create concatenated reference $(RM) tmp-tlm1 tmp-tlm2 tmp-tlm3 $(LZ4) -d -m tmp-tlm1.lz4 tmp-tlm2.lz4 tmp-tlm3.lz4 -c > tmp-tlm-concat2 test ! -f tmp-tlm1 # must not create file artefact @@ -264,9 +266,9 @@ test-lz4-multiple: lz4 datagen test-lz4-multiple-legacy: lz4 datagen @echo "\n ---- test multiple files (Legacy format) ----" - @./datagen -s1 > tmp-tlm1 2> $(VOID) - @./datagen -s2 -g100K > tmp-tlm2 2> $(VOID) - @./datagen -s3 -g200K > tmp-tlm3 2> $(VOID) + @$(DATAGEN) -s1 > tmp-tlm1 2> $(VOID) + @$(DATAGEN) -s2 -g100K > tmp-tlm2 2> $(VOID) + @$(DATAGEN) -s3 -g200K > tmp-tlm3 2> $(VOID) # compress multiple files using legacy format: one .lz4 per source file $(LZ4) -f -l -m tmp-tlm* test -f tmp-tlm1.lz4 @@ -282,7 +284,7 @@ test-lz4-multiple-legacy: lz4 datagen $(CMP) tmp-tlm2 tmp-tlm2-orig $(CMP) tmp-tlm3 tmp-tlm3-orig # compress multiple files into stdout using legacy format - cat tmp-tlm1.lz4 tmp-tlm2.lz4 tmp-tlm3.lz4 > tmp-tlm-concat1 + $(CAT) tmp-tlm1.lz4 tmp-tlm2.lz4 tmp-tlm3.lz4 > tmp-tlm-concat1 $(RM) *.lz4 $(LZ4) -l -m tmp-tlm1 tmp-tlm2 tmp-tlm3 -c > tmp-tlm-concat2 test ! -f tmp-tlm1.lz4 # must not create .lz4 artefact @@ -290,7 +292,7 @@ test-lz4-multiple-legacy: lz4 datagen # # # decompress multiple files into stdout using legacy format $(RM) tmp-tlm-concat1 tmp-tlm-concat2 $(LZ4) -l -f -m tmp-tlm1 tmp-tlm2 tmp-tlm3 # generate .lz4 to decompress - cat tmp-tlm1 tmp-tlm2 tmp-tlm3 > tmp-tlm-concat1 # create concatenated reference + $(CAT) tmp-tlm1 tmp-tlm2 tmp-tlm3 > tmp-tlm-concat1 # create concatenated reference $(RM) tmp-tlm1 tmp-tlm2 tmp-tlm3 $(LZ4) -d -m tmp-tlm1.lz4 tmp-tlm2.lz4 tmp-tlm3.lz4 -c > tmp-tlm-concat2 $(LZ4) -d -l -m tmp-tlm1.lz4 tmp-tlm2.lz4 tmp-tlm3.lz4 -c > tmp-tlm-concat2 # -l mustn't impact option -d @@ -302,18 +304,18 @@ test-lz4-multiple-legacy: lz4 datagen test-lz4-basic: lz4 datagen unlz4 lz4cat @echo "\n ---- test lz4 basic compression/decompression ----" - ./datagen -g0 | $(LZ4) -v | $(LZ4) -t - ./datagen -g16KB | $(LZ4) -9 | $(LZ4) -t - ./datagen -g20KB > tmp-tlb-dg20k + $(DATAGEN) -g0 | $(LZ4) -v | $(LZ4) -t + $(DATAGEN) -g16KB | $(LZ4) -9 | $(LZ4) -t + $(DATAGEN) -g20KB > tmp-tlb-dg20k $(LZ4) < tmp-tlb-dg20k | $(LZ4) -d > tmp-tlb-dec $(DIFF) -q tmp-tlb-dg20k tmp-tlb-dec $(LZ4) --no-frame-crc < tmp-tlb-dg20k | $(LZ4) -d > tmp-tlb-dec $(DIFF) -q tmp-tlb-dg20k tmp-tlb-dec - ./datagen | $(LZ4) -BI | $(LZ4) -t - ./datagen -g6M -P99 | $(LZ4) -9BD | $(LZ4) -t - ./datagen -g17M | $(LZ4) -9v | $(LZ4) -qt - ./datagen -g33M | $(LZ4) --no-frame-crc | $(LZ4) -t - ./datagen -g256MB | $(LZ4) -vqB4D | $(LZ4) -t + $(DATAGEN) | $(LZ4) -BI | $(LZ4) -t + $(DATAGEN) -g6M -P99 | $(LZ4) -9BD | $(LZ4) -t + $(DATAGEN) -g17M | $(LZ4) -9v | $(LZ4) -qt + $(DATAGEN) -g33M | $(LZ4) --no-frame-crc | $(LZ4) -t + $(DATAGEN) -g256MB | $(LZ4) -vqB4D | $(LZ4) -t @echo "hello world" > tmp-tlb-hw $(LZ4) --rm -f tmp-tlb-hw tmp-tlb-hw.lz4 test ! -f tmp-tlb-hw # must fail (--rm) @@ -342,18 +344,18 @@ test-lz4-basic: lz4 datagen unlz4 lz4cat $(DIFF) -q tmp-tlb-hw tmp-tlb4 $(LZ4) -f tmp-tlb-hw $(LZ4) --list tmp-tlb-hw.lz4 # test --list on valid single-frame file - cat tmp-tlb-hw >> tmp-tlb-hw.lz4 + $(CAT) tmp-tlb-hw >> tmp-tlb-hw.lz4 $(LZ4) -f tmp-tlb-hw.lz4 # uncompress valid frame followed by invalid data $(LZ4) -BX tmp-tlb-hw -c -q | $(LZ4) -tv # test block checksum - # ./datagen -g20KB generates the same file every single time - # cannot save output of ./datagen -g20KB as input file to lz4 because the following shell commands are run before ./datagen -g20KB - test "$(shell ./datagen -g20KB | $(LZ4) -c --fast | wc -c)" -lt "$(shell ./datagen -g20KB | $(LZ4) -c --fast=9 | wc -c)" # -1 vs -9 - test "$(shell ./datagen -g20KB | $(LZ4) -c -1 | wc -c)" -lt "$(shell ./datagen -g20KB| $(LZ4) -c --fast=1 | wc -c)" # 1 vs -1 - test "$(shell ./datagen -g20KB | $(LZ4) -c --fast=1 | wc -c)" -eq "$(shell ./datagen -g20KB| $(LZ4) -c --fast| wc -c)" # checks default fast compression is -1 + # $(DATAGEN) -g20KB generates the same file every single time + # cannot save output of $(DATAGEN) -g20KB as input file to lz4 because the following shell commands are run before $(DATAGEN) -g20KB + test "$(shell $(DATAGEN) -g20KB | $(LZ4) -c --fast | wc -c)" -lt "$(shell $(DATAGEN) -g20KB | $(LZ4) -c --fast=9 | wc -c)" # -1 vs -9 + test "$(shell $(DATAGEN) -g20KB | $(LZ4) -c -1 | wc -c)" -lt "$(shell $(DATAGEN) -g20KB| $(LZ4) -c --fast=1 | wc -c)" # 1 vs -1 + test "$(shell $(DATAGEN) -g20KB | $(LZ4) -c --fast=1 | wc -c)" -eq "$(shell $(DATAGEN) -g20KB| $(LZ4) -c --fast| wc -c)" # checks default fast compression is -1 ! $(LZ4) -c --fast=0 tmp-tlb-dg20K # lz4 should fail when fast=0 ! $(LZ4) -c --fast=-1 tmp-tlb-dg20K # lz4 should fail when fast=-1 # High --fast values can result in out-of-bound dereferences #876 - ./datagen -g1M | $(LZ4) -c --fast=999999999 > /dev/null + $(DATAGEN) -g1M | $(LZ4) -c --fast=999999999 > /dev/null # Test for #596 @echo "TEST" > tmp-tlb-test $(LZ4) -m tmp-tlb-test @@ -365,10 +367,10 @@ test-lz4-basic: lz4 datagen unlz4 lz4cat test-lz4-dict: lz4 datagen @echo "\n ---- test lz4 compression/decompression with dictionary ----" - ./datagen -g16KB > tmp-dict - ./datagen -g32KB > tmp-dict-sample-32k + $(DATAGEN) -g16KB > tmp-dict + $(DATAGEN) -g32KB > tmp-dict-sample-32k < tmp-dict-sample-32k $(LZ4) -D tmp-dict | $(LZ4) -dD tmp-dict | diff - tmp-dict-sample-32k - ./datagen -g128MB > tmp-dict-sample-128m + $(DATAGEN) -g128MB > tmp-dict-sample-128m < tmp-dict-sample-128m $(LZ4) -D tmp-dict | $(LZ4) -dD tmp-dict | diff - tmp-dict-sample-128m touch tmp-dict-sample-0 < tmp-dict-sample-0 $(LZ4) -D tmp-dict | $(LZ4) -dD tmp-dict | diff - tmp-dict-sample-0 @@ -377,10 +379,10 @@ test-lz4-dict: lz4 datagen < tmp-dict-sample-0 $(LZ4) -D tmp-dict-sample-0 | $(LZ4) -dD tmp-dict-sample-0 | diff - tmp-dict-sample-0 @echo "\n ---- test lz4 dictionary loading ----" - ./datagen -g128KB > tmp-dict-data-128KB + $(DATAGEN) -g128KB > tmp-dict-data-128KB set -e; \ for l in 0 1 4 128 32767 32768 32769 65535 65536 65537 98303 98304 98305 131071 131072 131073; do \ - ./datagen -g$$l > tmp-dict-$$l; \ + $(DATAGEN) -g$$l > tmp-dict-$$l; \ $(DD) if=tmp-dict-$$l of=tmp-dict-$$l-tail bs=1 count=65536 skip=$$((l > 65536 ? l - 65536 : 0)); \ < tmp-dict-$$l $(LZ4) -D stdin tmp-dict-data-128KB -c | $(LZ4) -dD tmp-dict-$$l-tail | $(DIFF) - tmp-dict-data-128KB; \ < tmp-dict-$$l-tail $(LZ4) -D stdin tmp-dict-data-128KB -c | $(LZ4) -dD tmp-dict-$$l | $(DIFF) - tmp-dict-data-128KB; \ @@ -390,12 +392,12 @@ test-lz4-dict: lz4 datagen test-lz4-hugefile: lz4 datagen @echo "\n ---- test huge files compression/decompression ----" - ./datagen -g6GB | $(LZ4) -vB5D | $(LZ4) -qt - ./datagen -g5GB | $(LZ4) -v4BD | $(LZ4) -qt + $(DATAGEN) -g6GB | $(LZ4) -vB5D | $(LZ4) -qt + $(DATAGEN) -g5GB | $(LZ4) -v4BD | $(LZ4) -qt # test large file size [2-4] GB - @./datagen -g3G -P100 | $(LZ4) -vv | $(LZ4) --decompress --force --sparse - tmphf1 + @$(DATAGEN) -g3G -P100 | $(LZ4) -vv | $(LZ4) --decompress --force --sparse - tmphf1 @ls -ls tmphf1 - @./datagen -g3G -P100 | $(LZ4) --quiet --content-size | $(LZ4) --verbose --decompress --force --sparse - tmphf2 + @$(DATAGEN) -g3G -P100 | $(LZ4) --quiet --content-size | $(LZ4) --verbose --decompress --force --sparse - tmphf2 @ls -ls tmphf2 $(DIFF) -s tmphf1 tmphf2 @$(RM) tmphf* @@ -404,14 +406,14 @@ test-lz4-testmode: lz4 datagen @echo "\n ---- bench mode ----" $(LZ4) -bi1 @echo "\n ---- test mode ----" - ! ./datagen | $(LZ4) -t - ! ./datagen | $(LZ4) -tf + ! $(DATAGEN) | $(LZ4) -t + ! $(DATAGEN) | $(LZ4) -tf @echo "\n ---- pass-through mode ----" @echo "Why hello there " > tmp-tlt2.lz4 ! $(LZ4) -f tmp-tlt2.lz4 > $(VOID) - ! ./datagen | $(LZ4) -dc > $(VOID) - ! ./datagen | $(LZ4) -df > $(VOID) - ./datagen | $(LZ4) -dcf > $(VOID) + ! $(DATAGEN) | $(LZ4) -dc > $(VOID) + ! $(DATAGEN) | $(LZ4) -df > $(VOID) + $(DATAGEN) | $(LZ4) -dcf > $(VOID) @echo "Hello World !" > tmp-tlt1 $(LZ4) -dcf tmp-tlt1 @echo "from underground..." > tmp-tlt2 @@ -425,16 +427,16 @@ test-lz4-testmode: lz4 datagen test-lz4-opt-parser: lz4 datagen @echo "\n ---- test opt-parser ----" - ./datagen -g16KB | $(LZ4) -12 | $(LZ4) -t - ./datagen -P10 | $(LZ4) -12B4 | $(LZ4) -t - ./datagen -g256K | $(LZ4) -12B4D | $(LZ4) -t - ./datagen -g512K -P25 | $(LZ4) -12BD | $(LZ4) -t - ./datagen -g1M | $(LZ4) -12B5 | $(LZ4) -t - ./datagen -g2M -P99 | $(LZ4) -11B4D | $(LZ4) -t - ./datagen -g4M | $(LZ4) -11vq | $(LZ4) -qt - ./datagen -g8M | $(LZ4) -11B4 | $(LZ4) -t - ./datagen -g16M -P90 | $(LZ4) -11B5 | $(LZ4) -t - ./datagen -g32M -P10 | $(LZ4) -11B5D | $(LZ4) -t + $(DATAGEN) -g16KB | $(LZ4) -12 | $(LZ4) -t + $(DATAGEN) -P10 | $(LZ4) -12B4 | $(LZ4) -t + $(DATAGEN) -g256K | $(LZ4) -12B4D | $(LZ4) -t + $(DATAGEN) -g512K -P25 | $(LZ4) -12BD | $(LZ4) -t + $(DATAGEN) -g1M | $(LZ4) -12B5 | $(LZ4) -t + $(DATAGEN) -g2M -P99 | $(LZ4) -11B4D | $(LZ4) -t + $(DATAGEN) -g4M | $(LZ4) -11vq | $(LZ4) -qt + $(DATAGEN) -g8M | $(LZ4) -11B4 | $(LZ4) -t + $(DATAGEN) -g16M -P90 | $(LZ4) -11B5 | $(LZ4) -t + $(DATAGEN) -g32M -P10 | $(LZ4) -11B5D | $(LZ4) -t test-lz4-essentials : lz4 datagen test-lz4-basic test-lz4-multiple test-lz4-multiple-legacy \ test-lz4-frame-concatenation test-lz4-testmode \ @@ -447,35 +449,35 @@ test-lz4: lz4 datagen test-lz4-essentials test-lz4-opt-parser \ test-lz4c: lz4c datagen @echo "\n ---- test lz4c variant ----" - ./datagen -g256MB | $(LZ4)c -l -v | $(LZ4)c -t + $(DATAGEN) -g256MB | $(LZ4)c -l -v | $(LZ4)c -t test-lz4c32: CFLAGS+=-m32 test-lz4c32: test-lz4 test-interop-32-64: lz4 lz4c32 datagen @echo "\n ---- test interoperability 32-bits -vs- 64 bits ----" - ./datagen -g16KB | $(LZ4)c32 -9 | $(LZ4) -t - ./datagen -P10 | $(LZ4) -9B4 | $(LZ4)c32 -t - ./datagen | $(LZ4)c32 | $(LZ4) -t - ./datagen -g1M | $(LZ4) -3B5 | $(LZ4)c32 -t - ./datagen -g256MB | $(LZ4)c32 -vqB4D | $(LZ4) -qt - ./datagen -g1G -P90 | $(LZ4) | $(LZ4)c32 -t - ./datagen -g6GB | $(LZ4)c32 -vq9BD | $(LZ4) -qt + $(DATAGEN) -g16KB | $(LZ4)c32 -9 | $(LZ4) -t + $(DATAGEN) -P10 | $(LZ4) -9B4 | $(LZ4)c32 -t + $(DATAGEN) | $(LZ4)c32 | $(LZ4) -t + $(DATAGEN) -g1M | $(LZ4) -3B5 | $(LZ4)c32 -t + $(DATAGEN) -g256MB | $(LZ4)c32 -vqB4D | $(LZ4) -qt + $(DATAGEN) -g1G -P90 | $(LZ4) | $(LZ4)c32 -t + $(DATAGEN) -g6GB | $(LZ4)c32 -vq9BD | $(LZ4) -qt test-lz4c32-basic: lz4c32 datagen @echo "\n ---- test lz4c32 32-bits version ----" - ./datagen -g16KB | $(LZ4)c32 -9 | $(LZ4)c32 -t - ./datagen | $(LZ4)c32 | $(LZ4)c32 -t - ./datagen -g256MB | $(LZ4)c32 -vqB4D | $(LZ4)c32 -qt - ./datagen -g6GB | $(LZ4)c32 -vqB5D | $(LZ4)c32 -qt + $(DATAGEN) -g16KB | $(LZ4)c32 -9 | $(LZ4)c32 -t + $(DATAGEN) | $(LZ4)c32 | $(LZ4)c32 -t + $(DATAGEN) -g256MB | $(LZ4)c32 -vqB4D | $(LZ4)c32 -qt + $(DATAGEN) -g6GB | $(LZ4)c32 -vqB5D | $(LZ4)c32 -qt test-platform: @echo "\n ---- test lz4 $(QEMU_SYS) platform ----" - $(QEMU_SYS) ./datagen -g16KB | $(QEMU_SYS) $(LZ4) -9 | $(QEMU_SYS) $(LZ4) -t - $(QEMU_SYS) ./datagen | $(QEMU_SYS) $(LZ4) | $(QEMU_SYS) $(LZ4) -t - $(QEMU_SYS) ./datagen -g256MB | $(QEMU_SYS) $(LZ4) -vqB4D | $(QEMU_SYS) $(LZ4) -qt + $(QEMU_SYS) $(DATAGEN) -g16KB | $(QEMU_SYS) $(LZ4) -9 | $(QEMU_SYS) $(LZ4) -t + $(QEMU_SYS) $(DATAGEN) | $(QEMU_SYS) $(LZ4) | $(QEMU_SYS) $(LZ4) -t + $(QEMU_SYS) $(DATAGEN) -g256MB | $(QEMU_SYS) $(LZ4) -vqB4D | $(QEMU_SYS) $(LZ4) -qt ifneq ($(QEMU_SYS),qemu-arm-static) - $(QEMU_SYS) ./datagen -g3GB | $(QEMU_SYS) $(LZ4) -vqB5D | $(QEMU_SYS) $(LZ4) -qt + $(QEMU_SYS) $(DATAGEN) -g3GB | $(QEMU_SYS) $(LZ4) -vqB5D | $(QEMU_SYS) $(LZ4) -qt endif test-fullbench: fullbench @@ -498,13 +500,13 @@ test-frametest32: test-frametest test-mem: lz4 datagen fuzzer frametest fullbench @echo "\n ---- valgrind tests : memory analyzer ----" - valgrind --leak-check=yes --error-exitcode=1 ./datagen -g50M > $(VOID) - ./datagen -g16KB > ftmdg16K + valgrind --leak-check=yes --error-exitcode=1 $(DATAGEN) -g50M > $(VOID) + $(DATAGEN) -g16KB > ftmdg16K valgrind --leak-check=yes --error-exitcode=1 $(LZ4) -9 -BD -f ftmdg16K $(VOID) - ./datagen -g16KB -s2 > ftmdg16K2 - ./datagen -g16KB -s3 > ftmdg16K3 + $(DATAGEN) -g16KB -s2 > ftmdg16K2 + $(DATAGEN) -g16KB -s3 > ftmdg16K3 valgrind --leak-check=yes --error-exitcode=1 $(LZ4) --force --multiple ftmdg16K ftmdg16K2 ftmdg16K3 - ./datagen -g7MB > ftmdg7M + $(DATAGEN) -g7MB > ftmdg7M valgrind --leak-check=yes --error-exitcode=1 $(LZ4) -9 -B5D -f ftmdg7M ftmdg16K2 valgrind --leak-check=yes --error-exitcode=1 $(LZ4) -t ftmdg16K2 valgrind --leak-check=yes --error-exitcode=1 $(LZ4) -bi1 ftmdg7M -- cgit v1.2.3 From 224735ea1b7b224e14caf2e39b21854c439a7179 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 25 Aug 2020 14:42:15 -0700 Subject: added more tests around --content-size notably in association with `stdin` --- tests/Makefile | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tests/Makefile b/tests/Makefile index 6f29020..2fe9e69 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -212,7 +212,16 @@ test-lz4-contentSize: lz4 datagen $(DATAGEN) -g15M > tmplc1 $(LZ4) -v tmplc1 -c | $(LZ4) -t $(LZ4) -v --content-size tmplc1 -c | $(LZ4) -d > tmplc2 - $(DIFF) -s tmplc1 tmplc2 + $(DIFF) tmplc1 tmplc2 + $(LZ4) -f tmplc1 + $(LZ4) --content-size tmplc1 -c > tmplc2.lz4 + ! $(DIFF) tmplc1.lz4 tmplc2.lz4 # must differ, due to content size + $(LZ4) --content-size < tmplc1 > tmplc3.lz4 + $(DIFF) tmplc2.lz4 tmplc3.lz4 # both must contain content size + $(CAT) tmplc1 | $(LZ4) > tmplc4.lz4 + $(DIFF) tmplc1.lz4 tmplc4.lz4 # both don't have content size + $(CAT) tmplc1 | $(LZ4) --content-size > tmplc5.lz4 # can't determine content size + $(DIFF) tmplc1.lz4 tmplc5.lz4 # both don't have content size @$(RM) tmplc* test-lz4-frame-concatenation: lz4 datagen -- cgit v1.2.3 From 5b3f9453108f94aef856304db8146cde54bdcbb8 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 25 Aug 2020 14:53:08 -0700 Subject: fix test under non-interactive environment --- tests/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Makefile b/tests/Makefile index 2fe9e69..866ff5d 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -213,7 +213,7 @@ test-lz4-contentSize: lz4 datagen $(LZ4) -v tmplc1 -c | $(LZ4) -t $(LZ4) -v --content-size tmplc1 -c | $(LZ4) -d > tmplc2 $(DIFF) tmplc1 tmplc2 - $(LZ4) -f tmplc1 + $(LZ4) -f tmplc1 -c > tmplc1.lz4 $(LZ4) --content-size tmplc1 -c > tmplc2.lz4 ! $(DIFF) tmplc1.lz4 tmplc2.lz4 # must differ, due to content size $(LZ4) --content-size < tmplc1 > tmplc3.lz4 -- cgit v1.2.3 From 5243173b23b4deff16a5685ebfcf493e896a756e Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 25 Aug 2020 22:17:29 -0700 Subject: added documentation about LZ4_FORCE_SW_BITCOUNT Also : added memory-frugal software byte count for big endian 64-bit cpus. Disabled by default. --- lib/README.md | 20 ++++++++++++++------ lib/lz4.c | 21 +++++++++++++++++++-- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/lib/README.md b/lib/README.md index 707d777..3653c81 100644 --- a/lib/README.md +++ b/lib/README.md @@ -46,11 +46,11 @@ and `LZ4F_PUBLISH_STATIC_FUNCTIONS`. #### Build macros -The following build macro can be selected at compilation time : +The following build macro can be selected to adjust source code behavior at compilation time : -- `LZ4_FAST_DEC_LOOP` : this triggers the optimized decompression loop. - This loops works great on x86/x64 cpus, and is automatically enabled on this platform. - It's possible to enable or disable it manually, by passing `LZ4_FAST_DEC_LOOP=1` or `0` to the preprocessor. +- `LZ4_FAST_DEC_LOOP` : this triggers a speed optimized decompression loop, more powerful on modern cpus. + This loop works great on x86, x64 and aarch64 cpus, and is automatically enabled for them. + It's also possible to enable or disable it manually, by passing `LZ4_FAST_DEC_LOOP=1` or `0` to the preprocessor. For example, with `gcc` : `-DLZ4_FAST_DEC_LOOP=1`, and with `make` : `CPPFLAGS+=-DLZ4_FAST_DEC_LOOP=1 make lz4`. @@ -66,9 +66,17 @@ The following build macro can be selected at compilation time : Should this be a problem, it's generally possible to make the compiler ignore these warnings, for example with `-Wno-deprecated-declarations` on `gcc`, or `_CRT_SECURE_NO_WARNINGS` for Visual Studio. - Another method is to define `LZ4_DISABLE_DEPRECATE_WARNINGS` + Another project-specific method is to define `LZ4_DISABLE_DEPRECATE_WARNINGS` before including the LZ4 header files. +- `LZ4_FORCE_SW_BITCOUNT` : by default, the compression algorithm tries to determine lengths + by using bitcount instructions, generally implemented as fast single instructions in many cpus. + In case the target cpus doesn't support it, or compiler intrinsic doesn't work, or feature bad performance, + it's possible to use an optimized software path instead. + This is achieved by setting this build macros . + In most cases, it's not expected to be necessary, + but it can be legitimately considered for less common platforms. + #### Amalgamation @@ -103,7 +111,7 @@ The compiled executable will require LZ4 DLL which is available at `dll\liblz4.d #### Miscellaneous -Other files present in the directory are not source code. There are : +Other files present in the directory are not source code. They are : - `LICENSE` : contains the BSD license text - `Makefile` : `make` script to compile and install lz4 library (static and dynamic) diff --git a/lib/lz4.c b/lib/lz4.c index e10b58e..0ca7b21 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -88,6 +88,7 @@ * Define this parameter if your target system or compiler does not support hardware bit count */ #if defined(_MSC_VER) && defined(_WIN32_WCE) /* Visual Studio for WinCE doesn't support Hardware bit count */ +# undef LZ4_FORCE_SW_BITCOUNT /* avoid double def */ # define LZ4_FORCE_SW_BITCOUNT #endif @@ -527,6 +528,9 @@ static unsigned LZ4_NbCommonBytes (reg_t val) !defined(LZ4_FORCE_SW_BITCOUNT) return (unsigned)__builtin_clzll((U64)val) >> 3; # else +#if 1 + /* this method is probably faster, + * but adds a 128 bytes lookup table */ static const unsigned char ctz7_tab[128] = { 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, @@ -537,9 +541,22 @@ static unsigned LZ4_NbCommonBytes (reg_t val) 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, }; - const U64 mask = 0x0101010101010101ULL; - U64 t = (((val >> 8) - mask) | val) & mask; + U64 const mask = 0x0101010101010101ULL; + U64 const t = (((val >> 8) - mask) | val) & mask; return ctz7_tab[(t * 0x0080402010080402ULL) >> 57]; +#else + /* this method doesn't consume memory space like the previous one, + * but it contains several branches, + * that may end up slowing execution */ + static const U32 by32 = sizeof(val)*4; /* 32 on 64 bits (goal), 16 on 32 bits. + Just to avoid some static analyzer complaining about shift by 32 on 32-bits target. + Note that this code path is never triggered in 32-bits mode. */ + unsigned r; + if (!(val>>by32)) { r=4; } else { r=0; val>>=by32; } + if (!(val>>16)) { r+=2; val>>=8; } else { val>>=24; } + r += (!val); + return r; +#endif # endif } else /* 32 bits */ { # if (defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 3) || \ -- cgit v1.2.3 From c5d6f8a8be3927c0bec91bcc58667a6cfad244ad Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 27 Aug 2020 00:17:57 -0700 Subject: fix #783 LZ4_decompress_safe_partial() now also supports a scenario where nb_bytes_to_generate is <= block_decompressed_size And nb_bytes_to_read is >= block_compressed_size. Previously, the only supported scenario was nb_bytes_to_read == block_compress_size. Pay attention that, if nb_bytes_to_read is > block_compressed_size, then, necessarily, it requires that nb_bytes_to_generate is <= block_decompress_size. If both are larger, it will generate corrupted data. --- lib/lz4.c | 39 +++++++++++++++++++++++---------------- lib/lz4.h | 34 ++++++++++++++++++++++------------ tests/fuzzer.c | 8 ++++---- 3 files changed, 49 insertions(+), 32 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 06d24da..0628eac 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1813,7 +1813,8 @@ LZ4_decompress_generic( if ((dict==usingExtDict) && (match < lowPrefix)) { if (unlikely(op+length > oend-LASTLITERALS)) { if (partialDecoding) { - length = MIN(length, (size_t)(oend-op)); /* reach end of buffer */ + DEBUGLOG(7, "partialDecoding: dictionary match, close to dstEnd"); + length = MIN(length, (size_t)(oend-op)); } else { goto _output_error; /* end-of-block condition violated */ } } @@ -1921,29 +1922,34 @@ LZ4_decompress_generic( || ((!endOnInput) && (cpy>oend-WILDCOPYLENGTH)) ) { /* We've either hit the input parsing restriction or the output parsing restriction. - * If we've hit the input parsing condition then this must be the last sequence. - * If we've hit the output parsing condition then we are either using partialDecoding - * or we've hit the output parsing condition. + * In the normal scenario, decoding a full block, it must be the last sequence, + * otherwise it's an error (invalid input or dimensions). + * In partialDecoding scenario, it's necessary to ensure there is no buffer overflow. */ if (partialDecoding) { /* Since we are partial decoding we may be in this block because of the output parsing * restriction, which is not valid since the output buffer is allowed to be undersized. */ assert(endOnInput); - /* If we're in this block because of the input parsing condition, then we must be on the - * last sequence (or invalid), so we must check that we exactly consume the input. + DEBUGLOG(7, "partialDecoding: copying literals, close to input or output end") + DEBUGLOG(7, "partialDecoding: literal length = %u", (unsigned)length); + DEBUGLOG(7, "partialDecoding: remaining space in dstBuffer : %i", (int)(oend - op)); + DEBUGLOG(7, "partialDecoding: remaining space in srcBuffer : %i", (int)(iend - ip)); + /* Finishing in the middle of a literals segment, + * due to lack of input. */ - if ((ip+length>iend-(2+1+LASTLITERALS)) && (ip+length != iend) && (cpy != oend)) { goto _output_error; } - assert(ip+length <= iend); - /* We are finishing in the middle of a literals segment. - * Break after the copy. + if (ip+length > iend) { + length = (size_t)(iend-ip); + cpy = op + length; + } + /* Finishing in the middle of a literals segment, + * due to lack of output space. */ if (cpy > oend) { cpy = oend; assert(op<=oend); length = (size_t)(oend-op); } - assert(ip+length <= iend); } else { /* We must be on the last sequence because of the parsing limitations so check * that we exactly regenerate the original size (must be exact when !endOnInput). @@ -1954,14 +1960,15 @@ LZ4_decompress_generic( */ if ((endOnInput) && ((ip+length != iend) || (cpy > oend))) { goto _output_error; } } - memmove(op, ip, length); /* supports overlapping memory regions, which only matters for in-place decompression scenarios */ + memmove(op, ip, length); /* supports overlapping memory regions; only matters for in-place decompression scenarios */ ip += length; op += length; - /* Necessarily EOF when !partialDecoding. When partialDecoding - * it is EOF if we've either filled the output buffer or hit - * the input parsing restriction. + /* Necessarily EOF when !partialDecoding. + * When partialDecoding, it is EOF if we've either + * filled the output buffer or + * can't proceed with reading an offset for following match. */ - if (!partialDecoding || (cpy == oend) || (ip == iend)) { + if (!partialDecoding || (cpy == oend) || (ip >= (iend-2))) { break; } } else { diff --git a/lib/lz4.h b/lib/lz4.h index 5209c10..5d2475c 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -221,25 +221,35 @@ LZ4LIB_API int LZ4_compress_destSize (const char* src, char* dst, int* srcSizePt * Decompress an LZ4 compressed block, of size 'srcSize' at position 'src', * into destination buffer 'dst' of size 'dstCapacity'. * Up to 'targetOutputSize' bytes will be decoded. - * The function stops decoding on reaching this objective, - * which can boost performance when only the beginning of a block is required. + * The function stops decoding on reaching this objective. + * This can be useful to boost performance + * whenever only the beginning of a block is required. * - * @return : the number of bytes decoded in `dst` (necessarily <= dstCapacity) + * @return : the number of bytes decoded in `dst` (necessarily <= targetOutputSize) * If source stream is detected malformed, function returns a negative result. * - * Note : @return can be < targetOutputSize, if compressed block contains less data. + * Note 1 : @return can be < targetOutputSize, if compressed block contains less data. * - * Note 2 : this function features 2 parameters, targetOutputSize and dstCapacity, - * and expects targetOutputSize <= dstCapacity. - * It effectively stops decoding on reaching targetOutputSize, + * Note 2 : targetOutputSize must be <= dstCapacity + * + * Note 3 : this function effectively stops decoding on reaching targetOutputSize, * so dstCapacity is kind of redundant. - * This is because in a previous version of this function, - * decoding operation would not "break" a sequence in the middle. - * As a consequence, there was no guarantee that decoding would stop at exactly targetOutputSize, + * This is because in older versions of this function, + * decoding operation would still write complete sequences. + * Therefore, there was no guarantee that it would stop writing at exactly targetOutputSize, * it could write more bytes, though only up to dstCapacity. * Some "margin" used to be required for this operation to work properly. - * This is no longer necessary. - * The function nonetheless keeps its signature, in an effort to not break API. + * Thankfully, this is no longer necessary. + * The function nonetheless keeps the same signature, in an effort to preserve API compatibility. + * + * Note 4 : If srcSize is the exact size of the block, + * then targetOutputSize can be any value, + * including larger than the block's decompressed size. + * The function will, at most, generate block's decompressed size. + * + * Note 5 : If srcSize is _larger_ than block's compressed size, + * then targetOutputSize **MUST** be <= block's decompressed size. + * Otherwise, *silent corruption will occur*. */ LZ4LIB_API int LZ4_decompress_safe_partial (const char* src, char* dst, int srcSize, int targetOutputSize, int dstCapacity); diff --git a/tests/fuzzer.c b/tests/fuzzer.c index cbb53ca..beeb9d6 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -618,13 +618,13 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c /* Test partial decoding => must work */ FUZ_DISPLAYTEST("test LZ4_decompress_safe_partial"); - { size_t const missingBytes = FUZ_rand(&randState) % (unsigned)blockSize; - int const targetSize = (int)((size_t)blockSize - missingBytes); + { size_t const missingOutBytes = FUZ_rand(&randState) % (unsigned)blockSize; + int const targetSize = (int)((size_t)blockSize - missingOutBytes); size_t const extraneousInBytes = FUZ_rand(&randState) % 2; int const inCSize = (int)((size_t)compressedSize + extraneousInBytes); char const sentinel = decodedBuffer[targetSize] = block[targetSize] ^ 0x5A; - //DISPLAY("compressedSize=%i, inCSize=%i \n", compressedSize, inCSize); - //DISPLAY("decompressedSize=%i, targetDstSize=%i \n", blockSize, targetSize); + DISPLAYLEVEL(6,"compressedSize=%i, inCSize=%i \n", compressedSize, inCSize); + DISPLAYLEVEL(6,"decompressedSize=%i, targetDstSize=%i \n", blockSize, targetSize); int const decResult = LZ4_decompress_safe_partial(compressedBuffer, decodedBuffer, inCSize, targetSize, blockSize); FUZ_CHECKTEST(decResult<0, "LZ4_decompress_safe_partial failed despite valid input data (error:%i)", decResult); FUZ_CHECKTEST(decResult != targetSize, "LZ4_decompress_safe_partial did not regenerated required amount of data (%i < %i <= %i)", decResult, targetSize, blockSize); -- cgit v1.2.3 From 875308f31f23949e297dbed525d982269a95fced Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 27 Aug 2020 00:38:58 -0700 Subject: fixed strict c90 support --- tests/fuzzer.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/fuzzer.c b/tests/fuzzer.c index beeb9d6..c385afe 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -623,8 +623,6 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c size_t const extraneousInBytes = FUZ_rand(&randState) % 2; int const inCSize = (int)((size_t)compressedSize + extraneousInBytes); char const sentinel = decodedBuffer[targetSize] = block[targetSize] ^ 0x5A; - DISPLAYLEVEL(6,"compressedSize=%i, inCSize=%i \n", compressedSize, inCSize); - DISPLAYLEVEL(6,"decompressedSize=%i, targetDstSize=%i \n", blockSize, targetSize); int const decResult = LZ4_decompress_safe_partial(compressedBuffer, decodedBuffer, inCSize, targetSize, blockSize); FUZ_CHECKTEST(decResult<0, "LZ4_decompress_safe_partial failed despite valid input data (error:%i)", decResult); FUZ_CHECKTEST(decResult != targetSize, "LZ4_decompress_safe_partial did not regenerated required amount of data (%i < %i <= %i)", decResult, targetSize, blockSize); -- cgit v1.2.3 From 8b75d403d86eaf9786da89a49ca02444916c462e Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 27 Aug 2020 09:20:01 -0700 Subject: fixed minor pre-existing printf formatting different signedness --- tests/fuzzer.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/fuzzer.c b/tests/fuzzer.c index c385afe..4658d79 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -863,8 +863,8 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c if (blockSize > missingBytes) { decodedBuffer[blockSize-missingBytes] = 0; ret = LZ4_decompress_safe_usingDict(compressedBuffer, decodedBuffer, blockContinueCompressedSize, blockSize-missingBytes, dict, dictSize); - FUZ_CHECKTEST(ret>=0, "LZ4_decompress_safe_usingDict should have failed : output buffer too small (-%u byte)", missingBytes); - FUZ_CHECKTEST(decodedBuffer[blockSize-missingBytes], "LZ4_decompress_safe_usingDict overrun specified output buffer size (-%u byte) (blockSize=%i)", missingBytes, blockSize); + FUZ_CHECKTEST(ret>=0, "LZ4_decompress_safe_usingDict should have failed : output buffer too small (-%i byte)", missingBytes); + FUZ_CHECKTEST(decodedBuffer[blockSize-missingBytes], "LZ4_decompress_safe_usingDict overrun specified output buffer size (-%i byte) (blockSize=%i)", missingBytes, blockSize); } } /* Compress HC using External dictionary */ -- cgit v1.2.3 From 4f33a5935b36783abb930252e4da487625817ee2 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 15 Sep 2020 13:45:06 -0700 Subject: added test triggering NULL arithmetic with usan described in #847 --- tests/fuzzer.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/fuzzer.c b/tests/fuzzer.c index 4658d79..5ca0d31 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -1055,6 +1055,14 @@ static void FUZ_unitTests(int compressionLevel) FUZ_CHECKTEST(r >= 0, "LZ4_decompress_safe() should fail"); } } + + /* useful to trigger undefined sanitizer */ + DISPLAYLEVEL(3, "LZ4_compress_default() with NULL input \n"); + { int const maxCSize = LZ4_compressBound(0); + int const cSize = LZ4_compress_default(NULL, testCompressed, 0, maxCSize); + FUZ_CHECKTEST(cSize==1 && testCompressed[0]==0, "compressed empty is byte 0"); + } + /* in-place compression test */ DISPLAYLEVEL(3, "in-place compression using LZ4_compress_default() :"); { int const sampleSize = 65 KB; -- cgit v1.2.3 From da1272979f4b79a7066631335a68512350a071fb Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 15 Sep 2020 14:54:53 -0700 Subject: fix #847 support NULL input without triggering undefined sanitizer --- tests/fuzzer.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/fuzzer.c b/tests/fuzzer.c index 5ca0d31..1d8b5f6 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -1059,8 +1059,11 @@ static void FUZ_unitTests(int compressionLevel) /* useful to trigger undefined sanitizer */ DISPLAYLEVEL(3, "LZ4_compress_default() with NULL input \n"); { int const maxCSize = LZ4_compressBound(0); - int const cSize = LZ4_compress_default(NULL, testCompressed, 0, maxCSize); - FUZ_CHECKTEST(cSize==1 && testCompressed[0]==0, "compressed empty is byte 0"); + int const cSize = LZ4_compress_default(NULL, testCompressed, 0, maxCSize); + FUZ_CHECKTEST(!(cSize==1 && testCompressed[0]==0), + "compressing empty should give byte 0" + " (maxCSize == %u) (cSize == %u)", + (unsigned)maxCSize, (unsigned)cSize); } /* in-place compression test */ -- cgit v1.2.3 From ee01df1271518df8757e692ce10766e66472ea93 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 16 Sep 2020 23:46:39 -0700 Subject: added the actual code change --- lib/lz4.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 53 insertions(+), 6 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 0628eac..6d918ea 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -819,8 +819,13 @@ LZ4_prepareTable(LZ4_stream_t_internal* const cctx, } /** LZ4_compress_generic() : - inlined, to ensure branches are decided at compilation time */ -LZ4_FORCE_INLINE int LZ4_compress_generic( + * inlined, to ensure branches are decided at compilation time. + * Presumed already validated at this stage: + * - source != NULL + * - inputSize > 0 + * - outputDirective == notLimited || maxOutputSize > 0 + */ +LZ4_FORCE_INLINE int LZ4_compress_generic_validated( LZ4_stream_t_internal* const cctx, const char* const source, char* const dest, @@ -867,11 +872,10 @@ LZ4_FORCE_INLINE int LZ4_compress_generic( U32 offset = 0; U32 forwardH; - DEBUGLOG(5, "LZ4_compress_generic: srcSize=%i, tableType=%u", inputSize, tableType); + DEBUGLOG(5, "LZ4_compress_generic_validated: srcSize=%i, tableType=%u", inputSize, tableType); + assert(ip != NULL); /* If init conditions are not met, we don't have to mark stream * as having dirty context, since no action was taken yet */ - if (outputDirective == fillOutput && maxOutputSize < 1) { return 0; } /* Impossible to store anything */ - if ((U32)inputSize > (U32)LZ4_MAX_INPUT_SIZE) { return 0; } /* Unsupported inputSize, too large (or negative) */ if ((tableType == byU16) && (inputSize>=LZ4_64Klimit)) { return 0; } /* Size too large (not within 64K limit) */ if (tableType==byPtr) assert(dictDirective==noDict); /* only supported use case with byPtr */ assert(acceleration >= 1); @@ -1204,12 +1208,55 @@ _last_literals: if (outputDirective == fillOutput) { *inputConsumed = (int) (((const char*)ip)-source); } - DEBUGLOG(5, "LZ4_compress_generic: compressed %i bytes into %i bytes", inputSize, (int)(((char*)op) - dest)); result = (int)(((char*)op) - dest); assert(result > 0); + DEBUGLOG(5, "LZ4_compress_generic: compressed %i bytes into %i bytes", inputSize, result); return result; } +/** LZ4_compress_generic() : + * inlined, to ensure branches are decided at compilation time; + * takes care of input == (NULL, 0) and outputSize == 0 + * and forward the rest to LZ4_compress_generic_validated */ +LZ4_FORCE_INLINE int LZ4_compress_generic( + LZ4_stream_t_internal* const cctx, + const char* const source, + char* const dest, + const int inputSize, + int *inputConsumed, /* only written when outputDirective == fillOutput */ + const int maxOutputSize, + const limitedOutput_directive outputDirective, + const tableType_t tableType, + const dict_directive dictDirective, + const dictIssue_directive dictIssue, + const int acceleration) +{ + DEBUGLOG(5, "LZ4_compress_generic: srcSize=%i, maxOutputSize=%i", + inputSize, maxOutputSize); + + /* If init conditions are not met, we don't have to mark stream + * as having dirty context, since no action was taken yet */ + if (outputDirective != notLimited && maxOutputSize < 1) { return 0; } /* Impossible to store anything */ + assert(dest != NULL); + + if ((U32)inputSize > (U32)LZ4_MAX_INPUT_SIZE) { return 0; } /* Unsupported inputSize, too large (or negative) */ + if (inputSize == 0) { /* source == NULL supported if inputSize == 0 */ + assert(outputDirective == notLimited || maxOutputSize >= 1); + dest[0] = 0; + if (outputDirective == fillOutput) { + assert (inputConsumed != NULL); + *inputConsumed = 0; + } + return 1; + } + assert(source != NULL); + + return LZ4_compress_generic_validated(cctx, source, dest, inputSize, + inputConsumed, /* only written when outputDirective == fillOutput */ + maxOutputSize, outputDirective, + tableType, dictDirective, dictIssue, acceleration); +} + int LZ4_compress_fast_extState(void* state, const char* source, char* dest, int inputSize, int maxOutputSize, int acceleration) { -- cgit v1.2.3 From b4a64e0eac8f39415b2fa3573561f9325f7296c1 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 17 Sep 2020 10:29:31 -0700 Subject: fix ubsan test which now fails with a clear error as intended (not just print a status and move on). should be reproduced on travisCI --- Makefile | 6 +++--- tests/Makefile | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index ab695ea..ef1fd2e 100644 --- a/Makefile +++ b/Makefile @@ -124,8 +124,8 @@ check: .PHONY: test test: - $(MAKE) -C $(TESTDIR) $@ - $(MAKE) -C $(EXDIR) $@ + CFLAGS="$(CFLAGS)" $(MAKE) -C $(TESTDIR) $@ + CFLAGS="$(CFLAGS)" $(MAKE) -C $(EXDIR) $@ clangtest: CFLAGS ?= -O3 clangtest: CFLAGS += -Werror -Wconversion -Wno-sign-conversion @@ -143,7 +143,7 @@ clangtest-native: clean @CFLAGS="-O3 -Werror -Wconversion -Wno-sign-conversion" $(MAKE) -C $(TESTDIR) native CC=clang usan: clean - CC=clang CFLAGS="-O3 -g -fsanitize=undefined" $(MAKE) test FUZZER_TIME="-T30s" NB_LOOPS=-i1 + CC=clang CFLAGS="-O3 -g -fsanitize=undefined -fno-sanitize-recover=undefined" $(MAKE) test FUZZER_TIME="-T30s" NB_LOOPS=-i1 usan32: clean CFLAGS="-m32 -O3 -g -fsanitize=undefined" $(MAKE) test FUZZER_TIME="-T30s" NB_LOOPS=-i1 diff --git a/tests/Makefile b/tests/Makefile index 5a6ec59..43c9651 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -35,7 +35,7 @@ PYTHON ?= python3 DEBUGLEVEL?= 1 DEBUGFLAGS = -g -DLZ4_DEBUG=$(DEBUGLEVEL) -CFLAGS ?= -O3 # can select custom optimization flags. For example : CFLAGS=-O2 make +CFLAGS ?= -O3 # can select custom optimization flags. Example : CFLAGS=-O2 make CFLAGS += -Wall -Wextra -Wundef -Wcast-qual -Wcast-align -Wshadow \ -Wswitch-enum -Wdeclaration-after-statement -Wstrict-prototypes \ -Wpointer-arith -Wstrict-aliasing=1 -- cgit v1.2.3 From 10d2e1c69465ac51d210909ca7a447a1397736a1 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 17 Sep 2020 14:43:02 -0700 Subject: fixed lz4frame with blocks of size 1 properly track history --- lib/lz4.c | 40 +++++++++++++++++++--------------------- lib/lz4frame.c | 5 ++++- tests/frametest.c | 6 ++++-- 3 files changed, 27 insertions(+), 24 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 6d918ea..2222d53 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -823,7 +823,6 @@ LZ4_prepareTable(LZ4_stream_t_internal* const cctx, * Presumed already validated at this stage: * - source != NULL * - inputSize > 0 - * - outputDirective == notLimited || maxOutputSize > 0 */ LZ4_FORCE_INLINE int LZ4_compress_generic_validated( LZ4_stream_t_internal* const cctx, @@ -876,6 +875,7 @@ LZ4_FORCE_INLINE int LZ4_compress_generic_validated( assert(ip != NULL); /* If init conditions are not met, we don't have to mark stream * as having dirty context, since no action was taken yet */ + if (outputDirective == fillOutput && maxOutputSize < 1) { return 0; } /* Impossible to store anything */ if ((tableType == byU16) && (inputSize>=LZ4_64Klimit)) { return 0; } /* Size too large (not within 64K limit) */ if (tableType==byPtr) assert(dictDirective==noDict); /* only supported use case with byPtr */ assert(acceleration >= 1); @@ -1216,44 +1216,41 @@ _last_literals: /** LZ4_compress_generic() : * inlined, to ensure branches are decided at compilation time; - * takes care of input == (NULL, 0) and outputSize == 0 + * takes care of src == (NULL, 0) * and forward the rest to LZ4_compress_generic_validated */ LZ4_FORCE_INLINE int LZ4_compress_generic( LZ4_stream_t_internal* const cctx, - const char* const source, - char* const dest, - const int inputSize, + const char* const src, + char* const dst, + const int srcSize, int *inputConsumed, /* only written when outputDirective == fillOutput */ - const int maxOutputSize, + const int dstCapacity, const limitedOutput_directive outputDirective, const tableType_t tableType, const dict_directive dictDirective, const dictIssue_directive dictIssue, const int acceleration) { - DEBUGLOG(5, "LZ4_compress_generic: srcSize=%i, maxOutputSize=%i", - inputSize, maxOutputSize); - - /* If init conditions are not met, we don't have to mark stream - * as having dirty context, since no action was taken yet */ - if (outputDirective != notLimited && maxOutputSize < 1) { return 0; } /* Impossible to store anything */ - assert(dest != NULL); + DEBUGLOG(5, "LZ4_compress_generic: srcSize=%i, dstCapacity=%i", + srcSize, dstCapacity); - if ((U32)inputSize > (U32)LZ4_MAX_INPUT_SIZE) { return 0; } /* Unsupported inputSize, too large (or negative) */ - if (inputSize == 0) { /* source == NULL supported if inputSize == 0 */ - assert(outputDirective == notLimited || maxOutputSize >= 1); - dest[0] = 0; + if ((U32)srcSize > (U32)LZ4_MAX_INPUT_SIZE) { return 0; } /* Unsupported srcSize, too large (or negative) */ + if (srcSize == 0) { /* src == NULL supported if srcSize == 0 */ + DEBUGLOG(5, "Generating an empty block"); + assert(outputDirective == notLimited || dstCapacity >= 1); + assert(dst != NULL); + dst[0] = 0; if (outputDirective == fillOutput) { assert (inputConsumed != NULL); *inputConsumed = 0; } return 1; } - assert(source != NULL); + assert(src != NULL); - return LZ4_compress_generic_validated(cctx, source, dest, inputSize, - inputConsumed, /* only written when outputDirective == fillOutput */ - maxOutputSize, outputDirective, + return LZ4_compress_generic_validated(cctx, src, dst, srcSize, + inputConsumed, /* only written into if outputDirective == fillOutput */ + dstCapacity, outputDirective, tableType, dictDirective, dictIssue, acceleration); } @@ -2124,6 +2121,7 @@ LZ4_decompress_generic( /* end of decoding */ if (endOnInput) { + DEBUGLOG(5, "decoded %i bytes", (int) (((char*)op)-dst)); return (int) (((char*)op)-dst); /* Nb of output bytes decoded */ } else { return (int) (((const char*)ip)-src); /* Nb of input bytes read */ diff --git a/lib/lz4frame.c b/lib/lz4frame.c index e11f1c8..890fc96 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -750,6 +750,7 @@ static size_t LZ4F_makeBlock(void* dst, (int)(srcSize), (int)(srcSize-1), level, cdict); if (cSize == 0) { /* compression failed */ + DEBUGLOG(5, "LZ4F_makeBlock: compression failed, creating a raw block (size %u)", (U32)srcSize); cSize = (U32)srcSize; LZ4F_writeLE32(cSizePtr, cSize | LZ4F_BLOCKUNCOMPRESSED_FLAG); memcpy(cSizePtr+BHSize, src, srcSize); @@ -1487,15 +1488,17 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, size_t const nextCBlockSize = blockHeader & 0x7FFFFFFFU; size_t const crcSize = dctx->frameInfo.blockChecksumFlag * BFSize; if (blockHeader==0) { /* frameEnd signal, no more block */ + DEBUGLOG(5, "end of frame"); dctx->dStage = dstage_getSuffix; break; } if (nextCBlockSize > dctx->maxBlockSize) { return err0r(LZ4F_ERROR_maxBlockSize_invalid); } - if (LZ4F_readLE32(selectedIn) & LZ4F_BLOCKUNCOMPRESSED_FLAG) { + if (blockHeader & LZ4F_BLOCKUNCOMPRESSED_FLAG) { /* next block is uncompressed */ dctx->tmpInTarget = nextCBlockSize; + DEBUGLOG(5, "next block is uncompressed (size %u)", (U32)nextCBlockSize); if (dctx->frameInfo.blockChecksumFlag) { (void)XXH32_reset(&dctx->blockChecksum, 0); } diff --git a/tests/frametest.c b/tests/frametest.c index 236a98c..54c7fd2 100644 --- a/tests/frametest.c +++ b/tests/frametest.c @@ -785,7 +785,7 @@ typedef enum { o_contiguous, o_noncontiguous, o_overwrite } o_scenario_e; static void locateBuffDiff(const void* buff1, const void* buff2, size_t size, o_scenario_e o_scenario) { - if (displayLevel >= 5) { + if (displayLevel >= 2) { size_t p=0; const BYTE* b1=(const BYTE*)buff1; const BYTE* b2=(const BYTE*)buff2; @@ -862,8 +862,10 @@ size_t test_lz4f_decompression_wBuffers( "Decompression overwrites beyond assigned dst size", op[oSizeMax], mark); } - if (LZ4F_getErrorCode(moreToFlush) == LZ4F_ERROR_contentChecksum_invalid) + if (LZ4F_getErrorCode(moreToFlush) == LZ4F_ERROR_contentChecksum_invalid) { + DISPLAYLEVEL(2, "checksum error detected \n"); locateBuffDiff(srcRef, dst, decompressedSize, o_scenario); + } if (LZ4F_isError(moreToFlush)) return moreToFlush; XXH64_update(&xxh64, op, oSize); -- cgit v1.2.3 From b223f11b2163a80c30084b14cec8dabe944740f5 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 17 Sep 2020 15:34:03 -0700 Subject: ubsan: make pointer overflow recoverable the way `base` is used must allow 2-complement address overflow. `base` is effectively a virtual pointer, never dereferenced directly. What matters is `base + index`. This is the address that will be dereference and must be valid (it is properly validated). --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index ef1fd2e..6bf952c 100644 --- a/Makefile +++ b/Makefile @@ -143,7 +143,7 @@ clangtest-native: clean @CFLAGS="-O3 -Werror -Wconversion -Wno-sign-conversion" $(MAKE) -C $(TESTDIR) native CC=clang usan: clean - CC=clang CFLAGS="-O3 -g -fsanitize=undefined -fno-sanitize-recover=undefined" $(MAKE) test FUZZER_TIME="-T30s" NB_LOOPS=-i1 + CC=clang CFLAGS="-O3 -g -fsanitize=undefined -fno-sanitize-recover=undefined -fsanitize-recover=pointer-overflow" $(MAKE) test FUZZER_TIME="-T30s" NB_LOOPS=-i1 usan32: clean CFLAGS="-m32 -O3 -g -fsanitize=undefined" $(MAKE) test FUZZER_TIME="-T30s" NB_LOOPS=-i1 -- cgit v1.2.3 From 4f57219a2247a807e2b65f61501a48e37e261ab8 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 17 Sep 2020 16:49:21 -0700 Subject: fixed ubsan tests with dynamic library --- Makefile | 9 ++++++--- tests/.gitignore | 1 + 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 6bf952c..c1869f6 100644 --- a/Makefile +++ b/Makefile @@ -124,8 +124,8 @@ check: .PHONY: test test: - CFLAGS="$(CFLAGS)" $(MAKE) -C $(TESTDIR) $@ - CFLAGS="$(CFLAGS)" $(MAKE) -C $(EXDIR) $@ + CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)" $(MAKE) -C $(TESTDIR) $@ + CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)" $(MAKE) -C $(EXDIR) $@ clangtest: CFLAGS ?= -O3 clangtest: CFLAGS += -Werror -Wconversion -Wno-sign-conversion @@ -142,8 +142,11 @@ clangtest-native: clean @CFLAGS="-O3 -Werror -Wconversion -Wno-sign-conversion" $(MAKE) -C $(PRGDIR) native CC=clang @CFLAGS="-O3 -Werror -Wconversion -Wno-sign-conversion" $(MAKE) -C $(TESTDIR) native CC=clang +usan: CC = clang +usan: CFLAGS = -O3 -g -fsanitize=undefined -fno-sanitize-recover=undefined -fsanitize-recover=pointer-overflow +usan: LDFLAGS = $(CFLAGS) usan: clean - CC=clang CFLAGS="-O3 -g -fsanitize=undefined -fno-sanitize-recover=undefined -fsanitize-recover=pointer-overflow" $(MAKE) test FUZZER_TIME="-T30s" NB_LOOPS=-i1 + CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)" $(MAKE) test FUZZER_TIME="-T30s" NB_LOOPS=-i1 usan32: clean CFLAGS="-m32 -O3 -g -fsanitize=undefined" $(MAKE) test FUZZER_TIME="-T30s" NB_LOOPS=-i1 diff --git a/tests/.gitignore b/tests/.gitignore index 0d13df8..99351af 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -11,6 +11,7 @@ fasttest roundTripTest checkTag checkFrame +decompress-partial # test artefacts tmp* -- cgit v1.2.3 From dc27b7ae4157024384b4766e501d9ba2788ddfd3 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 17 Sep 2020 20:59:01 -0700 Subject: comment bug on older versions of ZSTD_compress_destSize() following investigation in #859 --- lib/lz4.h | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/lz4.h b/lib/lz4.h index 5d2475c..5474005 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -213,7 +213,18 @@ LZ4LIB_API int LZ4_compress_fast_extState (void* state, const char* src, char* d * New value is necessarily <= input value. * @return : Nb bytes written into 'dst' (necessarily <= targetDestSize) * or 0 if compression fails. -*/ + * + * Note : from v1.8.2 to v1.9.1, this function had a bug (fixed un v1.9.2+): + * the produced compressed content could, in specific circumstances, + * require to be decompressed into a destination buffer larger + * by at least 1 byte than the content to decompress. + * If an application uses `LZ4_compress_destSize()`, + * it's highly recommended to update liblz4 to v1.9.2 or better. + * If this can't be done or ensured, + * the receiving decompression function should provide + * a dstCapacity which is > decompressedSize, by at least 1 byte. + * See https://github.com/lz4/lz4/issues/859 for details + */ LZ4LIB_API int LZ4_compress_destSize (const char* src, char* dst, int* srcSizePtr, int targetDstSize); -- cgit v1.2.3 From 63942cdebadc26e7ed43c37dc0d35db2fb71304b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Attila=20Ol=C3=A1h?= Date: Wed, 23 Sep 2020 12:49:25 +0000 Subject: Add CMake option LZ4_BUILD_CLI. Defaults to ON so nothing changes, but it can be set to OFF so that the CLI is not built at all, only the library. --- build/cmake/CMakeLists.txt | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/build/cmake/CMakeLists.txt b/build/cmake/CMakeLists.txt index 42d92ea..1bb2f5a 100644 --- a/build/cmake/CMakeLists.txt +++ b/build/cmake/CMakeLists.txt @@ -12,6 +12,7 @@ set(LZ4_TOP_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../..") +option(LZ4_BUILD_CLI "Build lz4 program" ON) option(LZ4_BUILD_LEGACY_LZ4C "Build lz4c progam with legacy argument support" ON) # Parse version information @@ -124,10 +125,12 @@ else() endif() # lz4 -set(LZ4_PROGRAMS_BUILT lz4cli) -add_executable(lz4cli ${LZ4_CLI_SOURCES}) -set_target_properties(lz4cli PROPERTIES OUTPUT_NAME lz4) -target_link_libraries(lz4cli ${LZ4_LINK_LIBRARY}) +if (LZ4_BUILD_CLI) + set(LZ4_PROGRAMS_BUILT lz4cli) + add_executable(lz4cli ${LZ4_CLI_SOURCES}) + set_target_properties(lz4cli PROPERTIES OUTPUT_NAME lz4) + target_link_libraries(lz4cli ${LZ4_LINK_LIBRARY}) +endif() # lz4c if (LZ4_BUILD_LEGACY_LZ4C) @@ -189,7 +192,7 @@ if(NOT LZ4_BUNDLED_MODE) DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") # install lz4cat and unlz4 symlinks on *nix - if(UNIX) + if(UNIX AND LZ4_BUILD_CLI) install(CODE " foreach(f lz4cat unlz4) set(dest \"\$ENV{DESTDIR}${CMAKE_INSTALL_FULL_BINDIR}/\${f}\") @@ -205,7 +208,7 @@ if(NOT LZ4_BUNDLED_MODE) install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${f}.1" DESTINATION "${CMAKE_INSTALL_MANDIR}/man1") endforeach() - endif(UNIX) + endif(UNIX AND LZ4_BUILD_CLI) endif(NOT LZ4_BUNDLED_MODE) # pkg-config -- cgit v1.2.3 From ee4f37d2840c1faa41fb7110bbf6d1e58bf30ace Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sat, 26 Sep 2020 11:31:57 -0700 Subject: fix compressing into NULL fails properly bug discovered by oss-fuzz --- lib/lz4.c | 1 + tests/fuzzer.c | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 2222d53..d21eef4 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1236,6 +1236,7 @@ LZ4_FORCE_INLINE int LZ4_compress_generic( if ((U32)srcSize > (U32)LZ4_MAX_INPUT_SIZE) { return 0; } /* Unsupported srcSize, too large (or negative) */ if (srcSize == 0) { /* src == NULL supported if srcSize == 0 */ + if (outputDirective != notLimited && dstCapacity <= 0) return 0; /* no output, can't write anything */ DEBUGLOG(5, "Generating an empty block"); assert(outputDirective == notLimited || dstCapacity >= 1); assert(dst != NULL); diff --git a/tests/fuzzer.c b/tests/fuzzer.c index 1d8b5f6..4dbf6a7 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -1056,15 +1056,24 @@ static void FUZ_unitTests(int compressionLevel) } } - /* useful to trigger undefined sanitizer */ - DISPLAYLEVEL(3, "LZ4_compress_default() with NULL input \n"); + /* to be tested with undefined sanitizer */ + DISPLAYLEVEL(3, "LZ4_compress_default() with NULL input:"); { int const maxCSize = LZ4_compressBound(0); int const cSize = LZ4_compress_default(NULL, testCompressed, 0, maxCSize); FUZ_CHECKTEST(!(cSize==1 && testCompressed[0]==0), "compressing empty should give byte 0" - " (maxCSize == %u) (cSize == %u)", - (unsigned)maxCSize, (unsigned)cSize); + " (maxCSize == %i) (cSize == %i) (byte == 0x%02X)", + maxCSize, cSize, testCompressed[0]); } + DISPLAYLEVEL(3, " OK \n"); + + DISPLAYLEVEL(3, "LZ4_compress_default() with both NULL input and output:"); + { int const cSize = LZ4_compress_default(NULL, NULL, 0, 0); + FUZ_CHECKTEST(cSize != 0, + "compressing into NULL must fail" + " (cSize == %i != 0)", cSize); + } + DISPLAYLEVEL(3, " OK \n"); /* in-place compression test */ DISPLAYLEVEL(3, "in-place compression using LZ4_compress_default() :"); -- cgit v1.2.3 From 9730d91110184a83df9e8be8a0d2a1c57851685f Mon Sep 17 00:00:00 2001 From: Anton Kochkov Date: Sun, 27 Sep 2020 17:07:51 +0800 Subject: Fix compilation with TinyCC --- lib/lz4.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 2222d53..817570d 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -514,7 +514,7 @@ static unsigned LZ4_NbCommonBytes (reg_t val) return (unsigned)r >> 3; # elif (defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 3) || \ ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))))) && \ - !defined(LZ4_FORCE_SW_BITCOUNT) + !defined(__TINYC__) && !defined(LZ4_FORCE_SW_BITCOUNT) return (unsigned)__builtin_ctz((U32)val) >> 3; # else const U32 m = 0x01010101; @@ -525,7 +525,7 @@ static unsigned LZ4_NbCommonBytes (reg_t val) if (sizeof(val)==8) { # if (defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 3) || \ ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))))) && \ - !defined(LZ4_FORCE_SW_BITCOUNT) + !defined(__TINYC__) && !defined(LZ4_FORCE_SW_BITCOUNT) return (unsigned)__builtin_clzll((U64)val) >> 3; # else #if 1 -- cgit v1.2.3 From e7fe105ac6ed02019d34731d2ba3aceb11b51bb1 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sun, 27 Sep 2020 21:04:40 -0700 Subject: fix efficiency of LZ4_compress_HC_destSize() LZ4_compress_HC_destSize() had a tendency to discard its last match when this match overflowed specified dstBuffer limit. The impact is generally moderate, but occasionally huge, typically when this last match is very large (such as compressing a bunch of zeroes). Issue #784 fixed for both Chain and Opt implementations. Added a unit test suggested by @remittor checking this topic. --- lib/lz4.c | 3 +- lib/lz4hc.c | 90 ++++++++++++++++++++++++++++++++++++-------------- tests/fuzzer.c | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 161 insertions(+), 34 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 2222d53..c192d5b 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -247,6 +247,7 @@ static const int LZ4_minLength = (MFLIMIT+1); /*-************************************ * Types **************************************/ +#include #if defined(__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) # include typedef uint8_t BYTE; @@ -256,7 +257,6 @@ static const int LZ4_minLength = (MFLIMIT+1); typedef uint64_t U64; typedef uintptr_t uptrval; #else -# include # if UINT_MAX != 4294967295UL # error "LZ4 code (when not C++ or C99) assumes that sizeof(int) == 4" # endif @@ -1192,6 +1192,7 @@ _last_literals: return 0; /* cannot compress within `dst` budget. Stored indexes in hash table are nonetheless fine */ } } + DEBUGLOG(6, "Final literal run : %i literals", (int)lastRun); if (lastRun >= RUN_MASK) { size_t accumulator = lastRun - RUN_MASK; *op++ = RUN_MASK << ML_BITS; diff --git a/lib/lz4hc.c b/lib/lz4hc.c index 687f87e..aefc95b 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -490,7 +490,9 @@ LZ4_FORCE_INLINE int LZ4HC_encodeSequence ( /* Encode Literal length */ length = (size_t)(*ip - *anchor); - if ((limit) && ((*op + (length / 255) + length + (2 + 1 + LASTLITERALS)) > oend)) return 1; /* Check output limit */ + LZ4_STATIC_ASSERT(notLimited == 0); + /* Check output limit */ + if (limit && ((*op + (length / 255) + length + (2 + 1 + LASTLITERALS)) > oend)) return 1; if (length >= RUN_MASK) { size_t len = length - RUN_MASK; *token = (RUN_MASK << ML_BITS); @@ -511,7 +513,7 @@ LZ4_FORCE_INLINE int LZ4HC_encodeSequence ( /* Encode MatchLength */ assert(matchLength >= MINMATCH); length = (size_t)matchLength - MINMATCH; - if ((limit) && (*op + (length / 255) + (1 + LASTLITERALS) > oend)) return 1; /* Check output limit */ + if (limit && (*op + (length / 255) + (1 + LASTLITERALS) > oend)) return 1; /* Check output limit */ if (length >= ML_MASK) { *token += ML_MASK; length -= ML_MASK; @@ -565,7 +567,7 @@ LZ4_FORCE_INLINE int LZ4HC_compress_hashChain ( /* init */ *srcSizePtr = 0; if (limit == fillOutput) oend -= LASTLITERALS; /* Hack for support LZ4 format restriction */ - if (inputSize < LZ4_minLength) goto _last_literals; /* Input too small, no compression (all literals) */ + if (inputSize < LZ4_minLength) goto _last_literals; /* Input too small, no compression (all literals) */ /* Main Loop */ while (ip <= mflimit) { @@ -637,7 +639,11 @@ _Search3: if (LZ4HC_encodeSequence(UPDATABLE(ip, op, anchor), ml, ref, limit, oend)) goto _dest_overflow; ip = start2; optr = op; - if (LZ4HC_encodeSequence(UPDATABLE(ip, op, anchor), ml2, ref2, limit, oend)) goto _dest_overflow; + if (LZ4HC_encodeSequence(UPDATABLE(ip, op, anchor), ml2, ref2, limit, oend)) { + ml = ml2; + ref = ref2; + goto _dest_overflow; + } continue; } @@ -709,17 +715,18 @@ _Search3: _last_literals: /* Encode Last Literals */ { size_t lastRunSize = (size_t)(iend - anchor); /* literals */ - size_t litLength = (lastRunSize + 255 - RUN_MASK) / 255; - size_t const totalSize = 1 + litLength + lastRunSize; + size_t llAdd = (lastRunSize + 255 - RUN_MASK) / 255; + size_t const totalSize = 1 + llAdd + lastRunSize; if (limit == fillOutput) oend += LASTLITERALS; /* restore correct value */ if (limit && (op + totalSize > oend)) { - if (limit == limitedOutput) return 0; /* Check output limit */ + if (limit == limitedOutput) return 0; /* adapt lastRunSize to fill 'dest' */ lastRunSize = (size_t)(oend - op) - 1; - litLength = (lastRunSize + 255 - RUN_MASK) / 255; - lastRunSize -= litLength; + llAdd = (lastRunSize + 256 - RUN_MASK) / 256; + lastRunSize -= llAdd; } - ip = anchor + lastRunSize; + DEBUGLOG(6, "Final literal run : %i literals", (int)lastRunSize); + ip = ip + lastRunSize; /* can be != iend if limit==fillOutput */ if (lastRunSize >= RUN_MASK) { size_t accumulator = lastRunSize - RUN_MASK; @@ -739,9 +746,23 @@ _last_literals: _dest_overflow: if (limit == fillOutput) { + /* Assumption : ip, anchor, ml and ref must be set correctly */ + size_t const ll = (size_t)(ip - anchor); + size_t const ll_addbytes = (ll + 240) / 255; + size_t const ll_totalCost = 1 + ll_addbytes + ll; + BYTE* const maxLitPos = oend - 3; /* 2 for offset, 1 for token */ op = optr; /* restore correct out pointer */ + if (op + ll_totalCost <= maxLitPos) { + /* ll validated; now how many matches ? */ + size_t const bytesLeftForMl = (size_t)(maxLitPos - (op+ll_totalCost)); + size_t const maxMlSize = MINMATCH + (ML_MASK-1) + (bytesLeftForMl * 255); + assert(maxMlSize < INT_MAX); assert(ml >= 0); + if ((size_t)ml > maxMlSize) ml = (int)maxMlSize; + LZ4HC_encodeSequence(UPDATABLE(ip, op, anchor), ml, ref, notLimited, oend); + } goto _last_literals; } + /* compression failed */ return 0; } @@ -788,7 +809,8 @@ LZ4_FORCE_INLINE int LZ4HC_compress_generic_internal ( { lz4opt,16384,LZ4_OPT_NUM }, /* 12==LZ4HC_CLEVEL_MAX */ }; - DEBUGLOG(4, "LZ4HC_compress_generic(ctx=%p, src=%p, srcSize=%d)", ctx, src, *srcSizePtr); + DEBUGLOG(4, "LZ4HC_compress_generic(ctx=%p, src=%p, srcSize=%d, limit=%d)", + ctx, src, *srcSizePtr, limit); if (limit == fillOutput && dstCapacity < 1) return 0; /* Impossible to store anything */ if ((U32)*srcSizePtr > (U32)LZ4_MAX_INPUT_SIZE) return 0; /* Unsupported input size (too large or negative) */ @@ -1075,8 +1097,8 @@ static int LZ4_compressHC_continue_generic (LZ4_streamHC_t* LZ4_streamHCPtr, limitedOutput_directive limit) { LZ4HC_CCtx_internal* const ctxPtr = &LZ4_streamHCPtr->internal_donotuse; - DEBUGLOG(4, "LZ4_compressHC_continue_generic(ctx=%p, src=%p, srcSize=%d)", - LZ4_streamHCPtr, src, *srcSizePtr); + DEBUGLOG(5, "LZ4_compressHC_continue_generic(ctx=%p, src=%p, srcSize=%d, limit=%d)", + LZ4_streamHCPtr, src, *srcSizePtr, limit); assert(ctxPtr != NULL); /* auto-init if forgotten */ if (ctxPtr->base == NULL) LZ4HC_init_internal (ctxPtr, (const BYTE*) src); @@ -1303,6 +1325,8 @@ static int LZ4HC_compress_optimal ( LZ4HC_CCtx_internal* ctx, BYTE* op = (BYTE*) dst; BYTE* opSaved = (BYTE*) dst; BYTE* oend = op + dstCapacity; + int ovml = MINMATCH; /* overflow - last sequence */ + const BYTE* ovref = NULL; /* init */ #ifdef LZ4HC_HEAPMODE @@ -1328,8 +1352,11 @@ static int LZ4HC_compress_optimal ( LZ4HC_CCtx_internal* ctx, int const firstML = firstMatch.len; const BYTE* const matchPos = ip - firstMatch.off; opSaved = op; - if ( LZ4HC_encodeSequence(UPDATABLE(ip, op, anchor), firstML, matchPos, limit, oend) ) /* updates ip, op and anchor */ + if ( LZ4HC_encodeSequence(UPDATABLE(ip, op, anchor), firstML, matchPos, limit, oend) ) { /* updates ip, op and anchor */ + ovml = firstML; + ovref = matchPos; goto _dest_overflow; + } continue; } @@ -1471,7 +1498,7 @@ static int LZ4HC_compress_optimal ( LZ4HC_CCtx_internal* ctx, best_off = opt[last_match_pos].off; cur = last_match_pos - best_mlen; - encode: /* cur, last_match_pos, best_mlen, best_off must be set */ +encode: /* cur, last_match_pos, best_mlen, best_off must be set */ assert(cur < LZ4_OPT_NUM); assert(last_match_pos >= 1); /* == 1 when only one candidate */ DEBUGLOG(6, "reverse traversal, looking for shortest path (last_match_pos=%i)", last_match_pos); @@ -1501,12 +1528,14 @@ static int LZ4HC_compress_optimal ( LZ4HC_CCtx_internal* ctx, assert(ml >= MINMATCH); assert((offset >= 1) && (offset <= LZ4_DISTANCE_MAX)); opSaved = op; - if ( LZ4HC_encodeSequence(UPDATABLE(ip, op, anchor), ml, ip - offset, limit, oend) ) /* updates ip, op and anchor */ + if ( LZ4HC_encodeSequence(UPDATABLE(ip, op, anchor), ml, ip - offset, limit, oend) ) { /* updates ip, op and anchor */ + ovml = ml; + ovref = ip - offset; goto _dest_overflow; - } } + } } } } /* while (ip <= mflimit) */ - _last_literals: +_last_literals: /* Encode Last Literals */ { size_t lastRunSize = (size_t)(iend - anchor); /* literals */ size_t litLength = (lastRunSize + 255 - RUN_MASK) / 255; @@ -1541,14 +1570,27 @@ static int LZ4HC_compress_optimal ( LZ4HC_CCtx_internal* ctx, retval = (int) ((char*)op-dst); goto _return_label; - _dest_overflow: - if (limit == fillOutput) { - op = opSaved; /* restore correct out pointer */ - goto _last_literals; +_dest_overflow: +if (limit == fillOutput) { + /* Assumption : ip, anchor, ovml and ovref must be set correctly */ + size_t const ll = (size_t)(ip - anchor); + size_t const ll_addbytes = (ll + 240) / 255; + size_t const ll_totalCost = 1 + ll_addbytes + ll; + BYTE* const maxLitPos = oend - 3; /* 2 for offset, 1 for token */ + op = opSaved; /* restore correct out pointer */ + if (op + ll_totalCost <= maxLitPos) { + /* ll validated; now how many matches ? */ + size_t const bytesLeftForMl = (size_t)(maxLitPos - (op+ll_totalCost)); + size_t const maxMlSize = MINMATCH + (ML_MASK-1) + (bytesLeftForMl * 255); + assert(maxMlSize < INT_MAX); assert(ovml >= 0); + if ((size_t)ovml > maxMlSize) ovml = (int)maxMlSize; + LZ4HC_encodeSequence(UPDATABLE(ip, op, anchor), ovml, ovref, notLimited, oend); } - _return_label: + goto _last_literals; +} +_return_label: #ifdef LZ4HC_HEAPMODE free(opt); #endif return retval; - } +} diff --git a/tests/fuzzer.c b/tests/fuzzer.c index 1d8b5f6..e611bb4 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -1191,12 +1191,11 @@ static void FUZ_unitTests(int compressionLevel) /* LZ4 HC streaming tests */ { LZ4_streamHC_t sHC; /* statically allocated */ - U64 crcOrig; int result; LZ4_initStreamHC(&sHC, sizeof(sHC)); /* Allocation test */ - DISPLAYLEVEL(3, " Basic HC allocation : "); + DISPLAYLEVEL(3, "Basic HC allocation : "); { LZ4_streamHC_t* const sp = LZ4_createStreamHC(); FUZ_CHECKTEST(sp==NULL, "LZ4_createStreamHC() allocation failed"); LZ4_freeStreamHC(sp); @@ -1204,7 +1203,7 @@ static void FUZ_unitTests(int compressionLevel) DISPLAYLEVEL(3, " OK \n"); /* simple HC compression test */ - DISPLAYLEVEL(3, " Simple HC round-trip : "); + DISPLAYLEVEL(3, "Simple HC round-trip : "); { U64 const crc64 = XXH64(testInput, testCompressedSize, 0); LZ4_setCompressionLevel(&sHC, compressionLevel); result = LZ4_compress_HC_continue(&sHC, testInput, testCompressed, testCompressedSize, testCompressedSize-1); @@ -1219,7 +1218,7 @@ static void FUZ_unitTests(int compressionLevel) DISPLAYLEVEL(3, " OK \n"); /* long sequence test */ - DISPLAYLEVEL(3, " Long sequence HC test : "); + DISPLAYLEVEL(3, "Long sequence HC_destSize test : "); { size_t const blockSize = 1 MB; size_t const targetSize = 4116; /* size carefully selected to trigger an overflow */ void* const block = malloc(blockSize); @@ -1237,8 +1236,13 @@ static void FUZ_unitTests(int compressionLevel) assert(targetSize < INT_MAX); result = LZ4_compress_HC_destSize(&sHC, (const char*)block, (char*)dstBlock, &srcSize, (int)targetSize, 3); DISPLAYLEVEL(4, "cSize=%i; readSize=%i; ", result, srcSize); - FUZ_CHECKTEST(result!=4116, "LZ4_compress_HC_destSize() : compression must fill dstBuffer completely, but no more !"); - FUZ_CHECKTEST(((char*)dstBlock)[targetSize] != sentinel, "LZ4_compress_HC_destSize()") + FUZ_CHECKTEST(result != 4116, "LZ4_compress_HC_destSize() : " + "compression (%i->%i) must fill dstBuffer (%i) exactly", + srcSize, result, (int)targetSize); + FUZ_CHECKTEST(((char*)dstBlock)[targetSize] != sentinel, + "LZ4_compress_HC_destSize() overwrites dst buffer"); + FUZ_CHECKTEST(srcSize < 1045000, "LZ4_compress_HC_destSize() doesn't compress enough" + " (%i -> %i , expected > %i)", srcSize, result, 1045000); LZ4_resetStreamHC_fast(&sHC, 3); /* make sure the context is clean after the test */ free(block); @@ -1247,7 +1251,7 @@ static void FUZ_unitTests(int compressionLevel) DISPLAYLEVEL(3, " OK \n"); /* simple dictionary HC compression test */ - DISPLAYLEVEL(3, " HC dictionary compression test : "); + DISPLAYLEVEL(3, "HC dictionary compression test : "); { U64 const crc64 = XXH64(testInput + 64 KB, testCompressedSize, 0); LZ4_resetStreamHC_fast(&sHC, compressionLevel); LZ4_loadDictHC(&sHC, testInput, 64 KB); @@ -1316,6 +1320,7 @@ static void FUZ_unitTests(int compressionLevel) XXH64_reset(&crcNewState, 0); while (segStart + segSize < testInputSize) { + XXH64_hash_t crcOrig; XXH64_update(&crcOrigState, testInput + segStart, segSize); crcOrig = XXH64_digest(&crcOrigState); assert(segSize <= INT_MAX); @@ -1362,6 +1367,7 @@ static void FUZ_unitTests(int compressionLevel) while (iNext + messageSize < testCompressedSize) { int compressedSize; + XXH64_hash_t crcOrig; XXH64_update(&xxhOrig, testInput + iNext, messageSize); crcOrig = XXH64_digest(&xxhOrig); @@ -1376,7 +1382,7 @@ static void FUZ_unitTests(int compressionLevel) FUZ_CHECKTEST(result!=(int)messageSize, "ringBuffer : LZ4_decompress_safe_continue() test failed"); XXH64_update(&xxhNewSafe, testVerify + dNext, messageSize); - { U64 const crcNew = XXH64_digest(&xxhNewSafe); + { XXH64_hash_t const crcNew = XXH64_digest(&xxhNewSafe); FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe_continue() decompression corruption"); } assert(messageSize < INT_MAX); @@ -1384,7 +1390,7 @@ static void FUZ_unitTests(int compressionLevel) FUZ_CHECKTEST(result!=compressedSize, "ringBuffer : LZ4_decompress_fast_continue() test failed"); XXH64_update(&xxhNewFast, testVerify + dNext, messageSize); - { U64 const crcNew = XXH64_digest(&xxhNewFast); + { XXH64_hash_t const crcNew = XXH64_digest(&xxhNewFast); FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_fast_continue() decompression corruption"); } /* prepare next message */ @@ -1405,6 +1411,7 @@ static void FUZ_unitTests(int compressionLevel) */ { XXH64_state_t xxhOrig; XXH64_state_t xxhNewSafe, xxhNewFast; + XXH64_hash_t crcOrig; LZ4_streamDecode_t decodeStateSafe, decodeStateFast; const int maxMessageSizeLog = 12; const int maxMessageSize = 1 << maxMessageSizeLog; @@ -1501,8 +1508,85 @@ static void FUZ_unitTests(int compressionLevel) iNext = (FUZ_rand(&randState) & 65535); if (dNext + maxMessageSize > dBufferSize) dNext = 0; } + } /* Ring buffer test : Non synchronized decoder */ + } + + DISPLAYLEVEL(3, "LZ4_compress_HC_destSize : "); + /* encode congenerical sequence test for HC compressors */ + { LZ4_streamHC_t sHC; /* statically allocated */ + int const src_buf_size = 3 MB; + int const dst_buf_size = 6 KB; + int const payload = 0; + int const dst_step = 43; + int const dst_min_len = 33 + (FUZ_rand(&randState) % dst_step); + int const dst_max_len = 5000; + int slen, dlen; + char* sbuf1 = (char*)malloc(src_buf_size + 1); + char* sbuf2 = (char*)malloc(src_buf_size + 1); + char* dbuf1 = (char*)malloc(dst_buf_size + 1); + char* dbuf2 = (char*)malloc(dst_buf_size + 1); + + assert(dst_buf_size > dst_max_len); + if (!sbuf1 || !sbuf2 || !dbuf1 || !dbuf2) { + EXIT_MSG("not enough memory for FUZ_unitTests (destSize)"); + } + for (dlen = dst_min_len; dlen <= dst_max_len; dlen += dst_step) { + int src_len = (dlen - 10)*255 + 24; + if (src_len + 10 >= src_buf_size) break; /* END of check */ + for (slen = src_len - 3; slen <= src_len + 3; slen++) { + int srcsz1, srcsz2; + int dsz1, dsz2; + int res1, res2; + char const endchk = (char)0x88; + DISPLAYLEVEL(5, "slen = %i, ", slen); + + srcsz1 = slen; + memset(sbuf1, payload, slen); + memset(dbuf1, 0, dlen); + dbuf1[dlen] = endchk; + dsz1 = LZ4_compress_destSize(sbuf1, dbuf1, &srcsz1, dlen); + DISPLAYLEVEL(5, "LZ4_compress_destSize: %i bytes compressed into %i bytes, ", srcsz1, dsz1); + DISPLAYLEVEL(5, "last token : 0x%0X, ", dbuf1[dsz1 - 6]); + DISPLAYLEVEL(5, "last ML extra lenbyte : 0x%0X, \n", dbuf1[dsz1 - 7]); + FUZ_CHECKTEST(dbuf1[dlen] != endchk, "LZ4_compress_destSize() overwrite dst buffer !"); + FUZ_CHECKTEST(dsz1 <= 0, "LZ4_compress_destSize() compression failed"); + FUZ_CHECKTEST(dsz1 > dlen, "LZ4_compress_destSize() result larger than dst buffer !"); + FUZ_CHECKTEST(srcsz1 > slen, "LZ4_compress_destSize() read more than src buffer !"); + + res1 = LZ4_decompress_safe(dbuf1, sbuf1, dsz1, src_buf_size); + FUZ_CHECKTEST(res1 != srcsz1, "LZ4_compress_destSize() decompression failed!"); + + srcsz2 = slen; + memset(sbuf2, payload, slen); + memset(dbuf2, 0, dlen); + dbuf2[dlen] = endchk; + LZ4_resetStreamHC(&sHC, compressionLevel); + dsz2 = LZ4_compress_HC_destSize(&sHC, sbuf2, dbuf2, &srcsz2, dlen, compressionLevel); + DISPLAYLEVEL(5, "LZ4_compress_HC_destSize: %i bytes compressed into %i bytes, ", srcsz2, dsz2); + DISPLAYLEVEL(5, "last token : 0x%0X, ", dbuf2[dsz2 - 6]); + DISPLAYLEVEL(5, "last ML extra lenbyte : 0x%0X, \n", dbuf2[dsz2 - 7]); + FUZ_CHECKTEST(dbuf2[dlen] != endchk, "LZ4_compress_HC_destSize() overwrite dst buffer !"); + FUZ_CHECKTEST(dsz2 <= 0, "LZ4_compress_HC_destSize() compression failed"); + FUZ_CHECKTEST(dsz2 > dlen, "LZ4_compress_HC_destSize() result larger than dst buffer !"); + FUZ_CHECKTEST(srcsz2 > slen, "LZ4_compress_HC_destSize() read more than src buffer !"); + FUZ_CHECKTEST(dsz2 != dsz1, "LZ4_compress_HC_destSize() return incorrect result !"); + FUZ_CHECKTEST(srcsz2 != srcsz1, "LZ4_compress_HC_destSize() return incorrect src buffer size " + ": srcsz2(%i) != srcsz1(%i)", srcsz2, srcsz1); + FUZ_CHECKTEST(memcmp(dbuf2, dbuf1, (size_t)dsz2), "LZ4_compress_HC_destSize() return incorrect data into dst buffer !"); + + res2 = LZ4_decompress_safe(dbuf2, sbuf1, dsz2, src_buf_size); + FUZ_CHECKTEST(res2 != srcsz1, "LZ4_compress_HC_destSize() decompression failed!"); + + FUZ_CHECKTEST(memcmp(sbuf1, sbuf2, (size_t)res2), "LZ4_compress_HC_destSize() decompression corruption!"); + } } + free(sbuf1); + free(sbuf2); + free(dbuf1); + free(dbuf2); } + DISPLAYLEVEL(3, " OK \n"); + /* clean up */ free(testInput); -- cgit v1.2.3 From a13c79d56dacc0a21a95f3420737e5d3f5b61cf8 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sun, 27 Sep 2020 22:47:05 -0700 Subject: fix incorrect counting after truncation of last sequence --- lib/lz4hc.c | 5 +++-- tests/fuzzer.c | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/lz4hc.c b/lib/lz4hc.c index aefc95b..7c7b990 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -481,7 +481,7 @@ LZ4_FORCE_INLINE int LZ4HC_encodeSequence ( U32 const cost = 1 + llAdd + ll + 2 + mlAdd; if (start==NULL) start = *anchor; /* only works for single segment */ /* g_debuglog_enable = (pos >= 2228) & (pos <= 2262); */ - DEBUGLOG(6, "pos:%7u -- literals:%3u, match:%4i, offset:%5u, cost:%3u + %u", + DEBUGLOG(6, "pos:%7u -- literals:%4u, match:%4i, offset:%5u, cost:%4u + %5u", pos, (U32)(*ip - *anchor), matchLength, (U32)(*ip-match), cost, totalCost); @@ -726,7 +726,7 @@ _last_literals: lastRunSize -= llAdd; } DEBUGLOG(6, "Final literal run : %i literals", (int)lastRunSize); - ip = ip + lastRunSize; /* can be != iend if limit==fillOutput */ + ip = anchor + lastRunSize; /* can be != iend if limit==fillOutput */ if (lastRunSize >= RUN_MASK) { size_t accumulator = lastRunSize - RUN_MASK; @@ -751,6 +751,7 @@ _dest_overflow: size_t const ll_addbytes = (ll + 240) / 255; size_t const ll_totalCost = 1 + ll_addbytes + ll; BYTE* const maxLitPos = oend - 3; /* 2 for offset, 1 for token */ + DEBUGLOG(6, "Last sequence overflowing"); op = optr; /* restore correct out pointer */ if (op + ll_totalCost <= maxLitPos) { /* ll validated; now how many matches ? */ diff --git a/tests/fuzzer.c b/tests/fuzzer.c index e611bb4..ef9577d 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -447,7 +447,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c decodedBuffer[srcSize] = canary; { int const dSize = LZ4_decompress_safe(compressedBuffer, decodedBuffer, cSize, srcSize); FUZ_CHECKTEST(dSize<0, "LZ4_decompress_safe() failed on data compressed by LZ4_compressHC_destSize"); - FUZ_CHECKTEST(dSize!=srcSize, "LZ4_decompress_safe() failed : did not fully decompressed data"); + FUZ_CHECKTEST(dSize!=srcSize, "LZ4_decompress_safe() failed : decompressed %i bytes, was supposed to decompress %i bytes", dSize, srcSize); } FUZ_CHECKTEST(decodedBuffer[srcSize] != canary, "LZ4_decompress_safe() overwrite dst buffer !"); { U32 const crcDec = XXH32(decodedBuffer, (size_t)srcSize, 0); -- cgit v1.2.3 From 89736e4e27358ed463c2482553cfae1b1176c797 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sun, 27 Sep 2020 23:59:56 -0700 Subject: ensure last match not too close to end must respect MFLIMIT distance from oend --- lib/lz4.c | 17 +++++++++++------ lib/lz4hc.c | 44 +++++++++++++++++++++++++++++--------------- tests/fuzzer.c | 14 ++++++++------ 3 files changed, 48 insertions(+), 27 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index c192d5b..21c8b4c 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1824,10 +1824,10 @@ LZ4_decompress_generic( length = token & ML_MASK; if (length == ML_MASK) { - variable_length_error error = ok; - if ((checkOffset) && (unlikely(match + dictSize < lowPrefix))) { goto _output_error; } /* Error : offset outside buffers */ - length += read_variable_length(&ip, iend - LASTLITERALS + 1, endOnInput, 0, &error); - if (error != ok) { goto _output_error; } + variable_length_error error = ok; + if ((checkOffset) && (unlikely(match + dictSize < lowPrefix))) { goto _output_error; } /* Error : offset outside buffers */ + length += read_variable_length(&ip, iend - LASTLITERALS + 1, endOnInput, 0, &error); + if (error != ok) { goto _output_error; } if ((safeDecode) && unlikely((uptrval)(op)+length<(uptrval)op)) { goto _output_error; } /* overflow detection */ length += MINMATCH; if (op + length >= oend - FASTLOOP_SAFE_DISTANCE) { @@ -1853,7 +1853,7 @@ LZ4_decompress_generic( continue; } } } - if ((checkOffset) && (unlikely(match + dictSize < lowPrefix))) { goto _output_error; } /* Error : offset outside buffers */ + if (checkOffset && (unlikely(match + dictSize < lowPrefix))) { goto _output_error; } /* Error : offset outside buffers */ /* match starting within external dictionary */ if ((dict==usingExtDict) && (match < lowPrefix)) { if (unlikely(op+length > oend-LASTLITERALS)) { @@ -2003,7 +2003,12 @@ LZ4_decompress_generic( /* We must be on the last sequence (or invalid) because of the parsing limitations * so check that we exactly consume the input and don't overrun the output buffer. */ - if ((endOnInput) && ((ip+length != iend) || (cpy > oend))) { goto _output_error; } + if ((endOnInput) && ((ip+length != iend) || (cpy > oend))) { + DEBUGLOG(6, "should have been last run of literals") + DEBUGLOG(6, "ip(%p) + length(%i) = %p != iend (%p)", ip, (int)length, ip+length, iend); + DEBUGLOG(6, "or cpy(%p) > oend(%p)", cpy, oend); + goto _output_error; + } } memmove(op, ip, length); /* supports overlapping memory regions; only matters for in-place decompression scenarios */ ip += length; diff --git a/lib/lz4hc.c b/lib/lz4hc.c index 7c7b990..6e4d732 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -492,7 +492,11 @@ LZ4_FORCE_INLINE int LZ4HC_encodeSequence ( length = (size_t)(*ip - *anchor); LZ4_STATIC_ASSERT(notLimited == 0); /* Check output limit */ - if (limit && ((*op + (length / 255) + length + (2 + 1 + LASTLITERALS)) > oend)) return 1; + if (limit && ((*op + (length / 255) + length + (2 + 1 + LASTLITERALS)) > oend)) { + DEBUGLOG(6, "Not enough room to write %i literals (%i bytes remaining)", + (int)length, (int)(oend-*op)); + return 1; + } if (length >= RUN_MASK) { size_t len = length - RUN_MASK; *token = (RUN_MASK << ML_BITS); @@ -513,7 +517,10 @@ LZ4_FORCE_INLINE int LZ4HC_encodeSequence ( /* Encode MatchLength */ assert(matchLength >= MINMATCH); length = (size_t)matchLength - MINMATCH; - if (limit && (*op + (length / 255) + (1 + LASTLITERALS) > oend)) return 1; /* Check output limit */ + if (limit && (*op + (length / 255) + (1 + LASTLITERALS) > oend)) { + DEBUGLOG(6, "Not enough room to write match length"); + return 1; /* Check output limit */ + } if (length >= ML_MASK) { *token += ML_MASK; length -= ML_MASK; @@ -721,7 +728,7 @@ _last_literals: if (limit && (op + totalSize > oend)) { if (limit == limitedOutput) return 0; /* adapt lastRunSize to fill 'dest' */ - lastRunSize = (size_t)(oend - op) - 1; + lastRunSize = (size_t)(oend - op) - 1 /*token*/; llAdd = (lastRunSize + 256 - RUN_MASK) / 256; lastRunSize -= llAdd; } @@ -754,13 +761,14 @@ _dest_overflow: DEBUGLOG(6, "Last sequence overflowing"); op = optr; /* restore correct out pointer */ if (op + ll_totalCost <= maxLitPos) { - /* ll validated; now how many matches ? */ + /* ll validated; now adjust match length */ size_t const bytesLeftForMl = (size_t)(maxLitPos - (op+ll_totalCost)); size_t const maxMlSize = MINMATCH + (ML_MASK-1) + (bytesLeftForMl * 255); assert(maxMlSize < INT_MAX); assert(ml >= 0); if ((size_t)ml > maxMlSize) ml = (int)maxMlSize; - LZ4HC_encodeSequence(UPDATABLE(ip, op, anchor), ml, ref, notLimited, oend); - } + if ((oend + LASTLITERALS) - (op + ll_totalCost + 2) - 1 + ml >= MFLIMIT) { + LZ4HC_encodeSequence(UPDATABLE(ip, op, anchor), ml, ref, notLimited, oend); + } } goto _last_literals; } /* compression failed */ @@ -1539,8 +1547,8 @@ encode: /* cur, last_match_pos, best_mlen, best_off must be set */ _last_literals: /* Encode Last Literals */ { size_t lastRunSize = (size_t)(iend - anchor); /* literals */ - size_t litLength = (lastRunSize + 255 - RUN_MASK) / 255; - size_t const totalSize = 1 + litLength + lastRunSize; + size_t llAdd = (lastRunSize + 255 - RUN_MASK) / 255; + size_t const totalSize = 1 + llAdd + lastRunSize; if (limit == fillOutput) oend += LASTLITERALS; /* restore correct value */ if (limit && (op + totalSize > oend)) { if (limit == limitedOutput) { /* Check output limit */ @@ -1548,11 +1556,12 @@ _last_literals: goto _return_label; } /* adapt lastRunSize to fill 'dst' */ - lastRunSize = (size_t)(oend - op) - 1; - litLength = (lastRunSize + 255 - RUN_MASK) / 255; - lastRunSize -= litLength; + lastRunSize = (size_t)(oend - op) - 1 /*token*/; + llAdd = (lastRunSize + 256 - RUN_MASK) / 256; + lastRunSize -= llAdd; } - ip = anchor + lastRunSize; + DEBUGLOG(6, "Final literal run : %i literals", (int)lastRunSize); + ip = anchor + lastRunSize; /* can be != iend if limit==fillOutput */ if (lastRunSize >= RUN_MASK) { size_t accumulator = lastRunSize - RUN_MASK; @@ -1578,15 +1587,20 @@ if (limit == fillOutput) { size_t const ll_addbytes = (ll + 240) / 255; size_t const ll_totalCost = 1 + ll_addbytes + ll; BYTE* const maxLitPos = oend - 3; /* 2 for offset, 1 for token */ + DEBUGLOG(6, "Last sequence overflowing (only %i bytes remaining)", (int)(oend-1-opSaved)); op = opSaved; /* restore correct out pointer */ if (op + ll_totalCost <= maxLitPos) { - /* ll validated; now how many matches ? */ + /* ll validated; now adjust match length */ size_t const bytesLeftForMl = (size_t)(maxLitPos - (op+ll_totalCost)); size_t const maxMlSize = MINMATCH + (ML_MASK-1) + (bytesLeftForMl * 255); assert(maxMlSize < INT_MAX); assert(ovml >= 0); if ((size_t)ovml > maxMlSize) ovml = (int)maxMlSize; - LZ4HC_encodeSequence(UPDATABLE(ip, op, anchor), ovml, ovref, notLimited, oend); - } + if ((oend + LASTLITERALS) - (op + ll_totalCost + 2) - 1 + ovml >= MFLIMIT) { + DEBUGLOG(6, "Space to end : %i + ml (%i)", (int)((oend + LASTLITERALS) - (op + ll_totalCost + 2) - 1), ovml); + DEBUGLOG(6, "Before : ip = %p, anchor = %p", ip, anchor); + LZ4HC_encodeSequence(UPDATABLE(ip, op, anchor), ovml, ovref, notLimited, oend); + DEBUGLOG(6, "After : ip = %p, anchor = %p", ip, anchor); + } } goto _last_literals; } _return_label: diff --git a/tests/fuzzer.c b/tests/fuzzer.c index ef9577d..14d1190 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -446,10 +446,10 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c FUZ_DISPLAYTEST(); decodedBuffer[srcSize] = canary; { int const dSize = LZ4_decompress_safe(compressedBuffer, decodedBuffer, cSize, srcSize); - FUZ_CHECKTEST(dSize<0, "LZ4_decompress_safe() failed on data compressed by LZ4_compressHC_destSize"); - FUZ_CHECKTEST(dSize!=srcSize, "LZ4_decompress_safe() failed : decompressed %i bytes, was supposed to decompress %i bytes", dSize, srcSize); + FUZ_CHECKTEST(dSize<0, "LZ4_decompress_safe failed (%i) on data compressed by LZ4_compressHC_destSize", dSize); + FUZ_CHECKTEST(dSize!=srcSize, "LZ4_decompress_safe failed : decompressed %i bytes, was supposed to decompress %i bytes", dSize, srcSize); } - FUZ_CHECKTEST(decodedBuffer[srcSize] != canary, "LZ4_decompress_safe() overwrite dst buffer !"); + FUZ_CHECKTEST(decodedBuffer[srcSize] != canary, "LZ4_decompress_safe overwrite dst buffer !"); { U32 const crcDec = XXH32(decodedBuffer, (size_t)srcSize, 0); FUZ_CHECKTEST(crcDec!=crcBase, "LZ4_decompress_safe() corrupted decoded data"); } } @@ -931,7 +931,8 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c LZ4_resetStreamHC_fast (LZ4_streamHC, compressionLevel); LZ4_attach_HC_dictionary(LZ4_streamHC, LZ4dictHC); ret = LZ4_compress_HC_continue(LZ4_streamHC, block, compressedBuffer, blockSize, blockContinueCompressedSize); - FUZ_CHECKTEST(ret!=blockContinueCompressedSize, "LZ4_compress_HC_continue using ExtDictCtx and fast reset size is different (%i != %i)", ret, blockContinueCompressedSize); + FUZ_CHECKTEST(ret!=blockContinueCompressedSize, "LZ4_compress_HC_continue using ExtDictCtx and fast reset size is different (%i != %i)", + ret, blockContinueCompressedSize); FUZ_CHECKTEST(ret<=0, "LZ4_compress_HC_continue using ExtDictCtx and fast reset should work : enough size available within output buffer"); FUZ_CHECKTEST(LZ4_streamHC->internal_donotuse.dirty, "Context should be clean"); @@ -957,7 +958,8 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c LZ4_loadDictHC(LZ4dictHC, dict, dictSize); LZ4_setCompressionLevel(LZ4dictHC, compressionLevel); blockContinueCompressedSize = LZ4_compress_HC_continue_destSize(LZ4dictHC, block, compressedBuffer, &consumedSize, availableSpace); - DISPLAYLEVEL(5, " LZ4_compress_HC_continue_destSize : compressed %6i/%6i into %6i/%6i at cLevel=%i\n", consumedSize, blockSize, blockContinueCompressedSize, availableSpace, compressionLevel); + DISPLAYLEVEL(5, " LZ4_compress_HC_continue_destSize : compressed %6i/%6i into %6i/%6i at cLevel=%i \n", + consumedSize, blockSize, blockContinueCompressedSize, availableSpace, compressionLevel); FUZ_CHECKTEST(blockContinueCompressedSize==0, "LZ4_compress_HC_continue_destSize failed"); FUZ_CHECKTEST(blockContinueCompressedSize > availableSpace, "LZ4_compress_HC_continue_destSize write overflow"); FUZ_CHECKTEST(consumedSize > blockSize, "LZ4_compress_HC_continue_destSize read overflow"); @@ -965,7 +967,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c FUZ_DISPLAYTEST(); decodedBuffer[consumedSize] = 0; ret = LZ4_decompress_safe_usingDict(compressedBuffer, decodedBuffer, blockContinueCompressedSize, consumedSize, dict, dictSize); - FUZ_CHECKTEST(ret!=consumedSize, "LZ4_decompress_safe_usingDict did not regenerate original data"); + FUZ_CHECKTEST(ret != consumedSize, "LZ4_decompress_safe_usingDict regenerated %i bytes (%i expected)", ret, consumedSize); FUZ_CHECKTEST(decodedBuffer[consumedSize], "LZ4_decompress_safe_usingDict overrun specified output buffer size") { U32 const crcSrc = XXH32(block, (size_t)consumedSize, 0); U32 const crcDst = XXH32(decodedBuffer, (size_t)consumedSize, 0); -- cgit v1.2.3 From ab89dda91d2c878c1f0638dabacf97b5a68ec361 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 28 Sep 2020 11:39:00 -0700 Subject: improved last literals run on LZ4_compress_destSize applying new more accurate formula from LZ4_compress_HC_destSize() also : fix some minor display issue in tests/frametest --- lib/lz4.c | 4 ++-- tests/frametest.c | 18 ++++++++++-------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 21c8b4c..dd0ed4b 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1185,8 +1185,8 @@ _last_literals: if (outputDirective == fillOutput) { /* adapt lastRun to fill 'dst' */ assert(olimit >= op); - lastRun = (size_t)(olimit-op) - 1; - lastRun -= (lastRun+240)/255; + lastRun = (size_t)(olimit-op) - 1/*token*/; + lastRun -= (lastRun + 256 - RUN_MASK) / 256; /*additional length tokens*/ } else { assert(outputDirective == limitedOutput); return 0; /* cannot compress within `dst` budget. Stored indexes in hash table are nonetheless fine */ diff --git a/tests/frametest.c b/tests/frametest.c index 54c7fd2..3a0f64f 100644 --- a/tests/frametest.c +++ b/tests/frametest.c @@ -815,7 +815,8 @@ size_t test_lz4f_decompression_wBuffers( U64 crcOrig, U32* const randState, LZ4F_dctx* const dCtx, - U32 seed, U32 testNb) + U32 seed, U32 testNb, + int findErrorPos) { const BYTE* ip = (const BYTE*)cSrc; const BYTE* const iend = ip + cSize; @@ -863,8 +864,8 @@ size_t test_lz4f_decompression_wBuffers( op[oSizeMax], mark); } if (LZ4F_getErrorCode(moreToFlush) == LZ4F_ERROR_contentChecksum_invalid) { - DISPLAYLEVEL(2, "checksum error detected \n"); - locateBuffDiff(srcRef, dst, decompressedSize, o_scenario); + if (findErrorPos) DISPLAYLEVEL(2, "checksum error detected \n"); + if (findErrorPos) locateBuffDiff(srcRef, dst, decompressedSize, o_scenario); } if (LZ4F_isError(moreToFlush)) return moreToFlush; @@ -885,7 +886,7 @@ size_t test_lz4f_decompression_wBuffers( if (totalOut) { /* otherwise, it's a skippable frame */ U64 const crcDecoded = XXH64_digest(&xxh64); if (crcDecoded != crcOrig) { - locateBuffDiff(srcRef, dst, decompressedSize, o_scenario); + if (findErrorPos) locateBuffDiff(srcRef, dst, decompressedSize, o_scenario); return LZ4F_ERROR_contentChecksum_invalid; } } return 0; @@ -897,7 +898,8 @@ size_t test_lz4f_decompression(const void* cSrc, size_t cSize, U64 crcOrig, U32* const randState, LZ4F_dctx* const dCtx, - U32 seed, U32 testNb) + U32 seed, U32 testNb, + int findErrorPos) { o_scenario_e const o_scenario = (o_scenario_e)(FUZ_rand(randState) % 3); /* 0 : contiguous; 1 : non-contiguous; 2 : dst overwritten */ /* tighten dst buffer conditions */ @@ -914,7 +916,7 @@ size_t test_lz4f_decompression(const void* cSrc, size_t cSize, crcOrig, randState, dCtx, - seed, testNb); + seed, testNb, findErrorPos); free(dstBuffer); return result; @@ -1068,7 +1070,7 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi /* multi-segments decompression */ DISPLAYLEVEL(6, "normal decompression \n"); - { size_t result = test_lz4f_decompression(compressedBuffer, cSize, srcStart, srcSize, crcOrig, &randState, dCtx, seed, testNb); + { size_t result = test_lz4f_decompression(compressedBuffer, cSize, srcStart, srcSize, crcOrig, &randState, dCtx, seed, testNb, 1 /*findError*/ ); CHECK (LZ4F_isError(result), "multi-segment decompression failed (error %i => %s)", (int)result, LZ4F_getErrorName(result)); } @@ -1098,7 +1100,7 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi /* test decompression on noisy src */ DISPLAYLEVEL(6, "noisy decompression \n"); - test_lz4f_decompression(compressedBuffer, cSize, srcStart, srcSize, crcOrig, &randState, dCtxNoise, seed, testNb); + test_lz4f_decompression(compressedBuffer, cSize, srcStart, srcSize, crcOrig, &randState, dCtxNoise, seed, testNb, 0 /*don't search error Pos*/ ); /* note : we don't analyze result here : it probably failed, which is expected. * The sole purpose is to catch potential out-of-bound reads and writes. */ LZ4F_resetDecompressionContext(dCtxNoise); /* context must be reset after an error */ -- cgit v1.2.3 From 293713a4fa085d73f396200d2387631b045c118e Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 29 Sep 2020 14:27:39 -0700 Subject: bump version number to v1.9.3 --- doc/lz4_manual.html | 66 ++++++++++++++++++++++++++++++++---------------- doc/lz4frame_manual.html | 6 ++--- lib/lz4.h | 2 +- 3 files changed, 48 insertions(+), 26 deletions(-) diff --git a/doc/lz4_manual.html b/doc/lz4_manual.html index a477584..208dbb9 100644 --- a/doc/lz4_manual.html +++ b/doc/lz4_manual.html @@ -1,10 +1,10 @@ -1.9.2 Manual +1.9.3 Manual -

        1.9.2 Manual

        +

        1.9.3 Manual


        Contents

          @@ -117,7 +117,8 @@ The larger the acceleration value, the faster the algorithm, but also the lesser the compression. It's a trade-off. It can be fine tuned, with each successive value providing roughly +~3% to speed. An acceleration value of "1" is the same as regular LZ4_compress_default() - Values <= 0 will be replaced by ACCELERATION_DEFAULT (currently == 1, see lz4.c). + Values <= 0 will be replaced by LZ4_ACCELERATION_DEFAULT (currently == 1, see lz4.c). + Values > LZ4_ACCELERATION_MAX will be replaced by LZ4_ACCELERATION_MAX (currently == 65537, see lz4.c).


    int LZ4_sizeofState(void);
    @@ -140,31 +141,53 @@ int LZ4_compress_fast_extState (void* state, const char* src, char* dst, int src
                    New value is necessarily <= input value.
      @return : Nb bytes written into 'dst' (necessarily <= targetDestSize)
                or 0 if compression fails.
    +
    + Note : from v1.8.2 to v1.9.1, this function had a bug (fixed un v1.9.2+):
    +        the produced compressed content could, in specific circumstances,
    +        require to be decompressed into a destination buffer larger
    +        by at least 1 byte than the content to decompress.
    +        If an application uses `LZ4_compress_destSize()`,
    +        it's highly recommended to update liblz4 to v1.9.2 or better.
    +        If this can't be done or ensured,
    +        the receiving decompression function should provide
    +        a dstCapacity which is > decompressedSize, by at least 1 byte.
    +        See https://github.com/lz4/lz4/issues/859 for details
    + 
     


    int LZ4_decompress_safe_partial (const char* src, char* dst, int srcSize, int targetOutputSize, int dstCapacity);
     

    Decompress an LZ4 compressed block, of size 'srcSize' at position 'src', into destination buffer 'dst' of size 'dstCapacity'. Up to 'targetOutputSize' bytes will be decoded. - The function stops decoding on reaching this objective, - which can boost performance when only the beginning of a block is required. + The function stops decoding on reaching this objective. + This can be useful to boost performance + whenever only the beginning of a block is required. - @return : the number of bytes decoded in `dst` (necessarily <= dstCapacity) + @return : the number of bytes decoded in `dst` (necessarily <= targetOutputSize) If source stream is detected malformed, function returns a negative result. - Note : @return can be < targetOutputSize, if compressed block contains less data. + Note 1 : @return can be < targetOutputSize, if compressed block contains less data. + + Note 2 : targetOutputSize must be <= dstCapacity - Note 2 : this function features 2 parameters, targetOutputSize and dstCapacity, - and expects targetOutputSize <= dstCapacity. - It effectively stops decoding on reaching targetOutputSize, + Note 3 : this function effectively stops decoding on reaching targetOutputSize, so dstCapacity is kind of redundant. - This is because in a previous version of this function, - decoding operation would not "break" a sequence in the middle. - As a consequence, there was no guarantee that decoding would stop at exactly targetOutputSize, + This is because in older versions of this function, + decoding operation would still write complete sequences. + Therefore, there was no guarantee that it would stop writing at exactly targetOutputSize, it could write more bytes, though only up to dstCapacity. Some "margin" used to be required for this operation to work properly. - This is no longer necessary. - The function nonetheless keeps its signature, in an effort to not break API. + Thankfully, this is no longer necessary. + The function nonetheless keeps the same signature, in an effort to preserve API compatibility. + + Note 4 : If srcSize is the exact size of the block, + then targetOutputSize can be any value, + including larger than the block's decompressed size. + The function will, at most, generate block's decompressed size. + + Note 5 : If srcSize is _larger_ than block's compressed size, + then targetOutputSize **MUST** be <= block's decompressed size. + Otherwise, *silent corruption will occur*.


    @@ -494,18 +517,17 @@ union LZ4_streamDecode_u {
    #ifdef LZ4_DISABLE_DEPRECATE_WARNINGS
     #  define LZ4_DEPRECATED(message)   /* disable deprecation warnings */
     #else
    -#  define LZ4_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
     #  if defined (__cplusplus) && (__cplusplus >= 201402) /* C++14 or greater */
     #    define LZ4_DEPRECATED(message) [[deprecated(message)]]
    -#  elif (LZ4_GCC_VERSION >= 405) || defined(__clang__)
    -#    define LZ4_DEPRECATED(message) __attribute__((deprecated(message)))
    -#  elif (LZ4_GCC_VERSION >= 301)
    -#    define LZ4_DEPRECATED(message) __attribute__((deprecated))
     #  elif defined(_MSC_VER)
     #    define LZ4_DEPRECATED(message) __declspec(deprecated(message))
    +#  elif defined(__clang__) || (defined(__GNUC__) && (__GNUC__ * 10 + __GNUC_MINOR__ >= 45))
    +#    define LZ4_DEPRECATED(message) __attribute__((deprecated(message)))
    +#  elif defined(__GNUC__) && (__GNUC__ * 10 + __GNUC_MINOR__ >= 31)
    +#    define LZ4_DEPRECATED(message) __attribute__((deprecated))
     #  else
    -#    pragma message("WARNING: You need to implement LZ4_DEPRECATED for this compiler")
    -#    define LZ4_DEPRECATED(message)
    +#    pragma message("WARNING: LZ4_DEPRECATED needs custom implementation for this compiler")
    +#    define LZ4_DEPRECATED(message)   /* disabled */
     #  endif
     #endif /* LZ4_DISABLE_DEPRECATE_WARNINGS */
     

    diff --git a/doc/lz4frame_manual.html b/doc/lz4frame_manual.html index 72f27c8..607583b 100644 --- a/doc/lz4frame_manual.html +++ b/doc/lz4frame_manual.html @@ -1,10 +1,10 @@ -1.9.2 Manual +1.9.3 Manual -

    1.9.2 Manual

    +

    1.9.3 Manual


    Contents

      @@ -167,7 +167,7 @@ LZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_cctx* cctx); @return is always the same for a srcSize and prefsPtr. prefsPtr is optional : when NULL is provided, preferences will be set to cover worst case scenario. tech details : - @return includes the possibility that internal buffer might already be filled by up to (blockSize-1) bytes. + @return if automatic flushing is not enabled, includes the possibility that internal buffer might already be filled by up to (blockSize-1) bytes. It also includes frame footer (ending + checksum), since it might be generated by LZ4F_compressEnd(). @return doesn't include frame header, as it was already generated by LZ4F_compressBegin(). diff --git a/lib/lz4.h b/lib/lz4.h index 5474005..b11275e 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -100,7 +100,7 @@ extern "C" { /*------ Version ------*/ #define LZ4_VERSION_MAJOR 1 /* for breaking interface changes */ #define LZ4_VERSION_MINOR 9 /* for new (non-breaking) interface capabilities */ -#define LZ4_VERSION_RELEASE 2 /* for tweaks, bug-fixes, or development */ +#define LZ4_VERSION_RELEASE 3 /* for tweaks, bug-fixes, or development */ #define LZ4_VERSION_NUMBER (LZ4_VERSION_MAJOR *100*100 + LZ4_VERSION_MINOR *100 + LZ4_VERSION_RELEASE) -- cgit v1.2.3 From 392809d66e6c98a7c8653a2a32f6843af0f4306a Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 28 Sep 2020 22:37:20 -0700 Subject: fix minor static analyzer warnings detected by scan-build and cppcheck fix #786 --- doc/lz4_manual.html | 62 ++++++++++++++++++++++++++++------------- doc/lz4frame_manual.html | 2 +- lib/lz4frame.c | 21 +++++++------- lib/lz4hc.c | 9 +++--- ossfuzz/compress_frame_fuzzer.c | 4 +-- ossfuzz/fuzz_data_producer.c | 4 +-- programs/lz4cli.c | 2 +- programs/lz4io.c | 22 +++++++-------- programs/lz4io.h | 8 +++--- tests/checkFrame.c | 1 + tests/fullbench.c | 24 ++++++++-------- 11 files changed, 90 insertions(+), 69 deletions(-) diff --git a/doc/lz4_manual.html b/doc/lz4_manual.html index a477584..edb90c7 100644 --- a/doc/lz4_manual.html +++ b/doc/lz4_manual.html @@ -117,7 +117,8 @@ The larger the acceleration value, the faster the algorithm, but also the lesser the compression. It's a trade-off. It can be fine tuned, with each successive value providing roughly +~3% to speed. An acceleration value of "1" is the same as regular LZ4_compress_default() - Values <= 0 will be replaced by ACCELERATION_DEFAULT (currently == 1, see lz4.c). + Values <= 0 will be replaced by LZ4_ACCELERATION_DEFAULT (currently == 1, see lz4.c). + Values > LZ4_ACCELERATION_MAX will be replaced by LZ4_ACCELERATION_MAX (currently == 65537, see lz4.c).


    int LZ4_sizeofState(void);
    @@ -140,31 +141,53 @@ int LZ4_compress_fast_extState (void* state, const char* src, char* dst, int src
                    New value is necessarily <= input value.
      @return : Nb bytes written into 'dst' (necessarily <= targetDestSize)
                or 0 if compression fails.
    +
    + Note : from v1.8.2 to v1.9.1, this function had a bug (fixed un v1.9.2+):
    +        the produced compressed content could, in specific circumstances,
    +        require to be decompressed into a destination buffer larger
    +        by at least 1 byte than the content to decompress.
    +        If an application uses `LZ4_compress_destSize()`,
    +        it's highly recommended to update liblz4 to v1.9.2 or better.
    +        If this can't be done or ensured,
    +        the receiving decompression function should provide
    +        a dstCapacity which is > decompressedSize, by at least 1 byte.
    +        See https://github.com/lz4/lz4/issues/859 for details
    + 
     


    int LZ4_decompress_safe_partial (const char* src, char* dst, int srcSize, int targetOutputSize, int dstCapacity);
     

    Decompress an LZ4 compressed block, of size 'srcSize' at position 'src', into destination buffer 'dst' of size 'dstCapacity'. Up to 'targetOutputSize' bytes will be decoded. - The function stops decoding on reaching this objective, - which can boost performance when only the beginning of a block is required. + The function stops decoding on reaching this objective. + This can be useful to boost performance + whenever only the beginning of a block is required. - @return : the number of bytes decoded in `dst` (necessarily <= dstCapacity) + @return : the number of bytes decoded in `dst` (necessarily <= targetOutputSize) If source stream is detected malformed, function returns a negative result. - Note : @return can be < targetOutputSize, if compressed block contains less data. + Note 1 : @return can be < targetOutputSize, if compressed block contains less data. + + Note 2 : targetOutputSize must be <= dstCapacity - Note 2 : this function features 2 parameters, targetOutputSize and dstCapacity, - and expects targetOutputSize <= dstCapacity. - It effectively stops decoding on reaching targetOutputSize, + Note 3 : this function effectively stops decoding on reaching targetOutputSize, so dstCapacity is kind of redundant. - This is because in a previous version of this function, - decoding operation would not "break" a sequence in the middle. - As a consequence, there was no guarantee that decoding would stop at exactly targetOutputSize, + This is because in older versions of this function, + decoding operation would still write complete sequences. + Therefore, there was no guarantee that it would stop writing at exactly targetOutputSize, it could write more bytes, though only up to dstCapacity. Some "margin" used to be required for this operation to work properly. - This is no longer necessary. - The function nonetheless keeps its signature, in an effort to not break API. + Thankfully, this is no longer necessary. + The function nonetheless keeps the same signature, in an effort to preserve API compatibility. + + Note 4 : If srcSize is the exact size of the block, + then targetOutputSize can be any value, + including larger than the block's decompressed size. + The function will, at most, generate block's decompressed size. + + Note 5 : If srcSize is _larger_ than block's compressed size, + then targetOutputSize **MUST** be <= block's decompressed size. + Otherwise, *silent corruption will occur*.


    @@ -494,18 +517,17 @@ union LZ4_streamDecode_u {
    #ifdef LZ4_DISABLE_DEPRECATE_WARNINGS
     #  define LZ4_DEPRECATED(message)   /* disable deprecation warnings */
     #else
    -#  define LZ4_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
     #  if defined (__cplusplus) && (__cplusplus >= 201402) /* C++14 or greater */
     #    define LZ4_DEPRECATED(message) [[deprecated(message)]]
    -#  elif (LZ4_GCC_VERSION >= 405) || defined(__clang__)
    -#    define LZ4_DEPRECATED(message) __attribute__((deprecated(message)))
    -#  elif (LZ4_GCC_VERSION >= 301)
    -#    define LZ4_DEPRECATED(message) __attribute__((deprecated))
     #  elif defined(_MSC_VER)
     #    define LZ4_DEPRECATED(message) __declspec(deprecated(message))
    +#  elif defined(__clang__) || (defined(__GNUC__) && (__GNUC__ * 10 + __GNUC_MINOR__ >= 45))
    +#    define LZ4_DEPRECATED(message) __attribute__((deprecated(message)))
    +#  elif defined(__GNUC__) && (__GNUC__ * 10 + __GNUC_MINOR__ >= 31)
    +#    define LZ4_DEPRECATED(message) __attribute__((deprecated))
     #  else
    -#    pragma message("WARNING: You need to implement LZ4_DEPRECATED for this compiler")
    -#    define LZ4_DEPRECATED(message)
    +#    pragma message("WARNING: LZ4_DEPRECATED needs custom implementation for this compiler")
    +#    define LZ4_DEPRECATED(message)   /* disabled */
     #  endif
     #endif /* LZ4_DISABLE_DEPRECATE_WARNINGS */
     

    diff --git a/doc/lz4frame_manual.html b/doc/lz4frame_manual.html index 72f27c8..f94a647 100644 --- a/doc/lz4frame_manual.html +++ b/doc/lz4frame_manual.html @@ -167,7 +167,7 @@ LZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_cctx* cctx); @return is always the same for a srcSize and prefsPtr. prefsPtr is optional : when NULL is provided, preferences will be set to cover worst case scenario. tech details : - @return includes the possibility that internal buffer might already be filled by up to (blockSize-1) bytes. + @return if automatic flushing is not enabled, includes the possibility that internal buffer might already be filled by up to (blockSize-1) bytes. It also includes frame footer (ending + checksum), since it might be generated by LZ4F_compressEnd(). @return doesn't include frame header, as it was already generated by LZ4F_compressBegin(). diff --git a/lib/lz4frame.c b/lib/lz4frame.c index 890fc96..03facb0 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -533,7 +533,7 @@ void LZ4F_freeCDict(LZ4F_CDict* cdict) * 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, unsigned version) +LZ4F_errorCode_t LZ4F_createCompressionContext(LZ4F_cctx** LZ4F_compressionContextPtr, unsigned version) { LZ4F_cctx_t* const cctxPtr = (LZ4F_cctx_t*)ALLOC_AND_ZERO(sizeof(LZ4F_cctx_t)); if (cctxPtr==NULL) return err0r(LZ4F_ERROR_allocation_failed); @@ -541,20 +541,18 @@ LZ4F_errorCode_t LZ4F_createCompressionContext(LZ4F_compressionContext_t* LZ4F_c cctxPtr->version = version; cctxPtr->cStage = 0; /* Next stage : init stream */ - *LZ4F_compressionContextPtr = (LZ4F_compressionContext_t)cctxPtr; + *LZ4F_compressionContextPtr = cctxPtr; return LZ4F_OK_NoError; } -LZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_compressionContext_t LZ4F_compressionContext) +LZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_cctx* cctxPtr) { - LZ4F_cctx_t* const cctxPtr = (LZ4F_cctx_t*)LZ4F_compressionContext; - if (cctxPtr != NULL) { /* support free on NULL */ - FREEMEM(cctxPtr->lz4CtxPtr); /* works because LZ4_streamHC_t and LZ4_stream_t are simple POD types */ + FREEMEM(cctxPtr->lz4CtxPtr); /* note: LZ4_streamHC_t and LZ4_stream_t are simple POD types */ FREEMEM(cctxPtr->tmpBuff); - FREEMEM(LZ4F_compressionContext); + FREEMEM(cctxPtr); } return LZ4F_OK_NoError; @@ -1291,8 +1289,10 @@ static void LZ4F_updateDict(LZ4F_dctx* dctx, const BYTE* dstPtr, size_t dstSize, const BYTE* dstBufferStart, unsigned withinTmp) { - if (dctx->dictSize==0) + if (dctx->dictSize==0) { + assert(dstPtr != NULL); dctx->dict = (const BYTE*)dstPtr; /* priority to dictionary continuity */ + } if (dctx->dict + dctx->dictSize == dstPtr) { /* dictionary continuity, directly within dstBuffer */ dctx->dictSize += dstSize; @@ -1382,13 +1382,14 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, const BYTE* const srcEnd = srcStart + *srcSizePtr; const BYTE* srcPtr = srcStart; BYTE* const dstStart = (BYTE*)dstBuffer; - BYTE* const dstEnd = dstStart + *dstSizePtr; + BYTE* const dstEnd = dstStart ? dstStart + *dstSizePtr : NULL; BYTE* dstPtr = dstStart; const BYTE* selectedIn = NULL; unsigned doAnotherStage = 1; size_t nextSrcSizeHint = 1; + if (dstBuffer == NULL) assert(*dstSizePtr == 0); MEM_INIT(&optionsNull, 0, sizeof(optionsNull)); if (decompressOptionsPtr==NULL) decompressOptionsPtr = &optionsNull; *srcSizePtr = 0; @@ -1590,7 +1591,7 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, selectedIn = srcPtr; srcPtr += dctx->tmpInTarget; - if (0) /* jump over next block */ + if (0) /* always jump over next block */ case dstage_storeCBlock: { size_t const wantedData = dctx->tmpInTarget - dctx->tmpInSize; size_t const inputLeft = (size_t)(srcEnd-srcPtr); diff --git a/lib/lz4hc.c b/lib/lz4hc.c index 6e4d732..97dfabd 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -981,10 +981,10 @@ int LZ4_compress_HC_destSize(void* state, const char* source, char* dest, int* s /* allocation */ LZ4_streamHC_t* LZ4_createStreamHC(void) { - LZ4_streamHC_t* const LZ4_streamHCPtr = (LZ4_streamHC_t*)ALLOC(sizeof(LZ4_streamHC_t)); - if (LZ4_streamHCPtr==NULL) return NULL; - LZ4_initStreamHC(LZ4_streamHCPtr, sizeof(*LZ4_streamHCPtr)); /* full initialization, malloc'ed buffer can be full of garbage */ - return LZ4_streamHCPtr; + LZ4_streamHC_t* state = (LZ4_streamHC_t*)ALLOC(sizeof(LZ4_streamHC_t)); + if (state==NULL) return NULL; + state = LZ4_initStreamHC(state, sizeof(*state)); /* full initialization, malloc'ed buffer can be full of garbage */ + return state; } int LZ4_freeStreamHC (LZ4_streamHC_t* LZ4_streamHCPtr) @@ -1347,7 +1347,6 @@ static int LZ4HC_compress_optimal ( LZ4HC_CCtx_internal* ctx, if (sufficient_len >= LZ4_OPT_NUM) sufficient_len = LZ4_OPT_NUM-1; /* Main Loop */ - assert(ip - anchor < LZ4_MAX_INPUT_SIZE); while (ip <= mflimit) { int const llen = (int)(ip - anchor); int best_mlen, best_off; diff --git a/ossfuzz/compress_frame_fuzzer.c b/ossfuzz/compress_frame_fuzzer.c index bb14fc2..568ae14 100644 --- a/ossfuzz/compress_frame_fuzzer.c +++ b/ossfuzz/compress_frame_fuzzer.c @@ -28,8 +28,8 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) char* const dst = (char*)malloc(dstCapacity); char* const rt = (char*)malloc(size); - FUZZ_ASSERT(dst); - FUZZ_ASSERT(rt); + FUZZ_ASSERT(dst!=NULL); + FUZZ_ASSERT(rt!=NULL); /* If compression succeeds it must round trip correctly. */ size_t const dstSize = diff --git a/ossfuzz/fuzz_data_producer.c b/ossfuzz/fuzz_data_producer.c index cc06958..670fbf5 100644 --- a/ossfuzz/fuzz_data_producer.c +++ b/ossfuzz/fuzz_data_producer.c @@ -5,8 +5,8 @@ struct FUZZ_dataProducer_s{ size_t size; }; -FUZZ_dataProducer_t *FUZZ_dataProducer_create(const uint8_t *data, size_t size) { - FUZZ_dataProducer_t *producer = malloc(sizeof(FUZZ_dataProducer_t)); +FUZZ_dataProducer_t* FUZZ_dataProducer_create(const uint8_t* data, size_t size) { + FUZZ_dataProducer_t* const producer = malloc(sizeof(FUZZ_dataProducer_t)); FUZZ_ASSERT(producer != NULL); diff --git a/programs/lz4cli.c b/programs/lz4cli.c index 67dcaa1..d7d4f81 100644 --- a/programs/lz4cli.c +++ b/programs/lz4cli.c @@ -395,7 +395,7 @@ int main(int argc, const char** argv) if (!strcmp(argument, "--favor-decSpeed")) { LZ4IO_favorDecSpeed(prefs, 1); continue; } if (!strcmp(argument, "--verbose")) { displayLevel++; continue; } if (!strcmp(argument, "--quiet")) { if (displayLevel) displayLevel--; continue; } - if (!strcmp(argument, "--version")) { DISPLAYOUT(WELCOME_MESSAGE); return 0; } + if (!strcmp(argument, "--version")) { DISPLAYOUT(WELCOME_MESSAGE); goto _cleanup; } if (!strcmp(argument, "--help")) { usage_advanced(exeName); goto _cleanup; } if (!strcmp(argument, "--keep")) { LZ4IO_setRemoveSrcFile(prefs, 0); continue; } /* keep source file (default) */ if (!strcmp(argument, "--rm")) { LZ4IO_setRemoveSrcFile(prefs, 1); continue; } diff --git a/programs/lz4io.c b/programs/lz4io.c index 0f3507e..3ffc519 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -1291,7 +1291,7 @@ int LZ4IO_decompressMultipleFilenames(LZ4IO_prefs_t* const prefs, size_t const suffixSize = strlen(suffix); dRess_t ress = LZ4IO_createDResources(prefs); - if (outFileName==NULL) return ifntSize; /* not enough memory */ + if (outFileName==NULL) EXM_THROW(70, "Memory allocation error"); ress.dstFile = LZ4IO_openDstFile(prefs, stdoutmark); for (i=0; ifileSize = (finput == NULL) ? 0 : UTIL_getOpenFileSize(finput); + + if (finput == NULL) return LZ4IO_not_a_file; + cfinfo->fileSize = UTIL_getOpenFileSize(finput); while (!feof(finput)) { LZ4IO_frameInfo_t frameInfo = LZ4IO_INIT_FRAMEINFO; @@ -1560,8 +1562,7 @@ LZ4IO_getCompressedFileInfo(LZ4IO_cFileInfo_t* cfinfo, const char* input_filenam totalBlocksSize + 4, "-", "-"); result = LZ4IO_LZ4F_OK; - } - } + } } break; case LZ4IO_SKIPPABLE0: frameInfo.frameType = skippableFrame; @@ -1628,8 +1629,7 @@ int LZ4IO_displayCompressedFilesInfo(const char** inFileNames, size_t ifnIdx) assert(op_result == LZ4IO_format_not_known); DISPLAYLEVEL(1, "lz4: %s: File format not recognized \n", inFileNames[idx]); return 0; - } - } + } } DISPLAYLEVEL(3, "\n"); if (g_displayLevel < 3) { /* Display Summary */ @@ -1648,10 +1648,8 @@ int LZ4IO_displayCompressedFilesInfo(const char** inFileNames, size_t ifnIdx) DISPLAYOUT("%9s %s\n", "-", cfinfo.fileName); - } - } - } - } + } } } /* if (g_displayLevel < 3) */ + } /* for (; idx < ifnIdx; idx++) */ return result; } diff --git a/programs/lz4io.h b/programs/lz4io.h index b189e35..4763180 100644 --- a/programs/lz4io.h +++ b/programs/lz4io.h @@ -39,13 +39,13 @@ /* ************************************************** */ /* Special input/output values */ /* ************************************************** */ +#define stdinmark "stdin" +#define stdoutmark "stdout" #define NULL_OUTPUT "null" -static const char stdinmark[] = "stdin"; -static const char stdoutmark[] = "stdout"; #ifdef _WIN32 -static const char nulmark[] = "nul"; +#define nulmark "nul" #else -static const char nulmark[] = "/dev/null"; +#define nulmark "/dev/null" #endif /* ************************************************** */ diff --git a/tests/checkFrame.c b/tests/checkFrame.c index 139a599..f39d2ac 100644 --- a/tests/checkFrame.c +++ b/tests/checkFrame.c @@ -301,6 +301,7 @@ int main(int argc, const char** argv) freeCResources(ress); EXM_THROW(1, "%s: %s \n", argument, strerror(errno)); } + assert (srcFile != NULL); err = frameCheck(ress, srcFile, bsid, blockSize); freeCResources(ress); fclose(srcFile); diff --git a/tests/fullbench.c b/tests/fullbench.c index 7d74d3f..77f475b 100644 --- a/tests/fullbench.c +++ b/tests/fullbench.c @@ -399,25 +399,25 @@ int fullSpeedBench(const char** fileNamesTable, int nbFiles) char* compressed_buff=NULL; const char* const inFileName = fileNamesTable[fileIdx++]; FILE* const inFile = fopen( inFileName, "rb" ); - U64 inFileSize; - size_t benchedSize; + U64 const inFileSize = UTIL_getFileSize(inFileName); + size_t benchedSize = BMK_findMaxMem(inFileSize*2) / 2; /* because 2 buffers */ int nbChunks; int maxCompressedChunkSize; size_t readSize; int compressedBuffSize; U32 crcOriginal; - /* Check file existence */ - if (inFile==NULL) { DISPLAY( "Pb opening %s\n", inFileName); return 11; } + /* Check infile pre-requisites */ + if (inFile==NULL) { DISPLAY("Pb opening %s \n", inFileName); return 11; } + if (inFileSize==0) { DISPLAY("file is empty \n"); fclose(inFile); return 11; } + if (benchedSize==0) { DISPLAY("not enough memory \n"); fclose(inFile); return 11; } /* Memory size adjustments */ - inFileSize = UTIL_getFileSize(inFileName); - if (inFileSize==0) { DISPLAY( "file is empty\n"); fclose(inFile); return 11; } - benchedSize = BMK_findMaxMem(inFileSize*2) / 2; /* because 2 buffers */ - if (benchedSize==0) { DISPLAY( "not enough memory\n"); fclose(inFile); return 11; } if ((U64)benchedSize > inFileSize) benchedSize = (size_t)inFileSize; - if (benchedSize < inFileSize) - DISPLAY("Not enough memory for '%s' full size; testing %i MB only...\n", inFileName, (int)(benchedSize>>20)); + if (benchedSize < inFileSize) { + DISPLAY("Not enough memory for '%s' full size; testing %i MB only... \n", + inFileName, (int)(benchedSize>>20)); + } /* Allocation */ chunkP = (struct chunkParameters*) malloc(((benchedSize / (size_t)g_chunkSize)+1) * sizeof(struct chunkParameters)); @@ -427,7 +427,7 @@ int fullSpeedBench(const char** fileNamesTable, int nbFiles) compressedBuffSize = nbChunks * maxCompressedChunkSize; compressed_buff = (char*)malloc((size_t)compressedBuffSize); if(!chunkP || !orig_buff || !compressed_buff) { - DISPLAY("\nError: not enough memory!\n"); + DISPLAY("\nError: not enough memory! \n"); fclose(inFile); free(orig_buff); free(compressed_buff); @@ -475,7 +475,7 @@ int fullSpeedBench(const char** fileNamesTable, int nbFiles) size_t remaining = benchedSize; char* in = orig_buff; char* out = compressed_buff; - nbChunks = (int) (((int)benchedSize + (g_chunkSize-1))/ g_chunkSize); + assert(nbChunks >= 1); for (i=0; i Date: Mon, 28 Sep 2020 22:37:20 -0700 Subject: fix minor static analyzer warnings detected by scan-build, cppcheck and advanved compilation flags fix #786 --- doc/lz4_manual.html | 62 ++++++++++++++++++++++++++++------------- doc/lz4frame_manual.html | 2 +- lib/lz4.c | 36 +++++++++++++----------- lib/lz4frame.c | 21 +++++++------- lib/lz4hc.c | 19 ++++++------- ossfuzz/compress_frame_fuzzer.c | 4 +-- ossfuzz/fuzz_data_producer.c | 4 +-- programs/lz4cli.c | 2 +- programs/lz4io.c | 22 +++++++-------- programs/lz4io.h | 8 +++--- tests/checkFrame.c | 1 + tests/fullbench.c | 24 ++++++++-------- 12 files changed, 114 insertions(+), 91 deletions(-) diff --git a/doc/lz4_manual.html b/doc/lz4_manual.html index a477584..edb90c7 100644 --- a/doc/lz4_manual.html +++ b/doc/lz4_manual.html @@ -117,7 +117,8 @@ The larger the acceleration value, the faster the algorithm, but also the lesser the compression. It's a trade-off. It can be fine tuned, with each successive value providing roughly +~3% to speed. An acceleration value of "1" is the same as regular LZ4_compress_default() - Values <= 0 will be replaced by ACCELERATION_DEFAULT (currently == 1, see lz4.c). + Values <= 0 will be replaced by LZ4_ACCELERATION_DEFAULT (currently == 1, see lz4.c). + Values > LZ4_ACCELERATION_MAX will be replaced by LZ4_ACCELERATION_MAX (currently == 65537, see lz4.c).


    int LZ4_sizeofState(void);
    @@ -140,31 +141,53 @@ int LZ4_compress_fast_extState (void* state, const char* src, char* dst, int src
                    New value is necessarily <= input value.
      @return : Nb bytes written into 'dst' (necessarily <= targetDestSize)
                or 0 if compression fails.
    +
    + Note : from v1.8.2 to v1.9.1, this function had a bug (fixed un v1.9.2+):
    +        the produced compressed content could, in specific circumstances,
    +        require to be decompressed into a destination buffer larger
    +        by at least 1 byte than the content to decompress.
    +        If an application uses `LZ4_compress_destSize()`,
    +        it's highly recommended to update liblz4 to v1.9.2 or better.
    +        If this can't be done or ensured,
    +        the receiving decompression function should provide
    +        a dstCapacity which is > decompressedSize, by at least 1 byte.
    +        See https://github.com/lz4/lz4/issues/859 for details
    + 
     


    int LZ4_decompress_safe_partial (const char* src, char* dst, int srcSize, int targetOutputSize, int dstCapacity);
     

    Decompress an LZ4 compressed block, of size 'srcSize' at position 'src', into destination buffer 'dst' of size 'dstCapacity'. Up to 'targetOutputSize' bytes will be decoded. - The function stops decoding on reaching this objective, - which can boost performance when only the beginning of a block is required. + The function stops decoding on reaching this objective. + This can be useful to boost performance + whenever only the beginning of a block is required. - @return : the number of bytes decoded in `dst` (necessarily <= dstCapacity) + @return : the number of bytes decoded in `dst` (necessarily <= targetOutputSize) If source stream is detected malformed, function returns a negative result. - Note : @return can be < targetOutputSize, if compressed block contains less data. + Note 1 : @return can be < targetOutputSize, if compressed block contains less data. + + Note 2 : targetOutputSize must be <= dstCapacity - Note 2 : this function features 2 parameters, targetOutputSize and dstCapacity, - and expects targetOutputSize <= dstCapacity. - It effectively stops decoding on reaching targetOutputSize, + Note 3 : this function effectively stops decoding on reaching targetOutputSize, so dstCapacity is kind of redundant. - This is because in a previous version of this function, - decoding operation would not "break" a sequence in the middle. - As a consequence, there was no guarantee that decoding would stop at exactly targetOutputSize, + This is because in older versions of this function, + decoding operation would still write complete sequences. + Therefore, there was no guarantee that it would stop writing at exactly targetOutputSize, it could write more bytes, though only up to dstCapacity. Some "margin" used to be required for this operation to work properly. - This is no longer necessary. - The function nonetheless keeps its signature, in an effort to not break API. + Thankfully, this is no longer necessary. + The function nonetheless keeps the same signature, in an effort to preserve API compatibility. + + Note 4 : If srcSize is the exact size of the block, + then targetOutputSize can be any value, + including larger than the block's decompressed size. + The function will, at most, generate block's decompressed size. + + Note 5 : If srcSize is _larger_ than block's compressed size, + then targetOutputSize **MUST** be <= block's decompressed size. + Otherwise, *silent corruption will occur*.


    @@ -494,18 +517,17 @@ union LZ4_streamDecode_u {
    #ifdef LZ4_DISABLE_DEPRECATE_WARNINGS
     #  define LZ4_DEPRECATED(message)   /* disable deprecation warnings */
     #else
    -#  define LZ4_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
     #  if defined (__cplusplus) && (__cplusplus >= 201402) /* C++14 or greater */
     #    define LZ4_DEPRECATED(message) [[deprecated(message)]]
    -#  elif (LZ4_GCC_VERSION >= 405) || defined(__clang__)
    -#    define LZ4_DEPRECATED(message) __attribute__((deprecated(message)))
    -#  elif (LZ4_GCC_VERSION >= 301)
    -#    define LZ4_DEPRECATED(message) __attribute__((deprecated))
     #  elif defined(_MSC_VER)
     #    define LZ4_DEPRECATED(message) __declspec(deprecated(message))
    +#  elif defined(__clang__) || (defined(__GNUC__) && (__GNUC__ * 10 + __GNUC_MINOR__ >= 45))
    +#    define LZ4_DEPRECATED(message) __attribute__((deprecated(message)))
    +#  elif defined(__GNUC__) && (__GNUC__ * 10 + __GNUC_MINOR__ >= 31)
    +#    define LZ4_DEPRECATED(message) __attribute__((deprecated))
     #  else
    -#    pragma message("WARNING: You need to implement LZ4_DEPRECATED for this compiler")
    -#    define LZ4_DEPRECATED(message)
    +#    pragma message("WARNING: LZ4_DEPRECATED needs custom implementation for this compiler")
    +#    define LZ4_DEPRECATED(message)   /* disabled */
     #  endif
     #endif /* LZ4_DISABLE_DEPRECATE_WARNINGS */
     

    diff --git a/doc/lz4frame_manual.html b/doc/lz4frame_manual.html index 72f27c8..f94a647 100644 --- a/doc/lz4frame_manual.html +++ b/doc/lz4frame_manual.html @@ -167,7 +167,7 @@ LZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_cctx* cctx); @return is always the same for a srcSize and prefsPtr. prefsPtr is optional : when NULL is provided, preferences will be set to cover worst case scenario. tech details : - @return includes the possibility that internal buffer might already be filled by up to (blockSize-1) bytes. + @return if automatic flushing is not enabled, includes the possibility that internal buffer might already be filled by up to (blockSize-1) bytes. It also includes frame footer (ending + checksum), since it might be generated by LZ4F_compressEnd(). @return doesn't include frame header, as it was already generated by LZ4F_compressBegin(). diff --git a/lib/lz4.c b/lib/lz4.c index 17477a5..db6150a 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1683,25 +1683,27 @@ typedef enum { decode_full_block = 0, partial_decode = 1 } earlyEnd_directive; */ typedef enum { loop_error = -2, initial_error = -1, ok = 0 } variable_length_error; LZ4_FORCE_INLINE unsigned -read_variable_length(const BYTE**ip, const BYTE* lencheck, int loop_check, int initial_check, variable_length_error* error) -{ - U32 length = 0; - U32 s; - if (initial_check && unlikely((*ip) >= lencheck)) { /* overflow detection */ - *error = initial_error; - return length; - } - do { - s = **ip; - (*ip)++; - length += s; - if (loop_check && unlikely((*ip) >= lencheck)) { /* overflow detection */ - *error = loop_error; - return length; +read_variable_length(const BYTE**ip, const BYTE* lencheck, + endCondition_directive loop_check, endCondition_directive initial_check, + variable_length_error* error) +{ + U32 length = 0; + U32 s; + if (initial_check && unlikely((*ip) >= lencheck)) { /* overflow detection */ + *error = initial_error; + return length; } - } while (s==255); + do { + s = **ip; + (*ip)++; + length += s; + if (loop_check && unlikely((*ip) >= lencheck)) { /* overflow detection */ + *error = loop_error; + return length; + } + } while (s==255); - return length; + return length; } /*! LZ4_decompress_generic() : diff --git a/lib/lz4frame.c b/lib/lz4frame.c index 890fc96..03facb0 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -533,7 +533,7 @@ void LZ4F_freeCDict(LZ4F_CDict* cdict) * 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, unsigned version) +LZ4F_errorCode_t LZ4F_createCompressionContext(LZ4F_cctx** LZ4F_compressionContextPtr, unsigned version) { LZ4F_cctx_t* const cctxPtr = (LZ4F_cctx_t*)ALLOC_AND_ZERO(sizeof(LZ4F_cctx_t)); if (cctxPtr==NULL) return err0r(LZ4F_ERROR_allocation_failed); @@ -541,20 +541,18 @@ LZ4F_errorCode_t LZ4F_createCompressionContext(LZ4F_compressionContext_t* LZ4F_c cctxPtr->version = version; cctxPtr->cStage = 0; /* Next stage : init stream */ - *LZ4F_compressionContextPtr = (LZ4F_compressionContext_t)cctxPtr; + *LZ4F_compressionContextPtr = cctxPtr; return LZ4F_OK_NoError; } -LZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_compressionContext_t LZ4F_compressionContext) +LZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_cctx* cctxPtr) { - LZ4F_cctx_t* const cctxPtr = (LZ4F_cctx_t*)LZ4F_compressionContext; - if (cctxPtr != NULL) { /* support free on NULL */ - FREEMEM(cctxPtr->lz4CtxPtr); /* works because LZ4_streamHC_t and LZ4_stream_t are simple POD types */ + FREEMEM(cctxPtr->lz4CtxPtr); /* note: LZ4_streamHC_t and LZ4_stream_t are simple POD types */ FREEMEM(cctxPtr->tmpBuff); - FREEMEM(LZ4F_compressionContext); + FREEMEM(cctxPtr); } return LZ4F_OK_NoError; @@ -1291,8 +1289,10 @@ static void LZ4F_updateDict(LZ4F_dctx* dctx, const BYTE* dstPtr, size_t dstSize, const BYTE* dstBufferStart, unsigned withinTmp) { - if (dctx->dictSize==0) + if (dctx->dictSize==0) { + assert(dstPtr != NULL); dctx->dict = (const BYTE*)dstPtr; /* priority to dictionary continuity */ + } if (dctx->dict + dctx->dictSize == dstPtr) { /* dictionary continuity, directly within dstBuffer */ dctx->dictSize += dstSize; @@ -1382,13 +1382,14 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, const BYTE* const srcEnd = srcStart + *srcSizePtr; const BYTE* srcPtr = srcStart; BYTE* const dstStart = (BYTE*)dstBuffer; - BYTE* const dstEnd = dstStart + *dstSizePtr; + BYTE* const dstEnd = dstStart ? dstStart + *dstSizePtr : NULL; BYTE* dstPtr = dstStart; const BYTE* selectedIn = NULL; unsigned doAnotherStage = 1; size_t nextSrcSizeHint = 1; + if (dstBuffer == NULL) assert(*dstSizePtr == 0); MEM_INIT(&optionsNull, 0, sizeof(optionsNull)); if (decompressOptionsPtr==NULL) decompressOptionsPtr = &optionsNull; *srcSizePtr = 0; @@ -1590,7 +1591,7 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, selectedIn = srcPtr; srcPtr += dctx->tmpInTarget; - if (0) /* jump over next block */ + if (0) /* always jump over next block */ case dstage_storeCBlock: { size_t const wantedData = dctx->tmpInTarget - dctx->tmpInSize; size_t const inputLeft = (size_t)(srcEnd-srcPtr); diff --git a/lib/lz4hc.c b/lib/lz4hc.c index 6e4d732..28b91f7 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -270,7 +270,7 @@ LZ4HC_InsertAndGetWiderMatch ( DEBUGLOG(7, "First match at index %u / %u (lowestMatchIndex)", matchIndex, lowestMatchIndex); - while ((matchIndex>=lowestMatchIndex) && (nbAttempts)) { + while ((matchIndex>=lowestMatchIndex) && (nbAttempts>0)) { int matchLength=0; nbAttempts--; assert(matchIndex < ipIndex); @@ -410,7 +410,7 @@ LZ4HC_InsertAndGetWiderMatch ( } /* while ((matchIndex>=lowestMatchIndex) && (nbAttempts)) */ if ( dict == usingDictCtxHc - && nbAttempts + && nbAttempts > 0 && ipIndex - lowestMatchIndex < LZ4_DISTANCE_MAX) { size_t const dictEndOffset = (size_t)(dictCtx->end - dictCtx->base); U32 dictMatchIndex = dictCtx->hashTable[LZ4HC_hashPtr(ip)]; @@ -544,7 +544,7 @@ LZ4_FORCE_INLINE int LZ4HC_compress_hashChain ( char* const dest, int* srcSizePtr, int const maxOutputSize, - unsigned maxNbAttempts, + int maxNbAttempts, const limitedOutput_directive limit, const dictCtx_directive dict ) @@ -799,7 +799,7 @@ LZ4_FORCE_INLINE int LZ4HC_compress_generic_internal ( typedef enum { lz4hc, lz4opt } lz4hc_strat_e; typedef struct { lz4hc_strat_e strat; - U32 nbSearches; + int nbSearches; U32 targetLength; } cParams_t; static const cParams_t clTable[LZ4HC_CLEVEL_MAX+1] = { @@ -839,7 +839,7 @@ LZ4_FORCE_INLINE int LZ4HC_compress_generic_internal ( assert(cParam.strat == lz4opt); result = LZ4HC_compress_optimal(ctx, src, dst, srcSizePtr, dstCapacity, - (int)cParam.nbSearches, cParam.targetLength, limit, + cParam.nbSearches, cParam.targetLength, limit, cLevel == LZ4HC_CLEVEL_MAX, /* ultra mode */ dict, favor); } @@ -981,10 +981,10 @@ int LZ4_compress_HC_destSize(void* state, const char* source, char* dest, int* s /* allocation */ LZ4_streamHC_t* LZ4_createStreamHC(void) { - LZ4_streamHC_t* const LZ4_streamHCPtr = (LZ4_streamHC_t*)ALLOC(sizeof(LZ4_streamHC_t)); - if (LZ4_streamHCPtr==NULL) return NULL; - LZ4_initStreamHC(LZ4_streamHCPtr, sizeof(*LZ4_streamHCPtr)); /* full initialization, malloc'ed buffer can be full of garbage */ - return LZ4_streamHCPtr; + LZ4_streamHC_t* state = (LZ4_streamHC_t*)ALLOC(sizeof(LZ4_streamHC_t)); + if (state==NULL) return NULL; + state = LZ4_initStreamHC(state, sizeof(*state)); /* full initialization, malloc'ed buffer can be full of garbage */ + return state; } int LZ4_freeStreamHC (LZ4_streamHC_t* LZ4_streamHCPtr) @@ -1347,7 +1347,6 @@ static int LZ4HC_compress_optimal ( LZ4HC_CCtx_internal* ctx, if (sufficient_len >= LZ4_OPT_NUM) sufficient_len = LZ4_OPT_NUM-1; /* Main Loop */ - assert(ip - anchor < LZ4_MAX_INPUT_SIZE); while (ip <= mflimit) { int const llen = (int)(ip - anchor); int best_mlen, best_off; diff --git a/ossfuzz/compress_frame_fuzzer.c b/ossfuzz/compress_frame_fuzzer.c index bb14fc2..568ae14 100644 --- a/ossfuzz/compress_frame_fuzzer.c +++ b/ossfuzz/compress_frame_fuzzer.c @@ -28,8 +28,8 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) char* const dst = (char*)malloc(dstCapacity); char* const rt = (char*)malloc(size); - FUZZ_ASSERT(dst); - FUZZ_ASSERT(rt); + FUZZ_ASSERT(dst!=NULL); + FUZZ_ASSERT(rt!=NULL); /* If compression succeeds it must round trip correctly. */ size_t const dstSize = diff --git a/ossfuzz/fuzz_data_producer.c b/ossfuzz/fuzz_data_producer.c index cc06958..670fbf5 100644 --- a/ossfuzz/fuzz_data_producer.c +++ b/ossfuzz/fuzz_data_producer.c @@ -5,8 +5,8 @@ struct FUZZ_dataProducer_s{ size_t size; }; -FUZZ_dataProducer_t *FUZZ_dataProducer_create(const uint8_t *data, size_t size) { - FUZZ_dataProducer_t *producer = malloc(sizeof(FUZZ_dataProducer_t)); +FUZZ_dataProducer_t* FUZZ_dataProducer_create(const uint8_t* data, size_t size) { + FUZZ_dataProducer_t* const producer = malloc(sizeof(FUZZ_dataProducer_t)); FUZZ_ASSERT(producer != NULL); diff --git a/programs/lz4cli.c b/programs/lz4cli.c index 67dcaa1..d7d4f81 100644 --- a/programs/lz4cli.c +++ b/programs/lz4cli.c @@ -395,7 +395,7 @@ int main(int argc, const char** argv) if (!strcmp(argument, "--favor-decSpeed")) { LZ4IO_favorDecSpeed(prefs, 1); continue; } if (!strcmp(argument, "--verbose")) { displayLevel++; continue; } if (!strcmp(argument, "--quiet")) { if (displayLevel) displayLevel--; continue; } - if (!strcmp(argument, "--version")) { DISPLAYOUT(WELCOME_MESSAGE); return 0; } + if (!strcmp(argument, "--version")) { DISPLAYOUT(WELCOME_MESSAGE); goto _cleanup; } if (!strcmp(argument, "--help")) { usage_advanced(exeName); goto _cleanup; } if (!strcmp(argument, "--keep")) { LZ4IO_setRemoveSrcFile(prefs, 0); continue; } /* keep source file (default) */ if (!strcmp(argument, "--rm")) { LZ4IO_setRemoveSrcFile(prefs, 1); continue; } diff --git a/programs/lz4io.c b/programs/lz4io.c index 0f3507e..3ffc519 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -1291,7 +1291,7 @@ int LZ4IO_decompressMultipleFilenames(LZ4IO_prefs_t* const prefs, size_t const suffixSize = strlen(suffix); dRess_t ress = LZ4IO_createDResources(prefs); - if (outFileName==NULL) return ifntSize; /* not enough memory */ + if (outFileName==NULL) EXM_THROW(70, "Memory allocation error"); ress.dstFile = LZ4IO_openDstFile(prefs, stdoutmark); for (i=0; ifileSize = (finput == NULL) ? 0 : UTIL_getOpenFileSize(finput); + + if (finput == NULL) return LZ4IO_not_a_file; + cfinfo->fileSize = UTIL_getOpenFileSize(finput); while (!feof(finput)) { LZ4IO_frameInfo_t frameInfo = LZ4IO_INIT_FRAMEINFO; @@ -1560,8 +1562,7 @@ LZ4IO_getCompressedFileInfo(LZ4IO_cFileInfo_t* cfinfo, const char* input_filenam totalBlocksSize + 4, "-", "-"); result = LZ4IO_LZ4F_OK; - } - } + } } break; case LZ4IO_SKIPPABLE0: frameInfo.frameType = skippableFrame; @@ -1628,8 +1629,7 @@ int LZ4IO_displayCompressedFilesInfo(const char** inFileNames, size_t ifnIdx) assert(op_result == LZ4IO_format_not_known); DISPLAYLEVEL(1, "lz4: %s: File format not recognized \n", inFileNames[idx]); return 0; - } - } + } } DISPLAYLEVEL(3, "\n"); if (g_displayLevel < 3) { /* Display Summary */ @@ -1648,10 +1648,8 @@ int LZ4IO_displayCompressedFilesInfo(const char** inFileNames, size_t ifnIdx) DISPLAYOUT("%9s %s\n", "-", cfinfo.fileName); - } - } - } - } + } } } /* if (g_displayLevel < 3) */ + } /* for (; idx < ifnIdx; idx++) */ return result; } diff --git a/programs/lz4io.h b/programs/lz4io.h index b189e35..4763180 100644 --- a/programs/lz4io.h +++ b/programs/lz4io.h @@ -39,13 +39,13 @@ /* ************************************************** */ /* Special input/output values */ /* ************************************************** */ +#define stdinmark "stdin" +#define stdoutmark "stdout" #define NULL_OUTPUT "null" -static const char stdinmark[] = "stdin"; -static const char stdoutmark[] = "stdout"; #ifdef _WIN32 -static const char nulmark[] = "nul"; +#define nulmark "nul" #else -static const char nulmark[] = "/dev/null"; +#define nulmark "/dev/null" #endif /* ************************************************** */ diff --git a/tests/checkFrame.c b/tests/checkFrame.c index 139a599..f39d2ac 100644 --- a/tests/checkFrame.c +++ b/tests/checkFrame.c @@ -301,6 +301,7 @@ int main(int argc, const char** argv) freeCResources(ress); EXM_THROW(1, "%s: %s \n", argument, strerror(errno)); } + assert (srcFile != NULL); err = frameCheck(ress, srcFile, bsid, blockSize); freeCResources(ress); fclose(srcFile); diff --git a/tests/fullbench.c b/tests/fullbench.c index 7d74d3f..77f475b 100644 --- a/tests/fullbench.c +++ b/tests/fullbench.c @@ -399,25 +399,25 @@ int fullSpeedBench(const char** fileNamesTable, int nbFiles) char* compressed_buff=NULL; const char* const inFileName = fileNamesTable[fileIdx++]; FILE* const inFile = fopen( inFileName, "rb" ); - U64 inFileSize; - size_t benchedSize; + U64 const inFileSize = UTIL_getFileSize(inFileName); + size_t benchedSize = BMK_findMaxMem(inFileSize*2) / 2; /* because 2 buffers */ int nbChunks; int maxCompressedChunkSize; size_t readSize; int compressedBuffSize; U32 crcOriginal; - /* Check file existence */ - if (inFile==NULL) { DISPLAY( "Pb opening %s\n", inFileName); return 11; } + /* Check infile pre-requisites */ + if (inFile==NULL) { DISPLAY("Pb opening %s \n", inFileName); return 11; } + if (inFileSize==0) { DISPLAY("file is empty \n"); fclose(inFile); return 11; } + if (benchedSize==0) { DISPLAY("not enough memory \n"); fclose(inFile); return 11; } /* Memory size adjustments */ - inFileSize = UTIL_getFileSize(inFileName); - if (inFileSize==0) { DISPLAY( "file is empty\n"); fclose(inFile); return 11; } - benchedSize = BMK_findMaxMem(inFileSize*2) / 2; /* because 2 buffers */ - if (benchedSize==0) { DISPLAY( "not enough memory\n"); fclose(inFile); return 11; } if ((U64)benchedSize > inFileSize) benchedSize = (size_t)inFileSize; - if (benchedSize < inFileSize) - DISPLAY("Not enough memory for '%s' full size; testing %i MB only...\n", inFileName, (int)(benchedSize>>20)); + if (benchedSize < inFileSize) { + DISPLAY("Not enough memory for '%s' full size; testing %i MB only... \n", + inFileName, (int)(benchedSize>>20)); + } /* Allocation */ chunkP = (struct chunkParameters*) malloc(((benchedSize / (size_t)g_chunkSize)+1) * sizeof(struct chunkParameters)); @@ -427,7 +427,7 @@ int fullSpeedBench(const char** fileNamesTable, int nbFiles) compressedBuffSize = nbChunks * maxCompressedChunkSize; compressed_buff = (char*)malloc((size_t)compressedBuffSize); if(!chunkP || !orig_buff || !compressed_buff) { - DISPLAY("\nError: not enough memory!\n"); + DISPLAY("\nError: not enough memory! \n"); fclose(inFile); free(orig_buff); free(compressed_buff); @@ -475,7 +475,7 @@ int fullSpeedBench(const char** fileNamesTable, int nbFiles) size_t remaining = benchedSize; char* in = orig_buff; char* out = compressed_buff; - nbChunks = (int) (((int)benchedSize + (g_chunkSize-1))/ g_chunkSize); + assert(nbChunks >= 1); for (i=0; i Date: Tue, 29 Sep 2020 21:53:42 -0700 Subject: fix conversion warning --- lib/lz4.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index db6150a..0290ea2 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1684,7 +1684,7 @@ typedef enum { decode_full_block = 0, partial_decode = 1 } earlyEnd_directive; typedef enum { loop_error = -2, initial_error = -1, ok = 0 } variable_length_error; LZ4_FORCE_INLINE unsigned read_variable_length(const BYTE**ip, const BYTE* lencheck, - endCondition_directive loop_check, endCondition_directive initial_check, + int loop_check, int initial_check, variable_length_error* error) { U32 length = 0; @@ -1784,7 +1784,7 @@ LZ4_decompress_generic( /* decode literal length */ if (length == RUN_MASK) { variable_length_error error = ok; - length += read_variable_length(&ip, iend-RUN_MASK, endOnInput, endOnInput, &error); + length += read_variable_length(&ip, iend-RUN_MASK, (int)endOnInput, (int)endOnInput, &error); if (error == initial_error) { goto _output_error; } if ((safeDecode) && unlikely((uptrval)(op)+length<(uptrval)(op))) { goto _output_error; } /* overflow detection */ if ((safeDecode) && unlikely((uptrval)(ip)+length<(uptrval)(ip))) { goto _output_error; } /* overflow detection */ @@ -1829,7 +1829,7 @@ LZ4_decompress_generic( if (length == ML_MASK) { variable_length_error error = ok; if ((checkOffset) && (unlikely(match + dictSize < lowPrefix))) { goto _output_error; } /* Error : offset outside buffers */ - length += read_variable_length(&ip, iend - LASTLITERALS + 1, endOnInput, 0, &error); + length += read_variable_length(&ip, iend - LASTLITERALS + 1, (int)endOnInput, 0, &error); if (error != ok) { goto _output_error; } if ((safeDecode) && unlikely((uptrval)(op)+length<(uptrval)op)) { goto _output_error; } /* overflow detection */ length += MINMATCH; @@ -1954,7 +1954,7 @@ LZ4_decompress_generic( /* decode literal length */ if (length == RUN_MASK) { variable_length_error error = ok; - length += read_variable_length(&ip, iend-RUN_MASK, endOnInput, endOnInput, &error); + length += read_variable_length(&ip, iend-RUN_MASK, (int)endOnInput, (int)endOnInput, &error); if (error == initial_error) { goto _output_error; } if ((safeDecode) && unlikely((uptrval)(op)+length<(uptrval)(op))) { goto _output_error; } /* overflow detection */ if ((safeDecode) && unlikely((uptrval)(ip)+length<(uptrval)(ip))) { goto _output_error; } /* overflow detection */ @@ -2039,7 +2039,7 @@ LZ4_decompress_generic( _copy_match: if (length == ML_MASK) { variable_length_error error = ok; - length += read_variable_length(&ip, iend - LASTLITERALS + 1, endOnInput, 0, &error); + length += read_variable_length(&ip, iend - LASTLITERALS + 1, (int)endOnInput, 0, &error); if (error != ok) goto _output_error; if ((safeDecode) && unlikely((uptrval)(op)+length<(uptrval)op)) goto _output_error; /* overflow detection */ } -- cgit v1.2.3 From fe74eb2031b9463e14ef8d0f17d4219bdbfe2b76 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 30 Sep 2020 17:39:42 -0700 Subject: added memcpy() related SA warning fixes memcpy() on NULL is UB, even if length is 0. --- lib/lz4frame.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/lz4frame.c b/lib/lz4frame.c index 03facb0..2c62d78 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -1289,8 +1289,9 @@ static void LZ4F_updateDict(LZ4F_dctx* dctx, const BYTE* dstPtr, size_t dstSize, const BYTE* dstBufferStart, unsigned withinTmp) { + /* hint to static analyzer : dstPtr can't be NULL at this stage */ + assert(dstPtr != NULL); if (dstPtr==NULL) return; if (dctx->dictSize==0) { - assert(dstPtr != NULL); dctx->dict = (const BYTE*)dstPtr; /* priority to dictionary continuity */ } @@ -1310,7 +1311,7 @@ static void LZ4F_updateDict(LZ4F_dctx* dctx, /* dstBuffer does not contain whole useful history (64 KB), so it must be saved within tmpOut */ - if ((withinTmp) && (dctx->dict == dctx->tmpOutBuffer)) { /* continue history within tmpOutBuffer */ + if (withinTmp && (dctx->dict == dctx->tmpOutBuffer)) { /* continue history within tmpOutBuffer */ /* withinTmp expectation : content of [dstPtr,dstSize] is same as [dict+dictSize,dstSize], so we just extend it */ assert(dctx->dict + dctx->dictSize == dctx->tmpOut + dctx->tmpOutStart); dctx->dictSize += dstSize; @@ -1517,6 +1518,7 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, } case dstage_copyDirect: /* uncompressed block */ + if (dstPtr==NULL) { doAnotherStage = 0; break; } { size_t const minBuffSize = MIN((size_t)(srcEnd-srcPtr), (size_t)(dstEnd-dstPtr)); size_t const sizeToCopy = MIN(dctx->tmpInTarget, minBuffSize); memcpy(dstPtr, srcPtr, sizeToCopy); @@ -1529,8 +1531,9 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, dctx->frameRemainingSize -= sizeToCopy; /* history management (linked blocks only)*/ - if (dctx->frameInfo.blockMode == LZ4F_blockLinked) + if (dctx->frameInfo.blockMode == LZ4F_blockLinked) { LZ4F_updateDict(dctx, dstPtr, sizeToCopy, dstStart, 0); + } srcPtr += sizeToCopy; dstPtr += sizeToCopy; @@ -1628,6 +1631,7 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, const char* dict = (const char*)dctx->dict; size_t dictSize = dctx->dictSize; int decodedSize; + assert(dstPtr != NULL); if (dict && dictSize > 1 GB) { /* the dictSize param is an int, avoid truncation / sign issues */ dict += dictSize - 64 KB; @@ -1645,8 +1649,9 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, dctx->frameRemainingSize -= (size_t)decodedSize; /* dictionary management */ - if (dctx->frameInfo.blockMode==LZ4F_blockLinked) + if (dctx->frameInfo.blockMode==LZ4F_blockLinked) { LZ4F_updateDict(dctx, dstPtr, (size_t)decodedSize, dstStart, 0); + } dstPtr += decodedSize; dctx->dStage = dstage_getBlockHeader; @@ -1693,6 +1698,7 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, /* fall-through */ case dstage_flushOut: /* flush decoded data from tmpOut to dstBuffer */ + if (dstPtr == NULL) { doAnotherStage = 0; nextSrcSizeHint = BHSize; break; } { size_t const sizeToCopy = MIN(dctx->tmpOutSize - dctx->tmpOutStart, (size_t)(dstEnd-dstPtr)); memcpy(dstPtr, dctx->tmpOut + dctx->tmpOutStart, sizeToCopy); @@ -1815,6 +1821,7 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, LZ4F_STATIC_ASSERT((unsigned)dstage_init == 2); if ( (dctx->frameInfo.blockMode==LZ4F_blockLinked) /* next block will use up to 64KB from previous ones */ && (dctx->dict != dctx->tmpOutBuffer) /* dictionary is not already within tmp */ + && (dctx->dict != NULL) /* dictionary exists */ && (!decompressOptionsPtr->stableDst) /* cannot rely on dst data to remain there for next call */ && ((unsigned)(dctx->dStage)-2 < (unsigned)(dstage_getSuffix)-2) ) /* valid stages : [init ... getSuffix[ */ { @@ -1824,9 +1831,9 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, const BYTE* oldDictEnd = dctx->dict + dctx->dictSize - dctx->tmpOutStart; if (dctx->tmpOutSize > 64 KB) copySize = 0; if (copySize > preserveSize) copySize = preserveSize; + assert(dctx->tmpOutBuffer != NULL); - if (copySize > 0) - memcpy(dctx->tmpOutBuffer + preserveSize - copySize, oldDictEnd - copySize, copySize); + memcpy(dctx->tmpOutBuffer + preserveSize - copySize, oldDictEnd - copySize, copySize); dctx->dict = dctx->tmpOutBuffer; dctx->dictSize = preserveSize + dctx->tmpOutStart; @@ -1834,8 +1841,7 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, const BYTE* const oldDictEnd = dctx->dict + dctx->dictSize; size_t const newDictSize = MIN(dctx->dictSize, 64 KB); - if (newDictSize > 0) - memcpy(dctx->tmpOutBuffer, oldDictEnd - newDictSize, newDictSize); + memcpy(dctx->tmpOutBuffer, oldDictEnd - newDictSize, newDictSize); dctx->dict = dctx->tmpOutBuffer; dctx->dictSize = newDictSize; -- cgit v1.2.3 From 1e31f1d25cda257b11d62e8700ed650dca1bbe54 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 30 Sep 2020 22:51:17 -0700 Subject: fix bad init scenario --- lib/lz4hc.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/lz4hc.c b/lib/lz4hc.c index 28b91f7..cd802d8 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -981,9 +981,11 @@ int LZ4_compress_HC_destSize(void* state, const char* source, char* dest, int* s /* allocation */ LZ4_streamHC_t* LZ4_createStreamHC(void) { - LZ4_streamHC_t* state = (LZ4_streamHC_t*)ALLOC(sizeof(LZ4_streamHC_t)); - if (state==NULL) return NULL; - state = LZ4_initStreamHC(state, sizeof(*state)); /* full initialization, malloc'ed buffer can be full of garbage */ + LZ4_streamHC_t* const state = (LZ4_streamHC_t*)ALLOC(sizeof(LZ4_streamHC_t)); + if (LZ4_initStreamHC(state, sizeof(*state)) == NULL) { + free(state); + return NULL; + } return state; } -- cgit v1.2.3 From 03f006a7eabf476a6424821753337fefc6984dae Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 1 Oct 2020 10:48:22 -0700 Subject: make scan-build accept assert() --- Makefile | 2 +- lib/lz4frame.c | 15 +++++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index c1869f6..a45611c 100644 --- a/Makefile +++ b/Makefile @@ -153,7 +153,7 @@ usan32: clean .PHONY: staticAnalyze staticAnalyze: clean - CFLAGS=-g scan-build --status-bugs -v $(MAKE) all + CPPFLAGS=-DLZ4_DEBUG=1 CFLAGS=-g scan-build --status-bugs -v --force-analyze-debug-code $(MAKE) all V=1 DEBUGLEVEL=1 .PHONY: cppcheck cppcheck: diff --git a/lib/lz4frame.c b/lib/lz4frame.c index 2c62d78..bfdef5d 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -1284,18 +1284,20 @@ LZ4F_errorCode_t LZ4F_getFrameInfo(LZ4F_dctx* dctx, /* LZ4F_updateDict() : - * only used for LZ4F_blockLinked mode */ + * only used for LZ4F_blockLinked mode + * Condition : dstPtr != NULL + */ static void LZ4F_updateDict(LZ4F_dctx* dctx, const BYTE* dstPtr, size_t dstSize, const BYTE* dstBufferStart, unsigned withinTmp) { - /* hint to static analyzer : dstPtr can't be NULL at this stage */ - assert(dstPtr != NULL); if (dstPtr==NULL) return; + assert(dstPtr != NULL); if (dctx->dictSize==0) { - dctx->dict = (const BYTE*)dstPtr; /* priority to dictionary continuity */ + dctx->dict = (const BYTE*)dstPtr; /* priority to prefix mode */ } + assert(dctx->dict != NULL); - if (dctx->dict + dctx->dictSize == dstPtr) { /* dictionary continuity, directly within dstBuffer */ + if (dctx->dict + dctx->dictSize == dstPtr) { /* prefix mode, everything within dstBuffer */ dctx->dictSize += dstSize; return; } @@ -1309,7 +1311,8 @@ static void LZ4F_updateDict(LZ4F_dctx* dctx, assert(dstSize < 64 KB); /* if dstSize >= 64 KB, dictionary would be set into dstBuffer directly */ - /* dstBuffer does not contain whole useful history (64 KB), so it must be saved within tmpOut */ + /* dstBuffer does not contain whole useful history (64 KB), so it must be saved within tmpOutBuffer */ + assert(dctx->tmpOutBuffer != NULL); if (withinTmp && (dctx->dict == dctx->tmpOutBuffer)) { /* continue history within tmpOutBuffer */ /* withinTmp expectation : content of [dstPtr,dstSize] is same as [dict+dictSize,dstSize], so we just extend it */ -- cgit v1.2.3 From 1e5ff76b36c5eaa35aea78f690be045d496a0f3a Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 1 Oct 2020 19:07:56 -0700 Subject: add LZ4F_decompress() tests with (NULL,0) input and output fix one (rare & complex) issue discovered by this test --- lib/lz4frame.c | 84 ++++++++++++++++++++++++++++++++++--------------------- tests/frametest.c | 4 ++- 2 files changed, 55 insertions(+), 33 deletions(-) diff --git a/lib/lz4frame.c b/lib/lz4frame.c index bfdef5d..2976bb1 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -991,6 +991,7 @@ size_t LZ4F_compressEnd(LZ4F_cctx* cctxPtr, BYTE* dstPtr = dstStart; size_t const flushSize = LZ4F_flush(cctxPtr, dstBuffer, dstCapacity, compressOptionsPtr); + DEBUGLOG(5,"LZ4F_compressEnd: dstCapacity=%u", (unsigned)dstCapacity); if (LZ4F_isError(flushSize)) return flushSize; dstPtr += flushSize; @@ -1004,6 +1005,7 @@ size_t LZ4F_compressEnd(LZ4F_cctx* cctxPtr, if (cctxPtr->prefs.frameInfo.contentChecksumFlag == LZ4F_contentChecksumEnabled) { U32 const xxh = XXH32_digest(&(cctxPtr->xxh)); if (dstCapacity < 8) return err0r(LZ4F_ERROR_dstMaxSize_tooSmall); + DEBUGLOG(5,"Writing 32-bit content checksum"); LZ4F_writeLE32(dstPtr, xxh); dstPtr+=4; /* content Checksum */ } @@ -1114,6 +1116,7 @@ static size_t LZ4F_decodeHeader(LZ4F_dctx* dctx, const void* src, size_t srcSize size_t frameHeaderSize; const BYTE* srcPtr = (const BYTE*)src; + DEBUGLOG(5, "LZ4F_decodeHeader"); /* need to decode header to get frameInfo */ if (srcSize < minFHSize) return err0r(LZ4F_ERROR_frameHeader_incomplete); /* minimal frame header size */ MEM_INIT(&(dctx->frameInfo), 0, sizeof(dctx->frameInfo)); @@ -1134,8 +1137,10 @@ static size_t LZ4F_decodeHeader(LZ4F_dctx* dctx, const void* src, size_t srcSize /* control magic number */ #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION - if (LZ4F_readLE32(srcPtr) != LZ4F_MAGICNUMBER) + if (LZ4F_readLE32(srcPtr) != LZ4F_MAGICNUMBER) { + DEBUGLOG(4, "frame header error : unknown magic number"); return err0r(LZ4F_ERROR_frameType_unknown); + } #endif dctx->frameInfo.frameType = LZ4F_frame; @@ -1393,6 +1398,8 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, size_t nextSrcSizeHint = 1; + DEBUGLOG(5, "LZ4F_decompress : %p,%u => %p,%u", + srcBuffer, (unsigned)*srcSizePtr, dstBuffer, (unsigned)*dstSizePtr); if (dstBuffer == NULL) assert(*dstSizePtr == 0); MEM_INIT(&optionsNull, 0, sizeof(optionsNull)); if (decompressOptionsPtr==NULL) decompressOptionsPtr = &optionsNull; @@ -1407,6 +1414,7 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, { case dstage_getFrameHeader: + DEBUGLOG(6, "dstage_getFrameHeader"); if ((size_t)(srcEnd-srcPtr) >= maxFHSize) { /* enough to decode - shortcut */ size_t const hSize = LZ4F_decodeHeader(dctx, srcPtr, (size_t)(srcEnd-srcPtr)); /* will update dStage appropriately */ if (LZ4F_isError(hSize)) return hSize; @@ -1420,6 +1428,7 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, /* fall-through */ case dstage_storeFrameHeader: + DEBUGLOG(6, "dstage_storeFrameHeader"); { size_t const sizeToCopy = MIN(dctx->tmpInTarget - dctx->tmpInSize, (size_t)(srcEnd - srcPtr)); memcpy(dctx->header + dctx->tmpInSize, srcPtr, sizeToCopy); dctx->tmpInSize += sizeToCopy; @@ -1436,6 +1445,7 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, break; case dstage_init: + DEBUGLOG(6, "dstage_init"); if (dctx->frameInfo.contentChecksumFlag) (void)XXH32_reset(&(dctx->xxh), 0); /* internal buffers allocation */ { size_t const bufferNeeded = dctx->maxBlockSize @@ -1521,22 +1531,26 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, } case dstage_copyDirect: /* uncompressed block */ - if (dstPtr==NULL) { doAnotherStage = 0; break; } - { size_t const minBuffSize = MIN((size_t)(srcEnd-srcPtr), (size_t)(dstEnd-dstPtr)); - size_t const sizeToCopy = MIN(dctx->tmpInTarget, minBuffSize); - memcpy(dstPtr, srcPtr, sizeToCopy); - if (dctx->frameInfo.blockChecksumFlag) { - (void)XXH32_update(&dctx->blockChecksum, srcPtr, sizeToCopy); - } - if (dctx->frameInfo.contentChecksumFlag) - (void)XXH32_update(&dctx->xxh, srcPtr, sizeToCopy); - if (dctx->frameInfo.contentSize) - dctx->frameRemainingSize -= sizeToCopy; + DEBUGLOG(6, "dstage_copyDirect"); + { size_t sizeToCopy; + if (dstPtr == NULL) { + sizeToCopy = 0; + } else { + size_t const minBuffSize = MIN((size_t)(srcEnd-srcPtr), (size_t)(dstEnd-dstPtr)); + sizeToCopy = MIN(dctx->tmpInTarget, minBuffSize); + memcpy(dstPtr, srcPtr, sizeToCopy); + if (dctx->frameInfo.blockChecksumFlag) { + (void)XXH32_update(&dctx->blockChecksum, srcPtr, sizeToCopy); + } + if (dctx->frameInfo.contentChecksumFlag) + (void)XXH32_update(&dctx->xxh, srcPtr, sizeToCopy); + if (dctx->frameInfo.contentSize) + dctx->frameRemainingSize -= sizeToCopy; - /* history management (linked blocks only)*/ - if (dctx->frameInfo.blockMode == LZ4F_blockLinked) { - LZ4F_updateDict(dctx, dstPtr, sizeToCopy, dstStart, 0); - } + /* history management (linked blocks only)*/ + if (dctx->frameInfo.blockMode == LZ4F_blockLinked) { + LZ4F_updateDict(dctx, dstPtr, sizeToCopy, dstStart, 0); + } } srcPtr += sizeToCopy; dstPtr += sizeToCopy; @@ -1549,15 +1563,16 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, break; } dctx->tmpInTarget -= sizeToCopy; /* need to copy more */ - nextSrcSizeHint = dctx->tmpInTarget + - +(dctx->frameInfo.blockChecksumFlag ? BFSize : 0) - + BHSize /* next header size */; - doAnotherStage = 0; - break; } + nextSrcSizeHint = dctx->tmpInTarget + + +(dctx->frameInfo.blockChecksumFlag ? BFSize : 0) + + BHSize /* next header size */; + doAnotherStage = 0; + break; /* check block checksum for recently transferred uncompressed block */ case dstage_getBlockChecksum: + DEBUGLOG(6, "dstage_getBlockChecksum"); { const void* crcSrc; if ((srcEnd-srcPtr >= 4) && (dctx->tmpInSize==0)) { crcSrc = srcPtr; @@ -1577,8 +1592,12 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, { U32 const readCRC = LZ4F_readLE32(crcSrc); U32 const calcCRC = XXH32_digest(&dctx->blockChecksum); #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION - if (readCRC != calcCRC) + DEBUGLOG(6, "compare block checksum"); + if (readCRC != calcCRC) { + DEBUGLOG(4, "incorrect block checksum: %08X != %08X", + readCRC, calcCRC); return err0r(LZ4F_ERROR_blockChecksum_invalid); + } #else (void)readCRC; (void)calcCRC; @@ -1588,6 +1607,7 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, break; case dstage_getCBlock: + DEBUGLOG(6, "dstage_getCBlock"); if ((size_t)(srcEnd-srcPtr) < dctx->tmpInTarget) { dctx->tmpInSize = 0; dctx->dStage = dstage_storeCBlock; @@ -1701,8 +1721,9 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, /* fall-through */ case dstage_flushOut: /* flush decoded data from tmpOut to dstBuffer */ - if (dstPtr == NULL) { doAnotherStage = 0; nextSrcSizeHint = BHSize; break; } - { size_t const sizeToCopy = MIN(dctx->tmpOutSize - dctx->tmpOutStart, (size_t)(dstEnd-dstPtr)); + DEBUGLOG(6, "dstage_flushOut"); + if (dstPtr != NULL) { + size_t const sizeToCopy = MIN(dctx->tmpOutSize - dctx->tmpOutStart, (size_t)(dstEnd-dstPtr)); memcpy(dstPtr, dctx->tmpOut + dctx->tmpOutStart, sizeToCopy); /* dictionary management */ @@ -1711,16 +1732,15 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, dctx->tmpOutStart += sizeToCopy; dstPtr += sizeToCopy; - - if (dctx->tmpOutStart == dctx->tmpOutSize) { /* all flushed */ - dctx->dStage = dstage_getBlockHeader; /* get next block */ - break; - } - /* could not flush everything : stop there, just request a block header */ - doAnotherStage = 0; - nextSrcSizeHint = BHSize; + } + if (dctx->tmpOutStart == dctx->tmpOutSize) { /* all flushed */ + dctx->dStage = dstage_getBlockHeader; /* get next block */ break; } + /* could not flush everything : stop there, just request a block header */ + doAnotherStage = 0; + nextSrcSizeHint = BHSize; + break; case dstage_getSuffix: if (dctx->frameRemainingSize) diff --git a/tests/frametest.c b/tests/frametest.c index 3a0f64f..4d91bdf 100644 --- a/tests/frametest.c +++ b/tests/frametest.c @@ -851,9 +851,11 @@ size_t test_lz4f_decompression_wBuffers( /* read data from byte-exact buffer to catch out-of-bound reads */ { void* const iBuffer = malloc(iSizeMax); + void* const tmpop = (FUZ_rand(randState) & (oSize == 0)) ? NULL : op; + const void* const tmpip = (FUZ_rand(randState) & (iSize == 0)) ? NULL : iBuffer; assert(iBuffer != NULL); memcpy(iBuffer, ip, iSizeMax); - moreToFlush = LZ4F_decompress(dCtx, op, &oSize, iBuffer, &iSize, &dOptions); + moreToFlush = LZ4F_decompress(dCtx, tmpop, &oSize, tmpip, &iSize, &dOptions); free(iBuffer); } DISPLAYLEVEL(7, "oSize=%u, readSize=%u \n", (unsigned)oSize, (unsigned)iSize); -- cgit v1.2.3 From a88c8fb43875361e2dd6a0c9f84099c24dc89671 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 1 Oct 2020 23:27:02 -0700 Subject: make it possible to select SCANBUILD binary on command line --- Makefile | 4 +++- tests/frametest.c | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index a45611c..744005f 100644 --- a/Makefile +++ b/Makefile @@ -151,9 +151,11 @@ usan: clean usan32: clean CFLAGS="-m32 -O3 -g -fsanitize=undefined" $(MAKE) test FUZZER_TIME="-T30s" NB_LOOPS=-i1 +SCANBUILD ?= scan-build +SCANBUILD_FLAGS += --status-bugs -v --force-analyze-debug-code .PHONY: staticAnalyze staticAnalyze: clean - CPPFLAGS=-DLZ4_DEBUG=1 CFLAGS=-g scan-build --status-bugs -v --force-analyze-debug-code $(MAKE) all V=1 DEBUGLEVEL=1 + CPPFLAGS=-DLZ4_DEBUG=1 CFLAGS=-g $(SCANBUILD) $(SCANBUILD_FLAGS) $(MAKE) all V=1 DEBUGLEVEL=1 .PHONY: cppcheck cppcheck: diff --git a/tests/frametest.c b/tests/frametest.c index 4d91bdf..70f949d 100644 --- a/tests/frametest.c +++ b/tests/frametest.c @@ -1037,7 +1037,7 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi op += 4; if ((prefsPtr!= NULL) && prefsPtr->frameInfo.blockChecksumFlag) { U32 const bc32 = XXH32(op, 0, 0); - op[0] = (BYTE)bc32; /* little endian format */ + op[0] = (BYTE)bc32; /* little endian format */ op[1] = (BYTE)(bc32>>8); op[2] = (BYTE)(bc32>>16); op[3] = (BYTE)(bc32>>24); -- cgit v1.2.3 From 056cdd6cc67fbce0f83c59e30dd6a1196d59727f Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 1 Oct 2020 23:42:42 -0700 Subject: define sentinelTest to help scan-build detect the condition --- tests/frametest.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/frametest.c b/tests/frametest.c index 70f949d..2633c90 100644 --- a/tests/frametest.c +++ b/tests/frametest.c @@ -839,13 +839,14 @@ size_t test_lz4f_decompression_wBuffers( size_t iSize = iSizeMax; size_t const oSizeCand = (FUZ_rand(randState) & ((1< Date: Fri, 2 Oct 2020 13:06:51 -0700 Subject: added LZ4_streamHC_t init test which includes an alignment test --- tests/fuzzer.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/tests/fuzzer.c b/tests/fuzzer.c index 6c2ccb7..820ac8e 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -1200,6 +1200,25 @@ static void FUZ_unitTests(int compressionLevel) } } + DISPLAYLEVEL(3, "LZ4_initStreamHC with multiple valid alignments : "); + { struct { + LZ4_streamHC_t hc1; + char c1; + LZ4_streamHC_t hc2; + char c2; + LZ4_streamHC_t hc3; + } shc; + DISPLAYLEVEL(3, "hc1(%p) hc2(%p) hc3(%p) size(0x%x): ", + &(shc.hc1), &(shc.hc2), &(shc.hc3), (unsigned)sizeof(LZ4_streamHC_t)); + FUZ_CHECKTEST( LZ4_initStreamHC(&(shc.hc1), sizeof(shc.hc1)) == NULL, "hc1 (%p) failed init", &(shc.hc1) ); + FUZ_CHECKTEST( LZ4_initStreamHC(&(shc.hc2), sizeof(shc.hc2)) == NULL, "hc2 (%p) failed init", &(shc.hc2) ); + FUZ_CHECKTEST( LZ4_initStreamHC(&(shc.hc3), sizeof(shc.hc3)) == NULL, "hc3 (%p) failed init", &(shc.hc3) ); + FUZ_CHECKTEST( LZ4_initStreamHC((char*)&(shc.hc1) + 1, sizeof(shc.hc1)) != NULL, + "hc1+1 (%p) init must fail, due to bad alignment", (char*)&(shc.hc1) + 1 ); + /* no need to release anything : LZ4_streamHC_t is a simple POD type */ + } + DISPLAYLEVEL(3, "OK \n"); + /* LZ4 HC streaming tests */ { LZ4_streamHC_t sHC; /* statically allocated */ int result; @@ -1211,7 +1230,7 @@ static void FUZ_unitTests(int compressionLevel) FUZ_CHECKTEST(sp==NULL, "LZ4_createStreamHC() allocation failed"); LZ4_freeStreamHC(sp); } - DISPLAYLEVEL(3, " OK \n"); + DISPLAYLEVEL(3, "OK \n"); /* simple HC compression test */ DISPLAYLEVEL(3, "Simple HC round-trip : "); @@ -1226,7 +1245,7 @@ static void FUZ_unitTests(int compressionLevel) { U64 const crcNew = XXH64(testVerify, testCompressedSize, 0); FUZ_CHECKTEST(crc64!=crcNew, "LZ4_decompress_safe() decompression corruption"); } } - DISPLAYLEVEL(3, " OK \n"); + DISPLAYLEVEL(3, "OK \n"); /* long sequence test */ DISPLAYLEVEL(3, "Long sequence HC_destSize test : "); -- cgit v1.2.3 From 58c0a549adef617aa92124353d8a5a76094df17c Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 2 Oct 2020 14:10:00 -0700 Subject: fix cppcheck unused variable warning --- tests/fuzzer.c | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/fuzzer.c b/tests/fuzzer.c index 820ac8e..4fef4af 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -1208,6 +1208,7 @@ static void FUZ_unitTests(int compressionLevel) char c2; LZ4_streamHC_t hc3; } shc; + (void)shc.c1; (void)shc.c2; /* tell cppcheck these variables are unused */ DISPLAYLEVEL(3, "hc1(%p) hc2(%p) hc3(%p) size(0x%x): ", &(shc.hc1), &(shc.hc2), &(shc.hc3), (unsigned)sizeof(LZ4_streamHC_t)); FUZ_CHECKTEST( LZ4_initStreamHC(&(shc.hc1), sizeof(shc.hc1)) == NULL, "hc1 (%p) failed init", &(shc.hc1) ); -- cgit v1.2.3 From c661adda10529f9b4d4fa14ea7e5c68ec66ccaa4 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 2 Oct 2020 15:43:46 -0700 Subject: fixed x32 test on Travis --- .travis.yml | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6074f08..2281394 100644 --- a/.travis.yml +++ b/.travis.yml @@ -47,18 +47,15 @@ matrix: - make clean travis-install - make clean clangtest - - # 14.04 LTS Server Edition 64 bit - - name: (Trusty) i386 gcc test - dist: trusty + - name: x32 compatibility test addons: apt: packages: - - libc6-dev-i386 - gcc-multilib script: - make -C tests test MOREFLAGS=-mx32 + # 14.04 LTS Server Edition 64 bit # presume clang >= v3.9.0 - name: (Trusty) USan test dist: trusty -- cgit v1.2.3 From 2631002f74631145ee7385932035487fb08227f3 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 2 Oct 2020 15:51:14 -0700 Subject: preserver alignment test on Visual Studio x64 this it works fine in this environment (only x86 is suspicious) --- lib/lz4hc.c | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/lib/lz4hc.c b/lib/lz4hc.c index cd802d8..cf03eba 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -53,7 +53,7 @@ #include "lz4hc.h" -/*=== Common LZ4 definitions ===*/ +/*=== Common definitions ===*/ #if defined(__GNUC__) # pragma GCC diagnostic ignored "-Wunused-function" #endif @@ -61,19 +61,29 @@ # pragma clang diagnostic ignored "-Wunused-function" #endif -/*=== Enums ===*/ -typedef enum { noDictCtx, usingDictCtxHc } dictCtx_directive; - - #define LZ4_COMMONDEFS_ONLY #ifndef LZ4_SRC_INCLUDED #include "lz4.c" /* LZ4_count, constants, mem */ #endif + +/*=== Enums ===*/ +typedef enum { noDictCtx, usingDictCtxHc } dictCtx_directive; + + /*=== Constants ===*/ #define OPTIMAL_ML (int)((ML_MASK-1)+MINMATCH) #define LZ4_OPT_NUM (1<<12) +/* for some reason, Visual Studio fails the aligment test on 32-bit x86 : + * it reports an aligment of 8-bytes, + * while LZ4_streamHC_t only requires alignment of 4-bytes + * resulting in initialization error when allocating state with malloc() */ +#if (defined(_MSC_VER) && !defined(_M_X64)) +# define LZ4_ALIGN_TEST 0 +#else +# define LZ4_ALIGN_TEST 1 +#endif /*=== Macros ===*/ #define MIN(a,b) ( (a) < (b) ? (a) : (b) ) @@ -161,8 +171,7 @@ int LZ4HC_countBack(const BYTE* const ip, const BYTE* const match, static U32 LZ4HC_rotatePattern(size_t const rotate, U32 const pattern) { size_t const bitsToRotate = (rotate & (sizeof(pattern) - 1)) << 3; - if (bitsToRotate == 0) - return pattern; + if (bitsToRotate == 0) return pattern; return LZ4HC_rotl32(pattern, (int)bitsToRotate); } @@ -912,9 +921,7 @@ LZ4HC_compress_generic ( int LZ4_sizeofStateHC(void) { return (int)sizeof(LZ4_streamHC_t); } -#ifndef _MSC_VER /* for some reason, Visual fails the aligment test on 32-bit x86 : - * it reports an aligment of 8-bytes, - * while actually aligning LZ4_streamHC_t on 4 bytes. */ +#if LZ4_ALIGN_TEST static size_t LZ4_streamHC_t_alignment(void) { typedef struct { char c; LZ4_streamHC_t t; } t_a; @@ -927,9 +934,7 @@ static size_t LZ4_streamHC_t_alignment(void) int LZ4_compress_HC_extStateHC_fastReset (void* state, const char* src, char* dst, int srcSize, int dstCapacity, int compressionLevel) { LZ4HC_CCtx_internal* const ctx = &((LZ4_streamHC_t*)state)->internal_donotuse; -#ifndef _MSC_VER /* for some reason, Visual fails the aligment test on 32-bit x86 : - * it reports an aligment of 8-bytes, - * while actually aligning LZ4_streamHC_t on 4 bytes. */ +#if LZ4_ALIGN_TEST assert(((size_t)state & (LZ4_streamHC_t_alignment() - 1)) == 0); /* check alignment */ #endif if (((size_t)(state)&(sizeof(void*)-1)) != 0) return 0; /* Error : state is not aligned for pointers (32 or 64 bits) */ @@ -1003,9 +1008,7 @@ LZ4_streamHC_t* LZ4_initStreamHC (void* buffer, size_t size) LZ4_streamHC_t* const LZ4_streamHCPtr = (LZ4_streamHC_t*)buffer; if (buffer == NULL) return NULL; if (size < sizeof(LZ4_streamHC_t)) return NULL; -#ifndef _MSC_VER /* for some reason, Visual fails the aligment test on 32-bit x86 : - * it reports an aligment of 8-bytes, - * while actually aligning LZ4_streamHC_t on 4 bytes. */ +#if LZ4_ALIGN_TEST if (((size_t)buffer) & (LZ4_streamHC_t_alignment() - 1)) return NULL; /* alignment check */ #endif /* if compilation fails here, LZ4_STREAMHCSIZE must be increased */ -- cgit v1.2.3 From 389eacdfeaefee5326de2352ff42aa0195f156a0 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 2 Oct 2020 16:37:34 -0700 Subject: Appveyor: added compilation and runtime fuzzer tests to all Windows compiler targets --- appveyor.yml | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 57b6dda..31894d3 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -47,10 +47,14 @@ build_script: make -v && echo ----- && if not [%PLATFORM%]==[clang] ( - make -C programs lz4 && make -C tests fullbench && make -C lib lib + make -C programs lz4 && + make -C tests fullbench && + make -C tests fuzzer && + make -C lib lib ) ELSE ( make -C programs lz4 CC=clang MOREFLAGS="--target=x86_64-w64-mingw32 -Werror -Wconversion -Wno-sign-conversion" && make -C tests fullbench CC=clang MOREFLAGS="--target=x86_64-w64-mingw32 -Werror -Wconversion -Wno-sign-conversion" && + make -C tests fuzzer CC=clang MOREFLAGS="--target=x86_64-w64-mingw32 -Werror -Wconversion -Wno-sign-conversion" && make -C lib lib CC=clang MOREFLAGS="--target=x86_64-w64-mingw32 -Werror -Wconversion -Wno-sign-conversion" ) ) @@ -77,7 +81,7 @@ build_script: 7z.exe a bin\lz4_x86.zip NEWS .\bin\lz4.exe .\bin\README.md .\bin\example .\bin\dll .\bin\static .\bin\include && appveyor PushArtifact bin\lz4_x86.zip ) - - if [%COMPILER%]==[gcc] (COPY tests\fullbench.exe programs\) + - if [%COMPILER%]==[gcc] (COPY tests\*.exe programs\) - if [%COMPILER%]==[visual] ( ECHO *** && ECHO *** Building Visual Studio 2010 %PLATFORM%\%CONFIGURATION% && @@ -110,7 +114,9 @@ test_script: lz4 -i1b10 lz4.exe && lz4 -i1b15 lz4.exe && echo ------- lz4 tested ------- && - fullbench.exe -i1 fullbench.exe + fullbench.exe -i1 fullbench.exe && + echo trying to launch fuzzer.exe && + fuzzer.exe -v -T30s ) artifacts: -- cgit v1.2.3 From 4b97866333523b2bc778d88fe68b6b86ea26ce6e Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 2 Oct 2020 18:02:41 -0700 Subject: reduce new test stack usage use heap instead --- tests/fuzzer.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/tests/fuzzer.c b/tests/fuzzer.c index 4fef4af..3eb5789 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -1201,22 +1201,24 @@ static void FUZ_unitTests(int compressionLevel) } DISPLAYLEVEL(3, "LZ4_initStreamHC with multiple valid alignments : "); - { struct { + { typedef struct { LZ4_streamHC_t hc1; char c1; LZ4_streamHC_t hc2; char c2; LZ4_streamHC_t hc3; - } shc; - (void)shc.c1; (void)shc.c2; /* tell cppcheck these variables are unused */ + } shct; + shct* const shc = malloc(sizeof(*shc)); + assert(shc != NULL); + memset(shc, 0, sizeof(*shc)); DISPLAYLEVEL(3, "hc1(%p) hc2(%p) hc3(%p) size(0x%x): ", - &(shc.hc1), &(shc.hc2), &(shc.hc3), (unsigned)sizeof(LZ4_streamHC_t)); - FUZ_CHECKTEST( LZ4_initStreamHC(&(shc.hc1), sizeof(shc.hc1)) == NULL, "hc1 (%p) failed init", &(shc.hc1) ); - FUZ_CHECKTEST( LZ4_initStreamHC(&(shc.hc2), sizeof(shc.hc2)) == NULL, "hc2 (%p) failed init", &(shc.hc2) ); - FUZ_CHECKTEST( LZ4_initStreamHC(&(shc.hc3), sizeof(shc.hc3)) == NULL, "hc3 (%p) failed init", &(shc.hc3) ); - FUZ_CHECKTEST( LZ4_initStreamHC((char*)&(shc.hc1) + 1, sizeof(shc.hc1)) != NULL, - "hc1+1 (%p) init must fail, due to bad alignment", (char*)&(shc.hc1) + 1 ); - /* no need to release anything : LZ4_streamHC_t is a simple POD type */ + &(shc->hc1), &(shc->hc2), &(shc->hc3), (unsigned)sizeof(LZ4_streamHC_t)); + FUZ_CHECKTEST( LZ4_initStreamHC(&(shc->hc1), sizeof(shc->hc1)) == NULL, "hc1 (%p) failed init", &(shc->hc1) ); + FUZ_CHECKTEST( LZ4_initStreamHC(&(shc->hc2), sizeof(shc->hc2)) == NULL, "hc2 (%p) failed init", &(shc->hc2) ); + FUZ_CHECKTEST( LZ4_initStreamHC(&(shc->hc3), sizeof(shc->hc3)) == NULL, "hc3 (%p) failed init", &(shc->hc3) ); + FUZ_CHECKTEST( LZ4_initStreamHC((char*)&(shc->hc1) + 1, sizeof(shc->hc1)) != NULL, + "hc1+1 (%p) init must fail, due to bad alignment", (char*)&(shc->hc1) + 1 ); + free(shc); } DISPLAYLEVEL(3, "OK \n"); -- cgit v1.2.3 From c24e979063946a966c0474bb51a385e891164502 Mon Sep 17 00:00:00 2001 From: remittor Date: Sat, 3 Oct 2020 18:28:24 +0300 Subject: [lz4hc] Made function LZ4HC_encodeSequence a human readable --- lib/lz4hc.c | 53 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/lib/lz4hc.c b/lib/lz4hc.c index cd802d8..080816e 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -460,83 +460,90 @@ int LZ4HC_InsertAndFindBestMatch(LZ4HC_CCtx_internal* const hc4, /* Index tabl * @return : 0 if ok, * 1 if buffer issue detected */ LZ4_FORCE_INLINE int LZ4HC_encodeSequence ( - const BYTE** ip, - BYTE** op, - const BYTE** anchor, + const BYTE** _ip, + BYTE** _op, + const BYTE** _anchor, int matchLength, const BYTE* const match, limitedOutput_directive limit, BYTE* oend) { +#define ip (*_ip) +#define op (*_op) +#define anchor (*_anchor) + size_t length; - BYTE* const token = (*op)++; + BYTE* const token = op++; #if defined(LZ4_DEBUG) && (LZ4_DEBUG >= 6) static const BYTE* start = NULL; static U32 totalCost = 0; - U32 const pos = (start==NULL) ? 0 : (U32)(*anchor - start); - U32 const ll = (U32)(*ip - *anchor); + U32 const pos = (start==NULL) ? 0 : (U32)(anchor - start); + U32 const ll = (U32)(ip - anchor); U32 const llAdd = (ll>=15) ? ((ll-15) / 255) + 1 : 0; U32 const mlAdd = (matchLength>=19) ? ((matchLength-19) / 255) + 1 : 0; U32 const cost = 1 + llAdd + ll + 2 + mlAdd; - if (start==NULL) start = *anchor; /* only works for single segment */ + if (start==NULL) start = anchor; /* only works for single segment */ /* g_debuglog_enable = (pos >= 2228) & (pos <= 2262); */ DEBUGLOG(6, "pos:%7u -- literals:%4u, match:%4i, offset:%5u, cost:%4u + %5u", pos, - (U32)(*ip - *anchor), matchLength, (U32)(*ip-match), + (U32)(ip - anchor), matchLength, (U32)(ip-match), cost, totalCost); totalCost += cost; #endif /* Encode Literal length */ - length = (size_t)(*ip - *anchor); + length = (size_t)(ip - anchor); LZ4_STATIC_ASSERT(notLimited == 0); /* Check output limit */ - if (limit && ((*op + (length / 255) + length + (2 + 1 + LASTLITERALS)) > oend)) { + if (limit && ((op + (length / 255) + length + (2 + 1 + LASTLITERALS)) > oend)) { DEBUGLOG(6, "Not enough room to write %i literals (%i bytes remaining)", - (int)length, (int)(oend-*op)); + (int)length, (int)(oend - op)); return 1; } if (length >= RUN_MASK) { size_t len = length - RUN_MASK; *token = (RUN_MASK << ML_BITS); - for(; len >= 255 ; len -= 255) *(*op)++ = 255; - *(*op)++ = (BYTE)len; + for(; len >= 255 ; len -= 255) *op++ = 255; + *op++ = (BYTE)len; } else { *token = (BYTE)(length << ML_BITS); } /* Copy Literals */ - LZ4_wildCopy8(*op, *anchor, (*op) + length); - *op += length; + LZ4_wildCopy8(op, anchor, op + length); + op += length; /* Encode Offset */ - assert( (*ip - match) <= LZ4_DISTANCE_MAX ); /* note : consider providing offset as a value, rather than as a pointer difference */ - LZ4_writeLE16(*op, (U16)(*ip-match)); *op += 2; + assert( (ip - match) <= LZ4_DISTANCE_MAX ); /* note : consider providing offset as a value, rather than as a pointer difference */ + LZ4_writeLE16(op, (U16)(ip - match)); op += 2; /* Encode MatchLength */ assert(matchLength >= MINMATCH); length = (size_t)matchLength - MINMATCH; - if (limit && (*op + (length / 255) + (1 + LASTLITERALS) > oend)) { + if (limit && (op + (length / 255) + (1 + LASTLITERALS) > oend)) { DEBUGLOG(6, "Not enough room to write match length"); return 1; /* Check output limit */ } if (length >= ML_MASK) { *token += ML_MASK; length -= ML_MASK; - for(; length >= 510 ; length -= 510) { *(*op)++ = 255; *(*op)++ = 255; } - if (length >= 255) { length -= 255; *(*op)++ = 255; } - *(*op)++ = (BYTE)length; + for(; length >= 510 ; length -= 510) { *op++ = 255; *op++ = 255; } + if (length >= 255) { length -= 255; *op++ = 255; } + *op++ = (BYTE)length; } else { *token += (BYTE)(length); } /* Prepare next loop */ - *ip += matchLength; - *anchor = *ip; + ip += matchLength; + anchor = ip; return 0; } +#undef ip +#undef op +#undef anchor LZ4_FORCE_INLINE int LZ4HC_compress_hashChain ( LZ4HC_CCtx_internal* const ctx, -- cgit v1.2.3 From c4792cdfa917911bea91ebf245e02a1a4b9038c6 Mon Sep 17 00:00:00 2001 From: remittor Date: Tue, 6 Oct 2020 17:16:43 +0300 Subject: Fix: The "inline" specifier do not use for LZ4_wildCopy8 and LZ4_wildCopy32 This problem was reproduced on MSVC 2015 (32-bit). Both functions were called using the operator "call". --- lib/lz4.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/lz4.c b/lib/lz4.c index 0290ea2..1d08e10 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -162,7 +162,7 @@ # define LZ4_FORCE_O2_INLINE_GCC_PPC64LE __attribute__((optimize("O2"))) LZ4_FORCE_INLINE #else # define LZ4_FORCE_O2_GCC_PPC64LE -# define LZ4_FORCE_O2_INLINE_GCC_PPC64LE static +# define LZ4_FORCE_O2_INLINE_GCC_PPC64LE LZ4_FORCE_INLINE #endif #if (defined(__GNUC__) && (__GNUC__ >= 3)) || (defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 800)) || defined(__clang__) -- cgit v1.2.3 From 749bd91a06be385b6485bfc7fe94e8a86b3ed13a Mon Sep 17 00:00:00 2001 From: remittor Date: Wed, 7 Oct 2020 09:51:08 +0300 Subject: Replace define LZ4_FORCE_O2_INLINE_GCC_PPC64LE to LZ4_FORCE_INLINE There is no reason to separate these two definitions! --- lib/lz4.c | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 1d08e10..0f53857 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -143,7 +143,7 @@ # endif /* _MSC_VER */ #endif /* LZ4_FORCE_INLINE */ -/* LZ4_FORCE_O2_GCC_PPC64LE and LZ4_FORCE_O2_INLINE_GCC_PPC64LE +/* LZ4_FORCE_O2 and LZ4_FORCE_INLINE * gcc on ppc64le generates an unrolled SIMDized loop for LZ4_wildCopy8, * together with a simple 8-byte copy loop as a fall-back path. * However, this optimization hurts the decompression speed by >30%, @@ -158,11 +158,11 @@ * of LZ4_wildCopy8 does not affect the compression speed. */ #if defined(__PPC64__) && defined(__LITTLE_ENDIAN__) && defined(__GNUC__) && !defined(__clang__) -# define LZ4_FORCE_O2_GCC_PPC64LE __attribute__((optimize("O2"))) -# define LZ4_FORCE_O2_INLINE_GCC_PPC64LE __attribute__((optimize("O2"))) LZ4_FORCE_INLINE +# define LZ4_FORCE_O2 __attribute__((optimize("O2"))) +# undef LZ4_FORCE_INLINE +# define LZ4_FORCE_INLINE static __inline __attribute__((optimize("O2"),always_inline)) #else -# define LZ4_FORCE_O2_GCC_PPC64LE -# define LZ4_FORCE_O2_INLINE_GCC_PPC64LE LZ4_FORCE_INLINE +# define LZ4_FORCE_O2 #endif #if (defined(__GNUC__) && (__GNUC__ >= 3)) || (defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 800)) || defined(__clang__) @@ -381,7 +381,7 @@ static void LZ4_writeLE16(void* memPtr, U16 value) } /* customized variant of memcpy, which can overwrite up to 8 bytes beyond dstEnd */ -LZ4_FORCE_O2_INLINE_GCC_PPC64LE +LZ4_FORCE_INLINE void LZ4_wildCopy8(void* dstPtr, const void* srcPtr, void* dstEnd) { BYTE* d = (BYTE*)dstPtr; @@ -410,7 +410,7 @@ static const int dec64table[8] = {0, 0, 0, -1, -4, 1, 2, 3}; #if LZ4_FAST_DEC_LOOP -LZ4_FORCE_O2_INLINE_GCC_PPC64LE void +LZ4_FORCE_INLINE void LZ4_memcpy_using_offset_base(BYTE* dstPtr, const BYTE* srcPtr, BYTE* dstEnd, const size_t offset) { if (offset < 8) { @@ -434,7 +434,7 @@ LZ4_memcpy_using_offset_base(BYTE* dstPtr, const BYTE* srcPtr, BYTE* dstEnd, con /* customized variant of memcpy, which can overwrite up to 32 bytes beyond dstEnd * this version copies two times 16 bytes (instead of one time 32 bytes) * because it must be compatible with offsets >= 16. */ -LZ4_FORCE_O2_INLINE_GCC_PPC64LE void +LZ4_FORCE_INLINE void LZ4_wildCopy32(void* dstPtr, const void* srcPtr, void* dstEnd) { BYTE* d = (BYTE*)dstPtr; @@ -447,7 +447,7 @@ LZ4_wildCopy32(void* dstPtr, const void* srcPtr, void* dstEnd) /* LZ4_memcpy_using_offset() presumes : * - dstEnd >= dstPtr + MINMATCH * - there is at least 8 bytes available to write after dstEnd */ -LZ4_FORCE_O2_INLINE_GCC_PPC64LE void +LZ4_FORCE_INLINE void LZ4_memcpy_using_offset(BYTE* dstPtr, const BYTE* srcPtr, BYTE* dstEnd, const size_t offset) { BYTE v[8]; @@ -2145,7 +2145,7 @@ LZ4_decompress_generic( /*===== Instantiate the API decoding functions. =====*/ -LZ4_FORCE_O2_GCC_PPC64LE +LZ4_FORCE_O2 int LZ4_decompress_safe(const char* source, char* dest, int compressedSize, int maxDecompressedSize) { return LZ4_decompress_generic(source, dest, compressedSize, maxDecompressedSize, @@ -2153,7 +2153,7 @@ int LZ4_decompress_safe(const char* source, char* dest, int compressedSize, int (BYTE*)dest, NULL, 0); } -LZ4_FORCE_O2_GCC_PPC64LE +LZ4_FORCE_O2 int LZ4_decompress_safe_partial(const char* src, char* dst, int compressedSize, int targetOutputSize, int dstCapacity) { dstCapacity = MIN(targetOutputSize, dstCapacity); @@ -2162,7 +2162,7 @@ int LZ4_decompress_safe_partial(const char* src, char* dst, int compressedSize, noDict, (BYTE*)dst, NULL, 0); } -LZ4_FORCE_O2_GCC_PPC64LE +LZ4_FORCE_O2 int LZ4_decompress_fast(const char* source, char* dest, int originalSize) { return LZ4_decompress_generic(source, dest, 0, originalSize, @@ -2172,7 +2172,7 @@ int LZ4_decompress_fast(const char* source, char* dest, int originalSize) /*===== Instantiate a few more decoding cases, used more than once. =====*/ -LZ4_FORCE_O2_GCC_PPC64LE /* Exported, an obsolete API function. */ +LZ4_FORCE_O2 /* Exported, an obsolete API function. */ int LZ4_decompress_safe_withPrefix64k(const char* source, char* dest, int compressedSize, int maxOutputSize) { return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, @@ -2188,7 +2188,7 @@ int LZ4_decompress_fast_withPrefix64k(const char* source, char* dest, int origin return LZ4_decompress_fast(source, dest, originalSize); } -LZ4_FORCE_O2_GCC_PPC64LE +LZ4_FORCE_O2 static int LZ4_decompress_safe_withSmallPrefix(const char* source, char* dest, int compressedSize, int maxOutputSize, size_t prefixSize) { @@ -2197,7 +2197,7 @@ static int LZ4_decompress_safe_withSmallPrefix(const char* source, char* dest, i (BYTE*)dest-prefixSize, NULL, 0); } -LZ4_FORCE_O2_GCC_PPC64LE +LZ4_FORCE_O2 int LZ4_decompress_safe_forceExtDict(const char* source, char* dest, int compressedSize, int maxOutputSize, const void* dictStart, size_t dictSize) @@ -2207,7 +2207,7 @@ int LZ4_decompress_safe_forceExtDict(const char* source, char* dest, (BYTE*)dest, (const BYTE*)dictStart, dictSize); } -LZ4_FORCE_O2_GCC_PPC64LE +LZ4_FORCE_O2 static int LZ4_decompress_fast_extDict(const char* source, char* dest, int originalSize, const void* dictStart, size_t dictSize) { @@ -2296,7 +2296,7 @@ int LZ4_decoderRingBufferSize(int maxBlockSize) If it's not possible, save the relevant part of decoded data into a safe buffer, and indicate where it stands using LZ4_setStreamDecode() */ -LZ4_FORCE_O2_GCC_PPC64LE +LZ4_FORCE_O2 int LZ4_decompress_safe_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* source, char* dest, int compressedSize, int maxOutputSize) { LZ4_streamDecode_t_internal* lz4sd = &LZ4_streamDecode->internal_donotuse; @@ -2336,7 +2336,7 @@ int LZ4_decompress_safe_continue (LZ4_streamDecode_t* LZ4_streamDecode, const ch return result; } -LZ4_FORCE_O2_GCC_PPC64LE +LZ4_FORCE_O2 int LZ4_decompress_fast_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* source, char* dest, int originalSize) { LZ4_streamDecode_t_internal* lz4sd = &LZ4_streamDecode->internal_donotuse; -- cgit v1.2.3 From 0e3933edd435c54cc2e21e38f5d4ba7bf644a24e Mon Sep 17 00:00:00 2001 From: remittor Date: Wed, 7 Oct 2020 09:52:40 +0300 Subject: Replace "static" to "LZ4_FORCE_INLINE" for small functions The "static" specifier does not guarantee that the function will be inlined. --- lib/lz4.c | 36 ++++++++++++++++++------------------ lib/lz4hc.c | 4 ++-- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 0f53857..6adfafd 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -299,7 +299,7 @@ typedef enum { #define LZ4_memcpy(dst, src, size) memcpy(dst, src, size) #endif -static unsigned LZ4_isLittleEndian(void) +LZ4_FORCE_INLINE unsigned LZ4_isLittleEndian(void) { const union { U32 u; BYTE c[4]; } one = { 1 }; /* don't use static : performance detrimental */ return one.c[0]; @@ -309,12 +309,12 @@ static unsigned LZ4_isLittleEndian(void) #if defined(LZ4_FORCE_MEMORY_ACCESS) && (LZ4_FORCE_MEMORY_ACCESS==2) /* lie to the compiler about data alignment; use with caution */ -static U16 LZ4_read16(const void* memPtr) { return *(const U16*) memPtr; } -static U32 LZ4_read32(const void* memPtr) { return *(const U32*) memPtr; } -static reg_t LZ4_read_ARCH(const void* memPtr) { return *(const reg_t*) memPtr; } +LZ4_FORCE_INLINE U16 LZ4_read16(const void* memPtr) { return *(const U16*) memPtr; } +LZ4_FORCE_INLINE U32 LZ4_read32(const void* memPtr) { return *(const U32*) memPtr; } +LZ4_FORCE_INLINE reg_t LZ4_read_ARCH(const void* memPtr) { return *(const reg_t*) memPtr; } -static void LZ4_write16(void* memPtr, U16 value) { *(U16*)memPtr = value; } -static void LZ4_write32(void* memPtr, U32 value) { *(U32*)memPtr = value; } +LZ4_FORCE_INLINE void LZ4_write16(void* memPtr, U16 value) { *(U16*)memPtr = value; } +LZ4_FORCE_INLINE void LZ4_write32(void* memPtr, U32 value) { *(U32*)memPtr = value; } #elif defined(LZ4_FORCE_MEMORY_ACCESS) && (LZ4_FORCE_MEMORY_ACCESS==1) @@ -322,36 +322,36 @@ static void LZ4_write32(void* memPtr, U32 value) { *(U32*)memPtr = value; } /* currently only defined for gcc and icc */ typedef union { U16 u16; U32 u32; reg_t uArch; } __attribute__((packed)) unalign; -static U16 LZ4_read16(const void* ptr) { return ((const unalign*)ptr)->u16; } -static U32 LZ4_read32(const void* ptr) { return ((const unalign*)ptr)->u32; } -static reg_t LZ4_read_ARCH(const void* ptr) { return ((const unalign*)ptr)->uArch; } +LZ4_FORCE_INLINE U16 LZ4_read16(const void* ptr) { return ((const unalign*)ptr)->u16; } +LZ4_FORCE_INLINE U32 LZ4_read32(const void* ptr) { return ((const unalign*)ptr)->u32; } +LZ4_FORCE_INLINE reg_t LZ4_read_ARCH(const void* ptr) { return ((const unalign*)ptr)->uArch; } -static void LZ4_write16(void* memPtr, U16 value) { ((unalign*)memPtr)->u16 = value; } -static void LZ4_write32(void* memPtr, U32 value) { ((unalign*)memPtr)->u32 = value; } +LZ4_FORCE_INLINE void LZ4_write16(void* memPtr, U16 value) { ((unalign*)memPtr)->u16 = value; } +LZ4_FORCE_INLINE void LZ4_write32(void* memPtr, U32 value) { ((unalign*)memPtr)->u32 = value; } #else /* safe and portable access using memcpy() */ -static U16 LZ4_read16(const void* memPtr) +LZ4_FORCE_INLINE U16 LZ4_read16(const void* memPtr) { U16 val; LZ4_memcpy(&val, memPtr, sizeof(val)); return val; } -static U32 LZ4_read32(const void* memPtr) +LZ4_FORCE_INLINE U32 LZ4_read32(const void* memPtr) { U32 val; LZ4_memcpy(&val, memPtr, sizeof(val)); return val; } -static reg_t LZ4_read_ARCH(const void* memPtr) +LZ4_FORCE_INLINE reg_t LZ4_read_ARCH(const void* memPtr) { reg_t val; LZ4_memcpy(&val, memPtr, sizeof(val)); return val; } -static void LZ4_write16(void* memPtr, U16 value) +LZ4_FORCE_INLINE void LZ4_write16(void* memPtr, U16 value) { LZ4_memcpy(memPtr, &value, sizeof(value)); } -static void LZ4_write32(void* memPtr, U32 value) +LZ4_FORCE_INLINE void LZ4_write32(void* memPtr, U32 value) { LZ4_memcpy(memPtr, &value, sizeof(value)); } @@ -359,7 +359,7 @@ static void LZ4_write32(void* memPtr, U32 value) #endif /* LZ4_FORCE_MEMORY_ACCESS */ -static U16 LZ4_readLE16(const void* memPtr) +LZ4_FORCE_INLINE U16 LZ4_readLE16(const void* memPtr) { if (LZ4_isLittleEndian()) { return LZ4_read16(memPtr); @@ -369,7 +369,7 @@ static U16 LZ4_readLE16(const void* memPtr) } } -static void LZ4_writeLE16(void* memPtr, U16 value) +LZ4_FORCE_INLINE void LZ4_writeLE16(void* memPtr, U16 value) { if (LZ4_isLittleEndian()) { LZ4_write16(memPtr, value); diff --git a/lib/lz4hc.c b/lib/lz4hc.c index cd802d8..8e3bd70 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -158,7 +158,7 @@ int LZ4HC_countBack(const BYTE* const ip, const BYTE* const match, #endif -static U32 LZ4HC_rotatePattern(size_t const rotate, U32 const pattern) +LZ4_FORCE_INLINE U32 LZ4HC_rotatePattern(size_t const rotate, U32 const pattern) { size_t const bitsToRotate = (rotate & (sizeof(pattern) - 1)) << 3; if (bitsToRotate == 0) @@ -223,7 +223,7 @@ LZ4HC_reverseCountPattern(const BYTE* ip, const BYTE* const iLow, U32 pattern) * 4 byte MINMATCH would overflow. * @returns true if the match index is okay. */ -static int LZ4HC_protectDictEnd(U32 const dictLimit, U32 const matchIndex) +LZ4_FORCE_INLINE int LZ4HC_protectDictEnd(U32 const dictLimit, U32 const matchIndex) { return ((U32)((dictLimit - 1) - matchIndex) >= 3); } -- cgit v1.2.3 From ad552101ddeef3bf1967048ecb3cc5844a1ad470 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sat, 31 Oct 2020 01:55:04 -0700 Subject: fix minor explicit cast --- tests/fuzzer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/fuzzer.c b/tests/fuzzer.c index 3eb5789..84f9693 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -1208,7 +1208,7 @@ static void FUZ_unitTests(int compressionLevel) char c2; LZ4_streamHC_t hc3; } shct; - shct* const shc = malloc(sizeof(*shc)); + shct* const shc = (shct*)malloc(sizeof(*shc)); assert(shc != NULL); memset(shc, 0, sizeof(*shc)); DISPLAYLEVEL(3, "hc1(%p) hc2(%p) hc3(%p) size(0x%x): ", -- cgit v1.2.3 From 073fe18edf0411523ca5c3935b5b7805e4cafe14 Mon Sep 17 00:00:00 2001 From: Nick Terrell Date: Thu, 5 Nov 2020 10:27:43 -0800 Subject: [ossfuzz] Fix parallel builds With `make -j` multiple builds of `standaloneengine.o` happened in parallel. Fix this by detecting `standaloneengine.o` and moving it to a depedency. --- ossfuzz/Makefile | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/ossfuzz/Makefile b/ossfuzz/Makefile index f247405..2ec1675 100644 --- a/ossfuzz/Makefile +++ b/ossfuzz/Makefile @@ -26,7 +26,7 @@ # ########################################################################## LZ4DIR := ../lib -LIB_FUZZING_ENGINE ?= standaloneengine.o +LIB_FUZZING_ENGINE ?= DEBUGLEVEL?= 1 DEBUGFLAGS = -g -DLZ4_DEBUG=$(DEBUGLEVEL) @@ -59,12 +59,12 @@ $(LZ4DIR)/liblz4.a: $(CC) -c $(LZ4_CFLAGS) $(LZ4_CPPFLAGS) $< -o $@ # Generic rule for generating fuzzers -%_fuzzer: %_fuzzer.o lz4_helpers.o fuzz_data_producer.o $(LZ4DIR)/liblz4.a - # Compile the standalone code just in case. The OSS-Fuzz code might - # override the LIB_FUZZING_ENGINE value to "-fsanitize=fuzzer" - $(CC) -c $(LZ4_CFLAGS) $(LZ4_CPPFLAGS) standaloneengine.c -o standaloneengine.o - - # Now compile the actual fuzzer. +ifeq ($(LIB_FUZZING_ENGINE),) + LIB_FUZZING_DEPS := standaloneengine.o +else + LIB_FUZZING_DEPS := +endif +%_fuzzer: %_fuzzer.o lz4_helpers.o fuzz_data_producer.o $(LZ4DIR)/liblz4.a $(LIB_FUZZING_DEPS) $(CXX) $(LZ4_CXXFLAGS) $(LZ4_CPPFLAGS) $(LDFLAGS) $(LIB_FUZZING_ENGINE) $^ -o $@$(EXT) %_fuzzer_clean: -- cgit v1.2.3 From e968a241293fe86c0f8b115a1223d9d78a0eda00 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 6 Nov 2020 14:46:48 -0800 Subject: unified alignment test across lz4.c and lz4hc.c --- lib/lz4.c | 33 +++++++++++++++++++------- lib/lz4hc.c | 24 +++++-------------- tests/fuzzer.c | 74 ++++++++++++++++++++++++++++++++++++---------------------- 3 files changed, 76 insertions(+), 55 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 0290ea2..9e6abba 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -178,6 +178,20 @@ #define unlikely(expr) expect((expr) != 0, 0) #endif +/* for some reason, Visual Studio can fail the aligment test on 32-bit x86 : + * it sometimes report an aligment of 8-bytes (at least in some configurations), + * while only providing a `malloc()` memory area aligned on 4-bytes, + * which is inconsistent with malloc() contract. + * The source of the issue is still unclear. + * Mitigation : made the alignment test optional */ +#ifndef LZ4_ALIGN_TEST /* can be externally provided */ +# if (defined(_MSC_VER) && !defined(_M_X64)) +# define LZ4_ALIGN_TEST 0 /* disable on win32+visual */ +# else +# define LZ4_ALIGN_TEST 1 +# endif +#endif + /*-************************************ * Memory routines @@ -243,6 +257,11 @@ static const int LZ4_minLength = (MFLIMIT+1); # define DEBUGLOG(l, ...) {} /* disabled */ #endif +static int LZ4_isAligned(const void* ptr, size_t alignment) +{ + return ((size_t)ptr & (alignment -1)) == 0; +} + /*-************************************ * Types @@ -1406,26 +1425,22 @@ LZ4_stream_t* LZ4_createStream(void) return lz4s; } -#ifndef _MSC_VER /* for some reason, Visual fails the aligment test on 32-bit x86 : - it reports an aligment of 8-bytes, - while actually aligning LZ4_stream_t on 4 bytes. */ static size_t LZ4_stream_t_alignment(void) { +#if LZ4_ALIGN_TEST typedef struct { char c; LZ4_stream_t t; } t_a; return sizeof(t_a) - sizeof(LZ4_stream_t); -} +#else + return 1; /* effectively disabled */ #endif +} LZ4_stream_t* LZ4_initStream (void* buffer, size_t size) { DEBUGLOG(5, "LZ4_initStream"); if (buffer == NULL) { return NULL; } if (size < sizeof(LZ4_stream_t)) { return NULL; } -#ifndef _MSC_VER /* for some reason, Visual fails the aligment test on 32-bit x86 : - it reports an aligment of 8-bytes, - while actually aligning LZ4_stream_t on 4 bytes. */ - if (((size_t)buffer) & (LZ4_stream_t_alignment() - 1)) { return NULL; } /* alignment check */ -#endif + if (!LZ4_isAligned(buffer, LZ4_stream_t_alignment())) return NULL; MEM_INIT(buffer, 0, sizeof(LZ4_stream_t)); return (LZ4_stream_t*)buffer; } diff --git a/lib/lz4hc.c b/lib/lz4hc.c index cf03eba..29b6073 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -75,15 +75,6 @@ typedef enum { noDictCtx, usingDictCtxHc } dictCtx_directive; #define OPTIMAL_ML (int)((ML_MASK-1)+MINMATCH) #define LZ4_OPT_NUM (1<<12) -/* for some reason, Visual Studio fails the aligment test on 32-bit x86 : - * it reports an aligment of 8-bytes, - * while LZ4_streamHC_t only requires alignment of 4-bytes - * resulting in initialization error when allocating state with malloc() */ -#if (defined(_MSC_VER) && !defined(_M_X64)) -# define LZ4_ALIGN_TEST 0 -#else -# define LZ4_ALIGN_TEST 1 -#endif /*=== Macros ===*/ #define MIN(a,b) ( (a) < (b) ? (a) : (b) ) @@ -921,23 +912,22 @@ LZ4HC_compress_generic ( int LZ4_sizeofStateHC(void) { return (int)sizeof(LZ4_streamHC_t); } -#if LZ4_ALIGN_TEST static size_t LZ4_streamHC_t_alignment(void) { +#if LZ4_ALIGN_TEST typedef struct { char c; LZ4_streamHC_t t; } t_a; return sizeof(t_a) - sizeof(LZ4_streamHC_t); -} +#else + return 1; /* effectively disabled */ #endif +} /* state is presumed correctly initialized, * in which case its size and alignment have already been validate */ int LZ4_compress_HC_extStateHC_fastReset (void* state, const char* src, char* dst, int srcSize, int dstCapacity, int compressionLevel) { LZ4HC_CCtx_internal* const ctx = &((LZ4_streamHC_t*)state)->internal_donotuse; -#if LZ4_ALIGN_TEST - assert(((size_t)state & (LZ4_streamHC_t_alignment() - 1)) == 0); /* check alignment */ -#endif - if (((size_t)(state)&(sizeof(void*)-1)) != 0) return 0; /* Error : state is not aligned for pointers (32 or 64 bits) */ + if (!LZ4_isAligned(state, LZ4_streamHC_t_alignment())) return 0; LZ4_resetStreamHC_fast((LZ4_streamHC_t*)state, compressionLevel); LZ4HC_init_internal (ctx, (const BYTE*)src); if (dstCapacity < LZ4_compressBound(srcSize)) @@ -1008,9 +998,7 @@ LZ4_streamHC_t* LZ4_initStreamHC (void* buffer, size_t size) LZ4_streamHC_t* const LZ4_streamHCPtr = (LZ4_streamHC_t*)buffer; if (buffer == NULL) return NULL; if (size < sizeof(LZ4_streamHC_t)) return NULL; -#if LZ4_ALIGN_TEST - if (((size_t)buffer) & (LZ4_streamHC_t_alignment() - 1)) return NULL; /* alignment check */ -#endif + if (!LZ4_isAligned(buffer, LZ4_streamHC_t_alignment())) return NULL; /* if compilation fails here, LZ4_STREAMHCSIZE must be increased */ LZ4_STATIC_ASSERT(sizeof(LZ4HC_CCtx_internal) <= LZ4_STREAMHCSIZE); DEBUGLOG(4, "LZ4_initStreamHC(%p, %u)", LZ4_streamHCPtr, (unsigned)size); diff --git a/tests/fuzzer.c b/tests/fuzzer.c index 84f9693..29c6a8a 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -1125,27 +1125,47 @@ static void FUZ_unitTests(int compressionLevel) } } } } } DISPLAYLEVEL(3, " OK \n"); - /* LZ4 streaming tests */ - { LZ4_stream_t streamingState; - U64 crcOrig; - int result; + DISPLAYLEVEL(3, "LZ4_initStream with multiple valid alignments : "); + { typedef struct { + LZ4_stream_t state1; + LZ4_stream_t state2; + char c; + LZ4_stream_t state3; + } shct; + shct* const shc = (shct*)malloc(sizeof(*shc)); + assert(shc != NULL); + memset(shc, 0, sizeof(*shc)); + DISPLAYLEVEL(3, "state1(%p) state2(%p) state3(%p) size(0x%x): ", + &(shc->state1), &(shc->state2), &(shc->state3), (unsigned)sizeof(LZ4_stream_t)); + FUZ_CHECKTEST( LZ4_initStream(&(shc->state1), sizeof(shc->state1)) == NULL, "state1 (%p) failed init", &(shc->state1) ); + FUZ_CHECKTEST( LZ4_initStream(&(shc->state2), sizeof(shc->state2)) == NULL, "state2 (%p) failed init", &(shc->state2) ); + FUZ_CHECKTEST( LZ4_initStream(&(shc->state3), sizeof(shc->state3)) == NULL, "state3 (%p) failed init", &(shc->state3) ); + FUZ_CHECKTEST( LZ4_initStream((char*)&(shc->state1) + 1, sizeof(shc->state1)) != NULL, + "hc1+1 (%p) init must fail, due to bad alignment", (char*)&(shc->state1) + 1 ); + free(shc); + } + DISPLAYLEVEL(3, "all inits OK \n"); - /* Allocation test */ - { LZ4_stream_t* const statePtr = LZ4_createStream(); - FUZ_CHECKTEST(statePtr==NULL, "LZ4_createStream() allocation failed"); - LZ4_freeStream(statePtr); - } + /* Allocation test */ + { LZ4_stream_t* const statePtr = LZ4_createStream(); + FUZ_CHECKTEST(statePtr==NULL, "LZ4_createStream() allocation failed"); + LZ4_freeStream(statePtr); + } - /* simple compression test */ - crcOrig = XXH64(testInput, testCompressedSize, 0); - LZ4_initStream(&streamingState, sizeof(streamingState)); - result = LZ4_compress_fast_continue(&streamingState, testInput, testCompressed, testCompressedSize, testCompressedSize-1, 1); - FUZ_CHECKTEST(result==0, "LZ4_compress_fast_continue() compression failed!"); + /* LZ4 streaming tests */ + { LZ4_stream_t streamingState; - result = LZ4_decompress_safe(testCompressed, testVerify, result, testCompressedSize); - FUZ_CHECKTEST(result!=(int)testCompressedSize, "LZ4_decompress_safe() decompression failed"); - { U64 const crcNew = XXH64(testVerify, testCompressedSize, 0); - FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe() decompression corruption"); } + /* simple compression test */ + { U64 const crcOrig = XXH64(testInput, testCompressedSize, 0); + LZ4_initStream(&streamingState, sizeof(streamingState)); + { int const cs = LZ4_compress_fast_continue(&streamingState, testInput, testCompressed, testCompressedSize, testCompressedSize-1, 1); + FUZ_CHECKTEST(cs==0, "LZ4_compress_fast_continue() compression failed!"); + { int const r = LZ4_decompress_safe(testCompressed, testVerify, cs, testCompressedSize); + FUZ_CHECKTEST(r!=(int)testCompressedSize, "LZ4_decompress_safe() decompression failed"); + } } + { U64 const crcNew = XXH64(testVerify, testCompressedSize, 0); + FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe() decompression corruption"); + } } /* ring buffer test */ { XXH64_state_t xxhOrig; @@ -1167,7 +1187,7 @@ static void FUZ_unitTests(int compressionLevel) LZ4_setStreamDecode(&decodeStateFast, NULL, 0); while (iNext + messageSize < testCompressedSize) { - int compressedSize; + int compressedSize; U64 crcOrig; XXH64_update(&xxhOrig, testInput + iNext, messageSize); crcOrig = XXH64_digest(&xxhOrig); @@ -1175,15 +1195,15 @@ static void FUZ_unitTests(int compressionLevel) compressedSize = LZ4_compress_fast_continue(&streamingState, ringBuffer + rNext, testCompressed, (int)messageSize, testCompressedSize-ringBufferSize, 1); FUZ_CHECKTEST(compressedSize==0, "LZ4_compress_fast_continue() compression failed"); - result = LZ4_decompress_safe_continue(&decodeStateSafe, testCompressed, testVerify + dNext, compressedSize, (int)messageSize); - FUZ_CHECKTEST(result!=(int)messageSize, "ringBuffer : LZ4_decompress_safe_continue() test failed"); + { int const r = LZ4_decompress_safe_continue(&decodeStateSafe, testCompressed, testVerify + dNext, compressedSize, (int)messageSize); + FUZ_CHECKTEST(r!=(int)messageSize, "ringBuffer : LZ4_decompress_safe_continue() test failed"); } XXH64_update(&xxhNewSafe, testVerify + dNext, messageSize); { U64 const crcNew = XXH64_digest(&xxhNewSafe); FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe_continue() decompression corruption"); } - result = LZ4_decompress_fast_continue(&decodeStateFast, testCompressed, testVerify + dNext, (int)messageSize); - FUZ_CHECKTEST(result!=compressedSize, "ringBuffer : LZ4_decompress_fast_continue() test failed"); + { int const r = LZ4_decompress_fast_continue(&decodeStateFast, testCompressed, testVerify + dNext, (int)messageSize); + FUZ_CHECKTEST(r!=compressedSize, "ringBuffer : LZ4_decompress_fast_continue() test failed"); } XXH64_update(&xxhNewFast, testVerify + dNext, messageSize); { U64 const crcNew = XXH64_digest(&xxhNewFast); @@ -1196,16 +1216,14 @@ static void FUZ_unitTests(int compressionLevel) messageSize = (FUZ_rand(&randState) & maxMessageSizeMask) + 1; if (rNext + messageSize > ringBufferSize) rNext = 0; if (dNext + messageSize > dBufferSize) dNext = 0; - } - } + } } } DISPLAYLEVEL(3, "LZ4_initStreamHC with multiple valid alignments : "); { typedef struct { LZ4_streamHC_t hc1; - char c1; LZ4_streamHC_t hc2; - char c2; + char c; LZ4_streamHC_t hc3; } shct; shct* const shc = (shct*)malloc(sizeof(*shc)); @@ -1220,7 +1238,7 @@ static void FUZ_unitTests(int compressionLevel) "hc1+1 (%p) init must fail, due to bad alignment", (char*)&(shc->hc1) + 1 ); free(shc); } - DISPLAYLEVEL(3, "OK \n"); + DISPLAYLEVEL(3, "all inits OK \n"); /* LZ4 HC streaming tests */ { LZ4_streamHC_t sHC; /* statically allocated */ -- cgit v1.2.3 From 78433070abd9da36f225a5be1c302e42cdad67bd Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 6 Nov 2020 14:48:43 -0800 Subject: document LZ4_ALIGN_TEST --- lib/README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/README.md b/lib/README.md index 3653c81..7a9e4fd 100644 --- a/lib/README.md +++ b/lib/README.md @@ -77,6 +77,9 @@ The following build macro can be selected to adjust source code behavior at comp In most cases, it's not expected to be necessary, but it can be legitimately considered for less common platforms. +- `LZ4_ALIGN_TEST` : disable state alignment test when set to 0. + Is generally enabled by default, except on win32+visual. + #### Amalgamation -- cgit v1.2.3 From 0c56f838edfb1eb06553985a141e5243f740ef27 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 6 Nov 2020 16:15:51 -0800 Subject: unified internal state declaration align on `void*` instead : there is no `long long` inside the structure --- lib/lz4.h | 60 ++++++++++++++++++++++++++---------------------------------- lib/lz4hc.h | 55 +++++++++++++++---------------------------------------- 2 files changed, 41 insertions(+), 74 deletions(-) diff --git a/lib/lz4.h b/lib/lz4.h index b11275e..78c2542 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -573,68 +573,60 @@ LZ4LIB_STATIC_API void LZ4_attach_dictionary(LZ4_stream_t* workingStream, const ************************************************************** * Do not use these definitions directly. * They are only exposed to allow static allocation of `LZ4_stream_t` and `LZ4_streamDecode_t`. - * Accessing members will expose code to API and/or ABI break in future versions of the library. + * Accessing members will expose user code to API and/or ABI break in future versions of the library. **************************************************************/ #define LZ4_HASHLOG (LZ4_MEMORY_USAGE-2) #define LZ4_HASHTABLESIZE (1 << LZ4_MEMORY_USAGE) #define LZ4_HASH_SIZE_U32 (1 << LZ4_HASHLOG) /* required as macro for static allocation */ #if defined(__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) -#include - -typedef struct LZ4_stream_t_internal LZ4_stream_t_internal; -struct LZ4_stream_t_internal { - uint32_t hashTable[LZ4_HASH_SIZE_U32]; - uint32_t currentOffset; - uint32_t tableType; - const uint8_t* dictionary; - const LZ4_stream_t_internal* dictCtx; - uint32_t dictSize; -}; - -typedef struct { - const uint8_t* externalDict; - size_t extDictSize; - const uint8_t* prefixEnd; - size_t prefixSize; -} LZ4_streamDecode_t_internal; - +# include + typedef int8_t LZ4_i8; + typedef uint8_t LZ4_byte; + typedef uint16_t LZ4_u16; + typedef uint32_t LZ4_u32; #else + typedef signed char LZ4_i8; + typedef unsigned char LZ4_byte; + typedef unsigned short LZ4_u16; + typedef unsigned int LZ4_u32; +#endif typedef struct LZ4_stream_t_internal LZ4_stream_t_internal; struct LZ4_stream_t_internal { - unsigned int hashTable[LZ4_HASH_SIZE_U32]; - unsigned int currentOffset; - unsigned int tableType; - const unsigned char* dictionary; + LZ4_u32 hashTable[LZ4_HASH_SIZE_U32]; + LZ4_u32 currentOffset; + LZ4_u32 tableType; + const LZ4_byte* dictionary; const LZ4_stream_t_internal* dictCtx; - unsigned int dictSize; + LZ4_u32 dictSize; }; typedef struct { - const unsigned char* externalDict; - const unsigned char* prefixEnd; + const LZ4_byte* externalDict; size_t extDictSize; + const LZ4_byte* prefixEnd; size_t prefixSize; } LZ4_streamDecode_t_internal; -#endif /*! LZ4_stream_t : - * information structure to track an LZ4 stream. + * Do not use below internal definitions directly ! + * Declare or allocate an LZ4_stream_t instead. * LZ4_stream_t can also be created using LZ4_createStream(), which is recommended. * The structure definition can be convenient for static allocation * (on stack, or as part of larger structure). * Init this structure with LZ4_initStream() before first use. * note : only use this definition in association with static linking ! - * this definition is not API/ABI safe, and may change in a future version. + * this definition is not API/ABI safe, and may change in future versions. */ -#define LZ4_STREAMSIZE_U64 ((1 << (LZ4_MEMORY_USAGE-3)) + 4 + ((sizeof(void*)==16) ? 4 : 0) /*AS-400*/ ) -#define LZ4_STREAMSIZE (LZ4_STREAMSIZE_U64 * sizeof(unsigned long long)) +#define LZ4_STREAMSIZE_VOIDP ((sizeof(LZ4_stream_t_internal) + sizeof(void*)-1) / sizeof(void*)) +#define LZ4_STREAMSIZE (LZ4_STREAMSIZE_VOIDP * sizeof(void*)) union LZ4_stream_u { - unsigned long long table[LZ4_STREAMSIZE_U64]; + void* table[LZ4_STREAMSIZE_VOIDP]; LZ4_stream_t_internal internal_donotuse; -} ; /* previously typedef'd to LZ4_stream_t */ +}; /* previously typedef'd to LZ4_stream_t */ + /*! LZ4_initStream() : v1.9.0+ * An LZ4_stream_t structure must be initialized at least once. diff --git a/lib/lz4hc.h b/lib/lz4hc.h index 44e35bb..228bb11 100644 --- a/lib/lz4hc.h +++ b/lib/lz4hc.h @@ -198,57 +198,32 @@ LZ4LIB_API int LZ4_saveDictHC (LZ4_streamHC_t* streamHCPtr, char* safeBuffer, in #define LZ4HC_HASH_MASK (LZ4HC_HASHTABLESIZE - 1) -#if defined(__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) -#include - -typedef struct LZ4HC_CCtx_internal LZ4HC_CCtx_internal; -struct LZ4HC_CCtx_internal -{ - uint32_t hashTable[LZ4HC_HASHTABLESIZE]; - uint16_t chainTable[LZ4HC_MAXD]; - const uint8_t* end; /* next block here to continue on current prefix */ - const uint8_t* base; /* All index relative to this position */ - const uint8_t* dictBase; /* alternate base for extDict */ - uint32_t dictLimit; /* below that point, need extDict */ - uint32_t lowLimit; /* below that point, no more dict */ - uint32_t nextToUpdate; /* index from which to continue dictionary update */ - short compressionLevel; - int8_t favorDecSpeed; /* favor decompression speed if this flag set, - otherwise, favor compression ratio */ - int8_t dirty; /* stream has to be fully reset if this flag is set */ - const LZ4HC_CCtx_internal* dictCtx; -}; - -#else - typedef struct LZ4HC_CCtx_internal LZ4HC_CCtx_internal; struct LZ4HC_CCtx_internal { - unsigned int hashTable[LZ4HC_HASHTABLESIZE]; - unsigned short chainTable[LZ4HC_MAXD]; - const unsigned char* end; /* next block here to continue on current prefix */ - const unsigned char* base; /* All index relative to this position */ - const unsigned char* dictBase; /* alternate base for extDict */ - unsigned int dictLimit; /* below that point, need extDict */ - unsigned int lowLimit; /* below that point, no more dict */ - unsigned int nextToUpdate; /* index from which to continue dictionary update */ - short compressionLevel; - char favorDecSpeed; /* favor decompression speed if this flag set, - otherwise, favor compression ratio */ - char dirty; /* stream has to be fully reset if this flag is set */ + LZ4_u32 hashTable[LZ4HC_HASHTABLESIZE]; + LZ4_u16 chainTable[LZ4HC_MAXD]; + const LZ4_byte* end; /* next block here to continue on current prefix */ + const LZ4_byte* base; /* All index relative to this position */ + const LZ4_byte* dictBase; /* alternate base for extDict */ + LZ4_u32 dictLimit; /* below that point, need extDict */ + LZ4_u32 lowLimit; /* below that point, no more dict */ + LZ4_u32 nextToUpdate; /* index from which to continue dictionary update */ + short compressionLevel; + LZ4_i8 favorDecSpeed; /* favor decompression speed if this flag set, + otherwise, favor compression ratio */ + LZ4_i8 dirty; /* stream has to be fully reset if this flag is set */ const LZ4HC_CCtx_internal* dictCtx; }; -#endif - /* Do not use these definitions directly ! * Declare or allocate an LZ4_streamHC_t instead. */ -#define LZ4_STREAMHCSIZE (4*LZ4HC_HASHTABLESIZE + 2*LZ4HC_MAXD + 56 + ((sizeof(void*)==16) ? 56 : 0) /* AS400*/ ) /* 262200 or 262256*/ -#define LZ4_STREAMHCSIZE_SIZET (LZ4_STREAMHCSIZE / sizeof(size_t)) +#define LZ4_STREAMHCSIZE_VOIDP ((sizeof(LZ4HC_CCtx_internal) + sizeof(void*)-1) / sizeof(void*)) +#define LZ4_STREAMHCSIZE (LZ4_STREAMHCSIZE_VOIDP * sizeof(void*)) union LZ4_streamHC_u { - size_t table[LZ4_STREAMHCSIZE_SIZET]; + void* table[LZ4_STREAMHCSIZE_VOIDP]; LZ4HC_CCtx_internal internal_donotuse; }; /* previously typedef'd to LZ4_streamHC_t */ -- cgit v1.2.3 From 211d653ff866ee98a51ab135067369f52f6bdc7b Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 6 Nov 2020 16:43:14 -0800 Subject: re-enable alignment test on all targets --- lib/README.md | 5 +++-- lib/lz4.c | 20 ++++++-------------- lib/lz4.h | 2 +- 3 files changed, 10 insertions(+), 17 deletions(-) diff --git a/lib/README.md b/lib/README.md index 7a9e4fd..ee5d3ba 100644 --- a/lib/README.md +++ b/lib/README.md @@ -77,8 +77,9 @@ The following build macro can be selected to adjust source code behavior at comp In most cases, it's not expected to be necessary, but it can be legitimately considered for less common platforms. -- `LZ4_ALIGN_TEST` : disable state alignment test when set to 0. - Is generally enabled by default, except on win32+visual. +- `LZ4_ALIGN_TEST` : alignment test ensures that the memory area + passed as argument to become a compression state is suitable aligned. + This test can be disabled, if it proves flaky, by setting this value to 0. #### Amalgamation diff --git a/lib/lz4.c b/lib/lz4.c index 9e6abba..c902654 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -178,18 +178,10 @@ #define unlikely(expr) expect((expr) != 0, 0) #endif -/* for some reason, Visual Studio can fail the aligment test on 32-bit x86 : - * it sometimes report an aligment of 8-bytes (at least in some configurations), - * while only providing a `malloc()` memory area aligned on 4-bytes, - * which is inconsistent with malloc() contract. - * The source of the issue is still unclear. - * Mitigation : made the alignment test optional */ +/* Should the alignment test prove unreliable, for some reason, + * it can be disabled by setting LZ4_ALIGN_TEST to 0 */ #ifndef LZ4_ALIGN_TEST /* can be externally provided */ -# if (defined(_MSC_VER) && !defined(_M_X64)) -# define LZ4_ALIGN_TEST 0 /* disable on win32+visual */ -# else -# define LZ4_ALIGN_TEST 1 -# endif +# define LZ4_ALIGN_TEST 1 #endif @@ -476,7 +468,7 @@ LZ4_memcpy_using_offset(BYTE* dstPtr, const BYTE* srcPtr, BYTE* dstEnd, const si switch(offset) { case 1: - memset(v, *srcPtr, 8); + MEM_INIT(v, *srcPtr, 8); break; case 2: LZ4_memcpy(v, srcPtr, 2); @@ -1441,7 +1433,7 @@ LZ4_stream_t* LZ4_initStream (void* buffer, size_t size) if (buffer == NULL) { return NULL; } if (size < sizeof(LZ4_stream_t)) { return NULL; } if (!LZ4_isAligned(buffer, LZ4_stream_t_alignment())) return NULL; - MEM_INIT(buffer, 0, sizeof(LZ4_stream_t)); + MEM_INIT(buffer, 0, sizeof(LZ4_stream_t_internal)); return (LZ4_stream_t*)buffer; } @@ -1450,7 +1442,7 @@ LZ4_stream_t* LZ4_initStream (void* buffer, size_t size) void LZ4_resetStream (LZ4_stream_t* LZ4_stream) { DEBUGLOG(5, "LZ4_resetStream (ctx:%p)", LZ4_stream); - MEM_INIT(LZ4_stream, 0, sizeof(LZ4_stream_t)); + MEM_INIT(LZ4_stream, 0, sizeof(LZ4_stream_t_internal)); } void LZ4_resetStream_fast(LZ4_stream_t* ctx) { diff --git a/lib/lz4.h b/lib/lz4.h index 78c2542..026279b 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -618,7 +618,7 @@ typedef struct { * (on stack, or as part of larger structure). * Init this structure with LZ4_initStream() before first use. * note : only use this definition in association with static linking ! - * this definition is not API/ABI safe, and may change in future versions. + * this definition is not API/ABI safe, and may change in future versions. */ #define LZ4_STREAMSIZE_VOIDP ((sizeof(LZ4_stream_t_internal) + sizeof(void*)-1) / sizeof(void*)) #define LZ4_STREAMSIZE (LZ4_STREAMSIZE_VOIDP * sizeof(void*)) -- cgit v1.2.3 From 67e661a2ad39258bcb5c6fcc31c9239ff3aabc71 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 6 Nov 2020 18:26:13 -0800 Subject: static state size for better inter-version compatibility --- lib/lz4.h | 4 ++-- lib/lz4hc.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/lz4.h b/lib/lz4.h index 026279b..c4324af 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -620,8 +620,8 @@ typedef struct { * note : only use this definition in association with static linking ! * this definition is not API/ABI safe, and may change in future versions. */ -#define LZ4_STREAMSIZE_VOIDP ((sizeof(LZ4_stream_t_internal) + sizeof(void*)-1) / sizeof(void*)) -#define LZ4_STREAMSIZE (LZ4_STREAMSIZE_VOIDP * sizeof(void*)) +#define LZ4_STREAMSIZE 16416 /* static size, for inter-version compatibility */ +#define LZ4_STREAMSIZE_VOIDP (LZ4_STREAMSIZE / sizeof(void*)) union LZ4_stream_u { void* table[LZ4_STREAMSIZE_VOIDP]; LZ4_stream_t_internal internal_donotuse; diff --git a/lib/lz4hc.h b/lib/lz4hc.h index 228bb11..3d441fb 100644 --- a/lib/lz4hc.h +++ b/lib/lz4hc.h @@ -220,8 +220,8 @@ struct LZ4HC_CCtx_internal /* Do not use these definitions directly ! * Declare or allocate an LZ4_streamHC_t instead. */ -#define LZ4_STREAMHCSIZE_VOIDP ((sizeof(LZ4HC_CCtx_internal) + sizeof(void*)-1) / sizeof(void*)) -#define LZ4_STREAMHCSIZE (LZ4_STREAMHCSIZE_VOIDP * sizeof(void*)) +#define LZ4_STREAMHCSIZE 262200 /* static size, for inter-version compatibility */ +#define LZ4_STREAMHCSIZE_VOIDP (LZ4_STREAMHCSIZE / sizeof(void*)) union LZ4_streamHC_u { void* table[LZ4_STREAMHCSIZE_VOIDP]; LZ4HC_CCtx_internal internal_donotuse; -- cgit v1.2.3 From 27959b43ec4d4afe1e9d7654258eacf5e29ecf2b Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 6 Nov 2020 20:46:35 -0800 Subject: LZ4F_decompress requires a valid dctx state This is now explicitly documented and asserted. fix #927 --- lib/README.md | 10 +++++----- lib/lz4frame.c | 1 + lib/lz4frame.h | 6 ++++-- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/README.md b/lib/README.md index ee5d3ba..318106d 100644 --- a/lib/README.md +++ b/lib/README.md @@ -49,7 +49,7 @@ and `LZ4F_PUBLISH_STATIC_FUNCTIONS`. The following build macro can be selected to adjust source code behavior at compilation time : - `LZ4_FAST_DEC_LOOP` : this triggers a speed optimized decompression loop, more powerful on modern cpus. - This loop works great on x86, x64 and aarch64 cpus, and is automatically enabled for them. + This loop works great on `x86`, `x64` and `aarch64` cpus, and is automatically enabled for them. It's also possible to enable or disable it manually, by passing `LZ4_FAST_DEC_LOOP=1` or `0` to the preprocessor. For example, with `gcc` : `-DLZ4_FAST_DEC_LOOP=1`, and with `make` : `CPPFLAGS+=-DLZ4_FAST_DEC_LOOP=1 make lz4`. @@ -66,8 +66,8 @@ The following build macro can be selected to adjust source code behavior at comp Should this be a problem, it's generally possible to make the compiler ignore these warnings, for example with `-Wno-deprecated-declarations` on `gcc`, or `_CRT_SECURE_NO_WARNINGS` for Visual Studio. - Another project-specific method is to define `LZ4_DISABLE_DEPRECATE_WARNINGS` - before including the LZ4 header files. + This build macro offers another project-specific method + by defining `LZ4_DISABLE_DEPRECATE_WARNINGS` before including the LZ4 header files. - `LZ4_FORCE_SW_BITCOUNT` : by default, the compression algorithm tries to determine lengths by using bitcount instructions, generally implemented as fast single instructions in many cpus. @@ -78,8 +78,8 @@ The following build macro can be selected to adjust source code behavior at comp but it can be legitimately considered for less common platforms. - `LZ4_ALIGN_TEST` : alignment test ensures that the memory area - passed as argument to become a compression state is suitable aligned. - This test can be disabled, if it proves flaky, by setting this value to 0. + passed as argument to become a compression state is suitably aligned. + This test can be disabled if it proves flaky, by setting this value to 0. #### Amalgamation diff --git a/lib/lz4frame.c b/lib/lz4frame.c index 2976bb1..e02b76c 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -1405,6 +1405,7 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, if (decompressOptionsPtr==NULL) decompressOptionsPtr = &optionsNull; *srcSizePtr = 0; *dstSizePtr = 0; + assert(dctx != NULL); /* behaves as a state machine */ diff --git a/lib/lz4frame.h b/lib/lz4frame.h index c669aec..6e91e2d 100644 --- a/lib/lz4frame.h +++ b/lib/lz4frame.h @@ -431,8 +431,10 @@ LZ4FLIB_API size_t LZ4F_getFrameInfo(LZ4F_dctx* dctx, const void* srcBuffer, size_t* srcSizePtr); /*! LZ4F_decompress() : - * Call this function repetitively to regenerate compressed data from `srcBuffer`. - * The function will read up to *srcSizePtr bytes from srcBuffer, + * Call this function repetitively to regenerate data compressed in `srcBuffer`. + * + * The function requires a valid dctx state. + * It will read up to *srcSizePtr bytes from srcBuffer, * and decompress data into dstBuffer, of capacity *dstSizePtr. * * The nb of bytes consumed from srcBuffer will be written into *srcSizePtr (necessarily <= original value). -- cgit v1.2.3 From d4bfcf8489becbba084bceb0ddc74dc7c945cdde Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sat, 7 Nov 2020 10:06:52 -0800 Subject: fix #935 minor: identical declaration and prototypes of `LZ4HC_compress_optimal()` also : very minor optimization of `LZ4_memcpy_using_offset()` --- lib/lz4.c | 5 +++-- lib/lz4hc.c | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 9d547e5..e51fbd7 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -424,7 +424,9 @@ static const int dec64table[8] = {0, 0, 0, -1, -4, 1, 2, 3}; LZ4_FORCE_INLINE void LZ4_memcpy_using_offset_base(BYTE* dstPtr, const BYTE* srcPtr, BYTE* dstEnd, const size_t offset) { + assert(srcPtr + offset == dstPtr); if (offset < 8) { + LZ4_write32(dstPtr, 0); /* silence an msan warning when offset==0 */ dstPtr[0] = srcPtr[0]; dstPtr[1] = srcPtr[1]; dstPtr[2] = srcPtr[2]; @@ -464,7 +466,6 @@ LZ4_memcpy_using_offset(BYTE* dstPtr, const BYTE* srcPtr, BYTE* dstEnd, const si BYTE v[8]; assert(dstEnd >= dstPtr + MINMATCH); - LZ4_write32(dstPtr, 0); /* silence an msan warning when offset==0 */ switch(offset) { case 1: @@ -473,7 +474,7 @@ LZ4_memcpy_using_offset(BYTE* dstPtr, const BYTE* srcPtr, BYTE* dstEnd, const si case 2: LZ4_memcpy(v, srcPtr, 2); LZ4_memcpy(&v[2], srcPtr, 2); - LZ4_memcpy(&v[4], &v[0], 4); + LZ4_memcpy(&v[4], v, 4); break; case 4: LZ4_memcpy(v, srcPtr, 4); diff --git a/lib/lz4hc.c b/lib/lz4hc.c index 15bedec..85121d4 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -789,7 +789,7 @@ static int LZ4HC_compress_optimal( LZ4HC_CCtx_internal* ctx, int const nbSearches, size_t sufficient_len, const limitedOutput_directive limit, int const fullUpdate, const dictCtx_directive dict, - HCfavor_e favorDecSpeed); + const HCfavor_e favorDecSpeed); LZ4_FORCE_INLINE int LZ4HC_compress_generic_internal ( -- cgit v1.2.3 From f61eeb7793cd86a1ec80b68b5cd962b108c0489a Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sat, 7 Nov 2020 11:02:30 -0800 Subject: Revert "Replace "static" to "LZ4_FORCE_INLINE" for small functions" This reverts commit 0e3933edd435c54cc2e21e38f5d4ba7bf644a24e. --- lib/lz4.c | 36 ++++++++++++++++++------------------ lib/lz4hc.c | 4 ++-- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 9d547e5..84f6125 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -310,7 +310,7 @@ typedef enum { #define LZ4_memcpy(dst, src, size) memcpy(dst, src, size) #endif -LZ4_FORCE_INLINE unsigned LZ4_isLittleEndian(void) +static unsigned LZ4_isLittleEndian(void) { const union { U32 u; BYTE c[4]; } one = { 1 }; /* don't use static : performance detrimental */ return one.c[0]; @@ -320,12 +320,12 @@ LZ4_FORCE_INLINE unsigned LZ4_isLittleEndian(void) #if defined(LZ4_FORCE_MEMORY_ACCESS) && (LZ4_FORCE_MEMORY_ACCESS==2) /* lie to the compiler about data alignment; use with caution */ -LZ4_FORCE_INLINE U16 LZ4_read16(const void* memPtr) { return *(const U16*) memPtr; } -LZ4_FORCE_INLINE U32 LZ4_read32(const void* memPtr) { return *(const U32*) memPtr; } -LZ4_FORCE_INLINE reg_t LZ4_read_ARCH(const void* memPtr) { return *(const reg_t*) memPtr; } +static U16 LZ4_read16(const void* memPtr) { return *(const U16*) memPtr; } +static U32 LZ4_read32(const void* memPtr) { return *(const U32*) memPtr; } +static reg_t LZ4_read_ARCH(const void* memPtr) { return *(const reg_t*) memPtr; } -LZ4_FORCE_INLINE void LZ4_write16(void* memPtr, U16 value) { *(U16*)memPtr = value; } -LZ4_FORCE_INLINE void LZ4_write32(void* memPtr, U32 value) { *(U32*)memPtr = value; } +static void LZ4_write16(void* memPtr, U16 value) { *(U16*)memPtr = value; } +static void LZ4_write32(void* memPtr, U32 value) { *(U32*)memPtr = value; } #elif defined(LZ4_FORCE_MEMORY_ACCESS) && (LZ4_FORCE_MEMORY_ACCESS==1) @@ -333,36 +333,36 @@ LZ4_FORCE_INLINE void LZ4_write32(void* memPtr, U32 value) { *(U32*)memPtr = val /* currently only defined for gcc and icc */ typedef union { U16 u16; U32 u32; reg_t uArch; } __attribute__((packed)) unalign; -LZ4_FORCE_INLINE U16 LZ4_read16(const void* ptr) { return ((const unalign*)ptr)->u16; } -LZ4_FORCE_INLINE U32 LZ4_read32(const void* ptr) { return ((const unalign*)ptr)->u32; } -LZ4_FORCE_INLINE reg_t LZ4_read_ARCH(const void* ptr) { return ((const unalign*)ptr)->uArch; } +static U16 LZ4_read16(const void* ptr) { return ((const unalign*)ptr)->u16; } +static U32 LZ4_read32(const void* ptr) { return ((const unalign*)ptr)->u32; } +static reg_t LZ4_read_ARCH(const void* ptr) { return ((const unalign*)ptr)->uArch; } -LZ4_FORCE_INLINE void LZ4_write16(void* memPtr, U16 value) { ((unalign*)memPtr)->u16 = value; } -LZ4_FORCE_INLINE void LZ4_write32(void* memPtr, U32 value) { ((unalign*)memPtr)->u32 = value; } +static void LZ4_write16(void* memPtr, U16 value) { ((unalign*)memPtr)->u16 = value; } +static void LZ4_write32(void* memPtr, U32 value) { ((unalign*)memPtr)->u32 = value; } #else /* safe and portable access using memcpy() */ -LZ4_FORCE_INLINE U16 LZ4_read16(const void* memPtr) +static U16 LZ4_read16(const void* memPtr) { U16 val; LZ4_memcpy(&val, memPtr, sizeof(val)); return val; } -LZ4_FORCE_INLINE U32 LZ4_read32(const void* memPtr) +static U32 LZ4_read32(const void* memPtr) { U32 val; LZ4_memcpy(&val, memPtr, sizeof(val)); return val; } -LZ4_FORCE_INLINE reg_t LZ4_read_ARCH(const void* memPtr) +static reg_t LZ4_read_ARCH(const void* memPtr) { reg_t val; LZ4_memcpy(&val, memPtr, sizeof(val)); return val; } -LZ4_FORCE_INLINE void LZ4_write16(void* memPtr, U16 value) +static void LZ4_write16(void* memPtr, U16 value) { LZ4_memcpy(memPtr, &value, sizeof(value)); } -LZ4_FORCE_INLINE void LZ4_write32(void* memPtr, U32 value) +static void LZ4_write32(void* memPtr, U32 value) { LZ4_memcpy(memPtr, &value, sizeof(value)); } @@ -370,7 +370,7 @@ LZ4_FORCE_INLINE void LZ4_write32(void* memPtr, U32 value) #endif /* LZ4_FORCE_MEMORY_ACCESS */ -LZ4_FORCE_INLINE U16 LZ4_readLE16(const void* memPtr) +static U16 LZ4_readLE16(const void* memPtr) { if (LZ4_isLittleEndian()) { return LZ4_read16(memPtr); @@ -380,7 +380,7 @@ LZ4_FORCE_INLINE U16 LZ4_readLE16(const void* memPtr) } } -LZ4_FORCE_INLINE void LZ4_writeLE16(void* memPtr, U16 value) +static void LZ4_writeLE16(void* memPtr, U16 value) { if (LZ4_isLittleEndian()) { LZ4_write16(memPtr, value); diff --git a/lib/lz4hc.c b/lib/lz4hc.c index 15bedec..0c89e42 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -159,7 +159,7 @@ int LZ4HC_countBack(const BYTE* const ip, const BYTE* const match, #endif -LZ4_FORCE_INLINE U32 LZ4HC_rotatePattern(size_t const rotate, U32 const pattern) +static U32 LZ4HC_rotatePattern(size_t const rotate, U32 const pattern) { size_t const bitsToRotate = (rotate & (sizeof(pattern) - 1)) << 3; if (bitsToRotate == 0) return pattern; @@ -223,7 +223,7 @@ LZ4HC_reverseCountPattern(const BYTE* ip, const BYTE* const iLow, U32 pattern) * 4 byte MINMATCH would overflow. * @returns true if the match index is okay. */ -LZ4_FORCE_INLINE int LZ4HC_protectDictEnd(U32 const dictLimit, U32 const matchIndex) +static int LZ4HC_protectDictEnd(U32 const dictLimit, U32 const matchIndex) { return ((U32)((dictLimit - 1) - matchIndex) >= 3); } -- cgit v1.2.3 From a2222a879fe77c33c96b5f9954a0328dc6df9850 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sat, 7 Nov 2020 18:12:26 -0800 Subject: fix #926 fix incorrect behavior of LZ4_saveDictHC() when invoked right after initialization. --- lib/lz4hc.c | 38 ++++++++++++++++++++------------------ tests/fuzzer.c | 20 ++++++++++++++++++-- 2 files changed, 38 insertions(+), 20 deletions(-) diff --git a/lib/lz4hc.c b/lib/lz4hc.c index ef7ded3..a74144a 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -1003,18 +1003,16 @@ int LZ4_freeStreamHC (LZ4_streamHC_t* LZ4_streamHCPtr) LZ4_streamHC_t* LZ4_initStreamHC (void* buffer, size_t size) { LZ4_streamHC_t* const LZ4_streamHCPtr = (LZ4_streamHC_t*)buffer; + LZ4HC_CCtx_internal* const hcstate = &(LZ4_streamHCPtr->internal_donotuse); + /* if compilation fails here, LZ4_STREAMHCSIZE must be increased */ + LZ4_STATIC_ASSERT(sizeof(LZ4HC_CCtx_internal) <= LZ4_STREAMHCSIZE); + DEBUGLOG(4, "LZ4_initStreamHC(%p, %u)", buffer, (unsigned)size); + /* check conditions */ if (buffer == NULL) return NULL; if (size < sizeof(LZ4_streamHC_t)) return NULL; if (!LZ4_isAligned(buffer, LZ4_streamHC_t_alignment())) return NULL; - /* if compilation fails here, LZ4_STREAMHCSIZE must be increased */ - LZ4_STATIC_ASSERT(sizeof(LZ4HC_CCtx_internal) <= LZ4_STREAMHCSIZE); - DEBUGLOG(4, "LZ4_initStreamHC(%p, %u)", LZ4_streamHCPtr, (unsigned)size); - /* end-base will trigger a clearTable on starting compression */ - LZ4_streamHCPtr->internal_donotuse.end = (const BYTE *)(ptrdiff_t)-1; - LZ4_streamHCPtr->internal_donotuse.base = NULL; - LZ4_streamHCPtr->internal_donotuse.dictCtx = NULL; - LZ4_streamHCPtr->internal_donotuse.favorDecSpeed = 0; - LZ4_streamHCPtr->internal_donotuse.dirty = 0; + /* init */ + MEM_INIT(hcstate, 0, sizeof(*hcstate)); LZ4_setCompressionLevel(LZ4_streamHCPtr, LZ4HC_CLEVEL_DEFAULT); return LZ4_streamHCPtr; } @@ -1100,10 +1098,11 @@ static void LZ4HC_setExternalDict(LZ4HC_CCtx_internal* ctxPtr, const BYTE* newBl ctxPtr->dictCtx = NULL; } -static int LZ4_compressHC_continue_generic (LZ4_streamHC_t* LZ4_streamHCPtr, - const char* src, char* dst, - int* srcSizePtr, int dstCapacity, - limitedOutput_directive limit) +static int +LZ4_compressHC_continue_generic (LZ4_streamHC_t* LZ4_streamHCPtr, + const char* src, char* dst, + int* srcSizePtr, int dstCapacity, + limitedOutput_directive limit) { LZ4HC_CCtx_internal* const ctxPtr = &LZ4_streamHCPtr->internal_donotuse; DEBUGLOG(5, "LZ4_compressHC_continue_generic(ctx=%p, src=%p, srcSize=%d, limit=%d)", @@ -1131,8 +1130,7 @@ static int LZ4_compressHC_continue_generic (LZ4_streamHC_t* LZ4_streamHCPtr, if (sourceEnd > dictEnd) sourceEnd = dictEnd; ctxPtr->lowLimit = (U32)(sourceEnd - ctxPtr->dictBase); if (ctxPtr->dictLimit - ctxPtr->lowLimit < 4) ctxPtr->lowLimit = ctxPtr->dictLimit; - } - } + } } return LZ4HC_compress_generic (ctxPtr, src, dst, srcSizePtr, dstCapacity, ctxPtr->compressionLevel, limit); } @@ -1152,13 +1150,17 @@ int LZ4_compress_HC_continue_destSize (LZ4_streamHC_t* LZ4_streamHCPtr, const ch -/* dictionary saving */ - +/* LZ4_saveDictHC : + * save history content + * into a user-provided buffer + * which is then used to continue compression + */ int LZ4_saveDictHC (LZ4_streamHC_t* LZ4_streamHCPtr, char* safeBuffer, int dictSize) { LZ4HC_CCtx_internal* const streamPtr = &LZ4_streamHCPtr->internal_donotuse; int const prefixSize = (int)(streamPtr->end - (streamPtr->base + streamPtr->dictLimit)); - DEBUGLOG(4, "LZ4_saveDictHC(%p, %p, %d)", LZ4_streamHCPtr, safeBuffer, dictSize); + DEBUGLOG(5, "LZ4_saveDictHC(%p, %p, %d)", LZ4_streamHCPtr, safeBuffer, dictSize); + assert(prefixSize >= 0); if (dictSize > 64 KB) dictSize = 64 KB; if (dictSize < 4) dictSize = 0; if (dictSize > prefixSize) dictSize = prefixSize; diff --git a/tests/fuzzer.c b/tests/fuzzer.c index 29c6a8a..a411dd4 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -1135,13 +1135,13 @@ static void FUZ_unitTests(int compressionLevel) shct* const shc = (shct*)malloc(sizeof(*shc)); assert(shc != NULL); memset(shc, 0, sizeof(*shc)); - DISPLAYLEVEL(3, "state1(%p) state2(%p) state3(%p) size(0x%x): ", + DISPLAYLEVEL(3, "state1(%p) state2(%p) state3(%p) LZ4_stream_t size(0x%x): ", &(shc->state1), &(shc->state2), &(shc->state3), (unsigned)sizeof(LZ4_stream_t)); FUZ_CHECKTEST( LZ4_initStream(&(shc->state1), sizeof(shc->state1)) == NULL, "state1 (%p) failed init", &(shc->state1) ); FUZ_CHECKTEST( LZ4_initStream(&(shc->state2), sizeof(shc->state2)) == NULL, "state2 (%p) failed init", &(shc->state2) ); FUZ_CHECKTEST( LZ4_initStream(&(shc->state3), sizeof(shc->state3)) == NULL, "state3 (%p) failed init", &(shc->state3) ); FUZ_CHECKTEST( LZ4_initStream((char*)&(shc->state1) + 1, sizeof(shc->state1)) != NULL, - "hc1+1 (%p) init must fail, due to bad alignment", (char*)&(shc->state1) + 1 ); + "hc1+1 (%p) init must fail, due to bad alignment", (char*)&(shc->state1) + 1 ); free(shc); } DISPLAYLEVEL(3, "all inits OK \n"); @@ -1268,6 +1268,22 @@ static void FUZ_unitTests(int compressionLevel) } } DISPLAYLEVEL(3, "OK \n"); + /* saveDictHC test #926 */ + DISPLAYLEVEL(3, "saveDictHC test #926 : "); + { LZ4_streamHC_t* const ctx = LZ4_initStreamHC(&sHC, sizeof(sHC)); + assert(ctx != NULL); /* ensure init is successful */ + + /* Check access violation with asan */ + FUZ_CHECKTEST( LZ4_saveDictHC(&sHC, NULL, 0) != 0, + "LZ4_saveDictHC() can't save anything into (NULL,0)"); + + /* Check access violation with asan */ + { char tmp_buffer[240] = { 0 }; + FUZ_CHECKTEST( LZ4_saveDictHC(&sHC, tmp_buffer, sizeof(tmp_buffer)) != 0, + "LZ4_saveDictHC() can't save anything since compression hasn't started"); + } } + + /* long sequence test */ DISPLAYLEVEL(3, "Long sequence HC_destSize test : "); { size_t const blockSize = 1 MB; -- cgit v1.2.3 From b16b9fcdbc110876c9cd82ac1c3392abc0c2e1b3 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sat, 7 Nov 2020 18:24:02 -0800 Subject: added similar tests for LZ4_saveDict() fast (non-HC) variant --- tests/fuzzer.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/tests/fuzzer.c b/tests/fuzzer.c index a411dd4..99361dd 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -1167,6 +1167,22 @@ static void FUZ_unitTests(int compressionLevel) FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe() decompression corruption"); } } + /* early saveDict */ + DISPLAYLEVEL(3, "saveDict (right after init) : "); + { LZ4_stream_t* const ctx = LZ4_initStream(&streamingState, sizeof(streamingState)); + assert(ctx != NULL); /* ensure init is successful */ + + /* Check access violation with asan */ + FUZ_CHECKTEST( LZ4_saveDict(&streamingState, NULL, 0) != 0, + "LZ4_saveDict() can't save anything into (NULL,0)"); + + /* Check access violation with asan */ + { char tmp_buffer[240] = { 0 }; + FUZ_CHECKTEST( LZ4_saveDict(&streamingState, tmp_buffer, sizeof(tmp_buffer)) != 0, + "LZ4_saveDict() can't save anything since compression hasn't started"); + } } + DISPLAYLEVEL(3, "OK \n"); + /* ring buffer test */ { XXH64_state_t xxhOrig; XXH64_state_t xxhNewSafe, xxhNewFast; @@ -1282,7 +1298,7 @@ static void FUZ_unitTests(int compressionLevel) FUZ_CHECKTEST( LZ4_saveDictHC(&sHC, tmp_buffer, sizeof(tmp_buffer)) != 0, "LZ4_saveDictHC() can't save anything since compression hasn't started"); } } - + DISPLAYLEVEL(3, "OK \n"); /* long sequence test */ DISPLAYLEVEL(3, "Long sequence HC_destSize test : "); -- cgit v1.2.3 From e251a840253c5564364bf5c8c662a6fe623dc3e9 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sat, 7 Nov 2020 19:38:07 -0800 Subject: fix minor UBs - check alignment before casting a pointer - saveDict : don't memmove() on NULL dst --- lib/lz4.c | 4 +++- lib/lz4hc.c | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index c41a084..427673e 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1661,7 +1661,9 @@ 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 = (int)dict->dictSize; } - memmove(safeBuffer, previousDictEnd - dictSize, dictSize); + if (safeBuffer == NULL) assert(dictSize == 0); + if (safeBuffer != NULL) + memmove(safeBuffer, previousDictEnd - dictSize, dictSize); dict->dictionary = (const BYTE*)safeBuffer; dict->dictSize = (U32)dictSize; diff --git a/lib/lz4hc.c b/lib/lz4hc.c index a74144a..8875f1a 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -1003,7 +1003,6 @@ int LZ4_freeStreamHC (LZ4_streamHC_t* LZ4_streamHCPtr) LZ4_streamHC_t* LZ4_initStreamHC (void* buffer, size_t size) { LZ4_streamHC_t* const LZ4_streamHCPtr = (LZ4_streamHC_t*)buffer; - LZ4HC_CCtx_internal* const hcstate = &(LZ4_streamHCPtr->internal_donotuse); /* if compilation fails here, LZ4_STREAMHCSIZE must be increased */ LZ4_STATIC_ASSERT(sizeof(LZ4HC_CCtx_internal) <= LZ4_STREAMHCSIZE); DEBUGLOG(4, "LZ4_initStreamHC(%p, %u)", buffer, (unsigned)size); @@ -1012,7 +1011,8 @@ LZ4_streamHC_t* LZ4_initStreamHC (void* buffer, size_t size) if (size < sizeof(LZ4_streamHC_t)) return NULL; if (!LZ4_isAligned(buffer, LZ4_streamHC_t_alignment())) return NULL; /* init */ - MEM_INIT(hcstate, 0, sizeof(*hcstate)); + { LZ4HC_CCtx_internal* const hcstate = &(LZ4_streamHCPtr->internal_donotuse); + MEM_INIT(hcstate, 0, sizeof(*hcstate)); } LZ4_setCompressionLevel(LZ4_streamHCPtr, LZ4HC_CLEVEL_DEFAULT); return LZ4_streamHCPtr; } -- cgit v1.2.3 From be634559e3b6bb7bce77cc83ec2080b2bfb6c844 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sun, 8 Nov 2020 12:54:59 -0800 Subject: update obsolete section documentation and update manuals. fix #712 --- doc/lz4_manual.html | 63 ++++++++++++++++++++++++++++++------------------ doc/lz4frame_manual.html | 6 +++-- lib/lz4.h | 23 +++++++++--------- 3 files changed, 54 insertions(+), 38 deletions(-) diff --git a/doc/lz4_manual.html b/doc/lz4_manual.html index 208dbb9..47fe18d 100644 --- a/doc/lz4_manual.html +++ b/doc/lz4_manual.html @@ -16,7 +16,7 @@

  • Streaming Compression Functions
  • Streaming Decompression Functions
  • Experimental section
  • -
  • PRIVATE DEFINITIONS
  • +
  • Private Definitions
  • Obsolete Functions

  • @@ -446,39 +446,33 @@ int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream);

    #define LZ4_COMPRESS_INPLACE_BUFFER_SIZE(maxCompressedSize)   ((maxCompressedSize) + LZ4_COMPRESS_INPLACE_MARGIN)  /**< maxCompressedSize is generally LZ4_COMPRESSBOUND(inputSize), but can be set to any lower value, with the risk that compression can fail (return code 0(zero)) */
     

    -

    PRIVATE DEFINITIONS

    +

    Private Definitions

      Do not use these definitions directly.
      They are only exposed to allow static allocation of `LZ4_stream_t` and `LZ4_streamDecode_t`.
    - Accessing members will expose code to API and/or ABI break in future versions of the library.
    + Accessing members will expose user code to API and/or ABI break in future versions of the library.
     
    typedef struct {
    -    const uint8_t* externalDict;
    +    const LZ4_byte* externalDict;
         size_t extDictSize;
    -    const uint8_t* prefixEnd;
    +    const LZ4_byte* prefixEnd;
         size_t prefixSize;
     } LZ4_streamDecode_t_internal;
     

    -
    typedef struct {
    -    const unsigned char* externalDict;
    -    const unsigned char* prefixEnd;
    -    size_t extDictSize;
    -    size_t prefixSize;
    -} LZ4_streamDecode_t_internal;
    -

    -
    #define LZ4_STREAMSIZE_U64 ((1 << (LZ4_MEMORY_USAGE-3)) + 4 + ((sizeof(void*)==16) ? 4 : 0) /*AS-400*/ )
    -#define LZ4_STREAMSIZE     (LZ4_STREAMSIZE_U64 * sizeof(unsigned long long))
    +
    #define LZ4_STREAMSIZE       16416  /* static size, for inter-version compatibility */
    +#define LZ4_STREAMSIZE_VOIDP (LZ4_STREAMSIZE / sizeof(void*))
     union LZ4_stream_u {
    -    unsigned long long table[LZ4_STREAMSIZE_U64];
    +    void* table[LZ4_STREAMSIZE_VOIDP];
         LZ4_stream_t_internal internal_donotuse;
    -} ;  /* previously typedef'd to LZ4_stream_t */
    -

    information structure to track an LZ4 stream. +}; /* previously typedef'd to LZ4_stream_t */ +

    Do not use below internal definitions directly ! + Declare or allocate an LZ4_stream_t instead. LZ4_stream_t can also be created using LZ4_createStream(), which is recommended. The structure definition can be convenient for static allocation (on stack, or as part of larger structure). Init this structure with LZ4_initStream() before first use. note : only use this definition in association with static linking ! - this definition is not API/ABI safe, and may change in a future version. + this definition is not API/ABI safe, and may change in future versions.


    @@ -542,18 +536,39 @@ union LZ4_streamDecode_u {


    -

    These functions used to be faster than LZ4_decompress_safe(), - but it has changed, and they are now slower than LZ4_decompress_safe(). +

    LZ4_DEPRECATED("use LZ4_compress_default() instead")       LZ4LIB_API int LZ4_compress               (const char* src, char* dest, int srcSize);
    +LZ4_DEPRECATED("use LZ4_compress_default() instead")       LZ4LIB_API int LZ4_compress_limitedOutput (const char* src, char* dest, int srcSize, int maxOutputSize);
    +LZ4_DEPRECATED("use LZ4_compress_fast_extState() instead") LZ4LIB_API int LZ4_compress_withState               (void* state, const char* source, char* dest, int inputSize);
    +LZ4_DEPRECATED("use LZ4_compress_fast_extState() instead") LZ4LIB_API int LZ4_compress_limitedOutput_withState (void* state, const char* source, char* dest, int inputSize, int maxOutputSize);
    +LZ4_DEPRECATED("use LZ4_compress_fast_continue() instead") LZ4LIB_API int LZ4_compress_continue                (LZ4_stream_t* LZ4_streamPtr, const char* source, char* dest, int inputSize);
    +LZ4_DEPRECATED("use LZ4_compress_fast_continue() instead") LZ4LIB_API int LZ4_compress_limitedOutput_continue  (LZ4_stream_t* LZ4_streamPtr, const char* source, char* dest, int inputSize, int maxOutputSize);
    +


    + +
    LZ4_DEPRECATED("use LZ4_decompress_fast() instead") LZ4LIB_API int LZ4_uncompress (const char* source, char* dest, int outputSize);
    +LZ4_DEPRECATED("use LZ4_decompress_safe() instead") LZ4LIB_API int LZ4_uncompress_unknownOutputSize (const char* source, char* dest, int isize, int maxOutputSize);
    +


    + +
    LZ4_DEPRECATED("use LZ4_decompress_safe_usingDict() instead") LZ4LIB_API int LZ4_decompress_safe_withPrefix64k (const char* src, char* dst, int compressedSize, int maxDstSize);
    +LZ4_DEPRECATED("use LZ4_decompress_fast_usingDict() instead") LZ4LIB_API int LZ4_decompress_fast_withPrefix64k (const char* src, char* dst, int originalSize);
    +


    + +
    LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe() instead")
    +int LZ4_decompress_fast (const char* src, char* dst, int originalSize);
    +LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe_continue() instead")
    +int LZ4_decompress_fast_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* src, char* dst, int originalSize);
    +LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe_usingDict() instead")
    +int LZ4_decompress_fast_usingDict (const char* src, char* dst, int originalSize, const char* dictStart, int dictSize);
    +

    These functions used to be faster than LZ4_decompress_safe(), + but this is no longer the case. They are now slower. This is because LZ4_decompress_fast() doesn't know the input size, - and therefore must progress more cautiously in the input buffer to not read beyond the end of block. + and therefore must progress more cautiously into the input buffer to not read beyond the end of block. On top of that `LZ4_decompress_fast()` is not protected vs malformed or malicious inputs, making it a security liability. As a consequence, LZ4_decompress_fast() is strongly discouraged, and deprecated. The last remaining LZ4_decompress_fast() specificity is that it can decompress a block without knowing its compressed size. - Such functionality could be achieved in a more secure manner, - by also providing the maximum size of input buffer, - but it would require new prototypes, and adaptation of the implementation to this new use case. + Such functionality can be achieved in a more secure manner + by employing LZ4_decompress_safe_partial(). Parameters: originalSize : is the uncompressed size to regenerate. diff --git a/doc/lz4frame_manual.html b/doc/lz4frame_manual.html index 607583b..2758306 100644 --- a/doc/lz4frame_manual.html +++ b/doc/lz4frame_manual.html @@ -299,8 +299,10 @@ LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_dctx* dctx); void* dstBuffer, size_t* dstSizePtr, const void* srcBuffer, size_t* srcSizePtr, const LZ4F_decompressOptions_t* dOptPtr); -

    Call this function repetitively to regenerate compressed data from `srcBuffer`. - The function will read up to *srcSizePtr bytes from srcBuffer, +

    Call this function repetitively to regenerate data compressed in `srcBuffer`. + + The function requires a valid dctx state. + It will read up to *srcSizePtr bytes from srcBuffer, and decompress data into dstBuffer, of capacity *dstSizePtr. The nb of bytes consumed from srcBuffer will be written into *srcSizePtr (necessarily <= original value). diff --git a/lib/lz4.h b/lib/lz4.h index c4324af..7ab1e48 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -569,7 +569,7 @@ LZ4LIB_STATIC_API void LZ4_attach_dictionary(LZ4_stream_t* workingStream, const #define LZ4_H_98237428734687 /*-************************************************************ - * PRIVATE DEFINITIONS + * Private Definitions ************************************************************** * Do not use these definitions directly. * They are only exposed to allow static allocation of `LZ4_stream_t` and `LZ4_streamDecode_t`. @@ -693,7 +693,7 @@ union LZ4_streamDecode_u { # endif #endif /* LZ4_DISABLE_DEPRECATE_WARNINGS */ -/* Obsolete compression functions */ +/*! Obsolete compression functions (since v1.7.3) */ LZ4_DEPRECATED("use LZ4_compress_default() instead") LZ4LIB_API int LZ4_compress (const char* src, char* dest, int srcSize); LZ4_DEPRECATED("use LZ4_compress_default() instead") LZ4LIB_API int LZ4_compress_limitedOutput (const char* src, char* dest, int srcSize, int maxOutputSize); LZ4_DEPRECATED("use LZ4_compress_fast_extState() instead") LZ4LIB_API int LZ4_compress_withState (void* state, const char* source, char* dest, int inputSize); @@ -701,11 +701,12 @@ LZ4_DEPRECATED("use LZ4_compress_fast_extState() instead") LZ4LIB_API int LZ4_co LZ4_DEPRECATED("use LZ4_compress_fast_continue() instead") LZ4LIB_API int LZ4_compress_continue (LZ4_stream_t* LZ4_streamPtr, const char* source, char* dest, int inputSize); LZ4_DEPRECATED("use LZ4_compress_fast_continue() instead") LZ4LIB_API int LZ4_compress_limitedOutput_continue (LZ4_stream_t* LZ4_streamPtr, const char* source, char* dest, int inputSize, int maxOutputSize); -/* Obsolete decompression functions */ +/*! Obsolete decompression functions (since v1.8.0) */ LZ4_DEPRECATED("use LZ4_decompress_fast() instead") LZ4LIB_API int LZ4_uncompress (const char* source, char* dest, int outputSize); LZ4_DEPRECATED("use LZ4_decompress_safe() instead") LZ4LIB_API int LZ4_uncompress_unknownOutputSize (const char* source, char* dest, int isize, int maxOutputSize); -/* Obsolete streaming functions; degraded functionality; do not use! +/* Obsolete streaming functions (since v1.7.0) + * degraded functionality; do not use! * * In order to perform streaming compression, these functions depended on data * that is no longer tracked in the state. They have been preserved as well as @@ -719,23 +720,22 @@ LZ4_DEPRECATED("Use LZ4_createStream() instead") LZ4LIB_API int LZ4_sizeofStre LZ4_DEPRECATED("Use LZ4_resetStream() instead") LZ4LIB_API int LZ4_resetStreamState(void* state, char* inputBuffer); LZ4_DEPRECATED("Use LZ4_saveDict() instead") LZ4LIB_API char* LZ4_slideInputBuffer (void* state); -/* Obsolete streaming decoding functions */ +/*! Obsolete streaming decoding functions (since v1.7.0) */ LZ4_DEPRECATED("use LZ4_decompress_safe_usingDict() instead") LZ4LIB_API int LZ4_decompress_safe_withPrefix64k (const char* src, char* dst, int compressedSize, int maxDstSize); LZ4_DEPRECATED("use LZ4_decompress_fast_usingDict() instead") LZ4LIB_API int LZ4_decompress_fast_withPrefix64k (const char* src, char* dst, int originalSize); -/*! LZ4_decompress_fast() : **unsafe!** +/*! Obsolete LZ4_decompress_fast variants (since v1.9.0) : * These functions used to be faster than LZ4_decompress_safe(), - * but it has changed, and they are now slower than LZ4_decompress_safe(). + * but this is no longer the case. They are now slower. * This is because LZ4_decompress_fast() doesn't know the input size, - * and therefore must progress more cautiously in the input buffer to not read beyond the end of block. + * and therefore must progress more cautiously into the input buffer to not read beyond the end of block. * On top of that `LZ4_decompress_fast()` is not protected vs malformed or malicious inputs, making it a security liability. * As a consequence, LZ4_decompress_fast() is strongly discouraged, and deprecated. * * The last remaining LZ4_decompress_fast() specificity is that * it can decompress a block without knowing its compressed size. - * Such functionality could be achieved in a more secure manner, - * by also providing the maximum size of input buffer, - * but it would require new prototypes, and adaptation of the implementation to this new use case. + * Such functionality can be achieved in a more secure manner + * by employing LZ4_decompress_safe_partial(). * * Parameters: * originalSize : is the uncompressed size to regenerate. @@ -750,7 +750,6 @@ LZ4_DEPRECATED("use LZ4_decompress_fast_usingDict() instead") LZ4LIB_API int LZ4 * But they may happen if input data is invalid (error or intentional tampering). * As a consequence, use these functions in trusted environments with trusted data **only**. */ - LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe() instead") LZ4LIB_API int LZ4_decompress_fast (const char* src, char* dst, int originalSize); LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe_continue() instead") -- cgit v1.2.3 From af58d2369c8d4614ccd69452617b283e6562768e Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 4 Dec 2019 08:49:06 -0800 Subject: added test for LZ4_decompress_safe_withPrefix64k() --- tests/fullbench.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/tests/fullbench.c b/tests/fullbench.c index 77f475b..a0c42b4 100644 --- a/tests/fullbench.c +++ b/tests/fullbench.c @@ -24,8 +24,8 @@ */ -// S_ISREG & gettimeofday() are not supported by MSVC #if defined(_MSC_VER) || defined(_WIN32) + /* S_ISREG & gettimeofday() are not supported by MSVC */ # define BMK_LEGACY_TIMER 1 #endif @@ -134,7 +134,7 @@ static clock_t BMK_GetClockSpan( clock_t clockStart ) static size_t BMK_findMaxMem(U64 requiredMem) { size_t step = 64 MB; - BYTE* testmem=NULL; + BYTE* testmem = NULL; requiredMem = (((requiredMem >> 26) + 1) << 26); requiredMem += 2*step; @@ -292,9 +292,14 @@ static int local_LZ4_decompress_fast_usingExtDict(const char* in, char* out, int return outSize; } +static int local_LZ4_decompress_safe_withPrefix64k(const char* in, char* out, int inSize, int outSize) +{ + LZ4_decompress_safe_withPrefix64k(in, out, inSize, outSize); + 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, out - 65536, 65536); return outSize; } @@ -608,6 +613,7 @@ int fullSpeedBench(const char** fileNamesTable, int nbFiles) case 2: decompressionFunction = local_LZ4_decompress_fast_usingDict_prefix; dName = "LZ4_decompress_fast_usingDict(prefix)"; break; case 3: decompressionFunction = local_LZ4_decompress_fast_usingExtDict; dName = "LZ4_decompress_fast_using(Ext)Dict"; break; case 4: decompressionFunction = LZ4_decompress_safe; dName = "LZ4_decompress_safe"; break; + case 5: decompressionFunction = local_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"; checkResult = 0; break; #ifndef LZ4_DLL_IMPORT -- cgit v1.2.3 From 38b77ece91aba6c72bc00590f31762f45da6e8fa Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 4 Dec 2019 08:57:12 -0800 Subject: fullbench: added LZ4F_decompress_noHint() --- tests/fullbench.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/fullbench.c b/tests/fullbench.c index a0c42b4..4ec30ce 100644 --- a/tests/fullbench.c +++ b/tests/fullbench.c @@ -386,6 +386,39 @@ static int local_LZ4F_decompress_followHint(const char* src, char* dst, int srcS } +/* always provide input by block of 64 KB */ +static int local_LZ4F_decompress_noHint(const char* src, char* dst, int srcSize, int dstSize) +{ + size_t totalInSize = (size_t)srcSize; + size_t maxOutSize = (size_t)dstSize; + + size_t inPos = 0; + size_t inSize = 64 KB; + size_t outPos = 0; + size_t outRemaining = maxOutSize - outPos; + + for (;;) { + size_t const sizeHint = LZ4F_decompress(g_dCtx, dst+outPos, &outRemaining, src+inPos, &inSize, NULL); + assert(!LZ4F_isError(sizeHint)); + + inPos += inSize; + inSize = (inPos + 64 KB <= totalInSize) ? 64 KB : totalInSize - inPos; + + outPos += outRemaining; + outRemaining = maxOutSize - outPos; + + if (!sizeHint) break; + } + + /* frame completed */ + if (inPos != totalInSize) { + DISPLAY("Error decompressing frame : must read (%u) full frame (%u) \n", + (unsigned)inPos, (unsigned)totalInSize); + exit(10); + } + return (int)outPos; + +} #define NB_COMPRESSION_ALGORITHMS 100 #define NB_DECOMPRESSION_ALGORITHMS 100 @@ -621,8 +654,10 @@ int fullSpeedBench(const char** fileNamesTable, int nbFiles) #endif case 10: case 11: + case 12: if (dAlgNb == 10) { decompressionFunction = local_LZ4F_decompress; dName = "LZ4F_decompress"; } /* can be skipped */ if (dAlgNb == 11) { decompressionFunction = local_LZ4F_decompress_followHint; dName = "LZ4F_decompress_followHint"; } /* can be skipped */ + if (dAlgNb == 12) { decompressionFunction = local_LZ4F_decompress_noHint; dName = "LZ4F_decompress_noHint"; } /* can be skipped */ /* prepare compressed data using frame format */ { size_t const fcsize = LZ4F_compressFrame(compressed_buff, (size_t)compressedBuffSize, orig_buff, benchedSize, NULL); assert(!LZ4F_isError(fcsize)); -- cgit v1.2.3 From 2964b8a6f6d93e86af52f2d23ec18f02421ebd74 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sun, 8 Nov 2020 13:21:58 -0800 Subject: fix #874 coverity reported a warning regarding a memcpy() overwrite. This is a false positive (the memory area is large enough), but it's true that it's not trivial to determine (encompassing struct), and it's proper anyway to only memcpy() just the right amount of data. --- lib/lz4.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/lz4.c b/lib/lz4.c index 427673e..aca97ed 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1606,7 +1606,7 @@ int LZ4_compress_fast_continue (LZ4_stream_t* LZ4_stream, * cost to copy the dictionary's tables into the active context, * so that the compression loop is only looking into one table. */ - LZ4_memcpy(streamPtr, streamPtr->dictCtx, sizeof(LZ4_stream_t)); + LZ4_memcpy(streamPtr, streamPtr->dictCtx, sizeof(*streamPtr)); result = LZ4_compress_generic(streamPtr, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, usingExtDict, noDictIssue, acceleration); } else { result = LZ4_compress_generic(streamPtr, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, usingDictCtx, noDictIssue, acceleration); -- cgit v1.2.3 From 2a2b10f19242aedc8598b74bcb4614d03485c3c0 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sun, 8 Nov 2020 18:08:43 -0800 Subject: fixed remaining ubsan warnings --- lib/lz4.c | 2 +- lib/lz4hc.c | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 427673e..b066c55 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1662,7 +1662,7 @@ int LZ4_saveDict (LZ4_stream_t* LZ4_dict, char* safeBuffer, int dictSize) if ((U32)dictSize > dict->dictSize) { dictSize = (int)dict->dictSize; } if (safeBuffer == NULL) assert(dictSize == 0); - if (safeBuffer != NULL) + if (dictSize > 0) memmove(safeBuffer, previousDictEnd - dictSize, dictSize); dict->dictionary = (const BYTE*)safeBuffer; diff --git a/lib/lz4hc.c b/lib/lz4hc.c index 8875f1a..286ff68 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -1164,13 +1164,16 @@ int LZ4_saveDictHC (LZ4_streamHC_t* LZ4_streamHCPtr, char* safeBuffer, int dictS if (dictSize > 64 KB) dictSize = 64 KB; if (dictSize < 4) dictSize = 0; if (dictSize > prefixSize) dictSize = prefixSize; - memmove(safeBuffer, streamPtr->end - dictSize, dictSize); + if (safeBuffer == NULL) assert(dictSize == 0); + if (dictSize > 0) + memmove(safeBuffer, streamPtr->end - dictSize, dictSize); { U32 const endIndex = (U32)(streamPtr->end - streamPtr->base); streamPtr->end = (const BYTE*)safeBuffer + dictSize; streamPtr->base = streamPtr->end - endIndex; streamPtr->dictLimit = endIndex - (U32)dictSize; streamPtr->lowLimit = endIndex - (U32)dictSize; - if (streamPtr->nextToUpdate < streamPtr->dictLimit) streamPtr->nextToUpdate = streamPtr->dictLimit; + if (streamPtr->nextToUpdate < streamPtr->dictLimit) + streamPtr->nextToUpdate = streamPtr->dictLimit; } return dictSize; } -- cgit v1.2.3 From c76564b944d1fe6e4ffc6dc3d002becfc3c74b5d Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sun, 8 Nov 2020 19:48:47 -0800 Subject: attempt at silencing cppcheck --- tests/fuzzer.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/tests/fuzzer.c b/tests/fuzzer.c index 99361dd..ba36621 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -341,7 +341,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c exit(1); \ } -# define FUZ_CHECKTEST(cond, ...) { if (cond) { EXIT_MSG(__VA_ARGS__) } } +# define FUZ_CHECKTEST(cond, ...) if (cond) { EXIT_MSG(__VA_ARGS__) } # define FUZ_DISPLAYTEST(...) { \ testNb++; \ @@ -1135,7 +1135,7 @@ static void FUZ_unitTests(int compressionLevel) shct* const shc = (shct*)malloc(sizeof(*shc)); assert(shc != NULL); memset(shc, 0, sizeof(*shc)); - DISPLAYLEVEL(3, "state1(%p) state2(%p) state3(%p) LZ4_stream_t size(0x%x): ", + DISPLAYLEVEL(4, "state1(%p) state2(%p) state3(%p) LZ4_stream_t size(0x%x): ", &(shc->state1), &(shc->state2), &(shc->state3), (unsigned)sizeof(LZ4_stream_t)); FUZ_CHECKTEST( LZ4_initStream(&(shc->state1), sizeof(shc->state1)) == NULL, "state1 (%p) failed init", &(shc->state1) ); FUZ_CHECKTEST( LZ4_initStream(&(shc->state2), sizeof(shc->state2)) == NULL, "state2 (%p) failed init", &(shc->state2) ); @@ -1156,16 +1156,16 @@ static void FUZ_unitTests(int compressionLevel) { LZ4_stream_t streamingState; /* simple compression test */ - { U64 const crcOrig = XXH64(testInput, testCompressedSize, 0); - LZ4_initStream(&streamingState, sizeof(streamingState)); - { int const cs = LZ4_compress_fast_continue(&streamingState, testInput, testCompressed, testCompressedSize, testCompressedSize-1, 1); - FUZ_CHECKTEST(cs==0, "LZ4_compress_fast_continue() compression failed!"); - { int const r = LZ4_decompress_safe(testCompressed, testVerify, cs, testCompressedSize); - FUZ_CHECKTEST(r!=(int)testCompressedSize, "LZ4_decompress_safe() decompression failed"); - } } - { U64 const crcNew = XXH64(testVerify, testCompressedSize, 0); - FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe() decompression corruption"); + LZ4_initStream(&streamingState, sizeof(streamingState)); + { int const cs = LZ4_compress_fast_continue(&streamingState, testInput, testCompressed, testCompressedSize, testCompressedSize-1, 1); + FUZ_CHECKTEST(cs==0, "LZ4_compress_fast_continue() compression failed!"); + { int const r = LZ4_decompress_safe(testCompressed, testVerify, cs, testCompressedSize); + FUZ_CHECKTEST(r!=(int)testCompressedSize, "LZ4_decompress_safe() decompression failed"); } } + { U64 const crcOrig = XXH64(testInput, testCompressedSize, 0); + U64 const crcNew = XXH64(testVerify, testCompressedSize, 0); + FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe() decompression corruption"); + } /* early saveDict */ DISPLAYLEVEL(3, "saveDict (right after init) : "); @@ -1245,7 +1245,7 @@ static void FUZ_unitTests(int compressionLevel) shct* const shc = (shct*)malloc(sizeof(*shc)); assert(shc != NULL); memset(shc, 0, sizeof(*shc)); - DISPLAYLEVEL(3, "hc1(%p) hc2(%p) hc3(%p) size(0x%x): ", + DISPLAYLEVEL(4, "hc1(%p) hc2(%p) hc3(%p) size(0x%x): ", &(shc->hc1), &(shc->hc2), &(shc->hc3), (unsigned)sizeof(LZ4_streamHC_t)); FUZ_CHECKTEST( LZ4_initStreamHC(&(shc->hc1), sizeof(shc->hc1)) == NULL, "hc1 (%p) failed init", &(shc->hc1) ); FUZ_CHECKTEST( LZ4_initStreamHC(&(shc->hc2), sizeof(shc->hc2)) == NULL, "hc2 (%p) failed init", &(shc->hc2) ); -- cgit v1.2.3 From 52646e8d7517b9b399d3e3719c65816afdc833ab Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sun, 8 Nov 2020 21:17:32 -0800 Subject: first proposal for LZ4_USER_MEMORY_FUNCTIONS makes it possible to replace at link time malloc, calloc and free by user-provided functions which must be named LZ4_malloc(), LZ4_calloc() and LZ4_free(). answer #937 --- .travis.yml | 4 +++- lib/README.md | 4 ++++ lib/lz4.c | 21 +++++++++++++++++---- lib/lz4hc.c | 15 +++++++-------- tests/Makefile | 9 ++++++--- tests/fullbench.c | 8 ++++++++ 6 files changed, 45 insertions(+), 16 deletions(-) diff --git a/.travis.yml b/.travis.yml index 2281394..de9dce8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -31,11 +31,13 @@ matrix: script: - CC=clang MOREFLAGS=-fsanitize=address make -C tests test-frametest test-fuzzer - - name: Custom LZ4_DISTANCE_MAX ; Build lz4-wlib (CLI linked to dynamic library) + - name: Custom LZ4_DISTANCE_MAX ; lz4-wlib (CLI linked to dynamic library); LZ4_USER_MEMORY_FUNCTIONS script: - MOREFLAGS=-DLZ4_DISTANCE_MAX=8000 make check - make clean - make -C programs lz4-wlib + - make clean + - make fullbench-wmalloc # test LZ4_USER_MEMORY_FUNCTIONS - name: (Precise) g++ and clang CMake test dist: precise diff --git a/lib/README.md b/lib/README.md index 318106d..e2af868 100644 --- a/lib/README.md +++ b/lib/README.md @@ -69,6 +69,10 @@ The following build macro can be selected to adjust source code behavior at comp This build macro offers another project-specific method by defining `LZ4_DISABLE_DEPRECATE_WARNINGS` before including the LZ4 header files. +- `LZ4_USER_MEMORY_FUNCTIONS` : replace calls to 's `malloc`, `calloc` and `free` + by user-defined functions, which must be called `LZ4_malloc()`, `LZ4_calloc()` and `LZ4_free()`. + User functions must be available at link time. + - `LZ4_FORCE_SW_BITCOUNT` : by default, the compression algorithm tries to determine lengths by using bitcount instructions, generally implemented as fast single instructions in many cpus. In case the target cpus doesn't support it, or compiler intrinsic doesn't work, or feature bad performance, diff --git a/lib/lz4.c b/lib/lz4.c index 427673e..a8cc420 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -188,10 +188,23 @@ /*-************************************ * Memory routines **************************************/ -#include /* malloc, calloc, free */ -#define ALLOC(s) malloc(s) -#define ALLOC_AND_ZERO(s) calloc(1,s) -#define FREEMEM(p) free(p) +#ifdef LZ4_USER_MEMORY_FUNCTIONS +/* memory management functions can be customized by user project. + * Below functions must exist somewhere in the Project + * and be available at link time */ +void* LZ4_malloc(size_t s); +void* LZ4_calloc(size_t s); +void LZ4_free(void* p); +# define ALLOC(s) LZ4_malloc(s) +# define ALLOC_AND_ZERO(s) LZ4_calloc(s) +# define FREEMEM(p) LZ4_free(p) +#else +# include /* malloc, calloc, free */ +# define ALLOC(s) malloc(s) +# define ALLOC_AND_ZERO(s) calloc(1,s) +# define FREEMEM(p) free(p) +#endif + #include /* memset, memcpy */ #define MEM_INIT(p,v,s) memset((p),(v),(s)) diff --git a/lib/lz4hc.c b/lib/lz4hc.c index 8875f1a..8320f33 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -93,7 +93,7 @@ static U32 LZ4HC_hashPtr(const void* ptr) { return HASH_FUNCTION(LZ4_read32(ptr) **************************************/ static void LZ4HC_clearTables (LZ4HC_CCtx_internal* hc4) { - MEM_INIT((void*)hc4->hashTable, 0, sizeof(hc4->hashTable)); + MEM_INIT(hc4->hashTable, 0, sizeof(hc4->hashTable)); MEM_INIT(hc4->chainTable, 0xFF, sizeof(hc4->chainTable)); } @@ -983,11 +983,10 @@ int LZ4_compress_HC_destSize(void* state, const char* source, char* dest, int* s /* allocation */ LZ4_streamHC_t* LZ4_createStreamHC(void) { - LZ4_streamHC_t* const state = (LZ4_streamHC_t*)ALLOC(sizeof(LZ4_streamHC_t)); - if (LZ4_initStreamHC(state, sizeof(*state)) == NULL) { - free(state); - return NULL; - } + LZ4_streamHC_t* const state = + (LZ4_streamHC_t*)ALLOC_AND_ZERO(sizeof(LZ4_streamHC_t)); + if (state == NULL) return NULL; + LZ4_setCompressionLevel(state, LZ4HC_CLEVEL_DEFAULT); return state; } @@ -1323,7 +1322,7 @@ static int LZ4HC_compress_optimal ( LZ4HC_CCtx_internal* ctx, int retval = 0; #define TRAILING_LITERALS 3 #ifdef LZ4HC_HEAPMODE - LZ4HC_optimal_t* const opt = (LZ4HC_optimal_t*)malloc(sizeof(LZ4HC_optimal_t) * (LZ4_OPT_NUM + TRAILING_LITERALS)); + LZ4HC_optimal_t* const opt = (LZ4HC_optimal_t*)ALLOC(sizeof(LZ4HC_optimal_t) * (LZ4_OPT_NUM + TRAILING_LITERALS)); #else LZ4HC_optimal_t opt[LZ4_OPT_NUM + TRAILING_LITERALS]; /* ~64 KB, which is a bit large for stack... */ #endif @@ -1606,7 +1605,7 @@ if (limit == fillOutput) { } _return_label: #ifdef LZ4HC_HEAPMODE - free(opt); + FREEMEM(opt); #endif return retval; } diff --git a/tests/Makefile b/tests/Makefile index 43c9651..476849e 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -53,6 +53,7 @@ TEST_FILES := COPYING FUZZER_TIME := -T90s NB_LOOPS ?= -i1 +.PHONY: default default: all all: fullbench fuzzer frametest roundTripTest datagen checkFrame decompress-partial @@ -89,6 +90,10 @@ fullbench-dll: fullbench.c $(LZ4DIR)/xxhash.c $(MAKE) -C $(LZ4DIR) liblz4 $(CC) $(FLAGS) $^ -o $@$(EXT) -DLZ4_DLL_IMPORT=1 $(LZ4DIR)/dll/$(LIBLZ4).dll +# test LZ4_USER_MEMORY_FUNCTIONS +fullbench-wmalloc: CPPFLAGS += -DLZ4_USER_MEMORY_FUNCTIONS +fullbench-wmalloc: fullbench + fuzzer : lz4.o lz4hc.o xxhash.o fuzzer.c $(CC) $(FLAGS) $^ -o $@$(EXT) @@ -107,6 +112,7 @@ checkFrame : lz4frame.o lz4.o lz4hc.o xxhash.o checkFrame.c decompress-partial: lz4.o decompress-partial.c $(CC) $(FLAGS) $^ -o $@$(EXT) +.PHONY: clean clean: @$(MAKE) -C $(LZ4DIR) $@ > $(VOID) @$(MAKE) -C $(PRGDIR) $@ > $(VOID) @@ -170,9 +176,6 @@ test32: test test-amalgamation: lz4_all.o -lz4_all.o: lz4_all.c - $(CC) $(CFLAGS) $(CPPFLAGS) -c $^ -o $@ - lz4_all.c: $(LZ4DIR)/lz4.c $(LZ4DIR)/lz4hc.c $(LZ4DIR)/lz4frame.c $(CAT) $^ > $@ diff --git a/tests/fullbench.c b/tests/fullbench.c index 77f475b..0e3c009 100644 --- a/tests/fullbench.c +++ b/tests/fullbench.c @@ -155,6 +155,14 @@ static size_t BMK_findMaxMem(U64 requiredMem) } +/********************************************************* +* Memory management, to test LZ4_USER_MEMORY_FUNCTIONS +*********************************************************/ +void* LZ4_malloc(size_t s) { return malloc(s); } +void* LZ4_calloc(size_t s) { return calloc(1,s); } +void LZ4_free(void* p) { return free(p); } + + /********************************************************* * Benchmark function *********************************************************/ -- cgit v1.2.3 From 243ea5647e7ec89970a0e80d9fd3bb1c783c8ec2 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sun, 8 Nov 2020 23:36:24 -0800 Subject: fix travis test --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index de9dce8..31bc675 100644 --- a/.travis.yml +++ b/.travis.yml @@ -37,7 +37,7 @@ matrix: - make clean - make -C programs lz4-wlib - make clean - - make fullbench-wmalloc # test LZ4_USER_MEMORY_FUNCTIONS + - make -C tests fullbench-wmalloc # test LZ4_USER_MEMORY_FUNCTIONS - name: (Precise) g++ and clang CMake test dist: precise -- cgit v1.2.3 From adc46ea4c176380fb104273f66f1e618713a2481 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 9 Nov 2020 00:29:52 -0800 Subject: fix fullbench's LZ4_free() --- tests/fullbench.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/fullbench.c b/tests/fullbench.c index dad6c14..39932d2 100644 --- a/tests/fullbench.c +++ b/tests/fullbench.c @@ -160,7 +160,7 @@ static size_t BMK_findMaxMem(U64 requiredMem) *********************************************************/ void* LZ4_malloc(size_t s) { return malloc(s); } void* LZ4_calloc(size_t s) { return calloc(1,s); } -void LZ4_free(void* p) { return free(p); } +void LZ4_free(void* p) { free(p); } /********************************************************* -- cgit v1.2.3 From a29683980264b046797e207c6e995cae0a12b0a8 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 9 Nov 2020 08:44:26 -0800 Subject: changed LZ4_calloc() to a 2-arguments signature to remain similar to stdlib's calloc(). Updated test to use c++ compiler for stricter signature check. --- .travis.yml | 2 ++ lib/lz4.c | 4 ++-- tests/fullbench.c | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 31bc675..f201d52 100644 --- a/.travis.yml +++ b/.travis.yml @@ -38,6 +38,8 @@ matrix: - make -C programs lz4-wlib - make clean - make -C tests fullbench-wmalloc # test LZ4_USER_MEMORY_FUNCTIONS + - make clean + - CC="c++ -Wno-deprecated" make -C tests fullbench-wmalloc # stricter function signature check - name: (Precise) g++ and clang CMake test dist: precise diff --git a/lib/lz4.c b/lib/lz4.c index 128e7b2..5fe3433 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -193,10 +193,10 @@ * Below functions must exist somewhere in the Project * and be available at link time */ void* LZ4_malloc(size_t s); -void* LZ4_calloc(size_t s); +void* LZ4_calloc(size_t n, size_t s); void LZ4_free(void* p); # define ALLOC(s) LZ4_malloc(s) -# define ALLOC_AND_ZERO(s) LZ4_calloc(s) +# define ALLOC_AND_ZERO(s) LZ4_calloc(1,s) # define FREEMEM(p) LZ4_free(p) #else # include /* malloc, calloc, free */ diff --git a/tests/fullbench.c b/tests/fullbench.c index 39932d2..181128e 100644 --- a/tests/fullbench.c +++ b/tests/fullbench.c @@ -159,7 +159,7 @@ static size_t BMK_findMaxMem(U64 requiredMem) * Memory management, to test LZ4_USER_MEMORY_FUNCTIONS *********************************************************/ void* LZ4_malloc(size_t s) { return malloc(s); } -void* LZ4_calloc(size_t s) { return calloc(1,s); } +void* LZ4_calloc(size_t n, size_t s) { (void)n; return calloc(1,s); } void LZ4_free(void* p) { free(p); } -- cgit v1.2.3 From 2c4af01e199d605ef6b9ba6be85fe8edcb23aa8a Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 9 Nov 2020 10:58:31 -0800 Subject: proper fullbench's LZ4_calloc() --- tests/fullbench.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/fullbench.c b/tests/fullbench.c index 181128e..cb9b684 100644 --- a/tests/fullbench.c +++ b/tests/fullbench.c @@ -159,7 +159,7 @@ static size_t BMK_findMaxMem(U64 requiredMem) * Memory management, to test LZ4_USER_MEMORY_FUNCTIONS *********************************************************/ void* LZ4_malloc(size_t s) { return malloc(s); } -void* LZ4_calloc(size_t n, size_t s) { (void)n; return calloc(1,s); } +void* LZ4_calloc(size_t n, size_t s) { return calloc(n,s); } void LZ4_free(void* p) { free(p); } -- cgit v1.2.3 From 86f5e9d0f4a561dfefb3a773ee4198236bf6e202 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 9 Nov 2020 21:23:56 -0800 Subject: minor : lz4frame include stdlib only if needed --- lib/lz4frame.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/lz4frame.c b/lib/lz4frame.c index e02b76c..ec02c92 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -71,8 +71,8 @@ * towards another library or solution of their choice * by modifying below section. */ -#include /* malloc, calloc, free */ #ifndef LZ4_SRC_INCLUDED /* avoid redefinition when sources are coalesced */ +# include /* malloc, calloc, free */ # define ALLOC(s) malloc(s) # define ALLOC_AND_ZERO(s) calloc(1,(s)) # define FREEMEM(p) free(p) -- cgit v1.2.3 From f9631e44a83b3b1278d6d3405acca20ecc8ad4c5 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 9 Nov 2020 21:56:49 -0800 Subject: updated NEWS for v1.9.3 --- NEWS | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index 34ca486..91f7506 100644 --- a/NEWS +++ b/NEWS @@ -1,8 +1,24 @@ v1.9.3 -perf: faster speed on Visual Studio, by @wolfpld +perf: highly improved speed in kernel space, by @terrelln +perf: faster speed with Visual Studio, thanks to @wolfpld and @remittor +perf: improved dictionary compression speed, by @felixhandte +perf: fixed LZ4_compress_HC_destSize() ratio, detected by @hsiangkao +perf: reduced stack usage in high compression mode, by @Yanpas +api : LZ4_decompress_safe_partial() supports unknown compressed size, requested by @jfkthame +api : improved LZ4F_compressBound() with automatic flushing, by Christopher Harvie +api : can (de)compress to/from NULL without UBs +api : fix alignment test on 32-bit systems (state initialization) +api : fix LZ4_saveDictHC() in corner case scenario, detected by @IgorKorkin +cli : --list command, compressed file content statistics, by Filipe Calasans +cli : benchmark mode supports dictionary, by @rkoradi +cli : fix --fast with large argument, detected by @picoHz +build: link to user-defined memory functions with LZ4_USER_MEMORY_FUNCTIONS, suggested by Yuriy Levchenko build: contrib/cmake_unofficial/ moved to build/cmake/ build: visual/* moved to build/ - +build: updated meson script, by @neheb +build: tinycc support, by Anton Kochkov +install: Haiku support, by Jerome Duval +doc : updated LZ4 frame format, clarify EndMark v1.9.2 fix : out-of-bound read in exceptional circumstances when using decompress_partial(), by @terrelln -- cgit v1.2.3 From d2a1f3b23935db35dc04a6842b988bec28afadab Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 11 Nov 2020 00:32:49 -0800 Subject: fix cmake build of shared dll with visual inspired from #891 --- build/cmake/CMakeLists.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/build/cmake/CMakeLists.txt b/build/cmake/CMakeLists.txt index 1bb2f5a..57501ee 100644 --- a/build/cmake/CMakeLists.txt +++ b/build/cmake/CMakeLists.txt @@ -107,6 +107,10 @@ if(BUILD_SHARED_LIBS) OUTPUT_NAME lz4 SOVERSION "${LZ4_VERSION_MAJOR}" VERSION "${LZ4_VERSION_STRING}") + if(MSVC) + target_compile_definitions(lz4_shared PRIVATE + LZ4_DLL_EXPORT=1) + endif() list(APPEND LZ4_LIBRARIES_BUILT lz4_shared) endif() if(BUILD_STATIC_LIBS) -- cgit v1.2.3 From 1921a059932aa9f46ae9ea42aa2f6f88b918db2b Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 11 Nov 2020 17:12:35 -0800 Subject: appveyor artifact generation fix presence of the dynamic library in produced package --- appveyor.yml | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 31894d3..b4c27ef 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,6 +1,10 @@ version: 1.0.{build} environment: matrix: + - COMPILER: "gcc" + PLATFORM: "mingw64" + - COMPILER: "gcc" + PLATFORM: "mingw32" - COMPILER: "visual" CONFIGURATION: "Debug" PLATFORM: "x64" @@ -13,10 +17,6 @@ environment: - COMPILER: "visual" CONFIGURATION: "Release" PLATFORM: "Win32" - - COMPILER: "gcc" - PLATFORM: "mingw64" - - COMPILER: "gcc" - PLATFORM: "mingw32" - COMPILER: "gcc" PLATFORM: "clang" @@ -50,7 +50,7 @@ build_script: make -C programs lz4 && make -C tests fullbench && make -C tests fuzzer && - make -C lib lib + make -C lib lib V=1 ) ELSE ( make -C programs lz4 CC=clang MOREFLAGS="--target=x86_64-w64-mingw32 -Werror -Wconversion -Wno-sign-conversion" && make -C tests fullbench CC=clang MOREFLAGS="--target=x86_64-w64-mingw32 -Werror -Wconversion -Wno-sign-conversion" && @@ -67,18 +67,18 @@ build_script: COPY lib\lz4hc.h bin\include\ && COPY lib\lz4frame.h bin\include\ && COPY lib\liblz4.a bin\static\liblz4_static.lib && - COPY lib\dll\liblz4.* bin\dll\ && + COPY lib\dll\* bin\dll\ && COPY lib\dll\example\Makefile bin\example\ && COPY lib\dll\example\fullbench-dll.* bin\example\ && COPY lib\dll\example\README.md bin\ && COPY programs\lz4.exe bin\lz4.exe ) - if [%COMPILER%]==[gcc] if [%PLATFORM%]==[mingw64] ( - 7z.exe a bin\lz4_x64.zip NEWS .\bin\lz4.exe .\bin\README.md .\bin\example .\bin\dll .\bin\static .\bin\include && + 7z.exe a -bb1 bin\lz4_x64.zip NEWS .\bin\lz4.exe .\bin\README.md .\bin\example .\bin\dll .\bin\static .\bin\include && appveyor PushArtifact bin\lz4_x64.zip ) - if [%COMPILER%]==[gcc] if [%PLATFORM%]==[mingw32] ( - 7z.exe a bin\lz4_x86.zip NEWS .\bin\lz4.exe .\bin\README.md .\bin\example .\bin\dll .\bin\static .\bin\include && + 7z.exe a -bb1 bin\lz4_x86.zip NEWS .\bin\lz4.exe .\bin\README.md .\bin\example .\bin\dll .\bin\static .\bin\include && appveyor PushArtifact bin\lz4_x86.zip ) - if [%COMPILER%]==[gcc] (COPY tests\*.exe programs\) -- cgit v1.2.3 From b7c6fc9f6194a1dd78ffc8406b83ea68a155845a Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 13 Nov 2020 10:53:59 -0800 Subject: minor refactor of lz4io for readability. Mostly around --list capability --- lib/lz4frame.h | 5 +- programs/lz4io.c | 191 +++++++++++++++++++++++++++---------------------------- 2 files changed, 98 insertions(+), 98 deletions(-) diff --git a/lib/lz4frame.h b/lib/lz4frame.h index 6e91e2d..4573317 100644 --- a/lib/lz4frame.h +++ b/lib/lz4frame.h @@ -108,7 +108,7 @@ LZ4FLIB_API const char* LZ4F_getErrorName(LZ4F_errorCode_t code); /**< return /*-************************************ * Frame compression types - **************************************/ + ************************************* */ /* #define LZ4F_ENABLE_OBSOLETE_ENUMS // uncomment to enable obsolete enums */ #ifdef LZ4F_ENABLE_OBSOLETE_ENUMS # define LZ4F_OBSOLETE_ENUM(x) , LZ4F_DEPRECATE(x) = LZ4F_##x @@ -118,7 +118,8 @@ LZ4FLIB_API const char* LZ4F_getErrorName(LZ4F_errorCode_t code); /**< return /* The larger the block size, the (slightly) better the compression ratio, * though there are diminishing returns. - * Larger blocks also increase memory usage on both compression and decompression sides. */ + * Larger blocks also increase memory usage on both compression and decompression sides. + */ typedef enum { LZ4F_default=0, LZ4F_max64KB=4, diff --git a/programs/lz4io.c b/programs/lz4io.c index 3ffc519..2590d6d 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -111,20 +111,20 @@ static clock_t g_time = 0; **************************************/ struct LZ4IO_prefs_s { - int passThrough; - int overwrite; - int testMode; - int blockSizeId; - size_t blockSize; - int blockChecksum; - int streamChecksum; - int blockIndependence; - int sparseFileSupport; - int contentSizeFlag; - int useDictionary; - unsigned favorDecSpeed; - const char* dictionaryFilename; - int removeSrcFile; + int passThrough; + int overwrite; + int testMode; + int blockSizeId; + size_t blockSize; + int blockChecksum; + int streamChecksum; + int blockIndependence; + int sparseFileSupport; + int contentSizeFlag; + int useDictionary; + unsigned favorDecSpeed; + const char* dictionaryFilename; + int removeSrcFile; }; /************************************** @@ -440,7 +440,8 @@ int LZ4IO_compressFilename_Legacy(LZ4IO_prefs_t* const prefs, const char* input_ /* Compress Block */ outSize = compressionFunction(in_buff, out_buff+4, (int)inSize, outBuffSize, compressionlevel); - compressedfilesize += outSize+4; + assert(outSize >= 0); + compressedfilesize += (unsigned long long)outSize+4; DISPLAYUPDATE(2, "\rRead : %i MB ==> %.2f%% ", (int)(filesize>>20), (double)compressedfilesize/filesize*100); @@ -448,7 +449,7 @@ int LZ4IO_compressFilename_Legacy(LZ4IO_prefs_t* const prefs, const char* input_ assert(outSize > 0); assert(outSize < outBuffSize); LZ4IO_writeLE32(out_buff, (unsigned)outSize); - { size_t const writeSize = fwrite(out_buff, 1, outSize+4, foutput); + { size_t const writeSize = fwrite(out_buff, 1, (size_t)outSize+4, foutput); if (writeSize != (size_t)(outSize+4)) EXM_THROW(24, "Write error : cannot write compressed block"); } } @@ -919,7 +920,7 @@ static unsigned LZ4IO_fwriteSparse(LZ4IO_prefs_t* const prefs, FILE* file, const int const seekResult = UTIL_fseek(file, storedSkips, SEEK_CUR); if (seekResult) EXM_THROW(74, "Sparse skip error ; try --no-sparse"); storedSkips = 0; - { size_t const sizeCheck = fwrite(restPtr, 1, restEnd - restPtr, file); + { size_t const sizeCheck = fwrite(restPtr, 1, (size_t)(restEnd - restPtr), file); if (sizeCheck != (size_t)(restEnd - restPtr)) EXM_THROW(75, "Write error : cannot write decoded end of block"); } } } @@ -1099,7 +1100,10 @@ static unsigned long long LZ4IO_decompressLZ4F(LZ4IO_prefs_t* const prefs, dRess #define PTSIZE (64 KB) #define PTSIZET (PTSIZE / sizeof(size_t)) -static unsigned long long LZ4IO_passThrough(LZ4IO_prefs_t* const prefs, FILE* finput, FILE* foutput, unsigned char MNstore[MAGICNUMBER_SIZE]) +static unsigned long long +LZ4IO_passThrough(LZ4IO_prefs_t* const prefs, + FILE* finput, FILE* foutput, + unsigned char MNstore[MAGICNUMBER_SIZE]) { size_t buffer[PTSIZET]; size_t readBytes = 1; @@ -1351,7 +1355,7 @@ typedef struct { unsigned short allContentSize; } LZ4IO_cFileInfo_t; -#define LZ4IO_INIT_CFILEINFO { NULL, 0ULL, 0, LZ4IO_INIT_FRAMEINFO, 1, 1, 1 } +#define LZ4IO_INIT_CFILEINFO { NULL, 0ULL, 0, LZ4IO_INIT_FRAMEINFO, 1, 1, 1 } typedef enum { LZ4IO_LZ4F_OK, LZ4IO_format_not_known, LZ4IO_not_a_file } LZ4IO_infoResult; @@ -1363,9 +1367,11 @@ static const char * LZ4IO_frameTypeNames[] = {"LZ4Frame", "LegacyFrame", "Skippa returns 0 in case it can't succesfully skip block data. Assumes SEEK_CUR after frame header. */ -static unsigned long long LZ4IO_skipBlocksData(FILE* finput, - const LZ4F_blockChecksum_t blockChecksumFlag, - const LZ4F_contentChecksum_t contentChecksumFlag) { +static unsigned long long +LZ4IO_skipBlocksData(FILE* finput, + const LZ4F_blockChecksum_t blockChecksumFlag, + const LZ4F_contentChecksum_t contentChecksumFlag) +{ unsigned char blockInfo[LZ4F_BLOCK_HEADER_SIZE]; unsigned long long totalBlocksSize = 0; for (;;) { @@ -1374,8 +1380,7 @@ static unsigned long long LZ4IO_skipBlocksData(FILE* finput, return 0; } totalBlocksSize += LZ4F_BLOCK_HEADER_SIZE; - { - const unsigned long nextCBlockSize = LZ4IO_readLE32(&blockInfo) & 0x7FFFFFFFU; + { const unsigned long nextCBlockSize = LZ4IO_readLE32(&blockInfo) & 0x7FFFFFFFU; const unsigned long nextBlock = nextCBlockSize + (blockChecksumFlag * LZ4F_BLOCK_CHECKSUM_SIZE); if (nextCBlockSize == 0) { /* Reached EndMark */ @@ -1390,11 +1395,9 @@ static unsigned long long LZ4IO_skipBlocksData(FILE* finput, } totalBlocksSize += nextBlock; /* skip to the next block */ - if (UTIL_fseek(finput, nextBlock, SEEK_CUR) != 0) { - return 0; - } - } - } + assert(nextBlock < LONG_MAX); + if (UTIL_fseek(finput, (long)nextBlock, SEEK_CUR) != 0) return 0; + } } return totalBlocksSize; } @@ -1405,7 +1408,8 @@ static unsigned long long LZ4IO_skipBlocksData(FILE* finput, This works as long as legacy block header size = magic number size. Assumes SEEK_CUR after frame header. */ -static unsigned long long LZ4IO_skipLegacyBlocksData(FILE* finput) { +static unsigned long long LZ4IO_skipLegacyBlocksData(FILE* finput) +{ unsigned char blockInfo[LZIO_LEGACY_BLOCK_HEADER_SIZE]; unsigned long long totalBlocksSize = 0; LZ4IO_STATIC_ASSERT(LZIO_LEGACY_BLOCK_HEADER_SIZE == MAGICNUMBER_SIZE); @@ -1428,14 +1432,15 @@ static unsigned long long LZ4IO_skipLegacyBlocksData(FILE* finput) { /* skip to the next block */ if (UTIL_fseek(finput, nextCBlockSize, SEEK_CUR) != 0) { return 0; - } - } - } + } } } return totalBlocksSize; } -/* buffer : must be a valid memory area of at least 4 bytes */ -const char* LZ4IO_blockTypeID(int sizeID, int blockMode, char* buffer) { +/* LZ4IO_blockTypeID: + * return human-readable block type, following command line convention + * buffer : must be a valid memory area of at least 4 bytes */ +const char* LZ4IO_blockTypeID(LZ4F_blockSizeID_t sizeID, LZ4F_blockMode_t blockMode, char buffer[4]) +{ buffer[0] = 'B'; assert(sizeID >= 4); assert(sizeID <= 7); buffer[1] = (char)(sizeID + '0'); @@ -1445,7 +1450,8 @@ const char* LZ4IO_blockTypeID(int sizeID, int blockMode, char* buffer) { } /* buffer : must be valid memory area of at least 10 bytes */ -static const char* LZ4IO_toHuman(long double size, char *buf) { +static const char* LZ4IO_toHuman(long double size, char *buf) +{ const char units[] = {"\0KMGTPEZY"}; size_t i = 0; for (; size >= 1024; i++) size /= 1024; @@ -1454,14 +1460,15 @@ static const char* LZ4IO_toHuman(long double size, char *buf) { } /* Get filename without path prefix */ -static const char* LZ4IO_baseName(const char* input_filename) { +static const char* LZ4IO_baseName(const char* input_filename) +{ const char* b = strrchr(input_filename, '/'); if (!b) b = strrchr(input_filename, '\\'); if (!b) return input_filename; return b + 1; } -/* Report frame/s information in verbose mode. +/* Report frame/s information (--list) in verbose mode (-v). * Will populate file info with fileName and frameSummary where applicable. * - TODO : * + report nb of blocks, hence max. possible decompressed size (when not reported in header) @@ -1480,11 +1487,12 @@ LZ4IO_getCompressedFileInfo(LZ4IO_cFileInfo_t* cfinfo, const char* input_filenam LZ4IO_frameInfo_t frameInfo = LZ4IO_INIT_FRAMEINFO; unsigned magicNumber; /* Get MagicNumber */ - size_t nbReadBytes = fread(buffer, 1, MAGICNUMBER_SIZE, finput); - if (nbReadBytes == 0) { break; } /* EOF */ - result = LZ4IO_format_not_known; /* default result (error) */ - if (nbReadBytes != MAGICNUMBER_SIZE) - EXM_THROW(40, "Unrecognized header : Magic Number unreadable"); + { size_t const nbReadBytes = fread(buffer, 1, MAGICNUMBER_SIZE, finput); + if (nbReadBytes == 0) { break; } /* EOF */ + result = LZ4IO_format_not_known; /* default result (error) */ + if (nbReadBytes != MAGICNUMBER_SIZE) { + EXM_THROW(40, "Unrecognized header : Magic Number unreadable"); + } } magicNumber = LZ4IO_readLE32(buffer); /* Little Endian format */ if (LZ4IO_isSkippableMagicNumber(magicNumber)) magicNumber = LZ4IO_SKIPPABLE0; /* fold skippable magic numbers */ @@ -1497,56 +1505,49 @@ LZ4IO_getCompressedFileInfo(LZ4IO_cFileInfo_t* cfinfo, const char* input_filenam if (!readBytes || ferror(finput)) EXM_THROW(71, "Error reading %s", input_filename); } { size_t hSize = LZ4F_headerSize(&buffer, LZ4F_HEADER_SIZE_MIN); - if (!LZ4F_isError(hSize)) { - if (hSize > (LZ4F_HEADER_SIZE_MIN + MAGICNUMBER_SIZE)) { - /* We've already read LZ4F_HEADER_SIZE_MIN so read any extra until hSize*/ - const size_t readBytes = fread(buffer + LZ4F_HEADER_SIZE_MIN, 1, hSize - LZ4F_HEADER_SIZE_MIN, finput); - if (!readBytes || ferror(finput)) EXM_THROW(72, "Error reading %s", input_filename); - } - /* Create decompression context */ - { LZ4F_dctx* dctx; - unsigned isError = LZ4F_isError(LZ4F_createDecompressionContext(&dctx, LZ4F_VERSION)); - if (!isError) { - isError = LZ4F_isError(LZ4F_getFrameInfo(dctx, &frameInfo.lz4FrameInfo, buffer, &hSize)); - LZ4F_freeDecompressionContext(dctx); - if (!isError) { - if ((cfinfo->frameSummary.lz4FrameInfo.blockSizeID != frameInfo.lz4FrameInfo.blockSizeID || - cfinfo->frameSummary.lz4FrameInfo.blockMode != frameInfo.lz4FrameInfo.blockMode) - && cfinfo->frameCount != 0) - cfinfo->eqBlockTypes = 0; - { const unsigned long long totalBlocksSize = LZ4IO_skipBlocksData(finput, - frameInfo.lz4FrameInfo.blockChecksumFlag, - frameInfo.lz4FrameInfo.contentChecksumFlag); - if (totalBlocksSize) { - char bTypeBuffer[5]; - LZ4IO_blockTypeID(frameInfo.lz4FrameInfo.blockSizeID, frameInfo.lz4FrameInfo.blockMode, bTypeBuffer); - DISPLAYLEVEL(3, " %6llu %14s %5s %8s", - cfinfo->frameCount + 1, - LZ4IO_frameTypeNames[frameInfo.frameType], - bTypeBuffer, - frameInfo.lz4FrameInfo.contentChecksumFlag ? "XXH32" : "-"); - if (frameInfo.lz4FrameInfo.contentSize) { - { double const ratio = (double)(totalBlocksSize + hSize) / frameInfo.lz4FrameInfo.contentSize * 100; - DISPLAYLEVEL(3, " %20llu %20llu %9.2f%%\n", - totalBlocksSize + hSize, - frameInfo.lz4FrameInfo.contentSize, - ratio); - } - /* Now we've consumed frameInfo we can use it to store the total contentSize */ - frameInfo.lz4FrameInfo.contentSize += cfinfo->frameSummary.lz4FrameInfo.contentSize; - } - else { - DISPLAYLEVEL(3, " %20llu %20s %9s \n", totalBlocksSize + hSize, "-", "-"); - cfinfo->allContentSize = 0; - } - result = LZ4IO_LZ4F_OK; + if (LZ4F_isError(hSize)) break; + if (hSize > (LZ4F_HEADER_SIZE_MIN + MAGICNUMBER_SIZE)) { + /* We've already read LZ4F_HEADER_SIZE_MIN so read any extra until hSize*/ + const size_t readBytes = fread(buffer + LZ4F_HEADER_SIZE_MIN, 1, hSize - LZ4F_HEADER_SIZE_MIN, finput); + if (!readBytes || ferror(finput)) EXM_THROW(72, "Error reading %s", input_filename); + } + /* Create decompression context */ + { LZ4F_dctx* dctx; + if ( LZ4F_isError(LZ4F_createDecompressionContext(&dctx, LZ4F_VERSION)) ) break; + { unsigned const frameInfoError = LZ4F_isError(LZ4F_getFrameInfo(dctx, &frameInfo.lz4FrameInfo, buffer, &hSize)); + LZ4F_freeDecompressionContext(dctx); + if (frameInfoError) break; + if ((cfinfo->frameSummary.lz4FrameInfo.blockSizeID != frameInfo.lz4FrameInfo.blockSizeID || + cfinfo->frameSummary.lz4FrameInfo.blockMode != frameInfo.lz4FrameInfo.blockMode) + && cfinfo->frameCount != 0) + cfinfo->eqBlockTypes = 0; + { const unsigned long long totalBlocksSize = LZ4IO_skipBlocksData(finput, + frameInfo.lz4FrameInfo.blockChecksumFlag, + frameInfo.lz4FrameInfo.contentChecksumFlag); + if (totalBlocksSize) { + char bTypeBuffer[5]; + LZ4IO_blockTypeID(frameInfo.lz4FrameInfo.blockSizeID, frameInfo.lz4FrameInfo.blockMode, bTypeBuffer); + DISPLAYLEVEL(3, " %6llu %14s %5s %8s", + cfinfo->frameCount + 1, + LZ4IO_frameTypeNames[frameInfo.frameType], + bTypeBuffer, + frameInfo.lz4FrameInfo.contentChecksumFlag ? "XXH32" : "-"); + if (frameInfo.lz4FrameInfo.contentSize) { + { double const ratio = (double)(totalBlocksSize + hSize) / frameInfo.lz4FrameInfo.contentSize * 100; + DISPLAYLEVEL(3, " %20llu %20llu %9.2f%%\n", + totalBlocksSize + hSize, + frameInfo.lz4FrameInfo.contentSize, + ratio); } + /* Now we've consumed frameInfo we can use it to store the total contentSize */ + frameInfo.lz4FrameInfo.contentSize += cfinfo->frameSummary.lz4FrameInfo.contentSize; } - } - } - } - } - } + else { + DISPLAYLEVEL(3, " %20llu %20s %9s \n", totalBlocksSize + hSize, "-", "-"); + cfinfo->allContentSize = 0; + } + result = LZ4IO_LZ4F_OK; + } } } } } break; case LEGACY_MAGICNUMBER: frameInfo.frameType = legacyFrame; @@ -1569,7 +1570,7 @@ LZ4IO_getCompressedFileInfo(LZ4IO_cFileInfo_t* cfinfo, const char* input_filenam if (cfinfo->frameSummary.frameType != skippableFrame && cfinfo->frameCount != 0) cfinfo->eqFrameTypes = 0; cfinfo->eqBlockTypes = 0; cfinfo->allContentSize = 0; - { nbReadBytes = fread(buffer, 1, 4, finput); + { size_t const nbReadBytes = fread(buffer, 1, 4, finput); if (nbReadBytes != 4) EXM_THROW(42, "Stream error : skippable size unreadable"); } @@ -1594,12 +1595,10 @@ LZ4IO_getCompressedFileInfo(LZ4IO_cFileInfo_t* cfinfo, const char* input_filenam } break; } - if (result != LZ4IO_LZ4F_OK) { - break; - } + if (result != LZ4IO_LZ4F_OK) break; cfinfo->frameSummary = frameInfo; cfinfo->frameCount++; - } + } /* while (!feof(finput)) */ fclose(finput); return result; } -- cgit v1.2.3 From 22729ecb8b062fc7d6c7f3e7f19fd31e70d78fa0 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 13 Nov 2020 11:55:33 -0800 Subject: minor refactor mostly on the compression code path --- programs/lz4io.c | 60 ++++++++++++++++++++++++++------------------------------ 1 file changed, 28 insertions(+), 32 deletions(-) diff --git a/programs/lz4io.c b/programs/lz4io.c index 2590d6d..baebd17 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -600,9 +600,7 @@ static LZ4F_CDict* LZ4IO_createCDict(LZ4IO_prefs_t* const prefs) { size_t dictionarySize; void* dictionaryBuffer; LZ4F_CDict* cdict; - if (!prefs->useDictionary) { - return NULL; - } + if (!prefs->useDictionary) return NULL; dictionaryBuffer = LZ4IO_createDict(prefs, &dictionarySize); if (!dictionaryBuffer) EXM_THROW(25, "Dictionary error : could not create dictionary"); cdict = LZ4F_createCDict(dictionaryBuffer, dictionarySize); @@ -654,7 +652,6 @@ LZ4IO_compressFilename_extRess(LZ4IO_prefs_t* const io_prefs, cRess_t ress, { unsigned long long filesize = 0; unsigned long long compressedfilesize = 0; - FILE* srcFile; FILE* dstFile; void* const srcBuffer = ress.srcBuffer; void* const dstBuffer = ress.dstBuffer; @@ -665,13 +662,12 @@ LZ4IO_compressFilename_extRess(LZ4IO_prefs_t* const io_prefs, cRess_t ress, LZ4F_preferences_t prefs; /* Init */ - srcFile = LZ4IO_openSrcFile(srcFileName); + FILE* const srcFile = LZ4IO_openSrcFile(srcFileName); if (srcFile == NULL) return 1; dstFile = LZ4IO_openDstFile(io_prefs, dstFileName); if (dstFile == NULL) { fclose(srcFile); return 1; } memset(&prefs, 0, sizeof(prefs)); - /* Set compression parameters */ prefs.autoFlush = 1; prefs.compressionLevel = compressionLevel; @@ -695,41 +691,41 @@ LZ4IO_compressFilename_extRess(LZ4IO_prefs_t* const io_prefs, cRess_t ress, /* single-block file */ if (readSize < blockSize) { /* Compress in single pass */ - size_t cSize = LZ4F_compressFrame_usingCDict(ctx, dstBuffer, dstBufferSize, srcBuffer, readSize, ress.cdict, &prefs); - if (LZ4F_isError(cSize)) EXM_THROW(31, "Compression failed : %s", LZ4F_getErrorName(cSize)); + size_t const cSize = LZ4F_compressFrame_usingCDict(ctx, dstBuffer, dstBufferSize, srcBuffer, readSize, ress.cdict, &prefs); + if (LZ4F_isError(cSize)) + EXM_THROW(31, "Compression failed : %s", LZ4F_getErrorName(cSize)); compressedfilesize = cSize; DISPLAYUPDATE(2, "\rRead : %u MB ==> %.2f%% ", (unsigned)(filesize>>20), (double)compressedfilesize/(filesize+!filesize)*100); /* avoid division by zero */ /* Write Block */ - { size_t const sizeCheck = fwrite(dstBuffer, 1, cSize, dstFile); - if (sizeCheck!=cSize) EXM_THROW(32, "Write error : cannot write compressed block"); + if (fwrite(dstBuffer, 1, cSize, dstFile) != cSize) { + EXM_THROW(32, "Write error : failed writing single-block compressed frame"); } } else /* multiple-blocks file */ { - /* Write Archive Header */ - size_t headerSize = LZ4F_compressBegin_usingCDict(ctx, dstBuffer, dstBufferSize, ress.cdict, &prefs); + /* Write Frame Header */ + size_t const headerSize = LZ4F_compressBegin_usingCDict(ctx, dstBuffer, dstBufferSize, ress.cdict, &prefs); if (LZ4F_isError(headerSize)) EXM_THROW(33, "File header generation failed : %s", LZ4F_getErrorName(headerSize)); - { size_t const sizeCheck = fwrite(dstBuffer, 1, headerSize, dstFile); - if (sizeCheck!=headerSize) EXM_THROW(34, "Write error : cannot write header"); } + if (fwrite(dstBuffer, 1, headerSize, dstFile) != headerSize) + EXM_THROW(34, "Write error : cannot write header"); compressedfilesize += headerSize; - /* Main Loop */ + /* Main Loop - one block at a time */ while (readSize>0) { - size_t outSize; - - /* Compress Block */ - outSize = LZ4F_compressUpdate(ctx, dstBuffer, dstBufferSize, srcBuffer, readSize, NULL); - if (LZ4F_isError(outSize)) EXM_THROW(35, "Compression failed : %s", LZ4F_getErrorName(outSize)); + size_t const outSize = LZ4F_compressUpdate(ctx, dstBuffer, dstBufferSize, srcBuffer, readSize, NULL); + if (LZ4F_isError(outSize)) + EXM_THROW(35, "Compression failed : %s", LZ4F_getErrorName(outSize)); compressedfilesize += outSize; - DISPLAYUPDATE(2, "\rRead : %u MB ==> %.2f%% ", (unsigned)(filesize>>20), (double)compressedfilesize/filesize*100); + DISPLAYUPDATE(2, "\rRead : %u MB ==> %.2f%% ", + (unsigned)(filesize>>20), (double)compressedfilesize/filesize*100); /* Write Block */ - { size_t const sizeCheck = fwrite(dstBuffer, 1, outSize, dstFile); - if (sizeCheck!=outSize) EXM_THROW(36, "Write error : cannot write compressed block"); } + if (fwrite(dstBuffer, 1, outSize, dstFile) != outSize) + EXM_THROW(36, "Write error : cannot write compressed block"); /* Read next block */ readSize = fread(srcBuffer, (size_t)1, (size_t)blockSize, srcFile); @@ -737,18 +733,18 @@ LZ4IO_compressFilename_extRess(LZ4IO_prefs_t* const io_prefs, cRess_t ress, } if (ferror(srcFile)) EXM_THROW(37, "Error reading %s ", srcFileName); - /* End of Stream mark */ - headerSize = LZ4F_compressEnd(ctx, dstBuffer, dstBufferSize, NULL); - if (LZ4F_isError(headerSize)) EXM_THROW(38, "End of file generation failed : %s", LZ4F_getErrorName(headerSize)); - - { size_t const sizeCheck = fwrite(dstBuffer, 1, headerSize, dstFile); - if (sizeCheck!=headerSize) EXM_THROW(39, "Write error : cannot write end of stream"); } - compressedfilesize += headerSize; - } + /* End of Frame mark */ + { size_t const endSize = LZ4F_compressEnd(ctx, dstBuffer, dstBufferSize, NULL); + if (LZ4F_isError(endSize)) + EXM_THROW(38, "End of frame error : %s", LZ4F_getErrorName(endSize)); + if (fwrite(dstBuffer, 1, endSize, dstFile) != endSize) + EXM_THROW(39, "Write error : cannot write end of frame"); + compressedfilesize += endSize; + } } /* Release file handlers */ fclose (srcFile); - if (strcmp(dstFileName,stdoutmark)) fclose (dstFile); /* do not close stdout */ + if (strcmp(dstFileName,stdoutmark)) fclose (dstFile); /* do not close stdout */ /* Copy owner, file permissions and modification time */ { stat_t statbuf; -- cgit v1.2.3 From f81f3a254e2dc0a2f951340c9723c7bc504118b6 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 13 Nov 2020 16:44:39 -0800 Subject: LZ4IO_fwriteSparse doesn't need writable prefs --- programs/lz4io.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/programs/lz4io.c b/programs/lz4io.c index baebd17..2bd0401 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -325,6 +325,7 @@ static FILE* LZ4IO_openSrcFile(const char* srcFileName) } /** FIO_openDstFile() : + * prefs is writable, because sparseFileSupport might be updated * condition : `dstFileName` must be non-NULL. * @result : FILE* to `dstFileName`, or NULL if it fails */ static FILE* LZ4IO_openDstFile(LZ4IO_prefs_t* const prefs, const char* dstFileName) @@ -858,7 +859,11 @@ static unsigned LZ4IO_readLE32 (const void* s) } -static unsigned LZ4IO_fwriteSparse(LZ4IO_prefs_t* const prefs, FILE* file, const void* buffer, size_t bufferSize, unsigned storedSkips) +static unsigned +LZ4IO_fwriteSparse(FILE* file, + const void* buffer, size_t bufferSize, + int sparseFileSupport, + unsigned storedSkips) { const size_t sizeT = sizeof(size_t); const size_t maskT = sizeT -1 ; @@ -868,7 +873,7 @@ static unsigned LZ4IO_fwriteSparse(LZ4IO_prefs_t* const prefs, FILE* file, const const size_t* const bufferTEnd = bufferT + bufferSizeT; const size_t segmentSizeT = (32 KB) / sizeT; - if (!prefs->sparseFileSupport) { /* normal write */ + if (!sparseFileSupport) { /* normal write */ size_t const sizeCheck = fwrite(buffer, 1, bufferSize, file); if (sizeCheck != bufferSize) EXM_THROW(70, "Write error : cannot write decoded block"); return 0; @@ -971,7 +976,7 @@ static unsigned long long LZ4IO_decodeLegacyStream(LZ4IO_prefs_t* const prefs, F if (decodeSize < 0) EXM_THROW(53, "Decoding Failed ! Corrupted input detected !"); streamSize += (unsigned long long)decodeSize; /* Write Block */ - storedSkips = LZ4IO_fwriteSparse(prefs, foutput, out_buff, (size_t)decodeSize, storedSkips); /* success or die */ + storedSkips = LZ4IO_fwriteSparse(foutput, out_buff, (size_t)decodeSize, prefs->sparseFileSupport, storedSkips); /* success or die */ } } if (ferror(finput)) EXM_THROW(54, "Read error : ferror"); @@ -1076,7 +1081,7 @@ static unsigned long long LZ4IO_decompressLZ4F(LZ4IO_prefs_t* const prefs, dRess /* Write Block */ if (decodedBytes) { if (!prefs->testMode) - storedSkips = LZ4IO_fwriteSparse(prefs, dstFile, ress.dstBuffer, decodedBytes, storedSkips); + storedSkips = LZ4IO_fwriteSparse(dstFile, ress.dstBuffer, decodedBytes, prefs->sparseFileSupport, storedSkips); filesize += decodedBytes; DISPLAYUPDATE(2, "\rDecompressed : %u MB ", (unsigned)(filesize>>20)); } @@ -1112,7 +1117,7 @@ LZ4IO_passThrough(LZ4IO_prefs_t* const prefs, while (readBytes) { readBytes = fread(buffer, 1, PTSIZE, finput); total += readBytes; - storedSkips = LZ4IO_fwriteSparse(prefs, foutput, buffer, readBytes, storedSkips); + storedSkips = LZ4IO_fwriteSparse(foutput, buffer, readBytes, prefs->sparseFileSupport, storedSkips); } if (ferror(finput)) EXM_THROW(51, "Read Error"); -- cgit v1.2.3 From a203cb2a6eda7b794ea648ee426704041c119e1d Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 13 Nov 2020 20:51:49 -0800 Subject: LZ4IO_decodeLegacyStream() doesn't need mutable prefs --- programs/lz4io.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/programs/lz4io.c b/programs/lz4io.c index 2bd0401..3f85c56 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -931,18 +931,18 @@ LZ4IO_fwriteSparse(FILE* file, static void LZ4IO_fwriteSparseEnd(FILE* file, unsigned storedSkips) { - if (storedSkips>0) { /* implies g_sparseFileSupport>0 */ - int const seekResult = UTIL_fseek(file, storedSkips-1, SEEK_CUR); - if (seekResult != 0) EXM_THROW(69, "Final skip error (sparse file)\n"); - { const char lastZeroByte[1] = { 0 }; - size_t const sizeCheck = fwrite(lastZeroByte, 1, 1, file); - if (sizeCheck != 1) EXM_THROW(69, "Write error : cannot write last zero\n"); - } } + if (storedSkips>0) { /* implies sparseFileSupport>0 */ + const char lastZeroByte[1] = { 0 }; + if (UTIL_fseek(file, storedSkips-1, SEEK_CUR) != 0) + EXM_THROW(69, "Final skip error (sparse file)\n"); + if (fwrite(lastZeroByte, 1, 1, file) != 1) + EXM_THROW(69, "Write error : cannot write last zero\n"); + } } static unsigned g_magicRead = 0; /* out-parameter of LZ4IO_decodeLegacyStream() */ -static unsigned long long LZ4IO_decodeLegacyStream(LZ4IO_prefs_t* const prefs, FILE* finput, FILE* foutput) +static unsigned long long LZ4IO_decodeLegacyStream(FILE* finput, FILE* foutput, const LZ4IO_prefs_t* prefs) { unsigned long long streamSize = 0; unsigned storedSkips = 0; @@ -1173,7 +1173,7 @@ static unsigned long long selectDecoder(LZ4IO_prefs_t* const prefs, dRess_t ress return LZ4IO_decompressLZ4F(prefs, ress, finput, foutput); case LEGACY_MAGICNUMBER: DISPLAYLEVEL(4, "Detected : Legacy format \n"); - return LZ4IO_decodeLegacyStream(prefs, finput, foutput); + return LZ4IO_decodeLegacyStream(finput, foutput, prefs); case LZ4IO_SKIPPABLE0: DISPLAYLEVEL(4, "Skipping detected skippable area \n"); { size_t const nbReadBytes = fread(MNstore, 1, 4, finput); -- cgit v1.2.3 From c8c3f8e62e8036e429732c4b954b37d34f80568a Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 13 Nov 2020 20:57:26 -0800 Subject: LZ4IO_decompressLZ4F() doesn't need mutable prefs --- programs/lz4io.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/programs/lz4io.c b/programs/lz4io.c index 3f85c56..3753271 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -1045,7 +1045,10 @@ static void LZ4IO_freeDResources(dRess_t ress) } -static unsigned long long LZ4IO_decompressLZ4F(LZ4IO_prefs_t* const prefs, dRess_t ress, FILE* srcFile, FILE* dstFile) +static unsigned long long +LZ4IO_decompressLZ4F(dRess_t ress, + FILE* const srcFile, FILE* const dstFile, + const LZ4IO_prefs_t* const prefs) { unsigned long long filesize = 0; LZ4F_errorCode_t nextToLoad; @@ -1144,7 +1147,10 @@ static int fseek_u32(FILE *fp, unsigned offset, int where) } #define ENDOFSTREAM ((unsigned long long)-1) -static unsigned long long selectDecoder(LZ4IO_prefs_t* const prefs, dRess_t ress, FILE* finput, FILE* foutput) +static unsigned long long +selectDecoder(LZ4IO_prefs_t* const prefs, + dRess_t ress, + FILE* finput, FILE* foutput) { unsigned char MNstore[MAGICNUMBER_SIZE]; unsigned magicNumber; @@ -1170,7 +1176,7 @@ static unsigned long long selectDecoder(LZ4IO_prefs_t* const prefs, dRess_t ress switch(magicNumber) { case LZ4IO_MAGICNUMBER: - return LZ4IO_decompressLZ4F(prefs, ress, finput, foutput); + return LZ4IO_decompressLZ4F(ress, finput, foutput, prefs); case LEGACY_MAGICNUMBER: DISPLAYLEVEL(4, "Detected : Legacy format \n"); return LZ4IO_decodeLegacyStream(finput, foutput, prefs); @@ -1207,7 +1213,10 @@ static unsigned long long selectDecoder(LZ4IO_prefs_t* const prefs, dRess_t ress } -static int LZ4IO_decompressSrcFile(LZ4IO_prefs_t* const prefs, dRess_t ress, const char* input_filename, const char* output_filename) +static int +LZ4IO_decompressSrcFile(LZ4IO_prefs_t* const prefs, + dRess_t ress, + const char* input_filename, const char* output_filename) { FILE* const foutput = ress.dstFile; unsigned long long filesize = 0; @@ -1215,6 +1224,7 @@ static int LZ4IO_decompressSrcFile(LZ4IO_prefs_t* const prefs, dRess_t ress, con /* Init */ FILE* const finput = LZ4IO_openSrcFile(input_filename); if (finput==NULL) return 1; + assert(foutput != NULL); /* Loop over multiple streams */ for ( ; ; ) { /* endless loop, see break condition */ -- cgit v1.2.3 From 8d37662e481e95527f9ecf97abbb160f0a674cad Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 13 Nov 2020 21:47:17 -0800 Subject: LZ4IO_passThrough() doesn't need prefs --- programs/lz4io.c | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/programs/lz4io.c b/programs/lz4io.c index 3753271..9395037 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -1102,25 +1102,30 @@ LZ4IO_decompressLZ4F(dRess_t ress, } +/* LZ4IO_passThrough: + * just output the same content as input, no decoding. + * This is a capability of zcat, and by extension lz4cat + * MNstore : contain the first MAGICNUMBER_SIZE bytes already read from finput + */ #define PTSIZE (64 KB) #define PTSIZET (PTSIZE / sizeof(size_t)) static unsigned long long -LZ4IO_passThrough(LZ4IO_prefs_t* const prefs, - FILE* finput, FILE* foutput, - unsigned char MNstore[MAGICNUMBER_SIZE]) +LZ4IO_passThrough(FILE* finput, FILE* foutput, + unsigned char MNstore[MAGICNUMBER_SIZE], + int sparseFileSupport) { size_t buffer[PTSIZET]; size_t readBytes = 1; unsigned long long total = MAGICNUMBER_SIZE; unsigned storedSkips = 0; - size_t const sizeCheck = fwrite(MNstore, 1, MAGICNUMBER_SIZE, foutput); - if (sizeCheck != MAGICNUMBER_SIZE) EXM_THROW(50, "Pass-through write error"); - + if (fwrite(MNstore, 1, MAGICNUMBER_SIZE, foutput) != MAGICNUMBER_SIZE) { + EXM_THROW(50, "Pass-through write error"); + } while (readBytes) { - readBytes = fread(buffer, 1, PTSIZE, finput); + readBytes = fread(buffer, 1, sizeof(buffer), finput); total += readBytes; - storedSkips = LZ4IO_fwriteSparse(foutput, buffer, readBytes, prefs->sparseFileSupport, storedSkips); + storedSkips = LZ4IO_fwriteSparse(foutput, buffer, readBytes, sparseFileSupport, storedSkips); } if (ferror(finput)) EXM_THROW(51, "Read Error"); @@ -1198,7 +1203,7 @@ selectDecoder(LZ4IO_prefs_t* const prefs, /* Wrong magic number at the beginning of 1st stream */ if (!prefs->testMode && prefs->overwrite && prefs->passThrough) { nbFrames = 0; - return LZ4IO_passThrough(prefs, finput, foutput, MNstore); + return LZ4IO_passThrough(finput, foutput, MNstore, prefs->sparseFileSupport); } EXM_THROW(44,"Unrecognized header : file cannot be decoded"); } -- cgit v1.2.3 From 2b3445a2692967267c9289cc4ff0af7a14bcc1c5 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 13 Nov 2020 21:49:43 -0800 Subject: selectDecoder() doesn't need mutable prefs --- programs/lz4io.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/programs/lz4io.c b/programs/lz4io.c index 9395037..842976e 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -1153,9 +1153,9 @@ static int fseek_u32(FILE *fp, unsigned offset, int where) #define ENDOFSTREAM ((unsigned long long)-1) static unsigned long long -selectDecoder(LZ4IO_prefs_t* const prefs, - dRess_t ress, - FILE* finput, FILE* foutput) +selectDecoder(dRess_t ress, + FILE* finput, FILE* foutput, + const LZ4IO_prefs_t* const prefs) { unsigned char MNstore[MAGICNUMBER_SIZE]; unsigned magicNumber; @@ -1234,7 +1234,7 @@ LZ4IO_decompressSrcFile(LZ4IO_prefs_t* const prefs, /* Loop over multiple streams */ for ( ; ; ) { /* endless loop, see break condition */ unsigned long long const decodedSize = - selectDecoder(prefs, ress, finput, foutput); + selectDecoder(ress, finput, foutput, prefs); if (decodedSize == ENDOFSTREAM) break; filesize += decodedSize; } -- cgit v1.2.3 From 8af38013e0161e829f463a73f340a4da7a8b2a5d Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 13 Nov 2020 21:51:08 -0800 Subject: LZ4IO_decompressSrcFile() doesn't need mutable prefs --- programs/lz4io.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/programs/lz4io.c b/programs/lz4io.c index 842976e..d243224 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -1219,9 +1219,9 @@ selectDecoder(dRess_t ress, static int -LZ4IO_decompressSrcFile(LZ4IO_prefs_t* const prefs, - dRess_t ress, - const char* input_filename, const char* output_filename) +LZ4IO_decompressSrcFile(dRess_t ress, + const char* input_filename, const char* output_filename, + const LZ4IO_prefs_t* const prefs) { FILE* const foutput = ress.dstFile; unsigned long long filesize = 0; @@ -1267,7 +1267,7 @@ static int LZ4IO_decompressDstFile(LZ4IO_prefs_t* const prefs, dRess_t ress, con stat_result = 1; ress.dstFile = foutput; - LZ4IO_decompressSrcFile(prefs, ress, input_filename, output_filename); + LZ4IO_decompressSrcFile(ress, input_filename, output_filename, prefs); fclose(foutput); @@ -1318,7 +1318,7 @@ int LZ4IO_decompressMultipleFilenames(LZ4IO_prefs_t* const prefs, size_t const ifnSize = strlen(inFileNamesTable[i]); const char* const suffixPtr = inFileNamesTable[i] + ifnSize - suffixSize; if (!strcmp(suffix, stdoutmark)) { - missingFiles += LZ4IO_decompressSrcFile(prefs, ress, inFileNamesTable[i], stdoutmark); + missingFiles += LZ4IO_decompressSrcFile(ress, inFileNamesTable[i], stdoutmark, prefs); continue; } if (ofnSize <= ifnSize-suffixSize+1) { -- cgit v1.2.3 From 772a312b00ad7b47967e0c4d62e742a86d8bd2cb Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 13 Nov 2020 22:16:39 -0800 Subject: LZ4IO_createDict() doesn't need prefs --- programs/lz4io.c | 57 +++++++++++++++++++++++++++++--------------------------- tests/Makefile | 2 +- 2 files changed, 31 insertions(+), 28 deletions(-) diff --git a/programs/lz4io.c b/programs/lz4io.c index d243224..27e8979 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -325,7 +325,7 @@ static FILE* LZ4IO_openSrcFile(const char* srcFileName) } /** FIO_openDstFile() : - * prefs is writable, because sparseFileSupport might be updated + * prefs is writable, because sparseFileSupport might be updated. * condition : `dstFileName` must be non-NULL. * @result : FILE* to `dstFileName`, or NULL if it fails */ static FILE* LZ4IO_openDstFile(LZ4IO_prefs_t* const prefs, const char* dstFileName) @@ -334,18 +334,19 @@ static FILE* LZ4IO_openDstFile(LZ4IO_prefs_t* const prefs, const char* dstFileNa assert(dstFileName != NULL); if (!strcmp (dstFileName, stdoutmark)) { - DISPLAYLEVEL(4,"Using stdout for output\n"); + DISPLAYLEVEL(4, "Using stdout for output \n"); f = stdout; SET_BINARY_MODE(stdout); if (prefs->sparseFileSupport==1) { prefs->sparseFileSupport = 0; - DISPLAYLEVEL(4, "Sparse File Support is automatically disabled on stdout ; try --sparse \n"); + DISPLAYLEVEL(4, "Sparse File Support automatically disabled on stdout ;" + " to force-enable it, add --sparse command \n"); } } else { if (!prefs->overwrite && strcmp (dstFileName, nulmark)) { /* Check if destination file already exists */ - f = fopen( dstFileName, "rb" ); - if (f != NULL) { /* dest exists, prompt for overwrite authorization */ - fclose(f); + FILE* const testf = fopen( dstFileName, "rb" ); + if (testf != NULL) { /* dest exists, prompt for overwrite authorization */ + fclose(testf); if (g_displayLevel <= 1) { /* No interaction possible */ DISPLAY("%s already exists; not overwritten \n", dstFileName); return NULL; @@ -393,7 +394,9 @@ static int LZ4IO_LZ4_compress(const char* src, char* dst, int srcSize, int dstSi /* LZ4IO_compressFilename_Legacy : * This function is intentionally "hidden" (not published in .h) * It generates compressed streams using the old 'legacy' format */ -int LZ4IO_compressFilename_Legacy(LZ4IO_prefs_t* const prefs, const char* input_filename, const char* output_filename, int compressionlevel) +int LZ4IO_compressFilename_Legacy(LZ4IO_prefs_t* const prefs, + const char* input_filename, const char* output_filename, + int compressionlevel) { typedef int (*compress_f)(const char* src, char* dst, int srcSize, int dstSize, int cLevel); compress_f const compressionFunction = (compressionlevel < 3) ? LZ4IO_LZ4_compress : LZ4_compress_HC; @@ -425,18 +428,16 @@ int LZ4IO_compressFilename_Legacy(LZ4IO_prefs_t* const prefs, const char* input_ /* Write Archive Header */ LZ4IO_writeLE32(out_buff, LEGACY_MAGICNUMBER); - { size_t const writeSize = fwrite(out_buff, 1, MAGICNUMBER_SIZE, foutput); - if (writeSize != MAGICNUMBER_SIZE) - EXM_THROW(22, "Write error : cannot write header"); - } + if (fwrite(out_buff, 1, MAGICNUMBER_SIZE, foutput) != MAGICNUMBER_SIZE) + EXM_THROW(22, "Write error : cannot write header"); /* Main Loop */ while (1) { int outSize; /* Read Block */ size_t const inSize = fread(in_buff, (size_t)1, (size_t)LEGACY_BLOCKSIZE, finput); - assert(inSize <= LEGACY_BLOCKSIZE); if (inSize == 0) break; + assert(inSize <= LEGACY_BLOCKSIZE); filesize += inSize; /* Compress Block */ @@ -450,9 +451,8 @@ int LZ4IO_compressFilename_Legacy(LZ4IO_prefs_t* const prefs, const char* input_ assert(outSize > 0); assert(outSize < outBuffSize); LZ4IO_writeLE32(out_buff, (unsigned)outSize); - { size_t const writeSize = fwrite(out_buff, 1, (size_t)outSize+4, foutput); - if (writeSize != (size_t)(outSize+4)) - EXM_THROW(24, "Write error : cannot write compressed block"); + if (fwrite(out_buff, 1, (size_t)outSize+4, foutput) != (size_t)(outSize+4)) { + EXM_THROW(24, "Write error : cannot write compressed block"); } } if (ferror(finput)) EXM_THROW(25, "Error while reading %s ", input_filename); @@ -539,22 +539,20 @@ typedef struct { LZ4F_CDict* cdict; } cRess_t; -static void* LZ4IO_createDict(LZ4IO_prefs_t* const prefs, size_t *dictSize) { +static void* LZ4IO_createDict(size_t* dictSize, const char* const dictFilename) +{ size_t readSize; size_t dictEnd = 0; size_t dictLen = 0; size_t dictStart; size_t circularBufSize = LZ4_MAX_DICT_SIZE; - char* circularBuf; - char* dictBuf; - const char* dictFilename = prefs->dictionaryFilename; + char* circularBuf = (char*)malloc(circularBufSize); + char* dictBuf; FILE* dictFile; + if (!circularBuf) EXM_THROW(25, "Allocation error : not enough memory for circular buffer"); if (!dictFilename) EXM_THROW(25, "Dictionary error : no filename provided"); - circularBuf = (char *) malloc(circularBufSize); - if (!circularBuf) EXM_THROW(25, "Allocation error : not enough memory"); - dictFile = LZ4IO_openSrcFile(dictFilename); if (!dictFile) EXM_THROW(25, "Dictionary error : could not open dictionary file"); @@ -584,7 +582,7 @@ static void* LZ4IO_createDict(LZ4IO_prefs_t* const prefs, size_t *dictSize) { circularBuf = NULL; } else { /* Otherwise, we will alloc a new buffer and copy our dict into that. */ - dictBuf = (char *) malloc(dictLen ? dictLen : 1); + dictBuf = (char *)malloc(dictLen ? dictLen : 1); if (!dictBuf) EXM_THROW(25, "Allocation error : not enough memory"); memcpy(dictBuf, circularBuf + dictStart, circularBufSize - dictStart); @@ -602,7 +600,7 @@ static LZ4F_CDict* LZ4IO_createCDict(LZ4IO_prefs_t* const prefs) { void* dictionaryBuffer; LZ4F_CDict* cdict; if (!prefs->useDictionary) return NULL; - dictionaryBuffer = LZ4IO_createDict(prefs, &dictionarySize); + dictionaryBuffer = LZ4IO_createDict(&dictionarySize, prefs->dictionaryFilename); if (!dictionaryBuffer) EXM_THROW(25, "Dictionary error : could not create dictionary"); cdict = LZ4F_createCDict(dictionaryBuffer, dictionarySize); free(dictionaryBuffer); @@ -643,6 +641,7 @@ static void LZ4IO_freeCResources(cRess_t ress) /* * LZ4IO_compressFilename_extRess() + * io_prefs is mutable, as it may update sparseFileSupport * result : 0 : compression completed correctly * 1 : missing or pb opening srcFileName */ @@ -1002,14 +1001,15 @@ typedef struct { size_t dictBufferSize; } dRess_t; -static void LZ4IO_loadDDict(LZ4IO_prefs_t* const prefs, dRess_t* ress) { +static void LZ4IO_loadDDict(LZ4IO_prefs_t* const prefs, dRess_t* ress) +{ if (!prefs->useDictionary) { ress->dictBuffer = NULL; ress->dictBufferSize = 0; return; } - ress->dictBuffer = LZ4IO_createDict(prefs, &ress->dictBufferSize); + ress->dictBuffer = LZ4IO_createDict(&ress->dictBufferSize, prefs->dictionaryFilename); if (!ress->dictBuffer) EXM_THROW(25, "Dictionary error : could not create dictionary"); } @@ -1255,7 +1255,10 @@ LZ4IO_decompressSrcFile(dRess_t ress, } -static int LZ4IO_decompressDstFile(LZ4IO_prefs_t* const prefs, dRess_t ress, const char* input_filename, const char* output_filename) +static int +LZ4IO_decompressDstFile(LZ4IO_prefs_t* const prefs, + dRess_t ress, + const char* input_filename, const char* output_filename) { stat_t statbuf; int stat_result = 0; diff --git a/tests/Makefile b/tests/Makefile index 476849e..6eee132 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -420,7 +420,7 @@ test-lz4-hugefile: lz4 datagen test-lz4-testmode: lz4 datagen @echo "\n ---- bench mode ----" - $(LZ4) -bi1 + $(LZ4) -bi0 @echo "\n ---- test mode ----" ! $(DATAGEN) | $(LZ4) -t ! $(DATAGEN) | $(LZ4) -tf -- cgit v1.2.3 From 5fa6417b2e3ecb9d6434c632b39547b58e5ea12f Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 13 Nov 2020 22:20:49 -0800 Subject: dictionary functions don't need prefs mutability --- programs/lz4io.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/programs/lz4io.c b/programs/lz4io.c index 27e8979..975f590 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -595,7 +595,8 @@ static void* LZ4IO_createDict(size_t* dictSize, const char* const dictFilename) return dictBuf; } -static LZ4F_CDict* LZ4IO_createCDict(LZ4IO_prefs_t* const prefs) { +static LZ4F_CDict* LZ4IO_createCDict(const LZ4IO_prefs_t* const prefs) +{ size_t dictionarySize; void* dictionaryBuffer; LZ4F_CDict* cdict; @@ -1001,7 +1002,7 @@ typedef struct { size_t dictBufferSize; } dRess_t; -static void LZ4IO_loadDDict(LZ4IO_prefs_t* const prefs, dRess_t* ress) +static void LZ4IO_loadDDict(dRess_t* ress, const LZ4IO_prefs_t* const prefs) { if (!prefs->useDictionary) { ress->dictBuffer = NULL; @@ -1014,7 +1015,7 @@ static void LZ4IO_loadDDict(LZ4IO_prefs_t* const prefs, dRess_t* ress) } static const size_t LZ4IO_dBufferSize = 64 KB; -static dRess_t LZ4IO_createDResources(LZ4IO_prefs_t* const prefs) +static dRess_t LZ4IO_createDResources(const LZ4IO_prefs_t* const prefs) { dRess_t ress; @@ -1029,7 +1030,7 @@ static dRess_t LZ4IO_createDResources(LZ4IO_prefs_t* const prefs) ress.dstBuffer = malloc(ress.dstBufferSize); if (!ress.srcBuffer || !ress.dstBuffer) EXM_THROW(61, "Allocation error : not enough memory"); - LZ4IO_loadDDict(prefs, &ress); + LZ4IO_loadDDict(&ress, prefs); ress.dstFile = NULL; return ress; -- cgit v1.2.3 From 849ebae524c85c102aad9b085c6725bd20de6a32 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sat, 14 Nov 2020 10:52:19 -0800 Subject: fixed force --sparse mode --- programs/lz4io.c | 2 +- tmp | Bin 0 -> 65536 bytes tmpsparse | Bin 0 -> 65536 bytes 3 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 tmp create mode 100644 tmpsparse diff --git a/programs/lz4io.c b/programs/lz4io.c index 975f590..36be639 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -272,7 +272,7 @@ int LZ4IO_setNotificationLevel(int level) /* Default setting : 0 (disabled) */ int LZ4IO_setSparseFile(LZ4IO_prefs_t* const prefs, int enable) { - prefs->sparseFileSupport = (enable!=0); + prefs->sparseFileSupport = 2*(enable!=0); return prefs->sparseFileSupport; } diff --git a/tmp b/tmp new file mode 100644 index 0000000..c97c12f Binary files /dev/null and b/tmp differ diff --git a/tmpsparse b/tmpsparse new file mode 100644 index 0000000..c97c12f Binary files /dev/null and b/tmpsparse differ -- cgit v1.2.3 From 7315c6e7a00494b2b8618379209732260686524f Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sat, 14 Nov 2020 11:13:08 -0800 Subject: LZ4IO_openDstFile() doesn't need pref mutability --- programs/lz4io.c | 63 ++++++++++++++++++++++++++++------------------------- programs/platform.h | 6 ++--- 2 files changed, 36 insertions(+), 33 deletions(-) diff --git a/programs/lz4io.c b/programs/lz4io.c index 36be639..302d747 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -159,28 +159,28 @@ struct LZ4IO_prefs_s { LZ4IO_prefs_t* LZ4IO_defaultPreferences(void) { - LZ4IO_prefs_t* const ret = (LZ4IO_prefs_t*)malloc(sizeof(LZ4IO_prefs_t)); - if (!ret) EXM_THROW(21, "Allocation error : not enough memory"); - ret->passThrough = 0; - ret->overwrite = 1; - ret->testMode = 0; - ret->blockSizeId = LZ4IO_BLOCKSIZEID_DEFAULT; - ret->blockSize = 0; - ret->blockChecksum = 0; - ret->streamChecksum = 1; - ret->blockIndependence = 1; - ret->sparseFileSupport = 1; - ret->contentSizeFlag = 0; - ret->useDictionary = 0; - ret->favorDecSpeed = 0; - ret->dictionaryFilename = NULL; - ret->removeSrcFile = 0; - return ret; + LZ4IO_prefs_t* const ret = (LZ4IO_prefs_t*)malloc(sizeof(*ret)); + if (!ret) EXM_THROW(21, "Allocation error : not enough memory"); + ret->passThrough = 0; + ret->overwrite = 1; + ret->testMode = 0; + ret->blockSizeId = LZ4IO_BLOCKSIZEID_DEFAULT; + ret->blockSize = 0; + ret->blockChecksum = 0; + ret->streamChecksum = 1; + ret->blockIndependence = 1; + ret->sparseFileSupport = 1; + ret->contentSizeFlag = 0; + ret->useDictionary = 0; + ret->favorDecSpeed = 0; + ret->dictionaryFilename = NULL; + ret->removeSrcFile = 0; + return ret; } void LZ4IO_freePreferences(LZ4IO_prefs_t* const prefs) { - free(prefs); + free(prefs); } @@ -242,20 +242,21 @@ size_t LZ4IO_setBlockSize(LZ4IO_prefs_t* const prefs, size_t blockSize) return prefs->blockSize; } +/* Default setting : 1 == independent blocks */ int LZ4IO_setBlockMode(LZ4IO_prefs_t* const prefs, LZ4IO_blockMode_t blockMode) { prefs->blockIndependence = (blockMode == LZ4IO_blockIndependent); return prefs->blockIndependence; } -/* Default setting : no block checksum */ +/* Default setting : 0 == no block checksum */ int LZ4IO_setBlockChecksumMode(LZ4IO_prefs_t* const prefs, int enable) { prefs->blockChecksum = (enable != 0); return prefs->blockChecksum; } -/* Default setting : checksum enabled */ +/* Default setting : 1 == checksum enabled */ int LZ4IO_setStreamChecksumMode(LZ4IO_prefs_t* const prefs, int enable) { prefs->streamChecksum = (enable != 0); @@ -269,10 +270,10 @@ int LZ4IO_setNotificationLevel(int level) return g_displayLevel; } -/* Default setting : 0 (disabled) */ +/* Default setting : 1 (auto: enabled on file, disabled on stdout) */ int LZ4IO_setSparseFile(LZ4IO_prefs_t* const prefs, int enable) { - prefs->sparseFileSupport = 2*(enable!=0); + prefs->sparseFileSupport = 2*(enable!=0); /* 2==force enable */ return prefs->sparseFileSupport; } @@ -328,7 +329,7 @@ static FILE* LZ4IO_openSrcFile(const char* srcFileName) * prefs is writable, because sparseFileSupport might be updated. * condition : `dstFileName` must be non-NULL. * @result : FILE* to `dstFileName`, or NULL if it fails */ -static FILE* LZ4IO_openDstFile(LZ4IO_prefs_t* const prefs, const char* dstFileName) +static FILE* LZ4IO_openDstFile(const char* dstFileName, const LZ4IO_prefs_t* const prefs) { FILE* f; assert(dstFileName != NULL); @@ -338,7 +339,6 @@ static FILE* LZ4IO_openDstFile(LZ4IO_prefs_t* const prefs, const char* dstFileNa f = stdout; SET_BINARY_MODE(stdout); if (prefs->sparseFileSupport==1) { - prefs->sparseFileSupport = 0; DISPLAYLEVEL(4, "Sparse File Support automatically disabled on stdout ;" " to force-enable it, add --sparse command \n"); } @@ -364,7 +364,9 @@ static FILE* LZ4IO_openDstFile(LZ4IO_prefs_t* const prefs, const char* dstFileNa } /* sparse file */ - if (f && prefs->sparseFileSupport) { SET_SPARSE_FILE_MODE(f); } + { int const sparseMode = (prefs->sparseFileSupport - (f==stdout)) > 0; + if (f && sparseMode) SET_SPARSE_FILE_MODE(f); + } return f; } @@ -414,7 +416,7 @@ int LZ4IO_compressFilename_Legacy(LZ4IO_prefs_t* const prefs, if (finput == NULL) EXM_THROW(20, "%s : open file error ", input_filename); - foutput = LZ4IO_openDstFile(prefs, output_filename); + foutput = LZ4IO_openDstFile(output_filename, prefs); if (foutput == NULL) { fclose(finput); EXM_THROW(20, "%s : open file error ", input_filename); @@ -665,7 +667,7 @@ LZ4IO_compressFilename_extRess(LZ4IO_prefs_t* const io_prefs, cRess_t ress, /* Init */ FILE* const srcFile = LZ4IO_openSrcFile(srcFileName); if (srcFile == NULL) return 1; - dstFile = LZ4IO_openDstFile(io_prefs, dstFileName); + dstFile = LZ4IO_openDstFile(dstFileName, io_prefs); if (dstFile == NULL) { fclose(srcFile); return 1; } memset(&prefs, 0, sizeof(prefs)); @@ -872,8 +874,9 @@ LZ4IO_fwriteSparse(FILE* file, size_t bufferSizeT = bufferSize / sizeT; const size_t* const bufferTEnd = bufferT + bufferSizeT; const size_t segmentSizeT = (32 KB) / sizeT; + int const sparseMode = (sparseFileSupport - (file==stdout)) > 0; - if (!sparseFileSupport) { /* normal write */ + if (!sparseMode) { /* normal write */ size_t const sizeCheck = fwrite(buffer, 1, bufferSize, file); if (sizeCheck != bufferSize) EXM_THROW(70, "Write error : cannot write decoded block"); return 0; @@ -1263,7 +1266,7 @@ LZ4IO_decompressDstFile(LZ4IO_prefs_t* const prefs, { stat_t statbuf; int stat_result = 0; - FILE* const foutput = LZ4IO_openDstFile(prefs, output_filename); + FILE* const foutput = LZ4IO_openDstFile(output_filename, prefs); if (foutput==NULL) return 1; /* failure */ if ( strcmp(input_filename, stdinmark) @@ -1316,7 +1319,7 @@ int LZ4IO_decompressMultipleFilenames(LZ4IO_prefs_t* const prefs, dRess_t ress = LZ4IO_createDResources(prefs); if (outFileName==NULL) EXM_THROW(70, "Memory allocation error"); - ress.dstFile = LZ4IO_openDstFile(prefs, stdoutmark); + ress.dstFile = LZ4IO_openDstFile(stdoutmark, prefs); for (i=0; i= 1 is equal to found _POSIX_VERSION -***************************************************************/ +************************************************************** */ #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)) /* UNIX-like OS */ \ || defined(__midipix__) || defined(__VMS)) # if (defined(__APPLE__) && defined(__MACH__)) || defined(__SVR4) || defined(_AIX) || defined(__hpux) /* POSIX.1–2001 (SUSv3) conformant */ \ @@ -105,7 +105,7 @@ extern "C" { /*-********************************************* * Detect if isatty() and fileno() are available -************************************************/ +*********************************************** */ #if (defined(__linux__) && (PLATFORM_POSIX_VERSION >= 1)) || (PLATFORM_POSIX_VERSION >= 200112L) || defined(__DJGPP__) # include /* isatty */ # define IS_CONSOLE(stdStream) isatty(fileno(stdStream)) @@ -128,7 +128,7 @@ static __inline int IS_CONSOLE(FILE* stdStream) /****************************** * OS-specific Includes -******************************/ +***************************** */ #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(_WIN32) # include /* _O_BINARY */ # include /* _setmode, _fileno, _get_osfhandle */ -- cgit v1.2.3 From 36eadb205ae1fda0d92b9e4c0c1eb1ee79e7b943 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sat, 14 Nov 2020 12:40:17 -0800 Subject: all usages of prefs within lz4io are const --- programs/lz4cli.c | 21 +++++++++++-------- programs/lz4io.c | 62 +++++++++++++++++++++++++++---------------------------- programs/lz4io.h | 10 ++++----- 3 files changed, 48 insertions(+), 45 deletions(-) diff --git a/programs/lz4cli.c b/programs/lz4cli.c index d7d4f81..523b8a8 100644 --- a/programs/lz4cli.c +++ b/programs/lz4cli.c @@ -93,11 +93,11 @@ static unsigned displayLevel = 2; /* 0 : no display ; 1: errors only ; 2 : dow ***************************************/ #define DEFAULT_COMPRESSOR LZ4IO_compressFilename #define DEFAULT_DECOMPRESSOR LZ4IO_decompressFilename -int LZ4IO_compressFilename_Legacy(LZ4IO_prefs_t* const prefs, const char* input_filename, const char* output_filename, int compressionlevel); /* hidden function */ -int LZ4IO_compressMultipleFilenames_Legacy(LZ4IO_prefs_t* const prefs, +int LZ4IO_compressFilename_Legacy(const char* input_filename, const char* output_filename, int compressionlevel, const LZ4IO_prefs_t* prefs); /* hidden function */ +int LZ4IO_compressMultipleFilenames_Legacy( const char** inFileNamesTable, int ifntSize, const char* suffix, - int compressionLevel); + int compressionLevel, const LZ4IO_prefs_t* prefs); /*-*************************** * Functions @@ -747,10 +747,11 @@ int main(int argc, const char** argv) if (ifnIdx == 0) multiple_inputs = 0; if (mode == om_decompress) { if (multiple_inputs) { + const char* const dec_extension = !strcmp(output_filename,stdoutmark) ? stdoutmark : LZ4_EXTENSION; assert(ifnIdx <= INT_MAX); - operationResult = LZ4IO_decompressMultipleFilenames(prefs, inFileNames, (int)ifnIdx, !strcmp(output_filename,stdoutmark) ? stdoutmark : LZ4_EXTENSION); + operationResult = LZ4IO_decompressMultipleFilenames(inFileNames, (int)ifnIdx, dec_extension, prefs); } else { - operationResult = DEFAULT_DECOMPRESSOR(prefs, input_filename, output_filename); + operationResult = DEFAULT_DECOMPRESSOR(input_filename, output_filename, prefs); } } else if (mode == om_list){ operationResult = LZ4IO_displayCompressedFilesInfo(inFileNames, ifnIdx); @@ -758,16 +759,18 @@ int main(int argc, const char** argv) if (legacy_format) { DISPLAYLEVEL(3, "! Generating LZ4 Legacy format (deprecated) ! \n"); if(multiple_inputs){ - LZ4IO_compressMultipleFilenames_Legacy(prefs, inFileNames, (int)ifnIdx, !strcmp(output_filename,stdoutmark) ? stdoutmark : LZ4_EXTENSION, cLevel); + const char* const leg_extension = !strcmp(output_filename,stdoutmark) ? stdoutmark : LZ4_EXTENSION; + LZ4IO_compressMultipleFilenames_Legacy(inFileNames, (int)ifnIdx, leg_extension, cLevel, prefs); } else { - LZ4IO_compressFilename_Legacy(prefs, input_filename, output_filename, cLevel); + LZ4IO_compressFilename_Legacy(input_filename, output_filename, cLevel, prefs); } } else { if (multiple_inputs) { + const char* const comp_extension = !strcmp(output_filename,stdoutmark) ? stdoutmark : LZ4_EXTENSION; assert(ifnIdx <= INT_MAX); - operationResult = LZ4IO_compressMultipleFilenames(prefs, inFileNames, (int)ifnIdx, !strcmp(output_filename,stdoutmark) ? stdoutmark : LZ4_EXTENSION, cLevel); + operationResult = LZ4IO_compressMultipleFilenames(inFileNames, (int)ifnIdx, comp_extension, cLevel, prefs); } else { - operationResult = DEFAULT_COMPRESSOR(prefs, input_filename, output_filename, cLevel); + operationResult = DEFAULT_COMPRESSOR(input_filename, output_filename, cLevel, prefs); } } } _cleanup: diff --git a/programs/lz4io.c b/programs/lz4io.c index 302d747..4c4678c 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -178,7 +178,7 @@ LZ4IO_prefs_t* LZ4IO_defaultPreferences(void) return ret; } -void LZ4IO_freePreferences(LZ4IO_prefs_t* const prefs) +void LZ4IO_freePreferences(LZ4IO_prefs_t* prefs) { free(prefs); } @@ -396,9 +396,8 @@ static int LZ4IO_LZ4_compress(const char* src, char* dst, int srcSize, int dstSi /* LZ4IO_compressFilename_Legacy : * This function is intentionally "hidden" (not published in .h) * It generates compressed streams using the old 'legacy' format */ -int LZ4IO_compressFilename_Legacy(LZ4IO_prefs_t* const prefs, - const char* input_filename, const char* output_filename, - int compressionlevel) +int LZ4IO_compressFilename_Legacy(const char* input_filename, const char* output_filename, + int compressionlevel, const LZ4IO_prefs_t* prefs) { typedef int (*compress_f)(const char* src, char* dst, int srcSize, int dstSize, int cLevel); compress_f const compressionFunction = (compressionlevel < 3) ? LZ4IO_LZ4_compress : LZ4_compress_HC; @@ -483,10 +482,10 @@ int LZ4IO_compressFilename_Legacy(LZ4IO_prefs_t* const prefs, /* LZ4IO_compressMultipleFilenames_Legacy : * This function is intentionally "hidden" (not published in .h) * It generates multiple compressed streams using the old 'legacy' format */ -int LZ4IO_compressMultipleFilenames_Legacy(LZ4IO_prefs_t* const prefs, +int LZ4IO_compressMultipleFilenames_Legacy( const char** inFileNamesTable, int ifntSize, const char* suffix, - int compressionLevel) + int compressionLevel, const LZ4IO_prefs_t* prefs) { int i; int missed_files = 0; @@ -500,9 +499,9 @@ int LZ4IO_compressMultipleFilenames_Legacy(LZ4IO_prefs_t* const prefs, for (i=0; iblockSize; cRess_t ress; @@ -644,14 +643,13 @@ static void LZ4IO_freeCResources(cRess_t ress) /* * LZ4IO_compressFilename_extRess() - * io_prefs is mutable, as it may update sparseFileSupport * result : 0 : compression completed correctly * 1 : missing or pb opening srcFileName */ static int -LZ4IO_compressFilename_extRess(LZ4IO_prefs_t* const io_prefs, cRess_t ress, +LZ4IO_compressFilename_extRess(cRess_t ress, const char* srcFileName, const char* dstFileName, - int compressionLevel) + int compressionLevel, const LZ4IO_prefs_t* const io_prefs) { unsigned long long filesize = 0; unsigned long long compressedfilesize = 0; @@ -773,13 +771,13 @@ LZ4IO_compressFilename_extRess(LZ4IO_prefs_t* const io_prefs, cRess_t ress, } -int LZ4IO_compressFilename(LZ4IO_prefs_t* const prefs, const char* srcFileName, const char* dstFileName, int compressionLevel) +int LZ4IO_compressFilename(const char* srcFileName, const char* dstFileName, int compressionLevel, const LZ4IO_prefs_t* prefs) { UTIL_time_t const timeStart = UTIL_getTime(); clock_t const cpuStart = clock(); cRess_t const ress = LZ4IO_createCResources(prefs); - int const result = LZ4IO_compressFilename_extRess(prefs, ress, srcFileName, dstFileName, compressionLevel); + int const result = LZ4IO_compressFilename_extRess(ress, srcFileName, dstFileName, compressionLevel, prefs); /* Free resources */ LZ4IO_freeCResources(ress); @@ -797,10 +795,11 @@ int LZ4IO_compressFilename(LZ4IO_prefs_t* const prefs, const char* srcFileName, } -int LZ4IO_compressMultipleFilenames(LZ4IO_prefs_t* const prefs, +int LZ4IO_compressMultipleFilenames( const char** inFileNamesTable, int ifntSize, const char* suffix, - int compressionLevel) + int compressionLevel, + const LZ4IO_prefs_t* prefs) { int i; int missed_files = 0; @@ -816,9 +815,9 @@ int LZ4IO_compressMultipleFilenames(LZ4IO_prefs_t* const prefs, for (i=0; i Date: Sat, 14 Nov 2020 12:44:18 -0800 Subject: fix minor parse warning --- programs/lz4io.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/programs/lz4io.c b/programs/lz4io.c index 4c4678c..a274798 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -365,7 +365,7 @@ static FILE* LZ4IO_openDstFile(const char* dstFileName, const LZ4IO_prefs_t* con /* sparse file */ { int const sparseMode = (prefs->sparseFileSupport - (f==stdout)) > 0; - if (f && sparseMode) SET_SPARSE_FILE_MODE(f); + if (f && sparseMode) { SET_SPARSE_FILE_MODE(f); } } return f; -- cgit v1.2.3 From 581c459b4ec4bbaae518a0ef9baa552f21f9e24e Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sat, 14 Nov 2020 14:26:08 -0800 Subject: restrict BitScanForward() to VS2005+ suggested by @aqrit in #947 --- lib/lz4.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/lz4.c b/lib/lz4.c index 5fe3433..5c6cf4d 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -533,7 +533,7 @@ static unsigned LZ4_NbCommonBytes (reg_t val) return (unsigned)(((U64)((val & (m - 1)) * m)) >> 56); # endif } else /* 32 bits */ { -# if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT) +# if defined(_MSC_VER) && (_MSC_VER >= 1400) && !defined(LZ4_FORCE_SW_BITCOUNT) unsigned long r; _BitScanForward(&r, (U32)val); return (unsigned)r >> 3; -- cgit v1.2.3 From e0f85f2fc8e8868b2623a5344781f269bda7d4ba Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sat, 14 Nov 2020 16:24:36 -0800 Subject: better visual conformance only include on vs2005+ (#947) remove some useless #pragma fix a few minor Visual warnings --- lib/lz4.c | 7 +++---- programs/util.h | 6 +++--- tests/checkFrame.c | 9 --------- tests/frametest.c | 3 +-- tests/fuzzer.c | 20 +++++++++++--------- 5 files changed, 18 insertions(+), 27 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 5c6cf4d..9f5e9bf 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -121,10 +121,9 @@ /*-************************************ * Compiler Options **************************************/ -#ifdef _MSC_VER /* Visual Studio */ -# include -# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ -# pragma warning(disable : 4293) /* disable: C4293: too large shift (32-bits) */ +#if defined(_MSC_VER) && (_MSC_VER >= 1400) /* Visual Studio 2005+ */ +# include /* only present in VS2005+ */ +# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ #endif /* _MSC_VER */ #ifndef LZ4_FORCE_INLINE diff --git a/programs/util.h b/programs/util.h index 2f3d0a8..733c1ca 100644 --- a/programs/util.h +++ b/programs/util.h @@ -594,15 +594,15 @@ UTIL_createFileList(const char** inputNames, unsigned inputNamesNb, for (i=0, pos=0, nbFiles=0; i= bufSize) { while (pos + len >= bufSize) bufSize += LIST_SIZE_INCREASE; buf = (char*)UTIL_realloc(buf, bufSize); if (!buf) return NULL; } assert(pos + len < bufSize); - strncpy(buf + pos, inputNames[i], bufSize - pos); - pos += len + 1; + memcpy(buf + pos, inputNames[i], len); + pos += len; nbFiles++; } else { char* bufend = buf + bufSize; diff --git a/tests/checkFrame.c b/tests/checkFrame.c index f39d2ac..f9a1c14 100644 --- a/tests/checkFrame.c +++ b/tests/checkFrame.c @@ -23,15 +23,6 @@ - LZ4 source repository : https://github.com/lz4/lz4 */ - /*-************************************ - * Compiler specific - **************************************/ - #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 **************************************/ diff --git a/tests/frametest.c b/tests/frametest.c index 2633c90..241fd5f 100644 --- a/tests/frametest.c +++ b/tests/frametest.c @@ -27,8 +27,7 @@ * Compiler specific **************************************/ #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 : 26451) /* disable: Arithmetic overflow */ #endif diff --git a/tests/fuzzer.c b/tests/fuzzer.c index ba36621..a824813 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -1097,7 +1097,7 @@ static void FUZ_unitTests(int compressionLevel) { int const dSize = LZ4_decompress_safe(testCompressed, testVerify, cSize, testInputSize); assert(dSize == sampleSize); /* correct size */ { XXH32_hash_t const crcCheck = XXH32(testVerify, (size_t)dSize, 0); - assert(crcCheck == crcOrig); + FUZ_CHECKTEST(crcCheck != crcOrig, "LZ4_decompress_safe decompression corruption"); } } } DISPLAYLEVEL(3, " OK \n"); @@ -1121,7 +1121,7 @@ static void FUZ_unitTests(int compressionLevel) { int const dSize = LZ4_decompress_safe(startInput, testVerify, cSize, sampleSize); assert(dSize == sampleSize); /* correct size */ { XXH64_hash_t const crcCheck = XXH64(testVerify, (size_t)dSize, 0); - assert(crcCheck == crcOrig); + FUZ_CHECKTEST(crcCheck != crcOrig, "LZ4_decompress_safe decompression corruption"); } } } } } DISPLAYLEVEL(3, " OK \n"); @@ -1173,12 +1173,12 @@ static void FUZ_unitTests(int compressionLevel) assert(ctx != NULL); /* ensure init is successful */ /* Check access violation with asan */ - FUZ_CHECKTEST( LZ4_saveDict(&streamingState, NULL, 0) != 0, + FUZ_CHECKTEST( LZ4_saveDict(ctx, NULL, 0) != 0, "LZ4_saveDict() can't save anything into (NULL,0)"); /* Check access violation with asan */ { char tmp_buffer[240] = { 0 }; - FUZ_CHECKTEST( LZ4_saveDict(&streamingState, tmp_buffer, sizeof(tmp_buffer)) != 0, + FUZ_CHECKTEST( LZ4_saveDict(ctx, tmp_buffer, sizeof(tmp_buffer)) != 0, "LZ4_saveDict() can't save anything since compression hasn't started"); } } DISPLAYLEVEL(3, "OK \n"); @@ -1290,12 +1290,12 @@ static void FUZ_unitTests(int compressionLevel) assert(ctx != NULL); /* ensure init is successful */ /* Check access violation with asan */ - FUZ_CHECKTEST( LZ4_saveDictHC(&sHC, NULL, 0) != 0, + FUZ_CHECKTEST( LZ4_saveDictHC(ctx, NULL, 0) != 0, "LZ4_saveDictHC() can't save anything into (NULL,0)"); /* Check access violation with asan */ { char tmp_buffer[240] = { 0 }; - FUZ_CHECKTEST( LZ4_saveDictHC(&sHC, tmp_buffer, sizeof(tmp_buffer)) != 0, + FUZ_CHECKTEST( LZ4_saveDictHC(ctx, tmp_buffer, sizeof(tmp_buffer)) != 0, "LZ4_saveDictHC() can't save anything since compression hasn't started"); } } DISPLAYLEVEL(3, "OK \n"); @@ -1596,7 +1596,7 @@ static void FUZ_unitTests(int compressionLevel) DISPLAYLEVEL(3, "LZ4_compress_HC_destSize : "); /* encode congenerical sequence test for HC compressors */ - { LZ4_streamHC_t sHC; /* statically allocated */ + { LZ4_streamHC_t* const sHC = LZ4_createStreamHC(); int const src_buf_size = 3 MB; int const dst_buf_size = 6 KB; int const payload = 0; @@ -1609,6 +1609,7 @@ static void FUZ_unitTests(int compressionLevel) char* dbuf1 = (char*)malloc(dst_buf_size + 1); char* dbuf2 = (char*)malloc(dst_buf_size + 1); + assert(sHC != NULL); assert(dst_buf_size > dst_max_len); if (!sbuf1 || !sbuf2 || !dbuf1 || !dbuf2) { EXIT_MSG("not enough memory for FUZ_unitTests (destSize)"); @@ -1643,8 +1644,8 @@ static void FUZ_unitTests(int compressionLevel) memset(sbuf2, payload, slen); memset(dbuf2, 0, dlen); dbuf2[dlen] = endchk; - LZ4_resetStreamHC(&sHC, compressionLevel); - dsz2 = LZ4_compress_HC_destSize(&sHC, sbuf2, dbuf2, &srcsz2, dlen, compressionLevel); + LZ4_resetStreamHC(sHC, compressionLevel); + dsz2 = LZ4_compress_HC_destSize(sHC, sbuf2, dbuf2, &srcsz2, dlen, compressionLevel); DISPLAYLEVEL(5, "LZ4_compress_HC_destSize: %i bytes compressed into %i bytes, ", srcsz2, dsz2); DISPLAYLEVEL(5, "last token : 0x%0X, ", dbuf2[dsz2 - 6]); DISPLAYLEVEL(5, "last ML extra lenbyte : 0x%0X, \n", dbuf2[dsz2 - 7]); @@ -1663,6 +1664,7 @@ static void FUZ_unitTests(int compressionLevel) FUZ_CHECKTEST(memcmp(sbuf1, sbuf2, (size_t)res2), "LZ4_compress_HC_destSize() decompression corruption!"); } } + LZ4_freeStreamHC(sHC); free(sbuf1); free(sbuf2); free(dbuf1); -- cgit v1.2.3 From 99ba44a596d2b86090fa85bf09f9a2642bfd0001 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sat, 14 Nov 2020 17:25:31 -0800 Subject: fixed minor Visual warning doesn't happen on my environment, though it's a different version of Visual Studio --- tests/frametest.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/frametest.c b/tests/frametest.c index 241fd5f..e613cbf 100644 --- a/tests/frametest.c +++ b/tests/frametest.c @@ -1060,7 +1060,7 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi CHECK(op[dstEndSize] != canaryByte, "LZ4F_compressEnd writes beyond dstCapacity !"); if (LZ4F_isError(flushedSize)) { if (tooSmallDstEnd) /* failure is allowed */ continue; - CHECK(1, "Compression completion failed (error %i : %s)", + CHECK(!tooSmallDstEnd, "Compression completion failed (error %i : %s)", (int)flushedSize, LZ4F_getErrorName(flushedSize)); } op += flushedSize; -- cgit v1.2.3 From f61b034cd7cd16c288db999263a77ab8a0e05610 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sat, 14 Nov 2020 20:39:23 -0800 Subject: fix minor win32 warning --- lib/lz4hc.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/lz4hc.c b/lib/lz4hc.c index 8c6063f..77c9f43 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -172,7 +172,8 @@ static unsigned LZ4HC_countPattern(const BYTE* ip, const BYTE* const iEnd, U32 const pattern32) { const BYTE* const iStart = ip; - reg_t const pattern = (sizeof(pattern)==8) ? (reg_t)pattern32 + (((reg_t)pattern32) << 32) : pattern32; + reg_t const pattern = (sizeof(pattern)==8) ? + (reg_t)pattern32 + (((reg_t)pattern32) << (sizeof(pattern)*4)) : pattern32; while (likely(ip < iEnd-(sizeof(pattern)-1))) { reg_t const diff = LZ4_read_ARCH(ip) ^ pattern; -- cgit v1.2.3 From 0760724577e7c62cfdc1de6ecae400ab653bb2bf Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sun, 15 Nov 2020 01:31:09 -0800 Subject: update doc --- README.md | 14 +------------- contrib/debian/copyright | 4 ++-- doc/lz4_Frame_format.md | 4 ++-- examples/Makefile | 2 +- lib/Makefile | 2 +- programs/Makefile | 2 +- 6 files changed, 8 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 451238b..bdb028c 100644 --- a/README.md +++ b/README.md @@ -26,25 +26,13 @@ LZ4 library is provided as open-source software using BSD 2-Clause license. |Branch |Status | |------------|---------| -|master | [![Build Status][travisMasterBadge]][travisLink] [![Build status][AppveyorMasterBadge]][AppveyorLink] [![coverity][coverBadge]][coverlink] | |dev | [![Build Status][travisDevBadge]][travisLink] [![Build status][AppveyorDevBadge]][AppveyorLink] | -[travisMasterBadge]: https://travis-ci.org/lz4/lz4.svg?branch=master "Continuous Integration test suite" [travisDevBadge]: https://travis-ci.org/lz4/lz4.svg?branch=dev "Continuous Integration test suite" [travisLink]: https://travis-ci.org/lz4/lz4 -[AppveyorMasterBadge]: https://ci.appveyor.com/api/projects/status/github/lz4/lz4?branch=master&svg=true "Windows test suite" [AppveyorDevBadge]: https://ci.appveyor.com/api/projects/status/github/lz4/lz4?branch=dev&svg=true "Windows test suite" [AppveyorLink]: https://ci.appveyor.com/project/YannCollet/lz4-1lndh -[coverBadge]: https://scan.coverity.com/projects/4735/badge.svg "Static code analysis of Master branch" -[coverlink]: https://scan.coverity.com/projects/4735 - -> **Branch Policy:** -> - The "master" branch is considered stable, at all times. -> - The "dev" branch is the one where all contributions must be merged - before being promoted to master. -> + If you plan to propose a patch, please commit into the "dev" branch, - or its own feature branch. - Direct commit to "master" are not permitted. + Benchmarks ------------------------- diff --git a/contrib/debian/copyright b/contrib/debian/copyright index 18a7f48..0914768 100644 --- a/contrib/debian/copyright +++ b/contrib/debian/copyright @@ -4,6 +4,6 @@ Upstream-Contact: Yann Collet Source: https://github.com/lz4/lz4 Files: * -Copyright: (C) 2011+ Yann Collet +Copyright: (C) 2011-2020 Yann Collet License: GPL-2+ - The full text of license: https://github.com/Cyan4973/lz4/blob/master/lib/LICENSE + The full text of license: https://github.com/lz4/lz4/blob/dev/lib/LICENSE diff --git a/doc/lz4_Frame_format.md b/doc/lz4_Frame_format.md index e7cbdbf..7e08841 100644 --- a/doc/lz4_Frame_format.md +++ b/doc/lz4_Frame_format.md @@ -263,7 +263,7 @@ This field uses 4-bytes, format is little-endian. If the highest bit is set (`1`), the block is uncompressed. If the highest bit is not set (`0`), the block is LZ4-compressed, -using the [LZ4 block format specification](https://github.com/lz4/lz4/blob/master/doc/lz4_Block_format.md). +using the [LZ4 block format specification](https://github.com/lz4/lz4/blob/dev/doc/lz4_Block_format.md). All other bits give the size, in bytes, of the data section. The size does not include the block checksum if present. @@ -284,7 +284,7 @@ __Data__ Where the actual data to decode stands. It might be compressed or not, depending on previous field indications. -When compressed, the data must respect the [LZ4 block format specification](https://github.com/lz4/lz4/blob/master/doc/lz4_Block_format.md). +When compressed, the data must respect the [LZ4 block format specification](https://github.com/lz4/lz4/blob/dev/doc/lz4_Block_format.md). Note that a block is not necessarily full. Uncompressed size of data can be any size __up to__ _Block_Maximum_Size_, diff --git a/examples/Makefile b/examples/Makefile index 6a34b33..3ec3e21 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -19,7 +19,7 @@ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # # You can contact the author at : -# - LZ4 source repository : https://github.com/Cyan4973/lz4 +# - LZ4 source repository : https://github.com/lz4/lz4 # - LZ4 forum froup : https://groups.google.com/forum/#!forum/lz4c # ########################################################################## # This makefile compile and test diff --git a/lib/Makefile b/lib/Makefile index 8f21d3d..c12949b 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -28,7 +28,7 @@ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # # You can contact the author at : -# - LZ4 source repository : https://github.com/Cyan4973/lz4 +# - LZ4 source repository : https://github.com/lz4/lz4 # - LZ4 forum froup : https://groups.google.com/forum/#!forum/lz4c # ################################################################ diff --git a/programs/Makefile b/programs/Makefile index 4256ae8..c1053f6 100644 --- a/programs/Makefile +++ b/programs/Makefile @@ -22,7 +22,7 @@ # # You can contact the author at : # - LZ4 homepage : http://www.lz4.org -# - LZ4 source repository : https://github.com/Cyan4973/lz4 +# - LZ4 source repository : https://github.com/lz4/lz4 # ########################################################################## # lz4 : Command Line Utility, supporting gzip-like arguments # lz4c : CLU, supporting also legacy lz4demo arguments -- cgit v1.2.3 From 48d9b7416b4c4ef89ac8ad3fc6b2fff7bcb8a870 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sun, 15 Nov 2020 19:46:12 -0800 Subject: update cli entry --- NEWS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS b/NEWS index 91f7506..401931e 100644 --- a/NEWS +++ b/NEWS @@ -9,7 +9,7 @@ api : improved LZ4F_compressBound() with automatic flushing, by Christopher Harv api : can (de)compress to/from NULL without UBs api : fix alignment test on 32-bit systems (state initialization) api : fix LZ4_saveDictHC() in corner case scenario, detected by @IgorKorkin -cli : --list command, compressed file content statistics, by Filipe Calasans +cli : `-l` legacy format is now compatible with `-m` multiple files, by Filipe Calasans cli : benchmark mode supports dictionary, by @rkoradi cli : fix --fast with large argument, detected by @picoHz build: link to user-defined memory functions with LZ4_USER_MEMORY_FUNCTIONS, suggested by Yuriy Levchenko -- cgit v1.2.3