blob: 4525fb09484427297853ca5819dcfaffee9265f1 [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,
Dave Rodgman45ec9752019-03-07 16:30:44 -080023 size_t ti, void *wrkmem, signed char *state_offset,
24 const unsigned char bitstream_version)
Richard Purdie64c70b12007-07-10 17:22:24 -070025{
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +020026 const unsigned char *ip;
27 unsigned char *op;
Richard Purdie64c70b12007-07-10 17:22:24 -070028 const unsigned char * const in_end = in + in_len;
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +020029 const unsigned char * const ip_end = in + in_len - 20;
30 const unsigned char *ii;
31 lzo_dict_t * const dict = (lzo_dict_t *) wrkmem;
Richard Purdie64c70b12007-07-10 17:22:24 -070032
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +020033 op = out;
34 ip = in;
35 ii = ip;
36 ip += ti < 4 ? 4 - ti : 0;
Richard Purdie64c70b12007-07-10 17:22:24 -070037
38 for (;;) {
Dave Rodgman5ee40142019-03-07 16:30:40 -080039 const unsigned char *m_pos = NULL;
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +020040 size_t t, m_len, m_off;
41 u32 dv;
Dave Rodgman5ee40142019-03-07 16:30:40 -080042 u32 run_length = 0;
Richard Purdie64c70b12007-07-10 17:22:24 -070043literal:
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +020044 ip += 1 + ((ip - ii) >> 5);
45next:
Richard Purdie64c70b12007-07-10 17:22:24 -070046 if (unlikely(ip >= ip_end))
47 break;
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +020048 dv = get_unaligned_le32(ip);
Dave Rodgman5ee40142019-03-07 16:30:40 -080049
Dave Rodgman45ec9752019-03-07 16:30:44 -080050 if (dv == 0 && bitstream_version) {
Dave Rodgman5ee40142019-03-07 16:30:40 -080051 const unsigned char *ir = ip + 4;
52 const unsigned char *limit = ip_end
53 < (ip + MAX_ZERO_RUN_LENGTH + 1)
54 ? ip_end : ip + MAX_ZERO_RUN_LENGTH + 1;
55#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && \
56 defined(LZO_FAST_64BIT_MEMORY_ACCESS)
57 u64 dv64;
58
59 for (; (ir + 32) <= limit; ir += 32) {
60 dv64 = get_unaligned((u64 *)ir);
61 dv64 |= get_unaligned((u64 *)ir + 1);
62 dv64 |= get_unaligned((u64 *)ir + 2);
63 dv64 |= get_unaligned((u64 *)ir + 3);
64 if (dv64)
65 break;
66 }
67 for (; (ir + 8) <= limit; ir += 8) {
68 dv64 = get_unaligned((u64 *)ir);
69 if (dv64) {
70# if defined(__LITTLE_ENDIAN)
71 ir += __builtin_ctzll(dv64) >> 3;
72# elif defined(__BIG_ENDIAN)
73 ir += __builtin_clzll(dv64) >> 3;
74# else
75# error "missing endian definition"
76# endif
77 break;
78 }
79 }
80#else
81 while ((ir < (const unsigned char *)
82 ALIGN((uintptr_t)ir, 4)) &&
83 (ir < limit) && (*ir == 0))
84 ir++;
85 for (; (ir + 4) <= limit; ir += 4) {
86 dv = *((u32 *)ir);
87 if (dv) {
88# if defined(__LITTLE_ENDIAN)
89 ir += __builtin_ctz(dv) >> 3;
90# elif defined(__BIG_ENDIAN)
91 ir += __builtin_clz(dv) >> 3;
92# else
93# error "missing endian definition"
94# endif
95 break;
96 }
97 }
98#endif
99 while (likely(ir < limit) && unlikely(*ir == 0))
100 ir++;
101 run_length = ir - ip;
102 if (run_length > MAX_ZERO_RUN_LENGTH)
103 run_length = MAX_ZERO_RUN_LENGTH;
104 } else {
105 t = ((dv * 0x1824429d) >> (32 - D_BITS)) & D_MASK;
106 m_pos = in + dict[t];
107 dict[t] = (lzo_dict_t) (ip - in);
108 if (unlikely(dv != get_unaligned_le32(m_pos)))
109 goto literal;
110 }
Richard Purdie64c70b12007-07-10 17:22:24 -0700111
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200112 ii -= ti;
113 ti = 0;
114 t = ip - ii;
115 if (t != 0) {
Richard Purdie64c70b12007-07-10 17:22:24 -0700116 if (t <= 3) {
Dave Rodgman5ee40142019-03-07 16:30:40 -0800117 op[*state_offset] |= t;
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200118 COPY4(op, ii);
119 op += t;
120 } else if (t <= 16) {
Richard Purdie64c70b12007-07-10 17:22:24 -0700121 *op++ = (t - 3);
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200122 COPY8(op, ii);
123 COPY8(op + 8, ii + 8);
124 op += t;
Richard Purdie64c70b12007-07-10 17:22:24 -0700125 } else {
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200126 if (t <= 18) {
127 *op++ = (t - 3);
128 } else {
129 size_t tt = t - 18;
Richard Purdie64c70b12007-07-10 17:22:24 -0700130 *op++ = 0;
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200131 while (unlikely(tt > 255)) {
132 tt -= 255;
Richard Purdie64c70b12007-07-10 17:22:24 -0700133 *op++ = 0;
134 }
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200135 *op++ = tt;
Richard Purdie64c70b12007-07-10 17:22:24 -0700136 }
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200137 do {
138 COPY8(op, ii);
139 COPY8(op + 8, ii + 8);
140 op += 16;
141 ii += 16;
142 t -= 16;
143 } while (t >= 16);
144 if (t > 0) do {
145 *op++ = *ii++;
146 } while (--t > 0);
Richard Purdie64c70b12007-07-10 17:22:24 -0700147 }
Richard Purdie64c70b12007-07-10 17:22:24 -0700148 }
149
Dave Rodgman5ee40142019-03-07 16:30:40 -0800150 if (unlikely(run_length)) {
151 ip += run_length;
152 run_length -= MIN_ZERO_RUN_LENGTH;
153 put_unaligned_le32((run_length << 21) | 0xfffc18
154 | (run_length & 0x7), op);
155 op += 4;
156 run_length = 0;
157 *state_offset = -3;
158 goto finished_writing_instruction;
159 }
160
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200161 m_len = 4;
162 {
163#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && defined(LZO_USE_CTZ64)
164 u64 v;
165 v = get_unaligned((const u64 *) (ip + m_len)) ^
166 get_unaligned((const u64 *) (m_pos + m_len));
167 if (unlikely(v == 0)) {
168 do {
169 m_len += 8;
170 v = get_unaligned((const u64 *) (ip + m_len)) ^
171 get_unaligned((const u64 *) (m_pos + m_len));
172 if (unlikely(ip + m_len >= ip_end))
173 goto m_len_done;
174 } while (v == 0);
175 }
176# if defined(__LITTLE_ENDIAN)
177 m_len += (unsigned) __builtin_ctzll(v) / 8;
178# elif defined(__BIG_ENDIAN)
179 m_len += (unsigned) __builtin_clzll(v) / 8;
180# else
181# error "missing endian definition"
182# endif
183#elif defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && defined(LZO_USE_CTZ32)
184 u32 v;
185 v = get_unaligned((const u32 *) (ip + m_len)) ^
186 get_unaligned((const u32 *) (m_pos + m_len));
187 if (unlikely(v == 0)) {
188 do {
189 m_len += 4;
190 v = get_unaligned((const u32 *) (ip + m_len)) ^
191 get_unaligned((const u32 *) (m_pos + m_len));
192 if (v != 0)
193 break;
194 m_len += 4;
195 v = get_unaligned((const u32 *) (ip + m_len)) ^
196 get_unaligned((const u32 *) (m_pos + m_len));
197 if (unlikely(ip + m_len >= ip_end))
198 goto m_len_done;
199 } while (v == 0);
200 }
201# if defined(__LITTLE_ENDIAN)
202 m_len += (unsigned) __builtin_ctz(v) / 8;
203# elif defined(__BIG_ENDIAN)
204 m_len += (unsigned) __builtin_clz(v) / 8;
205# else
206# error "missing endian definition"
207# endif
208#else
209 if (unlikely(ip[m_len] == m_pos[m_len])) {
210 do {
211 m_len += 1;
212 if (ip[m_len] != m_pos[m_len])
213 break;
214 m_len += 1;
215 if (ip[m_len] != m_pos[m_len])
216 break;
217 m_len += 1;
218 if (ip[m_len] != m_pos[m_len])
219 break;
220 m_len += 1;
221 if (ip[m_len] != m_pos[m_len])
222 break;
223 m_len += 1;
224 if (ip[m_len] != m_pos[m_len])
225 break;
226 m_len += 1;
227 if (ip[m_len] != m_pos[m_len])
228 break;
229 m_len += 1;
230 if (ip[m_len] != m_pos[m_len])
231 break;
232 m_len += 1;
233 if (unlikely(ip + m_len >= ip_end))
234 goto m_len_done;
235 } while (ip[m_len] == m_pos[m_len]);
236 }
237#endif
238 }
239m_len_done:
Richard Purdie64c70b12007-07-10 17:22:24 -0700240
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200241 m_off = ip - m_pos;
242 ip += m_len;
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200243 if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET) {
244 m_off -= 1;
245 *op++ = (((m_len - 1) << 5) | ((m_off & 7) << 2));
246 *op++ = (m_off >> 3);
247 } else if (m_off <= M3_MAX_OFFSET) {
248 m_off -= 1;
249 if (m_len <= M3_MAX_LEN)
250 *op++ = (M3_MARKER | (m_len - 2));
251 else {
252 m_len -= M3_MAX_LEN;
253 *op++ = M3_MARKER | 0;
254 while (unlikely(m_len > 255)) {
255 m_len -= 255;
256 *op++ = 0;
257 }
258 *op++ = (m_len);
259 }
260 *op++ = (m_off << 2);
261 *op++ = (m_off >> 6);
262 } else {
263 m_off -= 0x4000;
264 if (m_len <= M4_MAX_LEN)
265 *op++ = (M4_MARKER | ((m_off >> 11) & 8)
266 | (m_len - 2));
267 else {
268 m_len -= M4_MAX_LEN;
269 *op++ = (M4_MARKER | ((m_off >> 11) & 8));
270 while (unlikely(m_len > 255)) {
271 m_len -= 255;
272 *op++ = 0;
273 }
274 *op++ = (m_len);
275 }
276 *op++ = (m_off << 2);
277 *op++ = (m_off >> 6);
278 }
Dave Rodgman5ee40142019-03-07 16:30:40 -0800279 *state_offset = -2;
280finished_writing_instruction:
281 ii = ip;
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200282 goto next;
283 }
Richard Purdie64c70b12007-07-10 17:22:24 -0700284 *out_len = op - out;
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200285 return in_end - (ii - ti);
Richard Purdie64c70b12007-07-10 17:22:24 -0700286}
287
Dave Rodgman45ec9752019-03-07 16:30:44 -0800288int lzogeneric1x_1_compress(const unsigned char *in, size_t in_len,
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200289 unsigned char *out, size_t *out_len,
Dave Rodgman45ec9752019-03-07 16:30:44 -0800290 void *wrkmem, const unsigned char bitstream_version)
Richard Purdie64c70b12007-07-10 17:22:24 -0700291{
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200292 const unsigned char *ip = in;
Richard Purdie64c70b12007-07-10 17:22:24 -0700293 unsigned char *op = out;
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200294 size_t l = in_len;
295 size_t t = 0;
Dave Rodgman5ee40142019-03-07 16:30:40 -0800296 signed char state_offset = -2;
Dave Rodgman45ec9752019-03-07 16:30:44 -0800297 unsigned int m4_max_offset;
Dave Rodgman5ee40142019-03-07 16:30:40 -0800298
299 // LZO v0 will never write 17 as first byte,
300 // so this is used to version the bitstream
Dave Rodgman45ec9752019-03-07 16:30:44 -0800301 if (bitstream_version > 0) {
302 *op++ = 17;
303 *op++ = bitstream_version;
304 m4_max_offset = M4_MAX_OFFSET_V1;
305 } else {
306 m4_max_offset = M4_MAX_OFFSET_V0;
307 }
Richard Purdie64c70b12007-07-10 17:22:24 -0700308
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200309 while (l > 20) {
Dave Rodgman45ec9752019-03-07 16:30:44 -0800310 size_t ll = l <= (m4_max_offset + 1) ? l : (m4_max_offset + 1);
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200311 uintptr_t ll_end = (uintptr_t) ip + ll;
312 if ((ll_end + ((t + ll) >> 5)) <= ll_end)
313 break;
314 BUILD_BUG_ON(D_SIZE * sizeof(lzo_dict_t) > LZO1X_1_MEM_COMPRESS);
315 memset(wrkmem, 0, D_SIZE * sizeof(lzo_dict_t));
Dave Rodgman45ec9752019-03-07 16:30:44 -0800316 t = lzo1x_1_do_compress(ip, ll, op, out_len, t, wrkmem,
317 &state_offset, bitstream_version);
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200318 ip += ll;
Richard Purdie64c70b12007-07-10 17:22:24 -0700319 op += *out_len;
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200320 l -= ll;
Richard Purdie64c70b12007-07-10 17:22:24 -0700321 }
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200322 t += l;
Richard Purdie64c70b12007-07-10 17:22:24 -0700323
324 if (t > 0) {
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200325 const unsigned char *ii = in + in_len - t;
Richard Purdie64c70b12007-07-10 17:22:24 -0700326
327 if (op == out && t <= 238) {
328 *op++ = (17 + t);
329 } else if (t <= 3) {
Dave Rodgman5ee40142019-03-07 16:30:40 -0800330 op[state_offset] |= t;
Richard Purdie64c70b12007-07-10 17:22:24 -0700331 } else if (t <= 18) {
332 *op++ = (t - 3);
333 } else {
334 size_t tt = t - 18;
Richard Purdie64c70b12007-07-10 17:22:24 -0700335 *op++ = 0;
336 while (tt > 255) {
337 tt -= 255;
338 *op++ = 0;
339 }
Richard Purdie64c70b12007-07-10 17:22:24 -0700340 *op++ = tt;
341 }
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200342 if (t >= 16) do {
343 COPY8(op, ii);
344 COPY8(op + 8, ii + 8);
345 op += 16;
346 ii += 16;
347 t -= 16;
348 } while (t >= 16);
349 if (t > 0) do {
Richard Purdie64c70b12007-07-10 17:22:24 -0700350 *op++ = *ii++;
351 } while (--t > 0);
352 }
353
354 *op++ = M4_MARKER | 1;
355 *op++ = 0;
356 *op++ = 0;
357
358 *out_len = op - out;
359 return LZO_E_OK;
360}
Dave Rodgman45ec9752019-03-07 16:30:44 -0800361
362int lzo1x_1_compress(const unsigned char *in, size_t in_len,
363 unsigned char *out, size_t *out_len,
364 void *wrkmem)
365{
366 return lzogeneric1x_1_compress(in, in_len, out, out_len, wrkmem, 0);
367}
368
369int lzorle1x_1_compress(const unsigned char *in, size_t in_len,
370 unsigned char *out, size_t *out_len,
371 void *wrkmem)
372{
373 return lzogeneric1x_1_compress(in, in_len, out, out_len,
374 wrkmem, LZO_VERSION);
375}
376
Richard Purdie64c70b12007-07-10 17:22:24 -0700377EXPORT_SYMBOL_GPL(lzo1x_1_compress);
Dave Rodgman45ec9752019-03-07 16:30:44 -0800378EXPORT_SYMBOL_GPL(lzorle1x_1_compress);
Richard Purdie64c70b12007-07-10 17:22:24 -0700379
380MODULE_LICENSE("GPL");
381MODULE_DESCRIPTION("LZO1X-1 Compressor");