lz4_decompress.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. length += len;
  69. }
  70. /* copy literals */
  71. cpy = op + length;
  72. if (unlikely(cpy > oend - COPYLENGTH)) {
  73. /*
  74. * Error: not enough place for another match
  75. * (min 4) + 5 literals
  76. */
  77. if (cpy != oend)
  78. goto _output_error;
  79. memcpy(op, ip, length);
  80. ip += length;
  81. break; /* EOF */
  82. }
  83. LZ4_WILDCOPY(ip, op, cpy);
  84. ip -= (op - cpy);
  85. op = cpy;
  86. /* get offset */
  87. LZ4_READ_LITTLEENDIAN_16(ref, cpy, ip);
  88. ip += 2;
  89. /* Error: offset create reference outside destination buffer */
  90. if (unlikely(ref < (BYTE *const) dest))
  91. goto _output_error;
  92. /* get matchlength */
  93. length = token & ML_MASK;
  94. if (length == ML_MASK) {
  95. for (; *ip == 255; length += 255)
  96. ip++;
  97. length += *ip++;
  98. }
  99. /* copy repeated sequence */
  100. if (unlikely((op - ref) < STEPSIZE)) {
  101. #if LZ4_ARCH64
  102. size_t dec64 = dec64table[op - ref];
  103. #else
  104. const int dec64 = 0;
  105. #endif
  106. op[0] = ref[0];
  107. op[1] = ref[1];
  108. op[2] = ref[2];
  109. op[3] = ref[3];
  110. op += 4;
  111. ref += 4;
  112. ref -= dec32table[op-ref];
  113. PUT4(ref, op);
  114. op += STEPSIZE - 4;
  115. ref -= dec64;
  116. } else {
  117. LZ4_COPYSTEP(ref, op);
  118. }
  119. cpy = op + length - (STEPSIZE - 4);
  120. if (cpy > (oend - COPYLENGTH)) {
  121. /* Error: request to write beyond destination buffer */
  122. if (cpy > oend)
  123. goto _output_error;
  124. LZ4_SECURECOPY(ref, op, (oend - COPYLENGTH));
  125. while (op < cpy)
  126. *op++ = *ref++;
  127. op = cpy;
  128. /*
  129. * Check EOF (should never happen, since last 5 bytes
  130. * are supposed to be literals)
  131. */
  132. if (op == oend)
  133. goto _output_error;
  134. continue;
  135. }
  136. LZ4_SECURECOPY(ref, op, cpy);
  137. op = cpy; /* correction */
  138. }
  139. /* end of decoding */
  140. return (int) (((char *)ip) - source);
  141. /* write overflow error detected */
  142. _output_error:
  143. return (int) (-(((char *)ip) - source));
  144. }
  145. static int lz4_uncompress_unknownoutputsize(const char *source, char *dest,
  146. int isize, size_t maxoutputsize)
  147. {
  148. const BYTE *ip = (const BYTE *) source;
  149. const BYTE *const iend = ip + isize;
  150. const BYTE *ref;
  151. BYTE *op = (BYTE *) dest;
  152. BYTE * const oend = op + maxoutputsize;
  153. BYTE *cpy;
  154. size_t dec32table[] = {0, 3, 2, 3, 0, 0, 0, 0};
  155. #if LZ4_ARCH64
  156. size_t dec64table[] = {0, 0, 0, -1, 0, 1, 2, 3};
  157. #endif
  158. /* Main Loop */
  159. while (ip < iend) {
  160. unsigned token;
  161. size_t length;
  162. /* get runlength */
  163. token = *ip++;
  164. length = (token >> ML_BITS);
  165. if (length == RUN_MASK) {
  166. int s = 255;
  167. while ((ip < iend) && (s == 255)) {
  168. s = *ip++;
  169. length += s;
  170. }
  171. }
  172. /* copy literals */
  173. cpy = op + length;
  174. if ((cpy > oend - COPYLENGTH) ||
  175. (ip + length > iend - COPYLENGTH)) {
  176. if (cpy > oend)
  177. goto _output_error;/* writes beyond buffer */
  178. if (ip + length != iend)
  179. goto _output_error;/*
  180. * Error: LZ4 format requires
  181. * to consume all input
  182. * at this stage
  183. */
  184. memcpy(op, ip, length);
  185. op += length;
  186. break;/* Necessarily EOF, due to parsing restrictions */
  187. }
  188. LZ4_WILDCOPY(ip, op, cpy);
  189. ip -= (op - cpy);
  190. op = cpy;
  191. /* get offset */
  192. LZ4_READ_LITTLEENDIAN_16(ref, cpy, ip);
  193. ip += 2;
  194. if (ref < (BYTE * const) dest)
  195. goto _output_error;
  196. /*
  197. * Error : offset creates reference
  198. * outside of destination buffer
  199. */
  200. /* get matchlength */
  201. length = (token & ML_MASK);
  202. if (length == ML_MASK) {
  203. while (ip < iend) {
  204. int s = *ip++;
  205. length += s;
  206. if (s == 255)
  207. continue;
  208. break;
  209. }
  210. }
  211. /* copy repeated sequence */
  212. if (unlikely((op - ref) < STEPSIZE)) {
  213. #if LZ4_ARCH64
  214. size_t dec64 = dec64table[op - ref];
  215. #else
  216. const int dec64 = 0;
  217. #endif
  218. op[0] = ref[0];
  219. op[1] = ref[1];
  220. op[2] = ref[2];
  221. op[3] = ref[3];
  222. op += 4;
  223. ref += 4;
  224. ref -= dec32table[op - ref];
  225. PUT4(ref, op);
  226. op += STEPSIZE - 4;
  227. ref -= dec64;
  228. } else {
  229. LZ4_COPYSTEP(ref, op);
  230. }
  231. cpy = op + length - (STEPSIZE-4);
  232. if (cpy > oend - COPYLENGTH) {
  233. if (cpy > oend)
  234. goto _output_error; /* write outside of buf */
  235. LZ4_SECURECOPY(ref, op, (oend - COPYLENGTH));
  236. while (op < cpy)
  237. *op++ = *ref++;
  238. op = cpy;
  239. /*
  240. * Check EOF (should never happen, since last 5 bytes
  241. * are supposed to be literals)
  242. */
  243. if (op == oend)
  244. goto _output_error;
  245. continue;
  246. }
  247. LZ4_SECURECOPY(ref, op, cpy);
  248. op = cpy; /* correction */
  249. }
  250. /* end of decoding */
  251. return (int) (((char *) op) - dest);
  252. /* write overflow error detected */
  253. _output_error:
  254. return (int) (-(((char *) ip) - source));
  255. }
  256. int lz4_decompress(const char *src, size_t *src_len, char *dest,
  257. size_t actual_dest_len)
  258. {
  259. int ret = -1;
  260. int input_len = 0;
  261. input_len = lz4_uncompress(src, dest, actual_dest_len);
  262. if (input_len < 0)
  263. goto exit_0;
  264. *src_len = input_len;
  265. return 0;
  266. exit_0:
  267. return ret;
  268. }
  269. #ifndef STATIC
  270. EXPORT_SYMBOL(lz4_decompress);
  271. #endif
  272. int lz4_decompress_unknownoutputsize(const char *src, size_t src_len,
  273. char *dest, size_t *dest_len)
  274. {
  275. int ret = -1;
  276. int out_len = 0;
  277. out_len = lz4_uncompress_unknownoutputsize(src, dest, src_len,
  278. *dest_len);
  279. if (out_len < 0)
  280. goto exit_0;
  281. *dest_len = out_len;
  282. return 0;
  283. exit_0:
  284. return ret;
  285. }
  286. #ifndef STATIC
  287. EXPORT_SYMBOL(lz4_decompress_unknownoutputsize);
  288. MODULE_LICENSE("Dual BSD/GPL");
  289. MODULE_DESCRIPTION("LZ4 Decompressor");
  290. #endif