Skip to content

Commit 7c787e5

Browse files
committed
Switch php_hash_fetch_ops() to use zend_string
This has two advantages: If the string is already lowercase, we do not need to copy it, and it will hopefully match the interned string name of the hash, making the comparison more efficient.
1 parent a9c04ae commit 7c787e5

File tree

2 files changed

+29
-29
lines changed

2 files changed

+29
-29
lines changed

ext/hash/hash.c

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,11 @@ static struct mhash_bc_entry mhash_to_hash[MHASH_NUM_ALGOS] = {
8383

8484
/* Hash Registry Access */
8585

86-
PHP_HASH_API const php_hash_ops *php_hash_fetch_ops(const char *algo, size_t algo_len) /* {{{ */
86+
PHP_HASH_API const php_hash_ops *php_hash_fetch_ops(zend_string *algo) /* {{{ */
8787
{
88-
char *lower = zend_str_tolower_dup(algo, algo_len);
89-
php_hash_ops *ops = zend_hash_str_find_ptr(&php_hash_hashtable, lower, algo_len);
90-
efree(lower);
88+
zend_string *lower = zend_string_tolower(algo);
89+
php_hash_ops *ops = zend_hash_find_ptr(&php_hash_hashtable, lower);
90+
zend_string_release(lower);
9191

9292
return ops;
9393
}
@@ -115,21 +115,21 @@ PHP_HASH_API int php_hash_copy(const void *ops, void *orig_context, void *dest_c
115115

116116
static void php_hash_do_hash(INTERNAL_FUNCTION_PARAMETERS, int isfilename, zend_bool raw_output_default) /* {{{ */
117117
{
118-
zend_string *digest;
119-
char *algo, *data;
120-
size_t algo_len, data_len;
118+
zend_string *digest, *algo;
119+
char *data;
120+
size_t data_len;
121121
zend_bool raw_output = raw_output_default;
122122
const php_hash_ops *ops;
123123
void *context;
124124
php_stream *stream = NULL;
125125

126-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss|b", &algo, &algo_len, &data, &data_len, &raw_output) == FAILURE) {
126+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ss|b", &algo, &data, &data_len, &raw_output) == FAILURE) {
127127
RETURN_THROWS();
128128
}
129129

130-
ops = php_hash_fetch_ops(algo, algo_len);
130+
ops = php_hash_fetch_ops(algo);
131131
if (!ops) {
132-
php_error_docref(NULL, E_WARNING, "Unknown hashing algorithm: %s", algo);
132+
php_error_docref(NULL, E_WARNING, "Unknown hashing algorithm: %s", ZSTR_VAL(algo));
133133
RETURN_FALSE;
134134
}
135135
if (isfilename) {
@@ -236,27 +236,27 @@ static inline void php_hash_hmac_round(unsigned char *final, const php_hash_ops
236236

237237
static void php_hash_do_hash_hmac(INTERNAL_FUNCTION_PARAMETERS, int isfilename, zend_bool raw_output_default) /* {{{ */
238238
{
239-
zend_string *digest;
240-
char *algo, *data, *key;
239+
zend_string *digest, *algo;
240+
char *data, *key;
241241
unsigned char *K;
242-
size_t algo_len, data_len, key_len;
242+
size_t data_len, key_len;
243243
zend_bool raw_output = raw_output_default;
244244
const php_hash_ops *ops;
245245
void *context;
246246
php_stream *stream = NULL;
247247

248-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sss|b", &algo, &algo_len, &data, &data_len,
248+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Sss|b", &algo, &data, &data_len,
249249
&key, &key_len, &raw_output) == FAILURE) {
250250
RETURN_THROWS();
251251
}
252252

253-
ops = php_hash_fetch_ops(algo, algo_len);
253+
ops = php_hash_fetch_ops(algo);
254254
if (!ops) {
255-
zend_throw_error(NULL, "Unknown hashing algorithm: %s", algo);
255+
zend_throw_error(NULL, "Unknown hashing algorithm: %s", ZSTR_VAL(algo));
256256
RETURN_THROWS();
257257
}
258258
else if (!ops->is_crypto) {
259-
zend_throw_error(NULL, "Non-cryptographic hashing algorithm: %s", algo);
259+
zend_throw_error(NULL, "Non-cryptographic hashing algorithm: %s", ZSTR_VAL(algo));
260260
RETURN_THROWS();
261261
}
262262

@@ -356,7 +356,7 @@ PHP_FUNCTION(hash_init)
356356
RETURN_THROWS();
357357
}
358358

359-
ops = php_hash_fetch_ops(ZSTR_VAL(algo), ZSTR_LEN(algo));
359+
ops = php_hash_fetch_ops(algo);
360360
if (!ops) {
361361
zend_throw_error(NULL, "Unknown hashing algorithm: %s", ZSTR_VAL(algo));
362362
RETURN_THROWS();
@@ -645,7 +645,7 @@ PHP_FUNCTION(hash_hkdf)
645645
RETURN_THROWS();
646646
}
647647

648-
ops = php_hash_fetch_ops(ZSTR_VAL(algo), ZSTR_LEN(algo));
648+
ops = php_hash_fetch_ops(algo);
649649
if (!ops) {
650650
zend_throw_error(NULL, "Unknown hashing algorithm: %s", ZSTR_VAL(algo));
651651
RETURN_THROWS();
@@ -732,26 +732,26 @@ Generate a PBKDF2 hash of the given password and salt
732732
Returns lowercase hexits by default */
733733
PHP_FUNCTION(hash_pbkdf2)
734734
{
735-
zend_string *returnval;
736-
char *algo, *salt, *pass = NULL;
735+
zend_string *returnval, *algo;
736+
char *salt, *pass = NULL;
737737
unsigned char *computed_salt, *digest, *temp, *result, *K1, *K2 = NULL;
738738
zend_long loops, i, j, iterations, digest_length = 0, length = 0;
739-
size_t algo_len, pass_len, salt_len = 0;
739+
size_t pass_len, salt_len = 0;
740740
zend_bool raw_output = 0;
741741
const php_hash_ops *ops;
742742
void *context;
743743

744-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sssl|lb", &algo, &algo_len, &pass, &pass_len, &salt, &salt_len, &iterations, &length, &raw_output) == FAILURE) {
744+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Sssl|lb", &algo, &pass, &pass_len, &salt, &salt_len, &iterations, &length, &raw_output) == FAILURE) {
745745
RETURN_THROWS();
746746
}
747747

748-
ops = php_hash_fetch_ops(algo, algo_len);
748+
ops = php_hash_fetch_ops(algo);
749749
if (!ops) {
750-
zend_throw_error(NULL, "Unknown hashing algorithm: %s", algo);
750+
zend_throw_error(NULL, "Unknown hashing algorithm: %s", ZSTR_VAL(algo));
751751
RETURN_THROWS();
752752
}
753753
else if (!ops->is_crypto) {
754-
zend_throw_error(NULL, "Non-cryptographic hashing algorithm: %s", algo);
754+
zend_throw_error(NULL, "Non-cryptographic hashing algorithm: %s", ZSTR_VAL(algo));
755755
RETURN_THROWS();
756756
}
757757

@@ -1035,7 +1035,7 @@ PHP_FUNCTION(mhash_get_block_size)
10351035
if (algorithm >= 0 && algorithm < MHASH_NUM_ALGOS) {
10361036
struct mhash_bc_entry algorithm_lookup = mhash_to_hash[algorithm];
10371037
if (algorithm_lookup.mhash_name) {
1038-
const php_hash_ops *ops = php_hash_fetch_ops(algorithm_lookup.hash_name, strlen(algorithm_lookup.hash_name));
1038+
const php_hash_ops *ops = zend_hash_str_find_ptr(&php_hash_hashtable, algorithm_lookup.hash_name, strlen(algorithm_lookup.hash_name));
10391039
if (ops) {
10401040
RETVAL_LONG(ops->digest_size);
10411041
}
@@ -1078,7 +1078,7 @@ PHP_FUNCTION(mhash_keygen_s2k)
10781078
if (algorithm >= 0 && algorithm < MHASH_NUM_ALGOS) {
10791079
struct mhash_bc_entry algorithm_lookup = mhash_to_hash[algorithm];
10801080
if (algorithm_lookup.mhash_name) {
1081-
const php_hash_ops *ops = php_hash_fetch_ops(algorithm_lookup.hash_name, strlen(algorithm_lookup.hash_name));
1081+
const php_hash_ops *ops = zend_hash_str_find_ptr(&php_hash_hashtable, algorithm_lookup.hash_name, strlen(algorithm_lookup.hash_name));
10821082
if (ops) {
10831083
unsigned char null = '\0';
10841084
void *context;

ext/hash/php_hash.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ PHP_FUNCTION(hash_pbkdf2);
142142
PHP_FUNCTION(hash_equals);
143143

144144
extern PHP_HASH_API zend_class_entry *php_hashcontext_ce;
145-
PHP_HASH_API const php_hash_ops *php_hash_fetch_ops(const char *algo, size_t algo_len);
145+
PHP_HASH_API const php_hash_ops *php_hash_fetch_ops(zend_string *algo);
146146
PHP_HASH_API void php_hash_register_algo(const char *algo, const php_hash_ops *ops);
147147
PHP_HASH_API int php_hash_copy(const void *ops, void *orig_context, void *dest_context);
148148

0 commit comments

Comments
 (0)