2 * LZ4 - Fast LZ compression algorithm
4 * Copyright (C) 2011-2013, Yann Collet.
5 * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following disclaimer
15 * in the documentation and/or other materials provided with the
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * You can contact the author at :
31 * - LZ4 homepage : http://fastcompression.blogspot.com/p/lz4.html
32 * - LZ4 source repository : http://code.google.com/p/lz4/
37 static int LZ4_uncompress_unknownOutputSize(const char *source, char *dest,
38 int isize, int maxOutputSize);
42 lz4_decompress(void *s_start, void *d_start, size_t s_len, size_t d_len, int dummy __unused)
44 const uint8_t *src = s_start;
45 uint32_t bufsiz = htonl(*(uint32_t *)src);
47 /* invalid compressed buffer size encoded at start */
48 if (bufsiz + 4 > s_len)
52 * Returns 0 on success (decompression function returned non-negative)
53 * and non-zero on failure (decompression function returned negative).
55 return (LZ4_uncompress_unknownOutputSize(s_start + 4, d_start, bufsiz,
60 * CPU Feature Detection
64 #if (defined(__x86_64__) || defined(__x86_64) || defined(__amd64__) || \
65 defined(__amd64) || defined(__ppc64__) || defined(_WIN64) || \
66 defined(__LP64__) || defined(_LP64))
73 * Little Endian or Big Endian?
74 * Note: overwrite the below #define if you know your architecture endianess.
76 #if BYTE_ORDER == BIG_ENDIAN
77 #define LZ4_BIG_ENDIAN 1
80 * Little Endian assumed. PDP Endian and other very rare endian format
88 #if __STDC_VERSION__ >= 199901L /* C99 */
89 /* "restrict" is a known keyword */
91 /* Disable restrict */
95 #define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
97 #define lz4_bswap16(x) ((unsigned short int) ((((x) >> 8) & 0xffu) \
98 | (((x) & 0xffu) << 8)))
100 #if (GCC_VERSION >= 302) || (__INTEL_COMPILER >= 800) || defined(__clang__)
101 #define expect(expr, value) (__builtin_expect((expr), (value)))
103 #define expect(expr, value) (expr)
106 #define likely(expr) expect((expr) != 0, 1)
107 #define unlikely(expr) expect((expr) != 0, 0)
116 typedef struct _U16_S {
119 typedef struct _U32_S {
122 typedef struct _U64_S {
126 #define A64(x) (((U64_S *)(x))->v)
127 #define A32(x) (((U32_S *)(x))->v)
128 #define A16(x) (((U16_S *)(x))->v)
136 #define LASTLITERALS 5
139 #define ML_MASK ((1U<<ML_BITS)-1)
140 #define RUN_BITS (8-ML_BITS)
141 #define RUN_MASK ((1U<<RUN_BITS)-1)
144 * Architecture-specific macros
150 #define LZ4_COPYSTEP(s, d) A64(d) = A64(s); d += 8; s += 8;
151 #define LZ4_COPYPACKET(s, d) LZ4_COPYSTEP(s, d)
152 #define LZ4_SECURECOPY(s, d, e) if (d < e) LZ4_WILDCOPY(s, d, e)
154 #define INITBASE(base) const BYTE* const base = ip
159 #define LZ4_COPYSTEP(s, d) A32(d) = A32(s); d += 4; s += 4;
160 #define LZ4_COPYPACKET(s, d) LZ4_COPYSTEP(s, d); LZ4_COPYSTEP(s, d);
161 #define LZ4_SECURECOPY LZ4_WILDCOPY
162 #define HTYPE const BYTE*
163 #define INITBASE(base) const int base = 0
166 #if (defined(LZ4_BIG_ENDIAN) && !defined(BIG_ENDIAN_NATIVE_BUT_INCOMPATIBLE))
167 #define LZ4_READ_LITTLEENDIAN_16(d, s, p) \
168 { U16 v = A16(p); v = lz4_bswap16(v); d = (s) - v; }
169 #define LZ4_WRITE_LITTLEENDIAN_16(p, i) \
170 { U16 v = (U16)(i); v = lz4_bswap16(v); A16(p) = v; p += 2; }
172 #define LZ4_READ_LITTLEENDIAN_16(d, s, p) { d = (s) - A16(p); }
173 #define LZ4_WRITE_LITTLEENDIAN_16(p, v) { A16(p) = v; p += 2; }
177 #define LZ4_WILDCOPY(s, d, e) do { LZ4_COPYPACKET(s, d) } while (d < e);
179 /* Decompression functions */
182 LZ4_uncompress_unknownOutputSize(const char *source,
183 char *dest, int isize, int maxOutputSize)
185 /* Local Variables */
186 const BYTE *restrict ip = (const BYTE *) source;
187 const BYTE *const iend = ip + isize;
188 const BYTE *restrict ref;
190 BYTE *restrict op = (BYTE *) dest;
191 BYTE *const oend = op + maxOutputSize;
194 size_t dec[] = { 0, 3, 2, 3, 0, 0, 0, 0 };
203 if ((length = (token >> ML_BITS)) == RUN_MASK) {
205 while ((ip < iend) && (s == 255)) {
212 if ((cpy > oend - COPYLENGTH) ||
213 (ip + length > iend - COPYLENGTH)) {
216 * Error: request to write beyond destination
220 if (ip + length > iend)
222 * Error : request to read beyond source
226 memcpy(op, ip, length);
230 /* Error : LZ4 format violation */
232 /* Necessarily EOF, due to parsing restrictions. */
235 LZ4_WILDCOPY(ip, op, cpy);
240 LZ4_READ_LITTLEENDIAN_16(ref, cpy, ip);
242 if (ref < (BYTE * const) dest)
244 * Error: offset creates reference outside of
245 * destination buffer.
249 /* get matchlength */
250 if ((length = (token & ML_MASK)) == ML_MASK) {
259 /* copy repeated sequence */
260 if unlikely(op - ref < STEPSIZE) {
262 size_t dec2table[] = { 0, 0, 0, -1, 0, 1, 2, 3 };
263 size_t dec2 = dec2table[op - ref];
271 ref -= dec[op - ref];
276 LZ4_COPYSTEP(ref, op);
278 cpy = op + length - (STEPSIZE - 4);
279 if (cpy > oend - COPYLENGTH) {
282 * Error: request to write outside of
283 * destination buffer.
286 LZ4_SECURECOPY(ref, op, (oend - COPYLENGTH));
292 * Check EOF (should never happen, since last
293 * 5 bytes are supposed to be literals).
298 LZ4_SECURECOPY(ref, op, cpy);
299 op = cpy; /* correction */
302 /* end of decoding */
303 return (int)(((char *)op) - dest);
305 /* write overflow error detected */
307 return (int)(-(((char *)ip) - source));