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 | |
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>
-rw-r--r-- | coreutils/uudecode.c | 118 | ||||
-rw-r--r-- | include/libbb.h | 3 | ||||
-rw-r--r-- | libbb/uuencode.c | 84 |
3 files changed, 192 insertions, 13 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 | } |
diff --git a/include/libbb.h b/include/libbb.h index 1b7c0b83a..ef4a34f07 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -2026,14 +2026,17 @@ char *percent_decode_in_place(char *str, int strict) FAST_FUNC; | |||
2026 | 2026 | ||
2027 | 2027 | ||
2028 | extern const char bb_uuenc_tbl_base64[] ALIGN1; | 2028 | extern const char bb_uuenc_tbl_base64[] ALIGN1; |
2029 | extern const char bb_uuenc_tbl_base32[] ALIGN1; | ||
2029 | extern const char bb_uuenc_tbl_std[] ALIGN1; | 2030 | extern const char bb_uuenc_tbl_std[] ALIGN1; |
2030 | void bb_uuencode(char *store, const void *s, int length, const char *tbl) FAST_FUNC; | 2031 | void bb_uuencode(char *store, const void *s, int length, const char *tbl) FAST_FUNC; |
2031 | enum { | 2032 | enum { |
2032 | BASE64_FLAG_UU_STOP = 0x100, | 2033 | BASE64_FLAG_UU_STOP = 0x100, |
2034 | BASE64_32 = 0x200, /* base32 */ | ||
2033 | /* Sign-extends to a value which never matches fgetc result: */ | 2035 | /* Sign-extends to a value which never matches fgetc result: */ |
2034 | BASE64_FLAG_NO_STOP_CHAR = 0x80, | 2036 | BASE64_FLAG_NO_STOP_CHAR = 0x80, |
2035 | }; | 2037 | }; |
2036 | const char *decode_base64(char **pp_dst, const char *src) FAST_FUNC; | 2038 | const char *decode_base64(char **pp_dst, const char *src) FAST_FUNC; |
2039 | const char *decode_base32(char **pp_dst, const char *src) FAST_FUNC; | ||
2037 | void read_base64(FILE *src_stream, FILE *dst_stream, int flags) FAST_FUNC; | 2040 | void read_base64(FILE *src_stream, FILE *dst_stream, int flags) FAST_FUNC; |
2038 | 2041 | ||
2039 | typedef struct md5_ctx_t { | 2042 | typedef struct md5_ctx_t { |
diff --git a/libbb/uuencode.c b/libbb/uuencode.c index d36b34f63..b4ee20c20 100644 --- a/libbb/uuencode.c +++ b/libbb/uuencode.c | |||
@@ -8,7 +8,8 @@ | |||
8 | */ | 8 | */ |
9 | #include "libbb.h" | 9 | #include "libbb.h" |
10 | 10 | ||
11 | /* Conversion table. for base 64 */ | 11 | /* Conversion tables */ |
12 | /* for base 64 */ | ||
12 | const char bb_uuenc_tbl_base64[65 + 1] ALIGN1 = { | 13 | const char bb_uuenc_tbl_base64[65 + 1] ALIGN1 = { |
13 | 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', | 14 | 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', |
14 | 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', | 15 | 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', |
@@ -21,7 +22,16 @@ const char bb_uuenc_tbl_base64[65 + 1] ALIGN1 = { | |||
21 | '=' /* termination character */, | 22 | '=' /* termination character */, |
22 | '\0' /* needed for uudecode.c only */ | 23 | '\0' /* needed for uudecode.c only */ |
23 | }; | 24 | }; |
24 | 25 | #if ENABLE_BASE32 | |
26 | const char bb_uuenc_tbl_base32[33 + 1] ALIGN1 = { | ||
27 | 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', | ||
28 | 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', | ||
29 | 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', | ||
30 | 'Y', 'Z', '2', '3', '4', '5', '6', '7', | ||
31 | '=', | ||
32 | '\0' | ||
33 | }; | ||
34 | #endif | ||
25 | const char bb_uuenc_tbl_std[65] ALIGN1 = { | 35 | const char bb_uuenc_tbl_std[65] ALIGN1 = { |
26 | '`', '!', '"', '#', '$', '%', '&', '\'', | 36 | '`', '!', '"', '#', '$', '%', '&', '\'', |
27 | '(', ')', '*', '+', ',', '-', '.', '/', | 37 | '(', ')', '*', '+', ',', '-', '.', '/', |
@@ -153,6 +163,68 @@ const char* FAST_FUNC decode_base64(char **pp_dst, const char *src) | |||
153 | return src_tail; | 163 | return src_tail; |
154 | } | 164 | } |
155 | 165 | ||
166 | #if ENABLE_BASE32 | ||
167 | const char* FAST_FUNC decode_base32(char **pp_dst, const char *src) | ||
168 | { | ||
169 | char *dst = *pp_dst; | ||
170 | const char *src_tail; | ||
171 | |||
172 | while (1) { | ||
173 | unsigned char five_bit[8]; | ||
174 | int count = 0; | ||
175 | |||
176 | /* Fetch up to eight 5-bit values */ | ||
177 | src_tail = src; | ||
178 | while (count < 8) { | ||
179 | char *table_ptr; | ||
180 | int ch; | ||
181 | |||
182 | /* Get next _valid_ character. | ||
183 | * bb_uuenc_tbl_base32[] contains this string: | ||
184 | * 0 1 2 3 | ||
185 | * 01234567890123456789012345678901 | ||
186 | * "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=" | ||
187 | */ | ||
188 | do { | ||
189 | ch = *src; | ||
190 | if (ch == '\0') { | ||
191 | if (count == 0) { | ||
192 | src_tail = src; | ||
193 | } | ||
194 | goto ret; | ||
195 | } | ||
196 | src++; | ||
197 | table_ptr = strchr(bb_uuenc_tbl_base32, toupper(ch)); | ||
198 | } while (!table_ptr); | ||
199 | |||
200 | /* Convert encoded character to decimal */ | ||
201 | ch = table_ptr - bb_uuenc_tbl_base32; | ||
202 | |||
203 | /* ch is 32 if char was '=', otherwise 0..31 */ | ||
204 | if (ch == 32) | ||
205 | break; | ||
206 | five_bit[count] = ch; | ||
207 | count++; | ||
208 | } | ||
209 | |||
210 | /* Transform 5-bit values to 8-bit ones */ | ||
211 | if (count > 1) // xxxxx xxx-- ----- ----- ----- ----- ----- ----- | ||
212 | *dst++ = five_bit[0] << 3 | five_bit[1] >> 2; | ||
213 | if (count > 3) // ----- ---xx xxxxx x---- ----- ----- ----- ----- | ||
214 | *dst++ = five_bit[1] << 6 | five_bit[2] << 1 | five_bit[3] >> 4; | ||
215 | if (count > 4) // ----- ----- ----- -xxxx xxxx- ----- ----- ----- | ||
216 | *dst++ = five_bit[3] << 4 | five_bit[4] >> 1; | ||
217 | if (count > 6) // ----- ----- ----- ----- ----x xxxxx xx--- ----- | ||
218 | *dst++ = five_bit[4] << 7 | five_bit[5] << 2 | five_bit[6] >> 3; | ||
219 | if (count > 7) // ----- ----- ----- ----- ----- ----- --xxx xxxxx | ||
220 | *dst++ = five_bit[6] << 5 | five_bit[7]; | ||
221 | } /* while (1) */ | ||
222 | ret: | ||
223 | *pp_dst = dst; | ||
224 | return src_tail; | ||
225 | } | ||
226 | #endif | ||
227 | |||
156 | /* | 228 | /* |
157 | * Decode base64 encoded stream. | 229 | * Decode base64 encoded stream. |
158 | * Can stop on EOF, specified char, or on uuencode-style "====" line: | 230 | * Can stop on EOF, specified char, or on uuencode-style "====" line: |
@@ -163,6 +235,7 @@ void FAST_FUNC read_base64(FILE *src_stream, FILE *dst_stream, int flags) | |||
163 | /* Note that EOF _can_ be passed as exit_char too */ | 235 | /* Note that EOF _can_ be passed as exit_char too */ |
164 | #define exit_char ((int)(signed char)flags) | 236 | #define exit_char ((int)(signed char)flags) |
165 | #define uu_style_end (flags & BASE64_FLAG_UU_STOP) | 237 | #define uu_style_end (flags & BASE64_FLAG_UU_STOP) |
238 | #define base32 (flags & BASE64_32) | ||
166 | 239 | ||
167 | /* uuencoded files have 61 byte lines. Use 64 byte buffer | 240 | /* uuencoded files have 61 byte lines. Use 64 byte buffer |
168 | * to process line at a time. | 241 | * to process line at a time. |
@@ -204,7 +277,12 @@ void FAST_FUNC read_base64(FILE *src_stream, FILE *dst_stream, int flags) | |||
204 | return; | 277 | return; |
205 | 278 | ||
206 | out_tail = out_buf; | 279 | out_tail = out_buf; |
207 | in_tail = decode_base64(&out_tail, in_buf); | 280 | #if ENABLE_BASE32 |
281 | if (base32) | ||
282 | in_tail = decode_base32(&out_tail, in_buf); | ||
283 | else | ||
284 | #endif | ||
285 | in_tail = decode_base64(&out_tail, in_buf); | ||
208 | 286 | ||
209 | fwrite(out_buf, (out_tail - out_buf), 1, dst_stream); | 287 | fwrite(out_buf, (out_tail - out_buf), 1, dst_stream); |
210 | 288 | ||