]> granicus.if.org Git - php/commitdiff
Refactor base64 to returning zend_string
authorXinchen Hui <laruence@gmail.com>
Mon, 24 Feb 2014 10:48:22 +0000 (18:48 +0800)
committerXinchen Hui <laruence@gmail.com>
Mon, 24 Feb 2014 10:48:22 +0000 (18:48 +0800)
ext/standard/base64.c
ext/standard/base64.h
ext/standard/http_fopen_wrapper.c
ext/standard/password.c
main/main.c

index bcc2fa6b84a5920564765e0619730f0a86146a6e..3bc96267b29fd2d57a9bd178fb11a5d2a229138c 100644 (file)
@@ -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;
        }
index 2358c58951a2a4d280735d14a6f0740b82758d90..e58565702a95242ce8cb26c2388fc66995714930 100644 (file)
@@ -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 */
 
index db87e6907cb4c3644e2b1c6d4a89f145a76d78d5..55a1d12cc78c237263b4910a4e321cb9f51a54b4 100644 (file)
@@ -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 */
index a76bf847d31b701b09983885ebc94e564ec7aa43..f5b925f530c7c05976b4cc5527ec758377808375 100644 (file)
@@ -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;
 }
 /* }}} */
index 9d6fe67755e8cdad0484a56ed8e83bce1a712168..821a1a7bfb965aea3879369ce256e514e0c7bc52 100644 (file)
@@ -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);
                }
        }