1/*
2 * LZ4 - Fast LZ compression algorithm
3 * Header File
4 * Copyright (C) 2011-2013, Yann Collet.
5 * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
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
16 * distribution.
17 *
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.
29 *
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/
33 *
34 * $FreeBSD$
35 */
36
37static int LZ4_uncompress_unknownOutputSize(const char *source, char *dest,
38					    int isize, int maxOutputSize);
39
40/* ARGSUSED */
41static int
42lz4_decompress(void *s_start, void *d_start, size_t s_len, size_t d_len, int dummy __unused)
43{
44	const uint8_t *src = s_start;
45	uint32_t bufsiz = htonl(*(uint32_t *)src);
46
47	/* invalid compressed buffer size encoded at start */
48	if (bufsiz + 4 > s_len)
49		return (1);
50
51	/*
52	 * Returns 0 on success (decompression function returned non-negative)
53	 * and non-zero on failure (decompression function returned negative).
54	 */
55	return (LZ4_uncompress_unknownOutputSize(s_start + 4, d_start, bufsiz,
56	    d_len) < 0);
57}
58
59/*
60 * CPU Feature Detection
61 */
62
63/* 32 or 64 bits ? */
64#if (defined(__x86_64__) || defined(__x86_64) || defined(__amd64__) || \
65	defined(__amd64) || defined(__ppc64__) || defined(_WIN64) || \
66	defined(__LP64__) || defined(_LP64))
67#define	LZ4_ARCH64	1
68#else
69#define	LZ4_ARCH64	0
70#endif
71
72/*
73 * Little Endian or Big Endian?
74 * Note: overwrite the below #define if you know your architecture endianess.
75 */
76#if BYTE_ORDER == BIG_ENDIAN
77#define	LZ4_BIG_ENDIAN	1
78#else
79	/*
80	 * Little Endian assumed. PDP Endian and other very rare endian format
81	 * are unsupported.
82	 */
83#endif
84
85/*
86 * Compiler Options
87 */
88#if __STDC_VERSION__ >= 199901L	/* C99 */
89/* "restrict" is a known keyword */
90#else
91/* Disable restrict */
92#define	restrict
93#endif
94
95#define	GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
96
97#define	lz4_bswap16(x) ((unsigned short int) ((((x) >> 8) & 0xffu) \
98	| (((x) & 0xffu) << 8)))
99
100#if (GCC_VERSION >= 302) || (__INTEL_COMPILER >= 800) || defined(__clang__)
101#define	expect(expr, value)    (__builtin_expect((expr), (value)))
102#else
103#define	expect(expr, value)    (expr)
104#endif
105
106#define	likely(expr)	expect((expr) != 0, 1)
107#define	unlikely(expr)	expect((expr) != 0, 0)
108
109/* Basic types */
110#define	BYTE	uint8_t
111#define	U16	uint16_t
112#define	U32	uint32_t
113#define	S32	int32_t
114#define	U64	uint64_t
115
116typedef struct _U16_S {
117	U16 v;
118} U16_S;
119typedef struct _U32_S {
120	U32 v;
121} U32_S;
122typedef struct _U64_S {
123	U64 v;
124} U64_S;
125
126#define	A64(x)	(((U64_S *)(x))->v)
127#define	A32(x)	(((U32_S *)(x))->v)
128#define	A16(x)	(((U16_S *)(x))->v)
129
130/*
131 * Constants
132 */
133#define	MINMATCH 4
134
135#define	COPYLENGTH 8
136#define	LASTLITERALS 5
137
138#define	ML_BITS 4
139#define	ML_MASK ((1U<<ML_BITS)-1)
140#define	RUN_BITS (8-ML_BITS)
141#define	RUN_MASK ((1U<<RUN_BITS)-1)
142
143/*
144 * Architecture-specific macros
145 */
146#if LZ4_ARCH64
147#define	STEPSIZE 8
148#define	UARCH U64
149#define	AARCH A64
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)
153#define	HTYPE U32
154#define	INITBASE(base)		const BYTE* const base = ip
155#else
156#define	STEPSIZE 4
157#define	UARCH U32
158#define	AARCH A32
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
164#endif
165
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; }
171#else
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; }
174#endif
175
176/* Macros */
177#define	LZ4_WILDCOPY(s, d, e) do { LZ4_COPYPACKET(s, d) } while (d < e);
178
179/* Decompression functions */
180
181static int
182LZ4_uncompress_unknownOutputSize(const char *source,
183    char *dest, int isize, int maxOutputSize)
184{
185	/* Local Variables */
186	const BYTE *restrict ip = (const BYTE *) source;
187	const BYTE *const iend = ip + isize;
188	const BYTE *restrict ref;
189
190	BYTE *restrict op = (BYTE *) dest;
191	BYTE *const oend = op + maxOutputSize;
192	BYTE *cpy;
193
194	size_t dec[] = { 0, 3, 2, 3, 0, 0, 0, 0 };
195
196	/* Main Loop */
197	while (ip < iend) {
198		BYTE token;
199		int length;
200
201		/* get runlength */
202		token = *ip++;
203		if ((length = (token >> ML_BITS)) == RUN_MASK) {
204			int s = 255;
205			while ((ip < iend) && (s == 255)) {
206				s = *ip++;
207				length += s;
208			}
209		}
210		/* copy literals */
211		cpy = op + length;
212		if ((cpy > oend - COPYLENGTH) ||
213		    (ip + length > iend - COPYLENGTH)) {
214			if (cpy > oend)
215				/*
216				 * Error: request to write beyond destination
217				 * buffer.
218				 */
219				goto _output_error;
220			if (ip + length > iend)
221				/*
222				 * Error : request to read beyond source
223				 * buffer.
224				 */
225				goto _output_error;
226			memcpy(op, ip, length);
227			op += length;
228			ip += length;
229			if (ip < iend)
230				/* Error : LZ4 format violation */
231				goto _output_error;
232			/* Necessarily EOF, due to parsing restrictions. */
233			break;
234		}
235		LZ4_WILDCOPY(ip, op, cpy);
236		ip -= (op - cpy);
237		op = cpy;
238
239		/* get offset */
240		LZ4_READ_LITTLEENDIAN_16(ref, cpy, ip);
241		ip += 2;
242		if (ref < (BYTE * const) dest)
243			/*
244			 * Error: offset creates reference outside of
245			 * destination buffer.
246			 */
247			goto _output_error;
248
249		/* get matchlength */
250		if ((length = (token & ML_MASK)) == ML_MASK) {
251			while (ip < iend) {
252				int s = *ip++;
253				length += s;
254				if (s == 255)
255					continue;
256				break;
257			}
258		}
259		/* copy repeated sequence */
260		if unlikely(op - ref < STEPSIZE) {
261#if LZ4_ARCH64
262			size_t dec2table[] = { 0, 0, 0, -1, 0, 1, 2, 3 };
263			size_t dec2 = dec2table[op - ref];
264#else
265			const int dec2 = 0;
266#endif
267			*op++ = *ref++;
268			*op++ = *ref++;
269			*op++ = *ref++;
270			*op++ = *ref++;
271			ref -= dec[op - ref];
272			A32(op) = A32(ref);
273			op += STEPSIZE - 4;
274			ref -= dec2;
275		} else {
276			LZ4_COPYSTEP(ref, op);
277		}
278		cpy = op + length - (STEPSIZE - 4);
279		if (cpy > oend - COPYLENGTH) {
280			if (cpy > oend)
281				/*
282				 * Error: request to write outside of
283				 * destination buffer.
284				 */
285				goto _output_error;
286			LZ4_SECURECOPY(ref, op, (oend - COPYLENGTH));
287			while (op < cpy)
288				*op++ = *ref++;
289			op = cpy;
290			if (op == oend)
291				/*
292				 * Check EOF (should never happen, since last
293				 * 5 bytes are supposed to be literals).
294				 */
295				break;
296			continue;
297		}
298		LZ4_SECURECOPY(ref, op, cpy);
299		op = cpy;	/* correction */
300	}
301
302	/* end of decoding */
303	return (int)(((char *)op) - dest);
304
305	/* write overflow error detected */
306	_output_error:
307	return (int)(-(((char *)ip) - source));
308}
309