Blame view

lib/lz4/lz4_decompress.c 20 KB
cffb78b0e   Kyungsik Lee   decompressor: add...
1
  /*
cffb78b0e   Kyungsik Lee   decompressor: add...
2
   * LZ4 - Fast LZ compression algorithm
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
3
4
   * Copyright (C) 2011 - 2016, Yann Collet.
   * BSD 2 - Clause License (http://www.opensource.org/licenses/bsd - license.php)
cffb78b0e   Kyungsik Lee   decompressor: add...
5
6
7
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions are
   * met:
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
8
9
10
   *	* Redistributions of source code must retain the above copyright
   *	  notice, this list of conditions and the following disclaimer.
   *	* Redistributions in binary form must reproduce the above
cffb78b0e   Kyungsik Lee   decompressor: add...
11
12
13
   * copyright notice, this list of conditions and the following disclaimer
   * in the documentation and/or other materials provided with the
   * distribution.
cffb78b0e   Kyungsik Lee   decompressor: add...
14
15
16
17
18
19
20
21
22
23
24
   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
   * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
25
26
27
   * You can contact the author at :
   *	- LZ4 homepage : http://www.lz4.org
   *	- LZ4 source repository : https://github.com/lz4/lz4
cffb78b0e   Kyungsik Lee   decompressor: add...
28
   *
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
29
30
   *	Changed for kernel usage by:
   *	Sven Schmidt <4sschmid@informatik.uni-hamburg.de>
cffb78b0e   Kyungsik Lee   decompressor: add...
31
   */
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
32
33
34
35
36
37
  /*-************************************
   *	Dependencies
   **************************************/
  #include <linux/lz4.h>
  #include "lz4defs.h"
  #include <linux/init.h>
cffb78b0e   Kyungsik Lee   decompressor: add...
38
39
  #include <linux/module.h>
  #include <linux/kernel.h>
cffb78b0e   Kyungsik Lee   decompressor: add...
40
  #include <asm/unaligned.h>
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
41
42
43
  /*-*****************************
   *	Decompression functions
   *******************************/
2209fda32   Gao Xiang   lib/lz4: update L...
44
45
46
47
48
49
50
51
52
53
54
55
  
  #define DEBUGLOG(l, ...) {}	/* disabled */
  
  #ifndef assert
  #define assert(condition) ((void)0)
  #endif
  
  /*
   * LZ4_decompress_generic() :
   * This generic decompression function covers all use cases.
   * It shall be instantiated several times, using different sets of directives.
   * Note that it is important for performance that this function really get inlined,
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
56
57
58
   * in order to remove useless branches during compilation optimization.
   */
  static FORCE_INLINE int LZ4_decompress_generic(
2209fda32   Gao Xiang   lib/lz4: update L...
59
60
61
  	 const char * const src,
  	 char * const dst,
  	 int srcSize,
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
62
63
  		/*
  		 * If endOnInput == endOnInputSize,
2209fda32   Gao Xiang   lib/lz4: update L...
64
  		 * this value is `dstCapacity`
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
65
66
67
  		 */
  	 int outputSize,
  	 /* endOnOutputSize, endOnInputSize */
2209fda32   Gao Xiang   lib/lz4: update L...
68
  	 endCondition_directive endOnInput,
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
69
  	 /* full, partial */
2209fda32   Gao Xiang   lib/lz4: update L...
70
  	 earlyEnd_directive partialDecoding,
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
71
  	 /* noDict, withPrefix64k, usingExtDict */
2209fda32   Gao Xiang   lib/lz4: update L...
72
73
  	 dict_directive dict,
  	 /* always <= dst, == dst when no prefix */
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
74
75
76
77
78
79
  	 const BYTE * const lowPrefix,
  	 /* only if dict == usingExtDict */
  	 const BYTE * const dictStart,
  	 /* note : = 0 if noDict */
  	 const size_t dictSize
  	 )
