Skip to content

Commit 3459e6a

Browse files
committed
Upgrade lz4 to 1.9.4 and ensure that we have the checksum flags turned on.
1 parent 8c82d23 commit 3459e6a

File tree

14 files changed

+1695
-675
lines changed

14 files changed

+1695
-675
lines changed

typed_python/DeserializationBuffer.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ bool DeserializationBuffer::decompress() {
5050
size_t bytesWritten = 1024 * 1024;
5151
size_t bytesRead = bytesToDecompress - bytesDecompressed;
5252

53-
LZ4F_decompress(
53+
size_t res = LZ4F_decompress(
5454
compressionContext,
5555
compressionBuffer,
5656
&bytesWritten,
@@ -59,6 +59,13 @@ bool DeserializationBuffer::decompress() {
5959
nullptr
6060
);
6161

62+
if (LZ4F_isError(res)) {
63+
throw std::runtime_error(
64+
std::string("Error decompressing data using LZ4: ")
65+
+ LZ4F_getErrorName(res)
66+
);
67+
}
68+
6269
if (m_read_head_offset) {
6370
m_decompressed_buffer.erase(
6471
m_decompressed_buffer.begin(),

typed_python/SerializationBuffer.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,15 @@ void SerializationBuffer::compress() {
2929
return;
3030
}
3131

32+
LZ4F_preferences_t lz4Prefs;
33+
memset(&lz4Prefs, 0, sizeof(lz4Prefs));
34+
35+
lz4Prefs.frameInfo.contentChecksumFlag = LZ4F_contentChecksumEnabled;
36+
lz4Prefs.frameInfo.blockChecksumFlag = LZ4F_blockChecksumEnabled;
37+
3238
//replace the data we have here with a block of 4 bytes of size of compressed data and
3339
//then the data stream
34-
size_t bytesRequired = LZ4F_compressFrameBound(m_size - m_last_compression_point, nullptr);
40+
size_t bytesRequired = LZ4F_compressFrameBound(m_size - m_last_compression_point, &lz4Prefs);
3541

3642
void* compressedBytes = malloc(bytesRequired);
3743

@@ -45,8 +51,15 @@ void SerializationBuffer::compress() {
4551
bytesRequired,
4652
m_buffer + m_last_compression_point,
4753
m_size - m_last_compression_point,
48-
nullptr
54+
&lz4Prefs
4955
);
56+
57+
if (LZ4F_isError(compressedBytecount)) {
58+
throw std::runtime_error(
59+
std::string("Error compressing data using LZ4: ")
60+
+ LZ4F_getErrorName(compressedBytecount)
61+
);
62+
}
5063
}
5164

5265
m_size = m_last_compression_point;

typed_python/lz4/Makefile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,18 +175,19 @@ liblz4.pc: liblz4.pc.in Makefile
175175
-e 's|@LIBDIR@|$(libdir)|' \
176176
-e 's|@INCLUDEDIR@|$(includedir)|' \
177177
-e 's|@VERSION@|$(LIBVER)|' \
178+
-e 's|=${prefix}/|=$${prefix}/|' \
178179
$< >$@
179180

180181
install: lib liblz4.pc
181182
$(INSTALL_DIR) $(DESTDIR)$(pkgconfigdir)/ $(DESTDIR)$(includedir)/ $(DESTDIR)$(libdir)/ $(DESTDIR)$(bindir)/
182183
$(INSTALL_DATA) liblz4.pc $(DESTDIR)$(pkgconfigdir)/
183-
@echo Installing libraries
184+
@echo Installing libraries in $(DESTDIR)$(libdir)
184185
ifeq ($(BUILD_STATIC),yes)
185186
$(INSTALL_DATA) liblz4.a $(DESTDIR)$(libdir)/liblz4.a
186187
$(INSTALL_DATA) lz4frame_static.h $(DESTDIR)$(includedir)/lz4frame_static.h
187188
endif
188189
ifeq ($(BUILD_SHARED),yes)
189-
# Traditionnally, one installs the DLLs in the bin directory as programs
190+
# Traditionally, one installs the DLLs in the bin directory as programs
190191
# search them first in their directory. This allows to not pollute system
191192
# directories (like c:/windows/system32), nor modify the PATH variable.
192193
ifeq ($(WINBASED),yes)
@@ -198,7 +199,7 @@ install: lib liblz4.pc
198199
$(LN_SF) liblz4.$(SHARED_EXT_VER) $(DESTDIR)$(libdir)/liblz4.$(SHARED_EXT)
199200
endif
200201
endif
201-
@echo Installing headers in $(includedir)
202+
@echo Installing headers in $(DESTDIR)$(includedir)
202203
$(INSTALL_DATA) lz4.h $(DESTDIR)$(includedir)/lz4.h
203204
$(INSTALL_DATA) lz4hc.h $(DESTDIR)$(includedir)/lz4hc.h
204205
$(INSTALL_DATA) lz4frame.h $(DESTDIR)$(includedir)/lz4frame.h

typed_python/lz4/README.md

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@ LZ4 - Library Files
22
================================
33

44
The `/lib` directory contains many files, but depending on project's objectives,
5-
not all of them are necessary.
5+
not all of them are required.
6+
Limited systems may want to reduce the nb of source files to include
7+
as a way to reduce binary size and dependencies.
68

7-
#### Minimal LZ4 build
9+
Capabilities are added at the "level" granularity, detailed below.
10+
11+
#### Level 1 : Minimal LZ4 build
812

913
The minimum required is **`lz4.c`** and **`lz4.h`**,
1014
which provides the fast compression and decompression algorithms.
1115
They generate and decode data using the [LZ4 block format].
1216

1317

14-
#### High Compression variant
18+
#### Level 2 : High Compression variant
1519

1620
For more compression ratio at the cost of compression speed,
1721
the High Compression variant called **lz4hc** is available.
@@ -20,22 +24,37 @@ This variant also compresses data using the [LZ4 block format],
2024
and depends on regular `lib/lz4.*` source files.
2125

2226

23-
#### Frame support, for interoperability
27+
#### Level 3 : Frame support, for interoperability
2428

2529
In order to produce compressed data compatible with `lz4` command line utility,
2630
it's necessary to use the [official interoperable frame format].
2731
This format is generated and decoded automatically by the **lz4frame** library.
2832
Its public API is described in `lib/lz4frame.h`.
2933
In order to work properly, lz4frame needs all other modules present in `/lib`,
3034
including, lz4 and lz4hc, and also **xxhash**.
31-
So it's necessary to include all `*.c` and `*.h` files present in `/lib`.
35+
So it's necessary to also include `xxhash.c` and `xxhash.h`.
36+
37+
38+
#### Level 4 : File compression operations
39+
40+
As a helper around file operations,
41+
the library has been recently extended with `lz4file.c` and `lz4file.h`
42+
(still considered experimental at the time of this writing).
43+
These helpers allow opening, reading, writing, and closing files
44+
using transparent LZ4 compression / decompression.
45+
As a consequence, using `lz4file` adds a dependency on `<stdio.h>`.
46+
47+
`lz4file` relies on `lz4frame` in order to produce compressed data
48+
conformant to the [LZ4 Frame format] specification.
49+
Consequently, to enable this capability,
50+
it's necessary to include all `*.c` and `*.h` files from `lib/` directory.
3251

3352

3453
#### Advanced / Experimental API
3554

3655
Definitions which are not guaranteed to remain stable in future versions,
3756
are protected behind macros, such as `LZ4_STATIC_LINKING_ONLY`.
38-
As the name strongly implies, these definitions should only be invoked
57+
As the name suggests, these definitions should only be invoked
3958
in the context of static linking ***only***.
4059
Otherwise, dependent application may fail on API or ABI break in the future.
4160
The associated symbols are also not exposed by the dynamic library by default.
@@ -58,7 +77,7 @@ The following build macro can be selected to adjust source code behavior at comp
5877
Set to 65535 by default, which is the maximum value supported by lz4 format.
5978
Reducing maximum distance will reduce opportunities for LZ4 to find matches,
6079
hence will produce a worse compression ratio.
61-
However, a smaller max distance can allow compatibility with specific decoders using limited memory budget.
80+
Setting a smaller max distance could allow compatibility with specific decoders with limited memory budget.
6281
This build macro only influences the compressed output of the compressor.
6382

6483
- `LZ4_DISABLE_DEPRECATE_WARNINGS` : invoking a deprecated function will make the compiler generate a warning.
@@ -69,22 +88,34 @@ The following build macro can be selected to adjust source code behavior at comp
6988
This build macro offers another project-specific method
7089
by defining `LZ4_DISABLE_DEPRECATE_WARNINGS` before including the LZ4 header files.
7190

72-
- `LZ4_USER_MEMORY_FUNCTIONS` : replace calls to <stdlib>'s `malloc`, `calloc` and `free`
73-
by user-defined functions, which must be called `LZ4_malloc()`, `LZ4_calloc()` and `LZ4_free()`.
74-
User functions must be available at link time.
75-
7691
- `LZ4_FORCE_SW_BITCOUNT` : by default, the compression algorithm tries to determine lengths
7792
by using bitcount instructions, generally implemented as fast single instructions in many cpus.
7893
In case the target cpus doesn't support it, or compiler intrinsic doesn't work, or feature bad performance,
7994
it's possible to use an optimized software path instead.
80-
This is achieved by setting this build macros .
95+
This is achieved by setting this build macros.
8196
In most cases, it's not expected to be necessary,
8297
but it can be legitimately considered for less common platforms.
8398

8499
- `LZ4_ALIGN_TEST` : alignment test ensures that the memory area
85100
passed as argument to become a compression state is suitably aligned.
86101
This test can be disabled if it proves flaky, by setting this value to 0.
87102

103+
- `LZ4_USER_MEMORY_FUNCTIONS` : replace calls to `<stdlib,h>`'s `malloc()`, `calloc()` and `free()`
104+
by user-defined functions, which must be named `LZ4_malloc()`, `LZ4_calloc()` and `LZ4_free()`.
105+
User functions must be available at link time.
106+
107+
- `LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION` :
108+
Remove support of dynamic memory allocation.
109+
For more details, see description of this macro in `lib/lz4.c`.
110+
111+
- `LZ4_FREESTANDING` : by setting this build macro to 1,
112+
LZ4/HC removes dependencies on the C standard library,
113+
including allocation functions and `memmove()`, `memcpy()`, and `memset()`.
114+
This build macro is designed to help use LZ4/HC in restricted environments
115+
(embedded, bootloader, etc).
116+
For more details, see description of this macro in `lib/lz4.h`.
117+
118+
88119

89120
#### Amalgamation
90121

@@ -101,7 +132,7 @@ All `*.h` files present in `/lib` remain necessary to compile `lz4_all.c`.
101132

102133
DLL can be created using MinGW+MSYS with the `make liblz4` command.
103134
This command creates `dll\liblz4.dll` and the import library `dll\liblz4.lib`.
104-
To override the `dlltool` command when cross-compiling on Linux, just set the `DLLTOOL` variable. Example of cross compilation on Linux with mingw-w64 64 bits:
135+
To override the `dlltool` command when cross-compiling on Linux, just set the `DLLTOOL` variable. Example of cross compilation on Linux with mingw-w64 64 bits:
105136
```
106137
make BUILD_STATIC=no CC=x86_64-w64-mingw32-gcc DLLTOOL=x86_64-w64-mingw32-dlltool OS=Windows_NT
107138
```
@@ -127,6 +158,7 @@ Other files present in the directory are not source code. They are :
127158
- `README.md` : this file
128159

129160
[official interoperable frame format]: ../doc/lz4_Frame_format.md
161+
[LZ4 Frame format]: ../doc/lz4_Frame_format.md
130162
[LZ4 block format]: ../doc/lz4_Block_format.md
131163

132164

typed_python/lz4/liblz4.pc.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ Name: lz4
1010
Description: extremely fast lossless compression algorithm library
1111
URL: http://www.lz4.org/
1212
Version: @VERSION@
13-
Libs: -L@LIBDIR@ -llz4
14-
Cflags: -I@INCLUDEDIR@
13+
Libs: -L${libdir} -llz4
14+
Cflags: -I${includedir}

0 commit comments

Comments
 (0)