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 int 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;
653 uint8_t *wrk_mem = NULL;
655 if (h->method == M_LZO1X_1)
656 wrk_mem = xzalloc(LZO1X_1_MEM_COMPRESS);
657 else if (h->method == M_LZO1X_1_15)
658 wrk_mem = xzalloc(LZO1X_1_15_MEM_COMPRESS);
659 else if (h->method == M_LZO1X_999)
660 wrk_mem = xzalloc(LZO1X_999_MEM_COMPRESS);
664 l = full_read(0, b1, block_size);
665 src_len = (l > 0 ? l : 0);
667 /* write uncompressed block size */
670 /* exit if last block */
674 /* compute checksum of uncompressed block */
675 if (h->flags & F_ADLER32_D)
676 d_adler32 = lzo_adler32(ADLER32_INIT_VALUE, b1, src_len);
677 if (h->flags & F_CRC32_D)
678 d_crc32 = lzo_crc32(CRC32_INIT_VALUE, b1, src_len);
681 if (h->method == M_LZO1X_1)
682 r = lzo1x_1_compress(b1, src_len, b2, &dst_len, wrk_mem);
683 else if (h->method == M_LZO1X_1_15)
684 r = lzo1x_1_15_compress(b1, src_len, b2, &dst_len, wrk_mem);
685 #if ENABLE_LZOP_COMPR_HIGH
686 else if (h->method == M_LZO1X_999)
687 r = lzo1x_999_compress_level(b1, src_len, b2, &dst_len,
691 bb_error_msg_and_die("internal error");
693 if (r != 0) /* not LZO_E_OK */
694 bb_error_msg_and_die("internal error - compression failed");
696 /* write compressed block size */
697 if (dst_len < src_len) {
699 if (h->method == M_LZO1X_999) {
700 unsigned new_len = src_len;
701 r = lzo1x_optimize(b2, dst_len, b1, &new_len, NULL);
702 if (r != 0 /*LZO_E_OK*/ || new_len != src_len)
703 bb_error_msg_and_die("internal error - optimization failed");
707 /* data actually expanded => store data uncompressed */
711 /* write checksum of uncompressed block */
712 if (h->flags & F_ADLER32_D)
714 if (h->flags & F_CRC32_D)
717 if (dst_len < src_len) {
718 /* write checksum of compressed block */
719 if (h->flags & F_ADLER32_C)
720 write32(lzo_adler32(ADLER32_INIT_VALUE, b2, dst_len));
721 if (h->flags & F_CRC32_C)
722 write32(lzo_crc32(CRC32_INIT_VALUE, b2, dst_len));
723 /* write compressed block data */
724 xwrite(1, b2, dst_len);
726 /* write uncompressed block data */
727 xwrite(1, b1, src_len);
737 static FAST_FUNC void lzo_check(
739 uint8_t* buf, unsigned len,
740 uint32_t FAST_FUNC (*fn)(uint32_t, const uint8_t*, unsigned),
743 /* This function, by having the same order of parameters
744 * as fn, and by being marked FAST_FUNC (same as fn),
745 * saves a dozen bytes of code.
747 uint32_t c = fn(init, buf, len);
749 bb_error_msg_and_die("checksum error");
752 /**********************************************************************/
754 /**********************************************************************/
755 static NOINLINE int lzo_decompress(const header_t *h)
757 unsigned block_size = LZO_BLOCK_SIZE;
759 uint32_t src_len, dst_len;
760 uint32_t c_adler32 = ADLER32_INIT_VALUE;
761 uint32_t d_adler32 = ADLER32_INIT_VALUE;
762 uint32_t c_crc32 = CRC32_INIT_VALUE, d_crc32 = CRC32_INIT_VALUE;
764 uint32_t mcs_block_size = MAX_COMPRESSED_SIZE(block_size);
770 /* read uncompressed block size */
773 /* exit if last block */
777 /* error if split file */
778 if (dst_len == 0xffffffffL)
779 /* should not happen - not yet implemented */
780 bb_error_msg_and_die("this file is a split lzop file");
782 if (dst_len > MAX_BLOCK_SIZE)
783 bb_error_msg_and_die("corrupted data");
785 /* read compressed block size */
787 if (src_len <= 0 || src_len > dst_len)
788 bb_error_msg_and_die("corrupted data");
790 if (dst_len > block_size) {
795 block_size = dst_len;
796 mcs_block_size = MAX_COMPRESSED_SIZE(block_size);
799 /* read checksum of uncompressed block */
800 if (h->flags & F_ADLER32_D)
801 d_adler32 = read32();
802 if (h->flags & F_CRC32_D)
805 /* read checksum of compressed block */
806 if (src_len < dst_len) {
807 if (h->flags & F_ADLER32_C)
808 c_adler32 = read32();
809 if (h->flags & F_CRC32_C)
814 b2 = xzalloc(mcs_block_size);
815 /* read the block into the end of our buffer */
816 b1 = b2 + mcs_block_size - src_len;
817 xread(0, b1, src_len);
819 if (src_len < dst_len) {
820 unsigned d = dst_len;
822 if (!(option_mask32 & OPT_F)) {
823 /* verify checksum of compressed block */
824 if (h->flags & F_ADLER32_C)
825 lzo_check(ADLER32_INIT_VALUE,
827 lzo_adler32, c_adler32);
828 if (h->flags & F_CRC32_C)
829 lzo_check(CRC32_INIT_VALUE,
835 // if (option_mask32 & OPT_F)
836 // r = lzo1x_decompress(b1, src_len, b2, &d, NULL);
838 r = lzo1x_decompress_safe(b1, src_len, b2, &d, NULL);
840 if (r != 0 /*LZO_E_OK*/ || dst_len != d) {
841 bb_error_msg_and_die("corrupted data");
845 /* "stored" block => no decompression */
849 if (!(option_mask32 & OPT_F)) {
850 /* verify checksum of uncompressed block */
851 if (h->flags & F_ADLER32_D)
852 lzo_check(ADLER32_INIT_VALUE,
854 lzo_adler32, d_adler32);
855 if (h->flags & F_CRC32_D)
856 lzo_check(CRC32_INIT_VALUE,
861 /* write uncompressed block data */
862 xwrite(1, dst, dst_len);
869 /**********************************************************************/
870 // lzop file signature (shamelessly borrowed from PNG)
871 /**********************************************************************/
873 * The first nine bytes of a lzop file always contain the following values:
876 * --- --- --- --- --- --- --- --- ---
877 * (hex) 89 4c 5a 4f 00 0d 0a 1a 0a
878 * (decimal) 137 76 90 79 0 13 10 26 10
879 * (C notation - ASCII) \211 L Z O \0 \r \n \032 \n
882 /* (vda) comparison with lzop v1.02rc1 ("lzop -1 <FILE" cmd):
883 * Only slight differences in header:
884 * -00000000 89 4c 5a 4f 00 0d 0a 1a 0a 10 20 20 20 09 40 02
885 * +00000000 89 4c 5a 4f 00 0d 0a 1a 0a 10 10 20 30 09 40 02
887 * version lib_version
888 * -00000010 01 03 00 00 0d 00 00 81 a4 49 f7 a6 3f 00 00 00
889 * +00000010 01 03 00 00 01 00 00 00 00 00 00 00 00 00 00 00
890 * ^^^^^^^^^^^ ^^^^^^^^^^^ ^^^^^^^^^^^
892 * -00000020 00 00 2d 67 04 17 00 04 00 00 00 03 ed ec 9d 6d
893 * +00000020 00 00 10 5f 00 c1 00 04 00 00 00 03 ed ec 9d 6d
896 * The rest is identical.
898 static const unsigned char lzop_magic[9] = {
899 0x89, 0x4c, 0x5a, 0x4f, 0x00, 0x0d, 0x0a, 0x1a, 0x0a
902 /* This coding is derived from Alexander Lehmann's pngcheck code. */
903 static void check_magic(void)
905 unsigned char magic[sizeof(lzop_magic)];
906 xread(0, magic, sizeof(magic));
907 if (memcmp(magic, lzop_magic, sizeof(lzop_magic)) != 0)
908 bb_error_msg_and_die("bad magic number");
911 /**********************************************************************/
913 /**********************************************************************/
914 static void write_header(const header_t *h)
918 xwrite(1, lzop_magic, sizeof(lzop_magic));
920 init_chksum(&G.chksum_out);
922 f_write16(h->version);
923 f_write16(h->lib_version);
924 f_write16(h->version_needed_to_extract);
930 f_write32(h->gmtdiff);
932 l = (int) strlen(h->name);
937 f_write32(chksum_getresult(&G.chksum_out, h));
940 static int read_header(header_t *h)
946 memset(h, 0, sizeof(*h));
947 h->version_needed_to_extract = 0x0900; /* first lzop version */
950 init_chksum(&G.chksum_in);
952 h->version = f_read16();
953 if (h->version < 0x0900)
955 h->lib_version = f_read16();
956 if (h->version >= 0x0940) {
957 h->version_needed_to_extract = f_read16();
958 if (h->version_needed_to_extract > LZOP_VERSION)
960 if (h->version_needed_to_extract < 0x0900)
963 h->method = f_read8();
964 if (h->version >= 0x0940)
965 h->level = f_read8();
966 h->flags = f_read32();
967 if (h->flags & F_H_FILTER)
968 return 16; /* filter not supported */
969 h->mode = f_read32();
970 h->mtime = f_read32();
971 if (h->version >= 0x0940)
972 h->gmtdiff = f_read32();
979 checksum = chksum_getresult(&G.chksum_in, h);
980 h->header_checksum = f_read32();
981 if (h->header_checksum != checksum)
986 r = lzo_get_method(h);
990 /* check reserved flags */
991 if (h->flags & F_RESERVED)
994 /* skip extra field [not used yet] */
995 if (h->flags & F_H_EXTRA_FIELD) {
998 /* note: the checksum also covers the length */
999 init_chksum(&G.chksum_in);
1000 h->extra_field_len = f_read32();
1001 for (k = 0; k < h->extra_field_len; k++)
1003 checksum = chksum_getresult(&G.chksum_in, h);
1004 h->extra_field_checksum = f_read32();
1005 if (h->extra_field_checksum != checksum)
1012 static void p_header(header_t *h)
1019 bb_error_msg_and_die("header_error %d", r);
1022 /**********************************************************************/
1024 /**********************************************************************/
1025 static void lzo_set_method(header_t *h)
1029 if (option_mask32 & OPT_1) {
1030 h->method = M_LZO1X_1_15;
1031 } else if (option_mask32 & OPT_789) {
1032 #if ENABLE_LZOP_COMPR_HIGH
1033 h->method = M_LZO1X_999;
1034 if (option_mask32 & OPT_7)
1036 else if (option_mask32 & OPT_8)
1041 bb_error_msg_and_die("high compression not compiled in");
1043 } else { /* levels 2..6 or none (defaults to level 3) */
1044 h->method = M_LZO1X_1;
1045 level = 5; /* levels 2-6 are actually the same */
1051 static int do_lzo_compress(void)
1056 memset(h, 0, sizeof(*h));
1060 h->version = (LZOP_VERSION & 0xffff);
1061 h->version_needed_to_extract = 0x0940;
1062 h->lib_version = lzo_version() & 0xffff;
1064 h->flags = (F_OS & F_OS_MASK) | (F_CS & F_CS_MASK);
1066 if (!(option_mask32 & OPT_F) || h->method == M_LZO1X_999) {
1067 h->flags |= F_ADLER32_D;
1068 if (option_mask32 & OPT_C)
1069 h->flags |= F_ADLER32_C;
1072 return lzo_compress(h);
1076 /**********************************************************************/
1078 /**********************************************************************/
1079 static int do_lzo_decompress(void)
1085 return lzo_decompress(&header);
1088 static char* FAST_FUNC make_new_name_lzop(char *filename, const char *expected_ext UNUSED_PARAM)
1090 if (option_mask32 & OPT_DECOMPRESS) {
1091 char *extension = strrchr(filename, '.');
1092 if (!extension || strcmp(extension + 1, "lzo") != 0)
1093 return xasprintf("%s.out", filename);
1097 return xasprintf("%s.lzo", filename);
1100 static IF_DESKTOP(long long) int FAST_FUNC pack_lzop(transformer_state_t *xstate UNUSED_PARAM)
1102 if (option_mask32 & OPT_DECOMPRESS)
1103 return do_lzo_decompress();
1104 return do_lzo_compress();
1107 int lzop_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1108 int lzop_main(int argc UNUSED_PARAM, char **argv)
1110 getopt32(argv, OPTION_STRING);
1113 if (applet_name[4] == 'c')
1114 option_mask32 |= (OPT_STDOUT | OPT_DECOMPRESS);
1116 if (applet_name[4] == 'o')
1117 option_mask32 |= OPT_DECOMPRESS;
1119 global_crc32_table = crc32_filltable(NULL, 0);
1120 return bbunpack(argv, pack_lzop, make_new_name_lzop, /*unused:*/ NULL);