cffb78b0e   Kyungsik Lee   decompressor: add...
80
  {
2209fda32   Gao Xiang   lib/lz4: update L...
81
82
  	const BYTE *ip = (const BYTE *) src;
  	const BYTE * const iend = ip + srcSize;
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
83

2209fda32   Gao Xiang   lib/lz4: update L...
84
  	BYTE *op = (BYTE *) dst;
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
85
  	BYTE * const oend = op + outputSize;
cffb78b0e   Kyungsik Lee   decompressor: add...
86
  	BYTE *cpy;
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
87
88
  
  	const BYTE * const dictEnd = (const BYTE *)dictStart + dictSize;
2209fda32   Gao Xiang   lib/lz4: update L...
89
90
  	static const unsigned int inc32table[8] = {0, 1, 2, 1, 0, 4, 4, 4};
  	static const int dec64table[8] = {0, 0, 0, -1, -4, 1, 2, 3};
cffb78b0e   Kyungsik Lee   decompressor: add...
91

4e1a33b10   Sven Schmidt   lib: update LZ4 c...
92
93
  	const int safeDecode = (endOnInput == endOnInputSize);
  	const int checkOffset = ((safeDecode) && (dictSize < (int)(64 * KB)));
2209fda32   Gao Xiang   lib/lz4: update L...
94
95
96
97
98
99
100
101
  	/* Set up the "end" pointers for the shortcut. */
  	const BYTE *const shortiend = iend -
  		(endOnInput ? 14 : 8) /*maxLL*/ - 2 /*offset*/;
  	const BYTE *const shortoend = oend -
  		(endOnInput ? 14 : 8) /*maxLL*/ - 18 /*maxML*/;
  
  	DEBUGLOG(5, "%s (srcSize:%i, dstSize:%i)", __func__,
  		 srcSize, outputSize);
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
102
  	/* Special cases */
2209fda32   Gao Xiang   lib/lz4: update L...
103
104
  	assert(lowPrefix <= op);
  	assert(src != NULL);
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
105
106
107
  
  	/* Empty output buffer */
  	if ((endOnInput) && (unlikely(outputSize == 0)))
2209fda32   Gao Xiang   lib/lz4: update L...
108
  		return ((srcSize == 1) && (*ip == 0)) ? 0 : -1;
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
109
110
111
  
  	if ((!endOnInput) && (unlikely(outputSize == 0)))
  		return (*ip == 0 ? 1 : -1);
2209fda32   Gao Xiang   lib/lz4: update L...
112
113
  	if ((endOnInput) && unlikely(srcSize == 0))
  		return -1;
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
114
  	/* Main Loop : decode sequences */
cffb78b0e   Kyungsik Lee   decompressor: add...
115
  	while (1) {
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
116
117
118
119
120
121
  		size_t length;
  		const BYTE *match;
  		size_t offset;
  
  		/* get literal length */
  		unsigned int const token = *ip++;
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
122
  		length = token>>ML_BITS;
cffb78b0e   Kyungsik Lee   decompressor: add...
123

2209fda32   Gao Xiang   lib/lz4: update L...
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
  		/* ip < iend before the increment */
  		assert(!endOnInput || ip <= iend);
  
  		/*
  		 * A two-stage shortcut for the most common case:
  		 * 1) If the literal length is 0..14, and there is enough
  		 * space, enter the shortcut and copy 16 bytes on behalf
  		 * of the literals (in the fast mode, only 8 bytes can be
  		 * safely copied this way).
  		 * 2) Further if the match length is 4..18, copy 18 bytes
  		 * in a similar manner; but we ensure that there's enough
  		 * space in the output for those 18 bytes earlier, upon
  		 * entering the shortcut (in other words, there is a
  		 * combined check for both stages).
  		 */
  		if ((endOnInput ? length != RUN_MASK : length <= 8)
  		   /*
  		    * strictly "less than" on input, to re-enter
  		    * the loop with at least one byte
  		    */
  		   && likely((endOnInput ? ip < shortiend : 1) &
  			     (op <= shortoend))) {
  			/* Copy the literals */
  			memcpy(op, ip, endOnInput ? 16 : 8);
  			op += length; ip += length;
  
  			/*
  			 * The second stage:
  			 * prepare for match copying, decode full info.
  			 * If it doesn't work out, the info won't be wasted.
  			 */
  			length = token & ML_MASK; /* match length */
  			offset = LZ4_readLE16(ip);
  			ip += 2;
  			match = op - offset;
  			assert(match <= op); /* check overflow */
  
  			/* Do not deal with overlapping matches. */
  			if ((length != ML_MASK) &&
  			    (offset >= 8) &&
  			    (dict == withPrefix64k || match >= lowPrefix)) {
  				/* Copy the match. */
  				memcpy(op + 0, match + 0, 8);
  				memcpy(op + 8, match + 8, 8);
  				memcpy(op + 16, match + 16, 2);
  				op += length + MINMATCH;
  				/* Both stages worked, load the next token. */
  				continue;
  			}
  
  			/*
  			 * The second stage didn't work out, but the info
  			 * is ready. Propel it right to the point of match
  			 * copying.
  			 */
  			goto _copy_match;
  		}
  
  		/* decode literal length */
cffb78b0e   Kyungsik Lee   decompressor: add...
183
  		if (length == RUN_MASK) {
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
184
  			unsigned int s;
cffb78b0e   Kyungsik Lee   decompressor: add...
185

2209fda32   Gao Xiang   lib/lz4: update L...
186
187
188
189
  			if (unlikely(endOnInput ? ip >= iend - RUN_MASK : 0)) {
  				/* overflow detection */
  				goto _output_error;
  			}
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
190
191
192
193
194
195
196
197
  			do {
  				s = *ip++;
  				length += s;
  			} while (likely(endOnInput
  				? ip < iend - RUN_MASK
  				: 1) & (s == 255));
  
  			if ((safeDecode)
2209fda32   Gao Xiang   lib/lz4: update L...
198
199
  			    && unlikely((uptrval)(op) +
  					length < (uptrval)(op))) {
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
200
  				/* overflow detection */
206204a11   Greg Kroah-Hartman   lz4: ensure lengt...
201
  				goto _output_error;
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
202
203
  			}
  			if ((safeDecode)
2209fda32   Gao Xiang   lib/lz4: update L...
204
205
  			    && unlikely((uptrval)(ip) +
  					length < (uptrval)(ip))) {
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
206
207
208
  				/* overflow detection */
  				goto _output_error;
  			}
cffb78b0e   Kyungsik Lee   decompressor: add...
209
210
211
212
  		}
  
  		/* copy literals */
  		cpy = op + length;
2209fda32   Gao Xiang   lib/lz4: update L...
213
214
215
  		LZ4_STATIC_ASSERT(MFLIMIT >= WILDCOPYLENGTH);
  
  		if (((endOnInput) && ((cpy > oend - MFLIMIT)
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
216
217
218
219
220
  			|| (ip + length > iend - (2 + 1 + LASTLITERALS))))
  			|| ((!endOnInput) && (cpy > oend - WILDCOPYLENGTH))) {
  			if (partialDecoding) {
  				if (cpy > oend) {
  					/*
2209fda32   Gao Xiang   lib/lz4: update L...
221
222
  					 * Partial decoding :
  					 * stop in the middle of literal segment
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
223
  					 */
2209fda32   Gao Xiang   lib/lz4: update L...
224
225
  					cpy = oend;
  					length = oend - op;
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
  				}
  				if ((endOnInput)
  					&& (ip + length > iend)) {
  					/*
  					 * Error :
  					 * read attempt beyond
  					 * end of input buffer
  					 */
  					goto _output_error;
  				}
  			} else {
  				if ((!endOnInput)
  					&& (cpy != oend)) {
  					/*
  					 * Error :
  					 * block decoding must
  					 * stop exactly there
  					 */
  					goto _output_error;
  				}
  				if ((endOnInput)
  					&& ((ip + length != iend)
  					|| (cpy > oend))) {
  					/*
  					 * Error :
  					 * input must be consumed
  					 */
  					goto _output_error;
  				}
  			}
cffb78b0e   Kyungsik Lee   decompressor: add...
256
257
258
  
  			memcpy(op, ip, length);
  			ip += length;
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
259
  			op += length;
2209fda32   Gao Xiang   lib/lz4: update L...
260

4e1a33b10   Sven Schmidt   lib: update LZ4 c...
261
  			/* Necessarily EOF, due to parsing restrictions */
2209fda32   Gao Xiang   lib/lz4: update L...
262
263
264
265
266
267
268
  			if (!partialDecoding || (cpy == oend))
  				break;
  		} else {
  			/* may overwrite up to WILDCOPYLENGTH beyond cpy */
  			LZ4_wildCopy(op, ip, cpy);
  			ip += length;
  			op = cpy;
cffb78b0e   Kyungsik Lee   decompressor: add...
269
  		}
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
270

cffb78b0e   Kyungsik Lee   decompressor: add...
271
  		/* get offset */
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
272
  		offset = LZ4_readLE16(ip);
cffb78b0e   Kyungsik Lee   decompressor: add...
273
  		ip += 2;
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
274
  		match = op - offset;
cffb78b0e   Kyungsik Lee   decompressor: add...
275

2209fda32   Gao Xiang   lib/lz4: update L...
276
277
278
279
280
  		/* get matchlength */
  		length = token & ML_MASK;
  
  _copy_match:
  		if ((checkOffset) && (unlikely(match + dictSize < lowPrefix))) {
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
281
  			/* Error : offset outside buffers */
cffb78b0e   Kyungsik Lee   decompressor: add...
282
  			goto _output_error;
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
283
284
285
  		}
  
  		/* costs ~1%; silence an msan warning when offset == 0 */
2209fda32   Gao Xiang   lib/lz4: update L...
286
287
288
289
290
291
292
293
294
295
  		/*
  		 * note : when partialDecoding, there is no guarantee that
  		 * at least 4 bytes remain available in output buffer
  		 */
  		if (!partialDecoding) {
  			assert(oend > op);
  			assert(oend - op >= 4);
  
  			LZ4_write32(op, (U32)offset);
  		}
cffb78b0e   Kyungsik Lee   decompressor: add...
296

cffb78b0e   Kyungsik Lee   decompressor: add...
297
  		if (length == ML_MASK) {
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
298
299
300
301
302
303
304
305
306
307
308
309
310
  			unsigned int s;
  
  			do {
  				s = *ip++;
  
  				if ((endOnInput) && (ip > iend - LASTLITERALS))
  					goto _output_error;
  
  				length += s;
  			} while (s == 255);
  
  			if ((safeDecode)
  				&& unlikely(
2209fda32   Gao Xiang   lib/lz4: update L...
311
  					(uptrval)(op) + length < (uptrval)op)) {
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
312
  				/* overflow detection */
4148c1f67   Greg Kroah-Hartman   lz4: fix another ...
313
  				goto _output_error;
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
314
  			}
cffb78b0e   Kyungsik Lee   decompressor: add...
315
  		}
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
316
  		length += MINMATCH;
2209fda32   Gao Xiang   lib/lz4: update L...
317
  		/* match starting within external dictionary */
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
318
319
320
  		if ((dict == usingExtDict) && (match < lowPrefix)) {
  			if (unlikely(op + length > oend - LASTLITERALS)) {
  				/* doesn't respect parsing restriction */
2209fda32   Gao Xiang   lib/lz4: update L...
321
322
323
  				if (!partialDecoding)
  					goto _output_error;
  				length = min(length, (size_t)(oend - op));
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
324
325
326
327
  			}
  
  			if (length <= (size_t)(lowPrefix - match)) {
  				/*
2209fda32   Gao Xiang   lib/lz4: update L...
328
329
  				 * match fits entirely within external
  				 * dictionary : just copy
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
330
331
332
333
334
335
  				 */
  				memmove(op, dictEnd - (lowPrefix - match),
  					length);
  				op += length;
  			} else {
  				/*
2209fda32   Gao Xiang   lib/lz4: update L...
336
  				 * match stretches into both external
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
337
338
339
340
341
342
343
  				 * dictionary and current block
  				 */
  				size_t const copySize = (size_t)(lowPrefix - match);
  				size_t const restSize = length - copySize;
  
  				memcpy(op, dictEnd - copySize, copySize);
  				op += copySize;
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
344
345
346
347
348
349
350
351
352
353
354
355
  				if (restSize > (size_t)(op - lowPrefix)) {
  					/* overlap copy */
  					BYTE * const endOfMatch = op + restSize;
  					const BYTE *copyFrom = lowPrefix;
  
  					while (op < endOfMatch)
  						*op++ = *copyFrom++;
  				} else {
  					memcpy(op, lowPrefix, restSize);
  					op += restSize;
  				}
  			}
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
356
357
358
359
360
  			continue;
  		}
  
  		/* copy match within block */
  		cpy = op + length;
2209fda32   Gao Xiang   lib/lz4: update L...
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
  		/*
  		 * partialDecoding :
  		 * may not respect endBlock parsing restrictions
  		 */
  		assert(op <= oend);
  		if (partialDecoding &&
  		    (cpy > oend - MATCH_SAFEGUARD_DISTANCE)) {
  			size_t const mlen = min(length, (size_t)(oend - op));
  			const BYTE * const matchEnd = match + mlen;
  			BYTE * const copyEnd = op + mlen;
  
  			if (matchEnd > op) {
  				/* overlap copy */
  				while (op < copyEnd)
  					*op++ = *match++;
  			} else {
  				memcpy(op, match, mlen);
  			}
  			op = copyEnd;
  			if (op == oend)
  				break;
  			continue;
  		}
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
384

2209fda32   Gao Xiang   lib/lz4: update L...
385
  		if (unlikely(offset < 8)) {
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
386
387
388
389
  			op[0] = match[0];
  			op[1] = match[1];
  			op[2] = match[2];
  			op[3] = match[3];
2209fda32   Gao Xiang   lib/lz4: update L...
390
  			match += inc32table[offset];
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
391
  			memcpy(op + 4, match, 4);
2209fda32   Gao Xiang   lib/lz4: update L...
392
  			match -= dec64table[offset];
cffb78b0e   Kyungsik Lee   decompressor: add...
393
  		} else {
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
394
395
  			LZ4_copy8(op, match);
  			match += 8;
cffb78b0e   Kyungsik Lee   decompressor: add...
396
  		}
cffb78b0e   Kyungsik Lee   decompressor: add...
397

4e1a33b10   Sven Schmidt   lib: update LZ4 c...
398
  		op += 8;
2209fda32   Gao Xiang   lib/lz4: update L...
399
  		if (unlikely(cpy > oend - MATCH_SAFEGUARD_DISTANCE)) {
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
400
401
402
403
404
405
406
  			BYTE * const oCopyLimit = oend - (WILDCOPYLENGTH - 1);
  
  			if (cpy > oend - LASTLITERALS) {
  				/*
  				 * Error : last LASTLITERALS bytes
  				 * must be literals (uncompressed)
  				 */
d5e7cafd6   JeHyeon Yeon   LZ4 : fix the dat...
407
  				goto _output_error;
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
408
409
410
411
412
413
414
  			}
  
  			if (op < oCopyLimit) {
  				LZ4_wildCopy(op, match, oCopyLimit);
  				match += oCopyLimit - op;
  				op = oCopyLimit;
  			}
cffb78b0e   Kyungsik Lee   decompressor: add...
415
  			while (op < cpy)
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
416
417
418
  				*op++ = *match++;
  		} else {
  			LZ4_copy8(op, match);
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
419
420
  			if (length > 16)
  				LZ4_wildCopy(op + 8, match + 8, cpy);
cffb78b0e   Kyungsik Lee   decompressor: add...
421
  		}
2209fda32   Gao Xiang   lib/lz4: update L...
422
  		op = cpy; /* wildcopy correction */
cffb78b0e   Kyungsik Lee   decompressor: add...
423
  	}
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
424

cffb78b0e   Kyungsik Lee   decompressor: add...
425
  	/* end of decoding */
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
426
427
  	if (endOnInput) {
  		/* Nb of output bytes decoded */
2209fda32   Gao Xiang   lib/lz4: update L...
428
  		return (int) (((char *)op) - dst);
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
429
430
  	} else {
  		/* Nb of input bytes read */
2209fda32   Gao Xiang   lib/lz4: update L...
431
  		return (int) (((const char *)ip) - src);
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
432
  	}
cffb78b0e   Kyungsik Lee   decompressor: add...
433

4e1a33b10   Sven Schmidt   lib: update LZ4 c...
434
  	/* Overflow error detected */
cffb78b0e   Kyungsik Lee   decompressor: add...
435
  _output_error:
2209fda32   Gao Xiang   lib/lz4: update L...
436
  	return (int) (-(((const char *)ip) - src)) - 1;
cffb78b0e   Kyungsik Lee   decompressor: add...
437
  }
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
438
439
  int LZ4_decompress_safe(const char *source, char *dest,
  	int compressedSize, int maxDecompressedSize)
