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 12777 static const char *sfp_input(pTHX_ SV *sv, STRLEN *lenp) {
82 12777 ▶ SvGETMAGIC(sv);
Branch: 2 branches
BranchTrueFalseTotal
SvGETMAGIC(sv); 0 12777 12777
SvGETMAGIC(sv); 0 0 0
83  
84 12777 ▶ if (!SvOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(sv)) { 1 12776 12777
85 1 croak("HTTP::libsfparse: input is undefined");
86 }
87  
88 12776 ▶ if (SvUTF8(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(sv)) { 3 12773 12776
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 12773 return SvPV_nomg_const(sv, *lenp);
100 }
101  
102 /* ------------------------------------------------------------------------
103 * Value conversion
104 * ------------------------------------------------------------------------ */
105  
106 16939 static SV *sfp_int_sv(pTHX_ int64_t v) {
107 16939 return newSVi64(v);
108 }
109  
110 22363 static SV *sfp_vec_sv(pTHX_ const sfparse_vec *v) {
111 22363 ▶ if (v->len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (v->len == 0) { 8 22355 22363
112 8 return newSVpvn("", 0);
113 }
114  
115 22355 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 53 static SV *sfp_decode_sv(pTHX_ const sfparse_vec *src, sfp_decoder decode) {
123 SV *sv;
124 sfparse_vec dest;
125  
126 53 ▶ if (src->len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (src->len == 0) { 8 45 53
127 8 return newSVpvn("", 0);
128 }
129  
130 45 sv = newSV(0);
131 45 sv_grow(sv, src->len + 1);
132 45 SvPOK_only(sv);
133  
134 45 dest.base = (uint8_t *)SvPVX(sv);
135 45 dest.len = 0;
136  
137 45 decode(&dest, src);
138  
139 assert(dest.len <= src->len);
140  
141 45 SvCUR_set(sv, dest.len);
142 45 *SvEND(sv) = '\0';
143  
144 45 ▶ SvTAINT(sv);
Branch: 2 branches
BranchTrueFalseTotal
SvTAINT(sv); 0 45 45
SvTAINT(sv); 0 0 0
145  
146 45 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 30598 static SV *sfp_make_value(pTHX_ const sfparse_value *val) {
174 30598 AV *av = newAV();
175 30598 SV *ref = newRV_noinc((SV *)av);
176  
177 /* Fill to the full width up front: the slots left alone read as undef. */
178 30598 av_fill(av, SFP_ITEM_WIDTH - 1);
179  
180 30598 switch (val->type) {
181 2529 case SFPARSE_TYPE_BOOLEAN:
182 2529 (void)av_store(av, SFP_VALUE, newSViv(val->boolean ? 1 : 0));
183  
184 2529 break;
185 16939 case SFPARSE_TYPE_INTEGER:
186 case SFPARSE_TYPE_DATE:
187 16939 (void)av_store(av, SFP_VALUE, sfp_int_sv(aTHX_ val->integer));
188  
189 16939 break;
190 2184 case SFPARSE_TYPE_DECIMAL:
191 2184 (void)av_store(av, SFP_VALUE,
192 newSVnv((NV)val->decimal.numer / (NV)val->decimal.denom));
193  
194 2184 break;
195 2147 case SFPARSE_TYPE_STRING:
196 2147 ▶ if (val->flags & SFPARSE_VALUE_FLAG_ESCAPED_STRING) {
Branch: 1 branch
BranchTrueFalseTotal
if (val->flags & SFPARSE_VALUE_FLAG_ESCAPED_STRING) { 8 2139 2147
197 8 (void)av_store(av, SFP_VALUE,
198 sfp_decode_sv(aTHX_ & val->vec, sfparse_unescape));
199 } else {
200 2139 (void)av_store(av, SFP_VALUE, sfp_vec_sv(aTHX_ & val->vec));
201 }
202  
203 2147 break;
204 3558 case SFPARSE_TYPE_TOKEN:
205 3558 (void)av_store(av, SFP_VALUE, sfp_vec_sv(aTHX_ & val->vec));
206  
207 3558 break;
208 26 case SFPARSE_TYPE_BYTESEQ:
209 26 (void)av_store(av, SFP_VALUE,
210 sfp_decode_sv(aTHX_ & val->vec, sfparse_base64decode));
211  
212 26 break;
213 19 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 19 SV *sv = sfp_decode_sv(aTHX_ & val->vec, sfparse_pctdecode);
217  
218 19 SvUTF8_on(sv);
219  
220 19 (void)av_store(av, SFP_VALUE, sv);
221  
222 19 break;
223 }
224 3196 case SFPARSE_TYPE_INNER_LIST:
225 /* The members are the caller's to fill in. */
226  
227 3196 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 30598 (void)av_store(av, SFP_TYPE, newSViv((IV)val->type));
234  
235 30598 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 30442 static int sfp_param(sfp_wrap *w, sfparse_vec *key, sfparse_value *val) {
246 30442 int rv = sfparse_parser_param(&w->sfp, key, val);
247  
248 30442 ▶ if (rv != 0 && rv != SFPARSE_ERR_EOF) {
Branch: 2 branches
BranchTrueFalseTotal
if (rv != 0 && rv != SFPARSE_ERR_EOF) { 20386 10056 30442
if (rv != 0 && rv != SFPARSE_ERR_EOF) { 187 20199 20386
249 187 sfp_mark_failed(w);
250 }
251  
252 30442 return rv;
253 }
254  
255 12535 static int sfp_inner_list(sfp_wrap *w, sfparse_value *val) {
256 12535 int rv = sfparse_parser_inner_list(&w->sfp, val);
257  
258 12535 ▶ if (rv != 0 && rv != SFPARSE_ERR_EOF) {
Branch: 2 branches
BranchTrueFalseTotal
if (rv != 0 && rv != SFPARSE_ERR_EOF) { 3194 9341 12535
if (rv != 0 && rv != SFPARSE_ERR_EOF) { 154 3040 3194
259 154 sfp_mark_failed(w);
260 }
261  
262 12535 return rv;
263 }
264  
265 23634 static int sfp_top(sfp_wrap *w, U8 which, sfparse_vec *key,
266 sfparse_value *val) {
267 int rv;
268  
269 23634 switch (which) {
270 12082 case SFP_TOP_DICT:
271 12082 rv = sfparse_parser_dict(&w->sfp, key, val);
272  
273 12082 break;
274 6312 case SFP_TOP_LIST:
275 6312 rv = sfparse_parser_list(&w->sfp, val);
276  
277 6312 break;
278 5240 default:
279 5240 rv = sfparse_parser_item(&w->sfp, val);
280  
281 5240 break;
282 }
283  
284 23634 ▶ if (rv != 0 && rv != SFPARSE_ERR_EOF) {
Branch: 2 branches
BranchTrueFalseTotal
if (rv != 0 && rv != SFPARSE_ERR_EOF) { 12433 11201 23634
if (rv != 0 && rv != SFPARSE_ERR_EOF) { 9322 3111 12433
285 9322 sfp_mark_failed(w);
286 }
287  
288 23634 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 20386 static SV *sfp_build_params(pTHX_ sfp_wrap *w) {
303 20386 AV *av = newAV();
304 20386 SV *ref = newRV_noinc((SV *)av);
305  
306 10056 for (;;) {
307 sfparse_vec key;
308 sfparse_value val;
309 AV *pair;
310 30442 int rv = sfp_param(w, &key, &val);
311  
312 30442 ▶ if (rv == SFPARSE_ERR_EOF) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv == SFPARSE_ERR_EOF) { 20199 10243 30442
313 20199 break;
314 }
315  
316 10243 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 187 10056 10243
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 10056 pair = newAV();
325  
326 10056 av_fill(pair, SFP_PAIR_WIDTH - 1);
327 10056 (void)av_store(pair, SFP_KEY, sfp_vec_sv(aTHX_ & key));
328 10056 (void)av_store(pair, SFP_ITEM, sfp_make_value(aTHX_ & val));
329 10056 av_push(av, newRV_noinc((SV *)pair));
330 }
331  
332 20199 return ref;
333 }
334  
335 20542 static SV *sfp_build_item(pTHX_ sfp_wrap *w, const sfparse_value *val) {
336 20542 SV *ref = sfp_make_value(aTHX_ val);
337 20542 AV *av = (AV *)SvRV(ref);
338 SV *params;
339  
340 20542 ▶ if (val->type == SFPARSE_TYPE_INNER_LIST) {
Branch: 1 branch
BranchTrueFalseTotal
if (val->type == SFPARSE_TYPE_INNER_LIST) { 3196 17346 20542
341 3196 AV *members = newAV();
342  
343 3196 (void)av_store(av, SFP_VALUE, newRV_noinc((SV *)members));
344  
345 9339 for (;;) {
346 sfparse_value inner;
347 SV *item;
348 12535 int rv = sfp_inner_list(w, &inner);
349  
350 12535 ▶ if (rv == SFPARSE_ERR_EOF) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv == SFPARSE_ERR_EOF) { 3040 9495 12535
351 3040 break;
352 }
353  
354 9495 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 154 9341 9495
355 154 SvREFCNT_dec(ref);
356  
357 156 return NULL;
358 }
359  
360 9341 item = sfp_build_item(aTHX_ w, &inner);
361  
362 9341 ▶ if (item == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (item == NULL) { 2 9339 9341
363 2 SvREFCNT_dec(ref);
364  
365 2 return NULL;
366 }
367  
368 9339 av_push(members, item);
369 }
370 }
371  
372 20386 params = sfp_build_params(aTHX_ w);
373  
374 20386 ▶ if (params == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (params == NULL) { 187 20199 20386
375 187 SvREFCNT_dec(ref);
376  
377 187 return NULL;
378 }
379  
380 20199 (void)av_store(av, SFP_PARAMS, params);
381  
382 20199 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 12777 static void sfp_init(pTHX_ sfp_wrap *w, SV *input) {
394 const char *s;
395 STRLEN len;
396  
397 12777 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 12774 ▶ if (len > 32767) {
Branch: 1 branch
BranchTrueFalseTotal
if (len > 32767) { 0 12774 12774
401 0 croak("HTTP::libsfparse: field is too large");
402 }
403  
404 12774 Zero(w, 1, sfp_wrap);
405  
406 12774 ▶ if (len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (len > 0) { 12495 279 12774
407 12495 Newx(w->data, len, char);
408 12495 Copy(s, w->data, len, char);
409 }
410  
411 12774 sfparse_parser_init(&w->sfp, (const uint8_t *)w->data, len);
412 12774 }
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 12 ▶ PERL_MATH_INT64_LOAD_OR_CROAK;
Branch: 1 branch
BranchTrueFalseTotal
PERL_MATH_INT64_LOAD_OR_CROAK; 0 12 12
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 12 HV *stash = gv_stashpvs("HTTP::libsfparse", GV_ADD);
447 size_t i;
448 180 ▶ for (i = 0; i < sizeof(constants) / sizeof(constants[0]); ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < sizeof(constants) / sizeof(constants[0]); ++i) { 168 12 180
449 168 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 3960 sfp_init(aTHX_ &w, input);
463  
464 3957 rv = sfp_top(&w, SFP_TOP_ITEM, NULL, &val);
465  
466 3957 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 2563 1394 3957
467 2563 sfp_mark_failed(&w);
468 2563 Safefree(w.data);
469 2563 sfp_croak_parse(aTHX_ &w);
470 }
471  
472 1394 item = sfp_build_item(aTHX_ &w, &val);
473  
474 1394 ▶ if (item == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (item == NULL) { 111 1283 1394
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 1283 rv = sfp_top(&w, SFP_TOP_ITEM, NULL, NULL);
482  
483 1283 ▶ if (rv != SFPARSE_ERR_EOF) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != SFPARSE_ERR_EOF) { 682 601 1283
484 682 sfp_mark_failed(&w);
485 }
486  
487 1283 Safefree(w.data);
488  
489 1283 ▶ if (rv != SFPARSE_ERR_EOF) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != SFPARSE_ERR_EOF) { 682 601 1283
490 682 SvREFCNT_dec(item);
491 682 sfp_croak_parse(aTHX_ &w);
492 }
493  
494 601 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 3345 sfp_init(aTHX_ &w, input);
507  
508 3345 av = newAV();
509 3345 ref = newRV_noinc((SV *)av);
510  
511 2967 for (;;) {
512 sfparse_value val;
513 SV *item;
514 6312 int rv = sfp_top(&w, SFP_TOP_LIST, NULL, &val);
515  
516 6312 ▶ if (rv == SFPARSE_ERR_EOF) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv == SFPARSE_ERR_EOF) { 256 6056 6312
517 256 break;
518 }
519  
520 6056 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 2871 3185 6056
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 3185 item = sfp_build_item(aTHX_ &w, &val);
528  
529 3185 ▶ if (item == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (item == NULL) { 218 2967 3185
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 2967 av_push(av, item);
537 }
538  
539 256 Safefree(w.data);
540  
541 256 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 5472 sfp_init(aTHX_ &w, input);
554  
555 5472 av = newAV();
556 5472 ref = newRV_noinc((SV *)av);
557  
558 6610 for (;;) {
559 sfparse_vec key;
560 sfparse_value val;
561 SV *item;
562 AV *pair;
563 12082 int rv = sfp_top(&w, SFP_TOP_DICT, &key, &val);
564  
565 12082 ▶ if (rv == SFPARSE_ERR_EOF) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv == SFPARSE_ERR_EOF) { 2254 9828 12082
566 2254 break;
567 }
568  
569 9828 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 3206 6622 9828
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 6622 item = sfp_build_item(aTHX_ &w, &val);
577  
578 6622 ▶ if (item == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (item == NULL) { 12 6610 6622
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 6610 pair = newAV();
586  
587 6610 av_fill(pair, SFP_PAIR_WIDTH - 1);
588 6610 (void)av_store(pair, SFP_KEY, sfp_vec_sv(aTHX_ & key));
589 6610 (void)av_store(pair, SFP_ITEM, item);
590 6610 av_push(av, newRV_noinc((SV *)pair));
591 }
592  
593 2254 Safefree(w.data);
594  
595 2254 RETVAL = ref;
596 OUTPUT:
597 RETVAL
« src/sfparse.c ↑ Summary blib/lib/HTTP/libsfparse.pm »