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