From: Marcus Boerger Date: Thu, 11 Apr 2002 08:07:22 +0000 (+0000) Subject: thread safe X-Git-Tag: php-4.3.0dev-ZendEngine2-Preview1~722 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=4407312d4f8172f9ca25564abee7e5be5a6a0447;p=php thread safe --- diff --git a/ext/standard/base64.c b/ext/standard/base64.c index 4a6533a194..4cd8c36cc6 100644 --- a/ext/standard/base64.c +++ b/ext/standard/base64.c @@ -22,15 +22,37 @@ #include "php.h" #include "base64.h" -static char base64_table[] = +/* {{{ */ +static const char base64_table[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', '\0' }; -static char base64_pad = '='; +static const char base64_pad = '='; + +static const short base64_reverse_table[256] = { + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, + -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, + 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, + -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, + 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 +}; +/* }}} */ + +/* {{{ */ unsigned char *php_base64_encode(const unsigned char *str, int length, int *ret_length) { const unsigned char *current = str; int i = 0; @@ -66,28 +88,49 @@ unsigned char *php_base64_encode(const unsigned char *str, int length, int *ret_ result[i] = '\0'; return result; } +/* }}} */ + +/* {{{ */ +/* generate reverse table (do not set index 0 to 64) +static unsigned short base64_reverse_table[256]; +#define rt base64_reverse_table +void php_base64_init() { + char *s = emalloc(10240), *sp; + char *chp; + short idx; + + for(ch = 0; ch < 256; ch++) { + chp = strchr(base64_table, ch); + if(ch && chp) { + idx = chp - base64_table; + if (idx >= 64) idx = -1; + rt[ch] = idx; + } else { + rt[ch] = -1; + } + } + sp = s; + sprintf(sp, "static const short base64_reverse_table[256] = {\n"); + for(ch =0; ch < 256;) { + sp = s+strlen(s); + sprintf(sp, "\t% 3d,% 3d,% 3d,% 3d,% 3d,% 3d,% 3d,% 3d,% 3d,% 3d,% 3d,% 3d,% 3d,% 3d,% 3d,% 3d,\n", rt[ch+0], rt[ch+1], rt[ch+2], rt[ch+3], rt[ch+4], rt[ch+5], rt[ch+6], rt[ch+7], rt[ch+8], rt[ch+9], rt[ch+10], rt[ch+11], rt[ch+12], rt[ch+13], rt[ch+14], rt[ch+15]); + ch += 16; + } + sprintf(sp, "};"); + php_error(E_NOTICE,"reverse_table:\n%s", s); + efree(s); +} +*/ +/* }}} */ +/* {{{ */ /* as above, but backwards. :) */ unsigned char *php_base64_decode(const unsigned char *str, int length, int *ret_length) { const unsigned char *current = str; int ch, i = 0, j = 0, k; /* this sucks for threaded environments */ - static short reverse_table[256]; - static int table_built; unsigned char *result; - - 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; - } - } - } - + result = (unsigned char *)emalloc(length + 1); if (result == NULL) { return NULL; @@ -106,7 +149,7 @@ unsigned char *php_base64_decode(const unsigned char *str, int length, int *ret_ if (ch == ' ') ch = '+'; - ch = reverse_table[ch]; + ch = base64_reverse_table[ch]; if (ch < 0) continue; switch(i % 4) { @@ -148,6 +191,7 @@ unsigned char *php_base64_decode(const unsigned char *str, int length, int *ret_ result[k] = '\0'; return result; } +/* }}} */ /* {{{ proto string base64_encode(string str) Encodes string using MIME base64 algorithm */