LCOV - code coverage report
Current view: top level - src - streams.h (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 98.1 % 214 210
Test Date: 2024-08-28 04:44:32 Functions: 96.3 % 27 26
Branches: 26.5 % 4306 1143

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2                 :             : // Copyright (c) 2009-2022 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/overflow.h>
      13                 :             : 
      14                 :             : #include <algorithm>
      15                 :             : #include <assert.h>
      16                 :             : #include <cstddef>
      17                 :             : #include <cstdio>
      18                 :             : #include <ios>
      19                 :             : #include <limits>
      20                 :             : #include <optional>
      21                 :             : #include <stdint.h>
      22                 :             : #include <string.h>
      23                 :             : #include <string>
      24                 :             : #include <utility>
      25                 :             : #include <vector>
      26                 :             : 
      27                 :             : namespace util {
      28                 :      403037 : inline void Xor(Span<std::byte> write, Span<const std::byte> key, size_t key_offset = 0)
      29                 :             : {
      30         [ +  - ]:      403037 :     if (key.size() == 0) {
      31                 :             :         return;
      32                 :             :     }
      33                 :      403037 :     key_offset %= key.size();
      34                 :             : 
      35         [ +  + ]:     7650598 :     for (size_t i = 0, j = key_offset; i != write.size(); i++) {
      36         [ +  + ]:     7247561 :         write[i] ^= key[j++];
      37                 :             : 
      38                 :             :         // This potentially acts on very many bytes of data, so it's
      39                 :             :         // important that we calculate `j`, i.e. the `key` index in this
      40                 :             :         // way instead of doing a %, which would effectively be a division
      41                 :             :         // for each byte Xor'd -- much slower than need be.
      42         [ +  + ]:     7247561 :         if (j == key.size())
      43                 :      860705 :             j = 0;
      44                 :             :     }
      45                 :             : }
      46                 :             : } // namespace util
      47                 :             : 
      48                 :             : /* Minimal stream for overwriting and/or appending to an existing byte vector
      49                 :             :  *
      50                 :             :  * The referenced vector will grow as necessary
      51                 :             :  */
      52                 :             : class VectorWriter
      53                 :             : {
      54                 :             : public:
      55                 :             : /*
      56                 :             :  * @param[in]  vchDataIn  Referenced byte vector to overwrite/append
      57                 :             :  * @param[in]  nPosIn Starting position. Vector index where writes should start. The vector will initially
      58                 :             :  *                    grow as necessary to max(nPosIn, vec.size()). So to append, use vec.size().
      59                 :             : */
      60                 :         303 :     VectorWriter(std::vector<unsigned char>& vchDataIn, size_t nPosIn) : vchData{vchDataIn}, nPos{nPosIn}
      61                 :             :     {
      62         [ +  + ]:         303 :         if(nPos > vchData.size())
      63                 :           1 :             vchData.resize(nPos);
      64                 :         303 :     }
      65                 :             : /*
      66                 :             :  * (other params same as above)
      67                 :             :  * @param[in]  args  A list of items to serialize starting at nPosIn.
      68                 :             : */
      69                 :             :     template <typename... Args>
      70   [ +  -  +  -  :          38 :     VectorWriter(std::vector<unsigned char>& vchDataIn, size_t nPosIn, Args&&... args) : VectorWriter{vchDataIn, nPosIn}
          +  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  +  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  +  
             -  +  -  -  
              - ][ +  - ]
           [ +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
      71                 :             :     {
      72   [ +  -  +  -  :          38 :         ::SerializeMany(*this, std::forward<Args>(args)...);
          +  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  +  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  +  
             -  +  -  -  
              - ][ +  - ]
           [ +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
      73                 :          30 :     }
      74                 :        1465 :     void write(Span<const std::byte> src)
      75                 :             :     {
      76         [ -  + ]:        1465 :         assert(nPos <= vchData.size());
      77         [ +  + ]:        1465 :         size_t nOverwrite = std::min(src.size(), vchData.size() - nPos);
      78         [ +  + ]:        1465 :         if (nOverwrite) {
      79                 :          19 :             memcpy(vchData.data() + nPos, src.data(), nOverwrite);
      80                 :             :         }
      81         [ +  + ]:        1465 :         if (nOverwrite < src.size()) {
      82                 :        1447 :             vchData.insert(vchData.end(), UCharCast(src.data()) + nOverwrite, UCharCast(src.end()));
      83                 :             :         }
      84                 :        1465 :         nPos += src.size();
      85                 :        1465 :     }
      86                 :             :     template <typename T>
      87   [ #  #  #  # ]:        1081 :     VectorWriter& operator<<(const T& obj)
           [ +  -  +  -  
           +  - ][ +  - ]
      88                 :             :     {
      89   [ +  -  +  -  :        2075 :         ::Serialize(*this, obj);
          +  -  +  -  +  
           - ][ +  -  +  
          -  +  -  +  -  
          #  #  #  #  #  
           # ][ +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
           - ][ #  #  #  
             #  #  #  #  
                      # ]
      90                 :         163 :         return (*this);
      91                 :             :     }
      92                 :             : 
      93                 :             : private:
      94                 :             :     std::vector<unsigned char>& vchData;
      95                 :             :     size_t nPos;
      96                 :             : };
      97                 :             : 
      98                 :             : /** Minimal stream for reading from an existing byte array by Span.
      99                 :             :  */
     100                 :             : class SpanReader
     101                 :             : {
     102                 :             : private:
     103                 :             :     Span<const unsigned char> m_data;
     104                 :             : 
     105                 :             : public:
     106                 :             :     /**
     107                 :             :      * @param[in]  data Referenced byte vector to overwrite/append
     108                 :             :      */
     109   [ +  -  #  #  :         573 :     explicit SpanReader(Span<const unsigned char> data) : m_data{data} {}
          #  #  #  #  #  
           # ][ -  -  -  
          -  -  -  -  -  
           +  - ][ +  -  
             +  -  #  # ]
           [ +  -  +  -  
           +  - ][ #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
     110                 :             : 
     111                 :             :     template<typename T>
     112   [ #  #  #  #  :       13964 :     SpanReader& operator>>(T&& obj)
           #  # ][ +  -  
          +  -  +  -  -  
             +  +  -  -  
                      + ]
     113                 :             :     {
     114   [ #  #  #  #  :       13963 :         ::Unserialize(*this, obj);
          #  #  #  #  #  
          #  #  #  #  #  
           #  # ][ -  + ]
           [ +  -  +  +  
          #  #  #  #  #  
             #  #  #  #  
           # ][ +  -  -  
           -  -  - ][ +  
          +  +  -  +  -  
          +  -  +  -  +  
           -  +  - ][ #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
     115                 :         207 :         return (*this);
     116                 :             :     }
     117                 :             : 
     118   [ +  -  +  -  :           5 :     size_t size() const { return m_data.size(); }
          +  -  +  -  +  
                      - ]
     119   [ +  +  #  # ]:          23 :     bool empty() const { return m_data.empty(); }
           [ -  +  -  + ]
           [ +  -  +  -  
          +  -  +  -  +  
                -  +  - ]
     120                 :             : 
     121                 :       14561 :     void read(Span<std::byte> dst)
     122                 :             :     {
     123         [ +  - ]:       14561 :         if (dst.size() == 0) {
     124                 :             :             return;
     125                 :             :         }
     126                 :             : 
     127                 :             :         // Read from the beginning of the buffer
     128         [ +  + ]:       14561 :         if (dst.size() > m_data.size()) {
     129         [ +  - ]:          10 :             throw std::ios_base::failure("SpanReader::read(): end of data");
     130                 :             :         }
     131                 :       14551 :         memcpy(dst.data(), m_data.data(), dst.size());
     132                 :       14551 :         m_data = m_data.subspan(dst.size());
     133                 :             :     }
     134                 :             : 
     135                 :             :     void ignore(size_t n)
     136                 :             :     {
     137                 :             :         m_data = m_data.subspan(n);
     138                 :             :     }
     139                 :             : };
     140                 :             : 
     141                 :             : /** Double ended buffer combining vector and stream-like interfaces.
     142                 :             :  *
     143                 :             :  * >> and << read and write unformatted data using the above serialization templates.
     144                 :             :  * Fills with data in linear time; some stringstream implementations take N^2 time.
     145                 :             :  */
     146   [ +  +  +  +  :     4553308 : class DataStream
           #  # ][ +  -  
             +  -  +  - ]
           [ +  -  #  # ]
           [ +  -  +  -  
             +  -  +  - ]
     147                 :             : {
     148                 :             : protected:
     149                 :             :     using vector_type = SerializeData;
     150                 :             :     vector_type vch;
     151                 :             :     vector_type::size_type m_read_pos{0};
     152                 :             : 
     153                 :             : public:
     154                 :             :     typedef vector_type::allocator_type   allocator_type;
     155                 :             :     typedef vector_type::size_type        size_type;
     156                 :             :     typedef vector_type::difference_type  difference_type;
     157                 :             :     typedef vector_type::reference        reference;
     158                 :             :     typedef vector_type::const_reference  const_reference;
     159                 :             :     typedef vector_type::value_type       value_type;
     160                 :             :     typedef vector_type::iterator         iterator;
     161                 :             :     typedef vector_type::const_iterator   const_iterator;
     162                 :             :     typedef vector_type::reverse_iterator reverse_iterator;
     163                 :             : 
     164         [ +  - ]:        1723 :     explicit DataStream() = default;
     165                 :         781 :     explicit DataStream(Span<const uint8_t> sp) : DataStream{AsBytes(sp)} {}
     166                 :       90035 :     explicit DataStream(Span<const value_type> sp) : vch(sp.data(), sp.data() + sp.size()) {}
     167                 :             : 
     168                 :          19 :     std::string str() const
     169                 :             :     {
     170                 :          38 :         return std::string{UCharCast(data()), UCharCast(data() + size())};
     171                 :             :     }
     172                 :             : 
     173                 :             : 
     174                 :             :     //
     175                 :             :     // Vector subset
     176                 :             :     //
     177                 :             :     const_iterator begin() const                     { return vch.begin() + m_read_pos; }
     178                 :       95741 :     iterator begin()                                 { return vch.begin() + m_read_pos; }
     179                 :             :     const_iterator end() const                       { return vch.end(); }
     180         [ +  - ]:       50457 :     iterator end()                                   { return vch.end(); }
           [ +  -  +  - ]
     181 [ +  + ][ #  #  :    12207811 :     size_type size() const                           { return vch.size() - m_read_pos; }
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  # ][ +  -  
          +  -  +  -  +  
          -  +  -  #  #  
          #  #  #  #  #  
             #  #  #  #  
           # ][ +  -  +  
          -  +  -  +  -  
          #  #  #  #  #  
           # ][ -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
                +  +  - ]
           [ +  -  +  - ]
           [ -  -  +  -  
          -  -  -  -  -  
           -  +  - ][ #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
           # ][ +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
           - ][ +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
           -  +  - ][ #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
           # ][ #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
     182   [ +  -  +  -  :        1353 :     bool empty() const                               { return vch.size() == m_read_pos; }
          #  #  #  #  #  
           #  #  # ][ #  
          #  #  #  #  #  
          #  #  #  #  #  
           # ][ +  +  +  
             +  +  +  +  
           + ][ +  -  +  
          -  +  -  +  -  
           +  + ][ -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
             -  +  -  +  
           - ][ #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  #  # ][ #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
     183                 :         309 :     void resize(size_type n, value_type c = value_type{}) { vch.resize(n + m_read_pos, c); }
     184   [ +  -  #  #  :     4277505 :     void reserve(size_type n)                        { vch.reserve(n + m_read_pos); }
          #  #  #  #  #  
          #  #  #  #  #  
           #  # ][ +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  -  -  
          -  -  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          -  -  -  -  -  
          -  +  -  +  -  
          -  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  -  
          -  -  -  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
           -  -  - ][ +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
           - ][ #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
           [ +  -  +  -  
          +  -  +  -  +  
           - ][ -  -  +  
          -  -  -  -  -  
             -  -  +  - ]
           [ -  -  +  -  
          +  -  +  -  -  
          -  +  -  +  -  
           -  - ][ +  -  
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     185                 :             :     const_reference operator[](size_type pos) const  { return vch[pos + m_read_pos]; }
     186         [ +  + ]:     7633159 :     reference operator[](size_type pos)              { return vch[pos + m_read_pos]; }
     187   [ +  +  +  +  :       86504 :     void clear()                                     { vch.clear(); m_read_pos = 0; }
          #  #  #  #  #  
          #  #  #  #  #  
           #  # ][ -  +  
             -  +  -  + ]
           [ -  +  +  -  
          +  +  +  -  +  
          +  +  -  +  -  
           -  - ][ -  +  
          +  -  -  +  +  
           + ][ -  +  +  
          -  -  +  +  -  
          -  +  +  -  +  
          +  +  -  +  +  
             +  -  +  + ]
           [ +  -  +  -  
          +  -  +  -  +  
           -  +  - ][ -  
          +  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
           -  +  - ][ +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  #  # ][ +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     188 [ +  - ][ +  -  :     4209083 :     value_type* data()                               { return vch.data() + m_read_pos; }
          +  -  +  -  +  
          -  +  -  #  #  
          #  #  #  #  #  
             #  #  #  #  
           # ][ +  -  +  
          -  +  -  +  -  
          #  #  #  #  #  
           # ][ +  -  +  
          -  #  #  #  #  
           #  # ][ -  -  
          +  -  -  -  -  
             -  -  -  +  
           - ][ #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  #  # ][ +  
          -  +  -  +  -  
          +  -  +  -  +  
           -  +  - ][ +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
           - ][ -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
           -  +  - ][ #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
     189                 :          20 :     const value_type* data() const                   { return vch.data() + m_read_pos; }
     190                 :             : 
     191                 :             :     inline void Compact()
     192                 :             :     {
     193                 :             :         vch.erase(vch.begin(), vch.begin() + m_read_pos);
     194                 :             :         m_read_pos = 0;
     195                 :             :     }
     196                 :             : 
     197                 :             :     bool Rewind(std::optional<size_type> n = std::nullopt)
     198                 :             :     {
     199                 :             :         // Total rewind if no size is passed
     200                 :             :         if (!n) {
     201                 :             :             m_read_pos = 0;
     202                 :             :             return true;
     203                 :             :         }
     204                 :             :         // Rewind by n characters if the buffer hasn't been compacted yet
     205                 :             :         if (*n > m_read_pos)
     206                 :             :             return false;
     207                 :             :         m_read_pos -= *n;
     208                 :             :         return true;
     209                 :             :     }
     210                 :             : 
     211                 :             : 
     212                 :             :     //
     213                 :             :     // Stream subset
     214                 :             :     //
     215         [ +  + ]:       23503 :     bool eof() const             { return size() == 0; }
     216         [ #  # ]:           0 :     int in_avail() const         { return size(); }
     217                 :             : 
     218                 :     1447865 :     void read(Span<value_type> dst)
     219                 :             :     {
     220         [ +  + ]:     1447865 :         if (dst.size() == 0) return;
     221                 :             : 
     222                 :             :         // Read from the beginning of the buffer
     223                 :     1446874 :         auto next_read_pos{CheckedAdd(m_read_pos, dst.size())};
     224   [ +  -  +  + ]:     1446874 :         if (!next_read_pos.has_value() || next_read_pos.value() > vch.size()) {
     225         [ +  - ]:          30 :             throw std::ios_base::failure("DataStream::read(): end of data");
     226                 :             :         }
     227         [ +  + ]:     1446844 :         memcpy(dst.data(), &vch[m_read_pos], dst.size());
     228         [ +  + ]:     1446844 :         if (next_read_pos.value() == vch.size()) {
     229                 :      131126 :             m_read_pos = 0;
     230         [ +  - ]:      131126 :             vch.clear();
     231                 :      131126 :             return;
     232                 :             :         }
     233                 :     1315718 :         m_read_pos = next_read_pos.value();
     234                 :             :     }
     235                 :             : 
     236                 :           7 :     void ignore(size_t num_ignore)
     237                 :             :     {
     238                 :             :         // Ignore from the beginning of the buffer
     239                 :           7 :         auto next_read_pos{CheckedAdd(m_read_pos, num_ignore)};
     240   [ +  -  +  + ]:           7 :         if (!next_read_pos.has_value() || next_read_pos.value() > vch.size()) {
     241         [ +  - ]:           1 :             throw std::ios_base::failure("DataStream::ignore(): end of data");
     242                 :             :         }
     243         [ +  + ]:           6 :         if (next_read_pos.value() == vch.size()) {
     244                 :           3 :             m_read_pos = 0;
     245         [ +  + ]:           3 :             vch.clear();
     246                 :           3 :             return;
     247                 :             :         }
     248                 :           3 :         m_read_pos = next_read_pos.value();
     249                 :             :     }
     250                 :             : 
     251                 :    25041445 :     void write(Span<const value_type> src)
     252                 :             :     {
     253                 :             :         // Write to the end of the buffer
     254                 :    25041445 :         vch.insert(vch.end(), src.begin(), src.end());
     255                 :    25041445 :     }
     256                 :             : 
     257                 :             :     template<typename T>
     258   [ +  -  +  -  :      581702 :     DataStream& operator<<(const T& obj)
             +  -  +  - ]
           [ +  -  +  -  
             #  #  #  # ]
           [ +  -  +  -  
          +  -  +  -  +  
                      - ]
     259                 :             :     {
     260 [ +  - ][ +  -  :     9259489 :         ::Serialize(*this, obj);
          +  -  +  -  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
           [ +  -  +  -  
          +  -  +  -  +  
             -  +  -  +  
           - ][ +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          -  -  -  -  -  
          -  -  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          -  -  -  -  -  
          -  +  -  +  -  
          -  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  -  -  
          -  -  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  -  +  
          -  +  -  +  -  
          +  -  +  -  -  
           - ][ +  -  +  
          -  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  # ][ +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  # ][ +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
           - ][ -  -  -  
          -  +  -  +  -  
          -  -  -  -  -  
          -  +  -  #  #  
             #  #  #  # ]
           [ +  -  +  -  
          +  -  -  -  -  
          -  +  -  +  -  
          +  -  +  -  +  
          -  -  -  -  -  
          +  -  +  -  -  
           - ][ +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
           [ +  -  +  -  
          +  -  +  -  +  
           -  +  - ][ +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
           +  - ][ +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
           # ][ +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
           [ +  -  +  -  
          +  -  +  -  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  #  # ][ #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
           # ][ #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
           [ #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
           [ #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
     261                 :     4914002 :         return (*this);
     262                 :             :     }
     263                 :             : 
     264                 :             :     template<typename T>
     265 [ +  - ][ +  -  :      463498 :     DataStream& operator>>(T&& obj)
          +  -  +  -  +  
          -  +  -  +  -  
          -  -  -  -  -  
          -  -  -  -  -  
             -  -  -  - ]
           [ -  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          -  -  -  -  -  
          -  -  +  -  -  
           +  - ][ +  -  
          +  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
           [ +  -  +  -  
          +  -  +  -  +  
           - ][ #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
           # ][ #  #  #  
                #  #  # ]
     266                 :             :     {
     267   [ +  +  #  #  :      535456 :         ::Unserialize(*this, obj);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           # ][ +  -  +  
             +  +  +  +  
           + ][ #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  # ][ +  -  
          -  +  +  -  +  
          +  +  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
             -  -  -  - ]
           [ -  -  -  -  
          -  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  -  -  -  -  
          -  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  +  -  -  
          +  -  +  -  +  
          -  -  +  +  -  
          -  -  -  -  -  
          -  -  -  -  -  
          +  -  -  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  -  
             -  -  -  - ]
           [ +  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          +  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  +  
          -  -  -  -  -  
          +  -  +  -  +  
          -  +  -  +  -  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
           [ +  -  +  +  
             +  -  #  # ]
           [ +  -  +  -  
          +  -  +  -  -  
          -  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
           # ][ +  -  +  
          -  +  -  +  -  
          +  +  +  +  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
           [ +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
             -  +  -  +  
           - ][ +  -  +  
          -  #  #  #  #  
           #  # ][ +  +  
          +  +  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
           - ][ +  -  +  
          -  -  +  -  +  
          -  +  +  -  +  
          -  -  +  +  -  
          +  -  +  -  +  
          -  -  +  +  -  
          -  +  +  -  +  
          -  -  +  -  +  
             +  -  +  - ]
           [ +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  #  # ][ #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  #  # ][ #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
           # ][ #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
           [ #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
     268                 :      325197 :         return (*this);
     269                 :             :     }
     270                 :             : 
     271                 :             :     /**
     272                 :             :      * XOR the contents of this stream with a certain key.
     273                 :             :      *
     274                 :             :      * @param[in] key    The key used to XOR the data in this stream.
     275                 :             :      */
     276                 :      103496 :     void Xor(const std::vector<unsigned char>& key)
     277                 :             :     {
     278                 :      103496 :         util::Xor(MakeWritableByteSpan(*this), MakeByteSpan(key));
     279                 :      103496 :     }
     280                 :             : };
     281                 :             : 
     282                 :             : template <typename IStream>
     283                 :             : class BitStreamReader
     284                 :             : {
     285                 :             : private:
     286                 :             :     IStream& m_istream;
     287                 :             : 
     288                 :             :     /// Buffered byte read in from the input stream. A new byte is read into the
     289                 :             :     /// buffer when m_offset reaches 8.
     290                 :             :     uint8_t m_buffer{0};
     291                 :             : 
     292                 :             :     /// Number of high order bits in m_buffer already returned by previous
     293                 :             :     /// Read() calls. The next bit to be returned is at this offset from the
     294                 :             :     /// most significant bit position.
     295                 :             :     int m_offset{8};
     296                 :             : 
     297                 :             : public:
     298         [ +  - ]:         211 :     explicit BitStreamReader(IStream& istream) : m_istream(istream) {}
     299                 :             : 
     300                 :             :     /** Read the specified number of bits from the stream. The data is returned
     301                 :             :      * in the nbits least significant bits of a 64-bit uint.
     302                 :             :      */
     303                 :       23682 :     uint64_t Read(int nbits) {
     304         [ +  - ]:       23682 :         if (nbits < 0 || nbits > 64) {
     305         [ #  # ]:           0 :             throw std::out_of_range("nbits must be between 0 and 64");
     306                 :             :         }
     307                 :             : 
     308                 :             :         uint64_t data = 0;
     309         [ +  + ]:       58458 :         while (nbits > 0) {
     310         [ +  + ]:       34777 :             if (m_offset == 8) {
     311                 :       13750 :                 m_istream >> m_buffer;
     312                 :       13749 :                 m_offset = 0;
     313                 :             :             }
     314                 :             : 
     315         [ +  + ]:       34776 :             int bits = std::min(8 - m_offset, nbits);
     316                 :       34776 :             data <<= bits;
     317                 :       34776 :             data |= static_cast<uint8_t>(m_buffer << m_offset) >> (8 - bits);
     318                 :       34776 :             m_offset += bits;
     319                 :       34776 :             nbits -= bits;
     320                 :             :         }
     321                 :       23681 :         return data;
     322                 :             :     }
     323                 :             : };
     324                 :             : 
     325                 :             : template <typename OStream>
     326                 :             : class BitStreamWriter
     327                 :             : {
     328                 :             : private:
     329                 :             :     OStream& m_ostream;
     330                 :             : 
     331                 :             :     /// Buffered byte waiting to be written to the output stream. The byte is
     332                 :             :     /// written buffer when m_offset reaches 8 or Flush() is called.
     333                 :             :     uint8_t m_buffer{0};
     334                 :             : 
     335                 :             :     /// Number of high order bits in m_buffer already written by previous
     336                 :             :     /// Write() calls and not yet flushed to the stream. The next bit to be
     337                 :             :     /// written to is at this offset from the most significant bit position.
     338                 :             :     int m_offset{0};
     339                 :             : 
     340                 :             : public:
     341         [ +  - ]:         236 :     explicit BitStreamWriter(OStream& ostream) : m_ostream(ostream) {}
     342                 :             : 
     343                 :         236 :     ~BitStreamWriter()
     344                 :             :     {
     345                 :         236 :         Flush();
     346                 :         236 :     }
     347                 :             : 
     348                 :             :     /** Write the nbits least significant bits of a 64-bit int to the output
     349                 :             :      * stream. Data is buffered until it completes an octet.
     350                 :             :      */
     351                 :         873 :     void Write(uint64_t data, int nbits) {
     352         [ +  - ]:         873 :         if (nbits < 0 || nbits > 64) {
     353         [ #  # ]:           0 :             throw std::out_of_range("nbits must be between 0 and 64");
     354                 :             :         }
     355                 :             : 
     356         [ +  + ]:        2417 :         while (nbits > 0) {
     357         [ +  + ]:        1544 :             int bits = std::min(8 - m_offset, nbits);
     358                 :        1544 :             m_buffer |= (data << (64 - nbits)) >> (64 - 8 + m_offset);
     359                 :        1544 :             m_offset += bits;
     360                 :        1544 :             nbits -= bits;
     361                 :             : 
     362         [ +  + ]:        1544 :             if (m_offset == 8) {
     363                 :         709 :                 Flush();
     364                 :             :             }
     365                 :             :         }
     366                 :         873 :     }
     367                 :             : 
     368                 :             :     /** Flush any unwritten bits to the output stream, padding with 0's to the
     369                 :             :      * next byte boundary.
     370                 :             :      */
     371                 :        1181 :     void Flush() {
     372         [ +  + ]:        1181 :         if (m_offset == 0) {
     373                 :             :             return;
     374                 :             :         }
     375                 :             : 
     376                 :         944 :         m_ostream << m_buffer;
     377                 :         944 :         m_buffer = 0;
     378                 :         944 :         m_offset = 0;
     379                 :             :     }
     380                 :             : };
     381                 :             : 
     382                 :             : /** Non-refcounted RAII wrapper for FILE*
     383                 :             :  *
     384                 :             :  * Will automatically close the file when it goes out of scope if not null.
     385                 :             :  * If you're returning the file pointer, return file.release().
     386                 :             :  * If you need to close the file early, use file.fclose() instead of fclose(file).
     387                 :             :  */
     388                 :             : class AutoFile
     389                 :             : {
     390                 :             : protected:
     391                 :             :     std::FILE* m_file;
     392                 :             :     std::vector<std::byte> m_xor;
     393                 :             : 
     394                 :             : public:
     395                 :       18112 :     explicit AutoFile(std::FILE* file, std::vector<std::byte> data_xor={}) : m_file{file}, m_xor{std::move(data_xor)} {}
     396                 :             : 
     397 [ +  - ][ +  -  :       18079 :     ~AutoFile() { fclose(); }
          +  -  +  -  #  
           # ][ +  -  +  
             -  +  -  +  
                      - ]
     398                 :             : 
     399                 :             :     // Disallow copies
     400                 :             :     AutoFile(const AutoFile&) = delete;
     401                 :             :     AutoFile& operator=(const AutoFile&) = delete;
     402                 :             : 
     403                 :         187 :     bool feof() const { return std::feof(m_file); }
     404                 :             : 
     405                 :       18152 :     int fclose()
     406                 :             :     {
     407         [ +  - ]:       18005 :         if (auto rel{release()}) return std::fclose(rel);
     408                 :             :         return 0;
     409                 :             :     }
     410                 :             : 
     411                 :             :     /** Get wrapped FILE* with transfer of ownership.
     412                 :             :      * @note This will invalidate the AutoFile object, and makes it the responsibility of the caller
     413                 :             :      * of this function to clean up the returned FILE*.
     414                 :             :      */
     415                 :       18152 :     std::FILE* release()
     416                 :             :     {
     417                 :       18152 :         std::FILE* ret{m_file};
     418                 :       18152 :         m_file = nullptr;
     419         [ +  + ]:       18152 :         return ret;
           [ +  -  -  - ]
     420                 :             :     }
     421                 :             : 
     422                 :             :     /** Get wrapped FILE* without transfer of ownership.
     423                 :             :      * @note Ownership of the FILE* will remain with this class. Use this only if the scope of the
     424                 :             :      * AutoFile outlives use of the passed pointer.
     425                 :             :      */
     426   [ +  -  +  - ]:       14258 :     std::FILE* Get() const { return m_file; }
           [ +  -  #  # ]
           [ +  -  +  -  
                   +  - ]
     427                 :             : 
     428                 :             :     /** Return true if the wrapped FILE* is nullptr, false otherwise.
     429                 :             :      */
     430   [ -  -  -  +  :       17961 :     bool IsNull() const { return m_file == nullptr; }
             +  -  -  + ]
           [ -  +  #  # ]
           [ -  +  +  +  
          #  #  #  #  #  
             #  #  #  #  
           # ][ -  -  -  
          -  -  -  +  +  
          -  +  -  +  -  
           + ][ +  -  +  
                -  +  - ]
     431                 :             : 
     432                 :             :     /** Continue with a different XOR key */
     433   [ #  #  #  # ]:           0 :     void SetXor(std::vector<std::byte> data_xor) { m_xor = data_xor; }
     434                 :             : 
     435                 :             :     /** Implementation detail, only used internally. */
     436                 :             :     std::size_t detail_fread(Span<std::byte> dst);
     437                 :             : 
     438                 :             :     void seek(int64_t offset, int origin);
     439                 :             :     int64_t tell();
     440                 :             : 
     441                 :             :     //
     442                 :             :     // Stream subset
     443                 :             :     //
     444                 :             :     void read(Span<std::byte> dst);
     445                 :             :     void ignore(size_t nSize);
     446                 :             :     void write(Span<const std::byte> src);
     447                 :             : 
     448                 :             :     template <typename T>
     449 [ #  # ][ #  #  :       71106 :     AutoFile& operator<<(const T& obj)
          #  #  #  #  #  
                #  #  # ]
     450                 :             :     {
     451   [ +  -  +  - ]:       89409 :         ::Serialize(*this, obj);
           [ #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           # ][ +  -  -  
          -  -  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                -  +  - ]
           [ +  -  #  # ]
           [ -  -  -  -  
             -  -  +  - ]
           [ +  -  +  +  
           +  - ][ +  -  
          +  -  +  -  -  
             +  +  -  +  
           - ][ #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  # ][ #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
     452                 :       45833 :         return *this;
     453                 :             :     }
     454                 :             : 
     455                 :             :     template <typename T>
     456         [ #  # ]:        5004 :     AutoFile& operator>>(T&& obj)
           [ +  -  +  - ]
           [ #  #  #  #  
             #  #  #  # ]
           [ #  #  #  #  
          #  #  #  #  #  
                      # ]
     457                 :             :     {
     458   [ +  -  #  #  :       13738 :         ::Unserialize(*this, obj);
             #  #  #  # ]
           [ +  -  +  +  
             +  -  +  + ]
           [ -  -  -  +  
          -  -  -  -  -  
          +  +  -  -  +  
          -  +  +  -  +  
          -  -  +  +  -  
          +  -  +  -  +  
          -  -  -  +  -  
             -  -  -  - ]
           [ +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
           +  - ][ +  -  
          -  -  -  -  +  
                -  +  - ]
           [ +  -  +  - ]
           [ -  +  +  -  
          +  -  +  -  -  
             +  +  -  -  
                      + ]
     459                 :        9384 :         return *this;
     460                 :             :     }
     461                 :             : };
     462                 :             : 
     463                 :             : /** Wrapper around an AutoFile& that implements a ring buffer to
     464                 :             :  *  deserialize from. It guarantees the ability to rewind a given number of bytes.
     465                 :             :  *
     466                 :             :  *  Will automatically close the file when it goes out of scope if not null.
     467                 :             :  *  If you need to close the file early, use file.fclose() instead of fclose(file).
     468                 :             :  */
     469                 :          52 : class BufferedFile
     470                 :             : {
     471                 :             : private:
     472                 :             :     AutoFile& m_src;
     473                 :             :     uint64_t nSrcPos{0};  //!< how many bytes have been read from source
     474                 :             :     uint64_t m_read_pos{0}; //!< how many bytes have been read from this
     475                 :             :     uint64_t nReadLimit;  //!< up to which position we're allowed to read
     476                 :             :     uint64_t nRewind;     //!< how many bytes we guarantee to rewind
     477                 :             :     std::vector<std::byte> vchBuf; //!< the buffer
     478                 :             : 
     479                 :             :     //! read data from the source to fill the buffer
     480                 :         284 :     bool Fill() {
     481         [ +  + ]:         284 :         unsigned int pos = nSrcPos % vchBuf.size();
     482                 :         284 :         unsigned int readNow = vchBuf.size() - pos;
     483                 :         284 :         unsigned int nAvail = vchBuf.size() - (nSrcPos - m_read_pos) - nRewind;
     484         [ +  + ]:         284 :         if (nAvail < readNow)
     485                 :         247 :             readNow = nAvail;
     486         [ +  - ]:         284 :         if (readNow == 0)
     487                 :             :             return false;
     488                 :         284 :         size_t nBytes{m_src.detail_fread(Span{vchBuf}.subspan(pos, readNow))};
     489         [ +  + ]:         284 :         if (nBytes == 0) {
     490   [ -  +  +  - ]:           1 :             throw std::ios_base::failure{m_src.feof() ? "BufferedFile::Fill: end of file" : "BufferedFile::Fill: fread failed"};
     491                 :             :         }
     492                 :         283 :         nSrcPos += nBytes;
     493                 :         283 :         return true;
     494                 :             :     }
     495                 :             : 
     496                 :             :     //! Advance the stream's read pointer (m_read_pos) by up to 'length' bytes,
     497                 :             :     //! filling the buffer from the file so that at least one byte is available.
     498                 :             :     //! Return a pointer to the available buffer data and the number of bytes
     499                 :             :     //! (which may be less than the requested length) that may be accessed
     500                 :             :     //! beginning at that pointer.
     501                 :        3210 :     std::pair<std::byte*, size_t> AdvanceStream(size_t length)
     502                 :             :     {
     503         [ -  + ]:        3210 :         assert(m_read_pos <= nSrcPos);
     504         [ +  + ]:        3210 :         if (m_read_pos + length > nReadLimit) {
     505         [ +  - ]:           2 :             throw std::ios_base::failure("Attempt to position past buffer limit");
     506                 :             :         }
     507                 :             :         // If there are no bytes available, read from the file.
     508   [ +  +  +  - ]:        3208 :         if (m_read_pos == nSrcPos && length > 0) Fill();
     509                 :             : 
     510                 :        3207 :         size_t buffer_offset{static_cast<size_t>(m_read_pos % vchBuf.size())};
     511                 :        3207 :         size_t buffer_available{static_cast<size_t>(vchBuf.size() - buffer_offset)};
     512                 :        3207 :         size_t bytes_until_source_pos{static_cast<size_t>(nSrcPos - m_read_pos)};
     513                 :        3207 :         size_t advance{std::min({length, buffer_available, bytes_until_source_pos})};
     514                 :        3207 :         m_read_pos += advance;
     515                 :        3207 :         return std::make_pair(&vchBuf[buffer_offset], advance);
     516                 :             :     }
     517                 :             : 
     518                 :             : public:
     519                 :          53 :     BufferedFile(AutoFile& file, uint64_t nBufSize, uint64_t nRewindIn)
     520                 :          53 :         : m_src{file}, nReadLimit{std::numeric_limits<uint64_t>::max()}, nRewind{nRewindIn}, vchBuf(nBufSize, std::byte{0})
     521                 :             :     {
     522         [ +  + ]:          53 :         if (nRewindIn >= nBufSize)
     523         [ +  - ]:           1 :             throw std::ios_base::failure("Rewind limit must be less than buffer size");
     524                 :          53 :     }
     525                 :             : 
     526                 :             :     //! check whether we're at the end of the source file
     527                 :        3882 :     bool eof() const {
     528   [ +  +  +  + ]:        3882 :         return m_read_pos == nSrcPos && m_src.feof();
     529                 :             :     }
     530                 :             : 
     531                 :             :     //! read a number of bytes
     532                 :        2598 :     void read(Span<std::byte> dst)
     533                 :             :     {
     534         [ +  + ]:        5293 :         while (dst.size() > 0) {
     535                 :        2697 :             auto [buffer_pointer, length]{AdvanceStream(dst.size())};
     536                 :        2695 :             memcpy(dst.data(), buffer_pointer, length);
     537                 :        2695 :             dst = dst.subspan(length);
     538                 :             :         }
     539                 :        2596 :     }
     540                 :             : 
     541                 :             :     //! Move the read position ahead in the stream to the given position.
     542                 :             :     //! Use SetPos() to back up in the stream, not SkipTo().
     543                 :         620 :     void SkipTo(const uint64_t file_pos)
     544                 :             :     {
     545         [ +  - ]:         620 :         assert(file_pos >= m_read_pos);
     546         [ +  + ]:        1132 :         while (m_read_pos < file_pos) AdvanceStream(file_pos - m_read_pos);
     547                 :         619 :     }
     548                 :             : 
     549                 :             :     //! return the current reading position
     550                 :        5201 :     uint64_t GetPos() const {
     551   [ #  #  #  #  :        5201 :         return m_read_pos;
           #  # ][ +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                -  +  - ]
     552                 :             :     }
     553                 :             : 
     554                 :             :     //! rewind to a given reading position
     555                 :         668 :     bool SetPos(uint64_t nPos) {
     556 [ #  # ][ +  +  :         668 :         size_t bufsize = vchBuf.size();
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
                +  +  - ]
     557 [ #  # ][ +  +  :         668 :         if (nPos + bufsize < nSrcPos) {
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
                +  +  - ]
     558                 :             :             // rewinding too far, rewind as far as possible
     559                 :          67 :             m_read_pos = nSrcPos - bufsize;
     560                 :          67 :             return false;
     561                 :             :         }
     562 [ #  # ][ +  +  :         598 :         if (nPos > nSrcPos) {
          -  +  -  +  -  
             +  -  +  -  
                      + ]
     563                 :             :             // can't go this far forward, go as far as possible
     564                 :          17 :             m_read_pos = nSrcPos;
     565                 :          17 :             return false;
     566                 :             :         }
     567                 :         584 :         m_read_pos = nPos;
     568                 :         584 :         return true;
     569                 :             :     }
     570                 :             : 
     571                 :             :     //! prevent reading beyond a certain position
     572                 :             :     //! no argument removes the limit
     573                 :        3195 :     bool SetLimit(uint64_t nPos = std::numeric_limits<uint64_t>::max()) {
     574 [ #  # ][ +  -  :        3194 :         if (nPos < m_read_pos)
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     575                 :             :             return false;
     576                 :        3195 :         nReadLimit = nPos;
     577         [ +  - ]:        3195 :         return true;
     578                 :             :     }
     579                 :             : 
     580                 :             :     template<typename T>
     581 [ #  # ][ +  -  :         671 :     BufferedFile& operator>>(T&& obj) {
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  -  +  +  -  
          +  -  +  -  +  
             -  -  +  +  
                      - ]
     582   [ #  #  #  #  :        2596 :         ::Unserialize(*this, obj);
             #  #  #  # ]
           [ +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          -  -  +  -  +  
          -  +  -  +  -  
          +  -  -  -  +  
                      - ]
           [ #  #  #  # ]
     583                 :        1927 :         return (*this);
     584                 :             :     }
     585                 :             : 
     586                 :             :     //! search for a given byte in the stream, and remain positioned on it
     587                 :         650 :     void FindByte(std::byte byte)
     588                 :             :     {
     589                 :             :         // For best performance, avoid mod operation within the loop.
     590                 :         650 :         size_t buf_offset{size_t(m_read_pos % uint64_t(vchBuf.size()))};
     591                 :         718 :         while (true) {
     592         [ +  + ]:         718 :             if (m_read_pos == nSrcPos) {
     593                 :             :                 // No more bytes available; read from the file into the buffer,
     594                 :             :                 // setting nSrcPos to one beyond the end of the new data.
     595                 :             :                 // Throws exception if end-of-file reached.
     596                 :          81 :                 Fill();
     597                 :             :             }
     598         [ +  + ]:         718 :             const size_t len{std::min<size_t>(vchBuf.size() - buf_offset, nSrcPos - m_read_pos)};
     599         [ +  + ]:         718 :             const auto it_start{vchBuf.begin() + buf_offset};
     600                 :         718 :             const auto it_find{std::find(it_start, it_start + len, byte)};
     601         [ +  + ]:         718 :             const size_t inc{size_t(std::distance(it_start, it_find))};
     602                 :         718 :             m_read_pos += inc;
     603         [ +  + ]:         718 :             if (inc < len) break;
     604                 :          68 :             buf_offset += inc;
     605         [ +  + ]:          68 :             if (buf_offset >= vchBuf.size()) buf_offset = 0;
     606                 :             :         }
     607                 :         650 :     }
     608                 :             : };
     609                 :             : 
     610                 :             : #endif // BITCOIN_STREAMS_H
        

Generated by: LCOV version 2.0-1