2 * LZ4 - Fast LZ compression algorithm
3 * Copyright (C) 2011 - 2016, Yann Collet.
4 * BSD 2 - Clause License (http://www.opensource.org/licenses/bsd - license.php)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 * You can contact the author at :
26 * - LZ4 homepage : http://www.lz4.org
27 * - LZ4 source repository : https://github.com/lz4/lz4
29 * Changed for kernel usage by:
30 * Sven Schmidt <4sschmid@informatik.uni-hamburg.de>
33 /*-************************************
35 **************************************/
36 #include <linux/lz4.h>
38 #include <linux/init.h>
39 #include <linux/module.h>
40 #include <linux/kernel.h>
41 #include <asm/unaligned.h>
43 /*-*****************************
44 * Decompression functions
45 *******************************/
46 /* LZ4_decompress_generic() :
47 * This generic decompression function cover all use cases.
48 * It shall be instantiated several times, using different sets of directives
49 * Note that it is important this generic function is really inlined,
50 * in order to remove useless branches during compilation optimization.
52 static FORCE_INLINE int LZ4_decompress_generic(
53 const char * const source,
57 * If endOnInput == endOnInputSize,
58 * this value is the max size of Output Buffer.
61 /* endOnOutputSize, endOnInputSize */
65 /* only used if partialDecoding == partial */
67 /* noDict, withPrefix64k, usingExtDict */
69 /* == dest when no prefix */
70 const BYTE * const lowPrefix,
71 /* only if dict == usingExtDict */
72 const BYTE * const dictStart,
73 /* note : = 0 if noDict */
78 const BYTE *ip = (const BYTE *) source;
79 const BYTE * const iend = ip + inputSize;
81 BYTE *op = (BYTE *) dest;
82 BYTE * const oend = op + outputSize;
84 BYTE *oexit = op + targetOutputSize;
85 const BYTE * const lowLimit = lowPrefix - dictSize;
87 const BYTE * const dictEnd = (const BYTE *)dictStart + dictSize;
88 static const unsigned int dec32table[] = { 0, 1, 2, 1, 4, 4, 4, 4 };
89 static const int dec64table[] = { 0, 0, 0, -1, 0, 1, 2, 3 };
91 const int safeDecode = (endOnInput == endOnInputSize);
92 const int checkOffset = ((safeDecode) && (dictSize < (int)(64 * KB)));
95 /* targetOutputSize too high => decode everything */
96 if ((partialDecoding) && (oexit > oend - MFLIMIT))
97 oexit = oend - MFLIMIT;
99 /* Empty output buffer */
100 if ((endOnInput) && (unlikely(outputSize == 0)))
101 return ((inputSize == 1) && (*ip == 0)) ? 0 : -1;
103 if ((!endOnInput) && (unlikely(outputSize == 0)))
104 return (*ip == 0 ? 1 : -1);
106 /* Main Loop : decode sequences */
112 /* get literal length */
113 unsigned int const token = *ip++;
115 length = token>>ML_BITS;
117 if (length == RUN_MASK) {
123 } while (likely(endOnInput
124 ? ip < iend - RUN_MASK
129 (size_t)(op + length) < (size_t)(op))) {
130 /* overflow detection */
135 (size_t)(ip + length) < (size_t)(ip))) {
136 /* overflow detection */
143 if (((endOnInput) && ((cpy > (partialDecoding ? oexit : oend - MFLIMIT))
144 || (ip + length > iend - (2 + 1 + LASTLITERALS))))
145 || ((!endOnInput) && (cpy > oend - WILDCOPYLENGTH))) {
146 if (partialDecoding) {
150 * write attempt beyond end of output buffer
155 && (ip + length > iend)) {
158 * read attempt beyond
159 * end of input buffer
168 * block decoding must
174 && ((ip + length != iend)
178 * input must be consumed
184 memcpy(op, ip, length);
187 /* Necessarily EOF, due to parsing restrictions */
191 LZ4_wildCopy(op, ip, cpy);
196 offset = LZ4_readLE16(ip);
200 if ((checkOffset) && (unlikely(match < lowLimit))) {
201 /* Error : offset outside buffers */
205 /* costs ~1%; silence an msan warning when offset == 0 */
206 LZ4_write32(op, (U32)offset);
208 /* get matchlength */
209 length = token & ML_MASK;
210 if (length == ML_MASK) {
216 if ((endOnInput) && (ip > iend - LASTLITERALS))
224 (size_t)(op + length) < (size_t)op)) {
225 /* overflow detection */
232 /* check external dictionary */
233 if ((dict == usingExtDict) && (match < lowPrefix)) {
234 if (unlikely(op + length > oend - LASTLITERALS)) {
235 /* doesn't respect parsing restriction */
239 if (length <= (size_t)(lowPrefix - match)) {
241 * match can be copied as a single segment
242 * from external dictionary
244 memmove(op, dictEnd - (lowPrefix - match),
249 * match encompass external
250 * dictionary and current block
252 size_t const copySize = (size_t)(lowPrefix - match);
253 size_t const restSize = length - copySize;
255 memcpy(op, dictEnd - copySize, copySize);
258 if (restSize > (size_t)(op - lowPrefix)) {
260 BYTE * const endOfMatch = op + restSize;
261 const BYTE *copyFrom = lowPrefix;
263 while (op < endOfMatch)
266 memcpy(op, lowPrefix, restSize);
274 /* copy match within block */
277 if (unlikely(offset < 8)) {
278 const int dec64 = dec64table[offset];
284 match += dec32table[offset];
285 memcpy(op + 4, match, 4);
288 LZ4_copy8(op, match);
294 if (unlikely(cpy > oend - 12)) {
295 BYTE * const oCopyLimit = oend - (WILDCOPYLENGTH - 1);
297 if (cpy > oend - LASTLITERALS) {
299 * Error : last LASTLITERALS bytes
300 * must be literals (uncompressed)
305 if (op < oCopyLimit) {
306 LZ4_wildCopy(op, match, oCopyLimit);
307 match += oCopyLimit - op;
314 LZ4_copy8(op, match);
317 LZ4_wildCopy(op + 8, match + 8, cpy);
320 op = cpy; /* correction */
323 /* end of decoding */
325 /* Nb of output bytes decoded */
326 return (int) (((char *)op) - dest);
328 /* Nb of input bytes read */
329 return (int) (((const char *)ip) - source);
332 /* Overflow error detected */
337 int LZ4_decompress_safe(const char *source, char *dest,
338 int compressedSize, int maxDecompressedSize)
340 return LZ4_decompress_generic(source, dest, compressedSize,
341 maxDecompressedSize, endOnInputSize, full, 0,
342 noDict, (BYTE *)dest, NULL, 0);
345 int LZ4_decompress_safe_partial(const char *source, char *dest,
346 int compressedSize, int targetOutputSize, int maxDecompressedSize)
348 return LZ4_decompress_generic(source, dest, compressedSize,
349 maxDecompressedSize, endOnInputSize, partial,
350 targetOutputSize, noDict, (BYTE *)dest, NULL, 0);
353 int LZ4_decompress_fast(const char *source, char *dest, int originalSize)
355 return LZ4_decompress_generic(source, dest, 0, originalSize,
356 endOnOutputSize, full, 0, withPrefix64k,
357 (BYTE *)(dest - 64 * KB), NULL, 64 * KB);
360 int LZ4_setStreamDecode(LZ4_streamDecode_t *LZ4_streamDecode,
361 const char *dictionary, int dictSize)
363 LZ4_streamDecode_t_internal *lz4sd = (LZ4_streamDecode_t_internal *) LZ4_streamDecode;
365 lz4sd->prefixSize = (size_t) dictSize;
366 lz4sd->prefixEnd = (const BYTE *) dictionary + dictSize;
367 lz4sd->externalDict = NULL;
368 lz4sd->extDictSize = 0;
374 * These decoding functions allow decompression of multiple blocks
375 * in "streaming" mode.
376 * Previously decoded blocks must still be available at the memory
377 * position where they were decoded.
378 * If it's not possible, save the relevant part of
379 * decoded data into a safe buffer,
380 * and indicate where it stands using LZ4_setStreamDecode()
382 int LZ4_decompress_safe_continue(LZ4_streamDecode_t *LZ4_streamDecode,
383 const char *source, char *dest, int compressedSize, int maxOutputSize)
385 LZ4_streamDecode_t_internal *lz4sd = &LZ4_streamDecode->internal_donotuse;
388 if (lz4sd->prefixEnd == (BYTE *)dest) {
389 result = LZ4_decompress_generic(source, dest,
392 endOnInputSize, full, 0,
393 usingExtDict, lz4sd->prefixEnd - lz4sd->prefixSize,
400 lz4sd->prefixSize += result;
401 lz4sd->prefixEnd += result;
403 lz4sd->extDictSize = lz4sd->prefixSize;
404 lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize;
405 result = LZ4_decompress_generic(source, dest,
406 compressedSize, maxOutputSize,
407 endOnInputSize, full, 0,
408 usingExtDict, (BYTE *)dest,
409 lz4sd->externalDict, lz4sd->extDictSize);
412 lz4sd->prefixSize = result;
413 lz4sd->prefixEnd = (BYTE *)dest + result;
419 int LZ4_decompress_fast_continue(LZ4_streamDecode_t *LZ4_streamDecode,
420 const char *source, char *dest, int originalSize)
422 LZ4_streamDecode_t_internal *lz4sd = &LZ4_streamDecode->internal_donotuse;
425 if (lz4sd->prefixEnd == (BYTE *)dest) {
426 result = LZ4_decompress_generic(source, dest, 0, originalSize,
427 endOnOutputSize, full, 0,
429 lz4sd->prefixEnd - lz4sd->prefixSize,
430 lz4sd->externalDict, lz4sd->extDictSize);
435 lz4sd->prefixSize += originalSize;
436 lz4sd->prefixEnd += originalSize;
438 lz4sd->extDictSize = lz4sd->prefixSize;
439 lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize;
440 result = LZ4_decompress_generic(source, dest, 0, originalSize,
441 endOnOutputSize, full, 0,
442 usingExtDict, (BYTE *)dest,
443 lz4sd->externalDict, lz4sd->extDictSize);
446 lz4sd->prefixSize = originalSize;
447 lz4sd->prefixEnd = (BYTE *)dest + originalSize;
454 * Advanced decoding functions :
456 * These decoding functions work the same as "_continue" ones,
457 * the dictionary must be explicitly provided within parameters
459 static FORCE_INLINE int LZ4_decompress_usingDict_generic(const char *source,
460 char *dest, int compressedSize, int maxOutputSize, int safe,
461 const char *dictStart, int dictSize)
464 return LZ4_decompress_generic(source, dest,
465 compressedSize, maxOutputSize, safe, full, 0,
466 noDict, (BYTE *)dest, NULL, 0);
467 if (dictStart + dictSize == dest) {
468 if (dictSize >= (int)(64 * KB - 1))
469 return LZ4_decompress_generic(source, dest,
470 compressedSize, maxOutputSize, safe, full, 0,
471 withPrefix64k, (BYTE *)dest - 64 * KB, NULL, 0);
472 return LZ4_decompress_generic(source, dest, compressedSize,
473 maxOutputSize, safe, full, 0, noDict,
474 (BYTE *)dest - dictSize, NULL, 0);
476 return LZ4_decompress_generic(source, dest, compressedSize,
477 maxOutputSize, safe, full, 0, usingExtDict,
478 (BYTE *)dest, (const BYTE *)dictStart, dictSize);
481 int LZ4_decompress_safe_usingDict(const char *source, char *dest,
482 int compressedSize, int maxOutputSize,
483 const char *dictStart, int dictSize)
485 return LZ4_decompress_usingDict_generic(source, dest,
486 compressedSize, maxOutputSize, 1, dictStart, dictSize);
489 int LZ4_decompress_fast_usingDict(const char *source, char *dest,
490 int originalSize, const char *dictStart, int dictSize)
492 return LZ4_decompress_usingDict_generic(source, dest, 0,
493 originalSize, 0, dictStart, dictSize);
497 EXPORT_SYMBOL(LZ4_decompress_safe);
498 EXPORT_SYMBOL(LZ4_decompress_safe_partial);
499 EXPORT_SYMBOL(LZ4_decompress_fast);
500 EXPORT_SYMBOL(LZ4_setStreamDecode);
501 EXPORT_SYMBOL(LZ4_decompress_safe_continue);
502 EXPORT_SYMBOL(LZ4_decompress_fast_continue);
503 EXPORT_SYMBOL(LZ4_decompress_safe_usingDict);
504 EXPORT_SYMBOL(LZ4_decompress_fast_usingDict);
506 MODULE_LICENSE("Dual BSD/GPL");
507 MODULE_DESCRIPTION("LZ4 decompressor");