MessagePack for C++
unpack.hpp
Go to the documentation of this file.
1 //
2 // MessagePack for C++ deserializing routine
3 //
4 // Copyright (C) 2008-2016 FURUHASHI Sadayuki and KONDO Takatoshi
5 //
6 // Distributed under the Boost Software License, Version 1.0.
7 // (See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9 //
10 #ifndef MSGPACK_V1_UNPACK_HPP
11 #define MSGPACK_V1_UNPACK_HPP
12 
13 #include "msgpack/versioning.hpp"
14 #include "msgpack/unpack_decl.hpp"
15 #include "msgpack/object.hpp"
16 #include "msgpack/zone.hpp"
18 #include "msgpack/unpack_define.h"
19 #include "msgpack/cpp_config.hpp"
20 #include "msgpack/sysdep.h"
21 
22 #include <memory>
23 
24 #if !defined(MSGPACK_USE_CPP03)
25 #include <atomic>
26 #endif
27 
28 
29 #if defined(_MSC_VER)
30 // avoiding confliction std::max, std::min, and macro in windows.h
31 #ifndef NOMINMAX
32 #define NOMINMAX
33 #endif
34 #endif // defined(_MSC_VER)
35 
36 namespace msgpack {
37 
41 
42 namespace detail {
43 
44 class unpack_user {
45 public:
47  void* user_data = MSGPACK_NULLPTR,
48  unpack_limit const& limit = unpack_limit())
49  :m_func(f), m_user_data(user_data), m_limit(limit) {}
50  msgpack::zone const& zone() const { return *m_zone; }
51  msgpack::zone& zone() { return *m_zone; }
52  void set_zone(msgpack::zone& zone) { m_zone = &zone; }
53  bool referenced() const { return m_referenced; }
54  void set_referenced(bool referenced) { m_referenced = referenced; }
55  unpack_reference_func reference_func() const { return m_func; }
56  void* user_data() const { return m_user_data; }
57  unpack_limit const& limit() const { return m_limit; }
58  unpack_limit& limit() { return m_limit; }
59 
60 private:
61  msgpack::zone* m_zone;
62  bool m_referenced;
63  unpack_reference_func m_func;
64  void* m_user_data;
65  unpack_limit m_limit;
66 };
67 
68 inline void unpack_uint8(uint8_t d, msgpack::object& o)
70 
71 inline void unpack_uint16(uint16_t d, msgpack::object& o)
73 
74 inline void unpack_uint32(uint32_t d, msgpack::object& o)
76 
77 inline void unpack_uint64(uint64_t d, msgpack::object& o)
79 
80 inline void unpack_int8(int8_t d, msgpack::object& o)
81 { if(d >= 0) { o.type = msgpack::type::POSITIVE_INTEGER; o.via.u64 = d; }
82  else { o.type = msgpack::type::NEGATIVE_INTEGER; o.via.i64 = d; } }
83 
84 inline void unpack_int16(int16_t d, msgpack::object& o)
85 { if(d >= 0) { o.type = msgpack::type::POSITIVE_INTEGER; o.via.u64 = d; }
86  else { o.type = msgpack::type::NEGATIVE_INTEGER; o.via.i64 = d; } }
87 
88 inline void unpack_int32(int32_t d, msgpack::object& o)
89 { if(d >= 0) { o.type = msgpack::type::POSITIVE_INTEGER; o.via.u64 = d; }
90  else { o.type = msgpack::type::NEGATIVE_INTEGER; o.via.i64 = d; } }
91 
92 inline void unpack_int64(int64_t d, msgpack::object& o)
93 { if(d >= 0) { o.type = msgpack::type::POSITIVE_INTEGER; o.via.u64 = d; }
94  else { o.type = msgpack::type::NEGATIVE_INTEGER; o.via.i64 = d; } }
95 
96 inline void unpack_float(float d, msgpack::object& o)
97 { o.type = msgpack::type::FLOAT32; o.via.f64 = d; }
98 
99 inline void unpack_double(double d, msgpack::object& o)
100 { o.type = msgpack::type::FLOAT64; o.via.f64 = d; }
101 
103 { o.type = msgpack::type::NIL; }
104 
106 { o.type = msgpack::type::BOOLEAN; o.via.boolean = true; }
107 
109 { o.type = msgpack::type::BOOLEAN; o.via.boolean = false; }
110 
111 struct unpack_array {
112  void operator()(unpack_user& u, uint32_t n, msgpack::object& o) const {
113  if (n > u.limit().array()) throw msgpack::array_size_overflow("array size overflow");
115  o.via.array.size = 0;
116  size_t size = n*sizeof(msgpack::object);
117  if (size / sizeof(msgpack::object) != n) {
118  throw msgpack::array_size_overflow("array size overflow");
119  }
120  o.via.array.ptr = static_cast<msgpack::object*>(u.zone().allocate_align(size));
121  }
122 };
123 
125 {
126 #if defined(__GNUC__) && !defined(__clang__)
127  std::memcpy(&c.via.array.ptr[c.via.array.size++], &o, sizeof(msgpack::object));
128 #else /* __GNUC__ && !__clang__ */
129  c.via.array.ptr[c.via.array.size++] = o;
130 #endif /* __GNUC__ && !__clang__ */
131 }
132 
133 struct unpack_map {
134  void operator()(unpack_user& u, uint32_t n, msgpack::object& o) const {
135  if (n > u.limit().map()) throw msgpack::map_size_overflow("map size overflow");
137  o.via.map.size = 0;
138  size_t size = n*sizeof(msgpack::object_kv);
139  if (size / sizeof(msgpack::object_kv) != n) {
140  throw msgpack::map_size_overflow("map size overflow");
141  }
142  o.via.map.ptr = static_cast<msgpack::object_kv*>(u.zone().allocate_align(size));
143  }
144 };
145 
147 {
148 #if defined(__GNUC__) && !defined(__clang__)
149  std::memcpy(&c.via.map.ptr[c.via.map.size].key, &k, sizeof(msgpack::object));
150  std::memcpy(&c.via.map.ptr[c.via.map.size].val, &v, sizeof(msgpack::object));
151 #else /* __GNUC__ && !__clang__ */
152  c.via.map.ptr[c.via.map.size].key = k;
153  c.via.map.ptr[c.via.map.size].val = v;
154 #endif /* __GNUC__ && !__clang__ */
155  ++c.via.map.size;
156 }
157 
158 inline void unpack_str(unpack_user& u, const char* p, uint32_t l, msgpack::object& o)
159 {
161  if (u.reference_func() && u.reference_func()(o.type, l, u.user_data())) {
162  o.via.str.ptr = p;
163  u.set_referenced(true);
164  }
165  else {
166  if (l > u.limit().str()) throw msgpack::str_size_overflow("str size overflow");
167  char* tmp = static_cast<char*>(u.zone().allocate_align(l));
168  std::memcpy(tmp, p, l);
169  o.via.str.ptr = tmp;
170  }
171  o.via.str.size = l;
172 }
173 
174 inline void unpack_bin(unpack_user& u, const char* p, uint32_t l, msgpack::object& o)
175 {
177  if (u.reference_func() && u.reference_func()(o.type, l, u.user_data())) {
178  o.via.bin.ptr = p;
179  u.set_referenced(true);
180  }
181  else {
182  if (l > u.limit().bin()) throw msgpack::bin_size_overflow("bin size overflow");
183  char* tmp = static_cast<char*>(u.zone().allocate_align(l));
184  std::memcpy(tmp, p, l);
185  o.via.bin.ptr = tmp;
186  }
187  o.via.bin.size = l;
188 }
189 
190 inline void unpack_ext(unpack_user& u, const char* p, std::size_t l, msgpack::object& o)
191 {
193  if (u.reference_func() && u.reference_func()(o.type, l, u.user_data())) {
194  o.via.ext.ptr = p;
195  u.set_referenced(true);
196  }
197  else {
198  if (l > u.limit().ext()) throw msgpack::ext_size_overflow("ext size overflow");
199  char* tmp = static_cast<char*>(u.zone().allocate_align(l));
200  std::memcpy(tmp, p, l);
201  o.via.ext.ptr = tmp;
202  }
203  o.via.ext.size = static_cast<uint32_t>(l - 1);
204 }
205 
206 
208 public:
209  msgpack::object const& obj() const { return m_obj; }
210  msgpack::object& obj() { return m_obj; }
211  void set_obj(msgpack::object const& obj) { m_obj = obj; }
212  std::size_t count() const { return m_count; }
213  void set_count(std::size_t count) { m_count = count; }
214  std::size_t decr_count() { return --m_count; }
215  uint32_t container_type() const { return m_container_type; }
216  void set_container_type(uint32_t container_type) { m_container_type = container_type; }
217  msgpack::object const& map_key() const { return m_map_key; }
218  void set_map_key(msgpack::object const& map_key) { m_map_key = map_key; }
219 private:
220  msgpack::object m_obj;
221  std::size_t m_count;
222  uint32_t m_container_type;
223  msgpack::object m_map_key;
224 };
225 
226 inline void init_count(void* buffer)
227 {
228 #if defined(MSGPACK_USE_CPP03)
229  *reinterpret_cast<volatile _msgpack_atomic_counter_t*>(buffer) = 1;
230 #else // defined(MSGPACK_USE_CPP03)
231  new (buffer) std::atomic<unsigned int>(1);
232 #endif // defined(MSGPACK_USE_CPP03)
233 }
234 
235 inline void decr_count(void* buffer)
236 {
237 #if defined(MSGPACK_USE_CPP03)
238  if(_msgpack_sync_decr_and_fetch(reinterpret_cast<volatile _msgpack_atomic_counter_t*>(buffer)) == 0) {
239  free(buffer);
240  }
241 #else // defined(MSGPACK_USE_CPP03)
242  if (--*reinterpret_cast<std::atomic<unsigned int>*>(buffer) == 0) {
243  free(buffer);
244  }
245 #endif // defined(MSGPACK_USE_CPP03)
246 }
247 
248 inline void incr_count(void* buffer)
249 {
250 #if defined(MSGPACK_USE_CPP03)
251  _msgpack_sync_incr_and_fetch(reinterpret_cast<volatile _msgpack_atomic_counter_t*>(buffer));
252 #else // defined(MSGPACK_USE_CPP03)
253  ++*reinterpret_cast<std::atomic<unsigned int>*>(buffer);
254 #endif // defined(MSGPACK_USE_CPP03)
255 }
256 
257 #if defined(MSGPACK_USE_CPP03)
258 inline _msgpack_atomic_counter_t get_count(void* buffer)
259 {
260  return *reinterpret_cast<volatile _msgpack_atomic_counter_t*>(buffer);
261 }
262 #else // defined(MSGPACK_USE_CPP03)
263 inline std::atomic<unsigned int> const& get_count(void* buffer)
264 {
265  return *reinterpret_cast<std::atomic<unsigned int>*>(buffer);
266 }
267 #endif // defined(MSGPACK_USE_CPP03)
268 
269 template <typename T>
270 struct value {
271  typedef T type;
272 };
273 template <>
274 struct value<fix_tag> {
275  typedef uint32_t type;
276 };
277 
278 template <typename T>
279 inline typename msgpack::enable_if<sizeof(T) == sizeof(fix_tag)>::type load(uint32_t& dst, const char* n) {
280  dst = static_cast<uint32_t>(*reinterpret_cast<const uint8_t*>(n)) & 0x0f;
281 }
282 
283 template <typename T>
284 inline typename msgpack::enable_if<sizeof(T) == 1>::type load(T& dst, const char* n) {
285  dst = static_cast<T>(*reinterpret_cast<const uint8_t*>(n));
286 }
287 
288 template <typename T>
289 inline typename msgpack::enable_if<sizeof(T) == 2>::type load(T& dst, const char* n) {
290  _msgpack_load16(T, n, &dst);
291 }
292 
293 template <typename T>
294 inline typename msgpack::enable_if<sizeof(T) == 4>::type load(T& dst, const char* n) {
295  _msgpack_load32(T, n, &dst);
296 }
297 
298 template <typename T>
299 inline typename msgpack::enable_if<sizeof(T) == 8>::type load(T& dst, const char* n) {
300  _msgpack_load64(T, n, &dst);
301 }
302 
303 class context {
304 public:
306  :m_trail(0), m_user(f, user_data, limit), m_cs(MSGPACK_CS_HEADER)
307  {
308  m_stack.reserve(MSGPACK_EMBED_STACK_SIZE);
309  m_stack.push_back(unpack_stack());
310  }
311 
312  void init()
313  {
314  m_cs = MSGPACK_CS_HEADER;
315  m_trail = 0;
316  m_stack.resize(1);
317  m_stack[0].set_obj(msgpack::object());
318  }
319 
320  msgpack::object const& data() const
321  {
322  return m_stack[0].obj();
323  }
324 
326  {
327  return m_user;
328  }
329 
330  unpack_user const& user() const
331  {
332  return m_user;
333  }
334 
335  int execute(const char* data, std::size_t len, std::size_t& off);
336 
337 private:
338  template <typename T>
339  static uint32_t next_cs(T p)
340  {
341  return static_cast<uint32_t>(*p) & 0x1f;
342  }
343 
344  template <typename T, typename Func>
345  int push_aggregate(
346  Func const& f,
347  uint32_t container_type,
348  msgpack::object& obj,
349  const char* load_pos,
350  std::size_t& off) {
351  typename value<T>::type tmp;
352  load<T>(tmp, load_pos);
353  f(m_user, tmp, m_stack.back().obj());
354  if(tmp == 0) {
355  obj = m_stack.back().obj();
356  int ret = push_proc(obj, off);
357  if (ret != 0) return ret;
358  }
359  else {
360  m_stack.back().set_container_type(container_type);
361  m_stack.back().set_count(tmp);
362  if (m_stack.size() <= m_user.limit().depth()) {
363  m_stack.push_back(unpack_stack());
364  }
365  else {
366  throw msgpack::depth_size_overflow("depth size overflow");
367  }
368  m_cs = MSGPACK_CS_HEADER;
369  ++m_current;
370  }
371  return 0;
372  }
373 
374  int push_item(msgpack::object& obj) {
375  bool finish = false;
376  while (!finish) {
377  if(m_stack.size() == 1) {
378  return 1;
379  }
380  unpack_stack& sp = *(m_stack.end() - 2);
381  switch(sp.container_type()) {
382  case MSGPACK_CT_ARRAY_ITEM:
383  unpack_array_item(sp.obj(), obj);
384  if(sp.decr_count() == 0) {
385  obj = sp.obj();
386  m_stack.pop_back();
387  }
388  else {
389  finish = true;
390  }
391  break;
392  case MSGPACK_CT_MAP_KEY:
393  sp.set_map_key(obj);
394  sp.set_container_type(MSGPACK_CT_MAP_VALUE);
395  finish = true;
396  break;
397  case MSGPACK_CT_MAP_VALUE:
398  unpack_map_item(sp.obj(), sp.map_key(), obj);
399  if(sp.decr_count() == 0) {
400  obj = sp.obj();
401  m_stack.pop_back();
402  }
403  else {
404  sp.set_container_type(MSGPACK_CT_MAP_KEY);
405  finish = true;
406  }
407  break;
408  default:
409  return -1;
410  }
411  }
412  return 0;
413  }
414 
415  int push_proc(msgpack::object& obj, std::size_t& off) {
416  int ret = push_item(obj);
417  if (ret > 0) {
418  m_stack[0].set_obj(obj);
419  ++m_current;
420  /*printf("-- finish --\n"); */
421  off = m_current - m_start;
422  }
423  else if (ret < 0) {
424  off = m_current - m_start;
425  }
426  else {
427  m_cs = MSGPACK_CS_HEADER;
428  ++m_current;
429  }
430  return ret;
431  }
432 
433  template <std::size_t N>
434  static void check_ext_size(std::size_t /*size*/) {
435  }
436 
437 private:
438  char const* m_start;
439  char const* m_current;
440 
441  std::size_t m_trail;
442  unpack_user m_user;
443  uint32_t m_cs;
444  std::vector<unpack_stack> m_stack;
445 };
446 
447 template <>
448 inline void context::check_ext_size<4>(std::size_t size) {
449  if (size == 0xffffffff) throw msgpack::ext_size_overflow("ext size overflow");
450 }
451 
452 inline int context::execute(const char* data, std::size_t len, std::size_t& off)
453 {
454  assert(len >= off);
455 
456  m_start = data;
457  m_current = data + off;
458  const char* const pe = data + len;
459  const char* n = MSGPACK_NULLPTR;
460 
461  msgpack::object obj;
462 
463  if(m_current == pe) {
464  off = m_current - m_start;
465  return 0;
466  }
467  bool fixed_trail_again = false;
468  do {
469  if (m_cs == MSGPACK_CS_HEADER) {
470  fixed_trail_again = false;
471  int selector = *reinterpret_cast<const unsigned char*>(m_current);
472  if (0x00 <= selector && selector <= 0x7f) { // Positive Fixnum
473  unpack_uint8(*reinterpret_cast<const uint8_t*>(m_current), obj);
474  int ret = push_proc(obj, off);
475  if (ret != 0) return ret;
476  } else if(0xe0 <= selector && selector <= 0xff) { // Negative Fixnum
477  unpack_int8(*reinterpret_cast<const int8_t*>(m_current), obj);
478  int ret = push_proc(obj, off);
479  if (ret != 0) return ret;
480  } else if (0xc4 <= selector && selector <= 0xdf) {
481  const uint32_t trail[] = {
482  1, // bin 8 0xc4
483  2, // bin 16 0xc5
484  4, // bin 32 0xc6
485  1, // ext 8 0xc7
486  2, // ext 16 0xc8
487  4, // ext 32 0xc9
488  4, // float 32 0xca
489  8, // float 64 0xcb
490  1, // uint 8 0xcc
491  2, // uint 16 0xcd
492  4, // uint 32 0xce
493  8, // uint 64 0xcf
494  1, // int 8 0xd0
495  2, // int 16 0xd1
496  4, // int 32 0xd2
497  8, // int 64 0xd3
498  2, // fixext 1 0xd4
499  3, // fixext 2 0xd5
500  5, // fixext 4 0xd6
501  9, // fixext 8 0xd7
502  17,// fixext 16 0xd8
503  1, // str 8 0xd9
504  2, // str 16 0xda
505  4, // str 32 0xdb
506  2, // array 16 0xdc
507  4, // array 32 0xdd
508  2, // map 16 0xde
509  4, // map 32 0xdf
510  };
511  m_trail = trail[selector - 0xc4];
512  m_cs = next_cs(m_current);
513  fixed_trail_again = true;
514  } else if(0xa0 <= selector && selector <= 0xbf) { // FixStr
515  m_trail = static_cast<uint32_t>(*m_current) & 0x1f;
516  if(m_trail == 0) {
517  unpack_str(m_user, n, static_cast<uint32_t>(m_trail), obj);
518  int ret = push_proc(obj, off);
519  if (ret != 0) return ret;
520  }
521  else {
522  m_cs = MSGPACK_ACS_STR_VALUE;
523  fixed_trail_again = true;
524  }
525 
526  } else if(0x90 <= selector && selector <= 0x9f) { // FixArray
527  int ret = push_aggregate<fix_tag>(
528  unpack_array(), MSGPACK_CT_ARRAY_ITEM, obj, m_current, off);
529  if (ret != 0) return ret;
530  } else if(0x80 <= selector && selector <= 0x8f) { // FixMap
531  int ret = push_aggregate<fix_tag>(
532  unpack_map(), MSGPACK_CT_MAP_KEY, obj, m_current, off);
533  if (ret != 0) return ret;
534  } else if(selector == 0xc2) { // false
535  unpack_false(obj);
536  int ret = push_proc(obj, off);
537  if (ret != 0) return ret;
538  } else if(selector == 0xc3) { // true
539  unpack_true(obj);
540  int ret = push_proc(obj, off);
541  if (ret != 0) return ret;
542  } else if(selector == 0xc0) { // nil
543  unpack_nil(obj);
544  int ret = push_proc(obj, off);
545  if (ret != 0) return ret;
546  } else {
547  off = m_current - m_start;
548  return -1;
549  }
550  // end MSGPACK_CS_HEADER
551  }
552  if (m_cs != MSGPACK_CS_HEADER || fixed_trail_again) {
553  if (fixed_trail_again) {
554  ++m_current;
555  fixed_trail_again = false;
556  }
557  if(static_cast<std::size_t>(pe - m_current) < m_trail) {
558  off = m_current - m_start;
559  return 0;
560  }
561  n = m_current;
562  m_current += m_trail - 1;
563  switch(m_cs) {
564  //case MSGPACK_CS_
565  //case MSGPACK_CS_
566  case MSGPACK_CS_FLOAT: {
567  union { uint32_t i; float f; } mem;
568  load<uint32_t>(mem.i, n);
569  unpack_float(mem.f, obj);
570  int ret = push_proc(obj, off);
571  if (ret != 0) return ret;
572  } break;
573  case MSGPACK_CS_DOUBLE: {
574  union { uint64_t i; double f; } mem;
575  load<uint64_t>(mem.i, n);
576 #if defined(TARGET_OS_IPHONE)
577  // ok
578 #elif defined(__arm__) && !(__ARM_EABI__) // arm-oabi
579  // https://github.com/msgpack/msgpack-perl/pull/1
580  mem.i = (mem.i & 0xFFFFFFFFUL) << 32UL | (mem.i >> 32UL);
581 #endif
582  unpack_double(mem.f, obj);
583  int ret = push_proc(obj, off);
584  if (ret != 0) return ret;
585  } break;
586  case MSGPACK_CS_UINT_8: {
587  uint8_t tmp;
588  load<uint8_t>(tmp, n);
589  unpack_uint8(tmp, obj);
590  int ret = push_proc(obj, off);
591  if (ret != 0) return ret;
592  } break;
593  case MSGPACK_CS_UINT_16: {
594  uint16_t tmp;
595  load<uint16_t>(tmp, n);
596  unpack_uint16(tmp, obj);
597  int ret = push_proc(obj, off);
598  if (ret != 0) return ret;
599  } break;
600  case MSGPACK_CS_UINT_32: {
601  uint32_t tmp;
602  load<uint32_t>(tmp, n);
603  unpack_uint32(tmp, obj);
604  int ret = push_proc(obj, off);
605  if (ret != 0) return ret;
606  } break;
607  case MSGPACK_CS_UINT_64: {
608  uint64_t tmp;
609  load<uint64_t>(tmp, n);
610  unpack_uint64(tmp, obj);
611  int ret = push_proc(obj, off);
612  if (ret != 0) return ret;
613  } break;
614  case MSGPACK_CS_INT_8: {
615  int8_t tmp;
616  load<int8_t>(tmp, n);
617  unpack_int8(tmp, obj);
618  int ret = push_proc(obj, off);
619  if (ret != 0) return ret;
620  } break;
621  case MSGPACK_CS_INT_16: {
622  int16_t tmp;
623  load<int16_t>(tmp, n);
624  unpack_int16(tmp, obj);
625  int ret = push_proc(obj, off);
626  if (ret != 0) return ret;
627  } break;
628  case MSGPACK_CS_INT_32: {
629  int32_t tmp;
630  load<int32_t>(tmp, n);
631  unpack_int32(tmp, obj);
632  int ret = push_proc(obj, off);
633  if (ret != 0) return ret;
634  } break;
635  case MSGPACK_CS_INT_64: {
636  int64_t tmp;
637  load<int64_t>(tmp, n);
638  unpack_int64(tmp, obj);
639  int ret = push_proc(obj, off);
640  if (ret != 0) return ret;
641  } break;
642  case MSGPACK_CS_FIXEXT_1: {
643  unpack_ext(m_user, n, 1+1, obj);
644  int ret = push_proc(obj, off);
645  if (ret != 0) return ret;
646  } break;
647  case MSGPACK_CS_FIXEXT_2: {
648  unpack_ext(m_user, n, 2+1, obj);
649  int ret = push_proc(obj, off);
650  if (ret != 0) return ret;
651  } break;
652  case MSGPACK_CS_FIXEXT_4: {
653  unpack_ext(m_user, n, 4+1, obj);
654  int ret = push_proc(obj, off);
655  if (ret != 0) return ret;
656  } break;
657  case MSGPACK_CS_FIXEXT_8: {
658  unpack_ext(m_user, n, 8+1, obj);
659  int ret = push_proc(obj, off);
660  if (ret != 0) return ret;
661  } break;
662  case MSGPACK_CS_FIXEXT_16: {
663  unpack_ext(m_user, n, 16+1, obj);
664  int ret = push_proc(obj, off);
665  if (ret != 0) return ret;
666  } break;
667  case MSGPACK_CS_STR_8: {
668  uint8_t tmp;
669  load<uint8_t>(tmp, n);
670  m_trail = tmp;
671  if(m_trail == 0) {
672  unpack_str(m_user, n, static_cast<uint32_t>(m_trail), obj);
673  int ret = push_proc(obj, off);
674  if (ret != 0) return ret;
675  }
676  else {
677  m_cs = MSGPACK_ACS_STR_VALUE;
678  fixed_trail_again = true;
679  }
680  } break;
681  case MSGPACK_CS_BIN_8: {
682  uint8_t tmp;
683  load<uint8_t>(tmp, n);
684  m_trail = tmp;
685  if(m_trail == 0) {
686  unpack_bin(m_user, n, static_cast<uint32_t>(m_trail), obj);
687  int ret = push_proc(obj, off);
688  if (ret != 0) return ret;
689  }
690  else {
691  m_cs = MSGPACK_ACS_BIN_VALUE;
692  fixed_trail_again = true;
693  }
694  } break;
695  case MSGPACK_CS_EXT_8: {
696  uint8_t tmp;
697  load<uint8_t>(tmp, n);
698  m_trail = tmp + 1;
699  if(m_trail == 0) {
700  unpack_ext(m_user, n, m_trail, obj);
701  int ret = push_proc(obj, off);
702  if (ret != 0) return ret;
703  }
704  else {
705  m_cs = MSGPACK_ACS_EXT_VALUE;
706  fixed_trail_again = true;
707  }
708  } break;
709  case MSGPACK_CS_STR_16: {
710  uint16_t tmp;
711  load<uint16_t>(tmp, n);
712  m_trail = tmp;
713  if(m_trail == 0) {
714  unpack_str(m_user, n, static_cast<uint32_t>(m_trail), obj);
715  int ret = push_proc(obj, off);
716  if (ret != 0) return ret;
717  }
718  else {
719  m_cs = MSGPACK_ACS_STR_VALUE;
720  fixed_trail_again = true;
721  }
722  } break;
723  case MSGPACK_CS_BIN_16: {
724  uint16_t tmp;
725  load<uint16_t>(tmp, n);
726  m_trail = tmp;
727  if(m_trail == 0) {
728  unpack_bin(m_user, n, static_cast<uint32_t>(m_trail), obj);
729  int ret = push_proc(obj, off);
730  if (ret != 0) return ret;
731  }
732  else {
733  m_cs = MSGPACK_ACS_BIN_VALUE;
734  fixed_trail_again = true;
735  }
736  } break;
737  case MSGPACK_CS_EXT_16: {
738  uint16_t tmp;
739  load<uint16_t>(tmp, n);
740  m_trail = tmp + 1;
741  if(m_trail == 0) {
742  unpack_ext(m_user, n, m_trail, obj);
743  int ret = push_proc(obj, off);
744  if (ret != 0) return ret;
745  }
746  else {
747  m_cs = MSGPACK_ACS_EXT_VALUE;
748  fixed_trail_again = true;
749  }
750  } break;
751  case MSGPACK_CS_STR_32: {
752  uint32_t tmp;
753  load<uint32_t>(tmp, n);
754  m_trail = tmp;
755  if(m_trail == 0) {
756  unpack_str(m_user, n, static_cast<uint32_t>(m_trail), obj);
757  int ret = push_proc(obj, off);
758  if (ret != 0) return ret;
759  }
760  else {
761  m_cs = MSGPACK_ACS_STR_VALUE;
762  fixed_trail_again = true;
763  }
764  } break;
765  case MSGPACK_CS_BIN_32: {
766  uint32_t tmp;
767  load<uint32_t>(tmp, n);
768  m_trail = tmp;
769  if(m_trail == 0) {
770  unpack_bin(m_user, n, static_cast<uint32_t>(m_trail), obj);
771  int ret = push_proc(obj, off);
772  if (ret != 0) return ret;
773  }
774  else {
775  m_cs = MSGPACK_ACS_BIN_VALUE;
776  fixed_trail_again = true;
777  }
778  } break;
779  case MSGPACK_CS_EXT_32: {
780  uint32_t tmp;
781  load<uint32_t>(tmp, n);
782  check_ext_size<sizeof(std::size_t)>(tmp);
783  m_trail = tmp;
784  ++m_trail;
785  if(m_trail == 0) {
786  unpack_ext(m_user, n, m_trail, obj);
787  int ret = push_proc(obj, off);
788  if (ret != 0) return ret;
789  }
790  else {
791  m_cs = MSGPACK_ACS_EXT_VALUE;
792  fixed_trail_again = true;
793  }
794  } break;
795  case MSGPACK_ACS_STR_VALUE: {
796  unpack_str(m_user, n, static_cast<uint32_t>(m_trail), obj);
797  int ret = push_proc(obj, off);
798  if (ret != 0) return ret;
799  } break;
800  case MSGPACK_ACS_BIN_VALUE: {
801  unpack_bin(m_user, n, static_cast<uint32_t>(m_trail), obj);
802  int ret = push_proc(obj, off);
803  if (ret != 0) return ret;
804  } break;
805  case MSGPACK_ACS_EXT_VALUE: {
806  unpack_ext(m_user, n, m_trail, obj);
807  int ret = push_proc(obj, off);
808  if (ret != 0) return ret;
809  } break;
810  case MSGPACK_CS_ARRAY_16: {
811  int ret = push_aggregate<uint16_t>(
812  unpack_array(), MSGPACK_CT_ARRAY_ITEM, obj, n, off);
813  if (ret != 0) return ret;
814  } break;
815  case MSGPACK_CS_ARRAY_32: {
816  /* FIXME security guard */
817  int ret = push_aggregate<uint32_t>(
818  unpack_array(), MSGPACK_CT_ARRAY_ITEM, obj, n, off);
819  if (ret != 0) return ret;
820  } break;
821  case MSGPACK_CS_MAP_16: {
822  int ret = push_aggregate<uint16_t>(
823  unpack_map(), MSGPACK_CT_MAP_KEY, obj, n, off);
824  if (ret != 0) return ret;
825  } break;
826  case MSGPACK_CS_MAP_32: {
827  /* FIXME security guard */
828  int ret = push_aggregate<uint32_t>(
829  unpack_map(), MSGPACK_CT_MAP_KEY, obj, n, off);
830  if (ret != 0) return ret;
831  } break;
832  default:
833  off = m_current - m_start;
834  return -1;
835  }
836  }
837  } while(m_current != pe);
838 
839  off = m_current - m_start;
840  return 0;
841 }
842 
843 } // detail
844 
845 
847 class unpacker {
848 public:
850 
858  unpacker(unpack_reference_func f = &unpacker::default_reference_func,
859  void* user_data = MSGPACK_NULLPTR,
860  std::size_t initial_buffer_size = MSGPACK_UNPACKER_INIT_BUFFER_SIZE,
861  unpack_limit const& limit = unpack_limit());
862 
863 #if !defined(MSGPACK_USE_CPP03)
864  unpacker(unpacker&& other);
865  unpacker& operator=(unpacker&& other);
866 #endif // !defined(MSGPACK_USE_CPP03)
867 
868  ~unpacker();
869 
870 public:
872 
879  void reserve_buffer(std::size_t size = MSGPACK_UNPACKER_RESERVE_SIZE);
880 
882 
887  char* buffer();
888 
890 
896  std::size_t buffer_capacity() const;
897 
899 
908  void buffer_consumed(std::size_t size);
909 
911 
924  MSGPACK_DEPRECATED("please use reference version instead")
925  bool next(msgpack::object_handle* result);
926 
928 
941  bool next(msgpack::object_handle& result, bool& referenced);
942 
944 
955  bool next(msgpack::object_handle& result);
956 
958 
961  std::size_t message_size() const;
962 
964  bool execute();
965 
967  msgpack::object const& data();
968 
970  msgpack::zone* release_zone();
971 
973  void reset_zone();
974 
976  void reset();
977 
978 public:
980 
986  std::size_t parsed_size() const;
987 
989 
995  char* nonparsed_buffer();
996 
998 
1004  std::size_t nonparsed_size() const;
1005 
1007 
1014  void skip_nonparsed_buffer(std::size_t size);
1015 
1017 
1021  void remove_nonparsed_buffer();
1022 
1023 private:
1024  void expand_buffer(std::size_t size);
1025  int execute_imp();
1026  bool flush_zone();
1027  static bool default_reference_func(msgpack::type::object_type type, std::size_t len, void*);
1028 
1029 private:
1030  char* m_buffer;
1031  std::size_t m_used;
1032  std::size_t m_free;
1033  std::size_t m_off;
1034  std::size_t m_parsed;
1036  std::size_t m_initial_buffer_size;
1037  detail::context m_ctx;
1038 
1039 #if defined(MSGPACK_USE_CPP03)
1040 private:
1041  unpacker(const unpacker&);
1042  unpacker& operator=(const unpacker&);
1043 #else // defined(MSGPACK_USE_CPP03)
1044  unpacker(const unpacker&) = delete;
1045  unpacker& operator=(const unpacker&) = delete;
1046 #endif // defined(MSGPACK_USE_CPP03)
1047 };
1048 
1050  void* user_data,
1051  std::size_t initial_buffer_size,
1052  unpack_limit const& limit)
1053  :m_z(new msgpack::zone), m_ctx(f, user_data, limit)
1054 {
1055  if(initial_buffer_size < COUNTER_SIZE) {
1056  initial_buffer_size = COUNTER_SIZE;
1057  }
1058 
1059  char* buffer = static_cast<char*>(::malloc(initial_buffer_size));
1060  if(!buffer) {
1061  throw std::bad_alloc();
1062  }
1063 
1064  m_buffer = buffer;
1065  m_used = COUNTER_SIZE;
1066  m_free = initial_buffer_size - m_used;
1067  m_off = COUNTER_SIZE;
1068  m_parsed = 0;
1069  m_initial_buffer_size = initial_buffer_size;
1070 
1071  detail::init_count(m_buffer);
1072 
1073  m_ctx.init();
1074  m_ctx.user().set_zone(*m_z);
1075  m_ctx.user().set_referenced(false);
1076 }
1077 
1078 #if !defined(MSGPACK_USE_CPP03)
1079 // Move constructor and move assignment operator
1080 
1082  :m_buffer(other.m_buffer),
1083  m_used(other.m_used),
1084  m_free(other.m_free),
1085  m_off(other.m_off),
1086  m_parsed(other.m_parsed),
1087  m_z(std::move(other.m_z)),
1088  m_initial_buffer_size(other.m_initial_buffer_size),
1089  m_ctx(other.m_ctx) {
1090  other.m_buffer = MSGPACK_NULLPTR;
1091 }
1092 
1094  this->~unpacker();
1095  new (this) unpacker(std::move(other));
1096  return *this;
1097 }
1098 
1099 #endif // !defined(MSGPACK_USE_CPP03)
1100 
1101 
1103 {
1104  // These checks are required for move operations.
1105  if (m_buffer) detail::decr_count(m_buffer);
1106 }
1107 
1108 
1109 inline void unpacker::reserve_buffer(std::size_t size)
1110 {
1111  if(m_free >= size) return;
1112  expand_buffer(size);
1113 }
1114 
1115 inline void unpacker::expand_buffer(std::size_t size)
1116 {
1117  if(m_used == m_off && detail::get_count(m_buffer) == 1
1118  && !m_ctx.user().referenced()) {
1119  // rewind buffer
1120  m_free += m_used - COUNTER_SIZE;
1121  m_used = COUNTER_SIZE;
1122  m_off = COUNTER_SIZE;
1123 
1124  if(m_free >= size) return;
1125  }
1126 
1127  if(m_off == COUNTER_SIZE) {
1128  std::size_t next_size = (m_used + m_free) * 2; // include COUNTER_SIZE
1129  while(next_size < size + m_used) {
1130  std::size_t tmp_next_size = next_size * 2;
1131  if (tmp_next_size <= next_size) {
1132  next_size = size + m_used;
1133  break;
1134  }
1135  next_size = tmp_next_size;
1136  }
1137 
1138  char* tmp = static_cast<char*>(::realloc(m_buffer, next_size));
1139  if(!tmp) {
1140  throw std::bad_alloc();
1141  }
1142 
1143  m_buffer = tmp;
1144  m_free = next_size - m_used;
1145 
1146  } else {
1147  std::size_t next_size = m_initial_buffer_size; // include COUNTER_SIZE
1148  std::size_t not_parsed = m_used - m_off;
1149  while(next_size < size + not_parsed + COUNTER_SIZE) {
1150  std::size_t tmp_next_size = next_size * 2;
1151  if (tmp_next_size <= next_size) {
1152  next_size = size + not_parsed + COUNTER_SIZE;
1153  break;
1154  }
1155  next_size = tmp_next_size;
1156  }
1157 
1158  char* tmp = static_cast<char*>(::malloc(next_size));
1159  if(!tmp) {
1160  throw std::bad_alloc();
1161  }
1162 
1163  detail::init_count(tmp);
1164 
1165  std::memcpy(tmp+COUNTER_SIZE, m_buffer + m_off, not_parsed);
1166 
1167  if(m_ctx.user().referenced()) {
1168  try {
1169  m_z->push_finalizer(&detail::decr_count, m_buffer);
1170  }
1171  catch (...) {
1172  ::free(tmp);
1173  throw;
1174  }
1175  m_ctx.user().set_referenced(false);
1176  } else {
1177  detail::decr_count(m_buffer);
1178  }
1179 
1180  m_buffer = tmp;
1181  m_used = not_parsed + COUNTER_SIZE;
1182  m_free = next_size - m_used;
1183  m_off = COUNTER_SIZE;
1184  }
1185 }
1186 
1187 inline char* unpacker::buffer()
1188 {
1189  return m_buffer + m_used;
1190 }
1191 
1192 inline std::size_t unpacker::buffer_capacity() const
1193 {
1194  return m_free;
1195 }
1196 
1197 inline void unpacker::buffer_consumed(std::size_t size)
1198 {
1199  m_used += size;
1200  m_free -= size;
1201 }
1202 
1204 {
1205  referenced = false;
1206  int ret = execute_imp();
1207  if(ret < 0) {
1208  throw msgpack::parse_error("parse error");
1209  }
1210 
1211  if(ret == 0) {
1212  result.zone().reset();
1213  result.set(msgpack::object());
1214  return false;
1215 
1216  } else {
1217  referenced = m_ctx.user().referenced();
1218  result.zone().reset( release_zone() );
1219  result.set(data());
1220  reset();
1221  return true;
1222  }
1223 }
1224 
1226 {
1227  bool referenced;
1228  return next(result, referenced);
1229 }
1230 
1232 {
1233  return next(*result);
1234 }
1235 
1236 
1237 inline bool unpacker::execute()
1238 {
1239  int ret = execute_imp();
1240  if(ret < 0) {
1241  throw msgpack::parse_error("parse error");
1242  } else if(ret == 0) {
1243  return false;
1244  } else {
1245  return true;
1246  }
1247 }
1248 
1249 inline int unpacker::execute_imp()
1250 {
1251  std::size_t off = m_off;
1252  int ret = m_ctx.execute(m_buffer, m_used, m_off);
1253  if(m_off > off) {
1254  m_parsed += m_off - off;
1255  }
1256  return ret;
1257 }
1258 
1260 {
1261  return m_ctx.data();
1262 }
1263 
1265 {
1266  if(!flush_zone()) {
1267  return MSGPACK_NULLPTR;
1268  }
1269 
1270  msgpack::zone* r = new msgpack::zone;
1271  msgpack::zone* old = m_z.release();
1272  m_z.reset(r);
1273  m_ctx.user().set_zone(*m_z);
1274 
1275  return old;
1276 }
1277 
1279 {
1280  m_z->clear();
1281 }
1282 
1283 inline bool unpacker::flush_zone()
1284 {
1285  if(m_ctx.user().referenced()) {
1286  try {
1287  m_z->push_finalizer(&detail::decr_count, m_buffer);
1288  } catch (...) {
1289  return false;
1290  }
1291  m_ctx.user().set_referenced(false);
1292 
1293  detail::incr_count(m_buffer);
1294  }
1295 
1296  return true;
1297 }
1298 
1299 inline void unpacker::reset()
1300 {
1301  m_ctx.init();
1302  // don't reset referenced flag
1303  m_parsed = 0;
1304 }
1305 
1306 inline std::size_t unpacker::message_size() const
1307 {
1308  return m_parsed - m_off + m_used;
1309 }
1310 
1311 inline std::size_t unpacker::parsed_size() const
1312 {
1313  return m_parsed;
1314 }
1315 
1317 {
1318  return m_buffer + m_off;
1319 }
1320 
1321 inline std::size_t unpacker::nonparsed_size() const
1322 {
1323  return m_used - m_off;
1324 }
1325 
1326 inline void unpacker::skip_nonparsed_buffer(std::size_t size)
1327 {
1328  m_off += size;
1329 }
1330 
1332 {
1333  m_used = m_off;
1334 }
1335 
1336 namespace detail {
1337 
1338 inline parse_return
1339 unpack_imp(const char* data, std::size_t len, std::size_t& off,
1340  msgpack::zone& result_zone, msgpack::object& result, bool& referenced,
1341  unpack_reference_func f = MSGPACK_NULLPTR, void* user_data = MSGPACK_NULLPTR,
1342  unpack_limit const& limit = unpack_limit())
1343 {
1344  std::size_t noff = off;
1345 
1346  if(len <= noff) {
1347  // FIXME
1348  return PARSE_CONTINUE;
1349  }
1350 
1351  detail::context ctx(f, user_data, limit);
1352  ctx.init();
1353 
1354  ctx.user().set_zone(result_zone);
1355  ctx.user().set_referenced(false);
1356  referenced = false;
1357 
1358  int e = ctx.execute(data, len, noff);
1359  if(e < 0) {
1360  return PARSE_PARSE_ERROR;
1361  }
1362 
1363  referenced = ctx.user().referenced();
1364  off = noff;
1365 
1366  if(e == 0) {
1367  return PARSE_CONTINUE;
1368  }
1369 
1370  result = ctx.data();
1371 
1372  if(noff < len) {
1373  return PARSE_EXTRA_BYTES;
1374  }
1375 
1376  return PARSE_SUCCESS;
1377 }
1378 
1379 } // detail
1380 
1381 // reference version
1382 
1384  const char* data, std::size_t len, std::size_t& off, bool& referenced,
1385  unpack_reference_func f, void* user_data,
1386  unpack_limit const& limit
1387 )
1388 {
1389  msgpack::object obj;
1391  referenced = false;
1392  std::size_t noff = off;
1394  data, len, noff, *z, obj, referenced, f, user_data, limit);
1395 
1396  switch(ret) {
1397  case PARSE_SUCCESS:
1398  off = noff;
1399  return msgpack::object_handle(obj, msgpack::move(z));
1400  case PARSE_EXTRA_BYTES:
1401  off = noff;
1402  return msgpack::object_handle(obj, msgpack::move(z));
1403  case PARSE_CONTINUE:
1404  throw msgpack::insufficient_bytes("insufficient bytes");
1405  case PARSE_PARSE_ERROR:
1406  default:
1407  throw msgpack::parse_error("parse error");
1408  }
1409  return msgpack::object_handle();
1410 }
1411 
1413  const char* data, std::size_t len, std::size_t& off,
1414  unpack_reference_func f, void* user_data,
1415  unpack_limit const& limit)
1416 {
1417  bool referenced;
1418  return unpack(data, len, off, referenced, f, user_data, limit);
1419 }
1420 
1422  const char* data, std::size_t len, bool& referenced,
1423  unpack_reference_func f, void* user_data,
1424  unpack_limit const& limit)
1425 {
1426  std::size_t off = 0;
1427  return unpack(data, len, off, referenced, f, user_data, limit);
1428 }
1429 
1431  const char* data, std::size_t len,
1432  unpack_reference_func f, void* user_data,
1433  unpack_limit const& limit)
1434 {
1435  bool referenced;
1436  std::size_t off = 0;
1437  return unpack(data, len, off, referenced, f, user_data, limit);
1438 }
1439 
1440 inline void unpack(
1441  msgpack::object_handle& result,
1442  const char* data, std::size_t len, std::size_t& off, bool& referenced,
1443  unpack_reference_func f, void* user_data,
1444  unpack_limit const& limit)
1445 {
1446  msgpack::object obj;
1448  referenced = false;
1449  std::size_t noff = off;
1451  data, len, noff, *z, obj, referenced, f, user_data, limit);
1452 
1453  switch(ret) {
1454  case PARSE_SUCCESS:
1455  off = noff;
1456  result.set(obj);
1457  result.zone() = msgpack::move(z);
1458  return;
1459  case PARSE_EXTRA_BYTES:
1460  off = noff;
1461  result.set(obj);
1462  result.zone() = msgpack::move(z);
1463  return;
1464  case PARSE_CONTINUE:
1465  throw msgpack::insufficient_bytes("insufficient bytes");
1466  case PARSE_PARSE_ERROR:
1467  default:
1468  throw msgpack::parse_error("parse error");
1469  }
1470 }
1471 
1472 inline void unpack(
1473  msgpack::object_handle& result,
1474  const char* data, std::size_t len, std::size_t& off,
1475  unpack_reference_func f, void* user_data,
1476  unpack_limit const& limit)
1477 {
1478  bool referenced;
1479  unpack(result, data, len, off, referenced, f, user_data, limit);
1480 }
1481 
1482 inline void unpack(
1483  msgpack::object_handle& result,
1484  const char* data, std::size_t len, bool& referenced,
1485  unpack_reference_func f, void* user_data,
1486  unpack_limit const& limit)
1487 {
1488  std::size_t off = 0;
1489  unpack(result, data, len, off, referenced, f, user_data, limit);
1490 }
1491 
1492 inline void unpack(
1493  msgpack::object_handle& result,
1494  const char* data, std::size_t len,
1495  unpack_reference_func f, void* user_data,
1496  unpack_limit const& limit)
1497 {
1498  bool referenced;
1499  std::size_t off = 0;
1500  unpack(result, data, len, off, referenced, f, user_data, limit);
1501 }
1502 
1503 
1505  msgpack::zone& z,
1506  const char* data, std::size_t len, std::size_t& off, bool& referenced,
1507  unpack_reference_func f, void* user_data,
1508  unpack_limit const& limit)
1509 {
1510  msgpack::object obj;
1511  std::size_t noff = off;
1512  referenced = false;
1514  data, len, noff, z, obj, referenced, f, user_data, limit);
1515 
1516  switch(ret) {
1517  case PARSE_SUCCESS:
1518  off = noff;
1519  return obj;
1520  case PARSE_EXTRA_BYTES:
1521  off = noff;
1522  return obj;
1523  case PARSE_CONTINUE:
1524  throw msgpack::insufficient_bytes("insufficient bytes");
1525  case PARSE_PARSE_ERROR:
1526  default:
1527  throw msgpack::parse_error("parse error");
1528  }
1529  return obj;
1530 }
1531 
1533  msgpack::zone& z,
1534  const char* data, std::size_t len, std::size_t& off,
1535  unpack_reference_func f, void* user_data,
1536  unpack_limit const& limit)
1537 {
1538  bool referenced;
1539  return unpack(z, data, len, off, referenced, f, user_data, limit);
1540 }
1541 
1543  msgpack::zone& z,
1544  const char* data, std::size_t len, bool& referenced,
1545  unpack_reference_func f, void* user_data,
1546  unpack_limit const& limit)
1547 {
1548  std::size_t off = 0;
1549  return unpack(z, data, len, off, referenced, f, user_data, limit);
1550 }
1551 
1553  msgpack::zone& z,
1554  const char* data, std::size_t len,
1555  unpack_reference_func f, void* user_data,
1556  unpack_limit const& limit)
1557 {
1558  bool referenced;
1559  std::size_t off = 0;
1560  return unpack(z, data, len, off, referenced, f, user_data, limit);
1561 }
1562 
1563 // obsolete
1564 // pointer version
1565 MSGPACK_DEPRECATED("please use reference version instead")
1566 inline void unpack(
1567  msgpack::object_handle* result,
1568  const char* data, std::size_t len, std::size_t* off, bool* referenced,
1569  unpack_reference_func f, void* user_data,
1570  unpack_limit const& limit)
1571 {
1572  if (off)
1573  if (referenced) unpack(*result, data, len, *off, *referenced, f, user_data, limit);
1574  else unpack(*result, data, len, *off, f, user_data, limit);
1575  else
1576  if (referenced) unpack(*result, data, len, *referenced, f, user_data, limit);
1577  else unpack(*result, data, len, f, user_data, limit);
1578 }
1579 
1580 inline bool unpacker::default_reference_func(msgpack::type::object_type /*type*/, std::size_t /*len*/, void*)
1581 {
1582  return true;
1583 }
1584 
1586 } // MSGPACK_API_VERSION_NAMESPACE(v1)
1588 
1589 } // namespace msgpack
1590 
1591 
1592 #endif // MSGPACK_V1_UNPACK_HPP
uint32_t type
Definition: unpack.hpp:275
bool next()
Unpack one msgpack::object.
Definition: parse.hpp:938
Definition: object_fwd_decl.hpp:39
Definition: unpack.hpp:44
Definition: unpack.hpp:111
void operator()(unpack_user &u, uint32_t n, msgpack::object &o) const
Definition: unpack.hpp:112
char * nonparsed_buffer()
Get the address that is not parsed in the buffer.
Definition: unpack.hpp:1316
Definition: unpack.hpp:133
void unpack_int8(int8_t d, msgpack::object &o)
Definition: unpack.hpp:80
msgpack::object_kv * ptr
Definition: object_fwd.hpp:29
void incr_count(void *buffer)
Definition: unpack.hpp:248
Definition: unpack_decl.hpp:87
Definition: unpack_exception.hpp:79
uint32_t size
Definition: object_fwd.hpp:23
std::size_t bin() const
Definition: unpack_decl.hpp:105
void set_referenced(bool referenced)
Definition: unpack.hpp:54
std::size_t ext() const
Definition: unpack_decl.hpp:106
void * allocate_align(size_t size, size_t align=MSGPACK_ZONE_ALIGN)
Definition: cpp03_zone.hpp:236
~unpacker()
Definition: unpack.hpp:1102
void operator()(unpack_user &u, uint32_t n, msgpack::object &o) const
Definition: unpack.hpp:134
Definition: object_fwd_decl.hpp:33
void check_ext_size(std::size_t)
Definition: parse.hpp:216
Definition: object_fwd_decl.hpp:30
const char * ptr
Definition: object_fwd.hpp:39
#define MSGPACK_UNPACKER_INIT_BUFFER_SIZE
Definition: unpack_decl.hpp:43
void unpack_ext(unpack_user &u, const char *p, std::size_t l, msgpack::object &o)
Definition: unpack.hpp:190
parse_return
Definition: parse_return.hpp:23
parse_return unpack_imp(const char *data, std::size_t len, std::size_t &off, msgpack::zone &result_zone, msgpack::object &result, bool &referenced, unpack_reference_func f=MSGPACK_NULLPTR, void *user_data=MSGPACK_NULLPTR, unpack_limit const &limit=unpack_limit())
Definition: unpack.hpp:1339
void set_container_type(uint32_t container_type)
Definition: unpack.hpp:216
uint32_t container_type() const
Definition: unpack.hpp:215
void unpack_uint32(uint32_t d, msgpack::object &o)
Definition: unpack.hpp:74
std::size_t map() const
Definition: unpack_decl.hpp:103
std::size_t nonparsed_size() const
Get the size of the buffer that is not parsed.
Definition: unpack.hpp:1321
void set(msgpack::object const &obj)
Definition: object.hpp:63
void init()
Definition: unpack.hpp:312
Definition: parse_return.hpp:27
union_type via
Definition: object_fwd.hpp:93
std::size_t count() const
Definition: unpack.hpp:212
void unpack_str(unpack_user &u, const char *p, uint32_t l, msgpack::object &o)
Definition: unpack.hpp:158
bool execute()
Definition: unpack.hpp:1237
bool(* unpack_reference_func)(msgpack::type::object_type type, std::size_t size, void *user_data)
The type of reference or copy judging function.
Definition: unpack_decl.hpp:74
Definition: object_fwd_decl.hpp:40
Definition: unpack_exception.hpp:106
void unpack_int16(int16_t d, msgpack::object &o)
Definition: unpack.hpp:84
void unpack_bin(unpack_user &u, const char *p, uint32_t l, msgpack::object &o)
Definition: unpack.hpp:174
const char * ptr
Definition: object_fwd.hpp:46
unpack_user const & user() const
Definition: unpack.hpp:330
msgpack::object * ptr
Definition: object_fwd.hpp:24
void unpack_uint8(uint8_t d, msgpack::object &o)
Definition: unpack.hpp:68
msgpack::zone const & zone() const
Definition: unpack.hpp:50
msgpack::object val
Definition: object.hpp:31
context(unpack_reference_func f, void *user_data, unpack_limit const &limit)
Definition: unpack.hpp:305
uint32_t size
Definition: object_fwd.hpp:38
bool referenced() const
Definition: create_object_visitor.hpp:63
T type
Definition: unpack.hpp:271
Definition: adaptor_base.hpp:15
Definition: unpack_exception.hpp:34
Definition: unpack_exception.hpp:97
const char * ptr
Definition: object_fwd.hpp:34
std::size_t parsed_size() const
Get parsed message size.
Definition: unpack.hpp:1311
Definition: cpp03_zone.hpp:22
unpacker(unpack_reference_func f=&unpacker::default_reference_func, void *user_data=MSGPACK_NULLPTR, std::size_t initial_buffer_size=MSGPACK_UNPACKER_INIT_BUFFER_SIZE, unpack_limit const &limit=unpack_limit())
Constructor.
Definition: unpack.hpp:1049
void unpack_false(msgpack::object &o)
Definition: unpack.hpp:108
object_type
Definition: object_fwd_decl.hpp:28
bool boolean
Definition: object_fwd.hpp:77
int execute(const char *data, std::size_t len, std::size_t &off)
Definition: unpack.hpp:452
void set_obj(msgpack::object const &obj)
Definition: unpack.hpp:211
void check_ext_size< 4 >(std::size_t size)
Definition: parse.hpp:220
Definition: object.hpp:29
void remove_nonparsed_buffer()
Remove nonparsed buffer and reset the current position as a new start point.
Definition: unpack.hpp:1331
Definition: object_fwd_decl.hpp:29
int64_t i64
Definition: object_fwd.hpp:79
Definition: object_fwd_decl.hpp:32
void reserve_buffer(std::size_t size=MSGPACK_UNPACKER_RESERVE_SIZE)
Reserve a buffer memory.
Definition: unpack.hpp:1109
Definition: unpack.hpp:303
void unpack_float(float d, msgpack::object &o)
Definition: unpack.hpp:96
Definition: unpack_exception.hpp:88
void init_count(void *buffer)
Definition: unpack.hpp:226
void unpack_double(double d, msgpack::object &o)
Definition: unpack.hpp:99
unpack_reference_func reference_func() const
Definition: unpack.hpp:55
msgpack::object & obj()
Definition: unpack.hpp:210
void unpack_uint64(uint64_t d, msgpack::object &o)
Definition: unpack.hpp:77
Definition: parse_return.hpp:26
uint32_t size
Definition: object_fwd.hpp:45
Definition: unpack_exception.hpp:43
msgpack::enable_if< sizeof(T)==sizeof(fix_tag)>::type load(uint32_t &dst, const char *n)
Definition: unpack.hpp:279
Definition: unpack_exception.hpp:61
void reset_zone()
Definition: unpack.hpp:1278
unpack_limit const & limit() const
Definition: unpack.hpp:57
std::size_t size(T const &t)
Definition: size_equal_only.hpp:24
msgpack::zone * release_zone()
Definition: unpack.hpp:1264
void unpack_int64(int64_t d, msgpack::object &o)
Definition: unpack.hpp:92
std::size_t str() const
Definition: unpack_decl.hpp:104
void unpack_uint16(uint16_t d, msgpack::object &o)
Definition: unpack.hpp:71
void unpack_true(msgpack::object &o)
Definition: unpack.hpp:105
void set_map_key(msgpack::object const &map_key)
Definition: unpack.hpp:218
void buffer_consumed(std::size_t size)
Notify a buffer consumed information to msgpack::unpacker.
Definition: unpack.hpp:1197
double f64
Definition: object_fwd.hpp:84
msgpack::object const & data()
Definition: unpack.hpp:1259
Definition: object_fwd_decl.hpp:43
msgpack::object_array array
Definition: object_fwd.hpp:85
char * buffer()
Get buffer pointer.
Definition: unpack.hpp:1187
const size_t COUNTER_SIZE
Definition: unpack_decl.hpp:40
unpack_user(unpack_reference_func f=MSGPACK_NULLPTR, void *user_data=MSGPACK_NULLPTR, unpack_limit const &limit=unpack_limit())
Definition: unpack.hpp:46
msgpack::object_handle unpack(const char *data, std::size_t len, std::size_t &off, bool &referenced, unpack_reference_func f, void *user_data, unpack_limit const &limit)
Unpack msgpack::object from a buffer.
Definition: unpack.hpp:1383
Definition: cpp_config_decl.hpp:56
msgpack::object_map map
Definition: object_fwd.hpp:86
std::atomic< unsigned int > const & get_count(void *buffer)
Definition: unpack.hpp:263
msgpack::object_str str
Definition: object_fwd.hpp:87
std::size_t array() const
Definition: unpack_decl.hpp:102
Object class that corresponding to MessagePack format object.
Definition: object_fwd.hpp:75
Definition: unpack_exception.hpp:70
std::size_t message_size() const
Get message size.
Definition: unpack.hpp:1306
msgpack::type::object_type type
Definition: object_fwd.hpp:92
msgpack::object const & map_key() const
Definition: unpack.hpp:217
Definition: unpack.hpp:207
#define MSGPACK_API_VERSION_NAMESPACE(ns)
Definition: versioning.hpp:58
Definition: object_fwd_decl.hpp:41
void reset()
Definition: unpack.hpp:1299
unpacker & operator=(unpacker &&other)
Definition: unpack.hpp:1093
Definition: parse_return.hpp:24
msgpack::object key
Definition: object.hpp:30
Definition: parse_return.hpp:25
uint32_t size
Definition: object_fwd.hpp:28
Definition: object_fwd_decl.hpp:42
msgpack::object const & data() const
Definition: unpack.hpp:320
msgpack::unique_ptr< msgpack::zone > & zone()
Get unique_ptr reference of zone.
Definition: object.hpp:77
Definition: object_fwd_decl.hpp:34
void set_count(std::size_t count)
Definition: unpack.hpp:213
uint32_t size
Definition: object_fwd.hpp:33
void unpack_nil(msgpack::object &o)
Definition: unpack.hpp:102
msgpack::zone & zone()
Definition: unpack.hpp:51
void unpack_map_item(msgpack::object &c, msgpack::object const &k, msgpack::object const &v)
Definition: unpack.hpp:146
void decr_count(void *buffer)
Definition: unpack.hpp:235
Unpacking class for a stream deserialization.
Definition: unpack.hpp:847
Definition: unpack_decl.hpp:180
std::size_t buffer_capacity() const
Get buffer capacity.
Definition: unpack.hpp:1192
void unpack_int32(int32_t d, msgpack::object &o)
Definition: unpack.hpp:88
unpack_limit & limit()
Definition: unpack.hpp:58
#define MSGPACK_NULLPTR
Definition: cpp_config_decl.hpp:35
void unpack_array_item(msgpack::object &c, msgpack::object const &o)
Definition: unpack.hpp:124
T & move(T &t)
void * user_data() const
Definition: unpack.hpp:56
void set_zone(msgpack::zone &zone)
Definition: unpack.hpp:52
msgpack::object_ext ext
Definition: object_fwd.hpp:89
Definition: unpack.hpp:270
std::size_t decr_count()
Definition: unpack.hpp:214
msgpack::object const & obj() const
Definition: unpack.hpp:209
void skip_nonparsed_buffer(std::size_t size)
Skip the specified size of non-parsed buffer.
Definition: unpack.hpp:1326
#define MSGPACK_DEPRECATED(msg)
Definition: cpp_config.hpp:132
The class holds object and zone.
Definition: object.hpp:43
unpack_user & user()
Definition: unpack.hpp:325
bool referenced() const
Definition: unpack.hpp:53
Definition: object_fwd_decl.hpp:31
uint64_t u64
Definition: object_fwd.hpp:78
msgpack::object_bin bin
Definition: object_fwd.hpp:88
#define MSGPACK_UNPACKER_RESERVE_SIZE
Definition: unpack_decl.hpp:47