1 /* 1 /* 2 * LZ4 - Fast LZ compression algorithm 2 * LZ4 - Fast LZ compression algorithm 3 * Copyright (C) 2011 - 2016, Yann Collet. 3 * Copyright (C) 2011 - 2016, Yann Collet. 4 * BSD 2 - Clause License (http://www.opensour 4 * BSD 2 - Clause License (http://www.opensource.org/licenses/bsd - license.php) 5 * Redistribution and use in source and binary 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that t 6 * modification, are permitted provided that the following conditions are 7 * met: 7 * met: 8 * * Redistributions of source code must 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions a 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials 12 * in the documentation and/or other materials provided with the 13 * distribution. 13 * distribution. 14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT 14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 15 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTI 15 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCH 16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 17 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 17 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 18 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIR 18 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGE 19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 20 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, S 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE PO 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 * You can contact the author at : 25 * You can contact the author at : 26 * - LZ4 homepage : http://www.lz4.org 26 * - LZ4 homepage : http://www.lz4.org 27 * - LZ4 source repository : https://gith 27 * - LZ4 source repository : https://github.com/lz4/lz4 28 * 28 * 29 * Changed for kernel usage by: 29 * Changed for kernel usage by: 30 * Sven Schmidt <4sschmid@informatik.uni- 30 * Sven Schmidt <4sschmid@informatik.uni-hamburg.de> 31 */ 31 */ 32 32 33 /*-************************************ 33 /*-************************************ 34 * Dependencies 34 * Dependencies 35 **************************************/ 35 **************************************/ 36 #include <linux/lz4.h> 36 #include <linux/lz4.h> 37 #include "lz4defs.h" 37 #include "lz4defs.h" 38 #include <linux/init.h> 38 #include <linux/init.h> 39 #include <linux/module.h> 39 #include <linux/module.h> 40 #include <linux/kernel.h> 40 #include <linux/kernel.h> 41 #include <linux/unaligned.h> !! 41 #include <asm/unaligned.h> 42 42 43 /*-***************************** 43 /*-***************************** 44 * Decompression functions 44 * Decompression functions 45 *******************************/ 45 *******************************/ 46 46 47 #define DEBUGLOG(l, ...) {} /* disabled */ 47 #define DEBUGLOG(l, ...) {} /* disabled */ 48 48 49 #ifndef assert 49 #ifndef assert 50 #define assert(condition) ((void)0) 50 #define assert(condition) ((void)0) 51 #endif 51 #endif 52 52 53 /* 53 /* 54 * LZ4_decompress_generic() : 54 * LZ4_decompress_generic() : 55 * This generic decompression function covers 55 * This generic decompression function covers all use cases. 56 * It shall be instantiated several times, usi 56 * It shall be instantiated several times, using different sets of directives. 57 * Note that it is important for performance t 57 * Note that it is important for performance that this function really get inlined, 58 * in order to remove useless branches during 58 * in order to remove useless branches during compilation optimization. 59 */ 59 */ 60 static FORCE_INLINE int LZ4_decompress_generic 60 static FORCE_INLINE int LZ4_decompress_generic( 61 const char * const src, 61 const char * const src, 62 char * const dst, 62 char * const dst, 63 int srcSize, 63 int srcSize, 64 /* 64 /* 65 * If endOnInput == endOnInput 65 * If endOnInput == endOnInputSize, 66 * this value is `dstCapacity` 66 * this value is `dstCapacity` 67 */ 67 */ 68 int outputSize, 68 int outputSize, 69 /* endOnOutputSize, endOnInputSize */ 69 /* endOnOutputSize, endOnInputSize */ 70 endCondition_directive endOnInput, 70 endCondition_directive endOnInput, 71 /* full, partial */ 71 /* full, partial */ 72 earlyEnd_directive partialDecoding, 72 earlyEnd_directive partialDecoding, 73 /* noDict, withPrefix64k, usingExtDic 73 /* noDict, withPrefix64k, usingExtDict */ 74 dict_directive dict, 74 dict_directive dict, 75 /* always <= dst, == dst when no pref 75 /* always <= dst, == dst when no prefix */ 76 const BYTE * const lowPrefix, 76 const BYTE * const lowPrefix, 77 /* only if dict == usingExtDict */ 77 /* only if dict == usingExtDict */ 78 const BYTE * const dictStart, 78 const BYTE * const dictStart, 79 /* note : = 0 if noDict */ 79 /* note : = 0 if noDict */ 80 const size_t dictSize 80 const size_t dictSize 81 ) 81 ) 82 { 82 { 83 const BYTE *ip = (const BYTE *) src; 83 const BYTE *ip = (const BYTE *) src; 84 const BYTE * const iend = ip + srcSize 84 const BYTE * const iend = ip + srcSize; 85 85 86 BYTE *op = (BYTE *) dst; 86 BYTE *op = (BYTE *) dst; 87 BYTE * const oend = op + outputSize; 87 BYTE * const oend = op + outputSize; 88 BYTE *cpy; 88 BYTE *cpy; 89 89 90 const BYTE * const dictEnd = (const BY 90 const BYTE * const dictEnd = (const BYTE *)dictStart + dictSize; 91 static const unsigned int inc32table[8 91 static const unsigned int inc32table[8] = {0, 1, 2, 1, 0, 4, 4, 4}; 92 static const int dec64table[8] = {0, 0 92 static const int dec64table[8] = {0, 0, 0, -1, -4, 1, 2, 3}; 93 93 94 const int safeDecode = (endOnInput == 94 const int safeDecode = (endOnInput == endOnInputSize); 95 const int checkOffset = ((safeDecode) 95 const int checkOffset = ((safeDecode) && (dictSize < (int)(64 * KB))); 96 96 97 /* Set up the "end" pointers for the s 97 /* Set up the "end" pointers for the shortcut. */ 98 const BYTE *const shortiend = iend - 98 const BYTE *const shortiend = iend - 99 (endOnInput ? 14 : 8) /*maxLL* 99 (endOnInput ? 14 : 8) /*maxLL*/ - 2 /*offset*/; 100 const BYTE *const shortoend = oend - 100 const BYTE *const shortoend = oend - 101 (endOnInput ? 14 : 8) /*maxLL* 101 (endOnInput ? 14 : 8) /*maxLL*/ - 18 /*maxML*/; 102 102 103 DEBUGLOG(5, "%s (srcSize:%i, dstSize:% 103 DEBUGLOG(5, "%s (srcSize:%i, dstSize:%i)", __func__, 104 srcSize, outputSize); 104 srcSize, outputSize); 105 105 106 /* Special cases */ 106 /* Special cases */ 107 assert(lowPrefix <= op); 107 assert(lowPrefix <= op); 108 assert(src != NULL); 108 assert(src != NULL); 109 109 110 /* Empty output buffer */ 110 /* Empty output buffer */ 111 if ((endOnInput) && (unlikely(outputSi 111 if ((endOnInput) && (unlikely(outputSize == 0))) 112 return ((srcSize == 1) && (*ip 112 return ((srcSize == 1) && (*ip == 0)) ? 0 : -1; 113 113 114 if ((!endOnInput) && (unlikely(outputS 114 if ((!endOnInput) && (unlikely(outputSize == 0))) 115 return (*ip == 0 ? 1 : -1); 115 return (*ip == 0 ? 1 : -1); 116 116 117 if ((endOnInput) && unlikely(srcSize = 117 if ((endOnInput) && unlikely(srcSize == 0)) 118 return -1; 118 return -1; 119 119 120 /* Main Loop : decode sequences */ 120 /* Main Loop : decode sequences */ 121 while (1) { 121 while (1) { 122 size_t length; 122 size_t length; 123 const BYTE *match; 123 const BYTE *match; 124 size_t offset; 124 size_t offset; 125 125 126 /* get literal length */ 126 /* get literal length */ 127 unsigned int const token = *ip 127 unsigned int const token = *ip++; 128 length = token>>ML_BITS; 128 length = token>>ML_BITS; 129 129 130 /* ip < iend before the increm 130 /* ip < iend before the increment */ 131 assert(!endOnInput || ip <= ie 131 assert(!endOnInput || ip <= iend); 132 132 133 /* 133 /* 134 * A two-stage shortcut for th 134 * A two-stage shortcut for the most common case: 135 * 1) If the literal length is 135 * 1) If the literal length is 0..14, and there is enough 136 * space, enter the shortcut a 136 * space, enter the shortcut and copy 16 bytes on behalf 137 * of the literals (in the fas 137 * of the literals (in the fast mode, only 8 bytes can be 138 * safely copied this way). 138 * safely copied this way). 139 * 2) Further if the match len 139 * 2) Further if the match length is 4..18, copy 18 bytes 140 * in a similar manner; but we 140 * in a similar manner; but we ensure that there's enough 141 * space in the output for tho 141 * space in the output for those 18 bytes earlier, upon 142 * entering the shortcut (in o 142 * entering the shortcut (in other words, there is a 143 * combined check for both sta 143 * combined check for both stages). 144 * << 145 * The & in the likely() below << 146 * some compilers can produce << 147 */ 144 */ 148 if ((endOnInput ? length != RU 145 if ((endOnInput ? length != RUN_MASK : length <= 8) 149 /* 146 /* 150 * strictly "less than" on 147 * strictly "less than" on input, to re-enter 151 * the loop with at least o 148 * the loop with at least one byte 152 */ 149 */ 153 && likely((endOnInput ? ip 150 && likely((endOnInput ? ip < shortiend : 1) & 154 (op <= shortoend) 151 (op <= shortoend))) { 155 /* Copy the literals * 152 /* Copy the literals */ 156 LZ4_memcpy(op, ip, end !! 153 memcpy(op, ip, endOnInput ? 16 : 8); 157 op += length; ip += le 154 op += length; ip += length; 158 155 159 /* 156 /* 160 * The second stage: 157 * The second stage: 161 * prepare for match c 158 * prepare for match copying, decode full info. 162 * If it doesn't work 159 * If it doesn't work out, the info won't be wasted. 163 */ 160 */ 164 length = token & ML_MA 161 length = token & ML_MASK; /* match length */ 165 offset = LZ4_readLE16( 162 offset = LZ4_readLE16(ip); 166 ip += 2; 163 ip += 2; 167 match = op - offset; 164 match = op - offset; 168 assert(match <= op); / 165 assert(match <= op); /* check overflow */ 169 166 170 /* Do not deal with ov 167 /* Do not deal with overlapping matches. */ 171 if ((length != ML_MASK 168 if ((length != ML_MASK) && 172 (offset >= 8) && 169 (offset >= 8) && 173 (dict == withPrefi 170 (dict == withPrefix64k || match >= lowPrefix)) { 174 /* Copy the ma 171 /* Copy the match. */ 175 LZ4_memcpy(op !! 172 memcpy(op + 0, match + 0, 8); 176 LZ4_memcpy(op !! 173 memcpy(op + 8, match + 8, 8); 177 LZ4_memcpy(op !! 174 memcpy(op + 16, match + 16, 2); 178 op += length + 175 op += length + MINMATCH; 179 /* Both stages 176 /* Both stages worked, load the next token. */ 180 continue; 177 continue; 181 } 178 } 182 179 183 /* 180 /* 184 * The second stage di 181 * The second stage didn't work out, but the info 185 * is ready. Propel it 182 * is ready. Propel it right to the point of match 186 * copying. 183 * copying. 187 */ 184 */ 188 goto _copy_match; 185 goto _copy_match; 189 } 186 } 190 187 191 /* decode literal length */ 188 /* decode literal length */ 192 if (length == RUN_MASK) { 189 if (length == RUN_MASK) { 193 unsigned int s; 190 unsigned int s; 194 191 195 if (unlikely(endOnInpu 192 if (unlikely(endOnInput ? ip >= iend - RUN_MASK : 0)) { 196 /* overflow de 193 /* overflow detection */ 197 goto _output_e 194 goto _output_error; 198 } 195 } 199 do { 196 do { 200 s = *ip++; 197 s = *ip++; 201 length += s; 198 length += s; 202 } while (likely(endOnI 199 } while (likely(endOnInput 203 ? ip < iend - 200 ? ip < iend - RUN_MASK 204 : 1) & (s == 2 201 : 1) & (s == 255)); 205 202 206 if ((safeDecode) 203 if ((safeDecode) 207 && unlikely((uptrv 204 && unlikely((uptrval)(op) + 208 length 205 length < (uptrval)(op))) { 209 /* overflow de 206 /* overflow detection */ 210 goto _output_e 207 goto _output_error; 211 } 208 } 212 if ((safeDecode) 209 if ((safeDecode) 213 && unlikely((uptrv 210 && unlikely((uptrval)(ip) + 214 length 211 length < (uptrval)(ip))) { 215 /* overflow de 212 /* overflow detection */ 216 goto _output_e 213 goto _output_error; 217 } 214 } 218 } 215 } 219 216 220 /* copy literals */ 217 /* copy literals */ 221 cpy = op + length; 218 cpy = op + length; 222 LZ4_STATIC_ASSERT(MFLIMIT >= W 219 LZ4_STATIC_ASSERT(MFLIMIT >= WILDCOPYLENGTH); 223 220 224 if (((endOnInput) && ((cpy > o 221 if (((endOnInput) && ((cpy > oend - MFLIMIT) 225 || (ip + length > iend 222 || (ip + length > iend - (2 + 1 + LASTLITERALS)))) 226 || ((!endOnInput) && ( 223 || ((!endOnInput) && (cpy > oend - WILDCOPYLENGTH))) { 227 if (partialDecoding) { 224 if (partialDecoding) { 228 if (cpy > oend 225 if (cpy > oend) { 229 /* 226 /* 230 * Par 227 * Partial decoding : 231 * sto 228 * stop in the middle of literal segment 232 */ 229 */ 233 cpy = 230 cpy = oend; 234 length 231 length = oend - op; 235 } 232 } 236 if ((endOnInpu 233 if ((endOnInput) 237 && (ip 234 && (ip + length > iend)) { 238 /* 235 /* 239 * Err 236 * Error : 240 * rea 237 * read attempt beyond 241 * end 238 * end of input buffer 242 */ 239 */ 243 goto _ 240 goto _output_error; 244 } 241 } 245 } else { 242 } else { 246 if ((!endOnInp 243 if ((!endOnInput) 247 && (cp 244 && (cpy != oend)) { 248 /* 245 /* 249 * Err 246 * Error : 250 * blo 247 * block decoding must 251 * sto 248 * stop exactly there 252 */ 249 */ 253 goto _ 250 goto _output_error; 254 } 251 } 255 if ((endOnInpu 252 if ((endOnInput) 256 && ((i 253 && ((ip + length != iend) 257 || (cp 254 || (cpy > oend))) { 258 /* 255 /* 259 * Err 256 * Error : 260 * inp 257 * input must be consumed 261 */ 258 */ 262 goto _ 259 goto _output_error; 263 } 260 } 264 } 261 } 265 262 266 /* !! 263 memcpy(op, ip, length); 267 * supports overlappin << 268 * for in-place decomp << 269 */ << 270 LZ4_memmove(op, ip, le << 271 ip += length; 264 ip += length; 272 op += length; 265 op += length; 273 266 274 /* Necessarily EOF whe !! 267 /* Necessarily EOF, due to parsing restrictions */ 275 * When partialDecodin !! 268 if (!partialDecoding || (cpy == oend)) 276 * filled the output b << 277 * can't proceed with << 278 */ << 279 if (!partialDecoding | << 280 break; 269 break; 281 } else { 270 } else { 282 /* may overwrite up to 271 /* may overwrite up to WILDCOPYLENGTH beyond cpy */ 283 LZ4_wildCopy(op, ip, c 272 LZ4_wildCopy(op, ip, cpy); 284 ip += length; 273 ip += length; 285 op = cpy; 274 op = cpy; 286 } 275 } 287 276 288 /* get offset */ 277 /* get offset */ 289 offset = LZ4_readLE16(ip); 278 offset = LZ4_readLE16(ip); 290 ip += 2; 279 ip += 2; 291 match = op - offset; 280 match = op - offset; 292 281 293 /* get matchlength */ 282 /* get matchlength */ 294 length = token & ML_MASK; 283 length = token & ML_MASK; 295 284 296 _copy_match: 285 _copy_match: 297 if ((checkOffset) && (unlikely 286 if ((checkOffset) && (unlikely(match + dictSize < lowPrefix))) { 298 /* Error : offset outs 287 /* Error : offset outside buffers */ 299 goto _output_error; 288 goto _output_error; 300 } 289 } 301 290 302 /* costs ~1%; silence an msan 291 /* costs ~1%; silence an msan warning when offset == 0 */ 303 /* 292 /* 304 * note : when partialDecoding 293 * note : when partialDecoding, there is no guarantee that 305 * at least 4 bytes remain ava 294 * at least 4 bytes remain available in output buffer 306 */ 295 */ 307 if (!partialDecoding) { 296 if (!partialDecoding) { 308 assert(oend > op); 297 assert(oend > op); 309 assert(oend - op >= 4) 298 assert(oend - op >= 4); 310 299 311 LZ4_write32(op, (U32)o 300 LZ4_write32(op, (U32)offset); 312 } 301 } 313 302 314 if (length == ML_MASK) { 303 if (length == ML_MASK) { 315 unsigned int s; 304 unsigned int s; 316 305 317 do { 306 do { 318 s = *ip++; 307 s = *ip++; 319 308 320 if ((endOnInpu 309 if ((endOnInput) && (ip > iend - LASTLITERALS)) 321 goto _ 310 goto _output_error; 322 311 323 length += s; 312 length += s; 324 } while (s == 255); 313 } while (s == 255); 325 314 326 if ((safeDecode) 315 if ((safeDecode) 327 && unlikely( 316 && unlikely( 328 (uptrv 317 (uptrval)(op) + length < (uptrval)op)) { 329 /* overflow de 318 /* overflow detection */ 330 goto _output_e 319 goto _output_error; 331 } 320 } 332 } 321 } 333 322 334 length += MINMATCH; 323 length += MINMATCH; 335 324 336 /* match starting within exter 325 /* match starting within external dictionary */ 337 if ((dict == usingExtDict) && 326 if ((dict == usingExtDict) && (match < lowPrefix)) { 338 if (unlikely(op + leng 327 if (unlikely(op + length > oend - LASTLITERALS)) { 339 /* doesn't res 328 /* doesn't respect parsing restriction */ 340 if (!partialDe 329 if (!partialDecoding) 341 goto _ 330 goto _output_error; 342 length = min(l 331 length = min(length, (size_t)(oend - op)); 343 } 332 } 344 333 345 if (length <= (size_t) 334 if (length <= (size_t)(lowPrefix - match)) { 346 /* 335 /* 347 * match fits 336 * match fits entirely within external 348 * dictionary 337 * dictionary : just copy 349 */ 338 */ 350 memmove(op, di 339 memmove(op, dictEnd - (lowPrefix - match), 351 length 340 length); 352 op += length; 341 op += length; 353 } else { 342 } else { 354 /* 343 /* 355 * match stret 344 * match stretches into both external 356 * dictionary 345 * dictionary and current block 357 */ 346 */ 358 size_t const c 347 size_t const copySize = (size_t)(lowPrefix - match); 359 size_t const r 348 size_t const restSize = length - copySize; 360 349 361 LZ4_memcpy(op, !! 350 memcpy(op, dictEnd - copySize, copySize); 362 op += copySize 351 op += copySize; 363 if (restSize > 352 if (restSize > (size_t)(op - lowPrefix)) { 364 /* ove 353 /* overlap copy */ 365 BYTE * 354 BYTE * const endOfMatch = op + restSize; 366 const 355 const BYTE *copyFrom = lowPrefix; 367 356 368 while 357 while (op < endOfMatch) 369 358 *op++ = *copyFrom++; 370 } else { 359 } else { 371 LZ4_me !! 360 memcpy(op, lowPrefix, restSize); 372 op += 361 op += restSize; 373 } 362 } 374 } 363 } 375 continue; 364 continue; 376 } 365 } 377 366 378 /* copy match within block */ 367 /* copy match within block */ 379 cpy = op + length; 368 cpy = op + length; 380 369 381 /* 370 /* 382 * partialDecoding : 371 * partialDecoding : 383 * may not respect endBlock pa 372 * may not respect endBlock parsing restrictions 384 */ 373 */ 385 assert(op <= oend); 374 assert(op <= oend); 386 if (partialDecoding && 375 if (partialDecoding && 387 (cpy > oend - MATCH_SAFEGU 376 (cpy > oend - MATCH_SAFEGUARD_DISTANCE)) { 388 size_t const mlen = mi 377 size_t const mlen = min(length, (size_t)(oend - op)); 389 const BYTE * const mat 378 const BYTE * const matchEnd = match + mlen; 390 BYTE * const copyEnd = 379 BYTE * const copyEnd = op + mlen; 391 380 392 if (matchEnd > op) { 381 if (matchEnd > op) { 393 /* overlap cop 382 /* overlap copy */ 394 while (op < co 383 while (op < copyEnd) 395 *op++ 384 *op++ = *match++; 396 } else { 385 } else { 397 LZ4_memcpy(op, !! 386 memcpy(op, match, mlen); 398 } 387 } 399 op = copyEnd; 388 op = copyEnd; 400 if (op == oend) 389 if (op == oend) 401 break; 390 break; 402 continue; 391 continue; 403 } 392 } 404 393 405 if (unlikely(offset < 8)) { 394 if (unlikely(offset < 8)) { 406 op[0] = match[0]; 395 op[0] = match[0]; 407 op[1] = match[1]; 396 op[1] = match[1]; 408 op[2] = match[2]; 397 op[2] = match[2]; 409 op[3] = match[3]; 398 op[3] = match[3]; 410 match += inc32table[of 399 match += inc32table[offset]; 411 LZ4_memcpy(op + 4, mat !! 400 memcpy(op + 4, match, 4); 412 match -= dec64table[of 401 match -= dec64table[offset]; 413 } else { 402 } else { 414 LZ4_copy8(op, match); 403 LZ4_copy8(op, match); 415 match += 8; 404 match += 8; 416 } 405 } 417 406 418 op += 8; 407 op += 8; 419 408 420 if (unlikely(cpy > oend - MATC 409 if (unlikely(cpy > oend - MATCH_SAFEGUARD_DISTANCE)) { 421 BYTE * const oCopyLimi 410 BYTE * const oCopyLimit = oend - (WILDCOPYLENGTH - 1); 422 411 423 if (cpy > oend - LASTL 412 if (cpy > oend - LASTLITERALS) { 424 /* 413 /* 425 * Error : las 414 * Error : last LASTLITERALS bytes 426 * must be lit 415 * must be literals (uncompressed) 427 */ 416 */ 428 goto _output_e 417 goto _output_error; 429 } 418 } 430 419 431 if (op < oCopyLimit) { 420 if (op < oCopyLimit) { 432 LZ4_wildCopy(o 421 LZ4_wildCopy(op, match, oCopyLimit); 433 match += oCopy 422 match += oCopyLimit - op; 434 op = oCopyLimi 423 op = oCopyLimit; 435 } 424 } 436 while (op < cpy) 425 while (op < cpy) 437 *op++ = *match 426 *op++ = *match++; 438 } else { 427 } else { 439 LZ4_copy8(op, match); 428 LZ4_copy8(op, match); 440 if (length > 16) 429 if (length > 16) 441 LZ4_wildCopy(o 430 LZ4_wildCopy(op + 8, match + 8, cpy); 442 } 431 } 443 op = cpy; /* wildcopy correcti 432 op = cpy; /* wildcopy correction */ 444 } 433 } 445 434 446 /* end of decoding */ 435 /* end of decoding */ 447 if (endOnInput) { 436 if (endOnInput) { 448 /* Nb of output bytes decoded 437 /* Nb of output bytes decoded */ 449 return (int) (((char *)op) - d 438 return (int) (((char *)op) - dst); 450 } else { 439 } else { 451 /* Nb of input bytes read */ 440 /* Nb of input bytes read */ 452 return (int) (((const char *)i 441 return (int) (((const char *)ip) - src); 453 } 442 } 454 443 455 /* Overflow error detected */ 444 /* Overflow error detected */ 456 _output_error: 445 _output_error: 457 return (int) (-(((const char *)ip) - s 446 return (int) (-(((const char *)ip) - src)) - 1; 458 } 447 } 459 448 460 int LZ4_decompress_safe(const char *source, ch 449 int LZ4_decompress_safe(const char *source, char *dest, 461 int compressedSize, int maxDecompresse 450 int compressedSize, int maxDecompressedSize) 462 { 451 { 463 return LZ4_decompress_generic(source, 452 return LZ4_decompress_generic(source, dest, 464 compress 453 compressedSize, maxDecompressedSize, 465 endOnInp 454 endOnInputSize, decode_full_block, 466 noDict, 455 noDict, (BYTE *)dest, NULL, 0); 467 } 456 } 468 457 469 int LZ4_decompress_safe_partial(const char *sr 458 int LZ4_decompress_safe_partial(const char *src, char *dst, 470 int compressedSize, int targetOutputSi 459 int compressedSize, int targetOutputSize, int dstCapacity) 471 { 460 { 472 dstCapacity = min(targetOutputSize, ds 461 dstCapacity = min(targetOutputSize, dstCapacity); 473 return LZ4_decompress_generic(src, dst 462 return LZ4_decompress_generic(src, dst, compressedSize, dstCapacity, 474 endOnInp 463 endOnInputSize, partial_decode, 475 noDict, 464 noDict, (BYTE *)dst, NULL, 0); 476 } 465 } 477 466 478 int LZ4_decompress_fast(const char *source, ch 467 int LZ4_decompress_fast(const char *source, char *dest, int originalSize) 479 { 468 { 480 return LZ4_decompress_generic(source, 469 return LZ4_decompress_generic(source, dest, 0, originalSize, 481 endOnOut 470 endOnOutputSize, decode_full_block, 482 withPref 471 withPrefix64k, 483 (BYTE *) 472 (BYTE *)dest - 64 * KB, NULL, 0); 484 } 473 } 485 474 486 /* ===== Instantiate a few more decoding cases 475 /* ===== Instantiate a few more decoding cases, used more than once. ===== */ 487 476 488 static int LZ4_decompress_safe_withPrefix64k(c !! 477 int LZ4_decompress_safe_withPrefix64k(const char *source, char *dest, 489 int comp 478 int compressedSize, int maxOutputSize) 490 { 479 { 491 return LZ4_decompress_generic(source, 480 return LZ4_decompress_generic(source, dest, 492 compress 481 compressedSize, maxOutputSize, 493 endOnInp 482 endOnInputSize, decode_full_block, 494 withPref 483 withPrefix64k, 495 (BYTE *) 484 (BYTE *)dest - 64 * KB, NULL, 0); 496 } 485 } 497 486 498 static int LZ4_decompress_safe_withSmallPrefix 487 static int LZ4_decompress_safe_withSmallPrefix(const char *source, char *dest, 499 488 int compressedSize, 500 489 int maxOutputSize, 501 490 size_t prefixSize) 502 { 491 { 503 return LZ4_decompress_generic(source, 492 return LZ4_decompress_generic(source, dest, 504 compress 493 compressedSize, maxOutputSize, 505 endOnInp 494 endOnInputSize, decode_full_block, 506 noDict, 495 noDict, 507 (BYTE *) 496 (BYTE *)dest - prefixSize, NULL, 0); 508 } 497 } 509 498 510 static int LZ4_decompress_safe_forceExtDict(co !! 499 int LZ4_decompress_safe_forceExtDict(const char *source, char *dest, 511 in !! 500 int compressedSize, int maxOutputSize, 512 co !! 501 const void *dictStart, size_t dictSize) 513 { 502 { 514 return LZ4_decompress_generic(source, 503 return LZ4_decompress_generic(source, dest, 515 compress 504 compressedSize, maxOutputSize, 516 endOnInp 505 endOnInputSize, decode_full_block, 517 usingExt 506 usingExtDict, (BYTE *)dest, 518 (const B 507 (const BYTE *)dictStart, dictSize); 519 } 508 } 520 509 521 static int LZ4_decompress_fast_extDict(const c 510 static int LZ4_decompress_fast_extDict(const char *source, char *dest, 522 int ori 511 int originalSize, 523 const v 512 const void *dictStart, size_t dictSize) 524 { 513 { 525 return LZ4_decompress_generic(source, 514 return LZ4_decompress_generic(source, dest, 526 0, origi 515 0, originalSize, 527 endOnOut 516 endOnOutputSize, decode_full_block, 528 usingExt 517 usingExtDict, (BYTE *)dest, 529 (const B 518 (const BYTE *)dictStart, dictSize); 530 } 519 } 531 520 532 /* 521 /* 533 * The "double dictionary" mode, for use with 522 * The "double dictionary" mode, for use with e.g. ring buffers: the first part 534 * of the dictionary is passed as prefix, and 523 * of the dictionary is passed as prefix, and the second via dictStart + dictSize. 535 * These routines are used only once, in LZ4_d 524 * These routines are used only once, in LZ4_decompress_*_continue(). 536 */ 525 */ 537 static FORCE_INLINE 526 static FORCE_INLINE 538 int LZ4_decompress_safe_doubleDict(const char 527 int LZ4_decompress_safe_doubleDict(const char *source, char *dest, 539 int compres 528 int compressedSize, int maxOutputSize, 540 size_t pref 529 size_t prefixSize, 541 const void 530 const void *dictStart, size_t dictSize) 542 { 531 { 543 return LZ4_decompress_generic(source, 532 return LZ4_decompress_generic(source, dest, 544 compress 533 compressedSize, maxOutputSize, 545 endOnInp 534 endOnInputSize, decode_full_block, 546 usingExt 535 usingExtDict, (BYTE *)dest - prefixSize, 547 (const B 536 (const BYTE *)dictStart, dictSize); 548 } 537 } 549 538 550 static FORCE_INLINE 539 static FORCE_INLINE 551 int LZ4_decompress_fast_doubleDict(const char 540 int LZ4_decompress_fast_doubleDict(const char *source, char *dest, 552 int origina 541 int originalSize, size_t prefixSize, 553 const void 542 const void *dictStart, size_t dictSize) 554 { 543 { 555 return LZ4_decompress_generic(source, 544 return LZ4_decompress_generic(source, dest, 556 0, origi 545 0, originalSize, 557 endOnOut 546 endOnOutputSize, decode_full_block, 558 usingExt 547 usingExtDict, (BYTE *)dest - prefixSize, 559 (const B 548 (const BYTE *)dictStart, dictSize); 560 } 549 } 561 550 562 /* ===== streaming decompression functions === 551 /* ===== streaming decompression functions ===== */ 563 552 564 int LZ4_setStreamDecode(LZ4_streamDecode_t *LZ 553 int LZ4_setStreamDecode(LZ4_streamDecode_t *LZ4_streamDecode, 565 const char *dictionary, int dictSize) 554 const char *dictionary, int dictSize) 566 { 555 { 567 LZ4_streamDecode_t_internal *lz4sd = 556 LZ4_streamDecode_t_internal *lz4sd = 568 &LZ4_streamDecode->internal_do 557 &LZ4_streamDecode->internal_donotuse; 569 558 570 lz4sd->prefixSize = (size_t) dictSize; 559 lz4sd->prefixSize = (size_t) dictSize; 571 lz4sd->prefixEnd = (const BYTE *) dict 560 lz4sd->prefixEnd = (const BYTE *) dictionary + dictSize; 572 lz4sd->externalDict = NULL; 561 lz4sd->externalDict = NULL; 573 lz4sd->extDictSize = 0; 562 lz4sd->extDictSize = 0; 574 return 1; 563 return 1; 575 } 564 } 576 565 577 /* 566 /* 578 * *_continue() : 567 * *_continue() : 579 * These decoding functions allow decompressio 568 * These decoding functions allow decompression of multiple blocks 580 * in "streaming" mode. 569 * in "streaming" mode. 581 * Previously decoded blocks must still be ava 570 * Previously decoded blocks must still be available at the memory 582 * position where they were decoded. 571 * position where they were decoded. 583 * If it's not possible, save the relevant par 572 * If it's not possible, save the relevant part of 584 * decoded data into a safe buffer, 573 * decoded data into a safe buffer, 585 * and indicate where it stands using LZ4_setS 574 * and indicate where it stands using LZ4_setStreamDecode() 586 */ 575 */ 587 int LZ4_decompress_safe_continue(LZ4_streamDec 576 int LZ4_decompress_safe_continue(LZ4_streamDecode_t *LZ4_streamDecode, 588 const char *source, char *dest, int co 577 const char *source, char *dest, int compressedSize, int maxOutputSize) 589 { 578 { 590 LZ4_streamDecode_t_internal *lz4sd = 579 LZ4_streamDecode_t_internal *lz4sd = 591 &LZ4_streamDecode->internal_do 580 &LZ4_streamDecode->internal_donotuse; 592 int result; 581 int result; 593 582 594 if (lz4sd->prefixSize == 0) { 583 if (lz4sd->prefixSize == 0) { 595 /* The first call, no dictiona 584 /* The first call, no dictionary yet. */ 596 assert(lz4sd->extDictSize == 0 585 assert(lz4sd->extDictSize == 0); 597 result = LZ4_decompress_safe(s 586 result = LZ4_decompress_safe(source, dest, 598 compressedSize, maxOut 587 compressedSize, maxOutputSize); 599 if (result <= 0) 588 if (result <= 0) 600 return result; 589 return result; 601 lz4sd->prefixSize = result; 590 lz4sd->prefixSize = result; 602 lz4sd->prefixEnd = (BYTE *)des 591 lz4sd->prefixEnd = (BYTE *)dest + result; 603 } else if (lz4sd->prefixEnd == (BYTE * 592 } else if (lz4sd->prefixEnd == (BYTE *)dest) { 604 /* They're rolling the current 593 /* They're rolling the current segment. */ 605 if (lz4sd->prefixSize >= 64 * 594 if (lz4sd->prefixSize >= 64 * KB - 1) 606 result = LZ4_decompres 595 result = LZ4_decompress_safe_withPrefix64k(source, dest, 607 compressedSize 596 compressedSize, maxOutputSize); 608 else if (lz4sd->extDictSize == 597 else if (lz4sd->extDictSize == 0) 609 result = LZ4_decompres 598 result = LZ4_decompress_safe_withSmallPrefix(source, 610 dest, compress 599 dest, compressedSize, maxOutputSize, 611 lz4sd->prefixS 600 lz4sd->prefixSize); 612 else 601 else 613 result = LZ4_decompres 602 result = LZ4_decompress_safe_doubleDict(source, dest, 614 compressedSize 603 compressedSize, maxOutputSize, 615 lz4sd->prefixS 604 lz4sd->prefixSize, 616 lz4sd->externa 605 lz4sd->externalDict, lz4sd->extDictSize); 617 if (result <= 0) 606 if (result <= 0) 618 return result; 607 return result; 619 lz4sd->prefixSize += result; 608 lz4sd->prefixSize += result; 620 lz4sd->prefixEnd += result; 609 lz4sd->prefixEnd += result; 621 } else { 610 } else { 622 /* 611 /* 623 * The buffer wraps around, or 612 * The buffer wraps around, or they're 624 * switching to another buffer 613 * switching to another buffer. 625 */ 614 */ 626 lz4sd->extDictSize = lz4sd->pr 615 lz4sd->extDictSize = lz4sd->prefixSize; 627 lz4sd->externalDict = lz4sd->p 616 lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize; 628 result = LZ4_decompress_safe_f 617 result = LZ4_decompress_safe_forceExtDict(source, dest, 629 compressedSize, maxOut 618 compressedSize, maxOutputSize, 630 lz4sd->externalDict, l 619 lz4sd->externalDict, lz4sd->extDictSize); 631 if (result <= 0) 620 if (result <= 0) 632 return result; 621 return result; 633 lz4sd->prefixSize = result; 622 lz4sd->prefixSize = result; 634 lz4sd->prefixEnd = (BYTE *)de 623 lz4sd->prefixEnd = (BYTE *)dest + result; 635 } 624 } 636 625 637 return result; 626 return result; 638 } 627 } 639 628 640 int LZ4_decompress_fast_continue(LZ4_streamDec 629 int LZ4_decompress_fast_continue(LZ4_streamDecode_t *LZ4_streamDecode, 641 const char *source, char *dest, int or 630 const char *source, char *dest, int originalSize) 642 { 631 { 643 LZ4_streamDecode_t_internal *lz4sd = & 632 LZ4_streamDecode_t_internal *lz4sd = &LZ4_streamDecode->internal_donotuse; 644 int result; 633 int result; 645 634 646 if (lz4sd->prefixSize == 0) { 635 if (lz4sd->prefixSize == 0) { 647 assert(lz4sd->extDictSize == 0 636 assert(lz4sd->extDictSize == 0); 648 result = LZ4_decompress_fast(s 637 result = LZ4_decompress_fast(source, dest, originalSize); 649 if (result <= 0) 638 if (result <= 0) 650 return result; 639 return result; 651 lz4sd->prefixSize = originalSi 640 lz4sd->prefixSize = originalSize; 652 lz4sd->prefixEnd = (BYTE *)des 641 lz4sd->prefixEnd = (BYTE *)dest + originalSize; 653 } else if (lz4sd->prefixEnd == (BYTE * 642 } else if (lz4sd->prefixEnd == (BYTE *)dest) { 654 if (lz4sd->prefixSize >= 64 * 643 if (lz4sd->prefixSize >= 64 * KB - 1 || 655 lz4sd->extDictSize == 0) 644 lz4sd->extDictSize == 0) 656 result = LZ4_decompres 645 result = LZ4_decompress_fast(source, dest, 657 646 originalSize); 658 else 647 else 659 result = LZ4_decompres 648 result = LZ4_decompress_fast_doubleDict(source, dest, 660 originalSize, 649 originalSize, lz4sd->prefixSize, 661 lz4sd->externa 650 lz4sd->externalDict, lz4sd->extDictSize); 662 if (result <= 0) 651 if (result <= 0) 663 return result; 652 return result; 664 lz4sd->prefixSize += originalS 653 lz4sd->prefixSize += originalSize; 665 lz4sd->prefixEnd += originalS 654 lz4sd->prefixEnd += originalSize; 666 } else { 655 } else { 667 lz4sd->extDictSize = lz4sd->pr 656 lz4sd->extDictSize = lz4sd->prefixSize; 668 lz4sd->externalDict = lz4sd->p 657 lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize; 669 result = LZ4_decompress_fast_e 658 result = LZ4_decompress_fast_extDict(source, dest, 670 originalSize, lz4sd->e 659 originalSize, lz4sd->externalDict, lz4sd->extDictSize); 671 if (result <= 0) 660 if (result <= 0) 672 return result; 661 return result; 673 lz4sd->prefixSize = originalSi 662 lz4sd->prefixSize = originalSize; 674 lz4sd->prefixEnd = (BYTE *)des 663 lz4sd->prefixEnd = (BYTE *)dest + originalSize; 675 } 664 } 676 return result; 665 return result; 677 } 666 } 678 667 679 int LZ4_decompress_safe_usingDict(const char * 668 int LZ4_decompress_safe_usingDict(const char *source, char *dest, 680 int compress 669 int compressedSize, int maxOutputSize, 681 const char * 670 const char *dictStart, int dictSize) 682 { 671 { 683 if (dictSize == 0) 672 if (dictSize == 0) 684 return LZ4_decompress_safe(sou 673 return LZ4_decompress_safe(source, dest, 685 com 674 compressedSize, maxOutputSize); 686 if (dictStart+dictSize == dest) { 675 if (dictStart+dictSize == dest) { 687 if (dictSize >= 64 * KB - 1) 676 if (dictSize >= 64 * KB - 1) 688 return LZ4_decompress_ 677 return LZ4_decompress_safe_withPrefix64k(source, dest, 689 compressedSize 678 compressedSize, maxOutputSize); 690 return LZ4_decompress_safe_wit 679 return LZ4_decompress_safe_withSmallPrefix(source, dest, 691 compressedSize, maxOut 680 compressedSize, maxOutputSize, dictSize); 692 } 681 } 693 return LZ4_decompress_safe_forceExtDic 682 return LZ4_decompress_safe_forceExtDict(source, dest, 694 compressedSize, maxOutputSize, 683 compressedSize, maxOutputSize, dictStart, dictSize); 695 } 684 } 696 685 697 int LZ4_decompress_fast_usingDict(const char * 686 int LZ4_decompress_fast_usingDict(const char *source, char *dest, 698 int original 687 int originalSize, 699 const char * 688 const char *dictStart, int dictSize) 700 { 689 { 701 if (dictSize == 0 || dictStart + dictS 690 if (dictSize == 0 || dictStart + dictSize == dest) 702 return LZ4_decompress_fast(sou 691 return LZ4_decompress_fast(source, dest, originalSize); 703 692 704 return LZ4_decompress_fast_extDict(sou 693 return LZ4_decompress_fast_extDict(source, dest, originalSize, 705 dictStart, dictSize); 694 dictStart, dictSize); 706 } 695 } 707 696 708 #ifndef STATIC 697 #ifndef STATIC 709 EXPORT_SYMBOL(LZ4_decompress_safe); 698 EXPORT_SYMBOL(LZ4_decompress_safe); 710 EXPORT_SYMBOL(LZ4_decompress_safe_partial); 699 EXPORT_SYMBOL(LZ4_decompress_safe_partial); 711 EXPORT_SYMBOL(LZ4_decompress_fast); 700 EXPORT_SYMBOL(LZ4_decompress_fast); 712 EXPORT_SYMBOL(LZ4_setStreamDecode); 701 EXPORT_SYMBOL(LZ4_setStreamDecode); 713 EXPORT_SYMBOL(LZ4_decompress_safe_continue); 702 EXPORT_SYMBOL(LZ4_decompress_safe_continue); 714 EXPORT_SYMBOL(LZ4_decompress_fast_continue); 703 EXPORT_SYMBOL(LZ4_decompress_fast_continue); 715 EXPORT_SYMBOL(LZ4_decompress_safe_usingDict); 704 EXPORT_SYMBOL(LZ4_decompress_safe_usingDict); 716 EXPORT_SYMBOL(LZ4_decompress_fast_usingDict); 705 EXPORT_SYMBOL(LZ4_decompress_fast_usingDict); 717 706 718 MODULE_LICENSE("Dual BSD/GPL"); 707 MODULE_LICENSE("Dual BSD/GPL"); 719 MODULE_DESCRIPTION("LZ4 decompressor"); 708 MODULE_DESCRIPTION("LZ4 decompressor"); 720 #endif 709 #endif 721 710
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.