2 This file is part of the lzop file compressor.
4 Copyright (C) 1996..2003 Markus Franz Xaver Johannes Oberhumer
7 Markus F.X.J. Oberhumer <markus@oberhumer.com>
8 http://www.oberhumer.com/opensource/lzop/
10 lzop and the LZO library are free software; you can redistribute them
11 and/or modify them under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2 of
13 the License, or (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; see the file COPYING.
22 If not, write to the Free Software Foundation, Inc.,
23 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 "Minimalized" for busybox by Alain Knaff
32 //config: Lzop compression/decompresion.
34 //config:config LZOP_COMPR_HIGH
35 //config: bool "lzop compression levels 7,8,9 (not very useful)"
37 //config: depends on LZOP
39 //config: High levels (7,8,9) of lzop compression. These levels
40 //config: are actually slower than gzip at equivalent compression ratios
41 //config: and take up 3.2K of code.
43 //applet:IF_LZOP(APPLET(lzop, BB_DIR_BIN, BB_SUID_DROP))
44 //applet:IF_LZOP(APPLET_ODDNAME(lzopcat, lzop, BB_DIR_USR_BIN, BB_SUID_DROP, lzopcat))
45 //applet:IF_LZOP(APPLET_ODDNAME(unlzop, lzop, BB_DIR_USR_BIN, BB_SUID_DROP, unlzop))
46 //kbuild:lib-$(CONFIG_LZOP) += lzop.o
48 //usage:#define lzop_trivial_usage
49 //usage: "[-cfvd123456789CF] [FILE]..."
50 //usage:#define lzop_full_usage "\n\n"
51 //usage: " -1..9 Compression level"
52 //usage: "\n -d Decompress"
53 //usage: "\n -c Write to stdout"
54 //usage: "\n -f Force"
55 //usage: "\n -v Verbose"
56 //usage: "\n -F Don't store or verify checksum"
57 //usage: "\n -C Also write checksum of compressed block"
59 //usage:#define lzopcat_trivial_usage
60 //usage: "[-vCF] [FILE]..."
61 //usage:#define lzopcat_full_usage "\n\n"
62 //usage: " -v Verbose"
63 //usage: "\n -F Don't store or verify checksum"
65 //usage:#define unlzop_trivial_usage
66 //usage: "[-cfvCF] [FILE]..."
67 //usage:#define unlzop_full_usage "\n\n"
68 //usage: " -c Write to stdout"
69 //usage: "\n -f Force"
70 //usage: "\n -v Verbose"
71 //usage: "\n -F Don't store or verify checksum"
74 #include "common_bufsiz.h"
75 #include "bb_archive.h"
76 #include "liblzo_interface.h"
78 /* lzo-2.03/src/lzo_ptr.h */
79 #define pd(a,b) ((unsigned)((a)-(b)))
81 #define lzo_version() LZO_VERSION
82 #define lzo_sizeof_dict_t (sizeof(uint8_t*))
84 /* lzo-2.03/include/lzo/lzo1x.h */
85 #define LZO1X_1_MEM_COMPRESS (16384 * lzo_sizeof_dict_t)
86 #define LZO1X_1_15_MEM_COMPRESS (32768 * lzo_sizeof_dict_t)
87 #define LZO1X_999_MEM_COMPRESS (14 * 16384 * sizeof(short))
89 /* lzo-2.03/src/lzo1x_oo.c */
90 #define NO_LIT UINT_MAX
92 /**********************************************************************/
93 static void copy2(uint8_t* ip
, const uint8_t* m_pos
, unsigned off
)
102 static void copy3(uint8_t* ip
, const uint8_t* m_pos
, unsigned off
)
106 ip
[2] = ip
[1] = m_pos
[0];
118 /**********************************************************************/
119 // optimize a block of data.
120 /**********************************************************************/
121 #define TEST_IP (ip < ip_end)
122 #define TEST_OP (op <= op_end)
124 static NOINLINE
int lzo1x_optimize(uint8_t *in
, unsigned in_len
,
125 uint8_t *out
, unsigned *out_len
,
126 void* wrkmem UNUSED_PARAM
)
132 uint8_t* const ip_end
= in
+ in_len
;
133 uint8_t* const op_end
= out
+ *out_len
;
134 uint8_t* litp
= NULL
;
136 unsigned next_lit
= NO_LIT
;
138 unsigned long o_m1_a
= 0, o_m1_b
= 0, o_m2
= 0, o_m3_a
= 0, o_m3_b
= 0;
140 // LZO_UNUSED(wrkmem);
151 goto first_literal_run
;
154 while (TEST_IP
&& TEST_OP
) {
173 do *op
++ = *ip
++; while (--t
> 0);
180 m_pos
= op
- 1 - 0x800;
182 m_pos
= op
- 1 - 0x400;
195 if (t
< 16) { /* a M1 match */
204 /* test if a match follows */
205 if (nl
== 0 && lit
== 1 && ip
[0] >= 16) {
207 /* adjust length of previous short run */
209 *litp
= (unsigned char)((*litp
& ~3) | lit
);
210 /* copy over the 2 literals that replace the match */
211 copy2(ip
-2, m_pos
, pd(op
, m_pos
));
214 /* test if a literal run follows */
219 && (lit
+ 2 + ip
[0] < 16)
222 /* remove short run */
224 /* copy over the 2 literals that replace the match */
225 copy2(ip
-3+1, m_pos
, pd(op
, m_pos
));
226 /* move literals 1 byte ahead */
229 memmove(litp
+1, litp
, lit
);
230 /* insert new length of long literal run */
232 *litp
= (unsigned char)(lit
- 3);
237 goto copy_literal_run
;
244 if (t
>= 64) { /* a M2 match */
247 m_pos
-= (t
>> 2) & 7;
251 m_pos
-= (t
>> 2) & 3;
259 /* test if in beetween two long literal runs */
260 if (t
== 1 && lit
> 3 && nl
== 0
261 && ip
[0] < 16 && ip
[0] != 0 && (lit
+ 3 + ip
[0] < 16)
264 /* copy over the 3 literals that replace the match */
265 copy3(ip
-1-2, m_pos
, pd(op
, m_pos
));
266 /* set new length of previous literal run */
268 *litp
= (unsigned char)(lit
- 3);
273 goto copy_literal_run
;
276 if (t
>= 32) { /* a M3 match */
287 } else { /* a M4 match */
289 m_pos
-= (t
& 8) << 11;
307 /* test if in beetween two matches */
308 if (t
== 1 && lit
== 0 && nl
== 0 && ip
[0] >= 16) {
310 /* make a previous short run */
312 *litp
= (unsigned char)((*litp
& ~3) | lit
);
313 /* copy over the 3 literals that replace the match */
314 copy3(ip
-3, m_pos
, pd(op
, m_pos
));
317 /* test if a literal run follows */
318 else if (t
== 1 && lit
<= 3 && nl
== 0
319 && ip
[0] < 16 && ip
[0] != 0 && (lit
+ 3 + ip
[0] < 16)
322 /* remove short run */
324 /* copy over the 3 literals that replace the match */
325 copy3(ip
-4+1, m_pos
, pd(op
, m_pos
));
326 /* move literals 1 byte ahead */
329 memmove(litp
+1,litp
,lit
);
330 /* insert new length of long literal run */
332 *litp
= (unsigned char)(lit
- 3);
338 goto copy_literal_run
;
344 do *op
++ = *m_pos
++; while (--t
> 0);
348 if (next_lit
== NO_LIT
) {
360 do *op
++ = *ip
++; while (--t
> 0);
362 } while (TEST_IP
&& TEST_OP
);
365 /* no EOF code was found */
366 *out_len
= pd(op
, out
);
367 return LZO_E_EOF_NOT_FOUND
;
370 // LZO_UNUSED(o_m1_a); LZO_UNUSED(o_m1_b); LZO_UNUSED(o_m2);
371 // LZO_UNUSED(o_m3_a); LZO_UNUSED(o_m3_b);
372 *out_len
= pd(op
, out
);
373 return (ip
== ip_end
? LZO_E_OK
:
374 (ip
< ip_end
? LZO_E_INPUT_NOT_CONSUMED
: LZO_E_INPUT_OVERRUN
));
377 /**********************************************************************/
378 #define F_OS F_OS_UNIX
379 #define F_CS F_CS_NATIVE
381 /**********************************************************************/
382 #define ADLER32_INIT_VALUE 1
383 #define CRC32_INIT_VALUE 0
385 /**********************************************************************/
392 /**********************************************************************/
394 #define F_ADLER32_D 0x00000001L
395 #define F_ADLER32_C 0x00000002L
396 #define F_H_EXTRA_FIELD 0x00000040L
397 #define F_H_GMTDIFF 0x00000080L
398 #define F_CRC32_D 0x00000100L
399 #define F_CRC32_C 0x00000200L
400 #define F_H_FILTER 0x00000800L
401 #define F_H_CRC32 0x00001000L
402 #define F_MASK 0x00003FFFL
404 /* operating system & file system that created the file [mostly unused] */
405 #define F_OS_UNIX 0x03000000L
406 #define F_OS_SHIFT 24
407 #define F_OS_MASK 0xff000000L
409 /* character set for file name encoding [mostly unused] */
410 #define F_CS_NATIVE 0x00000000L
411 #define F_CS_SHIFT 20
412 #define F_CS_MASK 0x00f00000L
414 /* these bits must be zero */
415 #define F_RESERVED ((F_MASK | F_OS_MASK | F_CS_MASK) ^ 0xffffffffL)
417 typedef struct chksum_t
{
422 typedef struct header_t
{
424 unsigned lib_version
;
425 unsigned version_needed_to_extract
;
430 uint32_t header_checksum
;
432 uint32_t extra_field_len
;
433 uint32_t extra_field_checksum
;
435 unsigned char method
;
443 /*const uint32_t *lzo_crc32_table;*/
447 #define G (*(struct globals*)bb_common_bufsiz1)
448 #define INIT_G() do { setup_common_bufsiz(); } while (0)
449 //#define G (*ptr_to_globals)
450 //#define INIT_G() do {
451 // SET_PTR_TO_GLOBALS(xzalloc(sizeof(G)));
455 /**********************************************************************/
456 #define LZOP_VERSION 0x1010
457 //#define LZOP_VERSION_STRING "1.01"
458 //#define LZOP_VERSION_DATE "Apr 27th 2003"
460 #define OPTION_STRING "cfvqdt123456789CF"
462 /* Note: must be kept in sync with archival/bbunzip.c */
464 OPT_STDOUT
= (1 << 0),
465 OPT_FORCE
= (1 << 1),
466 OPT_VERBOSE
= (1 << 2),
467 OPT_QUIET
= (1 << 3),
468 OPT_DECOMPRESS
= (1 << 4),
483 /**********************************************************************/
485 // adapted from free code by Mark Adler <madler@alumni.caltech.edu>
486 // see http://www.zlib.org/
487 /**********************************************************************/
488 static FAST_FUNC
uint32_t
489 lzo_adler32(uint32_t adler
, const uint8_t* buf
, unsigned len
)
492 LZO_BASE
= 65521, /* largest prime smaller than 65536 */
493 /* NMAX is the largest n such that
494 * 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
497 uint32_t s1
= adler
& 0xffff;
498 uint32_t s2
= (adler
>> 16) & 0xffff;
505 k
= len
< LZO_NMAX
? (unsigned) len
: LZO_NMAX
;
514 return (s2
<< 16) | s1
;
517 static FAST_FUNC
uint32_t
518 lzo_crc32(uint32_t c
, const uint8_t* buf
, unsigned len
)
520 //if (buf == NULL) - impossible
523 return ~crc32_block_endian0(~c
, buf
, len
, global_crc32_table
);
526 /**********************************************************************/
527 static void init_chksum(chksum_t
*ct
)
529 ct
->f_adler32
= ADLER32_INIT_VALUE
;
530 ct
->f_crc32
= CRC32_INIT_VALUE
;
533 static void add_bytes_to_chksum(chksum_t
*ct
, const void* buf
, int cnt
)
535 /* We need to handle the two checksums at once, because at the
536 * beginning of the header, we don't know yet which one we'll
538 ct
->f_adler32
= lzo_adler32(ct
->f_adler32
, (const uint8_t*)buf
, cnt
);
539 ct
->f_crc32
= lzo_crc32(ct
->f_crc32
, (const uint8_t*)buf
, cnt
);
542 static uint32_t chksum_getresult(chksum_t
*ct
, const header_t
*h
)
544 return (h
->flags
& F_H_CRC32
) ? ct
->f_crc32
: ct
->f_adler32
;
547 /**********************************************************************/
548 static uint32_t read32(void)
555 static void write32(uint32_t v
)
561 static void f_write(const void* buf
, int cnt
)
564 add_bytes_to_chksum(&G
.chksum_out
, buf
, cnt
);
567 static void f_read(void* buf
, int cnt
)
570 add_bytes_to_chksum(&G
.chksum_in
, buf
, cnt
);
573 static int f_read8(void)
580 static void f_write8(uint8_t v
)
585 static unsigned f_read16(void)
592 static void f_write16(uint16_t v
)
598 static uint32_t f_read32(void)
605 static void f_write32(uint32_t v
)
611 /**********************************************************************/
612 static int lzo_get_method(header_t
*h
)
615 if (h
->method
== M_LZO1X_1
) {
618 } else if (h
->method
== M_LZO1X_1_15
) {
621 } else if (h
->method
== M_LZO1X_999
) {
625 return -1; /* not a LZO method */
627 /* check compression level */
628 if (h
->level
< 1 || h
->level
> 9)
634 /**********************************************************************/
635 #define LZO_BLOCK_SIZE (256 * 1024l)
636 #define MAX_BLOCK_SIZE (64 * 1024l * 1024l) /* DO NOT CHANGE */
638 /* LZO may expand uncompressible data by a small amount */
639 #define MAX_COMPRESSED_SIZE(x) ((x) + (x) / 16 + 64 + 3)
641 /**********************************************************************/
643 /**********************************************************************/
644 static NOINLINE
int lzo_compress(const header_t
*h
)
646 unsigned block_size
= LZO_BLOCK_SIZE
;
647 int r
= 0; /* LZO_E_OK */
648 uint8_t *const b1
= xzalloc(block_size
);
649 uint8_t *const b2
= xzalloc(MAX_COMPRESSED_SIZE(block_size
));
650 unsigned src_len
= 0, dst_len
= 0;
651 uint32_t d_adler32
= ADLER32_INIT_VALUE
;
652 uint32_t d_crc32
= CRC32_INIT_VALUE
;
654 uint8_t *wrk_mem
= NULL
;
656 if (h
->method
== M_LZO1X_1
)
657 wrk_mem
= xzalloc(LZO1X_1_MEM_COMPRESS
);
658 else if (h
->method
== M_LZO1X_1_15
)
659 wrk_mem
= xzalloc(LZO1X_1_15_MEM_COMPRESS
);
660 else if (h
->method
== M_LZO1X_999
)
661 wrk_mem
= xzalloc(LZO1X_999_MEM_COMPRESS
);
665 l
= full_read(0, b1
, block_size
);
666 src_len
= (l
> 0 ? l
: 0);
668 /* write uncompressed block size */
671 /* exit if last block */
675 /* compute checksum of uncompressed block */
676 if (h
->flags
& F_ADLER32_D
)
677 d_adler32
= lzo_adler32(ADLER32_INIT_VALUE
, b1
, src_len
);
678 if (h
->flags
& F_CRC32_D
)
679 d_crc32
= lzo_crc32(CRC32_INIT_VALUE
, b1
, src_len
);
682 if (h
->method
== M_LZO1X_1
)
683 r
= lzo1x_1_compress(b1
, src_len
, b2
, &dst_len
, wrk_mem
);
684 else if (h
->method
== M_LZO1X_1_15
)
685 r
= lzo1x_1_15_compress(b1
, src_len
, b2
, &dst_len
, wrk_mem
);
686 #if ENABLE_LZOP_COMPR_HIGH
687 else if (h
->method
== M_LZO1X_999
)
688 r
= lzo1x_999_compress_level(b1
, src_len
, b2
, &dst_len
,
692 bb_error_msg_and_die("internal error");
694 if (r
!= 0) /* not LZO_E_OK */
695 bb_error_msg_and_die("internal error - compression failed");
697 /* write compressed block size */
698 if (dst_len
< src_len
) {
700 if (h
->method
== M_LZO1X_999
) {
701 unsigned new_len
= src_len
;
702 r
= lzo1x_optimize(b2
, dst_len
, b1
, &new_len
, NULL
);
703 if (r
!= 0 /*LZO_E_OK*/ || new_len
!= src_len
)
704 bb_error_msg_and_die("internal error - optimization failed");
708 /* data actually expanded => store data uncompressed */
712 /* write checksum of uncompressed block */
713 if (h
->flags
& F_ADLER32_D
)
715 if (h
->flags
& F_CRC32_D
)
718 if (dst_len
< src_len
) {
719 /* write checksum of compressed block */
720 if (h
->flags
& F_ADLER32_C
)
721 write32(lzo_adler32(ADLER32_INIT_VALUE
, b2
, dst_len
));
722 if (h
->flags
& F_CRC32_C
)
723 write32(lzo_crc32(CRC32_INIT_VALUE
, b2
, dst_len
));
724 /* write compressed block data */
725 xwrite(1, b2
, dst_len
);
727 /* write uncompressed block data */
728 xwrite(1, b1
, src_len
);
738 static FAST_FUNC
void lzo_check(
740 uint8_t* buf
, unsigned len
,
741 uint32_t FAST_FUNC (*fn
)(uint32_t, const uint8_t*, unsigned),
744 /* This function, by having the same order of parameters
745 * as fn, and by being marked FAST_FUNC (same as fn),
746 * saves a dozen bytes of code.
748 uint32_t c
= fn(init
, buf
, len
);
750 bb_error_msg_and_die("checksum error");
753 /**********************************************************************/
755 /**********************************************************************/
756 static NOINLINE
int lzo_decompress(const header_t
*h
)
758 unsigned block_size
= LZO_BLOCK_SIZE
;
760 uint32_t src_len
, dst_len
;
761 uint32_t c_adler32
= ADLER32_INIT_VALUE
;
762 uint32_t d_adler32
= ADLER32_INIT_VALUE
;
763 uint32_t c_crc32
= CRC32_INIT_VALUE
, d_crc32
= CRC32_INIT_VALUE
;
765 uint32_t mcs_block_size
= MAX_COMPRESSED_SIZE(block_size
);
771 /* read uncompressed block size */
774 /* exit if last block */
778 /* error if split file */
779 if (dst_len
== 0xffffffffL
)
780 /* should not happen - not yet implemented */
781 bb_error_msg_and_die("this file is a split lzop file");
783 if (dst_len
> MAX_BLOCK_SIZE
)
784 bb_error_msg_and_die("corrupted data");
786 /* read compressed block size */
788 if (src_len
<= 0 || src_len
> dst_len
)
789 bb_error_msg_and_die("corrupted data");
791 if (dst_len
> block_size
) {
796 block_size
= dst_len
;
797 mcs_block_size
= MAX_COMPRESSED_SIZE(block_size
);
800 /* read checksum of uncompressed block */
801 if (h
->flags
& F_ADLER32_D
)
802 d_adler32
= read32();
803 if (h
->flags
& F_CRC32_D
)
806 /* read checksum of compressed block */
807 if (src_len
< dst_len
) {
808 if (h
->flags
& F_ADLER32_C
)
809 c_adler32
= read32();
810 if (h
->flags
& F_CRC32_C
)
815 b2
= xzalloc(mcs_block_size
);
816 /* read the block into the end of our buffer */
817 b1
= b2
+ mcs_block_size
- src_len
;
818 xread(0, b1
, src_len
);
820 if (src_len
< dst_len
) {
821 unsigned d
= dst_len
;
823 if (!(option_mask32
& OPT_F
)) {
824 /* verify checksum of compressed block */
825 if (h
->flags
& F_ADLER32_C
)
826 lzo_check(ADLER32_INIT_VALUE
,
828 lzo_adler32
, c_adler32
);
829 if (h
->flags
& F_CRC32_C
)
830 lzo_check(CRC32_INIT_VALUE
,
836 // if (option_mask32 & OPT_F)
837 // r = lzo1x_decompress(b1, src_len, b2, &d, NULL);
839 r
= lzo1x_decompress_safe(b1
, src_len
, b2
, &d
, NULL
);
841 if (r
!= 0 /*LZO_E_OK*/ || dst_len
!= d
) {
842 bb_error_msg_and_die("corrupted data");
846 /* "stored" block => no decompression */
850 if (!(option_mask32
& OPT_F
)) {
851 /* verify checksum of uncompressed block */
852 if (h
->flags
& F_ADLER32_D
)
853 lzo_check(ADLER32_INIT_VALUE
,
855 lzo_adler32
, d_adler32
);
856 if (h
->flags
& F_CRC32_D
)
857 lzo_check(CRC32_INIT_VALUE
,
862 /* write uncompressed block data */
863 xwrite(1, dst
, dst_len
);
870 /**********************************************************************/
871 // lzop file signature (shamelessly borrowed from PNG)
872 /**********************************************************************/
874 * The first nine bytes of a lzop file always contain the following values:
877 * --- --- --- --- --- --- --- --- ---
878 * (hex) 89 4c 5a 4f 00 0d 0a 1a 0a
879 * (decimal) 137 76 90 79 0 13 10 26 10
880 * (C notation - ASCII) \211 L Z O \0 \r \n \032 \n
883 /* (vda) comparison with lzop v1.02rc1 ("lzop -1 <FILE" cmd):
884 * Only slight differences in header:
885 * -00000000 89 4c 5a 4f 00 0d 0a 1a 0a 10 20 20 20 09 40 02
886 * +00000000 89 4c 5a 4f 00 0d 0a 1a 0a 10 10 20 30 09 40 02
888 * version lib_version
889 * -00000010 01 03 00 00 0d 00 00 81 a4 49 f7 a6 3f 00 00 00
890 * +00000010 01 03 00 00 01 00 00 00 00 00 00 00 00 00 00 00
891 * ^^^^^^^^^^^ ^^^^^^^^^^^ ^^^^^^^^^^^
893 * -00000020 00 00 2d 67 04 17 00 04 00 00 00 03 ed ec 9d 6d
894 * +00000020 00 00 10 5f 00 c1 00 04 00 00 00 03 ed ec 9d 6d
897 * The rest is identical.
899 static const unsigned char lzop_magic
[9] ALIGN1
= {
900 0x89, 0x4c, 0x5a, 0x4f, 0x00, 0x0d, 0x0a, 0x1a, 0x0a
903 /* This coding is derived from Alexander Lehmann's pngcheck code. */
904 static void check_magic(void)
906 unsigned char magic
[sizeof(lzop_magic
)];
907 xread(0, magic
, sizeof(magic
));
908 if (memcmp(magic
, lzop_magic
, sizeof(lzop_magic
)) != 0)
909 bb_error_msg_and_die("bad magic number");
912 /**********************************************************************/
914 /**********************************************************************/
915 static void write_header(const header_t
*h
)
919 xwrite(1, lzop_magic
, sizeof(lzop_magic
));
921 init_chksum(&G
.chksum_out
);
923 f_write16(h
->version
);
924 f_write16(h
->lib_version
);
925 f_write16(h
->version_needed_to_extract
);
931 f_write32(h
->gmtdiff
);
933 l
= (int) strlen(h
->name
);
938 f_write32(chksum_getresult(&G
.chksum_out
, h
));
941 static int read_header(header_t
*h
)
947 memset(h
, 0, sizeof(*h
));
948 h
->version_needed_to_extract
= 0x0900; /* first lzop version */
951 init_chksum(&G
.chksum_in
);
953 h
->version
= f_read16();
954 if (h
->version
< 0x0900)
956 h
->lib_version
= f_read16();
957 if (h
->version
>= 0x0940) {
958 h
->version_needed_to_extract
= f_read16();
959 if (h
->version_needed_to_extract
> LZOP_VERSION
)
961 if (h
->version_needed_to_extract
< 0x0900)
964 h
->method
= f_read8();
965 if (h
->version
>= 0x0940)
966 h
->level
= f_read8();
967 h
->flags
= f_read32();
968 if (h
->flags
& F_H_FILTER
)
969 return 16; /* filter not supported */
970 h
->mode
= f_read32();
971 h
->mtime
= f_read32();
972 if (h
->version
>= 0x0940)
973 h
->gmtdiff
= f_read32();
980 checksum
= chksum_getresult(&G
.chksum_in
, h
);
981 h
->header_checksum
= f_read32();
982 if (h
->header_checksum
!= checksum
)
987 r
= lzo_get_method(h
);
991 /* check reserved flags */
992 if (h
->flags
& F_RESERVED
)
995 /* skip extra field [not used yet] */
996 if (h
->flags
& F_H_EXTRA_FIELD
) {
999 /* note: the checksum also covers the length */
1000 init_chksum(&G
.chksum_in
);
1001 h
->extra_field_len
= f_read32();
1002 for (k
= 0; k
< h
->extra_field_len
; k
++)
1004 checksum
= chksum_getresult(&G
.chksum_in
, h
);
1005 h
->extra_field_checksum
= f_read32();
1006 if (h
->extra_field_checksum
!= checksum
)
1013 static void p_header(header_t
*h
)
1020 bb_error_msg_and_die("header_error %d", r
);
1023 /**********************************************************************/
1025 /**********************************************************************/
1026 static void lzo_set_method(header_t
*h
)
1030 if (option_mask32
& OPT_1
) {
1031 h
->method
= M_LZO1X_1_15
;
1032 } else if (option_mask32
& OPT_789
) {
1033 #if ENABLE_LZOP_COMPR_HIGH
1034 h
->method
= M_LZO1X_999
;
1035 if (option_mask32
& OPT_7
)
1037 else if (option_mask32
& OPT_8
)
1042 bb_error_msg_and_die("high compression not compiled in");
1044 } else { /* levels 2..6 or none (defaults to level 3) */
1045 h
->method
= M_LZO1X_1
;
1046 level
= 5; /* levels 2-6 are actually the same */
1052 static int do_lzo_compress(void)
1057 memset(h
, 0, sizeof(*h
));
1061 h
->version
= (LZOP_VERSION
& 0xffff);
1062 h
->version_needed_to_extract
= 0x0940;
1063 h
->lib_version
= lzo_version() & 0xffff;
1065 h
->flags
= (F_OS
& F_OS_MASK
) | (F_CS
& F_CS_MASK
);
1067 if (!(option_mask32
& OPT_F
) || h
->method
== M_LZO1X_999
) {
1068 h
->flags
|= F_ADLER32_D
;
1069 if (option_mask32
& OPT_C
)
1070 h
->flags
|= F_ADLER32_C
;
1073 return lzo_compress(h
);
1077 /**********************************************************************/
1079 /**********************************************************************/
1080 static int do_lzo_decompress(void)
1086 return lzo_decompress(&header
);
1089 static char* FAST_FUNC
make_new_name_lzop(char *filename
, const char *expected_ext UNUSED_PARAM
)
1091 if (option_mask32
& OPT_DECOMPRESS
) {
1092 char *extension
= strrchr(filename
, '.');
1093 if (!extension
|| strcmp(extension
+ 1, "lzo") != 0)
1094 return xasprintf("%s.out", filename
);
1098 return xasprintf("%s.lzo", filename
);
1101 static IF_DESKTOP(long long) int FAST_FUNC
pack_lzop(transformer_state_t
*xstate UNUSED_PARAM
)
1103 if (option_mask32
& OPT_DECOMPRESS
)
1104 return do_lzo_decompress();
1105 return do_lzo_compress();
1108 int lzop_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
1109 int lzop_main(int argc UNUSED_PARAM
, char **argv
)
1111 getopt32(argv
, OPTION_STRING
);
1114 if (applet_name
[4] == 'c')
1115 option_mask32
|= (OPT_STDOUT
| OPT_DECOMPRESS
);
1117 if (applet_name
[4] == 'o')
1118 option_mask32
|= OPT_DECOMPRESS
;
1120 global_crc32_table
= crc32_filltable(NULL
, 0);
1121 return bbunpack(argv
, pack_lzop
, make_new_name_lzop
, /*unused:*/ NULL
);