summaryrefslogtreecommitdiff
path: root/ext/hash/hash.c
diff options
context:
space:
mode:
Diffstat (limited to 'ext/hash/hash.c')
-rw-r--r--ext/hash/hash.c84
1 files changed, 50 insertions, 34 deletions
diff --git a/ext/hash/hash.c b/ext/hash/hash.c
index 4e3820f35e..bf1d1577ca 100644
--- a/ext/hash/hash.c
+++ b/ext/hash/hash.c
@@ -52,7 +52,7 @@ struct mhash_bc_entry {
int value;
};
-#define MHASH_NUM_ALGOS 35
+#define MHASH_NUM_ALGOS 42
static struct mhash_bc_entry mhash_to_hash[MHASH_NUM_ALGOS] = {
{"CRC32", "crc32", 0}, /* used by bzip */
@@ -90,6 +90,13 @@ static struct mhash_bc_entry mhash_to_hash[MHASH_NUM_ALGOS] = {
{"FNV1A64", "fnv1a64", 32},
{"JOAAT", "joaat", 33},
{"CRC32C", "crc32c", 34}, /* Castagnoli's CRC, used by iSCSI, SCTP, Btrfs, ext4, etc */
+ {"MURMUR3A", "murmur3a", 35},
+ {"MURMUR3C", "murmur3c", 36},
+ {"MURMUR3F", "murmur3f", 37},
+ {"XXH32", "xxh32", 38},
+ {"XXH64", "xxh64", 39},
+ {"XXH3", "xxh3", 40},
+ {"XXH128", "xxh128", 41},
};
#endif
@@ -346,7 +353,7 @@ PHP_HASH_API int php_hash_unserialize(php_hashcontext_object *hash, zend_long ma
/* Userspace */
static void php_hash_do_hash(
- zval *return_value, zend_string *algo, char *data, size_t data_len, zend_bool raw_output, bool isfilename
+ zval *return_value, zend_string *algo, char *data, size_t data_len, bool raw_output, bool isfilename, HashTable *args
) /* {{{ */ {
zend_string *digest;
const php_hash_ops *ops;
@@ -371,7 +378,7 @@ static void php_hash_do_hash(
}
context = php_hash_alloc_context(ops);
- ops->hash_init(context);
+ ops->hash_init(context, args);
if (isfilename) {
char buf[1024];
@@ -414,16 +421,18 @@ PHP_FUNCTION(hash)
zend_string *algo;
char *data;
size_t data_len;
- zend_bool raw_output = 0;
+ bool raw_output = 0;
+ HashTable *args = NULL;
- ZEND_PARSE_PARAMETERS_START(2, 3)
+ ZEND_PARSE_PARAMETERS_START(2, 4)
Z_PARAM_STR(algo)
Z_PARAM_STRING(data, data_len)
Z_PARAM_OPTIONAL
Z_PARAM_BOOL(raw_output)
+ Z_PARAM_ARRAY_HT(args)
ZEND_PARSE_PARAMETERS_END();
- php_hash_do_hash(return_value, algo, data, data_len, raw_output, 0);
+ php_hash_do_hash(return_value, algo, data, data_len, raw_output, 0, args);
}
/* }}} */
@@ -434,16 +443,18 @@ PHP_FUNCTION(hash_file)
zend_string *algo;
char *data;
size_t data_len;
- zend_bool raw_output = 0;
+ bool raw_output = 0;
+ HashTable *args = NULL;
ZEND_PARSE_PARAMETERS_START(2, 3)
Z_PARAM_STR(algo)
Z_PARAM_STRING(data, data_len)
Z_PARAM_OPTIONAL
Z_PARAM_BOOL(raw_output)
+ Z_PARAM_ARRAY_HT(args)
ZEND_PARSE_PARAMETERS_END();
- php_hash_do_hash(return_value, algo, data, data_len, raw_output, 1);
+ php_hash_do_hash(return_value, algo, data, data_len, raw_output, 1, args);
}
/* }}} */
@@ -465,7 +476,7 @@ static inline void php_hash_hmac_prep_key(unsigned char *K, const php_hash_ops *
memset(K, 0, ops->block_size);
if (key_len > ops->block_size) {
/* Reduce the key first */
- ops->hash_init(context);
+ ops->hash_init(context, NULL);
ops->hash_update(context, key, key_len);
ops->hash_final(K, context);
} else {
@@ -476,14 +487,14 @@ static inline void php_hash_hmac_prep_key(unsigned char *K, const php_hash_ops *
}
static inline void php_hash_hmac_round(unsigned char *final, const php_hash_ops *ops, void *context, const unsigned char *key, const unsigned char *data, const zend_long data_size) {
- ops->hash_init(context);
+ ops->hash_init(context, NULL);
ops->hash_update(context, key, ops->block_size);
ops->hash_update(context, data, data_size);
ops->hash_final(final, context);
}
static void php_hash_do_hash_hmac(
- zval *return_value, zend_string *algo, char *data, size_t data_len, char *key, size_t key_len, zend_bool raw_output, bool isfilename
+ zval *return_value, zend_string *algo, char *data, size_t data_len, char *key, size_t key_len, bool raw_output, bool isfilename
) /* {{{ */ {
zend_string *digest;
unsigned char *K;
@@ -519,7 +530,7 @@ static void php_hash_do_hash_hmac(
if (isfilename) {
char buf[1024];
ssize_t n;
- ops->hash_init(context);
+ ops->hash_init(context, NULL);
ops->hash_update(context, K, ops->block_size);
while ((n = php_stream_read(stream, buf, sizeof(buf))) > 0) {
ops->hash_update(context, (unsigned char *) buf, n);
@@ -567,7 +578,7 @@ PHP_FUNCTION(hash_hmac)
zend_string *algo;
char *data, *key;
size_t data_len, key_len;
- zend_bool raw_output = 0;
+ bool raw_output = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Sss|b", &algo, &data, &data_len, &key, &key_len, &raw_output) == FAILURE) {
RETURN_THROWS();
@@ -584,7 +595,7 @@ PHP_FUNCTION(hash_hmac_file)
zend_string *algo;
char *data, *key;
size_t data_len, key_len;
- zend_bool raw_output = 0;
+ bool raw_output = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Sss|b", &algo, &data, &data_len, &key, &key_len, &raw_output) == FAILURE) {
RETURN_THROWS();
@@ -602,8 +613,9 @@ PHP_FUNCTION(hash_init)
void *context;
const php_hash_ops *ops;
php_hashcontext_object *hash;
+ HashTable *args = NULL;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|lS", &algo, &options, &key) == FAILURE) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|lSh", &algo, &options, &key, &args) == FAILURE) {
RETURN_THROWS();
}
@@ -629,7 +641,7 @@ PHP_FUNCTION(hash_init)
hash = php_hashcontext_from_object(Z_OBJ_P(return_value));
context = php_hash_alloc_context(ops);
- ops->hash_init(context);
+ ops->hash_init(context, args);
hash->ops = ops;
hash->context = context;
@@ -647,7 +659,7 @@ PHP_FUNCTION(hash_init)
ops->hash_update(context, (unsigned char *) ZSTR_VAL(key), ZSTR_LEN(key));
ops->hash_final((unsigned char *) K, context);
/* Make the context ready to start over */
- ops->hash_init(context);
+ ops->hash_init(context, args);
} else {
memcpy(K, ZSTR_VAL(key), ZSTR_LEN(key));
}
@@ -765,7 +777,7 @@ PHP_FUNCTION(hash_final)
{
zval *zhash;
php_hashcontext_object *hash;
- zend_bool raw_output = 0;
+ bool raw_output = 0;
zend_string *digest;
size_t digest_len;
@@ -789,7 +801,7 @@ PHP_FUNCTION(hash_final)
}
/* Feed this result into the outer hash */
- hash->ops->hash_init(hash->context);
+ hash->ops->hash_init(hash->context, NULL);
hash->ops->hash_update(hash->context, hash->key, hash->ops->block_size);
hash->ops->hash_update(hash->context, (unsigned char *) ZSTR_VAL(digest), hash->ops->digest_size);
hash->ops->hash_final((unsigned char *) ZSTR_VAL(digest), hash->context);
@@ -912,7 +924,7 @@ PHP_FUNCTION(hash_hkdf)
context = php_hash_alloc_context(ops);
// Extract
- ops->hash_init(context);
+ ops->hash_init(context, NULL);
K = emalloc(ops->block_size);
php_hash_hmac_prep_key(K, ops, context,
(unsigned char *) (salt ? ZSTR_VAL(salt) : ""), salt ? ZSTR_LEN(salt) : 0);
@@ -932,7 +944,7 @@ PHP_FUNCTION(hash_hkdf)
c[0] = (i & 0xFF);
php_hash_hmac_prep_key(K, ops, context, prk, ops->digest_size);
- ops->hash_init(context);
+ ops->hash_init(context, NULL);
ops->hash_update(context, K, ops->block_size);
if (i > 1) {
@@ -974,11 +986,12 @@ PHP_FUNCTION(hash_pbkdf2)
unsigned char *computed_salt, *digest, *temp, *result, *K1, *K2 = NULL;
zend_long loops, i, j, iterations, digest_length = 0, length = 0;
size_t pass_len, salt_len = 0;
- zend_bool raw_output = 0;
+ bool raw_output = 0;
const php_hash_ops *ops;
void *context;
+ HashTable *args;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "Sssl|lb", &algo, &pass, &pass_len, &salt, &salt_len, &iterations, &length, &raw_output) == FAILURE) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "Sssl|lbh", &algo, &pass, &pass_len, &salt, &salt_len, &iterations, &length, &raw_output, &args) == FAILURE) {
RETURN_THROWS();
}
@@ -1004,7 +1017,7 @@ PHP_FUNCTION(hash_pbkdf2)
}
context = php_hash_alloc_context(ops);
- ops->hash_init(context);
+ ops->hash_init(context, args);
K1 = emalloc(ops->block_size);
K2 = emalloc(ops->block_size);
@@ -1209,7 +1222,7 @@ PHP_FUNCTION(mhash)
if (key) {
php_hash_do_hash_hmac(return_value, algo, data, data_len, key, key_len, 1, 0);
} else {
- php_hash_do_hash(return_value, algo, data, data_len, 1, 0);
+ php_hash_do_hash(return_value, algo, data, data_len, 1, 0, NULL);
}
if (algo) {
@@ -1316,13 +1329,13 @@ PHP_FUNCTION(mhash_keygen_s2k)
}
context = php_hash_alloc_context(ops);
- ops->hash_init(context);
+ ops->hash_init(context, NULL);
key = ecalloc(1, times * block_size);
digest = emalloc(ops->digest_size + 1);
for (i = 0; i < times; i++) {
- ops->hash_init(context);
+ ops->hash_init(context, NULL);
for (j=0;j<i;j++) {
ops->hash_update(context, &null, 1);
@@ -1389,7 +1402,7 @@ static zend_object *php_hashcontext_clone(zend_object *zobj) {
newobj->ops = oldobj->ops;
newobj->options = oldobj->options;
newobj->context = php_hash_alloc_context(newobj->ops);
- newobj->ops->hash_init(newobj->context);
+ newobj->ops->hash_init(newobj->context, NULL);
if (SUCCESS != newobj->ops->hash_copy(newobj->ops, oldobj->context, newobj->context)) {
efree(newobj->context);
@@ -1526,8 +1539,8 @@ PHP_METHOD(HashContext, __unserialize)
hash->ops = ops;
hash->context = php_hash_alloc_context(ops);
- ops->hash_init(hash->context);
hash->options = options;
+ ops->hash_init(hash->context, NULL);
unserialize_result = ops->hash_unserialize(hash, magic, hash_zv);
if (unserialize_result != SUCCESS) {
@@ -1544,8 +1557,6 @@ PHP_METHOD(HashContext, __unserialize)
/* {{{ PHP_MINIT_FUNCTION */
PHP_MINIT_FUNCTION(hash)
{
- zend_class_entry ce;
-
zend_hash_init(&php_hash_hashtable, 35, NULL, NULL, 1);
php_hash_register_algo("md2", &php_hash_md2_ops);
@@ -1586,6 +1597,13 @@ PHP_MINIT_FUNCTION(hash)
php_hash_register_algo("fnv164", &php_hash_fnv164_ops);
php_hash_register_algo("fnv1a64", &php_hash_fnv1a64_ops);
php_hash_register_algo("joaat", &php_hash_joaat_ops);
+ php_hash_register_algo("murmur3a", &php_hash_murmur3a_ops);
+ php_hash_register_algo("murmur3c", &php_hash_murmur3c_ops);
+ php_hash_register_algo("murmur3f", &php_hash_murmur3f_ops);
+ php_hash_register_algo("xxh32", &php_hash_xxh32_ops);
+ php_hash_register_algo("xxh64", &php_hash_xxh64_ops);
+ php_hash_register_algo("xxh3", &php_hash_xxh3_64_ops);
+ php_hash_register_algo("xxh128", &php_hash_xxh3_128_ops);
PHP_HASH_HAVAL_REGISTER(3,128);
PHP_HASH_HAVAL_REGISTER(3,160);
@@ -1607,9 +1625,7 @@ PHP_MINIT_FUNCTION(hash)
REGISTER_LONG_CONSTANT("HASH_HMAC", PHP_HASH_HMAC, CONST_CS | CONST_PERSISTENT);
- INIT_CLASS_ENTRY(ce, "HashContext", class_HashContext_methods);
- php_hashcontext_ce = zend_register_internal_class(&ce);
- php_hashcontext_ce->ce_flags |= ZEND_ACC_FINAL;
+ php_hashcontext_ce = register_class_HashContext();
php_hashcontext_ce->create_object = php_hashcontext_create;
memcpy(&php_hashcontext_handlers, &std_object_handlers,