libsfparse.xs

filter: Statementstmt 98.1% 216 / 220 Branchbran 87.8% 72 / 82 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 95.3% 288 / 302 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/sfparse.c ↑ Summary blib/lib/HTTP/libsfparse.pm »
1 #define PERL_NO_GET_CONTEXT 1
2  
3 #include "EXTERN.h"
4 #include "perl.h"
5 #include "XSUB.h"
6  
7 #include "ppport.h"
8  
9 #define MATH_INT64_NATIVE_IF_AVAILABLE
10 #include "perl_math_int64.h"
11 #include "sfparse.h"
12  
13 /* ------------------------------------------------------------------------
14 * Parser state.
15 *
16 * libsfparse is a pull parser with a state machine of its own: every entry
17 * point knows which calls may follow the last one, and calls abort() if it
18 * is called somewhere that state machine does not allow. That is not
19 * something a Perl program can catch, so it must not be reachable.
20 *
21 * It is not: each of the three functions this module exposes drives one
22 * parser from start to finish inside a single call, in the order the state
23 * machine expects, and nothing is shared between calls. No sequence of
24 * calls from Perl can put a parser somewhere the library does not expect,
25 * because Perl never sees a parser at all.
26 *
27 * Keep that true when editing below. The library is vendored unmodified, so
28 * a mistake here is a core dump rather than an exception.
29 *
30 * The only state carried here is `failed', which pins the offset of a parse
31 * error down while the input copy is still allocated, so that the croak
32 * after Safefree() never touches a freed pointer.
33 * ------------------------------------------------------------------------ */
34  
35 /* Which top level entry point to call. A selector for one call, not
36 remembered state: the library's own state machine is what refuses a field
37 value read as more than one of the three shapes. */
38 #define SFP_TOP_DICT 0
39 #define SFP_TOP_LIST 1
40 #define SFP_TOP_ITEM 2
41  
42 typedef struct sfp_wrap {
43 sfparse_parser sfp;
44 char *data; /* owned copy of the input, NULL when it is empty */
45 U8 failed; /* a parse error was reported */
46 UV err_off; /* byte offset where that error was reported */
47 } sfp_wrap;
48  
49 /* Byte offset the parser has reached, for diagnostics. sfparse_parser.pos
50 is nominally private, but its offset within the struct comes from the
51 header we compile against, so reading it is ABI-safe. */
52 9663 static UV sfp_offset(const sfp_wrap *w) {
53 9663 ▶ if (w->sfp.pos == NULL || w->data == NULL) {
Branch: 2 branches
BranchTrueFalseTotal
if (w->sfp.pos == NULL || w->data == NULL) { 9570 93 9663
if (w->sfp.pos == NULL || w->data == NULL) { 0 9570 9570
54 93 return 0;
55 }
56  
57 9570 return (UV)(w->sfp.pos - (const uint8_t *)w->data);
58 }
59  
60 /* Pin down the error offset while the input copy is still allocated, so that
61 a croak after Safefree() never touches the freed pointer. */
62 19326 static void sfp_mark_failed(sfp_wrap *w) {
63 19326 ▶ if (!w->failed) {
Branch: 1 branch
BranchTrueFalseTotal
if (!w->failed) { 9663 9663 19326
64 9663 w->err_off = sfp_offset(w);
65 9663 w->failed = 1;
66 }
67 19326 }
68  
69 9663 static void sfp_croak_parse(pTHX_ const sfp_wrap *w) {
70 9663 ▶ croak("HTTP::libsfparse: parse error at offset %" UVuf,
Branch: 1 branch
BranchTrueFalseTotal
croak("HTTP::libsfparse: parse error at offset %" UVuf, 9663 0 9663
71 w->failed ? w->err_off : sfp_offset(w));
72 }
73  
74 /* ------------------------------------------------------------------------
75 * Input handling
76 * ------------------------------------------------------------------------ */
77  
78 /* Structured field values are byte strings. Accept a byte string, or a
79 character string that is losslessly representable as bytes; refuse
80 anything else rather than silently parsing utf8 internals. */
81 12770 static const char *sfp_input(pTHX_ SV *sv, STRLEN *lenp) {
82 12770 ▶ SvGETMAGIC(sv);
Branch: 2 branches
BranchTrueFalseTotal
SvGETMAGIC(sv); 0 12770 12770
SvGETMAGIC(sv); 0 0 0
83  
84 12770 ▶ if (!SvOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(sv)) { 1 12769 12770
85 1 croak("HTTP::libsfparse: input is undefined");
86 }
87  
88 12769 ▶ if (SvUTF8(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(sv)) { 3 12766 12769
89 3 SV *tmp = newSVsv_nomg(sv);
90  
91 3 ▶ if (!sv_utf8_downgrade(tmp, TRUE)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_utf8_downgrade(tmp, TRUE)) { 2 1 3
92 2 croak("HTTP::libsfparse: input must be a byte string, but it contains "
93 "characters above U+00FF; encode it first");
94 }
95  
96 1 return SvPV_nomg_const(tmp, *lenp);
97 }
98  
99 12766 return SvPV_nomg_const(sv, *lenp);
100 }
101  
102 /* ------------------------------------------------------------------------
103 * Value conversion
104 * ------------------------------------------------------------------------ */
105  
106 16937 static SV *sfp_int_sv(pTHX_ int64_t v) {
107 16937 return newSVi64(v);
108 }
109  
110 22353 static SV *sfp_vec_sv(pTHX_ const sfparse_vec *v) {
111 22353 ▶ if (v->len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (v->len == 0) { 8 22345 22353
112 8 return newSVpvn("", 0);
113 }
114  
115 22345 return newSVpvn((const char *)v->base, v->len);
116 }
117  
118 typedef void (*sfp_decoder)(sfparse_vec *dest, const sfparse_vec *src);
119  
120 /* sfparse_unescape(), sfparse_base64decode() and sfparse_pctdecode() all
121 write at most src->len bytes, so one buffer of that size always fits. */
122 50 static SV *sfp_decode_sv(pTHX_ const sfparse_vec *src, sfp_decoder decode) {
123 SV *sv;
124 sfparse_vec dest;
125  
126 50 ▶ if (src->len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (src->len == 0) { 8 42 50
127 8 return newSVpvn("", 0);
128 }
129  
130 42 sv = newSV(0);
131 42 sv_grow(sv, src->len + 1);
132 42 SvPOK_only(sv);
133  
134 42 dest.base = (uint8_t *)SvPVX(sv);
135 42 dest.len = 0;
136  
137 42 decode(&dest, src);
138  
139 assert(dest.len <= src->len);
140  
141 42 SvCUR_set(sv, dest.len);
142 42 *SvEND(sv) = '\0';
143  
144 42 ▶ SvTAINT(sv);
Branch: 2 branches
BranchTrueFalseTotal
SvTAINT(sv); 0 42 42
SvTAINT(sv); 0 0 0
145  
146 42 return sv;
147 }
148  
149 /* Every value is an array of three elements:
150  
151 [ SFPARSE_TYPE_*, value, params ]
152  
153 so that a caller can index into any of them without first looking at the
154 type. `value' is left unset for an inner list, whose members the builder
155 fills in.
156  
157 A dictionary member and a parameter are each a [ key, item ] pair.
158  
159 These names are the ones exported to Perl, so that a caller can index a
160 value by name rather than by number, and both sides of the binding take
161 the layout from this one place. */
162  
163 #define SFP_TYPE 0
164 #define SFP_VALUE 1
165 #define SFP_PARAMS 2
166  
167 #define SFP_KEY 0
168 #define SFP_ITEM 1
169  
170 #define SFP_ITEM_WIDTH 3
171 #define SFP_PAIR_WIDTH 2
172  
173 30583 static SV *sfp_make_value(pTHX_ const sfparse_value *val) {
174 30583 AV *av = newAV();
175 30583 SV *ref = newRV_noinc((SV *)av);
176  
177 /* Fill to the full width up front: the slots left alone read as undef. */
178 30583 av_fill(av, SFP_ITEM_WIDTH - 1);
179  
180 30583 switch (val->type) {
181 2528 case SFPARSE_TYPE_BOOLEAN:
182 2528 (void)av_store(av, SFP_VALUE, newSViv(val->boolean ? 1 : 0));
183  
184 2528 break;
185 16937 case SFPARSE_TYPE_INTEGER:
186 case SFPARSE_TYPE_DATE:
187 16937 (void)av_store(av, SFP_VALUE, sfp_int_sv(aTHX_ val->integer));
188  
189 16937 break;
190 2182 case SFPARSE_TYPE_DECIMAL:
191 2182 (void)av_store(av, SFP_VALUE,
192 newSVnv((NV)val->decimal.numer / (NV)val->decimal.denom));
193  
194 2182 break;
195 2144 case SFPARSE_TYPE_STRING:
196 2144 ▶ if (val->flags & SFPARSE_VALUE_FLAG_ESCAPED_STRING) {
Branch: 1 branch
BranchTrueFalseTotal
if (val->flags & SFPARSE_VALUE_FLAG_ESCAPED_STRING) { 8 2136 2144
197 8 (void)av_store(av, SFP_VALUE,
198 sfp_decode_sv(aTHX_ & val->vec, sfparse_unescape));
199 } else {
200 2136 (void)av_store(av, SFP_VALUE, sfp_vec_sv(aTHX_ & val->vec));
201 }
202  
203 2144 break;
204 3555 case SFPARSE_TYPE_TOKEN:
205 3555 (void)av_store(av, SFP_VALUE, sfp_vec_sv(aTHX_ & val->vec));
206  
207 3555 break;
208 25 case SFPARSE_TYPE_BYTESEQ:
209 25 (void)av_store(av, SFP_VALUE,
210 sfp_decode_sv(aTHX_ & val->vec, sfparse_base64decode));
211  
212 25 break;
213 17 case SFPARSE_TYPE_DISPSTRING: {
214 /* The parser has already validated the utf8, so flag the result as
215 characters rather than handing back percent-encoded bytes. */
216 17 SV *sv = sfp_decode_sv(aTHX_ & val->vec, sfparse_pctdecode);
217  
218 17 SvUTF8_on(sv);
219  
220 17 (void)av_store(av, SFP_VALUE, sv);
221  
222 17 break;
223 }
224 3195 case SFPARSE_TYPE_INNER_LIST:
225 /* The members are the caller's to fill in. */
226  
227 3195 break;
228 0 default:
229 0 SvREFCNT_dec(ref);
230 0 croak("HTTP::libsfparse: unknown sfparse value type %d", (int)val->type);
231 }
232  
233 30583 (void)av_store(av, SFP_TYPE, newSViv((IV)val->type));
234  
235 30583 return ref;
236 }
237  
238 /* ------------------------------------------------------------------------
239 * The library's entry points. These are the whole of the parsing logic:
240 * call libsfparse and remember a parse error, so that its offset is still
241 * available once the input copy has been freed. The return code is the
242 * library's own: 0, SFPARSE_ERR_EOF or SFPARSE_ERR_PARSE.
243 * ------------------------------------------------------------------------ */
244  
245 30427 static int sfp_param(sfp_wrap *w, sfparse_vec *key, sfparse_value *val) {
246 30427 int rv = sfparse_parser_param(&w->sfp, key, val);
247  
248 30427 ▶ if (rv != 0 && rv != SFPARSE_ERR_EOF) {
Branch: 2 branches
BranchTrueFalseTotal
if (rv != 0 && rv != SFPARSE_ERR_EOF) { 20373 10054 30427
if (rv != 0 && rv != SFPARSE_ERR_EOF) { 187 20186 20373
249 187 sfp_mark_failed(w);
250 }
251  
252 30427 return rv;
253 }
254  
255 12531 static int sfp_inner_list(sfp_wrap *w, sfparse_value *val) {
256 12531 int rv = sfparse_parser_inner_list(&w->sfp, val);
257  
258 12531 ▶ if (rv != 0 && rv != SFPARSE_ERR_EOF) {
Branch: 2 branches
BranchTrueFalseTotal
if (rv != 0 && rv != SFPARSE_ERR_EOF) { 3193 9338 12531
if (rv != 0 && rv != SFPARSE_ERR_EOF) { 154 3039 3193
259 154 sfp_mark_failed(w);
260 }
261  
262 12531 return rv;
263 }
264  
265 23617 static int sfp_top(sfp_wrap *w, U8 which, sfparse_vec *key,
266 sfparse_value *val) {
267 int rv;
268  
269 23617 switch (which) {
270 12079 case SFP_TOP_DICT:
271 12079 rv = sfparse_parser_dict(&w->sfp, key, val);
272  
273 12079 break;
274 6308 case SFP_TOP_LIST:
275 6308 rv = sfparse_parser_list(&w->sfp, val);
276  
277 6308 break;
278 5230 default:
279 5230 rv = sfparse_parser_item(&w->sfp, val);
280  
281 5230 break;
282 }
283  
284 23617 ▶ if (rv != 0 && rv != SFPARSE_ERR_EOF) {
Branch: 2 branches
BranchTrueFalseTotal
if (rv != 0 && rv != SFPARSE_ERR_EOF) { 12426 11191 23617
if (rv != 0 && rv != SFPARSE_ERR_EOF) { 9322 3104 12426
285 9322 sfp_mark_failed(w);
286 }
287  
288 23617 return rv;
289 }
290  
291 /* ------------------------------------------------------------------------
292 * One shot builders. On a parse error they free what they have built and
293 * return NULL, so that the XSUB can croak without leaking.
294 * ------------------------------------------------------------------------ */
295  
296 static SV *sfp_build_params(pTHX_ sfp_wrap *w);
297 static SV *sfp_build_item(pTHX_ sfp_wrap *w, const sfparse_value *val);
298  
299 /* Parameters are an array of [ key, value ] pairs in field order, keeping
300 duplicate keys: RFC 9651 says the last of a repeated key wins, but only a
301 caller that can see them all can reject a field that repeats one. */
302 20373 static SV *sfp_build_params(pTHX_ sfp_wrap *w) {
303 20373 AV *av = newAV();
304 20373 SV *ref = newRV_noinc((SV *)av);
305  
306 10054 for (;;) {
307 sfparse_vec key;
308 sfparse_value val;
309 AV *pair;
310 30427 int rv = sfp_param(w, &key, &val);
311  
312 30427 ▶ if (rv == SFPARSE_ERR_EOF) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv == SFPARSE_ERR_EOF) { 20186 10241 30427
313 20186 break;
314 }
315  
316 10241 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 187 10054 10241
317 187 SvREFCNT_dec(ref);
318  
319 187 return NULL;
320 }
321  
322 /* A parameter value is always a bare item, never an inner list, so it
323 needs no recursion into sfp_build_item(). */
324 10054 pair = newAV();
325  
326 10054 av_fill(pair, SFP_PAIR_WIDTH - 1);
327 10054 (void)av_store(pair, SFP_KEY, sfp_vec_sv(aTHX_ & key));
328 10054 (void)av_store(pair, SFP_ITEM, sfp_make_value(aTHX_ & val));
329 10054 av_push(av, newRV_noinc((SV *)pair));
330 }
331  
332 20186 return ref;
333 }
334  
335 20529 static SV *sfp_build_item(pTHX_ sfp_wrap *w, const sfparse_value *val) {
336 20529 SV *ref = sfp_make_value(aTHX_ val);
337 20529 AV *av = (AV *)SvRV(ref);
338 SV *params;
339  
340 20529 ▶ if (val->type == SFPARSE_TYPE_INNER_LIST) {
Branch: 1 branch
BranchTrueFalseTotal
if (val->type == SFPARSE_TYPE_INNER_LIST) { 3195 17334 20529
341 3195 AV *members = newAV();
342  
343 3195 (void)av_store(av, SFP_VALUE, newRV_noinc((SV *)members));
344  
345 9336 for (;;) {
346 sfparse_value inner;
347 SV *item;
348 12531 int rv = sfp_inner_list(w, &inner);
349  
350 12531 ▶ if (rv == SFPARSE_ERR_EOF) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv == SFPARSE_ERR_EOF) { 3039 9492 12531
351 3039 break;
352 }
353  
354 9492 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 154 9338 9492
355 154 SvREFCNT_dec(ref);
356  
357 156 return NULL;
358 }
359  
360 9338 item = sfp_build_item(aTHX_ w, &inner);
361  
362 9338 ▶ if (item == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (item == NULL) { 2 9336 9338
363 2 SvREFCNT_dec(ref);
364  
365 2 return NULL;
366 }
367  
368 9336 av_push(members, item);
369 }
370 }
371  
372 20373 params = sfp_build_params(aTHX_ w);
373  
374 20373 ▶ if (params == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (params == NULL) { 187 20186 20373
375 187 SvREFCNT_dec(ref);
376  
377 187 return NULL;
378 }
379  
380 20186 (void)av_store(av, SFP_PARAMS, params);
381  
382 20186 return ref;
383 }
384  
385 /* ------------------------------------------------------------------------
386 * Setting a parser up
387 * ------------------------------------------------------------------------ */
388  
389 /* The library points straight into the bytes it is given and keeps no copy,
390 so take one: that way nothing the input SV does later -- being freed,
391 reallocated or modified by magic -- can move the ground under the parser
392 mid-call. */
393 12770 static void sfp_init(pTHX_ sfp_wrap *w, SV *input) {
394 const char *s;
395 STRLEN len;
396  
397 12770 s = sfp_input(aTHX_ input, &len);
398  
399 /* The HTTPWG test suite requires large fields, but most modern web servers limit fields to 8KiB */
400 12767 ▶ if (len > 32767) {
Branch: 1 branch
BranchTrueFalseTotal
if (len > 32767) { 0 12767 12767
401 0 croak("HTTP::libsfparse: field is too large");
402 }
403  
404 12767 Zero(w, 1, sfp_wrap);
405  
406 12767 ▶ if (len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (len > 0) { 12488 279 12767
407 12488 Newx(w->data, len, char);
408 12488 Copy(s, w->data, len, char);
409 }
410  
411 12767 sfparse_parser_init(&w->sfp, (const uint8_t *)w->data, len);
412 12767 }
413  
414 MODULE = HTTP::libsfparse PACKAGE = HTTP::libsfparse PREFIX = sfp_xs_
415  
416 PROTOTYPES: DISABLE
417  
418 # The type constants are the library's own enumerators, taken from the header
419 # this is compiled against rather than written out here, so that re-vendoring
420 # a libsfparse whose enum has grown or been reordered cannot leave Perl with
421 # stale numbers. The index constants come from the #defines above, which is
422 # also what the code building the structures uses.
423  
424 BOOT:
425 11 ▶ PERL_MATH_INT64_LOAD_OR_CROAK;
Branch: 1 branch
BranchTrueFalseTotal
PERL_MATH_INT64_LOAD_OR_CROAK; 0 11 11
426 {
427 static const struct {
428 const char *name;
429 int value;
430 } constants[] = {
431 {"SFPARSE_TYPE_BOOLEAN", SFPARSE_TYPE_BOOLEAN},
432 {"SFPARSE_TYPE_INTEGER", SFPARSE_TYPE_INTEGER},
433 {"SFPARSE_TYPE_DECIMAL", SFPARSE_TYPE_DECIMAL},
434 {"SFPARSE_TYPE_STRING", SFPARSE_TYPE_STRING},
435 {"SFPARSE_TYPE_TOKEN", SFPARSE_TYPE_TOKEN},
436 {"SFPARSE_TYPE_BYTESEQ", SFPARSE_TYPE_BYTESEQ},
437 {"SFPARSE_TYPE_INNER_LIST", SFPARSE_TYPE_INNER_LIST},
438 {"SFPARSE_TYPE_DATE", SFPARSE_TYPE_DATE},
439 {"SFPARSE_TYPE_DISPSTRING", SFPARSE_TYPE_DISPSTRING},
440 {"SFP_TYPE", SFP_TYPE},
441 {"SFP_VALUE", SFP_VALUE},
442 {"SFP_PARAMS", SFP_PARAMS},
443 {"SFP_KEY", SFP_KEY},
444 {"SFP_ITEM", SFP_ITEM}
445 };
446 11 HV *stash = gv_stashpvs("HTTP::libsfparse", GV_ADD);
447 size_t i;
448 165 ▶ for (i = 0; i < sizeof(constants) / sizeof(constants[0]); ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < sizeof(constants) / sizeof(constants[0]); ++i) { 154 11 165
449 154 newCONSTSUB(stash, constants[i].name, newSViv((IV)constants[i].value));
450 }
451 }
452  
453 SV *
454 sfp_xs_parse_item(input)
455 SV *input
456 PREINIT:
457 sfp_wrap w;
458 sfparse_value val;
459 SV *item;
460 int rv;
461 CODE:
462 3955 sfp_init(aTHX_ &w, input);
463  
464 3952 rv = sfp_top(&w, SFP_TOP_ITEM, NULL, &val);
465  
466 3952 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 2563 1389 3952
467 2563 sfp_mark_failed(&w);
468 2563 Safefree(w.data);
469 2563 sfp_croak_parse(aTHX_ &w);
470 }
471  
472 1389 item = sfp_build_item(aTHX_ &w, &val);
473  
474 1389 ▶ if (item == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (item == NULL) { 111 1278 1389
475 111 sfp_mark_failed(&w);
476 111 Safefree(w.data);
477 111 sfp_croak_parse(aTHX_ &w);
478 }
479  
480 /* A second read must report EOF, otherwise there is trailing garbage. */
481 1278 rv = sfp_top(&w, SFP_TOP_ITEM, NULL, NULL);
482  
483 1278 ▶ if (rv != SFPARSE_ERR_EOF) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != SFPARSE_ERR_EOF) { 682 596 1278
484 682 sfp_mark_failed(&w);
485 }
486  
487 1278 Safefree(w.data);
488  
489 1278 ▶ if (rv != SFPARSE_ERR_EOF) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != SFPARSE_ERR_EOF) { 682 596 1278
490 682 SvREFCNT_dec(item);
491 682 sfp_croak_parse(aTHX_ &w);
492 }
493  
494 596 RETVAL = item;
495 OUTPUT:
496 RETVAL
497  
498 SV *
499 sfp_xs_parse_list(input)
500 SV *input
501 PREINIT:
502 sfp_wrap w;
503 AV *av;
504 SV *ref;
505 CODE:
506 3344 sfp_init(aTHX_ &w, input);
507  
508 3344 av = newAV();
509 3344 ref = newRV_noinc((SV *)av);
510  
511 2964 for (;;) {
512 sfparse_value val;
513 SV *item;
514 6308 int rv = sfp_top(&w, SFP_TOP_LIST, NULL, &val);
515  
516 6308 ▶ if (rv == SFPARSE_ERR_EOF) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv == SFPARSE_ERR_EOF) { 255 6053 6308
517 255 break;
518 }
519  
520 6053 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 2871 3182 6053
521 2871 sfp_mark_failed(&w);
522 2871 SvREFCNT_dec(ref);
523 2871 Safefree(w.data);
524 2871 sfp_croak_parse(aTHX_ &w);
525 }
526  
527 3182 item = sfp_build_item(aTHX_ &w, &val);
528  
529 3182 ▶ if (item == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (item == NULL) { 218 2964 3182
530 218 sfp_mark_failed(&w);
531 218 SvREFCNT_dec(ref);
532 218 Safefree(w.data);
533 218 sfp_croak_parse(aTHX_ &w);
534 }
535  
536 2964 av_push(av, item);
537 }
538  
539 255 Safefree(w.data);
540  
541 255 RETVAL = ref;
542 OUTPUT:
543 RETVAL
544  
545 SV *
546 sfp_xs_parse_dictionary(input)
547 SV *input
548 PREINIT:
549 sfp_wrap w;
550 AV *av;
551 SV *ref;
552 CODE:
553 5471 sfp_init(aTHX_ &w, input);
554  
555 5471 av = newAV();
556 5471 ref = newRV_noinc((SV *)av);
557  
558 6608 for (;;) {
559 sfparse_vec key;
560 sfparse_value val;
561 SV *item;
562 AV *pair;
563 12079 int rv = sfp_top(&w, SFP_TOP_DICT, &key, &val);
564  
565 12079 ▶ if (rv == SFPARSE_ERR_EOF) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv == SFPARSE_ERR_EOF) { 2253 9826 12079
566 2253 break;
567 }
568  
569 9826 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 3206 6620 9826
570 3206 sfp_mark_failed(&w);
571 3206 SvREFCNT_dec(ref);
572 3206 Safefree(w.data);
573 3206 sfp_croak_parse(aTHX_ &w);
574 }
575  
576 6620 item = sfp_build_item(aTHX_ &w, &val);
577  
578 6620 ▶ if (item == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (item == NULL) { 12 6608 6620
579 12 sfp_mark_failed(&w);
580 12 SvREFCNT_dec(ref);
581 12 Safefree(w.data);
582 12 sfp_croak_parse(aTHX_ &w);
583 }
584  
585 6608 pair = newAV();
586  
587 6608 av_fill(pair, SFP_PAIR_WIDTH - 1);
588 6608 (void)av_store(pair, SFP_KEY, sfp_vec_sv(aTHX_ & key));
589 6608 (void)av_store(pair, SFP_ITEM, item);
590 6608 av_push(av, newRV_noinc((SV *)pair));
591 }
592  
593 2253 Safefree(w.data);
594  
595 2253 RETVAL = ref;
596 OUTPUT:
597 RETVAL
« src/sfparse.c ↑ Summary blib/lib/HTTP/libsfparse.pm »