Linux-libre 3.16.41-gnu
[librecmc/linux-libre.git] / lib / lz4 / lz4_decompress.c
1 /*
2  * LZ4 Decompressor for Linux kernel
3  *
4  * Copyright (C) 2013, LG Electronics, Kyungsik Lee <kyungsik.lee@lge.com>
5  *
6  * Based on LZ4 implementation by Yann Collet.
7  *
8  * LZ4 - Fast LZ compression algorithm
9  * Copyright (C) 2011-2012, Yann Collet.
10  * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are
14  * met:
15  *
16  *     * Redistributions of source code must retain the above copyright
17  * notice, this list of conditions and the following disclaimer.
18  *     * Redistributions in binary form must reproduce the above
19  * copyright notice, this list of conditions and the following disclaimer
20  * in the documentation and/or other materials provided with the
21  * distribution.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  *
35  *  You can contact the author at :
36  *  - LZ4 homepage : http://fastcompression.blogspot.com/p/lz4.html
37  *  - LZ4 source repository : http://code.google.com/p/lz4/
38  */
39
40 #ifndef STATIC
41 #include <linux/module.h>
42 #include <linux/kernel.h>
43 #endif
44 #include <linux/lz4.h>
45
46 #include <asm/unaligned.h>
47
48 #include "lz4defs.h"
49
50 static int lz4_uncompress(const char *source, char *dest, int osize)
51 {
52         const BYTE *ip = (const BYTE *) source;
53         const BYTE *ref;
54         BYTE *op = (BYTE *) dest;
55         BYTE * const oend = op + osize;
56         BYTE *cpy;
57         unsigned token;
58         size_t length;
59         size_t dec32table[] = {0, 3, 2, 3, 0, 0, 0, 0};
60 #if LZ4_ARCH64
61         size_t dec64table[] = {0, 0, 0, -1, 0, 1, 2, 3};
62 #endif
63
64         while (1) {
65
66                 /* get runlength */
67                 token = *ip++;
68                 length = (token >> ML_BITS);
69                 if (length == RUN_MASK) {
70                         size_t len;
71
72                         len = *ip++;
73                         for (; len == 255; length += 255)
74                                 len = *ip++;
75                         if (unlikely(length > (size_t)(length + len)))
76                                 goto _output_error;
77                         length += len;
78                 }
79
80                 /* copy literals */
81                 cpy = op + length;
82                 if (unlikely(cpy > oend - COPYLENGTH)) {
83                         /*
84                          * Error: not enough place for another match
85                          * (min 4) + 5 literals
86                          */
87                         if (cpy != oend)
88                                 goto _output_error;
89
90                         memcpy(op, ip, length);
91                         ip += length;
92                         break; /* EOF */
93                 }
94                 LZ4_WILDCOPY(ip, op, cpy);
95                 ip -= (op - cpy);
96                 op = cpy;
97
98                 /* get offset */
99                 LZ4_READ_LITTLEENDIAN_16(ref, cpy, ip);
100                 ip += 2;
101
102                 /* Error: offset create reference outside destination buffer */
103                 if (unlikely(ref < (BYTE *const) dest))
104                         goto _output_error;
105
106                 /* get matchlength */
107                 length = token & ML_MASK;
108                 if (length == ML_MASK) {
109                         for (; *ip == 255; length += 255)
110                                 ip++;
111                         if (unlikely(length > (size_t)(length + *ip)))
112                                 goto _output_error;
113                         length += *ip++;
114                 }
115
116                 /* copy repeated sequence */
117                 if (unlikely((op - ref) < STEPSIZE)) {
118 #if LZ4_ARCH64
119                         size_t dec64 = dec64table[op - ref];
120 #else
121                         const int dec64 = 0;
122 #endif
123                         op[0] = ref[0];
124                         op[1] = ref[1];
125                         op[2] = ref[2];
126                         op[3] = ref[3];
127                         op += 4;
128                         ref += 4;
129                         ref -= dec32table[op-ref];
130                         PUT4(ref, op);
131                         op += STEPSIZE - 4;
132                         ref -= dec64;
133                 } else {
134                         LZ4_COPYSTEP(ref, op);
135                 }
136                 cpy = op + length - (STEPSIZE - 4);
137                 if (cpy > (oend - COPYLENGTH)) {
138
139                         /* Error: request to write beyond destination buffer */
140                         if (cpy > oend)
141                                 goto _output_error;
142 #if LZ4_ARCH64
143                         if ((ref + COPYLENGTH) > oend)
144 #else
145                         if ((ref + COPYLENGTH) > oend ||
146                                         (op + COPYLENGTH) > oend)
147 #endif
148                                 goto _output_error;
149                         LZ4_SECURECOPY(ref, op, (oend - COPYLENGTH));
150                         while (op < cpy)
151                                 *op++ = *ref++;
152                         op = cpy;
153                         /*
154                          * Check EOF (should never happen, since last 5 bytes
155                          * are supposed to be literals)
156                          */
157                         if (op == oend)
158                                 goto _output_error;
159                         continue;
160                 }
161                 LZ4_SECURECOPY(ref, op, cpy);
162                 op = cpy; /* correction */
163         }
164         /* end of decoding */
165         return (int) (((char *)ip) - source);
166
167         /* write overflow error detected */
168 _output_error:
169         return -1;
170 }
171
172 static int lz4_uncompress_unknownoutputsize(const char *source, char *dest,
173                                 int isize, size_t maxoutputsize)
174 {
175         const BYTE *ip = (const BYTE *) source;
176         const BYTE *const iend = ip + isize;
177         const BYTE *ref;
178
179
180         BYTE *op = (BYTE *) dest;
181         BYTE * const oend = op + maxoutputsize;
182         BYTE *cpy;
183
184         size_t dec32table[] = {0, 3, 2, 3, 0, 0, 0, 0};
185 #if LZ4_ARCH64
186         size_t dec64table[] = {0, 0, 0, -1, 0, 1, 2, 3};
187 #endif
188
189         /* Main Loop */
190         while (ip < iend) {
191
192                 unsigned token;
193                 size_t length;
194
195                 /* get runlength */
196                 token = *ip++;
197                 length = (token >> ML_BITS);
198                 if (length == RUN_MASK) {
199                         int s = 255;
200                         while ((ip < iend) && (s == 255)) {
201                                 s = *ip++;
202                                 if (unlikely(length > (size_t)(length + s)))
203                                         goto _output_error;
204                                 length += s;
205                         }
206                 }
207                 /* copy literals */
208                 cpy = op + length;
209                 if ((cpy > oend - COPYLENGTH) ||
210                         (ip + length > iend - COPYLENGTH)) {
211
212                         if (cpy > oend)
213                                 goto _output_error;/* writes beyond buffer */
214
215                         if (ip + length != iend)
216                                 goto _output_error;/*
217                                                     * Error: LZ4 format requires
218                                                     * to consume all input
219                                                     * at this stage
220                                                     */
221                         memcpy(op, ip, length);
222                         op += length;
223                         break;/* Necessarily EOF, due to parsing restrictions */
224                 }
225                 LZ4_WILDCOPY(ip, op, cpy);
226                 ip -= (op - cpy);
227                 op = cpy;
228
229                 /* get offset */
230                 LZ4_READ_LITTLEENDIAN_16(ref, cpy, ip);
231                 ip += 2;
232                 if (ref < (BYTE * const) dest)
233                         goto _output_error;
234                         /*
235                          * Error : offset creates reference
236                          * outside of destination buffer
237                          */
238
239                 /* get matchlength */
240                 length = (token & ML_MASK);
241                 if (length == ML_MASK) {
242                         while (ip < iend) {
243                                 int s = *ip++;
244                                 if (unlikely(length > (size_t)(length + s)))
245                                         goto _output_error;
246                                 length += s;
247                                 if (s == 255)
248                                         continue;
249                                 break;
250                         }
251                 }
252
253                 /* copy repeated sequence */
254                 if (unlikely((op - ref) < STEPSIZE)) {
255 #if LZ4_ARCH64
256                         size_t dec64 = dec64table[op - ref];
257 #else
258                         const int dec64 = 0;
259 #endif
260                                 op[0] = ref[0];
261                                 op[1] = ref[1];
262                                 op[2] = ref[2];
263                                 op[3] = ref[3];
264                                 op += 4;
265                                 ref += 4;
266                                 ref -= dec32table[op - ref];
267                                 PUT4(ref, op);
268                                 op += STEPSIZE - 4;
269                                 ref -= dec64;
270                 } else {
271                         LZ4_COPYSTEP(ref, op);
272                 }
273                 cpy = op + length - (STEPSIZE-4);
274                 if (cpy > oend - COPYLENGTH) {
275                         if (cpy > oend)
276                                 goto _output_error; /* write outside of buf */
277 #if LZ4_ARCH64
278                         if ((ref + COPYLENGTH) > oend)
279 #else
280                         if ((ref + COPYLENGTH) > oend ||
281                                         (op + COPYLENGTH) > oend)
282 #endif
283                                 goto _output_error;
284                         LZ4_SECURECOPY(ref, op, (oend - COPYLENGTH));
285                         while (op < cpy)
286                                 *op++ = *ref++;
287                         op = cpy;
288                         /*
289                          * Check EOF (should never happen, since last 5 bytes
290                          * are supposed to be literals)
291                          */
292                         if (op == oend)
293                                 goto _output_error;
294                         continue;
295                 }
296                 LZ4_SECURECOPY(ref, op, cpy);
297                 op = cpy; /* correction */
298         }
299         /* end of decoding */
300         return (int) (((char *) op) - dest);
301
302         /* write overflow error detected */
303 _output_error:
304         return -1;
305 }
306
307 int lz4_decompress(const unsigned char *src, size_t *src_len,
308                 unsigned char *dest, size_t actual_dest_len)
309 {
310         int ret = -1;
311         int input_len = 0;
312
313         input_len = lz4_uncompress(src, dest, actual_dest_len);
314         if (input_len < 0)
315                 goto exit_0;
316         *src_len = input_len;
317
318         return 0;
319 exit_0:
320         return ret;
321 }
322 #ifndef STATIC
323 EXPORT_SYMBOL(lz4_decompress);
324 #endif
325
326 int lz4_decompress_unknownoutputsize(const unsigned char *src, size_t src_len,
327                 unsigned char *dest, size_t *dest_len)
328 {
329         int ret = -1;
330         int out_len = 0;
331
332         out_len = lz4_uncompress_unknownoutputsize(src, dest, src_len,
333                                         *dest_len);
334         if (out_len < 0)
335                 goto exit_0;
336         *dest_len = out_len;
337
338         return 0;
339 exit_0:
340         return ret;
341 }
342 #ifndef STATIC
343 EXPORT_SYMBOL(lz4_decompress_unknownoutputsize);
344
345 MODULE_LICENSE("Dual BSD/GPL");
346 MODULE_DESCRIPTION("LZ4 Decompressor");
347 #endif