- leaks
[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  * Noop iterator over all edges in a block.
39  *
40  * @param cls Closure, not used.
41  * @param token Token that follows to next state.
42  * @param len Lenght of token.
43  * @param key Hash of next state.
44  * 
45  * @return GNUNET_YES, to keep iterating
46  */
47 static int
48 check_edge (void *cls,
49             const char *token,
50             size_t len,
51             const struct GNUNET_HashCode *key)
52 {
53   return GNUNET_YES;
54 }
55
56
57 /**
58  * Check if the regex block is well formed, including all edges
59  *
60  * @param block The start of the block.
61  * @param size The size of the block.
62  *
63  * @return GNUNET_OK in case it's fine, GNUNET_SYSERR otherwise.
64  */
65 int
66 GNUNET_MESH_regex_block_check (const struct MeshRegexBlock *block,
67                                size_t size)
68 {
69   return GNUNET_MESH_regex_block_iterate(block, size, &check_edge, NULL);
70 }
71
72
73 /**
74  * Iterate over all edges of a block of a regex state.
75  *
76  * @param block Block to iterate over.
77  * @param size Size of block.
78  * @param iterator Function to call on each edge in the block.
79  * @param iter_cls Closure for the iterator.
80  *
81  * @return How many bytes of block have been processed
82  */
83 int
84 GNUNET_MESH_regex_block_iterate (const struct MeshRegexBlock *block,
85                                  size_t size,
86                                  GNUNET_MESH_EgdeIterator iterator,
87                                  void *iter_cls)
88 {
89   struct MeshRegexEdge *edge;
90   unsigned int n;
91   unsigned int n_token;
92   unsigned int i;
93   size_t offset;
94   char *aux;
95
96   offset = sizeof (struct MeshRegexBlock);
97   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
98               "* Start iterating block of size %u, off %u\n",
99               size, offset);
100   if (offset > size) // Is it safe to access the regex block?
101   {
102     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
103               "*   Block is smaller than struct MeshRegexBlock, END\n");
104     return GNUNET_SYSERR;
105   }
106   n = ntohl (block->n_proof);
107   offset += n;
108   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
109               "*  Proof length: %u, off %u\n", n, offset);
110   if (offset > size) // Is it safe to access the regex proof?
111   {
112     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
113                 "*   Block is smaller than Block + proof, END\n");
114     return GNUNET_SYSERR;
115   }
116   aux = (char *) &block[1];  // Skip regex block
117   aux = &aux[n];             // Skip regex proof
118   n = ntohl (block->n_edges);
119   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*  Edges: %u\n", n);
120   for (i = 0; i < n; i++) // aux always points at the end of the previous block
121   {
122     offset += sizeof (struct MeshRegexEdge);
123     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*   Edge %u, off %u\n", i, offset);
124     if (offset > size) // Is it safe to access the next edge block?
125     {
126       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
127                   "*   Size not enough for MeshRegexEdge, END\n");
128       return GNUNET_SYSERR;
129     }
130     edge = (struct MeshRegexEdge *) aux;
131     n_token = ntohl (edge->n_token);
132     offset += n_token;
133     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
134                 "*    Token lenght %u, off %u\n", n_token, offset);
135     if (offset > size) // Is it safe to access the edge token?
136     {
137       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
138                   "*   Size not enough for edge token, END\n");
139       return GNUNET_SYSERR;
140     }
141     aux = (char *) &edge[1]; // Skip edge block
142     if (NULL != iterator)
143         if (GNUNET_NO == iterator (iter_cls, aux, n_token, &edge->key))
144             return GNUNET_OK;
145     aux = &aux[n_token];     // Skip edge token
146   }
147   // The total size should be exactly the size of (regex + all edges) blocks
148   if (offset == size)
149   {
150     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
151                   "* Block processed, END OK\n");
152     return GNUNET_OK;
153   }
154   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
155               "*   Size %u, read %u END KO\n", size, offset);
156   return GNUNET_SYSERR;
157 }
158
159 #if 0                           /* keep Emacsens' auto-indent happy */
160 {
161 #endif
162 #ifdef __cplusplus
163 }
164 #endif
165
166 /* end of mesh_block_lib.c */