msgpack/perl/xs-src/unpack.c

407 lines
9.1 KiB
C
Raw Normal View History

2009-04-15 13:09:05 +09:00
#define NEED_newRV_noinc
#define NEED_sv_2pv_flags
2010-09-12 00:09:44 +09:00
#include "xshelper.h"
2009-04-15 12:55:41 +09:00
typedef struct {
bool finished;
bool incremented;
2009-04-15 12:55:41 +09:00
} unpack_user;
#include "msgpack/unpack_define.h"
#define msgpack_unpack_struct(name) \
struct template ## name
#define msgpack_unpack_func(ret, name) \
2010-09-10 21:00:27 +09:00
STATIC_INLINE ret template ## name
2009-04-15 12:55:41 +09:00
#define msgpack_unpack_callback(name) \
template_callback ## name
#define msgpack_unpack_object SV*
#define msgpack_unpack_user unpack_user
2009-07-30 16:22:00 +09:00
/* ---------------------------------------------------------------------- */
/* utility functions */
2010-09-10 20:42:40 +09:00
STATIC_INLINE SV *
2009-07-30 16:22:00 +09:00
get_bool (const char *name) {
2010-09-12 00:09:44 +09:00
dTHX;
SV * sv = sv_mortalcopy(get_sv( name, 1 ));
2009-07-30 16:22:00 +09:00
SvREADONLY_on(sv);
SvREADONLY_on( SvRV(sv) );
return sv;
}
/* ---------------------------------------------------------------------- */
2009-04-15 12:55:41 +09:00
struct template_context;
typedef struct template_context msgpack_unpack_t;
static void template_init(msgpack_unpack_t* u);
static SV* template_data(msgpack_unpack_t* u);
2010-09-15 13:07:44 +09:00
static int template_execute(msgpack_unpack_t* u PERL_UNUSED_DECL,
2009-04-15 12:55:41 +09:00
const char* data, size_t len, size_t* off);
2010-09-15 13:07:44 +09:00
STATIC_INLINE SV* template_callback_root(unpack_user* u PERL_UNUSED_DECL)
2010-09-15 13:03:47 +09:00
{
dTHX;
return &PL_sv_undef;
2010-09-15 13:03:47 +09:00
}
2009-04-15 12:55:41 +09:00
2010-09-15 13:07:44 +09:00
STATIC_INLINE int template_callback_uint8(unpack_user* u PERL_UNUSED_DECL, uint8_t d, SV** o)
2010-09-15 13:03:47 +09:00
{
dTHX;
*o = sv_2mortal(newSVuv(d));
return 0;
}
2009-04-15 12:55:41 +09:00
2010-09-15 13:07:44 +09:00
STATIC_INLINE int template_callback_uint16(unpack_user* u PERL_UNUSED_DECL, uint16_t d, SV** o)
2010-09-15 13:03:47 +09:00
{
dTHX;
*o = sv_2mortal(newSVuv(d));
return 0;
}
2009-04-15 12:55:41 +09:00
2010-09-15 13:07:44 +09:00
STATIC_INLINE int template_callback_uint32(unpack_user* u PERL_UNUSED_DECL, uint32_t d, SV** o)
2010-09-15 13:03:47 +09:00
{
dTHX;
*o = sv_2mortal(newSVuv(d));
return 0;
}
2009-04-15 12:55:41 +09:00
2010-09-15 13:07:44 +09:00
STATIC_INLINE int template_callback_uint64(unpack_user* u PERL_UNUSED_DECL, uint64_t d, SV** o)
{
2010-09-12 00:09:44 +09:00
dTHX;
#if IVSIZE==4
*o = sv_2mortal(newSVnv(d));
#else
*o = sv_2mortal(newSVuv(d));
#endif
return 0;
}
2009-04-15 12:55:41 +09:00
2010-09-15 13:03:47 +09:00
STATIC_INLINE int template_callback_int8(unpack_user* u PERL_UNUSED_DECL, int8_t d, SV** o)
{
dTHX;
*o = sv_2mortal(newSViv(d));
return 0;
}
2009-04-15 12:55:41 +09:00
2010-09-15 13:03:47 +09:00
STATIC_INLINE int template_callback_int16(unpack_user* u PERL_UNUSED_DECL, int16_t d, SV** o)
{
dTHX;
*o = sv_2mortal(newSViv(d));
return 0;
}
2009-04-15 12:55:41 +09:00
2010-09-15 13:07:44 +09:00
STATIC_INLINE int template_callback_int32(unpack_user* u PERL_UNUSED_DECL, int32_t d, SV** o)
2010-09-15 13:03:47 +09:00
{
dTHX;
*o = sv_2mortal(newSViv(d));
return 0;
}
2009-04-15 12:55:41 +09:00
2010-09-15 13:07:44 +09:00
STATIC_INLINE int template_callback_int64(unpack_user* u PERL_UNUSED_DECL, int64_t d, SV** o)
2010-09-15 13:03:47 +09:00
{
dTHX;
2010-09-15 13:09:14 +09:00
#if IVSIZE==4
*o = sv_2mortal(newSVnv(d));
#else
2010-09-15 13:03:47 +09:00
*o = sv_2mortal(newSViv(d));
2010-09-15 13:09:14 +09:00
#endif
2010-09-15 13:03:47 +09:00
return 0;
}
2009-04-15 12:55:41 +09:00
2010-09-15 13:07:44 +09:00
STATIC_INLINE int template_callback_float(unpack_user* u PERL_UNUSED_DECL, float d, SV** o)
2010-09-15 13:03:47 +09:00
{
dTHX;
*o = sv_2mortal(newSVnv(d));
return 0;
}
2009-04-15 12:55:41 +09:00
2010-09-15 13:07:44 +09:00
STATIC_INLINE int template_callback_double(unpack_user* u PERL_UNUSED_DECL, double d, SV** o)
2010-09-15 13:03:47 +09:00
{
dTHX;
*o = sv_2mortal(newSVnv(d));
return 0;
}
2009-04-15 12:55:41 +09:00
/* &PL_sv_undef is not so good. see http://gist.github.com/387743 */
2010-09-15 13:07:44 +09:00
STATIC_INLINE int template_callback_nil(unpack_user* u PERL_UNUSED_DECL, SV** o)
2010-09-15 13:03:47 +09:00
{
dTHX;
*o = sv_newmortal();
return 0;
}
2009-04-15 12:55:41 +09:00
2010-09-15 13:07:44 +09:00
STATIC_INLINE int template_callback_true(unpack_user* u PERL_UNUSED_DECL, SV** o)
2010-09-15 13:03:47 +09:00
{
dTHX;
*o = get_bool("Data::MessagePack::true");
return 0;
}
2009-04-15 12:55:41 +09:00
2010-09-15 13:07:44 +09:00
STATIC_INLINE int template_callback_false(unpack_user* u PERL_UNUSED_DECL, SV** o)
2010-09-15 13:03:47 +09:00
{
dTHX; *o = get_bool("Data::MessagePack::false");
return 0;
}
2009-04-15 12:55:41 +09:00
2010-09-15 13:07:44 +09:00
STATIC_INLINE int template_callback_array(unpack_user* u PERL_UNUSED_DECL, unsigned int n, SV** o)
2010-09-15 13:03:47 +09:00
{
dTHX;
2010-09-15 13:12:17 +09:00
AV* const a = newAV();
*o = sv_2mortal(newRV_noinc((SV*)a));
av_extend(a, n + 1);
2010-09-15 13:03:47 +09:00
return 0;
}
2009-04-15 12:55:41 +09:00
2010-09-15 13:07:44 +09:00
STATIC_INLINE int template_callback_array_item(unpack_user* u PERL_UNUSED_DECL, SV** c, SV* o)
2010-09-15 13:03:47 +09:00
{
dTHX;
2010-09-15 13:22:39 +09:00
AV* const a = (AV*)SvRV(*c);
(void)av_store(a, AvFILLp(a) + 1, o); // the same as av_push(a, o)
2010-09-15 13:12:17 +09:00
SvREFCNT_inc_simple_void_NN(o);
2010-09-15 13:03:47 +09:00
return 0;
}
2009-04-15 12:55:41 +09:00
2010-09-15 13:07:44 +09:00
STATIC_INLINE int template_callback_map(unpack_user* u PERL_UNUSED_DECL, unsigned int n PERL_UNUSED_DECL, SV** o)
2010-09-15 13:03:47 +09:00
{
dTHX;
2010-09-15 13:12:17 +09:00
HV* const h = newHV();
*o = sv_2mortal(newRV_noinc((SV*)h));
2010-09-15 13:03:47 +09:00
return 0;
}
2009-04-15 12:55:41 +09:00
2010-09-15 13:07:44 +09:00
STATIC_INLINE int template_callback_map_item(unpack_user* u PERL_UNUSED_DECL, SV** c, SV* k, SV* v)
2010-09-15 13:03:47 +09:00
{
dTHX;
2010-09-15 13:07:44 +09:00
(void)hv_store_ent((HV*)SvRV(*c), k, v, 0);
2010-09-15 13:12:17 +09:00
SvREFCNT_inc_simple_void_NN(v);
2010-09-15 13:03:47 +09:00
return 0;
}
2009-04-15 12:55:41 +09:00
2010-09-15 13:07:44 +09:00
STATIC_INLINE int template_callback_raw(unpack_user* u PERL_UNUSED_DECL, const char* b PERL_UNUSED_DECL, const char* p, unsigned int l, SV** o)
2010-09-15 13:03:47 +09:00
{
dTHX;
2010-09-15 13:20:20 +09:00
/* newSVpvn_flags(p, l, SVs_TEMP) returns an undef if l == 0 */
*o = ((l==0) ? newSVpvs_flags("", SVs_TEMP) : newSVpvn_flags(p, l, SVs_TEMP));
2010-09-15 13:03:47 +09:00
return 0;
}
2009-04-15 12:55:41 +09:00
2010-09-15 13:41:10 +09:00
#define UNPACKER(from, name) \
msgpack_unpack_t *name; \
if(!(SvROK(from) && SvIOK(SvRV(from)))) { \
Perl_croak(aTHX_ "Invalid unpacker instance for " #name); \
} \
name = INT2PTR(msgpack_unpack_t*, SvIVX(SvRV((from)))); \
if(name == NULL) { \
Perl_croak(aTHX_ "NULL found for " # name " when shouldn't be."); \
}
2009-04-15 12:55:41 +09:00
#include "msgpack/unpack_template.h"
2010-09-15 13:16:13 +09:00
STATIC_INLINE SV* _msgpack_unpack(SV* data, size_t limit PERL_UNUSED_DECL) {
2009-04-15 12:55:41 +09:00
msgpack_unpack_t mp;
2010-09-12 00:09:44 +09:00
dTHX;
unpack_user u = {false, false};
2009-04-15 12:55:41 +09:00
int ret;
size_t from = 0;
STRLEN dlen;
const char * dptr = SvPV_const(data, dlen);
SV* obj;
2009-04-15 12:55:41 +09:00
template_init(&mp);
mp.user = u;
ret = template_execute(&mp, dptr, (size_t)dlen, &from);
obj = template_data(&mp);
2009-04-15 12:55:41 +09:00
if(ret < 0) {
Perl_croak(aTHX_ "parse error.");
} else if(ret == 0) {
Perl_croak(aTHX_ "insufficient bytes.");
} else {
if(from < dlen) {
Perl_croak(aTHX_ "extra bytes.");
}
return obj;
2009-04-15 12:55:41 +09:00
}
}
XS(xs_unpack) {
dXSARGS;
2010-09-15 13:16:13 +09:00
SV* const data = ST(1);
size_t limit;
2009-04-15 12:55:41 +09:00
2010-09-15 13:16:13 +09:00
if (items == 2) {
limit = sv_len(data);
2009-04-15 12:55:41 +09:00
}
2010-09-15 13:16:13 +09:00
else if(items == 3) {
limit = SvUVx(ST(2));
2009-04-15 12:55:41 +09:00
}
2010-09-15 13:16:13 +09:00
else {
Perl_croak(aTHX_ "Usage: Data::MessagePack->unpack('data' [, $limit])");
}
ST(0) = _msgpack_unpack(data, limit);
2009-04-15 12:55:41 +09:00
XSRETURN(1);
}
/* ------------------------------ stream -- */
2010-05-03 00:46:15 +09:00
/* http://twitter.com/frsyuki/status/13249304748 */
2009-04-15 12:55:41 +09:00
2010-09-10 20:45:17 +09:00
STATIC_INLINE void _reset(SV* self) {
2010-09-12 00:09:44 +09:00
dTHX;
unpack_user u = {false, false};
2010-05-03 00:46:15 +09:00
2009-04-15 12:55:41 +09:00
UNPACKER(self, mp);
template_init(mp);
mp->user = u;
}
XS(xs_unpacker_new) {
dXSARGS;
2009-04-15 23:11:26 +09:00
if (items != 1) {
Perl_croak(aTHX_ "Usage: Data::MessagePack::Unpacker->new()");
}
2009-04-15 12:55:41 +09:00
SV* self = sv_newmortal();
msgpack_unpack_t *mp;
Newx(mp, 1, msgpack_unpack_t);
sv_setref_pv(self, "Data::MessagePack::Unpacker", mp);
_reset(self);
ST(0) = self;
XSRETURN(1);
}
2010-09-15 14:07:33 +09:00
STATIC_INLINE SV* _execute_impl(SV* self, SV* data, UV off, size_t limit) {
2010-09-12 00:09:44 +09:00
dTHX;
2009-04-15 12:55:41 +09:00
UNPACKER(self, mp);
size_t from = off;
const char* dptr = SvPV_nolen_const(data);
int ret;
2010-09-15 14:07:33 +09:00
if(from >= limit) {
2010-09-15 15:06:03 +09:00
Perl_croak(aTHX_ "offset (%lu) is bigger than data buffer size (%lu)", (unsigned long)off, (unsigned long)limit);
2009-04-15 12:55:41 +09:00
}
2010-09-15 14:07:33 +09:00
ret = template_execute(mp, dptr, limit, &from);
2009-04-15 12:55:41 +09:00
if(ret < 0) {
Perl_croak(aTHX_ "parse error.");
} else {
2010-09-15 14:09:03 +09:00
mp->user.finished = (ret > 0) ? true : false;
2010-05-03 00:46:15 +09:00
return sv_2mortal(newSVuv(from));
2009-04-15 12:55:41 +09:00
}
}
XS(xs_unpacker_execute) {
dXSARGS;
2009-04-15 23:11:26 +09:00
if (items != 3) {
2010-09-15 14:20:32 +09:00
Perl_croak(aTHX_ "Usage: $unpacker->execute(data, off)");
2009-04-15 23:11:26 +09:00
}
2010-05-03 00:46:15 +09:00
UNPACKER(ST(0), mp);
2009-04-15 23:11:26 +09:00
{
SV* self = ST(0);
SV* data = ST(1);
2010-05-03 00:46:15 +09:00
IV off = SvIV(ST(2)); /* offset of $data. normaly, 0. */
2009-04-15 12:55:41 +09:00
2010-09-15 14:07:33 +09:00
ST(0) = _execute_impl(self, data, off, (size_t)sv_len(data));
2010-05-03 00:46:15 +09:00
{
SV * d2 = template_data(mp);
if (!mp->user.incremented && d2) {
SvREFCNT_inc(d2);
mp->user.incremented = true;
2010-05-03 00:46:15 +09:00
}
}
2009-04-15 23:11:26 +09:00
}
2009-04-15 12:55:41 +09:00
XSRETURN(1);
}
XS(xs_unpacker_execute_limit) {
dXSARGS;
2009-04-15 23:11:26 +09:00
if (items != 4) {
Perl_croak(aTHX_ "Usage: $unpacker->execute_limit(data, off, limit)");
}
2009-04-15 12:55:41 +09:00
SV* self = ST(0);
SV* data = ST(1);
IV off = SvIV(ST(2));
IV limit = SvIV(ST(3));
2010-09-15 14:07:33 +09:00
ST(0) = _execute_impl(self, data, off, (size_t)limit);
2009-04-15 12:55:41 +09:00
XSRETURN(1);
}
XS(xs_unpacker_is_finished) {
dXSARGS;
2009-04-15 23:11:26 +09:00
if (items != 1) {
Perl_croak(aTHX_ "Usage: $unpacker->is_finished()");
}
2009-04-15 12:55:41 +09:00
UNPACKER(ST(0), mp);
ST(0) = boolSV(mp->user.finished);
2009-04-15 12:55:41 +09:00
XSRETURN(1);
}
XS(xs_unpacker_data) {
dXSARGS;
2009-04-15 23:11:26 +09:00
if (items != 1) {
Perl_croak(aTHX_ "Usage: $unpacker->data()");
}
2009-04-15 12:55:41 +09:00
UNPACKER(ST(0), mp);
2010-09-15 14:21:44 +09:00
ST(0) = sv_mortalcopy(template_data(mp));
2009-04-15 12:55:41 +09:00
XSRETURN(1);
}
XS(xs_unpacker_reset) {
dXSARGS;
if (items != 1) {
Perl_croak(aTHX_ "Usage: $unpacker->reset()");
}
2010-05-03 00:46:15 +09:00
UNPACKER(ST(0), mp);
{
SV * data = template_data(mp);
2010-09-15 14:25:50 +09:00
SvREFCNT_dec(data);
2010-05-03 00:46:15 +09:00
}
2009-04-15 12:55:41 +09:00
_reset(ST(0));
XSRETURN(0);
}
XS(xs_unpacker_destroy) {
dXSARGS;
if (items != 1) {
Perl_croak(aTHX_ "Usage: $unpacker->DESTROY()");
}
UNPACKER(ST(0), mp);
SV * data = template_data(mp);
if (SvOK(data)) {
2010-05-03 00:46:15 +09:00
SvREFCNT_dec(data);
}
Safefree(mp);
XSRETURN(0);
}