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 #if ENABLE_FEATURE_CLEAN_UP
120 BZ2_bzCompressEnd(strm);
125 static uint8_t level;
128 USE_DESKTOP(long long) int compressStream(void)
130 USE_DESKTOP(long long) int total;
132 bz_stream bzs; /* it's small */
136 #define wbuf (iobuf + IOBUF_SIZE)
138 iobuf = xmalloc(2 * IOBUF_SIZE);
140 BZ2_bzCompressInit(strm, level);
143 count = full_read(STDIN_FILENO, rbuf, IOBUF_SIZE);
145 bb_perror_msg("read error");
148 count = bz_write(strm, rbuf, count, wbuf);
150 bb_perror_msg(count < 0 ? "write error" : "short write");
155 total = bz_write_tail(strm, wbuf);
157 /* we had no error _only_ if count == 0 */
158 return count == 0 ? total : -1;
162 char* make_new_name_bzip2(char *filename)
164 return xasprintf("%s.bz2", filename);
167 int bzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
168 int bzip2_main(int argc, char **argv)
172 /* standard bzip2 flags
173 * -d --decompress force decompression
174 * -z --compress force compression
175 * -k --keep keep (don't delete) input files
176 * -f --force overwrite existing output files
177 * -t --test test compressed file integrity
178 * -c --stdout output to standard out
179 * -q --quiet suppress noncritical error messages
180 * -v --verbose be verbose (a 2nd -v gives more)
181 * -s --small use less memory (at most 2500k)
182 * -1 .. -9 set block size to 100k .. 900k
183 * --fast alias for -1
184 * --best alias for -9
187 opt_complementary = "s2"; /* -s means -2 (compatibility) */
188 /* Must match bbunzip's constants OPT_STDOUT, OPT_FORCE! */
189 opt = getopt32(argv, "cfv" USE_BUNZIP2("d") "123456789qzs" );
190 #if ENABLE_BUNZIP2 /* bunzip2_main may not be visible... */
192 return bunzip2_main(argc, argv);
197 opt = (uint8_t)opt; /* isolate bits for -1..-8 */
198 opt |= 0x100; /* if nothing else, assume -9 */
206 option_mask32 &= 0x7; /* ignore all except -cfv */
207 return bbunpack(argv, make_new_name_bzip2, compressStream);