2 * LZO1X Decompressor from MiniLZO
4 * Copyright (C) 1996-2005 Markus F.X.J. Oberhumer <markus@oberhumer.com>
6 * The full LZO package can be found at:
7 * http://www.oberhumer.com/opensource/lzo/
9 * Changed for kernel use by:
10 * Nitin Gupta <nitingupta910@gmail.com>
11 * Richard Purdie <rpurdie@openedhand.com>
15 #include <linux/lzo.h>
16 #include <asm/byteorder.h>
17 #include <asm/unaligned.h>
20 #define HAVE_IP(x, ip_end, ip) ((size_t)(ip_end - ip) < (x))
21 #define HAVE_OP(x, op_end, op) ((size_t)(op_end - op) < (x))
22 #define HAVE_LB(m_pos, out, op) (m_pos < out || m_pos >= op)
24 #define COPY4(dst, src) \
25 put_unaligned(get_unaligned((const u32 *)(src)), (u32 *)(dst))
27 int lzo1x_decompress_safe(const unsigned char *in, size_t in_len,
28 unsigned char *out, size_t *out_len)
30 const unsigned char * const ip_end = in + in_len;
31 unsigned char * const op_end = out + *out_len;
32 const unsigned char *ip = in, *m_pos;
33 unsigned char *op = out;
42 if (HAVE_OP(t, op_end, op))
44 if (HAVE_IP(t + 1, ip_end, ip))
49 goto first_literal_run;
52 while ((ip < ip_end)) {
57 if (HAVE_IP(1, ip_end, ip))
62 if (HAVE_IP(1, ip_end, ip))
67 if (HAVE_OP(t + 3, op_end, op))
69 if (HAVE_IP(t + 4, ip_end, ip))
99 m_pos = op - (1 + M2_MAX_OFFSET);
103 if (HAVE_LB(m_pos, out, op))
104 goto lookbehind_overrun;
106 if (HAVE_OP(3, op_end, op))
118 m_pos -= (t >> 2) & 7;
121 if (HAVE_LB(m_pos, out, op))
122 goto lookbehind_overrun;
123 if (HAVE_OP(t + 3 - 1, op_end, op))
126 } else if (t >= 32) {
129 if (HAVE_IP(1, ip_end, ip))
134 if (HAVE_IP(1, ip_end, ip))
140 m_pos -= get_unaligned_le16(ip) >> 2;
142 } else if (t >= 16) {
144 m_pos -= (t & 8) << 11;
148 if (HAVE_IP(1, ip_end, ip))
153 if (HAVE_IP(1, ip_end, ip))
158 m_pos -= get_unaligned_le16(ip) >> 2;
168 if (HAVE_LB(m_pos, out, op))
169 goto lookbehind_overrun;
170 if (HAVE_OP(2, op_end, op))
178 if (HAVE_LB(m_pos, out, op))
179 goto lookbehind_overrun;
180 if (HAVE_OP(t + 3 - 1, op_end, op))
183 if (t >= 2 * 4 - (3 - 1) && (op - m_pos) >= 4) {
211 if (HAVE_OP(t, op_end, op))
213 if (HAVE_IP(t + 1, ip_end, ip))
224 } while (ip < ip_end);
228 return LZO_E_EOF_NOT_FOUND;
232 return (ip == ip_end ? LZO_E_OK :
233 (ip < ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
236 return LZO_E_INPUT_OVERRUN;
240 return LZO_E_OUTPUT_OVERRUN;
244 return LZO_E_LOOKBEHIND_OVERRUN;