GNUnet  0.11.x
Data Structures | Functions
regex_test_lib.c File Reference

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

#include "platform.h"
#include "gnunet_util_lib.h"
Include dependency graph for regex_test_lib.c:

Go to the source code of this file.

Data Structures

struct  RegexCombineCtx
 Struct to hold the tree formed by prefix-combining the regexes. More...
 

Functions

static int c2i (char c, int size)
 Char 2 int. More...
 
static void debugctx (struct RegexCombineCtx *ctx, int level)
 Printf the combined regex ctx. More...
 
static void regex_add (struct RegexCombineCtx *ctx, const char *regex)
 Add a single regex to a context, combining with existing regex by-prefix. More...
 
static struct RegexCombineCtxnew_regex_ctx (unsigned int alphabet_size)
 Create and initialize a new RegexCombineCtx. More...
 
static void move_children (struct RegexCombineCtx *dst, const struct RegexCombineCtx *src)
 
static char * regex_combine (struct RegexCombineCtx *ctx)
 Extract a string from all prefix-combined regexes. More...
 
static unsigned int get_prefix_length (const char *s1, const char *s2)
 Get the number of matching characters on the prefix of both strings. More...
 
static struct RegexCombineCtxget_longest_prefix (struct RegexCombineCtx *ctx, const char *regex)
 Return the child context with the longest prefix match with the regex. More...
 
static void regex_add_multiple (struct RegexCombineCtx *ctx, const char *regex, struct RegexCombineCtx **children)
 
static void regex_split (struct RegexCombineCtx *ctx, unsigned int len, unsigned int prefix_l)
 Add a single regex to a context, splitting the existing state. More...
 
static void regex_ctx_destroy (struct RegexCombineCtx *ctx)
 Free all resources used by the context node and all its children. More...
 
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...
 

Detailed Description

library to read regexes representing IP networks from a file.

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

Author
Bartlomiej Polot

Definition in file regex_test_lib.c.

Function Documentation

◆ c2i()

static int c2i ( char  c,
int  size 
)
static

Char 2 int.

Convert a character into its int value depending on the base used

Parameters
cChar
sizebase (2, 8 or 16(hex))
Returns
Int in range [0, (base-1)]

Definition at line 65 of file regex_test_lib.c.

References GNUNET_assert, GNUNET_ERROR_TYPE_ERROR, and GNUNET_log.

Referenced by debugctx(), regex_add(), regex_add_multiple(), and regex_split().

66 {
67  switch (size)
68  {
69  case 2:
70  case 8:
71  return c - '0';
72  break;
73 
74  case 16:
75  if ((c >= '0') && (c <= '9') )
76  return c - '0';
77  else if ((c >= 'A') && (c <= 'F') )
78  return c - 'A' + 10;
79  else if ((c >= 'a') && (c <= 'f') )
80  return c - 'a' + 10;
81  else
82  {
84  "Cannot convert char %c in base %u\n",
85  c, size);
86  GNUNET_assert (0);
87  }
88  break;
89 
90  default:
91  GNUNET_assert (0);
92  }
93 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static unsigned int size
Size of the "table".
Definition: peer.c:67
#define GNUNET_log(kind,...)
Here is the caller graph for this function:

◆ debugctx()

static void debugctx ( struct RegexCombineCtx ctx,
int  level 
)
static

Printf the combined regex ctx.

Parameters
ctxThe ctx to printf
levelIndentation level to start with

Definition at line 120 of file regex_test_lib.c.

References c2i(), RegexCombineCtx::children, ctx, regex_add(), RegexCombineCtx::s, and RegexCombineCtx::size.

Referenced by REGEX_TEST_combine().

121 {
122 #if DEBUG_REGEX
123  if (NULL != ctx->s)
124  {
125  space (level - 1);
126  fprintf (stderr, "%u:'%s'\n", c2i (ctx->s[0], ctx->size), ctx->s);
127  }
128  else
129  fprintf (stderr, "ROOT (base %u)\n", ctx->size);
130  for (unsigned int i = 0; i < ctx->size; i++)
131  {
132  if (NULL != ctx->children[i])
133  {
134  space (level);
135  debugctx (ctx->children[i], level + 1);
136  }
137  }
138  fflush (stderr);
139 #endif
140 }
char * s
Token.
struct RegexCombineCtx ** children
Child nodes with same prefix and token.
unsigned int size
Alphabet size (how many children there are)
static void debugctx(struct RegexCombineCtx *ctx, int level)
Printf the combined regex ctx.
static int c2i(char c, int size)
Char 2 int.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ regex_add()

