bzip2: implement -1..-9 command line flags
[oweals/busybox.git] / archival / bzip2.c
1 /*
2  * Copyright (C) 2007 Denys Vlasenko <vda.linux@googlemail.com>
3  *
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.
8  */
9
10 #include "libbb.h"
11
12 #define CONFIG_BZIP2_FEATURE_SPEED 1
13
14 /* Speed test:
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%.
19  *
20  * Test run of all CONFIG_BZIP2_FEATURE_SPEED values on a 11Mb text file:
21  *     Size   Time (3 runs)
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
28  */
29
30
31 #define BZ_DEBUG 0
32 /* Takes ~300 bytes, detects corruption caused by bad RAM etc */
33 #define BZ_LIGHT_DEBUG 0
34
35 #include "bz/bzlib.h"
36
37 #include "bz/bzlib_private.h"
38
39 #include "bz/blocksort.c"
40 #include "bz/bzlib.c"
41 #include "bz/compress.c"
42 #include "bz/huffman.c"
43
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.
49  */
50 enum {
51         IOBUF_SIZE = 8 * 1024
52 };
53
54 /* Returns:
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
59  */
60 static
61 ssize_t bz_write(bz_stream *strm, void* rbuf, ssize_t rlen, void *wbuf)
62 {
63         int n, n2, ret;
64
65         strm->avail_in = rlen;
66         strm->next_in  = rbuf;
67         while (1) {
68                 strm->avail_out = IOBUF_SIZE;
69                 strm->next_out = wbuf;
70
71                 ret = BZ2_bzCompress(strm, BZ_RUN);
72                 if (ret != BZ_RUN_OK)
73                         bb_error_msg_and_die("internal error %d", ret);
74
75                 n = IOBUF_SIZE - strm->avail_out;
76                 if (n) {
77                         /* short reads must have errno == 0 */
78                         errno = 0;
79                         n2 = full_write(STDOUT_FILENO, wbuf, n);
80                         if (n2 != n)
81                                 return n2 ? n2 : 1;
82                 }
83
84                 if (strm->avail_in == 0)
85                         return 0;
86         }
87 }
88
89
90 /*---------------------------------------------------*/
91 static
92 USE_DESKTOP(long long) int bz_write_tail(bz_stream *strm, void *wbuf)
93 {
94         int n, n2, ret;
95         USE_DESKTOP(long long) int total;
96
97         total = -1;
98         while (1) {
99                 strm->avail_out = IOBUF_SIZE;
100                 strm->next_out = wbuf;
101
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);
105
106                 n = IOBUF_SIZE - strm->avail_out;
107                 if (n) {
108                         n2 = full_write(STDOUT_FILENO, wbuf, n);
109                         if (n2 != n)
110                                 goto err;
111                 }
112
113                 if (ret == BZ_STREAM_END)
114                         break;
115         }
116
117         total = 0 USE_DESKTOP( + strm->total_out );
118  err:
119         BZ2_bzCompressEnd(strm);
120         return total;
121 }
122
123 static uint8_t level;
124
125 static
126 USE_DESKTOP(long long) int compressStream(void)
127 {
128         USE_DESKTOP(long long) int total;
129         ssize_t count;
130         bz_stream bzs; /* it's small */
131 #define strm (&bzs)
132         char *iobuf;
133 #define rbuf iobuf
134 #define wbuf (iobuf + IOBUF_SIZE)
135
136         iobuf = xmalloc(2 * IOBUF_SIZE);
137
138         BZ2_bzCompressInit(strm, level);
139
140         while (1) {
141                 count = full_read(STDIN_FILENO, rbuf, IOBUF_SIZE);
142                 if (count < 0)
143                         bb_perror_msg("read error");
144                 if (count <= 0)
145                         break;
146                 count = bz_write(strm, rbuf, count, wbuf);
147                 if (count) {
148                         bb_perror_msg(count < 0 ? "write error" : "short write");
149                         break;
150                 }
151         }
152
153         total = bz_write_tail(strm, wbuf);
154         free(iobuf);
155         /* we had no error _only_ if count == 0 */
156         return count == 0 ? total : -1;
157 }
158
159 static
160 char* make_new_name_bzip2(char *filename)
161 {
162         return xasprintf("%s.bz2", filename);
163 }
164
165 int bzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
166 int bzip2_main(int argc, char **argv)
167 {
168         unsigned opt;
169
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
183          */
184
185         /* Must match bbunzip's constants OPT_STDOUT, OPT_FORCE! */
186         opt = getopt32(argv, "cfv" USE_BUNZIP2("d") "123456789qzs" );
187 #if ENABLE_BUNZIP2 /* bunzip2_main may not be visible... */
188         if (opt & 0x8) // -d
189                 return bunzip2_main(argc, argv);
190         opt >>= 4;
191 #else
192         opt >>= 3;
193 #endif
194         opt = (uint8_t)opt; /* isolate bits for -1..-8 */
195         opt |= 0x100; /* if nothing else, assume -9 */
196         level = 1;
197         while (!(opt & 1)) {
198                 level++;
199                 opt >>= 1;
200         }
201
202         argv += optind;
203         option_mask32 &= 0x7; /* ignore all except -cfv */
204         return bbunpack(argv, make_new_name_bzip2, compressStream);
205 }