109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
264c70b1cSRichard Purdie /*
38b975bd3SMarkus F.X.J. Oberhumer * LZO1X Compressor from LZO
464c70b1cSRichard Purdie *
58b975bd3SMarkus F.X.J. Oberhumer * Copyright (C) 1996-2012 Markus F.X.J. Oberhumer <markus@oberhumer.com>
664c70b1cSRichard Purdie *
764c70b1cSRichard Purdie * The full LZO package can be found at:
864c70b1cSRichard Purdie * http://www.oberhumer.com/opensource/lzo/
964c70b1cSRichard Purdie *
108b975bd3SMarkus F.X.J. Oberhumer * Changed for Linux kernel use by:
1164c70b1cSRichard Purdie * Nitin Gupta <nitingupta910@gmail.com>
1264c70b1cSRichard Purdie * Richard Purdie <rpurdie@openedhand.com>
1364c70b1cSRichard Purdie */
1464c70b1cSRichard Purdie
1564c70b1cSRichard Purdie #include <linux/module.h>
1664c70b1cSRichard Purdie #include <linux/kernel.h>
1764c70b1cSRichard Purdie #include <asm/unaligned.h>
188b975bd3SMarkus F.X.J. Oberhumer #include <linux/lzo.h>
1964c70b1cSRichard Purdie #include "lzodefs.h"
2064c70b1cSRichard Purdie
2164c70b1cSRichard Purdie static noinline size_t
lzo1x_1_do_compress(const unsigned char * in,size_t in_len,unsigned char * out,size_t * out_len,size_t ti,void * wrkmem,signed char * state_offset,const unsigned char bitstream_version)228b975bd3SMarkus F.X.J. Oberhumer lzo1x_1_do_compress(const unsigned char *in, size_t in_len,
238b975bd3SMarkus F.X.J. Oberhumer unsigned char *out, size_t *out_len,
2445ec975eSDave Rodgman size_t ti, void *wrkmem, signed char *state_offset,
2545ec975eSDave Rodgman const unsigned char bitstream_version)
2664c70b1cSRichard Purdie {
278b975bd3SMarkus F.X.J. Oberhumer const unsigned char *ip;
288b975bd3SMarkus F.X.J. Oberhumer unsigned char *op;
2964c70b1cSRichard Purdie const unsigned char * const in_end = in + in_len;
308b975bd3SMarkus F.X.J. Oberhumer const unsigned char * const ip_end = in + in_len - 20;
318b975bd3SMarkus F.X.J. Oberhumer const unsigned char *ii;
328b975bd3SMarkus F.X.J. Oberhumer lzo_dict_t * const dict = (lzo_dict_t *) wrkmem;
3364c70b1cSRichard Purdie
348b975bd3SMarkus F.X.J. Oberhumer op = out;
358b975bd3SMarkus F.X.J. Oberhumer ip = in;
368b975bd3SMarkus F.X.J. Oberhumer ii = ip;
378b975bd3SMarkus F.X.J. Oberhumer ip += ti < 4 ? 4 - ti : 0;
3864c70b1cSRichard Purdie
3964c70b1cSRichard Purdie for (;;) {
405ee4014aSDave Rodgman const unsigned char *m_pos = NULL;
418b975bd3SMarkus F.X.J. Oberhumer size_t t, m_len, m_off;
428b975bd3SMarkus F.X.J. Oberhumer u32 dv;
435ee4014aSDave Rodgman u32 run_length = 0;
4464c70b1cSRichard Purdie literal:
458b975bd3SMarkus F.X.J. Oberhumer ip += 1 + ((ip - ii) >> 5);
468b975bd3SMarkus F.X.J. Oberhumer next:
4764c70b1cSRichard Purdie if (unlikely(ip >= ip_end))
4864c70b1cSRichard Purdie break;
498b975bd3SMarkus F.X.J. Oberhumer dv = get_unaligned_le32(ip);
505ee4014aSDave Rodgman
5145ec975eSDave Rodgman if (dv == 0 && bitstream_version) {
525ee4014aSDave Rodgman const unsigned char *ir = ip + 4;
53*a10c9edeSJiangshan Yi const unsigned char *limit = min(ip_end, ip + MAX_ZERO_RUN_LENGTH + 1);
545ee4014aSDave Rodgman #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && \
555ee4014aSDave Rodgman defined(LZO_FAST_64BIT_MEMORY_ACCESS)
565ee4014aSDave Rodgman u64 dv64;
575ee4014aSDave Rodgman
585ee4014aSDave Rodgman for (; (ir + 32) <= limit; ir += 32) {
595ee4014aSDave Rodgman dv64 = get_unaligned((u64 *)ir);
605ee4014aSDave Rodgman dv64 |= get_unaligned((u64 *)ir + 1);
615ee4014aSDave Rodgman dv64 |= get_unaligned((u64 *)ir + 2);
625ee4014aSDave Rodgman dv64 |= get_unaligned((u64 *)ir + 3);
635ee4014aSDave Rodgman if (dv64)
645ee4014aSDave Rodgman break;
655ee4014aSDave Rodgman }
665ee4014aSDave Rodgman for (; (ir + 8) <= limit; ir += 8) {
675ee4014aSDave Rodgman dv64 = get_unaligned((u64 *)ir);
685ee4014aSDave Rodgman if (dv64) {
695ee4014aSDave Rodgman # if defined(__LITTLE_ENDIAN)
705ee4014aSDave Rodgman ir += __builtin_ctzll(dv64) >> 3;
715ee4014aSDave Rodgman # elif defined(__BIG_ENDIAN)
725ee4014aSDave Rodgman ir += __builtin_clzll(dv64) >> 3;
735ee4014aSDave Rodgman # else
745ee4014aSDave Rodgman # error "missing endian definition"
755ee4014aSDave Rodgman # endif
765ee4014aSDave Rodgman break;
775ee4014aSDave Rodgman }
785ee4014aSDave Rodgman }
795ee4014aSDave Rodgman #else
805ee4014aSDave Rodgman while ((ir < (const unsigned char *)
815ee4014aSDave Rodgman ALIGN((uintptr_t)ir, 4)) &&
825ee4014aSDave Rodgman (ir < limit) && (*ir == 0))
835ee4014aSDave Rodgman ir++;
8409b35b41SDave Rodgman if (IS_ALIGNED((uintptr_t)ir, 4)) {
855ee4014aSDave Rodgman for (; (ir + 4) <= limit; ir += 4) {
865ee4014aSDave Rodgman dv = *((u32 *)ir);
875ee4014aSDave Rodgman if (dv) {
885ee4014aSDave Rodgman # if defined(__LITTLE_ENDIAN)
895ee4014aSDave Rodgman ir += __builtin_ctz(dv) >> 3;
905ee4014aSDave Rodgman # elif defined(__BIG_ENDIAN)
915ee4014aSDave Rodgman ir += __builtin_clz(dv) >> 3;
925ee4014aSDave Rodgman # else
935ee4014aSDave Rodgman # error "missing endian definition"
945ee4014aSDave Rodgman # endif
955ee4014aSDave Rodgman break;
965ee4014aSDave Rodgman }
975ee4014aSDave Rodgman }
9809b35b41SDave Rodgman }
995ee4014aSDave Rodgman #endif
1005ee4014aSDave Rodgman while (likely(ir < limit) && unlikely(*ir == 0))
1015ee4014aSDave Rodgman ir++;
1025ee4014aSDave Rodgman run_length = ir - ip;
1035ee4014aSDave Rodgman if (run_length > MAX_ZERO_RUN_LENGTH)
1045ee4014aSDave Rodgman run_length = MAX_ZERO_RUN_LENGTH;
1055ee4014aSDave Rodgman } else {
1068b975bd3SMarkus F.X.J. Oberhumer t = ((dv * 0x1824429d) >> (32 - D_BITS)) & D_MASK;
1078b975bd3SMarkus F.X.J. Oberhumer m_pos = in + dict[t];
1088b975bd3SMarkus F.X.J. Oberhumer dict[t] = (lzo_dict_t) (ip - in);
1098b975bd3SMarkus F.X.J. Oberhumer if (unlikely(dv != get_unaligned_le32(m_pos)))
1108b975bd3SMarkus F.X.J. Oberhumer goto literal;
1115ee4014aSDave Rodgman }
11264c70b1cSRichard Purdie
1138b975bd3SMarkus F.X.J. Oberhumer ii -= ti;
1148b975bd3SMarkus F.X.J. Oberhumer ti = 0;
1158b975bd3SMarkus F.X.J. Oberhumer t = ip - ii;
1168b975bd3SMarkus F.X.J. Oberhumer if (t != 0) {
11764c70b1cSRichard Purdie if (t <= 3) {
1185ee4014aSDave Rodgman op[*state_offset] |= t;
1198b975bd3SMarkus F.X.J. Oberhumer COPY4(op, ii);
1208b975bd3SMarkus F.X.J. Oberhumer op += t;
1218b975bd3SMarkus F.X.J. Oberhumer } else if (t <= 16) {
1228b975bd3SMarkus F.X.J. Oberhumer *op++ = (t - 3);
1238b975bd3SMarkus F.X.J. Oberhumer COPY8(op, ii);
1248b975bd3SMarkus F.X.J. Oberhumer COPY8(op + 8, ii + 8);
1258b975bd3SMarkus F.X.J. Oberhumer op += t;
1268b975bd3SMarkus F.X.J. Oberhumer } else {
1278b975bd3SMarkus F.X.J. Oberhumer if (t <= 18) {
12864c70b1cSRichard Purdie *op++ = (t - 3);
12964c70b1cSRichard Purdie } else {
13064c70b1cSRichard Purdie size_t tt = t - 18;
13164c70b1cSRichard Purdie *op++ = 0;
1328b975bd3SMarkus F.X.J. Oberhumer while (unlikely(tt > 255)) {
13364c70b1cSRichard Purdie tt -= 255;
13464c70b1cSRichard Purdie *op++ = 0;
13564c70b1cSRichard Purdie }
13664c70b1cSRichard Purdie *op++ = tt;
13764c70b1cSRichard Purdie }
13864c70b1cSRichard Purdie do {
1398b975bd3SMarkus F.X.J. Oberhumer COPY8(op, ii);
1408b975bd3SMarkus F.X.J. Oberhumer COPY8(op + 8, ii + 8);
1418b975bd3SMarkus F.X.J. Oberhumer op += 16;
1428b975bd3SMarkus F.X.J. Oberhumer ii += 16;
1438b975bd3SMarkus F.X.J. Oberhumer t -= 16;
1448b975bd3SMarkus F.X.J. Oberhumer } while (t >= 16);
1458b975bd3SMarkus F.X.J. Oberhumer if (t > 0) do {
14664c70b1cSRichard Purdie *op++ = *ii++;
14764c70b1cSRichard Purdie } while (--t > 0);
14864c70b1cSRichard Purdie }
1498b975bd3SMarkus F.X.J. Oberhumer }
15064c70b1cSRichard Purdie
1515ee4014aSDave Rodgman if (unlikely(run_length)) {
1525ee4014aSDave Rodgman ip += run_length;
1535ee4014aSDave Rodgman run_length -= MIN_ZERO_RUN_LENGTH;
1545ee4014aSDave Rodgman put_unaligned_le32((run_length << 21) | 0xfffc18
1555ee4014aSDave Rodgman | (run_length & 0x7), op);
1565ee4014aSDave Rodgman op += 4;
1575ee4014aSDave Rodgman run_length = 0;
1585ee4014aSDave Rodgman *state_offset = -3;
1595ee4014aSDave Rodgman goto finished_writing_instruction;
1605ee4014aSDave Rodgman }
1615ee4014aSDave Rodgman
1628b975bd3SMarkus F.X.J. Oberhumer m_len = 4;
1638b975bd3SMarkus F.X.J. Oberhumer {
1648b975bd3SMarkus F.X.J. Oberhumer #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && defined(LZO_USE_CTZ64)
1658b975bd3SMarkus F.X.J. Oberhumer u64 v;
1668b975bd3SMarkus F.X.J. Oberhumer v = get_unaligned((const u64 *) (ip + m_len)) ^
1678b975bd3SMarkus F.X.J. Oberhumer get_unaligned((const u64 *) (m_pos + m_len));
1688b975bd3SMarkus F.X.J. Oberhumer if (unlikely(v == 0)) {
1698b975bd3SMarkus F.X.J. Oberhumer do {
1708b975bd3SMarkus F.X.J. Oberhumer m_len += 8;
1718b975bd3SMarkus F.X.J. Oberhumer v = get_unaligned((const u64 *) (ip + m_len)) ^
1728b975bd3SMarkus F.X.J. Oberhumer get_unaligned((const u64 *) (m_pos + m_len));
1738b975bd3SMarkus F.X.J. Oberhumer if (unlikely(ip + m_len >= ip_end))
1748b975bd3SMarkus F.X.J. Oberhumer goto m_len_done;
1758b975bd3SMarkus F.X.J. Oberhumer } while (v == 0);
1768b975bd3SMarkus F.X.J. Oberhumer }
1778b975bd3SMarkus F.X.J. Oberhumer # if defined(__LITTLE_ENDIAN)
1788b975bd3SMarkus F.X.J. Oberhumer m_len += (unsigned) __builtin_ctzll(v) / 8;
1798b975bd3SMarkus F.X.J. Oberhumer # elif defined(__BIG_ENDIAN)
1808b975bd3SMarkus F.X.J. Oberhumer m_len += (unsigned) __builtin_clzll(v) / 8;
1818b975bd3SMarkus F.X.J. Oberhumer # else
1828b975bd3SMarkus F.X.J. Oberhumer # error "missing endian definition"
1838b975bd3SMarkus F.X.J. Oberhumer # endif
1848b975bd3SMarkus F.X.J. Oberhumer #elif defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && defined(LZO_USE_CTZ32)
1858b975bd3SMarkus F.X.J. Oberhumer u32 v;
1868b975bd3SMarkus F.X.J. Oberhumer v = get_unaligned((const u32 *) (ip + m_len)) ^
1878b975bd3SMarkus F.X.J. Oberhumer get_unaligned((const u32 *) (m_pos + m_len));
1888b975bd3SMarkus F.X.J. Oberhumer if (unlikely(v == 0)) {
1898b975bd3SMarkus F.X.J. Oberhumer do {
1908b975bd3SMarkus F.X.J. Oberhumer m_len += 4;
1918b975bd3SMarkus F.X.J. Oberhumer v = get_unaligned((const u32 *) (ip + m_len)) ^
1928b975bd3SMarkus F.X.J. Oberhumer get_unaligned((const u32 *) (m_pos + m_len));
1938b975bd3SMarkus F.X.J. Oberhumer if (v != 0)
1948b975bd3SMarkus F.X.J. Oberhumer break;
1958b975bd3SMarkus F.X.J. Oberhumer m_len += 4;
1968b975bd3SMarkus F.X.J. Oberhumer v = get_unaligned((const u32 *) (ip + m_len)) ^
1978b975bd3SMarkus F.X.J. Oberhumer get_unaligned((const u32 *) (m_pos + m_len));
1988b975bd3SMarkus F.X.J. Oberhumer if (unlikely(ip + m_len >= ip_end))
1998b975bd3SMarkus F.X.J. Oberhumer goto m_len_done;
2008b975bd3SMarkus F.X.J. Oberhumer } while (v == 0);
2018b975bd3SMarkus F.X.J. Oberhumer }
2028b975bd3SMarkus F.X.J. Oberhumer # if defined(__LITTLE_ENDIAN)
2038b975bd3SMarkus F.X.J. Oberhumer m_len += (unsigned) __builtin_ctz(v) / 8;
2048b975bd3SMarkus F.X.J. Oberhumer # elif defined(__BIG_ENDIAN)
2058b975bd3SMarkus F.X.J. Oberhumer m_len += (unsigned) __builtin_clz(v) / 8;
2068b975bd3SMarkus F.X.J. Oberhumer # else
2078b975bd3SMarkus F.X.J. Oberhumer # error "missing endian definition"
2088b975bd3SMarkus F.X.J. Oberhumer # endif
2098b975bd3SMarkus F.X.J. Oberhumer #else
2108b975bd3SMarkus F.X.J. Oberhumer if (unlikely(ip[m_len] == m_pos[m_len])) {
2118b975bd3SMarkus F.X.J. Oberhumer do {
2128b975bd3SMarkus F.X.J. Oberhumer m_len += 1;
2138b975bd3SMarkus F.X.J. Oberhumer if (ip[m_len] != m_pos[m_len])
2148b975bd3SMarkus F.X.J. Oberhumer break;
2158b975bd3SMarkus F.X.J. Oberhumer m_len += 1;
2168b975bd3SMarkus F.X.J. Oberhumer if (ip[m_len] != m_pos[m_len])
2178b975bd3SMarkus F.X.J. Oberhumer break;
2188b975bd3SMarkus F.X.J. Oberhumer m_len += 1;
2198b975bd3SMarkus F.X.J. Oberhumer if (ip[m_len] != m_pos[m_len])
2208b975bd3SMarkus F.X.J. Oberhumer break;
2218b975bd3SMarkus F.X.J. Oberhumer m_len += 1;
2228b975bd3SMarkus F.X.J. Oberhumer if (ip[m_len] != m_pos[m_len])
2238b975bd3SMarkus F.X.J. Oberhumer break;
2248b975bd3SMarkus F.X.J. Oberhumer m_len += 1;
2258b975bd3SMarkus F.X.J. Oberhumer if (ip[m_len] != m_pos[m_len])
2268b975bd3SMarkus F.X.J. Oberhumer break;
2278b975bd3SMarkus F.X.J. Oberhumer m_len += 1;
2288b975bd3SMarkus F.X.J. Oberhumer if (ip[m_len] != m_pos[m_len])
2298b975bd3SMarkus F.X.J. Oberhumer break;
2308b975bd3SMarkus F.X.J. Oberhumer m_len += 1;
2318b975bd3SMarkus F.X.J. Oberhumer if (ip[m_len] != m_pos[m_len])
2328b975bd3SMarkus F.X.J. Oberhumer break;
2338b975bd3SMarkus F.X.J. Oberhumer m_len += 1;
2348b975bd3SMarkus F.X.J. Oberhumer if (unlikely(ip + m_len >= ip_end))
2358b975bd3SMarkus F.X.J. Oberhumer goto m_len_done;
2368b975bd3SMarkus F.X.J. Oberhumer } while (ip[m_len] == m_pos[m_len]);
2378b975bd3SMarkus F.X.J. Oberhumer }
2388b975bd3SMarkus F.X.J. Oberhumer #endif
2398b975bd3SMarkus F.X.J. Oberhumer }
2408b975bd3SMarkus F.X.J. Oberhumer m_len_done:
24164c70b1cSRichard Purdie
2428b975bd3SMarkus F.X.J. Oberhumer m_off = ip - m_pos;
2438b975bd3SMarkus F.X.J. Oberhumer ip += m_len;
2448b975bd3SMarkus F.X.J. Oberhumer if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET) {
24564c70b1cSRichard Purdie m_off -= 1;
2468b975bd3SMarkus F.X.J. Oberhumer *op++ = (((m_len - 1) << 5) | ((m_off & 7) << 2));
24764c70b1cSRichard Purdie *op++ = (m_off >> 3);
24864c70b1cSRichard Purdie } else if (m_off <= M3_MAX_OFFSET) {
24964c70b1cSRichard Purdie m_off -= 1;
2508b975bd3SMarkus F.X.J. Oberhumer if (m_len <= M3_MAX_LEN)
25164c70b1cSRichard Purdie *op++ = (M3_MARKER | (m_len - 2));
2528b975bd3SMarkus F.X.J. Oberhumer else {
2538b975bd3SMarkus F.X.J. Oberhumer m_len -= M3_MAX_LEN;
25464c70b1cSRichard Purdie *op++ = M3_MARKER | 0;
2558b975bd3SMarkus F.X.J. Oberhumer while (unlikely(m_len > 255)) {
25664c70b1cSRichard Purdie m_len -= 255;
25764c70b1cSRichard Purdie *op++ = 0;
25864c70b1cSRichard Purdie }
25964c70b1cSRichard Purdie *op++ = (m_len);
26064c70b1cSRichard Purdie }
2618b975bd3SMarkus F.X.J. Oberhumer *op++ = (m_off << 2);
2628b975bd3SMarkus F.X.J. Oberhumer *op++ = (m_off >> 6);
2638b975bd3SMarkus F.X.J. Oberhumer } else {
2648b975bd3SMarkus F.X.J. Oberhumer m_off -= 0x4000;
2658b975bd3SMarkus F.X.J. Oberhumer if (m_len <= M4_MAX_LEN)
2668b975bd3SMarkus F.X.J. Oberhumer *op++ = (M4_MARKER | ((m_off >> 11) & 8)
2678b975bd3SMarkus F.X.J. Oberhumer | (m_len - 2));
2688b975bd3SMarkus F.X.J. Oberhumer else {
269b5265c81SDave Rodgman if (unlikely(((m_off & 0x403f) == 0x403f)
270b5265c81SDave Rodgman && (m_len >= 261)
271b5265c81SDave Rodgman && (m_len <= 264))
272b5265c81SDave Rodgman && likely(bitstream_version)) {
273b5265c81SDave Rodgman // Under lzo-rle, block copies
274b5265c81SDave Rodgman // for 261 <= length <= 264 and
275b5265c81SDave Rodgman // (distance & 0x80f3) == 0x80f3
276b5265c81SDave Rodgman // can result in ambiguous
277b5265c81SDave Rodgman // output. Adjust length
278b5265c81SDave Rodgman // to 260 to prevent ambiguity.
279b5265c81SDave Rodgman ip -= m_len - 260;
280b5265c81SDave Rodgman m_len = 260;
281b5265c81SDave Rodgman }
2828b975bd3SMarkus F.X.J. Oberhumer m_len -= M4_MAX_LEN;
2838b975bd3SMarkus F.X.J. Oberhumer *op++ = (M4_MARKER | ((m_off >> 11) & 8));
2848b975bd3SMarkus F.X.J. Oberhumer while (unlikely(m_len > 255)) {
2858b975bd3SMarkus F.X.J. Oberhumer m_len -= 255;
2868b975bd3SMarkus F.X.J. Oberhumer *op++ = 0;
28764c70b1cSRichard Purdie }
2888b975bd3SMarkus F.X.J. Oberhumer *op++ = (m_len);
2898b975bd3SMarkus F.X.J. Oberhumer }
2908b975bd3SMarkus F.X.J. Oberhumer *op++ = (m_off << 2);
29164c70b1cSRichard Purdie *op++ = (m_off >> 6);
29264c70b1cSRichard Purdie }
2935ee4014aSDave Rodgman *state_offset = -2;
2945ee4014aSDave Rodgman finished_writing_instruction:
2955ee4014aSDave Rodgman ii = ip;
2968b975bd3SMarkus F.X.J. Oberhumer goto next;
29764c70b1cSRichard Purdie }
29864c70b1cSRichard Purdie *out_len = op - out;
2998b975bd3SMarkus F.X.J. Oberhumer return in_end - (ii - ti);
30064c70b1cSRichard Purdie }
30164c70b1cSRichard Purdie
lzogeneric1x_1_compress(const unsigned char * in,size_t in_len,unsigned char * out,size_t * out_len,void * wrkmem,const unsigned char bitstream_version)30233fb626bSJason Yan static int lzogeneric1x_1_compress(const unsigned char *in, size_t in_len,
3038b975bd3SMarkus F.X.J. Oberhumer unsigned char *out, size_t *out_len,
30445ec975eSDave Rodgman void *wrkmem, const unsigned char bitstream_version)
30564c70b1cSRichard Purdie {
3068b975bd3SMarkus F.X.J. Oberhumer const unsigned char *ip = in;
30764c70b1cSRichard Purdie unsigned char *op = out;
308b11ed18eSDave Rodgman unsigned char *data_start;
3098b975bd3SMarkus F.X.J. Oberhumer size_t l = in_len;
3108b975bd3SMarkus F.X.J. Oberhumer size_t t = 0;
3115ee4014aSDave Rodgman signed char state_offset = -2;
31245ec975eSDave Rodgman unsigned int m4_max_offset;
3135ee4014aSDave Rodgman
314b11ed18eSDave Rodgman // LZO v0 will never write 17 as first byte (except for zero-length
315b11ed18eSDave Rodgman // input), so this is used to version the bitstream
31645ec975eSDave Rodgman if (bitstream_version > 0) {
3175ee4014aSDave Rodgman *op++ = 17;
31845ec975eSDave Rodgman *op++ = bitstream_version;
31945ec975eSDave Rodgman m4_max_offset = M4_MAX_OFFSET_V1;
32045ec975eSDave Rodgman } else {
32145ec975eSDave Rodgman m4_max_offset = M4_MAX_OFFSET_V0;
32245ec975eSDave Rodgman }
32364c70b1cSRichard Purdie
324b11ed18eSDave Rodgman data_start = op;
325b11ed18eSDave Rodgman
3268b975bd3SMarkus F.X.J. Oberhumer while (l > 20) {
327*a10c9edeSJiangshan Yi size_t ll = min_t(size_t, l, m4_max_offset + 1);
3288b975bd3SMarkus F.X.J. Oberhumer uintptr_t ll_end = (uintptr_t) ip + ll;
3298b975bd3SMarkus F.X.J. Oberhumer if ((ll_end + ((t + ll) >> 5)) <= ll_end)
3308b975bd3SMarkus F.X.J. Oberhumer break;
3318b975bd3SMarkus F.X.J. Oberhumer BUILD_BUG_ON(D_SIZE * sizeof(lzo_dict_t) > LZO1X_1_MEM_COMPRESS);
3328b975bd3SMarkus F.X.J. Oberhumer memset(wrkmem, 0, D_SIZE * sizeof(lzo_dict_t));
33345ec975eSDave Rodgman t = lzo1x_1_do_compress(ip, ll, op, out_len, t, wrkmem,
33445ec975eSDave Rodgman &state_offset, bitstream_version);
3358b975bd3SMarkus F.X.J. Oberhumer ip += ll;
33664c70b1cSRichard Purdie op += *out_len;
3378b975bd3SMarkus F.X.J. Oberhumer l -= ll;
33864c70b1cSRichard Purdie }
3398b975bd3SMarkus F.X.J. Oberhumer t += l;
34064c70b1cSRichard Purdie
34164c70b1cSRichard Purdie if (t > 0) {
3428b975bd3SMarkus F.X.J. Oberhumer const unsigned char *ii = in + in_len - t;
34364c70b1cSRichard Purdie
344b11ed18eSDave Rodgman if (op == data_start && t <= 238) {
34564c70b1cSRichard Purdie *op++ = (17 + t);
34664c70b1cSRichard Purdie } else if (t <= 3) {
3475ee4014aSDave Rodgman op[state_offset] |= t;
34864c70b1cSRichard Purdie } else if (t <= 18) {
34964c70b1cSRichard Purdie *op++ = (t - 3);
35064c70b1cSRichard Purdie } else {
35164c70b1cSRichard Purdie size_t tt = t - 18;
35264c70b1cSRichard Purdie *op++ = 0;
35364c70b1cSRichard Purdie while (tt > 255) {
35464c70b1cSRichard Purdie tt -= 255;
35564c70b1cSRichard Purdie *op++ = 0;
35664c70b1cSRichard Purdie }
35764c70b1cSRichard Purdie *op++ = tt;
35864c70b1cSRichard Purdie }
3598b975bd3SMarkus F.X.J. Oberhumer if (t >= 16) do {
3608b975bd3SMarkus F.X.J. Oberhumer COPY8(op, ii);
3618b975bd3SMarkus F.X.J. Oberhumer COPY8(op + 8, ii + 8);
3628b975bd3SMarkus F.X.J. Oberhumer op += 16;
3638b975bd3SMarkus F.X.J. Oberhumer ii += 16;
3648b975bd3SMarkus F.X.J. Oberhumer t -= 16;
3658b975bd3SMarkus F.X.J. Oberhumer } while (t >= 16);
3668b975bd3SMarkus F.X.J. Oberhumer if (t > 0) do {
36764c70b1cSRichard Purdie *op++ = *ii++;
36864c70b1cSRichard Purdie } while (--t > 0);
36964c70b1cSRichard Purdie }
37064c70b1cSRichard Purdie
37164c70b1cSRichard Purdie *op++ = M4_MARKER | 1;
37264c70b1cSRichard Purdie *op++ = 0;
37364c70b1cSRichard Purdie *op++ = 0;
37464c70b1cSRichard Purdie
37564c70b1cSRichard Purdie *out_len = op - out;
37664c70b1cSRichard Purdie return LZO_E_OK;
37764c70b1cSRichard Purdie }
37845ec975eSDave Rodgman
lzo1x_1_compress(const unsigned char * in,size_t in_len,unsigned char * out,size_t * out_len,void * wrkmem)37945ec975eSDave Rodgman int lzo1x_1_compress(const unsigned char *in, size_t in_len,
38045ec975eSDave Rodgman unsigned char *out, size_t *out_len,
38145ec975eSDave Rodgman void *wrkmem)
38245ec975eSDave Rodgman {
38345ec975eSDave Rodgman return lzogeneric1x_1_compress(in, in_len, out, out_len, wrkmem, 0);
38445ec975eSDave Rodgman }
38545ec975eSDave Rodgman
lzorle1x_1_compress(const unsigned char * in,size_t in_len,unsigned char * out,size_t * out_len,void * wrkmem)38645ec975eSDave Rodgman int lzorle1x_1_compress(const unsigned char *in, size_t in_len,
38745ec975eSDave Rodgman unsigned char *out, size_t *out_len,
38845ec975eSDave Rodgman void *wrkmem)
38945ec975eSDave Rodgman {
39045ec975eSDave Rodgman return lzogeneric1x_1_compress(in, in_len, out, out_len,
39145ec975eSDave Rodgman wrkmem, LZO_VERSION);
39245ec975eSDave Rodgman }
39345ec975eSDave Rodgman
39464c70b1cSRichard Purdie EXPORT_SYMBOL_GPL(lzo1x_1_compress);
39545ec975eSDave Rodgman EXPORT_SYMBOL_GPL(lzorle1x_1_compress);
39664c70b1cSRichard Purdie
39764c70b1cSRichard Purdie MODULE_LICENSE("GPL");
39864c70b1cSRichard Purdie MODULE_DESCRIPTION("LZO1X-1 Compressor");
399