2018-07-04 17:11:49 +08:00
|
|
|
// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
|
|
|
|
// de Barcelona (UAB).
|
|
|
|
//
|
|
|
|
// This work is licensed under the terms of the MIT license.
|
|
|
|
// For a copy, see <https://opensource.org/licenses/MIT>.
|
|
|
|
|
2018-09-05 17:56:51 +08:00
|
|
|
#include "Buffer.h"
|
2018-07-04 17:11:49 +08:00
|
|
|
|
2019-05-03 17:54:25 +08:00
|
|
|
#include <boost/random/independent_bits.hpp>
|
2018-07-24 23:38:46 +08:00
|
|
|
|
2018-07-04 17:11:49 +08:00
|
|
|
#include <climits>
|
|
|
|
#include <random>
|
|
|
|
|
|
|
|
namespace util {
|
2018-09-05 17:56:51 +08:00
|
|
|
namespace buffer {
|
2018-07-04 17:11:49 +08:00
|
|
|
|
2018-09-05 17:56:51 +08:00
|
|
|
shared_buffer make_random(size_t size) {
|
2018-07-04 17:11:49 +08:00
|
|
|
if (size == 0u)
|
|
|
|
return make_empty();
|
2018-07-24 23:38:46 +08:00
|
|
|
using random_bytes_engine = boost::random::independent_bits_engine<
|
2018-07-04 17:11:49 +08:00
|
|
|
std::random_device,
|
|
|
|
CHAR_BIT,
|
|
|
|
unsigned char>;
|
|
|
|
random_bytes_engine rbe;
|
2018-09-05 17:56:51 +08:00
|
|
|
auto buffer = make_empty(size);
|
|
|
|
std::generate(buffer->begin(), buffer->end(), std::ref(rbe));
|
|
|
|
return buffer;
|
2018-07-04 17:11:49 +08:00
|
|
|
}
|
|
|
|
|
2018-09-05 17:56:51 +08:00
|
|
|
std::string to_hex_string(const Buffer &buf, size_t length) {
|
|
|
|
length = std::min(static_cast<size_t>(buf.size()), length);
|
2018-07-04 17:11:49 +08:00
|
|
|
auto buffer = std::make_unique<char[]>(2u * length + 1u);
|
|
|
|
for (auto i = 0u; i < length; ++i)
|
2018-09-05 17:56:51 +08:00
|
|
|
sprintf(&buffer[2u * i], "%02x", buf.data()[i]);
|
|
|
|
if (length < buf.size())
|
2018-07-04 17:11:49 +08:00
|
|
|
return std::string(buffer.get()) + std::string("...");
|
|
|
|
return std::string(buffer.get());
|
|
|
|
}
|
|
|
|
|
2018-09-05 17:56:51 +08:00
|
|
|
} // namespace buffer
|
2018-07-04 17:11:49 +08:00
|
|
|
} // namespace util
|