diff options
| author | Denys Vlasenko <vda.linux@googlemail.com> | 2020-11-27 20:44:55 +0100 |
|---|---|---|
| committer | Denys Vlasenko <vda.linux@googlemail.com> | 2020-11-27 20:45:15 +0100 |
| commit | 170b8628fabff2d81606cac052a35f8cf91cc7b2 (patch) | |
| tree | c9e02294eb2d1e82966a266ad06c8312ae676b51 | |
| parent | dc68a5ddac23e34fe00299d55501043bd83ae817 (diff) | |
| download | busybox-w32-170b8628fabff2d81606cac052a35f8cf91cc7b2.tar.gz busybox-w32-170b8628fabff2d81606cac052a35f8cf91cc7b2.tar.bz2 busybox-w32-170b8628fabff2d81606cac052a35f8cf91cc7b2.zip | |
libbb: smaller and faster decode_base64()
function old new delta
decode_base64 195 180 -15
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
| -rw-r--r-- | libbb/uuencode.c | 100 | ||||
| -rw-r--r-- | networking/httpd.c | 37 |
2 files changed, 36 insertions, 101 deletions
diff --git a/libbb/uuencode.c b/libbb/uuencode.c index b4ee20c20..2e9edb219 100644 --- a/libbb/uuencode.c +++ b/libbb/uuencode.c | |||
| @@ -82,7 +82,7 @@ void FAST_FUNC bb_uuencode(char *p, const void *src, int length, const char *tbl | |||
| 82 | } | 82 | } |
| 83 | 83 | ||
| 84 | /* | 84 | /* |
| 85 | * Decode base64 encoded string. Stops on '\0'. | 85 | * Decode base64 encoded string. Stops on NUL after terminating "=" or "==". |
| 86 | * | 86 | * |
| 87 | * Returns: pointer to the undecoded part of source. | 87 | * Returns: pointer to the undecoded part of source. |
| 88 | * If points to '\0', then the source was fully decoded. | 88 | * If points to '\0', then the source was fully decoded. |
| @@ -91,76 +91,48 @@ void FAST_FUNC bb_uuencode(char *p, const void *src, int length, const char *tbl | |||
| 91 | const char* FAST_FUNC decode_base64(char **pp_dst, const char *src) | 91 | const char* FAST_FUNC decode_base64(char **pp_dst, const char *src) |
| 92 | { | 92 | { |
| 93 | char *dst = *pp_dst; | 93 | char *dst = *pp_dst; |
| 94 | const char *src_tail; | 94 | unsigned ch = 0; |
| 95 | 95 | int i = 0; | |
| 96 | while (1) { | ||
| 97 | unsigned char six_bit[4]; | ||
| 98 | int count = 0; | ||
| 99 | 96 | ||
| 100 | /* Fetch up to four 6-bit values */ | 97 | while (*src) { |
| 101 | src_tail = src; | 98 | int t = (unsigned char)*src++; |
| 102 | while (count < 4) { | ||
| 103 | char *table_ptr; | ||
| 104 | int ch; | ||
| 105 | 99 | ||
| 106 | /* Get next _valid_ character. | 100 | /* "if" forest is faster than strchr(bb_uuenc_tbl_base64, t) */ |
| 107 | * bb_uuenc_tbl_base64[] contains this string: | 101 | if (t >= '0' && t <= '9') |
| 108 | * 0 1 2 3 4 5 6 | 102 | t = t - '0' + 52; |
| 109 | * 01234567890123456789012345678901234567890123456789012345678901234 | 103 | else if (t >= 'A' && t <= 'Z') |
| 110 | * "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=" | 104 | t = t - 'A'; |
| 111 | */ | 105 | else if (t >= 'a' && t <= 'z') |
| 112 | do { | 106 | t = t - 'a' + 26; |
| 113 | ch = *src; | 107 | else if (t == '+') |
| 114 | if (ch == '\0') { | 108 | t = 62; |
| 115 | if (count == 0) { | 109 | else if (t == '/') |
| 116 | /* Example: | 110 | t = 63; |
| 117 | * If we decode "QUJD <NUL>", we want | 111 | else if (t == '=' && (i == 3 || (i == 2 && *src == '='))) |
| 118 | * to return ptr to NUL, not to ' ', | 112 | /* the above disallows "==AA", "A===", "AA=A" etc */ |
| 119 | * because we did fully decode | 113 | t = 0x1000000; |
| 120 | * the string (to "ABC"). | 114 | else |
| 121 | */ | ||
| 122 | src_tail = src; | ||
| 123 | } | ||
| 124 | goto ret; | ||
| 125 | } | ||
| 126 | src++; | ||
| 127 | table_ptr = strchr(bb_uuenc_tbl_base64, ch); | ||
| 128 | //TODO: add BASE64_FLAG_foo to die on bad char? | 115 | //TODO: add BASE64_FLAG_foo to die on bad char? |
| 129 | } while (!table_ptr); | 116 | continue; |
| 130 | |||
| 131 | /* Convert encoded character to decimal */ | ||
| 132 | ch = table_ptr - bb_uuenc_tbl_base64; | ||
| 133 | 117 | ||
| 134 | /* ch is 64 if char was '=', otherwise 0..63 */ | 118 | ch = (ch << 6) | t; |
| 135 | if (ch == 64) | 119 | if (++i == 4) { |
| 120 | *dst++ = (char) (ch >> 16); | ||
| 121 | *dst++ = (char) (ch >> 8); | ||
| 122 | *dst++ = (char) ch; | ||
| 123 | i = 0; | ||
| 124 | if (ch & 0x1000000) { /* was last input char '='? */ | ||
| 125 | dst--; | ||
| 126 | if (ch & (0x1000000 << 6)) /* was it "=="? */ | ||
| 127 | dst--; | ||
| 136 | break; | 128 | break; |
| 137 | six_bit[count] = ch; | 129 | } |
| 138 | count++; | 130 | ch = 0; |
| 139 | } | 131 | } |
| 140 | 132 | } | |
| 141 | /* Transform 6-bit values to 8-bit ones. | ||
| 142 | * count can be < 4 when we decode the tail: | ||
| 143 | * "eQ==" -> "y", not "y NUL NUL". | ||
| 144 | * Note that (count > 1) is always true, | ||
| 145 | * "x===" encoding is not valid: | ||
| 146 | * even a single zero byte encodes as "AA==". | ||
| 147 | * However, with current logic we come here with count == 1 | ||
| 148 | * when we decode "==" tail. | ||
| 149 | */ | ||
| 150 | if (count > 1) | ||
| 151 | *dst++ = six_bit[0] << 2 | six_bit[1] >> 4; | ||
| 152 | if (count > 2) | ||
| 153 | *dst++ = six_bit[1] << 4 | six_bit[2] >> 2; | ||
| 154 | if (count > 3) | ||
| 155 | *dst++ = six_bit[2] << 6 | six_bit[3]; | ||
| 156 | /* Note that if we decode "AA==" and ate first '=', | ||
| 157 | * we just decoded one char (count == 2) and now we'll | ||
| 158 | * do the loop once more to decode second '='. | ||
| 159 | */ | ||
| 160 | } /* while (1) */ | ||
| 161 | ret: | ||
| 162 | *pp_dst = dst; | 133 | *pp_dst = dst; |
| 163 | return src_tail; | 134 | /* i should be zero here if full 4-char block was decoded */ |
| 135 | return src - i; /* -i rejects truncations: e.g. "MQ" and "MQ=" (correct encoding is "MQ==" -> "1") */ | ||
| 164 | } | 136 | } |
| 165 | 137 | ||
| 166 | #if ENABLE_BASE32 | 138 | #if ENABLE_BASE32 |
diff --git a/networking/httpd.c b/networking/httpd.c index 961f8cab4..4ffd89c48 100644 --- a/networking/httpd.c +++ b/networking/httpd.c | |||
| @@ -1017,46 +1017,9 @@ static char *encodeString(const char *string) | |||
| 1017 | */ | 1017 | */ |
| 1018 | static void decodeBase64(char *Data) | 1018 | static void decodeBase64(char *Data) |
| 1019 | { | 1019 | { |
| 1020 | # if ENABLE_BASE64 || ENABLE_UUDECODE | ||
| 1021 | /* Call decode_base64() from uuencode.c */ | ||
| 1022 | char *eptr = Data; | 1020 | char *eptr = Data; |
| 1023 | decode_base64(&eptr, Data); | 1021 | decode_base64(&eptr, Data); |
| 1024 | *eptr = '\0'; | 1022 | *eptr = '\0'; |
| 1025 | # else | ||
| 1026 | const unsigned char *in = (const unsigned char *)Data; | ||
| 1027 | /* The decoded size will be at most 3/4 the size of the encoded */ | ||
| 1028 | unsigned ch = 0; | ||
| 1029 | int i = 0; | ||
| 1030 | |||
| 1031 | while (*in) { | ||
| 1032 | int t = *in++; | ||
| 1033 | |||
| 1034 | if (t >= '0' && t <= '9') | ||
| 1035 | t = t - '0' + 52; | ||
| 1036 | else if (t >= 'A' && t <= 'Z') | ||
| 1037 | t = t - 'A'; | ||
| 1038 | else if (t >= 'a' && t <= 'z') | ||
| 1039 | t = t - 'a' + 26; | ||
| 1040 | else if (t == '+') | ||
| 1041 | t = 62; | ||
| 1042 | else if (t == '/') | ||
| 1043 | t = 63; | ||
| 1044 | else if (t == '=') | ||
| 1045 | t = 0; | ||
| 1046 | else | ||
| 1047 | continue; | ||
| 1048 | |||
| 1049 | ch = (ch << 6) | t; | ||
| 1050 | i++; | ||
| 1051 | if (i == 4) { | ||
| 1052 | *Data++ = (char) (ch >> 16); | ||
| 1053 | *Data++ = (char) (ch >> 8); | ||
| 1054 | *Data++ = (char) ch; | ||
| 1055 | i = 0; | ||
| 1056 | } | ||
| 1057 | } | ||
| 1058 | *Data = '\0'; | ||
| 1059 | # endif | ||
| 1060 | } | 1023 | } |
| 1061 | #endif | 1024 | #endif |
| 1062 | 1025 | ||
