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