blob: 8563081e8da38fb81e0335d2589c9fcebcd81266 [file] [log] [blame]
Richard Purdie64c70b12007-07-10 17:22:24 -07001/*
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +02002 * LZO1X Decompressor from LZO
Richard Purdie64c70b12007-07-10 17:22:24 -07003 *
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +02004 * Copyright (C) 1996-2012 Markus F.X.J. Oberhumer <markus@oberhumer.com>
Richard Purdie64c70b12007-07-10 17:22:24 -07005 *
6 * The full LZO package can be found at:
7 * http://www.oberhumer.com/opensource/lzo/
8 *
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +02009 * Changed for Linux kernel use by:
Richard Purdie64c70b12007-07-10 17:22:24 -070010 * Nitin Gupta <nitingupta910@gmail.com>
11 * Richard Purdie <rpurdie@openedhand.com>
12 */
13
Albin Tonnerre7dd65fe2010-01-08 14:42:42 -080014#ifndef STATIC
Richard Purdie64c70b12007-07-10 17:22:24 -070015#include <linux/module.h>
16#include <linux/kernel.h>
Albin Tonnerre7dd65fe2010-01-08 14:42:42 -080017#endif
Richard Purdie64c70b12007-07-10 17:22:24 -070018#include <asm/unaligned.h>
Albin Tonnerre7dd65fe2010-01-08 14:42:42 -080019#include <linux/lzo.h>
Richard Purdie64c70b12007-07-10 17:22:24 -070020#include "lzodefs.h"
21
Greg Kroah-Hartman206a81c2014-06-20 22:00:53 -070022#define HAVE_IP(t, x) \
23 (((size_t)(ip_end - ip) >= (size_t)(t + x)) && \
24 (((t + x) >= t) && ((t + x) >= x)))
25
26#define HAVE_OP(t, x) \
27 (((size_t)(op_end - op) >= (size_t)(t + x)) && \
28 (((t + x) >= t) && ((t + x) >= x)))
29
30#define NEED_IP(t, x) \
31 do { \
32 if (!HAVE_IP(t, x)) \
33 goto input_overrun; \
34 } while (0)
35
36#define NEED_OP(t, x) \
37 do { \
38 if (!HAVE_OP(t, x)) \
39 goto output_overrun; \
40 } while (0)
41
42#define TEST_LB(m_pos) \
43 do { \
44 if ((m_pos) < out) \
45 goto lookbehind_overrun; \
46 } while (0)
Richard Purdie64c70b12007-07-10 17:22:24 -070047
48int lzo1x_decompress_safe(const unsigned char *in, size_t in_len,
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +020049 unsigned char *out, size_t *out_len)
Richard Purdie64c70b12007-07-10 17:22:24 -070050{
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +020051 unsigned char *op;
52 const unsigned char *ip;
53 size_t t, next;
54 size_t state = 0;
55 const unsigned char *m_pos;
Richard Purdie64c70b12007-07-10 17:22:24 -070056 const unsigned char * const ip_end = in + in_len;
57 unsigned char * const op_end = out + *out_len;
Richard Purdie64c70b12007-07-10 17:22:24 -070058
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +020059 op = out;
60 ip = in;
Richard Purdie64c70b12007-07-10 17:22:24 -070061
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +020062 if (unlikely(in_len < 3))
63 goto input_overrun;
Richard Purdie64c70b12007-07-10 17:22:24 -070064 if (*ip > 17) {
65 t = *ip++ - 17;
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +020066 if (t < 4) {
67 next = t;
Richard Purdie64c70b12007-07-10 17:22:24 -070068 goto match_next;
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +020069 }
70 goto copy_literal_run;
Richard Purdie64c70b12007-07-10 17:22:24 -070071 }
72
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +020073 for (;;) {
Richard Purdie64c70b12007-07-10 17:22:24 -070074 t = *ip++;
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +020075 if (t < 16) {
76 if (likely(state == 0)) {
77 if (unlikely(t == 0)) {
78 while (unlikely(*ip == 0)) {
79 t += 255;
80 ip++;
Greg Kroah-Hartman206a81c2014-06-20 22:00:53 -070081 NEED_IP(1, 0);
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +020082 }
83 t += 15 + *ip++;
84 }
85 t += 3;
86copy_literal_run:
87#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
Greg Kroah-Hartman206a81c2014-06-20 22:00:53 -070088 if (likely(HAVE_IP(t, 15) && HAVE_OP(t, 15))) {
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +020089 const unsigned char *ie = ip + t;
90 unsigned char *oe = op + t;
91 do {
92 COPY8(op, ip);
93 op += 8;
94 ip += 8;
95 COPY8(op, ip);
96 op += 8;
97 ip += 8;
98 } while (ip < ie);
99 ip = ie;
100 op = oe;
101 } else
102#endif
103 {
Greg Kroah-Hartman206a81c2014-06-20 22:00:53 -0700104 NEED_OP(t, 0);
105 NEED_IP(t, 3);
Richard Purdie64c70b12007-07-10 17:22:24 -0700106 do {
107 *op++ = *ip++;
108 } while (--t > 0);
109 }
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200110 state = 4;
111 continue;
112 } else if (state != 4) {
113 next = t & 3;
Richard Purdie64c70b12007-07-10 17:22:24 -0700114 m_pos = op - 1;
115 m_pos -= t >> 2;
116 m_pos -= *ip++ << 2;
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200117 TEST_LB(m_pos);
Greg Kroah-Hartman206a81c2014-06-20 22:00:53 -0700118 NEED_OP(2, 0);
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200119 op[0] = m_pos[0];
120 op[1] = m_pos[1];
121 op += 2;
122 goto match_next;
Richard Purdie64c70b12007-07-10 17:22:24 -0700123 } else {
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200124 next = t & 3;
125 m_pos = op - (1 + M2_MAX_OFFSET);
126 m_pos -= t >> 2;
127 m_pos -= *ip++ << 2;
128 t = 3;
129 }
130 } else if (t >= 64) {
131 next = t & 3;
132 m_pos = op - 1;
133 m_pos -= (t >> 2) & 7;
134 m_pos -= *ip++ << 3;
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)) {
140 t += 255;
141 ip++;
Greg Kroah-Hartman206a81c2014-06-20 22:00:53 -0700142 NEED_IP(1, 0);
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200143 }
144 t += 31 + *ip++;
Greg Kroah-Hartman206a81c2014-06-20 22:00:53 -0700145 NEED_IP(2, 0);
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200146 }
147 m_pos = op - 1;
148 next = get_unaligned_le16(ip);
149 ip += 2;
150 m_pos -= next >> 2;
151 next &= 3;
152 } else {
153 m_pos = op;
154 m_pos -= (t & 8) << 11;
155 t = (t & 7) + (3 - 1);
156 if (unlikely(t == 2)) {
157 while (unlikely(*ip == 0)) {
158 t += 255;
159 ip++;
Greg Kroah-Hartman206a81c2014-06-20 22:00:53 -0700160 NEED_IP(1, 0);
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200161 }
162 t += 7 + *ip++;
Greg Kroah-Hartman206a81c2014-06-20 22:00:53 -0700163 NEED_IP(2, 0);
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200164 }
165 next = get_unaligned_le16(ip);
166 ip += 2;
167 m_pos -= next >> 2;
168 next &= 3;
169 if (m_pos == op)
170 goto eof_found;
171 m_pos -= 0x4000;
172 }
173 TEST_LB(m_pos);
174#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
175 if (op - m_pos >= 8) {
176 unsigned char *oe = op + t;
Greg Kroah-Hartman206a81c2014-06-20 22:00:53 -0700177 if (likely(HAVE_OP(t, 15))) {
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200178 do {
179 COPY8(op, m_pos);
180 op += 8;
181 m_pos += 8;
182 COPY8(op, m_pos);
183 op += 8;
184 m_pos += 8;
185 } while (op < oe);
186 op = oe;
Greg Kroah-Hartman206a81c2014-06-20 22:00:53 -0700187 if (HAVE_IP(6, 0)) {
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200188 state = next;
189 COPY4(op, ip);
190 op += next;
191 ip += next;
192 continue;
193 }
194 } else {
Greg Kroah-Hartman206a81c2014-06-20 22:00:53 -0700195 NEED_OP(t, 0);
Richard Purdie64c70b12007-07-10 17:22:24 -0700196 do {
197 *op++ = *m_pos++;
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200198 } while (op < oe);
Richard Purdie64c70b12007-07-10 17:22:24 -0700199 }
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200200 } else
201#endif
202 {
203 unsigned char *oe = op + t;
Greg Kroah-Hartman206a81c2014-06-20 22:00:53 -0700204 NEED_OP(t, 0);
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200205 op[0] = m_pos[0];
206 op[1] = m_pos[1];
207 op += 2;
208 m_pos += 2;
209 do {
210 *op++ = *m_pos++;
211 } while (op < oe);
212 }
Richard Purdie64c70b12007-07-10 17:22:24 -0700213match_next:
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200214 state = next;
215 t = next;
216#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
Greg Kroah-Hartman206a81c2014-06-20 22:00:53 -0700217 if (likely(HAVE_IP(6, 0) && HAVE_OP(4, 0))) {
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200218 COPY4(op, ip);
219 op += t;
220 ip += t;
221 } else
222#endif
223 {
Greg Kroah-Hartman206a81c2014-06-20 22:00:53 -0700224 NEED_IP(t, 3);
225 NEED_OP(t, 0);
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200226 while (t > 0) {
Richard Purdie64c70b12007-07-10 17:22:24 -0700227 *op++ = *ip++;
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200228 t--;
Richard Purdie64c70b12007-07-10 17:22:24 -0700229 }
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200230 }
Richard Purdie64c70b12007-07-10 17:22:24 -0700231 }
232
Richard Purdie64c70b12007-07-10 17:22:24 -0700233eof_found:
234 *out_len = op - out;
Markus F.X.J. Oberhumer8b975bd2012-08-13 17:25:44 +0200235 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);
238
Richard Purdie64c70b12007-07-10 17:22:24 -0700239input_overrun:
240 *out_len = op - out;
241 return LZO_E_INPUT_OVERRUN;
242
243output_overrun:
244 *out_len = op - out;
245 return LZO_E_OUTPUT_OVERRUN;
246
247lookbehind_overrun:
248 *out_len = op - out;
249 return LZO_E_LOOKBEHIND_OVERRUN;
250}
Albin Tonnerre7dd65fe2010-01-08 14:42:42 -0800251#ifndef STATIC
Richard Purdie64c70b12007-07-10 17:22:24 -0700252EXPORT_SYMBOL_GPL(lzo1x_decompress_safe);
253
254MODULE_LICENSE("GPL");
255MODULE_DESCRIPTION("LZO1X Decompressor");
256
Albin Tonnerre7dd65fe2010-01-08 14:42:42 -0800257#endif