line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
// |
2
|
|
|
|
|
|
|
// Copyright (c) 2018 Andrey Semashev |
3
|
|
|
|
|
|
|
// |
4
|
|
|
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. |
5
|
|
|
|
|
|
|
// (See accompanying file LICENSE_1_0.txt or copy at |
6
|
|
|
|
|
|
|
// http://www.boost.org/LICENCE_1_0.txt) |
7
|
|
|
|
|
|
|
// |
8
|
|
|
|
|
|
|
// getrandom() capable platforms |
9
|
|
|
|
|
|
|
// |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
#include |
12
|
|
|
|
|
|
|
#include |
13
|
|
|
|
|
|
|
#include |
14
|
|
|
|
|
|
|
#include |
15
|
|
|
|
|
|
|
#include |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
#if !defined(BOOST_UUID_RANDOM_PROVIDER_GETRANDOM_IMPL_GETRANDOM) |
18
|
|
|
|
|
|
|
#if BOOST_LIB_C_GNU >= BOOST_VERSION_NUMBER(2, 25, 0) |
19
|
|
|
|
|
|
|
#define BOOST_UUID_RANDOM_PROVIDER_GETRANDOM_HAS_LIBC_WRAPPER |
20
|
|
|
|
|
|
|
#elif defined(__has_include) |
21
|
|
|
|
|
|
|
#if __has_include() |
22
|
|
|
|
|
|
|
#define BOOST_UUID_RANDOM_PROVIDER_GETRANDOM_HAS_LIBC_WRAPPER |
23
|
|
|
|
|
|
|
#endif |
24
|
|
|
|
|
|
|
#endif |
25
|
|
|
|
|
|
|
#endif // !defined(BOOST_UUID_RANDOM_PROVIDER_GETRANDOM_IMPL_GETRANDOM) |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
#if defined(BOOST_UUID_RANDOM_PROVIDER_GETRANDOM_HAS_LIBC_WRAPPER) |
28
|
|
|
|
|
|
|
#include |
29
|
|
|
|
|
|
|
#else |
30
|
|
|
|
|
|
|
#include // ssize_t |
31
|
|
|
|
|
|
|
#endif |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
namespace boost { |
34
|
|
|
|
|
|
|
namespace uuids { |
35
|
|
|
|
|
|
|
namespace detail { |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
class random_provider_base |
38
|
|
|
|
|
|
|
{ |
39
|
|
|
|
|
|
|
public: |
40
|
|
|
|
|
|
|
//! Obtain entropy and place it into a memory location |
41
|
|
|
|
|
|
|
//! \param[in] buf the location to write entropy |
42
|
|
|
|
|
|
|
//! \param[in] siz the number of bytes to acquire |
43
|
1001
|
|
|
|
|
|
void get_random_bytes(void *buf, std::size_t siz) |
44
|
|
|
|
|
|
|
{ |
45
|
1001
|
|
|
|
|
|
std::size_t offset = 0; |
46
|
2002
|
100
|
|
|
|
|
while (offset < siz) |
47
|
|
|
|
|
|
|
{ |
48
|
1001
|
|
|
|
|
|
ssize_t sz = get_random(static_cast< char* >(buf) + offset, siz - offset, 0u); |
49
|
|
|
|
|
|
|
|
50
|
1001
|
50
|
|
|
|
|
if (BOOST_UNLIKELY(sz < 0)) |
51
|
|
|
|
|
|
|
{ |
52
|
0
|
|
|
|
|
|
int err = errno; |
53
|
0
|
0
|
|
|
|
|
if (err == EINTR) |
54
|
0
|
|
|
|
|
|
continue; |
55
|
0
|
0
|
|
|
|
|
BOOST_THROW_EXCEPTION(entropy_error(err, "getrandom")); |
|
|
0
|
|
|
|
|
|
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
1001
|
|
|
|
|
|
offset += sz; |
59
|
|
|
|
|
|
|
} |
60
|
1001
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
private: |
63
|
1001
|
|
|
|
|
|
static ssize_t get_random(void *buf, std::size_t size, unsigned int flags) |
64
|
|
|
|
|
|
|
{ |
65
|
|
|
|
|
|
|
#if defined(BOOST_UUID_RANDOM_PROVIDER_GETRANDOM_IMPL_GETRANDOM) |
66
|
|
|
|
|
|
|
return BOOST_UUID_RANDOM_PROVIDER_GETRANDOM_IMPL_GETRANDOM(buf, size, flags); |
67
|
|
|
|
|
|
|
#elif defined(BOOST_UUID_RANDOM_PROVIDER_GETRANDOM_HAS_LIBC_WRAPPER) |
68
|
|
|
|
|
|
|
return ::getrandom(buf, size, flags); |
69
|
|
|
|
|
|
|
#else |
70
|
1001
|
|
|
|
|
|
return ::syscall(SYS_getrandom, buf, size, flags); |
71
|
|
|
|
|
|
|
#endif |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
}; |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
} // detail |
76
|
|
|
|
|
|
|
} // uuids |
77
|
|
|
|
|
|
|
} // boost |