X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?p=sim%2Fe2-interface.git;a=blobdiff_plain;f=e2sim%2Fasn1c%2Fasn_random_fill.c;fp=e2sim%2Fasn1c%2Fasn_random_fill.c;h=15771b6c7cf70ea98a0e06111c928e6cf24373bd;hp=0000000000000000000000000000000000000000;hb=f5900596513e4359fc839ba361da085674e90b68;hpb=215d350dbe06a4c7ee370815f26128a7f7e160cb diff --git a/e2sim/asn1c/asn_random_fill.c b/e2sim/asn1c/asn_random_fill.c new file mode 100644 index 0000000..15771b6 --- /dev/null +++ b/e2sim/asn1c/asn_random_fill.c @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2017 Lev Walkin . + * All rights reserved. + * Redistribution and modifications are permitted subject to BSD license. + */ +#include +#include +#include + +int +asn_random_fill(const struct asn_TYPE_descriptor_s *td, void **struct_ptr, + size_t length) { + + if(td && td->op->random_fill) { + asn_random_fill_result_t res = + td->op->random_fill(td, struct_ptr, 0, length); + return (res.code == ARFILL_OK) ? 0 : -1; + } else { + return -1; + } +} + +static uintmax_t +asn__intmax_range(intmax_t lb, intmax_t ub) { + assert(lb <= ub); + if((ub < 0) == (lb < 0)) { + return ub - lb; + } else if(lb < 0) { + return 1 + ((uintmax_t)ub + (uintmax_t)-(lb + 1)); + } else { + assert(!"Unreachable"); + return 0; + } +} + +intmax_t +asn_random_between(intmax_t lb, intmax_t rb) { + if(lb == rb) { + return lb; + } else { + const uintmax_t intmax_max = ((~(uintmax_t)0) >> 1); + uintmax_t range = asn__intmax_range(lb, rb); + uintmax_t value = 0; + uintmax_t got_entropy = 0; + (void)intmax_max; + int max = 0xffffff; + +#ifdef __WIN32__ + max = RAND_MAX-1; +#endif + + assert(RAND_MAX > max); /* Seen 7ffffffd! */ + assert(range < intmax_max); + + for(; got_entropy < range;) { + got_entropy = (got_entropy << 24) | max; +#ifdef HAVE_RANDOM + value = (value << 24) | (random() % max); +#else + value = (value << 24) | (rand() % max); +#endif + } + + return lb + (intmax_t)(value % (range + 1)); + } +}