aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/QUtil.cc
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2012-12-28 22:37:46 +0100
committerJay Berkenbilt <ejb@ql.org>2012-12-31 16:32:32 +0100
commit4eccb9d87b793ad2b6e1532ef4c89ab9d2bb3a90 (patch)
treea6b1bb9319ac1363a76da0a2b86453688bb149aa /libqpdf/QUtil.cc
parent16a23368e738be88669f4fbf4d3341dd473519c7 (diff)
downloadqpdf-4eccb9d87b793ad2b6e1532ef4c89ab9d2bb3a90.tar.zst
Add random number functions to QUtil
Diffstat (limited to 'libqpdf/QUtil.cc')
-rw-r--r--libqpdf/QUtil.cc39
1 files changed, 39 insertions, 0 deletions
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);
+ }
+}