unpack.hpp 49 KB

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