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