blob: 236eb21167b5db1f5cd542efbf6c66dd770455fc [file] [log] [blame]
Richard Purdie64c70b12007-07-10 17:22:24 -07001/*
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +02002 * LZO1X Compressor from LZO
Richard Purdie64c70b12007-07-10 17:22:24 -07003 *
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +02004 * Copyright (C) 1996-2012 Markus F.X.J. Oberhumer <markus@oberhumer.com>
Richard Purdie64c70b12007-07-10 17:22:24 -07005 *
6 * The full LZO package can be found at:
7 * http://www.oberhumer.com/opensource/lzo/
8 *
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +02009 * Changed for Linux kernel use by:
Richard Purdie64c70b12007-07-10 17:22:24 -070010 * Nitin Gupta <nitingupta910@gmail.com>
11 * Richard Purdie <rpurdie@openedhand.com>
12 */
13
14#include <linux/module.h>
15#include <linux/kernel.h>
Richard Purdie64c70b12007-07-10 17:22:24 -070016#include <asm/unaligned.h>
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +020017#include <linux/lzo.h>
Richard Purdie64c70b12007-07-10 17:22:24 -070018#include "lzodefs.h"
19
20static noinline size_t
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +020021lzo1x_1_do_compress(const unsigned char *in, size_t in_len,
22 unsigned char *out, size_t *out_len,
23 size_t ti, void *wrkmem)
Richard Purdie64c70b12007-07-10 17:22:24 -070024{
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +020025 const unsigned char *ip;
26 unsigned char *op;
Richard Purdie64c70b12007-07-10 17:22:24 -070027 const unsigned char * const in_end = in + in_len;
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +020028 const unsigned char * const ip_end = in + in_len - 20;
29 const unsigned char *ii;
30 lzo_dict_t * const dict = (lzo_dict_t *) wrkmem;
Richard Purdie64c70b12007-07-10 17:22:24 -070031
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +020032 op = out;
33 ip = in;
34 ii = ip;
35 ip += ti < 4 ? 4 - ti : 0;
Richard Purdie64c70b12007-07-10 17:22:24 -070036
37 for (;;) {
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +020038 const unsigned char *m_pos;
39 size_t t, m_len, m_off;
40 u32 dv;
Richard Purdie64c70b12007-07-10 17:22:24 -070041literal:
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +020042 ip += 1 + ((ip - ii) >> 5);
43next:
Richard Purdie64c70b12007-07-10 17:22:24 -070044 if (unlikely(ip >= ip_end))
45 break;
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +020046 dv = get_unaligned_le32(ip);
47 t = ((dv * 0x1824429d) >> (32 - D_BITS)) & D_MASK;
48 m_pos = in + dict[t];
49 dict[t] = (lzo_dict_t) (ip - in);
50 if (unlikely(dv != get_unaligned_le32(m_pos)))
51 goto literal;
Richard Purdie64c70b12007-07-10 17:22:24 -070052
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +020053 ii -= ti;
54 ti = 0;
55 t = ip - ii;
56 if (t != 0) {
Richard Purdie64c70b12007-07-10 17:22:24 -070057 if (t <= 3) {
58 op[-2] |= t;
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +020059 COPY4(op, ii);
60 op += t;
61 } else if (t <= 16) {
Richard Purdie64c70b12007-07-10 17:22:24 -070062 *op++ = (t - 3);
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +020063 COPY8(op, ii);
64 COPY8(op + 8, ii + 8);
65 op += t;
Richard Purdie64c70b12007-07-10 17:22:24 -070066 } else {
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +020067 if (t <= 18) {
68 *op++ = (t - 3);
69 } else {
70 size_t tt = t - 18;
Richard Purdie64c70b12007-07-10 17:22:24 -070071 *op++ = 0;
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +020072 while (unlikely(tt > 255)) {
73 tt -= 255;
Richard Purdie64c70b12007-07-10 17:22:24 -070074 *op++ = 0;
75 }
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +020076 *op++ = tt;
Richard Purdie64c70b12007-07-10 17:22:24 -070077 }
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +020078 do {
79 COPY8(op, ii);
80 COPY8(op + 8, ii + 8);
81 op += 16;
82 ii += 16;
83 t -= 16;
84 } while (t >= 16);
85 if (t > 0) do {
86 *op++ = *ii++;
87 } while (--t > 0);
Richard Purdie64c70b12007-07-10 17:22:24 -070088 }
Richard Purdie64c70b12007-07-10 17:22:24 -070089 }
90
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +020091 m_len = 4;
92 {
93#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && defined(LZO_USE_CTZ64)
94 u64 v;
95 v = get_unaligned((const u64 *) (ip + m_len)) ^
96 get_unaligned((const u64 *) (m_pos + m_len));
97 if (unlikely(v == 0)) {
98 do {
99 m_len += 8;
100 v = get_unaligned((const u64 *) (ip + m_len)) ^
101 get_unaligned((const u64 *) (m_pos + m_len));
102 if (unlikely(ip + m_len >= ip_end))
103 goto m_len_done;
104 } while (v == 0);
105 }
106# if defined(__LITTLE_ENDIAN)
107 m_len += (unsigned) __builtin_ctzll(v) / 8;
108# elif defined(__BIG_ENDIAN)
109 m_len += (unsigned) __builtin_clzll(v) / 8;
110# else
111# error "missing endian definition"
112# endif
113#elif defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && defined(LZO_USE_CTZ32)
114 u32 v;
115 v = get_unaligned((const u32 *) (ip + m_len)) ^
116 get_unaligned((const u32 *) (m_pos + m_len));
117 if (unlikely(v == 0)) {
118 do {
119 m_len += 4;
120 v = get_unaligned((const u32 *) (ip + m_len)) ^
121 get_unaligned((const u32 *) (m_pos + m_len));
122 if (v != 0)
123 break;
124 m_len += 4;
125 v = get_unaligned((const u32 *) (ip + m_len)) ^
126 get_unaligned((const u32 *) (m_pos + m_len));
127 if (unlikely(ip + m_len >= ip_end))
128 goto m_len_done;
129 } while (v == 0);
130 }
131# if defined(__LITTLE_ENDIAN)
132 m_len += (unsigned) __builtin_ctz(v) / 8;
133# elif defined(__BIG_ENDIAN)
134 m_len += (unsigned) __builtin_clz(v) / 8;
135# else
136# error "missing endian definition"
137# endif
138#else
139 if (unlikely(ip[m_len] == m_pos[m_len])) {
140 do {
141 m_len += 1;
142 if (ip[m_len] != m_pos[m_len])
143 break;
144 m_len += 1;
145 if (ip[m_len] != m_pos[m_len])
146 break;
147 m_len += 1;
148 if (ip[m_len] != m_pos[m_len])
149 break;
150 m_len += 1;
151 if (ip[m_len] != m_pos[m_len])
152 break;
153 m_len += 1;
154 if (ip[m_len] != m_pos[m_len])
155 break;
156 m_len += 1;
157 if (ip[m_len] != m_pos[m_len])
158 break;
159 m_len += 1;
160 if (ip[m_len] != m_pos[m_len])
161 break;
162 m_len += 1;
163 if (unlikely(ip + m_len >= ip_end))
164 goto m_len_done;
165 } while (ip[m_len] == m_pos[m_len]);
166 }
167#endif
168 }
169m_len_done:
Richard Purdie64c70b12007-07-10 17:22:24 -0700170
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200171 m_off = ip - m_pos;
172 ip += m_len;
173 ii = ip;
174 if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET) {
175 m_off -= 1;
176 *op++ = (((m_len - 1) << 5) | ((m_off & 7) << 2));
177 *op++ = (m_off >> 3);
178 } else if (m_off <= M3_MAX_OFFSET) {
179 m_off -= 1;
180 if (m_len <= M3_MAX_LEN)
181 *op++ = (M3_MARKER | (m_len - 2));
182 else {
183 m_len -= M3_MAX_LEN;
184 *op++ = M3_MARKER | 0;
185 while (unlikely(m_len > 255)) {
186 m_len -= 255;
187 *op++ = 0;
188 }
189 *op++ = (m_len);
190 }
191 *op++ = (m_off << 2);
192 *op++ = (m_off >> 6);
193 } else {
194 m_off -= 0x4000;
195 if (m_len <= M4_MAX_LEN)
196 *op++ = (M4_MARKER | ((m_off >> 11) & 8)
197 | (m_len - 2));
198 else {
199 m_len -= M4_MAX_LEN;
200 *op++ = (M4_MARKER | ((m_off >> 11) & 8));
201 while (unlikely(m_len > 255)) {
202 m_len -= 255;
203 *op++ = 0;
204 }
205 *op++ = (m_len);
206 }
207 *op++ = (m_off << 2);
208 *op++ = (m_off >> 6);
209 }
210 goto next;
211 }
Richard Purdie64c70b12007-07-10 17:22:24 -0700212 *out_len = op - out;
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200213 return in_end - (ii - ti);
Richard Purdie64c70b12007-07-10 17:22:24 -0700214}
215
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200216int lzo1x_1_compress(const unsigned char *in, size_t in_len,
217 unsigned char *out, size_t *out_len,
218 void *wrkmem)
Richard Purdie64c70b12007-07-10 17:22:24 -0700219{
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200220 const unsigned char *ip = in;
Richard Purdie64c70b12007-07-10 17:22:24 -0700221 unsigned char *op = out;
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200222 size_t l = in_len;
223 size_t t = 0;
Richard Purdie64c70b12007-07-10 17:22:24 -0700224
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200225 while (l > 20) {
226 size_t ll = l <= (M4_MAX_OFFSET + 1) ? l : (M4_MAX_OFFSET + 1);
227 uintptr_t ll_end = (uintptr_t) ip + ll;
228 if ((ll_end + ((t + ll) >> 5)) <= ll_end)
229 break;
230 BUILD_BUG_ON(D_SIZE * sizeof(lzo_dict_t) > LZO1X_1_MEM_COMPRESS);
231 memset(wrkmem, 0, D_SIZE * sizeof(lzo_dict_t));
232 t = lzo1x_1_do_compress(ip, ll, op, out_len, t, wrkmem);
233 ip += ll;
Richard Purdie64c70b12007-07-10 17:22:24 -0700234 op += *out_len;
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200235 l -= ll;
Richard Purdie64c70b12007-07-10 17:22:24 -0700236 }
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200237 t += l;
Richard Purdie64c70b12007-07-10 17:22:24 -0700238
239 if (t > 0) {
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200240 const unsigned char *ii = in + in_len - t;
Richard Purdie64c70b12007-07-10 17:22:24 -0700241
242 if (op == out && t <= 238) {
243 *op++ = (17 + t);
244 } else if (t <= 3) {
245 op[-2] |= t;
246 } else if (t <= 18) {
247 *op++ = (t - 3);
248 } else {
249 size_t tt = t - 18;
Richard Purdie64c70b12007-07-10 17:22:24 -0700250 *op++ = 0;
251 while (tt > 255) {
252 tt -= 255;
253 *op++ = 0;
254 }
Richard Purdie64c70b12007-07-10 17:22:24 -0700255 *op++ = tt;
256 }
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200257 if (t >= 16) do {
258 COPY8(op, ii);
259 COPY8(op + 8, ii + 8);
260 op += 16;
261 ii += 16;
262 t -= 16;
263 } while (t >= 16);
264 if (t > 0) do {
Richard Purdie64c70b12007-07-10 17:22:24 -0700265 *op++ = *ii++;
266 } while (--t > 0);
267 }
268
269 *op++ = M4_MARKER | 1;
270 *op++ = 0;
271 *op++ = 0;
272
273 *out_len = op - out;
274 return LZO_E_OK;
275}
276EXPORT_SYMBOL_GPL(lzo1x_1_compress);
277
278MODULE_LICENSE("GPL");
279MODULE_DESCRIPTION("LZO1X-1 Compressor");