aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/QUtil.cc
diff options
context:
space:
mode:
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);
+ }
+}