unpack.h
Go to the documentation of this file.
/*
* MessagePack for C unpacking routine
*
* Copyright (C) 2008-2009 FURUHASHI Sadayuki
*
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*/
#ifndef MSGPACK_UNPACKER_H
#define MSGPACK_UNPACKER_H
#include "zone.h"
#include "object.h"
#include <string.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct msgpack_unpacked {
msgpack_zone* zone;
msgpack_object data;
} msgpack_unpacked;
typedef enum {
MSGPACK_UNPACK_SUCCESS = 2,
MSGPACK_UNPACK_EXTRA_BYTES = 1,
MSGPACK_UNPACK_CONTINUE = 0,
MSGPACK_UNPACK_PARSE_ERROR = -1,
MSGPACK_UNPACK_NOMEM_ERROR = -2
} msgpack_unpack_return;
MSGPACK_DLLEXPORT
msgpack_unpack_return
msgpack_unpack_next(msgpack_unpacked* result,
const char* data, size_t len, size_t* off);
typedef struct msgpack_unpacker {
char* buffer;
size_t used;
size_t free;
size_t off;
size_t parsed;
msgpack_zone* z;
size_t initial_buffer_size;
void* ctx;
} msgpack_unpacker;
#ifndef MSGPACK_UNPACKER_INIT_BUFFER_SIZE
#define MSGPACK_UNPACKER_INIT_BUFFER_SIZE (64*1024)
#endif
MSGPACK_DLLEXPORT
bool msgpack_unpacker_init(msgpack_unpacker* mpac, size_t initial_buffer_size);
MSGPACK_DLLEXPORT
void msgpack_unpacker_destroy(msgpack_unpacker* mpac);
MSGPACK_DLLEXPORT
msgpack_unpacker* msgpack_unpacker_new(size_t initial_buffer_size);
MSGPACK_DLLEXPORT
void msgpack_unpacker_free(msgpack_unpacker* mpac);
#ifndef MSGPACK_UNPACKER_RESERVE_SIZE
#define MSGPACK_UNPACKER_RESERVE_SIZE (32*1024)
#endif
static inline bool msgpack_unpacker_reserve_buffer(msgpack_unpacker* mpac, size_t size);
static inline char* msgpack_unpacker_buffer(msgpack_unpacker* mpac);
static inline size_t msgpack_unpacker_buffer_capacity(const msgpack_unpacker* mpac);
static inline void msgpack_unpacker_buffer_consumed(msgpack_unpacker* mpac, size_t size);
MSGPACK_DLLEXPORT
msgpack_unpack_return msgpack_unpacker_next(msgpack_unpacker* mpac, msgpack_unpacked* pac);
MSGPACK_DLLEXPORT
msgpack_unpack_return msgpack_unpacker_next_with_size(msgpack_unpacker* mpac,
msgpack_unpacked* result,
size_t *p_bytes);
static inline void msgpack_unpacked_init(msgpack_unpacked* result);
static inline void msgpack_unpacked_destroy(msgpack_unpacked* result);
static inline msgpack_zone* msgpack_unpacked_release_zone(msgpack_unpacked* result);
MSGPACK_DLLEXPORT
int msgpack_unpacker_execute(msgpack_unpacker* mpac);
MSGPACK_DLLEXPORT
msgpack_object msgpack_unpacker_data(msgpack_unpacker* mpac);
MSGPACK_DLLEXPORT
msgpack_zone* msgpack_unpacker_release_zone(msgpack_unpacker* mpac);
MSGPACK_DLLEXPORT
void msgpack_unpacker_reset_zone(msgpack_unpacker* mpac);
MSGPACK_DLLEXPORT
void msgpack_unpacker_reset(msgpack_unpacker* mpac);
static inline size_t msgpack_unpacker_message_size(const msgpack_unpacker* mpac);
// obsolete
MSGPACK_DLLEXPORT
msgpack_unpack_return
msgpack_unpack(const char* data, size_t len, size_t* off,
msgpack_zone* result_zone, msgpack_object* result);
static inline size_t msgpack_unpacker_parsed_size(const msgpack_unpacker* mpac);
MSGPACK_DLLEXPORT
bool msgpack_unpacker_flush_zone(msgpack_unpacker* mpac);
MSGPACK_DLLEXPORT
bool msgpack_unpacker_expand_buffer(msgpack_unpacker* mpac, size_t size);
static inline bool msgpack_unpacker_reserve_buffer(msgpack_unpacker* mpac, size_t size)
{
if(mpac->free >= size) { return true; }
return msgpack_unpacker_expand_buffer(mpac, size);
}
static inline char* msgpack_unpacker_buffer(msgpack_unpacker* mpac)
{
return mpac->buffer + mpac->used;
}
static inline size_t msgpack_unpacker_buffer_capacity(const msgpack_unpacker* mpac)
{
return mpac->free;
}
static inline void msgpack_unpacker_buffer_consumed(msgpack_unpacker* mpac, size_t size)
{
mpac->used += size;
mpac->free -= size;
}
static inline size_t msgpack_unpacker_message_size(const msgpack_unpacker* mpac)
{
return mpac->parsed - mpac->off + mpac->used;
}
static inline size_t msgpack_unpacker_parsed_size(const msgpack_unpacker* mpac)
{
return mpac->parsed;
}
static inline void msgpack_unpacked_init(msgpack_unpacked* result)
{
memset(result, 0, sizeof(msgpack_unpacked));
}
static inline void msgpack_unpacked_destroy(msgpack_unpacked* result)
{
if(result->zone != NULL) {
msgpack_zone_free(result->zone);
result->zone = NULL;
memset(&result->data, 0, sizeof(msgpack_object));
}
}
static inline msgpack_zone* msgpack_unpacked_release_zone(msgpack_unpacked* result)
{
if(result->zone != NULL) {
msgpack_zone* z = result->zone;
result->zone = NULL;
return z;
}
return NULL;
}
#ifdef __cplusplus
}
#endif
#endif /* msgpack/unpack.h */
msgpack_unpacked::data
msgpack_object data
Definition: unpack.h:30
MSGPACK_UNPACK_SUCCESS
Definition: unpack.h:34
off
const char size_t size_t * off
Definition: unpack_template.h:90
msgpack_unpack_next
MSGPACK_DLLEXPORT msgpack_unpack_return msgpack_unpack_next(msgpack_unpacked *result, const char *data, size_t len, size_t *off)
MSGPACK_UNPACK_PARSE_ERROR
Definition: unpack.h:37
msgpack_unpack
MSGPACK_DLLEXPORT msgpack_unpack_return msgpack_unpack(const char *data, size_t len, size_t *off, msgpack_zone *result_zone, msgpack_object *result)
msgpack_unpacker_data
MSGPACK_DLLEXPORT msgpack_object msgpack_unpacker_data(msgpack_unpacker *mpac)
MSGPACK_DLLEXPORT
#define MSGPACK_DLLEXPORT
Definition: sysdep.h:42
msgpack_unpacker_free
MSGPACK_DLLEXPORT void msgpack_unpacker_free(msgpack_unpacker *mpac)
Frees a streaming deserializer created by msgpack_unpacker_new(size_t).
msgpack_unpacked
Definition: unpack.h:28
msgpack_unpacker::ctx
void * ctx
Definition: unpack.h:64
msgpack_unpacker_new
MSGPACK_DLLEXPORT msgpack_unpacker * msgpack_unpacker_new(size_t initial_buffer_size)
Creates a streaming deserializer.
msgpack_unpacker
struct msgpack_unpacker msgpack_unpacker
msgpack_unpack_return
msgpack_unpack_return
Definition: unpack.h:33
msgpack_unpacker_next
MSGPACK_DLLEXPORT msgpack_unpack_return msgpack_unpacker_next(msgpack_unpacker *mpac, msgpack_unpacked *pac)
Deserializes one object.
msgpack_unpacker::used
size_t used
Definition: unpack.h:58
msgpack_unpacker::parsed
size_t parsed
Definition: unpack.h:61
msgpack_unpacker_reset_zone
MSGPACK_DLLEXPORT void msgpack_unpacker_reset_zone(msgpack_unpacker *mpac)
msgpack_unpacker_init
MSGPACK_DLLEXPORT bool msgpack_unpacker_init(msgpack_unpacker *mpac, size_t initial_buffer_size)
Initializes a streaming deserializer.
msgpack_unpacker::free
size_t free
Definition: unpack.h:59
msgpack_unpacker_flush_zone
MSGPACK_DLLEXPORT bool msgpack_unpacker_flush_zone(msgpack_unpacker *mpac)
msgpack_unpacker_release_zone
MSGPACK_DLLEXPORT msgpack_zone * msgpack_unpacker_release_zone(msgpack_unpacker *mpac)
msgpack_unpacker::z
msgpack_zone * z
Definition: unpack.h:62
msgpack_unpacker_reset
MSGPACK_DLLEXPORT void msgpack_unpacker_reset(msgpack_unpacker *mpac)
msgpack_unpacker::off
size_t off
Definition: unpack.h:60
msgpack_unpacker
Definition: unpack.h:56
MSGPACK_UNPACK_NOMEM_ERROR
Definition: unpack.h:38
msgpack_object
Definition: object.h:90
msgpack_unpacked::zone
msgpack_zone * zone
Definition: unpack.h:29
zone.h
msgpack_unpacker_execute
MSGPACK_DLLEXPORT int msgpack_unpacker_execute(msgpack_unpacker *mpac)
msgpack_unpacker_destroy
MSGPACK_DLLEXPORT void msgpack_unpacker_destroy(msgpack_unpacker *mpac)
Destroys a streaming deserializer initialized by msgpack_unpacker_init(msgpack_unpacker*, size_t).
object.h
msgpack_unpacker_next_with_size
MSGPACK_DLLEXPORT msgpack_unpack_return msgpack_unpacker_next_with_size(msgpack_unpacker *mpac, msgpack_unpacked *result, size_t *p_bytes)
Deserializes one object and set the number of parsed bytes involved.
msgpack_unpacked
struct msgpack_unpacked msgpack_unpacked
msgpack_unpacker_expand_buffer
MSGPACK_DLLEXPORT bool msgpack_unpacker_expand_buffer(msgpack_unpacker *mpac, size_t size)
msgpack_zone_free
MSGPACK_DLLEXPORT void msgpack_zone_free(msgpack_zone *zone)
msgpack_zone
Definition: zone.h:46
msgpack_unpacker::initial_buffer_size
size_t initial_buffer_size
Definition: unpack.h:63
MSGPACK_UNPACK_CONTINUE
Definition: unpack.h:36
len
const char size_t len
Definition: unpack_template.h:89
MSGPACK_UNPACK_EXTRA_BYTES
Definition: unpack.h:35
msgpack_unpacker::buffer
char * buffer
Definition: unpack.h:57