mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-04-25 01:19:07 +02:00
Modified unpack functions to class member functions.
This commit is contained in:
parent
ec5c1194fc
commit
11afd4820f
@ -47,12 +47,17 @@ namespace msgpack {
|
||||
|
||||
namespace detail {
|
||||
|
||||
struct unpack_user {
|
||||
zone* z;
|
||||
bool referenced;
|
||||
class unpack_user {
|
||||
public:
|
||||
zone* z() const { return z_; }
|
||||
void set_z(zone* z) { z_ = z; }
|
||||
bool referenced() const { return referenced_; }
|
||||
void set_referenced(bool referenced) { referenced_ = referenced; }
|
||||
private:
|
||||
zone* z_;
|
||||
bool referenced_;
|
||||
};
|
||||
|
||||
|
||||
static inline object template_callback_root(unpack_user* u)
|
||||
{ object o = {}; return o; }
|
||||
|
||||
@ -103,7 +108,7 @@ static inline int template_callback_array(unpack_user* u, unsigned int n, object
|
||||
{
|
||||
o->type = type::ARRAY;
|
||||
o->via.array.size = 0;
|
||||
o->via.array.ptr = (object*)u->z->malloc(n*sizeof(object));
|
||||
o->via.array.ptr = (object*)u->z()->malloc(n*sizeof(object));
|
||||
if(o->via.array.ptr == NULL) { return -1; }
|
||||
return 0;
|
||||
}
|
||||
@ -115,7 +120,7 @@ static inline int template_callback_map(unpack_user* u, unsigned int n, object*
|
||||
{
|
||||
o->type = type::MAP;
|
||||
o->via.map.size = 0;
|
||||
o->via.map.ptr = (object_kv*)u->z->malloc(n*sizeof(object_kv));
|
||||
o->via.map.ptr = (object_kv*)u->z()->malloc(n*sizeof(object_kv));
|
||||
if(o->via.map.ptr == NULL) { return -1; }
|
||||
return 0;
|
||||
}
|
||||
@ -133,27 +138,30 @@ static inline int template_callback_raw(unpack_user* u, const char* b, const cha
|
||||
o->type = type::RAW;
|
||||
o->via.raw.ptr = p;
|
||||
o->via.raw.size = l;
|
||||
u->referenced = true;
|
||||
u->set_referenced(true);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
struct template_unpack_stack {
|
||||
object obj;
|
||||
size_t count;
|
||||
unsigned int ct;
|
||||
object map_key;
|
||||
class template_unpack_stack {
|
||||
public:
|
||||
object obj() const { return obj_; }
|
||||
object& obj() { return obj_; }
|
||||
void setObj(object obj) { obj_ = obj; }
|
||||
size_t count() const { return count_; }
|
||||
void set_count(size_t count) { count_ = count; }
|
||||
size_t decl_count() { return --count_; }
|
||||
unsigned int ct() const { return ct_; }
|
||||
void set_ct(unsigned int ct) { ct_ = ct; }
|
||||
object map_key() const { return map_key_; }
|
||||
void set_map_key(object map_key) { map_key_ = map_key; }
|
||||
private:
|
||||
object obj_;
|
||||
size_t count_;
|
||||
unsigned int ct_;
|
||||
object map_key_;
|
||||
};
|
||||
|
||||
struct template_context {
|
||||
unpack_user user;
|
||||
unsigned int cs;
|
||||
unsigned int trail;
|
||||
unsigned int top;
|
||||
template_unpack_stack stack[MSGPACK_EMBED_STACK_SIZE];
|
||||
};
|
||||
|
||||
|
||||
inline void init_count(void* buffer)
|
||||
{
|
||||
*(volatile _msgpack_atomic_counter_t*)buffer = 1;
|
||||
@ -178,26 +186,37 @@ inline _msgpack_atomic_counter_t get_count(void* buffer)
|
||||
return *(volatile _msgpack_atomic_counter_t*)buffer;
|
||||
}
|
||||
|
||||
inline void template_init(template_context& ctx)
|
||||
class template_context {
|
||||
public:
|
||||
template_context():cs_(CS_HEADER), trail_(0), top_(0)
|
||||
{
|
||||
ctx.cs = CS_HEADER;
|
||||
ctx.trail = 0;
|
||||
ctx.top = 0;
|
||||
ctx.stack[0].obj = template_callback_root(&ctx.user);
|
||||
stack_[0].setObj(template_callback_root(&user_));
|
||||
}
|
||||
|
||||
object template_data(template_context const& ctx)
|
||||
void init()
|
||||
{
|
||||
return ctx.stack[0].obj;
|
||||
cs_ = CS_HEADER;
|
||||
trail_ = 0;
|
||||
top_ = 0;
|
||||
stack_[0].setObj(template_callback_root(&user_));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline unsigned int next_cs(T p)
|
||||
object data() const
|
||||
{
|
||||
return (unsigned int)*p & 0x1f;
|
||||
return stack_[0].obj();
|
||||
}
|
||||
|
||||
int template_execute(template_context& ctx, const char* data, size_t len, size_t* off)
|
||||
unpack_user& user()
|
||||
{
|
||||
return user_;
|
||||
}
|
||||
|
||||
unpack_user const& user() const
|
||||
{
|
||||
return user_;
|
||||
}
|
||||
|
||||
int execute(const char* data, size_t len, size_t* off)
|
||||
{
|
||||
assert(len >= *off);
|
||||
|
||||
@ -205,14 +224,11 @@ int template_execute(template_context& ctx, const char* data, size_t len, size_t
|
||||
const unsigned char* const pe = (unsigned char*)data + len;
|
||||
const void* n = nullptr;
|
||||
|
||||
unsigned int trail = ctx.trail;
|
||||
unsigned int cs = ctx.cs;
|
||||
unsigned int top = ctx.top;
|
||||
template_unpack_stack* stack = ctx.stack;
|
||||
/*
|
||||
unsigned int stack_size = ctx.stack_size;
|
||||
*/
|
||||
unpack_user* user = &ctx.user;
|
||||
unsigned int trail = trail_;
|
||||
unsigned int cs = cs_;
|
||||
unsigned int top = top_;
|
||||
template_unpack_stack* stack = stack_;
|
||||
unpack_user* user = &user_;
|
||||
|
||||
object obj;
|
||||
template_unpack_stack* c = nullptr;
|
||||
@ -296,19 +312,19 @@ int template_execute(template_context& ctx, const char* data, size_t len, size_t
|
||||
|
||||
} else if(0x90 <= *p && *p <= 0x9f) { // FixArray
|
||||
if(top >= MSGPACK_EMBED_STACK_SIZE) { goto _failed; } /* FIXME */
|
||||
if(template_callback_array(user, ((unsigned int)*p) & 0x0f, &stack[top].obj) < 0) { goto _failed; }
|
||||
if((((unsigned int)*p) & 0x0f) == 0) { obj = stack[top].obj; goto _push; }
|
||||
stack[top].ct = CT_ARRAY_ITEM;
|
||||
stack[top].count = ((unsigned int)*p) & 0x0f;
|
||||
if(template_callback_array(user, ((unsigned int)*p) & 0x0f, &stack[top].obj()) < 0) { goto _failed; }
|
||||
if((((unsigned int)*p) & 0x0f) == 0) { obj = stack[top].obj(); goto _push; }
|
||||
stack[top].set_ct(CT_ARRAY_ITEM);
|
||||
stack[top].set_count(((unsigned int)*p) & 0x0f);
|
||||
++top;
|
||||
goto _header_again;
|
||||
|
||||
} else if(0x80 <= *p && *p <= 0x8f) { // FixMap
|
||||
if(top >= MSGPACK_EMBED_STACK_SIZE) { goto _failed; } /* FIXME */
|
||||
if(template_callback_map(user, ((unsigned int)*p) & 0x0f, &stack[top].obj) < 0) { goto _failed; }
|
||||
if((((unsigned int)*p) & 0x0f) == 0) { obj = stack[top].obj; goto _push; }
|
||||
stack[top].ct = CT_MAP_KEY;
|
||||
stack[top].count = ((unsigned int)*p) & 0x0f;
|
||||
if(template_callback_map(user, ((unsigned int)*p) & 0x0f, &stack[top].obj()) < 0) { goto _failed; }
|
||||
if((((unsigned int)*p) & 0x0f) == 0) { obj = stack[top].obj(); goto _push; }
|
||||
stack[top].set_ct(CT_MAP_KEY);
|
||||
stack[top].set_count(((unsigned int)*p) & 0x0f);
|
||||
++top;
|
||||
goto _header_again;
|
||||
|
||||
@ -391,37 +407,37 @@ int template_execute(template_context& ctx, const char* data, size_t len, size_t
|
||||
goto _push;
|
||||
case CS_ARRAY_16:
|
||||
if(top >= MSGPACK_EMBED_STACK_SIZE) { goto _failed; } /* FIXME */
|
||||
if(template_callback_array(user, _msgpack_load16(uint16_t, n), &stack[top].obj) < 0) { goto _failed; }
|
||||
if(_msgpack_load16(uint16_t, n) == 0) { obj = stack[top].obj; goto _push; }
|
||||
stack[top].ct = CT_ARRAY_ITEM;
|
||||
stack[top].count = _msgpack_load16(uint16_t, n);
|
||||
if(template_callback_array(user, _msgpack_load16(uint16_t, n), &stack[top].obj()) < 0) { goto _failed; }
|
||||
if(_msgpack_load16(uint16_t, n) == 0) { obj = stack[top].obj(); goto _push; }
|
||||
stack[top].set_ct(CT_ARRAY_ITEM);
|
||||
stack[top].set_count(_msgpack_load16(uint16_t, n));
|
||||
++top;
|
||||
goto _header_again;
|
||||
case CS_ARRAY_32:
|
||||
/* FIXME security guard */
|
||||
if(top >= MSGPACK_EMBED_STACK_SIZE) { goto _failed; } /* FIXME */
|
||||
if(template_callback_array(user, _msgpack_load32(uint32_t, n), &stack[top].obj) < 0) { goto _failed; }
|
||||
if(_msgpack_load32(uint32_t, n) == 0) { obj = stack[top].obj; goto _push; }
|
||||
stack[top].ct = CT_ARRAY_ITEM;
|
||||
stack[top].count = _msgpack_load32(uint32_t, n);
|
||||
if(template_callback_array(user, _msgpack_load32(uint32_t, n), &stack[top].obj()) < 0) { goto _failed; }
|
||||
if(_msgpack_load32(uint32_t, n) == 0) { obj = stack[top].obj(); goto _push; }
|
||||
stack[top].set_ct(CT_ARRAY_ITEM);
|
||||
stack[top].set_count(_msgpack_load32(uint32_t, n));
|
||||
++top;
|
||||
goto _header_again;
|
||||
|
||||
case CS_MAP_16:
|
||||
if(top >= MSGPACK_EMBED_STACK_SIZE) { goto _failed; } /* FIXME */
|
||||
if(template_callback_map(user, _msgpack_load16(uint16_t, n), &stack[top].obj) < 0) { goto _failed; }
|
||||
if(_msgpack_load16(uint16_t, n) == 0) { obj = stack[top].obj; goto _push; }
|
||||
stack[top].ct = CT_MAP_KEY;
|
||||
stack[top].count = _msgpack_load16(uint16_t, n);
|
||||
if(template_callback_map(user, _msgpack_load16(uint16_t, n), &stack[top].obj()) < 0) { goto _failed; }
|
||||
if(_msgpack_load16(uint16_t, n) == 0) { obj = stack[top].obj(); goto _push; }
|
||||
stack[top].set_ct(CT_MAP_KEY);
|
||||
stack[top].set_count(_msgpack_load16(uint16_t, n));
|
||||
++top;
|
||||
goto _header_again;
|
||||
case CS_MAP_32:
|
||||
/* FIXME security guard */
|
||||
if(top >= MSGPACK_EMBED_STACK_SIZE) { goto _failed; } /* FIXME */
|
||||
if(template_callback_map(user, _msgpack_load32(uint32_t, n), &stack[top].obj) < 0) { goto _failed; }
|
||||
if(_msgpack_load32(uint32_t, n) == 0) { obj = stack[top].obj; goto _push; }
|
||||
stack[top].ct = CT_MAP_KEY;
|
||||
stack[top].count = _msgpack_load32(uint32_t, n);
|
||||
if(template_callback_map(user, _msgpack_load32(uint32_t, n), &stack[top].obj()) < 0) { goto _failed; }
|
||||
if(_msgpack_load32(uint32_t, n) == 0) { obj = stack[top].obj(); goto _push; }
|
||||
stack[top].set_ct(CT_MAP_KEY);
|
||||
stack[top].set_count(_msgpack_load32(uint32_t, n));
|
||||
++top;
|
||||
goto _header_again;
|
||||
|
||||
@ -433,29 +449,29 @@ int template_execute(template_context& ctx, const char* data, size_t len, size_t
|
||||
_push:
|
||||
if(top == 0) { goto _finish; }
|
||||
c = &stack[top-1];
|
||||
switch(c->ct) {
|
||||
switch(c->ct()) {
|
||||
case CT_ARRAY_ITEM:
|
||||
if(template_callback_array_item(user, &c->obj, obj) < 0) { goto _failed; }
|
||||
if(--c->count == 0) {
|
||||
obj = c->obj;
|
||||
if(template_callback_array_item(user, &c->obj(), obj) < 0) { goto _failed; }
|
||||
if(c->decl_count() == 0) {
|
||||
obj = c->obj();
|
||||
--top;
|
||||
/*printf("stack pop %d\n", top);*/
|
||||
goto _push;
|
||||
}
|
||||
goto _header_again;
|
||||
case CT_MAP_KEY:
|
||||
c->map_key = obj;
|
||||
c->ct = CT_MAP_VALUE;
|
||||
c->set_map_key(obj);
|
||||
c->set_ct(CT_MAP_VALUE);
|
||||
goto _header_again;
|
||||
case CT_MAP_VALUE:
|
||||
if(template_callback_map_item(user, &c->obj, c->map_key, obj) < 0) { goto _failed; }
|
||||
if(--c->count == 0) {
|
||||
obj = c->obj;
|
||||
if(template_callback_map_item(user, &c->obj(), c->map_key(), obj) < 0) { goto _failed; }
|
||||
if(c->decl_count() == 0) {
|
||||
obj = c->obj();
|
||||
--top;
|
||||
/*printf("stack pop %d\n", top);*/
|
||||
goto _push;
|
||||
}
|
||||
c->ct = CT_MAP_KEY;
|
||||
c->set_ct(CT_MAP_KEY);
|
||||
goto _header_again;
|
||||
|
||||
default:
|
||||
@ -470,7 +486,7 @@ _header_again:
|
||||
|
||||
|
||||
_finish:
|
||||
stack[0].obj = obj;
|
||||
stack[0].setObj(obj);
|
||||
++p;
|
||||
ret = 1;
|
||||
/*printf("-- finish --\n"); */
|
||||
@ -486,16 +502,28 @@ _out:
|
||||
goto _end;
|
||||
|
||||
_end:
|
||||
ctx.cs = cs;
|
||||
ctx.trail = trail;
|
||||
ctx.top = top;
|
||||
cs_ = cs;
|
||||
trail_ = trail;
|
||||
top_ = top;
|
||||
*off = p - (const unsigned char*)data;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
private:
|
||||
template <typename T>
|
||||
static unsigned int next_cs(T p)
|
||||
{
|
||||
return (unsigned int)*p & 0x1f;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
unpack_user user_;
|
||||
unsigned int cs_;
|
||||
unsigned int trail_;
|
||||
unsigned int top_;
|
||||
template_unpack_stack stack_[MSGPACK_EMBED_STACK_SIZE];
|
||||
};
|
||||
|
||||
} // detail
|
||||
|
||||
@ -689,9 +717,9 @@ inline unpacker::unpacker(size_t initial_buffer_size)
|
||||
|
||||
detail::init_count(buffer_);
|
||||
|
||||
detail::template_init(ctx_);
|
||||
ctx_.user.z = z_;
|
||||
ctx_.user.referenced = false;
|
||||
ctx_.init();
|
||||
ctx_.user().set_z(z_);
|
||||
ctx_.user().set_referenced(false);
|
||||
}
|
||||
|
||||
inline unpacker::~unpacker()
|
||||
@ -710,7 +738,7 @@ inline void unpacker::reserve_buffer(size_t size)
|
||||
inline void unpacker::expand_buffer(size_t size)
|
||||
{
|
||||
if(used_ == off_ && detail::get_count(buffer_) == 1
|
||||
&& !ctx_.user.referenced) {
|
||||
&& !ctx_.user().referenced()) {
|
||||
// rewind buffer
|
||||
free_ += used_ - COUNTER_SIZE;
|
||||
used_ = COUNTER_SIZE;
|
||||
@ -749,7 +777,7 @@ inline void unpacker::expand_buffer(size_t size)
|
||||
|
||||
::memcpy(tmp+COUNTER_SIZE, buffer_ + off_, not_parsed);
|
||||
|
||||
if(ctx_.user.referenced) {
|
||||
if(ctx_.user().referenced()) {
|
||||
try {
|
||||
z_->push_finalizer(&detail::decl_count, buffer_);
|
||||
}
|
||||
@ -757,7 +785,7 @@ inline void unpacker::expand_buffer(size_t size)
|
||||
::free(tmp);
|
||||
throw;
|
||||
}
|
||||
ctx_.user.referenced = false;
|
||||
ctx_.user().set_referenced(false);
|
||||
} else {
|
||||
detail::decl_count(buffer_);
|
||||
}
|
||||
@ -822,8 +850,7 @@ inline bool unpacker::execute()
|
||||
inline int unpacker::execute_imp()
|
||||
{
|
||||
size_t off = off_;
|
||||
int ret = detail::template_execute(ctx_,
|
||||
buffer_, used_, &off_);
|
||||
int ret = ctx_.execute(buffer_, used_, &off_);
|
||||
if(off_ > off) {
|
||||
parsed_ += off_ - off;
|
||||
}
|
||||
@ -832,7 +859,7 @@ inline int unpacker::execute_imp()
|
||||
|
||||
inline object unpacker::data()
|
||||
{
|
||||
return template_data(ctx_);
|
||||
return ctx_.data();
|
||||
}
|
||||
|
||||
inline zone* unpacker::release_zone()
|
||||
@ -848,7 +875,7 @@ inline zone* unpacker::release_zone()
|
||||
|
||||
zone* old = z_;
|
||||
z_ = r;
|
||||
ctx_.user.z = z_;
|
||||
ctx_.user().set_z(z_);
|
||||
|
||||
return old;
|
||||
}
|
||||
@ -860,13 +887,13 @@ inline void unpacker::reset_zone()
|
||||
|
||||
inline bool unpacker::flush_zone()
|
||||
{
|
||||
if(ctx_.user.referenced) {
|
||||
if(ctx_.user().referenced()) {
|
||||
try {
|
||||
z_->push_finalizer(&detail::decl_count, buffer_);
|
||||
} catch (...) {
|
||||
return false;
|
||||
}
|
||||
ctx_.user.referenced = false;
|
||||
ctx_.user().set_referenced(false);
|
||||
|
||||
detail::incr_count(buffer_);
|
||||
}
|
||||
@ -876,7 +903,7 @@ inline bool unpacker::flush_zone()
|
||||
|
||||
inline void unpacker::reset()
|
||||
{
|
||||
detail::template_init(ctx_);
|
||||
ctx_.init();
|
||||
// don't reset referenced flag
|
||||
parsed_ = 0;
|
||||
}
|
||||
@ -926,12 +953,12 @@ unpack_imp(const char* data, size_t len, size_t* off,
|
||||
}
|
||||
|
||||
detail::template_context ctx;
|
||||
detail::template_init(ctx);
|
||||
ctx.init();
|
||||
|
||||
ctx.user.z = result_zone;
|
||||
ctx.user.referenced = false;
|
||||
ctx.user().set_z(result_zone);
|
||||
ctx.user().set_referenced(false);
|
||||
|
||||
int e = detail::template_execute(ctx, data, len, &noff);
|
||||
int e = ctx.execute(data, len, &noff);
|
||||
if(e < 0) {
|
||||
return UNPACK_PARSE_ERROR;
|
||||
}
|
||||
@ -942,7 +969,7 @@ unpack_imp(const char* data, size_t len, size_t* off,
|
||||
return UNPACK_CONTINUE;
|
||||
}
|
||||
|
||||
*result = detail::template_data(ctx);
|
||||
*result = ctx.data();
|
||||
|
||||
if(noff < len) {
|
||||
return UNPACK_EXTRA_BYTES;
|
||||
|
Loading…
x
Reference in New Issue
Block a user