blob: b74da447e81e1fb28a656abe6359d33d49535765 [file] [log] [blame]
Kyungsik Leecffb78b2013-07-08 16:01:45 -07001/*
2 * LZ4 Decompressor for Linux kernel
3 *
Kyungsik Leee76e1fd2013-07-08 16:01:46 -07004 * Copyright (C) 2013, LG Electronics, Kyungsik Lee <kyungsik.lee@lge.com>
Kyungsik Leecffb78b2013-07-08 16:01:45 -07005 *
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
50static 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++;
Greg Kroah-Hartman206204a2014-06-20 22:01:41 -070075 if (unlikely(length > (size_t)(length + len)))
76 goto _output_error;
Kyungsik Leecffb78b2013-07-08 16:01:45 -070077 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++;
Greg Kroah-Hartman4148c1f2014-06-24 16:59:01 -0400111 if (unlikely(length > (size_t)(length + *ip)))
112 goto _output_error;
Kyungsik Leecffb78b2013-07-08 16:01:45 -0700113 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 LZ4_SECURECOPY(ref, op, (oend - COPYLENGTH));
143 while (op < cpy)
144 *op++ = *ref++;
145 op = cpy;
146 /*
147 * Check EOF (should never happen, since last 5 bytes
148 * are supposed to be literals)
149 */
150 if (op == oend)
151 goto _output_error;
152 continue;
153 }
154 LZ4_SECURECOPY(ref, op, cpy);
155 op = cpy; /* correction */
156 }
157 /* end of decoding */
158 return (int) (((char *)ip) - source);
159
160 /* write overflow error detected */
161_output_error:
Greg Kroah-Hartman4148c1f2014-06-24 16:59:01 -0400162 return -1;
Kyungsik Leecffb78b2013-07-08 16:01:45 -0700163}
164
165static int lz4_uncompress_unknownoutputsize(const char *source, char *dest,
166 int isize, size_t maxoutputsize)
167{
168 const BYTE *ip = (const BYTE *) source;
169 const BYTE *const iend = ip + isize;
170 const BYTE *ref;
171
172
173 BYTE *op = (BYTE *) dest;
174 BYTE * const oend = op + maxoutputsize;
175 BYTE *cpy;
176
177 size_t dec32table[] = {0, 3, 2, 3, 0, 0, 0, 0};
178#if LZ4_ARCH64
179 size_t dec64table[] = {0, 0, 0, -1, 0, 1, 2, 3};
180#endif
181
182 /* Main Loop */
183 while (ip < iend) {
184
185 unsigned token;
186 size_t length;
187
188 /* get runlength */
189 token = *ip++;
190 length = (token >> ML_BITS);
191 if (length == RUN_MASK) {
192 int s = 255;
193 while ((ip < iend) && (s == 255)) {
194 s = *ip++;
195 length += s;
196 }
197 }
198 /* copy literals */
199 cpy = op + length;
200 if ((cpy > oend - COPYLENGTH) ||
201 (ip + length > iend - COPYLENGTH)) {
202
203 if (cpy > oend)
204 goto _output_error;/* writes beyond buffer */
205
206 if (ip + length != iend)
207 goto _output_error;/*
208 * Error: LZ4 format requires
209 * to consume all input
210 * at this stage
211 */
212 memcpy(op, ip, length);
213 op += length;
214 break;/* Necessarily EOF, due to parsing restrictions */
215 }
216 LZ4_WILDCOPY(ip, op, cpy);
217 ip -= (op - cpy);
218 op = cpy;
219
220 /* get offset */
221 LZ4_READ_LITTLEENDIAN_16(ref, cpy, ip);
222 ip += 2;
223 if (ref < (BYTE * const) dest)
224 goto _output_error;
225 /*
226 * Error : offset creates reference
227 * outside of destination buffer
228 */
229
230 /* get matchlength */
231 length = (token & ML_MASK);
232 if (length == ML_MASK) {
233 while (ip < iend) {
234 int s = *ip++;
235 length += s;
236 if (s == 255)
237 continue;
238 break;
239 }
240 }
241
242 /* copy repeated sequence */
243 if (unlikely((op - ref) < STEPSIZE)) {
244#if LZ4_ARCH64
245 size_t dec64 = dec64table[op - ref];
246#else
247 const int dec64 = 0;
248#endif
249 op[0] = ref[0];
250 op[1] = ref[1];
251 op[2] = ref[2];
252 op[3] = ref[3];
253 op += 4;
254 ref += 4;
255 ref -= dec32table[op - ref];
256 PUT4(ref, op);
257 op += STEPSIZE - 4;
258 ref -= dec64;
259 } else {
260 LZ4_COPYSTEP(ref, op);
261 }
262 cpy = op + length - (STEPSIZE-4);
263 if (cpy > oend - COPYLENGTH) {
264 if (cpy > oend)
265 goto _output_error; /* write outside of buf */
266
267 LZ4_SECURECOPY(ref, op, (oend - COPYLENGTH));
268 while (op < cpy)
269 *op++ = *ref++;
270 op = cpy;
271 /*
272 * Check EOF (should never happen, since last 5 bytes
273 * are supposed to be literals)
274 */
275 if (op == oend)
276 goto _output_error;
277 continue;
278 }
279 LZ4_SECURECOPY(ref, op, cpy);
280 op = cpy; /* correction */
281 }
282 /* end of decoding */
283 return (int) (((char *) op) - dest);
284
285 /* write overflow error detected */
286_output_error:
287 return (int) (-(((char *) ip) - source));
288}
289
Sergey Senozhatskyb34081f12013-09-11 14:26:32 -0700290int lz4_decompress(const unsigned char *src, size_t *src_len,
291 unsigned char *dest, size_t actual_dest_len)
Kyungsik Leecffb78b2013-07-08 16:01:45 -0700292{
293 int ret = -1;
294 int input_len = 0;
295
296 input_len = lz4_uncompress(src, dest, actual_dest_len);
297 if (input_len < 0)
298 goto exit_0;
299 *src_len = input_len;
300
301 return 0;
302exit_0:
303 return ret;
304}
305#ifndef STATIC
Richard Laageree8a99b2013-08-22 16:35:47 -0700306EXPORT_SYMBOL(lz4_decompress);
Kyungsik Leecffb78b2013-07-08 16:01:45 -0700307#endif
308
Sergey Senozhatskyb34081f12013-09-11 14:26:32 -0700309int lz4_decompress_unknownoutputsize(const unsigned char *src, size_t src_len,
310 unsigned char *dest, size_t *dest_len)
Kyungsik Leecffb78b2013-07-08 16:01:45 -0700311{
312 int ret = -1;
313 int out_len = 0;
314
315 out_len = lz4_uncompress_unknownoutputsize(src, dest, src_len,
316 *dest_len);
317 if (out_len < 0)
318 goto exit_0;
319 *dest_len = out_len;
320
321 return 0;
322exit_0:
323 return ret;
324}
325#ifndef STATIC
Richard Laageree8a99b2013-08-22 16:35:47 -0700326EXPORT_SYMBOL(lz4_decompress_unknownoutputsize);
Kyungsik Leecffb78b2013-07-08 16:01:45 -0700327
Richard Laageree8a99b2013-08-22 16:35:47 -0700328MODULE_LICENSE("Dual BSD/GPL");
Kyungsik Leecffb78b2013-07-08 16:01:45 -0700329MODULE_DESCRIPTION("LZ4 Decompressor");
330#endif