cffb78b0e   Kyungsik Lee   decompressor: add...
440
  {
2209fda32   Gao Xiang   lib/lz4: update L...
441
442
443
444
  	return LZ4_decompress_generic(source, dest,
  				      compressedSize, maxDecompressedSize,
  				      endOnInputSize, decode_full_block,
  				      noDict, (BYTE *)dest, NULL, 0);
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
445
  }
cffb78b0e   Kyungsik Lee   decompressor: add...
446

2209fda32   Gao Xiang   lib/lz4: update L...
447
448
  int LZ4_decompress_safe_partial(const char *src, char *dst,
  	int compressedSize, int targetOutputSize, int dstCapacity)
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
449
  {
2209fda32   Gao Xiang   lib/lz4: update L...
450
451
452
453
  	dstCapacity = min(targetOutputSize, dstCapacity);
  	return LZ4_decompress_generic(src, dst, compressedSize, dstCapacity,
  				      endOnInputSize, partial_decode,
  				      noDict, (BYTE *)dst, NULL, 0);
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
454
  }
cffb78b0e   Kyungsik Lee   decompressor: add...
455

4e1a33b10   Sven Schmidt   lib: update LZ4 c...
456
457
458
  int LZ4_decompress_fast(const char *source, char *dest, int originalSize)
  {
  	return LZ4_decompress_generic(source, dest, 0, originalSize,
2209fda32   Gao Xiang   lib/lz4: update L...
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
  				      endOnOutputSize, decode_full_block,
  				      withPrefix64k,
  				      (BYTE *)dest - 64 * KB, NULL, 0);
  }
  
  /* ===== Instantiate a few more decoding cases, used more than once. ===== */
  
  int LZ4_decompress_safe_withPrefix64k(const char *source, char *dest,
  				      int compressedSize, int maxOutputSize)
  {
  	return LZ4_decompress_generic(source, dest,
  				      compressedSize, maxOutputSize,
  				      endOnInputSize, decode_full_block,
  				      withPrefix64k,
  				      (BYTE *)dest - 64 * KB, NULL, 0);
  }
  
  static int LZ4_decompress_safe_withSmallPrefix(const char *source, char *dest,
  					       int compressedSize,
  					       int maxOutputSize,
  					       size_t prefixSize)
  {
  	return LZ4_decompress_generic(source, dest,
  				      compressedSize, maxOutputSize,
  				      endOnInputSize, decode_full_block,
  				      noDict,
  				      (BYTE *)dest - prefixSize, NULL, 0);
  }
  
  int LZ4_decompress_safe_forceExtDict(const char *source, char *dest,
  				     int compressedSize, int maxOutputSize,
  				     const void *dictStart, size_t dictSize)
  {
  	return LZ4_decompress_generic(source, dest,
  				      compressedSize, maxOutputSize,
  				      endOnInputSize, decode_full_block,
  				      usingExtDict, (BYTE *)dest,
  				      (const BYTE *)dictStart, dictSize);
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
497
  }
