summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSascha Schumann <sas@php.net>1999-07-21 09:02:05 +0000
committerSascha Schumann <sas@php.net>1999-07-21 09:02:05 +0000
commit63f7648fdc647426e1ec31f987b8c2bc3924c412 (patch)
tree9f79073444215865583aa696d5c34878fa0b8a93
parenta7e3a952e7c4983e9f37f01da55ca449bca466d8 (diff)
downloadphp-git-63f7648fdc647426e1ec31f987b8c2bc3924c412.tar.gz
use reverse lookup array, submitted by bfranklin@dct.com, #1755
-rw-r--r--ext/standard/base64.c18
1 files changed, 15 insertions, 3 deletions
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: