Adaptive Framework  0.9.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
afw_function_random.c
Go to the documentation of this file.
1 // See the 'COPYING' file in the project root for licensing information.
2 /*
3  * afw_function_execute_* functions for random
4  *
5  * Copyright (c) 2010-2023 Clemson University
6  *
7  */
8 
14 #include "afw_internal.h"
15 
16 
17 
18 /*
19  * Adaptive function: random_base64Binary
20  *
21  * afw_function_execute_random_base64Binary
22  *
23  * See afw_function_bindings.h for more information.
24  *
25  * This returns a specified number of random octets as dataType base64Binary.
26  *
27  * This function is not pure, so it may return a different result
28  * given exactly the same parameters.
29  *
30  * Declaration:
31  *
32  * ```
33  * function random_base64Binary(
34  * numberOfOctets: integer
35  * ): base64Binary;
36  * ```
37  *
38  * Parameters:
39  *
40  * numberOfOctets - (integer) The number of random octets to generate.
41  *
42  * Returns:
43  *
44  * (base64Binary)
45  */
46 const afw_value_t *
49 {
50  const afw_value_integer_t *numberOfOctets;
52 
54  1, integer);
55 
56  if (numberOfOctets->internal < 0) {
57  AFW_THROW_ERROR_Z(general, "numberOfOctets must be greater than 0",
58  x->xctx);
59  }
60 
61 
62  result = afw_value_allocate_base64Binary(x->p, x->xctx);
63  result->internal.size = (afw_size_t)numberOfOctets->internal;
64  result->internal.ptr = afw_pool_malloc(x->p,
65  result->internal.size, x->xctx);
66  apr_generate_random_bytes((unsigned char *)result->internal.ptr,
67  result->internal.size);
68 
69  return (const afw_value_t *)result;
70 }
71 
72 
73 
74 /*
75  * Adaptive function: random_digits
76  *
77  * afw_function_execute_random_digits
78  *
79  * See afw_function_bindings.h for more information.
80  *
81  * Generate a string containing a specified number of random decimal digits.
82  *
83  * This function is not pure, so it may return a different result
84  * given exactly the same parameters.
85  *
86  * Declaration:
87  *
88  * ```
89  * function random_digits(
90  * numberOfDigits: integer
91  * ): string;
92  * ```
93  *
94  * Parameters:
95  *
96  * numberOfDigits - (integer) The number (1-18) of digits to generate.
97  *
98  * Returns:
99  *
100  * (string)
101  */
102 const afw_value_t *
105 {
106  const afw_value_integer_t *numberOfDigits;
107  afw_value_string_t *result;
108  afw_uint64_t n;
109  int count;
110  char *c;
111 
113  1, integer);
114 
115  if (numberOfDigits->internal < 1 || numberOfDigits->internal > 18) {
116  AFW_THROW_ERROR_Z(general, "numberOfDigits can be 1-18", x->xctx);
117  }
118 
119 
120  result = afw_value_allocate_string(x->p, x->xctx);
121  result->internal.len = (afw_size_t)numberOfDigits->internal;
122  result->internal.s = afw_pool_malloc(x->p, result->internal.len, x->xctx);
123  apr_generate_random_bytes((unsigned char *)&n, sizeof(n));
124 
125  for (count = (int)numberOfDigits->internal,
126  c = (char *)result->internal.s;
127  count > 0;
128  count--, c++)
129  {
130  *c = (n % 10) + '0';
131  n /= 10;
132  }
133 
134  return (const afw_value_t *)result;
135 }
136 
137 
138 
139 /*
140  * Adaptive function: random_hexBinary
141  *
142  * afw_function_execute_random_hexBinary
143  *
144  * See afw_function_bindings.h for more information.
145  *
146  * This returns a specified number of random octets as dataType hexBinary.
147  *
148  * This function is not pure, so it may return a different result
149  * given exactly the same parameters.
150  *
151  * Declaration:
152  *
153  * ```
154  * function random_hexBinary(
155  * numberOfOctets: integer
156  * ): hexBinary;
157  * ```
158  *
159  * Parameters:
160  *
161  * numberOfOctets - (integer) The number of random octets to generate.
162  *
163  * Returns:
164  *
165  * (hexBinary)
166  */
167 const afw_value_t *
170 {
171  const afw_value_integer_t *numberOfOctets;
172  afw_value_hexBinary_t *result;
173 
175  1, integer);
176 
177  if (numberOfOctets->internal < 0) {
178  AFW_THROW_ERROR_Z(general, "numberOfOctets must be greater than 0",
179  x->xctx);
180  }
181 
182 
183  result = afw_value_allocate_hexBinary(x->p, x->xctx);
184  result->internal.size = (afw_size_t)numberOfOctets->internal;
185  result->internal.ptr = afw_pool_malloc(x->p,
186  result->internal.size, x->xctx);
187  apr_generate_random_bytes((unsigned char *)result->internal.ptr,
188  result->internal.size);
189 
190  return (const afw_value_t *)result;
191 }
192 
193 
194 
195 /*
196  * Adaptive function: random_integer
197  *
198  * afw_function_execute_random_integer
199  *
200  * See afw_function_bindings.h for more information.
201  *
202  * This returns a random integer between specified values inclusive.
203  *
204  * This function is not pure, so it may return a different result
205  * given exactly the same parameters.
206  *
207  * Declaration:
208  *
209  * ```
210  * function random_integer(
211  * min: integer,
212  * max: integer
213  * ): integer;
214  * ```
215  *
216  * Parameters:
217  *
218  * min - (integer) Minimum integer inclusive.
219  *
220  * max - (integer) Maximum integer inclusive.
221  *
222  * Returns:
223  *
224  * (integer) A random integer.
225  */
226 const afw_value_t *
229 {
230  const afw_value_integer_t *min;
231  const afw_value_integer_t *max;
232  afw_integer_t range;
233  afw_value_integer_t *result;
234 
237 
238  range = max->internal - min->internal + 1;
239  if (range <= 0) {
240  AFW_THROW_ERROR_Z(general,
241  "range between min and max is out of bounds",
242  x->xctx);
243  }
244 
245  result = afw_value_allocate_integer(x->p, x->xctx);
246  apr_generate_random_bytes((unsigned char*)&result->internal,
247  sizeof(afw_integer_t));
248  if (result->internal < 0) {
249  if (result->internal == AFW_INTEGER_MIN) {
250  result->internal = 0;
251  }
252  else {
253  result->internal = -result->internal;
254  }
255  }
256  result->internal = min->internal + (result->internal % range);
257 
258  return (const afw_value_t *)result;
259 }
260 
261 
262 
263 /*
264  * Adaptive function: random_number
265  *
266  * afw_function_execute_random_number
267  *
268  * See afw_function_bindings.h for more information.
269  *
270  * This returns a random double between specified values
271  *
272  * This function is not pure, so it may return a different result
273  * given exactly the same parameters.
274  *
275  * Declaration:
276  *
277  * ```
278  * function random_number(
279  * min?: double,
280  * max?: double
281  * ): double;
282  * ```
283  *
284  * Parameters:
285  *
286  * min - (optional double) Minimum double inclusive. The default is 0.0.
287  *
288  * max - (optional double) Maximum double exclusive. The default is 1.0.
289  *
290  * Returns:
291  *
292  * (double)
293  */
294 const afw_value_t *
297 {
298  const afw_value_double_t *d;
299  afw_double_t min;
300  afw_double_t max;
301  afw_double_t range;
302  afw_integer_t random;
303  afw_value_double_t *result;
304 
305  min = 0.0;
306  max = 1.0;
307 
310  min = d->internal;
311  }
312 
315  max = d->internal;
316  }
317 
318  range = max - min;
319  if (range <= 0.0) {
320  AFW_THROW_ERROR_Z(general,
321  "range between min and max is out of bounds",
322  x->xctx);
323  }
324 
325  apr_generate_random_bytes((unsigned char*)&random, sizeof(random));
326  if (random < 0) {
327  if (random == AFW_INTEGER_MIN) {
328  random = 0;
329  }
330  else {
331  random = -random;
332  }
333  }
334 
335  result = afw_value_allocate_double(x->p, x->xctx);
336  result->internal = min + (random / ((afw_double_t)AFW_INTEGER_MAX / range));
337 
338  return (const afw_value_t *)result;
339 }
Adaptive Framework Core Internal.
afw_value_allocate_base64Binary(const afw_pool_t *p, afw_xctx_t *xctx)
Allocate function for unmanaged data type base64Binary value.
afw_value_allocate_double(const afw_pool_t *p, afw_xctx_t *xctx)
Allocate function for unmanaged data type double value.
afw_value_allocate_hexBinary(const afw_pool_t *p, afw_xctx_t *xctx)
Allocate function for unmanaged data type hexBinary value.
afw_value_allocate_integer(const afw_pool_t *p, afw_xctx_t *xctx)
Allocate function for unmanaged data type integer value.
afw_value_allocate_string(const afw_pool_t *p, afw_xctx_t *xctx)
Allocate function for unmanaged data type string value.
apr_uint64_t afw_uint64_t
64-bit unsigned integer.
Definition: afw_common.h:190
double afw_double_t
Normal AFW number is double.
Definition: afw_common.h:202
apr_size_t afw_size_t
size_t.
Definition: afw_common.h:151
#define AFW_INTEGER_MAX
largest afw_integer_t
Definition: afw_common.h:281
#define AFW_INTEGER_MIN
smallest afw_integer_t
Definition: afw_common.h:291
apr_int64_t afw_integer_t
typedef for big signed int.
Definition: afw_common.h:321
#define AFW_THROW_ERROR_Z(code, message_z, xctx)
Macro used to set error and 0 rv in xctx and throw it.
Definition: afw_error.h:283
#define AFW_FUNCTION_EVALUATE_REQUIRED_DATA_TYPE_PARAMETER(A_RESULT, A_N, A_TYPE)
Evaluate an arg for a particular data type.
Definition: afw_function.h:328
#define AFW_FUNCTION_PARAMETER_IS_PRESENT(A_N)
Determine if a specific parameter value is present.
Definition: afw_function.h:242
const afw_value_t * afw_function_execute_random_digits(afw_function_execute_t *x)
Adaptive Function random_digits
const afw_value_t * afw_function_execute_random_base64Binary(afw_function_execute_t *x)
Adaptive Function random_base64Binary
const afw_value_t * afw_function_execute_random_hexBinary(afw_function_execute_t *x)
Adaptive Function random_hexBinary
const afw_value_t * afw_function_execute_random_integer(afw_function_execute_t *x)
Adaptive Function random_integer
const afw_value_t * afw_function_execute_random_number(afw_function_execute_t *x)
Adaptive Function random_number
#define afw_pool_malloc(instance, size, xctx)
Call method malloc of interface afw_pool.
Function execute parameter.
Definition: afw_function.h:53
afw_xctx_t * xctx
The execution context (xctx) of caller.
Definition: afw_function.h:62
const afw_pool_t * p
Pool for result.
Definition: afw_function.h:59
struct for data type base64Binary values.
struct for data type double values.
struct for data type hexBinary values.
struct for data type integer values.
Interface afw_value public struct.
struct for data type string values.