/root/bitcoin/src/streams.h
Line | Count | Source (jump to first uncovered line) |
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 | | inline void Xor(Span<std::byte> write, Span<const std::byte> key, size_t key_offset = 0) |
29 | 0 | { |
30 | 0 | if (key.size() == 0) { |
31 | 0 | return; |
32 | 0 | } |
33 | 0 | key_offset %= key.size(); |
34 | |
|
35 | 0 | for (size_t i = 0, j = key_offset; i != write.size(); i++) { |
36 | 0 | 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 | 0 | if (j == key.size()) |
43 | 0 | j = 0; |
44 | 0 | } |
45 | 0 | } |
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 | 0 | VectorWriter(std::vector<unsigned char>& vchDataIn, size_t nPosIn) : vchData{vchDataIn}, nPos{nPosIn} |
61 | 0 | { |
62 | 0 | if(nPos > vchData.size()) |
63 | 0 | vchData.resize(nPos); |
64 | 0 | } |
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 | 0 | VectorWriter(std::vector<unsigned char>& vchDataIn, size_t nPosIn, Args&&... args) : VectorWriter{vchDataIn, nPosIn} |
71 | 0 | { |
72 | 0 | ::SerializeMany(*this, std::forward<Args>(args)...); |
73 | 0 | } Unexecuted instantiation: _ZN12VectorWriterC2IJ13ParamsWrapperI20TransactionSerParamsSt6vectorI6CBlockSaIS4_EEEEEERS3_IhSaIhEEmDpOT_ Unexecuted instantiation: _ZN12VectorWriterC2IJ13ParamsWrapperI20TransactionSerParams25CBlockHeaderAndShortTxIDsEEEERSt6vectorIhSaIhEEmDpOT_ Unexecuted instantiation: _ZN12VectorWriterC2IJ13ParamsWrapperI20TransactionSerParams6CBlockEEEERSt6vectorIhSaIhEEmDpOT_ Unexecuted instantiation: _ZN12VectorWriterC2IJ4SpanISt4byteEEEERSt6vectorIhSaIhEEmDpOT_ Unexecuted instantiation: _ZN12VectorWriterC2IJRi7WrapperI19CustomUintFormatterILi8ELb0EER12ServiceFlagsEll13ParamsWrapperIN8CNetAddr9SerParamsE8CServiceElSC_mNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiRbEEERSt6vectorIhSaIhEEmDpOT_ Unexecuted instantiation: _ZN12VectorWriterC2IJR14CMessageHeaderEEERSt6vectorIhSaIhEEmDpOT_ Unexecuted instantiation: _ZN12VectorWriterC2IJbRKmEEERSt6vectorIhSaIhEEmDpOT_ Unexecuted instantiation: _ZN12VectorWriterC2IJRK25CBlockHeaderAndShortTxIDsEEERSt6vectorIhSaIhEEmDpOT_ Unexecuted instantiation: _ZN12VectorWriterC2IJRSt6vectorI4CInvSaIS2_EEEEERS1_IhSaIhEEmDpOT_ Unexecuted instantiation: _ZN12VectorWriterC2IJRKiRmRKlS3_13ParamsWrapperIN8CNetAddr9SerParamsE8CServiceES3_SA_S3_RNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES2_RKbEEERSt6vectorIhSaIhEEmDpOT_ Unexecuted instantiation: _ZN12VectorWriterC2IJRKjRKmEEERSt6vectorIhSaIhEEmDpOT_ Unexecuted instantiation: _ZN12VectorWriterC2IJRKSt5arrayISt4byteLm168EEEEERSt6vectorIhSaIhEEmDpOT_ Unexecuted instantiation: _ZN12VectorWriterC2IJRK13CBlockLocator7uint256EEERSt6vectorIhSaIhEEmDpOT_ Unexecuted instantiation: _ZN12VectorWriterC2IJ13ParamsWrapperI20TransactionSerParamsK12CTransactionEEEERSt6vectorIhSaIhEEmDpOT_ Unexecuted instantiation: _ZN12VectorWriterC2IJ4SpanIhEEEERSt6vectorIhSaIhEEmDpOT_ Unexecuted instantiation: _ZN12VectorWriterC2IJ13ParamsWrapperI20TransactionSerParamsK6CBlockEEEERSt6vectorIhSaIhEEmDpOT_ Unexecuted instantiation: _ZN12VectorWriterC2IJR12CMerkleBlockEEERSt6vectorIhSaIhEEmDpOT_ Unexecuted instantiation: _ZN12VectorWriterC2IJR25CBlockHeaderAndShortTxIDsEEERSt6vectorIhSaIhEEmDpOT_ Unexecuted instantiation: _ZN12VectorWriterC2IJR17BlockTransactionsEEERSt6vectorIhSaIhEEmDpOT_ Unexecuted instantiation: _ZN12VectorWriterC2IJSt6vectorI12CBlockHeaderSaIS2_EEEEERS1_IhSaIhEEmDpOT_ Unexecuted instantiation: _ZN12VectorWriterC2IJR24BlockTransactionsRequestEEERSt6vectorIhSaIhEEmDpOT_ Unexecuted instantiation: _ZN12VectorWriterC2IJRmEEERSt6vectorIhSaIhEEmDpOT_ Unexecuted instantiation: _ZN12VectorWriterC2IJRK11BlockFilterEEERSt6vectorIhSaIhEEmDpOT_ Unexecuted instantiation: _ZN12VectorWriterC2IJRh7uint256RS2_RSt6vectorIS2_SaIS2_EEEEERS4_IhSaIhEEmDpOT_ Unexecuted instantiation: _ZN12VectorWriterC2IJRh7uint256RSt6vectorIS2_SaIS2_EEEEERS3_IhSaIhEEmDpOT_ Unexecuted instantiation: _ZN12VectorWriterC2IJ13ParamsWrapperIN8CAddress9SerParamsESt6vectorIS2_SaIS2_EEEEEERS4_IhSaIhEEmDpOT_ Unexecuted instantiation: _ZN12VectorWriterC2IJRlEEERSt6vectorIhSaIhEEmDpOT_ |
74 | | void write(Span<const std::byte> src) |
75 | 0 | { |
76 | 0 | assert(nPos <= vchData.size()); |
77 | 0 | size_t nOverwrite = std::min(src.size(), vchData.size() - nPos); |
78 | 0 | if (nOverwrite) { |
79 | 0 | memcpy(vchData.data() + nPos, src.data(), nOverwrite); |
80 | 0 | } |
81 | 0 | if (nOverwrite < src.size()) { |
82 | 0 | vchData.insert(vchData.end(), UCharCast(src.data()) + nOverwrite, UCharCast(src.end())); |
83 | 0 | } |
84 | 0 | nPos += src.size(); |
85 | 0 | } |
86 | | template <typename T> |
87 | | VectorWriter& operator<<(const T& obj) |
88 | 0 | { |
89 | 0 | ::Serialize(*this, obj); |
90 | 0 | return (*this); |
91 | 0 | } Unexecuted instantiation: cluster_linearize.cpp:_ZN12VectorWriterlsI7WrapperIN12_GLOBAL__N_117DepGraphFormatterERKN17cluster_linearize8DepGraphIN13bitset_detail9IntBitSetIjEEEEEEERS_RKT_ Unexecuted instantiation: _ZN12VectorWriterlsI7WrapperI15VarIntFormatterIL10VarIntMode1EERKiEEERS_RKT_ Unexecuted instantiation: _ZN12VectorWriterlsI7WrapperI15VarIntFormatterIL10VarIntMode0EERmEEERS_RKT_ Unexecuted instantiation: _ZN12VectorWriterlsIhEERS_RKT_ Unexecuted instantiation: _ZN12VectorWriterlsISt3setI7uint256St4lessIS2_ESaIS2_EEEERS_RKT_ Unexecuted instantiation: _ZN12VectorWriterlsI4SpanIKhEEERS_RKT_ Unexecuted instantiation: _ZN12VectorWriterlsIA4_hEERS_RKT_ Unexecuted instantiation: _ZN12VectorWriterlsIjEERS_RKT_ Unexecuted instantiation: _ZN12VectorWriterlsISt6vectorIhSaIhEEEERS_RKT_ Unexecuted instantiation: _ZN12VectorWriterlsI7uint256EERS_RKT_ Unexecuted instantiation: _ZN12VectorWriterlsIiEERS_RKT_ |
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 | 0 | explicit SpanReader(Span<const unsigned char> data) : m_data{data} {} |
110 | | |
111 | | template<typename T> |
112 | | SpanReader& operator>>(T&& obj) |
113 | 0 | { |
114 | 0 | ::Unserialize(*this, obj); |
115 | 0 | return (*this); |
116 | 0 | } Unexecuted instantiation: cluster_linearize.cpp:_ZN10SpanReaderrsI7WrapperIN12_GLOBAL__N_117DepGraphFormatterERN17cluster_linearize8DepGraphIN13bitset_detail9IntBitSetIjEEEEEEERS_OT_ Unexecuted instantiation: _ZN10SpanReaderrsI7WrapperI15VarIntFormatterIL10VarIntMode1EERiEEERS_OT_ Unexecuted instantiation: _ZN10SpanReaderrsI7WrapperI15VarIntFormatterIL10VarIntMode0EERmEEERS_OT_ Unexecuted instantiation: _ZN10SpanReaderrsIRmEERS_OT_ Unexecuted instantiation: _ZN10SpanReaderrsIRhEERS_OT_ Unexecuted instantiation: _ZN10SpanReaderrsIRSt6vectorIhSaIhEEEERS_OT_ Unexecuted instantiation: _ZN10SpanReaderrsIR11XOnlyPubKeyEERS_OT_ Unexecuted instantiation: _ZN10SpanReaderrsIR7uint256EERS_OT_ Unexecuted instantiation: _ZN10SpanReaderrsI13ParamsWrapperI20TransactionSerParams19CMutableTransactionEEERS_OT_ Unexecuted instantiation: _ZN10SpanReaderrsIR6CTxOutEERS_OT_ Unexecuted instantiation: _ZN10SpanReaderrsIR7CScriptEERS_OT_ Unexecuted instantiation: _ZN10SpanReaderrsIRSt6vectorIS1_IhSaIhEESaIS3_EEEERS_OT_ |
117 | | |
118 | 0 | size_t size() const { return m_data.size(); } |
119 | 0 | bool empty() const { return m_data.empty(); } |
120 | | |
121 | | void read(Span<std::byte> dst) |
122 | 0 | { |
123 | 0 | if (dst.size() == 0) { |
124 | 0 | return; |
125 | 0 | } |
126 | | |
127 | | // Read from the beginning of the buffer |
128 | 0 | if (dst.size() > m_data.size()) { |
129 | 0 | throw std::ios_base::failure("SpanReader::read(): end of data"); |
130 | 0 | } |
131 | 0 | memcpy(dst.data(), m_data.data(), dst.size()); |
132 | 0 | m_data = m_data.subspan(dst.size()); |
133 | 0 | } |
134 | | |
135 | | void ignore(size_t n) |
136 | 0 | { |
137 | 0 | m_data = m_data.subspan(n); |
138 | 0 | } |
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 | | 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 | 0 | explicit DataStream() = default; |
165 | 0 | explicit DataStream(Span<const uint8_t> sp) : DataStream{AsBytes(sp)} {} |
166 | 0 | explicit DataStream(Span<const value_type> sp) : vch(sp.data(), sp.data() + sp.size()) {} |
167 | | |
168 | | std::string str() const |
169 | 0 | { |
170 | 0 | return std::string{UCharCast(data()), UCharCast(data() + size())}; |
171 | 0 | } |
172 | | |
173 | | |
174 | | // |
175 | | // Vector subset |
176 | | // |
177 | 0 | const_iterator begin() const { return vch.begin() + m_read_pos; } |
178 | 0 | iterator begin() { return vch.begin() + m_read_pos; } |
179 | 0 | const_iterator end() const { return vch.end(); } |
180 | 0 | iterator end() { return vch.end(); } |
181 | 0 | size_type size() const { return vch.size() - m_read_pos; } |
182 | 0 | bool empty() const { return vch.size() == m_read_pos; } |
183 | 0 | void resize(size_type n, value_type c = value_type{}) { vch.resize(n + m_read_pos, c); } |
184 | 0 | void reserve(size_type n) { vch.reserve(n + m_read_pos); } |
185 | 0 | const_reference operator[](size_type pos) const { return vch[pos + m_read_pos]; } |
186 | 0 | reference operator[](size_type pos) { return vch[pos + m_read_pos]; } |
187 | 0 | void clear() { vch.clear(); m_read_pos = 0; } |
188 | 0 | value_type* data() { return vch.data() + m_read_pos; } |
189 | 0 | const value_type* data() const { return vch.data() + m_read_pos; } |
190 | | |
191 | | inline void Compact() |
192 | 0 | { |
193 | 0 | vch.erase(vch.begin(), vch.begin() + m_read_pos); |
194 | 0 | m_read_pos = 0; |
195 | 0 | } |
196 | | |
197 | | bool Rewind(std::optional<size_type> n = std::nullopt) |
198 | 0 | { |
199 | 0 | // Total rewind if no size is passed |
200 | 0 | if (!n) { |
201 | 0 | m_read_pos = 0; |
202 | 0 | return true; |
203 | 0 | } |
204 | 0 | // Rewind by n characters if the buffer hasn't been compacted yet |
205 | 0 | if (*n > m_read_pos) |
206 | 0 | return false; |
207 | 0 | m_read_pos -= *n; |
208 | 0 | return true; |
209 | 0 | } |
210 | | |
211 | | |
212 | | // |
213 | | // Stream subset |
214 | | // |
215 | 0 | bool eof() const { return size() == 0; } |
216 | 0 | int in_avail() const { return size(); } |
217 | | |
218 | | void read(Span<value_type> dst) |
219 | 0 | { |
220 | 0 | if (dst.size() == 0) return; |
221 | | |
222 | | // Read from the beginning of the buffer |
223 | 0 | auto next_read_pos{CheckedAdd(m_read_pos, dst.size())}; |
224 | 0 | if (!next_read_pos.has_value() || next_read_pos.value() > vch.size()) { |
225 | 0 | throw std::ios_base::failure("DataStream::read(): end of data"); |
226 | 0 | } |
227 | 0 | memcpy(dst.data(), &vch[m_read_pos], dst.size()); |
228 | 0 | if (next_read_pos.value() == vch.size()) { |
229 | 0 | m_read_pos = 0; |
230 | 0 | vch.clear(); |
231 | 0 | return; |
232 | 0 | } |
233 | 0 | m_read_pos = next_read_pos.value(); |
234 | 0 | } |
235 | | |
236 | | void ignore(size_t num_ignore) |
237 | 0 | { |
238 | | // Ignore from the beginning of the buffer |
239 | 0 | auto next_read_pos{CheckedAdd(m_read_pos, num_ignore)}; |
240 | 0 | if (!next_read_pos.has_value() || next_read_pos.value() > vch.size()) { |
241 | 0 | throw std::ios_base::failure("DataStream::ignore(): end of data"); |
242 | 0 | } |
243 | 0 | if (next_read_pos.value() == vch.size()) { |
244 | 0 | m_read_pos = 0; |
245 | 0 | vch.clear(); |
246 | 0 | return; |
247 | 0 | } |
248 | 0 | m_read_pos = next_read_pos.value(); |
249 | 0 | } |
250 | | |
251 | | void write(Span<const value_type> src) |
252 | 0 | { |
253 | | // Write to the end of the buffer |
254 | 0 | vch.insert(vch.end(), src.begin(), src.end()); |
255 | 0 | } |
256 | | |
257 | | template<typename T> |
258 | | DataStream& operator<<(const T& obj) |
259 | 0 | { |
260 | 0 | ::Serialize(*this, obj); |
261 | 0 | return (*this); |
262 | 0 | } Unexecuted instantiation: _ZN10DataStreamlsI7AddrManEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI20AddrManDeterministicEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI11BlockFilterEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsIhEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI7uint256EERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI4SpanIKhEEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsISt6vectorIhSaIhEEEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI14CBlockFileInfoEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI25CBlockHeaderAndShortTxIDsEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI8CFeeRateEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI12CMerkleBlockEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI9COutPointEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI18CPartialMerkleTreeEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI7CPubKeyEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI7CScriptEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI5CTxInEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI11FlatFilePosEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI13KeyOriginInfoEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI26PartiallySignedTransactionEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsIA5_hEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsIA4_hEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsIjEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI9PSBTInputEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI10PSBTOutputEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI20PrefilledTransactionEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI13ParamsWrapperI20TransactionSerParams6CBlockEEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI13CBlockLocatorEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI12CBlockHeaderEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI7CTxUndoEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI7WrapperI15VarIntFormatterIL10VarIntMode0EERmEEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI4SpanIhEEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI7WrapperI15VarIntFormatterIL10VarIntMode0EERjEEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI10CBlockUndoEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI4CoinEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI13ParamsWrapperIN8CNetAddr9SerParamsEKS2_EEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI13ParamsWrapperIN8CNetAddr9SerParamsEK8CServiceEEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI14CMessageHeaderEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI13ParamsWrapperIN8CAddress9SerParamsEKS2_EEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI4CInvEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI12CBloomFilterEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI15CDiskBlockIndexEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI7WrapperI16TxOutCompressionR6CTxOutEEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI17BlockTransactionsEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI24BlockTransactionsRequestEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsIN4node16SnapshotMetadataEEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsISt5arrayIhLm5EEEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsItEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsISt5arrayIhLm4EEEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsImEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI7uint160EERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsIlEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsIiEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsIsEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsIaEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsIbEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsISt6vectorIiSaIiEEEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI9prevectorILj8EijiEEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI13ParamsWrapperI20TransactionSerParams19CMutableTransactionEEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI7WrapperI22LimitedStringFormatterILm10EERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI13ParamsWrapperI20TransactionSerParamsK12CTransactionEEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI4SpanIKSt4byteEEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsISt4pairINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES7_EEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsISt4pairINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE22transaction_identifierILb0EEEEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsIN6wallet9CWalletTxEEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI13ParamsWrapperI20TransactionSerParamsKSt10shared_ptrIK12CTransactionEEEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsISt3mapINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES7_St4lessIS7_ESaISt4pairIKS7_S7_EEEEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsISt6vectorISt4pairINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES8_ESaIS9_EEEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsISt4pairINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE7uint256EEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsISt4pairINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE7CPubKeyEEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsIN6wallet12CKeyMetadataEEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsISt4pairISt6vectorIh16secure_allocatorIhEE7uint256EEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsISt4pairISt6vectorIhSaIhEE7uint256EEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsISt4pairINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEjEEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsIN6wallet10CMasterKeyEEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsISt4pairINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE7uint160EEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsISt4pairINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE7CScriptEEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsISt4pairINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEElEEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsIN6wallet8CKeyPoolEEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsISt4pairINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEhEEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsISt4pairINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES1_I7uint2567CPubKeyEEEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsIN6wallet16WalletDescriptorEEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsISt4pairIS1_INSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE7uint256ES1_IjjEEEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsISt4pairIS1_INSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE7uint256EjEEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsISt4pairINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES1_I22transaction_identifierILb0EEjEEEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsISt4pairINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES1_IS7_S7_EEEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsIN6wallet8CHDChainEEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI6CTxOutEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsISt4pairIhiEEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsISt4pairIh7uint256EEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsISt4pairIhNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsISt6vectorI5CCoinSaIS2_EEEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI13ParamsWrapperI20TransactionSerParamsK6CBlockEEERS_RKT_ Unexecuted instantiation: txdb.cpp:_ZN10DataStreamlsIN12_GLOBAL__N_19CoinEntryEEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsISt6vectorI7uint256SaIS2_EEEERS_RKT_ Unexecuted instantiation: blockfilterindex.cpp:_ZN10DataStreamlsIN12_GLOBAL__N_111DBHeightKeyEEERS_RKT_ Unexecuted instantiation: blockfilterindex.cpp:_ZN10DataStreamlsIN12_GLOBAL__N_19DBHashKeyEEERS_RKT_ Unexecuted instantiation: blockfilterindex.cpp:_ZN10DataStreamlsIN12_GLOBAL__N_15DBValEEERS_RKT_ Unexecuted instantiation: blockfilterindex.cpp:_ZN10DataStreamlsISt4pairI7uint256N12_GLOBAL__N_15DBValEEEERS_RKT_ Unexecuted instantiation: coinstatsindex.cpp:_ZN10DataStreamlsIN12_GLOBAL__N_111DBHeightKeyEEERS_RKT_ Unexecuted instantiation: coinstatsindex.cpp:_ZN10DataStreamlsIN12_GLOBAL__N_19DBHashKeyEEERS_RKT_ Unexecuted instantiation: coinstatsindex.cpp:_ZN10DataStreamlsIN12_GLOBAL__N_15DBValEEERS_RKT_ Unexecuted instantiation: coinstatsindex.cpp:_ZN10DataStreamlsISt4pairI7uint256N12_GLOBAL__N_15DBValEEEERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI10MuHash3072EERS_RKT_ Unexecuted instantiation: _ZN10DataStreamlsI10CDiskTxPosEERS_RKT_ |
263 | | |
264 | | template<typename T> |
265 | | DataStream& operator>>(T&& obj) |
266 | 0 | { |
267 | 0 | ::Unserialize(*this, obj); |
268 | 0 | return (*this); |
269 | 0 | } Unexecuted instantiation: _ZN10DataStreamrsIR20AddrManDeterministicEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsI13ParamsWrapperI20TransactionSerParams6CBlockEEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIR12CBlockHeaderEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIR13CBlockLocatorEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIR14CBlockFileInfoEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIR11BlockFilterEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIRhEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIR7uint256EERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIRSt6vectorIhSaIhEEEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIR9COutPointEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsI13ParamsWrapperI20TransactionSerParams19CMutableTransactionEEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIR15CDiskBlockIndexEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIR4CoinEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsI7WrapperI15VarIntFormatterIL10VarIntMode0EERmEEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsI7WrapperI15VarIntFormatterIL10VarIntMode0EERjEEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsI4SpanIhEEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsI13ParamsWrapperIN8CAddress9SerParamsE8AddrInfoEEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIR25CBlockHeaderAndShortTxIDsEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIR8CFeeRateEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIR12CMerkleBlockEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIR18CPartialMerkleTreeEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIR7CPubKeyEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIR7CScriptEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIR5CTxInEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIR11FlatFilePosEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIR13KeyOriginInfoEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIR26PartiallySignedTransactionEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIRA5_hEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIRA4_hEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIRjEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIR9PSBTInputEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIRSt3setI7uint256St4lessIS2_ESaIS2_EEEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIR10PSBTOutputEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIR20PrefilledTransactionEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIR13ParamsWrapperI20TransactionSerParams6CBlockEEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIR7CTxUndoEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIR10CBlockUndoEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsI13ParamsWrapperIN8CNetAddr9SerParamsES2_EEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsI13ParamsWrapperIN8CNetAddr9SerParamsE8CServiceEEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIR14CMessageHeaderEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsI13ParamsWrapperIN8CAddress9SerParamsES2_EEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIR4CInvEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIR12CBloomFilterEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIR7WrapperI16TxOutCompressionR6CTxOutEEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIR17BlockTransactionsEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIR24BlockTransactionsRequestEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIRN4node16SnapshotMetadataEEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIRSt5arrayIhLm5EEEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIRtEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIRSt5arrayIhLm4EEEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIRmEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIR7uint160EERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIRSt6vectorI12CBlockHeaderSaIS2_EEEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIRlEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIRiEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIRsEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIRaEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIRbEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIRSt6vectorI5CTxInSaIS2_EEEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIRSt6vectorI6CTxOutSaIS2_EEEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIRSt6vectorIS1_IhSaIhEESaIS3_EEEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIR6CTxOutEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIR7WrapperI22LimitedStringFormatterILm10EERNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIRNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsI13ParamsWrapperIN8CAddress9SerParamsE8CNetAddrEEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIRN6wallet12CKeyMetadataEEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIRN6wallet8CKeyPoolEEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIiEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIRN6wallet16WalletDescriptorEEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIRN6wallet9CWalletTxEEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsI13ParamsWrapperI20TransactionSerParamsSt10shared_ptrIK12CTransactionEEEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIRSt6vectorI7uint256SaIS2_EEEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIRSt6vectorIN6wallet9CMerkleTxESaIS3_EEEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIRSt3mapINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES7_St4lessIS7_ESaISt4pairIKS7_S7_EEEEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIRSt6vectorISt4pairINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES8_ESaIS9_EEEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIR22transaction_identifierILb0EEEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIRSt6vectorIh16secure_allocatorIhEEEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIRN6wallet10CMasterKeyEEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIRN6wallet8CHDChainEEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsI7WrapperI19CustomUintFormatterILi1ELb0EERN11AddrManImpl6FormatEEEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsI7WrapperI19CustomUintFormatterILi8ELb0EER12ServiceFlagsEEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsI7WrapperI22LimitedStringFormatterILm256EERNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsI13ParamsWrapperIN8CAddress9SerParamsESt6vectorIS2_SaIS2_EEEEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIRSt6vectorI4CInvSaIS2_EEEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIRSt4pairIh7uint256EEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIRSt6vectorI9COutPointSaIS2_EEEERS_OT_ Unexecuted instantiation: txdb.cpp:_ZN10DataStreamrsIRN12_GLOBAL__N_19CoinEntryEEERS_OT_ Unexecuted instantiation: blockfilterindex.cpp:_ZN10DataStreamrsIRN12_GLOBAL__N_111DBHeightKeyEEERS_OT_ Unexecuted instantiation: blockfilterindex.cpp:_ZN10DataStreamrsIRSt4pairI7uint256N12_GLOBAL__N_15DBValEEEERS_OT_ Unexecuted instantiation: blockfilterindex.cpp:_ZN10DataStreamrsIRN12_GLOBAL__N_15DBValEEERS_OT_ Unexecuted instantiation: coinstatsindex.cpp:_ZN10DataStreamrsIRN12_GLOBAL__N_111DBHeightKeyEEERS_OT_ Unexecuted instantiation: coinstatsindex.cpp:_ZN10DataStreamrsIRSt4pairI7uint256N12_GLOBAL__N_15DBValEEEERS_OT_ Unexecuted instantiation: coinstatsindex.cpp:_ZN10DataStreamrsIRN12_GLOBAL__N_15DBValEEERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIR10MuHash3072EERS_OT_ Unexecuted instantiation: _ZN10DataStreamrsIR10CDiskTxPosEERS_OT_ |
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 | | void Xor(const std::vector<unsigned char>& key) |
277 | 0 | { |
278 | 0 | util::Xor(MakeWritableByteSpan(*this), MakeByteSpan(key)); |
279 | 0 | } |
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 | 0 | 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 | 0 | uint64_t Read(int nbits) { |
304 | 0 | if (nbits < 0 || nbits > 64) { |
305 | 0 | throw std::out_of_range("nbits must be between 0 and 64"); |
306 | 0 | } |
307 | | |
308 | 0 | uint64_t data = 0; |
309 | 0 | while (nbits > 0) { |
310 | 0 | if (m_offset == 8) { |
311 | 0 | m_istream >> m_buffer; |
312 | 0 | m_offset = 0; |
313 | 0 | } |
314 | |
|
315 | 0 | int bits = std::min(8 - m_offset, nbits); |
316 | 0 | data <<= bits; |
317 | 0 | data |= static_cast<uint8_t>(m_buffer << m_offset) >> (8 - bits); |
318 | 0 | m_offset += bits; |
319 | 0 | nbits -= bits; |
320 | 0 | } |
321 | 0 | return data; |
322 | 0 | } |
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 | 0 | explicit BitStreamWriter(OStream& ostream) : m_ostream(ostream) {} |
342 | | |
343 | | ~BitStreamWriter() |
344 | 0 | { |
345 | 0 | Flush(); |
346 | 0 | } |
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 | 0 | void Write(uint64_t data, int nbits) { |
352 | 0 | if (nbits < 0 || nbits > 64) { |
353 | 0 | throw std::out_of_range("nbits must be between 0 and 64"); |
354 | 0 | } |
355 | | |
356 | 0 | while (nbits > 0) { |
357 | 0 | int bits = std::min(8 - m_offset, nbits); |
358 | 0 | m_buffer |= (data << (64 - nbits)) >> (64 - 8 + m_offset); |
359 | 0 | m_offset += bits; |
360 | 0 | nbits -= bits; |
361 | |
|
362 | 0 | if (m_offset == 8) { |
363 | 0 | Flush(); |
364 | 0 | } |
365 | 0 | } |
366 | 0 | } |
367 | | |
368 | | /** Flush any unwritten bits to the output stream, padding with 0's to the |
369 | | * next byte boundary. |
370 | | */ |
371 | 0 | void Flush() { |
372 | 0 | if (m_offset == 0) { |
373 | 0 | return; |
374 | 0 | } |
375 | | |
376 | 0 | m_ostream << m_buffer; |
377 | 0 | m_buffer = 0; |
378 | 0 | m_offset = 0; |
379 | 0 | } |
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 | | std::optional<int64_t> m_position; |
394 | | |
395 | | public: |
396 | | explicit AutoFile(std::FILE* file, std::vector<std::byte> data_xor={}); |
397 | | |
398 | 0 | ~AutoFile() { fclose(); } |
399 | | |
400 | | // Disallow copies |
401 | | AutoFile(const AutoFile&) = delete; |
402 | | AutoFile& operator=(const AutoFile&) = delete; |
403 | | |
404 | 0 | bool feof() const { return std::feof(m_file); } |
405 | | |
406 | | int fclose() |
407 | 0 | { |
408 | 0 | if (auto rel{release()}) return std::fclose(rel); |
409 | 0 | return 0; |
410 | 0 | } |
411 | | |
412 | | /** Get wrapped FILE* with transfer of ownership. |
413 | | * @note This will invalidate the AutoFile object, and makes it the responsibility of the caller |
414 | | * of this function to clean up the returned FILE*. |
415 | | */ |
416 | | std::FILE* release() |
417 | 0 | { |
418 | 0 | std::FILE* ret{m_file}; |
419 | 0 | m_file = nullptr; |
420 | 0 | return ret; |
421 | 0 | } |
422 | | |
423 | | /** Return true if the wrapped FILE* is nullptr, false otherwise. |
424 | | */ |
425 | 0 | bool IsNull() const { return m_file == nullptr; } |
426 | | |
427 | | /** Continue with a different XOR key */ |
428 | 0 | void SetXor(std::vector<std::byte> data_xor) { m_xor = data_xor; } |
429 | | |
430 | | /** Implementation detail, only used internally. */ |
431 | | std::size_t detail_fread(Span<std::byte> dst); |
432 | | |
433 | | /** Wrapper around fseek(). Will throw if seeking is not possible. */ |
434 | | void seek(int64_t offset, int origin); |
435 | | |
436 | | /** Find position within the file. Will throw if unknown. */ |
437 | | int64_t tell(); |
438 | | |
439 | | /** Wrapper around FileCommit(). */ |
440 | | bool Commit(); |
441 | | |
442 | | /** Wrapper around TruncateFile(). */ |
443 | | bool Truncate(unsigned size); |
444 | | |
445 | | // |
446 | | // Stream subset |
447 | | // |
448 | | void read(Span<std::byte> dst); |
449 | | void ignore(size_t nSize); |
450 | | void write(Span<const std::byte> src); |
451 | | |
452 | | template <typename T> |
453 | | AutoFile& operator<<(const T& obj) |
454 | 0 | { |
455 | 0 | ::Serialize(*this, obj); |
456 | 0 | return *this; |
457 | 0 | } Unexecuted instantiation: _ZN8AutoFilelsIbEERS_RKT_ Unexecuted instantiation: _ZN8AutoFilelsIaEERS_RKT_ Unexecuted instantiation: _ZN8AutoFilelsIhEERS_RKT_ Unexecuted instantiation: _ZN8AutoFilelsIsEERS_RKT_ Unexecuted instantiation: _ZN8AutoFilelsItEERS_RKT_ Unexecuted instantiation: _ZN8AutoFilelsIiEERS_RKT_ Unexecuted instantiation: _ZN8AutoFilelsIjEERS_RKT_ Unexecuted instantiation: _ZN8AutoFilelsIlEERS_RKT_ Unexecuted instantiation: _ZN8AutoFilelsImEERS_RKT_ Unexecuted instantiation: _ZN8AutoFilelsINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEERS_RKT_ Unexecuted instantiation: _ZN8AutoFilelsISt6vectorIhSaIhEEEERS_RKT_ Unexecuted instantiation: _ZN8AutoFilelsI4SpanIhEEERS_RKT_ Unexecuted instantiation: _ZN8AutoFilelsIN4node16SnapshotMetadataEEERS_RKT_ Unexecuted instantiation: _ZN8AutoFilelsISt5arrayIhLm5EEEERS_RKT_ Unexecuted instantiation: _ZN8AutoFilelsISt5arrayIhLm4EEEERS_RKT_ Unexecuted instantiation: _ZN8AutoFilelsI7uint256EERS_RKT_ Unexecuted instantiation: _ZN8AutoFilelsI4SpanIKhEEERS_RKT_ Unexecuted instantiation: _ZN8AutoFilelsI22transaction_identifierILb0EEEERS_RKT_ Unexecuted instantiation: _ZN8AutoFilelsI4CoinEERS_RKT_ Unexecuted instantiation: _ZN8AutoFilelsI7WrapperI15VarIntFormatterIL10VarIntMode0EERmEEERS_RKT_ Unexecuted instantiation: _ZN8AutoFilelsI7WrapperI15VarIntFormatterIL10VarIntMode0EERjEEERS_RKT_ Unexecuted instantiation: _ZN8AutoFilelsI4SpanIKcEEERS_RKT_ Unexecuted instantiation: _ZN8AutoFilelsISt5arrayISt4byteLm8EEEERS_RKT_ Unexecuted instantiation: _ZN8AutoFilelsI10CBlockUndoEERS_RKT_ Unexecuted instantiation: _ZN8AutoFilelsI13ParamsWrapperI20TransactionSerParamsK6CBlockEEERS_RKT_ Unexecuted instantiation: _ZN8AutoFilelsISt6vectorISt4byteSaIS2_EEEERS_RKT_ Unexecuted instantiation: _ZN8AutoFilelsI13ParamsWrapperI20TransactionSerParamsK12CTransactionEEERS_RKT_ Unexecuted instantiation: _ZN8AutoFilelsISt3mapI7uint256lSt4lessIS2_ESaISt4pairIKS2_lEEEEERS_RKT_ Unexecuted instantiation: _ZN8AutoFilelsISt3setI7uint256St4lessIS2_ESaIS2_EEEERS_RKT_ Unexecuted instantiation: fees.cpp:_ZN8AutoFilelsI7WrapperIN12_GLOBAL__N_122EncodedDoubleFormatterERKdEEERS_RKT_ Unexecuted instantiation: fees.cpp:_ZN8AutoFilelsI7WrapperI15VectorFormatterIN12_GLOBAL__N_122EncodedDoubleFormatterEERKSt6vectorIdSaIdEEEEERS_RKT_ Unexecuted instantiation: fees.cpp:_ZN8AutoFilelsI7WrapperI15VectorFormatterIS2_IN12_GLOBAL__N_122EncodedDoubleFormatterEEERKSt6vectorIS7_IdSaIdEESaIS9_EEEEERS_RKT_ |
458 | | |
459 | | template <typename T> |
460 | | AutoFile& operator>>(T&& obj) |
461 | 0 | { |
462 | 0 | ::Unserialize(*this, obj); |
463 | 0 | return *this; |
464 | 0 | } Unexecuted instantiation: _ZN8AutoFilersIRbEERS_OT_ Unexecuted instantiation: _ZN8AutoFilersIRaEERS_OT_ Unexecuted instantiation: _ZN8AutoFilersIRhEERS_OT_ Unexecuted instantiation: _ZN8AutoFilersIRsEERS_OT_ Unexecuted instantiation: _ZN8AutoFilersIRtEERS_OT_ Unexecuted instantiation: _ZN8AutoFilersIRiEERS_OT_ Unexecuted instantiation: _ZN8AutoFilersIRjEERS_OT_ Unexecuted instantiation: _ZN8AutoFilersIRlEERS_OT_ Unexecuted instantiation: _ZN8AutoFilersIRmEERS_OT_ Unexecuted instantiation: _ZN8AutoFilersIRNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEERS_OT_ Unexecuted instantiation: _ZN8AutoFilersIRSt6vectorIhSaIhEEEERS_OT_ Unexecuted instantiation: _ZN8AutoFilersIRN4node16SnapshotMetadataEEERS_OT_ Unexecuted instantiation: _ZN8AutoFilersIRSt5arrayIhLm5EEEERS_OT_ Unexecuted instantiation: _ZN8AutoFilersIRSt5arrayIhLm4EEEERS_OT_ Unexecuted instantiation: _ZN8AutoFilersIR7uint256EERS_OT_ Unexecuted instantiation: _ZN8AutoFilersIRN6wallet8MetaPageEEERS_OT_ Unexecuted instantiation: _ZN8AutoFilersIRSt5arrayISt4byteLm20EEEERS_OT_ Unexecuted instantiation: _ZN8AutoFilersIRA368_cEERS_OT_ Unexecuted instantiation: _ZN8AutoFilersIRA12_cEERS_OT_ Unexecuted instantiation: _ZN8AutoFilersIRA20_hEERS_OT_ Unexecuted instantiation: _ZN8AutoFilersIRA16_hEERS_OT_ Unexecuted instantiation: _ZN8AutoFilersIRN6wallet10PageHeaderEEERS_OT_ Unexecuted instantiation: _ZN8AutoFilersIRN6wallet11RecordsPageEEERS_OT_ Unexecuted instantiation: _ZN8AutoFilersIRN6wallet12RecordHeaderEEERS_OT_ Unexecuted instantiation: _ZN8AutoFilersIRN6wallet10DataRecordEEERS_OT_ Unexecuted instantiation: _ZN8AutoFilersIRN6wallet14OverflowRecordEEERS_OT_ Unexecuted instantiation: _ZN8AutoFilersIRN6wallet12InternalPageEEERS_OT_ Unexecuted instantiation: _ZN8AutoFilersIRN6wallet14InternalRecordEEERS_OT_ Unexecuted instantiation: _ZN8AutoFilersIRN6wallet12OverflowPageEEERS_OT_ Unexecuted instantiation: _ZN8AutoFilersI7WrapperI19CustomUintFormatterILi1ELb0EERN11AddrManImpl6FormatEEEERS_OT_ Unexecuted instantiation: _ZN8AutoFilersIRSt5arrayISt4byteLm8EEEERS_OT_ Unexecuted instantiation: _ZN8AutoFilersI13ParamsWrapperI20TransactionSerParams6CBlockEEERS_OT_ Unexecuted instantiation: _ZN8AutoFilersIRSt6vectorISt4byteSaIS2_EEEERS_OT_ Unexecuted instantiation: _ZN8AutoFilersI13ParamsWrapperI20TransactionSerParamsSt10shared_ptrIK12CTransactionEEEERS_OT_ Unexecuted instantiation: _ZN8AutoFilersIRSt3mapI7uint256lSt4lessIS2_ESaISt4pairIKS2_lEEEEERS_OT_ Unexecuted instantiation: _ZN8AutoFilersIRSt3setI7uint256St4lessIS2_ESaIS2_EEEERS_OT_ Unexecuted instantiation: fees.cpp:_ZN8AutoFilersI7WrapperIN12_GLOBAL__N_122EncodedDoubleFormatterERdEEERS_OT_ Unexecuted instantiation: fees.cpp:_ZN8AutoFilersI7WrapperI15VectorFormatterIN12_GLOBAL__N_122EncodedDoubleFormatterEERSt6vectorIdSaIdEEEEERS_OT_ Unexecuted instantiation: fees.cpp:_ZN8AutoFilersI7WrapperI15VectorFormatterIS2_IN12_GLOBAL__N_122EncodedDoubleFormatterEEERSt6vectorIS7_IdSaIdEESaIS9_EEEEERS_OT_ Unexecuted instantiation: _ZN8AutoFilersIR22transaction_identifierILb0EEEERS_OT_ Unexecuted instantiation: _ZN8AutoFilersIR4CoinEERS_OT_ Unexecuted instantiation: _ZN8AutoFilersI7WrapperI15VarIntFormatterIL10VarIntMode0EERmEEERS_OT_ Unexecuted instantiation: _ZN8AutoFilersI7WrapperI15VarIntFormatterIL10VarIntMode0EERjEEERS_OT_ Unexecuted instantiation: _ZN8AutoFilersI4SpanIhEEERS_OT_ Unexecuted instantiation: _ZN8AutoFilersIRSt4byteEERS_OT_ Unexecuted instantiation: _ZN8AutoFilersIR12CBlockHeaderEERS_OT_ |
465 | | }; |
466 | | |
467 | | /** Wrapper around an AutoFile& that implements a ring buffer to |
468 | | * deserialize from. It guarantees the ability to rewind a given number of bytes. |
469 | | * |
470 | | * Will automatically close the file when it goes out of scope if not null. |
471 | | * If you need to close the file early, use file.fclose() instead of fclose(file). |
472 | | */ |
473 | | class BufferedFile |
474 | | { |
475 | | private: |
476 | | AutoFile& m_src; |
477 | | uint64_t nSrcPos{0}; //!< how many bytes have been read from source |
478 | | uint64_t m_read_pos{0}; //!< how many bytes have been read from this |
479 | | uint64_t nReadLimit; //!< up to which position we're allowed to read |
480 | | uint64_t nRewind; //!< how many bytes we guarantee to rewind |
481 | | std::vector<std::byte> vchBuf; //!< the buffer |
482 | | |
483 | | //! read data from the source to fill the buffer |
484 | 0 | bool Fill() { |
485 | 0 | unsigned int pos = nSrcPos % vchBuf.size(); |
486 | 0 | unsigned int readNow = vchBuf.size() - pos; |
487 | 0 | unsigned int nAvail = vchBuf.size() - (nSrcPos - m_read_pos) - nRewind; |
488 | 0 | if (nAvail < readNow) |
489 | 0 | readNow = nAvail; |
490 | 0 | if (readNow == 0) |
491 | 0 | return false; |
492 | 0 | size_t nBytes{m_src.detail_fread(Span{vchBuf}.subspan(pos, readNow))}; |
493 | 0 | if (nBytes == 0) { |
494 | 0 | throw std::ios_base::failure{m_src.feof() ? "BufferedFile::Fill: end of file" : "BufferedFile::Fill: fread failed"}; |
495 | 0 | } |
496 | 0 | nSrcPos += nBytes; |
497 | 0 | return true; |
498 | 0 | } |
499 | | |
500 | | //! Advance the stream's read pointer (m_read_pos) by up to 'length' bytes, |
501 | | //! filling the buffer from the file so that at least one byte is available. |
502 | | //! Return a pointer to the available buffer data and the number of bytes |
503 | | //! (which may be less than the requested length) that may be accessed |
504 | | //! beginning at that pointer. |
505 | | std::pair<std::byte*, size_t> AdvanceStream(size_t length) |
506 | 0 | { |
507 | 0 | assert(m_read_pos <= nSrcPos); |
508 | 0 | if (m_read_pos + length > nReadLimit) { |
509 | 0 | throw std::ios_base::failure("Attempt to position past buffer limit"); |
510 | 0 | } |
511 | | // If there are no bytes available, read from the file. |
512 | 0 | if (m_read_pos == nSrcPos && length > 0) Fill(); |
513 | |
|
514 | 0 | size_t buffer_offset{static_cast<size_t>(m_read_pos % vchBuf.size())}; |
515 | 0 | size_t buffer_available{static_cast<size_t>(vchBuf.size() - buffer_offset)}; |
516 | 0 | size_t bytes_until_source_pos{static_cast<size_t>(nSrcPos - m_read_pos)}; |
517 | 0 | size_t advance{std::min({length, buffer_available, bytes_until_source_pos})}; |
518 | 0 | m_read_pos += advance; |
519 | 0 | return std::make_pair(&vchBuf[buffer_offset], advance); |
520 | 0 | } |
521 | | |
522 | | public: |
523 | | BufferedFile(AutoFile& file, uint64_t nBufSize, uint64_t nRewindIn) |
524 | 0 | : m_src{file}, nReadLimit{std::numeric_limits<uint64_t>::max()}, nRewind{nRewindIn}, vchBuf(nBufSize, std::byte{0}) |
525 | 0 | { |
526 | 0 | if (nRewindIn >= nBufSize) |
527 | 0 | throw std::ios_base::failure("Rewind limit must be less than buffer size"); |
528 | 0 | } |
529 | | |
530 | | //! check whether we're at the end of the source file |
531 | 0 | bool eof() const { |
532 | 0 | return m_read_pos == nSrcPos && m_src.feof(); |
533 | 0 | } |
534 | | |
535 | | //! read a number of bytes |
536 | | void read(Span<std::byte> dst) |
537 | 0 | { |
538 | 0 | while (dst.size() > 0) { |
539 | 0 | auto [buffer_pointer, length]{AdvanceStream(dst.size())}; |
540 | 0 | memcpy(dst.data(), buffer_pointer, length); |
541 | 0 | dst = dst.subspan(length); |
542 | 0 | } |
543 | 0 | } |
544 | | |
545 | | //! Move the read position ahead in the stream to the given position. |
546 | | //! Use SetPos() to back up in the stream, not SkipTo(). |
547 | | void SkipTo(const uint64_t file_pos) |
548 | 0 | { |
549 | 0 | assert(file_pos >= m_read_pos); |
550 | 0 | while (m_read_pos < file_pos) AdvanceStream(file_pos - m_read_pos); |
551 | 0 | } |
552 | | |
553 | | //! return the current reading position |
554 | 0 | uint64_t GetPos() const { |
555 | 0 | return m_read_pos; |
556 | 0 | } |
557 | | |
558 | | //! rewind to a given reading position |
559 | 0 | bool SetPos(uint64_t nPos) { |
560 | 0 | size_t bufsize = vchBuf.size(); |
561 | 0 | if (nPos + bufsize < nSrcPos) { |
562 | | // rewinding too far, rewind as far as possible |
563 | 0 | m_read_pos = nSrcPos - bufsize; |
564 | 0 | return false; |
565 | 0 | } |
566 | 0 | if (nPos > nSrcPos) { |
567 | | // can't go this far forward, go as far as possible |
568 | 0 | m_read_pos = nSrcPos; |
569 | 0 | return false; |
570 | 0 | } |
571 | 0 | m_read_pos = nPos; |
572 | 0 | return true; |
573 | 0 | } |
574 | | |
575 | | //! prevent reading beyond a certain position |
576 | | //! no argument removes the limit |
577 | 0 | bool SetLimit(uint64_t nPos = std::numeric_limits<uint64_t>::max()) { |
578 | 0 | if (nPos < m_read_pos) |
579 | 0 | return false; |
580 | 0 | nReadLimit = nPos; |
581 | 0 | return true; |
582 | 0 | } |
583 | | |
584 | | template<typename T> |
585 | 0 | BufferedFile& operator>>(T&& obj) { |
586 | 0 | ::Unserialize(*this, obj); |
587 | 0 | return (*this); |
588 | 0 | } Unexecuted instantiation: _ZN12BufferedFilersIRbEERS_OT_ Unexecuted instantiation: _ZN12BufferedFilersIRaEERS_OT_ Unexecuted instantiation: _ZN12BufferedFilersIRhEERS_OT_ Unexecuted instantiation: _ZN12BufferedFilersIRsEERS_OT_ Unexecuted instantiation: _ZN12BufferedFilersIRtEERS_OT_ Unexecuted instantiation: _ZN12BufferedFilersIRiEERS_OT_ Unexecuted instantiation: _ZN12BufferedFilersIRjEERS_OT_ Unexecuted instantiation: _ZN12BufferedFilersIRlEERS_OT_ Unexecuted instantiation: _ZN12BufferedFilersIRmEERS_OT_ Unexecuted instantiation: _ZN12BufferedFilersIRNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEERS_OT_ Unexecuted instantiation: _ZN12BufferedFilersIRSt6vectorIhSaIhEEEERS_OT_ Unexecuted instantiation: _ZN12BufferedFilersIRSt5arrayIhLm4EEEERS_OT_ Unexecuted instantiation: _ZN12BufferedFilersIR12CBlockHeaderEERS_OT_ Unexecuted instantiation: _ZN12BufferedFilersI13ParamsWrapperI20TransactionSerParams6CBlockEEERS_OT_ |
589 | | |
590 | | //! search for a given byte in the stream, and remain positioned on it |
591 | | void FindByte(std::byte byte) |
592 | 0 | { |
593 | | // For best performance, avoid mod operation within the loop. |
594 | 0 | size_t buf_offset{size_t(m_read_pos % uint64_t(vchBuf.size()))}; |
595 | 0 | while (true) { |
596 | 0 | if (m_read_pos == nSrcPos) { |
597 | | // No more bytes available; read from the file into the buffer, |
598 | | // setting nSrcPos to one beyond the end of the new data. |
599 | | // Throws exception if end-of-file reached. |
600 | 0 | Fill(); |
601 | 0 | } |
602 | 0 | const size_t len{std::min<size_t>(vchBuf.size() - buf_offset, nSrcPos - m_read_pos)}; |
603 | 0 | const auto it_start{vchBuf.begin() + buf_offset}; |
604 | 0 | const auto it_find{std::find(it_start, it_start + len, byte)}; |
605 | 0 | const size_t inc{size_t(std::distance(it_start, it_find))}; |
606 | 0 | m_read_pos += inc; |
607 | 0 | if (inc < len) break; |
608 | 0 | buf_offset += inc; |
609 | 0 | if (buf_offset >= vchBuf.size()) buf_offset = 0; |
610 | 0 | } |
611 | 0 | } |
612 | | }; |
613 | | |
614 | | #endif // BITCOIN_STREAMS_H |