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(x) ((size_t)(ip_end - ip) >= (size_t)(x))
23 #define HAVE_OP(x) ((size_t)(op_end - op) >= (size_t)(x))
24 #define NEED_IP(x) if (!HAVE_IP(x)) goto input_overrun
25 #define NEED_OP(x) if (!HAVE_OP(x)) goto output_overrun
26 #define TEST_LB(m_pos) if ((m_pos) < out) goto lookbehind_overrun
28 /* This MAX_255_COUNT is the maximum number of times we can add 255 to a base
29 * count without overflowing an integer. The multiply will overflow when
30 * multiplying 255 by more than MAXINT/255. The sum will overflow earlier
31 * depending on the base count. Since the base count is taken from a u8
32 * and a few bits, it is safe to assume that it will always be lower than
33 * or equal to 2*255, thus we can always prevent any overflow by accepting
34 * two less 255 steps. See Documentation/lzo.txt for more information.
36 #define MAX_255_COUNT ((((size_t)~0) / 255) - 2)
38 int lzo1x_decompress_safe(const unsigned char *in, size_t in_len,
39 unsigned char *out, size_t *out_len)
42 const unsigned char *ip;
45 const unsigned char *m_pos;
46 const unsigned char * const ip_end = in + in_len;
47 unsigned char * const op_end = out + *out_len;
52 if (unlikely(in_len < 3))
60 goto copy_literal_run;
66 if (likely(state == 0)) {
67 if (unlikely(t == 0)) {
69 const unsigned char *ip_last = ip;
71 while (unlikely(*ip == 0)) {
75 offset = ip - ip_last;
76 if (unlikely(offset > MAX_255_COUNT))
79 offset = (offset << 8) - offset;
80 t += offset + 15 + *ip++;
84 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
85 if (likely(HAVE_IP(t + 15) && HAVE_OP(t + 15))) {
86 const unsigned char *ie = ip + t;
87 unsigned char *oe = op + t;
109 } else if (state != 4) {
122 m_pos = op - (1 + M2_MAX_OFFSET);
127 } else if (t >= 64) {
130 m_pos -= (t >> 2) & 7;
132 t = (t >> 5) - 1 + (3 - 1);
133 } else if (t >= 32) {
134 t = (t & 31) + (3 - 1);
135 if (unlikely(t == 2)) {
137 const unsigned char *ip_last = ip;
139 while (unlikely(*ip == 0)) {
143 offset = ip - ip_last;
144 if (unlikely(offset > MAX_255_COUNT))
147 offset = (offset << 8) - offset;
148 t += offset + 31 + *ip++;
152 next = get_unaligned_le16(ip);
158 m_pos -= (t & 8) << 11;
159 t = (t & 7) + (3 - 1);
160 if (unlikely(t == 2)) {
162 const unsigned char *ip_last = ip;
164 while (unlikely(*ip == 0)) {
168 offset = ip - ip_last;
169 if (unlikely(offset > MAX_255_COUNT))
172 offset = (offset << 8) - offset;
173 t += offset + 7 + *ip++;
176 next = get_unaligned_le16(ip);
185 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
186 if (op - m_pos >= 8) {
187 unsigned char *oe = op + t;
188 if (likely(HAVE_OP(t + 15))) {
214 unsigned char *oe = op + t;
227 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
228 if (likely(HAVE_IP(6) && HAVE_OP(4))) {
246 return (t != 3 ? LZO_E_ERROR :
247 ip == ip_end ? LZO_E_OK :
248 ip < ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN);
252 return LZO_E_INPUT_OVERRUN;
256 return LZO_E_OUTPUT_OVERRUN;
260 return LZO_E_LOOKBEHIND_OVERRUN;
263 EXPORT_SYMBOL_GPL(lzo1x_decompress_safe);
265 MODULE_LICENSE("GPL");
266 MODULE_DESCRIPTION("LZO1X Decompressor");