]> granicus.if.org Git - php/commitdiff
Refactor php_crypt to returning zend_string
authorXinchen Hui <laruence@gmail.com>
Tue, 25 Feb 2014 04:46:51 +0000 (12:46 +0800)
committerXinchen Hui <laruence@gmail.com>
Tue, 25 Feb 2014 04:46:51 +0000 (12:46 +0800)
ext/standard/crypt.c
ext/standard/password.c
ext/standard/php_crypt.h

index c789a3593d1270b3cdf296192dbe3e1d7430e6e4..f7696cf5511399a02afbce5012f3dc0c77a9e57e 100644 (file)
@@ -145,9 +145,10 @@ static void php_to64(char *s, long v, int n) /* {{{ */
 }
 /* }}} */
 
-PHPAPI int php_crypt(const char *password, const int pass_len, const char *salt, int salt_len, char **result)
+PHPAPI zend_string *php_crypt(const char *password, const int pass_len, const char *salt, int salt_len)
 {
        char *crypt_res;
+       zend_string *result;
 /* Windows (win32/crypt) has a stripped down version of libxcrypt and 
        a CryptoApi md5_crypt implementation */
 #if PHP_USE_PHP_CRYPT_R
@@ -159,10 +160,9 @@ PHPAPI int php_crypt(const char *password, const int pass_len, const char *salt,
 
                        out = php_md5_crypt_r(password, salt, output);
                        if (out) {
-                               *result = estrdup(out);
-                               return SUCCESS;
+                               return STR_INIT(out, strlen(out), 0);
                        }
-                       return FAILURE;
+                       return NULL;
                } else if (salt[0]=='$' && salt[1]=='6' && salt[2]=='$') {
                        char *output;
                        output = emalloc(PHP_MAX_SALT_LEN);
@@ -171,12 +171,12 @@ PHPAPI int php_crypt(const char *password, const int pass_len, const char *salt,
                        if (!crypt_res) {
                                memset(output, 0, PHP_MAX_SALT_LEN);
                                efree(output);
-                               return FAILURE;
+                               return NULL;
                        } else {
-                               *result = estrdup(output);
+                               result = STR_INIT(output, strlen(output), 0);
                                memset(output, 0, PHP_MAX_SALT_LEN);
                                efree(output);
-                               return SUCCESS;
+                               return result;
                        }
                } else if (salt[0]=='$' && salt[1]=='5' && salt[2]=='$') {
                        char *output;
@@ -186,12 +186,12 @@ PHPAPI int php_crypt(const char *password, const int pass_len, const char *salt,
                        if (!crypt_res) {
                                memset(output, 0, PHP_MAX_SALT_LEN);
                                efree(output);
-                               return FAILURE;
+                               return NULL;
                        } else {
-                               *result = estrdup(output);
+                               result = STR_INIT(output, strlen(output), 0);
                                memset(output, 0, PHP_MAX_SALT_LEN);
                                efree(output);
-                               return SUCCESS;
+                               return result;
                        }
                } else if (
                                salt[0] == '$' &&
@@ -208,11 +208,11 @@ PHPAPI int php_crypt(const char *password, const int pass_len, const char *salt,
                        crypt_res = php_crypt_blowfish_rn(password, salt, output, sizeof(output));
                        if (!crypt_res) {
                                memset(output, 0, PHP_MAX_SALT_LEN + 1);
-                               return FAILURE;
+                               return NULL;
                        } else {
-                               *result = estrdup(output);
+                               result = STR_INIT(output, strlen(output), 0);
                                memset(output, 0, PHP_MAX_SALT_LEN + 1);
-                               return SUCCESS;
+                               return result;
                        }
                } else {
                        memset(&buffer, 0, sizeof(buffer));
@@ -220,10 +220,10 @@ PHPAPI int php_crypt(const char *password, const int pass_len, const char *salt,
 
                        crypt_res = _crypt_extended_r(password, salt, &buffer);
                        if (!crypt_res) {
-                               return FAILURE;
+                               return NULL;
                        } else {
-                               *result = estrdup(crypt_res);
-                               return SUCCESS;
+                               result = STR_INIT(crypt_res, strlen(crypt_res), 0);
+                               return result;
                        }
                }
        }
@@ -243,8 +243,8 @@ PHPAPI int php_crypt(const char *password, const int pass_len, const char *salt,
                if (!crypt_res) {
                        return FAILURE;
                } else {
-                       *result = estrdup(crypt_res);
-                       return SUCCESS;
+                       result = STR_INIT(crypt_res, strlen(crypt_res), 0);
+                       return result;
                }
        }
 # endif
@@ -258,9 +258,10 @@ PHPAPI int php_crypt(const char *password, const int pass_len, const char *salt,
 PHP_FUNCTION(crypt)
 {
        char salt[PHP_MAX_SALT_LEN + 1];
-       char *str, *salt_in = NULL, *result = NULL;
+       char *str, *salt_in = NULL;
        int str_len, salt_in_len = 0;
        salt[0] = salt[PHP_MAX_SALT_LEN] = '\0';
+       zend_string *result;
 
        /* This will produce suitable results if people depend on DES-encryption
         * available (passing always 2-character salt). At least for glibc6.1 */
@@ -293,15 +294,14 @@ PHP_FUNCTION(crypt)
        }
        salt[salt_in_len] = '\0';
 
-       if (php_crypt(str, str_len, salt, salt_in_len, &result) == FAILURE) {
+       if ((result = php_crypt(str, str_len, salt, salt_in_len)) == NULL) {
                if (salt[0] == '*' && salt[1] == '0') {
                        RETURN_STRING("*1");
                } else {
                        RETURN_STRING("*0");
                }
        }
-//???  RETURN_STRING(result, 0);
-       RETURN_STRING(result);
+       RETURN_STR(result);
 }
 /* }}} */
 #endif
index f5b925f530c7c05976b4cc5527ec758377808375..de0de3d80da42fd480fc559c48a2a5a9fa180e72 100644 (file)
@@ -272,17 +272,18 @@ PHP_FUNCTION(password_verify)
 {
        int status = 0, i;
        int password_len, hash_len;
-       char *ret, *password, *hash;
+       char *password, *hash;
+       zend_string *ret;
        
        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &password, &password_len, &hash, &hash_len) == FAILURE) {
                RETURN_FALSE;
        }
-       if (php_crypt(password, password_len, hash, hash_len, &ret) == FAILURE) {
+       if ((ret = php_crypt(password, password_len, hash, hash_len)) == NULL) {
                RETURN_FALSE;
        }
 
-       if (strlen(ret) != hash_len || hash_len < 13) {
-               efree(ret);
+       if (ret->len != hash_len || hash_len < 13) {
+               STR_FREE(ret);
                RETURN_FALSE;
        }
        
@@ -291,10 +292,10 @@ PHP_FUNCTION(password_verify)
         * equality check that will always check every byte of both
         * values. */
        for (i = 0; i < hash_len; i++) {
-               status |= (ret[i] ^ hash[i]);
+               status |= (ret->val[i] ^ hash[i]);
        }
 
-       efree(ret);
+       STR_FREE(ret);
 
        RETURN_BOOL(status == 0);
        
@@ -305,12 +306,13 @@ PHP_FUNCTION(password_verify)
 Hash a password */
 PHP_FUNCTION(password_hash)
 {
-       char *hash_format, *hash, *salt, *password, *result;
+       char *hash_format, *hash, *salt, *password;
        long algo = 0;
        int password_len = 0, hash_len;
        size_t salt_len = 0, required_salt_len = 0, hash_format_len;
        HashTable *options = 0;
        zval *option_buffer;
+       zend_string *result;
 
        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl|H", &password, &password_len, &algo, &options) == FAILURE) {
                return;
@@ -432,20 +434,19 @@ PHP_FUNCTION(password_hash)
        /* This cast is safe, since both values are defined here in code and cannot overflow */
        hash_len = (int) (hash_format_len + salt_len);
 
-       if (php_crypt(password, password_len, hash, hash_len, &result) == FAILURE) {
+       if ((result = php_crypt(password, password_len, hash, hash_len)) == NULL) {
                efree(hash);
                RETURN_FALSE;
        }
 
        efree(hash);
 
-       if (strlen(result) < 13) {
-               efree(result);
+       if (result->len < 13) {
+               STR_FREE(result);
                RETURN_FALSE;
        }
 
-//???  RETURN_STRING(result, 0);
-       RETURN_STRING(result);
+       RETURN_STR(result);
 }
 /* }}} */
 
index f0b96010bc39019e7fe181f1053b63cf0191981e..a23811c3201775ea19efadd624c71e0711404912 100644 (file)
@@ -23,7 +23,7 @@
 #ifndef PHP_CRYPT_H
 #define PHP_CRYPT_H
 
-PHPAPI int php_crypt(const char *password, const int pass_len, const char *salt, int salt_len, char **result);
+PHPAPI zend_string *php_crypt(const char *password, const int pass_len, const char *salt, int salt_len);
 PHP_FUNCTION(crypt);
 #if HAVE_CRYPT
 PHP_MINIT_FUNCTION(crypt);