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 UNLZOP
35 //config: bool "unlzop"
38 //config: Lzop decompresion.
40 //config:config LZOPCAT
41 //config: bool "lzopcat"
44 //config: Alias to "unlzop -c".
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:IF_UNLZOP(APPLET_ODDNAME(unlzop, lzop, BB_DIR_USR_BIN, BB_SUID_DROP, unlzop))
57 //applet:IF_LZOPCAT(APPLET_ODDNAME(lzopcat, lzop, BB_DIR_USR_BIN, BB_SUID_DROP, lzopcat))
58 //kbuild:lib-$(CONFIG_LZOP) += lzop.o
59 //kbuild:lib-$(CONFIG_UNLZOP) += lzop.o
60 //kbuild:lib-$(CONFIG_LZOPCAT) += lzop.o
62 //usage:#define lzop_trivial_usage
63 //usage: "[-cfvd123456789CF] [FILE]..."
64 //usage:#define lzop_full_usage "\n\n"
65 //usage: " -1..9 Compression level"
66 //usage: "\n -d Decompress"
67 //usage: "\n -c Write to stdout"
68 //usage: "\n -f Force"
69 //usage: "\n -v Verbose"
70 //usage: "\n -F Don't store or verify checksum"
71 //usage: "\n -C Also write checksum of compressed block"
73 //usage:#define lzopcat_trivial_usage
74 //usage: "[-vF] [FILE]..."
75 //usage:#define lzopcat_full_usage "\n\n"
76 //usage: " -v Verbose"
77 //usage: "\n -F Don't verify checksum"
79 //usage:#define unlzop_trivial_usage
80 //usage: "[-cfvF] [FILE]..."
81 //usage:#define unlzop_full_usage "\n\n"
82 //usage: " -c Write to stdout"
83 //usage: "\n -f Force"
84 //usage: "\n -v Verbose"
85 //usage: "\n -F Don't verify checksum"
88 #include "common_bufsiz.h"
89 #include "bb_archive.h"
90 #include "liblzo_interface.h"
92 /* lzo-2.03/src/lzo_ptr.h */
93 #define pd(a,b) ((unsigned)((a)-(b)))
95 #define lzo_version() LZO_VERSION
96 #define lzo_sizeof_dict_t (sizeof(uint8_t*))
98 /* lzo-2.03/include/lzo/lzo1x.h */
99 #define LZO1X_1_MEM_COMPRESS (16384 * lzo_sizeof_dict_t)
100 #define LZO1X_1_15_MEM_COMPRESS (32768 * lzo_sizeof_dict_t)
101 #define LZO1X_999_MEM_COMPRESS (14 * 16384 * sizeof(short))
103 /* lzo-2.03/src/lzo1x_oo.c */
104 #define NO_LIT UINT_MAX
106 /**********************************************************************/
107 static void copy2(uint8_t* ip, const uint8_t* m_pos, unsigned off)
116 static void copy3(uint8_t* ip, const uint8_t* m_pos, unsigned off)
120 ip[2] = ip[1] = m_pos[0];
132 /**********************************************************************/
133 // optimize a block of data.
134 /**********************************************************************/
135 #define TEST_IP (ip < ip_end)
136 #define TEST_OP (op <= op_end)
138 static NOINLINE int lzo1x_optimize(uint8_t *in, unsigned in_len,
139 uint8_t *out, unsigned *out_len,
140 void* wrkmem UNUSED_PARAM)
146 uint8_t* const ip_end = in + in_len;
147 uint8_t* const op_end = out + *out_len;
148 uint8_t* litp = NULL;
150 unsigned next_lit = NO_LIT;
152 unsigned long o_m1_a = 0, o_m1_b = 0, o_m2 = 0, o_m3_a = 0, o_m3_b = 0;
154 // LZO_UNUSED(wrkmem);
165 goto first_literal_run;
168 while (TEST_IP && TEST_OP) {
187 do *op++ = *ip++; while (--t > 0);
194 m_pos = op - 1 - 0x800;
196 m_pos = op - 1 - 0x400;
209 if (t < 16) { /* a M1 match */
218 /* test if a match follows */
219 if (nl == 0 && lit == 1 && ip[0] >= 16) {
221 /* adjust length of previous short run */
223 *litp = (unsigned char)((*litp & ~3) | lit);
224 /* copy over the 2 literals that replace the match */
225 copy2(ip-2, m_pos, pd(op, m_pos));
228 /* test if a literal run follows */
233 && (lit + 2 + ip[0] < 16)
236 /* remove short run */
238 /* copy over the 2 literals that replace the match */
239 copy2(ip-3+1, m_pos, pd(op, m_pos));
240 /* move literals 1 byte ahead */
243 memmove(litp+1, litp, lit);
244 /* insert new length of long literal run */
246 *litp = (unsigned char)(lit - 3);
251 goto copy_literal_run;
258 if (t >= 64) { /* a M2 match */
261 m_pos -= (t >> 2) & 7;
265 m_pos -= (t >> 2) & 3;
273 /* test if in beetween two long literal runs */
274 if (t == 1 && lit > 3 && nl == 0
275 && ip[0] < 16 && ip[0] != 0 && (lit + 3 + ip[0] < 16)
278 /* copy over the 3 literals that replace the match */
279 copy3(ip-1-2, m_pos, pd(op, m_pos));
280 /* set new length of previous literal run */
282 *litp = (unsigned char)(lit - 3);
287 goto copy_literal_run;
290 if (t >= 32) { /* a M3 match */
301 } else { /* a M4 match */
303 m_pos -= (t & 8) << 11;
321 /* test if in beetween two matches */
322 if (t == 1 && lit == 0 && nl == 0 && ip[0] >= 16) {
324 /* make a previous short run */
326 *litp = (unsigned char)((*litp & ~3) | lit);
327 /* copy over the 3 literals that replace the match */
328 copy3(ip-3, m_pos, pd(op, m_pos));
331 /* test if a literal run follows */
332 else if (t == 1 && lit <= 3 && nl == 0
333 && ip[0] < 16 && ip[0] != 0 && (lit + 3 + ip[0] < 16)
336 /* remove short run */
338 /* copy over the 3 literals that replace the match */
339 copy3(ip-4+1, m_pos, pd(op, m_pos));
340 /* move literals 1 byte ahead */
343 memmove(litp+1,litp,lit);
344 /* insert new length of long literal run */
346 *litp = (unsigned char)(lit - 3);
352 goto copy_literal_run;
358 do *op++ = *m_pos++; while (--t > 0);
362 if (next_lit == NO_LIT) {
374 do *op++ = *ip++; while (--t > 0);
376 } while (TEST_IP && TEST_OP);
379 /* no EOF code was found */
380 *out_len = pd(op, out);
381 return LZO_E_EOF_NOT_FOUND;
384 // LZO_UNUSED(o_m1_a); LZO_UNUSED(o_m1_b); LZO_UNUSED(o_m2);
385 // LZO_UNUSED(o_m3_a); LZO_UNUSED(o_m3_b);
386 *out_len = pd(op, out);
387 return (ip == ip_end ? LZO_E_OK :
388 (ip < ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
391 /**********************************************************************/
392 #define F_OS F_OS_UNIX
393 #define F_CS F_CS_NATIVE
395 /**********************************************************************/
396 #define ADLER32_INIT_VALUE 1
397 #define CRC32_INIT_VALUE 0
399 /**********************************************************************/
406 /**********************************************************************/
408 #define F_ADLER32_D 0x00000001L
409 #define F_ADLER32_C 0x00000002L
410 #define F_H_EXTRA_FIELD 0x00000040L
411 #define F_H_GMTDIFF 0x00000080L
412 #define F_CRC32_D 0x00000100L
413 #define F_CRC32_C 0x00000200L
414 #define F_H_FILTER 0x00000800L
415 #define F_H_CRC32 0x00001000L
416 #define F_MASK 0x00003FFFL
418 /* operating system & file system that created the file [mostly unused] */
419 #define F_OS_UNIX 0x03000000L
420 #define F_OS_SHIFT 24
421 #define F_OS_MASK 0xff000000L
423 /* character set for file name encoding [mostly unused] */
424 #define F_CS_NATIVE 0x00000000L
425 #define F_CS_SHIFT 20
426 #define F_CS_MASK 0x00f00000L
428 /* these bits must be zero */
429 #define F_RESERVED ((F_MASK | F_OS_MASK | F_CS_MASK) ^ 0xffffffffL)
431 typedef struct chksum_t {
436 typedef struct header_t {
438 unsigned lib_version;
439 unsigned version_needed_to_extract;
444 uint32_t header_checksum;
446 uint32_t extra_field_len;
447 uint32_t extra_field_checksum;
449 unsigned char method;
457 /*const uint32_t *lzo_crc32_table;*/
461 #define G (*(struct globals*)bb_common_bufsiz1)
462 #define INIT_G() do { setup_common_bufsiz(); } while (0)
463 //#define G (*ptr_to_globals)
464 //#define INIT_G() do {
465 // SET_PTR_TO_GLOBALS(xzalloc(sizeof(G)));
469 /**********************************************************************/
470 #define LZOP_VERSION 0x1010
471 //#define LZOP_VERSION_STRING "1.01"
472 //#define LZOP_VERSION_DATE "Apr 27th 2003"
474 #define OPTION_STRING "cfvqdt123456789CF"
476 /* Note: must be kept in sync with archival/bbunzip.c */
478 OPT_STDOUT = (1 << 0),
479 OPT_FORCE = (1 << 1),
480 OPT_VERBOSE = (1 << 2),
481 OPT_QUIET = (1 << 3),
482 OPT_DECOMPRESS = (1 << 4),
497 /**********************************************************************/
499 // adapted from free code by Mark Adler <madler@alumni.caltech.edu>
500 // see http://www.zlib.org/
501 /**********************************************************************/
502 static FAST_FUNC uint32_t
503 lzo_adler32(uint32_t adler, const uint8_t* buf, unsigned len)
506 LZO_BASE = 65521, /* largest prime smaller than 65536 */
507 /* NMAX is the largest n such that
508 * 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
511 uint32_t s1 = adler & 0xffff;
512 uint32_t s2 = (adler >> 16) & 0xffff;
519 k = len < LZO_NMAX ? (unsigned) len : LZO_NMAX;
528 return (s2 << 16) | s1;
531 static FAST_FUNC uint32_t
532 lzo_crc32(uint32_t c, const uint8_t* buf, unsigned len)
534 //if (buf == NULL) - impossible
537 return ~crc32_block_endian0(~c, buf, len, global_crc32_table);
540 /**********************************************************************/
541 static void init_chksum(chksum_t *ct)
543 ct->f_adler32 = ADLER32_INIT_VALUE;
544 ct->f_crc32 = CRC32_INIT_VALUE;
547 static void add_bytes_to_chksum(chksum_t *ct, const void* buf, int cnt)
549 /* We need to handle the two checksums at once, because at the
550 * beginning of the header, we don't know yet which one we'll
552 ct->f_adler32 = lzo_adler32(ct->f_adler32, (const uint8_t*)buf, cnt);
553 ct->f_crc32 = lzo_crc32(ct->f_crc32, (const uint8_t*)buf, cnt);
556 static uint32_t chksum_getresult(chksum_t *ct, const header_t *h)
558 return (h->flags & F_H_CRC32) ? ct->f_crc32 : ct->f_adler32;
561 /**********************************************************************/
562 static uint32_t read32(void)
569 static void write32(uint32_t v)
575 static void f_write(const void* buf, int cnt)
578 add_bytes_to_chksum(&G.chksum_out, buf, cnt);
581 static void f_read(void* buf, int cnt)
584 add_bytes_to_chksum(&G.chksum_in, buf, cnt);
587 static int f_read8(void)
594 static void f_write8(uint8_t v)
599 static unsigned f_read16(void)
606 static void f_write16(uint16_t v)
612 static uint32_t f_read32(void)
619 static void f_write32(uint32_t v)
625 /**********************************************************************/
626 static int lzo_get_method(header_t *h)
629 if (h->method == M_LZO1X_1) {
632 } else if (h->method == M_LZO1X_1_15) {
635 } else if (h->method == M_LZO1X_999) {
639 return -1; /* not a LZO method */
641 /* check compression level */
642 if (h->level < 1 || h->level > 9)
648 /**********************************************************************/
649 #define LZO_BLOCK_SIZE (256 * 1024l)
650 #define MAX_BLOCK_SIZE (64 * 1024l * 1024l) /* DO NOT CHANGE */
652 /* LZO may expand uncompressible data by a small amount */
653 #define MAX_COMPRESSED_SIZE(x) ((x) + (x) / 16 + 64 + 3)
655 /**********************************************************************/
657 /**********************************************************************/
658 static NOINLINE int lzo_compress(const header_t *h)
660 unsigned block_size = LZO_BLOCK_SIZE;
661 int r = 0; /* LZO_E_OK */
662 uint8_t *const b1 = xzalloc(block_size);
663 uint8_t *const b2 = xzalloc(MAX_COMPRESSED_SIZE(block_size));
664 unsigned src_len = 0, dst_len = 0;
665 uint32_t d_adler32 = ADLER32_INIT_VALUE;
666 uint32_t d_crc32 = CRC32_INIT_VALUE;
668 uint8_t *wrk_mem = NULL;
670 if (h->method == M_LZO1X_1)
671 wrk_mem = xzalloc(LZO1X_1_MEM_COMPRESS);
672 else if (h->method == M_LZO1X_1_15)
673 wrk_mem = xzalloc(LZO1X_1_15_MEM_COMPRESS);
674 else if (h->method == M_LZO1X_999)
675 wrk_mem = xzalloc(LZO1X_999_MEM_COMPRESS);
679 l = full_read(0, b1, block_size);
680 src_len = (l > 0 ? l : 0);
682 /* write uncompressed block size */
685 /* exit if last block */
689 /* compute checksum of uncompressed block */
690 if (h->flags & F_ADLER32_D)
691 d_adler32 = lzo_adler32(ADLER32_INIT_VALUE, b1, src_len);
692 if (h->flags & F_CRC32_D)
693 d_crc32 = lzo_crc32(CRC32_INIT_VALUE, b1, src_len);
696 if (h->method == M_LZO1X_1)
697 r = lzo1x_1_compress(b1, src_len, b2, &dst_len, wrk_mem);
698 else if (h->method == M_LZO1X_1_15)
699 r = lzo1x_1_15_compress(b1, src_len, b2, &dst_len, wrk_mem);
700 #if ENABLE_LZOP_COMPR_HIGH
701 else if (h->method == M_LZO1X_999)
702 r = lzo1x_999_compress_level(b1, src_len, b2, &dst_len,
706 bb_error_msg_and_die("internal error");
708 if (r != 0) /* not LZO_E_OK */
709 bb_error_msg_and_die("internal error - compression failed");
711 /* write compressed block size */
712 if (dst_len < src_len) {
714 if (h->method == M_LZO1X_999) {
715 unsigned new_len = src_len;
716 r = lzo1x_optimize(b2, dst_len, b1, &new_len, NULL);
717 if (r != 0 /*LZO_E_OK*/ || new_len != src_len)
718 bb_error_msg_and_die("internal error - optimization failed");
722 /* data actually expanded => store data uncompressed */
726 /* write checksum of uncompressed block */
727 if (h->flags & F_ADLER32_D)
729 if (h->flags & F_CRC32_D)
732 if (dst_len < src_len) {
733 /* write checksum of compressed block */
734 if (h->flags & F_ADLER32_C)
735 write32(lzo_adler32(ADLER32_INIT_VALUE, b2, dst_len));
736 if (h->flags & F_CRC32_C)
737 write32(lzo_crc32(CRC32_INIT_VALUE, b2, dst_len));
738 /* write compressed block data */
739 xwrite(1, b2, dst_len);
741 /* write uncompressed block data */
742 xwrite(1, b1, src_len);
752 static FAST_FUNC void lzo_check(
754 uint8_t* buf, unsigned len,
755 uint32_t FAST_FUNC (*fn)(uint32_t, const uint8_t*, unsigned),
758 /* This function, by having the same order of parameters
759 * as fn, and by being marked FAST_FUNC (same as fn),
760 * saves a dozen bytes of code.
762 uint32_t c = fn(init, buf, len);
764 bb_error_msg_and_die("checksum error");
767 /**********************************************************************/
769 /**********************************************************************/
770 static NOINLINE int lzo_decompress(const header_t *h)
772 unsigned block_size = LZO_BLOCK_SIZE;
774 uint32_t src_len, dst_len;
775 uint32_t c_adler32 = ADLER32_INIT_VALUE;
776 uint32_t d_adler32 = ADLER32_INIT_VALUE;
777 uint32_t c_crc32 = CRC32_INIT_VALUE, d_crc32 = CRC32_INIT_VALUE;
779 uint32_t mcs_block_size = MAX_COMPRESSED_SIZE(block_size);
785 /* read uncompressed block size */
788 /* exit if last block */
792 /* error if split file */
793 if (dst_len == 0xffffffffL)
794 /* should not happen - not yet implemented */
795 bb_error_msg_and_die("this file is a split lzop file");
797 if (dst_len > MAX_BLOCK_SIZE)
798 bb_error_msg_and_die("corrupted data");
800 /* read compressed block size */
802 if (src_len <= 0 || src_len > dst_len)
803 bb_error_msg_and_die("corrupted data");
805 if (dst_len > block_size) {
810 block_size = dst_len;
811 mcs_block_size = MAX_COMPRESSED_SIZE(block_size);
814 /* read checksum of uncompressed block */
815 if (h->flags & F_ADLER32_D)
816 d_adler32 = read32();
817 if (h->flags & F_CRC32_D)
820 /* read checksum of compressed block */
821 if (src_len < dst_len) {
822 if (h->flags & F_ADLER32_C)
823 c_adler32 = read32();
824 if (h->flags & F_CRC32_C)
829 b2 = xzalloc(mcs_block_size);
830 /* read the block into the end of our buffer */
831 b1 = b2 + mcs_block_size - src_len;
832 xread(0, b1, src_len);
834 if (src_len < dst_len) {
835 unsigned d = dst_len;
837 if (!(option_mask32 & OPT_F)) {
838 /* verify checksum of compressed block */
839 if (h->flags & F_ADLER32_C)
840 lzo_check(ADLER32_INIT_VALUE,
842 lzo_adler32, c_adler32);
843 if (h->flags & F_CRC32_C)
844 lzo_check(CRC32_INIT_VALUE,
850 // if (option_mask32 & OPT_F)
851 // r = lzo1x_decompress(b1, src_len, b2, &d, NULL);
853 r = lzo1x_decompress_safe(b1, src_len, b2, &d, NULL);
855 if (r != 0 /*LZO_E_OK*/ || dst_len != d) {
856 bb_error_msg_and_die("corrupted data");
860 /* "stored" block => no decompression */
864 if (!(option_mask32 & OPT_F)) {
865 /* verify checksum of uncompressed block */
866 if (h->flags & F_ADLER32_D)
867 lzo_check(ADLER32_INIT_VALUE,
869 lzo_adler32, d_adler32);
870 if (h->flags & F_CRC32_D)
871 lzo_check(CRC32_INIT_VALUE,
876 /* write uncompressed block data */
877 xwrite(1, dst, dst_len);
884 /**********************************************************************/
885 // lzop file signature (shamelessly borrowed from PNG)
886 /**********************************************************************/
888 * The first nine bytes of a lzop file always contain the following values:
891 * --- --- --- --- --- --- --- --- ---
892 * (hex) 89 4c 5a 4f 00 0d 0a 1a 0a
893 * (decimal) 137 76 90 79 0 13 10 26 10
894 * (C notation - ASCII) \211 L Z O \0 \r \n \032 \n
897 /* (vda) comparison with lzop v1.02rc1 ("lzop -1 <FILE" cmd):
898 * Only slight differences in header:
899 * -00000000 89 4c 5a 4f 00 0d 0a 1a 0a 10 20 20 20 09 40 02
900 * +00000000 89 4c 5a 4f 00 0d 0a 1a 0a 10 10 20 30 09 40 02
902 * version lib_version
903 * -00000010 01 03 00 00 0d 00 00 81 a4 49 f7 a6 3f 00 00 00
904 * +00000010 01 03 00 00 01 00 00 00 00 00 00 00 00 00 00 00
905 * ^^^^^^^^^^^ ^^^^^^^^^^^ ^^^^^^^^^^^
907 * -00000020 00 00 2d 67 04 17 00 04 00 00 00 03 ed ec 9d 6d
908 * +00000020 00 00 10 5f 00 c1 00 04 00 00 00 03 ed ec 9d 6d
911 * The rest is identical.
913 static const unsigned char lzop_magic[9] ALIGN1 = {
914 0x89, 0x4c, 0x5a, 0x4f, 0x00, 0x0d, 0x0a, 0x1a, 0x0a
917 /* This coding is derived from Alexander Lehmann's pngcheck code. */
918 static void check_magic(void)
920 unsigned char magic[sizeof(lzop_magic)];
921 xread(0, magic, sizeof(magic));
922 if (memcmp(magic, lzop_magic, sizeof(lzop_magic)) != 0)
923 bb_error_msg_and_die("bad magic number");
926 /**********************************************************************/
928 /**********************************************************************/
929 static void write_header(const header_t *h)
933 xwrite(1, lzop_magic, sizeof(lzop_magic));
935 init_chksum(&G.chksum_out);
937 f_write16(h->version);
938 f_write16(h->lib_version);
939 f_write16(h->version_needed_to_extract);
945 f_write32(h->gmtdiff);
947 l = (int) strlen(h->name);
952 f_write32(chksum_getresult(&G.chksum_out, h));
955 static int read_header(header_t *h)
961 memset(h, 0, sizeof(*h));
962 h->version_needed_to_extract = 0x0900; /* first lzop version */
965 init_chksum(&G.chksum_in);
967 h->version = f_read16();
968 if (h->version < 0x0900)
970 h->lib_version = f_read16();
971 if (h->version >= 0x0940) {
972 h->version_needed_to_extract = f_read16();
973 if (h->version_needed_to_extract > LZOP_VERSION)
975 if (h->version_needed_to_extract < 0x0900)
978 h->method = f_read8();
979 if (h->version >= 0x0940)
980 h->level = f_read8();
981 h->flags = f_read32();
982 if (h->flags & F_H_FILTER)
983 return 16; /* filter not supported */
984 h->mode = f_read32();
985 h->mtime = f_read32();
986 if (h->version >= 0x0940)
987 h->gmtdiff = f_read32();
994 checksum = chksum_getresult(&G.chksum_in, h);
995 h->header_checksum = f_read32();
996 if (h->header_checksum != checksum)
1001 r = lzo_get_method(h);
1005 /* check reserved flags */
1006 if (h->flags & F_RESERVED)
1009 /* skip extra field [not used yet] */
1010 if (h->flags & F_H_EXTRA_FIELD) {
1013 /* note: the checksum also covers the length */
1014 init_chksum(&G.chksum_in);
1015 h->extra_field_len = f_read32();
1016 for (k = 0; k < h->extra_field_len; k++)
1018 checksum = chksum_getresult(&G.chksum_in, h);
1019 h->extra_field_checksum = f_read32();
1020 if (h->extra_field_checksum != checksum)
1027 static void p_header(header_t *h)
1034 bb_error_msg_and_die("header_error %d", r);
1037 /**********************************************************************/
1039 /**********************************************************************/
1040 static void lzo_set_method(header_t *h)
1044 if (option_mask32 & OPT_1) {
1045 h->method = M_LZO1X_1_15;
1046 } else if (option_mask32 & OPT_789) {
1047 #if ENABLE_LZOP_COMPR_HIGH
1048 h->method = M_LZO1X_999;
1049 if (option_mask32 & OPT_7)
1051 else if (option_mask32 & OPT_8)
1056 bb_error_msg_and_die("high compression not compiled in");
1058 } else { /* levels 2..6 or none (defaults to level 3) */
1059 h->method = M_LZO1X_1;
1060 level = 5; /* levels 2-6 are actually the same */
1066 static int do_lzo_compress(void)
1071 memset(h, 0, sizeof(*h));
1075 h->version = (LZOP_VERSION & 0xffff);
1076 h->version_needed_to_extract = 0x0940;
1077 h->lib_version = lzo_version() & 0xffff;
1079 h->flags = (F_OS & F_OS_MASK) | (F_CS & F_CS_MASK);
1081 if (!(option_mask32 & OPT_F) || h->method == M_LZO1X_999) {
1082 h->flags |= F_ADLER32_D;
1083 if (option_mask32 & OPT_C)
1084 h->flags |= F_ADLER32_C;
1087 return lzo_compress(h);
1091 /**********************************************************************/
1093 /**********************************************************************/
1094 static int do_lzo_decompress(void)
1100 return lzo_decompress(&header);
1103 static char* FAST_FUNC make_new_name_lzop(char *filename, const char *expected_ext UNUSED_PARAM)
1105 if (option_mask32 & OPT_DECOMPRESS) {
1106 char *extension = strrchr(filename, '.');
1107 if (!extension || strcmp(extension + 1, "lzo") != 0)
1108 return xasprintf("%s.out", filename);
1112 return xasprintf("%s.lzo", filename);
1115 static IF_DESKTOP(long long) int FAST_FUNC pack_lzop(transformer_state_t *xstate UNUSED_PARAM)
1117 if (option_mask32 & OPT_DECOMPRESS)
1118 return do_lzo_decompress();
1119 return do_lzo_compress();
1122 int lzop_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1123 int lzop_main(int argc UNUSED_PARAM, char **argv)
1125 getopt32(argv, OPTION_STRING);
1128 if (ENABLE_LZOPCAT && applet_name[4] == 'c')
1129 option_mask32 |= (OPT_STDOUT | OPT_DECOMPRESS);
1131 if (ENABLE_UNLZOP && applet_name[4] == 'o')
1132 option_mask32 |= OPT_DECOMPRESS;
1134 global_crc32_table = crc32_filltable(NULL, 0);
1135 return bbunpack(argv, pack_lzop, make_new_name_lzop, /*unused:*/ NULL);