lz4_decompress.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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 <[email protected]>
  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. /* LZ4_decompress_generic() :
  45. * This generic decompression function cover all use cases.
  46. * It shall be instantiated several times, using different sets of directives
  47. * Note that it is important this generic function is really inlined,
  48. * in order to remove useless branches during compilation optimization.
  49. */
  50. static FORCE_INLINE int LZ4_decompress_generic(
  51. const char * const source,
  52. char * const dest,
  53. int inputSize,
  54. /*
  55. * If endOnInput == endOnInputSize,
  56. * this value is the max size of Output Buffer.
  57. */
  58. int outputSize,
  59. /* endOnOutputSize, endOnInputSize */
  60. int endOnInput,
  61. /* full, partial */
  62. int partialDecoding,
  63. /* only used if partialDecoding == partial */
  64. int targetOutputSize,
  65. /* noDict, withPrefix64k, usingExtDict */
  66. int dict,
  67. /* == dest when no prefix */
  68. const BYTE * const lowPrefix,
  69. /* only if dict == usingExtDict */
  70. const BYTE * const dictStart,
  71. /* note : = 0 if noDict */
  72. const size_t dictSize
  73. )
  74. {
  75. /* Local Variables */
  76. const BYTE *ip = (const BYTE *) source;
  77. const BYTE * const iend = ip + inputSize;
  78. BYTE *op = (BYTE *) dest;
  79. BYTE * const oend = op + outputSize;
  80. BYTE *cpy;
  81. BYTE *oexit = op + targetOutputSize;
  82. const BYTE * const lowLimit = lowPrefix - dictSize;
  83. const BYTE * const dictEnd = (const BYTE *)dictStart + dictSize;
  84. const unsigned int dec32table[] = { 0, 1, 2, 1, 4, 4, 4, 4 };
  85. const int dec64table[] = { 0, 0, 0, -1, 0, 1, 2, 3 };
  86. const int safeDecode = (endOnInput == endOnInputSize);
  87. const int checkOffset = ((safeDecode) && (dictSize < (int)(64 * KB)));
  88. /* Special cases */
  89. /* targetOutputSize too high => decode everything */
  90. if ((partialDecoding) && (oexit > oend - MFLIMIT))
  91. oexit = oend - MFLIMIT;
  92. /* Empty output buffer */
  93. if ((endOnInput) && (unlikely(outputSize == 0)))
  94. return ((inputSize == 1) && (*ip == 0)) ? 0 : -1;
  95. if ((!endOnInput) && (unlikely(outputSize == 0)))
  96. return (*ip == 0 ? 1 : -1);
  97. /* Main Loop : decode sequences */
  98. while (1) {
  99. size_t length;
  100. const BYTE *match;
  101. size_t offset;
  102. /* get literal length */
  103. unsigned int const token = *ip++;
  104. length = token>>ML_BITS;
  105. if (length == RUN_MASK) {
  106. unsigned int s;
  107. do {
  108. s = *ip++;
  109. length += s;
  110. } while (likely(endOnInput
  111. ? ip < iend - RUN_MASK
  112. : 1) & (s == 255));
  113. if ((safeDecode)
  114. && unlikely(
  115. (size_t)(op + length) < (size_t)(op))) {
  116. /* overflow detection */
  117. goto _output_error;
  118. }
  119. if ((safeDecode)
  120. && unlikely(
  121. (size_t)(ip + length) < (size_t)(ip))) {
  122. /* overflow detection */
  123. goto _output_error;
  124. }
  125. }
  126. /* copy literals */
  127. cpy = op + length;
  128. if (((endOnInput) && ((cpy > (partialDecoding ? oexit : oend - MFLIMIT))
  129. || (ip + length > iend - (2 + 1 + LASTLITERALS))))
  130. || ((!endOnInput) && (cpy > oend - WILDCOPYLENGTH))) {
  131. if (partialDecoding) {
  132. if (cpy > oend) {
  133. /*
  134. * Error :
  135. * write attempt beyond end of output buffer
  136. */
  137. goto _output_error;
  138. }
  139. if ((endOnInput)
  140. && (ip + length > iend)) {
  141. /*
  142. * Error :
  143. * read attempt beyond
  144. * end of input buffer
  145. */
  146. goto _output_error;
  147. }
  148. } else {
  149. if ((!endOnInput)
  150. && (cpy != oend)) {
  151. /*
  152. * Error :
  153. * block decoding must
  154. * stop exactly there
  155. */
  156. goto _output_error;
  157. }
  158. if ((endOnInput)
  159. && ((ip + length != iend)
  160. || (cpy > oend))) {
  161. /*
  162. * Error :
  163. * input must be consumed
  164. */
  165. goto _output_error;
  166. }
  167. }
  168. memcpy(op, ip, length);
  169. ip += length;
  170. op += length;
  171. /* Necessarily EOF, due to parsing restrictions */
  172. break;
  173. }
  174. LZ4_wildCopy(op, ip, cpy);
  175. ip += length;
  176. op = cpy;
  177. /* get offset */
  178. offset = LZ4_readLE16(ip);
  179. ip += 2;
  180. match = op - offset;
  181. if ((checkOffset) && (unlikely(match < lowLimit))) {
  182. /* Error : offset outside buffers */
  183. goto _output_error;
  184. }
  185. /* costs ~1%; silence an msan warning when offset == 0 */
  186. LZ4_write32(op, (U32)offset);
  187. /* get matchlength */
  188. length = token & ML_MASK;
  189. if (length == ML_MASK) {
  190. unsigned int s;
  191. do {
  192. s = *ip++;
  193. if ((endOnInput) && (ip > iend - LASTLITERALS))
  194. goto _output_error;
  195. length += s;
  196. } while (s == 255);
  197. if ((safeDecode)
  198. && unlikely(
  199. (size_t)(op + length) < (size_t)op)) {
  200. /* overflow detection */
  201. goto _output_error;
  202. }
  203. }
  204. length += MINMATCH;
  205. /* check external dictionary */
  206. if ((dict == usingExtDict) && (match < lowPrefix)) {
  207. if (unlikely(op + length > oend - LASTLITERALS)) {
  208. /* doesn't respect parsing restriction */
  209. goto _output_error;
  210. }
  211. if (length <= (size_t)(lowPrefix - match)) {
  212. /*
  213. * match can be copied as a single segment
  214. * from external dictionary
  215. */
  216. memmove(op, dictEnd - (lowPrefix - match),
  217. length);
  218. op += length;
  219. } else {
  220. /*
  221. * match encompass external
  222. * dictionary and current block
  223. */
  224. size_t const copySize = (size_t)(lowPrefix - match);
  225. size_t const restSize = length - copySize;
  226. memcpy(op, dictEnd - copySize, copySize);
  227. op += copySize;
  228. if (restSize > (size_t)(op - lowPrefix)) {
  229. /* overlap copy */
  230. BYTE * const endOfMatch = op + restSize;
  231. const BYTE *copyFrom = lowPrefix;
  232. while (op < endOfMatch)
  233. *op++ = *copyFrom++;
  234. } else {
  235. memcpy(op, lowPrefix, restSize);
  236. op += restSize;
  237. }
  238. }
  239. continue;
  240. }
  241. /* copy match within block */
  242. cpy = op + length;
  243. if (unlikely(offset < 8)) {
  244. const int dec64 = dec64table[offset];
  245. op[0] = match[0];
  246. op[1] = match[1];
  247. op[2] = match[2];
  248. op[3] = match[3];
  249. match += dec32table[offset];
  250. memcpy(op + 4, match, 4);
  251. match -= dec64;
  252. } else {
  253. LZ4_copy8(op, match);
  254. match += 8;
  255. }
  256. op += 8;
  257. if (unlikely(cpy > oend - 12)) {
  258. BYTE * const oCopyLimit = oend - (WILDCOPYLENGTH - 1);
  259. if (cpy > oend - LASTLITERALS) {
  260. /*
  261. * Error : last LASTLITERALS bytes
  262. * must be literals (uncompressed)
  263. */
  264. goto _output_error;
  265. }
  266. if (op < oCopyLimit) {
  267. LZ4_wildCopy(op, match, oCopyLimit);
  268. match += oCopyLimit - op;
  269. op = oCopyLimit;
  270. }
  271. while (op < cpy)
  272. *op++ = *match++;
  273. } else {
  274. LZ4_copy8(op, match);
  275. if (length > 16)
  276. LZ4_wildCopy(op + 8, match + 8, cpy);
  277. }
  278. op = cpy; /* correction */
  279. }
  280. /* end of decoding */
  281. if (endOnInput) {
  282. /* Nb of output bytes decoded */
  283. return (int) (((char *)op) - dest);
  284. } else {
  285. /* Nb of input bytes read */
  286. return (int) (((const char *)ip) - source);
  287. }
  288. /* Overflow error detected */
  289. _output_error:
  290. return -1;
  291. }
  292. int LZ4_decompress_safe(const char *source, char *dest,
  293. int compressedSize, int maxDecompressedSize)
  294. {
  295. return LZ4_decompress_generic(source, dest, compressedSize,
  296. maxDecompressedSize, endOnInputSize, full, 0,
  297. noDict, (BYTE *)dest, NULL, 0);
  298. }
  299. int LZ4_decompress_safe_partial(const char *source, char *dest,
  300. int compressedSize, int targetOutputSize, int maxDecompressedSize)
  301. {
  302. return LZ4_decompress_generic(source, dest, compressedSize,
  303. maxDecompressedSize, endOnInputSize, partial,
  304. targetOutputSize, noDict, (BYTE *)dest, NULL, 0);
  305. }
  306. int LZ4_decompress_fast(const char *source, char *dest, int originalSize)
  307. {
  308. return LZ4_decompress_generic(source, dest, 0, originalSize,
  309. endOnOutputSize, full, 0, withPrefix64k,
  310. (BYTE *)(dest - 64 * KB), NULL, 64 * KB);
  311. }
  312. int LZ4_setStreamDecode(LZ4_streamDecode_t *LZ4_streamDecode,
  313. const char *dictionary, int dictSize)
  314. {
  315. LZ4_streamDecode_t_internal *lz4sd = (LZ4_streamDecode_t_internal *) LZ4_streamDecode;
  316. lz4sd->prefixSize = (size_t) dictSize;
  317. lz4sd->prefixEnd = (const BYTE *) dictionary + dictSize;
  318. lz4sd->externalDict = NULL;
  319. lz4sd->extDictSize = 0;
  320. return 1;
  321. }
  322. /*
  323. * *_continue() :
  324. * These decoding functions allow decompression of multiple blocks
  325. * in "streaming" mode.
  326. * Previously decoded blocks must still be available at the memory
  327. * position where they were decoded.
  328. * If it's not possible, save the relevant part of
  329. * decoded data into a safe buffer,
  330. * and indicate where it stands using LZ4_setStreamDecode()
  331. */
  332. int LZ4_decompress_safe_continue(LZ4_streamDecode_t *LZ4_streamDecode,
  333. const char *source, char *dest, int compressedSize, int maxOutputSize)
  334. {
  335. LZ4_streamDecode_t_internal *lz4sd = &LZ4_streamDecode->internal_donotuse;
  336. int result;
  337. if (lz4sd->prefixEnd == (BYTE *)dest) {
  338. result = LZ4_decompress_generic(source, dest,
  339. compressedSize,
  340. maxOutputSize,
  341. endOnInputSize, full, 0,
  342. usingExtDict, lz4sd->prefixEnd - lz4sd->prefixSize,
  343. lz4sd->externalDict,
  344. lz4sd->extDictSize);
  345. if (result <= 0)
  346. return result;
  347. lz4sd->prefixSize += result;
  348. lz4sd->prefixEnd += result;
  349. } else {
  350. lz4sd->extDictSize = lz4sd->prefixSize;
  351. lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize;
  352. result = LZ4_decompress_generic(source, dest,
  353. compressedSize, maxOutputSize,
  354. endOnInputSize, full, 0,
  355. usingExtDict, (BYTE *)dest,
  356. lz4sd->externalDict, lz4sd->extDictSize);
  357. if (result <= 0)
  358. return result;
  359. lz4sd->prefixSize = result;
  360. lz4sd->prefixEnd = (BYTE *)dest + result;
  361. }
  362. return result;
  363. }
  364. int LZ4_decompress_fast_continue(LZ4_streamDecode_t *LZ4_streamDecode,
  365. const char *source, char *dest, int originalSize)
  366. {
  367. LZ4_streamDecode_t_internal *lz4sd = &LZ4_streamDecode->internal_donotuse;
  368. int result;
  369. if (lz4sd->prefixEnd == (BYTE *)dest) {
  370. result = LZ4_decompress_generic(source, dest, 0, originalSize,
  371. endOnOutputSize, full, 0,
  372. usingExtDict,
  373. lz4sd->prefixEnd - lz4sd->prefixSize,
  374. lz4sd->externalDict, lz4sd->extDictSize);
  375. if (result <= 0)
  376. return result;
  377. lz4sd->prefixSize += originalSize;
  378. lz4sd->prefixEnd += originalSize;
  379. } else {
  380. lz4sd->extDictSize = lz4sd->prefixSize;
  381. lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize;
  382. result = LZ4_decompress_generic(source, dest, 0, originalSize,
  383. endOnOutputSize, full, 0,
  384. usingExtDict, (BYTE *)dest,
  385. lz4sd->externalDict, lz4sd->extDictSize);
  386. if (result <= 0)
  387. return result;
  388. lz4sd->prefixSize = originalSize;
  389. lz4sd->prefixEnd = (BYTE *)dest + originalSize;
  390. }
  391. return result;
  392. }
  393. /*
  394. * Advanced decoding functions :
  395. * *_usingDict() :
  396. * These decoding functions work the same as "_continue" ones,
  397. * the dictionary must be explicitly provided within parameters
  398. */
  399. static FORCE_INLINE int LZ4_decompress_usingDict_generic(const char *source,
  400. char *dest, int compressedSize, int maxOutputSize, int safe,
  401. const char *dictStart, int dictSize)
  402. {
  403. if (dictSize == 0)
  404. return LZ4_decompress_generic(source, dest,
  405. compressedSize, maxOutputSize, safe, full, 0,
  406. noDict, (BYTE *)dest, NULL, 0);
  407. if (dictStart + dictSize == dest) {
  408. if (dictSize >= (int)(64 * KB - 1))
  409. return LZ4_decompress_generic(source, dest,
  410. compressedSize, maxOutputSize, safe, full, 0,
  411. withPrefix64k, (BYTE *)dest - 64 * KB, NULL, 0);
  412. return LZ4_decompress_generic(source, dest, compressedSize,
  413. maxOutputSize, safe, full, 0, noDict,
  414. (BYTE *)dest - dictSize, NULL, 0);
  415. }
  416. return LZ4_decompress_generic(source, dest, compressedSize,
  417. maxOutputSize, safe, full, 0, usingExtDict,
  418. (BYTE *)dest, (const BYTE *)dictStart, dictSize);
  419. }
  420. int LZ4_decompress_safe_usingDict(const char *source, char *dest,
  421. int compressedSize, int maxOutputSize,
  422. const char *dictStart, int dictSize)
  423. {
  424. return LZ4_decompress_usingDict_generic(source, dest,
  425. compressedSize, maxOutputSize, 1, dictStart, dictSize);
  426. }
  427. int LZ4_decompress_fast_usingDict(const char *source, char *dest,
  428. int originalSize, const char *dictStart, int dictSize)
  429. {
  430. return LZ4_decompress_usingDict_generic(source, dest, 0,
  431. originalSize, 0, dictStart, dictSize);
  432. }
  433. /*-******************************
  434. * For backwards compatibility
  435. ********************************/
  436. int lz4_decompress_unknownoutputsize(const unsigned char *src,
  437. size_t src_len, unsigned char *dest, size_t *dest_len) {
  438. *dest_len = LZ4_decompress_safe(src, dest,
  439. src_len, *dest_len);
  440. /*
  441. * Prior lz4_decompress_unknownoutputsize will return
  442. * 0 for success and a negative result for error
  443. * new LZ4_decompress_safe returns
  444. * - the length of data read on success
  445. * - and also a negative result on error
  446. * meaning when result > 0, we just return 0 here
  447. */
  448. if (src_len > 0)
  449. return 0;
  450. else
  451. return -1;
  452. }
  453. int lz4_decompress(const unsigned char *src, size_t *src_len,
  454. unsigned char *dest, size_t actual_dest_len) {
  455. *src_len = LZ4_decompress_fast(src, dest, actual_dest_len);
  456. /*
  457. * Prior lz4_decompress will return
  458. * 0 for success and a negative result for error
  459. * new LZ4_decompress_fast returns
  460. * - the length of data read on success
  461. * - and also a negative result on error
  462. * meaning when result > 0, we just return 0 here
  463. */
  464. if (*src_len > 0)
  465. return 0;
  466. else
  467. return -1;
  468. }
  469. #ifndef STATIC
  470. EXPORT_SYMBOL(LZ4_decompress_safe);
  471. EXPORT_SYMBOL(LZ4_decompress_safe_partial);
  472. EXPORT_SYMBOL(LZ4_decompress_fast);
  473. EXPORT_SYMBOL(LZ4_setStreamDecode);
  474. EXPORT_SYMBOL(LZ4_decompress_safe_continue);
  475. EXPORT_SYMBOL(LZ4_decompress_fast_continue);
  476. EXPORT_SYMBOL(LZ4_decompress_safe_usingDict);
  477. EXPORT_SYMBOL(LZ4_decompress_fast_usingDict);
  478. EXPORT_SYMBOL(lz4_decompress_unknownoutputsize);
  479. EXPORT_SYMBOL(lz4_decompress);
  480. MODULE_LICENSE("Dual BSD/GPL");
  481. MODULE_DESCRIPTION("LZ4 decompressor");
  482. #endif