LCOV - code coverage report
Current view: top level - src - streams.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 14 39 35.9 %
Date: 2023-11-10 23:46:46 Functions: 3 4 75.0 %
Branches: 6 48 12.5 %

           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                 :       4088 : std::size_t AutoFile::detail_fread(Span<std::byte> dst)
      11                 :            : {
      12 [ +  - ][ #  # ]:       4088 :     if (!m_file) throw std::ios_base::failure("AutoFile::read: file handle is nullptr");
      13         [ +  - ]:       4088 :     if (m_xor.empty()) {
      14                 :       4088 :         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                 :       4088 : }
      23                 :            : 
      24                 :       4088 : void AutoFile::read(Span<std::byte> dst)
      25                 :            : {
      26         [ +  - ]:       4088 :     if (detail_fread(dst) != dst.size()) {
      27 [ #  # ][ #  # ]:          0 :         throw std::ios_base::failure(feof() ? "AutoFile::read: end of file" : "AutoFile::read: fread failed");
      28                 :            :     }
      29                 :       4088 : }
      30                 :            : 
      31                 :          0 : void AutoFile::ignore(size_t nSize)
      32                 :            : {
      33 [ #  # ][ #  # ]:          0 :     if (!m_file) throw std::ios_base::failure("AutoFile::ignore: file handle is nullptr");
      34                 :            :     unsigned char data[4096];
      35         [ #  # ]:          0 :     while (nSize > 0) {
      36                 :          0 :         size_t nNow = std::min<size_t>(nSize, sizeof(data));
      37         [ #  # ]:          0 :         if (std::fread(data, 1, nNow, m_file) != nNow) {
      38 [ #  # ][ #  # ]:          0 :             throw std::ios_base::failure(feof() ? "AutoFile::ignore: end of file" : "AutoFile::ignore: fread failed");
      39                 :            :         }
      40                 :          0 :         nSize -= nNow;
      41                 :            :     }
      42                 :          0 : }
      43                 :            : 
      44                 :       4971 : void AutoFile::write(Span<const std::byte> src)
      45                 :            : {
      46 [ +  - ][ #  # ]:       4971 :     if (!m_file) throw std::ios_base::failure("AutoFile::write: file handle is nullptr");
      47         [ +  - ]:       4971 :     if (m_xor.empty()) {
      48         [ +  - ]:       4971 :         if (std::fwrite(src.data(), 1, src.size(), m_file) != src.size()) {
      49         [ #  # ]:          0 :             throw std::ios_base::failure("AutoFile::write: write failed");
      50                 :            :         }
      51                 :       4971 :     } 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                 :       4971 : }

Generated by: LCOV version 1.14