lzo1x_decompress.c 6.5 KB

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