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/module.h>
16 #include <linux/kernel.h>
19 #include <asm/unaligned.h>
20 #include <linux/lzo.h>
23 #define HAVE_IP(x, ip_end, ip) ((size_t)(ip_end - ip) < (x))
24 #define HAVE_OP(x, op_end, op) ((size_t)(op_end - op) < (x))
25 #define HAVE_LB(m_pos, out, op) (m_pos < out || m_pos >= op)
27 #define COPY4(dst, src) \
28 put_unaligned(get_unaligned((const u32 *)(src)), (u32 *)(dst))
30 int lzo1x_decompress_safe(const unsigned char *in
, size_t in_len
,
31 unsigned char *out
, size_t *out_len
)
33 const unsigned char * const ip_end
= in
+ in_len
;
34 unsigned char * const op_end
= out
+ *out_len
;
35 const unsigned char *ip
= in
, *m_pos
;
36 unsigned char *op
= out
;
45 if (HAVE_OP(t
, op_end
, op
))
47 if (HAVE_IP(t
+ 1, ip_end
, ip
))
52 goto first_literal_run
;
55 while ((ip
< ip_end
)) {
60 if (HAVE_IP(1, ip_end
, ip
))
65 if (HAVE_IP(1, ip_end
, ip
))
70 if (HAVE_OP(t
+ 3, op_end
, op
))
72 if (HAVE_IP(t
+ 4, ip_end
, ip
))
102 m_pos
= op
- (1 + M2_MAX_OFFSET
);
106 if (HAVE_LB(m_pos
, out
, op
))
107 goto lookbehind_overrun
;
109 if (HAVE_OP(3, op_end
, op
))
121 m_pos
-= (t
>> 2) & 7;
124 if (HAVE_LB(m_pos
, out
, op
))
125 goto lookbehind_overrun
;
126 if (HAVE_OP(t
+ 3 - 1, op_end
, op
))
129 } else if (t
>= 32) {
132 if (HAVE_IP(1, ip_end
, ip
))
137 if (HAVE_IP(1, ip_end
, ip
))
143 m_pos
-= get_unaligned_le16(ip
) >> 2;
145 } else if (t
>= 16) {
147 m_pos
-= (t
& 8) << 11;
151 if (HAVE_IP(1, ip_end
, ip
))
156 if (HAVE_IP(1, ip_end
, ip
))
161 m_pos
-= get_unaligned_le16(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");