2 This file is part of GNUnet.
3 (C) 2012,2013 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.
21 * @author Bartlomiej Polot
22 * @file regex/regex_block_lib.c
25 #include "regex_block_lib.h"
27 #define LOG(kind,...) GNUNET_log_from (kind,"regex-bck",__VA_ARGS__)
30 * Struct to keep track of the xquery while iterating all the edges in a block.
32 struct regex_block_xquery_ctx
35 * Xquery: string we are looking for.
40 * Has any edge matched the xquery so far? (GNUNET_OK / GNUNET_NO)
47 * Iterator over all edges in a block, checking for a presence of a given query.
49 * @param cls Closure, (xquery context).
50 * @param token Token that follows to next state.
51 * @param len Lenght of token.
52 * @param key Hash of next state.
54 * @return GNUNET_YES, to keep iterating
57 check_edge (void *cls,
60 const struct GNUNET_HashCode *key)
62 struct regex_block_xquery_ctx *ctx = cls;
64 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " edge %.*s [%u]\n",
65 (int) len, token, len);
66 if (NULL == ctx->xquery)
68 if (strlen (ctx->xquery) < len)
70 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " too long!\n");
73 if (0 == strncmp (ctx->xquery, token, len))
75 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " OK!\n");
76 ctx->found = GNUNET_OK;
80 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " KO!\n");
83 return GNUNET_YES; /* keep checking for malformed data! */
88 GNUNET_REGEX_block_check (const struct RegexBlock *block,
93 struct regex_block_xquery_ctx ctx;
95 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
96 "* Checking block with xquery \"%s\"\n",
97 NULL != xquery ? xquery : "NULL");
98 if ( (GNUNET_YES == ntohl (block->accepting)) &&
99 ( (NULL == xquery) || ('\0' == xquery[0]) )
103 ctx.found = GNUNET_NO;
104 res = GNUNET_REGEX_block_iterate (block, size, &check_edge, &ctx);
105 if (GNUNET_SYSERR == res)
106 return GNUNET_SYSERR;
114 GNUNET_REGEX_block_iterate (const struct RegexBlock *block,
116 GNUNET_REGEX_EgdeIterator iterator,
119 struct RegexEdge *edge;
121 unsigned int n_token;
126 offset = sizeof (struct RegexBlock);
127 LOG (GNUNET_ERROR_TYPE_DEBUG,
128 "* Start iterating block of size %u, off %u\n",
130 if (offset >= size) /* Is it safe to access the regex block? */
132 LOG (GNUNET_ERROR_TYPE_WARNING,
133 "* Block is smaller than struct RegexBlock, END\n");
135 return GNUNET_SYSERR;
137 n = ntohl (block->n_proof);
139 LOG (GNUNET_ERROR_TYPE_DEBUG,
140 "* Proof length: %u, off %u\n", n, offset);
141 if (offset >= size) /* Is it safe to access the regex proof? */
143 LOG (GNUNET_ERROR_TYPE_WARNING,
144 "* Block is smaller than Block + proof, END\n");
146 return GNUNET_SYSERR;
148 aux = (char *) &block[1]; /* Skip regex block */
149 aux = &aux[n]; /* Skip regex proof */
150 n = ntohl (block->n_edges);
151 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Edges: %u\n", n);
152 /* aux always points at the end of the previous block */
153 for (i = 0; i < n; i++)
155 offset += sizeof (struct RegexEdge);
156 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Edge %u, off %u\n", i, offset);
157 if (offset >= size) /* Is it safe to access the next edge block? */
159 LOG (GNUNET_ERROR_TYPE_WARNING,
160 "* Size not enough for RegexEdge, END\n");
162 return GNUNET_SYSERR;
164 edge = (struct RegexEdge *) aux;
165 n_token = ntohl (edge->n_token);
167 LOG (GNUNET_ERROR_TYPE_DEBUG,
168 "* Token length %u, off %u\n", n_token, offset);
169 if (offset > size) /* Is it safe to access the edge token? */
171 LOG (GNUNET_ERROR_TYPE_WARNING,
172 "* Size not enough for edge token, END\n");
174 return GNUNET_SYSERR;
176 aux = (char *) &edge[1]; /* Skip edge block */
177 if (NULL != iterator)
178 if (GNUNET_NO == iterator (iter_cls, aux, n_token, &edge->key))
180 aux = &aux[n_token]; /* Skip edge token */
182 /* The total size should be exactly the size of (regex + all edges) blocks
183 * If size == -1, block is from cache and therefore previously checked and
184 * assumed correct. */
185 if (offset == size || SIZE_MAX == size)
187 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Block processed, END OK\n");
190 LOG (GNUNET_ERROR_TYPE_WARNING,
191 "* Size %u (%d), read %u END KO\n", size, size, offset);
193 return GNUNET_SYSERR;
196 /* end of regex_block_lib.c */