Bitcoin Core  27.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 
24 void AutoFile::seek(int64_t offset, int origin)
25 {
26  if (IsNull()) {
27  throw std::ios_base::failure("AutoFile::seek: file handle is nullptr");
28  }
29  if (std::fseek(m_file, offset, origin) != 0) {
30  throw std::ios_base::failure(feof() ? "AutoFile::seek: end of file" : "AutoFile::seek: fseek failed");
31  }
32 }
33 
34 int64_t AutoFile::tell()
35 {
36  if (IsNull()) {
37  throw std::ios_base::failure("AutoFile::tell: file handle is nullptr");
38  }
39  int64_t r{std::ftell(m_file)};
40  if (r < 0) {
41  throw std::ios_base::failure("AutoFile::tell: ftell failed");
42  }
43  return r;
44 }
45 
47 {
48  if (detail_fread(dst) != dst.size()) {
49  throw std::ios_base::failure(feof() ? "AutoFile::read: end of file" : "AutoFile::read: fread failed");
50  }
51 }
52 
53 void AutoFile::ignore(size_t nSize)
54 {
55  if (!m_file) throw std::ios_base::failure("AutoFile::ignore: file handle is nullptr");
56  unsigned char data[4096];
57  while (nSize > 0) {
58  size_t nNow = std::min<size_t>(nSize, sizeof(data));
59  if (std::fread(data, 1, nNow, m_file) != nNow) {
60  throw std::ios_base::failure(feof() ? "AutoFile::ignore: end of file" : "AutoFile::ignore: fread failed");
61  }
62  nSize -= nNow;
63  }
64 }
65 
67 {
68  if (!m_file) throw std::ios_base::failure("AutoFile::write: file handle is nullptr");
69  if (m_xor.empty()) {
70  if (std::fwrite(src.data(), 1, src.size(), m_file) != src.size()) {
71  throw std::ios_base::failure("AutoFile::write: write failed");
72  }
73  } else {
74  auto current_pos{std::ftell(m_file)};
75  if (current_pos < 0) throw std::ios_base::failure("AutoFile::write: ftell failed");
76  std::array<std::byte, 4096> buf;
77  while (src.size() > 0) {
78  auto buf_now{Span{buf}.first(std::min<size_t>(src.size(), buf.size()))};
79  std::copy(src.begin(), src.begin() + buf_now.size(), buf_now.begin());
80  util::Xor(buf_now, m_xor, current_pos);
81  if (std::fwrite(buf_now.data(), 1, buf_now.size(), m_file) != buf_now.size()) {
82  throw std::ios_base::failure{"XorFile::write: failed"};
83  }
84  src = src.subspan(buf_now.size());
85  current_pos += buf_now.size();
86  }
87  }
88 }
int ret
bool feof() const
Definition: streams.h:403
void ignore(size_t nSize)
Definition: streams.cpp:53
std::vector< std::byte > m_xor
Definition: streams.h:392
std::FILE * m_file
Definition: streams.h:391
void seek(int64_t offset, int origin)
Definition: streams.cpp:24
void read(Span< std::byte > dst)
Definition: streams.cpp:46
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
Definition: streams.h:430
std::size_t detail_fread(Span< std::byte > dst)
Implementation detail, only used internally.
Definition: streams.cpp:10
int64_t tell()
Definition: streams.cpp:34
void write(Span< const std::byte > src)
Definition: streams.cpp:66
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