decompress_unlzo.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. #define PREBOOT
  34. #include "lzo/lzo1x_decompress_safe.c"
  35. #else
  36. #include <linux/decompress/unlzo.h>
  37. #endif
  38. #include <linux/types.h>
  39. #include <linux/lzo.h>
  40. #include <linux/decompress/mm.h>
  41. #include <linux/compiler.h>
  42. #include <asm/unaligned.h>
  43. static const unsigned char lzop_magic[] = {
  44. 0x89, 0x4c, 0x5a, 0x4f, 0x00, 0x0d, 0x0a, 0x1a, 0x0a };
  45. #define LZO_BLOCK_SIZE (256*1024l)
  46. #define HEADER_HAS_FILTER 0x00000800L
  47. #define HEADER_SIZE_MIN (9 + 7 + 4 + 8 + 1 + 4)
  48. #define HEADER_SIZE_MAX (9 + 7 + 1 + 8 + 8 + 4 + 1 + 255 + 4)
  49. STATIC inline long INIT parse_header(u8 *input, long *skip, long in_len)
  50. {
  51. int l;
  52. u8 *parse = input;
  53. u8 *end = input + in_len;
  54. u8 level = 0;
  55. u16 version;
  56. /*
  57. * Check that there's enough input to possibly have a valid header.
  58. * Then it is possible to parse several fields until the minimum
  59. * size may have been used.
  60. */
  61. if (in_len < HEADER_SIZE_MIN)
  62. return 0;
  63. /* read magic: 9 first bits */
  64. for (l = 0; l < 9; l++) {
  65. if (*parse++ != lzop_magic[l])
  66. return 0;
  67. }
  68. /* get version (2bytes), skip library version (2),
  69. * 'need to be extracted' version (2) and
  70. * method (1) */
  71. version = get_unaligned_be16(parse);
  72. parse += 7;
  73. if (version >= 0x0940)
  74. level = *parse++;
  75. if (get_unaligned_be32(parse) & HEADER_HAS_FILTER)
  76. parse += 8; /* flags + filter info */
  77. else
  78. parse += 4; /* flags */
  79. /*
  80. * At least mode, mtime_low, filename length, and checksum must
  81. * be left to be parsed. If also mtime_high is present, it's OK
  82. * because the next input buffer check is after reading the
  83. * filename length.
  84. */
  85. if (end - parse < 8 + 1 + 4)
  86. return 0;
  87. /* skip mode and mtime_low */
  88. parse += 8;
  89. if (version >= 0x0940)
  90. parse += 4; /* skip mtime_high */
  91. l = *parse++;
  92. /* don't care about the file name, and skip checksum */
  93. if (end - parse < l + 4)
  94. return 0;
  95. parse += l + 4;
  96. *skip = parse - input;
  97. return 1;
  98. }
  99. STATIC int INIT unlzo(u8 *input, long in_len,
  100. long (*fill)(void *, unsigned long),
  101. long (*flush)(void *, unsigned long),
  102. u8 *output, long *posp,
  103. void (*error) (char *x))
  104. {
  105. u8 r = 0;
  106. long skip = 0;
  107. u32 src_len, dst_len;
  108. size_t tmp;
  109. u8 *in_buf, *in_buf_save, *out_buf;
  110. int ret = -1;
  111. if (output) {
  112. out_buf = output;
  113. } else if (!flush) {
  114. error("NULL output pointer and no flush function provided");
  115. goto exit;
  116. } else {
  117. out_buf = malloc(LZO_BLOCK_SIZE);
  118. if (!out_buf) {
  119. error("Could not allocate output buffer");
  120. goto exit;
  121. }
  122. }
  123. if (input && fill) {
  124. error("Both input pointer and fill function provided, don't know what to do");
  125. goto exit_1;
  126. } else if (input) {
  127. in_buf = input;
  128. } else if (!fill) {
  129. error("NULL input pointer and missing fill function");
  130. goto exit_1;
  131. } else {
  132. in_buf = malloc(lzo1x_worst_compress(LZO_BLOCK_SIZE));
  133. if (!in_buf) {
  134. error("Could not allocate input buffer");
  135. goto exit_1;
  136. }
  137. }
  138. in_buf_save = in_buf;
  139. if (posp)
  140. *posp = 0;
  141. if (fill) {
  142. /*
  143. * Start from in_buf + HEADER_SIZE_MAX to make it possible
  144. * to use memcpy() to copy the unused data to the beginning
  145. * of the buffer. This way memmove() isn't needed which
  146. * is missing from pre-boot environments of most archs.
  147. */
  148. in_buf += HEADER_SIZE_MAX;
  149. in_len = fill(in_buf, HEADER_SIZE_MAX);
  150. }
  151. if (!parse_header(in_buf, &skip, in_len)) {
  152. error("invalid header");
  153. goto exit_2;
  154. }
  155. in_buf += skip;
  156. in_len -= skip;
  157. if (fill) {
  158. /* Move the unused data to the beginning of the buffer. */
  159. memcpy(in_buf_save, in_buf, in_len);
  160. in_buf = in_buf_save;
  161. }
  162. if (posp)
  163. *posp = skip;
  164. for (;;) {
  165. /* read uncompressed block size */
  166. if (fill && in_len < 4) {
  167. skip = fill(in_buf + in_len, 4 - in_len);
  168. if (skip > 0)
  169. in_len += skip;
  170. }
  171. if (in_len < 4) {
  172. error("file corrupted");
  173. goto exit_2;
  174. }
  175. dst_len = get_unaligned_be32(in_buf);
  176. in_buf += 4;
  177. in_len -= 4;
  178. /* exit if last block */
  179. if (dst_len == 0) {
  180. if (posp)
  181. *posp += 4;
  182. break;
  183. }
  184. if (dst_len > LZO_BLOCK_SIZE) {
  185. error("dest len longer than block size");
  186. goto exit_2;
  187. }
  188. /* read compressed block size, and skip block checksum info */
  189. if (fill && in_len < 8) {
  190. skip = fill(in_buf + in_len, 8 - in_len);
  191. if (skip > 0)
  192. in_len += skip;
  193. }
  194. if (in_len < 8) {
  195. error("file corrupted");
  196. goto exit_2;
  197. }
  198. src_len = get_unaligned_be32(in_buf);
  199. in_buf += 8;
  200. in_len -= 8;
  201. if (src_len <= 0 || src_len > dst_len) {
  202. error("file corrupted");
  203. goto exit_2;
  204. }
  205. /* decompress */
  206. if (fill && in_len < src_len) {
  207. skip = fill(in_buf + in_len, src_len - in_len);
  208. if (skip > 0)
  209. in_len += skip;
  210. }
  211. if (in_len < src_len) {
  212. error("file corrupted");
  213. goto exit_2;
  214. }
  215. tmp = dst_len;
  216. /* When the input data is not compressed at all,
  217. * lzo1x_decompress_safe will fail, so call memcpy()
  218. * instead */
  219. if (unlikely(dst_len == src_len))
  220. memcpy(out_buf, in_buf, src_len);
  221. else {
  222. r = lzo1x_decompress_safe((u8 *) in_buf, src_len,
  223. out_buf, &tmp);
  224. if (r != LZO_E_OK || dst_len != tmp) {
  225. error("Compressed data violation");
  226. goto exit_2;
  227. }
  228. }
  229. if (flush && flush(out_buf, dst_len) != dst_len)
  230. goto exit_2;
  231. if (output)
  232. out_buf += dst_len;
  233. if (posp)
  234. *posp += src_len + 12;
  235. in_buf += src_len;
  236. in_len -= src_len;
  237. if (fill) {
  238. /*
  239. * If there happens to still be unused data left in
  240. * in_buf, move it to the beginning of the buffer.
  241. * Use a loop to avoid memmove() dependency.
  242. */
  243. if (in_len > 0)
  244. for (skip = 0; skip < in_len; ++skip)
  245. in_buf_save[skip] = in_buf[skip];
  246. in_buf = in_buf_save;
  247. }
  248. }
  249. ret = 0;
  250. exit_2:
  251. if (!input)
  252. free(in_buf_save);
  253. exit_1:
  254. if (!output)
  255. free(out_buf);
  256. exit:
  257. return ret;
  258. }
  259. #ifdef PREBOOT
  260. STATIC int INIT __decompress(unsigned char *buf, long len,
  261. long (*fill)(void*, unsigned long),
  262. long (*flush)(void*, unsigned long),
  263. unsigned char *out_buf, long olen,
  264. long *pos,
  265. void (*error)(char *x))
  266. {
  267. return unlzo(buf, len, fill, flush, out_buf, pos, error);
  268. }
  269. #endif