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