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