10 #ifndef MSGPACK_V1_UNPACK_HPP
11 #define MSGPACK_V1_UNPACK_HPP
18 #include "msgpack/unpack_define.h"
20 #include "msgpack/sysdep.h"
24 #if !defined(MSGPACK_USE_CPP03)
34 #endif // defined(_MSC_VER)
117 #if SIZE_MAX == UINT_MAX
120 #endif // SIZE_MAX == UINT_MAX
129 #if defined(__GNUC__) && !defined(__clang__)
143 #if SIZE_MAX == UINT_MAX
146 #endif // SIZE_MAX == UINT_MAX
155 #if defined(__GNUC__) && !defined(__clang__)
175 std::memcpy(tmp, p, l);
191 std::memcpy(tmp, p, l);
207 std::memcpy(tmp, p, l);
210 o.
via.
ext.
size =
static_cast<uint32_t
>(l - 1);
219 std::size_t
count()
const {
return m_count; }
229 uint32_t m_container_type;
235 #if defined(MSGPACK_USE_CPP03)
236 *
reinterpret_cast<volatile _msgpack_atomic_counter_t*
>(buffer) = 1;
237 #else // defined(MSGPACK_USE_CPP03)
238 new (buffer) std::atomic<unsigned int>(1);
239 #endif // defined(MSGPACK_USE_CPP03)
244 #if defined(MSGPACK_USE_CPP03)
245 if(_msgpack_sync_decr_and_fetch(
reinterpret_cast<volatile _msgpack_atomic_counter_t*
>(buffer)) == 0) {
248 #else // defined(MSGPACK_USE_CPP03)
249 if (--*
reinterpret_cast<std::atomic<unsigned int>*
>(buffer) == 0) {
252 #endif // defined(MSGPACK_USE_CPP03)
257 #if defined(MSGPACK_USE_CPP03)
258 _msgpack_sync_incr_and_fetch(
reinterpret_cast<volatile _msgpack_atomic_counter_t*
>(buffer));
259 #else // defined(MSGPACK_USE_CPP03)
260 ++*
reinterpret_cast<std::atomic<unsigned int>*
>(buffer);
261 #endif // defined(MSGPACK_USE_CPP03)
264 #if defined(MSGPACK_USE_CPP03)
265 inline _msgpack_atomic_counter_t
get_count(
void* buffer)
267 return *
reinterpret_cast<volatile _msgpack_atomic_counter_t*
>(buffer);
269 #else // defined(MSGPACK_USE_CPP03)
270 inline std::atomic<unsigned int>
const&
get_count(
void* buffer)
272 return *
reinterpret_cast<std::atomic<unsigned int>*
>(buffer);
274 #endif // defined(MSGPACK_USE_CPP03)
276 template <
typename T>
285 template <
typename T>
287 dst =
static_cast<uint32_t
>(*
reinterpret_cast<const uint8_t*
>(n)) & 0x0f;
290 template <
typename T>
292 dst =
static_cast<T
>(*
reinterpret_cast<const uint8_t*
>(n));
295 template <
typename T>
297 _msgpack_load16(T, n, &dst);
300 template <
typename T>
302 _msgpack_load32(T, n, &dst);
305 template <
typename T>
307 _msgpack_load64(T, n, &dst);
313 :m_trail(0), m_user(f, user_data, limit), m_cs(MSGPACK_CS_HEADER)
315 m_stack.reserve(MSGPACK_EMBED_STACK_SIZE);
321 m_cs = MSGPACK_CS_HEADER;
329 return m_stack[0].obj();
342 int execute(
const char*
data, std::size_t len, std::size_t& off);
345 template <
typename T>
346 static uint32_t next_cs(T p)
348 return static_cast<uint32_t
>(*p) & 0x1f;
351 template <
typename T,
typename Func>
354 uint32_t container_type,
356 const char* load_pos,
359 load<T>(tmp, load_pos);
360 f(m_user, tmp, m_stack.back().obj());
362 obj = m_stack.back().obj();
363 int ret = push_proc(obj, off);
364 if (ret != 0)
return ret;
367 m_stack.back().set_container_type(container_type);
368 m_stack.back().set_count(tmp);
369 if (m_stack.size() <= m_user.
limit().
depth()) {
370 m_stack.push_back(unpack_stack());
375 m_cs = MSGPACK_CS_HEADER;
384 if(m_stack.size() == 1) {
387 unpack_stack& sp = *(m_stack.end() - 2);
388 switch(sp.container_type()) {
389 case MSGPACK_CT_ARRAY_ITEM:
391 if(sp.decr_count() == 0) {
399 case MSGPACK_CT_MAP_KEY:
401 sp.set_container_type(MSGPACK_CT_MAP_VALUE);
404 case MSGPACK_CT_MAP_VALUE:
406 if(sp.decr_count() == 0) {
411 sp.set_container_type(MSGPACK_CT_MAP_KEY);
423 int ret = push_item(obj);
425 m_stack[0].set_obj(obj);
428 off =
static_cast<std::size_t
>(m_current - m_start);
431 off =
static_cast<std::size_t
>(m_current - m_start);
434 m_cs = MSGPACK_CS_HEADER;
440 template <std::
size_t N>
441 static void check_ext_size(std::size_t ) {
446 char const* m_current;
451 std::vector<unpack_stack> m_stack;
455 inline void context::check_ext_size<4>(std::size_t
size) {
464 m_current =
data + off;
465 const char*
const pe =
data + len;
470 if(m_current == pe) {
471 off =
static_cast<std::size_t
>(m_current - m_start);
474 bool fixed_trail_again =
false;
476 if (m_cs == MSGPACK_CS_HEADER) {
477 fixed_trail_again =
false;
478 int selector = *
reinterpret_cast<const unsigned char*
>(m_current);
479 if (0x00 <= selector && selector <= 0x7f) {
480 unpack_uint8(*
reinterpret_cast<const uint8_t*
>(m_current), obj);
481 int ret = push_proc(obj, off);
482 if (ret != 0)
return ret;
483 }
else if(0xe0 <= selector && selector <= 0xff) {
484 unpack_int8(*
reinterpret_cast<const int8_t*
>(m_current), obj);
485 int ret = push_proc(obj, off);
486 if (ret != 0)
return ret;
487 }
else if (0xc4 <= selector && selector <= 0xdf) {
488 const uint32_t trail[] = {
518 m_trail = trail[selector - 0xc4];
519 m_cs = next_cs(m_current);
520 fixed_trail_again =
true;
521 }
else if(0xa0 <= selector && selector <= 0xbf) {
522 m_trail =
static_cast<uint32_t
>(*m_current) & 0x1f;
524 unpack_str(m_user, n,
static_cast<uint32_t
>(m_trail), obj);
525 int ret = push_proc(obj, off);
526 if (ret != 0)
return ret;
529 m_cs = MSGPACK_ACS_STR_VALUE;
530 fixed_trail_again =
true;
533 }
else if(0x90 <= selector && selector <= 0x9f) {
534 int ret = push_aggregate<fix_tag>(
535 unpack_array(), MSGPACK_CT_ARRAY_ITEM, obj, m_current, off);
536 if (ret != 0)
return ret;
537 }
else if(0x80 <= selector && selector <= 0x8f) {
538 int ret = push_aggregate<fix_tag>(
539 unpack_map(), MSGPACK_CT_MAP_KEY, obj, m_current, off);
540 if (ret != 0)
return ret;
541 }
else if(selector == 0xc2) {
543 int ret = push_proc(obj, off);
544 if (ret != 0)
return ret;
545 }
else if(selector == 0xc3) {
547 int ret = push_proc(obj, off);
548 if (ret != 0)
return ret;
549 }
else if(selector == 0xc0) {
551 int ret = push_proc(obj, off);
552 if (ret != 0)
return ret;
554 off =
static_cast<std::size_t
>(m_current - m_start);
559 if (m_cs != MSGPACK_CS_HEADER || fixed_trail_again) {
560 if (fixed_trail_again) {
562 fixed_trail_again =
false;
564 if(
static_cast<std::size_t
>(pe - m_current) < m_trail) {
565 off =
static_cast<std::size_t
>(m_current - m_start);
569 m_current += m_trail - 1;
573 case MSGPACK_CS_FLOAT: {
574 union { uint32_t i;
float f; } mem;
575 load<uint32_t>(mem.i, n);
577 int ret = push_proc(obj, off);
578 if (ret != 0)
return ret;
580 case MSGPACK_CS_DOUBLE: {
581 union { uint64_t i;
double f; } mem;
582 load<uint64_t>(mem.i, n);
583 #if defined(TARGET_OS_IPHONE)
585 #elif defined(__arm__) && !(__ARM_EABI__) // arm-oabi
587 mem.i = (mem.i & 0xFFFFFFFFUL) << 32UL | (mem.i >> 32UL);
590 int ret = push_proc(obj, off);
591 if (ret != 0)
return ret;
593 case MSGPACK_CS_UINT_8: {
595 load<uint8_t>(tmp, n);
597 int ret = push_proc(obj, off);
598 if (ret != 0)
return ret;
600 case MSGPACK_CS_UINT_16: {
602 load<uint16_t>(tmp, n);
604 int ret = push_proc(obj, off);
605 if (ret != 0)
return ret;
607 case MSGPACK_CS_UINT_32: {
609 load<uint32_t>(tmp, n);
611 int ret = push_proc(obj, off);
612 if (ret != 0)
return ret;
614 case MSGPACK_CS_UINT_64: {
616 load<uint64_t>(tmp, n);
618 int ret = push_proc(obj, off);
619 if (ret != 0)
return ret;
621 case MSGPACK_CS_INT_8: {
623 load<int8_t>(tmp, n);
625 int ret = push_proc(obj, off);
626 if (ret != 0)
return ret;
628 case MSGPACK_CS_INT_16: {
630 load<int16_t>(tmp, n);
632 int ret = push_proc(obj, off);
633 if (ret != 0)
return ret;
635 case MSGPACK_CS_INT_32: {
637 load<int32_t>(tmp, n);
639 int ret = push_proc(obj, off);
640 if (ret != 0)
return ret;
642 case MSGPACK_CS_INT_64: {
644 load<int64_t>(tmp, n);
646 int ret = push_proc(obj, off);
647 if (ret != 0)
return ret;
649 case MSGPACK_CS_FIXEXT_1: {
651 int ret = push_proc(obj, off);
652 if (ret != 0)
return ret;
654 case MSGPACK_CS_FIXEXT_2: {
656 int ret = push_proc(obj, off);
657 if (ret != 0)
return ret;
659 case MSGPACK_CS_FIXEXT_4: {
661 int ret = push_proc(obj, off);
662 if (ret != 0)
return ret;
664 case MSGPACK_CS_FIXEXT_8: {
666 int ret = push_proc(obj, off);
667 if (ret != 0)
return ret;
669 case MSGPACK_CS_FIXEXT_16: {
671 int ret = push_proc(obj, off);
672 if (ret != 0)
return ret;
674 case MSGPACK_CS_STR_8: {
676 load<uint8_t>(tmp, n);
679 unpack_str(m_user, n,
static_cast<uint32_t
>(m_trail), obj);
680 int ret = push_proc(obj, off);
681 if (ret != 0)
return ret;
684 m_cs = MSGPACK_ACS_STR_VALUE;
685 fixed_trail_again =
true;
688 case MSGPACK_CS_BIN_8: {
690 load<uint8_t>(tmp, n);
693 unpack_bin(m_user, n,
static_cast<uint32_t
>(m_trail), obj);
694 int ret = push_proc(obj, off);
695 if (ret != 0)
return ret;
698 m_cs = MSGPACK_ACS_BIN_VALUE;
699 fixed_trail_again =
true;
702 case MSGPACK_CS_EXT_8: {
704 load<uint8_t>(tmp, n);
708 int ret = push_proc(obj, off);
709 if (ret != 0)
return ret;
712 m_cs = MSGPACK_ACS_EXT_VALUE;
713 fixed_trail_again =
true;
716 case MSGPACK_CS_STR_16: {
718 load<uint16_t>(tmp, n);
721 unpack_str(m_user, n,
static_cast<uint32_t
>(m_trail), obj);
722 int ret = push_proc(obj, off);
723 if (ret != 0)
return ret;
726 m_cs = MSGPACK_ACS_STR_VALUE;
727 fixed_trail_again =
true;
730 case MSGPACK_CS_BIN_16: {
732 load<uint16_t>(tmp, n);
735 unpack_bin(m_user, n,
static_cast<uint32_t
>(m_trail), obj);
736 int ret = push_proc(obj, off);
737 if (ret != 0)
return ret;
740 m_cs = MSGPACK_ACS_BIN_VALUE;
741 fixed_trail_again =
true;
744 case MSGPACK_CS_EXT_16: {
746 load<uint16_t>(tmp, n);
750 int ret = push_proc(obj, off);
751 if (ret != 0)
return ret;
754 m_cs = MSGPACK_ACS_EXT_VALUE;
755 fixed_trail_again =
true;
758 case MSGPACK_CS_STR_32: {
760 load<uint32_t>(tmp, n);
763 unpack_str(m_user, n,
static_cast<uint32_t
>(m_trail), obj);
764 int ret = push_proc(obj, off);
765 if (ret != 0)
return ret;
768 m_cs = MSGPACK_ACS_STR_VALUE;
769 fixed_trail_again =
true;
772 case MSGPACK_CS_BIN_32: {
774 load<uint32_t>(tmp, n);
777 unpack_bin(m_user, n,
static_cast<uint32_t
>(m_trail), obj);
778 int ret = push_proc(obj, off);
779 if (ret != 0)
return ret;
782 m_cs = MSGPACK_ACS_BIN_VALUE;
783 fixed_trail_again =
true;
786 case MSGPACK_CS_EXT_32: {
788 load<uint32_t>(tmp, n);
789 check_ext_size<sizeof(std::size_t)>(tmp);
794 int ret = push_proc(obj, off);
795 if (ret != 0)
return ret;
798 m_cs = MSGPACK_ACS_EXT_VALUE;
799 fixed_trail_again =
true;
802 case MSGPACK_ACS_STR_VALUE: {
803 unpack_str(m_user, n,
static_cast<uint32_t
>(m_trail), obj);
804 int ret = push_proc(obj, off);
805 if (ret != 0)
return ret;
807 case MSGPACK_ACS_BIN_VALUE: {
808 unpack_bin(m_user, n,
static_cast<uint32_t
>(m_trail), obj);
809 int ret = push_proc(obj, off);
810 if (ret != 0)
return ret;
812 case MSGPACK_ACS_EXT_VALUE: {
814 int ret = push_proc(obj, off);
815 if (ret != 0)
return ret;
817 case MSGPACK_CS_ARRAY_16: {
818 int ret = push_aggregate<uint16_t>(
820 if (ret != 0)
return ret;
822 case MSGPACK_CS_ARRAY_32: {
824 int ret = push_aggregate<uint32_t>(
826 if (ret != 0)
return ret;
828 case MSGPACK_CS_MAP_16: {
829 int ret = push_aggregate<uint16_t>(
830 unpack_map(), MSGPACK_CT_MAP_KEY, obj, n, off);
831 if (ret != 0)
return ret;
833 case MSGPACK_CS_MAP_32: {
835 int ret = push_aggregate<uint32_t>(
836 unpack_map(), MSGPACK_CT_MAP_KEY, obj, n, off);
837 if (ret != 0)
return ret;
840 off =
static_cast<std::size_t
>(m_current - m_start);
844 }
while(m_current != pe);
846 off =
static_cast<std::size_t
>(m_current - m_start);
869 #if !defined(MSGPACK_USE_CPP03)
872 #endif // !defined(MSGPACK_USE_CPP03)
1030 void expand_buffer(std::size_t
size);
1040 std::size_t m_parsed;
1042 std::size_t m_initial_buffer_size;
1045 #if defined(MSGPACK_USE_CPP03)
1049 #else // defined(MSGPACK_USE_CPP03)
1052 #endif // defined(MSGPACK_USE_CPP03)
1057 std::size_t initial_buffer_size,
1059 :m_z(new
msgpack::
zone), m_ctx(f, user_data, limit)
1065 char*
buffer =
static_cast<char*
>(::malloc(initial_buffer_size));
1067 throw std::bad_alloc();
1072 m_free = initial_buffer_size - m_used;
1075 m_initial_buffer_size = initial_buffer_size;
1084 #if !defined(MSGPACK_USE_CPP03)
1088 :m_buffer(other.m_buffer),
1089 m_used(other.m_used),
1090 m_free(other.m_free),
1092 m_parsed(other.m_parsed),
1093 m_z(std::
move(other.m_z)),
1094 m_initial_buffer_size(other.m_initial_buffer_size),
1095 m_ctx(other.m_ctx) {
1105 #endif // !defined(MSGPACK_USE_CPP03)
1117 if(m_free >=
size)
return;
1118 expand_buffer(
size);
1121 inline void unpacker::expand_buffer(std::size_t
size)
1130 if(m_free >=
size)
return;
1134 std::size_t next_size = (m_used + m_free) * 2;
1135 while(next_size <
size + m_used) {
1136 std::size_t tmp_next_size = next_size * 2;
1137 if (tmp_next_size <= next_size) {
1138 next_size =
size + m_used;
1141 next_size = tmp_next_size;
1144 char* tmp =
static_cast<char*
>(::realloc(m_buffer, next_size));
1146 throw std::bad_alloc();
1150 m_free = next_size - m_used;
1153 std::size_t next_size = m_initial_buffer_size;
1154 std::size_t not_parsed = m_used - m_off;
1156 std::size_t tmp_next_size = next_size * 2;
1157 if (tmp_next_size <= next_size) {
1161 next_size = tmp_next_size;
1164 char* tmp =
static_cast<char*
>(::malloc(next_size));
1166 throw std::bad_alloc();
1171 std::memcpy(tmp+
COUNTER_SIZE, m_buffer + m_off, not_parsed);
1188 m_free = next_size - m_used;
1195 return m_buffer + m_used;
1212 int ret = execute_imp();
1218 result.
zone().reset();
1234 return next(result, referenced);
1239 return next(*result);
1245 int ret = execute_imp();
1248 }
else if(ret == 0) {
1255 inline int unpacker::execute_imp()
1257 std::size_t off = m_off;
1258 int ret = m_ctx.
execute(m_buffer, m_used, m_off);
1260 m_parsed += m_off - off;
1267 return m_ctx.
data();
1289 inline bool unpacker::flush_zone()
1314 return m_parsed - m_off + m_used;
1324 return m_buffer + m_off;
1329 return m_used - m_off;
1350 std::size_t noff = off;
1364 int e = ctx.
execute(data, len, noff);
1376 result = ctx.
data();
1390 const char* data, std::size_t len, std::size_t& off,
bool& referenced,
1398 std::size_t noff = off;
1400 data, len, noff, *z, obj, referenced, f, user_data, limit);
1419 const char* data, std::size_t len, std::size_t& off,
1424 return unpack(data, len, off, referenced, f, user_data, limit);
1428 const char* data, std::size_t len,
bool& referenced,
1432 std::size_t off = 0;
1433 return unpack(data, len, off, referenced, f, user_data, limit);
1437 const char* data, std::size_t len,
1442 std::size_t off = 0;
1443 return unpack(data, len, off, referenced, f, user_data, limit);
1448 const char* data, std::size_t len, std::size_t& off,
bool& referenced,
1455 std::size_t noff = off;
1457 data, len, noff, *z, obj, referenced, f, user_data, limit);
1480 const char* data, std::size_t len, std::size_t& off,
1485 unpack(result, data, len, off, referenced, f, user_data, limit);
1490 const char* data, std::size_t len,
bool& referenced,
1494 std::size_t off = 0;
1495 unpack(result, data, len, off, referenced, f, user_data, limit);
1500 const char* data, std::size_t len,
1505 std::size_t off = 0;
1506 unpack(result, data, len, off, referenced, f, user_data, limit);
1512 const char* data, std::size_t len, std::size_t& off,
bool& referenced,
1517 std::size_t noff = off;
1520 data, len, noff, z, obj, referenced, f, user_data, limit);
1540 const char* data, std::size_t len, std::size_t& off,
1545 return unpack(z, data, len, off, referenced, f, user_data, limit);
1550 const char* data, std::size_t len,
bool& referenced,
1554 std::size_t off = 0;
1555 return unpack(z, data, len, off, referenced, f, user_data, limit);
1560 const char* data, std::size_t len,
1565 std::size_t off = 0;
1566 return unpack(z, data, len, off, referenced, f, user_data, limit);
1574 const
char* data, std::
size_t len, std::
size_t* off,
bool* referenced,
1579 if (referenced)
unpack(*result, data, len, *off, *referenced, f, user_data, limit);
1580 else unpack(*result, data, len, *off, f, user_data, limit);
1582 if (referenced)
unpack(*result, data, len, *referenced, f, user_data, limit);
1583 else unpack(*result, data, len, f, user_data, limit);
1598 #endif // MSGPACK_V1_UNPACK_HPP