MessagePack for C
unpack.h
Go to the documentation of this file.
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 
13 #include "zone.h"
14 #include "object.h"
15 #include <string.h>
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 
21 
28 typedef struct msgpack_unpacked {
32 
33 typedef enum {
40 
41 
45  const char* data, size_t len, size_t* off);
46 
56 typedef struct msgpack_unpacker {
57  char* buffer;
58  size_t used;
59  size_t free;
60  size_t off;
61  size_t parsed;
64  void* ctx;
66 
67 
68 #ifndef MSGPACK_UNPACKER_INIT_BUFFER_SIZE
69 #define MSGPACK_UNPACKER_INIT_BUFFER_SIZE (64*1024)
70 #endif
71 
77 bool msgpack_unpacker_init(msgpack_unpacker* mpac, size_t initial_buffer_size);
78 
84 
85 
91 msgpack_unpacker* msgpack_unpacker_new(size_t initial_buffer_size);
92 
98 
99 
100 #ifndef MSGPACK_UNPACKER_RESERVE_SIZE
101 #define MSGPACK_UNPACKER_RESERVE_SIZE (32*1024)
102 #endif
103 
111 static inline bool msgpack_unpacker_reserve_buffer(msgpack_unpacker* mpac, size_t size);
112 
120 static inline char* msgpack_unpacker_buffer(msgpack_unpacker* mpac);
121 
129 static inline size_t msgpack_unpacker_buffer_capacity(const msgpack_unpacker* mpac);
130 
138 static inline void msgpack_unpacker_buffer_consumed(msgpack_unpacker* mpac, size_t size);
139 
140 
148 
155 static inline void msgpack_unpacked_init(msgpack_unpacked* result);
156 
160 static inline void msgpack_unpacked_destroy(msgpack_unpacked* result);
161 
166 static inline msgpack_zone* msgpack_unpacked_release_zone(msgpack_unpacked* result);
167 
168 
171 
174 
177 
180 
183 
184 static inline size_t msgpack_unpacker_message_size(const msgpack_unpacker* mpac);
185 
186 
190 // obsolete
193 msgpack_unpack(const char* data, size_t len, size_t* off,
194  msgpack_zone* result_zone, msgpack_object* result);
195 
196 
197 
198 
199 static inline size_t msgpack_unpacker_parsed_size(const msgpack_unpacker* mpac);
200 
203 
205 bool msgpack_unpacker_expand_buffer(msgpack_unpacker* mpac, size_t size);
206 
207 static inline bool msgpack_unpacker_reserve_buffer(msgpack_unpacker* mpac, size_t size)
208 {
209  if(mpac->free >= size) { return true; }
210  return msgpack_unpacker_expand_buffer(mpac, size);
211 }
212 
213 static inline char* msgpack_unpacker_buffer(msgpack_unpacker* mpac)
214 {
215  return mpac->buffer + mpac->used;
216 }
217 
218 static inline size_t msgpack_unpacker_buffer_capacity(const msgpack_unpacker* mpac)
219 {
220  return mpac->free;
221 }
222 
223 static inline void msgpack_unpacker_buffer_consumed(msgpack_unpacker* mpac, size_t size)
224 {
225  mpac->used += size;
226  mpac->free -= size;
227 }
228 
229 static inline size_t msgpack_unpacker_message_size(const msgpack_unpacker* mpac)
230 {
231  return mpac->parsed - mpac->off + mpac->used;
232 }
233 
234 static inline size_t msgpack_unpacker_parsed_size(const msgpack_unpacker* mpac)
235 {
236  return mpac->parsed;
237 }
238 
239 
240 static inline void msgpack_unpacked_init(msgpack_unpacked* result)
241 {
242  memset(result, 0, sizeof(msgpack_unpacked));
243 }
244 
245 static inline void msgpack_unpacked_destroy(msgpack_unpacked* result)
246 {
247  if(result->zone != NULL) {
248  msgpack_zone_free(result->zone);
249  result->zone = NULL;
250  memset(&result->data, 0, sizeof(msgpack_object));
251  }
252 }
253 
254 static inline msgpack_zone* msgpack_unpacked_release_zone(msgpack_unpacked* result)
255 {
256  if(result->zone != NULL) {
257  msgpack_zone* z = result->zone;
258  result->zone = NULL;
259  return z;
260  }
261  return NULL;
262 }
263 
264 
265 #ifdef __cplusplus
266 }
267 #endif
268 
269 #endif /* msgpack/unpack.h */
270 
msgpack_object data
Definition: unpack.h:30
Definition: unpack.h:34
const char size_t size_t * off
Definition: unpack_template.h:90
MSGPACK_DLLEXPORT msgpack_unpack_return msgpack_unpack_next(msgpack_unpacked *result, const char *data, size_t len, size_t *off)
Definition: unpack.h:37
MSGPACK_DLLEXPORT msgpack_unpack_return msgpack_unpack(const char *data, size_t len, size_t *off, msgpack_zone *result_zone, msgpack_object *result)
MSGPACK_DLLEXPORT msgpack_object msgpack_unpacker_data(msgpack_unpacker *mpac)
#define MSGPACK_DLLEXPORT
Definition: sysdep.h:37
MSGPACK_DLLEXPORT void msgpack_unpacker_free(msgpack_unpacker *mpac)
Frees a streaming deserializer created by msgpack_unpacker_new(size_t).
Definition: unpack.h:28
void * ctx
Definition: unpack.h:64
MSGPACK_DLLEXPORT msgpack_unpacker * msgpack_unpacker_new(size_t initial_buffer_size)
Creates a streaming deserializer.
struct msgpack_unpacker msgpack_unpacker
msgpack_unpack_return
Definition: unpack.h:33
MSGPACK_DLLEXPORT msgpack_unpack_return msgpack_unpacker_next(msgpack_unpacker *mpac, msgpack_unpacked *pac)
Deserializes one object.
size_t used
Definition: unpack.h:58
size_t parsed
Definition: unpack.h:61
MSGPACK_DLLEXPORT void msgpack_unpacker_reset_zone(msgpack_unpacker *mpac)
MSGPACK_DLLEXPORT bool msgpack_unpacker_init(msgpack_unpacker *mpac, size_t initial_buffer_size)
Initializes a streaming deserializer.
size_t free
Definition: unpack.h:59
MSGPACK_DLLEXPORT bool msgpack_unpacker_flush_zone(msgpack_unpacker *mpac)
MSGPACK_DLLEXPORT msgpack_zone * msgpack_unpacker_release_zone(msgpack_unpacker *mpac)
msgpack_zone * z
Definition: unpack.h:62
MSGPACK_DLLEXPORT void msgpack_unpacker_reset(msgpack_unpacker *mpac)
size_t off
Definition: unpack.h:60
Definition: unpack.h:56
Definition: unpack.h:38
Definition: object.h:88
msgpack_zone * zone
Definition: unpack.h:29
MSGPACK_DLLEXPORT int msgpack_unpacker_execute(msgpack_unpacker *mpac)
MSGPACK_DLLEXPORT void msgpack_unpacker_destroy(msgpack_unpacker *mpac)
Destroys a streaming deserializer initialized by msgpack_unpacker_init(msgpack_unpacker*, size_t).
struct msgpack_unpacked msgpack_unpacked
MSGPACK_DLLEXPORT bool msgpack_unpacker_expand_buffer(msgpack_unpacker *mpac, size_t size)
MSGPACK_DLLEXPORT void msgpack_zone_free(msgpack_zone *zone)
Definition: zone.h:46
size_t initial_buffer_size
Definition: unpack.h:63
Definition: unpack.h:36
const char size_t len
Definition: unpack_template.h:89
Definition: unpack.h:35
char * buffer
Definition: unpack.h:57