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 "bb_archive.h"
75 #include "liblzo_interface.h"
77 /* lzo-2.03/src/lzo_ptr.h */
78 #define pd(a,b) ((unsigned)((a)-(b)))
80 #define lzo_version() LZO_VERSION
81 #define lzo_sizeof_dict_t (sizeof(uint8_t*))
83 /* lzo-2.03/include/lzo/lzo1x.h */
84 #define LZO1X_1_MEM_COMPRESS (16384 * lzo_sizeof_dict_t)
85 #define LZO1X_1_15_MEM_COMPRESS (32768 * lzo_sizeof_dict_t)
86 #define LZO1X_999_MEM_COMPRESS (14 * 16384 * sizeof(short))
88 /* lzo-2.03/src/lzo1x_oo.c */
89 #define NO_LIT UINT_MAX
91 /**********************************************************************/
92 static void copy2(uint8_t* ip, const uint8_t* m_pos, unsigned off)
101 static void copy3(uint8_t* ip, const uint8_t* m_pos, unsigned off)
105 ip[2] = ip[1] = m_pos[0];
117 /**********************************************************************/
118 // optimize a block of data.
119 /**********************************************************************/
120 #define TEST_IP (ip < ip_end)
121 #define TEST_OP (op <= op_end)
123 static NOINLINE int lzo1x_optimize(uint8_t *in, unsigned in_len,
124 uint8_t *out, unsigned *out_len,
125 void* wrkmem UNUSED_PARAM)
131 uint8_t* const ip_end = in + in_len;
132 uint8_t* const op_end = out + *out_len;
133 uint8_t* litp = NULL;
135 unsigned next_lit = NO_LIT;
137 unsigned long o_m1_a = 0, o_m1_b = 0, o_m2 = 0, o_m3_a = 0, o_m3_b = 0;
139 // LZO_UNUSED(wrkmem);
150 goto first_literal_run;
153 while (TEST_IP && TEST_OP) {
172 do *op++ = *ip++; while (--t > 0);
179 m_pos = op - 1 - 0x800;
181 m_pos = op - 1 - 0x400;
194 if (t < 16) { /* a M1 match */
203 /* test if a match follows */
204 if (nl == 0 && lit == 1 && ip[0] >= 16) {
206 /* adjust length of previous short run */
208 *litp = (unsigned char)((*litp & ~3) | lit);
209 /* copy over the 2 literals that replace the match */
210 copy2(ip-2, m_pos, pd(op, m_pos));
213 /* test if a literal run follows */
218 && (lit + 2 + ip[0] < 16)
221 /* remove short run */
223 /* copy over the 2 literals that replace the match */
224 copy2(ip-3+1, m_pos, pd(op, m_pos));
225 /* move literals 1 byte ahead */
228 memmove(litp+1, litp, lit);
229 /* insert new length of long literal run */
231 *litp = (unsigned char)(lit - 3);
236 goto copy_literal_run;
243 if (t >= 64) { /* a M2 match */
246 m_pos -= (t >> 2) & 7;
250 m_pos -= (t >> 2) & 3;
258 /* test if in beetween two long literal runs */
259 if (t == 1 && lit > 3 && nl == 0
260 && ip[0] < 16 && ip[0] != 0 && (lit + 3 + ip[0] < 16)
263 /* copy over the 3 literals that replace the match */
264 copy3(ip-1-2, m_pos, pd(op, m_pos));
265 /* set new length of previous literal run */
267 *litp = (unsigned char)(lit - 3);
272 goto copy_literal_run;
275 if (t >= 32) { /* a M3 match */
286 } else { /* a M4 match */
288 m_pos -= (t & 8) << 11;
306 /* test if in beetween two matches */
307 if (t == 1 && lit == 0 && nl == 0 && ip[0] >= 16) {
309 /* make a previous short run */
311 *litp = (unsigned char)((*litp & ~3) | lit);
312 /* copy over the 3 literals that replace the match */
313 copy3(ip-3, m_pos, pd(op, m_pos));
316 /* test if a literal run follows */
317 else if (t == 1 && lit <= 3 && nl == 0
318 && ip[0] < 16 && ip[0] != 0 && (lit + 3 + ip[0] < 16)
321 /* remove short run */
323 /* copy over the 3 literals that replace the match */
324 copy3(ip-4+1, m_pos, pd(op, m_pos));
325 /* move literals 1 byte ahead */
328 memmove(litp+1,litp,lit);
329 /* insert new length of long literal run */
331 *litp = (unsigned char)(lit - 3);
337 goto copy_literal_run;
343 do *op++ = *m_pos++; while (--t > 0);
347 if (next_lit == NO_LIT) {
359 do *op++ = *ip++; while (--t > 0);
361 } while (TEST_IP && TEST_OP);
364 /* no EOF code was found */
365 *out_len = pd(op, out);
366 return LZO_E_EOF_NOT_FOUND;
369 // LZO_UNUSED(o_m1_a); LZO_UNUSED(o_m1_b); LZO_UNUSED(o_m2);
370 // LZO_UNUSED(o_m3_a); LZO_UNUSED(o_m3_b);
371 *out_len = pd(op, out);
372 return (ip == ip_end ? LZO_E_OK :
373 (ip < ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
376 /**********************************************************************/
377 #define F_OS F_OS_UNIX
378 #define F_CS F_CS_NATIVE
380 /**********************************************************************/
381 #define ADLER32_INIT_VALUE 1
382 #define CRC32_INIT_VALUE 0
384 /**********************************************************************/
391 /**********************************************************************/
393 #define F_ADLER32_D 0x00000001L
394 #define F_ADLER32_C 0x00000002L
395 #define F_H_EXTRA_FIELD 0x00000040L
396 #define F_H_GMTDIFF 0x00000080L
397 #define F_CRC32_D 0x00000100L
398 #define F_CRC32_C 0x00000200L
399 #define F_H_FILTER 0x00000800L
400 #define F_H_CRC32 0x00001000L
401 #define F_MASK 0x00003FFFL
403 /* operating system & file system that created the file [mostly unused] */
404 #define F_OS_UNIX 0x03000000L
405 #define F_OS_SHIFT 24
406 #define F_OS_MASK 0xff000000L
408 /* character set for file name encoding [mostly unused] */
409 #define F_CS_NATIVE 0x00000000L
410 #define F_CS_SHIFT 20
411 #define F_CS_MASK 0x00f00000L
413 /* these bits must be zero */
414 #define F_RESERVED ((F_MASK | F_OS_MASK | F_CS_MASK) ^ 0xffffffffL)
416 typedef struct chksum_t {
421 typedef struct header_t {
423 unsigned lib_version;
424 unsigned version_needed_to_extract;
429 uint32_t header_checksum;
431 uint32_t extra_field_len;
432 uint32_t extra_field_checksum;
434 unsigned char method;
442 /*const uint32_t *lzo_crc32_table;*/
446 #define G (*(struct globals*)&bb_common_bufsiz1)
447 #define INIT_G() do { } while (0)
448 //#define G (*ptr_to_globals)
449 //#define INIT_G() do {
450 // SET_PTR_TO_GLOBALS(xzalloc(sizeof(G)));
454 /**********************************************************************/
455 #define LZOP_VERSION 0x1010
456 //#define LZOP_VERSION_STRING "1.01"
457 //#define LZOP_VERSION_DATE "Apr 27th 2003"
459 #define OPTION_STRING "cfvqdt123456789CF"
461 /* Note: must be kept in sync with archival/bbunzip.c */
463 OPT_STDOUT = (1 << 0),
464 OPT_FORCE = (1 << 1),
465 OPT_VERBOSE = (1 << 2),
466 OPT_QUIET = (1 << 3),
467 OPT_DECOMPRESS = (1 << 4),
482 /**********************************************************************/
484 // adapted from free code by Mark Adler <madler@alumni.caltech.edu>
485 // see http://www.zlib.org/
486 /**********************************************************************/
487 static FAST_FUNC uint32_t
488 lzo_adler32(uint32_t adler, const uint8_t* buf, unsigned len)
491 LZO_BASE = 65521, /* largest prime smaller than 65536 */
492 /* NMAX is the largest n such that
493 * 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
496 uint32_t s1 = adler & 0xffff;
497 uint32_t s2 = (adler >> 16) & 0xffff;
504 k = len < LZO_NMAX ? (unsigned) len : LZO_NMAX;
513 return (s2 << 16) | s1;
516 static FAST_FUNC uint32_t
517 lzo_crc32(uint32_t c, const uint8_t* buf, unsigned len)
519 //if (buf == NULL) - impossible
522 return ~crc32_block_endian0(~c, buf, len, global_crc32_table);
525 /**********************************************************************/
526 static void init_chksum(chksum_t *ct)
528 ct->f_adler32 = ADLER32_INIT_VALUE;
529 ct->f_crc32 = CRC32_INIT_VALUE;
532 static void add_bytes_to_chksum(chksum_t *ct, const void* buf, int cnt)
534 /* We need to handle the two checksums at once, because at the
535 * beginning of the header, we don't know yet which one we'll
537 ct->f_adler32 = lzo_adler32(ct->f_adler32, (const uint8_t*)buf, cnt);
538 ct->f_crc32 = lzo_crc32(ct->f_crc32, (const uint8_t*)buf, cnt);
541 static uint32_t chksum_getresult(chksum_t *ct, const header_t *h)
543 return (h->flags & F_H_CRC32) ? ct->f_crc32 : ct->f_adler32;
546 /**********************************************************************/
547 static uint32_t read32(void)
554 static void write32(uint32_t v)
560 static void f_write(const void* buf, int cnt)
563 add_bytes_to_chksum(&G.chksum_out, buf, cnt);
566 static void f_read(void* buf, int cnt)
569 add_bytes_to_chksum(&G.chksum_in, buf, cnt);
572 static int f_read8(void)
579 static void f_write8(uint8_t v)
584 static unsigned f_read16(void)
591 static void f_write16(uint16_t v)
597 static uint32_t f_read32(void)
604 static void f_write32(uint32_t v)
610 /**********************************************************************/
611 static int lzo_get_method(header_t *h)
614 if (h->method == M_LZO1X_1) {
617 } else if (h->method == M_LZO1X_1_15) {
620 } else if (h->method == M_LZO1X_999) {
624 return -1; /* not a LZO method */
626 /* check compression level */
627 if (h->level < 1 || h->level > 9)
633 /**********************************************************************/
634 #define LZO_BLOCK_SIZE (256 * 1024l)
635 #define MAX_BLOCK_SIZE (64 * 1024l * 1024l) /* DO NOT CHANGE */
637 /* LZO may expand uncompressible data by a small amount */
638 #define MAX_COMPRESSED_SIZE(x) ((x) + (x) / 16 + 64 + 3)
640 /**********************************************************************/
642 /**********************************************************************/
643 static NOINLINE smallint lzo_compress(const header_t *h)
645 unsigned block_size = LZO_BLOCK_SIZE;
646 int r = 0; /* LZO_E_OK */
647 uint8_t *const b1 = xzalloc(block_size);
648 uint8_t *const b2 = xzalloc(MAX_COMPRESSED_SIZE(block_size));
649 unsigned src_len = 0, dst_len = 0;
650 uint32_t d_adler32 = ADLER32_INIT_VALUE;
651 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 smallint 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;
766 uint32_t mcs_block_size = MAX_COMPRESSED_SIZE(block_size);
772 /* read uncompressed block size */
775 /* exit if last block */
779 /* error if split file */
780 if (dst_len == 0xffffffffL)
781 /* should not happen - not yet implemented */
782 bb_error_msg_and_die("this file is a split lzop file");
784 if (dst_len > MAX_BLOCK_SIZE)
785 bb_error_msg_and_die("corrupted data");
787 /* read compressed block size */
789 if (src_len <= 0 || src_len > dst_len)
790 bb_error_msg_and_die("corrupted data");
792 if (dst_len > block_size) {
797 block_size = dst_len;
798 mcs_block_size = MAX_COMPRESSED_SIZE(block_size);
801 /* read checksum of uncompressed block */
802 if (h->flags & F_ADLER32_D)
803 d_adler32 = read32();
804 if (h->flags & F_CRC32_D)
807 /* read checksum of compressed block */
808 if (src_len < dst_len) {
809 if (h->flags & F_ADLER32_C)
810 c_adler32 = read32();
811 if (h->flags & F_CRC32_C)
816 b2 = xzalloc(mcs_block_size);
817 /* read the block into the end of our buffer */
818 b1 = b2 + mcs_block_size - src_len;
819 xread(0, b1, src_len);
821 if (src_len < dst_len) {
822 unsigned d = dst_len;
824 if (!(option_mask32 & OPT_F)) {
825 /* verify checksum of compressed block */
826 if (h->flags & F_ADLER32_C)
827 lzo_check(ADLER32_INIT_VALUE,
829 lzo_adler32, c_adler32);
830 if (h->flags & F_CRC32_C)
831 lzo_check(CRC32_INIT_VALUE,
837 // if (option_mask32 & OPT_F)
838 // r = lzo1x_decompress(b1, src_len, b2, &d, NULL);
840 r = lzo1x_decompress_safe(b1, src_len, b2, &d, NULL);
842 if (r != 0 /*LZO_E_OK*/ || dst_len != d) {
843 bb_error_msg_and_die("corrupted data");
847 /* "stored" block => no decompression */
851 if (!(option_mask32 & OPT_F)) {
852 /* verify checksum of uncompressed block */
853 if (h->flags & F_ADLER32_D)
854 lzo_check(ADLER32_INIT_VALUE,
856 lzo_adler32, d_adler32);
857 if (h->flags & F_CRC32_D)
858 lzo_check(CRC32_INIT_VALUE,
863 /* write uncompressed block data */
864 xwrite(1, dst, dst_len);
871 /**********************************************************************/
872 // lzop file signature (shamelessly borrowed from PNG)
873 /**********************************************************************/
875 * The first nine bytes of a lzop file always contain the following values:
878 * --- --- --- --- --- --- --- --- ---
879 * (hex) 89 4c 5a 4f 00 0d 0a 1a 0a
880 * (decimal) 137 76 90 79 0 13 10 26 10
881 * (C notation - ASCII) \211 L Z O \0 \r \n \032 \n
884 /* (vda) comparison with lzop v1.02rc1 ("lzop -1 <FILE" cmd):
885 * Only slight differences in header:
886 * -00000000 89 4c 5a 4f 00 0d 0a 1a 0a 10 20 20 20 09 40 02
887 * +00000000 89 4c 5a 4f 00 0d 0a 1a 0a 10 10 20 30 09 40 02
889 * version lib_version
890 * -00000010 01 03 00 00 0d 00 00 81 a4 49 f7 a6 3f 00 00 00
891 * +00000010 01 03 00 00 01 00 00 00 00 00 00 00 00 00 00 00
892 * ^^^^^^^^^^^ ^^^^^^^^^^^ ^^^^^^^^^^^
894 * -00000020 00 00 2d 67 04 17 00 04 00 00 00 03 ed ec 9d 6d
895 * +00000020 00 00 10 5f 00 c1 00 04 00 00 00 03 ed ec 9d 6d
898 * The rest is identical.
900 static const unsigned char lzop_magic[9] = {
901 0x89, 0x4c, 0x5a, 0x4f, 0x00, 0x0d, 0x0a, 0x1a, 0x0a
904 /* This coding is derived from Alexander Lehmann's pngcheck code. */
905 static void check_magic(void)
907 unsigned char magic[sizeof(lzop_magic)];
908 xread(0, magic, sizeof(magic));
909 if (memcmp(magic, lzop_magic, sizeof(lzop_magic)) != 0)
910 bb_error_msg_and_die("bad magic number");
913 /**********************************************************************/
915 /**********************************************************************/
916 static void write_header(const header_t *h)
920 xwrite(1, lzop_magic, sizeof(lzop_magic));
922 init_chksum(&G.chksum_out);
924 f_write16(h->version);
925 f_write16(h->lib_version);
926 f_write16(h->version_needed_to_extract);
932 f_write32(h->gmtdiff);
934 l = (int) strlen(h->name);
939 f_write32(chksum_getresult(&G.chksum_out, h));
942 static int read_header(header_t *h)
948 memset(h, 0, sizeof(*h));
949 h->version_needed_to_extract = 0x0900; /* first lzop version */
952 init_chksum(&G.chksum_in);
954 h->version = f_read16();
955 if (h->version < 0x0900)
957 h->lib_version = f_read16();
958 if (h->version >= 0x0940) {
959 h->version_needed_to_extract = f_read16();
960 if (h->version_needed_to_extract > LZOP_VERSION)
962 if (h->version_needed_to_extract < 0x0900)
965 h->method = f_read8();
966 if (h->version >= 0x0940)
967 h->level = f_read8();
968 h->flags = f_read32();
969 if (h->flags & F_H_FILTER)
970 return 16; /* filter not supported */
971 h->mode = f_read32();
972 h->mtime = f_read32();
973 if (h->version >= 0x0940)
974 h->gmtdiff = f_read32();
981 checksum = chksum_getresult(&G.chksum_in, h);
982 h->header_checksum = f_read32();
983 if (h->header_checksum != checksum)
988 r = lzo_get_method(h);
992 /* check reserved flags */
993 if (h->flags & F_RESERVED)
996 /* skip extra field [not used yet] */
997 if (h->flags & F_H_EXTRA_FIELD) {
1000 /* note: the checksum also covers the length */
1001 init_chksum(&G.chksum_in);
1002 h->extra_field_len = f_read32();
1003 for (k = 0; k < h->extra_field_len; k++)
1005 checksum = chksum_getresult(&G.chksum_in, h);
1006 h->extra_field_checksum = f_read32();
1007 if (h->extra_field_checksum != checksum)
1014 static void p_header(header_t *h)
1021 bb_error_msg_and_die("header_error %d", r);
1024 /**********************************************************************/
1026 /**********************************************************************/
1027 static void lzo_set_method(header_t *h)
1031 if (option_mask32 & OPT_1) {
1032 h->method = M_LZO1X_1_15;
1033 } else if (option_mask32 & OPT_789) {
1034 #if ENABLE_LZOP_COMPR_HIGH
1035 h->method = M_LZO1X_999;
1036 if (option_mask32 & OPT_7)
1038 else if (option_mask32 & OPT_8)
1043 bb_error_msg_and_die("high compression not compiled in");
1045 } else { /* levels 2..6 or none (defaults to level 3) */
1046 h->method = M_LZO1X_1;
1047 level = 5; /* levels 2-6 are actually the same */
1053 static smallint do_lzo_compress(void)
1058 memset(h, 0, sizeof(*h));
1062 h->version = (LZOP_VERSION & 0xffff);
1063 h->version_needed_to_extract = 0x0940;
1064 h->lib_version = lzo_version() & 0xffff;
1066 h->flags = (F_OS & F_OS_MASK) | (F_CS & F_CS_MASK);
1068 if (!(option_mask32 & OPT_F) || h->method == M_LZO1X_999) {
1069 h->flags |= F_ADLER32_D;
1070 if (option_mask32 & OPT_C)
1071 h->flags |= F_ADLER32_C;
1074 return lzo_compress(h);
1078 /**********************************************************************/
1080 /**********************************************************************/
1081 static smallint do_lzo_decompress(void)
1087 return lzo_decompress(&header);
1090 static char* FAST_FUNC make_new_name_lzop(char *filename, const char *expected_ext UNUSED_PARAM)
1092 if (option_mask32 & OPT_DECOMPRESS) {
1093 char *extension = strrchr(filename, '.');
1094 if (!extension || strcmp(extension + 1, "lzo") != 0)
1095 return xasprintf("%s.out", filename);
1099 return xasprintf("%s.lzo", filename);
1102 static IF_DESKTOP(long long) int FAST_FUNC pack_lzop(transformer_state_t *xstate UNUSED_PARAM)
1104 if (option_mask32 & OPT_DECOMPRESS)
1105 return do_lzo_decompress();
1106 return do_lzo_compress();
1109 int lzop_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1110 int lzop_main(int argc UNUSED_PARAM, char **argv)
1112 getopt32(argv, OPTION_STRING);
1115 if (applet_name[4] == 'c')
1116 option_mask32 |= (OPT_STDOUT | OPT_DECOMPRESS);
1118 if (applet_name[4] == 'o')
1119 option_mask32 |= OPT_DECOMPRESS;
1121 global_crc32_table = crc32_filltable(NULL, 0);
1122 return bbunpack(argv, pack_lzop, make_new_name_lzop, /*unused:*/ NULL);