From 51976dcb24d7a65cf2f4d8b05339e5b1093abc38 Mon Sep 17 00:00:00 2001
From: Yann Collet
It's possible to have input and output sharing the same buffer,
- for highly contrained memory environments.
+ for highly constrained memory environments.
In both cases, it requires input to lay at the end of the buffer,
and decompression to start at beginning of the buffer.
Buffer size must feature some margin, hence be larger than final size.
@@ -459,7 +459,7 @@ int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream);
size_t prefixSize;
} LZ4_streamDecode_t_internal;
const char* LZ4F_getErrorName(LZ4F_errorCode_t code); /**< return error code string; for debugging */
-Frame compression types
+Frame compression types
+
typedef enum {
LZ4F_default=0,
diff --git a/examples/Makefile b/examples/Makefile
index 3ec3e21..24b58c9 100644
--- a/examples/Makefile
+++ b/examples/Makefile
@@ -1,6 +1,6 @@
# ##########################################################################
# LZ4 examples - Makefile
-# Copyright (C) Yann Collet 2011-2014
+# Copyright (C) Yann Collet 2011-2020
#
# GPL v2 License
#
diff --git a/lib/LICENSE b/lib/LICENSE
index 74c2cdd..4884916 100644
--- a/lib/LICENSE
+++ b/lib/LICENSE
@@ -1,5 +1,5 @@
LZ4 Library
-Copyright (c) 2011-2016, Yann Collet
+Copyright (c) 2011-2020, Yann Collet
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
diff --git a/lib/Makefile b/lib/Makefile
index c12949b..a11fce4 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -1,6 +1,6 @@
# ################################################################
# LZ4 library - Makefile
-# Copyright (C) Yann Collet 2011-2016
+# Copyright (C) Yann Collet 2011-2020
# All rights reserved.
#
# This Makefile is validated for Linux, macOS, *BSD, Hurd, Solaris, MSYS2 targets
diff --git a/lib/dll/example/Makefile b/lib/dll/example/Makefile
index e987956..eb8cc1e 100644
--- a/lib/dll/example/Makefile
+++ b/lib/dll/example/Makefile
@@ -1,6 +1,6 @@
# ##########################################################################
# LZ4 programs - Makefile
-# Copyright (C) Yann Collet 2016
+# Copyright (C) Yann Collet 2016-2020
#
# GPL v2 License
#
diff --git a/lib/liblz4-dll.rc.in b/lib/liblz4-dll.rc.in
index bf9adf5..e2d84b6 100644
--- a/lib/liblz4-dll.rc.in
+++ b/lib/liblz4-dll.rc.in
@@ -22,7 +22,7 @@ BEGIN
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 "LegalCopyright", "Copyright (C) 2013-2020, Yann Collet"
VALUE "OriginalFilename", "@LIBLZ4@.dll"
VALUE "ProductName", "LZ4"
VALUE "ProductVersion", "@LIBVER_MAJOR@.@LIBVER_MINOR@.@LIBVER_PATCH@.0"
diff --git a/lib/liblz4.pc.in b/lib/liblz4.pc.in
index cb31cd7..211477f 100644
--- a/lib/liblz4.pc.in
+++ b/lib/liblz4.pc.in
@@ -1,5 +1,5 @@
# LZ4 - Fast LZ compression algorithm
-# Copyright (C) 2011-2014, Yann Collet.
+# Copyright (C) 2011-2020, Yann Collet.
# BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
prefix=@PREFIX@
diff --git a/lib/lz4frame.h b/lib/lz4frame.h
index 4573317..267f289 100644
--- a/lib/lz4frame.h
+++ b/lib/lz4frame.h
@@ -1,7 +1,7 @@
/*
LZ4 auto-framing library
Header File
- Copyright (C) 2011-2017, Yann Collet.
+ Copyright (C) 2011-2020, 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
diff --git a/lib/lz4frame_static.h b/lib/lz4frame_static.h
index 925a2c5..2b44a63 100644
--- a/lib/lz4frame_static.h
+++ b/lib/lz4frame_static.h
@@ -1,7 +1,7 @@
/*
LZ4 auto-framing library
Header File for static linking only
- Copyright (C) 2011-2016, Yann Collet.
+ Copyright (C) 2011-2020, Yann Collet.
BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
diff --git a/lib/lz4hc.c b/lib/lz4hc.c
index 77c9f43..feee612 100644
--- a/lib/lz4hc.c
+++ b/lib/lz4hc.c
@@ -1,6 +1,6 @@
/*
LZ4 HC - High Compression Mode of LZ4
- Copyright (C) 2011-2017, Yann Collet.
+ Copyright (C) 2011-2020, Yann Collet.
BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
diff --git a/lib/lz4hc.h b/lib/lz4hc.h
index 3d441fb..b423990 100644
--- a/lib/lz4hc.h
+++ b/lib/lz4hc.h
@@ -1,7 +1,7 @@
/*
LZ4 HC - High Compression Mode of LZ4
Header File
- Copyright (C) 2011-2017, Yann Collet.
+ Copyright (C) 2011-2020, 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
diff --git a/programs/Makefile b/programs/Makefile
index c1053f6..594be55 100644
--- a/programs/Makefile
+++ b/programs/Makefile
@@ -1,6 +1,6 @@
# ##########################################################################
# LZ4 programs - Makefile
-# Copyright (C) Yann Collet 2011-2017
+# Copyright (C) Yann Collet 2011-2020
#
# This Makefile is validated for Linux, macOS, *BSD, Hurd, Solaris, MSYS2 targets
#
diff --git a/programs/bench.c b/programs/bench.c
index 3357d14..603705c 100644
--- a/programs/bench.c
+++ b/programs/bench.c
@@ -1,6 +1,6 @@
/*
bench.c - Demo program to benchmark open-source compression algorithms
- Copyright (C) Yann Collet 2012-2016
+ Copyright (C) Yann Collet 2012-2020
GPL v2 License
diff --git a/programs/bench.h b/programs/bench.h
index 22ebf60..642d496 100644
--- a/programs/bench.h
+++ b/programs/bench.h
@@ -1,6 +1,6 @@
/*
bench.h - Demo program to benchmark open-source compression algorithm
- Copyright (C) Yann Collet 2012-2016
+ Copyright (C) Yann Collet 2012-2020
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
diff --git a/programs/datagen.c b/programs/datagen.c
index 24a2da2..f448640 100644
--- a/programs/datagen.c
+++ b/programs/datagen.c
@@ -1,6 +1,6 @@
/*
datagen.c - compressible data generator test tool
- Copyright (C) Yann Collet 2012-2016
+ Copyright (C) Yann Collet 2012-2020
GPL v2 License
diff --git a/programs/datagen.h b/programs/datagen.h
index 91c5b02..c20c9c7 100644
--- a/programs/datagen.h
+++ b/programs/datagen.h
@@ -1,6 +1,6 @@
/*
datagen.h - compressible data generator header
- Copyright (C) Yann Collet 2012-2016
+ Copyright (C) Yann Collet 2012-2020
GPL v2 License
diff --git a/programs/lz4-exe.rc.in b/programs/lz4-exe.rc.in
index 7b81030..bcf4d7d 100644
--- a/programs/lz4-exe.rc.in
+++ b/programs/lz4-exe.rc.in
@@ -13,7 +13,7 @@ FILETYPE 1
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 "LegalCopyright", "Copyright (C) 2013-2020, Yann Collet"
VALUE "OriginalFilename", "@PROGNAME@.@EXT@"
VALUE "ProductName", "LZ4"
VALUE "ProductVersion", "@LIBVER_MAJOR@.@LIBVER_MINOR@.@LIBVER_PATCH@.0"
@@ -24,4 +24,3 @@ FILETYPE 1
VALUE "Translation", 0x0409, 1200
}
}
-
diff --git a/programs/lz4cli.c b/programs/lz4cli.c
index 523b8a8..57a6ab9 100644
--- a/programs/lz4cli.c
+++ b/programs/lz4cli.c
@@ -1,6 +1,6 @@
/*
LZ4cli - LZ4 Command Line Interface
- Copyright (C) Yann Collet 2011-2016
+ Copyright (C) Yann Collet 2011-2020
GPL v2 License
diff --git a/programs/lz4io.c b/programs/lz4io.c
index a274798..2d69d6c 100644
--- a/programs/lz4io.c
+++ b/programs/lz4io.c
@@ -1,6 +1,6 @@
/*
LZ4io.c - LZ4 File/Stream Interface
- Copyright (C) Yann Collet 2011-2017
+ Copyright (C) Yann Collet 2011-2020
GPL v2 License
diff --git a/programs/lz4io.h b/programs/lz4io.h
index d6d7eee..b68f085 100644
--- a/programs/lz4io.h
+++ b/programs/lz4io.h
@@ -1,6 +1,6 @@
/*
LZ4io.h - LZ4 File/Stream Interface
- Copyright (C) Yann Collet 2011-2016
+ Copyright (C) Yann Collet 2011-2020
GPL v2 License
This program is free software; you can redistribute it and/or modify
diff --git a/programs/platform.h b/programs/platform.h
index ab8300d..abcd67b 100644
--- a/programs/platform.h
+++ b/programs/platform.h
@@ -1,6 +1,6 @@
/*
platform.h - compiler and OS detection
- Copyright (C) 2016-present, Przemyslaw Skibinski, Yann Collet
+ Copyright (C) 2016-2020, Przemyslaw Skibinski, Yann Collet
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
diff --git a/programs/util.h b/programs/util.h
index 733c1ca..9a38f1c 100644
--- a/programs/util.h
+++ b/programs/util.h
@@ -1,6 +1,6 @@
/*
util.h - utility functions
- Copyright (C) 2016-present, Przemyslaw Skibinski, Yann Collet
+ Copyright (C) 2016-2020, Przemyslaw Skibinski, Yann Collet
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
diff --git a/tests/checkFrame.c b/tests/checkFrame.c
index f9a1c14..2b50bae 100644
--- a/tests/checkFrame.c
+++ b/tests/checkFrame.c
@@ -1,6 +1,6 @@
/*
checkFrame - verify frame headers
- Copyright (C) Yann Collet 2014-present
+ Copyright (C) Yann Collet 2014-2020
GPL v2 License
diff --git a/tests/checkTag.c b/tests/checkTag.c
index 4a33415..5e5a034 100644
--- a/tests/checkTag.c
+++ b/tests/checkTag.c
@@ -1,6 +1,6 @@
/*
checkTag.c - Version validation tool for LZ4
- Copyright (C) Yann Collet 2018 - present
+ Copyright (C) Yann Collet 2018-2020
GPL v2 License
diff --git a/tests/datagencli.c b/tests/datagencli.c
index c985197..9efe27e 100644
--- a/tests/datagencli.c
+++ b/tests/datagencli.c
@@ -1,7 +1,7 @@
/*
datagencli.c
compressible data command line generator
- Copyright (C) Yann Collet 2012-2016
+ Copyright (C) Yann Collet 2012-2020
GPL v2 License
diff --git a/tests/frametest.c b/tests/frametest.c
index e613cbf..f06d9b7 100644
--- a/tests/frametest.c
+++ b/tests/frametest.c
@@ -1,6 +1,6 @@
/*
frameTest - test tool for lz4frame
- Copyright (C) Yann Collet 2014-2016
+ Copyright (C) Yann Collet 2014-2020
GPL v2 License
diff --git a/tests/fullbench.c b/tests/fullbench.c
index cb9b684..55bf0b7 100644
--- a/tests/fullbench.c
+++ b/tests/fullbench.c
@@ -1,6 +1,6 @@
/*
bench.c - Demo program to benchmark open-source compression algorithm
- Copyright (C) Yann Collet 2012-2016
+ Copyright (C) Yann Collet 2012-2020
GPL v2 License
diff --git a/tests/fuzzer.c b/tests/fuzzer.c
index a824813..03bdf01 100644
--- a/tests/fuzzer.c
+++ b/tests/fuzzer.c
@@ -1,6 +1,6 @@
/*
fuzzer.c - Fuzzer test tool for LZ4
- Copyright (C) Yann Collet 2012-2017
+ Copyright (C) Yann Collet 2012-2020
GPL v2 License
diff --git a/tests/roundTripTest.c b/tests/roundTripTest.c
index 2d34451..9145c42 100644
--- a/tests/roundTripTest.c
+++ b/tests/roundTripTest.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
+ * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under both the BSD-style license (found in the
diff --git a/tests/test-lz4-speed.py b/tests/test-lz4-speed.py
index ca8f010..4a17d93 100644
--- a/tests/test-lz4-speed.py
+++ b/tests/test-lz4-speed.py
@@ -1,7 +1,7 @@
#! /usr/bin/env python3
#
-# Copyright (c) 2016-present, Przemyslaw Skibinski, Yann Collet, Facebook, Inc.
+# Copyright (c) 2016-2020, Przemyslaw Skibinski, Yann Collet, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
@@ -152,7 +152,7 @@ def benchmark_and_compare(branch, commit, last_commit, args, executableName, md5
% (os.getloadavg()[0], args.maxLoadAvg, sleepTime))
time.sleep(sleepTime)
start_load = str(os.getloadavg())
- result = execute('programs/%s -rqi5b1e%s %s' % (executableName, args.lastCLevel, testFilePath), print_output=True)
+ result = execute('programs/%s -rqi5b1e%s %s' % (executableName, args.lastCLevel, testFilePath), print_output=True)
end_load = str(os.getloadavg())
linesExpected = args.lastCLevel + 1
if len(result) != linesExpected:
--
cgit v0.12
From 165fdddc283571330eb2fed0e489ee11222dadbf Mon Sep 17 00:00:00 2001
From: Yann Collet
-#define LZ4_STREAMSIZE 16416 /* static size, for inter-version compatibility */
+
#define LZ4_STREAMSIZE ((1UL << LZ4_MEMORY_USAGE) + 32) /* static size, for inter-version compatibility */
#define LZ4_STREAMSIZE_VOIDP (LZ4_STREAMSIZE / sizeof(void*))
union LZ4_stream_u {
void* table[LZ4_STREAMSIZE_VOIDP];
diff --git a/doc/lz4frame_manual.html b/doc/lz4frame_manual.html
index 57a165c..281c0d6 100644
--- a/doc/lz4frame_manual.html
+++ b/doc/lz4frame_manual.html
@@ -140,7 +140,8 @@ LZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_cctx* cctx);
The version provided MUST be LZ4F_VERSION. It is intended to track potential version mismatch, notably when using DLL.
The function will provide a pointer to a fully allocated LZ4F_cctx object.
If @return != zero, there was an error during context creation.
- Object can release its memory using LZ4F_freeCompressionContext();
+ Object can be released using LZ4F_freeCompressionContext();
+ Note: LZ4F_freeCompressionContext() works with NULL pointers (do nothing).
#ifndef LZ4_MEMORY_USAGE -# define LZ4_MEMORY_USAGE 14 +# define LZ4_MEMORY_USAGE LZ4_MEMORY_USAGE_DEFAULT #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 better cache locality. +
Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; ) + Increasing memory usage improves compression ratio, at the cost of speed. + Reduced memory usage may improve speed at the cost of ratio, thanks to better cache locality. Default value is 14, for 16KB, which nicely fits into Intel x86 L1 cache
Returns the maximum possible compressed size with LZ4F_compressFrame() given srcSize and preferences. `preferencesPtr` is optional. It can be replaced by NULL, in which case, the function will assume default preferences. Note : this result is only usable with LZ4F_compressFrame(). - It may also be used with LZ4F_compressUpdate() _if no flush() operation_ is performed. + It may also be relevant to LZ4F_compressUpdate() _only if_ no flush() operation is ever performed.
typedef enum { + LZ4B_COMPRESSED, + LZ4B_UNCOMPRESSED, +} LZ4F_blockCompression_t; +
typedef struct { LZ4F_blockSizeID_t blockSizeID; /* max64KB, max256KB, max1MB, max4MB; 0 == default */ LZ4F_blockMode_t blockMode; /* LZ4F_blockLinked, LZ4F_blockIndependent; 0 == default */ diff --git a/examples/frameCompress.c b/examples/frameCompress.c index 9eaa4da..3219014 100644 --- a/examples/frameCompress.c +++ b/examples/frameCompress.c @@ -11,9 +11,9 @@ #include#include +#include #include - #define IN_CHUNK_SIZE (16*1024) static const LZ4F_preferences_t kPrefs = { @@ -57,10 +57,11 @@ static compressResult_t compress_file_internal(FILE* f_in, FILE* f_out, LZ4F_compressionContext_t ctx, void* inBuff, size_t inChunkSize, - void* outBuff, size_t outCapacity) + void* outBuff, size_t outCapacity, + FILE* f_unc, long uncOffset) { compressResult_t result = { 1, 0, 0 }; /* result for an error */ - unsigned long long count_in = 0, count_out; + long long count_in = 0, count_out, bytesToOffset = -1; assert(f_in != NULL); assert(f_out != NULL); assert(ctx != NULL); @@ -81,22 +82,48 @@ compress_file_internal(FILE* f_in, FILE* f_out, /* stream file */ for (;;) { - size_t const readSize = fread(inBuff, 1, IN_CHUNK_SIZE, f_in); + size_t compressedSize; + long long inSize = IN_CHUNK_SIZE; + if (uncOffset >= 0) { + bytesToOffset = uncOffset - count_in; + + /* read only remaining bytes to offset position */ + if (bytesToOffset < IN_CHUNK_SIZE && bytesToOffset > 0) { + inSize = bytesToOffset; + } + } + + /* input data is at uncompressed data offset */ + if (bytesToOffset <= 0 && uncOffset >= 0 && f_unc) { + size_t const readSize = fread(inBuff, 1, inSize, f_unc); + if (readSize == 0) { + uncOffset = -1; + continue; + } + count_in += readSize; + compressedSize = LZ4F_uncompressedUpdate(ctx, + outBuff, outCapacity, + inBuff, readSize, + NULL); + } else { + size_t const readSize = fread(inBuff, 1, inSize, f_in); if (readSize == 0) break; /* nothing left to read from input file */ count_in += readSize; - - size_t const compressedSize = LZ4F_compressUpdate(ctx, + compressedSize = LZ4F_compressUpdate(ctx, outBuff, outCapacity, inBuff, readSize, NULL); - if (LZ4F_isError(compressedSize)) { - printf("Compression failed: error %u \n", (unsigned)compressedSize); - return result; - } - printf("Writing %u bytes\n", (unsigned)compressedSize); - safe_fwrite(outBuff, 1, compressedSize, f_out); - count_out += compressedSize; + } + + if (LZ4F_isError(compressedSize)) { + printf("Compression failed: error %u \n", (unsigned)compressedSize); + return result; + } + + printf("Writing %u bytes\n", (unsigned)compressedSize); + safe_fwrite(outBuff, 1, compressedSize, f_out); + count_out += compressedSize; } /* flush whatever remains within internal buffers */ @@ -120,7 +147,8 @@ compress_file_internal(FILE* f_in, FILE* f_out, } static compressResult_t -compress_file(FILE* f_in, FILE* f_out) +compress_file(FILE* f_in, FILE* f_out, + FILE* f_unc, int uncOffset) { assert(f_in != NULL); assert(f_out != NULL); @@ -137,7 +165,8 @@ compress_file(FILE* f_in, FILE* f_out) result = compress_file_internal(f_in, f_out, ctx, src, IN_CHUNK_SIZE, - outbuff, outbufCapacity); + outbuff, outbufCapacity, + f_unc, uncOffset); } else { printf("error : resource allocation failed \n"); } @@ -305,52 +334,106 @@ static int decompress_file(FILE* f_in, FILE* f_out) } -int compareFiles(FILE* fp0, FILE* fp1) +int compareFiles(FILE* fp0, FILE* fp1, FILE* fpUnc, long uncOffset) { int result = 0; + long bytesRead = 0; + long bytesToOffset = -1; + long b1Size = 1024; while (result==0) { + char b1[b1Size]; + size_t r1; + size_t bytesToRead = sizeof b1; + if (uncOffset >= 0) { + bytesToOffset = uncOffset - bytesRead; + + /* read remainder to offset */ + if (bytesToOffset < b1Size) { + bytesToRead = bytesToOffset; + } + } + char b0[1024]; - char b1[1024]; - size_t const r0 = fread(b0, 1, sizeof(b0), fp0); - size_t const r1 = fread(b1, 1, sizeof(b1), fp1); + size_t r0; + if (bytesToOffset <= 0 && fpUnc) { + bytesToRead = sizeof b1; + r0 = fread(b0, 1,bytesToRead, fpUnc); + } else { + r0 = fread(b0, 1, bytesToRead, fp0); + } + + r1 = fread(b1, 1, r0, fp1); result = (r0 != r1); if (!r0 || !r1) break; if (!result) result = memcmp(b0, b1, r0); + + bytesRead += r1; } return result; } -int main(int argc, const char **argv) { +int main(int argc, char **argv) { char inpFilename[256] = { 0 }; char lz4Filename[256] = { 0 }; char decFilename[256] = { 0 }; + int uncOffset = -1; + char uncFilename[256] = { 0 }; + int opt; + if (argc < 2) { printf("Please specify input filename\n"); - return 0; + return EXIT_FAILURE; } snprintf(inpFilename, 256, "%s", argv[1]); snprintf(lz4Filename, 256, "%s.lz4", argv[1]); snprintf(decFilename, 256, "%s.lz4.dec", argv[1]); + while ((opt = getopt(argc, argv, "o:d:")) != -1) { + switch (opt) { + case 'd': + snprintf(uncFilename, 256, "%s", optarg); + break; + case 'o': + uncOffset = atoi(optarg); + break; + default: + printf("usage: %s [-o -d ]\n", argv[0]); + printf("-o uncompressed data offset\n"); + printf(" inject uncompressed data at this offset into the lz4 file\n"); + printf("-d uncompressed file\n"); + printf(" file to inject without compression into the lz4 file\n"); + return EXIT_FAILURE; + } + } + printf("inp = [%s]\n", inpFilename); printf("lz4 = [%s]\n", lz4Filename); printf("dec = [%s]\n", decFilename); + if (uncOffset > 0) { + printf("unc = [%s]\n", uncFilename); + printf("ofs = [%i]\n", uncOffset); + } /* compress */ { FILE* const inpFp = fopen(inpFilename, "rb"); FILE* const outFp = fopen(lz4Filename, "wb"); + FILE* const uncFp = fopen(uncFilename, "rb"); printf("compress : %s -> %s\n", inpFilename, lz4Filename); - compressResult_t const ret = compress_file(inpFp, outFp); + compressResult_t const ret = compress_file( + inpFp, outFp, + uncFp, uncOffset); fclose(outFp); fclose(inpFp); + if (uncFp) + fclose(uncFp); if (ret.error) { printf("compress : failed with code %i\n", ret.error); @@ -383,12 +466,16 @@ int main(int argc, const char **argv) { /* verify */ { FILE* const inpFp = fopen(inpFilename, "rb"); FILE* const decFp = fopen(decFilename, "rb"); + FILE* const uncFp = fopen(uncFilename, "rb"); printf("verify : %s <-> %s\n", inpFilename, decFilename); - int const cmp = compareFiles(inpFp, decFp); + int const cmp = compareFiles(inpFp, decFp, + uncFp, uncOffset); fclose(decFp); fclose(inpFp); + if (uncFp) + fclose(uncFp); if (cmp) { printf("corruption detected : decompressed file differs from original\n"); diff --git a/lib/lz4.c b/lib/lz4.c index a2272cf..16ed3d3 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1679,6 +1679,15 @@ int LZ4_compress_forceExtDict (LZ4_stream_t* LZ4_dict, const char* source, char* return result; } +int LZ4_DictSize (LZ4_stream_t* LZ4_dict, int dictSize) +{ + LZ4_stream_t_internal* const dict = &LZ4_dict->internal_donotuse; + + if ((U32)dictSize > 64 KB) { dictSize = 64 KB; } /* useless to define a dictionary > 64 KB */ + if ((U32)dictSize > dict->dictSize) { dictSize = (int)dict->dictSize; } + + return dictSize; +} /*! LZ4_saveDict() : * If previously compressed data block is not guaranteed to remain available at its memory location, @@ -1690,12 +1699,9 @@ int LZ4_compress_forceExtDict (LZ4_stream_t* LZ4_dict, const char* source, char* int LZ4_saveDict (LZ4_stream_t* LZ4_dict, char* safeBuffer, int dictSize) { LZ4_stream_t_internal* const dict = &LZ4_dict->internal_donotuse; - + dictSize = LZ4_DictSize(LZ4_dict, dictSize); DEBUGLOG(5, "LZ4_saveDict : dictSize=%i, safeBuffer=%p", dictSize, safeBuffer); - 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 (safeBuffer == NULL) assert(dictSize == 0); if (dictSize > 0) { const BYTE* const previousDictEnd = dict->dictionary + dict->dictSize; diff --git a/lib/lz4.h b/lib/lz4.h index 6c068c6..1e793fd 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -346,6 +346,8 @@ LZ4LIB_API int LZ4_loadDict (LZ4_stream_t* streamPtr, const char* dictionary, in */ LZ4LIB_API int LZ4_compress_fast_continue (LZ4_stream_t* streamPtr, const char* src, char* dst, int srcSize, int dstCapacity, int acceleration); +LZ4LIB_API int LZ4_DictSize (LZ4_stream_t* LZ4_dict, int dictSize); + /*! LZ4_saveDict() : * If last 64KB data cannot be guaranteed to remain available at its current memory location, * save it into a safer place (char* safeBuffer). diff --git a/lib/lz4frame.c b/lib/lz4frame.c index a0275ca..bcf9629 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -129,7 +129,8 @@ static int g_debuglog_enable = 1; **************************************/ #if !defined (__VMS) && (defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) ) # include - typedef uint8_t BYTE; +#include +typedef uint8_t BYTE; typedef uint16_t U16; typedef uint32_t U32; typedef int32_t S32; @@ -236,6 +237,7 @@ typedef struct LZ4F_cctx_s void* lz4CtxPtr; U16 lz4CtxAlloc; /* sized for: 0 = none, 1 = lz4 ctx, 2 = lz4hc ctx */ U16 lz4CtxState; /* in use as: 0 = none, 1 = lz4 ctx, 2 = lz4hc ctx */ + LZ4F_blockCompression_t blockCompression; } LZ4F_cctx_t; @@ -757,14 +759,27 @@ 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) + LZ4F_blockChecksum_t crcFlag, + LZ4F_blockCompression_t blockCompression) { BYTE* const cSizePtr = (BYTE*)dst; - U32 cSize = (U32)compress(lz4ctx, (const char*)src, (char*)(cSizePtr+BHSize), - (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); + U32 cSize; + if (compress != NULL) { + cSize = (U32)compress(lz4ctx, (const char*)src, (char*)(cSizePtr+BHSize), + (int)(srcSize), (int)(srcSize-1), + level, cdict); + } else { + cSize = (U32)srcSize; + /* force no compression if compress callback is null */ + blockCompression = LZ4B_UNCOMPRESSED; + } + + if (cSize == 0) { /* compression failed */ + DEBUGLOG(5, "LZ4F_makeBlock: compression failed, creating a raw block (size %u)", (U32)srcSize); + blockCompression = LZ4B_UNCOMPRESSED; + } + + if (blockCompression == LZ4B_UNCOMPRESSED) { cSize = (U32)srcSize; LZ4F_writeLE32(cSizePtr, cSize | LZ4F_BLOCKUNCOMPRESSED_FLAG); memcpy(cSizePtr+BHSize, src, srcSize); @@ -824,33 +839,48 @@ static compressFunc_t LZ4F_selectCompression(LZ4F_blockMode_t blockMode, int lev return LZ4F_compressBlockHC_continue; } +static int LZ4F_maxDictSize(void) { + return 64 KB; +} + /* Save history (up to 64KB) into @tmpBuff */ static int LZ4F_localSaveDict(LZ4F_cctx_t* cctxPtr) { if (cctxPtr->prefs.compressionLevel < LZ4HC_CLEVEL_MIN) - return LZ4_saveDict ((LZ4_stream_t*)(cctxPtr->lz4CtxPtr), (char*)(cctxPtr->tmpBuff), 64 KB); - return LZ4_saveDictHC ((LZ4_streamHC_t*)(cctxPtr->lz4CtxPtr), (char*)(cctxPtr->tmpBuff), 64 KB); + return LZ4_saveDict ((LZ4_stream_t*)(cctxPtr->lz4CtxPtr), (char*)(cctxPtr->tmpBuff), LZ4F_maxDictSize()); + return LZ4_saveDictHC ((LZ4_streamHC_t*)(cctxPtr->lz4CtxPtr), (char*)(cctxPtr->tmpBuff), LZ4F_maxDictSize()); +} + +static int LZ4F_localDictSize(LZ4F_cctx_t* cctxPtr) +{ + if (cctxPtr->prefs.compressionLevel < LZ4HC_CLEVEL_MIN) + return LZ4_DictSize ((LZ4_stream_t*)(cctxPtr->lz4CtxPtr), LZ4F_maxDictSize()); + return LZ4_DictHCSize ((LZ4_streamHC_t*)(cctxPtr->lz4CtxPtr), LZ4F_maxDictSize()); } typedef enum { notDone, fromTmpBuffer, fromSrcBuffer } LZ4F_lastBlockStatus; static const LZ4F_compressOptions_t k_cOptionsNull = { 0, { 0, 0, 0 } }; -/*! LZ4F_compressUpdate() : + + /*! LZ4F_compressUpdateImpl() : * LZ4F_compressUpdate() can be called repetitively to compress as much data as necessary. * When successful, the function always entirely consumes @srcBuffer. * src data is either buffered or compressed into @dstBuffer. - * @dstCapacity MUST be >= LZ4F_compressBound(srcSize, preferencesPtr). + * If the block compression does not match the compression of the previous block, the old data is flushed + * and operations continue with the new compression mode. + * @dstCapacity MUST be >= LZ4F_compressBound(srcSize, preferencesPtr) when block compression is turned on. * @compressOptionsPtr is optional : provide NULL to mean "default". * @return : the number of bytes written into dstBuffer. It can be zero, meaning input data was just buffered. * or an error code if it fails (which can be tested using LZ4F_isError()) * After an error, the state is left in a UB state, and must be re-initialized. */ -size_t LZ4F_compressUpdate(LZ4F_cctx* cctxPtr, - void* dstBuffer, size_t dstCapacity, +static size_t LZ4F_compressUpdateImpl(LZ4F_cctx* cctxPtr, + void* dstBuffer, size_t dstCapacity, const void* srcBuffer, size_t srcSize, - const LZ4F_compressOptions_t* compressOptionsPtr) -{ + const LZ4F_compressOptions_t* compressOptionsPtr, + LZ4F_blockCompression_t blockCompression) + { size_t const blockSize = cctxPtr->maxBlockSize; const BYTE* srcPtr = (const BYTE*)srcBuffer; const BYTE* const srcEnd = srcPtr + srcSize; @@ -858,49 +888,62 @@ size_t LZ4F_compressUpdate(LZ4F_cctx* cctxPtr, BYTE* dstPtr = dstStart; LZ4F_lastBlockStatus lastBlockCompressed = notDone; compressFunc_t const compress = LZ4F_selectCompression(cctxPtr->prefs.frameInfo.blockMode, cctxPtr->prefs.compressionLevel); - + size_t bytesWritten = 0; DEBUGLOG(4, "LZ4F_compressUpdate (srcSize=%zu)", srcSize); + /* flush currently written block, to continue with new block compression */ + if (cctxPtr->blockCompression != blockCompression) { + bytesWritten = LZ4F_flush(cctxPtr, dstBuffer, dstCapacity, compressOptionsPtr); + cctxPtr->blockCompression = blockCompression; + } + RETURN_ERROR_IF(cctxPtr->cStage != 1, compressionState_uninitialized); /* state must be initialized and waiting for next block */ - if (dstCapacity < LZ4F_compressBound_internal(srcSize, &(cctxPtr->prefs), cctxPtr->tmpInSize)) - RETURN_ERROR(dstMaxSize_tooSmall); + + if (blockCompression == LZ4B_COMPRESSED && + dstCapacity < LZ4F_compressBound_internal(srcSize, &(cctxPtr->prefs), cctxPtr->tmpInSize)) + RETURN_ERROR(dstMaxSize_tooSmall); + + if (blockCompression == LZ4B_UNCOMPRESSED && dstCapacity < srcSize) + RETURN_ERROR(dstMaxSize_tooSmall); + if (compressOptionsPtr == NULL) compressOptionsPtr = &k_cOptionsNull; /* complete tmp buffer */ if (cctxPtr->tmpInSize > 0) { /* some data already within tmp buffer */ - size_t const sizeToCopy = blockSize - cctxPtr->tmpInSize; - assert(blockSize > cctxPtr->tmpInSize); - if (sizeToCopy > srcSize) { - /* add src to tmpIn buffer */ - memcpy(cctxPtr->tmpIn + cctxPtr->tmpInSize, srcBuffer, srcSize); - srcPtr = srcEnd; - cctxPtr->tmpInSize += srcSize; - /* still needs some CRC */ - } else { - /* complete tmpIn block and then compress it */ - lastBlockCompressed = fromTmpBuffer; - memcpy(cctxPtr->tmpIn + cctxPtr->tmpInSize, srcBuffer, sizeToCopy); - srcPtr += sizeToCopy; - - dstPtr += LZ4F_makeBlock(dstPtr, - cctxPtr->tmpIn, blockSize, - compress, cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel, - cctxPtr->cdict, - cctxPtr->prefs.frameInfo.blockChecksumFlag); - - if (cctxPtr->prefs.frameInfo.blockMode==LZ4F_blockLinked) cctxPtr->tmpIn += blockSize; - cctxPtr->tmpInSize = 0; - } } + size_t const sizeToCopy = blockSize - cctxPtr->tmpInSize; + assert(blockSize > cctxPtr->tmpInSize); + if (sizeToCopy > srcSize) { + /* add src to tmpIn buffer */ + memcpy(cctxPtr->tmpIn + cctxPtr->tmpInSize, srcBuffer, srcSize); + srcPtr = srcEnd; + cctxPtr->tmpInSize += srcSize; + /* still needs some CRC */ + } else { + /* complete tmpIn block and then compress it */ + lastBlockCompressed = fromTmpBuffer; + memcpy(cctxPtr->tmpIn + cctxPtr->tmpInSize, srcBuffer, sizeToCopy); + srcPtr += sizeToCopy; - while ((size_t)(srcEnd - srcPtr) >= blockSize) { - /* compress full blocks */ - lastBlockCompressed = fromSrcBuffer; dstPtr += LZ4F_makeBlock(dstPtr, - srcPtr, blockSize, + cctxPtr->tmpIn, blockSize, compress, cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel, cctxPtr->cdict, - cctxPtr->prefs.frameInfo.blockChecksumFlag); - srcPtr += blockSize; + cctxPtr->prefs.frameInfo.blockChecksumFlag, blockCompression); + + if (cctxPtr->prefs.frameInfo.blockMode==LZ4F_blockLinked) cctxPtr->tmpIn += blockSize; + cctxPtr->tmpInSize = 0; + } + } + + while ((size_t)(srcEnd - srcPtr) >= blockSize) { + /* compress full blocks */ + lastBlockCompressed = fromSrcBuffer; + dstPtr += LZ4F_makeBlock(dstPtr, + srcPtr, blockSize, + compress, cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel, + cctxPtr->cdict, + cctxPtr->prefs.frameInfo.blockChecksumFlag, blockCompression); + srcPtr += blockSize; } if ((cctxPtr->prefs.autoFlush) && (srcPtr < srcEnd)) { @@ -910,19 +953,29 @@ size_t LZ4F_compressUpdate(LZ4F_cctx* cctxPtr, srcPtr, (size_t)(srcEnd - srcPtr), compress, cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel, cctxPtr->cdict, - cctxPtr->prefs.frameInfo.blockChecksumFlag); - srcPtr = srcEnd; + cctxPtr->prefs.frameInfo.blockChecksumFlag, + blockCompression); + srcPtr = srcEnd; } /* preserve dictionary within @tmpBuff whenever necessary */ if ((cctxPtr->prefs.frameInfo.blockMode==LZ4F_blockLinked) && (lastBlockCompressed==fromSrcBuffer)) { - if (compressOptionsPtr->stableSrc) { - cctxPtr->tmpIn = cctxPtr->tmpBuff; /* src is stable : dictionary remains in src across invocations */ + if (compressOptionsPtr->stableSrc) { + cctxPtr->tmpIn = cctxPtr->tmpBuff; /* src is stable : dictionary remains in src across invocations */ + } else { + int realDictSize; + if (blockCompression == LZ4B_COMPRESSED) { + realDictSize = LZ4F_localSaveDict(cctxPtr); } else { - int const realDictSize = LZ4F_localSaveDict(cctxPtr); - assert(0 <= realDictSize && realDictSize <= 64 KB); - cctxPtr->tmpIn = cctxPtr->tmpBuff + realDictSize; + /* only keep the space of the dictionary, so dict data is kept for the next compressedUpdate + * this is only relevant if linked block mode + * */ + realDictSize = LZ4F_localDictSize(cctxPtr); } + + assert(0 <= realDictSize && realDictSize <= 64 KB); + cctxPtr->tmpIn = cctxPtr->tmpBuff + realDictSize; + } } /* keep tmpIn within limits */ @@ -931,24 +984,75 @@ size_t LZ4F_compressUpdate(LZ4F_cctx* cctxPtr, { /* only preserve 64KB within internal buffer. Ensures there is enough room for next block. * note: this situation necessarily implies lastBlockCompressed==fromTmpBuffer */ - int const realDictSize = LZ4F_localSaveDict(cctxPtr); + int realDictSize; + if (blockCompression == LZ4B_COMPRESSED) { + realDictSize = LZ4F_localSaveDict(cctxPtr); + } else { + /* only keep the space of the dictionary, so dict data is kept for the next compressedUpdate*/ + realDictSize = LZ4F_maxDictSize(); + } cctxPtr->tmpIn = cctxPtr->tmpBuff + realDictSize; assert((cctxPtr->tmpIn + blockSize) <= (cctxPtr->tmpBuff + cctxPtr->maxBufferSize)); } /* some input data left, necessarily < blockSize */ if (srcPtr < srcEnd) { - /* fill tmp buffer */ - size_t const sizeToCopy = (size_t)(srcEnd - srcPtr); - memcpy(cctxPtr->tmpIn, srcPtr, sizeToCopy); - cctxPtr->tmpInSize = sizeToCopy; + /* fill tmp buffer */ + size_t const sizeToCopy = (size_t)(srcEnd - srcPtr); + memcpy(cctxPtr->tmpIn, srcPtr, sizeToCopy); + cctxPtr->tmpInSize = sizeToCopy; } if (cctxPtr->prefs.frameInfo.contentChecksumFlag == LZ4F_contentChecksumEnabled) - (void)XXH32_update(&(cctxPtr->xxh), srcBuffer, srcSize); + (void)XXH32_update(&(cctxPtr->xxh), srcBuffer, srcSize); cctxPtr->totalInSize += srcSize; - return (size_t)(dstPtr - dstStart); + return bytesWritten + (size_t)(dstPtr - dstStart); +} + +/*! LZ4F_compressUpdate() : + * LZ4F_compressUpdate() can be called repetitively to compress as much data as necessary. + * When successful, the function always entirely consumes @srcBuffer. + * src data is either buffered or compressed into @dstBuffer. + * If previously an uncompressed block was written, buffered data is flushed + * before appending compressed data is continued. + * @dstCapacity MUST be >= LZ4F_compressBound(srcSize, preferencesPtr). + * @compressOptionsPtr is optional : provide NULL to mean "default". + * @return : the number of bytes written into dstBuffer. It can be zero, meaning input data was just buffered. + * or an error code if it fails (which can be tested using LZ4F_isError()) + * After an error, the state is left in a UB state, and must be re-initialized. + */ +size_t LZ4F_compressUpdate(LZ4F_cctx* cctxPtr, + void* dstBuffer, size_t dstCapacity, + const void* srcBuffer, size_t srcSize, + const LZ4F_compressOptions_t* compressOptionsPtr) +{ + return LZ4F_compressUpdateImpl(cctxPtr, + dstBuffer, dstCapacity, + srcBuffer, srcSize, + compressOptionsPtr, LZ4B_COMPRESSED); +} + +/*! LZ4F_compressUpdate() : + * LZ4F_compressUpdate() can be called repetitively to compress as much data as necessary. + * When successful, the function always entirely consumes @srcBuffer. + * src data is either buffered or compressed into @dstBuffer. + * If previously an uncompressed block was written, buffered data is flushed + * before appending compressed data is continued. + * @dstCapacity MUST be >= LZ4F_compressBound(srcSize, preferencesPtr). + * @compressOptionsPtr is optional : provide NULL to mean "default". + * @return : the number of bytes written into dstBuffer. It can be zero, meaning input data was just buffered. + * or an error code if it fails (which can be tested using LZ4F_isError()) + * After an error, the state is left in a UB state, and must be re-initialized. + */ +LZ4FLIB_API size_t LZ4F_uncompressedUpdate(LZ4F_cctx* cctxPtr, + void* dstBuffer, size_t dstCapacity, + const void* srcBuffer, size_t srcSize, + const LZ4F_compressOptions_t* compressOptionsPtr) { + return LZ4F_compressUpdateImpl(cctxPtr, + dstBuffer, dstCapacity, + srcBuffer, srcSize, + compressOptionsPtr, LZ4B_UNCOMPRESSED); } @@ -981,7 +1085,7 @@ size_t LZ4F_flush(LZ4F_cctx* cctxPtr, cctxPtr->tmpIn, cctxPtr->tmpInSize, compress, cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel, cctxPtr->cdict, - cctxPtr->prefs.frameInfo.blockChecksumFlag); + cctxPtr->prefs.frameInfo.blockChecksumFlag, cctxPtr->blockCompression); assert(((void)"flush overflows dstBuffer!", (size_t)(dstPtr - dstStart) <= dstCapacity)); if (cctxPtr->prefs.frameInfo.blockMode == LZ4F_blockLinked) diff --git a/lib/lz4frame.h b/lib/lz4frame.h index 74f19cd..18d33e1 100644 --- a/lib/lz4frame.h +++ b/lib/lz4frame.h @@ -160,6 +160,11 @@ typedef enum { LZ4F_OBSOLETE_ENUM(skippableFrame) } LZ4F_frameType_t; +typedef enum { + LZ4B_COMPRESSED, + LZ4B_UNCOMPRESSED +} LZ4F_blockCompression_t; + #ifdef LZ4F_ENABLE_OBSOLETE_ENUMS typedef LZ4F_blockSizeID_t blockSizeID_t; typedef LZ4F_blockMode_t blockMode_t; @@ -303,6 +308,8 @@ LZ4FLIB_API size_t LZ4F_compressBound(size_t srcSize, const LZ4F_preferences_t* * This value is provided by LZ4F_compressBound(). * If this condition is not respected, LZ4F_compress() will fail (result is an errorCode). * After an error, the state is left in a UB state, and must be re-initialized or freed. + * If previously an uncompressed block was written, buffered data is flushed + * before appending compressed data is continued. * `cOptPtr` is optional : NULL can be provided, in which case all options are set to default. * @return : number of bytes written into `dstBuffer` (it can be zero, meaning input data was just buffered). * or an error code if it fails (which can be tested using LZ4F_isError()) @@ -312,6 +319,22 @@ LZ4FLIB_API size_t LZ4F_compressUpdate(LZ4F_cctx* cctx, const void* srcBuffer, size_t srcSize, const LZ4F_compressOptions_t* cOptPtr); +/*! LZ4F_uncompressedUpdate() : + * LZ4F_uncompressedUpdate() can be called repetitively to add as much data uncompressed data as necessary. + * Important rule: dstCapacity MUST be large enough to store the entire source buffer as + * no compression is done for this operation + * If this condition is not respected, LZ4F_uncompressedUpdate() will fail (result is an errorCode). + * After an error, the state is left in a UB state, and must be re-initialized or freed. + * If previously a compressed block was written, buffered data is flushed + * before appending uncompressed data is continued. + * `cOptPtr` is optional : NULL can be provided, in which case all options are set to default. + * @return : number of bytes written into `dstBuffer` (it can be zero, meaning input data was just buffered). + * or an error code if it fails (which can be tested using LZ4F_isError()) + */ +LZ4FLIB_API size_t LZ4F_uncompressedUpdate(LZ4F_cctx* cctx, + void* dstBuffer, size_t dstCapacity, + const void* srcBuffer, size_t srcSize, + const LZ4F_compressOptions_t* cOptPtr); /*! LZ4F_flush() : * When data must be generated and sent immediately, without waiting for a block to be completely filled, * it's possible to call LZ4_flush(). It will immediately compress any data buffered within cctx. diff --git a/lib/lz4hc.c b/lib/lz4hc.c index 99650a6..da806ef 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -1154,6 +1154,16 @@ int LZ4_compress_HC_continue_destSize (LZ4_streamHC_t* LZ4_streamHCPtr, const ch return LZ4_compressHC_continue_generic(LZ4_streamHCPtr, src, dst, srcSizePtr, targetDestSize, fillOutput); } +int LZ4_DictHCSize(LZ4_streamHC_t* LZ4_streamHCPtr, int dictSize) { + LZ4HC_CCtx_internal* const streamPtr = &LZ4_streamHCPtr->internal_donotuse; + int const prefixSize = (int)(streamPtr->end - (streamPtr->base + streamPtr->dictLimit)); + 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; + return dictSize; +} /* LZ4_saveDictHC : @@ -1164,12 +1174,7 @@ int LZ4_compress_HC_continue_destSize (LZ4_streamHC_t* LZ4_streamHCPtr, const ch 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(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; + dictSize = LZ4_DictHCSize(LZ4_streamHCPtr, dictSize); if (safeBuffer == NULL) assert(dictSize == 0); if (dictSize > 0) memmove(safeBuffer, streamPtr->end - dictSize, dictSize); diff --git a/lib/lz4hc.h b/lib/lz4hc.h index f4afc9b..11671dc 100644 --- a/lib/lz4hc.h +++ b/lib/lz4hc.h @@ -173,6 +173,8 @@ 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_DictHCSize(LZ4_streamHC_t* LZ4_streamHCPtr, int dictSize); + LZ4LIB_API int LZ4_saveDictHC (LZ4_streamHC_t* streamHCPtr, char* safeBuffer, int maxDictSize); -- cgit v0.12 From 62f6cef564a9478b92e6dbd0faa81eaa1a90b34f Mon Sep 17 00:00:00 2001 From: Alexander Mohr Date: Fri, 10 Jun 2022 06:00:38 +0000 Subject: review: Fix review findings This commit fixes the review findings Signed-off-by: Alexander Mohr --- doc/lz4_manual.html | 12 ++++++++++++ doc/lz4frame_manual.html | 37 +++++++++++++++++++++++++------------ lib/lz4.c | 14 ++++++++++++-- lib/lz4.h | 13 +++++++++++-- lib/lz4frame.c | 7 +++++-- lib/lz4frame.h | 5 ----- lib/lz4hc.c | 14 ++++++++++++-- lib/lz4hc.h | 14 ++++++++++++-- 8 files changed, 89 insertions(+), 27 deletions(-) diff --git a/doc/lz4_manual.html b/doc/lz4_manual.html index 037cfc0..13c1ae6 100644 --- a/doc/lz4_manual.html +++ b/doc/lz4_manual.html @@ -391,6 +391,18 @@ int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream);
LZ4LIB_STATIC_API int LZ4_getDictSize (LZ4_stream_t* LZ4_dict, int dictSize); +Get the size of the dictionary. This can be used for adding data without + compression to the LZ4 archive. If linked blocked mode is used the memory + of the dictionary is kept free. + This way uncompressed data does not influence the effectiveness of the + dictionary. + @param LZ4_dict Pointer to the dictionary to get the size of. + @param dictSize The maximum dictionary size. (Normally 64 KB). + @return The size of the dictionary. + +
It's possible to have input and output sharing the same buffer, for highly constrained memory environments. diff --git a/doc/lz4frame_manual.html b/doc/lz4frame_manual.html index 9490b51..b47a92f 100644 --- a/doc/lz4frame_manual.html +++ b/doc/lz4frame_manual.html @@ -75,11 +75,6 @@ LZ4F_OBSOLETE_ENUM(skippableFrame) } LZ4F_frameType_t;
typedef enum { - LZ4B_COMPRESSED, - LZ4B_UNCOMPRESSED, -} LZ4F_blockCompression_t; -
typedef struct { LZ4F_blockSizeID_t blockSizeID; /* max64KB, max256KB, max1MB, max4MB; 0 == default */ LZ4F_blockMode_t blockMode; /* LZ4F_blockLinked, LZ4F_blockIndependent; 0 == default */ @@ -189,22 +184,40 @@ LZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_cctx* cctx); This value is provided by LZ4F_compressBound(). If this condition is not respected, LZ4F_compress() will fail (result is an errorCode). After an error, the state is left in a UB state, and must be re-initialized or freed. + If previously an uncompressed block was written, buffered data is flushed + before appending compressed data is continued. `cOptPtr` is optional : NULL can be provided, in which case all options are set to default. @return : number of bytes written into `dstBuffer` (it can be zero, meaning input data was just buffered). or an error code if it fails (which can be tested using LZ4F_isError())
size_t LZ4F_flush(LZ4F_cctx* cctx, +size_t LZ4F_uncompressedUpdate(LZ4F_cctx* cctx, + void* dstBuffer, size_t dstCapacity, + const void* srcBuffer, size_t srcSize, + const LZ4F_compressOptions_t* cOptPtr); +/*! LZ4F_flush() : + * When data must be generated and sent immediately, without waiting for a block to be completely filled, + * it's possible to call LZ4_flush(). It will immediately compress any data buffered within cctx. + * `dstCapacity` must be large enough to ensure the operation will be successful. + * `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). + */ +size_t LZ4F_flush(LZ4F_cctx* cctx, void* dstBuffer, size_t dstCapacity, const LZ4F_compressOptions_t* cOptPtr); -When data must be generated and sent immediately, without waiting for a block to be completely filled, - it's possible to call LZ4_flush(). It will immediately compress any data buffered within cctx. - `dstCapacity` must be large enough to ensure the operation will be successful. - `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) +
LZ4F_uncompressedUpdate() can be called repetitively to add as much data uncompressed data as necessary. + Important rule: dstCapacity MUST be large enough to store the entire source buffer as + no compression is done for this operation + If this condition is not respected, LZ4F_uncompressedUpdate() will fail (result is an errorCode). + After an error, the state is left in a UB state, and must be re-initialized or freed. + If previously a compressed block was written, buffered data is flushed + before appending uncompressed data is continued. + `cOptPtr` is optional : NULL can be provided, in which case all options are set to default. + @return : number of bytes written into `dstBuffer` (it can be zero, meaning input data was just buffered). 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).
size_t LZ4F_uncompressedUpdate(LZ4F_cctx* cctx, - void* dstBuffer, size_t dstCapacity, - const void* srcBuffer, size_t srcSize, - const LZ4F_compressOptions_t* cOptPtr); -/*! LZ4F_flush() : - * When data must be generated and sent immediately, without waiting for a block to be completely filled, - * it's possible to call LZ4_flush(). It will immediately compress any data buffered within cctx. - * `dstCapacity` must be large enough to ensure the operation will be successful. - * `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). - */ -size_t LZ4F_flush(LZ4F_cctx* cctx, +size_t LZ4F_flush(LZ4F_cctx* cctx, void* dstBuffer, size_t dstCapacity, const LZ4F_compressOptions_t* cOptPtr); -LZ4F_uncompressedUpdate() can be called repetitively to add as much data uncompressed data as necessary. - Important rule: dstCapacity MUST be large enough to store the entire source buffer as - no compression is done for this operation - If this condition is not respected, LZ4F_uncompressedUpdate() will fail (result is an errorCode). - After an error, the state is left in a UB state, and must be re-initialized or freed. - If previously a compressed block was written, buffered data is flushed - before appending uncompressed data is continued. - `cOptPtr` is optional : NULL can be provided, in which case all options are set to default. - @return : number of bytes written into `dstBuffer` (it can be zero, meaning input data was just buffered). +
When data must be generated and sent immediately, without waiting for a block to be completely filled, + it's possible to call LZ4_flush(). It will immediately compress any data buffered within cctx. + `dstCapacity` must be large enough to ensure the operation will be successful. + `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).
typedef enum { LZ4F_LIST_ERRORS(LZ4F_GENERATE_ENUM) _LZ4F_dummy_error_enum_for_c89_never_used } LZ4F_errorCodes;
LZ4FLIB_STATIC_API size_t LZ4F_uncompressedUpdate(LZ4F_cctx* cctx, + void* dstBuffer, size_t dstCapacity, + const void* srcBuffer, size_t srcSize, + const LZ4F_compressOptions_t* cOptPtr); +LZ4F_uncompressedUpdate() can be called repetitively to add as much data uncompressed data as necessary. + Important rule: dstCapacity MUST be large enough to store the entire source buffer as + no compression is done for this operation + If this condition is not respected, LZ4F_uncompressedUpdate() will fail (result is an errorCode). + After an error, the state is left in a UB state, and must be re-initialized or freed. + If previously a compressed block was written, buffered data is flushed + before appending uncompressed data is continued. + `cOptPtr` is optional : NULL can be provided, in which case all options are set to default. + @return : number of bytes written into `dstBuffer` (it can be zero, meaning input data was just buffered). + or an error code if it fails (which can be tested using LZ4F_isError()) + +
LZ4FLIB_STATIC_API LZ4F_CDict* LZ4F_createCDict(const void* dictBuffer, size_t dictSize); diff --git a/examples/frameCompress.c b/examples/frameCompress.c index 3219014..25ff729 100644 --- a/examples/frameCompress.c +++ b/examples/frameCompress.c @@ -13,6 +13,7 @@ #include#include +#include #define IN_CHUNK_SIZE (16*1024) diff --git a/lib/lz4frame.h b/lib/lz4frame.h index 20bfb8b..7d81fa0 100644 --- a/lib/lz4frame.h +++ b/lib/lz4frame.h @@ -314,22 +314,6 @@ LZ4FLIB_API size_t LZ4F_compressUpdate(LZ4F_cctx* cctx, const void* srcBuffer, size_t srcSize, const LZ4F_compressOptions_t* cOptPtr); -/*! LZ4F_uncompressedUpdate() : - * LZ4F_uncompressedUpdate() can be called repetitively to add as much data uncompressed data as necessary. - * Important rule: dstCapacity MUST be large enough to store the entire source buffer as - * no compression is done for this operation - * If this condition is not respected, LZ4F_uncompressedUpdate() will fail (result is an errorCode). - * After an error, the state is left in a UB state, and must be re-initialized or freed. - * If previously a compressed block was written, buffered data is flushed - * before appending uncompressed data is continued. - * `cOptPtr` is optional : NULL can be provided, in which case all options are set to default. - * @return : number of bytes written into `dstBuffer` (it can be zero, meaning input data was just buffered). - * or an error code if it fails (which can be tested using LZ4F_isError()) - */ -LZ4FLIB_API size_t LZ4F_uncompressedUpdate(LZ4F_cctx* cctx, - void* dstBuffer, size_t dstCapacity, - const void* srcBuffer, size_t srcSize, - const LZ4F_compressOptions_t* cOptPtr); /*! LZ4F_flush() : * When data must be generated and sent immediately, without waiting for a block to be completely filled, * it's possible to call LZ4_flush(). It will immediately compress any data buffered within cctx. @@ -560,6 +544,23 @@ LZ4FLIB_STATIC_API LZ4F_errorCodes LZ4F_getErrorCode(size_t functionResult); LZ4FLIB_STATIC_API size_t LZ4F_getBlockSize(unsigned); +/*! LZ4F_uncompressedUpdate() : + * LZ4F_uncompressedUpdate() can be called repetitively to add as much data uncompressed data as necessary. + * Important rule: dstCapacity MUST be large enough to store the entire source buffer as + * no compression is done for this operation + * If this condition is not respected, LZ4F_uncompressedUpdate() will fail (result is an errorCode). + * After an error, the state is left in a UB state, and must be re-initialized or freed. + * If previously a compressed block was written, buffered data is flushed + * before appending uncompressed data is continued. + * `cOptPtr` is optional : NULL can be provided, in which case all options are set to default. + * @return : number of bytes written into `dstBuffer` (it can be zero, meaning input data was just buffered). + * or an error code if it fails (which can be tested using LZ4F_isError()) + */ +LZ4FLIB_STATIC_API size_t LZ4F_uncompressedUpdate(LZ4F_cctx* cctx, + void* dstBuffer, size_t dstCapacity, + const void* srcBuffer, size_t srcSize, + const LZ4F_compressOptions_t* cOptPtr); + /********************************** * Bulk processing dictionary API *********************************/ diff --git a/ossfuzz/fuzz.h b/ossfuzz/fuzz.h index eefac63..917a304 100644 --- a/ossfuzz/fuzz.h +++ b/ossfuzz/fuzz.h @@ -41,6 +41,11 @@ extern "C" { int LLVMFuzzerTestOneInput(const uint8_t *src, size_t size); +/** + * Test if injection of uncompressed data into a stream is working properly + */ +int LLVMFuzzerUncompressedDataInjection(const uint8_t *data, size_t size) + #ifdef __cplusplus } #endif -- cgit v0.12 From 1738b50443d1446c9c80ebd7c9272427f2644809 Mon Sep 17 00:00:00 2001 From: Alexander Mohr Date: Fri, 10 Jun 2022 19:45:21 +0000 Subject: fuzz-test: add fuzz test for uncompressed api add a fuzzing test for uncompressed frame api Signed-off-by: Alexander Mohr --- lib/lz4frame.c | 3 +- ossfuzz/Makefile | 1 + ossfuzz/fuzz.h | 5 - ossfuzz/round_trip_frame_uncompressed_fuzzer.c | 127 +++++++++++++++++++++++++ 4 files changed, 129 insertions(+), 7 deletions(-) create mode 100644 ossfuzz/round_trip_frame_uncompressed_fuzzer.c diff --git a/lib/lz4frame.c b/lib/lz4frame.c index 0c78a1f..c7e0595 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -129,8 +129,7 @@ static int g_debuglog_enable = 1; **************************************/ #if !defined (__VMS) && (defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) ) # include -#include -typedef uint8_t BYTE; + typedef uint8_t BYTE; typedef uint16_t U16; typedef uint32_t U32; typedef int32_t S32; diff --git a/ossfuzz/Makefile b/ossfuzz/Makefile index 2ec1675..deb2938 100644 --- a/ossfuzz/Makefile +++ b/ossfuzz/Makefile @@ -45,6 +45,7 @@ FUZZERS := \ round_trip_hc_fuzzer \ compress_frame_fuzzer \ round_trip_frame_fuzzer \ + round_trip_frame_uncompressed_fuzzer \ decompress_frame_fuzzer .PHONY: all diff --git a/ossfuzz/fuzz.h b/ossfuzz/fuzz.h index 917a304..eefac63 100644 --- a/ossfuzz/fuzz.h +++ b/ossfuzz/fuzz.h @@ -41,11 +41,6 @@ extern "C" { int LLVMFuzzerTestOneInput(const uint8_t *src, size_t size); -/** - * Test if injection of uncompressed data into a stream is working properly - */ -int LLVMFuzzerUncompressedDataInjection(const uint8_t *data, size_t size) - #ifdef __cplusplus } #endif diff --git a/ossfuzz/round_trip_frame_uncompressed_fuzzer.c b/ossfuzz/round_trip_frame_uncompressed_fuzzer.c new file mode 100644 index 0000000..631d149 --- /dev/null +++ b/ossfuzz/round_trip_frame_uncompressed_fuzzer.c @@ -0,0 +1,127 @@ +/** + * 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_data_producer.h" +#include "fuzz_helpers.h" +#include "lz4.h" +#include "lz4_helpers.h" +#include "lz4frame.h" +#include "lz4frame_static.h" + +static void decompress_data(LZ4F_dctx *dctx, void *src, void *dst, + size_t dstCapacity, size_t readSize) { + size_t ret = 1; + const void *srcPtr = (const char *)src; + void *dstPtr = (char *)dst; + const void *const srcEnd = (const char *)srcPtr + readSize; + + while (ret != 0) { + while (srcPtr < srcEnd && ret != 0) { + /* Any data within dst has been flushed at this stage */ + size_t dstSize = dstCapacity; + size_t srcSize = (const char *)srcEnd - (const char *)srcPtr; + ret = LZ4F_decompress(dctx, dstPtr, &dstSize, srcPtr, &srcSize, + /* LZ4F_decompressOptions_t */ NULL); + FUZZ_ASSERT(!LZ4F_isError(ret)); + + /* Update input */ + srcPtr = (const char *)srcPtr + srcSize; + dstPtr = (char *)dstPtr + dstSize; + } + + FUZZ_ASSERT(srcPtr <= srcEnd); + } +} + +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); + + uint8_t *uncompressedData = malloc(size); + size_t uncompressedOffset = rand() % (size + 1); + + FUZZ_dataProducer_t *uncompressedProducer = + FUZZ_dataProducer_create(uncompressedData, size); + size_t uncompressedSize = + FUZZ_dataProducer_remainingBytes(uncompressedProducer); + + size_t const dstCapacity = + LZ4F_compressFrameBound(LZ4_compressBound(size), &prefs) + + uncompressedSize; + char *const dst = (char *)malloc(dstCapacity); + size_t rtCapacity = dstCapacity; + char *const rt = (char *)malloc(rtCapacity); + + FUZZ_ASSERT(dst); + FUZZ_ASSERT(rt); + + /* Compression must succeed and round trip correctly. */ + LZ4F_compressionContext_t ctx; + size_t const ctxCreation = LZ4F_createCompressionContext(&ctx, LZ4F_VERSION); + FUZZ_ASSERT(!LZ4F_isError(ctxCreation)); + + size_t const headerSize = LZ4F_compressBegin(ctx, dst, dstCapacity, &prefs); + FUZZ_ASSERT(!LZ4F_isError(headerSize)); + size_t compressedSize = headerSize; + + /* Compress data before uncompressed offset */ + size_t lz4Return = LZ4F_compressUpdate(ctx, dst + compressedSize, dstCapacity, + data, uncompressedOffset, NULL); + FUZZ_ASSERT(!LZ4F_isError(lz4Return)); + compressedSize += lz4Return; + + /* Add uncompressed data */ + lz4Return = LZ4F_uncompressedUpdate(ctx, dst + compressedSize, dstCapacity, + uncompressedData, uncompressedSize, NULL); + FUZZ_ASSERT(!LZ4F_isError(lz4Return)); + compressedSize += lz4Return; + + /* Compress data after uncompressed offset */ + lz4Return = LZ4F_compressUpdate(ctx, dst + compressedSize, dstCapacity, + data + uncompressedOffset, + size - uncompressedOffset, NULL); + FUZZ_ASSERT(!LZ4F_isError(lz4Return)); + compressedSize += lz4Return; + + /* Finish compression */ + lz4Return = LZ4F_compressEnd(ctx, dst + compressedSize, dstCapacity, NULL); + FUZZ_ASSERT(!LZ4F_isError(lz4Return)); + compressedSize += lz4Return; + + LZ4F_decompressOptions_t opts; + memset(&opts, 0, sizeof(opts)); + opts.stableDst = 1; + LZ4F_dctx *dctx; + LZ4F_createDecompressionContext(&dctx, LZ4F_VERSION); + FUZZ_ASSERT(dctx); + + decompress_data(dctx, dst, rt, rtCapacity, compressedSize); + + LZ4F_freeDecompressionContext(dctx); + + char *const expectedData = (char *)malloc(size + uncompressedSize); + memcpy(expectedData, data, uncompressedOffset); + memcpy(expectedData + uncompressedOffset, uncompressedData, uncompressedSize); + memcpy(expectedData + uncompressedOffset + uncompressedSize, + data + uncompressedOffset, size - uncompressedOffset); + + FUZZ_ASSERT_MSG(!memcmp(expectedData, rt, size), "Corruption!"); + free(expectedData); + + free(dst); + free(rt); + free(uncompressedData); + + FUZZ_dataProducer_free(producer); + FUZZ_dataProducer_free(uncompressedProducer); + LZ4F_freeCompressionContext(ctx); + return 0; +} -- cgit v0.12 From 3c57d2f185c2b482fde69a4c5f04dbb48bacab58 Mon Sep 17 00:00:00 2001 From: Alexander Mohr Date: Sat, 11 Jun 2022 22:46:49 +0200 Subject: lz4frame: fix different linkage error Signed-off-by: Alexander Mohr --- lib/lz4frame.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/lz4frame.c b/lib/lz4frame.c index c7e0595..788f19f 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -1047,7 +1047,7 @@ size_t LZ4F_compressUpdate(LZ4F_cctx* cctxPtr, * or an error code if it fails (which can be tested using LZ4F_isError()) * After an error, the state is left in a UB state, and must be re-initialized. */ -LZ4FLIB_API size_t LZ4F_uncompressedUpdate(LZ4F_cctx* cctxPtr, +LZ4FLIB_STATIC_API size_t LZ4F_uncompressedUpdate(LZ4F_cctx* cctxPtr, void* dstBuffer, size_t dstCapacity, const void* srcBuffer, size_t srcSize, const LZ4F_compressOptions_t* compressOptionsPtr) { -- cgit v0.12 From 9a42a9db94ff4c71de5590cae8d0f90339e6733b Mon Sep 17 00:00:00 2001 From: Alexander Mohr Date: Sat, 11 Jun 2022 23:14:56 +0200 Subject: dict-size: make lz4 context const change the context to const to make clear that the context is not modified --- doc/lz4_manual.html | 2 +- lib/lz4.c | 4 ++-- lib/lz4.h | 2 +- lib/lz4hc.c | 4 ++-- lib/lz4hc.h | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/doc/lz4_manual.html b/doc/lz4_manual.html index 13c1ae6..700cb84 100644 --- a/doc/lz4_manual.html +++ b/doc/lz4_manual.html @@ -391,7 +391,7 @@ int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream);
LZ4LIB_STATIC_API int LZ4_getDictSize (LZ4_stream_t* LZ4_dict, int dictSize); +LZ4LIB_STATIC_API int LZ4_getDictSize (const LZ4_stream_t* LZ4_dict, int dictSize);Get the size of the dictionary. This can be used for adding data without compression to the LZ4 archive. If linked blocked mode is used the memory of the dictionary is kept free. diff --git a/lib/lz4.c b/lib/lz4.c index 932e2cd..289162d 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1689,9 +1689,9 @@ int LZ4_compress_forceExtDict (LZ4_stream_t* LZ4_dict, const char* source, char* * @param dictSize The maximum dictionary size. (Normally 64 KB). * @return The size of the dictionary. */ -int LZ4_getDictSize (LZ4_stream_t* LZ4_dict, int dictSize) +int LZ4_getDictSize (const LZ4_stream_t* LZ4_dict, int dictSize) { - LZ4_stream_t_internal* const dict = &LZ4_dict->internal_donotuse; + const LZ4_stream_t_internal* const dict = &LZ4_dict->internal_donotuse; if ((U32)dictSize > 64 KB) { dictSize = 64 KB; } /* useless to define a dictionary > 64 KB */ if ((U32)dictSize > dict->dictSize) { dictSize = (int)dict->dictSize; } diff --git a/lib/lz4.h b/lib/lz4.h index f2a529f..ce2288e 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -519,7 +519,7 @@ LZ4LIB_STATIC_API void LZ4_attach_dictionary(LZ4_stream_t* workingStream, const * @param dictSize The maximum dictionary size. (Normally 64 KB). * @return The size of the dictionary. */ -LZ4LIB_STATIC_API int LZ4_getDictSize (LZ4_stream_t* LZ4_dict, int dictSize); +LZ4LIB_STATIC_API int LZ4_getDictSize (const LZ4_stream_t* LZ4_dict, int dictSize); /*! In-place compression and decompression * diff --git a/lib/lz4hc.c b/lib/lz4hc.c index bf6294d..b5bc880 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -1164,8 +1164,8 @@ int LZ4_compress_HC_continue_destSize (LZ4_streamHC_t* LZ4_streamHCPtr, const ch * @param dictSize The maximum dictionary size. (Normally 64 KB). * @return The size of the dictionary. */ -int LZ4_getDictHCSize(LZ4_streamHC_t* LZ4_streamHCPtr, int dictSize) { - LZ4HC_CCtx_internal* const streamPtr = &LZ4_streamHCPtr->internal_donotuse; +int LZ4_getDictHCSize(const LZ4_streamHC_t* LZ4_streamHCPtr, int dictSize) { + const LZ4HC_CCtx_internal* const streamPtr = &LZ4_streamHCPtr->internal_donotuse; int const prefixSize = (int)(streamPtr->end - (streamPtr->base + streamPtr->dictLimit)); DEBUGLOG(5, "LZ4_saveDictHC(%p, %p, %d)", LZ4_streamHCPtr, safeBuffer, dictSize); assert(prefixSize >= 0); diff --git a/lib/lz4hc.h b/lib/lz4hc.h index e62dfa7..3b31624 100644 --- a/lib/lz4hc.h +++ b/lib/lz4hc.h @@ -415,7 +415,7 @@ LZ4LIB_STATIC_API void LZ4_attach_HC_dictionary( * @param dictSize The maximum dictionary size. (Normally 64 KB). * @return The size of the dictionary. */ -LZ4LIB_STATIC_API int LZ4_getDictHCSize(LZ4_streamHC_t* LZ4_streamHCPtr, int dictSize); +LZ4LIB_STATIC_API int LZ4_getDictHCSize(const LZ4_streamHC_t* LZ4_streamHCPtr, int dictSize); #if defined (__cplusplus) } -- cgit v0.12 From af447b22c80d25c8d88cc3147c120e027b18d0ae Mon Sep 17 00:00:00 2001 From: Alexander Mohr
Date: Sun, 12 Jun 2022 00:41:55 +0200 Subject: meson: fix meson build add static dependency to examples --- contrib/meson/meson/examples/meson.build | 2 +- lib/lz4frame.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/contrib/meson/meson/examples/meson.build b/contrib/meson/meson/examples/meson.build index 791bd94..f6b6c41 100644 --- a/contrib/meson/meson/examples/meson.build +++ b/contrib/meson/meson/examples/meson.build @@ -26,7 +26,7 @@ foreach e, src : examples executable( e, lz4_source_root / 'examples' / src, - dependencies: liblz4_dep, + dependencies: [liblz4_dep, liblz4_internal_dep], install: false ) endforeach diff --git a/lib/lz4frame.c b/lib/lz4frame.c index 788f19f..251521e 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -1047,7 +1047,7 @@ size_t LZ4F_compressUpdate(LZ4F_cctx* cctxPtr, * or an error code if it fails (which can be tested using LZ4F_isError()) * After an error, the state is left in a UB state, and must be re-initialized. */ -LZ4FLIB_STATIC_API size_t LZ4F_uncompressedUpdate(LZ4F_cctx* cctxPtr, +size_t LZ4F_uncompressedUpdate(LZ4F_cctx* cctxPtr, void* dstBuffer, size_t dstCapacity, const void* srcBuffer, size_t srcSize, const LZ4F_compressOptions_t* compressOptionsPtr) { -- cgit v0.12 From 5065080664437efcb4215823b9ad95f5571a9d7c Mon Sep 17 00:00:00 2001 From: Alexander Mohr Date: Mon, 13 Jun 2022 07:46:53 +0200 Subject: ossfuzz: extend fuzzing test to include linked blocks fuzzing test now tests linked and independent blocks Signed-off-by: Alexander Mohr --- ossfuzz/round_trip_frame_uncompressed_fuzzer.c | 29 +++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/ossfuzz/round_trip_frame_uncompressed_fuzzer.c b/ossfuzz/round_trip_frame_uncompressed_fuzzer.c index 631d149..cf9cbb9 100644 --- a/ossfuzz/round_trip_frame_uncompressed_fuzzer.c +++ b/ossfuzz/round_trip_frame_uncompressed_fuzzer.c @@ -15,7 +15,7 @@ #include "lz4frame.h" #include "lz4frame_static.h" -static void decompress_data(LZ4F_dctx *dctx, void *src, void *dst, +static void decompress(LZ4F_dctx *dctx, void *src, void *dst, size_t dstCapacity, size_t readSize) { size_t ret = 1; const void *srcPtr = (const char *)src; @@ -40,9 +40,8 @@ static void decompress_data(LZ4F_dctx *dctx, void *src, void *dst, } } -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); +static void compress_round_trip(const uint8_t* data, size_t size, + FUZZ_dataProducer_t *producer, LZ4F_preferences_t const prefs) { size = FUZZ_dataProducer_remainingBytes(producer); uint8_t *uncompressedData = malloc(size); @@ -103,7 +102,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { LZ4F_createDecompressionContext(&dctx, LZ4F_VERSION); FUZZ_ASSERT(dctx); - decompress_data(dctx, dst, rt, rtCapacity, compressedSize); + decompress(dctx, dst, rt, rtCapacity, compressedSize); LZ4F_freeDecompressionContext(dctx); @@ -123,5 +122,25 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { FUZZ_dataProducer_free(producer); FUZZ_dataProducer_free(uncompressedProducer); LZ4F_freeCompressionContext(ctx); +} + +static void compress_linked_block_mode(const uint8_t* data, size_t size) { + FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size); + LZ4F_preferences_t prefs = FUZZ_dataProducer_preferences(producer); + prefs.frameInfo.blockMode = LZ4F_blockLinked; + compress_round_trip(data, size, producer, prefs); +} + +static void compress_independent_block_mode(const uint8_t* data, size_t size) { + FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size); + LZ4F_preferences_t prefs = FUZZ_dataProducer_preferences(producer); + prefs.frameInfo.blockMode = LZ4F_blockIndependent; + compress_round_trip(data, size, producer, prefs); +} + + +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { + compress_linked_block_mode(data, size); + compress_independent_block_mode(data, size); return 0; } -- cgit v0.12 From b2f61471941372a1aee49fefee1811a150757892 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 30 Jun 2022 18:47:08 -0700 Subject: test lz4 compression on a block device block device created as part of the test. Requires sudo rights. --- .github/workflows/ci.yml | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 33319ce..9e9c790 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -250,7 +250,6 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 # https://github.com/actions/checkout - - name: custom LZ4_DISTANCE_MAX; test LZ4_USER_MEMORY_FUNCTIONS run: | MOREFLAGS='-DLZ4_DISTANCE_MAX=8000' make V=1 check @@ -261,6 +260,21 @@ jobs: make V=1 clean CC="c++ -Wno-deprecated" make V=1 -C tests fullbench-wmalloc # stricter function signature check + # test block device compression #1086 + lz4cli-block-device: + name: Test lz4 compression on a block device + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 # https://github.com/actions/checkout + - name: create a block device, compress it with lz4 # alternative : blindly use /dev/loop0, seems to always exist + run: | + make lz4 + dd if=/dev/zero of=full0.img bs=2M count=1 + BLOCK_DEVICE=$(sudo losetup --show -fP full0.img) + sudo chmod 666 $BLOCK_DEVICE + ./lz4 -v $BLOCK_DEVICE -c > /dev/null + sudo losetup -d $BLOCK_DEVICE + rm full0.img ############################################################### -- cgit v0.12 From 24b50935f9d56bb82214961dbcbd83f8c296b44b Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 30 Jun 2022 21:53:29 -0700 Subject: fail on requesting to process 3+ file names in legacy mode warning only if -f is selected. --- programs/lz4cli.c | 50 ++++++++++++++++++++++++-------------------------- programs/lz4io.c | 2 +- tests/Makefile | 9 ++++----- 3 files changed, 29 insertions(+), 32 deletions(-) diff --git a/programs/lz4cli.c b/programs/lz4cli.c index b5cb000..6302505 100644 --- a/programs/lz4cli.c +++ b/programs/lz4cli.c @@ -314,6 +314,7 @@ int main(int argc, const char** argv) cLevelLast=-10000, legacy_format=0, forceStdout=0, + forceOverwrite=0, main_pause=0, multiple_inputs=0, all_arguments_are_files=0, @@ -491,7 +492,7 @@ int main(int argc, const char** argv) case 't': mode = om_test; break; /* Overwrite */ - case 'f': LZ4IO_setOverwrite(prefs, 1); break; + case 'f': forceOverwrite=1; LZ4IO_setOverwrite(prefs, 1); break; /* Verbose mode */ case 'v': displayLevel++; break; @@ -581,20 +582,24 @@ int main(int argc, const char** argv) } /* Store in *inFileNames[] if -m is used. */ - if (multiple_inputs) { inFileNames[ifnIdx++]=argument; continue; } + if (multiple_inputs) { inFileNames[ifnIdx++] = argument; continue; } - /* Store first non-option arg in input_filename to preserve original cli logic. */ - if (!input_filename) { input_filename=argument; continue; } + /* original cli logic : lz4 input output */ + /* First non-option arg is input_filename. */ + if (!input_filename) { input_filename = argument; continue; } - /* Second non-option arg in output_filename to preserve original cli logic. */ + /* Second non-option arg is output_filename */ if (!output_filename) { - output_filename=argument; + output_filename = argument; if (!strcmp (output_filename, nullOutput)) output_filename = nulmark; continue; } - /* 3rd non-option arg should not exist */ - DISPLAYLEVEL(1, "Warning : %s won't be used ! Do you want multiple input files (-m) ? \n", argument); + /* 3rd+ non-option arg should not exist */ + DISPLAYLEVEL(1, "%s : %s won't be used ! Do you want multiple input files (-m) ? \n", + forceOverwrite ? "Warning" : "Error", + argument); + if (!forceOverwrite) exit(1); } DISPLAYLEVEL(3, WELCOME_MESSAGE); @@ -659,8 +664,7 @@ int main(int argc, const char** argv) if (!strcmp(input_filename, stdinmark)) { /* if input==stdin and no output defined, stdout becomes default output */ if (!output_filename) output_filename = stdoutmark; - } - else{ + } else { #ifdef UTIL_HAS_CREATEFILELIST if (!recursive && !UTIL_isRegFile(input_filename)) { #else @@ -668,8 +672,7 @@ int main(int argc, const char** argv) #endif 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)) { @@ -679,7 +682,7 @@ int main(int argc, const char** argv) * 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; + output_filename = stdoutmark; break; } if (mode == om_auto) { /* auto-determine compression or decompression, based on file extension */ @@ -695,7 +698,7 @@ int main(int argc, const char** argv) DISPLAYLEVEL(2, "Compressed filename will be : %s \n", output_filename); break; } - if (mode == om_decompress) {/* decompression to file (automatic name will work only if input filename has correct format extension) */ + if (mode == om_decompress) {/* decompress to file (automatic output name only works if input filename has correct format extension) */ size_t outl; size_t const inl = strlen(input_filename); dynNameSpace = (char*)calloc(1,inl+1); @@ -704,20 +707,17 @@ int main(int argc, const char** argv) outl = inl; if (inl>4) while ((outl >= inl-4) && (input_filename[outl] == extension[outl-inl+4])) dynNameSpace[outl--]=0; - if (outl != inl-5) { DISPLAYLEVEL(1, "Cannot determine an output filename\n"); badusage(exeName); } + if (outl != inl-5) { DISPLAYLEVEL(1, "Cannot determine an output filename \n"); badusage(exeName); } output_filename = dynNameSpace; DISPLAYLEVEL(2, "Decoding file %s \n", output_filename); } break; } - if (mode == om_list){ - if(!multiple_inputs){ - inFileNames[ifnIdx++] = input_filename; - } - } - else{ - if (multiple_inputs==0) assert(output_filename); + if (mode == om_list) { + if (!multiple_inputs) inFileNames[ifnIdx++] = input_filename; + } else { + if (!multiple_inputs) assert(output_filename != NULL); } /* when multiple_inputs==1, output_filename may simply be useless, * however, output_filename must be !NULL for next strcmp() tests */ @@ -775,10 +775,8 @@ _cleanup: if (main_pause) waitEnter(); free(dynNameSpace); #ifdef UTIL_HAS_CREATEFILELIST - if (extendedFileList) { - UTIL_freeFileList(extendedFileList, fileNamesBuf); - inFileNames = NULL; - } + UTIL_freeFileList(extendedFileList, fileNamesBuf); + inFileNames = NULL; #endif LZ4IO_freePreferences(prefs); free((void*)inFileNames); diff --git a/programs/lz4io.c b/programs/lz4io.c index 6f636b5..c012215 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -348,7 +348,7 @@ static FILE* LZ4IO_openDstFile(const char* dstFileName, const LZ4IO_prefs_t* con DISPLAY("%s already exists; not overwritten \n", dstFileName); return NULL; } - DISPLAY("%s already exists; do you wish to overwrite (y/N) ? ", dstFileName); + DISPLAY("%s already exists; do you want to overwrite (y/N) ? ", dstFileName); { int ch = getchar(); if ((ch!='Y') && (ch!='y')) { DISPLAY(" not overwritten \n"); diff --git a/tests/Makefile b/tests/Makefile index 9f83f06..059fcda 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -154,6 +154,7 @@ endif # note : we should probably settle on a single compare utility CMP:=cmp +GREP:=grep DIFF:=diff ifneq (,$(filter $(shell $(UNAME)),SunOS)) DIFF:=gdiff @@ -347,8 +348,7 @@ test-lz4-basic: lz4 datagen unlz4 lz4cat $(LZ4) --rm -f tmp-tlb-hw tmp-tlb-hw.lz4 test ! -f tmp-tlb-hw # must fail (--rm) test -f tmp-tlb-hw.lz4 - $(PRGDIR)/lz4cat tmp-tlb-hw.lz4 # must display hello world - test -f tmp-tlb-hw.lz4 + $(PRGDIR)/lz4cat tmp-tlb-hw.lz4 | $(GREP) "hello world" $(PRGDIR)/unlz4 --rm tmp-tlb-hw.lz4 tmp-tlb-hw test -f tmp-tlb-hw test ! -f tmp-tlb-hw.lz4 # must fail (--rm) @@ -369,7 +369,8 @@ test-lz4-basic: lz4 datagen unlz4 lz4cat $(LZ4) -d --rm -- -z tmp-tlb4 # uncompresses ./-z into tmp-tlb4 test ! -f ./-z $(DIFF) -q tmp-tlb-hw tmp-tlb4 - $(LZ4) -f tmp-tlb-hw + ! $(LZ4) tmp-tlb2 tmp-tlb3 tmp-tlb4 # must fail: refuse to handle 3+ file names + $(LZ4) -f tmp-tlb-hw # create tmp-tlb-hw.lz4, for next tests $(LZ4) --list tmp-tlb-hw.lz4 # test --list on valid single-frame file $(LZ4) --list < tmp-tlb-hw.lz4 # test --list from stdin (file only) $(CAT) tmp-tlb-hw >> tmp-tlb-hw.lz4 @@ -392,7 +393,6 @@ test-lz4-basic: lz4 datagen unlz4 lz4cat @$(RM) tmp-tlb* - test-lz4-dict: lz4 datagen @echo "\n ---- test lz4 compression/decompression with dictionary ----" $(DATAGEN) -g16KB > tmp-dict @@ -415,7 +415,6 @@ test-lz4-dict: lz4 datagen < 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* test-lz4-hugefile: lz4 datagen -- cgit v0.12 From 35565bf0dc118c8825c437ec320572b6c78a2a3e Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 1 Jul 2022 01:36:32 -0700 Subject: refactored logic to test special file names --- programs/lz4cli.c | 8 +++-- programs/lz4io.c | 102 ++++++++++++++++++++++++++++++++---------------------- programs/util.h | 46 +++++++++++++++++------- 3 files changed, 98 insertions(+), 58 deletions(-) diff --git a/programs/lz4cli.c b/programs/lz4cli.c index 6302505..26148ee 100644 --- a/programs/lz4cli.c +++ b/programs/lz4cli.c @@ -654,17 +654,19 @@ int main(int argc, const char** argv) mode = om_decompress; /* defer to decompress */ } - /* compress or decompress */ + /* No input provided => use stdin */ if (!input_filename) input_filename = stdinmark; - /* Check if input is defined as console; trigger an error in this case */ + + /* Refuse to use the console as input */ if (!strcmp(input_filename, stdinmark) && IS_CONSOLE(stdin) ) { DISPLAYLEVEL(1, "refusing to read from a console\n"); exit(1); } + if (!strcmp(input_filename, stdinmark)) { /* if input==stdin and no output defined, stdout becomes default output */ if (!output_filename) output_filename = stdoutmark; - } else { + } else { /* input != stdin, so it's a file name */ #ifdef UTIL_HAS_CREATEFILELIST if (!recursive && !UTIL_isRegFile(input_filename)) { #else diff --git a/programs/lz4io.c b/programs/lz4io.c index c012215..8527fa7 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -103,10 +103,28 @@ 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 */ /************************************** +* 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 **************************************/ @@ -127,27 +145,6 @@ struct LZ4IO_prefs_s { int removeSrcFile; }; -/************************************** -* 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); \ -} - - -/************************************** -* Version modifiers -**************************************/ -#define DEFAULT_DECOMPRESSOR LZ4IO_decompressLZ4F /* ************************************************** */ @@ -295,6 +292,26 @@ void LZ4IO_setRemoveSrcFile(LZ4IO_prefs_t* const prefs, unsigned flag) /* ************************************************************************ ** +** ********************** String functions ********************* ** +** ************************************************************************ */ + +static int LZ4IO_isDevNull(const char* s) +{ + return UTIL_sameString(s, nulmark); +} + +static int LZ4IO_isStdin(const char* s) +{ + return UTIL_sameString(s, stdinmark); +} + +static int LZ4IO_isStdout(const char* s) +{ + return UTIL_sameString(s, stdoutmark); +} + + +/* ************************************************************************ ** ** ********************** LZ4 File / Pipe compression ********************* ** ** ************************************************************************ */ @@ -310,13 +327,13 @@ static FILE* LZ4IO_openSrcFile(const char* srcFileName) { FILE* f; - if (!strcmp (srcFileName, stdinmark)) { - DISPLAYLEVEL(4,"Using stdin for input\n"); + if (LZ4IO_isStdin(srcFileName)) { + DISPLAYLEVEL(4,"Using stdin for input \n"); f = stdin; SET_BINARY_MODE(stdin); } else { f = fopen(srcFileName, "rb"); - if ( f==NULL ) DISPLAYLEVEL(1, "%s: %s \n", srcFileName, strerror(errno)); + if (f==NULL) DISPLAYLEVEL(1, "%s: %s \n", srcFileName, strerror(errno)); } return f; @@ -331,7 +348,7 @@ static FILE* LZ4IO_openDstFile(const char* dstFileName, const LZ4IO_prefs_t* con FILE* f; assert(dstFileName != NULL); - if (!strcmp (dstFileName, stdoutmark)) { + if (LZ4IO_isStdout(dstFileName)) { DISPLAYLEVEL(4, "Using stdout for output \n"); f = stdout; SET_BINARY_MODE(stdout); @@ -340,7 +357,7 @@ static FILE* LZ4IO_openDstFile(const char* dstFileName, const LZ4IO_prefs_t* con " to force-enable it, add --sparse command \n"); } } else { - if (!prefs->overwrite && strcmp (dstFileName, nulmark)) { /* Check if destination file already exists */ + if (!prefs->overwrite && !LZ4IO_isDevNull(dstFileName)) { /* Check if destination file already exists */ FILE* const testf = fopen( dstFileName, "rb" ); if (testf != NULL) { /* dest exists, prompt for overwrite authorization */ fclose(testf); @@ -474,7 +491,7 @@ int LZ4IO_compressFilename_Legacy(const char* input_filename, const char* output free(in_buff); free(out_buff); fclose(finput); - if (strcmp(output_filename,stdoutmark)) fclose(foutput); /* do not close stdout */ + if (!LZ4IO_isStdout(output_filename)) fclose(foutput); /* do not close stdout */ return 0; } @@ -499,7 +516,7 @@ int LZ4IO_compressMultipleFilenames_Legacy( /* loop on each file */ for (i=0; i compress into a file => generate its name */ if (ofnSize <= ifnSize+suffixSize+1) { free(dstFileName); ofnSize = ifnSize + 20; @@ -1275,7 +1293,7 @@ LZ4IO_decompressDstFile(dRess_t ress, FILE* const foutput = LZ4IO_openDstFile(output_filename, prefs); if (foutput==NULL) return 1; /* failure */ - if ( strcmp(input_filename, stdinmark) + if ( !LZ4IO_isStdin(input_filename) && UTIL_getFileStat(input_filename, &statbuf)) stat_result = 1; @@ -1286,8 +1304,8 @@ LZ4IO_decompressDstFile(dRess_t ress, /* Copy owner, file permissions and modification time */ if ( stat_result != 0 - && strcmp (output_filename, stdoutmark) - && strcmp (output_filename, nulmark)) { + && !LZ4IO_isStdout(output_filename) + && !LZ4IO_isDevNull(output_filename)) { UTIL_setFileStat(output_filename, &statbuf); /* should return value be read ? or is silent fail good enough ? */ } @@ -1331,7 +1349,7 @@ int LZ4IO_decompressMultipleFilenames( for (i=0; i Date: Fri, 1 Jul 2022 01:45:12 -0700 Subject: fix #1086 just remove the specific code of #704, it's not necessary and produces side effects. --- programs/lz4cli.c | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/programs/lz4cli.c b/programs/lz4cli.c index 26148ee..54a2e03 100644 --- a/programs/lz4cli.c +++ b/programs/lz4cli.c @@ -666,15 +666,7 @@ int main(int argc, const char** argv) if (!strcmp(input_filename, stdinmark)) { /* if input==stdin and no output defined, stdout becomes default output */ if (!output_filename) output_filename = stdoutmark; - } else { /* input != stdin, so it's a file name */ -#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); - } } + } /* No output filename ==> try to select one automatically (when possible) */ while ((!output_filename) && (multiple_inputs==0)) { -- cgit v0.12 From fcbf585598da6af5878693a58fa460b772e8cc61 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 1 Jul 2022 02:07:43 -0700 Subject: minor refactor : EXM_THROW -> END_PROCESS --- programs/lz4io.c | 146 ++++++++++++++++++++++++++----------------------------- 1 file changed, 70 insertions(+), 76 deletions(-) diff --git a/programs/lz4io.c b/programs/lz4io.c index 8527fa7..78581d8 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -114,7 +114,7 @@ static clock_t g_time = 0; # define DEBUG 0 #endif #define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__); -#define EXM_THROW(error, ...) \ +#define END_PROCESS(error, ...) \ { \ DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \ DISPLAYLEVEL(1, "Error %i : ", error); \ @@ -124,9 +124,9 @@ static clock_t g_time = 0; } -/************************************** -* Local Parameters -**************************************/ +/* ************************************************** */ +/* ****************** Parameters ******************** */ +/* ************************************************** */ struct LZ4IO_prefs_s { int passThrough; @@ -145,16 +145,10 @@ struct LZ4IO_prefs_s { int removeSrcFile; }; - - -/* ************************************************** */ -/* ****************** Parameters ******************** */ -/* ************************************************** */ - LZ4IO_prefs_t* LZ4IO_defaultPreferences(void) { LZ4IO_prefs_t* const ret = (LZ4IO_prefs_t*)malloc(sizeof(*ret)); - if (!ret) EXM_THROW(21, "Allocation error : not enough memory"); + if (!ret) END_PROCESS(21, "Allocation error : not enough memory"); ret->passThrough = 0; ret->overwrite = 1; ret->testMode = 0; @@ -357,7 +351,8 @@ static FILE* LZ4IO_openDstFile(const char* dstFileName, const LZ4IO_prefs_t* con " to force-enable it, add --sparse command \n"); } } else { - if (!prefs->overwrite && !LZ4IO_isDevNull(dstFileName)) { /* Check if destination file already exists */ + if (!prefs->overwrite && !LZ4IO_isDevNull(dstFileName)) { + /* Check if destination file already exists */ FILE* const testf = fopen( dstFileName, "rb" ); if (testf != NULL) { /* dest exists, prompt for overwrite authorization */ fclose(testf); @@ -431,24 +426,24 @@ int LZ4IO_compressFilename_Legacy(const char* input_filename, const char* output /* Init */ clock_t const clockStart = clock(); if (finput == NULL) - EXM_THROW(20, "%s : open file error ", input_filename); + END_PROCESS(20, "%s : open file error ", input_filename); foutput = LZ4IO_openDstFile(output_filename, prefs); if (foutput == NULL) { fclose(finput); - EXM_THROW(20, "%s : open file error ", input_filename); + END_PROCESS(20, "%s : open file error ", input_filename); } /* Allocate Memory */ in_buff = (char*)malloc(LEGACY_BLOCKSIZE); out_buff = (char*)malloc((size_t)outBuffSize + 4); if (!in_buff || !out_buff) - EXM_THROW(21, "Allocation error : not enough memory"); + END_PROCESS(21, "Allocation error : not enough memory"); /* Write Archive Header */ LZ4IO_writeLE32(out_buff, LEGACY_MAGICNUMBER); if (fwrite(out_buff, 1, MAGICNUMBER_SIZE, foutput) != MAGICNUMBER_SIZE) - EXM_THROW(22, "Write error : cannot write header"); + END_PROCESS(22, "Write error : cannot write header"); /* Main Loop */ while (1) { @@ -471,13 +466,13 @@ int LZ4IO_compressFilename_Legacy(const char* input_filename, const char* output assert(outSize < outBuffSize); LZ4IO_writeLE32(out_buff, (unsigned)outSize); if (fwrite(out_buff, 1, (size_t)outSize+4, foutput) != (size_t)(outSize+4)) { - EXM_THROW(24, "Write error : cannot write compressed block"); + END_PROCESS(24, "Write error : cannot write compressed block"); } } - if (ferror(finput)) EXM_THROW(25, "Error while reading %s ", input_filename); + if (ferror(finput)) END_PROCESS(24, "Error while reading %s ", input_filename); /* Status */ clockEnd = clock(); - if (clockEnd==clockStart) clockEnd+=1; /* avoid division by zero (speed) */ + clockEnd += (clockEnd==clockStart); /* avoid division by zero (speed) */ filesize += !filesize; /* avoid division by zero (ratio) */ DISPLAYLEVEL(2, "\r%79s\r", ""); /* blank line */ DISPLAYLEVEL(2,"Compressed %llu bytes into %llu bytes ==> %.2f%%\n", @@ -491,7 +486,7 @@ int LZ4IO_compressFilename_Legacy(const char* input_filename, const char* output free(in_buff); free(out_buff); fclose(finput); - if (!LZ4IO_isStdout(output_filename)) fclose(foutput); /* do not close stdout */ + if (!LZ4IO_isStdout(output_filename)) fclose(foutput); /* do not close stdout */ return 0; } @@ -548,7 +543,6 @@ int LZ4IO_compressMultipleFilenames_Legacy( /********************************************* * Compression using Frame format *********************************************/ - typedef struct { void* srcBuffer; size_t srcBufferSize; @@ -569,11 +563,11 @@ static void* LZ4IO_createDict(size_t* dictSize, const char* const dictFilename) 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"); + if (!circularBuf) END_PROCESS(25, "Allocation error : not enough memory for circular buffer"); + if (!dictFilename) END_PROCESS(26, "Dictionary error : no filename provided"); dictFile = LZ4IO_openSrcFile(dictFilename); - if (!dictFile) EXM_THROW(25, "Dictionary error : could not open dictionary file"); + if (!dictFile) END_PROCESS(27, "Dictionary error : could not open dictionary file"); /* opportunistically seek to the part of the file we care about. * If this fails it's not a problem since we'll just read everything anyways. */ @@ -602,7 +596,7 @@ static void* LZ4IO_createDict(size_t* dictSize, const char* const dictFilename) } else { /* Otherwise, we will alloc a new buffer and copy our dict into that. */ dictBuf = (char *)malloc(dictLen ? dictLen : 1); - if (!dictBuf) EXM_THROW(25, "Allocation error : not enough memory"); + if (!dictBuf) END_PROCESS(28, "Allocation error : not enough memory"); memcpy(dictBuf, circularBuf + dictStart, circularBufSize - dictStart); memcpy(dictBuf + circularBufSize - dictStart, circularBuf, dictLen - (circularBufSize - dictStart)); @@ -621,7 +615,7 @@ static LZ4F_CDict* LZ4IO_createCDict(const LZ4IO_prefs_t* const prefs) LZ4F_CDict* cdict; if (!prefs->useDictionary) return NULL; dictionaryBuffer = LZ4IO_createDict(&dictionarySize, prefs->dictionaryFilename); - if (!dictionaryBuffer) EXM_THROW(25, "Dictionary error : could not create dictionary"); + if (!dictionaryBuffer) END_PROCESS(29, "Dictionary error : could not create dictionary"); cdict = LZ4F_createCDict(dictionaryBuffer, dictionarySize); free(dictionaryBuffer); return cdict; @@ -633,14 +627,14 @@ static cRess_t LZ4IO_createCResources(const LZ4IO_prefs_t* const prefs) cRess_t ress; LZ4F_errorCode_t const errorCode = LZ4F_createCompressionContext(&(ress.ctx), LZ4F_VERSION); - if (LZ4F_isError(errorCode)) EXM_THROW(30, "Allocation error : can't create LZ4F context : %s", LZ4F_getErrorName(errorCode)); + if (LZ4F_isError(errorCode)) END_PROCESS(30, "Allocation error : can't create LZ4F context : %s", LZ4F_getErrorName(errorCode)); /* Allocate Memory */ ress.srcBuffer = malloc(blockSize); ress.srcBufferSize = blockSize; ress.dstBufferSize = LZ4F_compressFrameBound(blockSize, NULL); /* cover worst case */ ress.dstBuffer = malloc(ress.dstBufferSize); - if (!ress.srcBuffer || !ress.dstBuffer) EXM_THROW(31, "Allocation error : not enough memory"); + if (!ress.srcBuffer || !ress.dstBuffer) END_PROCESS(31, "Allocation error : not enough memory"); ress.cdict = LZ4IO_createCDict(prefs); @@ -656,7 +650,7 @@ static void LZ4IO_freeCResources(cRess_t ress) ress.cdict = NULL; { LZ4F_errorCode_t const errorCode = LZ4F_freeCompressionContext(ress.ctx); - if (LZ4F_isError(errorCode)) EXM_THROW(38, "Error : can't free LZ4F context resource : %s", LZ4F_getErrorName(errorCode)); } + if (LZ4F_isError(errorCode)) END_PROCESS(35, "Error : can't free LZ4F context resource : %s", LZ4F_getErrorName(errorCode)); } } /* @@ -704,7 +698,7 @@ LZ4IO_compressFilename_extRess(cRess_t ress, /* read first block */ readSize = fread(srcBuffer, (size_t)1, blockSize, srcFile); - if (ferror(srcFile)) EXM_THROW(30, "Error reading %s ", srcFileName); + if (ferror(srcFile)) END_PROCESS(40, "Error reading %s ", srcFileName); filesize += readSize; /* single-block file */ @@ -712,14 +706,14 @@ LZ4IO_compressFilename_extRess(cRess_t ress, /* Compress in single pass */ 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)); + END_PROCESS(41, "Compression failed : %s", LZ4F_getErrorName(cSize)); compressedfilesize = cSize; DISPLAYUPDATE(2, "\rRead : %u MiB ==> %.2f%% ", (unsigned)(filesize>>20), (double)compressedfilesize/(filesize+!filesize)*100); /* avoid division by zero */ /* Write Block */ if (fwrite(dstBuffer, 1, cSize, dstFile) != cSize) { - EXM_THROW(32, "Write error : failed writing single-block compressed frame"); + END_PROCESS(42, "Write error : failed writing single-block compressed frame"); } } else @@ -728,36 +722,36 @@ LZ4IO_compressFilename_extRess(cRess_t ress, { /* 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)); + if (LZ4F_isError(headerSize)) END_PROCESS(43, "File header generation failed : %s", LZ4F_getErrorName(headerSize)); if (fwrite(dstBuffer, 1, headerSize, dstFile) != headerSize) - EXM_THROW(34, "Write error : cannot write header"); + END_PROCESS(44, "Write error : cannot write header"); compressedfilesize += headerSize; /* Main Loop - one block at a time */ while (readSize>0) { 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)); + END_PROCESS(45, "Compression failed : %s", LZ4F_getErrorName(outSize)); compressedfilesize += outSize; DISPLAYUPDATE(2, "\rRead : %u MiB ==> %.2f%% ", (unsigned)(filesize>>20), (double)compressedfilesize/filesize*100); /* Write Block */ if (fwrite(dstBuffer, 1, outSize, dstFile) != outSize) - EXM_THROW(36, "Write error : cannot write compressed block"); + END_PROCESS(46, "Write error : cannot write compressed block"); /* Read next block */ readSize = fread(srcBuffer, (size_t)1, (size_t)blockSize, srcFile); filesize += readSize; } - if (ferror(srcFile)) EXM_THROW(37, "Error reading %s ", srcFileName); + if (ferror(srcFile)) END_PROCESS(47, "Error reading %s ", srcFileName); /* 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)); + END_PROCESS(48, "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"); + END_PROCESS(49, "Write error : cannot write end of frame"); compressedfilesize += endSize; } } @@ -776,7 +770,7 @@ LZ4IO_compressFilename_extRess(cRess_t ress, if (io_prefs->removeSrcFile) { /* remove source file : --rm */ if (remove(srcFileName)) - EXM_THROW(40, "Remove error : %s: %s", srcFileName, strerror(errno)); + END_PROCESS(50, "Remove error : %s: %s", srcFileName, strerror(errno)); } /* Final Status */ @@ -896,14 +890,14 @@ LZ4IO_fwriteSparse(FILE* file, 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"); + if (sizeCheck != bufferSize) END_PROCESS(70, "Write error : cannot write decoded block"); return 0; } /* avoid int overflow */ if (storedSkips > 1 GB) { int const seekResult = UTIL_fseek(file, 1 GB, SEEK_CUR); - if (seekResult != 0) EXM_THROW(71, "1 GB skip error (sparse file support)"); + if (seekResult != 0) END_PROCESS(71, "1 GB skip error (sparse file support)"); storedSkips -= 1 GB; } @@ -920,13 +914,13 @@ LZ4IO_fwriteSparse(FILE* file, if (nb0T != seg0SizeT) { /* not all 0s */ errno = 0; { int const seekResult = UTIL_fseek(file, storedSkips, SEEK_CUR); - if (seekResult) EXM_THROW(72, "Sparse skip error(%d): %s ; try --no-sparse", (int)errno, strerror(errno)); + if (seekResult) END_PROCESS(72, "Sparse skip error(%d): %s ; try --no-sparse", (int)errno, strerror(errno)); } storedSkips = 0; seg0SizeT -= nb0T; ptrT += nb0T; { size_t const sizeCheck = fwrite(ptrT, sizeT, seg0SizeT, file); - if (sizeCheck != seg0SizeT) EXM_THROW(73, "Write error : cannot write decoded block"); + if (sizeCheck != seg0SizeT) END_PROCESS(73, "Write error : cannot write decoded block"); } } ptrT += seg0SizeT; } @@ -940,10 +934,10 @@ LZ4IO_fwriteSparse(FILE* file, storedSkips += (unsigned) (restPtr - restStart); if (restPtr != restEnd) { int const seekResult = UTIL_fseek(file, storedSkips, SEEK_CUR); - if (seekResult) EXM_THROW(74, "Sparse skip error ; try --no-sparse"); + if (seekResult) END_PROCESS(74, "Sparse skip error ; try --no-sparse"); storedSkips = 0; { 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"); + if (sizeCheck != (size_t)(restEnd - restPtr)) END_PROCESS(75, "Write error : cannot write decoded end of block"); } } } @@ -955,9 +949,9 @@ static void LZ4IO_fwriteSparseEnd(FILE* file, unsigned storedSkips) 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"); + END_PROCESS(68, "Final skip error (sparse file)\n"); if (fwrite(lastZeroByte, 1, 1, file) != 1) - EXM_THROW(69, "Write error : cannot write last zero\n"); + END_PROCESS(69, "Write error : cannot write last zero\n"); } } @@ -973,7 +967,7 @@ LZ4IO_decodeLegacyStream(FILE* finput, FILE* foutput, const LZ4IO_prefs_t* prefs /* Allocate Memory */ 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"); + if (!in_buff || !out_buff) END_PROCESS(51, "Allocation error : not enough memory"); /* Main Loop */ while (1) { @@ -982,7 +976,7 @@ LZ4IO_decodeLegacyStream(FILE* finput, FILE* foutput, const LZ4IO_prefs_t* prefs /* Block Size */ { size_t const sizeCheck = fread(in_buff, 1, LZ4IO_LEGACY_BLOCK_HEADER_SIZE, finput); if (sizeCheck == 0) break; /* Nothing to read : file read is completed */ - if (sizeCheck != LZ4IO_LEGACY_BLOCK_HEADER_SIZE) EXM_THROW(52, "Read error : cannot access block size "); + if (sizeCheck != LZ4IO_LEGACY_BLOCK_HEADER_SIZE) END_PROCESS(52, "Read error : cannot access block size "); } blockSize = LZ4IO_readLE32(in_buff); /* Convert to Little Endian */ if (blockSize > LZ4_COMPRESSBOUND(LEGACY_BLOCKSIZE)) { @@ -993,16 +987,16 @@ LZ4IO_decodeLegacyStream(FILE* finput, FILE* foutput, const LZ4IO_prefs_t* prefs /* Read Block */ { size_t const sizeCheck = fread(in_buff, 1, blockSize, finput); - if (sizeCheck != blockSize) EXM_THROW(52, "Read error : cannot access compressed block !"); } + if (sizeCheck != blockSize) END_PROCESS(53, "Read error : cannot access compressed block !"); } /* Decode Block */ { 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 !"); + if (decodeSize < 0) END_PROCESS(54, "Decoding Failed ! Corrupted input detected !"); streamSize += (unsigned long long)decodeSize; /* Write Block */ storedSkips = LZ4IO_fwriteSparse(foutput, out_buff, (size_t)decodeSize, prefs->sparseFileSupport, storedSkips); /* success or die */ } } - if (ferror(finput)) EXM_THROW(54, "Read error : ferror"); + if (ferror(finput)) END_PROCESS(55, "Read error : ferror"); LZ4IO_fwriteSparseEnd(foutput, storedSkips); @@ -1035,7 +1029,7 @@ static void LZ4IO_loadDDict(dRess_t* ress, const LZ4IO_prefs_t* const prefs) } ress->dictBuffer = LZ4IO_createDict(&ress->dictBufferSize, prefs->dictionaryFilename); - if (!ress->dictBuffer) EXM_THROW(25, "Dictionary error : could not create dictionary"); + if (!ress->dictBuffer) END_PROCESS(25, "Dictionary error : could not create dictionary"); } static const size_t LZ4IO_dBufferSize = 64 KB; @@ -1045,14 +1039,14 @@ static dRess_t LZ4IO_createDResources(const LZ4IO_prefs_t* const prefs) /* init */ 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)); + if (LZ4F_isError(errorCode)) END_PROCESS(60, "Can't create LZ4F context : %s", LZ4F_getErrorName(errorCode)); /* Allocate Memory */ ress.srcBufferSize = LZ4IO_dBufferSize; ress.srcBuffer = malloc(ress.srcBufferSize); ress.dstBufferSize = LZ4IO_dBufferSize; ress.dstBuffer = malloc(ress.dstBufferSize); - if (!ress.srcBuffer || !ress.dstBuffer) EXM_THROW(61, "Allocation error : not enough memory"); + if (!ress.srcBuffer || !ress.dstBuffer) END_PROCESS(61, "Allocation error : not enough memory"); LZ4IO_loadDDict(&ress, prefs); @@ -1063,7 +1057,7 @@ static dRess_t LZ4IO_createDResources(const LZ4IO_prefs_t* const prefs) static void LZ4IO_freeDResources(dRess_t ress) { LZ4F_errorCode_t errorCode = LZ4F_freeDecompressionContext(ress.dCtx); - if (LZ4F_isError(errorCode)) EXM_THROW(69, "Error : can't free LZ4F context resource : %s", LZ4F_getErrorName(errorCode)); + if (LZ4F_isError(errorCode)) END_PROCESS(69, "Error : can't free LZ4F context resource : %s", LZ4F_getErrorName(errorCode)); free(ress.srcBuffer); free(ress.dstBuffer); free(ress.dictBuffer); @@ -1084,7 +1078,7 @@ LZ4IO_decompressLZ4F(dRess_t ress, size_t outSize= 0; LZ4IO_writeLE32(ress.srcBuffer, LZ4IO_MAGICNUMBER); nextToLoad = LZ4F_decompress_usingDict(ress.dCtx, ress.dstBuffer, &outSize, ress.srcBuffer, &inSize, ress.dictBuffer, ress.dictBufferSize, NULL); - if (LZ4F_isError(nextToLoad)) EXM_THROW(62, "Header error : %s", LZ4F_getErrorName(nextToLoad)); + if (LZ4F_isError(nextToLoad)) END_PROCESS(62, "Header error : %s", LZ4F_getErrorName(nextToLoad)); } /* Main Loop */ @@ -1103,7 +1097,7 @@ LZ4IO_decompressLZ4F(dRess_t ress, size_t remaining = readSize - pos; decodedBytes = ress.dstBufferSize; nextToLoad = LZ4F_decompress_usingDict(ress.dCtx, ress.dstBuffer, &decodedBytes, (char*)(ress.srcBuffer)+pos, &remaining, ress.dictBuffer, ress.dictBufferSize, NULL); - if (LZ4F_isError(nextToLoad)) EXM_THROW(66, "Decompression error : %s", LZ4F_getErrorName(nextToLoad)); + if (LZ4F_isError(nextToLoad)) END_PROCESS(66, "Decompression error : %s", LZ4F_getErrorName(nextToLoad)); pos += remaining; /* Write Block */ @@ -1118,10 +1112,10 @@ LZ4IO_decompressLZ4F(dRess_t ress, } } /* can be out because readSize == 0, which could be an fread() error */ - if (ferror(srcFile)) EXM_THROW(67, "Read error"); + if (ferror(srcFile)) END_PROCESS(67, "Read error"); if (!prefs->testMode) LZ4IO_fwriteSparseEnd(dstFile, storedSkips); - if (nextToLoad!=0) EXM_THROW(68, "Unfinished stream"); + if (nextToLoad!=0) END_PROCESS(68, "Unfinished stream"); return filesize; } @@ -1145,14 +1139,14 @@ LZ4IO_passThrough(FILE* finput, FILE* foutput, unsigned storedSkips = 0; if (fwrite(MNstore, 1, MAGICNUMBER_SIZE, foutput) != MAGICNUMBER_SIZE) { - EXM_THROW(50, "Pass-through write error"); + END_PROCESS(50, "Pass-through write error"); } while (readBytes) { readBytes = fread(buffer, 1, sizeof(buffer), finput); total += readBytes; storedSkips = LZ4IO_fwriteSparse(foutput, buffer, readBytes, sparseFileSupport, storedSkips); } - if (ferror(finput)) EXM_THROW(51, "Read Error"); + if (ferror(finput)) END_PROCESS(51, "Read Error"); LZ4IO_fwriteSparseEnd(foutput, storedSkips); return total; @@ -1198,7 +1192,7 @@ selectDecoder(dRess_t ress, size_t const nbReadBytes = fread(MNstore, 1, MAGICNUMBER_SIZE, finput); if (nbReadBytes==0) { nbFrames = 0; return ENDOFSTREAM; } /* EOF */ if (nbReadBytes != MAGICNUMBER_SIZE) - EXM_THROW(40, "Unrecognized header : Magic Number unreadable"); + END_PROCESS(40, "Unrecognized header : Magic Number unreadable"); magicNumber = LZ4IO_readLE32(MNstore); /* Little Endian format */ } if (LZ4IO_isSkippableMagicNumber(magicNumber)) @@ -1215,12 +1209,12 @@ selectDecoder(dRess_t ress, DISPLAYLEVEL(4, "Skipping detected skippable area \n"); { size_t const nbReadBytes = fread(MNstore, 1, 4, finput); if (nbReadBytes != 4) - EXM_THROW(42, "Stream error : skippable size unreadable"); + END_PROCESS(42, "Stream error : skippable size unreadable"); } { unsigned const size = LZ4IO_readLE32(MNstore); int const errorNb = fseek_u32(finput, size, SEEK_CUR); if (errorNb != 0) - EXM_THROW(43, "Stream error : cannot skip skippable area"); + END_PROCESS(43, "Stream error : cannot skip skippable area"); } return 0; default: @@ -1230,7 +1224,7 @@ selectDecoder(dRess_t ress, nbFrames = 0; return LZ4IO_passThrough(finput, foutput, MNstore, prefs->sparseFileSupport); } - EXM_THROW(44,"Unrecognized header : file cannot be decoded"); + END_PROCESS(44,"Unrecognized header : file cannot be decoded"); } { long int const position = ftell(finput); /* only works for files < 2 GB */ DISPLAYLEVEL(2, "Stream followed by undecodable data "); @@ -1270,7 +1264,7 @@ LZ4IO_decompressSrcFile(dRess_t ress, fclose(finput); if (prefs->removeSrcFile) { /* --rm */ if (remove(input_filename)) - EXM_THROW(45, "Remove error : %s: %s", input_filename, strerror(errno)); + END_PROCESS(45, "Remove error : %s: %s", input_filename, strerror(errno)); } /* Final Status */ @@ -1343,7 +1337,7 @@ int LZ4IO_decompressMultipleFilenames( size_t const suffixSize = strlen(suffix); dRess_t ress = LZ4IO_createDResources(prefs); - if (outFileName==NULL) EXM_THROW(70, "Memory allocation error"); + if (outFileName==NULL) END_PROCESS(70, "Memory allocation error"); ress.dstFile = LZ4IO_openDstFile(stdoutmark, prefs); for (i=0; i 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); + if (!readBytes || ferror(finput)) END_PROCESS(71, "Error reading %s", input_filename); } { size_t hSize = LZ4F_headerSize(&buffer, LZ4F_HEADER_SIZE_MIN); 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); + if (!readBytes || ferror(finput)) END_PROCESS(72, "Error reading %s", input_filename); } /* Create decompression context */ { LZ4F_dctx* dctx; @@ -1635,12 +1629,12 @@ LZ4IO_getCompressedFileInfo(LZ4IO_cFileInfo_t* cfinfo, const char* input_filenam cfinfo->allContentSize = 0; { size_t const nbReadBytes = fread(buffer, 1, 4, finput); if (nbReadBytes != 4) - EXM_THROW(42, "Stream error : skippable size unreadable"); + END_PROCESS(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"); + END_PROCESS(43, "Stream error : cannot skip skippable area"); DISPLAYLEVEL(3, " %6llu %14s %5s %8s %20u %20s %9s\n", cfinfo->frameCount + 1, "SkippableFrame", -- cgit v0.12 From 149644df49ba112daeff85f0bed229253516cc8c Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 1 Jul 2022 02:21:44 -0700 Subject: fixed -tm which was broken up to now. --- programs/lz4cli.c | 6 ++++-- programs/lz4io.c | 4 ++-- tests/Makefile | 5 +++++ 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/programs/lz4cli.c b/programs/lz4cli.c index 54a2e03..45f88f4 100644 --- a/programs/lz4cli.c +++ b/programs/lz4cli.c @@ -739,8 +739,10 @@ 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); + const char* dec_extension = LZ4_EXTENSION; + if (!strcmp(output_filename, stdoutmark)) dec_extension = stdoutmark; + if (!strcmp(output_filename, nulmark)) dec_extension = nulmark; + assert(ifnIdx < INT_MAX); operationResult = LZ4IO_decompressMultipleFilenames(inFileNames, (int)ifnIdx, dec_extension, prefs); } else { operationResult = DEFAULT_DECOMPRESSOR(input_filename, output_filename, prefs); diff --git a/programs/lz4io.c b/programs/lz4io.c index 78581d8..36736c2 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -1343,8 +1343,8 @@ int LZ4IO_decompressMultipleFilenames( for (i=0; i Date: Fri, 1 Jul 2022 02:27:43 -0700 Subject: fixed minor leak --- programs/lz4cli.c | 10 +++------- programs/util.h | 4 ++-- tests/Makefile | 2 +- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/programs/lz4cli.c b/programs/lz4cli.c index 45f88f4..254a6ce 100644 --- a/programs/lz4cli.c +++ b/programs/lz4cli.c @@ -331,9 +331,8 @@ int main(int argc, const char** argv) const char extension[] = LZ4_EXTENSION; size_t blockSize = LZ4IO_setBlockSizeID(prefs, LZ4_BLOCKSIZEID_DEFAULT); const char* const exeName = lastNameFromPath(argv[0]); -#ifdef UTIL_HAS_CREATEFILELIST - const char** extendedFileList = NULL; char* fileNamesBuf = NULL; +#ifdef UTIL_HAS_CREATEFILELIST unsigned fileNamesNb, recursive=0; #endif @@ -622,7 +621,7 @@ int main(int argc, const char** argv) input_filename = inFileNames[0]; #ifdef UTIL_HAS_CREATEFILELIST if (recursive) { /* at this stage, filenameTable is a list of paths, which can contain both files and directories */ - extendedFileList = UTIL_createFileList(inFileNames, ifnIdx, &fileNamesBuf, &fileNamesNb); + const char** extendedFileList = UTIL_createFileList(inFileNames, ifnIdx, &fileNamesBuf, &fileNamesNb); if (extendedFileList) { unsigned u; for (u=0; u Date: Fri, 1 Jul 2022 10:36:22 -0700 Subject: ignore ossfuzz artifacts --- ossfuzz/.gitignore | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 ossfuzz/.gitignore diff --git a/ossfuzz/.gitignore b/ossfuzz/.gitignore new file mode 100644 index 0000000..2dc764a --- /dev/null +++ b/ossfuzz/.gitignore @@ -0,0 +1,27 @@ +# Object files +*.o +*.ko + +# Libraries +*.lib +*.a + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib +*.dSYM # apple + +# Executables +compress_frame_fuzzer +compress_fuzzer +compress_hc_fuzzer +decompress_frame_fuzzer +decompress_fuzzer +round_trip_frame_fuzzer +round_trip_fuzzer +round_trip_hc_fuzzer +round_trip_stream_fuzzer + + -- cgit v0.12 From 5392531e8711a4b7f60927af1c0ac3cb1a91d2b0 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 1 Jul 2022 14:53:16 -0700 Subject: added fuzzer test for LZ4F_uncompressedUpdate in frametest --- tests/frametest.c | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/tests/frametest.c b/tests/frametest.c index 09def51..939d4c2 100644 --- a/tests/frametest.c +++ b/tests/frametest.c @@ -54,7 +54,7 @@ /* unoptimized version; solves endianness & alignment issues */ static void FUZ_writeLE32 (void* dstVoidPtr, U32 value32) { - BYTE* dstPtr = (BYTE*)dstVoidPtr; + BYTE* const dstPtr = (BYTE*)dstVoidPtr; dstPtr[0] = (BYTE) value32; dstPtr[1] = (BYTE)(value32 >> 8); dstPtr[2] = (BYTE)(value32 >> 16); @@ -1015,18 +1015,38 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi while (ip < iend) { unsigned const nbBitsSeg = FUZ_rand(&randState) % maxBits; size_t const sampleMax = (FUZ_rand(&randState) & ((1< 0) && !neverFlush && ((FUZ_rand(&randState) & 15) == 1)) { + size_t const uSize = FUZ_rand(&randState) % iSize; + DISPLAYLEVEL(2, "insert %zu / %zu (blockSize=%uKB) \n", uSize, iSize, 1 << (2*prefs.frameInfo.blockSizeID - 2)); + { size_t const flushedSize = LZ4F_uncompressedUpdate(cCtx, op, (size_t)(oend-op), ip, uSize, &cOptions); + CHECK(LZ4F_isError(flushedSize), "Insert uncompressed data failed (error %i : %s)", + (int)flushedSize, LZ4F_getErrorName(flushedSize)); + op += flushedSize; + ip += uSize; + } + iSize -= uSize; + { size_t const flushedSize = LZ4F_flush(cCtx, op, (size_t)(oend-op), &cOptions); + CHECK(LZ4F_isError(flushedSize), "Flush after LZ4F_uncompressedUpdate failed (error %i : %s)", + (int)flushedSize, LZ4F_getErrorName(flushedSize)); + op += flushedSize; + } + } +#endif + + { size_t const flushedSize = LZ4F_compressUpdate(cCtx, op, oSize, ip, iSize, &cOptions); + CHECK(LZ4F_isError(flushedSize), "Compression failed (error %i : %s)", (int)flushedSize, LZ4F_getErrorName(flushedSize)); - op += flushedSize; - ip += iSize; + op += flushedSize; + ip += iSize; + } { unsigned const forceFlush = neverFlush ? 0 : ((FUZ_rand(&randState) & 3) == 1); if (forceFlush) { @@ -1040,11 +1060,8 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi op[3] = 0x80; /* 0x80000000U in little-endian format */ op += 4; if ((prefsPtr!= NULL) && prefsPtr->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); + /* add block checksum (even for empty blocks) */ + FUZ_writeLE32(op, XXH32(op, 0, 0)); op += 4; } } } } } /* while (ip Date: Mon, 4 Jul 2022 08:51:37 +0200 Subject: lz4frame: correct start and size after flush when the block mode changes a flush is executed, to prevent mixing compressed and uncompressed data. Prior to this commit dstStart, dstPtr, dstCapacity where not updated to include the offset from bytesWritten. For inputs > blockSize this meant the flushed data was overwritten. Signed-off-by: Alexander Mohr --- lib/lz4frame.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/lz4frame.c b/lib/lz4frame.c index 251521e..f32ed1d 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -886,7 +886,7 @@ static size_t LZ4F_compressUpdateImpl(LZ4F_cctx* cctxPtr, size_t const blockSize = cctxPtr->maxBlockSize; const BYTE* srcPtr = (const BYTE*)srcBuffer; const BYTE* const srcEnd = srcPtr + srcSize; - BYTE* const dstStart = (BYTE*)dstBuffer; + BYTE* dstStart = (BYTE*)dstBuffer; BYTE* dstPtr = dstStart; LZ4F_lastBlockStatus lastBlockCompressed = notDone; compressFunc_t const compress = LZ4F_selectCompression(cctxPtr->prefs.frameInfo.blockMode, cctxPtr->prefs.compressionLevel); @@ -896,6 +896,9 @@ static size_t LZ4F_compressUpdateImpl(LZ4F_cctx* cctxPtr, /* flush currently written block, to continue with new block compression */ if (cctxPtr->blockCompression != blockCompression) { bytesWritten = LZ4F_flush(cctxPtr, dstBuffer, dstCapacity, compressOptionsPtr); + dstStart = (BYTE*)dstBuffer + bytesWritten; + dstPtr = dstStart; + dstCapacity -= bytesWritten; cctxPtr->blockCompression = blockCompression; } -- cgit v0.12 From 6fb713358b0ccb66dc5a61f99530be7e145a85b4 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 4 Jul 2022 14:29:35 -0700 Subject: silence a useless MSVC warning --- lib/lz4.c | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/lz4.c b/lib/lz4.c index 7f4f175..7374f6e 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -124,6 +124,7 @@ #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 */ +# pragma warning(disable : 6237) /* disable: C6237: conditional expression is always 0 */ #endif /* _MSC_VER */ #ifndef LZ4_FORCE_INLINE -- cgit v0.12 From 0af5edc87395084803bc70b529a758f6e69b97ff Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 4 Jul 2022 15:58:47 -0700 Subject: updated dll README --- lib/dll/example/README.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/dll/example/README.md b/lib/dll/example/README.md index 223e473..6d93248 100644 --- a/lib/dll/example/README.md +++ b/lib/dll/example/README.md @@ -4,8 +4,8 @@ LZ4 Windows binary package #### The package contents - `lz4.exe` : Command Line Utility, supporting gzip-like arguments -- `dll\liblz4.dll` : The DLL of LZ4 library -- `dll\liblz4.lib` : The import library of LZ4 library for Visual C++ +- `dll\msys-lz4-1.dll` : The DLL of LZ4 library, compiled by msys +- `dll\liblz4.dll.a` : The import library of LZ4 library for Visual C++ - `example\` : The example of usage of LZ4 library - `include\` : Header files required with LZ4 library - `static\liblz4_static.lib` : The static LZ4 library @@ -35,15 +35,15 @@ Use `cd example` and `make` to build `fullbench-dll` and `fullbench-lib`. #### Using LZ4 DLL with gcc/MinGW -The header files from `include\` and the dynamic library `dll\liblz4.dll` +The header files from `include\` and the dynamic library `dll\msys-lz4-1.dll` are required to compile a project using gcc/MinGW. 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: +file it should be linked with `dll\msys-lz4-1.dll`. For example: ``` - gcc $(CFLAGS) -Iinclude\ test-dll.c -o test-dll dll\liblz4.dll + gcc $(CFLAGS) -Iinclude\ test-dll.c -o test-dll dll\msys-lz4-1.dll ``` -The compiled executable will require LZ4 DLL which is available at `dll\liblz4.dll`. +The compiled executable will require LZ4 DLL which is available at `dll\msys-lz4-1.dll`. #### The example of usage of static and dynamic LZ4 libraries with Visual C++ @@ -56,14 +56,14 @@ then the solution will upgraded to the current version. #### Using LZ4 DLL with Visual C++ -The header files from `include\` and the import library `dll\liblz4.lib` +The header files from `include\` and the import library `dll\liblz4.dll.a` are required to compile a project using Visual C++. 1. The header files should be added to `Additional Include Directories` that can be found in project properties `C/C++` then `General`. 2. The import library has to be added to `Additional Dependencies` that can be found in project properties `Linker` then `Input`. - If one will provide only the name `liblz4.lib` without a full path to the library + If one will provide only the name `liblz4.dll.a` without a full path to the library the directory has to be added to `Linker\General\Additional Library Directories`. -The compiled executable will require LZ4 DLL which is available at `dll\liblz4.dll`. +The compiled executable will require LZ4 DLL which is available at `dll\msys-lz4-1.dll`. -- cgit v0.12 From f745a01cfd6363093de97e6969661cbe99e2393d Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 4 Jul 2022 16:36:19 -0700 Subject: clarify yet another time what dual-license means --- ossfuzz/fuzz_helpers.h | 3 ++- tests/roundTripTest.c | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/ossfuzz/fuzz_helpers.h b/ossfuzz/fuzz_helpers.h index aae359e..efd9acf 100644 --- a/ossfuzz/fuzz_helpers.h +++ b/ossfuzz/fuzz_helpers.h @@ -4,7 +4,8 @@ * * 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). + * in the COPYING file in the root directory of this source tree), + * meaning you may select, at your option, one of the above-listed licenses. */ /** diff --git a/tests/roundTripTest.c b/tests/roundTripTest.c index 233a745..3e9d6ed 100644 --- a/tests/roundTripTest.c +++ b/tests/roundTripTest.c @@ -4,8 +4,8 @@ * * 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). - * You may select, at your option, one of the above-listed licenses. + * in the COPYING file in the root directory of this source tree), + * meaning you may select, at your option, one of the above-listed licenses. */ /* -- cgit v0.12 From 42eb47d42f041054140b8e08ffc6ba85e9f092f2 Mon Sep 17 00:00:00 2001 From: Alexander Mohr Date: Tue, 5 Jul 2022 11:35:58 +0200 Subject: uncompressed-api: allow uncompressed_update only for independent blocks Signed-off-by: Alexander Mohr --- doc/lz4_manual.html | 12 ---- lib/lz4.c | 24 ++------ lib/lz4.h | 11 ---- lib/lz4frame.c | 172 ++++++++++++++++++++++------------------------------ lib/lz4frame.h | 1 + lib/lz4hc.c | 27 ++------- 6 files changed, 84 insertions(+), 163 deletions(-) diff --git a/doc/lz4_manual.html b/doc/lz4_manual.html index 700cb84..037cfc0 100644 --- a/doc/lz4_manual.html +++ b/doc/lz4_manual.html @@ -391,18 +391,6 @@ int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream);
-LZ4LIB_STATIC_API int LZ4_getDictSize (const LZ4_stream_t* LZ4_dict, int dictSize); -Get the size of the dictionary. This can be used for adding data without - compression to the LZ4 archive. If linked blocked mode is used the memory - of the dictionary is kept free. - This way uncompressed data does not influence the effectiveness of the - dictionary. - @param LZ4_dict Pointer to the dictionary to get the size of. - @param dictSize The maximum dictionary size. (Normally 64 KB). - @return The size of the dictionary. - -
-It's possible to have input and output sharing the same buffer, for highly constrained memory environments. diff --git a/lib/lz4.c b/lib/lz4.c index 289162d..a2272cf 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1679,25 +1679,6 @@ int LZ4_compress_forceExtDict (LZ4_stream_t* LZ4_dict, const char* source, char* return result; } -/*! LZ4_getDictSize(): - * Get the size of the dictionary. This can be used for adding data without - * compression to the LZ4 archive. If linked blocked mode is used the memory - * of the dictionary is kept free. - * This way uncompressed data does not influence the effectiveness of the - * dictionary. - * @param LZ4_dict Pointer to the dictionary to get the size of. - * @param dictSize The maximum dictionary size. (Normally 64 KB). - * @return The size of the dictionary. - */ -int LZ4_getDictSize (const LZ4_stream_t* LZ4_dict, int dictSize) -{ - const LZ4_stream_t_internal* const dict = &LZ4_dict->internal_donotuse; - - if ((U32)dictSize > 64 KB) { dictSize = 64 KB; } /* useless to define a dictionary > 64 KB */ - if ((U32)dictSize > dict->dictSize) { dictSize = (int)dict->dictSize; } - - return dictSize; -} /*! LZ4_saveDict() : * If previously compressed data block is not guaranteed to remain available at its memory location, @@ -1709,9 +1690,12 @@ int LZ4_getDictSize (const LZ4_stream_t* LZ4_dict, int dictSize) int LZ4_saveDict (LZ4_stream_t* LZ4_dict, char* safeBuffer, int dictSize) { LZ4_stream_t_internal* const dict = &LZ4_dict->internal_donotuse; - dictSize = LZ4_getDictSize(LZ4_dict, dictSize); + DEBUGLOG(5, "LZ4_saveDict : dictSize=%i, safeBuffer=%p", dictSize, safeBuffer); + 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 (safeBuffer == NULL) assert(dictSize == 0); if (dictSize > 0) { const BYTE* const previousDictEnd = dict->dictionary + dict->dictSize; diff --git a/lib/lz4.h b/lib/lz4.h index ce2288e..6c068c6 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -509,17 +509,6 @@ 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); -/*! LZ4_getDictSize(): - * Get the size of the dictionary. This can be used for adding data without - * compression to the LZ4 archive. If linked blocked mode is used the memory - * of the dictionary is kept free. - * This way uncompressed data does not influence the effectiveness of the - * dictionary. - * @param LZ4_dict Pointer to the dictionary to get the size of. - * @param dictSize The maximum dictionary size. (Normally 64 KB). - * @return The size of the dictionary. - */ -LZ4LIB_STATIC_API int LZ4_getDictSize (const LZ4_stream_t* LZ4_dict, int dictSize); /*! In-place compression and decompression * diff --git a/lib/lz4frame.c b/lib/lz4frame.c index f32ed1d..3042d6f 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -841,23 +841,12 @@ static compressFunc_t LZ4F_selectCompression(LZ4F_blockMode_t blockMode, int lev return LZ4F_compressBlockHC_continue; } -static int LZ4F_maxDictSize(void) { - return 64 KB; -} - /* Save history (up to 64KB) into @tmpBuff */ static int LZ4F_localSaveDict(LZ4F_cctx_t* cctxPtr) { if (cctxPtr->prefs.compressionLevel < LZ4HC_CLEVEL_MIN) - return LZ4_saveDict ((LZ4_stream_t*)(cctxPtr->lz4CtxPtr), (char*)(cctxPtr->tmpBuff), LZ4F_maxDictSize()); - return LZ4_saveDictHC ((LZ4_streamHC_t*)(cctxPtr->lz4CtxPtr), (char*)(cctxPtr->tmpBuff), LZ4F_maxDictSize()); -} - -static int LZ4F_localDictSize(LZ4F_cctx_t* cctxPtr) -{ - if (cctxPtr->prefs.compressionLevel < LZ4HC_CLEVEL_MIN) - return LZ4_getDictSize ((LZ4_stream_t*)(cctxPtr->lz4CtxPtr), LZ4F_maxDictSize()); - return LZ4_getDictHCSize ((LZ4_streamHC_t*)(cctxPtr->lz4CtxPtr), LZ4F_maxDictSize()); + return LZ4_saveDict ((LZ4_stream_t*)(cctxPtr->lz4CtxPtr), (char*)(cctxPtr->tmpBuff), 64 KB); + return LZ4_saveDictHC ((LZ4_streamHC_t*)(cctxPtr->lz4CtxPtr), (char*)(cctxPtr->tmpBuff), 64 KB); } typedef enum { notDone, fromTmpBuffer, fromSrcBuffer } LZ4F_lastBlockStatus; @@ -886,69 +875,64 @@ static size_t LZ4F_compressUpdateImpl(LZ4F_cctx* cctxPtr, size_t const blockSize = cctxPtr->maxBlockSize; const BYTE* srcPtr = (const BYTE*)srcBuffer; const BYTE* const srcEnd = srcPtr + srcSize; - BYTE* dstStart = (BYTE*)dstBuffer; + BYTE* const dstStart = (BYTE*)dstBuffer; BYTE* dstPtr = dstStart; LZ4F_lastBlockStatus lastBlockCompressed = notDone; compressFunc_t const compress = LZ4F_selectCompression(cctxPtr->prefs.frameInfo.blockMode, cctxPtr->prefs.compressionLevel); - size_t bytesWritten = 0; + size_t bytesWritten; DEBUGLOG(4, "LZ4F_compressUpdate (srcSize=%zu)", srcSize); - /* flush currently written block, to continue with new block compression */ - if (cctxPtr->blockCompression != blockCompression) { - bytesWritten = LZ4F_flush(cctxPtr, dstBuffer, dstCapacity, compressOptionsPtr); - dstStart = (BYTE*)dstBuffer + bytesWritten; - dstPtr = dstStart; - dstCapacity -= bytesWritten; - cctxPtr->blockCompression = blockCompression; - } - RETURN_ERROR_IF(cctxPtr->cStage != 1, compressionState_uninitialized); /* state must be initialized and waiting for next block */ - - if (blockCompression == LZ4B_COMPRESSED && - dstCapacity < LZ4F_compressBound_internal(srcSize, &(cctxPtr->prefs), cctxPtr->tmpInSize)) - RETURN_ERROR(dstMaxSize_tooSmall); + if (dstCapacity < LZ4F_compressBound_internal(srcSize, &(cctxPtr->prefs), cctxPtr->tmpInSize)) + RETURN_ERROR(dstMaxSize_tooSmall); if (blockCompression == LZ4B_UNCOMPRESSED && dstCapacity < srcSize) - RETURN_ERROR(dstMaxSize_tooSmall); + RETURN_ERROR(dstMaxSize_tooSmall); + + /* flush currently written block, to continue with new block compression */ + if (cctxPtr->blockCompression != blockCompression) { + bytesWritten = LZ4F_flush(cctxPtr, dstBuffer, dstCapacity, compressOptionsPtr); + dstPtr += bytesWritten; + cctxPtr->blockCompression = blockCompression; + } if (compressOptionsPtr == NULL) compressOptionsPtr = &k_cOptionsNull; /* complete tmp buffer */ if (cctxPtr->tmpInSize > 0) { /* some data already within tmp buffer */ - size_t const sizeToCopy = blockSize - cctxPtr->tmpInSize; - assert(blockSize > cctxPtr->tmpInSize); - if (sizeToCopy > srcSize) { - /* add src to tmpIn buffer */ - memcpy(cctxPtr->tmpIn + cctxPtr->tmpInSize, srcBuffer, srcSize); - srcPtr = srcEnd; - cctxPtr->tmpInSize += srcSize; - /* still needs some CRC */ - } else { - /* complete tmpIn block and then compress it */ - lastBlockCompressed = fromTmpBuffer; - memcpy(cctxPtr->tmpIn + cctxPtr->tmpInSize, srcBuffer, sizeToCopy); - srcPtr += sizeToCopy; + size_t const sizeToCopy = blockSize - cctxPtr->tmpInSize; + assert(blockSize > cctxPtr->tmpInSize); + if (sizeToCopy > srcSize) { + /* add src to tmpIn buffer */ + memcpy(cctxPtr->tmpIn + cctxPtr->tmpInSize, srcBuffer, srcSize); + srcPtr = srcEnd; + cctxPtr->tmpInSize += srcSize; + /* still needs some CRC */ + } else { + /* complete tmpIn block and then compress it */ + lastBlockCompressed = fromTmpBuffer; + memcpy(cctxPtr->tmpIn + cctxPtr->tmpInSize, srcBuffer, sizeToCopy); + srcPtr += sizeToCopy; + + dstPtr += LZ4F_makeBlock(dstPtr, + cctxPtr->tmpIn, blockSize, + compress, cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel, + cctxPtr->cdict, + cctxPtr->prefs.frameInfo.blockChecksumFlag, blockCompression); + if (cctxPtr->prefs.frameInfo.blockMode==LZ4F_blockLinked) cctxPtr->tmpIn += blockSize; + cctxPtr->tmpInSize = 0; + } } + while ((size_t)(srcEnd - srcPtr) >= blockSize) { + /* compress full blocks */ + lastBlockCompressed = fromSrcBuffer; dstPtr += LZ4F_makeBlock(dstPtr, - cctxPtr->tmpIn, blockSize, + srcPtr, blockSize, compress, cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel, cctxPtr->cdict, - cctxPtr->prefs.frameInfo.blockChecksumFlag, blockCompression); - - if (cctxPtr->prefs.frameInfo.blockMode==LZ4F_blockLinked) cctxPtr->tmpIn += blockSize; - cctxPtr->tmpInSize = 0; - } - } - - while ((size_t)(srcEnd - srcPtr) >= blockSize) { - /* compress full blocks */ - lastBlockCompressed = fromSrcBuffer; - dstPtr += LZ4F_makeBlock(dstPtr, - srcPtr, blockSize, - compress, cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel, - cctxPtr->cdict, - cctxPtr->prefs.frameInfo.blockChecksumFlag, blockCompression); - srcPtr += blockSize; + cctxPtr->prefs.frameInfo.blockChecksumFlag, + blockCompression); + srcPtr += blockSize; } if ((cctxPtr->prefs.autoFlush) && (srcPtr < srcEnd)) { @@ -960,27 +944,20 @@ static size_t LZ4F_compressUpdateImpl(LZ4F_cctx* cctxPtr, cctxPtr->cdict, cctxPtr->prefs.frameInfo.blockChecksumFlag, blockCompression); - srcPtr = srcEnd; + srcPtr = srcEnd; } /* preserve dictionary within @tmpBuff whenever necessary */ if ((cctxPtr->prefs.frameInfo.blockMode==LZ4F_blockLinked) && (lastBlockCompressed==fromSrcBuffer)) { - if (compressOptionsPtr->stableSrc) { - cctxPtr->tmpIn = cctxPtr->tmpBuff; /* src is stable : dictionary remains in src across invocations */ - } else { - int realDictSize; - if (blockCompression == LZ4B_COMPRESSED) { - realDictSize = LZ4F_localSaveDict(cctxPtr); + /* linked blocks are only supported in compressed mode, see LZ4F_uncompressedUpdate */ + assert(blockCompression == LZ4B_COMPRESSED); + if (compressOptionsPtr->stableSrc) { + cctxPtr->tmpIn = cctxPtr->tmpBuff; /* src is stable : dictionary remains in src across invocations */ } else { - /* only keep the space of the dictionary, so dict data is kept for the next compressedUpdate - * this is only relevant if linked block mode - * */ - realDictSize = LZ4F_localDictSize(cctxPtr); + int const realDictSize = LZ4F_localSaveDict(cctxPtr); + assert(0 <= realDictSize && realDictSize <= 64 KB); + cctxPtr->tmpIn = cctxPtr->tmpBuff + realDictSize; } - - assert(0 <= realDictSize && realDictSize <= 64 KB); - cctxPtr->tmpIn = cctxPtr->tmpBuff + realDictSize; - } } /* keep tmpIn within limits */ @@ -989,30 +966,24 @@ static size_t LZ4F_compressUpdateImpl(LZ4F_cctx* cctxPtr, { /* only preserve 64KB within internal buffer. Ensures there is enough room for next block. * note: this situation necessarily implies lastBlockCompressed==fromTmpBuffer */ - int realDictSize; - if (blockCompression == LZ4B_COMPRESSED) { - realDictSize = LZ4F_localSaveDict(cctxPtr); - } else { - /* only keep the space of the dictionary, so dict data is kept for the next compressedUpdate*/ - realDictSize = LZ4F_maxDictSize(); - } + int const realDictSize = LZ4F_localSaveDict(cctxPtr); cctxPtr->tmpIn = cctxPtr->tmpBuff + realDictSize; assert((cctxPtr->tmpIn + blockSize) <= (cctxPtr->tmpBuff + cctxPtr->maxBufferSize)); } /* some input data left, necessarily < blockSize */ if (srcPtr < srcEnd) { - /* fill tmp buffer */ - size_t const sizeToCopy = (size_t)(srcEnd - srcPtr); - memcpy(cctxPtr->tmpIn, srcPtr, sizeToCopy); - cctxPtr->tmpInSize = sizeToCopy; + /* fill tmp buffer */ + size_t const sizeToCopy = (size_t)(srcEnd - srcPtr); + memcpy(cctxPtr->tmpIn, srcPtr, sizeToCopy); + cctxPtr->tmpInSize = sizeToCopy; } if (cctxPtr->prefs.frameInfo.contentChecksumFlag == LZ4F_contentChecksumEnabled) - (void)XXH32_update(&(cctxPtr->xxh), srcBuffer, srcSize); + (void)XXH32_update(&(cctxPtr->xxh), srcBuffer, srcSize); cctxPtr->totalInSize += srcSize; - return bytesWritten + (size_t)(dstPtr - dstStart); + return (size_t)(dstPtr - dstStart); } /*! LZ4F_compressUpdate() : @@ -1032,10 +1003,10 @@ size_t LZ4F_compressUpdate(LZ4F_cctx* cctxPtr, const void* srcBuffer, size_t srcSize, const LZ4F_compressOptions_t* compressOptionsPtr) { - return LZ4F_compressUpdateImpl(cctxPtr, - dstBuffer, dstCapacity, - srcBuffer, srcSize, - compressOptionsPtr, LZ4B_COMPRESSED); + return LZ4F_compressUpdateImpl(cctxPtr, + dstBuffer, dstCapacity, + srcBuffer, srcSize, + compressOptionsPtr, LZ4B_COMPRESSED); } /*! LZ4F_compressUpdate() : @@ -1044,6 +1015,7 @@ size_t LZ4F_compressUpdate(LZ4F_cctx* cctxPtr, * src data is either buffered or compressed into @dstBuffer. * If previously an uncompressed block was written, buffered data is flushed * before appending compressed data is continued. + * This is only supported when LZ4F_blockIndependent is used * @dstCapacity MUST be >= LZ4F_compressBound(srcSize, preferencesPtr). * @compressOptionsPtr is optional : provide NULL to mean "default". * @return : the number of bytes written into dstBuffer. It can be zero, meaning input data was just buffered. @@ -1051,13 +1023,14 @@ size_t LZ4F_compressUpdate(LZ4F_cctx* cctxPtr, * After an error, the state is left in a UB state, and must be re-initialized. */ size_t LZ4F_uncompressedUpdate(LZ4F_cctx* cctxPtr, - void* dstBuffer, size_t dstCapacity, - const void* srcBuffer, size_t srcSize, - const LZ4F_compressOptions_t* compressOptionsPtr) { - return LZ4F_compressUpdateImpl(cctxPtr, - dstBuffer, dstCapacity, - srcBuffer, srcSize, - compressOptionsPtr, LZ4B_UNCOMPRESSED); + void* dstBuffer, size_t dstCapacity, + const void* srcBuffer, size_t srcSize, + const LZ4F_compressOptions_t* compressOptionsPtr) { + assert(cctxPtr->prefs.frameInfo.blockMode == LZ4F_blockIndependent); + return LZ4F_compressUpdateImpl(cctxPtr, + dstBuffer, dstCapacity, + srcBuffer, srcSize, + compressOptionsPtr, LZ4B_UNCOMPRESSED); } @@ -1090,7 +1063,8 @@ size_t LZ4F_flush(LZ4F_cctx* cctxPtr, cctxPtr->tmpIn, cctxPtr->tmpInSize, compress, cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel, cctxPtr->cdict, - cctxPtr->prefs.frameInfo.blockChecksumFlag, cctxPtr->blockCompression); + cctxPtr->prefs.frameInfo.blockChecksumFlag, + cctxPtr->blockCompression); assert(((void)"flush overflows dstBuffer!", (size_t)(dstPtr - dstStart) <= dstCapacity)); if (cctxPtr->prefs.frameInfo.blockMode == LZ4F_blockLinked) diff --git a/lib/lz4frame.h b/lib/lz4frame.h index 7d81fa0..691ad50 100644 --- a/lib/lz4frame.h +++ b/lib/lz4frame.h @@ -552,6 +552,7 @@ LZ4FLIB_STATIC_API size_t LZ4F_getBlockSize(unsigned); * After an error, the state is left in a UB state, and must be re-initialized or freed. * If previously a compressed block was written, buffered data is flushed * before appending uncompressed data is continued. + * This is only supported when LZ4F_blockIndependent is used * `cOptPtr` is optional : NULL can be provided, in which case all options are set to default. * @return : number of bytes written into `dstBuffer` (it can be zero, meaning input data was just buffered). * or an error code if it fails (which can be tested using LZ4F_isError()) diff --git a/lib/lz4hc.c b/lib/lz4hc.c index b5bc880..99650a6 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -1154,26 +1154,6 @@ int LZ4_compress_HC_continue_destSize (LZ4_streamHC_t* LZ4_streamHCPtr, const ch return LZ4_compressHC_continue_generic(LZ4_streamHCPtr, src, dst, srcSizePtr, targetDestSize, fillOutput); } -/*! LZ4_getDictHCSize(): - * Get the size of the dictionary. This can be used for adding data without - * compression to the LZ4 archive. If linked blocked mode is used the memory - * of the dictionary is kept free. - * This way uncompressed data does not influence the effectiveness of the - * dictionary. - * @param LZ4_dict Pointer to the dictionary to get the size of. - * @param dictSize The maximum dictionary size. (Normally 64 KB). - * @return The size of the dictionary. - */ -int LZ4_getDictHCSize(const LZ4_streamHC_t* LZ4_streamHCPtr, int dictSize) { - const LZ4HC_CCtx_internal* const streamPtr = &LZ4_streamHCPtr->internal_donotuse; - int const prefixSize = (int)(streamPtr->end - (streamPtr->base + streamPtr->dictLimit)); - 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; - return dictSize; -} /* LZ4_saveDictHC : @@ -1184,7 +1164,12 @@ int LZ4_getDictHCSize(const LZ4_streamHC_t* LZ4_streamHCPtr, int dictSize) { int LZ4_saveDictHC (LZ4_streamHC_t* LZ4_streamHCPtr, char* safeBuffer, int dictSize) { LZ4HC_CCtx_internal* const streamPtr = &LZ4_streamHCPtr->internal_donotuse; - dictSize = LZ4_getDictHCSize(LZ4_streamHCPtr, dictSize); + int const prefixSize = (int)(streamPtr->end - (streamPtr->base + streamPtr->dictLimit)); + 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; if (safeBuffer == NULL) assert(dictSize == 0); if (dictSize > 0) memmove(safeBuffer, streamPtr->end - dictSize, dictSize); -- cgit v0.12 From 0ac3c74de1b6de584c361f3e9485dde35f10c756 Mon Sep 17 00:00:00 2001 From: Alexander Mohr
Date: Tue, 5 Jul 2022 20:52:50 +0200 Subject: review: fix findings * replace assert with test for LZ4F_uncompressedUpdate * update documentation to incldue correct docstring * remove unecessary entry point * remove compress_linked_block_mode from fuzzing test Signed-off-by: Alexander Mohr --- contrib/meson/meson/examples/meson.build | 2 +- contrib/meson/meson/lib/meson.build | 2 +- doc/lz4frame_manual.html | 1 + lib/lz4frame.c | 2 +- lib/lz4hc.h | 12 ------------ ossfuzz/round_trip_frame_uncompressed_fuzzer.c | 8 -------- 6 files changed, 4 insertions(+), 23 deletions(-) diff --git a/contrib/meson/meson/examples/meson.build b/contrib/meson/meson/examples/meson.build index f6b6c41..65f54ca 100644 --- a/contrib/meson/meson/examples/meson.build +++ b/contrib/meson/meson/examples/meson.build @@ -26,7 +26,7 @@ foreach e, src : examples executable( e, lz4_source_root / 'examples' / src, - dependencies: [liblz4_dep, liblz4_internal_dep], + dependencies: [liblz4_internal_dep], install: false ) endforeach diff --git a/contrib/meson/meson/lib/meson.build b/contrib/meson/meson/lib/meson.build index 2ff294d..469cd09 100644 --- a/contrib/meson/meson/lib/meson.build +++ b/contrib/meson/meson/lib/meson.build @@ -43,7 +43,7 @@ liblz4_dep = declare_dependency( include_directories: include_directories(lz4_source_root / 'lib') ) -if get_option('tests') or get_option('programs') +if get_option('tests') or get_option('programs') or get_option('examples') liblz4_internal = static_library( 'lz4-internal', objects: liblz4.extract_all_objects(recursive: true), diff --git a/doc/lz4frame_manual.html b/doc/lz4frame_manual.html index c55c6e9..0ae5150 100644 --- a/doc/lz4frame_manual.html +++ b/doc/lz4frame_manual.html @@ -355,6 +355,7 @@ LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_dctx* dctx); After an error, the state is left in a UB state, and must be re-initialized or freed. If previously a compressed block was written, buffered data is flushed before appending uncompressed data is continued. + This is only supported when LZ4F_blockIndependent is used `cOptPtr` is optional : NULL can be provided, in which case all options are set to default. @return : number of bytes written into `dstBuffer` (it can be zero, meaning input data was just buffered). or an error code if it fails (which can be tested using LZ4F_isError()) diff --git a/lib/lz4frame.c b/lib/lz4frame.c index 3042d6f..80e244b 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -1026,7 +1026,7 @@ size_t LZ4F_uncompressedUpdate(LZ4F_cctx* cctxPtr, void* dstBuffer, size_t dstCapacity, const void* srcBuffer, size_t srcSize, const LZ4F_compressOptions_t* compressOptionsPtr) { - assert(cctxPtr->prefs.frameInfo.blockMode == LZ4F_blockIndependent); + RETURN_ERROR_IF(cctxPtr->prefs.frameInfo.blockMode != LZ4F_blockIndependent, blockMode_invalid); return LZ4F_compressUpdateImpl(cctxPtr, dstBuffer, dstCapacity, srcBuffer, srcSize, diff --git a/lib/lz4hc.h b/lib/lz4hc.h index 3b31624..f4afc9b 100644 --- a/lib/lz4hc.h +++ b/lib/lz4hc.h @@ -405,18 +405,6 @@ LZ4LIB_STATIC_API void LZ4_attach_HC_dictionary( LZ4_streamHC_t *working_stream, const LZ4_streamHC_t *dictionary_stream); -/*! LZ4_getDictHCSize(): - * Get the size of the dictionary. This can be used for adding data without - * compression to the LZ4 archive. If linked blocked mode is used the memory - * of the dictionary is kept free. - * This way uncompressed data does not influence the effectiveness of the - * dictionary. - * @param LZ4_dict Pointer to the dictionary to get the size of. - * @param dictSize The maximum dictionary size. (Normally 64 KB). - * @return The size of the dictionary. - */ -LZ4LIB_STATIC_API int LZ4_getDictHCSize(const LZ4_streamHC_t* LZ4_streamHCPtr, int dictSize); - #if defined (__cplusplus) } #endif diff --git a/ossfuzz/round_trip_frame_uncompressed_fuzzer.c b/ossfuzz/round_trip_frame_uncompressed_fuzzer.c index cf9cbb9..e578052 100644 --- a/ossfuzz/round_trip_frame_uncompressed_fuzzer.c +++ b/ossfuzz/round_trip_frame_uncompressed_fuzzer.c @@ -124,13 +124,6 @@ static void compress_round_trip(const uint8_t* data, size_t size, LZ4F_freeCompressionContext(ctx); } -static void compress_linked_block_mode(const uint8_t* data, size_t size) { - FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size); - LZ4F_preferences_t prefs = FUZZ_dataProducer_preferences(producer); - prefs.frameInfo.blockMode = LZ4F_blockLinked; - compress_round_trip(data, size, producer, prefs); -} - static void compress_independent_block_mode(const uint8_t* data, size_t size) { FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size); LZ4F_preferences_t prefs = FUZZ_dataProducer_preferences(producer); @@ -140,7 +133,6 @@ static void compress_independent_block_mode(const uint8_t* data, size_t size) { int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { - compress_linked_block_mode(data, size); compress_independent_block_mode(data, size); return 0; } -- cgit v0.12 From 6e242d1915df794d2f5c337f3bc29146efa98588 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 5 Jul 2022 14:08:36 -0700 Subject: update frametest for new condition for uncompressedUpdate --- tests/frametest.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/tests/frametest.c b/tests/frametest.c index 939d4c2..a496c3c 100644 --- a/tests/frametest.c +++ b/tests/frametest.c @@ -1023,21 +1023,18 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi #if 1 /* insert uncompressed segment */ - if ((iSize>0) && !neverFlush && ((FUZ_rand(&randState) & 15) == 1)) { + if ( (iSize>0) + && !neverFlush /* do not mess with compressBound when neverFlush is set */ + && prefsPtr != NULL /* prefs are set */ + && prefs.frameInfo.blockMode == LZ4F_blockIndependent /* uncompressedUpdate is only valid with blockMode==independent */ + && (FUZ_rand(&randState) & 15) == 1 ) { size_t const uSize = FUZ_rand(&randState) % iSize; - DISPLAYLEVEL(2, "insert %zu / %zu (blockSize=%uKB) \n", uSize, iSize, 1 << (2*prefs.frameInfo.blockSizeID - 2)); - { size_t const flushedSize = LZ4F_uncompressedUpdate(cCtx, op, (size_t)(oend-op), ip, uSize, &cOptions); - CHECK(LZ4F_isError(flushedSize), "Insert uncompressed data failed (error %i : %s)", - (int)flushedSize, LZ4F_getErrorName(flushedSize)); - op += flushedSize; - ip += uSize; - } + size_t const flushedSize = LZ4F_uncompressedUpdate(cCtx, op, (size_t)(oend-op), ip, uSize, &cOptions); + CHECK(LZ4F_isError(flushedSize), "Insert uncompressed data failed (error %i : %s)", + (int)flushedSize, LZ4F_getErrorName(flushedSize)); + op += flushedSize; + ip += uSize; iSize -= uSize; - { size_t const flushedSize = LZ4F_flush(cCtx, op, (size_t)(oend-op), &cOptions); - CHECK(LZ4F_isError(flushedSize), "Flush after LZ4F_uncompressedUpdate failed (error %i : %s)", - (int)flushedSize, LZ4F_getErrorName(flushedSize)); - op += flushedSize; - } } #endif -- cgit v0.12 From d3d3fad70c1409bbfafb663dcb5da53de8b757e2 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 5 Jul 2022 15:39:15 -0700 Subject: ignore ossfuzz artifact --- ossfuzz/.gitignore | 27 ++++----------------------- 1 file changed, 4 insertions(+), 23 deletions(-) diff --git a/ossfuzz/.gitignore b/ossfuzz/.gitignore index 2dc764a..0ef0d2b 100644 --- a/ossfuzz/.gitignore +++ b/ossfuzz/.gitignore @@ -1,27 +1,8 @@ -# Object files -*.o -*.ko -# Libraries -*.lib -*.a +# build artefacts +round_trip_frame_uncompressed_fuzzer -# Shared objects (inc. Windows DLLs) -*.dll -*.so -*.so.* -*.dylib -*.dSYM # apple - -# Executables -compress_frame_fuzzer -compress_fuzzer -compress_hc_fuzzer -decompress_frame_fuzzer -decompress_fuzzer -round_trip_frame_fuzzer -round_trip_fuzzer -round_trip_hc_fuzzer -round_trip_stream_fuzzer +# test artefacts +# local tests -- cgit v0.12 From 910ec80d2856cfa825e2230ff2de8347a4fa4522 Mon Sep 17 00:00:00 2001 From: Yonatan Komornik Date: Fri, 8 Jul 2022 14:03:14 -0700 Subject: - Fixed incorrect free in `round_trip_fuzzer.c` (https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=48884) - Fixed `round_trip_frame_uncompressed_fuzzer.c` to not use uninitialized memory (https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=48910) --- ossfuzz/round_trip_frame_uncompressed_fuzzer.c | 214 ++++++++++++------------- ossfuzz/round_trip_fuzzer.c | 2 +- 2 files changed, 106 insertions(+), 110 deletions(-) diff --git a/ossfuzz/round_trip_frame_uncompressed_fuzzer.c b/ossfuzz/round_trip_frame_uncompressed_fuzzer.c index e578052..76a99d2 100644 --- a/ossfuzz/round_trip_frame_uncompressed_fuzzer.c +++ b/ossfuzz/round_trip_frame_uncompressed_fuzzer.c @@ -16,123 +16,119 @@ #include "lz4frame_static.h" static void decompress(LZ4F_dctx *dctx, void *src, void *dst, - size_t dstCapacity, size_t readSize) { - size_t ret = 1; - const void *srcPtr = (const char *)src; - void *dstPtr = (char *)dst; - const void *const srcEnd = (const char *)srcPtr + readSize; - - while (ret != 0) { - while (srcPtr < srcEnd && ret != 0) { - /* Any data within dst has been flushed at this stage */ - size_t dstSize = dstCapacity; - size_t srcSize = (const char *)srcEnd - (const char *)srcPtr; - ret = LZ4F_decompress(dctx, dstPtr, &dstSize, srcPtr, &srcSize, - /* LZ4F_decompressOptions_t */ NULL); - FUZZ_ASSERT(!LZ4F_isError(ret)); - - /* Update input */ - srcPtr = (const char *)srcPtr + srcSize; - dstPtr = (char *)dstPtr + dstSize; + size_t dstCapacity, size_t readSize) { + size_t ret = 1; + const void *srcPtr = (const char *) src; + void *dstPtr = (char *) dst; + const void *const srcEnd = (const char *) srcPtr + readSize; + + while (ret != 0) { + while (srcPtr < srcEnd && ret != 0) { + /* Any data within dst has been flushed at this stage */ + size_t dstSize = dstCapacity; + size_t srcSize = (const char *) srcEnd - (const char *) srcPtr; + ret = LZ4F_decompress(dctx, dstPtr, &dstSize, srcPtr, &srcSize, + /* LZ4F_decompressOptions_t */ NULL); + FUZZ_ASSERT(!LZ4F_isError(ret)); + + /* Update input */ + srcPtr = (const char *) srcPtr + srcSize; + dstPtr = (char *) dstPtr + dstSize; + } + + FUZZ_ASSERT(srcPtr <= srcEnd); } - - FUZZ_ASSERT(srcPtr <= srcEnd); - } } -static void compress_round_trip(const uint8_t* data, size_t size, +static void compress_round_trip(const uint8_t *data, size_t size, FUZZ_dataProducer_t *producer, LZ4F_preferences_t const prefs) { - size = FUZZ_dataProducer_remainingBytes(producer); - - uint8_t *uncompressedData = malloc(size); - size_t uncompressedOffset = rand() % (size + 1); - - FUZZ_dataProducer_t *uncompressedProducer = - FUZZ_dataProducer_create(uncompressedData, size); - size_t uncompressedSize = - FUZZ_dataProducer_remainingBytes(uncompressedProducer); - - size_t const dstCapacity = - LZ4F_compressFrameBound(LZ4_compressBound(size), &prefs) + - uncompressedSize; - char *const dst = (char *)malloc(dstCapacity); - size_t rtCapacity = dstCapacity; - char *const rt = (char *)malloc(rtCapacity); - - FUZZ_ASSERT(dst); - FUZZ_ASSERT(rt); - - /* Compression must succeed and round trip correctly. */ - LZ4F_compressionContext_t ctx; - size_t const ctxCreation = LZ4F_createCompressionContext(&ctx, LZ4F_VERSION); - FUZZ_ASSERT(!LZ4F_isError(ctxCreation)); - - size_t const headerSize = LZ4F_compressBegin(ctx, dst, dstCapacity, &prefs); - FUZZ_ASSERT(!LZ4F_isError(headerSize)); - size_t compressedSize = headerSize; - - /* Compress data before uncompressed offset */ - size_t lz4Return = LZ4F_compressUpdate(ctx, dst + compressedSize, dstCapacity, - data, uncompressedOffset, NULL); - FUZZ_ASSERT(!LZ4F_isError(lz4Return)); - compressedSize += lz4Return; - - /* Add uncompressed data */ - lz4Return = LZ4F_uncompressedUpdate(ctx, dst + compressedSize, dstCapacity, - uncompressedData, uncompressedSize, NULL); - FUZZ_ASSERT(!LZ4F_isError(lz4Return)); - compressedSize += lz4Return; - - /* Compress data after uncompressed offset */ - lz4Return = LZ4F_compressUpdate(ctx, dst + compressedSize, dstCapacity, - data + uncompressedOffset, - size - uncompressedOffset, NULL); - FUZZ_ASSERT(!LZ4F_isError(lz4Return)); - compressedSize += lz4Return; - - /* Finish compression */ - lz4Return = LZ4F_compressEnd(ctx, dst + compressedSize, dstCapacity, NULL); - FUZZ_ASSERT(!LZ4F_isError(lz4Return)); - compressedSize += lz4Return; - - LZ4F_decompressOptions_t opts; - memset(&opts, 0, sizeof(opts)); - opts.stableDst = 1; - LZ4F_dctx *dctx; - LZ4F_createDecompressionContext(&dctx, LZ4F_VERSION); - FUZZ_ASSERT(dctx); - - decompress(dctx, dst, rt, rtCapacity, compressedSize); - - LZ4F_freeDecompressionContext(dctx); - - char *const expectedData = (char *)malloc(size + uncompressedSize); - memcpy(expectedData, data, uncompressedOffset); - memcpy(expectedData + uncompressedOffset, uncompressedData, uncompressedSize); - memcpy(expectedData + uncompressedOffset + uncompressedSize, - data + uncompressedOffset, size - uncompressedOffset); - - FUZZ_ASSERT_MSG(!memcmp(expectedData, rt, size), "Corruption!"); - free(expectedData); - - free(dst); - free(rt); - free(uncompressedData); - - FUZZ_dataProducer_free(producer); - FUZZ_dataProducer_free(uncompressedProducer); - LZ4F_freeCompressionContext(ctx); + + // Choose random uncompressed offset start and end by producing seeds from random data, calculate the remaining + // data size that will be used for compression later and use the seeds to actually calculate the offsets + size_t const uncompressedOffsetSeed = FUZZ_dataProducer_retrieve32(producer); + size_t const uncompressedEndOffsetSeed = FUZZ_dataProducer_retrieve32(producer); + size = FUZZ_dataProducer_remainingBytes(producer); + + size_t const uncompressedOffset = FUZZ_getRange_from_uint32(uncompressedOffsetSeed, 0, size); + size_t const uncompressedEndOffset = FUZZ_getRange_from_uint32(uncompressedEndOffsetSeed, uncompressedOffset, size); + size_t const uncompressedSize = uncompressedEndOffset - uncompressedOffset; + FUZZ_ASSERT(uncompressedOffset <= uncompressedEndOffset); + FUZZ_ASSERT(uncompressedEndOffset <= size); + + const uint8_t *const uncompressedData = data + uncompressedOffset; + + size_t const dstCapacity = + LZ4F_compressFrameBound(LZ4_compressBound(size), &prefs) + + uncompressedSize; + char *const dst = (char *) malloc(dstCapacity); + size_t rtCapacity = dstCapacity; + char *const rt = (char *) malloc(rtCapacity); + + FUZZ_ASSERT(dst); + FUZZ_ASSERT(rt); + + /* Compression must succeed and round trip correctly. */ + LZ4F_compressionContext_t ctx; + size_t const ctxCreation = LZ4F_createCompressionContext(&ctx, LZ4F_VERSION); + FUZZ_ASSERT(!LZ4F_isError(ctxCreation)); + + size_t const headerSize = LZ4F_compressBegin(ctx, dst, dstCapacity, &prefs); + FUZZ_ASSERT(!LZ4F_isError(headerSize)); + size_t compressedSize = headerSize; + + /* Compress data before uncompressed offset */ + size_t lz4Return = LZ4F_compressUpdate(ctx, dst + compressedSize, dstCapacity, + data, uncompressedOffset, NULL); + FUZZ_ASSERT(!LZ4F_isError(lz4Return)); + compressedSize += lz4Return; + + /* Add uncompressed data */ + lz4Return = LZ4F_uncompressedUpdate(ctx, dst + compressedSize, dstCapacity, + uncompressedData, uncompressedSize, NULL); + FUZZ_ASSERT(!LZ4F_isError(lz4Return)); + compressedSize += lz4Return; + + /* Compress data after uncompressed offset */ + lz4Return = LZ4F_compressUpdate(ctx, dst + compressedSize, dstCapacity, + data + uncompressedEndOffset, + size - uncompressedEndOffset, NULL); + FUZZ_ASSERT(!LZ4F_isError(lz4Return)); + compressedSize += lz4Return; + + /* Finish compression */ + lz4Return = LZ4F_compressEnd(ctx, dst + compressedSize, dstCapacity, NULL); + FUZZ_ASSERT(!LZ4F_isError(lz4Return)); + compressedSize += lz4Return; + + LZ4F_decompressOptions_t opts; + memset(&opts, 0, sizeof(opts)); + opts.stableDst = 1; + LZ4F_dctx *dctx; + LZ4F_createDecompressionContext(&dctx, LZ4F_VERSION); + FUZZ_ASSERT(dctx); + + decompress(dctx, dst, rt, rtCapacity, compressedSize); + + LZ4F_freeDecompressionContext(dctx); + + FUZZ_ASSERT_MSG(!memcmp(data, rt, size), "Corruption!"); + + free(dst); + free(rt); + + FUZZ_dataProducer_free(producer); + LZ4F_freeCompressionContext(ctx); } -static void compress_independent_block_mode(const uint8_t* data, size_t size) { - FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size); - LZ4F_preferences_t prefs = FUZZ_dataProducer_preferences(producer); - prefs.frameInfo.blockMode = LZ4F_blockIndependent; - compress_round_trip(data, size, producer, prefs); +static void compress_independent_block_mode(const uint8_t *data, size_t size) { + FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size); + LZ4F_preferences_t prefs = FUZZ_dataProducer_preferences(producer); + prefs.frameInfo.blockMode = LZ4F_blockIndependent; + compress_round_trip(data, size, producer, prefs); } int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { - compress_independent_block_mode(data, size); - return 0; + compress_independent_block_mode(data, size); + return 0; } diff --git a/ossfuzz/round_trip_fuzzer.c b/ossfuzz/round_trip_fuzzer.c index 7a2f768..2c35d9a 100644 --- a/ossfuzz/round_trip_fuzzer.c +++ b/ossfuzz/round_trip_fuzzer.c @@ -108,7 +108,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) free(partial); } - free(dst); + free(dstPlusLargePrefix); free(rt); FUZZ_dataProducer_free(producer); -- cgit v0.12 From 58fd9d08c8e6e0a31b4ad2e6cbc3bf0253d1e562 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sun, 10 Jul 2022 02:02:12 -0700 Subject: add ossfuzz tests to Github Actions --- .github/workflows/ci.yml | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fa7e7e9..82ac2d1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -436,6 +436,36 @@ jobs: - name: examples (compile as C++ code) run: make V=1 -C examples clean cxxtest + # lasts ~20mn + oss-fuzz: + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + sanitizer: [address, undefined, memory] + steps: + - name: Build Fuzzers (${{ matrix.sanitizer }}) + id: build + uses: google/oss-fuzz/infra/cifuzz/actions/build_fuzzers@master + with: + oss-fuzz-project-name: 'lz4' + dry-run: false + sanitizer: ${{ matrix.sanitizer }} + - name: Run Fuzzers (${{ matrix.sanitizer }}) + uses: google/oss-fuzz/infra/cifuzz/actions/run_fuzzers@master + with: + oss-fuzz-project-name: 'lz4' + fuzz-seconds: 600 + dry-run: false + sanitizer: ${{ matrix.sanitizer }} + - name: Upload Crash + uses: actions/upload-artifact@v1 + if: failure() && steps.build.outcome == 'success' + with: + name: ${{ matrix.sanitizer }}-artifacts + path: ./out/artifacts + + ############################################################### # Platforms -- cgit v0.12 From 0b0e3330bcc62b8582ce6fee3a7465391f7b56c7 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sun, 10 Jul 2022 15:25:43 -0700 Subject: minor frame format clarification no need to specify that a decoder can "ignore the checksum". --- doc/lz4_Frame_format.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/doc/lz4_Frame_format.md b/doc/lz4_Frame_format.md index 7b6c2ef..97a2cbe 100644 --- a/doc/lz4_Frame_format.md +++ b/doc/lz4_Frame_format.md @@ -244,8 +244,7 @@ One-byte checksum of combined descriptor fields, including optional ones. The value is the second byte of `xxh32()` : ` (xxh32()>>8) & 0xFF ` using zero as a seed, and the full Frame Descriptor as an input (including optional fields when they are present). -A wrong checksum indicates an error in the descriptor. -Header checksum is informational and can be skipped. +A wrong checksum indicates that the descriptor is erroneous. Data Blocks -- cgit v0.12 From 34f25c3c1d34249c2ef029449360fd8c4110faf7 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sun, 10 Jul 2022 15:32:42 -0700 Subject: fixed direct-leak in round_trip_fuzzer.c reported by oss-fuzz --- ossfuzz/round_trip_fuzzer.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ossfuzz/round_trip_fuzzer.c b/ossfuzz/round_trip_fuzzer.c index 2c35d9a..6236201 100644 --- a/ossfuzz/round_trip_fuzzer.c +++ b/ossfuzz/round_trip_fuzzer.c @@ -23,13 +23,13 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) size_t const largeSize = 64 * 1024 - 1; size_t const smallSize = 1024; char* const dstPlusLargePrefix = (char*)malloc(dstCapacity + largeSize); + FUZZ_ASSERT(dstPlusLargePrefix); char* const dstPlusSmallPrefix = dstPlusLargePrefix + largeSize - smallSize; char* const largeDict = (char*)malloc(largeSize); + FUZZ_ASSERT(largeDict); char* const smallDict = largeDict + largeSize - smallSize; char* const dst = dstPlusLargePrefix + largeSize; char* const rt = (char*)malloc(size); - - FUZZ_ASSERT(dst); FUZZ_ASSERT(rt); /* Compression must succeed and round trip correctly. */ @@ -109,6 +109,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) } free(dstPlusLargePrefix); + free(largeDict); free(rt); FUZZ_dataProducer_free(producer); -- cgit v0.12 From b4f508608f8f9423350cf48e46dd22c452f0b809 Mon Sep 17 00:00:00 2001 From: jonrumsey Date: Mon, 11 Jul 2022 11:28:32 +0100 Subject: Change definitions of LZ4_STREAMSIZE, LZ4_STREAMDECODESIZE and LZ4_STREAMHCSIZE to factor in OS400 pointer length and structure alignment rules Update the length values on platforms where pointers are 16-bytes, factor in implicit compiler padding to ensure proper alignment of members and overall structure lengths --- lib/lz4.h | 30 ++++++++++++++++++++++++++++-- lib/lz4hc.h | 20 +++++++++++++++++++- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/lib/lz4.h b/lib/lz4.h index 07cc18e..1bd7d26 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -631,8 +631,22 @@ typedef struct { * 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. + * Note : OS400 pointers are 16 bytes and the compiler adds 8 bytes of padding after + * tableType and 12 bytes after dictSize to ensure the structure is word aligned: + * |========================================================= + * | Offset | Length | Member Name + * |========================================================= + * | 0 | 16384 | hashTable[4096] + * | 16384 | 4 | currentOffset + * | 16388 | 4 | tableType + * | 16392 | 8 | ***PADDING*** + * | 16400 | 16 | dictionary + * | 16416 | 16 | dictCtx + * | 16432 | 4 | dictSize + * | 16436 | 12 | ***PADDING*** + * ========================================================== */ -#define LZ4_STREAMSIZE ((1UL << LZ4_MEMORY_USAGE) + 32) /* static size, for inter-version compatibility */ +#define LZ4_STREAMSIZE ((1UL << LZ4_MEMORY_USAGE) + ((sizeof(void*)==16) ? 64 : 32)) /* static size, for inter-version compatibility */ #define LZ4_STREAMSIZE_VOIDP (LZ4_STREAMSIZE / sizeof(void*)) union LZ4_stream_u { void* table[LZ4_STREAMSIZE_VOIDP]; @@ -663,8 +677,20 @@ LZ4LIB_API LZ4_stream_t* LZ4_initStream (void* buffer, size_t size); * note : only use in association with static linking ! * this definition is not API/ABI safe, * and may change in a future version ! + * Note : Same story as LZ4_STREAMSIZE for OS400 in terms of additional padding to + * ensure pointers start on and structures finish on 16 byte boundaries + * |========================================================= + * | Offset | Length | Member Name + * |========================================================= + * | 0 | 16 | externalDict + * | 16 | 4 | extDictSize + * | 20 | 12 | ***PADDING*** + * | 32 | 16 | prefixEnd + * | 48 | 4 | prefixSize + * | 52 | 12 | ***PADDING*** + * ========================================================== */ -#define LZ4_STREAMDECODESIZE_U64 (4 + ((sizeof(void*)==16) ? 2 : 0) /*AS-400*/ ) +#define LZ4_STREAMDECODESIZE_U64 (4 + ((sizeof(void*)==16) ? 4 : 0)) #define LZ4_STREAMDECODESIZE (LZ4_STREAMDECODESIZE_U64 * sizeof(unsigned long long)) union LZ4_streamDecode_u { unsigned long long table[LZ4_STREAMDECODESIZE_U64]; diff --git a/lib/lz4hc.h b/lib/lz4hc.h index f4afc9b..6176457 100644 --- a/lib/lz4hc.h +++ b/lib/lz4hc.h @@ -219,8 +219,26 @@ struct LZ4HC_CCtx_internal /* Do not use these definitions directly ! * Declare or allocate an LZ4_streamHC_t instead. + * Note : OS400 uses 16 byte pointers and so the structure size is larger than other + * platforms + * |=========================================================== + * | Offset | Length | Member Name + * |=========================================================== + * | 0 | 131072 | hashTable[32768] + * | 131072 | 131072 | chainTable[65536] + * | 262144 | 16 | end + * | 262160 | 16 | base + * | 262176 | 16 | dictBase + * | 262192 | 4 | dictLimit + * | 262196 | 4 | lowLimit + * | 262200 | 4 | nextToUpdate + * | 262204 | 2 | compressionLevel + * | 262206 | 1 | favorDecSpeed + * | 262207 | 1 | dirty + * | 262208 | 16 | dictCtx + * ============================================================ */ -#define LZ4_STREAMHCSIZE 262200 /* static size, for inter-version compatibility */ +#define LZ4_STREAMHCSIZE (262200 + ((sizeof(void*)==16) ? 24 : 0)) /* static size, for inter-version compatibility */ #define LZ4_STREAMHCSIZE_VOIDP (LZ4_STREAMHCSIZE / sizeof(void*)) union LZ4_streamHC_u { void* table[LZ4_STREAMHCSIZE_VOIDP]; -- cgit v0.12 From 9de5b571d77f3b33c75750783012e13747bb270d Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 11 Jul 2022 03:46:15 -0700 Subject: minor refactor : simplify LZ4F_makeBlock one less argument --- lib/lz4frame.c | 47 ++++++++++++++++++++--------------------------- 1 file changed, 20 insertions(+), 27 deletions(-) diff --git a/lib/lz4frame.c b/lib/lz4frame.c index 80e244b..aec2728 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -761,27 +761,16 @@ 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, - LZ4F_blockCompression_t blockCompression) + LZ4F_blockChecksum_t crcFlag) { BYTE* const cSizePtr = (BYTE*)dst; U32 cSize; - if (compress != NULL) { - cSize = (U32)compress(lz4ctx, (const char*)src, (char*)(cSizePtr+BHSize), - (int)(srcSize), (int)(srcSize-1), - level, cdict); - } else { - cSize = (U32)srcSize; - /* force no compression if compress callback is null */ - blockCompression = LZ4B_UNCOMPRESSED; - } - - if (cSize == 0) { /* compression failed */ - DEBUGLOG(5, "LZ4F_makeBlock: compression failed, creating a raw block (size %u)", (U32)srcSize); - blockCompression = LZ4B_UNCOMPRESSED; - } + assert(compress != NULL); + cSize = (U32)compress(lz4ctx, (const char*)src, (char*)(cSizePtr+BHSize), + (int)(srcSize), (int)(srcSize-1), + level, cdict); - if (blockCompression == LZ4B_UNCOMPRESSED) { + if (cSize == 0 || cSize >= srcSize) { cSize = (U32)srcSize; LZ4F_writeLE32(cSizePtr, cSize | LZ4F_BLOCKUNCOMPRESSED_FLAG); memcpy(cSizePtr+BHSize, src, srcSize); @@ -831,8 +820,15 @@ static int LZ4F_compressBlockHC_continue(void* ctx, const char* src, char* dst, return LZ4_compress_HC_continue((LZ4_streamHC_t*)ctx, src, dst, srcSize, dstCapacity); } -static compressFunc_t LZ4F_selectCompression(LZ4F_blockMode_t blockMode, int level) +static int LZ4F_doNotCompressBlock(void* ctx, const char* src, char* dst, int srcSize, int dstCapacity, int level, const LZ4F_CDict* cdict) +{ + (void)ctx; (void)src; (void)dst; (void)srcSize; (void)dstCapacity; (void)level; (void)cdict; + return 0; +} + +static compressFunc_t LZ4F_selectCompression(LZ4F_blockMode_t blockMode, int level, LZ4F_blockCompression_t compressMode) { + if (compressMode == LZ4B_UNCOMPRESSED) return LZ4F_doNotCompressBlock; if (level < LZ4HC_CLEVEL_MIN) { if (blockMode == LZ4F_blockIndependent) return LZ4F_compressBlock; return LZ4F_compressBlock_continue; @@ -878,7 +874,7 @@ static size_t LZ4F_compressUpdateImpl(LZ4F_cctx* cctxPtr, BYTE* const dstStart = (BYTE*)dstBuffer; BYTE* dstPtr = dstStart; LZ4F_lastBlockStatus lastBlockCompressed = notDone; - compressFunc_t const compress = LZ4F_selectCompression(cctxPtr->prefs.frameInfo.blockMode, cctxPtr->prefs.compressionLevel); + compressFunc_t const compress = LZ4F_selectCompression(cctxPtr->prefs.frameInfo.blockMode, cctxPtr->prefs.compressionLevel, blockCompression); size_t bytesWritten; DEBUGLOG(4, "LZ4F_compressUpdate (srcSize=%zu)", srcSize); @@ -918,7 +914,7 @@ static size_t LZ4F_compressUpdateImpl(LZ4F_cctx* cctxPtr, cctxPtr->tmpIn, blockSize, compress, cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel, cctxPtr->cdict, - cctxPtr->prefs.frameInfo.blockChecksumFlag, blockCompression); + cctxPtr->prefs.frameInfo.blockChecksumFlag); if (cctxPtr->prefs.frameInfo.blockMode==LZ4F_blockLinked) cctxPtr->tmpIn += blockSize; cctxPtr->tmpInSize = 0; } } @@ -930,8 +926,7 @@ static size_t LZ4F_compressUpdateImpl(LZ4F_cctx* cctxPtr, srcPtr, blockSize, compress, cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel, cctxPtr->cdict, - cctxPtr->prefs.frameInfo.blockChecksumFlag, - blockCompression); + cctxPtr->prefs.frameInfo.blockChecksumFlag); srcPtr += blockSize; } @@ -942,8 +937,7 @@ static size_t LZ4F_compressUpdateImpl(LZ4F_cctx* cctxPtr, srcPtr, (size_t)(srcEnd - srcPtr), compress, cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel, cctxPtr->cdict, - cctxPtr->prefs.frameInfo.blockChecksumFlag, - blockCompression); + cctxPtr->prefs.frameInfo.blockChecksumFlag); srcPtr = srcEnd; } @@ -1056,15 +1050,14 @@ size_t LZ4F_flush(LZ4F_cctx* cctxPtr, (void)compressOptionsPtr; /* not useful (yet) */ /* select compression function */ - compress = LZ4F_selectCompression(cctxPtr->prefs.frameInfo.blockMode, cctxPtr->prefs.compressionLevel); + compress = LZ4F_selectCompression(cctxPtr->prefs.frameInfo.blockMode, cctxPtr->prefs.compressionLevel, cctxPtr->blockCompression); /* compress tmp buffer */ dstPtr += LZ4F_makeBlock(dstPtr, cctxPtr->tmpIn, cctxPtr->tmpInSize, compress, cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel, cctxPtr->cdict, - cctxPtr->prefs.frameInfo.blockChecksumFlag, - cctxPtr->blockCompression); + cctxPtr->prefs.frameInfo.blockChecksumFlag); assert(((void)"flush overflows dstBuffer!", (size_t)(dstPtr - dstStart) <= dstCapacity)); if (cctxPtr->prefs.frameInfo.blockMode == LZ4F_blockLinked) -- cgit v0.12 From aab32f454e0255e6575b062b07042e35b7098d56 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 4 Jul 2022 20:31:56 -0700 Subject: first ABI compat tests only use current march & default compiler --- tests/.gitignore | 3 + tests/Makefile | 9 ++- tests/abiTest.c | 216 ++++++++++++++++++++++++++++++++++++++++++++++++++ tests/test-lz4-abi.py | 150 +++++++++++++++++++++++++++++++++++ 4 files changed, 377 insertions(+), 1 deletion(-) create mode 100644 tests/abiTest.c create mode 100644 tests/test-lz4-abi.py diff --git a/tests/.gitignore b/tests/.gitignore index 346c989..5337fdb 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -13,9 +13,12 @@ checkTag checkFrame decompress-partial decompress-partial-usingDict +abiTest + # test artefacts tmp* versionsTest +abiTests lz4_all.c # local tests diff --git a/tests/Makefile b/tests/Makefile index b67f135..6f063a1 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -127,7 +127,8 @@ clean: fasttest$(EXT) roundTripTest$(EXT) \ datagen$(EXT) checkTag$(EXT) \ frameTest$(EXT) decompress-partial$(EXT) \ - lz4_all.c + abiTest$(EXT) \ + lz4_all.c @$(RM) -rf $(TESTDIR) @echo Cleaning completed @@ -139,6 +140,12 @@ versionsTest: listTest: lz4 QEMU_SYS=$(QEMU_SYS) $(PYTHON) test-lz4-list.py +abiTest: LDLIBS += -llz4 + +.PHONY: abiTests +abiTests: + $(PYTHON) test-lz4-abi.py + checkTag: checkTag.c $(LZ4DIR)/lz4.h $(CC) $(FLAGS) $< -o $@$(EXT) diff --git a/tests/abiTest.c b/tests/abiTest.c new file mode 100644 index 0000000..c637511 --- /dev/null +++ b/tests/abiTest.c @@ -0,0 +1,216 @@ +/* + * Copyright (c) 2016-2020, Yann Collet, 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), + * meaning you may select, at your option, one of the above-listed licenses. + */ + +/* + * abiTest : + * ensure ABI stability expectations are not broken by a new version +**/ + + +/*=========================================== +* Dependencies +*==========================================*/ +#include /* size_t */ +#include /* malloc, free, exit */ +#include /* fprintf */ +#include /* strcmp */ +#include +#include /* stat */ +#include /* stat */ +#include "xxhash.h" + +#include "lz4.h" +#include "lz4frame.h" + + +/*=========================================== +* Macros +*==========================================*/ +#define MIN(a,b) ( (a) < (b) ? (a) : (b) ) + +#define MSG(...) fprintf(stderr, __VA_ARGS__) + +#define CONTROL_MSG(c, ...) { \ + if ((c)) { \ + MSG(__VA_ARGS__); \ + MSG(" \n"); \ + abort(); \ + } \ +} + + +static size_t checkBuffers(const void* buff1, const void* buff2, size_t buffSize) +{ + const char* const ip1 = (const char*)buff1; + const char* const ip2 = (const char*)buff2; + size_t pos; + + for (pos=0; pos = LZ4_compressBound(srcSize)` + * for compression to be guaranteed to work */ +static void roundTripTest(void* resultBuff, size_t resultBuffCapacity, + void* compressedBuff, size_t compressedBuffCapacity, + const void* srcBuff, size_t srcSize) +{ + int const acceleration = 1; + // Note : can't use LZ4_initStream(), because it's only present since v1.9.0 + memset(&LZ4_cState, 0, sizeof(LZ4_cState)); + { int const cSize = LZ4_compress_fast_continue(&LZ4_cState, (const char*)srcBuff, (char*)compressedBuff, (int)srcSize, (int)compressedBuffCapacity, acceleration); + CONTROL_MSG(cSize == 0, "Compression error !"); + { int const dInit = LZ4_setStreamDecode(&LZ4_dState, NULL, 0); + CONTROL_MSG(dInit == 0, "LZ4_setStreamDecode error !"); + } + { int const dSize = LZ4_decompress_safe_continue (&LZ4_dState, (const char*)compressedBuff, (char*)resultBuff, cSize, (int)resultBuffCapacity); + CONTROL_MSG(dSize < 0, "Decompression detected an error !"); + CONTROL_MSG(dSize != (int)srcSize, "Decompression corruption error : wrong decompressed size !"); + } } + + /* check potential content corruption error */ + assert(resultBuffCapacity >= srcSize); + { size_t const errorPos = checkBuffers(srcBuff, resultBuff, srcSize); + CONTROL_MSG(errorPos != srcSize, + "Silent decoding corruption, at pos %u !!!", + (unsigned)errorPos); + } + +} + +static void roundTripCheck(const void* srcBuff, size_t srcSize) +{ + size_t const cBuffSize = LZ4_COMPRESSBOUND(srcSize); + void* const cBuff = malloc(cBuffSize); + void* const rBuff = malloc(cBuffSize); + + if (!cBuff || !rBuff) { + fprintf(stderr, "not enough memory ! \n"); + exit(1); + } + + roundTripTest(rBuff, cBuffSize, + cBuff, cBuffSize, + srcBuff, srcSize); + + free(rBuff); + free(cBuff); +} + + +static size_t 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... */ +#else + struct stat statbuf; + r = stat(infilename, &statbuf); + if (r || !S_ISREG(statbuf.st_mode)) return 0; /* No good... */ +#endif + return (size_t)statbuf.st_size; +} + + +static int isDirectory(const char* infilename) +{ + int r; +#if defined(_MSC_VER) + struct _stat64 statbuf; + r = _stat64(infilename, &statbuf); + if (!r && (statbuf.st_mode & _S_IFDIR)) return 1; +#else + struct stat statbuf; + r = stat(infilename, &statbuf); + if (!r && S_ISDIR(statbuf.st_mode)) return 1; +#endif + return 0; +} + + +/** loadFile() : + * requirement : `buffer` size >= `fileSize` */ +static void loadFile(void* buffer, const char* fileName, size_t fileSize) +{ + FILE* const f = fopen(fileName, "rb"); + if (isDirectory(fileName)) { + MSG("Ignoring %s directory \n", fileName); + exit(2); + } + if (f==NULL) { + MSG("Impossible to open %s \n", fileName); + exit(3); + } + { size_t const readSize = fread(buffer, 1, fileSize, f); + if (readSize != fileSize) { + MSG("Error reading %s \n", fileName); + exit(5); + } } + fclose(f); +} + + +static void fileCheck(const char* fileName) +{ + size_t const fileSize = getFileSize(fileName); + void* const buffer = malloc(fileSize + !fileSize /* avoid 0 */); + if (!buffer) { + MSG("not enough memory \n"); + exit(4); + } + loadFile(buffer, fileName, fileSize); + roundTripCheck(buffer, fileSize); + free (buffer); +} + + +int bad_usage(const char* exeName) +{ + MSG(" \n"); + MSG("bad usage: \n"); + MSG(" \n"); + MSG("%s [Options] fileName \n", exeName); + MSG(" \n"); + MSG("Options: \n"); + MSG("-# : use #=[0-9] compression level (default:0 == random) \n"); + return 1; +} + + +int main(int argCount, const char** argv) +{ + const char* const exeName = argv[0]; + int argNb = 1; + MSG("starting abiTest: \n"); + + assert(argCount >= 1); + if (argCount < 2) return bad_usage(exeName); + + if (argNb >= argCount) return bad_usage(exeName); + + fileCheck(argv[argNb]); + MSG("no pb detected \n"); + return 0; +} diff --git a/tests/test-lz4-abi.py b/tests/test-lz4-abi.py new file mode 100644 index 0000000..378f62f --- /dev/null +++ b/tests/test-lz4-abi.py @@ -0,0 +1,150 @@ +#!/usr/bin/env python3 +"""Test LZ4 interoperability between versions""" + +# +# Copyright (C) 2011-present, Takayuki Matsuoka +# All rights reserved. +# GPL v2 License +# + +import glob +import subprocess +import filecmp +import os +import shutil +import sys +import hashlib + +repo_url = 'https://github.com/lz4/lz4.git' +tmp_dir_name = 'tests/abiTests' +env_flags = ' ' # '-j MOREFLAGS="-g -O0 -fsanitize=address"' +make_cmd = 'make' +git_cmd = 'git' +test_dat_src = ['README.md'] +head = 'v999' + +def proc(cmd_args, pipe=True, env=False): + if env == False: + env = os.environ.copy() + # we want the address sanitizer for abi tests + env["MOREFLAGS"] = "-fsanitize=address" + if pipe: + subproc = subprocess.Popen(cmd_args, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + env = env) + else: + subproc = subprocess.Popen(cmd_args, env = env) + return subproc.communicate() + +def make(args, pipe=True, env=False): + return proc([make_cmd] + ['-j'] + ['V=1'] + args, pipe, env) + +def git(args, pipe=True): + return proc([git_cmd] + args, pipe) + +def get_git_tags(): + # Only start from first v1.7.x format release + stdout, stderr = git(['tag', '-l', 'v[1-9].[0-9].[0-9]']) + tags = stdout.decode('utf-8').split() + return tags + +# https://stackoverflow.com/a/19711609/2132223 +def sha1_of_file(filepath): + with open(filepath, 'rb') as f: + return hashlib.sha1(f.read()).hexdigest() + +if __name__ == '__main__': + error_code = 0 + base_dir = os.getcwd() + '/..' # /path/to/lz4 + tmp_dir = base_dir + '/' + tmp_dir_name # /path/to/lz4/tests/versionsTest + clone_dir = tmp_dir + '/' + 'lz4' # /path/to/lz4/tests/versionsTest/lz4 + lib_dir = base_dir + '/lib' # /path/to/lz4/lib + test_dir = base_dir + '/tests' + os.makedirs(tmp_dir, exist_ok=True) + + # since Travis clones limited depth, we should clone full repository + if not os.path.isdir(clone_dir): + git(['clone', repo_url, clone_dir]) + + # Retrieve all release tags + print('Retrieve all release tags :') + os.chdir(clone_dir) + tags = [head] + get_git_tags() + print(tags); + + # Build all versions of liblz4 + # note : naming scheme only works on Linux + for tag in tags: + print('building library ', tag) + os.chdir(base_dir) +# if not os.path.isfile(dst_liblz4) or tag == head: + if tag != head: + r_dir = '{}/{}'.format(tmp_dir, tag) # /path/to/lz4/test/lz4test/ + #print('r_dir = ', r_dir) # for debug + os.makedirs(r_dir, exist_ok=True) + os.chdir(clone_dir) + git(['--work-tree=' + r_dir, 'checkout', tag, '--', '.']) + os.chdir(r_dir + '/lib') # /path/to/lz4/lz4test/ /lib + else: + # print('lib_dir = {}', lib_dir) # for debug + os.chdir(lib_dir) # for debug + make(['clean']) + make(['liblz4']) + + print(' ') + print('******************************') + print('Round trip expecting current ABI but linking to older Dynamic Library version') + print('******************************') + os.chdir(test_dir) + # Start with matching version : should be no problem + build_env = os.environ.copy() + build_env["LDFLAGS"] = "-L../lib" + build_env["LDLIBS"] = "-llz4" + # we use asan to detect any out-of-bound read or write + build_env["MOREFLAGS"] = "-fsanitize=address" + os.remove('abiTest') + make(['abiTest'], env=build_env) + proc(['./abiTest'] + ['README.md']) + + for tag in tags: + print('linking to lib tag = ', tag) + run_env = os.environ.copy() + run_env["LD_LIBRARY_PATH"] = 'abiTests/{}/lib'.format(tag) + # check we are linking to the right library version at run time + proc(['ldd'] + ['./abiTest'], pipe=False, env=run_env) + # now run with mismatched library version + proc(['./abiTest'] + test_dat_src, pipe=False, env=run_env) + + print(' ') + print('******************************') + print('Round trip using current Dynamic Library expecting older ABI version') + print('******************************') + + for tag in tags: + print('building using older lib ', tag) + build_env = os.environ.copy() + if tag != head: + build_env["CPPFLAGS"] = '-IabiTests/{}/lib'.format(tag) + build_env["LDFLAGS"] = '-LabiTests/{}/lib'.format(tag) + else: + build_env["CPPFLAGS"] = '-I../lib' + build_env["LDFLAGS"] = '-L../lib' + build_env["LDLIBS"] = "-llz4" + build_env["MOREFLAGS"] = "-fsanitize=address" + os.remove('abiTest') + make(['abiTest'], pipe=False, env=build_env) + + print('run with CURRENT library version (head)') + run_env = os.environ.copy() + run_env["LD_LIBRARY_PATH"] = '../lib' + # check we are linking to the right library version at run time + proc(['ldd'] + ['./abiTest'], pipe=False, env=run_env) + # now run with mismatched library version + proc(['./abiTest'] + test_dat_src, pipe=False, env=run_env) + + + if error_code != 0: + print('ERROR') + + sys.exit(error_code) -- cgit v0.12 From 3e2426b198293d1e64de949c40fe0ddc2fb9befb Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 12 Jul 2022 09:40:45 -0700 Subject: write liblz4 dynamic library version requires liblz4 >= v1.7.5 --- .gitignore | 1 + tests/abiTest.c | 6 ++++-- tests/check_liblz4_version.sh | 6 ++++++ tests/test-lz4-abi.py | 29 ++++++++++++++++++----------- 4 files changed, 29 insertions(+), 13 deletions(-) create mode 100755 tests/check_liblz4_version.sh diff --git a/.gitignore b/.gitignore index 05120f8..ed02057 100644 --- a/.gitignore +++ b/.gitignore @@ -25,6 +25,7 @@ _codelite/ _codelite_lz4/ bin/ *.zip +*.swp # analyzers infer-out diff --git a/tests/abiTest.c b/tests/abiTest.c index c637511..423dc1f 100644 --- a/tests/abiTest.c +++ b/tests/abiTest.c @@ -77,6 +77,7 @@ static void roundTripTest(void* resultBuff, size_t resultBuffCapacity, { int const acceleration = 1; // Note : can't use LZ4_initStream(), because it's only present since v1.9.0 + MSG("Initializing LZ4_cState, of size %zu bytes \n", sizeof(LZ4_cState)); memset(&LZ4_cState, 0, sizeof(LZ4_cState)); { int const cSize = LZ4_compress_fast_continue(&LZ4_cState, (const char*)srcBuff, (char*)compressedBuff, (int)srcSize, (int)compressedBuffCapacity, acceleration); CONTROL_MSG(cSize == 0, "Compression error !"); @@ -95,7 +96,6 @@ static void roundTripTest(void* resultBuff, size_t resultBuffCapacity, "Silent decoding corruption, at pos %u !!!", (unsigned)errorPos); } - } static void roundTripCheck(const void* srcBuff, size_t srcSize) @@ -203,7 +203,9 @@ int main(int argCount, const char** argv) { const char* const exeName = argv[0]; int argNb = 1; - MSG("starting abiTest: \n"); + MSG("abiTest, built binary based on API %s \n", LZ4_VERSION_STRING); + // Note : LZ4_versionString() requires >= v1.7.5+ + MSG("currently linked to dll %s \n", LZ4_versionString()); assert(argCount >= 1); if (argCount < 2) return bad_usage(exeName); diff --git a/tests/check_liblz4_version.sh b/tests/check_liblz4_version.sh new file mode 100755 index 0000000..9304204 --- /dev/null +++ b/tests/check_liblz4_version.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env sh +set -e + +# written as a script shell, because pipe management in python is horrible +ldd $1 | grep liblz4 + diff --git a/tests/test-lz4-abi.py b/tests/test-lz4-abi.py index 378f62f..e194ce2 100644 --- a/tests/test-lz4-abi.py +++ b/tests/test-lz4-abi.py @@ -29,13 +29,17 @@ def proc(cmd_args, pipe=True, env=False): # we want the address sanitizer for abi tests env["MOREFLAGS"] = "-fsanitize=address" if pipe: - subproc = subprocess.Popen(cmd_args, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - env = env) + s = subprocess.Popen(cmd_args, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + env = env) else: - subproc = subprocess.Popen(cmd_args, env = env) - return subproc.communicate() + s = subprocess.Popen(cmd_args, env = env) + r = s.communicate() + if s.poll() != 0: + print(' s.poll() = ', s.poll()) + sys.exit(1) + return r def make(args, pipe=True, env=False): return proc([make_cmd] + ['-j'] + ['V=1'] + args, pipe, env) @@ -68,10 +72,11 @@ if __name__ == '__main__': git(['clone', repo_url, clone_dir]) # Retrieve all release tags - print('Retrieve all release tags :') + print('Retrieve release tags >= v1.7.5 :') os.chdir(clone_dir) tags = [head] + get_git_tags() - print(tags); + tags = [x for x in tags if (x >= 'v1.7.5')] + print(tags) # Build all versions of liblz4 # note : naming scheme only works on Linux @@ -103,7 +108,8 @@ if __name__ == '__main__': build_env["LDLIBS"] = "-llz4" # we use asan to detect any out-of-bound read or write build_env["MOREFLAGS"] = "-fsanitize=address" - os.remove('abiTest') + if os.path.isfile('abiTest'): + os.remove('abiTest') make(['abiTest'], env=build_env) proc(['./abiTest'] + ['README.md']) @@ -112,7 +118,7 @@ if __name__ == '__main__': run_env = os.environ.copy() run_env["LD_LIBRARY_PATH"] = 'abiTests/{}/lib'.format(tag) # check we are linking to the right library version at run time - proc(['ldd'] + ['./abiTest'], pipe=False, env=run_env) + proc(['./check_liblz4_version.sh'] + ['./abiTest'], pipe=False, env=run_env) # now run with mismatched library version proc(['./abiTest'] + test_dat_src, pipe=False, env=run_env) @@ -122,6 +128,7 @@ if __name__ == '__main__': print('******************************') for tag in tags: + print(' ') print('building using older lib ', tag) build_env = os.environ.copy() if tag != head: @@ -139,7 +146,7 @@ if __name__ == '__main__': run_env = os.environ.copy() run_env["LD_LIBRARY_PATH"] = '../lib' # check we are linking to the right library version at run time - proc(['ldd'] + ['./abiTest'], pipe=False, env=run_env) + proc(['./check_liblz4_version.sh'] + ['./abiTest'], pipe=False, env=run_env) # now run with mismatched library version proc(['./abiTest'] + test_dat_src, pipe=False, env=run_env) -- cgit v0.12 From d64575391967af5fbbc3d991ecb9035fb3460531 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 12 Jul 2022 18:59:54 +0200 Subject: generalize across all 3 ABI architectures --- tests/abiTest.c | 1 - tests/test-lz4-abi.py | 154 ++++++++++++++++++++++++++++---------------------- 2 files changed, 85 insertions(+), 70 deletions(-) diff --git a/tests/abiTest.c b/tests/abiTest.c index 423dc1f..91e4e79 100644 --- a/tests/abiTest.c +++ b/tests/abiTest.c @@ -77,7 +77,6 @@ static void roundTripTest(void* resultBuff, size_t resultBuffCapacity, { int const acceleration = 1; // Note : can't use LZ4_initStream(), because it's only present since v1.9.0 - MSG("Initializing LZ4_cState, of size %zu bytes \n", sizeof(LZ4_cState)); memset(&LZ4_cState, 0, sizeof(LZ4_cState)); { int const cSize = LZ4_compress_fast_continue(&LZ4_cState, (const char*)srcBuff, (char*)compressedBuff, (int)srcSize, (int)compressedBuffCapacity, acceleration); CONTROL_MSG(cSize == 0, "Compression error !"); diff --git a/tests/test-lz4-abi.py b/tests/test-lz4-abi.py index e194ce2..0c8fd05 100644 --- a/tests/test-lz4-abi.py +++ b/tests/test-lz4-abi.py @@ -26,8 +26,6 @@ head = 'v999' def proc(cmd_args, pipe=True, env=False): if env == False: env = os.environ.copy() - # we want the address sanitizer for abi tests - env["MOREFLAGS"] = "-fsanitize=address" if pipe: s = subprocess.Popen(cmd_args, stdout=subprocess.PIPE, @@ -42,6 +40,10 @@ def proc(cmd_args, pipe=True, env=False): return r def make(args, pipe=True, env=False): + if env == False: + env = os.environ.copy() + # we want the address sanitizer for abi tests + env["MOREFLAGS"] = "-fsanitize=address" return proc([make_cmd] + ['-j'] + ['V=1'] + args, pipe, env) def git(args, pipe=True): @@ -78,77 +80,91 @@ if __name__ == '__main__': tags = [x for x in tags if (x >= 'v1.7.5')] print(tags) - # Build all versions of liblz4 - # note : naming scheme only works on Linux - for tag in tags: - print('building library ', tag) - os.chdir(base_dir) -# if not os.path.isfile(dst_liblz4) or tag == head: - if tag != head: - r_dir = '{}/{}'.format(tmp_dir, tag) # /path/to/lz4/test/lz4test/ - #print('r_dir = ', r_dir) # for debug - os.makedirs(r_dir, exist_ok=True) - os.chdir(clone_dir) - git(['--work-tree=' + r_dir, 'checkout', tag, '--', '.']) - os.chdir(r_dir + '/lib') # /path/to/lz4/lz4test/ /lib - else: - # print('lib_dir = {}', lib_dir) # for debug - os.chdir(lib_dir) # for debug - make(['clean']) - make(['liblz4']) - - print(' ') - print('******************************') - print('Round trip expecting current ABI but linking to older Dynamic Library version') - print('******************************') - os.chdir(test_dir) - # Start with matching version : should be no problem - build_env = os.environ.copy() - build_env["LDFLAGS"] = "-L../lib" - build_env["LDLIBS"] = "-llz4" - # we use asan to detect any out-of-bound read or write - build_env["MOREFLAGS"] = "-fsanitize=address" - if os.path.isfile('abiTest'): - os.remove('abiTest') - make(['abiTest'], env=build_env) - proc(['./abiTest'] + ['README.md']) - - for tag in tags: - print('linking to lib tag = ', tag) - run_env = os.environ.copy() - run_env["LD_LIBRARY_PATH"] = 'abiTests/{}/lib'.format(tag) - # check we are linking to the right library version at run time - proc(['./check_liblz4_version.sh'] + ['./abiTest'], pipe=False, env=run_env) - # now run with mismatched library version - proc(['./abiTest'] + test_dat_src, pipe=False, env=run_env) - - print(' ') - print('******************************') - print('Round trip using current Dynamic Library expecting older ABI version') - print('******************************') - - for tag in tags: + # loop across architectures + for march in ['-m64', '-m32', '-mx32']: + print(' ') + print('=====================================') + print('Testing architecture ' + march); + print('=====================================') + + # Build all versions of liblz4 + # note : naming scheme only works on Linux + for tag in tags: + print('building library ', tag) + os.chdir(base_dir) + # if not os.path.isfile(dst_liblz4) or tag == head: + if tag != head: + r_dir = '{}/{}'.format(tmp_dir, tag) # /path/to/lz4/test/lz4test/ + #print('r_dir = ', r_dir) # for debug + os.makedirs(r_dir, exist_ok=True) + os.chdir(clone_dir) + git(['--work-tree=' + r_dir, 'checkout', tag, '--', '.']) + os.chdir(r_dir + '/lib') # /path/to/lz4/lz4test/ /lib + else: + # print('lib_dir = {}', lib_dir) # for debug + os.chdir(lib_dir) + make(['clean']) + build_env = os.environ.copy() + build_env["CFLAGS"] = march + build_env["MOREFLAGS"] = "-fsanitize=address" + make(['liblz4'], env=build_env) + print(' ') - print('building using older lib ', tag) + print('******************************') + print('Round trip expecting current ABI but linking to older Dynamic Library version') + print('******************************') + os.chdir(test_dir) + # Start with matching version : should be no problem build_env = os.environ.copy() - if tag != head: - build_env["CPPFLAGS"] = '-IabiTests/{}/lib'.format(tag) - build_env["LDFLAGS"] = '-LabiTests/{}/lib'.format(tag) - else: - build_env["CPPFLAGS"] = '-I../lib' - build_env["LDFLAGS"] = '-L../lib' + build_env["CFLAGS"] = march + build_env["LDFLAGS"] = "-L../lib" build_env["LDLIBS"] = "-llz4" + # we use asan to detect any out-of-bound read or write build_env["MOREFLAGS"] = "-fsanitize=address" - os.remove('abiTest') - make(['abiTest'], pipe=False, env=build_env) - - print('run with CURRENT library version (head)') - run_env = os.environ.copy() - run_env["LD_LIBRARY_PATH"] = '../lib' - # check we are linking to the right library version at run time - proc(['./check_liblz4_version.sh'] + ['./abiTest'], pipe=False, env=run_env) - # now run with mismatched library version - proc(['./abiTest'] + test_dat_src, pipe=False, env=run_env) + if os.path.isfile('abiTest'): + os.remove('abiTest') + make(['abiTest'], env=build_env, pipe=False) + + for tag in tags: + print('linking to lib tag = ', tag) + run_env = os.environ.copy() + if tag == head: + run_env["LD_LIBRARY_PATH"] = '../lib' + else: + run_env["LD_LIBRARY_PATH"] = 'abiTests/{}/lib'.format(tag) + # check we are linking to the right library version at run time + proc(['./check_liblz4_version.sh'] + ['./abiTest'], pipe=False, env=run_env) + # now run with mismatched library version + proc(['./abiTest'] + test_dat_src, pipe=False, env=run_env) + + print(' ') + print('******************************') + print('Round trip using current Dynamic Library expecting older ABI version') + print('******************************') + + for tag in tags: + print(' ') + print('building using older lib ', tag) + build_env = os.environ.copy() + if tag != head: + build_env["CPPFLAGS"] = '-IabiTests/{}/lib'.format(tag) + build_env["LDFLAGS"] = '-LabiTests/{}/lib'.format(tag) + else: + build_env["CPPFLAGS"] = '-I../lib' + build_env["LDFLAGS"] = '-L../lib' + build_env["LDLIBS"] = "-llz4" + build_env["CFLAGS"] = march + build_env["MOREFLAGS"] = "-fsanitize=address" + os.remove('abiTest') + make(['abiTest'], pipe=False, env=build_env) + + print('run with CURRENT library version (head)') + run_env = os.environ.copy() + run_env["LD_LIBRARY_PATH"] = '../lib' + # check we are linking to the right library version at run time + proc(['./check_liblz4_version.sh'] + ['./abiTest'], pipe=False, env=run_env) + # now run with mismatched library version + proc(['./abiTest'] + test_dat_src, pipe=False, env=run_env) if error_code != 0: -- cgit v0.12 From d577589c31c2681e7fa097a7024317d128fd210e Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 12 Jul 2022 19:41:56 +0200 Subject: added abiTests to github --- .github/workflows/ci.yml | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 82ac2d1..f8c49d4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -222,6 +222,21 @@ jobs: run: make V=1 -C tests versionsTest + lz4-abi: + name: LZ4 inter-versions ABI test + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 # https://github.com/actions/checkout + + - name: apt-get install + run: | + sudo apt-get update + sudo apt-get install gcc-multilib + + - name: make -C tests abiTests + run: make V=1 -C tests abiTests + + lz4-frame: name: LZ4 frame test runs-on: ubuntu-latest -- cgit v0.12 From d174f975d218c8d2c4df79da3b80a11c50c59e5a Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 11 Jul 2022 05:04:41 -0700 Subject: clarify static sizes of states for static allocation --- lib/lz4.c | 11 +++++------ lib/lz4.h | 55 ++++++++++--------------------------------------------- lib/lz4hc.c | 4 +--- lib/lz4hc.h | 35 +++++++++++++++++------------------ 4 files changed, 33 insertions(+), 72 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 7374f6e..3f468d7 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -686,7 +686,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(void) { return LZ4_STREAMSIZE; } +int LZ4_sizeofState(void) { return sizeof(LZ4_stream_t); } /*-**************************************** @@ -1443,7 +1443,7 @@ int LZ4_compress_destSize(const char* src, char* dst, int* srcSizePtr, int targe LZ4_stream_t* LZ4_createStream(void) { 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 */ + LZ4_STATIC_ASSERT(sizeof(LZ4_stream_t) >= sizeof(LZ4_stream_t_internal)); DEBUGLOG(4, "LZ4_createStream %p", lz4s); if (lz4s == NULL) return NULL; LZ4_initStream(lz4s, sizeof(*lz4s)); @@ -2323,9 +2323,8 @@ 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; + LZ4_STATIC_ASSERT(sizeof(LZ4_streamDecode_t) >= sizeof(LZ4_streamDecode_t_internal)); + return (LZ4_streamDecode_t*) ALLOC_AND_ZERO(sizeof(LZ4_streamDecode_t)); } int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream) @@ -2550,7 +2549,7 @@ int LZ4_uncompress_unknownOutputSize (const char* source, char* dest, int isize, /* Obsolete Streaming functions */ -int LZ4_sizeofStreamState(void) { return LZ4_STREAMSIZE; } +int LZ4_sizeofStreamState(void) { return sizeof(LZ4_stream_t); } int LZ4_resetStreamState(void* state, char* inputBuffer) { diff --git a/lib/lz4.h b/lib/lz4.h index 1bd7d26..9a896d2 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -623,34 +623,14 @@ typedef struct { /*! 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 future versions. - * Note : OS400 pointers are 16 bytes and the compiler adds 8 bytes of padding after - * tableType and 12 bytes after dictSize to ensure the structure is word aligned: - * |========================================================= - * | Offset | Length | Member Name - * |========================================================= - * | 0 | 16384 | hashTable[4096] - * | 16384 | 4 | currentOffset - * | 16388 | 4 | tableType - * | 16392 | 8 | ***PADDING*** - * | 16400 | 16 | dictionary - * | 16416 | 16 | dictCtx - * | 16432 | 4 | dictSize - * | 16436 | 12 | ***PADDING*** - * ========================================================== + * Never ever use below internal definitions directly ! + * These definitions are not API/ABI safe, and may change in future versions. + * If you need static allocation, declare or allocate an LZ4_stream_t object. */ -#define LZ4_STREAMSIZE ((1UL << LZ4_MEMORY_USAGE) + ((sizeof(void*)==16) ? 64 : 32)) /* static size, for inter-version compatibility */ -#define LZ4_STREAMSIZE_VOIDP (LZ4_STREAMSIZE / sizeof(void*)) +#define LZ4_STREAMSIZE ((1UL << LZ4_MEMORY_USAGE) + 32) /* static size, for inter-version compatibility */ union LZ4_stream_u { - void* table[LZ4_STREAMSIZE_VOIDP]; LZ4_stream_t_internal internal_donotuse; + char minStateSize[LZ4_STREAMSIZE]; }; /* previously typedef'd to LZ4_stream_t */ @@ -672,29 +652,14 @@ LZ4LIB_API LZ4_stream_t* LZ4_initStream (void* buffer, size_t size); /*! 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 ! - * Note : Same story as LZ4_STREAMSIZE for OS400 in terms of additional padding to - * ensure pointers start on and structures finish on 16 byte boundaries - * |========================================================= - * | Offset | Length | Member Name - * |========================================================= - * | 0 | 16 | externalDict - * | 16 | 4 | extDictSize - * | 20 | 12 | ***PADDING*** - * | 32 | 16 | prefixEnd - * | 48 | 4 | prefixSize - * | 52 | 12 | ***PADDING*** - * ========================================================== + * Never ever use below internal definitions directly ! + * These definitions are not API/ABI safe, and may change in future versions. + * If you need static allocation, declare or allocate an LZ4_streamDecode_t object. */ -#define LZ4_STREAMDECODESIZE_U64 (4 + ((sizeof(void*)==16) ? 4 : 0)) -#define LZ4_STREAMDECODESIZE (LZ4_STREAMDECODESIZE_U64 * sizeof(unsigned long long)) +#define LZ4_STREAMDECODESIZE 32 union LZ4_streamDecode_u { - unsigned long long table[LZ4_STREAMDECODESIZE_U64]; LZ4_streamDecode_t_internal internal_donotuse; + char minStateSize[LZ4_STREAMDECODESIZE]; } ; /* previously typedef'd to LZ4_streamDecode_t */ diff --git a/lib/lz4hc.c b/lib/lz4hc.c index 99650a6..77b4767 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -1005,8 +1005,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; - /* 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; @@ -1205,7 +1203,7 @@ int LZ4_compressHC_limitedOutput_continue (LZ4_streamHC_t* ctx, const char* src, /* Deprecated streaming functions */ -int LZ4_sizeofStreamStateHC(void) { return LZ4_STREAMHCSIZE; } +int LZ4_sizeofStreamStateHC(void) { return sizeof(LZ4_streamHC_t); } /* state is presumed correctly sized, aka >= sizeof(LZ4_streamHC_t) * @return : 0 on success, !=0 if error */ diff --git a/lib/lz4hc.h b/lib/lz4hc.h index 6176457..bead076 100644 --- a/lib/lz4hc.h +++ b/lib/lz4hc.h @@ -217,37 +217,36 @@ struct LZ4HC_CCtx_internal }; -/* Do not use these definitions directly ! +/* Never ever use these definitions directly ! * Declare or allocate an LZ4_streamHC_t instead. * Note : OS400 uses 16 byte pointers and so the structure size is larger than other * platforms * |=========================================================== - * | Offset | Length | Member Name + * | Offset | Length | Member Name * |=========================================================== - * | 0 | 131072 | hashTable[32768] + * | 0 | 131072 | hashTable[32768] * | 131072 | 131072 | chainTable[65536] - * | 262144 | 16 | end - * | 262160 | 16 | base - * | 262176 | 16 | dictBase - * | 262192 | 4 | dictLimit - * | 262196 | 4 | lowLimit - * | 262200 | 4 | nextToUpdate - * | 262204 | 2 | compressionLevel - * | 262206 | 1 | favorDecSpeed - * | 262207 | 1 | dirty - * | 262208 | 16 | dictCtx + * | 262144 | 16 | end + * | 262160 | 16 | base + * | 262176 | 16 | dictBase + * | 262192 | 4 | dictLimit + * | 262196 | 4 | lowLimit + * | 262200 | 4 | nextToUpdate + * | 262204 | 2 | compressionLevel + * | 262206 | 1 | favorDecSpeed + * | 262207 | 1 | dirty + * | 262208 | 16 | dictCtx * ============================================================ */ -#define LZ4_STREAMHCSIZE (262200 + ((sizeof(void*)==16) ? 24 : 0)) /* static size, for inter-version compatibility */ -#define LZ4_STREAMHCSIZE_VOIDP (LZ4_STREAMHCSIZE / sizeof(void*)) +#define LZ4_STREAMHCSIZE 262200 /* static size, for inter-version compatibility */ union LZ4_streamHC_u { - void* table[LZ4_STREAMHCSIZE_VOIDP]; LZ4HC_CCtx_internal internal_donotuse; + char minStateSize[LZ4_STREAMHCSIZE]; }; /* previously typedef'd to LZ4_streamHC_t */ /* LZ4_streamHC_t : * This structure allows static allocation of LZ4 HC streaming state. - * This can be used to allocate statically, on state, or as part of a larger structure. + * This can be used to allocate statically on stack, or as part of a larger structure. * * Such state **must** be initialized using LZ4_initStreamHC() before first use. * @@ -262,7 +261,7 @@ union LZ4_streamHC_u { * 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); +LZ4LIB_API LZ4_streamHC_t* LZ4_initStreamHC(void* buffer, size_t size); /*-************************************ -- cgit v0.12 From 60f8eb6f4ca44162f26a7a60e8c1346ce4362999 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 12 Jul 2022 21:41:51 +0200 Subject: minor : specify min versions for library version identifiers --- lib/lz4.h | 42 +++++++++++++++++++++--------------------- lib/lz4hc.h | 29 +++++------------------------ tests/abiTest.c | 1 + 3 files changed, 27 insertions(+), 45 deletions(-) diff --git a/lib/lz4.h b/lib/lz4.h index 9a896d2..a8c6d1e 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -107,10 +107,10 @@ extern "C" { #define LZ4_LIB_VERSION LZ4_VERSION_MAJOR.LZ4_VERSION_MINOR.LZ4_VERSION_RELEASE #define LZ4_QUOTE(str) #str #define LZ4_EXPAND_AND_QUOTE(str) LZ4_QUOTE(str) -#define LZ4_VERSION_STRING LZ4_EXPAND_AND_QUOTE(LZ4_LIB_VERSION) +#define LZ4_VERSION_STRING LZ4_EXPAND_AND_QUOTE(LZ4_LIB_VERSION) /* requires v1.7.3+ */ -LZ4LIB_API int LZ4_versionNumber (void); /**< library version number; useful to check dll version */ -LZ4LIB_API const char* LZ4_versionString (void); /**< library version string; useful to check dll version */ +LZ4LIB_API int LZ4_versionNumber (void); /**< library version number; useful to check dll version; requires v1.3.0+ */ +LZ4LIB_API const char* LZ4_versionString (void); /**< library version string; useful to check dll version; requires v1.7.5+ */ /*-************************************ @@ -604,6 +604,12 @@ LZ4LIB_STATIC_API void LZ4_attach_dictionary(LZ4_stream_t* workingStream, const typedef unsigned int LZ4_u32; #endif +/*! LZ4_stream_t : + * Never ever use below internal definitions directly ! + * These definitions are not API/ABI safe, and may change in future versions. + * If you need static allocation, declare or allocate an LZ4_stream_t object. +**/ + typedef struct LZ4_stream_t_internal LZ4_stream_t_internal; struct LZ4_stream_t_internal { LZ4_u32 hashTable[LZ4_HASH_SIZE_U32]; @@ -614,23 +620,10 @@ struct LZ4_stream_t_internal { LZ4_u32 dictSize; }; -typedef struct { - const LZ4_byte* externalDict; - size_t extDictSize; - const LZ4_byte* prefixEnd; - size_t prefixSize; -} LZ4_streamDecode_t_internal; - - -/*! LZ4_stream_t : - * Never ever use below internal definitions directly ! - * These definitions are not API/ABI safe, and may change in future versions. - * If you need static allocation, declare or allocate an LZ4_stream_t object. - */ -#define LZ4_STREAMSIZE ((1UL << LZ4_MEMORY_USAGE) + 32) /* static size, for inter-version compatibility */ +#define LZ4_STREAM_MINSIZE ((1UL << LZ4_MEMORY_USAGE) + 32) /* static size, for inter-version compatibility */ union LZ4_stream_u { + char minStateSize[LZ4_STREAM_MINSIZE]; LZ4_stream_t_internal internal_donotuse; - char minStateSize[LZ4_STREAMSIZE]; }; /* previously typedef'd to LZ4_stream_t */ @@ -655,11 +648,18 @@ LZ4LIB_API LZ4_stream_t* LZ4_initStream (void* buffer, size_t size); * Never ever use below internal definitions directly ! * These definitions are not API/ABI safe, and may change in future versions. * If you need static allocation, declare or allocate an LZ4_streamDecode_t object. - */ -#define LZ4_STREAMDECODESIZE 32 +**/ +typedef struct { + const LZ4_byte* externalDict; + size_t extDictSize; + const LZ4_byte* prefixEnd; + size_t prefixSize; +} LZ4_streamDecode_t_internal; + +#define LZ4_STREAMDECODE_MINSIZE 32 union LZ4_streamDecode_u { + char minStateSize[LZ4_STREAMDECODE_MINSIZE]; LZ4_streamDecode_t_internal internal_donotuse; - char minStateSize[LZ4_STREAMDECODESIZE]; } ; /* previously typedef'd to LZ4_streamDecode_t */ diff --git a/lib/lz4hc.h b/lib/lz4hc.h index bead076..61e228d 100644 --- a/lib/lz4hc.h +++ b/lib/lz4hc.h @@ -198,6 +198,9 @@ LZ4LIB_API int LZ4_saveDictHC (LZ4_streamHC_t* streamHCPtr, char* safeBuffer, in #define LZ4HC_HASH_MASK (LZ4HC_HASHTABLESIZE - 1) +/* Never ever use these definitions directly ! + * Declare or allocate an LZ4_streamHC_t instead. +**/ typedef struct LZ4HC_CCtx_internal LZ4HC_CCtx_internal; struct LZ4HC_CCtx_internal { @@ -216,32 +219,10 @@ struct LZ4HC_CCtx_internal const LZ4HC_CCtx_internal* dictCtx; }; - -/* Never ever use these definitions directly ! - * Declare or allocate an LZ4_streamHC_t instead. - * Note : OS400 uses 16 byte pointers and so the structure size is larger than other - * platforms - * |=========================================================== - * | Offset | Length | Member Name - * |=========================================================== - * | 0 | 131072 | hashTable[32768] - * | 131072 | 131072 | chainTable[65536] - * | 262144 | 16 | end - * | 262160 | 16 | base - * | 262176 | 16 | dictBase - * | 262192 | 4 | dictLimit - * | 262196 | 4 | lowLimit - * | 262200 | 4 | nextToUpdate - * | 262204 | 2 | compressionLevel - * | 262206 | 1 | favorDecSpeed - * | 262207 | 1 | dirty - * | 262208 | 16 | dictCtx - * ============================================================ - */ -#define LZ4_STREAMHCSIZE 262200 /* static size, for inter-version compatibility */ +#define LZ4_STREAMHC_MINSIZE 262200 /* static size, for inter-version compatibility */ union LZ4_streamHC_u { + char minStateSize[LZ4_STREAMHC_MINSIZE]; LZ4HC_CCtx_internal internal_donotuse; - char minStateSize[LZ4_STREAMHCSIZE]; }; /* previously typedef'd to LZ4_streamHC_t */ /* LZ4_streamHC_t : diff --git a/tests/abiTest.c b/tests/abiTest.c index 91e4e79..e46004a 100644 --- a/tests/abiTest.c +++ b/tests/abiTest.c @@ -202,6 +202,7 @@ int main(int argCount, const char** argv) { const char* const exeName = argv[0]; int argNb = 1; + // Note : LZ4_VERSION_STRING requires >= v1.7.3+ MSG("abiTest, built binary based on API %s \n", LZ4_VERSION_STRING); // Note : LZ4_versionString() requires >= v1.7.5+ MSG("currently linked to dll %s \n", LZ4_versionString()); -- cgit v0.12 From 3da483c0e62af4da75005e9eef30bad00435cb20 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 13 Jul 2022 09:57:42 +0200 Subject: Re-organize state's internal to be more compact produces less padding, notably on OS400 following #1070 by @jonrumsey --- lib/lz4.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/lz4.h b/lib/lz4.h index a8c6d1e..7081e76 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -613,11 +613,12 @@ LZ4LIB_STATIC_API void LZ4_attach_dictionary(LZ4_stream_t* workingStream, const typedef struct LZ4_stream_t_internal LZ4_stream_t_internal; struct LZ4_stream_t_internal { LZ4_u32 hashTable[LZ4_HASH_SIZE_U32]; - LZ4_u32 currentOffset; - LZ4_u32 tableType; const LZ4_byte* dictionary; const LZ4_stream_t_internal* dictCtx; + LZ4_u32 currentOffset; + LZ4_u32 tableType; LZ4_u32 dictSize; + /* Implicit padding to ensure structure is aligned */ }; #define LZ4_STREAM_MINSIZE ((1UL << LZ4_MEMORY_USAGE) + 32) /* static size, for inter-version compatibility */ @@ -640,7 +641,7 @@ union LZ4_stream_u { * 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); @@ -651,8 +652,8 @@ LZ4LIB_API LZ4_stream_t* LZ4_initStream (void* buffer, size_t size); **/ typedef struct { const LZ4_byte* externalDict; - size_t extDictSize; const LZ4_byte* prefixEnd; + size_t extDictSize; size_t prefixSize; } LZ4_streamDecode_t_internal; -- cgit v0.12 From 20d4ff53610124ade31b787ef622dc9e21f63d8d Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 13 Jul 2022 11:48:31 +0200 Subject: removed ->base from lz4hc state replaced by ->prefixStart --- lib/lz4hc.c | 133 ++++++++++++++++++++++++++++++------------------------------ lib/lz4hc.h | 2 +- 2 files changed, 68 insertions(+), 67 deletions(-) diff --git a/lib/lz4hc.c b/lib/lz4hc.c index 77b4767..0360f73 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -42,7 +42,7 @@ * Select how default compression function will allocate workplace memory, * in stack (0:fastest), or in heap (1:requires malloc()). * Since workplace is rather large, heap mode is recommended. - */ +**/ #ifndef LZ4HC_HEAPMODE # define LZ4HC_HEAPMODE 1 #endif @@ -99,18 +99,20 @@ static void LZ4HC_clearTables (LZ4HC_CCtx_internal* hc4) static void LZ4HC_init_internal (LZ4HC_CCtx_internal* hc4, const BYTE* start) { - uptrval startingOffset = (uptrval)(hc4->end - hc4->base); - if (startingOffset > 1 GB) { + size_t const bufferSize = (size_t)(hc4->end - hc4->prefixStart); + size_t newStartingOffset = bufferSize + hc4->dictLimit; + assert(newStartingOffset >= bufferSize); /* check overflow */ + if (newStartingOffset > 1 GB) { LZ4HC_clearTables(hc4); - startingOffset = 0; + newStartingOffset = 0; } - startingOffset += 64 KB; - hc4->nextToUpdate = (U32) startingOffset; - hc4->base = start - startingOffset; + newStartingOffset += 64 KB; + hc4->nextToUpdate = (U32) newStartingOffset; + hc4->prefixStart = start; hc4->end = start; - hc4->dictBase = start - startingOffset; - hc4->dictLimit = (U32) startingOffset; - hc4->lowLimit = (U32) startingOffset; + hc4->dictBase = start - newStartingOffset; + hc4->dictLimit = (U32) newStartingOffset; + hc4->lowLimit = (U32) newStartingOffset; } @@ -119,12 +121,15 @@ LZ4_FORCE_INLINE void LZ4HC_Insert (LZ4HC_CCtx_internal* hc4, const BYTE* ip) { U16* const chainTable = hc4->chainTable; U32* const hashTable = hc4->hashTable; - const BYTE* const base = hc4->base; - U32 const target = (U32)(ip - base); + const BYTE* const prefixPtr = hc4->prefixStart; + U32 const prefixIdx = hc4->dictLimit; + U32 const target = (U32)(ip - prefixPtr) + prefixIdx; U32 idx = hc4->nextToUpdate; + assert(ip >= prefixPtr); + assert(target >= prefixIdx); while (idx < target) { - U32 const h = LZ4HC_hashPtr(base+idx); + U32 const h = LZ4HC_hashPtr(prefixPtr+idx-prefixIdx); size_t delta = idx - hashTable[h]; if (delta>LZ4_DISTANCE_MAX) delta = LZ4_DISTANCE_MAX; DELTANEXTU16(chainTable, idx) = (U16)delta; @@ -247,10 +252,9 @@ LZ4HC_InsertAndGetWiderMatch ( U16* const chainTable = hc4->chainTable; U32* const HashTable = hc4->hashTable; const LZ4HC_CCtx_internal * const dictCtx = hc4->dictCtx; - const BYTE* const base = hc4->base; - const U32 dictLimit = hc4->dictLimit; - const BYTE* const lowPrefixPtr = base + dictLimit; - const U32 ipIndex = (U32)(ip - base); + const BYTE* const prefixPtr = hc4->prefixStart; + const U32 prefixIdx = hc4->dictLimit; + const U32 ipIndex = (U32)(ip - prefixPtr) + prefixIdx; const int withinStartDistance = (hc4->lowLimit + (LZ4_DISTANCE_MAX + 1) > ipIndex); const U32 lowestMatchIndex = (withinStartDistance) ? hc4->lowLimit : ipIndex - LZ4_DISTANCE_MAX; const BYTE* const dictBase = hc4->dictBase; @@ -275,16 +279,13 @@ LZ4HC_InsertAndGetWiderMatch ( assert(matchIndex < ipIndex); if (favorDecSpeed && (ipIndex - matchIndex < 8)) { /* do nothing */ - } else if (matchIndex >= dictLimit) { /* within current Prefix */ - const BYTE* const matchPtr = base + matchIndex; - DEBUGLOG(2, "matchPtr = %p", matchPtr); - DEBUGLOG(2, "lowPrefixPtr = %p", lowPrefixPtr); - assert(matchPtr >= lowPrefixPtr); + } else if (matchIndex >= prefixIdx) { /* within current Prefix */ + const BYTE* const matchPtr = prefixPtr + matchIndex - prefixIdx; assert(matchPtr < ip); assert(longest >= 1); 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; + int const back = lookBackLength ? LZ4HC_countBack(ip, matchPtr, iLowLimit, prefixPtr) : 0; matchLength = MINMATCH + (int)LZ4_count(ip+MINMATCH, matchPtr+MINMATCH, iHighLimit); matchLength -= back; if (matchLength > longest) { @@ -294,20 +295,20 @@ LZ4HC_InsertAndGetWiderMatch ( } } } } else { /* lowestMatchIndex <= matchIndex < dictLimit */ const BYTE* const matchPtr = dictBase + matchIndex; - if ( likely(matchIndex <= dictLimit - 4) + if ( likely(matchIndex <= prefixIdx - 4) && (LZ4_read32(matchPtr) == pattern) ) { const BYTE* const dictStart = dictBase + hc4->lowLimit; int back = 0; - const BYTE* vLimit = ip + (dictLimit - matchIndex); + const BYTE* vLimit = ip + (prefixIdx - matchIndex); if (vLimit > iHighLimit) vLimit = iHighLimit; matchLength = (int)LZ4_count(ip+MINMATCH, matchPtr+MINMATCH, vLimit) + MINMATCH; if ((ip+matchLength == vLimit) && (vLimit < iHighLimit)) - matchLength += LZ4_count(ip+matchLength, lowPrefixPtr, iHighLimit); + matchLength += LZ4_count(ip+matchLength, prefixPtr, iHighLimit); back = lookBackLength ? LZ4HC_countBack(ip, matchPtr, iLowLimit, dictStart) : 0; matchLength -= back; if (matchLength > longest) { longest = matchLength; - *matchpos = base + matchIndex + back; /* virtual pos, relative to ip, to retrieve offset */ + *matchpos = prefixPtr - prefixIdx + matchIndex + back; /* virtual pos, relative to ip, to retrieve offset */ *startpos = ip + back; } } } @@ -347,23 +348,23 @@ LZ4HC_InsertAndGetWiderMatch ( repeat = rep_not; } } if ( (repeat == rep_confirmed) && (matchCandidateIdx >= lowestMatchIndex) - && LZ4HC_protectDictEnd(dictLimit, matchCandidateIdx) ) { - const int extDict = matchCandidateIdx < dictLimit; - const BYTE* const matchPtr = (extDict ? dictBase : base) + matchCandidateIdx; + && LZ4HC_protectDictEnd(prefixIdx, matchCandidateIdx) ) { + const int extDict = matchCandidateIdx < prefixIdx; + const BYTE* const matchPtr = (extDict ? dictBase : prefixPtr - prefixIdx) + matchCandidateIdx; if (LZ4_read32(matchPtr) == pattern) { /* good candidate */ const BYTE* const dictStart = dictBase + hc4->lowLimit; - const BYTE* const iLimit = extDict ? dictBase + dictLimit : iHighLimit; + const BYTE* const iLimit = extDict ? dictBase + prefixIdx : iHighLimit; size_t forwardPatternLength = LZ4HC_countPattern(matchPtr+sizeof(pattern), iLimit, pattern) + sizeof(pattern); if (extDict && matchPtr + forwardPatternLength == iLimit) { U32 const rotatedPattern = LZ4HC_rotatePattern(forwardPatternLength, pattern); - forwardPatternLength += LZ4HC_countPattern(lowPrefixPtr, iHighLimit, rotatedPattern); + forwardPatternLength += LZ4HC_countPattern(prefixPtr, iHighLimit, rotatedPattern); } - { const BYTE* const lowestMatchPtr = extDict ? dictStart : lowPrefixPtr; + { const BYTE* const lowestMatchPtr = extDict ? dictStart : prefixPtr; size_t backLength = LZ4HC_reverseCountPattern(matchPtr, lowestMatchPtr, pattern); size_t currentSegmentLength; - if (!extDict && matchPtr - backLength == lowPrefixPtr && hc4->lowLimit < dictLimit) { + if (!extDict && matchPtr - backLength == prefixPtr && hc4->lowLimit < prefixIdx) { U32 const rotatedPattern = LZ4HC_rotatePattern((U32)(-(int)backLength), pattern); - backLength += LZ4HC_reverseCountPattern(dictBase + dictLimit, dictStart, rotatedPattern); + backLength += LZ4HC_reverseCountPattern(dictBase + prefixIdx, dictStart, rotatedPattern); } /* Limit backLength not go further than lowestMatchIndex */ backLength = matchCandidateIdx - MAX(matchCandidateIdx - (U32)backLength, lowestMatchIndex); @@ -373,28 +374,28 @@ LZ4HC_InsertAndGetWiderMatch ( if ( (currentSegmentLength >= srcPatternLength) /* current pattern segment large enough to contain full srcPatternLength */ && (forwardPatternLength <= srcPatternLength) ) { /* haven't reached this position yet */ U32 const newMatchIndex = matchCandidateIdx + (U32)forwardPatternLength - (U32)srcPatternLength; /* best position, full pattern, might be followed by more match */ - if (LZ4HC_protectDictEnd(dictLimit, newMatchIndex)) + if (LZ4HC_protectDictEnd(prefixIdx, newMatchIndex)) matchIndex = newMatchIndex; else { /* Can only happen if started in the prefix */ - assert(newMatchIndex >= dictLimit - 3 && newMatchIndex < dictLimit && !extDict); - matchIndex = dictLimit; + assert(newMatchIndex >= prefixIdx - 3 && newMatchIndex < prefixIdx && !extDict); + matchIndex = prefixIdx; } } else { 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; + if (!LZ4HC_protectDictEnd(prefixIdx, newMatchIndex)) { + assert(newMatchIndex >= prefixIdx - 3 && newMatchIndex < prefixIdx && !extDict); + matchIndex = prefixIdx; } 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 ((size_t)(ip - base) - matchIndex > LZ4_DISTANCE_MAX) break; + assert(prefixPtr - prefixIdx + matchIndex != ip); + if ((size_t)(ip - prefixPtr) + prefixIdx - matchIndex > LZ4_DISTANCE_MAX) break; assert(maxML < 2 GB); longest = (int)maxML; - *matchpos = base + matchIndex; /* virtual pos, relative to ip, to retrieve offset */ + *matchpos = prefixPtr - prefixIdx + matchIndex; /* virtual pos, relative to ip, to retrieve offset */ *startpos = ip; } { U32 const distToNextPattern = DELTANEXTU16(chainTable, matchIndex); @@ -413,12 +414,12 @@ LZ4HC_InsertAndGetWiderMatch ( if ( dict == usingDictCtxHc && nbAttempts > 0 && ipIndex - lowestMatchIndex < LZ4_DISTANCE_MAX) { - size_t const dictEndOffset = (size_t)(dictCtx->end - dictCtx->base); + size_t const dictEndOffset = (size_t)(dictCtx->end - dictCtx->prefixStart) + dictCtx->dictLimit; U32 dictMatchIndex = dictCtx->hashTable[LZ4HC_hashPtr(ip)]; assert(dictEndOffset <= 1 GB); matchIndex = dictMatchIndex + lowestMatchIndex - (U32)dictEndOffset; while (ipIndex - matchIndex <= LZ4_DISTANCE_MAX && nbAttempts--) { - const BYTE* const matchPtr = dictCtx->base + dictMatchIndex; + const BYTE* const matchPtr = dictCtx->prefixStart - dictCtx->dictLimit + dictMatchIndex; if (LZ4_read32(matchPtr) == pattern) { int mlt; @@ -426,11 +427,11 @@ LZ4HC_InsertAndGetWiderMatch ( const BYTE* vLimit = ip + (dictEndOffset - dictMatchIndex); if (vLimit > iHighLimit) vLimit = iHighLimit; mlt = (int)LZ4_count(ip+MINMATCH, matchPtr+MINMATCH, vLimit) + MINMATCH; - back = lookBackLength ? LZ4HC_countBack(ip, matchPtr, iLowLimit, dictCtx->base + dictCtx->dictLimit) : 0; + back = lookBackLength ? LZ4HC_countBack(ip, matchPtr, iLowLimit, dictCtx->prefixStart) : 0; mlt -= back; if (mlt > longest) { longest = mlt; - *matchpos = base + matchIndex + back; + *matchpos = prefixPtr - prefixIdx + matchIndex + back; *startpos = ip + back; } } @@ -884,7 +885,7 @@ LZ4HC_compress_generic_dictCtx ( limitedOutput_directive limit ) { - const size_t position = (size_t)(ctx->end - ctx->base) - ctx->lowLimit; + const size_t position = (size_t)(ctx->end - ctx->prefixStart) + ctx->dictLimit - ctx->lowLimit; assert(ctx->dictCtx != NULL); if (position >= 64 KB) { ctx->dictCtx = NULL; @@ -1030,13 +1031,13 @@ void LZ4_resetStreamHC_fast (LZ4_streamHC_t* LZ4_streamHCPtr, int compressionLev if (LZ4_streamHCPtr->internal_donotuse.dirty) { LZ4_initStreamHC(LZ4_streamHCPtr, sizeof(*LZ4_streamHCPtr)); } else { - /* preserve end - base : can trigger clearTable's threshold */ + /* preserve end - prefixStart : can trigger clearTable's threshold */ if (LZ4_streamHCPtr->internal_donotuse.end != NULL) { - LZ4_streamHCPtr->internal_donotuse.end -= (uptrval)LZ4_streamHCPtr->internal_donotuse.base; + LZ4_streamHCPtr->internal_donotuse.end -= (uptrval)LZ4_streamHCPtr->internal_donotuse.prefixStart; } else { - assert(LZ4_streamHCPtr->internal_donotuse.base == NULL); + assert(LZ4_streamHCPtr->internal_donotuse.prefixStart == NULL); } - LZ4_streamHCPtr->internal_donotuse.base = NULL; + LZ4_streamHCPtr->internal_donotuse.prefixStart = NULL; LZ4_streamHCPtr->internal_donotuse.dictCtx = NULL; } LZ4_setCompressionLevel(LZ4_streamHCPtr, compressionLevel); @@ -1087,14 +1088,14 @@ void LZ4_attach_HC_dictionary(LZ4_streamHC_t *working_stream, const LZ4_streamHC static void LZ4HC_setExternalDict(LZ4HC_CCtx_internal* ctxPtr, const BYTE* newBlock) { DEBUGLOG(4, "LZ4HC_setExternalDict(%p, %p)", ctxPtr, newBlock); - if (ctxPtr->end >= ctxPtr->base + ctxPtr->dictLimit + 4) + if (ctxPtr->end >= ctxPtr->prefixStart + 4) LZ4HC_Insert (ctxPtr, ctxPtr->end-3); /* Referencing remaining dictionary content */ /* 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); - ctxPtr->dictBase = ctxPtr->base; - ctxPtr->base = newBlock - ctxPtr->dictLimit; + ctxPtr->dictBase = ctxPtr->prefixStart - ctxPtr->dictLimit; + ctxPtr->dictLimit += (U32)(ctxPtr->end - ctxPtr->prefixStart); + ctxPtr->prefixStart = newBlock; ctxPtr->end = newBlock; ctxPtr->nextToUpdate = ctxPtr->dictLimit; /* match referencing will resume from there */ @@ -1113,11 +1114,11 @@ LZ4_compressHC_continue_generic (LZ4_streamHC_t* LZ4_streamHCPtr, LZ4_streamHCPtr, src, *srcSizePtr, limit); assert(ctxPtr != NULL); /* auto-init if forgotten */ - if (ctxPtr->base == NULL) LZ4HC_init_internal (ctxPtr, (const BYTE*) src); + if (ctxPtr->prefixStart == NULL) LZ4HC_init_internal (ctxPtr, (const BYTE*) src); /* Check overflow */ - if ((size_t)(ctxPtr->end - ctxPtr->base) > 2 GB) { - size_t dictSize = (size_t)(ctxPtr->end - ctxPtr->base) - ctxPtr->dictLimit; + if ((size_t)(ctxPtr->end - ctxPtr->prefixStart) + ctxPtr->dictLimit > 2 GB) { + size_t dictSize = (size_t)(ctxPtr->end - ctxPtr->prefixStart); if (dictSize > 64 KB) dictSize = 64 KB; LZ4_loadDictHC(LZ4_streamHCPtr, (const char*)(ctxPtr->end) - dictSize, (int)dictSize); } @@ -1162,7 +1163,7 @@ int LZ4_compress_HC_continue_destSize (LZ4_streamHC_t* LZ4_streamHCPtr, const ch 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)); + int const prefixSize = (int)(streamPtr->end - streamPtr->prefixStart); DEBUGLOG(5, "LZ4_saveDictHC(%p, %p, %d)", LZ4_streamHCPtr, safeBuffer, dictSize); assert(prefixSize >= 0); if (dictSize > 64 KB) dictSize = 64 KB; @@ -1171,9 +1172,9 @@ int LZ4_saveDictHC (LZ4_streamHC_t* LZ4_streamHCPtr, char* safeBuffer, int dictS if (safeBuffer == NULL) assert(dictSize == 0); if (dictSize > 0) memmove(safeBuffer, streamPtr->end - dictSize, dictSize); - { U32 const endIndex = (U32)(streamPtr->end - streamPtr->base); + { U32 const endIndex = (U32)(streamPtr->end - streamPtr->prefixStart) + streamPtr->dictLimit; streamPtr->end = (const BYTE*)safeBuffer + dictSize; - streamPtr->base = streamPtr->end - endIndex; + streamPtr->prefixStart = streamPtr->end - dictSize; streamPtr->dictLimit = endIndex - (U32)dictSize; streamPtr->lowLimit = endIndex - (U32)dictSize; if (streamPtr->nextToUpdate < streamPtr->dictLimit) @@ -1242,11 +1243,11 @@ int LZ4_compressHC2_limitedOutput_continue (void* LZ4HC_Data, const char* src, c char* LZ4_slideInputBufferHC(void* LZ4HC_Data) { - LZ4_streamHC_t *ctx = (LZ4_streamHC_t*)LZ4HC_Data; - const BYTE *bufferStart = ctx->internal_donotuse.base + ctx->internal_donotuse.lowLimit; + LZ4_streamHC_t* const ctx = (LZ4_streamHC_t*)LZ4HC_Data; + const BYTE* bufferStart = ctx->internal_donotuse.prefixStart - ctx->internal_donotuse.dictLimit + ctx->internal_donotuse.lowLimit; LZ4_resetStreamHC_fast(ctx, ctx->internal_donotuse.compressionLevel); /* avoid const char * -> char * conversion warning :( */ - return (char *)(uptrval)bufferStart; + return (char*)(uptrval)bufferStart; } diff --git a/lib/lz4hc.h b/lib/lz4hc.h index 61e228d..3a5e72c 100644 --- a/lib/lz4hc.h +++ b/lib/lz4hc.h @@ -207,7 +207,7 @@ struct LZ4HC_CCtx_internal 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* prefixStart; /* Indexes 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 */ -- cgit v0.12 From 47681c72ff0cbed4591ed362d6ac960f6be5cddf Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 13 Jul 2022 12:40:11 +0200 Subject: removed ->dictBase from lz4hc state replaced by ->dictStart --- lib/lz4hc.c | 59 +++++++++++++++++++++++++++++++++-------------------------- lib/lz4hc.h | 2 +- 2 files changed, 34 insertions(+), 27 deletions(-) diff --git a/lib/lz4hc.c b/lib/lz4hc.c index 0360f73..6b139fa 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -107,12 +107,12 @@ static void LZ4HC_init_internal (LZ4HC_CCtx_internal* hc4, const BYTE* start) newStartingOffset = 0; } newStartingOffset += 64 KB; - hc4->nextToUpdate = (U32) newStartingOffset; + hc4->nextToUpdate = (U32)newStartingOffset; hc4->prefixStart = start; hc4->end = start; - hc4->dictBase = start - newStartingOffset; - hc4->dictLimit = (U32) newStartingOffset; - hc4->lowLimit = (U32) newStartingOffset; + hc4->dictStart = start; + hc4->dictLimit = (U32)newStartingOffset; + hc4->lowLimit = (U32)newStartingOffset; } @@ -257,7 +257,9 @@ LZ4HC_InsertAndGetWiderMatch ( const U32 ipIndex = (U32)(ip - prefixPtr) + prefixIdx; const int withinStartDistance = (hc4->lowLimit + (LZ4_DISTANCE_MAX + 1) > ipIndex); const U32 lowestMatchIndex = (withinStartDistance) ? hc4->lowLimit : ipIndex - LZ4_DISTANCE_MAX; - const BYTE* const dictBase = hc4->dictBase; + const BYTE* const dictStart = hc4->dictStart; + const U32 dictIdx = hc4->lowLimit; + const BYTE* const dictEnd = dictStart + prefixIdx - dictIdx; int const lookBackLength = (int)(ip-iLowLimit); int nbAttempts = maxNbAttempts; U32 matchChainPos = 0; @@ -294,10 +296,10 @@ LZ4HC_InsertAndGetWiderMatch ( *startpos = ip + back; } } } } else { /* lowestMatchIndex <= matchIndex < dictLimit */ - const BYTE* const matchPtr = dictBase + matchIndex; + const BYTE* const matchPtr = dictStart + (matchIndex - dictIdx); + assert(matchIndex >= dictIdx); if ( likely(matchIndex <= prefixIdx - 4) && (LZ4_read32(matchPtr) == pattern) ) { - const BYTE* const dictStart = dictBase + hc4->lowLimit; int back = 0; const BYTE* vLimit = ip + (prefixIdx - matchIndex); if (vLimit > iHighLimit) vLimit = iHighLimit; @@ -350,10 +352,9 @@ LZ4HC_InsertAndGetWiderMatch ( if ( (repeat == rep_confirmed) && (matchCandidateIdx >= lowestMatchIndex) && LZ4HC_protectDictEnd(prefixIdx, matchCandidateIdx) ) { const int extDict = matchCandidateIdx < prefixIdx; - const BYTE* const matchPtr = (extDict ? dictBase : prefixPtr - prefixIdx) + matchCandidateIdx; + const BYTE* const matchPtr = (extDict ? dictStart - dictIdx : prefixPtr - prefixIdx) + matchCandidateIdx; if (LZ4_read32(matchPtr) == pattern) { /* good candidate */ - const BYTE* const dictStart = dictBase + hc4->lowLimit; - const BYTE* const iLimit = extDict ? dictBase + prefixIdx : iHighLimit; + const BYTE* const iLimit = extDict ? dictEnd : iHighLimit; size_t forwardPatternLength = LZ4HC_countPattern(matchPtr+sizeof(pattern), iLimit, pattern) + sizeof(pattern); if (extDict && matchPtr + forwardPatternLength == iLimit) { U32 const rotatedPattern = LZ4HC_rotatePattern(forwardPatternLength, pattern); @@ -362,9 +363,11 @@ LZ4HC_InsertAndGetWiderMatch ( { const BYTE* const lowestMatchPtr = extDict ? dictStart : prefixPtr; size_t backLength = LZ4HC_reverseCountPattern(matchPtr, lowestMatchPtr, pattern); size_t currentSegmentLength; - if (!extDict && matchPtr - backLength == prefixPtr && hc4->lowLimit < prefixIdx) { + if (!extDict + && matchPtr - backLength == prefixPtr + && dictIdx < prefixIdx) { U32 const rotatedPattern = LZ4HC_rotatePattern((U32)(-(int)backLength), pattern); - backLength += LZ4HC_reverseCountPattern(dictBase + prefixIdx, dictStart, rotatedPattern); + backLength += LZ4HC_reverseCountPattern(dictEnd, dictStart, rotatedPattern); } /* Limit backLength not go further than lowestMatchIndex */ backLength = matchCandidateIdx - MAX(matchCandidateIdx - (U32)backLength, lowestMatchIndex); @@ -443,13 +446,13 @@ LZ4HC_InsertAndGetWiderMatch ( return longest; } -LZ4_FORCE_INLINE -int LZ4HC_InsertAndFindBestMatch(LZ4HC_CCtx_internal* const hc4, /* Index table will be updated */ - const BYTE* const ip, const BYTE* const iLimit, - const BYTE** matchpos, - const int maxNbAttempts, - const int patternAnalysis, - const dictCtx_directive dict) +LZ4_FORCE_INLINE int +LZ4HC_InsertAndFindBestMatch(LZ4HC_CCtx_internal* const hc4, /* Index table will be updated */ + const BYTE* const ip, const BYTE* const iLimit, + const BYTE** matchpos, + const int maxNbAttempts, + const int patternAnalysis, + const dictCtx_directive dict) { const BYTE* uselessPtr = ip; /* note : LZ4HC_InsertAndGetWiderMatch() is able to modify the starting position of a match (*startpos), @@ -885,7 +888,7 @@ LZ4HC_compress_generic_dictCtx ( limitedOutput_directive limit ) { - const size_t position = (size_t)(ctx->end - ctx->prefixStart) + ctx->dictLimit - ctx->lowLimit; + const size_t position = (size_t)(ctx->end - ctx->prefixStart) + (ctx->dictLimit - ctx->lowLimit); assert(ctx->dictCtx != NULL); if (position >= 64 KB) { ctx->dictCtx = NULL; @@ -1093,7 +1096,7 @@ static void LZ4HC_setExternalDict(LZ4HC_CCtx_internal* ctxPtr, const BYTE* newBl /* Only one memory segment for extDict, so any previous extDict is lost at this stage */ ctxPtr->lowLimit = ctxPtr->dictLimit; - ctxPtr->dictBase = ctxPtr->prefixStart - ctxPtr->dictLimit; + ctxPtr->dictStart = ctxPtr->prefixStart; ctxPtr->dictLimit += (U32)(ctxPtr->end - ctxPtr->prefixStart); ctxPtr->prefixStart = newBlock; ctxPtr->end = newBlock; @@ -1129,13 +1132,16 @@ LZ4_compressHC_continue_generic (LZ4_streamHC_t* LZ4_streamHCPtr, /* Check overlapping input/dictionary space */ { const BYTE* sourceEnd = (const BYTE*) src + *srcSizePtr; - const BYTE* const dictBegin = ctxPtr->dictBase + ctxPtr->lowLimit; - const BYTE* const dictEnd = ctxPtr->dictBase + ctxPtr->dictLimit; + const BYTE* const dictBegin = ctxPtr->dictStart; + const BYTE* const dictEnd = ctxPtr->dictStart + (ctxPtr->dictLimit - ctxPtr->lowLimit); if ((sourceEnd > dictBegin) && ((const BYTE*)src < dictEnd)) { if (sourceEnd > dictEnd) sourceEnd = dictEnd; - ctxPtr->lowLimit = (U32)(sourceEnd - ctxPtr->dictBase); - if (ctxPtr->dictLimit - ctxPtr->lowLimit < 4) ctxPtr->lowLimit = ctxPtr->dictLimit; - } } + ctxPtr->lowLimit += (U32)(sourceEnd - ctxPtr->dictStart); + ctxPtr->dictStart += (U32)(sourceEnd - ctxPtr->dictStart); + if (ctxPtr->dictLimit - ctxPtr->lowLimit < 4) { + ctxPtr->lowLimit = ctxPtr->dictLimit; + ctxPtr->dictStart = ctxPtr->prefixStart; + } } } return LZ4HC_compress_generic (ctxPtr, src, dst, srcSizePtr, dstCapacity, ctxPtr->compressionLevel, limit); } @@ -1177,6 +1183,7 @@ int LZ4_saveDictHC (LZ4_streamHC_t* LZ4_streamHCPtr, char* safeBuffer, int dictS streamPtr->prefixStart = streamPtr->end - dictSize; streamPtr->dictLimit = endIndex - (U32)dictSize; streamPtr->lowLimit = endIndex - (U32)dictSize; + streamPtr->dictStart = streamPtr->prefixStart; if (streamPtr->nextToUpdate < streamPtr->dictLimit) streamPtr->nextToUpdate = streamPtr->dictLimit; } diff --git a/lib/lz4hc.h b/lib/lz4hc.h index 3a5e72c..d2e5193 100644 --- a/lib/lz4hc.h +++ b/lib/lz4hc.h @@ -208,7 +208,7 @@ struct LZ4HC_CCtx_internal LZ4_u16 chainTable[LZ4HC_MAXD]; const LZ4_byte* end; /* next block here to continue on current prefix */ const LZ4_byte* prefixStart; /* Indexes relative to this position */ - const LZ4_byte* dictBase; /* alternate base for extDict */ + const LZ4_byte* dictStart; /* alternate reference 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 */ -- cgit v0.12 From db836b5519ab1498504dc27c989b71b87d3d4c4d Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 13 Jul 2022 15:41:11 +0200 Subject: declare experimental prototype for LZ4F custom Memory manager --- lib/lz4frame.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/lib/lz4frame.h b/lib/lz4frame.h index 2c5a559..79522b6 100644 --- a/lib/lz4frame.h +++ b/lib/lz4frame.h @@ -545,6 +545,29 @@ typedef enum { LZ4F_LIST_ERRORS(LZ4F_GENERATE_ENUM) LZ4FLIB_STATIC_API LZ4F_errorCodes LZ4F_getErrorCode(size_t functionResult); + +/*! Custom memory allocation : + * These prototypes make it possible to pass your own allocation/free functions. + * ZSTD_customMem is provided at creation time, using ZSTD_create*_advanced() variants listed below. + * All allocation/free operations will be completed using these custom variants instead of regular ones. + */ +typedef void* (*LZ4F_allocFunction) (void* opaqueState, size_t size); +typedef void* (*LZ4F_callocFunction) (void* opaqueState, size_t size); +typedef void (*LZ4F_freeFunction) (void* opaqueState, void* address); +typedef struct { + LZ4F_allocFunction customAlloc; + LZ4F_callocFunction customCalloc; + LZ4F_freeFunction customFree; + void* opaqueState; +} LZ4F_customMem; +static +#ifdef __GNUC__ +__attribute__((__unused__)) +#endif +LZ4F_customMem const LZ4F_defaultCMem = { NULL, NULL, NULL, NULL }; /**< this constant defers to stdlib's functions */ + +LZ4FLIB_STATIC_API LZ4F_cctx* LZ4F_createCompressionContext_advanced(LZ4F_customMem customMem); + LZ4FLIB_STATIC_API size_t LZ4F_getBlockSize(unsigned); /*! LZ4F_uncompressedUpdate() : -- cgit v0.12 From 7deae4bd22c0d3d264a5928058668ff177cc7323 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 13 Jul 2022 15:55:56 +0200 Subject: minor : proper interface for LZ4F_getBlockSize() and proper documentation. Also : updated manual --- doc/lz4_manual.html | 51 +++++++++++++----------------------------------- doc/lz4frame_manual.html | 16 +++++++++++---- lib/lz4frame.c | 2 +- lib/lz4frame.h | 6 +++++- 4 files changed, 32 insertions(+), 43 deletions(-) diff --git a/doc/lz4_manual.html b/doc/lz4_manual.html index 037cfc0..5461ab9 100644 --- a/doc/lz4_manual.html +++ b/doc/lz4_manual.html @@ -50,9 +50,9 @@ Version
-int LZ4_versionNumber (void); /**< library version number; useful to check dll version */ +int LZ4_versionNumber (void); /**< library version number; useful to check dll version; requires v1.3.0+ */
-const char* LZ4_versionString (void); /**< library version string; useful to check dll version */ +-const char* LZ4_versionString (void); /**< library version string; useful to check dll version; requires v1.7.5+ */
Tuning parameter
@@ -452,28 +452,9 @@ int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream); Accessing members will expose user code to API and/or ABI break in future versions of the library.typedef struct { - const LZ4_byte* externalDict; - size_t extDictSize; - const LZ4_byte* prefixEnd; - size_t prefixSize; -} LZ4_streamDecode_t_internal; -
-#define LZ4_STREAMSIZE ((1UL << LZ4_MEMORY_USAGE) + 32) /* 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; -}; /* 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 future versions. - +
Never ever use below internal definitions directly ! + These definitions are not API/ABI safe, and may change in future versions. + If you need static allocation, declare or allocate an LZ4_stream_t object.
LZ4_stream_t* LZ4_initStream (void* buffer, size_t size); @@ -489,21 +470,17 @@ union LZ4_stream_u { 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 -
-#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]; - 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() 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 ! - +
typedef struct { + const LZ4_byte* externalDict; + const LZ4_byte* prefixEnd; + size_t extDictSize; + size_t prefixSize; +} LZ4_streamDecode_t_internal; +Never ever use below internal definitions directly ! + These definitions are not API/ABI safe, and may change in future versions. + If you need static allocation, declare or allocate an LZ4_streamDecode_t object.
Obsolete Functions
diff --git a/doc/lz4frame_manual.html b/doc/lz4frame_manual.html index 0ae5150..196881e 100644 --- a/doc/lz4frame_manual.html +++ b/doc/lz4frame_manual.html @@ -135,13 +135,16 @@LZ4F_errorCode_t LZ4F_createCompressionContext(LZ4F_cctx** cctxPtr, unsigned version); LZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_cctx* cctx); -The first thing to do is to create a compressionContext object, which will be used in all compression operations. +
The first thing to do is to create a compressionContext object, + which will keep track of operation state during streaming compression. This is achieved using LZ4F_createCompressionContext(), which takes as argument a version. The version provided MUST be LZ4F_VERSION. It is intended to track potential version mismatch, notably when using DLL. The function will provide a pointer to a fully allocated LZ4F_cctx object. - If @return != zero, there was an error during context creation. - Object can be released using LZ4F_freeCompressionContext(); - Note: LZ4F_freeCompressionContext() works with NULL pointers (do nothing). + If @return != zero, there context creation failed. + Once all streaming compression jobs are completed, + the state object can be released using LZ4F_freeCompressionContext(). + Note1 : LZ4F_freeCompressionContext() is always successful. Its return value can be ignored. + Note2 : LZ4F_freeCompressionContext() works fine with NULL input pointers (do nothing).
typedef enum { LZ4F_LIST_ERRORS(LZ4F_GENERATE_ENUM) _LZ4F_dummy_error_enum_for_c89_never_used } LZ4F_errorCodes;
LZ4FLIB_STATIC_API size_t LZ4F_getBlockSize(LZ4F_blockSizeID_t blockSizeID); +Return, in scalar format (size_t), + the maximum block size associated with blockSizeID. +
LZ4FLIB_STATIC_API size_t LZ4F_uncompressedUpdate(LZ4F_cctx* cctx, void* dstBuffer, size_t dstCapacity, const void* srcBuffer, size_t srcSize, diff --git a/lib/lz4frame.c b/lib/lz4frame.c index aec2728..2b20a87 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -285,7 +285,7 @@ unsigned LZ4F_getVersion(void) { return LZ4F_VERSION; } int LZ4F_compressionLevel_max(void) { return LZ4HC_CLEVEL_MAX; } -size_t LZ4F_getBlockSize(unsigned blockSizeID) +size_t LZ4F_getBlockSize(LZ4F_blockSizeID_t blockSizeID) { static const size_t blockSizes[4] = { 64 KB, 256 KB, 1 MB, 4 MB }; diff --git a/lib/lz4frame.h b/lib/lz4frame.h index 2c5a559..1de09d8 100644 --- a/lib/lz4frame.h +++ b/lib/lz4frame.h @@ -545,7 +545,11 @@ 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); +/*! LZ4F_getBlockSize() : + * Return, in scalar format (size_t), + * the maximum block size associated with blockSizeID. +**/ +LZ4FLIB_STATIC_API size_t LZ4F_getBlockSize(LZ4F_blockSizeID_t blockSizeID); /*! LZ4F_uncompressedUpdate() : * LZ4F_uncompressedUpdate() can be called repetitively to add as much data uncompressed data as necessary. -- cgit v0.12 From 832b444266053ab86d32b8a2f8b25a3f8abff703 Mon Sep 17 00:00:00 2001 From: Yann Collet+Date: Wed, 13 Jul 2022 16:18:22 +0200 Subject: fix stricter enum type requirements for C++ --- lib/lz4frame.c | 8 ++++---- tests/frametest.c | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/lz4frame.c b/lib/lz4frame.c index 2b20a87..5373083 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -292,9 +292,9 @@ size_t LZ4F_getBlockSize(LZ4F_blockSizeID_t blockSizeID) if (blockSizeID == 0) blockSizeID = LZ4F_BLOCKSIZEID_DEFAULT; if (blockSizeID < LZ4F_max64KB || blockSizeID > LZ4F_max4MB) RETURN_ERROR(maxBlockSize_invalid); - blockSizeID -= LZ4F_max64KB; - return blockSizes[blockSizeID]; -} + { int const blockSizeIdx = (int)blockSizeID - (int)LZ4F_max64KB; + return blockSizes[blockSizeIdx]; +} } /*-************************************ * Private functions @@ -1291,7 +1291,7 @@ static size_t LZ4F_decodeHeader(LZ4F_dctx* dctx, const void* src, size_t srcSize dctx->frameInfo.blockChecksumFlag = (LZ4F_blockChecksum_t)blockChecksumFlag; dctx->frameInfo.contentChecksumFlag = (LZ4F_contentChecksum_t)contentChecksumFlag; dctx->frameInfo.blockSizeID = (LZ4F_blockSizeID_t)blockSizeID; - dctx->maxBlockSize = LZ4F_getBlockSize(blockSizeID); + dctx->maxBlockSize = LZ4F_getBlockSize((LZ4F_blockSizeID_t)blockSizeID); if (contentSizeFlag) dctx->frameRemainingSize = dctx->frameInfo.contentSize = LZ4F_readLE64(srcPtr+6); diff --git a/tests/frametest.c b/tests/frametest.c index a496c3c..58eac38 100644 --- a/tests/frametest.c +++ b/tests/frametest.c @@ -686,20 +686,20 @@ int basicTests(U32 seed, double compressibility) { size_t result; unsigned blockSizeID; for (blockSizeID = 4; blockSizeID < 8; ++blockSizeID) { - result = LZ4F_getBlockSize(blockSizeID); + result = LZ4F_getBlockSize((LZ4F_blockSizeID_t)blockSizeID); CHECK(result); DISPLAYLEVEL(3, "Returned block size of %u bytes for blockID %u \n", (unsigned)result, blockSizeID); } /* Test an invalid input that's too large */ - result = LZ4F_getBlockSize(8); + result = LZ4F_getBlockSize((LZ4F_blockSizeID_t)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); + result = LZ4F_getBlockSize((LZ4F_blockSizeID_t)3); if(!LZ4F_isError(result) || LZ4F_getErrorCode(result) != LZ4F_ERROR_maxBlockSize_invalid) goto _output_error; -- cgit v0.12 From 331f7e29f11801528219e5dbbfac9d421914ddcb Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 13 Jul 2022 15:05:40 +0200 Subject: restore gcc-11 tests for -m32 and -mx32 --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f8c49d4..f15a954 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -40,7 +40,7 @@ jobs: # gcc { pkgs: '', cc: gcc, cxx: g++, x32: 'true', x86: 'true', cxxtest: 'true', os: ubuntu-latest, }, - { pkgs: 'gcc-11 g++-11 lib32gcc-11-dev libx32gcc-11-dev', cc: gcc-11, cxx: g++-11, x32: 'fail', x86: 'fail', cxxtest: 'true', os: ubuntu-20.04, }, + { pkgs: 'gcc-11 g++-11 lib32gcc-11-dev libx32gcc-11-dev', cc: gcc-11, cxx: g++-11, x32: 'true', x86: 'true', cxxtest: 'true', os: ubuntu-20.04, }, { pkgs: 'gcc-10 lib32gcc-10-dev libx32gcc-10-dev', cc: gcc-10, cxx: g++-10, x32: 'true', x86: 'true', cxxtest: 'true', os: ubuntu-20.04, }, { pkgs: 'gcc-9 lib32gcc-9-dev libx32gcc-9-dev', cc: gcc-9, cxx: g++-9, x32: 'true', x86: 'true', cxxtest: 'true', os: ubuntu-20.04, }, { pkgs: 'gcc-8 g++-8 lib32gcc-8-dev libx32gcc-8-dev', cc: gcc-8, cxx: g++-8, x32: 'true', x86: 'true', cxxtest: 'true', os: ubuntu-20.04, }, -- cgit v0.12 From 270529e80e12f0aa1778d64047472d049129c15d Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 13 Jul 2022 18:37:53 +0200 Subject: implemented first custom memory manager interface for compression context only for the time being, using LZ4F_createCompressionContext_advanced(). Added basic test in frametest.c --- lib/lz4frame.c | 172 +++++++++++++++++++++++++++++++++++------------------- lib/lz4frame.h | 48 ++++++++------- tests/frametest.c | 60 +++++++++++++++++-- 3 files changed, 195 insertions(+), 85 deletions(-) diff --git a/lib/lz4frame.c b/lib/lz4frame.c index aec2728..8db6b46 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -45,7 +45,7 @@ * Compiler Options **************************************/ #ifdef _MSC_VER /* Visual Studio */ -# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ +# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ #endif @@ -63,6 +63,19 @@ /*-************************************ +* Library declarations +**************************************/ +#define LZ4F_STATIC_LINKING_ONLY +#include "lz4frame.h" +#define LZ4_STATIC_LINKING_ONLY +#include "lz4.h" +#define LZ4_HC_STATIC_LINKING_ONLY +#include "lz4hc.h" +#define XXH_STATIC_LINKING_ONLY +#include "xxhash.h" + + +/*-************************************ * Memory routines **************************************/ /* @@ -70,7 +83,13 @@ * malloc(), calloc() and free() * towards another library or solution of their choice * by modifying below section. - */ +**/ + +#include /* memset, memcpy, memmove */ +#ifndef LZ4_SRC_INCLUDED /* avoid redefinition when sources are coalesced */ +# define MEM_INIT(p,v,s) memset((p),(v),(s)) +#endif + #ifndef LZ4_SRC_INCLUDED /* avoid redefinition when sources are coalesced */ # include /* malloc, calloc, free */ # define ALLOC(s) malloc(s) @@ -78,23 +97,42 @@ # define FREEMEM(p) free(p) #endif -#include /* memset, memcpy, memmove */ -#ifndef LZ4_SRC_INCLUDED /* avoid redefinition when sources are coalesced */ -# define MEM_INIT(p,v,s) memset((p),(v),(s)) -#endif +static void* LZ4F_calloc(size_t s, LZ4F_CustomMem cmem) +{ + /* custom calloc defined : use it */ + if (cmem.customCalloc != NULL) { + return cmem.customCalloc(cmem.opaqueState, s); + } + /* nothing defined : use default 's calloc() */ + if (cmem.customAlloc == NULL) { + return ALLOC_AND_ZERO(s); + } + /* only custom alloc defined : use it, and combine it with memset() */ + { void* const p = cmem.customAlloc(cmem.opaqueState, s); + if (p != NULL) MEM_INIT(p, 0, s); + return p; +} } +static void* LZ4F_malloc(size_t s, LZ4F_CustomMem cmem) +{ + /* custom malloc defined : use it */ + if (cmem.customAlloc != NULL) { + return cmem.customAlloc(cmem.opaqueState, s); + } + /* nothing defined : use default 's malloc() */ + return ALLOC(s); +} -/*-************************************ -* Library declarations -**************************************/ -#define LZ4F_STATIC_LINKING_ONLY -#include "lz4frame.h" -#define LZ4_STATIC_LINKING_ONLY -#include "lz4.h" -#define LZ4_HC_STATIC_LINKING_ONLY -#include "lz4hc.h" -#define XXH_STATIC_LINKING_ONLY -#include "xxhash.h" +static void LZ4F_free(void* p, LZ4F_CustomMem cmem) +{ + /* custom malloc defined : use it */ + if (cmem.customFree != NULL) { + cmem.customFree(cmem.opaqueState, p); + return; + } + /* nothing defined : use default 's free() */ + FREEMEM(p); +} /*-************************************ @@ -225,6 +263,7 @@ typedef enum { LZ4B_COMPRESSED, LZ4B_UNCOMPRESSED} LZ4F_blockCompression_t; typedef struct LZ4F_cctx_s { + LZ4F_CustomMem cmem; LZ4F_preferences_t prefs; U32 version; U32 cStage; @@ -441,27 +480,26 @@ size_t LZ4F_compressFrame(void* dstBuffer, size_t dstCapacity, { size_t result; #if (LZ4F_HEAPMODE) - LZ4F_cctx_t *cctxPtr; + LZ4F_cctx_t* cctxPtr; result = LZ4F_createCompressionContext(&cctxPtr, LZ4F_VERSION); FORWARD_IF_ERROR(result); #else LZ4F_cctx_t cctx; LZ4_stream_t lz4ctx; - LZ4F_cctx_t *cctxPtr = &cctx; + LZ4F_cctx_t* const cctxPtr = &cctx; - DEBUGLOG(4, "LZ4F_compressFrame"); MEM_INIT(&cctx, 0, sizeof(cctx)); cctx.version = LZ4F_VERSION; cctx.maxBufferSize = 5 MB; /* mess with real buffer size to prevent dynamic allocation; works only because autoflush==1 & stableSrc==1 */ - if (preferencesPtr == NULL || - preferencesPtr->compressionLevel < LZ4HC_CLEVEL_MIN) - { + if ( preferencesPtr == NULL + || preferencesPtr->compressionLevel < LZ4HC_CLEVEL_MIN ) { LZ4_initStream(&lz4ctx, sizeof(lz4ctx)); cctxPtr->lz4CtxPtr = &lz4ctx; cctxPtr->lz4CtxAlloc = 1; cctxPtr->lz4CtxState = 1; } #endif + DEBUGLOG(4, "LZ4F_compressFrame"); result = LZ4F_compressFrame_usingCDict(cctxPtr, dstBuffer, dstCapacity, srcBuffer, srcSize, @@ -470,10 +508,9 @@ size_t LZ4F_compressFrame(void* dstBuffer, size_t dstCapacity, #if (LZ4F_HEAPMODE) LZ4F_freeCompressionContext(cctxPtr); #else - if (preferencesPtr != NULL && - preferencesPtr->compressionLevel >= LZ4HC_CLEVEL_MIN) - { - FREEMEM(cctxPtr->lz4CtxPtr); + if ( preferencesPtr != NULL + && preferencesPtr->compressionLevel >= LZ4HC_CLEVEL_MIN ) { + LZ4F_free(cctxPtr->lz4CtxPtr, cctxPtr->cmem); } #endif return result; @@ -499,14 +536,14 @@ struct LZ4F_CDict_s { LZ4F_CDict* LZ4F_createCDict(const void* dictBuffer, size_t dictSize) { const char* dictStart = (const char*)dictBuffer; - LZ4F_CDict* const cdict = (LZ4F_CDict*) ALLOC(sizeof(*cdict)); + LZ4F_CDict* const cdict = (LZ4F_CDict*)LZ4F_malloc(sizeof(*cdict), LZ4F_defaultCMem); DEBUGLOG(4, "LZ4F_createCDict"); if (!cdict) return NULL; if (dictSize > 64 KB) { dictStart += dictSize - 64 KB; dictSize = 64 KB; } - cdict->dictContent = ALLOC(dictSize); + cdict->dictContent = LZ4F_malloc(dictSize, LZ4F_defaultCMem); cdict->fastCtx = LZ4_createStream(); cdict->HCCtx = LZ4_createStreamHC(); if (!cdict->dictContent || !cdict->fastCtx || !cdict->HCCtx) { @@ -523,10 +560,10 @@ LZ4F_CDict* LZ4F_createCDict(const void* dictBuffer, size_t dictSize) void LZ4F_freeCDict(LZ4F_CDict* cdict) { if (cdict==NULL) return; /* support free on NULL */ - FREEMEM(cdict->dictContent); + LZ4F_free(cdict->dictContent, LZ4F_defaultCMem); LZ4_freeStream(cdict->fastCtx); LZ4_freeStreamHC(cdict->HCCtx); - FREEMEM(cdict); + LZ4F_free(cdict, LZ4F_defaultCMem); } @@ -534,6 +571,20 @@ void LZ4F_freeCDict(LZ4F_CDict* cdict) * Advanced compression functions ***********************************/ +LZ4F_cctx* +LZ4F_createCompressionContext_advanced(LZ4F_CustomMem customMem, unsigned version) +{ + LZ4F_cctx* const cctxPtr = + (LZ4F_cctx*)LZ4F_calloc(sizeof(LZ4F_cctx), customMem); + if (cctxPtr==NULL) return NULL; + + cctxPtr->cmem = customMem; + cctxPtr->version = version; + cctxPtr->cStage = 0; /* Uninitialized. Next stage : init cctx */ + + return cctxPtr; +} + /*! LZ4F_createCompressionContext() : * The first thing to do is to create a compressionContext object, which will be used in all compression operations. * This is achieved using LZ4F_createCompressionContext(), which takes as argument a version and an LZ4F_preferences_t structure. @@ -541,17 +592,16 @@ void LZ4F_freeCDict(LZ4F_CDict* cdict) * The function will provide a pointer to an allocated LZ4F_compressionContext_t object. * If the result LZ4F_errorCode_t is not OK_NoError, there was an error during context creation. * Object can release its memory using LZ4F_freeCompressionContext(); - */ -LZ4F_errorCode_t LZ4F_createCompressionContext(LZ4F_cctx** 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)); - RETURN_ERROR_IF(cctxPtr==NULL, allocation_failed); - - cctxPtr->version = version; - cctxPtr->cStage = 0; /* Uninitialized. Next stage : init cctx */ - - *LZ4F_compressionContextPtr = cctxPtr; + assert(LZ4F_compressionContextPtr != NULL); /* considered a violation of narrow contract */ + /* in case it nonetheless happen in production */ + RETURN_ERROR_IF(LZ4F_compressionContextPtr == NULL, parameter_null); + *LZ4F_compressionContextPtr = LZ4F_createCompressionContext_advanced(LZ4F_defaultCMem, version); + RETURN_ERROR_IF(*LZ4F_compressionContextPtr==NULL, allocation_failed); return LZ4F_OK_NoError; } @@ -559,11 +609,10 @@ LZ4F_errorCode_t LZ4F_createCompressionContext(LZ4F_cctx** LZ4F_compressionConte LZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_cctx* cctxPtr) { if (cctxPtr != NULL) { /* support free on NULL */ - FREEMEM(cctxPtr->lz4CtxPtr); /* note: LZ4_streamHC_t and LZ4_stream_t are simple POD types */ - FREEMEM(cctxPtr->tmpBuff); - FREEMEM(cctxPtr); + LZ4F_free(cctxPtr->lz4CtxPtr, cctxPtr->cmem); /* note: LZ4_streamHC_t and LZ4_stream_t are simple POD types */ + LZ4F_free(cctxPtr->tmpBuff, cctxPtr->cmem); + LZ4F_free(cctxPtr, cctxPtr->cmem); } - return LZ4F_OK_NoError; } @@ -633,11 +682,17 @@ size_t LZ4F_compressBegin_usingCDict(LZ4F_cctx* cctxPtr, int allocatedSize = ctxTypeID_to_size(cctxPtr->lz4CtxAlloc); if (allocatedSize < requiredSize) { /* not enough space allocated */ - FREEMEM(cctxPtr->lz4CtxPtr); + LZ4F_free(cctxPtr->lz4CtxPtr, cctxPtr->cmem); if (cctxPtr->prefs.compressionLevel < LZ4HC_CLEVEL_MIN) { - cctxPtr->lz4CtxPtr = LZ4_createStream(); + /* must take ownership of memory allocation, + * in order to respect custom allocator contract */ + cctxPtr->lz4CtxPtr = LZ4F_malloc(sizeof(LZ4_stream_t), cctxPtr->cmem); + if (cctxPtr->lz4CtxPtr) + LZ4_initStream(cctxPtr->lz4CtxPtr, sizeof(LZ4_stream_t)); } else { - cctxPtr->lz4CtxPtr = LZ4_createStreamHC(); + cctxPtr->lz4CtxPtr = LZ4F_malloc(sizeof(LZ4_streamHC_t), cctxPtr->cmem); + if (cctxPtr->lz4CtxPtr) + LZ4_initStreamHC(cctxPtr->lz4CtxPtr, sizeof(LZ4_streamHC_t)); } RETURN_ERROR_IF(cctxPtr->lz4CtxPtr == NULL, allocation_failed); cctxPtr->lz4CtxAlloc = ctxTypeID; @@ -652,8 +707,7 @@ size_t LZ4F_compressBegin_usingCDict(LZ4F_cctx* cctxPtr, LZ4_setCompressionLevel((LZ4_streamHC_t*)cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel); } cctxPtr->lz4CtxState = ctxTypeID; - } - } + } } /* Buffer Management */ if (cctxPtr->prefs.frameInfo.blockSizeID == 0) @@ -666,8 +720,8 @@ size_t LZ4F_compressBegin_usingCDict(LZ4F_cctx* cctxPtr, if (cctxPtr->maxBufferSize < requiredBuffSize) { cctxPtr->maxBufferSize = 0; - FREEMEM(cctxPtr->tmpBuff); - cctxPtr->tmpBuff = (BYTE*)ALLOC_AND_ZERO(requiredBuffSize); + LZ4F_free(cctxPtr->tmpBuff, cctxPtr->cmem); + cctxPtr->tmpBuff = (BYTE*)LZ4F_calloc(requiredBuffSize, cctxPtr->cmem); RETURN_ERROR_IF(cctxPtr->tmpBuff == NULL, allocation_failed); cctxPtr->maxBufferSize = requiredBuffSize; } } @@ -1168,7 +1222,7 @@ 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)); + LZ4F_dctx* const dctx = (LZ4F_dctx*)LZ4F_calloc(sizeof(LZ4F_dctx), LZ4F_defaultCMem); if (dctx == NULL) { /* failed allocation */ *LZ4F_decompressionContextPtr = NULL; RETURN_ERROR(allocation_failed); @@ -1184,9 +1238,9 @@ LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_dctx* dctx) LZ4F_errorCode_t result = LZ4F_OK_NoError; if (dctx != NULL) { /* can accept NULL input, like free() */ result = (LZ4F_errorCode_t)dctx->dStage; - FREEMEM(dctx->tmpIn); - FREEMEM(dctx->tmpOutBuffer); - FREEMEM(dctx); + LZ4F_free(dctx->tmpIn, LZ4F_defaultCMem); + LZ4F_free(dctx->tmpOutBuffer, LZ4F_defaultCMem); + LZ4F_free(dctx, LZ4F_defaultCMem); } return result; } @@ -1550,11 +1604,11 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, + ((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 + BFSize /* block checksum */); + LZ4F_free(dctx->tmpIn, LZ4F_defaultCMem); + dctx->tmpIn = (BYTE*)LZ4F_malloc(dctx->maxBlockSize + BFSize /* block checksum */, LZ4F_defaultCMem); RETURN_ERROR_IF(dctx->tmpIn == NULL, allocation_failed); - FREEMEM(dctx->tmpOutBuffer); - dctx->tmpOutBuffer= (BYTE*)ALLOC(bufferNeeded); + LZ4F_free(dctx->tmpOutBuffer, LZ4F_defaultCMem); + dctx->tmpOutBuffer= (BYTE*)LZ4F_malloc(bufferNeeded, LZ4F_defaultCMem); RETURN_ERROR_IF(dctx->tmpOutBuffer== NULL, allocation_failed); dctx->maxBufferSize = bufferNeeded; } } diff --git a/lib/lz4frame.h b/lib/lz4frame.h index 79522b6..2c87564 100644 --- a/lib/lz4frame.h +++ b/lib/lz4frame.h @@ -243,17 +243,20 @@ typedef struct { LZ4FLIB_API unsigned LZ4F_getVersion(void); /*! LZ4F_createCompressionContext() : - * The first thing to do is to create a compressionContext object, - * which will keep track of operation state during streaming compression. - * This is achieved using LZ4F_createCompressionContext(), which takes as argument a version. - * The version provided MUST be LZ4F_VERSION. It is intended to track potential version mismatch, notably when using DLL. - * The function will provide a pointer to a fully allocated LZ4F_cctx object. - * If @return != zero, there context creation failed. - * Once all streaming compression jobs are completed, - * the state object can be released using LZ4F_freeCompressionContext(). - * Note1 : LZ4F_freeCompressionContext() is always successful. Its return value can be ignored. - * Note2 : LZ4F_freeCompressionContext() works fine with NULL input pointers (do nothing). - */ + * The first thing to do is to create a compressionContext object, + * which will keep track of operation state during streaming compression. + * This is achieved using LZ4F_createCompressionContext(), which takes as argument a version, + * and a pointer to LZ4F_cctx*, to write the resulting pointer into. + * @version provided MUST be LZ4F_VERSION. It is intended to track potential version mismatch, notably when using DLL. + * The function provides a pointer to a fully allocated LZ4F_cctx object. + * @cctxPtr MUST be != NULL. + * If @return != zero, context creation failed. + * A created compression context can be employed multiple times for consecutive streaming operations. + * Once all streaming compression jobs are completed, + * the state object can be released using LZ4F_freeCompressionContext(). + * Note1 : LZ4F_freeCompressionContext() is always successful. Its return value can be ignored. + * Note2 : LZ4F_freeCompressionContext() works fine with NULL input pointers (do nothing). +**/ LZ4FLIB_API LZ4F_errorCode_t LZ4F_createCompressionContext(LZ4F_cctx** cctxPtr, unsigned version); LZ4FLIB_API LZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_cctx* cctx); @@ -535,6 +538,7 @@ extern "C" { ITEM(ERROR_contentChecksum_invalid) \ ITEM(ERROR_frameDecoding_alreadyStarted) \ ITEM(ERROR_compressionState_uninitialized) \ + ITEM(ERROR_parameter_null) \ ITEM(ERROR_maxCode) #define LZ4F_GENERATE_ENUM(ENUM) LZ4F_##ENUM, @@ -547,26 +551,26 @@ LZ4FLIB_STATIC_API LZ4F_errorCodes LZ4F_getErrorCode(size_t functionResult); /*! Custom memory allocation : - * These prototypes make it possible to pass your own allocation/free functions. - * ZSTD_customMem is provided at creation time, using ZSTD_create*_advanced() variants listed below. + * These prototypes make it possible to pass custom allocation/free functions. + * LZ4F_customMem is provided at state creation time, using LZ4F_createCompressionContext_advanced() listed below. * All allocation/free operations will be completed using these custom variants instead of regular ones. */ -typedef void* (*LZ4F_allocFunction) (void* opaqueState, size_t size); -typedef void* (*LZ4F_callocFunction) (void* opaqueState, size_t size); -typedef void (*LZ4F_freeFunction) (void* opaqueState, void* address); +typedef void* (*LZ4F_AllocFunction) (void* opaqueState, size_t size); +typedef void* (*LZ4F_CallocFunction) (void* opaqueState, size_t size); +typedef void (*LZ4F_FreeFunction) (void* opaqueState, void* address); typedef struct { - LZ4F_allocFunction customAlloc; - LZ4F_callocFunction customCalloc; - LZ4F_freeFunction customFree; + LZ4F_AllocFunction customAlloc; + LZ4F_CallocFunction customCalloc; /* optional; when not defined, uses customAlloc + memset */ + LZ4F_FreeFunction customFree; void* opaqueState; -} LZ4F_customMem; +} LZ4F_CustomMem; static #ifdef __GNUC__ __attribute__((__unused__)) #endif -LZ4F_customMem const LZ4F_defaultCMem = { NULL, NULL, NULL, NULL }; /**< this constant defers to stdlib's functions */ +LZ4F_CustomMem const LZ4F_defaultCMem = { NULL, NULL, NULL, NULL }; /**< this constant defers to stdlib's functions */ -LZ4FLIB_STATIC_API LZ4F_cctx* LZ4F_createCompressionContext_advanced(LZ4F_customMem customMem); +LZ4FLIB_STATIC_API LZ4F_cctx* LZ4F_createCompressionContext_advanced(LZ4F_CustomMem customMem, unsigned version); LZ4FLIB_STATIC_API size_t LZ4F_getBlockSize(unsigned); diff --git a/tests/frametest.c b/tests/frametest.c index a496c3c..b646f4b 100644 --- a/tests/frametest.c +++ b/tests/frametest.c @@ -104,12 +104,63 @@ static U32 use_pause = 0; #define MIN(a,b) ( (a) < (b) ? (a) : (b) ) #define MAX(a,b) ( (a) > (b) ? (a) : (b) ) +typedef struct { + int nbAllocs; +} Test_alloc_state; +static Test_alloc_state g_testAllocState = { 0 }; + +static void* dummy_malloc(void* state, size_t s) +{ + Test_alloc_state* const t = (Test_alloc_state*)state; + void* const p = malloc(s); + if (p==NULL) return NULL; + assert(t != NULL); + t->nbAllocs += 1; + DISPLAYLEVEL(6, "Allocating %zu bytes at address %p \n", s, p); + DISPLAYLEVEL(5, "nb allocated memory segments : %i \n", t->nbAllocs); + return p; +} + +static void* dummy_calloc(void* state, size_t s) +{ + Test_alloc_state* const t = (Test_alloc_state*)state; + void* const p = calloc(1, s); + if (p==NULL) return NULL; + assert(t != NULL); + t->nbAllocs += 1; + DISPLAYLEVEL(6, "Allocating and zeroing %zu bytes at address %p \n", s, p); + DISPLAYLEVEL(5, "nb allocated memory segments : %i \n", t->nbAllocs); + return p; +} + +static void dummy_free(void* state, void* p) +{ + Test_alloc_state* const t = (Test_alloc_state*)state; + if (p==NULL) { + DISPLAYLEVEL(5, "free() on NULL \n"); + return; + } + DISPLAYLEVEL(6, "freeing memory at address %p \n", p); + free(p); + assert(t != NULL); + DISPLAYLEVEL(5, "nb of allocated memory segments before this free : %i \n", t->nbAllocs); + assert(t->nbAllocs > 0); + t->nbAllocs -= 1; +} + +static const LZ4F_CustomMem lz4f_cmem_test = { + dummy_malloc, + dummy_calloc, + dummy_free, + &g_testAllocState +}; + + static clock_t FUZ_GetClockSpan(clock_t clockStart) { return clock() - clockStart; /* works even if overflow; max span ~ 30 mn */ } - #define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r))) unsigned int FUZ_rand(unsigned int* src) { @@ -121,7 +172,6 @@ unsigned int FUZ_rand(unsigned int* src) return rand32 >> 5; } - #define FUZ_RAND15BITS (FUZ_rand(seed) & 0x7FFF) #define FUZ_RANDLENGTH ( (FUZ_rand(seed) & 3) ? (FUZ_rand(seed) % 15) : (FUZ_rand(seed) % 510) + 15) static void FUZ_fillCompressibleNoiseBuffer(void* buffer, size_t bufferSize, double proba, U32* seed) @@ -515,7 +565,9 @@ int basicTests(U32 seed, double compressibility) /* dictID tests */ { size_t cErr; U32 const dictID = 0x99; - CHECK( LZ4F_createCompressionContext(&cctx, LZ4F_VERSION) ); + /* test advanced variant with custom allocator functions */ + cctx = LZ4F_createCompressionContext_advanced(lz4f_cmem_test, LZ4F_VERSION); + if (cctx==NULL) goto _output_error; DISPLAYLEVEL(3, "insert a dictID : "); memset(&prefs.frameInfo, 0, sizeof(prefs.frameInfo)); @@ -944,7 +996,7 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi clock_t const startClock = clock(); clock_t const clockDuration = duration_s * CLOCKS_PER_SEC; - /* Create buffers */ + /* Create states & 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); -- cgit v0.12 From a3c4f0d0a31580da09a955b6783203d6c383a9e4 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 13 Jul 2022 21:39:59 +0200 Subject: implemented LZ4F_createDecompressionContext_advanced() --- lib/lz4frame.c | 39 +++++++++++++++++++++++++-------------- lib/lz4frame.h | 8 +++++--- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/lib/lz4frame.c b/lib/lz4frame.c index 6b4d67d..c65256d 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -1193,6 +1193,7 @@ typedef enum { } dStage_t; struct LZ4F_dctx_s { + LZ4F_CustomMem cmem; LZ4F_frameInfo_t frameInfo; U32 version; dStage_t dStage; @@ -1214,22 +1215,32 @@ struct LZ4F_dctx_s { }; /* typedef'd to LZ4F_dctx in lz4frame.h */ +LZ4F_dctx* LZ4F_createDecompressionContext_advanced(LZ4F_CustomMem customMem, unsigned version) +{ + LZ4F_dctx* const dctx = (LZ4F_dctx*)LZ4F_calloc(sizeof(LZ4F_dctx), customMem); + if (dctx == NULL) return NULL; + + dctx->cmem = customMem; + dctx->version = version; + return dctx; +} + /*! LZ4F_createDecompressionContext() : * Create a decompressionContext object, which will track all decompression operations. * Provides a pointer to a fully allocated and initialized LZ4F_decompressionContext object. * Object can later be released using LZ4F_freeDecompressionContext(). * @return : if != 0, there was an error during context creation. */ -LZ4F_errorCode_t LZ4F_createDecompressionContext(LZ4F_dctx** LZ4F_decompressionContextPtr, unsigned versionNumber) +LZ4F_errorCode_t +LZ4F_createDecompressionContext(LZ4F_dctx** LZ4F_decompressionContextPtr, unsigned versionNumber) { - LZ4F_dctx* const dctx = (LZ4F_dctx*)LZ4F_calloc(sizeof(LZ4F_dctx), LZ4F_defaultCMem); - if (dctx == NULL) { /* failed allocation */ - *LZ4F_decompressionContextPtr = NULL; + assert(LZ4F_decompressionContextPtr != NULL); /* violation of narrow contract */ + RETURN_ERROR_IF(LZ4F_decompressionContextPtr == NULL, parameter_null); /* in case it nonetheless happen in production */ + + *LZ4F_decompressionContextPtr = LZ4F_createDecompressionContext_advanced(LZ4F_defaultCMem, versionNumber); + if (*LZ4F_decompressionContextPtr == NULL) { /* failed allocation */ RETURN_ERROR(allocation_failed); } - - dctx->version = versionNumber; - *LZ4F_decompressionContextPtr = dctx; return LZ4F_OK_NoError; } @@ -1238,9 +1249,9 @@ LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_dctx* dctx) LZ4F_errorCode_t result = LZ4F_OK_NoError; if (dctx != NULL) { /* can accept NULL input, like free() */ result = (LZ4F_errorCode_t)dctx->dStage; - LZ4F_free(dctx->tmpIn, LZ4F_defaultCMem); - LZ4F_free(dctx->tmpOutBuffer, LZ4F_defaultCMem); - LZ4F_free(dctx, LZ4F_defaultCMem); + LZ4F_free(dctx->tmpIn, dctx->cmem); + LZ4F_free(dctx->tmpOutBuffer, dctx->cmem); + LZ4F_free(dctx, dctx->cmem); } return result; } @@ -1604,11 +1615,11 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, + ((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*/ - LZ4F_free(dctx->tmpIn, LZ4F_defaultCMem); - dctx->tmpIn = (BYTE*)LZ4F_malloc(dctx->maxBlockSize + BFSize /* block checksum */, LZ4F_defaultCMem); + LZ4F_free(dctx->tmpIn, dctx->cmem); + dctx->tmpIn = (BYTE*)LZ4F_malloc(dctx->maxBlockSize + BFSize /* block checksum */, dctx->cmem); RETURN_ERROR_IF(dctx->tmpIn == NULL, allocation_failed); - LZ4F_free(dctx->tmpOutBuffer, LZ4F_defaultCMem); - dctx->tmpOutBuffer= (BYTE*)LZ4F_malloc(bufferNeeded, LZ4F_defaultCMem); + LZ4F_free(dctx->tmpOutBuffer, dctx->cmem); + dctx->tmpOutBuffer= (BYTE*)LZ4F_malloc(bufferNeeded, dctx->cmem); RETURN_ERROR_IF(dctx->tmpOutBuffer== NULL, allocation_failed); dctx->maxBufferSize = bufferNeeded; } } diff --git a/lib/lz4frame.h b/lib/lz4frame.h index 1b4ee76..0d2ebf9 100644 --- a/lib/lz4frame.h +++ b/lib/lz4frame.h @@ -364,9 +364,10 @@ typedef struct { /*! LZ4F_createDecompressionContext() : * Create an LZ4F_dctx object, to track all decompression operations. - * The version provided MUST be LZ4F_VERSION. - * The function provides a pointer to an allocated and initialized LZ4F_dctx object. - * The result is an errorCode, which can be tested using LZ4F_isError(). + * @version provided MUST be LZ4F_VERSION. + * @dctxPtr MUST be valid. + * The function fills @dctxPtr with the value of a pointer to an allocated and initialized LZ4F_dctx object. + * The @return is an errorCode, which can be tested using LZ4F_isError(). * dctx memory can be released using LZ4F_freeDecompressionContext(); * Result of LZ4F_freeDecompressionContext() indicates current state of decompressionContext when being released. * That is, it should be == 0 if decompression has been completed fully and correctly. @@ -571,6 +572,7 @@ __attribute__((__unused__)) LZ4F_CustomMem const LZ4F_defaultCMem = { NULL, NULL, NULL, NULL }; /**< this constant defers to stdlib's functions */ LZ4FLIB_STATIC_API LZ4F_cctx* LZ4F_createCompressionContext_advanced(LZ4F_CustomMem customMem, unsigned version); +LZ4FLIB_STATIC_API LZ4F_dctx* LZ4F_createDecompressionContext_advanced(LZ4F_CustomMem customMem, unsigned version); /*! LZ4F_getBlockSize() : -- cgit v0.12 From e17f7a22411ccd49bed5204ce3cf5ec0b260e634 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 13 Jul 2022 21:41:25 +0200 Subject: tested clang-12 with -m32 & -mx32 --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f15a954..5e08543 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -51,7 +51,7 @@ jobs: # clang { pkgs: 'lib32gcc-11-dev libx32gcc-11-dev', cc: clang, cxx: clang++, x32: 'fail', x86: 'fail', cxxtest: 'true', os: ubuntu-latest, }, - { pkgs: 'clang-12 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-12, cxx: clang++-12, x32: 'fail', x86: 'fail', cxxtest: 'true', os: ubuntu-20.04, }, + { pkgs: 'clang-12 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-12, cxx: clang++-12, x32: 'true', x86: 'true', cxxtest: 'true', os: ubuntu-20.04, }, { pkgs: 'clang-11 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-11, cxx: clang++-11, x32: 'fail', x86: 'fail', cxxtest: 'true', os: ubuntu-20.04, }, { pkgs: 'clang-10 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-10, cxx: clang++-10, x32: 'fail', x86: 'fail', cxxtest: 'true', os: ubuntu-20.04, }, { pkgs: 'clang-9 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-9, cxx: clang++-9, x32: 'fail', x86: 'fail', cxxtest: 'true', os: ubuntu-20.04, }, -- cgit v0.12 From e535d6424a2952dad6db73e2882abaef76a5e5e8 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 14 Jul 2022 01:29:58 +0200 Subject: implemented LZ4F_createCDict_advanced() --- lib/lz4frame.c | 45 +++++++++++++++++++++++++++++---------------- lib/lz4frame.h | 50 ++++++++++++++++++++++++++------------------------ tests/frametest.c | 11 +++++++++-- 3 files changed, 64 insertions(+), 42 deletions(-) diff --git a/lib/lz4frame.c b/lib/lz4frame.c index c65256d..258d85e 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -522,30 +522,31 @@ size_t LZ4F_compressFrame(void* dstBuffer, size_t dstCapacity, *****************************************************/ struct LZ4F_CDict_s { + LZ4F_CustomMem cmem; void* dictContent; LZ4_stream_t* fastCtx; LZ4_streamHC_t* HCCtx; }; /* typedef'd to LZ4F_CDict within lz4frame_static.h */ -/*! LZ4F_createCDict() : - * When compressing multiple messages / blocks with the same dictionary, it's recommended to load it just once. - * LZ4F_createCDict() will create a digested dictionary, ready to start future compression operations without startup delay. - * LZ4F_CDict can be created once and shared by multiple threads concurrently, since its usage is read-only. - * @dictBuffer can be released after LZ4F_CDict creation, since its content is copied within CDict - * @return : digested dictionary for compression, or NULL if failed */ -LZ4F_CDict* LZ4F_createCDict(const void* dictBuffer, size_t dictSize) +LZ4F_CDict* +LZ4F_createCDict_advanced(LZ4F_CustomMem cmem, const void* dictBuffer, size_t dictSize) { const char* dictStart = (const char*)dictBuffer; - LZ4F_CDict* const cdict = (LZ4F_CDict*)LZ4F_malloc(sizeof(*cdict), LZ4F_defaultCMem); - DEBUGLOG(4, "LZ4F_createCDict"); + LZ4F_CDict* const cdict = (LZ4F_CDict*)LZ4F_malloc(sizeof(*cdict), cmem); + DEBUGLOG(4, "LZ4F_createCDict_advanced"); if (!cdict) return NULL; + cdict->cmem = cmem; if (dictSize > 64 KB) { dictStart += dictSize - 64 KB; dictSize = 64 KB; } - cdict->dictContent = LZ4F_malloc(dictSize, LZ4F_defaultCMem); - cdict->fastCtx = LZ4_createStream(); - cdict->HCCtx = LZ4_createStreamHC(); + cdict->dictContent = LZ4F_malloc(dictSize, cmem); + cdict->fastCtx = (LZ4_stream_t*)LZ4F_malloc(sizeof(LZ4_stream_t), cmem); + if (cdict->fastCtx) + LZ4_initStream(cdict->fastCtx, sizeof(LZ4_stream_t)); + cdict->HCCtx = (LZ4_streamHC_t*)LZ4F_malloc(sizeof(LZ4_streamHC_t), cmem); + if (cdict->HCCtx) + LZ4_initStream(cdict->HCCtx, sizeof(LZ4_streamHC_t)); if (!cdict->dictContent || !cdict->fastCtx || !cdict->HCCtx) { LZ4F_freeCDict(cdict); return NULL; @@ -557,13 +558,25 @@ LZ4F_CDict* LZ4F_createCDict(const void* dictBuffer, size_t dictSize) return cdict; } +/*! LZ4F_createCDict() : + * When compressing multiple messages / blocks with the same dictionary, it's recommended to load it just once. + * LZ4F_createCDict() will create a digested dictionary, ready to start future compression operations without startup delay. + * LZ4F_CDict can be created once and shared by multiple threads concurrently, since its usage is read-only. + * @dictBuffer can be released after LZ4F_CDict creation, since its content is copied within CDict + * @return : digested dictionary for compression, or NULL if failed */ +LZ4F_CDict* LZ4F_createCDict(const void* dictBuffer, size_t dictSize) +{ + DEBUGLOG(4, "LZ4F_createCDict"); + return LZ4F_createCDict_advanced(LZ4F_defaultCMem, dictBuffer, dictSize); +} + void LZ4F_freeCDict(LZ4F_CDict* cdict) { if (cdict==NULL) return; /* support free on NULL */ - LZ4F_free(cdict->dictContent, LZ4F_defaultCMem); - LZ4_freeStream(cdict->fastCtx); - LZ4_freeStreamHC(cdict->HCCtx); - LZ4F_free(cdict, LZ4F_defaultCMem); + LZ4F_free(cdict->dictContent, cdict->cmem); + LZ4F_free(cdict->fastCtx, cdict->cmem); + LZ4F_free(cdict->HCCtx, cdict->cmem); + LZ4F_free(cdict, cdict->cmem); } diff --git a/lib/lz4frame.h b/lib/lz4frame.h index 0d2ebf9..74df963 100644 --- a/lib/lz4frame.h +++ b/lib/lz4frame.h @@ -551,30 +551,6 @@ typedef enum { LZ4F_LIST_ERRORS(LZ4F_GENERATE_ENUM) LZ4FLIB_STATIC_API LZ4F_errorCodes LZ4F_getErrorCode(size_t functionResult); -/*! Custom memory allocation : - * These prototypes make it possible to pass custom allocation/free functions. - * LZ4F_customMem is provided at state creation time, using LZ4F_createCompressionContext_advanced() listed below. - * All allocation/free operations will be completed using these custom variants instead of regular ones. - */ -typedef void* (*LZ4F_AllocFunction) (void* opaqueState, size_t size); -typedef void* (*LZ4F_CallocFunction) (void* opaqueState, size_t size); -typedef void (*LZ4F_FreeFunction) (void* opaqueState, void* address); -typedef struct { - LZ4F_AllocFunction customAlloc; - LZ4F_CallocFunction customCalloc; /* optional; when not defined, uses customAlloc + memset */ - LZ4F_FreeFunction customFree; - void* opaqueState; -} LZ4F_CustomMem; -static -#ifdef __GNUC__ -__attribute__((__unused__)) -#endif -LZ4F_CustomMem const LZ4F_defaultCMem = { NULL, NULL, NULL, NULL }; /**< this constant defers to stdlib's functions */ - -LZ4FLIB_STATIC_API LZ4F_cctx* LZ4F_createCompressionContext_advanced(LZ4F_CustomMem customMem, unsigned version); -LZ4FLIB_STATIC_API LZ4F_dctx* LZ4F_createDecompressionContext_advanced(LZ4F_CustomMem customMem, unsigned version); - - /*! LZ4F_getBlockSize() : * Return, in scalar format (size_t), * the maximum block size associated with blockSizeID. @@ -674,6 +650,32 @@ LZ4FLIB_STATIC_API size_t LZ4F_decompress_usingDict( const void* dict, size_t dictSize, const LZ4F_decompressOptions_t* decompressOptionsPtr); + +/*! Custom memory allocation : + * These prototypes make it possible to pass custom allocation/free functions. + * LZ4F_customMem is provided at state creation time, using LZ4F_create*_advanced() listed below. + * All allocation/free operations will be completed using these custom variants instead of regular ones. + */ +typedef void* (*LZ4F_AllocFunction) (void* opaqueState, size_t size); +typedef void* (*LZ4F_CallocFunction) (void* opaqueState, size_t size); +typedef void (*LZ4F_FreeFunction) (void* opaqueState, void* address); +typedef struct { + LZ4F_AllocFunction customAlloc; + LZ4F_CallocFunction customCalloc; /* optional; when not defined, uses customAlloc + memset */ + LZ4F_FreeFunction customFree; + void* opaqueState; +} LZ4F_CustomMem; +static +#ifdef __GNUC__ +__attribute__((__unused__)) +#endif +LZ4F_CustomMem const LZ4F_defaultCMem = { NULL, NULL, NULL, NULL }; /**< this constant defers to stdlib's functions */ + +LZ4FLIB_STATIC_API LZ4F_cctx* LZ4F_createCompressionContext_advanced(LZ4F_CustomMem customMem, unsigned version); +LZ4FLIB_STATIC_API LZ4F_dctx* LZ4F_createDecompressionContext_advanced(LZ4F_CustomMem customMem, unsigned version); +LZ4FLIB_STATIC_API LZ4F_CDict* LZ4F_createCDict_advanced(LZ4F_CustomMem customMem, const void* dictBuffer, size_t dictSize); + + #if defined (__cplusplus) } #endif diff --git a/tests/frametest.c b/tests/frametest.c index 00b8acb..ec2cb12 100644 --- a/tests/frametest.c +++ b/tests/frametest.c @@ -143,9 +143,9 @@ static void dummy_free(void* state, void* p) DISPLAYLEVEL(6, "freeing memory at address %p \n", p); free(p); assert(t != NULL); - DISPLAYLEVEL(5, "nb of allocated memory segments before this free : %i \n", t->nbAllocs); - assert(t->nbAllocs > 0); t->nbAllocs -= 1; + DISPLAYLEVEL(5, "nb of allocated memory segments after this free : %i \n", t->nbAllocs); + assert(t->nbAllocs >= 0); } static const LZ4F_CustomMem lz4f_cmem_test = { @@ -595,6 +595,13 @@ int basicTests(U32 seed, double compressibility) if (cdict == NULL) goto _output_error; CHECK( LZ4F_createCompressionContext(&cctx, LZ4F_VERSION) ); + DISPLAYLEVEL(3, "Testing LZ4F_createCDict_advanced : "); + { LZ4F_CDict* const cda = LZ4F_createCDict_advanced(lz4f_cmem_test, CNBuffer, dictSize); + if (cda == NULL) goto _output_error; + LZ4F_freeCDict(cda); + } + DISPLAYLEVEL(3, "OK \n"); + DISPLAYLEVEL(3, "LZ4F_compressFrame_usingCDict, with NULL dict : "); CHECK_V(cSizeNoDict, LZ4F_compressFrame_usingCDict(cctx, compressedBuffer, dstCapacity, -- cgit v0.12 From 2480d9cd8a7e8f480527a971b3c5c853fee75f45 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 14 Jul 2022 00:39:40 +0200 Subject: enable -m32 & -mx32 tests on all version of clang >= 4.0 clang 3.9 fails -m32 & -mx32, likely because it lacks the corresponding abi/library support. Oh well, it's not that important. Supporting all clang >= 4.0 is great enough for these tests. --- .github/workflows/ci.yml | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5e08543..0b1650c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -50,17 +50,17 @@ jobs: { pkgs: 'gcc-4.8 g++-4.8 lib32gcc-4.8-dev libx32gcc-4.8-dev', cc: gcc-4.8, cxx: g++-4.8, x32: 'true', x86: 'true', cxxtest: 'true', os: ubuntu-18.04, }, # clang - { pkgs: 'lib32gcc-11-dev libx32gcc-11-dev', cc: clang, cxx: clang++, x32: 'fail', x86: 'fail', cxxtest: 'true', os: ubuntu-latest, }, + { pkgs: 'lib32gcc-11-dev libx32gcc-11-dev', cc: clang, cxx: clang++, x32: 'true', x86: 'true', cxxtest: 'true', os: ubuntu-latest, }, { pkgs: 'clang-12 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-12, cxx: clang++-12, x32: 'true', x86: 'true', cxxtest: 'true', os: ubuntu-20.04, }, - { pkgs: 'clang-11 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-11, cxx: clang++-11, x32: 'fail', x86: 'fail', cxxtest: 'true', os: ubuntu-20.04, }, - { pkgs: 'clang-10 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-10, cxx: clang++-10, x32: 'fail', x86: 'fail', cxxtest: 'true', os: ubuntu-20.04, }, - { pkgs: 'clang-9 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-9, cxx: clang++-9, x32: 'fail', x86: 'fail', cxxtest: 'true', os: ubuntu-20.04, }, - { pkgs: 'clang-8 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-8, cxx: clang++-8, x32: 'fail', x86: 'fail', cxxtest: 'true', os: ubuntu-20.04, }, - { pkgs: 'clang-7 lib32gcc-7-dev libx32gcc-7-dev', cc: clang-7, cxx: clang++-7, x32: 'fail', x86: 'fail', cxxtest: 'true', os: ubuntu-20.04, }, - { pkgs: 'clang-6.0 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-6.0, cxx: clang++-6.0, x32: 'fail', x86: 'fail', cxxtest: 'true', os: ubuntu-20.04, }, - { pkgs: 'clang-5.0 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-5.0, cxx: clang++-5.0, x32: 'fail', x86: 'fail', cxxtest: 'true', os: ubuntu-18.04, }, - { pkgs: 'clang-4.0 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-4.0, cxx: clang++-4.0, x32: 'fail', x86: 'fail', cxxtest: 'true', os: ubuntu-18.04, }, - { pkgs: 'clang-3.9 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-3.9, cxx: clang++-3.9, x32: 'fail', x86: 'fail', cxxtest: 'false', os: ubuntu-18.04, }, + { pkgs: 'clang-11 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-11, cxx: clang++-11, x32: 'true', x86: 'true', cxxtest: 'true', os: ubuntu-20.04, }, + { pkgs: 'clang-10 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-10, cxx: clang++-10, x32: 'true', x86: 'true', cxxtest: 'true', os: ubuntu-20.04, }, + { pkgs: 'clang-9 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-9, cxx: clang++-9, x32: 'true', x86: 'true', cxxtest: 'true', os: ubuntu-20.04, }, + { pkgs: 'clang-8 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-8, cxx: clang++-8, x32: 'true', x86: 'true', cxxtest: 'true', os: ubuntu-20.04, }, + { pkgs: 'clang-7 lib32gcc-7-dev libx32gcc-7-dev', cc: clang-7, cxx: clang++-7, x32: 'true', x86: 'true', cxxtest: 'true', os: ubuntu-20.04, }, + { pkgs: 'clang-6.0 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-6.0, cxx: clang++-6.0, x32: 'true', x86: 'true', cxxtest: 'true', os: ubuntu-20.04, }, + { pkgs: 'clang-5.0 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-5.0, cxx: clang++-5.0, x32: 'true', x86: 'true', cxxtest: 'true', os: ubuntu-18.04, }, + { pkgs: 'clang-4.0 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-4.0, cxx: clang++-4.0, x32: 'true', x86: 'true', cxxtest: 'true', os: ubuntu-18.04, }, + { pkgs: 'clang-3.9', cc: clang-3.9, cxx: clang++-3.9, x32: 'fail', x86: 'fail', cxxtest: 'false', os: ubuntu-18.04, }, ] runs-on: ${{ matrix.os }} -- cgit v0.12 From 4e9c680c807af82e11993f0ea177f9e97fd28e4a Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 14 Jul 2022 21:20:51 +0200 Subject: minor update of .github/workflows README --- .github/workflows/README.md | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/.github/workflows/README.md b/.github/workflows/README.md index eddfd3f..eaa9f03 100644 --- a/.github/workflows/README.md +++ b/.github/workflows/README.md @@ -1,17 +1,5 @@ This directory contains [GitHub Actions](https://github.com/features/actions) workflow files. -# Maintenance Schedule - -[`ubuntu-16.04` environment will be removed at September, 2021.]((https://github.blog/changelog/2021-04-29-github-actions-ubuntu-16-04-lts-virtual-environment-will-be-removed-on-september-20-2021/). ----------------------------------------------------------------- - -We also will remove test for the following compilers. - -- gcc: 4.4, 4.6, 4.7 -- clang: 3.5, 3.6, 3.7, 3.8 - - - # Known issues ## USAN, ASAN (`lz4-ubsan-x64`, `lz4-ubsan-x86`, `lz4-asan-x64`) -- cgit v0.12 From 1d17532d70d0b93032de4ada17c1787ee8939f15 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 14 Jul 2022 21:24:42 +0200 Subject: simplify travis script by removing commented tests (now transferred to Github Actions) --- .travis.yml | 202 +----------------------------------------------------------- 1 file changed, 1 insertion(+), 201 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4656cbb..0aeea6e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,126 +3,7 @@ language: c matrix: fast_finish: true include: -# # OS X Mavericks -# - name: (macOS) General Test -# os: osx -# compiler: clang -# script: -# - make # test library build -# - make clean -# - 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 -# dist: precise -# script: -# - make -C tests test-lz4 test-lz4c test-fullbench -# -# - 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 -# -# - 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: 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 -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 -# script: -# - make cxxtest -# - make clean -# - make examples -# - make clean cmake -# - make clean travis-install -# - make clean clangtest -# -# - name: x32 compatibility test -# addons: -# apt: -# packages: -# - 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 -# compiler: clang -# script: -# - make usan MOREFLAGS=-Wcomma -Werror -# -# - name: (Trusty) valgrind test -# dist: trusty -# install: -# - sudo apt-get install -qq valgrind -# script: -# - make c_standards -# - make -C tests test-lz4 test-mem -# -# - name: (Trusty) c-to-c++ test -# dist: trusty -# script: -# - make ctocpptest -# -# - name: (Trusty) i386 benchmark + version test -# dist: trusty -# 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 -# install: -# - 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 -# -# - name: (Trusty) gcc-6 standard C compilation -# dist: trusty -# addons: -# apt: -# sources: -# - ubuntu-toolchain-r-test -# packages: -# - gcc-6 -# env: -# - CC=gcc-6 -# script: -# - make c_standards -# - make -C tests test-lz4 MOREFLAGS=-Werror -# -# - name: (Trusty) arm + aarch64 compilation -# dist: trusty -# 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 -# + - name: aarch64 real-hw tests arch: arm64 script: @@ -138,57 +19,6 @@ matrix: script: - make test -# - name: (Xenial) gcc-5 compilation -# dist: xenial -# install: -# - sudo apt-get install -qq libc6-dev-i386 gcc-multilib -# script: -# - make -C tests test-lz4 clean test-lz4c32 MOREFLAGS=-Werror -# -# - name: (Trusty) clang-3.8 compilation -# dist: trusty -# addons: -# apt: -# sources: -# - ubuntu-toolchain-r-test -# - llvm-toolchain-precise-3.8 -# packages: -# - clang-3.8 -# script: -# - make -C tests test-lz4 CC=clang-3.8 -# -# - name: (Trusty) PowerPC + PPC64 compilation -# dist: trusty -# 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 -# compiler: clang -# install: -# - sudo apt-get install -qq cppcheck -# script: -# - make staticAnalyze -# - make cppcheck -# -# - name: (Trusty) gcc-4.4 compilation -# dist: trusty -# addons: -# apt: -# sources: -# - ubuntu-toolchain-r-test -# packages: -# - 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 - name: tag build if: tag =~ ^v[0-9]\.[0-9] @@ -197,36 +27,6 @@ matrix: - 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 -# - | -# 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 -# - tree ./staging - # oss-fuzz compilation test - name: Compile OSS-Fuzz targets script: -- cgit v0.12 From 684ebfd4be59edc50ab7dc1475eed36a66deef63 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 14 Jul 2022 21:59:27 +0200 Subject: Test support of Standard Makefile Variables to detect issues such as #958 --- .github/workflows/ci.yml | 10 ++++++++++ Makefile | 39 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0b1650c..abc7600 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -207,6 +207,16 @@ jobs: run: make V=1 -C tests test-fuzzer32 + lz4-standard-makefile-variables: + name: LZ4 Makefile - support for standard variables + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 # https://github.com/actions/checkout + + - name: make standard_variables + run: make V=1 standard_variables + + lz4-versions: name: LZ4 versions test runs-on: ubuntu-latest diff --git a/Makefile b/Makefile index fd62945..9e12ae7 100644 --- a/Makefile +++ b/Makefile @@ -233,6 +233,43 @@ c_standards_c99: clean .PHONY: c_standards_c11 c_standards_c11: clean - $(MAKE) clean; CFLAGS="-std=c11 -Werror" $(MAKE) all + $(MAKE) clean; CFLAGS="-std=c11 -Werror" $(MAKE) all + +# The following test ensures that standard Makefile variables set through environment +# are correctly transmitted at compilation stage. +# This test is meant to detect issues like https://github.com/lz4/lz4/issues/958 +.PHONY: standard_variables +standard_variables: clean + @echo ================= + @echo Check support of Makefile Standard variables through environment + @echo note : this test requires V=1 to work properly + @echo ================= + CC="cc -DCC_TEST" \ + CFLAGS=-DCFLAGS_TEST \ + CPPFLAGS=-DCPPFLAGS_TEST \ + LDFLAGS=-DLDFLAGS_TEST \ + LDLIBS=-DLDLIBS_TEST \ + $(MAKE) V=1 > tmpsv + # Note: just checking the presence of custom flags + # would not detect situations where custom flags are + # supported in some part of the Makefile, and missed in others. + # So the test checks if they are present the _right nb of times_. + # However, checking static quantities makes this test brittle, + # because quantities (7, 2 and 1) can still evolve in future, + # for example when source directories or Makefile evolve. + if [ $$(grep CC_TEST tmpsv | wc -l) -ne 7 ]; then \ + echo "CC environment variable missed" && False; fi + if [ $$(grep CFLAGS_TEST tmpsv | wc -l) -ne 7 ]; then \ + echo "CFLAGS environment variable missed" && False; fi + if [ $$(grep CPPFLAGS_TEST tmpsv | wc -l) -ne 7 ]; then \ + echo "CPPFLAGS environment variable missed" && False; fi + if [ $$(grep LDFLAGS_TEST tmpsv | wc -l) -ne 2 ]; then \ + echo "LDFLAGS environment variable missed" && False; fi + if [ $$(grep LDLIBS_TEST tmpsv | wc -l) -ne 1 ]; then \ + echo "LDLIBS environment variable missed" && False; fi + @echo ================= + @echo all custom variables detected + @echo ================= + $(RM) tmpsv endif # MSYS POSIX -- cgit v0.12 From 0e9ab694f22fa3c1ec316f997a4277e30b70ee25 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 15 Jul 2022 12:13:45 +0200 Subject: refactor test ensuring that make test does not depend on console status Fix #990 --- .github/workflows/ci.yml | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0b1650c..616926e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -581,10 +581,9 @@ jobs: - name: make test run: make V=1 clean test MOREFLAGS='-Werror -Wconversion -Wno-sign-conversion' - - name: make test | tee - # test scenario where `stdout` is not the console - run: make V=1 clean test MOREFLAGS='-Werror -Wconversion -Wno-sign-conversion' | tee - + - name: Ensure `make test` doesn't depend on the status of the console + # see issue #990 for detailed explanations + run: make test > /dev/null ############################################################### -- cgit v0.12 From 35d58a5ea7fccef9fd08fce6a5f5e6efda7daa57 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 15 Jul 2022 13:05:43 +0200 Subject: fix decompress-partial-usingDict.c The recently added test decompress-partial-usingDict tends to fail for unknown reasons, more frequently under the combination for clang-9 + `-mx32`. There is a suspicion that the test is using too much stack. Fixing that, + adding traces, to get more information if it fails again. --- tests/decompress-partial-usingDict.c | 37 +++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/tests/decompress-partial-usingDict.c b/tests/decompress-partial-usingDict.c index cfcb971..8b85106 100644 --- a/tests/decompress-partial-usingDict.c +++ b/tests/decompress-partial-usingDict.c @@ -1,6 +1,7 @@ -#include "stdio.h" -#include "string.h" -#include "stdlib.h" +#include +#include +#include +#include #include "lz4.h" const char source[] = @@ -33,27 +34,35 @@ int main(void) size_t const smallSize = 1024; size_t const largeSize = 64 * 1024 - 1; char cmpBuffer[BUFFER_SIZE]; - char buffer[BUFFER_SIZE + largeSize]; + char* const buffer = (char*)malloc(BUFFER_SIZE + largeSize); char* outBuffer = buffer + largeSize; char* const dict = (char*)malloc(largeSize); char* const largeDict = dict; char* const smallDict = dict + largeSize - smallSize; - int cmpSize; int i; + int cmpSize; + + printf("starting test decompress-partial-usingDict : \n"); + assert(buffer != NULL); + assert(dict != NULL); cmpSize = LZ4_compress_default(source, cmpBuffer, srcLen, BUFFER_SIZE); for (i = cmpSize; i < cmpSize + 10; ++i) { int result = LZ4_decompress_safe_partial_usingDict(cmpBuffer, outBuffer, i, srcLen, BUFFER_SIZE, NULL, 0); - if ((result < 0) || (result != srcLen) || memcmp(source, outBuffer, srcLen)) { + if ( (result < 0) + || (result != srcLen) + || memcmp(source, outBuffer, (size_t)srcLen) ) { printf("test decompress-partial-usingDict with no dict error \n"); return -1; } } - + for (i = cmpSize; i < cmpSize + 10; ++i) { int result = LZ4_decompress_safe_partial_usingDict(cmpBuffer, outBuffer, i, srcLen, BUFFER_SIZE, outBuffer - smallSize, smallSize); - if ((result < 0) || (result != srcLen) || memcmp(source, outBuffer, srcLen)) { + if ( (result < 0) + || (result != srcLen) + || memcmp(source, outBuffer, (size_t)srcLen) ) { printf("test decompress-partial-usingDict with small prefix error \n"); return -1; } @@ -61,7 +70,9 @@ int main(void) for (i = cmpSize; i < cmpSize + 10; ++i) { int result = LZ4_decompress_safe_partial_usingDict(cmpBuffer, outBuffer, i, srcLen, BUFFER_SIZE, buffer, largeSize); - if ((result < 0) || (result != srcLen) || memcmp(source, outBuffer, srcLen)) { + if ( (result < 0) + || (result != srcLen) + || memcmp(source, outBuffer, (size_t)srcLen) ) { printf("test decompress-partial-usingDict with large prefix error \n"); return -1; } @@ -69,7 +80,9 @@ int main(void) for (i = cmpSize; i < cmpSize + 10; ++i) { int result = LZ4_decompress_safe_partial_usingDict(cmpBuffer, outBuffer, i, srcLen, BUFFER_SIZE, smallDict, smallSize); - if ((result < 0) || (result != srcLen) || memcmp(source, outBuffer, srcLen)) { + if ( (result < 0) + || (result != srcLen) + || memcmp(source, outBuffer, (size_t)srcLen) ) { printf("test decompress-partial-usingDict with small external dict error \n"); return -1; } @@ -77,7 +90,9 @@ int main(void) for (i = cmpSize; i < cmpSize + 10; ++i) { int result = LZ4_decompress_safe_partial_usingDict(cmpBuffer, outBuffer, i, srcLen, BUFFER_SIZE, largeDict, largeSize); - if ((result < 0) || (result != srcLen) || memcmp(source, outBuffer, srcLen)) { + if ( (result < 0) + || (result != srcLen) + || memcmp(source, outBuffer, (size_t)srcLen) ) { printf("test decompress-partial-usingDict with large external dict error \n"); return -1; } -- cgit v0.12 From 6784e78e006329ba67eb799a1cb94dd7267a9241 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 15 Jul 2022 16:11:26 +0200 Subject: support skippable frames within pipe fix #977 fseek() doesn't work for pipe, switch to "read and forget" mode in such case. --- lib/lz4frame.c | 2 -- lib/lz4frame.h | 2 ++ programs/lz4io.c | 25 ++++++++++++++++++++++--- tests/Makefile | 14 +++++++++++++- tests/frametest.c | 2 -- tests/goldenSamples/skip.bin | Bin 0 -> 38 bytes 6 files changed, 37 insertions(+), 8 deletions(-) create mode 100644 tests/goldenSamples/skip.bin diff --git a/lib/lz4frame.c b/lib/lz4frame.c index 5373083..0516f1f 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -206,8 +206,6 @@ static void LZ4F_writeLE64 (void* dst, U64 value64) #define _4BITS 0x0F #define _8BITS 0xFF -#define LZ4F_MAGIC_SKIPPABLE_START 0x184D2A50U -#define LZ4F_MAGICNUMBER 0x184D2204U #define LZ4F_BLOCKUNCOMPRESSED_FLAG 0x80000000U #define LZ4F_BLOCKSIZEID_DEFAULT LZ4F_max64KB diff --git a/lib/lz4frame.h b/lib/lz4frame.h index 1de09d8..7ebbfec 100644 --- a/lib/lz4frame.h +++ b/lib/lz4frame.h @@ -376,6 +376,8 @@ LZ4FLIB_API LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_dctx* dctx); * Streaming decompression functions *************************************/ +#define LZ4F_MAGICNUMBER 0x184D2204U +#define LZ4F_MAGIC_SKIPPABLE_START 0x184D2A50U #define LZ4F_MIN_SIZE_TO_KNOW_HEADER_LENGTH 5 /*! LZ4F_headerSize() : v1.9.0+ diff --git a/programs/lz4io.c b/programs/lz4io.c index 36736c2..5644775 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -1152,6 +1152,23 @@ LZ4IO_passThrough(FILE* finput, FILE* foutput, return total; } +/* when fseek() doesn't work (pipe scenario), + * read and forget from input. +**/ +#define SKIP_BUFF_SIZE (16 KB) +#define MIN(a,b) ( ((a)<(b)) ? (a) : (b) ) +static int skipStream(FILE* f, unsigned offset) +{ + char buf[SKIP_BUFF_SIZE]; + while (offset > 0) { + size_t const tr = MIN(offset, sizeof(buf)); + size_t const r = fread(buf, 1, tr, f); + if (r != tr) return 1; /* error reading f */ + offset -= (unsigned)tr; + } + assert(offset == 0); + return 0; +} /** Safely handle cases when (unsigned)offset > LONG_MAX */ static int fseek_u32(FILE *fp, unsigned offset, int where) @@ -1163,13 +1180,15 @@ static int fseek_u32(FILE *fp, unsigned offset, int where) while (offset > 0) { unsigned s = offset; if (s > stepMax) s = stepMax; - errorNb = UTIL_fseek(fp, (long) s, SEEK_CUR); - if (errorNb != 0) break; - offset -= s; + errorNb = UTIL_fseek(fp, (long)s, SEEK_CUR); + if (errorNb==0) { offset -= s; continue; } + errorNb = skipStream(fp, offset); + offset = 0; } return errorNb; } + #define ENDOFSTREAM ((unsigned long long)-1) #define DECODING_ERROR ((unsigned long long)-2) static unsigned long long diff --git a/tests/Makefile b/tests/Makefile index 6f063a1..002fb40 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -342,6 +342,17 @@ test-lz4-multiple-legacy: lz4 datagen ! $(LZ4) -f -l -m tmp-tlm-concat1 notHere-legacy tmp-tlm-concat2 # must fail : notHere-legacy not present @$(RM) tmp-tlm* +SKIPFILE = goldenSamples/skip.bin +test-lz4-skippable: lz4 datagen + @echo "\n ---- test lz4 with skippable frames ----" + $(LZ4) -dc $(SKIPFILE) + $(LZ4) -dc < $(SKIPFILE) + cat $(SKIPFILE) | $(LZ4) -dc + echo "Hello from Valid Frame!\n" > tmplsk + $(LZ4) tmplsk -c > tmplsk.lz4 + cat $(SKIPFILE) tmplsk.lz4 $(SKIPFILE) | $(LZ4) -dc + $(RM) tmplsk* + test-lz4-basic: lz4 datagen unlz4 lz4cat @echo "\n ---- test lz4 basic compression/decompression ----" $(DATAGEN) -g0 | $(LZ4) -v | $(LZ4) -t @@ -484,7 +495,8 @@ test-lz4-essentials : lz4 datagen test-lz4-basic test-lz4-multiple test-lz4-mult @$(RM) tmp* test-lz4: lz4 datagen test-lz4-essentials test-lz4-opt-parser \ - test-lz4-sparse test-lz4-hugefile test-lz4-dict + test-lz4-sparse test-lz4-hugefile test-lz4-dict \ + test-lz4-skippable @$(RM) tmp* test-lz4c: lz4c datagen diff --git a/tests/frametest.c b/tests/frametest.c index 58eac38..2d355bb 100644 --- a/tests/frametest.c +++ b/tests/frametest.c @@ -65,8 +65,6 @@ static void FUZ_writeLE32 (void* dstVoidPtr, U32 value32) /*-************************************ * Constants **************************************/ -#define LZ4F_MAGIC_SKIPPABLE_START 0x184D2A50U - #define KB *(1U<<10) #define MB *(1U<<20) #define GB *(1U<<30) diff --git a/tests/goldenSamples/skip.bin b/tests/goldenSamples/skip.bin new file mode 100644 index 0000000..1a8b9d5 Binary files /dev/null and b/tests/goldenSamples/skip.bin differ -- cgit v0.12 From a45597d260b9c30959dc85244f10d6dd0a624562 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sat, 16 Jul 2022 15:10:06 +0200 Subject: test independence for parallel run for `make -j test`. note : test-install is no longer part of `make test` It will have to be run on its own. --- tests/Makefile | 434 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 228 insertions(+), 206 deletions(-) diff --git a/tests/Makefile b/tests/Makefile index 002fb40..469db9a 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -179,7 +179,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-decompress-partial +test: test-lz4 test-lz4c test-frametest test-fullbench test-fuzzer test-amalgamation listTest test-decompress-partial .PHONY: test32 test32: CFLAGS+=-m32 @@ -201,257 +201,278 @@ test-compile-with-lz4-memory-usage: $(MAKE) clean; CFLAGS=-O0 CPPFLAGS=-D'LZ4_MEMORY_USAGE=LZ4_MEMORY_USAGE_MAX' $(MAKE) all .PHONY: test-lz4-sparse +# Rules regarding Temporary test files : +# Each test must use its own unique set of names during execution. +# Each temporary test file must begin by an FPREFIX. +# Each FPREFIX must be unique for each test. +# All FPREFIX must start with `tmp`, for `make clean` +# All tests must clean their temporary test files on successful completion, +# and only their test files : do not employ sweeping statements such `rm tmp*` or `rm *.lz4` +test-lz4-sparse: FPREFIX = tmp-tls test-lz4-sparse: lz4 datagen @echo "\n ---- test sparse file support ----" - $(DATAGEN) -g5M -P100 > tmplsdg5M - $(LZ4) -B4D tmplsdg5M -c | $(LZ4) -dv --sparse > tmplscB4 - $(DIFF) -s tmplsdg5M tmplscB4 - $(LZ4) -B5D tmplsdg5M -c | $(LZ4) -dv --sparse > tmplscB5 - $(DIFF) -s tmplsdg5M tmplscB5 - $(LZ4) -B6D tmplsdg5M -c | $(LZ4) -dv --sparse > tmplscB6 - $(DIFF) -s tmplsdg5M tmplscB6 - $(LZ4) -B7D tmplsdg5M -c | $(LZ4) -dv --sparse > tmplscB7 - $(DIFF) -s tmplsdg5M tmplscB7 - $(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 - ls -ls tmplsodd - @$(RM) tmpls* + $(DATAGEN) -g5M -P100 > $(FPREFIX)dg5M + $(LZ4) -B4D $(FPREFIX)dg5M -c | $(LZ4) -dv --sparse > $(FPREFIX)cB4 + $(DIFF) -s $(FPREFIX)dg5M $(FPREFIX)cB4 + $(LZ4) -B5D $(FPREFIX)dg5M -c | $(LZ4) -dv --sparse > $(FPREFIX)cB5 + $(DIFF) -s $(FPREFIX)dg5M $(FPREFIX)cB5 + $(LZ4) -B6D $(FPREFIX)dg5M -c | $(LZ4) -dv --sparse > $(FPREFIX)cB6 + $(DIFF) -s $(FPREFIX)dg5M $(FPREFIX)cB6 + $(LZ4) -B7D $(FPREFIX)dg5M -c | $(LZ4) -dv --sparse > $(FPREFIX)cB7 + $(DIFF) -s $(FPREFIX)dg5M $(FPREFIX)cB7 + $(LZ4) $(FPREFIX)dg5M -c | $(LZ4) -dv --no-sparse > $(FPREFIX)nosparse + $(DIFF) -s $(FPREFIX)dg5M $(FPREFIX)nosparse + ls -ls $(FPREFIX)* + $(DATAGEN) -s1 -g1200007 -P100 | $(LZ4) | $(LZ4) -dv --sparse > $(FPREFIX)odd # Odd size file (to generate non-full last block) + $(DATAGEN) -s1 -g1200007 -P100 | $(DIFF) -s - $(FPREFIX)odd + ls -ls $(FPREFIX)odd + @$(RM) $(FPREFIX)* @echo "\n Compatibility with Console :" echo "Hello World 1 !" | $(LZ4) | $(LZ4) -d -c 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 - $(LZ4) -B5 -v tmplsdg1M tmplsc - $(LZ4) -d -v tmplsc tmplsr - $(LZ4) -d -v tmplsc -c >> tmplsr - ls -ls tmp* - $(DIFF) tmpls2M tmplsr - @$(RM) tmpls* - + $(DATAGEN) -P100 -g1M > $(FPREFIX)dg1M + $(CAT) $(FPREFIX)dg1M $(FPREFIX)dg1M > $(FPREFIX)2M + $(LZ4) -B5 -v $(FPREFIX)dg1M $(FPREFIX)c + $(LZ4) -d -v $(FPREFIX)c $(FPREFIX)r + $(LZ4) -d -v $(FPREFIX)c -c >> $(FPREFIX)r + ls -ls $(FPREFIX)* + $(DIFF) $(FPREFIX)2M $(FPREFIX)r + @$(RM) $(FPREFIX)* + +test-lz4-contentSize: FPREFIX = tmp-lzc test-lz4-contentSize: lz4 datagen @echo "\n ---- test original size support ----" - $(DATAGEN) -g15M > tmplc1 - $(LZ4) -v tmplc1 -c | $(LZ4) -t - $(LZ4) -v --content-size tmplc1 -c | $(LZ4) -d > tmplc2 - $(DIFF) tmplc1 tmplc2 - $(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 - $(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* - + $(DATAGEN) -g15M > $(FPREFIX) + $(LZ4) -v $(FPREFIX) -c | $(LZ4) -t + $(LZ4) -v --content-size $(FPREFIX) -c | $(LZ4) -d > $(FPREFIX)-dup + $(DIFF) $(FPREFIX) $(FPREFIX)-dup + $(LZ4) -f $(FPREFIX) -c > $(FPREFIX).lz4 # compressed with content size + $(LZ4) --content-size $(FPREFIX) -c > $(FPREFIX)-wcz.lz4 + ! $(DIFF) $(FPREFIX).lz4 $(FPREFIX)-wcz.lz4 # must differ, due to content size + $(LZ4) --content-size < $(FPREFIX) > $(FPREFIX)-wcz2.lz4 # can determine content size because stdin is just a file + $(DIFF) $(FPREFIX)-wcz.lz4 $(FPREFIX)-wcz2.lz4 # both must contain content size + $(CAT) $(FPREFIX) | $(LZ4) > $(FPREFIX)-ncz.lz4 + $(DIFF) $(FPREFIX).lz4 $(FPREFIX)-ncz.lz4 # both don't have content size + $(CAT) $(FPREFIX) | $(LZ4) --content-size > $(FPREFIX)-ncz2.lz4 # can't determine content size + $(DIFF) $(FPREFIX).lz4 $(FPREFIX)-ncz2.lz4 # both don't have content size + @$(RM) $(FPREFIX)* + +test-lz4-frame-concatenation: FPREFIX = tmp-lfc 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 - $(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 -c > tmp-lfc-result - $(CMP) tmp-lfc-src tmp-lfc-result - @$(RM) tmp-lfc-* + @echo -n > $(FPREFIX)-empty + @echo hi > $(FPREFIX)-nonempty + $(CAT) $(FPREFIX)-nonempty $(FPREFIX)-empty $(FPREFIX)-nonempty > $(FPREFIX)-src + $(LZ4) -zq $(FPREFIX)-empty -c > $(FPREFIX)-empty.lz4 + $(LZ4) -zq $(FPREFIX)-nonempty -c > $(FPREFIX)-nonempty.lz4 + $(CAT) $(FPREFIX)-nonempty.lz4 $(FPREFIX)-empty.lz4 $(FPREFIX)-nonempty.lz4 > $(FPREFIX)-concat.lz4 + $(LZ4) -d $(FPREFIX)-concat.lz4 -c > $(FPREFIX)-result + $(CMP) $(FPREFIX)-src $(FPREFIX)-result + @$(RM) $(FPREFIX)* @echo frame concatenation test completed +test-lz4-multiple: FPREFIX = tmp-tml 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 > $(FPREFIX)1 2> $(VOID) + @$(DATAGEN) -s2 -g100K > $(FPREFIX)2 2> $(VOID) + @$(DATAGEN) -s3 -g200K > $(FPREFIX)3 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 + $(LZ4) -f -m $(FPREFIX)* + test -f $(FPREFIX)1.lz4 + test -f $(FPREFIX)2.lz4 + test -f $(FPREFIX)3.lz4 # 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 + mv $(FPREFIX)1 $(FPREFIX)1-orig + mv $(FPREFIX)2 $(FPREFIX)2-orig + mv $(FPREFIX)3 $(FPREFIX)3-orig + $(LZ4) -d -f -m $(FPREFIX)*.lz4 + $(CMP) $(FPREFIX)1 $(FPREFIX)1-orig # must be identical + $(CMP) $(FPREFIX)2 $(FPREFIX)2-orig + $(CMP) $(FPREFIX)3 $(FPREFIX)3-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 + $(CAT) $(FPREFIX)1.lz4 $(FPREFIX)2.lz4 $(FPREFIX)3.lz4 > $(FPREFIX)-concat1 + $(RM) $(FPREFIX)*.lz4 + $(LZ4) -m $(FPREFIX)1 $(FPREFIX)2 $(FPREFIX)3 -c > $(FPREFIX)-concat2 + test ! -f $(FPREFIX)1.lz4 # must not create .lz4 artefact + $(CMP) $(FPREFIX)-concat1 $(FPREFIX)-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 + $(RM) $(FPREFIX)-concat1 $(FPREFIX)-concat2 + $(LZ4) -f -m $(FPREFIX)1 $(FPREFIX)2 $(FPREFIX)3 # generate .lz4 to decompress + $(CAT) $(FPREFIX)1 $(FPREFIX)2 $(FPREFIX)3 > $(FPREFIX)-concat1 # create concatenated reference + $(RM) $(FPREFIX)1 $(FPREFIX)2 $(FPREFIX)3 + $(LZ4) -d -m $(FPREFIX)1.lz4 $(FPREFIX)2.lz4 $(FPREFIX)3.lz4 -c > $(FPREFIX)-concat2 + test ! -f $(FPREFIX)1 # must not create file artefact + $(CMP) $(FPREFIX)-concat1 $(FPREFIX)-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 + ! $(LZ4) -f -m $(FPREFIX)-concat1 notHere $(FPREFIX)-concat2 # must fail : notHere not present # test lz4-compressed file - $(LZ4) -tm tmp-tlm-concat1.lz4 - $(LZ4) -tm tmp-tlm-concat1.lz4 tmp-tlm-concat2.lz4 + $(LZ4) -tm $(FPREFIX)-concat1.lz4 + $(LZ4) -tm $(FPREFIX)-concat1.lz4 $(FPREFIX)-concat2.lz4 # test multiple lz4 files, one of which is absent (must fail) - ! $(LZ4) -tm tmp-tlm-concat1.lz4 notHere.lz4 tmp-tlm-concat2.lz4 - @$(RM) tmp-tlm* + ! $(LZ4) -tm $(FPREFIX)-concat1.lz4 notHere.lz4 $(FPREFIX)-concat2.lz4 + @$(RM) $(FPREFIX)* +test-lz4-multiple-legacy: FPREFIX = tmp-lml 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 > $(FPREFIX)1 2> $(VOID) + @$(DATAGEN) -s2 -g100K > $(FPREFIX)2 2> $(VOID) + @$(DATAGEN) -s3 -g200K > $(FPREFIX)3 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 + $(LZ4) -f -l -m $(FPREFIX)* + test -f $(FPREFIX)1.lz4 + test -f $(FPREFIX)2.lz4 + test -f $(FPREFIX)3.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 + mv $(FPREFIX)1 $(FPREFIX)1-orig + mv $(FPREFIX)2 $(FPREFIX)2-orig + mv $(FPREFIX)3 $(FPREFIX)3-orig + $(LZ4) -d -f -m $(FPREFIX)*.lz4 + $(LZ4) -l -d -f -m $(FPREFIX)*.lz4 # -l mustn't impact -d option + $(CMP) $(FPREFIX)1 $(FPREFIX)1-orig # must be identical + $(CMP) $(FPREFIX)2 $(FPREFIX)2-orig + $(CMP) $(FPREFIX)3 $(FPREFIX)3-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 + $(CAT) $(FPREFIX)1.lz4 $(FPREFIX)2.lz4 $(FPREFIX)3.lz4 > $(FPREFIX)-concat1 + $(RM) $(FPREFIX)*.lz4 + $(LZ4) -l -m $(FPREFIX)1 $(FPREFIX)2 $(FPREFIX)3 -c > $(FPREFIX)-concat2 + test ! -f $(FPREFIX)1.lz4 # must not create .lz4 artefact + $(CMP) $(FPREFIX)-concat1 $(FPREFIX)-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 + $(RM) $(FPREFIX)-concat1 $(FPREFIX)-concat2 + $(LZ4) -l -f -m $(FPREFIX)1 $(FPREFIX)2 $(FPREFIX)3 # generate .lz4 to decompress + $(CAT) $(FPREFIX)1 $(FPREFIX)2 $(FPREFIX)3 > $(FPREFIX)-concat1 # create concatenated reference + $(RM) $(FPREFIX)1 $(FPREFIX)2 $(FPREFIX)3 + $(LZ4) -d -m $(FPREFIX)1.lz4 $(FPREFIX)2.lz4 $(FPREFIX)3.lz4 -c > $(FPREFIX)-concat2 + $(LZ4) -d -l -m $(FPREFIX)1.lz4 $(FPREFIX)2.lz4 $(FPREFIX)3.lz4 -c > $(FPREFIX)-concat2 # -l mustn't impact option -d + test ! -f $(FPREFIX)1 # must not create file artefact + $(CMP) $(FPREFIX)-concat1 $(FPREFIX)-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* + ! $(LZ4) -f -l -m $(FPREFIX)-concat1 notHere-legacy $(FPREFIX)-concat2 # must fail : notHere-legacy not present + @$(RM) $(FPREFIX)* SKIPFILE = goldenSamples/skip.bin +test-lz4-skippable: FPREFIX = tmp-lsk test-lz4-skippable: lz4 datagen @echo "\n ---- test lz4 with skippable frames ----" $(LZ4) -dc $(SKIPFILE) $(LZ4) -dc < $(SKIPFILE) cat $(SKIPFILE) | $(LZ4) -dc - echo "Hello from Valid Frame!\n" > tmplsk - $(LZ4) tmplsk -c > tmplsk.lz4 - cat $(SKIPFILE) tmplsk.lz4 $(SKIPFILE) | $(LZ4) -dc - $(RM) tmplsk* + echo "Hello from Valid Frame!\n" | $(LZ4) -c > $(FPREFIX).lz4 + cat $(SKIPFILE) $(FPREFIX).lz4 $(SKIPFILE) | $(LZ4) -dc + $(RM) $(FPREFIX)* +test-lz4-basic: FPREFIX = tmp-tlb 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 - $(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) -g20KB > $(FPREFIX)-dg20k + $(LZ4) < $(FPREFIX)-dg20k | $(LZ4) -d > $(FPREFIX)-dec + $(DIFF) -q $(FPREFIX)-dg20k $(FPREFIX)-dec + $(LZ4) --no-frame-crc < $(FPREFIX)-dg20k | $(LZ4) -d > $(FPREFIX)-dec + $(DIFF) -q $(FPREFIX)-dg20k $(FPREFIX)-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 - @echo "hello world" > tmp-tlb-hw - $(LZ4) --rm -f tmp-tlb-hw tmp-tlb-hw.lz4 - test ! -f tmp-tlb-hw # must fail (--rm) - test -f tmp-tlb-hw.lz4 - $(PRGDIR)/lz4cat tmp-tlb-hw.lz4 | $(GREP) "hello world" - $(PRGDIR)/unlz4 --rm tmp-tlb-hw.lz4 tmp-tlb-hw - test -f tmp-tlb-hw - test ! -f tmp-tlb-hw.lz4 # must fail (--rm) - test ! -f tmp-tlb-hw.lz4.lz4 # must fail (unlz4) - $(PRGDIR)/lz4cat tmp-tlb-hw # pass-through mode - test -f tmp-tlb-hw - test ! -f tmp-tlb-hw.lz4 # must fail (lz4cat) - $(LZ4) tmp-tlb-hw tmp-tlb-hw.lz4 # creates tmp-tlb-hw.lz4 - $(PRGDIR)/lz4cat < tmp-tlb-hw.lz4 > tmp-tlb3 # checks lz4cat works with stdin (#285) - $(DIFF) -q tmp-tlb-hw tmp-tlb3 - $(PRGDIR)/lz4cat < tmp-tlb-hw > tmp-tlb2 # checks lz4cat works in pass-through mode - $(DIFF) -q tmp-tlb-hw tmp-tlb2 - cp tmp-tlb-hw ./-d + @echo "hello world" > $(FPREFIX)-hw + $(LZ4) --rm -f $(FPREFIX)-hw $(FPREFIX)-hw.lz4 + test ! -f $(FPREFIX)-hw # must fail (--rm) + test -f $(FPREFIX)-hw.lz4 + $(PRGDIR)/lz4cat $(FPREFIX)-hw.lz4 | $(GREP) "hello world" + $(PRGDIR)/unlz4 --rm $(FPREFIX)-hw.lz4 $(FPREFIX)-hw + test -f $(FPREFIX)-hw + test ! -f $(FPREFIX)-hw.lz4 # must fail (--rm) + test ! -f $(FPREFIX)-hw.lz4.lz4 # must fail (unlz4) + $(PRGDIR)/lz4cat $(FPREFIX)-hw # pass-through mode + test -f $(FPREFIX)-hw + test ! -f $(FPREFIX)-hw.lz4 # must fail (lz4cat) + $(LZ4) $(FPREFIX)-hw $(FPREFIX)-hw.lz4 # creates $(FPREFIX)-hw.lz4 + $(PRGDIR)/lz4cat < $(FPREFIX)-hw.lz4 > $(FPREFIX)3 # checks lz4cat works with stdin (#285) + $(DIFF) -q $(FPREFIX)-hw $(FPREFIX)3 + $(PRGDIR)/lz4cat < $(FPREFIX)-hw > $(FPREFIX)2 # checks lz4cat works in pass-through mode + $(DIFF) -q $(FPREFIX)-hw $(FPREFIX)2 + cp $(FPREFIX)-hw ./-d $(LZ4) --rm -- -d -d.lz4 # compresses ./d into ./-d.lz4 test -f ./-d.lz4 test ! -f ./-d mv ./-d.lz4 ./-z - $(LZ4) -d --rm -- -z tmp-tlb4 # uncompresses ./-z into tmp-tlb4 + $(LZ4) -d --rm -- -z $(FPREFIX)4 # uncompresses ./-z into $(FPREFIX)4 test ! -f ./-z - $(DIFF) -q tmp-tlb-hw tmp-tlb4 - ! $(LZ4) tmp-tlb2 tmp-tlb3 tmp-tlb4 # must fail: refuse to handle 3+ file names - $(LZ4) -f tmp-tlb-hw # create tmp-tlb-hw.lz4, for next tests - $(LZ4) --list tmp-tlb-hw.lz4 # test --list on valid single-frame file - $(LZ4) --list < tmp-tlb-hw.lz4 # test --list from stdin (file only) - $(CAT) tmp-tlb-hw >> tmp-tlb-hw.lz4 - ! $(LZ4) -f tmp-tlb-hw.lz4 # uncompress valid frame followed by invalid data (must fail now) - $(LZ4) -BX tmp-tlb-hw -c -q | $(LZ4) -tv # test block checksum + $(DIFF) -q $(FPREFIX)-hw $(FPREFIX)4 + ! $(LZ4) $(FPREFIX)2 $(FPREFIX)3 $(FPREFIX)4 # must fail: refuse to handle 3+ file names + $(LZ4) -f $(FPREFIX)-hw # create $(FPREFIX)-hw.lz4, for next tests + $(LZ4) --list $(FPREFIX)-hw.lz4 # test --list on valid single-frame file + $(LZ4) --list < $(FPREFIX)-hw.lz4 # test --list from stdin (file only) + $(CAT) $(FPREFIX)-hw >> $(FPREFIX)-hw.lz4 + ! $(LZ4) -f $(FPREFIX)-hw.lz4 # uncompress valid frame followed by invalid data (must fail now) + $(LZ4) -BX $(FPREFIX)-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 - ! $(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 + ! $(LZ4) -c --fast=0 $(FPREFIX)-dg20K # lz4 should fail when fast=0 + ! $(LZ4) -c --fast=-1 $(FPREFIX)-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 - $(LZ4) tmp-tlb-test.lz4 tmp-tlb-test2 - $(DIFF) -q tmp-tlb-test tmp-tlb-test2 - @$(RM) tmp-tlb* + @echo "TEST" > $(FPREFIX)-test + $(LZ4) -m $(FPREFIX)-test + $(LZ4) $(FPREFIX)-test.lz4 $(FPREFIX)-test2 + $(DIFF) -q $(FPREFIX)-test $(FPREFIX)-test2 + @$(RM) $(FPREFIX)* +test-lz4-dict: FPREFIX = tmp-dict test-lz4-dict: lz4 datagen @echo "\n ---- test lz4 compression/decompression with dictionary ----" - $(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 - < 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 + $(DATAGEN) -g16KB > $(FPREFIX) + $(DATAGEN) -g32KB > $(FPREFIX)-sample-32k + < $(FPREFIX)-sample-32k $(LZ4) -D $(FPREFIX) | $(LZ4) -dD $(FPREFIX) | diff - $(FPREFIX)-sample-32k + $(DATAGEN) -g128MB > $(FPREFIX)-sample-128m + < $(FPREFIX)-sample-128m $(LZ4) -D $(FPREFIX) | $(LZ4) -dD $(FPREFIX) | diff - $(FPREFIX)-sample-128m + touch $(FPREFIX)-sample-0 + < $(FPREFIX)-sample-0 $(LZ4) -D $(FPREFIX) | $(LZ4) -dD $(FPREFIX) | diff - $(FPREFIX)-sample-0 - < tmp-dict-sample-32k $(LZ4) -D tmp-dict-sample-0 | $(LZ4) -dD tmp-dict-sample-0 | diff - tmp-dict-sample-32k - < tmp-dict-sample-0 $(LZ4) -D tmp-dict-sample-0 | $(LZ4) -dD tmp-dict-sample-0 | diff - tmp-dict-sample-0 + < $(FPREFIX)-sample-32k $(LZ4) -D $(FPREFIX)-sample-0 | $(LZ4) -dD $(FPREFIX)-sample-0 | diff - $(FPREFIX)-sample-32k + < $(FPREFIX)-sample-0 $(LZ4) -D $(FPREFIX)-sample-0 | $(LZ4) -dD $(FPREFIX)-sample-0 | diff - $(FPREFIX)-sample-0 @echo "\n ---- test lz4 dictionary loading ----" - $(DATAGEN) -g128KB > tmp-dict-data-128KB + $(DATAGEN) -g128KB > $(FPREFIX)-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; \ - $(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; \ + $(DATAGEN) -g$$l > $(FPREFIX)-$$l; \ + $(DD) if=$(FPREFIX)-$$l of=$(FPREFIX)-$$l-tail bs=1 count=65536 skip=$$((l > 65536 ? l - 65536 : 0)); \ + < $(FPREFIX)-$$l $(LZ4) -D stdin $(FPREFIX)-data-128KB -c | $(LZ4) -dD $(FPREFIX)-$$l-tail | $(DIFF) - $(FPREFIX)-data-128KB; \ + < $(FPREFIX)-$$l-tail $(LZ4) -D stdin $(FPREFIX)-data-128KB -c | $(LZ4) -dD $(FPREFIX)-$$l | $(DIFF) - $(FPREFIX)-data-128KB; \ done - @$(RM) tmp-dict* + @$(RM) $(FPREFIX)* -test-lz4-hugefile: lz4 datagen +test-lz4hc-hugefile: lz4 datagen + @echo "\n ---- test HC compression/decompression of huge files ----" + $(DATAGEN) -g4200MB | $(LZ4) -v3BD | $(LZ4) -qt + +test-lz4-fast-hugefile: FPREFIX = tmp-lfh +test-lz4-fast-hugefile: lz4 datagen @echo "\n ---- test huge files compression/decompression ----" - ./datagen -g6GB | $(LZ4) -vB5D | $(LZ4) -qt - ./datagen -g4500MB | $(LZ4) -v3BD | $(LZ4) -qt + $(DATAGEN) -g6GB | $(LZ4) -vB5D | $(LZ4) -qt # test large file size [2-4] GB - @$(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 - @ls -ls tmphf2 - $(DIFF) -s tmphf1 tmphf2 - @$(RM) tmphf* + @$(DATAGEN) -g3G -P100 | $(LZ4) -vv | $(LZ4) --decompress --force --sparse - $(FPREFIX)1 + @ls -ls $(FPREFIX)1 + @$(DATAGEN) -g3G -P100 | $(LZ4) --quiet --content-size | $(LZ4) --verbose --decompress --force --sparse - $(FPREFIX)2 + @ls -ls $(FPREFIX)2 + $(DIFF) -s $(FPREFIX)1 $(FPREFIX)2 + @$(RM) $(FPREFIX)* + +test-lz4-hugefile: test-lz4-fast-hugefile test-lz4hc-hugefile +test-lz4-testmode: FPREFIX = tmp-ltm test-lz4-testmode: lz4 datagen @echo "\n ---- bench mode ----" $(LZ4) -bi0 @@ -459,21 +480,21 @@ test-lz4-testmode: lz4 datagen ! $(DATAGEN) | $(LZ4) -t ! $(DATAGEN) | $(LZ4) -tf @echo "\n ---- pass-through mode ----" - @echo "Why hello there " > tmp-tlt2.lz4 - ! $(LZ4) -f tmp-tlt2.lz4 > $(VOID) + @echo "Why hello there " > $(FPREFIX)2.lz4 + ! $(LZ4) -f $(FPREFIX)2.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 - $(LZ4) -dcfm tmp-tlt1 tmp-tlt2 - @echo "\n ---- non-existing source ----" + @echo "Hello World !" > $(FPREFIX)1 + $(LZ4) -dcf $(FPREFIX)1 + @echo "from underground..." > $(FPREFIX)2 + $(LZ4) -dcfm $(FPREFIX)1 $(FPREFIX)2 + @echo "\n ---- non-existing source (must fail cleanly) ----" ! $(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 + @$(RM) $(FPREFIX)* test-lz4-opt-parser: lz4 datagen @echo "\n ---- test opt-parser ----" @@ -492,16 +513,15 @@ test-lz4-opt-parser: lz4 datagen 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* test-lz4: lz4 datagen test-lz4-essentials test-lz4-opt-parser \ test-lz4-sparse test-lz4-hugefile test-lz4-dict \ test-lz4-skippable - @$(RM) tmp* +test-lz4c: LZ4C = $(LZ4)c test-lz4c: lz4c datagen @echo "\n ---- test lz4c variant ----" - $(DATAGEN) -g256MB | $(LZ4)c -l -v | $(LZ4)c -t + $(DATAGEN) -g256MB | $(LZ4C) -l -v | $(LZ4C) -t test-lz4c32: CFLAGS+=-m32 test-lz4c32: test-lz4 @@ -550,25 +570,27 @@ test-frametest: frametest test-frametest32: CFLAGS += -m32 test-frametest32: test-frametest +VALGRIND = valgrind --leak-check=yes --error-exitcode=1 +test-mem: FPREFIX = tmp-tvm 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 $(LZ4) -9 -BD -f ftmdg16K $(VOID) - $(DATAGEN) -g16KB -s2 > ftmdg16K2 - $(DATAGEN) -g16KB -s3 > ftmdg16K3 - valgrind --leak-check=yes --error-exitcode=1 $(LZ4) --force --multiple ftmdg16K ftmdg16K2 ftmdg16K3 - $(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 - valgrind --leak-check=yes --error-exitcode=1 ./fullbench -i1 ftmdg7M ftmdg16K2 - valgrind --leak-check=yes --error-exitcode=1 $(LZ4) -B4D -f -vq ftmdg7M $(VOID) - valgrind --leak-check=yes --error-exitcode=1 $(LZ4) --list -m ftm*.lz4 - valgrind --leak-check=yes --error-exitcode=1 $(LZ4) --list -m -v ftm*.lz4 - $(RM) ftm* - valgrind --leak-check=yes --error-exitcode=1 ./fuzzer -i64 -t1 - valgrind --leak-check=yes --error-exitcode=1 ./frametest -i256 + $(VALGRIND) $(DATAGEN) -g50M > $(VOID) + $(DATAGEN) -g16KB > $(FPREFIX)dg16K + $(VALGRIND) $(LZ4) -9 -BD -f $(FPREFIX)dg16K $(VOID) + $(DATAGEN) -g16KB -s2 > $(FPREFIX)dg16K2 + $(DATAGEN) -g16KB -s3 > $(FPREFIX)dg16K3 + $(VALGRIND) $(LZ4) --force --multiple $(FPREFIX)dg16K $(FPREFIX)dg16K2 $(FPREFIX)dg16K3 + $(DATAGEN) -g7MB > $(FPREFIX)dg7M + $(VALGRIND) $(LZ4) -9 -B5D -f $(FPREFIX)dg7M $(FPREFIX)dg16K2 + $(VALGRIND) $(LZ4) -t $(FPREFIX)dg16K2 + $(VALGRIND) $(LZ4) -bi1 $(FPREFIX)dg7M + $(VALGRIND) ./fullbench -i1 $(FPREFIX)dg7M $(FPREFIX)dg16K2 + $(VALGRIND) $(LZ4) -B4D -f -vq $(FPREFIX)dg7M $(VOID) + $(VALGRIND) $(LZ4) --list -m $(FPREFIX)*.lz4 + $(VALGRIND) $(LZ4) --list -m -v $(FPREFIX)*.lz4 + $(RM) $(FPREFIX)* + $(VALGRIND) ./fuzzer -i64 -t1 + $(VALGRIND) ./frametest -i256 test-mem32: lz4c32 datagen # unfortunately, valgrind doesn't seem to work with non-native binary... -- cgit v0.12 From 1db2e64a0de0a19d4e69fc49da60eb31fc6dda72 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sun, 17 Jul 2022 21:44:32 +0200 Subject: update CI `make test` to employ `-j` for faster parallel processing --- .github/workflows/ci.yml | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0e6d69c..1e8f1e6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -117,7 +117,7 @@ jobs: - name: make -C tests test-lz4 if: always() - run: MOREFLAGS='-Werror' make V=1 -C tests clean test-lz4 + run: make clean; MOREFLAGS='-Werror' make -j V=1 -C tests test-lz4 - name: make clangtest (clang only) if: ${{ startsWith( matrix.cc , 'clang' ) }} @@ -125,7 +125,7 @@ jobs: - name: make -C tests test MOREFLAGS='-mx32' if: ${{ matrix.x32 == 'true' }} - run: LDFLAGS='-Wl,--verbose' MOREFLAGS='-mx32' make V=1 -C tests clean test + run: make clean; LDFLAGS='-Wl,--verbose' MOREFLAGS='-mx32' make -j V=1 -C tests test - name: make -C tests test-lz4c32 if: ${{ matrix.x86 == 'true' }} @@ -139,11 +139,11 @@ jobs: - name: make -C tests test MOREFLAGS='-mx32' || echo Ignore failure for now. if: ${{ matrix.x32 == 'fail' }} - run: LDFLAGS='-Wl,--verbose' MOREFLAGS='-mx32' make V=1 -C tests clean test || $FIXME__LZ4_CI_IGNORE + run: make clean; LDFLAGS='-Wl,--verbose' MOREFLAGS='-mx32' make -j V=1 -C tests test || $FIXME__LZ4_CI_IGNORE - name: make -C tests test-lz4c32 || echo Ignore failure for now. if: ${{ matrix.x86 == 'fail' }} - run: LDFLAGS='-Wl,--verbose' MOREFLAGS='-Werror' make V=1 -C tests clean test-lz4c32 || $FIXME__LZ4_CI_IGNORE + run: make clean; LDFLAGS='-Wl,--verbose' MOREFLAGS='-Werror' make V=1 -C tests test-lz4c32 || $FIXME__LZ4_CI_IGNORE # # ############################################################### @@ -171,10 +171,10 @@ jobs: sudo apt-get install gcc-multilib - name: benchmark (-C tests test-lz4) - run: make V=1 -C tests test-lz4 + run: make -j V=1 -C tests test-lz4 - name: benchmark (-C tests test-lz4c) - run: make V=1 -C tests test-lz4c + run: make -j V=1 -C tests test-lz4c - name: benchmark (-C tests test-lz4c32) run: make V=1 -C tests test-lz4c32 @@ -589,11 +589,11 @@ jobs: run: CFLAGS="-Werror" make V=1 clean default - name: make test - run: make V=1 clean test MOREFLAGS='-Werror -Wconversion -Wno-sign-conversion' + run: make clean; make -j V=1 test MOREFLAGS='-Werror -Wconversion -Wno-sign-conversion' - name: Ensure `make test` doesn't depend on the status of the console # see issue #990 for detailed explanations - run: make test > /dev/null + run: make -j test > /dev/null ############################################################### -- cgit v0.12 From 24803605c912a5246d5d5fc6cbeebba064ae01e8 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sun, 17 Jul 2022 21:47:10 +0200 Subject: add dedicated install test per platform --- .github/workflows/ci.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1e8f1e6..3101d4f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -87,6 +87,10 @@ jobs: if: always() run: make V=1 + - name: install test + if: always() + run: make clean; make V=1 -C tests test-install + - name: make all if: always() run: make V=1 clean all -- cgit v0.12 From 598eafde2de0227c8b54e1a630c6746567db37e2 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 29 Jul 2022 14:34:53 +0200 Subject: minor : fix conversion warnings --- programs/bench.c | 50 +++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/programs/bench.c b/programs/bench.c index 603705c..242946d 100644 --- a/programs/bench.c +++ b/programs/bench.c @@ -267,7 +267,7 @@ static clock_t g_time = 0; # define DEBUG 0 #endif #define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__); -#define EXM_THROW(error, ...) \ +#define END_PROCESS(error, ...) \ { \ DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \ DISPLAYLEVEL(1, "Error %i : ", error); \ @@ -324,7 +324,7 @@ static int BMK_benchMem(const void* srcBuffer, size_t srcSize, size_t const blockSize = (g_blockSize>=32 ? g_blockSize : srcSize) + (!srcSize) /* avoid div by 0 */ ; U32 const maxNbBlocks = (U32) ((srcSize + (blockSize-1)) / blockSize) + nbFiles; blockParam_t* const blockTable = (blockParam_t*) malloc(maxNbBlocks * sizeof(blockParam_t)); - size_t const maxCompressedSize = LZ4_compressBound((int)srcSize) + (maxNbBlocks * 1024); /* add some room for safety */ + size_t const maxCompressedSize = (size_t)LZ4_compressBound((int)srcSize) + (maxNbBlocks * 1024); /* add some room for safety */ void* const compressedBuffer = malloc(maxCompressedSize); void* const resultBuffer = malloc(srcSize); U32 nbBlocks; @@ -332,7 +332,7 @@ static int BMK_benchMem(const void* srcBuffer, size_t srcSize, /* checks */ if (!compressedBuffer || !resultBuffer || !blockTable) - EXM_THROW(31, "allocation error : not enough memory"); + END_PROCESS(31, "allocation error : not enough memory"); if (strlen(displayName)>17) displayName += strlen(displayName)-17; /* can only display 17 characters */ @@ -408,7 +408,7 @@ static int BMK_benchMem(const void* srcBuffer, size_t srcSize, &compP, blockTable[blockNb].srcPtr, blockTable[blockNb].cPtr, (int)blockTable[blockNb].srcSize, (int)blockTable[blockNb].cRoom); - if (LZ4_isError(rSize)) EXM_THROW(1, "LZ4 compression failed"); + if (LZ4_isError(rSize)) END_PROCESS(1, "LZ4 compression failed"); blockTable[blockNb].cSize = rSize; } } { U64 const clockSpan = UTIL_clockSpanNano(clockStart); @@ -594,21 +594,21 @@ static void BMK_loadFiles(void* buffer, size_t bufferSize, continue; } f = fopen(fileNamesTable[n], "rb"); - if (f==NULL) EXM_THROW(10, "impossible to open file %s", fileNamesTable[n]); + if (f==NULL) END_PROCESS(10, "impossible to open file %s", fileNamesTable[n]); DISPLAYUPDATE(2, "Loading %s... \r", fileNamesTable[n]); if (fileSize > bufferSize-pos) { /* buffer too small - stop after this file */ fileSize = bufferSize-pos; nbFiles=n; } { size_t const readSize = fread(((char*)buffer)+pos, 1, (size_t)fileSize, f); - if (readSize != (size_t)fileSize) EXM_THROW(11, "could not read %s", fileNamesTable[n]); + if (readSize != (size_t)fileSize) END_PROCESS(11, "could not read %s", fileNamesTable[n]); pos += readSize; } fileSizes[n] = (size_t)fileSize; totalSize += (size_t)fileSize; fclose(f); } - if (totalSize == 0) EXM_THROW(12, "no data to bench"); + if (totalSize == 0) END_PROCESS(12, "no data to bench"); } static void BMK_benchFileTable(const char** fileNamesTable, unsigned nbFiles, @@ -621,11 +621,11 @@ static void BMK_benchFileTable(const char** fileNamesTable, unsigned nbFiles, U64 const totalSizeToLoad = UTIL_getTotalFileSize(fileNamesTable, nbFiles); char mfName[20] = {0}; - if (!fileSizes) EXM_THROW(12, "not enough memory for fileSizes"); + if (!fileSizes) END_PROCESS(12, "not enough memory for fileSizes"); /* Memory allocation & restrictions */ benchedSize = BMK_findMaxMem(totalSizeToLoad * 3) / 3; - if (benchedSize==0) EXM_THROW(12, "not enough memory"); + if (benchedSize==0) END_PROCESS(12, "not enough memory"); if ((U64)benchedSize > totalSizeToLoad) benchedSize = (size_t)totalSizeToLoad; if (benchedSize > LZ4_MAX_INPUT_SIZE) { benchedSize = LZ4_MAX_INPUT_SIZE; @@ -635,7 +635,7 @@ static void BMK_benchFileTable(const char** fileNamesTable, unsigned nbFiles, DISPLAY("Not enough memory; testing %u MB only...\n", (U32)(benchedSize >> 20)); } srcBuffer = malloc(benchedSize + !benchedSize); /* avoid alloc of zero */ - if (!srcBuffer) EXM_THROW(12, "not enough memory"); + if (!srcBuffer) END_PROCESS(12, "not enough memory"); /* Load input buffer */ BMK_loadFiles(srcBuffer, benchedSize, fileSizes, fileNamesTable, nbFiles); @@ -663,7 +663,7 @@ static void BMK_syntheticTest(int cLevel, int cLevelLast, double compressibility void* const srcBuffer = malloc(benchedSize); /* Memory allocation */ - if (!srcBuffer) EXM_THROW(21, "not enough memory"); + if (!srcBuffer) END_PROCESS(21, "not enough memory"); /* Fill input buffer */ RDG_genBuffer(srcBuffer, benchedSize, compressibility, 0.0, 0); @@ -700,7 +700,7 @@ int BMK_benchFiles(const char** fileNamesTable, unsigned nbFiles, { double const compressibility = (double)g_compressibilityDefault / 100; char* dictBuf = NULL; - int dictSize = 0; + size_t dictSize = 0; if (cLevel > LZ4HC_CLEVEL_MAX) cLevel = LZ4HC_CLEVEL_MAX; if (cLevelLast > LZ4HC_CLEVEL_MAX) cLevelLast = LZ4HC_CLEVEL_MAX; @@ -709,36 +709,36 @@ int BMK_benchFiles(const char** fileNamesTable, unsigned nbFiles, if (dictFileName) { FILE* dictFile = NULL; - U64 dictFileSize = UTIL_getFileSize(dictFileName); - if (!dictFileSize) EXM_THROW(25, "Dictionary error : could not stat dictionary file"); + U64 const dictFileSize = UTIL_getFileSize(dictFileName); + if (!dictFileSize) END_PROCESS(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 (!dictFile) END_PROCESS(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"); + if (UTIL_fseek(dictFile, (long)(dictFileSize - dictSize), SEEK_SET)) + END_PROCESS(25, "Dictionary error : could not seek dictionary file"); } else { - dictSize = (int)dictFileSize; + dictSize = (size_t)dictFileSize; } - dictBuf = (char *)malloc(dictSize); - if (!dictBuf) EXM_THROW(25, "Allocation error : not enough memory"); + dictBuf = (char*)malloc(dictSize); + if (!dictBuf) END_PROCESS(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"); + if (fread(dictBuf, 1, dictSize, dictFile) != dictSize) + END_PROCESS(25, "Dictionary error : could not read dictionary file"); fclose(dictFile); } if (nbFiles == 0) - BMK_syntheticTest(cLevel, cLevelLast, compressibility, dictBuf, dictSize); + BMK_syntheticTest(cLevel, cLevelLast, compressibility, dictBuf, (int)dictSize); else { if (g_benchSeparately) - BMK_benchFilesSeparately(fileNamesTable, nbFiles, cLevel, cLevelLast, dictBuf, dictSize); + BMK_benchFilesSeparately(fileNamesTable, nbFiles, cLevel, cLevelLast, dictBuf, (int)dictSize); else - BMK_benchFileTable(fileNamesTable, nbFiles, cLevel, cLevelLast, dictBuf, dictSize); + BMK_benchFileTable(fileNamesTable, nbFiles, cLevel, cLevelLast, dictBuf, (int)dictSize); } free(dictBuf); -- cgit v0.12 From df4bb410e31b913328687bc8b04a3f39f79efc8d Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 29 Jul 2022 14:51:52 +0200 Subject: updated documentation of bench unit --- programs/bench.c | 4 ++-- programs/bench.h | 25 +++++++++++++++++++------ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/programs/bench.c b/programs/bench.c index 242946d..85bf87a 100644 --- a/programs/bench.c +++ b/programs/bench.c @@ -677,7 +677,8 @@ static void BMK_syntheticTest(int cLevel, int cLevelLast, double compressibility } -int BMK_benchFilesSeparately(const char** fileNamesTable, unsigned nbFiles, +static int +BMK_benchFilesSeparately(const char** fileNamesTable, unsigned nbFiles, int cLevel, int cLevelLast, const char* dictBuf, int dictSize) { @@ -685,7 +686,6 @@ int BMK_benchFilesSeparately(const char** fileNamesTable, unsigned nbFiles, if (cLevel > 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); for (fileNb=0; fileNb +/* BMK_benchFiles() : + * Benchmark all files provided through array @fileNamesTable. + * All files must be valid, otherwise benchmark fails. + * Roundtrip measurements are done for each file individually, but + * unless BMK_setBenchSeparately() is set, all results are agglomerated. + * The method benchmarks all compression levels from @cLevelStart to @cLevelLast, + * both inclusive, providing one result per compression level. + * If @cLevelLast <= @cLevelStart, BMK_benchFiles() benchmarks @cLevelStart only. + * @dictFileName is optional, it's possible to provide NULL. + * When provided, compression and decompression use the specified file as dictionary. + * Only one dictionary can be provided, in which case it's applied to all benchmarked files. +**/ int BMK_benchFiles(const char** fileNamesTable, unsigned nbFiles, - int cLevel, int cLevelLast, + int cLevelStart, int cLevelLast, const char* dictFileName); /* Set Parameters */ -void BMK_setNbSeconds(unsigned nbLoops); -void BMK_setBlockSize(size_t blockSize); -void BMK_setAdditionalParam(int additionalParam); -void BMK_setNotificationLevel(unsigned level); -void BMK_setBenchSeparately(int separate); +void BMK_setNbSeconds(unsigned nbSeconds); /* minimum benchmark duration, in seconds, for both compression and decompression */ +void BMK_setBlockSize(size_t blockSize); /* Internally cut input file(s) into independent blocks of specified size */ +void BMK_setNotificationLevel(unsigned level); /* Influence verbosity level */ +void BMK_setBenchSeparately(int separate); /* When providing multiple files, output one result per file */ + +void BMK_setAdditionalParam(int additionalParam); /* hidden param, influence output format, for python parsing */ #endif /* BENCH_H_125623623633 */ -- cgit v0.12 From cd96e3e7a504311cd229cf536ede86e5febec9a7 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 29 Jul 2022 15:24:50 +0200 Subject: minor refactor to prepare bench.c for multiple decoding functions. --- lib/lz4.h | 21 ++++- lib/lz4frame.h | 69 ++++++++-------- programs/bench.c | 245 ++++++++++++++++++++++++++++--------------------------- 3 files changed, 176 insertions(+), 159 deletions(-) diff --git a/lib/lz4.h b/lib/lz4.h index 7081e76..383dc07 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -417,7 +417,10 @@ LZ4LIB_API int LZ4_decoderRingBufferSize(int maxBlockSize); * save the last 64KB of decoded data into a safe buffer where it can't be modified during decompression, * 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_safe_continue (LZ4_streamDecode_t* LZ4_streamDecode, + const char* src, char* dst, + int srcSize, int dstCapacity); /*! LZ4_decompress_*_usingDict() : @@ -428,9 +431,17 @@ LZ4LIB_API int LZ4_decompress_safe_continue (LZ4_streamDecode_t* LZ4_streamDecod * 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_safe_usingDict(const char* src, char* dst, + int srcSize, int dstCapcity, + const char* dictStart, int dictSize); + +LZ4LIB_API int +LZ4_decompress_safe_partial_usingDict(const char* src, char* dst, + int compressedSize, + int targetOutputSize, int maxOutputSize, + const char* dictStart, int dictSize); -LZ4LIB_API int LZ4_decompress_safe_partial_usingDict(const char* source, char* dest, int compressedSize, int targetOutputSize, int maxOutputSize, const char* dictStart, int dictSize); #endif /* LZ4_H_2983827168210 */ @@ -508,7 +519,9 @@ LZ4LIB_STATIC_API int LZ4_compress_fast_extState_fastReset (void* state, const c * stream (and source buffer) must remain in-place / accessible / unchanged * through the completion of the first compression call on the stream. */ -LZ4LIB_STATIC_API void LZ4_attach_dictionary(LZ4_stream_t* workingStream, const LZ4_stream_t* dictionaryStream); +LZ4LIB_STATIC_API void +LZ4_attach_dictionary(LZ4_stream_t* workingStream, + const LZ4_stream_t* dictionaryStream); /*! In-place compression and decompression diff --git a/lib/lz4frame.h b/lib/lz4frame.h index a864ef9..c75fd4d 100644 --- a/lib/lz4frame.h +++ b/lib/lz4frame.h @@ -56,9 +56,9 @@ extern "C" { /** Introduction - lz4frame.h implements LZ4 frame specification (doc/lz4_Frame_format.md). - lz4frame.h provides frame compression functions that take care - of encoding standard metadata alongside LZ4-compressed blocks. + lz4frame.h implements LZ4 frame specification: see doc/lz4_Frame_format.md . + LZ4 Frames are interoperable on any systems. + The are compatible with `lz4` CLI. */ /*-*************************************************************** @@ -397,7 +397,7 @@ LZ4FLIB_API 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: user can call LZ4F_decompress() directly. + * Its usage is optional: user can also invoke LZ4F_decompress() directly. * * Extracted information will fill an existing LZ4F_frameInfo_t structure. * This can be useful for allocation and dictionary identification purposes. @@ -438,9 +438,10 @@ LZ4FLIB_API size_t LZ4F_headerSize(const void* src, size_t srcSize); * 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. */ -LZ4FLIB_API size_t LZ4F_getFrameInfo(LZ4F_dctx* dctx, - LZ4F_frameInfo_t* frameInfoPtr, - const void* srcBuffer, size_t* srcSizePtr); +LZ4FLIB_API size_t +LZ4F_getFrameInfo(LZ4F_dctx* dctx, + LZ4F_frameInfo_t* frameInfoPtr, + const void* srcBuffer, size_t* srcSizePtr); /*! LZ4F_decompress() : * Call this function repetitively to regenerate data compressed in `srcBuffer`. @@ -473,10 +474,11 @@ LZ4FLIB_API size_t LZ4F_getFrameInfo(LZ4F_dctx* dctx, * * After a frame is fully decoded, dctx can be used again to decompress another frame. */ -LZ4FLIB_API size_t LZ4F_decompress(LZ4F_dctx* dctx, - void* dstBuffer, size_t* dstSizePtr, - const void* srcBuffer, size_t* srcSizePtr, - const LZ4F_decompressOptions_t* dOptPtr); +LZ4FLIB_API size_t +LZ4F_decompress(LZ4F_dctx* dctx, + void* dstBuffer, size_t* dstSizePtr, + const void* srcBuffer, size_t* srcSizePtr, + const LZ4F_decompressOptions_t* dOptPtr); /*! LZ4F_resetDecompressionContext() : added in v1.8.0 @@ -572,10 +574,11 @@ LZ4FLIB_STATIC_API size_t LZ4F_getBlockSize(LZ4F_blockSizeID_t blockSizeID); * @return : number of bytes written into `dstBuffer` (it can be zero, meaning input data was just buffered). * or an error code if it fails (which can be tested using LZ4F_isError()) */ -LZ4FLIB_STATIC_API size_t LZ4F_uncompressedUpdate(LZ4F_cctx* cctx, - void* dstBuffer, size_t dstCapacity, - const void* srcBuffer, size_t srcSize, - const LZ4F_compressOptions_t* cOptPtr); +LZ4FLIB_STATIC_API size_t +LZ4F_uncompressedUpdate(LZ4F_cctx* cctx, + void* dstBuffer, size_t dstCapacity, + const void* srcBuffer, size_t srcSize, + const LZ4F_compressOptions_t* cOptPtr); /********************************** * Bulk processing dictionary API @@ -619,12 +622,12 @@ LZ4FLIB_STATIC_API void LZ4F_freeCDict(LZ4F_CDict* CDict); * but it's not recommended, as it's the only way to provide dictID in the frame header. * @return : number of bytes written into dstBuffer. * or an error code if it fails (can be tested using LZ4F_isError()) */ -LZ4FLIB_STATIC_API size_t LZ4F_compressFrame_usingCDict( - LZ4F_cctx* cctx, - void* dst, size_t dstCapacity, - const void* src, size_t srcSize, - const LZ4F_CDict* cdict, - const LZ4F_preferences_t* preferencesPtr); +LZ4FLIB_STATIC_API size_t +LZ4F_compressFrame_usingCDict(LZ4F_cctx* cctx, + void* dst, size_t dstCapacity, + const void* src, size_t srcSize, + const LZ4F_CDict* cdict, + const LZ4F_preferences_t* preferencesPtr); /*! LZ4F_compressBegin_usingCDict() : @@ -634,23 +637,23 @@ LZ4FLIB_STATIC_API size_t LZ4F_compressFrame_usingCDict( * however, it's the only way to provide dictID in the frame header. * @return : number of bytes written into dstBuffer for the header, * or an error code (which can be tested using LZ4F_isError()) */ -LZ4FLIB_STATIC_API size_t LZ4F_compressBegin_usingCDict( - LZ4F_cctx* cctx, - void* dstBuffer, size_t dstCapacity, - const LZ4F_CDict* cdict, - const LZ4F_preferences_t* prefsPtr); +LZ4FLIB_STATIC_API size_t +LZ4F_compressBegin_usingCDict(LZ4F_cctx* cctx, + void* dstBuffer, size_t dstCapacity, + const LZ4F_CDict* cdict, + const LZ4F_preferences_t* prefsPtr); /*! LZ4F_decompress_usingDict() : * Same as LZ4F_decompress(), using a predefined dictionary. * Dictionary is used "in place", without any preprocessing. - * It must remain accessible throughout the entire frame decoding. */ -LZ4FLIB_STATIC_API size_t LZ4F_decompress_usingDict( - LZ4F_dctx* dctxPtr, - void* dstBuffer, size_t* dstSizePtr, - const void* srcBuffer, size_t* srcSizePtr, - const void* dict, size_t dictSize, - const LZ4F_decompressOptions_t* decompressOptionsPtr); +** It must remain accessible throughout the entire frame decoding. */ +LZ4FLIB_STATIC_API size_t +LZ4F_decompress_usingDict(LZ4F_dctx* dctxPtr, + void* dstBuffer, size_t* dstSizePtr, + const void* srcBuffer, size_t* srcSizePtr, + const void* dict, size_t dictSize, + const LZ4F_decompressOptions_t* decompressOptionsPtr); /*! Custom memory allocation : diff --git a/programs/bench.c b/programs/bench.c index 85bf87a..633433f 100644 --- a/programs/bench.c +++ b/programs/bench.c @@ -54,7 +54,91 @@ /* ************************************* -* Compression parameters and functions +* Constants +***************************************/ +#ifndef LZ4_GIT_COMMIT_STRING +# define LZ4_GIT_COMMIT_STRING "" +#else +# define LZ4_GIT_COMMIT_STRING LZ4_EXPAND_AND_QUOTE(LZ4_GIT_COMMIT) +#endif + +#define NBSECONDS 3 +#define TIMELOOP_MICROSEC 1*1000000ULL /* 1 second */ +#define TIMELOOP_NANOSEC 1*1000000000ULL /* 1 second */ +#define ACTIVEPERIOD_MICROSEC 70*1000000ULL /* 70 seconds */ +#define COOLPERIOD_SEC 10 +#define DECOMP_MULT 1 /* test decompression DECOMP_MULT times longer than compression */ + +#define KB *(1 <<10) +#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; + + +/* ************************************* +* console display +***************************************/ +#define DISPLAY(...) fprintf(stderr, __VA_ARGS__) +#define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); } +static U32 g_displayLevel = 2; /* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : + progression; 4 : + information */ + +#define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \ + if ((clock() - g_time > refreshRate) || (g_displayLevel>=4)) \ + { g_time = clock(); DISPLAY(__VA_ARGS__); \ + if (g_displayLevel>=4) fflush(stdout); } } +static const clock_t refreshRate = CLOCKS_PER_SEC * 15 / 100; +static clock_t g_time = 0; + + +/* ************************************* +* DEBUG and error conditions +***************************************/ +#ifndef DEBUG +# define DEBUG 0 +#endif +#define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__); +#define END_PROCESS(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); \ +} + +#define LZ4_isError(errcode) (errcode==0) + + +/* ************************************* +* Benchmark Parameters +***************************************/ +static U32 g_nbSeconds = NBSECONDS; +static size_t g_blockSize = 0; +int g_additionalParam = 0; +int g_benchSeparately = 0; + +void BMK_setNotificationLevel(unsigned level) { g_displayLevel=level; } + +void BMK_setAdditionalParam(int additionalParam) { g_additionalParam=additionalParam; } + +void BMK_setNbSeconds(unsigned nbSeconds) +{ + g_nbSeconds = nbSeconds; + DISPLAYLEVEL(3, "- test >= %u seconds per compression / decompression -\n", g_nbSeconds); +} + +void BMK_setBlockSize(size_t blockSize) { g_blockSize = blockSize; } + +void BMK_setBenchSeparately(int separate) { g_benchSeparately = (separate!=0); } + + +/* ************************************* + * Compression state management ***************************************/ struct compressionParameters @@ -79,8 +163,8 @@ struct compressionParameters const struct compressionParameters* pThis); }; -static void LZ4_compressInitNoStream( - struct compressionParameters* pThis) +static void +LZ4_compressInitNoStream(struct compressionParameters* pThis) { pThis->LZ4_stream = NULL; pThis->LZ4_dictStream = NULL; @@ -88,8 +172,8 @@ static void LZ4_compressInitNoStream( pThis->LZ4_dictStreamHC = NULL; } -static void LZ4_compressInitStream( - struct compressionParameters* pThis) +static void +LZ4_compressInitStream(struct compressionParameters* pThis) { pThis->LZ4_stream = LZ4_createStream(); pThis->LZ4_dictStream = LZ4_createStream(); @@ -98,8 +182,8 @@ static void LZ4_compressInitStream( LZ4_loadDict(pThis->LZ4_dictStream, pThis->dictBuf, pThis->dictSize); } -static void LZ4_compressInitStreamHC( - struct compressionParameters* pThis) +static void +LZ4_compressInitStreamHC(struct compressionParameters* pThis) { pThis->LZ4_stream = NULL; pThis->LZ4_dictStream = NULL; @@ -108,83 +192,84 @@ static void LZ4_compressInitStreamHC( LZ4_loadDictHC(pThis->LZ4_dictStreamHC, pThis->dictBuf, pThis->dictSize); } -static void LZ4_compressResetNoStream( - const struct compressionParameters* pThis) +static void +LZ4_compressResetNoStream(const struct compressionParameters* pThis) { (void)pThis; } -static void LZ4_compressResetStream( - const struct compressionParameters* 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) +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) +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) +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) +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) +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) +static void +LZ4_compressCleanupNoStream(const struct compressionParameters* pThis) { (void)pThis; } -static void LZ4_compressCleanupStream( - const struct compressionParameters* 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) +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) +static void +LZ4_buildCompressionParameters(struct compressionParameters* pParams, + int cLevel, + const char* dictBuf, int dictSize) { pParams->cLevel = cLevel; pParams->dictBuf = dictBuf; @@ -215,90 +300,6 @@ static void LZ4_buildCompressionParameters( } } -#define LZ4_isError(errcode) (errcode==0) - - -/* ************************************* -* Constants -***************************************/ -#ifndef LZ4_GIT_COMMIT_STRING -# define LZ4_GIT_COMMIT_STRING "" -#else -# define LZ4_GIT_COMMIT_STRING LZ4_EXPAND_AND_QUOTE(LZ4_GIT_COMMIT) -#endif - -#define NBSECONDS 3 -#define TIMELOOP_MICROSEC 1*1000000ULL /* 1 second */ -#define TIMELOOP_NANOSEC 1*1000000000ULL /* 1 second */ -#define ACTIVEPERIOD_MICROSEC 70*1000000ULL /* 70 seconds */ -#define COOLPERIOD_SEC 10 -#define DECOMP_MULT 1 /* test decompression DECOMP_MULT times longer than compression */ - -#define KB *(1 <<10) -#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; - - -/* ************************************* -* console display -***************************************/ -#define DISPLAY(...) fprintf(stderr, __VA_ARGS__) -#define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); } -static U32 g_displayLevel = 2; /* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : + progression; 4 : + information */ - -#define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \ - if ((clock() - g_time > refreshRate) || (g_displayLevel>=4)) \ - { g_time = clock(); DISPLAY(__VA_ARGS__); \ - if (g_displayLevel>=4) fflush(stdout); } } -static const clock_t refreshRate = CLOCKS_PER_SEC * 15 / 100; -static clock_t g_time = 0; - - -/* ************************************* -* Exceptions -***************************************/ -#ifndef DEBUG -# define DEBUG 0 -#endif -#define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__); -#define END_PROCESS(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); \ -} - - -/* ************************************* -* Benchmark Parameters -***************************************/ -static U32 g_nbSeconds = NBSECONDS; -static size_t g_blockSize = 0; -int g_additionalParam = 0; -int g_benchSeparately = 0; - -void BMK_setNotificationLevel(unsigned level) { g_displayLevel=level; } - -void BMK_setAdditionalParam(int additionalParam) { g_additionalParam=additionalParam; } - -void BMK_setNbSeconds(unsigned nbSeconds) -{ - g_nbSeconds = nbSeconds; - DISPLAYLEVEL(3, "- test >= %u seconds per compression / decompression -\n", g_nbSeconds); -} - -void BMK_setBlockSize(size_t blockSize) { g_blockSize = blockSize; } - -void BMK_setBenchSeparately(int separate) { g_benchSeparately = (separate!=0); } - /* ******************************************************** * Bench functions -- cgit v0.12 From 4f4d09a0d1604da4a93d05716e586d73231c2e63 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 29 Jul 2022 18:57:36 +0200 Subject: implement decoder-only benchmark mode requires an LZ4 Frame as input --- lib/lz4.h | 2 +- lib/lz4frame.h | 12 ++--- programs/bench.c | 145 +++++++++++++++++++++++++++++++++++++++++------------- programs/bench.h | 1 + programs/lz4cli.c | 13 +++-- tests/Makefile | 3 ++ tests/fullbench.c | 3 +- 7 files changed, 135 insertions(+), 44 deletions(-) diff --git a/lib/lz4.h b/lib/lz4.h index 383dc07..fee8890 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -433,7 +433,7 @@ LZ4_decompress_safe_continue (LZ4_streamDecode_t* LZ4_streamDecode, */ LZ4LIB_API int LZ4_decompress_safe_usingDict(const char* src, char* dst, - int srcSize, int dstCapcity, + int srcSize, int dstCapacity, const char* dictStart, int dictSize); LZ4LIB_API int diff --git a/lib/lz4frame.h b/lib/lz4frame.h index c75fd4d..1c2d0dd 100644 --- a/lib/lz4frame.h +++ b/lib/lz4frame.h @@ -54,12 +54,12 @@ extern "C" { /** - Introduction - - lz4frame.h implements LZ4 frame specification: see doc/lz4_Frame_format.md . - LZ4 Frames are interoperable on any systems. - The are compatible with `lz4` CLI. -*/ + * Introduction + * + * lz4frame.h implements LZ4 frame specification: see doc/lz4_Frame_format.md . + * LZ4 Frames are compatible with `lz4` CLI, + * and designed to be interoperable with any system. +**/ /*-*************************************************************** * Compiler specifics diff --git a/programs/bench.c b/programs/bench.c index 633433f..7836717 100644 --- a/programs/bench.c +++ b/programs/bench.c @@ -51,6 +51,7 @@ #include "lz4.h" #define LZ4_HC_STATIC_LINKING_ONLY #include "lz4hc.h" +#include "lz4frame.h" /* LZ4F_decompress */ /* ************************************* @@ -121,6 +122,7 @@ static U32 g_nbSeconds = NBSECONDS; static size_t g_blockSize = 0; int g_additionalParam = 0; int g_benchSeparately = 0; +int g_decodeOnly = 0; void BMK_setNotificationLevel(unsigned level) { g_displayLevel=level; } @@ -136,6 +138,8 @@ void BMK_setBlockSize(size_t blockSize) { g_blockSize = blockSize; } void BMK_setBenchSeparately(int separate) { g_benchSeparately = (separate!=0); } +void BMK_setDecodeOnlyMode(int set) { g_decodeOnly = (set!=0); } + /* ************************************* * Compression state management @@ -301,6 +305,32 @@ LZ4_buildCompressionParameters(struct compressionParameters* pParams, } +typedef int (*DecFunction_f)(const char* src, char* dst, + int srcSize, int dstCapacity, + const char* dictStart, int dictSize); + +static LZ4F_dctx* g_dctx = NULL; + +static int +LZ4F_decompress_binding(const char* src, char* dst, + int srcSize, int dstCapacity, + const char* dictStart, int dictSize) +{ + size_t dstSize = (size_t)dstCapacity; + size_t readSize = (size_t)srcSize; + size_t const decStatus = LZ4F_decompress(g_dctx, + dst, &dstSize, + src, &readSize, + NULL /* dOptPtr */); + if ( (decStatus == 0) /* decompression successful */ + && ((int)readSize==srcSize) /* consume all input */ ) + return (int)dstSize; + /* else, error */ + return -1; + (void)dictStart; (void)dictSize; /* not compatible with dictionary yet */ +} + + /* ******************************************************** * Bench functions **********************************************************/ @@ -322,12 +352,15 @@ static int BMK_benchMem(const void* srcBuffer, size_t srcSize, 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; + size_t const blockSize = (g_blockSize>=32 && !g_decodeOnly ? g_blockSize : srcSize) + (!srcSize) /* avoid div by 0 */ ; + U32 const maxNbBlocks = (U32)((srcSize + (blockSize-1)) / blockSize) + nbFiles; blockParam_t* const blockTable = (blockParam_t*) malloc(maxNbBlocks * sizeof(blockParam_t)); size_t const maxCompressedSize = (size_t)LZ4_compressBound((int)srcSize) + (maxNbBlocks * 1024); /* add some room for safety */ void* const compressedBuffer = malloc(maxCompressedSize); - void* const resultBuffer = malloc(srcSize); + size_t const decMultiplier = g_decodeOnly ? 255 : 1; + size_t const maxInSize = (size_t)LZ4_MAX_INPUT_SIZE / decMultiplier; + size_t const maxDecSize = srcSize < maxInSize ? srcSize * decMultiplier : LZ4_MAX_INPUT_SIZE; + void* const resultBuffer = malloc(maxDecSize); U32 nbBlocks; struct compressionParameters compP; @@ -340,6 +373,11 @@ static int BMK_benchMem(const void* srcBuffer, size_t srcSize, /* init */ LZ4_buildCompressionParameters(&compP, cLevel, dictBuf, dictSize); compP.initFunction(&compP); + if (g_dctx==NULL) { + LZ4F_createDecompressionContext(&g_dctx, LZ4F_VERSION); + if (g_dctx==NULL) + END_PROCESS(1, "allocation error - decompression state"); + } /* Init blockTable data */ { const char* srcPtr = (const char*)srcBuffer; @@ -352,6 +390,8 @@ static int BMK_benchMem(const void* srcBuffer, size_t srcSize, U32 const blockEnd = nbBlocks + nbBlocksforThisFile; for ( ; nbBlocks \r", marks[markNb], displayName, (U32)srcSize); - if (!cCompleted) memset(compressedBuffer, 0xE5, maxCompressedSize); /* warm up and erase result buffer */ + DISPLAYLEVEL(2, "%2s-%-17.17s :%10u ->\r", marks[markNb], displayName, (U32)totalRSize); + if (!cCompleted) memset(compressedBuffer, 0xE5, maxCompressedSize); /* warm up and erase compressed buffer */ UTIL_sleepMilli(1); /* give processor time to other processes */ UTIL_waitForNextTick(); @@ -424,17 +472,18 @@ static int BMK_benchMem(const void* srcBuffer, size_t srcSize, } totalCTime += clockSpan; cCompleted = totalCTime>maxTime; - } } - - cSize = 0; - { U32 blockNb; for (blockNb=0; blockNb %10u (%5.3f),%6.1f MB/s\r", - marks[markNb], displayName, (U32)srcSize, (U32)cSize, ratio, - ((double)srcSize / fastestC) * 1000 ); - + } + + cSize = 0; + { U32 blockNb; for (blockNb=0; blockNb %10u (%5.3f),%6.1f MB/s\r", + marks[markNb], displayName, + (U32)totalRSize, (U32)cSize, ratio, + ((double)totalRSize / fastestC) * 1000 ); + } (void)fastestD; (void)crcOrig; /* unused when decompression disabled */ #if 1 /* Decompression */ @@ -444,17 +493,30 @@ static int BMK_benchMem(const void* srcBuffer, size_t srcSize, UTIL_waitForNextTick(); if (!dCompleted) { + const DecFunction_f decFunction = g_decodeOnly ? + LZ4F_decompress_binding : LZ4_decompress_safe_usingDict; + const char* const decString = g_decodeOnly ? + "LZ4F_decompress" : "LZ4_decompress_safe_usingDict"; UTIL_time_t const clockStart = UTIL_getTime(); U32 nbLoops; + for (nbLoops=0; nbLoops < nbDecodeLoops; nbLoops++) { U32 blockNb; for (blockNb=0; blockNb (DECOMP_MULT*maxTime); } } + if (g_decodeOnly) { + unsigned u; + totalRSize = 0; + for (u=0; u %10u (%5.3f),%6.1f MB/s ,%6.1f MB/s\r", - marks[markNb], displayName, (U32)srcSize, (U32)cSize, ratio, - ((double)srcSize / fastestC) * 1000, - ((double)srcSize / fastestD) * 1000); - - /* CRC Checking */ - { U64 const crcCheck = XXH64(resultBuffer, srcSize, 0); + marks[markNb], displayName, + (U32)totalRSize, (U32)cSize, ratio, + ((double)totalRSize / fastestC) * 1000, + ((double)totalRSize / fastestD) * 1000); + + /* CRC Checking (not possible in decode-only mode)*/ + if (!g_decodeOnly) { + U64 const crcCheck = XXH64(resultBuffer, srcSize, 0); if (crcOrig!=crcCheck) { size_t u; DISPLAY("\n!!! WARNING !!! %17s : Invalid Checksum : %x != %x \n", displayName, (unsigned)crcOrig, (unsigned)crcCheck); @@ -704,17 +774,26 @@ int BMK_benchFiles(const char** fileNamesTable, unsigned nbFiles, size_t dictSize = 0; if (cLevel > LZ4HC_CLEVEL_MAX) cLevel = LZ4HC_CLEVEL_MAX; + if (g_decodeOnly) { + DISPLAYLEVEL(2, "Benchmark Decompression (only) of LZ4 Frame \n"); + cLevelLast = cLevel; + } 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 (cLevelLast > cLevel) + DISPLAYLEVEL(2, "Benchmarking levels from %d to %d\n", cLevel, cLevelLast); if (dictFileName) { FILE* dictFile = NULL; U64 const dictFileSize = UTIL_getFileSize(dictFileName); - if (!dictFileSize) END_PROCESS(25, "Dictionary error : could not stat dictionary file"); + if (!dictFileSize) + END_PROCESS(25, "Dictionary error : could not stat dictionary file"); + if (g_decodeOnly) + END_PROCESS(26, "Error : LZ4 Frame decoder mode not compatible with dictionary yet"); dictFile = fopen(dictFileName, "rb"); - if (!dictFile) END_PROCESS(25, "Dictionary error : could not open dictionary file"); + if (!dictFile) + END_PROCESS(25, "Dictionary error : could not open dictionary file"); if (dictFileSize > LZ4_MAX_DICT_SIZE) { dictSize = LZ4_MAX_DICT_SIZE; diff --git a/programs/bench.h b/programs/bench.h index fc1f691..9b0f667 100644 --- a/programs/bench.h +++ b/programs/bench.h @@ -46,6 +46,7 @@ void BMK_setNbSeconds(unsigned nbSeconds); /* minimum benchmark duration, in se void BMK_setBlockSize(size_t blockSize); /* Internally cut input file(s) into independent blocks of specified size */ void BMK_setNotificationLevel(unsigned level); /* Influence verbosity level */ void BMK_setBenchSeparately(int separate); /* When providing multiple files, output one result per file */ +void BMK_setDecodeOnlyMode(int set); /* v1.9.4+: set benchmark mode to decode only */ void BMK_setAdditionalParam(int additionalParam); /* hidden param, influence output format, for python parsing */ diff --git a/programs/lz4cli.c b/programs/lz4cli.c index 254a6ce..42132b9 100644 --- a/programs/lz4cli.c +++ b/programs/lz4cli.c @@ -377,8 +377,12 @@ int main(int argc, const char** argv) if (argument[1]=='-') { if (!strcmp(argument, "--")) { all_arguments_are_files = 1; continue; } if (!strcmp(argument, "--compress")) { mode = om_compress; continue; } - if ((!strcmp(argument, "--decompress")) - || (!strcmp(argument, "--uncompress"))) { mode = om_decompress; continue; } + if ( (!strcmp(argument, "--decompress")) + || (!strcmp(argument, "--uncompress"))) { + if (mode != om_bench) mode = om_decompress; + BMK_setDecodeOnlyMode(1); + continue; + } if (!strcmp(argument, "--multiple")) { multiple_inputs = 1; continue; } if (!strcmp(argument, "--test")) { mode = om_test; continue; } if (!strcmp(argument, "--force")) { LZ4IO_setOverwrite(prefs, 1); continue; } @@ -478,7 +482,10 @@ int main(int argc, const char** argv) case 'l': legacy_format = 1; blockSize = 8 MB; break; /* Decoding */ - case 'd': mode = om_decompress; break; + case 'd': + if (mode != om_bench) mode = om_decompress; + BMK_setDecodeOnlyMode(1); + break; /* Force stdout, even if stdout==console */ case 'c': diff --git a/tests/Makefile b/tests/Makefile index 469db9a..d804f25 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -476,6 +476,9 @@ test-lz4-testmode: FPREFIX = tmp-ltm test-lz4-testmode: lz4 datagen @echo "\n ---- bench mode ----" $(LZ4) -bi0 + $(DATAGEN) > $(FPREFIX) + $(LZ4) -f $(FPREFIX) -c > $(FPREFIX).lz4 + $(LZ4) -bdi0 $(FPREFIX).lz4 # test benchmark decode-only mode @echo "\n ---- test mode ----" ! $(DATAGEN) | $(LZ4) -t ! $(DATAGEN) | $(LZ4) -tf diff --git a/tests/fullbench.c b/tests/fullbench.c index ec20dcb..9c13996 100644 --- a/tests/fullbench.c +++ b/tests/fullbench.c @@ -397,7 +397,8 @@ static int local_LZ4F_decompress_followHint(const char* src, char* dst, int srcS size_t outRemaining = maxOutSize - outPos; for (;;) { - size_t const sizeHint = LZ4F_decompress(g_dCtx, dst+outPos, &outRemaining, src+inPos, &inSize, NULL); + size_t const sizeHint = + LZ4F_decompress(g_dCtx, dst+outPos, &outRemaining, src+inPos, &inSize, NULL); assert(!LZ4F_isError(sizeHint)); inPos += inSize; -- cgit v0.12 From e8f0baa3f9bef8976aefc7c6fb9f0e3e6b5d7215 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 29 Jul 2022 21:46:54 +0200 Subject: added ability to skip checksum calculation when decoding LZ4 Frames --- lib/lz4frame.c | 24 +++++++++++++++--------- lib/lz4frame.h | 8 ++++++-- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/lib/lz4frame.c b/lib/lz4frame.c index fa987d7..ffd0e9c 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -1222,6 +1222,7 @@ struct LZ4F_dctx_s { size_t tmpOutStart; XXH32_state_t xxh; XXH32_state_t blockChecksum; + int skipChecksum; BYTE header[LZ4F_HEADER_SIZE_MAX]; }; /* typedef'd to LZ4F_dctx in lz4frame.h */ @@ -1275,6 +1276,7 @@ void LZ4F_resetDecompressionContext(LZ4F_dctx* dctx) dctx->dStage = dstage_getFrameHeader; dctx->dict = NULL; dctx->dictSize = 0; + dctx->skipChecksum = 0; } @@ -1537,7 +1539,6 @@ static void LZ4F_updateDict(LZ4F_dctx* dctx, } - /*! LZ4F_decompress() : * Call this function repetitively to regenerate compressed data in srcBuffer. * The function will attempt to decode up to *srcSizePtr bytes from srcBuffer @@ -1581,6 +1582,7 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, *srcSizePtr = 0; *dstSizePtr = 0; assert(dctx != NULL); + dctx->skipChecksum |= (decompressOptionsPtr->skipChecksums != 0); /* once set, disable for the remainder of the frame */ /* behaves as a state machine */ @@ -1711,11 +1713,13 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, 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->skipChecksum) { + 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.contentChecksumFlag) - (void)XXH32_update(&dctx->xxh, srcPtr, sizeToCopy); if (dctx->frameInfo.contentSize) dctx->frameRemainingSize -= sizeToCopy; @@ -1761,7 +1765,8 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, } crcSrc = dctx->header; } - { U32 const readCRC = LZ4F_readLE32(crcSrc); + if (!dctx->skipChecksum) { + U32 const readCRC = LZ4F_readLE32(crcSrc); U32 const calcCRC = XXH32_digest(&dctx->blockChecksum); #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION DEBUGLOG(6, "compare block checksum"); @@ -1837,7 +1842,7 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, (int)dctx->tmpInTarget, (int)dctx->maxBlockSize, dict, (int)dictSize); RETURN_ERROR_IF(decodedSize < 0, decompressionFailed); - if (dctx->frameInfo.contentChecksumFlag) + if ((dctx->frameInfo.contentChecksumFlag) && (!dctx->skipChecksum)) XXH32_update(&(dctx->xxh), dstPtr, (size_t)decodedSize); if (dctx->frameInfo.contentSize) dctx->frameRemainingSize -= (size_t)decodedSize; @@ -1880,7 +1885,7 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, (int)dctx->tmpInTarget, (int)dctx->maxBlockSize, dict, (int)dictSize); RETURN_ERROR_IF(decodedSize < 0, decompressionFailed); - if (dctx->frameInfo.contentChecksumFlag) + if (dctx->frameInfo.contentChecksumFlag && !dctx->skipChecksum) XXH32_update(&(dctx->xxh), dctx->tmpOut, (size_t)decodedSize); if (dctx->frameInfo.contentSize) dctx->frameRemainingSize -= (size_t)decodedSize; @@ -1945,7 +1950,8 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, } /* if (dctx->dStage == dstage_storeSuffix) */ /* case dstage_checkSuffix: */ /* no direct entry, avoid initialization risks */ - { U32 const readCRC = LZ4F_readLE32(selectedIn); + if (!dctx->skipChecksum) { + U32 const readCRC = LZ4F_readLE32(selectedIn); U32 const resultCRC = XXH32_digest(&(dctx->xxh)); #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION RETURN_ERROR_IF(readCRC != resultCRC, contentChecksum_invalid); diff --git a/lib/lz4frame.h b/lib/lz4frame.h index 1c2d0dd..1bdf6c4 100644 --- a/lib/lz4frame.h +++ b/lib/lz4frame.h @@ -355,8 +355,12 @@ typedef struct LZ4F_dctx_s LZ4F_dctx; /* incomplete type */ typedef LZ4F_dctx* LZ4F_decompressionContext_t; /* compatibility with previous API versions */ typedef struct { - unsigned stableDst; /* pledges that last 64KB decompressed data will remain available unmodified. This optimization skips storage operations in tmp buffers. */ - unsigned reserved[3]; /* must be set to zero for forward compatibility */ + unsigned stableDst; /* pledges that last 64KB decompressed data will remain available unmodified between invocations. + * This optimization skips storage operations in tmp buffers. */ + unsigned skipChecksums; /* disable checksum calculation and verification, even when one is present in frame, to save CPU time. + * Setting this option to 1 once disables all checksums for the rest of the frame. */ + unsigned reserved1; /* must be set to zero for forward compatibility */ + unsigned reserved0; /* idem */ } LZ4F_decompressOptions_t; -- cgit v0.12 From f01b7b5209c447acdee0db817f239e9925497329 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 29 Jul 2022 22:13:38 +0200 Subject: can select validation of CRC during benchmark on command line, using existing long command --no-frame-crc. Note : it's effectively more than that, since _all_ checksums are disabled. --- lib/lz4frame.c | 8 ++++---- programs/bench.c | 13 +++++++++++-- programs/bench.h | 1 + programs/lz4cli.c | 4 ++-- 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/lib/lz4frame.c b/lib/lz4frame.c index ffd0e9c..cba5744 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -1959,11 +1959,11 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, (void)readCRC; (void)resultCRC; #endif - nextSrcSizeHint = 0; - LZ4F_resetDecompressionContext(dctx); - doAnotherStage = 0; - break; } + nextSrcSizeHint = 0; + LZ4F_resetDecompressionContext(dctx); + doAnotherStage = 0; + break; case dstage_getSFrameSize: if ((srcEnd - srcPtr) >= 4) { diff --git a/programs/bench.c b/programs/bench.c index 7836717..00de426 100644 --- a/programs/bench.c +++ b/programs/bench.c @@ -123,6 +123,7 @@ static size_t g_blockSize = 0; int g_additionalParam = 0; int g_benchSeparately = 0; int g_decodeOnly = 0; +unsigned g_skipChecksums = 0; void BMK_setNotificationLevel(unsigned level) { g_displayLevel=level; } @@ -140,6 +141,8 @@ void BMK_setBenchSeparately(int separate) { g_benchSeparately = (separate!=0); } void BMK_setDecodeOnlyMode(int set) { g_decodeOnly = (set!=0); } +void BMK_skipChecksums(int skip) { g_skipChecksums = (skip!=0); } + /* ************************************* * Compression state management @@ -318,10 +321,11 @@ LZ4F_decompress_binding(const char* src, char* dst, { size_t dstSize = (size_t)dstCapacity; size_t readSize = (size_t)srcSize; + LZ4F_decompressOptions_t const dOpt = { 1, g_skipChecksums, 0, 0 }; size_t const decStatus = LZ4F_decompress(g_dctx, dst, &dstSize, src, &readSize, - NULL /* dOptPtr */); + &dOpt); if ( (decStatus == 0) /* decompression successful */ && ((int)readSize==srcSize) /* consume all input */ ) return (int)dstSize; @@ -775,7 +779,12 @@ int BMK_benchFiles(const char** fileNamesTable, unsigned nbFiles, if (cLevel > LZ4HC_CLEVEL_MAX) cLevel = LZ4HC_CLEVEL_MAX; if (g_decodeOnly) { - DISPLAYLEVEL(2, "Benchmark Decompression (only) of LZ4 Frame \n"); + DISPLAYLEVEL(2, "Benchmark Decompression of LZ4 Frame "); + if (g_skipChecksums) { + DISPLAYLEVEL(2, "_without_ checksum even when present \n"); + } else { + DISPLAYLEVEL(2, "+ Checksum when present \n"); + } cLevelLast = cLevel; } if (cLevelLast > LZ4HC_CLEVEL_MAX) cLevelLast = LZ4HC_CLEVEL_MAX; diff --git a/programs/bench.h b/programs/bench.h index 9b0f667..1d81a99 100644 --- a/programs/bench.h +++ b/programs/bench.h @@ -47,6 +47,7 @@ void BMK_setBlockSize(size_t blockSize); /* Internally cut input file(s) into void BMK_setNotificationLevel(unsigned level); /* Influence verbosity level */ void BMK_setBenchSeparately(int separate); /* When providing multiple files, output one result per file */ void BMK_setDecodeOnlyMode(int set); /* v1.9.4+: set benchmark mode to decode only */ +void BMK_skipChecksums(int skip); /* v1.9.4+: only useful for DecodeOnlyMode; do not calculate checksum when present, to save CPU time */ void BMK_setAdditionalParam(int additionalParam); /* hidden param, influence output format, for python parsing */ diff --git a/programs/lz4cli.c b/programs/lz4cli.c index 42132b9..ca4951e 100644 --- a/programs/lz4cli.c +++ b/programs/lz4cli.c @@ -389,8 +389,8 @@ int main(int argc, const char** argv) 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(prefs, 1); continue; } - if (!strcmp(argument, "--no-frame-crc")) { LZ4IO_setStreamChecksumMode(prefs, 0); continue; } + if (!strcmp(argument, "--frame-crc")) { LZ4IO_setStreamChecksumMode(prefs, 1); BMK_skipChecksums(0); continue; } + if (!strcmp(argument, "--no-frame-crc")) { LZ4IO_setStreamChecksumMode(prefs, 0); BMK_skipChecksums(1); 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; } -- cgit v0.12 From 8f18b4b4e6de05b86036973e69aeaa1a31f7d271 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 29 Jul 2022 22:18:02 +0200 Subject: introduced new `--no-crc` command which disables both frame and block checksums. --- programs/lz4.1.md | 3 +++ programs/lz4cli.c | 1 + tests/Makefile | 2 ++ 3 files changed, 6 insertions(+) diff --git a/programs/lz4.1.md b/programs/lz4.1.md index 56c0053..39dc925 100644 --- a/programs/lz4.1.md +++ b/programs/lz4.1.md @@ -188,6 +188,9 @@ only the latest one will be applied. * `--[no-]frame-crc`: Select frame checksum (default:enabled) +* `--no-crc`: + Disable both frame and block checksums + * `--[no-]content-size`: Header includes original size (default:not present)
Note : this option can only be activated when the original size can be diff --git a/programs/lz4cli.c b/programs/lz4cli.c index ca4951e..51969fd 100644 --- a/programs/lz4cli.c +++ b/programs/lz4cli.c @@ -391,6 +391,7 @@ int main(int argc, const char** argv) || (!strcmp(argument, "--to-stdout"))) { forceStdout=1; output_filename=stdoutmark; continue; } if (!strcmp(argument, "--frame-crc")) { LZ4IO_setStreamChecksumMode(prefs, 1); BMK_skipChecksums(0); continue; } if (!strcmp(argument, "--no-frame-crc")) { LZ4IO_setStreamChecksumMode(prefs, 0); BMK_skipChecksums(1); continue; } + if (!strcmp(argument, "--no-crc")) { LZ4IO_setStreamChecksumMode(prefs, 0); LZ4IO_setBlockChecksumMode(prefs, 0); BMK_skipChecksums(1); 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; } diff --git a/tests/Makefile b/tests/Makefile index d804f25..d266ae5 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -376,6 +376,7 @@ test-lz4-basic: lz4 datagen unlz4 lz4cat $(LZ4) --no-frame-crc < $(FPREFIX)-dg20k | $(LZ4) -d > $(FPREFIX)-dec $(DIFF) -q $(FPREFIX)-dg20k $(FPREFIX)-dec $(DATAGEN) | $(LZ4) -BI | $(LZ4) -t + $(DATAGEN) | $(LZ4) --no-crc | $(LZ4) -t $(DATAGEN) -g6M -P99 | $(LZ4) -9BD | $(LZ4) -t $(DATAGEN) -g17M | $(LZ4) -9v | $(LZ4) -qt $(DATAGEN) -g33M | $(LZ4) --no-frame-crc | $(LZ4) -t @@ -479,6 +480,7 @@ test-lz4-testmode: lz4 datagen $(DATAGEN) > $(FPREFIX) $(LZ4) -f $(FPREFIX) -c > $(FPREFIX).lz4 $(LZ4) -bdi0 $(FPREFIX).lz4 # test benchmark decode-only mode + $(LZ4) -bdi0 --no-crc $(FPREFIX).lz4 # test benchmark decode-only mode @echo "\n ---- test mode ----" ! $(DATAGEN) | $(LZ4) -t ! $(DATAGEN) | $(LZ4) -tf -- cgit v0.12 From 08262342935fb0ae41ecf8e229771031fdd80506 Mon Sep 17 00:00:00 2001 From: Yann ColletDate: Fri, 29 Jul 2022 22:32:41 +0200 Subject: added options.skipChecksums to local fuzzer test --- tests/frametest.c | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/frametest.c b/tests/frametest.c index ed4186a..3301955 100644 --- a/tests/frametest.c +++ b/tests/frametest.c @@ -906,6 +906,7 @@ size_t test_lz4f_decompression_wBuffers( memset(&dOptions, 0, sizeof(dOptions)); dOptions.stableDst = FUZ_rand(randState) & 1; if (o_scenario == o_overwrite) dOptions.stableDst = 0; /* overwrite mode */ + dOptions.skipChecksums = FUZ_rand(randState) & 127; if (sentinelTest) op[oSizeMax] = mark; DISPLAYLEVEL(7, "dstCapacity=%u, presentedInput=%u \n", (unsigned)oSize, (unsigned)iSize); -- cgit v0.12 From 5797d570180013f62f75dbab23300d5de295a8a6 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 29 Jul 2022 23:21:40 +0200 Subject: extend disabling checksum validation to normal lz4 CLI decompression note : it's unlikely to improve speed, as in most cases I/O is slower than lz4 decompression, but maybe in extreme scenarios, it might show a difference. --- programs/lz4io.c | 29 +++++++++++++++++++++++++---- tests/Makefile | 2 +- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/programs/lz4io.c b/programs/lz4io.c index 5644775..8b70b91 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -1072,13 +1072,22 @@ LZ4IO_decompressLZ4F(dRess_t ress, unsigned long long filesize = 0; LZ4F_errorCode_t nextToLoad; unsigned storedSkips = 0; + LZ4F_decompressOptions_t const dOpt_skipCrc = { 0, 1, 0, 0 }; + const LZ4F_decompressOptions_t* const dOptPtr = + ((prefs->blockChecksum==0) && (prefs->streamChecksum==0)) ? + &dOpt_skipCrc : NULL; /* Init feed with magic number (already consumed from FILE* sFile) */ { size_t inSize = MAGICNUMBER_SIZE; size_t outSize= 0; LZ4IO_writeLE32(ress.srcBuffer, LZ4IO_MAGICNUMBER); - nextToLoad = LZ4F_decompress_usingDict(ress.dCtx, ress.dstBuffer, &outSize, ress.srcBuffer, &inSize, ress.dictBuffer, ress.dictBufferSize, NULL); - if (LZ4F_isError(nextToLoad)) END_PROCESS(62, "Header error : %s", LZ4F_getErrorName(nextToLoad)); + nextToLoad = LZ4F_decompress_usingDict(ress.dCtx, + ress.dstBuffer, &outSize, + ress.srcBuffer, &inSize, + ress.dictBuffer, ress.dictBufferSize, + dOptPtr); /* set it once, it's enough */ + if (LZ4F_isError(nextToLoad)) + END_PROCESS(62, "Header error : %s", LZ4F_getErrorName(nextToLoad)); } /* Main Loop */ @@ -1096,8 +1105,13 @@ LZ4IO_decompressLZ4F(dRess_t ress, /* Decode Input (at least partially) */ size_t remaining = readSize - pos; decodedBytes = ress.dstBufferSize; - nextToLoad = LZ4F_decompress_usingDict(ress.dCtx, ress.dstBuffer, &decodedBytes, (char*)(ress.srcBuffer)+pos, &remaining, ress.dictBuffer, ress.dictBufferSize, NULL); - if (LZ4F_isError(nextToLoad)) END_PROCESS(66, "Decompression error : %s", LZ4F_getErrorName(nextToLoad)); + nextToLoad = LZ4F_decompress_usingDict(ress.dCtx, + ress.dstBuffer, &decodedBytes, + (char*)(ress.srcBuffer)+pos, &remaining, + ress.dictBuffer, ress.dictBufferSize, + NULL); + if (LZ4F_isError(nextToLoad)) + END_PROCESS(66, "Decompression error : %s", LZ4F_getErrorName(nextToLoad)); pos += remaining; /* Write Block */ @@ -1327,6 +1341,10 @@ LZ4IO_decompressDstFile(dRess_t ress, } +/* Note : LZ4IO_decompressFilename() + * can provide total decompression time for the specified fileName. + * This information is not available with LZ4IO_decompressMultipleFilenames(). + */ int LZ4IO_decompressFilename(const char* input_filename, const char* output_filename, const LZ4IO_prefs_t* prefs) { dRess_t const ress = LZ4IO_createDResources(prefs); @@ -1357,6 +1375,9 @@ int LZ4IO_decompressMultipleFilenames( dRess_t ress = LZ4IO_createDResources(prefs); if (outFileName==NULL) END_PROCESS(70, "Memory allocation error"); + if (prefs->blockChecksum==0 && prefs->streamChecksum==0) { + DISPLAYLEVEL(4, "disabling checksum validation during decoding \n"); + } ress.dstFile = LZ4IO_openDstFile(stdoutmark, prefs); for (i=0; i $(FPREFIX)-hw $(LZ4) --rm -f $(FPREFIX)-hw $(FPREFIX)-hw.lz4 test ! -f $(FPREFIX)-hw # must fail (--rm) -- cgit v0.12 From 9978bb90cf45e455a146a7beaf2e4d08baaed6c0 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 29 Jul 2022 23:31:08 +0200 Subject: fixed minor pedantic warning --- programs/bench.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/programs/bench.c b/programs/bench.c index 00de426..5a56e6f 100644 --- a/programs/bench.c +++ b/programs/bench.c @@ -321,8 +321,10 @@ LZ4F_decompress_binding(const char* src, char* dst, { size_t dstSize = (size_t)dstCapacity; size_t readSize = (size_t)srcSize; - LZ4F_decompressOptions_t const dOpt = { 1, g_skipChecksums, 0, 0 }; - size_t const decStatus = LZ4F_decompress(g_dctx, + LZ4F_decompressOptions_t dOpt = { 1, 0, 0, 0 }; + size_t decStatus; + dOpt.skipChecksums = g_skipChecksums; + decStatus = LZ4F_decompress(g_dctx, dst, &dstSize, src, &readSize, &dOpt); -- cgit v0.12 From 5d80375deddf33f2392eb2ea678cf2e203bb24fb Mon Sep 17 00:00:00 2001 From: Takayuki Matsuoka Date: Sun, 31 Jul 2022 20:59:09 +0900 Subject: New macro for memcpy, memmove and memset This changeset introduces the following external macros. - Add new macro LZ4_memset() which enables to inject external function as memset(). - Similar macro LZ4_memmove() for memmove(). - In same manner, LZ4_memcpy() also can be overriden by external macro. --- lib/lz4.c | 31 ++++++++++++++++++++++--------- lib/lz4hc.c | 8 ++++---- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 3f468d7..0dd337a 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -206,7 +206,10 @@ void LZ4_free(void* p); #endif #include /* memset, memcpy */ -#define MEM_INIT(p,v,s) memset((p),(v),(s)) +#if !defined(LZ4_memset) +# define LZ4_memset(p,v,s) memset((p),(v),(s)) +#endif +#define MEM_INIT(p,v,s) LZ4_memset((p),(v),(s)) /*-************************************ @@ -317,10 +320,20 @@ typedef enum { * 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) +#if !defined(LZ4_memcpy) +# 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 +#endif + +#if !defined(LZ4_memmove) +# if defined(__GNUC__) && (__GNUC__ >= 4) +# define LZ4_memmove __builtin_memmove +# else +# define LZ4_memmove memmove +# endif #endif static unsigned LZ4_isLittleEndian(void) @@ -1703,7 +1716,7 @@ int LZ4_saveDict (LZ4_stream_t* LZ4_dict, char* safeBuffer, int dictSize) if (dictSize > 0) { const BYTE* const previousDictEnd = dict->dictionary + dict->dictSize; assert(dict->dictionary); - memmove(safeBuffer, previousDictEnd - dictSize, dictSize); + LZ4_memmove(safeBuffer, previousDictEnd - dictSize, dictSize); } dict->dictionary = (const BYTE*)safeBuffer; @@ -1920,7 +1933,7 @@ LZ4_decompress_generic( if (length <= (size_t)(lowPrefix-match)) { /* match fits entirely within external dictionary : just copy */ - memmove(op, dictEnd - (lowPrefix-match), length); + LZ4_memmove(op, dictEnd - (lowPrefix-match), length); op += length; } else { /* match stretches into both external dictionary and current block */ @@ -2064,7 +2077,7 @@ LZ4_decompress_generic( goto _output_error; } } - memmove(op, ip, length); /* supports overlapping memory regions; only matters for in-place decompression scenarios */ + LZ4_memmove(op, ip, length); /* supports overlapping memory regions; only matters for in-place decompression scenarios */ ip += length; op += length; /* Necessarily EOF when !partialDecoding. @@ -2109,7 +2122,7 @@ LZ4_decompress_generic( if (length <= (size_t)(lowPrefix-match)) { /* match fits entirely within external dictionary : just copy */ - memmove(op, dictEnd - (lowPrefix-match), length); + LZ4_memmove(op, dictEnd - (lowPrefix-match), length); op += length; } else { /* match stretches into both external dictionary and current block */ diff --git a/lib/lz4hc.c b/lib/lz4hc.c index 6b139fa..e854cb4 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -755,7 +755,7 @@ _last_literals: } else { *op++ = (BYTE)(lastRunSize << ML_BITS); } - memcpy(op, anchor, lastRunSize); + LZ4_memcpy(op, anchor, lastRunSize); op += lastRunSize; } @@ -894,7 +894,7 @@ LZ4HC_compress_generic_dictCtx ( ctx->dictCtx = NULL; return LZ4HC_compress_generic_noDictCtx(ctx, src, dst, srcSizePtr, dstCapacity, cLevel, limit); } else if (position == 0 && *srcSizePtr > 4 KB) { - memcpy(ctx, ctx->dictCtx, sizeof(LZ4HC_CCtx_internal)); + LZ4_memcpy(ctx, ctx->dictCtx, sizeof(LZ4HC_CCtx_internal)); LZ4HC_setExternalDict(ctx, (const BYTE *)src); ctx->compressionLevel = (short)cLevel; return LZ4HC_compress_generic_noDictCtx(ctx, src, dst, srcSizePtr, dstCapacity, cLevel, limit); @@ -1177,7 +1177,7 @@ int LZ4_saveDictHC (LZ4_streamHC_t* LZ4_streamHCPtr, char* safeBuffer, int dictS if (dictSize > prefixSize) dictSize = prefixSize; if (safeBuffer == NULL) assert(dictSize == 0); if (dictSize > 0) - memmove(safeBuffer, streamPtr->end - dictSize, dictSize); + LZ4_memmove(safeBuffer, streamPtr->end - dictSize, dictSize); { U32 const endIndex = (U32)(streamPtr->end - streamPtr->prefixStart) + streamPtr->dictLimit; streamPtr->end = (const BYTE*)safeBuffer + dictSize; streamPtr->prefixStart = streamPtr->end - dictSize; @@ -1587,7 +1587,7 @@ _last_literals: } else { *op++ = (BYTE)(lastRunSize << ML_BITS); } - memcpy(op, anchor, lastRunSize); + LZ4_memcpy(op, anchor, lastRunSize); op += lastRunSize; } -- cgit v0.12 From fa889cf6daa12e2ce9e7ac42f1eaccf2466b4c7c Mon Sep 17 00:00:00 2001 From: Takayuki Matsuoka Date: Sun, 31 Jul 2022 21:10:55 +0900 Subject: Introduce LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION This changeset introduces new compile time switch macro LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION which removes the following functions when it's defined. ``` // lz4.c LZ4_createStream LZ4_freeStream LZ4_createStreamDecode LZ4_freeStreamDecode LZ4_create // legacy // lz4hc.c LZ4_createStreamHC(void) LZ4_freeStreamHC LZ4_createHC // legacy LZ4_freeHC // legacy ``` These functions uses dynamic memory allocation functions such as malloc() and free(). It'll be useful for freestanding environment which doesn't have these allocation functions. Since this change breaks API, this macro is only valid with lz4 as a static linked object. --- lib/lz4.c | 17 ++++++++++++++++- lib/lz4hc.c | 2 ++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/lz4.c b/lib/lz4.c index 3f468d7..77b24f7 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -188,7 +188,14 @@ /*-************************************ * Memory routines **************************************/ -#ifdef LZ4_USER_MEMORY_FUNCTIONS +#if defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION) +# undef LZ4_ALLOC +# undef LZ4_ALLOC_AND_ZERO +# undef LZ4_FREEMEM +# define LZ4_ALLOC(x) lz4_error_memory_allocation_is_disabled +# define LZ4_ALLOC_AND_ZERO(x) lz4_error_memory_allocation_is_disabled +# define LZ4_FREEMEM(x) lz4_error_memory_allocation_is_disabled +#elif defined(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 */ @@ -1440,6 +1447,7 @@ int LZ4_compress_destSize(const char* src, char* dst, int* srcSizePtr, int targe * Streaming functions ********************************/ +#if !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION) LZ4_stream_t* LZ4_createStream(void) { LZ4_stream_t* const lz4s = (LZ4_stream_t*)ALLOC(sizeof(LZ4_stream_t)); @@ -1449,6 +1457,7 @@ LZ4_stream_t* LZ4_createStream(void) LZ4_initStream(lz4s, sizeof(*lz4s)); return lz4s; } +#endif static size_t LZ4_stream_t_alignment(void) { @@ -1482,6 +1491,7 @@ void LZ4_resetStream_fast(LZ4_stream_t* ctx) { LZ4_prepareTable(&(ctx->internal_donotuse), 0, byU32); } +#if !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION) int LZ4_freeStream (LZ4_stream_t* LZ4_stream) { if (!LZ4_stream) return 0; /* support free on NULL */ @@ -1489,6 +1499,7 @@ int LZ4_freeStream (LZ4_stream_t* LZ4_stream) FREEMEM(LZ4_stream); return (0); } +#endif #define HASH_UNIT sizeof(reg_t) @@ -2321,6 +2332,7 @@ int LZ4_decompress_fast_doubleDict(const char* source, char* dest, int originalS /*===== streaming decompression functions =====*/ +#if !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION) LZ4_streamDecode_t* LZ4_createStreamDecode(void) { LZ4_STATIC_ASSERT(sizeof(LZ4_streamDecode_t) >= sizeof(LZ4_streamDecode_t_internal)); @@ -2333,6 +2345,7 @@ int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream) FREEMEM(LZ4_stream); return 0; } +#endif /*! LZ4_setStreamDecode() : * Use this function to instruct where to find the dictionary. @@ -2558,11 +2571,13 @@ int LZ4_resetStreamState(void* state, char* inputBuffer) return 0; } +#if !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION) void* LZ4_create (char* inputBuffer) { (void)inputBuffer; return LZ4_createStream(); } +#endif char* LZ4_slideInputBuffer (void* state) { diff --git a/lib/lz4hc.c b/lib/lz4hc.c index 6b139fa..8122bd8 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -988,6 +988,7 @@ int LZ4_compress_HC_destSize(void* state, const char* source, char* dest, int* s * Streaming Functions **************************************/ /* allocation */ +#if !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION) LZ4_streamHC_t* LZ4_createStreamHC(void) { LZ4_streamHC_t* const state = @@ -1004,6 +1005,7 @@ int LZ4_freeStreamHC (LZ4_streamHC_t* LZ4_streamHCPtr) FREEMEM(LZ4_streamHCPtr); return 0; } +#endif LZ4_streamHC_t* LZ4_initStreamHC (void* buffer, size_t size) -- cgit v0.12 From 6c8508323a87e7ce0bdea57ab18def17000effab Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sun, 31 Jul 2022 14:29:27 +0200 Subject: updated LZ4 Block Format documentation hopefully answering concerns expressed in #792 --- doc/lz4_Block_format.md | 211 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 144 insertions(+), 67 deletions(-) diff --git a/doc/lz4_Block_format.md b/doc/lz4_Block_format.md index a0017b9..8a4b083 100644 --- a/doc/lz4_Block_format.md +++ b/doc/lz4_Block_format.md @@ -1,24 +1,22 @@ LZ4 Block Format Description ============================ -Last revised: 2022-02-02. +Last revised: 2022-07-31 . Author : Yann Collet -This specification is intended for developers -willing to produce LZ4-compatible compressed data blocks -using any programming language. +This specification is intended for developers willing to +produce or read LZ4 compressed data blocks +using any programming language of their choice. -LZ4 is an LZ77-type compressor with a fixed, byte-oriented encoding. +LZ4 is an LZ77-type compressor with a fixed byte-oriented encoding format. 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]). This design is assumed to favor simplicity and speed. -It helps later on for optimizations, compactness, and features. -This document describes only the block format, +This document describes only the Block Format, not how the compressor nor decompressor actually work. -The correctness of the decompressor should not depend -on implementation details of the compressor, and vice versa. +For more details on such topics, see later section "Implementation Notes". [LZ4 Frame format]: lz4_Frame_format.md @@ -28,7 +26,7 @@ Compressed block format ----------------------- An LZ4 compressed block is composed of sequences. A sequence is a suite of literals (not-compressed bytes), -followed by a match copy. +followed by a match copy operation. Each sequence starts with a `token`. The `token` is a one byte value, separated into two 4-bits fields. @@ -38,14 +36,20 @@ Therefore each field ranges from 0 to 15. The first field uses the 4 high-bits of the token. It provides the length of literals to follow. -If the field value is 0, then there is no literal. -If it is 15, then we need to add some more bytes to indicate the full length. -Each additional byte then represent a value from 0 to 255, +If the field value is smaller than 15, +then it represents the total nb of literals present in the sequence, +including 0, in which case there is no literal. + +The value 15 is a special case: more bytes are required to indicate the full length. +Each additional byte then represents a value from 0 to 255, which is added to the previous value to produce a total length. -When the byte value is 255, another byte must read and added, and so on. -There can be any number of bytes of value "255" following `token`. -There is no "size limit". -(Side note : this is why a not-compressible input block is expanded by 0.4%). +When the byte value is 255, another byte must be read and added, and so on. +There can be any number of bytes of value `255` following `token`. +The Block Format does not define any "size limit", +though real implementations may feature some practical limits +(see more details in later chapter "Implementation Notes"). + +Note : this format explains why a non-compressible input block is expanded by 0.4%. Example 1 : A literal length of 48 will be represented as : @@ -64,104 +68,177 @@ Example 3 : A literal length of 15 will be represented as : - 0 : (=15-15) yes, the zero must be output Following `token` and optional length bytes, are the literals themselves. -They are exactly as numerous as previously decoded (length of literals). -It's possible that there are zero literals. +They are exactly as numerous as just decoded (length of literals). +Reminder: it's possible that there are zero literals. Following the literals is the match copy operation. -It starts by the `offset`. +It starts by the `offset` value. This is a 2 bytes value, in little endian format (the 1st byte is the "low" byte, the 2nd one is the "high" byte). -The `offset` represents the position of the match to be copied from. +The `offset` represents the position of the match to be copied from the past. For example, 1 means "current position - 1 byte". -The maximum `offset` value is 65535, 65536 and beyond cannot be coded. -Note that 0 is an invalid offset value. -The presence of such a value denotes an invalid (corrupted) block. +The maximum `offset` value is 65535. 65536 and beyond cannot be coded. +Note that 0 is an invalid `offset` value. +The presence of a 0 `offset` value denotes an invalid (corrupted) block. Then the `matchlength` can be extracted. -For this, we use the second token field, the low 4-bits. +For this, we use the second `token` field, the low 4-bits. Such a value, obviously, ranges from 0 to 15. -However here, 0 means that the copy operation will be minimal. +However here, 0 means that the copy operation is minimal. The minimum length of a match, called `minmatch`, is 4. -As a consequence, a 0 value means 4 bytes, and a value of 15 means 19+ bytes. -Similar to literal length, on reaching the highest possible value (15), -one must read additional bytes, one at a time, with values ranging from 0 to 255. +As a consequence, a 0 value means 4 bytes. +Similarly to literal length, any value smaller than 15 represents a length, +to which 4 (`minmatch`) must be added, thus ranging from 4 to 18. +A value of 15 is special, meaning 19+ bytes, +to which one must read additional bytes, one at a time, +with each byte value ranging from 0 to 255. They are added to total to provide the final match length. A 255 value means there is another byte to read and add. -There is no limit to the number of optional "255" bytes that can be present. -(Note: this points towards a maximum achievable compression ratio of about 250). +There is no limit to the number of optional `255` bytes that can be present, +and therefore no limit to representable match length, +though real-life implementations are likely going to enforce limits for practical reasons (see more details in "Implementation Notes" section below). + +Note: this format has a maximum achievable compression ratio of about ~250. Decoding the `matchlength` reaches the end of current sequence. -Next byte will be the start of another sequence. -But before moving to next sequence, -it's time to use the decoded match position and length. -The decoder copies `matchlength` bytes from match position to current position. - -In some cases, `matchlength` can be larger than `offset`. -Therefore, since `match_pos + matchlength > current_pos`, -later bytes to copy are not decoded yet. -This is called an "overlap match", and must be handled with special care. -A common case is an offset of 1, -meaning the last byte is repeated `matchlength` times. +Next byte will be the start of another sequence, and therefore a new `token`. -End of block restrictions ------------------------ +End of block conditions +------------------------- There are specific restrictions required to terminate an LZ4 block. 1. The last sequence contains only literals. - The block ends right after them. + The block ends right after the literals (no `offset` field). 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, + Even empty input can be represented, using 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 contains only literals. + The last match is part of the _penultimate_ sequence. + 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. - - However, when a block can reference data from another block, - it can start immediately with a match and no literal, - therefore a block of exactly 12 bytes can be compressed. + blocks < 12 bytes cannot be compressed. + And as an extension, _independent_ blocks < 13 bytes cannot be compressed, + because they must start by at least one literal, + that the match can then copy afterwards. 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 compatibility with a wide range of historical decoders -which rely on these conditions in their speed-oriented design. +which rely on these conditions for their speed-oriented design. -Additional notes +Implementation notes ----------------------- -If the decoder will decompress data from any external source, -it is recommended to ensure that the decoder is resilient to corrupted data, -and typically not vulnerable to buffer overflow manipulations. +The LZ4 Block Format only defines the compressed format, +it does not tell how to create a decoder or an encoder, +which design is left free to the imagination of the implementer. + +However, thanks to experience, there are a number of typical topics that +most implementations will have to consider. +This section tries to provide a few guidelines. + +#### Metadata + +An LZ4-compressed Block requires additional metadata for proper decoding. +Typically, a decoder will require the compressed block's size, +and an upper bound of decompressed size. +Other variants exist, such as knowing the decompressed size, +and having an upper bound of the input size. +The Block Format does not specify how to transmit such information, +which is considered an out-of-band information channel. +That's because in many cases, the information is present in the environment. +For example, databases must store the size of their compressed block for indexing, +and know that their decompressed block can't be larger than a certain threshold. + +If you need a format which is "self-contained", +and also transports the necessary metadata for proper decoding on any platform, +consider employing the [LZ4 Frame format] instead. + +#### Large lengths + +While the Block Format does not define any maximum value for length fields, +in practice, most implementations will feature some form of limit, +since it's expected for such values to be stored into registers of fixed bit width. + +If length fields use 64-bit registers, +then it can be assumed that there is no practical limit, +as it would require a single continuous block of multiple petabytes to reach it, +which is unreasonable by today's standard. + +If length fields use 32-bit registers, then it can be overflowed, +but requires a compressed block of size > 16 MB. +Therefore, implementations that do not deal with compressed blocks > 16 MB are safe. +However, if such a case is allowed, +then it's recommended to check that no large length overflows the register. + +If length fields use 16-bit registers, +then it's definitely possible to overflow such register, +with less than < 300 bytes of compressed data. + +A conformant decoder should be able to detect length overflows when it's possible, +and simply error out when that happens. +The input block might not be invalid, +it's just not decodable by the local decoder implementation. + +Note that, in order to be compatible with the larger LZ4 ecosystem, +it's recommended to be able to read and represent lengths of up to 4 MB, +and to accept blocks of size up to 4 MB. +Such limits are compatible with 32-bit length registers, +and prevent overflow of 32-bit registers. + +#### Safe decoding + +If a decoder receives compressed data from any external source, +it is recommended to ensure that the decoder is resilient to corrupted input, +and made safe from buffer overflow manipulations. Always ensure that read and write operations remain within the limits of provided buffers. -Test the decoder with fuzzers + +Of particular importance, ensure that the nb of bytes instructed to copy +does not overflow neither the input nor the output buffers. +Ensure also, when reading an offset value, that the resulting position to copy +does not reach beyond the beginning of the buffer. +Such a situation can happen during the first 64 KB of decoded data. + +For more safety, test the decoder with fuzzers to ensure it's resilient to improbable sequences of conditions. Combine them with sanitizers, in order to catch overflows (asan) or initialization issues (msan). + Pay some attention to offset 0 scenario, which is invalid, -and therefore must not be blindly decoded -(a naive implementation could preserve destination buffer content, +and therefore must not be blindly decoded: +a naive implementation could preserve destination buffer content, which could then result in information disclosure -if such buffer was uninitialized and still containing private data). +if such buffer was uninitialized and still containing private data. For reference, in such a scenario, the reference LZ4 decoder clears the match segment with `0` bytes, though other solutions are certainly possible. +Finally, pay attention to the "overlap match" scenario, +when `matchlength` is larger than `offset`. +In which case, since `match_pos + matchlength > current_pos`, +some of the later bytes to copy do not exist yet, +and will be generated during the early stage of match copy operation. +Such scenario must be handled with special care. +A common case is an offset of 1, +meaning the last byte is repeated `matchlength` times. + +#### Compression techniques + +The core of a LZ4 compressor is to detect duplicated data across past 64 KB. The format makes no assumption nor limits to the way a compressor searches and selects matches within the source data block. -Multiple techniques can be considered, -featuring distinct time / performance trade offs. For example, an upper compression limit can be reached, -using a technique called "full optimal parsing", at very high cpu cost. +using a technique called "full optimal parsing", at high cpu and memory cost. +But multiple other techniques can be considered, +featuring distinct time / performance trade offs. As long as the specified format is respected, -the result will be compatible and decodable by any compliant decoder. +the result will be compatible with and decodable by any compliant decoder. -- cgit v0.12 From 33474853075599c69cb95b8312fe4981411fa346 Mon Sep 17 00:00:00 2001 From: Dominique Pelle Date: Sun, 31 Jul 2022 17:47:00 +0200 Subject: fix: various typos --- .github/ISSUE_TEMPLATE/bug_report.md | 2 +- .github/workflows/README.md | 2 +- contrib/snap/README.md | 2 +- doc/lz4_Block_format.md | 4 ++-- examples/blockStreaming_lineByLine.md | 4 ++-- examples/dictionaryRandomAccess.md | 2 +- examples/streaming_api_basics.md | 2 +- lib/README.md | 2 +- lib/dll/example/README.md | 2 +- lib/lz4.c | 2 +- programs/bench.c | 2 +- programs/lz4cli.c | 2 +- tests/README.md | 2 +- 13 files changed, 15 insertions(+), 15 deletions(-) diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md index 86b7696..e47afe7 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -26,7 +26,7 @@ If applicable, add screenshots to help explain your problem. - Version [e.g. 22] - Compiler [e.g. gcc] - Build System [e.g. Makefile] - - Other hardware specs [e.g Core 2 duo...] + - Other hardware specs [e.g. Core 2 duo...] **Additional context** Add any other context about the problem here. diff --git a/.github/workflows/README.md b/.github/workflows/README.md index eaa9f03..306d875 100644 --- a/.github/workflows/README.md +++ b/.github/workflows/README.md @@ -5,7 +5,7 @@ This directory contains [GitHub Actions](https://github.com/features/actions) wo ## USAN, ASAN (`lz4-ubsan-x64`, `lz4-ubsan-x86`, `lz4-asan-x64`) For now, `lz4-ubsan-*` ignores the exit code of `make usan` and `make usan32`. -Because there're several issues which may take relatively long time to resolve. +Because there are several issues which may take relatively long time to resolve. We'll fully enable it when we ensure `make usan` is ready for all commits and PRs. diff --git a/contrib/snap/README.md b/contrib/snap/README.md index 612d6d7..55c97e0 100644 --- a/contrib/snap/README.md +++ b/contrib/snap/README.md @@ -6,7 +6,7 @@ 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 +executes within a controlled environment 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). diff --git a/doc/lz4_Block_format.md b/doc/lz4_Block_format.md index 8a4b083..9e80227 100644 --- a/doc/lz4_Block_format.md +++ b/doc/lz4_Block_format.md @@ -60,7 +60,7 @@ Example 2 : A literal length of 280 will be represented as : - 15 : value for the 4-bits High field - 255 : following byte is maxed, since 280-15 >= 255 - - 10 : (=280 - 15 - 255) ) remaining length to reach 280 + - 10 : (=280 - 15 - 255) remaining length to reach 280 Example 3 : A literal length of 15 will be represented as : @@ -239,6 +239,6 @@ searches and selects matches within the source data block. For example, an upper compression limit can be reached, using a technique called "full optimal parsing", at high cpu and memory cost. But multiple other techniques can be considered, -featuring distinct time / performance trade offs. +featuring distinct time / performance trade-offs. As long as the specified format is respected, the result will be compatible with and decodable by any compliant decoder. diff --git a/examples/blockStreaming_lineByLine.md b/examples/blockStreaming_lineByLine.md index 90342f6..7b66883 100644 --- a/examples/blockStreaming_lineByLine.md +++ b/examples/blockStreaming_lineByLine.md @@ -107,7 +107,7 @@ This is called "External Dictionary Mode". In Line#X+2 (see (5)), finally LZ4 forget almost all memories but still remains Line#X+1. This is the same situation as Line#2. -Continue these procedure to the end of text file. +Continue these procedures to the end of text file. ## How the decompression works @@ -119,4 +119,4 @@ Decompression will do reverse order. - Output decompressed plain text line to the file. - Forward ringbuffer offset. If offset exceeds end of the ringbuffer, reset it. -Continue these procedure to the end of the compressed file. +Continue these procedures to the end of the compressed file. diff --git a/examples/dictionaryRandomAccess.md b/examples/dictionaryRandomAccess.md index fb1fade..c6f4388 100644 --- a/examples/dictionaryRandomAccess.md +++ b/examples/dictionaryRandomAccess.md @@ -64,4 +64,4 @@ Decompression will do reverse order. - Read the next block. - Decompress it and write that page to the file. -Continue these procedure until all the required data has been read. +Continue these procedures until all the required data has been read. diff --git a/examples/streaming_api_basics.md b/examples/streaming_api_basics.md index abffaef..6f5ae41 100644 --- a/examples/streaming_api_basics.md +++ b/examples/streaming_api_basics.md @@ -9,7 +9,7 @@ LZ4 has the following API sets : It guarantees interoperability with other LZ4 framing format compliant tools/libraries 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. + It compresses single raw memory block to LZ4 memory block and vice versa. - "Streaming" API : This is designed for complex things. For example, compress huge stream data in restricted memory environment. diff --git a/lib/README.md b/lib/README.md index d08e0f1..244d65c 100644 --- a/lib/README.md +++ b/lib/README.md @@ -96,7 +96,7 @@ The following build macro can be selected to adjust source code behavior at comp 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 . + 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. diff --git a/lib/dll/example/README.md b/lib/dll/example/README.md index 6d93248..b93914b 100644 --- a/lib/dll/example/README.md +++ b/lib/dll/example/README.md @@ -51,7 +51,7 @@ The compiled executable will require LZ4 DLL which is available at `dll\msys-lz4 Open `example\fullbench-dll.sln` to compile `fullbench-dll` that uses a dynamic LZ4 library from the `dll` directory. The solution works with Visual C++ 2010 or newer. When one will open the solution with Visual C++ newer than 2010 -then the solution will upgraded to the current version. +then the solution will be upgraded to the current version. #### Using LZ4 DLL with Visual C++ diff --git a/lib/lz4.c b/lib/lz4.c index 0dd337a..9d85ba1 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -538,7 +538,7 @@ static unsigned LZ4_NbCommonBytes (reg_t val) * including _tzcnt_u64. Therefore, we need to neuter the _tzcnt_u64 code path for ARM64EC. ****************************************************************************************************/ # if defined(__clang__) && (__clang_major__ < 10) - /* Avoid undefined clang-cl intrinics issue. + /* Avoid undefined clang-cl intrinsics issue. * See https://github.com/lz4/lz4/pull/1017 for details. */ return (unsigned)__builtin_ia32_tzcnt_u64(val) >> 3; # else diff --git a/programs/bench.c b/programs/bench.c index 5a56e6f..4d35ef9 100644 --- a/programs/bench.c +++ b/programs/bench.c @@ -409,7 +409,7 @@ static int BMK_benchMem(const void* srcBuffer, size_t srcSize, remaining -= thisBlockSize; } } } - /* warmimg up memory */ + /* warming up memory */ RDG_genBuffer(compressedBuffer, maxCompressedSize, 0.10, 0.50, 1); /* decode-only mode : copy input to @compressedBuffer */ diff --git a/programs/lz4cli.c b/programs/lz4cli.c index 51969fd..8c3f9fd 100644 --- a/programs/lz4cli.c +++ b/programs/lz4cli.c @@ -186,7 +186,7 @@ static int usage_longhelp(const char* exeName) DISPLAY( "\n"); DISPLAY( "Compression levels : \n"); DISPLAY( "---------------------\n"); - DISPLAY( "-0 ... -2 => Fast compression, all identicals\n"); + DISPLAY( "-0 ... -2 => Fast compression, all identical\n"); DISPLAY( "-3 ... -%d => High compression; higher number == more compression but slower\n", LZ4HC_CLEVEL_MAX); DISPLAY( "\n"); DISPLAY( "stdin, stdout and the console : \n"); diff --git a/tests/README.md b/tests/README.md index 6b8302c..65437de 100644 --- a/tests/README.md +++ b/tests/README.md @@ -25,7 +25,7 @@ After `sleepTime` (an optional parameter, default 300 seconds) seconds the scrip If a new commit is found it is compiled and a speed benchmark for this commit is performed. The results of the speed benchmark are compared to the previous results. If compression or decompression speed for one of lz4 levels is lower than `lowerLimit` (an optional parameter, default 0.98) the speed benchmark is restarted. -If second results are also lower than `lowerLimit` the warning e-mail is send to recipients from the list (the `emails` parameter). +If second results are also lower than `lowerLimit` the warning e-mail is sent to recipients from the list (the `emails` parameter). Additional remarks: - To be sure that speed results are accurate the script should be run on a "stable" target system with no other jobs running in parallel -- cgit v0.12 From 9173ca37d793a6cbb1e5a8657d8e6ebafc53230c Mon Sep 17 00:00:00 2001 From: Takayuki Matsuoka Date: Mon, 1 Aug 2022 06:12:45 +0900 Subject: Fix : Internal memory allocation macro names --- lib/lz4.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 77b24f7..b9c5a65 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -189,12 +189,9 @@ * Memory routines **************************************/ #if defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION) -# undef LZ4_ALLOC -# undef LZ4_ALLOC_AND_ZERO -# undef LZ4_FREEMEM -# define LZ4_ALLOC(x) lz4_error_memory_allocation_is_disabled -# define LZ4_ALLOC_AND_ZERO(x) lz4_error_memory_allocation_is_disabled -# define LZ4_FREEMEM(x) lz4_error_memory_allocation_is_disabled +# define ALLOC(s) lz4_error_memory_allocation_is_disabled +# define ALLOC_AND_ZERO(s) lz4_error_memory_allocation_is_disabled +# define FREEMEM(p) lz4_error_memory_allocation_is_disabled #elif defined(LZ4_USER_MEMORY_FUNCTIONS) /* memory management functions can be customized by user project. * Below functions must exist somewhere in the Project -- cgit v0.12 From efd123e1f150afe3c612ab392a28cb7475b0bd98 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 2 Aug 2022 12:50:21 +0200 Subject: introduce LZ4_decompress_unsafe_generic() designed to support specifically LZ4_decompress_fast*() variants. The end goal is to offload this support from LZ4_decompress_generic to improve its maintenance. --- lib/lz4.c | 176 +++++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 158 insertions(+), 18 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 9d85ba1..d041753 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1737,6 +1737,128 @@ typedef enum { decode_full_block = 0, partial_decode = 1 } earlyEnd_directive; #undef MIN #define MIN(a,b) ( (a) < (b) ? (a) : (b) ) + +/* variant for decompress_unsafe() + * does not know end of input + * presumes input is well formed + * note : will consume at least one byte */ +size_t read_long_length_no_check(const BYTE** pp) +{ + size_t b, l = 0; + do { b = **pp; (*pp)++; l += b; } while (b==255); + DEBUGLOG(6, "read_long_length_no_check: +length=%zu using %zu input bytes", l, l/255 + 1) + return l; +} + +/* core decoder variant for LZ4_decompress_fast*() + * for legacy support only : these entry points are deprecated. + * - Presumes input is correctly formed (no defense vs malformed inputs) + * - Does not know input size (presume input buffer is "large enough") + * - Decompress a full block (only) + * @return : nb of bytes read from input. + * Note : this variant is not optimized for speed, just for maintenance. + * the goal is to remove support of decompress_fast*() variants by v2.0 +**/ +LZ4_FORCE_INLINE int +LZ4_decompress_unsafe_generic( + const BYTE* const istart, + BYTE* const ostart, + int decompressedSize, + + size_t prefixSize, + const BYTE* const dictStart, /* only if dict==usingExtDict */ + const size_t dictSize /* note: =0 if dictStart==NULL */ + ) +{ + const BYTE* ip = istart; + BYTE* op = (BYTE*)ostart; + BYTE* const oend = ostart + decompressedSize; + const BYTE* const prefixStart = ostart - prefixSize; + + DEBUGLOG(5, "LZ4_decompress_unsafe_generic"); + if (dictStart == NULL) assert(dictSize == 0); + + while (1) { + /* start new sequence */ + unsigned token = *ip++; + + /* literals */ + { size_t ll = token >> ML_BITS; + if (ll==15) { + /* long literal length */ + ll += read_long_length_no_check(&ip); + } + if ((size_t)(oend-op) < ll) return -1; /* output buffer overflow */ + LZ4_memmove(op, ip, ll); /* support in-place decompression */ + op += ll; + ip += ll; + if ((size_t)(oend-op) < MFLIMIT) { + if (op==oend) break; /* end of block */ + DEBUGLOG(5, "invalid: literals end at distance %zi from end of block", oend-op); + /* incorrect end of block : + * last match must start at least MFLIMIT==12 bytes before end of output block */ + return -1; + } } + + /* match */ + { size_t ml = token & 15; + size_t const offset = LZ4_readLE16(ip); + ip+=2; + + if (ml==15) { + /* long literal length */ + ml += read_long_length_no_check(&ip); + } + ml += MINMATCH; + + if ((size_t)(oend-op) < ml) return -1; /* output buffer overflow */ + + { const BYTE* match = op - offset; + + /* out of range */ + if (offset > (size_t)(op - prefixStart) + dictSize) { + DEBUGLOG(6, "offset out of range"); + return -1; + } + + /* check special case : extDict */ + if (offset > (size_t)(op - prefixStart)) { + /* extDict scenario */ + const BYTE* const dictEnd = dictStart + dictSize; + const BYTE* extMatch = dictEnd - (offset - (size_t)(op-prefixStart)); + size_t const extml = (size_t)(dictEnd - extMatch); + if (extml > ml) { + /* match entirely within extDict */ + LZ4_memmove(op, extMatch, ml); + op += ml; + ml = 0; + } else { + /* match split between extDict & prefix */ + LZ4_memmove(op, extMatch, extml); + op += extml; + ml -= extml; + } + match = prefixStart; + } + + /* match copy - slow variant, supporting overlap copy */ + { size_t u; + for (u=0; u internal_donotuse; + LZ4_streamDecode_t_internal* const lz4sd = + (assert(LZ4_streamDecode!=NULL), &LZ4_streamDecode->internal_donotuse); int result; + + DEBUGLOG(5, "LZ4_decompress_fast_continue (toDecodeSize=%i)", originalSize); assert(originalSize >= 0); if (lz4sd->prefixSize == 0) { + DEBUGLOG(5, "first invocation : no prefix nor extDict"); assert(lz4sd->extDictSize == 0); result = LZ4_decompress_fast(source, dest, originalSize); if (result <= 0) return result; 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) - result = LZ4_decompress_fast(source, dest, originalSize); - else - result = LZ4_decompress_fast_doubleDict(source, dest, originalSize, - lz4sd->prefixSize, lz4sd->externalDict, lz4sd->extDictSize); + DEBUGLOG(5, "continue using existing prefix"); + result = LZ4_decompress_unsafe_generic( + (const BYTE*)source, (BYTE*)dest, originalSize, + lz4sd->prefixSize, + lz4sd->externalDict, lz4sd->extDictSize); if (result <= 0) return result; lz4sd->prefixSize += (size_t)originalSize; lz4sd->prefixEnd += originalSize; } else { + DEBUGLOG(5, "prefix becomes extDict"); lz4sd->extDictSize = lz4sd->prefixSize; lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize; result = LZ4_decompress_fast_extDict(source, dest, originalSize, @@ -2510,7 +2648,9 @@ int LZ4_decompress_safe_partial_usingDict(const char* source, char* dest, int co 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_unsafe_generic( + (const BYTE*)source, (BYTE*)dest, originalSize, + (size_t)dictSize, NULL, 0); assert(dictSize >= 0); return LZ4_decompress_fast_extDict(source, dest, originalSize, dictStart, (size_t)dictSize); } -- cgit v0.12 From 7d7cddfac1c438a4c781dbdc2ffeb248f668cd4c Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 2 Aug 2022 14:36:01 +0200 Subject: remove support of decompress_fast*() from decompress_generic() since it's now supported by decompress_unsafe(). The goal is to improve maintenability of decompress_generic() by reducing its complexity. --- lib/lz4.c | 135 ++++++++++++++++++++------------------------------------------ 1 file changed, 43 insertions(+), 92 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index d041753..fe14609 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1716,7 +1716,7 @@ int LZ4_saveDict (LZ4_stream_t* LZ4_dict, char* safeBuffer, int dictSize) if (dictSize > 0) { const BYTE* const previousDictEnd = dict->dictionary + dict->dictSize; assert(dict->dictionary); - LZ4_memmove(safeBuffer, previousDictEnd - dictSize, dictSize); + LZ4_memmove(safeBuffer, previousDictEnd - dictSize, (size_t)dictSize); } dict->dictionary = (const BYTE*)safeBuffer; @@ -1731,7 +1731,6 @@ int LZ4_saveDict (LZ4_stream_t* LZ4_dict, char* safeBuffer, int dictSize) * Decompression functions ********************************/ -typedef enum { endOnOutputSize = 0, endOnInputSize = 1 } endCondition_directive; typedef enum { decode_full_block = 0, partial_decode = 1 } earlyEnd_directive; #undef MIN @@ -1870,7 +1869,7 @@ LZ4_decompress_unsafe_generic( 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, + int initial_check, variable_length_error* error) { U32 length = 0; @@ -1883,7 +1882,7 @@ read_variable_length(const BYTE**ip, const BYTE* lencheck, s = **ip; (*ip)++; length += s; - if (loop_check && unlikely((*ip) >= lencheck)) { /* overflow detection */ + if (unlikely((*ip) >= lencheck)) { /* overflow detection */ *error = loop_error; return length; } @@ -1905,7 +1904,6 @@ LZ4_decompress_generic( int srcSize, int outputSize, /* If endOnInput==endOnInputSize, this value is `dstCapacity` */ - endCondition_directive endOnInput, /* endOnOutputSize, endOnInputSize */ earlyEnd_directive partialDecoding, /* full, partial */ dict_directive dict, /* noDict, withPrefix64k, usingExtDict */ const BYTE* const lowPrefix, /* always <= dst, == dst when no prefix */ @@ -1924,13 +1922,12 @@ LZ4_decompress_generic( const BYTE* const dictEnd = (dictStart == NULL) ? NULL : dictStart + dictSize; - const int safeDecode = (endOnInput==endOnInputSize); - const int checkOffset = ((safeDecode) && (dictSize < (int)(64 KB))); + const int checkOffset = (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* const shortiend = iend - 14 /*maxLL*/ - 2 /*offset*/; + const BYTE* const shortoend = oend - 14 /*maxLL*/ - 18 /*maxML*/; const BYTE* match; size_t offset; @@ -1942,13 +1939,12 @@ LZ4_decompress_generic( /* Special cases */ assert(lowPrefix <= op); - if ((endOnInput) && (unlikely(outputSize==0))) { + if (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; } + if (unlikely(srcSize==0)) { return -1; } /* Currently the fast loop shows a regression on qualcomm arm chips. */ #if LZ4_FAST_DEC_LOOP @@ -1961,46 +1957,31 @@ 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); } + assert(ip < iend); 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, (int)endOnInput, (int)endOnInput, &error); + length += read_variable_length(&ip, iend-RUN_MASK, 1, &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 (unlikely((uptrval)(op)+length<(uptrval)(op))) { goto _output_error; } /* overflow detection */ + if (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; } - LZ4_wildCopy32(op, ip, cpy); - } 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 */ - } + if ((cpy>oend-32) || (ip+length>iend-32)) { goto safe_literal_copy; } + LZ4_wildCopy32(op, ip, cpy); ip += length; op = cpy; } else { cpy = op+length; - 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 */ - 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 */ - LZ4_memcpy(op, ip, 8); - if (length > 8) { LZ4_memcpy(op+8, ip+8, 8); } - } + 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 */ + LZ4_memcpy(op, ip, 16); ip += length; op = cpy; } @@ -2015,9 +1996,9 @@ 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, (int)endOnInput, 0, &error); + length += read_variable_length(&ip, iend - LASTLITERALS + 1, 0, &error); if (error != ok) { goto _output_error; } - if ((safeDecode) && unlikely((uptrval)(op)+length<(uptrval)op)) { goto _output_error; } /* overflow detection */ + if (unlikely((uptrval)(op)+length<(uptrval)op)) { goto _output_error; } /* overflow detection */ length += MINMATCH; if (op + length >= oend - FASTLOOP_SAFE_DISTANCE) { goto safe_match_copy; @@ -2091,11 +2072,10 @@ LZ4_decompress_generic( /* Main Loop : decode remaining sequences where output < FASTLOOP_SAFE_DISTANCE */ while (1) { + assert(ip < iend); token = *ip++; length = token >> ML_BITS; /* literal length */ - assert(!endOnInput || ip <= iend); /* ip < iend before the increment */ - /* 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 @@ -2105,11 +2085,11 @@ LZ4_decompress_generic( * 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) + if ( (length != RUN_MASK) /* strictly "less than" on input, to re-enter the loop with at least one byte */ - && likely((endOnInput ? ip < shortiend : 1) & (op <= shortoend)) ) { + && likely((ip < shortiend) & (op <= shortoend)) ) { /* Copy the literals */ - LZ4_memcpy(op, ip, endOnInput ? 16 : 8); + LZ4_memcpy(op, ip, 16); op += length; ip += length; /* The second stage: prepare for match copying, decode full info. @@ -2140,10 +2120,10 @@ LZ4_decompress_generic( /* decode literal length */ if (length == RUN_MASK) { variable_length_error error = ok; - length += read_variable_length(&ip, iend-RUN_MASK, (int)endOnInput, (int)endOnInput, &error); + length += read_variable_length(&ip, iend-RUN_MASK, 1, &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 (unlikely((uptrval)(op)+length<(uptrval)(op))) { goto _output_error; } /* overflow detection */ + if (unlikely((uptrval)(ip)+length<(uptrval)(ip))) { goto _output_error; } /* overflow detection */ } /* copy literals */ @@ -2152,9 +2132,7 @@ LZ4_decompress_generic( safe_literal_copy: #endif LZ4_STATIC_ASSERT(MFLIMIT >= WILDCOPYLENGTH); - if ( ((endOnInput) && ((cpy>oend-MFLIMIT) || (ip+length>iend-(2+1+LASTLITERALS))) ) - || ((!endOnInput) && (cpy>oend-WILDCOPYLENGTH)) ) - { + if ((cpy>oend-MFLIMIT) || (ip+length>iend-(2+1+LASTLITERALS))) { /* We've either hit the input parsing restriction or the output parsing restriction. * In the normal scenario, decoding a full block, it must be the last sequence, * otherwise it's an error (invalid input or dimensions). @@ -2164,7 +2142,6 @@ LZ4_decompress_generic( /* 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); 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)); @@ -2185,14 +2162,10 @@ LZ4_decompress_generic( length = (size_t)(oend-op); } } 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). - */ - 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))) { + if ((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); @@ -2225,9 +2198,9 @@ LZ4_decompress_generic( _copy_match: if (length == ML_MASK) { variable_length_error error = ok; - length += read_variable_length(&ip, iend - LASTLITERALS + 1, (int)endOnInput, 0, &error); + length += read_variable_length(&ip, iend - LASTLITERALS + 1, 0, &error); if (error != ok) goto _output_error; - if ((safeDecode) && unlikely((uptrval)(op)+length<(uptrval)op)) goto _output_error; /* overflow detection */ + if (unlikely((uptrval)(op)+length<(uptrval)op)) goto _output_error; /* overflow detection */ } length += MINMATCH; @@ -2315,12 +2288,8 @@ 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 */ - } + DEBUGLOG(5, "decoded %i bytes", (int) (((char*)op)-dst)); + return (int) (((char*)op)-dst); /* Nb of output bytes decoded */ /* Overflow error detected */ _output_error: @@ -2335,7 +2304,7 @@ LZ4_FORCE_O2 int LZ4_decompress_safe(const char* source, char* dest, int compressedSize, int maxDecompressedSize) { return LZ4_decompress_generic(source, dest, compressedSize, maxDecompressedSize, - endOnInputSize, decode_full_block, noDict, + decode_full_block, noDict, (BYTE*)dest, NULL, 0); } @@ -2344,7 +2313,7 @@ int LZ4_decompress_safe_partial(const char* src, char* dst, int compressedSize, { dstCapacity = MIN(targetOutputSize, dstCapacity); return LZ4_decompress_generic(src, dst, compressedSize, dstCapacity, - endOnInputSize, partial_decode, + partial_decode, noDict, (BYTE*)dst, NULL, 0); } @@ -2352,15 +2321,9 @@ LZ4_FORCE_O2 int LZ4_decompress_fast(const char* source, char* dest, int originalSize) { DEBUGLOG(5, "LZ4_decompress_fast"); -#if 0 - return LZ4_decompress_generic(source, dest, 0, originalSize, - endOnOutputSize, decode_full_block, withPrefix64k, - (BYTE*)dest - 64 KB, NULL, 0); -#else return LZ4_decompress_unsafe_generic( (const BYTE*)source, (BYTE*)dest, originalSize, 0, NULL, 0); -#endif } /*===== Instantiate a few more decoding cases, used more than once. =====*/ @@ -2369,7 +2332,7 @@ 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, - endOnInputSize, decode_full_block, withPrefix64k, + decode_full_block, withPrefix64k, (BYTE*)dest - 64 KB, NULL, 0); } @@ -2378,22 +2341,16 @@ static int LZ4_decompress_safe_partial_withPrefix64k(const char* source, char* d { dstCapacity = MIN(targetOutputSize, dstCapacity); return LZ4_decompress_generic(source, dest, compressedSize, dstCapacity, - endOnInputSize, partial_decode, withPrefix64k, + partial_decode, withPrefix64k, (BYTE*)dest - 64 KB, NULL, 0); } /* Another obsolete API function, paired with the previous one. */ int LZ4_decompress_fast_withPrefix64k(const char* source, char* dest, int originalSize) { -#if 0 - /* LZ4_decompress_fast doesn't validate match offsets, - * and thus serves well with any prefixed dictionary. */ - return LZ4_decompress_fast(source, dest, originalSize); -#else return LZ4_decompress_unsafe_generic( (const BYTE*)source, (BYTE*)dest, originalSize, 64 KB, NULL, 0); -#endif } LZ4_FORCE_O2 @@ -2401,7 +2358,7 @@ static int LZ4_decompress_safe_withSmallPrefix(const char* source, char* dest, i size_t prefixSize) { return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, - endOnInputSize, decode_full_block, noDict, + decode_full_block, noDict, (BYTE*)dest-prefixSize, NULL, 0); } @@ -2411,7 +2368,7 @@ static int LZ4_decompress_safe_partial_withSmallPrefix(const char* source, char* { dstCapacity = MIN(targetOutputSize, dstCapacity); return LZ4_decompress_generic(source, dest, compressedSize, dstCapacity, - endOnInputSize, partial_decode, noDict, + partial_decode, noDict, (BYTE*)dest-prefixSize, NULL, 0); } @@ -2421,7 +2378,7 @@ int LZ4_decompress_safe_forceExtDict(const char* source, char* dest, const void* dictStart, size_t dictSize) { return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, - endOnInputSize, decode_full_block, usingExtDict, + decode_full_block, usingExtDict, (BYTE*)dest, (const BYTE*)dictStart, dictSize); } @@ -2432,7 +2389,7 @@ int LZ4_decompress_safe_partial_forceExtDict(const char* source, char* dest, { dstCapacity = MIN(targetOutputSize, dstCapacity); return LZ4_decompress_generic(source, dest, compressedSize, dstCapacity, - endOnInputSize, partial_decode, usingExtDict, + partial_decode, usingExtDict, (BYTE*)dest, (const BYTE*)dictStart, dictSize); } @@ -2440,15 +2397,9 @@ LZ4_FORCE_O2 static int LZ4_decompress_fast_extDict(const char* source, char* dest, int originalSize, const void* dictStart, size_t dictSize) { -#if 0 - return LZ4_decompress_generic(source, dest, 0, originalSize, - endOnOutputSize, decode_full_block, usingExtDict, - (BYTE*)dest, (const BYTE*)dictStart, dictSize); -#else return LZ4_decompress_unsafe_generic( (const BYTE*)source, (BYTE*)dest, originalSize, 0, (const BYTE*)dictStart, dictSize); -#endif } /* The "double dictionary" mode, for use with e.g. ring buffers: the first part @@ -2460,7 +2411,7 @@ int LZ4_decompress_safe_doubleDict(const char* source, char* dest, int compresse size_t prefixSize, const void* dictStart, size_t dictSize) { return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, - endOnInputSize, decode_full_block, usingExtDict, + decode_full_block, usingExtDict, (BYTE*)dest-prefixSize, (const BYTE*)dictStart, dictSize); } -- cgit v0.12 From 63e9a622493edfbd9fd05c96f043e74016609836 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 2 Aug 2022 15:56:05 +0200 Subject: refactor read_variable_length() updated documentation, more assert(), overflow detection in 32-bit mode --- lib/lz4.c | 48 +++++++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index fe14609..2aeb051 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1860,21 +1860,23 @@ LZ4_decompress_unsafe_generic( /* 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. - */ + * @ip : input pointer + * @ilimit : position after which if length is not decoded, the input is necessarily corrupted. + * @initial_check - check ip >= ipmax before start of loop. Returns initial_error if so. + * @error (output) - error code. Must 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, +typedef size_t Rvl_t; +LZ4_FORCE_INLINE Rvl_t +read_variable_length(const BYTE**ip, const BYTE* ilimit, int initial_check, variable_length_error* error) { - U32 length = 0; - U32 s; - if (initial_check && unlikely((*ip) >= lencheck)) { /* overflow detection */ + Rvl_t s, length = 0; + assert(ip != NULL); + assert(*ip != NULL); + assert(ilimit != NULL); + if (initial_check && unlikely((*ip) >= ilimit)) { /* overflow detection */ *error = initial_error; return length; } @@ -1882,7 +1884,12 @@ read_variable_length(const BYTE**ip, const BYTE* lencheck, s = **ip; (*ip)++; length += s; - if (unlikely((*ip) >= lencheck)) { /* overflow detection */ + if (unlikely((*ip) >= ilimit)) { /* overflow detection */ + *error = loop_error; + return length; + } + /* accumulator overflow detection (32-bit mode only) */ + if ((sizeof(length)<8) && unlikely(length > ((Rvl_t)(-1)/2)) ) { *error = loop_error; return length; } @@ -1946,14 +1953,17 @@ LZ4_decompress_generic( } if (unlikely(srcSize==0)) { return -1; } - /* Currently the fast loop shows a regression on qualcomm arm chips. */ + /* LZ4_FAST_DEC_LOOP: + * designed for modern OoO performance cpus, + * where copying reliably 32-bytes is preferable to an unpredictable branch. + * note : fast loop may show a regression for some client arm chips. */ #if LZ4_FAST_DEC_LOOP if ((oend - op) < FASTLOOP_SAFE_DISTANCE) { DEBUGLOG(6, "skip fast decode loop"); goto safe_decode; } - /* Fast loop : decode sequences as long as output < iend-FASTLOOP_SAFE_DISTANCE */ + /* Fast loop : decode sequences as long as output < oend-FASTLOOP_SAFE_DISTANCE */ while (1) { /* Main fastloop assertion: We can always wildcopy FASTLOOP_SAFE_DISTANCE */ assert(oend - op >= FASTLOOP_SAFE_DISTANCE); @@ -1980,7 +1990,7 @@ LZ4_decompress_generic( 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 */ + /* Literals can only be <= 14, but hope compilers optimize better when copy by a register size */ LZ4_memcpy(op, ip, 16); ip += length; op = cpy; } @@ -1988,7 +1998,7 @@ LZ4_decompress_generic( /* get offset */ offset = LZ4_readLE16(ip); ip+=2; match = op - offset; - assert(match <= op); + assert(match <= op); /* overflow check */ /* get matchlength */ length = token & ML_MASK; @@ -2009,7 +2019,7 @@ LZ4_decompress_generic( goto safe_match_copy; } - /* Fastpath check: Avoids a branch in LZ4_wildCopy32 if true */ + /* Fastpath check: skip LZ4_wildCopy32 when true */ if ((dict == withPrefix64k) || (match >= lowPrefix)) { if (offset >= 8) { assert(match >= lowPrefix); @@ -2172,7 +2182,7 @@ LZ4_decompress_generic( goto _output_error; } } - LZ4_memmove(op, ip, length); /* supports overlapping memory regions; only matters for in-place decompression scenarios */ + LZ4_memmove(op, ip, length); /* supports overlapping memory regions, for in-place decompression scenarios */ ip += length; op += length; /* Necessarily EOF when !partialDecoding. @@ -2184,7 +2194,7 @@ LZ4_decompress_generic( break; } } else { - LZ4_wildCopy8(op, ip, cpy); /* may overwrite up to WILDCOPYLENGTH beyond cpy */ + LZ4_wildCopy8(op, ip, cpy); /* can overwrite up to 8 bytes beyond cpy */ ip += length; op = cpy; } -- cgit v0.12 From ccd92cb43ba6e6565752edc87a78dcdcf3180317 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Tue, 2 Aug 2022 16:47:37 +0200 Subject: simplify read_variable_length() single sumtype return value --- lib/lz4.c | 50 ++++++++++++++++++++++++-------------------------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 2aeb051..de0194f 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -1865,33 +1865,29 @@ LZ4_decompress_unsafe_generic( * @initial_check - check ip >= ipmax before start of loop. Returns initial_error if so. * @error (output) - error code. Must be set to 0 before call. **/ -typedef enum { loop_error = -2, initial_error = -1, ok = 0 } variable_length_error; typedef size_t Rvl_t; +static const Rvl_t rvl_error = (Rvl_t)(-1); LZ4_FORCE_INLINE Rvl_t -read_variable_length(const BYTE**ip, const BYTE* ilimit, - int initial_check, - variable_length_error* error) +read_variable_length(const BYTE** ip, const BYTE* ilimit, + int initial_check) { Rvl_t s, length = 0; assert(ip != NULL); assert(*ip != NULL); assert(ilimit != NULL); - if (initial_check && unlikely((*ip) >= ilimit)) { /* overflow detection */ - *error = initial_error; - return length; + if (initial_check && unlikely((*ip) >= ilimit)) { /* read limit reached */ + return rvl_error; } do { s = **ip; (*ip)++; length += s; - if (unlikely((*ip) >= ilimit)) { /* overflow detection */ - *error = loop_error; - return length; + if (unlikely((*ip) > ilimit)) { /* read limit reached */ + return rvl_error; } /* accumulator overflow detection (32-bit mode only) */ if ((sizeof(length)<8) && unlikely(length > ((Rvl_t)(-1)/2)) ) { - *error = loop_error; - return length; + return rvl_error; } } while (s==255); @@ -1973,9 +1969,9 @@ LZ4_decompress_generic( /* decode literal length */ if (length == RUN_MASK) { - variable_length_error error = ok; - length += read_variable_length(&ip, iend-RUN_MASK, 1, &error); - if (error == initial_error) { goto _output_error; } + size_t const addl = read_variable_length(&ip, iend-RUN_MASK, 1); + if (addl == rvl_error) { goto _output_error; } + length += addl; if (unlikely((uptrval)(op)+length<(uptrval)(op))) { goto _output_error; } /* overflow detection */ if (unlikely((uptrval)(ip)+length<(uptrval)(ip))) { goto _output_error; } /* overflow detection */ @@ -2004,12 +2000,12 @@ 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, 0, &error); - if (error != ok) { goto _output_error; } - if (unlikely((uptrval)(op)+length<(uptrval)op)) { goto _output_error; } /* overflow detection */ + size_t const addl = read_variable_length(&ip, iend - LASTLITERALS + 1, 0); + if (addl == rvl_error) { goto _output_error; } + length += addl; length += MINMATCH; + if (unlikely((uptrval)(op)+length<(uptrval)op)) { goto _output_error; } /* overflow detection */ + if ((checkOffset) && (unlikely(match + dictSize < lowPrefix))) { goto _output_error; } /* Error : offset outside buffers */ if (op + length >= oend - FASTLOOP_SAFE_DISTANCE) { goto safe_match_copy; } @@ -2036,6 +2032,7 @@ LZ4_decompress_generic( if (checkOffset && (unlikely(match + dictSize < lowPrefix))) { goto _output_error; } /* Error : offset outside buffers */ /* match starting within external dictionary */ if ((dict==usingExtDict) && (match < lowPrefix)) { + assert(dictEnd != NULL); if (unlikely(op+length > oend-LASTLITERALS)) { if (partialDecoding) { DEBUGLOG(7, "partialDecoding: dictionary match, close to dstEnd"); @@ -2129,9 +2126,9 @@ LZ4_decompress_generic( /* decode literal length */ if (length == RUN_MASK) { - variable_length_error error = ok; - length += read_variable_length(&ip, iend-RUN_MASK, 1, &error); - if (error == initial_error) { goto _output_error; } + size_t const addl = read_variable_length(&ip, iend-RUN_MASK, 1); + if (addl == rvl_error) { goto _output_error; } + length += addl; if (unlikely((uptrval)(op)+length<(uptrval)(op))) { goto _output_error; } /* overflow detection */ if (unlikely((uptrval)(ip)+length<(uptrval)(ip))) { goto _output_error; } /* overflow detection */ } @@ -2207,9 +2204,9 @@ LZ4_decompress_generic( _copy_match: if (length == ML_MASK) { - variable_length_error error = ok; - length += read_variable_length(&ip, iend - LASTLITERALS + 1, 0, &error); - if (error != ok) goto _output_error; + size_t const addl = read_variable_length(&ip, iend - LASTLITERALS + 1, 0); + if (addl == rvl_error) { goto _output_error; } + length += addl; if (unlikely((uptrval)(op)+length<(uptrval)op)) goto _output_error; /* overflow detection */ } length += MINMATCH; @@ -2220,6 +2217,7 @@ LZ4_decompress_generic( if ((checkOffset) && (unlikely(match + dictSize < lowPrefix))) goto _output_error; /* Error : offset outside buffers */ /* match starting within external dictionary */ if ((dict==usingExtDict) && (match < lowPrefix)) { + assert(dictEnd != NULL); if (unlikely(op+length > oend-LASTLITERALS)) { if (partialDecoding) length = MIN(length, (size_t)(oend-op)); else goto _output_error; /* doesn't respect parsing restriction */ -- cgit v0.12 From d9e0741aee5ef902c4a4b0ca42167f9b19734e69 Mon Sep 17 00:00:00 2001 From: Takayuki Matsuoka Date: Sat, 6 Aug 2022 19:45:13 +0900 Subject: Add: Doxygen comment for LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION --- lib/lz4.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/lz4.c b/lib/lz4.c index 5fae002..6ece5dc 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -188,6 +188,17 @@ /*-************************************ * Memory routines **************************************/ + +/*! LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION : + * Disable relatively high-level LZ4/HC functions that use dynamic memory + * allocation functions (malloc(), calloc(), free()). + * + * Note that this is a compile-time switch. And since it disables + * public/stable LZ4 v1 API functions, we don't recommend using this + * symbol to generate a library for distribution. + * + * The following functions are removed when this symbol is defined. + */ #if defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION) # define ALLOC(s) lz4_error_memory_allocation_is_disabled # define ALLOC_AND_ZERO(s) lz4_error_memory_allocation_is_disabled -- cgit v0.12 From e1276aebe2736af82f0022196a62ae7a9050a0c4 Mon Sep 17 00:00:00 2001 From: Takayuki Matsuoka Date: Sat, 6 Aug 2022 19:46:01 +0900 Subject: Fix: Disable prototypes in header file --- lib/lz4.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/lz4.h b/lib/lz4.h index fee8890..7c37ab3 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -281,8 +281,10 @@ 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) */ +#if !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION) LZ4LIB_API LZ4_stream_t* LZ4_createStream(void); LZ4LIB_API int LZ4_freeStream (LZ4_stream_t* streamPtr); +#endif /* !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION) */ /*! LZ4_resetStream_fast() : v1.9.0+ * Use this to prepare an LZ4_stream_t for a new chain of dependent blocks @@ -366,8 +368,10 @@ typedef union LZ4_streamDecode_u LZ4_streamDecode_t; /* tracking context */ * creation / destruction of streaming decompression tracking context. * A tracking context can be re-used multiple times. */ +#if !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION) LZ4LIB_API LZ4_streamDecode_t* LZ4_createStreamDecode(void); LZ4LIB_API int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream); +#endif /* !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION) */ /*! LZ4_setStreamDecode() : * An LZ4_streamDecode_t context can be allocated once and re-used multiple times. -- cgit v0.12 From 721e76d1af1d248d0e6fefb2a090cd4b1c4a08f2 Mon Sep 17 00:00:00 2001 From: Takayuki Matsuoka Date: Sun, 7 Aug 2022 19:06:37 +0900 Subject: Add LZ4_FREESTANDING --- lib/lz4.c | 4 +++- lib/lz4.h | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/lz4.c b/lib/lz4.c index 5fae002..ad12281 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -209,7 +209,9 @@ void LZ4_free(void* p); # define FREEMEM(p) free(p) #endif -#include /* memset, memcpy */ +#if ! LZ4_FREESTANDING +# include /* memset, memcpy */ +#endif #if !defined(LZ4_memset) # define LZ4_memset(p,v,s) memset((p),(v),(s)) #endif diff --git a/lib/lz4.h b/lib/lz4.h index fee8890..01ab445 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -97,6 +97,29 @@ extern "C" { # define LZ4LIB_API LZ4LIB_VISIBILITY #endif +/*! + * LZ4_FREESTANDING : + * Enable "freestanding mode" that is suitable for typical freestanding environment. + * In freestanding mode, some LZ4/HC functions which use heap are disabled. + */ +#if defined(LZ4_FREESTANDING) && (LZ4_FREESTANDING == 1) +# define LZ4_HEAPMODE 0 +# define LZ4HC_HEAPMODE 0 +# define LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION 1 +# if !defined(LZ4_memcpy) +# error "LZ4_FREESTANDING requires macro 'LZ4_memcpy'." +# endif +# if !defined(LZ4_memset) +# error "LZ4_FREESTANDING requires macro 'LZ4_memset'." +# endif +# if !defined(LZ4_memmove) +# error "LZ4_FREESTANDING requires macro 'LZ4_memmove'." +# endif +#elif ! defined(LZ4_FREESTANDING) +# define LZ4_FREESTANDING 0 +#endif + + /*------ Version ------*/ #define LZ4_VERSION_MAJOR 1 /* for breaking interface changes */ #define LZ4_VERSION_MINOR 9 /* for new (non-breaking) interface capabilities */ -- cgit v0.12 From 50915609a9a0c1feb616c4de534f4a388d48f79a Mon Sep 17 00:00:00 2001 From: Takayuki Matsuoka Date: Sun, 7 Aug 2022 19:07:41 +0900 Subject: Fix: Disable LZ4HC correspond functions when LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION is enabled --- lib/lz4hc.c | 2 ++ lib/lz4hc.h | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/lz4hc.c b/lib/lz4hc.c index 4771ef8..b21ad6b 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -1225,6 +1225,7 @@ int LZ4_resetStreamStateHC(void* state, char* inputBuffer) return 0; } +#if !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION) void* LZ4_createHC (const char* inputBuffer) { LZ4_streamHC_t* const hc4 = LZ4_createStreamHC(); @@ -1239,6 +1240,7 @@ int LZ4_freeHC (void* LZ4HC_Data) FREEMEM(LZ4HC_Data); return 0; } +#endif int LZ4_compressHC2_continue (void* LZ4HC_Data, const char* src, char* dst, int srcSize, int cLevel) { diff --git a/lib/lz4hc.h b/lib/lz4hc.h index d2e5193..e937acf 100644 --- a/lib/lz4hc.h +++ b/lib/lz4hc.h @@ -270,9 +270,11 @@ LZ4_DEPRECATED("use LZ4_compress_HC_continue() instead") LZ4LIB_API int LZ4_comp * LZ4_slideInputBufferHC() will truncate the history of the stream, rather * than preserve a window-sized chunk of history. */ +#if !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION) 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); +#endif +LZ4_DEPRECATED("use LZ4_saveDictHC() instead") LZ4LIB_API char* LZ4_slideInputBufferHC (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_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); -- cgit v0.12 From f88f02f78c6c609c912ce0ee1773ee07d6e2e7ac Mon Sep 17 00:00:00 2001 From: Takayuki Matsuoka Date: Sun, 7 Aug 2022 19:08:59 +0900 Subject: Add LZ4_FREESTANDING test on Linux x86-64 platform Also added tests/Makefile entry "test-freestanding". --- tests/.gitignore | 1 + tests/Makefile | 15 +++- tests/freestanding.c | 231 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 244 insertions(+), 3 deletions(-) create mode 100644 tests/freestanding.c diff --git a/tests/.gitignore b/tests/.gitignore index 5337fdb..c7d8f19 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -14,6 +14,7 @@ checkFrame decompress-partial decompress-partial-usingDict abiTest +freestanding # test artefacts tmp* diff --git a/tests/Makefile b/tests/Makefile index 4b6ea48..33309b4 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -56,7 +56,7 @@ NB_LOOPS ?= -i1 .PHONY: default default: all -all: fullbench fuzzer frametest roundTripTest datagen checkFrame decompress-partial +all: fullbench fuzzer frametest roundTripTest datagen checkFrame decompress-partial freestanding all32: CFLAGS+=-m32 all32: all @@ -115,6 +115,9 @@ decompress-partial: lz4.o decompress-partial.c decompress-partial-usingDict: lz4.o decompress-partial-usingDict.c $(CC) $(FLAGS) $^ -o $@$(EXT) +freestanding: freestanding.c + $(CC) -ffreestanding -nostdlib $^ -o $@$(EXT) + .PHONY: clean clean: @$(MAKE) -C $(LZ4DIR) $@ > $(VOID) @@ -127,7 +130,7 @@ clean: fasttest$(EXT) roundTripTest$(EXT) \ datagen$(EXT) checkTag$(EXT) \ frameTest$(EXT) decompress-partial$(EXT) \ - abiTest$(EXT) \ + abiTest$(EXT) freestanding$(EXT) \ lz4_all.c @$(RM) -rf $(TESTDIR) @echo Cleaning completed @@ -179,7 +182,7 @@ list: check: test-lz4-essentials .PHONY: test -test: test-lz4 test-lz4c test-frametest test-fullbench test-fuzzer test-amalgamation listTest test-decompress-partial +test: test-lz4 test-lz4c test-frametest test-fullbench test-fuzzer test-amalgamation listTest test-decompress-partial test-freestanding .PHONY: test32 test32: CFLAGS+=-m32 @@ -606,4 +609,10 @@ test-decompress-partial : decompress-partial decompress-partial-usingDict @echo "\n ---- test decompress-partial-usingDict ----" ./decompress-partial-usingDict$(EXT) +test-freestanding: freestanding + @echo "\n ---- test freestanding ----" + ./freestanding$(EXT) + strace ./freestanding$(EXT) + ltrace ./freestanding$(EXT) + endif diff --git a/tests/freestanding.c b/tests/freestanding.c new file mode 100644 index 0000000..2781358 --- /dev/null +++ b/tests/freestanding.c @@ -0,0 +1,231 @@ +// Basic test for LZ4_FREESTANDING + +// $ gcc -ffreestanding -nostdlib freestanding.c && ./a.out || echo $? + +// $ strace ./a.out +// execve("./a.out", ["./a.out"], 0x7fffaf5fa580 /* 22 vars */) = 0 +// brk(NULL) = 0x56536f4fe000 +// arch_prctl(0x3001 /* ARCH_??? */, 0x7fffc9e74950) = -1 EINVAL (Invalid argument) +// mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fd5c9c2b000 +// access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory) +// arch_prctl(ARCH_SET_FS, 0x7fd5c9c2bc40) = 0 +// set_tid_address(0x7fd5c9c2bf10) = 381 +// set_robust_list(0x7fd5c9c2bf20, 24) = 0 +// rseq(0x7fd5c9c2c5e0, 0x20, 0, 0x53053053) = 0 +// mprotect(0x56536ea63000, 4096, PROT_READ) = 0 +// exit(0) = ? +// +++ exited with 0 +++ + +// $ ltrace ./a.out +// +++ exited (status 0) +++ + +#if !defined(__x86_64__) || !defined(__linux__) +# error This test only supports Linux __x86_64__ +#endif + +#include +#include + +static void MY_exit(int exitCode); +static void MY_abort(void); +void *memmove(void *dst, const void *src, size_t n); +void *memcpy(void * restrict dst, const void * restrict src, size_t n); +void *memset(void *s, int c, size_t n); +int memcmp(const void *s1, const void *s2, size_t n); + +// LZ4/HC basic freestanding setup +#define LZ4_FREESTANDING 1 +#define LZ4_memmove(dst, src, size) memmove((dst),(src),(size)) +#define LZ4_memcpy(dst, src, size) memcpy((dst),(src),(size)) +#define LZ4_memset(p,v,s) memset((p),(v),(s)) + +#include "../lib/lz4.c" +#include "../lib/lz4hc.c" + + +// Test for LZ4 +static void test_lz4(const uint8_t* srcData, int srcSize) { + // Compress + static uint8_t compressBuffer[1024 * 1024]; + const int compressedSize = LZ4_compress_default( + srcData, + compressBuffer, + srcSize, + sizeof(compressBuffer) + ); + if (compressedSize <= 0) { + MY_exit(__LINE__); + } + + // Decompress + static uint8_t decompressBuffer[1024 * 1024]; + const int decompressedSize = LZ4_decompress_safe( + compressBuffer, + decompressBuffer, + compressedSize, + sizeof(decompressBuffer) + ); + if (decompressedSize <= 0) { + MY_exit(__LINE__); + } + + // Verify + if (decompressedSize != srcSize) { + MY_exit(__LINE__); + } + if (memcmp(srcData, decompressBuffer, srcSize) != 0) { + MY_exit(__LINE__); + } +} + + +// Test for LZ4HC +static void test_lz4hc(const uint8_t* srcData, int srcSize) { + // Compress + static uint8_t compressBuffer[1024 * 1024]; + const int compressedSize = LZ4_compress_HC( + srcData, + compressBuffer, + srcSize, + sizeof(compressBuffer), + LZ4HC_CLEVEL_DEFAULT + ); + if (compressedSize <= 0) { + MY_exit(__LINE__); + } + + // Decompress + static uint8_t decompressBuffer[1024 * 1024]; + const int decompressedSize = LZ4_decompress_safe( + compressBuffer, + decompressBuffer, + compressedSize, + sizeof(decompressBuffer) + ); + if (decompressedSize <= 0) { + MY_exit(__LINE__); + } + + // Verify + if (decompressedSize != srcSize) { + MY_exit(__LINE__); + } + if (memcmp(srcData, decompressBuffer, srcSize) != 0) { + MY_exit(__LINE__); + } +} + + +static void test(void) { + // First 256 bytes of lz4/README.md + static const uint8_t README_md[] = { + 0x4c, 0x5a, 0x34, 0x20, 0x2d, 0x20, 0x45, 0x78, 0x74, 0x72, 0x65, 0x6d, 0x65, 0x6c, 0x79, 0x20, + 0x66, 0x61, 0x73, 0x74, 0x20, 0x63, 0x6f, 0x6d, 0x70, 0x72, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, + 0x0a, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, + 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, + 0x3d, 0x0a, 0x0a, 0x4c, 0x5a, 0x34, 0x20, 0x69, 0x73, 0x20, 0x6c, 0x6f, 0x73, 0x73, 0x6c, 0x65, + 0x73, 0x73, 0x20, 0x63, 0x6f, 0x6d, 0x70, 0x72, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x20, 0x61, + 0x6c, 0x67, 0x6f, 0x72, 0x69, 0x74, 0x68, 0x6d, 0x2c, 0x0a, 0x70, 0x72, 0x6f, 0x76, 0x69, 0x64, + 0x69, 0x6e, 0x67, 0x20, 0x63, 0x6f, 0x6d, 0x70, 0x72, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x20, + 0x73, 0x70, 0x65, 0x65, 0x64, 0x20, 0x3e, 0x20, 0x35, 0x30, 0x30, 0x20, 0x4d, 0x42, 0x2f, 0x73, + 0x20, 0x70, 0x65, 0x72, 0x20, 0x63, 0x6f, 0x72, 0x65, 0x2c, 0x0a, 0x73, 0x63, 0x61, 0x6c, 0x61, + 0x62, 0x6c, 0x65, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x2d, 0x63, + 0x6f, 0x72, 0x65, 0x73, 0x20, 0x43, 0x50, 0x55, 0x2e, 0x0a, 0x49, 0x74, 0x20, 0x66, 0x65, 0x61, + 0x74, 0x75, 0x72, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x20, 0x65, 0x78, 0x74, 0x72, 0x65, 0x6d, 0x65, + 0x6c, 0x79, 0x20, 0x66, 0x61, 0x73, 0x74, 0x20, 0x64, 0x65, 0x63, 0x6f, 0x64, 0x65, 0x72, 0x2c, + 0x0a, 0x77, 0x69, 0x74, 0x68, 0x20, 0x73, 0x70, 0x65, 0x65, 0x64, 0x20, 0x69, 0x6e, 0x20, 0x6d, + 0x75, 0x6c, 0x74, 0x69, 0x70, 0x6c, 0x65, 0x20, 0x47, 0x42, 0x2f, 0x73, 0x20, 0x70, 0x65, 0x72, + }; + + static const uint8_t* srcData = README_md; + static const int srcSize = (int) sizeof(README_md); + test_lz4 (srcData, srcSize); + test_lz4hc(srcData, srcSize); +} + + +// low level syscall +#define SYS_exit (60) + +static __inline long os_syscall1(long n, long a1) { + register long rax __asm__ ("rax") = n; + register long rdi __asm__ ("rdi") = a1; + __asm__ __volatile__ ("syscall" : "+r"(rax) : "r"(rdi) : "rcx", "r11", "memory"); + return rax; +} + +static void MY_exit(int exitCode) { + (void) os_syscall1(SYS_exit, exitCode); + __builtin_unreachable(); // suppress "warning: 'noreturn' function does return" +} + +static void MY_abort(void) { + MY_exit(-1); +} + +// https://refspecs.linuxbase.org/LSB_3.0.0/LSB-Core-generic/LSB-Core-generic/baselib---assert-fail-1.html +void __assert_fail(const char * assertion, const char * file, unsigned int line, const char * function) { + MY_abort(); +} + + +// GCC requires memcpy, memmove, memset and memcmp. +// https://gcc.gnu.org/onlinedocs/gcc/Standards.html +// > GCC requires the freestanding environment provide memcpy, memmove, memset and memcmp. +void *memmove(void *dst, const void *src, size_t n) { + uint8_t* d = dst; + const uint8_t* s = src; + + if (d > s) { + d += n; + s += n; + while (n--) { + *--d = *--s; + } + } else { + while (n--) { + *d++ = *s++; + } + } + return dst; +} + +void *memcpy(void * restrict dst, const void * restrict src, size_t n) { + return memmove(dst, src, n); +} + +void *memset(void *s, int c, size_t n) { + uint8_t* p = s; + while (n--) { + *p++ = (uint8_t) c; + } + return s; +} + +int memcmp(const void *s1, const void *s2, size_t n) { + const uint8_t* p1 = (const uint8_t*) s1; + const uint8_t* p2 = (const uint8_t*) s2; + while (n--) { + const uint8_t c1 = *p1++; + const uint8_t c2 = *p2++; + if (c1 < c2) { + return -1; + } else if (c1 > c2) { + return 1; + } + } + return 0; +} + + +// +void _start(void) { + test(); + MY_exit(0); +} + +int main(int argc, char** argv) { + test(); + MY_exit(0); + return 0; +} -- cgit v0.12 From 70e76e50b1f011685d95622049e63cf2bd47e5f8 Mon Sep 17 00:00:00 2001 From: Takayuki Matsuoka Date: Sun, 7 Aug 2022 19:26:01 +0900 Subject: Fix for ctocpptest --- tests/freestanding.c | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/tests/freestanding.c b/tests/freestanding.c index 2781358..00cd695 100644 --- a/tests/freestanding.c +++ b/tests/freestanding.c @@ -29,7 +29,7 @@ static void MY_exit(int exitCode); static void MY_abort(void); void *memmove(void *dst, const void *src, size_t n); -void *memcpy(void * restrict dst, const void * restrict src, size_t n); +void *memcpy(void * __restrict__ dst, const void * __restrict__ src, size_t n); void *memset(void *s, int c, size_t n); int memcmp(const void *s1, const void *s2, size_t n); @@ -48,8 +48,8 @@ static void test_lz4(const uint8_t* srcData, int srcSize) { // Compress static uint8_t compressBuffer[1024 * 1024]; const int compressedSize = LZ4_compress_default( - srcData, - compressBuffer, + (const char*) srcData, + (char*) compressBuffer, srcSize, sizeof(compressBuffer) ); @@ -60,8 +60,8 @@ static void test_lz4(const uint8_t* srcData, int srcSize) { // Decompress static uint8_t decompressBuffer[1024 * 1024]; const int decompressedSize = LZ4_decompress_safe( - compressBuffer, - decompressBuffer, + (const char*) compressBuffer, + (char*) decompressBuffer, compressedSize, sizeof(decompressBuffer) ); @@ -84,8 +84,8 @@ static void test_lz4hc(const uint8_t* srcData, int srcSize) { // Compress static uint8_t compressBuffer[1024 * 1024]; const int compressedSize = LZ4_compress_HC( - srcData, - compressBuffer, + (const char*) srcData, + (char*) compressBuffer, srcSize, sizeof(compressBuffer), LZ4HC_CLEVEL_DEFAULT @@ -97,8 +97,8 @@ static void test_lz4hc(const uint8_t* srcData, int srcSize) { // Decompress static uint8_t decompressBuffer[1024 * 1024]; const int decompressedSize = LZ4_decompress_safe( - compressBuffer, - decompressBuffer, + (const char*) compressBuffer, + (char*) decompressBuffer, compressedSize, sizeof(decompressBuffer) ); @@ -173,8 +173,8 @@ void __assert_fail(const char * assertion, const char * file, unsigned int line, // https://gcc.gnu.org/onlinedocs/gcc/Standards.html // > GCC requires the freestanding environment provide memcpy, memmove, memset and memcmp. void *memmove(void *dst, const void *src, size_t n) { - uint8_t* d = dst; - const uint8_t* s = src; + uint8_t* d = (uint8_t*) dst; + const uint8_t* s = (const uint8_t*) src; if (d > s) { d += n; @@ -190,12 +190,12 @@ void *memmove(void *dst, const void *src, size_t n) { return dst; } -void *memcpy(void * restrict dst, const void * restrict src, size_t n) { +void *memcpy(void * __restrict__ dst, const void * __restrict__ src, size_t n) { return memmove(dst, src, n); } void *memset(void *s, int c, size_t n) { - uint8_t* p = s; + uint8_t* p = (uint8_t*) s; while (n--) { *p++ = (uint8_t) c; } @@ -219,6 +219,9 @@ int memcmp(const void *s1, const void *s2, size_t n) { // +#if defined(__cplusplus) +extern "C" +#endif void _start(void) { test(); MY_exit(0); -- cgit v0.12 From cdf515c90ff32f681be3ee97e5889f8cdd9ed143 Mon Sep 17 00:00:00 2001 From: Takayuki Matsuoka Date: Sun, 7 Aug 2022 19:39:50 +0900 Subject: Fix: Add extern "C" to all standard C replacement functions --- tests/freestanding.c | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/tests/freestanding.c b/tests/freestanding.c index 00cd695..6bf2e6d 100644 --- a/tests/freestanding.c +++ b/tests/freestanding.c @@ -26,12 +26,19 @@ #include #include +#if defined(__cplusplus) +# define EXTERN_C extern "C" +#else +# define EXTERN_C +#endif + + static void MY_exit(int exitCode); static void MY_abort(void); -void *memmove(void *dst, const void *src, size_t n); -void *memcpy(void * __restrict__ dst, const void * __restrict__ src, size_t n); -void *memset(void *s, int c, size_t n); -int memcmp(const void *s1, const void *s2, size_t n); +EXTERN_C void *memmove(void *dst, const void *src, size_t n); +EXTERN_C void *memcpy(void * __restrict__ dst, const void * __restrict__ src, size_t n); +EXTERN_C void *memset(void *s, int c, size_t n); +EXTERN_C int memcmp(const void *s1, const void *s2, size_t n); // LZ4/HC basic freestanding setup #define LZ4_FREESTANDING 1 @@ -42,7 +49,6 @@ int memcmp(const void *s1, const void *s2, size_t n); #include "../lib/lz4.c" #include "../lib/lz4hc.c" - // Test for LZ4 static void test_lz4(const uint8_t* srcData, int srcSize) { // Compress @@ -172,7 +178,7 @@ void __assert_fail(const char * assertion, const char * file, unsigned int line, // GCC requires memcpy, memmove, memset and memcmp. // https://gcc.gnu.org/onlinedocs/gcc/Standards.html // > GCC requires the freestanding environment provide memcpy, memmove, memset and memcmp. -void *memmove(void *dst, const void *src, size_t n) { +EXTERN_C void *memmove(void *dst, const void *src, size_t n) { uint8_t* d = (uint8_t*) dst; const uint8_t* s = (const uint8_t*) src; @@ -190,11 +196,11 @@ void *memmove(void *dst, const void *src, size_t n) { return dst; } -void *memcpy(void * __restrict__ dst, const void * __restrict__ src, size_t n) { +EXTERN_C void *memcpy(void * __restrict__ dst, const void * __restrict__ src, size_t n) { return memmove(dst, src, n); } -void *memset(void *s, int c, size_t n) { +EXTERN_C void *memset(void *s, int c, size_t n) { uint8_t* p = (uint8_t*) s; while (n--) { *p++ = (uint8_t) c; @@ -202,7 +208,7 @@ void *memset(void *s, int c, size_t n) { return s; } -int memcmp(const void *s1, const void *s2, size_t n) { +EXTERN_C int memcmp(const void *s1, const void *s2, size_t n) { const uint8_t* p1 = (const uint8_t*) s1; const uint8_t* p2 = (const uint8_t*) s2; while (n--) { @@ -219,10 +225,7 @@ int memcmp(const void *s1, const void *s2, size_t n) { // -#if defined(__cplusplus) -extern "C" -#endif -void _start(void) { +EXTERN_C void _start(void) { test(); MY_exit(0); } -- cgit v0.12 From 431a793b243360bd962e8f6f02884606693b34d9 Mon Sep 17 00:00:00 2001 From: Takayuki Matsuoka Date: Sun, 7 Aug 2022 19:43:54 +0900 Subject: Fix: implement empty _start and main for non-Linux or x86-64 platforms --- tests/freestanding.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/freestanding.c b/tests/freestanding.c index 6bf2e6d..ceff4c5 100644 --- a/tests/freestanding.c +++ b/tests/freestanding.c @@ -19,10 +19,6 @@ // $ ltrace ./a.out // +++ exited (status 0) +++ -#if !defined(__x86_64__) || !defined(__linux__) -# error This test only supports Linux __x86_64__ -#endif - #include #include @@ -33,6 +29,11 @@ #endif +#if !defined(__x86_64__) || !defined(__linux__) +EXTERN_C void _start(void) { } +int main(int argc, char** argv) { return 0; } +#else + static void MY_exit(int exitCode); static void MY_abort(void); EXTERN_C void *memmove(void *dst, const void *src, size_t n); @@ -235,3 +236,4 @@ int main(int argc, char** argv) { MY_exit(0); return 0; } +#endif -- cgit v0.12 From 79b40d61b87531e76c70871e60166811a9e57563 Mon Sep 17 00:00:00 2001 From: Takayuki Matsuoka Date: Sun, 7 Aug 2022 20:07:01 +0900 Subject: Fix: Ignore exitcode of strace and ltrace --- tests/Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Makefile b/tests/Makefile index 33309b4..dc63041 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -612,7 +612,7 @@ test-decompress-partial : decompress-partial decompress-partial-usingDict test-freestanding: freestanding @echo "\n ---- test freestanding ----" ./freestanding$(EXT) - strace ./freestanding$(EXT) - ltrace ./freestanding$(EXT) + -strace ./freestanding$(EXT) + -ltrace ./freestanding$(EXT) endif -- cgit v0.12 From aa2699707b575d55dbc22e96e81cad6357cd8526 Mon Sep 17 00:00:00 2001 From: Takayuki Matsuoka Date: Mon, 8 Aug 2022 18:04:13 +0900 Subject: Remove test-freestanding from tests/Makefile "all" and "test" Since test-freestanding is able to be compiled and executed in specific environment, we should not run it in our standard test. --- tests/Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Makefile b/tests/Makefile index dc63041..93a5581 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -56,7 +56,7 @@ NB_LOOPS ?= -i1 .PHONY: default default: all -all: fullbench fuzzer frametest roundTripTest datagen checkFrame decompress-partial freestanding +all: fullbench fuzzer frametest roundTripTest datagen checkFrame decompress-partial all32: CFLAGS+=-m32 all32: all @@ -182,7 +182,7 @@ list: check: test-lz4-essentials .PHONY: test -test: test-lz4 test-lz4c test-frametest test-fullbench test-fuzzer test-amalgamation listTest test-decompress-partial test-freestanding +test: test-lz4 test-lz4c test-frametest test-fullbench test-fuzzer test-amalgamation listTest test-decompress-partial .PHONY: test32 test32: CFLAGS+=-m32 -- cgit v0.12 From 4b74762203201997a5d01c075809fd6f83b906b7 Mon Sep 17 00:00:00 2001 From: Takayuki Matsuoka Date: Mon, 8 Aug 2022 18:04:48 +0900 Subject: Add test-freestanding to Makefile --- Makefile | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Makefile b/Makefile index 9e12ae7..e70c3db 100644 --- a/Makefile +++ b/Makefile @@ -190,6 +190,10 @@ platformTest: clean versionsTest: clean $(MAKE) -C $(TESTDIR) $@ +.PHONY: test-freestanding +test-freestanding: + $(MAKE) -C $(TESTDIR) clean $@ + .PHONY: cxxtest cxx32test cxxtest cxx32test: CC := "$(CXX) -Wno-deprecated" cxxtest cxx32test: CFLAGS = -O3 -Wall -Wextra -Wundef -Wshadow -Wcast-align -Werror -- cgit v0.12 From d0460e4aaab84d50f2db0cb303ea0f6fe84745ce Mon Sep 17 00:00:00 2001 From: Takayuki Matsuoka Date: Mon, 8 Aug 2022 18:11:05 +0900 Subject: Add freestanding test to specific target in ci.yml The 'freestanding' flag indicates a capability of compiling and executing freestanding code. Currently it requires Linux, x86_64 and (relatively newer) gcc/g++. --- .github/workflows/ci.yml | 66 ++++++++++++++++++++++++++---------------------- 1 file changed, 36 insertions(+), 30 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3101d4f..f27b99a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -25,42 +25,44 @@ jobs: include: [ # You can access the following values via ${{ matrix.??? }} # - # pkgs : apt-get package names. It can include multiple package names which are delimited by space. - # cc : C compiler executable. - # cxx : C++ compiler executable for `make ctocpptest`. - # x32 : Set 'true' if compiler supports x32. Otherwise, set 'false'. - # Set 'fail' if it supports x32 but fails for now. 'fail' cases must be removed. - # x86 : Set 'true' if compiler supports x86 (-m32). Otherwise, set 'false'. - # Set 'fail' if it supports x86 but fails for now. 'fail' cases must be removed. - # cxxtest : Set 'true' if it can be compiled as C++ code. Otherwise, set 'false'. - # os : GitHub Actions YAML workflow label. See https://github.com/actions/virtual-environments#available-environments + # pkgs : apt-get package names. It can include multiple package names which are delimited by space. + # cc : C compiler executable. + # cxx : C++ compiler executable for `make ctocpptest`. + # x32 : Set 'true' if compiler supports x32. Otherwise, set 'false'. + # Set 'fail' if it supports x32 but fails for now. 'fail' cases must be removed. + # x86 : Set 'true' if compiler supports x86 (-m32). Otherwise, set 'false'. + # Set 'fail' if it supports x86 but fails for now. 'fail' cases must be removed. + # cxxtest : Set 'true' if it can be compiled as C++ code. Otherwise, set 'false'. + # freestanding : Set 'true' if it can be compiled and execute freestanding code. Otherwise, set 'false'. + # Usually, it requires Linux, x86_64 and gcc/g++. + # os : GitHub Actions YAML workflow label. See https://github.com/actions/virtual-environments#available-environments # cc - { pkgs: '', cc: cc, cxx: c++, x32: 'true', x86: 'true', cxxtest: 'true', os: ubuntu-latest, }, + { pkgs: '', cc: cc, cxx: c++, x32: 'true', x86: 'true', cxxtest: 'true', freestanding: 'true', os: ubuntu-latest, }, # gcc - { pkgs: '', cc: gcc, cxx: g++, x32: 'true', x86: 'true', cxxtest: 'true', os: ubuntu-latest, }, - { pkgs: 'gcc-11 g++-11 lib32gcc-11-dev libx32gcc-11-dev', cc: gcc-11, cxx: g++-11, x32: 'true', x86: 'true', cxxtest: 'true', os: ubuntu-20.04, }, - { pkgs: 'gcc-10 lib32gcc-10-dev libx32gcc-10-dev', cc: gcc-10, cxx: g++-10, x32: 'true', x86: 'true', cxxtest: 'true', os: ubuntu-20.04, }, - { pkgs: 'gcc-9 lib32gcc-9-dev libx32gcc-9-dev', cc: gcc-9, cxx: g++-9, x32: 'true', x86: 'true', cxxtest: 'true', os: ubuntu-20.04, }, - { pkgs: 'gcc-8 g++-8 lib32gcc-8-dev libx32gcc-8-dev', cc: gcc-8, cxx: g++-8, x32: 'true', x86: 'true', cxxtest: 'true', os: ubuntu-20.04, }, - { pkgs: 'gcc-7 g++-7 lib32gcc-7-dev libx32gcc-7-dev', cc: gcc-7, cxx: g++-7, x32: 'true', x86: 'true', cxxtest: 'true', os: ubuntu-20.04, }, - { pkgs: 'gcc-6 g++-6 lib32gcc-6-dev libx32gcc-6-dev', cc: gcc-6, cxx: g++-6, x32: 'true', x86: 'true', cxxtest: 'true', os: ubuntu-18.04, }, - { pkgs: 'gcc-5 g++-5 lib32gcc-5-dev libx32gcc-5-dev', cc: gcc-5, cxx: g++-5, x32: 'true', x86: 'true', cxxtest: 'true', os: ubuntu-18.04, }, - { pkgs: 'gcc-4.8 g++-4.8 lib32gcc-4.8-dev libx32gcc-4.8-dev', cc: gcc-4.8, cxx: g++-4.8, x32: 'true', x86: 'true', cxxtest: 'true', os: ubuntu-18.04, }, + { pkgs: '', cc: gcc, cxx: g++, x32: 'true', x86: 'true', cxxtest: 'true', freestanding: 'true', os: ubuntu-latest, }, + { pkgs: 'gcc-11 g++-11 lib32gcc-11-dev libx32gcc-11-dev', cc: gcc-11, cxx: g++-11, x32: 'true', x86: 'true', cxxtest: 'true', freestanding: 'true', os: ubuntu-20.04, }, + { pkgs: 'gcc-10 lib32gcc-10-dev libx32gcc-10-dev', cc: gcc-10, cxx: g++-10, x32: 'true', x86: 'true', cxxtest: 'true', freestanding: 'true', os: ubuntu-20.04, }, + { pkgs: 'gcc-9 lib32gcc-9-dev libx32gcc-9-dev', cc: gcc-9, cxx: g++-9, x32: 'true', x86: 'true', cxxtest: 'true', freestanding: 'true', os: ubuntu-20.04, }, + { pkgs: 'gcc-8 g++-8 lib32gcc-8-dev libx32gcc-8-dev', cc: gcc-8, cxx: g++-8, x32: 'true', x86: 'true', cxxtest: 'true', freestanding: 'true', os: ubuntu-20.04, }, + { pkgs: 'gcc-7 g++-7 lib32gcc-7-dev libx32gcc-7-dev', cc: gcc-7, cxx: g++-7, x32: 'true', x86: 'true', cxxtest: 'true', freestanding: 'true', os: ubuntu-20.04, }, + { pkgs: 'gcc-6 g++-6 lib32gcc-6-dev libx32gcc-6-dev', cc: gcc-6, cxx: g++-6, x32: 'true', x86: 'true', cxxtest: 'true', freestanding: 'true', os: ubuntu-18.04, }, + { pkgs: 'gcc-5 g++-5 lib32gcc-5-dev libx32gcc-5-dev', cc: gcc-5, cxx: g++-5, x32: 'true', x86: 'true', cxxtest: 'true', freestanding: 'true', os: ubuntu-18.04, }, + { pkgs: 'gcc-4.8 g++-4.8 lib32gcc-4.8-dev libx32gcc-4.8-dev', cc: gcc-4.8, cxx: g++-4.8, x32: 'true', x86: 'true', cxxtest: 'true', freestanding: 'true', os: ubuntu-18.04, }, # clang - { pkgs: 'lib32gcc-11-dev libx32gcc-11-dev', cc: clang, cxx: clang++, x32: 'true', x86: 'true', cxxtest: 'true', os: ubuntu-latest, }, - { pkgs: 'clang-12 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-12, cxx: clang++-12, x32: 'true', x86: 'true', cxxtest: 'true', os: ubuntu-20.04, }, - { pkgs: 'clang-11 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-11, cxx: clang++-11, x32: 'true', x86: 'true', cxxtest: 'true', os: ubuntu-20.04, }, - { pkgs: 'clang-10 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-10, cxx: clang++-10, x32: 'true', x86: 'true', cxxtest: 'true', os: ubuntu-20.04, }, - { pkgs: 'clang-9 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-9, cxx: clang++-9, x32: 'true', x86: 'true', cxxtest: 'true', os: ubuntu-20.04, }, - { pkgs: 'clang-8 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-8, cxx: clang++-8, x32: 'true', x86: 'true', cxxtest: 'true', os: ubuntu-20.04, }, - { pkgs: 'clang-7 lib32gcc-7-dev libx32gcc-7-dev', cc: clang-7, cxx: clang++-7, x32: 'true', x86: 'true', cxxtest: 'true', os: ubuntu-20.04, }, - { pkgs: 'clang-6.0 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-6.0, cxx: clang++-6.0, x32: 'true', x86: 'true', cxxtest: 'true', os: ubuntu-20.04, }, - { pkgs: 'clang-5.0 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-5.0, cxx: clang++-5.0, x32: 'true', x86: 'true', cxxtest: 'true', os: ubuntu-18.04, }, - { pkgs: 'clang-4.0 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-4.0, cxx: clang++-4.0, x32: 'true', x86: 'true', cxxtest: 'true', os: ubuntu-18.04, }, - { pkgs: 'clang-3.9', cc: clang-3.9, cxx: clang++-3.9, x32: 'fail', x86: 'fail', cxxtest: 'false', os: ubuntu-18.04, }, + { pkgs: 'lib32gcc-11-dev libx32gcc-11-dev', cc: clang, cxx: clang++, x32: 'true', x86: 'true', cxxtest: 'true', freestanding: 'false', os: ubuntu-latest, }, + { pkgs: 'clang-12 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-12, cxx: clang++-12, x32: 'true', x86: 'true', cxxtest: 'true', freestanding: 'false', os: ubuntu-20.04, }, + { pkgs: 'clang-11 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-11, cxx: clang++-11, x32: 'true', x86: 'true', cxxtest: 'true', freestanding: 'false', os: ubuntu-20.04, }, + { pkgs: 'clang-10 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-10, cxx: clang++-10, x32: 'true', x86: 'true', cxxtest: 'true', freestanding: 'false', os: ubuntu-20.04, }, + { pkgs: 'clang-9 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-9, cxx: clang++-9, x32: 'true', x86: 'true', cxxtest: 'true', freestanding: 'false', os: ubuntu-20.04, }, + { pkgs: 'clang-8 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-8, cxx: clang++-8, x32: 'true', x86: 'true', cxxtest: 'true', freestanding: 'false', os: ubuntu-20.04, }, + { pkgs: 'clang-7 lib32gcc-7-dev libx32gcc-7-dev', cc: clang-7, cxx: clang++-7, x32: 'true', x86: 'true', cxxtest: 'true', freestanding: 'false', os: ubuntu-20.04, }, + { pkgs: 'clang-6.0 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-6.0, cxx: clang++-6.0, x32: 'true', x86: 'true', cxxtest: 'true', freestanding: 'false', os: ubuntu-20.04, }, + { pkgs: 'clang-5.0 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-5.0, cxx: clang++-5.0, x32: 'true', x86: 'true', cxxtest: 'true', freestanding: 'false', os: ubuntu-18.04, }, + { pkgs: 'clang-4.0 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-4.0, cxx: clang++-4.0, x32: 'true', x86: 'true', cxxtest: 'true', freestanding: 'false', os: ubuntu-18.04, }, + { pkgs: 'clang-3.9', cc: clang-3.9, cxx: clang++-3.9, x32: 'fail', x86: 'fail', cxxtest: 'false', freestanding: 'false', os: ubuntu-18.04, }, ] runs-on: ${{ matrix.os }} @@ -111,6 +113,10 @@ jobs: if: ${{ matrix.cxxtest == 'true' }} run: make V=1 clean cxxtest + - name: make test-freestanding + if: ${{ matrix.freestanding == 'true' }} + run: make V=1 clean test-freestanding + - name: make -C programs default if: always() run: make V=1 -C programs clean default -- cgit v0.12 From d0928a7f2071b9f06a32780d79c0c5a0b204c360 Mon Sep 17 00:00:00 2001 From: Takayuki Matsuoka Date: Thu, 11 Aug 2022 01:09:53 +0900 Subject: Add short document of LZ4_FREESTANDING to lz4.h --- lib/lz4.h | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/lz4.h b/lib/lz4.h index 01ab445..9d93eb1 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -97,10 +97,17 @@ extern "C" { # define LZ4LIB_API LZ4LIB_VISIBILITY #endif -/*! - * LZ4_FREESTANDING : - * Enable "freestanding mode" that is suitable for typical freestanding environment. - * In freestanding mode, some LZ4/HC functions which use heap are disabled. +/*! LZ4_FREESTANDING : + * When this macro is set to 1, it enables "freestanding mode" that is + * suitable for typical freestanding environment which doesn't support + * standard C library. + * + * - LZ4_FREESTANDING is a compile-time switch. + * - It requires the following macros to be defined: + * LZ4_memcpy, LZ4_memmove, LZ4_memset. + * - It only enables LZ4/HC functions which don't use heap. + * All LZ4F_* functions are not supported. + * - See tests/freestanding.c to check its basic setup. */ #if defined(LZ4_FREESTANDING) && (LZ4_FREESTANDING == 1) # define LZ4_HEAPMODE 0 -- cgit v0.12 From 67c321935bf188d40a7ed208d30513bf6cfd6b98 Mon Sep 17 00:00:00 2001 From: Takayuki Matsuoka Date: Thu, 11 Aug 2022 16:13:24 +0900 Subject: Fix document for LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION in lz4.c --- lib/lz4.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/lz4.c b/lib/lz4.c index e37adb1..2a31735 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -197,7 +197,12 @@ * public/stable LZ4 v1 API functions, we don't recommend using this * symbol to generate a library for distribution. * - * The following functions are removed when this symbol is defined. + * The following public functions are removed when this symbol is defined. + * - lz4 : LZ4_createStream, LZ4_freeStream, + * LZ4_createStreamDecode, LZ4_freeStreamDecode, LZ4_create (deprecated) + * - lz4hc : LZ4_createStreamHC, LZ4_freeStreamHC, + * LZ4_createHC (deprecated), LZ4_freeHC (deprecated) + * - lz4frame, lz4file : All LZ4F_* functions */ #if defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION) # define ALLOC(s) lz4_error_memory_allocation_is_disabled -- cgit v0.12 From b41ee9935d4fdd8051ec89f0a14682c6821aa6ae Mon Sep 17 00:00:00 2001 From: Takayuki Matsuoka Date: Thu, 11 Aug 2022 16:13:56 +0900 Subject: Add short description of LZ4_FREESTANDING and _DISABLE_MEMORY_ALLOCATION --- lib/README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/README.md b/lib/README.md index 244d65c..c9916a9 100644 --- a/lib/README.md +++ b/lib/README.md @@ -104,6 +104,18 @@ The following build macro can be selected to adjust source code behavior at comp 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. +- `LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION` : some LZ4/HC (level 1 and 2) public + functions internally invoke dynamic memory allocation functions of the C standard library. + By defining this build macro, these LZ4/HC functions are disabled to ensure to remove + dependency to the standard library. + See also the description of this macro in lib/lz4.c. + +- `LZ4_FREESTANDING` : by setting this build macro to 1, LZ4/HC (level 1 and 2) removes + dependencies on the C standard library, including memmove, memcpy, and memset. + This build macro is designed to help use LZ4/HC in restricted environments + (embedded, bootloader, etc). + See also the description of this macro in lib/lz4.h. + #### Amalgamation -- cgit v0.12 From 18b293d9fd11d6af7c9fd6af51af71557c31d08f Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 11 Aug 2022 14:06:17 -0700 Subject: updated documentation in anticipation for `v1.9.4` release --- NEWS | 19 +++++++ doc/lz4_manual.html | 47 ++++++++++++++++-- doc/lz4frame_manual.html | 126 +++++++++++++++++++++++++++++------------------ lib/README.md | 26 +++++----- programs/lz4.1.md | 3 ++ 5 files changed, 156 insertions(+), 65 deletions(-) diff --git a/NEWS b/NEWS index 401931e..c2cef0e 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,22 @@ +v1.9.4 +perf : faster decoding speed (~+20%) on aarch64 platforms +api : new function `LZ4_decompress_safe_partial_usingDict()` by @yawqi +api : lz4frame: ability to provide custom allocators at state creation +api : can skip checksum validation for improved decoding speed +api : new experimental unit `lz4file` for file i/o API, by @anjiahao1 +api : new experimental function `LZ4F_uncompressedUpdate()`, by @alexmohr +cli : `--list` works on `stdin` input, by @Low-power +cli : `--no-crc` does not produce (compression) nor check (decompression) checksums +cli : fix: `--test` and `--list` produce an error code when parsing invalid input +cli : fix: support skippable frames when passed via `stdin`, reported by @davidmankin +build: fix: Makefile respects CFLAGS directives passed via environment variable +build: `LZ4_FREESTANDING`, new build macro for freestanding environments, by @t-mat +build: `make` and `make test` are compatible with `-j` parallel run +build: AS/400 compatibility, by @jonrumsey +build: Solaris 10 compatibility, by @pekdon +build: improved meson script, by @eli-schwartz +doc : Updated LZ4 block format, provide an "implementation notes" section + v1.9.3 perf: highly improved speed in kernel space, by @terrelln perf: faster speed with Visual Studio, thanks to @wolfpld and @remittor diff --git a/doc/lz4_manual.html b/doc/lz4_manual.html index 5461ab9..6fafb21 100644 --- a/doc/lz4_manual.html +++ b/doc/lz4_manual.html @@ -48,6 +48,35 @@ The `lz4` CLI can only manage frames.
#if defined(LZ4_FREESTANDING) && (LZ4_FREESTANDING == 1) +# define LZ4_HEAPMODE 0 +# define LZ4HC_HEAPMODE 0 +# define LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION 1 +# if !defined(LZ4_memcpy) +# error "LZ4_FREESTANDING requires macro 'LZ4_memcpy'." +# endif +# if !defined(LZ4_memset) +# error "LZ4_FREESTANDING requires macro 'LZ4_memset'." +# endif +# if !defined(LZ4_memmove) +# error "LZ4_FREESTANDING requires macro 'LZ4_memmove'." +# endif +#elif ! defined(LZ4_FREESTANDING) +# define LZ4_FREESTANDING 0 +#endif +When this macro is set to 1, it enables "freestanding mode" that is + suitable for typical freestanding environment which doesn't support + standard C library. + + - LZ4_FREESTANDING is a compile-time switch. + - It requires the following macros to be defined: + LZ4_memcpy, LZ4_memmove, LZ4_memset. + - It only enables LZ4/HC functions which don't use heap. + All LZ4F_* functions are not supported. + - See tests/freestanding.c to check its basic setup. + +
int LZ4_versionNumber (void); /**< library version number; useful to check dll version; requires v1.3.0+ */ @@ -267,8 +296,10 @@ int LZ4_compress_fast_extState (void* state, const char* src, char* dst, int srcStreaming Decompression Functions
Bufferless synchronous API-LZ4_streamDecode_t* LZ4_createStreamDecode(void); +#if !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION) +LZ4_streamDecode_t* LZ4_createStreamDecode(void); int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream); +#endif /* !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION) */creation / destruction of streaming decompression tracking context. A tracking context can be re-used multiple times. @@ -297,7 +328,10 @@ 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_safe_continue (LZ4_streamDecode_t* LZ4_streamDecode, + const char* src, char* dst, + int srcSize, int dstCapacity);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. @@ -323,7 +357,10 @@ int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream); then indicate where this data is saved using LZ4_setStreamDecode(), before decompressing next block.
-int LZ4_decompress_safe_usingDict (const char* src, char* dst, int srcSize, int dstCapcity, const char* dictStart, int dictSize); +int +LZ4_decompress_safe_usingDict(const char* src, char* dst, + int srcSize, int dstCapacity, + 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. @@ -363,7 +400,9 @@ int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream);
-LZ4LIB_STATIC_API void LZ4_attach_dictionary(LZ4_stream_t* workingStream, const LZ4_stream_t* dictionaryStream); +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. diff --git a/doc/lz4frame_manual.html b/doc/lz4frame_manual.html index 196881e..cfb437e 100644 --- a/doc/lz4frame_manual.html +++ b/doc/lz4frame_manual.html @@ -22,9 +22,9 @@
- lz4frame.h implements LZ4 frame specification (doc/lz4_Frame_format.md). - lz4frame.h provides frame compression functions that take care - of encoding standard metadata alongside LZ4-compressed blocks. + lz4frame.h implements LZ4 frame specification: see doc/lz4_Frame_format.md . + LZ4 Frames are compatible with `lz4` CLI, + and designed to be interoperable with any system.
LZ4F_errorCode_t LZ4F_createCompressionContext(LZ4F_cctx** cctxPtr, unsigned version); LZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_cctx* cctx); -The first thing to do is to create a compressionContext object, - which will keep track of operation state during streaming compression. - This is achieved using LZ4F_createCompressionContext(), which takes as argument a version. - The version provided MUST be LZ4F_VERSION. It is intended to track potential version mismatch, notably when using DLL. - The function will provide a pointer to a fully allocated LZ4F_cctx object. - If @return != zero, there context creation failed. - Once all streaming compression jobs are completed, - the state object can be released using LZ4F_freeCompressionContext(). - Note1 : LZ4F_freeCompressionContext() is always successful. Its return value can be ignored. - Note2 : LZ4F_freeCompressionContext() works fine with NULL input pointers (do nothing). - +
The first thing to do is to create a compressionContext object, + which will keep track of operation state during streaming compression. + This is achieved using LZ4F_createCompressionContext(), which takes as argument a version, + and a pointer to LZ4F_cctx*, to write the resulting pointer into. + @version provided MUST be LZ4F_VERSION. It is intended to track potential version mismatch, notably when using DLL. + The function provides a pointer to a fully allocated LZ4F_cctx object. + @cctxPtr MUST be != NULL. + If @return != zero, context creation failed. + A created compression context can be employed multiple times for consecutive streaming operations. + Once all streaming compression jobs are completed, + the state object can be released using LZ4F_freeCompressionContext(). + Note1 : LZ4F_freeCompressionContext() is always successful. Its return value can be ignored. + Note2 : LZ4F_freeCompressionContext() works fine with NULL input pointers (do nothing).
typedef struct { - unsigned stableDst; /* pledges that last 64KB decompressed data will remain available unmodified. This optimization skips storage operations in tmp buffers. */ - unsigned reserved[3]; /* must be set to zero for forward compatibility */ + unsigned stableDst; /* pledges that last 64KB decompressed data will remain available unmodified between invocations. + * This optimization skips storage operations in tmp buffers. */ + unsigned skipChecksums; /* disable checksum calculation and verification, even when one is present in frame, to save CPU time. + * Setting this option to 1 once disables all checksums for the rest of the frame. */ + unsigned reserved1; /* must be set to zero for forward compatibility */ + unsigned reserved0; /* idem */ } LZ4F_decompressOptions_t;
LZ4F_errorCode_t LZ4F_createDecompressionContext(LZ4F_dctx** dctxPtr, unsigned version); LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_dctx* dctx);Create an LZ4F_dctx object, to track all decompression operations. - The version provided MUST be LZ4F_VERSION. - The function provides a pointer to an allocated and initialized LZ4F_dctx object. - The result is an errorCode, which can be tested using LZ4F_isError(). + @version provided MUST be LZ4F_VERSION. + @dctxPtr MUST be valid. + The function fills @dctxPtr with the value of a pointer to an allocated and initialized LZ4F_dctx object. + The @return is an errorCode, which can be tested using LZ4F_isError(). dctx memory can be released using LZ4F_freeDecompressionContext(); Result of LZ4F_freeDecompressionContext() indicates current state of decompressionContext when being released. That is, it should be == 0 if decompression has been completed fully and correctly. @@ -254,11 +261,12 @@ LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_dctx* dctx);
size_t LZ4F_getFrameInfo(LZ4F_dctx* dctx, - LZ4F_frameInfo_t* frameInfoPtr, - const void* srcBuffer, size_t* srcSizePtr); +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: user can call LZ4F_decompress() directly. + Its usage is optional: user can also invoke LZ4F_decompress() directly. Extracted information will fill an existing LZ4F_frameInfo_t structure. This can be useful for allocation and dictionary identification purposes. @@ -301,10 +309,11 @@ LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_dctx* dctx);
-size_t LZ4F_decompress(LZ4F_dctx* dctx, - void* dstBuffer, size_t* dstSizePtr, - const void* srcBuffer, size_t* srcSizePtr, - const LZ4F_decompressOptions_t* dOptPtr); +size_t +LZ4F_decompress(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 data compressed in `srcBuffer`. The function requires a valid dctx state. @@ -352,10 +361,11 @@ LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_dctx* dctx); the maximum block size associated with blockSizeID.
-LZ4FLIB_STATIC_API size_t LZ4F_uncompressedUpdate(LZ4F_cctx* cctx, - void* dstBuffer, size_t dstCapacity, - const void* srcBuffer, size_t srcSize, - const LZ4F_compressOptions_t* cOptPtr); +LZ4FLIB_STATIC_API size_t +LZ4F_uncompressedUpdate(LZ4F_cctx* cctx, + void* dstBuffer, size_t dstCapacity, + const void* srcBuffer, size_t srcSize, + const LZ4F_compressOptions_t* cOptPtr);LZ4F_uncompressedUpdate() can be called repetitively to add as much data uncompressed data as necessary. Important rule: dstCapacity MUST be large enough to store the entire source buffer as no compression is done for this operation @@ -380,12 +390,12 @@ LZ4FLIB_STATIC_API void LZ4F_freeCDict(LZ4F_CDict* CDict); `dictBuffer` can be released after LZ4_CDict creation, since its content is copied within CDict
-LZ4FLIB_STATIC_API size_t LZ4F_compressFrame_usingCDict( - LZ4F_cctx* cctx, - void* dst, size_t dstCapacity, - const void* src, size_t srcSize, - const LZ4F_CDict* cdict, - const LZ4F_preferences_t* preferencesPtr); +LZ4FLIB_STATIC_API size_t +LZ4F_compressFrame_usingCDict(LZ4F_cctx* cctx, + void* dst, size_t dstCapacity, + const void* src, size_t srcSize, + const LZ4F_CDict* cdict, + const LZ4F_preferences_t* preferencesPtr);Compress an entire srcBuffer into a valid LZ4 frame using a digested Dictionary. cctx must point to a context created by LZ4F_createCompressionContext(). If cdict==NULL, compress without a dictionary. @@ -397,11 +407,11 @@ LZ4FLIB_STATIC_API void LZ4F_freeCDict(LZ4F_CDict* CDict); or an error code if it fails (can be tested using LZ4F_isError())
-LZ4FLIB_STATIC_API size_t LZ4F_compressBegin_usingCDict( - LZ4F_cctx* cctx, - void* dstBuffer, size_t dstCapacity, - const LZ4F_CDict* cdict, - const LZ4F_preferences_t* prefsPtr); +LZ4FLIB_STATIC_API size_t +LZ4F_compressBegin_usingCDict(LZ4F_cctx* cctx, + void* dstBuffer, size_t dstCapacity, + const LZ4F_CDict* cdict, + const LZ4F_preferences_t* prefsPtr);Inits streaming dictionary compression, and writes the frame header into dstBuffer. dstCapacity must be >= LZ4F_HEADER_SIZE_MAX bytes. `prefsPtr` is optional : you may provide NULL as argument, @@ -410,16 +420,36 @@ LZ4FLIB_STATIC_API void LZ4F_freeCDict(LZ4F_CDict* CDict); or an error code (which can be tested using LZ4F_isError())
-LZ4FLIB_STATIC_API size_t LZ4F_decompress_usingDict( - LZ4F_dctx* dctxPtr, - void* dstBuffer, size_t* dstSizePtr, - const void* srcBuffer, size_t* srcSizePtr, - const void* dict, size_t dictSize, - const LZ4F_decompressOptions_t* decompressOptionsPtr); +LZ4FLIB_STATIC_API size_t +LZ4F_decompress_usingDict(LZ4F_dctx* dctxPtr, + void* dstBuffer, size_t* dstSizePtr, + const void* srcBuffer, size_t* srcSizePtr, + const void* dict, size_t dictSize, + const LZ4F_decompressOptions_t* decompressOptionsPtr);Same as LZ4F_decompress(), using a predefined dictionary. Dictionary is used "in place", without any preprocessing. It must remain accessible throughout the entire frame decoding.
+typedef void* (*LZ4F_AllocFunction) (void* opaqueState, size_t size); +typedef void* (*LZ4F_CallocFunction) (void* opaqueState, size_t size); +typedef void (*LZ4F_FreeFunction) (void* opaqueState, void* address); +typedef struct { + LZ4F_AllocFunction customAlloc; + LZ4F_CallocFunction customCalloc; /* optional; when not defined, uses customAlloc + memset */ + LZ4F_FreeFunction customFree; + void* opaqueState; +} LZ4F_CustomMem; +static +#ifdef __GNUC__ +__attribute__((__unused__)) +#endif +LZ4F_CustomMem const LZ4F_defaultCMem = { NULL, NULL, NULL, NULL }; /**< this constant defers to stdlib's functions */ +These prototypes make it possible to pass custom allocation/free functions. + LZ4F_customMem is provided at state creation time, using LZ4F_create*_advanced() listed below. + All allocation/free operations will be completed using these custom variants instead of regular
ones. + +
+ diff --git a/lib/README.md b/lib/README.md index c9916a9..08d1cef 100644 --- a/lib/README.md +++ b/lib/README.md @@ -77,7 +77,7 @@ The following build macro can be selected to adjust source code behavior at comp 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 a worse compression ratio. - However, a smaller max distance can allow compatibility with specific decoders using limited memory budget. + Setting a smaller max distance could allow compatibility with specific decoders with 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. @@ -88,10 +88,6 @@ 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, @@ -104,17 +100,21 @@ The following build macro can be selected to adjust source code behavior at comp 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. -- `LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION` : some LZ4/HC (level 1 and 2) public - functions internally invoke dynamic memory allocation functions of the C standard library. - By defining this build macro, these LZ4/HC functions are disabled to ensure to remove - dependency to the standard library. - See also the description of this macro in lib/lz4.c. +- `LZ4_USER_MEMORY_FUNCTIONS` : replace calls to ` `'s `malloc()`, `calloc()` and `free()` + by user-defined functions, which must be named `LZ4_malloc()`, `LZ4_calloc()` and `LZ4_free()`. + User functions must be available at link time. -- `LZ4_FREESTANDING` : by setting this build macro to 1, LZ4/HC (level 1 and 2) removes - dependencies on the C standard library, including memmove, memcpy, and memset. +- `LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION` : + Remove support of dynamic memory allocation. + For more details, see description of this macro in `lib/lz4.c`. + +- `LZ4_FREESTANDING` : by setting this build macro to 1, + LZ4/HC removes dependencies on the C standard library, + including allocation functions and `memmove()`, `memcpy()`, and `memset()`. This build macro is designed to help use LZ4/HC in restricted environments (embedded, bootloader, etc). - See also the description of this macro in lib/lz4.h. + For more details, see description of this macro in `lib/lz4.h`. + #### Amalgamation diff --git a/programs/lz4.1.md b/programs/lz4.1.md index 39dc925..4160c64 100644 --- a/programs/lz4.1.md +++ b/programs/lz4.1.md @@ -185,6 +185,9 @@ only the latest one will be applied. * `-BD`: Blocks depend on predecessors (improves compression ratio, more noticeable on small blocks) +* `-BX`: + Generate block checksums (default:disabled) + * `--[no-]frame-crc`: Select frame checksum (default:enabled) -- cgit v0.12 From 23af1d776dda4ecc3596930c59dc29a9a7f8c28a Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 11 Aug 2022 14:33:27 -0700 Subject: updated man page --- programs/lz4.1 | 16 ++++++++++++---- programs/lz4.1.md | 8 ++++---- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/programs/lz4.1 b/programs/lz4.1 index d758ed5..7cb98d6 100644 --- a/programs/lz4.1 +++ b/programs/lz4.1 @@ -1,5 +1,5 @@ . -.TH "LZ4" "1" "July 2019" "lz4 1.9.2" "User Commands" +.TH "LZ4" "1" "August 2022" "lz4 v1.9.4" "User Commands" . .SH "NAME" \fBlz4\fR \- lz4, unlz4, lz4cat \- Compress or decompress \.lz4 files @@ -17,7 +17,7 @@ When writing scripts that need to decompress files, it is recommended to always use the name \fBlz4\fR with appropriate arguments (\fBlz4 \-d\fR or \fBlz4 \-dc\fR) instead of the names \fBunlz4\fR and \fBlz4cat\fR\. . .SH "DESCRIPTION" -\fBlz4\fR is an extremely fast lossless compression algorithm, based on \fBbyte\-aligned LZ77\fR family of compression scheme\. \fBlz4\fR offers compression speeds of 400 MB/s per core, linearly scalable with multi\-core CPUs\. It features an extremely fast decoder, with speed in multiple GB/s per core, typically reaching RAM speed limit on multi\-core systems\. The native file format is the \fB\.lz4\fR format\. +\fBlz4\fR is an extremely fast lossless compression algorithm, based on \fBbyte\-aligned LZ77\fR family of compression scheme\. \fBlz4\fR offers compression speeds > 500 MB/s per core, linearly scalable with multi\-core CPUs\. It features an extremely fast decoder, offering speed in multiple GB/s per core, typically reaching RAM speed limit on multi\-core systems\. The native file format is the \fB\.lz4\fR format\. . .SS "Difference between lz4 and gzip" \fBlz4\fR supports a command line syntax similar \fIbut not identical\fR to \fBgzip(1)\fR\. Differences are : @@ -32,7 +32,7 @@ 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 preserves original files +\fBlz4\fR preserves original files (see \fB\-\-rm\fR to erase source file on completion) . .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) @@ -121,7 +121,7 @@ Switch to ultra\-fast compression levels\. The higher the value, the faster the . .TP \fB\-\-best\fR -Set highest compression level\. Same as -12\. +Set highest compression level\. Same as \-12\. . .TP \fB\-\-favor\-decSpeed\fR @@ -169,10 +169,18 @@ Produce independent blocks (default) Blocks depend on predecessors (improves compression ratio, more noticeable on small blocks) . .TP +\fB\-BX\fR +Generate block checksums (default:disabled) +. +.TP \fB\-\-[no\-]frame\-crc\fR Select frame checksum (default:enabled) . .TP +\fB\-\-no\-crc\fR +Disable both frame and block checksums +. +.TP \fB\-\-[no\-]content\-size\fR Header includes original size (default:not present) . diff --git a/programs/lz4.1.md b/programs/lz4.1.md index 4160c64..06c06cf 100644 --- a/programs/lz4.1.md +++ b/programs/lz4.1.md @@ -20,9 +20,9 @@ DESCRIPTION `lz4` is an extremely fast lossless compression algorithm, based on **byte-aligned LZ77** family of compression scheme. -`lz4` offers compression speeds of 400 MB/s per core, linearly scalable with -multi-core CPUs. -It features an extremely fast decoder, with speed in multiple GB/s per core, +`lz4` offers compression speeds > 500 MB/s per core, +linearly scalable with multi-core CPUs. +It features an extremely fast decoder, offering speed in multiple GB/s per core, typically reaching RAM speed limit on multi-core systems. The native file format is the `.lz4` format. @@ -34,7 +34,7 @@ Differences are : * `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` preserves original files (see `--rm` to erase source file on completion) * `lz4` shows real-time notification statistics during compression or decompression of a single file (use `-q` to silence them) -- cgit v0.12 From a28421e1299be51c530f4ac0e9ad968ce127303e Mon Sep 17 00:00:00 2001 From: SpaceIm <30052553+SpaceIm@users.noreply.github.com> Date: Fri, 12 Aug 2022 00:09:48 +0200 Subject: cmake: move cmake_minimum_required() before project() cmake_minimum_required() must always be the first instruction of a CMakeLists. project() should come after cmake_minimum_required() as soon as possible. Therefore option() are moved after project(). --- build/cmake/CMakeLists.txt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/build/cmake/CMakeLists.txt b/build/cmake/CMakeLists.txt index e92115b..eb7007b 100644 --- a/build/cmake/CMakeLists.txt +++ b/build/cmake/CMakeLists.txt @@ -10,10 +10,9 @@ # 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}/../..") +cmake_minimum_required(VERSION 2.8.12) -option(LZ4_BUILD_CLI "Build lz4 program" ON) -option(LZ4_BUILD_LEGACY_LZ4C "Build lz4c program with legacy argument support" ON) +set(LZ4_TOP_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../..") # Parse version information file(STRINGS "${LZ4_TOP_SOURCE_DIR}/lib/lz4.h" LZ4_VERSION_MAJOR REGEX "^#define LZ4_VERSION_MAJOR +([0-9]+) +.*$") @@ -34,7 +33,8 @@ else() LANGUAGES C) endif() -cmake_minimum_required (VERSION 2.8.12) +option(LZ4_BUILD_CLI "Build lz4 program" ON) +option(LZ4_BUILD_LEGACY_LZ4C "Build lz4c program with legacy argument support" ON) # If LZ4 is being bundled in another project, we don't want to # install anything. However, we want to let people override this, so -- cgit v0.12 From ec487d3265cdc5387b13b5242045b87ea8880e76 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 11 Aug 2022 17:48:35 -0700 Subject: faster CLI decompression speed for frames with -BD4 setting lz4frame favors the faster prefix mode when decompressing a frame with linked blocks. This significantly improved CLI decompression on files compressed with -BD4 setting. On my laptop, decompressing `enwik9` went from 0.89s to 0.52s. This improvement is only for linked blocks. It's more visible for small block sizes. --- lib/lz4frame.c | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/lib/lz4frame.c b/lib/lz4frame.c index cba5744..174f9ae 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -1310,8 +1310,7 @@ static size_t LZ4F_decodeHeader(LZ4F_dctx* dctx, const void* src, size_t srcSize } else { dctx->dStage = dstage_getSFrameSize; return 4; - } - } + } } /* control magic number */ #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION @@ -1371,8 +1370,7 @@ static size_t LZ4F_decodeHeader(LZ4F_dctx* dctx, const void* src, size_t srcSize dctx->frameInfo.blockSizeID = (LZ4F_blockSizeID_t)blockSizeID; dctx->maxBlockSize = LZ4F_getBlockSize((LZ4F_blockSizeID_t)blockSizeID); if (contentSizeFlag) - dctx->frameRemainingSize = - dctx->frameInfo.contentSize = LZ4F_readLE64(srcPtr+6); + dctx->frameRemainingSize = dctx->frameInfo.contentSize = LZ4F_readLE64(srcPtr+6); if (dictIDFlag) dctx->frameInfo.dictID = LZ4F_readLE32(srcPtr + frameHeaderSize - 5); @@ -1467,16 +1465,14 @@ LZ4F_errorCode_t LZ4F_getFrameInfo(LZ4F_dctx* dctx, /* LZ4F_updateDict() : * only used for LZ4F_blockLinked mode - * Condition : dstPtr != NULL + * Condition : @dstPtr != NULL */ static void LZ4F_updateDict(LZ4F_dctx* dctx, const BYTE* dstPtr, size_t dstSize, const BYTE* dstBufferStart, unsigned withinTmp) { assert(dstPtr != NULL); - if (dctx->dictSize==0) { - dctx->dict = (const BYTE*)dstPtr; /* priority to prefix mode */ - } + if (dctx->dictSize==0) dctx->dict = (const BYTE*)dstPtr; /* will lead to prefix mode */ assert(dctx->dict != NULL); if (dctx->dict + dctx->dictSize == dstPtr) { /* prefix mode, everything within dstBuffer */ @@ -1813,7 +1809,10 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, } /* At this stage, input is large enough to decode a block */ + + /* First, decode and control block checksum if it exists */ if (dctx->frameInfo.blockChecksumFlag) { + assert(dctx->tmpInTarget >= 4); dctx->tmpInTarget -= 4; assert(selectedIn != NULL); /* selectedIn is defined at this stage (either srcPtr, or dctx->tmpIn) */ { U32 const readBlockCrc = LZ4F_readLE32(selectedIn + dctx->tmpInTarget); @@ -1826,17 +1825,22 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, #endif } } - if ((size_t)(dstEnd-dstPtr) >= dctx->maxBlockSize) { + /* decode directly into destination buffer if there is enough room */ + if ( ((size_t)(dstEnd-dstPtr) >= dctx->maxBlockSize) + /* unless the dictionary is stored in tmpOut: + * in which case it's faster to decode within tmpOut + * to benefit from prefix speedup */ + && !(dctx->dict!= NULL && (const BYTE*)dctx->dict + dctx->dictSize == dctx->tmpOut) ) + { 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 */ + /* overflow control : dctx->dictSize is an int, avoid truncation / sign issues */ dict += dictSize - 64 KB; dictSize = 64 KB; } - /* enough capacity in `dst` to decompress directly there */ decodedSize = LZ4_decompress_safe_usingDict( (const char*)selectedIn, (char*)dstPtr, (int)dctx->tmpInTarget, (int)dctx->maxBlockSize, @@ -1853,25 +1857,27 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, } dstPtr += decodedSize; - dctx->dStage = dstage_getBlockHeader; + dctx->dStage = dstage_getBlockHeader; /* end of block, let's get another one */ break; } /* not enough place into dst : decode into tmpOut */ - /* ensure enough place for tmpOut */ + + /* manage dictionary */ if (dctx->frameInfo.blockMode == LZ4F_blockLinked) { if (dctx->dict == dctx->tmpOutBuffer) { + /* truncate dictionary to 64 KB if too big */ if (dctx->dictSize > 128 KB) { memcpy(dctx->tmpOutBuffer, dctx->dict + dctx->dictSize - 64 KB, 64 KB); dctx->dictSize = 64 KB; } dctx->tmpOut = dctx->tmpOutBuffer + dctx->dictSize; - } else { /* dict not within tmp */ + } else { /* dict not within tmpOut */ size_t const reservedDictSpace = MIN(dctx->dictSize, 64 KB); dctx->tmpOut = dctx->tmpOutBuffer + reservedDictSpace; } } - /* Decode block */ + /* Decode block into tmpOut */ { const char* dict = (const char*)dctx->dict; size_t dictSize = dctx->dictSize; int decodedSize; @@ -2014,7 +2020,7 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, } /* switch (dctx->dStage) */ } /* while (doAnotherStage) */ - /* preserve history within tmp whenever necessary */ + /* preserve history within tmpOut whenever necessary */ 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 */ -- cgit v0.12 From 07e3fd8b6482080d4c377c918a71875bff14cb2b Mon Sep 17 00:00:00 2001 From: Takayuki Matsuoka Date: Fri, 12 Aug 2022 13:01:47 +0900 Subject: Add ubuntu-22.04 and the latest C/C++ compilers for Linux --- .github/workflows/ci.yml | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f27b99a..c490bc7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -42,9 +42,10 @@ jobs: # gcc { pkgs: '', cc: gcc, cxx: g++, x32: 'true', x86: 'true', cxxtest: 'true', freestanding: 'true', os: ubuntu-latest, }, - { pkgs: 'gcc-11 g++-11 lib32gcc-11-dev libx32gcc-11-dev', cc: gcc-11, cxx: g++-11, x32: 'true', x86: 'true', cxxtest: 'true', freestanding: 'true', os: ubuntu-20.04, }, - { pkgs: 'gcc-10 lib32gcc-10-dev libx32gcc-10-dev', cc: gcc-10, cxx: g++-10, x32: 'true', x86: 'true', cxxtest: 'true', freestanding: 'true', os: ubuntu-20.04, }, - { pkgs: 'gcc-9 lib32gcc-9-dev libx32gcc-9-dev', cc: gcc-9, cxx: g++-9, x32: 'true', x86: 'true', cxxtest: 'true', freestanding: 'true', os: ubuntu-20.04, }, + { pkgs: 'gcc-12 g++-12 lib32gcc-12-dev libx32gcc-12-dev', cc: gcc-12, cxx: g++-12, x32: 'true', x86: 'true', cxxtest: 'true', freestanding: 'true', os: ubuntu-22.04, }, + { pkgs: 'gcc-11 g++-11 lib32gcc-11-dev libx32gcc-11-dev', cc: gcc-11, cxx: g++-11, x32: 'true', x86: 'true', cxxtest: 'true', freestanding: 'true', os: ubuntu-22.04, }, + { pkgs: 'gcc-10 g++-10 lib32gcc-10-dev libx32gcc-10-dev', cc: gcc-10, cxx: g++-10, x32: 'true', x86: 'true', cxxtest: 'true', freestanding: 'true', os: ubuntu-22.04, }, + { pkgs: 'gcc-9 g++-9 lib32gcc-9-dev libx32gcc-9-dev', cc: gcc-9, cxx: g++-9, x32: 'true', x86: 'true', cxxtest: 'true', freestanding: 'true', os: ubuntu-22.04, }, { pkgs: 'gcc-8 g++-8 lib32gcc-8-dev libx32gcc-8-dev', cc: gcc-8, cxx: g++-8, x32: 'true', x86: 'true', cxxtest: 'true', freestanding: 'true', os: ubuntu-20.04, }, { pkgs: 'gcc-7 g++-7 lib32gcc-7-dev libx32gcc-7-dev', cc: gcc-7, cxx: g++-7, x32: 'true', x86: 'true', cxxtest: 'true', freestanding: 'true', os: ubuntu-20.04, }, { pkgs: 'gcc-6 g++-6 lib32gcc-6-dev libx32gcc-6-dev', cc: gcc-6, cxx: g++-6, x32: 'true', x86: 'true', cxxtest: 'true', freestanding: 'true', os: ubuntu-18.04, }, @@ -53,8 +54,10 @@ jobs: # clang { pkgs: 'lib32gcc-11-dev libx32gcc-11-dev', cc: clang, cxx: clang++, x32: 'true', x86: 'true', cxxtest: 'true', freestanding: 'false', os: ubuntu-latest, }, - { pkgs: 'clang-12 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-12, cxx: clang++-12, x32: 'true', x86: 'true', cxxtest: 'true', freestanding: 'false', os: ubuntu-20.04, }, - { pkgs: 'clang-11 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-11, cxx: clang++-11, x32: 'true', x86: 'true', cxxtest: 'true', freestanding: 'false', os: ubuntu-20.04, }, + { pkgs: 'clang-14 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-14, cxx: clang++-14, x32: 'true', x86: 'true', cxxtest: 'true', freestanding: 'false', os: ubuntu-22.04, }, + { pkgs: 'clang-13 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-13, cxx: clang++-13, x32: 'true', x86: 'true', cxxtest: 'true', freestanding: 'false', os: ubuntu-22.04, }, + { pkgs: 'clang-12 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-12, cxx: clang++-12, x32: 'true', x86: 'true', cxxtest: 'true', freestanding: 'false', os: ubuntu-22.04, }, + { pkgs: 'clang-11 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-11, cxx: clang++-11, x32: 'true', x86: 'true', cxxtest: 'true', freestanding: 'false', os: ubuntu-22.04, }, { pkgs: 'clang-10 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-10, cxx: clang++-10, x32: 'true', x86: 'true', cxxtest: 'true', freestanding: 'false', os: ubuntu-20.04, }, { pkgs: 'clang-9 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-9, cxx: clang++-9, x32: 'true', x86: 'true', cxxtest: 'true', freestanding: 'false', os: ubuntu-20.04, }, { pkgs: 'clang-8 lib32gcc-11-dev libx32gcc-11-dev', cc: clang-8, cxx: clang++-8, x32: 'true', x86: 'true', cxxtest: 'true', freestanding: 'false', os: ubuntu-20.04, }, -- cgit v0.12 From b8c4120aa1d335c01877a81a9bf741d18bb72172 Mon Sep 17 00:00:00 2001 From: Takayuki Matsuoka Date: Fri, 12 Aug 2022 23:28:05 +0900 Subject: Clone MSVC project (from VS2017 to VS2022) --- build/VS2022/datagen/datagen.vcxproj | 173 +++++++++++++++++++++ build/VS2022/frametest/frametest.vcxproj | 180 ++++++++++++++++++++++ build/VS2022/fullbench-dll/fullbench-dll.vcxproj | 184 +++++++++++++++++++++++ build/VS2022/fullbench/fullbench.vcxproj | 180 ++++++++++++++++++++++ build/VS2022/fuzzer/fuzzer.vcxproj | 177 ++++++++++++++++++++++ build/VS2022/liblz4-dll/liblz4-dll.rc | 51 +++++++ build/VS2022/liblz4-dll/liblz4-dll.vcxproj | 183 ++++++++++++++++++++++ build/VS2022/liblz4/liblz4.vcxproj | 179 ++++++++++++++++++++++ build/VS2022/lz4.sln | 103 +++++++++++++ 9 files changed, 1410 insertions(+) create mode 100644 build/VS2022/datagen/datagen.vcxproj create mode 100644 build/VS2022/frametest/frametest.vcxproj create mode 100644 build/VS2022/fullbench-dll/fullbench-dll.vcxproj create mode 100644 build/VS2022/fullbench/fullbench.vcxproj create mode 100644 build/VS2022/fuzzer/fuzzer.vcxproj create mode 100644 build/VS2022/liblz4-dll/liblz4-dll.rc create mode 100644 build/VS2022/liblz4-dll/liblz4-dll.vcxproj create mode 100644 build/VS2022/liblz4/liblz4.vcxproj create mode 100644 build/VS2022/lz4.sln diff --git a/build/VS2022/datagen/datagen.vcxproj b/build/VS2022/datagen/datagen.vcxproj new file mode 100644 index 0000000..30e159e --- /dev/null +++ b/build/VS2022/datagen/datagen.vcxproj @@ -0,0 +1,173 @@ + + + \ No newline at end of file diff --git a/build/VS2022/frametest/frametest.vcxproj b/build/VS2022/frametest/frametest.vcxproj new file mode 100644 index 0000000..a3a403d --- /dev/null +++ b/build/VS2022/frametest/frametest.vcxproj @@ -0,0 +1,180 @@ + ++ ++ +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/VS2022/fullbench-dll/fullbench-dll.vcxproj b/build/VS2022/fullbench-dll/fullbench-dll.vcxproj new file mode 100644 index 0000000..d54a8d7 --- /dev/null +++ b/build/VS2022/fullbench-dll/fullbench-dll.vcxproj @@ -0,0 +1,184 @@ + ++ ++ +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/VS2022/fullbench/fullbench.vcxproj b/build/VS2022/fullbench/fullbench.vcxproj new file mode 100644 index 0000000..54c9743 --- /dev/null +++ b/build/VS2022/fullbench/fullbench.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 +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/VS2022/fuzzer/fuzzer.vcxproj b/build/VS2022/fuzzer/fuzzer.vcxproj new file mode 100644 index 0000000..aa6fe42 --- /dev/null +++ b/build/VS2022/fuzzer/fuzzer.vcxproj @@ -0,0 +1,177 @@ + ++ ++ +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/VS2022/liblz4-dll/liblz4-dll.rc b/build/VS2022/liblz4-dll/liblz4-dll.rc new file mode 100644 index 0000000..e089c24 --- /dev/null +++ b/build/VS2022/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-2020, 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/VS2022/liblz4-dll/liblz4-dll.vcxproj b/build/VS2022/liblz4-dll/liblz4-dll.vcxproj new file mode 100644 index 0000000..8e7ee3b --- /dev/null +++ b/build/VS2022/liblz4-dll/liblz4-dll.vcxproj @@ -0,0 +1,183 @@ + ++ ++ +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/VS2022/liblz4/liblz4.vcxproj b/build/VS2022/liblz4/liblz4.vcxproj new file mode 100644 index 0000000..948f7db --- /dev/null +++ b/build/VS2022/liblz4/liblz4.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 +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/VS2022/lz4.sln b/build/VS2022/lz4.sln new file mode 100644 index 0000000..6a2779f --- /dev/null +++ b/build/VS2022/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 -- cgit v0.12 From 5e22228e88340ec05cd9b3e275ef7a81af2709e8 Mon Sep 17 00:00:00 2001 From: Takayuki Matsuoka+ ++ +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 + ++ ++ + + + + + ++ + + + + + +Date: Fri, 12 Aug 2022 23:29:14 +0900 Subject: Update MSVC 2022 project's PlatformToolset to v143 --- build/VS2022/datagen/datagen.vcxproj | 8 ++++---- build/VS2022/frametest/frametest.vcxproj | 8 ++++---- build/VS2022/fullbench-dll/fullbench-dll.vcxproj | 8 ++++---- build/VS2022/fullbench/fullbench.vcxproj | 8 ++++---- build/VS2022/fuzzer/fuzzer.vcxproj | 8 ++++---- build/VS2022/liblz4-dll/liblz4-dll.vcxproj | 8 ++++---- build/VS2022/liblz4/liblz4.vcxproj | 8 ++++---- 7 files changed, 28 insertions(+), 28 deletions(-) diff --git a/build/VS2022/datagen/datagen.vcxproj b/build/VS2022/datagen/datagen.vcxproj index 30e159e..69034d4 100644 --- a/build/VS2022/datagen/datagen.vcxproj +++ b/build/VS2022/datagen/datagen.vcxproj @@ -30,27 +30,27 @@ Application true Unicode -v141 +v143 Application true Unicode -v141 +v143 Application false Unicode true -v141 +v143 Application false Unicode true -v141 +v143 diff --git a/build/VS2022/frametest/frametest.vcxproj b/build/VS2022/frametest/frametest.vcxproj index a3a403d..6b7ff75 100644 --- a/build/VS2022/frametest/frametest.vcxproj +++ b/build/VS2022/frametest/frametest.vcxproj @@ -30,27 +30,27 @@ Application true Unicode -v141 +v143 Application true Unicode -v141 +v143 Application false Unicode true -v141 +v143 Application false Unicode true -v141 +v143 diff --git a/build/VS2022/fullbench-dll/fullbench-dll.vcxproj b/build/VS2022/fullbench-dll/fullbench-dll.vcxproj index d54a8d7..143dc06 100644 --- a/build/VS2022/fullbench-dll/fullbench-dll.vcxproj +++ b/build/VS2022/fullbench-dll/fullbench-dll.vcxproj @@ -30,27 +30,27 @@ Application true Unicode -v141 +v143 Application true Unicode -v141 +v143 Application false Unicode true -v141 +v143 Application false Unicode true -v141 +v143 diff --git a/build/VS2022/fullbench/fullbench.vcxproj b/build/VS2022/fullbench/fullbench.vcxproj index 54c9743..57f4b5a 100644 --- a/build/VS2022/fullbench/fullbench.vcxproj +++ b/build/VS2022/fullbench/fullbench.vcxproj @@ -30,27 +30,27 @@ Application true Unicode -v141 +v143 Application true Unicode -v141 +v143 Application false Unicode true -v141 +v143 Application false Unicode true -v141 +v143 diff --git a/build/VS2022/fuzzer/fuzzer.vcxproj b/build/VS2022/fuzzer/fuzzer.vcxproj index aa6fe42..83482c2 100644 --- a/build/VS2022/fuzzer/fuzzer.vcxproj +++ b/build/VS2022/fuzzer/fuzzer.vcxproj @@ -30,27 +30,27 @@ Application true Unicode -v141 +v143 Application true Unicode -v141 +v143 Application false Unicode true -v141 +v143 Application false Unicode true -v141 +v143 diff --git a/build/VS2022/liblz4-dll/liblz4-dll.vcxproj b/build/VS2022/liblz4-dll/liblz4-dll.vcxproj index 8e7ee3b..532ac75 100644 --- a/build/VS2022/liblz4-dll/liblz4-dll.vcxproj +++ b/build/VS2022/liblz4-dll/liblz4-dll.vcxproj @@ -31,27 +31,27 @@ DynamicLibrary true Unicode -v141 +v143 DynamicLibrary true Unicode -v141 +v143 DynamicLibrary false Unicode true -v141 +v143 DynamicLibrary false Unicode true -v141 +v143 diff --git a/build/VS2022/liblz4/liblz4.vcxproj b/build/VS2022/liblz4/liblz4.vcxproj index 948f7db..fdddaaa 100644 --- a/build/VS2022/liblz4/liblz4.vcxproj +++ b/build/VS2022/liblz4/liblz4.vcxproj @@ -30,27 +30,27 @@ StaticLibrary true Unicode -v141 +v143 StaticLibrary true Unicode -v141 +v143 StaticLibrary false Unicode true -v141 +v143 StaticLibrary false Unicode true -v141 +v143 -- cgit v0.12 From 0fc36f1bc758e3d66cbbf863a5701d0ec006cd1a Mon Sep 17 00:00:00 2001 From: Takayuki Matsuoka Date: Fri, 12 Aug 2022 23:48:43 +0900 Subject: Suppress false positive warning from MSVC MSVC (17.3 or earlier) reports the following warning lz4\lib\lz4.c(527): warning C6385: Reading invalid data from 'v'. Line 527 is : LZ4_memcpy(&v[4], v, 4); But, obviously v[0..3] is always filled with meaningful value. Therefore, this warning report is wrong. We must revisit this issue with future version of MSVC. --- lib/lz4.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/lz4.c b/lib/lz4.c index 2a31735..654bfdf 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -524,7 +524,14 @@ 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); +#if defined(_MSC_VER) && (_MSC_VER <= 1933) /* MSVC 2022 ver 17.3 or earlier */ +# pragma warning(push) +# pragma warning(disable : 6385) /* warning C6385: Reading invalid data from 'v'. */ +#endif LZ4_memcpy(&v[4], v, 4); +#if defined(_MSC_VER) && (_MSC_VER <= 1933) /* MSVC 2022 ver 17.3 or earlier */ +# pragma warning(pop) +#endif break; case 4: LZ4_memcpy(v, srcPtr, 4); -- cgit v0.12 From af0d7c0cb811e83a89d0ded3ba3bdea1895a2f8a Mon Sep 17 00:00:00 2001 From: Takayuki Matsuoka Date: Fri, 12 Aug 2022 23:59:34 +0900 Subject: Suppress warning from rc.exe Since rc.exe (the resource compiler) is legacy compiler, it truncates preprocessor symbol name length to 32 chars. And it reports the following warning lz4\build\VS2022\..\..\lib\lz4.h(314): warning RC4011: identifier truncated to 'LZ4_STATIC_LINKING_ONLY_DISABLE' lz4\build\VS2022\..\..\lib\lz4.h(401): warning RC4011: identifier truncated to 'LZ4_STATIC_LINKING_ONLY_DISABLE' This patch detects rc.exe and just skips long symbol. --- lib/lz4.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/lz4.h b/lib/lz4.h index c9a59e3..383d966 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -311,10 +311,12 @@ 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) */ +#if !defined(RC_INVOKED) /* https://docs.microsoft.com/en-us/windows/win32/menurc/predefined-macros */ #if !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION) LZ4LIB_API LZ4_stream_t* LZ4_createStream(void); LZ4LIB_API int LZ4_freeStream (LZ4_stream_t* streamPtr); #endif /* !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION) */ +#endif /*! LZ4_resetStream_fast() : v1.9.0+ * Use this to prepare an LZ4_stream_t for a new chain of dependent blocks @@ -398,10 +400,12 @@ typedef union LZ4_streamDecode_u LZ4_streamDecode_t; /* tracking context */ * creation / destruction of streaming decompression tracking context. * A tracking context can be re-used multiple times. */ +#if !defined(RC_INVOKED) /* https://docs.microsoft.com/en-us/windows/win32/menurc/predefined-macros */ #if !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION) LZ4LIB_API LZ4_streamDecode_t* LZ4_createStreamDecode(void); LZ4LIB_API int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream); #endif /* !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION) */ +#endif /*! LZ4_setStreamDecode() : * An LZ4_streamDecode_t context can be allocated once and re-used multiple times. -- cgit v0.12 From f892f828838c064dab8d754388506c94e37c3fe1 Mon Sep 17 00:00:00 2001 From: Takayuki Matsuoka Date: Sat, 13 Aug 2022 00:25:13 +0900 Subject: Suppress false positive warning from MSVC (datagencli.c) MSVC 2022 reports the follwing false positve warnings: lz4\tests\datagencli.c(110): warning C26451: Arithmetic overflow: Using operator '-' on a 4 byte value and then casting the result to a 8 byte value. Cast the value to the wider type before calling operator '-' to avoid overflow (io.2). lz4\tests\datagencli.c(134): warning C26451: Arithmetic overflow: Using operator '-' on a 4 byte value and then casting the result to a 8 byte value. Cast the value to the wider type before calling operator '-' to avoid overflow (io.2). lz4\tests\datagencli.c(146): warning C26451: Arithmetic overflow: Using operator '-' on a 4 byte value and then casting the result to a 8 byte value. Cast the value to the wider type before calling operator '-' to avoid overflow (io.2). Although they're absolutely compiler's and static analyzer's bug, it'd always be nice to use the standard library. --- tests/datagencli.c | 32 ++++---------------------------- 1 file changed, 4 insertions(+), 28 deletions(-) diff --git a/tests/datagencli.c b/tests/datagencli.c index 9efe27e..1a99190 100644 --- a/tests/datagencli.c +++ b/tests/datagencli.c @@ -104,12 +104,7 @@ int main(int argc, char** argv) case 'g': argument++; size=0; - while ((*argument>='0') && (*argument<='9')) - { - size *= 10; - size += *argument - '0'; - argument++; - } + size = strtoull(argument, &argument, 10); if (*argument=='K') { size <<= 10; argument++; } if (*argument=='M') { size <<= 20; argument++; } if (*argument=='G') { size <<= 30; argument++; } @@ -117,35 +112,16 @@ int main(int argc, char** argv) break; case 's': argument++; - seed=0; - while ((*argument>='0') && (*argument<='9')) - { - seed *= 10; - seed += *argument - '0'; - argument++; - } + seed = (U32) strtoul(argument, &argument, 10); break; case 'P': argument++; - proba=0.0; - while ((*argument>='0') && (*argument<='9')) - { - proba *= 10; - proba += *argument - '0'; - argument++; - } - if (proba>100.) proba=100.; + proba = (double) strtoull(argument, &argument, 10); proba /= 100.; break; case 'L': /* hidden argument : Literal distribution probability */ argument++; - litProba=0.; - while ((*argument>='0') && (*argument<='9')) - { - litProba *= 10; - litProba += *argument - '0'; - argument++; - } + litProba = (double) strtoull(argument, &argument, 10); if (litProba>100.) litProba=100.; litProba /= 100.; break; -- cgit v0.12 From 46d12d661edda40bedc0ce38247e170b13575aaf Mon Sep 17 00:00:00 2001 From: Takayuki Matsuoka Date: Sat, 13 Aug 2022 00:36:28 +0900 Subject: Suppress false positive warning from MSVC (fuzzer.c) Suppress the following false positive warnings from MSVC: - Disable all arithmetic overflow (C26451) - Suppress C6385: Reading invalid data from 'compressedBuffer'. - Add ULL suffix to unsigned 64-bits constants. --- tests/fuzzer.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/fuzzer.c b/tests/fuzzer.c index 07d63a2..341a2b0 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -30,6 +30,7 @@ # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ # pragma warning(disable : 4146) /* disable: C4146: minus unsigned expression */ # pragma warning(disable : 4310) /* disable: C4310: constant char value > 127 */ +# pragma warning(disable : 26451) /* disable: C26451: Arithmetic overflow */ #endif @@ -494,7 +495,14 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c { char* const cBuffer_exact = (char*)malloc((size_t)compressedSize); assert(cBuffer_exact != NULL); assert(compressedSize <= (int)compressedBufferSize); +#if defined(_MSC_VER) && (_MSC_VER <= 1933) /* MSVC 2022 ver 17.3 or earlier */ +# pragma warning(push) +# pragma warning(disable : 6385) /* lz4\tests\fuzzer.c(497): warning C6385: Reading invalid data from 'compressedBuffer'. */ +#endif memcpy(cBuffer_exact, compressedBuffer, compressedSize); +#if defined(_MSC_VER) && (_MSC_VER <= 1933) /* MSVC 2022 ver 17.3 or earlier */ +# pragma warning(pop) +#endif /* Test decoding with output size exactly correct => must work */ FUZ_DISPLAYTEST("LZ4_decompress_fast() with exact output buffer"); @@ -571,7 +579,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c for (;;) { /* keep some original src */ { U32 const nbBits = FUZ_rand(&randState) % maxNbBits; - size_t const mask = (1< Date: Sat, 13 Aug 2022 00:46:44 +0900 Subject: Fix: remove unused value This patch fixes the following error from "make staticAnalyze" datagencli.c:106:21: warning: Value stored to 'size' is never read size=0; ^ ~ --- tests/datagencli.c | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/datagencli.c b/tests/datagencli.c index 1a99190..2976b53 100644 --- a/tests/datagencli.c +++ b/tests/datagencli.c @@ -103,7 +103,6 @@ int main(int argc, char** argv) return usage(programName); case 'g': argument++; - size=0; size = strtoull(argument, &argument, 10); if (*argument=='K') { size <<= 10; argument++; } if (*argument=='M') { size <<= 20; argument++; } -- cgit v0.12 -- cgit v0.12 From ff4b136ab87a525ce814d350b66677bfe3eabe9b Mon Sep 17 00:00:00 2001 From: Takayuki Matsuoka Date: Sat, 13 Aug 2022 03:59:49 +0900 Subject: Fix: replace strtoull with _strtoui64 for MSVC2010 https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/strtoui64-wcstoui64-strtoui64-l-wcstoui64-l --- tests/datagencli.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/datagencli.c b/tests/datagencli.c index 2976b53..ccb27df 100644 --- a/tests/datagencli.c +++ b/tests/datagencli.c @@ -34,6 +34,14 @@ /************************************** +* Compiler specific +**************************************/ +#ifdef _MSC_VER /* Visual Studio */ +#define strtoull _strtoui64 /* https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/strtoui64-wcstoui64-strtoui64-l-wcstoui64-l */ +#endif + + +/************************************** * Constants **************************************/ #define KB *(1 <<10) -- cgit v0.12 From ae179a9c16aba4dc3b6adefd440c9b77bda1a785 Mon Sep 17 00:00:00 2001 From: Takayuki Matsuoka Date: Sat, 13 Aug 2022 05:39:25 +0900 Subject: Add note about RC_INVOKED --- lib/lz4.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/lz4.h b/lib/lz4.h index 383d966..491c608 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -311,6 +311,19 @@ 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) */ +/** + Note about RC_INVOKED + + - RC_INVOKED is predefined symbol of rc.exe (the resource compiler which is part of MSVC/Visual Studio). + https://docs.microsoft.com/en-us/windows/win32/menurc/predefined-macros + + - Since rc.exe is a legacy compiler, it truncates long symbol (> 30 chars) + and reports warning "RC4011: identifier truncated". + + - To eliminate the warning, we surround long preprocessor symbol with + "#if !defined(RC_INVOKED) ... #endif" block that means + "skip this block when rc.exe is trying to read it". +*/ #if !defined(RC_INVOKED) /* https://docs.microsoft.com/en-us/windows/win32/menurc/predefined-macros */ #if !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION) LZ4LIB_API LZ4_stream_t* LZ4_createStream(void); -- cgit v0.12 From cfd6ab32522280079c2e6d3ea995f172b9ae0312 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 15 Aug 2022 14:24:49 -0700 Subject: update NEWS for v1.9.4 --- NEWS | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS b/NEWS index c2cef0e..0a56992 100644 --- a/NEWS +++ b/NEWS @@ -1,5 +1,6 @@ v1.9.4 perf : faster decoding speed (~+20%) on aarch64 platforms +perf : faster decoding speed (~+70%) for -BD4 setting in CLI api : new function `LZ4_decompress_safe_partial_usingDict()` by @yawqi api : lz4frame: ability to provide custom allocators at state creation api : can skip checksum validation for improved decoding speed @@ -14,6 +15,7 @@ build: `LZ4_FREESTANDING`, new build macro for freestanding environments, by @t- build: `make` and `make test` are compatible with `-j` parallel run build: AS/400 compatibility, by @jonrumsey build: Solaris 10 compatibility, by @pekdon +build: MSVC 2022 support, by @t-mat build: improved meson script, by @eli-schwartz doc : Updated LZ4 block format, provide an "implementation notes" section -- cgit v0.12