LCOV - code coverage report
Current view: top level - src - streams.h (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 75 290 25.9 %
Date: 2023-11-10 23:46:46 Functions: 87 469 18.6 %
Branches: 15 112 13.4 %

           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                 :          1 : inline void Xor(Span<std::byte> write, Span<const std::byte> key, size_t key_offset = 0)
      29                 :            : {
      30         [ -  + ]:          1 :     if (key.size() == 0) {
      31                 :          0 :         return;
      32                 :            :     }
      33                 :          1 :     key_offset %= key.size();
      34                 :            : 
      35         [ +  + ]:         10 :     for (size_t i = 0, j = key_offset; i != write.size(); i++) {
      36                 :          9 :         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         [ +  + ]:          9 :         if (j == key.size())
      43                 :          1 :             j = 0;
      44                 :          9 :     }
      45                 :          1 : }
      46                 :            : } // namespace util
      47                 :            : 
      48                 :            : template<typename Stream>
      49                 :            : class OverrideStream
      50                 :            : {
      51                 :            :     Stream* stream;
      52                 :            : 
      53                 :            :     const int nVersion;
      54                 :            : 
      55                 :            : public:
      56                 :          0 :     OverrideStream(Stream* stream_, int nVersion_) : stream{stream_}, nVersion{nVersion_} {}
      57                 :            : 
      58                 :            :     template<typename T>
      59                 :          0 :     OverrideStream<Stream>& operator<<(const T& obj)
      60                 :            :     {
      61                 :          0 :         ::Serialize(*this, obj);
      62                 :          0 :         return (*this);
      63                 :            :     }
      64                 :            : 
      65                 :            :     template<typename T>
      66                 :          0 :     OverrideStream<Stream>& operator>>(T&& obj)
      67                 :            :     {
      68                 :          0 :         ::Unserialize(*this, obj);
      69                 :          0 :         return (*this);
      70                 :            :     }
      71                 :            : 
      72                 :          0 :     void write(Span<const std::byte> src)
      73                 :            :     {
      74                 :          0 :         stream->write(src);
      75                 :          0 :     }
      76                 :            : 
      77                 :          0 :     void read(Span<std::byte> dst)
      78                 :            :     {
      79                 :          0 :         stream->read(dst);
      80                 :          0 :     }
      81                 :            : 
      82                 :          0 :     int GetVersion() const { return nVersion; }
      83                 :          0 :     size_t size() const { return stream->size(); }
      84                 :            :     void ignore(size_t size) { return stream->ignore(size); }
      85                 :            : };
      86                 :            : 
      87                 :            : /* Minimal stream for overwriting and/or appending to an existing byte vector
      88                 :            :  *
      89                 :            :  * The referenced vector will grow as necessary
      90                 :            :  */
      91                 :            : class CVectorWriter
      92                 :            : {
      93                 :            :  public:
      94                 :            : 
      95                 :            : /*
      96                 :            :  * @param[in]  nVersionIn Serialization Version (including any flags)
      97                 :            :  * @param[in]  vchDataIn  Referenced byte vector to overwrite/append
      98                 :            :  * @param[in]  nPosIn Starting position. Vector index where writes should start. The vector will initially
      99                 :            :  *                    grow as necessary to max(nPosIn, vec.size()). So to append, use vec.size().
     100                 :            : */
     101                 :          0 :     CVectorWriter(int nVersionIn, std::vector<unsigned char>& vchDataIn, size_t nPosIn) : nVersion{nVersionIn}, vchData{vchDataIn}, nPos{nPosIn}
     102                 :            :     {
     103         [ #  # ]:          0 :         if(nPos > vchData.size())
     104                 :          0 :             vchData.resize(nPos);
     105                 :          0 :     }
     106                 :            : /*
     107                 :            :  * (other params same as above)
     108                 :            :  * @param[in]  args  A list of items to serialize starting at nPosIn.
     109                 :            : */
     110                 :            :     template <typename... Args>
     111                 :          0 :     CVectorWriter(int nVersionIn, std::vector<unsigned char>& vchDataIn, size_t nPosIn, Args&&... args) : CVectorWriter{nVersionIn, vchDataIn, nPosIn}
     112                 :            :     {
     113                 :          0 :         ::SerializeMany(*this, std::forward<Args>(args)...);
     114                 :          0 :     }
     115                 :          0 :     void write(Span<const std::byte> src)
     116                 :            :     {
     117         [ #  # ]:          0 :         assert(nPos <= vchData.size());
     118                 :          0 :         size_t nOverwrite = std::min(src.size(), vchData.size() - nPos);
     119         [ #  # ]:          0 :         if (nOverwrite) {
     120                 :          0 :             memcpy(vchData.data() + nPos, src.data(), nOverwrite);
     121                 :          0 :         }
     122         [ #  # ]:          0 :         if (nOverwrite < src.size()) {
     123                 :          0 :             vchData.insert(vchData.end(), UCharCast(src.data()) + nOverwrite, UCharCast(src.end()));
     124                 :          0 :         }
     125                 :          0 :         nPos += src.size();
     126                 :          0 :     }
     127                 :            :     template<typename T>
     128                 :          0 :     CVectorWriter& operator<<(const T& obj)
     129                 :            :     {
     130                 :          0 :         ::Serialize(*this, obj);
     131                 :          0 :         return (*this);
     132                 :            :     }
     133                 :          0 :     int GetVersion() const
     134                 :            :     {
     135                 :          0 :         return nVersion;
     136                 :            :     }
     137                 :            : 
     138                 :            : private:
     139                 :            :     const int nVersion;
     140                 :            :     std::vector<unsigned char>& vchData;
     141                 :            :     size_t nPos;
     142                 :            : };
     143                 :            : 
     144                 :            : /** Minimal stream for reading from an existing byte array by Span.
     145                 :            :  */
     146                 :            : class SpanReader
     147                 :            : {
     148                 :            : private:
     149                 :            :     const int m_version;
     150                 :            :     Span<const unsigned char> m_data;
     151                 :            : 
     152                 :            : public:
     153                 :            :     /**
     154                 :            :      * @param[in]  version Serialization Version (including any flags)
     155                 :            :      * @param[in]  data Referenced byte vector to overwrite/append
     156                 :            :      */
     157                 :          0 :     SpanReader(int version, Span<const unsigned char> data)
     158                 :          0 :         : m_version{version}, m_data{data} {}
     159                 :            : 
     160                 :            :     template<typename T>
     161                 :          0 :     SpanReader& operator>>(T&& obj)
     162                 :            :     {
     163                 :          0 :         ::Unserialize(*this, obj);
     164                 :          0 :         return (*this);
     165                 :            :     }
     166                 :            : 
     167                 :          0 :     int GetVersion() const { return m_version; }
     168                 :            : 
     169                 :            :     size_t size() const { return m_data.size(); }
     170                 :          0 :     bool empty() const { return m_data.empty(); }
     171                 :            : 
     172                 :          0 :     void read(Span<std::byte> dst)
     173                 :            :     {
     174         [ #  # ]:          0 :         if (dst.size() == 0) {
     175                 :          0 :             return;
     176                 :            :         }
     177                 :            : 
     178                 :            :         // Read from the beginning of the buffer
     179         [ #  # ]:          0 :         if (dst.size() > m_data.size()) {
     180         [ #  # ]:          0 :             throw std::ios_base::failure("SpanReader::read(): end of data");
     181                 :            :         }
     182                 :          0 :         memcpy(dst.data(), m_data.data(), dst.size());
     183                 :          0 :         m_data = m_data.subspan(dst.size());
     184                 :          0 :     }
     185                 :            : };
     186                 :            : 
     187                 :            : /** Double ended buffer combining vector and stream-like interfaces.
     188                 :            :  *
     189                 :            :  * >> and << read and write unformatted data using the above serialization templates.
     190                 :            :  * Fills with data in linear time; some stringstream implementations take N^2 time.
     191                 :            :  */
     192                 :          0 : class DataStream
     193                 :            : {
     194                 :            : protected:
     195                 :            :     using vector_type = SerializeData;
     196                 :            :     vector_type vch;
     197                 :      40721 :     vector_type::size_type m_read_pos{0};
     198                 :            : 
     199                 :            : public:
     200                 :            :     typedef vector_type::allocator_type   allocator_type;
     201                 :            :     typedef vector_type::size_type        size_type;
     202                 :            :     typedef vector_type::difference_type  difference_type;
     203                 :            :     typedef vector_type::reference        reference;
     204                 :            :     typedef vector_type::const_reference  const_reference;
     205                 :            :     typedef vector_type::value_type       value_type;
     206                 :            :     typedef vector_type::iterator         iterator;
     207                 :            :     typedef vector_type::const_iterator   const_iterator;
     208                 :            :     typedef vector_type::reverse_iterator reverse_iterator;
     209                 :            : 
     210                 :      77760 :     explicit DataStream() {}
     211                 :          0 :     explicit DataStream(Span<const uint8_t> sp) : DataStream{AsBytes(sp)} {}
     212                 :       3682 :     explicit DataStream(Span<const value_type> sp) : vch(sp.data(), sp.data() + sp.size()) {}
     213                 :            : 
     214                 :          0 :     std::string str() const
     215                 :            :     {
     216         [ #  # ]:          0 :         return std::string{UCharCast(data()), UCharCast(data() + size())};
     217                 :          0 :     }
     218                 :            : 
     219                 :            : 
     220                 :            :     //
     221                 :            :     // Vector subset
     222                 :            :     //
     223                 :            :     const_iterator begin() const                     { return vch.begin() + m_read_pos; }
     224                 :      28817 :     iterator begin()                                 { return vch.begin() + m_read_pos; }
     225                 :            :     const_iterator end() const                       { return vch.end(); }
     226                 :      28817 :     iterator end()                                   { return vch.end(); }
     227                 :      10021 :     size_type size() const                           { return vch.size() - m_read_pos; }
     228                 :         20 :     bool empty() const                               { return vch.size() == m_read_pos; }
     229                 :          0 :     void resize(size_type n, value_type c = value_type{}) { vch.resize(n + m_read_pos, c); }
     230                 :      38817 :     void reserve(size_type n)                        { vch.reserve(n + m_read_pos); }
     231                 :            :     const_reference operator[](size_type pos) const  { return vch[pos + m_read_pos]; }
     232                 :          0 :     reference operator[](size_type pos)              { return vch[pos + m_read_pos]; }
     233                 :          2 :     void clear()                                     { vch.clear(); m_read_pos = 0; }
     234                 :      10021 :     value_type* data()                               { return vch.data() + m_read_pos; }
     235                 :          0 :     const value_type* data() const                   { return vch.data() + m_read_pos; }
     236                 :            : 
     237                 :            :     inline void Compact()
     238                 :            :     {
     239                 :            :         vch.erase(vch.begin(), vch.begin() + m_read_pos);
     240                 :            :         m_read_pos = 0;
     241                 :            :     }
     242                 :            : 
     243                 :            :     bool Rewind(std::optional<size_type> n = std::nullopt)
     244                 :            :     {
     245                 :            :         // Total rewind if no size is passed
     246                 :            :         if (!n) {
     247                 :            :             m_read_pos = 0;
     248                 :            :             return true;
     249                 :            :         }
     250                 :            :         // Rewind by n characters if the buffer hasn't been compacted yet
     251                 :            :         if (*n > m_read_pos)
     252                 :            :             return false;
     253                 :            :         m_read_pos -= *n;
     254                 :            :         return true;
     255                 :            :     }
     256                 :            : 
     257                 :            : 
     258                 :            :     //
     259                 :            :     // Stream subset
     260                 :            :     //
     261                 :          0 :     bool eof() const             { return size() == 0; }
     262                 :          0 :     int in_avail() const         { return size(); }
     263                 :            : 
     264                 :      20939 :     void read(Span<value_type> dst)
     265                 :            :     {
     266         [ +  - ]:      20939 :         if (dst.size() == 0) return;
     267                 :            : 
     268                 :            :         // Read from the beginning of the buffer
     269                 :      20939 :         auto next_read_pos{CheckedAdd(m_read_pos, dst.size())};
     270         [ +  + ]:      20939 :         if (!next_read_pos.has_value() || next_read_pos.value() > vch.size()) {
     271         [ +  - ]:       1024 :             throw std::ios_base::failure("DataStream::read(): end of data");
     272                 :            :         }
     273                 :      19915 :         memcpy(dst.data(), &vch[m_read_pos], dst.size());
     274         [ +  + ]:      19915 :         if (next_read_pos.value() == vch.size()) {
     275                 :         41 :             m_read_pos = 0;
     276                 :         41 :             vch.clear();
     277                 :         41 :             return;
     278                 :            :         }
     279                 :      19874 :         m_read_pos = next_read_pos.value();
     280                 :      19915 :     }
     281                 :            : 
     282                 :          0 :     void ignore(size_t num_ignore)
     283                 :            :     {
     284                 :            :         // Ignore from the beginning of the buffer
     285                 :          0 :         auto next_read_pos{CheckedAdd(m_read_pos, num_ignore)};
     286         [ #  # ]:          0 :         if (!next_read_pos.has_value() || next_read_pos.value() > vch.size()) {
     287         [ #  # ]:          0 :             throw std::ios_base::failure("DataStream::ignore(): end of data");
     288                 :            :         }
     289         [ #  # ]:          0 :         if (next_read_pos.value() == vch.size()) {
     290                 :          0 :             m_read_pos = 0;
     291                 :          0 :             vch.clear();
     292                 :          0 :             return;
     293                 :            :         }
     294                 :          0 :         m_read_pos = next_read_pos.value();
     295                 :          0 :     }
     296                 :            : 
     297                 :     190132 :     void write(Span<const value_type> src)
     298                 :            :     {
     299                 :            :         // Write to the end of the buffer
     300                 :     190132 :         vch.insert(vch.end(), src.begin(), src.end());
     301                 :     190132 :     }
     302                 :            : 
     303                 :            :     template<typename T>
     304                 :      48658 :     DataStream& operator<<(const T& obj)
     305                 :            :     {
     306                 :      48658 :         ::Serialize(*this, obj);
     307                 :      48658 :         return (*this);
     308                 :            :     }
     309                 :            : 
     310                 :            :     template<typename T>
     311                 :          0 :     DataStream& operator>>(T&& obj)
     312                 :            :     {
     313                 :          0 :         ::Unserialize(*this, obj);
     314                 :          0 :         return (*this);
     315                 :            :     }
     316                 :            : 
     317                 :            :     /**
     318                 :            :      * XOR the contents of this stream with a certain key.
     319                 :            :      *
     320                 :            :      * @param[in] key    The key used to XOR the data in this stream.
     321                 :            :      */
     322                 :          1 :     void Xor(const std::vector<unsigned char>& key)
     323                 :            :     {
     324                 :          1 :         util::Xor(MakeWritableByteSpan(*this), MakeByteSpan(key));
     325                 :          1 :     }
     326                 :            : };
     327                 :            : 
     328                 :          0 : class CDataStream : public DataStream
     329                 :            : {
     330                 :            : private:
     331                 :            :     int nType;
     332                 :            :     int nVersion;
     333                 :            : 
     334                 :            : public:
     335                 :      14430 :     explicit CDataStream(int nTypeIn, int nVersionIn)
     336                 :      14430 :         : nType{nTypeIn},
     337                 :      14430 :           nVersion{nVersionIn} {}
     338                 :            : 
     339                 :       1841 :     explicit CDataStream(Span<const uint8_t> sp, int type, int version) : CDataStream{AsBytes(sp), type, version} {}
     340                 :       1841 :     explicit CDataStream(Span<const value_type> sp, int nTypeIn, int nVersionIn)
     341                 :       1841 :         : DataStream{sp},
     342                 :       1841 :           nType{nTypeIn},
     343                 :       1841 :           nVersion{nVersionIn} {}
     344                 :            : 
     345                 :            :     int GetType() const          { return nType; }
     346                 :          0 :     void SetVersion(int n)       { nVersion = n; }
     347                 :        939 :     int GetVersion() const       { return nVersion; }
     348                 :            : 
     349                 :            :     template <typename T>
     350                 :      17424 :     CDataStream& operator<<(const T& obj)
     351                 :            :     {
     352                 :      17424 :         ::Serialize(*this, obj);
     353                 :      17424 :         return *this;
     354                 :            :     }
     355                 :            : 
     356                 :            :     template <typename T>
     357                 :       4800 :     CDataStream& operator>>(T&& obj)
     358                 :            :     {
     359                 :       4800 :         ::Unserialize(*this, obj);
     360                 :       4800 :         return *this;
     361                 :            :     }
     362                 :            : };
     363                 :            : 
     364                 :            : template <typename IStream>
     365                 :            : class BitStreamReader
     366                 :            : {
     367                 :            : private:
     368                 :            :     IStream& m_istream;
     369                 :            : 
     370                 :            :     /// Buffered byte read in from the input stream. A new byte is read into the
     371                 :            :     /// buffer when m_offset reaches 8.
     372                 :          0 :     uint8_t m_buffer{0};
     373                 :            : 
     374                 :            :     /// Number of high order bits in m_buffer already returned by previous
     375                 :            :     /// Read() calls. The next bit to be returned is at this offset from the
     376                 :            :     /// most significant bit position.
     377                 :          0 :     int m_offset{8};
     378                 :            : 
     379                 :            : public:
     380                 :          0 :     explicit BitStreamReader(IStream& istream) : m_istream(istream) {}
     381                 :            : 
     382                 :            :     /** Read the specified number of bits from the stream. The data is returned
     383                 :            :      * in the nbits least significant bits of a 64-bit uint.
     384                 :            :      */
     385                 :          0 :     uint64_t Read(int nbits) {
     386         [ #  # ]:          0 :         if (nbits < 0 || nbits > 64) {
     387         [ #  # ]:          0 :             throw std::out_of_range("nbits must be between 0 and 64");
     388                 :            :         }
     389                 :            : 
     390                 :          0 :         uint64_t data = 0;
     391         [ #  # ]:          0 :         while (nbits > 0) {
     392         [ #  # ]:          0 :             if (m_offset == 8) {
     393                 :          0 :                 m_istream >> m_buffer;
     394                 :          0 :                 m_offset = 0;
     395                 :          0 :             }
     396                 :            : 
     397                 :          0 :             int bits = std::min(8 - m_offset, nbits);
     398                 :          0 :             data <<= bits;
     399                 :          0 :             data |= static_cast<uint8_t>(m_buffer << m_offset) >> (8 - bits);
     400                 :          0 :             m_offset += bits;
     401                 :          0 :             nbits -= bits;
     402                 :            :         }
     403                 :          0 :         return data;
     404                 :          0 :     }
     405                 :            : };
     406                 :            : 
     407                 :            : template <typename OStream>
     408                 :            : class BitStreamWriter
     409                 :            : {
     410                 :            : private:
     411                 :            :     OStream& m_ostream;
     412                 :            : 
     413                 :            :     /// Buffered byte waiting to be written to the output stream. The byte is
     414                 :            :     /// written buffer when m_offset reaches 8 or Flush() is called.
     415                 :          0 :     uint8_t m_buffer{0};
     416                 :            : 
     417                 :            :     /// Number of high order bits in m_buffer already written by previous
     418                 :            :     /// Write() calls and not yet flushed to the stream. The next bit to be
     419                 :            :     /// written to is at this offset from the most significant bit position.
     420                 :          0 :     int m_offset{0};
     421                 :            : 
     422                 :            : public:
     423                 :          0 :     explicit BitStreamWriter(OStream& ostream) : m_ostream(ostream) {}
     424                 :            : 
     425                 :          0 :     ~BitStreamWriter()
     426                 :            :     {
     427         [ #  # ]:          0 :         Flush();
     428                 :          0 :     }
     429                 :            : 
     430                 :            :     /** Write the nbits least significant bits of a 64-bit int to the output
     431                 :            :      * stream. Data is buffered until it completes an octet.
     432                 :            :      */
     433                 :          0 :     void Write(uint64_t data, int nbits) {
     434         [ #  # ]:          0 :         if (nbits < 0 || nbits > 64) {
     435         [ #  # ]:          0 :             throw std::out_of_range("nbits must be between 0 and 64");
     436                 :            :         }
     437                 :            : 
     438         [ #  # ]:          0 :         while (nbits > 0) {
     439                 :          0 :             int bits = std::min(8 - m_offset, nbits);
     440                 :          0 :             m_buffer |= (data << (64 - nbits)) >> (64 - 8 + m_offset);
     441                 :          0 :             m_offset += bits;
     442                 :          0 :             nbits -= bits;
     443                 :            : 
     444         [ #  # ]:          0 :             if (m_offset == 8) {
     445                 :          0 :                 Flush();
     446                 :          0 :             }
     447                 :            :         }
     448                 :          0 :     }
     449                 :            : 
     450                 :            :     /** Flush any unwritten bits to the output stream, padding with 0's to the
     451                 :            :      * next byte boundary.
     452                 :            :      */
     453                 :          0 :     void Flush() {
     454         [ #  # ]:          0 :         if (m_offset == 0) {
     455                 :          0 :             return;
     456                 :            :         }
     457                 :            : 
     458                 :          0 :         m_ostream << m_buffer;
     459                 :          0 :         m_buffer = 0;
     460                 :          0 :         m_offset = 0;
     461                 :          0 :     }
     462                 :            : };
     463                 :            : 
     464                 :            : /** Non-refcounted RAII wrapper for FILE*
     465                 :            :  *
     466                 :            :  * Will automatically close the file when it goes out of scope if not null.
     467                 :            :  * If you're returning the file pointer, return file.release().
     468                 :            :  * If you need to close the file early, use file.fclose() instead of fclose(file).
     469                 :            :  */
     470                 :            : class AutoFile
     471                 :            : {
     472                 :            : protected:
     473                 :            :     std::FILE* m_file;
     474                 :            :     const std::vector<std::byte> m_xor;
     475                 :            : 
     476                 :            : public:
     477                 :        454 :     explicit AutoFile(std::FILE* file, std::vector<std::byte> data_xor={}) : m_file{file}, m_xor{std::move(data_xor)} {}
     478                 :            : 
     479         [ +  - ]:        454 :     ~AutoFile() { fclose(); }
     480                 :            : 
     481                 :            :     // Disallow copies
     482                 :            :     AutoFile(const AutoFile&) = delete;
     483                 :            :     AutoFile& operator=(const AutoFile&) = delete;
     484                 :            : 
     485                 :          0 :     bool feof() const { return std::feof(m_file); }
     486                 :            : 
     487                 :        454 :     int fclose()
     488                 :            :     {
     489         [ +  + ]:        454 :         if (auto rel{release()}) return std::fclose(rel);
     490                 :          1 :         return 0;
     491                 :        454 :     }
     492                 :            : 
     493                 :            :     /** Get wrapped FILE* with transfer of ownership.
     494                 :            :      * @note This will invalidate the AutoFile object, and makes it the responsibility of the caller
     495                 :            :      * of this function to clean up the returned FILE*.
     496                 :            :      */
     497                 :        454 :     std::FILE* release()
     498                 :            :     {
     499                 :        454 :         std::FILE* ret{m_file};
     500                 :        454 :         m_file = nullptr;
     501                 :        454 :         return ret;
     502                 :            :     }
     503                 :            : 
     504                 :            :     /** Get wrapped FILE* without transfer of ownership.
     505                 :            :      * @note Ownership of the FILE* will remain with this class. Use this only if the scope of the
     506                 :            :      * AutoFile outlives use of the passed pointer.
     507                 :            :      */
     508                 :        301 :     std::FILE* Get() const { return m_file; }
     509                 :            : 
     510                 :            :     /** Return true if the wrapped FILE* is nullptr, false otherwise.
     511                 :            :      */
     512                 :        454 :     bool IsNull() const { return m_file == nullptr; }
     513                 :            : 
     514                 :            :     /** Implementation detail, only used internally. */
     515                 :            :     std::size_t detail_fread(Span<std::byte> dst);
     516                 :            : 
     517                 :            :     //
     518                 :            :     // Stream subset
     519                 :            :     //
     520                 :            :     void read(Span<std::byte> dst);
     521                 :            :     void ignore(size_t nSize);
     522                 :            :     void write(Span<const std::byte> src);
     523                 :            : 
     524                 :            :     template <typename T>
     525                 :          0 :     AutoFile& operator<<(const T& obj)
     526                 :            :     {
     527                 :          0 :         ::Serialize(*this, obj);
     528                 :          0 :         return *this;
     529                 :            :     }
     530                 :            : 
     531                 :            :     template <typename T>
     532                 :          0 :     AutoFile& operator>>(T&& obj)
     533                 :            :     {
     534                 :          0 :         ::Unserialize(*this, obj);
     535                 :          0 :         return *this;
     536                 :            :     }
     537                 :            : };
     538                 :            : 
     539                 :          0 : class CAutoFile : public AutoFile
     540                 :            : {
     541                 :            : private:
     542                 :            :     const int nVersion;
     543                 :            : 
     544                 :            : public:
     545         [ +  - ]:        453 :     explicit CAutoFile(std::FILE* file, int version, std::vector<std::byte> data_xor = {}) : AutoFile{file, std::move(data_xor)}, nVersion{version} {}
     546                 :        454 :     int GetVersion() const       { return nVersion; }
     547                 :            : 
     548                 :            :     template<typename T>
     549                 :       2710 :     CAutoFile& operator<<(const T& obj)
     550                 :            :     {
     551                 :       2710 :         ::Serialize(*this, obj);
     552                 :       2710 :         return (*this);
     553                 :            :     }
     554                 :            : 
     555                 :            :     template<typename T>
     556                 :       1210 :     CAutoFile& operator>>(T&& obj)
     557                 :            :     {
     558                 :       1210 :         ::Unserialize(*this, obj);
     559                 :       1210 :         return (*this);
     560                 :            :     }
     561                 :            : };
     562                 :            : 
     563                 :            : /** Wrapper around a CAutoFile& that implements a ring buffer to
     564                 :            :  *  deserialize from. It guarantees the ability to rewind a given number of bytes.
     565                 :            :  *
     566                 :            :  *  Will automatically close the file when it goes out of scope if not null.
     567                 :            :  *  If you need to close the file early, use file.fclose() instead of fclose(file).
     568                 :            :  */
     569                 :          0 : class BufferedFile
     570                 :            : {
     571                 :            : private:
     572                 :            :     CAutoFile& m_src;
     573                 :          0 :     uint64_t nSrcPos{0};  //!< how many bytes have been read from source
     574                 :          0 :     uint64_t m_read_pos{0}; //!< how many bytes have been read from this
     575                 :            :     uint64_t nReadLimit;  //!< up to which position we're allowed to read
     576                 :            :     uint64_t nRewind;     //!< how many bytes we guarantee to rewind
     577                 :            :     std::vector<std::byte> vchBuf; //!< the buffer
     578                 :            : 
     579                 :            :     //! read data from the source to fill the buffer
     580                 :          0 :     bool Fill() {
     581                 :          0 :         unsigned int pos = nSrcPos % vchBuf.size();
     582                 :          0 :         unsigned int readNow = vchBuf.size() - pos;
     583                 :          0 :         unsigned int nAvail = vchBuf.size() - (nSrcPos - m_read_pos) - nRewind;
     584         [ #  # ]:          0 :         if (nAvail < readNow)
     585                 :          0 :             readNow = nAvail;
     586         [ #  # ]:          0 :         if (readNow == 0)
     587                 :          0 :             return false;
     588                 :          0 :         size_t nBytes{m_src.detail_fread(Span{vchBuf}.subspan(pos, readNow))};
     589         [ #  # ]:          0 :         if (nBytes == 0) {
     590 [ #  # ][ #  # ]:          0 :             throw std::ios_base::failure{m_src.feof() ? "BufferedFile::Fill: end of file" : "BufferedFile::Fill: fread failed"};
     591                 :            :         }
     592                 :          0 :         nSrcPos += nBytes;
     593                 :          0 :         return true;
     594                 :          0 :     }
     595                 :            : 
     596                 :            :     //! Advance the stream's read pointer (m_read_pos) by up to 'length' bytes,
     597                 :            :     //! filling the buffer from the file so that at least one byte is available.
     598                 :            :     //! Return a pointer to the available buffer data and the number of bytes
     599                 :            :     //! (which may be less than the requested length) that may be accessed
     600                 :            :     //! beginning at that pointer.
     601                 :          0 :     std::pair<std::byte*, size_t> AdvanceStream(size_t length)
     602                 :            :     {
     603         [ #  # ]:          0 :         assert(m_read_pos <= nSrcPos);
     604         [ #  # ]:          0 :         if (m_read_pos + length > nReadLimit) {
     605         [ #  # ]:          0 :             throw std::ios_base::failure("Attempt to position past buffer limit");
     606                 :            :         }
     607                 :            :         // If there are no bytes available, read from the file.
     608 [ #  # ][ #  # ]:          0 :         if (m_read_pos == nSrcPos && length > 0) Fill();
     609                 :            : 
     610                 :          0 :         size_t buffer_offset{static_cast<size_t>(m_read_pos % vchBuf.size())};
     611                 :          0 :         size_t buffer_available{static_cast<size_t>(vchBuf.size() - buffer_offset)};
     612                 :          0 :         size_t bytes_until_source_pos{static_cast<size_t>(nSrcPos - m_read_pos)};
     613                 :          0 :         size_t advance{std::min({length, buffer_available, bytes_until_source_pos})};
     614                 :          0 :         m_read_pos += advance;
     615                 :          0 :         return std::make_pair(&vchBuf[buffer_offset], advance);
     616                 :          0 :     }
     617                 :            : 
     618                 :            : public:
     619                 :          0 :     BufferedFile(CAutoFile& file, uint64_t nBufSize, uint64_t nRewindIn)
     620 [ #  # ][ #  # ]:          0 :         : m_src{file}, nReadLimit{std::numeric_limits<uint64_t>::max()}, nRewind{nRewindIn}, vchBuf(nBufSize, std::byte{0})
     621                 :            :     {
     622         [ #  # ]:          0 :         if (nRewindIn >= nBufSize)
     623 [ #  # ][ #  # ]:          0 :             throw std::ios_base::failure("Rewind limit must be less than buffer size");
     624                 :          0 :     }
     625                 :            : 
     626                 :          0 :     int GetVersion() const { return m_src.GetVersion(); }
     627                 :            : 
     628                 :            :     //! check whether we're at the end of the source file
     629                 :          0 :     bool eof() const {
     630         [ #  # ]:          0 :         return m_read_pos == nSrcPos && m_src.feof();
     631                 :            :     }
     632                 :            : 
     633                 :            :     //! read a number of bytes
     634                 :          0 :     void read(Span<std::byte> dst)
     635                 :            :     {
     636         [ #  # ]:          0 :         while (dst.size() > 0) {
     637                 :          0 :             auto [buffer_pointer, length]{AdvanceStream(dst.size())};
     638                 :          0 :             memcpy(dst.data(), buffer_pointer, length);
     639                 :          0 :             dst = dst.subspan(length);
     640                 :            :         }
     641                 :          0 :     }
     642                 :            : 
     643                 :            :     //! Move the read position ahead in the stream to the given position.
     644                 :            :     //! Use SetPos() to back up in the stream, not SkipTo().
     645                 :          0 :     void SkipTo(const uint64_t file_pos)
     646                 :            :     {
     647         [ #  # ]:          0 :         assert(file_pos >= m_read_pos);
     648         [ #  # ]:          0 :         while (m_read_pos < file_pos) AdvanceStream(file_pos - m_read_pos);
     649                 :          0 :     }
     650                 :            : 
     651                 :            :     //! return the current reading position
     652                 :          0 :     uint64_t GetPos() const {
     653                 :          0 :         return m_read_pos;
     654                 :            :     }
     655                 :            : 
     656                 :            :     //! rewind to a given reading position
     657                 :          0 :     bool SetPos(uint64_t nPos) {
     658                 :          0 :         size_t bufsize = vchBuf.size();
     659         [ #  # ]:          0 :         if (nPos + bufsize < nSrcPos) {
     660                 :            :             // rewinding too far, rewind as far as possible
     661                 :          0 :             m_read_pos = nSrcPos - bufsize;
     662                 :          0 :             return false;
     663                 :            :         }
     664         [ #  # ]:          0 :         if (nPos > nSrcPos) {
     665                 :            :             // can't go this far forward, go as far as possible
     666                 :          0 :             m_read_pos = nSrcPos;
     667                 :          0 :             return false;
     668                 :            :         }
     669                 :          0 :         m_read_pos = nPos;
     670                 :          0 :         return true;
     671                 :          0 :     }
     672                 :            : 
     673                 :            :     //! prevent reading beyond a certain position
     674                 :            :     //! no argument removes the limit
     675                 :          0 :     bool SetLimit(uint64_t nPos = std::numeric_limits<uint64_t>::max()) {
     676         [ #  # ]:          0 :         if (nPos < m_read_pos)
     677                 :          0 :             return false;
     678                 :          0 :         nReadLimit = nPos;
     679                 :          0 :         return true;
     680                 :          0 :     }
     681                 :            : 
     682                 :            :     template<typename T>
     683                 :          0 :     BufferedFile& operator>>(T&& obj) {
     684                 :          0 :         ::Unserialize(*this, obj);
     685                 :          0 :         return (*this);
     686                 :            :     }
     687                 :            : 
     688                 :            :     //! search for a given byte in the stream, and remain positioned on it
     689                 :          0 :     void FindByte(std::byte byte)
     690                 :            :     {
     691                 :            :         // For best performance, avoid mod operation within the loop.
     692                 :          0 :         size_t buf_offset{size_t(m_read_pos % uint64_t(vchBuf.size()))};
     693                 :          0 :         while (true) {
     694         [ #  # ]:          0 :             if (m_read_pos == nSrcPos) {
     695                 :            :                 // No more bytes available; read from the file into the buffer,
     696                 :            :                 // setting nSrcPos to one beyond the end of the new data.
     697                 :            :                 // Throws exception if end-of-file reached.
     698                 :          0 :                 Fill();
     699                 :          0 :             }
     700                 :          0 :             const size_t len{std::min<size_t>(vchBuf.size() - buf_offset, nSrcPos - m_read_pos)};
     701                 :          0 :             const auto it_start{vchBuf.begin() + buf_offset};
     702                 :          0 :             const auto it_find{std::find(it_start, it_start + len, byte)};
     703                 :          0 :             const size_t inc{size_t(std::distance(it_start, it_find))};
     704                 :          0 :             m_read_pos += inc;
     705         [ #  # ]:          0 :             if (inc < len) break;
     706                 :          0 :             buf_offset += inc;
     707         [ #  # ]:          0 :             if (buf_offset >= vchBuf.size()) buf_offset = 0;
     708                 :            :         }
     709                 :          0 :     }
     710                 :            : };
     711                 :            : 
     712                 :            : #endif // BITCOIN_STREAMS_H

Generated by: LCOV version 1.14