lzo1x_decompress.c 5.0 KB

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