summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXinchen Hui <laruence@gmail.com>2014-02-24 18:48:22 +0800
committerXinchen Hui <laruence@gmail.com>2014-02-24 18:48:22 +0800
commit93428dc6b902f23e56bf01e87c63ea0d6d6c03a4 (patch)
treeb90898523b92e51deab4cf0339d09fe2c65c264a
parent1e5a4f281d21abab8101301ea155f9c0394ab02f (diff)
downloadphp-git-93428dc6b902f23e56bf01e87c63ea0d6d6c03a4.tar.gz
Refactor base64 to returning zend_string
-rw-r--r--ext/standard/base64.c72
-rw-r--r--ext/standard/base64.h6
-rw-r--r--ext/standard/http_fopen_wrapper.c8
-rw-r--r--ext/standard/password.c19
-rw-r--r--main/main.c13
5 files changed, 55 insertions, 63 deletions
diff --git a/ext/standard/base64.c b/ext/standard/base64.c
index bcc2fa6b84..3bc96267b2 100644
--- a/ext/standard/base64.c
+++ b/ext/standard/base64.c
@@ -53,21 +53,18 @@ static const short base64_reverse_table[256] = {
};
/* }}} */
-PHPAPI unsigned char *php_base64_encode(const unsigned char *str, int length, int *ret_length) /* {{{ */
+PHPAPI zend_string *php_base64_encode(const unsigned char *str, int length) /* {{{ */
{
const unsigned char *current = str;
unsigned char *p;
- unsigned char *result;
+ zend_string *result;
if (length < 0) {
- if (ret_length != NULL) {
- *ret_length = 0;
- }
return NULL;
}
- result = (unsigned char *) safe_emalloc((length + 2) / 3, 4 * sizeof(char), 1);
- p = result;
+ result = STR_ALLOC(((length + 2) / 3) * 4 * sizeof(char), 0);
+ p = (unsigned char *)result->val;
while (length > 2) { /* keep going until we have less than 24 bits */
*p++ = base64_table[current[0] >> 2];
@@ -92,10 +89,10 @@ PHPAPI unsigned char *php_base64_encode(const unsigned char *str, int length, in
*p++ = base64_pad;
}
}
- if (ret_length != NULL) {
- *ret_length = (int)(p - result);
- }
*p = '\0';
+
+ result->len = (p - (unsigned char *)result->val);
+
return result;
}
/* }}} */
@@ -134,20 +131,20 @@ void php_base64_init(void)
*/
/* }}} */
-PHPAPI unsigned char *php_base64_decode(const unsigned char *str, int length, int *ret_length) /* {{{ */
+PHPAPI zend_string *php_base64_decode(const unsigned char *str, int length) /* {{{ */
{
- return php_base64_decode_ex(str, length, ret_length, 0);
+ return php_base64_decode_ex(str, length, 0);
}
/* }}} */
-PHPAPI unsigned char *php_base64_decode_ex(const unsigned char *str, int length, int *ret_length, zend_bool strict) /* {{{ */
+PHPAPI zend_string *php_base64_decode_ex(const unsigned char *str, int length, zend_bool strict) /* {{{ */
{
const unsigned char *current = str;
int ch, i = 0, j = 0, k;
/* this sucks for threaded environments */
- unsigned char *result;
+ zend_string *result;
- result = (unsigned char *)safe_emalloc(length, 1, 1);
+ result = STR_ALLOC(length, 0);
/* run through the whole string, converting as we go */
while ((ch = *current++) != '\0' && length-- > 0) {
@@ -161,7 +158,7 @@ PHPAPI unsigned char *php_base64_decode_ex(const unsigned char *str, int length,
continue;
}
}
- efree(result);
+ STR_FREE(result);
return NULL;
}
continue;
@@ -171,24 +168,24 @@ PHPAPI unsigned char *php_base64_decode_ex(const unsigned char *str, int length,
if ((!strict && ch < 0) || ch == -1) { /* a space or some other separator character, we simply skip over */
continue;
} else if (ch == -2) {
- efree(result);
+ STR_FREE(result);
return NULL;
}
switch(i % 4) {
case 0:
- result[j] = ch << 2;
+ result->val[j] = ch << 2;
break;
case 1:
- result[j++] |= ch >> 4;
- result[j] = (ch & 0x0f) << 4;
+ result->val[j++] |= ch >> 4;
+ result->val[j] = (ch & 0x0f) << 4;
break;
case 2:
- result[j++] |= ch >>2;
- result[j] = (ch & 0x03) << 6;
+ result->val[j++] |= ch >>2;
+ result->val[j] = (ch & 0x03) << 6;
break;
case 3:
- result[j++] |= ch;
+ result->val[j++] |= ch;
break;
}
i++;
@@ -199,18 +196,17 @@ PHPAPI unsigned char *php_base64_decode_ex(const unsigned char *str, int length,
if (ch == base64_pad) {
switch(i % 4) {
case 1:
- efree(result);
+ STR_FREE(result);
return NULL;
case 2:
k++;
case 3:
- result[k] = 0;
+ result->val[k] = 0;
}
}
- if(ret_length) {
- *ret_length = j;
- }
- result[j] = '\0';
+ result->len = j;
+ result->val[result->len] = '\0';
+
return result;
}
/* }}} */
@@ -220,16 +216,15 @@ PHPAPI unsigned char *php_base64_decode_ex(const unsigned char *str, int length,
PHP_FUNCTION(base64_encode)
{
char *str;
- unsigned char *result;
- int str_len, ret_length;
+ int str_len;
+ zend_string *result;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
return;
}
- result = php_base64_encode((unsigned char*)str, str_len, &ret_length);
+ result = php_base64_encode((unsigned char*)str, str_len);
if (result != NULL) {
-//??? RETVAL_STRINGL((char*)result, ret_length, 0);
- RETVAL_STRINGL((char*)result, ret_length);
+ RETURN_STR(result);
} else {
RETURN_FALSE;
}
@@ -241,17 +236,16 @@ PHP_FUNCTION(base64_encode)
PHP_FUNCTION(base64_decode)
{
char *str;
- unsigned char *result;
zend_bool strict = 0;
- int str_len, ret_length;
+ int str_len;
+ zend_string *result;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &str, &str_len, &strict) == FAILURE) {
return;
}
- result = php_base64_decode_ex((unsigned char*)str, str_len, &ret_length, strict);
+ result = php_base64_decode_ex((unsigned char*)str, str_len, strict);
if (result != NULL) {
-//??? RETVAL_STRINGL((char*)result, ret_length, 0);
- RETVAL_STRINGL((char*)result, ret_length);
+ RETURN_STR(result);
} else {
RETURN_FALSE;
}
diff --git a/ext/standard/base64.h b/ext/standard/base64.h
index 2358c58951..e58565702a 100644
--- a/ext/standard/base64.h
+++ b/ext/standard/base64.h
@@ -24,9 +24,9 @@
PHP_FUNCTION(base64_decode);
PHP_FUNCTION(base64_encode);
-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 zend_string *php_base64_encode(const unsigned char *, int);
+PHPAPI extern zend_string *php_base64_decode_ex(const unsigned char *, int, zend_bool);
+PHPAPI extern zend_string *php_base64_decode(const unsigned char *, int);
#endif /* BASE64_H */
diff --git a/ext/standard/http_fopen_wrapper.c b/ext/standard/http_fopen_wrapper.c
index db87e6907c..55a1d12cc7 100644
--- a/ext/standard/http_fopen_wrapper.c
+++ b/ext/standard/http_fopen_wrapper.c
@@ -526,6 +526,7 @@ finish:
/* auth header if it was specified */
if (((have_header & HTTP_HEADER_AUTH) == 0) && resource->user) {
+ zend_string *stmp;
/* decode the strings first */
php_url_decode(resource->user, strlen(resource->user));
@@ -539,15 +540,14 @@ finish:
strcat(scratch, resource->pass);
}
- tmp = (char*)php_base64_encode((unsigned char*)scratch, strlen(scratch), NULL);
+ stmp = php_base64_encode((unsigned char*)scratch, strlen(scratch));
- if (snprintf(scratch, scratch_len, "Authorization: Basic %s\r\n", tmp) > 0) {
+ if (snprintf(scratch, scratch_len, "Authorization: Basic %s\r\n", stmp->val) > 0) {
php_stream_write(stream, scratch, strlen(scratch));
php_stream_notify_info(context, PHP_STREAM_NOTIFY_AUTH_REQUIRED, NULL, 0);
}
- efree(tmp);
- tmp = NULL;
+ STR_FREE(stmp);
}
/* if the user has configured who they are, send a From: line */
diff --git a/ext/standard/password.c b/ext/standard/password.c
index a76bf847d3..f5b925f530 100644
--- a/ext/standard/password.c
+++ b/ext/standard/password.c
@@ -82,28 +82,27 @@ static int php_password_salt_is_alphabet(const char *str, const size_t len) /* {
static int php_password_salt_to64(const char *str, const size_t str_len, const size_t out_len, char *ret) /* {{{ */
{
size_t pos = 0;
- size_t ret_len = 0;
- unsigned char *buffer;
+ zend_string *buffer;
if ((int) str_len < 0) {
return FAILURE;
}
- buffer = php_base64_encode((unsigned char*) str, (int) str_len, (int*) &ret_len);
- if (ret_len < out_len) {
+ buffer = php_base64_encode((unsigned char*) str, (int) str_len);
+ if (buffer->len < out_len) {
/* Too short of an encoded string generated */
- efree(buffer);
+ STR_RELEASE(buffer);
return FAILURE;
}
for (pos = 0; pos < out_len; pos++) {
- if (buffer[pos] == '+') {
+ if (buffer->val[pos] == '+') {
ret[pos] = '.';
- } else if (buffer[pos] == '=') {
- efree(buffer);
+ } else if (buffer->val[pos] == '=') {
+ STR_FREE(buffer);
return FAILURE;
} else {
- ret[pos] = buffer[pos];
+ ret[pos] = buffer->val[pos];
}
}
- efree(buffer);
+ STR_FREE(buffer);
return SUCCESS;
}
/* }}} */
diff --git a/main/main.c b/main/main.c
index 9d6fe67755..821a1a7bfb 100644
--- a/main/main.c
+++ b/main/main.c
@@ -2584,19 +2584,18 @@ PHPAPI int php_handle_auth_data(const char *auth TSRMLS_DC)
if (auth && auth[0] != '\0' && strncmp(auth, "Basic ", 6) == 0) {
char *pass;
- char *user;
+ zend_string *user;
- user = php_base64_decode(auth + 6, strlen(auth) - 6, NULL);
+ user = php_base64_decode(auth + 6, strlen(auth) - 6);
if (user) {
- pass = strchr(user, ':');
+ pass = strchr(user->val, ':');
if (pass) {
*pass++ = '\0';
- SG(request_info).auth_user = user;
+ SG(request_info).auth_user = estrndup(user->val, user->len);
SG(request_info).auth_password = estrdup(pass);
ret = 0;
- } else {
- efree(user);
- }
+ }
+ STR_FREE(user);
}
}