cffb78b0e   Kyungsik Lee   decompressor: add...
498

2209fda32   Gao Xiang   lib/lz4: update L...
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
  static int LZ4_decompress_fast_extDict(const char *source, char *dest,
  				       int originalSize,
  				       const void *dictStart, size_t dictSize)
  {
  	return LZ4_decompress_generic(source, dest,
  				      0, originalSize,
  				      endOnOutputSize, decode_full_block,
  				      usingExtDict, (BYTE *)dest,
  				      (const BYTE *)dictStart, dictSize);
  }
  
  /*
   * The "double dictionary" mode, for use with e.g. ring buffers: the first part
   * of the dictionary is passed as prefix, and the second via dictStart + dictSize.
   * These routines are used only once, in LZ4_decompress_*_continue().
   */
  static FORCE_INLINE
  int LZ4_decompress_safe_doubleDict(const char *source, char *dest,
  				   int compressedSize, int maxOutputSize,
  				   size_t prefixSize,
  				   const void *dictStart, size_t dictSize)
  {
  	return LZ4_decompress_generic(source, dest,
  				      compressedSize, maxOutputSize,
  				      endOnInputSize, decode_full_block,
  				      usingExtDict, (BYTE *)dest - prefixSize,
  				      (const BYTE *)dictStart, dictSize);
  }
  
  static FORCE_INLINE
  int LZ4_decompress_fast_doubleDict(const char *source, char *dest,
  				   int originalSize, size_t prefixSize,
  				   const void *dictStart, size_t dictSize)
  {
  	return LZ4_decompress_generic(source, dest,
  				      0, originalSize,
  				      endOnOutputSize, decode_full_block,
  				      usingExtDict, (BYTE *)dest - prefixSize,
  				      (const BYTE *)dictStart, dictSize);
  }
  
  /* ===== streaming decompression functions ===== */
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
541
542
543
  int LZ4_setStreamDecode(LZ4_streamDecode_t *LZ4_streamDecode,
  	const char *dictionary, int dictSize)
  {
2209fda32   Gao Xiang   lib/lz4: update L...
544
545
  	LZ4_streamDecode_t_internal *lz4sd =
  		&LZ4_streamDecode->internal_donotuse;
cffb78b0e   Kyungsik Lee   decompressor: add...
546

4e1a33b10   Sven Schmidt   lib: update LZ4 c...
547
548
549
550
551
552
  	lz4sd->prefixSize = (size_t) dictSize;
  	lz4sd->prefixEnd = (const BYTE *) dictionary + dictSize;
  	lz4sd->externalDict = NULL;
  	lz4sd->extDictSize	= 0;
  	return 1;
  }
