lzo1x_decompress.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. int lzo1x_decompress_safe(const unsigned char *in, size_t in_len,
  24. unsigned char *out, size_t *out_len)
  25. {
  26. const unsigned char * const ip_end = in + in_len;
  27. unsigned char * const op_end = out + *out_len;
  28. const unsigned char *ip = in, *m_pos;
  29. unsigned char *op = out;
  30. size_t t;
  31. *out_len = 0;
  32. if (*ip > 17) {
  33. t = *ip++ - 17;
  34. if (t < 4)
  35. goto match_next;
  36. if (HAVE_OP(t, op_end, op))
  37. goto output_overrun;
  38. if (HAVE_IP(t + 1, ip_end, ip))
  39. goto input_overrun;
  40. do {
  41. *op++ = *ip++;
  42. } while (--t > 0);
  43. goto first_literal_run;
  44. }
  45. while ((ip < ip_end)) {
  46. t = *ip++;
  47. if (t >= 16)
  48. goto match;
  49. if (t == 0) {
  50. if (HAVE_IP(1, ip_end, ip))
  51. goto input_overrun;
  52. while (*ip == 0) {
  53. t += 255;
  54. ip++;
  55. if (HAVE_IP(1, ip_end, ip))
  56. goto input_overrun;
  57. }
  58. t += 15 + *ip++;
  59. }
  60. if (HAVE_OP(t + 3, op_end, op))
  61. goto output_overrun;
  62. if (HAVE_IP(t + 4, ip_end, ip))
  63. goto input_overrun;
  64. COPY4(op, ip);
  65. op += 4;
  66. ip += 4;
  67. if (--t > 0) {
  68. if (t >= 4) {
  69. do {
  70. COPY4(op, ip);
  71. op += 4;
  72. ip += 4;
  73. t -= 4;
  74. } while (t >= 4);
  75. if (t > 0) {
  76. do {
  77. *op++ = *ip++;
  78. } while (--t > 0);
  79. }
  80. } else {
  81. do {
  82. *op++ = *ip++;
  83. } while (--t > 0);
  84. }
  85. }
  86. first_literal_run:
  87. t = *ip++;
  88. if (t >= 16)
  89. goto match;
  90. m_pos = op - (1 + M2_MAX_OFFSET);
  91. m_pos -= t >> 2;
  92. m_pos -= *ip++ << 2;
  93. if (HAVE_LB(m_pos, out, op))
  94. goto lookbehind_overrun;
  95. if (HAVE_OP(3, op_end, op))
  96. goto output_overrun;
  97. *op++ = *m_pos++;
  98. *op++ = *m_pos++;
  99. *op++ = *m_pos;
  100. goto match_done;
  101. do {
  102. match:
  103. if (t >= 64) {
  104. m_pos = op - 1;
  105. m_pos -= (t >> 2) & 7;
  106. m_pos -= *ip++ << 3;
  107. t = (t >> 5) - 1;
  108. if (HAVE_LB(m_pos, out, op))
  109. goto lookbehind_overrun;
  110. if (HAVE_OP(t + 3 - 1, op_end, op))
  111. goto output_overrun;
  112. goto copy_match;
  113. } else if (t >= 32) {
  114. t &= 31;
  115. if (t == 0) {
  116. if (HAVE_IP(1, ip_end, ip))
  117. goto input_overrun;
  118. while (*ip == 0) {
  119. t += 255;
  120. ip++;
  121. if (HAVE_IP(1, ip_end, ip))
  122. goto input_overrun;
  123. }
  124. t += 31 + *ip++;
  125. }
  126. m_pos = op - 1;
  127. m_pos -= get_unaligned_le16(ip) >> 2;
  128. ip += 2;
  129. } else if (t >= 16) {
  130. m_pos = op;
  131. m_pos -= (t & 8) << 11;
  132. t &= 7;
  133. if (t == 0) {
  134. if (HAVE_IP(1, ip_end, ip))
  135. goto input_overrun;
  136. while (*ip == 0) {
  137. t += 255;
  138. ip++;
  139. if (HAVE_IP(1, ip_end, ip))
  140. goto input_overrun;
  141. }
  142. t += 7 + *ip++;
  143. }
  144. m_pos -= get_unaligned_le16(ip) >> 2;
  145. ip += 2;
  146. if (m_pos == op)
  147. goto eof_found;
  148. m_pos -= 0x4000;
  149. } else {
  150. m_pos = op - 1;
  151. m_pos -= t >> 2;
  152. m_pos -= *ip++ << 2;
  153. if (HAVE_LB(m_pos, out, op))
  154. goto lookbehind_overrun;
  155. if (HAVE_OP(2, op_end, op))
  156. goto output_overrun;
  157. *op++ = *m_pos++;
  158. *op++ = *m_pos;
  159. goto match_done;
  160. }
  161. if (HAVE_LB(m_pos, out, op))
  162. goto lookbehind_overrun;
  163. if (HAVE_OP(t + 3 - 1, op_end, op))
  164. goto output_overrun;
  165. if (t >= 2 * 4 - (3 - 1) && (op - m_pos) >= 4) {
  166. COPY4(op, m_pos);
  167. op += 4;
  168. m_pos += 4;
  169. t -= 4 - (3 - 1);
  170. do {
  171. COPY4(op, m_pos);
  172. op += 4;
  173. m_pos += 4;
  174. t -= 4;
  175. } while (t >= 4);
  176. if (t > 0)
  177. do {
  178. *op++ = *m_pos++;
  179. } while (--t > 0);
  180. } else {
  181. copy_match:
  182. *op++ = *m_pos++;
  183. *op++ = *m_pos++;
  184. do {
  185. *op++ = *m_pos++;
  186. } while (--t > 0);
  187. }
  188. match_done:
  189. t = ip[-2] & 3;
  190. if (t == 0)
  191. break;
  192. match_next:
  193. if (HAVE_OP(t, op_end, op))
  194. goto output_overrun;
  195. if (HAVE_IP(t + 1, ip_end, ip))
  196. goto input_overrun;
  197. *op++ = *ip++;
  198. if (t > 1) {
  199. *op++ = *ip++;
  200. if (t > 2)
  201. *op++ = *ip++;
  202. }
  203. t = *ip++;
  204. } while (ip < ip_end);
  205. }
  206. *out_len = op - out;
  207. return LZO_E_EOF_NOT_FOUND;
  208. eof_found:
  209. *out_len = op - out;
  210. return (ip == ip_end ? LZO_E_OK :
  211. (ip < ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
  212. input_overrun:
  213. *out_len = op - out;
  214. return LZO_E_INPUT_OVERRUN;
  215. output_overrun:
  216. *out_len = op - out;
  217. return LZO_E_OUTPUT_OVERRUN;
  218. lookbehind_overrun:
  219. *out_len = op - out;
  220. return LZO_E_LOOKBEHIND_OVERRUN;
  221. }