aboutsummaryrefslogtreecommitdiff
path: root/src/regex/regex_block_lib.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2013-06-26 15:54:54 +0000
committerChristian Grothoff <christian@grothoff.org>2013-06-26 15:54:54 +0000
commit7706f0a08b2425622121c23a7d2054794f2d677c (patch)
treed4b658006d8b0418b2afabe51bfae14c3f59bb97 /src/regex/regex_block_lib.c
parent133a5ecf306d35f9e86fb5a1f8b120783f6afdf9 (diff)
downloadgnunet-7706f0a08b2425622121c23a7d2054794f2d677c.tar.gz
gnunet-7706f0a08b2425622121c23a7d2054794f2d677c.zip
-removing unnecessary key from regex blocks, compacting some of the other fields
Diffstat (limited to 'src/regex/regex_block_lib.c')
-rw-r--r--src/regex/regex_block_lib.c86
1 files changed, 56 insertions, 30 deletions
diff --git a/src/regex/regex_block_lib.c b/src/regex/regex_block_lib.c
index bd3ec9679..01c591caa 100644
--- a/src/regex/regex_block_lib.c
+++ b/src/regex/regex_block_lib.c
@@ -40,8 +40,8 @@
40 */ 40 */
41int 41int
42REGEX_BLOCK_check_proof (const char *proof, 42REGEX_BLOCK_check_proof (const char *proof,
43 size_t proof_len, 43 size_t proof_len,
44 const struct GNUNET_HashCode *key) 44 const struct GNUNET_HashCode *key)
45{ 45{
46 struct GNUNET_HashCode key_check; 46 struct GNUNET_HashCode key_check;
47 47
@@ -50,7 +50,6 @@ REGEX_BLOCK_check_proof (const char *proof,
50 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Proof check failed, was NULL.\n"); 50 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Proof check failed, was NULL.\n");
51 return GNUNET_NO; 51 return GNUNET_NO;
52 } 52 }
53
54 GNUNET_CRYPTO_hash (proof, proof_len, &key_check); 53 GNUNET_CRYPTO_hash (proof, proof_len, &key_check);
55 return (0 == 54 return (0 ==
56 GNUNET_CRYPTO_hash_cmp (key, &key_check)) ? GNUNET_OK : GNUNET_NO; 55 GNUNET_CRYPTO_hash_cmp (key, &key_check)) ? GNUNET_OK : GNUNET_NO;
@@ -72,10 +71,6 @@ struct CheckEdgeContext
72 */ 71 */
73 int found; 72 int found;
74 73
75 /**
76 * Key of the block we are iterating (for debug purposes).
77 */
78 char *key;
79}; 74};
80 75
81 76
@@ -99,7 +94,7 @@ check_edge (void *cls,
99 94
100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 95 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
101 "edge %.*s [%u]: %s->%s\n", 96 "edge %.*s [%u]: %s->%s\n",
102 (int) len, token, len, ctx->key, GNUNET_h2s(key)); 97 (int) len, token, len, GNUNET_h2s(key));
103 if (NULL == ctx->xquery) 98 if (NULL == ctx->xquery)
104 return GNUNET_YES; 99 return GNUNET_YES;
105 if (strlen (ctx->xquery) < len) 100 if (strlen (ctx->xquery) < len)
@@ -115,6 +110,7 @@ check_edge (void *cls,
115 * 110 *
116 * @param block The start of the block. 111 * @param block The start of the block.
117 * @param size The size of the block. 112 * @param size The size of the block.
113 * @param query the query for the block
118 * @param xquery String describing the edge we are looking for. 114 * @param xquery String describing the edge we are looking for.
119 * Can be NULL in case this is a put block. 115 * Can be NULL in case this is a put block.
120 * 116 *
@@ -125,34 +121,33 @@ check_edge (void *cls,
125int 121int
126REGEX_BLOCK_check (const struct RegexBlock *block, 122REGEX_BLOCK_check (const struct RegexBlock *block,
127 size_t size, 123 size_t size,
124 const struct GNUNET_HashCode *query,
128 const char *xquery) 125 const char *xquery)
129{ 126{
130 struct CheckEdgeContext ctx; 127 struct CheckEdgeContext ctx;
131 int res; 128 int res;
132 uint32_t len; 129 uint16_t len;
133 130
134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
135 "Checking block with xquery `%s'\n", 132 "Checking block with xquery `%s'\n",
136 NULL != xquery ? xquery : "NULL"); 133 NULL != xquery ? xquery : "NULL");
137 len = ntohl (block->n_proof); 134 len = ntohs (block->proof_len);
138 if (size < sizeof (struct RegexBlock) + len) 135 if (size < sizeof (struct RegexBlock) + len)
139 { 136 {
140 GNUNET_break_op (0); 137 GNUNET_break_op (0);
141 return GNUNET_SYSERR; 138 return GNUNET_SYSERR;
142 } 139 }
143 if (GNUNET_OK != REGEX_BLOCK_check_proof ((const char *) &block[1], len, &block->key)) 140 if (GNUNET_OK != REGEX_BLOCK_check_proof ((const char *) &block[1], len, query))
144 { 141 {
145 GNUNET_break_op (0); 142 GNUNET_break_op (0);
146 return GNUNET_SYSERR; 143 return GNUNET_SYSERR;
147 } 144 }
148 if ( (GNUNET_YES == ntohl (block->accepting)) && 145 if ( (GNUNET_YES == ntohs (block->is_accepting)) &&
149 ( (NULL == xquery) || ('\0' == xquery[0]) ) ) 146 ( (NULL == xquery) || ('\0' == xquery[0]) ) )
150 return GNUNET_OK; 147 return GNUNET_OK;
151 ctx.xquery = xquery; 148 ctx.xquery = xquery;
152 ctx.found = GNUNET_NO; 149 ctx.found = GNUNET_NO;
153 ctx.key = GNUNET_strdup (GNUNET_h2s (&block->key));
154 res = REGEX_BLOCK_iterate (block, size, &check_edge, &ctx); 150 res = REGEX_BLOCK_iterate (block, size, &check_edge, &ctx);
155 GNUNET_free (ctx.key);
156 if (GNUNET_SYSERR == res) 151 if (GNUNET_SYSERR == res)
157 return GNUNET_SYSERR; 152 return GNUNET_SYSERR;
158 if (NULL == xquery) 153 if (NULL == xquery)
@@ -162,6 +157,32 @@ REGEX_BLOCK_check (const struct RegexBlock *block,
162 157
163 158
164/** 159/**
160 * Obtain the key that a particular block is to be stored under.
161 *
162 * @param block block to get the key from
163 * @param block_len number of bytes in block
164 * @param query where to store the key
165 * @return GNUNET_OK on success, GNUNET_SYSERR if the block is malformed
166 */
167int
168REGEX_BLOCK_get_key (const struct RegexBlock *block,
169 size_t block_len,
170 struct GNUNET_HashCode *key)
171{
172 uint16_t len;
173
174 len = ntohs (block->proof_len);
175 if (block_len < sizeof (struct RegexBlock) + len)
176 {
177 GNUNET_break_op (0);
178 return GNUNET_SYSERR;
179 }
180 GNUNET_CRYPTO_hash (&block[1], len, key);
181 return GNUNET_OK;
182}
183
184
185/**
165 * Iterate over all edges of a block of a regex state. 186 * Iterate over all edges of a block of a regex state.
166 * 187 *
167 * @param block Block to iterate over. 188 * @param block Block to iterate over.
@@ -179,9 +200,9 @@ REGEX_BLOCK_check (const struct RegexBlock *block,
179 */ 200 */
180int 201int
181REGEX_BLOCK_iterate (const struct RegexBlock *block, 202REGEX_BLOCK_iterate (const struct RegexBlock *block,
182 size_t size, 203 size_t size,
183 REGEX_INTERNAL_EgdeIterator iterator, 204 REGEX_INTERNAL_EgdeIterator iterator,
184 void *iter_cls) 205 void *iter_cls)
185{ 206{
186 struct RegexEdge *edge; 207 struct RegexEdge *edge;
187 unsigned int n; 208 unsigned int n;
@@ -196,7 +217,7 @@ REGEX_BLOCK_iterate (const struct RegexBlock *block,
196 GNUNET_break_op (0); 217 GNUNET_break_op (0);
197 return GNUNET_SYSERR; 218 return GNUNET_SYSERR;
198 } 219 }
199 n = ntohl (block->n_proof); 220 n = ntohs (block->proof_len);
200 offset += n; 221 offset += n;
201 if (offset >= size) /* Is it safe to access the regex proof? */ 222 if (offset >= size) /* Is it safe to access the regex proof? */
202 { 223 {
@@ -208,7 +229,7 @@ REGEX_BLOCK_iterate (const struct RegexBlock *block,
208 n = ntohl (block->n_edges); 229 n = ntohl (block->n_edges);
209 LOG (GNUNET_ERROR_TYPE_DEBUG, 230 LOG (GNUNET_ERROR_TYPE_DEBUG,
210 "Start iterating block of size %u, proof %u, off %u edges %u\n", 231 "Start iterating block of size %u, proof %u, off %u edges %u\n",
211 size, ntohl (block->n_proof), offset, n); 232 size, ntohs (block->proof_len), offset, n);
212 /* aux always points at the end of the previous block */ 233 /* aux always points at the end of the previous block */
213 for (i = 0; i < n; i++) 234 for (i = 0; i < n; i++)
214 { 235 {
@@ -257,15 +278,16 @@ REGEX_BLOCK_iterate (const struct RegexBlock *block,
257 * @param proof proof string for the block 278 * @param proof proof string for the block
258 * @param num_edges number of edges in the block 279 * @param num_edges number of edges in the block
259 * @param edges the edges of the block 280 * @param edges the edges of the block
260 * @return the regex block 281 * @param accepting is this an accepting state
282 * @param rsize set to the size of the returned block (OUT-only)
283 * @return the regex block, NULL on error
261 */ 284 */
262struct RegexBlock * 285struct RegexBlock *
263REGEX_BLOCK_create (const struct GNUNET_HashCode *key, 286REGEX_BLOCK_create (const char *proof,
264 const char *proof, 287 unsigned int num_edges,
265 unsigned int num_edges, 288 const struct REGEX_BLOCK_Edge *edges,
266 const struct REGEX_BLOCK_Edge *edges, 289 int accepting,
267 int accepting, 290 size_t *rsize)
268 size_t *rsize)
269{ 291{
270 struct RegexBlock *block; 292 struct RegexBlock *block;
271 struct RegexEdge *block_edge; 293 struct RegexEdge *block_edge;
@@ -275,13 +297,17 @@ REGEX_BLOCK_create (const struct GNUNET_HashCode *key,
275 unsigned int offset; 297 unsigned int offset;
276 char *aux; 298 char *aux;
277 299
278 len = strlen(proof); 300 len = strlen (proof);
301 if (len > UINT16_MAX)
302 {
303 GNUNET_break (0);
304 return NULL;
305 }
279 size = sizeof (struct RegexBlock) + len; 306 size = sizeof (struct RegexBlock) + len;
280 block = GNUNET_malloc (size); 307 block = GNUNET_malloc (size);
281 block->key = *key; 308 block->proof_len = htons (len);
282 block->n_proof = htonl (len);
283 block->n_edges = htonl (num_edges); 309 block->n_edges = htonl (num_edges);
284 block->accepting = htonl (accepting); 310 block->is_accepting = htons (accepting);
285 311
286 /* Store the proof at the end of the block. */ 312 /* Store the proof at the end of the block. */
287 aux = (char *) &block[1]; 313 aux = (char *) &block[1];