| 1 |
/* |
| 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 <[email protected]> |
| 31 |
*/ |
| 32 |
|
| 33 |
/*-************************************ |
| 34 |
* Dependencies |
| 35 |
**************************************/ |
| 36 |
#include "lz4defs.h" |
| 37 |
#include <linux/init.h> |
| 38 |
#include <linux/module.h> |
| 39 |
#include <linux/kernel.h> |
| 40 |
#include <linux/unaligned.h> |
| 41 |
|
| 42 |
/*-***************************** |
| 43 |
* Decompression functions |
| 44 |
*******************************/ |
| 45 |
|
| 46 |
#define DEBUGLOG(l, ...) {} /* disabled */ |
| 47 |
|
| 48 |
#ifndef assert |
| 49 |
#define assert(condition) ((void)0) |
| 50 |
#endif |
| 51 |
|
| 52 |
/* |
| 53 |
* LZ4_decompress_generic() : |
| 54 |
* This generic decompression function covers all use cases. |
| 55 |
* It shall be instantiated several times, using different sets of directives. |
| 56 |
* Note that it is important for performance that this function really get inlined, |
| 57 |
* in order to remove useless branches during compilation optimization. |
| 58 |
*/ |
| 59 |
static FORCE_INLINE int LZ4_decompress_generic( |
| 60 |
const char * const src, |
| 61 |
char * const dst, |
| 62 |
int srcSize, |
| 63 |
/* |
| 64 |
* If endOnInput == endOnInputSize, |
| 65 |
* this value is `dstCapacity` |
| 66 |
*/ |
| 67 |
int outputSize, |
| 68 |
/* endOnOutputSize, endOnInputSize */ |
| 69 |
endCondition_directive endOnInput, |
| 70 |
/* full, partial */ |
| 71 |
earlyEnd_directive partialDecoding, |
| 72 |
/* noDict, withPrefix64k, usingExtDict */ |
| 73 |
dict_directive dict, |
| 74 |
/* always <= dst, == dst when no prefix */ |
| 75 |
const BYTE * const lowPrefix, |
| 76 |
/* only if dict == usingExtDict */ |
| 77 |
const BYTE * const dictStart, |
| 78 |
/* note : = 0 if noDict */ |
| 79 |
const size_t dictSize |
| 80 |
) |
| 81 |
{ |
| 82 |
const BYTE *ip = (const BYTE *) src; |
| 83 |
const BYTE * const iend = ip + srcSize; |
| 84 |
|
| 85 |
BYTE *op = (BYTE *) dst; |
| 86 |
BYTE * const oend = op + outputSize; |
| 87 |
BYTE *cpy; |
| 88 |
|
| 89 |
const BYTE * const dictEnd = (const BYTE *)dictStart + dictSize; |
| 90 |
static const unsigned int inc32table[8] = {0, 1, 2, 1, 0, 4, 4, 4}; |
| 91 |
static const int dec64table[8] = {0, 0, 0, -1, -4, 1, 2, 3}; |
| 92 |
|
| 93 |
const int safeDecode = (endOnInput == endOnInputSize); |
| 94 |
const int checkOffset = ((safeDecode) && (dictSize < (int)(64 * KB))); |
| 95 |
|
| 96 |
/* Set up the "end" pointers for the shortcut. */ |
| 97 |
const BYTE *const shortiend = iend - |
| 98 |
(endOnInput ? 14 : 8) /*maxLL*/ - 2 /*offset*/; |
| 99 |
const BYTE *const shortoend = oend - |
| 100 |
(endOnInput ? 14 : 8) /*maxLL*/ - 18 /*maxML*/; |
| 101 |
|
| 102 |
DEBUGLOG(5, "%s (srcSize:%i, dstSize:%i)", __func__, |
| 103 |
srcSize, outputSize); |
| 104 |
|
| 105 |
/* Special cases */ |
| 106 |
assert(lowPrefix <= op); |
| 107 |
assert(src != NULL); |
| 108 |
|
| 109 |
/* Empty output buffer */ |
| 110 |
if ((endOnInput) && (unlikely(outputSize == 0))) |
| 111 |
return ((srcSize == 1) && (*ip == 0)) ? 0 : -1; |
| 112 |
|
| 113 |
if ((!endOnInput) && (unlikely(outputSize == 0))) |
| 114 |
return (*ip == 0 ? 1 : -1); |
| 115 |
|
| 116 |
if ((endOnInput) && unlikely(srcSize == 0)) |
| 117 |
return -1; |
| 118 |
|
| 119 |
/* Main Loop : decode sequences */ |
| 120 |
while (1) { |
| 121 |
size_t length; |
| 122 |
const BYTE *match; |
| 123 |
size_t offset; |
| 124 |
|
| 125 |
/* get literal length */ |
| 126 |
unsigned int const token = *ip++; |
| 127 |
length = token>>ML_BITS; |
| 128 |
|
| 129 |
/* ip < iend before the increment */ |
| 130 |
assert(!endOnInput || ip <= iend); |
| 131 |
|
| 132 |
/* |
| 133 |
* A two-stage shortcut for the most common case: |
| 134 |
* 1) If the literal length is 0..14, and there is enough |
| 135 |
* space, enter the shortcut and copy 16 bytes on behalf |
| 136 |
* of the literals (in the fast mode, only 8 bytes can be |
| 137 |
* safely copied this way). |
| 138 |
* 2) Further if the match length is 4..18, copy 18 bytes |
| 139 |
* in a similar manner; but we ensure that there's enough |
| 140 |
* space in the output for those 18 bytes earlier, upon |
| 141 |
* entering the shortcut (in other words, there is a |
| 142 |
* combined check for both stages). |
| 143 |
* |
| 144 |
* The & in the likely() below is intentionally not && so that |
| 145 |
* some compilers can produce better parallelized runtime code |
| 146 |
*/ |
| 147 |
if ((endOnInput ? length != RUN_MASK : length <= 8) |
| 148 |
/* |
| 149 |
* strictly "less than" on input, to re-enter |
| 150 |
* the loop with at least one byte |
| 151 |
*/ |
| 152 |
&& likely((endOnInput ? ip < shortiend : 1) & |
| 153 |
(op <= shortoend))) { |
| 154 |
/* Copy the literals */ |
| 155 |
LZ4_memcpy(op, ip, endOnInput ? 16 : 8); |
| 156 |
op += length; ip += length; |
| 157 |
|
| 158 |
/* |
| 159 |
* The second stage: |
| 160 |
* prepare for match copying, decode full info. |
| 161 |
* If it doesn't work out, the info won't be wasted. |
| 162 |
*/ |
| 163 |
length = token & ML_MASK; /* match length */ |
| 164 |
offset = LZ4_readLE16(ip); |
| 165 |
ip += 2; |
| 166 |
match = op - offset; |
| 167 |
assert(match <= op); /* check overflow */ |
| 168 |
|
| 169 |
/* Do not deal with overlapping matches. */ |
| 170 |
if ((length != ML_MASK) && |
| 171 |
(offset >= 8) && |
| 172 |
(dict == withPrefix64k || match >= lowPrefix)) { |
| 173 |
/* Copy the match. */ |
| 174 |
LZ4_memcpy(op + 0, match + 0, 8); |
| 175 |
LZ4_memcpy(op + 8, match + 8, 8); |
| 176 |
LZ4_memcpy(op + 16, match + 16, 2); |
| 177 |
op += length + MINMATCH; |
| 178 |
/* Both stages worked, load the next token. */ |
| 179 |
continue; |
| 180 |
} |
| 181 |
|
| 182 |
/* |
| 183 |
* The second stage didn't work out, but the info |
| 184 |
* is ready. Propel it right to the point of match |
| 185 |
* copying. |
| 186 |
*/ |
| 187 |
goto _copy_match; |
| 188 |
} |
| 189 |
|
| 190 |
/* decode literal length */ |
| 191 |
if (length == RUN_MASK) { |
| 192 |
unsigned int s; |
| 193 |
|
| 194 |
if (unlikely(endOnInput ? ip >= iend - RUN_MASK : 0)) { |
| 195 |
/* overflow detection */ |
| 196 |
goto _output_error; |
| 197 |
} |
| 198 |
do { |
| 199 |
s = *ip++; |
| 200 |
length += s; |
| 201 |
} while (likely(endOnInput |
| 202 |
? ip < iend - RUN_MASK |
| 203 |
: 1) & (s == 255)); |
| 204 |
|
| 205 |
if ((safeDecode) |
| 206 |
&& unlikely((uptrval)(op) + |
| 207 |
length < (uptrval)(op))) { |
| 208 |
/* overflow detection */ |
| 209 |
goto _output_error; |
| 210 |
} |
| 211 |
if ((safeDecode) |
| 212 |
&& unlikely((uptrval)(ip) + |
| 213 |
length < (uptrval)(ip))) { |
| 214 |
/* overflow detection */ |
| 215 |
goto _output_error; |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
/* copy literals */ |
| 220 |
cpy = op + length; |
| 221 |
LZ4_STATIC_ASSERT(MFLIMIT >= WILDCOPYLENGTH); |
| 222 |
|
| 223 |
if (((endOnInput) && ((cpy > oend - MFLIMIT) |
| 224 |
|| (ip + length > iend - (2 + 1 + LASTLITERALS)))) |
| 225 |
|| ((!endOnInput) && (cpy > oend - WILDCOPYLENGTH))) { |
| 226 |
if (partialDecoding) { |
| 227 |
if (cpy > oend) { |
| 228 |
/* |
| 229 |
* Partial decoding : |
| 230 |
* stop in the middle of literal segment |
| 231 |
*/ |
| 232 |
cpy = oend; |
| 233 |
length = oend - op; |
| 234 |
} |
| 235 |
if ((endOnInput) |
| 236 |
&& (ip + length > iend)) { |
| 237 |
/* |
| 238 |
* Error : |
| 239 |
* read attempt beyond |
| 240 |
* end of input buffer |
| 241 |
*/ |
| 242 |
goto _output_error; |
| 243 |
} |
| 244 |
} else { |
| 245 |
if ((!endOnInput) |
| 246 |
&& (cpy != oend)) { |
| 247 |
/* |
| 248 |
* Error : |
| 249 |
* block decoding must |
| 250 |
* stop exactly there |
| 251 |
*/ |
| 252 |
goto _output_error; |
| 253 |
} |
| 254 |
if ((endOnInput) |
| 255 |
&& ((ip + length != iend) |
| 256 |
|| (cpy > oend))) { |
| 257 |
/* |
| 258 |
* Error : |
| 259 |
* input must be consumed |
| 260 |
*/ |
| 261 |
goto _output_error; |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
/* |
| 266 |
* supports overlapping memory regions; only matters |
| 267 |
* for in-place decompression scenarios |
| 268 |
*/ |
| 269 |
LZ4_memmove(op, ip, length); |
| 270 |
ip += length; |
| 271 |
op += length; |
| 272 |
|
| 273 |
/* Necessarily EOF when !partialDecoding. |
| 274 |
* When partialDecoding, it is EOF if we've either |
| 275 |
* filled the output buffer or |
| 276 |
* can't proceed with reading an offset for following match. |
| 277 |
*/ |
| 278 |
if (!partialDecoding || (cpy == oend) || (ip >= (iend - 2))) |
| 279 |
break; |
| 280 |
} else { |
| 281 |
/* may overwrite up to WILDCOPYLENGTH beyond cpy */ |
| 282 |
LZ4_wildCopy(op, ip, cpy); |
| 283 |
ip += length; |
| 284 |
op = cpy; |
| 285 |
} |
| 286 |
|
| 287 |
/* get offset */ |
| 288 |
offset = LZ4_readLE16(ip); |
| 289 |
ip += 2; |
| 290 |
match = op - offset; |
| 291 |
|
| 292 |
/* get matchlength */ |
| 293 |
length = token & ML_MASK; |
| 294 |
|
| 295 |
_copy_match: |
| 296 |
if ((checkOffset) && (unlikely(match + dictSize < lowPrefix))) { |
| 297 |
/* Error : offset outside buffers */ |
| 298 |
goto _output_error; |
| 299 |
} |
| 300 |
|
| 301 |
/* costs ~1%; silence an msan warning when offset == 0 */ |
| 302 |
/* |
| 303 |
* note : when partialDecoding, there is no guarantee that |
| 304 |
* at least 4 bytes remain available in output buffer |
| 305 |
*/ |
| 306 |
if (!partialDecoding) { |
| 307 |
assert(oend > op); |
| 308 |
assert(oend - op >= 4); |
| 309 |
|
| 310 |
LZ4_write32(op, (U32)offset); |
| 311 |
} |
| 312 |
|
| 313 |
if (length == ML_MASK) { |
| 314 |
unsigned int s; |
| 315 |
|
| 316 |
do { |
| 317 |
s = *ip++; |
| 318 |
|
| 319 |
if ((endOnInput) && (ip > iend - LASTLITERALS)) |
| 320 |
goto _output_error; |
| 321 |
|
| 322 |
length += s; |
| 323 |
} while (s == 255); |
| 324 |
|
| 325 |
if ((safeDecode) |
| 326 |
&& unlikely( |
| 327 |
(uptrval)(op) + length < (uptrval)op)) { |
| 328 |
/* overflow detection */ |
| 329 |
goto _output_error; |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
length += MINMATCH; |
| 334 |
|
| 335 |
/* match starting within external dictionary */ |
| 336 |
if ((dict == usingExtDict) && (match < lowPrefix)) { |
| 337 |
if (unlikely(op + length > oend - LASTLITERALS)) { |
| 338 |
/* doesn't respect parsing restriction */ |
| 339 |
if (!partialDecoding) |
| 340 |
goto _output_error; |
| 341 |
length = min(length, (size_t)(oend - op)); |
| 342 |
} |
| 343 |
|
| 344 |
if (length <= (size_t)(lowPrefix - match)) { |
| 345 |
/* |
| 346 |
* match fits entirely within external |
| 347 |
* dictionary : just copy |
| 348 |
*/ |
| 349 |
memmove(op, dictEnd - (lowPrefix - match), |
| 350 |
length); |
| 351 |
op += length; |
| 352 |
} else { |
| 353 |
/* |
| 354 |
* match stretches into both external |
| 355 |
* dictionary and current block |
| 356 |
*/ |
| 357 |
size_t const copySize = (size_t)(lowPrefix - match); |
| 358 |
size_t const restSize = length - copySize; |
| 359 |
|
| 360 |
LZ4_memcpy(op, dictEnd - copySize, copySize); |
| 361 |
op += copySize; |
| 362 |
if (restSize > (size_t)(op - lowPrefix)) { |
| 363 |
/* overlap copy */ |
| 364 |
BYTE * const endOfMatch = op + restSize; |
| 365 |
const BYTE *copyFrom = lowPrefix; |
| 366 |
|
| 367 |
while (op < endOfMatch) |
| 368 |
*op++ = *copyFrom++; |
| 369 |
} else { |
| 370 |
LZ4_memcpy(op, lowPrefix, restSize); |
| 371 |
op += restSize; |
| 372 |
} |
| 373 |
} |
| 374 |
continue; |
| 375 |
} |
| 376 |
|
| 377 |
/* copy match within block */ |
| 378 |
cpy = op + length; |
| 379 |
|
| 380 |
/* |
| 381 |
* partialDecoding : |
| 382 |
* may not respect endBlock parsing restrictions |
| 383 |
*/ |
| 384 |
assert(op <= oend); |
| 385 |
if (partialDecoding && |
| 386 |
(cpy > oend - MATCH_SAFEGUARD_DISTANCE)) { |
| 387 |
size_t const mlen = min(length, (size_t)(oend - op)); |
| 388 |
const BYTE * const matchEnd = match + mlen; |
| 389 |
BYTE * const copyEnd = op + mlen; |
| 390 |
|
| 391 |
if (matchEnd > op) { |
| 392 |
/* overlap copy */ |
| 393 |
while (op < copyEnd) |
| 394 |
*op++ = *match++; |
| 395 |
} else { |
| 396 |
LZ4_memcpy(op, match, mlen); |
| 397 |
} |
| 398 |
op = copyEnd; |
| 399 |
if (op == oend) |
| 400 |
break; |
| 401 |
continue; |
| 402 |
} |
| 403 |
|
| 404 |
if (unlikely(offset < 8)) { |
| 405 |
op[0] = match[0]; |
| 406 |
op[1] = match[1]; |
| 407 |
op[2] = match[2]; |
| 408 |
op[3] = match[3]; |
| 409 |
match += inc32table[offset]; |
| 410 |
LZ4_memcpy(op + 4, match, 4); |
| 411 |
match -= dec64table[offset]; |
| 412 |
} else { |
| 413 |
LZ4_copy8(op, match); |
| 414 |
match += 8; |
| 415 |
} |
| 416 |
|
| 417 |
op += 8; |
| 418 |
|
| 419 |
if (unlikely(cpy > oend - MATCH_SAFEGUARD_DISTANCE)) { |
| 420 |
BYTE * const oCopyLimit = oend - (WILDCOPYLENGTH - 1); |
| 421 |
|
| 422 |
if (cpy > oend - LASTLITERALS) { |
| 423 |
/* |
| 424 |
* Error : last LASTLITERALS bytes |
| 425 |
* must be literals (uncompressed) |
| 426 |
*/ |
| 427 |
goto _output_error; |
| 428 |
} |
| 429 |
|
| 430 |
if (op < oCopyLimit) { |
| 431 |
LZ4_wildCopy(op, match, oCopyLimit); |
| 432 |
match += oCopyLimit - op; |
| 433 |
op = oCopyLimit; |
| 434 |
} |
| 435 |
while (op < cpy) |
| 436 |
*op++ = *match++; |
| 437 |
} else { |
| 438 |
LZ4_copy8(op, match); |
| 439 |
if (length > 16) |
| 440 |
LZ4_wildCopy(op + 8, match + 8, cpy); |
| 441 |
} |
| 442 |
op = cpy; /* wildcopy correction */ |
| 443 |
} |
| 444 |
|
| 445 |
/* end of decoding */ |
| 446 |
if (endOnInput) { |
| 447 |
/* Nb of output bytes decoded */ |
| 448 |
return (int) (((char *)op) - dst); |
| 449 |
} else { |
| 450 |
/* Nb of input bytes read */ |
| 451 |
return (int) (((const char *)ip) - src); |
| 452 |
} |
| 453 |
|
| 454 |
/* Overflow error detected */ |
| 455 |
_output_error: |
| 456 |
return (int) (-(((const char *)ip) - src)) - 1; |
| 457 |
} |
| 458 |
|
| 459 |
int LZ4_decompress_safe(const char *source, char *dest, |
| 460 |
int compressedSize, int maxDecompressedSize) |
| 461 |
{ |
| 462 |
return LZ4_decompress_generic(source, dest, |
| 463 |
compressedSize, maxDecompressedSize, |
| 464 |
endOnInputSize, decode_full_block, |
| 465 |
noDict, (BYTE *)dest, NULL, 0); |
| 466 |
} |
| 467 |
|
| 468 |
int LZ4_decompress_safe_partial(const char *src, char *dst, |
| 469 |
int compressedSize, int targetOutputSize, int dstCapacity) |
| 470 |
{ |
| 471 |
dstCapacity = min(targetOutputSize, dstCapacity); |
| 472 |
return LZ4_decompress_generic(src, dst, compressedSize, dstCapacity, |
| 473 |
endOnInputSize, partial_decode, |
| 474 |
noDict, (BYTE *)dst, NULL, 0); |
| 475 |
} |
| 476 |
|
| 477 |
int LZ4_decompress_fast(const char *source, char *dest, int originalSize) |
| 478 |
{ |
| 479 |
return LZ4_decompress_generic(source, dest, 0, originalSize, |
| 480 |
endOnOutputSize, decode_full_block, |
| 481 |
withPrefix64k, |
| 482 |
(BYTE *)dest - 64 * KB, NULL, 0); |
| 483 |
} |
| 484 |
|
| 485 |
/* ===== Instantiate a few more decoding cases, used more than once. ===== */ |
| 486 |
|
| 487 |
static int LZ4_decompress_safe_withPrefix64k(const char *source, char *dest, |
| 488 |
int compressedSize, int maxOutputSize) |
| 489 |
{ |
| 490 |
return LZ4_decompress_generic(source, dest, |
| 491 |
compressedSize, maxOutputSize, |
| 492 |
endOnInputSize, decode_full_block, |
| 493 |
withPrefix64k, |
| 494 |
(BYTE *)dest - 64 * KB, NULL, 0); |
| 495 |
} |
| 496 |
|
| 497 |
static int LZ4_decompress_safe_withSmallPrefix(const char *source, char *dest, |
| 498 |
int compressedSize, |
| 499 |
int maxOutputSize, |
| 500 |
size_t prefixSize) |
| 501 |
{ |
| 502 |
return LZ4_decompress_generic(source, dest, |
| 503 |
compressedSize, maxOutputSize, |
| 504 |
endOnInputSize, decode_full_block, |
| 505 |
noDict, |
| 506 |
(BYTE *)dest - prefixSize, NULL, 0); |
| 507 |
} |
| 508 |
|
| 509 |
static int LZ4_decompress_safe_forceExtDict(const char *source, char *dest, |
| 510 |
int compressedSize, int maxOutputSize, |
| 511 |
const void *dictStart, size_t dictSize) |
| 512 |
{ |
| 513 |
return LZ4_decompress_generic(source, dest, |
| 514 |
compressedSize, maxOutputSize, |
| 515 |
endOnInputSize, decode_full_block, |
| 516 |
usingExtDict, (BYTE *)dest, |
| 517 |
(const BYTE *)dictStart, dictSize); |
| 518 |
} |
| 519 |
|
| 520 |
static int LZ4_decompress_fast_extDict(const char *source, char *dest, |
| 521 |
int originalSize, |
| 522 |
const void *dictStart, size_t dictSize) |
| 523 |
{ |
| 524 |
return LZ4_decompress_generic(source, dest, |
| 525 |
0, originalSize, |
| 526 |
endOnOutputSize, decode_full_block, |
| 527 |
usingExtDict, (BYTE *)dest, |
| 528 |
(const BYTE *)dictStart, dictSize); |
| 529 |
} |
| 530 |
|
| 531 |
/* |
| 532 |
* The "double dictionary" mode, for use with e.g. ring buffers: the first part |
| 533 |
* of the dictionary is passed as prefix, and the second via dictStart + dictSize. |
| 534 |
* These routines are used only once, in LZ4_decompress_*_continue(). |
| 535 |
*/ |
| 536 |
static FORCE_INLINE |
| 537 |
int LZ4_decompress_safe_doubleDict(const char *source, char *dest, |
| 538 |
int compressedSize, int maxOutputSize, |
| 539 |
size_t prefixSize, |
| 540 |
const void *dictStart, size_t dictSize) |
| 541 |
{ |
| 542 |
return LZ4_decompress_generic(source, dest, |
| 543 |
compressedSize, maxOutputSize, |
| 544 |
endOnInputSize, decode_full_block, |
| 545 |
usingExtDict, (BYTE *)dest - prefixSize, |
| 546 |
(const BYTE *)dictStart, dictSize); |
| 547 |
} |
| 548 |
|
| 549 |
static FORCE_INLINE |
| 550 |
int LZ4_decompress_fast_doubleDict(const char *source, char *dest, |
| 551 |
int originalSize, size_t prefixSize, |
| 552 |
const void *dictStart, size_t dictSize) |
| 553 |
{ |
| 554 |
return LZ4_decompress_generic(source, dest, |
| 555 |
0, originalSize, |
| 556 |
endOnOutputSize, decode_full_block, |
| 557 |
usingExtDict, (BYTE *)dest - prefixSize, |
| 558 |
(const BYTE *)dictStart, dictSize); |
| 559 |
} |
| 560 |
|
| 561 |
/* ===== streaming decompression functions ===== */ |
| 562 |
|
| 563 |
int LZ4_setStreamDecode(LZ4_streamDecode_t *LZ4_streamDecode, |
| 564 |
const char *dictionary, int dictSize) |
| 565 |
{ |
| 566 |
LZ4_streamDecode_t_internal *lz4sd = |
| 567 |
&LZ4_streamDecode->internal_donotuse; |
| 568 |
|
| 569 |
lz4sd->prefixSize = (size_t) dictSize; |
| 570 |
lz4sd->prefixEnd = (const BYTE *) dictionary + dictSize; |
| 571 |
lz4sd->externalDict = NULL; |
| 572 |
lz4sd->extDictSize = 0; |
| 573 |
return 1; |
| 574 |
} |
| 575 |
|
| 576 |
/* |
| 577 |
* *_continue() : |
| 578 |
* These decoding functions allow decompression of multiple blocks |
| 579 |
* in "streaming" mode. |
| 580 |
* Previously decoded blocks must still be available at the memory |
| 581 |
* position where they were decoded. |
| 582 |
* If it's not possible, save the relevant part of |
| 583 |
* decoded data into a safe buffer, |
| 584 |
* and indicate where it stands using LZ4_setStreamDecode() |
| 585 |
*/ |
| 586 |
int LZ4_decompress_safe_continue(LZ4_streamDecode_t *LZ4_streamDecode, |
| 587 |
const char *source, char *dest, int compressedSize, int maxOutputSize) |
| 588 |
{ |
| 589 |
LZ4_streamDecode_t_internal *lz4sd = |
| 590 |
&LZ4_streamDecode->internal_donotuse; |
| 591 |
int result; |
| 592 |
|
| 593 |
if (lz4sd->prefixSize == 0) { |
| 594 |
/* The first call, no dictionary yet. */ |
| 595 |
assert(lz4sd->extDictSize == 0); |
| 596 |
result = LZ4_decompress_safe(source, dest, |
| 597 |
compressedSize, maxOutputSize); |
| 598 |
if (result <= 0) |
| 599 |
return result; |
| 600 |
lz4sd->prefixSize = result; |
| 601 |
lz4sd->prefixEnd = (BYTE *)dest + result; |
| 602 |
} else if (lz4sd->prefixEnd == (BYTE *)dest) { |
| 603 |
/* They're rolling the current segment. */ |
| 604 |
if (lz4sd->prefixSize >= 64 * KB - 1) |
| 605 |
result = LZ4_decompress_safe_withPrefix64k(source, dest, |
| 606 |
compressedSize, maxOutputSize); |
| 607 |
else if (lz4sd->extDictSize == 0) |
| 608 |
result = LZ4_decompress_safe_withSmallPrefix(source, |
| 609 |
dest, compressedSize, maxOutputSize, |
| 610 |
lz4sd->prefixSize); |
| 611 |
else |
| 612 |
result = LZ4_decompress_safe_doubleDict(source, dest, |
| 613 |
compressedSize, maxOutputSize, |
| 614 |
lz4sd->prefixSize, |
| 615 |
lz4sd->externalDict, lz4sd->extDictSize); |
| 616 |
if (result <= 0) |
| 617 |
return result; |
| 618 |
lz4sd->prefixSize += result; |
| 619 |
lz4sd->prefixEnd += result; |
| 620 |
} else { |
| 621 |
/* |
| 622 |
* The buffer wraps around, or they're |
| 623 |
* switching to another buffer. |
| 624 |
*/ |
| 625 |
lz4sd->extDictSize = lz4sd->prefixSize; |
| 626 |
lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize; |
| 627 |
result = LZ4_decompress_safe_forceExtDict(source, dest, |
| 628 |
compressedSize, maxOutputSize, |
| 629 |
lz4sd->externalDict, lz4sd->extDictSize); |
| 630 |
if (result <= 0) |
| 631 |
return result; |
| 632 |
lz4sd->prefixSize = result; |
| 633 |
lz4sd->prefixEnd = (BYTE *)dest + result; |
| 634 |
} |
| 635 |
|
| 636 |
return result; |
| 637 |
} |
| 638 |
|
| 639 |
int LZ4_decompress_fast_continue(LZ4_streamDecode_t *LZ4_streamDecode, |
| 640 |
const char *source, char *dest, int originalSize) |
| 641 |
{ |
| 642 |
LZ4_streamDecode_t_internal *lz4sd = &LZ4_streamDecode->internal_donotuse; |
| 643 |
int result; |
| 644 |
|
| 645 |
if (lz4sd->prefixSize == 0) { |
| 646 |
assert(lz4sd->extDictSize == 0); |
| 647 |
result = LZ4_decompress_fast(source, dest, originalSize); |
| 648 |
if (result <= 0) |
| 649 |
return result; |
| 650 |
lz4sd->prefixSize = originalSize; |
| 651 |
lz4sd->prefixEnd = (BYTE *)dest + originalSize; |
| 652 |
} else if (lz4sd->prefixEnd == (BYTE *)dest) { |
| 653 |
if (lz4sd->prefixSize >= 64 * KB - 1 || |
| 654 |
lz4sd->extDictSize == 0) |
| 655 |
result = LZ4_decompress_fast(source, dest, |
| 656 |
originalSize); |
| 657 |
else |
| 658 |
result = LZ4_decompress_fast_doubleDict(source, dest, |
| 659 |
originalSize, lz4sd->prefixSize, |
| 660 |
lz4sd->externalDict, lz4sd->extDictSize); |
| 661 |
if (result <= 0) |
| 662 |
return result; |
| 663 |
lz4sd->prefixSize += originalSize; |
| 664 |
lz4sd->prefixEnd += originalSize; |
| 665 |
} else { |
| 666 |
lz4sd->extDictSize = lz4sd->prefixSize; |
| 667 |
lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize; |
| 668 |
result = LZ4_decompress_fast_extDict(source, dest, |
| 669 |
originalSize, lz4sd->externalDict, lz4sd->extDictSize); |
| 670 |
if (result <= 0) |
| 671 |
return result; |
| 672 |
lz4sd->prefixSize = originalSize; |
| 673 |
lz4sd->prefixEnd = (BYTE *)dest + originalSize; |
| 674 |
} |
| 675 |
return result; |
| 676 |
} |
| 677 |
|
| 678 |
int LZ4_decompress_safe_usingDict(const char *source, char *dest, |
| 679 |
int compressedSize, int maxOutputSize, |
| 680 |
const char *dictStart, int dictSize) |
| 681 |
{ |
| 682 |
if (dictSize == 0) |
| 683 |
return LZ4_decompress_safe(source, dest, |
| 684 |
compressedSize, maxOutputSize); |
| 685 |
if (dictStart+dictSize == dest) { |
| 686 |
if (dictSize >= 64 * KB - 1) |
| 687 |
return LZ4_decompress_safe_withPrefix64k(source, dest, |
| 688 |
compressedSize, maxOutputSize); |
| 689 |
return LZ4_decompress_safe_withSmallPrefix(source, dest, |
| 690 |
compressedSize, maxOutputSize, dictSize); |
| 691 |
} |
| 692 |
return LZ4_decompress_safe_forceExtDict(source, dest, |
| 693 |
compressedSize, maxOutputSize, dictStart, dictSize); |
| 694 |
} |
| 695 |
|
| 696 |
int LZ4_decompress_fast_usingDict(const char *source, char *dest, |
| 697 |
int originalSize, |
| 698 |
const char *dictStart, int dictSize) |
| 699 |
{ |
| 700 |
if (dictSize == 0 || dictStart + dictSize == dest) |
| 701 |
return LZ4_decompress_fast(source, dest, originalSize); |
| 702 |
|
| 703 |
return LZ4_decompress_fast_extDict(source, dest, originalSize, |
| 704 |
dictStart, dictSize); |
| 705 |
} |
| 706 |
|
| 707 |
#ifndef STATIC |
| 708 |
EXPORT_SYMBOL(LZ4_decompress_safe); |
| 709 |
EXPORT_SYMBOL(LZ4_decompress_safe_partial); |
| 710 |
EXPORT_SYMBOL(LZ4_decompress_fast); |
| 711 |
EXPORT_SYMBOL(LZ4_setStreamDecode); |
| 712 |
EXPORT_SYMBOL(LZ4_decompress_safe_continue); |
| 713 |
EXPORT_SYMBOL(LZ4_decompress_fast_continue); |
| 714 |
EXPORT_SYMBOL(LZ4_decompress_safe_usingDict); |
| 715 |
EXPORT_SYMBOL(LZ4_decompress_fast_usingDict); |
| 716 |
|
| 717 |
MODULE_LICENSE("Dual BSD/GPL"); |
| 718 |
MODULE_DESCRIPTION("LZ4 decompressor"); |
| 719 |
#endif |