2009-02-15 09:09:55 +00:00
|
|
|
/*
|
2009-02-15 09:10:02 +00:00
|
|
|
* MessagePack for C unpacking routine
|
2009-02-15 09:09:55 +00:00
|
|
|
*
|
2009-02-15 09:10:02 +00:00
|
|
|
* Copyright (C) 2008-2009 FURUHASHI Sadayuki
|
2009-02-15 09:09:55 +00:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2009-02-15 09:09:57 +00:00
|
|
|
#include "msgpack/unpack.h"
|
2009-02-15 09:09:57 +00:00
|
|
|
#include "msgpack/unpack_define.h"
|
2009-02-15 09:09:55 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2009-02-15 09:09:57 +00:00
|
|
|
|
|
|
|
#define msgpack_unpack_struct(name) \
|
2009-02-15 09:10:02 +00:00
|
|
|
struct template ## name
|
2009-02-15 09:09:57 +00:00
|
|
|
|
|
|
|
#define msgpack_unpack_func(ret, name) \
|
2009-02-15 09:10:02 +00:00
|
|
|
ret template_func ## name
|
2009-02-15 09:09:57 +00:00
|
|
|
|
|
|
|
#define msgpack_unpack_callback(name) \
|
2009-02-15 09:10:02 +00:00
|
|
|
template_callback ## name
|
2009-02-15 09:09:57 +00:00
|
|
|
|
|
|
|
#define msgpack_unpack_object void*
|
|
|
|
|
|
|
|
#define msgpack_unpack_user msgpack_unpack_t
|
|
|
|
|
|
|
|
|
|
|
|
struct template_context;
|
|
|
|
|
|
|
|
static void template_func_init(struct template_context* ctx);
|
|
|
|
|
|
|
|
static void* template_func_data(struct template_context* ctx);
|
|
|
|
|
|
|
|
static int template_func_execute(struct template_context* ctx,
|
|
|
|
const char* data, size_t len, size_t* off);
|
|
|
|
|
|
|
|
|
|
|
|
static inline void* template_callback_init(msgpack_unpack_t* x)
|
|
|
|
{ return NULL; }
|
|
|
|
|
|
|
|
static inline void* template_callback_uint8(msgpack_unpack_t* x, uint8_t d)
|
|
|
|
{ return x->callback.unpack_uint8(x->data, d); }
|
|
|
|
|
|
|
|
static inline void* template_callback_uint16(msgpack_unpack_t* x, uint16_t d)
|
|
|
|
{ return x->callback.unpack_uint16(x->data, d); }
|
|
|
|
|
|
|
|
static inline void* template_callback_uint32(msgpack_unpack_t* x, uint32_t d)
|
|
|
|
{ return x->callback.unpack_uint32(x->data, d); }
|
|
|
|
|
|
|
|
static inline void* template_callback_uint64(msgpack_unpack_t* x, uint64_t d)
|
|
|
|
{ return x->callback.unpack_uint64(x->data, d); }
|
|
|
|
|
|
|
|
static inline void* template_callback_int8(msgpack_unpack_t* x, int8_t d)
|
|
|
|
{ return x->callback.unpack_int8(x->data, d); }
|
|
|
|
|
|
|
|
static inline void* template_callback_int16(msgpack_unpack_t* x, int16_t d)
|
|
|
|
{ return x->callback.unpack_int16(x->data, d); }
|
|
|
|
|
|
|
|
static inline void* template_callback_int32(msgpack_unpack_t* x, int32_t d)
|
|
|
|
{ return x->callback.unpack_int32(x->data, d); }
|
|
|
|
|
|
|
|
static inline void* template_callback_int64(msgpack_unpack_t* x, int64_t d)
|
|
|
|
{ return x->callback.unpack_int64(x->data, d); }
|
|
|
|
|
|
|
|
static inline void* template_callback_float(msgpack_unpack_t* x, float d)
|
|
|
|
{ return x->callback.unpack_float(x->data, d); }
|
|
|
|
|
|
|
|
static inline void* template_callback_double(msgpack_unpack_t* x, double d)
|
|
|
|
{ return x->callback.unpack_double(x->data, d); }
|
|
|
|
|
|
|
|
static inline void* template_callback_nil(msgpack_unpack_t* x)
|
|
|
|
{ return x->callback.unpack_nil(x->data); }
|
|
|
|
|
|
|
|
static inline void* template_callback_true(msgpack_unpack_t* x)
|
|
|
|
{ return x->callback.unpack_true(x->data); }
|
|
|
|
|
|
|
|
static inline void* template_callback_false(msgpack_unpack_t* x)
|
|
|
|
{ return x->callback.unpack_false(x->data); }
|
|
|
|
|
|
|
|
static inline void* template_callback_array(msgpack_unpack_t* x, unsigned int n)
|
|
|
|
{ return x->callback.unpack_array(x->data, n); }
|
|
|
|
|
2009-02-15 09:09:58 +00:00
|
|
|
static inline void template_callback_array_item(msgpack_unpack_t* x, void** c, void* o)
|
|
|
|
{ x->callback.unpack_array_item(x->data, *c, o); }
|
2009-02-15 09:09:57 +00:00
|
|
|
|
|
|
|
static inline void* template_callback_map(msgpack_unpack_t* x, unsigned int n)
|
|
|
|
{ return x->callback.unpack_map(x->data, n); }
|
|
|
|
|
2009-02-15 09:09:58 +00:00
|
|
|
static inline void template_callback_map_item(msgpack_unpack_t* x, void** c, void* k, void* v)
|
|
|
|
{ x->callback.unpack_map_item(x->data, *c, k, v); }
|
2009-02-15 09:09:57 +00:00
|
|
|
|
|
|
|
static inline void* template_callback_raw(msgpack_unpack_t* x, const char* b, const char* p, unsigned int l)
|
|
|
|
{ return x->callback.unpack_raw(x->data, b, p, l); }
|
|
|
|
|
|
|
|
|
|
|
|
#include "msgpack/unpack_template.h"
|
|
|
|
|
|
|
|
|
2009-02-15 09:09:55 +00:00
|
|
|
msgpack_unpack_t* msgpack_unpack_new(void* data, msgpack_unpack_callback* callback)
|
|
|
|
{
|
2009-02-15 09:09:57 +00:00
|
|
|
struct template_context* ctx;
|
|
|
|
ctx = (struct template_context*)calloc(1, sizeof(struct template_context));
|
2009-02-15 09:09:55 +00:00
|
|
|
if(ctx == NULL) { return NULL; }
|
2009-02-15 09:09:57 +00:00
|
|
|
template_func_init(ctx);
|
2009-02-15 09:09:55 +00:00
|
|
|
((msgpack_unpack_t*)ctx)->data = data;
|
|
|
|
((msgpack_unpack_t*)ctx)->callback = *callback;
|
|
|
|
return (msgpack_unpack_t*)ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
void msgpack_unpack_free(msgpack_unpack_t* ctx)
|
|
|
|
{
|
2009-02-15 09:09:57 +00:00
|
|
|
free((struct template_context*)ctx);
|
2009-02-15 09:09:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void* msgpack_unpack_data(msgpack_unpack_t* ctx)
|
|
|
|
{
|
2009-02-15 09:09:57 +00:00
|
|
|
return template_func_data((struct template_context*)ctx);
|
2009-02-15 09:09:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void msgpack_unpack_reset(msgpack_unpack_t* ctx)
|
|
|
|
{
|
2009-02-15 09:09:57 +00:00
|
|
|
msgpack_unpack_t x = ((struct template_context*)ctx)->user;
|
|
|
|
template_func_init((struct template_context*)ctx);
|
|
|
|
((struct template_context*)ctx)->user = x;
|
2009-02-15 09:09:55 +00:00
|
|
|
}
|
|
|
|
|
2009-02-15 09:09:57 +00:00
|
|
|
int msgpack_unpack_execute(msgpack_unpack_t* ctx,
|
|
|
|
const char* data, size_t len, size_t* off)
|
2009-02-15 09:09:55 +00:00
|
|
|
{
|
2009-02-15 09:09:57 +00:00
|
|
|
return template_func_execute(
|
|
|
|
(struct template_context*)ctx,
|
2009-02-15 09:09:55 +00:00
|
|
|
data, len, off);
|
|
|
|
}
|
|
|
|
|
2009-02-15 09:09:57 +00:00
|
|
|
|