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