libsfparse.xs

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