-
Notifications
You must be signed in to change notification settings - Fork 13
/
mss.c
463 lines (367 loc) · 10.9 KB
/
mss.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
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*
Copyright 2012 Anjuke Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <sys/time.h>
#include <php.h>
#include <php_ini.h>
#include "php_mss.h"
#include "ahocorasick.h"
//
#define MCP_TYPE_MATCH 1
#define MCP_TYPE_CLOSURE 2
#define MCP_TYPE_ARRAY 3
typedef struct {
int type;
void *value;
} match_callback_param_t;
typedef struct {
zval *callback;
zval *ext;
} user_callback_param_t;
static int match_callback_closure(AC_MATCH_t *m, user_callback_param_t *ucp
TSRMLS_DC) {
zval *retval;
zval invoke;
ZVAL_STRING(&invoke, "__invoke", 8);
zval **args[4];
int argv;
if (ucp->ext) {
args[3] = &(ucp->ext);
argv = 4;
} else {
argv = 3;
}
int i;
for (i=0; i < m->match_num; i++) {
AC_PATTERN_t *pattern = &(m->patterns[i]);
zval *kw;
zval *idx;
zval *type;
ALLOC_INIT_ZVAL(kw);
ALLOC_INIT_ZVAL(idx);
ALLOC_INIT_ZVAL(type);
ZVAL_STRING(kw, pattern->astring, pattern->length);
ZVAL_LONG(idx, m->position - pattern->length);
if (pattern->rep.stringy) {
ZVAL_STRING(type, pattern->rep.stringy, strlen(pattern->rep.stringy));
} else {
ZVAL_NULL(type);
}
args[0] = &kw;
args[1] = &idx;
args[2] = &type;
if (call_user_function_ex(NULL, &(ucp->callback), &invoke, &retval,
argv, args, 0, NULL TSRMLS_CC) != SUCCESS) {
zend_error(E_ERROR, "invoke callback failed");
}
zval_ptr_dtor(&type);
zval_ptr_dtor(&idx);
zval_ptr_dtor(&kw);
if (Z_LVAL_P(retval)) {
return 1;
}
}
return 0;
}
static int match_callback(AC_MATCH_t *m, void *param TSRMLS_DC) {
match_callback_param_t *mcp = (match_callback_param_t *)param;
if (mcp->type == MCP_TYPE_MATCH) {
*(zend_bool *)(mcp->value) = 1;
return 1;
}
if (mcp->type == MCP_TYPE_CLOSURE) {
return match_callback_closure(m,
(user_callback_param_t *)(mcp->value) TSRMLS_CC);
}
// MCP_TYPE_ARRAY
zval *matches = (zval *)(mcp->value);
int i;
for (i=0; i < m->match_num; i++) {
AC_PATTERN_t *pattern = &(m->patterns[i]);
zval *match;
ALLOC_INIT_ZVAL(match);
array_init(match);
add_index_string(match, 0, pattern->astring, 1);
add_index_long(match, 1, m->position - pattern->length);
if (pattern->rep.stringy) {
add_index_string(match, 2, pattern->rep.stringy, 1);
}
add_next_index_zval(matches, match);
}
return 0;
}
//
int le_mss, le_mss_persist;
#define PHP_MSS_RES_NAME "MSS resource"
typedef struct {
void *value;
void *next;
} list_item_t;
typedef struct {
AC_AUTOMATA_t *ac;
union {
char *name;
int persist;
};
time_t timestamp;
list_item_t *head;
list_item_t *tail;
} mss_t;
static void mss_free(mss_t *mss TSRMLS_DC) {
if (!mss) {
return;
}
if (mss->ac) {
ac_automata_release(mss->ac);
}
if (mss->name) {
pefree(mss->name, mss->persist);
}
list_item_t *curr = mss->head;
while (curr) {
list_item_t *next = curr->next;
if (curr->value) {
pefree(curr->value, mss->persist);
}
pefree(curr, mss->persist);
curr = next;
}
pefree(mss, mss->persist);
}
static void mss_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) {
mss_t *mss = (mss_t *)rsrc->ptr;
mss_free(mss TSRMLS_CC);
}
static void mss_persist_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) {
mss_t *mss = (mss_t *)rsrc->ptr;
mss_free(mss TSRMLS_CC);
}
static zend_function_entry mss_functions[] = {
PHP_FE(mss_create, NULL)
PHP_FE(mss_destroy, NULL)
PHP_FE(mss_timestamp, NULL)
PHP_FE(mss_is_ready, NULL)
PHP_FE(mss_add, NULL)
PHP_FE(mss_search, NULL)
PHP_FE(mss_match, NULL)
{NULL, NULL, NULL}
};
zend_module_entry mss_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
PHP_MSS_EXTNAME,
mss_functions, /* Functions */
PHP_MINIT(mss), /* MINIT */
NULL, /* MSHUTDOWN */
NULL, /* RINIT */
NULL, /* RSHUTDOWN */
NULL, /* MINFO */
#if ZEND_MODULE_API_NO >= 20010901
PHP_MSS_VERSION,
#endif
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_MSS
ZEND_GET_MODULE(mss)
#endif
PHP_MINIT_FUNCTION(mss) {
le_mss = zend_register_list_destructors_ex(mss_dtor, NULL,
PHP_MSS_RES_NAME, module_number);
le_mss_persist = zend_register_list_destructors_ex(NULL,
mss_persist_dtor, PHP_MSS_RES_NAME, module_number);
return SUCCESS;
}
PHP_FUNCTION(mss_create) {
char *name = NULL;
int name_len;
long expiry = -1;
zend_bool persist;
mss_t *mss = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sl",
&name, &name_len, &expiry) == FAILURE) {
RETURN_FALSE;
}
persist = name ? 1 : 0;
if (persist) {
zend_rsrc_list_entry *le;
if (zend_hash_find(&EG(persistent_list), name, name_len + 1,
(void **)&le) == SUCCESS) {
mss = le->ptr;
struct timeval tv;
gettimeofday(&tv, NULL);
if (expiry < 0 || tv.tv_sec - mss->timestamp < expiry) {
ZEND_REGISTER_RESOURCE(return_value, mss, le_mss_persist);
return;
}
zend_hash_del(&EG(persistent_list), name, name_len + 1);
}
}
mss = pemalloc(sizeof(mss_t), persist);
mss->ac = ac_automata_init(match_callback);
struct timeval tv;
gettimeofday(&tv, NULL);
mss->timestamp = tv.tv_sec;
mss->head = pemalloc(sizeof(list_item_t), persist);
mss->head->value = NULL;
mss->head->next = NULL;
mss->tail = mss->head;
if (persist) {
mss->name = pestrndup(name, name_len + 1, persist);
ZEND_REGISTER_RESOURCE(return_value, mss, le_mss_persist);
zend_rsrc_list_entry new_le;
new_le.ptr = mss;
new_le.type = le_mss_persist;
zend_hash_add(&EG(persistent_list), name, name_len + 1, &new_le,
sizeof(zend_rsrc_list_entry), NULL);
} else {
mss->name = NULL;
ZEND_REGISTER_RESOURCE(return_value, mss, le_mss);
}
}
PHP_FUNCTION(mss_destroy) {
mss_t *mss;
zval *zmss;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zmss)
== FAILURE) {
RETURN_FALSE;
}
ZEND_FETCH_RESOURCE2(mss, mss_t*, &zmss, -1, PHP_MSS_RES_NAME, le_mss,
le_mss_persist);
if (mss && mss->persist) {
zend_hash_del(&EG(persistent_list), mss->name, strlen(mss->name) + 1);
RETURN_TRUE;
}
RETURN_FALSE;
}
PHP_FUNCTION(mss_timestamp) {
mss_t *mss;
zval *zmss;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zmss)
== FAILURE) {
RETURN_FALSE;
}
ZEND_FETCH_RESOURCE2(mss, mss_t*, &zmss, -1, PHP_MSS_RES_NAME, le_mss,
le_mss_persist);
RETURN_LONG(mss->timestamp);
}
PHP_FUNCTION(mss_is_ready) {
mss_t *mss;
zval *zmss;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zmss)
== FAILURE) {
RETURN_FALSE;
}
ZEND_FETCH_RESOURCE2(mss, mss_t*, &zmss, -1, PHP_MSS_RES_NAME, le_mss,
le_mss_persist);
RETURN_BOOL(!mss->ac->automata_open)
}
PHP_FUNCTION(mss_add) {
mss_t *mss;
zval *zmss;
char *kw;
int kw_len;
char *type = NULL;
int type_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|s", &zmss,
&kw, &kw_len, &type, &type_len) == FAILURE) {
RETURN_FALSE;
}
if (kw_len < 1) {
php_error_docref(NULL TSRMLS_CC, E_WARNING,
"missing argument 'keyword'");
RETURN_FALSE;
}
ZEND_FETCH_RESOURCE2(mss, mss_t*, &zmss, -1, PHP_MSS_RES_NAME, le_mss,
le_mss_persist);
AC_PATTERN_t pattern;
pattern.astring = pestrdup(kw, mss->persist);
pattern.length = kw_len;
pattern.rep.stringy = type
? pestrdup(type, mss->persist)
: NULL;
list_item_t *item = pemalloc(sizeof(list_item_t), mss->persist);
item->value = pattern.astring;
item->next = NULL;
mss->tail->next = item;
mss->tail = item;
if (pattern.rep.stringy) {
item = pemalloc(sizeof(list_item_t), mss->persist);
item->value = pattern.rep.stringy;
item->next = NULL;
mss->tail->next = item;
mss->tail = item;
}
ac_automata_add(mss->ac, &pattern);
RETURN_TRUE;
}
PHP_FUNCTION(mss_search) {
mss_t *mss;
zval *zmss;
AC_TEXT_t text;
zval *callback = NULL;
zval *ext = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|oz", &zmss,
&text.astring, &text.length, &callback, &ext) == FAILURE) {
RETURN_FALSE;
}
ZEND_FETCH_RESOURCE2(mss, mss_t*, &zmss, -1, PHP_MSS_RES_NAME, le_mss,
le_mss_persist);
if (mss->ac->automata_open) {
ac_automata_finalize(mss->ac);
}
AC_AUTOMATA_t ac;
memcpy(&ac, mss->ac, sizeof(AC_AUTOMATA_t));
ac_automata_reset(&ac);
match_callback_param_t mcp;
if (callback) {
user_callback_param_t ucp;
ucp.callback = callback;
ucp.ext = ext;
mcp.type = MCP_TYPE_CLOSURE;
mcp.value = &ucp;
RETVAL_TRUE;
} else {
zval *matches = return_value;
array_init(matches);
mcp.type = MCP_TYPE_ARRAY;
mcp.value = matches;
}
ac_automata_search(&ac, &text, &mcp);
}
PHP_FUNCTION(mss_match) {
mss_t *mss;
zval *zmss;
AC_TEXT_t text;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &zmss,
&text.astring, &text.length) == FAILURE) {
RETURN_FALSE;
}
ZEND_FETCH_RESOURCE2(mss, mss_t*, &zmss, -1, PHP_MSS_RES_NAME, le_mss,
le_mss_persist);
if (mss->ac->automata_open) {
ac_automata_finalize(mss->ac);
}
AC_AUTOMATA_t *rac = emalloc(sizeof(AC_AUTOMATA_t));
memcpy(rac, mss->ac, sizeof(AC_AUTOMATA_t));
ac_automata_reset(rac);
match_callback_param_t mcp;
zend_bool matched = 0;
mcp.type = MCP_TYPE_MATCH;
mcp.value = &matched;
ac_automata_search(rac, &text, &mcp);
efree(rac);
RETURN_BOOL(matched);
}