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 /* This buys 6% speed for nearly 4k code */
13 /*#define FAST_GROUP6 1*/
17 #include "bz/bzlib_private.h"
19 #include "bz/blocksort.c"
21 #include "bz/compress.c"
22 #include "bz/crctable.c"
23 #include "bz/huffman.c"
24 #include "bz/randtable.c"
26 /* No point in being shy and having very small buffer here.
27 * bzip2 internal buffers are much bigger anyway, hundreds of kbytes.
28 * If iobuf is several pages long, malloc() may use mmap,
29 * making iobuf is page aligned and thus (maybe) have one memcpy less
30 * if kernel is clever enough.
37 * <0 on write errors (examine errno),
38 * >0 on short writes (errno == 0)
39 * 0 no error (entire input consume, gimme more)
40 * on "impossible" errors (internal bzip2 compressor bug) dies
43 ssize_t bz_write(bz_stream *strm, void* rbuf, ssize_t rlen, void *wbuf)
47 /* if (len == 0) return 0; */
49 strm->avail_in = rlen;
52 strm->avail_out = IOBUF_SIZE;
53 strm->next_out = wbuf;
55 ret = BZ2_bzCompress(strm, BZ_RUN);
57 bb_error_msg_and_die("internal error %d", ret);
59 n = IOBUF_SIZE - strm->avail_out;
61 /* short reads must have errno == 0 */
63 n2 = full_write(STDOUT_FILENO, wbuf, n);
68 if (strm->avail_in == 0)
74 /*---------------------------------------------------*/
76 USE_DESKTOP(long long) int bz_write_tail(bz_stream *strm, void *wbuf)
79 USE_DESKTOP(long long) int total;
83 strm->avail_out = IOBUF_SIZE;
84 strm->next_out = wbuf;
86 ret = BZ2_bzCompress(strm, BZ_FINISH);
87 if (ret != BZ_FINISH_OK && ret != BZ_STREAM_END)
88 bb_error_msg_and_die("internal error %d", ret);
90 n = IOBUF_SIZE - strm->avail_out;
92 n2 = full_write(STDOUT_FILENO, wbuf, n);
97 if (ret == BZ_STREAM_END)
101 total = 0 USE_DESKTOP( + strm->total_out );
103 BZ2_bzCompressEnd(strm);
109 USE_DESKTOP(long long) int compressStream(void)
111 USE_DESKTOP(long long) int total;
113 bz_stream bzs; /* it's small */
117 #define wbuf (iobuf + IOBUF_SIZE)
119 iobuf = xmalloc(2 * IOBUF_SIZE);
121 BZ2_bzCompressInit(strm, 9 /*blockSize100k*/);
124 count = full_read(STDIN_FILENO, rbuf, IOBUF_SIZE);
126 bb_perror_msg("read error");
129 count = bz_write(strm, rbuf, count, wbuf);
131 bb_perror_msg(count < 0 ? "write error" : "short write");
136 total = bz_write_tail(strm, wbuf);
138 /* we had no error _only_ if count == 0 */
139 return count == 0 ? total : -1;
143 char* make_new_name_bzip2(char *filename)
145 return xasprintf("%s.bz2", filename);
148 int bzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
149 int bzip2_main(int argc, char **argv)
153 /* Must match bbunzip's constants OPT_STDOUT, OPT_FORCE! */
154 opt = getopt32(argv, "cfv" USE_BUNZIP2("d") "q123456789" );
155 #if ENABLE_BUNZIP2 /* bunzip2_main may not be visible... */
157 return bunzip2_main(argc, argv);
159 option_mask32 &= 0x7; /* ignore -q, -0..9 */
160 //if (opt & 0x1) // -c
161 //if (opt & 0x2) // -f
162 //if (opt & 0x4) // -v
165 return bbunpack(argv, make_new_name_bzip2, compressStream);