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
76 typedef grub_size_t size_t;
78 typedef struct _U16_S {
81 typedef struct _U32_S {
84 typedef struct _U64_S {
88 #define A64(x) (((U64_S *)(x))->v)
89 #define A32(x) (((U32_S *)(x))->v)
90 #define A16(x) (((U16_S *)(x))->v)
98 #define LASTLITERALS 5
101 #define ML_MASK ((1U<<ML_BITS)-1)
102 #define RUN_BITS (8-ML_BITS)
103 #define RUN_MASK ((1U<<RUN_BITS)-1)
106 * Architecture-specific macros
112 #define LZ4_COPYSTEP(s, d) A64(d) = A64(s); d += 8; s += 8;
113 #define LZ4_COPYPACKET(s, d) LZ4_COPYSTEP(s, d)
114 #define LZ4_SECURECOPY(s, d, e) if (d < e) LZ4_WILDCOPY(s, d, e)
116 #define INITBASE(base) const BYTE* const base = ip
121 #define LZ4_COPYSTEP(s, d) A32(d) = A32(s); d += 4; s += 4;
122 #define LZ4_COPYPACKET(s, d) LZ4_COPYSTEP(s, d); LZ4_COPYSTEP(s, d);
123 #define LZ4_SECURECOPY LZ4_WILDCOPY
124 #define HTYPE const BYTE*
125 #define INITBASE(base) const int base = 0
128 #define LZ4_READ_LITTLEENDIAN_16(d, s, p) { d = (s) - grub_le_to_cpu16 (A16 (p)); }
129 #define LZ4_WRITE_LITTLEENDIAN_16(p, v) { A16(p) = grub_cpu_to_le16 (v); p += 2; }
132 #define LZ4_WILDCOPY(s, d, e) do { LZ4_COPYPACKET(s, d) } while (d < e);
134 /* Decompression functions */
136 lz4_decompress(void *s_start, void *d_start, size_t s_len, size_t d_len);
139 lz4_decompress(void *s_start, void *d_start, size_t s_len, size_t d_len)
141 const BYTE *src = s_start;
142 U32 bufsiz = (src[0] << 24) | (src[1] << 16) | (src[2] << 8) |
145 /* invalid compressed buffer size encoded at start */
146 if (bufsiz + 4 > s_len)
147 return grub_error(GRUB_ERR_BAD_FS,"lz4 decompression failed.");
150 * Returns 0 on success (decompression function returned non-negative)
151 * and appropriate error on failure (decompression function returned negative).
153 return (LZ4_uncompress_unknownOutputSize((char*)s_start + 4, d_start, bufsiz,
154 d_len) < 0)?grub_error(GRUB_ERR_BAD_FS,"lz4 decompression failed."):0;
158 LZ4_uncompress_unknownOutputSize(const char *source,
159 char *dest, int isize, int maxOutputSize)
161 /* Local Variables */
162 const BYTE * ip = (const BYTE *) source;
163 const BYTE *const iend = ip + isize;
166 BYTE * op = (BYTE *) dest;
167 BYTE *const oend = op + maxOutputSize;
170 size_t dec[] = { 0, 3, 2, 3, 0, 0, 0, 0 };
179 if ((length = (token >> ML_BITS)) == RUN_MASK) {
181 while ((ip < iend) && (s == 255)) {
187 if ((grub_addr_t) length > ~(grub_addr_t)op)
190 if ((cpy > oend - COPYLENGTH) ||
191 (ip + length > iend - COPYLENGTH)) {
194 * Error: request to write beyond destination
198 if (ip + length > iend)
200 * Error : request to read beyond source
204 grub_memcpy(op, ip, length);
208 /* Error : LZ4 format violation */
210 /* Necessarily EOF, due to parsing restrictions. */
213 LZ4_WILDCOPY(ip, op, cpy);
218 LZ4_READ_LITTLEENDIAN_16(ref, cpy, ip);
220 if (ref < (BYTE * const) dest)
222 * Error: offset creates reference outside of
223 * destination buffer.
227 /* get matchlength */
228 if ((length = (token & ML_MASK)) == ML_MASK) {
237 /* copy repeated sequence */
238 if unlikely(op - ref < STEPSIZE) {
240 size_t dec2table[] = { 0, 0, 0, -1, 0, 1, 2, 3 };
241 size_t dec2 = dec2table[op - ref];
249 ref -= dec[op - ref];
254 LZ4_COPYSTEP(ref, op);
256 cpy = op + length - (STEPSIZE - 4);
257 if (cpy > oend - COPYLENGTH) {
260 * Error: request to write outside of
261 * destination buffer.
264 LZ4_SECURECOPY(ref, op, (oend - COPYLENGTH));
270 * Check EOF (should never happen, since last
271 * 5 bytes are supposed to be literals).
276 LZ4_SECURECOPY(ref, op, cpy);
277 op = cpy; /* correction */
280 /* end of decoding */
281 return (int)(((char *)op) - dest);
283 /* write overflow error detected */
285 return (int)(-(((char *)ip) - source));