lzo1x_decompress.c 6.9 KB

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