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 /libbb | |
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 'libbb')
-rw-r--r-- | libbb/uuencode.c | 84 |
1 files changed, 81 insertions, 3 deletions
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 | ||