lzo1x_decompress.c 6.6 KB

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