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 #include <grub/misc.h>
38 #include <grub/types.h>
40 static int LZ4_uncompress_unknownOutputSize(const char *source, char *dest,
41 int isize, int maxOutputSize);
44 * CPU Feature Detection
48 #if (GRUB_CPU_SIZEOF_VOID_P == 8)
59 #define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
61 #if (GCC_VERSION >= 302) || (defined (__INTEL_COMPILER) && __INTEL_COMPILER >= 800) || defined(__clang__)
62 #define expect(expr, value) (__builtin_expect((expr), (value)))
64 #define expect(expr, value) (expr)
67 #define likely(expr) expect((expr) != 0, 1)
68 #define unlikely(expr) expect((expr) != 0, 0)
71 #define BYTE grub_uint8_t
72 #define U16 grub_uint16_t
73 #define U32 grub_uint32_t
74 #define S32 grub_int32_t
75 #define U64 grub_uint64_t
77 typedef struct _U16_S {
80 typedef struct _U32_S {
83 typedef struct _U64_S {
87 #define A64(x) (((U64_S *)(x))->v)
88 #define A32(x) (((U32_S *)(x))->v)
89 #define A16(x) (((U16_S *)(x))->v)
97 #define LASTLITERALS 5
100 #define ML_MASK ((1U<<ML_BITS)-1)
101 #define RUN_BITS (8-ML_BITS)
102 #define RUN_MASK ((1U<<RUN_BITS)-1)
105 * Architecture-specific macros
111 #define LZ4_COPYSTEP(s, d) A64(d) = A64(s); d += 8; s += 8;
112 #define LZ4_COPYPACKET(s, d) LZ4_COPYSTEP(s, d)
113 #define LZ4_SECURECOPY(s, d, e) if (d < e) LZ4_WILDCOPY(s, d, e)
115 #define INITBASE(base) const BYTE* const base = ip
120 #define LZ4_COPYSTEP(s, d) A32(d) = A32(s); d += 4; s += 4;
121 #define LZ4_COPYPACKET(s, d) LZ4_COPYSTEP(s, d); LZ4_COPYSTEP(s, d);
122 #define LZ4_SECURECOPY LZ4_WILDCOPY
123 #define HTYPE const BYTE*
124 #define INITBASE(base) const int base = 0
127 #define LZ4_READ_LITTLEENDIAN_16(d, s, p) { d = (s) - grub_le_to_cpu16 (A16 (p)); }
128 #define LZ4_WRITE_LITTLEENDIAN_16(p, v) { A16(p) = grub_cpu_to_le16 (v); p += 2; }
131 #define LZ4_WILDCOPY(s, d, e) do { LZ4_COPYPACKET(s, d) } while (d < e);
133 /* Decompression functions */
135 lz4_decompress(void *s_start, void *d_start, grub_size_t s_len, grub_size_t d_len);
138 lz4_decompress(void *s_start, void *d_start, grub_size_t s_len, grub_size_t d_len)
140 const BYTE *src = s_start;
141 U32 bufsiz = (src[0] << 24) | (src[1] << 16) | (src[2] << 8) |
144 /* invalid compressed buffer size encoded at start */
145 if (bufsiz + 4 > s_len)
146 return grub_error(GRUB_ERR_BAD_FS,"lz4 decompression failed.");
149 * Returns 0 on success (decompression function returned non-negative)
150 * and appropriate error on failure (decompression function returned negative).
152 return (LZ4_uncompress_unknownOutputSize((char*)s_start + 4, d_start, bufsiz,
153 d_len) < 0)?grub_error(GRUB_ERR_BAD_FS,"lz4 decompression failed."):0;
157 LZ4_uncompress_unknownOutputSize(const char *source,
158 char *dest, int isize, int maxOutputSize)
160 /* Local Variables */
161 const BYTE * ip = (const BYTE *) source;
162 const BYTE *const iend = ip + isize;
165 BYTE * op = (BYTE *) dest;
166 BYTE *const oend = op + maxOutputSize;
169 grub_size_t dec[] = { 0, 3, 2, 3, 0, 0, 0, 0 };
178 if ((length = (token >> ML_BITS)) == RUN_MASK) {
180 while ((ip < iend) && (s == 255)) {
186 if ((grub_addr_t) length > ~(grub_addr_t)op)
189 if ((cpy > oend - COPYLENGTH) ||
190 (ip + length > iend - COPYLENGTH)) {
193 * Error: request to write beyond destination
197 if (ip + length > iend)
199 * Error : request to read beyond source
203 grub_memcpy(op, ip, length);
207 /* Error : LZ4 format violation */
209 /* Necessarily EOF, due to parsing restrictions. */
212 LZ4_WILDCOPY(ip, op, cpy);
217 LZ4_READ_LITTLEENDIAN_16(ref, cpy, ip);
219 if (ref < (BYTE * const) dest)
221 * Error: offset creates reference outside of
222 * destination buffer.
226 /* get matchlength */
227 if ((length = (token & ML_MASK)) == ML_MASK) {
236 /* copy repeated sequence */
237 if unlikely(op - ref < STEPSIZE) {
239 grub_size_t dec2table[] = { 0, 0, 0, -1, 0, 1, 2, 3 };
240 grub_size_t dec2 = dec2table[op - ref];
248 ref -= dec[op - ref];
253 LZ4_COPYSTEP(ref, op);
255 cpy = op + length - (STEPSIZE - 4);
256 if (cpy > oend - COPYLENGTH) {
259 * Error: request to write outside of
260 * destination buffer.
263 LZ4_SECURECOPY(ref, op, (oend - COPYLENGTH));
269 * Check EOF (should never happen, since last
270 * 5 bytes are supposed to be literals).
275 LZ4_SECURECOPY(ref, op, cpy);
276 op = cpy; /* correction */
279 /* end of decoding */
280 return (int)(((char *)op) - dest);
282 /* write overflow error detected */
284 return (int)(-(((char *)ip) - source));