2019-07-28 10:31:56 +08:00
|
|
|
#ifndef ACL_TOKEN_TREE_INCLUDE_H
|
2014-11-19 00:25:21 +08:00
|
|
|
#define ACL_TOKEN_TREE_INCLUDE_H
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
#include "acl_define.h"
|
|
|
|
#include "acl_vstring.h"
|
2017-06-05 12:21:02 +08:00
|
|
|
#include "acl_iterator.h"
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
#define ACL_PRINT_CHAR(x) \
|
|
|
|
((((x) >= 'a' && (x) <='z') \
|
|
|
|
|| ((x) >= 'A' && (x) <= 'Z') \
|
|
|
|
|| ((x) >= '0' && (x) <= '9') \
|
|
|
|
|| (x) == ';' || (x) == '!' \
|
|
|
|
|| (x) == ':' || (x) == ',' \
|
|
|
|
|| (x) == '.' || (x) == '@' \
|
|
|
|
|| (x) == '#' || (x) == '$' \
|
|
|
|
|| (x) == '%' || (x) == '^' \
|
|
|
|
|| (x) == '&' || (x) == '*' \
|
|
|
|
|| (x) == '(' || (x) == ')' \
|
|
|
|
|| (x) == '-' || (x) == '=' \
|
|
|
|
|| (x) == '|' || (x) == '\\' \
|
|
|
|
|| (x) == '[' || (x) == ']' \
|
|
|
|
|| (x) == '{' || (x) == '}' \
|
|
|
|
|| (x) == '\'' || (x) == '"') \
|
|
|
|
? (x) : '-')
|
|
|
|
|
2017-06-05 12:21:02 +08:00
|
|
|
typedef struct ACL_TOKEN ACL_TOKEN;
|
|
|
|
|
|
|
|
struct ACL_TOKEN {
|
2017-06-03 23:45:18 +08:00
|
|
|
unsigned char ch;
|
|
|
|
unsigned int flag;
|
2014-11-19 00:25:21 +08:00
|
|
|
#define ACL_TOKEN_F_NONE 0
|
|
|
|
#define ACL_TOKEN_F_STOP (1 << 0)
|
|
|
|
#define ACL_TOKEN_F_PASS (1 << 1)
|
|
|
|
#define ACL_TOKEN_F_DENY (1 << 2)
|
|
|
|
#define ACL_TOKEN_F_UTF8 (1 << 3)
|
|
|
|
|
2017-06-05 12:21:02 +08:00
|
|
|
#define ACL_TOKEN_WIDTH 256
|
2014-11-19 00:25:21 +08:00
|
|
|
struct ACL_TOKEN *tokens[ACL_TOKEN_WIDTH];
|
|
|
|
struct ACL_TOKEN *parent;
|
2017-06-03 23:45:18 +08:00
|
|
|
void *ctx;
|
2017-06-05 12:21:02 +08:00
|
|
|
|
|
|
|
ACL_TOKEN *(*iter_head)(ACL_ITER*, ACL_TOKEN*);
|
|
|
|
ACL_TOKEN *(*iter_next)(ACL_ITER*, ACL_TOKEN*);
|
|
|
|
};
|
2014-11-19 00:25:21 +08:00
|
|
|
|
2017-06-03 18:12:02 +08:00
|
|
|
#define ACL_TOKEN_TREE_WORD_MATCH(acl_token_tree_in, word_in, acl_token_out) \
|
|
|
|
{ \
|
2018-08-19 15:31:26 +08:00
|
|
|
const unsigned char *_ptr = (const unsigned char*) word_in; \
|
|
|
|
ACL_TOKEN *_token_iter = acl_token_tree_in, *_token = NULL; \
|
|
|
|
while (*_ptr) { \
|
|
|
|
_token = _token_iter->tokens[*_ptr]; \
|
|
|
|
if (_token == NULL) \
|
|
|
|
break; \
|
|
|
|
_token_iter = _token; \
|
|
|
|
_ptr++; \
|
|
|
|
} \
|
|
|
|
if (_token && (_token->flag & ACL_TOKEN_F_STOP)) \
|
|
|
|
acl_token_out = _token; \
|
|
|
|
else \
|
|
|
|
acl_token_out = NULL; \
|
2017-06-03 18:12:02 +08:00
|
|
|
}
|
|
|
|
|
2014-11-19 00:25:21 +08:00
|
|
|
#define ACL_TOKEN_TREE_MATCH(acl_token_tree_in, s_in, delim_in, delim_tab_in, acl_token_out) do \
|
|
|
|
{ \
|
2018-08-19 15:31:26 +08:00
|
|
|
ACL_TOKEN *acl_token_iter = (acl_token_tree_in), *acl_token_tmp; \
|
|
|
|
(acl_token_out) = NULL; \
|
|
|
|
if (((const char*) delim_in)) { \
|
|
|
|
int _i; \
|
|
|
|
while (*(s_in)) { \
|
|
|
|
for (_i = 0; ((const char*) delim_in)[_i]; _i++) { \
|
|
|
|
if (*(s_in) == ((const char*) delim_in)[_i]) \
|
|
|
|
goto _END; \
|
|
|
|
} \
|
|
|
|
acl_token_tmp = acl_token_iter->tokens[*((const unsigned char*)(s_in))]; \
|
|
|
|
if (acl_token_tmp == NULL) { \
|
|
|
|
if ((acl_token_out)) \
|
|
|
|
break; \
|
|
|
|
acl_token_iter = (acl_token_tree_in); \
|
|
|
|
acl_token_tmp = acl_token_iter->tokens[*((const unsigned char*)(s_in))]; \
|
|
|
|
if (acl_token_tmp == NULL) { \
|
|
|
|
(s_in)++; \
|
|
|
|
continue; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
if ((acl_token_tmp->flag & ACL_TOKEN_F_STOP)) \
|
|
|
|
(acl_token_out) = acl_token_tmp; \
|
|
|
|
acl_token_iter = acl_token_tmp; \
|
|
|
|
(s_in)++; \
|
|
|
|
} \
|
2014-11-19 00:25:21 +08:00
|
|
|
_END: \
|
2018-08-19 15:31:26 +08:00
|
|
|
break; \
|
|
|
|
} else if (((char*) delim_tab_in)) { \
|
|
|
|
while (*(s_in)) { \
|
|
|
|
if (((char*) delim_tab_in)[*((const unsigned char*)(s_in))]) \
|
|
|
|
break; \
|
|
|
|
acl_token_tmp = acl_token_iter->tokens[*((const unsigned char*)(s_in))]; \
|
|
|
|
if (acl_token_tmp == NULL) { \
|
|
|
|
if ((acl_token_out)) \
|
|
|
|
break; \
|
|
|
|
acl_token_iter = (acl_token_tree_in); \
|
|
|
|
acl_token_tmp = acl_token_iter->tokens[*((const unsigned char*)(s_in))]; \
|
|
|
|
if (acl_token_tmp == NULL) { \
|
|
|
|
(s_in)++; \
|
|
|
|
continue; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
if ((acl_token_tmp->flag & ACL_TOKEN_F_STOP)) \
|
|
|
|
(acl_token_out) = acl_token_tmp; \
|
|
|
|
acl_token_iter = acl_token_tmp; \
|
|
|
|
(s_in)++; \
|
|
|
|
} \
|
|
|
|
} else { \
|
|
|
|
while (*(s_in)) { \
|
|
|
|
acl_token_tmp = acl_token_iter->tokens[*((const unsigned char*)(s_in))]; \
|
|
|
|
if (acl_token_tmp == NULL) { \
|
|
|
|
if ((acl_token_out)) \
|
|
|
|
break; \
|
|
|
|
acl_token_iter = (acl_token_tree_in); \
|
|
|
|
acl_token_tmp = acl_token_iter->tokens[*((const unsigned char*)(s_in))]; \
|
|
|
|
if (acl_token_tmp == NULL) { \
|
|
|
|
(s_in)++; \
|
|
|
|
continue; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
if ((acl_token_tmp->flag & ACL_TOKEN_F_STOP)) \
|
|
|
|
(acl_token_out) = acl_token_tmp; \
|
|
|
|
acl_token_iter = acl_token_tmp; \
|
|
|
|
(s_in)++; \
|
|
|
|
} \
|
|
|
|
} \
|
2014-11-19 00:25:21 +08:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
ACL_API char *acl_token_delim_tab_new(const char *delim);
|
|
|
|
ACL_API void acl_token_delim_tab_free(char *delim_tab);
|
|
|
|
ACL_API ACL_TOKEN *acl_token_new(void);
|
|
|
|
ACL_API void acl_token_free(ACL_TOKEN *token);
|
2017-06-03 23:45:18 +08:00
|
|
|
ACL_API void acl_token_name(ACL_TOKEN *token, ACL_VSTRING *buf);
|
|
|
|
ACL_API const char *acl_token_name1(ACL_TOKEN *token);
|
2018-08-19 15:31:26 +08:00
|
|
|
ACL_API ACL_TOKEN *acl_token_tree_add(ACL_TOKEN *tree,
|
2014-11-19 00:25:21 +08:00
|
|
|
const char *word, unsigned int flag, const void *ctx);
|
2018-08-19 15:31:26 +08:00
|
|
|
ACL_API ACL_TOKEN *acl_token_tree_add_word_map(ACL_TOKEN *tree,
|
2014-11-19 00:25:21 +08:00
|
|
|
const char *word, const char *word_map, unsigned int flag);
|
2018-08-19 15:31:26 +08:00
|
|
|
ACL_API ACL_TOKEN *acl_token_tree_word_match(ACL_TOKEN *tree,
|
2014-11-19 00:25:21 +08:00
|
|
|
const char *word);
|
2018-08-19 15:31:26 +08:00
|
|
|
ACL_API void *acl_token_tree_word_remove(ACL_TOKEN *tree, const char *word);
|
|
|
|
ACL_API ACL_TOKEN *acl_token_tree_match(ACL_TOKEN *tree,
|
2014-11-19 00:25:21 +08:00
|
|
|
const char **ptr, const char *delim, const char *delim_tab);
|
2018-08-19 15:31:26 +08:00
|
|
|
ACL_API void acl_token_tree_walk(ACL_TOKEN *tree,
|
2017-06-03 23:45:18 +08:00
|
|
|
void (*walk_fn)(ACL_TOKEN*, void*), void *arg);
|
2018-08-19 15:31:26 +08:00
|
|
|
ACL_API void acl_token_tree_print(ACL_TOKEN *tree);
|
2014-11-19 00:25:21 +08:00
|
|
|
ACL_API ACL_TOKEN *acl_token_tree_create(const char *s);
|
|
|
|
ACL_API ACL_TOKEN *acl_token_tree_create2(const char *s, const char *sep);
|
2018-08-19 15:31:26 +08:00
|
|
|
ACL_API void acl_token_tree_destroy(ACL_TOKEN *tree);
|
|
|
|
ACL_API void acl_token_tree_load_deny(const char *filepath, ACL_TOKEN *tree);
|
|
|
|
ACL_API void acl_token_tree_load_pass(const char *filepath, ACL_TOKEN *tree);
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|