lz4_decompress.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  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. * Dependencies
  34. **************************************/
  35. #include <linux/lz4.h>
  36. #include "lz4defs.h"
  37. #include <linux/init.h>
  38. #include <linux/module.h>
  39. #include <linux/kernel.h>
  40. #include <asm/unaligned.h>
  41. /*-*****************************
  42. * Decompression functions
  43. *******************************/
  44. #define DEBUGLOG(l, ...) {} /* disabled */
  45. #ifndef assert
  46. #define assert(condition) ((void)0)
  47. #endif
  48. /*
  49. * LZ4_decompress_generic() :
  50. * This generic decompression function covers all use cases.
  51. * It shall be instantiated several times, using different sets of directives.
  52. * Note that it is important for performance that this function really get inlined,
  53. * in order to remove useless branches during compilation optimization.
  54. */
  55. static FORCE_INLINE int LZ4_decompress_generic(
  56. const char * const src,
  57. char * const dst,
  58. int srcSize,
  59. /*
  60. * If endOnInput == endOnInputSize,
  61. * this value is `dstCapacity`
  62. */
  63. int outputSize,
  64. /* endOnOutputSize, endOnInputSize */
  65. endCondition_directive endOnInput,
  66. /* full, partial */
  67. earlyEnd_directive partialDecoding,
  68. /* noDict, withPrefix64k, usingExtDict */
  69. dict_directive dict,
  70. /* always <= dst, == dst when no prefix */
  71. const BYTE * const lowPrefix,
  72. /* only if dict == usingExtDict */
  73. const BYTE * const dictStart,
  74. /* note : = 0 if noDict */
  75. const size_t dictSize
  76. )
  77. {
  78. const BYTE *ip = (const BYTE *) src;
  79. const BYTE * const iend = ip + srcSize;
  80. BYTE *op = (BYTE *) dst;
  81. BYTE * const oend = op + outputSize;
  82. BYTE *cpy;
  83. const BYTE * const dictEnd = (const BYTE *)dictStart + dictSize;
  84. static const unsigned int inc32table[8] = {0, 1, 2, 1, 0, 4, 4, 4};
  85. static const int dec64table[8] = {0, 0, 0, -1, -4, 1, 2, 3};
  86. const int safeDecode = (endOnInput == endOnInputSize);
  87. const int checkOffset = ((safeDecode) && (dictSize < (int)(64 * KB)));
  88. /* Set up the "end" pointers for the shortcut. */
  89. const BYTE *const shortiend = iend -
  90. (endOnInput ? 14 : 8) /*maxLL*/ - 2 /*offset*/;
  91. const BYTE *const shortoend = oend -
  92. (endOnInput ? 14 : 8) /*maxLL*/ - 18 /*maxML*/;
  93. DEBUGLOG(5, "%s (srcSize:%i, dstSize:%i)", __func__,
  94. srcSize, outputSize);
  95. /* Special cases */
  96. assert(lowPrefix <= op);
  97. assert(src != NULL);
  98. /* Empty output buffer */
  99. if ((endOnInput) && (unlikely(outputSize == 0)))
  100. return ((srcSize == 1) && (*ip == 0)) ? 0 : -1;
  101. if ((!endOnInput) && (unlikely(outputSize == 0)))
  102. return (*ip == 0 ? 1 : -1);
  103. if ((endOnInput) && unlikely(srcSize == 0))
  104. return -1;
  105. /* Main Loop : decode sequences */
  106. while (1) {
  107. size_t length;
  108. const BYTE *match;
  109. size_t offset;
  110. /* get literal length */
  111. unsigned int const token = *ip++;
  112. length = token>>ML_BITS;
  113. /* ip < iend before the increment */
  114. assert(!endOnInput || ip <= iend);
  115. /*
  116. * A two-stage shortcut for the most common case:
  117. * 1) If the literal length is 0..14, and there is enough
  118. * space, enter the shortcut and copy 16 bytes on behalf
  119. * of the literals (in the fast mode, only 8 bytes can be
  120. * safely copied this way).
  121. * 2) Further if the match length is 4..18, copy 18 bytes
  122. * in a similar manner; but we ensure that there's enough
  123. * space in the output for those 18 bytes earlier, upon
  124. * entering the shortcut (in other words, there is a
  125. * combined check for both stages).
  126. *
  127. * The & in the likely() below is intentionally not && so that
  128. * some compilers can produce better parallelized runtime code
  129. */
  130. if ((endOnInput ? length != RUN_MASK : length <= 8)
  131. /*
  132. * strictly "less than" on input, to re-enter
  133. * the loop with at least one byte
  134. */
  135. && likely((endOnInput ? ip < shortiend : 1) &
  136. (op <= shortoend))) {
  137. /* Copy the literals */
  138. LZ4_memcpy(op, ip, endOnInput ? 16 : 8);
  139. op += length; ip += length;
  140. /*
  141. * The second stage:
  142. * prepare for match copying, decode full info.
  143. * If it doesn't work out, the info won't be wasted.
  144. */
  145. length = token & ML_MASK; /* match length */
  146. offset = LZ4_readLE16(ip);
  147. ip += 2;
  148. match = op - offset;
  149. assert(match <= op); /* check overflow */
  150. /* Do not deal with overlapping matches. */
  151. if ((length != ML_MASK) &&
  152. (offset >= 8) &&
  153. (dict == withPrefix64k || match >= lowPrefix)) {
  154. /* Copy the match. */
  155. LZ4_memcpy(op + 0, match + 0, 8);
  156. LZ4_memcpy(op + 8, match + 8, 8);
  157. LZ4_memcpy(op + 16, match + 16, 2);
  158. op += length + MINMATCH;
  159. /* Both stages worked, load the next token. */
  160. continue;
  161. }
  162. /*
  163. * The second stage didn't work out, but the info
  164. * is ready. Propel it right to the point of match
  165. * copying.
  166. */
  167. goto _copy_match;
  168. }
  169. /* decode literal length */
  170. if (length == RUN_MASK) {
  171. unsigned int s;
  172. if (unlikely(endOnInput ? ip >= iend - RUN_MASK : 0)) {
  173. /* overflow detection */
  174. goto _output_error;
  175. }
  176. do {
  177. s = *ip++;
  178. length += s;
  179. } while (likely(endOnInput
  180. ? ip < iend - RUN_MASK
  181. : 1) & (s == 255));
  182. if ((safeDecode)
  183. && unlikely((uptrval)(op) +
  184. length < (uptrval)(op))) {
  185. /* overflow detection */
  186. goto _output_error;
  187. }
  188. if ((safeDecode)
  189. && unlikely((uptrval)(ip) +
  190. length < (uptrval)(ip))) {
  191. /* overflow detection */
  192. goto _output_error;
  193. }
  194. }
  195. /* copy literals */
  196. cpy = op + length;
  197. LZ4_STATIC_ASSERT(MFLIMIT >= WILDCOPYLENGTH);
  198. if (((endOnInput) && ((cpy > oend - MFLIMIT)
  199. || (ip + length > iend - (2 + 1 + LASTLITERALS))))
  200. || ((!endOnInput) && (cpy > oend - WILDCOPYLENGTH))) {
  201. if (partialDecoding) {
  202. if (cpy > oend) {
  203. /*
  204. * Partial decoding :
  205. * stop in the middle of literal segment
  206. */
  207. cpy = oend;
  208. length = oend - op;
  209. }
  210. if ((endOnInput)
  211. && (ip + length > iend)) {
  212. /*
  213. * Error :
  214. * read attempt beyond
  215. * end of input buffer
  216. */
  217. goto _output_error;
  218. }
  219. } else {
  220. if ((!endOnInput)
  221. && (cpy != oend)) {
  222. /*
  223. * Error :
  224. * block decoding must
  225. * stop exactly there
  226. */
  227. goto _output_error;
  228. }
  229. if ((endOnInput)
  230. && ((ip + length != iend)
  231. || (cpy > oend))) {
  232. /*
  233. * Error :
  234. * input must be consumed
  235. */
  236. goto _output_error;
  237. }
  238. }
  239. /*
  240. * supports overlapping memory regions; only matters
  241. * for in-place decompression scenarios
  242. */
  243. LZ4_memmove(op, ip, length);
  244. ip += length;
  245. op += length;
  246. /* Necessarily EOF, due to parsing restrictions */
  247. if (!partialDecoding || (cpy == oend))
  248. break;
  249. } else {
  250. /* may overwrite up to WILDCOPYLENGTH beyond cpy */
  251. LZ4_wildCopy(op, ip, cpy);
  252. ip += length;
  253. op = cpy;
  254. }
  255. /* get offset */
  256. offset = LZ4_readLE16(ip);
  257. ip += 2;
  258. match = op - offset;
  259. /* get matchlength */
  260. length = token & ML_MASK;
  261. _copy_match:
  262. if ((checkOffset) && (unlikely(match + dictSize < lowPrefix))) {
  263. /* Error : offset outside buffers */
  264. goto _output_error;
  265. }
  266. /* costs ~1%; silence an msan warning when offset == 0 */
  267. /*
  268. * note : when partialDecoding, there is no guarantee that
  269. * at least 4 bytes remain available in output buffer
  270. */
  271. if (!partialDecoding) {
  272. assert(oend > op);
  273. assert(oend - op >= 4);
  274. LZ4_write32(op, (U32)offset);
  275. }
  276. if (length == ML_MASK) {
  277. unsigned int s;
  278. do {
  279. s = *ip++;
  280. if ((endOnInput) && (ip > iend - LASTLITERALS))
  281. goto _output_error;
  282. length += s;
  283. } while (s == 255);
  284. if ((safeDecode)
  285. && unlikely(
  286. (uptrval)(op) + length < (uptrval)op)) {
  287. /* overflow detection */
  288. goto _output_error;
  289. }
  290. }
  291. length += MINMATCH;
  292. /* match starting within external dictionary */
  293. if ((dict == usingExtDict) && (match < lowPrefix)) {
  294. if (unlikely(op + length > oend - LASTLITERALS)) {
  295. /* doesn't respect parsing restriction */
  296. if (!partialDecoding)
  297. goto _output_error;
  298. length = min(length, (size_t)(oend - op));
  299. }
  300. if (length <= (size_t)(lowPrefix - match)) {
  301. /*
  302. * match fits entirely within external
  303. * dictionary : just copy
  304. */
  305. memmove(op, dictEnd - (lowPrefix - match),
  306. length);
  307. op += length;
  308. } else {
  309. /*
  310. * match stretches into both external
  311. * dictionary and current block
  312. */
  313. size_t const copySize = (size_t)(lowPrefix - match);
  314. size_t const restSize = length - copySize;
  315. LZ4_memcpy(op, dictEnd - copySize, copySize);
  316. op += copySize;
  317. if (restSize > (size_t)(op - lowPrefix)) {
  318. /* overlap copy */
  319. BYTE * const endOfMatch = op + restSize;
  320. const BYTE *copyFrom = lowPrefix;
  321. while (op < endOfMatch)
  322. *op++ = *copyFrom++;
  323. } else {
  324. LZ4_memcpy(op, lowPrefix, restSize);
  325. op += restSize;
  326. }
  327. }
  328. continue;
  329. }
  330. /* copy match within block */
  331. cpy = op + length;
  332. /*
  333. * partialDecoding :
  334. * may not respect endBlock parsing restrictions
  335. */
  336. assert(op <= oend);
  337. if (partialDecoding &&
  338. (cpy > oend - MATCH_SAFEGUARD_DISTANCE)) {
  339. size_t const mlen = min(length, (size_t)(oend - op));
  340. const BYTE * const matchEnd = match + mlen;
  341. BYTE * const copyEnd = op + mlen;
  342. if (matchEnd > op) {
  343. /* overlap copy */
  344. while (op < copyEnd)
  345. *op++ = *match++;
  346. } else {
  347. LZ4_memcpy(op, match, mlen);
  348. }
  349. op = copyEnd;
  350. if (op == oend)
  351. break;
  352. continue;
  353. }
  354. if (unlikely(offset < 8)) {
  355. op[0] = match[0];
  356. op[1] = match[1];
  357. op[2] = match[2];
  358. op[3] = match[3];
  359. match += inc32table[offset];
  360. LZ4_memcpy(op + 4, match, 4);
  361. match -= dec64table[offset];
  362. } else {
  363. LZ4_copy8(op, match);
  364. match += 8;
  365. }
  366. op += 8;
  367. if (unlikely(cpy > oend - MATCH_SAFEGUARD_DISTANCE)) {
  368. BYTE * const oCopyLimit = oend - (WILDCOPYLENGTH - 1);
  369. if (cpy > oend - LASTLITERALS) {
  370. /*
  371. * Error : last LASTLITERALS bytes
  372. * must be literals (uncompressed)
  373. */
  374. goto _output_error;
  375. }
  376. if (op < oCopyLimit) {
  377. LZ4_wildCopy(op, match, oCopyLimit);
  378. match += oCopyLimit - op;
  379. op = oCopyLimit;
  380. }
  381. while (op < cpy)
  382. *op++ = *match++;
  383. } else {
  384. LZ4_copy8(op, match);
  385. if (length > 16)
  386. LZ4_wildCopy(op + 8, match + 8, cpy);
  387. }
  388. op = cpy; /* wildcopy correction */
  389. }
  390. /* end of decoding */
  391. if (endOnInput) {
  392. /* Nb of output bytes decoded */
  393. return (int) (((char *)op) - dst);
  394. } else {
  395. /* Nb of input bytes read */
  396. return (int) (((const char *)ip) - src);
  397. }
  398. /* Overflow error detected */
  399. _output_error:
  400. return (int) (-(((const char *)ip) - src)) - 1;
  401. }
  402. int LZ4_decompress_safe(const char *source, char *dest,
  403. int compressedSize, int maxDecompressedSize)
  404. {
  405. return LZ4_decompress_generic(source, dest,
  406. compressedSize, maxDecompressedSize,
  407. endOnInputSize, decode_full_block,
  408. noDict, (BYTE *)dest, NULL, 0);
  409. }
  410. int LZ4_decompress_safe_partial(const char *src, char *dst,
  411. int compressedSize, int targetOutputSize, int dstCapacity)
  412. {
  413. dstCapacity = min(targetOutputSize, dstCapacity);
  414. return LZ4_decompress_generic(src, dst, compressedSize, dstCapacity,
  415. endOnInputSize, partial_decode,
  416. noDict, (BYTE *)dst, NULL, 0);
  417. }
  418. int LZ4_decompress_fast(const char *source, char *dest, int originalSize)
  419. {
  420. return LZ4_decompress_generic(source, dest, 0, originalSize,
  421. endOnOutputSize, decode_full_block,
  422. withPrefix64k,
  423. (BYTE *)dest - 64 * KB, NULL, 0);
  424. }
  425. /* ===== Instantiate a few more decoding cases, used more than once. ===== */
  426. int LZ4_decompress_safe_withPrefix64k(const char *source, char *dest,
  427. int compressedSize, int maxOutputSize)
  428. {
  429. return LZ4_decompress_generic(source, dest,
  430. compressedSize, maxOutputSize,
  431. endOnInputSize, decode_full_block,
  432. withPrefix64k,
  433. (BYTE *)dest - 64 * KB, NULL, 0);
  434. }
  435. static int LZ4_decompress_safe_withSmallPrefix(const char *source, char *dest,
  436. int compressedSize,
  437. int maxOutputSize,
  438. size_t prefixSize)
  439. {
  440. return LZ4_decompress_generic(source, dest,
  441. compressedSize, maxOutputSize,
  442. endOnInputSize, decode_full_block,
  443. noDict,
  444. (BYTE *)dest - prefixSize, NULL, 0);
  445. }
  446. int LZ4_decompress_safe_forceExtDict(const char *source, char *dest,
  447. int compressedSize, int maxOutputSize,
  448. const void *dictStart, size_t dictSize)
  449. {
  450. return LZ4_decompress_generic(source, dest,
  451. compressedSize, maxOutputSize,
  452. endOnInputSize, decode_full_block,
  453. usingExtDict, (BYTE *)dest,
  454. (const BYTE *)dictStart, dictSize);
  455. }
  456. static int LZ4_decompress_fast_extDict(const char *source, char *dest,
  457. int originalSize,
  458. const void *dictStart, size_t dictSize)
  459. {
  460. return LZ4_decompress_generic(source, dest,
  461. 0, originalSize,
  462. endOnOutputSize, decode_full_block,
  463. usingExtDict, (BYTE *)dest,
  464. (const BYTE *)dictStart, dictSize);
  465. }
  466. /*
  467. * The "double dictionary" mode, for use with e.g. ring buffers: the first part
  468. * of the dictionary is passed as prefix, and the second via dictStart + dictSize.
  469. * These routines are used only once, in LZ4_decompress_*_continue().
  470. */
  471. static FORCE_INLINE
  472. int LZ4_decompress_safe_doubleDict(const char *source, char *dest,
  473. int compressedSize, int maxOutputSize,
  474. size_t prefixSize,
  475. const void *dictStart, size_t dictSize)
  476. {
  477. return LZ4_decompress_generic(source, dest,
  478. compressedSize, maxOutputSize,
  479. endOnInputSize, decode_full_block,
  480. usingExtDict, (BYTE *)dest - prefixSize,
  481. (const BYTE *)dictStart, dictSize);
  482. }
  483. static FORCE_INLINE
  484. int LZ4_decompress_fast_doubleDict(const char *source, char *dest,
  485. int originalSize, size_t prefixSize,
  486. const void *dictStart, size_t dictSize)
  487. {
  488. return LZ4_decompress_generic(source, dest,
  489. 0, originalSize,
  490. endOnOutputSize, decode_full_block,
  491. usingExtDict, (BYTE *)dest - prefixSize,
  492. (const BYTE *)dictStart, dictSize);
  493. }
  494. /* ===== streaming decompression functions ===== */
  495. int LZ4_setStreamDecode(LZ4_streamDecode_t *LZ4_streamDecode,
  496. const char *dictionary, int dictSize)
  497. {
  498. LZ4_streamDecode_t_internal *lz4sd =
  499. &LZ4_streamDecode->internal_donotuse;
  500. lz4sd->prefixSize = (size_t) dictSize;
  501. lz4sd->prefixEnd = (const BYTE *) dictionary + dictSize;
  502. lz4sd->externalDict = NULL;
  503. lz4sd->extDictSize = 0;
  504. return 1;
  505. }
  506. /*
  507. * *_continue() :
  508. * These decoding functions allow decompression of multiple blocks
  509. * in "streaming" mode.
  510. * Previously decoded blocks must still be available at the memory
  511. * position where they were decoded.
  512. * If it's not possible, save the relevant part of
  513. * decoded data into a safe buffer,
  514. * and indicate where it stands using LZ4_setStreamDecode()
  515. */
  516. int LZ4_decompress_safe_continue(LZ4_streamDecode_t *LZ4_streamDecode,
  517. const char *source, char *dest, int compressedSize, int maxOutputSize)
  518. {
  519. LZ4_streamDecode_t_internal *lz4sd =
  520. &LZ4_streamDecode->internal_donotuse;
  521. int result;
  522. if (lz4sd->prefixSize == 0) {
  523. /* The first call, no dictionary yet. */
  524. assert(lz4sd->extDictSize == 0);
  525. result = LZ4_decompress_safe(source, dest,
  526. compressedSize, maxOutputSize);
  527. if (result <= 0)
  528. return result;
  529. lz4sd->prefixSize = result;
  530. lz4sd->prefixEnd = (BYTE *)dest + result;
  531. } else if (lz4sd->prefixEnd == (BYTE *)dest) {
  532. /* They're rolling the current segment. */
  533. if (lz4sd->prefixSize >= 64 * KB - 1)
  534. result = LZ4_decompress_safe_withPrefix64k(source, dest,
  535. compressedSize, maxOutputSize);
  536. else if (lz4sd->extDictSize == 0)
  537. result = LZ4_decompress_safe_withSmallPrefix(source,
  538. dest, compressedSize, maxOutputSize,
  539. lz4sd->prefixSize);
  540. else
  541. result = LZ4_decompress_safe_doubleDict(source, dest,
  542. compressedSize, maxOutputSize,
  543. lz4sd->prefixSize,
  544. lz4sd->externalDict, lz4sd->extDictSize);
  545. if (result <= 0)
  546. return result;
  547. lz4sd->prefixSize += result;
  548. lz4sd->prefixEnd += result;
  549. } else {
  550. /*
  551. * The buffer wraps around, or they're
  552. * switching to another buffer.
  553. */
  554. lz4sd->extDictSize = lz4sd->prefixSize;
  555. lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize;
  556. result = LZ4_decompress_safe_forceExtDict(source, dest,
  557. compressedSize, maxOutputSize,
  558. lz4sd->externalDict, lz4sd->extDictSize);
  559. if (result <= 0)
  560. return result;
  561. lz4sd->prefixSize = result;
  562. lz4sd->prefixEnd = (BYTE *)dest + result;
  563. }
  564. return result;
  565. }
  566. int LZ4_decompress_fast_continue(LZ4_streamDecode_t *LZ4_streamDecode,
  567. const char *source, char *dest, int originalSize)
  568. {
  569. LZ4_streamDecode_t_internal *lz4sd = &LZ4_streamDecode->internal_donotuse;
  570. int result;
  571. if (lz4sd->prefixSize == 0) {
  572. assert(lz4sd->extDictSize == 0);
  573. result = LZ4_decompress_fast(source, dest, originalSize);
  574. if (result <= 0)
  575. return result;
  576. lz4sd->prefixSize = originalSize;
  577. lz4sd->prefixEnd = (BYTE *)dest + originalSize;
  578. } else if (lz4sd->prefixEnd == (BYTE *)dest) {
  579. if (lz4sd->prefixSize >= 64 * KB - 1 ||
  580. lz4sd->extDictSize == 0)
  581. result = LZ4_decompress_fast(source, dest,
  582. originalSize);
  583. else
  584. result = LZ4_decompress_fast_doubleDict(source, dest,
  585. originalSize, lz4sd->prefixSize,
  586. lz4sd->externalDict, lz4sd->extDictSize);
  587. if (result <= 0)
  588. return result;
  589. lz4sd->prefixSize += originalSize;
  590. lz4sd->prefixEnd += originalSize;
  591. } else {
  592. lz4sd->extDictSize = lz4sd->prefixSize;
  593. lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize;
  594. result = LZ4_decompress_fast_extDict(source, dest,
  595. originalSize, lz4sd->externalDict, lz4sd->extDictSize);
  596. if (result <= 0)
  597. return result;
  598. lz4sd->prefixSize = originalSize;
  599. lz4sd->prefixEnd = (BYTE *)dest + originalSize;
  600. }
  601. return result;
  602. }
  603. int LZ4_decompress_safe_usingDict(const char *source, char *dest,
  604. int compressedSize, int maxOutputSize,
  605. const char *dictStart, int dictSize)
  606. {
  607. if (dictSize == 0)
  608. return LZ4_decompress_safe(source, dest,
  609. compressedSize, maxOutputSize);
  610. if (dictStart+dictSize == dest) {
  611. if (dictSize >= 64 * KB - 1)
  612. return LZ4_decompress_safe_withPrefix64k(source, dest,
  613. compressedSize, maxOutputSize);
  614. return LZ4_decompress_safe_withSmallPrefix(source, dest,
  615. compressedSize, maxOutputSize, dictSize);
  616. }
  617. return LZ4_decompress_safe_forceExtDict(source, dest,
  618. compressedSize, maxOutputSize, dictStart, dictSize);
  619. }
  620. int LZ4_decompress_fast_usingDict(const char *source, char *dest,
  621. int originalSize,
  622. const char *dictStart, int dictSize)
  623. {
  624. if (dictSize == 0 || dictStart + dictSize == dest)
  625. return LZ4_decompress_fast(source, dest, originalSize);
  626. return LZ4_decompress_fast_extDict(source, dest, originalSize,
  627. dictStart, dictSize);
  628. }
  629. /*-******************************
  630. * For backwards compatibility
  631. ********************************/
  632. int lz4_decompress_unknownoutputsize(const unsigned char *src,
  633. size_t src_len, unsigned char *dest, size_t *dest_len) {
  634. *dest_len = LZ4_decompress_safe(src, dest,
  635. src_len, *dest_len);
  636. /*
  637. * Prior lz4_decompress_unknownoutputsize will return
  638. * 0 for success and a negative result for error
  639. * new LZ4_decompress_safe returns
  640. * - the length of data read on success
  641. * - and also a negative result on error
  642. * meaning when result > 0, we just return 0 here
  643. */
  644. if (src_len > 0)
  645. return 0;
  646. else
  647. return -1;
  648. }
  649. int lz4_decompress(const unsigned char *src, size_t *src_len,
  650. unsigned char *dest, size_t actual_dest_len) {
  651. *src_len = LZ4_decompress_fast(src, dest, actual_dest_len);
  652. /*
  653. * Prior lz4_decompress will return
  654. * 0 for success and a negative result for error
  655. * new LZ4_decompress_fast returns
  656. * - the length of data read on success
  657. * - and also a negative result on error
  658. * meaning when result > 0, we just return 0 here
  659. */
  660. if (*src_len > 0)
  661. return 0;
  662. else
  663. return -1;
  664. }
  665. #ifndef STATIC
  666. EXPORT_SYMBOL(LZ4_decompress_safe);
  667. EXPORT_SYMBOL(LZ4_decompress_safe_partial);
  668. EXPORT_SYMBOL(LZ4_decompress_fast);
  669. EXPORT_SYMBOL(LZ4_setStreamDecode);
  670. EXPORT_SYMBOL(LZ4_decompress_safe_continue);
  671. EXPORT_SYMBOL(LZ4_decompress_fast_continue);
  672. EXPORT_SYMBOL(LZ4_decompress_safe_usingDict);
  673. EXPORT_SYMBOL(LZ4_decompress_fast_usingDict);
  674. EXPORT_SYMBOL(lz4_decompress_unknownoutputsize);
  675. EXPORT_SYMBOL(lz4_decompress);
  676. MODULE_LICENSE("Dual BSD/GPL");
  677. MODULE_DESCRIPTION("LZ4 decompressor");
  678. #endif