lz4_decompress.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 const int dec32table[] = {0, 3, 2, 3, 0, 0, 0, 0};
  47. #if LZ4_ARCH64
  48. static const int dec64table[] = {0, 0, 0, -1, 0, 1, 2, 3};
  49. #endif
  50. static int lz4_uncompress(const char *source, char *dest, int osize)
  51. {
  52. const BYTE *ip = (const BYTE *) source;
  53. const BYTE *ref;
  54. BYTE *op = (BYTE *) dest;
  55. BYTE * const oend = op + osize;
  56. BYTE *cpy;
  57. unsigned token;
  58. size_t length;
  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. int 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. #if LZ4_ARCH64
  129. if ((ref + COPYLENGTH) > oend)
  130. #else
  131. if ((ref + COPYLENGTH) > oend ||
  132. (op + COPYLENGTH) > oend)
  133. #endif
  134. goto _output_error;
  135. LZ4_SECURECOPY(ref, op, (oend - COPYLENGTH));
  136. while (op < cpy)
  137. *op++ = *ref++;
  138. op = cpy;
  139. /*
  140. * Check EOF (should never happen, since last 5 bytes
  141. * are supposed to be literals)
  142. */
  143. if (op == oend)
  144. goto _output_error;
  145. continue;
  146. }
  147. LZ4_SECURECOPY(ref, op, cpy);
  148. op = cpy; /* correction */
  149. }
  150. /* end of decoding */
  151. return (int) (((char *)ip) - source);
  152. /* write overflow error detected */
  153. _output_error:
  154. return -1;
  155. }
  156. static int lz4_uncompress_unknownoutputsize(const char *source, char *dest,
  157. int isize, size_t maxoutputsize)
  158. {
  159. const BYTE *ip = (const BYTE *) source;
  160. const BYTE *const iend = ip + isize;
  161. const BYTE *ref;
  162. BYTE *op = (BYTE *) dest;
  163. BYTE * const oend = op + maxoutputsize;
  164. BYTE *cpy;
  165. /* Main Loop */
  166. while (ip < iend) {
  167. unsigned token;
  168. size_t length;
  169. /* get runlength */
  170. token = *ip++;
  171. length = (token >> ML_BITS);
  172. if (length == RUN_MASK) {
  173. int s = 255;
  174. while ((ip < iend) && (s == 255)) {
  175. s = *ip++;
  176. if (unlikely(length > (size_t)(length + s)))
  177. goto _output_error;
  178. length += s;
  179. }
  180. }
  181. /* copy literals */
  182. cpy = op + length;
  183. if ((cpy > oend - COPYLENGTH) ||
  184. (ip + length > iend - COPYLENGTH)) {
  185. if (cpy > oend)
  186. goto _output_error;/* writes beyond buffer */
  187. if (ip + length != iend)
  188. goto _output_error;/*
  189. * Error: LZ4 format requires
  190. * to consume all input
  191. * at this stage
  192. */
  193. memcpy(op, ip, length);
  194. op += length;
  195. break;/* Necessarily EOF, due to parsing restrictions */
  196. }
  197. LZ4_WILDCOPY(ip, op, cpy);
  198. ip -= (op - cpy);
  199. op = cpy;
  200. /* get offset */
  201. LZ4_READ_LITTLEENDIAN_16(ref, cpy, ip);
  202. ip += 2;
  203. if (ref < (BYTE * const) dest)
  204. goto _output_error;
  205. /*
  206. * Error : offset creates reference
  207. * outside of destination buffer
  208. */
  209. /* get matchlength */
  210. length = (token & ML_MASK);
  211. if (length == ML_MASK) {
  212. while (ip < iend) {
  213. int s = *ip++;
  214. if (unlikely(length > (size_t)(length + s)))
  215. goto _output_error;
  216. length += s;
  217. if (s == 255)
  218. continue;
  219. break;
  220. }
  221. }
  222. /* copy repeated sequence */
  223. if (unlikely((op - ref) < STEPSIZE)) {
  224. #if LZ4_ARCH64
  225. int dec64 = dec64table[op - ref];
  226. #else
  227. const int dec64 = 0;
  228. #endif
  229. op[0] = ref[0];
  230. op[1] = ref[1];
  231. op[2] = ref[2];
  232. op[3] = ref[3];
  233. op += 4;
  234. ref += 4;
  235. ref -= dec32table[op - ref];
  236. PUT4(ref, op);
  237. op += STEPSIZE - 4;
  238. ref -= dec64;
  239. } else {
  240. LZ4_COPYSTEP(ref, op);
  241. }
  242. cpy = op + length - (STEPSIZE-4);
  243. if (cpy > oend - COPYLENGTH) {
  244. if (cpy > oend)
  245. goto _output_error; /* write outside of buf */
  246. #if LZ4_ARCH64
  247. if ((ref + COPYLENGTH) > oend)
  248. #else
  249. if ((ref + COPYLENGTH) > oend ||
  250. (op + COPYLENGTH) > oend)
  251. #endif
  252. goto _output_error;
  253. LZ4_SECURECOPY(ref, op, (oend - COPYLENGTH));
  254. while (op < cpy)
  255. *op++ = *ref++;
  256. op = cpy;
  257. /*
  258. * Check EOF (should never happen, since last 5 bytes
  259. * are supposed to be literals)
  260. */
  261. if (op == oend)
  262. goto _output_error;
  263. continue;
  264. }
  265. LZ4_SECURECOPY(ref, op, cpy);
  266. op = cpy; /* correction */
  267. }
  268. /* end of decoding */
  269. return (int) (((char *) op) - dest);
  270. /* write overflow error detected */
  271. _output_error:
  272. return -1;
  273. }
  274. int lz4_decompress(const unsigned char *src, size_t *src_len,
  275. unsigned char *dest, size_t actual_dest_len)
  276. {
  277. int ret = -1;
  278. int input_len = 0;
  279. input_len = lz4_uncompress(src, dest, actual_dest_len);
  280. if (input_len < 0)
  281. goto exit_0;
  282. *src_len = input_len;
  283. return 0;
  284. exit_0:
  285. return ret;
  286. }
  287. #ifndef STATIC
  288. EXPORT_SYMBOL(lz4_decompress);
  289. #endif
  290. int lz4_decompress_unknownoutputsize(const unsigned char *src, size_t src_len,
  291. unsigned char *dest, size_t *dest_len)
  292. {
  293. int ret = -1;
  294. int out_len = 0;
  295. out_len = lz4_uncompress_unknownoutputsize(src, dest, src_len,
  296. *dest_len);
  297. if (out_len < 0)
  298. goto exit_0;
  299. *dest_len = out_len;
  300. return 0;
  301. exit_0:
  302. return ret;
  303. }
  304. #ifndef STATIC
  305. EXPORT_SYMBOL(lz4_decompress_unknownoutputsize);
  306. MODULE_LICENSE("Dual BSD/GPL");
  307. MODULE_DESCRIPTION("LZ4 Decompressor");
  308. #endif