GNUnet  0.10.x
Enumerations | Functions
regex_test_lib.h File Reference

library to read regexes representing IP networks from a file. More...

#include "regex_internal_lib.h"
Include dependency graph for regex_test_lib.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Enumerations

enum  REGEX_TEST_GraphSavingOptions { REGEX_TEST_GRAPH_DEFAULT = 0, REGEX_TEST_GRAPH_VERBOSE = 1, REGEX_TEST_GRAPH_COLORING = 2 }
 Options for graph creation function REGEX_TEST_automaton_save_graph. More...
 

Functions

char * REGEX_TEST_combine (char *const regexes[], unsigned int alphabet_size)
 Combine an array of regexes into a single prefix-shared regex. More...
 
char ** REGEX_TEST_read_from_file (const char *filename)
 Read a set of regexes from a file, one per line and return them in an array suitable for REGEX_TEST_combine. More...
 
void REGEX_TEST_free_from_file (char **regexes)
 Free all memory reserved for a set of regexes created by read_from_file. More...
 
char * REGEX_TEST_generate_random_regex (size_t rx_length, char *matching_str)
 Generate a (pseudo) random regular expression of length 'rx_length', as well as a (optional) string that will be matched by the generated regex. More...
 
char * REGEX_TEST_generate_random_string (size_t max_len)
 Generate a random string of maximum length 'max_len' that only contains literals allowed in a regular expression. More...
 
void REGEX_TEST_automaton_save_graph (struct REGEX_INTERNAL_Automaton *a, const char *filename, enum REGEX_TEST_GraphSavingOptions options)
 Save the given automaton as a GraphViz dot file. More...
 

Detailed Description

library to read regexes representing IP networks from a file.

and simplifying the into one big regex, in order to run tests (regex performance, regex profiler).

Author
Bertlomiej Polot

Definition in file regex_test_lib.h.

Enumeration Type Documentation

◆ REGEX_TEST_GraphSavingOptions

Options for graph creation function REGEX_TEST_automaton_save_graph.

Enumerator
REGEX_TEST_GRAPH_DEFAULT 

Default.

Do nothing special.

REGEX_TEST_GRAPH_VERBOSE 

The generated graph will include extra information such as the NFA states that were used to generate the DFA state.

REGEX_TEST_GRAPH_COLORING 

Enable graph coloring.

Will color each SCC in a different color.

Definition at line 116 of file regex_test_lib.h.

117 {
122 
128 
133 };
The generated graph will include extra information such as the NFA states that were used to generate ...
Enable graph coloring.

Function Documentation

◆ REGEX_TEST_combine()

char* REGEX_TEST_combine ( char *const  regexes[],
unsigned int  alphabet_size 
)

Combine an array of regexes into a single prefix-shared regex.

Returns a prefix-combine regex that matches the same strings as any of the original regexes.

WARNING: only useful for reading specific regexes for specific applications, namely the gnunet-regex-profiler / gnunet-regex-daemon. This function DOES NOT support arbitrary regex combining.

Parameters
regexesA NULL-terminated array of regexes.
alphabet_sizeSize of the alphabet the regex uses.
Returns
A string with a single regex that matches any of the original regexes

Definition at line 536 of file regex_test_lib.c.

