Merge branch 'master' of gnunet.org:gnunet
[oweals/gnunet.git] / src / regex / regex_block_lib.h
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
19 /**
20  * @author Bartlomiej Polot
21  * @file regex/regex_block_lib.h
22  * @brief common function to manipulate blocks stored by regex in the DHT
23  */
24
25 #ifndef REGEX_BLOCK_LIB_H_
26 #define REGEX_BLOCK_LIB_H_
27
28 #ifdef __cplusplus
29 extern "C"
30 {
31 #if 0
32   /* keep Emacsens' auto-indent happy */
33 }
34 #endif
35 #endif
36
37 #include "platform.h"
38 #include "block_regex.h"
39
40
41 /**
42  * Representation of a Regex node (and edges) in the DHT.
43  */
44 struct RegexBlock;
45
46
47 /**
48  * Edge representation.
49  */
50 struct REGEX_BLOCK_Edge
51 {
52   /**
53    * Label of the edge.  FIXME: might want to not consume exactly
54    * multiples of 8 bits, need length!
55    */
56   const char *label;
57
58   /**
59    * Destionation of the edge.
60    */
61   struct GNUNET_HashCode destination;
62 };
63
64
65 /**
66  * Check if the given 'proof' matches the given 'key'.
67  *
68  * @param proof partial regex of a state
69  * @param proof_len number of bytes in @a proof
70  * @param key hash of a state.
71  * @return #GNUNET_OK if the proof is valid for the given key.
72  */
73 int
74 REGEX_BLOCK_check_proof (const char *proof,
75                          size_t proof_len,
76                          const struct GNUNET_HashCode *key);
77
78
79 /**
80  * Check if the regex block is well formed, including all edges.
81  *
82  * @param block The start of the block.
83  * @param size The size of the @a block.
84  * @param query the query for the @a block
85  * @param xquery String describing the edge we are looking for.
86  *               Can be NULL in case this is a put block.
87  * @return #GNUNET_OK in case it's fine.
88  *         #GNUNET_NO in case the xquery exists and is not found (IRRELEVANT).
89  *         #GNUNET_SYSERR if the block is invalid.
90  */
91 int
92 REGEX_BLOCK_check (const struct RegexBlock *block,
93                    size_t size,
94                    const struct GNUNET_HashCode *query,
95                    const char *xquery);
96
97
98 /* FIXME: might want to use 'struct REGEX_BLOCK_Edge' here instead of 3 arguments! */
99
100 /**
101  * Iterator over edges in a block.
102  *
103  * @param cls Closure.
104  * @param token Token that follows to next state.
105  * @param len Length of token.
106  * @param key Hash of next state.
107  * @return #GNUNET_YES if should keep iterating, #GNUNET_NO otherwise.
108  */
109 typedef int
110 (*REGEX_INTERNAL_EgdeIterator)(void *cls,
111                                const char *token,
112                                size_t len,
113                                const struct GNUNET_HashCode *key);
114
115
116 /**
117  * Iterate over all edges of a block of a regex state.
118  *
119  * @param block Block to iterate over.
120  * @param size Size of block.
121  * @param iterator Function to call on each edge in the block.
122  * @param iter_cls Closure for the @a iterator.
123  * @return #GNUNET_SYSERR if an error has been encountered.
124  *         #GNUNET_OK if no error has been encountered.
125  *           Note that if the iterator stops the iteration by returning
126  *         #GNUNET_NO, the block will no longer be checked for further errors.
127  *           The return value will be #GNUNET_OK meaning that no errors were
128  *         found until the edge last notified to the iterator, but there might
129  *         be errors in further edges.
130  */
131 int
132 REGEX_BLOCK_iterate (const struct RegexBlock *block,
133                      size_t size,
134                      REGEX_INTERNAL_EgdeIterator iterator,
135                      void *iter_cls);
136
137
138 /**
139  * Obtain the key that a particular block is to be stored under.
140  *
141  * @param block block to get the key from
142  * @param block_len number of bytes in @a block
143  * @param key where to store the key
144  * @return #GNUNET_OK on success, #GNUNET_SYSERR if the block is malformed
145  */
146 int
147 REGEX_BLOCK_get_key (const struct RegexBlock *block,
148                      size_t block_len,
149                      struct GNUNET_HashCode *key);
150
151
152 /**
153  * Test if this block is marked as being an accept state.
154  *
155  * @param block block to test
156  * @param size number of bytes in block
157  * @return #GNUNET_YES if the block is accepting, #GNUNET_NO if not
158  */
159 int
160 GNUNET_BLOCK_is_accepting (const struct RegexBlock *block,
161                            size_t block_len);
162
163
164 /**
165  * Construct a regex block to be stored in the DHT.
166  *
167  * @param proof proof string for the block
168  * @param num_edges number of edges in the block
169  * @param edges the edges of the block
170  * @param accepting is this an accepting state
171  * @param rsize set to the size of the returned block (OUT-only)
172  * @return the regex block, NULL on error
173  */
174 struct RegexBlock *
175 REGEX_BLOCK_create (const char *proof,
176                     unsigned int num_edges,
177                     const struct REGEX_BLOCK_Edge *edges,
178                     int accepting,
179                     size_t *rsize);
180
181
182 #if 0                           /* keep Emacsens' auto-indent happy */
183 {
184 #endif
185 #ifdef __cplusplus
186 }
187 #endif
188
189 /* ifndef REGEX_BLOCK_LIB_H */
190 #endif
191 /* end of regex_block_lib.h */