Merge branch 'docs' of git://git.lwn.net/linux-2.6
[linux-2.6] / lib / lzo / lzo1x_decompress.c
1 /*
2  *  LZO1X Decompressor from MiniLZO
3  *
4  *  Copyright (C) 1996-2005 Markus F.X.J. Oberhumer <markus@oberhumer.com>
5  *
6  *  The full LZO package can be found at:
7  *  http://www.oberhumer.com/opensource/lzo/
8  *
9  *  Changed for kernel use by:
10  *  Nitin Gupta <nitingupta910@gmail.com>
11  *  Richard Purdie <rpurdie@openedhand.com>
12  */
13
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/lzo.h>
17 #include <asm/byteorder.h>
18 #include <asm/unaligned.h>
19 #include "lzodefs.h"
20
21 #define HAVE_IP(x, ip_end, ip) ((size_t)(ip_end - ip) < (x))
22 #define HAVE_OP(x, op_end, op) ((size_t)(op_end - op) < (x))
23 #define HAVE_LB(m_pos, out, op) (m_pos < out || m_pos >= op)
24
25 #define COPY4(dst, src) \
26                 put_unaligned(get_unaligned((const u32 *)(src)), (u32 *)(dst))
27
28 int lzo1x_decompress_safe(const unsigned char *in, size_t in_len,
29                         unsigned char *out, size_t *out_len)
30 {
31         const unsigned char * const ip_end = in + in_len;
32         unsigned char * const op_end = out + *out_len;
33         const unsigned char *ip = in, *m_pos;
34         unsigned char *op = out;
35         size_t t;
36
37         *out_len = 0;
38
39         if (*ip > 17) {
40                 t = *ip++ - 17;
41                 if (t < 4)
42                         goto match_next;
43                 if (HAVE_OP(t, op_end, op))
44                         goto output_overrun;
45                 if (HAVE_IP(t + 1, ip_end, ip))
46                         goto input_overrun;
47                 do {
48                         *op++ = *ip++;
49                 } while (--t > 0);
50                 goto first_literal_run;
51         }
52
53         while ((ip < ip_end)) {
54                 t = *ip++;
55                 if (t >= 16)
56                         goto match;
57                 if (t == 0) {
58                         if (HAVE_IP(1, ip_end, ip))
59                                 goto input_overrun;
60                         while (*ip == 0) {
61                                 t += 255;
62                                 ip++;
63                                 if (HAVE_IP(1, ip_end, ip))
64                                         goto input_overrun;
65                         }
66                         t += 15 + *ip++;
67                 }
68                 if (HAVE_OP(t + 3, op_end, op))
69                         goto output_overrun;
70                 if (HAVE_IP(t + 4, ip_end, ip))
71                         goto input_overrun;
72
73                 COPY4(op, ip);
74                 op += 4;
75                 ip += 4;
76                 if (--t > 0) {
77                         if (t >= 4) {
78                                 do {
79                                         COPY4(op, ip);
80                                         op += 4;
81                                         ip += 4;
82                                         t -= 4;
83                                 } while (t >= 4);
84                                 if (t > 0) {
85                                         do {
86                                                 *op++ = *ip++;
87                                         } while (--t > 0);
88                                 }
89                         } else {
90                                 do {
91                                         *op++ = *ip++;
92                                 } while (--t > 0);
93                         }
94                 }
95
96 first_literal_run:
97                 t = *ip++;
98                 if (t >= 16)
99                         goto match;
100                 m_pos = op - (1 + M2_MAX_OFFSET);
101                 m_pos -= t >> 2;
102                 m_pos -= *ip++ << 2;
103
104                 if (HAVE_LB(m_pos, out, op))
105                         goto lookbehind_overrun;
106
107                 if (HAVE_OP(3, op_end, op))
108                         goto output_overrun;
109                 *op++ = *m_pos++;
110                 *op++ = *m_pos++;
111                 *op++ = *m_pos;
112
113                 goto match_done;
114
115                 do {
116 match:
117                         if (t >= 64) {
118                                 m_pos = op - 1;
119                                 m_pos -= (t >> 2) & 7;
120                                 m_pos -= *ip++ << 3;
121                                 t = (t >> 5) - 1;
122                                 if (HAVE_LB(m_pos, out, op))
123                                         goto lookbehind_overrun;
124                                 if (HAVE_OP(t + 3 - 1, op_end, op))
125                                         goto output_overrun;
126                                 goto copy_match;
127                         } else if (t >= 32) {
128                                 t &= 31;
129                                 if (t == 0) {
130                                         if (HAVE_IP(1, ip_end, ip))
131                                                 goto input_overrun;
132                                         while (*ip == 0) {
133                                                 t += 255;
134                                                 ip++;
135                                                 if (HAVE_IP(1, ip_end, ip))
136                                                         goto input_overrun;
137                                         }
138                                         t += 31 + *ip++;
139                                 }
140                                 m_pos = op - 1;
141                                 m_pos -= le16_to_cpu(get_unaligned(
142                                         (const unsigned short *)ip)) >> 2;
143                                 ip += 2;
144                         } else if (t >= 16) {
145                                 m_pos = op;
146                                 m_pos -= (t & 8) << 11;
147
148                                 t &= 7;
149                                 if (t == 0) {
150                                         if (HAVE_IP(1, ip_end, ip))
151                                                 goto input_overrun;
152                                         while (*ip == 0) {
153                                                 t += 255;
154                                                 ip++;
155                                                 if (HAVE_IP(1, ip_end, ip))
156                                                         goto input_overrun;
157                                         }
158                                         t += 7 + *ip++;
159                                 }
160                                 m_pos -= le16_to_cpu(get_unaligned(
161                                         (const unsigned short *)ip)) >> 2;
162                                 ip += 2;
163                                 if (m_pos == op)
164                                         goto eof_found;
165                                 m_pos -= 0x4000;
166                         } else {
167                                 m_pos = op - 1;
168                                 m_pos -= t >> 2;
169                                 m_pos -= *ip++ << 2;
170
171                                 if (HAVE_LB(m_pos, out, op))
172                                         goto lookbehind_overrun;
173                                 if (HAVE_OP(2, op_end, op))
174                                         goto output_overrun;
175
176                                 *op++ = *m_pos++;
177                                 *op++ = *m_pos;
178                                 goto match_done;
179                         }
180
181                         if (HAVE_LB(m_pos, out, op))
182                                 goto lookbehind_overrun;
183                         if (HAVE_OP(t + 3 - 1, op_end, op))
184                                 goto output_overrun;
185
186                         if (t >= 2 * 4 - (3 - 1) && (op - m_pos) >= 4) {
187                                 COPY4(op, m_pos);
188                                 op += 4;
189                                 m_pos += 4;
190                                 t -= 4 - (3 - 1);
191                                 do {
192                                         COPY4(op, m_pos);
193                                         op += 4;
194                                         m_pos += 4;
195                                         t -= 4;
196                                 } while (t >= 4);
197                                 if (t > 0)
198                                         do {
199                                                 *op++ = *m_pos++;
200                                         } while (--t > 0);
201                         } else {
202 copy_match:
203                                 *op++ = *m_pos++;
204                                 *op++ = *m_pos++;
205                                 do {
206                                         *op++ = *m_pos++;
207                                 } while (--t > 0);
208                         }
209 match_done:
210                         t = ip[-2] & 3;
211                         if (t == 0)
212                                 break;
213 match_next:
214                         if (HAVE_OP(t, op_end, op))
215                                 goto output_overrun;
216                         if (HAVE_IP(t + 1, ip_end, ip))
217                                 goto input_overrun;
218
219                         *op++ = *ip++;
220                         if (t > 1) {
221                                 *op++ = *ip++;
222                                 if (t > 2)
223                                         *op++ = *ip++;
224                         }
225
226                         t = *ip++;
227                 } while (ip < ip_end);
228         }
229
230         *out_len = op - out;
231         return LZO_E_EOF_NOT_FOUND;
232
233 eof_found:
234         *out_len = op - out;
235         return (ip == ip_end ? LZO_E_OK :
236                 (ip < ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
237 input_overrun:
238         *out_len = op - out;
239         return LZO_E_INPUT_OVERRUN;
240
241 output_overrun:
242         *out_len = op - out;
243         return LZO_E_OUTPUT_OVERRUN;
244
245 lookbehind_overrun:
246         *out_len = op - out;
247         return LZO_E_LOOKBEHIND_OVERRUN;
248 }
249
250 EXPORT_SYMBOL_GPL(lzo1x_decompress_safe);
251
252 MODULE_LICENSE("GPL");
253 MODULE_DESCRIPTION("LZO1X Decompressor");
254