2 This file is part of GNUnet.
3 (C) 2012 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @author Bartlomiej Polot
23 * @file mesh/mesh_block_lib.c
30 /* keep Emacsens' auto-indent happy */
35 #include "mesh_block_lib.h"
38 * Noop iterator over all edges in a block.
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.
45 * @return GNUNET_YES, to keep iterating
48 check_edge (void *cls,
51 const struct GNUNET_HashCode *key)
58 * Check if the regex block is well formed, including all edges
60 * @param block The start of the block.
61 * @param size The size of the block.
63 * @return GNUNET_OK in case it's fine, GNUNET_SYSERR otherwise.
66 GNUNET_MESH_regex_block_check (const struct MeshRegexBlock *block,
69 return GNUNET_MESH_regex_block_iterate(block, size, &check_edge, NULL);
74 * Iterate over all edges of a block of a regex state.
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.
81 * @return How many bytes of block have been processed
84 GNUNET_MESH_regex_block_iterate (const struct MeshRegexBlock *block,
86 GNUNET_MESH_EgdeIterator iterator,
89 struct MeshRegexEdge *edge;
96 offset = sizeof (struct MeshRegexBlock);
97 if (offset > size) // Is it safe to access the regex block?
99 n = ntohl (block->n_proof);
101 if (offset > size) // Is it safe to access the regex proof?
102 return GNUNET_SYSERR;
103 aux = (char *) &block[1]; // Skip regex block
104 aux = &aux[n]; // Skip regex proof
105 n = ntohl (block->n_edges);
106 for (i = 0; i < n; n++) // aux always points at the end of the previous block
108 offset += sizeof (struct MeshRegexEdge);
109 if (offset > size) // Is it safe to access the next edge block?
110 return GNUNET_SYSERR;
111 edge = (struct MeshRegexEdge *) aux;
112 n_token = ntohl (edge->n_token);
114 if (offset > size) // Is it safe to access the edge token?
115 return GNUNET_SYSERR;
116 aux = (char *) &edge[1]; // Skip edge block
117 if (NULL != iterator)
118 if (GNUNET_NO == iterator (iter_cls, aux, n_token, &edge->key))
120 aux = &aux[n_token]; // Skip edge token
122 // The total size should be exactly the size of (regex + all edges) blocks
123 return (offset == size) ? GNUNET_OK : GNUNET_SYSERR;
126 #if 0 /* keep Emacsens' auto-indent happy */
133 /* end of mesh_block_lib.c */