Browse Source

Fixed bug #37244 (Added strict flag to base64_decode() that enforces

RFC3548 compliance).
PECL_OPENSSL
Ilia Alshanetsky 21 years ago
parent
commit
7e8409de8c
  1. 2
      NEWS
  2. 56
      ext/standard/base64.c
  3. 1
      ext/standard/base64.h

2
NEWS

@ -3,6 +3,8 @@ PHP NEWS
?? ??? 2006, PHP 5.2.0 ?? ??? 2006, PHP 5.2.0
- Fixed bug #37313 (sigemptyset() used without including <signal.h>). - Fixed bug #37313 (sigemptyset() used without including <signal.h>).
(jdolecek) (jdolecek)
- Fixed bug #37244 (Added strict flag to base64_decode() that enforces
RFC3548 compliance). (Ilia)
04 May 2006, PHP 5.1.4 04 May 2006, PHP 5.1.4
- Added "capture_peer_cert" and "capture_peer_cert_chain" context options - Added "capture_peer_cert" and "capture_peer_cert_chain" context options

56
ext/standard/base64.c

@ -34,22 +34,22 @@ static const char base64_table[] =
static const char base64_pad = '='; static const char base64_pad = '=';
static const short base64_reverse_table[256] = { 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,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
-2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -1, -2, -2, -1, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-1, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 62, -2, -2, -2, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -2, -2, -2, -2, -2, -2,
-2, 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, -2, -2, -2, -2, -2,
-2, 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, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2
}; };
/* }}} */ /* }}} */
@ -135,9 +135,14 @@ void php_base64_init()
*/ */
/* }}} */ /* }}} */
PHPAPI unsigned char *php_base64_decode(const unsigned char *str, int length, int *ret_length)
{
return php_base64_decode_ex(str, length, ret_length, 0);
}
/* {{{ php_base64_decode */ /* {{{ php_base64_decode */
/* as above, but backwards. :) */ /* as above, but backwards. :) */
PHPAPI unsigned char *php_base64_decode(const unsigned char *str, int length, int *ret_length)
PHPAPI unsigned char *php_base64_decode_ex(const unsigned char *str, int length, int *ret_length, zend_bool strict)
{ {
const unsigned char *current = str; const unsigned char *current = str;
int ch, i = 0, j = 0, k; int ch, i = 0, j = 0, k;
@ -145,16 +150,18 @@ PHPAPI unsigned char *php_base64_decode(const unsigned char *str, int length, in
unsigned char *result; unsigned char *result;
result = (unsigned char *)emalloc(length + 1); result = (unsigned char *)emalloc(length + 1);
if (result == NULL) {
return NULL;
}
/* run through the whole string, converting as we go */ /* run through the whole string, converting as we go */
while ((ch = *current++) != '\0' && length-- > 0) { while ((ch = *current++) != '\0' && length-- > 0) {
if (ch == base64_pad) break; if (ch == base64_pad) break;
ch = base64_reverse_table[ch]; ch = base64_reverse_table[ch];
if (ch < 0) continue;
if ((!strict && ch < 0) || ch == -1) { /* a space or some other separator character, we simply skip over */
continue;
} else if (ch == -2) {
efree(result);
return NULL;
}
switch(i % 4) { switch(i % 4) {
case 0: case 0:
@ -217,18 +224,19 @@ PHP_FUNCTION(base64_encode)
/* }}} */ /* }}} */
/* {{{ proto string base64_decode(string str)
/* {{{ proto string base64_decode(string str[, bool strict])
Decodes string using MIME base64 algorithm */ Decodes string using MIME base64 algorithm */
PHP_FUNCTION(base64_decode) PHP_FUNCTION(base64_decode)
{ {
char *str; char *str;
unsigned char *result; unsigned char *result;
zend_bool strict = 0;
int str_len, ret_length; int str_len, ret_length;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &str, &str_len, &strict) == FAILURE) {
return; return;
} }
result = php_base64_decode(str, str_len, &ret_length);
result = php_base64_decode_ex(str, str_len, &ret_length, strict);
if (result != NULL) { if (result != NULL) {
RETVAL_STRINGL(result, ret_length, 0); RETVAL_STRINGL(result, ret_length, 0);
} else { } else {

1
ext/standard/base64.h

@ -25,6 +25,7 @@ PHP_FUNCTION(base64_decode);
PHP_FUNCTION(base64_encode); PHP_FUNCTION(base64_encode);
PHPAPI extern unsigned char *php_base64_encode(const unsigned char *, int, int *); PHPAPI extern unsigned char *php_base64_encode(const unsigned char *, int, int *);
PHPAPI extern unsigned char *php_base64_decode_ex(const unsigned char *, int, int *, zend_bool);
PHPAPI extern unsigned char *php_base64_decode(const unsigned char *, int, int *); PHPAPI extern unsigned char *php_base64_decode(const unsigned char *, int, int *);
#endif /* BASE64_H */ #endif /* BASE64_H */

Loading…
Cancel
Save