Coverage Report

Created: 2025-06-10 13:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/bitcoin/src/streams.cpp
Line
Count
Source
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 <memusage.h>
6
#include <span.h>
7
#include <streams.h>
8
#include <util/fs_helpers.h>
9
10
#include <array>
11
12
AutoFile::AutoFile(std::FILE* file, std::vector<std::byte> data_xor)
13
9.08M
    : m_file{file}, m_xor{std::move(data_xor)}
14
9.08M
{
15
9.08M
    if (!IsNull()) {
  Branch (15:9): [True: 9.04M, False: 44.3k]
16
9.04M
        auto pos{std::ftell(m_file)};
17
9.04M
        if (pos >= 0) m_position = pos;
  Branch (17:13): [True: 9.04M, False: 31]
18
9.04M
    }
19
9.08M
}
20
21
std::size_t AutoFile::detail_fread(std::span<std::byte> dst)
22
4.53M
{
23
4.53M
    if (!m_file) throw std::ios_base::failure("AutoFile::read: file handle is nullptr");
  Branch (23:9): [True: 0, False: 4.53M]
24
4.53M
    size_t ret = std::fread(dst.data(), 1, dst.size(), m_file);
25
4.53M
    if (!m_xor.empty()) {
  Branch (25:9): [True: 4.53M, False: 0]
26
4.53M
        if (!m_position.has_value()) throw std::ios_base::failure("AutoFile::read: position unknown");
  Branch (26:13): [True: 0, False: 4.53M]
27
4.53M
        util::Xor(dst.subspan(0, ret), m_xor, *m_position);
28
4.53M
    }
29
4.53M
    if (m_position.has_value()) *m_position += ret;
  Branch (29:9): [True: 4.53M, False: 0]
30
4.53M
    return ret;
31
4.53M
}
32
33
void AutoFile::seek(int64_t offset, int origin)
34
0
{
35
0
    if (IsNull()) {
  Branch (35:9): [True: 0, False: 0]
36
0
        throw std::ios_base::failure("AutoFile::seek: file handle is nullptr");
37
0
    }
38
0
    if (std::fseek(m_file, offset, origin) != 0) {
  Branch (38:9): [True: 0, False: 0]
39
0
        throw std::ios_base::failure(feof() ? "AutoFile::seek: end of file" : "AutoFile::seek: fseek failed");
  Branch (39:38): [True: 0, False: 0]
40
0
    }
41
0
    if (origin == SEEK_SET) {
  Branch (41:9): [True: 0, False: 0]
42
0
        m_position = offset;
43
0
    } else if (origin == SEEK_CUR && m_position.has_value()) {
  Branch (43:16): [True: 0, False: 0]
  Branch (43:38): [True: 0, False: 0]
44
0
        *m_position += offset;
45
0
    } else {
46
0
        int64_t r{std::ftell(m_file)};
47
0
        if (r < 0) {
  Branch (47:13): [True: 0, False: 0]
48
0
            throw std::ios_base::failure("AutoFile::seek: ftell failed");
49
0
        }
50
0
        m_position = r;
51
0
    }
52
0
}
53
54
int64_t AutoFile::tell()
55
0
{
56
0
    if (!m_position.has_value()) throw std::ios_base::failure("AutoFile::tell: position unknown");
  Branch (56:9): [True: 0, False: 0]
57
0
    return *m_position;
58
0
}
59
60
void AutoFile::read(std::span<std::byte> dst)
61
2.30M
{
62
2.30M
    if (detail_fread(dst) != dst.size()) {
  Branch (62:9): [True: 0, False: 2.30M]
63
0
        throw std::ios_base::failure(feof() ? "AutoFile::read: end of file" : "AutoFile::read: fread failed");
  Branch (63:38): [True: 0, False: 0]
64
0
    }
65
2.30M
}
66
67
void AutoFile::ignore(size_t nSize)
68
0
{
69
0
    if (!m_file) throw std::ios_base::failure("AutoFile::ignore: file handle is nullptr");
  Branch (69:9): [True: 0, False: 0]
70
0
    unsigned char data[4096];
71
0
    while (nSize > 0) {
  Branch (71:12): [True: 0, False: 0]
72
0
        size_t nNow = std::min<size_t>(nSize, sizeof(data));
73
0
        if (std::fread(data, 1, nNow, m_file) != nNow) {
  Branch (73:13): [True: 0, False: 0]
74
0
            throw std::ios_base::failure(feof() ? "AutoFile::ignore: end of file" : "AutoFile::ignore: fread failed");
  Branch (74:42): [True: 0, False: 0]
75
0
        }
76
0
        nSize -= nNow;
77
0
        if (m_position.has_value()) *m_position += nNow;
  Branch (77:13): [True: 0, False: 0]
78
0
    }
79
0
}
80
81
void AutoFile::write(std::span<const std::byte> src)
82
375M
{
83
375M
    if (!m_file) throw std::ios_base::failure("AutoFile::write: file handle is nullptr");
  Branch (83:9): [True: 0, False: 375M]
84
375M
    if (m_xor.empty()) {
  Branch (84:9): [True: 375M, False: 548k]
85
375M
        if (std::fwrite(src.data(), 1, src.size(), m_file) != src.size()) {
  Branch (85:13): [True: 0, False: 375M]
86
0
            throw std::ios_base::failure("AutoFile::write: write failed");
87
0
        }
88
375M
        if (m_position.has_value()) *m_position += src.size();
  Branch (88:13): [True: 375M, False: 0]
89
375M
    } else {
90
548k
        std::array<std::byte, 4096> buf;
91
1.14M
        while (src.size()) {
  Branch (91:16): [True: 593k, False: 548k]
92
593k
            auto buf_now{std::span{buf}.first(std::min<size_t>(src.size(), buf.size()))};
93
593k
            std::copy_n(src.begin(), buf_now.size(), buf_now.begin());
94
593k
            write_buffer(buf_now);
95
593k
            src = src.subspan(buf_now.size());
96
593k
        }
97
548k
    }
98
375M
}
99
100
void AutoFile::write_buffer(std::span<std::byte> src)
101
5.06M
{
102
5.06M
    if (!m_file) throw std::ios_base::failure("AutoFile::write_buffer: file handle is nullptr");
  Branch (102:9): [True: 0, False: 5.06M]
103
5.06M
    if (m_xor.size()) {
  Branch (103:9): [True: 5.06M, False: 0]
104
5.06M
        if (!m_position) throw std::ios_base::failure("AutoFile::write_buffer: obfuscation position unknown");
  Branch (104:13): [True: 0, False: 5.06M]
105
5.06M
        util::Xor(src, m_xor, *m_position); // obfuscate in-place
106
5.06M
    }
107
5.06M
    if (std::fwrite(src.data(), 1, src.size(), m_file) != src.size()) {
  Branch (107:9): [True: 0, False: 5.06M]
108
0
        throw std::ios_base::failure("AutoFile::write_buffer: write failed");
109
0
    }
110
5.06M
    if (m_position) *m_position += src.size();
  Branch (110:9): [True: 5.06M, False: 0]
111
5.06M
}
112
113
bool AutoFile::Commit()
114
65.0k
{
115
65.0k
    return ::FileCommit(m_file);
116
65.0k
}
117
118
bool AutoFile::Truncate(unsigned size)
119
0
{
120
0
    return ::TruncateFile(m_file, size);
121
0
}
122
123
size_t DataStream::GetMemoryUsage() const noexcept
124
11.0M
{
125
11.0M
    return sizeof(*this) + memusage::DynamicUsage(vch);
126
11.0M
}