Branch data Line data Source code
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 : :
10 : 19 : std::size_t AutoFile::detail_fread(Span<std::byte> dst)
11 : : {
12 [ - + - - ]: 19 : if (!m_file) throw std::ios_base::failure("AutoFile::read: file handle is nullptr");
13 [ - + ]: 19 : if (m_xor.empty()) {
14 [ - - ]: 19 : return std::fread(dst.data(), 1, dst.size(), m_file);
15 : : } else {
16 : 19 : const auto init_pos{std::ftell(m_file)};
17 [ - + - - ]: 19 : if (init_pos < 0) throw std::ios_base::failure("AutoFile::read: ftell failed");
18 [ + - ]: 19 : std::size_t ret{std::fread(dst.data(), 1, dst.size(), m_file)};
19 : 19 : util::Xor(dst.subspan(0, ret), m_xor, init_pos);
20 : 19 : return ret;
21 : : }
22 : : }
23 : :
24 : 0 : void AutoFile::seek(int64_t offset, int origin)
25 : : {
26 [ # # ]: 0 : if (IsNull()) {
27 [ # # ]: 0 : throw std::ios_base::failure("AutoFile::seek: file handle is nullptr");
28 : : }
29 [ # # ]: 0 : if (std::fseek(m_file, offset, origin) != 0) {
30 [ # # # # ]: 0 : throw std::ios_base::failure(feof() ? "AutoFile::seek: end of file" : "AutoFile::seek: fseek failed");
31 : : }
32 : 0 : }
33 : :
34 : 0 : int64_t AutoFile::tell()
35 : : {
36 [ # # ]: 0 : if (IsNull()) {
37 [ # # ]: 0 : throw std::ios_base::failure("AutoFile::tell: file handle is nullptr");
38 : : }
39 : 0 : int64_t r{std::ftell(m_file)};
40 [ # # ]: 0 : if (r < 0) {
41 [ # # ]: 0 : throw std::ios_base::failure("AutoFile::tell: ftell failed");
42 : : }
43 : 0 : return r;
44 : : }
45 : :
46 : 19 : void AutoFile::read(Span<std::byte> dst)
47 : : {
48 [ - + ]: 19 : if (detail_fread(dst) != dst.size()) {
49 [ # # # # ]: 0 : throw std::ios_base::failure(feof() ? "AutoFile::read: end of file" : "AutoFile::read: fread failed");
50 : : }
51 : 19 : }
52 : :
53 : 0 : void AutoFile::ignore(size_t nSize)
54 : : {
55 [ # # # # ]: 0 : if (!m_file) throw std::ios_base::failure("AutoFile::ignore: file handle is nullptr");
56 : : unsigned char data[4096];
57 [ # # ]: 0 : while (nSize > 0) {
58 [ # # ]: 0 : size_t nNow = std::min<size_t>(nSize, sizeof(data));
59 [ # # # # ]: 0 : if (std::fread(data, 1, nNow, m_file) != nNow) {
60 [ # # # # ]: 0 : throw std::ios_base::failure(feof() ? "AutoFile::ignore: end of file" : "AutoFile::ignore: fread failed");
61 : : }
62 : 0 : nSize -= nNow;
63 : : }
64 : 0 : }
65 : :
66 : 22 : void AutoFile::write(Span<const std::byte> src)
67 : : {
68 [ - + - - ]: 22 : if (!m_file) throw std::ios_base::failure("AutoFile::write: file handle is nullptr");
69 [ + + ]: 22 : if (m_xor.empty()) {
70 [ - + ]: 1 : if (std::fwrite(src.data(), 1, src.size(), m_file) != src.size()) {
71 [ # # ]: 0 : throw std::ios_base::failure("AutoFile::write: write failed");
72 : : }
73 : : } else {
74 : 21 : auto current_pos{std::ftell(m_file)};
75 [ - + - - ]: 21 : if (current_pos < 0) throw std::ios_base::failure("AutoFile::write: ftell failed");
76 : : std::array<std::byte, 4096> buf;
77 [ + + ]: 42 : while (src.size() > 0) {
78 [ + - ]: 42 : auto buf_now{Span{buf}.first(std::min<size_t>(src.size(), buf.size()))};
79 : 21 : std::copy(src.begin(), src.begin() + buf_now.size(), buf_now.begin());
80 : 21 : util::Xor(buf_now, m_xor, current_pos);
81 [ - + ]: 21 : if (std::fwrite(buf_now.data(), 1, buf_now.size(), m_file) != buf_now.size()) {
82 [ # # ]: 0 : throw std::ios_base::failure{"XorFile::write: failed"};
83 : : }
84 : 21 : src = src.subspan(buf_now.size());
85 : 21 : current_pos += buf_now.size();
86 : : }
87 : : }
88 : 22 : }
|