MessagePack for C++
parse.hpp
Go to the documentation of this file.
1 //
2 // MessagePack for C++ deserializing routine
3 //
4 // Copyright (C) 2016-2017 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_V2_PARSE_HPP
11 #define MSGPACK_V2_PARSE_HPP
12 
13 #if MSGPACK_DEFAULT_API_VERSION >= 2
14 
15 #include <cstddef>
16 
17 #include <boost/assert.hpp>
18 
20 #include "msgpack/parse_return.hpp"
22 #include "msgpack/unpack_decl.hpp"
23 
24 namespace msgpack {
25 
29 
30 namespace detail {
31 
32 using v1::detail::fix_tag;
33 using v1::detail::value;
34 using v1::detail::load;
35 
36 template <typename VisitorHolder>
37 class context {
38 public:
39  context()
40  :m_trail(0), m_cs(MSGPACK_CS_HEADER)
41  {
42  }
43 
44  void init()
45  {
46  m_cs = MSGPACK_CS_HEADER;
47  m_trail = 0;
48  m_stack.clear();
49  holder().visitor().init();
50  }
51 
52  parse_return execute(const char* data, std::size_t len, std::size_t& off);
53 
54 private:
55  template <typename T>
56  static uint32_t next_cs(T p)
57  {
58  return static_cast<uint32_t>(*p) & 0x1f;
59  }
60 
61  VisitorHolder& holder() {
62  return static_cast<VisitorHolder&>(*this);
63  }
64 
65  template <typename T, typename StartVisitor, typename EndVisitor>
66  parse_return start_aggregate(
67  StartVisitor const& sv,
68  EndVisitor const& ev,
69  const char* load_pos,
70  std::size_t& off) {
71  typename value<T>::type size;
72  load<T>(size, load_pos);
73  ++m_current;
74  if (size == 0) {
75  if (!sv(size)) {
76  off = static_cast<std::size_t>(m_current - m_start);
77  return PARSE_STOP_VISITOR;
78  }
79  if (!ev()) {
80  off = static_cast<std::size_t>(m_current - m_start);
81  return PARSE_STOP_VISITOR;
82  }
83  parse_return ret = m_stack.consume(holder());
84  if (ret != PARSE_CONTINUE) {
85  off = static_cast<std::size_t>(m_current - m_start);
86  return ret;
87  }
88  }
89  else {
90  if (!sv(size)) {
91  off = static_cast<std::size_t>(m_current - m_start);
92  return PARSE_STOP_VISITOR;
93  }
94  parse_return ret = m_stack.push(holder(), sv.type(), static_cast<uint32_t>(size));
95  if (ret != PARSE_CONTINUE) {
96  off = static_cast<std::size_t>(m_current - m_start);
97  return ret;
98  }
99  }
100  m_cs = MSGPACK_CS_HEADER;
101  return PARSE_CONTINUE;
102  }
103 
104  parse_return after_visit_proc(bool visit_result, std::size_t& off) {
105  ++m_current;
106  if (!visit_result) {
107  off = static_cast<std::size_t>(m_current - m_start);
108  return PARSE_STOP_VISITOR;
109  }
110  parse_return ret = m_stack.consume(holder());
111  if (ret != PARSE_CONTINUE) {
112  off = static_cast<std::size_t>(m_current - m_start);
113  }
114  m_cs = MSGPACK_CS_HEADER;
115  return ret;
116  }
117 
118  struct array_sv {
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);
122  }
123  msgpack_container_type type() const { return MSGPACK_CT_ARRAY_ITEM; }
124  private:
125  VisitorHolder& m_visitor_holder;
126  };
127  struct array_ev {
128  array_ev(VisitorHolder& visitor_holder):m_visitor_holder(visitor_holder) {}
129  bool operator()() const {
130  return m_visitor_holder.visitor().end_array();
131  }
132  private:
133  VisitorHolder& m_visitor_holder;
134  };
135  struct map_sv {
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);
139  }
140  msgpack_container_type type() const { return MSGPACK_CT_MAP_KEY; }
141  private:
142  VisitorHolder& m_visitor_holder;
143  };
144  struct map_ev {
145  map_ev(VisitorHolder& visitor_holder):m_visitor_holder(visitor_holder) {}
146  bool operator()() const {
147  return m_visitor_holder.visitor().end_map();
148  }
149  private:
150  VisitorHolder& m_visitor_holder;
151  };
152 
153  struct unpack_stack {
154  struct stack_elem {
155  stack_elem(msgpack_container_type type, uint32_t rest):m_type(type), m_rest(rest) {}
156  msgpack_container_type m_type;
157  uint32_t m_rest;
158  };
159  unpack_stack() {
160  m_stack.reserve(MSGPACK_EMBED_STACK_SIZE);
161  }
162  parse_return push(VisitorHolder& visitor_holder, msgpack_container_type type, uint32_t rest) {
163  m_stack.push_back(stack_elem(type, rest));
164  switch (type) {
166  return visitor_holder.visitor().start_array_item() ? PARSE_CONTINUE : PARSE_STOP_VISITOR;
167  case MSGPACK_CT_MAP_KEY:
168  return visitor_holder.visitor().start_map_key() ? PARSE_CONTINUE : PARSE_STOP_VISITOR;
170  BOOST_ASSERT(0);
171  return PARSE_STOP_VISITOR;
172  }
173  BOOST_ASSERT(0);
174  return PARSE_STOP_VISITOR;
175  }
176  parse_return consume(VisitorHolder& visitor_holder) {
177  while (!m_stack.empty()) {
178  stack_elem& e = m_stack.back();
179  switch (e.m_type) {
181  if (!visitor_holder.visitor().end_array_item()) return PARSE_STOP_VISITOR;
182  if (--e.m_rest == 0) {
183  m_stack.pop_back();
184  if (!visitor_holder.visitor().end_array()) return PARSE_STOP_VISITOR;
185  }
186  else {
187  if (!visitor_holder.visitor().start_array_item()) return PARSE_STOP_VISITOR;
188  return PARSE_CONTINUE;
189  }
190  break;
191  case MSGPACK_CT_MAP_KEY:
192  if (!visitor_holder.visitor().end_map_key()) return PARSE_STOP_VISITOR;
193  if (!visitor_holder.visitor().start_map_value()) return PARSE_STOP_VISITOR;
194  e.m_type = MSGPACK_CT_MAP_VALUE;
195  return PARSE_CONTINUE;
197  if (!visitor_holder.visitor().end_map_value()) return PARSE_STOP_VISITOR;
198  if (--e.m_rest == 0) {
199  m_stack.pop_back();
200  if (!visitor_holder.visitor().end_map()) return PARSE_STOP_VISITOR;
201  }
202  else {
203  e.m_type = MSGPACK_CT_MAP_KEY;
204  if (!visitor_holder.visitor().start_map_key()) return PARSE_STOP_VISITOR;
205  return PARSE_CONTINUE;
206  }
207  break;
208  }
209  }
210  return PARSE_SUCCESS;
211  }
212  bool empty() const { return m_stack.empty(); }
213  void clear() { m_stack.clear(); }
214  private:
215  std::vector<stack_elem> m_stack;
216  };
217 
218  char const* m_start;
219  char const* m_current;
220 
221  std::size_t m_trail;
222  uint32_t m_cs;
223  uint32_t m_num_elements;
224  unpack_stack m_stack;
225 };
226 
227 template <std::size_t N>
228 inline void check_ext_size(std::size_t /*size*/) {
229 }
230 
231 template <>
232 inline void check_ext_size<4>(std::size_t size) {
233  if (size == 0xffffffff) throw msgpack::ext_size_overflow("ext size overflow");
234 }
235 
236 template <typename VisitorHolder>
237 inline parse_return context<VisitorHolder>::execute(const char* data, std::size_t len, std::size_t& off)
238 {
239  BOOST_ASSERT(len >= off);
240 
241  m_start = data;
242  m_current = data + off;
243  const char* const pe = data + len;
244  const char* n = MSGPACK_NULLPTR;
245 
246  if(m_current == pe) {
247  off = static_cast<std::size_t>(m_current - m_start);
248  return PARSE_CONTINUE;
249  }
250  bool fixed_trail_again = false;
251  do {
252  if (m_cs == MSGPACK_CS_HEADER) {
253  fixed_trail_again = false;
254  int selector = *reinterpret_cast<const unsigned char*>(m_current);
255  if (0x00 <= selector && selector <= 0x7f) { // Positive Fixnum
256  uint8_t tmp = *reinterpret_cast<const uint8_t*>(m_current);
257  bool visret = holder().visitor().visit_positive_integer(tmp);
258  parse_return upr = after_visit_proc(visret, off);
259  if (upr != PARSE_CONTINUE) return upr;
260  } else if(0xe0 <= selector && selector <= 0xff) { // Negative Fixnum
261  int8_t tmp = *reinterpret_cast<const int8_t*>(m_current);
262  bool visret = holder().visitor().visit_negative_integer(tmp);
263  parse_return upr = after_visit_proc(visret, off);
264  if (upr != PARSE_CONTINUE) return upr;
265  } else if (0xc4 <= selector && selector <= 0xdf) {
266  const uint32_t trail[] = {
267  1, // bin 8 0xc4
268  2, // bin 16 0xc5
269  4, // bin 32 0xc6
270  1, // ext 8 0xc7
271  2, // ext 16 0xc8
272  4, // ext 32 0xc9
273  4, // float 32 0xca
274  8, // float 64 0xcb
275  1, // uint 8 0xcc
276  2, // uint 16 0xcd
277  4, // uint 32 0xce
278  8, // uint 64 0xcf
279  1, // int 8 0xd0
280  2, // int 16 0xd1
281  4, // int 32 0xd2
282  8, // int 64 0xd3
283  2, // fixext 1 0xd4
284  3, // fixext 2 0xd5
285  5, // fixext 4 0xd6
286  9, // fixext 8 0xd7
287  17,// fixext 16 0xd8
288  1, // str 8 0xd9
289  2, // str 16 0xda
290  4, // str 32 0xdb
291  2, // array 16 0xdc
292  4, // array 32 0xdd
293  2, // map 16 0xde
294  4, // map 32 0xdf
295  };
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) { // FixStr
300  m_trail = static_cast<uint32_t>(*m_current) & 0x1f;
301  if(m_trail == 0) {
302  bool visret = holder().visitor().visit_str(n, static_cast<uint32_t>(m_trail));
303  parse_return upr = after_visit_proc(visret, off);
304  if (upr != PARSE_CONTINUE) return upr;
305  }
306  else {
307  m_cs = MSGPACK_ACS_STR_VALUE;
308  fixed_trail_again = true;
309  }
310  } else if(0x90 <= selector && selector <= 0x9f) { // FixArray
311  parse_return ret = start_aggregate<fix_tag>(array_sv(holder()), array_ev(holder()), m_current, off);
312  if (ret != PARSE_CONTINUE) return ret;
313  } else if(0x80 <= selector && selector <= 0x8f) { // FixMap
314  parse_return ret = start_aggregate<fix_tag>(map_sv(holder()), map_ev(holder()), m_current, off);
315  if (ret != PARSE_CONTINUE) return ret;
316  } else if(selector == 0xc2) { // false
317  bool visret = holder().visitor().visit_boolean(false);
318  parse_return upr = after_visit_proc(visret, off);
319  if (upr != PARSE_CONTINUE) return upr;
320  } else if(selector == 0xc3) { // true
321  bool visret = holder().visitor().visit_boolean(true);
322  parse_return upr = after_visit_proc(visret, off);
323  if (upr != PARSE_CONTINUE) return upr;
324  } else if(selector == 0xc0) { // nil
325  bool visret = holder().visitor().visit_nil();
326  parse_return upr = after_visit_proc(visret, off);
327  if (upr != PARSE_CONTINUE) return upr;
328  } else {
329  off = static_cast<std::size_t>(m_current - m_start);
330  holder().visitor().parse_error(off - 1, off);
331  return PARSE_PARSE_ERROR;
332  }
333  // end MSGPACK_CS_HEADER
334  }
335  if (m_cs != MSGPACK_CS_HEADER || fixed_trail_again) {
336  if (fixed_trail_again) {
337  ++m_current;
338  fixed_trail_again = false;
339  }
340  if(static_cast<std::size_t>(pe - m_current) < m_trail) {
341  off = static_cast<std::size_t>(m_current - m_start);
342  return PARSE_CONTINUE;
343  }
344  n = m_current;
345  m_current += m_trail - 1;
346  switch(m_cs) {
347  //case MSGPACK_CS_
348  //case MSGPACK_CS_
349  case MSGPACK_CS_FLOAT: {
350  union { uint32_t i; float f; } mem;
351  load<uint32_t>(mem.i, n);
352  bool visret = holder().visitor().visit_float32(mem.f);
353  parse_return upr = after_visit_proc(visret, off);
354  if (upr != PARSE_CONTINUE) return upr;
355  } break;
356  case MSGPACK_CS_DOUBLE: {
357  union { uint64_t i; double f; } mem;
358  load<uint64_t>(mem.i, n);
359 #if defined(TARGET_OS_IPHONE)
360  // ok
361 #elif defined(__arm__) && !(__ARM_EABI__) // arm-oabi
362  // https://github.com/msgpack/msgpack-perl/pull/1
363  mem.i = (mem.i & 0xFFFFFFFFUL) << 32UL | (mem.i >> 32UL);
364 #endif
365  bool visret = holder().visitor().visit_float64(mem.f);
366  parse_return upr = after_visit_proc(visret, off);
367  if (upr != PARSE_CONTINUE) return upr;
368  } break;
369  case MSGPACK_CS_UINT_8: {
370  uint8_t tmp;
371  load<uint8_t>(tmp, n);
372  bool visret = holder().visitor().visit_positive_integer(tmp);
373  parse_return upr = after_visit_proc(visret, off);
374  if (upr != PARSE_CONTINUE) return upr;
375  } break;
376  case MSGPACK_CS_UINT_16: {
377  uint16_t tmp;
378  load<uint16_t>(tmp, n);
379  bool visret = holder().visitor().visit_positive_integer(tmp);
380  parse_return upr = after_visit_proc(visret, off);
381  if (upr != PARSE_CONTINUE) return upr;
382  } break;
383  case MSGPACK_CS_UINT_32: {
384  uint32_t tmp;
385  load<uint32_t>(tmp, n);
386  bool visret = holder().visitor().visit_positive_integer(tmp);
387  parse_return upr = after_visit_proc(visret, off);
388  if (upr != PARSE_CONTINUE) return upr;
389  } break;
390  case MSGPACK_CS_UINT_64: {
391  uint64_t tmp;
392  load<uint64_t>(tmp, n);
393  bool visret = holder().visitor().visit_positive_integer(tmp);
394  parse_return upr = after_visit_proc(visret, off);
395  if (upr != PARSE_CONTINUE) return upr;
396  } break;
397  case MSGPACK_CS_INT_8: {
398  int8_t tmp;
399  load<int8_t>(tmp, n);
400  bool visret = holder().visitor().visit_negative_integer(tmp);
401  parse_return upr = after_visit_proc(visret, off);
402  if (upr != PARSE_CONTINUE) return upr;
403  } break;
404  case MSGPACK_CS_INT_16: {
405  int16_t tmp;
406  load<int16_t>(tmp, n);
407  bool visret = holder().visitor().visit_negative_integer(tmp);
408  parse_return upr = after_visit_proc(visret, off);
409  if (upr != PARSE_CONTINUE) return upr;
410  } break;
411  case MSGPACK_CS_INT_32: {
412  int32_t tmp;
413  load<int32_t>(tmp, n);
414  bool visret = holder().visitor().visit_negative_integer(tmp);
415  parse_return upr = after_visit_proc(visret, off);
416  if (upr != PARSE_CONTINUE) return upr;
417  } break;
418  case MSGPACK_CS_INT_64: {
419  int64_t tmp;
420  load<int64_t>(tmp, n);
421  bool visret = holder().visitor().visit_negative_integer(tmp);
422  parse_return upr = after_visit_proc(visret, off);
423  if (upr != PARSE_CONTINUE) return upr;
424  } break;
425  case MSGPACK_CS_FIXEXT_1: {
426  bool visret = holder().visitor().visit_ext(n, 1+1);
427  parse_return upr = after_visit_proc(visret, off);
428  if (upr != PARSE_CONTINUE) return upr;
429  } break;
430  case MSGPACK_CS_FIXEXT_2: {
431  bool visret = holder().visitor().visit_ext(n, 2+1);
432  parse_return upr = after_visit_proc(visret, off);
433  if (upr != PARSE_CONTINUE) return upr;
434  } break;
435  case MSGPACK_CS_FIXEXT_4: {
436  bool visret = holder().visitor().visit_ext(n, 4+1);
437  parse_return upr = after_visit_proc(visret, off);
438  if (upr != PARSE_CONTINUE) return upr;
439  } break;
440  case MSGPACK_CS_FIXEXT_8: {
441  bool visret = holder().visitor().visit_ext(n, 8+1);
442  parse_return upr = after_visit_proc(visret, off);
443  if (upr != PARSE_CONTINUE) return upr;
444  } break;
445  case MSGPACK_CS_FIXEXT_16: {
446  bool visret = holder().visitor().visit_ext(n, 16+1);
447  parse_return upr = after_visit_proc(visret, off);
448  if (upr != PARSE_CONTINUE) return upr;
449  } break;
450  case MSGPACK_CS_STR_8: {
451  uint8_t tmp;
452  load<uint8_t>(tmp, n);
453  m_trail = tmp;
454  if(m_trail == 0) {
455  bool visret = holder().visitor().visit_str(n, static_cast<uint32_t>(m_trail));
456  parse_return upr = after_visit_proc(visret, off);
457  if (upr != PARSE_CONTINUE) return upr;
458  }
459  else {
460  m_cs = MSGPACK_ACS_STR_VALUE;
461  fixed_trail_again = true;
462  }
463  } break;
464  case MSGPACK_CS_BIN_8: {
465  uint8_t tmp;
466  load<uint8_t>(tmp, n);
467  m_trail = tmp;
468  if(m_trail == 0) {
469  bool visret = holder().visitor().visit_bin(n, static_cast<uint32_t>(m_trail));
470  parse_return upr = after_visit_proc(visret, off);
471  if (upr != PARSE_CONTINUE) return upr;
472  }
473  else {
474  m_cs = MSGPACK_ACS_BIN_VALUE;
475  fixed_trail_again = true;
476  }
477  } break;
478  case MSGPACK_CS_EXT_8: {
479  uint8_t tmp;
480  load<uint8_t>(tmp, n);
481  m_trail = tmp + 1;
482  if(m_trail == 0) {
483  bool visret = holder().visitor().visit_ext(n, static_cast<uint32_t>(m_trail));
484  parse_return upr = after_visit_proc(visret, off);
485  if (upr != PARSE_CONTINUE) return upr;
486  }
487  else {
488  m_cs = MSGPACK_ACS_EXT_VALUE;
489  fixed_trail_again = true;
490  }
491  } break;
492  case MSGPACK_CS_STR_16: {
493  uint16_t tmp;
494  load<uint16_t>(tmp, n);
495  m_trail = tmp;
496  if(m_trail == 0) {
497  bool visret = holder().visitor().visit_str(n, static_cast<uint32_t>(m_trail));
498  parse_return upr = after_visit_proc(visret, off);
499  if (upr != PARSE_CONTINUE) return upr;
500  }
501  else {
502  m_cs = MSGPACK_ACS_STR_VALUE;
503  fixed_trail_again = true;
504  }
505  } break;
506  case MSGPACK_CS_BIN_16: {
507  uint16_t tmp;
508  load<uint16_t>(tmp, n);
509  m_trail = tmp;
510  if(m_trail == 0) {
511  bool visret = holder().visitor().visit_bin(n, static_cast<uint32_t>(m_trail));
512  parse_return upr = after_visit_proc(visret, off);
513  if (upr != PARSE_CONTINUE) return upr;
514  }
515  else {
516  m_cs = MSGPACK_ACS_BIN_VALUE;
517  fixed_trail_again = true;
518  }
519  } break;
520  case MSGPACK_CS_EXT_16: {
521  uint16_t tmp;
522  load<uint16_t>(tmp, n);
523  m_trail = tmp + 1;
524  if(m_trail == 0) {
525  bool visret = holder().visitor().visit_ext(n, static_cast<uint32_t>(m_trail));
526  parse_return upr = after_visit_proc(visret, off);
527  if (upr != PARSE_CONTINUE) return upr;
528  }
529  else {
530  m_cs = MSGPACK_ACS_EXT_VALUE;
531  fixed_trail_again = true;
532  }
533  } break;
534  case MSGPACK_CS_STR_32: {
535  uint32_t tmp;
536  load<uint32_t>(tmp, n);
537  m_trail = tmp;
538  if(m_trail == 0) {
539  bool visret = holder().visitor().visit_str(n, static_cast<uint32_t>(m_trail));
540  parse_return upr = after_visit_proc(visret, off);
541  if (upr != PARSE_CONTINUE) return upr;
542  }
543  else {
544  m_cs = MSGPACK_ACS_STR_VALUE;
545  fixed_trail_again = true;
546  }
547  } break;
548  case MSGPACK_CS_BIN_32: {
549  uint32_t tmp;
550  load<uint32_t>(tmp, n);
551  m_trail = tmp;
552  if(m_trail == 0) {
553  bool visret = holder().visitor().visit_bin(n, static_cast<uint32_t>(m_trail));
554  parse_return upr = after_visit_proc(visret, off);
555  if (upr != PARSE_CONTINUE) return upr;
556  }
557  else {
558  m_cs = MSGPACK_ACS_BIN_VALUE;
559  fixed_trail_again = true;
560  }
561  } break;
562  case MSGPACK_CS_EXT_32: {
563  uint32_t tmp;
564  load<uint32_t>(tmp, n);
565  check_ext_size<sizeof(std::size_t)>(tmp);
566  m_trail = tmp;
567  ++m_trail;
568  if(m_trail == 0) {
569  bool visret = holder().visitor().visit_ext(n, static_cast<uint32_t>(m_trail));
570  parse_return upr = after_visit_proc(visret, off);
571  if (upr != PARSE_CONTINUE) return upr;
572  }
573  else {
574  m_cs = MSGPACK_ACS_EXT_VALUE;
575  fixed_trail_again = true;
576  }
577  } break;
578  case MSGPACK_ACS_STR_VALUE: {
579  bool visret = holder().visitor().visit_str(n, static_cast<uint32_t>(m_trail));
580  parse_return upr = after_visit_proc(visret, off);
581  if (upr != PARSE_CONTINUE) return upr;
582  } break;
583  case MSGPACK_ACS_BIN_VALUE: {
584  bool visret = holder().visitor().visit_bin(n, static_cast<uint32_t>(m_trail));
585  parse_return upr = after_visit_proc(visret, off);
586  if (upr != PARSE_CONTINUE) return upr;
587  } break;
588  case MSGPACK_ACS_EXT_VALUE: {
589  bool visret = holder().visitor().visit_ext(n, static_cast<uint32_t>(m_trail));
590  parse_return upr = after_visit_proc(visret, off);
591  if (upr != PARSE_CONTINUE) return upr;
592  } break;
593  case MSGPACK_CS_ARRAY_16: {
594  parse_return ret = start_aggregate<uint16_t>(array_sv(holder()), array_ev(holder()), n, off);
595  if (ret != PARSE_CONTINUE) return ret;
596 
597  } break;
598  case MSGPACK_CS_ARRAY_32: {
599  parse_return ret = start_aggregate<uint32_t>(array_sv(holder()), array_ev(holder()), n, off);
600  if (ret != PARSE_CONTINUE) return ret;
601  } break;
602  case MSGPACK_CS_MAP_16: {
603  parse_return ret = start_aggregate<uint16_t>(map_sv(holder()), map_ev(holder()), n, off);
604  if (ret != PARSE_CONTINUE) return ret;
605  } break;
606  case MSGPACK_CS_MAP_32: {
607  parse_return ret = start_aggregate<uint32_t>(map_sv(holder()), map_ev(holder()), n, off);
608  if (ret != PARSE_CONTINUE) return ret;
609  } break;
610  default:
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));
613  return PARSE_PARSE_ERROR;
614  }
615  }
616  } while(m_current != pe);
617 
618  off = static_cast<std::size_t>(m_current - m_start);
619  return PARSE_CONTINUE;
620 }
621 
622 } // detail
623 
624 
626 
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;
631 public:
633 
641  parser(ReferencedBufferHook& hook,
642  std::size_t initial_buffer_size = MSGPACK_UNPACKER_INIT_BUFFER_SIZE);
643 
644 #if !defined(MSGPACK_USE_CPP03)
645  parser(this_type&& other);
646  this_type& operator=(this_type&& other);
647 #endif // !defined(MSGPACK_USE_CPP03)
648 
649  ~parser();
650 
651 public:
653 
660  void reserve_buffer(std::size_t size = MSGPACK_UNPACKER_RESERVE_SIZE);
661 
663 
668  char* buffer();
669 
671 
677  std::size_t buffer_capacity() const;
678 
680 
689  void buffer_consumed(std::size_t size);
690 
692 
702  bool next();
703 
705 
708  std::size_t message_size() const;
709 
710 public:
712 
718  std::size_t parsed_size() const;
719 
721 
727  char* nonparsed_buffer();
728 
730 
736  std::size_t nonparsed_size() const;
737 
739 
746  void skip_nonparsed_buffer(std::size_t size);
747 
749 
753  void remove_nonparsed_buffer();
754 
755  void reset();
756 
757 protected:
758  char* get_raw_buffer() {
759  return m_buffer;
760  }
761 private:
762  void expand_buffer(std::size_t size);
763  parse_return execute_imp();
764 
765 private:
766  char* m_buffer;
767  std::size_t m_used;
768  std::size_t m_free;
769  std::size_t m_off;
770  std::size_t m_parsed;
771  std::size_t m_initial_buffer_size;
772  ReferencedBufferHook& m_referenced_buffer_hook;
773 
774 #if defined(MSGPACK_USE_CPP03)
775 private:
776  parser(const this_type&);
777  this_type& operator=(const this_type&);
778 #else // defined(MSGPACK_USE_CPP03)
779 public:
780  parser(const this_type&) = delete;
781  this_type& operator=(const this_type&) = delete;
782 #endif // defined(MSGPACK_USE_CPP03)
783 };
784 
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)
790 {
791  if(initial_buffer_size < COUNTER_SIZE) {
792  initial_buffer_size = COUNTER_SIZE;
793  }
794 
795  char* buffer = static_cast<char*>(::malloc(initial_buffer_size));
796  if(!buffer) {
797  throw std::bad_alloc();
798  }
799 
800  m_buffer = buffer;
801  m_used = COUNTER_SIZE;
802  m_free = initial_buffer_size - m_used;
803  m_off = COUNTER_SIZE;
804  m_parsed = 0;
805  m_initial_buffer_size = initial_buffer_size;
806 
807  detail::init_count(m_buffer);
808 }
809 
810 #if !defined(MSGPACK_USE_CPP03)
811 // Move constructor and move assignment operator
812 
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),
819  m_off(other.m_off),
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) {
823  other.m_buffer = MSGPACK_NULLPTR;
824  other.m_used = 0;
825  other.m_free = 0;
826  other.m_off = 0;
827  other.m_parsed = 0;
828 }
829 
830 template <typename VisitorHolder, typename ReferencedBufferHook>
831 inline parser<VisitorHolder, ReferencedBufferHook>& parser<VisitorHolder, ReferencedBufferHook>::operator=(this_type&& other) {
832  this->~parser();
833  new (this) this_type(std::move(other));
834  return *this;
835 }
836 
837 #endif // !defined(MSGPACK_USE_CPP03)
838 
839 
840 template <typename VisitorHolder, typename ReferencedBufferHook>
841 inline parser<VisitorHolder, ReferencedBufferHook>::~parser()
842 {
843  // These checks are required for move operations.
844  if (m_buffer) detail::decr_count(m_buffer);
845 }
846 
847 
848 template <typename VisitorHolder, typename ReferencedBufferHook>
849 inline void parser<VisitorHolder, ReferencedBufferHook>::reserve_buffer(std::size_t size)
850 {
851  if(m_free >= size) return;
852  expand_buffer(size);
853 }
854 
855 template <typename VisitorHolder, typename ReferencedBufferHook>
856 inline void parser<VisitorHolder, ReferencedBufferHook>::expand_buffer(std::size_t size)
857 {
858  if(m_used == m_off && detail::get_count(m_buffer) == 1
859  && !static_cast<VisitorHolder&>(*this).visitor().referenced()) {
860  // rewind buffer
861  m_free += m_used - COUNTER_SIZE;
862  m_used = COUNTER_SIZE;
863  m_off = COUNTER_SIZE;
864 
865  if(m_free >= size) return;
866  }
867 
868  if(m_off == COUNTER_SIZE) {
869  std::size_t next_size = (m_used + m_free) * 2; // include COUNTER_SIZE
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;
874  break;
875  }
876  next_size = tmp_next_size;
877  }
878 
879  char* tmp = static_cast<char*>(::realloc(m_buffer, next_size));
880  if(!tmp) {
881  throw std::bad_alloc();
882  }
883 
884  m_buffer = tmp;
885  m_free = next_size - m_used;
886 
887  } else {
888  std::size_t next_size = m_initial_buffer_size; // include COUNTER_SIZE
889  std::size_t not_parsed = m_used - m_off;
890  while(next_size < size + not_parsed + COUNTER_SIZE) {
891  std::size_t tmp_next_size = next_size * 2;
892  if (tmp_next_size <= next_size) {
893  next_size = size + not_parsed + COUNTER_SIZE;
894  break;
895  }
896  next_size = tmp_next_size;
897  }
898 
899  char* tmp = static_cast<char*>(::malloc(next_size));
900  if(!tmp) {
901  throw std::bad_alloc();
902  }
903 
904  detail::init_count(tmp);
905 
906  std::memcpy(tmp+COUNTER_SIZE, m_buffer + m_off, not_parsed);
907 
908  if(static_cast<VisitorHolder&>(*this).referenced()) {
909  try {
910  m_referenced_buffer_hook(m_buffer);
911  }
912  catch (...) {
913  ::free(tmp);
914  throw;
915  }
916  static_cast<VisitorHolder&>(*this).set_referenced(false);
917  } else {
918  detail::decr_count(m_buffer);
919  }
920 
921  m_buffer = tmp;
922  m_used = not_parsed + COUNTER_SIZE;
923  m_free = next_size - m_used;
924  m_off = COUNTER_SIZE;
925  }
926 }
927 
928 template <typename VisitorHolder, typename ReferencedBufferHook>
929 inline char* parser<VisitorHolder, ReferencedBufferHook>::buffer()
930 {
931  return m_buffer + m_used;
932 }
933 
934 template <typename VisitorHolder, typename ReferencedBufferHook>
935 inline std::size_t parser<VisitorHolder, ReferencedBufferHook>::buffer_capacity() const
936 {
937  return m_free;
938 }
939 
940 template <typename VisitorHolder, typename ReferencedBufferHook>
941 inline void parser<VisitorHolder, ReferencedBufferHook>::buffer_consumed(std::size_t size)
942 {
943  m_used += size;
944  m_free -= size;
945 }
946 
947 template <typename VisitorHolder, typename ReferencedBufferHook>
948  inline bool parser<VisitorHolder, ReferencedBufferHook>::next()
949 {
950  parse_return ret = execute_imp();
951  return ret == PARSE_SUCCESS;
952 }
953 
954 template <typename VisitorHolder, typename ReferencedBufferHook>
955 inline parse_return parser<VisitorHolder, ReferencedBufferHook>::execute_imp()
956 {
957  std::size_t off = m_off;
958  parse_return ret = context_type::execute(m_buffer, m_used, m_off);
959  if(m_off > off) {
960  m_parsed += m_off - off;
961  }
962  return ret;
963 }
964 
965 template <typename VisitorHolder, typename ReferencedBufferHook>
966 inline void parser<VisitorHolder, ReferencedBufferHook>::reset()
967 {
968  context_type::init();
969  // don't reset referenced flag
970  m_parsed = 0;
971 }
972 
973 template <typename VisitorHolder, typename ReferencedBufferHook>
974 inline std::size_t parser<VisitorHolder, ReferencedBufferHook>::message_size() const
975 {
976  return m_parsed - m_off + m_used;
977 }
978 
979 template <typename VisitorHolder, typename ReferencedBufferHook>
980 inline std::size_t parser<VisitorHolder, ReferencedBufferHook>::parsed_size() const
981 {
982  return m_parsed;
983 }
984 
985 template <typename VisitorHolder, typename ReferencedBufferHook>
986 inline char* parser<VisitorHolder, ReferencedBufferHook>::nonparsed_buffer()
987 {
988  return m_buffer + m_off;
989 }
990 
991 template <typename VisitorHolder, typename ReferencedBufferHook>
992 inline std::size_t parser<VisitorHolder, ReferencedBufferHook>::nonparsed_size() const
993 {
994  return m_used - m_off;
995 }
996 
997 template <typename VisitorHolder, typename ReferencedBufferHook>
998 inline void parser<VisitorHolder, ReferencedBufferHook>::skip_nonparsed_buffer(std::size_t size)
999 {
1000  m_off += size;
1001 }
1002 
1003 template <typename VisitorHolder, typename ReferencedBufferHook>
1004 inline void parser<VisitorHolder, ReferencedBufferHook>::remove_nonparsed_buffer()
1005 {
1006  m_used = m_off;
1007 }
1008 
1009 template <typename Visitor>
1010 inline bool parse(const char* data, size_t len, size_t& off, Visitor& v) {
1011  parse_return ret = msgpack::detail::parse_imp(data, len, off, v);
1012  return ret == PARSE_SUCCESS || ret == PARSE_EXTRA_BYTES;
1013 }
1014 
1015 template <typename Visitor>
1016 inline bool parse(const char* data, size_t len, Visitor& v) {
1017  std::size_t off = 0;
1018  return msgpack::parse(data, len, off, v);
1019 }
1020 
1021 namespace detail {
1022 
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);
1028  }
1029  Visitor& visitor() const { return m_visitor; }
1030  Visitor& m_visitor;
1031 };
1032 
1033 template <typename Visitor>
1034 inline parse_return
1035 parse_imp(const char* data, size_t len, size_t& off, Visitor& v) {
1036  std::size_t noff = off;
1037 
1038  if(len <= noff) {
1039  // FIXME
1040  v.insufficient_bytes(noff, noff);
1041  return PARSE_CONTINUE;
1042  }
1043  detail::parse_helper<Visitor> h(v);
1044  parse_return ret = h.execute(data, len, noff);
1045  switch (ret) {
1046  case PARSE_CONTINUE:
1047  off = noff;
1048  v.insufficient_bytes(noff - 1, noff);
1049  return ret;
1050  case PARSE_SUCCESS:
1051  off = noff;
1052  if(noff < len) {
1053  return PARSE_EXTRA_BYTES;
1054  }
1055  return ret;
1056  default:
1057  return ret;
1058  }
1059 }
1060 
1061 } // detail
1062 
1063 
1065 } // MSGPACK_API_VERSION_NAMESPACE(v2)
1067 
1068 } // namespace msgpack
1069 
1070 #endif // MSGPACK_DEFAULT_API_VERSION >= 2
1071 
1072 #endif // MSGPACK_V2_PARSE_HPP
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