lzo1x_decompress.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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, remaining;
  61. int r;
  62. src = parse_header(src);
  63. if (!src)
  64. return LZO_E_ERROR;
  65. remaining = *dst_len;
  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. /* abort if buffer ran out of room */
  81. if (dlen > remaining)
  82. return LZO_E_OUTPUT_OVERRUN;
  83. /* decompress */
  84. tmp = dlen;
  85. r = lzo1x_decompress_safe((u8 *) src, slen, dst, &tmp);
  86. if (r != LZO_E_OK) {
  87. *dst_len = dst - start;
  88. return r;
  89. }
  90. if (dlen != tmp)
  91. return LZO_E_ERROR;
  92. src += slen;
  93. dst += dlen;
  94. remaining -= dlen;
  95. }
  96. return LZO_E_INPUT_OVERRUN;
  97. }
  98. int lzo1x_decompress_safe(const unsigned char *in, size_t in_len,
  99. unsigned char *out, size_t *out_len)
  100. {
  101. const unsigned char * const ip_end = in + in_len;
  102. unsigned char * const op_end = out + *out_len;
  103. const unsigned char *ip = in, *m_pos;
  104. unsigned char *op = out;
  105. size_t t;
  106. *out_len = 0;
  107. if (*ip > 17) {
  108. t = *ip++ - 17;
  109. if (t < 4)
  110. goto match_next;
  111. if (HAVE_OP(t, op_end, op))
  112. goto output_overrun;
  113. if (HAVE_IP(t + 1, ip_end, ip))
  114. goto input_overrun;
  115. do {
  116. *op++ = *ip++;
  117. } while (--t > 0);
  118. goto first_literal_run;
  119. }
  120. while ((ip < ip_end)) {
  121. t = *ip++;
  122. if (t >= 16)
  123. goto match;
  124. if (t == 0) {
  125. if (HAVE_IP(1, ip_end, ip))
  126. goto input_overrun;
  127. while (*ip == 0) {
  128. t += 255;
  129. ip++;
  130. if (HAVE_IP(1, ip_end, ip))
  131. goto input_overrun;
  132. }
  133. t += 15 + *ip++;
  134. }
  135. if (HAVE_OP(t + 3, op_end, op))
  136. goto output_overrun;
  137. if (HAVE_IP(t + 4, ip_end, ip))
  138. goto input_overrun;
  139. COPY4(op, ip);
  140. op += 4;
  141. ip += 4;
  142. if (--t > 0) {
  143. if (t >= 4) {
  144. do {
  145. COPY4(op, ip);
  146. op += 4;
  147. ip += 4;
  148. t -= 4;
  149. } while (t >= 4);
  150. if (t > 0) {
  151. do {
  152. *op++ = *ip++;
  153. } while (--t > 0);
  154. }
  155. } else {
  156. do {
  157. *op++ = *ip++;
  158. } while (--t > 0);
  159. }
  160. }
  161. first_literal_run:
  162. t = *ip++;
  163. if (t >= 16)
  164. goto match;
  165. m_pos = op - (1 + M2_MAX_OFFSET);
  166. m_pos -= t >> 2;
  167. m_pos -= *ip++ << 2;
  168. if (HAVE_LB(m_pos, out, op))
  169. goto lookbehind_overrun;
  170. if (HAVE_OP(3, op_end, op))
  171. goto output_overrun;
  172. *op++ = *m_pos++;
  173. *op++ = *m_pos++;
  174. *op++ = *m_pos;
  175. goto match_done;
  176. do {
  177. match:
  178. if (t >= 64) {
  179. m_pos = op - 1;
  180. m_pos -= (t >> 2) & 7;
  181. m_pos -= *ip++ << 3;
  182. t = (t >> 5) - 1;
  183. if (HAVE_LB(m_pos, out, op))
  184. goto lookbehind_overrun;
  185. if (HAVE_OP(t + 3 - 1, op_end, op))
  186. goto output_overrun;
  187. goto copy_match;
  188. } else if (t >= 32) {
  189. t &= 31;
  190. if (t == 0) {
  191. if (HAVE_IP(1, ip_end, ip))
  192. goto input_overrun;
  193. while (*ip == 0) {
  194. t += 255;
  195. ip++;
  196. if (HAVE_IP(1, ip_end, ip))
  197. goto input_overrun;
  198. }
  199. t += 31 + *ip++;
  200. }
  201. m_pos = op - 1;
  202. m_pos -= get_unaligned_le16(ip) >> 2;
  203. ip += 2;
  204. } else if (t >= 16) {
  205. m_pos = op;
  206. m_pos -= (t & 8) << 11;
  207. t &= 7;
  208. if (t == 0) {
  209. if (HAVE_IP(1, ip_end, ip))
  210. goto input_overrun;
  211. while (*ip == 0) {
  212. t += 255;
  213. ip++;
  214. if (HAVE_IP(1, ip_end, ip))
  215. goto input_overrun;
  216. }
  217. t += 7 + *ip++;
  218. }
  219. m_pos -= get_unaligned_le16(ip) >> 2;
  220. ip += 2;
  221. if (m_pos == op)
  222. goto eof_found;
  223. m_pos -= 0x4000;
  224. } else {
  225. m_pos = op - 1;
  226. m_pos -= t >> 2;
  227. m_pos -= *ip++ << 2;
  228. if (HAVE_LB(m_pos, out, op))
  229. goto lookbehind_overrun;
  230. if (HAVE_OP(2, op_end, op))
  231. goto output_overrun;
  232. *op++ = *m_pos++;
  233. *op++ = *m_pos;
  234. goto match_done;
  235. }
  236. if (HAVE_LB(m_pos, out, op))
  237. goto lookbehind_overrun;
  238. if (HAVE_OP(t + 3 - 1, op_end, op))
  239. goto output_overrun;
  240. if (t >= 2 * 4 - (3 - 1) && (op - m_pos) >= 4) {
  241. COPY4(op, m_pos);
  242. op += 4;
  243. m_pos += 4;
  244. t -= 4 - (3 - 1);
  245. do {
  246. COPY4(op, m_pos);
  247. op += 4;
  248. m_pos += 4;
  249. t -= 4;
  250. } while (t >= 4);
  251. if (t > 0)
  252. do {
  253. *op++ = *m_pos++;
  254. } while (--t > 0);
  255. } else {
  256. copy_match:
  257. *op++ = *m_pos++;
  258. *op++ = *m_pos++;
  259. do {
  260. *op++ = *m_pos++;
  261. } while (--t > 0);
  262. }
  263. match_done:
  264. t = ip[-2] & 3;
  265. if (t == 0)
  266. break;
  267. match_next:
  268. if (HAVE_OP(t, op_end, op))
  269. goto output_overrun;
  270. if (HAVE_IP(t + 1, ip_end, ip))
  271. goto input_overrun;
  272. *op++ = *ip++;
  273. if (t > 1) {
  274. *op++ = *ip++;
  275. if (t > 2)
  276. *op++ = *ip++;
  277. }
  278. t = *ip++;
  279. } while (ip < ip_end);
  280. }
  281. *out_len = op - out;
  282. return LZO_E_EOF_NOT_FOUND;
  283. eof_found:
  284. *out_len = op - out;
  285. return (ip == ip_end ? LZO_E_OK :
  286. (ip < ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
  287. input_overrun:
  288. *out_len = op - out;
  289. return LZO_E_INPUT_OVERRUN;
  290. output_overrun:
  291. *out_len = op - out;
  292. return LZO_E_OUTPUT_OVERRUN;
  293. lookbehind_overrun:
  294. *out_len = op - out;
  295. return LZO_E_LOOKBEHIND_OVERRUN;
  296. }