LCOV - code coverage report
Current view: top level - src - streams.h (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 97.2 % 254 247
Test Date: 2026-04-27 06:58:15 Functions: 100.0 % 32 32
Branches: 40.6 % 4240 1723

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2                 :             : // Copyright (c) 2009-present The Bitcoin Core developers
       3                 :             : // Distributed under the MIT software license, see the accompanying
       4                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       5                 :             : 
       6                 :             : #ifndef BITCOIN_STREAMS_H
       7                 :             : #define BITCOIN_STREAMS_H
       8                 :             : 
       9                 :             : #include <serialize.h>
      10                 :             : #include <span.h>
      11                 :             : #include <support/allocators/zeroafterfree.h>
      12                 :             : #include <util/check.h>
      13                 :             : #include <util/log.h>
      14                 :             : #include <util/obfuscation.h>
      15                 :             : #include <util/overflow.h>
      16                 :             : #include <util/syserror.h>
      17                 :             : 
      18                 :             : #include <algorithm>
      19                 :             : #include <cassert>
      20                 :             : #include <cstddef>
      21                 :             : #include <cstdint>
      22                 :             : #include <cstdio>
      23                 :             : #include <cstring>
      24                 :             : #include <ios>
      25                 :             : #include <limits>
      26                 :             : #include <optional>
      27                 :             : #include <string>
      28                 :             : #include <vector>
      29                 :             : 
      30                 :             : /* Minimal stream for overwriting and/or appending to an existing byte vector
      31                 :             :  *
      32                 :             :  * The referenced vector will grow as necessary
      33                 :             :  */
      34                 :             : class VectorWriter
      35                 :             : {
      36                 :             : public:
      37                 :             : /*
      38                 :             :  * @param[in]  vchDataIn  Referenced byte vector to overwrite/append
      39                 :             :  * @param[in]  nPosIn Starting position. Vector index where writes should start. The vector will initially
      40                 :             :  *                    grow as necessary to max(nPosIn, vec.size()). So to append, use vec.size().
      41                 :             : */
      42                 :      352354 :     VectorWriter(std::vector<unsigned char>& vchDataIn, size_t nPosIn) : vchData{vchDataIn}, nPos{nPosIn}
      43                 :             :     {
      44   [ -  +  +  + ]:      352354 :         if(nPos > vchData.size())
      45                 :           1 :             vchData.resize(nPos);
      46                 :      352354 :     }
      47                 :             : /*
      48                 :             :  * (other params same as above)
      49                 :             :  * @param[in]  args  A list of items to serialize starting at nPosIn.
      50                 :             : */
      51                 :             :     template <typename... Args>
      52   [ +  -  +  -  :      323251 :     VectorWriter(std::vector<unsigned char>& vchDataIn, size_t nPosIn, Args&&... args) : VectorWriter{vchDataIn, nPosIn}
          +  -  +  -  +  
          -  +  -  -  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
           +  - ][ +  - ]
           [ +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
      53                 :             :     {
      54   [ +  -  +  -  :      323251 :         ::SerializeMany(*this, std::forward<Args>(args)...);
          +  -  +  -  +  
          -  +  -  -  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
           +  - ][ +  - ]
           [ +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
      55                 :      160023 :     }
      56                 :     7764426 :     void write(std::span<const std::byte> src)
      57                 :             :     {
      58   [ -  +  -  + ]:     7764426 :         assert(nPos <= vchData.size());
      59         [ +  + ]:     7764426 :         size_t nOverwrite = std::min(src.size(), vchData.size() - nPos);
      60         [ +  + ]:     7764426 :         if (nOverwrite) {
      61                 :          19 :             memcpy(vchData.data() + nPos, src.data(), nOverwrite);
      62                 :             :         }
      63         [ +  + ]:     7764426 :         if (nOverwrite < src.size()) {
      64                 :     7764408 :             vchData.insert(vchData.end(), UCharCast(src.data()) + nOverwrite, UCharCast(src.data() + src.size()));
      65                 :             :         }
      66                 :     7764426 :         nPos += src.size();
      67                 :     7764426 :     }
      68                 :             :     template <typename T>
      69                 :      408624 :     VectorWriter& operator<<(const T& obj)
      70                 :             :     {
      71   [ +  -  +  -  :      408602 :         ::Serialize(*this, obj);
          +  -  +  -  +  
             -  +  -  +  
           - ][ +  -  +  
          -  +  -  +  -  
          #  #  #  #  #  
           # ][ +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
           [ +  -  +  -  
          +  -  +  -  +  
                      - ]
      72                 :      159388 :         return (*this);
      73                 :             :     }
      74                 :             : 
      75                 :             : private:
      76                 :             :     std::vector<unsigned char>& vchData;
      77                 :             :     size_t nPos;
      78                 :             : };
      79                 :             : 
      80                 :             : /** Minimal stream for reading from an existing byte array by std::span.
      81                 :             :  */
      82                 :             : class SpanReader
      83                 :             : {
      84                 :             : private:
      85                 :             :     std::span<const std::byte> m_data;
      86                 :             : 
      87                 :             : public:
      88                 :             :     /**
      89                 :             :      * @param[in]  data Referenced byte vector to overwrite/append
      90                 :             :      */
      91                 :       90801 :     explicit SpanReader(std::span<const unsigned char> data) : m_data{std::as_bytes(data)} {}
      92   [ +  +  +  - ]:      549578 :     explicit SpanReader(std::span<const std::byte> data) : m_data{data} {}
           [ +  +  #  # ]
           [ +  +  +  +  
                   +  + ]
      93                 :             : 
      94                 :             :     template<typename T>
      95                 :     1483915 :     SpanReader& operator>>(T&& obj)
      96                 :             :     {
      97   [ +  +  +  -  :     1482706 :         ::Unserialize(*this, obj);
          +  -  +  -  +  
           - ][ +  +  #  
          #  #  #  #  #  
           #  # ][ +  +  
          +  +  +  +  +  
           + ][ +  +  +  
             +  #  #  #  
           # ][ +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  +  +  +  +  
           + ][ +  +  +  
          -  +  -  +  -  
          +  +  +  -  +  
          -  +  -  +  +  
          +  -  +  -  +  
          -  +  +  +  -  
          +  -  +  -  +  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
           [ #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           # ][ +  -  +  
          -  +  -  +  -  
          -  +  +  -  -  
           +  #  # ][ +  
          -  +  -  +  -  
          +  -  -  +  -  
           + ][ +  -  +  
          -  -  +  +  -  
          +  -  +  -  +  
                -  +  - ]
      98                 :     1296425 :         return (*this);
      99                 :             :     }
     100                 :             : 
     101   [ +  +  +  +  :       23150 :     size_t size() const { return m_data.size(); }
          +  +  -  +  -  
          +  -  +  -  +  
          +  -  -  +  +  
          -  +  +  -  +  
           -  + ][ #  # ]
           [ +  -  +  -  
          +  -  +  -  +  
                      - ]
     102         [ +  + ]:       83995 :     bool empty() const { return m_data.empty(); }
           [ +  +  +  + ]
           [ +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  -  
           + ][ #  #  #  
             #  #  #  #  
           # ][ +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
     103                 :             : 
     104                 :    10933857 :     void read(std::span<std::byte> dst)
     105                 :             :     {
     106         [ +  + ]:    10933857 :         if (dst.size() == 0) {
     107                 :             :             return;
     108                 :             :         }
     109                 :             : 
     110                 :             :         // Read from the beginning of the buffer
     111         [ +  + ]:    10932932 :         if (dst.size() > m_data.size()) {
     112         [ +  - ]:        3858 :             throw std::ios_base::failure("SpanReader::read(): end of data");
     113                 :             :         }
     114                 :    10931003 :         memcpy(dst.data(), m_data.data(), dst.size());
     115                 :    10931003 :         m_data = m_data.subspan(dst.size());
     116                 :             :     }
     117                 :             : 
     118                 :           1 :     void ignore(size_t n)
     119                 :             :     {
     120         [ +  - ]:           1 :         if (n > m_data.size()) {
     121         [ +  - ]:           2 :             throw std::ios_base::failure("SpanReader::ignore(): end of data");
     122                 :             :         }
     123                 :           0 :         m_data = m_data.subspan(n);
     124                 :           0 :     }
     125                 :             : };
     126                 :             : 
     127                 :             : /** Minimal stream for writing to an existing span of bytes.
     128                 :             :  */
     129                 :             : class SpanWriter
     130                 :             : {
     131                 :             : private:
     132                 :             :     std::span<std::byte> m_dest;
     133                 :             : 
     134                 :             : public:
     135                 :          16 :     explicit SpanWriter(std::span<std::byte> dest) : m_dest{dest} {}
     136                 :             :     template <typename... Args>
     137                 :           2 :     SpanWriter(std::span<std::byte> dest, Args&&... args) : SpanWriter{dest}
     138                 :             :     {
     139         [ -  + ]:           2 :         ::SerializeMany(*this, std::forward<Args>(args)...);
     140                 :           0 :     }
     141                 :             : 
     142                 :          99 :     void write(std::span<const std::byte> src)
     143                 :             :     {
     144         [ +  + ]:          99 :         if (src.size() > m_dest.size()) {
     145         [ +  - ]:           4 :             throw std::ios_base::failure("SpanWriter::write(): exceeded buffer size");
     146                 :             :         }
     147                 :          97 :         memcpy(m_dest.data(), src.data(), src.size());
     148                 :          97 :         m_dest = m_dest.subspan(src.size());
     149                 :          97 :     }
     150                 :             : 
     151                 :             :     template<typename T>
     152                 :          49 :     SpanWriter& operator<<(const T& obj)
     153                 :             :     {
     154         [ +  - ]:          47 :         ::Serialize(*this, obj);
           [ +  -  -  + ]
     155                 :          15 :         return *this;
     156                 :             :     }
     157                 :             : };
     158                 :             : 
     159                 :             : /** Double ended buffer combining vector and stream-like interfaces.
     160                 :             :  *
     161                 :             :  * >> and << read and write unformatted data using the above serialization templates.
     162                 :             :  * Fills with data in linear time; some stringstream implementations take N^2 time.
     163                 :             :  */
     164   [ +  +  +  - ]:     8223025 : class DataStream
           [ +  -  #  #  
           #  # ][ +  -  
             +  -  +  - ]
     165                 :             : {
     166                 :             : protected:
     167                 :             :     using vector_type = SerializeData;
     168                 :             :     vector_type vch;
     169                 :             :     vector_type::size_type m_read_pos{0};
     170                 :             : 
     171                 :             : public:
     172                 :             :     typedef vector_type::allocator_type   allocator_type;
     173                 :             :     typedef vector_type::size_type        size_type;
     174                 :             :     typedef vector_type::difference_type  difference_type;
     175                 :             :     typedef vector_type::reference        reference;
     176                 :             :     typedef vector_type::const_reference  const_reference;
     177                 :             :     typedef vector_type::value_type       value_type;
     178                 :             :     typedef vector_type::iterator         iterator;
     179                 :             :     typedef vector_type::const_iterator   const_iterator;
     180                 :             :     typedef vector_type::reverse_iterator reverse_iterator;
     181                 :             : 
     182                 :             :     explicit DataStream() = default;
     183                 :         214 :     explicit DataStream(std::span<const uint8_t> sp) : DataStream{std::as_bytes(sp)} {}
     184                 :      408030 :     explicit DataStream(std::span<const value_type> sp) : vch(sp.data(), sp.data() + sp.size()) {}
     185                 :             : 
     186                 :         759 :     std::string str() const
     187                 :             :     {
     188                 :        2277 :         return std::string{UCharCast(data()), UCharCast(data() + size())};
     189                 :             :     }
     190                 :             : 
     191                 :             :     //
     192                 :             :     // Vector subset
     193                 :             :     //
     194                 :             :     const_iterator begin() const                     { return vch.begin() + m_read_pos; }
     195                 :      255000 :     iterator begin()                                 { return vch.begin() + m_read_pos; }
     196                 :             :     const_iterator end() const                       { return vch.end(); }
     197         [ +  - ]:      127609 :     iterator end()                                   { return vch.end(); }
           [ +  -  +  - ]
           [ +  -  +  -  
             +  -  +  - ]
     198   [ -  +  +  + ]:    17128703 :     size_type size() const                           { return vch.size() - m_read_pos; }
           [ -  +  +  +  
          -  +  +  +  -  
          +  +  -  -  +  
          +  -  -  +  +  
          +  -  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  #  # ][ #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
           # ][ -  +  +  
          -  -  +  +  +  
          +  +  +  +  +  
             -  -  +  +  
           - ][ -  +  +  
          -  -  +  +  +  
             +  -  -  + ]
           [ -  +  +  -  
          -  +  +  -  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
           [ -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          +  -  +  -  +  
             -  +  +  - ]
           [ -  +  +  -  
          -  +  +  -  -  
          +  +  -  -  +  
          +  -  -  +  +  
          -  -  +  +  -  
          -  +  +  -  -  
          +  +  -  -  +  
          +  -  -  +  +  
          -  -  +  +  -  
             -  +  +  - ]
           [ -  +  +  -  
          -  +  +  -  -  
          +  +  -  -  +  
          +  -  -  +  +  
          -  -  +  +  -  
          -  +  +  -  -  
          +  +  -  -  +  
          +  -  -  +  +  
          -  -  +  +  -  
          -  +  +  -  -  
          +  +  -  -  +  
          +  -  -  +  +  
          -  -  +  +  -  
          -  +  +  -  -  
             +  +  -  -  
           - ][ -  +  -  
          +  -  +  -  +  
          -  +  -  +  +  
          -  -  +  -  +  
          -  +  +  -  -  
          +  -  +  -  +  
          +  -  -  +  +  
          -  -  +  +  -  
          -  +  +  -  -  
           +  +  - ][ -  
          +  +  -  -  +  
          +  -  -  -  -  
          -  -  -  -  -  
          -  +  +  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  +  +  -  -  
          +  +  -  -  +  
          +  -  -  +  +  
           - ][ -  +  -  
          +  +  -  -  +  
          -  +  +  -  -  
          +  -  +  -  +  
          -  +  -  -  -  
          +  +  -  -  +  
          +  -  -  +  +  
          -  -  +  +  -  
             -  -  -  - ]
           [ -  +  +  +  
          +  +  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  #  # ][ -  
          +  -  +  -  +  
          +  -  -  +  -  
          +  +  -  -  +  
          +  -  -  +  -  
          +  +  -  -  +  
          +  -  #  #  #  
           # ][ -  +  +  
          -  -  +  -  +  
          -  -  -  -  -  
          +  -  -  -  -  
          -  +  +  -  -  
          +  -  -  -  -  
             -  +  +  - ]
           [ -  +  -  +  
          +  -  -  +  -  
             +  -  +  +  
           - ][ -  +  #  
          #  #  #  #  #  
          #  #  #  #  #  
           # ][ -  +  +  
          +  +  +  +  +  
                   -  + ]
     199   [ -  +  +  +  :       20768 :     bool empty() const                               { return vch.size() == m_read_pos; }
          -  +  +  +  -  
          +  +  +  -  +  
           +  + ][ -  +  
          +  -  -  +  +  
          -  -  +  +  -  
          -  +  +  -  -  
          +  +  +  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  # ][ -  +  
          -  +  -  +  +  
          -  -  +  +  -  
          -  +  +  -  -  
          +  +  -  -  +  
          +  -  -  +  +  
          -  -  +  +  -  
          -  +  +  -  -  
          +  +  -  -  +  
          +  -  -  +  +  
          -  -  +  +  -  
          -  +  +  -  -  
          +  +  -  -  +  
          +  -  -  +  +  
          -  -  +  +  -  
          -  +  +  -  -  
          +  +  -  -  +  
           +  - ][ -  +  
          +  -  -  -  -  
                      - ]
     200                 :      334998 :     void resize(size_type n, value_type c = value_type{}) { vch.resize(n + m_read_pos, c); }
     201   [ +  -  +  -  :     6803485 :     void reserve(size_type n)                        { vch.reserve(n + m_read_pos); }
          +  -  +  -  +  
           - ][ +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
           -  -  - ][ +  
          -  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
           # ][ +  -  -  
          -  -  -  +  -  
             -  -  +  - ]
           [ +  -  +  - ]
           [ +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
           -  +  - ][ +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  -  -  -  -  
          -  -  -  -  -  
          -  +  -  +  -  
          +  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  -  
             -  -  -  -  
           - ][ -  -  -  
          -  +  -  +  -  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  #  # ][ +  
          -  +  -  +  -  
          +  -  +  -  -  
          -  -  -  -  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  -  -  
          -  -  -  +  -  
          +  -  -  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  +  -  +  -  
          +  -  -  -  -  
          -  -  -  +  -  
          +  -  +  -  -  
             -  -  -  -  
                      - ]
     202                 :             :     const_reference operator[](size_type pos) const  { return vch[pos + m_read_pos]; }
     203         [ +  + ]:     8739600 :     reference operator[](size_type pos)              { return vch[pos + m_read_pos]; }
     204   [ +  +  +  -  :     1252501 :     void clear()                                     { vch.clear(); m_read_pos = 0; }
          +  +  +  -  +  
          +  +  -  +  +  
          +  -  +  +  +  
           -  +  + ][ +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  -  
          #  #  #  #  #  
           # ][ +  +  +  
          +  #  #  #  #  
          #  #  #  #  #  
           #  #  # ][ +  
          -  +  -  +  -  
          +  -  +  -  +  
           - ][ +  -  +  
          -  +  -  +  -  
           +  - ][ +  +  
          +  +  +  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
           - ][ -  +  +  
          -  -  +  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
           - ][ +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
           -  +  - ][ -  
             +  -  +  -  
                      + ]
     205   [ -  +  +  +  :     8246310 :     value_type* data()                               { return vch.data() + m_read_pos; }
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
           #  # ][ -  +  
          -  +  -  -  -  
          -  -  +  -  -  
          -  -  -  -  -  
          -  -  +  -  +  
             -  +  -  + ]
           [ -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
           +  -  + ][ -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  -  -  +  -  
          +  -  +  -  +  
           -  - ][ -  +  
          -  +  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           # ][ -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
           #  # ][ -  +  
          -  +  -  +  -  
          -  -  +  -  -  
          -  +  -  +  -  
           -  -  + ][ -  
          +  -  +  -  +  
             +  +  -  + ]
           [ +  +  #  #  
          #  #  #  #  #  
           # ][ -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
           +  -  + ][ -  
          +  -  +  -  +  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     206         [ -  + ]:       14840 :     const value_type* data() const                   { return vch.data() + m_read_pos; }
     207                 :             : 
     208                 :             :     //
     209                 :             :     // Stream subset
     210                 :             :     //
     211                 :    14393503 :     void read(std::span<value_type> dst)
     212                 :             :     {
     213         [ +  + ]:    14393503 :         if (dst.size() == 0) return;
     214                 :             : 
     215                 :             :         // Read from the beginning of the buffer
     216         [ +  - ]:    14393495 :         auto next_read_pos{CheckedAdd(m_read_pos, dst.size())};
     217   [ +  -  -  +  :    14393495 :         if (!next_read_pos.has_value() || next_read_pos.value() > vch.size()) {
                   +  + ]
     218         [ +  - ]:        4462 :             throw std::ios_base::failure("DataStream::read(): end of data");
     219                 :             :         }
     220         [ -  + ]:    14391264 :         memcpy(dst.data(), &vch[m_read_pos], dst.size());
     221   [ -  +  +  + ]:    14391264 :         if (next_read_pos.value() == vch.size()) {
     222                 :             :             // If fully consumed, reset to empty state.
     223         [ +  - ]:      778552 :             clear();
     224                 :      778552 :             return;
     225                 :             :         }
     226                 :    13612712 :         m_read_pos = next_read_pos.value();
     227                 :             :     }
     228                 :             : 
     229                 :        3255 :     void ignore(size_t num_ignore)
     230                 :             :     {
     231                 :             :         // Ignore from the beginning of the buffer
     232                 :        3255 :         auto next_read_pos{CheckedAdd(m_read_pos, num_ignore)};
     233   [ +  -  -  +  :        3255 :         if (!next_read_pos.has_value() || next_read_pos.value() > vch.size()) {
                   -  + ]
     234         [ #  # ]:           0 :             throw std::ios_base::failure("DataStream::ignore(): end of data");
     235                 :             :         }
     236         [ +  + ]:        3255 :         if (next_read_pos.value() == vch.size()) {
     237                 :             :             // If all bytes are ignored, reset to empty state.
     238         [ +  + ]:           3 :             clear();
     239                 :           3 :             return;
     240                 :             :         }
     241                 :        3252 :         m_read_pos = next_read_pos.value();
     242                 :             :     }
     243                 :             : 
     244                 :    42916384 :     void write(std::span<const value_type> src)
     245                 :             :     {
     246                 :             :         // Write to the end of the buffer
     247                 :    42916384 :         vch.insert(vch.end(), src.begin(), src.end());
     248                 :    42916384 :     }
     249                 :             : 
     250                 :             :     template<typename T>
     251                 :    16781549 :     DataStream& operator<<(const T& obj)
     252                 :             :     {
     253 [ +  - ][ +  -  :    15881618 :         ::Serialize(*this, obj);
          +  -  +  -  +  
          -  +  -  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
           [ +  -  +  -  
          +  -  -  -  -  
          -  -  -  -  -  
          -  -  +  -  +  
          -  +  -  +  -  
          +  -  #  #  #  
           # ][ +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
           [ +  -  +  -  
          +  -  +  -  +  
             -  +  -  +  
           - ][ +  -  +  
             -  +  -  +  
           - ][ +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
           +  - ][ +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          -  -  -  -  -  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  -  
          -  -  -  -  +  
          -  +  -  -  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  +  -  +  
          -  +  -  -  -  
          -  -  -  -  +  
          -  +  -  +  -  
          -  -  -  -  -  
          -  -  -  +  -  
          +  -  +  -  +  
           -  -  - ][ +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  -  -  -  -  
          -  -  -  -  -  
          -  +  -  -  -  
          -  -  -  -  -  
             -  +  -  +  
           - ][ +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  -  - ]
           [ +  -  +  -  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  #  # ][ +  
          -  +  -  +  -  
          +  -  +  -  +  
           - ][ +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
           - ][ +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
             -  +  -  +  
           - ][ +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
           [ +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          -  -  -  -  -  
          -  -  -  -  -  
          +  -  +  -  +  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  -  
          -  -  -  -  +  
          -  +  -  +  -  
          +  -  +  -  -  
                      - ]
     254                 :     7546427 :         return (*this);
     255                 :             :     }
     256                 :             : 
     257                 :             :     template <typename T>
     258                 :     2404867 :     DataStream& operator>>(T&& obj)
     259                 :             :     {
     260         [ +  + ]:     2308877 :         ::Unserialize(*this, obj);
           [ +  -  +  + ]
           [ -  -  -  -  
          -  -  -  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  +  -  -  +  
          -  +  -  +  -  
          -  +  +  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  +  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
           -  -  - ][ +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  +  
          +  -  +  -  +  
          -  +  +  +  -  
             +  -  +  - ]
           [ -  -  +  -  
          -  -  -  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  +  +  -  -  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
           -  +  - ][ +  
          -  +  -  +  -  
          +  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  # ][ +  -  
          +  -  +  -  +  
             -  +  -  +  
           - ][ +  +  +  
          +  +  -  +  -  
          +  -  +  -  +  
             -  +  -  +  
           - ][ +  -  -  
          +  -  +  -  +  
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  -  
          +  +  -  -  +  
          +  -  +  -  -  
          +  -  +  +  -  
           +  - ][ +  -  
          +  -  +  -  +  
          -  +  -  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  # ][ +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     261                 :     1804614 :         return (*this);
     262                 :             :     }
     263                 :             : 
     264                 :             :     /** Compute total memory usage of this object (own memory + any dynamic memory). */
     265                 :             :     size_t GetMemoryUsage() const noexcept;
     266                 :             : };
     267                 :             : 
     268                 :             : template <typename IStream>
     269                 :             : class BitStreamReader
     270                 :             : {
     271                 :             : private:
     272                 :             :     IStream& m_istream;
     273                 :             : 
     274                 :             :     /// Buffered byte read in from the input stream. A new byte is read into the
     275                 :             :     /// buffer when m_offset reaches 8.
     276                 :             :     uint8_t m_buffer{0};
     277                 :             : 
     278                 :             :     /// Number of high order bits in m_buffer already returned by previous
     279                 :             :     /// Read() calls. The next bit to be returned is at this offset from the
     280                 :             :     /// most significant bit position.
     281                 :             :     int m_offset{8};
     282                 :             : 
     283                 :             : public:
     284         [ +  - ]:        1246 :     explicit BitStreamReader(IStream& istream) : m_istream(istream) {}
     285                 :             : 
     286                 :             :     /** Read the specified number of bits from the stream. The data is returned
     287                 :             :      * in the nbits least significant bits of a 64-bit uint.
     288                 :             :      */
     289                 :       25804 :     uint64_t Read(int nbits) {
     290         [ +  - ]:       25804 :         if (nbits < 0 || nbits > 64) {
     291         [ #  # ]:           0 :             throw std::out_of_range("nbits must be between 0 and 64");
     292                 :             :         }
     293                 :             : 
     294                 :             :         uint64_t data = 0;
     295         [ +  + ]:       64668 :         while (nbits > 0) {
     296         [ +  + ]:       38865 :             if (m_offset == 8) {
     297                 :       16722 :                 m_istream >> m_buffer;
     298                 :       16721 :                 m_offset = 0;
     299                 :             :             }
     300                 :             : 
     301         [ +  + ]:       38864 :             int bits = std::min(8 - m_offset, nbits);
     302                 :       38864 :             data <<= bits;
     303                 :       38864 :             data |= static_cast<uint8_t>(m_buffer << m_offset) >> (8 - bits);
     304                 :       38864 :             m_offset += bits;
     305                 :       38864 :             nbits -= bits;
     306                 :             :         }
     307                 :       25803 :         return data;
     308                 :             :     }
     309                 :             : };
     310                 :             : 
     311                 :             : template <typename OStream>
     312                 :             : class BitStreamWriter
     313                 :             : {
     314                 :             : private:
     315                 :             :     OStream& m_ostream;
     316                 :             : 
     317                 :             :     /// Buffered byte waiting to be written to the output stream. The byte is
     318                 :             :     /// written buffer when m_offset reaches 8 or Flush() is called.
     319                 :             :     uint8_t m_buffer{0};
     320                 :             : 
     321                 :             :     /// Number of high order bits in m_buffer already written by previous
     322                 :             :     /// Write() calls and not yet flushed to the stream. The next bit to be
     323                 :             :     /// written to is at this offset from the most significant bit position.
     324                 :             :     int m_offset{0};
     325                 :             : 
     326                 :             : public:
     327         [ +  - ]:       16321 :     explicit BitStreamWriter(OStream& ostream) : m_ostream(ostream) {}
     328                 :             : 
     329                 :       16321 :     ~BitStreamWriter()
     330                 :             :     {
     331                 :       16321 :         Flush();
     332                 :       16321 :     }
     333                 :             : 
     334                 :             :     /** Write the nbits least significant bits of a 64-bit int to the output
     335                 :             :      * stream. Data is buffered until it completes an octet.
     336                 :             :      */
     337                 :       39009 :     void Write(uint64_t data, int nbits) {
     338         [ +  - ]:       39009 :         if (nbits < 0 || nbits > 64) {
     339         [ #  # ]:           0 :             throw std::out_of_range("nbits must be between 0 and 64");
     340                 :             :         }
     341                 :             : 
     342         [ +  + ]:      111613 :         while (nbits > 0) {
     343         [ +  + ]:       72604 :             int bits = std::min(8 - m_offset, nbits);
     344                 :       72604 :             m_buffer |= (data << (64 - nbits)) >> (64 - 8 + m_offset);
     345                 :       72604 :             m_offset += bits;
     346                 :       72604 :             nbits -= bits;
     347                 :             : 
     348         [ +  + ]:       72604 :             if (m_offset == 8) {
     349                 :       33737 :                 Flush();
     350                 :             :             }
     351                 :             :         }
     352                 :       39009 :     }
     353                 :             : 
     354                 :             :     /** Flush any unwritten bits to the output stream, padding with 0's to the
     355                 :             :      * next byte boundary.
     356                 :             :      */
     357                 :       66379 :     void Flush() {
     358         [ +  + ]:       66379 :         if (m_offset == 0) {
     359                 :             :             return;
     360                 :             :         }
     361                 :             : 
     362                 :       49970 :         m_ostream << m_buffer;
     363                 :       49970 :         m_buffer = 0;
     364                 :       49970 :         m_offset = 0;
     365                 :             :     }
     366                 :             : };
     367                 :             : 
     368                 :             : /** Non-refcounted RAII wrapper for FILE*
     369                 :             :  *
     370                 :             :  * Will automatically close the file when it goes out of scope if not null.
     371                 :             :  * If you're returning the file pointer, return file.release().
     372                 :             :  * If you need to close the file early, use autofile.fclose() instead of fclose(underlying_FILE).
     373                 :             :  *
     374                 :             :  * @note If the file has been written to, then the caller must close it
     375                 :             :  * explicitly with the `fclose()` method, check if it returns an error and treat
     376                 :             :  * such an error as if the `write()` method failed. The OS's `fclose(3)` may
     377                 :             :  * fail to flush to disk data that has been previously written, rendering the
     378                 :             :  * file corrupt.
     379                 :             :  */
     380                 :             : class AutoFile
     381                 :             : {
     382                 :             : protected:
     383                 :             :     std::FILE* m_file;
     384                 :             :     Obfuscation m_obfuscation;
     385                 :             :     std::optional<int64_t> m_position;
     386                 :             :     bool m_was_written{false};
     387                 :             : 
     388                 :             : public:
     389                 :             :     explicit AutoFile(std::FILE* file, const Obfuscation& obfuscation = {});
     390                 :             : 
     391                 :      517036 :     ~AutoFile()
     392                 :             :     {
     393         [ +  + ]:      517036 :         if (m_was_written) {
     394                 :             :             // Callers that wrote to the file must have closed it explicitly
     395                 :             :             // with the fclose() method and checked that the close succeeded.
     396                 :             :             // This is because here in the destructor we have no way to signal
     397                 :             :             // errors from fclose() which, after write, could mean the file is
     398                 :             :             // corrupted and must be handled properly at the call site.
     399                 :             :             // Destructors in C++ cannot signal an error to the callers because
     400                 :             :             // they do not return a value and are not allowed to throw exceptions.
     401                 :      279900 :             Assume(IsNull());
     402                 :             :         }
     403                 :             : 
     404         [ -  + ]:      235532 :         if (fclose() != 0) {
     405                 :           0 :             LogError("Failed to close file: %s", SysErrorString(errno));
     406                 :             :         }
     407                 :      517036 :     }
     408                 :             : 
     409                 :             :     // Disallow copies
     410                 :             :     AutoFile(const AutoFile&) = delete;
     411                 :             :     AutoFile& operator=(const AutoFile&) = delete;
     412                 :             : 
     413                 :         245 :     bool feof() const { return std::feof(m_file); }
     414                 :             : 
     415                 :      797032 :     [[nodiscard]] int fclose()
     416                 :             :     {
     417 [ +  - ][ +  -  :      515528 :         if (auto rel{release()}) return std::fclose(rel);
             +  -  +  - ]
           [ +  -  +  - ]
           [ +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
           [ +  -  +  -  
          +  -  +  -  #  
          #  #  #  #  #  
             #  #  #  # ]
     418                 :             :         return 0;
     419                 :             :     }
     420                 :             : 
     421                 :             :     /** Get wrapped FILE* with transfer of ownership.
     422                 :             :      * @note This will invalidate the AutoFile object, and makes it the responsibility of the caller
     423                 :             :      * of this function to clean up the returned FILE*.
     424                 :             :      */
     425                 :      797032 :     std::FILE* release()
     426                 :             :     {
     427                 :      797032 :         std::FILE* ret{m_file};
     428                 :      797032 :         m_file = nullptr;
     429 [ +  + ][ +  -  :      797032 :         return ret;
             -  -  -  - ]
           [ +  -  +  +  
           #  # ][ +  -  
          +  -  +  -  +  
           + ][ +  -  +  
          -  +  -  +  -  
           -  - ][ +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                -  -  - ]
     430                 :             :     }
     431                 :             : 
     432                 :             :     /** Return true if the wrapped FILE* is nullptr, false otherwise.
     433                 :             :      */
     434   [ +  +  +  +  :     1315996 :     bool IsNull() const { return m_file == nullptr; }
           +  + ][ -  + ]
           [ +  +  +  + ]
           [ +  -  +  -  
          -  +  +  +  -  
           +  +  + ][ +  
          +  -  +  +  +  
                   -  + ]
     435                 :             : 
     436                 :             :     /** Continue with a different XOR key */
     437                 :        1430 :     void SetObfuscation(const Obfuscation& obfuscation) { m_obfuscation = obfuscation; }
     438                 :             : 
     439                 :             :     /** Implementation detail, only used internally. */
     440                 :             :     std::size_t detail_fread(std::span<std::byte> dst);
     441                 :             : 
     442                 :             :     /** Wrapper around fseek(). Will throw if seeking is not possible. */
     443                 :             :     void seek(int64_t offset, int origin);
     444                 :             : 
     445                 :             :     /** Find position within the file. Will throw if unknown. */
     446                 :             :     int64_t tell();
     447                 :             : 
     448                 :             :     /** Return the size of the file. Will throw if unknown. */
     449                 :             :     int64_t size();
     450                 :             : 
     451                 :             :     /** Wrapper around FileCommit(). */
     452                 :             :     bool Commit();
     453                 :             : 
     454                 :             :     /** Wrapper around TruncateFile(). */
     455                 :             :     bool Truncate(unsigned size);
     456                 :             : 
     457                 :             :     //! Write a mutable buffer more efficiently than write(), obfuscating the buffer in-place.
     458                 :             :     void write_buffer(std::span<std::byte> src);
     459                 :             : 
     460                 :             :     //
     461                 :             :     // Stream subset
     462                 :             :     //
     463                 :             :     void read(std::span<std::byte> dst);
     464                 :             :     void ignore(size_t nSize);
     465                 :             :     void write(std::span<const std::byte> src);
     466                 :             : 
     467                 :             :     template <typename T>
     468                 :    42088518 :     AutoFile& operator<<(const T& obj)
     469                 :             :     {
     470   [ +  -  +  -  :    42053796 :         ::Serialize(*this, obj);
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
           [ +  -  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
           [ +  -  +  - ]
           [ +  -  +  +  
           +  - ][ +  -  
          +  -  +  -  +  
          -  +  +  +  -  
           +  - ][ +  -  
          +  -  +  -  +  
                      - ]
     471                 :       46928 :         return *this;
     472                 :             :     }
     473                 :             : 
     474                 :             :     template <typename T>
     475                 :    23099791 :     AutoFile& operator>>(T&& obj)
     476                 :             :     {
     477   [ +  -  +  +  :    22917291 :         ::Unserialize(*this, obj);
             +  +  +  + ]
           [ +  +  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
           [ +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  + ]
           [ +  -  +  -  
           +  - ][ +  -  
          +  -  +  -  +  
                -  +  - ]
           [ +  -  +  - ]
           [ +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  -  
           -  - ][ +  -  
          +  -  -  +  -  
          +  +  -  +  -  
          +  -  -  +  +  
           -  -  + ][ #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     478                 :      205991 :         return *this;
     479                 :             :     }
     480                 :             : };
     481                 :             : 
     482                 :             : using DataBuffer = std::vector<std::byte>;
     483                 :             : 
     484                 :             : /** Wrapper around an AutoFile& that implements a ring buffer to
     485                 :             :  *  deserialize from. It guarantees the ability to rewind a given number of bytes.
     486                 :             :  *
     487                 :             :  *  Will automatically close the file when it goes out of scope if not null.
     488                 :             :  *  If you need to close the file early, use file.fclose() instead of fclose(file).
     489                 :             :  */
     490                 :          70 : class BufferedFile
     491                 :             : {
     492                 :             : private:
     493                 :             :     AutoFile& m_src;
     494                 :             :     uint64_t nSrcPos{0};  //!< how many bytes have been read from source
     495                 :             :     uint64_t m_read_pos{0}; //!< how many bytes have been read from this
     496                 :             :     uint64_t nReadLimit;  //!< up to which position we're allowed to read
     497                 :             :     uint64_t nRewind;     //!< how many bytes we guarantee to rewind
     498                 :             :     DataBuffer vchBuf;
     499                 :             : 
     500                 :             :     //! read data from the source to fill the buffer
     501                 :         407 :     bool Fill() {
     502         [ -  + ]:         407 :         unsigned int pos = nSrcPos % vchBuf.size();
     503                 :         407 :         unsigned int readNow = vchBuf.size() - pos;
     504                 :         407 :         unsigned int nAvail = vchBuf.size() - (nSrcPos - m_read_pos) - nRewind;
     505         [ +  + ]:         407 :         if (nAvail < readNow)
     506                 :         340 :             readNow = nAvail;
     507         [ +  - ]:         407 :         if (readNow == 0)
     508                 :             :             return false;
     509                 :         407 :         size_t nBytes{m_src.detail_fread(std::span{vchBuf}.subspan(pos, readNow))};
     510         [ +  + ]:         407 :         if (nBytes == 0) {
     511   [ -  +  +  - ]:          32 :             throw std::ios_base::failure{m_src.feof() ? "BufferedFile::Fill: end of file" : "BufferedFile::Fill: fread failed"};
     512                 :             :         }
     513                 :         391 :         nSrcPos += nBytes;
     514                 :         391 :         return true;
     515                 :             :     }
     516                 :             : 
     517                 :             :     //! Advance the stream's read pointer (m_read_pos) by up to 'length' bytes,
     518                 :             :     //! filling the buffer from the file so that at least one byte is available.
     519                 :             :     //! Return a pointer to the available buffer data and the number of bytes
     520                 :             :     //! (which may be less than the requested length) that may be accessed
     521                 :             :     //! beginning at that pointer.
     522                 :       80018 :     std::pair<std::byte*, size_t> AdvanceStream(size_t length)
     523                 :             :     {
     524         [ -  + ]:       80018 :         assert(m_read_pos <= nSrcPos);
     525         [ +  + ]:       80018 :         if (m_read_pos + length > nReadLimit) {
     526         [ +  - ]:           4 :             throw std::ios_base::failure("Attempt to position past buffer limit");
     527                 :             :         }
     528                 :             :         // If there are no bytes available, read from the file.
     529   [ +  +  +  - ]:       80016 :         if (m_read_pos == nSrcPos && length > 0) Fill();
     530                 :             : 
     531         [ -  + ]:       80015 :         size_t buffer_offset{static_cast<size_t>(m_read_pos % vchBuf.size())};
     532                 :       80015 :         size_t buffer_available{static_cast<size_t>(vchBuf.size() - buffer_offset)};
     533                 :       80015 :         size_t bytes_until_source_pos{static_cast<size_t>(nSrcPos - m_read_pos)};
     534                 :       80015 :         size_t advance{std::min({length, buffer_available, bytes_until_source_pos})};
     535                 :       80015 :         m_read_pos += advance;
     536                 :       80015 :         return std::make_pair(&vchBuf[buffer_offset], advance);
     537                 :             :     }
     538                 :             : 
     539                 :             : public:
     540                 :          71 :     BufferedFile(AutoFile& file LIFETIMEBOUND, uint64_t nBufSize, uint64_t nRewindIn)
     541                 :          71 :         : m_src{file}, nReadLimit{std::numeric_limits<uint64_t>::max()}, nRewind{nRewindIn}, vchBuf(nBufSize, std::byte{0})
     542                 :             :     {
     543         [ +  + ]:          71 :         if (nRewindIn >= nBufSize)
     544         [ +  - ]:           2 :             throw std::ios_base::failure("Rewind limit must be less than buffer size");
     545                 :          71 :     }
     546                 :             : 
     547                 :             :     //! check whether we're at the end of the source file
     548                 :        6291 :     bool eof() const {
     549   [ +  +  +  + ]:        6291 :         return m_read_pos == nSrcPos && m_src.feof();
     550                 :             :     }
     551                 :             : 
     552                 :             :     //! read a number of bytes
     553                 :       77017 :     void read(std::span<std::byte> dst)
     554                 :             :     {
     555         [ +  + ]:      154131 :         while (dst.size() > 0) {
     556                 :       77116 :             auto [buffer_pointer, length]{AdvanceStream(dst.size())};
     557                 :       77114 :             memcpy(dst.data(), buffer_pointer, length);
     558                 :       77114 :             dst = dst.subspan(length);
     559                 :             :         }
     560                 :       77015 :     }
     561                 :             : 
     562                 :             :     //! Move the read position ahead in the stream to the given position.
     563                 :             :     //! Use SetPos() to back up in the stream, not SkipTo().
     564                 :        3009 :     void SkipTo(const uint64_t file_pos)
     565                 :             :     {
     566         [ +  - ]:        3009 :         assert(file_pos >= m_read_pos);
     567         [ +  + ]:        5910 :         while (m_read_pos < file_pos) AdvanceStream(file_pos - m_read_pos);
     568                 :        3008 :     }
     569                 :             : 
     570                 :             :     //! return the current reading position
     571                 :       11990 :     uint64_t GetPos() const {
     572   [ +  -  +  +  :       11990 :         return m_read_pos;
           +  - ][ +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                -  +  - ]
     573                 :             :     }
     574                 :             : 
     575                 :             :     //! rewind to a given reading position
     576                 :        5065 :     bool SetPos(uint64_t nPos) {
     577 [ -  + ][ -  +  :        5065 :         size_t bufsize = vchBuf.size();
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
                +  -  + ]
     578 [ -  + ][ +  +  :        5065 :         if (nPos + bufsize < nSrcPos) {
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
                +  +  - ]
     579                 :             :             // rewinding too far, rewind as far as possible
     580                 :          67 :             m_read_pos = nSrcPos - bufsize;
     581                 :          67 :             return false;
     582                 :             :         }
     583 [ -  + ][ +  +  :        4995 :         if (nPos > nSrcPos) {
          -  +  -  +  -  
             +  -  +  -  
                      + ]
     584                 :             :             // can't go this far forward, go as far as possible
     585                 :          17 :             m_read_pos = nSrcPos;
     586                 :          17 :             return false;
     587                 :             :         }
     588                 :        4981 :         m_read_pos = nPos;
     589                 :        4981 :         return true;
     590                 :             :     }
     591                 :             : 
     592                 :             :     //! prevent reading beyond a certain position
     593                 :             :     //! no argument removes the limit
     594                 :        7990 :     bool SetLimit(uint64_t nPos = std::numeric_limits<uint64_t>::max()) {
     595 [ +  - ][ +  -  :        5583 :         if (nPos < m_read_pos)
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     596                 :             :             return false;
     597                 :        7990 :         nReadLimit = nPos;
     598         [ +  + ]:        7990 :         return true;
     599                 :             :     }
     600                 :             : 
     601                 :             :     template<typename T>
     602                 :       11758 :     BufferedFile& operator>>(T&& obj) {
     603   [ +  -  +  -  :       11758 :         ::Unserialize(*this, obj);
             +  -  +  - ]
           [ +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          -  +  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     604                 :       11756 :         return (*this);
     605                 :             :     }
     606                 :             : 
     607                 :             :     //! search for a given byte in the stream, and remain positioned on it
     608                 :        3056 :     void FindByte(std::byte byte)
     609                 :             :     {
     610                 :             :         // For best performance, avoid mod operation within the loop.
     611         [ -  + ]:        3056 :         size_t buf_offset{size_t(m_read_pos % uint64_t(vchBuf.size()))};
     612                 :        3229 :         while (true) {
     613         [ +  + ]:        3229 :             if (m_read_pos == nSrcPos) {
     614                 :             :                 // No more bytes available; read from the file into the buffer,
     615                 :             :                 // setting nSrcPos to one beyond the end of the new data.
     616                 :             :                 // Throws exception if end-of-file reached.
     617                 :         204 :                 Fill();
     618                 :             :             }
     619   [ -  +  +  + ]:        3214 :             const size_t len{std::min<size_t>(vchBuf.size() - buf_offset, nSrcPos - m_read_pos)};
     620         [ +  + ]:        3214 :             const auto it_start{vchBuf.begin() + buf_offset};
     621                 :        3214 :             const auto it_find{std::find(it_start, it_start + len, byte)};
     622         [ +  + ]:        3214 :             const size_t inc{size_t(std::distance(it_start, it_find))};
     623                 :        3214 :             m_read_pos += inc;
     624         [ +  + ]:        3214 :             if (inc < len) break;
     625                 :         173 :             buf_offset += inc;
     626         [ +  + ]:         173 :             if (buf_offset >= vchBuf.size()) buf_offset = 0;
     627                 :             :         }
     628                 :        3041 :     }
     629                 :             : };
     630                 :             : 
     631                 :             : /**
     632                 :             :  * Wrapper that buffers reads from an underlying stream.
     633                 :             :  * Requires underlying stream to support read() and detail_fread() calls
     634                 :             :  * to support fixed-size and variable-sized reads, respectively.
     635                 :             :  */
     636                 :             : template <typename S>
     637                 :       56298 : class BufferedReader
     638                 :             : {
     639                 :             :     S& m_src;
     640                 :             :     DataBuffer m_buf;
     641                 :             :     size_t m_buf_pos;
     642                 :             : 
     643                 :             : public:
     644                 :             :     //! Requires stream ownership to prevent leaving the stream at an unexpected position after buffered reads.
     645                 :       56298 :     explicit BufferedReader(S&& stream LIFETIMEBOUND, size_t size = 1 << 16)
     646                 :             :         requires std::is_rvalue_reference_v<S&&>
     647   [ +  -  +  - ]:       56298 :         : m_src{stream}, m_buf(size), m_buf_pos{size} {}
           [ +  -  +  -  
                   +  - ]
     648                 :             : 
     649                 :      338952 :     void read(std::span<std::byte> dst)
     650                 :             :     {
     651   [ -  +  +  +  :      621603 :         if (const auto available{std::min(dst.size(), m_buf.size() - m_buf_pos)}) {
                   +  + ]
     652                 :      282652 :             std::copy_n(m_buf.begin() + m_buf_pos, available, dst.begin());
     653                 :      282652 :             m_buf_pos += available;
     654                 :      282652 :             dst = dst.subspan(available);
     655                 :             :         }
     656         [ +  + ]:      338952 :         if (dst.size()) {
     657   [ -  +  -  + ]:       56301 :             assert(m_buf_pos == m_buf.size());
     658                 :       56301 :             m_src.read(dst);
     659                 :             : 
     660                 :       56298 :             m_buf_pos = 0;
     661         [ -  + ]:       56298 :             m_buf.resize(m_src.detail_fread(m_buf));
     662                 :             :         }
     663                 :      338949 :     }
     664                 :             : 
     665                 :             :     template <typename T>
     666                 :       56297 :     BufferedReader& operator>>(T&& obj)
     667                 :             :     {
     668         [ +  - ]:       56296 :         Unserialize(*this, obj);
           [ +  -  +  - ]
     669                 :       56296 :         return *this;
     670                 :             :     }
     671                 :             : };
     672                 :             : 
     673                 :             : /**
     674                 :             :  * Wrapper that buffers writes to an underlying stream.
     675                 :             :  * Requires underlying stream to support write_buffer() method
     676                 :             :  * for efficient buffer flushing and obfuscation.
     677                 :             :  */
     678                 :             : template <typename S>
     679                 :             : class BufferedWriter
     680                 :             : {
     681                 :             :     S& m_dst;
     682                 :             :     DataBuffer m_buf;
     683                 :             :     size_t m_buf_pos{0};
     684                 :             : 
     685                 :             : public:
     686   [ +  -  +  -  :      258532 :     explicit BufferedWriter(S& stream LIFETIMEBOUND, size_t size = 1 << 16) : m_dst{stream}, m_buf(size) {}
             +  -  +  - ]
           [ +  -  +  -  
                   +  - ]
     687                 :             : 
     688   [ +  -  +  - ]:      258532 :     ~BufferedWriter() { flush(); }
     689                 :             : 
     690                 :      367516 :     void flush()
     691                 :             :     {
     692   [ +  +  -  + ]:      367516 :         if (m_buf_pos) m_dst.write_buffer(std::span{m_buf}.first(m_buf_pos));
     693                 :      367516 :         m_buf_pos = 0;
     694                 :      367516 :     }
     695                 :             : 
     696                 :     6888647 :     void write(std::span<const std::byte> src)
     697                 :             :     {
     698   [ -  +  +  +  :    27663559 :         while (const auto available{std::min(src.size(), m_buf.size() - m_buf_pos)}) {
                   +  + ]
     699                 :     6997620 :             std::copy_n(src.begin(), available, m_buf.begin() + m_buf_pos);
     700                 :     6997620 :             m_buf_pos += available;
     701   [ -  +  +  + ]:     6997620 :             if (m_buf_pos == m_buf.size()) flush();
     702                 :     6997620 :             src = src.subspan(available);
     703                 :             :         }
     704                 :     6888647 :     }
     705                 :             : 
     706                 :             :     template <typename T>
     707                 :     1223749 :     BufferedWriter& operator<<(const T& obj)
     708                 :             :     {
     709   [ +  -  +  -  :      965218 :         Serialize(*this, obj);
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
           [ +  -  +  - ]
     710                 :      642672 :         return *this;
     711                 :             :     }
     712                 :             : };
     713                 :             : 
     714                 :             : #endif // BITCOIN_STREAMS_H
        

Generated by: LCOV version 2.0-1