lzo1x_decompress.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * LZO1X Decompressor from MiniLZO
  3. *
  4. * Copyright (C) 1996-2005 Markus F.X.J. Oberhumer <markus@oberhumer.com>
  5. *
  6. * The full LZO package can be found at:
  7. * http://www.oberhumer.com/opensource/lzo/
  8. *
  9. * Changed for kernel use by:
  10. * Nitin Gupta <nitingupta910@gmail.com>
  11. * Richard Purdie <rpurdie@openedhand.com>
  12. */
  13. #include <common.h>
  14. #include <linux/lzo.h>
  15. #include <asm/byteorder.h>
  16. #include <asm/unaligned.h>
  17. #include "lzodefs.h"
  18. #define HAVE_IP(x, ip_end, ip) ((size_t)(ip_end - ip) < (x))
  19. #define HAVE_OP(x, op_end, op) ((size_t)(op_end - op) < (x))
  20. #define HAVE_LB(m_pos, out, op) (m_pos < out || m_pos >= op)
  21. #define COPY4(dst, src) \
  22. put_unaligned(get_unaligned((const u32 *)(src)), (u32 *)(dst))
  23. static const unsigned char lzop_magic[] = {
  24. 0x89, 0x4c, 0x5a, 0x4f, 0x00, 0x0d, 0x0a, 0x1a, 0x0a
  25. };
  26. #define HEADER_HAS_FILTER 0x00000800L
  27. static inline const unsigned char *parse_header(const unsigned char *src)
  28. {
  29. u8 level = 0;
  30. u16 version;
  31. int i;
  32. /* read magic: 9 first bytes */
  33. for (i = 0; i < ARRAY_SIZE(lzop_magic); i++) {
  34. if (*src++ != lzop_magic[i])
  35. return NULL;
  36. }
  37. /* get version (2bytes), skip library version (2),
  38. * 'need to be extracted' version (2) and
  39. * method (1) */
  40. version = get_unaligned_be16(src);
  41. src += 7;
  42. if (version >= 0x0940)
  43. level = *src++;
  44. if (get_unaligned_be32(src) & HEADER_HAS_FILTER)
  45. src += 4; /* filter info */
  46. /* skip flags, mode and mtime_low */
  47. src += 12;
  48. if (version >= 0x0940)
  49. src += 4; /* skip mtime_high */
  50. i = *src++;
  51. /* don't care about the file name, and skip checksum */
  52. src += i + 4;
  53. return src;
  54. }
  55. int lzop_decompress(const unsigned char *src, size_t src_len,
  56. unsigned char *dst, size_t *dst_len)
  57. {
  58. unsigned char *start = dst;
  59. const unsigned char *send = src + src_len;
  60. u32 slen, dlen;
  61. size_t tmp;
  62. int r;
  63. src = parse_header(src);
  64. if (!src)
  65. return LZO_E_ERROR;
  66. while (src < send) {
  67. /* read uncompressed block size */
  68. dlen = get_unaligned_be32(src);
  69. src += 4;
  70. /* exit if last block */
  71. if (dlen == 0) {
  72. *dst_len = dst - start;
  73. return LZO_E_OK;
  74. }
  75. /* read compressed block size, and skip block checksum info */
  76. slen = get_unaligned_be32(src);
  77. src += 8;
  78. if (slen <= 0 || slen > dlen)
  79. return LZO_E_ERROR;
  80. /* decompress */
  81. tmp = dlen;
  82. r = lzo1x_decompress_safe((u8 *) src, slen, dst, &tmp);
  83. if (r != LZO_E_OK)
  84. return r;
  85. if (dlen != tmp)
  86. return LZO_E_ERROR;
  87. src += slen;
  88. dst += dlen;
  89. }
  90. return LZO_E_INPUT_OVERRUN;
  91. }
  92. int lzo1x_decompress_safe(const unsigned char *in, size_t in_len,
  93. unsigned char *out, size_t *out_len)
  94. {
  95. const unsigned char * const ip_end = in + in_len;
  96. unsigned char * const op_end = out + *out_len;
  97. const unsigned char *ip = in, *m_pos;
  98. unsigned char *op = out;
  99. size_t t;
  100. *out_len = 0;
  101. if (*ip > 17) {
  102. t = *ip++ - 17;
  103. if (t < 4)
  104. goto match_next;
  105. if (HAVE_OP(t, op_end, op))
  106. goto output_overrun;
  107. if (HAVE_IP(t + 1, ip_end, ip))
  108. goto input_overrun;
  109. do {
  110. *op++ = *ip++;
  111. } while (--t > 0);
  112. goto first_literal_run;
  113. }
  114. while ((ip < ip_end)) {
  115. t = *ip++;
  116. if (t >= 16)
  117. goto match;
  118. if (t == 0) {
  119. if (HAVE_IP(1, ip_end, ip))
  120. goto input_overrun;
  121. while (*ip == 0) {
  122. t += 255;
  123. ip++;
  124. if (HAVE_IP(1, ip_end, ip))
  125. goto input_overrun;
  126. }
  127. t += 15 + *ip++;
  128. }
  129. if (HAVE_OP(t + 3, op_end, op))
  130. goto output_overrun;
  131. if (HAVE_IP(t + 4, ip_end, ip))
  132. goto input_overrun;
  133. COPY4(op, ip);
  134. op += 4;
  135. ip += 4;
  136. if (--t > 0) {
  137. if (t >= 4) {
  138. do {
  139. COPY4(op, ip);
  140. op += 4;
  141. ip += 4;
  142. t -= 4;
  143. } while (t >= 4);
  144. if (t > 0) {
  145. do {
  146. *op++ = *ip++;
  147. } while (--t > 0);
  148. }
  149. } else {
  150. do {
  151. *op++ = *ip++;
  152. } while (--t > 0);
  153. }
  154. }
  155. first_literal_run:
  156. t = *ip++;
  157. if (t >= 16)
  158. goto match;
  159. m_pos = op - (1 + M2_MAX_OFFSET);
  160. m_pos -= t >> 2;
  161. m_pos -= *ip++ << 2;
  162. if (HAVE_LB(m_pos, out, op))
  163. goto lookbehind_overrun;
  164. if (HAVE_OP(3, op_end, op))
  165. goto output_overrun;
  166. *op++ = *m_pos++;
  167. *op++ = *m_pos++;
  168. *op++ = *m_pos;
  169. goto match_done;
  170. do {
  171. match:
  172. if (t >= 64) {
  173. m_pos = op - 1;
  174. m_pos -= (t >> 2) & 7;
  175. m_pos -= *ip++ << 3;
  176. t = (t >> 5) - 1;
  177. if (HAVE_LB(m_pos, out, op))
  178. goto lookbehind_overrun;
  179. if (HAVE_OP(t + 3 - 1, op_end, op))
  180. goto output_overrun;
  181. goto copy_match;
  182. } else if (t >= 32) {
  183. t &= 31;
  184. if (t == 0) {
  185. if (HAVE_IP(1, ip_end, ip))
  186. goto input_overrun;
  187. while (*ip == 0) {
  188. t += 255;
  189. ip++;
  190. if (HAVE_IP(1, ip_end, ip))
  191. goto input_overrun;
  192. }
  193. t += 31 + *ip++;
  194. }
  195. m_pos = op - 1;
  196. m_pos -= get_unaligned_le16(ip) >> 2;
  197. ip += 2;
  198. } else if (t >= 16) {
  199. m_pos = op;
  200. m_pos -= (t & 8) << 11;
  201. t &= 7;
  202. if (t == 0) {
  203. if (HAVE_IP(1, ip_end, ip))
  204. goto input_overrun;
  205. while (*ip == 0) {
  206. t += 255;
  207. ip++;
  208. if (HAVE_IP(1, ip_end, ip))
  209. goto input_overrun;
  210. }
  211. t += 7 + *ip++;
  212. }
  213. m_pos -= get_unaligned_le16(ip) >> 2;
  214. ip += 2;
  215. if (m_pos == op)
  216. goto eof_found;
  217. m_pos -= 0x4000;
  218. } else {
  219. m_pos = op - 1;
  220. m_pos -= t >> 2;
  221. m_pos -= *ip++ << 2;
  222. if (HAVE_LB(m_pos, out, op))
  223. goto lookbehind_overrun;
  224. if (HAVE_OP(2, op_end, op))
  225. goto output_overrun;
  226. *op++ = *m_pos++;
  227. *op++ = *m_pos;
  228. goto match_done;
  229. }
  230. if (HAVE_LB(m_pos, out, op))
  231. goto lookbehind_overrun;
  232. if (HAVE_OP(t + 3 - 1, op_end, op))
  233. goto output_overrun;
  234. if (t >= 2 * 4 - (3 - 1) && (op - m_pos) >= 4) {
  235. COPY4(op, m_pos);
  236. op += 4;
  237. m_pos += 4;
  238. t -= 4 - (3 - 1);
  239. do {
  240. COPY4(op, m_pos);
  241. op += 4;
  242. m_pos += 4;
  243. t -= 4;
  244. } while (t >= 4);
  245. if (t > 0)
  246. do {
  247. *op++ = *m_pos++;
  248. } while (--t > 0);
  249. } else {
  250. copy_match:
  251. *op++ = *m_pos++;
  252. *op++ = *m_pos++;
  253. do {
  254. *op++ = *m_pos++;
  255. } while (--t > 0);
  256. }
  257. match_done:
  258. t = ip[-2] & 3;
  259. if (t == 0)
  260. break;
  261. match_next:
  262. if (HAVE_OP(t, op_end, op))
  263. goto output_overrun;
  264. if (HAVE_IP(t + 1, ip_end, ip))
  265. goto input_overrun;
  266. *op++ = *ip++;
  267. if (t > 1) {
  268. *op++ = *ip++;
  269. if (t > 2)
  270. *op++ = *ip++;
  271. }
  272. t = *ip++;
  273. } while (ip < ip_end);
  274. }
  275. *out_len = op - out;
  276. return LZO_E_EOF_NOT_FOUND;
  277. eof_found:
  278. *out_len = op - out;
  279. return (ip == ip_end ? LZO_E_OK :
  280. (ip < ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
  281. input_overrun:
  282. *out_len = op - out;
  283. return LZO_E_INPUT_OVERRUN;
  284. output_overrun:
  285. *out_len = op - out;
  286. return LZO_E_OUTPUT_OVERRUN;
  287. lookbehind_overrun:
  288. *out_len = op - out;
  289. return LZO_E_LOOKBEHIND_OVERRUN;
  290. }