References ctx, debugctx(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, new_regex_ctx(), regex_add(), regex_combine(), and regex_ctx_destroy().

Referenced by main(), and run().

537 {
538  unsigned int i;
539  char *combined;
540  const char *current;
541  struct RegexCombineCtx *ctx;
542 
543  ctx = new_regex_ctx (alphabet_size);
544  for (i = 0; regexes[i]; i++)
545  {
546  current = regexes[i];
547  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex %u: %s\n", i, current);
548  regex_add (ctx, current);
549  debugctx (ctx, 0);
550  }
551  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\nCombining...\n");
552  debugctx (ctx, 0);
553 
554  combined = regex_combine (ctx);
555 
556  regex_ctx_destroy (ctx);
557 
558  return combined;
559 }
Struct to hold the tree formed by prefix-combining the regexes.
static struct GNUNET_DNSSTUB_Context * ctx
Context for DNS resolution.
static void debugctx(struct RegexCombineCtx *ctx, int level)
Printf the combined regex ctx.
static void regex_ctx_destroy(struct RegexCombineCtx *ctx)
Free all resources used by the context node and all its children.
static char * regex_combine(struct RegexCombineCtx *ctx)
Extract a string from all prefix-combined regexes.
#define GNUNET_log(kind,...)
static struct RegexCombineCtx * new_regex_ctx(unsigned int alphabet_size)
Create and initialize a new RegexCombineCtx.
static void regex_add(struct RegexCombineCtx *ctx, const char *regex)
Add a single regex to a context, combining with exisiting regex by-prefix.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ REGEX_TEST_read_from_file()

char** REGEX_TEST_read_from_file ( const char *  filename)

Read a set of regexes from a file, one per line and return them in an array suitable for REGEX_TEST_combine.

The array must be free'd using REGEX_TEST_free_from_file.

Parameters
filenameName of the file containing the regexes.
Returns
A newly allocated, NULL terminated array of regexes.

Definition at line 572 of file regex_test_lib.c.

References removetrailingwhitespace::f, GNUNET_array_grow, GNUNET_assert, GNUNET_DISK_file_close(), GNUNET_DISK_file_handle_size(), GNUNET_DISK_file_open(), GNUNET_DISK_file_read(), GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_free_non_null, GNUNET_log, GNUNET_malloc, GNUNET_OK, GNUNET_realloc, len, and RegexCombineCtx::size.

Referenced by main(), and run().

573 {
574  struct GNUNET_DISK_FileHandle *f;
575  unsigned int nr;
576  unsigned int offset;
577  off_t size;
578  size_t len;
579  char *buffer;
580  char *regex;
581  char **regexes;
582 
586  if (NULL == f)
587  {
589  "Can't open file %s for reading\n", filename);
590  return NULL;
591  }
592  if (GNUNET_OK != GNUNET_DISK_file_handle_size (f, &size))
593  {
595  "Can't get size of file %s\n", filename);
597  return NULL;
598  }
600  "using file %s, size %llu\n",
601  filename, (unsigned long long) size);
602 
603  buffer = GNUNET_malloc (size + 1);
604  GNUNET_DISK_file_read (f, buffer, size);
606  regexes = GNUNET_malloc (sizeof (char *));
607  nr = 1;
608  offset = 0;
609  regex = NULL;
610  do
611  {
612  if (NULL == regex)
613  regex = GNUNET_malloc (size + 1);
614  len = (size_t) sscanf (&buffer[offset], "%s", regex);
615  if (0 == len)
616  break;
617  len = strlen (regex);
618  offset += len + 1;
619  if (len < 1)
620  continue;
621  regex[len] = '\0';
622  regex = GNUNET_realloc (regex, len + 1);
623  GNUNET_array_grow (regexes, nr, nr + 1);
624  GNUNET_assert (NULL == regexes[nr - 2]);
625  regexes[nr - 2] = regex;
626  regexes[nr - 1] = NULL;
627  regex = NULL;
628  } while (offset < size);
629  GNUNET_free_non_null (regex);
630  GNUNET_free (buffer);
631 
632  return regexes;
633 }
Open the file for reading.
int GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1817
ssize_t GNUNET_DISK_file_read(const struct GNUNET_DISK_FileHandle *h, void *result, size_t len)
Read the contents of a binary file into a buffer.
Definition: disk.c:881
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
Nobody is allowed to do anything to the file.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#define GNUNET_free_non_null(ptr)
Free the memory pointed to by ptr if ptr is not NULL.
#define GNUNET_array_grow(arr, size, tsize)
Grow a well-typed (!) array.
#define GNUNET_realloc(ptr, size)
Wrapper around realloc.
static char * filename
static unsigned int size
Size of the "table".
Definition: peer.c:67
int GNUNET_DISK_file_handle_size(struct GNUNET_DISK_FileHandle *fh, off_t *size)
Get the size of an open file.
Definition: disk.c:208
#define GNUNET_log(kind,...)
struct GNUNET_DISK_FileHandle * GNUNET_DISK_file_open(const char *fn, enum GNUNET_DISK_OpenFlags flags, enum GNUNET_DISK_AccessPermissions perm)
Open a file.
Definition: disk.c:1673
Handle used to access files (and pipes).
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
Here is the call graph for this function:
Here is the caller graph for this function:

