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/
34 #include "lzo/lzo1x_decompress.c"
36 #include <linux/slab.h>
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
53 STATIC inline int INIT parse_header(u8 *input, u8 *skip)
60 /* read magic: 9 first bits */
61 for (l = 0; l < 9; l++) {
62 if (*parse++ != lzop_magic[l])
65 /* get version (2bytes), skip library version (2),
66 * 'need to be extracted' version (2) and
68 version = get_unaligned_be16(parse);
70 if (version >= 0x0940)
72 if (get_unaligned_be32(parse) & HEADER_HAS_FILTER)
73 parse += 8; /* flags + filter info */
75 parse += 4; /* flags */
77 /* skip mode and mtime_low */
79 if (version >= 0x0940)
80 parse += 4; /* skip mtime_high */
83 /* don't care about the file name, and skip checksum */
86 *skip = parse - input;
90 STATIC inline int INIT unlzo(u8 *input, int in_len,
91 int (*fill) (void *, unsigned int),
92 int (*flush) (void *, unsigned int),
93 u8 *output, int *posp,
94 void (*error_fn) (char *x))
99 u8 *in_buf, *in_buf_save, *out_buf;
100 int obytes_processed = 0;
102 set_error_fn(error_fn);
107 error("NULL output pointer and no flush function provided");
110 out_buf = malloc(LZO_BLOCK_SIZE);
112 error("Could not allocate output buffer");
118 error("Both input pointer and fill function provided, don't know what to do");
122 } else if (!fill || !posp) {
123 error("NULL input pointer and missing position pointer or fill function");
126 in_buf = malloc(lzo1x_worst_compress(LZO_BLOCK_SIZE));
128 error("Could not allocate input buffer");
132 in_buf_save = in_buf;
138 fill(in_buf, lzo1x_worst_compress(LZO_BLOCK_SIZE));
140 if (!parse_header(input, &skip)) {
141 error("invalid header");
150 /* read uncompressed block size */
151 dst_len = get_unaligned_be32(in_buf);
154 /* exit if last block */
161 if (dst_len > LZO_BLOCK_SIZE) {
162 error("dest len longer than block size");
166 /* read compressed block size, and skip block checksum info */
167 src_len = get_unaligned_be32(in_buf);
170 if (src_len <= 0 || src_len > dst_len) {
171 error("file corrupted");
177 r = lzo1x_decompress_safe((u8 *) in_buf, src_len,
180 if (r != LZO_E_OK || dst_len != tmp) {
181 error("Compressed data violation");
185 obytes_processed += dst_len;
187 flush(out_buf, dst_len);
191 *posp += src_len + 12;
193 in_buf = in_buf_save;
194 fill(in_buf, lzo1x_worst_compress(LZO_BLOCK_SIZE));
206 return obytes_processed;
209 #define decompress unlzo