]>
Commit | Line | Data |
---|---|---|
7dd65feb AT |
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, 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 | |
36 | #include <linux/slab.h> | |
37 | #include <linux/decompress/unlzo.h> | |
38 | #endif | |
39 | ||
40 | #include <linux/types.h> | |
41 | #include <linux/lzo.h> | |
42 | #include <linux/decompress/mm.h> | |
43 | ||
44 | #include <linux/compiler.h> | |
45 | #include <asm/unaligned.h> | |
46 | ||
47 | static const unsigned char lzop_magic[] = { | |
48 | 0x89, 0x4c, 0x5a, 0x4f, 0x00, 0x0d, 0x0a, 0x1a, 0x0a }; | |
49 | ||
50 | #define LZO_BLOCK_SIZE (256*1024l) | |
51 | #define HEADER_HAS_FILTER 0x00000800L | |
52 | ||
53 | STATIC inline int INIT parse_header(u8 *input, u8 *skip) | |
54 | { | |
55 | int l; | |
56 | u8 *parse = input; | |
57 | u8 level = 0; | |
58 | u16 version; | |
59 | ||
60 | /* read magic: 9 first bits */ | |
61 | for (l = 0; l < 9; l++) { | |
62 | if (*parse++ != lzop_magic[l]) | |
63 | return 0; | |
64 | } | |
65 | /* get version (2bytes), skip library version (2), | |
66 | * 'need to be extracted' version (2) and | |
67 | * method (1) */ | |
68 | version = get_unaligned_be16(parse); | |
69 | parse += 7; | |
70 | if (version >= 0x0940) | |
71 | level = *parse++; | |
72 | if (get_unaligned_be32(parse) & HEADER_HAS_FILTER) | |
73 | parse += 8; /* flags + filter info */ | |
74 | else | |
75 | parse += 4; /* flags */ | |
76 | ||
77 | /* skip mode and mtime_low */ | |
78 | parse += 8; | |
79 | if (version >= 0x0940) | |
80 | parse += 4; /* skip mtime_high */ | |
81 | ||
82 | l = *parse++; | |
83 | /* don't care about the file name, and skip checksum */ | |
84 | parse += l + 4; | |
85 | ||
86 | *skip = parse - input; | |
87 | return 1; | |
88 | } | |
89 | ||
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, | |
93685ad2 | 94 | void (*error) (char *x)) |
7dd65feb AT |
95 | { |
96 | u8 skip = 0, r = 0; | |
97 | u32 src_len, dst_len; | |
98 | size_t tmp; | |
99 | u8 *in_buf, *in_buf_save, *out_buf; | |
ccdb4004 | 100 | int ret = -1; |
7dd65feb | 101 | |
7dd65feb AT |
102 | if (output) { |
103 | out_buf = output; | |
104 | } else if (!flush) { | |
105 | error("NULL output pointer and no flush function provided"); | |
106 | goto exit; | |
107 | } else { | |
108 | out_buf = malloc(LZO_BLOCK_SIZE); | |
109 | if (!out_buf) { | |
110 | error("Could not allocate output buffer"); | |
111 | goto exit; | |
112 | } | |
113 | } | |
114 | ||
115 | if (input && fill) { | |
116 | error("Both input pointer and fill function provided, don't know what to do"); | |
117 | goto exit_1; | |
118 | } else if (input) { | |
119 | in_buf = input; | |
120 | } else if (!fill || !posp) { | |
121 | error("NULL input pointer and missing position pointer or fill function"); | |
122 | goto exit_1; | |
123 | } else { | |
124 | in_buf = malloc(lzo1x_worst_compress(LZO_BLOCK_SIZE)); | |
125 | if (!in_buf) { | |
126 | error("Could not allocate input buffer"); | |
127 | goto exit_1; | |
128 | } | |
129 | } | |
130 | in_buf_save = in_buf; | |
131 | ||
132 | if (posp) | |
133 | *posp = 0; | |
134 | ||
135 | if (fill) | |
136 | fill(in_buf, lzo1x_worst_compress(LZO_BLOCK_SIZE)); | |
137 | ||
138 | if (!parse_header(input, &skip)) { | |
139 | error("invalid header"); | |
140 | goto exit_2; | |
141 | } | |
142 | in_buf += skip; | |
143 | ||
144 | if (posp) | |
145 | *posp = skip; | |
146 | ||
147 | for (;;) { | |
148 | /* read uncompressed block size */ | |
149 | dst_len = get_unaligned_be32(in_buf); | |
150 | in_buf += 4; | |
151 | ||
152 | /* exit if last block */ | |
153 | if (dst_len == 0) { | |
154 | if (posp) | |
155 | *posp += 4; | |
156 | break; | |
157 | } | |
158 | ||
159 | if (dst_len > LZO_BLOCK_SIZE) { | |
160 | error("dest len longer than block size"); | |
161 | goto exit_2; | |
162 | } | |
163 | ||
164 | /* read compressed block size, and skip block checksum info */ | |
165 | src_len = get_unaligned_be32(in_buf); | |
166 | in_buf += 8; | |
167 | ||
168 | if (src_len <= 0 || src_len > dst_len) { | |
169 | error("file corrupted"); | |
170 | goto exit_2; | |
171 | } | |
172 | ||
173 | /* decompress */ | |
174 | tmp = dst_len; | |
ccdb4004 AT |
175 | |
176 | /* When the input data is not compressed at all, | |
177 | * lzo1x_decompress_safe will fail, so call memcpy() | |
178 | * instead */ | |
179 | if (unlikely(dst_len == src_len)) | |
180 | memcpy(out_buf, in_buf, src_len); | |
181 | else { | |
182 | r = lzo1x_decompress_safe((u8 *) in_buf, src_len, | |
7dd65feb AT |
183 | out_buf, &tmp); |
184 | ||
ccdb4004 AT |
185 | if (r != LZO_E_OK || dst_len != tmp) { |
186 | error("Compressed data violation"); | |
187 | goto exit_2; | |
188 | } | |
7dd65feb AT |
189 | } |
190 | ||
7dd65feb AT |
191 | if (flush) |
192 | flush(out_buf, dst_len); | |
193 | if (output) | |
194 | out_buf += dst_len; | |
195 | if (posp) | |
196 | *posp += src_len + 12; | |
197 | if (fill) { | |
198 | in_buf = in_buf_save; | |
199 | fill(in_buf, lzo1x_worst_compress(LZO_BLOCK_SIZE)); | |
200 | } else | |
201 | in_buf += src_len; | |
202 | } | |
203 | ||
ccdb4004 | 204 | ret = 0; |
7dd65feb AT |
205 | exit_2: |
206 | if (!input) | |
207 | free(in_buf); | |
208 | exit_1: | |
209 | if (!output) | |
210 | free(out_buf); | |
211 | exit: | |
ccdb4004 | 212 | return ret; |
7dd65feb AT |
213 | } |
214 | ||
215 | #define decompress unlzo |