3 * Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
5 * Redistribution and modifications are permitted subject to BSD license.
7 #include <asn_internal.h>
8 #include <asn_random_fill.h>
9 #include <constr_TYPE.h>
12 asn_random_fill(const struct asn_TYPE_descriptor_s *td, void **struct_ptr,
15 if(td && td->op->random_fill) {
16 asn_random_fill_result_t res =
17 td->op->random_fill(td, struct_ptr, 0, length);
18 return (res.code == ARFILL_OK) ? 0 : -1;
25 asn__intmax_range(intmax_t lb, intmax_t ub) {
27 if((ub < 0) == (lb < 0)) {
30 return 1 + ((uintmax_t)ub + (uintmax_t)-(lb + 1));
32 assert(!"Unreachable");
38 asn_random_between(intmax_t lb, intmax_t rb) {
42 const uintmax_t intmax_max = ((~(uintmax_t)0) >> 1);
43 uintmax_t range = asn__intmax_range(lb, rb);
45 uintmax_t got_entropy = 0;
47 assert(RAND_MAX > 0xffffff); /* Seen 7ffffffd! */
48 assert(range < intmax_max);
50 for(; got_entropy < range;) {
51 got_entropy = (got_entropy << 24) | 0xffffff;
52 value = (value << 24) | (random() % 0xffffff);
55 return lb + (intmax_t)(value % (range + 1));