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 : 12287728 : std::size_t AutoFile::detail_fread(Span<std::byte> dst) 11 : : { 12 [ + + + - ]: 12287728 : if (!m_file) throw std::ios_base::failure("AutoFile::read: file handle is nullptr"); 13 [ + - ]: 12285278 : if (m_xor.empty()) { 14 : 12285278 : return std::fread(dst.data(), 1, dst.size(), m_file); 15 : : } 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"); 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 : : } 22 : 12285278 : } 23 : : 24 : 12254420 : void AutoFile::read(Span<std::byte> dst) 25 : : { 26 [ + + ]: 12254420 : if (detail_fread(dst) != dst.size()) { 27 [ + - + - ]: 2828 : throw std::ios_base::failure(feof() ? "AutoFile::read: end of file" : "AutoFile::read: fread failed"); 28 : : } 29 : 12251592 : } 30 : : 31 : 1281 : void AutoFile::ignore(size_t nSize) 32 : : { 33 [ + + + - ]: 1281 : if (!m_file) throw std::ios_base::failure("AutoFile::ignore: file handle is nullptr"); 34 : : unsigned char data[4096]; 35 [ + + ]: 1231 : while (nSize > 0) { 36 : 888 : size_t nNow = std::min<size_t>(nSize, sizeof(data)); 37 [ + + ]: 888 : if (std::fread(data, 1, nNow, m_file) != nNow) { 38 [ + - - + ]: 691 : throw std::ios_base::failure(feof() ? "AutoFile::ignore: end of file" : "AutoFile::ignore: fread failed"); 39 : : } 40 : 197 : nSize -= nNow; 41 : : } 42 : 343 : } 43 : : 44 : 1835784 : void AutoFile::write(Span<const std::byte> src) 45 : : { 46 [ + + + - ]: 1835784 : if (!m_file) throw std::ios_base::failure("AutoFile::write: file handle is nullptr"); 47 [ + - ]: 1832501 : if (m_xor.empty()) { 48 [ + + ]: 1832501 : if (std::fwrite(src.data(), 1, src.size(), m_file) != src.size()) { 49 [ + - ]: 2650 : throw std::ios_base::failure("AutoFile::write: write failed"); 50 : : } 51 : 1829851 : } else { 52 : 0 : auto current_pos{std::ftell(m_file)}; 53 [ # # # # ]: 0 : if (current_pos < 0) throw std::ios_base::failure("AutoFile::write: ftell failed"); 54 : : std::array<std::byte, 4096> buf; 55 [ # # ]: 0 : while (src.size() > 0) { 56 : 0 : auto buf_now{Span{buf}.first(std::min<size_t>(src.size(), buf.size()))}; 57 : 0 : std::copy(src.begin(), src.begin() + buf_now.size(), buf_now.begin()); 58 : 0 : util::Xor(buf_now, m_xor, current_pos); 59 [ # # ]: 0 : if (std::fwrite(buf_now.data(), 1, buf_now.size(), m_file) != buf_now.size()) { 60 [ # # ]: 0 : throw std::ios_base::failure{"XorFile::write: failed"}; 61 : : } 62 : 0 : src = src.subspan(buf_now.size()); 63 : 0 : current_pos += buf_now.size(); 64 : : } 65 : : } 66 : 1829851 : }