2 * LZO1X Decompressor from MiniLZO
6 * The full LZO package can be found at:
7 * http://www.oberhumer.com/opensource/lzo/
9 * Changed for kernel use by:
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
34 bool lzop_is_valid_header(const unsigned char *src)
37 /* read magic: 9 first bytes */
38 for (i = 0; i < ARRAY_SIZE(lzop_magic); i++) {
39 if (*src++ != lzop_magic[i])
45 static inline const unsigned char *parse_header(const unsigned char *src)
50 if (!lzop_is_valid_header(src))
56 /* get version (2bytes), skip library version (2),
57 * 'need to be extracted' version (2) and
59 version = get_unaligned_be16(src);
61 if (version >= 0x0940)
63 if (get_unaligned_be32(src) & HEADER_HAS_FILTER)
64 src += 4; /* filter info */
66 /* skip flags, mode and mtime_low */
68 if (version >= 0x0940)
69 src += 4; /* skip mtime_high */
72 /* don't care about the file name, and skip checksum */
78 int lzop_decompress(const unsigned char *src, size_t src_len,
79 unsigned char *dst, size_t *dst_len)
81 unsigned char *start = dst;
82 const unsigned char *send = src + src_len;
84 size_t tmp, remaining;
87 src = parse_header(src);
93 /* read uncompressed block size */
94 dlen = get_unaligned_be32(src);
97 /* exit if last block */
99 *dst_len = dst - start;
103 /* read compressed block size, and skip block checksum info */
104 slen = get_unaligned_be32(src);
107 if (slen <= 0 || slen > dlen)
110 /* abort if buffer ran out of room */
111 if (dlen > remaining)
112 return LZO_E_OUTPUT_OVERRUN;
114 /* When the input data is not compressed at all,
115 * lzo1x_decompress_safe will fail, so call memcpy()
118 memcpy(dst, src, slen);
122 r = lzo1x_decompress_safe((u8 *)src, slen, dst, &tmp);
125 *dst_len = dst - start;
138 return LZO_E_INPUT_OVERRUN;
141 int lzo1x_decompress_safe(const unsigned char *in, size_t in_len,
142 unsigned char *out, size_t *out_len)
144 const unsigned char * const ip_end = in + in_len;
145 unsigned char * const op_end = out + *out_len;
146 const unsigned char *ip = in, *m_pos;
147 unsigned char *op = out;
156 if (HAVE_OP(t, op_end, op))
158 if (HAVE_IP(t + 1, ip_end, ip))
163 goto first_literal_run;
166 while ((ip < ip_end)) {
171 if (HAVE_IP(1, ip_end, ip))
176 if (HAVE_IP(1, ip_end, ip))
181 if (HAVE_OP(t + 3, op_end, op))
183 if (HAVE_IP(t + 4, ip_end, ip))
213 m_pos = op - (1 + M2_MAX_OFFSET);
217 if (HAVE_LB(m_pos, out, op))
218 goto lookbehind_overrun;
220 if (HAVE_OP(3, op_end, op))
232 m_pos -= (t >> 2) & 7;
235 if (HAVE_LB(m_pos, out, op))
236 goto lookbehind_overrun;
237 if (HAVE_OP(t + 3 - 1, op_end, op))
240 } else if (t >= 32) {
243 if (HAVE_IP(1, ip_end, ip))
248 if (HAVE_IP(1, ip_end, ip))
254 m_pos -= get_unaligned_le16(ip) >> 2;
256 } else if (t >= 16) {
258 m_pos -= (t & 8) << 11;
262 if (HAVE_IP(1, ip_end, ip))
267 if (HAVE_IP(1, ip_end, ip))
272 m_pos -= get_unaligned_le16(ip) >> 2;
282 if (HAVE_LB(m_pos, out, op))
283 goto lookbehind_overrun;
284 if (HAVE_OP(2, op_end, op))
292 if (HAVE_LB(m_pos, out, op))
293 goto lookbehind_overrun;
294 if (HAVE_OP(t + 3 - 1, op_end, op))
297 if (t >= 2 * 4 - (3 - 1) && (op - m_pos) >= 4) {
325 if (HAVE_OP(t, op_end, op))
327 if (HAVE_IP(t + 1, ip_end, ip))
338 } while (ip < ip_end);
342 return LZO_E_EOF_NOT_FOUND;
346 return (ip == ip_end ? LZO_E_OK :
347 (ip < ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
350 return LZO_E_INPUT_OVERRUN;
354 return LZO_E_OUTPUT_OVERRUN;
358 return LZO_E_LOOKBEHIND_OVERRUN;