- allow GNUNET_BLOCK_evaluate on PUT requests for regex blocks
[oweals/gnunet.git] / src / regex / regex_block_lib.c
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  * @author Bartlomiej Polot
22  * @file regex/regex_block_lib.c
23  */
24 #include "platform.h"
25 #include "regex_block_lib.h"
26
27 #define LOG(kind,...) GNUNET_log_from (kind,"regex-bck",__VA_ARGS__)
28
29 /**
30  * Struct to keep track of the xquery while iterating all the edges in a block.
31  */
32 struct regex_block_xquery_ctx
33 {
34   /**
35    * Xquery: string we are looking for.
36    */
37   const char *xquery;
38
39   /**
40    * Has any edge matched the xquery so far? (GNUNET_OK / GNUNET_NO)
41    */
42   int found;
43 };
44
45
46 /**
47  * Iterator over all edges in a block, checking for a presence of a given query.
48  *
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.
53  * 
54  * @return GNUNET_YES, to keep iterating
55  */
56 static int
57 check_edge (void *cls,
58             const char *token,
59             size_t len,
60             const struct GNUNET_HashCode *key)
61 {
62   struct regex_block_xquery_ctx *ctx = cls;
63
64   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  edge %.*s [%u]\n",
65               (int) len, token, len);
66   if (NULL == ctx->xquery)
67     return GNUNET_YES;
68   if (strlen (ctx->xquery) < len)
69   {
70     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  too long!\n");
71     return GNUNET_YES;
72   }
73   if (0 == strncmp (ctx->xquery, token, len))
74   {
75     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  OK!\n");
76     ctx->found = GNUNET_OK;
77   }
78   else
79   {
80     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  KO!\n");
81   }
82
83   return GNUNET_YES; /* keep checking for malformed data! */
84 }
85
86
87 int
88 GNUNET_REGEX_block_check (const struct RegexBlock *block,
89                           size_t size,
90                           const char *xquery)
91 {
92   int res;
93   struct regex_block_xquery_ctx ctx;
94
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]) )
100      )
101     return GNUNET_OK;
102   ctx.xquery = xquery;
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;
107   if (NULL == xquery)
108     return GNUNET_YES;
109   return ctx.found;
110 }
111
112
113 int
114 GNUNET_REGEX_block_iterate (const struct RegexBlock *block,
115                             size_t size,
116                             GNUNET_REGEX_EgdeIterator iterator,
117                             void *iter_cls)
118 {
119   struct RegexEdge *edge;
120   unsigned int n;
121   unsigned int n_token;
122   unsigned int i;
123   size_t offset;
124   char *aux;
125
126   offset = sizeof (struct RegexBlock);
127   LOG (GNUNET_ERROR_TYPE_DEBUG,
128        "* Start iterating block of size %u, off %u\n",
129        size, offset);
130   if (offset >= size) /* Is it safe to access the regex block? */
131   {
132     LOG (GNUNET_ERROR_TYPE_WARNING,
133          "*   Block is smaller than struct RegexBlock, END\n");
134     GNUNET_break_op (0);
135     return GNUNET_SYSERR;
136   }
137   n = ntohl (block->n_proof);
138   offset += n;
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? */
142   {
143     LOG (GNUNET_ERROR_TYPE_WARNING,
144          "*   Block is smaller than Block + proof, END\n");
145     GNUNET_break_op (0);
146     return GNUNET_SYSERR;
147   }
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++)
154   {
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? */
158     {
159       LOG (GNUNET_ERROR_TYPE_WARNING,
160            "*   Size not enough for RegexEdge, END\n");
161       GNUNET_break_op (0);
162       return GNUNET_SYSERR;
163     }
164     edge = (struct RegexEdge *) aux;
165     n_token = ntohl (edge->n_token);
166     offset += 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? */
170     {
171       LOG (GNUNET_ERROR_TYPE_WARNING,
172            "*   Size not enough for edge token, END\n");
173       GNUNET_break_op (0);
174       return GNUNET_SYSERR;
175     }
176     aux = (char *) &edge[1]; /* Skip edge block */
177     if (NULL != iterator)
178         if (GNUNET_NO == iterator (iter_cls, aux, n_token, &edge->key))
179             return GNUNET_OK;
180     aux = &aux[n_token];     /* Skip edge token */
181   }
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)
186   {
187     LOG (GNUNET_ERROR_TYPE_DEBUG, "* Block processed, END OK\n");
188     return GNUNET_OK;
189   }
190   LOG (GNUNET_ERROR_TYPE_WARNING,
191        "*   Size %u (%d), read %u END KO\n", size, size, offset);
192   GNUNET_break_op (0);
193   return GNUNET_SYSERR;
194 }
195
196 /* end of regex_block_lib.c */