bzip2: size reduction, to just below 9k.
[platform/upstream/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
124 static
125 USE_DESKTOP(long long) int compressStream(void)
126 {
127         USE_DESKTOP(long long) int total;
128         ssize_t count;
129         bz_stream bzs; /* it's small */
130 #define strm (&bzs)
131         char *iobuf;
132 #define rbuf iobuf
133 #define wbuf (iobuf + IOBUF_SIZE)
134
135         iobuf = xmalloc(2 * IOBUF_SIZE);
136
137         BZ2_bzCompressInit(strm, 9 /*blockSize100k*/);
138
139         while (1) {
140                 count = full_read(STDIN_FILENO, rbuf, IOBUF_SIZE);
141                 if (count < 0)
142                         bb_perror_msg("read error");
143                 if (count <= 0)
144                         break;
145                 count = bz_write(strm, rbuf, count, wbuf);
146                 if (count) {
147                         bb_perror_msg(count < 0 ? "write error" : "short write");
148                         break;
149                 }
150         }
151
152         total = bz_write_tail(strm, wbuf);
153         free(iobuf);
154         /* we had no error _only_ if count == 0 */
155         return count == 0 ? total : -1;
156 }
157
158 static
159 char* make_new_name_bzip2(char *filename)
160 {
161         return xasprintf("%s.bz2", filename);
162 }
163
164 int bzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
165 int bzip2_main(int argc, char **argv)
166 {
167         unsigned opt;
168
169         /* Must match bbunzip's constants OPT_STDOUT, OPT_FORCE! */
170         opt = getopt32(argv, "cfv" USE_BUNZIP2("d") "q123456789" );
171 #if ENABLE_BUNZIP2 /* bunzip2_main may not be visible... */
172         if (opt & 0x8) // -d
173                 return bunzip2_main(argc, argv);
174 #endif
175         option_mask32 &= 0x7; /* ignore -q, -0..9 */
176         //if (opt & 0x1) // -c
177         //if (opt & 0x2) // -f
178         //if (opt & 0x4) // -v
179         argv += optind;
180
181         return bbunpack(argv, make_new_name_bzip2, compressStream);
182 }