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
86 * Unaligned memory access is automatically enabled for "common" CPU,
87 * such as x86. For others CPU, the compiler will be more cautious, and
88 * insert extra code to ensure aligned access is respected. If you know
89 * your target CPU supports unaligned memory access, you may want to
90 * force this option manually to improve performance
92 #if defined(__ARM_FEATURE_UNALIGNED)
93 #define LZ4_FORCE_UNALIGNED_ACCESS 1
99 #if __STDC_VERSION__ >= 199901L /* C99 */
100 /* "restrict" is a known keyword */
102 /* Disable restrict */
106 #define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
108 #define lz4_bswap16(x) ((unsigned short int) ((((x) >> 8) & 0xffu) \
109 | (((x) & 0xffu) << 8)))
111 #if (GCC_VERSION >= 302) || (__INTEL_COMPILER >= 800) || defined(__clang__)
112 #define expect(expr, value) (__builtin_expect((expr), (value)))
114 #define expect(expr, value) (expr)
117 #define likely(expr) expect((expr) != 0, 1)
118 #define unlikely(expr) expect((expr) != 0, 0)
127 #ifndef LZ4_FORCE_UNALIGNED_ACCESS
131 typedef struct _U16_S {
134 typedef struct _U32_S {
137 typedef struct _U64_S {
141 #ifndef LZ4_FORCE_UNALIGNED_ACCESS
145 #define A64(x) (((U64_S *)(x))->v)
146 #define A32(x) (((U32_S *)(x))->v)
147 #define A16(x) (((U16_S *)(x))->v)
155 #define LASTLITERALS 5
158 #define ML_MASK ((1U<<ML_BITS)-1)
159 #define RUN_BITS (8-ML_BITS)
160 #define RUN_MASK ((1U<<RUN_BITS)-1)
163 * Architecture-specific macros
169 #define LZ4_COPYSTEP(s, d) A64(d) = A64(s); d += 8; s += 8;
170 #define LZ4_COPYPACKET(s, d) LZ4_COPYSTEP(s, d)
171 #define LZ4_SECURECOPY(s, d, e) if (d < e) LZ4_WILDCOPY(s, d, e)
173 #define INITBASE(base) const BYTE* const base = ip
178 #define LZ4_COPYSTEP(s, d) A32(d) = A32(s); d += 4; s += 4;
179 #define LZ4_COPYPACKET(s, d) LZ4_COPYSTEP(s, d); LZ4_COPYSTEP(s, d);
180 #define LZ4_SECURECOPY LZ4_WILDCOPY
181 #define HTYPE const BYTE*
182 #define INITBASE(base) const int base = 0
185 #if (defined(LZ4_BIG_ENDIAN) && !defined(BIG_ENDIAN_NATIVE_BUT_INCOMPATIBLE))
186 #define LZ4_READ_LITTLEENDIAN_16(d, s, p) \
187 { U16 v = A16(p); v = lz4_bswap16(v); d = (s) - v; }
188 #define LZ4_WRITE_LITTLEENDIAN_16(p, i) \
189 { U16 v = (U16)(i); v = lz4_bswap16(v); A16(p) = v; p += 2; }
191 #define LZ4_READ_LITTLEENDIAN_16(d, s, p) { d = (s) - A16(p); }
192 #define LZ4_WRITE_LITTLEENDIAN_16(p, v) { A16(p) = v; p += 2; }
196 #define LZ4_WILDCOPY(s, d, e) do { LZ4_COPYPACKET(s, d) } while (d < e);
198 /* Decompression functions */
201 LZ4_uncompress_unknownOutputSize(const char *source,
202 char *dest, int isize, int maxOutputSize)
204 /* Local Variables */
205 const BYTE *restrict ip = (const BYTE *) source;
206 const BYTE *const iend = ip + isize;
207 const BYTE *restrict ref;
209 BYTE *restrict op = (BYTE *) dest;
210 BYTE *const oend = op + maxOutputSize;
213 size_t dec[] = { 0, 3, 2, 3, 0, 0, 0, 0 };
222 if ((length = (token >> ML_BITS)) == RUN_MASK) {
224 while ((ip < iend) && (s == 255)) {
231 if ((cpy > oend - COPYLENGTH) ||
232 (ip + length > iend - COPYLENGTH)) {
235 * Error: request to write beyond destination
239 if (ip + length > iend)
241 * Error : request to read beyond source
245 memcpy(op, ip, length);
249 /* Error : LZ4 format violation */
251 /* Necessarily EOF, due to parsing restrictions. */
254 LZ4_WILDCOPY(ip, op, cpy);
259 LZ4_READ_LITTLEENDIAN_16(ref, cpy, ip);
261 if (ref < (BYTE * const) dest)
263 * Error: offset creates reference outside of
264 * destination buffer.
268 /* get matchlength */
269 if ((length = (token & ML_MASK)) == ML_MASK) {
278 /* copy repeated sequence */
279 if unlikely(op - ref < STEPSIZE) {
281 size_t dec2table[] = { 0, 0, 0, -1, 0, 1, 2, 3 };
282 size_t dec2 = dec2table[op - ref];
290 ref -= dec[op - ref];
295 LZ4_COPYSTEP(ref, op);
297 cpy = op + length - (STEPSIZE - 4);
298 if (cpy > oend - COPYLENGTH) {
301 * Error: request to write outside of
302 * destination buffer.
305 LZ4_SECURECOPY(ref, op, (oend - COPYLENGTH));
311 * Check EOF (should never happen, since last
312 * 5 bytes are supposed to be literals).
317 LZ4_SECURECOPY(ref, op, cpy);
318 op = cpy; /* correction */
321 /* end of decoding */
322 return (int)(((char *)op) - dest);
324 /* write overflow error detected */
326 return (int)(-(((char *)ip) - source));