lz4_decompress.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * LZ4 Decompressor for Linux kernel
  3. *
  4. * Copyright (C) 2013, LG Electronics, Kyungsik Lee <kyungsik.lee@lge.com>
  5. *
  6. * Based on LZ4 implementation by Yann Collet.
  7. *
  8. * LZ4 - Fast LZ compression algorithm
  9. * Copyright (C) 2011-2012, Yann Collet.
  10. * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are
  14. * met:
  15. *
  16. * * Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. * * Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following disclaimer
  20. * in the documentation and/or other materials provided with the
  21. * distribution.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  26. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  27. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  28. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  29. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  30. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  31. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  32. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. * You can contact the author at :
  36. * - LZ4 homepage : http://fastcompression.blogspot.com/p/lz4.html
  37. * - LZ4 source repository : http://code.google.com/p/lz4/
  38. */
  39. #ifndef STATIC
  40. #include <linux/module.h>
  41. #include <linux/kernel.h>
  42. #endif
  43. #include <linux/lz4.h>
  44. #include <asm/unaligned.h>
  45. #include "lz4defs.h"
  46. static int lz4_uncompress(const char *source, char *dest, int osize)
  47. {
  48. const BYTE *ip = (const BYTE *) source;
  49. const BYTE *ref;
  50. BYTE *op = (BYTE *) dest;
  51. BYTE * const oend = op + osize;
  52. BYTE *cpy;
  53. unsigned token;
  54. size_t length;
  55. size_t dec32table[] = {0, 3, 2, 3, 0, 0, 0, 0};
  56. #if LZ4_ARCH64
  57. size_t dec64table[] = {0, 0, 0, -1, 0, 1, 2, 3};
  58. #endif
  59. while (1) {
  60. /* get runlength */
  61. token = *ip++;
  62. length = (token >> ML_BITS);
  63. if (length == RUN_MASK) {
  64. size_t len;
  65. len = *ip++;
  66. for (; len == 255; length += 255)
  67. len = *ip++;
  68. if (unlikely(length > (size_t)(length + len)))
  69. goto _output_error;
  70. length += len;
  71. }
  72. /* copy literals */
  73. cpy = op + length;
  74. if (unlikely(cpy > oend - COPYLENGTH)) {
  75. /*
  76. * Error: not enough place for another match
  77. * (min 4) + 5 literals
  78. */
  79. if (cpy != oend)
  80. goto _output_error;
  81. memcpy(op, ip, length);
  82. ip += length;
  83. break; /* EOF */
  84. }
  85. LZ4_WILDCOPY(ip, op, cpy);
  86. ip -= (op - cpy);
  87. op = cpy;
  88. /* get offset */
  89. LZ4_READ_LITTLEENDIAN_16(ref, cpy, ip);
  90. ip += 2;
  91. /* Error: offset create reference outside destination buffer */
  92. if (unlikely(ref < (BYTE *const) dest))
  93. goto _output_error;
  94. /* get matchlength */
  95. length = token & ML_MASK;
  96. if (length == ML_MASK) {
  97. for (; *ip == 255; length += 255)
  98. ip++;
  99. if (unlikely(length > (size_t)(length + *ip)))
  100. goto _output_error;
  101. length += *ip++;
  102. }
  103. /* copy repeated sequence */
  104. if (unlikely((op - ref) < STEPSIZE)) {
  105. #if LZ4_ARCH64
  106. size_t dec64 = dec64table[op - ref];
  107. #else
  108. const int dec64 = 0;
  109. #endif
  110. op[0] = ref[0];
  111. op[1] = ref[1];
  112. op[2] = ref[2];
  113. op[3] = ref[3];
  114. op += 4;
  115. ref += 4;
  116. ref -= dec32table[op-ref];
  117. PUT4(ref, op);
  118. op += STEPSIZE - 4;
  119. ref -= dec64;
  120. } else {
  121. LZ4_COPYSTEP(ref, op);
  122. }
  123. cpy = op + length - (STEPSIZE - 4);
  124. if (cpy > (oend - COPYLENGTH)) {
  125. /* Error: request to write beyond destination buffer */
  126. if (cpy > oend)
  127. goto _output_error;
  128. LZ4_SECURECOPY(ref, op, (oend - COPYLENGTH));
  129. while (op < cpy)
  130. *op++ = *ref++;
  131. op = cpy;
  132. /*
  133. * Check EOF (should never happen, since last 5 bytes
  134. * are supposed to be literals)
  135. */
  136. if (op == oend)
  137. goto _output_error;
  138. continue;
  139. }
  140. LZ4_SECURECOPY(ref, op, cpy);
  141. op = cpy; /* correction */
  142. }
  143. /* end of decoding */
  144. return (int) (((char *)ip) - source);
  145. /* write overflow error detected */
  146. _output_error:
  147. return -1;
  148. }
  149. static int lz4_uncompress_unknownoutputsize(const char *source, char *dest,
  150. int isize, size_t maxoutputsize)
  151. {
  152. const BYTE *ip = (const BYTE *) source;
  153. const BYTE *const iend = ip + isize;
  154. const BYTE *ref;
  155. BYTE *op = (BYTE *) dest;
  156. BYTE * const oend = op + maxoutputsize;
  157. BYTE *cpy;
  158. size_t dec32table[] = {0, 3, 2, 3, 0, 0, 0, 0};
  159. #if LZ4_ARCH64
  160. size_t dec64table[] = {0, 0, 0, -1, 0, 1, 2, 3};
  161. #endif
  162. /* Main Loop */
  163. while (ip < iend) {
  164. unsigned token;
  165. size_t length;
  166. /* get runlength */
  167. token = *ip++;
  168. length = (token >> ML_BITS);
  169. if (length == RUN_MASK) {
  170. int s = 255;
  171. while ((ip < iend) && (s == 255)) {
  172. s = *ip++;
  173. if (unlikely(length > (size_t)(length + s)))
  174. goto _output_error;
  175. length += s;
  176. }
  177. }
  178. /* copy literals */
  179. cpy = op + length;
  180. if ((cpy > oend - COPYLENGTH) ||
  181. (ip + length > iend - COPYLENGTH)) {
  182. if (cpy > oend)
  183. goto _output_error;/* writes beyond buffer */
  184. if (ip + length != iend)
  185. goto _output_error;/*
  186. * Error: LZ4 format requires
  187. * to consume all input
  188. * at this stage
  189. */
  190. memcpy(op, ip, length);
  191. op += length;
  192. break;/* Necessarily EOF, due to parsing restrictions */
  193. }
  194. LZ4_WILDCOPY(ip, op, cpy);
  195. ip -= (op - cpy);
  196. op = cpy;
  197. /* get offset */
  198. LZ4_READ_LITTLEENDIAN_16(ref, cpy, ip);
  199. ip += 2;
  200. if (ref < (BYTE * const) dest)
  201. goto _output_error;
  202. /*
  203. * Error : offset creates reference
  204. * outside of destination buffer
  205. */
  206. /* get matchlength */
  207. length = (token & ML_MASK);
  208. if (length == ML_MASK) {
  209. while (ip < iend) {
  210. int s = *ip++;
  211. if (unlikely(length > (size_t)(length + s)))
  212. goto _output_error;
  213. length += s;
  214. if (s == 255)
  215. continue;
  216. break;
  217. }
  218. }
  219. /* copy repeated sequence */
  220. if (unlikely((op - ref) < STEPSIZE)) {
  221. #if LZ4_ARCH64
  222. size_t dec64 = dec64table[op - ref];
  223. #else
  224. const int dec64 = 0;
  225. #endif
  226. op[0] = ref[0];
  227. op[1] = ref[1];
  228. op[2] = ref[2];
  229. op[3] = ref[3];
  230. op += 4;
  231. ref += 4;
  232. ref -= dec32table[op - ref];
  233. PUT4(ref, op);
  234. op += STEPSIZE - 4;
  235. ref -= dec64;
  236. } else {
  237. LZ4_COPYSTEP(ref, op);
  238. }
  239. cpy = op + length - (STEPSIZE-4);
  240. if (cpy > oend - COPYLENGTH) {
  241. if (cpy > oend)
  242. goto _output_error; /* write outside of buf */
  243. LZ4_SECURECOPY(ref, op, (oend - COPYLENGTH));
  244. while (op < cpy)
  245. *op++ = *ref++;
  246. op = cpy;
  247. /*
  248. * Check EOF (should never happen, since last 5 bytes
  249. * are supposed to be literals)
  250. */
  251. if (op == oend)
  252. goto _output_error;
  253. continue;
  254. }
  255. LZ4_SECURECOPY(ref, op, cpy);
  256. op = cpy; /* correction */
  257. }
  258. /* end of decoding */
  259. return (int) (((char *) op) - dest);
  260. /* write overflow error detected */
  261. _output_error:
  262. return -1;
  263. }
  264. int lz4_decompress(const unsigned char *src, size_t *src_len,
  265. unsigned char *dest, size_t actual_dest_len)
  266. {
  267. int ret = -1;
  268. int input_len = 0;
  269. input_len = lz4_uncompress(src, dest, actual_dest_len);
  270. if (input_len < 0)
  271. goto exit_0;
  272. *src_len = input_len;
  273. return 0;
  274. exit_0:
  275. return ret;
  276. }
  277. #ifndef STATIC
  278. EXPORT_SYMBOL(lz4_decompress);
  279. #endif
  280. int lz4_decompress_unknownoutputsize(const unsigned char *src, size_t src_len,
  281. unsigned char *dest, size_t *dest_len)
  282. {
  283. int ret = -1;
  284. int out_len = 0;
  285. out_len = lz4_uncompress_unknownoutputsize(src, dest, src_len,
  286. *dest_len);
  287. if (out_len < 0)
  288. goto exit_0;
  289. *dest_len = out_len;
  290. return 0;
  291. exit_0:
  292. return ret;
  293. }
  294. #ifndef STATIC
  295. EXPORT_SYMBOL(lz4_decompress_unknownoutputsize);
  296. MODULE_LICENSE("Dual BSD/GPL");
  297. MODULE_DESCRIPTION("LZ4 Decompressor");
  298. #endif