static void regex_add ( struct RegexCombineCtx ctx,
const char *  regex 
)
static

Add a single regex to a context, combining with existing regex by-prefix.

Parameters
ctxContext with 0 or more regexes.
regexRegex to add.

Definition at line 444 of file regex_test_lib.c.

References c2i(), RegexCombineCtx::children, get_longest_prefix(), get_prefix_length(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, GNUNET_strdup, l, len, new_regex_ctx(), p, regex_add_multiple(), regex_split(), RegexCombineCtx::s, and RegexCombineCtx::size.

Referenced by debugctx(), and REGEX_TEST_combine().

445 {
446  struct RegexCombineCtx *p;
447  struct RegexCombineCtx *newctx;
448  long unsigned int l;
449  unsigned int prefix_l;
450  const char *rest_r;
451  const char *rest_s;
452  size_t len;
453  int idx;
454 
456  "regex_add '%s' into '%s'\n",
457  regex, ctx->s);
458  l = strlen (regex);
459  if (0UL == l)
460  return;
461 
462  /* If the regex is in the form of (a|b|c), add every character separately */
463  if ('(' == regex[0])
464  {
465  regex_add_multiple (ctx, regex, NULL);
466  return;
467  }
468 
469  p = get_longest_prefix (ctx, regex);
470  if (NULL != p)
471  {
472  /* There is some prefix match, reduce regex and try again */
473  prefix_l = get_prefix_length (p->s, regex);
474  rest_s = &p->s[prefix_l];
475  rest_r = &regex[prefix_l];
476  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "chosen '%s' [%u]\n", p->s, prefix_l);
477  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "prefix r '%.*s'\n", prefix_l, p->s);
478  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "rest r '%s'\n", rest_r);
479  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "rest s '%s'\n", rest_s);
480  len = strlen (p->s);
481  if (prefix_l < len)
482  {
483  regex_split (p, len, prefix_l);
484  }
485  regex_add (p, rest_r);
486  return;
487  }
488 
489  /* There is no prefix match, add new */
490  idx = c2i (regex[0], ctx->size);
491  if ((NULL == ctx->children[idx]) && (NULL != ctx->s))
492  {
493  /* this was the end before, add empty string */
494  newctx = new_regex_ctx (ctx->size);
495  newctx->s = GNUNET_strdup ("");
496  ctx->children[idx] = newctx;
497  }
498  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no match\n");
499  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " new state %s\n", regex);
500  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " under %s\n", ctx->s);
501  newctx = new_regex_ctx (ctx->size);
502  newctx->s = GNUNET_strdup (regex);
503  ctx->children[idx] = newctx;
504 }
char * s
Token.
struct RegexCombineCtx ** children
Child nodes with same prefix and token.
Struct to hold the tree formed by prefix-combining the regexes.
static struct RegexCombineCtx * get_longest_prefix(struct RegexCombineCtx *ctx, const char *regex)
Return the child context with the longest prefix match with the regex.
static void regex_add_multiple(struct RegexCombineCtx *ctx, const char *regex, struct RegexCombineCtx **children)
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
unsigned int size
Alphabet size (how many children there are)
static struct LoggingHandle * l
static struct GNUNET_OS_Process * p
Helper process we started.
Definition: gnunet-qr.c:59
static int c2i(char c, int size)
Char 2 int.
static void regex_split(struct RegexCombineCtx *ctx, unsigned int len, unsigned int prefix_l)
Add a single regex to a context, splitting the existing state.
#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 existing regex by-prefix.
static unsigned int get_prefix_length(const char *s1, const char *s2)
Get the number of matching characters on the prefix of both strings.
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:

◆ new_regex_ctx()

static struct RegexCombineCtx* new_regex_ctx ( unsigned int  alphabet_size)
static

