1 // SPDX-License-Identifier: GPL-2.0-only
3 * LZO1X Compressor from LZO
5 * Copyright (C) 1996-2012 Markus F.X.J. Oberhumer <markus@oberhumer.com>
7 * The full LZO package can be found at:
8 * http://www.oberhumer.com/opensource/lzo/
10 * Changed for Linux kernel use by:
11 * Nitin Gupta <nitingupta910@gmail.com>
12 * Richard Purdie <rpurdie@openedhand.com>
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <asm/unaligned.h>
18 #include <linux/lzo.h>
21 static noinline size_t
22 lzo1x_1_do_compress(const unsigned char *in, size_t in_len,
23 unsigned char *out, size_t *out_len,
24 size_t ti, void *wrkmem, signed char *state_offset,
25 const unsigned char bitstream_version)
27 const unsigned char *ip;
29 const unsigned char * const in_end = in + in_len;
30 const unsigned char * const ip_end = in + in_len - 20;
31 const unsigned char *ii;
32 lzo_dict_t * const dict = (lzo_dict_t *) wrkmem;
37 ip += ti < 4 ? 4 - ti : 0;
40 const unsigned char *m_pos = NULL;
41 size_t t, m_len, m_off;
45 ip += 1 + ((ip - ii) >> 5);
47 if (unlikely(ip >= ip_end))
49 dv = get_unaligned_le32(ip);
51 if (dv == 0 && bitstream_version) {
52 const unsigned char *ir = ip + 4;
53 const unsigned char *limit = ip_end
54 < (ip + MAX_ZERO_RUN_LENGTH + 1)
55 ? ip_end : ip + MAX_ZERO_RUN_LENGTH + 1;
56 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && \
57 defined(LZO_FAST_64BIT_MEMORY_ACCESS)
60 for (; (ir + 32) <= limit; ir += 32) {
61 dv64 = get_unaligned((u64 *)ir);
62 dv64 |= get_unaligned((u64 *)ir + 1);
63 dv64 |= get_unaligned((u64 *)ir + 2);
64 dv64 |= get_unaligned((u64 *)ir + 3);
68 for (; (ir + 8) <= limit; ir += 8) {
69 dv64 = get_unaligned((u64 *)ir);
71 # if defined(__LITTLE_ENDIAN)
72 ir += __builtin_ctzll(dv64) >> 3;
73 # elif defined(__BIG_ENDIAN)
74 ir += __builtin_clzll(dv64) >> 3;
76 # error "missing endian definition"
82 while ((ir < (const unsigned char *)
83 ALIGN((uintptr_t)ir, 4)) &&
84 (ir < limit) && (*ir == 0))
86 if (IS_ALIGNED((uintptr_t)ir, 4)) {
87 for (; (ir + 4) <= limit; ir += 4) {
90 # if defined(__LITTLE_ENDIAN)
91 ir += __builtin_ctz(dv) >> 3;
92 # elif defined(__BIG_ENDIAN)
93 ir += __builtin_clz(dv) >> 3;
95 # error "missing endian definition"
102 while (likely(ir < limit) && unlikely(*ir == 0))
104 run_length = ir - ip;
105 if (run_length > MAX_ZERO_RUN_LENGTH)
106 run_length = MAX_ZERO_RUN_LENGTH;
108 t = ((dv * 0x1824429d) >> (32 - D_BITS)) & D_MASK;
109 m_pos = in + dict[t];
110 dict[t] = (lzo_dict_t) (ip - in);
111 if (unlikely(dv != get_unaligned_le32(m_pos)))
120 op[*state_offset] |= t;
123 } else if (t <= 16) {
126 COPY8(op + 8, ii + 8);
134 while (unlikely(tt > 255)) {
142 COPY8(op + 8, ii + 8);
153 if (unlikely(run_length)) {
155 run_length -= MIN_ZERO_RUN_LENGTH;
156 put_unaligned_le32((run_length << 21) | 0xfffc18
157 | (run_length & 0x7), op);
161 goto finished_writing_instruction;
166 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && defined(LZO_USE_CTZ64)
168 v = get_unaligned((const u64 *) (ip + m_len)) ^
169 get_unaligned((const u64 *) (m_pos + m_len));
170 if (unlikely(v == 0)) {
173 v = get_unaligned((const u64 *) (ip + m_len)) ^
174 get_unaligned((const u64 *) (m_pos + m_len));
175 if (unlikely(ip + m_len >= ip_end))
179 # if defined(__LITTLE_ENDIAN)
180 m_len += (unsigned) __builtin_ctzll(v) / 8;
181 # elif defined(__BIG_ENDIAN)
182 m_len += (unsigned) __builtin_clzll(v) / 8;
184 # error "missing endian definition"
186 #elif defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && defined(LZO_USE_CTZ32)
188 v = get_unaligned((const u32 *) (ip + m_len)) ^
189 get_unaligned((const u32 *) (m_pos + m_len));
190 if (unlikely(v == 0)) {
193 v = get_unaligned((const u32 *) (ip + m_len)) ^
194 get_unaligned((const u32 *) (m_pos + m_len));
198 v = get_unaligned((const u32 *) (ip + m_len)) ^
199 get_unaligned((const u32 *) (m_pos + m_len));
200 if (unlikely(ip + m_len >= ip_end))
204 # if defined(__LITTLE_ENDIAN)
205 m_len += (unsigned) __builtin_ctz(v) / 8;
206 # elif defined(__BIG_ENDIAN)
207 m_len += (unsigned) __builtin_clz(v) / 8;
209 # error "missing endian definition"
212 if (unlikely(ip[m_len] == m_pos[m_len])) {
215 if (ip[m_len] != m_pos[m_len])
218 if (ip[m_len] != m_pos[m_len])
221 if (ip[m_len] != m_pos[m_len])
224 if (ip[m_len] != m_pos[m_len])
227 if (ip[m_len] != m_pos[m_len])
230 if (ip[m_len] != m_pos[m_len])
233 if (ip[m_len] != m_pos[m_len])
236 if (unlikely(ip + m_len >= ip_end))
238 } while (ip[m_len] == m_pos[m_len]);
246 if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET) {
248 *op++ = (((m_len - 1) << 5) | ((m_off & 7) << 2));
249 *op++ = (m_off >> 3);
250 } else if (m_off <= M3_MAX_OFFSET) {
252 if (m_len <= M3_MAX_LEN)
253 *op++ = (M3_MARKER | (m_len - 2));
256 *op++ = M3_MARKER | 0;
257 while (unlikely(m_len > 255)) {
263 *op++ = (m_off << 2);
264 *op++ = (m_off >> 6);
267 if (m_len <= M4_MAX_LEN)
268 *op++ = (M4_MARKER | ((m_off >> 11) & 8)
271 if (unlikely(((m_off & 0x403f) == 0x403f)
274 && likely(bitstream_version)) {
275 // Under lzo-rle, block copies
276 // for 261 <= length <= 264 and
277 // (distance & 0x80f3) == 0x80f3
278 // can result in ambiguous
279 // output. Adjust length
280 // to 260 to prevent ambiguity.
285 *op++ = (M4_MARKER | ((m_off >> 11) & 8));
286 while (unlikely(m_len > 255)) {
292 *op++ = (m_off << 2);
293 *op++ = (m_off >> 6);
296 finished_writing_instruction:
301 return in_end - (ii - ti);
304 static int lzogeneric1x_1_compress(const unsigned char *in, size_t in_len,
305 unsigned char *out, size_t *out_len,
306 void *wrkmem, const unsigned char bitstream_version)
308 const unsigned char *ip = in;
309 unsigned char *op = out;
310 unsigned char *data_start;
313 signed char state_offset = -2;
314 unsigned int m4_max_offset;
316 // LZO v0 will never write 17 as first byte (except for zero-length
317 // input), so this is used to version the bitstream
318 if (bitstream_version > 0) {
320 *op++ = bitstream_version;
321 m4_max_offset = M4_MAX_OFFSET_V1;
323 m4_max_offset = M4_MAX_OFFSET_V0;
329 size_t ll = l <= (m4_max_offset + 1) ? l : (m4_max_offset + 1);
330 uintptr_t ll_end = (uintptr_t) ip + ll;
331 if ((ll_end + ((t + ll) >> 5)) <= ll_end)
333 BUILD_BUG_ON(D_SIZE * sizeof(lzo_dict_t) > LZO1X_1_MEM_COMPRESS);
334 memset(wrkmem, 0, D_SIZE * sizeof(lzo_dict_t));
335 t = lzo1x_1_do_compress(ip, ll, op, out_len, t, wrkmem,
336 &state_offset, bitstream_version);
344 const unsigned char *ii = in + in_len - t;
346 if (op == data_start && t <= 238) {
349 op[state_offset] |= t;
350 } else if (t <= 18) {
363 COPY8(op + 8, ii + 8);
373 *op++ = M4_MARKER | 1;
381 int lzo1x_1_compress(const unsigned char *in, size_t in_len,
382 unsigned char *out, size_t *out_len,
385 return lzogeneric1x_1_compress(in, in_len, out, out_len, wrkmem, 0);
388 int lzorle1x_1_compress(const unsigned char *in, size_t in_len,
389 unsigned char *out, size_t *out_len,
392 return lzogeneric1x_1_compress(in, in_len, out, out_len,
393 wrkmem, LZO_VERSION);
396 EXPORT_SYMBOL_GPL(lzo1x_1_compress);
397 EXPORT_SYMBOL_GPL(lzorle1x_1_compress);
399 MODULE_LICENSE("GPL");
400 MODULE_DESCRIPTION("LZO1X-1 Compressor");