forked from danpurpura/MurmurHash3PHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphp_murmurhash3.cpp
132 lines (111 loc) · 3.39 KB
/
php_murmurhash3.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* MurmurHash3PHP - php extension for MurmurHash3
* Copyright (C) 2011 Jaromir Capik <tavvva@email.cz>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
extern "C" {
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_murmurhash3.h"
}
#include "MurmurHash3.h"
#define MURMURHASH3_128_OUTPUT_LENGTH 16
#define MURMURHASH3_32_OUTPUT_LENGTH 4
/* {{{ argument information */
ZEND_BEGIN_ARG_INFO_EX(arg_info_murmurhash3, 0, 0, 2)
ZEND_ARG_TYPE_INFO(0, key, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, seed, IS_LONG, 0)
ZEND_END_ARG_INFO()
static zend_function_entry murmurhash3_functions[] = {
ZEND_FALIAS(murmurhash3, murmurhash3_128, arg_info_murmurhash3)
ZEND_FE(murmurhash3_128, arg_info_murmurhash3)
ZEND_FE(murmurhash3_32, arg_info_murmurhash3)
{NULL, NULL, NULL}
};
zend_module_entry murmurhash3_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
PHP_MURMURHASH3_EXTNAME,
murmurhash3_functions,
NULL,
NULL,
NULL,
NULL,
NULL,
#if ZEND_MODULE_API_NO >= 20010901
PHP_MURMURHASH3_VERSION,
#endif
STANDARD_MODULE_PROPERTIES
};
extern "C" {
#ifdef COMPILE_DL_MURMURHASH3
ZEND_GET_MODULE(murmurhash3)
#endif
}
// Convert uint8 to hex representation (2 characters)
void c2h(uint8_t c, char *r)
{
const char *hex = "0123456789abcdef";
r[0] = hex[c / 16];
r[1] = hex[c % 16];
}
ZEND_FUNCTION(murmurhash3_128)
{
char *key;
size_t key_len;
long seed;
char output[MURMURHASH3_128_OUTPUT_LENGTH + 1];
char result[MURMURHASH3_128_OUTPUT_LENGTH * 2 + 1];
// Parse the input parameters
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sl", &key, &key_len, &seed) == FAILURE) {
RETURN_NULL();
}
// Calculate the hash
MurmurHash3_x64_128 ( key, key_len, (uint32_t)seed, output );
output[MURMURHASH3_128_OUTPUT_LENGTH] = 0;
// Convert to HEX
for (int i=0; i<MURMURHASH3_128_OUTPUT_LENGTH; i++) {
c2h(output[i], &result[i*2]);
}
result[MURMURHASH3_128_OUTPUT_LENGTH * 2] = 0;
// Return the result
RETURN_STRING(result);
}
ZEND_FUNCTION(murmurhash3_32)
{
char *key;
size_t key_len;
long seed;
char output[MURMURHASH3_32_OUTPUT_LENGTH + 1];
char result[MURMURHASH3_32_OUTPUT_LENGTH * 2 + 1];
// Parse the input parameters
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sl", &key, &key_len, &seed) == FAILURE) {
RETURN_NULL();
}
// Calculate the hash
MurmurHash3_x86_32 ( key, key_len, (uint32_t)seed, output );
output[MURMURHASH3_32_OUTPUT_LENGTH] = 0;
// Convert to HEX
for (int i=0; i<MURMURHASH3_32_OUTPUT_LENGTH; i++) {
c2h(output[i], &result[i*2]);
}
result[MURMURHASH3_32_OUTPUT_LENGTH * 2] = 0;
// Return the result
RETURN_STRING(result);
}