f4c158e3a022aa1af35a6937190d5e0bc90e31ce
2 * LZO decompressor for the Linux kernel. Code borrowed from the lzo
3 * implementation by Markus Franz Xaver Johannes Oberhumer.
5 * Linux kernel adaptation:
7 * Albin Tonnerre, Free Electrons <albin.tonnerre@free-electrons.com>
10 * Copyright (C) 1996-2005 Markus Franz Xaver Johannes Oberhumer
11 * All Rights Reserved.
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.
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.
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.
28 * Markus F.X.J. Oberhumer
29 * <markus@oberhumer.com>
30 * http://www.oberhumer.com/opensource/lzop/
35 #include "lzo/lzo1x_decompress_safe.c"
37 #include <linux/decompress/unlzo.h>
40 #include <linux/types.h>
41 #include <linux/lzo.h>
42 #include <linux/decompress/mm.h>
44 #include <linux/compiler.h>
45 #include <asm/unaligned.h>
47 static const unsigned char lzop_magic
[] = {
48 0x89, 0x4c, 0x5a, 0x4f, 0x00, 0x0d, 0x0a, 0x1a, 0x0a };
50 #define LZO_BLOCK_SIZE (256*1024l)
51 #define HEADER_HAS_FILTER 0x00000800L
52 #define HEADER_SIZE_MIN (9 + 7 + 4 + 8 + 1 + 4)
53 #define HEADER_SIZE_MAX (9 + 7 + 1 + 8 + 8 + 4 + 1 + 255 + 4)
55 STATIC
inline long INIT
parse_header(u8
*input
, long *skip
, long in_len
)
59 u8
*end
= input
+ in_len
;
64 * Check that there's enough input to possibly have a valid header.
65 * Then it is possible to parse several fields until the minimum
66 * size may have been used.
68 if (in_len
< HEADER_SIZE_MIN
)
71 /* read magic: 9 first bits */
72 for (l
= 0; l
< 9; l
++) {
73 if (*parse
++ != lzop_magic
[l
])
76 /* get version (2bytes), skip library version (2),
77 * 'need to be extracted' version (2) and
79 version
= get_unaligned_be16(parse
);
81 if (version
>= 0x0940)
83 if (get_unaligned_be32(parse
) & HEADER_HAS_FILTER
)
84 parse
+= 8; /* flags + filter info */
86 parse
+= 4; /* flags */
89 * At least mode, mtime_low, filename length, and checksum must
90 * be left to be parsed. If also mtime_high is present, it's OK
91 * because the next input buffer check is after reading the
94 if (end
- parse
< 8 + 1 + 4)
97 /* skip mode and mtime_low */
99 if (version
>= 0x0940)
100 parse
+= 4; /* skip mtime_high */
103 /* don't care about the file name, and skip checksum */
104 if (end
- parse
< l
+ 4)
108 *skip
= parse
- input
;
112 STATIC
int INIT
unlzo(u8
*input
, long in_len
,
113 long (*fill
)(void *, unsigned long),
114 long (*flush
)(void *, unsigned long),
115 u8
*output
, long *posp
,
116 void (*error
) (char *x
))
120 u32 src_len
, dst_len
;
122 u8
*in_buf
, *in_buf_save
, *out_buf
;
128 error("NULL output pointer and no flush function provided");
131 out_buf
= malloc(LZO_BLOCK_SIZE
);
133 error("Could not allocate output buffer");
139 error("Both input pointer and fill function provided, don't know what to do");
144 error("NULL input pointer and missing fill function");
147 in_buf
= malloc(lzo1x_worst_compress(LZO_BLOCK_SIZE
));
149 error("Could not allocate input buffer");
153 in_buf_save
= in_buf
;
160 * Start from in_buf + HEADER_SIZE_MAX to make it possible
161 * to use memcpy() to copy the unused data to the beginning
162 * of the buffer. This way memmove() isn't needed which
163 * is missing from pre-boot environments of most archs.
165 in_buf
+= HEADER_SIZE_MAX
;
166 in_len
= fill(in_buf
, HEADER_SIZE_MAX
);
169 if (!parse_header(in_buf
, &skip
, in_len
)) {
170 error("invalid header");
177 /* Move the unused data to the beginning of the buffer. */
178 memcpy(in_buf_save
, in_buf
, in_len
);
179 in_buf
= in_buf_save
;
186 /* read uncompressed block size */
187 if (fill
&& in_len
< 4) {
188 skip
= fill(in_buf
+ in_len
, 4 - in_len
);
193 error("file corrupted");
196 dst_len
= get_unaligned_be32(in_buf
);
200 /* exit if last block */
207 if (dst_len
> LZO_BLOCK_SIZE
) {
208 error("dest len longer than block size");
212 /* read compressed block size, and skip block checksum info */
213 if (fill
&& in_len
< 8) {
214 skip
= fill(in_buf
+ in_len
, 8 - in_len
);
219 error("file corrupted");
222 src_len
= get_unaligned_be32(in_buf
);
226 if (src_len
<= 0 || src_len
> dst_len
) {
227 error("file corrupted");
232 if (fill
&& in_len
< src_len
) {
233 skip
= fill(in_buf
+ in_len
, src_len
- in_len
);
237 if (in_len
< src_len
) {
238 error("file corrupted");
243 /* When the input data is not compressed at all,
244 * lzo1x_decompress_safe will fail, so call memcpy()
246 if (unlikely(dst_len
== src_len
))
247 memcpy(out_buf
, in_buf
, src_len
);
249 r
= lzo1x_decompress_safe((u8
*) in_buf
, src_len
,
252 if (r
!= LZO_E_OK
|| dst_len
!= tmp
) {
253 error("Compressed data violation");
258 if (flush
&& flush(out_buf
, dst_len
) != dst_len
)
263 *posp
+= src_len
+ 12;
269 * If there happens to still be unused data left in
270 * in_buf, move it to the beginning of the buffer.
271 * Use a loop to avoid memmove() dependency.
274 for (skip
= 0; skip
< in_len
; ++skip
)
275 in_buf_save
[skip
] = in_buf
[skip
];
276 in_buf
= in_buf_save
;
292 STATIC
int INIT
__decompress(unsigned char *buf
, long len
,
293 long (*fill
)(void*, unsigned long),
294 long (*flush
)(void*, unsigned long),
295 unsigned char *out_buf
, long olen
,
297 void (*error
)(char *x
))
299 return unlzo(buf
, len
, fill
, flush
, out_buf
, pos
, error
);