2 * LZO1X Decompressor from LZO
4 * Copyright (C) 1996-2012 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 Linux 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>
18 #include <asm/unaligned.h>
19 #include <linux/lzo.h>
22 #define HAVE_IP(t, x) \
23 (((size_t)(ip_end - ip) >= (size_t)(t + x)) && \
24 (((t + x) >= t) && ((t + x) >= x)))
26 #define HAVE_OP(t, x) \
27 (((size_t)(op_end - op) >= (size_t)(t + x)) && \
28 (((t + x) >= t) && ((t + x) >= x)))
30 #define NEED_IP(t, x) \
36 #define NEED_OP(t, x) \
39 goto output_overrun; \
42 #define TEST_LB(m_pos) \
45 goto lookbehind_overrun; \
48 int lzo1x_decompress_safe(const unsigned char *in
, size_t in_len
,
49 unsigned char *out
, size_t *out_len
)
52 const unsigned char *ip
;
55 const unsigned char *m_pos
;
56 const unsigned char * const ip_end
= in
+ in_len
;
57 unsigned char * const op_end
= out
+ *out_len
;
62 if (unlikely(in_len
< 3))
70 goto copy_literal_run
;
76 if (likely(state
== 0)) {
77 if (unlikely(t
== 0)) {
78 while (unlikely(*ip
== 0)) {
87 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
88 if (likely(HAVE_IP(t
, 15) && HAVE_OP(t
, 15))) {
89 const unsigned char *ie
= ip
+ t
;
90 unsigned char *oe
= op
+ t
;
112 } else if (state
!= 4) {
125 m_pos
= op
- (1 + M2_MAX_OFFSET
);
130 } else if (t
>= 64) {
133 m_pos
-= (t
>> 2) & 7;
135 t
= (t
>> 5) - 1 + (3 - 1);
136 } else if (t
>= 32) {
137 t
= (t
& 31) + (3 - 1);
138 if (unlikely(t
== 2)) {
139 while (unlikely(*ip
== 0)) {
148 next
= get_unaligned_le16(ip
);
154 m_pos
-= (t
& 8) << 11;
155 t
= (t
& 7) + (3 - 1);
156 if (unlikely(t
== 2)) {
157 while (unlikely(*ip
== 0)) {
165 next
= get_unaligned_le16(ip
);
174 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
175 if (op
- m_pos
>= 8) {
176 unsigned char *oe
= op
+ t
;
177 if (likely(HAVE_OP(t
, 15))) {
203 unsigned char *oe
= op
+ t
;
216 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
217 if (likely(HAVE_IP(6, 0) && HAVE_OP(4, 0))) {
235 return (t
!= 3 ? LZO_E_ERROR
:
236 ip
== ip_end
? LZO_E_OK
:
237 ip
< ip_end
? LZO_E_INPUT_NOT_CONSUMED
: LZO_E_INPUT_OVERRUN
);
241 return LZO_E_INPUT_OVERRUN
;
245 return LZO_E_OUTPUT_OVERRUN
;
249 return LZO_E_LOOKBEHIND_OVERRUN
;
252 EXPORT_SYMBOL_GPL(lzo1x_decompress_safe
);
254 MODULE_LICENSE("GPL");
255 MODULE_DESCRIPTION("LZO1X Decompressor");