◆ REGEX_TEST_free_from_file()

void REGEX_TEST_free_from_file ( char **  regexes)

Free all memory reserved for a set of regexes created by read_from_file.

Parameters
regexesNULL-terminated array of regexes.

Definition at line 642 of file regex_test_lib.c.

References GNUNET_free.

Referenced by main(), and run().

643 {
644  unsigned int i;
645 
646  for (i = 0; regexes[i]; i++)
647  GNUNET_free (regexes[i]);
648  GNUNET_free (regexes);
649 }
#define GNUNET_free(ptr)
Wrapper around free.
Here is the caller graph for this function:

◆ REGEX_TEST_generate_random_regex()

char* REGEX_TEST_generate_random_regex ( size_t  rx_length,
char *  matching_str 
)

Generate a (pseudo) random regular expression of length 'rx_length', as well as a (optional) string that will be matched by the generated regex.

The returned regex needs to be freed.

Parameters
rx_lengthlength of the random regex.
matching_str(optional) pointer to a string that will contain a string that will be matched by the generated regex, if 'matching_str' pointer was not NULL.
Returns
NULL if 'rx_length' is 0, a random regex of length 'rx_length', which needs to be freed, otherwise.

The returned regex needs to be freed.

Parameters
rx_lengthlength of the random regex.
matching_str(optional) pointer to a string that will contain a string that will be matched by the generated regex, if 'matching_str' pointer was not NULL. Make sure you allocated at least rx_length+1 bytes for this sting.
Returns
NULL if 'rx_length' is 0, a random regex of length 'rx_length', which needs to be freed, otherwise.

Definition at line 64 of file regex_test_random.c.

References get_random_literal(), GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_CRYPTO_random_u32(), and GNUNET_malloc.

65 {
66  char *rx;
67  char *rx_p;
68  char *matching_strp;
69  unsigned int i;
70  unsigned int char_op_switch;
71  unsigned int last_was_op;
72  int rx_op;
73  char current_char;
74 
75  if (0 == rx_length)
76  return NULL;
77 
78  if (NULL != matching_str)
79  matching_strp = matching_str;
80  else
81  matching_strp = NULL;
82 
83  rx = GNUNET_malloc (rx_length + 1);
84  rx_p = rx;
85  current_char = 0;
86  last_was_op = 1;
87 
88  for (i = 0; i < rx_length; i++)
89  {
91 
92  if (0 == char_op_switch && !last_was_op)
93  {
94  last_was_op = 1;
96 
97  switch (rx_op)
98  {
99  case 0:
100  current_char = '+';
101  break;
102  case 1:
103  current_char = '*';
104  break;
105  case 2:
106  current_char = '?';
107  break;
108  case 3:
109  if (i < rx_length - 1) /* '|' cannot be at the end */
110  current_char = '|';
111  else
112  current_char = get_random_literal ();
113  break;
114  }
115  }
116  else
117  {
118  current_char = get_random_literal ();
119  last_was_op = 0;
120  }
121 
122  if (NULL != matching_strp &&
123  (current_char != '+' && current_char != '*' && current_char != '?' &&
124  current_char != '|'))
125  {
126  *matching_strp = current_char;
127  matching_strp++;
128  }
129 
130  *rx_p = current_char;
131  rx_p++;
132  }
133  *rx_p = '\0';
134  if (NULL != matching_strp)
135  *matching_strp = '\0';
136 
137  return rx;
138 }
uint32_t GNUNET_CRYPTO_random_u32(enum GNUNET_CRYPTO_Quality mode, uint32_t i)
Produce a random value.
static char get_random_literal()
Get a (pseudo) random valid literal for building a regular expression.
No good quality of the operation is needed (i.e., random numbers can be pseudo-random).
#define GNUNET_malloc(size)
Wrapper around malloc.
Here is the call graph for this function:

◆ REGEX_TEST_generate_random_string()

char* REGEX_TEST_generate_random_string ( size_t  max_len)

Generate a random string of maximum length 'max_len' that only contains literals allowed in a regular expression.

The string might be 0 chars long but is garantueed to be shorter or equal to 'max_len'.

Parameters
max_lenmaximum length of the string that should be generated.
Returns
random string that needs to be freed.

Definition at line 151 of file regex_test_random.c.

References get_random_literal(), GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_CRYPTO_random_u32(), GNUNET_malloc, GNUNET_strdup, and len.

152 {
153  unsigned int i;
154  char *str;
155  size_t len;
156 
157  if (1 > max_len)
158  return GNUNET_strdup ("");
159 
160  len = (size_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max_len);
161  str = GNUNET_malloc (len + 1);
162 
163  for (i = 0; i < len; i++)
164  {
165  str[i] = get_random_literal ();
166  }
167 
168  str[i] = '\0';
169 
170  return str;
171 }
uint32_t GNUNET_CRYPTO_random_u32(enum GNUNET_CRYPTO_Quality mode, uint32_t i)
Produce a random value.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
static char get_random_literal()
Get a (pseudo) random valid literal for building a regular expression.
No good quality of the operation is needed (i.e., random numbers can be pseudo-random).
#define GNUNET_malloc(size)
Wrapper around malloc.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
Here is the call graph for this function:

◆ REGEX_TEST_automaton_save_graph()

void REGEX_TEST_automaton_save_graph ( struct REGEX_INTERNAL_Automaton a,
const char *  filename,
enum REGEX_TEST_GraphSavingOptions  options 
)

Save the given automaton as a GraphViz dot file.

Parameters
athe automaton to be saved.
filenamewhere to save the file.
optionsoptions for graph generation that include coloring or verbose mode

Definition at line 271 of file regex_test_graph.c.

References REGEX_TEST_Graph_Context::coloring, end, REGEX_TEST_Graph_Context::filep, GNUNET_ERROR_TYPE_ERROR, GNUNET_log, GNUNET_NO, GNUNET_YES, REGEX_INTERNAL_automaton_traverse(), REGEX_TEST_automaton_save_graph_step(), REGEX_TEST_GRAPH_COLORING, REGEX_TEST_GRAPH_VERBOSE, scc_tarjan(), start, REGEX_INTERNAL_Automaton::start, and REGEX_TEST_Graph_Context::verbose.

274 {
275  char *start;
276  char *end;
278 
279  if (NULL == a)
280  {
281  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print NFA, was NULL!");
282  return;
283  }
284 
285  if (NULL == filename || strlen (filename) < 1)
286  {
287  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No Filename given!");
288  return;
289  }
290 
291  ctx.filep = fopen (filename, "w");
292  ctx.verbose =
294  ctx.coloring =
296 
297  if (NULL == ctx.filep)
298  {
299  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not open file for writing: %s",
300  filename);
301  return;
302  }
303 
304  /* First add the SCCs to the automaton, so we can color them nicely */
305  if (GNUNET_YES == ctx.coloring)
306  scc_tarjan (a);
307 
308  start = "digraph G {\nrankdir=LR\n";
309  fwrite (start, strlen (start), 1, ctx.filep);
310 
311  REGEX_INTERNAL_automaton_traverse (a, a->start, NULL, NULL,
313  &ctx);
314 
315  end = "\n}\n";
316  fwrite (end, strlen (end), 1, ctx.filep);
317  fclose (ctx.filep);
318 }
static int end
Set if we are to shutdown all services (including ARM).
Definition: gnunet-arm.c:34
Context for graph creation.
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
The generated graph will include extra information such as the NFA states that were used to generate ...
static int start
Set if we are to start default services (including ARM).
Definition: gnunet-arm.c:39
#define GNUNET_NO
Definition: gnunet_common.h:81
static struct GNUNET_DNSSTUB_Context * ctx
Context for DNS resolution.
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&#39;s start state, visiting all reachable states and calling &#39;action&#39; on each one of them.
static char * filename
Enable graph coloring.
struct REGEX_INTERNAL_State * start
First state of the automaton.
void REGEX_TEST_automaton_save_graph_step(void *cls, unsigned int count, struct REGEX_INTERNAL_State *s)
Save a state to an open file pointer.
static void scc_tarjan(struct REGEX_INTERNAL_Automaton *a)
Detect all SCCs (Strongly Connected Components) inside the given automaton.
#define GNUNET_log(kind,...)
#define GNUNET_YES
Definition: gnunet_common.h:80
Here is the call graph for this function: