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 <xen/unaligned.h>
37 #else
38 
get_unaligned_be16(const void * p)39 static inline uint16_t get_unaligned_be16(const void *p)
40 {
41 	uint16_t v;
42 
43 	memcpy(&v, p, sizeof(v));
44 
45 	return be16_to_cpu(v);
46 }
47 
get_unaligned_be32(const void * p)48 static inline uint32_t get_unaligned_be32(const void *p)
49 {
50 	uint32_t v;
51 
52 	memcpy(&v, p, sizeof(v));
53 
54 	return be32_to_cpu(v);
55 }
56 
57 #endif
58 
59 static const unsigned char lzop_magic[] = {
60 	0x89, 0x4c, 0x5a, 0x4f, 0x00, 0x0d, 0x0a, 0x1a, 0x0a };
61 
62 #define LZO_BLOCK_SIZE        (256*1024L)
63 #define HEADER_HAS_FILTER      0x00000800L
64 #define HEADER_SIZE_MIN       (9 + 7     + 4 + 8     + 1       + 4)
65 #define HEADER_SIZE_MAX       (9 + 7 + 1 + 8 + 8 + 4 + 1 + 255 + 4)
66 
parse_header(u8 * input,int * skip,int in_len)67 static int __init parse_header(u8 *input, int *skip, int in_len)
68 {
69 	int l;
70 	u8 *parse = input;
71 	u8 *end = input + in_len;
72 	u8 level = 0;
73 	u16 version;
74 
75 	/*
76 	 * Check that there's enough input to possibly have a valid header.
77 	 * Then it is possible to parse several fields until the minimum
78 	 * size may have been used.
79 	 */
80 	if (in_len < HEADER_SIZE_MIN)
81 		return 0;
82 
83 	/* read magic: 9 first bits */
84 	for (l = 0; l < 9; l++) {
85 		if (*parse++ != lzop_magic[l])
86 			return 0;
87 	}
88 	/* get version (2bytes), skip library version (2),
89 	 * 'need to be extracted' version (2) and
90 	 * method (1) */
91 	version = get_unaligned_be16(parse);
92 	parse += 7;
93 	if (version >= 0x0940)
94 		level = *parse++;
95 	if (get_unaligned_be32(parse) & HEADER_HAS_FILTER)
96 		parse += 8; /* flags + filter info */
97 	else
98 		parse += 4; /* flags */
99 
100 	/*
101 	 * At least mode, mtime_low, filename length, and checksum must
102 	 * be left to be parsed. If also mtime_high is present, it's OK
103 	 * because the next input buffer check is after reading the
104 	 * filename length.
105 	 */
106 	if (end - parse < 8 + 1 + 4)
107 		return 0;
108 
109 	/* skip mode and mtime_low */
110 	parse += 8;
111 	if (version >= 0x0940)
112 		parse += 4;	/* skip mtime_high */
113 
114 	l = *parse++;
115 	/* don't care about the file name, and skip checksum */
116 	if (end - parse < l + 4)
117 		return 0;
118 	parse += l + 4;
119 
120 	*skip = parse - input;
121 	return 1;
122 }
123 
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))124 int __init unlzo(unsigned char *input, unsigned int in_len,
125 		 int (*fill) (void *, unsigned int),
126 		 int (*flush) (void *, unsigned int),
127 		 unsigned char *output, unsigned int *posp,
128 		 void (*error) (const char *x))
129 {
130 	u8 r = 0;
131 	int skip = 0;
132 	u32 src_len, dst_len;
133 	size_t tmp;
134 	u8 *in_buf, *in_buf_save, *out_buf;
135 	int ret = -1;
136 
137 	if (output) {
138 		out_buf = output;
139 	} else if (!flush) {
140 		error("NULL output pointer and no flush function provided");
141 		goto exit;
142 	} else {
143 		out_buf = malloc(LZO_BLOCK_SIZE);
144 		if (!out_buf) {
145 			error("Could not allocate output buffer");
146 			goto exit;
147 		}
148 	}
149 
150 	if (input && fill) {
151 		error("Both input pointer and fill function provided, don't know what to do");
152 		goto exit_1;
153 	} else if (input) {
154 		in_buf = input;
155 	} else if (!fill || !posp) {
156 		error("NULL input pointer and missing position pointer or fill function");
157 		goto exit_1;
158 	} else {
159 		in_buf = malloc(lzo1x_worst_compress(LZO_BLOCK_SIZE));
160 		if (!in_buf) {
161 			error("Could not allocate input buffer");
162 			goto exit_1;
163 		}
164 	}
165 	in_buf_save = in_buf;
166 
167 	if (posp)
168 		*posp = 0;
169 
170 	if (fill)
171 		fill(in_buf, lzo1x_worst_compress(LZO_BLOCK_SIZE));
172 
173 	if (!parse_header(input, &skip, in_len)) {
174 		error("invalid header");
175 		goto exit_2;
176 	}
177 	in_buf += skip;
178 	in_len -= skip;
179 
180 	if (posp)
181 		*posp = skip;
182 
183 	for (;;) {
184 		/* read uncompressed block size */
185 		if (in_len < 4) {
186 			error("file corrupted");
187 			goto exit_2;
188 		}
189 		dst_len = get_unaligned_be32(in_buf);
190 		in_buf += 4;
191 		in_len -= 4;
192 
193 		/* exit if last block */
194 		if (dst_len == 0) {
195 			if (posp)
196 				*posp += 4;
197 			break;
198 		}
199 
200 		if (dst_len > LZO_BLOCK_SIZE) {
201 			error("dest len longer than block size");
202 			goto exit_2;
203 		}
204 
205 		/* read compressed block size, and skip block checksum info */
206 		if (in_len < 8) {
207 			error("file corrupted");
208 			goto exit_2;
209 		}
210 		src_len = get_unaligned_be32(in_buf);
211 		in_buf += 8;
212 		in_len -= 8;
213 
214 		if (src_len <= 0 || src_len > dst_len || src_len > in_len) {
215 			error("file corrupted");
216 			goto exit_2;
217 		}
218 
219 		/* decompress */
220 		tmp = dst_len;
221 
222 		/* When the input data is not compressed at all,
223 		 * lzo1x_decompress_safe will fail, so call memcpy()
224 		 * instead */
225 		if (unlikely(dst_len == src_len))
226 			memcpy(out_buf, in_buf, src_len);
227 		else {
228 			r = lzo1x_decompress_safe(in_buf, src_len,
229 						  out_buf, &tmp);
230 
231 			if (r != LZO_E_OK || dst_len != tmp) {
232 				error("Compressed data violation");
233 				goto exit_2;
234 			}
235 		}
236 
237 		if (flush && flush(out_buf, dst_len) != dst_len)
238 			goto exit_2;
239 		if (output)
240 			out_buf += dst_len;
241 		if (posp)
242 			*posp += src_len + 12;
243 		if (fill) {
244 			in_buf = in_buf_save;
245 			fill(in_buf, lzo1x_worst_compress(LZO_BLOCK_SIZE));
246 		} else {
247 			in_buf += src_len;
248 			in_len -= src_len;
249 		}
250 	}
251 
252 	ret = 0;
253 exit_2:
254 	if (!input)
255 		free(in_buf_save);
256 exit_1:
257 	if (!output)
258 		free(out_buf);
259 exit:
260 	return ret;
261 }
262