cffb78b0e   Kyungsik Lee   decompressor: add...
553

4e1a33b10   Sven Schmidt   lib: update LZ4 c...
554
555
556
557
558
559
560
561
562
563
564
565
566
  /*
   * *_continue() :
   * These decoding functions allow decompression of multiple blocks
   * in "streaming" mode.
   * Previously decoded blocks must still be available at the memory
   * position where they were decoded.
   * If it's not possible, save the relevant part of
   * decoded data into a safe buffer,
   * and indicate where it stands using LZ4_setStreamDecode()
   */
  int LZ4_decompress_safe_continue(LZ4_streamDecode_t *LZ4_streamDecode,
  	const char *source, char *dest, int compressedSize, int maxOutputSize)
  {
2209fda32   Gao Xiang   lib/lz4: update L...
567
568
  	LZ4_streamDecode_t_internal *lz4sd =
  		&LZ4_streamDecode->internal_donotuse;
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
569
  	int result;
2209fda32   Gao Xiang   lib/lz4: update L...
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
  	if (lz4sd->prefixSize == 0) {
  		/* The first call, no dictionary yet. */
  		assert(lz4sd->extDictSize == 0);
  		result = LZ4_decompress_safe(source, dest,
  			compressedSize, maxOutputSize);
  		if (result <= 0)
  			return result;
  		lz4sd->prefixSize = result;
  		lz4sd->prefixEnd = (BYTE *)dest + result;
  	} else if (lz4sd->prefixEnd == (BYTE *)dest) {
  		/* They're rolling the current segment. */
  		if (lz4sd->prefixSize >= 64 * KB - 1)
  			result = LZ4_decompress_safe_withPrefix64k(source, dest,
  				compressedSize, maxOutputSize);
  		else if (lz4sd->extDictSize == 0)
  			result = LZ4_decompress_safe_withSmallPrefix(source,
  				dest, compressedSize, maxOutputSize,
  				lz4sd->prefixSize);
  		else
  			result = LZ4_decompress_safe_doubleDict(source, dest,
  				compressedSize, maxOutputSize,
  				lz4sd->prefixSize,
  				lz4sd->externalDict, lz4sd->extDictSize);
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
593
594
  		if (result <= 0)
  			return result;
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
595
  		lz4sd->prefixSize += result;
2209fda32   Gao Xiang   lib/lz4: update L...
596
  		lz4sd->prefixEnd  += result;
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
597
  	} else {
2209fda32   Gao Xiang   lib/lz4: update L...
598
599
600
601
  		/*
  		 * The buffer wraps around, or they're
  		 * switching to another buffer.
  		 */
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
602
603
  		lz4sd->extDictSize = lz4sd->prefixSize;
  		lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize;
2209fda32   Gao Xiang   lib/lz4: update L...
604
  		result = LZ4_decompress_safe_forceExtDict(source, dest,
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
605
  			compressedSize, maxOutputSize,
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
606
607
608
609
  			lz4sd->externalDict, lz4sd->extDictSize);
  		if (result <= 0)
  			return result;
  		lz4sd->prefixSize = result;
2209fda32   Gao Xiang   lib/lz4: update L...
610
  		lz4sd->prefixEnd  = (BYTE *)dest + result;
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
611
  	}
cffb78b0e   Kyungsik Lee   decompressor: add...
612

4e1a33b10   Sven Schmidt   lib: update LZ4 c...
613
614
  	return result;
  }
