blob: 00cb0d0b73e165a92ab1fe55e9ba1c6855f1520e [file] [log] [blame]
Kyungsik Leecffb78b2013-07-08 16:01:45 -07001/*
Kyungsik Leecffb78b2013-07-08 16:01:45 -07002 * LZ4 - Fast LZ compression algorithm
Sven Schmidt4e1a33b2017-02-24 15:01:12 -08003 * Copyright (C) 2011 - 2016, Yann Collet.
4 * BSD 2 - Clause License (http://www.opensource.org/licenses/bsd - license.php)
Kyungsik Leecffb78b2013-07-08 16:01:45 -07005 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
Sven Schmidt4e1a33b2017-02-24 15:01:12 -08008 * * 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
Kyungsik Leecffb78b2013-07-08 16:01:45 -070011 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
Kyungsik Leecffb78b2013-07-08 16:01:45 -070014 * 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.
Sven Schmidt4e1a33b2017-02-24 15:01:12 -080025 * You can contact the author at :
26 * - LZ4 homepage : http://www.lz4.org
27 * - LZ4 source repository : https://github.com/lz4/lz4
Kyungsik Leecffb78b2013-07-08 16:01:45 -070028 *
Sven Schmidt4e1a33b2017-02-24 15:01:12 -080029 * Changed for kernel usage by:
30 * Sven Schmidt <4sschmid@informatik.uni-hamburg.de>
Kyungsik Leecffb78b2013-07-08 16:01:45 -070031 */
32
Sven Schmidt4e1a33b2017-02-24 15:01:12 -080033/*-************************************
34 * Dependencies
35 **************************************/
36#include <linux/lz4.h>
37#include "lz4defs.h"
38#include <linux/init.h>
Kyungsik Leecffb78b2013-07-08 16:01:45 -070039#include <linux/module.h>
40#include <linux/kernel.h>
Kyungsik Leecffb78b2013-07-08 16:01:45 -070041#include <asm/unaligned.h>
42
Sven Schmidt4e1a33b2017-02-24 15:01:12 -080043/*-*****************************
44 * Decompression functions
45 *******************************/
Gao Xiang2209fda2018-10-30 15:07:28 -070046
47#define DEBUGLOG(l, ...) {} /* disabled */
48
49#ifndef assert
50#define assert(condition) ((void)0)
51#endif
52
53/*
54 * LZ4_decompress_generic() :
55 * This generic decompression function covers all use cases.
56 * It shall be instantiated several times, using different sets of directives.
57 * Note that it is important for performance that this function really get inlined,
Sven Schmidt4e1a33b2017-02-24 15:01:12 -080058 * in order to remove useless branches during compilation optimization.
59 */
60static FORCE_INLINE int LZ4_decompress_generic(
Gao Xiang2209fda2018-10-30 15:07:28 -070061 const char * const src,
62 char * const dst,
63 int srcSize,
Sven Schmidt4e1a33b2017-02-24 15:01:12 -080064 /*
65 * If endOnInput == endOnInputSize,
Gao Xiang2209fda2018-10-30 15:07:28 -070066 * this value is `dstCapacity`
Sven Schmidt4e1a33b2017-02-24 15:01:12 -080067 */
68 int outputSize,
69 /* endOnOutputSize, endOnInputSize */
Gao Xiang2209fda2018-10-30 15:07:28 -070070 endCondition_directive endOnInput,
Sven Schmidt4e1a33b2017-02-24 15:01:12 -080071 /* full, partial */
Gao Xiang2209fda2018-10-30 15:07:28 -070072 earlyEnd_directive partialDecoding,
Sven Schmidt4e1a33b2017-02-24 15:01:12 -080073 /* noDict, withPrefix64k, usingExtDict */
Gao Xiang2209fda2018-10-30 15:07:28 -070074 dict_directive dict,
75 /* always <= dst, == dst when no prefix */
Sven Schmidt4e1a33b2017-02-24 15:01:12 -080076 const BYTE * const lowPrefix,
77 /* only if dict == usingExtDict */
78 const BYTE * const dictStart,
79 /* note : = 0 if noDict */
80 const size_t dictSize
81 )
Kyungsik Leecffb78b2013-07-08 16:01:45 -070082{
Gao Xiang2209fda2018-10-30 15:07:28 -070083 const BYTE *ip = (const BYTE *) src;
84 const BYTE * const iend = ip + srcSize;
Sven Schmidt4e1a33b2017-02-24 15:01:12 -080085
Gao Xiang2209fda2018-10-30 15:07:28 -070086 BYTE *op = (BYTE *) dst;
Sven Schmidt4e1a33b2017-02-24 15:01:12 -080087 BYTE * const oend = op + outputSize;
Kyungsik Leecffb78b2013-07-08 16:01:45 -070088 BYTE *cpy;
Kyungsik Leecffb78b2013-07-08 16:01:45 -070089
Sven Schmidt4e1a33b2017-02-24 15:01:12 -080090 const BYTE * const dictEnd = (const BYTE *)dictStart + dictSize;
Gao Xiang2209fda2018-10-30 15:07:28 -070091 static const unsigned int inc32table[8] = {0, 1, 2, 1, 0, 4, 4, 4};
92 static const int dec64table[8] = {0, 0, 0, -1, -4, 1, 2, 3};
Sven Schmidt4e1a33b2017-02-24 15:01:12 -080093
94 const int safeDecode = (endOnInput == endOnInputSize);
95 const int checkOffset = ((safeDecode) && (dictSize < (int)(64 * KB)));
96
Gao Xiang2209fda2018-10-30 15:07:28 -070097 /* Set up the "end" pointers for the shortcut. */
98 const BYTE *const shortiend = iend -
99 (endOnInput ? 14 : 8) /*maxLL*/ - 2 /*offset*/;
100 const BYTE *const shortoend = oend -
101 (endOnInput ? 14 : 8) /*maxLL*/ - 18 /*maxML*/;
102
103 DEBUGLOG(5, "%s (srcSize:%i, dstSize:%i)", __func__,
104 srcSize, outputSize);
105
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800106 /* Special cases */
Gao Xiang2209fda2018-10-30 15:07:28 -0700107 assert(lowPrefix <= op);
108 assert(src != NULL);
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800109
110 /* Empty output buffer */
111 if ((endOnInput) && (unlikely(outputSize == 0)))
Gao Xiang2209fda2018-10-30 15:07:28 -0700112 return ((srcSize == 1) && (*ip == 0)) ? 0 : -1;
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800113
114 if ((!endOnInput) && (unlikely(outputSize == 0)))
115 return (*ip == 0 ? 1 : -1);
116
Gao Xiang2209fda2018-10-30 15:07:28 -0700117 if ((endOnInput) && unlikely(srcSize == 0))
118 return -1;
119
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800120 /* Main Loop : decode sequences */
Kyungsik Leecffb78b2013-07-08 16:01:45 -0700121 while (1) {
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800122 size_t length;
123 const BYTE *match;
124 size_t offset;
Kyungsik Leecffb78b2013-07-08 16:01:45 -0700125
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800126 /* get literal length */
127 unsigned int const token = *ip++;
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800128 length = token>>ML_BITS;
129
Gao Xiang2209fda2018-10-30 15:07:28 -0700130 /* ip < iend before the increment */
131 assert(!endOnInput || ip <= iend);
132
133 /*
134 * A two-stage shortcut for the most common case:
135 * 1) If the literal length is 0..14, and there is enough
136 * space, enter the shortcut and copy 16 bytes on behalf
137 * of the literals (in the fast mode, only 8 bytes can be
138 * safely copied this way).
139 * 2) Further if the match length is 4..18, copy 18 bytes
140 * in a similar manner; but we ensure that there's enough
141 * space in the output for those 18 bytes earlier, upon
142 * entering the shortcut (in other words, there is a
143 * combined check for both stages).
Joe Perchese8ec0492020-06-10 18:41:32 -0700144 *
145 * The & in the likely() below is intentionally not && so that
146 * some compilers can produce better parallelized runtime code
Gao Xiang2209fda2018-10-30 15:07:28 -0700147 */
148 if ((endOnInput ? length != RUN_MASK : length <= 8)
149 /*
150 * strictly "less than" on input, to re-enter
151 * the loop with at least one byte
152 */
153 && likely((endOnInput ? ip < shortiend : 1) &
154 (op <= shortoend))) {
155 /* Copy the literals */
Nick Terrellb1a3e752020-08-14 17:30:10 -0700156 LZ4_memcpy(op, ip, endOnInput ? 16 : 8);
Gao Xiang2209fda2018-10-30 15:07:28 -0700157 op += length; ip += length;
158
159 /*
160 * The second stage:
161 * prepare for match copying, decode full info.
162 * If it doesn't work out, the info won't be wasted.
163 */
164 length = token & ML_MASK; /* match length */
165 offset = LZ4_readLE16(ip);
166 ip += 2;
167 match = op - offset;
168 assert(match <= op); /* check overflow */
169
170 /* Do not deal with overlapping matches. */
171 if ((length != ML_MASK) &&
172 (offset >= 8) &&
173 (dict == withPrefix64k || match >= lowPrefix)) {
174 /* Copy the match. */
Nick Terrellb1a3e752020-08-14 17:30:10 -0700175 LZ4_memcpy(op + 0, match + 0, 8);
176 LZ4_memcpy(op + 8, match + 8, 8);
177 LZ4_memcpy(op + 16, match + 16, 2);
Gao Xiang2209fda2018-10-30 15:07:28 -0700178 op += length + MINMATCH;
179 /* Both stages worked, load the next token. */
180 continue;
181 }
182
183 /*
184 * The second stage didn't work out, but the info
185 * is ready. Propel it right to the point of match
186 * copying.
187 */
188 goto _copy_match;
189 }
190
191 /* decode literal length */
Kyungsik Leecffb78b2013-07-08 16:01:45 -0700192 if (length == RUN_MASK) {
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800193 unsigned int s;
Kyungsik Leecffb78b2013-07-08 16:01:45 -0700194
Gao Xiang2209fda2018-10-30 15:07:28 -0700195 if (unlikely(endOnInput ? ip >= iend - RUN_MASK : 0)) {
196 /* overflow detection */
197 goto _output_error;
198 }
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800199 do {
200 s = *ip++;
201 length += s;
202 } while (likely(endOnInput
203 ? ip < iend - RUN_MASK
204 : 1) & (s == 255));
205
206 if ((safeDecode)
Gao Xiang2209fda2018-10-30 15:07:28 -0700207 && unlikely((uptrval)(op) +
208 length < (uptrval)(op))) {
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800209 /* overflow detection */
Greg Kroah-Hartman206204a2014-06-20 22:01:41 -0700210 goto _output_error;
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800211 }
212 if ((safeDecode)
Gao Xiang2209fda2018-10-30 15:07:28 -0700213 && unlikely((uptrval)(ip) +
214 length < (uptrval)(ip))) {
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800215 /* overflow detection */
216 goto _output_error;
217 }
Kyungsik Leecffb78b2013-07-08 16:01:45 -0700218 }
219
220 /* copy literals */
221 cpy = op + length;
Gao Xiang2209fda2018-10-30 15:07:28 -0700222 LZ4_STATIC_ASSERT(MFLIMIT >= WILDCOPYLENGTH);
223
224 if (((endOnInput) && ((cpy > oend - MFLIMIT)
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800225 || (ip + length > iend - (2 + 1 + LASTLITERALS))))
226 || ((!endOnInput) && (cpy > oend - WILDCOPYLENGTH))) {
227 if (partialDecoding) {
228 if (cpy > oend) {
229 /*
Gao Xiang2209fda2018-10-30 15:07:28 -0700230 * Partial decoding :
231 * stop in the middle of literal segment
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800232 */
Gao Xiang2209fda2018-10-30 15:07:28 -0700233 cpy = oend;
234 length = oend - op;
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800235 }
236 if ((endOnInput)
237 && (ip + length > iend)) {
238 /*
239 * Error :
240 * read attempt beyond
241 * end of input buffer
242 */
243 goto _output_error;
244 }
245 } else {
246 if ((!endOnInput)
247 && (cpy != oend)) {
248 /*
249 * Error :
250 * block decoding must
251 * stop exactly there
252 */
253 goto _output_error;
254 }
255 if ((endOnInput)
256 && ((ip + length != iend)
257 || (cpy > oend))) {
258 /*
259 * Error :
260 * input must be consumed
261 */
262 goto _output_error;
263 }
264 }
Kyungsik Leecffb78b2013-07-08 16:01:45 -0700265
Nick Terrellb1a3e752020-08-14 17:30:10 -0700266 LZ4_memcpy(op, ip, length);
Kyungsik Leecffb78b2013-07-08 16:01:45 -0700267 ip += length;
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800268 op += length;
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800269
Gao Xiang2209fda2018-10-30 15:07:28 -0700270 /* Necessarily EOF, due to parsing restrictions */
271 if (!partialDecoding || (cpy == oend))
272 break;
273 } else {
274 /* may overwrite up to WILDCOPYLENGTH beyond cpy */
275 LZ4_wildCopy(op, ip, cpy);
276 ip += length;
277 op = cpy;
278 }
Kyungsik Leecffb78b2013-07-08 16:01:45 -0700279
280 /* get offset */
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800281 offset = LZ4_readLE16(ip);
Kyungsik Leecffb78b2013-07-08 16:01:45 -0700282 ip += 2;
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800283 match = op - offset;
Kyungsik Leecffb78b2013-07-08 16:01:45 -0700284
Gao Xiang2209fda2018-10-30 15:07:28 -0700285 /* get matchlength */
286 length = token & ML_MASK;
287
288_copy_match:
289 if ((checkOffset) && (unlikely(match + dictSize < lowPrefix))) {
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800290 /* Error : offset outside buffers */
Kyungsik Leecffb78b2013-07-08 16:01:45 -0700291 goto _output_error;
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800292 }
293
294 /* costs ~1%; silence an msan warning when offset == 0 */
Gao Xiang2209fda2018-10-30 15:07:28 -0700295 /*
296 * note : when partialDecoding, there is no guarantee that
297 * at least 4 bytes remain available in output buffer
298 */
299 if (!partialDecoding) {
300 assert(oend > op);
301 assert(oend - op >= 4);
Kyungsik Leecffb78b2013-07-08 16:01:45 -0700302
Gao Xiang2209fda2018-10-30 15:07:28 -0700303 LZ4_write32(op, (U32)offset);
304 }
305
Kyungsik Leecffb78b2013-07-08 16:01:45 -0700306 if (length == ML_MASK) {
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800307 unsigned int s;
308
309 do {
310 s = *ip++;
311
312 if ((endOnInput) && (ip > iend - LASTLITERALS))
313 goto _output_error;
314
315 length += s;
316 } while (s == 255);
317
318 if ((safeDecode)
319 && unlikely(
Gao Xiang2209fda2018-10-30 15:07:28 -0700320 (uptrval)(op) + length < (uptrval)op)) {
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800321 /* overflow detection */
Greg Kroah-Hartman4148c1f2014-06-24 16:59:01 -0400322 goto _output_error;
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800323 }
Kyungsik Leecffb78b2013-07-08 16:01:45 -0700324 }
325
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800326 length += MINMATCH;
Kyungsik Leecffb78b2013-07-08 16:01:45 -0700327
Gao Xiang2209fda2018-10-30 15:07:28 -0700328 /* match starting within external dictionary */
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800329 if ((dict == usingExtDict) && (match < lowPrefix)) {
330 if (unlikely(op + length > oend - LASTLITERALS)) {
331 /* doesn't respect parsing restriction */
Gao Xiang2209fda2018-10-30 15:07:28 -0700332 if (!partialDecoding)
333 goto _output_error;
334 length = min(length, (size_t)(oend - op));
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800335 }
336
337 if (length <= (size_t)(lowPrefix - match)) {
338 /*
Gao Xiang2209fda2018-10-30 15:07:28 -0700339 * match fits entirely within external
340 * dictionary : just copy
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800341 */
342 memmove(op, dictEnd - (lowPrefix - match),
343 length);
344 op += length;
345 } else {
346 /*
Gao Xiang2209fda2018-10-30 15:07:28 -0700347 * match stretches into both external
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800348 * dictionary and current block
349 */
350 size_t const copySize = (size_t)(lowPrefix - match);
351 size_t const restSize = length - copySize;
352
Nick Terrellb1a3e752020-08-14 17:30:10 -0700353 LZ4_memcpy(op, dictEnd - copySize, copySize);
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800354 op += copySize;
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800355 if (restSize > (size_t)(op - lowPrefix)) {
356 /* overlap copy */
357 BYTE * const endOfMatch = op + restSize;
358 const BYTE *copyFrom = lowPrefix;
359
360 while (op < endOfMatch)
361 *op++ = *copyFrom++;
362 } else {
Nick Terrellb1a3e752020-08-14 17:30:10 -0700363 LZ4_memcpy(op, lowPrefix, restSize);
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800364 op += restSize;
365 }
366 }
Kyungsik Leecffb78b2013-07-08 16:01:45 -0700367 continue;
368 }
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800369
370 /* copy match within block */
371 cpy = op + length;
372
Gao Xiang2209fda2018-10-30 15:07:28 -0700373 /*
374 * partialDecoding :
375 * may not respect endBlock parsing restrictions
376 */
377 assert(op <= oend);
378 if (partialDecoding &&
379 (cpy > oend - MATCH_SAFEGUARD_DISTANCE)) {
380 size_t const mlen = min(length, (size_t)(oend - op));
381 const BYTE * const matchEnd = match + mlen;
382 BYTE * const copyEnd = op + mlen;
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800383
Gao Xiang2209fda2018-10-30 15:07:28 -0700384 if (matchEnd > op) {
385 /* overlap copy */
386 while (op < copyEnd)
387 *op++ = *match++;
388 } else {
Nick Terrellb1a3e752020-08-14 17:30:10 -0700389 LZ4_memcpy(op, match, mlen);
Gao Xiang2209fda2018-10-30 15:07:28 -0700390 }
391 op = copyEnd;
392 if (op == oend)
393 break;
394 continue;
395 }
396
397 if (unlikely(offset < 8)) {
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800398 op[0] = match[0];
399 op[1] = match[1];
400 op[2] = match[2];
401 op[3] = match[3];
Gao Xiang2209fda2018-10-30 15:07:28 -0700402 match += inc32table[offset];
Nick Terrellb1a3e752020-08-14 17:30:10 -0700403 LZ4_memcpy(op + 4, match, 4);
Gao Xiang2209fda2018-10-30 15:07:28 -0700404 match -= dec64table[offset];
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800405 } else {
406 LZ4_copy8(op, match);
407 match += 8;
408 }
409
410 op += 8;
411
Gao Xiang2209fda2018-10-30 15:07:28 -0700412 if (unlikely(cpy > oend - MATCH_SAFEGUARD_DISTANCE)) {
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800413 BYTE * const oCopyLimit = oend - (WILDCOPYLENGTH - 1);
414
415 if (cpy > oend - LASTLITERALS) {
416 /*
417 * Error : last LASTLITERALS bytes
418 * must be literals (uncompressed)
419 */
420 goto _output_error;
421 }
422
423 if (op < oCopyLimit) {
424 LZ4_wildCopy(op, match, oCopyLimit);
425 match += oCopyLimit - op;
426 op = oCopyLimit;
427 }
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800428 while (op < cpy)
429 *op++ = *match++;
430 } else {
431 LZ4_copy8(op, match);
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800432 if (length > 16)
433 LZ4_wildCopy(op + 8, match + 8, cpy);
434 }
Gao Xiang2209fda2018-10-30 15:07:28 -0700435 op = cpy; /* wildcopy correction */
Kyungsik Leecffb78b2013-07-08 16:01:45 -0700436 }
Kyungsik Leecffb78b2013-07-08 16:01:45 -0700437
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800438 /* end of decoding */
439 if (endOnInput) {
440 /* Nb of output bytes decoded */
Gao Xiang2209fda2018-10-30 15:07:28 -0700441 return (int) (((char *)op) - dst);
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800442 } else {
443 /* Nb of input bytes read */
Gao Xiang2209fda2018-10-30 15:07:28 -0700444 return (int) (((const char *)ip) - src);
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800445 }
446
447 /* Overflow error detected */
Kyungsik Leecffb78b2013-07-08 16:01:45 -0700448_output_error:
Gao Xiang2209fda2018-10-30 15:07:28 -0700449 return (int) (-(((const char *)ip) - src)) - 1;
Kyungsik Leecffb78b2013-07-08 16:01:45 -0700450}
451
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800452int LZ4_decompress_safe(const char *source, char *dest,
453 int compressedSize, int maxDecompressedSize)
Kyungsik Leecffb78b2013-07-08 16:01:45 -0700454{
Gao Xiang2209fda2018-10-30 15:07:28 -0700455 return LZ4_decompress_generic(source, dest,
456 compressedSize, maxDecompressedSize,
457 endOnInputSize, decode_full_block,
458 noDict, (BYTE *)dest, NULL, 0);
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800459}
Kyungsik Leecffb78b2013-07-08 16:01:45 -0700460
Gao Xiang2209fda2018-10-30 15:07:28 -0700461int LZ4_decompress_safe_partial(const char *src, char *dst,
462 int compressedSize, int targetOutputSize, int dstCapacity)
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800463{
Gao Xiang2209fda2018-10-30 15:07:28 -0700464 dstCapacity = min(targetOutputSize, dstCapacity);
465 return LZ4_decompress_generic(src, dst, compressedSize, dstCapacity,
466 endOnInputSize, partial_decode,
467 noDict, (BYTE *)dst, NULL, 0);
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800468}
Kyungsik Leecffb78b2013-07-08 16:01:45 -0700469
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800470int LZ4_decompress_fast(const char *source, char *dest, int originalSize)
471{
472 return LZ4_decompress_generic(source, dest, 0, originalSize,
Gao Xiang2209fda2018-10-30 15:07:28 -0700473 endOnOutputSize, decode_full_block,
474 withPrefix64k,
475 (BYTE *)dest - 64 * KB, NULL, 0);
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800476}
Kyungsik Leecffb78b2013-07-08 16:01:45 -0700477
Gao Xiang2209fda2018-10-30 15:07:28 -0700478/* ===== Instantiate a few more decoding cases, used more than once. ===== */
479
480int LZ4_decompress_safe_withPrefix64k(const char *source, char *dest,
481 int compressedSize, int maxOutputSize)
482{
483 return LZ4_decompress_generic(source, dest,
484 compressedSize, maxOutputSize,
485 endOnInputSize, decode_full_block,
486 withPrefix64k,
487 (BYTE *)dest - 64 * KB, NULL, 0);
488}
489
490static int LZ4_decompress_safe_withSmallPrefix(const char *source, char *dest,
491 int compressedSize,
492 int maxOutputSize,
493 size_t prefixSize)
494{
495 return LZ4_decompress_generic(source, dest,
496 compressedSize, maxOutputSize,
497 endOnInputSize, decode_full_block,
498 noDict,
499 (BYTE *)dest - prefixSize, NULL, 0);
500}
501
502int LZ4_decompress_safe_forceExtDict(const char *source, char *dest,
503 int compressedSize, int maxOutputSize,
504 const void *dictStart, size_t dictSize)
505{
506 return LZ4_decompress_generic(source, dest,
507 compressedSize, maxOutputSize,
508 endOnInputSize, decode_full_block,
509 usingExtDict, (BYTE *)dest,
510 (const BYTE *)dictStart, dictSize);
511}
512
513static int LZ4_decompress_fast_extDict(const char *source, char *dest,
514 int originalSize,
515 const void *dictStart, size_t dictSize)
516{
517 return LZ4_decompress_generic(source, dest,
518 0, originalSize,
519 endOnOutputSize, decode_full_block,
520 usingExtDict, (BYTE *)dest,
521 (const BYTE *)dictStart, dictSize);
522}
523
524/*
525 * The "double dictionary" mode, for use with e.g. ring buffers: the first part
526 * of the dictionary is passed as prefix, and the second via dictStart + dictSize.
527 * These routines are used only once, in LZ4_decompress_*_continue().
528 */
529static FORCE_INLINE
530int LZ4_decompress_safe_doubleDict(const char *source, char *dest,
531 int compressedSize, int maxOutputSize,
532 size_t prefixSize,
533 const void *dictStart, size_t dictSize)
534{
535 return LZ4_decompress_generic(source, dest,
536 compressedSize, maxOutputSize,
537 endOnInputSize, decode_full_block,
538 usingExtDict, (BYTE *)dest - prefixSize,
539 (const BYTE *)dictStart, dictSize);
540}
541
542static FORCE_INLINE
543int LZ4_decompress_fast_doubleDict(const char *source, char *dest,
544 int originalSize, size_t prefixSize,
545 const void *dictStart, size_t dictSize)
546{
547 return LZ4_decompress_generic(source, dest,
548 0, originalSize,
549 endOnOutputSize, decode_full_block,
550 usingExtDict, (BYTE *)dest - prefixSize,
551 (const BYTE *)dictStart, dictSize);
552}
553
554/* ===== streaming decompression functions ===== */
555
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800556int LZ4_setStreamDecode(LZ4_streamDecode_t *LZ4_streamDecode,
557 const char *dictionary, int dictSize)
558{
Gao Xiang2209fda2018-10-30 15:07:28 -0700559 LZ4_streamDecode_t_internal *lz4sd =
560 &LZ4_streamDecode->internal_donotuse;
Kyungsik Leecffb78b2013-07-08 16:01:45 -0700561
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800562 lz4sd->prefixSize = (size_t) dictSize;
563 lz4sd->prefixEnd = (const BYTE *) dictionary + dictSize;
564 lz4sd->externalDict = NULL;
565 lz4sd->extDictSize = 0;
566 return 1;
567}
Kyungsik Leecffb78b2013-07-08 16:01:45 -0700568
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800569/*
570 * *_continue() :
571 * These decoding functions allow decompression of multiple blocks
572 * in "streaming" mode.
573 * Previously decoded blocks must still be available at the memory
574 * position where they were decoded.
575 * If it's not possible, save the relevant part of
576 * decoded data into a safe buffer,
577 * and indicate where it stands using LZ4_setStreamDecode()
578 */
579int LZ4_decompress_safe_continue(LZ4_streamDecode_t *LZ4_streamDecode,
580 const char *source, char *dest, int compressedSize, int maxOutputSize)
581{
Gao Xiang2209fda2018-10-30 15:07:28 -0700582 LZ4_streamDecode_t_internal *lz4sd =
583 &LZ4_streamDecode->internal_donotuse;
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800584 int result;
Kyungsik Leecffb78b2013-07-08 16:01:45 -0700585
Gao Xiang2209fda2018-10-30 15:07:28 -0700586 if (lz4sd->prefixSize == 0) {
587 /* The first call, no dictionary yet. */
588 assert(lz4sd->extDictSize == 0);
589 result = LZ4_decompress_safe(source, dest,
590 compressedSize, maxOutputSize);
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800591 if (result <= 0)
592 return result;
Gao Xiang2209fda2018-10-30 15:07:28 -0700593 lz4sd->prefixSize = result;
594 lz4sd->prefixEnd = (BYTE *)dest + result;
595 } else if (lz4sd->prefixEnd == (BYTE *)dest) {
596 /* They're rolling the current segment. */
597 if (lz4sd->prefixSize >= 64 * KB - 1)
598 result = LZ4_decompress_safe_withPrefix64k(source, dest,
599 compressedSize, maxOutputSize);
600 else if (lz4sd->extDictSize == 0)
601 result = LZ4_decompress_safe_withSmallPrefix(source,
602 dest, compressedSize, maxOutputSize,
603 lz4sd->prefixSize);
604 else
605 result = LZ4_decompress_safe_doubleDict(source, dest,
606 compressedSize, maxOutputSize,
607 lz4sd->prefixSize,
608 lz4sd->externalDict, lz4sd->extDictSize);
609 if (result <= 0)
610 return result;
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800611 lz4sd->prefixSize += result;
Gao Xiang2209fda2018-10-30 15:07:28 -0700612 lz4sd->prefixEnd += result;
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800613 } else {
Gao Xiang2209fda2018-10-30 15:07:28 -0700614 /*
615 * The buffer wraps around, or they're
616 * switching to another buffer.
617 */
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800618 lz4sd->extDictSize = lz4sd->prefixSize;
619 lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize;
Gao Xiang2209fda2018-10-30 15:07:28 -0700620 result = LZ4_decompress_safe_forceExtDict(source, dest,
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800621 compressedSize, maxOutputSize,
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800622 lz4sd->externalDict, lz4sd->extDictSize);
623 if (result <= 0)
624 return result;
625 lz4sd->prefixSize = result;
Gao Xiang2209fda2018-10-30 15:07:28 -0700626 lz4sd->prefixEnd = (BYTE *)dest + result;
Kyungsik Leecffb78b2013-07-08 16:01:45 -0700627 }
Kyungsik Leecffb78b2013-07-08 16:01:45 -0700628
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800629 return result;
630}
631
632int LZ4_decompress_fast_continue(LZ4_streamDecode_t *LZ4_streamDecode,
633 const char *source, char *dest, int originalSize)
634{
635 LZ4_streamDecode_t_internal *lz4sd = &LZ4_streamDecode->internal_donotuse;
636 int result;
637
Gao Xiang2209fda2018-10-30 15:07:28 -0700638 if (lz4sd->prefixSize == 0) {
639 assert(lz4sd->extDictSize == 0);
640 result = LZ4_decompress_fast(source, dest, originalSize);
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800641 if (result <= 0)
642 return result;
643 lz4sd->prefixSize = originalSize;
Gao Xiang2209fda2018-10-30 15:07:28 -0700644 lz4sd->prefixEnd = (BYTE *)dest + originalSize;
645 } else if (lz4sd->prefixEnd == (BYTE *)dest) {
646 if (lz4sd->prefixSize >= 64 * KB - 1 ||
647 lz4sd->extDictSize == 0)
648 result = LZ4_decompress_fast(source, dest,
649 originalSize);
650 else
651 result = LZ4_decompress_fast_doubleDict(source, dest,
652 originalSize, lz4sd->prefixSize,
653 lz4sd->externalDict, lz4sd->extDictSize);
654 if (result <= 0)
655 return result;
656 lz4sd->prefixSize += originalSize;
657 lz4sd->prefixEnd += originalSize;
658 } else {
659 lz4sd->extDictSize = lz4sd->prefixSize;
660 lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize;
661 result = LZ4_decompress_fast_extDict(source, dest,
662 originalSize, lz4sd->externalDict, lz4sd->extDictSize);
663 if (result <= 0)
664 return result;
665 lz4sd->prefixSize = originalSize;
666 lz4sd->prefixEnd = (BYTE *)dest + originalSize;
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800667 }
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800668 return result;
669}
670
Gao Xiang2209fda2018-10-30 15:07:28 -0700671int LZ4_decompress_safe_usingDict(const char *source, char *dest,
672 int compressedSize, int maxOutputSize,
673 const char *dictStart, int dictSize)
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800674{
675 if (dictSize == 0)
Gao Xiang2209fda2018-10-30 15:07:28 -0700676 return LZ4_decompress_safe(source, dest,
677 compressedSize, maxOutputSize);
678 if (dictStart+dictSize == dest) {
679 if (dictSize >= 64 * KB - 1)
680 return LZ4_decompress_safe_withPrefix64k(source, dest,
681 compressedSize, maxOutputSize);
682 return LZ4_decompress_safe_withSmallPrefix(source, dest,
683 compressedSize, maxOutputSize, dictSize);
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800684 }
Gao Xiang2209fda2018-10-30 15:07:28 -0700685 return LZ4_decompress_safe_forceExtDict(source, dest,
686 compressedSize, maxOutputSize, dictStart, dictSize);
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800687}
688
689int LZ4_decompress_fast_usingDict(const char *source, char *dest,
Gao Xiang2209fda2018-10-30 15:07:28 -0700690 int originalSize,
691 const char *dictStart, int dictSize)
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800692{
Gao Xiang2209fda2018-10-30 15:07:28 -0700693 if (dictSize == 0 || dictStart + dictSize == dest)
694 return LZ4_decompress_fast(source, dest, originalSize);
695
696 return LZ4_decompress_fast_extDict(source, dest, originalSize,
697 dictStart, dictSize);
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800698}
699
Kyungsik Leecffb78b2013-07-08 16:01:45 -0700700#ifndef STATIC
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800701EXPORT_SYMBOL(LZ4_decompress_safe);
702EXPORT_SYMBOL(LZ4_decompress_safe_partial);
703EXPORT_SYMBOL(LZ4_decompress_fast);
704EXPORT_SYMBOL(LZ4_setStreamDecode);
705EXPORT_SYMBOL(LZ4_decompress_safe_continue);
706EXPORT_SYMBOL(LZ4_decompress_fast_continue);
707EXPORT_SYMBOL(LZ4_decompress_safe_usingDict);
708EXPORT_SYMBOL(LZ4_decompress_fast_usingDict);
Kyungsik Leecffb78b2013-07-08 16:01:45 -0700709
Richard Laageree8a99b2013-08-22 16:35:47 -0700710MODULE_LICENSE("Dual BSD/GPL");
Sven Schmidt4e1a33b2017-02-24 15:01:12 -0800711MODULE_DESCRIPTION("LZ4 decompressor");
Kyungsik Leecffb78b2013-07-08 16:01:45 -0700712#endif