GNUnet 0.22.2
regex_internal.h File Reference
Include dependency graph for regex_internal.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  REGEX_INTERNAL_Transition
 Transition between two states. More...
 
struct  REGEX_INTERNAL_StateSet
 Set of states. More...
 
struct  REGEX_INTERNAL_State
 A state. More...
 
struct  REGEX_INTERNAL_Automaton
 Automaton representation. More...
 
struct  REGEX_INTERNAL_Context
 Context that contains an id counter for states and transitions as well as a DLL of automatons used as a stack for NFA construction. More...
 

Macros

#define ALLOWED_LITERALS    "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
 char array of literals that are allowed inside a regex (apart from the operators) More...
 

Typedefs

typedef int(* REGEX_INTERNAL_traverse_check) (void *cls, struct REGEX_INTERNAL_State *s, struct REGEX_INTERNAL_Transition *t)
 Function that gets passed to automaton traversal and is called before each next traversal from state 's' using transition 't' to check if traversal should proceed. More...
 
typedef void(* REGEX_INTERNAL_traverse_action) (void *cls, const unsigned int count, struct REGEX_INTERNAL_State *s)
 Function that is called with each state, when traversing an automaton. More...
 

Enumerations

enum  REGEX_INTERNAL_AutomatonType { NFA , DFA }
 Type of an automaton. More...
 

Functions

struct REGEX_INTERNAL_AutomatonREGEX_INTERNAL_construct_nfa (const char *regex, const size_t len)
 Construct an NFA by parsing the regex string of length 'len'. More...
 
void REGEX_INTERNAL_automaton_traverse (const struct REGEX_INTERNAL_Automaton *a, struct REGEX_INTERNAL_State *start, REGEX_INTERNAL_traverse_check check, void *check_cls, REGEX_INTERNAL_traverse_action action, void *action_cls)
 Traverses the given automaton using depth-first-search (DFS) from it's start state, visiting all reachable states and calling 'action' on each one of them. More...
 
const char * REGEX_INTERNAL_get_canonical_regex (struct REGEX_INTERNAL_Automaton *a)
 Get the canonical regex of the given automaton. More...
 
unsigned int REGEX_INTERNAL_get_transition_count (struct REGEX_INTERNAL_Automaton *a)
 Get the number of transitions that are contained in the given automaton. More...
 
void REGEX_INTERNAL_dfa_add_multi_strides (struct REGEX_INTERNAL_Context *regex_ctx, struct REGEX_INTERNAL_Automaton *dfa, const unsigned int stride_len)
 Adds multi-strided transitions to the given 'dfa'. More...
 

Macro Definition Documentation

◆ ALLOWED_LITERALS

#define ALLOWED_LITERALS    "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

char array of literals that are allowed inside a regex (apart from the operators)

Definition at line 42 of file regex_internal.h.

Typedef Documentation

◆ REGEX_INTERNAL_traverse_check

typedef int(* REGEX_INTERNAL_traverse_check) (void *cls, struct REGEX_INTERNAL_State *s, struct REGEX_INTERNAL_Transition *t)

Function that gets passed to automaton traversal and is called before each next traversal from state 's' using transition 't' to check if traversal should proceed.

Return GNUNET_NO to stop traversal or GNUNET_YES to continue.

Parameters
clsclosure for the check.
scurrent state in the traversal.
tcurrent transition from state 's' that will be used for the next step.
Returns
GNUNET_YES to proceed traversal, GNUNET_NO to stop.

Definition at line 343 of file regex_internal.h.

◆ REGEX_INTERNAL_traverse_action

typedef void(* REGEX_INTERNAL_traverse_action) (void *cls, const unsigned int count, struct REGEX_INTERNAL_State *s)

Function that is called with each state, when traversing an automaton.

Parameters
clsclosure.
countcurrent count of the state, from 0 to a->state_count -1.
sstate.

Definition at line 356 of file regex_internal.h.

Enumeration Type Documentation

◆ REGEX_INTERNAL_AutomatonType

Type of an automaton.

Enumerator
NFA 
DFA 

Definition at line 249 of file regex_internal.h.

250{
251 NFA,
252 DFA
253};
@ NFA
@ DFA

Function Documentation

◆ REGEX_INTERNAL_construct_nfa()

struct REGEX_INTERNAL_Automaton * REGEX_INTERNAL_construct_nfa ( const char *  regex,
const size_t  len 
)

Construct an NFA by parsing the regex string of length 'len'.

Parameters
regexregular expression string.
lenlength of the string.
Returns
NFA, needs to be freed using REGEX_INTERNAL_automaton_destroy.
Parameters
regexregular expression string
lenlength of the string
Returns
NFA, needs to be freed using REGEX_INTERNAL_destroy_automaton

Definition at line 2795 of file regex_internal.c.

