1*27162e4eSAndroid Build Coastguard WorkerLZ4 - Library Files 2*27162e4eSAndroid Build Coastguard Worker================================ 3*27162e4eSAndroid Build Coastguard Worker 4*27162e4eSAndroid Build Coastguard WorkerThe `/lib` directory contains many files, but depending on project's objectives, 5*27162e4eSAndroid Build Coastguard Workernot all of them are required. 6*27162e4eSAndroid Build Coastguard WorkerLimited systems may want to reduce the nb of source files to include 7*27162e4eSAndroid Build Coastguard Workeras a way to reduce binary size and dependencies. 8*27162e4eSAndroid Build Coastguard Worker 9*27162e4eSAndroid Build Coastguard WorkerCapabilities are added at the "level" granularity, detailed below. 10*27162e4eSAndroid Build Coastguard Worker 11*27162e4eSAndroid Build Coastguard Worker#### Level 1 : Minimal LZ4 build 12*27162e4eSAndroid Build Coastguard Worker 13*27162e4eSAndroid Build Coastguard WorkerThe minimum required is **`lz4.c`** and **`lz4.h`**, 14*27162e4eSAndroid Build Coastguard Workerwhich provides the fast compression and decompression algorithms. 15*27162e4eSAndroid Build Coastguard WorkerThey generate and decode data using the [LZ4 block format]. 16*27162e4eSAndroid Build Coastguard Worker 17*27162e4eSAndroid Build Coastguard Worker 18*27162e4eSAndroid Build Coastguard Worker#### Level 2 : High Compression variant 19*27162e4eSAndroid Build Coastguard Worker 20*27162e4eSAndroid Build Coastguard WorkerFor more compression ratio at the cost of compression speed, 21*27162e4eSAndroid Build Coastguard Workerthe High Compression variant called **lz4hc** is available. 22*27162e4eSAndroid Build Coastguard WorkerAdd files **`lz4hc.c`** and **`lz4hc.h`**. 23*27162e4eSAndroid Build Coastguard WorkerThis variant also compresses data using the [LZ4 block format], 24*27162e4eSAndroid Build Coastguard Workerand depends on regular `lib/lz4.*` source files. 25*27162e4eSAndroid Build Coastguard Worker 26*27162e4eSAndroid Build Coastguard Worker 27*27162e4eSAndroid Build Coastguard Worker#### Level 3 : Frame support, for interoperability 28*27162e4eSAndroid Build Coastguard Worker 29*27162e4eSAndroid Build Coastguard WorkerIn order to produce compressed data compatible with `lz4` command line utility, 30*27162e4eSAndroid Build Coastguard Workerit's necessary to use the [official interoperable frame format]. 31*27162e4eSAndroid Build Coastguard WorkerThis format is generated and decoded automatically by the **lz4frame** library. 32*27162e4eSAndroid Build Coastguard WorkerIts public API is described in `lib/lz4frame.h`. 33*27162e4eSAndroid Build Coastguard WorkerIn order to work properly, lz4frame needs all other modules present in `/lib`, 34*27162e4eSAndroid Build Coastguard Workerincluding, lz4 and lz4hc, and also **xxhash**. 35*27162e4eSAndroid Build Coastguard WorkerSo it's necessary to also include `xxhash.c` and `xxhash.h`. 36*27162e4eSAndroid Build Coastguard Worker 37*27162e4eSAndroid Build Coastguard Worker 38*27162e4eSAndroid Build Coastguard Worker#### Level 4 : File compression operations 39*27162e4eSAndroid Build Coastguard Worker 40*27162e4eSAndroid Build Coastguard WorkerAs a helper around file operations, 41*27162e4eSAndroid Build Coastguard Workerthe library has been recently extended with `lz4file.c` and `lz4file.h` 42*27162e4eSAndroid Build Coastguard Worker(still considered experimental at the time of this writing). 43*27162e4eSAndroid Build Coastguard WorkerThese helpers allow opening, reading, writing, and closing files 44*27162e4eSAndroid Build Coastguard Workerusing transparent LZ4 compression / decompression. 45*27162e4eSAndroid Build Coastguard WorkerAs a consequence, using `lz4file` adds a dependency on `<stdio.h>`. 46*27162e4eSAndroid Build Coastguard Worker 47*27162e4eSAndroid Build Coastguard Worker`lz4file` relies on `lz4frame` in order to produce compressed data 48*27162e4eSAndroid Build Coastguard Workerconformant to the [LZ4 Frame format] specification. 49*27162e4eSAndroid Build Coastguard WorkerConsequently, to enable this capability, 50*27162e4eSAndroid Build Coastguard Workerit's necessary to include all `*.c` and `*.h` files from `lib/` directory. 51*27162e4eSAndroid Build Coastguard Worker 52*27162e4eSAndroid Build Coastguard Worker 53*27162e4eSAndroid Build Coastguard Worker#### Advanced / Experimental API 54*27162e4eSAndroid Build Coastguard Worker 55*27162e4eSAndroid Build Coastguard WorkerDefinitions which are not guaranteed to remain stable in future versions, 56*27162e4eSAndroid Build Coastguard Workerare protected behind macros, such as `LZ4_STATIC_LINKING_ONLY`. 57*27162e4eSAndroid Build Coastguard WorkerAs the name suggests, these definitions should only be invoked 58*27162e4eSAndroid Build Coastguard Workerin the context of static linking ***only***. 59*27162e4eSAndroid Build Coastguard WorkerOtherwise, dependent application may fail on API or ABI break in the future. 60*27162e4eSAndroid Build Coastguard WorkerThe associated symbols are also not exposed by the dynamic library by default. 61*27162e4eSAndroid Build Coastguard WorkerShould they be nonetheless needed, it's possible to force their publication 62*27162e4eSAndroid Build Coastguard Workerby using build macros `LZ4_PUBLISH_STATIC_FUNCTIONS` 63*27162e4eSAndroid Build Coastguard Workerand `LZ4F_PUBLISH_STATIC_FUNCTIONS`. 64*27162e4eSAndroid Build Coastguard Worker 65*27162e4eSAndroid Build Coastguard Worker 66*27162e4eSAndroid Build Coastguard Worker#### Build macros 67*27162e4eSAndroid Build Coastguard Worker 68*27162e4eSAndroid Build Coastguard WorkerThe following build macro can be selected to adjust source code behavior at compilation time : 69*27162e4eSAndroid Build Coastguard Worker 70*27162e4eSAndroid Build Coastguard Worker- `LZ4_FAST_DEC_LOOP` : this triggers a speed optimized decompression loop, more powerful on modern cpus. 71*27162e4eSAndroid Build Coastguard Worker This loop works great on `x86`, `x64` and `aarch64` cpus, and is automatically enabled for them. 72*27162e4eSAndroid Build Coastguard Worker It's also possible to enable or disable it manually, by passing `LZ4_FAST_DEC_LOOP=1` or `0` to the preprocessor. 73*27162e4eSAndroid Build Coastguard Worker For example, with `gcc` : `-DLZ4_FAST_DEC_LOOP=1`, 74*27162e4eSAndroid Build Coastguard Worker and with `make` : `CPPFLAGS+=-DLZ4_FAST_DEC_LOOP=1 make lz4`. 75*27162e4eSAndroid Build Coastguard Worker 76*27162e4eSAndroid Build Coastguard Worker- `LZ4_DISTANCE_MAX` : control the maximum offset that the compressor will allow. 77*27162e4eSAndroid Build Coastguard Worker Set to 65535 by default, which is the maximum value supported by lz4 format. 78*27162e4eSAndroid Build Coastguard Worker Reducing maximum distance will reduce opportunities for LZ4 to find matches, 79*27162e4eSAndroid Build Coastguard Worker hence will produce a worse compression ratio. 80*27162e4eSAndroid Build Coastguard Worker Setting a smaller max distance could allow compatibility with specific decoders with limited memory budget. 81*27162e4eSAndroid Build Coastguard Worker This build macro only influences the compressed output of the compressor. 82*27162e4eSAndroid Build Coastguard Worker 83*27162e4eSAndroid Build Coastguard Worker- `LZ4_DISABLE_DEPRECATE_WARNINGS` : invoking a deprecated function will make the compiler generate a warning. 84*27162e4eSAndroid Build Coastguard Worker This is meant to invite users to update their source code. 85*27162e4eSAndroid Build Coastguard Worker Should this be a problem, it's generally possible to make the compiler ignore these warnings, 86*27162e4eSAndroid Build Coastguard Worker for example with `-Wno-deprecated-declarations` on `gcc`, 87*27162e4eSAndroid Build Coastguard Worker or `_CRT_SECURE_NO_WARNINGS` for Visual Studio. 88*27162e4eSAndroid Build Coastguard Worker This build macro offers another project-specific method 89*27162e4eSAndroid Build Coastguard Worker by defining `LZ4_DISABLE_DEPRECATE_WARNINGS` before including the LZ4 header files. 90*27162e4eSAndroid Build Coastguard Worker 91*27162e4eSAndroid Build Coastguard Worker- `LZ4_FORCE_SW_BITCOUNT` : by default, the compression algorithm tries to determine lengths 92*27162e4eSAndroid Build Coastguard Worker by using bitcount instructions, generally implemented as fast single instructions in many cpus. 93*27162e4eSAndroid Build Coastguard Worker In case the target cpus doesn't support it, or compiler intrinsic doesn't work, or feature bad performance, 94*27162e4eSAndroid Build Coastguard Worker it's possible to use an optimized software path instead. 95*27162e4eSAndroid Build Coastguard Worker This is achieved by setting this build macros. 96*27162e4eSAndroid Build Coastguard Worker In most cases, it's not expected to be necessary, 97*27162e4eSAndroid Build Coastguard Worker but it can be legitimately considered for less common platforms. 98*27162e4eSAndroid Build Coastguard Worker 99*27162e4eSAndroid Build Coastguard Worker- `LZ4_ALIGN_TEST` : alignment test ensures that the memory area 100*27162e4eSAndroid Build Coastguard Worker passed as argument to become a compression state is suitably aligned. 101*27162e4eSAndroid Build Coastguard Worker This test can be disabled if it proves flaky, by setting this value to 0. 102*27162e4eSAndroid Build Coastguard Worker 103*27162e4eSAndroid Build Coastguard Worker- `LZ4_USER_MEMORY_FUNCTIONS` : replace calls to `<stdlib.h>`'s `malloc()`, `calloc()` and `free()` 104*27162e4eSAndroid Build Coastguard Worker by user-defined functions, which must be named `LZ4_malloc()`, `LZ4_calloc()` and `LZ4_free()`. 105*27162e4eSAndroid Build Coastguard Worker User functions must be available at link time. 106*27162e4eSAndroid Build Coastguard Worker 107*27162e4eSAndroid Build Coastguard Worker- `LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION` : 108*27162e4eSAndroid Build Coastguard Worker Remove support of dynamic memory allocation. 109*27162e4eSAndroid Build Coastguard Worker For more details, see description of this macro in `lib/lz4.c`. 110*27162e4eSAndroid Build Coastguard Worker 111*27162e4eSAndroid Build Coastguard Worker- `LZ4_STATIC_LINKING_ONLY_ENDIANNESS_INDEPENDENT_OUTPUT` : experimental feature aimed at producing the same 112*27162e4eSAndroid Build Coastguard Worker compressed output on platforms of different endianness (i.e. little-endian and big-endian). 113*27162e4eSAndroid Build Coastguard Worker Output on little-endian platforms shall remain unchanged, while big-endian platforms will start producing 114*27162e4eSAndroid Build Coastguard Worker the same output as little-endian ones. This isn't expected to impact backward- and forward-compatibility 115*27162e4eSAndroid Build Coastguard Worker in any way. 116*27162e4eSAndroid Build Coastguard Worker 117*27162e4eSAndroid Build Coastguard Worker- `LZ4_FREESTANDING` : by setting this build macro to 1, 118*27162e4eSAndroid Build Coastguard Worker LZ4/HC removes dependencies on the C standard library, 119*27162e4eSAndroid Build Coastguard Worker including allocation functions and `memmove()`, `memcpy()`, and `memset()`. 120*27162e4eSAndroid Build Coastguard Worker This build macro is designed to help use LZ4/HC in restricted environments 121*27162e4eSAndroid Build Coastguard Worker (embedded, bootloader, etc). 122*27162e4eSAndroid Build Coastguard Worker For more details, see description of this macro in `lib/lz4.h`. 123*27162e4eSAndroid Build Coastguard Worker 124*27162e4eSAndroid Build Coastguard Worker- `LZ4_HEAPMODE` : Select how stateless compression functions like `LZ4_compress_default()` 125*27162e4eSAndroid Build Coastguard Worker allocate memory for their hash table, 126*27162e4eSAndroid Build Coastguard Worker in memory stack (0:default, fastest), or in memory heap (1:requires malloc()). 127*27162e4eSAndroid Build Coastguard Worker 128*27162e4eSAndroid Build Coastguard Worker- `LZ4HC_HEAPMODE` : Select how stateless HC compression functions like `LZ4_compress_HC()` 129*27162e4eSAndroid Build Coastguard Worker allocate memory for their workspace: 130*27162e4eSAndroid Build Coastguard Worker in stack (0), or in heap (1:default). 131*27162e4eSAndroid Build Coastguard Worker Since workspace is rather large, stack can be inconvenient, hence heap mode is recommended. 132*27162e4eSAndroid Build Coastguard Worker 133*27162e4eSAndroid Build Coastguard Worker- `LZ4F_HEAPMODE` : selects how `LZ4F_compressFrame()` allocates the compression state, 134*27162e4eSAndroid Build Coastguard Worker either on stack (default, value 0) or using heap memory (value 1). 135*27162e4eSAndroid Build Coastguard Worker 136*27162e4eSAndroid Build Coastguard Worker 137*27162e4eSAndroid Build Coastguard Worker#### Makefile variables 138*27162e4eSAndroid Build Coastguard Worker 139*27162e4eSAndroid Build Coastguard WorkerThe following `Makefile` variables can be selected to alter the profile of produced binaries : 140*27162e4eSAndroid Build Coastguard Worker- `BUILD_SHARED` : generate `liblz4` dynamic library (enabled by default) 141*27162e4eSAndroid Build Coastguard Worker- `BUILD_STATIC` : generate `liblz4` static library (enabled by default) 142*27162e4eSAndroid Build Coastguard Worker 143*27162e4eSAndroid Build Coastguard Worker 144*27162e4eSAndroid Build Coastguard Worker#### Amalgamation 145*27162e4eSAndroid Build Coastguard Worker 146*27162e4eSAndroid Build Coastguard Workerlz4 source code can be amalgamated into a single file. 147*27162e4eSAndroid Build Coastguard WorkerOne can combine all source code into `lz4_all.c` by using following command: 148*27162e4eSAndroid Build Coastguard Worker``` 149*27162e4eSAndroid Build Coastguard Workercat lz4.c lz4hc.c lz4frame.c > lz4_all.c 150*27162e4eSAndroid Build Coastguard Worker``` 151*27162e4eSAndroid Build Coastguard Worker(`cat` file order is important) then compile `lz4_all.c`. 152*27162e4eSAndroid Build Coastguard WorkerAll `*.h` files present in `/lib` remain necessary to compile `lz4_all.c`. 153*27162e4eSAndroid Build Coastguard Worker 154*27162e4eSAndroid Build Coastguard Worker 155*27162e4eSAndroid Build Coastguard Worker#### Windows : using MinGW+MSYS to create DLL 156*27162e4eSAndroid Build Coastguard Worker 157*27162e4eSAndroid Build Coastguard WorkerDLL can be created using MinGW+MSYS with the `make liblz4` command. 158*27162e4eSAndroid Build Coastguard WorkerThis command creates `dll\liblz4.dll` and the import library `dll\liblz4.lib`. 159*27162e4eSAndroid Build Coastguard WorkerTo 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: 160*27162e4eSAndroid Build Coastguard Worker``` 161*27162e4eSAndroid Build Coastguard Workermake BUILD_STATIC=no CC=x86_64-w64-mingw32-gcc DLLTOOL=x86_64-w64-mingw32-dlltool OS=Windows_NT 162*27162e4eSAndroid Build Coastguard Worker``` 163*27162e4eSAndroid Build Coastguard WorkerThe import library is only required with Visual C++. 164*27162e4eSAndroid Build Coastguard WorkerThe header files `lz4.h`, `lz4hc.h`, `lz4frame.h` and the dynamic library 165*27162e4eSAndroid Build Coastguard Worker`dll\liblz4.dll` are required to compile a project using gcc/MinGW. 166*27162e4eSAndroid Build Coastguard WorkerThe dynamic library has to be added to linking options. 167*27162e4eSAndroid Build Coastguard WorkerIt means that if a project that uses LZ4 consists of a single `test-dll.c` 168*27162e4eSAndroid Build Coastguard Workerfile it should be linked with `dll\liblz4.dll`. For example: 169*27162e4eSAndroid Build Coastguard Worker``` 170*27162e4eSAndroid Build Coastguard Worker $(CC) $(CFLAGS) -Iinclude/ test-dll.c -o test-dll dll\liblz4.dll 171*27162e4eSAndroid Build Coastguard Worker``` 172*27162e4eSAndroid Build Coastguard WorkerThe compiled executable will require LZ4 DLL which is available at `dll\liblz4.dll`. 173*27162e4eSAndroid Build Coastguard Worker 174*27162e4eSAndroid Build Coastguard Worker 175*27162e4eSAndroid Build Coastguard Worker#### Miscellaneous 176*27162e4eSAndroid Build Coastguard Worker 177*27162e4eSAndroid Build Coastguard WorkerOther files present in the directory are not source code. They are : 178*27162e4eSAndroid Build Coastguard Worker 179*27162e4eSAndroid Build Coastguard Worker - `LICENSE` : contains the BSD license text 180*27162e4eSAndroid Build Coastguard Worker - `Makefile` : `make` script to compile and install lz4 library (static and dynamic) 181*27162e4eSAndroid Build Coastguard Worker - `liblz4.pc.in` : for `pkg-config` (used in `make install`) 182*27162e4eSAndroid Build Coastguard Worker - `README.md` : this file 183*27162e4eSAndroid Build Coastguard Worker 184*27162e4eSAndroid Build Coastguard Worker[official interoperable frame format]: ../doc/lz4_Frame_format.md 185*27162e4eSAndroid Build Coastguard Worker[LZ4 Frame format]: ../doc/lz4_Frame_format.md 186*27162e4eSAndroid Build Coastguard Worker[LZ4 block format]: ../doc/lz4_Block_format.md 187*27162e4eSAndroid Build Coastguard Worker 188*27162e4eSAndroid Build Coastguard Worker 189*27162e4eSAndroid Build Coastguard Worker#### License 190*27162e4eSAndroid Build Coastguard Worker 191*27162e4eSAndroid Build Coastguard WorkerAll source material within __lib__ directory are BSD 2-Clause licensed. 192*27162e4eSAndroid Build Coastguard WorkerSee [LICENSE](LICENSE) for details. 193*27162e4eSAndroid Build Coastguard WorkerThe license is also reminded at the top of each source file. 194