Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/.clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ bugprone-use-after-move,
misc-unused-using-decls,
modernize-use-default-member-init,
modernize-use-emplace,
modernize-use-noexcept,
modernize-use-nullptr,
performance-*,
-performance-avoid-endl,
Expand Down
2 changes: 2 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,7 @@ libbitcoin_util_a_SOURCES = \
random.cpp \
randomenv.cpp \
stacktraces.cpp \
streams.cpp \
support/cleanse.cpp \
support/lockedpool.cpp \
sync.cpp \
Expand Down Expand Up @@ -1373,6 +1374,7 @@ libdashkernel_la_SOURCES = \
script/standard.cpp \
shutdown.cpp \
spork.cpp \
streams.cpp \
support/cleanse.cpp \
support/lockedpool.cpp \
sync.cpp \
Expand Down
3 changes: 2 additions & 1 deletion src/Makefile.bench.include
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ bench_bench_dash_SOURCES = \
bench/strencodings.cpp \
bench/string_cast.cpp \
bench/util_time.cpp \
bench/verify_script.cpp
bench/verify_script.cpp \
bench/xor.cpp

nodist_bench_bench_dash_SOURCES = $(GENERATED_BENCH_FILES)

Expand Down
2 changes: 1 addition & 1 deletion src/bench/bench.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <string>
#include <vector>

#include <bench/nanobench.h>
#include <bench/nanobench.h> // IWYU pragma: export

/*
* Usage:
Expand Down
24 changes: 24 additions & 0 deletions src/bench/xor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or https://opensource.org/license/mit/.

#include <bench/bench.h>

#include <random.h>
#include <streams.h>

#include <cstddef>
#include <vector>

static void Xor(benchmark::Bench& bench)
{
FastRandomContext frc{/*fDeterministic=*/true};
auto data{frc.randbytes<std::byte>(1024)};
auto key{frc.randbytes<std::byte>(31)};

bench.batch(data.size()).unit("byte").run([&] {
util::Xor(data, key);
});
}

BENCHMARK(Xor, benchmark::PriorityLevel::HIGH);
2 changes: 0 additions & 2 deletions src/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ class CHashWriter : public HashWriter

template<typename T>
CHashWriter& operator<<(const T& obj) {
// Serialize to this stream
::Serialize(*this, obj);
return (*this);
}
Expand Down Expand Up @@ -196,7 +195,6 @@ class CHashVerifier : public CHashWriter
template<typename T>
CHashVerifier<Source>& operator>>(T&& obj)
{
// Unserialize from this stream
::Unserialize(*this, obj);
return (*this);
}
Expand Down
66 changes: 66 additions & 0 deletions src/streams.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) 2009-present The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or https://opensource.org/license/mit/.

#include <span.h>
#include <streams.h>

#include <array>

std::size_t AutoFile::detail_fread(Span<std::byte> dst)
{
if (!m_file) throw std::ios_base::failure("AutoFile::read: file handle is nullptr");
if (m_xor.empty()) {
return std::fread(dst.data(), 1, dst.size(), m_file);
} else {
const auto init_pos{std::ftell(m_file)};
if (init_pos < 0) throw std::ios_base::failure("AutoFile::read: ftell failed");
std::size_t ret{std::fread(dst.data(), 1, dst.size(), m_file)};
util::Xor(dst.subspan(0, ret), m_xor, init_pos);
return ret;
}
}

void AutoFile::read(Span<std::byte> dst)
{
if (detail_fread(dst) != dst.size()) {
throw std::ios_base::failure(feof() ? "AutoFile::read: end of file" : "AutoFile::read: fread failed");
}
}

void AutoFile::ignore(size_t nSize)
{
if (!m_file) throw std::ios_base::failure("AutoFile::ignore: file handle is nullptr");
unsigned char data[4096];
while (nSize > 0) {
size_t nNow = std::min<size_t>(nSize, sizeof(data));
if (std::fread(data, 1, nNow, m_file) != nNow) {
throw std::ios_base::failure(feof() ? "AutoFile::ignore: end of file" : "AutoFile::ignore: fread failed");
}
nSize -= nNow;
}
}

void AutoFile::write(Span<const std::byte> src)
{
if (!m_file) throw std::ios_base::failure("AutoFile::write: file handle is nullptr");
if (m_xor.empty()) {
if (std::fwrite(src.data(), 1, src.size(), m_file) != src.size()) {
throw std::ios_base::failure("AutoFile::write: write failed");
}
} else {
auto current_pos{std::ftell(m_file)};
if (current_pos < 0) throw std::ios_base::failure("AutoFile::write: ftell failed");
std::array<std::byte, 4096> buf;
while (src.size() > 0) {
auto buf_now{Span{buf}.first(std::min<size_t>(src.size(), buf.size()))};
std::copy(src.begin(), src.begin() + buf_now.size(), buf_now.begin());
util::Xor(buf_now, m_xor, current_pos);
if (std::fwrite(buf_now.data(), 1, buf_now.size(), m_file) != buf_now.size()) {
throw std::ios_base::failure{"XorFile::write: failed"};
}
src = src.subspan(buf_now.size());
current_pos += buf_now.size();
}
}
}
Loading
Loading