- fix comments
[oweals/gnunet.git] / src / mesh / mesh_block_lib.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012 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 mesh/mesh_block_lib.c
24  */
25
26 #ifdef __cplusplus
27 extern "C"
28 {
29 #if 0
30   /* keep Emacsens' auto-indent happy */
31 }
32 #endif
33 #endif
34
35 #include "mesh_block_lib.h"
36
37 /**
38  * Iterator over edges in a block.
39  *
40  * @param cls Closure.
41  * @param token Token that follows to next state.
42  * @param len Lenght of token.
43  * @param key Hash of next state.
44  */
45 static int
46 check_edge (void *cls,
47             const char *token,
48             size_t len,
49             const struct GNUNET_HashCode *key)
50 {
51   return GNUNET_YES;
52 }
53
54
55 /**
56  * Check if the regex block is well formed, including all edges
57  *
58  * @param block The start of the block.
59  * @param size The size of the block.
60  *
61  * @return GNUNET_OK in case it's fine, GNUNET_SYSERR otherwise.
62  */
63 int
64 GNUNET_MESH_regex_block_check (const struct MeshRegexBlock *block,
65                                size_t size)
66 {
67   return GNUNET_MESH_regex_block_iterate(NULL, block, size, &check_edge);
68 }
69
70
71 /**
72  * Iterate over all edges of a block of a regex state.
73  *
74  * @param cls Closure for the iterator.
75  * @param block Block to iterate over.
76  * @param size Size of block.
77  * @param iterator Function to call on each edge in the block.
78  *
79  * @return How many bytes of block have been processed
80  */
81 int
82 GNUNET_MESH_regex_block_iterate (void *cls,
83                                  const struct MeshRegexBlock *block,
84                                  size_t size,
85                                  GNUNET_MESH_EgdeIterator iterator)
86 {
87   struct MeshRegexEdge *edge;
88   unsigned int n;
89   unsigned int n_token;
90   unsigned int i;
91   size_t offset;
92   char *aux;
93
94   offset = sizeof (struct MeshRegexBlock);
95   if (offset > size) // Is it safe to access the regex block?
96     return GNUNET_SYSERR;
97   n = ntohl (block->n_proof);
98   offset =+ n;
99   if (offset > size) // Is it safe to access the regex proof?
100     return GNUNET_SYSERR;
101   aux = (char *) &block[1];  // Skip regex block
102   aux = &aux[n];             // Skip regex proof
103   n = ntohl (block->n_edges);
104   for (i = 0; i < n; n++) // aux always points at the end of the previous block
105   {
106     offset += sizeof (struct MeshRegexEdge);
107     if (offset > size) // Is it safe to access the next edge block?
108       return GNUNET_SYSERR;
109     edge = (struct MeshRegexEdge *) aux;
110     n_token = ntohl (edge->n_token);
111     offset += n_token;
112     if (offset > size) // Is it safe to access the edge token?
113       return GNUNET_SYSERR;
114     aux = (char *) &edge[1]; // Skip edge block
115     if (NULL != iterator)
116         if (GNUNET_NO == iterator (cls, aux, n_token, &edge->key))
117             return GNUNET_OK;
118     aux = &aux[n_token];     // Skip edge token
119   }
120   // The total size should be exactly the size of (regex + all edges) blocks
121   return (offset == size) ? GNUNET_OK : GNUNET_SYSERR;
122 }
123
124 #if 0                           /* keep Emacsens' auto-indent happy */
125 {
126 #endif
127 #ifdef __cplusplus
128 }
129 #endif
130
131 /* end of mesh_block_lib.c */