forked from Megasaxon/php-xxhash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphp_xxhash.c
220 lines (181 loc) · 4.46 KB
/
php_xxhash.c
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
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_xxhash.h"
#define XXH_STATIC_LINKING_ONLY
#define XXH_IMPLEMENTATION
#define XXH_INLINE_ALL
#include "xxhash.h"
#ifdef COMPILE_DL_XXHASH
ZEND_GET_MODULE(xxhash);
#ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE();
#endif
#endif
PHP_MINFO_FUNCTION(xxhash)
{
php_info_print_table_start();
php_info_print_table_header(2, "xxhash support", "enabled");
php_info_print_table_row(2, "extension version", PHP_XXHASH_VERSION);
php_info_print_table_row(2, "xxhash release", "0.8.0");
php_info_print_table_end();
}
static char * base62_digits="0123456789"
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
static char *b62_encode(uint64_t num)
{
/* 64 bits is up to 11 digits in base 62 */
static char enc[15] ;
int i, len ;
uint64_t rem ;
char c ;
/* Zero out output buffer */
memset(enc, 0, sizeof(enc));
if (num==0) {
enc[0]='0';
return enc;
}
i=0 ;
while (num>0) {
rem = num % 62 ;
enc[i] = base62_digits[rem];
i++ ;
num /= 62 ;
}
/* Reverse string */
len = (int)strlen(enc);
for (i=0 ; i<len/2 ; i++) {
c = enc[i] ;
enc[i] = enc[len-i-1];
enc[len-i-1] = c ;
}
return enc ;
}
uint64_t b62_decode(char * s, size_t len)
{
uint64_t num ;
uint64_t p ;
char * pos ;
int i ;
if (!s || len > 15)
return 0 ;
p=1;
num=0;
for (i=len-1 ; i>=0 ; i--) {
pos = strchr(base62_digits, (int)s[i]);
if (!pos) {
return 0 ;
}
num += (uint64_t)(pos-base62_digits) * p ;
p *= 62 ;
}
return num ;
}
PHP_FUNCTION(xxhash32)
{
zend_string *data;
zend_string *strg;
uint64_t sum;
uint64_t seed = 0;
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_STR(data)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(seed)
ZEND_PARSE_PARAMETERS_END();
// compute the checksum
sum = XXH32(ZSTR_VAL(data), ZSTR_LEN(data), seed);
//convert to a hex string
strg = strpprintf(0, "%08x", sum);
// return the checksum
RETURN_STR(strg);
}
PHP_FUNCTION(xxhash64)
{
zend_string *data;
zend_string *strg;
uint64_t sum;
uint64_t seed = 0;
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_STR(data)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(seed)
ZEND_PARSE_PARAMETERS_END();
// compute the checksum
sum = XXH64(ZSTR_VAL(data), ZSTR_LEN(data), seed);
//convert to a hex string
strg = strpprintf(0, "%08x%08x", (unsigned int)(sum >> 32), (unsigned int)sum);
// return the checksum
RETURN_STR(strg);
}
PHP_FUNCTION(xxhash64_base62)
{
zend_string *data;
zend_string *strg;
uint64_t sum;
uint64_t seed = 0;
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_STR(data)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(seed)
ZEND_PARSE_PARAMETERS_END();
// compute the checksum
sum = XXH64(ZSTR_VAL(data), ZSTR_LEN(data), seed);
//convert to a hex string
strg = strpprintf(0, "%s", b62_encode(sum));
// return the checksum
RETURN_STR(strg);
}
PHP_FUNCTION(base62_decode)
{
zend_long sum;
zend_string *data;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STR(data)
ZEND_PARSE_PARAMETERS_END();
sum = (zend_long) b62_decode(ZSTR_VAL(data), ZSTR_LEN(data));
RETURN_LONG(sum);
}
PHP_FUNCTION(base62_encode)
{
zend_long sum;
zend_string *strg;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_LONG(sum)
ZEND_PARSE_PARAMETERS_END();
strg = strpprintf(0, "%s", b62_encode(sum));
RETURN_STR(strg);
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_xxhash, 0, 0, 1)
ZEND_ARG_TYPE_INFO(0, data, IS_STRING, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_xxhash_long, 0, 0, 1)
ZEND_ARG_TYPE_INFO(0, sum, IS_LONG, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_xxhash2, 0, 0, 2)
ZEND_ARG_TYPE_INFO(0, data, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, seed, IS_LONG, 0)
ZEND_END_ARG_INFO()
const zend_function_entry xxhash_functions[] = {
ZEND_FE(xxhash32, arginfo_xxhash2)
ZEND_FE(xxhash64, arginfo_xxhash2)
ZEND_FE(xxhash64_base62, arginfo_xxhash2)
ZEND_FE(base62_decode, arginfo_xxhash)
ZEND_FE(base62_encode, arginfo_xxhash_long)
PHP_FE_END
};
zend_module_entry xxhash_module_entry = {
STANDARD_MODULE_HEADER,
"xxhash",
xxhash_functions,
NULL,
NULL,
NULL,
NULL,
PHP_MINFO(xxhash),
PHP_XXHASH_VERSION,
STANDARD_MODULE_PROPERTIES
};