aboutsummaryrefslogtreecommitdiff
path: root/archival/bzip2.c
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2018-02-01 01:00:58 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2018-02-01 01:00:58 +0100
commit1b8ac93a2010e85eaca959f520e00961a2bb5c65 (patch)
treec4acec7713237a3f4ca59b4810651a02d3c937e2 /archival/bzip2.c
parent54a174eb015e786e8097d6e0fdba3ebfe3b27a9d (diff)
downloadbusybox-w32-1b8ac93a2010e85eaca959f520e00961a2bb5c65.tar.gz
busybox-w32-1b8ac93a2010e85eaca959f520e00961a2bb5c65.tar.bz2
busybox-w32-1b8ac93a2010e85eaca959f520e00961a2bb5c65.zip
bzip2: code shrink, stop using global data variable
function old new delta compressStream 523 538 +15 level 1 - -1 bzip2_main 110 73 -37 ------------------------------------------------------------------------------ (add/remove: 0/1 grow/shrink: 1/1 up/down: 15/-38) Total: -23 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'archival/bzip2.c')
-rw-r--r--archival/bzip2.c32
1 files changed, 17 insertions, 15 deletions
diff --git a/archival/bzip2.c b/archival/bzip2.c
index d6fd9296d..c3969a9e8 100644
--- a/archival/bzip2.c
+++ b/archival/bzip2.c
@@ -83,15 +83,13 @@
83/* No point in being shy and having very small buffer here. 83/* No point in being shy and having very small buffer here.
84 * bzip2 internal buffers are much bigger anyway, hundreds of kbytes. 84 * bzip2 internal buffers are much bigger anyway, hundreds of kbytes.
85 * If iobuf is several pages long, malloc() may use mmap, 85 * If iobuf is several pages long, malloc() may use mmap,
86 * making iobuf is page aligned and thus (maybe) have one memcpy less 86 * making iobuf page aligned and thus (maybe) have one memcpy less
87 * if kernel is clever enough. 87 * if kernel is clever enough.
88 */ 88 */
89enum { 89enum {
90 IOBUF_SIZE = 8 * 1024 90 IOBUF_SIZE = 8 * 1024
91}; 91};
92 92
93static uint8_t level;
94
95/* NB: compressStream() has to return -1 on errors, not die. 93/* NB: compressStream() has to return -1 on errors, not die.
96 * bbunpack() will correctly clean up in this case 94 * bbunpack() will correctly clean up in this case
97 * (delete incomplete .bz2 file) 95 * (delete incomplete .bz2 file)
@@ -143,6 +141,7 @@ static
143IF_DESKTOP(long long) int FAST_FUNC compressStream(transformer_state_t *xstate UNUSED_PARAM) 141IF_DESKTOP(long long) int FAST_FUNC compressStream(transformer_state_t *xstate UNUSED_PARAM)
144{ 142{
145 IF_DESKTOP(long long) int total; 143 IF_DESKTOP(long long) int total;
144 unsigned opt, level;
146 ssize_t count; 145 ssize_t count;
147 bz_stream bzs; /* it's small */ 146 bz_stream bzs; /* it's small */
148#define strm (&bzs) 147#define strm (&bzs)
@@ -151,6 +150,16 @@ IF_DESKTOP(long long) int FAST_FUNC compressStream(transformer_state_t *xstate U
151#define wbuf (iobuf + IOBUF_SIZE) 150#define wbuf (iobuf + IOBUF_SIZE)
152 151
153 iobuf = xmalloc(2 * IOBUF_SIZE); 152 iobuf = xmalloc(2 * IOBUF_SIZE);
153
154 opt = option_mask32 >> (sizeof("cfkvq" IF_FEATURE_BZIP2_DECOMPRESS("dt") "zs") - 1);
155 opt |= 0x100; /* if nothing else, assume -9 */
156 level = 0;
157 for (;;) {
158 level++;
159 if (opt & 1) break;
160 opt >>= 1;
161 }
162
154 BZ2_bzCompressInit(strm, level); 163 BZ2_bzCompressInit(strm, level);
155 164
156 while (1) { 165 while (1) {
@@ -197,25 +206,18 @@ int bzip2_main(int argc UNUSED_PARAM, char **argv)
197 206
198 opt = getopt32(argv, "^" 207 opt = getopt32(argv, "^"
199 /* Must match bbunzip's constants OPT_STDOUT, OPT_FORCE! */ 208 /* Must match bbunzip's constants OPT_STDOUT, OPT_FORCE! */
200 "cfkv" IF_FEATURE_BZIP2_DECOMPRESS("dt") "123456789qzs" 209 "cfkvq" IF_FEATURE_BZIP2_DECOMPRESS("dt") "zs123456789"
201 "\0" "s2" /* -s means -2 (compatibility) */ 210 "\0" "s2" /* -s means -2 (compatibility) */
202 ); 211 );
203#if ENABLE_FEATURE_BZIP2_DECOMPRESS /* bunzip2_main may not be visible... */ 212#if ENABLE_FEATURE_BZIP2_DECOMPRESS /* bunzip2_main may not be visible... */
204 if (opt & 0x30) // -d and/or -t 213 if (opt & (3 << 5)) /* -d and/or -t */
205 return bunzip2_main(argc, argv); 214 return bunzip2_main(argc, argv);
206 opt >>= 6;
207#else 215#else
208 opt >>= 4; 216 /* clear "decompress" and "test" bits (or bbunpack() can get confused) */
217 /* in !BZIP2_DECOMPRESS config, these bits are -zs and are unused */
218 option_mask32 = opt & ~(3 << 5);
209#endif 219#endif
210 opt = (uint8_t)opt; /* isolate bits for -1..-8 */
211 opt |= 0x100; /* if nothing else, assume -9 */
212 level = 1;
213 while (!(opt & 1)) {
214 level++;
215 opt >>= 1;
216 }
217 220
218 argv += optind; 221 argv += optind;
219 option_mask32 &= 0xf; /* ignore all except -cfkv */
220 return bbunpack(argv, compressStream, append_ext, "bz2"); 222 return bbunpack(argv, compressStream, append_ext, "bz2");
221} 223}