lzo1x_decompress.c 4.9 KB

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