Bug #1755: base64_decode uses inefficient algorithm
| From: | bfranklin at dct dot com | Date: | Tue, 20 Jul 1999 16:34:31 +0000 |
| Subject: | Bug #1755: base64_decode uses inefficient algorithm | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-8778@lists.php.net to get a copy of this message | ||
From: bfranklin@dct.com
Operating system: Sparc Solaris 2.7
PHP version: 4.0 Beta 1
PHP Bug Type: Performance problem
Bug description: base64_decode uses inefficient algorithm
Base64_decode uses strchr to lookup the location of a character in base64_table[], causing a
function call and (on average) 32 comparisons per encoded character.
It would be far more efficient to build a static array of reverse lookup values to use.
Here's a patch to fix this:
--- base64.c.orig Tue Jul 20 10:29:42 1999
+++ base64.c Tue Jul 20 10:51:54 1999
@@ -71,18 +71,34 @@
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;
+ static int reverse_table[256];
+ static int table_built = 0;
unsigned char *result = (unsigned char *)emalloc((length / 4 * 3 + 1) * sizeof(char));
if (result == NULL) {
return NULL;
}
+ /* build table of reverse mappings to speed things up */
+ if( ! table_built )
+ {
+ char *chp;
+ for(ch=0;ch<256;ch++)
+ {
+ chp = strchr(base64_table, ch);
+ if( chp != NULL )
+ reverse_table[ch] = chp - base64_table;
+ else
+ reverse_table[ch] = -1;
+ }
+ table_built = 1;
+ }
+
/* 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: