blob: 855d9d30ec4577300a128bf5d17f4f5c380362af [file] [log] [blame]
Albin Tonnerre7dd65fe2010-01-08 14:42:42 -08001/*
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, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 *
28 * Markus F.X.J. Oberhumer
29 * <markus@oberhumer.com>
30 * http://www.oberhumer.com/opensource/lzop/
31 */
32
33#ifdef STATIC
34#include "lzo/lzo1x_decompress.c"
35#else
Albin Tonnerre7dd65fe2010-01-08 14:42:42 -080036#include <linux/decompress/unlzo.h>
37#endif
38
39#include <linux/types.h>
40#include <linux/lzo.h>
41#include <linux/decompress/mm.h>
42
43#include <linux/compiler.h>
44#include <asm/unaligned.h>
45
46static const unsigned char lzop_magic[] = {
47 0x89, 0x4c, 0x5a, 0x4f, 0x00, 0x0d, 0x0a, 0x1a, 0x0a };
48
49#define LZO_BLOCK_SIZE (256*1024l)
50#define HEADER_HAS_FILTER 0x00000800L
51
52STATIC inline int INIT parse_header(u8 *input, u8 *skip)
53{
54 int l;
55 u8 *parse = input;
56 u8 level = 0;
57 u16 version;
58
59 /* read magic: 9 first bits */
60 for (l = 0; l < 9; l++) {
61 if (*parse++ != lzop_magic[l])
62 return 0;
63 }
64 /* get version (2bytes), skip library version (2),
65 * 'need to be extracted' version (2) and
66 * method (1) */
67 version = get_unaligned_be16(parse);
68 parse += 7;
69 if (version >= 0x0940)
70 level = *parse++;
71 if (get_unaligned_be32(parse) & HEADER_HAS_FILTER)
72 parse += 8; /* flags + filter info */
73 else
74 parse += 4; /* flags */
75
76 /* skip mode and mtime_low */
77 parse += 8;
78 if (version >= 0x0940)
79 parse += 4; /* skip mtime_high */
80
81 l = *parse++;
82 /* don't care about the file name, and skip checksum */
83 parse += l + 4;
84
85 *skip = parse - input;
86 return 1;
87}
88
89STATIC inline int INIT unlzo(u8 *input, int in_len,
90 int (*fill) (void *, unsigned int),
91 int (*flush) (void *, unsigned int),
92 u8 *output, int *posp,
Lasse Collin93685ad22011-01-12 17:01:14 -080093 void (*error) (char *x))
Albin Tonnerre7dd65fe2010-01-08 14:42:42 -080094{
95 u8 skip = 0, r = 0;
96 u32 src_len, dst_len;
97 size_t tmp;
98 u8 *in_buf, *in_buf_save, *out_buf;
Albin Tonnerreccdb4002010-04-23 13:17:58 -040099 int ret = -1;
Albin Tonnerre7dd65fe2010-01-08 14:42:42 -0800100
Albin Tonnerre7dd65fe2010-01-08 14:42:42 -0800101 if (output) {
102 out_buf = output;
103 } else if (!flush) {
104 error("NULL output pointer and no flush function provided");
105 goto exit;
106 } else {
107 out_buf = malloc(LZO_BLOCK_SIZE);
108 if (!out_buf) {
109 error("Could not allocate output buffer");
110 goto exit;
111 }
112 }
113
114 if (input && fill) {
115 error("Both input pointer and fill function provided, don't know what to do");
116 goto exit_1;
117 } else if (input) {
118 in_buf = input;
119 } else if (!fill || !posp) {
120 error("NULL input pointer and missing position pointer or fill function");
121 goto exit_1;
122 } else {
123 in_buf = malloc(lzo1x_worst_compress(LZO_BLOCK_SIZE));
124 if (!in_buf) {
125 error("Could not allocate input buffer");
126 goto exit_1;
127 }
128 }
129 in_buf_save = in_buf;
130
131 if (posp)
132 *posp = 0;
133
134 if (fill)
135 fill(in_buf, lzo1x_worst_compress(LZO_BLOCK_SIZE));
136
137 if (!parse_header(input, &skip)) {
138 error("invalid header");
139 goto exit_2;
140 }
141 in_buf += skip;
142
143 if (posp)
144 *posp = skip;
145
146 for (;;) {
147 /* read uncompressed block size */
148 dst_len = get_unaligned_be32(in_buf);
149 in_buf += 4;
150
151 /* exit if last block */
152 if (dst_len == 0) {
153 if (posp)
154 *posp += 4;
155 break;
156 }
157
158 if (dst_len > LZO_BLOCK_SIZE) {
159 error("dest len longer than block size");
160 goto exit_2;
161 }
162
163 /* read compressed block size, and skip block checksum info */
164 src_len = get_unaligned_be32(in_buf);
165 in_buf += 8;
166
167 if (src_len <= 0 || src_len > dst_len) {
168 error("file corrupted");
169 goto exit_2;
170 }
171
172 /* decompress */
173 tmp = dst_len;
Albin Tonnerreccdb4002010-04-23 13:17:58 -0400174
175 /* When the input data is not compressed at all,
176 * lzo1x_decompress_safe will fail, so call memcpy()
177 * instead */
178 if (unlikely(dst_len == src_len))
179 memcpy(out_buf, in_buf, src_len);
180 else {
181 r = lzo1x_decompress_safe((u8 *) in_buf, src_len,
Albin Tonnerre7dd65fe2010-01-08 14:42:42 -0800182 out_buf, &tmp);
183
Albin Tonnerreccdb4002010-04-23 13:17:58 -0400184 if (r != LZO_E_OK || dst_len != tmp) {
185 error("Compressed data violation");
186 goto exit_2;
187 }
Albin Tonnerre7dd65fe2010-01-08 14:42:42 -0800188 }
189
Lasse Collin8f9b54a2011-01-12 17:01:20 -0800190 if (flush && flush(out_buf, dst_len) != dst_len)
191 goto exit_2;
Albin Tonnerre7dd65fe2010-01-08 14:42:42 -0800192 if (output)
193 out_buf += dst_len;
194 if (posp)
195 *posp += src_len + 12;
196 if (fill) {
197 in_buf = in_buf_save;
198 fill(in_buf, lzo1x_worst_compress(LZO_BLOCK_SIZE));
199 } else
200 in_buf += src_len;
201 }
202
Albin Tonnerreccdb4002010-04-23 13:17:58 -0400203 ret = 0;
Albin Tonnerre7dd65fe2010-01-08 14:42:42 -0800204exit_2:
205 if (!input)
206 free(in_buf);
207exit_1:
208 if (!output)
209 free(out_buf);
210exit:
Albin Tonnerreccdb4002010-04-23 13:17:58 -0400211 return ret;
Albin Tonnerre7dd65fe2010-01-08 14:42:42 -0800212}
213
214#define decompress unlzo