summaryrefslogtreecommitdiff
path: root/src/sp_utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/sp_utils.c')
-rw-r--r--src/sp_utils.c43
1 files changed, 17 insertions, 26 deletions
diff --git a/src/sp_utils.c b/src/sp_utils.c
index 2979d98..8dbd14e 100644
--- a/src/sp_utils.c
+++ b/src/sp_utils.c
@@ -173,7 +173,8 @@ char* sp_convert_to_string(zval* zv) {
173 return estrdup(""); 173 return estrdup("");
174} 174}
175 175
176bool sp_match_value(const char* value, const char* to_match, const sp_pcre* rx) { 176bool sp_match_value(const char* value, const char* to_match,
177 const sp_pcre* rx) {
177 if (to_match) { 178 if (to_match) {
178 if (0 == strcmp(to_match, value)) { 179 if (0 == strcmp(to_match, value)) {
179 return true; 180 return true;
@@ -255,7 +256,8 @@ void sp_log_disable_ret(const char* restrict path,
255 } 256 }
256} 257}
257 258
258bool sp_match_array_key(const zval* zv, const char* to_match, const sp_pcre* rx) { 259bool sp_match_array_key(const zval* zv, const char* to_match,
260 const sp_pcre* rx) {
259 zend_string* key; 261 zend_string* key;
260 zend_ulong idx; 262 zend_ulong idx;
261 263
@@ -300,16 +302,14 @@ bool sp_match_array_value(const zval* arr, const char* to_match,
300} 302}
301 303
302int hook_function(const char* original_name, HashTable* hook_table, 304int hook_function(const char* original_name, HashTable* hook_table,
303 void (*new_function)(INTERNAL_FUNCTION_PARAMETERS), 305 void (*new_function)(INTERNAL_FUNCTION_PARAMETERS)) {
304 bool hook_execution_table) {
305 zend_internal_function* func; 306 zend_internal_function* func;
306 HashTable* ht =
307 hook_execution_table == true ? EG(function_table) : CG(function_table);
308 307
309 /* The `mb` module likes to hook functions, like strlen->mb_strlen, 308 /* The `mb` module likes to hook functions, like strlen->mb_strlen,
310 * so we have to hook both of them. */ 309 * so we have to hook both of them. */
311 310
312 if ((func = zend_hash_str_find_ptr(ht, VAR_AND_LEN(original_name)))) { 311 if ((func = zend_hash_str_find_ptr(CG(function_table),
312 VAR_AND_LEN(original_name)))) {
313 if (func->handler == new_function) { 313 if (func->handler == new_function) {
314 return SUCCESS; 314 return SUCCESS;
315 } 315 }
@@ -332,9 +332,9 @@ int hook_function(const char* original_name, HashTable* hook_table,
332 332
333 if (0 == strncmp(original_name, "mb_", 3)) { 333 if (0 == strncmp(original_name, "mb_", 3)) {
334 CG(compiler_options) |= ZEND_COMPILE_NO_BUILTIN_STRLEN; 334 CG(compiler_options) |= ZEND_COMPILE_NO_BUILTIN_STRLEN;
335 if (zend_hash_str_find(ht, VAR_AND_LEN(original_name + 3))) { 335 if (zend_hash_str_find(CG(function_table),
336 hook_function(original_name + 3, hook_table, new_function, 336 VAR_AND_LEN(original_name + 3))) {
337 hook_execution_table); 337 hook_function(original_name + 3, hook_table, new_function);
338 } 338 }
339 } else { // TODO this can be moved somewhere else to gain some marginal perfs 339 } else { // TODO this can be moved somewhere else to gain some marginal perfs
340 CG(compiler_options) |= ZEND_COMPILE_NO_BUILTIN_STRLEN; 340 CG(compiler_options) |= ZEND_COMPILE_NO_BUILTIN_STRLEN;
@@ -342,7 +342,7 @@ int hook_function(const char* original_name, HashTable* hook_table,
342 memcpy(mb_name, "mb_", 3); 342 memcpy(mb_name, "mb_", 3);
343 memcpy(mb_name + 3, VAR_AND_LEN(original_name)); 343 memcpy(mb_name + 3, VAR_AND_LEN(original_name));
344 if (zend_hash_str_find(CG(function_table), VAR_AND_LEN(mb_name))) { 344 if (zend_hash_str_find(CG(function_table), VAR_AND_LEN(mb_name))) {
345 hook_function(mb_name, hook_table, new_function, hook_execution_table); 345 hook_function(mb_name, hook_table, new_function);
346 } 346 }
347 } 347 }
348 348
@@ -350,26 +350,17 @@ int hook_function(const char* original_name, HashTable* hook_table,
350} 350}
351 351
352int hook_regexp(const sp_pcre* regexp, HashTable* hook_table, 352int hook_regexp(const sp_pcre* regexp, HashTable* hook_table,
353 void (*new_function)(INTERNAL_FUNCTION_PARAMETERS), 353 void (*new_function)(INTERNAL_FUNCTION_PARAMETERS)) {
354 bool hook_execution_table) {
355 zend_string* key; 354 zend_string* key;
356 HashTable* ht =
357 hook_execution_table == true ? EG(function_table) : CG(function_table);
358 355
359 ZEND_HASH_FOREACH_STR_KEY(ht, key) { 356 ZEND_HASH_FOREACH_STR_KEY(CG(function_table), key)
360 if (key) { 357 if (key) {
361 int ret = sp_is_regexp_matching_len(regexp, key->val, key->len); 358 if (true == sp_is_regexp_matching_len(regexp, key->val, key->len)) {
362 if (ret < 0) { /* Error or no match*/ 359 hook_function(key->val, hook_table, new_function);
363 if (PCRE_ERROR_NOMATCH != ret) {
364 sp_log_err("pcre", "Runtime error with pcre, error code: %d", ret);
365 return FAILURE;
366 }
367 continue;
368 }
369 hook_function(key->val, hook_table, new_function, hook_execution_table);
370 } 360 }
371 } 361 }
372 ZEND_HASH_FOREACH_END(); 362 ZEND_HASH_FOREACH_END();
363
373 return SUCCESS; 364 return SUCCESS;
374} 365}
375 366