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 const int dec32table
[] = {0, 3, 2, 3, 0, 0, 0, 0};
52 static const int dec64table
[] = {0, 0, 0, -1, 0, 1, 2, 3};
55 static int lz4_uncompress(const char *source
, char *dest
, int osize
)
57 const BYTE
*ip
= (const BYTE
*) source
;
59 BYTE
*op
= (BYTE
*) dest
;
60 BYTE
* const oend
= op
+ osize
;
69 length
= (token
>> ML_BITS
);
70 if (length
== RUN_MASK
) {
74 for (; len
== 255; length
+= 255)
76 if (unlikely(length
> (size_t)(length
+ len
)))
83 if (unlikely(cpy
> oend
- COPYLENGTH
)) {
85 * Error: not enough place for another match
86 * (min 4) + 5 literals
91 memcpy(op
, ip
, length
);
95 LZ4_WILDCOPY(ip
, op
, cpy
);
100 LZ4_READ_LITTLEENDIAN_16(ref
, cpy
, ip
);
103 /* Error: offset create reference outside destination buffer */
104 if (unlikely(ref
< (BYTE
*const) dest
))
107 /* get matchlength */
108 length
= token
& ML_MASK
;
109 if (length
== ML_MASK
) {
110 for (; *ip
== 255; length
+= 255)
112 if (unlikely(length
> (size_t)(length
+ *ip
)))
117 /* copy repeated sequence */
118 if (unlikely((op
- ref
) < STEPSIZE
)) {
120 int dec64
= dec64table
[op
- ref
];
130 ref
-= dec32table
[op
-ref
];
135 LZ4_COPYSTEP(ref
, op
);
137 cpy
= op
+ length
- (STEPSIZE
- 4);
138 if (cpy
> (oend
- COPYLENGTH
)) {
140 /* Error: request to write beyond destination buffer */
144 if ((ref
+ COPYLENGTH
) > oend
)
146 if ((ref
+ COPYLENGTH
) > oend
||
147 (op
+ COPYLENGTH
) > oend
)
150 LZ4_SECURECOPY(ref
, op
, (oend
- COPYLENGTH
));
155 * Check EOF (should never happen, since last 5 bytes
156 * are supposed to be literals)
162 LZ4_SECURECOPY(ref
, op
, cpy
);
163 op
= cpy
; /* correction */
165 /* end of decoding */
166 return (int) (((char *)ip
) - source
);
168 /* write overflow error detected */
173 static int lz4_uncompress_unknownoutputsize(const char *source
, char *dest
,
174 int isize
, size_t maxoutputsize
)
176 const BYTE
*ip
= (const BYTE
*) source
;
177 const BYTE
*const iend
= ip
+ isize
;
181 BYTE
*op
= (BYTE
*) dest
;
182 BYTE
* const oend
= op
+ maxoutputsize
;
193 length
= (token
>> ML_BITS
);
194 if (length
== RUN_MASK
) {
196 while ((ip
< iend
) && (s
== 255)) {
198 if (unlikely(length
> (size_t)(length
+ s
)))
205 if ((cpy
> oend
- COPYLENGTH
) ||
206 (ip
+ length
> iend
- COPYLENGTH
)) {
209 goto _output_error
;/* writes beyond buffer */
211 if (ip
+ length
!= iend
)
212 goto _output_error
;/*
213 * Error: LZ4 format requires
214 * to consume all input
217 memcpy(op
, ip
, length
);
219 break;/* Necessarily EOF, due to parsing restrictions */
221 LZ4_WILDCOPY(ip
, op
, cpy
);
226 LZ4_READ_LITTLEENDIAN_16(ref
, cpy
, ip
);
228 if (ref
< (BYTE
* const) dest
)
231 * Error : offset creates reference
232 * outside of destination buffer
235 /* get matchlength */
236 length
= (token
& ML_MASK
);
237 if (length
== ML_MASK
) {
240 if (unlikely(length
> (size_t)(length
+ s
)))
249 /* copy repeated sequence */
250 if (unlikely((op
- ref
) < STEPSIZE
)) {
252 int dec64
= dec64table
[op
- ref
];
262 ref
-= dec32table
[op
- ref
];
267 LZ4_COPYSTEP(ref
, op
);
269 cpy
= op
+ length
- (STEPSIZE
-4);
270 if (cpy
> oend
- COPYLENGTH
) {
272 goto _output_error
; /* write outside of buf */
274 if ((ref
+ COPYLENGTH
) > oend
)
276 if ((ref
+ COPYLENGTH
) > oend
||
277 (op
+ COPYLENGTH
) > oend
)
280 LZ4_SECURECOPY(ref
, op
, (oend
- COPYLENGTH
));
285 * Check EOF (should never happen, since last 5 bytes
286 * are supposed to be literals)
292 LZ4_SECURECOPY(ref
, op
, cpy
);
293 op
= cpy
; /* correction */
295 /* end of decoding */
296 return (int) (((char *) op
) - dest
);
298 /* write overflow error detected */
303 int lz4_decompress(const unsigned char *src
, size_t *src_len
,
304 unsigned char *dest
, size_t actual_dest_len
)
309 input_len
= lz4_uncompress(src
, dest
, actual_dest_len
);
312 *src_len
= input_len
;
319 EXPORT_SYMBOL(lz4_decompress
);
322 int lz4_decompress_unknownoutputsize(const unsigned char *src
, size_t src_len
,
323 unsigned char *dest
, size_t *dest_len
)
328 out_len
= lz4_uncompress_unknownoutputsize(src
, dest
, src_len
,
339 EXPORT_SYMBOL(lz4_decompress_unknownoutputsize
);
341 MODULE_LICENSE("Dual BSD/GPL");
342 MODULE_DESCRIPTION("LZ4 Decompressor");