2 * LZO decompressor for barebox. 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 <linux/types.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
)
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);
65 for (l
= 0; l
< 9; l
++) {
66 if (buf
[l
] != lzop_magic
[l
])
70 ret
= read(in_fd
, buf
, 4); /* version, lib_version */
73 version
= get_unaligned_be16(buf
);
75 if (version
>= 0x0940) {
76 ret
= read(in_fd
, buf
, 2); /* version to extract */
81 ret
= read(in_fd
, buf
, 1); /* method */
85 if (version
>= 0x0940)
86 read(in_fd
, buf
, 1); /* level */
88 ret
= read(in_fd
, buf
, 4); /* flags */
92 if (get_unaligned_be32(buf
) & HEADER_HAS_FILTER
) {
93 ret
= read(in_fd
, buf
, 4); /* skip filter info */
98 /* skip mode and mtime_low */
99 ret
= read(in_fd
, buf
, 8);
103 if (version
>= 0x0940) {
104 ret
= read(in_fd
, buf
, 4); /* skip mtime_high */
109 ret
= read(in_fd
, &l
, 1);
112 /* don't care about the file name, and skip checksum */
113 ret
= read(in_fd
, buf
, l
+ 4);
120 int unlzo(int in_fd
, int out_fd
, int *dest_len
)
123 u32 src_len
, dst_len
;
125 u8
*in_buf
, *out_buf
;
126 int obytes_processed
= 0;
127 unsigned char buf
[8];
130 if (parse_header(in_fd
))
133 out_buf
= xmalloc(LZO_BLOCK_SIZE
);
134 in_buf
= xmalloc(lzo1x_worst_compress(LZO_BLOCK_SIZE
));
137 /* read uncompressed block size */
138 ret
= read(in_fd
, buf
, 4);
141 dst_len
= get_unaligned_be32(buf
);
143 /* exit if last block */
147 if (dst_len
> LZO_BLOCK_SIZE
) {
148 printf("dest len longer than block size");
152 /* read compressed block size, and skip block checksum info */
153 ret
= read(in_fd
, buf
, 8);
157 src_len
= get_unaligned_be32(buf
);
159 if (src_len
<= 0 || src_len
> dst_len
) {
160 printf("file corrupted");
163 ret
= read(in_fd
, in_buf
, src_len
);
169 if (src_len
< dst_len
) {
170 r
= lzo1x_decompress_safe((u8
*) in_buf
, src_len
,
172 if (r
!= LZO_E_OK
|| dst_len
!= tmp
) {
173 printf("Compressed data violation");
176 ret
= write(out_fd
, out_buf
, dst_len
);
180 if (src_len
!= dst_len
) {
181 printf("Compressed data violation");
184 ret
= write(out_fd
, in_buf
, dst_len
);
189 obytes_processed
+= dst_len
;
196 *dest_len
= obytes_processed
;