1 #ifndef SP_UTILS_H
2 #define SP_UTILS_H
3 
4 #include "ext/hash/php_hash.h"
5 #include "ext/hash/php_hash_sha.h"
6 #include "ext/standard/md5.h"
7 
8 #include "sp_config.h"
9 #include "sp_list.h"
10 
11 #if defined(__GNUC__)
12 #if __GNUC__ >= 3
13 #define sp_pure __attribute__((pure))
14 #define sp_const __attribute__((const))
15 #else
16 #define sp_pure
17 #define sp_const
18 #endif
19 #endif
20 
21 #define VAR_AND_LEN(var) var, strlen(var)
22 
23 #define SHA256_SIZE 32
24 
25 #define HOOK_FUNCTION(original_name, hook_table, new_function) \
26   hook_function(original_name, SNUFFLEUPAGUS_G(hook_table), new_function)
27 
28 #define HOOK_FUNCTION_BY_REGEXP(regexp, hook_table, new_function) \
29   hook_regexp(regexp, SNUFFLEUPAGUS_G(hook_table), new_function)
30 
31 #define SP_TYPE_LOG (0)
32 #define SP_TYPE_DROP (1)
33 #define SP_TYPE_SIMULATION (2)
34 
35 #define SP_LOG_DEBUG E_NOTICE
36 #define SP_LOG_ERROR E_ERROR
37 #define SP_LOG_WARN E_WARNING
38 
39 #define sp_log_msg(feature, level, ...) \
40   sp_log_msgf(feature, level, SP_TYPE_LOG, __VA_ARGS__)
41 #define sp_log_drop(feature, ...) \
42   sp_log_msgf(feature, SP_LOG_ERROR, SP_TYPE_DROP, __VA_ARGS__)
43 #define sp_log_simulation(feature, ...) \
44   sp_log_msgf(feature, SP_LOG_WARN, SP_TYPE_SIMULATION, __VA_ARGS__)
45 #define sp_log_auto(feature, is_simulation, ...)                     \
46   sp_log_msgf(feature, (is_simulation ? SP_LOG_WARN : SP_LOG_ERROR), \
47               (is_simulation ? SP_TYPE_SIMULATION : SP_TYPE_DROP),   \
48               __VA_ARGS__)
49 
50 #define sp_log_err(feature, ...) \
51   sp_log_msgf(feature, SP_LOG_ERROR, SP_TYPE_LOG, __VA_ARGS__)
52 #define sp_log_warn(feature, ...) \
53   sp_log_msgf(feature, SP_LOG_WARN, SP_TYPE_LOG, __VA_ARGS__)
54 #ifdef SP_DEBUG
55 #define sp_log_debug(...) \
56   sp_log_msgf("DEBUG", SP_LOG_DEBUG, SP_TYPE_LOG, __VA_ARGS__)
57 #else
58 #define sp_log_debug(...)
59 #endif
60 
61 #define GET_SUFFIX(x) (x == 1) ? "st" : ((x == 2) ? "nd" : "th")
62 
63 const char *get_ipaddr(void);
64 void sp_log_msgf(char const *restrict feature, int level, int type,
65                  const char *restrict fmt, ...);
66 int compute_hash(const char *const restrict filename, char *restrict file_hash);
67 const zend_string *sp_zval_to_zend_string(const zval *);
68 bool sp_match_value(const zend_string *, const zend_string *, const sp_pcre *);
69 bool sp_match_array_key(const zval *, const zend_string *, const sp_pcre *);
70 bool sp_match_array_value(const zval *, const zend_string *, const sp_pcre *);
71 void sp_log_disable(const char *restrict, const char *restrict,
72                     const zend_string *restrict, const sp_disabled_function *);
73 void sp_log_disable_ret(const char *restrict, const zend_string *restrict,
74                         const sp_disabled_function *);
75 int hook_function(const char *, HashTable *, zif_handler);
76 int hook_regexp(const sp_pcre *, HashTable *, zif_handler);
77 bool check_is_in_eval_whitelist(const zend_string *const function_name);
78 int sp_log_request(const zend_string *restrict folder,
79                    const zend_string *restrict text_repr,
80                    char const *const from);
81 bool sp_zend_string_equals(const zend_string *s1, const zend_string *s2);
82 
83 #endif /* SP_UTILS_H */
84