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; |
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 |
if (SvSTASH (sv) == boolean_stash) |
275 |
encode_ch (enc, SvIV (sv) ? 0xe0 | 21 : 0xe0 | 20); |
276 |
else if (SvSTASH (sv) == error_stash) |
277 |
encode_ch (enc, 0xe0 | 23); |
278 |
else if (SvSTASH (sv) == tagged_stash) |
279 |
{ |
280 |
if (svt != SVt_PVAV) |
281 |
croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
282 |
|
283 |
encode_uint (enc, 0xc0, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
284 |
encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); |
285 |
} |
286 |
else |
287 |
{ |
288 |
// we re-bless the reference to get overload and other niceties right |
289 |
GV *to_cbor = gv_fetchmethod_autoload (SvSTASH (sv), "TO_CBOR", 0); |
290 |
|
291 |
if (to_cbor) |
292 |
{ |
293 |
dSP; |
294 |
|
295 |
ENTER; SAVETMPS; PUSHMARK (SP); |
296 |
XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
297 |
|
298 |
// calling with G_SCALAR ensures that we always get a 1 return value |
299 |
PUTBACK; |
300 |
call_sv ((SV *)GvCV (to_cbor), G_SCALAR); |
301 |
SPAGAIN; |
302 |
|
303 |
// catch this surprisingly common error |
304 |
if (SvROK (TOPs) && SvRV (TOPs) == sv) |
305 |
croak ("%s::TO_CBOR method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
306 |
|
307 |
sv = POPs; |
308 |
PUTBACK; |
309 |
|
310 |
encode_sv (enc, sv); |
311 |
|
312 |
FREETMPS; LEAVE; |
313 |
} |
314 |
else |
315 |
croak ("encountered object '%s', but no TO_CBOR method available on it", |
316 |
SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
317 |
} |
318 |
} |
319 |
else if (svt == SVt_PVHV) |
320 |
encode_hv (enc, (HV *)sv); |
321 |
else if (svt == SVt_PVAV) |
322 |
encode_av (enc, (AV *)sv); |
323 |
else if (svt < SVt_PVAV) |
324 |
{ |
325 |
STRLEN len = 0; |
326 |
char *pv = svt ? SvPV (sv, len) : 0; |
327 |
|
328 |
if (len == 1 && *pv == '1') |
329 |
encode_ch (enc, 0xe0 | 21); |
330 |
else if (len == 1 && *pv == '0') |
331 |
encode_ch (enc, 0xe0 | 20); |
332 |
else if (enc->cbor.flags & F_ALLOW_UNKNOWN) |
333 |
encode_ch (enc, 0xe0 | 23); |
334 |
else |
335 |
croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
336 |
SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
337 |
} |
338 |
else if (enc->cbor.flags & F_ALLOW_UNKNOWN) |
339 |
encode_ch (enc, 0xe0 | 23); |
340 |
else |
341 |
croak ("encountered %s, but CBOR can only represent references to arrays or hashes", |
342 |
SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
343 |
} |
344 |
|
345 |
static void |
346 |
encode_nv (enc_t *enc, SV *sv) |
347 |
{ |
348 |
double nv = SvNVX (sv); |
349 |
|
350 |
need (enc, 9); |
351 |
|
352 |
if (ecb_expect_false (nv == (U32)nv)) |
353 |
encode_uint (enc, 0x00, (U32)nv); |
354 |
//TODO: maybe I32? |
355 |
else if (ecb_expect_false (nv == (float)nv)) |
356 |
{ |
357 |
uint32_t fp = ecb_float_to_binary32 (nv); |
358 |
|
359 |
*enc->cur++ = 0xe0 | 26; |
360 |
|
361 |
if (!ecb_big_endian ()) |
362 |
fp = ecb_bswap32 (fp); |
363 |
|
364 |
memcpy (enc->cur, &fp, 4); |
365 |
enc->cur += 4; |
366 |
} |
367 |
else |
368 |
{ |
369 |
uint64_t fp = ecb_double_to_binary64 (nv); |
370 |
|
371 |
*enc->cur++ = 0xe0 | 27; |
372 |
|
373 |
if (!ecb_big_endian ()) |
374 |
fp = ecb_bswap64 (fp); |
375 |
|
376 |
memcpy (enc->cur, &fp, 8); |
377 |
enc->cur += 8; |
378 |
} |
379 |
} |
380 |
|
381 |
static void |
382 |
encode_sv (enc_t *enc, SV *sv) |
383 |
{ |
384 |
SvGETMAGIC (sv); |
385 |
|
386 |
if (SvPOKp (sv)) |
387 |
{ |
388 |
STRLEN len; |
389 |
char *str = SvPV (sv, len); |
390 |
encode_str (enc, SvUTF8 (sv), str, len); |
391 |
} |
392 |
else if (SvNOKp (sv)) |
393 |
encode_nv (enc, sv); |
394 |
else if (SvIOKp (sv)) |
395 |
{ |
396 |
if (SvIsUV (sv)) |
397 |
encode_uint (enc, 0x00, SvUVX (sv)); |
398 |
else if (SvIVX (sv) >= 0) |
399 |
encode_uint (enc, 0x00, SvIVX (sv)); |
400 |
else |
401 |
encode_uint (enc, 0x20, -(SvIVX (sv) + 1)); |
402 |
} |
403 |
else if (SvROK (sv)) |
404 |
encode_rv (enc, SvRV (sv)); |
405 |
else if (!SvOK (sv)) |
406 |
encode_ch (enc, 0xe0 | 22); |
407 |
else if (enc->cbor.flags & F_ALLOW_UNKNOWN) |
408 |
encode_ch (enc, 0xe0 | 23); |
409 |
else |
410 |
croak ("encountered perl type (%s,0x%x) that CBOR cannot handle, check your input data", |
411 |
SvPV_nolen (sv), (unsigned int)SvFLAGS (sv)); |
412 |
} |
413 |
|
414 |
static SV * |
415 |
encode_cbor (SV *scalar, CBOR *cbor) |
416 |
{ |
417 |
enc_t enc; |
418 |
|
419 |
enc.cbor = *cbor; |
420 |
enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
421 |
enc.cur = SvPVX (enc.sv); |
422 |
enc.end = SvEND (enc.sv); |
423 |
enc.depth = 0; |
424 |
|
425 |
SvPOK_only (enc.sv); |
426 |
encode_sv (&enc, scalar); |
427 |
|
428 |
SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
429 |
*SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
430 |
|
431 |
if (enc.cbor.flags & F_SHRINK) |
432 |
shrink (enc.sv); |
433 |
|
434 |
return enc.sv; |
435 |
} |
436 |
|
437 |
///////////////////////////////////////////////////////////////////////////// |
438 |
// decoder |
439 |
|
440 |
// structure used for decoding CBOR |
441 |
typedef struct |
442 |
{ |
443 |
U8 *cur; // current parser pointer |
444 |
U8 *end; // end of input string |
445 |
const char *err; // parse error, if != 0 |
446 |
CBOR cbor; |
447 |
U32 depth; // recursion depth |
448 |
U32 maxdepth; // recursion depth limit |
449 |
} dec_t; |
450 |
|
451 |
#define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
452 |
|
453 |
#define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") |
454 |
|
455 |
#define DEC_INC_DEPTH if (++dec->depth > dec->cbor.max_depth) ERR (ERR_NESTING_EXCEEDED) |
456 |
#define DEC_DEC_DEPTH --dec->depth |
457 |
|
458 |
static UV |
459 |
decode_uint (dec_t *dec) |
460 |
{ |
461 |
switch (*dec->cur & 31) |
462 |
{ |
463 |
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: |
464 |
case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: |
465 |
case 16: case 17: case 18: case 19: case 20: case 21: case 22: case 23: |
466 |
return *dec->cur++ & 31; |
467 |
|
468 |
case 24: |
469 |
WANT (2); |
470 |
dec->cur += 2; |
471 |
return dec->cur[-1]; |
472 |
|
473 |
case 25: |
474 |
WANT (3); |
475 |
dec->cur += 3; |
476 |
return (((UV)dec->cur[-2]) << 8) |
477 |
| ((UV)dec->cur[-1]); |
478 |
|
479 |
case 26: |
480 |
WANT (5); |
481 |
dec->cur += 5; |
482 |
return (((UV)dec->cur[-4]) << 24) |
483 |
| (((UV)dec->cur[-3]) << 16) |
484 |
| (((UV)dec->cur[-2]) << 8) |
485 |
| ((UV)dec->cur[-1]); |
486 |
|
487 |
case 27: |
488 |
WANT (9); |
489 |
dec->cur += 9; |
490 |
return (((UV)dec->cur[-8]) << 56) |
491 |
| (((UV)dec->cur[-7]) << 48) |
492 |
| (((UV)dec->cur[-6]) << 40) |
493 |
| (((UV)dec->cur[-5]) << 32) |
494 |
| (((UV)dec->cur[-4]) << 24) |
495 |
| (((UV)dec->cur[-3]) << 16) |
496 |
| (((UV)dec->cur[-2]) << 8) |
497 |
| ((UV)dec->cur[-1]); |
498 |
|
499 |
default: |
500 |
ERR ("corrupted CBOR data (unsupported integer minor encoding)"); |
501 |
} |
502 |
|
503 |
fail: |
504 |
return 0; |
505 |
} |
506 |
|
507 |
static SV *decode_sv (dec_t *dec); |
508 |
|
509 |
static SV * |
510 |
decode_av (dec_t *dec) |
511 |
{ |
512 |
AV *av = newAV (); |
513 |
|
514 |
DEC_INC_DEPTH; |
515 |
|
516 |
if ((*dec->cur & 31) == 31) |
517 |
{ |
518 |
++dec->cur; |
519 |
|
520 |
for (;;) |
521 |
{ |
522 |
WANT (1); |
523 |
|
524 |
if (*dec->cur == (0xe0 | 31)) |
525 |
{ |
526 |
++dec->cur; |
527 |
break; |
528 |
} |
529 |
|
530 |
av_push (av, decode_sv (dec)); |
531 |
} |
532 |
} |
533 |
else |
534 |
{ |
535 |
int i, len = decode_uint (dec); |
536 |
|
537 |
av_fill (av, len - 1); |
538 |
|
539 |
for (i = 0; i < len; ++i) |
540 |
AvARRAY (av)[i] = decode_sv (dec); |
541 |
} |
542 |
|
543 |
DEC_DEC_DEPTH; |
544 |
return newRV_noinc ((SV *)av); |
545 |
|
546 |
fail: |
547 |
SvREFCNT_dec (av); |
548 |
DEC_DEC_DEPTH; |
549 |
return &PL_sv_undef; |
550 |
} |
551 |
|
552 |
static SV * |
553 |
decode_hv (dec_t *dec) |
554 |
{ |
555 |
HV *hv = newHV (); |
556 |
|
557 |
DEC_INC_DEPTH; |
558 |
|
559 |
if ((*dec->cur & 31) == 31) |
560 |
{ |
561 |
++dec->cur; |
562 |
|
563 |
for (;;) |
564 |
{ |
565 |
WANT (1); |
566 |
|
567 |
if (*dec->cur == (0xe0 | 31)) |
568 |
{ |
569 |
++dec->cur; |
570 |
break; |
571 |
} |
572 |
|
573 |
SV *k = decode_sv (dec); |
574 |
SV *v = decode_sv (dec); |
575 |
|
576 |
hv_store_ent (hv, k, v, 0); |
577 |
} |
578 |
} |
579 |
else |
580 |
{ |
581 |
int len = decode_uint (dec); |
582 |
|
583 |
while (len--) |
584 |
{ |
585 |
SV *k = decode_sv (dec); |
586 |
SV *v = decode_sv (dec); |
587 |
|
588 |
hv_store_ent (hv, k, v, 0); |
589 |
} |
590 |
} |
591 |
|
592 |
DEC_DEC_DEPTH; |
593 |
return newRV_noinc ((SV *)hv); |
594 |
|
595 |
fail: |
596 |
SvREFCNT_dec (hv); |
597 |
DEC_DEC_DEPTH; |
598 |
return &PL_sv_undef; |
599 |
} |
600 |
|
601 |
static SV * |
602 |
decode_str (dec_t *dec, int utf8) |
603 |
{ |
604 |
SV *sv = 0; |
605 |
|
606 |
if ((*dec->cur & 31) == 31) |
607 |
{ |
608 |
++dec->cur; |
609 |
|
610 |
sv = newSVpvn ("", 0); |
611 |
|
612 |
// not very fast, and certainly not robust against illegal input |
613 |
for (;;) |
614 |
{ |
615 |
WANT (1); |
616 |
|
617 |
if (*dec->cur == (0xe0 | 31)) |
618 |
{ |
619 |
++dec->cur; |
620 |
break; |
621 |
} |
622 |
|
623 |
sv_catsv (sv, decode_sv (dec)); |
624 |
} |
625 |
} |
626 |
else |
627 |
{ |
628 |
STRLEN len = decode_uint (dec); |
629 |
|
630 |
WANT (len); |
631 |
sv = newSVpvn (dec->cur, len); |
632 |
dec->cur += len; |
633 |
} |
634 |
|
635 |
if (utf8) |
636 |
SvUTF8_on (sv); |
637 |
|
638 |
return sv; |
639 |
|
640 |
fail: |
641 |
SvREFCNT_dec (sv); |
642 |
return &PL_sv_undef; |
643 |
} |
644 |
|
645 |
static SV * |
646 |
decode_tagged (dec_t *dec) |
647 |
{ |
648 |
UV tag = decode_uint (dec); |
649 |
SV *sv = decode_sv (dec); |
650 |
|
651 |
if (tag == CBOR_TAG_MAGIC) |
652 |
return sv; |
653 |
|
654 |
if (tag == CBOR_TAG_PERL_OBJECT) |
655 |
{ |
656 |
if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
657 |
ERR ("corrupted CBOR data (non-array perl object)"); |
658 |
|
659 |
// TODO |
660 |
} |
661 |
|
662 |
AV *av = newAV (); |
663 |
av_push (av, newSVuv (tag)); |
664 |
av_push (av, sv); |
665 |
|
666 |
HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
667 |
? cbor_tagged_stash |
668 |
: gv_stashpv ("CBOR::XS::Tagged" , 1); |
669 |
|
670 |
return sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
671 |
|
672 |
fail: |
673 |
SvREFCNT_dec (sv); |
674 |
return &PL_sv_undef; |
675 |
} |
676 |
|
677 |
static SV * |
678 |
decode_sv (dec_t *dec) |
679 |
{ |
680 |
WANT (1); |
681 |
|
682 |
switch (*dec->cur >> 5) |
683 |
{ |
684 |
case 0: // unsigned int |
685 |
return newSVuv (decode_uint (dec)); |
686 |
case 1: // negative int |
687 |
return newSViv (-1 - (IV)decode_uint (dec)); |
688 |
case 2: // octet string |
689 |
return decode_str (dec, 0); |
690 |
case 3: // utf-8 string |
691 |
return decode_str (dec, 1); |
692 |
case 4: // array |
693 |
return decode_av (dec); |
694 |
case 5: // map |
695 |
return decode_hv (dec); |
696 |
case 6: // tag |
697 |
return decode_tagged (dec); |
698 |
case 7: // misc |
699 |
switch (*dec->cur++ & 31) |
700 |
{ |
701 |
case 20: |
702 |
#if CBOR_SLOW |
703 |
types_false = get_bool ("Types::Serialiser::false"); |
704 |
#endif |
705 |
return newSVsv (types_false); |
706 |
case 21: |
707 |
#if CBOR_SLOW |
708 |
types_true = get_bool ("Types::Serialiser::true"); |
709 |
#endif |
710 |
return newSVsv (types_true); |
711 |
case 22: |
712 |
return newSVsv (&PL_sv_undef); |
713 |
case 23: |
714 |
#if CBOR_SLOW |
715 |
types_error = get_bool ("Types::Serialiser::error"); |
716 |
#endif |
717 |
return newSVsv (types_error); |
718 |
|
719 |
case 25: |
720 |
{ |
721 |
WANT (2); |
722 |
|
723 |
uint16_t fp = (dec->cur[0] << 8) | dec->cur[1]; |
724 |
dec->cur += 2; |
725 |
|
726 |
return newSVnv (ecb_binary16_to_float (fp)); |
727 |
} |
728 |
|
729 |
case 26: |
730 |
{ |
731 |
uint32_t fp; |
732 |
WANT (4); |
733 |
memcpy (&fp, dec->cur, 4); |
734 |
dec->cur += 4; |
735 |
|
736 |
if (!ecb_big_endian ()) |
737 |
fp = ecb_bswap32 (fp); |
738 |
|
739 |
return newSVnv (ecb_binary32_to_float (fp)); |
740 |
} |
741 |
|
742 |
case 27: |
743 |
{ |
744 |
uint64_t fp; |
745 |
WANT (8); |
746 |
memcpy (&fp, dec->cur, 8); |
747 |
dec->cur += 8; |
748 |
|
749 |
if (!ecb_big_endian ()) |
750 |
fp = ecb_bswap64 (fp); |
751 |
|
752 |
return newSVnv (ecb_binary64_to_double (fp)); |
753 |
} |
754 |
|
755 |
// 0..19 unassigned |
756 |
// 24 reserved + unassigned (reserved values are not encodable) |
757 |
default: |
758 |
ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); |
759 |
} |
760 |
|
761 |
break; |
762 |
} |
763 |
|
764 |
fail: |
765 |
return &PL_sv_undef; |
766 |
} |
767 |
|
768 |
static SV * |
769 |
decode_cbor (SV *string, CBOR *cbor, char **offset_return) |
770 |
{ |
771 |
dec_t dec; |
772 |
SV *sv; |
773 |
|
774 |
/* work around bugs in 5.10 where manipulating magic values |
775 |
* makes perl ignore the magic in subsequent accesses. |
776 |
* also make a copy of non-PV values, to get them into a clean |
777 |
* state (SvPV should do that, but it's buggy, see below). |
778 |
*/ |
779 |
/*SvGETMAGIC (string);*/ |
780 |
if (SvMAGICAL (string) || !SvPOK (string)) |
781 |
string = sv_2mortal (newSVsv (string)); |
782 |
|
783 |
SvUPGRADE (string, SVt_PV); |
784 |
|
785 |
/* work around a bug in perl 5.10, which causes SvCUR to fail an |
786 |
* assertion with -DDEBUGGING, although SvCUR is documented to |
787 |
* return the xpv_cur field which certainly exists after upgrading. |
788 |
* according to nicholas clark, calling SvPOK fixes this. |
789 |
* But it doesn't fix it, so try another workaround, call SvPV_nolen |
790 |
* and hope for the best. |
791 |
* Damnit, SvPV_nolen still trips over yet another assertion. This |
792 |
* assertion business is seriously broken, try yet another workaround |
793 |
* for the broken -DDEBUGGING. |
794 |
*/ |
795 |
{ |
796 |
#ifdef DEBUGGING |
797 |
STRLEN offset = SvOK (string) ? sv_len (string) : 0; |
798 |
#else |
799 |
STRLEN offset = SvCUR (string); |
800 |
#endif |
801 |
|
802 |
if (offset > cbor->max_size && cbor->max_size) |
803 |
croak ("attempted decode of CBOR text of %lu bytes size, but max_size is set to %lu", |
804 |
(unsigned long)SvCUR (string), (unsigned long)cbor->max_size); |
805 |
} |
806 |
|
807 |
sv_utf8_downgrade (string, 0); |
808 |
|
809 |
dec.cbor = *cbor; |
810 |
dec.cur = (U8 *)SvPVX (string); |
811 |
dec.end = (U8 *)SvEND (string); |
812 |
dec.err = 0; |
813 |
dec.depth = 0; |
814 |
|
815 |
sv = decode_sv (&dec); |
816 |
|
817 |
if (offset_return) |
818 |
*offset_return = dec.cur; |
819 |
|
820 |
if (!(offset_return || !sv)) |
821 |
if (dec.cur != dec.end && !dec.err) |
822 |
dec.err = "garbage after CBOR object"; |
823 |
|
824 |
if (dec.err) |
825 |
{ |
826 |
SvREFCNT_dec (sv); |
827 |
croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)SvPVX (string), (int)(uint8_t)*dec.cur); |
828 |
} |
829 |
|
830 |
sv = sv_2mortal (sv); |
831 |
|
832 |
return sv; |
833 |
} |
834 |
|
835 |
///////////////////////////////////////////////////////////////////////////// |
836 |
// XS interface functions |
837 |
|
838 |
MODULE = CBOR::XS PACKAGE = CBOR::XS |
839 |
|
840 |
BOOT: |
841 |
{ |
842 |
cbor_stash = gv_stashpv ("CBOR::XS" , 1); |
843 |
cbor_tagged_stash = gv_stashpv ("CBOR::XS::Tagged" , 1); |
844 |
|
845 |
types_boolean_stash = gv_stashpv ("Types::Serialiser::Boolean", 1); |
846 |
types_error_stash = gv_stashpv ("Types::Serialiser::Error" , 1); |
847 |
|
848 |
types_true = get_bool ("Types::Serialiser::true" ); |
849 |
types_false = get_bool ("Types::Serialiser::false"); |
850 |
types_error = get_bool ("Types::Serialiser::error"); |
851 |
} |
852 |
|
853 |
PROTOTYPES: DISABLE |
854 |
|
855 |
void CLONE (...) |
856 |
CODE: |
857 |
cbor_stash = 0; |
858 |
cbor_tagged_stash = 0; |
859 |
types_error_stash = 0; |
860 |
types_boolean_stash = 0; |
861 |
|
862 |
void new (char *klass) |
863 |
PPCODE: |
864 |
{ |
865 |
SV *pv = NEWSV (0, sizeof (CBOR)); |
866 |
SvPOK_only (pv); |
867 |
cbor_init ((CBOR *)SvPVX (pv)); |
868 |
XPUSHs (sv_2mortal (sv_bless ( |
869 |
newRV_noinc (pv), |
870 |
strEQ (klass, "CBOR::XS") ? CBOR_STASH : gv_stashpv (klass, 1) |
871 |
))); |
872 |
} |
873 |
|
874 |
void shrink (CBOR *self, int enable = 1) |
875 |
ALIAS: |
876 |
shrink = F_SHRINK |
877 |
allow_unknown = F_ALLOW_UNKNOWN |
878 |
PPCODE: |
879 |
{ |
880 |
if (enable) |
881 |
self->flags |= ix; |
882 |
else |
883 |
self->flags &= ~ix; |
884 |
|
885 |
XPUSHs (ST (0)); |
886 |
} |
887 |
|
888 |
void get_shrink (CBOR *self) |
889 |
ALIAS: |
890 |
get_shrink = F_SHRINK |
891 |
get_allow_unknown = F_ALLOW_UNKNOWN |
892 |
PPCODE: |
893 |
XPUSHs (boolSV (self->flags & ix)); |
894 |
|
895 |
void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
896 |
PPCODE: |
897 |
self->max_depth = max_depth; |
898 |
XPUSHs (ST (0)); |
899 |
|
900 |
U32 get_max_depth (CBOR *self) |
901 |
CODE: |
902 |
RETVAL = self->max_depth; |
903 |
OUTPUT: |
904 |
RETVAL |
905 |
|
906 |
void max_size (CBOR *self, U32 max_size = 0) |
907 |
PPCODE: |
908 |
self->max_size = max_size; |
909 |
XPUSHs (ST (0)); |
910 |
|
911 |
int get_max_size (CBOR *self) |
912 |
CODE: |
913 |
RETVAL = self->max_size; |
914 |
OUTPUT: |
915 |
RETVAL |
916 |
|
917 |
#if 0 //TODO |
918 |
|
919 |
void filter_cbor_object (CBOR *self, SV *cb = &PL_sv_undef) |
920 |
PPCODE: |
921 |
{ |
922 |
SvREFCNT_dec (self->cb_object); |
923 |
self->cb_object = SvOK (cb) ? newSVsv (cb) : 0; |
924 |
|
925 |
XPUSHs (ST (0)); |
926 |
} |
927 |
|
928 |
void filter_cbor_single_key_object (CBOR *self, SV *key, SV *cb = &PL_sv_undef) |
929 |
PPCODE: |
930 |
{ |
931 |
if (!self->cb_sk_object) |
932 |
self->cb_sk_object = newHV (); |
933 |
|
934 |
if (SvOK (cb)) |
935 |
hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
936 |
else |
937 |
{ |
938 |
hv_delete_ent (self->cb_sk_object, key, G_DISCARD, 0); |
939 |
|
940 |
if (!HvKEYS (self->cb_sk_object)) |
941 |
{ |
942 |
SvREFCNT_dec (self->cb_sk_object); |
943 |
self->cb_sk_object = 0; |
944 |
} |
945 |
} |
946 |
|
947 |
XPUSHs (ST (0)); |
948 |
} |
949 |
|
950 |
#endif |
951 |
|
952 |
void encode (CBOR *self, SV *scalar) |
953 |
PPCODE: |
954 |
PUTBACK; scalar = encode_cbor (scalar, self); SPAGAIN; |
955 |
XPUSHs (scalar); |
956 |
|
957 |
void decode (CBOR *self, SV *cborstr) |
958 |
PPCODE: |
959 |
PUTBACK; cborstr = decode_cbor (cborstr, self, 0); SPAGAIN; |
960 |
XPUSHs (cborstr); |
961 |
|
962 |
void decode_prefix (CBOR *self, SV *cborstr) |
963 |
PPCODE: |
964 |
{ |
965 |
SV *sv; |
966 |
char *offset; |
967 |
PUTBACK; sv = decode_cbor (cborstr, self, &offset); SPAGAIN; |
968 |
EXTEND (SP, 2); |
969 |
PUSHs (sv); |
970 |
PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
971 |
} |
972 |
|
973 |
#if 0 |
974 |
|
975 |
void DESTROY (CBOR *self) |
976 |
CODE: |
977 |
SvREFCNT_dec (self->cb_sk_object); |
978 |
SvREFCNT_dec (self->cb_object); |
979 |
|
980 |
#endif |
981 |
|
982 |
PROTOTYPES: ENABLE |
983 |
|
984 |
void encode_cbor (SV *scalar) |
985 |
PPCODE: |
986 |
{ |
987 |
CBOR cbor; |
988 |
cbor_init (&cbor); |
989 |
PUTBACK; scalar = encode_cbor (scalar, &cbor); SPAGAIN; |
990 |
XPUSHs (scalar); |
991 |
} |
992 |
|
993 |
void decode_cbor (SV *cborstr) |
994 |
PPCODE: |
995 |
{ |
996 |
CBOR cbor; |
997 |
cbor_init (&cbor); |
998 |
PUTBACK; cborstr = decode_cbor (cborstr, &cbor, 0); SPAGAIN; |
999 |
XPUSHs (cborstr); |
1000 |
} |
1001 |
|