asn_random_fill.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
3  * All rights reserved.
4  * Redistribution and modifications are permitted subject to BSD license.
5  */
9 #include <stdlib.h>
10 
11 int
12 asn_random_fill(const struct asn_TYPE_descriptor_s *td, void **struct_ptr,
13  size_t length) {
14 
15  if(td && td->op->random_fill) {
17  td->op->random_fill(td, struct_ptr, 0, length);
18  return (res.code == ARFILL_OK) ? 0 : -1;
19  } else {
20  return -1;
21  }
22 }
23 
24 static uintmax_t
25 asn__intmax_range(intmax_t lb, intmax_t ub) {
26  assert(lb <= ub);
27  if((ub < 0) == (lb < 0)) {
28  return ub - lb;
29  } else if(lb < 0) {
30  return 1 + ((uintmax_t)ub + (uintmax_t)-(lb + 1));
31  } else {
32  assert(!"Unreachable");
33  return 0;
34  }
35 }
36 
37 intmax_t
38 asn_random_between(intmax_t lb, intmax_t rb) {
39  if(lb == rb) {
40  return lb;
41  } else {
42  const uintmax_t intmax_max = ((~(uintmax_t)0) >> 1);
43  uintmax_t range = asn__intmax_range(lb, rb);
44  uintmax_t value = 0;
45  uintmax_t got_entropy = 0;
46  (void)intmax_max;
47  int max = 0xffffff;
48 
49 #ifdef __WIN32__
50  max = RAND_MAX-1;
51 #endif
52 
53  assert(RAND_MAX > max); /* Seen 7ffffffd! */
54  assert(range < intmax_max);
55 
56  for(; got_entropy < range;) {
57  got_entropy = (got_entropy << 24) | max;
58 #ifdef HAVE_RANDOM
59  value = (value << 24) | (random() % max);
60 #else
61  value = (value << 24) | (rand() % max);
62 #endif
63  }
64 
65  return lb + (intmax_t)(value % (range + 1));
66  }
67 }
asn_random_fill_result_s::code
enum asn_random_fill_result_s::@3 code
constr_TYPE.h
asn_TYPE_descriptor_s
Definition: constr_TYPE.h:224
asn_random_fill_result_s
Definition: asn_random_fill.h:24
asn_TYPE_operation_s::random_fill
asn_random_fill_f * random_fill
Definition: constr_TYPE.h:201
asn__intmax_range
static uintmax_t asn__intmax_range(intmax_t lb, intmax_t ub)
Definition: asn_random_fill.c:25
asn_TYPE_descriptor_s::op
asn_TYPE_operation_t * op
Definition: constr_TYPE.h:232
asn_random_fill
int asn_random_fill(const struct asn_TYPE_descriptor_s *td, void **struct_ptr, size_t length)
Definition: asn_random_fill.c:12
asn_random_fill.h
asn_internal.h
asn_random_between
intmax_t asn_random_between(intmax_t lb, intmax_t rb)
Definition: asn_random_fill.c:38


etsi_its_spatem_ts_coding
Author(s): Jean-Pierre Busch , Guido Küppers , Lennart Reiher
autogenerated on Sun May 18 2025 02:29:28