-cleaning up block code, moving all of the block logic into the block library
[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  * Edge representation.
45  */
46 struct REGEX_INTERNAL_Edge
47 {
48   /**
49    * Label of the edge.  FIXME: might want to not consume exactly multiples of 8 bits, need length!
50    */
51   const char *label;
52
53   /**
54    * Destionation of the edge.
55    */
56   struct GNUNET_HashCode destination;
57 };
58
59
60 /**
61  * Check if the regex block is well formed, including all edges.
62  *
63  * @param block The start of the block.
64  * @param size The size of the block.
65  * @param xquery String describing the edge we are looking for.
66  *               Can be NULL in case this is a put block.
67  *
68  * @return GNUNET_OK in case it's fine.
69  *         GNUNET_NO in case the xquery exists and is not found (IRRELEVANT).
70  *         GNUNET_SYSERR if the block is invalid.
71  */
72 int
73 REGEX_INTERNAL_block_check (const struct RegexBlock *block,
74                             size_t size,
75                             const char *xquery);
76
77
78 /* FIXME: might want to use 'struct REGEX_INTERNAL_Edge' here instead of 3 arguments! */
79
80 /**
81  * Iterator over edges in a block.
82  *
83  * @param cls Closure.
84  * @param token Token that follows to next state.
85  * @param len Length of token.
86  * @param key Hash of next state. 
87  *
88  * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
89  */
90 typedef int (*REGEX_INTERNAL_EgdeIterator)(void *cls,
91                                          const char *token,
92                                          size_t len,
93                                          const struct GNUNET_HashCode *key);
94
95
96 /**
97  * Iterate over all edges of a block of a regex state.
98  *
99  * @param block Block to iterate over.
100  * @param size Size of block.
101  * @param iterator Function to call on each edge in the block.
102  * @param iter_cls Closure for the iterator.
103  *
104  * @return GNUNET_SYSERR if an error has been encountered.
105  *         GNUNET_OK if no error has been encountered.
106  *           Note that if the iterator stops the iteration by returning
107  *         GNUNET_NO, the block will no longer be checked for further errors.
108  *           The return value will be GNUNET_OK meaning that no errors were
109  *         found until the edge last notified to the iterator, but there might
110  *         be errors in further edges.
111  */
112 int
113 REGEX_INTERNAL_block_iterate (const struct RegexBlock *block,
114                             size_t size,
115                             REGEX_INTERNAL_EgdeIterator iterator,
116                             void *iter_cls);
117
118
119 /**
120  * Construct a regex block to be stored in the DHT.
121  *
122  * @param proof proof string for the block
123  * @param num_edges number of edges in the block
124  * @param edges the edges of the block
125  * @return the regex block
126  */
127 struct RegexBlock *
128 REGEX_INTERNAL_block_create (const struct GNUNET_HashCode *key,
129                              const char *proof,
130                              unsigned int num_edges,
131                              const struct REGEX_INTERNAL_Edge *edges,
132                              int accepting,
133                              size_t *rsize);
134
135
136 #if 0                           /* keep Emacsens' auto-indent happy */
137 {
138 #endif
139 #ifdef __cplusplus
140 }
141 #endif
142
143 /* ifndef REGEX_BLOCK_LIB_H */
144 #endif
145 /* end of regex_block_lib.h */