LCOV - code coverage report
Current view: top level - src - streams.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 88.9 % 81 72
Test Date: 2025-11-28 04:05:21 Functions: 83.3 % 12 10
Branches: 65.7 % 108 71

             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 <memusage.h>
       6                 :             : #include <span.h>
       7                 :             : #include <streams.h>
       8                 :             : #include <util/fs_helpers.h>
       9                 :             : #include <util/obfuscation.h>
      10                 :             : 
      11                 :             : #include <array>
      12                 :             : 
      13         [ +  + ]:      251028 : AutoFile::AutoFile(std::FILE* file, const Obfuscation& obfuscation) : m_file{file}, m_obfuscation{obfuscation}
      14                 :             : {
      15         [ +  + ]:      251028 :     if (!IsNull()) {
      16                 :      230062 :         auto pos{std::ftell(m_file)};
      17         [ +  + ]:      230062 :         if (pos >= 0) m_position = pos;
      18                 :             :     }
      19                 :      251028 : }
      20                 :             : 
      21                 :    28804659 : std::size_t AutoFile::detail_fread(std::span<std::byte> dst)
      22                 :             : {
      23   [ +  +  +  - ]:    28805103 :     if (!m_file) throw std::ios_base::failure("AutoFile::read: file handle is nullptr");
      24         [ +  - ]:    28804215 :     const size_t ret = std::fread(dst.data(), 1, dst.size(), m_file);
      25         [ +  + ]:    28804215 :     if (m_obfuscation) {
      26   [ +  +  +  - ]:      647758 :         if (!m_position) throw std::ios_base::failure("AutoFile::read: position unknown");
      27         [ -  + ]:      646946 :         m_obfuscation(dst.subspan(0, ret), *m_position);
      28                 :             :     }
      29         [ +  + ]:    28803809 :     if (m_position) *m_position += ret;
      30                 :    28803809 :     return ret;
      31                 :             : }
      32                 :             : 
      33                 :         274 : void AutoFile::seek(int64_t offset, int origin)
      34                 :             : {
      35         [ -  + ]:         274 :     if (IsNull()) {
      36         [ #  # ]:           0 :         throw std::ios_base::failure("AutoFile::seek: file handle is nullptr");
      37                 :             :     }
      38         [ -  + ]:         274 :     if (std::fseek(m_file, offset, origin) != 0) {
      39   [ #  #  #  # ]:           0 :         throw std::ios_base::failure(feof() ? "AutoFile::seek: end of file" : "AutoFile::seek: fseek failed");
      40                 :             :     }
      41         [ +  + ]:         274 :     if (origin == SEEK_SET) {
      42                 :         152 :         m_position = offset;
      43   [ +  +  +  - ]:         122 :     } else if (origin == SEEK_CUR && m_position.has_value()) {
      44                 :          64 :         *m_position += offset;
      45                 :             :     } else {
      46                 :          58 :         int64_t r{std::ftell(m_file)};
      47         [ -  + ]:          58 :         if (r < 0) {
      48         [ #  # ]:           0 :             throw std::ios_base::failure("AutoFile::seek: ftell failed");
      49                 :             :         }
      50                 :          58 :         m_position = r;
      51                 :             :     }
      52                 :         274 : }
      53                 :             : 
      54                 :         116 : int64_t AutoFile::tell()
      55                 :             : {
      56   [ -  +  -  - ]:         116 :     if (!m_position.has_value()) throw std::ios_base::failure("AutoFile::tell: position unknown");
      57                 :         116 :     return *m_position;
      58                 :             : }
      59                 :             : 
      60                 :          58 : int64_t AutoFile::size()
      61                 :             : {
      62         [ -  + ]:          58 :     if (IsNull()) {
      63         [ #  # ]:           0 :         throw std::ios_base::failure("AutoFile::size: file handle is nullptr");
      64                 :             :     }
      65                 :             :     // Temporarily save the current position
      66                 :          58 :     int64_t current_pos = tell();
      67                 :          58 :     seek(0, SEEK_END);
      68                 :          58 :     int64_t file_size = tell();
      69                 :             :     // Restore the original position
      70                 :          58 :     seek(current_pos, SEEK_SET);
      71                 :          58 :     return file_size;
      72                 :             : }
      73                 :             : 
      74                 :    28491986 : void AutoFile::read(std::span<std::byte> dst)
      75                 :             : {
      76         [ +  + ]:    28491986 :     if (detail_fread(dst) != dst.size()) {
      77   [ +  +  +  - ]:        8428 :         throw std::ios_base::failure(feof() ? "AutoFile::read: end of file" : "AutoFile::read: fread failed");
      78                 :             :     }
      79                 :    28487564 : }
      80                 :             : 
      81                 :        1347 : void AutoFile::ignore(size_t nSize)
      82                 :             : {
      83   [ +  +  +  - ]:        1519 :     if (!m_file) throw std::ios_base::failure("AutoFile::ignore: file handle is nullptr");
      84                 :             :     unsigned char data[4096];
      85         [ +  + ]:        3355 :     while (nSize > 0) {
      86         [ +  + ]:        2588 :         size_t nNow = std::min<size_t>(nSize, sizeof(data));
      87   [ -  +  +  + ]:        5176 :         if (std::fread(data, 1, nNow, m_file) != nNow) {
      88   [ +  +  +  - ]:        1020 :             throw std::ios_base::failure(feof() ? "AutoFile::ignore: end of file" : "AutoFile::ignore: fread failed");
      89                 :             :         }
      90                 :        2180 :         nSize -= nNow;
      91         [ +  + ]:        2180 :         if (m_position.has_value()) *m_position += nNow;
      92                 :             :     }
      93                 :         767 : }
      94                 :             : 
      95                 :     1602172 : void AutoFile::write(std::span<const std::byte> src)
      96                 :             : {
      97   [ +  +  +  - ]:     1603105 :     if (!m_file) throw std::ios_base::failure("AutoFile::write: file handle is nullptr");
      98         [ +  + ]:     1601239 :     if (!m_obfuscation) {
      99         [ +  + ]:     1269345 :         if (std::fwrite(src.data(), 1, src.size(), m_file) != src.size()) {
     100         [ +  - ]:        4498 :             throw std::ios_base::failure("AutoFile::write: write failed");
     101                 :             :         }
     102                 :     1267096 :         m_was_written = true;
     103         [ +  + ]:     1267096 :         if (m_position.has_value()) *m_position += src.size();
     104                 :             :     } else {
     105                 :             :         std::array<std::byte, 4096> buf;
     106         [ +  + ]:      663012 :         while (src.size()) {
     107         [ +  - ]:      663482 :             auto buf_now{std::span{buf}.first(std::min<size_t>(src.size(), buf.size()))};
     108                 :      331741 :             std::copy_n(src.begin(), buf_now.size(), buf_now.begin());
     109                 :      331741 :             write_buffer(buf_now);
     110                 :      331118 :             src = src.subspan(buf_now.size());
     111                 :             :         }
     112                 :             :     }
     113                 :     1598367 : }
     114                 :             : 
     115                 :      545600 : void AutoFile::write_buffer(std::span<std::byte> src)
     116                 :             : {
     117   [ -  +  -  - ]:      545600 :     if (!m_file) throw std::ios_base::failure("AutoFile::write_buffer: file handle is nullptr");
     118         [ +  - ]:      545600 :     if (m_obfuscation) {
     119   [ +  +  +  - ]:      545834 :         if (!m_position) throw std::ios_base::failure("AutoFile::write_buffer: obfuscation position unknown");
     120                 :      545366 :         m_obfuscation(src, *m_position); // obfuscate in-place
     121                 :             :     }
     122         [ +  + ]:      545366 :     if (std::fwrite(src.data(), 1, src.size(), m_file) != src.size()) {
     123         [ +  - ]:         778 :         throw std::ios_base::failure("AutoFile::write_buffer: write failed");
     124                 :             :     }
     125                 :      544977 :     m_was_written = true;
     126         [ +  - ]:      544977 :     if (m_position) *m_position += src.size();
     127                 :      544977 : }
     128                 :             : 
     129                 :           0 : bool AutoFile::Commit()
     130                 :             : {
     131                 :           0 :     return ::FileCommit(m_file);
     132                 :             : }
     133                 :             : 
     134                 :           0 : bool AutoFile::Truncate(unsigned size)
     135                 :             : {
     136                 :           0 :     m_was_written = true;
     137                 :           0 :     return ::TruncateFile(m_file, size);
     138                 :             : }
     139                 :             : 
     140                 :      208956 : size_t DataStream::GetMemoryUsage() const noexcept
     141                 :             : {
     142         [ -  + ]:      208956 :     return sizeof(*this) + memusage::DynamicUsage(vch);
     143                 :             : }
        

Generated by: LCOV version 2.0-1