2 * LZ4 Decompressor for Linux kernel
4 * Copyright (C) 2013, LG Electronics, Kyungsik Lee <kyungsik.lee@lge.com>
6 * Based on LZ4 implementation by Yann Collet.
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)
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions are
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
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.
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/
41 #include <linux/module.h>
42 #include <linux/kernel.h>
44 #include <linux/lz4.h>
46 #include <asm/unaligned.h>
50 static int lz4_uncompress(const char *source
, char *dest
, int osize
)
52 const BYTE
*ip
= (const BYTE
*) source
;
54 BYTE
*op
= (BYTE
*) dest
;
55 BYTE
* const oend
= op
+ osize
;
59 size_t dec32table
[] = {0, 3, 2, 3, 0, 0, 0, 0};
61 size_t dec64table
[] = {0, 0, 0, -1, 0, 1, 2, 3};
68 length
= (token
>> ML_BITS
);
69 if (length
== RUN_MASK
) {
73 for (; len
== 255; length
+= 255)
80 if (unlikely(cpy
> oend
- COPYLENGTH
)) {
82 * Error: not enough place for another match
83 * (min 4) + 5 literals
88 memcpy(op
, ip
, length
);
92 LZ4_WILDCOPY(ip
, op
, cpy
);
97 LZ4_READ_LITTLEENDIAN_16(ref
, cpy
, ip
);
100 /* Error: offset create reference outside destination buffer */
101 if (unlikely(ref
< (BYTE
*const) dest
))
104 /* get matchlength */
105 length
= token
& ML_MASK
;
106 if (length
== ML_MASK
) {
107 for (; *ip
== 255; length
+= 255)
112 /* copy repeated sequence */
113 if (unlikely((op
- ref
) < STEPSIZE
)) {
115 size_t dec64
= dec64table
[op
- ref
];
125 ref
-= dec32table
[op
-ref
];
130 LZ4_COPYSTEP(ref
, op
);
132 cpy
= op
+ length
- (STEPSIZE
- 4);
133 if (cpy
> (oend
- COPYLENGTH
)) {
135 /* Error: request to write beyond destination buffer */
138 LZ4_SECURECOPY(ref
, op
, (oend
- COPYLENGTH
));
143 * Check EOF (should never happen, since last 5 bytes
144 * are supposed to be literals)
150 LZ4_SECURECOPY(ref
, op
, cpy
);
151 op
= cpy
; /* correction */
153 /* end of decoding */
154 return (int) (((char *)ip
) - source
);
156 /* write overflow error detected */
158 return (int) (-(((char *)ip
) - source
));
161 static int lz4_uncompress_unknownoutputsize(const char *source
, char *dest
,
162 int isize
, size_t maxoutputsize
)
164 const BYTE
*ip
= (const BYTE
*) source
;
165 const BYTE
*const iend
= ip
+ isize
;
169 BYTE
*op
= (BYTE
*) dest
;
170 BYTE
* const oend
= op
+ maxoutputsize
;
173 size_t dec32table
[] = {0, 3, 2, 3, 0, 0, 0, 0};
175 size_t dec64table
[] = {0, 0, 0, -1, 0, 1, 2, 3};
186 length
= (token
>> ML_BITS
);
187 if (length
== RUN_MASK
) {
189 while ((ip
< iend
) && (s
== 255)) {
196 if ((cpy
> oend
- COPYLENGTH
) ||
197 (ip
+ length
> iend
- COPYLENGTH
)) {
200 goto _output_error
;/* writes beyond buffer */
202 if (ip
+ length
!= iend
)
203 goto _output_error
;/*
204 * Error: LZ4 format requires
205 * to consume all input
208 memcpy(op
, ip
, length
);
210 break;/* Necessarily EOF, due to parsing restrictions */
212 LZ4_WILDCOPY(ip
, op
, cpy
);
217 LZ4_READ_LITTLEENDIAN_16(ref
, cpy
, ip
);
219 if (ref
< (BYTE
* const) dest
)
222 * Error : offset creates reference
223 * outside of destination buffer
226 /* get matchlength */
227 length
= (token
& ML_MASK
);
228 if (length
== ML_MASK
) {
238 /* copy repeated sequence */
239 if (unlikely((op
- ref
) < STEPSIZE
)) {
241 size_t dec64
= dec64table
[op
- ref
];
251 ref
-= dec32table
[op
- ref
];
256 LZ4_COPYSTEP(ref
, op
);
258 cpy
= op
+ length
- (STEPSIZE
-4);
259 if (cpy
> oend
- COPYLENGTH
) {
261 goto _output_error
; /* write outside of buf */
263 LZ4_SECURECOPY(ref
, op
, (oend
- COPYLENGTH
));
268 * Check EOF (should never happen, since last 5 bytes
269 * are supposed to be literals)
275 LZ4_SECURECOPY(ref
, op
, cpy
);
276 op
= cpy
; /* correction */
278 /* end of decoding */
279 return (int) (((char *) op
) - dest
);
281 /* write overflow error detected */
283 return (int) (-(((char *) ip
) - source
));
286 int lz4_decompress(const char *src
, size_t *src_len
, char *dest
,
287 size_t actual_dest_len
)
292 input_len
= lz4_uncompress(src
, dest
, actual_dest_len
);
295 *src_len
= input_len
;
302 EXPORT_SYMBOL_GPL(lz4_decompress
);
305 int lz4_decompress_unknownoutputsize(const char *src
, size_t src_len
,
306 char *dest
, size_t *dest_len
)
311 out_len
= lz4_uncompress_unknownoutputsize(src
, dest
, src_len
,
322 EXPORT_SYMBOL_GPL(lz4_decompress_unknownoutputsize
);
324 MODULE_LICENSE("GPL");
325 MODULE_DESCRIPTION("LZ4 Decompressor");