omap: nand remove unnecessary condition
[barebox-mini2440.git] / lib / decompress_unlzo.c
blob8f9cdcf27b6392a0969d8e09443b52711e41b002
1 /*
2 * LZO decompressor for barebox. Code borrowed from the lzo
3 * implementation by Markus Franz Xaver Johannes Oberhumer.
5 * Linux kernel adaptation:
6 * Copyright (C) 2009
7 * Albin Tonnerre, Free Electrons <albin.tonnerre@free-electrons.com>
9 * Original code:
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/
33 #include <common.h>
34 #include <malloc.h>
35 #include <linux/types.h>
36 #include <lzo.h>
37 #include <errno.h>
38 #include <fs.h>
39 #include <xfuncs.h>
41 #include <linux/compiler.h>
42 #include <asm/unaligned.h>
44 static const unsigned char lzop_magic[] = {
45 0x89, 0x4c, 0x5a, 0x4f, 0x00, 0x0d, 0x0a, 0x1a, 0x0a };
47 #define LZO_BLOCK_SIZE (256*1024l)
48 #define HEADER_HAS_FILTER 0x00000800L
50 static inline int parse_header(int in_fd)
52 u8 l;
53 u16 version;
54 int ret;
55 unsigned char buf[256]; /* maximum filename length + 1 */
57 /* read magic (9), library version (2), 'need to be extracted'
58 * version (2) and method (1)
60 ret = read(in_fd, buf, 9);
61 if (ret < 0)
62 return ret;
64 /* check magic */
65 for (l = 0; l < 9; l++) {
66 if (buf[l] != lzop_magic[l])
67 return -EINVAL;
70 ret = read(in_fd, buf, 4); /* version, lib_version */
71 if (ret < 0)
72 return ret;
73 version = get_unaligned_be16(buf);
75 if (version >= 0x0940) {
76 ret = read(in_fd, buf, 2); /* version to extract */
77 if (ret < 0)
78 return ret;
81 ret = read(in_fd, buf, 1); /* method */
82 if (ret < 0)
83 return ret;
85 if (version >= 0x0940)
86 read(in_fd, buf, 1); /* level */
88 ret = read(in_fd, buf, 4); /* flags */
89 if (ret < 0)
90 return ret;
92 if (get_unaligned_be32(buf) & HEADER_HAS_FILTER) {
93 ret = read(in_fd, buf, 4); /* skip filter info */
94 if (ret < 0)
95 return ret;
98 /* skip mode and mtime_low */
99 ret = read(in_fd, buf, 8);
100 if (ret < 0)
101 return ret;
103 if (version >= 0x0940) {
104 ret = read(in_fd, buf, 4); /* skip mtime_high */
105 if (ret < 0)
106 return ret;
109 ret = read(in_fd, &l, 1);
110 if (ret < 0)
111 return ret;
112 /* don't care about the file name, and skip checksum */
113 ret = read(in_fd, buf, l + 4);
114 if (ret < 0)
115 return ret;
117 return 0;
120 int unlzo(int in_fd, int out_fd, int *dest_len)
122 u8 r = 0;
123 u32 src_len, dst_len;
124 size_t tmp;
125 u8 *in_buf, *out_buf;
126 int obytes_processed = 0;
127 unsigned char buf[8];
128 int ret;
130 if (parse_header(in_fd))
131 return -EINVAL;
133 out_buf = xmalloc(LZO_BLOCK_SIZE);
134 in_buf = xmalloc(lzo1x_worst_compress(LZO_BLOCK_SIZE));
136 for (;;) {
137 /* read uncompressed block size */
138 ret = read(in_fd, buf, 4);
139 if (ret < 0)
140 goto exit_free;
141 dst_len = get_unaligned_be32(buf);
143 /* exit if last block */
144 if (dst_len == 0)
145 break;
147 if (dst_len > LZO_BLOCK_SIZE) {
148 printf("dest len longer than block size");
149 goto exit_free;
152 /* read compressed block size, and skip block checksum info */
153 ret = read(in_fd, buf, 8);
154 if (ret < 0)
155 goto exit_free;
157 src_len = get_unaligned_be32(buf);
159 if (src_len <= 0 || src_len > dst_len) {
160 printf("file corrupted");
161 goto exit_free;
163 ret = read(in_fd, in_buf, src_len);
164 if (ret < 0)
165 goto exit_free;
167 /* decompress */
168 tmp = dst_len;
169 if (src_len < dst_len) {
170 r = lzo1x_decompress_safe((u8 *) in_buf, src_len,
171 out_buf, &tmp);
172 if (r != LZO_E_OK || dst_len != tmp) {
173 printf("Compressed data violation");
174 goto exit_free;
176 ret = write(out_fd, out_buf, dst_len);
177 if (ret < 0)
178 goto exit_free;
179 } else {
180 if (src_len != dst_len) {
181 printf("Compressed data violation");
182 goto exit_free;
184 ret = write(out_fd, in_buf, dst_len);
185 if (ret < 0)
186 goto exit_free;
189 obytes_processed += dst_len;
192 exit_free:
193 free(in_buf);
194 free(out_buf);
196 *dest_len = obytes_processed;
197 return 0;