Kyungsik Lee | cffb78b | 2013-07-08 16:01:45 -0700 | [diff] [blame] | 1 | /* |
Kyungsik Lee | cffb78b | 2013-07-08 16:01:45 -0700 | [diff] [blame] | 2 | * LZ4 - Fast LZ compression algorithm |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 3 | * Copyright (C) 2011 - 2016, Yann Collet. |
| 4 | * BSD 2 - Clause License (http://www.opensource.org/licenses/bsd - license.php) |
Kyungsik Lee | cffb78b | 2013-07-08 16:01:45 -0700 | [diff] [blame] | 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are |
| 7 | * met: |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 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 |
Kyungsik Lee | cffb78b | 2013-07-08 16:01:45 -0700 | [diff] [blame] | 11 | * copyright notice, this list of conditions and the following disclaimer |
| 12 | * in the documentation and/or other materials provided with the |
| 13 | * distribution. |
Kyungsik Lee | cffb78b | 2013-07-08 16:01:45 -0700 | [diff] [blame] | 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. |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 25 | * You can contact the author at : |
| 26 | * - LZ4 homepage : http://www.lz4.org |
| 27 | * - LZ4 source repository : https://github.com/lz4/lz4 |
Kyungsik Lee | cffb78b | 2013-07-08 16:01:45 -0700 | [diff] [blame] | 28 | * |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 29 | * Changed for kernel usage by: |
| 30 | * Sven Schmidt <4sschmid@informatik.uni-hamburg.de> |
Kyungsik Lee | cffb78b | 2013-07-08 16:01:45 -0700 | [diff] [blame] | 31 | */ |
| 32 | |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 33 | /*-************************************ |
| 34 | * Dependencies |
| 35 | **************************************/ |
| 36 | #include <linux/lz4.h> |
| 37 | #include "lz4defs.h" |
| 38 | #include <linux/init.h> |
Kyungsik Lee | cffb78b | 2013-07-08 16:01:45 -0700 | [diff] [blame] | 39 | #include <linux/module.h> |
| 40 | #include <linux/kernel.h> |
Kyungsik Lee | cffb78b | 2013-07-08 16:01:45 -0700 | [diff] [blame] | 41 | #include <asm/unaligned.h> |
| 42 | |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 43 | /*-***************************** |
| 44 | * Decompression functions |
| 45 | *******************************/ |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 46 | |
| 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 Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 58 | * in order to remove useless branches during compilation optimization. |
| 59 | */ |
| 60 | static FORCE_INLINE int LZ4_decompress_generic( |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 61 | const char * const src, |
| 62 | char * const dst, |
| 63 | int srcSize, |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 64 | /* |
| 65 | * If endOnInput == endOnInputSize, |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 66 | * this value is `dstCapacity` |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 67 | */ |
| 68 | int outputSize, |
| 69 | /* endOnOutputSize, endOnInputSize */ |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 70 | endCondition_directive endOnInput, |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 71 | /* full, partial */ |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 72 | earlyEnd_directive partialDecoding, |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 73 | /* noDict, withPrefix64k, usingExtDict */ |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 74 | dict_directive dict, |
| 75 | /* always <= dst, == dst when no prefix */ |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 76 | 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 Lee | cffb78b | 2013-07-08 16:01:45 -0700 | [diff] [blame] | 82 | { |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 83 | const BYTE *ip = (const BYTE *) src; |
| 84 | const BYTE * const iend = ip + srcSize; |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 85 | |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 86 | BYTE *op = (BYTE *) dst; |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 87 | BYTE * const oend = op + outputSize; |
Kyungsik Lee | cffb78b | 2013-07-08 16:01:45 -0700 | [diff] [blame] | 88 | BYTE *cpy; |
Kyungsik Lee | cffb78b | 2013-07-08 16:01:45 -0700 | [diff] [blame] | 89 | |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 90 | const BYTE * const dictEnd = (const BYTE *)dictStart + dictSize; |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 91 | 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 Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 93 | |
| 94 | const int safeDecode = (endOnInput == endOnInputSize); |
| 95 | const int checkOffset = ((safeDecode) && (dictSize < (int)(64 * KB))); |
| 96 | |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 97 | /* 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 Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 106 | /* Special cases */ |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 107 | assert(lowPrefix <= op); |
| 108 | assert(src != NULL); |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 109 | |
| 110 | /* Empty output buffer */ |
| 111 | if ((endOnInput) && (unlikely(outputSize == 0))) |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 112 | return ((srcSize == 1) && (*ip == 0)) ? 0 : -1; |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 113 | |
| 114 | if ((!endOnInput) && (unlikely(outputSize == 0))) |
| 115 | return (*ip == 0 ? 1 : -1); |
| 116 | |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 117 | if ((endOnInput) && unlikely(srcSize == 0)) |
| 118 | return -1; |
| 119 | |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 120 | /* Main Loop : decode sequences */ |
Kyungsik Lee | cffb78b | 2013-07-08 16:01:45 -0700 | [diff] [blame] | 121 | while (1) { |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 122 | size_t length; |
| 123 | const BYTE *match; |
| 124 | size_t offset; |
Kyungsik Lee | cffb78b | 2013-07-08 16:01:45 -0700 | [diff] [blame] | 125 | |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 126 | /* get literal length */ |
| 127 | unsigned int const token = *ip++; |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 128 | length = token>>ML_BITS; |
| 129 | |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 130 | /* 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 Perches | e8ec049 | 2020-06-10 18:41:32 -0700 | [diff] [blame] | 144 | * |
| 145 | * The & in the likely() below is intentionally not && so that |
| 146 | * some compilers can produce better parallelized runtime code |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 147 | */ |
| 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 Terrell | b1a3e75 | 2020-08-14 17:30:10 -0700 | [diff] [blame] | 156 | LZ4_memcpy(op, ip, endOnInput ? 16 : 8); |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 157 | 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 Terrell | b1a3e75 | 2020-08-14 17:30:10 -0700 | [diff] [blame] | 175 | LZ4_memcpy(op + 0, match + 0, 8); |
| 176 | LZ4_memcpy(op + 8, match + 8, 8); |
| 177 | LZ4_memcpy(op + 16, match + 16, 2); |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 178 | 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 Lee | cffb78b | 2013-07-08 16:01:45 -0700 | [diff] [blame] | 192 | if (length == RUN_MASK) { |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 193 | unsigned int s; |
Kyungsik Lee | cffb78b | 2013-07-08 16:01:45 -0700 | [diff] [blame] | 194 | |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 195 | if (unlikely(endOnInput ? ip >= iend - RUN_MASK : 0)) { |
| 196 | /* overflow detection */ |
| 197 | goto _output_error; |
| 198 | } |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 199 | 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 Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 207 | && unlikely((uptrval)(op) + |
| 208 | length < (uptrval)(op))) { |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 209 | /* overflow detection */ |
Greg Kroah-Hartman | 206204a | 2014-06-20 22:01:41 -0700 | [diff] [blame] | 210 | goto _output_error; |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 211 | } |
| 212 | if ((safeDecode) |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 213 | && unlikely((uptrval)(ip) + |
| 214 | length < (uptrval)(ip))) { |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 215 | /* overflow detection */ |
| 216 | goto _output_error; |
| 217 | } |
Kyungsik Lee | cffb78b | 2013-07-08 16:01:45 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | /* copy literals */ |
| 221 | cpy = op + length; |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 222 | LZ4_STATIC_ASSERT(MFLIMIT >= WILDCOPYLENGTH); |
| 223 | |
| 224 | if (((endOnInput) && ((cpy > oend - MFLIMIT) |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 225 | || (ip + length > iend - (2 + 1 + LASTLITERALS)))) |
| 226 | || ((!endOnInput) && (cpy > oend - WILDCOPYLENGTH))) { |
| 227 | if (partialDecoding) { |
| 228 | if (cpy > oend) { |
| 229 | /* |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 230 | * Partial decoding : |
| 231 | * stop in the middle of literal segment |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 232 | */ |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 233 | cpy = oend; |
| 234 | length = oend - op; |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 235 | } |
| 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 Lee | cffb78b | 2013-07-08 16:01:45 -0700 | [diff] [blame] | 265 | |
Gao Xiang | 89b1586 | 2020-12-15 20:44:03 -0800 | [diff] [blame] | 266 | /* |
| 267 | * supports overlapping memory regions; only matters |
| 268 | * for in-place decompression scenarios |
| 269 | */ |
| 270 | LZ4_memmove(op, ip, length); |
Kyungsik Lee | cffb78b | 2013-07-08 16:01:45 -0700 | [diff] [blame] | 271 | ip += length; |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 272 | op += length; |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 273 | |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 274 | /* Necessarily EOF, due to parsing restrictions */ |
| 275 | if (!partialDecoding || (cpy == oend)) |
| 276 | break; |
| 277 | } else { |
| 278 | /* may overwrite up to WILDCOPYLENGTH beyond cpy */ |
| 279 | LZ4_wildCopy(op, ip, cpy); |
| 280 | ip += length; |
| 281 | op = cpy; |
| 282 | } |
Kyungsik Lee | cffb78b | 2013-07-08 16:01:45 -0700 | [diff] [blame] | 283 | |
| 284 | /* get offset */ |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 285 | offset = LZ4_readLE16(ip); |
Kyungsik Lee | cffb78b | 2013-07-08 16:01:45 -0700 | [diff] [blame] | 286 | ip += 2; |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 287 | match = op - offset; |
Kyungsik Lee | cffb78b | 2013-07-08 16:01:45 -0700 | [diff] [blame] | 288 | |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 289 | /* get matchlength */ |
| 290 | length = token & ML_MASK; |
| 291 | |
| 292 | _copy_match: |
| 293 | if ((checkOffset) && (unlikely(match + dictSize < lowPrefix))) { |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 294 | /* Error : offset outside buffers */ |
Kyungsik Lee | cffb78b | 2013-07-08 16:01:45 -0700 | [diff] [blame] | 295 | goto _output_error; |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | /* costs ~1%; silence an msan warning when offset == 0 */ |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 299 | /* |
| 300 | * note : when partialDecoding, there is no guarantee that |
| 301 | * at least 4 bytes remain available in output buffer |
| 302 | */ |
| 303 | if (!partialDecoding) { |
| 304 | assert(oend > op); |
| 305 | assert(oend - op >= 4); |
Kyungsik Lee | cffb78b | 2013-07-08 16:01:45 -0700 | [diff] [blame] | 306 | |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 307 | LZ4_write32(op, (U32)offset); |
| 308 | } |
| 309 | |
Kyungsik Lee | cffb78b | 2013-07-08 16:01:45 -0700 | [diff] [blame] | 310 | if (length == ML_MASK) { |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 311 | unsigned int s; |
| 312 | |
| 313 | do { |
| 314 | s = *ip++; |
| 315 | |
| 316 | if ((endOnInput) && (ip > iend - LASTLITERALS)) |
| 317 | goto _output_error; |
| 318 | |
| 319 | length += s; |
| 320 | } while (s == 255); |
| 321 | |
| 322 | if ((safeDecode) |
| 323 | && unlikely( |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 324 | (uptrval)(op) + length < (uptrval)op)) { |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 325 | /* overflow detection */ |
Greg Kroah-Hartman | 4148c1f | 2014-06-24 16:59:01 -0400 | [diff] [blame] | 326 | goto _output_error; |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 327 | } |
Kyungsik Lee | cffb78b | 2013-07-08 16:01:45 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 330 | length += MINMATCH; |
Kyungsik Lee | cffb78b | 2013-07-08 16:01:45 -0700 | [diff] [blame] | 331 | |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 332 | /* match starting within external dictionary */ |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 333 | if ((dict == usingExtDict) && (match < lowPrefix)) { |
| 334 | if (unlikely(op + length > oend - LASTLITERALS)) { |
| 335 | /* doesn't respect parsing restriction */ |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 336 | if (!partialDecoding) |
| 337 | goto _output_error; |
| 338 | length = min(length, (size_t)(oend - op)); |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | if (length <= (size_t)(lowPrefix - match)) { |
| 342 | /* |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 343 | * match fits entirely within external |
| 344 | * dictionary : just copy |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 345 | */ |
| 346 | memmove(op, dictEnd - (lowPrefix - match), |
| 347 | length); |
| 348 | op += length; |
| 349 | } else { |
| 350 | /* |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 351 | * match stretches into both external |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 352 | * dictionary and current block |
| 353 | */ |
| 354 | size_t const copySize = (size_t)(lowPrefix - match); |
| 355 | size_t const restSize = length - copySize; |
| 356 | |
Nick Terrell | b1a3e75 | 2020-08-14 17:30:10 -0700 | [diff] [blame] | 357 | LZ4_memcpy(op, dictEnd - copySize, copySize); |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 358 | op += copySize; |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 359 | if (restSize > (size_t)(op - lowPrefix)) { |
| 360 | /* overlap copy */ |
| 361 | BYTE * const endOfMatch = op + restSize; |
| 362 | const BYTE *copyFrom = lowPrefix; |
| 363 | |
| 364 | while (op < endOfMatch) |
| 365 | *op++ = *copyFrom++; |
| 366 | } else { |
Nick Terrell | b1a3e75 | 2020-08-14 17:30:10 -0700 | [diff] [blame] | 367 | LZ4_memcpy(op, lowPrefix, restSize); |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 368 | op += restSize; |
| 369 | } |
| 370 | } |
Kyungsik Lee | cffb78b | 2013-07-08 16:01:45 -0700 | [diff] [blame] | 371 | continue; |
| 372 | } |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 373 | |
| 374 | /* copy match within block */ |
| 375 | cpy = op + length; |
| 376 | |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 377 | /* |
| 378 | * partialDecoding : |
| 379 | * may not respect endBlock parsing restrictions |
| 380 | */ |
| 381 | assert(op <= oend); |
| 382 | if (partialDecoding && |
| 383 | (cpy > oend - MATCH_SAFEGUARD_DISTANCE)) { |
| 384 | size_t const mlen = min(length, (size_t)(oend - op)); |
| 385 | const BYTE * const matchEnd = match + mlen; |
| 386 | BYTE * const copyEnd = op + mlen; |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 387 | |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 388 | if (matchEnd > op) { |
| 389 | /* overlap copy */ |
| 390 | while (op < copyEnd) |
| 391 | *op++ = *match++; |
| 392 | } else { |
Nick Terrell | b1a3e75 | 2020-08-14 17:30:10 -0700 | [diff] [blame] | 393 | LZ4_memcpy(op, match, mlen); |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 394 | } |
| 395 | op = copyEnd; |
| 396 | if (op == oend) |
| 397 | break; |
| 398 | continue; |
| 399 | } |
| 400 | |
| 401 | if (unlikely(offset < 8)) { |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 402 | op[0] = match[0]; |
| 403 | op[1] = match[1]; |
| 404 | op[2] = match[2]; |
| 405 | op[3] = match[3]; |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 406 | match += inc32table[offset]; |
Nick Terrell | b1a3e75 | 2020-08-14 17:30:10 -0700 | [diff] [blame] | 407 | LZ4_memcpy(op + 4, match, 4); |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 408 | match -= dec64table[offset]; |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 409 | } else { |
| 410 | LZ4_copy8(op, match); |
| 411 | match += 8; |
| 412 | } |
| 413 | |
| 414 | op += 8; |
| 415 | |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 416 | if (unlikely(cpy > oend - MATCH_SAFEGUARD_DISTANCE)) { |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 417 | BYTE * const oCopyLimit = oend - (WILDCOPYLENGTH - 1); |
| 418 | |
| 419 | if (cpy > oend - LASTLITERALS) { |
| 420 | /* |
| 421 | * Error : last LASTLITERALS bytes |
| 422 | * must be literals (uncompressed) |
| 423 | */ |
| 424 | goto _output_error; |
| 425 | } |
| 426 | |
| 427 | if (op < oCopyLimit) { |
| 428 | LZ4_wildCopy(op, match, oCopyLimit); |
| 429 | match += oCopyLimit - op; |
| 430 | op = oCopyLimit; |
| 431 | } |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 432 | while (op < cpy) |
| 433 | *op++ = *match++; |
| 434 | } else { |
| 435 | LZ4_copy8(op, match); |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 436 | if (length > 16) |
| 437 | LZ4_wildCopy(op + 8, match + 8, cpy); |
| 438 | } |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 439 | op = cpy; /* wildcopy correction */ |
Kyungsik Lee | cffb78b | 2013-07-08 16:01:45 -0700 | [diff] [blame] | 440 | } |
Kyungsik Lee | cffb78b | 2013-07-08 16:01:45 -0700 | [diff] [blame] | 441 | |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 442 | /* end of decoding */ |
| 443 | if (endOnInput) { |
| 444 | /* Nb of output bytes decoded */ |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 445 | return (int) (((char *)op) - dst); |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 446 | } else { |
| 447 | /* Nb of input bytes read */ |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 448 | return (int) (((const char *)ip) - src); |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | /* Overflow error detected */ |
Kyungsik Lee | cffb78b | 2013-07-08 16:01:45 -0700 | [diff] [blame] | 452 | _output_error: |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 453 | return (int) (-(((const char *)ip) - src)) - 1; |
Kyungsik Lee | cffb78b | 2013-07-08 16:01:45 -0700 | [diff] [blame] | 454 | } |
| 455 | |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 456 | int LZ4_decompress_safe(const char *source, char *dest, |
| 457 | int compressedSize, int maxDecompressedSize) |
Kyungsik Lee | cffb78b | 2013-07-08 16:01:45 -0700 | [diff] [blame] | 458 | { |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 459 | return LZ4_decompress_generic(source, dest, |
| 460 | compressedSize, maxDecompressedSize, |
| 461 | endOnInputSize, decode_full_block, |
| 462 | noDict, (BYTE *)dest, NULL, 0); |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 463 | } |
Kyungsik Lee | cffb78b | 2013-07-08 16:01:45 -0700 | [diff] [blame] | 464 | |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 465 | int LZ4_decompress_safe_partial(const char *src, char *dst, |
| 466 | int compressedSize, int targetOutputSize, int dstCapacity) |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 467 | { |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 468 | dstCapacity = min(targetOutputSize, dstCapacity); |
| 469 | return LZ4_decompress_generic(src, dst, compressedSize, dstCapacity, |
| 470 | endOnInputSize, partial_decode, |
| 471 | noDict, (BYTE *)dst, NULL, 0); |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 472 | } |
Kyungsik Lee | cffb78b | 2013-07-08 16:01:45 -0700 | [diff] [blame] | 473 | |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 474 | int LZ4_decompress_fast(const char *source, char *dest, int originalSize) |
| 475 | { |
| 476 | return LZ4_decompress_generic(source, dest, 0, originalSize, |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 477 | endOnOutputSize, decode_full_block, |
| 478 | withPrefix64k, |
| 479 | (BYTE *)dest - 64 * KB, NULL, 0); |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 480 | } |
Kyungsik Lee | cffb78b | 2013-07-08 16:01:45 -0700 | [diff] [blame] | 481 | |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 482 | /* ===== Instantiate a few more decoding cases, used more than once. ===== */ |
| 483 | |
Rajat Asthana | 7fde9d6 | 2021-06-30 18:56:13 -0700 | [diff] [blame] | 484 | static int LZ4_decompress_safe_withPrefix64k(const char *source, char *dest, |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 485 | int compressedSize, int maxOutputSize) |
| 486 | { |
| 487 | return LZ4_decompress_generic(source, dest, |
| 488 | compressedSize, maxOutputSize, |
| 489 | endOnInputSize, decode_full_block, |
| 490 | withPrefix64k, |
| 491 | (BYTE *)dest - 64 * KB, NULL, 0); |
| 492 | } |
| 493 | |
| 494 | static int LZ4_decompress_safe_withSmallPrefix(const char *source, char *dest, |
| 495 | int compressedSize, |
| 496 | int maxOutputSize, |
| 497 | size_t prefixSize) |
| 498 | { |
| 499 | return LZ4_decompress_generic(source, dest, |
| 500 | compressedSize, maxOutputSize, |
| 501 | endOnInputSize, decode_full_block, |
| 502 | noDict, |
| 503 | (BYTE *)dest - prefixSize, NULL, 0); |
| 504 | } |
| 505 | |
| 506 | int LZ4_decompress_safe_forceExtDict(const char *source, char *dest, |
| 507 | int compressedSize, int maxOutputSize, |
| 508 | const void *dictStart, size_t dictSize) |
| 509 | { |
| 510 | return LZ4_decompress_generic(source, dest, |
| 511 | compressedSize, maxOutputSize, |
| 512 | endOnInputSize, decode_full_block, |
| 513 | usingExtDict, (BYTE *)dest, |
| 514 | (const BYTE *)dictStart, dictSize); |
| 515 | } |
| 516 | |
| 517 | static int LZ4_decompress_fast_extDict(const char *source, char *dest, |
| 518 | int originalSize, |
| 519 | const void *dictStart, size_t dictSize) |
| 520 | { |
| 521 | return LZ4_decompress_generic(source, dest, |
| 522 | 0, originalSize, |
| 523 | endOnOutputSize, decode_full_block, |
| 524 | usingExtDict, (BYTE *)dest, |
| 525 | (const BYTE *)dictStart, dictSize); |
| 526 | } |
| 527 | |
| 528 | /* |
| 529 | * The "double dictionary" mode, for use with e.g. ring buffers: the first part |
| 530 | * of the dictionary is passed as prefix, and the second via dictStart + dictSize. |
| 531 | * These routines are used only once, in LZ4_decompress_*_continue(). |
| 532 | */ |
| 533 | static FORCE_INLINE |
| 534 | int LZ4_decompress_safe_doubleDict(const char *source, char *dest, |
| 535 | int compressedSize, int maxOutputSize, |
| 536 | size_t prefixSize, |
| 537 | const void *dictStart, size_t dictSize) |
| 538 | { |
| 539 | return LZ4_decompress_generic(source, dest, |
| 540 | compressedSize, maxOutputSize, |
| 541 | endOnInputSize, decode_full_block, |
| 542 | usingExtDict, (BYTE *)dest - prefixSize, |
| 543 | (const BYTE *)dictStart, dictSize); |
| 544 | } |
| 545 | |
| 546 | static FORCE_INLINE |
| 547 | int LZ4_decompress_fast_doubleDict(const char *source, char *dest, |
| 548 | int originalSize, size_t prefixSize, |
| 549 | const void *dictStart, size_t dictSize) |
| 550 | { |
| 551 | return LZ4_decompress_generic(source, dest, |
| 552 | 0, originalSize, |
| 553 | endOnOutputSize, decode_full_block, |
| 554 | usingExtDict, (BYTE *)dest - prefixSize, |
| 555 | (const BYTE *)dictStart, dictSize); |
| 556 | } |
| 557 | |
| 558 | /* ===== streaming decompression functions ===== */ |
| 559 | |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 560 | int LZ4_setStreamDecode(LZ4_streamDecode_t *LZ4_streamDecode, |
| 561 | const char *dictionary, int dictSize) |
| 562 | { |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 563 | LZ4_streamDecode_t_internal *lz4sd = |
| 564 | &LZ4_streamDecode->internal_donotuse; |
Kyungsik Lee | cffb78b | 2013-07-08 16:01:45 -0700 | [diff] [blame] | 565 | |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 566 | lz4sd->prefixSize = (size_t) dictSize; |
| 567 | lz4sd->prefixEnd = (const BYTE *) dictionary + dictSize; |
| 568 | lz4sd->externalDict = NULL; |
| 569 | lz4sd->extDictSize = 0; |
| 570 | return 1; |
| 571 | } |
Kyungsik Lee | cffb78b | 2013-07-08 16:01:45 -0700 | [diff] [blame] | 572 | |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 573 | /* |
| 574 | * *_continue() : |
| 575 | * These decoding functions allow decompression of multiple blocks |
| 576 | * in "streaming" mode. |
| 577 | * Previously decoded blocks must still be available at the memory |
| 578 | * position where they were decoded. |
| 579 | * If it's not possible, save the relevant part of |
| 580 | * decoded data into a safe buffer, |
| 581 | * and indicate where it stands using LZ4_setStreamDecode() |
| 582 | */ |
| 583 | int LZ4_decompress_safe_continue(LZ4_streamDecode_t *LZ4_streamDecode, |
| 584 | const char *source, char *dest, int compressedSize, int maxOutputSize) |
| 585 | { |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 586 | LZ4_streamDecode_t_internal *lz4sd = |
| 587 | &LZ4_streamDecode->internal_donotuse; |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 588 | int result; |
Kyungsik Lee | cffb78b | 2013-07-08 16:01:45 -0700 | [diff] [blame] | 589 | |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 590 | if (lz4sd->prefixSize == 0) { |
| 591 | /* The first call, no dictionary yet. */ |
| 592 | assert(lz4sd->extDictSize == 0); |
| 593 | result = LZ4_decompress_safe(source, dest, |
| 594 | compressedSize, maxOutputSize); |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 595 | if (result <= 0) |
| 596 | return result; |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 597 | lz4sd->prefixSize = result; |
| 598 | lz4sd->prefixEnd = (BYTE *)dest + result; |
| 599 | } else if (lz4sd->prefixEnd == (BYTE *)dest) { |
| 600 | /* They're rolling the current segment. */ |
| 601 | if (lz4sd->prefixSize >= 64 * KB - 1) |
| 602 | result = LZ4_decompress_safe_withPrefix64k(source, dest, |
| 603 | compressedSize, maxOutputSize); |
| 604 | else if (lz4sd->extDictSize == 0) |
| 605 | result = LZ4_decompress_safe_withSmallPrefix(source, |
| 606 | dest, compressedSize, maxOutputSize, |
| 607 | lz4sd->prefixSize); |
| 608 | else |
| 609 | result = LZ4_decompress_safe_doubleDict(source, dest, |
| 610 | compressedSize, maxOutputSize, |
| 611 | lz4sd->prefixSize, |
| 612 | lz4sd->externalDict, lz4sd->extDictSize); |
| 613 | if (result <= 0) |
| 614 | return result; |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 615 | lz4sd->prefixSize += result; |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 616 | lz4sd->prefixEnd += result; |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 617 | } else { |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 618 | /* |
| 619 | * The buffer wraps around, or they're |
| 620 | * switching to another buffer. |
| 621 | */ |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 622 | lz4sd->extDictSize = lz4sd->prefixSize; |
| 623 | lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize; |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 624 | result = LZ4_decompress_safe_forceExtDict(source, dest, |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 625 | compressedSize, maxOutputSize, |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 626 | lz4sd->externalDict, lz4sd->extDictSize); |
| 627 | if (result <= 0) |
| 628 | return result; |
| 629 | lz4sd->prefixSize = result; |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 630 | lz4sd->prefixEnd = (BYTE *)dest + result; |
Kyungsik Lee | cffb78b | 2013-07-08 16:01:45 -0700 | [diff] [blame] | 631 | } |
Kyungsik Lee | cffb78b | 2013-07-08 16:01:45 -0700 | [diff] [blame] | 632 | |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 633 | return result; |
| 634 | } |
| 635 | |
| 636 | int LZ4_decompress_fast_continue(LZ4_streamDecode_t *LZ4_streamDecode, |
| 637 | const char *source, char *dest, int originalSize) |
| 638 | { |
| 639 | LZ4_streamDecode_t_internal *lz4sd = &LZ4_streamDecode->internal_donotuse; |
| 640 | int result; |
| 641 | |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 642 | if (lz4sd->prefixSize == 0) { |
| 643 | assert(lz4sd->extDictSize == 0); |
| 644 | result = LZ4_decompress_fast(source, dest, originalSize); |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 645 | if (result <= 0) |
| 646 | return result; |
| 647 | lz4sd->prefixSize = originalSize; |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 648 | lz4sd->prefixEnd = (BYTE *)dest + originalSize; |
| 649 | } else if (lz4sd->prefixEnd == (BYTE *)dest) { |
| 650 | if (lz4sd->prefixSize >= 64 * KB - 1 || |
| 651 | lz4sd->extDictSize == 0) |
| 652 | result = LZ4_decompress_fast(source, dest, |
| 653 | originalSize); |
| 654 | else |
| 655 | result = LZ4_decompress_fast_doubleDict(source, dest, |
| 656 | originalSize, lz4sd->prefixSize, |
| 657 | lz4sd->externalDict, lz4sd->extDictSize); |
| 658 | if (result <= 0) |
| 659 | return result; |
| 660 | lz4sd->prefixSize += originalSize; |
| 661 | lz4sd->prefixEnd += originalSize; |
| 662 | } else { |
| 663 | lz4sd->extDictSize = lz4sd->prefixSize; |
| 664 | lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize; |
| 665 | result = LZ4_decompress_fast_extDict(source, dest, |
| 666 | originalSize, lz4sd->externalDict, lz4sd->extDictSize); |
| 667 | if (result <= 0) |
| 668 | return result; |
| 669 | lz4sd->prefixSize = originalSize; |
| 670 | lz4sd->prefixEnd = (BYTE *)dest + originalSize; |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 671 | } |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 672 | return result; |
| 673 | } |
| 674 | |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 675 | int LZ4_decompress_safe_usingDict(const char *source, char *dest, |
| 676 | int compressedSize, int maxOutputSize, |
| 677 | const char *dictStart, int dictSize) |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 678 | { |
| 679 | if (dictSize == 0) |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 680 | return LZ4_decompress_safe(source, dest, |
| 681 | compressedSize, maxOutputSize); |
| 682 | if (dictStart+dictSize == dest) { |
| 683 | if (dictSize >= 64 * KB - 1) |
| 684 | return LZ4_decompress_safe_withPrefix64k(source, dest, |
| 685 | compressedSize, maxOutputSize); |
| 686 | return LZ4_decompress_safe_withSmallPrefix(source, dest, |
| 687 | compressedSize, maxOutputSize, dictSize); |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 688 | } |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 689 | return LZ4_decompress_safe_forceExtDict(source, dest, |
| 690 | compressedSize, maxOutputSize, dictStart, dictSize); |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | int LZ4_decompress_fast_usingDict(const char *source, char *dest, |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 694 | int originalSize, |
| 695 | const char *dictStart, int dictSize) |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 696 | { |
Gao Xiang | 2209fda | 2018-10-30 15:07:28 -0700 | [diff] [blame] | 697 | if (dictSize == 0 || dictStart + dictSize == dest) |
| 698 | return LZ4_decompress_fast(source, dest, originalSize); |
| 699 | |
| 700 | return LZ4_decompress_fast_extDict(source, dest, originalSize, |
| 701 | dictStart, dictSize); |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 702 | } |
| 703 | |
Kyungsik Lee | cffb78b | 2013-07-08 16:01:45 -0700 | [diff] [blame] | 704 | #ifndef STATIC |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 705 | EXPORT_SYMBOL(LZ4_decompress_safe); |
| 706 | EXPORT_SYMBOL(LZ4_decompress_safe_partial); |
| 707 | EXPORT_SYMBOL(LZ4_decompress_fast); |
| 708 | EXPORT_SYMBOL(LZ4_setStreamDecode); |
| 709 | EXPORT_SYMBOL(LZ4_decompress_safe_continue); |
| 710 | EXPORT_SYMBOL(LZ4_decompress_fast_continue); |
| 711 | EXPORT_SYMBOL(LZ4_decompress_safe_usingDict); |
| 712 | EXPORT_SYMBOL(LZ4_decompress_fast_usingDict); |
Kyungsik Lee | cffb78b | 2013-07-08 16:01:45 -0700 | [diff] [blame] | 713 | |
Richard Laager | ee8a99b | 2013-08-22 16:35:47 -0700 | [diff] [blame] | 714 | MODULE_LICENSE("Dual BSD/GPL"); |
Sven Schmidt | 4e1a33b | 2017-02-24 15:01:12 -0800 | [diff] [blame] | 715 | MODULE_DESCRIPTION("LZ4 decompressor"); |
Kyungsik Lee | cffb78b | 2013-07-08 16:01:45 -0700 | [diff] [blame] | 716 | #endif |