Bitcoin Core  26.99.0
P2P Digital Currency
streams.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-present The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or https://opensource.org/license/mit/.
4 
5 #include <span.h>
6 #include <streams.h>
7 
8 #include <array>
9 
11 {
12  if (!m_file) throw std::ios_base::failure("AutoFile::read: file handle is nullptr");
13  if (m_xor.empty()) {
14  return std::fread(dst.data(), 1, dst.size(), m_file);
15  } else {
16  const auto init_pos{std::ftell(m_file)};
17  if (init_pos < 0) throw std::ios_base::failure("AutoFile::read: ftell failed");
18  std::size_t ret{std::fread(dst.data(), 1, dst.size(), m_file)};
19  util::Xor(dst.subspan(0, ret), m_xor, init_pos);
20  return ret;
21  }
22 }
23 
25 {
26  if (detail_fread(dst) != dst.size()) {
27  throw std::ios_base::failure(feof() ? "AutoFile::read: end of file" : "AutoFile::read: fread failed");
28  }
29 }
30 
31 void AutoFile::ignore(size_t nSize)
32 {
33  if (!m_file) throw std::ios_base::failure("AutoFile::ignore: file handle is nullptr");
34  unsigned char data[4096];
35  while (nSize > 0) {
36  size_t nNow = std::min<size_t>(nSize, sizeof(data));
37  if (std::fread(data, 1, nNow, m_file) != nNow) {
38  throw std::ios_base::failure(feof() ? "AutoFile::ignore: end of file" : "AutoFile::ignore: fread failed");
39  }
40  nSize -= nNow;
41  }
42 }
43 
45 {
46  if (!m_file) throw std::ios_base::failure("AutoFile::write: file handle is nullptr");
47  if (m_xor.empty()) {
48  if (std::fwrite(src.data(), 1, src.size(), m_file) != src.size()) {
49  throw std::ios_base::failure("AutoFile::write: write failed");
50  }
51  } else {
52  auto current_pos{std::ftell(m_file)};
53  if (current_pos < 0) throw std::ios_base::failure("AutoFile::write: ftell failed");
54  std::array<std::byte, 4096> buf;
55  while (src.size() > 0) {
56  auto buf_now{Span{buf}.first(std::min<size_t>(src.size(), buf.size()))};
57  std::copy(src.begin(), src.begin() + buf_now.size(), buf_now.begin());
58  util::Xor(buf_now, m_xor, current_pos);
59  if (std::fwrite(buf_now.data(), 1, buf_now.size(), m_file) != buf_now.size()) {
60  throw std::ios_base::failure{"XorFile::write: failed"};
61  }
62  src = src.subspan(buf_now.size());
63  current_pos += buf_now.size();
64  }
65  }
66 }
int ret
bool feof() const
Definition: streams.h:403
void ignore(size_t nSize)
Definition: streams.cpp:31
std::vector< std::byte > m_xor
Definition: streams.h:392
std::FILE * m_file
Definition: streams.h:391
void read(Span< std::byte > dst)
Definition: streams.cpp:24
std::size_t detail_fread(Span< std::byte > dst)
Implementation detail, only used internally.
Definition: streams.cpp:10
void write(Span< const std::byte > src)
Definition: streams.cpp:44
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:98
constexpr std::size_t size() const noexcept
Definition: span.h:187
constexpr C * data() const noexcept
Definition: span.h:174
constexpr C * begin() const noexcept
Definition: span.h:175
CONSTEXPR_IF_NOT_DEBUG Span< C > first(std::size_t count) const noexcept
Definition: span.h:205
CONSTEXPR_IF_NOT_DEBUG Span< C > subspan(std::size_t offset) const noexcept
Definition: span.h:195
void Xor(Span< std::byte > write, Span< const std::byte > key, size_t key_offset=0)
Definition: streams.h:28