Skip to content

Commit 63f7648

Browse files
author
Sascha Schumann
committed
use reverse lookup array, submitted by bfranklin@dct.com, php#1755
1 parent a7e3a95 commit 63f7648

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

ext/standard/base64.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,19 @@ unsigned char *_php3_base64_encode(const unsigned char *string, int length, int
7171
unsigned char *_php3_base64_decode(const unsigned char *string, int length, int *ret_length) {
7272
const unsigned char *current = string;
7373
int ch, i = 0, j = 0, k;
74+
/* this sucks for threaded environments */
75+
static short reverse_table[256];
76+
static int table_built;
77+
78+
if(++table_built == 1) {
79+
char *chp;
80+
for(ch = 0; ch < 256; ch++) {
81+
chp = strchr(base64_table, ch);
82+
if(chp)
83+
reverse_table[ch] = chp - base64_table;
84+
else
85+
reverse_table[ch] = -1;
86+
}
7487

7588
unsigned char *result = (unsigned char *)emalloc((length / 4 * 3 + 1) * sizeof(char));
7689
if (result == NULL) {
@@ -80,9 +93,8 @@ unsigned char *_php3_base64_decode(const unsigned char *string, int length, int
8093
/* run through the whole string, converting as we go */
8194
while ((ch = *current++) != '\0') {
8295
if (ch == base64_pad) break;
83-
ch = (int)strchr(base64_table, ch);
84-
if (ch == 0) continue;
85-
ch -= (int)base64_table;
96+
ch = reverse_table[ch];
97+
if (ch < 0) continue;
8698

8799
switch(i % 4) {
88100
case 0:

0 commit comments

Comments
 (0)