2f73449f0d4c63cadc7b5b4388250a4d474594b6
[grub.git] / grub-core / fs / zfs / zfs_lz4.c
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
35 #include <grub/err.h>
36 #include <grub/mm.h>
37 #include <grub/misc.h>
38 #include <grub/types.h>
39
40 static int LZ4_uncompress_unknownOutputSize(const char *source, char *dest,
41                                             int isize, int maxOutputSize);
42
43 /*
44  * CPU Feature Detection
45  */
46
47 /* 32 or 64 bits ? */
48 #if (GRUB_CPU_SIZEOF_VOID_P == 8)
49 #define LZ4_ARCH64      1
50 #else
51 #define LZ4_ARCH64      0
52 #endif
53
54 /*
55  * Compiler Options
56  */
57
58
59 #define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
60
61 #if (GCC_VERSION >= 302) || (defined (__INTEL_COMPILER) && __INTEL_COMPILER >= 800) || defined(__clang__)
62 #define expect(expr, value)    (__builtin_expect((expr), (value)))
63 #else
64 #define expect(expr, value)    (expr)
65 #endif
66
67 #define likely(expr)    expect((expr) != 0, 1)
68 #define unlikely(expr)  expect((expr) != 0, 0)
69
70 /* Basic types */
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;
77
78 typedef struct _U16_S {
79         U16 v;
80 } GRUB_PACKED U16_S;
81 typedef struct _U32_S {
82         U32 v;
83 } GRUB_PACKED U32_S;
84 typedef struct _U64_S {
85         U64 v;
86 } GRUB_PACKED U64_S;
87
88 #define A64(x)  (((U64_S *)(x))->v)
89 #define A32(x)  (((U32_S *)(x))->v)
90 #define A16(x)  (((U16_S *)(x))->v)
91
92 /*
93  * Constants
94  */
95 #define MINMATCH 4
96
97 #define COPYLENGTH 8
98 #define LASTLITERALS 5
99
100 #define ML_BITS 4
101 #define ML_MASK ((1U<<ML_BITS)-1)
102 #define RUN_BITS (8-ML_BITS)
103 #define RUN_MASK ((1U<<RUN_BITS)-1)
104
105 /*
106  * Architecture-specific macros
107  */
108 #if LZ4_ARCH64
109 #define STEPSIZE 8
110 #define UARCH U64
111 #define AARCH A64
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)
115 #define HTYPE U32
116 #define INITBASE(base)          const BYTE* const base = ip
117 #else
118 #define STEPSIZE 4
119 #define UARCH U32
120 #define AARCH A32
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
126 #endif
127
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; }
130
131 /* Macros */
132 #define LZ4_WILDCOPY(s, d, e) do { LZ4_COPYPACKET(s, d) } while (d < e);
133
134 /* Decompression functions */
135 grub_err_t
136 lz4_decompress(void *s_start, void *d_start, size_t s_len, size_t d_len);
137
138 grub_err_t
139 lz4_decompress(void *s_start, void *d_start, size_t s_len, size_t d_len)
140 {
141         const BYTE *src = s_start;
142         U32 bufsiz = (src[0] << 24) | (src[1] << 16) | (src[2] << 8) |
143             src[3];
144
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.");
148
149         /*
150          * Returns 0 on success (decompression function returned non-negative)
151          * and appropriate error on failure (decompression function returned negative).
152          */
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;
155 }
156
157 static int
158 LZ4_uncompress_unknownOutputSize(const char *source,
159     char *dest, int isize, int maxOutputSize)
160 {
161         /* Local Variables */
162         const BYTE * ip = (const BYTE *) source;
163         const BYTE *const iend = ip + isize;
164         const BYTE * ref;
165
166         BYTE * op = (BYTE *) dest;
167         BYTE *const oend = op + maxOutputSize;
168         BYTE *cpy;
169
170         size_t dec[] = { 0, 3, 2, 3, 0, 0, 0, 0 };
171
172         /* Main Loop */
173         while (ip < iend) {
174                 BYTE token;
175                 int length;
176
177                 /* get runlength */
178                 token = *ip++;
179                 if ((length = (token >> ML_BITS)) == RUN_MASK) {
180                         int s = 255;
181                         while ((ip < iend) && (s == 255)) {
182                                 s = *ip++;
183                                 length += s;
184                         }
185                 }
186                 /* copy literals */
187                 if ((grub_addr_t) length > ~(grub_addr_t)op)
188                   goto _output_error;
189                 cpy = op + length;
190                 if ((cpy > oend - COPYLENGTH) ||
191                     (ip + length > iend - COPYLENGTH)) {
192                         if (cpy > oend)
193                                 /*
194                                  * Error: request to write beyond destination
195                                  * buffer.
196                                  */
197                                 goto _output_error;
198                         if (ip + length > iend)
199                                 /*
200                                  * Error : request to read beyond source
201                                  * buffer.
202                                  */
203                                 goto _output_error;
204                         grub_memcpy(op, ip, length);
205                         op += length;
206                         ip += length;
207                         if (ip < iend)
208                                 /* Error : LZ4 format violation */
209                                 goto _output_error;
210                         /* Necessarily EOF, due to parsing restrictions. */
211                         break;
212                 }
213                 LZ4_WILDCOPY(ip, op, cpy);
214                 ip -= (op - cpy);
215                 op = cpy;
216
217                 /* get offset */
218                 LZ4_READ_LITTLEENDIAN_16(ref, cpy, ip);
219                 ip += 2;
220                 if (ref < (BYTE * const) dest)
221                         /*
222                          * Error: offset creates reference outside of
223                          * destination buffer.
224                          */
225                         goto _output_error;
226
227                 /* get matchlength */
228                 if ((length = (token & ML_MASK)) == ML_MASK) {
229                         while (ip < iend) {
230                                 int s = *ip++;
231                                 length += s;
232                                 if (s == 255)
233                                         continue;
234                                 break;
235                         }
236                 }
237                 /* copy repeated sequence */
238                 if unlikely(op - ref < STEPSIZE) {
239 #if LZ4_ARCH64
240                         size_t dec2table[] = { 0, 0, 0, -1, 0, 1, 2, 3 };
241                         size_t dec2 = dec2table[op - ref];
242 #else
243                         const int dec2 = 0;
244 #endif
245                         *op++ = *ref++;
246                         *op++ = *ref++;
247                         *op++ = *ref++;
248                         *op++ = *ref++;
249                         ref -= dec[op - ref];
250                         A32(op) = A32(ref);
251                         op += STEPSIZE - 4;
252                         ref -= dec2;
253                 } else {
254                         LZ4_COPYSTEP(ref, op);
255                 }
256                 cpy = op + length - (STEPSIZE - 4);
257                 if (cpy > oend - COPYLENGTH) {
258                         if (cpy > oend)
259                                 /*
260                                  * Error: request to write outside of
261                                  * destination buffer.
262                                  */
263                                 goto _output_error;
264                         LZ4_SECURECOPY(ref, op, (oend - COPYLENGTH));
265                         while (op < cpy)
266                                 *op++ = *ref++;
267                         op = cpy;
268                         if (op == oend)
269                                 /*
270                                  * Check EOF (should never happen, since last
271                                  * 5 bytes are supposed to be literals).
272                                  */
273                                 break;
274                         continue;
275                 }
276                 LZ4_SECURECOPY(ref, op, cpy);
277                 op = cpy;       /* correction */
278         }
279
280         /* end of decoding */
281         return (int)(((char *)op) - dest);
282
283         /* write overflow error detected */
284         _output_error:
285         return (int)(-(((char *)ip) - source));
286 }