10 #ifndef MSGPACK_V2_PARSE_HPP
11 #define MSGPACK_V2_PARSE_HPP
13 #if MSGPACK_DEFAULT_API_VERSION >= 2
17 #include <boost/assert.hpp>
32 using v1::detail::fix_tag;
33 using v1::detail::value;
36 template <
typename VisitorHolder>
49 holder().visitor().init();
56 static uint32_t next_cs(T p)
58 return static_cast<uint32_t
>(*p) & 0x1f;
61 VisitorHolder& holder() {
62 return static_cast<VisitorHolder&
>(*this);
65 template <
typename T,
typename StartVisitor,
typename EndVisitor>
67 StartVisitor
const& sv,
72 load<T>(
size, load_pos);
76 off =
static_cast<std::size_t
>(m_current - m_start);
80 off =
static_cast<std::size_t
>(m_current - m_start);
85 off =
static_cast<std::size_t
>(m_current - m_start);
91 off =
static_cast<std::size_t
>(m_current - m_start);
94 parse_return ret = m_stack.push(holder(), sv.type(),
static_cast<uint32_t
>(
size));
96 off =
static_cast<std::size_t
>(m_current - m_start);
104 parse_return after_visit_proc(
bool visit_result, std::size_t& off) {
107 off =
static_cast<std::size_t
>(m_current - m_start);
112 off =
static_cast<std::size_t
>(m_current - m_start);
119 array_sv(VisitorHolder& visitor_holder):m_visitor_holder(visitor_holder) {}
120 bool operator()(uint32_t
size)
const {
121 return m_visitor_holder.visitor().start_array(
size);
125 VisitorHolder& m_visitor_holder;
128 array_ev(VisitorHolder& visitor_holder):m_visitor_holder(visitor_holder) {}
129 bool operator()()
const {
130 return m_visitor_holder.visitor().end_array();
133 VisitorHolder& m_visitor_holder;
136 map_sv(VisitorHolder& visitor_holder):m_visitor_holder(visitor_holder) {}
137 bool operator()(uint32_t
size)
const {
138 return m_visitor_holder.visitor().start_map(
size);
142 VisitorHolder& m_visitor_holder;
145 map_ev(VisitorHolder& visitor_holder):m_visitor_holder(visitor_holder) {}
146 bool operator()()
const {
147 return m_visitor_holder.visitor().end_map();
150 VisitorHolder& m_visitor_holder;
153 struct unpack_stack {
163 m_stack.push_back(stack_elem(type, rest));
177 while (!m_stack.empty()) {
178 stack_elem& e = m_stack.back();
182 if (--e.m_rest == 0) {
198 if (--e.m_rest == 0) {
212 bool empty()
const {
return m_stack.empty(); }
213 void clear() { m_stack.clear(); }
215 std::vector<stack_elem> m_stack;
219 char const* m_current;
223 uint32_t m_num_elements;
224 unpack_stack m_stack;
227 template <std::
size_t N>
228 inline void check_ext_size(std::size_t ) {
232 inline void check_ext_size<4>(std::size_t
size) {
236 template <
typename VisitorHolder>
239 BOOST_ASSERT(len >= off);
242 m_current = data + off;
243 const char*
const pe = data + len;
246 if(m_current == pe) {
247 off =
static_cast<std::size_t
>(m_current - m_start);
250 bool fixed_trail_again =
false;
253 fixed_trail_again =
false;
254 int selector = *
reinterpret_cast<const unsigned char*
>(m_current);
255 if (0x00 <= selector && selector <= 0x7f) {
256 uint8_t tmp = *
reinterpret_cast<const uint8_t*
>(m_current);
257 bool visret = holder().visitor().visit_positive_integer(tmp);
260 }
else if(0xe0 <= selector && selector <= 0xff) {
261 int8_t tmp = *
reinterpret_cast<const int8_t*
>(m_current);
262 bool visret = holder().visitor().visit_negative_integer(tmp);
265 }
else if (0xc4 <= selector && selector <= 0xdf) {
266 const uint32_t trail[] = {
296 m_trail = trail[selector - 0xc4];
297 m_cs = next_cs(m_current);
298 fixed_trail_again =
true;
299 }
else if(0xa0 <= selector && selector <= 0xbf) {
300 m_trail =
static_cast<uint32_t
>(*m_current) & 0x1f;
302 bool visret = holder().visitor().visit_str(n,
static_cast<uint32_t
>(m_trail));
308 fixed_trail_again =
true;
310 }
else if(0x90 <= selector && selector <= 0x9f) {
311 parse_return ret = start_aggregate<fix_tag>(array_sv(holder()), array_ev(holder()), m_current, off);
313 }
else if(0x80 <= selector && selector <= 0x8f) {
314 parse_return ret = start_aggregate<fix_tag>(map_sv(holder()), map_ev(holder()), m_current, off);
316 }
else if(selector == 0xc2) {
317 bool visret = holder().visitor().visit_boolean(
false);
320 }
else if(selector == 0xc3) {
321 bool visret = holder().visitor().visit_boolean(
true);
324 }
else if(selector == 0xc0) {
325 bool visret = holder().visitor().visit_nil();
329 off =
static_cast<std::size_t
>(m_current - m_start);
330 holder().visitor().parse_error(off - 1, off);
336 if (fixed_trail_again) {
338 fixed_trail_again =
false;
340 if(
static_cast<std::size_t
>(pe - m_current) < m_trail) {
341 off =
static_cast<std::size_t
>(m_current - m_start);
345 m_current += m_trail - 1;
350 union { uint32_t i;
float f; } mem;
351 load<uint32_t>(mem.i, n);
352 bool visret = holder().visitor().visit_float32(mem.f);
357 union { uint64_t i;
double f; } mem;
358 load<uint64_t>(mem.i, n);
359 #if defined(TARGET_OS_IPHONE)
361 #elif defined(__arm__) && !(__ARM_EABI__)
363 mem.i = (mem.i & 0xFFFFFFFFUL) << 32UL | (mem.i >> 32UL);
365 bool visret = holder().visitor().visit_float64(mem.f);
371 load<uint8_t>(tmp, n);
372 bool visret = holder().visitor().visit_positive_integer(tmp);
378 load<uint16_t>(tmp, n);
379 bool visret = holder().visitor().visit_positive_integer(tmp);
385 load<uint32_t>(tmp, n);
386 bool visret = holder().visitor().visit_positive_integer(tmp);
392 load<uint64_t>(tmp, n);
393 bool visret = holder().visitor().visit_positive_integer(tmp);
399 load<int8_t>(tmp, n);
400 bool visret = holder().visitor().visit_negative_integer(tmp);
406 load<int16_t>(tmp, n);
407 bool visret = holder().visitor().visit_negative_integer(tmp);
413 load<int32_t>(tmp, n);
414 bool visret = holder().visitor().visit_negative_integer(tmp);
420 load<int64_t>(tmp, n);
421 bool visret = holder().visitor().visit_negative_integer(tmp);
426 bool visret = holder().visitor().visit_ext(n, 1+1);
431 bool visret = holder().visitor().visit_ext(n, 2+1);
436 bool visret = holder().visitor().visit_ext(n, 4+1);
441 bool visret = holder().visitor().visit_ext(n, 8+1);
446 bool visret = holder().visitor().visit_ext(n, 16+1);
452 load<uint8_t>(tmp, n);
455 bool visret = holder().visitor().visit_str(n,
static_cast<uint32_t
>(m_trail));
461 fixed_trail_again =
true;
466 load<uint8_t>(tmp, n);
469 bool visret = holder().visitor().visit_bin(n,
static_cast<uint32_t
>(m_trail));
475 fixed_trail_again =
true;
480 load<uint8_t>(tmp, n);
483 bool visret = holder().visitor().visit_ext(n,
static_cast<uint32_t
>(m_trail));
489 fixed_trail_again =
true;
494 load<uint16_t>(tmp, n);
497 bool visret = holder().visitor().visit_str(n,
static_cast<uint32_t
>(m_trail));
503 fixed_trail_again =
true;
508 load<uint16_t>(tmp, n);
511 bool visret = holder().visitor().visit_bin(n,
static_cast<uint32_t
>(m_trail));
517 fixed_trail_again =
true;
522 load<uint16_t>(tmp, n);
525 bool visret = holder().visitor().visit_ext(n,
static_cast<uint32_t
>(m_trail));
531 fixed_trail_again =
true;
536 load<uint32_t>(tmp, n);
539 bool visret = holder().visitor().visit_str(n,
static_cast<uint32_t
>(m_trail));
545 fixed_trail_again =
true;
550 load<uint32_t>(tmp, n);
553 bool visret = holder().visitor().visit_bin(n,
static_cast<uint32_t
>(m_trail));
559 fixed_trail_again =
true;
564 load<uint32_t>(tmp, n);
565 check_ext_size<sizeof(std::size_t)>(tmp);
569 bool visret = holder().visitor().visit_ext(n,
static_cast<uint32_t
>(m_trail));
575 fixed_trail_again =
true;
579 bool visret = holder().visitor().visit_str(n,
static_cast<uint32_t
>(m_trail));
584 bool visret = holder().visitor().visit_bin(n,
static_cast<uint32_t
>(m_trail));
589 bool visret = holder().visitor().visit_ext(n,
static_cast<uint32_t
>(m_trail));
594 parse_return ret = start_aggregate<uint16_t>(array_sv(holder()), array_ev(holder()), n, off);
599 parse_return ret = start_aggregate<uint32_t>(array_sv(holder()), array_ev(holder()), n, off);
603 parse_return ret = start_aggregate<uint16_t>(map_sv(holder()), map_ev(holder()), n, off);
607 parse_return ret = start_aggregate<uint32_t>(map_sv(holder()), map_ev(holder()), n, off);
611 off =
static_cast<std::size_t
>(m_current - m_start);
612 holder().visitor().parse_error(
static_cast<std::size_t
>(n - m_start - 1),
static_cast<std::size_t
>(n - m_start));
616 }
while(m_current != pe);
618 off =
static_cast<std::size_t
>(m_current - m_start);
627 template <
typename VisitorHolder,
typename ReferencedBufferHook>
628 class parser :
public detail::context<VisitorHolder> {
629 typedef parser<VisitorHolder, ReferencedBufferHook> this_type;
630 typedef detail::context<VisitorHolder> context_type;
641 parser(ReferencedBufferHook& hook,
644 #if !defined(MSGPACK_USE_CPP03)
645 parser(this_type&& other);
646 this_type& operator=(this_type&& other);
677 std::size_t buffer_capacity()
const;
689 void buffer_consumed(std::size_t
size);
708 std::size_t message_size()
const;
718 std::size_t parsed_size()
const;
727 char* nonparsed_buffer();
736 std::size_t nonparsed_size()
const;
746 void skip_nonparsed_buffer(std::size_t
size);
753 void remove_nonparsed_buffer();
758 char* get_raw_buffer() {
762 void expand_buffer(std::size_t
size);
770 std::size_t m_parsed;
771 std::size_t m_initial_buffer_size;
772 ReferencedBufferHook& m_referenced_buffer_hook;
774 #if defined(MSGPACK_USE_CPP03)
776 parser(
const this_type&);
777 this_type& operator=(
const this_type&);
780 parser(
const this_type&) =
delete;
781 this_type& operator=(
const this_type&) =
delete;
785 template <
typename VisitorHolder,
typename ReferencedBufferHook>
786 inline parser<VisitorHolder, ReferencedBufferHook>::parser(
787 ReferencedBufferHook& hook,
788 std::size_t initial_buffer_size)
789 :m_referenced_buffer_hook(hook)
795 char* buffer =
static_cast<char*
>(::malloc(initial_buffer_size));
797 throw std::bad_alloc();
802 m_free = initial_buffer_size - m_used;
805 m_initial_buffer_size = initial_buffer_size;
810 #if !defined(MSGPACK_USE_CPP03)
813 template <
typename VisitorHolder,
typename ReferencedBufferHook>
814 inline parser<VisitorHolder, ReferencedBufferHook>::parser(this_type&& other)
815 :context_type(std::move(other)),
816 m_buffer(other.m_buffer),
817 m_used(other.m_used),
818 m_free(other.m_free),
820 m_parsed(other.m_parsed),
821 m_initial_buffer_size(other.m_initial_buffer_size),
822 m_referenced_buffer_hook(other.m_referenced_buffer_hook) {
830 template <
typename VisitorHolder,
typename ReferencedBufferHook>
831 inline parser<VisitorHolder, ReferencedBufferHook>& parser<VisitorHolder, ReferencedBufferHook>::operator=(this_type&& other) {
833 new (
this) this_type(std::move(other));
840 template <
typename VisitorHolder,
typename ReferencedBufferHook>
841 inline parser<VisitorHolder, ReferencedBufferHook>::~parser()
848 template <
typename VisitorHolder,
typename ReferencedBufferHook>
849 inline void parser<VisitorHolder, ReferencedBufferHook>::reserve_buffer(std::size_t
size)
851 if(m_free >=
size)
return;
855 template <
typename VisitorHolder,
typename ReferencedBufferHook>
856 inline void parser<VisitorHolder, ReferencedBufferHook>::expand_buffer(std::size_t
size)
859 && !
static_cast<VisitorHolder&
>(*this).visitor().referenced()) {
865 if(m_free >=
size)
return;
869 std::size_t next_size = (m_used + m_free) * 2;
870 while(next_size <
size + m_used) {
871 std::size_t tmp_next_size = next_size * 2;
872 if (tmp_next_size <= next_size) {
873 next_size =
size + m_used;
876 next_size = tmp_next_size;
879 char* tmp =
static_cast<char*
>(::realloc(m_buffer, next_size));
881 throw std::bad_alloc();
885 m_free = next_size - m_used;
888 std::size_t next_size = m_initial_buffer_size;
889 std::size_t not_parsed = m_used - m_off;
891 std::size_t tmp_next_size = next_size * 2;
892 if (tmp_next_size <= next_size) {
896 next_size = tmp_next_size;
899 char* tmp =
static_cast<char*
>(::malloc(next_size));
901 throw std::bad_alloc();
906 std::memcpy(tmp+
COUNTER_SIZE, m_buffer + m_off, not_parsed);
908 if(
static_cast<VisitorHolder&
>(*this).referenced()) {
910 m_referenced_buffer_hook(m_buffer);
916 static_cast<VisitorHolder&
>(*this).set_referenced(
false);
923 m_free = next_size - m_used;
928 template <
typename VisitorHolder,
typename ReferencedBufferHook>
929 inline char* parser<VisitorHolder, ReferencedBufferHook>::buffer()
931 return m_buffer + m_used;
934 template <
typename VisitorHolder,
typename ReferencedBufferHook>
935 inline std::size_t parser<VisitorHolder, ReferencedBufferHook>::buffer_capacity()
const
940 template <
typename VisitorHolder,
typename ReferencedBufferHook>
941 inline void parser<VisitorHolder, ReferencedBufferHook>::buffer_consumed(std::size_t
size)
947 template <
typename VisitorHolder,
typename ReferencedBufferHook>
948 inline bool parser<VisitorHolder, ReferencedBufferHook>::next()
954 template <
typename VisitorHolder,
typename ReferencedBufferHook>
955 inline parse_return parser<VisitorHolder, ReferencedBufferHook>::execute_imp()
957 std::size_t off = m_off;
958 parse_return ret = context_type::execute(m_buffer, m_used, m_off);
960 m_parsed += m_off - off;
965 template <
typename VisitorHolder,
typename ReferencedBufferHook>
966 inline void parser<VisitorHolder, ReferencedBufferHook>::reset()
968 context_type::init();
973 template <
typename VisitorHolder,
typename ReferencedBufferHook>
974 inline std::size_t parser<VisitorHolder, ReferencedBufferHook>::message_size()
const
976 return m_parsed - m_off + m_used;
979 template <
typename VisitorHolder,
typename ReferencedBufferHook>
980 inline std::size_t parser<VisitorHolder, ReferencedBufferHook>::parsed_size()
const
985 template <
typename VisitorHolder,
typename ReferencedBufferHook>
986 inline char* parser<VisitorHolder, ReferencedBufferHook>::nonparsed_buffer()
988 return m_buffer + m_off;
991 template <
typename VisitorHolder,
typename ReferencedBufferHook>
992 inline std::size_t parser<VisitorHolder, ReferencedBufferHook>::nonparsed_size()
const
994 return m_used - m_off;
997 template <
typename VisitorHolder,
typename ReferencedBufferHook>
998 inline void parser<VisitorHolder, ReferencedBufferHook>::skip_nonparsed_buffer(std::size_t
size)
1003 template <
typename VisitorHolder,
typename ReferencedBufferHook>
1004 inline void parser<VisitorHolder, ReferencedBufferHook>::remove_nonparsed_buffer()
1009 template <
typename Visitor>
1010 inline bool parse(
const char* data,
size_t len,
size_t& off, Visitor& v) {
1015 template <
typename Visitor>
1016 inline bool parse(
const char* data,
size_t len, Visitor& v) {
1017 std::size_t off = 0;
1023 template <
typename Visitor>
1024 struct parse_helper : detail::context<parse_helper<Visitor> > {
1025 parse_helper(Visitor& v):m_visitor(v) {}
1026 parse_return execute(
const char* data, std::size_t len, std::size_t& off) {
1027 return detail::context<parse_helper<Visitor> >::execute(data, len, off);
1029 Visitor& visitor()
const {
return m_visitor; }
1033 template <
typename Visitor>
1035 parse_imp(
const char* data,
size_t len,
size_t& off, Visitor& v) {
1036 std::size_t noff = off;
1040 v.insufficient_bytes(noff, noff);
1043 detail::parse_helper<Visitor> h(v);
1048 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)
void init_count(void *buffer)
Definition: unpack.hpp:241
std::atomic< unsigned int > const & get_count(void *buffer)
Definition: unpack.hpp:278
void decr_count(void *buffer)
Definition: unpack.hpp:250
msgpack::enable_if< sizeof(T)==sizeof(fix_tag)>::type load(uint32_t &dst, const char *n)
Definition: unpack.hpp:294
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
bool parse(const char *data, size_t len, size_t &off, Visitor &v)
Unpack msgpack formatted data via a visitor.
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_UNPACKER_INIT_BUFFER_SIZE
Definition: unpack_decl.hpp:43
#define MSGPACK_UNPACKER_RESERVE_SIZE
Definition: unpack_decl.hpp:47
const size_t COUNTER_SIZE
Definition: unpack_decl.hpp:40
#define MSGPACK_API_VERSION_NAMESPACE(ns)
Definition: versioning.hpp:66