diff options
Diffstat (limited to 'src/regex/regex_test_lib.c')
-rw-r--r-- | src/regex/regex_test_lib.c | 484 |
1 files changed, 243 insertions, 241 deletions
diff --git a/src/regex/regex_test_lib.c b/src/regex/regex_test_lib.c index 7becd567c..d8eb22370 100644 --- a/src/regex/regex_test_lib.c +++ b/src/regex/regex_test_lib.c | |||
@@ -11,7 +11,7 @@ | |||
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | * Affero General Public License for more details. | 13 | * Affero General Public License for more details. |
14 | * | 14 | * |
15 | * You should have received a copy of the GNU Affero General Public License | 15 | * You should have received a copy of the GNU Affero General Public License |
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 | ||
@@ -32,8 +32,7 @@ | |||
32 | /** | 32 | /** |
33 | * Struct to hold the tree formed by prefix-combining the regexes. | 33 | * Struct to hold the tree formed by prefix-combining the regexes. |
34 | */ | 34 | */ |
35 | struct RegexCombineCtx | 35 | struct RegexCombineCtx { |
36 | { | ||
37 | /** | 36 | /** |
38 | * Child nodes with same prefix and token. | 37 | * Child nodes with same prefix and token. |
39 | */ | 38 | */ |
@@ -62,14 +61,15 @@ struct RegexCombineCtx | |||
62 | * @return Int in range [0, (base-1)] | 61 | * @return Int in range [0, (base-1)] |
63 | */ | 62 | */ |
64 | static int | 63 | static int |
65 | c2i (char c, int size) | 64 | c2i(char c, int size) |
66 | { | 65 | { |
67 | switch (size) | 66 | switch (size) |
68 | { | 67 | { |
69 | case 2: | 68 | case 2: |
70 | case 8: | 69 | case 8: |
71 | return c - '0'; | 70 | return c - '0'; |
72 | break; | 71 | break; |
72 | |||
73 | case 16: | 73 | case 16: |
74 | if (c >= '0' && c <= '9') | 74 | if (c >= '0' && c <= '9') |
75 | return c - '0'; | 75 | return c - '0'; |
@@ -78,16 +78,17 @@ c2i (char c, int size) | |||
78 | else if (c >= 'a' && c <= 'f') | 78 | else if (c >= 'a' && c <= 'f') |
79 | return c - 'a' + 10; | 79 | return c - 'a' + 10; |
80 | else | 80 | else |
81 | { | 81 | { |
82 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 82 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, |
83 | "Cannot convert char %c in base %u\n", | 83 | "Cannot convert char %c in base %u\n", |
84 | c, size); | 84 | c, size); |
85 | GNUNET_assert (0); | 85 | GNUNET_assert(0); |
86 | } | 86 | } |
87 | break; | 87 | break; |
88 | |||
88 | default: | 89 | default: |
89 | GNUNET_assert (0); | 90 | GNUNET_assert(0); |
90 | } | 91 | } |
91 | } | 92 | } |
92 | 93 | ||
93 | 94 | ||
@@ -97,10 +98,10 @@ c2i (char c, int size) | |||
97 | * @param n Indentation level | 98 | * @param n Indentation level |
98 | */ | 99 | */ |
99 | static void | 100 | static void |
100 | space (int n) | 101 | space(int n) |
101 | { | 102 | { |
102 | for (int i = 0; i < n; i++) | 103 | for (int i = 0; i < n; i++) |
103 | fprintf (stderr, "| "); | 104 | fprintf(stderr, "| "); |
104 | } | 105 | } |
105 | 106 | ||
106 | 107 | ||
@@ -111,24 +112,24 @@ space (int n) | |||
111 | * @param level Indentation level to start with | 112 | * @param level Indentation level to start with |
112 | */ | 113 | */ |
113 | static void | 114 | static void |
114 | debugctx (struct RegexCombineCtx *ctx, int level) | 115 | debugctx(struct RegexCombineCtx *ctx, int level) |
115 | { | 116 | { |
116 | #if DEBUG_REGEX | 117 | #if DEBUG_REGEX |
117 | if (NULL != ctx->s) | 118 | if (NULL != ctx->s) |
118 | { | 119 | { |
119 | space (level - 1); | 120 | space(level - 1); |
120 | fprintf (stderr, "%u:'%s'\n", c2i(ctx->s[0], ctx->size), ctx->s); | 121 | fprintf(stderr, "%u:'%s'\n", c2i(ctx->s[0], ctx->size), ctx->s); |
121 | } | 122 | } |
122 | else | 123 | else |
123 | fprintf (stderr, "ROOT (base %u)\n", ctx->size); | 124 | fprintf(stderr, "ROOT (base %u)\n", ctx->size); |
124 | for (unsigned int i = 0; i < ctx->size; i++) | 125 | for (unsigned int i = 0; i < ctx->size; i++) |
125 | { | ||
126 | if (NULL != ctx->children[i]) | ||
127 | { | 126 | { |
128 | space (level); | 127 | if (NULL != ctx->children[i]) |
129 | debugctx (ctx->children[i], level + 1); | 128 | { |
129 | space(level); | ||
130 | debugctx(ctx->children[i], level + 1); | ||
131 | } | ||
130 | } | 132 | } |
131 | } | ||
132 | fflush(stderr); | 133 | fflush(stderr); |
133 | #endif | 134 | #endif |
134 | } | 135 | } |
@@ -141,8 +142,8 @@ debugctx (struct RegexCombineCtx *ctx, int level) | |||
141 | * @param regex Regex to add. | 142 | * @param regex Regex to add. |
142 | */ | 143 | */ |
143 | static void | 144 | static void |
144 | regex_add (struct RegexCombineCtx *ctx, | 145 | regex_add(struct RegexCombineCtx *ctx, |
145 | const char *regex); | 146 | const char *regex); |
146 | 147 | ||
147 | 148 | ||
148 | /** | 149 | /** |
@@ -151,14 +152,14 @@ regex_add (struct RegexCombineCtx *ctx, | |||
151 | * @param alphabet_size Size of the alphabet (and the Trie array) | 152 | * @param alphabet_size Size of the alphabet (and the Trie array) |
152 | */ | 153 | */ |
153 | static struct RegexCombineCtx * | 154 | static struct RegexCombineCtx * |
154 | new_regex_ctx (unsigned int alphabet_size) | 155 | new_regex_ctx(unsigned int alphabet_size) |
155 | { | 156 | { |
156 | struct RegexCombineCtx *ctx; | 157 | struct RegexCombineCtx *ctx; |
157 | size_t array_size; | 158 | size_t array_size; |
158 | 159 | ||
159 | array_size = sizeof(struct RegexCombineCtx *) * alphabet_size; | 160 | array_size = sizeof(struct RegexCombineCtx *) * alphabet_size; |
160 | ctx = GNUNET_new (struct RegexCombineCtx); | 161 | ctx = GNUNET_new(struct RegexCombineCtx); |
161 | ctx->children = GNUNET_malloc (array_size); | 162 | ctx->children = GNUNET_malloc(array_size); |
162 | ctx->size = alphabet_size; | 163 | ctx->size = alphabet_size; |
163 | 164 | ||
164 | return ctx; | 165 | return ctx; |
@@ -166,19 +167,19 @@ new_regex_ctx (unsigned int alphabet_size) | |||
166 | 167 | ||
167 | 168 | ||
168 | static void | 169 | static void |
169 | move_children (struct RegexCombineCtx *dst, | 170 | move_children(struct RegexCombineCtx *dst, |
170 | const struct RegexCombineCtx *src) | 171 | const struct RegexCombineCtx *src) |
171 | { | 172 | { |
172 | size_t array_size; | 173 | size_t array_size; |
173 | 174 | ||
174 | array_size = sizeof(struct RegexCombineCtx *) * src->size; | 175 | array_size = sizeof(struct RegexCombineCtx *) * src->size; |
175 | GNUNET_memcpy (dst->children, | 176 | GNUNET_memcpy(dst->children, |
176 | src->children, | 177 | src->children, |
177 | array_size); | 178 | array_size); |
178 | for (unsigned int i = 0; i < src->size; i++) | 179 | for (unsigned int i = 0; i < src->size; i++) |
179 | { | 180 | { |
180 | src->children[i] = NULL; | 181 | src->children[i] = NULL; |
181 | } | 182 | } |
182 | } | 183 | } |
183 | 184 | ||
184 | 185 | ||
@@ -190,7 +191,7 @@ move_children (struct RegexCombineCtx *dst, | |||
190 | * @return Regex that matches any of the added regexes. | 191 | * @return Regex that matches any of the added regexes. |
191 | */ | 192 | */ |
192 | static char * | 193 | static char * |
193 | regex_combine (struct RegexCombineCtx *ctx) | 194 | regex_combine(struct RegexCombineCtx *ctx) |
194 | { | 195 | { |
195 | struct RegexCombineCtx *p; | 196 | struct RegexCombineCtx *p; |
196 | unsigned int i; | 197 | unsigned int i; |
@@ -200,56 +201,56 @@ regex_combine (struct RegexCombineCtx *ctx) | |||
200 | char *s; | 201 | char *s; |
201 | int opt; | 202 | int opt; |
202 | 203 | ||
203 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new combine %s\n", ctx->s); | 204 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "new combine %s\n", ctx->s); |
204 | regex = GNUNET_strdup (""); | 205 | regex = GNUNET_strdup(""); |
205 | opt = GNUNET_NO; | 206 | opt = GNUNET_NO; |
206 | for (i = 0; i < ctx->size; i++) | 207 | for (i = 0; i < ctx->size; i++) |
207 | { | ||
208 | p = ctx->children[i]; | ||
209 | if (NULL == p) | ||
210 | continue; | ||
211 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
212 | "adding '%s' to innner %s\n", | ||
213 | p->s, ctx->s); | ||
214 | s = regex_combine (p); | ||
215 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " total '%s'\n", s); | ||
216 | if (strlen(s) == 0) | ||
217 | { | 208 | { |
218 | opt = GNUNET_YES; | 209 | p = ctx->children[i]; |
219 | } | 210 | if (NULL == p) |
220 | else | 211 | continue; |
221 | { | 212 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, |
222 | GNUNET_asprintf (&tmp, "%s%s|", regex, s); | 213 | "adding '%s' to innner %s\n", |
223 | GNUNET_free_non_null (regex); | 214 | p->s, ctx->s); |
224 | regex = tmp; | 215 | s = regex_combine(p); |
216 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, " total '%s'\n", s); | ||
217 | if (strlen(s) == 0) | ||
218 | { | ||
219 | opt = GNUNET_YES; | ||
220 | } | ||
221 | else | ||
222 | { | ||
223 | GNUNET_asprintf(&tmp, "%s%s|", regex, s); | ||
224 | GNUNET_free_non_null(regex); | ||
225 | regex = tmp; | ||
226 | } | ||
227 | GNUNET_free_non_null(s); | ||
228 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, " so far '%s' for inner %s\n", regex, ctx->s); | ||
225 | } | 229 | } |
226 | GNUNET_free_non_null (s); | ||
227 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " so far '%s' for inner %s\n", regex, ctx->s); | ||
228 | } | ||
229 | 230 | ||
230 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "opt: %d, innner: '%s'\n", opt, regex); | 231 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "opt: %d, innner: '%s'\n", opt, regex); |
231 | len = strlen (regex); | 232 | len = strlen(regex); |
232 | if (0 == len) | 233 | if (0 == len) |
233 | { | 234 | { |
234 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "empty, returning ''\n"); | 235 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "empty, returning ''\n"); |
235 | GNUNET_free (regex); | 236 | GNUNET_free(regex); |
236 | return NULL == ctx->s ? NULL : GNUNET_strdup (ctx->s); | 237 | return NULL == ctx->s ? NULL : GNUNET_strdup(ctx->s); |
237 | } | 238 | } |
238 | 239 | ||
239 | if ('|' == regex[len - 1]) | 240 | if ('|' == regex[len - 1]) |
240 | regex[len - 1] = '\0'; | 241 | regex[len - 1] = '\0'; |
241 | 242 | ||
242 | if (NULL != ctx->s) | 243 | if (NULL != ctx->s) |
243 | { | 244 | { |
244 | if (opt) | 245 | if (opt) |
245 | GNUNET_asprintf (&s, "%s(%s)?", ctx->s, regex); | 246 | GNUNET_asprintf(&s, "%s(%s)?", ctx->s, regex); |
246 | else | 247 | else |
247 | GNUNET_asprintf (&s, "%s(%s)", ctx->s, regex); | 248 | GNUNET_asprintf(&s, "%s(%s)", ctx->s, regex); |
248 | GNUNET_free (regex); | 249 | GNUNET_free(regex); |
249 | regex = s; | 250 | regex = s; |
250 | } | 251 | } |
251 | 252 | ||
252 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "partial: %s\n", regex); | 253 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "partial: %s\n", regex); |
253 | return regex; | 254 | return regex; |
254 | } | 255 | } |
255 | 256 | ||
@@ -263,22 +264,22 @@ regex_combine (struct RegexCombineCtx *ctx) | |||
263 | * @return Number of characters of matching prefix. | 264 | * @return Number of characters of matching prefix. |
264 | */ | 265 | */ |
265 | static unsigned int | 266 | static unsigned int |
266 | get_prefix_length (const char *s1, const char *s2) | 267 | get_prefix_length(const char *s1, const char *s2) |
267 | { | 268 | { |
268 | unsigned int l1; | 269 | unsigned int l1; |
269 | unsigned int l2; | 270 | unsigned int l2; |
270 | unsigned int limit; | 271 | unsigned int limit; |
271 | unsigned int i; | 272 | unsigned int i; |
272 | 273 | ||
273 | l1 = strlen (s1); | 274 | l1 = strlen(s1); |
274 | l2 = strlen (s2); | 275 | l2 = strlen(s2); |
275 | limit = l1 > l2 ? l2 : l1; | 276 | limit = l1 > l2 ? l2 : l1; |
276 | 277 | ||
277 | for (i = 0; i < limit; i++) | 278 | for (i = 0; i < limit; i++) |
278 | { | 279 | { |
279 | if (s1[i] != s2[i]) | 280 | if (s1[i] != s2[i]) |
280 | return i; | 281 | return i; |
281 | } | 282 | } |
282 | return limit; | 283 | return limit; |
283 | } | 284 | } |
284 | 285 | ||
@@ -293,7 +294,7 @@ get_prefix_length (const char *s1, const char *s2) | |||
293 | * @return Child with the longest prefix, NULL if no child matches. | 294 | * @return Child with the longest prefix, NULL if no child matches. |
294 | */ | 295 | */ |
295 | static struct RegexCombineCtx * | 296 | static struct RegexCombineCtx * |
296 | get_longest_prefix (struct RegexCombineCtx *ctx, const char *regex) | 297 | get_longest_prefix(struct RegexCombineCtx *ctx, const char *regex) |
297 | { | 298 | { |
298 | struct RegexCombineCtx *p; | 299 | struct RegexCombineCtx *p; |
299 | struct RegexCombineCtx *best; | 300 | struct RegexCombineCtx *best; |
@@ -305,26 +306,26 @@ get_longest_prefix (struct RegexCombineCtx *ctx, const char *regex) | |||
305 | best = NULL; | 306 | best = NULL; |
306 | 307 | ||
307 | for (i = 0; i < ctx->size; i++) | 308 | for (i = 0; i < ctx->size; i++) |
308 | { | ||
309 | p = ctx->children[i]; | ||
310 | if (NULL == p) | ||
311 | continue; | ||
312 | |||
313 | l = get_prefix_length (p->s, regex); | ||
314 | if (l > best_l) | ||
315 | { | 309 | { |
316 | GNUNET_break (0 == best_l); | 310 | p = ctx->children[i]; |
317 | best = p; | 311 | if (NULL == p) |
318 | best_l = l; | 312 | continue; |
313 | |||
314 | l = get_prefix_length(p->s, regex); | ||
315 | if (l > best_l) | ||
316 | { | ||
317 | GNUNET_break(0 == best_l); | ||
318 | best = p; | ||
319 | best_l = l; | ||
320 | } | ||
319 | } | 321 | } |
320 | } | ||
321 | return best; | 322 | return best; |
322 | } | 323 | } |
323 | 324 | ||
324 | static void | 325 | static void |
325 | regex_add_multiple (struct RegexCombineCtx *ctx, | 326 | regex_add_multiple(struct RegexCombineCtx *ctx, |
326 | const char *regex, | 327 | const char *regex, |
327 | struct RegexCombineCtx **children) | 328 | struct RegexCombineCtx **children) |
328 | { | 329 | { |
329 | char tmp[2]; | 330 | char tmp[2]; |
330 | long unsigned int i; | 331 | long unsigned int i; |
@@ -333,43 +334,43 @@ regex_add_multiple (struct RegexCombineCtx *ctx, | |||
333 | unsigned int count; | 334 | unsigned int count; |
334 | 335 | ||
335 | if ('(' != regex[0]) | 336 | if ('(' != regex[0]) |
336 | { | 337 | { |
337 | GNUNET_assert (0); | 338 | GNUNET_assert(0); |
338 | } | 339 | } |
339 | 340 | ||
340 | /* Does the regex cover *all* possible children? Then don't add any, | 341 | /* Does the regex cover *all* possible children? Then don't add any, |
341 | * as it will be covered by the post-regex "(a-z)*" | 342 | * as it will be covered by the post-regex "(a-z)*" |
342 | */ | 343 | */ |
343 | l = strlen (regex); | 344 | l = strlen(regex); |
344 | count = 0; | 345 | count = 0; |
345 | for (i = 1UL; i < l; i++) | 346 | for (i = 1UL; i < l; i++) |
346 | { | ||
347 | if (regex[i] != '|' && regex[i] != ')') | ||
348 | { | 347 | { |
349 | count++; | 348 | if (regex[i] != '|' && regex[i] != ')') |
349 | { | ||
350 | count++; | ||
351 | } | ||
350 | } | 352 | } |
351 | } | ||
352 | if (count == ctx->size) | 353 | if (count == ctx->size) |
353 | { | 354 | { |
354 | return; | 355 | return; |
355 | } | 356 | } |
356 | 357 | ||
357 | /* Add every component as a child node */ | 358 | /* Add every component as a child node */ |
358 | tmp[1] = '\0'; | 359 | tmp[1] = '\0'; |
359 | for (i = 1UL; i < l; i++) | 360 | for (i = 1UL; i < l; i++) |
360 | { | ||
361 | if (regex[i] != '|' && regex[i] != ')') | ||
362 | { | 361 | { |
363 | tmp[0] = regex[i]; | 362 | if (regex[i] != '|' && regex[i] != ')') |
364 | newctx = new_regex_ctx(ctx->size); | 363 | { |
365 | newctx->s = GNUNET_strdup (tmp); | 364 | tmp[0] = regex[i]; |
366 | if (children != NULL) | 365 | newctx = new_regex_ctx(ctx->size); |
367 | GNUNET_memcpy (newctx->children, | 366 | newctx->s = GNUNET_strdup(tmp); |
368 | children, | 367 | if (children != NULL) |
369 | sizeof (*children) * ctx->size); | 368 | GNUNET_memcpy(newctx->children, |
370 | ctx->children[c2i(tmp[0], ctx->size)] = newctx; | 369 | children, |
370 | sizeof(*children) * ctx->size); | ||
371 | ctx->children[c2i(tmp[0], ctx->size)] = newctx; | ||
372 | } | ||
371 | } | 373 | } |
372 | } | ||
373 | } | 374 | } |
374 | 375 | ||
375 | /** | 376 | /** |
@@ -383,19 +384,19 @@ regex_add_multiple (struct RegexCombineCtx *ctx, | |||
383 | * @param prefix_l Lenght of common prefix of the new regex and @a ctx->s | 384 | * @param prefix_l Lenght of common prefix of the new regex and @a ctx->s |
384 | */ | 385 | */ |
385 | static void | 386 | static void |
386 | regex_split (struct RegexCombineCtx *ctx, | 387 | regex_split(struct RegexCombineCtx *ctx, |
387 | unsigned int len, | 388 | unsigned int len, |
388 | unsigned int prefix_l) | 389 | unsigned int prefix_l) |
389 | { | 390 | { |
390 | struct RegexCombineCtx *newctx; | 391 | struct RegexCombineCtx *newctx; |
391 | unsigned int idx; | 392 | unsigned int idx; |
392 | char *suffix; | 393 | char *suffix; |
393 | 394 | ||
394 | suffix = GNUNET_malloc (len - prefix_l + 1); | 395 | suffix = GNUNET_malloc(len - prefix_l + 1); |
395 | /* | 396 | /* |
396 | * We can use GNUNET_strlcpy because ctx->s is null-terminated | 397 | * We can use GNUNET_strlcpy because ctx->s is null-terminated |
397 | */ | 398 | */ |
398 | GNUNET_strlcpy (suffix, &ctx->s[prefix_l], len - prefix_l + 1); | 399 | GNUNET_strlcpy(suffix, &ctx->s[prefix_l], len - prefix_l + 1); |
399 | 400 | ||
400 | /* Suffix saved, truncate current node so it only contains the prefix, | 401 | /* Suffix saved, truncate current node so it only contains the prefix, |
401 | * copy any children nodes to put as grandchildren and initialize new empty | 402 | * copy any children nodes to put as grandchildren and initialize new empty |
@@ -405,21 +406,21 @@ regex_split (struct RegexCombineCtx *ctx, | |||
405 | 406 | ||
406 | /* If the suffix is an OR expression, add multiple children */ | 407 | /* If the suffix is an OR expression, add multiple children */ |
407 | if ('(' == suffix[0]) | 408 | if ('(' == suffix[0]) |
408 | { | 409 | { |
409 | struct RegexCombineCtx **tmp; | 410 | struct RegexCombineCtx **tmp; |
410 | 411 | ||
411 | tmp = ctx->children; | 412 | tmp = ctx->children; |
412 | ctx->children = GNUNET_malloc (sizeof(*tmp) * ctx->size); | 413 | ctx->children = GNUNET_malloc(sizeof(*tmp) * ctx->size); |
413 | regex_add_multiple (ctx, suffix, tmp); | 414 | regex_add_multiple(ctx, suffix, tmp); |
414 | GNUNET_free (suffix); | 415 | GNUNET_free(suffix); |
415 | GNUNET_free (tmp); | 416 | GNUNET_free(tmp); |
416 | return; | 417 | return; |
417 | } | 418 | } |
418 | 419 | ||
419 | /* The suffix is a normal string, add as one node */ | 420 | /* The suffix is a normal string, add as one node */ |
420 | newctx = new_regex_ctx (ctx->size); | 421 | newctx = new_regex_ctx(ctx->size); |
421 | newctx->s = suffix; | 422 | newctx->s = suffix; |
422 | move_children (newctx, ctx); | 423 | move_children(newctx, ctx); |
423 | idx = c2i(suffix[0], ctx->size); | 424 | idx = c2i(suffix[0], ctx->size); |
424 | ctx->children[idx] = newctx; | 425 | ctx->children[idx] = newctx; |
425 | } | 426 | } |
@@ -432,7 +433,7 @@ regex_split (struct RegexCombineCtx *ctx, | |||
432 | * @param regex Regex to add. | 433 | * @param regex Regex to add. |
433 | */ | 434 | */ |
434 | static void | 435 | static void |
435 | regex_add (struct RegexCombineCtx *ctx, const char *regex) | 436 | regex_add(struct RegexCombineCtx *ctx, const char *regex) |
436 | { | 437 | { |
437 | struct RegexCombineCtx *p; | 438 | struct RegexCombineCtx *p; |
438 | struct RegexCombineCtx *newctx; | 439 | struct RegexCombineCtx *newctx; |
@@ -443,54 +444,54 @@ regex_add (struct RegexCombineCtx *ctx, const char *regex) | |||
443 | size_t len; | 444 | size_t len; |
444 | int idx; | 445 | int idx; |
445 | 446 | ||
446 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 447 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, |
447 | "regex_add '%s' into '%s'\n", | 448 | "regex_add '%s' into '%s'\n", |
448 | regex, ctx->s); | 449 | regex, ctx->s); |
449 | l = strlen (regex); | 450 | l = strlen(regex); |
450 | if (0UL == l) | 451 | if (0UL == l) |
451 | return; | 452 | return; |
452 | 453 | ||
453 | /* If the regex is in the form of (a|b|c), add every character separately */ | 454 | /* If the regex is in the form of (a|b|c), add every character separately */ |
454 | if ('(' == regex[0]) | 455 | if ('(' == regex[0]) |
455 | { | 456 | { |
456 | regex_add_multiple (ctx, regex, NULL); | 457 | regex_add_multiple(ctx, regex, NULL); |
457 | return; | 458 | return; |
458 | } | 459 | } |
459 | 460 | ||
460 | p = get_longest_prefix (ctx, regex); | 461 | p = get_longest_prefix(ctx, regex); |
461 | if (NULL != p) | 462 | if (NULL != p) |
462 | { | ||
463 | /* There is some prefix match, reduce regex and try again */ | ||
464 | prefix_l = get_prefix_length (p->s, regex); | ||
465 | rest_s = &p->s[prefix_l]; | ||
466 | rest_r = ®ex[prefix_l]; | ||
467 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "chosen '%s' [%u]\n", p->s, prefix_l); | ||
468 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "prefix r '%.*s'\n", prefix_l, p->s); | ||
469 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "rest r '%s'\n", rest_r); | ||
470 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "rest s '%s'\n", rest_s); | ||
471 | len = strlen (p->s); | ||
472 | if (prefix_l < len) | ||
473 | { | 463 | { |
474 | regex_split (p, len, prefix_l); | 464 | /* There is some prefix match, reduce regex and try again */ |
465 | prefix_l = get_prefix_length(p->s, regex); | ||
466 | rest_s = &p->s[prefix_l]; | ||
467 | rest_r = ®ex[prefix_l]; | ||
468 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "chosen '%s' [%u]\n", p->s, prefix_l); | ||
469 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "prefix r '%.*s'\n", prefix_l, p->s); | ||
470 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "rest r '%s'\n", rest_r); | ||
471 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "rest s '%s'\n", rest_s); | ||
472 | len = strlen(p->s); | ||
473 | if (prefix_l < len) | ||
474 | { | ||
475 | regex_split(p, len, prefix_l); | ||
476 | } | ||
477 | regex_add(p, rest_r); | ||
478 | return; | ||
475 | } | 479 | } |
476 | regex_add (p, rest_r); | ||
477 | return; | ||
478 | } | ||
479 | 480 | ||
480 | /* There is no prefix match, add new */ | 481 | /* There is no prefix match, add new */ |
481 | idx = c2i(regex[0], ctx->size); | 482 | idx = c2i(regex[0], ctx->size); |
482 | if (NULL == ctx->children[idx] && NULL != ctx->s) | 483 | if (NULL == ctx->children[idx] && NULL != ctx->s) |
483 | { | 484 | { |
484 | /* this was the end before, add empty string */ | 485 | /* this was the end before, add empty string */ |
485 | newctx = new_regex_ctx (ctx->size); | 486 | newctx = new_regex_ctx(ctx->size); |
486 | newctx->s = GNUNET_strdup (""); | 487 | newctx->s = GNUNET_strdup(""); |
487 | ctx->children[idx] = newctx; | 488 | ctx->children[idx] = newctx; |
488 | } | 489 | } |
489 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no match\n"); | 490 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, " no match\n"); |
490 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " new state %s\n", regex); | 491 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, " new state %s\n", regex); |
491 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " under %s\n", ctx->s); | 492 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, " under %s\n", ctx->s); |
492 | newctx = new_regex_ctx(ctx->size); | 493 | newctx = new_regex_ctx(ctx->size); |
493 | newctx->s = GNUNET_strdup (regex); | 494 | newctx->s = GNUNET_strdup(regex); |
494 | ctx->children[idx] = newctx; | 495 | ctx->children[idx] = newctx; |
495 | } | 496 | } |
496 | 497 | ||
@@ -501,7 +502,7 @@ regex_add (struct RegexCombineCtx *ctx, const char *regex) | |||
501 | * @param ctx Context to free. | 502 | * @param ctx Context to free. |
502 | */ | 503 | */ |
503 | static void | 504 | static void |
504 | regex_ctx_destroy (struct RegexCombineCtx *ctx) | 505 | regex_ctx_destroy(struct RegexCombineCtx *ctx) |
505 | { | 506 | { |
506 | unsigned int i; | 507 | unsigned int i; |
507 | 508 | ||
@@ -509,12 +510,12 @@ regex_ctx_destroy (struct RegexCombineCtx *ctx) | |||
509 | return; | 510 | return; |
510 | 511 | ||
511 | for (i = 0; i < ctx->size; i++) | 512 | for (i = 0; i < ctx->size; i++) |
512 | { | 513 | { |
513 | regex_ctx_destroy (ctx->children[i]); | 514 | regex_ctx_destroy(ctx->children[i]); |
514 | } | 515 | } |
515 | GNUNET_free_non_null (ctx->s); /* 's' on root node is null */ | 516 | GNUNET_free_non_null(ctx->s); /* 's' on root node is null */ |
516 | GNUNET_free (ctx->children); | 517 | GNUNET_free(ctx->children); |
517 | GNUNET_free (ctx); | 518 | GNUNET_free(ctx); |
518 | } | 519 | } |
519 | 520 | ||
520 | 521 | ||
@@ -533,27 +534,27 @@ regex_ctx_destroy (struct RegexCombineCtx *ctx) | |||
533 | * @return A string with a single regex that matches any of the original regexes | 534 | * @return A string with a single regex that matches any of the original regexes |
534 | */ | 535 | */ |
535 | char * | 536 | char * |
536 | REGEX_TEST_combine (char * const regexes[], unsigned int alphabet_size) | 537 | REGEX_TEST_combine(char * const regexes[], unsigned int alphabet_size) |
537 | { | 538 | { |
538 | unsigned int i; | 539 | unsigned int i; |
539 | char *combined; | 540 | char *combined; |
540 | const char *current; | 541 | const char *current; |
541 | struct RegexCombineCtx *ctx; | 542 | struct RegexCombineCtx *ctx; |
542 | 543 | ||
543 | ctx = new_regex_ctx (alphabet_size); | 544 | ctx = new_regex_ctx(alphabet_size); |
544 | for (i = 0; regexes[i]; i++) | 545 | for (i = 0; regexes[i]; i++) |
545 | { | 546 | { |
546 | current = regexes[i]; | 547 | current = regexes[i]; |
547 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex %u: %s\n", i, current); | 548 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Regex %u: %s\n", i, current); |
548 | regex_add (ctx, current); | 549 | regex_add(ctx, current); |
549 | debugctx (ctx, 0); | 550 | debugctx(ctx, 0); |
550 | } | 551 | } |
551 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\nCombining...\n"); | 552 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "\nCombining...\n"); |
552 | debugctx (ctx, 0); | 553 | debugctx(ctx, 0); |
553 | 554 | ||
554 | combined = regex_combine (ctx); | 555 | combined = regex_combine(ctx); |
555 | 556 | ||
556 | regex_ctx_destroy (ctx); | 557 | regex_ctx_destroy(ctx); |
557 | 558 | ||
558 | return combined; | 559 | return combined; |
559 | } | 560 | } |
@@ -569,7 +570,7 @@ REGEX_TEST_combine (char * const regexes[], unsigned int alphabet_size) | |||
569 | * @return A newly allocated, NULL terminated array of regexes. | 570 | * @return A newly allocated, NULL terminated array of regexes. |
570 | */ | 571 | */ |
571 | char ** | 572 | char ** |
572 | REGEX_TEST_read_from_file (const char *filename) | 573 | REGEX_TEST_read_from_file(const char *filename) |
573 | { | 574 | { |
574 | struct GNUNET_DISK_FileHandle *f; | 575 | struct GNUNET_DISK_FileHandle *f; |
575 | unsigned int nr; | 576 | unsigned int nr; |
@@ -580,54 +581,55 @@ REGEX_TEST_read_from_file (const char *filename) | |||
580 | char *regex; | 581 | char *regex; |
581 | char **regexes; | 582 | char **regexes; |
582 | 583 | ||
583 | f = GNUNET_DISK_file_open (filename, | 584 | f = GNUNET_DISK_file_open(filename, |
584 | GNUNET_DISK_OPEN_READ, | 585 | GNUNET_DISK_OPEN_READ, |
585 | GNUNET_DISK_PERM_NONE); | 586 | GNUNET_DISK_PERM_NONE); |
586 | if (NULL == f) | 587 | if (NULL == f) |
587 | { | 588 | { |
588 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 589 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, |
589 | "Can't open file %s for reading\n", filename); | 590 | "Can't open file %s for reading\n", filename); |
590 | return NULL; | 591 | return NULL; |
591 | } | 592 | } |
592 | if (GNUNET_OK != GNUNET_DISK_file_handle_size (f, &size)) | 593 | if (GNUNET_OK != GNUNET_DISK_file_handle_size(f, &size)) |
593 | { | 594 | { |
594 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 595 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, |
595 | "Can't get size of file %s\n", filename); | 596 | "Can't get size of file %s\n", filename); |
596 | GNUNET_DISK_file_close (f); | 597 | GNUNET_DISK_file_close(f); |
597 | return NULL; | 598 | return NULL; |
598 | } | 599 | } |
599 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 600 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, |
600 | "using file %s, size %llu\n", | 601 | "using file %s, size %llu\n", |
601 | filename, (unsigned long long) size); | 602 | filename, (unsigned long long)size); |
602 | 603 | ||
603 | buffer = GNUNET_malloc (size + 1); | 604 | buffer = GNUNET_malloc(size + 1); |
604 | GNUNET_DISK_file_read (f, buffer, size); | 605 | GNUNET_DISK_file_read(f, buffer, size); |
605 | GNUNET_DISK_file_close (f); | 606 | GNUNET_DISK_file_close(f); |
606 | regexes = GNUNET_malloc (sizeof (char *)); | 607 | regexes = GNUNET_malloc(sizeof(char *)); |
607 | nr = 1; | 608 | nr = 1; |
608 | offset = 0; | 609 | offset = 0; |
609 | regex = NULL; | 610 | regex = NULL; |
610 | do | 611 | do |
611 | { | 612 | { |
612 | if (NULL == regex) | 613 | if (NULL == regex) |
613 | regex = GNUNET_malloc (size + 1); | 614 | regex = GNUNET_malloc(size + 1); |
614 | len = (size_t) sscanf (&buffer[offset], "%s", regex); | 615 | len = (size_t)sscanf(&buffer[offset], "%s", regex); |
615 | if (0 == len) | 616 | if (0 == len) |
616 | break; | 617 | break; |
617 | len = strlen (regex); | 618 | len = strlen(regex); |
618 | offset += len + 1; | 619 | offset += len + 1; |
619 | if (len < 1) | 620 | if (len < 1) |
620 | continue; | 621 | continue; |
621 | regex[len] = '\0'; | 622 | regex[len] = '\0'; |
622 | regex = GNUNET_realloc (regex, len + 1); | 623 | regex = GNUNET_realloc(regex, len + 1); |
623 | GNUNET_array_grow (regexes, nr, nr + 1); | 624 | GNUNET_array_grow(regexes, nr, nr + 1); |
624 | GNUNET_assert (NULL == regexes[nr - 2]); | 625 | GNUNET_assert(NULL == regexes[nr - 2]); |
625 | regexes[nr - 2] = regex; | 626 | regexes[nr - 2] = regex; |
626 | regexes[nr - 1] = NULL; | 627 | regexes[nr - 1] = NULL; |
627 | regex = NULL; | 628 | regex = NULL; |
628 | } while (offset < size); | 629 | } |
629 | GNUNET_free_non_null (regex); | 630 | while (offset < size); |
630 | GNUNET_free (buffer); | 631 | GNUNET_free_non_null(regex); |
632 | GNUNET_free(buffer); | ||
631 | 633 | ||
632 | return regexes; | 634 | return regexes; |
633 | } | 635 | } |
@@ -639,13 +641,13 @@ REGEX_TEST_read_from_file (const char *filename) | |||
639 | * @param regexes NULL-terminated array of regexes. | 641 | * @param regexes NULL-terminated array of regexes. |
640 | */ | 642 | */ |
641 | void | 643 | void |
642 | REGEX_TEST_free_from_file (char **regexes) | 644 | REGEX_TEST_free_from_file(char **regexes) |
643 | { | 645 | { |
644 | unsigned int i; | 646 | unsigned int i; |
645 | 647 | ||
646 | for (i = 0; regexes[i]; i++) | 648 | for (i = 0; regexes[i]; i++) |
647 | GNUNET_free (regexes[i]); | 649 | GNUNET_free(regexes[i]); |
648 | GNUNET_free (regexes); | 650 | GNUNET_free(regexes); |
649 | } | 651 | } |
650 | 652 | ||
651 | /* end of regex_test_lib.c */ | 653 | /* end of regex_test_lib.c */ |