]>
git.ipfire.org Git - people/ms/u-boot.git/blob - lib/lzo/lzo1x_decompress.c
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 static const unsigned char lzop_magic
[] = {
28 0x89, 0x4c, 0x5a, 0x4f, 0x00, 0x0d, 0x0a, 0x1a, 0x0a
31 #define HEADER_HAS_FILTER 0x00000800L
33 static inline const unsigned char *parse_header(const unsigned char *src
)
38 /* read magic: 9 first bytes */
39 for (i
= 0; i
< ARRAY_SIZE(lzop_magic
); i
++) {
40 if (*src
++ != lzop_magic
[i
])
43 /* get version (2bytes), skip library version (2),
44 * 'need to be extracted' version (2) and
46 version
= get_unaligned_be16(src
);
48 if (version
>= 0x0940)
50 if (get_unaligned_be32(src
) & HEADER_HAS_FILTER
)
51 src
+= 4; /* filter info */
53 /* skip flags, mode and mtime_low */
55 if (version
>= 0x0940)
56 src
+= 4; /* skip mtime_high */
59 /* don't care about the file name, and skip checksum */
65 int lzop_decompress(const unsigned char *src
, size_t src_len
,
66 unsigned char *dst
, size_t *dst_len
)
68 unsigned char *start
= dst
;
69 const unsigned char *send
= src
+ src_len
;
71 size_t tmp
, remaining
;
74 src
= parse_header(src
);
80 /* read uncompressed block size */
81 dlen
= get_unaligned_be32(src
);
84 /* exit if last block */
86 *dst_len
= dst
- start
;
90 /* read compressed block size, and skip block checksum info */
91 slen
= get_unaligned_be32(src
);
94 if (slen
<= 0 || slen
> dlen
)
97 /* abort if buffer ran out of room */
99 return LZO_E_OUTPUT_OVERRUN
;
103 r
= lzo1x_decompress_safe((u8
*) src
, slen
, dst
, &tmp
);
116 return LZO_E_INPUT_OVERRUN
;
119 int lzo1x_decompress_safe(const unsigned char *in
, size_t in_len
,
120 unsigned char *out
, size_t *out_len
)
122 const unsigned char * const ip_end
= in
+ in_len
;
123 unsigned char * const op_end
= out
+ *out_len
;
124 const unsigned char *ip
= in
, *m_pos
;
125 unsigned char *op
= out
;
134 if (HAVE_OP(t
, op_end
, op
))
136 if (HAVE_IP(t
+ 1, ip_end
, ip
))
141 goto first_literal_run
;
144 while ((ip
< ip_end
)) {
149 if (HAVE_IP(1, ip_end
, ip
))
154 if (HAVE_IP(1, ip_end
, ip
))
159 if (HAVE_OP(t
+ 3, op_end
, op
))
161 if (HAVE_IP(t
+ 4, ip_end
, ip
))
191 m_pos
= op
- (1 + M2_MAX_OFFSET
);
195 if (HAVE_LB(m_pos
, out
, op
))
196 goto lookbehind_overrun
;
198 if (HAVE_OP(3, op_end
, op
))
210 m_pos
-= (t
>> 2) & 7;
213 if (HAVE_LB(m_pos
, out
, op
))
214 goto lookbehind_overrun
;
215 if (HAVE_OP(t
+ 3 - 1, op_end
, op
))
218 } else if (t
>= 32) {
221 if (HAVE_IP(1, ip_end
, ip
))
226 if (HAVE_IP(1, ip_end
, ip
))
232 m_pos
-= get_unaligned_le16(ip
) >> 2;
234 } else if (t
>= 16) {
236 m_pos
-= (t
& 8) << 11;
240 if (HAVE_IP(1, ip_end
, ip
))
245 if (HAVE_IP(1, ip_end
, ip
))
250 m_pos
-= get_unaligned_le16(ip
) >> 2;
260 if (HAVE_LB(m_pos
, out
, op
))
261 goto lookbehind_overrun
;
262 if (HAVE_OP(2, op_end
, op
))
270 if (HAVE_LB(m_pos
, out
, op
))
271 goto lookbehind_overrun
;
272 if (HAVE_OP(t
+ 3 - 1, op_end
, op
))
275 if (t
>= 2 * 4 - (3 - 1) && (op
- m_pos
) >= 4) {
303 if (HAVE_OP(t
, op_end
, op
))
305 if (HAVE_IP(t
+ 1, ip_end
, ip
))
316 } while (ip
< ip_end
);
320 return LZO_E_EOF_NOT_FOUND
;
324 return (ip
== ip_end
? LZO_E_OK
:
325 (ip
< ip_end
? LZO_E_INPUT_NOT_CONSUMED
: LZO_E_INPUT_OVERRUN
));
328 return LZO_E_INPUT_OVERRUN
;
332 return LZO_E_OUTPUT_OVERRUN
;
336 return LZO_E_LOOKBEHIND_OVERRUN
;