// Copyright (c) 2023, The Monero Project // All rights reserved. // // Redistribution and use in source and binary forms, with or without modification, are // permitted provided that the following conditions are met: // // 1. Redistributions of source code must retain the above copyright notice, this list of // conditions and the following disclaimer. // // 2. Redistributions in binary form must reproduce the above copyright notice, this list // of conditions and the following disclaimer in the documentation and/or other // materials provided with the distribution. // // 3. Neither the name of the copyright holder nor the names of its contributors may be // used to endorse or promote products derived from this software without specific // prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #pragma once #include #include #include #include #include #include "byte_stream.h" // monero/contrib/epee/include #include "span.h" // monero/contrib/epee/include #include "wire/field.h" #include "wire/filters.h" #include "wire/msgpack/base.h" #include "wire/traits.h" #include "wire/write.h" namespace wire { //! Writes MSGPACK tokens one-at-a-time for DOMless output. class msgpack_writer : public writer { epee::byte_stream bytes_; std::size_t expected_; //! Tracks number of expected elements remaining const bool integer_keys_; bool needs_flush_; //! Provided data currently in `bytes_`. virtual void do_flush(epee::span); //! Flush written bytes to `do_flush(...)` if configured void check_flush(); void do_integer(std::intmax_t); void do_unsigned_integer(std::uintmax_t); template void integer_t(const T value) { if (0 <= value) { if (value <= msgpack::ftag_unsigned::max()) bytes_.put(std::uint8_t(value)); else // if multibyte do_unsigned_integer(std::uintmax_t(value)); } else // if negative do_integer(value); --expected_; } template void unsigned_integer_t(const T value) { if (value <= msgpack::ftag_unsigned::max()) bytes_.put(std::uint8_t(value)); else // if multibyte do_unsigned_integer(value); --expected_; } protected: msgpack_writer(epee::byte_stream&& sink, bool integer_keys, bool needs_flush) : writer(), bytes_(std::move(sink)), expected_(1), integer_keys_(integer_keys), needs_flush_(needs_flush) {} //! \throw std::logic_error if tree was not completed void check_complete(); //! \throw std::logic_error if incomplete msgpack tree. \return msgpack bytes epee::byte_stream take_msgpack(); //! Flush bytes in local buffer to `do_flush(...)` void flush() { do_flush({bytes_.data(), bytes_.size()}); bytes_.clear(); } public: msgpack_writer(const msgpack_writer&) = delete; virtual ~msgpack_writer() noexcept; msgpack_writer& operator=(const msgpack_writer&) = delete; void boolean(const bool value) override final { if (value) bytes_.put(std::uint8_t(msgpack::tag::True)); else bytes_.put(std::uint8_t(msgpack::tag::False)); --expected_; } void integer(const int value) override final { integer_t(value); } void integer(const std::intmax_t value) override final { integer_t(value); } void unsigned_integer(const unsigned value) override final { unsigned_integer_t(value); } void unsigned_integer(const std::uintmax_t value) override final { unsigned_integer_t(value); } void real(double) override final; //! \throw wire::exception if `source.size()` exceeds 2^32-1 void string(boost::string_ref source) override final; //! \throw wire::exception if `source.size()` exceeds 2^32-1 void binary(epee::span source) override final; //! \throw wire::exception if `items` exceeds 2^32-1. void start_array(std::size_t items) override final; void end_array() override final { --expected_; } //! \throw wire::exception if `items` exceeds 2^32-1 void start_object(std::size_t items) override final; void key(std::uintmax_t) override final; void key(boost::string_ref) override final; void key(unsigned, boost::string_ref) override final; void end_object() override final { --expected_; } }; //! Buffers entire JSON message in memory struct msgpack_slice_writer final : msgpack_writer { explicit msgpack_slice_writer(epee::byte_stream&& sink, bool integer_keys = false) : msgpack_writer(std::move(sink), integer_keys, false) {} explicit msgpack_slice_writer(bool integer_keys = false) : msgpack_slice_writer(epee::byte_stream{}, integer_keys) {} //! \throw std::logic_error if incomplete JSON tree \return JSON bytes epee::byte_stream take_sink() { return msgpack_writer::take_msgpack(); } }; //! Periodically flushes MsgPack data to `std::ostream` class msgpack_stream_writer final : public msgpack_writer { std::ostream& dest; virtual void do_flush(epee::span) override final; public: explicit msgpack_stream_writer(std::ostream& dest, bool integer_keys = false) : msgpack_writer(epee::byte_stream{}, integer_keys, true), dest(dest) {} //! Flush remaining bytes to stream \throw std::logic_error if incomplete JSON tree void finish() { check_complete(); flush(); } }; }