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/kernel.h>
39 #include <asm/unaligned.h>
41 /*-*****************************
42 * Decompression functions
43 *******************************/
44 /* LZ4_decompress_generic() :
45 * This generic decompression function cover all use cases.
46 * It shall be instantiated several times, using different sets of directives
47 * Note that it is important this generic function is really inlined,
48 * in order to remove useless branches during compilation optimization.
50 static FORCE_INLINE int LZ4_decompress_generic(
51 const char * const source,
55 * If endOnInput == endOnInputSize,
56 * this value is the max size of Output Buffer.
59 /* endOnOutputSize, endOnInputSize */
63 /* only used if partialDecoding == partial */
65 /* noDict, withPrefix64k, usingExtDict */
67 /* == dest when no prefix */
68 const BYTE * const lowPrefix,
69 /* only if dict == usingExtDict */
70 const BYTE * const dictStart,
71 /* note : = 0 if noDict */
76 const BYTE *ip = (const BYTE *) source;
77 const BYTE * const iend = ip + inputSize;
79 BYTE *op = (BYTE *) dest;
80 BYTE * const oend = op + outputSize;
82 BYTE *oexit = op + targetOutputSize;
83 const BYTE * const lowLimit = lowPrefix - dictSize;
85 const BYTE * const dictEnd = (const BYTE *)dictStart + dictSize;
86 const unsigned int dec32table[] = { 0, 1, 2, 1, 4, 4, 4, 4 };
87 const int dec64table[] = { 0, 0, 0, -1, 0, 1, 2, 3 };
89 const int safeDecode = (endOnInput == endOnInputSize);
90 const int checkOffset = ((safeDecode) && (dictSize < (int)(64 * KB)));
93 /* targetOutputSize too high => decode everything */
94 if ((partialDecoding) && (oexit > oend - MFLIMIT))
95 oexit = oend - MFLIMIT;
97 /* Empty output buffer */
98 if ((endOnInput) && (unlikely(outputSize == 0)))
99 return ((inputSize == 1) && (*ip == 0)) ? 0 : -1;
101 if ((!endOnInput) && (unlikely(outputSize == 0)))
102 return (*ip == 0 ? 1 : -1);
104 /* Main Loop : decode sequences */
110 /* get literal length */
111 unsigned int const token = *ip++;
113 length = token>>ML_BITS;
115 if (length == RUN_MASK) {
121 } while (likely(endOnInput
122 ? ip < iend - RUN_MASK
127 (size_t)(op + length) < (size_t)(op))) {
128 /* overflow detection */
133 (size_t)(ip + length) < (size_t)(ip))) {
134 /* overflow detection */
141 if (((endOnInput) && ((cpy > (partialDecoding ? oexit : oend - MFLIMIT))
142 || (ip + length > iend - (2 + 1 + LASTLITERALS))))
143 || ((!endOnInput) && (cpy > oend - WILDCOPYLENGTH))) {
144 if (partialDecoding) {
148 * write attempt beyond end of output buffer
153 && (ip + length > iend)) {
156 * read attempt beyond
157 * end of input buffer
166 * block decoding must
172 && ((ip + length != iend)
176 * input must be consumed
182 memcpy(op, ip, length);
185 /* Necessarily EOF, due to parsing restrictions */
189 LZ4_wildCopy(op, ip, cpy);
194 offset = LZ4_readLE16(ip);
198 if ((checkOffset) && (unlikely(match < lowLimit))) {
199 /* Error : offset outside buffers */
203 /* costs ~1%; silence an msan warning when offset == 0 */
204 LZ4_write32(op, (U32)offset);
206 /* get matchlength */
207 length = token & ML_MASK;
208 if (length == ML_MASK) {
214 if ((endOnInput) && (ip > iend - LASTLITERALS))
222 (size_t)(op + length) < (size_t)op)) {
223 /* overflow detection */
230 /* check external dictionary */
231 if ((dict == usingExtDict) && (match < lowPrefix)) {
232 if (unlikely(op + length > oend - LASTLITERALS)) {
233 /* doesn't respect parsing restriction */
237 if (length <= (size_t)(lowPrefix - match)) {
239 * match can be copied as a single segment
240 * from external dictionary
242 memmove(op, dictEnd - (lowPrefix - match),
247 * match encompass external
248 * dictionary and current block
250 size_t const copySize = (size_t)(lowPrefix - match);
251 size_t const restSize = length - copySize;
253 memcpy(op, dictEnd - copySize, copySize);
256 if (restSize > (size_t)(op - lowPrefix)) {
258 BYTE * const endOfMatch = op + restSize;
259 const BYTE *copyFrom = lowPrefix;
261 while (op < endOfMatch)
264 memcpy(op, lowPrefix, restSize);
272 /* copy match within block */
275 if (unlikely(offset < 8)) {
276 const int dec64 = dec64table[offset];
282 match += dec32table[offset];
283 memcpy(op + 4, match, 4);
286 LZ4_copy8(op, match);
292 if (unlikely(cpy > oend - 12)) {
293 BYTE * const oCopyLimit = oend - (WILDCOPYLENGTH - 1);
295 if (cpy > oend - LASTLITERALS) {
297 * Error : last LASTLITERALS bytes
298 * must be literals (uncompressed)
303 if (op < oCopyLimit) {
304 LZ4_wildCopy(op, match, oCopyLimit);
305 match += oCopyLimit - op;
312 LZ4_copy8(op, match);
315 LZ4_wildCopy(op + 8, match + 8, cpy);
318 op = cpy; /* correction */
321 /* end of decoding */
323 /* Nb of output bytes decoded */
324 return (int) (((char *)op) - dest);
326 /* Nb of input bytes read */
327 return (int) (((const char *)ip) - source);
330 /* Overflow error detected */
335 int LZ4_decompress_safe(const char *source, char *dest,
336 int compressedSize, int maxDecompressedSize)
338 return LZ4_decompress_generic(source, dest, compressedSize,
339 maxDecompressedSize, endOnInputSize, full, 0,
340 noDict, (BYTE *)dest, NULL, 0);
343 int LZ4_decompress_safe_partial(const char *source, char *dest,
344 int compressedSize, int targetOutputSize, int maxDecompressedSize)
346 return LZ4_decompress_generic(source, dest, compressedSize,
347 maxDecompressedSize, endOnInputSize, partial,
348 targetOutputSize, noDict, (BYTE *)dest, NULL, 0);
351 int LZ4_decompress_fast(const char *source, char *dest, int originalSize)
353 return LZ4_decompress_generic(source, dest, 0, originalSize,
354 endOnOutputSize, full, 0, withPrefix64k,
355 (BYTE *)(dest - 64 * KB), NULL, 64 * KB);
358 int LZ4_setStreamDecode(LZ4_streamDecode_t *LZ4_streamDecode,
359 const char *dictionary, int dictSize)
361 LZ4_streamDecode_t_internal *lz4sd = (LZ4_streamDecode_t_internal *) LZ4_streamDecode;
363 lz4sd->prefixSize = (size_t) dictSize;
364 lz4sd->prefixEnd = (const BYTE *) dictionary + dictSize;
365 lz4sd->externalDict = NULL;
366 lz4sd->extDictSize = 0;
372 * These decoding functions allow decompression of multiple blocks
373 * in "streaming" mode.
374 * Previously decoded blocks must still be available at the memory
375 * position where they were decoded.
376 * If it's not possible, save the relevant part of
377 * decoded data into a safe buffer,
378 * and indicate where it stands using LZ4_setStreamDecode()
380 int LZ4_decompress_safe_continue(LZ4_streamDecode_t *LZ4_streamDecode,
381 const char *source, char *dest, int compressedSize, int maxOutputSize)
383 LZ4_streamDecode_t_internal *lz4sd = &LZ4_streamDecode->internal_donotuse;
386 if (lz4sd->prefixEnd == (BYTE *)dest) {
387 result = LZ4_decompress_generic(source, dest,
390 endOnInputSize, full, 0,
391 usingExtDict, lz4sd->prefixEnd - lz4sd->prefixSize,
398 lz4sd->prefixSize += result;
399 lz4sd->prefixEnd += result;
401 lz4sd->extDictSize = lz4sd->prefixSize;
402 lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize;
403 result = LZ4_decompress_generic(source, dest,
404 compressedSize, maxOutputSize,
405 endOnInputSize, full, 0,
406 usingExtDict, (BYTE *)dest,
407 lz4sd->externalDict, lz4sd->extDictSize);
410 lz4sd->prefixSize = result;
411 lz4sd->prefixEnd = (BYTE *)dest + result;
417 int LZ4_decompress_fast_continue(LZ4_streamDecode_t *LZ4_streamDecode,
418 const char *source, char *dest, int originalSize)
420 LZ4_streamDecode_t_internal *lz4sd = &LZ4_streamDecode->internal_donotuse;
423 if (lz4sd->prefixEnd == (BYTE *)dest) {
424 result = LZ4_decompress_generic(source, dest, 0, originalSize,
425 endOnOutputSize, full, 0,
427 lz4sd->prefixEnd - lz4sd->prefixSize,
428 lz4sd->externalDict, lz4sd->extDictSize);
433 lz4sd->prefixSize += originalSize;
434 lz4sd->prefixEnd += originalSize;
436 lz4sd->extDictSize = lz4sd->prefixSize;
437 lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize;
438 result = LZ4_decompress_generic(source, dest, 0, originalSize,
439 endOnOutputSize, full, 0,
440 usingExtDict, (BYTE *)dest,
441 lz4sd->externalDict, lz4sd->extDictSize);
444 lz4sd->prefixSize = originalSize;
445 lz4sd->prefixEnd = (BYTE *)dest + originalSize;
452 * Advanced decoding functions :
454 * These decoding functions work the same as "_continue" ones,
455 * the dictionary must be explicitly provided within parameters
457 static FORCE_INLINE int LZ4_decompress_usingDict_generic(const char *source,
458 char *dest, int compressedSize, int maxOutputSize, int safe,
459 const char *dictStart, int dictSize)
462 return LZ4_decompress_generic(source, dest,
463 compressedSize, maxOutputSize, safe, full, 0,
464 noDict, (BYTE *)dest, NULL, 0);
465 if (dictStart + dictSize == dest) {
466 if (dictSize >= (int)(64 * KB - 1))
467 return LZ4_decompress_generic(source, dest,
468 compressedSize, maxOutputSize, safe, full, 0,
469 withPrefix64k, (BYTE *)dest - 64 * KB, NULL, 0);
470 return LZ4_decompress_generic(source, dest, compressedSize,
471 maxOutputSize, safe, full, 0, noDict,
472 (BYTE *)dest - dictSize, NULL, 0);
474 return LZ4_decompress_generic(source, dest, compressedSize,
475 maxOutputSize, safe, full, 0, usingExtDict,
476 (BYTE *)dest, (const BYTE *)dictStart, dictSize);
479 int LZ4_decompress_safe_usingDict(const char *source, char *dest,
480 int compressedSize, int maxOutputSize,
481 const char *dictStart, int dictSize)
483 return LZ4_decompress_usingDict_generic(source, dest,
484 compressedSize, maxOutputSize, 1, dictStart, dictSize);
487 int LZ4_decompress_fast_usingDict(const char *source, char *dest,
488 int originalSize, const char *dictStart, int dictSize)
490 return LZ4_decompress_usingDict_generic(source, dest, 0,
491 originalSize, 0, dictStart, dictSize);