blob: 4d0b59fa5550f8055a10fe54f8d04f5cc3d7fad3 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001/*
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
7 * met:
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
13 * distribution.
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
28 *
29 * Changed for kernel usage by:
30 * Sven Schmidt <4sschmid@informatik.uni-hamburg.de>
31 */
32
33/*-************************************
34 * Dependencies
35 **************************************/
36#include <linux/lz4.h>
37#include "lz4defs.h"
38#include <linux/init.h>
39#include <linux/module.h>
40#include <linux/kernel.h>
41#include <asm/unaligned.h>
42
43/*-*****************************
44 * Decompression functions
45 *******************************/
David Brazdil0f672f62019-12-10 10:32:29 +000046
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,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000058 * in order to remove useless branches during compilation optimization.
59 */
60static FORCE_INLINE int LZ4_decompress_generic(
David Brazdil0f672f62019-12-10 10:32:29 +000061 const char * const src,
62 char * const dst,
63 int srcSize,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000064 /*
65 * If endOnInput == endOnInputSize,
David Brazdil0f672f62019-12-10 10:32:29 +000066 * this value is `dstCapacity`
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000067 */
68 int outputSize,
69 /* endOnOutputSize, endOnInputSize */
David Brazdil0f672f62019-12-10 10:32:29 +000070 endCondition_directive endOnInput,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000071 /* full, partial */
David Brazdil0f672f62019-12-10 10:32:29 +000072 earlyEnd_directive partialDecoding,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000073 /* noDict, withPrefix64k, usingExtDict */
David Brazdil0f672f62019-12-10 10:32:29 +000074 dict_directive dict,
75 /* always <= dst, == dst when no prefix */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000076 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 )
82{
David Brazdil0f672f62019-12-10 10:32:29 +000083 const BYTE *ip = (const BYTE *) src;
84 const BYTE * const iend = ip + srcSize;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000085
David Brazdil0f672f62019-12-10 10:32:29 +000086 BYTE *op = (BYTE *) dst;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000087 BYTE * const oend = op + outputSize;
88 BYTE *cpy;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000089
90 const BYTE * const dictEnd = (const BYTE *)dictStart + dictSize;
David Brazdil0f672f62019-12-10 10:32:29 +000091 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};
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000093
94 const int safeDecode = (endOnInput == endOnInputSize);
95 const int checkOffset = ((safeDecode) && (dictSize < (int)(64 * KB)));
96
David Brazdil0f672f62019-12-10 10:32:29 +000097 /* 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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000106 /* Special cases */
David Brazdil0f672f62019-12-10 10:32:29 +0000107 assert(lowPrefix <= op);
108 assert(src != NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000109
110 /* Empty output buffer */
111 if ((endOnInput) && (unlikely(outputSize == 0)))
David Brazdil0f672f62019-12-10 10:32:29 +0000112 return ((srcSize == 1) && (*ip == 0)) ? 0 : -1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000113
114 if ((!endOnInput) && (unlikely(outputSize == 0)))
115 return (*ip == 0 ? 1 : -1);
116
David Brazdil0f672f62019-12-10 10:32:29 +0000117 if ((endOnInput) && unlikely(srcSize == 0))
118 return -1;
119
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000120 /* Main Loop : decode sequences */
121 while (1) {
122 size_t length;
123 const BYTE *match;
124 size_t offset;
125
126 /* get literal length */
127 unsigned int const token = *ip++;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000128 length = token>>ML_BITS;
129
David Brazdil0f672f62019-12-10 10:32:29 +0000130 /* 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).
144 */
145 if ((endOnInput ? length != RUN_MASK : length <= 8)
146 /*
147 * strictly "less than" on input, to re-enter
148 * the loop with at least one byte
149 */
150 && likely((endOnInput ? ip < shortiend : 1) &
151 (op <= shortoend))) {
152 /* Copy the literals */
153 memcpy(op, ip, endOnInput ? 16 : 8);
154 op += length; ip += length;
155
156 /*
157 * The second stage:
158 * prepare for match copying, decode full info.
159 * If it doesn't work out, the info won't be wasted.
160 */
161 length = token & ML_MASK; /* match length */
162 offset = LZ4_readLE16(ip);
163 ip += 2;
164 match = op - offset;
165 assert(match <= op); /* check overflow */
166
167 /* Do not deal with overlapping matches. */
168 if ((length != ML_MASK) &&
169 (offset >= 8) &&
170 (dict == withPrefix64k || match >= lowPrefix)) {
171 /* Copy the match. */
172 memcpy(op + 0, match + 0, 8);
173 memcpy(op + 8, match + 8, 8);
174 memcpy(op + 16, match + 16, 2);
175 op += length + MINMATCH;
176 /* Both stages worked, load the next token. */
177 continue;
178 }
179
180 /*
181 * The second stage didn't work out, but the info
182 * is ready. Propel it right to the point of match
183 * copying.
184 */
185 goto _copy_match;
186 }
187
188 /* decode literal length */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000189 if (length == RUN_MASK) {
190 unsigned int s;
191
David Brazdil0f672f62019-12-10 10:32:29 +0000192 if (unlikely(endOnInput ? ip >= iend - RUN_MASK : 0)) {
193 /* overflow detection */
194 goto _output_error;
195 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000196 do {
197 s = *ip++;
198 length += s;
199 } while (likely(endOnInput
200 ? ip < iend - RUN_MASK
201 : 1) & (s == 255));
202
203 if ((safeDecode)
David Brazdil0f672f62019-12-10 10:32:29 +0000204 && unlikely((uptrval)(op) +
205 length < (uptrval)(op))) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000206 /* overflow detection */
207 goto _output_error;
208 }
209 if ((safeDecode)
David Brazdil0f672f62019-12-10 10:32:29 +0000210 && unlikely((uptrval)(ip) +
211 length < (uptrval)(ip))) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000212 /* overflow detection */
213 goto _output_error;
214 }
215 }
216
217 /* copy literals */
218 cpy = op + length;
David Brazdil0f672f62019-12-10 10:32:29 +0000219 LZ4_STATIC_ASSERT(MFLIMIT >= WILDCOPYLENGTH);
220
221 if (((endOnInput) && ((cpy > oend - MFLIMIT)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000222 || (ip + length > iend - (2 + 1 + LASTLITERALS))))
223 || ((!endOnInput) && (cpy > oend - WILDCOPYLENGTH))) {
224 if (partialDecoding) {
225 if (cpy > oend) {
226 /*
David Brazdil0f672f62019-12-10 10:32:29 +0000227 * Partial decoding :
228 * stop in the middle of literal segment
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000229 */
David Brazdil0f672f62019-12-10 10:32:29 +0000230 cpy = oend;
231 length = oend - op;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000232 }
233 if ((endOnInput)
234 && (ip + length > iend)) {
235 /*
236 * Error :
237 * read attempt beyond
238 * end of input buffer
239 */
240 goto _output_error;
241 }
242 } else {
243 if ((!endOnInput)
244 && (cpy != oend)) {
245 /*
246 * Error :
247 * block decoding must
248 * stop exactly there
249 */
250 goto _output_error;
251 }
252 if ((endOnInput)
253 && ((ip + length != iend)
254 || (cpy > oend))) {
255 /*
256 * Error :
257 * input must be consumed
258 */
259 goto _output_error;
260 }
261 }
262
Olivier Deprez0e641232021-09-23 10:07:05 +0200263 /*
264 * supports overlapping memory regions; only matters
265 * for in-place decompression scenarios
266 */
267 LZ4_memmove(op, ip, length);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000268 ip += length;
269 op += length;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000270
David Brazdil0f672f62019-12-10 10:32:29 +0000271 /* Necessarily EOF, due to parsing restrictions */
272 if (!partialDecoding || (cpy == oend))
273 break;
274 } else {
275 /* may overwrite up to WILDCOPYLENGTH beyond cpy */
276 LZ4_wildCopy(op, ip, cpy);
277 ip += length;
278 op = cpy;
279 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000280
281 /* get offset */
282 offset = LZ4_readLE16(ip);
283 ip += 2;
284 match = op - offset;
285
David Brazdil0f672f62019-12-10 10:32:29 +0000286 /* get matchlength */
287 length = token & ML_MASK;
288
289_copy_match:
290 if ((checkOffset) && (unlikely(match + dictSize < lowPrefix))) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000291 /* Error : offset outside buffers */
292 goto _output_error;
293 }
294
295 /* costs ~1%; silence an msan warning when offset == 0 */
David Brazdil0f672f62019-12-10 10:32:29 +0000296 /*
297 * note : when partialDecoding, there is no guarantee that
298 * at least 4 bytes remain available in output buffer
299 */
300 if (!partialDecoding) {
301 assert(oend > op);
302 assert(oend - op >= 4);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000303
David Brazdil0f672f62019-12-10 10:32:29 +0000304 LZ4_write32(op, (U32)offset);
305 }
306
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000307 if (length == ML_MASK) {
308 unsigned int s;
309
310 do {
311 s = *ip++;
312
313 if ((endOnInput) && (ip > iend - LASTLITERALS))
314 goto _output_error;
315
316 length += s;
317 } while (s == 255);
318
319 if ((safeDecode)
320 && unlikely(
David Brazdil0f672f62019-12-10 10:32:29 +0000321 (uptrval)(op) + length < (uptrval)op)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000322 /* overflow detection */
323 goto _output_error;
324 }
325 }
326
327 length += MINMATCH;
328
David Brazdil0f672f62019-12-10 10:32:29 +0000329 /* match starting within external dictionary */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000330 if ((dict == usingExtDict) && (match < lowPrefix)) {
331 if (unlikely(op + length > oend - LASTLITERALS)) {
332 /* doesn't respect parsing restriction */
David Brazdil0f672f62019-12-10 10:32:29 +0000333 if (!partialDecoding)
334 goto _output_error;
335 length = min(length, (size_t)(oend - op));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000336 }
337
338 if (length <= (size_t)(lowPrefix - match)) {
339 /*
David Brazdil0f672f62019-12-10 10:32:29 +0000340 * match fits entirely within external
341 * dictionary : just copy
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000342 */
343 memmove(op, dictEnd - (lowPrefix - match),
344 length);
345 op += length;
346 } else {
347 /*
David Brazdil0f672f62019-12-10 10:32:29 +0000348 * match stretches into both external
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000349 * dictionary and current block
350 */
351 size_t const copySize = (size_t)(lowPrefix - match);
352 size_t const restSize = length - copySize;
353
354 memcpy(op, dictEnd - copySize, copySize);
355 op += copySize;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000356 if (restSize > (size_t)(op - lowPrefix)) {
357 /* overlap copy */
358 BYTE * const endOfMatch = op + restSize;
359 const BYTE *copyFrom = lowPrefix;
360
361 while (op < endOfMatch)
362 *op++ = *copyFrom++;
363 } else {
364 memcpy(op, lowPrefix, restSize);
365 op += restSize;
366 }
367 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000368 continue;
369 }
370
371 /* copy match within block */
372 cpy = op + length;
373
David Brazdil0f672f62019-12-10 10:32:29 +0000374 /*
375 * partialDecoding :
376 * may not respect endBlock parsing restrictions
377 */
378 assert(op <= oend);
379 if (partialDecoding &&
380 (cpy > oend - MATCH_SAFEGUARD_DISTANCE)) {
381 size_t const mlen = min(length, (size_t)(oend - op));
382 const BYTE * const matchEnd = match + mlen;
383 BYTE * const copyEnd = op + mlen;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000384
David Brazdil0f672f62019-12-10 10:32:29 +0000385 if (matchEnd > op) {
386 /* overlap copy */
387 while (op < copyEnd)
388 *op++ = *match++;
389 } else {
390 memcpy(op, match, mlen);
391 }
392 op = copyEnd;
393 if (op == oend)
394 break;
395 continue;
396 }
397
398 if (unlikely(offset < 8)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000399 op[0] = match[0];
400 op[1] = match[1];
401 op[2] = match[2];
402 op[3] = match[3];
David Brazdil0f672f62019-12-10 10:32:29 +0000403 match += inc32table[offset];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000404 memcpy(op + 4, match, 4);
David Brazdil0f672f62019-12-10 10:32:29 +0000405 match -= dec64table[offset];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000406 } else {
407 LZ4_copy8(op, match);
408 match += 8;
409 }
410
411 op += 8;
412
David Brazdil0f672f62019-12-10 10:32:29 +0000413 if (unlikely(cpy > oend - MATCH_SAFEGUARD_DISTANCE)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000414 BYTE * const oCopyLimit = oend - (WILDCOPYLENGTH - 1);
415
416 if (cpy > oend - LASTLITERALS) {
417 /*
418 * Error : last LASTLITERALS bytes
419 * must be literals (uncompressed)
420 */
421 goto _output_error;
422 }
423
424 if (op < oCopyLimit) {
425 LZ4_wildCopy(op, match, oCopyLimit);
426 match += oCopyLimit - op;
427 op = oCopyLimit;
428 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000429 while (op < cpy)
430 *op++ = *match++;
431 } else {
432 LZ4_copy8(op, match);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000433 if (length > 16)
434 LZ4_wildCopy(op + 8, match + 8, cpy);
435 }
David Brazdil0f672f62019-12-10 10:32:29 +0000436 op = cpy; /* wildcopy correction */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000437 }
438
439 /* end of decoding */
440 if (endOnInput) {
441 /* Nb of output bytes decoded */
David Brazdil0f672f62019-12-10 10:32:29 +0000442 return (int) (((char *)op) - dst);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000443 } else {
444 /* Nb of input bytes read */
David Brazdil0f672f62019-12-10 10:32:29 +0000445 return (int) (((const char *)ip) - src);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000446 }
447
448 /* Overflow error detected */
449_output_error:
David Brazdil0f672f62019-12-10 10:32:29 +0000450 return (int) (-(((const char *)ip) - src)) - 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000451}
452
453int LZ4_decompress_safe(const char *source, char *dest,
454 int compressedSize, int maxDecompressedSize)
455{
David Brazdil0f672f62019-12-10 10:32:29 +0000456 return LZ4_decompress_generic(source, dest,
457 compressedSize, maxDecompressedSize,
458 endOnInputSize, decode_full_block,
459 noDict, (BYTE *)dest, NULL, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000460}
461
David Brazdil0f672f62019-12-10 10:32:29 +0000462int LZ4_decompress_safe_partial(const char *src, char *dst,
463 int compressedSize, int targetOutputSize, int dstCapacity)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000464{
David Brazdil0f672f62019-12-10 10:32:29 +0000465 dstCapacity = min(targetOutputSize, dstCapacity);
466 return LZ4_decompress_generic(src, dst, compressedSize, dstCapacity,
467 endOnInputSize, partial_decode,
468 noDict, (BYTE *)dst, NULL, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000469}
470
471int LZ4_decompress_fast(const char *source, char *dest, int originalSize)
472{
473 return LZ4_decompress_generic(source, dest, 0, originalSize,
David Brazdil0f672f62019-12-10 10:32:29 +0000474 endOnOutputSize, decode_full_block,
475 withPrefix64k,
476 (BYTE *)dest - 64 * KB, NULL, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000477}
478
David Brazdil0f672f62019-12-10 10:32:29 +0000479/* ===== Instantiate a few more decoding cases, used more than once. ===== */
480
481int LZ4_decompress_safe_withPrefix64k(const char *source, char *dest,
482 int compressedSize, int maxOutputSize)
483{
484 return LZ4_decompress_generic(source, dest,
485 compressedSize, maxOutputSize,
486 endOnInputSize, decode_full_block,
487 withPrefix64k,
488 (BYTE *)dest - 64 * KB, NULL, 0);
489}
490
491static int LZ4_decompress_safe_withSmallPrefix(const char *source, char *dest,
492 int compressedSize,
493 int maxOutputSize,
494 size_t prefixSize)
495{
496 return LZ4_decompress_generic(source, dest,
497 compressedSize, maxOutputSize,
498 endOnInputSize, decode_full_block,
499 noDict,
500 (BYTE *)dest - prefixSize, NULL, 0);
501}
502
503int LZ4_decompress_safe_forceExtDict(const char *source, char *dest,
504 int compressedSize, int maxOutputSize,
505 const void *dictStart, size_t dictSize)
506{
507 return LZ4_decompress_generic(source, dest,
508 compressedSize, maxOutputSize,
509 endOnInputSize, decode_full_block,
510 usingExtDict, (BYTE *)dest,
511 (const BYTE *)dictStart, dictSize);
512}
513
514static int LZ4_decompress_fast_extDict(const char *source, char *dest,
515 int originalSize,
516 const void *dictStart, size_t dictSize)
517{
518 return LZ4_decompress_generic(source, dest,
519 0, originalSize,
520 endOnOutputSize, decode_full_block,
521 usingExtDict, (BYTE *)dest,
522 (const BYTE *)dictStart, dictSize);
523}
524
525/*
526 * The "double dictionary" mode, for use with e.g. ring buffers: the first part
527 * of the dictionary is passed as prefix, and the second via dictStart + dictSize.
528 * These routines are used only once, in LZ4_decompress_*_continue().
529 */
530static FORCE_INLINE
531int LZ4_decompress_safe_doubleDict(const char *source, char *dest,
532 int compressedSize, int maxOutputSize,
533 size_t prefixSize,
534 const void *dictStart, size_t dictSize)
535{
536 return LZ4_decompress_generic(source, dest,
537 compressedSize, maxOutputSize,
538 endOnInputSize, decode_full_block,
539 usingExtDict, (BYTE *)dest - prefixSize,
540 (const BYTE *)dictStart, dictSize);
541}
542
543static FORCE_INLINE
544int LZ4_decompress_fast_doubleDict(const char *source, char *dest,
545 int originalSize, size_t prefixSize,
546 const void *dictStart, size_t dictSize)
547{
548 return LZ4_decompress_generic(source, dest,
549 0, originalSize,
550 endOnOutputSize, decode_full_block,
551 usingExtDict, (BYTE *)dest - prefixSize,
552 (const BYTE *)dictStart, dictSize);
553}
554
555/* ===== streaming decompression functions ===== */
556
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000557int LZ4_setStreamDecode(LZ4_streamDecode_t *LZ4_streamDecode,
558 const char *dictionary, int dictSize)
559{
David Brazdil0f672f62019-12-10 10:32:29 +0000560 LZ4_streamDecode_t_internal *lz4sd =
561 &LZ4_streamDecode->internal_donotuse;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000562
563 lz4sd->prefixSize = (size_t) dictSize;
564 lz4sd->prefixEnd = (const BYTE *) dictionary + dictSize;
565 lz4sd->externalDict = NULL;
566 lz4sd->extDictSize = 0;
567 return 1;
568}
569
570/*
571 * *_continue() :
572 * These decoding functions allow decompression of multiple blocks
573 * in "streaming" mode.
574 * Previously decoded blocks must still be available at the memory
575 * position where they were decoded.
576 * If it's not possible, save the relevant part of
577 * decoded data into a safe buffer,
578 * and indicate where it stands using LZ4_setStreamDecode()
579 */
580int LZ4_decompress_safe_continue(LZ4_streamDecode_t *LZ4_streamDecode,
581 const char *source, char *dest, int compressedSize, int maxOutputSize)
582{
David Brazdil0f672f62019-12-10 10:32:29 +0000583 LZ4_streamDecode_t_internal *lz4sd =
584 &LZ4_streamDecode->internal_donotuse;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000585 int result;
586
David Brazdil0f672f62019-12-10 10:32:29 +0000587 if (lz4sd->prefixSize == 0) {
588 /* The first call, no dictionary yet. */
589 assert(lz4sd->extDictSize == 0);
590 result = LZ4_decompress_safe(source, dest,
591 compressedSize, maxOutputSize);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000592 if (result <= 0)
593 return result;
David Brazdil0f672f62019-12-10 10:32:29 +0000594 lz4sd->prefixSize = result;
595 lz4sd->prefixEnd = (BYTE *)dest + result;
596 } else if (lz4sd->prefixEnd == (BYTE *)dest) {
597 /* They're rolling the current segment. */
598 if (lz4sd->prefixSize >= 64 * KB - 1)
599 result = LZ4_decompress_safe_withPrefix64k(source, dest,
600 compressedSize, maxOutputSize);
601 else if (lz4sd->extDictSize == 0)
602 result = LZ4_decompress_safe_withSmallPrefix(source,
603 dest, compressedSize, maxOutputSize,
604 lz4sd->prefixSize);
605 else
606 result = LZ4_decompress_safe_doubleDict(source, dest,
607 compressedSize, maxOutputSize,
608 lz4sd->prefixSize,
609 lz4sd->externalDict, lz4sd->extDictSize);
610 if (result <= 0)
611 return result;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000612 lz4sd->prefixSize += result;
David Brazdil0f672f62019-12-10 10:32:29 +0000613 lz4sd->prefixEnd += result;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000614 } else {
David Brazdil0f672f62019-12-10 10:32:29 +0000615 /*
616 * The buffer wraps around, or they're
617 * switching to another buffer.
618 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000619 lz4sd->extDictSize = lz4sd->prefixSize;
620 lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize;
David Brazdil0f672f62019-12-10 10:32:29 +0000621 result = LZ4_decompress_safe_forceExtDict(source, dest,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000622 compressedSize, maxOutputSize,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000623 lz4sd->externalDict, lz4sd->extDictSize);
624 if (result <= 0)
625 return result;
626 lz4sd->prefixSize = result;
David Brazdil0f672f62019-12-10 10:32:29 +0000627 lz4sd->prefixEnd = (BYTE *)dest + result;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000628 }
629
630 return result;
631}
632
633int LZ4_decompress_fast_continue(LZ4_streamDecode_t *LZ4_streamDecode,
634 const char *source, char *dest, int originalSize)
635{
636 LZ4_streamDecode_t_internal *lz4sd = &LZ4_streamDecode->internal_donotuse;
637 int result;
638
David Brazdil0f672f62019-12-10 10:32:29 +0000639 if (lz4sd->prefixSize == 0) {
640 assert(lz4sd->extDictSize == 0);
641 result = LZ4_decompress_fast(source, dest, originalSize);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000642 if (result <= 0)
643 return result;
644 lz4sd->prefixSize = originalSize;
David Brazdil0f672f62019-12-10 10:32:29 +0000645 lz4sd->prefixEnd = (BYTE *)dest + originalSize;
646 } else if (lz4sd->prefixEnd == (BYTE *)dest) {
647 if (lz4sd->prefixSize >= 64 * KB - 1 ||
648 lz4sd->extDictSize == 0)
649 result = LZ4_decompress_fast(source, dest,
650 originalSize);
651 else
652 result = LZ4_decompress_fast_doubleDict(source, dest,
653 originalSize, lz4sd->prefixSize,
654 lz4sd->externalDict, lz4sd->extDictSize);
655 if (result <= 0)
656 return result;
657 lz4sd->prefixSize += originalSize;
658 lz4sd->prefixEnd += originalSize;
659 } else {
660 lz4sd->extDictSize = lz4sd->prefixSize;
661 lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize;
662 result = LZ4_decompress_fast_extDict(source, dest,
663 originalSize, lz4sd->externalDict, lz4sd->extDictSize);
664 if (result <= 0)
665 return result;
666 lz4sd->prefixSize = originalSize;
667 lz4sd->prefixEnd = (BYTE *)dest + originalSize;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000668 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000669 return result;
670}
671
David Brazdil0f672f62019-12-10 10:32:29 +0000672int LZ4_decompress_safe_usingDict(const char *source, char *dest,
673 int compressedSize, int maxOutputSize,
674 const char *dictStart, int dictSize)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000675{
676 if (dictSize == 0)
David Brazdil0f672f62019-12-10 10:32:29 +0000677 return LZ4_decompress_safe(source, dest,
678 compressedSize, maxOutputSize);
679 if (dictStart+dictSize == dest) {
680 if (dictSize >= 64 * KB - 1)
681 return LZ4_decompress_safe_withPrefix64k(source, dest,
682 compressedSize, maxOutputSize);
683 return LZ4_decompress_safe_withSmallPrefix(source, dest,
684 compressedSize, maxOutputSize, dictSize);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000685 }
David Brazdil0f672f62019-12-10 10:32:29 +0000686 return LZ4_decompress_safe_forceExtDict(source, dest,
687 compressedSize, maxOutputSize, dictStart, dictSize);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000688}
689
690int LZ4_decompress_fast_usingDict(const char *source, char *dest,
David Brazdil0f672f62019-12-10 10:32:29 +0000691 int originalSize,
692 const char *dictStart, int dictSize)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000693{
David Brazdil0f672f62019-12-10 10:32:29 +0000694 if (dictSize == 0 || dictStart + dictSize == dest)
695 return LZ4_decompress_fast(source, dest, originalSize);
696
697 return LZ4_decompress_fast_extDict(source, dest, originalSize,
698 dictStart, dictSize);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000699}
700
701#ifndef STATIC
702EXPORT_SYMBOL(LZ4_decompress_safe);
703EXPORT_SYMBOL(LZ4_decompress_safe_partial);
704EXPORT_SYMBOL(LZ4_decompress_fast);
705EXPORT_SYMBOL(LZ4_setStreamDecode);
706EXPORT_SYMBOL(LZ4_decompress_safe_continue);
707EXPORT_SYMBOL(LZ4_decompress_fast_continue);
708EXPORT_SYMBOL(LZ4_decompress_safe_usingDict);
709EXPORT_SYMBOL(LZ4_decompress_fast_usingDict);
710
711MODULE_LICENSE("Dual BSD/GPL");
712MODULE_DESCRIPTION("LZ4 decompressor");
713#endif