LCOV - code coverage report
Current view: top level - src - streams.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 93.2 % 59 55
Test Date: 2024-07-07 04:03:53 Functions: 100.0 % 6 6
Branches: 54.3 % 94 51

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

Generated by: LCOV version 2.0-1