1 /*
2  * LZO decompressor for the Linux kernel. Code borrowed from the lzo
3  * implementation by Markus Franz Xaver Johannes Oberhumer.
4  *
5  * Linux kernel adaptation:
6  * Copyright (C) 2009
7  * Albin Tonnerre, Free Electrons <albin.tonnerre@free-electrons.com>
8  *
9  * Original code:
10  * Copyright (C) 1996-2005 Markus Franz Xaver Johannes Oberhumer
11  * All Rights Reserved.
12  *
13  * lzop and the LZO library are free software; you can redistribute them
14  * and/or modify them under the terms of the GNU General Public License as
15  * published by the Free Software Foundation; either version 2 of
16  * the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; see the file COPYING.
25  * If not, see <http://www.gnu.org/licenses/>.
26  *
27  * Markus F.X.J. Oberhumer
28  * <markus@oberhumer.com>
29  * http://www.oberhumer.com/opensource/lzop/
30  */
31 
32 #include "decompress.h"
33 #include <xen/lzo.h>
34 
35 #ifdef __XEN__
36 #include <asm/byteorder.h>
37 #include <xen/unaligned.h>
38 #else
39 
get_unaligned_be16(const void * p)40 static inline u16 get_unaligned_be16(const void *p)
41 {
42 	return be16_to_cpup(p);
43 }
44 
get_unaligned_be32(const void * p)45 static inline u32 get_unaligned_be32(const void *p)
46 {
47 	return be32_to_cpup(p);
48 }
49 
50 #endif
51 
52 static const unsigned char lzop_magic[] = {
53 	0x89, 0x4c, 0x5a, 0x4f, 0x00, 0x0d, 0x0a, 0x1a, 0x0a };
54 
55 #define LZO_BLOCK_SIZE        (256*1024L)
56 #define HEADER_HAS_FILTER      0x00000800L
57 #define HEADER_SIZE_MIN       (9 + 7     + 4 + 8     + 1       + 4)
58 #define HEADER_SIZE_MAX       (9 + 7 + 1 + 8 + 8 + 4 + 1 + 255 + 4)
59 
parse_header(u8 * input,int * skip,int in_len)60 static int __init parse_header(u8 *input, int *skip, int in_len)
61 {
62 	int l;
63 	u8 *parse = input;
64 	u8 *end = input + in_len;
65 	u8 level = 0;
66 	u16 version;
67 
68 	/*
69 	 * Check that there's enough input to possibly have a valid header.
70 	 * Then it is possible to parse several fields until the minimum
71 	 * size may have been used.
72 	 */
73 	if (in_len < HEADER_SIZE_MIN)
74 		return 0;
75 
76 	/* read magic: 9 first bits */
77 	for (l = 0; l < 9; l++) {
78 		if (*parse++ != lzop_magic[l])
79 			return 0;
80 	}
81 	/* get version (2bytes), skip library version (2),
82 	 * 'need to be extracted' version (2) and
83 	 * method (1) */
84 	version = get_unaligned_be16(parse);
85 	parse += 7;
86 	if (version >= 0x0940)
87 		level = *parse++;
88 	if (get_unaligned_be32(parse) & HEADER_HAS_FILTER)
89 		parse += 8; /* flags + filter info */
90 	else
91 		parse += 4; /* flags */
92 
93 	/*
94 	 * At least mode, mtime_low, filename length, and checksum must
95 	 * be left to be parsed. If also mtime_high is present, it's OK
96 	 * because the next input buffer check is after reading the
97 	 * filename length.
98 	 */
99 	if (end - parse < 8 + 1 + 4)
100 		return 0;
101 
102 	/* skip mode and mtime_low */
103 	parse += 8;
104 	if (version >= 0x0940)
105 		parse += 4;	/* skip mtime_high */
106 
107 	l = *parse++;
108 	/* don't care about the file name, and skip checksum */
109 	if (end - parse < l + 4)
110 		return 0;
111 	parse += l + 4;
112 
113 	*skip = parse - input;
114 	return 1;
115 }
116 
unlzo(unsigned char * input,unsigned int in_len,int (* fill)(void *,unsigned int),int (* flush)(void *,unsigned int),unsigned char * output,unsigned int * posp,void (* error)(const char * x))117 int __init unlzo(unsigned char *input, unsigned int in_len,
118 		 int (*fill) (void *, unsigned int),
119 		 int (*flush) (void *, unsigned int),
120 		 unsigned char *output, unsigned int *posp,
121 		 void (*error) (const char *x))
122 {
123 	u8 r = 0;
124 	int skip = 0;
125 	u32 src_len, dst_len;
126 	size_t tmp;
127 	u8 *in_buf, *in_buf_save, *out_buf;
128 	int ret = -1;
129 
130 	if (output) {
131 		out_buf = output;
132 	} else if (!flush) {
133 		error("NULL output pointer and no flush function provided");
134 		goto exit;
135 	} else {
136 		out_buf = malloc(LZO_BLOCK_SIZE);
137 		if (!out_buf) {
138 			error("Could not allocate output buffer");
139 			goto exit;
140 		}
141 	}
142 
143 	if (input && fill) {
144 		error("Both input pointer and fill function provided, don't know what to do");
145 		goto exit_1;
146 	} else if (input) {
147 		in_buf = input;
148 	} else if (!fill || !posp) {
149 		error("NULL input pointer and missing position pointer or fill function");
150 		goto exit_1;
151 	} else {
152 		in_buf = malloc(lzo1x_worst_compress(LZO_BLOCK_SIZE));
153 		if (!in_buf) {
154 			error("Could not allocate input buffer");
155 			goto exit_1;
156 		}
157 	}
158 	in_buf_save = in_buf;
159 
160 	if (posp)
161 		*posp = 0;
162 
163 	if (fill)
164 		fill(in_buf, lzo1x_worst_compress(LZO_BLOCK_SIZE));
165 
166 	if (!parse_header(input, &skip, in_len)) {
167 		error("invalid header");
168 		goto exit_2;
169 	}
170 	in_buf += skip;
171 	in_len -= skip;
172 
173 	if (posp)
174 		*posp = skip;
175 
176 	for (;;) {
177 		/* read uncompressed block size */
178 		if (in_len < 4) {
179 			error("file corrupted");
180 			goto exit_2;
181 		}
182 		dst_len = get_unaligned_be32(in_buf);
183 		in_buf += 4;
184 		in_len -= 4;
185 
186 		/* exit if last block */
187 		if (dst_len == 0) {
188 			if (posp)
189 				*posp += 4;
190 			break;
191 		}
192 
193 		if (dst_len > LZO_BLOCK_SIZE) {
194 			error("dest len longer than block size");
195 			goto exit_2;
196 		}
197 
198 		/* read compressed block size, and skip block checksum info */
199 		if (in_len < 8) {
200 			error("file corrupted");
201 			goto exit_2;
202 		}
203 		src_len = get_unaligned_be32(in_buf);
204 		in_buf += 8;
205 		in_len -= 8;
206 
207 		if (src_len <= 0 || src_len > dst_len || src_len > in_len) {
208 			error("file corrupted");
209 			goto exit_2;
210 		}
211 
212 		/* decompress */
213 		tmp = dst_len;
214 
215 		/* When the input data is not compressed at all,
216 		 * lzo1x_decompress_safe will fail, so call memcpy()
217 		 * instead */
218 		if (unlikely(dst_len == src_len))
219 			memcpy(out_buf, in_buf, src_len);
220 		else {
221 			r = lzo1x_decompress_safe(in_buf, src_len,
222 						  out_buf, &tmp);
223 
224 			if (r != LZO_E_OK || dst_len != tmp) {
225 				error("Compressed data violation");
226 				goto exit_2;
227 			}
228 		}
229 
230 		if (flush && flush(out_buf, dst_len) != dst_len)
231 			goto exit_2;
232 		if (output)
233 			out_buf += dst_len;
234 		if (posp)
235 			*posp += src_len + 12;
236 		if (fill) {
237 			in_buf = in_buf_save;
238 			fill(in_buf, lzo1x_worst_compress(LZO_BLOCK_SIZE));
239 		} else {
240 			in_buf += src_len;
241 			in_len -= src_len;
242 		}
243 	}
244 
245 	ret = 0;
246 exit_2:
247 	if (!input)
248 		free(in_buf_save);
249 exit_1:
250 	if (!output)
251 		free(out_buf);
252 exit:
253 	return ret;
254 }
255