diff --git a/beast/random/rngfill.h b/beast/random/rngfill.h new file mode 100644 index 0000000000..7a7c1fe9e0 --- /dev/null +++ b/beast/random/rngfill.h @@ -0,0 +1,69 @@ +//------------------------------------------------------------------------------ +/* + This file is part of Beast: https://github.com/vinniefalco/Beast + Copyright 2014, Vinnie Falco + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +#ifndef BEAST_RANDOM_RNGFILL_H_INCLUDED +#define BEAST_RANDOM_RNGFILL_H_INCLUDED + +#include +#include +#include +#include // + +namespace beast { + +template +void +rngfill (void* buffer, std::size_t bytes, + Generator& g) +{ + using result_type = + typename Generator::result_type; + while (bytes >= sizeof(result_type)) + { + auto const v = g(); + std::memcpy(buffer, &v, sizeof(v)); + buffer = reinterpret_cast< + std::uint8_t*>(buffer) + sizeof(v); + bytes -= sizeof(v); + } + if (bytes > 0) + { + auto const v = g(); + std::memcpy(buffer, &v, bytes); + } +} + +template > +void +rngfill (std::array& a, Generator& g) +{ + using result_type = + typename Generator::result_type; + auto i = N / sizeof(result_type); + result_type* p = + reinterpret_cast(a.data()); + while (i--) + *p++ = g(); +} + +} // beast + +#endif