From: Sascha Schumann Date: Wed, 21 Jul 1999 09:02:05 +0000 (+0000) Subject: use reverse lookup array, submitted by bfranklin@dct.com, #1755 X-Git-Tag: php-4.0b1~36 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=63f7648fdc647426e1ec31f987b8c2bc3924c412;p=php use reverse lookup array, submitted by bfranklin@dct.com, #1755 --- diff --git a/ext/standard/base64.c b/ext/standard/base64.c index 70675d14e3..32c63a36c4 100644 --- a/ext/standard/base64.c +++ b/ext/standard/base64.c @@ -71,6 +71,19 @@ unsigned char *_php3_base64_encode(const unsigned char *string, int length, int unsigned char *_php3_base64_decode(const unsigned char *string, int length, int *ret_length) { const unsigned char *current = string; int ch, i = 0, j = 0, k; + /* this sucks for threaded environments */ + static short reverse_table[256]; + static int table_built; + + if(++table_built == 1) { + char *chp; + for(ch = 0; ch < 256; ch++) { + chp = strchr(base64_table, ch); + if(chp) + reverse_table[ch] = chp - base64_table; + else + reverse_table[ch] = -1; + } unsigned char *result = (unsigned char *)emalloc((length / 4 * 3 + 1) * sizeof(char)); if (result == NULL) { @@ -80,9 +93,8 @@ unsigned char *_php3_base64_decode(const unsigned char *string, int length, int /* run through the whole string, converting as we go */ while ((ch = *current++) != '\0') { if (ch == base64_pad) break; - ch = (int)strchr(base64_table, ch); - if (ch == 0) continue; - ch -= (int)base64_table; + ch = reverse_table[ch]; + if (ch < 0) continue; switch(i % 4) { case 0: