diff --git a/NOTICE b/NOTICE index 9728ab2fa35..4b9ffb79ab1 100644 --- a/NOTICE +++ b/NOTICE @@ -25,11 +25,6 @@ You may obtain a copy of the License at ~~~ -lib/wccp and lib/tsconfig developed by Network Geographics. -Copyright (C) 2010 Network Geographics, Inc. - -~~~ - WebSocket support developed by LinkedIn Copyright (C) 2014 LinkedIn @@ -92,8 +87,12 @@ Copyright (c) 2015-2016 Jason Kenny All Rights Reserved. Folly: Facebook Open-source Library https://github.com/facebook/folly - ~~ yaml-cpp: A YAML parser and emitter in C++ https://github.com/jbeder/yaml-cpp + +~~ + +fastlz: an ANSI C/C90 implementation of Lempel-Ziv 77 algorithm (LZ77) of lossless data compression. +https://github.com/ariya/FastLZ diff --git a/configure.ac b/configure.ac index 70cd480e9f4..e704b2028e4 100644 --- a/configure.ac +++ b/configure.ac @@ -2260,6 +2260,7 @@ AC_CONFIG_FILES([ include/ts/apidefs.h include/tscore/ink_config.h src/wccp/Makefile + lib/fastlz/Makefile lib/yamlcpp/Makefile mgmt/Makefile mgmt/api/Makefile diff --git a/iocore/cache/RamCacheCLFUS.cc b/iocore/cache/RamCacheCLFUS.cc index 9eca91d3ccf..d939091cb31 100644 --- a/iocore/cache/RamCacheCLFUS.cc +++ b/iocore/cache/RamCacheCLFUS.cc @@ -26,7 +26,7 @@ #include "P_Cache.h" #include "I_Tasks.h" -#include "tscore/fastlz.h" +#include "fastlz/fastlz.h" #ifdef HAVE_ZLIB_H #include #endif diff --git a/lib/ChangeLog b/lib/ChangeLog new file mode 100644 index 00000000000..af6eee7177d --- /dev/null +++ b/lib/ChangeLog @@ -0,0 +1,8 @@ +2020-02-02: Version 0.5.0 + + Minor speed improvement on the decompressor. + Prevent memory violation when decompressing corrupted input. + +2020-01-10: Version 0.4.0 + + Only code & infrastructure clean-up, no new functionality. diff --git a/lib/Makefile.am b/lib/Makefile.am index cc091b38b68..df594b3dfa3 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -16,7 +16,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -SUBDIRS = . records +SUBDIRS = . records fastlz if BUILD_PERL_LIB SUBDIRS += perl diff --git a/lib/README.md b/lib/README.md new file mode 100644 index 00000000000..6ec851ac909 --- /dev/null +++ b/lib/README.md @@ -0,0 +1,189 @@ +[![License](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT) +[![Code style](https://github.com/ariya/fastlz/workflows/Code%20style/badge.svg)](https://github.com/ariya/fastlz/actions) +[![Address Sanitizer](https://github.com/ariya/fastlz/workflows/Address%20Sanitizer/badge.svg)](https://github.com/ariya/fastlz/actions) + +## Overview + +FastLZ (MIT license) is an ANSI C/C90 implementation of [Lempel-Ziv 77 algorithm](https://en.wikipedia.org/wiki/LZ77_and_LZ78#LZ77) (LZ77) of lossless data compression. It is suitable to compress series of text/paragraphs, sequences of raw pixel data, or any other blocks of data with lots of repetition. It is not intended to be used on images, videos, and other formats of data typically already in an optimal compressed form. + +The focus for FastLZ is a very fast compression and decompression, doing that at the cost of the compression ratio. As an illustration, the comparison with zlib when compressing [enwik8](http://www.mattmahoney.net/dc/textdata.html) (also in [more details](https://github.com/inikep/lzbench)): + +||Ratio|Compression|Decompression +|--|--|--|--| +|FastLZ |54.2%|159 MB/s|305 MB/s| +|zlib -1|42.3%|50 MB/s|184 MB/s| +|zlib -9|36.5%|11 MB/s|185 MB/s| + +FastLZ is used by many software products, from a number of games (such as [Death Stranding](https://en.wikipedia.org/wiki/Death_Stranding)) to various open-source projects ([Godot Engine](https://godotengine.org/), [Facebook HHVM](https://hhvm.com/), [Apache Traffic Server](https://trafficserver.apache.org/), [Calligra Office](https://www.calligra.org/), [OSv](http://osv.io/), [Netty](https://netty.io/), etc). It even serves as the basis for other compression projects like [BLOSC](https://blosc.org/). + +For other implementations of byte-aligned LZ77, take a look at [LZ4](https://lz4.github.io/lz4/), [Snappy](http://google.github.io/snappy/), [Density](https://github.com/centaurean/density), [LZO](http://www.oberhumer.com/opensource/lzo/), [LZF](http://oldhome.schmorp.de/marc/liblzf.html), [LZJB](https://en.wikipedia.org/wiki/LZJB), [LZRW](http://www.ross.net/compression/lzrw1.html), etc. + +## Usage + +FastLZ can be used directly in any C/C++ applications. For other programming languages/environments, use the corresponding binding: + +* [Rust](https://crates.io/crates/fastlz), available on Crates: `cargo install fastlz` +* [Python](https://pypi.org/project/fastlz/), available on PyPi: `pip install fastlz` +* [JavaScript](https://www.npmjs.com/package/fastlz), available on npm: `npm install fastlz` +* [Ruby](https://rubygems.org/gems/fastlz), available on Rubygems: `gem install fastlz` +* Lua via [github.com/oneoo/lua-fastlz](https://github.com/oneoo/lua-fastlz) + +FastLZ consists of only two files: `fastlz.h` and `fastlz.c`. Just add these files to your project in order to use FastLZ. For the detailed information on the API to perform compression and decompression, see `fastlz.h`. + +For [Vcpkg](https://github.com/microsoft/vcpkg) users, FastLZ is [already available](https://github.com/microsoft/vcpkg): `vcpkg install fastlz`. + +A simple file compressor called `6pack` is included as an example on how to use FastLZ. The corresponding decompressor is `6unpack`. + +FastLZ supports any standard-conforming ANSI C/C90 compiler, including the popular ones such as GCC, Clang, Intel C++ Compiler, Visual Studio and even Tiny CC. FastLZ works well on a number of architectures (32-bit and 64-bit, big endian and little endian), from Intel/AMD, ARM, and MIPS. + +The continuous integration system runs an extensive set of compression-decompression round trips on the following systems: + + +For more details, check the corresponding [GitHub Actions build logs](https://github.com/ariya/FastLZ/actions). + +| | | | | +|--------------|---------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------| +| **amd64** | **Linux** | **Windows** | **macOS** | +| GCC | ![amd64_linux_gcc](https://github.com/ariya/FastLZ/workflows/amd64_linux_gcc/badge.svg) | ![amd64_windows_gcc](https://github.com/ariya/FastLZ/workflows/amd64_windows_gcc/badge.svg) | ![amd64_macos_gcc](https://github.com/ariya/FastLZ/workflows/amd64_macos_gcc/badge.svg) | +| Clang | ![amd64_linux_clang](https://github.com/ariya/FastLZ/workflows/amd64_linux_clang/badge.svg) | ![amd64_windows_clang](https://github.com/ariya/FastLZ/workflows/amd64_windows_clang/badge.svg) | ![amd64_macos_clang](https://github.com/ariya/FastLZ/workflows/amd64_macos_clang/badge.svg) | +| Intel CC | ![amd64_linux_icc](https://github.com/ariya/FastLZ/workflows/amd64_linux_icc/badge.svg) | | | +| TinyCC | ![amd64_linux_tcc](https://github.com/ariya/FastLZ/workflows/amd64_linux_tcc/badge.svg) | | | | +| VS 2017 | | ![amd64_windows_vs2017](https://github.com/ariya/FastLZ/workflows/amd64_windows_vs2017/badge.svg) | | +| VS 2019 | | ![amd64_windows_vs2019](https://github.com/ariya/FastLZ/workflows/amd64_windows_vs2019/badge.svg) | | +| **i686** | **Linux** | **Windows** | **macOS** | +| GCC | ![i686_linux_gcc](https://github.com/ariya/FastLZ/workflows/i686_linux_gcc/badge.svg) | | | +| Clang | ![i686_linux_clang](https://github.com/ariya/FastLZ/workflows/i686_linux_clang/badge.svg) | | | +| VS 2017 | | ![i686_windows_vs2017](https://github.com/ariya/FastLZ/workflows/i686_windows_vs2017/badge.svg) | | +| VS 2019 | | ![i686_windows_vs2019](https://github.com/ariya/FastLZ/workflows/i686_windows_vs2019/badge.svg) | | +| **arm64** | **Linux** | **Windows** | **macOS** | +| GCC | ![arm64_linux_gcc](https://github.com/ariya/FastLZ/workflows/arm64_linux_gcc/badge.svg) | | | +| **mips64** | **Linux** | **Windows** | **macOS** | +| GCC | ![mips64_linux_gcc](https://github.com/ariya/FastLZ/workflows/mips64_linux_gcc/badge.svg) | | | + + + +## Block Format + +Let us assume that FastLZ compresses an array of bytes, called the _uncompressed block_, into another array of bytes, called the _compressed block_. To understand what will be stored in the compressed block, it is illustrative to demonstrate how FastLZ will _decompress_ the block to retrieve the original uncompressed block. + +The first 5-bit of the block, i.e. the 5 most-significant bits of the first byte, is the **block tag**. Currently the block tag determines the compression level used to produce the compressed block. + +|Block tag|Compression level| +|---------|-----------------| +| 0 | Level 1 | +| 1 | Level 2 | + +The content of the block will vary depending on the compression level. + +### Block Format for Level 1 + +FastLZ Level 1 impements LZ77 compression algorithm with 8 KB sliding window and up to 264 bytes of match length. + +The compressed block consists of one or more **instructions**. +Each instruction starts with a 1-byte opcode, 2-byte opcode, or 3-byte opcode. + +| Instruction type | Opcode[0] | Opcode[1] | Opcode[2] +|-----------|------------------|--------------------|--| +| Literal run | `000`, L₅-L₀ | -|- | +| Short match | M₂-M₀, R₁₂-R₈ | R₇-R₀ | - | +| Long match | `111`, R₁₂-R₈ | M₇-R₀ | R₇-R₀ | + +Note that the _very first_ instruction in a compressed block is always a literal run. + +#### Literal run instruction + +For the literal run instruction, there is one or more bytes following the code. This is called the literal run. + +The 5 least-significant bits of `opcode[0]`, _L_, determines the **number of literals** following the opcode. The value of 0 indicates a 1-byte literal run, 1 indicates a 2-byte literal run, and so on. The minimum literal run is 1 and the maximum literal run is 32. + +The decompressor copies (_L + 1_) bytes of literal run, starting from the first one right after opcode. + +_Example_: If the compressed block is a 4-byte array of `[0x02, 0x41, 0x42, 0x43]`, then the opcode is `0x02` and that means a literal run of 3 bytes. The decompressor will then copy the subsequent 3 bytes, `[0x41, 0x42, 0x43]`, to the output buffer. The output buffer now represents the (original) uncompressed block, `[0x41, 0x42, 0x43]`. + +#### Short match instruction + +The 3 most-significant bits of `opcode[0]`, _M_, determines the **match length**. The value of 1 indicates a 3-byte match, 2 indicates a 4-byte match and so on. The minimum match length is 3 and the maximum match length is 8. + +The 5 least-significant bits of `opcode[0]` combined with the 8 bits of the `opcode[1]`, _R_, determines the **reference offset**. Since the offset is encoded in 13 bits, the minimum is 0 and the maximum is 8191. + +The following C code retrieves the match length and reference offset: + +```c +M = opcode[0] >> 5; +R = 256 * (opcode[0] << 5) + opcode[1]; +``` + +The decompressor copies _(M+2)_ bytes, starting from the location offsetted by _R_ in the output buffer. Note that _R_ is a *back reference*, i.e. the value of 0 corresponds the last byte in the output buffer, 1 is the second to last byte, and so forth. + +_Example 1_: If the compressed block is a 7-byte array of `[0x03, 0x41, 0x42, 0x43, 0x44, 0x20, 0x02]`, then there are two instructions in the there. The first instruction is the literal run of 4 bytes (due to _L = 3_). Thus, the decompressor copies 4 bytes to the output buffer, resulting in `[0x41, 0x42, 0x43, 0x44]`. The second instruction is the short match of 3 bytes (from _M = 1_, i.e `0x20 >> 5`) and the offset of 2. Therefore, the compressor goes back 2 bytes from the last position, copies 3 bytes (`[0x42, 0x43, 0x44]`), and appends them to the output buffer. The output buffer now represents the complete uncompressed data, `[0x41, 0x42, 0x43, 0x44, 0x42, 0x43, 0x44]`. + +_Example 2_: If the compressed block is a 4-byte array of `[0x00, 0x61, 0x40, 0x00]`, then there are two instructions in there. The first instruction is the literal run of just 1 byte (_L = 0_). Thus, the decompressor copies the byte (`0x61`) to the output buffer. The output buffer now becomes `[0x61]`. The second instruction is the short match of 4 bytes (from _M = 2_, i.e. `0x40 >> 5`) and the offset of 0. Therefore, the decompressor copies 4 bytes starting using the back reference of 0 (i.e. the position of `0x61`). The output buffer now represents the complete uncompressed data, `[0x61, 0x61, 0x61, 0x61, 0x61]`. + +#### Long match instruction + +The value of `opcode[1]`, _M_, determines the **match length**. The value of 0 indicates a 9-byte match, 1 indicates a 10-byte match and so on. The minimum match length is 9 and the maximum match length is 264. + +The 5 least-significant bits of `opcode[0]` combined with the 8 bits of `opcode[2]`, _R_, determines the **reference offset**. Since the offset is encoded in 13 bits, the minimum is 0 and the maximum is 8191. + +The following C code retrieves the match length and reference offset: + +```c +M = opcode[1]; +R = 256 * (opcode[0] << 5) + opcode[2]; +``` +The decompressor copies _(M+9)_ bytes, starting from the location offsetted by _R_ in the output buffer. Note that _R_ is a *back reference*, i.e. the value of 0 corresponds to the last byte in the output buffer, 1 is for the second to last byte, and so forth. + +_Example_: If the compressed block is a 4-byte array of `[0x01, 0x44, 0x45, 0xE0, 0x01, 0x01]`, then there are two instructions in there. The first instruction is the literal run with the length of 2 (due to _L = 1_). Thus, the decompressor copies the 2-byte literal run (`[0x44, 0x45]`) to the output buffer. The second instruction is the long match with the match length of 10 (from _M = 1_) and the offset of 1. Therefore, the decompressor copies 10 bytes starting using the back reference of 1 (i.e. the position of `0x44`). The output buffer now represents the complete uncompressed data, `[0x44, 0x45, 0x44, 0x45, 0x44, 0x45, 0x44, 0x45, 0x44, 0x45, 0x44, 0x45]`. + +#### Decompressor Reference Implementation + +The following 40-line C function implements a fully-functional decompressor for the above block format. Note that it is intended to be educational, e.g. no bound check is implemented, and therefore it is absolutely **unsafe** for production. + +```c +void fastlz_level1_decompress(const uint8_t* input, int length, uint8_t* output) { + int src = 0; + int dest = 0; + while (src < length) { + int type = input[src] >> 5; + if (type == 0) { + /* literal run */ + int run = 1 + input[src]; + src = src + 1; + while (run > 0) { + output[dest] = input[src]; + src = src + 1; + dest = dest + 1; + run = run - 1; + } + } else if (type < 7) { + /* short match */ + int ofs = 256 * (input[src] & 31) + input[src + 1]; + int len = 2 + (input[src] >> 5); + src = src + 2; + int ref = dest - ofs - 1; + while (len > 0) { + output[dest] = output[ref]; + ref = ref + 1; + dest = dest + 1; + len = len - 1; + } + } else { + /* long match */ + int ofs = 256 * (input[src] & 31) + input[src + 2]; + int len = 9 + input[src + 1]; + src = src + 3; + int ref = dest - ofs - 1; + while (len > 0) { + output[dest] = output[ref]; + ref = ref + 1; + dest = dest + 1; + len = len - 1; + } + } + } +} +``` + +### Block Format for Level 2 + +(To be written) diff --git a/lib/fastlz/LICENSE.MIT b/lib/fastlz/LICENSE.MIT new file mode 100644 index 00000000000..4938b4d0587 --- /dev/null +++ b/lib/fastlz/LICENSE.MIT @@ -0,0 +1,21 @@ +FastLZ - Byte-aligned LZ77 compression library +Copyright (C) 2005-2020 Ariya Hidayat + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + diff --git a/lib/fastlz/Makefile.am b/lib/fastlz/Makefile.am new file mode 100644 index 00000000000..af337173606 --- /dev/null +++ b/lib/fastlz/Makefile.am @@ -0,0 +1,28 @@ +# +# Makefile.am for fastlz +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +include $(top_srcdir)/build/tidy.mk + +AM_CPPFLAGS += -I$(abs_top_srcdir)/lib/fastlz + +noinst_LIBRARIES = libfastlz.a + +libfastlz_a_SOURCES = \ + fastlz.h \ + fastlz.cc diff --git a/lib/fastlz/fastlz.cc b/lib/fastlz/fastlz.cc new file mode 100644 index 00000000000..b29420f27b6 --- /dev/null +++ b/lib/fastlz/fastlz.cc @@ -0,0 +1,638 @@ +/* + FastLZ - Byte-aligned LZ77 compression library + Copyright (C) 2005-2020 Ariya Hidayat + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include "fastlz.h" + +#include + +/* + * Always check for bound when decompressing. + * Generally it is best to leave it defined. + */ +#define FASTLZ_SAFE +#if defined(FASTLZ_USE_SAFE_DECOMPRESSOR) && (FASTLZ_USE_SAFE_DECOMPRESSOR == 0) +#undef FASTLZ_SAFE +#endif + +/* + * Give hints to the compiler for branch prediction optimization. + */ +#if defined(__clang__) || (defined(__GNUC__) && (__GNUC__ > 2)) +#define FASTLZ_LIKELY(c) (__builtin_expect(!!(c), 1)) +#define FASTLZ_UNLIKELY(c) (__builtin_expect(!!(c), 0)) +#else +#define FASTLZ_LIKELY(c) (c) +#define FASTLZ_UNLIKELY(c) (c) +#endif + +/* + * Specialize custom 64-bit implementation for speed improvements. + */ +#if defined(__x86_64__) || defined(_M_X64) +#define FLZ_ARCH64 +#endif + +#if defined(FASTLZ_SAFE) +#define FASTLZ_BOUND_CHECK(cond) \ + if (FASTLZ_UNLIKELY(!(cond))) \ + return 0; +#else +#define FASTLZ_BOUND_CHECK(cond) \ + do { \ + } while (0) +#endif + +#if defined(FASTLZ_USE_MEMMOVE) && (FASTLZ_USE_MEMMOVE == 0) + +static void +fastlz_memmove(uint8_t *dest, const uint8_t *src, uint32_t count) +{ + do { + *dest++ = *src++; + } while (--count); +} + +static void +fastlz_memcpy(uint8_t *dest, const uint8_t *src, uint32_t count) +{ + return fastlz_memmove(dest, src, count); +} + +#else + +#include + +static void +fastlz_memmove(uint8_t *dest, const uint8_t *src, uint32_t count) +{ + if ((count > 4) && (dest >= src + count)) { + memmove(dest, src, count); + } else { + switch (count) { + default: + do { + *dest++ = *src++; + } while (--count); + break; + case 3: + *dest++ = *src++; + [[fallthrough]]; + case 2: + *dest++ = *src++; + [[fallthrough]]; + case 1: + *dest++ = *src++; + [[fallthrough]]; + case 0: + break; + } + } +} + +static void +fastlz_memcpy(uint8_t *dest, const uint8_t *src, uint32_t count) +{ + memcpy(dest, src, count); +} + +#endif + +#if defined(FLZ_ARCH64) + +static uint32_t +flz_readu32(const void *ptr) +{ + return *(const uint32_t *)ptr; +} + +static uint64_t +flz_readu64(const void *ptr) +{ + return *(const uint64_t *)ptr; +} + +static uint32_t +flz_cmp(const uint8_t *p, const uint8_t *q, const uint8_t *r) +{ + const uint8_t *start = p; + + if (flz_readu64(p) == flz_readu64(q)) { + p += 8; + q += 8; + } + if (flz_readu32(p) == flz_readu32(q)) { + p += 4; + q += 4; + } + while (q < r) + if (*p++ != *q++) + break; + return p - start; +} + +static void +flz_copy64(uint8_t *dest, const uint8_t *src, uint32_t count) +{ + const uint64_t *p = (const uint64_t *)src; + uint64_t *q = (uint64_t *)dest; + if (count < 16) { + if (count >= 8) { + *q++ = *p++; + } + *q++ = *p++; + } else { + *q++ = *p++; + *q++ = *p++; + *q++ = *p++; + *q++ = *p++; + } +} + +static void +flz_copy256(void *dest, const void *src) +{ + const uint64_t *p = (const uint64_t *)src; + uint64_t *q = (uint64_t *)dest; + *q++ = *p++; + *q++ = *p++; + *q++ = *p++; + *q++ = *p++; +} + +#endif /* FLZ_ARCH64 */ + +#if !defined(FLZ_ARCH64) + +static uint32_t +flz_readu32(const void *ptr) +{ + const uint8_t *p = (const uint8_t *)ptr; + return (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]; +} + +static uint32_t +flz_cmp(const uint8_t *p, const uint8_t *q, const uint8_t *r) +{ + const uint8_t *start = p; + while (q < r) + if (*p++ != *q++) + break; + return p - start; +} + +static void +flz_copy64(uint8_t *dest, const uint8_t *src, uint32_t count) +{ + const uint8_t *p = (const uint8_t *)src; + uint8_t *q = (uint8_t *)dest; + int c; + for (c = 0; c < count * 8; ++c) { + *q++ = *p++; + } +} + +static void +flz_copy256(void *dest, const void *src) +{ + const uint8_t *p = (const uint8_t *)src; + uint8_t *q = (uint8_t *)dest; + int c; + for (c = 0; c < 32; ++c) { + *q++ = *p++; + } +} + +#endif /* !FLZ_ARCH64 */ + +#define MAX_COPY 32 +#define MAX_LEN 264 /* 256 + 8 */ +#define MAX_L1_DISTANCE 8192 +#define MAX_L2_DISTANCE 8191 +#define MAX_FARDISTANCE (65535 + MAX_L2_DISTANCE - 1) + +#define HASH_LOG 14 +#define HASH_SIZE (1 << HASH_LOG) +#define HASH_MASK (HASH_SIZE - 1) + +static uint16_t +flz_hash(uint32_t v) +{ + uint32_t h = (v * 2654435769LL) >> (32 - HASH_LOG); + return h & HASH_MASK; +} + +static uint8_t * +flz_literals(uint32_t runs, const uint8_t *src, uint8_t *dest) +{ + while (runs >= MAX_COPY) { + *dest++ = MAX_COPY - 1; + flz_copy256(dest, src); + src += MAX_COPY; + dest += MAX_COPY; + runs -= MAX_COPY; + } + if (runs > 0) { + *dest++ = runs - 1; + flz_copy64(dest, src, runs); + dest += runs; + } + return dest; +} + +/* special case of memcpy: at most 32 bytes */ +static void +flz_smallcopy(uint8_t *dest, const uint8_t *src, uint32_t count) +{ +#if defined(FLZ_ARCH64) + if (count >= 8) { + const uint64_t *p = (const uint64_t *)src; + uint64_t *q = (uint64_t *)dest; + while (count > 8) { + *q++ = *p++; + count -= 8; + dest += 8; + src += 8; + } + } +#endif + fastlz_memcpy(dest, src, count); +} + +static uint8_t * +flz_finalize(uint32_t runs, const uint8_t *src, uint8_t *dest) +{ + while (runs >= MAX_COPY) { + *dest++ = MAX_COPY - 1; + flz_smallcopy(dest, src, MAX_COPY); + src += MAX_COPY; + dest += MAX_COPY; + runs -= MAX_COPY; + } + if (runs > 0) { + *dest++ = runs - 1; + flz_smallcopy(dest, src, runs); + dest += runs; + } + return dest; +} + +static uint8_t * +flz1_match(uint32_t len, uint32_t distance, uint8_t *op) +{ + --distance; + if (FASTLZ_UNLIKELY(len > MAX_LEN - 2)) + while (len > MAX_LEN - 2) { + *op++ = (7 << 5) + (distance >> 8); + *op++ = MAX_LEN - 2 - 7 - 2; + *op++ = (distance & 255); + len -= MAX_LEN - 2; + } + if (len < 7) { + *op++ = (len << 5) + (distance >> 8); + *op++ = (distance & 255); + } else { + *op++ = (7 << 5) + (distance >> 8); + *op++ = len - 7; + *op++ = (distance & 255); + } + return op; +} + +int +fastlz1_compress(const void *input, int length, void *output) +{ + const uint8_t *ip = (const uint8_t *)input; + const uint8_t *ip_start = ip; + const uint8_t *ip_bound = ip + length - 4; /* because readU32 */ + const uint8_t *ip_limit = ip + length - 12 - 1; + uint8_t *op = (uint8_t *)output; + + uint32_t htab[HASH_SIZE]; + uint32_t seq, hash; + + /* initializes hash table */ + for (hash = 0; hash < HASH_SIZE; ++hash) + htab[hash] = 0; + + /* we start with literal copy */ + const uint8_t *anchor = ip; + ip += 2; + + /* main loop */ + while (FASTLZ_LIKELY(ip < ip_limit)) { + const uint8_t *ref; + uint32_t distance, cmp; + + /* find potential match */ + do { + seq = flz_readu32(ip) & 0xffffff; + hash = flz_hash(seq); + ref = ip_start + htab[hash]; + htab[hash] = ip - ip_start; + distance = ip - ref; + cmp = FASTLZ_LIKELY(distance < MAX_L1_DISTANCE) ? flz_readu32(ref) & 0xffffff : 0x1000000; + if (FASTLZ_UNLIKELY(ip >= ip_limit)) + break; + ++ip; + } while (seq != cmp); + + if (FASTLZ_UNLIKELY(ip >= ip_limit)) + break; + --ip; + + if (FASTLZ_LIKELY(ip > anchor)) { + op = flz_literals(ip - anchor, anchor, op); + } + + uint32_t len = flz_cmp(ref + 3, ip + 3, ip_bound); + op = flz1_match(len, distance, op); + + /* update the hash at match boundary */ + ip += len; + seq = flz_readu32(ip); + hash = flz_hash(seq & 0xffffff); + htab[hash] = ip++ - ip_start; + seq >>= 8; + hash = flz_hash(seq); + htab[hash] = ip++ - ip_start; + + anchor = ip; + } + + uint32_t copy = (uint8_t *)input + length - anchor; + op = flz_finalize(copy, anchor, op); + + return op - (uint8_t *)output; +} + +int +fastlz1_decompress(const void *input, int length, void *output, int maxout) +{ + const uint8_t *ip = (const uint8_t *)input; + const uint8_t *ip_limit = ip + length; + const uint8_t *ip_bound = ip_limit - 2; + uint8_t *op = (uint8_t *)output; + uint8_t *op_limit = op + maxout; + uint32_t ctrl = (*ip++) & 31; + + while (1) { + if (ctrl >= 32) { + uint32_t len = (ctrl >> 5) - 1; + uint32_t ofs = (ctrl & 31) << 8; + const uint8_t *ref = op - ofs - 1; + if (len == 7 - 1) { + FASTLZ_BOUND_CHECK(ip <= ip_bound); + len += *ip++; + } + ref -= *ip++; + len += 3; + FASTLZ_BOUND_CHECK(op + len <= op_limit); + FASTLZ_BOUND_CHECK(ref >= (uint8_t *)output); + fastlz_memmove(op, ref, len); + op += len; + } else { + ctrl++; + FASTLZ_BOUND_CHECK(op + ctrl <= op_limit); + FASTLZ_BOUND_CHECK(ip + ctrl <= ip_limit); + fastlz_memcpy(op, ip, ctrl); + ip += ctrl; + op += ctrl; + } + + if (FASTLZ_UNLIKELY(ip > ip_bound)) + break; + ctrl = *ip++; + } + + return op - (uint8_t *)output; +} + +static uint8_t * +flz2_match(uint32_t len, uint32_t distance, uint8_t *op) +{ + --distance; + if (distance < MAX_L2_DISTANCE) { + if (len < 7) { + *op++ = (len << 5) + (distance >> 8); + *op++ = (distance & 255); + } else { + *op++ = (7 << 5) + (distance >> 8); + for (len -= 7; len >= 255; len -= 255) + *op++ = 255; + *op++ = len; + *op++ = (distance & 255); + } + } else { + /* far away, but not yet in the another galaxy... */ + if (len < 7) { + distance -= MAX_L2_DISTANCE; + *op++ = (len << 5) + 31; + *op++ = 255; + *op++ = distance >> 8; + *op++ = distance & 255; + } else { + distance -= MAX_L2_DISTANCE; + *op++ = (7 << 5) + 31; + for (len -= 7; len >= 255; len -= 255) + *op++ = 255; + *op++ = len; + *op++ = 255; + *op++ = distance >> 8; + *op++ = distance & 255; + } + } + return op; +} + +int +fastlz2_compress(const void *input, int length, void *output) +{ + const uint8_t *ip = (const uint8_t *)input; + const uint8_t *ip_start = ip; + const uint8_t *ip_bound = ip + length - 4; /* because readU32 */ + const uint8_t *ip_limit = ip + length - 12 - 1; + uint8_t *op = (uint8_t *)output; + + uint32_t htab[HASH_SIZE]; + uint32_t seq, hash; + + /* initializes hash table */ + for (hash = 0; hash < HASH_SIZE; ++hash) + htab[hash] = 0; + + /* we start with literal copy */ + const uint8_t *anchor = ip; + ip += 2; + + /* main loop */ + while (FASTLZ_LIKELY(ip < ip_limit)) { + const uint8_t *ref; + uint32_t distance, cmp; + + /* find potential match */ + do { + seq = flz_readu32(ip) & 0xffffff; + hash = flz_hash(seq); + ref = ip_start + htab[hash]; + htab[hash] = ip - ip_start; + distance = ip - ref; + cmp = FASTLZ_LIKELY(distance < MAX_FARDISTANCE) ? flz_readu32(ref) & 0xffffff : 0x1000000; + if (FASTLZ_UNLIKELY(ip >= ip_limit)) + break; + ++ip; + } while (seq != cmp); + + if (FASTLZ_UNLIKELY(ip >= ip_limit)) + break; + + --ip; + + /* far, needs at least 5-byte match */ + if (distance >= MAX_L2_DISTANCE) { + if (ref[3] != ip[3] || ref[4] != ip[4]) { + ++ip; + continue; + } + } + + if (FASTLZ_LIKELY(ip > anchor)) { + op = flz_literals(ip - anchor, anchor, op); + } + + uint32_t len = flz_cmp(ref + 3, ip + 3, ip_bound); + op = flz2_match(len, distance, op); + + /* update the hash at match boundary */ + ip += len; + seq = flz_readu32(ip); + hash = flz_hash(seq & 0xffffff); + htab[hash] = ip++ - ip_start; + seq >>= 8; + hash = flz_hash(seq); + htab[hash] = ip++ - ip_start; + + anchor = ip; + } + + uint32_t copy = (uint8_t *)input + length - anchor; + op = flz_finalize(copy, anchor, op); + + /* marker for fastlz2 */ + *(uint8_t *)output |= (1 << 5); + + return op - (uint8_t *)output; +} + +int +fastlz2_decompress(const void *input, int length, void *output, int maxout) +{ + const uint8_t *ip = (const uint8_t *)input; + const uint8_t *ip_limit = ip + length; + const uint8_t *ip_bound = ip_limit - 2; + uint8_t *op = (uint8_t *)output; + uint8_t *op_limit = op + maxout; + uint32_t ctrl = (*ip++) & 31; + + while (1) { + if (ctrl >= 32) { + uint32_t len = (ctrl >> 5) - 1; + uint32_t ofs = (ctrl & 31) << 8; + const uint8_t *ref = op - ofs - 1; + + uint8_t code; + if (len == 7 - 1) + do { + FASTLZ_BOUND_CHECK(ip <= ip_bound); + code = *ip++; + len += code; + } while (code == 255); + code = *ip++; + ref -= code; + len += 3; + + /* match from 16-bit distance */ + if (FASTLZ_UNLIKELY(code == 255)) + if (FASTLZ_LIKELY(ofs == (31 << 8))) { + FASTLZ_BOUND_CHECK(ip < ip_bound); + ofs = (*ip++) << 8; + ofs += *ip++; + ref = op - ofs - MAX_L2_DISTANCE - 1; + } + + FASTLZ_BOUND_CHECK(op + len <= op_limit); + FASTLZ_BOUND_CHECK(ref >= (uint8_t *)output); + fastlz_memmove(op, ref, len); + op += len; + } else { + ctrl++; + FASTLZ_BOUND_CHECK(op + ctrl <= op_limit); + FASTLZ_BOUND_CHECK(ip + ctrl <= ip_limit); + fastlz_memcpy(op, ip, ctrl); + ip += ctrl; + op += ctrl; + } + + if (FASTLZ_UNLIKELY(ip >= ip_limit)) + break; + ctrl = *ip++; + } + + return op - (uint8_t *)output; +} + +int +fastlz_compress(const void *input, int length, void *output) +{ + /* for short block, choose fastlz1 */ + if (length < 65536) + return fastlz1_compress(input, length, output); + + /* else... */ + return fastlz2_compress(input, length, output); +} + +int +fastlz_decompress(const void *input, int length, void *output, int maxout) +{ + /* magic identifier for compression level */ + int level = ((*(const uint8_t *)input) >> 5) + 1; + + if (level == 1) + return fastlz1_decompress(input, length, output, maxout); + if (level == 2) + return fastlz2_decompress(input, length, output, maxout); + + /* unknown level, trigger error */ + return 0; +} + +int +fastlz_compress_level(int level, const void *input, int length, void *output) +{ + if (level == 1) + return fastlz1_compress(input, length, output); + if (level == 2) + return fastlz2_compress(input, length, output); + + return 0; +} diff --git a/include/tscore/fastlz.h b/lib/fastlz/fastlz.h similarity index 76% rename from include/tscore/fastlz.h rename to lib/fastlz/fastlz.h index c0006c9f78e..42fccde5744 100644 --- a/include/tscore/fastlz.h +++ b/lib/fastlz/fastlz.h @@ -1,9 +1,6 @@ /* - FastLZ - lightning-fast lossless compression library - - Copyright (C) 2007 Ariya Hidayat (ariya@kde.org) - Copyright (C) 2006 Ariya Hidayat (ariya@kde.org) - Copyright (C) 2005 Ariya Hidayat (ariya@kde.org) + FastLZ - Byte-aligned LZ77 compression library + Copyright (C) 2005-2020 Ariya Hidayat Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -24,15 +21,16 @@ THE SOFTWARE. */ -#pragma once +#ifndef FASTLZ_H +#define FASTLZ_H -#define FASTLZ_VERSION 0x000100 +#define FASTLZ_VERSION 0x000500 #define FASTLZ_VERSION_MAJOR 0 -#define FASTLZ_VERSION_MINOR 0 +#define FASTLZ_VERSION_MINOR 5 #define FASTLZ_VERSION_REVISION 0 -#define FASTLZ_VERSION_STRING "0.1.0" +#define FASTLZ_VERSION_STRING "0.5.0" #if defined(__cplusplus) extern "C" { @@ -50,9 +48,17 @@ extern "C" { length (input buffer size). The input buffer and the output buffer can not overlap. + + Compression level can be specified in parameter level. At the moment, + only level 1 and level 2 are supported. + Level 1 is the fastest compression and generally useful for short data. + Level 2 is slightly slower but it gives better compression ratio. + + Note that the compressed data, regardless of the level, can always be + decompressed using the function fastlz_decompress below. */ -int fastlz_compress(const void *input, int length, void *output); +int fastlz_compress_level(int level, const void *input, int length, void *output); /** Decompress a block of compressed data and returns the size of the @@ -64,34 +70,28 @@ int fastlz_compress(const void *input, int length, void *output); Decompression is memory safe and guaranteed not to write the output buffer more than what is specified in maxout. + + Note that the decompression will always work, regardless of the + compression level specified in fastlz_compress_level above (when + producing the compressed block). */ int fastlz_decompress(const void *input, int length, void *output, int maxout); /** - Compress a block of data in the input buffer and returns the size of - compressed block. The size of input buffer is specified by length. The - minimum input buffer size is 16. - - The output buffer must be at least 5% larger than the input buffer - and can not be smaller than 66 bytes. - - If the input is not compressible, the return value might be larger than - length (input buffer size). + DEPRECATED. - The input buffer and the output buffer can not overlap. + This is similar to fastlz_compress_level above, but with the level + automatically chosen. - Compression level can be specified in parameter level. At the moment, - only level 1 and level 2 are supported. - Level 1 is the fastest compression and generally useful for short data. - Level 2 is slightly slower but it gives better compression ratio. - - Note that the compressed data, regardless of the level, can always be - decompressed using the function fastlz_decompress above. + This function is deprecated and it will be completely removed in some future + version. */ -int fastlz_compress_level(int level, const void *input, int length, void *output); +int fastlz_compress(const void *input, int length, void *output); #if defined(__cplusplus) } #endif + +#endif /* FASTLZ_H */ diff --git a/src/traffic_server/Makefile.inc b/src/traffic_server/Makefile.inc index 0e5f43cb0a1..1472ede40f0 100644 --- a/src/traffic_server/Makefile.inc +++ b/src/traffic_server/Makefile.inc @@ -74,6 +74,7 @@ traffic_server_traffic_server_LDADD = \ $(top_builddir)/iocore/hostdb/libinkhostdb.a \ $(top_builddir)/iocore/dns/libinkdns.a \ $(top_builddir)/iocore/cache/libinkcache.a \ + $(top_builddir)/lib/fastlz/libfastlz.a \ $(top_builddir)/iocore/aio/libinkaio.a \ $(top_builddir)/src/tscore/libtscore.la \ $(top_builddir)/src/tscpp/util/libtscpputil.la \ diff --git a/src/tscore/Makefile.am b/src/tscore/Makefile.am index a1cef02d0a4..cf72826159a 100644 --- a/src/tscore/Makefile.am +++ b/src/tscore/Makefile.am @@ -63,7 +63,6 @@ libtscore_la_SOURCES = \ Errata.cc \ EventNotify.cc \ Extendible.cc \ - fastlz.c \ Hash.cc \ HashFNV.cc \ HashMD5.cc \ diff --git a/src/tscore/fastlz.c b/src/tscore/fastlz.c deleted file mode 100644 index ebb5e8e173a..00000000000 --- a/src/tscore/fastlz.c +++ /dev/null @@ -1,535 +0,0 @@ -/* - FastLZ - lightning-fast lossless compression library - - Copyright (C) 2007 Ariya Hidayat (ariya@kde.org) - Copyright (C) 2006 Ariya Hidayat (ariya@kde.org) - Copyright (C) 2005 Ariya Hidayat (ariya@kde.org) - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in - all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. -*/ - -#if !defined(FASTLZ__COMPRESSOR) && !defined(FASTLZ_DECOMPRESSOR) - -/* - * Always check for bound when decompressing. - * Generally it is best to leave it defined. - */ -#define FASTLZ_SAFE - -/* - * Give hints to the compiler for branch prediction optimization. - */ -#if defined(__GNUC__) && (__GNUC__ > 2) -#define FASTLZ_EXPECT_CONDITIONAL(c) (__builtin_expect((c), 1)) -#define FASTLZ_UNEXPECT_CONDITIONAL(c) (__builtin_expect((c), 0)) -#else -#define FASTLZ_EXPECT_CONDITIONAL(c) (c) -#define FASTLZ_UNEXPECT_CONDITIONAL(c) (c) -#endif - -/* - * Use inlined functions for supported systems. - */ -#if defined(__GNUC__) || defined(__DMC__) || defined(__POCC__) || defined(__WATCOMC__) || defined(__SUNPRO_C) -#define FASTLZ_INLINE inline -#elif defined(__BORLANDC__) || defined(_MSC_VER) || defined(__LCC__) -#define FASTLZ_INLINE __inline -#else -#define FASTLZ_INLINE -#endif - -/* - * Prevent accessing more than 8-bit at once, except on x86 architectures. - */ -#if !defined(FASTLZ_STRICT_ALIGN) -#define FASTLZ_STRICT_ALIGN -#if defined(__i386__) || defined(__386) /* GNU C, Sun Studio */ -#undef FASTLZ_STRICT_ALIGN -#elif defined(__i486__) || defined(__i586__) || defined(__i686__) /* GNU C */ -#undef FASTLZ_STRICT_ALIGN -#elif defined(_M_IX86) /* Intel, MSVC */ -#undef FASTLZ_STRICT_ALIGN -#elif defined(__386) -#undef FASTLZ_STRICT_ALIGN -#elif defined(_X86_) /* MinGW */ -#undef FASTLZ_STRICT_ALIGN -#elif defined(__I86__) /* Digital Mars */ -#undef FASTLZ_STRICT_ALIGN -#endif -#endif - -/* - * FIXME: use preprocessor magic to set this on different platforms! - */ -typedef unsigned char flzuint8; -typedef unsigned short flzuint16; -typedef unsigned int flzuint32; - -/* prototypes */ -int fastlz_compress(const void *input, int length, void *output); -int fastlz_compress_level(int level, const void *input, int length, void *output); -int fastlz_decompress(const void *input, int length, void *output, int maxout); - -#define MAX_COPY 32 -#define MAX_LEN 264 /* 256 + 8 */ -#define MAX_DISTANCE 8192 - -#if !defined(FASTLZ_STRICT_ALIGN) -#define FASTLZ_READU16(p) *((const flzuint16 *)(p)) -#else -#define FASTLZ_READU16(p) ((p)[0] | (p)[1] << 8) -#endif - -#define HASH_LOG 13 -#define HASH_SIZE (1 << HASH_LOG) -#define HASH_MASK (HASH_SIZE - 1) -#define HASH_FUNCTION(v, p) \ - { \ - v = FASTLZ_READU16(p); \ - v ^= FASTLZ_READU16(p + 1) ^ (v >> (16 - HASH_LOG)); \ - v &= HASH_MASK; \ - } - -#undef FASTLZ_LEVEL -#define FASTLZ_LEVEL 1 - -#undef FASTLZ_COMPRESSOR -#undef FASTLZ_DECOMPRESSOR -#define FASTLZ_COMPRESSOR fastlz1_compress -#define FASTLZ_DECOMPRESSOR fastlz1_decompress -static FASTLZ_INLINE int FASTLZ_COMPRESSOR(const void *input, int length, void *output); -static FASTLZ_INLINE int FASTLZ_DECOMPRESSOR(const void *input, int length, void *output, int maxout); -#include "fastlz.c" - -#undef FASTLZ_LEVEL -#define FASTLZ_LEVEL 2 - -#undef MAX_DISTANCE -#define MAX_DISTANCE 8191 -#define MAX_FARDISTANCE (65535 + MAX_DISTANCE - 1) - -#undef FASTLZ_COMPRESSOR -#undef FASTLZ_DECOMPRESSOR -#define FASTLZ_COMPRESSOR fastlz2_compress -#define FASTLZ_DECOMPRESSOR fastlz2_decompress -static FASTLZ_INLINE int FASTLZ_COMPRESSOR(const void *input, int length, void *output); -static FASTLZ_INLINE int FASTLZ_DECOMPRESSOR(const void *input, int length, void *output, int maxout); -#include "fastlz.c" - -int -fastlz_compress(const void *input, int length, void *output) -{ - /* for short block, choose fastlz1 */ - if (length < 65536) - return fastlz1_compress(input, length, output); - - /* else... */ - return fastlz2_compress(input, length, output); -} - -int -fastlz_decompress(const void *input, int length, void *output, int maxout) -{ - /* magic identifier for compression level */ - int level = ((*(const flzuint8 *)input) >> 5) + 1; - - if (level == 1) - return fastlz1_decompress(input, length, output, maxout); - if (level == 2) - return fastlz2_decompress(input, length, output, maxout); - - /* unknown level, trigger error */ - return 0; -} - -int -fastlz_compress_level(int level, const void *input, int length, void *output) -{ - if (level == 1) - return fastlz1_compress(input, length, output); - if (level == 2) - return fastlz2_compress(input, length, output); - - return 0; -} - -#else /* !defined(FASTLZ_COMPRESSOR) && !defined(FASTLZ_DECOMPRESSOR) */ - -static FASTLZ_INLINE int -FASTLZ_COMPRESSOR(const void *input, int length, void *output) -{ - const flzuint8 *ip = (const flzuint8 *)input; - const flzuint8 *ip_bound = ip + length - 2; - const flzuint8 *ip_limit = ip + length - 12; - flzuint8 *op = (flzuint8 *)output; - - const flzuint8 *htab[HASH_SIZE]; - const flzuint8 **hslot; - flzuint32 hval; - - flzuint32 copy; - - /* sanity check */ - if (FASTLZ_UNEXPECT_CONDITIONAL(length < 4)) { - if (length) { - /* create literal copy only */ - *op++ = length - 1; - ip_bound++; - while (ip <= ip_bound) - *op++ = *ip++; - return length + 1; - } else - return 0; - } - - /* initializes hash table */ - for (hslot = htab; hslot < htab + HASH_SIZE; hslot++) - *hslot = ip; - - /* we start with literal copy */ - copy = 2; - *op++ = MAX_COPY - 1; - *op++ = *ip++; - *op++ = *ip++; - - /* main loop */ - while (FASTLZ_EXPECT_CONDITIONAL(ip < ip_limit)) { - const flzuint8 *ref; - flzuint32 distance; - - /* minimum match length */ - flzuint32 len = 3; - - /* comparison starting-point */ - const flzuint8 *anchor = ip; - -/* check for a run */ -#if FASTLZ_LEVEL == 2 - if (ip[0] == ip[-1] && FASTLZ_READU16(ip - 1) == FASTLZ_READU16(ip + 1)) { - distance = 1; - ref = anchor - 1 + 3; - goto match; - } -#endif - - /* find potential match */ - HASH_FUNCTION(hval, ip); - hslot = htab + hval; - ref = htab[hval]; - - /* calculate distance to the match */ - distance = anchor - ref; - - /* update hash table */ - *hslot = anchor; - - /* is this a match? check the first 3 bytes */ - if (distance == 0 || -#if FASTLZ_LEVEL == 1 - (distance >= MAX_DISTANCE) || -#else - (distance >= MAX_FARDISTANCE) || -#endif - *ref++ != *ip++ || *ref++ != *ip++ || *ref++ != *ip++) - goto literal; - -#if FASTLZ_LEVEL == 2 - /* far, needs at least 5-byte match */ - if (distance >= MAX_DISTANCE) { - if (*ip++ != *ref++ || *ip++ != *ref++) - goto literal; - len += 2; - } - - match: -#endif - - /* last matched byte */ - ip = anchor + len; - - /* distance is biased */ - distance--; - - if (!distance) { - /* zero distance means a run */ - flzuint8 x = ip[-1]; - while (ip < ip_bound) - if (*ref++ != x) - break; - else - ip++; - } else - for (;;) { - /* safe because the outer check against ip limit */ - if (*ref++ != *ip++) - break; - if (*ref++ != *ip++) - break; - if (*ref++ != *ip++) - break; - if (*ref++ != *ip++) - break; - if (*ref++ != *ip++) - break; - if (*ref++ != *ip++) - break; - if (*ref++ != *ip++) - break; - if (*ref++ != *ip++) - break; - while (ip < ip_bound) - if (*ref++ != *ip++) - break; - break; - } - - /* if we have copied something, adjust the copy count */ - if (copy) - /* copy is biased, '0' means 1 byte copy */ - *(op - copy - 1) = copy - 1; - else - /* back, to overwrite the copy count */ - op--; - - /* reset literal counter */ - copy = 0; - - /* length is biased, '1' means a match of 3 bytes */ - ip -= 3; - len = ip - anchor; - -/* encode the match */ -#if FASTLZ_LEVEL == 2 - if (distance < MAX_DISTANCE) { - if (len < 7) { - *op++ = (len << 5) + (distance >> 8); - *op++ = (distance & 255); - } else { - *op++ = (7 << 5) + (distance >> 8); - for (len -= 7; len >= 255; len -= 255) - *op++ = 255; - *op++ = len; - *op++ = (distance & 255); - } - } else { - /* far away, but not yet in the another galaxy... */ - if (len < 7) { - distance -= MAX_DISTANCE; - *op++ = (len << 5) + 31; - *op++ = 255; - *op++ = distance >> 8; - *op++ = distance & 255; - } else { - distance -= MAX_DISTANCE; - *op++ = (7 << 5) + 31; - for (len -= 7; len >= 255; len -= 255) - *op++ = 255; - *op++ = len; - *op++ = 255; - *op++ = distance >> 8; - *op++ = distance & 255; - } - } -#else - - if (FASTLZ_UNEXPECT_CONDITIONAL(len > MAX_LEN - 2)) - while (len > MAX_LEN - 2) { - *op++ = (7 << 5) + (distance >> 8); - *op++ = MAX_LEN - 2 - 7 - 2; - *op++ = (distance & 255); - len -= MAX_LEN - 2; - } - - if (len < 7) { - *op++ = (len << 5) + (distance >> 8); - *op++ = (distance & 255); - } else { - *op++ = (7 << 5) + (distance >> 8); - *op++ = len - 7; - *op++ = (distance & 255); - } -#endif - - /* update the hash at match boundary */ - HASH_FUNCTION(hval, ip); - htab[hval] = ip++; - HASH_FUNCTION(hval, ip); - htab[hval] = ip++; - - /* assuming literal copy */ - *op++ = MAX_COPY - 1; - - continue; - - literal: - *op++ = *anchor++; - ip = anchor; - copy++; - if (FASTLZ_UNEXPECT_CONDITIONAL(copy == MAX_COPY)) { - copy = 0; - *op++ = MAX_COPY - 1; - } - } - - /* left-over as literal copy */ - ip_bound++; - while (ip <= ip_bound) { - *op++ = *ip++; - copy++; - if (copy == MAX_COPY) { - copy = 0; - *op++ = MAX_COPY - 1; - } - } - - /* if we have copied something, adjust the copy length */ - if (copy) - *(op - copy - 1) = copy - 1; - else - op--; - -#if FASTLZ_LEVEL == 2 - /* marker for fastlz2 */ - *(flzuint8 *)output |= (1 << 5); -#endif - - return op - (flzuint8 *)output; -} - -static FASTLZ_INLINE int -FASTLZ_DECOMPRESSOR(const void *input, int length, void *output, int maxout) -{ - const flzuint8 *ip = (const flzuint8 *)input; - const flzuint8 *ip_limit = ip + length; - flzuint8 *op = (flzuint8 *)output; - flzuint8 *op_limit = op + maxout; - flzuint32 ctrl = (*ip++) & 31; - int loop = 1; - - do { - const flzuint8 *ref = op; - flzuint32 len = ctrl >> 5; - flzuint32 ofs = (ctrl & 31) << 8; - - if (ctrl >= 32) { -#if FASTLZ_LEVEL == 2 - flzuint8 code; -#endif - len--; - ref -= ofs; - if (len == 7 - 1) -#if FASTLZ_LEVEL == 1 - len += *ip++; - ref -= *ip++; -#else - do { - code = *ip++; - len += code; - } while (code == 255); - code = *ip++; - ref -= code; - - /* match from 16-bit distance */ - if (FASTLZ_UNEXPECT_CONDITIONAL(code == 255)) - if (FASTLZ_EXPECT_CONDITIONAL(ofs == (31 << 8))) { - ofs = (*ip++) << 8; - ofs += *ip++; - ref = op - ofs - MAX_DISTANCE; - } -#endif - -#ifdef FASTLZ_SAFE - if (FASTLZ_UNEXPECT_CONDITIONAL(op + len + 3 > op_limit)) - return 0; - - if (FASTLZ_UNEXPECT_CONDITIONAL(ref - 1 < (flzuint8 *)output)) - return 0; -#endif - - if (FASTLZ_EXPECT_CONDITIONAL(ip < ip_limit)) - ctrl = *ip++; - else - loop = 0; - - if (ref == op) { - /* optimize copy for a run */ - flzuint8 b = ref[-1]; - *op++ = b; - *op++ = b; - *op++ = b; - for (; len; --len) - *op++ = b; - } else { -#if !defined(FASTLZ_STRICT_ALIGN) - const flzuint16 *p; - flzuint16 *q; -#endif - /* copy from reference */ - ref--; - *op++ = *ref++; - *op++ = *ref++; - *op++ = *ref++; - -#if !defined(FASTLZ_STRICT_ALIGN) - /* copy a byte, so that now it's word aligned */ - if (len & 1) { - *op++ = *ref++; - len--; - } - - /* copy 16-bit at once */ - q = (flzuint16 *)op; - op += len; - p = (const flzuint16 *)ref; - for (len >>= 1; len > 4; len -= 4) { - *q++ = *p++; - *q++ = *p++; - *q++ = *p++; - *q++ = *p++; - } - for (; len; --len) - *q++ = *p++; -#else - for (; len; --len) - *op++ = *ref++; -#endif - } - } else { - ctrl++; -#ifdef FASTLZ_SAFE - if (FASTLZ_UNEXPECT_CONDITIONAL(op + ctrl > op_limit)) - return 0; - if (FASTLZ_UNEXPECT_CONDITIONAL(ip + ctrl > ip_limit)) - return 0; -#endif - - *op++ = *ip++; - for (--ctrl; ctrl; ctrl--) - *op++ = *ip++; - - loop = FASTLZ_EXPECT_CONDITIONAL(ip < ip_limit); - if (loop) - ctrl = *ip++; - } - } while (FASTLZ_EXPECT_CONDITIONAL(loop)); - - return op - (flzuint8 *)output; -} - -#endif /* !defined(FASTLZ_COMPRESSOR) && !defined(FASTLZ_DECOMPRESSOR) */