Create and initialize a new RegexCombineCtx.

Parameters
alphabet_sizeSize of the alphabet (and the Trie array)

Definition at line 160 of file regex_test_lib.c.

References RegexCombineCtx::children, ctx, GNUNET_malloc, GNUNET_new, and RegexCombineCtx::size.

Referenced by regex_add(), regex_add_multiple(), regex_split(), and REGEX_TEST_combine().

161 {
162  struct RegexCombineCtx *ctx;
163  size_t array_size;
164 
165  array_size = sizeof(struct RegexCombineCtx *) * alphabet_size;
166  ctx = GNUNET_new (struct RegexCombineCtx);
167  ctx->children = GNUNET_malloc (array_size);
168  ctx->size = alphabet_size;
169 
170  return ctx;
171 }
struct RegexCombineCtx ** children
Child nodes with same prefix and token.
Struct to hold the tree formed by prefix-combining the regexes.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
unsigned int size
Alphabet size (how many children there are)
static struct GNUNET_DNSSTUB_Context * ctx
Context for DNS resolution.
#define GNUNET_malloc(size)
Wrapper around malloc.
Here is the caller graph for this function:

◆ move_children()

static void move_children ( struct RegexCombineCtx dst,
const struct RegexCombineCtx src 
)
static

Definition at line 175 of file regex_test_lib.c.

References RegexCombineCtx::children, GNUNET_memcpy, and RegexCombineCtx::size.

Referenced by regex_split().

177 {
178  size_t array_size;
179 
180  array_size = sizeof(struct RegexCombineCtx *) * src->size;
181  GNUNET_memcpy (dst->children,
182  src->children,
183  array_size);
184  for (unsigned int i = 0; i < src->size; i++)
185  {
186  src->children[i] = NULL;
187  }
188 }
struct RegexCombineCtx ** children
Child nodes with same prefix and token.
Struct to hold the tree formed by prefix-combining the regexes.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
unsigned int size
Alphabet size (how many children there are)
Here is the caller graph for this function:

◆ regex_combine()

static char* regex_combine ( struct RegexCombineCtx ctx)
static

Extract a string from all prefix-combined regexes.

Parameters
ctxContext with 0 or more regexes.
Returns
Regex that matches any of the added regexes.

Definition at line 199 of file regex_test_lib.c.

References RegexCombineCtx::children, GNUNET_asprintf(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_log, GNUNET_NO, GNUNET_strdup, GNUNET_YES, len, p, RegexCombineCtx::s, and RegexCombineCtx::size.

Referenced by REGEX_TEST_combine().

200 {
201  struct RegexCombineCtx *p;
202  unsigned int i;
203  size_t len;
204  char *regex;
205  char *tmp;
206  char *s;
207  int opt;
208 
209  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new combine %s\n", ctx->s);
210  regex = GNUNET_strdup ("");
211  opt = GNUNET_NO;
212  for (i = 0; i < ctx->size; i++)
213  {
214  p = ctx->children[i];
215  if (NULL == p)
216  continue;
218  "adding '%s' to innner %s\n",
219  p->s, ctx->s);
220  s = regex_combine (p);
221  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " total '%s'\n", s);
222  if (strlen (s) == 0)
223  {
224  opt = GNUNET_YES;
225  }
226  else
227  {
228  GNUNET_asprintf (&tmp, "%s%s|", regex, s);
229  GNUNET_free (regex);
230  regex = tmp;
231  }
232  GNUNET_free (s);
233  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " so far '%s' for inner %s\n", regex,
234  ctx->s);
235  }
236 
237  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "opt: %d, innner: '%s'\n", opt, regex);
238  len = strlen (regex);
239  if (0 == len)
240  {
241  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "empty, returning ''\n");
242  GNUNET_free (regex);
243  return NULL == ctx->s ? NULL : GNUNET_strdup (ctx->s);
244  }
245 
246  if ('|' == regex[len - 1])
247  regex[len - 1] = '\0';
248 
249  if (NULL != ctx->s)
250  {
251  if (opt)
252  GNUNET_asprintf (&s, "%s(%s)?", ctx->s, regex);
253  else
254  GNUNET_asprintf (&s, "%s(%s)", ctx->s, regex);
255  GNUNET_free (regex);
256  regex = s;
257  }
258 
259  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "partial: %s\n", regex);
260  return regex;
261 }
char * s
Token.
struct RegexCombineCtx ** children
Child nodes with same prefix and token.
Struct to hold the tree formed by prefix-combining the regexes.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
unsigned int size
Alphabet size (how many children there are)
static struct GNUNET_OS_Process * p
Helper process we started.
Definition: gnunet-qr.c:59
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
static char * regex_combine(struct RegexCombineCtx *ctx)
Extract a string from all prefix-combined regexes.
#define GNUNET_log(kind,...)
#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:

◆ get_prefix_length()

static unsigned int get_prefix_length ( const char *  s1,
const char *  s2 
)
static

Get the number of matching characters on the prefix of both strings.

Parameters
s1String 1.
s2String 2.
Returns
Number of characters of matching prefix.

Definition at line 273 of file regex_test_lib.c.

Referenced by get_longest_prefix(), and regex_add().

274 {
275  unsigned int l1;
276  unsigned int l2;
277  unsigned int limit;
278  unsigned int i;
279 
280  l1 = strlen (s1);
281  l2 = strlen (s2);
282  limit = l1 > l2 ? l2 : l1;
283 
284  for (i = 0; i < limit; i++)
285  {
286  if (s1[i] != s2[i])
287  return i;
288  }
289  return limit;
290 }
Here is the caller graph for this function:

◆ get_longest_prefix()

static struct RegexCombineCtx* get_longest_prefix ( struct RegexCombineCtx ctx,
const char *  regex 
)
static

Return the child context with the longest prefix match with the regex.

Usually only one child will match, search all just in case.

Parameters
ctxContext whose children to search.
regexString to match.
Returns
Child with the longest prefix, NULL if no child matches.

Definition at line 303 of file regex_test_lib.c.

References RegexCombineCtx::children, get_prefix_length(), GNUNET_break, l, p, RegexCombineCtx::s, and RegexCombineCtx::size.

Referenced by regex_add().

304 {
305  struct RegexCombineCtx *p;
306  struct RegexCombineCtx *best;
307  unsigned int i;
308  unsigned int l;
309  unsigned int best_l;
310 
311  best_l = 0;
312  best = NULL;
313 
314  for (i = 0; i < ctx->size; i++)
315  {
316  p = ctx->children[i];
317  if (NULL == p)
318  continue;
319 
320  l = get_prefix_length (p->s, regex);
321  if (l > best_l)
322  {
323  GNUNET_break (0 == best_l);
324  best = p;
325  best_l = l;
326  }
327  }
328  return best;
329 }
char * s
Token.
struct RegexCombineCtx ** children
Child nodes with same prefix and token.
Struct to hold the tree formed by prefix-combining the regexes.
unsigned int size
Alphabet size (how many children there are)
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
static struct LoggingHandle * l
static struct GNUNET_OS_Process * p
Helper process we started.
Definition: gnunet-qr.c:59
static unsigned int get_prefix_length(const char *s1, const char *s2)
Get the number of matching characters on the prefix of both strings.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ regex_add_multiple()

static void regex_add_multiple ( struct RegexCombineCtx ctx,
const char *  regex,
struct RegexCombineCtx **  children 
)
static

Definition at line 333 of file regex_test_lib.c.

References c2i(), RegexCombineCtx::children, GNUNET_assert, GNUNET_memcpy, GNUNET_strdup, l, new_regex_ctx(), RegexCombineCtx::s, and RegexCombineCtx::size.

Referenced by regex_add(), and regex_split().

336 {
337  char tmp[2];
338  long unsigned int i;
339  size_t l;
340  struct RegexCombineCtx *newctx;
341  unsigned int count;
342 
343  if ('(' != regex[0])
344  {
345  GNUNET_assert (0);
346  }
347 
348  /* Does the regex cover *all* possible children? Then don't add any,
349  * as it will be covered by the post-regex "(a-z)*"
350  */
351  l = strlen (regex);
352  count = 0;
353  for (i = 1UL; i < l; i++)
354  {
355  if ((regex[i] != '|') && (regex[i] != ')') )
356  {
357  count++;
358  }
359  }
360  if (count == ctx->size)
361  {
362  return;
363  }
364 
365  /* Add every component as a child node */
366  tmp[1] = '\0';
367  for (i = 1UL; i < l; i++)
368  {
369  if ((regex[i] != '|') && (regex[i] != ')') )
370  {
371  tmp[0] = regex[i];
372  newctx = new_regex_ctx (ctx->size);
373  newctx->s = GNUNET_strdup (tmp);
374  if (children != NULL)
375  GNUNET_memcpy (newctx->children,
376  children,
377  sizeof(*children) * ctx->size);
378  ctx->children[c2i (tmp[0], ctx->size)] = newctx;
379  }
380  }
381 }
char * s
Token.
struct RegexCombineCtx ** children
Child nodes with same prefix and token.
Struct to hold the tree formed by prefix-combining the regexes.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
unsigned int size
Alphabet size (how many children there are)
static struct LoggingHandle * l
static int c2i(char c, int size)
Char 2 int.
static struct RegexCombineCtx * new_regex_ctx(unsigned int alphabet_size)
Create and initialize a new RegexCombineCtx.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ regex_split()

static void regex_split ( struct RegexCombineCtx ctx,
unsigned int  len,
unsigned int  prefix_l 
)
static

Add a single regex to a context, splitting the existing state.

We only had a partial match, split existing state, truncate the current node so it only contains the prefix, add suffix(es) as children.

Parameters
ctxContext to split.
lenLength of ctx->s
prefix_lLength of common prefix of the new regex and ctx->s

Definition at line 395 of file regex_test_lib.c.

References c2i(), RegexCombineCtx::children, GNUNET_free, GNUNET_malloc, GNUNET_strlcpy(), move_children(), new_regex_ctx(), regex_add_multiple(), RegexCombineCtx::s, and RegexCombineCtx::size.

Referenced by regex_add().

398 {
399  struct RegexCombineCtx *newctx;
400  unsigned int idx;
401  char *suffix;
402 
403  suffix = GNUNET_malloc (len - prefix_l + 1);
404  /*
405  * We can use GNUNET_strlcpy because ctx->s is null-terminated
406  */
407  GNUNET_strlcpy (suffix, &ctx->s[prefix_l], len - prefix_l + 1);
408 
409  /* Suffix saved, truncate current node so it only contains the prefix,
410  * copy any children nodes to put as grandchildren and initialize new empty
411  * children array.
412  */
413  ctx->s[prefix_l] = '\0';
414 
415  /* If the suffix is an OR expression, add multiple children */
416  if ('(' == suffix[0])
417  {
418  struct RegexCombineCtx **tmp;
419 
420  tmp = ctx->children;
421  ctx->children = GNUNET_malloc (sizeof(*tmp) * ctx->size);
422  regex_add_multiple (ctx, suffix, tmp);
423  GNUNET_free (suffix);
424  GNUNET_free (tmp);
425  return;
426  }
427 
428  /* The suffix is a normal string, add as one node */
429  newctx = new_regex_ctx (ctx->size);
430  newctx->s = suffix;
431  move_children (newctx, ctx);
432  idx = c2i (suffix[0], ctx->size);
433  ctx->children[idx] = newctx;
434 }
char * s
Token.
struct RegexCombineCtx ** children
Child nodes with same prefix and token.
Struct to hold the tree formed by prefix-combining the regexes.
static void regex_add_multiple(struct RegexCombineCtx *ctx, const char *regex, struct RegexCombineCtx **children)
unsigned int size
Alphabet size (how many children there are)
static void move_children(struct RegexCombineCtx *dst, const struct RegexCombineCtx *src)
static int c2i(char c, int size)
Char 2 int.
static struct RegexCombineCtx * new_regex_ctx(unsigned int alphabet_size)
Create and initialize a new RegexCombineCtx.
size_t GNUNET_strlcpy(char *dst, const char *src, size_t n)
Like strlcpy but portable.
Definition: strings.c:221
#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_ctx_destroy()

static void regex_ctx_destroy ( struct RegexCombineCtx ctx)
static

Free all resources used by the context node and all its children.

Parameters
ctxContext to free.

Definition at line 513 of file regex_test_lib.c.

References RegexCombineCtx::children, GNUNET_free, RegexCombineCtx::s, and RegexCombineCtx::size.

Referenced by REGEX_TEST_combine().

514 {
515  unsigned int i;
516 
517  if (NULL == ctx)
518  return;
519 
520  for (i = 0; i < ctx->size; i++)
521  {
522  regex_ctx_destroy (ctx->children[i]);
523  }
524  GNUNET_free (ctx->s); /* 's' on root node is null */
525  GNUNET_free (ctx->children);
526  GNUNET_free (ctx);
527 }
char * s
Token.
struct RegexCombineCtx ** children
Child nodes with same prefix and token.
unsigned int size
Alphabet size (how many children there are)
static void regex_ctx_destroy(struct RegexCombineCtx *ctx)
Free all resources used by the context node and all its children.
#define GNUNET_free(ptr)
Wrapper around free.
Here is the caller graph for this function:

◆ 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 545 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().

546 {
547  unsigned int i;
548  char *combined;
549  const char *current;
550  struct RegexCombineCtx *ctx;
551 
552  ctx = new_regex_ctx (alphabet_size);
553  for (i = 0; regexes[i]; i++)
554  {
555  current = regexes[i];
556  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex %u: %s\n", i, current);
557  regex_add (ctx, current);
558  debugctx (ctx, 0);
559  }
560  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\nCombining...\n");
561  debugctx (ctx, 0);
562 
563  combined = regex_combine (ctx);
564 
565  regex_ctx_destroy (ctx);
566 
567  return combined;
568 }
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 existing 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 581 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_log, GNUNET_malloc, GNUNET_OK, GNUNET_realloc, len, and RegexCombineCtx::size.

Referenced by main(), and run().

582 {
583  struct GNUNET_DISK_FileHandle *f;
584  unsigned int nr;
585  unsigned int offset;
586  off_t size;
587  size_t len;
588  char *buffer;
589  char *regex;
590  char **regexes;
591 
595  if (NULL == f)
596  {
598  "Can't open file %s for reading\n", filename);
599  return NULL;
600  }
601  if (GNUNET_OK != GNUNET_DISK_file_handle_size (f, &size))
602  {
604  "Can't get size of file %s\n", filename);
606  return NULL;
607  }
609  "using file %s, size %llu\n",
610  filename, (unsigned long long) size);
611 
612  buffer = GNUNET_malloc (size + 1);
613  GNUNET_DISK_file_read (f, buffer, size);
615  regexes = GNUNET_malloc (sizeof(char *));
616  nr = 1;
617  offset = 0;
618  regex = NULL;
619  do
620  {
621  if (NULL == regex)
622  regex = GNUNET_malloc (size + 1);
623  len = (size_t) sscanf (&buffer[offset], "%s", regex);
624  if (0 == len)
625  break;
626  len = strlen (regex);
627  offset += len + 1;
628  if (len < 1)
629  continue;
630  regex[len] = '\0';
631  regex = GNUNET_realloc (regex, len + 1);
632  GNUNET_array_grow (regexes, nr, nr + 1);
633  GNUNET_assert (NULL == regexes[nr - 2]);
634  regexes[nr - 2] = regex;
635  regexes[nr - 1] = NULL;
636  regex = NULL;
637  }
638  while (offset < size);
639  GNUNET_free (regex);
640  GNUNET_free (buffer);
641 
642  return regexes;
643 }
enum GNUNET_GenericReturnValue GNUNET_DISK_file_handle_size(struct GNUNET_DISK_FileHandle *fh, off_t *size)
Get the size of an open file.
Definition: disk.c:193
Open the file for reading.
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:602
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
enum GNUNET_GenericReturnValue GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1126
Nobody is allowed to do anything to the file.
#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
#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:1055
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 652 of file regex_test_lib.c.

References GNUNET_free.

Referenced by main(), and run().

653 {
654  unsigned int i;
655 
656  for (i = 0; regexes[i]; i++)
657  GNUNET_free (regexes[i]);
658  GNUNET_free (regexes);
659 }
#define GNUNET_free(ptr)
Wrapper around free.
Here is the caller graph for this function: