msgpack/perl/xs-src/unpack.c

473 lines
11 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-18 14:50:52 +09:00
#define NEED_my_snprintf
2010-09-12 00:09:44 +09:00
#include "xshelper.h"
2009-04-15 12:55:41 +09:00
#define MY_CXT_KEY "Data::MessagePack::_unpack_guts" XS_VERSION
typedef struct {
SV* msgpack_true;
SV* msgpack_false;
} my_cxt_t;
START_MY_CXT
2009-04-15 12:55:41 +09:00
typedef struct {
bool finished;
bool incremented;
bool utf8;
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
void init_Data__MessagePack_unpack(pTHX_ bool const cloning) {
2010-09-22 15:25:08 +09:00
// booleans are load on demand (lazy load).
if(!cloning) {
MY_CXT_INIT;
MY_CXT.msgpack_true = NULL;
MY_CXT.msgpack_false = NULL;
}
else {
MY_CXT_CLONE;
MY_CXT.msgpack_true = NULL;
MY_CXT.msgpack_false = NULL;
}
}
2009-07-30 16:22:00 +09:00
/* ---------------------------------------------------------------------- */
/* utility functions */
static SV*
load_bool(pTHX_ const char* const name) {
CV* const cv = get_cv(name, GV_ADD);
dSP;
2010-09-22 15:25:08 +09:00
ENTER;
SAVETMPS;
PUSHMARK(SP);
call_sv((SV*)cv, G_SCALAR);
SPAGAIN;
SV* const sv = newSVsv(POPs);
PUTBACK;
2010-09-22 15:25:08 +09:00
FREETMPS;
LEAVE;
assert(sv);
assert(sv_isobject(sv));
2010-10-12 23:05:58 +09:00
if(!SvOK(sv)) {
croak("Oops: Failed to load %"SVf, name);
}
return sv;
}
static SV*
get_bool(bool const value) {
2010-09-12 00:09:44 +09:00
dTHX;
dMY_CXT;
if(value) {
if(!MY_CXT.msgpack_true) {
MY_CXT.msgpack_true = load_bool(aTHX_ "Data::MessagePack::true");
}
return newSVsv(MY_CXT.msgpack_true);
}
else {
if(!MY_CXT.msgpack_false) {
MY_CXT.msgpack_false = load_bool(aTHX_ "Data::MessagePack::false");
}
return newSVsv(MY_CXT.msgpack_false);
}
2009-07-30 16:22:00 +09:00
}
/* ---------------------------------------------------------------------- */
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
{
2010-09-16 20:24:01 +09:00
return NULL;
2010-09-15 13:03:47 +09:00
}
2009-04-15 12:55:41 +09:00
2010-09-16 20:24:01 +09:00
#if IVSIZE == 4
2009-04-15 12:55:41 +09:00
2010-09-16 20:24:01 +09:00
STATIC_INLINE int template_callback_UV(unpack_user* u PERL_UNUSED_DECL, UV const d, SV** o)
2010-09-15 13:03:47 +09:00
{
dTHX;
2010-09-16 20:24:01 +09:00
*o = newSVuv(d);
2010-09-15 13:03:47 +09:00
return 0;
}
2009-04-15 12:55:41 +09:00
STATIC_INLINE int template_callback_IV(unpack_user* u PERL_UNUSED_DECL, IV const d, SV** o)
2010-09-15 13:03:47 +09:00
{
dTHX;
*o = newSViv(d);
2010-09-15 13:03:47 +09:00
return 0;
}
2009-04-15 12:55:41 +09:00
static int template_callback_uint64(unpack_user* u PERL_UNUSED_DECL, uint64_t const d, SV** o)
{
2010-09-12 00:09:44 +09:00
dTHX;
char tbuf[64];
STRLEN const len = my_snprintf(tbuf, sizeof(tbuf), "%llu", d);
*o = newSVpvn(tbuf, len);
return 0;
}
2009-04-15 12:55:41 +09:00
static int template_callback_int64(unpack_user* u PERL_UNUSED_DECL, int64_t const d, SV** o)
2010-09-15 13:03:47 +09:00
{
dTHX;
char tbuf[64];
STRLEN const len = my_snprintf(tbuf, sizeof(tbuf), "%lld", d);
*o = newSVpvn(tbuf, len);
2010-09-15 13:03:47 +09:00
return 0;
}
2009-04-15 12:55:41 +09:00
2010-09-16 20:24:01 +09:00
#else /* IVSIZE == 8 */
2009-04-15 12:55:41 +09:00
2010-09-16 20:24:01 +09:00
STATIC_INLINE int template_callback_UV(unpack_user* u PERL_UNUSED_DECL, UV const d, SV** o)
2010-09-15 13:03:47 +09:00
{
dTHX;
2010-09-16 20:24:01 +09:00
*o = newSVuv(d);
2010-09-15 13:03:47 +09:00
return 0;
}
2009-04-15 12:55:41 +09:00
2010-09-16 20:24:01 +09:00
#define template_callback_uint64 template_callback_UV
STATIC_INLINE int template_callback_IV(unpack_user* u PERL_UNUSED_DECL, IV const d, SV** o)
2010-09-15 13:03:47 +09:00
{
dTHX;
2010-09-16 20:24:01 +09:00
*o = newSViv(d);
2010-09-15 13:03:47 +09:00
return 0;
}
2009-04-15 12:55:41 +09:00
2010-09-17 18:08:34 +09:00
#define template_callback_int64 template_callback_IV
2010-09-16 20:24:01 +09:00
#endif /* IVSIZE */
#define template_callback_uint8 template_callback_UV
#define template_callback_uint16 template_callback_UV
#define template_callback_uint32 template_callback_UV
#define template_callback_int8 template_callback_IV
#define template_callback_int16 template_callback_IV
#define template_callback_int32 template_callback_IV
#define template_callback_float template_callback_double
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;
2010-09-16 20:24:01 +09:00
*o = newSVnv(d);
2010-09-15 13:03:47 +09:00
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;
2010-09-16 20:24:01 +09:00
*o = newSV(0);
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_true(unpack_user* u PERL_UNUSED_DECL, SV** o)
2010-09-15 13:03:47 +09:00
{
*o = get_bool(true);
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_false(unpack_user* u PERL_UNUSED_DECL, SV** o)
2010-09-15 13:03:47 +09:00
{
*o = get_bool(false);
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(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();
2010-09-16 20:24:01 +09:00
*o = newRV_noinc((SV*)a);
2010-09-15 13:12:17 +09:00
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);
2010-09-15 15:27:26 +09:00
assert(SvTYPE(a) == SVt_PVAV);
2010-09-15 13:22:39 +09:00
(void)av_store(a, AvFILLp(a) + 1, o); // the same as av_push(a, o)
2010-09-15 13:03:47 +09:00
return 0;
}
2009-04-15 12:55:41 +09:00
2010-09-16 20:31:34 +09:00
STATIC_INLINE int template_callback_map(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
HV* const h = newHV();
2010-09-16 20:31:34 +09:00
hv_ksplit(h, n);
2010-09-16 20:24:01 +09:00
*o = 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 15:27:26 +09:00
HV* const h = (HV*)SvRV(*c);
assert(SvTYPE(h) == SVt_PVHV);
(void)hv_store_ent(h, k, v, 0);
2010-09-16 20:24:01 +09:00
SvREFCNT_dec(k);
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-16 20:41:52 +09:00
/* newSVpvn(p, l) returns an undef if p == NULL */
2010-09-16 20:24:01 +09:00
*o = ((l==0) ? newSVpvs("") : newSVpvn(p, l));
if(u->utf8) {
sv_utf8_decode(*o);
}
2010-09-15 13:03:47 +09:00
return 0;
}
2009-04-15 12:55:41 +09:00
2010-09-16 20:24:01 +09:00
#include "msgpack/unpack_template.h"
2010-09-28 20:35:05 +09:00
#define UNPACKER(from, name) \
msgpack_unpack_t *name; \
{ \
SV* const obj = from; \
if(!(SvROK(obj) && SvIOK(SvRV(obj)))) { \
Perl_croak(aTHX_ "Invalid unpacker instance for " #name); \
} \
name = INT2PTR(msgpack_unpack_t*, SvIVX(SvRV((obj)))); \
if(name == NULL) { \
Perl_croak(aTHX_ "NULL found for " # name " when shouldn't be"); \
} \
2010-09-15 13:41:10 +09:00
}
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])");
}
2010-09-16 20:24:01 +09:00
STRLEN dlen;
const char* const dptr = SvPV_const(data, dlen);
msgpack_unpack_t mp;
template_init(&mp);
unpack_user const u = {false, false, false};
2010-09-16 20:24:01 +09:00
mp.user = u;
2009-04-15 12:55:41 +09:00
2010-09-16 20:24:01 +09:00
size_t from = 0;
int const ret = template_execute(&mp, dptr, (size_t)dlen, &from);
SV* const obj = template_data(&mp);
sv_2mortal(obj);
if(ret < 0) {
Perl_croak(aTHX_ "Data::MessagePack->unpack: parse error");
} else if(ret == 0) {
Perl_croak(aTHX_ "Data::MessagePack->unpack: insufficient bytes");
} else {
if(from < dlen) {
Perl_croak(aTHX_ "Data::MessagePack->unpack: extra bytes");
}
}
ST(0) = obj;
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-16 20:24:01 +09:00
STATIC_INLINE void _reset(SV* const self) {
2010-09-12 00:09:44 +09:00
dTHX;
unpack_user const u = {false, 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()");
}
2010-09-16 20:24:01 +09:00
SV* const self = sv_newmortal();
msgpack_unpack_t *mp;
2009-04-15 12:55:41 +09:00
2010-09-16 20:24:01 +09:00
Newxz(mp, 1, msgpack_unpack_t);
2009-04-15 12:55:41 +09:00
sv_setref_pv(self, "Data::MessagePack::Unpacker", mp);
_reset(self);
ST(0) = self;
XSRETURN(1);
}
XS(xs_unpacker_utf8) {
dXSARGS;
if (!(items == 1 || items == 2)) {
Perl_croak(aTHX_ "Usage: $unpacker->utf8([$bool)");
}
UNPACKER(ST(0), mp);
mp->user.utf8 = (items == 1 || sv_true(ST(1))) ? true : false;
XSRETURN(1); // returns $self
}
XS(xs_unpacker_get_utf8) {
dXSARGS;
if (items != 1) {
Perl_croak(aTHX_ "Usage: $unpacker->get_utf8()");
}
UNPACKER(ST(0), mp);
ST(0) = boolSV(mp->user.utf8);
XSRETURN(1);
}
2010-09-28 20:35:05 +09:00
STATIC_INLINE size_t
2010-09-16 20:24:01 +09:00
_execute_impl(SV* const self, SV* const data, UV const offset, UV const limit) {
2010-09-12 00:09:44 +09:00
dTHX;
2009-04-15 12:55:41 +09:00
2010-09-16 20:24:01 +09:00
if(offset >= limit) {
Perl_croak(aTHX_ "offset (%"UVuf") is bigger than data buffer size (%"UVuf")",
offset, limit);
}
UNPACKER(self, mp);
2009-04-15 12:55:41 +09:00
2010-09-16 20:24:01 +09:00
size_t from = offset;
const char* const dptr = SvPV_nolen_const(data);
2009-04-15 12:55:41 +09:00
2010-09-16 20:24:01 +09:00
int const ret = template_execute(mp, dptr, limit, &from);
2009-04-15 12:55:41 +09:00
2010-09-16 20:24:01 +09:00
if(ret < 0) {
Perl_croak(aTHX_ "Data::MessagePack::Unpacker: parse error while executing");
}
2010-09-28 20:35:05 +09:00
mp->user.finished = (ret > 0) ? true : false;
return from;
2009-04-15 12:55:41 +09:00
}
XS(xs_unpacker_execute) {
dXSARGS;
2010-09-16 20:24:01 +09:00
SV* const self = ST(0);
SV* const data = ST(1);
UV offset;
2010-05-03 00:46:15 +09:00
2010-09-16 20:24:01 +09:00
if (items == 2) {
offset = 0;
}
else if (items == 3) {
offset = SvUVx(ST(2));
2009-04-15 23:11:26 +09:00
}
2010-09-16 20:24:01 +09:00
else {
Perl_croak(aTHX_ "Usage: $unpacker->execute(data, offset = 0)");
}
2010-09-28 20:35:05 +09:00
dXSTARG;
sv_setuv(TARG, _execute_impl(self, data, offset, sv_len(data)));
ST(0) = TARG;
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) {
2010-09-16 20:24:01 +09:00
Perl_croak(aTHX_ "Usage: $unpacker->execute_limit(data, offset, limit)");
2009-04-15 23:11:26 +09:00
}
2010-09-16 20:24:01 +09:00
SV* const self = ST(0);
SV* const data = ST(1);
UV const offset = SvUVx(ST(2));
UV const limit = SvUVx(ST(3));
2009-04-15 12:55:41 +09:00
2010-09-28 20:35:05 +09:00
dXSTARG;
sv_setuv(TARG, _execute_impl(self, data, offset, limit));
ST(0) = TARG;
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
2010-09-16 20:24:01 +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
2010-09-16 20:24:01 +09:00
UNPACKER(ST(0), mp);
ST(0) = 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-09-16 20:24:01 +09:00
UNPACKER(ST(0), mp);
bool const utf8 = mp->user.utf8; // save
2010-09-15 15:27:26 +09:00
SV* const data = template_data(mp);
2010-09-28 20:35:05 +09:00
SvREFCNT_dec(data);
2009-04-15 12:55:41 +09:00
_reset(ST(0));
mp->user.utf8 = utf8;
2009-04-15 12:55:41 +09:00
XSRETURN(0);
}
XS(xs_unpacker_destroy) {
dXSARGS;
if (items != 1) {
Perl_croak(aTHX_ "Usage: $unpacker->DESTROY()");
}
2010-09-16 20:24:01 +09:00
UNPACKER(ST(0), mp);
2010-09-15 15:27:26 +09:00
SV* const data = template_data(mp);
2010-09-28 20:35:05 +09:00
SvREFCNT_dec(data);
Safefree(mp);
XSRETURN(0);
}