diff options
Diffstat (limited to 'src/plugin/regex/regex_block_lib.h')
-rw-r--r-- | src/plugin/regex/regex_block_lib.h | 193 |
1 files changed, 193 insertions, 0 deletions
diff --git a/src/plugin/regex/regex_block_lib.h b/src/plugin/regex/regex_block_lib.h new file mode 100644 index 000000000..11029b9af --- /dev/null +++ b/src/plugin/regex/regex_block_lib.h | |||
@@ -0,0 +1,193 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2012,2013 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @author Bartlomiej Polot | ||
23 | * @file regex/regex_block_lib.h | ||
24 | * @brief common function to manipulate blocks stored by regex in the DHT | ||
25 | */ | ||
26 | |||
27 | #ifndef REGEX_BLOCK_LIB_H_ | ||
28 | #define REGEX_BLOCK_LIB_H_ | ||
29 | |||
30 | #ifdef __cplusplus | ||
31 | extern "C" | ||
32 | { | ||
33 | #if 0 | ||
34 | /* keep Emacsens' auto-indent happy */ | ||
35 | } | ||
36 | #endif | ||
37 | #endif | ||
38 | |||
39 | #include "platform.h" | ||
40 | #include "block_regex.h" | ||
41 | |||
42 | |||
43 | /** | ||
44 | * Representation of a Regex node (and edges) in the DHT. | ||
45 | */ | ||
46 | struct RegexBlock; | ||
47 | |||
48 | |||
49 | /** | ||
50 | * Edge representation. | ||
51 | */ | ||
52 | struct REGEX_BLOCK_Edge | ||
53 | { | ||
54 | /** | ||
55 | * Label of the edge. FIXME: might want to not consume exactly | ||
56 | * multiples of 8 bits, need length! | ||
57 | */ | ||
58 | const char *label; | ||
59 | |||
60 | /** | ||
61 | * Destination of the edge. | ||
62 | */ | ||
63 | struct GNUNET_HashCode destination; | ||
64 | }; | ||
65 | |||
66 | |||
67 | /** | ||
68 | * Check if the given 'proof' matches the given 'key'. | ||
69 | * | ||
70 | * @param proof partial regex of a state | ||
71 | * @param proof_len number of bytes in @a proof | ||
72 | * @param key hash of a state. | ||
73 | * @return #GNUNET_OK if the proof is valid for the given key. | ||
74 | */ | ||
75 | int | ||
76 | REGEX_BLOCK_check_proof (const char *proof, | ||
77 | size_t proof_len, | ||
78 | const struct GNUNET_HashCode *key); | ||
79 | |||
80 | |||
81 | /** | ||
82 | * Check if the regex block is well formed, including all edges. | ||
83 | * | ||
84 | * @param block The start of the block. | ||
85 | * @param size The size of the @a block. | ||
86 | * @param query the query for the @a block | ||
87 | * @param xquery String describing the edge we are looking for. | ||
88 | * Can be NULL in case this is a put block. | ||
89 | * @return #GNUNET_OK in case it's fine. | ||
90 | * #GNUNET_NO in case the xquery exists and is not found (IRRELEVANT). | ||
91 | * #GNUNET_SYSERR if the block is invalid. | ||
92 | */ | ||
93 | int | ||
94 | REGEX_BLOCK_check (const struct RegexBlock *block, | ||
95 | size_t size, | ||
96 | const struct GNUNET_HashCode *query, | ||
97 | const char *xquery); | ||
98 | |||
99 | |||
100 | /* FIXME: might want to use 'struct REGEX_BLOCK_Edge' here instead of 3 arguments! */ | ||
101 | |||
102 | /** | ||
103 | * Iterator over edges in a block. | ||
104 | * | ||
105 | * @param cls Closure. | ||
106 | * @param token Token that follows to next state. | ||
107 | * @param len Length of token. | ||
108 | * @param key Hash of next state. | ||
109 | * @return #GNUNET_YES if should keep iterating, #GNUNET_NO otherwise. | ||
110 | */ | ||
111 | typedef int | ||
112 | (*REGEX_INTERNAL_EgdeIterator)(void *cls, | ||
113 | const char *token, | ||
114 | size_t len, | ||
115 | const struct GNUNET_HashCode *key); | ||
116 | |||
117 | |||
118 | /** | ||
119 | * Iterate over all edges of a block of a regex state. | ||
120 | * | ||
121 | * @param block Block to iterate over. | ||
122 | * @param size Size of block. | ||
123 | * @param iterator Function to call on each edge in the block. | ||
124 | * @param iter_cls Closure for the @a iterator. | ||
125 | * @return #GNUNET_SYSERR if an error has been encountered. | ||
126 | * #GNUNET_OK if no error has been encountered. | ||
127 | * Note that if the iterator stops the iteration by returning | ||
128 | * #GNUNET_NO, the block will no longer be checked for further errors. | ||
129 | * The return value will be #GNUNET_OK meaning that no errors were | ||
130 | * found until the edge last notified to the iterator, but there might | ||
131 | * be errors in further edges. | ||
132 | */ | ||
133 | int | ||
134 | REGEX_BLOCK_iterate (const struct RegexBlock *block, | ||
135 | size_t size, | ||
136 | REGEX_INTERNAL_EgdeIterator iterator, | ||
137 | void *iter_cls); | ||
138 | |||
139 | |||
140 | /** | ||
141 | * Obtain the key that a particular block is to be stored under. | ||
142 | * | ||
143 | * @param block block to get the key from | ||
144 | * @param block_len number of bytes in @a block | ||
145 | * @param key where to store the key | ||
146 | * @return #GNUNET_OK on success, #GNUNET_SYSERR if the block is malformed | ||
147 | */ | ||
148 | int | ||
149 | REGEX_BLOCK_get_key (const struct RegexBlock *block, | ||
150 | size_t block_len, | ||
151 | struct GNUNET_HashCode *key); | ||
152 | |||
153 | |||
154 | /** | ||
155 | * Test if this block is marked as being an accept state. | ||
156 | * | ||
157 | * @param block block to test | ||
158 | * @param size number of bytes in block | ||
159 | * @return #GNUNET_YES if the block is accepting, #GNUNET_NO if not | ||
160 | */ | ||
161 | int | ||
162 | GNUNET_BLOCK_is_accepting (const struct RegexBlock *block, | ||
163 | size_t block_len); | ||
164 | |||
165 | |||
166 | /** | ||
167 | * Construct a regex block to be stored in the DHT. | ||
168 | * | ||
169 | * @param proof proof string for the block | ||
170 | * @param num_edges number of edges in the block | ||
171 | * @param edges the edges of the block | ||
172 | * @param accepting is this an accepting state | ||
173 | * @param rsize set to the size of the returned block (OUT-only) | ||
174 | * @return the regex block, NULL on error | ||
175 | */ | ||
176 | struct RegexBlock * | ||
177 | REGEX_BLOCK_create (const char *proof, | ||
178 | unsigned int num_edges, | ||
179 | const struct REGEX_BLOCK_Edge *edges, | ||
180 | int accepting, | ||
181 | size_t *rsize); | ||
182 | |||
183 | |||
184 | #if 0 /* keep Emacsens' auto-indent happy */ | ||
185 | { | ||
186 | #endif | ||
187 | #ifdef __cplusplus | ||
188 | } | ||
189 | #endif | ||
190 | |||
191 | /* ifndef REGEX_BLOCK_LIB_H */ | ||
192 | #endif | ||
193 | /* end of regex_block_lib.h */ | ||