lzo1x_decompress.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 -= get_unaligned_le16(ip) >> 2;
  129. ip += 2;
  130. } else if (t >= 16) {
  131. m_pos = op;
  132. m_pos -= (t & 8) << 11;
  133. t &= 7;
  134. if (t == 0) {
  135. if (HAVE_IP(1, ip_end, ip))
  136. goto input_overrun;
  137. while (*ip == 0) {
  138. t += 255;
  139. ip++;
  140. if (HAVE_IP(1, ip_end, ip))
  141. goto input_overrun;
  142. }
  143. t += 7 + *ip++;
  144. }
  145. m_pos -= get_unaligned_le16(ip) >> 2;
  146. ip += 2;
  147. if (m_pos == op)
  148. goto eof_found;
  149. m_pos -= 0x4000;
  150. } else {
  151. m_pos = op - 1;
  152. m_pos -= t >> 2;
  153. m_pos -= *ip++ << 2;
  154. if (HAVE_LB(m_pos, out, op))
  155. goto lookbehind_overrun;
  156. if (HAVE_OP(2, op_end, op))
  157. goto output_overrun;
  158. *op++ = *m_pos++;
  159. *op++ = *m_pos;
  160. goto match_done;
  161. }
  162. if (HAVE_LB(m_pos, out, op))
  163. goto lookbehind_overrun;
  164. if (HAVE_OP(t + 3 - 1, op_end, op))
  165. goto output_overrun;
  166. if (t >= 2 * 4 - (3 - 1) && (op - m_pos) >= 4) {
  167. COPY4(op, m_pos);
  168. op += 4;
  169. m_pos += 4;
  170. t -= 4 - (3 - 1);
  171. do {
  172. COPY4(op, m_pos);
  173. op += 4;
  174. m_pos += 4;
  175. t -= 4;
  176. } while (t >= 4);
  177. if (t > 0)
  178. do {
  179. *op++ = *m_pos++;
  180. } while (--t > 0);
  181. } else {
  182. copy_match:
  183. *op++ = *m_pos++;
  184. *op++ = *m_pos++;
  185. do {
  186. *op++ = *m_pos++;
  187. } while (--t > 0);
  188. }
  189. match_done:
  190. t = ip[-2] & 3;
  191. if (t == 0)
  192. break;
  193. match_next:
  194. if (HAVE_OP(t, op_end, op))
  195. goto output_overrun;
  196. if (HAVE_IP(t + 1, ip_end, ip))
  197. goto input_overrun;
  198. *op++ = *ip++;
  199. if (t > 1) {
  200. *op++ = *ip++;
  201. if (t > 2)
  202. *op++ = *ip++;
  203. }
  204. t = *ip++;
  205. } while (ip < ip_end);
  206. }
  207. *out_len = op - out;
  208. return LZO_E_EOF_NOT_FOUND;
  209. eof_found:
  210. *out_len = op - out;
  211. return (ip == ip_end ? LZO_E_OK :
  212. (ip < ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
  213. input_overrun:
  214. *out_len = op - out;
  215. return LZO_E_INPUT_OVERRUN;
  216. output_overrun:
  217. *out_len = op - out;
  218. return LZO_E_OUTPUT_OVERRUN;
  219. lookbehind_overrun:
  220. *out_len = op - out;
  221. return LZO_E_LOOKBEHIND_OVERRUN;
  222. }
  223. EXPORT_SYMBOL_GPL(lzo1x_decompress_safe);
  224. MODULE_LICENSE("GPL");
  225. MODULE_DESCRIPTION("LZO1X Decompressor");