123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- #include "libbb.h"
- #define CONFIG_BZIP2_FEATURE_SPEED 1
- #define BZ_DEBUG 0
- #define BZ_LIGHT_DEBUG 0
- #include "bz/bzlib.h"
- #include "bz/bzlib_private.h"
- #include "bz/blocksort.c"
- #include "bz/bzlib.c"
- #include "bz/compress.c"
- #include "bz/huffman.c"
- enum {
- IOBUF_SIZE = 8 * 1024
- };
- static uint8_t level;
- static
- USE_DESKTOP(long long) int bz_write(bz_stream *strm, void* rbuf, ssize_t rlen, void *wbuf)
- {
- int n, n2, ret;
- strm->avail_in = rlen;
- strm->next_in = rbuf;
- while (1) {
- strm->avail_out = IOBUF_SIZE;
- strm->next_out = wbuf;
- ret = BZ2_bzCompress(strm, rlen ? BZ_RUN : BZ_FINISH);
- if (ret != BZ_RUN_OK
- && ret != BZ_FINISH_OK
- && ret != BZ_STREAM_END
- ) {
- bb_error_msg_and_die("internal error %d", ret);
- }
- n = IOBUF_SIZE - strm->avail_out;
- if (n) {
- n2 = full_write(STDOUT_FILENO, wbuf, n);
- if (n2 != n) {
- if (n2 >= 0)
- errno = 0;
- bb_perror_msg(n2 >= 0 ? "short write" : "write error");
- return -1;
- }
- }
- if (ret == BZ_STREAM_END)
- break;
- if (rlen && strm->avail_in == 0)
- break;
- }
- return 0 USE_DESKTOP( + strm->total_out );
- }
- static
- USE_DESKTOP(long long) int compressStream(void)
- {
- USE_DESKTOP(long long) int total;
- ssize_t count;
- bz_stream bzs;
- #define strm (&bzs)
- char *iobuf;
- #define rbuf iobuf
- #define wbuf (iobuf + IOBUF_SIZE)
- iobuf = xmalloc(2 * IOBUF_SIZE);
- BZ2_bzCompressInit(strm, level);
- while (1) {
- count = full_read(STDIN_FILENO, rbuf, IOBUF_SIZE);
- if (count < 0) {
- bb_perror_msg("read error");
- total = -1;
- break;
- }
-
- total = bz_write(strm, rbuf, count, wbuf);
- if (count == 0 || total < 0)
- break;
- }
- #if ENABLE_FEATURE_CLEAN_UP
- BZ2_bzCompressEnd(strm);
- free(iobuf);
- #endif
- return total;
- }
- static
- char* make_new_name_bzip2(char *filename)
- {
- return xasprintf("%s.bz2", filename);
- }
- int bzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int bzip2_main(int argc UNUSED_PARAM, char **argv)
- {
- unsigned opt;
-
- opt_complementary = "s2";
-
- opt = getopt32(argv, "cfv" USE_BUNZIP2("dt") "123456789qzs");
- #if ENABLE_BUNZIP2
- if (opt & 0x18)
- return bunzip2_main(argc, argv);
- opt >>= 5;
- #else
- opt >>= 3;
- #endif
- opt = (uint8_t)opt;
- opt |= 0x100;
- level = 1;
- while (!(opt & 1)) {
- level++;
- opt >>= 1;
- }
- argv += optind;
- option_mask32 &= 0x7;
- return bbunpack(argv, make_new_name_bzip2, compressStream);
- }
|