unpack_decl.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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_DECL_HPP
  11. #define MSGPACK_V1_UNPACK_DECL_HPP
  12. #include "msgpack/versioning.hpp"
  13. #include "msgpack/unpack_define.h"
  14. #include "msgpack/object.hpp"
  15. #include "msgpack/zone.hpp"
  16. #include "msgpack/cpp_config.hpp"
  17. #include "msgpack/sysdep.h"
  18. #include "msgpack/parse_return.hpp"
  19. #include <memory>
  20. #include <stdexcept>
  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. #ifdef _msgpack_atomic_counter_header
  31. #include _msgpack_atomic_counter_header
  32. #endif
  33. const size_t COUNTER_SIZE = sizeof(_msgpack_atomic_counter_t);
  34. #ifndef MSGPACK_UNPACKER_INIT_BUFFER_SIZE
  35. #define MSGPACK_UNPACKER_INIT_BUFFER_SIZE (64*1024)
  36. #endif
  37. #ifndef MSGPACK_UNPACKER_RESERVE_SIZE
  38. #define MSGPACK_UNPACKER_RESERVE_SIZE (32*1024)
  39. #endif
  40. // backward compatibility
  41. #ifndef MSGPACK_UNPACKER_DEFAULT_INITIAL_BUFFER_SIZE
  42. #define MSGPACK_UNPACKER_DEFAULT_INITIAL_BUFFER_SIZE MSGPACK_UNPACKER_INIT_BUFFER_SIZE
  43. #endif
  44. namespace msgpack {
  45. /// @cond
  46. MSGPACK_API_VERSION_NAMESPACE(v1) {
  47. /// @endcond
  48. /// The type of reference or copy judging function.
  49. /**
  50. * @param type msgpack data type.
  51. * @param size msgpack data size.
  52. * @param user_data The user_data that is set by msgpack::unpack functions.
  53. *
  54. * @return If the data should be referenced, then return true, otherwise (should be copied) false.
  55. *
  56. * This function is called when unpacking STR, BIN, or EXT.
  57. *
  58. */
  59. typedef bool (*unpack_reference_func)(msgpack::type::object_type type, std::size_t size, void* user_data);
  60. struct unpack_error;
  61. struct parse_error;
  62. struct insufficient_bytes;
  63. struct size_overflow;
  64. struct array_size_overflow;
  65. struct map_size_overflow;
  66. struct str_size_overflow;
  67. struct bin_size_overflow;
  68. struct ext_size_overflow;
  69. struct depth_size_overflow;
  70. class unpack_limit {
  71. public:
  72. unpack_limit(
  73. std::size_t array = 0xffffffff,
  74. std::size_t map = 0xffffffff,
  75. std::size_t str = 0xffffffff,
  76. std::size_t bin = 0xffffffff,
  77. std::size_t ext = 0xffffffff,
  78. std::size_t depth = 0xffffffff)
  79. :array_(array),
  80. map_(map),
  81. str_(str),
  82. bin_(bin),
  83. ext_(ext),
  84. depth_(depth) {}
  85. std::size_t array() const { return array_; }
  86. std::size_t map() const { return map_; }
  87. std::size_t str() const { return str_; }
  88. std::size_t bin() const { return bin_; }
  89. std::size_t ext() const { return ext_; }
  90. std::size_t depth() const { return depth_; }
  91. private:
  92. std::size_t array_;
  93. std::size_t map_;
  94. std::size_t str_;
  95. std::size_t bin_;
  96. std::size_t ext_;
  97. std::size_t depth_;
  98. };
  99. namespace detail {
  100. class unpack_user;
  101. void unpack_uint8(uint8_t d, msgpack::object& o);
  102. void unpack_uint16(uint16_t d, msgpack::object& o);
  103. void unpack_uint32(uint32_t d, msgpack::object& o);
  104. void unpack_uint64(uint64_t d, msgpack::object& o);
  105. void unpack_int8(int8_t d, msgpack::object& o);
  106. void unpack_int16(int16_t d, msgpack::object& o);
  107. void unpack_int32(int32_t d, msgpack::object& o);
  108. void unpack_int64(int64_t d, msgpack::object& o);
  109. void unpack_float(float d, msgpack::object& o);
  110. void unpack_double(double d, msgpack::object& o);
  111. void unpack_nil(msgpack::object& o);
  112. void unpack_true(msgpack::object& o);
  113. void unpack_false(msgpack::object& o);
  114. struct unpack_array;
  115. void unpack_array_item(msgpack::object& c, msgpack::object const& o);
  116. struct unpack_map;
  117. void unpack_map_item(msgpack::object& c, msgpack::object const& k, msgpack::object const& v);
  118. void unpack_str(unpack_user& u, const char* p, uint32_t l, msgpack::object& o);
  119. void unpack_bin(unpack_user& u, const char* p, uint32_t l, msgpack::object& o);
  120. void unpack_ext(unpack_user& u, const char* p, std::size_t l, msgpack::object& o);
  121. class unpack_stack;
  122. void init_count(void* buffer);
  123. void decr_count(void* buffer);
  124. void incr_count(void* buffer);
  125. #if defined(MSGPACK_USE_CPP03)
  126. _msgpack_atomic_counter_t get_count(void* buffer);
  127. #else // defined(MSGPACK_USE_CPP03)
  128. std::atomic<unsigned int> const& get_count(void* buffer);
  129. #endif // defined(MSGPACK_USE_CPP03)
  130. struct fix_tag {
  131. char f1[65]; // FIXME unique size is required. or use is_same meta function.
  132. };
  133. template <typename T>
  134. struct value;
  135. template <typename T>
  136. typename msgpack::enable_if<sizeof(T) == sizeof(fix_tag)>::type load(uint32_t& dst, const char* n);
  137. template <typename T>
  138. typename msgpack::enable_if<sizeof(T) == 1>::type load(T& dst, const char* n);
  139. template <typename T>
  140. typename msgpack::enable_if<sizeof(T) == 2>::type load(T& dst, const char* n);
  141. template <typename T>
  142. typename msgpack::enable_if<sizeof(T) == 4>::type load(T& dst, const char* n);
  143. template <typename T>
  144. typename msgpack::enable_if<sizeof(T) == 8>::type load(T& dst, const char* n);
  145. class context;
  146. } // detail
  147. typedef object_handle unpacked;
  148. /// Unpacking class for a stream deserialization.
  149. class unpacker;
  150. /// Unpack msgpack::object from a buffer.
  151. /**
  152. * @param data The pointer to the buffer.
  153. * @param len The length of the buffer.
  154. * @param off The offset position of the buffer. It is read and overwritten.
  155. * @param referenced If the unpacked object contains reference of the buffer, then set as true, otherwise false.
  156. * @param f A judging function that msgpack::object refer to the buffer.
  157. * @param user_data This parameter is passed to f.
  158. * @param limit The size limit information of msgpack::object.
  159. *
  160. * @return object_handle that contains unpacked data.
  161. *
  162. */
  163. object_handle unpack(
  164. const char* data, std::size_t len, std::size_t& off, bool& referenced,
  165. unpack_reference_func f = MSGPACK_NULLPTR, void* user_data = MSGPACK_NULLPTR, unpack_limit const& limit = unpack_limit());
  166. /// Unpack msgpack::object from a buffer.
  167. /**
  168. * @param data The pointer to the buffer.
  169. * @param len The length of the buffer.
  170. * @param off The offset position of the buffer. It is read and overwritten.
  171. * @param f A judging function that msgpack::object refer to the buffer.
  172. * @param user_data This parameter is passed to f.
  173. * @param limit The size limit information of msgpack::object.
  174. *
  175. * @return object_handle that contains unpacked data.
  176. *
  177. */
  178. object_handle unpack(
  179. const char* data, std::size_t len, std::size_t& off,
  180. unpack_reference_func f = MSGPACK_NULLPTR, void* user_data = MSGPACK_NULLPTR, unpack_limit const& limit = unpack_limit());
  181. /// Unpack msgpack::object from a buffer.
  182. /**
  183. * @param data The pointer to the buffer.
  184. * @param len The length of the buffer.
  185. * @param referenced If the unpacked object contains reference of the buffer, then set as true, otherwise false.
  186. * @param f A judging function that msgpack::object refer to the buffer.
  187. * @param user_data This parameter is passed to f.
  188. * @param limit The size limit information of msgpack::object.
  189. *
  190. * @return object_handle that contains unpacked data.
  191. *
  192. */
  193. object_handle unpack(
  194. const char* data, std::size_t len, bool& referenced,
  195. unpack_reference_func f = MSGPACK_NULLPTR, void* user_data = MSGPACK_NULLPTR, unpack_limit const& limit = unpack_limit());
  196. /// Unpack msgpack::object from a buffer.
  197. /**
  198. * @param data The pointer to the buffer.
  199. * @param len The length of the buffer.
  200. * @param f A judging function that msgpack::object refer to the buffer.
  201. * @param user_data This parameter is passed to f.
  202. * @param limit The size limit information of msgpack::object.
  203. *
  204. * @return object_handle that contains unpacked data.
  205. *
  206. */
  207. object_handle unpack(
  208. const char* data, std::size_t len,
  209. unpack_reference_func f = MSGPACK_NULLPTR, void* user_data = MSGPACK_NULLPTR, unpack_limit const& limit = unpack_limit());
  210. /// Unpack msgpack::object from a buffer.
  211. /**
  212. * @param result The object_handle that contains unpacked data.
  213. * @param data The pointer to the buffer.
  214. * @param len The length of the buffer.
  215. * @param off The offset position of the buffer. It is read and overwritten.
  216. * @param referenced If the unpacked object contains reference of the buffer, then set as true, otherwise false.
  217. * @param f A judging function that msgpack::object refer to the buffer.
  218. * @param user_data This parameter is passed to f.
  219. * @param limit The size limit information of msgpack::object.
  220. *
  221. *
  222. */
  223. void unpack(
  224. object_handle& result,
  225. const char* data, std::size_t len, std::size_t& off, bool& referenced,
  226. unpack_reference_func f = MSGPACK_NULLPTR, void* user_data = MSGPACK_NULLPTR, unpack_limit const& limit = unpack_limit());
  227. /// Unpack msgpack::object from a buffer.
  228. /**
  229. * @param result The object_handle that contains unpacked data.
  230. * @param data The pointer to the buffer.
  231. * @param len The length of the buffer.
  232. * @param off The offset position of the buffer. It is read and overwritten.
  233. * @param f A judging function that msgpack::object refer to the buffer.
  234. * @param user_data This parameter is passed to f.
  235. * @param limit The size limit information of msgpack::object.
  236. *
  237. *
  238. */
  239. void unpack(
  240. object_handle& result,
  241. const char* data, std::size_t len, std::size_t& off,
  242. unpack_reference_func f = MSGPACK_NULLPTR, void* user_data = MSGPACK_NULLPTR, unpack_limit const& limit = unpack_limit());
  243. /// Unpack msgpack::object from a buffer.
  244. /**
  245. * @param result The object_handle that contains unpacked data.
  246. * @param data The pointer to the buffer.
  247. * @param len The length of the buffer.
  248. * @param referenced If the unpacked object contains reference of the buffer, then set as true, otherwise false.
  249. * @param f A judging function that msgpack::object refer to the buffer.
  250. * @param user_data This parameter is passed to f.
  251. * @param limit The size limit information of msgpack::object.
  252. *
  253. *
  254. */
  255. void unpack(
  256. object_handle& result,
  257. const char* data, std::size_t len, bool& referenced,
  258. unpack_reference_func f = MSGPACK_NULLPTR, void* user_data = MSGPACK_NULLPTR, unpack_limit const& limit = unpack_limit());
  259. /// Unpack msgpack::object from a buffer.
  260. /**
  261. * @param result The object_handle that contains unpacked data.
  262. * @param data The pointer to the buffer.
  263. * @param len The length of the buffer.
  264. * @param f A judging function that msgpack::object refer to the buffer.
  265. * @param user_data This parameter is passed to f.
  266. * @param limit The size limit information of msgpack::object.
  267. *
  268. *
  269. */
  270. void unpack(
  271. object_handle& result,
  272. const char* data, std::size_t len,
  273. unpack_reference_func f = MSGPACK_NULLPTR, void* user_data = MSGPACK_NULLPTR, unpack_limit const& limit = unpack_limit());
  274. /// Unpack msgpack::object from a buffer.
  275. /**
  276. * @param z The msgpack::zone that is used as a memory of unpacked msgpack objects.
  277. * @param data The pointer to the buffer.
  278. * @param len The length of the buffer.
  279. * @param off The offset position of the buffer. It is read and overwritten.
  280. * @param referenced If the unpacked object contains reference of the buffer, then set as true, otherwise false.
  281. * @param f A judging function that msgpack::object refer to the buffer.
  282. * @param user_data This parameter is passed to f.
  283. * @param limit The size limit information of msgpack::object.
  284. *
  285. * @return msgpack::object that contains unpacked data.
  286. *
  287. */
  288. msgpack::object unpack(
  289. msgpack::zone& z,
  290. const char* data, std::size_t len, std::size_t& off, bool& referenced,
  291. unpack_reference_func f = MSGPACK_NULLPTR, void* user_data = MSGPACK_NULLPTR, unpack_limit const& limit = unpack_limit());
  292. /// Unpack msgpack::object from a buffer.
  293. /**
  294. * @param z The msgpack::zone that is used as a memory of unpacked msgpack objects.
  295. * @param data The pointer to the buffer.
  296. * @param len The length of the buffer.
  297. * @param off The offset position of the buffer. It is read and overwritten.
  298. * @param f A judging function that msgpack::object refer to the buffer.
  299. * @param user_data This parameter is passed to f.
  300. * @param limit The size limit information of msgpack::object.
  301. *
  302. * @return msgpack::object that contains unpacked data.
  303. *
  304. */
  305. msgpack::object unpack(
  306. msgpack::zone& z,
  307. const char* data, std::size_t len, std::size_t& off,
  308. unpack_reference_func f = MSGPACK_NULLPTR, void* user_data = MSGPACK_NULLPTR, unpack_limit const& limit = unpack_limit());
  309. /// Unpack msgpack::object from a buffer.
  310. /**
  311. * @param z The msgpack::zone that is used as a memory of unpacked msgpack objects.
  312. * @param data The pointer to the buffer.
  313. * @param len The length of the buffer.
  314. * @param referenced If the unpacked object contains reference of the buffer, then set as true, otherwise false.
  315. * @param f A judging function that msgpack::object refer to the buffer.
  316. * @param user_data This parameter is passed to f.
  317. * @param limit The size limit information of msgpack::object.
  318. *
  319. * @return msgpack::object that contains unpacked data.
  320. *
  321. */
  322. msgpack::object unpack(
  323. msgpack::zone& z,
  324. const char* data, std::size_t len, bool& referenced,
  325. unpack_reference_func f = MSGPACK_NULLPTR, void* user_data = MSGPACK_NULLPTR, unpack_limit const& limit = unpack_limit());
  326. /// Unpack msgpack::object from a buffer.
  327. /**
  328. * @param z The msgpack::zone that is used as a memory of unpacked msgpack objects.
  329. * @param data The pointer to the buffer.
  330. * @param len The length of the buffer.
  331. * @param f A judging function that msgpack::object refer to the buffer.
  332. * @param user_data This parameter is passed to f.
  333. * @param limit The size limit information of msgpack::object.
  334. *
  335. * @return msgpack::object that contains unpacked data.
  336. *
  337. */
  338. msgpack::object unpack(
  339. msgpack::zone& z,
  340. const char* data, std::size_t len,
  341. unpack_reference_func f = MSGPACK_NULLPTR, void* user_data = MSGPACK_NULLPTR, unpack_limit const& limit = unpack_limit());
  342. /// Unpack msgpack::object from a buffer. [obsolete]
  343. /**
  344. * @param result The object_handle that contains unpacked data.
  345. * @param data The pointer to the buffer.
  346. * @param len The length of the buffer.
  347. * @param off The offset position of the buffer. It is read and overwritten.
  348. * @param referenced If the unpacked object contains reference of the buffer, then set as true, otherwise false.
  349. * @param f A judging function that msgpack::object refer to the buffer.
  350. * @param user_data This parameter is passed to f.
  351. * @param limit The size limit information of msgpack::object.
  352. *
  353. * This function is obsolete. Use the reference inteface version of unpack functions instead of the pointer interface version.
  354. */
  355. void unpack(
  356. object_handle* result,
  357. const char* data, std::size_t len, std::size_t* off = MSGPACK_NULLPTR, bool* referenced = MSGPACK_NULLPTR,
  358. unpack_reference_func f = MSGPACK_NULLPTR, void* user_data = MSGPACK_NULLPTR, unpack_limit const& limit = unpack_limit());
  359. namespace detail {
  360. parse_return
  361. unpack_imp(const char* data, std::size_t len, std::size_t& off,
  362. msgpack::zone& result_zone, msgpack::object& result, bool& referenced,
  363. unpack_reference_func f, void* user_data,
  364. unpack_limit const& limit);
  365. } // detail
  366. /// @cond
  367. } // MSGPACK_API_VERSION_NAMESPACE(v1)
  368. /// @endcond
  369. } // namespace msgpack
  370. #endif // MSGPACK_V1_UNPACK_DECL_HPP