forked from robertdavidgraham/masscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmack.h
155 lines (131 loc) · 4.56 KB
/
smack.h
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
#ifndef _SMACK_H
#define _SMACK_H
#include <stdio.h>
#define SMACK_NOT_FOUND ((size_t)(~0))
/**
* "Anchor" flags are specified only for patterns that must
* match at the start of input, at the end, or both.
* These are equivalent to the regex specifiers "^" and "$"
* respectively.
*/
enum {
SMACK_ANCHOR_BEGIN = 0x01,
SMACK_ANCHOR_END = 0x02,
SMACK_SNMP_HACK = 0x04,
SMACK_WILDCARDS = 0x08,
};
enum {
SMACK_CASE_SENSITIVE = 0,
SMACK_CASE_INSENSITIVE = 1,
};
/**
* This is the function that will be called whenever SMACK
* finds a pattern.
*/
typedef int (*FOUND_CALLBACK)(size_t id, int offset, void *data);
/**
* Create the Aho-Corasick search object. After creation, you can start
* adding patterns, but you cannot use it for searching until you've
* compiled the patterns.
*/
struct SMACK *
smack_create(const char *name, unsigned nocase);
/**
* Cleans up and frees an object created with smack_create().
*/
void
smack_destroy(struct SMACK *smack);
/**
* Registers a pattern with the search engine. The 'smack' object
* must have been created with 'smack_create()', but you must not
* have yet called 'smack_compile()' to compile the patterns. The
* "id" field can contain a pointer (size_t is 64-bit on 64-bit
* systems).
*/
void
smack_add_pattern( struct SMACK * smack,
const void * pattern,
unsigned pattern_length,
size_t id,
unsigned flags);
/**
* You call this function after you have registered all the patterns
* using 'smack_add_pattern()' in order to compile the state-machine.
* Don't use the state-machine with 'smack_search()' until you have
* compiled all the patterns with this function.
*/
void
smack_compile(struct SMACK *smack);
/**
* Run the state-machine, searching for the compiled patterns within
* a block of data/text. This can only be called after "smack_compile()"
* has created the state-machine.
*
* If the input is fragmented, this function can be called repeatedly
* for each fragment. Patterns that cross fragments will still be
* detected.
*
* The caller must initialize "*state" to zero "0" before running this
* function on the first fragment, but must thereafter leave it
* unchanged between fragments. (If the caller resets the *state variable
* to zero between each fragment, then patterns that cross fragment
* boundaries cannot be detected).
*/
unsigned
smack_search( struct SMACK * smack,
const void * px,
unsigned length,
FOUND_CALLBACK cb_found,
void * cb_data,
unsigned * state);
size_t
smack_search_next( struct SMACK * smack,
unsigned * state,
const void * px,
unsigned * offset,
unsigned length
);
/**
* Called to terminate a search (after multiple calls to `smack_search_next()`.
* This triggers any patterns that SMACK_ANCHOR_END attribute on them.
* If more than one pattern can match at the end of the search text, then
* this should be called multiple times until SMACK_NOT_FOUND is
* returned.
* @return The 'id' field of a pattenr registered with the SMACK_ANCHOR_END
* attribute if that was found in the searched buffer, or SMACK_NOT_FOUND.
*/
size_t
smack_search_next_end( struct SMACK * smack,
unsigned * state
);
#define smack_search_start(state) (*(state)) = 0
/**
* If there are multiple matches at the current state, returns the next
* one. Otherwise, returns NOT_FOUND. Used with "smack_search_next()".
*/
size_t
smack_next_match( struct SMACK * smack,
unsigned * state);
/**
* Call this after search is done. This is not generally necessary.
* It's only purpose is to detect patterns that have the
* SMACK_ANCHOR_END flag set. If no pattern has that flag, then
* this function will do nothing.
*/
unsigned
smack_search_end( struct SMACK * smack,
FOUND_CALLBACK cb_found,
void * cb_data,
unsigned * state);
/**
* Runs a regression test on the module to make sure it's compiled
* correctly for the current platform.
*
* @return
* zero if regression test succeeds, non-zero on failure
*/
int
smack_selftest(void);
int
smack_benchmark(void);
#endif /*_SMACK_H*/