]> git.karo-electronics.de Git - linux-beck.git/blob - lib/lz4/lz4_decompress.c
99a03acb7d470570b816ada76f8045d9930c6a27
[linux-beck.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                         length += *ip++;
112                 }
113
114                 /* copy repeated sequence */
115                 if (unlikely((op - ref) < STEPSIZE)) {
116 #if LZ4_ARCH64
117                         size_t dec64 = dec64table[op - ref];
118 #else
119                         const int dec64 = 0;
120 #endif
121                         op[0] = ref[0];
122                         op[1] = ref[1];
123                         op[2] = ref[2];
124                         op[3] = ref[3];
125                         op += 4;
126                         ref += 4;
127                         ref -= dec32table[op-ref];
128                         PUT4(ref, op);
129                         op += STEPSIZE - 4;
130                         ref -= dec64;
131                 } else {
132                         LZ4_COPYSTEP(ref, op);
133                 }
134                 cpy = op + length - (STEPSIZE - 4);
135                 if (cpy > (oend - COPYLENGTH)) {
136
137                         /* Error: request to write beyond destination buffer */
138                         if (cpy > oend)
139                                 goto _output_error;
140                         LZ4_SECURECOPY(ref, op, (oend - COPYLENGTH));
141                         while (op < cpy)
142                                 *op++ = *ref++;
143                         op = cpy;
144                         /*
145                          * Check EOF (should never happen, since last 5 bytes
146                          * are supposed to be literals)
147                          */
148                         if (op == oend)
149                                 goto _output_error;
150                         continue;
151                 }
152                 LZ4_SECURECOPY(ref, op, cpy);
153                 op = cpy; /* correction */
154         }
155         /* end of decoding */
156         return (int) (((char *)ip) - source);
157
158         /* write overflow error detected */
159 _output_error:
160         return (int) (-(((char *)ip) - source));
161 }
162
163 static int lz4_uncompress_unknownoutputsize(const char *source, char *dest,
164                                 int isize, size_t maxoutputsize)
165 {
166         const BYTE *ip = (const BYTE *) source;
167         const BYTE *const iend = ip + isize;
168         const BYTE *ref;
169
170
171         BYTE *op = (BYTE *) dest;
172         BYTE * const oend = op + maxoutputsize;
173         BYTE *cpy;
174
175         size_t dec32table[] = {0, 3, 2, 3, 0, 0, 0, 0};
176 #if LZ4_ARCH64
177         size_t dec64table[] = {0, 0, 0, -1, 0, 1, 2, 3};
178 #endif
179
180         /* Main Loop */
181         while (ip < iend) {
182
183                 unsigned token;
184                 size_t length;
185
186                 /* get runlength */
187                 token = *ip++;
188                 length = (token >> ML_BITS);
189                 if (length == RUN_MASK) {
190                         int s = 255;
191                         while ((ip < iend) && (s == 255)) {
192                                 s = *ip++;
193                                 length += s;
194                         }
195                 }
196                 /* copy literals */
197                 cpy = op + length;
198                 if ((cpy > oend - COPYLENGTH) ||
199                         (ip + length > iend - COPYLENGTH)) {
200
201                         if (cpy > oend)
202                                 goto _output_error;/* writes beyond buffer */
203
204                         if (ip + length != iend)
205                                 goto _output_error;/*
206                                                     * Error: LZ4 format requires
207                                                     * to consume all input
208                                                     * at this stage
209                                                     */
210                         memcpy(op, ip, length);
211                         op += length;
212                         break;/* Necessarily EOF, due to parsing restrictions */
213                 }
214                 LZ4_WILDCOPY(ip, op, cpy);
215                 ip -= (op - cpy);
216                 op = cpy;
217
218                 /* get offset */
219                 LZ4_READ_LITTLEENDIAN_16(ref, cpy, ip);
220                 ip += 2;
221                 if (ref < (BYTE * const) dest)
222                         goto _output_error;
223                         /*
224                          * Error : offset creates reference
225                          * outside of destination buffer
226                          */
227
228                 /* get matchlength */
229                 length = (token & ML_MASK);
230                 if (length == ML_MASK) {
231                         while (ip < iend) {
232                                 int s = *ip++;
233                                 length += s;
234                                 if (s == 255)
235                                         continue;
236                                 break;
237                         }
238                 }
239
240                 /* copy repeated sequence */
241                 if (unlikely((op - ref) < STEPSIZE)) {
242 #if LZ4_ARCH64
243                         size_t dec64 = dec64table[op - ref];
244 #else
245                         const int dec64 = 0;
246 #endif
247                                 op[0] = ref[0];
248                                 op[1] = ref[1];
249                                 op[2] = ref[2];
250                                 op[3] = ref[3];
251                                 op += 4;
252                                 ref += 4;
253                                 ref -= dec32table[op - ref];
254                                 PUT4(ref, op);
255                                 op += STEPSIZE - 4;
256                                 ref -= dec64;
257                 } else {
258                         LZ4_COPYSTEP(ref, op);
259                 }
260                 cpy = op + length - (STEPSIZE-4);
261                 if (cpy > oend - COPYLENGTH) {
262                         if (cpy > oend)
263                                 goto _output_error; /* write outside of buf */
264
265                         LZ4_SECURECOPY(ref, op, (oend - COPYLENGTH));
266                         while (op < cpy)
267                                 *op++ = *ref++;
268                         op = cpy;
269                         /*
270                          * Check EOF (should never happen, since last 5 bytes
271                          * are supposed to be literals)
272                          */
273                         if (op == oend)
274                                 goto _output_error;
275                         continue;
276                 }
277                 LZ4_SECURECOPY(ref, op, cpy);
278                 op = cpy; /* correction */
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 }
287
288 int lz4_decompress(const unsigned char *src, size_t *src_len,
289                 unsigned char *dest, size_t actual_dest_len)
290 {
291         int ret = -1;
292         int input_len = 0;
293
294         input_len = lz4_uncompress(src, dest, actual_dest_len);
295         if (input_len < 0)
296                 goto exit_0;
297         *src_len = input_len;
298
299         return 0;
300 exit_0:
301         return ret;
302 }
303 #ifndef STATIC
304 EXPORT_SYMBOL(lz4_decompress);
305 #endif
306
307 int lz4_decompress_unknownoutputsize(const unsigned char *src, size_t src_len,
308                 unsigned char *dest, size_t *dest_len)
309 {
310         int ret = -1;
311         int out_len = 0;
312
313         out_len = lz4_uncompress_unknownoutputsize(src, dest, src_len,
314                                         *dest_len);
315         if (out_len < 0)
316                 goto exit_0;
317         *dest_len = out_len;
318
319         return 0;
320 exit_0:
321         return ret;
322 }
323 #ifndef STATIC
324 EXPORT_SYMBOL(lz4_decompress_unknownoutputsize);
325
326 MODULE_LICENSE("Dual BSD/GPL");
327 MODULE_DESCRIPTION("LZ4 Decompressor");
328 #endif