cffb78b0e   Kyungsik Lee   decompressor: add...
615

4e1a33b10   Sven Schmidt   lib: update LZ4 c...
616
617
618
619
620
  int LZ4_decompress_fast_continue(LZ4_streamDecode_t *LZ4_streamDecode,
  	const char *source, char *dest, int originalSize)
  {
  	LZ4_streamDecode_t_internal *lz4sd = &LZ4_streamDecode->internal_donotuse;
  	int result;
2209fda32   Gao Xiang   lib/lz4: update L...
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
  	if (lz4sd->prefixSize == 0) {
  		assert(lz4sd->extDictSize == 0);
  		result = LZ4_decompress_fast(source, dest, originalSize);
  		if (result <= 0)
  			return result;
  		lz4sd->prefixSize = originalSize;
  		lz4sd->prefixEnd = (BYTE *)dest + originalSize;
  	} else if (lz4sd->prefixEnd == (BYTE *)dest) {
  		if (lz4sd->prefixSize >= 64 * KB - 1 ||
  		    lz4sd->extDictSize == 0)
  			result = LZ4_decompress_fast(source, dest,
  						     originalSize);
  		else
  			result = LZ4_decompress_fast_doubleDict(source, dest,
  				originalSize, lz4sd->prefixSize,
  				lz4sd->externalDict, lz4sd->extDictSize);
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
637
638
  		if (result <= 0)
  			return result;
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
639
  		lz4sd->prefixSize += originalSize;
2209fda32   Gao Xiang   lib/lz4: update L...
640
  		lz4sd->prefixEnd  += originalSize;
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
641
642
643
  	} else {
  		lz4sd->extDictSize = lz4sd->prefixSize;
  		lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize;
2209fda32   Gao Xiang   lib/lz4: update L...
644
645
  		result = LZ4_decompress_fast_extDict(source, dest,
  			originalSize, lz4sd->externalDict, lz4sd->extDictSize);
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
646
647
648
  		if (result <= 0)
  			return result;
  		lz4sd->prefixSize = originalSize;
2209fda32   Gao Xiang   lib/lz4: update L...
649
  		lz4sd->prefixEnd = (BYTE *)dest + originalSize;
cffb78b0e   Kyungsik Lee   decompressor: add...
650
  	}
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
651
  	return result;
cffb78b0e   Kyungsik Lee   decompressor: add...
652
  }
