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>
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/lzo.h>
17 #include <asm/byteorder.h>
18 #include <asm/unaligned.h>
21 #define HAVE_IP(x, ip_end, ip) ((size_t)(ip_end - ip) < (x))
22 #define HAVE_OP(x, op_end, op) ((size_t)(op_end - op) < (x))
23 #define HAVE_LB(m_pos, out, op) (m_pos < out || m_pos >= op)
25 #define COPY4(dst, src) \
26 put_unaligned(get_unaligned((const u32 *)(src)), (u32 *)(dst))
28 int lzo1x_decompress_safe(const unsigned char *in, size_t in_len,
29 unsigned char *out, size_t *out_len)
31 const unsigned char * const ip_end = in + in_len;
32 unsigned char * const op_end = out + *out_len;
33 const unsigned char *ip = in, *m_pos;
34 unsigned char *op = out;
43 if (HAVE_OP(t, op_end, op))
45 if (HAVE_IP(t + 1, ip_end, ip))
50 goto first_literal_run;
53 while ((ip < ip_end)) {
58 if (HAVE_IP(1, ip_end, ip))
63 if (HAVE_IP(1, ip_end, ip))
68 if (HAVE_OP(t + 3, op_end, op))
70 if (HAVE_IP(t + 4, ip_end, ip))
100 m_pos = op - (1 + M2_MAX_OFFSET);
104 if (HAVE_LB(m_pos, out, op))
105 goto lookbehind_overrun;
107 if (HAVE_OP(3, op_end, op))
119 m_pos -= (t >> 2) & 7;
122 if (HAVE_LB(m_pos, out, op))
123 goto lookbehind_overrun;
124 if (HAVE_OP(t + 3 - 1, op_end, op))
127 } else if (t >= 32) {
130 if (HAVE_IP(1, ip_end, ip))
135 if (HAVE_IP(1, ip_end, ip))
141 m_pos -= get_unaligned_le16(ip) >> 2;
143 } else if (t >= 16) {
145 m_pos -= (t & 8) << 11;
149 if (HAVE_IP(1, ip_end, ip))
154 if (HAVE_IP(1, ip_end, ip))
159 m_pos -= get_unaligned_le16(ip) >> 2;
169 if (HAVE_LB(m_pos, out, op))
170 goto lookbehind_overrun;
171 if (HAVE_OP(2, op_end, op))
179 if (HAVE_LB(m_pos, out, op))
180 goto lookbehind_overrun;
181 if (HAVE_OP(t + 3 - 1, op_end, op))
184 if (t >= 2 * 4 - (3 - 1) && (op - m_pos) >= 4) {
212 if (HAVE_OP(t, op_end, op))
214 if (HAVE_IP(t + 1, ip_end, ip))
225 } while (ip < ip_end);
229 return LZO_E_EOF_NOT_FOUND;
233 return (ip == ip_end ? LZO_E_OK :
234 (ip < ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
237 return LZO_E_INPUT_OVERRUN;
241 return LZO_E_OUTPUT_OVERRUN;
245 return LZO_E_LOOKBEHIND_OVERRUN;
248 EXPORT_SYMBOL_GPL(lzo1x_decompress_safe);
250 MODULE_LICENSE("GPL");
251 MODULE_DESCRIPTION("LZO1X Decompressor");