aboutsummaryrefslogtreecommitdiff
path: root/contrib/epee/include/serialization/wire/write.h
blob: c2359918c01c7770d6fff309325044d39e1e6ca5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// 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 <boost/utility/string_ref.hpp>
#include <boost/range/size.hpp>
#include <cstdint>
#include <iterator>
#include <system_error>
#include <type_traits>

#include "byte_slice.h"
#include "serialization/wire/error.h"
#include "serialization/wire/field.h"
#include "serialization/wire/traits.h"
#include "span.h"

/*
  Custom types (e.g type `type` in namespace `ns`) can define an output function by:
    * `namespace wire { template<> struct is_array<ns::type> : std::true_type {}; }`
    * `namespace wire { template<> struct is_blob<ns::type> : std::true_type {}; }`
    * `namespace wire { void write_bytes(writer&, const ns::type&); }`
    * `namespace ns { void write_bytes(wire::writer&, const type&); }`

  See `wrappers.h` for `is_array` requirements, and `traits.h` for `is_blob`
  requirements. `write_bytes` function can also specify derived type for faster
  output (i.e.  `namespace ns { void write_bytes(wire::epee_writer&, type&); }`).
  Using the derived type allows the compiler to de-virtualize and allows for
  custom functions not defined by base interface. Using base interface allows
  for multiple formats with minimal instruction count. */

namespace wire
{
  //! Interface for converting C/C++ objects to "wire" (byte) formats.
  struct writer
  {
    writer() = default;

    virtual ~writer() noexcept;

    //! By default, insist on retrieving array size before writing array
    static constexpr std::true_type need_array_size() noexcept { return{}; }

    virtual void boolean(bool) = 0;

    virtual void integer(std::intmax_t) = 0;
    virtual void unsigned_integer(std::uintmax_t) = 0;

    virtual void real(double) = 0;

    virtual void string(boost::string_ref) = 0;
    virtual void binary(epee::span<const std::uint8_t>) = 0;

    virtual void start_array(std::size_t) = 0;
    virtual void end_array() = 0;

    virtual void start_object(std::size_t) = 0;
    virtual void key(boost::string_ref) = 0;
    virtual void binary_key(epee::span<const std::uint8_t>) = 0;
    virtual void end_object() = 0;

  protected:
    writer(const writer&) = default;
    writer(writer&&) = default;
    writer& operator=(const writer&) = default;
    writer& operator=(writer&&) = default;
  };

  template<typename W>
  inline void write_arithmetic(W& dest, const bool source)
  { dest.boolean(source); }

  template<typename W>
  inline void write_arithmetic(W& dest, const int source)
  { dest.integer(source); }

  template<typename W>
  inline void write_arithmetic(W& dest, const long source)
  { dest.integer(std::intmax_t(source)); }

  template<typename W>
  inline void write_arithmetic(W& dest, const long long source)
  { dest.integer(std::intmax_t(source)); }

  template<typename W>
  inline void write_arithmetic(W& dest, const unsigned source)
  { dest.unsigned_integer(source); }

  template<typename W>
  inline void write_arithmetic(W& dest, const unsigned long source)
  { dest.unsigned_integer(std::uintmax_t(source)); }

  template<typename W>
  inline void write_arithmetic(W& dest, const unsigned long long source)
  { dest.unsigned_integer(std::uintmax_t(source));}

  template<typename W>
  inline void write_arithmetic(W& dest, const double source)
  { dest.real(source); }

  // Template both arguments to allow derived writer specializations
  template<typename W, typename T>
  inline std::enable_if_t<std::is_arithmetic<T>::value> write_bytes(W& dest, const T source)
  { write_arithmetic(dest, source); }

  template<typename W>
  inline void write_bytes(W& dest, const boost::string_ref source)
  { dest.string(source); }

  template<typename W, typename T>
  inline std::enable_if_t<is_blob<T>::value> write_bytes(W& dest, const T& source)
  { dest.binary(epee::as_byte_span(source)); }

  template<typename W>
  inline void write_bytes(W& dest, const epee::span<const std::uint8_t> source)
  { dest.binary(source); }

  template<typename W>
  inline void write_bytes(W& dest, const epee::byte_slice& source)
  { write_bytes(dest, epee::to_span(source)); }

