diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-10-17 10:14:11 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-10-17 10:14:11 +0000 |
commit | 31acd1ba0a091577983ec02b336fe6388e80a942 (patch) | |
tree | c7bab1849f82338703cbfc9f63ff8d70054684d3 | |
parent | 16e74b74648a8f013065fcc4356164c78d7f22d5 (diff) | |
download | busybox-w32-31acd1ba0a091577983ec02b336fe6388e80a942.tar.gz busybox-w32-31acd1ba0a091577983ec02b336fe6388e80a942.tar.bz2 busybox-w32-31acd1ba0a091577983ec02b336fe6388e80a942.zip |
bzip2: more of code shrink
compressStream 503 473 -30
BZ2_bzCompress 78 - -78
------------------------------------------------------------------------------
(add/remove: 0/1 grow/shrink: 0/1 up/down: 0/-108) Total: -108 bytes
text data bss dec hex filename
676300 2538 12104 690942 a8afe busybox_old
676192 2538 12104 690834 a8a92 busybox_unstripped
-rw-r--r-- | archival/bz/bzlib.c | 8 | ||||
-rw-r--r-- | archival/bzip2.c | 94 |
2 files changed, 38 insertions, 64 deletions
diff --git a/archival/bz/bzlib.c b/archival/bz/bzlib.c index cdb596cb1..9957c2fbd 100644 --- a/archival/bz/bzlib.c +++ b/archival/bz/bzlib.c | |||
@@ -76,11 +76,9 @@ void init_RL(EState* s) | |||
76 | 76 | ||
77 | 77 | ||
78 | static | 78 | static |
79 | Bool isempty_RL(EState* s) | 79 | int isempty_RL(EState* s) |
80 | { | 80 | { |
81 | if (s->state_in_ch < 256 && s->state_in_len > 0) | 81 | return (s->state_in_ch >= 256 || s->state_in_len <= 0); |
82 | return False; | ||
83 | return True; | ||
84 | } | 82 | } |
85 | 83 | ||
86 | 84 | ||
@@ -333,7 +331,7 @@ int BZ2_bzCompress(bz_stream *strm, int action) | |||
333 | } | 331 | } |
334 | 332 | ||
335 | #ifdef FLUSH_IS_UNUSED | 333 | #ifdef FLUSH_IS_UNUSED |
336 | case_BZ_M_FLUSHING: | 334 | case_BZ_M_FLUSHING: |
337 | case BZ_M_FLUSHING: | 335 | case BZ_M_FLUSHING: |
338 | /*if (s->avail_in_expect != s->strm->avail_in) | 336 | /*if (s->avail_in_expect != s->strm->avail_in) |
339 | return BZ_SEQUENCE_ERROR;*/ | 337 | return BZ_SEQUENCE_ERROR;*/ |
diff --git a/archival/bzip2.c b/archival/bzip2.c index 67df14416..76963365f 100644 --- a/archival/bzip2.c +++ b/archival/bzip2.c | |||
@@ -51,79 +51,55 @@ enum { | |||
51 | IOBUF_SIZE = 8 * 1024 | 51 | IOBUF_SIZE = 8 * 1024 |
52 | }; | 52 | }; |
53 | 53 | ||
54 | static uint8_t level; | ||
55 | |||
56 | /* NB: compressStream() has to return -1 on errors, not die. | ||
57 | * bbunpack() will correctly clean up in this case | ||
58 | * (delete incomplete .bz2 file) | ||
59 | */ | ||
60 | |||
54 | /* Returns: | 61 | /* Returns: |
55 | * <0 on write errors (examine errno), | 62 | * -1 on errors |
56 | * >0 on short writes (errno == 0) | 63 | * total written bytes so far otherwise |
57 | * 0 no error (entire input consumed, gimme more) | ||
58 | * on "impossible" errors (internal bzip2 compressor bug) dies | ||
59 | */ | 64 | */ |
60 | static | 65 | static |
61 | ssize_t bz_write(bz_stream *strm, void* rbuf, ssize_t rlen, void *wbuf) | 66 | USE_DESKTOP(long long) int bz_write(bz_stream *strm, void* rbuf, ssize_t rlen, void *wbuf) |
62 | { | 67 | { |
63 | int n, n2, ret; | 68 | int n, n2, ret; |
64 | 69 | ||
65 | strm->avail_in = rlen; | 70 | strm->avail_in = rlen; |
66 | strm->next_in = rbuf; | 71 | strm->next_in = rbuf; |
67 | while (1) { | 72 | while (1) { |
68 | strm->avail_out = IOBUF_SIZE; | 73 | strm->avail_out = IOBUF_SIZE; |
69 | strm->next_out = wbuf; | 74 | strm->next_out = wbuf; |
70 | 75 | ||
71 | ret = BZ2_bzCompress(strm, BZ_RUN); | 76 | ret = BZ2_bzCompress(strm, rlen ? BZ_RUN : BZ_FINISH); |
72 | if (ret != BZ_RUN_OK) | 77 | if (ret != BZ_RUN_OK /* BZ_RUNning */ |
78 | && ret != BZ_FINISH_OK /* BZ_FINISHing, but not done yet */ | ||
79 | && ret != BZ_STREAM_END /* BZ_FINISHed */ | ||
80 | ) { | ||
73 | bb_error_msg_and_die("internal error %d", ret); | 81 | 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 | } | 82 | } |
83 | 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; | 84 | n = IOBUF_SIZE - strm->avail_out; |
107 | if (n) { | 85 | if (n) { |
108 | n2 = full_write(STDOUT_FILENO, wbuf, n); | 86 | n2 = full_write(STDOUT_FILENO, wbuf, n); |
109 | if (n2 != n) | 87 | if (n2 != n) { |
110 | goto err; | 88 | if (n2 >= 0) |
89 | errno = 0; /* prevent bogus error message */ | ||
90 | bb_perror_msg(n2 >= 0 ? "short write" : "write error"); | ||
91 | return -1; | ||
92 | } | ||
111 | } | 93 | } |
112 | 94 | ||
113 | if (ret == BZ_STREAM_END) | 95 | if (ret == BZ_STREAM_END) |
114 | break; | 96 | break; |
97 | if (rlen && strm->avail_in == 0) | ||
98 | break; | ||
115 | } | 99 | } |
116 | 100 | return 0 USE_DESKTOP( + strm->total_out ); | |
117 | total = 0 USE_DESKTOP( + strm->total_out ); | ||
118 | err: | ||
119 | #if ENABLE_FEATURE_CLEAN_UP | ||
120 | BZ2_bzCompressEnd(strm); | ||
121 | #endif | ||
122 | return total; | ||
123 | } | 101 | } |
124 | 102 | ||
125 | static uint8_t level; | ||
126 | |||
127 | static | 103 | static |
128 | USE_DESKTOP(long long) int compressStream(void) | 104 | USE_DESKTOP(long long) int compressStream(void) |
129 | { | 105 | { |
@@ -136,26 +112,26 @@ USE_DESKTOP(long long) int compressStream(void) | |||
136 | #define wbuf (iobuf + IOBUF_SIZE) | 112 | #define wbuf (iobuf + IOBUF_SIZE) |
137 | 113 | ||
138 | iobuf = xmalloc(2 * IOBUF_SIZE); | 114 | iobuf = xmalloc(2 * IOBUF_SIZE); |
139 | |||
140 | BZ2_bzCompressInit(strm, level); | 115 | BZ2_bzCompressInit(strm, level); |
141 | 116 | ||
142 | while (1) { | 117 | while (1) { |
143 | count = full_read(STDIN_FILENO, rbuf, IOBUF_SIZE); | 118 | count = full_read(STDIN_FILENO, rbuf, IOBUF_SIZE); |
144 | if (count < 0) | 119 | if (count < 0) { |
145 | bb_perror_msg("read error"); | 120 | bb_perror_msg("read error"); |
146 | if (count <= 0) | 121 | total = -1; |
147 | break; | ||
148 | count = bz_write(strm, rbuf, count, wbuf); | ||
149 | if (count) { | ||
150 | bb_perror_msg(count < 0 ? "write error" : "short write"); | ||
151 | break; | 122 | break; |
152 | } | 123 | } |
124 | /* if count == 0, bz_write finalizes compression */ | ||
125 | total = bz_write(strm, rbuf, count, wbuf); | ||
126 | if (count == 0 || total < 0) | ||
127 | break; | ||
153 | } | 128 | } |
154 | 129 | ||
155 | total = bz_write_tail(strm, wbuf); | 130 | #if ENABLE_FEATURE_CLEAN_UP |
131 | BZ2_bzCompressEnd(strm); | ||
156 | free(iobuf); | 132 | free(iobuf); |
157 | /* we had no error _only_ if count == 0 */ | 133 | #endif |
158 | return count == 0 ? total : -1; | 134 | return total; |
159 | } | 135 | } |
160 | 136 | ||
161 | static | 137 | static |