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