decompress_unlzo.c 6.4 KB

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