1 //===-------------------------- random.cpp --------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #if defined(_WIN32) 11 // Must be defined before including stdlib.h to enable rand_s(). 12 #define _CRT_RAND_S 13 #include <stdio.h> 14 #endif 15 16 #include "random" 17 #include "system_error" 18 19 #ifdef __sun__ 20 #define rename solaris_headers_are_broken 21 #endif 22 #if !defined(_WIN32) 23 #include <fcntl.h> 24 #include <unistd.h> 25 #endif // defined(_WIN32) 26 #include <errno.h> 27 28 _LIBCPP_BEGIN_NAMESPACE_STD 29 30 #if defined(_WIN32) 31 random_device::random_device(const string&) 32 { 33 } 34 35 random_device::~random_device() 36 { 37 } 38 39 unsigned 40 random_device::operator()() 41 { 42 unsigned r; 43 errno_t err = rand_s(&r); 44 if (err) 45 __throw_system_error(err, "random_device rand_s failed."); 46 return r; 47 } 48 #else 49 random_device::random_device(const string& __token) 50 : __f_(open(__token.c_str(), O_RDONLY)) 51 { 52 if (__f_ < 0) 53 __throw_system_error(errno, ("random_device failed to open " + __token).c_str()); 54 } 55 56 random_device::~random_device() 57 { 58 close(__f_); 59 } 60 61 unsigned 62 random_device::operator()() 63 { 64 unsigned r; 65 size_t n = sizeof(r); 66 char* p = reinterpret_cast<char*>(&r); 67 while (n > 0) 68 { 69 ssize_t s = read(__f_, p, n); 70 if (s == 0) 71 __throw_system_error(ENODATA, "random_device got EOF"); 72 if (s == -1) 73 { 74 if (errno != EINTR) 75 __throw_system_error(errno, "random_device got an unexpected error"); 76 continue; 77 } 78 n -= static_cast<size_t>(s); 79 p += static_cast<size_t>(s); 80 } 81 return r; 82 } 83 #endif // defined(_WIN32) 84 85 double 86 random_device::entropy() const _NOEXCEPT 87 { 88 return 0; 89 } 90 91 _LIBCPP_END_NAMESPACE_STD 92