2209fda32   Gao Xiang   lib/lz4: update L...
653
654
655
  int LZ4_decompress_safe_usingDict(const char *source, char *dest,
  				  int compressedSize, int maxOutputSize,
  				  const char *dictStart, int dictSize)
cffb78b0e   Kyungsik Lee   decompressor: add...
656
  {
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
657
  	if (dictSize == 0)
2209fda32   Gao Xiang   lib/lz4: update L...
658
659
660
661
662
663
664
665
  		return LZ4_decompress_safe(source, dest,
  					   compressedSize, maxOutputSize);
  	if (dictStart+dictSize == dest) {
  		if (dictSize >= 64 * KB - 1)
  			return LZ4_decompress_safe_withPrefix64k(source, dest,
  				compressedSize, maxOutputSize);
  		return LZ4_decompress_safe_withSmallPrefix(source, dest,
  			compressedSize, maxOutputSize, dictSize);
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
666
  	}
2209fda32   Gao Xiang   lib/lz4: update L...
667
668
  	return LZ4_decompress_safe_forceExtDict(source, dest,
  		compressedSize, maxOutputSize, dictStart, dictSize);
cffb78b0e   Kyungsik Lee   decompressor: add...
669
  }
cffb78b0e   Kyungsik Lee   decompressor: add...
670

