lzo1x_compress.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * LZO1X Compressor 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/unaligned.h>
  17. #include "lzodefs.h"
  18. static noinline size_t
  19. _lzo1x_1_do_compress(const unsigned char *in, size_t in_len,
  20. unsigned char *out, size_t *out_len, void *wrkmem)
  21. {
  22. const unsigned char * const in_end = in + in_len;
  23. const unsigned char * const ip_end = in + in_len - M2_MAX_LEN - 5;
  24. const unsigned char ** const dict = wrkmem;
  25. const unsigned char *ip = in, *ii = ip;
  26. const unsigned char *end, *m, *m_pos;
  27. size_t m_off, m_len, dindex;
  28. unsigned char *op = out;
  29. ip += 4;
  30. for (;;) {
  31. dindex = ((size_t)(0x21 * DX3(ip, 5, 5, 6)) >> 5) & D_MASK;
  32. m_pos = dict[dindex];
  33. if (m_pos < in)
  34. goto literal;
  35. if (ip == m_pos || ((size_t)(ip - m_pos) > M4_MAX_OFFSET))
  36. goto literal;
  37. m_off = ip - m_pos;
  38. if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
  39. goto try_match;
  40. dindex = (dindex & (D_MASK & 0x7ff)) ^ (D_HIGH | 0x1f);
  41. m_pos = dict[dindex];
  42. if (m_pos < in)
  43. goto literal;
  44. if (ip == m_pos || ((size_t)(ip - m_pos) > M4_MAX_OFFSET))
  45. goto literal;
  46. m_off = ip - m_pos;
  47. if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
  48. goto try_match;
  49. goto literal;
  50. try_match:
  51. if (get_unaligned((const unsigned short *)m_pos)
  52. == get_unaligned((const unsigned short *)ip)) {
  53. if (likely(m_pos[2] == ip[2]))
  54. goto match;
  55. }
  56. literal:
  57. dict[dindex] = ip;
  58. ++ip;
  59. if (unlikely(ip >= ip_end))
  60. break;
  61. continue;
  62. match:
  63. dict[dindex] = ip;
  64. if (ip != ii) {
  65. size_t t = ip - ii;
  66. if (t <= 3) {
  67. op[-2] |= t;
  68. } else if (t <= 18) {
  69. *op++ = (t - 3);
  70. } else {
  71. size_t tt = t - 18;
  72. *op++ = 0;
  73. while (tt > 255) {
  74. tt -= 255;
  75. *op++ = 0;
  76. }
  77. *op++ = tt;
  78. }
  79. do {
  80. *op++ = *ii++;
  81. } while (--t > 0);
  82. }
  83. ip += 3;
  84. if (m_pos[3] != *ip++ || m_pos[4] != *ip++
  85. || m_pos[5] != *ip++ || m_pos[6] != *ip++
  86. || m_pos[7] != *ip++ || m_pos[8] != *ip++) {
  87. --ip;
  88. m_len = ip - ii;
  89. if (m_off <= M2_MAX_OFFSET) {
  90. m_off -= 1;
  91. *op++ = (((m_len - 1) << 5)
  92. | ((m_off & 7) << 2));
  93. *op++ = (m_off >> 3);
  94. } else if (m_off <= M3_MAX_OFFSET) {
  95. m_off -= 1;
  96. *op++ = (M3_MARKER | (m_len - 2));
  97. goto m3_m4_offset;
  98. } else {
  99. m_off -= 0x4000;
  100. *op++ = (M4_MARKER | ((m_off & 0x4000) >> 11)
  101. | (m_len - 2));
  102. goto m3_m4_offset;
  103. }
  104. } else {
  105. end = in_end;
  106. m = m_pos + M2_MAX_LEN + 1;
  107. while (ip < end && *m == *ip) {
  108. m++;
  109. ip++;
  110. }
  111. m_len = ip - ii;
  112. if (m_off <= M3_MAX_OFFSET) {
  113. m_off -= 1;
  114. if (m_len <= 33) {
  115. *op++ = (M3_MARKER | (m_len - 2));
  116. } else {
  117. m_len -= 33;
  118. *op++ = M3_MARKER | 0;
  119. goto m3_m4_len;
  120. }
  121. } else {
  122. m_off -= 0x4000;
  123. if (m_len <= M4_MAX_LEN) {
  124. *op++ = (M4_MARKER
  125. | ((m_off & 0x4000) >> 11)
  126. | (m_len - 2));
  127. } else {
  128. m_len -= M4_MAX_LEN;
  129. *op++ = (M4_MARKER
  130. | ((m_off & 0x4000) >> 11));
  131. m3_m4_len:
  132. while (m_len > 255) {
  133. m_len -= 255;
  134. *op++ = 0;
  135. }
  136. *op++ = (m_len);
  137. }
  138. }
  139. m3_m4_offset:
  140. *op++ = ((m_off & 63) << 2);
  141. *op++ = (m_off >> 6);
  142. }
  143. ii = ip;
  144. if (unlikely(ip >= ip_end))
  145. break;
  146. }
  147. *out_len = op - out;
  148. return in_end - ii;
  149. }
  150. int lzo1x_1_compress(const unsigned char *in, size_t in_len, unsigned char *out,
  151. size_t *out_len, void *wrkmem)
  152. {
  153. const unsigned char *ii;
  154. unsigned char *op = out;
  155. size_t t;
  156. if (unlikely(in_len <= M2_MAX_LEN + 5)) {
  157. t = in_len;
  158. } else {
  159. t = _lzo1x_1_do_compress(in, in_len, op, out_len, wrkmem);
  160. op += *out_len;
  161. }
  162. if (t > 0) {
  163. ii = in + in_len - t;
  164. if (op == out && t <= 238) {
  165. *op++ = (17 + t);
  166. } else if (t <= 3) {
  167. op[-2] |= t;
  168. } else if (t <= 18) {
  169. *op++ = (t - 3);
  170. } else {
  171. size_t tt = t - 18;
  172. *op++ = 0;
  173. while (tt > 255) {
  174. tt -= 255;
  175. *op++ = 0;
  176. }
  177. *op++ = tt;
  178. }
  179. do {
  180. *op++ = *ii++;
  181. } while (--t > 0);
  182. }
  183. *op++ = M4_MARKER | 1;
  184. *op++ = 0;
  185. *op++ = 0;
  186. *out_len = op - out;
  187. return LZO_E_OK;
  188. }
  189. EXPORT_SYMBOL_GPL(lzo1x_1_compress);
  190. MODULE_LICENSE("GPL");
  191. MODULE_DESCRIPTION("LZO1X-1 Compressor");