paragraph for gnunet devs that don't know how to use the web
[oweals/gnunet.git] / src / regex / regex_internal.c
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2012 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14     
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/>.
17 */
18 /**
19  * @file src/regex/regex_internal.c
20  * @brief library to create Deterministic Finite Automatons (DFAs) from regular
21  * expressions (regexes).
22  * @author Maximilian Szengel
23  */
24 #include "platform.h"
25 #include "gnunet_util_lib.h"
26 #include "gnunet_regex_service.h"
27 #include "regex_internal_lib.h"
28 #include "regex_internal.h"
29
30
31 /**
32  * Set this to #GNUNET_YES to enable state naming. Used to debug NFA->DFA
33  * creation. Disabled by default for better performance.
34  */
35 #define REGEX_DEBUG_DFA GNUNET_NO
36
37 /**
38  * Set of states using MDLL API.
39  */
40 struct REGEX_INTERNAL_StateSet_MDLL
41 {
42   /**
43    * MDLL of states.
44    */
45   struct REGEX_INTERNAL_State *head;
46
47   /**
48    * MDLL of states.
49    */
50   struct REGEX_INTERNAL_State *tail;
51
52   /**
53    * Length of the MDLL.
54    */
55   unsigned int len;
56 };
57
58
59 /**
60  * Append state to the given StateSet.
61  *
62  * @param set set to be modified
63  * @param state state to be appended
64  */
65 static void
66 state_set_append (struct REGEX_INTERNAL_StateSet *set,
67                   struct REGEX_INTERNAL_State *state)
68 {
69   if (set->off == set->size)
70     GNUNET_array_grow (set->states, set->size, set->size * 2 + 4);
71   set->states[set->off++] = state;
72 }
73
74
75 /**
76  * Compare two strings for equality. If either is NULL they are not equal.
77  *
78  * @param str1 first string for comparison.
79  * @param str2 second string for comparison.
80  *
81  * @return 0 if the strings are the same or both NULL, 1 or -1 if not.
82  */
83 static int
84 nullstrcmp (const char *str1, const char *str2)
85 {
86   if ((NULL == str1) != (NULL == str2))
87     return -1;
88   if ((NULL == str1) && (NULL == str2))
89     return 0;
90
91   return strcmp (str1, str2);
92 }
93
94
95 /**
96  * Adds a transition from one state to another on @a label. Does not add
97  * duplicate states.
98  *
99  * @param ctx context
100  * @param from_state starting state for the transition
101  * @param label transition label
102  * @param to_state state to where the transition should point to
103  */
104 static void
105 state_add_transition (struct REGEX_INTERNAL_Context *ctx,
106                       struct REGEX_INTERNAL_State *from_state,
107                       const char *label,
108                       struct REGEX_INTERNAL_State *to_state)
109 {
110   struct REGEX_INTERNAL_Transition *t;
111   struct REGEX_INTERNAL_Transition *oth;
112
113   if (NULL == from_state)
114   {
115     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
116                 "Could not create Transition.\n");
117     return;
118   }
119
120   /* Do not add duplicate state transitions */
121   for (t = from_state->transitions_head; NULL != t; t = t->next)
122   {
123     if (t->to_state == to_state && 0 == nullstrcmp (t->label, label) &&
124         t->from_state == from_state)
125       return;
126   }
127
128   /* sort transitions by label */
129   for (oth = from_state->transitions_head; NULL != oth; oth = oth->next)
130   {
131     if (0 < nullstrcmp (oth->label, label))
132       break;
133   }
134
135   t = GNUNET_new (struct REGEX_INTERNAL_Transition);
136   if (NULL != ctx)
137     t->id = ctx->transition_id++;
138   if (NULL != label)
139     t->label = GNUNET_strdup (label);
140   else
141     t->label = NULL;
142   t->to_state = to_state;
143   t->from_state = from_state;
144
145   /* Add outgoing transition to 'from_state' */
146   from_state->transition_count++;
147   GNUNET_CONTAINER_DLL_insert_before (from_state->transitions_head,
148                                       from_state->transitions_tail, oth, t);
149 }
150
151
152 /**
153  * Remove a 'transition' from 'state'.
154  *
155  * @param state state from which the to-be-removed transition originates.
156  * @param transition transition that should be removed from state 'state'.
157  */
158 static void
159 state_remove_transition (struct REGEX_INTERNAL_State *state,
160                          struct REGEX_INTERNAL_Transition *transition)
161 {
162   if (NULL == state || NULL == transition)
163     return;
164
165   if (transition->from_state != state)
166     return;
167
168   GNUNET_free_non_null (transition->label);
169
170   state->transition_count--;
171   GNUNET_CONTAINER_DLL_remove (state->transitions_head, state->transitions_tail,
172                                transition);
173
174   GNUNET_free (transition);
175 }
176
177
178 /**
179  * Compare two states. Used for sorting.
180  *
181  * @param a first state
182  * @param b second state
183  *
184  * @return an integer less than, equal to, or greater than zero
185  *         if the first argument is considered to be respectively
186  *         less than, equal to, or greater than the second.
187  */
188 static int
189 state_compare (const void *a, const void *b)
190 {
191   struct REGEX_INTERNAL_State **s1 = (struct REGEX_INTERNAL_State **) a;
192   struct REGEX_INTERNAL_State **s2 = (struct REGEX_INTERNAL_State **) b;
193
194   return (*s1)->id - (*s2)->id;
195 }
196
197
198 /**
199  * Get all edges leaving state @a s.
200  *
201  * @param s state.
202  * @param edges all edges leaving @a s, expected to be allocated and have enough
203  *        space for `s->transitions_count` elements.
204  *
205  * @return number of edges.
206  */
207 static unsigned int
208 state_get_edges (struct REGEX_INTERNAL_State *s,
209                  struct REGEX_BLOCK_Edge *edges)
210 {
211   struct REGEX_INTERNAL_Transition *t;
212   unsigned int count;
213
214   if (NULL == s)
215     return 0;
216
217   count = 0;
218
219   for (t = s->transitions_head; NULL != t; t = t->next)
220   {
221     if (NULL != t->to_state)
222     {
223       edges[count].label = t->label;
224       edges[count].destination = t->to_state->hash;
225       count++;
226     }
227   }
228   return count;
229 }
230
231
232 /**
233  * Compare to state sets by comparing the id's of the states that are contained
234  * in each set. Both sets are expected to be sorted by id!
235  *
236  * @param sset1 first state set
237  * @param sset2 second state set
238  * @return 0 if the sets are equal, otherwise non-zero
239  */
240 static int
241 state_set_compare (struct REGEX_INTERNAL_StateSet *sset1,
242                    struct REGEX_INTERNAL_StateSet *sset2)
243 {
244   int result;
245   unsigned int i;
246
247   if (NULL == sset1 || NULL == sset2)
248     return 1;
249
250   result = sset1->off - sset2->off;
251   if (result < 0)
252     return -1;
253   if (result > 0)
254     return 1;
255   for (i = 0; i < sset1->off; i++)
256     if (0 != (result = state_compare (&sset1->states[i], &sset2->states[i])))
257       break;
258   return result;
259 }
260
261
262 /**
263  * Clears the given StateSet 'set'
264  *
265  * @param set set to be cleared
266  */
267 static void
268 state_set_clear (struct REGEX_INTERNAL_StateSet *set)
269 {
270   GNUNET_array_grow (set->states, set->size, 0);
271   set->off = 0;
272 }
273
274
275 /**
276  * Clears an automaton fragment. Does not destroy the states inside the
277  * automaton.
278  *
279  * @param a automaton to be cleared
280  */
281 static void
282 automaton_fragment_clear (struct REGEX_INTERNAL_Automaton *a)
283 {
284   if (NULL == a)
285     return;
286
287   a->start = NULL;
288   a->end = NULL;
289   a->states_head = NULL;
290   a->states_tail = NULL;
291   a->state_count = 0;
292   GNUNET_free (a);
293 }
294
295
296 /**
297  * Frees the memory used by State @a s
298  *
299  * @param s state that should be destroyed
300  */
301 static void
302 automaton_destroy_state (struct REGEX_INTERNAL_State *s)
303 {
304   struct REGEX_INTERNAL_Transition *t;
305   struct REGEX_INTERNAL_Transition *next_t;
306
307   if (NULL == s)
308     return;
309
310   GNUNET_free_non_null (s->name);
311   GNUNET_free_non_null (s->proof);
312   state_set_clear (&s->nfa_set);
313   for (t = s->transitions_head; NULL != t; t = next_t)
314   {
315     next_t = t->next;
316     state_remove_transition (s, t);
317   }
318
319   GNUNET_free (s);
320 }
321
322
323 /**
324  * Remove a state from the given automaton 'a'. Always use this function when
325  * altering the states of an automaton. Will also remove all transitions leading
326  * to this state, before destroying it.
327  *
328  * @param a automaton
329  * @param s state to remove
330  */
331 static void
332 automaton_remove_state (struct REGEX_INTERNAL_Automaton *a,
333                         struct REGEX_INTERNAL_State *s)
334 {
335   struct REGEX_INTERNAL_State *s_check;
336   struct REGEX_INTERNAL_Transition *t_check;
337   struct REGEX_INTERNAL_Transition *t_check_next;
338
339   if (NULL == a || NULL == s)
340     return;
341
342   /* remove all transitions leading to this state */
343   for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
344   {
345     for (t_check = s_check->transitions_head; NULL != t_check;
346          t_check = t_check_next)
347     {
348       t_check_next = t_check->next;
349       if (t_check->to_state == s)
350         state_remove_transition (s_check, t_check);
351     }
352   }
353
354   /* remove state */
355   GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s);
356   a->state_count--;
357
358   automaton_destroy_state (s);
359 }
360
361
362 /**
363  * Merge two states into one. Will merge 's1' and 's2' into 's1' and destroy
364  * 's2'. 's1' will contain all (non-duplicate) outgoing transitions of 's2'.
365  *
366  * @param ctx context
367  * @param a automaton
368  * @param s1 first state
369  * @param s2 second state, will be destroyed
370  */
371 static void
372 automaton_merge_states (struct REGEX_INTERNAL_Context *ctx,
373                         struct REGEX_INTERNAL_Automaton *a,
374                         struct REGEX_INTERNAL_State *s1,
375                         struct REGEX_INTERNAL_State *s2)
376 {
377   struct REGEX_INTERNAL_State *s_check;
378   struct REGEX_INTERNAL_Transition *t_check;
379   struct REGEX_INTERNAL_Transition *t;
380   struct REGEX_INTERNAL_Transition *t_next;
381   int is_dup;
382
383   if (s1 == s2)
384     return;
385
386   /* 1. Make all transitions pointing to s2 point to s1, unless this transition
387    * does not already exists, if it already exists remove transition. */
388   for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
389   {
390     for (t_check = s_check->transitions_head; NULL != t_check; t_check = t_next)
391     {
392       t_next = t_check->next;
393
394       if (s2 == t_check->to_state)
395       {
396         is_dup = GNUNET_NO;
397         for (t = t_check->from_state->transitions_head; NULL != t; t = t->next)
398         {
399           if (t->to_state == s1 && 0 == strcmp (t_check->label, t->label))
400             is_dup = GNUNET_YES;
401         }
402         if (GNUNET_NO == is_dup)
403           t_check->to_state = s1;
404         else
405           state_remove_transition (t_check->from_state, t_check);
406       }
407     }
408   }
409
410   /* 2. Add all transitions from s2 to sX to s1 */
411   for (t_check = s2->transitions_head; NULL != t_check; t_check = t_check->next)
412   {
413     if (t_check->to_state != s1)
414       state_add_transition (ctx, s1, t_check->label, t_check->to_state);
415   }
416
417   /* 3. Rename s1 to {s1,s2} */
418 #if REGEX_DEBUG_DFA
419   char *new_name;
420
421   new_name = s1->name;
422   GNUNET_asprintf (&s1->name, "{%s,%s}", new_name, s2->name);
423   GNUNET_free (new_name);
424 #endif
425
426   /* remove state */
427   GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s2);
428   a->state_count--;
429   automaton_destroy_state (s2);
430 }
431
432
433 /**
434  * Add a state to the automaton 'a', always use this function to alter the
435  * states DLL of the automaton.
436  *
437  * @param a automaton to add the state to
438  * @param s state that should be added
439  */
440 static void
441 automaton_add_state (struct REGEX_INTERNAL_Automaton *a,
442                      struct REGEX_INTERNAL_State *s)
443 {
444   GNUNET_CONTAINER_DLL_insert (a->states_head, a->states_tail, s);
445   a->state_count++;
446 }
447
448
449 /**
450  * Depth-first traversal (DFS) of all states that are reachable from state
451  * 's'. Performs 'action' on each visited state.
452  *
453  * @param s start state.
454  * @param marks an array of size a->state_count to remember which state was
455  *        already visited.
456  * @param count current count of the state.
457  * @param check function that is checked before advancing on each transition
458  *              in the DFS.
459  * @param check_cls closure for check.
460  * @param action action to be performed on each state.
461  * @param action_cls closure for action.
462  */
463 static void
464 automaton_state_traverse (struct REGEX_INTERNAL_State *s, int *marks,
465                           unsigned int *count,
466                           REGEX_INTERNAL_traverse_check check, void *check_cls,
467                           REGEX_INTERNAL_traverse_action action, void *action_cls)
468 {
469   struct REGEX_INTERNAL_Transition *t;
470
471   if (GNUNET_YES == marks[s->traversal_id])
472     return;
473
474   marks[s->traversal_id] = GNUNET_YES;
475
476   if (NULL != action)
477     action (action_cls, *count, s);
478
479   (*count)++;
480
481   for (t = s->transitions_head; NULL != t; t = t->next)
482   {
483     if (NULL == check ||
484         (NULL != check && GNUNET_YES == check (check_cls, s, t)))
485     {
486       automaton_state_traverse (t->to_state, marks, count, check, check_cls,
487                                 action, action_cls);
488     }
489   }
490 }
491
492
493 /**
494  * Traverses the given automaton using depth-first-search (DFS) from it's start
495  * state, visiting all reachable states and calling 'action' on each one of
496  * them.
497  *
498  * @param a automaton to be traversed.
499  * @param start start state, pass a->start or NULL to traverse the whole automaton.
500  * @param check function that is checked before advancing on each transition
501  *              in the DFS.
502  * @param check_cls closure for @a check.
503  * @param action action to be performed on each state.
504  * @param action_cls closure for @a action
505  */
506 void
507 REGEX_INTERNAL_automaton_traverse (const struct REGEX_INTERNAL_Automaton *a,
508                                    struct REGEX_INTERNAL_State *start,
509                                    REGEX_INTERNAL_traverse_check check,
510                                    void *check_cls,
511                                    REGEX_INTERNAL_traverse_action action,
512                                    void *action_cls)
513 {
514   unsigned int count;
515   struct REGEX_INTERNAL_State *s;
516
517   if (NULL == a || 0 == a->state_count)
518     return;
519
520   int marks[a->state_count];
521
522   for (count = 0, s = a->states_head; NULL != s && count < a->state_count;
523        s = s->next, count++)
524   {
525     s->traversal_id = count;
526     marks[s->traversal_id] = GNUNET_NO;
527   }
528
529   count = 0;
530
531   if (NULL == start)
532     s = a->start;
533   else
534     s = start;
535
536   automaton_state_traverse (s, marks, &count,
537                             check, check_cls,
538                             action, action_cls);
539 }
540
541
542 /**
543  * String container for faster string operations.
544  */
545 struct StringBuffer
546 {
547   /**
548    * Buffer holding the string (may start in the middle!);
549    * NOT 0-terminated!
550    */
551   char *sbuf;
552
553   /**
554    * Allocated buffer.
555    */
556   char *abuf;
557
558   /**
559    * Length of the string in the buffer.
560    */
561   size_t slen;
562
563   /**
564    * Number of bytes allocated for @e sbuf
565    */
566   unsigned int blen;
567
568   /**
569    * Buffer currently represents "NULL" (not the empty string!)
570    */
571   int16_t null_flag;
572
573   /**
574    * If this entry is part of the last/current generation array,
575    * this flag is #GNUNET_YES if the last and current generation are
576    * identical (and thus copying is unnecessary if the value didn't
577    * change).  This is used in an optimization that improves
578    * performance by about 1% --- if we use int16_t here.  With just
579    * "int" for both flags, performance drops (on my system) significantly,
580    * most likely due to increased cache misses.
581    */
582   int16_t synced;
583
584 };
585
586
587 /**
588  * Compare two strings for equality. If either is NULL they are not equal.
589  *
590  * @param s1 first string for comparison.
591  * @param s2 second string for comparison.
592  *
593  * @return 0 if the strings are the same or both NULL, 1 or -1 if not.
594  */
595 static int
596 sb_nullstrcmp (const struct StringBuffer *s1,
597                const struct StringBuffer *s2)
598 {
599   if ( (GNUNET_YES == s1->null_flag) &&
600        (GNUNET_YES == s2->null_flag) )
601     return 0;
602   if ( (GNUNET_YES == s1->null_flag) ||
603        (GNUNET_YES == s2->null_flag) )
604     return -1;
605   if (s1->slen != s2->slen)
606     return -1;
607   if (0 == s1->slen)
608     return 0;
609   return memcmp (s1->sbuf, s2->sbuf, s1->slen);
610 }
611
612
613 /**
614  * Compare two strings for equality.
615  *
616  * @param s1 first string for comparison.
617  * @param s2 second string for comparison.
618  *
619  * @return 0 if the strings are the same, 1 or -1 if not.
620  */
621 static int
622 sb_strcmp (const struct StringBuffer *s1,
623            const struct StringBuffer *s2)
624 {
625   if (s1->slen != s2->slen)
626     return -1;
627   if (0 == s1->slen)
628     return 0;
629   return memcmp (s1->sbuf, s2->sbuf, s1->slen);
630 }
631
632
633 /**
634  * Reallocate the buffer of 'ret' to fit 'nlen' characters;
635  * move the existing string to the beginning of the new buffer.
636  *
637  * @param ret current buffer, to be updated
638  * @param nlen target length for the buffer, must be at least ret->slen
639  */
640 static void
641 sb_realloc (struct StringBuffer *ret,
642             size_t nlen)
643 {
644   char *old;
645
646   GNUNET_assert (nlen >= ret->slen);
647   old = ret->abuf;
648   ret->abuf = GNUNET_malloc (nlen);
649   ret->blen = nlen;
650   GNUNET_memcpy (ret->abuf,
651           ret->sbuf,
652           ret->slen);
653   ret->sbuf = ret->abuf;
654   GNUNET_free_non_null (old);
655 }
656
657
658 /**
659  * Append a string.
660  *
661  * @param ret where to write the result
662  * @param sarg string to append
663  */
664 static void
665 sb_append (struct StringBuffer *ret,
666            const struct StringBuffer *sarg)
667 {
668   if (GNUNET_YES == ret->null_flag)
669     ret->slen = 0;
670   ret->null_flag = GNUNET_NO;
671   if (ret->blen < sarg->slen + ret->slen)
672     sb_realloc (ret, ret->blen + sarg->slen + 128);
673   GNUNET_memcpy (&ret->sbuf[ret->slen],
674           sarg->sbuf,
675           sarg->slen);
676   ret->slen += sarg->slen;
677 }
678
679
680 /**
681  * Append a C string.
682  *
683  * @param ret where to write the result
684  * @param cstr string to append
685  */
686 static void
687 sb_append_cstr (struct StringBuffer *ret,
688                 const char *cstr)
689 {
690   size_t cstr_len = strlen (cstr);
691
692   if (GNUNET_YES == ret->null_flag)
693     ret->slen = 0;
694   ret->null_flag = GNUNET_NO;
695   if (ret->blen < cstr_len + ret->slen)
696     sb_realloc (ret, ret->blen + cstr_len + 128);
697   GNUNET_memcpy (&ret->sbuf[ret->slen],
698           cstr,
699           cstr_len);
700   ret->slen += cstr_len;
701 }
702
703
704 /**
705  * Wrap a string buffer, that is, set ret to the format string
706  * which contains an "%s" which is to be replaced with the original
707  * content of 'ret'.  Note that optimizing this function is not
708  * really worth it, it is rarely called.
709  *
710  * @param ret where to write the result and take the input for %.*s from
711  * @param format format string, fprintf-style, with exactly one "%.*s"
712  * @param extra_chars how long will the result be, in addition to 'sarg' length
713  */
714 static void
715 sb_wrap (struct StringBuffer *ret,
716          const char *format,
717          size_t extra_chars)
718 {
719   char *temp;
720
721   if (GNUNET_YES == ret->null_flag)
722     ret->slen = 0;
723   ret->null_flag = GNUNET_NO;
724   temp = GNUNET_malloc (ret->slen + extra_chars + 1);
725   GNUNET_snprintf (temp,
726                    ret->slen + extra_chars + 1,
727                    format,
728                    (int) ret->slen,
729                    ret->sbuf);
730   GNUNET_free_non_null (ret->abuf);
731   ret->abuf = temp;
732   ret->sbuf = temp;
733   ret->blen = ret->slen + extra_chars + 1;
734   ret->slen = ret->slen + extra_chars;
735 }
736
737
738 /**
739  * Format a string buffer.    Note that optimizing this function is not
740  * really worth it, it is rarely called.
741  *
742  * @param ret where to write the result
743  * @param format format string, fprintf-style, with exactly one "%.*s"
744  * @param extra_chars how long will the result be, in addition to 'sarg' length
745  * @param sarg string to print into the format
746  */
747 static void
748 sb_printf1 (struct StringBuffer *ret,
749             const char *format,
750             size_t extra_chars,
751             const struct StringBuffer *sarg)
752 {
753   if (ret->blen < sarg->slen + extra_chars + 1)
754     sb_realloc (ret,
755                 sarg->slen + extra_chars + 1);
756   ret->null_flag = GNUNET_NO;
757   ret->sbuf = ret->abuf;
758   ret->slen = sarg->slen + extra_chars;
759   GNUNET_snprintf (ret->sbuf,
760                    ret->blen,
761                    format,
762                    (int) sarg->slen,
763                    sarg->sbuf);
764 }
765
766
767 /**
768  * Format a string buffer.
769  *
770  * @param ret where to write the result
771  * @param format format string, fprintf-style, with exactly two "%.*s"
772  * @param extra_chars how long will the result be, in addition to 'sarg1/2' length
773  * @param sarg1 first string to print into the format
774  * @param sarg2 second string to print into the format
775  */
776 static void
777 sb_printf2 (struct StringBuffer *ret,
778             const char *format,
779             size_t extra_chars,
780             const struct StringBuffer *sarg1,
781             const struct StringBuffer *sarg2)
782 {
783   if (ret->blen < sarg1->slen + sarg2->slen + extra_chars + 1)
784     sb_realloc (ret,
785                 sarg1->slen + sarg2->slen + extra_chars + 1);
786   ret->null_flag = GNUNET_NO;
787   ret->slen = sarg1->slen + sarg2->slen + extra_chars;
788   ret->sbuf = ret->abuf;
789   GNUNET_snprintf (ret->sbuf,
790                    ret->blen,
791                    format,
792                    (int) sarg1->slen,
793                    sarg1->sbuf,
794                    (int) sarg2->slen,
795                    sarg2->sbuf);
796 }
797
798
799 /**
800  * Format a string buffer.     Note that optimizing this function is not
801  * really worth it, it is rarely called.
802  *
803  * @param ret where to write the result
804  * @param format format string, fprintf-style, with exactly three "%.*s"
805  * @param extra_chars how long will the result be, in addition to 'sarg1/2/3' length
806  * @param sarg1 first string to print into the format
807  * @param sarg2 second string to print into the format
808  * @param sarg3 third string to print into the format
809  */
810 static void
811 sb_printf3 (struct StringBuffer *ret,
812             const char *format,
813             size_t extra_chars,
814             const struct StringBuffer *sarg1,
815             const struct StringBuffer *sarg2,
816             const struct StringBuffer *sarg3)
817 {
818   if (ret->blen < sarg1->slen + sarg2->slen + sarg3->slen + extra_chars + 1)
819     sb_realloc (ret,
820                 sarg1->slen + sarg2->slen + sarg3->slen + extra_chars + 1);
821   ret->null_flag = GNUNET_NO;
822   ret->slen = sarg1->slen + sarg2->slen + sarg3->slen + extra_chars;
823   ret->sbuf = ret->abuf;
824   GNUNET_snprintf (ret->sbuf,
825                    ret->blen,
826                    format,
827                    (int) sarg1->slen,
828                    sarg1->sbuf,
829                    (int) sarg2->slen,
830                    sarg2->sbuf,
831                    (int) sarg3->slen,
832                    sarg3->sbuf);
833 }
834
835
836 /**
837  * Free resources of the given string buffer.
838  *
839  * @param sb buffer to free (actual pointer is not freed, as they
840  *        should not be individually allocated)
841  */
842 static void
843 sb_free (struct StringBuffer *sb)
844 {
845   GNUNET_array_grow (sb->abuf,
846                      sb->blen,
847                      0);
848   sb->slen = 0;
849   sb->sbuf = NULL;
850   sb->null_flag= GNUNET_YES;
851 }
852
853
854 /**
855  * Copy the given string buffer from 'in' to 'out'.
856  *
857  * @param in input string
858  * @param out output string
859  */
860 static void
861 sb_strdup (struct StringBuffer *out,
862            const struct StringBuffer *in)
863
864 {
865   out->null_flag = in->null_flag;
866   if (GNUNET_YES == out->null_flag)
867     return;
868   if (out->blen < in->slen)
869   {
870     GNUNET_array_grow (out->abuf,
871                        out->blen,
872                        in->slen);
873   }
874   out->sbuf = out->abuf;
875   out->slen = in->slen;
876   GNUNET_memcpy (out->sbuf, in->sbuf, out->slen);
877 }
878
879
880 /**
881  * Copy the given string buffer from 'in' to 'out'.
882  *
883  * @param cstr input string
884  * @param out output string
885  */
886 static void
887 sb_strdup_cstr (struct StringBuffer *out,
888                 const char *cstr)
889 {
890   if (NULL == cstr)
891   {
892     out->null_flag = GNUNET_YES;
893     return;
894   }
895   out->null_flag = GNUNET_NO;
896   out->slen = strlen (cstr);
897   if (out->blen < out->slen)
898   {
899     GNUNET_array_grow (out->abuf,
900                        out->blen,
901                        out->slen);
902   }
903   out->sbuf = out->abuf;
904   GNUNET_memcpy (out->sbuf, cstr, out->slen);
905 }
906
907
908 /**
909  * Check if the given string @a str needs parentheses around it when
910  * using it to generate a regex.
911  *
912  * @param str string
913  *
914  * @return #GNUNET_YES if parentheses are needed, #GNUNET_NO otherwise
915  */
916 static int
917 needs_parentheses (const struct StringBuffer *str)
918 {
919   size_t slen;
920   const char *op;
921   const char *cl;
922   const char *pos;
923   const char *end;
924   unsigned int cnt;
925
926   if ((GNUNET_YES == str->null_flag) || ((slen = str->slen) < 2))
927     return GNUNET_NO;
928   pos = str->sbuf;
929   if ('(' != pos[0])
930     return GNUNET_YES;
931   end = str->sbuf + slen;
932   cnt = 1;
933   pos++;
934   while (cnt > 0)
935   {
936     cl = memchr (pos, ')', end - pos);
937     if (NULL == cl)
938     {
939       GNUNET_break (0);
940       return GNUNET_YES;
941     }
942     /* while '(' before ')', count opening parens */
943     while ( (NULL != (op = memchr (pos, '(', end - pos)))  &&
944             (op < cl) )
945     {
946       cnt++;
947       pos = op + 1;
948     }
949     /* got ')' first */
950     cnt--;
951     pos = cl + 1;
952   }
953   return (*pos == '\0') ? GNUNET_NO : GNUNET_YES;
954 }
955
956
957 /**
958  * Remove parentheses surrounding string @a str.
959  * Example: "(a)" becomes "a", "(a|b)|(a|c)" stays the same.
960  * You need to #GNUNET_free() the returned string.
961  *
962  * @param str string, modified to contain a
963  * @return string without surrounding parentheses, string 'str' if no preceding
964  *         epsilon could be found, NULL if 'str' was NULL
965  */
966 static void
967 remove_parentheses (struct StringBuffer *str)
968 {
969   size_t slen;
970   const char *pos;
971   const char *end;
972   const char *sbuf;
973   const char *op;
974   const char *cp;
975   unsigned int cnt;
976
977   if (0)
978     return;
979   sbuf = str->sbuf;
980   if ( (GNUNET_YES == str->null_flag) ||
981        (1 >=  (slen = str->slen)) ||
982        ('(' != str->sbuf[0]) ||
983        (')' != str->sbuf[slen - 1]) )
984     return;
985   cnt = 0;
986   pos = &sbuf[1];
987   end = &sbuf[slen - 1];
988   op = memchr (pos, '(', end - pos);
989   cp = memchr (pos, ')', end - pos);
990   while (NULL != cp)
991   {
992     while ( (NULL != op) &&
993             (op < cp) )
994     {
995       cnt++;
996       pos = op + 1;
997       op = memchr (pos, '(', end - pos);
998     }
999     while ( (NULL != cp) &&
1000             ( (NULL == op) ||
1001               (cp < op) ) )
1002     {
1003       if (0 == cnt)
1004         return; /* can't strip parens */
1005       cnt--;
1006       pos = cp + 1;
1007       cp = memchr (pos, ')', end - pos);
1008     }
1009   }
1010   if (0 != cnt)
1011   {
1012     GNUNET_break (0);
1013     return;
1014   }
1015   str->sbuf++;
1016   str->slen -= 2;
1017 }
1018
1019
1020 /**
1021  * Check if the string 'str' starts with an epsilon (empty string).
1022  * Example: "(|a)" is starting with an epsilon.
1023  *
1024  * @param str string to test
1025  *
1026  * @return 0 if str has no epsilon, 1 if str starts with '(|' and ends with ')'
1027  */
1028 static int
1029 has_epsilon (const struct StringBuffer *str)
1030 {
1031   return
1032     (GNUNET_YES != str->null_flag) &&
1033     (0 < str->slen) &&
1034     ('(' == str->sbuf[0]) &&
1035     ('|' == str->sbuf[1]) &&
1036     (')' == str->sbuf[str->slen - 1]);
1037 }
1038
1039
1040 /**
1041  * Remove an epsilon from the string str. Where epsilon is an empty string
1042  * Example: str = "(|a|b|c)", result: "a|b|c"
1043  * The returned string needs to be freed.
1044  *
1045  * @param str original string
1046  * @param ret where to return string without preceding epsilon, string 'str' if no preceding
1047  *         epsilon could be found, NULL if 'str' was NULL
1048  */
1049 static void
1050 remove_epsilon (const struct StringBuffer *str,
1051                 struct StringBuffer *ret)
1052 {
1053   if (GNUNET_YES == str->null_flag)
1054   {
1055     ret->null_flag = GNUNET_YES;
1056     return;
1057   }
1058   if ( (str->slen > 1) &&
1059        ('(' == str->sbuf[0]) &&
1060        ('|' == str->sbuf[1]) &&
1061        (')' == str->sbuf[str->slen - 1]) )
1062   {
1063     /* remove epsilon */
1064     if (ret->blen < str->slen - 3)
1065     {
1066       GNUNET_array_grow (ret->abuf,
1067                          ret->blen,
1068                          str->slen - 3);
1069     }
1070     ret->sbuf = ret->abuf;
1071     ret->slen = str->slen - 3;
1072     GNUNET_memcpy (ret->sbuf, &str->sbuf[2], ret->slen);
1073     return;
1074   }
1075   sb_strdup (ret, str);
1076 }
1077
1078
1079 /**
1080  * Compare n bytes of 'str1' and 'str2'
1081  *
1082  * @param str1 first string to compare
1083  * @param str2 second string for comparison
1084  * @param n number of bytes to compare
1085  *
1086  * @return -1 if any of the strings is NULL, 0 if equal, non 0 otherwise
1087  */
1088 static int
1089 sb_strncmp (const struct StringBuffer *str1,
1090             const struct StringBuffer *str2, size_t n)
1091 {
1092   size_t max;
1093
1094   if ( (str1->slen != str2->slen) &&
1095        ( (str1->slen < n) ||
1096          (str2->slen < n) ) )
1097     return -1;
1098   max = GNUNET_MAX (str1->slen, str2->slen);
1099   if (max > n)
1100     max = n;
1101   return memcmp (str1->sbuf, str2->sbuf, max);
1102 }
1103
1104
1105 /**
1106  * Compare n bytes of 'str1' and 'str2'
1107  *
1108  * @param str1 first string to compare
1109  * @param str2 second C string for comparison
1110  * @param n number of bytes to compare (and length of str2)
1111  *
1112  * @return -1 if any of the strings is NULL, 0 if equal, non 0 otherwise
1113  */
1114 static int
1115 sb_strncmp_cstr (const struct StringBuffer *str1,
1116                  const char *str2, size_t n)
1117 {
1118   if (str1->slen < n)
1119     return -1;
1120   return memcmp (str1->sbuf, str2, n);
1121 }
1122
1123
1124 /**
1125  * Initialize string buffer for storing strings of up to n
1126  * characters.
1127  *
1128  * @param sb buffer to initialize
1129  * @param n desired target length
1130  */
1131 static void
1132 sb_init (struct StringBuffer *sb,
1133          size_t n)
1134 {
1135   sb->null_flag = GNUNET_NO;
1136   sb->abuf = sb->sbuf = (0 == n) ? NULL : GNUNET_malloc (n);
1137   sb->blen = n;
1138   sb->slen = 0;
1139 }
1140
1141
1142 /**
1143  * Compare 'str1', starting from position 'k',  with whole 'str2'
1144  *
1145  * @param str1 first string to compare, starting from position 'k'
1146  * @param str2 second string for comparison
1147  * @param k starting position in 'str1'
1148  *
1149  * @return -1 if any of the strings is NULL, 0 if equal, non 0 otherwise
1150  */
1151 static int
1152 sb_strkcmp (const struct StringBuffer *str1,
1153             const struct StringBuffer *str2, size_t k)
1154 {
1155   if ( (GNUNET_YES == str1->null_flag) ||
1156        (GNUNET_YES == str2->null_flag) ||
1157        (k > str1->slen) ||
1158        (str1->slen - k != str2->slen) )
1159     return -1;
1160   return memcmp (&str1->sbuf[k], str2->sbuf, str2->slen);
1161 }
1162
1163
1164 /**
1165  * Helper function used as 'action' in 'REGEX_INTERNAL_automaton_traverse'
1166  * function to create the depth-first numbering of the states.
1167  *
1168  * @param cls states array.
1169  * @param count current state counter.
1170  * @param s current state.
1171  */
1172 static void
1173 number_states (void *cls, const unsigned int count,
1174                struct REGEX_INTERNAL_State *s)
1175 {
1176   struct REGEX_INTERNAL_State **states = cls;
1177
1178   s->dfs_id = count;
1179   if (NULL != states)
1180     states[count] = s;
1181 }
1182
1183
1184
1185 #define PRIS(a) \
1186   ((GNUNET_YES == a.null_flag) ? 6 : (int) a.slen), \
1187   ((GNUNET_YES == a.null_flag) ? "(null)" : a.sbuf)
1188
1189
1190 /**
1191  * Construct the regular expression given the inductive step,
1192  * $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^*
1193  * R^{(k-1)}_{kj}, and simplify the resulting expression saved in R_cur_ij.
1194  *
1195  * @param R_last_ij value of  $R^{(k-1)_{ij}.
1196  * @param R_last_ik value of  $R^{(k-1)_{ik}.
1197  * @param R_last_kk value of  $R^{(k-1)_{kk}.
1198  * @param R_last_kj value of  $R^{(k-1)_{kj}.
1199  * @param R_cur_ij result for this inductive step is saved in R_cur_ij, R_cur_ij
1200  *                 is expected to be NULL when called!
1201  * @param R_cur_l optimization -- kept between iterations to avoid realloc
1202  * @param R_cur_r optimization -- kept between iterations to avoid realloc
1203  */
1204 static void
1205 automaton_create_proofs_simplify (const struct StringBuffer *R_last_ij,
1206                                   const struct StringBuffer *R_last_ik,
1207                                   const struct StringBuffer *R_last_kk,
1208                                   const struct StringBuffer *R_last_kj,
1209                                   struct StringBuffer *R_cur_ij,
1210                                   struct StringBuffer *R_cur_l,
1211                                   struct StringBuffer *R_cur_r)
1212 {
1213   struct StringBuffer R_temp_ij;
1214   struct StringBuffer R_temp_ik;
1215   struct StringBuffer R_temp_kj;
1216   struct StringBuffer R_temp_kk;
1217   int eps_check;
1218   int ij_ik_cmp;
1219   int ij_kj_cmp;
1220   int ik_kk_cmp;
1221   int kk_kj_cmp;
1222   int clean_ik_kk_cmp;
1223   int clean_kk_kj_cmp;
1224   size_t length;
1225   size_t length_l;
1226   size_t length_r;
1227
1228   /*
1229    * $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
1230    * R_last == R^{(k-1)}, R_cur == R^{(k)}
1231    * R_cur_ij = R_cur_l | R_cur_r
1232    * R_cur_l == R^{(k-1)}_{ij}
1233    * R_cur_r == R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
1234    */
1235
1236   if ( (GNUNET_YES == R_last_ij->null_flag) &&
1237        ( (GNUNET_YES == R_last_ik->null_flag) ||
1238          (GNUNET_YES == R_last_kj->null_flag)))
1239   {
1240     /* R^{(k)}_{ij} = N | N */
1241     R_cur_ij->null_flag = GNUNET_YES;
1242     R_cur_ij->synced = GNUNET_NO;
1243     return;
1244   }
1245
1246   if ( (GNUNET_YES == R_last_ik->null_flag) ||
1247        (GNUNET_YES == R_last_kj->null_flag) )
1248   {
1249     /*  R^{(k)}_{ij} = R^{(k-1)}_{ij} | N */
1250     if (GNUNET_YES == R_last_ij->synced)
1251     {
1252       R_cur_ij->synced = GNUNET_YES;
1253       R_cur_ij->null_flag = GNUNET_NO;
1254       return;
1255     }
1256     R_cur_ij->synced = GNUNET_YES;
1257     sb_strdup (R_cur_ij, R_last_ij);
1258     return;
1259   }
1260   R_cur_ij->synced = GNUNET_NO;
1261
1262   /* $R^{(k)}_{ij} = N | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj} OR
1263    * $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj} */
1264
1265   R_cur_r->null_flag = GNUNET_YES;
1266   R_cur_r->slen = 0;
1267   R_cur_l->null_flag = GNUNET_YES;
1268   R_cur_l->slen = 0;
1269
1270   /* cache results from strcmp, we might need these many times */
1271   ij_kj_cmp = sb_nullstrcmp (R_last_ij, R_last_kj);
1272   ij_ik_cmp = sb_nullstrcmp (R_last_ij, R_last_ik);
1273   ik_kk_cmp = sb_nullstrcmp (R_last_ik, R_last_kk);
1274   kk_kj_cmp = sb_nullstrcmp (R_last_kk, R_last_kj);
1275
1276   /* Assign R_temp_(ik|kk|kj) to R_last[][] and remove epsilon as well
1277    * as parentheses, so we can better compare the contents */
1278
1279   memset (&R_temp_ij, 0, sizeof (struct StringBuffer));
1280   memset (&R_temp_ik, 0, sizeof (struct StringBuffer));
1281   memset (&R_temp_kk, 0, sizeof (struct StringBuffer));
1282   memset (&R_temp_kj, 0, sizeof (struct StringBuffer));
1283   remove_epsilon (R_last_ik, &R_temp_ik);
1284   remove_epsilon (R_last_kk, &R_temp_kk);
1285   remove_epsilon (R_last_kj, &R_temp_kj);
1286   remove_parentheses (&R_temp_ik);
1287   remove_parentheses (&R_temp_kk);
1288   remove_parentheses (&R_temp_kj);
1289   clean_ik_kk_cmp = sb_nullstrcmp (R_last_ik, &R_temp_kk);
1290   clean_kk_kj_cmp = sb_nullstrcmp (&R_temp_kk, R_last_kj);
1291
1292   /* construct R_cur_l (and, if necessary R_cur_r) */
1293   if (GNUNET_YES != R_last_ij->null_flag)
1294   {
1295     /* Assign R_temp_ij to R_last_ij and remove epsilon as well
1296      * as parentheses, so we can better compare the contents */
1297     remove_epsilon (R_last_ij, &R_temp_ij);
1298     remove_parentheses (&R_temp_ij);
1299
1300     if ( (0 == sb_strcmp (&R_temp_ij, &R_temp_ik)) &&
1301          (0 == sb_strcmp (&R_temp_ik, &R_temp_kk)) &&
1302          (0 == sb_strcmp (&R_temp_kk, &R_temp_kj)) )
1303     {
1304       if (0 == R_temp_ij.slen)
1305       {
1306         R_cur_r->null_flag = GNUNET_NO;
1307       }
1308       else if ((0 == sb_strncmp_cstr (R_last_ij, "(|", 2)) ||
1309                (0 == sb_strncmp_cstr (R_last_ik, "(|", 2) &&
1310                 0 == sb_strncmp_cstr (R_last_kj, "(|", 2)))
1311       {
1312         /*
1313          * a|(e|a)a*(e|a) = a*
1314          * a|(e|a)(e|a)*(e|a) = a*
1315          * (e|a)|aa*a = a*
1316          * (e|a)|aa*(e|a) = a*
1317          * (e|a)|(e|a)a*a = a*
1318          * (e|a)|(e|a)a*(e|a) = a*
1319          * (e|a)|(e|a)(e|a)*(e|a) = a*
1320          */
1321         if (GNUNET_YES == needs_parentheses (&R_temp_ij))
1322           sb_printf1 (R_cur_r, "(%.*s)*", 3, &R_temp_ij);
1323         else
1324           sb_printf1 (R_cur_r, "%.*s*", 1, &R_temp_ij);
1325       }
1326       else
1327       {
1328         /*
1329          * a|aa*a = a+
1330          * a|(e|a)a*a = a+
1331          * a|aa*(e|a) = a+
1332          * a|(e|a)(e|a)*a = a+
1333          * a|a(e|a)*(e|a) = a+
1334          */
1335         if (GNUNET_YES == needs_parentheses (&R_temp_ij))
1336           sb_printf1 (R_cur_r, "(%.*s)+", 3, &R_temp_ij);
1337         else
1338           sb_printf1 (R_cur_r, "%.*s+", 1, &R_temp_ij);
1339       }
1340     }
1341     else if ( (0 == ij_ik_cmp) && (0 == clean_kk_kj_cmp) && (0 != clean_ik_kk_cmp) )
1342     {
1343       /* a|ab*b = ab* */
1344       if (0 == R_last_kk->slen)
1345         sb_strdup (R_cur_r, R_last_ij);
1346       else if (GNUNET_YES == needs_parentheses (&R_temp_kk))
1347         sb_printf2 (R_cur_r, "%.*s(%.*s)*", 3, R_last_ij, &R_temp_kk);
1348       else
1349         sb_printf2 (R_cur_r, "%.*s%.*s*", 1, R_last_ij, R_last_kk);
1350       R_cur_l->null_flag = GNUNET_YES;
1351     }
1352     else if ( (0 == ij_kj_cmp) && (0 == clean_ik_kk_cmp) && (0 != clean_kk_kj_cmp))
1353     {
1354       /* a|bb*a = b*a */
1355       if (R_last_kk->slen < 1)
1356       {
1357         sb_strdup (R_cur_r, R_last_kj);
1358       }
1359       else if (GNUNET_YES == needs_parentheses (&R_temp_kk))
1360         sb_printf2 (R_cur_r, "(%.*s)*%.*s", 3, &R_temp_kk, R_last_kj);
1361       else
1362         sb_printf2 (R_cur_r, "%.*s*%.*s", 1, &R_temp_kk, R_last_kj);
1363
1364       R_cur_l->null_flag = GNUNET_YES;
1365     }
1366     else if ( (0 == ij_ik_cmp) && (0 == kk_kj_cmp) && (! has_epsilon (R_last_ij)) &&
1367               has_epsilon (R_last_kk))
1368     {
1369       /* a|a(e|b)*(e|b) = a|ab* = a|a|ab|abb|abbb|... = ab* */
1370       if (needs_parentheses (&R_temp_kk))
1371         sb_printf2 (R_cur_r, "%.*s(%.*s)*", 3, R_last_ij, &R_temp_kk);
1372       else
1373         sb_printf2 (R_cur_r, "%.*s%.*s*", 1, R_last_ij, &R_temp_kk);
1374       R_cur_l->null_flag = GNUNET_YES;
1375     }
1376     else if ( (0 == ij_kj_cmp) && (0 == ik_kk_cmp) && (! has_epsilon (R_last_ij)) &&
1377              has_epsilon (R_last_kk))
1378     {
1379       /* a|(e|b)(e|b)*a = a|b*a = a|a|ba|bba|bbba|...  = b*a */
1380       if (needs_parentheses (&R_temp_kk))
1381         sb_printf2 (R_cur_r, "(%.*s)*%.*s", 3, &R_temp_kk, R_last_ij);
1382       else
1383         sb_printf2 (R_cur_r, "%.*s*%.*s", 1, &R_temp_kk, R_last_ij);
1384       R_cur_l->null_flag = GNUNET_YES;
1385     }
1386     else
1387     {
1388       sb_strdup (R_cur_l, R_last_ij);
1389       remove_parentheses (R_cur_l);
1390     }
1391   }
1392   else
1393   {
1394     /* we have no left side */
1395     R_cur_l->null_flag = GNUNET_YES;
1396   }
1397
1398   /* construct R_cur_r, if not already constructed */
1399   if (GNUNET_YES == R_cur_r->null_flag)
1400   {
1401     length = R_temp_kk.slen - R_last_ik->slen;
1402
1403     /* a(ba)*bx = (ab)+x */
1404     if ( (length > 0) &&
1405          (GNUNET_YES != R_last_kk->null_flag) &&
1406          (0 < R_last_kk->slen) &&
1407          (GNUNET_YES != R_last_kj->null_flag) &&
1408          (0 < R_last_kj->slen) &&
1409          (GNUNET_YES != R_last_ik->null_flag) &&
1410          (0 < R_last_ik->slen) &&
1411          (0 == sb_strkcmp (&R_temp_kk, R_last_ik, length)) &&
1412          (0 == sb_strncmp (&R_temp_kk, R_last_kj, length)) )
1413     {
1414       struct StringBuffer temp_a;
1415       struct StringBuffer temp_b;
1416
1417       sb_init (&temp_a, length);
1418       sb_init (&temp_b, R_last_kj->slen - length);
1419
1420       length_l = length;
1421       temp_a.sbuf = temp_a.abuf;
1422       GNUNET_memcpy (temp_a.sbuf, R_last_kj->sbuf, length_l);
1423       temp_a.slen = length_l;
1424
1425       length_r = R_last_kj->slen - length;
1426       temp_b.sbuf = temp_b.abuf;
1427       GNUNET_memcpy (temp_b.sbuf, &R_last_kj->sbuf[length], length_r);
1428       temp_b.slen = length_r;
1429
1430       /* e|(ab)+ = (ab)* */
1431       if ( (GNUNET_YES != R_cur_l->null_flag) &&
1432            (0 == R_cur_l->slen) &&
1433            (0 == temp_b.slen) )
1434       {
1435         sb_printf2 (R_cur_r, "(%.*s%.*s)*", 3, R_last_ik, &temp_a);
1436         sb_free (R_cur_l);
1437         R_cur_l->null_flag = GNUNET_YES;
1438       }
1439       else
1440       {
1441         sb_printf3 (R_cur_r, "(%.*s%.*s)+%.*s", 3, R_last_ik, &temp_a, &temp_b);
1442       }
1443       sb_free (&temp_a);
1444       sb_free (&temp_b);
1445     }
1446     else if (0 == sb_strcmp (&R_temp_ik, &R_temp_kk) &&
1447              0 == sb_strcmp (&R_temp_kk, &R_temp_kj))
1448     {
1449       /*
1450        * (e|a)a*(e|a) = a*
1451        * (e|a)(e|a)*(e|a) = a*
1452        */
1453       if (has_epsilon (R_last_ik) && has_epsilon (R_last_kj))
1454       {
1455         if (needs_parentheses (&R_temp_kk))
1456           sb_printf1 (R_cur_r, "(%.*s)*", 3, &R_temp_kk);
1457         else
1458           sb_printf1 (R_cur_r, "%.*s*", 1, &R_temp_kk);
1459       }
1460       /* aa*a = a+a */
1461       else if ( (0 == clean_ik_kk_cmp) &&
1462                 (0 == clean_kk_kj_cmp) &&
1463                 (! has_epsilon (R_last_ik)) )
1464       {
1465         if (needs_parentheses (&R_temp_kk))
1466           sb_printf2 (R_cur_r, "(%.*s)+%.*s", 3, &R_temp_kk, &R_temp_kk);
1467         else
1468           sb_printf2 (R_cur_r, "%.*s+%.*s", 1, &R_temp_kk, &R_temp_kk);
1469       }
1470       /*
1471        * (e|a)a*a = a+
1472        * aa*(e|a) = a+
1473        * a(e|a)*(e|a) = a+
1474        * (e|a)a*a = a+
1475        */
1476       else
1477       {
1478         eps_check =
1479           (has_epsilon (R_last_ik) + has_epsilon (R_last_kk) +
1480            has_epsilon (R_last_kj));
1481
1482         if (1 == eps_check)
1483         {
1484           if (needs_parentheses (&R_temp_kk))
1485             sb_printf1 (R_cur_r, "(%.*s)+", 3, &R_temp_kk);
1486           else
1487             sb_printf1 (R_cur_r, "%.*s+", 1, &R_temp_kk);
1488         }
1489       }
1490     }
1491     /*
1492      * aa*b = a+b
1493      * (e|a)(e|a)*b = a*b
1494      */
1495     else if (0 == sb_strcmp (&R_temp_ik, &R_temp_kk))
1496     {
1497       if (has_epsilon (R_last_ik))
1498       {
1499         if (needs_parentheses (&R_temp_kk))
1500           sb_printf2 (R_cur_r, "(%.*s)*%.*s", 3, &R_temp_kk, R_last_kj);
1501         else
1502           sb_printf2 (R_cur_r, "%.*s*%.*s", 1, &R_temp_kk, R_last_kj);
1503       }
1504       else
1505       {
1506         if (needs_parentheses (&R_temp_kk))
1507           sb_printf2 (R_cur_r, "(%.*s)+%.*s", 3, &R_temp_kk, R_last_kj);
1508         else
1509           sb_printf2 (R_cur_r, "%.*s+%.*s", 1, &R_temp_kk, R_last_kj);
1510       }
1511     }
1512     /*
1513      * ba*a = ba+
1514      * b(e|a)*(e|a) = ba*
1515      */
1516     else if (0 == sb_strcmp (&R_temp_kk, &R_temp_kj))
1517     {
1518       if (has_epsilon (R_last_kj))
1519       {
1520         if (needs_parentheses (&R_temp_kk))
1521           sb_printf2 (R_cur_r, "%.*s(%.*s)*", 3, R_last_ik, &R_temp_kk);
1522         else
1523           sb_printf2 (R_cur_r, "%.*s%.*s*", 1, R_last_ik, &R_temp_kk);
1524       }
1525       else
1526       {
1527         if (needs_parentheses (&R_temp_kk))
1528           sb_printf2 (R_cur_r, "(%.*s)+%.*s", 3, R_last_ik, &R_temp_kk);
1529         else
1530           sb_printf2 (R_cur_r, "%.*s+%.*s", 1, R_last_ik, &R_temp_kk);
1531       }
1532     }
1533     else
1534     {
1535       if (0 < R_temp_kk.slen)
1536       {
1537         if (needs_parentheses (&R_temp_kk))
1538         {
1539           sb_printf3 (R_cur_r, "%.*s(%.*s)*%.*s", 3, R_last_ik, &R_temp_kk,
1540                       R_last_kj);
1541         }
1542         else
1543         {
1544           sb_printf3 (R_cur_r, "%.*s%.*s*%.*s", 1, R_last_ik, &R_temp_kk,
1545                       R_last_kj);
1546         }
1547       }
1548       else
1549       {
1550         sb_printf2 (R_cur_r, "%.*s%.*s", 0, R_last_ik, R_last_kj);
1551       }
1552     }
1553   }
1554   sb_free (&R_temp_ij);
1555   sb_free (&R_temp_ik);
1556   sb_free (&R_temp_kk);
1557   sb_free (&R_temp_kj);
1558
1559   if ( (GNUNET_YES == R_cur_l->null_flag) &&
1560        (GNUNET_YES == R_cur_r->null_flag) )
1561   {
1562     R_cur_ij->null_flag = GNUNET_YES;
1563     return;
1564   }
1565
1566   if ( (GNUNET_YES != R_cur_l->null_flag) &&
1567        (GNUNET_YES == R_cur_r->null_flag) )
1568   {
1569     struct StringBuffer tmp;
1570
1571     tmp = *R_cur_ij;
1572     *R_cur_ij = *R_cur_l;
1573     *R_cur_l = tmp;
1574     return;
1575   }
1576
1577   if ( (GNUNET_YES == R_cur_l->null_flag) &&
1578        (GNUNET_YES != R_cur_r->null_flag) )
1579   {
1580     struct StringBuffer tmp;
1581
1582     tmp = *R_cur_ij;
1583     *R_cur_ij = *R_cur_r;
1584     *R_cur_r = tmp;
1585     return;
1586   }
1587
1588   if (0 == sb_nullstrcmp (R_cur_l, R_cur_r))
1589   {
1590     struct StringBuffer tmp;
1591
1592     tmp = *R_cur_ij;
1593     *R_cur_ij = *R_cur_l;
1594     *R_cur_l = tmp;
1595     return;
1596   }
1597   sb_printf2 (R_cur_ij, "(%.*s|%.*s)", 3, R_cur_l, R_cur_r);
1598 }
1599
1600
1601 /**
1602  * Create proofs for all states in the given automaton. Implementation of the
1603  * algorithm descriped in chapter 3.2.1 of "Automata Theory, Languages, and
1604  * Computation 3rd Edition" by Hopcroft, Motwani and Ullman.
1605  *
1606  * Each state in the automaton gets assigned 'proof' and 'hash' (hash of the
1607  * proof) fields. The starting state will only have a valid proof/hash if it has
1608  * any incoming transitions.
1609  *
1610  * @param a automaton for which to assign proofs and hashes, must not be NULL
1611  */
1612 static int
1613 automaton_create_proofs (struct REGEX_INTERNAL_Automaton *a)
1614 {
1615   unsigned int n = a->state_count;
1616   struct REGEX_INTERNAL_State *states[n];
1617   struct StringBuffer *R_last;
1618   struct StringBuffer *R_cur;
1619   struct StringBuffer R_cur_r;
1620   struct StringBuffer R_cur_l;
1621   struct StringBuffer *R_swap;
1622   struct REGEX_INTERNAL_Transition *t;
1623   struct StringBuffer complete_regex;
1624   unsigned int i;
1625   unsigned int j;
1626   unsigned int k;
1627
1628   R_last = GNUNET_malloc_large (sizeof (struct StringBuffer) * n * n);
1629   R_cur = GNUNET_malloc_large (sizeof (struct StringBuffer) * n * n);
1630   if ( (NULL == R_last) ||
1631        (NULL == R_cur) )
1632   {
1633     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc");
1634     GNUNET_free_non_null (R_cur);
1635     GNUNET_free_non_null (R_last);
1636     return GNUNET_SYSERR;
1637   }
1638
1639   /* create depth-first numbering of the states, initializes 'state' */
1640   REGEX_INTERNAL_automaton_traverse (a, a->start, NULL, NULL, &number_states,
1641                                    states);
1642
1643   for (i = 0; i < n; i++)
1644     GNUNET_assert (NULL != states[i]);
1645   for (i = 0; i < n; i++)
1646     for (j = 0; j < n; j++)
1647       R_last[i *n + j].null_flag = GNUNET_YES;
1648
1649   /* Compute regular expressions of length "1" between each pair of states */
1650   for (i = 0; i < n; i++)
1651   {
1652     for (t = states[i]->transitions_head; NULL != t; t = t->next)
1653     {
1654       j = t->to_state->dfs_id;
1655       if (GNUNET_YES == R_last[i * n + j].null_flag)
1656       {
1657         sb_strdup_cstr (&R_last[i * n + j], t->label);
1658       }
1659       else
1660       {
1661         sb_append_cstr (&R_last[i * n + j], "|");
1662         sb_append_cstr (&R_last[i * n + j], t->label);
1663       }
1664     }
1665     /* add self-loop: i is reachable from i via epsilon-transition */
1666     if (GNUNET_YES == R_last[i * n + i].null_flag)
1667     {
1668       R_last[i * n + i].slen = 0;
1669       R_last[i * n + i].null_flag = GNUNET_NO;
1670     }
1671     else
1672     {
1673       sb_wrap (&R_last[i * n + i], "(|%.*s)", 3);
1674     }
1675   }
1676   for (i = 0; i < n; i++)
1677     for (j = 0; j < n; j++)
1678       if (needs_parentheses (&R_last[i * n + j]))
1679         sb_wrap (&R_last[i * n + j], "(%.*s)", 2);
1680   /* Compute regular expressions of length "k" between each pair of states per
1681    * induction */
1682   memset (&R_cur_l, 0, sizeof (struct StringBuffer));
1683   memset (&R_cur_r, 0, sizeof (struct StringBuffer));
1684   for (k = 0; k < n; k++)
1685   {
1686     for (i = 0; i < n; i++)
1687     {
1688       for (j = 0; j < n; j++)
1689       {
1690         /* Basis for the recursion:
1691          * $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
1692          * R_last == R^{(k-1)}, R_cur == R^{(k)}
1693          */
1694
1695         /* Create R_cur[i][j] and simplify the expression */
1696         automaton_create_proofs_simplify (&R_last[i * n + j], &R_last[i * n + k],
1697                                           &R_last[k * n + k], &R_last[k * n + j],
1698                                           &R_cur[i * n + j],
1699                                           &R_cur_l, &R_cur_r);
1700       }
1701     }
1702     /* set R_last = R_cur */
1703     R_swap = R_last;
1704     R_last = R_cur;
1705     R_cur = R_swap;
1706     /* clear 'R_cur' for next iteration */
1707     for (i = 0; i < n; i++)
1708       for (j = 0; j < n; j++)
1709         R_cur[i * n + j].null_flag = GNUNET_YES;
1710   }
1711   sb_free (&R_cur_l);
1712   sb_free (&R_cur_r);
1713   /* assign proofs and hashes */
1714   for (i = 0; i < n; i++)
1715   {
1716     if (GNUNET_YES != R_last[a->start->dfs_id * n + i].null_flag)
1717     {
1718       states[i]->proof = GNUNET_strndup (R_last[a->start->dfs_id * n + i].sbuf,
1719                                          R_last[a->start->dfs_id * n + i].slen);
1720       GNUNET_CRYPTO_hash (states[i]->proof, strlen (states[i]->proof),
1721                           &states[i]->hash);
1722     }
1723   }
1724
1725   /* complete regex for whole DFA: union of all pairs (start state/accepting
1726    * state(s)). */
1727   sb_init (&complete_regex, 16 * n);
1728   for (i = 0; i < n; i++)
1729   {
1730     if (states[i]->accepting)
1731     {
1732       if ( (0 == complete_regex.slen) &&
1733            (0 < R_last[a->start->dfs_id * n + i].slen) )
1734       {
1735         sb_append (&complete_regex,
1736                    &R_last[a->start->dfs_id * n + i]);
1737       }
1738       else if ( (GNUNET_YES != R_last[a->start->dfs_id * n + i].null_flag) &&
1739                 (0 < R_last[a->start->dfs_id * n + i].slen) )
1740       {
1741         sb_append_cstr (&complete_regex, "|");
1742         sb_append (&complete_regex,
1743                    &R_last[a->start->dfs_id * n + i]);
1744       }
1745     }
1746   }
1747   a->canonical_regex = GNUNET_strndup (complete_regex.sbuf, complete_regex.slen);
1748
1749   /* cleanup */
1750   sb_free (&complete_regex);
1751   for (i = 0; i < n; i++)
1752     for (j = 0; j < n; j++)
1753     {
1754       sb_free (&R_cur[i * n + j]);
1755       sb_free (&R_last[i * n + j]);
1756     }
1757   GNUNET_free (R_cur);
1758   GNUNET_free (R_last);
1759   return GNUNET_OK;
1760 }
1761
1762
1763 /**
1764  * Creates a new DFA state based on a set of NFA states. Needs to be freed using
1765  * automaton_destroy_state.
1766  *
1767  * @param ctx context
1768  * @param nfa_states set of NFA states on which the DFA should be based on
1769  *
1770  * @return new DFA state
1771  */
1772 static struct REGEX_INTERNAL_State *
1773 dfa_state_create (struct REGEX_INTERNAL_Context *ctx,
1774                   struct REGEX_INTERNAL_StateSet *nfa_states)
1775 {
1776   struct REGEX_INTERNAL_State *s;
1777   char *pos;
1778   size_t len;
1779   struct REGEX_INTERNAL_State *cstate;
1780   struct REGEX_INTERNAL_Transition *ctran;
1781   unsigned int i;
1782
1783   s = GNUNET_new (struct REGEX_INTERNAL_State);
1784   s->id = ctx->state_id++;
1785   s->index = -1;
1786   s->lowlink = -1;
1787
1788   if (NULL == nfa_states)
1789   {
1790     GNUNET_asprintf (&s->name, "s%i", s->id);
1791     return s;
1792   }
1793
1794   s->nfa_set = *nfa_states;
1795
1796   if (nfa_states->off < 1)
1797     return s;
1798
1799   /* Create a name based on 'nfa_states' */
1800   len = nfa_states->off * 14 + 4;
1801   s->name = GNUNET_malloc (len);
1802   strcat (s->name, "{");
1803   pos = s->name + 1;
1804
1805   for (i = 0; i < nfa_states->off; i++)
1806   {
1807     cstate = nfa_states->states[i];
1808     GNUNET_snprintf (pos,
1809                      pos - s->name + len,
1810                      "%i,",
1811                      cstate->id);
1812     pos += strlen (pos);
1813
1814     /* Add a transition for each distinct label to NULL state */
1815     for (ctran = cstate->transitions_head; NULL != ctran; ctran = ctran->next)
1816       if (NULL != ctran->label)
1817         state_add_transition (ctx, s, ctran->label, NULL);
1818
1819     /* If the nfa_states contain an accepting state, the new dfa state is also
1820      * accepting. */
1821     if (cstate->accepting)
1822       s->accepting = 1;
1823   }
1824   pos[-1] = '}';
1825   s->name = GNUNET_realloc (s->name, strlen (s->name) + 1);
1826
1827   memset (nfa_states, 0, sizeof (struct REGEX_INTERNAL_StateSet));
1828   return s;
1829 }
1830
1831
1832 /**
1833  * Move from the given state 's' to the next state on transition 'str'. Consumes
1834  * as much of the given 'str' as possible (usefull for strided DFAs). On return
1835  * 's' will point to the next state, and the length of the substring used for
1836  * this transition will be returned. If no transition possible 0 is returned and
1837  * 's' points to NULL.
1838  *
1839  * @param s starting state, will point to the next state or NULL (if no
1840  * transition possible)
1841  * @param str edge label to follow (will match longest common prefix)
1842  *
1843  * @return length of the substring comsumed from 'str'
1844  */
1845 static unsigned int
1846 dfa_move (struct REGEX_INTERNAL_State **s, const char *str)
1847 {
1848   struct REGEX_INTERNAL_Transition *t;
1849   struct REGEX_INTERNAL_State *new_s;
1850   unsigned int len;
1851   unsigned int max_len;
1852
1853   if (NULL == s)
1854     return 0;
1855
1856   new_s = NULL;
1857   max_len = 0;
1858   for (t = (*s)->transitions_head; NULL != t; t = t->next)
1859   {
1860     len = strlen (t->label);
1861
1862     if (0 == strncmp (t->label, str, len))
1863     {
1864       if (len >= max_len)
1865       {
1866         max_len = len;
1867         new_s = t->to_state;
1868       }
1869     }
1870   }
1871
1872   *s = new_s;
1873   return max_len;
1874 }
1875
1876
1877 /**
1878  * Set the given state 'marked' to #GNUNET_YES. Used by the
1879  * #dfa_remove_unreachable_states() function to detect unreachable states in the
1880  * automaton.
1881  *
1882  * @param cls closure, not used.
1883  * @param count count, not used.
1884  * @param s state where the marked attribute will be set to #GNUNET_YES.
1885  */
1886 static void
1887 mark_states (void *cls,
1888              const unsigned int count,
1889              struct REGEX_INTERNAL_State *s)
1890 {
1891   s->marked = GNUNET_YES;
1892 }
1893
1894
1895 /**
1896  * Remove all unreachable states from DFA 'a'. Unreachable states are those
1897  * states that are not reachable from the starting state.
1898  *
1899  * @param a DFA automaton
1900  */
1901 static void
1902 dfa_remove_unreachable_states (struct REGEX_INTERNAL_Automaton *a)
1903 {
1904   struct REGEX_INTERNAL_State *s;
1905   struct REGEX_INTERNAL_State *s_next;
1906
1907   /* 1. unmark all states */
1908   for (s = a->states_head; NULL != s; s = s->next)
1909     s->marked = GNUNET_NO;
1910
1911   /* 2. traverse dfa from start state and mark all visited states */
1912   REGEX_INTERNAL_automaton_traverse (a, a->start, NULL, NULL, &mark_states, NULL);
1913
1914   /* 3. delete all states that were not visited */
1915   for (s = a->states_head; NULL != s; s = s_next)
1916   {
1917     s_next = s->next;
1918     if (GNUNET_NO == s->marked)
1919       automaton_remove_state (a, s);
1920   }
1921 }
1922
1923
1924 /**
1925  * Remove all dead states from the DFA 'a'. Dead states are those states that do
1926  * not transition to any other state but themselves.
1927  *
1928  * @param a DFA automaton
1929  */
1930 static void
1931 dfa_remove_dead_states (struct REGEX_INTERNAL_Automaton *a)
1932 {
1933   struct REGEX_INTERNAL_State *s;
1934   struct REGEX_INTERNAL_State *s_next;
1935   struct REGEX_INTERNAL_Transition *t;
1936   int dead;
1937
1938   GNUNET_assert (DFA == a->type);
1939
1940   for (s = a->states_head; NULL != s; s = s_next)
1941   {
1942     s_next = s->next;
1943
1944     if (s->accepting)
1945       continue;
1946
1947     dead = 1;
1948     for (t = s->transitions_head; NULL != t; t = t->next)
1949     {
1950       if (NULL != t->to_state && t->to_state != s)
1951       {
1952         dead = 0;
1953         break;
1954       }
1955     }
1956
1957     if (0 == dead)
1958       continue;
1959
1960     /* state s is dead, remove it */
1961     automaton_remove_state (a, s);
1962   }
1963 }
1964
1965
1966 /**
1967  * Merge all non distinguishable states in the DFA 'a'
1968  *
1969  * @param ctx context
1970  * @param a DFA automaton
1971  * @return #GNUNET_OK on success
1972  */
1973 static int
1974 dfa_merge_nondistinguishable_states (struct REGEX_INTERNAL_Context *ctx,
1975                                      struct REGEX_INTERNAL_Automaton *a)
1976 {
1977   uint32_t *table;
1978   struct REGEX_INTERNAL_State *s1;
1979   struct REGEX_INTERNAL_State *s2;
1980   struct REGEX_INTERNAL_Transition *t1;
1981   struct REGEX_INTERNAL_Transition *t2;
1982   struct REGEX_INTERNAL_State *s1_next;
1983   struct REGEX_INTERNAL_State *s2_next;
1984   int change;
1985   unsigned int num_equal_edges;
1986   unsigned int i;
1987   unsigned int state_cnt;
1988   unsigned long long idx;
1989   unsigned long long idx1;
1990
1991   if ( (NULL == a) || (0 == a->state_count) )
1992   {
1993     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1994                 "Could not merge nondistinguishable states, automaton was NULL.\n");
1995     return GNUNET_SYSERR;
1996   }
1997
1998   state_cnt = a->state_count;
1999   table = GNUNET_malloc_large ((sizeof (uint32_t) * state_cnt * state_cnt / 32)  + sizeof (uint32_t));
2000   if (NULL == table)
2001   {
2002     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc");
2003     return GNUNET_SYSERR;
2004   }
2005
2006   for (i = 0, s1 = a->states_head; NULL != s1; s1 = s1->next)
2007     s1->marked = i++;
2008
2009   /* Mark all pairs of accepting/!accepting states */
2010   for (s1 = a->states_head; NULL != s1; s1 = s1->next)
2011     for (s2 = a->states_head; NULL != s2; s2 = s2->next)
2012       if ( (s1->accepting && !s2->accepting) ||
2013            (!s1->accepting && s2->accepting) )
2014       {
2015         idx = (unsigned long long) s1->marked * state_cnt + s2->marked;
2016         table[idx / 32] |= (1U << (idx % 32));
2017       }
2018
2019   /* Find all equal states */
2020   change = 1;
2021   while (0 != change)
2022   {
2023     change = 0;
2024     for (s1 = a->states_head; NULL != s1; s1 = s1->next)
2025     {
2026       for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next)
2027       {
2028         idx = (unsigned long long) s1->marked * state_cnt + s2->marked;
2029         if (0 != (table[idx / 32] & (1U << (idx % 32))))
2030           continue;
2031         num_equal_edges = 0;
2032         for (t1 = s1->transitions_head; NULL != t1; t1 = t1->next)
2033         {
2034           for (t2 = s2->transitions_head; NULL != t2; t2 = t2->next)
2035           {
2036             if (0 == strcmp (t1->label, t2->label))
2037             {
2038               num_equal_edges++;
2039               /* same edge, but targets definitively different, so we're different
2040                  as well */
2041               if (t1->to_state->marked > t2->to_state->marked)
2042                 idx1 = (unsigned long long) t1->to_state->marked * state_cnt + t2->to_state->marked;
2043               else
2044                 idx1 = (unsigned long long) t2->to_state->marked * state_cnt + t1->to_state->marked;
2045               if (0 != (table[idx1 / 32] & (1U << (idx1 % 32))))
2046               {
2047                 table[idx / 32] |= (1U << (idx % 32));
2048                 change = 1; /* changed a marker, need to run again */
2049               }
2050             }
2051           }
2052         }
2053         if ( (num_equal_edges != s1->transition_count) ||
2054              (num_equal_edges != s2->transition_count) )
2055         {
2056           /* Make sure ALL edges of possible equal states are the same */
2057           table[idx / 32] |= (1U << (idx % 32));
2058           change = 1;  /* changed a marker, need to run again */
2059         }
2060       }
2061     }
2062   }
2063
2064   /* Merge states that are equal */
2065   for (s1 = a->states_head; NULL != s1; s1 = s1_next)
2066   {
2067     s1_next = s1->next;
2068     for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2_next)
2069     {
2070       s2_next = s2->next;
2071       idx = (unsigned long long) s1->marked * state_cnt + s2->marked;
2072       if (0 == (table[idx / 32] & (1U << (idx % 32))))
2073         automaton_merge_states (ctx, a, s1, s2);
2074     }
2075   }
2076
2077   GNUNET_free (table);
2078   return GNUNET_OK;
2079 }
2080
2081
2082 /**
2083  * Minimize the given DFA 'a' by removing all unreachable states, removing all
2084  * dead states and merging all non distinguishable states
2085  *
2086  * @param ctx context
2087  * @param a DFA automaton
2088  * @return GNUNET_OK on success
2089  */
2090 static int
2091 dfa_minimize (struct REGEX_INTERNAL_Context *ctx,
2092               struct REGEX_INTERNAL_Automaton *a)
2093 {
2094   if (NULL == a)
2095     return GNUNET_SYSERR;
2096
2097   GNUNET_assert (DFA == a->type);
2098
2099   /* 1. remove unreachable states */
2100   dfa_remove_unreachable_states (a);
2101
2102   /* 2. remove dead states */
2103   dfa_remove_dead_states (a);
2104
2105   /* 3. Merge nondistinguishable states */
2106   if (GNUNET_OK != dfa_merge_nondistinguishable_states (ctx, a))
2107     return GNUNET_SYSERR;
2108   return GNUNET_OK;
2109 }
2110
2111
2112 /**
2113  * Context for adding strided transitions to a DFA.
2114  */
2115 struct REGEX_INTERNAL_Strided_Context
2116 {
2117   /**
2118    * Length of the strides.
2119    */
2120   const unsigned int stride;
2121
2122   /**
2123    * Strided transitions DLL. New strided transitions will be stored in this DLL
2124    * and afterwards added to the DFA.
2125    */
2126   struct REGEX_INTERNAL_Transition *transitions_head;
2127
2128   /**
2129    * Strided transitions DLL.
2130    */
2131   struct REGEX_INTERNAL_Transition *transitions_tail;
2132 };
2133
2134
2135 /**
2136  * Recursive helper function to add strides to a DFA.
2137  *
2138  * @param cls context, contains stride length and strided transitions DLL.
2139  * @param depth current depth of the depth-first traversal of the graph.
2140  * @param label current label, string that contains all labels on the path from
2141  *        'start' to 's'.
2142  * @param start start state for the depth-first traversal of the graph.
2143  * @param s current state in the depth-first traversal
2144  */
2145 static void
2146 dfa_add_multi_strides_helper (void *cls, const unsigned int depth, char *label,
2147                               struct REGEX_INTERNAL_State *start,
2148                               struct REGEX_INTERNAL_State *s)
2149 {
2150   struct REGEX_INTERNAL_Strided_Context *ctx = cls;
2151   struct REGEX_INTERNAL_Transition *t;
2152   char *new_label;
2153
2154   if (depth == ctx->stride)
2155   {
2156     t = GNUNET_new (struct REGEX_INTERNAL_Transition);
2157     t->label = GNUNET_strdup (label);
2158     t->to_state = s;
2159     t->from_state = start;
2160     GNUNET_CONTAINER_DLL_insert (ctx->transitions_head, ctx->transitions_tail,
2161                                  t);
2162   }
2163   else
2164   {
2165     for (t = s->transitions_head; NULL != t; t = t->next)
2166     {
2167       /* Do not consider self-loops, because it end's up in too many
2168        * transitions */
2169       if (t->to_state == t->from_state)
2170         continue;
2171
2172       if (NULL != label)
2173       {
2174         GNUNET_asprintf (&new_label, "%s%s", label, t->label);
2175       }
2176       else
2177         new_label = GNUNET_strdup (t->label);
2178
2179       dfa_add_multi_strides_helper (cls, (depth + 1), new_label, start,
2180                                     t->to_state);
2181     }
2182   }
2183   GNUNET_free_non_null (label);
2184 }
2185
2186
2187 /**
2188  * Function called for each state in the DFA. Starts a traversal of depth set in
2189  * context starting from state 's'.
2190  *
2191  * @param cls context.
2192  * @param count not used.
2193  * @param s current state.
2194  */
2195 static void
2196 dfa_add_multi_strides (void *cls, const unsigned int count,
2197                        struct REGEX_INTERNAL_State *s)
2198 {
2199   dfa_add_multi_strides_helper (cls, 0, NULL, s, s);
2200 }
2201
2202
2203 /**
2204  * Adds multi-strided transitions to the given 'dfa'.
2205  *
2206  * @param regex_ctx regex context needed to add transitions to the automaton.
2207  * @param dfa DFA to which the multi strided transitions should be added.
2208  * @param stride_len length of the strides.
2209  */
2210 void
2211 REGEX_INTERNAL_dfa_add_multi_strides (struct REGEX_INTERNAL_Context *regex_ctx,
2212                                     struct REGEX_INTERNAL_Automaton *dfa,
2213                                     const unsigned int stride_len)
2214 {
2215   struct REGEX_INTERNAL_Strided_Context ctx = { stride_len, NULL, NULL };
2216   struct REGEX_INTERNAL_Transition *t;
2217   struct REGEX_INTERNAL_Transition *t_next;
2218
2219   if (1 > stride_len || GNUNET_YES == dfa->is_multistrided)
2220     return;
2221
2222   /* Compute the new transitions of given stride_len */
2223   REGEX_INTERNAL_automaton_traverse (dfa, dfa->start, NULL, NULL,
2224                                    &dfa_add_multi_strides, &ctx);
2225
2226   /* Add all the new transitions to the automaton. */
2227   for (t = ctx.transitions_head; NULL != t; t = t_next)
2228   {
2229     t_next = t->next;
2230     state_add_transition (regex_ctx, t->from_state, t->label, t->to_state);
2231     GNUNET_CONTAINER_DLL_remove (ctx.transitions_head, ctx.transitions_tail, t);
2232     GNUNET_free_non_null (t->label);
2233     GNUNET_free (t);
2234   }
2235
2236   /* Mark this automaton as multistrided */
2237   dfa->is_multistrided = GNUNET_YES;
2238 }
2239
2240 /**
2241  * Recursive Helper function for DFA path compression. Does DFS on the DFA graph
2242  * and adds new transitions to the given transitions DLL and marks states that
2243  * should be removed by setting state->contained to GNUNET_YES.
2244  *
2245  * @param dfa DFA for which the paths should be compressed.
2246  * @param start starting state for linear path search.
2247  * @param cur current state in the recursive DFS.
2248  * @param label current label (string of traversed labels).
2249  * @param max_len maximal path compression length.
2250  * @param transitions_head transitions DLL.
2251  * @param transitions_tail transitions DLL.
2252  */
2253 void
2254 dfa_compress_paths_helper (struct REGEX_INTERNAL_Automaton *dfa,
2255                            struct REGEX_INTERNAL_State *start,
2256                            struct REGEX_INTERNAL_State *cur, char *label,
2257                            unsigned int max_len,
2258                            struct REGEX_INTERNAL_Transition **transitions_head,
2259                            struct REGEX_INTERNAL_Transition **transitions_tail)
2260 {
2261   struct REGEX_INTERNAL_Transition *t;
2262   char *new_label;
2263
2264
2265   if (NULL != label &&
2266       ((cur->incoming_transition_count > 1 || GNUNET_YES == cur->accepting ||
2267         GNUNET_YES == cur->marked) || (start != dfa->start && max_len > 0 &&
2268                                        max_len == strlen (label)) ||
2269        (start == dfa->start && GNUNET_REGEX_INITIAL_BYTES == strlen (label))))
2270   {
2271     t = GNUNET_new (struct REGEX_INTERNAL_Transition);
2272     t->label = GNUNET_strdup (label);
2273     t->to_state = cur;
2274     t->from_state = start;
2275     GNUNET_CONTAINER_DLL_insert (*transitions_head, *transitions_tail, t);
2276
2277     if (GNUNET_NO == cur->marked)
2278     {
2279       dfa_compress_paths_helper (dfa, cur, cur, NULL, max_len, transitions_head,
2280                                  transitions_tail);
2281     }
2282     return;
2283   }
2284   else if (cur != start)
2285     cur->contained = GNUNET_YES;
2286
2287   if (GNUNET_YES == cur->marked && cur != start)
2288     return;
2289
2290   cur->marked = GNUNET_YES;
2291
2292
2293   for (t = cur->transitions_head; NULL != t; t = t->next)
2294   {
2295     if (NULL != label)
2296       GNUNET_asprintf (&new_label, "%s%s", label, t->label);
2297     else
2298       new_label = GNUNET_strdup (t->label);
2299
2300     if (t->to_state != cur)
2301     {
2302       dfa_compress_paths_helper (dfa, start, t->to_state, new_label, max_len,
2303                                  transitions_head, transitions_tail);
2304     }
2305     GNUNET_free (new_label);
2306   }
2307 }
2308
2309
2310 /**
2311  * Compress paths in the given 'dfa'. Linear paths like 0->1->2->3 will be
2312  * compressed to 0->3 by combining transitions.
2313  *
2314  * @param regex_ctx context for adding new transitions.
2315  * @param dfa DFA representation, will directly modify the given DFA.
2316  * @param max_len maximal length of the compressed paths.
2317  */
2318 static void
2319 dfa_compress_paths (struct REGEX_INTERNAL_Context *regex_ctx,
2320                     struct REGEX_INTERNAL_Automaton *dfa, unsigned int max_len)
2321 {
2322   struct REGEX_INTERNAL_State *s;
2323   struct REGEX_INTERNAL_State *s_next;
2324   struct REGEX_INTERNAL_Transition *t;
2325   struct REGEX_INTERNAL_Transition *t_next;
2326   struct REGEX_INTERNAL_Transition *transitions_head = NULL;
2327   struct REGEX_INTERNAL_Transition *transitions_tail = NULL;
2328
2329   if (NULL == dfa)
2330     return;
2331
2332   /* Count the incoming transitions on each state. */
2333   for (s = dfa->states_head; NULL != s; s = s->next)
2334   {
2335     for (t = s->transitions_head; NULL != t; t = t->next)
2336     {
2337       if (NULL != t->to_state)
2338         t->to_state->incoming_transition_count++;
2339     }
2340   }
2341
2342   /* Unmark all states. */
2343   for (s = dfa->states_head; NULL != s; s = s->next)
2344   {
2345     s->marked = GNUNET_NO;
2346     s->contained = GNUNET_NO;
2347   }
2348
2349   /* Add strides and mark states that can be deleted. */
2350   dfa_compress_paths_helper (dfa, dfa->start, dfa->start, NULL, max_len,
2351                              &transitions_head, &transitions_tail);
2352
2353   /* Add all the new transitions to the automaton. */
2354   for (t = transitions_head; NULL != t; t = t_next)
2355   {
2356     t_next = t->next;
2357     state_add_transition (regex_ctx, t->from_state, t->label, t->to_state);
2358     GNUNET_CONTAINER_DLL_remove (transitions_head, transitions_tail, t);
2359     GNUNET_free_non_null (t->label);
2360     GNUNET_free (t);
2361   }
2362
2363   /* Remove marked states (including their incoming and outgoing transitions). */
2364   for (s = dfa->states_head; NULL != s; s = s_next)
2365   {
2366     s_next = s->next;
2367     if (GNUNET_YES == s->contained)
2368       automaton_remove_state (dfa, s);
2369   }
2370 }
2371
2372
2373 /**
2374  * Creates a new NFA fragment. Needs to be cleared using
2375  * automaton_fragment_clear.
2376  *
2377  * @param start starting state
2378  * @param end end state
2379  *
2380  * @return new NFA fragment
2381  */
2382 static struct REGEX_INTERNAL_Automaton *
2383 nfa_fragment_create (struct REGEX_INTERNAL_State *start,
2384                      struct REGEX_INTERNAL_State *end)
2385 {
2386   struct REGEX_INTERNAL_Automaton *n;
2387
2388   n = GNUNET_new (struct REGEX_INTERNAL_Automaton);
2389
2390   n->type = NFA;
2391   n->start = NULL;
2392   n->end = NULL;
2393   n->state_count = 0;
2394
2395   if (NULL == start || NULL == end)
2396     return n;
2397
2398   automaton_add_state (n, end);
2399   automaton_add_state (n, start);
2400
2401   n->state_count = 2;
2402
2403   n->start = start;
2404   n->end = end;
2405
2406   return n;
2407 }
2408
2409
2410 /**
2411  * Adds a list of states to the given automaton 'n'.
2412  *
2413  * @param n automaton to which the states should be added
2414  * @param states_head head of the DLL of states
2415  * @param states_tail tail of the DLL of states
2416  */
2417 static void
2418 nfa_add_states (struct REGEX_INTERNAL_Automaton *n,
2419                 struct REGEX_INTERNAL_State *states_head,
2420                 struct REGEX_INTERNAL_State *states_tail)
2421 {
2422   struct REGEX_INTERNAL_State *s;
2423
2424   if (NULL == n || NULL == states_head)
2425   {
2426     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not add states\n");
2427     return;
2428   }
2429
2430   if (NULL == n->states_head)
2431   {
2432     n->states_head = states_head;
2433     n->states_tail = states_tail;
2434     return;
2435   }
2436
2437   if (NULL != states_head)
2438   {
2439     n->states_tail->next = states_head;
2440     n->states_tail = states_tail;
2441   }
2442
2443   for (s = states_head; NULL != s; s = s->next)
2444     n->state_count++;
2445 }
2446
2447
2448 /**
2449  * Creates a new NFA state. Needs to be freed using automaton_destroy_state.
2450  *
2451  * @param ctx context
2452  * @param accepting is it an accepting state or not
2453  *
2454  * @return new NFA state
2455  */
2456 static struct REGEX_INTERNAL_State *
2457 nfa_state_create (struct REGEX_INTERNAL_Context *ctx, int accepting)
2458 {
2459   struct REGEX_INTERNAL_State *s;
2460
2461   s = GNUNET_new (struct REGEX_INTERNAL_State);
2462   s->id = ctx->state_id++;
2463   s->accepting = accepting;
2464   s->marked = GNUNET_NO;
2465   s->contained = 0;
2466   s->index = -1;
2467   s->lowlink = -1;
2468   s->scc_id = 0;
2469   s->name = NULL;
2470   GNUNET_asprintf (&s->name, "s%i", s->id);
2471
2472   return s;
2473 }
2474
2475
2476 /**
2477  * Calculates the closure set for the given set of states.
2478  *
2479  * @param ret set to sorted nfa closure on 'label' (epsilon closure if 'label' is NULL)
2480  * @param nfa the NFA containing 's'
2481  * @param states list of states on which to base the closure on
2482  * @param label transitioning label for which to base the closure on,
2483  *                pass NULL for epsilon transition
2484  */
2485 static void
2486 nfa_closure_set_create (struct REGEX_INTERNAL_StateSet *ret,
2487                         struct REGEX_INTERNAL_Automaton *nfa,
2488                         struct REGEX_INTERNAL_StateSet *states, const char *label)
2489 {
2490   struct REGEX_INTERNAL_State *s;
2491   unsigned int i;
2492   struct REGEX_INTERNAL_StateSet_MDLL cls_stack;
2493   struct REGEX_INTERNAL_State *clsstate;
2494   struct REGEX_INTERNAL_State *currentstate;
2495   struct REGEX_INTERNAL_Transition *ctran;
2496
2497   memset (ret, 0, sizeof (struct REGEX_INTERNAL_StateSet));
2498   if (NULL == states)
2499     return;
2500
2501   for (i = 0; i < states->off; i++)
2502   {
2503     s = states->states[i];
2504
2505     /* Add start state to closure only for epsilon closure */
2506     if (NULL == label)
2507       state_set_append (ret, s);
2508
2509     /* initialize work stack */
2510     cls_stack.head = NULL;
2511     cls_stack.tail = NULL;
2512     GNUNET_CONTAINER_MDLL_insert (ST, cls_stack.head, cls_stack.tail, s);
2513     cls_stack.len = 1;
2514
2515     while (NULL != (currentstate = cls_stack.tail))
2516     {
2517       GNUNET_CONTAINER_MDLL_remove (ST, cls_stack.head, cls_stack.tail,
2518                                     currentstate);
2519       cls_stack.len--;
2520       for (ctran = currentstate->transitions_head; NULL != ctran;
2521            ctran = ctran->next)
2522       {
2523         if (NULL == (clsstate = ctran->to_state))
2524           continue;
2525         if (0 != clsstate->contained)
2526           continue;
2527         if (0 != nullstrcmp (label, ctran->label))
2528           continue;
2529         state_set_append (ret, clsstate);
2530         GNUNET_CONTAINER_MDLL_insert_tail (ST, cls_stack.head, cls_stack.tail,
2531                                            clsstate);
2532         cls_stack.len++;
2533         clsstate->contained = 1;
2534       }
2535     }
2536   }
2537   for (i = 0; i < ret->off; i++)
2538     ret->states[i]->contained = 0;
2539
2540   if (ret->off > 1)
2541     qsort (ret->states, ret->off, sizeof (struct REGEX_INTERNAL_State *),
2542            &state_compare);
2543 }
2544
2545
2546 /**
2547  * Pops two NFA fragments (a, b) from the stack and concatenates them (ab)
2548  *
2549  * @param ctx context
2550  */
2551 static void
2552 nfa_add_concatenation (struct REGEX_INTERNAL_Context *ctx)
2553 {
2554   struct REGEX_INTERNAL_Automaton *a;
2555   struct REGEX_INTERNAL_Automaton *b;
2556   struct REGEX_INTERNAL_Automaton *new_nfa;
2557
2558   b = ctx->stack_tail;
2559   GNUNET_assert (NULL != b);
2560   GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
2561   a = ctx->stack_tail;
2562   GNUNET_assert (NULL != a);
2563   GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2564
2565   state_add_transition (ctx, a->end, NULL, b->start);
2566   a->end->accepting = 0;
2567   b->end->accepting = 1;
2568
2569   new_nfa = nfa_fragment_create (NULL, NULL);
2570   nfa_add_states (new_nfa, a->states_head, a->states_tail);
2571   nfa_add_states (new_nfa, b->states_head, b->states_tail);
2572   new_nfa->start = a->start;
2573   new_nfa->end = b->end;
2574   new_nfa->state_count += a->state_count + b->state_count;
2575   automaton_fragment_clear (a);
2576   automaton_fragment_clear (b);
2577
2578   GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
2579 }
2580
2581
2582 /**
2583  * Pops a NFA fragment from the stack (a) and adds a new fragment (a*)
2584  *
2585  * @param ctx context
2586  */
2587 static void
2588 nfa_add_star_op (struct REGEX_INTERNAL_Context *ctx)
2589 {
2590   struct REGEX_INTERNAL_Automaton *a;
2591   struct REGEX_INTERNAL_Automaton *new_nfa;
2592   struct REGEX_INTERNAL_State *start;
2593   struct REGEX_INTERNAL_State *end;
2594
2595   a = ctx->stack_tail;
2596
2597   if (NULL == a)
2598   {
2599     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2600                 "nfa_add_star_op failed, because there was no element on the stack");
2601     return;
2602   }
2603
2604   GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2605
2606   start = nfa_state_create (ctx, 0);
2607   end = nfa_state_create (ctx, 1);
2608
2609   state_add_transition (ctx, start, NULL, a->start);
2610   state_add_transition (ctx, start, NULL, end);
2611   state_add_transition (ctx, a->end, NULL, a->start);
2612   state_add_transition (ctx, a->end, NULL, end);
2613
2614   a->end->accepting = 0;
2615   end->accepting = 1;
2616
2617   new_nfa = nfa_fragment_create (start, end);
2618   nfa_add_states (new_nfa, a->states_head, a->states_tail);
2619   automaton_fragment_clear (a);
2620
2621   GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
2622 }
2623
2624
2625 /**
2626  * Pops an NFA fragment (a) from the stack and adds a new fragment (a+)
2627  *
2628  * @param ctx context
2629  */
2630 static void
2631 nfa_add_plus_op (struct REGEX_INTERNAL_Context *ctx)
2632 {
2633   struct REGEX_INTERNAL_Automaton *a;
2634
2635   a = ctx->stack_tail;
2636
2637   if (NULL == a)
2638   {
2639     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2640                 "nfa_add_plus_op failed, because there was no element on the stack");
2641     return;
2642   }
2643
2644   GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2645
2646   state_add_transition (ctx, a->end, NULL, a->start);
2647
2648   GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, a);
2649 }
2650
2651
2652 /**
2653  * Pops an NFA fragment (a) from the stack and adds a new fragment (a?)
2654  *
2655  * @param ctx context
2656  */
2657 static void
2658 nfa_add_question_op (struct REGEX_INTERNAL_Context *ctx)
2659 {
2660   struct REGEX_INTERNAL_Automaton *a;
2661   struct REGEX_INTERNAL_Automaton *new_nfa;
2662   struct REGEX_INTERNAL_State *start;
2663   struct REGEX_INTERNAL_State *end;
2664
2665   a = ctx->stack_tail;
2666   if (NULL == a)
2667   {
2668     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2669                 "nfa_add_question_op failed, because there was no element on the stack");
2670     return;
2671   }
2672
2673   GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2674
2675   start = nfa_state_create (ctx, 0);
2676   end = nfa_state_create (ctx, 1);
2677
2678   state_add_transition (ctx, start, NULL, a->start);
2679   state_add_transition (ctx, start, NULL, end);
2680   state_add_transition (ctx, a->end, NULL, end);
2681
2682   a->end->accepting = 0;
2683
2684   new_nfa = nfa_fragment_create (start, end);
2685   nfa_add_states (new_nfa, a->states_head, a->states_tail);
2686   GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
2687   automaton_fragment_clear (a);
2688 }
2689
2690
2691 /**
2692  * Pops two NFA fragments (a, b) from the stack and adds a new NFA fragment that
2693  * alternates between a and b (a|b)
2694  *
2695  * @param ctx context
2696  */
2697 static void
2698 nfa_add_alternation (struct REGEX_INTERNAL_Context *ctx)
2699 {
2700   struct REGEX_INTERNAL_Automaton *a;
2701   struct REGEX_INTERNAL_Automaton *b;
2702   struct REGEX_INTERNAL_Automaton *new_nfa;
2703   struct REGEX_INTERNAL_State *start;
2704   struct REGEX_INTERNAL_State *end;
2705
2706   b = ctx->stack_tail;
2707   GNUNET_assert (NULL != b);
2708   GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
2709   a = ctx->stack_tail;
2710   GNUNET_assert (NULL != a);
2711   GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2712
2713   start = nfa_state_create (ctx, 0);
2714   end = nfa_state_create (ctx, 1);
2715   state_add_transition (ctx, start, NULL, a->start);
2716   state_add_transition (ctx, start, NULL, b->start);
2717
2718   state_add_transition (ctx, a->end, NULL, end);
2719   state_add_transition (ctx, b->end, NULL, end);
2720
2721   a->end->accepting = 0;
2722   b->end->accepting = 0;
2723   end->accepting = 1;
2724
2725   new_nfa = nfa_fragment_create (start, end);
2726   nfa_add_states (new_nfa, a->states_head, a->states_tail);
2727   nfa_add_states (new_nfa, b->states_head, b->states_tail);
2728   automaton_fragment_clear (a);
2729   automaton_fragment_clear (b);
2730
2731   GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
2732 }
2733
2734
2735 /**
2736  * Adds a new nfa fragment to the stack
2737  *
2738  * @param ctx context
2739  * @param label label for nfa transition
2740  */
2741 static void
2742 nfa_add_label (struct REGEX_INTERNAL_Context *ctx, const char *label)
2743 {
2744   struct REGEX_INTERNAL_Automaton *n;
2745   struct REGEX_INTERNAL_State *start;
2746   struct REGEX_INTERNAL_State *end;
2747
2748   GNUNET_assert (NULL != ctx);
2749
2750   start = nfa_state_create (ctx, 0);
2751   end = nfa_state_create (ctx, 1);
2752   state_add_transition (ctx, start, label, end);
2753   n = nfa_fragment_create (start, end);
2754   GNUNET_assert (NULL != n);
2755   GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, n);
2756 }
2757
2758
2759 /**
2760  * Initialize a new context
2761  *
2762  * @param ctx context
2763  */
2764 static void
2765 REGEX_INTERNAL_context_init (struct REGEX_INTERNAL_Context *ctx)
2766 {
2767   if (NULL == ctx)
2768   {
2769     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Context was NULL!");
2770     return;
2771   }
2772   ctx->state_id = 0;
2773   ctx->transition_id = 0;
2774   ctx->stack_head = NULL;
2775   ctx->stack_tail = NULL;
2776 }
2777
2778
2779 /**
2780  * Construct an NFA by parsing the regex string of length 'len'.
2781  *
2782  * @param regex regular expression string
2783  * @param len length of the string
2784  *
2785  * @return NFA, needs to be freed using REGEX_INTERNAL_destroy_automaton
2786  */
2787 struct REGEX_INTERNAL_Automaton *
2788 REGEX_INTERNAL_construct_nfa (const char *regex, const size_t len)
2789 {
2790   struct REGEX_INTERNAL_Context ctx;
2791   struct REGEX_INTERNAL_Automaton *nfa;
2792   const char *regexp;
2793   char curlabel[2];
2794   char *error_msg;
2795   unsigned int count;
2796   unsigned int altcount;
2797   unsigned int atomcount;
2798   unsigned int poff;
2799   unsigned int psize;
2800   struct
2801   {
2802     int altcount;
2803     int atomcount;
2804   }     *p;
2805
2806   if (NULL == regex || 0 == strlen (regex) || 0 == len)
2807   {
2808     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2809                 "Could not parse regex. Empty regex string provided.\n");
2810
2811     return NULL;
2812   }
2813   REGEX_INTERNAL_context_init (&ctx);
2814
2815   regexp = regex;
2816   curlabel[1] = '\0';
2817   p = NULL;
2818   error_msg = NULL;
2819   altcount = 0;
2820   atomcount = 0;
2821   poff = 0;
2822   psize = 0;
2823
2824   for (count = 0; count < len && *regexp; count++, regexp++)
2825   {
2826     switch (*regexp)
2827     {
2828     case '(':
2829       if (atomcount > 1)
2830       {
2831         --atomcount;
2832         nfa_add_concatenation (&ctx);
2833       }
2834       if (poff == psize)
2835         GNUNET_array_grow (p, psize, psize * 2 + 4); /* FIXME why *2 +4? */
2836       p[poff].altcount = altcount;
2837       p[poff].atomcount = atomcount;
2838       poff++;
2839       altcount = 0;
2840       atomcount = 0;
2841       break;
2842     case '|':
2843       if (0 == atomcount)
2844       {
2845         error_msg = "Cannot append '|' to nothing";
2846         goto error;
2847       }
2848       while (--atomcount > 0)
2849         nfa_add_concatenation (&ctx);
2850       altcount++;
2851       break;
2852     case ')':
2853       if (0 == poff)
2854       {
2855         error_msg = "Missing opening '('";
2856         goto error;
2857       }
2858       if (0 == atomcount)
2859       {
2860         /* Ignore this: "()" */
2861         poff--;
2862         altcount = p[poff].altcount;
2863         atomcount = p[poff].atomcount;
2864         break;
2865       }
2866       while (--atomcount > 0)
2867         nfa_add_concatenation (&ctx);
2868       for (; altcount > 0; altcount--)
2869         nfa_add_alternation (&ctx);
2870       poff--;
2871       altcount = p[poff].altcount;
2872       atomcount = p[poff].atomcount;
2873       atomcount++;
2874       break;
2875     case '*':
2876       if (atomcount == 0)
2877       {
2878         error_msg = "Cannot append '*' to nothing";
2879         goto error;
2880       }
2881       nfa_add_star_op (&ctx);
2882       break;
2883     case '+':
2884       if (atomcount == 0)
2885       {
2886         error_msg = "Cannot append '+' to nothing";
2887         goto error;
2888       }
2889       nfa_add_plus_op (&ctx);
2890       break;
2891     case '?':
2892       if (atomcount == 0)
2893       {
2894         error_msg = "Cannot append '?' to nothing";
2895         goto error;
2896       }
2897       nfa_add_question_op (&ctx);
2898       break;
2899     default:
2900       if (atomcount > 1)
2901       {
2902         --atomcount;
2903         nfa_add_concatenation (&ctx);
2904       }
2905       curlabel[0] = *regexp;
2906       nfa_add_label (&ctx, curlabel);
2907       atomcount++;
2908       break;
2909     }
2910   }
2911   if (0 != poff)
2912   {
2913     error_msg = "Unbalanced parenthesis";
2914     goto error;
2915   }
2916   while (--atomcount > 0)
2917     nfa_add_concatenation (&ctx);
2918   for (; altcount > 0; altcount--)
2919     nfa_add_alternation (&ctx);
2920
2921   GNUNET_array_grow (p, psize, 0);
2922
2923   nfa = ctx.stack_tail;
2924   GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
2925
2926   if (NULL != ctx.stack_head)
2927   {
2928     error_msg = "Creating the NFA failed. NFA stack was not empty!";
2929     goto error;
2930   }
2931
2932   /* Remember the regex that was used to generate this NFA */
2933   nfa->regex = GNUNET_strdup (regex);
2934
2935   /* create depth-first numbering of the states for pretty printing */
2936   REGEX_INTERNAL_automaton_traverse (nfa, NULL, NULL, NULL, &number_states, NULL);
2937
2938   /* No multistriding added so far */
2939   nfa->is_multistrided = GNUNET_NO;
2940
2941   return nfa;
2942
2943 error:
2944   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse regex: `%s'\n", regex);
2945   if (NULL != error_msg)
2946     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", error_msg);
2947
2948   GNUNET_free_non_null (p);
2949
2950   while (NULL != (nfa = ctx.stack_head))
2951   {
2952     GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
2953     REGEX_INTERNAL_automaton_destroy (nfa);
2954   }
2955
2956   return NULL;
2957 }
2958
2959
2960 /**
2961  * Create DFA states based on given 'nfa' and starting with 'dfa_state'.
2962  *
2963  * @param ctx context.
2964  * @param nfa NFA automaton.
2965  * @param dfa DFA automaton.
2966  * @param dfa_state current dfa state, pass epsilon closure of first nfa state
2967  *                  for starting.
2968  */
2969 static void
2970 construct_dfa_states (struct REGEX_INTERNAL_Context *ctx,
2971                       struct REGEX_INTERNAL_Automaton *nfa,
2972                       struct REGEX_INTERNAL_Automaton *dfa,
2973                       struct REGEX_INTERNAL_State *dfa_state)
2974 {
2975   struct REGEX_INTERNAL_Transition *ctran;
2976   struct REGEX_INTERNAL_State *new_dfa_state;
2977   struct REGEX_INTERNAL_State *state_contains;
2978   struct REGEX_INTERNAL_State *state_iter;
2979   struct REGEX_INTERNAL_StateSet tmp;
2980   struct REGEX_INTERNAL_StateSet nfa_set;
2981
2982   for (ctran = dfa_state->transitions_head; NULL != ctran; ctran = ctran->next)
2983   {
2984     if (NULL == ctran->label || NULL != ctran->to_state)
2985       continue;
2986
2987     nfa_closure_set_create (&tmp, nfa, &dfa_state->nfa_set, ctran->label);
2988     nfa_closure_set_create (&nfa_set, nfa, &tmp, NULL);
2989     state_set_clear (&tmp);
2990
2991     state_contains = NULL;
2992     for (state_iter = dfa->states_head; NULL != state_iter;
2993          state_iter = state_iter->next)
2994     {
2995       if (0 == state_set_compare (&state_iter->nfa_set, &nfa_set))
2996       {
2997         state_contains = state_iter;
2998         break;
2999       }
3000     }
3001     if (NULL == state_contains)
3002     {
3003       new_dfa_state = dfa_state_create (ctx, &nfa_set);
3004       automaton_add_state (dfa, new_dfa_state);
3005       ctran->to_state = new_dfa_state;
3006       construct_dfa_states (ctx, nfa, dfa, new_dfa_state);
3007     }
3008     else
3009     {
3010       ctran->to_state = state_contains;
3011       state_set_clear (&nfa_set);
3012     }
3013   }
3014 }
3015
3016
3017 /**
3018  * Construct DFA for the given 'regex' of length 'len'.
3019  *
3020  * Path compression means, that for example a DFA o -> a -> b -> c -> o will be
3021  * compressed to o -> abc -> o. Note that this parameter influences the
3022  * non-determinism of states of the resulting NFA in the DHT (number of outgoing
3023  * edges with the same label). For example for an application that stores IPv4
3024  * addresses as bitstrings it could make sense to limit the path compression to
3025  * 4 or 8.
3026  *
3027  * @param regex regular expression string.
3028  * @param len length of the regular expression.
3029  * @param max_path_len limit the path compression length to the
3030  *        given value. If set to 1, no path compression is applied. Set to 0 for
3031  *        maximal possible path compression (generally not desireable).
3032  * @return DFA, needs to be freed using REGEX_INTERNAL_automaton_destroy.
3033  */
3034 struct REGEX_INTERNAL_Automaton *
3035 REGEX_INTERNAL_construct_dfa (const char *regex, const size_t len,
3036                               unsigned int max_path_len)
3037 {
3038   struct REGEX_INTERNAL_Context ctx;
3039   struct REGEX_INTERNAL_Automaton *dfa;
3040   struct REGEX_INTERNAL_Automaton *nfa;
3041   struct REGEX_INTERNAL_StateSet nfa_start_eps_cls;
3042   struct REGEX_INTERNAL_StateSet singleton_set;
3043
3044   REGEX_INTERNAL_context_init (&ctx);
3045
3046   /* Create NFA */
3047   nfa = REGEX_INTERNAL_construct_nfa (regex, len);
3048
3049   if (NULL == nfa)
3050   {
3051     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3052                 "Could not create DFA, because NFA creation failed\n");
3053     return NULL;
3054   }
3055
3056   dfa = GNUNET_new (struct REGEX_INTERNAL_Automaton);
3057   dfa->type = DFA;
3058   dfa->regex = GNUNET_strdup (regex);
3059
3060   /* Create DFA start state from epsilon closure */
3061   memset (&singleton_set, 0, sizeof (struct REGEX_INTERNAL_StateSet));
3062   state_set_append (&singleton_set, nfa->start);
3063   nfa_closure_set_create (&nfa_start_eps_cls, nfa, &singleton_set, NULL);
3064   state_set_clear (&singleton_set);
3065   dfa->start = dfa_state_create (&ctx, &nfa_start_eps_cls);
3066   automaton_add_state (dfa, dfa->start);
3067
3068   construct_dfa_states (&ctx, nfa, dfa, dfa->start);
3069   REGEX_INTERNAL_automaton_destroy (nfa);
3070
3071   /* Minimize DFA */
3072   if (GNUNET_OK != dfa_minimize (&ctx, dfa))
3073   {
3074     REGEX_INTERNAL_automaton_destroy (dfa);
3075     return NULL;
3076   }
3077
3078   /* Create proofs and hashes for all states */
3079   if (GNUNET_OK != automaton_create_proofs (dfa))
3080   {
3081     REGEX_INTERNAL_automaton_destroy (dfa);
3082     return NULL;
3083   }
3084
3085   /* Compress linear DFA paths */
3086   if (1 != max_path_len)
3087     dfa_compress_paths (&ctx, dfa, max_path_len);
3088
3089   return dfa;
3090 }
3091
3092
3093 /**
3094  * Free the memory allocated by constructing the REGEX_INTERNAL_Automaton data
3095  * structure.
3096  *
3097  * @param a automaton to be destroyed
3098  */
3099 void
3100 REGEX_INTERNAL_automaton_destroy (struct REGEX_INTERNAL_Automaton *a)
3101 {
3102   struct REGEX_INTERNAL_State *s;
3103   struct REGEX_INTERNAL_State *next_state;
3104
3105   if (NULL == a)
3106     return;
3107
3108   GNUNET_free_non_null (a->regex);
3109   GNUNET_free_non_null (a->canonical_regex);
3110
3111   for (s = a->states_head; NULL != s; s = next_state)
3112   {
3113     next_state = s->next;
3114     GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s);
3115     automaton_destroy_state (s);
3116   }
3117
3118   GNUNET_free (a);
3119 }
3120
3121
3122 /**
3123  * Evaluates the given string using the given DFA automaton
3124  *
3125  * @param a automaton, type must be DFA
3126  * @param string string that should be evaluated
3127  *
3128  * @return 0 if string matches, non-0 otherwise
3129  */
3130 static int
3131 evaluate_dfa (struct REGEX_INTERNAL_Automaton *a,
3132               const char *string)
3133 {
3134   const char *strp;
3135   struct REGEX_INTERNAL_State *s;
3136   unsigned int step_len;
3137
3138   if (DFA != a->type)
3139   {
3140     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3141                 "Tried to evaluate DFA, but NFA automaton given");
3142     return -1;
3143   }
3144
3145   s = a->start;
3146
3147   /* If the string is empty but the starting state is accepting, we accept. */
3148   if ((NULL == string || 0 == strlen (string)) && s->accepting)
3149     return 0;
3150
3151   for (strp = string; NULL != strp && *strp; strp += step_len)
3152   {
3153     step_len = dfa_move (&s, strp);
3154
3155     if (NULL == s)
3156       break;
3157   }
3158
3159   if (NULL != s && s->accepting)
3160     return 0;
3161
3162   return 1;
3163 }
3164
3165
3166 /**
3167  * Evaluates the given string using the given NFA automaton
3168  *
3169  * @param a automaton, type must be NFA
3170  * @param string string that should be evaluated
3171  * @return 0 if string matches, non-0 otherwise
3172  */
3173 static int
3174 evaluate_nfa (struct REGEX_INTERNAL_Automaton *a,
3175               const char *string)
3176 {
3177   const char *strp;
3178   char str[2];
3179   struct REGEX_INTERNAL_State *s;
3180   struct REGEX_INTERNAL_StateSet sset;
3181   struct REGEX_INTERNAL_StateSet new_sset;
3182   struct REGEX_INTERNAL_StateSet singleton_set;
3183   unsigned int i;
3184   int result;
3185
3186   if (NFA != a->type)
3187   {
3188     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3189                 "Tried to evaluate NFA, but DFA automaton given");
3190     return -1;
3191   }
3192
3193   /* If the string is empty but the starting state is accepting, we accept. */
3194   if ((NULL == string || 0 == strlen (string)) && a->start->accepting)
3195     return 0;
3196
3197   result = 1;
3198   memset (&singleton_set, 0, sizeof (struct REGEX_INTERNAL_StateSet));
3199   state_set_append (&singleton_set, a->start);
3200   nfa_closure_set_create (&sset, a, &singleton_set, NULL);
3201   state_set_clear (&singleton_set);
3202
3203   str[1] = '\0';
3204   for (strp = string; NULL != strp && *strp; strp++)
3205   {
3206     str[0] = *strp;
3207     nfa_closure_set_create (&new_sset, a, &sset, str);
3208     state_set_clear (&sset);
3209     nfa_closure_set_create (&sset, a, &new_sset, 0);
3210     state_set_clear (&new_sset);
3211   }
3212
3213   for (i = 0; i < sset.off; i++)
3214   {
3215     s = sset.states[i];
3216     if ( (NULL != s) && (s->accepting) )
3217     {
3218       result = 0;
3219       break;
3220     }
3221   }
3222
3223   state_set_clear (&sset);
3224   return result;
3225 }
3226
3227
3228 /**
3229  * Evaluates the given @a string against the given compiled regex @a a
3230  *
3231  * @param a automaton
3232  * @param string string to check
3233  * @return 0 if string matches, non-0 otherwise
3234  */
3235 int
3236 REGEX_INTERNAL_eval (struct REGEX_INTERNAL_Automaton *a,
3237                      const char *string)
3238 {
3239   int result;
3240
3241   switch (a->type)
3242   {
3243   case DFA:
3244     result = evaluate_dfa (a, string);
3245     break;
3246   case NFA:
3247     result = evaluate_nfa (a, string);
3248     break;
3249   default:
3250     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3251                 "Evaluating regex failed, automaton has no type!\n");
3252     result = GNUNET_SYSERR;
3253     break;
3254   }
3255
3256   return result;
3257 }
3258
3259
3260 /**
3261  * Get the canonical regex of the given automaton.
3262  * When constructing the automaton a proof is computed for each state,
3263  * consisting of the regular expression leading to this state. A complete
3264  * regex for the automaton can be computed by combining these proofs.
3265  * As of now this function is only useful for testing.
3266  *
3267  * @param a automaton for which the canonical regex should be returned.
3268  *
3269  * @return
3270  */
3271 const char *
3272 REGEX_INTERNAL_get_canonical_regex (struct REGEX_INTERNAL_Automaton *a)
3273 {
3274   if (NULL == a)
3275     return NULL;
3276
3277   return a->canonical_regex;
3278 }
3279
3280
3281 /**
3282  * Get the number of transitions that are contained in the given automaton.
3283  *
3284  * @param a automaton for which the number of transitions should be returned.
3285  *
3286  * @return number of transitions in the given automaton.
3287  */
3288 unsigned int
3289 REGEX_INTERNAL_get_transition_count (struct REGEX_INTERNAL_Automaton *a)
3290 {
3291   unsigned int t_count;
3292   struct REGEX_INTERNAL_State *s;
3293
3294   if (NULL == a)
3295     return 0;
3296
3297   t_count = 0;
3298   for (s = a->states_head; NULL != s; s = s->next)
3299     t_count += s->transition_count;
3300
3301   return t_count;
3302 }
3303
3304
3305 /**
3306  * Get the first key for the given @a input_string. This hashes the first x bits
3307  * of the @a input_string.
3308  *
3309  * @param input_string string.
3310  * @param string_len length of the @a input_string.
3311  * @param key pointer to where to write the hash code.
3312  * @return number of bits of @a input_string that have been consumed
3313  *         to construct the key
3314  */
3315 size_t
3316 REGEX_INTERNAL_get_first_key (const char *input_string,
3317                               size_t string_len,
3318                               struct GNUNET_HashCode *key)
3319 {
3320   size_t size;
3321
3322   size = string_len < GNUNET_REGEX_INITIAL_BYTES ? string_len :
3323                                                    GNUNET_REGEX_INITIAL_BYTES;
3324   if (NULL == input_string)
3325   {
3326     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3327                 "Given input string was NULL!\n");
3328     return 0;
3329   }
3330   GNUNET_CRYPTO_hash (input_string, size, key);
3331
3332   return size;
3333 }
3334
3335
3336 /**
3337  * Recursive function that calls the iterator for each synthetic start state.
3338  *
3339  * @param min_len minimum length of the path in the graph.
3340  * @param max_len maximum length of the path in the graph.
3341  * @param consumed_string string consumed by traversing the graph till this state.
3342  * @param state current state of the automaton.
3343  * @param iterator iterator function called for each edge.
3344  * @param iterator_cls closure for the @a iterator function.
3345  */
3346 static void
3347 iterate_initial_edge (unsigned int min_len,
3348                       unsigned int max_len,
3349                       char *consumed_string,
3350                       struct REGEX_INTERNAL_State *state,
3351                       REGEX_INTERNAL_KeyIterator iterator,
3352                       void *iterator_cls)
3353 {
3354   char *temp;
3355   struct REGEX_INTERNAL_Transition *t;
3356   unsigned int num_edges = state->transition_count;
3357   struct REGEX_BLOCK_Edge edges[num_edges];
3358   struct REGEX_BLOCK_Edge edge[1];
3359   struct GNUNET_HashCode hash;
3360   struct GNUNET_HashCode hash_new;
3361   unsigned int cur_len;
3362
3363   if (NULL != consumed_string)
3364     cur_len = strlen (consumed_string);
3365   else
3366     cur_len = 0;
3367
3368   if ( ( (cur_len >= min_len) ||
3369          (GNUNET_YES == state->accepting) ) &&
3370        (cur_len > 0) &&
3371        (NULL != consumed_string) )
3372   {
3373     if (cur_len <= max_len)
3374     {
3375       if ( (NULL != state->proof) &&
3376            (0 != strcmp (consumed_string,
3377                          state->proof)) )
3378       {
3379         (void) state_get_edges (state, edges);
3380         GNUNET_CRYPTO_hash (consumed_string,
3381                             strlen (consumed_string),
3382                             &hash);
3383         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3384                     "Start state for string `%s' is %s\n",
3385                     consumed_string,
3386                     GNUNET_h2s (&hash));
3387         iterator (iterator_cls,
3388                   &hash,
3389                   consumed_string,
3390                   state->accepting,
3391                   num_edges, edges);
3392       }
3393
3394       if ( (GNUNET_YES == state->accepting) &&
3395            (cur_len > 1) &&
3396            (state->transition_count < 1) &&
3397            (cur_len < max_len) )
3398       {
3399         /* Special case for regex consisting of just a string that is shorter than
3400          * max_len */
3401         edge[0].label = &consumed_string[cur_len - 1];
3402         edge[0].destination = state->hash;
3403         temp = GNUNET_strdup (consumed_string);
3404         temp[cur_len - 1] = '\0';
3405         GNUNET_CRYPTO_hash (temp,
3406                             cur_len - 1,
3407                             &hash_new);
3408         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3409                     "Start state for short string `%s' is %s\n",
3410                     temp,
3411                     GNUNET_h2s (&hash_new));
3412         iterator (iterator_cls,
3413                   &hash_new,
3414                   temp,
3415                   GNUNET_NO, 1,
3416                   edge);
3417         GNUNET_free (temp);
3418       }
3419     }
3420     else /* cur_len > max_len */
3421     {
3422       /* Case where the concatenated labels are longer than max_len, then split. */
3423       edge[0].label = &consumed_string[max_len];
3424       edge[0].destination = state->hash;
3425       temp = GNUNET_strdup (consumed_string);
3426       temp[max_len] = '\0';
3427       GNUNET_CRYPTO_hash (temp, max_len, &hash);
3428       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3429                   "Start state at split edge `%s'-`%s` is %s\n",
3430                   temp,
3431                   edge[0].label,
3432                   GNUNET_h2s (&hash_new));
3433       iterator (iterator_cls,
3434                 &hash,
3435                 temp,
3436                 GNUNET_NO,
3437                 1,
3438                 edge);
3439       GNUNET_free (temp);
3440     }
3441   }
3442
3443   if (cur_len < max_len)
3444   {
3445     for (t = state->transitions_head; NULL != t; t = t->next)
3446     {
3447       if (NULL != strchr (t->label,
3448                           (int) '.'))
3449       {
3450         /* Wildcards not allowed during starting states */
3451         GNUNET_break (0);
3452         continue;
3453       }
3454       if (NULL != consumed_string)
3455         GNUNET_asprintf (&temp,
3456                          "%s%s",
3457                          consumed_string,
3458                          t->label);
3459       else
3460         GNUNET_asprintf (&temp,
3461                          "%s",
3462                          t->label);
3463       iterate_initial_edge (min_len,
3464                             max_len,
3465                             temp,
3466                             t->to_state,
3467                             iterator,
3468                             iterator_cls);
3469       GNUNET_free (temp);
3470     }
3471   }
3472 }
3473
3474
3475 /**
3476  * Iterate over all edges starting from start state of automaton 'a'. Calling
3477  * iterator for each edge.
3478  *
3479  * @param a automaton.
3480  * @param iterator iterator called for each edge.
3481  * @param iterator_cls closure.
3482  */
3483 void
3484 REGEX_INTERNAL_iterate_all_edges (struct REGEX_INTERNAL_Automaton *a,
3485                                   REGEX_INTERNAL_KeyIterator iterator,
3486                                   void *iterator_cls)
3487 {
3488   struct REGEX_INTERNAL_State *s;
3489
3490   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3491               "Iterating over starting edges\n");
3492   iterate_initial_edge (GNUNET_REGEX_INITIAL_BYTES,
3493                         GNUNET_REGEX_INITIAL_BYTES,
3494                         NULL, a->start,
3495                         iterator, iterator_cls);
3496   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3497               "Iterating over DFA edges\n");
3498   for (s = a->states_head; NULL != s; s = s->next)
3499   {
3500     struct REGEX_BLOCK_Edge edges[s->transition_count];
3501     unsigned int num_edges;
3502
3503     num_edges = state_get_edges (s, edges);
3504     if ( ( (NULL != s->proof) &&
3505            (0 < strlen (s->proof)) ) || s->accepting)
3506     {
3507       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3508                   "Creating DFA edges at `%s' under key %s\n",
3509                   s->proof,
3510                   GNUNET_h2s (&s->hash));
3511       iterator (iterator_cls, &s->hash, s->proof,
3512                 s->accepting,
3513                 num_edges, edges);
3514     }
3515     s->marked = GNUNET_NO;
3516   }
3517 }
3518
3519
3520 /**
3521  * Struct to hold all the relevant state information in the HashMap.
3522  *
3523  * Contains the same info as the Regex Iterator parametes except the key,
3524  * which comes directly from the HashMap iterator.
3525  */
3526 struct temporal_state_store {
3527   int reachable;
3528   char *proof;
3529   int accepting;
3530   int num_edges;
3531   struct REGEX_BLOCK_Edge *edges;
3532 };
3533
3534
3535 /**
3536  * Store regex iterator and cls in one place to pass to the hashmap iterator.
3537  */
3538 struct client_iterator {
3539   REGEX_INTERNAL_KeyIterator iterator;
3540   void *iterator_cls;
3541 };
3542
3543
3544 /**
3545  * Iterator over all edges of a dfa. Stores all of them in a HashMap
3546  * for later reachability marking.
3547  *
3548  * @param cls Closure (HashMap)
3549  * @param key hash for current state.
3550  * @param proof proof for current state
3551  * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
3552  * @param num_edges number of edges leaving current state.
3553  * @param edges edges leaving current state.
3554  */
3555 static void
3556 store_all_states (void *cls,
3557                   const struct GNUNET_HashCode *key,
3558                   const char *proof,
3559                   int accepting,
3560                   unsigned int num_edges,
3561                   const struct REGEX_BLOCK_Edge *edges)
3562 {
3563   struct GNUNET_CONTAINER_MultiHashMap *hm = cls;
3564   struct temporal_state_store *tmp;
3565   size_t edges_size;
3566
3567   tmp = GNUNET_new (struct temporal_state_store);
3568   tmp->reachable = GNUNET_NO;
3569   tmp->proof = GNUNET_strdup (proof);
3570   tmp->accepting = accepting;
3571   tmp->num_edges = num_edges;
3572   edges_size = sizeof (struct REGEX_BLOCK_Edge) * num_edges;
3573   tmp->edges = GNUNET_malloc (edges_size);
3574   GNUNET_memcpy(tmp->edges, edges, edges_size);
3575   GNUNET_CONTAINER_multihashmap_put (hm, key, tmp,
3576                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
3577 }
3578
3579
3580 /**
3581  * Mark state as reachable and call recursively on all its edges.
3582  *
3583  * If already marked as reachable, do nothing.
3584  *
3585  * @param state State to mark as reachable.
3586  * @param hm HashMap which stores all the states indexed by key.
3587  */
3588 static void
3589 mark_as_reachable (struct temporal_state_store *state,
3590                    struct GNUNET_CONTAINER_MultiHashMap *hm)
3591 {
3592   struct temporal_state_store *child;
3593   unsigned int i;
3594
3595   if (GNUNET_YES == state->reachable)
3596     /* visited */
3597     return;
3598
3599   state->reachable = GNUNET_YES;
3600   for (i = 0; i < state->num_edges; i++)
3601   {
3602     child = GNUNET_CONTAINER_multihashmap_get (hm,
3603                                                &state->edges[i].destination);
3604     if (NULL == child)
3605     {
3606       GNUNET_break (0);
3607       continue;
3608     }
3609     mark_as_reachable (child, hm);
3610   }
3611 }
3612
3613
3614 /**
3615  * Iterator over hash map entries to mark the ones that are reachable.
3616  *
3617  * @param cls closure
3618  * @param key current key code
3619  * @param value value in the hash map
3620  * @return #GNUNET_YES if we should continue to iterate,
3621  *         #GNUNET_NO if not.
3622  */
3623 static int
3624 reachability_iterator (void *cls,
3625                        const struct GNUNET_HashCode *key,
3626                        void *value)
3627 {
3628   struct GNUNET_CONTAINER_MultiHashMap *hm = cls;
3629   struct temporal_state_store *state = value;
3630
3631   if (GNUNET_YES == state->reachable)
3632     /* already visited and marked */
3633     return GNUNET_YES;
3634
3635   if (GNUNET_REGEX_INITIAL_BYTES > strlen (state->proof) &&
3636       GNUNET_NO == state->accepting)
3637     /* not directly reachable */
3638     return GNUNET_YES;
3639
3640   mark_as_reachable (state, hm);
3641   return GNUNET_YES;
3642 }
3643
3644
3645 /**
3646  * Iterator over hash map entries.
3647  * Calling the callback on the ones marked as reachables.
3648  *
3649  * @param cls closure
3650  * @param key current key code
3651  * @param value value in the hash map
3652  * @return #GNUNET_YES if we should continue to iterate,
3653  *         #GNUNET_NO if not.
3654  */
3655 static int
3656 iterate_reachables (void *cls,
3657                     const struct GNUNET_HashCode *key,
3658                     void *value)
3659 {
3660   struct client_iterator *ci = cls;
3661   struct temporal_state_store *state = value;
3662
3663   if (GNUNET_YES == state->reachable)
3664   {
3665     ci->iterator (ci->iterator_cls, key,
3666                   state->proof, state->accepting,
3667                   state->num_edges, state->edges);
3668   }
3669   GNUNET_free (state->edges);
3670   GNUNET_free (state->proof);
3671   GNUNET_free (state);
3672   return GNUNET_YES;
3673
3674 }
3675
3676 /**
3677  * Iterate over all edges of automaton 'a' that are reachable from a state with
3678  * a proof of at least GNUNET_REGEX_INITIAL_BYTES characters.
3679  *
3680  * Call the iterator for each such edge.
3681  *
3682  * @param a automaton.
3683  * @param iterator iterator called for each reachable edge.
3684  * @param iterator_cls closure.
3685  */
3686 void
3687 REGEX_INTERNAL_iterate_reachable_edges (struct REGEX_INTERNAL_Automaton *a,
3688                                         REGEX_INTERNAL_KeyIterator iterator,
3689                                         void *iterator_cls)
3690 {
3691   struct GNUNET_CONTAINER_MultiHashMap *hm;
3692   struct client_iterator ci;
3693
3694   hm = GNUNET_CONTAINER_multihashmap_create (a->state_count * 2, GNUNET_NO);
3695   ci.iterator = iterator;
3696   ci.iterator_cls = iterator_cls;
3697
3698   REGEX_INTERNAL_iterate_all_edges (a, &store_all_states, hm);
3699   GNUNET_CONTAINER_multihashmap_iterate (hm, &reachability_iterator, hm);
3700   GNUNET_CONTAINER_multihashmap_iterate (hm, &iterate_reachables, &ci);
3701
3702   GNUNET_CONTAINER_multihashmap_destroy (hm);
3703 }
3704
3705
3706 /* end of regex_internal.c */