From 3b486116b2a7d123db008a984da39849ecf227e2 Mon Sep 17 00:00:00 2001 From: tjerk Date: Wed, 20 Feb 2013 14:12:09 +0800 Subject: [PATCH 1/3] added hash extension integration --- php_xxhash.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/php_xxhash.c b/php_xxhash.c index fc73bf7..a66a42b 100644 --- a/php_xxhash.c +++ b/php_xxhash.c @@ -6,6 +6,7 @@ #include "php_ini.h" #include "ext/standard/info.h" #include "php_xxhash.h" +#include "ext/hash/php_hash.h" #include "xxhash.c" @@ -13,8 +14,48 @@ ZEND_GET_MODULE(xxhash) #endif +typedef struct { + void *state; +} PHP_XXH32_CTX; + +PHP_HASH_API void PHP_XXH32Init(PHP_XXH32_CTX *context) +{ + context->state = XXH32_init(0); +} + +PHP_HASH_API void PHP_XXH32Update(PHP_XXH32_CTX *context, const unsigned char *input, unsigned int inputLen) +{ + XXH32_feed(context->state, (void *)input, (int)inputLen); +} + +PHP_HASH_API void PHP_XXH32Final(unsigned char digest[4], PHP_XXH32_CTX *context) +{ + unsigned int h32; + + h32 = XXH32_result(context->state); + + digest[0] = (unsigned char) ((h32 >> 24) & 0xff); + digest[1] = (unsigned char) ((h32 >> 16) & 0xff); + digest[2] = (unsigned char) ((h32 >> 8) & 0xff); + digest[3] = (unsigned char) (h32 & 0xff); + + context->state = XXH32_init(0); +} + +const php_hash_ops php_hash_xxh32_ops = { + (php_hash_init_func_t) PHP_XXH32Init, + (php_hash_update_func_t) PHP_XXH32Update, + (php_hash_final_func_t) PHP_XXH32Final, + (php_hash_copy_func_t) php_hash_copy, + 4, + 4, + sizeof(PHP_XXH32_CTX) +}; + PHP_MINIT_FUNCTION(xxhash) { + php_hash_register_algo("xx32", &php_hash_xxh32_ops); + return SUCCESS; } From 2c6b321a7562c3c1ee5172323d89dcb097b7ccb0 Mon Sep 17 00:00:00 2001 From: datibbaw Date: Wed, 20 Feb 2013 15:00:39 +0800 Subject: [PATCH 2/3] modified readme --- README.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 66314ca..4d7c268 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,20 @@ This extension adds one new PHP function: It will checksum the string, and return the checksum. +## `hash()` integration + +It also registers the hash algorithm to be used in the `hash()` functions: + +``` + hash('xx', $data); + +Or: + + $h = hash_init('xx'); + hash_update($h, $data); + echo hash_final($h); +``` + ## License -BSD 2-clause license. \ No newline at end of file +BSD 2-clause license. From 6691af038c6e26c9726d3456956b66497e48a1f2 Mon Sep 17 00:00:00 2001 From: datibbaw Date: Wed, 20 Feb 2013 15:02:05 +0800 Subject: [PATCH 3/3] fixed hash name typo --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 4d7c268..9fd1356 100644 --- a/README.md +++ b/README.md @@ -23,14 +23,14 @@ It will checksum the string, and return the checksum. ## `hash()` integration -It also registers the hash algorithm to be used in the `hash()` functions: +It also registers the `xx32` hash algorithm to be used in the [`hash()`](http://www.php.net/manual/en/ref.hash.php) function family: ``` - hash('xx', $data); + hash('xx32', $data); Or: - $h = hash_init('xx'); + $h = hash_init('xx32'); hash_update($h, $data); echo hash_final($h); ```