2796{
2798 struct REGEX_INTERNAL_Automaton *nfa;
2799 const char *regexp;
2800 char curlabel[2];
2801 const char *error_msg;
2802 unsigned int count;
2803 unsigned int altcount;
2804 unsigned int atomcount;
2805 unsigned int poff;
2806 unsigned int psize;
2807
2808 struct
2809 {
2810 int altcount;
2811 int atomcount;
2812 } *p;
2813
2814 if ((NULL == regex) || (0 == strlen (regex)) || (0 == len))
2815 {
2817 "Could not parse regex. Empty regex string provided.\n");
2818
2819 return NULL;
2820 }
2822
2823 regexp = regex;
2824 curlabel[1] = '\0';
2825 p = NULL;
2826 error_msg = NULL;
2827 altcount = 0;
2828 atomcount = 0;
2829 poff = 0;
2830 psize = 0;
2831
2832 for (count = 0; count < len && *regexp; count++, regexp++)
2833 {
2834 switch (*regexp)
2835 {
2836 case '(':
2837 if (atomcount > 1)
2838 {
2839 --atomcount;
2841 }
2842 if (poff == psize)
2843 GNUNET_array_grow (p, psize, psize * 2 + 4); /* FIXME why *2 +4? */
2844 p[poff].altcount = altcount;
2845 p[poff].atomcount = atomcount;
2846 poff++;
2847 altcount = 0;
2848 atomcount = 0;
2849 break;
2850
2851 case '|':
2852 if (0 == atomcount)
2853 {
2854 error_msg = "Cannot append '|' to nothing";
2855 goto error;
2856 }
2857 while (--atomcount > 0)
2859 altcount++;
2860 break;
2861
2862 case ')':
2863 if (0 == poff)
2864 {
2865 error_msg = "Missing opening '('";
2866 goto error;
2867 }
2868 if (0 == atomcount)
2869 {
2870 /* Ignore this: "()" */
2871 poff--;
2872 altcount = p[poff].altcount;
2873 atomcount = p[poff].atomcount;
2874 break;
2875 }
2876 while (--atomcount > 0)
2878 for (; altcount > 0; altcount--)
2880 poff--;
2881 altcount = p[poff].altcount;
2882 atomcount = p[poff].atomcount;
2883 atomcount++;
2884 break;
2885
2886 case '*':
2887 if (atomcount == 0)
2888 {
2889 error_msg = "Cannot append '*' to nothing";
2890 goto error;
2891 }
2893 break;
2894
2895 case '+':
2896 if (atomcount == 0)
2897 {
2898 error_msg = "Cannot append '+' to nothing";
2899 goto error;
2900 }
2902 break;
2903
2904 case '?':
2905 if (atomcount == 0)
2906 {
2907 error_msg = "Cannot append '?' to nothing";
2908 goto error;
2909 }
2911 break;
2912
2913 default:
2914 if (atomcount > 1)
2915 {
2916 --atomcount;
2918 }
2919 curlabel[0] = *regexp;
2920 nfa_add_label (&ctx, curlabel);
2921 atomcount++;
2922 break;
2923 }
2924 }
2925 if (0 != poff)
2926 {
2927 error_msg = "Unbalanced parenthesis";
2928 goto error;
2929 }
2930 while (--atomcount > 0)
2932 for (; altcount > 0; altcount--)
2934
2935 GNUNET_array_grow (p, psize, 0);
2936
2937 nfa = ctx.stack_tail;
2938 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
2939
2940 if (NULL != ctx.stack_head)
2941 {
2942 error_msg = "Creating the NFA failed. NFA stack was not empty!";
2943 goto error;
2944 }
2945
2946 /* Remember the regex that was used to generate this NFA */
2947 nfa->regex = GNUNET_strdup (regex);
2948
2949 /* create depth-first numbering of the states for pretty printing */
2951 NULL,
2952 NULL,
2953 NULL,
2955 NULL);
2956
2957 /* No multistriding added so far */
2959
2960 return nfa;
2961
2962error:
2963 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse regex: `%s'\n", regex);
2964 if (NULL != error_msg)
2965 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", error_msg);
2966
2967 GNUNET_free (p);
2968
2969 while (NULL != (nfa = ctx.stack_head))
2970 {
2971 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
2973 }
2974
2975 return NULL;
2976}
static struct GNUNET_FS_Handle * ctx
static struct GNUNET_OS_Process * p
Helper process we started.
Definition: gnunet-uri.c:38
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
#define GNUNET_log(kind,...)
@ GNUNET_NO
@ GNUNET_ERROR_TYPE_ERROR
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_array_grow(arr, size, tsize)
Grow a well-typed (!) array.
#define GNUNET_free(ptr)
Wrapper around free.
static void nfa_add_concatenation(struct REGEX_INTERNAL_Context *ctx)
Pops two NFA fragments (a, b) from the stack and concatenates them (ab)
void REGEX_INTERNAL_automaton_traverse(const struct REGEX_INTERNAL_Automaton *a, struct REGEX_INTERNAL_State *start, REGEX_INTERNAL_traverse_check check, void *check_cls, REGEX_INTERNAL_traverse_action action, void *action_cls)
Traverses the given automaton using depth-first-search (DFS) from it's start state,...
static void nfa_add_question_op(struct REGEX_INTERNAL_Context *ctx)
Pops an NFA fragment (a) from the stack and adds a new fragment (a?)
void REGEX_INTERNAL_automaton_destroy(struct REGEX_INTERNAL_Automaton *a)
Free the memory allocated by constructing the REGEX_INTERNAL_Automaton.
static void nfa_add_star_op(struct REGEX_INTERNAL_Context *ctx)
Pops a NFA fragment from the stack (a) and adds a new fragment (a*)
static void nfa_add_alternation(struct REGEX_INTERNAL_Context *ctx)
Pops two NFA fragments (a, b) from the stack and adds a new NFA fragment that alternates between a an...
static void nfa_add_label(struct REGEX_INTERNAL_Context *ctx, const char *label)
Adds a new nfa fragment to the stack.
static void number_states(void *cls, const unsigned int count, struct REGEX_INTERNAL_State *s)
Helper function used as 'action' in 'REGEX_INTERNAL_automaton_traverse' function to create the depth-...
static void REGEX_INTERNAL_context_init(struct REGEX_INTERNAL_Context *ctx)
Initialize a new context.
static void nfa_add_plus_op(struct REGEX_INTERNAL_Context *ctx)
Pops an NFA fragment (a) from the stack and adds a new fragment (a+)
Automaton representation.
int is_multistrided
GNUNET_YES, if multi strides have been added to the Automaton.
Context that contains an id counter for states and transitions as well as a DLL of automatons used as...

References ctx, GNUNET_array_grow, GNUNET_CONTAINER_DLL_remove, GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_log, GNUNET_NO, GNUNET_strdup, REGEX_INTERNAL_Automaton::is_multistrided, nfa_add_alternation(), nfa_add_concatenation(), nfa_add_label(), nfa_add_plus_op(), nfa_add_question_op(), nfa_add_star_op(), number_states(), p, REGEX_INTERNAL_Automaton::regex, REGEX_INTERNAL_automaton_destroy(), REGEX_INTERNAL_automaton_traverse(), and REGEX_INTERNAL_context_init().

Referenced by REGEX_INTERNAL_construct_dfa().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ REGEX_INTERNAL_automaton_traverse()

void REGEX_INTERNAL_automaton_traverse ( const struct REGEX_INTERNAL_Automaton a,
struct REGEX_INTERNAL_State start,
REGEX_INTERNAL_traverse_check  check,
void *  check_cls,
REGEX_INTERNAL_traverse_action  action,
void *  action_cls 
)

Traverses the given automaton using depth-first-search (DFS) from it's start state, visiting all reachable states and calling 'action' on each one of them.

Parameters
aautomaton to be traversed.
startstart state, pass a->start or NULL to traverse the whole automaton.
checkfunction that is checked before advancing on each transition in the DFS.
check_clsclosure for check.
actionaction to be performed on each state.
action_clsclosure for action

Definition at line 505 of file regex_internal.c.

511{
512 if ((NULL == a) || (0 == a->state_count))
513 return;
514
515 {
516 unsigned int count;
517 int marks[a->state_count];
518 struct REGEX_INTERNAL_State *s;
519
520
521 for (count = 0, s = a->states_head; NULL != s && count < a->state_count;
522 s = s->next, count++)
523 {
524 s->traversal_id = count;
525 marks[s->traversal_id] = GNUNET_NO;
526 }
527
528 count = 0;
529
530 if (NULL == start)
531 s = a->start;
532 else
533 s = start;
534
536 marks,
537 &count,
538 check,
539 check_cls,
540 action,
541 action_cls);
542 }
543}
static int start
Set if we are to start default services (including ARM).
Definition: gnunet-arm.c:38
static void automaton_state_traverse(struct REGEX_INTERNAL_State *s, int *marks, unsigned int *count, REGEX_INTERNAL_traverse_check check, void *check_cls, REGEX_INTERNAL_traverse_action action, void *action_cls)
Depth-first traversal (DFS) of all states that are reachable from state 's'.
struct REGEX_INTERNAL_State * start
First state of the automaton.
unsigned int state_count
Number of states in the automaton.
struct REGEX_INTERNAL_State * states_head
DLL of states.
struct REGEX_INTERNAL_State * next
This is a linked list to keep states in an automaton.
unsigned int traversal_id
Unique state id that is used for traversing the automaton.

References consensus-simulation::action, automaton_state_traverse(), GNUNET_NO, REGEX_INTERNAL_State::next, start, REGEX_INTERNAL_Automaton::start, REGEX_INTERNAL_Automaton::state_count, REGEX_INTERNAL_Automaton::states_head, and REGEX_INTERNAL_State::traversal_id.

Referenced by automaton_create_proofs(), dfa_remove_unreachable_states(), REGEX_INTERNAL_construct_nfa(), REGEX_INTERNAL_dfa_add_multi_strides(), and REGEX_TEST_automaton_save_graph().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ REGEX_INTERNAL_get_canonical_regex()

const char * REGEX_INTERNAL_get_canonical_regex ( struct REGEX_INTERNAL_Automaton a)

Get the canonical regex of the given automaton.

When constructing the automaton a proof is computed for each state, consisting of the regular expression leading to this state. A complete regex for the automaton can be computed by combining these proofs. As of now this function is only useful for testing.

Parameters
aautomaton for which the canonical regex should be returned.
Returns
canonical regex string.

Definition at line 3250 of file regex_internal.c.

3251{
3252 if (NULL == a)
3253 return NULL;
3254
3255 return a->canonical_regex;
3256}
char * canonical_regex
Canonical regex (result of RX->NFA->DFA->RX)

References REGEX_INTERNAL_Automaton::canonical_regex.

◆ REGEX_INTERNAL_get_transition_count()

unsigned int REGEX_INTERNAL_get_transition_count ( struct REGEX_INTERNAL_Automaton a)

Get the number of transitions that are contained in the given automaton.

Parameters
aautomaton for which the number of transitions should be returned.
Returns
number of transitions in the given automaton.

Definition at line 3267 of file regex_internal.c.

3268{
3269 unsigned int t_count;
3270 struct REGEX_INTERNAL_State *s;
3271
3272 if (NULL == a)
3273 return 0;
3274
3275 t_count = 0;
3276 for (s = a->states_head; NULL != s; s = s->next)
3277 t_count += s->transition_count;
3278
3279 return t_count;
3280}
unsigned int transition_count
Number of transitions from this state to other states.

References REGEX_INTERNAL_State::next, REGEX_INTERNAL_Automaton::states_head, and REGEX_INTERNAL_State::transition_count.

◆ REGEX_INTERNAL_dfa_add_multi_strides()

void REGEX_INTERNAL_dfa_add_multi_strides ( struct REGEX_INTERNAL_Context regex_ctx,
struct REGEX_INTERNAL_Automaton dfa,
const unsigned int  stride_len 
)

Adds multi-strided transitions to the given 'dfa'.

Parameters
regex_ctxregex context needed to add transitions to the automaton.
dfaDFA to which the multi strided transitions should be added.
stride_lenlength of the strides.

Definition at line 2183 of file regex_internal.c.

2186{
2187 struct REGEX_INTERNAL_Strided_Context ctx = { stride_len, NULL, NULL };
2189 struct REGEX_INTERNAL_Transition *t_next;
2190
2191 if ((1 > stride_len) || (GNUNET_YES == dfa->is_multistrided))
2192 return;
2193
2194 /* Compute the new transitions of given stride_len */
2196 dfa->start,
2197 NULL,
2198 NULL,
2200 &ctx);
2201
2202 /* Add all the new transitions to the automaton. */
2203 for (t = ctx.transitions_head; NULL != t; t = t_next)
2204 {
2205 t_next = t->next;
2206 state_add_transition (regex_ctx, t->from_state, t->label, t->to_state);
2207 GNUNET_CONTAINER_DLL_remove (ctx.transitions_head, ctx.transitions_tail, t);
2208 GNUNET_free (t->label);
2209 GNUNET_free (t);
2210 }
2211
2212 /* Mark this automaton as multistrided */
2214}
static struct GNUNET_SCHEDULER_Task * t
Main task.
@ GNUNET_YES
static void state_add_transition(struct REGEX_INTERNAL_Context *ctx, struct REGEX_INTERNAL_State *from_state, const char *label, struct REGEX_INTERNAL_State *to_state)
Adds a transition from one state to another on label.
static void dfa_add_multi_strides(void *cls, const unsigned int count, struct REGEX_INTERNAL_State *s)
Function called for each state in the DFA.
struct GNUNET_SCHEDULER_Task * next
This is a linked list.
Definition: scheduler.c:140
Context for adding strided transitions to a DFA.
Transition between two states.

References ctx, dfa_add_multi_strides(), GNUNET_CONTAINER_DLL_remove, GNUNET_free, GNUNET_YES, REGEX_INTERNAL_Automaton::is_multistrided, GNUNET_SCHEDULER_Task::next, REGEX_INTERNAL_automaton_traverse(), REGEX_INTERNAL_Automaton::start, state_add_transition(), and t.

Here is the call graph for this function: