GNUnet  0.10.x
regex_test_random.c
Go to the documentation of this file.
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  SPDX-License-Identifier: AGPL3.0-or-later
19 */
25 #include "platform.h"
26 #include "regex_test_lib.h"
27 #include "gnunet_crypto_lib.h"
28 #include "regex_internal.h"
29 
30 
36 static char
38 {
39  uint32_t ridx;
40 
41  ridx =
43  (uint32_t) strlen (ALLOWED_LITERALS));
44 
45  return ALLOWED_LITERALS[ridx];
46 }
47 
48 
63 char *
64 REGEX_TEST_generate_random_regex (size_t rx_length, char *matching_str)
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 }
139 
140 
150 char *
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 ALLOWED_LITERALS
char array of literals that are allowed inside a regex (apart from the operators) ...
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
cryptographic primitives for GNUnet
char * REGEX_TEST_generate_random_string(size_t max_len)
Generate a random string of maximum length &#39;max_len&#39; that only contains literals allowed in a regular...
char * REGEX_TEST_generate_random_regex(size_t rx_length, char *matching_str)
Generate a (pseudo) random regular expression of length &#39;rx_length&#39;, as well as a (optional) string t...
library to read regexes representing IP networks from a file.
common internal definitions for regex library.
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...