From 4eccb9d87b793ad2b6e1532ef4c89ab9d2bb3a90 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Fri, 28 Dec 2012 16:37:46 -0500 Subject: Add random number functions to QUtil --- libqpdf/QUtil.cc | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'libqpdf/QUtil.cc') diff --git a/libqpdf/QUtil.cc b/libqpdf/QUtil.cc index 20143087..3cdfdc49 100644 --- a/libqpdf/QUtil.cc +++ b/libqpdf/QUtil.cc @@ -333,3 +333,42 @@ QUtil::toUTF8(unsigned long uval) return result; } + +long +QUtil::random() +{ + static bool seeded_random = false; + if (! seeded_random) + { + // Seed the random number generator with something simple, but + // just to be interesting, don't use the unmodified current + // time.... + QUtil::srandom((int)QUtil::get_current_time() ^ 0xcccc); + seeded_random = true; + } + +#ifdef HAVE_RANDOM + return ::random(); +#else + return rand(); +#endif +} + +void +QUtil::srandom(unsigned int seed) +{ +#ifdef HAVE_RANDOM + ::srandom(seed); +#else + srand(seed); +#endif +} + +void +QUtil::initializeWithRandomBytes(unsigned char* data, size_t len) +{ + for (size_t i = 0; i < len; ++i) + { + data[i] = (unsigned char)((QUtil::random() & 0xff0) >> 4); + } +} -- cgit v1.2.3-54-g00ecf