4e1a33b10   Sven Schmidt   lib: update LZ4 c...
671
  int LZ4_decompress_fast_usingDict(const char *source, char *dest,
2209fda32   Gao Xiang   lib/lz4: update L...
672
673
  				  int originalSize,
  				  const char *dictStart, int dictSize)
cffb78b0e   Kyungsik Lee   decompressor: add...
674
  {
2209fda32   Gao Xiang   lib/lz4: update L...
675
676
677
678
679
  	if (dictSize == 0 || dictStart + dictSize == dest)
  		return LZ4_decompress_fast(source, dest, originalSize);
  
  	return LZ4_decompress_fast_extDict(source, dest, originalSize,
  		dictStart, dictSize);
cffb78b0e   Kyungsik Lee   decompressor: add...
680
  }
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
681

cffb78b0e   Kyungsik Lee   decompressor: add...
682
  #ifndef STATIC
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
683
684
685
686
687
688
689
690
  EXPORT_SYMBOL(LZ4_decompress_safe);
  EXPORT_SYMBOL(LZ4_decompress_safe_partial);
  EXPORT_SYMBOL(LZ4_decompress_fast);
  EXPORT_SYMBOL(LZ4_setStreamDecode);
  EXPORT_SYMBOL(LZ4_decompress_safe_continue);
  EXPORT_SYMBOL(LZ4_decompress_fast_continue);
  EXPORT_SYMBOL(LZ4_decompress_safe_usingDict);
  EXPORT_SYMBOL(LZ4_decompress_fast_usingDict);
cffb78b0e   Kyungsik Lee   decompressor: add...
691

ee8a99bdb   Richard Laager   lib/lz4: correct ...
692
  MODULE_LICENSE("Dual BSD/GPL");
4e1a33b10   Sven Schmidt   lib: update LZ4 c...
693
  MODULE_DESCRIPTION("LZ4 decompressor");
cffb78b0e   Kyungsik Lee   decompressor: add...
694
  #endif