| 1 |
#include "EXTERN.h" |
| 2 |
#include "perl.h" |
| 3 |
#include "XSUB.h" |
| 4 |
|
| 5 |
#include <assert.h> |
| 6 |
#include <string.h> |
| 7 |
#include <stdlib.h> |
| 8 |
#include <stdio.h> |
| 9 |
#include <limits.h> |
| 10 |
#include <float.h> |
| 11 |
|
| 12 |
#include "ecb.h" |
| 13 |
|
| 14 |
// known tags |
| 15 |
enum cbor_tag |
| 16 |
{ |
| 17 |
// inofficial extensions (pending iana registration) |
| 18 |
CBOR_TAG_PERL_OBJECT = 256, |
| 19 |
CBOR_TAG_GENERIC_OBJECT = 257, |
| 20 |
|
| 21 |
// rfc7049 |
| 22 |
CBOR_TAG_DATETIME = 0, // rfc4287, utf-8 |
| 23 |
CBOR_TAG_TIMESTAMP = 1, // unix timestamp, any |
| 24 |
CBOR_TAG_POS_BIGNUM = 2, // byte string |
| 25 |
CBOR_TAG_NEG_BIGNUM = 3, // byte string |
| 26 |
CBOR_TAG_DECIMAL = 4, // decimal fraction, array |
| 27 |
CBOR_TAG_BIGFLOAT = 5, // array |
| 28 |
|
| 29 |
CBOR_TAG_CONV_B64U = 21, // base64url, any |
| 30 |
CBOR_TAG_CONV_B64 = 22, // base64, any |
| 31 |
CBOR_TAG_CONV_HEX = 23, // base16, any |
| 32 |
CBOR_TAG_CBOR = 24, // embedded cbor, byte string |
| 33 |
|
| 34 |
CBOR_TAG_URI = 32, // URI rfc3986, utf-8 |
| 35 |
CBOR_TAG_B64U = 33, // base64url rfc4648, utf-8 |
| 36 |
CBOR_TAG_B64 = 34, // base6 rfc46484, utf-8 |
| 37 |
CBOR_TAG_REGEX = 35, // regex pcre/ecma262, utf-8 |
| 38 |
CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8 |
| 39 |
|
| 40 |
CBOR_TAG_MAGIC = 55799 // self-describe cbor |
| 41 |
}; |
| 42 |
|
| 43 |
#define F_SHRINK 0x00000200UL |
| 44 |
#define F_ALLOW_UNKNOWN 0x00002000UL |
| 45 |
|
| 46 |
#define INIT_SIZE 32 // initial scalar size to be allocated |
| 47 |
|
| 48 |
#define SB do { |
| 49 |
#define SE } while (0) |
| 50 |
|
| 51 |
#define IN_RANGE_INC(type,val,beg,end) \ |
| 52 |
((unsigned type)((unsigned type)(val) - (unsigned type)(beg)) \ |
| 53 |
<= (unsigned type)((unsigned type)(end) - (unsigned type)(beg))) |
| 54 |
|
| 55 |
#define ERR_NESTING_EXCEEDED "cbor text or perl structure exceeds maximum nesting level (max_depth set too low?)" |
| 56 |
|
| 57 |
#ifdef USE_ITHREADS |
| 58 |
# define CBOR_SLOW 1 |
| 59 |
# define CBOR_STASH (cbor_stash ? cbor_stash : gv_stashpv ("CBOR::XS", 1)) |
| 60 |
#else |
| 61 |
# define CBOR_SLOW 0 |
| 62 |
# define CBOR_STASH cbor_stash |
| 63 |
#endif |
| 64 |
|
| 65 |
static HV *cbor_stash, *types_boolean_stash, *types_error_stash, *cbor_tagged_stash; // CBOR::XS:: |
| 66 |
static SV *types_true, *types_false, *types_error, *sv_cbor; |
| 67 |
|
| 68 |
typedef struct { |
| 69 |
U32 flags; |
| 70 |
U32 max_depth; |
| 71 |
STRLEN max_size; |
| 72 |
} CBOR; |
| 73 |
|
| 74 |
ecb_inline void |
| 75 |
cbor_init (CBOR *cbor) |
| 76 |
{ |
| 77 |
Zero (cbor, 1, CBOR); |
| 78 |
cbor->max_depth = 512; |
| 79 |
} |
| 80 |
|
| 81 |
///////////////////////////////////////////////////////////////////////////// |
| 82 |
// utility functions |
| 83 |
|
| 84 |
ecb_inline SV * |
| 85 |
get_bool (const char *name) |
| 86 |
{ |
| 87 |
SV *sv = get_sv (name, 1); |
| 88 |
|
| 89 |
SvREADONLY_on (sv); |
| 90 |
SvREADONLY_on (SvRV (sv)); |
| 91 |
|
| 92 |
return sv; |
| 93 |
} |
| 94 |
|
| 95 |
ecb_inline void |
| 96 |
shrink (SV *sv) |
| 97 |
{ |
| 98 |
sv_utf8_downgrade (sv, 1); |
| 99 |
|
| 100 |
if (SvLEN (sv) > SvCUR (sv) + 1) |
| 101 |
{ |
| 102 |
#ifdef SvPV_shrink_to_cur |
| 103 |
SvPV_shrink_to_cur (sv); |
| 104 |
#elif defined (SvPV_renew) |
| 105 |
SvPV_renew (sv, SvCUR (sv) + 1); |
| 106 |
#endif |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
///////////////////////////////////////////////////////////////////////////// |
| 111 |
// fp hell |
| 112 |
|
| 113 |
//TODO |
| 114 |
|
| 115 |
///////////////////////////////////////////////////////////////////////////// |
| 116 |
// encoder |
| 117 |
|
| 118 |
// structure used for encoding CBOR |
| 119 |
typedef struct |
| 120 |
{ |
| 121 |
char *cur; // SvPVX (sv) + current output position |
| 122 |
char *end; // SvEND (sv) |
| 123 |
SV *sv; // result scalar |
| 124 |
CBOR cbor; |
| 125 |
U32 depth; // recursion level |
| 126 |
} enc_t; |
| 127 |
|
| 128 |
ecb_inline void |
| 129 |
need (enc_t *enc, STRLEN len) |
| 130 |
{ |
| 131 |
if (ecb_expect_false (enc->cur + len >= enc->end)) |
| 132 |
{ |
| 133 |
STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); |
| 134 |
SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
| 135 |
enc->cur = SvPVX (enc->sv) + cur; |
| 136 |
enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
ecb_inline void |
| 141 |
encode_ch (enc_t *enc, char ch) |
| 142 |
{ |
| 143 |
need (enc, 1); |
| 144 |
*enc->cur++ = ch; |
| 145 |
} |
| 146 |
|
| 147 |
static void |
| 148 |
encode_uint (enc_t *enc, int major, UV len) |
| 149 |
{ |
| 150 |
need (enc, 9); |
| 151 |
|
| 152 |
if (len < 24) |
| 153 |
*enc->cur++ = major | len; |
| 154 |
else if (len <= 0xff) |
| 155 |
{ |
| 156 |
*enc->cur++ = major | 24; |
| 157 |
*enc->cur++ = len; |
| 158 |
} |
| 159 |
else if (len <= 0xffff) |
| 160 |
{ |
| 161 |
*enc->cur++ = major | 25; |
| 162 |
*enc->cur++ = len >> 8; |
| 163 |
*enc->cur++ = len; |
| 164 |
} |
| 165 |
else if (len <= 0xffffffff) |
| 166 |
{ |
| 167 |
*enc->cur++ = major | 26; |
| 168 |
*enc->cur++ = len >> 24; |
| 169 |
*enc->cur++ = len >> 16; |
| 170 |
*enc->cur++ = len >> 8; |
| 171 |
*enc->cur++ = len; |
| 172 |
} |
| 173 |
else |
| 174 |
{ |
| 175 |
*enc->cur++ = major | 27; |
| 176 |
*enc->cur++ = len >> 56; |
| 177 |
*enc->cur++ = len >> 48; |
| 178 |
*enc->cur++ = len >> 40; |
| 179 |
*enc->cur++ = len >> 32; |
| 180 |
*enc->cur++ = len >> 24; |
| 181 |
*enc->cur++ = len >> 16; |
| 182 |
*enc->cur++ = len >> 8; |
| 183 |
*enc->cur++ = len; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
static void |
| 188 |
encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
| 189 |
{ |
| 190 |
encode_uint (enc, utf8 ? 0x60 : 0x40, len); |
| 191 |
need (enc, len); |
| 192 |
memcpy (enc->cur, str, len); |
| 193 |
enc->cur += len; |
| 194 |
} |
| 195 |
|
| 196 |
static void encode_sv (enc_t *enc, SV *sv); |
| 197 |
|
| 198 |
static void |
| 199 |
encode_av (enc_t *enc, AV *av) |
| 200 |
{ |
| 201 |
int i, len = av_len (av); |
| 202 |
|
| 203 |
if (enc->depth >= enc->cbor.max_depth) |
| 204 |
croak (ERR_NESTING_EXCEEDED); |
| 205 |
|
| 206 |
++enc->depth; |
| 207 |
|
| 208 |
encode_uint (enc, 0x80, len + 1); |
| 209 |
|
| 210 |
for (i = 0; i <= len; ++i) |
| 211 |
{ |
| 212 |
SV **svp = av_fetch (av, i, 0); |
| 213 |
encode_sv (enc, svp ? *svp : &PL_sv_undef); |
| 214 |
} |
| 215 |
|
| 216 |
--enc->depth; |
| 217 |
} |
| 218 |
|
| 219 |
static void |
| 220 |
encode_hv (enc_t *enc, HV *hv) |
| 221 |
{ |
| 222 |
HE *he; |
| 223 |
|
| 224 |
if (enc->depth >= enc->cbor.max_depth) |
| 225 |
croak (ERR_NESTING_EXCEEDED); |
| 226 |
|
| 227 |
++enc->depth; |
| 228 |
|
| 229 |
int pairs = hv_iterinit (hv); |
| 230 |
int mg = SvMAGICAL (hv); |
| 231 |
|
| 232 |
if (mg) |
| 233 |
encode_ch (enc, 0xa0 | 31); |
| 234 |
else |
| 235 |
encode_uint (enc, 0xa0, pairs); |
| 236 |
|
| 237 |
while ((he = hv_iternext (hv))) |
| 238 |
{ |
| 239 |
if (HeKLEN (he) == HEf_SVKEY) |
| 240 |
encode_sv (enc, HeSVKEY (he)); |
| 241 |
else |
| 242 |
encode_str (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
| 243 |
|
| 244 |
encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); |
| 245 |
} |
| 246 |
|
| 247 |
if (mg) |
| 248 |
encode_ch (enc, 0xe0 | 31); |
| 249 |
|
| 250 |
--enc->depth; |
| 251 |
} |
| 252 |
|
| 253 |
// encode objects, arrays and special \0=false and \1=true values. |
| 254 |
static void |
| 255 |
encode_rv (enc_t *enc, SV *sv) |
| 256 |
{ |
| 257 |
svtype svt; |
| 258 |
|
| 259 |
SvGETMAGIC (sv); |
| 260 |
svt = SvTYPE (sv); |
| 261 |
|
| 262 |
if (ecb_expect_false (SvOBJECT (sv))) |
| 263 |
{ |
| 264 |
HV *boolean_stash = !CBOR_SLOW || types_boolean_stash |
| 265 |
? types_boolean_stash |
| 266 |
: gv_stashpv ("Types::Serialiser::Boolean", 1); |
| 267 |
HV *error_stash = !CBOR_SLOW || types_error_stash |
| 268 |
? types_error_stash |
| 269 |
: gv_stashpv ("Types::Serialiser::Error", 1); |
| 270 |
HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
| 271 |
? cbor_tagged_stash |
| 272 |
: gv_stashpv ("CBOR::XS::Tagged" , 1); |
| 273 |
|
| 274 |
HV *stash = SvSTASH (sv); |
| 275 |
GV *method; |
| 276 |
|
| 277 |
if (stash == boolean_stash) |
| 278 |
encode_ch (enc, SvIV (sv) ? 0xe0 | 21 : 0xe0 | 20); |
| 279 |
else if (stash == error_stash) |
| 280 |
encode_ch (enc, 0xe0 | 23); |
| 281 |
else if (stash == tagged_stash) |
| 282 |
{ |
| 283 |
if (svt != SVt_PVAV) |
| 284 |
croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
| 285 |
|
| 286 |
encode_uint (enc, 0xc0, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
| 287 |
encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); |
| 288 |
} |
| 289 |
else if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
| 290 |
{ |
| 291 |
dSP; |
| 292 |
|
| 293 |
ENTER; SAVETMPS; PUSHMARK (SP); |
| 294 |
// we re-bless the reference to get overload and other niceties right |
| 295 |
XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
| 296 |
|
| 297 |
PUTBACK; |
| 298 |
// G_SCALAR ensures that return value is 1 |
| 299 |
call_sv ((SV *)GvCV (method), G_SCALAR); |
| 300 |
SPAGAIN; |
| 301 |
|
| 302 |
// catch this surprisingly common error |
| 303 |
if (SvROK (TOPs) && SvRV (TOPs) == sv) |
| 304 |
croak ("%s::TO_CBOR method returned same object as was passed instead of a new one", HvNAME (stash)); |
| 305 |
|
| 306 |
encode_sv (enc, POPs); |
| 307 |
|
| 308 |
PUTBACK; |
| 309 |
|
| 310 |
FREETMPS; LEAVE; |
| 311 |
} |
| 312 |
else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) |
| 313 |
{ |
| 314 |
dSP; |
| 315 |
|
| 316 |
ENTER; SAVETMPS; PUSHMARK (SP); |
| 317 |
EXTEND (SP, 2); |
| 318 |
// we re-bless the reference to get overload and other niceties right |
| 319 |
PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
| 320 |
PUSHs (sv_cbor); |
| 321 |
|
| 322 |
PUTBACK; |
| 323 |
int count = call_sv ((SV *)GvCV (method), G_ARRAY); |
| 324 |
SPAGAIN; |
| 325 |
|
| 326 |
// catch this surprisingly common error |
| 327 |
if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
| 328 |
croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); |
| 329 |
|
| 330 |
encode_uint (enc, 0xc0, CBOR_TAG_PERL_OBJECT); |
| 331 |
encode_uint (enc, 0x80, count + 1); |
| 332 |
encode_str (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
| 333 |
|
| 334 |
while (count) |
| 335 |
encode_sv (enc, SP[1 - count--]); |
| 336 |
|
| 337 |
PUTBACK; |
| 338 |
|
| 339 |
FREETMPS; LEAVE; |
| 340 |
} |
| 341 |
else |
| 342 |
croak ("encountered object '%s', but no TO_CBOR or FREEZE methods available on it", |
| 343 |
SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
| 344 |
} |
| 345 |
else if (svt == SVt_PVHV) |
| 346 |
encode_hv (enc, (HV *)sv); |
| 347 |
else if (svt == SVt_PVAV) |
| 348 |
encode_av (enc, (AV *)sv); |
| 349 |
else if (svt < SVt_PVAV) |
| 350 |
{ |
| 351 |
STRLEN len = 0; |
| 352 |
char *pv = svt ? SvPV (sv, len) : 0; |
| 353 |
|
| 354 |
if (len == 1 && *pv == '1') |
| 355 |
encode_ch (enc, 0xe0 | 21); |
| 356 |
else if (len == 1 && *pv == '0') |
| 357 |
encode_ch (enc, 0xe0 | 20); |
| 358 |
else if (enc->cbor.flags & F_ALLOW_UNKNOWN) |
| 359 |
encode_ch (enc, 0xe0 | 23); |
| 360 |
else |
| 361 |
croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
| 362 |
SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
| 363 |
} |
| 364 |
else if (enc->cbor.flags & F_ALLOW_UNKNOWN) |
| 365 |
encode_ch (enc, 0xe0 | 23); |
| 366 |
else |
| 367 |
croak ("encountered %s, but CBOR can only represent references to arrays or hashes", |
| 368 |
SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
| 369 |
} |
| 370 |
|
| 371 |
static void |
| 372 |
encode_nv (enc_t *enc, SV *sv) |
| 373 |
{ |
| 374 |
double nv = SvNVX (sv); |
| 375 |
|
| 376 |
need (enc, 9); |
| 377 |
|
| 378 |
if (ecb_expect_false (nv == (U32)nv)) |
| 379 |
encode_uint (enc, 0x00, (U32)nv); |
| 380 |
//TODO: maybe I32? |
| 381 |
else if (ecb_expect_false (nv == (float)nv)) |
| 382 |
{ |
| 383 |
uint32_t fp = ecb_float_to_binary32 (nv); |
| 384 |
|
| 385 |
*enc->cur++ = 0xe0 | 26; |
| 386 |
|
| 387 |
if (!ecb_big_endian ()) |
| 388 |
fp = ecb_bswap32 (fp); |
| 389 |
|
| 390 |
memcpy (enc->cur, &fp, 4); |
| 391 |
enc->cur += 4; |
| 392 |
} |
| 393 |
else |
| 394 |
{ |
| 395 |
uint64_t fp = ecb_double_to_binary64 (nv); |
| 396 |
|
| 397 |
*enc->cur++ = 0xe0 | 27; |
| 398 |
|
| 399 |
if (!ecb_big_endian ()) |
| 400 |
fp = ecb_bswap64 (fp); |
| 401 |
|
| 402 |
memcpy (enc->cur, &fp, 8); |
| 403 |
enc->cur += 8; |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
static void |
| 408 |
encode_sv (enc_t *enc, SV *sv) |
| 409 |
{ |
| 410 |
SvGETMAGIC (sv); |
| 411 |
|
| 412 |
if (SvPOKp (sv)) |
| 413 |
{ |
| 414 |
STRLEN len; |
| 415 |
char *str = SvPV (sv, len); |
| 416 |
encode_str (enc, SvUTF8 (sv), str, len); |
| 417 |
} |
| 418 |
else if (SvNOKp (sv)) |
| 419 |
encode_nv (enc, sv); |
| 420 |
else if (SvIOKp (sv)) |
| 421 |
{ |
| 422 |
if (SvIsUV (sv)) |
| 423 |
encode_uint (enc, 0x00, SvUVX (sv)); |
| 424 |
else if (SvIVX (sv) >= 0) |
| 425 |
encode_uint (enc, 0x00, SvIVX (sv)); |
| 426 |
else |
| 427 |
encode_uint (enc, 0x20, -(SvIVX (sv) + 1)); |
| 428 |
} |
| 429 |
else if (SvROK (sv)) |
| 430 |
encode_rv (enc, SvRV (sv)); |
| 431 |
else if (!SvOK (sv)) |
| 432 |
encode_ch (enc, 0xe0 | 22); |
| 433 |
else if (enc->cbor.flags & F_ALLOW_UNKNOWN) |
| 434 |
encode_ch (enc, 0xe0 | 23); |
| 435 |
else |
| 436 |
croak ("encountered perl type (%s,0x%x) that CBOR cannot handle, check your input data", |
| 437 |
SvPV_nolen (sv), (unsigned int)SvFLAGS (sv)); |
| 438 |
} |
| 439 |
|
| 440 |
static SV * |
| 441 |
encode_cbor (SV *scalar, CBOR *cbor) |
| 442 |
{ |
| 443 |
enc_t enc; |
| 444 |
|
| 445 |
enc.cbor = *cbor; |
| 446 |
enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
| 447 |
enc.cur = SvPVX (enc.sv); |
| 448 |
enc.end = SvEND (enc.sv); |
| 449 |
enc.depth = 0; |
| 450 |
|
| 451 |
SvPOK_only (enc.sv); |
| 452 |
encode_sv (&enc, scalar); |
| 453 |
|
| 454 |
SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
| 455 |
*SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
| 456 |
|
| 457 |
if (enc.cbor.flags & F_SHRINK) |
| 458 |
shrink (enc.sv); |
| 459 |
|
| 460 |
return enc.sv; |
| 461 |
} |
| 462 |
|
| 463 |
///////////////////////////////////////////////////////////////////////////// |
| 464 |
// decoder |
| 465 |
|
| 466 |
// structure used for decoding CBOR |
| 467 |
typedef struct |
| 468 |
{ |
| 469 |
U8 *cur; // current parser pointer |
| 470 |
U8 *end; // end of input string |
| 471 |
const char *err; // parse error, if != 0 |
| 472 |
CBOR cbor; |
| 473 |
U32 depth; // recursion depth |
| 474 |
U32 maxdepth; // recursion depth limit |
| 475 |
} dec_t; |
| 476 |
|
| 477 |
#define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
| 478 |
|
| 479 |
#define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") |
| 480 |
|
| 481 |
#define DEC_INC_DEPTH if (++dec->depth > dec->cbor.max_depth) ERR (ERR_NESTING_EXCEEDED) |
| 482 |
#define DEC_DEC_DEPTH --dec->depth |
| 483 |
|
| 484 |
static UV |
| 485 |
decode_uint (dec_t *dec) |
| 486 |
{ |
| 487 |
switch (*dec->cur & 31) |
| 488 |
{ |
| 489 |
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: |
| 490 |
case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: |
| 491 |
case 16: case 17: case 18: case 19: case 20: case 21: case 22: case 23: |
| 492 |
return *dec->cur++ & 31; |
| 493 |
|
| 494 |
case 24: |
| 495 |
WANT (2); |
| 496 |
dec->cur += 2; |
| 497 |
return dec->cur[-1]; |
| 498 |
|
| 499 |
case 25: |
| 500 |
WANT (3); |
| 501 |
dec->cur += 3; |
| 502 |
return (((UV)dec->cur[-2]) << 8) |
| 503 |
| ((UV)dec->cur[-1]); |
| 504 |
|
| 505 |
case 26: |
| 506 |
WANT (5); |
| 507 |
dec->cur += 5; |
| 508 |
return (((UV)dec->cur[-4]) << 24) |
| 509 |
| (((UV)dec->cur[-3]) << 16) |
| 510 |
| (((UV)dec->cur[-2]) << 8) |
| 511 |
| ((UV)dec->cur[-1]); |
| 512 |
|
| 513 |
case 27: |
| 514 |
WANT (9); |
| 515 |
dec->cur += 9; |
| 516 |
return (((UV)dec->cur[-8]) << 56) |
| 517 |
| (((UV)dec->cur[-7]) << 48) |
| 518 |
| (((UV)dec->cur[-6]) << 40) |
| 519 |
| (((UV)dec->cur[-5]) << 32) |
| 520 |
| (((UV)dec->cur[-4]) << 24) |
| 521 |
| (((UV)dec->cur[-3]) << 16) |
| 522 |
| (((UV)dec->cur[-2]) << 8) |
| 523 |
| ((UV)dec->cur[-1]); |
| 524 |
|
| 525 |
default: |
| 526 |
ERR ("corrupted CBOR data (unsupported integer minor encoding)"); |
| 527 |
} |
| 528 |
|
| 529 |
fail: |
| 530 |
return 0; |
| 531 |
} |
| 532 |
|
| 533 |
static SV *decode_sv (dec_t *dec); |
| 534 |
|
| 535 |
static SV * |
| 536 |
decode_av (dec_t *dec) |
| 537 |
{ |
| 538 |
AV *av = newAV (); |
| 539 |
|
| 540 |
DEC_INC_DEPTH; |
| 541 |
|
| 542 |
if ((*dec->cur & 31) == 31) |
| 543 |
{ |
| 544 |
++dec->cur; |
| 545 |
|
| 546 |
for (;;) |
| 547 |
{ |
| 548 |
WANT (1); |
| 549 |
|
| 550 |
if (*dec->cur == (0xe0 | 31)) |
| 551 |
{ |
| 552 |
++dec->cur; |
| 553 |
break; |
| 554 |
} |
| 555 |
|
| 556 |
av_push (av, decode_sv (dec)); |
| 557 |
} |
| 558 |
} |
| 559 |
else |
| 560 |
{ |
| 561 |
int i, len = decode_uint (dec); |
| 562 |
|
| 563 |
av_fill (av, len - 1); |
| 564 |
|
| 565 |
for (i = 0; i < len; ++i) |
| 566 |
AvARRAY (av)[i] = decode_sv (dec); |
| 567 |
} |
| 568 |
|
| 569 |
DEC_DEC_DEPTH; |
| 570 |
return newRV_noinc ((SV *)av); |
| 571 |
|
| 572 |
fail: |
| 573 |
SvREFCNT_dec (av); |
| 574 |
DEC_DEC_DEPTH; |
| 575 |
return &PL_sv_undef; |
| 576 |
} |
| 577 |
|
| 578 |
static SV * |
| 579 |
decode_hv (dec_t *dec) |
| 580 |
{ |
| 581 |
HV *hv = newHV (); |
| 582 |
|
| 583 |
DEC_INC_DEPTH; |
| 584 |
|
| 585 |
if ((*dec->cur & 31) == 31) |
| 586 |
{ |
| 587 |
++dec->cur; |
| 588 |
|
| 589 |
for (;;) |
| 590 |
{ |
| 591 |
WANT (1); |
| 592 |
|
| 593 |
if (*dec->cur == (0xe0 | 31)) |
| 594 |
{ |
| 595 |
++dec->cur; |
| 596 |
break; |
| 597 |
} |
| 598 |
|
| 599 |
SV *k = decode_sv (dec); |
| 600 |
SV *v = decode_sv (dec); |
| 601 |
|
| 602 |
hv_store_ent (hv, k, v, 0); |
| 603 |
SvREFCNT_dec (k); |
| 604 |
} |
| 605 |
} |
| 606 |
else |
| 607 |
{ |
| 608 |
int len = decode_uint (dec); |
| 609 |
|
| 610 |
while (len--) |
| 611 |
{ |
| 612 |
SV *k = decode_sv (dec); |
| 613 |
SV *v = decode_sv (dec); |
| 614 |
|
| 615 |
hv_store_ent (hv, k, v, 0); |
| 616 |
SvREFCNT_dec (k); |
| 617 |
} |
| 618 |
} |
| 619 |
|
| 620 |
DEC_DEC_DEPTH; |
| 621 |
return newRV_noinc ((SV *)hv); |
| 622 |
|
| 623 |
fail: |
| 624 |
SvREFCNT_dec (hv); |
| 625 |
DEC_DEC_DEPTH; |
| 626 |
return &PL_sv_undef; |
| 627 |
} |
| 628 |
|
| 629 |
static SV * |
| 630 |
decode_str (dec_t *dec, int utf8) |
| 631 |
{ |
| 632 |
SV *sv = 0; |
| 633 |
|
| 634 |
if ((*dec->cur & 31) == 31) |
| 635 |
{ |
| 636 |
++dec->cur; |
| 637 |
|
| 638 |
sv = newSVpvn ("", 0); |
| 639 |
|
| 640 |
// not very fast, and certainly not robust against illegal input |
| 641 |
for (;;) |
| 642 |
{ |
| 643 |
WANT (1); |
| 644 |
|
| 645 |
if (*dec->cur == (0xe0 | 31)) |
| 646 |
{ |
| 647 |
++dec->cur; |
| 648 |
break; |
| 649 |
} |
| 650 |
|
| 651 |
sv_catsv (sv, decode_sv (dec)); |
| 652 |
} |
| 653 |
} |
| 654 |
else |
| 655 |
{ |
| 656 |
STRLEN len = decode_uint (dec); |
| 657 |
|
| 658 |
WANT (len); |
| 659 |
sv = newSVpvn (dec->cur, len); |
| 660 |
dec->cur += len; |
| 661 |
} |
| 662 |
|
| 663 |
if (utf8) |
| 664 |
SvUTF8_on (sv); |
| 665 |
|
| 666 |
return sv; |
| 667 |
|
| 668 |
fail: |
| 669 |
SvREFCNT_dec (sv); |
| 670 |
return &PL_sv_undef; |
| 671 |
} |
| 672 |
|
| 673 |
static SV * |
| 674 |
decode_tagged (dec_t *dec) |
| 675 |
{ |
| 676 |
UV tag = decode_uint (dec); |
| 677 |
SV *sv = decode_sv (dec); |
| 678 |
|
| 679 |
if (tag == CBOR_TAG_MAGIC) |
| 680 |
return sv; |
| 681 |
else if (tag == CBOR_TAG_PERL_OBJECT) |
| 682 |
{ |
| 683 |
if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
| 684 |
ERR ("corrupted CBOR data (non-array perl object)"); |
| 685 |
|
| 686 |
AV *av = (AV *)SvRV (sv); |
| 687 |
int len = av_len (av) + 1; |
| 688 |
HV *stash = gv_stashsv (*av_fetch (av, 0, 1), 0); |
| 689 |
|
| 690 |
if (!stash) |
| 691 |
ERR ("cannot decode perl-object (package does not exist)"); |
| 692 |
|
| 693 |
GV *method = gv_fetchmethod_autoload (stash, "THAW", 0); |
| 694 |
|
| 695 |
if (!method) |
| 696 |
ERR ("cannot decode perl-object (package does not have a THAW method)"); |
| 697 |
|
| 698 |
dSP; |
| 699 |
|
| 700 |
ENTER; SAVETMPS; PUSHMARK (SP); |
| 701 |
EXTEND (SP, len + 1); |
| 702 |
// we re-bless the reference to get overload and other niceties right |
| 703 |
PUSHs (*av_fetch (av, 0, 1)); |
| 704 |
PUSHs (sv_cbor); |
| 705 |
|
| 706 |
int i; |
| 707 |
|
| 708 |
for (i = 1; i < len; ++i) |
| 709 |
PUSHs (*av_fetch (av, i, 1)); |
| 710 |
|
| 711 |
PUTBACK; |
| 712 |
call_sv ((SV *)GvCV (method), G_SCALAR); |
| 713 |
SPAGAIN; |
| 714 |
|
| 715 |
sv = SvREFCNT_inc (POPs); |
| 716 |
|
| 717 |
PUTBACK; |
| 718 |
|
| 719 |
FREETMPS; LEAVE; |
| 720 |
|
| 721 |
return sv; |
| 722 |
} |
| 723 |
else |
| 724 |
{ |
| 725 |
AV *av = newAV (); |
| 726 |
av_push (av, newSVuv (tag)); |
| 727 |
av_push (av, sv); |
| 728 |
|
| 729 |
HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
| 730 |
? cbor_tagged_stash |
| 731 |
: gv_stashpv ("CBOR::XS::Tagged" , 1); |
| 732 |
|
| 733 |
return sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
| 734 |
} |
| 735 |
|
| 736 |
fail: |
| 737 |
SvREFCNT_dec (sv); |
| 738 |
return &PL_sv_undef; |
| 739 |
} |
| 740 |
|
| 741 |
static SV * |
| 742 |
decode_sv (dec_t *dec) |
| 743 |
{ |
| 744 |
WANT (1); |
| 745 |
|
| 746 |
switch (*dec->cur >> 5) |
| 747 |
{ |
| 748 |
case 0: // unsigned int |
| 749 |
return newSVuv (decode_uint (dec)); |
| 750 |
case 1: // negative int |
| 751 |
return newSViv (-1 - (IV)decode_uint (dec)); |
| 752 |
case 2: // octet string |
| 753 |
return decode_str (dec, 0); |
| 754 |
case 3: // utf-8 string |
| 755 |
return decode_str (dec, 1); |
| 756 |
case 4: // array |
| 757 |
return decode_av (dec); |
| 758 |
case 5: // map |
| 759 |
return decode_hv (dec); |
| 760 |
case 6: // tag |
| 761 |
return decode_tagged (dec); |
| 762 |
case 7: // misc |
| 763 |
switch (*dec->cur++ & 31) |
| 764 |
{ |
| 765 |
case 20: |
| 766 |
#if CBOR_SLOW |
| 767 |
types_false = get_bool ("Types::Serialiser::false"); |
| 768 |
#endif |
| 769 |
return newSVsv (types_false); |
| 770 |
case 21: |
| 771 |
#if CBOR_SLOW |
| 772 |
types_true = get_bool ("Types::Serialiser::true"); |
| 773 |
#endif |
| 774 |
return newSVsv (types_true); |
| 775 |
case 22: |
| 776 |
return newSVsv (&PL_sv_undef); |
| 777 |
case 23: |
| 778 |
#if CBOR_SLOW |
| 779 |
types_error = get_bool ("Types::Serialiser::error"); |
| 780 |
#endif |
| 781 |
return newSVsv (types_error); |
| 782 |
|
| 783 |
case 25: |
| 784 |
{ |
| 785 |
WANT (2); |
| 786 |
|
| 787 |
uint16_t fp = (dec->cur[0] << 8) | dec->cur[1]; |
| 788 |
dec->cur += 2; |
| 789 |
|
| 790 |
return newSVnv (ecb_binary16_to_float (fp)); |
| 791 |
} |
| 792 |
|
| 793 |
case 26: |
| 794 |
{ |
| 795 |
uint32_t fp; |
| 796 |
WANT (4); |
| 797 |
memcpy (&fp, dec->cur, 4); |
| 798 |
dec->cur += 4; |
| 799 |
|
| 800 |
if (!ecb_big_endian ()) |
| 801 |
fp = ecb_bswap32 (fp); |
| 802 |
|
| 803 |
return newSVnv (ecb_binary32_to_float (fp)); |
| 804 |
} |
| 805 |
|
| 806 |
case 27: |
| 807 |
{ |
| 808 |
uint64_t fp; |
| 809 |
WANT (8); |
| 810 |
memcpy (&fp, dec->cur, 8); |
| 811 |
dec->cur += 8; |
| 812 |
|
| 813 |
if (!ecb_big_endian ()) |
| 814 |
fp = ecb_bswap64 (fp); |
| 815 |
|
| 816 |
return newSVnv (ecb_binary64_to_double (fp)); |
| 817 |
} |
| 818 |
|
| 819 |
// 0..19 unassigned |
| 820 |
// 24 reserved + unassigned (reserved values are not encodable) |
| 821 |
default: |
| 822 |
ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); |
| 823 |
} |
| 824 |
|
| 825 |
break; |
| 826 |
} |
| 827 |
|
| 828 |
fail: |
| 829 |
return &PL_sv_undef; |
| 830 |
} |
| 831 |
|
| 832 |
static SV * |
| 833 |
decode_cbor (SV *string, CBOR *cbor, char **offset_return) |
| 834 |
{ |
| 835 |
dec_t dec; |
| 836 |
SV *sv; |
| 837 |
|
| 838 |
/* work around bugs in 5.10 where manipulating magic values |
| 839 |
* makes perl ignore the magic in subsequent accesses. |
| 840 |
* also make a copy of non-PV values, to get them into a clean |
| 841 |
* state (SvPV should do that, but it's buggy, see below). |
| 842 |
*/ |
| 843 |
/*SvGETMAGIC (string);*/ |
| 844 |
if (SvMAGICAL (string) || !SvPOK (string)) |
| 845 |
string = sv_2mortal (newSVsv (string)); |
| 846 |
|
| 847 |
SvUPGRADE (string, SVt_PV); |
| 848 |
|
| 849 |
/* work around a bug in perl 5.10, which causes SvCUR to fail an |
| 850 |
* assertion with -DDEBUGGING, although SvCUR is documented to |
| 851 |
* return the xpv_cur field which certainly exists after upgrading. |
| 852 |
* according to nicholas clark, calling SvPOK fixes this. |
| 853 |
* But it doesn't fix it, so try another workaround, call SvPV_nolen |
| 854 |
* and hope for the best. |
| 855 |
* Damnit, SvPV_nolen still trips over yet another assertion. This |
| 856 |
* assertion business is seriously broken, try yet another workaround |
| 857 |
* for the broken -DDEBUGGING. |
| 858 |
*/ |
| 859 |
{ |
| 860 |
#ifdef DEBUGGING |
| 861 |
STRLEN offset = SvOK (string) ? sv_len (string) : 0; |
| 862 |
#else |
| 863 |
STRLEN offset = SvCUR (string); |
| 864 |
#endif |
| 865 |
|
| 866 |
if (offset > cbor->max_size && cbor->max_size) |
| 867 |
croak ("attempted decode of CBOR text of %lu bytes size, but max_size is set to %lu", |
| 868 |
(unsigned long)SvCUR (string), (unsigned long)cbor->max_size); |
| 869 |
} |
| 870 |
|
| 871 |
sv_utf8_downgrade (string, 0); |
| 872 |
|
| 873 |
dec.cbor = *cbor; |
| 874 |
dec.cur = (U8 *)SvPVX (string); |
| 875 |
dec.end = (U8 *)SvEND (string); |
| 876 |
dec.err = 0; |
| 877 |
dec.depth = 0; |
| 878 |
|
| 879 |
sv = decode_sv (&dec); |
| 880 |
|
| 881 |
if (offset_return) |
| 882 |
*offset_return = dec.cur; |
| 883 |
|
| 884 |
if (!(offset_return || !sv)) |
| 885 |
if (dec.cur != dec.end && !dec.err) |
| 886 |
dec.err = "garbage after CBOR object"; |
| 887 |
|
| 888 |
if (dec.err) |
| 889 |
{ |
| 890 |
SvREFCNT_dec (sv); |
| 891 |
croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)SvPVX (string), (int)(uint8_t)*dec.cur); |
| 892 |
} |
| 893 |
|
| 894 |
sv = sv_2mortal (sv); |
| 895 |
|
| 896 |
return sv; |
| 897 |
} |
| 898 |
|
| 899 |
///////////////////////////////////////////////////////////////////////////// |
| 900 |
// XS interface functions |
| 901 |
|
| 902 |
MODULE = CBOR::XS PACKAGE = CBOR::XS |
| 903 |
|
| 904 |
BOOT: |
| 905 |
{ |
| 906 |
cbor_stash = gv_stashpv ("CBOR::XS" , 1); |
| 907 |
cbor_tagged_stash = gv_stashpv ("CBOR::XS::Tagged" , 1); |
| 908 |
|
| 909 |
types_boolean_stash = gv_stashpv ("Types::Serialiser::Boolean", 1); |
| 910 |
types_error_stash = gv_stashpv ("Types::Serialiser::Error" , 1); |
| 911 |
|
| 912 |
types_true = get_bool ("Types::Serialiser::true" ); |
| 913 |
types_false = get_bool ("Types::Serialiser::false"); |
| 914 |
types_error = get_bool ("Types::Serialiser::error"); |
| 915 |
|
| 916 |
sv_cbor = newSVpv ("CBOR", 0); |
| 917 |
SvREADONLY_on (sv_cbor); |
| 918 |
} |
| 919 |
|
| 920 |
PROTOTYPES: DISABLE |
| 921 |
|
| 922 |
void CLONE (...) |
| 923 |
CODE: |
| 924 |
cbor_stash = 0; |
| 925 |
cbor_tagged_stash = 0; |
| 926 |
types_error_stash = 0; |
| 927 |
types_boolean_stash = 0; |
| 928 |
|
| 929 |
void new (char *klass) |
| 930 |
PPCODE: |
| 931 |
{ |
| 932 |
SV *pv = NEWSV (0, sizeof (CBOR)); |
| 933 |
SvPOK_only (pv); |
| 934 |
cbor_init ((CBOR *)SvPVX (pv)); |
| 935 |
XPUSHs (sv_2mortal (sv_bless ( |
| 936 |
newRV_noinc (pv), |
| 937 |
strEQ (klass, "CBOR::XS") ? CBOR_STASH : gv_stashpv (klass, 1) |
| 938 |
))); |
| 939 |
} |
| 940 |
|
| 941 |
void shrink (CBOR *self, int enable = 1) |
| 942 |
ALIAS: |
| 943 |
shrink = F_SHRINK |
| 944 |
allow_unknown = F_ALLOW_UNKNOWN |
| 945 |
PPCODE: |
| 946 |
{ |
| 947 |
if (enable) |
| 948 |
self->flags |= ix; |
| 949 |
else |
| 950 |
self->flags &= ~ix; |
| 951 |
|
| 952 |
XPUSHs (ST (0)); |
| 953 |
} |
| 954 |
|
| 955 |
void get_shrink (CBOR *self) |
| 956 |
ALIAS: |
| 957 |
get_shrink = F_SHRINK |
| 958 |
get_allow_unknown = F_ALLOW_UNKNOWN |
| 959 |
PPCODE: |
| 960 |
XPUSHs (boolSV (self->flags & ix)); |
| 961 |
|
| 962 |
void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
| 963 |
PPCODE: |
| 964 |
self->max_depth = max_depth; |
| 965 |
XPUSHs (ST (0)); |
| 966 |
|
| 967 |
U32 get_max_depth (CBOR *self) |
| 968 |
CODE: |
| 969 |
RETVAL = self->max_depth; |
| 970 |
OUTPUT: |
| 971 |
RETVAL |
| 972 |
|
| 973 |
void max_size (CBOR *self, U32 max_size = 0) |
| 974 |
PPCODE: |
| 975 |
self->max_size = max_size; |
| 976 |
XPUSHs (ST (0)); |
| 977 |
|
| 978 |
int get_max_size (CBOR *self) |
| 979 |
CODE: |
| 980 |
RETVAL = self->max_size; |
| 981 |
OUTPUT: |
| 982 |
RETVAL |
| 983 |
|
| 984 |
#if 0 //TODO |
| 985 |
|
| 986 |
void filter_cbor_object (CBOR *self, SV *cb = &PL_sv_undef) |
| 987 |
PPCODE: |
| 988 |
{ |
| 989 |
SvREFCNT_dec (self->cb_object); |
| 990 |
self->cb_object = SvOK (cb) ? newSVsv (cb) : 0; |
| 991 |
|
| 992 |
XPUSHs (ST (0)); |
| 993 |
} |
| 994 |
|
| 995 |
void filter_cbor_single_key_object (CBOR *self, SV *key, SV *cb = &PL_sv_undef) |
| 996 |
PPCODE: |
| 997 |
{ |
| 998 |
if (!self->cb_sk_object) |
| 999 |
self->cb_sk_object = newHV (); |
| 1000 |
|
| 1001 |
if (SvOK (cb)) |
| 1002 |
hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
| 1003 |
else |
| 1004 |
{ |
| 1005 |
hv_delete_ent (self->cb_sk_object, key, G_DISCARD, 0); |
| 1006 |
|
| 1007 |
if (!HvKEYS (self->cb_sk_object)) |
| 1008 |
{ |
| 1009 |
SvREFCNT_dec (self->cb_sk_object); |
| 1010 |
self->cb_sk_object = 0; |
| 1011 |
} |
| 1012 |
} |
| 1013 |
|
| 1014 |
XPUSHs (ST (0)); |
| 1015 |
} |
| 1016 |
|
| 1017 |
#endif |
| 1018 |
|
| 1019 |
void encode (CBOR *self, SV *scalar) |
| 1020 |
PPCODE: |
| 1021 |
PUTBACK; scalar = encode_cbor (scalar, self); SPAGAIN; |
| 1022 |
XPUSHs (scalar); |
| 1023 |
|
| 1024 |
void decode (CBOR *self, SV *cborstr) |
| 1025 |
PPCODE: |
| 1026 |
PUTBACK; cborstr = decode_cbor (cborstr, self, 0); SPAGAIN; |
| 1027 |
XPUSHs (cborstr); |
| 1028 |
|
| 1029 |
void decode_prefix (CBOR *self, SV *cborstr) |
| 1030 |
PPCODE: |
| 1031 |
{ |
| 1032 |
SV *sv; |
| 1033 |
char *offset; |
| 1034 |
PUTBACK; sv = decode_cbor (cborstr, self, &offset); SPAGAIN; |
| 1035 |
EXTEND (SP, 2); |
| 1036 |
PUSHs (sv); |
| 1037 |
PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
| 1038 |
} |
| 1039 |
|
| 1040 |
#if 0 |
| 1041 |
|
| 1042 |
void DESTROY (CBOR *self) |
| 1043 |
CODE: |
| 1044 |
SvREFCNT_dec (self->cb_sk_object); |
| 1045 |
SvREFCNT_dec (self->cb_object); |
| 1046 |
|
| 1047 |
#endif |
| 1048 |
|
| 1049 |
PROTOTYPES: ENABLE |
| 1050 |
|
| 1051 |
void encode_cbor (SV *scalar) |
| 1052 |
PPCODE: |
| 1053 |
{ |
| 1054 |
CBOR cbor; |
| 1055 |
cbor_init (&cbor); |
| 1056 |
PUTBACK; scalar = encode_cbor (scalar, &cbor); SPAGAIN; |
| 1057 |
XPUSHs (scalar); |
| 1058 |
} |
| 1059 |
|
| 1060 |
void decode_cbor (SV *cborstr) |
| 1061 |
PPCODE: |
| 1062 |
{ |
| 1063 |
CBOR cbor; |
| 1064 |
cbor_init (&cbor); |
| 1065 |
PUTBACK; cborstr = decode_cbor (cborstr, &cbor, 0); SPAGAIN; |
| 1066 |
XPUSHs (cborstr); |
| 1067 |
} |
| 1068 |
|