From 4fa1edf086555af5c34239f5350323239a3be78d Mon Sep 17 00:00:00 2001 From: Kevin Heifner Date: Wed, 5 Feb 2025 10:22:35 -0600 Subject: [PATCH] GH-1062 Add new datastream_mirror which keeps a copy of the stream data as it reads. --- libraries/libfc/include/fc/io/datastream.hpp | 32 ++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/libraries/libfc/include/fc/io/datastream.hpp b/libraries/libfc/include/fc/io/datastream.hpp index 47a9fbf70a..b6c427a913 100644 --- a/libraries/libfc/include/fc/io/datastream.hpp +++ b/libraries/libfc/include/fc/io/datastream.hpp @@ -176,6 +176,38 @@ class datastream +class datastream_mirror { +public: + explicit datastream_mirror( DataStream& ds, size_t reserve = 0 ) : ds(ds) { + mirror.reserve(reserve); + } + + void skip( size_t s ) { ds.skip(s); } + bool read( char* d, size_t s ) { + if (ds.read(d, s)) { + auto size = mirror.size(); + mirror.resize(size + s); + memcpy(mirror.data() + size, d, s); + return true; + } + return false; + } + + bool get( unsigned char& c ) { return read(&c, 1); } + bool get( char& c ) { return read(&c, 1); } + + std::vector extract_mirror() { return std::move(mirror); } + +private: + DataStream& ds; + std::vector mirror; +}; template