aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2020-11-27 21:25:34 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2020-11-27 21:25:34 +0100
commit2cd37d65e221f7267e97360d21f55a2318b25355 (patch)
treef126ad654f2c58e3f46c65d369ffd236bce36513
parent170b8628fabff2d81606cac052a35f8cf91cc7b2 (diff)
downloadbusybox-w32-2cd37d65e221f7267e97360d21f55a2318b25355.tar.gz
busybox-w32-2cd37d65e221f7267e97360d21f55a2318b25355.tar.bz2
busybox-w32-2cd37d65e221f7267e97360d21f55a2318b25355.zip
libbb: faster and smaller decode_base32()
function old new delta decode_base32 275 224 -51 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--libbb/uuencode.c84
1 files changed, 34 insertions, 50 deletions
diff --git a/libbb/uuencode.c b/libbb/uuencode.c
index 2e9edb219..7c7f1cf1c 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 NUL after terminating "=" or "==". 85 * Decode base64 encoded string.
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.
@@ -139,61 +139,45 @@ const char* FAST_FUNC decode_base64(char **pp_dst, const char *src)
139const char* FAST_FUNC decode_base32(char **pp_dst, const char *src) 139const char* FAST_FUNC decode_base32(char **pp_dst, const char *src)
140{ 140{
141 char *dst = *pp_dst; 141 char *dst = *pp_dst;
142 const char *src_tail; 142 uint64_t ch = 0;
143 143 int i = 0;
144 while (1) {
145 unsigned char five_bit[8];
146 int count = 0;
147
148 /* Fetch up to eight 5-bit values */
149 src_tail = src;
150 while (count < 8) {
151 char *table_ptr;
152 int ch;
153 144
154 /* Get next _valid_ character. 145 while (*src) {
155 * bb_uuenc_tbl_base32[] contains this string: 146 int t = (unsigned char)*src++;
156 * 0 1 2 3
157 * 01234567890123456789012345678901
158 * "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567="
159 */
160 do {
161 ch = *src;
162 if (ch == '\0') {
163 if (count == 0) {
164 src_tail = src;
165 }
166 goto ret;
167 }
168 src++;
169 table_ptr = strchr(bb_uuenc_tbl_base32, toupper(ch));
170 } while (!table_ptr);
171 147
172 /* Convert encoded character to decimal */ 148 /* "if" forest is faster than strchr(bb_uuenc_tbl_base32, t) */
173 ch = table_ptr - bb_uuenc_tbl_base32; 149 if (t >= '2' && t <= '7')
150 t = t - '2' + 26;
151 else if ((t|0x20) >= 'a' && (t|0x20) <= 'z')
152 t = (t|0x20) - 'a';
153 else if (t == '=' && i > 1)
154 t = 0;
155 else
156//TODO: add BASE64_FLAG_foo to die on bad char?
157 continue;
174 158
175 /* ch is 32 if char was '=', otherwise 0..31 */ 159 ch = (ch << 5) | t;
176 if (ch == 32) 160 if (++i == 8) {
161 *dst++ = (char) (ch >> 32);
162 *dst++ = (char) (ch >> 24);
163 *dst++ = (char) (ch >> 16);
164 *dst++ = (char) (ch >> 8);
165 *dst++ = (char) ch;
166 if (t == 0 && src[-1] == '=') { /* was last input char '='? */
167 const char *s = src;
168 while (*--s == '=' && --i != 0)
169 continue;
170 i = 8 - i; /* count of =, must be 1, 3, 4 or 6 */
171 dst -= (i+1) * 2 / 3; /* discard last 1, 2, 3 or 4 bytes */
172 i = 0;
177 break; 173 break;
178 five_bit[count] = ch; 174 }
179 count++; 175 i = 0;
180 } 176 }
181 177 }
182 /* Transform 5-bit values to 8-bit ones */
183 if (count > 1) // xxxxx xxx-- ----- ----- ----- ----- ----- -----
184 *dst++ = five_bit[0] << 3 | five_bit[1] >> 2;
185 if (count > 3) // ----- ---xx xxxxx x---- ----- ----- ----- -----
186 *dst++ = five_bit[1] << 6 | five_bit[2] << 1 | five_bit[3] >> 4;
187 if (count > 4) // ----- ----- ----- -xxxx xxxx- ----- ----- -----
188 *dst++ = five_bit[3] << 4 | five_bit[4] >> 1;
189 if (count > 6) // ----- ----- ----- ----- ----x xxxxx xx--- -----
190 *dst++ = five_bit[4] << 7 | five_bit[5] << 2 | five_bit[6] >> 3;
191 if (count > 7) // ----- ----- ----- ----- ----- ----- --xxx xxxxx
192 *dst++ = five_bit[6] << 5 | five_bit[7];
193 } /* while (1) */
194 ret:
195 *pp_dst = dst; 178 *pp_dst = dst;
196 return src_tail; 179 /* i should be zero here if full 8-char block was decoded */
180 return src - i;
197} 181}
198#endif 182#endif
199 183