From bc282425088455198a7a99511c75914477d4ed32 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 13 Apr 2024 23:14:51 +0200 Subject: Merging upstream version 1.9.3. Signed-off-by: Daniel Baumann --- dns_random.hh | 93 ++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 54 insertions(+), 39 deletions(-) (limited to 'dns_random.hh') diff --git a/dns_random.hh b/dns_random.hh index 5c5e441..c3bd314 100644 --- a/dns_random.hh +++ b/dns_random.hh @@ -24,54 +24,69 @@ #include #include -void dns_random_init(const std::string& data = "", bool force_reinit = false); -uint32_t dns_random(uint32_t n); -uint16_t dns_random_uint16(); +#include -namespace pdns { - struct dns_random_engine { +inline uint32_t dns_random(uint32_t upper_bound) +{ + return arc4random_uniform(upper_bound); +} + +inline uint32_t dns_random_uint32() +{ + return arc4random(); +} - typedef uint32_t result_type; +inline uint16_t dns_random_uint16() +{ + return arc4random() & 0xffff; +} - static constexpr result_type min() - { - return 0; - } +namespace pdns +{ +struct dns_random_engine +{ - static constexpr result_type max() - { - return std::numeric_limits::max() - 1; - } + using result_type = uint32_t; + + static constexpr result_type min() + { + return 0; + } - result_type operator()() - { - return dns_random(std::numeric_limits::max()); - } - }; + static constexpr result_type max() + { + return std::numeric_limits::max(); + } - /* minimum value that a PRNG should return for this upper bound to avoid a modulo bias */ - inline unsigned int random_minimum_acceptable_value(uint32_t upper_bound) + result_type operator()() { - /* Parts of this code come from arc4random_uniform */ - /* To avoid "modulo bias" for some methods, calculate - minimum acceptable value for random number to improve - uniformity. + return dns_random_uint32(); + } +}; + +/* minimum value that a PRNG should return for this upper bound to avoid a modulo bias */ +inline unsigned int random_minimum_acceptable_value(uint32_t upper_bound) +{ + /* Parts of this code come from arc4random_uniform */ + /* To avoid "modulo bias" for some methods, calculate + minimum acceptable value for random number to improve + uniformity. - On applicable rngs, we loop until the rng spews out - value larger than min, and then take modulo out of that. - */ - unsigned int min; + On applicable rngs, we loop until the rng spews out + value larger than min, and then take modulo out of that. + */ + unsigned int min = 0; #if (ULONG_MAX > 0xffffffffUL) - min = 0x100000000UL % upper_bound; + min = 0x100000000UL % upper_bound; #else - /* Calculate (2**32 % upper_bound) avoiding 64-bit math */ - if (upper_bound > 0x80000000) - min = 1 + ~upper_bound; /* 2**32 - upper_bound */ - else { - /* (2**32 - (x * 2)) % x == 2**32 % x when x <= 2**31 */ - min = ((0xffffffff - (upper_bound * 2)) + 1) % upper_bound; - } -#endif - return min; + /* Calculate (2**32 % upper_bound) avoiding 64-bit math */ + if (upper_bound > 0x80000000) + min = 1 + ~upper_bound; /* 2**32 - upper_bound */ + else { + /* (2**32 - (x * 2)) % x == 2**32 % x when x <= 2**31 */ + min = ((0xffffffff - (upper_bound * 2)) + 1) % upper_bound; } +#endif + return min; +} } -- cgit v1.2.3