2 * Copyright (C) 2007 Denys Vlasenko <vda.linux@googlemail.com>
4 * This file uses bzip2 library code which is written
5 * by Julian Seward <jseward@bzip.org>.
6 * See README and LICENSE files in bz/ directory for more information
7 * about bzip2 library code.
12 #define CONFIG_BZIP2_FEATURE_SPEED 1
15 * Compiled with gcc 4.2.1, run on Athlon 64 1800 MHz (512K L2 cache).
16 * Stock bzip2 is 26.4% slower than bbox bzip2 at SPEED 1
17 * (time to compress gcc-4.2.1.tar is 126.4% compared to bbox).
18 * At SPEED 5 difference is 32.7%.
20 * Test run of all CONFIG_BZIP2_FEATURE_SPEED values on a 11Mb text file:
22 * 0: 10828 4.145 4.146 4.148
23 * 1: 11097 3.845 3.860 3.861
24 * 2: 11392 3.763 3.767 3.768
25 * 3: 11892 3.722 3.724 3.727
26 * 4: 12740 3.637 3.640 3.644
27 * 5: 17273 3.497 3.509 3.509
32 /* Takes ~300 bytes, detects corruption caused by bad RAM etc */
33 #define BZ_LIGHT_DEBUG 0
37 #include "bz/bzlib_private.h"
39 #include "bz/blocksort.c"
41 #include "bz/compress.c"
42 #include "bz/huffman.c"
44 /* No point in being shy and having very small buffer here.
45 * bzip2 internal buffers are much bigger anyway, hundreds of kbytes.
46 * If iobuf is several pages long, malloc() may use mmap,
47 * making iobuf is page aligned and thus (maybe) have one memcpy less
48 * if kernel is clever enough.
55 * <0 on write errors (examine errno),
56 * >0 on short writes (errno == 0)
57 * 0 no error (entire input consumed, gimme more)
58 * on "impossible" errors (internal bzip2 compressor bug) dies
61 ssize_t bz_write(bz_stream *strm, void* rbuf, ssize_t rlen, void *wbuf)
65 strm->avail_in = rlen;
68 strm->avail_out = IOBUF_SIZE;
69 strm->next_out = wbuf;
71 ret = BZ2_bzCompress(strm, BZ_RUN);
73 bb_error_msg_and_die("internal error %d", ret);
75 n = IOBUF_SIZE - strm->avail_out;
77 /* short reads must have errno == 0 */
79 n2 = full_write(STDOUT_FILENO, wbuf, n);
84 if (strm->avail_in == 0)
90 /*---------------------------------------------------*/
92 USE_DESKTOP(long long) int bz_write_tail(bz_stream *strm, void *wbuf)
95 USE_DESKTOP(long long) int total;
99 strm->avail_out = IOBUF_SIZE;
100 strm->next_out = wbuf;
102 ret = BZ2_bzCompress(strm, BZ_FINISH);
103 if (ret != BZ_FINISH_OK && ret != BZ_STREAM_END)
104 bb_error_msg_and_die("internal error %d", ret);
106 n = IOBUF_SIZE - strm->avail_out;
108 n2 = full_write(STDOUT_FILENO, wbuf, n);
113 if (ret == BZ_STREAM_END)
117 total = 0 USE_DESKTOP( + strm->total_out );
119 BZ2_bzCompressEnd(strm);
125 USE_DESKTOP(long long) int compressStream(void)
127 USE_DESKTOP(long long) int total;
129 bz_stream bzs; /* it's small */
133 #define wbuf (iobuf + IOBUF_SIZE)
135 iobuf = xmalloc(2 * IOBUF_SIZE);
137 BZ2_bzCompressInit(strm, 9 /*blockSize100k*/);
140 count = full_read(STDIN_FILENO, rbuf, IOBUF_SIZE);
142 bb_perror_msg("read error");
145 count = bz_write(strm, rbuf, count, wbuf);
147 bb_perror_msg(count < 0 ? "write error" : "short write");
152 total = bz_write_tail(strm, wbuf);
154 /* we had no error _only_ if count == 0 */
155 return count == 0 ? total : -1;
159 char* make_new_name_bzip2(char *filename)
161 return xasprintf("%s.bz2", filename);
164 int bzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
165 int bzip2_main(int argc, char **argv)
169 /* Must match bbunzip's constants OPT_STDOUT, OPT_FORCE! */
170 opt = getopt32(argv, "cfv" USE_BUNZIP2("d") "q123456789" );
171 #if ENABLE_BUNZIP2 /* bunzip2_main may not be visible... */
173 return bunzip2_main(argc, argv);
175 option_mask32 &= 0x7; /* ignore -q, -0..9 */
176 //if (opt & 0x1) // -c
177 //if (opt & 0x2) // -f
178 //if (opt & 0x4) // -v
181 return bbunpack(argv, make_new_name_bzip2, compressStream);