From 83d45dde99e40393c08423cb8915da819b96a1d3 Mon Sep 17 00:00:00 2001 From: yuqing Date: Thu, 22 Jan 2015 11:09:56 +0800 Subject: [PATCH] php extention export simple_hash and time33_hash --- HISTORY | 3 +- libfastcommon.spec | 2 +- php-fastcommon/fastcommon.c | 63 +++++++++++++++++++++++++++++++++++++ php-fastcommon/fastcommon.h | 2 ++ php-fastcommon/test.php | 4 +++ 5 files changed, 72 insertions(+), 2 deletions(-) diff --git a/HISTORY b/HISTORY index d506883..0412874 100644 --- a/HISTORY +++ b/HISTORY @@ -1,6 +1,7 @@ -Version 1.13 2015-01-14 +Version 1.13 2015-01-22 * support php extension + * php extention export simple_hash and time33_hash Version 1.12 2014-12-05 * bug fixed: must check the return value of vsnprintf diff --git a/libfastcommon.spec b/libfastcommon.spec index 56bce2e..f9a5afa 100644 --- a/libfastcommon.spec +++ b/libfastcommon.spec @@ -1,5 +1,5 @@ Name: libfastcommon -Version: 1.0.12 +Version: 1.0.13 Release: 1%{?dist} Summary: c common functions library extracted from my open source projects FastDFS License: GPL diff --git a/php-fastcommon/fastcommon.c b/php-fastcommon/fastcommon.c index 7566b0e..c756668 100644 --- a/php-fastcommon/fastcommon.c +++ b/php-fastcommon/fastcommon.c @@ -19,6 +19,7 @@ #include #include "local_ip_func.h" #include "logger.h" +#include "hash.h" #include "sockopt.h" #include "fastcommon.h" @@ -37,6 +38,8 @@ const zend_fcall_info empty_fcall_info = { 0, NULL, NULL, NULL, NULL, 0, NULL, N zend_function_entry fastcommon_functions[] = { ZEND_FE(fastcommon_version, NULL) ZEND_FE(fastcommon_gethostaddrs, NULL) + ZEND_FE(fastcommon_time33_hash, NULL) + ZEND_FE(fastcommon_simple_hash, NULL) {NULL, NULL, NULL} /* Must be the last line */ }; @@ -177,3 +180,63 @@ ZEND_FUNCTION(fastcommon_gethostaddrs) } } +/* +array fastcommon_time33_hash(string str) +return unsigned hash code +*/ +ZEND_FUNCTION(fastcommon_time33_hash) +{ + int argc; + char *str; + int str_len; + + argc = ZEND_NUM_ARGS(); + if (argc != 1) { + logError("file: "__FILE__", line: %d, " + "fastcommon_time33_hash parameters count: %d is invalid", + __LINE__, argc); + RETURN_BOOL(false); + } + + str = NULL; + if (zend_parse_parameters(argc TSRMLS_CC, "s", &str, + &str_len) == FAILURE) + { + logError("file: "__FILE__", line: %d, " + "zend_parse_parameters fail!", __LINE__); + RETURN_BOOL(false); + } + + RETURN_LONG(Time33Hash(str, str_len)); +} + +/* +array fastcommon_simple_hash(string str) +return unsigned hash code +*/ +ZEND_FUNCTION(fastcommon_simple_hash) +{ + int argc; + char *str; + int str_len; + + argc = ZEND_NUM_ARGS(); + if (argc != 1) { + logError("file: "__FILE__", line: %d, " + "fastcommon_simple_hash parameters count: %d is invalid", + __LINE__, argc); + RETURN_BOOL(false); + } + + str = NULL; + if (zend_parse_parameters(argc TSRMLS_CC, "s", &str, + &str_len) == FAILURE) + { + logError("file: "__FILE__", line: %d, " + "zend_parse_parameters fail!", __LINE__); + RETURN_BOOL(false); + } + + RETURN_LONG(simple_hash(str, str_len)); +} + diff --git a/php-fastcommon/fastcommon.h b/php-fastcommon/fastcommon.h index 970b11c..b6a29fa 100644 --- a/php-fastcommon/fastcommon.h +++ b/php-fastcommon/fastcommon.h @@ -19,6 +19,8 @@ PHP_MINFO_FUNCTION(fastcommon); ZEND_FUNCTION(fastcommon_version); ZEND_FUNCTION(fastcommon_gethostaddrs); +ZEND_FUNCTION(fastcommon_time33_hash); +ZEND_FUNCTION(fastcommon_simple_hash); #ifdef __cplusplus } diff --git a/php-fastcommon/test.php b/php-fastcommon/test.php index 8bd5af8..b27e8a9 100644 --- a/php-fastcommon/test.php +++ b/php-fastcommon/test.php @@ -3,3 +3,7 @@ echo 'version: ' . fastcommon_version() . "\n"; var_dump(fastcommon_gethostaddrs()); +$s = 'this is a test.'; +echo 'simple_hash: ' . fastcommon_simple_hash($s) . "\n"; +echo 'time33_hash: ' . fastcommon_time33_hash($s) . "\n"; +