Coverage Report

Created: 2024-08-21 05:08

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