lzo1x_decompress_safe.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * LZO1X Decompressor from LZO
  3. *
  4. * Copyright (C) 1996-2012 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 Linux 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(t, x) \
  21. (((size_t)(ip_end - ip) >= (size_t)(t + x)) && \
  22. (((t + x) >= t) && ((t + x) >= x)))
  23. #define HAVE_OP(t, x) \
  24. (((size_t)(op_end - op) >= (size_t)(t + x)) && \
  25. (((t + x) >= t) && ((t + x) >= x)))
  26. #define NEED_IP(t, x) \
  27. do { \
  28. if (!HAVE_IP(t, x)) \
  29. goto input_overrun; \
  30. } while (0)
  31. #define NEED_OP(t, x) \
  32. do { \
  33. if (!HAVE_OP(t, x)) \
  34. goto output_overrun; \
  35. } while (0)
  36. #define TEST_LB(m_pos) \
  37. do { \
  38. if ((m_pos) < out) \
  39. goto lookbehind_overrun; \
  40. } while (0)
  41. int lzo1x_decompress_safe(const unsigned char *in, size_t in_len,
  42. unsigned char *out, size_t *out_len)
  43. {
  44. unsigned char *op;
  45. const unsigned char *ip;
  46. size_t t, next;
  47. size_t state = 0;
  48. const unsigned char *m_pos;
  49. const unsigned char * const ip_end = in + in_len;
  50. unsigned char * const op_end = out + *out_len;
  51. op = out;
  52. ip = in;
  53. if (unlikely(in_len < 3))
  54. goto input_overrun;
  55. if (*ip > 17) {
  56. t = *ip++ - 17;
  57. if (t < 4) {
  58. next = t;
  59. goto match_next;
  60. }
  61. goto copy_literal_run;
  62. }
  63. for (;;) {
  64. t = *ip++;
  65. if (t < 16) {
  66. if (likely(state == 0)) {
  67. if (unlikely(t == 0)) {
  68. while (unlikely(*ip == 0)) {
  69. t += 255;
  70. ip++;
  71. NEED_IP(1, 0);
  72. }
  73. t += 15 + *ip++;
  74. }
  75. t += 3;
  76. copy_literal_run:
  77. #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
  78. if (likely(HAVE_IP(t, 15) && HAVE_OP(t, 15))) {
  79. const unsigned char *ie = ip + t;
  80. unsigned char *oe = op + t;
  81. do {
  82. COPY8(op, ip);
  83. op += 8;
  84. ip += 8;
  85. COPY8(op, ip);
  86. op += 8;
  87. ip += 8;
  88. } while (ip < ie);
  89. ip = ie;
  90. op = oe;
  91. } else
  92. #endif
  93. {
  94. NEED_OP(t, 0);
  95. NEED_IP(t, 3);
  96. do {
  97. *op++ = *ip++;
  98. } while (--t > 0);
  99. }
  100. state = 4;
  101. continue;
  102. } else if (state != 4) {
  103. next = t & 3;
  104. m_pos = op - 1;
  105. m_pos -= t >> 2;
  106. m_pos -= *ip++ << 2;
  107. TEST_LB(m_pos);
  108. NEED_OP(2, 0);
  109. op[0] = m_pos[0];
  110. op[1] = m_pos[1];
  111. op += 2;
  112. goto match_next;
  113. } else {
  114. next = t & 3;
  115. m_pos = op - (1 + M2_MAX_OFFSET);
  116. m_pos -= t >> 2;
  117. m_pos -= *ip++ << 2;
  118. t = 3;
  119. }
  120. } else if (t >= 64) {
  121. next = t & 3;
  122. m_pos = op - 1;
  123. m_pos -= (t >> 2) & 7;
  124. m_pos -= *ip++ << 3;
  125. t = (t >> 5) - 1 + (3 - 1);
  126. } else if (t >= 32) {
  127. t = (t & 31) + (3 - 1);
  128. if (unlikely(t == 2)) {
  129. while (unlikely(*ip == 0)) {
  130. t += 255;
  131. ip++;
  132. NEED_IP(1, 0);
  133. }
  134. t += 31 + *ip++;
  135. NEED_IP(2, 0);
  136. }
  137. m_pos = op - 1;
  138. next = get_unaligned_le16(ip);
  139. ip += 2;
  140. m_pos -= next >> 2;
  141. next &= 3;
  142. } else {
  143. m_pos = op;
  144. m_pos -= (t & 8) << 11;
  145. t = (t & 7) + (3 - 1);
  146. if (unlikely(t == 2)) {
  147. while (unlikely(*ip == 0)) {
  148. t += 255;
  149. ip++;
  150. NEED_IP(1, 0);
  151. }
  152. t += 7 + *ip++;
  153. NEED_IP(2, 0);
  154. }
  155. next = get_unaligned_le16(ip);
  156. ip += 2;
  157. m_pos -= next >> 2;
  158. next &= 3;
  159. if (m_pos == op)
  160. goto eof_found;
  161. m_pos -= 0x4000;
  162. }
  163. TEST_LB(m_pos);
  164. #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
  165. if (op - m_pos >= 8) {
  166. unsigned char *oe = op + t;
  167. if (likely(HAVE_OP(t, 15))) {
  168. do {
  169. COPY8(op, m_pos);
  170. op += 8;
  171. m_pos += 8;
  172. COPY8(op, m_pos);
  173. op += 8;
  174. m_pos += 8;
  175. } while (op < oe);
  176. op = oe;
  177. if (HAVE_IP(6, 0)) {
  178. state = next;
  179. COPY4(op, ip);
  180. op += next;
  181. ip += next;
  182. continue;
  183. }
  184. } else {
  185. NEED_OP(t, 0);
  186. do {
  187. *op++ = *m_pos++;
  188. } while (op < oe);
  189. }
  190. } else
  191. #endif
  192. {
  193. unsigned char *oe = op + t;
  194. NEED_OP(t, 0);
  195. op[0] = m_pos[0];
  196. op[1] = m_pos[1];
  197. op += 2;
  198. m_pos += 2;
  199. do {
  200. *op++ = *m_pos++;
  201. } while (op < oe);
  202. }
  203. match_next:
  204. state = next;
  205. t = next;
  206. #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
  207. if (likely(HAVE_IP(6, 0) && HAVE_OP(4, 0))) {
  208. COPY4(op, ip);
  209. op += t;
  210. ip += t;
  211. } else
  212. #endif
  213. {
  214. NEED_IP(t, 3);
  215. NEED_OP(t, 0);
  216. while (t > 0) {
  217. *op++ = *ip++;
  218. t--;
  219. }
  220. }
  221. }
  222. eof_found:
  223. *out_len = op - out;
  224. return (t != 3 ? LZO_E_ERROR :
  225. ip == ip_end ? LZO_E_OK :
  226. ip < ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN);
  227. input_overrun:
  228. *out_len = op - out;
  229. return LZO_E_INPUT_OVERRUN;
  230. output_overrun:
  231. *out_len = op - out;
  232. return LZO_E_OUTPUT_OVERRUN;
  233. lookbehind_overrun:
  234. *out_len = op - out;
  235. return LZO_E_LOOKBEHIND_OVERRUN;
  236. }
  237. #ifndef STATIC
  238. EXPORT_SYMBOL_GPL(lzo1x_decompress_safe);
  239. MODULE_LICENSE("GPL");
  240. MODULE_DESCRIPTION("LZO1X Decompressor");
  241. #endif