10 #ifndef MSGPACK_V3_PARSE_HPP
11 #define MSGPACK_V3_PARSE_HPP
13 #if MSGPACK_DEFAULT_API_VERSION >= 2
17 #include <boost/assert.hpp>
29 template <
typename VisitorHolder>
42 holder().visitor().init();
49 static uint32_t next_cs(T p)
51 return static_cast<uint32_t
>(*p) & 0x1f;
54 VisitorHolder& holder() {
55 return static_cast<VisitorHolder&
>(*this);
58 template <
typename T,
typename StartVisitor,
typename EndVisitor>
60 StartVisitor
const& sv,
65 load<T>(
size, load_pos);
68 off =
static_cast<std::size_t
>(m_current - m_start);
72 off =
static_cast<std::size_t
>(m_current - m_start);
78 off =
static_cast<std::size_t
>(m_current - m_start);
84 off =
static_cast<std::size_t
>(m_current - m_start);
87 parse_return ret = m_stack.push(holder(), sv.type(),
static_cast<uint32_t
>(
size));
90 off =
static_cast<std::size_t
>(m_current - m_start);
98 parse_return after_visit_proc(
bool visit_result, std::size_t& off) {
100 off =
static_cast<std::size_t
>(m_current - m_start);
103 parse_return ret = m_stack.consume(holder(), m_current);
106 off =
static_cast<std::size_t
>(m_current - m_start);
113 array_sv(VisitorHolder& visitor_holder):m_visitor_holder(visitor_holder) {}
114 bool operator()(uint32_t
size)
const {
115 return m_visitor_holder.visitor().start_array(
size);
119 VisitorHolder& m_visitor_holder;
122 array_ev(VisitorHolder& visitor_holder):m_visitor_holder(visitor_holder) {}
123 bool operator()()
const {
124 return m_visitor_holder.visitor().end_array();
127 VisitorHolder& m_visitor_holder;
130 map_sv(VisitorHolder& visitor_holder):m_visitor_holder(visitor_holder) {}
131 bool operator()(uint32_t
size)
const {
132 return m_visitor_holder.visitor().start_map(
size);
136 VisitorHolder& m_visitor_holder;
139 map_ev(VisitorHolder& visitor_holder):m_visitor_holder(visitor_holder) {}
140 bool operator()()
const {
141 return m_visitor_holder.visitor().end_map();
144 VisitorHolder& m_visitor_holder;
147 struct unpack_stack {
157 m_stack.push_back(stack_elem(type, rest));
170 parse_return consume(VisitorHolder& visitor_holder,
char const*& current) {
171 while (!m_stack.empty()) {
172 stack_elem& e = m_stack.back();
175 if (!visitor_holder.visitor().end_array_item()) {
179 if (--e.m_rest == 0) {
181 if (!visitor_holder.visitor().end_array()) {
192 if (!visitor_holder.visitor().end_map_key()) {
200 if (!visitor_holder.visitor().end_map_value()) {
204 if (--e.m_rest == 0) {
206 if (!visitor_holder.visitor().end_map()) {
221 bool empty()
const {
return m_stack.empty(); }
222 void clear() { m_stack.clear(); }
224 std::vector<stack_elem> m_stack;
228 char const* m_current;
232 uint32_t m_num_elements;
233 unpack_stack m_stack;
236 template <std::
size_t N>
237 inline void check_ext_size(std::size_t ) {
241 inline void check_ext_size<4>(std::size_t
size) {
245 template <
typename VisitorHolder>
248 BOOST_ASSERT(len >= off);
251 m_current = data + off;
252 const char*
const pe = data + len;
255 if(m_current == pe) {
256 off =
static_cast<std::size_t
>(m_current - m_start);
259 bool fixed_trail_again =
false;
262 fixed_trail_again =
false;
263 int selector = *
reinterpret_cast<const unsigned char*
>(m_current);
264 if (0x00 <= selector && selector <= 0x7f) {
265 uint8_t tmp = *
reinterpret_cast<const uint8_t*
>(m_current);
266 bool visret = holder().visitor().visit_positive_integer(tmp);
269 }
else if(0xe0 <= selector && selector <= 0xff) {
270 int8_t tmp = *
reinterpret_cast<const int8_t*
>(m_current);
271 bool visret = holder().visitor().visit_negative_integer(tmp);
274 }
else if (0xc4 <= selector && selector <= 0xdf) {
275 const uint32_t trail[] = {
305 m_trail = trail[selector - 0xc4];
306 m_cs = next_cs(m_current);
307 fixed_trail_again =
true;
308 }
else if(0xa0 <= selector && selector <= 0xbf) {
309 m_trail =
static_cast<uint32_t
>(*m_current) & 0x1f;
311 bool visret = holder().visitor().visit_str(n,
static_cast<uint32_t
>(m_trail));
317 fixed_trail_again =
true;
319 }
else if(0x90 <= selector && selector <= 0x9f) {
320 parse_return ret = start_aggregate<fix_tag>(array_sv(holder()), array_ev(holder()), m_current, off);
322 }
else if(0x80 <= selector && selector <= 0x8f) {
323 parse_return ret = start_aggregate<fix_tag>(map_sv(holder()), map_ev(holder()), m_current, off);
325 }
else if(selector == 0xc2) {
326 bool visret = holder().visitor().visit_boolean(
false);
329 }
else if(selector == 0xc3) {
330 bool visret = holder().visitor().visit_boolean(
true);
333 }
else if(selector == 0xc0) {
334 bool visret = holder().visitor().visit_nil();
338 off =
static_cast<std::size_t
>(m_current - m_start);
339 holder().visitor().parse_error(off - 1, off);
345 if (fixed_trail_again) {
347 fixed_trail_again =
false;
349 if(
static_cast<std::size_t
>(pe - m_current) < m_trail) {
350 off =
static_cast<std::size_t
>(m_current - m_start);
354 m_current += m_trail - 1;
359 union { uint32_t i;
float f; } mem;
360 load<uint32_t>(mem.i, n);
361 bool visret = holder().visitor().visit_float32(mem.f);
366 union { uint64_t i;
double f; } mem;
367 load<uint64_t>(mem.i, n);
368 #if defined(TARGET_OS_IPHONE)
370 #elif defined(__arm__) && !(__ARM_EABI__)
372 mem.i = (mem.i & 0xFFFFFFFFUL) << 32UL | (mem.i >> 32UL);
374 bool visret = holder().visitor().visit_float64(mem.f);
380 load<uint8_t>(tmp, n);
381 bool visret = holder().visitor().visit_positive_integer(tmp);
387 load<uint16_t>(tmp, n);
388 bool visret = holder().visitor().visit_positive_integer(tmp);
394 load<uint32_t>(tmp, n);
395 bool visret = holder().visitor().visit_positive_integer(tmp);
401 load<uint64_t>(tmp, n);
402 bool visret = holder().visitor().visit_positive_integer(tmp);
408 load<int8_t>(tmp, n);
409 bool visret = holder().visitor().visit_negative_integer(tmp);
415 load<int16_t>(tmp, n);
416 bool visret = holder().visitor().visit_negative_integer(tmp);
422 load<int32_t>(tmp, n);
423 bool visret = holder().visitor().visit_negative_integer(tmp);
429 load<int64_t>(tmp, n);
430 bool visret = holder().visitor().visit_negative_integer(tmp);
435 bool visret = holder().visitor().visit_ext(n, 1+1);
440 bool visret = holder().visitor().visit_ext(n, 2+1);
445 bool visret = holder().visitor().visit_ext(n, 4+1);
450 bool visret = holder().visitor().visit_ext(n, 8+1);
455 bool visret = holder().visitor().visit_ext(n, 16+1);
461 load<uint8_t>(tmp, n);
464 bool visret = holder().visitor().visit_str(n,
static_cast<uint32_t
>(m_trail));
470 fixed_trail_again =
true;
475 load<uint8_t>(tmp, n);
478 bool visret = holder().visitor().visit_bin(n,
static_cast<uint32_t
>(m_trail));
484 fixed_trail_again =
true;
489 load<uint8_t>(tmp, n);
492 bool visret = holder().visitor().visit_ext(n,
static_cast<uint32_t
>(m_trail));
498 fixed_trail_again =
true;
503 load<uint16_t>(tmp, n);
506 bool visret = holder().visitor().visit_str(n,
static_cast<uint32_t
>(m_trail));
512 fixed_trail_again =
true;
517 load<uint16_t>(tmp, n);
520 bool visret = holder().visitor().visit_bin(n,
static_cast<uint32_t
>(m_trail));
526 fixed_trail_again =
true;
531 load<uint16_t>(tmp, n);
534 bool visret = holder().visitor().visit_ext(n,
static_cast<uint32_t
>(m_trail));
540 fixed_trail_again =
true;
545 load<uint32_t>(tmp, n);
548 bool visret = holder().visitor().visit_str(n,
static_cast<uint32_t
>(m_trail));
554 fixed_trail_again =
true;
559 load<uint32_t>(tmp, n);
562 bool visret = holder().visitor().visit_bin(n,
static_cast<uint32_t
>(m_trail));
568 fixed_trail_again =
true;
573 load<uint32_t>(tmp, n);
574 check_ext_size<sizeof(std::size_t)>(tmp);
578 bool visret = holder().visitor().visit_ext(n,
static_cast<uint32_t
>(m_trail));
584 fixed_trail_again =
true;
588 bool visret = holder().visitor().visit_str(n,
static_cast<uint32_t
>(m_trail));
593 bool visret = holder().visitor().visit_bin(n,
static_cast<uint32_t
>(m_trail));
598 bool visret = holder().visitor().visit_ext(n,
static_cast<uint32_t
>(m_trail));
603 parse_return ret = start_aggregate<uint16_t>(array_sv(holder()), array_ev(holder()), n, off);
608 parse_return ret = start_aggregate<uint32_t>(array_sv(holder()), array_ev(holder()), n, off);
612 parse_return ret = start_aggregate<uint16_t>(map_sv(holder()), map_ev(holder()), n, off);
616 parse_return ret = start_aggregate<uint32_t>(map_sv(holder()), map_ev(holder()), n, off);
620 off =
static_cast<std::size_t
>(m_current - m_start);
621 holder().visitor().parse_error(
static_cast<std::size_t
>(n - m_start - 1),
static_cast<std::size_t
>(n - m_start));
625 }
while(m_current != pe);
627 off =
static_cast<std::size_t
>(m_current - m_start);
631 template <
typename Visitor>
632 struct parse_helper : detail::context<parse_helper<Visitor> > {
633 parse_helper(Visitor& v):m_visitor(v) {}
634 parse_return execute(
const char* data, std::size_t len, std::size_t& off) {
635 return detail::context<parse_helper<Visitor> >::execute(data, len, off);
637 Visitor& visitor()
const {
return m_visitor; }
641 template <
typename Visitor>
643 parse_imp(
const char* data,
size_t len,
size_t& off, Visitor& v) {
644 std::size_t noff = off;
647 v.insufficient_bytes(noff, noff);
650 detail::parse_helper<Visitor> h(v);
655 v.insufficient_bytes(noff - 1, noff);
int execute(const char *data, std::size_t len, std::size_t &off)
Definition: unpack.hpp:467
msgpack::object const & data() const
Definition: unpack.hpp:335
context(unpack_reference_func f, void *user_data, unpack_limit const &limit)
Definition: unpack.hpp:320
void init()
Definition: unpack.hpp:327
parse_return parse_imp(const char *data, size_t len, size_t &off, Visitor &v)
std::size_t size(T const &t)
Definition: size_equal_only.hpp:24
Definition: adaptor_base.hpp:15
parse_return
Definition: parse_return.hpp:23
@ PARSE_CONTINUE
Definition: parse_return.hpp:26
@ PARSE_EXTRA_BYTES
Definition: parse_return.hpp:25
@ PARSE_STOP_VISITOR
Definition: parse_return.hpp:28
@ PARSE_SUCCESS
Definition: parse_return.hpp:24
@ PARSE_PARSE_ERROR
Definition: parse_return.hpp:27
T type
Definition: unpack.hpp:286
Definition: unpack_exception.hpp:97
msgpack_container_type
Definition: unpack_define.hpp:68
@ MSGPACK_CT_ARRAY_ITEM
Definition: unpack_define.hpp:69
@ MSGPACK_CT_MAP_VALUE
Definition: unpack_define.hpp:71
@ MSGPACK_CT_MAP_KEY
Definition: unpack_define.hpp:70
#define MSGPACK_EMBED_STACK_SIZE
Definition: unpack_define.hpp:16
@ MSGPACK_CS_EXT_32
Definition: unpack_define.hpp:33
@ MSGPACK_CS_EXT_16
Definition: unpack_define.hpp:32
@ MSGPACK_CS_STR_8
Definition: unpack_define.hpp:52
@ MSGPACK_CS_STR_32
Definition: unpack_define.hpp:54
@ MSGPACK_CS_DOUBLE
Definition: unpack_define.hpp:36
@ MSGPACK_CS_FIXEXT_4
Definition: unpack_define.hpp:48
@ MSGPACK_CS_UINT_32
Definition: unpack_define.hpp:39
@ MSGPACK_CS_MAP_16
Definition: unpack_define.hpp:57
@ MSGPACK_CS_BIN_32
Definition: unpack_define.hpp:29
@ MSGPACK_CS_BIN_16
Definition: unpack_define.hpp:28
@ MSGPACK_CS_UINT_64
Definition: unpack_define.hpp:40
@ MSGPACK_CS_FLOAT
Definition: unpack_define.hpp:35
@ MSGPACK_CS_ARRAY_32
Definition: unpack_define.hpp:56
@ MSGPACK_CS_FIXEXT_1
Definition: unpack_define.hpp:46
@ MSGPACK_CS_INT_8
Definition: unpack_define.hpp:41
@ MSGPACK_CS_INT_32
Definition: unpack_define.hpp:43
@ MSGPACK_ACS_BIN_VALUE
Definition: unpack_define.hpp:63
@ MSGPACK_CS_ARRAY_16
Definition: unpack_define.hpp:55
@ MSGPACK_CS_FIXEXT_16
Definition: unpack_define.hpp:50
@ MSGPACK_CS_STR_16
Definition: unpack_define.hpp:53
@ MSGPACK_ACS_STR_VALUE
Definition: unpack_define.hpp:62
@ MSGPACK_CS_BIN_8
Definition: unpack_define.hpp:27
@ MSGPACK_CS_INT_64
Definition: unpack_define.hpp:44
@ MSGPACK_CS_FIXEXT_2
Definition: unpack_define.hpp:47
@ MSGPACK_CS_HEADER
Definition: unpack_define.hpp:21
@ MSGPACK_CS_FIXEXT_8
Definition: unpack_define.hpp:49
@ MSGPACK_CS_MAP_32
Definition: unpack_define.hpp:58
@ MSGPACK_ACS_EXT_VALUE
Definition: unpack_define.hpp:64
@ MSGPACK_CS_EXT_8
Definition: unpack_define.hpp:31
@ MSGPACK_CS_INT_16
Definition: unpack_define.hpp:42
@ MSGPACK_CS_UINT_16
Definition: unpack_define.hpp:38
@ MSGPACK_CS_UINT_8
Definition: unpack_define.hpp:37
#define MSGPACK_NULLPTR
Definition: cpp_config_decl.hpp:85
#define MSGPACK_API_VERSION_NAMESPACE(ns)
Definition: versioning.hpp:66