-move struct RegexBlock into regex_block_lib
[oweals/gnunet.git] / src / regex / regex_block_lib.h
1 /*
2      This file is part of GNUnet.
3      (C) 2012,2013 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
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 multiples of 8 bits, need length!
56    */
57   const char *label;
58
59   /**
60    * Destionation of the edge.
61    */
62   struct GNUNET_HashCode destination;
63 };
64
65
66 /**
67  * Check if the given 'proof' matches the given 'key'.
68  *
69  * @param proof partial regex of a state
70  * @param proof_len number of bytes in 'proof'
71  * @param key hash of a state.
72  *
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 block.
86  * @param query the query for the block
87  * @param xquery String describing the edge we are looking for.
88  *               Can be NULL in case this is a put block.
89  *
90  * @return GNUNET_OK in case it's fine.
91  *         GNUNET_NO in case the xquery exists and is not found (IRRELEVANT).
92  *         GNUNET_SYSERR if the block is invalid.
93  */
94 int
95 REGEX_BLOCK_check (const struct RegexBlock *block,
96                    size_t size,
97                    const struct GNUNET_HashCode *query,
98                    const char *xquery);
99
100
101 /* FIXME: might want to use 'struct REGEX_BLOCK_Edge' here instead of 3 arguments! */
102
103 /**
104  * Iterator over edges in a block.
105  *
106  * @param cls Closure.
107  * @param token Token that follows to next state.
108  * @param len Length of token.
109  * @param key Hash of next state. 
110  *
111  * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
112  */
113 typedef int (*REGEX_INTERNAL_EgdeIterator)(void *cls,
114                                            const char *token,
115                                            size_t len,
116                                            const struct GNUNET_HashCode *key);
117
118
119 /**
120  * Iterate over all edges of a block of a regex state.
121  *
122  * @param block Block to iterate over.
123  * @param size Size of block.
124  * @param iterator Function to call on each edge in the block.
125  * @param iter_cls Closure for the iterator.
126  *
127  * @return GNUNET_SYSERR if an error has been encountered.
128  *         GNUNET_OK if no error has been encountered.
129  *           Note that if the iterator stops the iteration by returning
130  *         GNUNET_NO, the block will no longer be checked for further errors.
131  *           The return value will be GNUNET_OK meaning that no errors were
132  *         found until the edge last notified to the iterator, but there might
133  *         be errors in further edges.
134  */
135 int
136 REGEX_BLOCK_iterate (const struct RegexBlock *block,
137                             size_t size,
138                             REGEX_INTERNAL_EgdeIterator iterator,
139                             void *iter_cls);
140
141 /**
142  * Obtain the key that a particular block is to be stored under.
143  *
144  * @param block block to get the key from
145  * @param block_len number of bytes in block
146  * @param query where to store the key
147  * @return GNUNET_OK on success, GNUNET_SYSERR if the block is malformed
148  */
149 int
150 REGEX_BLOCK_get_key (const struct RegexBlock *block,
151                      size_t block_len,
152                      struct GNUNET_HashCode *key);
153
154
155 /**
156  * Test if this block is marked as being an accept state.
157  *
158  * @param block block to test
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
164
165 /**
166  * Construct a regex block to be stored in the DHT.
167  *
168  * @param proof proof string for the block
169  * @param num_edges number of edges in the block
170  * @param edges the edges of the block
171  * @param accepting is this an accepting state
172  * @param rsize set to the size of the returned block (OUT-only)
173  * @return the regex block, NULL on error
174  */
175 struct RegexBlock *
176 REGEX_BLOCK_create (const char *proof,
177                     unsigned int num_edges,
178                     const struct REGEX_BLOCK_Edge *edges,
179                     int accepting,
180                     size_t *rsize);
181
182
183 #if 0                           /* keep Emacsens' auto-indent happy */
184 {
185 #endif
186 #ifdef __cplusplus
187 }
188 #endif
189
190 /* ifndef REGEX_BLOCK_LIB_H */
191 #endif
192 /* end of regex_block_lib.h */