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 -= le16_to_cpu(get_unaligned(
142 (const unsigned short *)ip)) >> 2;
144 } else if (t >= 16) {
146 m_pos -= (t & 8) << 11;
150 if (HAVE_IP(1, ip_end, ip))
155 if (HAVE_IP(1, ip_end, ip))
160 m_pos -= le16_to_cpu(get_unaligned(
161 (const unsigned short *)ip)) >> 2;
171 if (HAVE_LB(m_pos, out, op))
172 goto lookbehind_overrun;
173 if (HAVE_OP(2, op_end, op))
181 if (HAVE_LB(m_pos, out, op))
182 goto lookbehind_overrun;
183 if (HAVE_OP(t + 3 - 1, op_end, op))
186 if (t >= 2 * 4 - (3 - 1) && (op - m_pos) >= 4) {
214 if (HAVE_OP(t, op_end, op))
216 if (HAVE_IP(t + 1, ip_end, ip))
227 } while (ip < ip_end);
231 return LZO_E_EOF_NOT_FOUND;
235 return (ip == ip_end ? LZO_E_OK :
236 (ip < ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
239 return LZO_E_INPUT_OVERRUN;
243 return LZO_E_OUTPUT_OVERRUN;
247 return LZO_E_LOOKBEHIND_OVERRUN;
250 EXPORT_SYMBOL_GPL(lzo1x_decompress_safe);
252 MODULE_LICENSE("GPL");
253 MODULE_DESCRIPTION("LZO1X Decompressor");