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 #include "unarchive.h"
30 #include "liblzo_interface.h"
32 /* lzo-2.03/src/lzo_ptr.h */
33 #define pd(a,b) ((unsigned)((a)-(b)))
35 #define lzo_version() LZO_VERSION
36 #define lzo_sizeof_dict_t (sizeof(uint8_t*))
38 /* lzo-2.03/include/lzo/lzo1x.h */
39 #define LZO1X_1_MEM_COMPRESS (16384 * lzo_sizeof_dict_t)
40 #define LZO1X_1_15_MEM_COMPRESS (32768 * lzo_sizeof_dict_t)
41 #define LZO1X_999_MEM_COMPRESS (14 * 16384 * sizeof(short))
43 /* lzo-2.03/src/lzo1x_oo.c */
44 #define NO_LIT UINT_MAX
46 /**********************************************************************/
47 static void copy2(uint8_t* ip, const uint8_t* m_pos, unsigned off)
56 static void copy3(uint8_t* ip, const uint8_t* m_pos, unsigned off)
60 ip[2] = ip[1] = m_pos[0];
72 /**********************************************************************/
73 // optimize a block of data.
74 /**********************************************************************/
75 #define TEST_IP (ip < ip_end)
76 #define TEST_OP (op <= op_end)
78 static int lzo1x_optimize(uint8_t *in, unsigned in_len,
79 uint8_t *out, unsigned *out_len,
80 void* wrkmem UNUSED_PARAM)
86 uint8_t* const ip_end = in + in_len;
87 uint8_t* const op_end = out + *out_len;
90 unsigned next_lit = NO_LIT;
92 unsigned long o_m1_a = 0, o_m1_b = 0, o_m2 = 0, o_m3_a = 0, o_m3_b = 0;
94 // LZO_UNUSED(wrkmem);
105 goto first_literal_run;
108 while (TEST_IP && TEST_OP) {
127 do *op++ = *ip++; while (--t > 0);
134 m_pos = op - 1 - 0x800;
136 m_pos = op - 1 - 0x400;
149 if (t < 16) { /* a M1 match */
158 /* test if a match follows */
159 if (nl == 0 && lit == 1 && ip[0] >= 16) {
161 /* adjust length of previous short run */
163 *litp = (unsigned char)((*litp & ~3) | lit);
164 /* copy over the 2 literals that replace the match */
165 copy2(ip-2, m_pos, pd(op, m_pos));
168 /* test if a literal run follows */
169 else if (nl == 0 && ip[0] < 16 && ip[0] != 0 &&
170 (lit + 2 + ip[0] < 16))
173 /* remove short run */
175 /* copy over the 2 literals that replace the match */
176 copy2(ip-3+1,m_pos,pd(op,m_pos));
177 /* move literals 1 byte ahead */
180 memmove(litp+1, litp, lit);
181 /* insert new length of long literal run */
183 *litp = (unsigned char)(lit - 3);
186 *op++ = *m_pos++; *op++ = *m_pos++;
187 goto copy_literal_run;
194 if (t >= 64) { /* a M2 match */
197 m_pos -= (t >> 2) & 7;
201 m_pos -= (t >> 2) & 3;
209 /* test if in beetween two long literal runs */
210 if (t == 1 && lit > 3 && nl == 0
211 && ip[0] < 16 && ip[0] != 0 && (lit + 3 + ip[0] < 16)
214 /* copy over the 3 literals that replace the match */
215 copy3(ip-1-2,m_pos,pd(op,m_pos));
216 /* set new length of previous literal run */
218 *litp = (unsigned char)(lit - 3);
223 goto copy_literal_run;
226 if (t >= 32) { /* a M3 match */
237 } else { /* a M4 match */
239 m_pos -= (t & 8) << 11;
257 /* test if in beetween two matches */
258 if (t == 1 && lit == 0 && nl == 0 && ip[0] >= 16) {
260 /* make a previous short run */
262 *litp = (unsigned char)((*litp & ~3) | lit);
263 /* copy over the 3 literals that replace the match */
264 copy3(ip-3,m_pos,pd(op,m_pos));
267 /* test if a literal run follows */
268 else if (t == 1 && lit <= 3 && nl == 0
269 && ip[0] < 16 && ip[0] != 0 && (lit + 3 + ip[0] < 16)
272 /* remove short run */
274 /* copy over the 3 literals that replace the match */
275 copy3(ip-4+1,m_pos,pd(op,m_pos));
276 /* move literals 1 byte ahead */
279 memmove(litp+1,litp,lit);
280 /* insert new length of long literal run */
282 *litp = (unsigned char)(lit - 3);
288 goto copy_literal_run;
294 do *op++ = *m_pos++; while (--t > 0);
298 if (next_lit == NO_LIT) {
310 do *op++ = *ip++; while (--t > 0);
312 } while (TEST_IP && TEST_OP);
315 /* no EOF code was found */
316 *out_len = pd(op, out);
317 return LZO_E_EOF_NOT_FOUND;
320 // LZO_UNUSED(o_m1_a); LZO_UNUSED(o_m1_b); LZO_UNUSED(o_m2);
321 // LZO_UNUSED(o_m3_a); LZO_UNUSED(o_m3_b);
322 *out_len = pd(op, out);
323 return (ip == ip_end ? LZO_E_OK :
324 (ip < ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
327 /**********************************************************************/
328 #define F_OS F_OS_UNIX
329 #define F_CS F_CS_NATIVE
331 /**********************************************************************/
332 #define ADLER32_INIT_VALUE 1
333 #define CRC32_INIT_VALUE 0
335 /**********************************************************************/
342 /**********************************************************************/
344 #define F_ADLER32_D 0x00000001L
345 #define F_ADLER32_C 0x00000002L
346 #define F_H_EXTRA_FIELD 0x00000040L
347 #define F_H_GMTDIFF 0x00000080L
348 #define F_CRC32_D 0x00000100L
349 #define F_CRC32_C 0x00000200L
350 #define F_H_FILTER 0x00000800L
351 #define F_H_CRC32 0x00001000L
352 #define F_MASK 0x00003FFFL
354 /* operating system & file system that created the file [mostly unused] */
355 #define F_OS_UNIX 0x03000000L
356 #define F_OS_SHIFT 24
357 #define F_OS_MASK 0xff000000L
359 /* character set for file name encoding [mostly unused] */
360 #define F_CS_NATIVE 0x00000000L
361 #define F_CS_SHIFT 20
362 #define F_CS_MASK 0x00f00000L
364 /* these bits must be zero */
365 #define F_RESERVED ((F_MASK | F_OS_MASK | F_CS_MASK) ^ 0xffffffffL)
367 typedef struct chksum_t {
372 typedef struct header_t {
374 unsigned lib_version;
375 unsigned version_needed_to_extract;
380 uint32_t header_checksum;
382 uint32_t extra_field_len;
383 uint32_t extra_field_checksum;
385 unsigned char method;
393 const uint32_t *lzo_crc32_table;
397 #define G (*(struct globals*)&bb_common_bufsiz1)
398 #define INIT_G() do { } while (0)
399 //#define G (*ptr_to_globals)
400 //#define INIT_G() do {
401 // SET_PTR_TO_GLOBALS(xzalloc(sizeof(G)));
405 /**********************************************************************/
406 #define LZOP_VERSION 0x1010
407 //#define LZOP_VERSION_STRING "1.01"
408 //#define LZOP_VERSION_DATE "Apr 27th 2003"
410 #define OPTION_STRING "cfvdt123456789CF"
413 OPT_STDOUT = (1 << 0),
414 OPT_FORCE = (1 << 1),
415 OPT_VERBOSE = (1 << 2),
416 OPT_DECOMPRESS = (1 << 3),
431 /**********************************************************************/
433 // adapted from free code by Mark Adler <madler@alumni.caltech.edu>
434 // see http://www.zlib.org/
435 /**********************************************************************/
436 static FAST_FUNC uint32_t
437 lzo_adler32(uint32_t adler, const uint8_t* buf, unsigned len)
440 LZO_BASE = 65521, /* largest prime smaller than 65536 */
441 /* NMAX is the largest n such that
442 * 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
445 uint32_t s1 = adler & 0xffff;
446 uint32_t s2 = (adler >> 16) & 0xffff;
453 k = len < LZO_NMAX ? (unsigned) len : LZO_NMAX;
462 return (s2 << 16) | s1;
465 static FAST_FUNC uint32_t
466 lzo_crc32(uint32_t c, const uint8_t* buf, unsigned len)
475 crc = G.lzo_crc32_table[((int)crc ^ *buf) & 0xff] ^ (crc >> 8);
483 /**********************************************************************/
484 static void init_chksum(chksum_t *ct)
486 ct->f_adler32 = ADLER32_INIT_VALUE;
487 ct->f_crc32 = CRC32_INIT_VALUE;
490 static void add_bytes_to_chksum(chksum_t *ct, const void* buf, int cnt)
492 /* We need to handle the two checksums at once, because at the
493 * beginning of the header, we don't know yet which one we'll
495 ct->f_adler32 = lzo_adler32(ct->f_adler32, (const uint8_t*)buf, cnt);
496 ct->f_crc32 = lzo_crc32(ct->f_crc32, (const uint8_t*)buf, cnt);
499 static uint32_t chksum_getresult(chksum_t *ct, const header_t *h)
501 return (h->flags & F_H_CRC32) ? ct->f_crc32 : ct->f_adler32;
504 /**********************************************************************/
505 static uint32_t read32(void)
512 static void write32(uint32_t v)
518 static void f_write(const void* buf, int cnt)
521 add_bytes_to_chksum(&G.chksum_out, buf, cnt);
524 static void f_read(void* buf, int cnt)
527 add_bytes_to_chksum(&G.chksum_in, buf, cnt);
530 static int f_read8(void)
537 static void f_write8(uint8_t v)
542 static unsigned f_read16(void)
549 static void f_write16(uint16_t v)
555 static uint32_t f_read32(void)
562 static void f_write32(uint32_t v)
568 /**********************************************************************/
569 static int lzo_get_method(header_t *h)
572 if (h->method == M_LZO1X_1) {
575 } else if (h->method == M_LZO1X_1_15) {
578 } else if (h->method == M_LZO1X_999) {
582 return -1; /* not a LZO method */
584 /* check compression level */
585 if (h->level < 1 || h->level > 9)
591 /**********************************************************************/
592 #define LZO_BLOCK_SIZE (256 * 1024l)
593 #define MAX_BLOCK_SIZE (64 * 1024l * 1024l) /* DO NOT CHANGE */
595 /* LZO may expand uncompressible data by a small amount */
596 #define MAX_COMPRESSED_SIZE(x) ((x) + (x) / 16 + 64 + 3)
598 /**********************************************************************/
600 /**********************************************************************/
601 static smallint lzo_compress(const header_t *h)
603 unsigned block_size = LZO_BLOCK_SIZE;
604 int r = 0; /* LZO_E_OK */
605 uint8_t *const b1 = xzalloc(block_size);
606 uint8_t *const b2 = xzalloc(MAX_COMPRESSED_SIZE(block_size));
607 unsigned src_len = 0, dst_len = 0;
608 uint32_t d_adler32 = ADLER32_INIT_VALUE;
609 uint32_t d_crc32 = CRC32_INIT_VALUE;
612 uint8_t *wrk_mem = NULL;
614 if (h->method == M_LZO1X_1)
615 wrk_mem = xzalloc(LZO1X_1_MEM_COMPRESS);
616 else if (h->method == M_LZO1X_1_15)
617 wrk_mem = xzalloc(LZO1X_1_15_MEM_COMPRESS);
618 else if (h->method == M_LZO1X_999)
619 wrk_mem = xzalloc(LZO1X_999_MEM_COMPRESS);
623 l = full_read(0, b1, block_size);
624 src_len = (l > 0 ? l : 0);
626 /* write uncompressed block size */
629 /* exit if last block */
633 /* compute checksum of uncompressed block */
634 if (h->flags & F_ADLER32_D)
635 d_adler32 = lzo_adler32(ADLER32_INIT_VALUE, b1, src_len);
636 if (h->flags & F_CRC32_D)
637 d_crc32 = lzo_crc32(CRC32_INIT_VALUE, b1, src_len);
640 if (h->method == M_LZO1X_1)
641 r = lzo1x_1_compress(b1, src_len, b2, &dst_len, wrk_mem);
642 else if (h->method == M_LZO1X_1_15)
643 r = lzo1x_1_15_compress(b1, src_len, b2, &dst_len, wrk_mem);
644 #if ENABLE_LZOP_COMPR_HIGH
645 else if (h->method == M_LZO1X_999)
646 r = lzo1x_999_compress_level(b1, src_len, b2, &dst_len,
650 bb_error_msg_and_die("internal error");
652 if (r != 0) /* not LZO_E_OK */
653 bb_error_msg_and_die("internal error - compression failed");
655 /* write compressed block size */
656 if (dst_len < src_len) {
658 if (h->method == M_LZO1X_999) {
659 unsigned new_len = src_len;
660 r = lzo1x_optimize(b2, dst_len, b1, &new_len, NULL);
661 if (r != 0 /*LZO_E_OK*/ || new_len != src_len)
662 bb_error_msg_and_die("internal error - optimization failed");
666 /* data actually expanded => store data uncompressed */
670 /* write checksum of uncompressed block */
671 if (h->flags & F_ADLER32_D)
673 if (h->flags & F_CRC32_D)
676 if (dst_len < src_len) {
677 /* write checksum of compressed block */
678 if (h->flags & F_ADLER32_C)
679 write32(lzo_adler32(ADLER32_INIT_VALUE, b2,
681 if (h->flags & F_CRC32_C)
682 write32(lzo_crc32(CRC32_INIT_VALUE, b2, dst_len));
683 /* write compressed block data */
684 xwrite(1, b2, dst_len);
686 /* write uncompressed block data */
687 xwrite(1, b1, src_len);
697 static void lzo_check(uint32_t FAST_FUNC (*fn)(uint32_t, const uint8_t*, unsigned),
698 uint32_t ref, uint32_t init,
699 uint8_t* buf, unsigned len)
701 uint32_t c = fn(init, buf, len);
703 bb_error_msg_and_die("checksum error");
706 /**********************************************************************/
708 /**********************************************************************/
709 static smallint lzo_decompress(const header_t *h)
711 unsigned block_size = LZO_BLOCK_SIZE;
713 uint32_t src_len, dst_len;
714 uint32_t c_adler32 = ADLER32_INIT_VALUE;
715 uint32_t d_adler32 = ADLER32_INIT_VALUE;
716 uint32_t c_crc32 = CRC32_INIT_VALUE, d_crc32 = CRC32_INIT_VALUE;
719 uint32_t mcs_block_size = MAX_COMPRESSED_SIZE(block_size);
725 /* read uncompressed block size */
728 /* exit if last block */
732 /* error if split file */
733 if (dst_len == 0xffffffffL)
734 /* should not happen - not yet implemented */
735 bb_error_msg_and_die("this file is a split lzop file");
737 if (dst_len > MAX_BLOCK_SIZE)
738 bb_error_msg_and_die("lzop file corrupted");
740 /* read compressed block size */
742 if (src_len <= 0 || src_len > dst_len)
743 bb_error_msg_and_die("lzop file corrupted");
745 if (dst_len > block_size) {
751 block_size = dst_len;
752 mcs_block_size = MAX_COMPRESSED_SIZE(block_size);
755 /* read checksum of uncompressed block */
756 if (h->flags & F_ADLER32_D)
757 d_adler32 = read32();
758 if (h->flags & F_CRC32_D)
761 /* read checksum of compressed block */
762 if (src_len < dst_len) {
763 if (h->flags & F_ADLER32_C)
764 c_adler32 = read32();
765 if (h->flags & F_CRC32_C)
770 b2 = xzalloc(mcs_block_size);
771 /* read the block into the end of our buffer */
772 b1 = b2 + mcs_block_size - src_len;
773 xread(0, b1, src_len);
775 if (src_len < dst_len) {
776 unsigned d = dst_len;
778 if (!(option_mask32 & OPT_F)) {
779 /* verify checksum of compressed block */
780 if (h->flags & F_ADLER32_C)
781 lzo_check(lzo_adler32, c_adler32,
784 if (h->flags & F_CRC32_C)
785 lzo_check(lzo_crc32, c_crc32,
791 // if (option_mask32 & OPT_F)
792 // r = lzo1x_decompress(b1, src_len, b2, &d, NULL);
794 r = lzo1x_decompress_safe(b1, src_len, b2, &d, NULL);
796 if (r != 0 /*LZO_E_OK*/ || dst_len != d) {
797 bb_error_msg_and_die("corrupted compressed data");
801 /* "stored" block => no decompression */
805 if (!(option_mask32 & OPT_F)) {
806 /* verify checksum of uncompressed block */
807 if (h->flags & F_ADLER32_D)
808 lzo_check(lzo_adler32, d_adler32, ADLER32_INIT_VALUE,
810 if (h->flags & F_CRC32_D)
811 lzo_check(lzo_crc32, d_crc32, CRC32_INIT_VALUE,
815 /* write uncompressed block data */
816 xwrite(1, dst, dst_len);
823 /**********************************************************************/
824 // lzop file signature (shamelessly borrowed from PNG)
825 /**********************************************************************/
827 * The first nine bytes of a lzop file always contain the following values:
830 * --- --- --- --- --- --- --- --- ---
831 * (hex) 89 4c 5a 4f 00 0d 0a 1a 0a
832 * (decimal) 137 76 90 79 0 13 10 26 10
833 * (C notation - ASCII) \211 L Z O \0 \r \n \032 \n
836 /* (vda) comparison with lzop v1.02rc1 ("lzop -1 <FILE" cmd):
837 * Only slight differences in header:
838 * -00000000 89 4c 5a 4f 00 0d 0a 1a 0a 10 20 20 20 09 40 02
839 * +00000000 89 4c 5a 4f 00 0d 0a 1a 0a 10 10 20 30 09 40 02
841 * version lib_version
842 * -00000010 01 03 00 00 0d 00 00 81 a4 49 f7 a6 3f 00 00 00
843 * +00000010 01 03 00 00 01 00 00 00 00 00 00 00 00 00 00 00
844 * ^^^^^^^^^^^ ^^^^^^^^^^^ ^^^^^^^^^^^
846 * -00000020 00 00 2d 67 04 17 00 04 00 00 00 03 ed ec 9d 6d
847 * +00000020 00 00 10 5f 00 c1 00 04 00 00 00 03 ed ec 9d 6d
850 * The rest is identical.
852 static const unsigned char lzop_magic[9] = {
853 0x89, 0x4c, 0x5a, 0x4f, 0x00, 0x0d, 0x0a, 0x1a, 0x0a
856 /* This coding is derived from Alexander Lehmann's pngcheck code. */
857 static void check_magic(void)
859 unsigned char magic[sizeof(lzop_magic)];
860 xread(0, magic, sizeof(magic));
861 if (memcmp(magic, lzop_magic, sizeof(lzop_magic)) != 0)
862 bb_error_msg_and_die("bad magic number");
865 /**********************************************************************/
867 /**********************************************************************/
868 static void write_header(const header_t *h)
872 xwrite(1, lzop_magic, sizeof(lzop_magic));
874 init_chksum(&G.chksum_out);
876 f_write16(h->version);
877 f_write16(h->lib_version);
878 f_write16(h->version_needed_to_extract);
884 f_write32(h->gmtdiff);
886 l = (int) strlen(h->name);
891 f_write32(chksum_getresult(&G.chksum_out, h));
894 static int read_header(header_t *h)
900 memset(h, 0, sizeof(*h));
901 h->version_needed_to_extract = 0x0900; /* first lzop version */
904 init_chksum(&G.chksum_in);
906 h->version = f_read16();
907 if (h->version < 0x0900)
909 h->lib_version = f_read16();
910 if (h->version >= 0x0940) {
911 h->version_needed_to_extract = f_read16();
912 if (h->version_needed_to_extract > LZOP_VERSION)
914 if (h->version_needed_to_extract < 0x0900)
917 h->method = f_read8();
918 if (h->version >= 0x0940)
919 h->level = f_read8();
920 h->flags = f_read32();
921 if (h->flags & F_H_FILTER)
922 return 16; /* filter not supported */
923 h->mode = f_read32();
924 h->mtime = f_read32();
925 if (h->version >= 0x0940)
926 h->gmtdiff = f_read32();
933 checksum = chksum_getresult(&G.chksum_in, h);
934 h->header_checksum = f_read32();
935 if (h->header_checksum != checksum)
940 r = lzo_get_method(h);
944 /* check reserved flags */
945 if (h->flags & F_RESERVED)
948 /* skip extra field [not used yet] */
949 if (h->flags & F_H_EXTRA_FIELD) {
952 /* note: the checksum also covers the length */
953 init_chksum(&G.chksum_in);
954 h->extra_field_len = f_read32();
955 for (k = 0; k < h->extra_field_len; k++)
957 checksum = chksum_getresult(&G.chksum_in, h);
958 h->extra_field_checksum = f_read32();
959 if (h->extra_field_checksum != checksum)
966 static void p_header(header_t *h)
973 bb_error_msg_and_die("header_error %d", r);
976 /**********************************************************************/
978 /**********************************************************************/
979 static void lzo_set_method(header_t *h)
983 if (option_mask32 & OPT_1) {
984 h->method = M_LZO1X_1_15;
985 } else if (option_mask32 & OPT_789) {
986 #if ENABLE_LZOP_COMPR_HIGH
987 h->method = M_LZO1X_999;
988 if (option_mask32 & OPT_7)
990 else if (option_mask32 & OPT_8)
995 bb_error_msg_and_die("high compression not compiled in");
997 } else { /* levels 2..6 or none (defaults to level 3) */
998 h->method = M_LZO1X_1;
999 level = 5; /* levels 2-6 are actually the same */
1005 static smallint do_lzo_compress(void)
1010 memset(h, 0, sizeof(*h));
1014 h->version = (LZOP_VERSION & 0xffff);
1015 h->version_needed_to_extract = 0x0940;
1016 h->lib_version = lzo_version() & 0xffff;
1018 h->flags = (F_OS & F_OS_MASK) | (F_CS & F_CS_MASK);
1020 if (!(option_mask32 & OPT_F) || h->method == M_LZO1X_999) {
1021 h->flags |= F_ADLER32_D;
1022 if (option_mask32 & OPT_C)
1023 h->flags |= F_ADLER32_C;
1026 return lzo_compress(h);
1030 /**********************************************************************/
1032 /**********************************************************************/
1033 static smallint do_lzo_decompress(void)
1039 return lzo_decompress(&header);
1042 static char* make_new_name_lzop(char *filename)
1044 if (option_mask32 & OPT_DECOMPRESS) {
1045 char *extension = strrchr(filename, '.');
1046 if (!extension || strcmp(extension + 1, "lzo") != 0)
1047 return xasprintf("%s.out", filename);
1051 return xasprintf("%s.lzo", filename);
1054 static IF_DESKTOP(long long) int pack_lzop(unpack_info_t *info UNUSED_PARAM)
1056 if (option_mask32 & OPT_DECOMPRESS)
1057 return do_lzo_decompress();
1058 return do_lzo_compress();
1061 int lzop_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1062 int lzop_main(int argc UNUSED_PARAM, char **argv)
1064 getopt32(argv, OPTION_STRING);
1067 if (applet_name[4] == 'c')
1068 option_mask32 |= (OPT_STDOUT | OPT_DECOMPRESS);
1070 if (applet_name[0] == 'u')
1071 option_mask32 |= OPT_DECOMPRESS;
1073 G.lzo_crc32_table = crc32_filltable(NULL, 0);
1074 return bbunpack(argv, make_new_name_lzop, pack_lzop);