aboutsummaryrefslogtreecommitdiff
path: root/src/regex/regex_block_lib.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2013-06-26 11:50:16 +0000
committerChristian Grothoff <christian@grothoff.org>2013-06-26 11:50:16 +0000
commit5f38f9933e6f19fcef0ded8aad3d6db4e6cbb666 (patch)
tree7c17cf8ad811c21520f0e73fd7124d5a33c8935b /src/regex/regex_block_lib.c
parentae4ab9e6ce8d7529bec34bef3d7db62c393dd918 (diff)
downloadgnunet-5f38f9933e6f19fcef0ded8aad3d6db4e6cbb666.tar.gz
gnunet-5f38f9933e6f19fcef0ded8aad3d6db4e6cbb666.zip
-cleaning up block code, moving all of the block logic into the block library
Diffstat (limited to 'src/regex/regex_block_lib.c')
-rw-r--r--src/regex/regex_block_lib.c115
1 files changed, 78 insertions, 37 deletions
diff --git a/src/regex/regex_block_lib.c b/src/regex/regex_block_lib.c
index 63b673fbe..052c712c1 100644
--- a/src/regex/regex_block_lib.c
+++ b/src/regex/regex_block_lib.c
@@ -20,6 +20,8 @@
20/** 20/**
21 * @author Bartlomiej Polot 21 * @author Bartlomiej Polot
22 * @file regex/regex_block_lib.c 22 * @file regex/regex_block_lib.c
23 * @brief functions for manipulating non-accept blocks stored for
24 * regex in the DHT
23 */ 25 */
24#include "platform.h" 26#include "platform.h"
25#include "regex_block_lib.h" 27#include "regex_block_lib.h"
@@ -66,45 +68,32 @@ check_edge (void *cls,
66{ 68{
67 struct regex_block_xquery_ctx *ctx = cls; 69 struct regex_block_xquery_ctx *ctx = cls;
68 70
69 71 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
70 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " edge %.*s [%u]: %s->%s\n", 72 "edge %.*s [%u]: %s->%s\n",
71 (int) len, token, len, ctx->key, GNUNET_h2s(key)); 73 (int) len, token, len, ctx->key, GNUNET_h2s(key));
72
73 if (NULL == ctx->xquery) 74 if (NULL == ctx->xquery)
74 return GNUNET_YES; 75 return GNUNET_YES;
75 if (strlen (ctx->xquery) < len) 76 if (strlen (ctx->xquery) < len)
76 { 77 return GNUNET_YES; /* too long */
77 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " too long!\n");
78 return GNUNET_YES;
79 }
80 if (0 == strncmp (ctx->xquery, token, len)) 78 if (0 == strncmp (ctx->xquery, token, len))
81 {
82 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " OK!\n");
83 ctx->found = GNUNET_OK; 79 ctx->found = GNUNET_OK;
84 }
85 else
86 {
87 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " KO!\n");
88 }
89
90 return GNUNET_YES; /* keep checking for malformed data! */ 80 return GNUNET_YES; /* keep checking for malformed data! */
91} 81}
92 82
93 83
94int 84int
95REGEX_INTERNAL_block_check (const struct RegexBlock *block, 85REGEX_INTERNAL_block_check (const struct RegexBlock *block,
96 size_t size, 86 size_t size,
97 const char *xquery) 87 const char *xquery)
98{ 88{
99 int res; 89 int res;
100 struct regex_block_xquery_ctx ctx; 90 struct regex_block_xquery_ctx ctx;
101 91
102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 92 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
103 "* Checking block with xquery \"%s\"\n", 93 "Checking block with xquery `%s'\n",
104 NULL != xquery ? xquery : "NULL"); 94 NULL != xquery ? xquery : "NULL");
105 if ( (GNUNET_YES == ntohl (block->accepting)) && 95 if ( (GNUNET_YES == ntohl (block->accepting)) &&
106 ( (NULL == xquery) || ('\0' == xquery[0]) ) 96 ( (NULL == xquery) || ('\0' == xquery[0]) ) )
107 )
108 return GNUNET_OK; 97 return GNUNET_OK;
109 ctx.xquery = xquery; 98 ctx.xquery = xquery;
110 ctx.found = GNUNET_NO; 99 ctx.found = GNUNET_NO;
@@ -133,31 +122,24 @@ REGEX_INTERNAL_block_iterate (const struct RegexBlock *block,
133 char *aux; 122 char *aux;
134 123
135 offset = sizeof (struct RegexBlock); 124 offset = sizeof (struct RegexBlock);
136 LOG (GNUNET_ERROR_TYPE_DEBUG,
137 "* Start iterating block of size %u, off %u\n",
138 size, offset);
139 if (offset >= size) /* Is it safe to access the regex block? */ 125 if (offset >= size) /* Is it safe to access the regex block? */
140 { 126 {
141 LOG (GNUNET_ERROR_TYPE_WARNING,
142 "* Block is smaller than struct RegexBlock, END\n");
143 GNUNET_break_op (0); 127 GNUNET_break_op (0);
144 return GNUNET_SYSERR; 128 return GNUNET_SYSERR;
145 } 129 }
146 n = ntohl (block->n_proof); 130 n = ntohl (block->n_proof);
147 offset += n; 131 offset += n;
148 LOG (GNUNET_ERROR_TYPE_DEBUG,
149 "* Proof length: %u, off %u\n", n, offset);
150 if (offset >= size) /* Is it safe to access the regex proof? */ 132 if (offset >= size) /* Is it safe to access the regex proof? */
151 { 133 {
152 LOG (GNUNET_ERROR_TYPE_WARNING,
153 "* Block is smaller than Block + proof, END\n");
154 GNUNET_break_op (0); 134 GNUNET_break_op (0);
155 return GNUNET_SYSERR; 135 return GNUNET_SYSERR;
156 } 136 }
157 aux = (char *) &block[1]; /* Skip regex block */ 137 aux = (char *) &block[1]; /* Skip regex block */
158 aux = &aux[n]; /* Skip regex proof */ 138 aux = &aux[n]; /* Skip regex proof */
159 n = ntohl (block->n_edges); 139 n = ntohl (block->n_edges);
160 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Edges: %u\n", n); 140 LOG (GNUNET_ERROR_TYPE_DEBUG,
141 "Start iterating block of size %u, proof %u, off %u edges %u\n",
142 size, ntohl (block->n_proof), offset, n);
161 /* aux always points at the end of the previous block */ 143 /* aux always points at the end of the previous block */
162 for (i = 0; i < n; i++) 144 for (i = 0; i < n; i++)
163 { 145 {
@@ -191,15 +173,74 @@ REGEX_INTERNAL_block_iterate (const struct RegexBlock *block,
191 /* The total size should be exactly the size of (regex + all edges) blocks 173 /* The total size should be exactly the size of (regex + all edges) blocks
192 * If size == -1, block is from cache and therefore previously checked and 174 * If size == -1, block is from cache and therefore previously checked and
193 * assumed correct. */ 175 * assumed correct. */
194 if (offset == size || SIZE_MAX == size) 176 if ( (offset != size) && (SIZE_MAX != size) )
195 { 177 {
196 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Block processed, END OK\n"); 178 GNUNET_break_op (0);
197 return GNUNET_OK; 179 return GNUNET_SYSERR;
198 } 180 }
199 LOG (GNUNET_ERROR_TYPE_WARNING, 181 return GNUNET_OK;
200 "* Size %u (%d), read %u END KO\n", size, size, offset);
201 GNUNET_break_op (0);
202 return GNUNET_SYSERR;
203} 182}
204 183
184
185/**
186 * Construct a regex block to be stored in the DHT.
187 *
188 * @param proof proof string for the block
189 * @param num_edges number of edges in the block
190 * @param edges the edges of the block
191 * @return the regex block
192 */
193struct RegexBlock *
194REGEX_INTERNAL_block_create (const struct GNUNET_HashCode *key,
195 const char *proof,
196 unsigned int num_edges,
197 const struct REGEX_INTERNAL_Edge *edges,
198 int accepting,
199 size_t *rsize)
200{
201 struct RegexBlock *block;
202 struct RegexEdge *block_edge;
203 size_t size;
204 size_t len;
205 unsigned int i;
206 unsigned int offset;
207 char *aux;
208
209 len = strlen(proof);
210 size = sizeof (struct RegexBlock) + len;
211 block = GNUNET_malloc (size);
212 block->key = *key;
213 block->n_proof = htonl (len);
214 block->n_edges = htonl (num_edges);
215 block->accepting = htonl (accepting);
216
217 /* Store the proof at the end of the block. */
218 aux = (char *) &block[1];
219 memcpy (aux, proof, len);
220 aux = &aux[len];
221
222 /* Store each edge in a variable length MeshEdge struct at the
223 * very end of the MeshRegexBlock structure.
224 */
225 for (i = 0; i < num_edges; i++)
226 {
227 /* aux points at the end of the last block */
228 len = strlen (edges[i].label);
229 size += sizeof (struct RegexEdge) + len;
230 // Calculate offset FIXME is this ok? use size instead?
231 offset = aux - (char *) block;
232 block = GNUNET_realloc (block, size);
233 aux = &((char *) block)[offset];
234 block_edge = (struct RegexEdge *) aux;
235 block_edge->key = edges[i].destination;
236 block_edge->n_token = htonl (len);
237 aux = (char *) &block_edge[1];
238 memcpy (aux, edges[i].label, len);
239 aux = &aux[len];
240 }
241 *rsize = size;
242 return block;
243}
244
245
205/* end of regex_block_lib.c */ 246/* end of regex_block_lib.c */