decompress_unlzo.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * LZO decompressor for the Linux kernel. Code borrowed from the lzo
  3. * implementation by Markus Franz Xaver Johannes Oberhumer.
  4. *
  5. * Linux kernel adaptation:
  6. * Copyright (C) 2009
  7. * Albin Tonnerre, Free Electrons <albin.tonnerre@free-electrons.com>
  8. *
  9. * Original code:
  10. * Copyright (C) 1996-2005 Markus Franz Xaver Johannes Oberhumer
  11. * All Rights Reserved.
  12. *
  13. * lzop and the LZO library are free software; you can redistribute them
  14. * and/or modify them under the terms of the GNU General Public License as
  15. * published by the Free Software Foundation; either version 2 of
  16. * the License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; see the file COPYING.
  25. * If not, write to the Free Software Foundation, Inc.,
  26. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  27. *
  28. * Markus F.X.J. Oberhumer
  29. * <markus@oberhumer.com>
  30. * http://www.oberhumer.com/opensource/lzop/
  31. */
  32. #ifdef STATIC
  33. #include "lzo/lzo1x_decompress_safe.c"
  34. #else
  35. #include <linux/decompress/unlzo.h>
  36. #endif
  37. #include <linux/types.h>
  38. #include <linux/lzo.h>
  39. #include <linux/decompress/mm.h>
  40. #include <linux/compiler.h>
  41. #include <asm/unaligned.h>
  42. static const unsigned char lzop_magic[] = {
  43. 0x89, 0x4c, 0x5a, 0x4f, 0x00, 0x0d, 0x0a, 0x1a, 0x0a };
  44. #define LZO_BLOCK_SIZE (256*1024l)
  45. #define HEADER_HAS_FILTER 0x00000800L
  46. #define HEADER_SIZE_MIN (9 + 7 + 4 + 8 + 1 + 4)
  47. #define HEADER_SIZE_MAX (9 + 7 + 1 + 8 + 8 + 4 + 1 + 255 + 4)
  48. STATIC inline long INIT parse_header(u8 *input, long *skip, long in_len)
  49. {
  50. int l;
  51. u8 *parse = input;
  52. u8 *end = input + in_len;
  53. u8 level = 0;
  54. u16 version;
  55. /*
  56. * Check that there's enough input to possibly have a valid header.
  57. * Then it is possible to parse several fields until the minimum
  58. * size may have been used.
  59. */
  60. if (in_len < HEADER_SIZE_MIN)
  61. return 0;
  62. /* read magic: 9 first bits */
  63. for (l = 0; l < 9; l++) {
  64. if (*parse++ != lzop_magic[l])
  65. return 0;
  66. }
  67. /* get version (2bytes), skip library version (2),
  68. * 'need to be extracted' version (2) and
  69. * method (1) */
  70. version = get_unaligned_be16(parse);
  71. parse += 7;
  72. if (version >= 0x0940)
  73. level = *parse++;
  74. if (get_unaligned_be32(parse) & HEADER_HAS_FILTER)
  75. parse += 8; /* flags + filter info */
  76. else
  77. parse += 4; /* flags */
  78. /*
  79. * At least mode, mtime_low, filename length, and checksum must
  80. * be left to be parsed. If also mtime_high is present, it's OK
  81. * because the next input buffer check is after reading the
  82. * filename length.
  83. */
  84. if (end - parse < 8 + 1 + 4)
  85. return 0;
  86. /* skip mode and mtime_low */
  87. parse += 8;
  88. if (version >= 0x0940)
  89. parse += 4; /* skip mtime_high */
  90. l = *parse++;
  91. /* don't care about the file name, and skip checksum */
  92. if (end - parse < l + 4)
  93. return 0;
  94. parse += l + 4;
  95. *skip = parse - input;
  96. return 1;
  97. }
  98. STATIC int INIT unlzo(u8 *input, long in_len,
  99. long (*fill)(void *, unsigned long),
  100. long (*flush)(void *, unsigned long),
  101. u8 *output, long *posp,
  102. void (*error) (char *x))
  103. {
  104. u8 r = 0;
  105. long skip = 0;
  106. u32 src_len, dst_len;
  107. size_t tmp;
  108. u8 *in_buf, *in_buf_save, *out_buf;
  109. int ret = -1;
  110. if (output) {
  111. out_buf = output;
  112. } else if (!flush) {
  113. error("NULL output pointer and no flush function provided");
  114. goto exit;
  115. } else {
  116. out_buf = malloc(LZO_BLOCK_SIZE);
  117. if (!out_buf) {
  118. error("Could not allocate output buffer");
  119. goto exit;
  120. }
  121. }
  122. if (input && fill) {
  123. error("Both input pointer and fill function provided, don't know what to do");
  124. goto exit_1;
  125. } else if (input) {
  126. in_buf = input;
  127. } else if (!fill) {
  128. error("NULL input pointer and missing fill function");
  129. goto exit_1;
  130. } else {
  131. in_buf = malloc(lzo1x_worst_compress(LZO_BLOCK_SIZE));
  132. if (!in_buf) {
  133. error("Could not allocate input buffer");
  134. goto exit_1;
  135. }
  136. }
  137. in_buf_save = in_buf;
  138. if (posp)
  139. *posp = 0;
  140. if (fill) {
  141. /*
  142. * Start from in_buf + HEADER_SIZE_MAX to make it possible
  143. * to use memcpy() to copy the unused data to the beginning
  144. * of the buffer. This way memmove() isn't needed which
  145. * is missing from pre-boot environments of most archs.
  146. */
  147. in_buf += HEADER_SIZE_MAX;
  148. in_len = fill(in_buf, HEADER_SIZE_MAX);
  149. }
  150. if (!parse_header(in_buf, &skip, in_len)) {
  151. error("invalid header");
  152. goto exit_2;
  153. }
  154. in_buf += skip;
  155. in_len -= skip;
  156. if (fill) {
  157. /* Move the unused data to the beginning of the buffer. */
  158. memcpy(in_buf_save, in_buf, in_len);
  159. in_buf = in_buf_save;
  160. }
  161. if (posp)
  162. *posp = skip;
  163. for (;;) {
  164. /* read uncompressed block size */
  165. if (fill && in_len < 4) {
  166. skip = fill(in_buf + in_len, 4 - in_len);
  167. if (skip > 0)
  168. in_len += skip;
  169. }
  170. if (in_len < 4) {
  171. error("file corrupted");
  172. goto exit_2;
  173. }
  174. dst_len = get_unaligned_be32(in_buf);
  175. in_buf += 4;
  176. in_len -= 4;
  177. /* exit if last block */
  178. if (dst_len == 0) {
  179. if (posp)
  180. *posp += 4;
  181. break;
  182. }
  183. if (dst_len > LZO_BLOCK_SIZE) {
  184. error("dest len longer than block size");
  185. goto exit_2;
  186. }
  187. /* read compressed block size, and skip block checksum info */
  188. if (fill && in_len < 8) {
  189. skip = fill(in_buf + in_len, 8 - in_len);
  190. if (skip > 0)
  191. in_len += skip;
  192. }
  193. if (in_len < 8) {
  194. error("file corrupted");
  195. goto exit_2;
  196. }
  197. src_len = get_unaligned_be32(in_buf);
  198. in_buf += 8;
  199. in_len -= 8;
  200. if (src_len <= 0 || src_len > dst_len) {
  201. error("file corrupted");
  202. goto exit_2;
  203. }
  204. /* decompress */
  205. if (fill && in_len < src_len) {
  206. skip = fill(in_buf + in_len, src_len - in_len);
  207. if (skip > 0)
  208. in_len += skip;
  209. }
  210. if (in_len < src_len) {
  211. error("file corrupted");
  212. goto exit_2;
  213. }
  214. tmp = dst_len;
  215. /* When the input data is not compressed at all,
  216. * lzo1x_decompress_safe will fail, so call memcpy()
  217. * instead */
  218. if (unlikely(dst_len == src_len))
  219. memcpy(out_buf, in_buf, src_len);
  220. else {
  221. r = lzo1x_decompress_safe((u8 *) in_buf, src_len,
  222. out_buf, &tmp);
  223. if (r != LZO_E_OK || dst_len != tmp) {
  224. error("Compressed data violation");
  225. goto exit_2;
  226. }
  227. }
  228. if (flush && flush(out_buf, dst_len) != dst_len)
  229. goto exit_2;
  230. if (output)
  231. out_buf += dst_len;
  232. if (posp)
  233. *posp += src_len + 12;
  234. in_buf += src_len;
  235. in_len -= src_len;
  236. if (fill) {
  237. /*
  238. * If there happens to still be unused data left in
  239. * in_buf, move it to the beginning of the buffer.
  240. * Use a loop to avoid memmove() dependency.
  241. */
  242. if (in_len > 0)
  243. for (skip = 0; skip < in_len; ++skip)
  244. in_buf_save[skip] = in_buf[skip];
  245. in_buf = in_buf_save;
  246. }
  247. }
  248. ret = 0;
  249. exit_2:
  250. if (!input)
  251. free(in_buf_save);
  252. exit_1:
  253. if (!output)
  254. free(out_buf);
  255. exit:
  256. return ret;
  257. }
  258. #define decompress unlzo