unpack.hpp 48 KB

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