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