forked from rryqszq4/php-murmurhash
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmurmurhash.cpp
More file actions
439 lines (370 loc) · 11.7 KB
/
murmurhash.cpp
File metadata and controls
439 lines (370 loc) · 11.7 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2015 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: quanzhao <rryqszq@gmail.com> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
//#include <stdint.h>
extern "C" {
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_murmurhash.h"
}
#include "MurmurHash1.h"
#include "MurmurHash2.h"
#include "MurmurHash3.h"
/* If you declare any globals in php_murmurhash.h uncomment this:
ZEND_DECLARE_MODULE_GLOBALS(murmurhash)
*/
/* True global resources - no need for thread safety here */
static int le_murmurhash;
ZEND_BEGIN_ARG_INFO_EX(arginfo_murmurhash1, 0, 0, 2)
ZEND_ARG_INFO(0, key)
ZEND_ARG_INFO(0, seed)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_murmurhash1_aligned, 0, 0, 2)
ZEND_ARG_INFO(0, key)
ZEND_ARG_INFO(0, seed)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_murmurhash2, 0, 0, 2)
ZEND_ARG_INFO(0, key)
ZEND_ARG_INFO(0, seed)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_murmurhash64a, 0, 0, 2)
ZEND_ARG_INFO(0, key)
ZEND_ARG_INFO(0, seed)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_murmurhash64b, 0, 0, 2)
ZEND_ARG_INFO(0, key)
ZEND_ARG_INFO(0, seed)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_murmurhash2a, 0, 0, 2)
ZEND_ARG_INFO(0, key)
ZEND_ARG_INFO(0, seed)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_murmurhash_neutral2, 0, 0, 2)
ZEND_ARG_INFO(0, key)
ZEND_ARG_INFO(0, seed)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_murmurhash_aligned2, 0, 0, 2)
ZEND_ARG_INFO(0, key)
ZEND_ARG_INFO(0, seed)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_murmurhash3_x86_32, 0, 0, 2)
ZEND_ARG_INFO(0, key)
ZEND_ARG_INFO(0, seed)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_murmurhash3_x86_128, 0, 0, 2)
ZEND_ARG_INFO(0, key)
ZEND_ARG_INFO(0, seed)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_murmurhash3_x64_128, 0, 0, 2)
ZEND_ARG_INFO(0, key)
ZEND_ARG_INFO(0, seed)
ZEND_END_ARG_INFO()
/* {{{ murmurhash_functions[]
*
* Every user visible function must have an entry in murmurhash_functions[].
*/
const zend_function_entry murmurhash_functions[] = {
//PHP_FE(confirm_murmurhash_compiled, NULL) /* For testing, remove later. */
PHP_FE(murmurhash1, arginfo_murmurhash1)
PHP_FE(murmurhash1_aligned, arginfo_murmurhash1_aligned)
PHP_FE(murmurhash2, arginfo_murmurhash2)
PHP_FE(murmurhash64a, arginfo_murmurhash64a)
PHP_FE(murmurhash64b, arginfo_murmurhash64b)
PHP_FE(murmurhash2a, arginfo_murmurhash2a)
PHP_FE(murmurhash_neutral2, arginfo_murmurhash_neutral2)
PHP_FE(murmurhash_aligned2, arginfo_murmurhash_aligned2)
PHP_FE(murmurhash3_x86_32, arginfo_murmurhash3_x86_32)
PHP_FE(murmurhash3_x86_128, arginfo_murmurhash3_x86_128)
PHP_FE(murmurhash3_x64_128, arginfo_murmurhash3_x64_128)
PHP_FE_END /* Must be the last line in murmurhash_functions[] */
};
/* }}} */
/* {{{ murmurhash_module_entry
*/
zend_module_entry murmurhash_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"murmurhash",
murmurhash_functions,
PHP_MINIT(murmurhash),
PHP_MSHUTDOWN(murmurhash),
PHP_RINIT(murmurhash), /* Replace with NULL if there's nothing to do at request start */
PHP_RSHUTDOWN(murmurhash), /* Replace with NULL if there's nothing to do at request end */
PHP_MINFO(murmurhash),
#if ZEND_MODULE_API_NO >= 20010901
PHP_MURMURHASH_VERSION,
#endif
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_MURMURHASH
ZEND_GET_MODULE(murmurhash)
#endif
/* {{{ PHP_INI
*/
/* Remove comments and fill if you need to have entries in php.ini
PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("murmurhash.global_value", "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_murmurhash_globals, murmurhash_globals)
STD_PHP_INI_ENTRY("murmurhash.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_murmurhash_globals, murmurhash_globals)
PHP_INI_END()
*/
/* }}} */
/* {{{ php_murmurhash_init_globals
*/
/* Uncomment this function if you have INI entries
static void php_murmurhash_init_globals(zend_murmurhash_globals *murmurhash_globals)
{
murmurhash_globals->global_value = 0;
murmurhash_globals->global_string = NULL;
}
*/
/* }}} */
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(murmurhash)
{
/* If you have INI entries, uncomment these lines
REGISTER_INI_ENTRIES();
*/
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(murmurhash)
{
/* uncomment this line if you have INI entries
UNREGISTER_INI_ENTRIES();
*/
return SUCCESS;
}
/* }}} */
/* Remove if there's nothing to do at request start */
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION(murmurhash)
{
return SUCCESS;
}
/* }}} */
/* Remove if there's nothing to do at request end */
/* {{{ PHP_RSHUTDOWN_FUNCTION
*/
PHP_RSHUTDOWN_FUNCTION(murmurhash)
{
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(murmurhash)
{
php_info_print_table_start();
php_info_print_table_header(2, "murmurhash support", "enabled");
php_info_print_table_end();
/* Remove comments if you have entries in php.ini
DISPLAY_INI_ENTRIES();
*/
}
/* }}} */
/* Remove the following function when you have successfully modified config.m4
so that your module can be compiled into PHP, it exists only for testing
purposes. */
/* Every user-visible function in PHP should document itself in the source */
/* {{{ proto string confirm_murmurhash_compiled(string arg)
Return a string to confirm that the module is compiled in */
/*PHP_FUNCTION(confirm_murmurhash_compiled)
{
char *arg = NULL;
int arg_len, len;
char *strg;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
return;
}
php_printf("%s, %d\n", arg, arg_len);
len = spprintf(&strg, 0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "murmurhash", arg);
RETURN_STRINGL(strg, len, 0);
}*/
/* }}} */
/* The previous line is meant for vim and emacs, so it can correctly fold and
unfold functions in source code. See the corresponding marks just before
function definition, where the functions purpose is also documented. Please
follow this convention for the convenience of others editing your code.
*/
PHP_FUNCTION(murmurhash1)
{
char *key = NULL;
size_t key_len = 0;
long seed = 0;
uint32_t output;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &key, &key_len, &seed) == FAILURE) {
RETURN_NULL();
}
output = MurmurHash1(key, key_len, seed);
//printf("%s, %d, %d, %lu\n", key, key_len, seed, (uint32_t)output);
RETURN_LONG(output);
}
PHP_FUNCTION(murmurhash1_aligned)
{
char *key = NULL;
size_t key_len = 0;
long seed = 0;
uint32_t output;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &key, &key_len, &seed) == FAILURE) {
RETURN_NULL();
}
output = MurmurHash1Aligned(key, key_len, seed);
//printf("%s, %d, %d, %lu\n", key, key_len, seed, (uint32_t)output);
RETURN_LONG(output);
}
PHP_FUNCTION(murmurhash2)
{
char *key = NULL;
size_t key_len = 0;
long seed = 0;
uint32_t output;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &key, &key_len, &seed) == FAILURE) {
RETURN_NULL();
}
output = MurmurHash2(key, key_len, seed);
//printf("%s, %d, %d, %lu\n", key, key_len, seed, (uint32_t)output);
RETURN_LONG(output);
}
PHP_FUNCTION(murmurhash64a)
{
char *key = NULL;
size_t key_len = 0;
long seed = 0;
uint64_t output;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &key, &key_len, &seed) == FAILURE) {
RETURN_NULL();
}
output = MurmurHash64A(key, key_len, seed);
//printf("%s, %d, %d, %llu\n", key, key_len, seed, (uint64_t)output);
RETURN_LONG(output);
}
PHP_FUNCTION(murmurhash64b)
{
char *key = NULL;
size_t key_len = 0;
long seed = 0;
uint64_t output;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &key, &key_len, &seed) == FAILURE) {
RETURN_NULL();
}
output = MurmurHash64B(key, key_len, seed);
//printf("%s, %d, %d, %llu\n", key, key_len, seed, (uint64_t)output);
RETURN_LONG(output);
}
PHP_FUNCTION(murmurhash2a)
{
char *key = NULL;
size_t key_len = 0;
long seed = 0;
uint64_t output;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &key, &key_len, &seed) == FAILURE) {
RETURN_NULL();
}
output = MurmurHash2A(key, key_len, seed);
//printf("%s, %d, %d, %llu\n", key, key_len, seed, (uint64_t)output);
RETURN_LONG(output);
}
PHP_FUNCTION(murmurhash_neutral2)
{
char *key = NULL;
size_t key_len = 0;
long seed = 0;
uint64_t output;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &key, &key_len, &seed) == FAILURE) {
RETURN_NULL();
}
output = MurmurHashNeutral2(key, key_len, seed);
//printf("%s, %d, %d, %llu\n", key, key_len, seed, (uint64_t)output);
RETURN_LONG(output);
}
PHP_FUNCTION(murmurhash_aligned2)
{
char *key = NULL;
size_t key_len = 0;
long seed = 0;
uint64_t output;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &key, &key_len, &seed) == FAILURE) {
RETURN_NULL();
}
output = MurmurHashAligned2(key, key_len, seed);
//printf("%s, %d, %d, %llu\n", key, key_len, seed, (uint64_t)output);
RETURN_LONG(output);
}
PHP_FUNCTION(murmurhash3_x86_32)
{
char *key = NULL;
size_t key_len = 0;
long seed = 0;
uint32_t output;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &key, &key_len, &seed) == FAILURE) {
RETURN_NULL();
}
MurmurHash3_x86_32(key, key_len, seed, &output);
//printf("%s, %d, %d, %lu\n", key, key_len, seed, output);
RETURN_LONG(output);
}
PHP_FUNCTION(murmurhash3_x86_128)
{
char *key = NULL;
size_t key_len = 0;
long seed = 0;
char output[16];
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &key, &key_len, &seed) == FAILURE) {
RETURN_NULL();
}
MurmurHash3_x86_128(key, key_len, seed, &output);
#if PHP_MAJOR_VERSION > 7 || PHP_MAJOR_VERSION == 7
RETURN_STRINGL(output, 16);
#else
RETURN_STRINGL(output, 16, 1);
#endif
}
PHP_FUNCTION(murmurhash3_x64_128)
{
char *key = NULL;
size_t key_len = 0;
long seed = 0;
char output[16];
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &key, &key_len, &seed) == FAILURE) {
RETURN_NULL();
}
MurmurHash3_x64_128(key, key_len, seed, &output);
#if PHP_MAJOR_VERSION > 7 || PHP_MAJOR_VERSION == 7
RETURN_STRINGL(output, 16);
#else
RETURN_STRINGL(output, 16, 1);
#endif
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/