unpack.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * MessagePack for C unpacking routine
  3. *
  4. * Copyright (C) 2008-2009 FURUHASHI Sadayuki
  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_UNPACKER_H
  11. #define MSGPACK_UNPACKER_H
  12. #include "zone.h"
  13. #include "object.h"
  14. #include <string.h>
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. /**
  19. * @defgroup msgpack_unpack Deserializer
  20. * @ingroup msgpack
  21. * @{
  22. */
  23. typedef struct msgpack_unpacked {
  24. msgpack_zone* zone;
  25. msgpack_object data;
  26. } msgpack_unpacked;
  27. typedef enum {
  28. MSGPACK_UNPACK_SUCCESS = 2,
  29. MSGPACK_UNPACK_EXTRA_BYTES = 1,
  30. MSGPACK_UNPACK_CONTINUE = 0,
  31. MSGPACK_UNPACK_PARSE_ERROR = -1,
  32. MSGPACK_UNPACK_NOMEM_ERROR = -2
  33. } msgpack_unpack_return;
  34. MSGPACK_DLLEXPORT
  35. msgpack_unpack_return
  36. msgpack_unpack_next(msgpack_unpacked* result,
  37. const char* data, size_t len, size_t* off);
  38. /** @} */
  39. /**
  40. * @defgroup msgpack_unpacker Streaming deserializer
  41. * @ingroup msgpack
  42. * @{
  43. */
  44. typedef struct msgpack_unpacker {
  45. char* buffer;
  46. size_t used;
  47. size_t free;
  48. size_t off;
  49. size_t parsed;
  50. msgpack_zone* z;
  51. size_t initial_buffer_size;
  52. void* ctx;
  53. } msgpack_unpacker;
  54. #ifndef MSGPACK_UNPACKER_INIT_BUFFER_SIZE
  55. #define MSGPACK_UNPACKER_INIT_BUFFER_SIZE (64*1024)
  56. #endif
  57. /**
  58. * Initializes a streaming deserializer.
  59. * The initialized deserializer must be destroyed by msgpack_unpacker_destroy(msgpack_unpacker*).
  60. */
  61. MSGPACK_DLLEXPORT
  62. bool msgpack_unpacker_init(msgpack_unpacker* mpac, size_t initial_buffer_size);
  63. /**
  64. * Destroys a streaming deserializer initialized by msgpack_unpacker_init(msgpack_unpacker*, size_t).
  65. */
  66. MSGPACK_DLLEXPORT
  67. void msgpack_unpacker_destroy(msgpack_unpacker* mpac);
  68. /**
  69. * Creates a streaming deserializer.
  70. * The created deserializer must be destroyed by msgpack_unpacker_free(msgpack_unpacker*).
  71. */
  72. MSGPACK_DLLEXPORT
  73. msgpack_unpacker* msgpack_unpacker_new(size_t initial_buffer_size);
  74. /**
  75. * Frees a streaming deserializer created by msgpack_unpacker_new(size_t).
  76. */
  77. MSGPACK_DLLEXPORT
  78. void msgpack_unpacker_free(msgpack_unpacker* mpac);
  79. #ifndef MSGPACK_UNPACKER_RESERVE_SIZE
  80. #define MSGPACK_UNPACKER_RESERVE_SIZE (32*1024)
  81. #endif
  82. /**
  83. * Reserves free space of the internal buffer.
  84. * Use this function to fill the internal buffer with
  85. * msgpack_unpacker_buffer(msgpack_unpacker*),
  86. * msgpack_unpacker_buffer_capacity(const msgpack_unpacker*) and
  87. * msgpack_unpacker_buffer_consumed(msgpack_unpacker*).
  88. */
  89. static inline bool msgpack_unpacker_reserve_buffer(msgpack_unpacker* mpac, size_t size);
  90. /**
  91. * Gets pointer to the free space of the internal buffer.
  92. * Use this function to fill the internal buffer with
  93. * msgpack_unpacker_reserve_buffer(msgpack_unpacker*, size_t),
  94. * msgpack_unpacker_buffer_capacity(const msgpack_unpacker*) and
  95. * msgpack_unpacker_buffer_consumed(msgpack_unpacker*).
  96. */
  97. static inline char* msgpack_unpacker_buffer(msgpack_unpacker* mpac);
  98. /**
  99. * Gets size of the free space of the internal buffer.
  100. * Use this function to fill the internal buffer with
  101. * msgpack_unpacker_reserve_buffer(msgpack_unpacker*, size_t),
  102. * msgpack_unpacker_buffer(const msgpack_unpacker*) and
  103. * msgpack_unpacker_buffer_consumed(msgpack_unpacker*).
  104. */
  105. static inline size_t msgpack_unpacker_buffer_capacity(const msgpack_unpacker* mpac);
  106. /**
  107. * Notifies the deserializer that the internal buffer filled.
  108. * Use this function to fill the internal buffer with
  109. * msgpack_unpacker_reserve_buffer(msgpack_unpacker*, size_t),
  110. * msgpack_unpacker_buffer(msgpack_unpacker*) and
  111. * msgpack_unpacker_buffer_capacity(const msgpack_unpacker*).
  112. */
  113. static inline void msgpack_unpacker_buffer_consumed(msgpack_unpacker* mpac, size_t size);
  114. /**
  115. * Deserializes one object.
  116. * Returns true if it successes. Otherwise false is returned.
  117. * @param pac pointer to an initialized msgpack_unpacked object.
  118. */
  119. MSGPACK_DLLEXPORT
  120. msgpack_unpack_return msgpack_unpacker_next(msgpack_unpacker* mpac, msgpack_unpacked* pac);
  121. /**
  122. * Deserializes one object and set the number of parsed bytes involved.
  123. * Returns true if it successes. Otherwise false is returned.
  124. * @param mpac pointer to an initialized msgpack_unpacker object.
  125. * @param result pointer to an initialized msgpack_unpacked object.
  126. * @param p_bytes pointer to variable that will be set with the number of parsed bytes.
  127. */
  128. MSGPACK_DLLEXPORT
  129. msgpack_unpack_return msgpack_unpacker_next_with_size(msgpack_unpacker* mpac,
  130. msgpack_unpacked* result,
  131. size_t *p_bytes);
  132. /**
  133. * Initializes a msgpack_unpacked object.
  134. * The initialized object must be destroyed by msgpack_unpacked_destroy(msgpack_unpacker*).
  135. * Use the object with msgpack_unpacker_next(msgpack_unpacker*, msgpack_unpacked*) or
  136. * msgpack_unpack_next(msgpack_unpacked*, const char*, size_t, size_t*).
  137. */
  138. static inline void msgpack_unpacked_init(msgpack_unpacked* result);
  139. /**
  140. * Destroys a streaming deserializer initialized by msgpack_unpacked().
  141. */
  142. static inline void msgpack_unpacked_destroy(msgpack_unpacked* result);
  143. /**
  144. * Releases the memory zone from msgpack_unpacked object.
  145. * The released zone must be freed by msgpack_zone_free(msgpack_zone*).
  146. */
  147. static inline msgpack_zone* msgpack_unpacked_release_zone(msgpack_unpacked* result);
  148. MSGPACK_DLLEXPORT
  149. int msgpack_unpacker_execute(msgpack_unpacker* mpac);
  150. MSGPACK_DLLEXPORT
  151. msgpack_object msgpack_unpacker_data(msgpack_unpacker* mpac);
  152. MSGPACK_DLLEXPORT
  153. msgpack_zone* msgpack_unpacker_release_zone(msgpack_unpacker* mpac);
  154. MSGPACK_DLLEXPORT
  155. void msgpack_unpacker_reset_zone(msgpack_unpacker* mpac);
  156. MSGPACK_DLLEXPORT
  157. void msgpack_unpacker_reset(msgpack_unpacker* mpac);
  158. static inline size_t msgpack_unpacker_message_size(const msgpack_unpacker* mpac);
  159. /** @} */
  160. // obsolete
  161. MSGPACK_DLLEXPORT
  162. msgpack_unpack_return
  163. msgpack_unpack(const char* data, size_t len, size_t* off,
  164. msgpack_zone* result_zone, msgpack_object* result);
  165. static inline size_t msgpack_unpacker_parsed_size(const msgpack_unpacker* mpac);
  166. MSGPACK_DLLEXPORT
  167. bool msgpack_unpacker_flush_zone(msgpack_unpacker* mpac);
  168. MSGPACK_DLLEXPORT
  169. bool msgpack_unpacker_expand_buffer(msgpack_unpacker* mpac, size_t size);
  170. static inline bool msgpack_unpacker_reserve_buffer(msgpack_unpacker* mpac, size_t size)
  171. {
  172. if(mpac->free >= size) { return true; }
  173. return msgpack_unpacker_expand_buffer(mpac, size);
  174. }
  175. static inline char* msgpack_unpacker_buffer(msgpack_unpacker* mpac)
  176. {
  177. return mpac->buffer + mpac->used;
  178. }
  179. static inline size_t msgpack_unpacker_buffer_capacity(const msgpack_unpacker* mpac)
  180. {
  181. return mpac->free;
  182. }
  183. static inline void msgpack_unpacker_buffer_consumed(msgpack_unpacker* mpac, size_t size)
  184. {
  185. mpac->used += size;
  186. mpac->free -= size;
  187. }
  188. static inline size_t msgpack_unpacker_message_size(const msgpack_unpacker* mpac)
  189. {
  190. return mpac->parsed - mpac->off + mpac->used;
  191. }
  192. static inline size_t msgpack_unpacker_parsed_size(const msgpack_unpacker* mpac)
  193. {
  194. return mpac->parsed;
  195. }
  196. static inline void msgpack_unpacked_init(msgpack_unpacked* result)
  197. {
  198. memset(result, 0, sizeof(msgpack_unpacked));
  199. }
  200. static inline void msgpack_unpacked_destroy(msgpack_unpacked* result)
  201. {
  202. if(result->zone != NULL) {
  203. msgpack_zone_free(result->zone);
  204. result->zone = NULL;
  205. memset(&result->data, 0, sizeof(msgpack_object));
  206. }
  207. }
  208. static inline msgpack_zone* msgpack_unpacked_release_zone(msgpack_unpacked* result)
  209. {
  210. if(result->zone != NULL) {
  211. msgpack_zone* z = result->zone;
  212. result->zone = NULL;
  213. return z;
  214. }
  215. return NULL;
  216. }
  217. #ifdef __cplusplus
  218. }
  219. #endif
  220. #endif /* msgpack/unpack.h */