lz4_decompress.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. if ((ref + COPYLENGTH) > oend ||
  129. (op + COPYLENGTH) > oend)
  130. goto _output_error;
  131. LZ4_SECURECOPY(ref, op, (oend - COPYLENGTH));
  132. while (op < cpy)
  133. *op++ = *ref++;
  134. op = cpy;
  135. /*
  136. * Check EOF (should never happen, since last 5 bytes
  137. * are supposed to be literals)
  138. */
  139. if (op == oend)
  140. goto _output_error;
  141. continue;
  142. }
  143. LZ4_SECURECOPY(ref, op, cpy);
  144. op = cpy; /* correction */
  145. }
  146. /* end of decoding */
  147. return (int) (((char *)ip) - source);
  148. /* write overflow error detected */
  149. _output_error:
  150. return -1;
  151. }
  152. static int lz4_uncompress_unknownoutputsize(const char *source, char *dest,
  153. int isize, size_t maxoutputsize)
  154. {
  155. const BYTE *ip = (const BYTE *) source;
  156. const BYTE *const iend = ip + isize;
  157. const BYTE *ref;
  158. BYTE *op = (BYTE *) dest;
  159. BYTE * const oend = op + maxoutputsize;
  160. BYTE *cpy;
  161. size_t dec32table[] = {0, 3, 2, 3, 0, 0, 0, 0};
  162. #if LZ4_ARCH64
  163. size_t dec64table[] = {0, 0, 0, -1, 0, 1, 2, 3};
  164. #endif
  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. size_t 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. LZ4_SECURECOPY(ref, op, (oend - COPYLENGTH));
  247. while (op < cpy)
  248. *op++ = *ref++;
  249. op = cpy;
  250. /*
  251. * Check EOF (should never happen, since last 5 bytes
  252. * are supposed to be literals)
  253. */
  254. if (op == oend)
  255. goto _output_error;
  256. continue;
  257. }
  258. LZ4_SECURECOPY(ref, op, cpy);
  259. op = cpy; /* correction */
  260. }
  261. /* end of decoding */
  262. return (int) (((char *) op) - dest);
  263. /* write overflow error detected */
  264. _output_error:
  265. return -1;
  266. }
  267. int lz4_decompress(const unsigned char *src, size_t *src_len,
  268. unsigned char *dest, size_t actual_dest_len)
  269. {
  270. int ret = -1;
  271. int input_len = 0;
  272. input_len = lz4_uncompress(src, dest, actual_dest_len);
  273. if (input_len < 0)
  274. goto exit_0;
  275. *src_len = input_len;
  276. return 0;
  277. exit_0:
  278. return ret;
  279. }
  280. #ifndef STATIC
  281. EXPORT_SYMBOL(lz4_decompress);
  282. #endif
  283. int lz4_decompress_unknownoutputsize(const unsigned char *src, size_t src_len,
  284. unsigned char *dest, size_t *dest_len)
  285. {
  286. int ret = -1;
  287. int out_len = 0;
  288. out_len = lz4_uncompress_unknownoutputsize(src, dest, src_len,
  289. *dest_len);
  290. if (out_len < 0)
  291. goto exit_0;
  292. *dest_len = out_len;
  293. return 0;
  294. exit_0:
  295. return ret;
  296. }
  297. #ifndef STATIC
  298. EXPORT_SYMBOL(lz4_decompress_unknownoutputsize);
  299. MODULE_LICENSE("Dual BSD/GPL");
  300. MODULE_DESCRIPTION("LZ4 Decompressor");
  301. #endif