xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
include
xrpl
beast
utility
rngfill.h
1
#pragma once
2
3
#include <
array
>
4
#include <
cstdint
>
5
#include <
cstring
>
6
7
namespace
beast
{
8
9
template
<
class
Generator>
10
void
11
rngfill
(
void
*
const
buffer,
std::size_t
const
bytes, Generator& g)
12
{
13
using
result_type
= Generator::result_type;
14
constexpr
std::size_t
kResultSize =
sizeof
(
result_type
);
15
16
auto
*
const
bufferStart =
static_cast<
std::uint8_t
*
>
(buffer);
17
std::size_t
const
completeIterations = bytes / kResultSize;
18
std::size_t
const
bytesRemaining = bytes % kResultSize;
19
20
for
(
std::size_t
count = 0; count < completeIterations; ++count)
21
{
22
result_type
const
v = g();
23
std::size_t
const
offset = count * kResultSize;
24
std::memcpy
(bufferStart + offset, &v, kResultSize);
25
}
26
27
if
(bytesRemaining > 0)
28
{
29
result_type
const
v = g();
30
std::size_t
const
offset = completeIterations * kResultSize;
31
std::memcpy
(bufferStart + offset, &v, bytesRemaining);
32
}
33
}
34
35
template
<
class
Generator, std::
size_t
N>
36
void
37
rngfill
(
std::array<std::uint8_t, N>
& a, Generator& g)
38
requires
(N %
sizeof
(
typename
Generator::result_type) == 0)
39
{
40
using
result_type
= Generator::result_type;
41
auto
i = N /
sizeof
(
result_type
);
42
auto
* p =
reinterpret_cast<
result_type
*
>
(a.data());
43
while
(i--)
44
*p++ = g();
45
}
46
47
}
// namespace beast
array
cstdint
cstring
std::uint8_t
std::memcpy
T memcpy(T... args)
beast
Definition
base_uint.h:697
beast::rngfill
void rngfill(void *const buffer, std::size_t const bytes, Generator &g)
Definition
rngfill.h:11
std::size_t
beast::result_type
Generated by
1.17.0