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