diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2020-11-25 22:47:00 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2020-11-26 09:04:16 +0100 |
commit | 20900489a1fff2a563999d90e24239206f202b21 (patch) | |
tree | 2acc4efd7d0ef9b2468f73b0b6861aca214a59da /coreutils | |
parent | 03eb6eba436ca6198e5346ebb9d22a30d2f527a4 (diff) | |
download | busybox-w32-20900489a1fff2a563999d90e24239206f202b21.tar.gz busybox-w32-20900489a1fff2a563999d90e24239206f202b21.tar.bz2 busybox-w32-20900489a1fff2a563999d90e24239206f202b21.zip |
base32: new applet
function old new delta
baseNUM_main - 568 +568
decode_base32 - 275 +275
bb_uuenc_tbl_base32 - 34 +34
read_base64 218 236 +18
applet_names 2732 2739 +7
applet_main 1580 1584 +4
packed_usage 33480 33478 -2
base64_main 208 - -208
------------------------------------------------------------------------------
(add/remove: 3/1 grow/shrink: 3/1 up/down: 906/-210) Total: 696 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'coreutils')
-rw-r--r-- | coreutils/uudecode.c | 118 |
1 files changed, 108 insertions, 10 deletions
diff --git a/coreutils/uudecode.c b/coreutils/uudecode.c index 5b2edd649..e4fb0d48b 100644 --- a/coreutils/uudecode.c +++ b/coreutils/uudecode.c | |||
@@ -168,9 +168,11 @@ int uudecode_main(int argc UNUSED_PARAM, char **argv) | |||
168 | } | 168 | } |
169 | #endif | 169 | #endif |
170 | 170 | ||
171 | //applet:IF_BASE64(APPLET(base64, BB_DIR_BIN, BB_SUID_DROP)) | 171 | //config:config BASE32 |
172 | 172 | //config: bool "base32 (4.9 kb)" | |
173 | //kbuild:lib-$(CONFIG_BASE64) += uudecode.o | 173 | //config: default y |
174 | //config: help | ||
175 | //config: Base32 encode and decode | ||
174 | 176 | ||
175 | //config:config BASE64 | 177 | //config:config BASE64 |
176 | //config: bool "base64 (4.9 kb)" | 178 | //config: bool "base64 (4.9 kb)" |
@@ -178,6 +180,14 @@ int uudecode_main(int argc UNUSED_PARAM, char **argv) | |||
178 | //config: help | 180 | //config: help |
179 | //config: Base64 encode and decode | 181 | //config: Base64 encode and decode |
180 | 182 | ||
183 | //usage:#define base32_trivial_usage | ||
184 | //usage: "[-d] [FILE]" | ||
185 | //usage:#define base32_full_usage "\n\n" | ||
186 | //usage: "Base32 encode or decode FILE to standard output" | ||
187 | //usage: "\n -d Decode data" | ||
188 | ////usage: "\n -w COL Wrap lines at COL (default 76, 0 disables)" | ||
189 | ////usage: "\n -i When decoding, ignore non-alphabet characters" | ||
190 | |||
181 | //usage:#define base64_trivial_usage | 191 | //usage:#define base64_trivial_usage |
182 | //usage: "[-d] [FILE]" | 192 | //usage: "[-d] [FILE]" |
183 | //usage:#define base64_full_usage "\n\n" | 193 | //usage:#define base64_full_usage "\n\n" |
@@ -186,9 +196,77 @@ int uudecode_main(int argc UNUSED_PARAM, char **argv) | |||
186 | ////usage: "\n -w COL Wrap lines at COL (default 76, 0 disables)" | 196 | ////usage: "\n -w COL Wrap lines at COL (default 76, 0 disables)" |
187 | ////usage: "\n -i When decoding, ignore non-alphabet characters" | 197 | ////usage: "\n -i When decoding, ignore non-alphabet characters" |
188 | 198 | ||
189 | #if ENABLE_BASE64 | 199 | // APPLET_ODDNAME:name main location suid_type help |
190 | int base64_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 200 | //applet:IF_BASE32(APPLET_ODDNAME(base32, baseNUM, BB_DIR_BIN, BB_SUID_DROP, base32)) |
191 | int base64_main(int argc UNUSED_PARAM, char **argv) | 201 | //applet:IF_BASE64(APPLET_ODDNAME(base64, baseNUM, BB_DIR_BIN, BB_SUID_DROP, base64)) |
202 | |||
203 | //kbuild:lib-$(CONFIG_BASE64) += uudecode.o | ||
204 | //kbuild:lib-$(CONFIG_BASE32) += uudecode.o | ||
205 | |||
206 | #if ENABLE_BASE32 || ENABLE_BASE64 | ||
207 | |||
208 | # if ENABLE_BASE32 | ||
209 | static void bb_b32encode(char *p, const void *src, int length) | ||
210 | { | ||
211 | #define tbl bb_uuenc_tbl_base32 | ||
212 | const unsigned char *s = src; | ||
213 | |||
214 | /* Transform 5x8 bits to 8x5 bits */ | ||
215 | while (length > 0) { | ||
216 | unsigned cur, next; | ||
217 | |||
218 | length--; | ||
219 | cur = *s++; | ||
220 | *p++ = tbl[cur >> 3]; // xxxxx--- -------- -------- -------- -------- | ||
221 | cur &= 7; | ||
222 | |||
223 | next = 0; | ||
224 | if (--length >= 0) | ||
225 | next = *s++; | ||
226 | *p++ = tbl[(cur << 2) + (next >> 6)]; // -----xxx xx------ -------- -------- -------- | ||
227 | cur = next & 0x3f; | ||
228 | |||
229 | *p++ = tbl[cur >> 1]; // -------- --xxxxx- -------- -------- -------- | ||
230 | cur &= 1; | ||
231 | |||
232 | next = 0; | ||
233 | if (--length >= 0) | ||
234 | next = *s++; | ||
235 | *p++ = tbl[(cur << 4) + (next >> 4)]; // -------- -------x xxxx---- -------- -------- | ||
236 | cur = next & 0xf; | ||
237 | |||
238 | next = 0; | ||
239 | if (--length >= 0) | ||
240 | next = *s++; | ||
241 | *p++ = tbl[(cur << 1) + (next >> 7)]; // -------- -------- ----xxxx x------- -------- | ||
242 | cur = next & 0x7f; | ||
243 | |||
244 | *p++ = tbl[cur >> 2]; // -------- -------- -------- -xxxxx-- -------- | ||
245 | cur &= 3; | ||
246 | |||
247 | next = 0; | ||
248 | if (--length >= 0) | ||
249 | next = *s++; | ||
250 | *p++ = tbl[(cur << 3) + (next >> 5)]; // -------- -------- -------- ------xx xxx----- | ||
251 | cur = next & 0x1f; | ||
252 | |||
253 | *p++ = tbl[cur]; // -------- -------- -------- -------- ---xxxxx | ||
254 | } | ||
255 | #undef tbl | ||
256 | /* Zero-terminate */ | ||
257 | *p = '\0'; | ||
258 | /* Pad as necessary */ | ||
259 | length = ((-length) * 3) >> 1; /* -4 => 6 pad chars, -3 => 4, -2 => 3, -1 => 1 */ | ||
260 | while (length--) { | ||
261 | *--p = '='; | ||
262 | } | ||
263 | } | ||
264 | # else | ||
265 | void bb_b32encode(char *p, const void *src, int length); /* undefined */ | ||
266 | # endif | ||
267 | |||
268 | int baseNUM_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | ||
269 | int baseNUM_main(int argc UNUSED_PARAM, char **argv) | ||
192 | { | 270 | { |
193 | FILE *src_stream; | 271 | FILE *src_stream; |
194 | unsigned opts; | 272 | unsigned opts; |
@@ -200,7 +278,10 @@ int base64_main(int argc UNUSED_PARAM, char **argv) | |||
200 | *--argv = (char*)"-"; | 278 | *--argv = (char*)"-"; |
201 | src_stream = xfopen_stdin(argv[0]); | 279 | src_stream = xfopen_stdin(argv[0]); |
202 | if (opts) { | 280 | if (opts) { |
203 | read_base64(src_stream, stdout, /*flags:*/ (unsigned char)EOF); | 281 | int flags = (unsigned char)EOF; |
282 | if (ENABLE_BASE32 && (!ENABLE_BASE64 || applet_name[4] == '3')) | ||
283 | flags = ((unsigned char)EOF) | BASE64_32; | ||
284 | read_base64(src_stream, stdout, flags); | ||
204 | } else { | 285 | } else { |
205 | enum { | 286 | enum { |
206 | SRC_BUF_SIZE = 76 / 4 * 3, /* this *MUST* be a multiple of 3 */ | 287 | SRC_BUF_SIZE = 76 / 4 * 3, /* this *MUST* be a multiple of 3 */ |
@@ -210,14 +291,31 @@ int base64_main(int argc UNUSED_PARAM, char **argv) | |||
210 | char dst_buf[DST_BUF_SIZE + 1]; | 291 | char dst_buf[DST_BUF_SIZE + 1]; |
211 | int src_fd = fileno(src_stream); | 292 | int src_fd = fileno(src_stream); |
212 | while (1) { | 293 | while (1) { |
213 | size_t size = full_read(src_fd, src_buf, SRC_BUF_SIZE); | 294 | size_t size; |
295 | if (ENABLE_BASE32 && (!ENABLE_BASE64 || applet_name[4] == '3')) | ||
296 | size = 72 / 8 * 5; | ||
297 | //FIXME: wrong, default width of base32 is not 72, but 76 chars | ||
298 | //(not a multiple of 8 - requires adding wrapping logic) | ||
299 | //when this is fixed, can implement -w COL too | ||
300 | else | ||
301 | size = SRC_BUF_SIZE; | ||
302 | |||
303 | size = full_read(src_fd, src_buf, size); | ||
214 | if (!size) | 304 | if (!size) |
215 | break; | 305 | break; |
216 | if ((ssize_t)size < 0) | 306 | if ((ssize_t)size < 0) |
217 | bb_simple_perror_msg_and_die(bb_msg_read_error); | 307 | bb_simple_perror_msg_and_die(bb_msg_read_error); |
308 | |||
218 | /* Encode the buffer we just read in */ | 309 | /* Encode the buffer we just read in */ |
219 | bb_uuencode(dst_buf, src_buf, size, bb_uuenc_tbl_base64); | 310 | if (ENABLE_BASE32 && (!ENABLE_BASE64 || applet_name[4] == '3')) { |
220 | xwrite(STDOUT_FILENO, dst_buf, 4 * ((size + 2) / 3)); | 311 | bb_b32encode(dst_buf, src_buf, size); |
312 | size = 8 * ((size + 4) / 5); | ||
313 | } else { | ||
314 | bb_uuencode(dst_buf, src_buf, size, bb_uuenc_tbl_base64); | ||
315 | size = 4 * ((size + 2) / 3); | ||
316 | } | ||
317 | |||
318 | xwrite(STDOUT_FILENO, dst_buf, size); | ||
221 | bb_putchar('\n'); | 319 | bb_putchar('\n'); |
222 | fflush(stdout); | 320 | fflush(stdout); |
223 | } | 321 | } |