  //! Use `write_bytes(...)` method if available for `T`.
  template<typename W, typename T>
  inline auto write_bytes(W& dest, const T& source) -> decltype(source.write_bytes(dest))
  { return source.write_bytes(dest); }
}

namespace wire_write
{
  /*! Don't add a function called `write_bytes` to this namespace, it will
      prevent ADL lookup. ADL lookup delays the function searching until the
      template is used instead of when its defined. This allows the unqualified
      calls to `write_bytes` in this namespace to "find" user functions that are
      declared after these functions. */

  template<typename W, typename T>
  inline void bytes(W& dest, const T& source)
  {
    write_bytes(dest, source); // ADL (searches every associated namespace)
  }

  //! Use writer `W` to convert `source` into bytes appended to `dest`.
  template<typename W, typename T, typename U>
  inline std::error_code to_bytes(T& dest, const U& source)
  {
    try
    {
      W out{std::move(dest)};
      bytes(out, source);
      dest = out.take_buffer();
    }
    catch (const wire::exception& e)
    {
      dest.clear();
      return e.code();
    }
    catch (...)
    {
      dest.clear();
      throw;
    }
    return {};
  }

  template<typename T>
  inline std::size_t array_size(std::true_type, const T& source)
  {
    static_assert(
      !std::is_same<typename std::iterator_traits<typename T::const_iterator>::iterator_category, std::input_iterator_tag>{},
      "Input iterators must use json (or similar) derived classes directly"
    );
    return boost::size(source);
  }

  template<typename T>
  inline constexpr std::size_t array_size(std::false_type, const T&) noexcept
  { return 0; }

  template<typename W, typename T>
  inline void array(W& dest, const T& source)
  {
    using value_type = typename T::value_type;
    static_assert(!std::is_same<value_type, char>::value, "write array of chars as string");
    static_assert(!std::is_same<value_type, std::int8_t>::value, "write array of signed chars as binary");
    static_assert(!std::is_same<value_type, std::uint8_t>::value, "write array of unsigned chars as binary");

    dest.start_array(array_size(dest.need_array_size(), source));
    for (const auto& elem : source)
      bytes(dest, elem);
    dest.end_array();
  }

  template<typename W, typename T>
  inline bool field(W& dest, const wire::field_<T, true>& field)
  {
    // Arrays always optional, see `wire/field.h`
    if (wire::available(field))
    {
      dest.key(field.name);
      bytes(dest, field.get_value());
    }
    return true;
  }

  template<typename W, typename T>
  inline bool field(W& dest, const wire::field_<T, false>& field)
  {
    if (wire::available(field))
    {
      dest.key(field.name);
      bytes(dest, *field.get_value());
    }
    return true;
  }

  template<typename W, typename T>
  inline std::enable_if_t<std::is_pod<T>::value> dynamic_object_key(W& dest, const T& source)
  {
    dest.binary_key(epee::as_byte_span(source));
  }

  template<typename W>
  inline void dynamic_object_key(W& dest, const boost::string_ref source)
  {
    dest.key(source);
  }

  template<typename W, typename T>
  inline void dynamic_object(W& dest, const T& source)
  {
    dest.start_object(source.size());
    for (const auto& elem : source)
    {
      dynamic_object_key(dest, elem.first);
      bytes(dest, elem.second);
    }
    dest.end_object();
  }

  template<typename W, typename... T>
  inline void object(W& dest, T&&... fields)
  {
    dest.start_object(wire::sum(std::size_t(wire::available(fields))...));
    const bool dummy[] = {field(dest, std::forward<T>(fields))...};
    dest.end_object();
    (void)dummy; // expand into array to get 0,1,2,etc order
  }
} // wire_write

namespace wire
{
  template<typename W, typename T>
  inline std::enable_if_t<is_array<T>::value> write_bytes(W& dest, const T& source)
  {
    wire_write::array(dest, source);
  }

  template<typename W, typename... T>
  inline std::enable_if_t<std::is_base_of<writer, W>::value> object(W& dest, T... fields)
  {
    wire_write::object(dest, std::move(fields)...);
  }

  template<typename W, typename... T>
  inline void object_fwd(const std::false_type /* is_read */, W& dest, T... fields)
  {
    wire::object(dest, std::move(fields)...);
  }
}