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
29 //config: bool "lzop (13 kb)"
32 //config: Lzop compression/decompresion.
34 //config:config UNLZOP
35 //config: bool "unlzop (13 kb)"
36 //config: default n # INCOMPAT: upstream lzop does not provide such tool
38 //config: Lzop decompresion.
40 //config:config LZOPCAT
41 //config: bool "lzopcat (13 kb)"
42 //config: default n # INCOMPAT: upstream lzop does not provide such tool
44 //config: Alias to "lzop -dc".
46 //config:config LZOP_COMPR_HIGH
47 //config: bool "lzop compression levels 7,8,9 (not very useful)"
49 //config: depends on LZOP || UNLZOP || LZOPCAT
51 //config: High levels (7,8,9) of lzop compression. These levels
52 //config: are actually slower than gzip at equivalent compression ratios
53 //config: and take up 3.2K of code.
55 //applet:IF_LZOP(APPLET(lzop, BB_DIR_BIN, BB_SUID_DROP))
56 // APPLET_ODDNAME:name main location suid_type help
57 //applet:IF_UNLZOP( APPLET_ODDNAME(unlzop, lzop, BB_DIR_USR_BIN, BB_SUID_DROP, unlzop))
58 //applet:IF_LZOPCAT(APPLET_ODDNAME(lzopcat, lzop, BB_DIR_USR_BIN, BB_SUID_DROP, lzopcat))
59 //kbuild:lib-$(CONFIG_LZOP) += lzop.o
60 //kbuild:lib-$(CONFIG_UNLZOP) += lzop.o
61 //kbuild:lib-$(CONFIG_LZOPCAT) += lzop.o
63 //usage:#define lzop_trivial_usage
64 //usage: "[-cfUvd123456789CF] [FILE]..."
65 //usage:#define lzop_full_usage "\n\n"
66 //usage: " -1..9 Compression level"
67 //usage: "\n -d Decompress"
68 //usage: "\n -c Write to stdout"
69 //usage: "\n -f Force"
70 //usage: "\n -U Delete input files"
71 ///////: "\n -k Keep input files" (default, so why bother documenting?)
72 //usage: "\n -v Verbose"
73 //usage: "\n -F Don't store or verify checksum"
74 //usage: "\n -C Also write checksum of compressed block"
76 //usage:#define lzopcat_trivial_usage
77 //usage: "[-vF] [FILE]..."
78 //usage:#define lzopcat_full_usage "\n\n"
79 //usage: " -v Verbose"
80 //usage: "\n -F Don't verify checksum"
82 //usage:#define unlzop_trivial_usage
83 //usage: "[-cfkvF] [FILE]..."
84 //usage:#define unlzop_full_usage "\n\n"
85 //usage: " -c Write to stdout"
86 //usage: "\n -f Force"
87 //usage: "\n -U Delete input files"
88 ///////: "\n -k Keep input files" (default, so why bother documenting?)
89 //usage: "\n -v Verbose"
90 //usage: "\n -F Don't verify checksum"
93 #include "common_bufsiz.h"
94 #include "bb_archive.h"
95 #include "liblzo_interface.h"
97 /* lzo-2.03/src/lzo_ptr.h */
98 #define pd(a,b) ((unsigned)((a)-(b)))
100 #define lzo_version() LZO_VERSION
101 #define lzo_sizeof_dict_t (sizeof(uint8_t*))
103 /* lzo-2.03/include/lzo/lzo1x.h */
104 #define LZO1X_1_MEM_COMPRESS (16384 * lzo_sizeof_dict_t)
105 #define LZO1X_1_15_MEM_COMPRESS (32768 * lzo_sizeof_dict_t)
106 #define LZO1X_999_MEM_COMPRESS (14 * 16384 * sizeof(short))
108 /* lzo-2.03/src/lzo1x_oo.c */
109 #define NO_LIT UINT_MAX
111 /**********************************************************************/
112 static void copy2(uint8_t* ip, const uint8_t* m_pos, unsigned off)
121 static void copy3(uint8_t* ip, const uint8_t* m_pos, unsigned off)
125 ip[2] = ip[1] = m_pos[0];
137 /**********************************************************************/
138 // optimize a block of data.
139 /**********************************************************************/
140 #define TEST_IP (ip < ip_end)
141 #define TEST_OP (op <= op_end)
143 static NOINLINE int lzo1x_optimize(uint8_t *in, unsigned in_len,
144 uint8_t *out, unsigned *out_len,
145 void* wrkmem UNUSED_PARAM)
151 uint8_t* const ip_end = in + in_len;
152 uint8_t* const op_end = out + *out_len;
153 uint8_t* litp = NULL;
155 unsigned next_lit = NO_LIT;
157 unsigned long o_m1_a = 0, o_m1_b = 0, o_m2 = 0, o_m3_a = 0, o_m3_b = 0;
159 // LZO_UNUSED(wrkmem);
170 goto first_literal_run;
173 while (TEST_IP && TEST_OP) {
192 do *op++ = *ip++; while (--t > 0);
199 m_pos = op - 1 - 0x800;
201 m_pos = op - 1 - 0x400;
214 if (t < 16) { /* a M1 match */
223 /* test if a match follows */
224 if (nl == 0 && lit == 1 && ip[0] >= 16) {
226 /* adjust length of previous short run */
228 *litp = (unsigned char)((*litp & ~3) | lit);
229 /* copy over the 2 literals that replace the match */
230 copy2(ip-2, m_pos, pd(op, m_pos));
233 /* test if a literal run follows */
238 && (lit + 2 + ip[0] < 16)
241 /* remove short run */
243 /* copy over the 2 literals that replace the match */
244 copy2(ip-3+1, m_pos, pd(op, m_pos));
245 /* move literals 1 byte ahead */
248 memmove(litp+1, litp, lit);
249 /* insert new length of long literal run */
251 *litp = (unsigned char)(lit - 3);
256 goto copy_literal_run;
263 if (t >= 64) { /* a M2 match */
266 m_pos -= (t >> 2) & 7;
270 m_pos -= (t >> 2) & 3;
278 /* test if in beetween two long literal runs */
279 if (t == 1 && lit > 3 && nl == 0
280 && ip[0] < 16 && ip[0] != 0 && (lit + 3 + ip[0] < 16)
283 /* copy over the 3 literals that replace the match */
284 copy3(ip-1-2, m_pos, pd(op, m_pos));
285 /* set new length of previous literal run */
287 *litp = (unsigned char)(lit - 3);
292 goto copy_literal_run;
295 if (t >= 32) { /* a M3 match */
306 } else { /* a M4 match */
308 m_pos -= (t & 8) << 11;
326 /* test if in beetween two matches */
327 if (t == 1 && lit == 0 && nl == 0 && ip[0] >= 16) {
329 /* make a previous short run */
331 *litp = (unsigned char)((*litp & ~3) | lit);
332 /* copy over the 3 literals that replace the match */
333 copy3(ip-3, m_pos, pd(op, m_pos));
336 /* test if a literal run follows */
337 else if (t == 1 && lit <= 3 && nl == 0
338 && ip[0] < 16 && ip[0] != 0 && (lit + 3 + ip[0] < 16)
341 /* remove short run */
343 /* copy over the 3 literals that replace the match */
344 copy3(ip-4+1, m_pos, pd(op, m_pos));
345 /* move literals 1 byte ahead */
348 memmove(litp+1,litp,lit);
349 /* insert new length of long literal run */
351 *litp = (unsigned char)(lit - 3);
357 goto copy_literal_run;
363 do *op++ = *m_pos++; while (--t > 0);
367 if (next_lit == NO_LIT) {
379 do *op++ = *ip++; while (--t > 0);
381 } while (TEST_IP && TEST_OP);
384 /* no EOF code was found */
385 *out_len = pd(op, out);
386 return LZO_E_EOF_NOT_FOUND;
389 // LZO_UNUSED(o_m1_a); LZO_UNUSED(o_m1_b); LZO_UNUSED(o_m2);
390 // LZO_UNUSED(o_m3_a); LZO_UNUSED(o_m3_b);
391 *out_len = pd(op, out);
392 return (ip == ip_end ? LZO_E_OK :
393 (ip < ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
396 /**********************************************************************/
397 #define F_OS F_OS_UNIX
398 #define F_CS F_CS_NATIVE
400 /**********************************************************************/
401 #define ADLER32_INIT_VALUE 1
402 #define CRC32_INIT_VALUE 0
404 /**********************************************************************/
411 /**********************************************************************/
413 #define F_ADLER32_D 0x00000001L
414 #define F_ADLER32_C 0x00000002L
415 #define F_H_EXTRA_FIELD 0x00000040L
416 #define F_H_GMTDIFF 0x00000080L
417 #define F_CRC32_D 0x00000100L
418 #define F_CRC32_C 0x00000200L
419 #define F_H_FILTER 0x00000800L
420 #define F_H_CRC32 0x00001000L
421 #define F_MASK 0x00003FFFL
423 /* operating system & file system that created the file [mostly unused] */
424 #define F_OS_UNIX 0x03000000L
425 #define F_OS_SHIFT 24
426 #define F_OS_MASK 0xff000000L
428 /* character set for file name encoding [mostly unused] */
429 #define F_CS_NATIVE 0x00000000L
430 #define F_CS_SHIFT 20
431 #define F_CS_MASK 0x00f00000L
433 /* these bits must be zero */
434 #define F_RESERVED ((F_MASK | F_OS_MASK | F_CS_MASK) ^ 0xffffffffL)
436 typedef struct chksum_t {
441 typedef struct header_t {
443 unsigned lib_version;
444 unsigned version_needed_to_extract;
449 uint32_t header_checksum;
451 uint32_t extra_field_len;
452 uint32_t extra_field_checksum;
454 unsigned char method;
462 /*const uint32_t *lzo_crc32_table;*/
466 #define G (*(struct globals*)bb_common_bufsiz1)
467 #define INIT_G() do { setup_common_bufsiz(); } while (0)
468 //#define G (*ptr_to_globals)
469 //#define INIT_G() do {
470 // SET_PTR_TO_GLOBALS(xzalloc(sizeof(G)));
474 /**********************************************************************/
475 #define LZOP_VERSION 0x1010
476 //#define LZOP_VERSION_STRING "1.01"
477 //#define LZOP_VERSION_DATE "Apr 27th 2003"
479 // lzop wants to be weird:
480 // unlike all other compressosrs, its -k "keep" option is the default,
481 // and -U is used to delete the source. We will invert the bit after getopt().
482 #define OPTION_STRING "cfUvqdt123456789CFk"
484 /* Note: must be kept in sync with archival/bbunzip.c */
486 OPT_STDOUT = (1 << 0),
487 OPT_FORCE = (1 << 1),
489 OPT_VERBOSE = (1 << 3),
490 OPT_QUIET = (1 << 4),
491 OPT_DECOMPRESS = (1 << 5),
505 OPT_789 = OPT_7 | OPT_8 | OPT_9
508 /**********************************************************************/
510 // adapted from free code by Mark Adler <madler@alumni.caltech.edu>
511 // see http://www.zlib.org/
512 /**********************************************************************/
513 static FAST_FUNC uint32_t
514 lzo_adler32(uint32_t adler, const uint8_t* buf, unsigned len)
517 LZO_BASE = 65521, /* largest prime smaller than 65536 */
518 /* NMAX is the largest n such that
519 * 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
522 uint32_t s1 = adler & 0xffff;
523 uint32_t s2 = (adler >> 16) & 0xffff;
530 k = len < LZO_NMAX ? (unsigned) len : LZO_NMAX;
539 return (s2 << 16) | s1;
542 static FAST_FUNC uint32_t
543 lzo_crc32(uint32_t c, const uint8_t* buf, unsigned len)
545 //if (buf == NULL) - impossible
548 return ~crc32_block_endian0(~c, buf, len, global_crc32_table);
551 /**********************************************************************/
552 static void init_chksum(chksum_t *ct)
554 ct->f_adler32 = ADLER32_INIT_VALUE;
555 ct->f_crc32 = CRC32_INIT_VALUE;
558 static void add_bytes_to_chksum(chksum_t *ct, const void* buf, int cnt)
560 /* We need to handle the two checksums at once, because at the
561 * beginning of the header, we don't know yet which one we'll
563 ct->f_adler32 = lzo_adler32(ct->f_adler32, (const uint8_t*)buf, cnt);
564 ct->f_crc32 = lzo_crc32(ct->f_crc32, (const uint8_t*)buf, cnt);
567 static uint32_t chksum_getresult(chksum_t *ct, const header_t *h)
569 return (h->flags & F_H_CRC32) ? ct->f_crc32 : ct->f_adler32;
572 /**********************************************************************/
573 static uint32_t read32(void)
580 static void write32(uint32_t v)
586 static void f_write(const void* buf, int cnt)
589 add_bytes_to_chksum(&G.chksum_out, buf, cnt);
592 static void f_read(void* buf, int cnt)
595 add_bytes_to_chksum(&G.chksum_in, buf, cnt);
598 static int f_read8(void)
605 static void f_write8(uint8_t v)
610 static unsigned f_read16(void)
617 static void f_write16(uint16_t v)
623 static uint32_t f_read32(void)
630 static void f_write32(uint32_t v)
636 /**********************************************************************/
637 static int lzo_get_method(header_t *h)
640 if (h->method == M_LZO1X_1) {
643 } else if (h->method == M_LZO1X_1_15) {
646 } else if (h->method == M_LZO1X_999) {
650 return -1; /* not a LZO method */
652 /* check compression level */
653 if (h->level < 1 || h->level > 9)
659 /**********************************************************************/
660 #define LZO_BLOCK_SIZE (256 * 1024l)
661 #define MAX_BLOCK_SIZE (64 * 1024l * 1024l) /* DO NOT CHANGE */
663 /* LZO may expand uncompressible data by a small amount */
664 #define MAX_COMPRESSED_SIZE(x) ((x) + (x) / 16 + 64 + 3)
666 /**********************************************************************/
668 /**********************************************************************/
669 static NOINLINE int lzo_compress(const header_t *h)
671 unsigned block_size = LZO_BLOCK_SIZE;
672 int r = 0; /* LZO_E_OK */
673 uint8_t *const b1 = xzalloc(block_size);
674 uint8_t *const b2 = xzalloc(MAX_COMPRESSED_SIZE(block_size));
675 unsigned src_len = 0, dst_len = 0;
676 uint32_t d_adler32 = ADLER32_INIT_VALUE;
677 uint32_t d_crc32 = CRC32_INIT_VALUE;
679 uint8_t *wrk_mem = NULL;
681 if (h->method == M_LZO1X_1)
682 wrk_mem = xzalloc(LZO1X_1_MEM_COMPRESS);
683 else if (h->method == M_LZO1X_1_15)
684 wrk_mem = xzalloc(LZO1X_1_15_MEM_COMPRESS);
685 else if (h->method == M_LZO1X_999)
686 wrk_mem = xzalloc(LZO1X_999_MEM_COMPRESS);
690 l = full_read(0, b1, block_size);
691 src_len = (l > 0 ? l : 0);
693 /* write uncompressed block size */
696 /* exit if last block */
700 /* compute checksum of uncompressed block */
701 if (h->flags & F_ADLER32_D)
702 d_adler32 = lzo_adler32(ADLER32_INIT_VALUE, b1, src_len);
703 if (h->flags & F_CRC32_D)
704 d_crc32 = lzo_crc32(CRC32_INIT_VALUE, b1, src_len);
707 if (h->method == M_LZO1X_1)
708 r = lzo1x_1_compress(b1, src_len, b2, &dst_len, wrk_mem);
709 else if (h->method == M_LZO1X_1_15)
710 r = lzo1x_1_15_compress(b1, src_len, b2, &dst_len, wrk_mem);
711 #if ENABLE_LZOP_COMPR_HIGH
712 else if (h->method == M_LZO1X_999)
713 r = lzo1x_999_compress_level(b1, src_len, b2, &dst_len,
717 bb_error_msg_and_die("internal error");
719 if (r != 0) /* not LZO_E_OK */
720 bb_error_msg_and_die("internal error - compression failed");
722 /* write compressed block size */
723 if (dst_len < src_len) {
725 if (h->method == M_LZO1X_999) {
726 unsigned new_len = src_len;
727 r = lzo1x_optimize(b2, dst_len, b1, &new_len, NULL);
728 if (r != 0 /*LZO_E_OK*/ || new_len != src_len)
729 bb_error_msg_and_die("internal error - optimization failed");
733 /* data actually expanded => store data uncompressed */
737 /* write checksum of uncompressed block */
738 if (h->flags & F_ADLER32_D)
740 if (h->flags & F_CRC32_D)
743 if (dst_len < src_len) {
744 /* write checksum of compressed block */
745 if (h->flags & F_ADLER32_C)
746 write32(lzo_adler32(ADLER32_INIT_VALUE, b2, dst_len));
747 if (h->flags & F_CRC32_C)
748 write32(lzo_crc32(CRC32_INIT_VALUE, b2, dst_len));
749 /* write compressed block data */
750 xwrite(1, b2, dst_len);
752 /* write uncompressed block data */
753 xwrite(1, b1, src_len);
763 static FAST_FUNC void lzo_check(
765 uint8_t* buf, unsigned len,
766 uint32_t FAST_FUNC (*fn)(uint32_t, const uint8_t*, unsigned),
769 /* This function, by having the same order of parameters
770 * as fn, and by being marked FAST_FUNC (same as fn),
771 * saves a dozen bytes of code.
773 uint32_t c = fn(init, buf, len);
775 bb_error_msg_and_die("checksum error");
778 /**********************************************************************/
780 /**********************************************************************/
781 static NOINLINE int lzo_decompress(const header_t *h)
783 unsigned block_size = LZO_BLOCK_SIZE;
785 uint32_t src_len, dst_len;
786 uint32_t c_adler32 = ADLER32_INIT_VALUE;
787 uint32_t d_adler32 = ADLER32_INIT_VALUE;
788 uint32_t c_crc32 = CRC32_INIT_VALUE, d_crc32 = CRC32_INIT_VALUE;
790 uint32_t mcs_block_size = MAX_COMPRESSED_SIZE(block_size);
796 /* read uncompressed block size */
799 /* exit if last block */
803 /* error if split file */
804 if (dst_len == 0xffffffffL)
805 /* should not happen - not yet implemented */
806 bb_error_msg_and_die("this file is a split lzop file");
808 if (dst_len > MAX_BLOCK_SIZE)
809 bb_error_msg_and_die("corrupted data");
811 /* read compressed block size */
813 if (src_len <= 0 || src_len > dst_len)
814 bb_error_msg_and_die("corrupted data");
816 if (dst_len > block_size) {
821 block_size = dst_len;
822 mcs_block_size = MAX_COMPRESSED_SIZE(block_size);
825 /* read checksum of uncompressed block */
826 if (h->flags & F_ADLER32_D)
827 d_adler32 = read32();
828 if (h->flags & F_CRC32_D)
831 /* read checksum of compressed block */
832 if (src_len < dst_len) {
833 if (h->flags & F_ADLER32_C)
834 c_adler32 = read32();
835 if (h->flags & F_CRC32_C)
840 b2 = xzalloc(mcs_block_size);
841 /* read the block into the end of our buffer */
842 b1 = b2 + mcs_block_size - src_len;
843 xread(0, b1, src_len);
845 if (src_len < dst_len) {
846 unsigned d = dst_len;
848 if (!(option_mask32 & OPT_F)) {
849 /* verify checksum of compressed block */
850 if (h->flags & F_ADLER32_C)
851 lzo_check(ADLER32_INIT_VALUE,
853 lzo_adler32, c_adler32);
854 if (h->flags & F_CRC32_C)
855 lzo_check(CRC32_INIT_VALUE,
861 // if (option_mask32 & OPT_F)
862 // r = lzo1x_decompress(b1, src_len, b2, &d, NULL);
864 r = lzo1x_decompress_safe(b1, src_len, b2, &d, NULL);
866 if (r != 0 /*LZO_E_OK*/ || dst_len != d) {
867 bb_error_msg_and_die("corrupted data");
871 /* "stored" block => no decompression */
875 if (!(option_mask32 & OPT_F)) {
876 /* verify checksum of uncompressed block */
877 if (h->flags & F_ADLER32_D)
878 lzo_check(ADLER32_INIT_VALUE,
880 lzo_adler32, d_adler32);
881 if (h->flags & F_CRC32_D)
882 lzo_check(CRC32_INIT_VALUE,
887 /* write uncompressed block data */
888 xwrite(1, dst, dst_len);
895 /**********************************************************************/
896 // lzop file signature (shamelessly borrowed from PNG)
897 /**********************************************************************/
899 * The first nine bytes of a lzop file always contain the following values:
902 * --- --- --- --- --- --- --- --- ---
903 * (hex) 89 4c 5a 4f 00 0d 0a 1a 0a
904 * (decimal) 137 76 90 79 0 13 10 26 10
905 * (C notation - ASCII) \211 L Z O \0 \r \n \032 \n
908 /* (vda) comparison with lzop v1.02rc1 ("lzop -1 <FILE" cmd):
909 * Only slight differences in header:
910 * -00000000 89 4c 5a 4f 00 0d 0a 1a 0a 10 20 20 20 09 40 02
911 * +00000000 89 4c 5a 4f 00 0d 0a 1a 0a 10 10 20 30 09 40 02
913 * version lib_version
914 * -00000010 01 03 00 00 0d 00 00 81 a4 49 f7 a6 3f 00 00 00
915 * +00000010 01 03 00 00 01 00 00 00 00 00 00 00 00 00 00 00
916 * ^^^^^^^^^^^ ^^^^^^^^^^^ ^^^^^^^^^^^
918 * -00000020 00 00 2d 67 04 17 00 04 00 00 00 03 ed ec 9d 6d
919 * +00000020 00 00 10 5f 00 c1 00 04 00 00 00 03 ed ec 9d 6d
922 * The rest is identical.
924 static const unsigned char lzop_magic[9] ALIGN1 = {
925 0x89, 0x4c, 0x5a, 0x4f, 0x00, 0x0d, 0x0a, 0x1a, 0x0a
928 /* This coding is derived from Alexander Lehmann's pngcheck code. */
929 static void check_magic(void)
931 unsigned char magic[sizeof(lzop_magic)];
932 xread(0, magic, sizeof(magic));
933 if (memcmp(magic, lzop_magic, sizeof(lzop_magic)) != 0)
934 bb_error_msg_and_die("bad magic number");
937 /**********************************************************************/
939 /**********************************************************************/
940 static void write_header(const header_t *h)
944 xwrite(1, lzop_magic, sizeof(lzop_magic));
946 init_chksum(&G.chksum_out);
948 f_write16(h->version);
949 f_write16(h->lib_version);
950 f_write16(h->version_needed_to_extract);
956 f_write32(h->gmtdiff);
958 l = (int) strlen(h->name);
963 f_write32(chksum_getresult(&G.chksum_out, h));
966 static int read_header(header_t *h)
972 memset(h, 0, sizeof(*h));
973 h->version_needed_to_extract = 0x0900; /* first lzop version */
976 init_chksum(&G.chksum_in);
978 h->version = f_read16();
979 if (h->version < 0x0900)
981 h->lib_version = f_read16();
982 if (h->version >= 0x0940) {
983 h->version_needed_to_extract = f_read16();
984 if (h->version_needed_to_extract > LZOP_VERSION)
986 if (h->version_needed_to_extract < 0x0900)
989 h->method = f_read8();
990 if (h->version >= 0x0940)
991 h->level = f_read8();
992 h->flags = f_read32();
993 if (h->flags & F_H_FILTER)
994 return 16; /* filter not supported */
995 h->mode = f_read32();
996 h->mtime = f_read32();
997 if (h->version >= 0x0940)
998 h->gmtdiff = f_read32();
1005 checksum = chksum_getresult(&G.chksum_in, h);
1006 h->header_checksum = f_read32();
1007 if (h->header_checksum != checksum)
1012 r = lzo_get_method(h);
1016 /* check reserved flags */
1017 if (h->flags & F_RESERVED)
1020 /* skip extra field [not used yet] */
1021 if (h->flags & F_H_EXTRA_FIELD) {
1024 /* note: the checksum also covers the length */
1025 init_chksum(&G.chksum_in);
1026 h->extra_field_len = f_read32();
1027 for (k = 0; k < h->extra_field_len; k++)
1029 checksum = chksum_getresult(&G.chksum_in, h);
1030 h->extra_field_checksum = f_read32();
1031 if (h->extra_field_checksum != checksum)
1038 static void p_header(header_t *h)
1045 bb_error_msg_and_die("header_error %d", r);
1048 /**********************************************************************/
1050 /**********************************************************************/
1051 static void lzo_set_method(header_t *h)
1055 if (option_mask32 & OPT_1) {
1056 h->method = M_LZO1X_1_15;
1057 } else if (option_mask32 & OPT_789) {
1058 #if ENABLE_LZOP_COMPR_HIGH
1059 h->method = M_LZO1X_999;
1060 if (option_mask32 & OPT_7)
1062 else if (option_mask32 & OPT_8)
1067 bb_error_msg_and_die("high compression not compiled in");
1069 } else { /* levels 2..6 or none (defaults to level 3) */
1070 h->method = M_LZO1X_1;
1071 level = 5; /* levels 2-6 are actually the same */
1077 static int do_lzo_compress(void)
1082 memset(h, 0, sizeof(*h));
1086 h->version = (LZOP_VERSION & 0xffff);
1087 h->version_needed_to_extract = 0x0940;
1088 h->lib_version = lzo_version() & 0xffff;
1090 h->flags = (F_OS & F_OS_MASK) | (F_CS & F_CS_MASK);
1092 if (!(option_mask32 & OPT_F) || h->method == M_LZO1X_999) {
1093 h->flags |= F_ADLER32_D;
1094 if (option_mask32 & OPT_C)
1095 h->flags |= F_ADLER32_C;
1098 return lzo_compress(h);
1102 /**********************************************************************/
1104 /**********************************************************************/
1105 static int do_lzo_decompress(void)
1111 return lzo_decompress(&header);
1114 static char* FAST_FUNC make_new_name_lzop(char *filename, const char *expected_ext UNUSED_PARAM)
1116 if (option_mask32 & OPT_DECOMPRESS) {
1117 char *extension = strrchr(filename, '.');
1118 if (!extension || strcmp(extension + 1, "lzo") != 0)
1119 return xasprintf("%s.out", filename);
1123 return xasprintf("%s.lzo", filename);
1126 static IF_DESKTOP(long long) int FAST_FUNC pack_lzop(transformer_state_t *xstate UNUSED_PARAM)
1128 if (option_mask32 & OPT_DECOMPRESS)
1129 return do_lzo_decompress();
1130 return do_lzo_compress();
1133 int lzop_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1134 int lzop_main(int argc UNUSED_PARAM, char **argv)
1136 getopt32(argv, OPTION_STRING);
1138 /* -U is "anti -k", invert bit for bbunpack(): */
1139 option_mask32 ^= OPT_KEEP;
1140 /* -k disables -U (if any): */
1141 /* opt_complementary "k-U"? - nope, only handles -Uk, not -kU */
1142 if (option_mask32 & OPT_k)
1143 option_mask32 |= OPT_KEEP;
1146 if (ENABLE_LZOPCAT && applet_name[4] == 'c')
1147 option_mask32 |= (OPT_STDOUT | OPT_DECOMPRESS);
1149 if (ENABLE_UNLZOP && applet_name[4] == 'o')
1150 option_mask32 |= OPT_DECOMPRESS;
1152 global_crc32_table = crc32_filltable(NULL, 0);
1153 return bbunpack(argv, pack_lzop, make_new_name_lzop, /*unused:*/ NULL);