[12] | 1 | /* boost random_device.cpp implementation |
---|
| 2 | * |
---|
| 3 | * Copyright Jens Maurer 2000 |
---|
| 4 | * Distributed under the Boost Software License, Version 1.0. (See |
---|
| 5 | * accompanying file LICENSE_1_0.txt or copy at |
---|
| 6 | * http://www.boost.org/LICENSE_1_0.txt) |
---|
| 7 | * |
---|
| 8 | * $Id: random_device.cpp,v 1.8 2004/07/27 03:43:34 dgregor Exp $ |
---|
| 9 | * |
---|
| 10 | */ |
---|
| 11 | |
---|
| 12 | #include <boost/nondet_random.hpp> |
---|
| 13 | #include <string> |
---|
| 14 | #include <cassert> |
---|
| 15 | |
---|
| 16 | |
---|
| 17 | #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION |
---|
| 18 | // A definition is required even for integral static constants |
---|
| 19 | const bool boost::random_device::has_fixed_range; |
---|
| 20 | const boost::random_device::result_type boost::random_device::min_value; |
---|
| 21 | const boost::random_device::result_type boost::random_device::max_value; |
---|
| 22 | #endif |
---|
| 23 | |
---|
| 24 | |
---|
| 25 | #ifdef __linux__ |
---|
| 26 | |
---|
| 27 | // the default is the unlimited capacity device, using some secure hash |
---|
| 28 | // try "/dev/random" for blocking when the entropy pool has drained |
---|
| 29 | const char * const boost::random_device::default_token = "/dev/urandom"; |
---|
| 30 | |
---|
| 31 | /* |
---|
| 32 | * This uses the POSIX interface for unbuffered reading. |
---|
| 33 | * Using buffered std::istream would consume entropy which may |
---|
| 34 | * not actually be used. Entropy is a precious good we avoid |
---|
| 35 | * wasting. |
---|
| 36 | */ |
---|
| 37 | |
---|
| 38 | #if defined(__GNUC__) && defined(_CXXRT_STD_NAME) |
---|
| 39 | // I have severe difficulty to get the POSIX includes to work with |
---|
| 40 | // -fhonor-std and Dietmar Kühl's standard C++ library. Hack around that |
---|
| 41 | // problem for now. |
---|
| 42 | extern "C" { |
---|
| 43 | static const int O_RDONLY = 0; |
---|
| 44 | extern int open(const char *__file, int __oflag, ...); |
---|
| 45 | extern int read(int __fd, __ptr_t __buf, size_t __nbytes); |
---|
| 46 | extern int close(int __fd); |
---|
| 47 | } |
---|
| 48 | #else |
---|
| 49 | #include <sys/types.h> |
---|
| 50 | #include <sys/stat.h> |
---|
| 51 | #include <fcntl.h> // open |
---|
| 52 | #include <unistd.h> // read, close |
---|
| 53 | #endif |
---|
| 54 | |
---|
| 55 | #include <errno.h> // errno |
---|
| 56 | #include <string.h> // strerror |
---|
| 57 | #include <stdexcept> // std::invalid_argument |
---|
| 58 | |
---|
| 59 | |
---|
| 60 | class boost::random_device::impl |
---|
| 61 | { |
---|
| 62 | public: |
---|
| 63 | impl(const std::string & token) : path(token) { |
---|
| 64 | fd = open(token.c_str(), O_RDONLY); |
---|
| 65 | if(fd < 0) |
---|
| 66 | error("cannot open"); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | ~impl() { if(close(fd) < 0) error("could not close"); } |
---|
| 70 | |
---|
| 71 | unsigned int next() { |
---|
| 72 | unsigned int result; |
---|
| 73 | long sz = read(fd, reinterpret_cast<char *>(&result), sizeof(result)); |
---|
| 74 | if(sz == -1) |
---|
| 75 | error("error while reading"); |
---|
| 76 | else if(sz != sizeof(result)) { |
---|
| 77 | errno = 0; |
---|
| 78 | error("EOF while reading"); |
---|
| 79 | } |
---|
| 80 | return result; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | private: |
---|
| 84 | void error(const std::string & msg) { |
---|
| 85 | throw std::invalid_argument("boost::random_device: " + msg + |
---|
| 86 | " random-number pseudo-device " + path + |
---|
| 87 | ": " + strerror(errno)); |
---|
| 88 | } |
---|
| 89 | const std::string path; |
---|
| 90 | int fd; |
---|
| 91 | }; |
---|
| 92 | |
---|
| 93 | #endif // __linux__ |
---|
| 94 | |
---|
| 95 | |
---|
| 96 | boost::random_device::random_device(const std::string& token) |
---|
| 97 | : pimpl(new impl(token)) |
---|
| 98 | { |
---|
| 99 | assert((std::numeric_limits<result_type>::max)() == max_value); |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | boost::random_device::~random_device() |
---|
| 103 | { |
---|
| 104 | // the complete class impl is now visible, so we're safe |
---|
| 105 | // (see comment in random.hpp) |
---|
| 106 | delete pimpl; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | double boost::random_device::entropy() const |
---|
| 110 | { |
---|
| 111 | return 10; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | unsigned int boost::random_device::operator()() |
---|
| 115 | { |
---|
| 116 | return pimpl->next(); |
---|
| 117 | } |
---|