XS.xs

filter: Statementstmt 92.6% 2040 / 2201 Branchbran 73.7% 1592 / 2158 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 83.3% 3632 / 4359 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« utf8_range.c ↑ Summary
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5 #define NEED_load_module
6 #define NEED_newCONSTSUB
7 #define NEED_vload_module
8 #define NEED_vnewSVpvf
9 #define NEED_warner
10 #define NEED_grok_number
11 #define NEED_grok_numeric_radix
12 #define NEED_newRV_noinc
13 #define NEED_sv_2pv_flags
14 #include "ppport.h"
15  
16 #include <assert.h>
17 #include <string.h>
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <limits.h>
21 #include <float.h>
22 #include <errno.h>
23  
24 /* SIMD UTF-8 validation (GH #213), see the header for provenance */
25 #include "utf8_range.h"
26  
27 /* The validator is a once-per-buffer cold path; keeping it out of line keeps
28 the parser's hot loops' register allocation unchanged. */
29 #if defined(__GNUC__) || defined(__clang__)
30 # define CJSON_NOINLINE __attribute__((noinline))
31 #else
32 # define CJSON_NOINLINE
33 #endif
34  
35 #ifndef OutputStream
36 # define OutputStream PerlIO *
37 #endif
38  
39 #if defined(__BORLANDC__) || defined(_MSC_VER)
40 # define snprintf _snprintf // C compilers have this in stdio.h
41 #endif
42  
43 #ifndef PERL_UNUSED_RESULT
44 # if defined(__GNUC__) && defined(HASATTRIBUTE_WARN_UNUSED_RESULT)
45 # define PERL_UNUSED_RESULT(v) STMT_START { __typeof__(v) z = (v); (void)sizeof(z); } STMT_END
46 # else
47 # define PERL_UNUSED_RESULT(v) ((void)(v))
48 # endif
49 #endif
50  
51 #if defined(_AIX) && (!defined(HAS_LONG_DOUBLE) || AIX_WORKAROUND)
52 #define HAVE_NO_POWL
53 #endif
54  
55 /* Freebsd 10: It has powl, but it is too bad. strtold is good. RT #101265 */
56 #if defined(__FreeBSD__) && defined(__clang__) && defined(USE_LONG_DOUBLE)
57 #define HAVE_BAD_POWL
58 #endif
59  
60 #if PERL_VERSION < 22 && defined(HAS_SETLOCALE)
61 #define NEED_NUMERIC_LOCALE_C
62 #ifdef I_XLOCALE
63 #include <xlocale.h>
64 #endif
65 #endif
66  
67 /* FIXME: still a refcount error */
68 #define HAVE_DECODE_BOM
69 #define UTF8BOM "\357\273\277" /* EF BB BF */
70 /* UTF16/32BOM is deprecated, RFC 8259 */
71 #define UTF16BOM "\377\376" /* FF FE or +UFEFF */
72 #define UTF16BOM_BE "\376\377" /* FE FF */
73 #define UTF32BOM "\377\376\000\000" /* FF FE 00 00 or +UFEFF */
74 #define UTF32BOM_BE "\000\000\376\377" /* 00 00 FE FF */
75  
76 /* Need to disable broken gcc-9.[0-3] -O1. Assume gcc-10 is also still broken */
77 /* Only gcc defines __GNUC_PATCHLEVEL__, clang and icc do define __GNUC__ */
78 #if defined(__GNUC__) && defined(__GNUC_PATCHLEVEL__) && \
79 (((__GNUC__ == 9) && (__GNUC_MINOR__ <= 3) || \
80 (__GNUC__ > 9)))
81 # define BROKEN_GCC_OPT
82 #endif
83  
84 /* mingw with USE_LONG_DOUBLE (and implied USE_MINGW_ANSI_STDIO) do use the
85 non-msvcrt inf/nan stringification in sprintf(). */
86 #if defined(WIN32) && !defined(__USE_MINGW_ANSI_STDIO) && !defined(USE_LONG_DOUBLE)
87 /* new ucrtd.dll runtime? We do not probe the runtime or variants in the Makefile.PL yet. */
88 #define STR_INF "inf"
89 #define STR_INF2 "inf.0"
90 #define STR_NAN "nan"
91 #define STR_QNAN "nan(ind)"
92 /* old standard msvcrt.dll */
93 #define STR_INF3 "1.#INF"
94 #define STR_INF4 "1.#INF.0"
95 #define STR_NAN2 "1.#IND"
96 #define STR_QNAN2 "1.#QNAN"
97 #define HAVE_QNAN
98 #elif defined(sun) || defined(__sun)
99 #define STR_INF "Infinity"
100 #define STR_NAN "NaN"
101 #elif defined(__hpux)
102 #define STR_INF "++"
103 #define STR_NAN "-?"
104 #define HAVE_NEG_NAN
105 #define STR_NEG_INF "---"
106 #define STR_NEG_NAN "?"
107 #elif defined(_AIX) || defined(_AIX50)
108 /* xlC compiler: __TOS_AIX__ FIXME: This does not work yet. GH #165 */
109 #define STR_INF "INF"
110 #define STR_INF2 "INF.0"
111 #define HAVE_NEG_NAN
112 #define STR_NEG_INF "-INF"
113 #define HAVE_NEG_NAN
114 #define HAVE_QNAN
115 #define STR_NAN "NaN"
116 #define STR_QNAN "NaNQ"
117 //#define STR_QNAN "NANQ"
118 #else
119 #define STR_INF "inf"
120 #define STR_NAN "nan"
121 #endif
122  
123 /* NV_INF compatibility for Perl 5.6 */
124 #if !defined(NV_INF) && defined(USE_LONG_DOUBLE) && defined(LDBL_INFINITY)
125 # define NV_INF LDBL_INFINITY
126 #endif
127 #if !defined(NV_INF) && defined(DBL_INFINITY)
128 # define NV_INF (NV)DBL_INFINITY
129 #endif
130 #if !defined(NV_INF) && defined(INFINITY)
131 # define NV_INF (NV)INFINITY
132 #endif
133 #if !defined(NV_INF) && defined(INF)
134 # define NV_INF (NV)INF
135 #endif
136 #if !defined(NV_INF) && defined(USE_LONG_DOUBLE) && defined(HUGE_VALL)
137 # define NV_INF (NV)HUGE_VALL
138 #endif
139 #if !defined(NV_INF) && defined(HUGE_VAL)
140 # define NV_INF (NV)HUGE_VAL
141 #endif
142  
143 /* NV_NAN compatibility for Perl 5.6 */
144 #if !defined(NV_NAN) && defined(USE_LONG_DOUBLE)
145 # if !defined(NV_NAN) && defined(LDBL_NAN)
146 # define NV_NAN LDBL_NAN
147 # endif
148 # if !defined(NV_NAN) && defined(LDBL_QNAN)
149 # define NV_NAN LDBL_QNAN
150 # endif
151 # if !defined(NV_NAN) && defined(LDBL_SNAN)
152 # define NV_NAN LDBL_SNAN
153 # endif
154 #endif
155 #if !defined(NV_NAN) && defined(DBL_NAN)
156 # define NV_NAN (NV)DBL_NAN
157 #endif
158 #if !defined(NV_NAN) && defined(DBL_QNAN)
159 # define NV_NAN (NV)DBL_QNAN
160 #endif
161 #if !defined(NV_NAN) && defined(DBL_SNAN)
162 # define NV_NAN (NV)DBL_SNAN
163 #endif
164 #if !defined(NV_NAN) && defined(QNAN)
165 # define NV_NAN (NV)QNAN
166 #endif
167 #if !defined(NV_NAN) && defined(SNAN)
168 # define NV_NAN (NV)SNAN
169 #endif
170 #if !defined(NV_NAN) && defined(NAN)
171 # define NV_NAN (NV)NAN
172 #endif
173  
174 /* modfl() segfaults for -Duselongdouble && 64-bit mingw64 && mingw
175 runtime version 4.0 [perl #125924] */
176 #if defined(USE_LONG_DOUBLE) && defined(__MINGW64__) \
177 && __MINGW64_VERSION_MAJOR == 4 && __MINGW64_VERSION_MINOR == 0
178 #undef HAS_MODFL
179 #undef Perl_modf
180 #define Perl_modf(nv, ip) mingw_modfl(nv, ip)
181 long double
182 mingw_modfl(long double x, long double *ip)
183 {
184 *ip = truncl(x);
185 return (x == *ip ? copysignl(0.0L, x) : x - *ip);
186 }
187 #endif
188  
189 /* some old perls do not have this, try to make it work, no */
190 /* guarantees, though. if it breaks, you get to keep the pieces. */
191 #ifndef UTF8_MAXBYTES
192 # define UTF8_MAXBYTES 13
193 #endif
194  
195 /* 5.6: */
196 #ifndef IS_NUMBER_IN_UV
197 #define IS_NUMBER_IN_UV 0x01 /* number within UV range (maybe not
198 int). value returned in pointed-
199 to UV */
200 #define IS_NUMBER_GREATER_THAN_UV_MAX 0x02 /* pointed to UV undefined */
201 #define IS_NUMBER_NOT_INT 0x04 /* saw . or E notation */
202 #define IS_NUMBER_NEG 0x08 /* leading minus sign */
203 #define IS_NUMBER_INFINITY 0x10 /* this is big */
204 #define IS_NUMBER_NAN 0x20 /* this is not */
205 #endif
206 #ifndef UNI_DISPLAY_QQ
207 #define UNI_DISPLAY_ISPRINT 0x0001
208 #define UNI_DISPLAY_BACKSLASH 0x0002
209 #define UNI_DISPLAY_QQ (UNI_DISPLAY_ISPRINT|UNI_DISPLAY_BACKSLASH)
210 #define UNI_DISPLAY_REGEX (UNI_DISPLAY_ISPRINT|UNI_DISPLAY_BACKSLASH)
211 #endif
212 /* with 5.6 hek can only be non-utf8 */
213 #ifndef HeKUTF8
214 #define HeKUTF8(he) 0
215 #endif
216 #ifndef GV_NOADD_NOINIT
217 #define GV_NOADD_NOINIT 0
218 #endif
219 /* since 5.8.1 */
220 #ifndef SvIsCOW_shared_hash
221 #define SvIsCOW_shared_hash(pv) 0
222 #endif
223 /* 5.8.1 has a broken assert_not_ROK */
224 #if PERL_VERSION == 8 && PERL_SUBVERSION == 1
225 # undef assert_not_ROK
226 # if defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
227 # define assert_not_ROK(sv) ({assert(!SvROK(sv) || !SvRV(sv));}),
228 # else
229 # define assert_not_ROK(sv)
230 # endif
231 #endif
232 /* 5.8 problem, it was renamed to HINT_BYTES with 5.8.0 */
233 #if PERL_VERSION < 7
234 #ifndef HINT_BYTES
235 #define HINT_BYTES 8
236 #endif
237 #endif
238 /* compatibility with perl <5.14 */
239 /* added with 5.13.6 */
240 #ifndef sv_cmp_flags
241 # define sv_cmp_flags(a,b,flags) sv_cmp((a),(b))
242 #endif
243 #ifndef SvTRUE_nomg
244 #define SvTRUE_nomg SvTRUE
245 #endif
246 #ifndef SvNV_nomg
247 #define SvNV_nomg SvNV
248 #endif
249 #ifndef PERL_UNICODE_MAX
250 #define PERL_UNICODE_MAX 0x10FFFF
251 #endif
252 #ifndef HvNAMELEN_get
253 # define HvNAMELEN_get(hv) strlen (HvNAME (hv))
254 #endif
255 #ifndef HvNAMELEN
256 # define HvNAMELEN(hv) HvNAMELEN_get (hv)
257 #endif
258 #ifndef HvNAMEUTF8
259 # define HvNAMEUTF8(hv) 0
260 #endif
261 #if 0
262 /* since 5.14 check use warnings 'nonchar' */
263 #ifdef WARN_NONCHAR
264 #define WARNER_NONCHAR(hi) \
265 Perl_ck_warner_d(aTHX_ packWARN(WARN_NONCHAR), \
266 "Unicode non-character U+%04" UVXf " is not " \
267 "recommended for open interchange", hi)
268 /* before check use warnings 'utf8' */
269 #elif PERL_VERSION > 10
270 #define WARNER_NONCHAR(hi) \
271 Perl_ck_warner_d(aTHX_ packWARN(WARN_UTF8), \
272 "Unicode non-character U+%04" UVXf " is illegal " \
273 "for interchange", hi)
274 #else
275 #define WARNER_NONCHAR(hi) \
276 Perl_warner(aTHX_ packWARN(WARN_UTF8), \
277 "Unicode non-character U+%04lX is illegal", (unsigned long)hi)
278 #endif
279 #else
280 #define WARNER_NONCHAR(hi)
281 #endif
282  
283 /* since 5.16 */
284 #ifndef GV_NO_SVGMAGIC
285 #define GV_NO_SVGMAGIC 0
286 #endif
287 /* since 5.18 */
288 #ifndef SvREFCNT_dec_NN
289 #define SvREFCNT_dec_NN(sv) SvREFCNT_dec(sv)
290 #endif
291 /* from cperl */
292 #ifndef strEQc
293 /* the buffer ends with \0, includes comparison of the \0.
294 better than strEQ as it uses memcmp, word-wise comparison. */
295 # define strEQc(s, c) memEQ(s, ("" c ""), sizeof(c))
296 #endif
297 #ifndef memEQc
298 /* excluding the final \0, so the string s may continue */
299 # define memEQc(s, c) memEQ(s, ("" c ""), sizeof(c)-1)
300 #endif
301 #ifndef He_IS_SVKEY
302 # define He_IS_SVKEY(he) HeKLEN (he) == HEf_SVKEY
303 #endif
304  
305 /* av_len has 2 different possible types */
306 #ifndef HVMAX_T
307 # if PERL_VERSION >= 20
308 # define HVMAX_T SSize_t
309 # else
310 # define HVMAX_T I32
311 # endif
312 #endif
313 /* and riter 3 */
314 #ifndef RITER_T
315 # ifdef USE_CPERL
316 # if PERL_VERSION >= 25
317 # define RITER_T U32
318 # else
319 # define RITER_T SSize_t
320 # endif
321 # else
322 # define RITER_T I32
323 # endif
324 #endif
325  
326 #if (PERL_REVISION > 5) || (PERL_REVISION == 5 && PERL_VERSION >= 36)
327 # define PERL_HAVE_BOOLEANS
328 #endif
329  
330 // i.e. "JSON" in big-endian
331 #define JSON_MAGIC 0x4A534F4E
332  
333 /* types */
334 #define JSON_TYPE_SCALAR 0x0000
335 #define JSON_TYPE_BOOL 0x0001
336 #define JSON_TYPE_INT 0x0002
337 #define JSON_TYPE_FLOAT 0x0003
338 #define JSON_TYPE_STRING 0x0004
339  
340 /* flags */
341 #define JSON_TYPE_CAN_BE_NULL 0x0100
342  
343 /* null type */
344 #define JSON_TYPE_NULL JSON_TYPE_CAN_BE_NULL
345  
346 /* classes */
347 #define JSON_TYPE_CLASS "Cpanel::JSON::XS::Type"
348 #define JSON_TYPE_ARRAYOF_CLASS "Cpanel::JSON::XS::Type::ArrayOf"
349 #define JSON_TYPE_HASHOF_CLASS "Cpanel::JSON::XS::Type::HashOf"
350 #define JSON_TYPE_ANYOF_CLASS "Cpanel::JSON::XS::Type::AnyOf"
351 #define JSON_TYPE_OPTIONAL_CLASS "Cpanel::JSON::XS::Type::Optional"
352  
353 #define JSON_TYPE_ANYOF_SCALAR_INDEX 0
354 #define JSON_TYPE_ANYOF_ARRAY_INDEX 1
355 #define JSON_TYPE_ANYOF_HASH_INDEX 2
356  
357 /* three extra for rounding, sign, and end of string */
358 #define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 3)
359  
360 #define F_ASCII 0x00000001UL
361 #define F_LATIN1 0x00000002UL
362 #define F_UTF8 0x00000004UL
363 #define F_INDENT 0x00000008UL
364 #define F_CANONICAL 0x00000010UL
365 #define F_SPACE_BEFORE 0x00000020UL
366 #define F_SPACE_AFTER 0x00000040UL
367 #define F_ALLOW_NONREF 0x00000100UL
368 #define F_SHRINK 0x00000200UL
369 #define F_ALLOW_BLESSED 0x00000400UL
370 #define F_CONV_BLESSED 0x00000800UL
371 #define F_RELAXED 0x00001000UL
372 #define F_ALLOW_UNKNOWN 0x00002000UL
373 #define F_ALLOW_TAGS 0x00004000UL
374 #define F_BINARY 0x00008000UL
375 #define F_ALLOW_BAREKEY 0x00010000UL
376 #define F_ALLOW_SQUOTE 0x00020000UL
377 #define F_ALLOW_BIGNUM 0x00040000UL
378 #define F_ESCAPE_SLASH 0x00080000UL
379 #define F_SORT_BY 0x00100000UL
380 #define F_ALLOW_STRINGIFY 0x00200000UL
381 #define F_UNBLESSED_BOOL 0x00400000UL
382 #define F_ALLOW_DUPKEYS 0x00800000UL
383 #define F_REQUIRE_TYPES 0x01000000UL
384 #define F_TYPE_ALL_STRING 0x02000000UL
385 #define F_DUPKEYS_AS_AREF 0x04000000UL
386 #define F_NONREF_SET 0x08000000UL /* allow_nonref was called explicitly */
387 #define F_HOOK 0x80000000UL /* some hooks exist, so slow-path processing */
388  
389 #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER
390 #define SET_RELAXED (F_RELAXED | F_ALLOW_BAREKEY | F_ALLOW_SQUOTE | F_ALLOW_DUPKEYS)
391  
392 #define INIT_SIZE 32 /* initial scalar size to be allocated */
393 #define INDENT_STEP 3 /* default spaces per indentation level */
394  
395 #define SHORT_STRING_LEN 16384 /* special-case strings of up to this size */
396  
397 #define DECODE_WANTS_OCTETS(json) ((json)->flags & F_UTF8)
398  
399 #define SB do {
400 #define SE } while (0)
401  
402 #if __GNUC__ >= 3
403 # define _expect(expr,value) __builtin_expect ((expr), (value))
404 # define INLINE static inline
405 #else
406 # define _expect(expr,value) (expr)
407 # define INLINE static
408 #endif
409 #ifndef LIKELY
410 #define LIKELY(expr) _expect ((long)(expr) != 0, 1)
411 #define UNLIKELY(expr) _expect ((long)(expr) != 0, 0)
412 #endif
413  
414 #define IN_RANGE_INC(type,val,beg,end) \
415 ((unsigned type)((unsigned type)(val) - (unsigned type)(beg)) \
416 <= (unsigned type)((unsigned type)(end) - (unsigned type)(beg)))
417  
418 #define ERR_NESTING_EXCEEDED "json text or perl structure exceeds maximum nesting level (max_depth set too low?)"
419  
420 # define JSON_STASH MY_CXT.json_stash
421  
422 #define MY_CXT_KEY "Cpanel::JSON::XS::_guts"
423  
424 typedef struct {
425 HV *json_stash; /* Cpanel::JSON::XS:: */
426 HV *json_boolean_stash; /* JSON::PP::Boolean:: */
427 HV *jsonold_boolean_stash; /* JSON::XS::Boolean:: if empty will be (HV*)1 */
428 HV *mojo_boolean_stash; /* Mojo::JSON::_Bool:: if empty will be (HV*)1 */
429 SV *json_true, *json_false;
430 SV *sv_json;
431 } my_cxt_t;
432  
433 /* the amount of HEs to allocate on the stack, when sorting keys */
434 #define STACK_HES 64
435  
436 START_MY_CXT
437  
438 INLINE SV * get_bool (pTHX_ const char *name);
439  
440 enum {
441 INCR_M_WS = 0, /* initial whitespace skipping, must be 0 */
442 INCR_M_STR, /* inside string */
443 INCR_M_BS, /* inside backslash */
444 INCR_M_C0, /* inside comment in initial whitespace sequence */
445 INCR_M_C1, /* inside comment in other places */
446 INCR_M_JSON, /* outside anything, count nesting */
447 INCR_M_SQSTR, /* inside single-quoted string (allow_singlequote) */
448 INCR_M_SQBS /* inside backslash inside single-quoted string */
449 };
450  
451 #define INCR_DONE(json) ((json)->incr_nest <= 0 && (json)->incr_mode == INCR_M_JSON)
452 /* the first text once complete, else everything buffered */
453 #define INCR_SIZE(json) (INCR_DONE (json) ? (json)->incr_pos : SvCUR ((json)->incr_text))
454  
455 typedef struct {
456 U32 flags;
457 U32 max_depth;
458 U32 indent_length; /* how much padding to use when indenting */
459 STRLEN max_size;
460  
461 SV *cb_object;
462 HV *cb_sk_object;
463 SV *cb_sort_by;
464  
465 /* for the incremental parser */
466 SV *incr_text; /* the source text so far */
467 STRLEN incr_pos; /* the current offset into the text */
468 int incr_nest; /* {[]}-nesting level */
469 unsigned char incr_mode;
470 unsigned char infnan_mode;
471  
472 /* corruption check */
473 U32 magic;
474 } JSON;
475  
476 #if defined(USE_ITHREADS) && (PERL_VERSION > 8 || (PERL_VERSION == 8 && PERL_SUBVERSION >= 9))
477 #define JSON_MG_DUP
478 #ifndef sv_dup_inc
479 #define sv_dup_inc(s,t) SvREFCNT_inc (sv_dup (s,t))
480 #endif
481 /* the object's PV is copied byte for byte into a new thread: give the copy its own SVs */
482 static int
483 json_mg_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *param)
484 {
485 JSON *json = (JSON *)SvPVX (mg->mg_obj);
486 if (SvLEN (mg->mg_obj) < sizeof (JSON))
487 return 0;
488 json->cb_object = sv_dup_inc (json->cb_object, param);
489 json->cb_sk_object = (HV *)sv_dup_inc ((SV *)json->cb_sk_object, param);
490 json->cb_sort_by = sv_dup_inc (json->cb_sort_by, param);
491 json->incr_text = sv_dup_inc (json->incr_text, param);
492 return 0;
493 }
494 static MGVTBL json_mg_vtbl = { 0, 0, 0, 0, 0, 0, json_mg_dup, 0 };
495 #endif
496  
497 INLINE void
498 1102 json_init (JSON *json)
499 {
500 1102 Zero (json, 1, JSON);
501 1102 json->flags = F_ALLOW_NONREF; /* GH #241: default like JSON::XS 4.0 */
502 1102 json->max_depth = 512;
503 1102 json->indent_length = INDENT_STEP;
504 1102 json->magic = JSON_MAGIC;
505 1102 }
506  
507 /* dTHX/threads TODO
508 END dtor not needed for these, all of these *s refcnts are owned by the stash. */
509 static void
510 69 init_MY_CXT(pTHX_ my_cxt_t * cxt)
511 {
512 69 cxt->json_stash = gv_stashpvn ("Cpanel::JSON::XS", sizeof("Cpanel::JSON::XS")-1, 1);
513 69 cxt->json_boolean_stash = gv_stashpvn ("JSON::PP::Boolean", sizeof("JSON::PP::Boolean")-1, 1);
514 69 cxt->jsonold_boolean_stash = gv_stashpvn ("JSON::XS::Boolean", sizeof("JSON::XS::Boolean")-1, 0);
515 69 cxt->mojo_boolean_stash = gv_stashpvn ("Mojo::JSON::_Bool", sizeof("Mojo::JSON::_Bool")-1, 0);
516 69 ▶ if ( !cxt->mojo_boolean_stash )
Branch: 1 branch
BranchTrueFalseTotal
if ( !cxt->mojo_boolean_stash ) 69 0 69
517 69 cxt->mojo_boolean_stash = (HV*)1; /* invalid ptr to compare against, better than a NULL stash */
518 69 ▶ if ( !cxt->jsonold_boolean_stash )
Branch: 1 branch
BranchTrueFalseTotal
if ( !cxt->jsonold_boolean_stash ) 69 0 69
519 69 cxt->jsonold_boolean_stash = (HV*)1;
520  
521 69 cxt->json_true = get_bool (aTHX_ "Cpanel::JSON::XS::true");
522 69 cxt->json_false = get_bool (aTHX_ "Cpanel::JSON::XS::false");
523  
524 69 cxt->sv_json = newSVpv ("JSON", 0);
525 69 SvREADONLY_on (cxt->sv_json);
526 69 }
527  
528  
529 /*/////////////////////////////////////////////////////////////////////////// */
530 /* utility functions */
531  
532 /* Validate the JSON struct which might get corrupted by wrong FREEZE/THAW
533 methods, or other serializers, or corrupting our magic object.
534 E.g. https://github.com/rurban/Cpanel-JSON-XS/issues/192 */
535 INLINE bool
536 2338 json_validate (JSON *json)
537 {
538 2338 return json->magic == JSON_MAGIC;
539 }
540  
541 /* Unpacks the 2 boolean objects from the global references */
542 INLINE SV *
543 138 get_bool (pTHX_ const char *name)
544 {
545 dMY_CXT;
546 #if PERL_VERSION > 7
547 138 SV *sv = get_sv (name, 1);
548 #else
549 SV *sv = GvSV(gv_fetchpv(name, 1, SVt_PV));
550 #endif
551 138 SV* rv = SvRV(sv);
552 138 ▶ if (!SvOBJECT(sv) || !SvSTASH(sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOBJECT(sv) || !SvSTASH(sv)) { 0 138 138
if (!SvOBJECT(sv) || !SvSTASH(sv)) { 0 0 0
553 138 SvREADONLY_off (sv);
554 138 SvREADONLY_off (rv);
555 138 (void)sv_bless(sv, MY_CXT.json_boolean_stash); /* bless the ref */
556 }
557 138 SvREADONLY_on (rv);
558 138 SvREADONLY_on (sv);
559 138 return sv;
560 }
561  
562 INLINE void
563 200 shrink (pTHX_ SV *sv)
564 {
565 /* ignore errors */
566 200 (void)sv_utf8_downgrade (sv, 1);
567  
568 200 ▶ if (SvLEN (sv) > SvCUR (sv) + 1)
Branch: 1 branch
BranchTrueFalseTotal
if (SvLEN (sv) > SvCUR (sv) + 1) 200 0 200
569 {
570 #ifdef SvPV_shrink_to_cur
571 200 ▶ SvPV_shrink_to_cur (sv);
Branch: 3 branches
BranchTrueFalseTotal
SvPV_shrink_to_cur (sv); 191 9 200
SvPV_shrink_to_cur (sv); 0 191 191
SvPV_shrink_to_cur (sv); 139 61 200
572 #elif defined (SvPV_renew)
573 SvPV_renew (sv, SvCUR (sv) + 1);
574 #endif
575 }
576 200 }
577  
578  
579 /* Decode an utf-8 character and return it, or (UV)-1 in
580 case of an error.
581 We special-case "safe" characters from U+80 .. U+7FF,
582 but use the very good perl function until 5.36 to parse anything else.
583 note that we never call this function for an ascii codepoints.
584 With 5.36 perl5 removed the API to decode utf8 again with flags for relaxed,
585 so we have to hack around this regression again.
586 */
587 INLINE UV
588 21793 decode_utf8 (pTHX_ unsigned char *s, STRLEN len, int relaxed, STRLEN *clen)
589 {
590 21793 ▶ if (LIKELY(len >= 2
Branch: 4 branches
BranchTrueFalseTotal
if (LIKELY(len >= 2 21793 0 21793
if (LIKELY(len >= 2 666 21127 21793
if (LIKELY(len >= 2 666 21127 21793
if (LIKELY(len >= 2 664 2 666
591 && IN_RANGE_INC (char, s[0], 0xc2, 0xdf)
592 && IN_RANGE_INC (char, s[1], 0x80, 0xbf)))
593 {
594 664 *clen = 2;
595 664 return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f);
596 }
597 else {
598 /* Since perl 5.14 we can disallow surrogates and illegal unicode above
599 U+10FFFF.
600 Before we could only warn with warnings 'utf8'.
601 Surrogates are never allowed for consistency with unpaired escaped surrogate
602 handling.
603 SUPER, above U+10FFFF is not allowed, unless we are in the relaxed mode.
604 */
605 #if PERL_VERSION >= 32
606 21129 ▶ UV c = utf8n_to_uvchr (s, len, clen,
Branch: 1 branch
BranchTrueFalseTotal
UV c = utf8n_to_uvchr (s, len, clen, 4 21125 21129
607 UTF8_CHECK_ONLY | UTF8_DISALLOW_SURROGATE | (relaxed ? 0 : UTF8_DISALLOW_SUPER));
608 #elif PERL_VERSION > 12
609 UV c = utf8n_to_uvuni (s, len, clen,
610 UTF8_CHECK_ONLY | UTF8_DISALLOW_SURROGATE | (relaxed ? 0 : UTF8_DISALLOW_SUPER));
611 #elif PERL_VERSION >= 8
612 UV c = utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY);
613 #endif
614 #if PERL_VERSION >= 8 && PERL_VERSION <= 12
615 if (c > PERL_UNICODE_MAX && !relaxed)
616 *clen = -1;
617 #endif
618 #if PERL_VERSION >= 8
619 21129 return c;
620 #else
621 /* 5.6 does not detect certain ill-formed sequences, esp. overflows,
622 which are security relevant. so we add code to detect these. */
623 UV c = utf8_to_uv(s, len, clen, UTF8_CHECK_ONLY);
624 if (!relaxed) {
625 if (!c || c > PERL_UNICODE_MAX)
626 *clen = -1;
627 /* need to check manually for some overflows. 5.6 unicode bug */
628 else if (len >= 2
629 && IN_RANGE_INC (char, s[0], 0xc0, 0xfe)
630 && !IN_RANGE_INC (char, s[0], 0xc2, 0xdf)) {
631 U8 *s0, *send;
632 UV uv = *s;
633 UV expectlen = UTF8SKIP(s);
634  
635 #define UTF_CONTINUATION_MASK ((U8) ((1U << 6) - 1))
636 #define UTF_ACCUMULATION_OVERFLOW_MASK \
637 (((UV) UTF_CONTINUATION_MASK) << ((sizeof(UV) * 8) - 6))
638  
639 s0 = s;
640 /*printf ("maybe overlong <%.*s> %d/%d %x %x\n", len, s, c,
641 *clen, s[0], s[1]);*/
642 if (*clen > 4) {
643 *clen = -1;
644 return c;
645 }
646 send = (U8*) s0 + ((expectlen <= len) ? len : len);
647 for (s = s0 + 1; s < send; s++) {
648 if (LIKELY(UTF8_IS_CONTINUATION(*s))) {
649 if (uv & UTF_ACCUMULATION_OVERFLOW_MASK) {
650 /*printf ("overflow\n");*/
651 *clen = -1;
652 return c;
653 }
654 uv = UTF8_ACCUMULATE(uv, *s);
655 }
656 else {
657 /*printf ("unexpected non continuation\n");*/
658 *clen = -1;
659 return c;
660 }
661 }
662 }
663 }
664 return c;
665 #endif
666 }
667 }
668  
669 /* Length of the UTF-8 sequence at s[0] for input already proven well-formed
670 by cjson_utf8_validate(): the lead byte is then 0xc2..0xf4, so 2..4 bytes
671 and no bounds check is needed (GH #213). */
672 #define CJSON_UTF8_CLEN(c) (2 + ((c) >= 0xe0) + ((c) >= 0xf0))
673  
674 /* One-shot, per buffer: run the SIMD validation and cache the verdict in
675 *checked / *trusted. Deliberately out of line and cold -- it only ever
676 runs once, and this way the caller's hot ascii loop keeps its registers. */
677 static CJSON_NOINLINE void
678 181 utf8_ensure_checked (pTHX_ const unsigned char *s, STRLEN len,
679 char *checked, char *trusted)
680 {
681 181 *checked = 1;
682 181 ▶ *trusted = len >= 16 && cjson_utf8_validate (s, (size_t)len);
Branch: 2 branches
BranchTrueFalseTotal
*trusted = len >= 16 && cjson_utf8_validate (s, (size_t)len); 77 104 181
*trusted = len >= 16 && cjson_utf8_validate (s, (size_t)len); 72 5 77
683 181 }
684  
685 /* Likewise for encoding, also never called for ascii codepoints. */
686 /* This function takes advantage of this fact, although current gcc's */
687 /* seem to optimise the check for >= 0x80 away anyways. */
688 INLINE unsigned char *
689 1619 encode_utf8 (unsigned char *s, UV ch)
690 {
691 UV uv_ch;
692 1619 ▶ if (UNLIKELY(ch < 0x000080))
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY(ch < 0x000080)) 0 1619 1619
693 0 *s++ = (unsigned char) ch;
694 1619 ▶ else if (LIKELY(ch < 0x000800)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (LIKELY(ch < 0x000800)) { 1025 594 1619
695 1025 uv_ch = 0xc0 | ( ch >> 6);
696 1025 *s++ = (unsigned char) uv_ch;
697 1025 uv_ch = 0x80 | ( ch & 0x3f);
698 1025 *s++ = (unsigned char) uv_ch;
699 }
700 594 ▶ else if (ch < 0x010000) {
Branch: 1 branch
BranchTrueFalseTotal
else if (ch < 0x010000) { 265 329 594
701 265 uv_ch = 0xe0 | ( ch >> 12);
702 265 *s++ = (unsigned char) uv_ch;
703 265 uv_ch = 0x80 | ((ch >> 6) & 0x3f);
704 265 *s++ = (unsigned char) uv_ch;
705 265 uv_ch = 0x80 | ( ch & 0x3f);
706 265 *s++ = (unsigned char) uv_ch;
707 }
708 329 ▶ else if (ch < 0x110000) {
Branch: 1 branch
BranchTrueFalseTotal
else if (ch < 0x110000) { 329 0 329
709 329 uv_ch = 0xf0 | ( ch >> 18);
710 329 *s++ = (unsigned char) uv_ch;
711 329 uv_ch = 0x80 | ((ch >> 12) & 0x3f);
712 329 *s++ = (unsigned char) uv_ch;
713 329 uv_ch = 0x80 | ((ch >> 6) & 0x3f);
714 329 *s++ = (unsigned char) uv_ch;
715 329 uv_ch = 0x80 | ( ch & 0x3f);
716 329 *s++ = (unsigned char) uv_ch;
717 }
718 1619 return s;
719 }
720  
721 /* convert offset to character index, sv must be string */
722 static STRLEN
723 372 ptr_to_index (pTHX_ SV *sv, const STRLEN offset)
724 {
725 372 return SvUTF8 (sv)
726 64 ? (STRLEN)utf8_distance ((U8*)(SvPVX(sv)+offset), (U8*)SvPVX (sv))
727 436 ▶ : offset;
Branch: 1 branch
BranchTrueFalseTotal
: offset; 64 308 372
728 }
729  
730 /*/////////////////////////////////////////////////////////////////////////// */
731 /* fp hell */
732  
733 #ifdef HAVE_NO_POWL
734 /* Ulisse Monari: this is a patch for AIX 5.3, perl 5.8.8 without HAS_LONG_DOUBLE
735 There Perl_pow maps to pow(...) - NOT TO powl(...), core dumps at Perl_pow(...)
736  
737 Base code is from http://bytes.com/topic/c/answers/748317-replacement-pow-function
738 This is my change to fs_pow that goes into libc/libm for calling fmod/exp/log.
739 NEED TO MODIFY Makefile, after perl Makefile.PL by adding "-lm" onto the LDDLFLAGS line */
740 static double fs_powEx(double x, double y)
741 {
742 double p = 0;
743  
744 if (0 > x && fmod(y, 1) == 0) {
745 if (fmod(y, 2) == 0) {
746 p = exp(log(-x) * y);
747 } else {
748 p = -exp(log(-x) * y);
749 }
750 } else {
751 if (x != 0 || 0 >= y) {
752 p = exp(log( x) * y);
753 }
754 }
755 return p;
756 }
757 #endif
758  
759 #ifndef Perl_strtod
760 /* scan a group of digits, and a trailing exponent */
761 static void
762 json_atof_scan1 (const char *s, NV *accum, int *expo, int postdp, int maxdepth)
763 {
764 UV uaccum = 0;
765 int eaccum = 0;
766  
767 #if defined(HAVE_BAD_POWL)
768 *accum = strtold(s, NULL);
769 #else
770 /* if we recurse too deep, skip all remaining digits */
771 /* to avoid a stack overflow attack */
772 if (UNLIKELY(--maxdepth <= 0))
773 while (*s >= '0' && *s <= '9')
774 ++s;
775  
776 for (;;)
777 {
778 U8 dig = (U8)(*s - '0');
779  
780 if (UNLIKELY(dig >= 10))
781 {
782 if (dig == (U8)('.' - '0'))
783 {
784 if (postdp)
785 break;
786 ++s;
787 json_atof_scan1 (s, accum, expo, 1, maxdepth);
788 }
789 else if ((dig | ' ') == 'e' - '0')
790 {
791 int exp2 = 0;
792 int neg = 0;
793  
794 ++s;
795  
796 if (*s == '-')
797 {
798 ++s;
799 neg = 1;
800 }
801 else if (*s == '+')
802 ++s;
803  
804 while (*s >= '0' && *s <= '9')
805 exp2 = exp2 * 10 + *s++ - '0';
806  
807 *expo += neg ? -exp2 : exp2;
808 }
809  
810 break;
811 }
812  
813 ++s;
814  
815 uaccum = uaccum * 10 + dig;
816 ++eaccum;
817  
818 /* if we have too many digits, then recurse for more */
819 /* we actually do this for rather few digits */
820 if (uaccum >= (UV_MAX - 9) / 10)
821 {
822 if (postdp) *expo -= eaccum;
823 json_atof_scan1 (s, accum, expo, postdp, maxdepth);
824 if (postdp) *expo += eaccum;
825  
826 break;
827 }
828 }
829  
830 /* This relies greatly on the quality of the pow ()
831 implementation of the platform, but a good
832 implementation is hard to beat.
833 (IEEE 754 conformant ones are required to be exact) */
834 if (postdp)
835 *expo -= eaccum;
836 #ifdef HAVE_NO_POWL
837 /* powf() unfortunately is not accurate enough */
838 *accum += uaccum * fs_powEx(10., *expo );
839 #else
840 *accum += uaccum * Perl_pow (10., *expo);
841 #endif
842 *expo += eaccum;
843 #endif
844 }
845 #endif
846  
847 static NV
848 77 json_atof (const char *s)
849 {
850 77 NV accum = 0.;
851 #ifndef Perl_strtod
852 int expo = 0;
853 #endif
854 77 int neg = 0;
855  
856 77 ▶ if (*s == '-')
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '-') 16 61 77
857 {
858 16 ++s;
859 16 neg = 1;
860 }
861  
862 /* Fix accuracy mismatches compared to perl (using strtod).
863 Available since v5.21.4, but see perl5 af5a4640bf25a62438c05f73a87c1d6be6096b02.
864 Resets LC_NUMERIC only since v5.29.10. (dot, not comma)
865 See GH #154, where the pow method may return a different result than strtod.
866 */
867 #ifdef Perl_strtod
868 77 accum = Perl_strtod (s, NULL);
869 #else
870 /* a recursion depth of ten gives us >>500 bits */
871 json_atof_scan1 (s, &accum, &expo, 0, 10);
872 #endif
873  
874 77 ▶ return neg ? -accum : accum;
Branch: 1 branch
BranchTrueFalseTotal
return neg ? -accum : accum; 16 61 77
875 }
876  
877 INLINE int
878 71 is_bignum_obj (pTHX_ SV *sv)
879 {
880 71 HV *stash = SvSTASH (sv);
881 71 ▶ return (stash == gv_stashpvs ("Math::BigInt", 0) || stash == gv_stashpvs ("Math::BigFloat", 0)) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
return (stash == gv_stashpvs ("Math::BigInt", 0) || stash == gv_stashpvs ("Math::BigFloat", 0)) ? 1 : 0; 32 39 71
return (stash == gv_stashpvs ("Math::BigInt", 0) || stash == gv_stashpvs ("Math::BigFloat", 0)) ? 1 : 0; 27 5 32
882 }
883  
884 /* Math::BigFloat with a large exponent: return its scientific form (bsstr), else NULL */
885 #define BIGNUM_MAX_PLAIN_EXP 64
886 static SV *
887 39 bignum_compact (pTHX_ SV *rv)
888 {
889 39 SV *res = NULL;
890 HV *stash;
891 int count;
892 39 dSP;
893  
894 39 ▶ if (!SvROK (rv) || !SvOBJECT (SvRV (rv)))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK (rv) || !SvOBJECT (SvRV (rv))) 39 0 39
if (!SvROK (rv) || !SvOBJECT (SvRV (rv))) 0 39 39
895 0 return NULL;
896 39 stash = gv_stashpvs ("Math::BigFloat", 0);
897 39 ▶ if (!stash || SvSTASH (SvRV (rv)) != stash)
Branch: 2 branches
BranchTrueFalseTotal
if (!stash || SvSTASH (SvRV (rv)) != stash) 39 0 39
if (!stash || SvSTASH (SvRV (rv)) != stash) 16 23 39
898 16 return NULL;
899  
900 23 ENTER; SAVETMPS;
901 23 ▶ PUSHMARK (SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK (SP); 0 23 23
902 23 ▶ XPUSHs (rv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs (rv); 0 23 23
903 23 PUTBACK;
904 23 count = call_method ("bsstr", G_SCALAR);
905 23 SPAGAIN;
906 23 ▶ if (count == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (count == 1) 23 0 23
907 23 res = newSVsv (POPs);
908 23 PUTBACK;
909 23 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 23 0 23
910  
911 23 ▶ if (res)
Branch: 1 branch
BranchTrueFalseTotal
if (res) 23 0 23
912 {
913 STRLEN len;
914 23 const char *s = SvPV (res, len);
915 23 const char *end = s + len;
916 23 const char *d = (const char *) memchr (s, 'e', len);
917 23 UV ev = 0;
918  
919 23 ▶ if (d)
Branch: 1 branch
BranchTrueFalseTotal
if (d) 20 3 23
920 {
921 20 d++;
922 20 ▶ if (d < end && (*d == '+' || *d == '-'))
Branch: 3 branches
BranchTrueFalseTotal
if (d < end && (*d == '+' || *d == '-')) 20 0 20
if (d < end && (*d == '+' || *d == '-')) 12 8 20
if (d < end && (*d == '+' || *d == '-')) 12 0 12
923 20 d++;
924 43 ▶ for (; d < end && *d >= '0' && *d <= '9'; d++)
Branch: 3 branches
BranchTrueFalseTotal
for (; d < end && *d >= '0' && *d <= '9'; d++) 24 19 43
for (; d < end && *d >= '0' && *d <= '9'; d++) 24 0 24
for (; d < end && *d >= '0' && *d <= '9'; d++) 24 0 24
925 {
926 24 ev = ev * 10 + (UV)(*d - '0');
927 24 ▶ if (ev > BIGNUM_MAX_PLAIN_EXP)
Branch: 1 branch
BranchTrueFalseTotal
if (ev > BIGNUM_MAX_PLAIN_EXP) 1 23 24
928 1 return sv_2mortal (res);
929 }
930 }
931 22 SvREFCNT_dec (res);
932 }
933 22 return NULL;
934 }
935  
936 INLINE int
937 147 is_bool_obj (pTHX_ SV *sv)
938 {
939 dMY_CXT;
940  
941 147 HV *bstash = MY_CXT.json_boolean_stash; /* JSON-XS-3.x interop (Types::Serialiser/JSON::PP::Boolean) */
942 147 HV *oldstash = MY_CXT.jsonold_boolean_stash; /* JSON-XS-2.x interop (JSON::XS::Boolean) */
943 147 HV *mstash = MY_CXT.mojo_boolean_stash; /* Mojo::JSON::_Bool interop */
944 147 HV *stash = SvSTASH (sv);
945  
946 147 ▶ return (stash == bstash || stash == mstash || stash == oldstash) ? 1 : 0;
Branch: 3 branches
BranchTrueFalseTotal
return (stash == bstash || stash == mstash || stash == oldstash) ? 1 : 0; 82 65 147
return (stash == bstash || stash == mstash || stash == oldstash) ? 1 : 0; 82 0 82
return (stash == bstash || stash == mstash || stash == oldstash) ? 1 : 0; 0 82 82
947 }
948  
949 /* target of scalar reference is bool? -1 == nope, 0 == false, 1 == true */
950 static int
951 50 ref_bool_type (pTHX_ SV *sv)
952 {
953 50 svtype svt = SvTYPE (sv);
954  
955 50 ▶ if (svt < SVt_PVAV)
Branch: 1 branch
BranchTrueFalseTotal
if (svt < SVt_PVAV) 27 23 50
956 {
957 27 STRLEN len = 0;
958 51 ▶ char *pv = svt ? SvPV_nomg (sv, len) : 0;
Branch: 1 branch
BranchTrueFalseTotal
char *pv = svt ? SvPV_nomg (sv, len) : 0; 24 3 27
959  
960 27 ▶ if (len == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (len == 1) { 9 18 27
961 9 ▶ if (*pv == '1')
Branch: 1 branch
BranchTrueFalseTotal
if (*pv == '1') 5 4 9
962 8 return 1;
963 4 ▶ else if (*pv == '0')
Branch: 1 branch
BranchTrueFalseTotal
else if (*pv == '0') 3 1 4
964 3 return 0;
965 }
966  
967 }
968  
969 42 return -1;
970 }
971  
972 /* returns whether scalar is not a reference in the sense of allow_nonref */
973 static int
974 36 json_nonref (pTHX_ SV *scalar)
975 {
976 36 ▶ if (!SvROK (scalar))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK (scalar)) 10 26 36
977 10 return 1;
978  
979 26 scalar = SvRV (scalar);
980  
981 26 ▶ if (!SvOBJECT (scalar) && ref_bool_type (aTHX_ scalar) >= 0)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOBJECT (scalar) && ref_bool_type (aTHX_ scalar) >= 0) 23 3 26
if (!SvOBJECT (scalar) && ref_bool_type (aTHX_ scalar) >= 0) 0 23 23
982 0 return 1;
983  
984 26 ▶ if (SvOBJECT (scalar) && is_bool_obj (aTHX_ scalar))
Branch: 2 branches
BranchTrueFalseTotal
if (SvOBJECT (scalar) && is_bool_obj (aTHX_ scalar)) 3 23 26
if (SvOBJECT (scalar) && is_bool_obj (aTHX_ scalar)) 3 0 3
985 3 return 1;
986
987 23 return 0;
988 }
989  
990 /*/////////////////////////////////////////////////////////////////////////// */
991 /* encoder */
992  
993 /* structure used for encoding JSON */
994 typedef struct
995 {
996 char *cur; /* SvPVX (sv) + current output position */
997 char *end; /* SvEND (sv) */
998 SV *sv; /* result scalar, also used as bounded chunk buffer when fp is set */
999 JSON json;
1000 JSON *orig_json; /* pointer to original JSON object (for recursion guard) */
1001 U32 indent; /* indentation level */
1002 UV limit; /* escape character values >= this value when encoding */
1003 PerlIO *fp; /* set by encode_to: stream output here instead of growing sv unbounded */
1004 UV written; /* total bytes flushed to fp so far */
1005 } enc_t;
1006  
1007 /* initial/streaming-chunk scalar size to be allocated. encode() keeps
1008 * growing this buffer to hold the whole result; encode_to() flushes it
1009 * to the filehandle and reuses it, so a larger chunk size only reduces
1010 * the number of write() syscalls, not peak memory. */
1011 #define STREAM_BUFSIZE 8192
1012  
1013 INLINE void
1014 932111 need (pTHX_ enc_t *enc, STRLEN len)
1015 {
1016 #if PERL_VERSION > 8 || (PERL_VERSION == 8 && PERL_SUBVERSION >= 1)
1017 DEBUG_v(Perl_deb(aTHX_ "need enc: %p %p %4ld, want: %lu\n", enc->cur, enc->end,
1018 (long)(enc->end - enc->cur), (unsigned long)len));
1019 #endif
1020 assert(enc->cur <= enc->end);
1021 932111 ▶ if (UNLIKELY(enc->cur + len >= enc->end))
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY(enc->cur + len >= enc->end)) 9498 922613 932111
1022 {
1023 9498 ▶ if (enc->fp)
Branch: 1 branch
BranchTrueFalseTotal
if (enc->fp) 49 9449 9498
1024 {
1025 49 STRLEN used = enc->cur - (char *)SvPVX (enc->sv);
1026 49 ▶ if (used)
Branch: 1 branch
BranchTrueFalseTotal
if (used) 49 0 49
1027 {
1028 49 ▶ if (PerlIO_write (enc->fp, SvPVX (enc->sv), used) != (SSize_t)used)
Branch: 1 branch
BranchTrueFalseTotal
if (PerlIO_write (enc->fp, SvPVX (enc->sv), used) != (SSize_t)used) 0 49 49
1029 0 croak ("Cpanel::JSON::XS::encode_to: error writing to filehandle: %s",
1030 Strerror (errno));
1031 49 enc->written += used;
1032 49 enc->cur = SvPVX (enc->sv);
1033 }
1034 49 ▶ if (UNLIKELY(enc->cur + len >= enc->end))
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY(enc->cur + len >= enc->end)) 0 49 49
1035 {
1036 /* single atom (e.g. a long string) bigger than our chunk buffer */
1037 0 ▶ SvGROW (enc->sv, len + 1);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW (enc->sv, len + 1); 0 0 0
SvGROW (enc->sv, len + 1); 0 0 0
1038 0 enc->cur = SvPVX (enc->sv);
1039 0 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
1040 }
1041 }
1042 else
1043 {
1044 9449 STRLEN cur = enc->cur - (char *)SvPVX (enc->sv);
1045 9449 ▶ SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); 9449 0 9449
SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); 7806 1643 9449
1046 9449 enc->cur = SvPVX (enc->sv) + cur;
1047 9449 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
1048 }
1049 }
1050 932111 }
1051  
1052 INLINE void
1053 560618 encode_ch (pTHX_ enc_t *enc, char ch)
1054 {
1055 560618 need (aTHX_ enc, 1);
1056 560618 *enc->cur++ = ch;
1057 560618 }
1058  
1059 static void
1060 141395 encode_str (pTHX_ enc_t *enc, char *str, STRLEN len, int is_utf8)
1061 {
1062 141395 char *end = str + len;
1063  
1064 #if PERL_VERSION < 8
1065 /* perl5.6 encodes to utf8 automatically, reverse it */
1066 if (is_utf8 && (enc->json.flags & F_BINARY))
1067 {
1068 str = (char *)utf8_to_bytes((U8*)str, &len);
1069 if (!str)
1070 croak ("illegal unicode character in binary string", str);
1071 end = str + len;
1072 }
1073 #endif
1074 141395 need (aTHX_ enc, len);
1075  
1076 310230 ▶ while (str < end)
Branch: 1 branch
BranchTrueFalseTotal
while (str < end) 168835 141395 310230
1077 {
1078 168835 unsigned char ch = *(unsigned char *)str;
1079 #if PERL_VERSION > 8 || (PERL_VERSION == 8 && PERL_SUBVERSION >= 1)
1080 DEBUG_v(Perl_deb(aTHX_ "str enc: %p %p %4ld, want: %lu\n", enc->cur, enc->end,
1081 (long)(enc->end - enc->cur), (long unsigned)len));
1082 #endif
1083 168835 ▶ if (LIKELY(ch >= 0x20 && ch < 0x80)) /* most common case */
Branch: 2 branches
BranchTrueFalseTotal
if (LIKELY(ch >= 0x20 && ch < 0x80)) /* most common case */ 165509 3326 168835
if (LIKELY(ch >= 0x20 && ch < 0x80)) /* most common case */ 138840 26669 165509
1084 {
1085 assert(enc->cur <= enc->end);
1086 138840 ▶ if (UNLIKELY(ch == '"')) /* but with slow exceptions */
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY(ch == '"')) /* but with slow exceptions */ 3797 135043 138840
1087 {
1088 3797 need (aTHX_ enc, 2);
1089 3797 *enc->cur++ = '\\';
1090 3797 *enc->cur++ = '"';
1091 3797 ++len;
1092 }
1093 135043 ▶ else if (UNLIKELY(ch == '\\'))
Branch: 1 branch
BranchTrueFalseTotal
else if (UNLIKELY(ch == '\\')) 764 134279 135043
1094 {
1095 764 need (aTHX_ enc, 2);
1096 764 *enc->cur++ = '\\';
1097 764 *enc->cur++ = '\\';
1098 764 ++len;
1099 }
1100 134279 ▶ else if (UNLIKELY(ch == '/' && (enc->json.flags & F_ESCAPE_SLASH)))
Branch: 2 branches
BranchTrueFalseTotal
else if (UNLIKELY(ch == '/' && (enc->json.flags & F_ESCAPE_SLASH))) 10353 123926 134279
else if (UNLIKELY(ch == '/' && (enc->json.flags & F_ESCAPE_SLASH))) 1 10352 10353
1101 {
1102 1 need (aTHX_ enc, 2);
1103 1 *enc->cur++ = '\\';
1104 1 *enc->cur++ = '/';
1105 1 ++len;
1106 }
1107 else
1108 134278 {
1109 /* fast path: bulk-copy a run of consecutive bytes that need no
1110 * escaping, instead of a need()+store per byte (GH #237). */
1111 134278 char *run_start = str;
1112 134278 int esc_slash = !!(enc->json.flags & F_ESCAPE_SLASH);
1113 STRLEN run;
1114  
1115 do
1116 2091418 ++str;
1117 while (str < end
1118 1961844 ▶ && (ch = *(unsigned char *)str) >= 0x20 && ch < 0x80
Branch: 2 branches
BranchTrueFalseTotal
&& (ch = *(unsigned char *)str) >= 0x20 && ch < 0x80 1961322 522 1961844
&& (ch = *(unsigned char *)str) >= 0x20 && ch < 0x80 1960844 478 1961322
1119 1960844 ▶ && ch != '"' && ch != '\\'
Branch: 2 branches
BranchTrueFalseTotal
&& ch != '"' && ch != '\\' 1957501 3343 1960844
&& ch != '"' && ch != '\\' 1957140 361 1957501
1120 4048558 ▶ && !(esc_slash && ch == '/'));
Branch: 3 branches
BranchTrueFalseTotal
&& !(esc_slash && ch == '/')); 1961844 129574 2091418
&& !(esc_slash && ch == '/')); 1957140 0 1957140
&& !(esc_slash && ch == '/')); 0 0 0
1121  
1122 134278 run = str - run_start;
1123 134278 need (aTHX_ enc, run);
1124 134278 memcpy (enc->cur, run_start, run);
1125 134278 enc->cur += run;
1126 134278 len -= run;
1127 134278 continue;
1128 }
1129  
1130 4562 ++str;
1131 }
1132 else
1133 {
1134 assert(enc->cur <= enc->end);
1135 29995 switch (ch)
1136 {
1137 81 case '\010': need (aTHX_ enc, 2);
1138 81 *enc->cur++ = '\\'; *enc->cur++ = 'b'; ++len; ++str; break;
1139 248 case '\011': need (aTHX_ enc, 2);
1140 248 *enc->cur++ = '\\'; *enc->cur++ = 't'; ++len; ++str; break;
1141 475 case '\012': need (aTHX_ enc, 2);
1142 475 *enc->cur++ = '\\'; *enc->cur++ = 'n'; ++len; ++str; break;
1143 66 case '\014': need (aTHX_ enc, 2);
1144 66 *enc->cur++ = '\\'; *enc->cur++ = 'f'; ++len; ++str; break;
1145 61 case '\015': need (aTHX_ enc, 2);
1146 61 *enc->cur++ = '\\'; *enc->cur++ = 'r'; ++len; ++str; break;
1147  
1148 29064 default:
1149 {
1150 STRLEN clen;
1151 UV uch;
1152  
1153 29064 ▶ if (is_utf8 && !(enc->json.flags & F_BINARY))
Branch: 2 branches
BranchTrueFalseTotal
if (is_utf8 && !(enc->json.flags & F_BINARY)) 23653 5411 29064
if (is_utf8 && !(enc->json.flags & F_BINARY)) 21636 2017 23653
1154 {
1155 21636 uch = decode_utf8 (aTHX_ (unsigned char *)str, end - str,
1156 21636 enc->json.flags & F_RELAXED, &clen);
1157 21636 ▶ if (clen == (STRLEN)-1)
Branch: 1 branch
BranchTrueFalseTotal
if (clen == (STRLEN)-1) 0 21636 21636
1158 0 croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str);
1159 }
1160 else
1161 {
1162 7428 uch = ch;
1163 7428 clen = 1;
1164 }
1165  
1166 29064 ▶ if (uch < 0x80/*0x20*/ || uch >= enc->limit)
Branch: 2 branches
BranchTrueFalseTotal
if (uch < 0x80/*0x20*/ || uch >= enc->limit) 26669 2395 29064
if (uch < 0x80/*0x20*/ || uch >= enc->limit) 5211 21458 26669
1167 {
1168 7606 ▶ if (enc->json.flags & F_BINARY)
Branch: 1 branch
BranchTrueFalseTotal
if (enc->json.flags & F_BINARY) 5015 2591 7606
1169 {
1170 /* MB cannot arrive here */
1171 5015 need (aTHX_ enc, 4);
1172 5015 *enc->cur++ = '\\';
1173 5015 *enc->cur++ = 'x';
1174 5015 *enc->cur++ = PL_hexdigit [(uch >> 4) & 15];
1175 5015 *enc->cur++ = PL_hexdigit [ uch & 15];
1176 5015 len += 3;
1177 }
1178 2591 ▶ else if (uch >= 0x10000UL)
Branch: 1 branch
BranchTrueFalseTotal
else if (uch >= 0x10000UL) 327 2264 2591
1179 {
1180 327 ▶ if (uch >= 0x110000UL)
Branch: 1 branch
BranchTrueFalseTotal
if (uch >= 0x110000UL) 0 327 327
1181 0 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch);
1182  
1183 327 need (aTHX_ enc, 12);
1184 327 sprintf (enc->cur, "\\u%04x\\u%04x",
1185 327 (int)((uch - 0x10000) / 0x400 + 0xD800),
1186 327 (int)((uch - 0x10000) % 0x400 + 0xDC00));
1187 327 enc->cur += 12;
1188 327 len += 11;
1189 }
1190 else
1191 {
1192 2264 need (aTHX_ enc, 6);
1193 2264 *enc->cur++ = '\\';
1194 2264 *enc->cur++ = 'u';
1195 2264 *enc->cur++ = PL_hexdigit [ uch >> 12 ];
1196 2264 *enc->cur++ = PL_hexdigit [(uch >> 8) & 15];
1197 2264 *enc->cur++ = PL_hexdigit [(uch >> 4) & 15];
1198 2264 *enc->cur++ = PL_hexdigit [ uch & 15];
1199 2264 len += 5;
1200 }
1201  
1202 7606 str += clen;
1203 }
1204 21458 ▶ else if (enc->json.flags & F_LATIN1)
Branch: 1 branch
BranchTrueFalseTotal
else if (enc->json.flags & F_LATIN1) 8 21450 21458
1205 {
1206 8 need (aTHX_ enc, 1);
1207 8 *enc->cur++ = (unsigned char)uch;
1208 8 str += clen;
1209 }
1210 21450 ▶ else if (enc->json.flags & F_BINARY)
Branch: 1 branch
BranchTrueFalseTotal
else if (enc->json.flags & F_BINARY) 0 21450 21450
1211 {
1212 0 need (aTHX_ enc, 1);
1213 0 *enc->cur++ = (unsigned char)uch;
1214 0 str += clen;
1215 }
1216 21450 ▶ else if (is_utf8)
Branch: 1 branch
BranchTrueFalseTotal
else if (is_utf8) 20919 531 21450
1217 {
1218 20919 need (aTHX_ enc, clen);
1219 20919 len += clen;
1220 do
1221 {
1222 62779 *enc->cur++ = *str++;
1223 }
1224 62779 ▶ while (--clen);
Branch: 1 branch
BranchTrueFalseTotal
while (--clen); 41860 20919 62779
1225 }
1226 else
1227 { /* never more than 11 bytes needed */
1228 531 need (aTHX_ enc, UTF8_MAXBYTES);
1229 531 enc->cur = (char*)encode_utf8 ((U8*)enc->cur, uch);
1230 531 ++str;
1231 531 len += UTF8_MAXBYTES - 1;
1232 }
1233 }
1234 }
1235 }
1236  
1237 34557 --len;
1238 }
1239 141395 }
1240  
1241 INLINE void
1242 6565 encode_const_str (pTHX_ enc_t *enc, const char *str, STRLEN len, int is_utf8)
1243 {
1244 6565 encode_str (aTHX_ enc, (char *)str, len, is_utf8);
1245 6565 }
1246  
1247 INLINE void
1248 202045 encode_indent (pTHX_ enc_t *enc)
1249 {
1250 202045 ▶ if (enc->json.flags & F_INDENT)
Branch: 1 branch
BranchTrueFalseTotal
if (enc->json.flags & F_INDENT) 48 201997 202045
1251 {
1252 48 int spaces = enc->indent * enc->json.indent_length;
1253  
1254 48 need (aTHX_ enc, spaces);
1255 48 memset (enc->cur, ' ', spaces);
1256 48 enc->cur += spaces;
1257 }
1258 202045 }
1259  
1260 INLINE void
1261 24 encode_space (pTHX_ enc_t *enc)
1262 {
1263 24 encode_ch (aTHX_ enc, ' ');
1264 24 }
1265  
1266 INLINE void
1267 91279 encode_nl (pTHX_ enc_t *enc)
1268 {
1269 91279 ▶ if (enc->json.flags & F_INDENT)
Branch: 1 branch
BranchTrueFalseTotal
if (enc->json.flags & F_INDENT) 58 91221 91279
1270 {
1271 58 encode_ch (aTHX_ enc, '\n');
1272 }
1273 91279 }
1274  
1275 INLINE void
1276 112427 encode_comma (pTHX_ enc_t *enc)
1277 {
1278 112427 encode_ch (aTHX_ enc, ',');
1279  
1280 112427 ▶ if (enc->json.flags & F_INDENT)
Branch: 1 branch
BranchTrueFalseTotal
if (enc->json.flags & F_INDENT) 12 112415 112427
1281 12 encode_nl (aTHX_ enc);
1282 112415 ▶ else if (enc->json.flags & F_SPACE_AFTER)
Branch: 1 branch
BranchTrueFalseTotal
else if (enc->json.flags & F_SPACE_AFTER) 2 112413 112415
1283 2 encode_space (aTHX_ enc);
1284 112427 }
1285  
1286 static void encode_sv (pTHX_ enc_t *enc, SV *sv, SV *typesv);
1287  
1288 static void
1289 18688 encode_av (pTHX_ enc_t *enc, AV *av, SV *typesv)
1290 {
1291 18688 AV *typeav = NULL;
1292 18688 HVMAX_T i, len = av_len (av);
1293  
1294 18688 ▶ if (enc->indent >= enc->json.max_depth)
Branch: 1 branch
BranchTrueFalseTotal
if (enc->indent >= enc->json.max_depth) 1 18687 18688
1295 1 croak (ERR_NESTING_EXCEEDED);
1296  
1297 18687 ▶ if (UNLIKELY (SvOK (typesv)))
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY (SvOK (typesv))) 24 18663 18687
1298 {
1299 24 ▶ if (SvROK (typesv) &&
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK (typesv) && 24 0 24
1300 24 ▶ SvOBJECT (SvRV (typesv)) &&
Branch: 1 branch
BranchTrueFalseTotal
SvOBJECT (SvRV (typesv)) && 17 7 24
1301 17 ▶ SvTYPE (SvRV (typesv)) == SVt_PVAV)
Branch: 1 branch
BranchTrueFalseTotal
SvTYPE (SvRV (typesv)) == SVt_PVAV) 11 6 17
1302 {
1303 11 HV *stash = SvSTASH (SvRV (typesv));
1304 11 ▶ char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL;
Branch: 7 branches
BranchTrueFalseTotal
char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL; 11 0 11
char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL; 11 0 11
char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL; 11 0 11
char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL; 0 11 11
char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL; 0 0 0
char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL; 11 0 11
char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL; 0 11 11
1305 11 ▶ if (LIKELY (name && strEQ (name, JSON_TYPE_ANYOF_CLASS)))
Branch: 2 branches
BranchTrueFalseTotal
if (LIKELY (name && strEQ (name, JSON_TYPE_ANYOF_CLASS))) 11 0 11
if (LIKELY (name && strEQ (name, JSON_TYPE_ANYOF_CLASS))) 11 0 11
1306 {
1307 11 AV *type_any = (AV *)SvRV (typesv);
1308 11 SV **typesv_ref = av_fetch (type_any, JSON_TYPE_ANYOF_ARRAY_INDEX, 0);
1309 11 ▶ if (UNLIKELY (!typesv_ref))
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY (!typesv_ref)) 0 11 11
1310 0 ▶ croak ("incorrectly constructed anyof type (%s, 0x%x) was specified for '%s'",
Branch: 1 branch
BranchTrueFalseTotal
croak ("incorrectly constructed anyof type (%s, 0x%x) was specified for '%s'", 0 0 0
1311 SvPV_nolen (typesv), (unsigned int)SvFLAGS (typesv),
1312 SvPV_nolen (sv_2mortal (newRV_inc ((SV *)av))));
1313 11 typesv = *typesv_ref;
1314 11 SvGETMAGIC (typesv);
1315 11 ▶ if (!SvOK (typesv))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK (typesv)) 0 11 11
1316 0 croak ("no array alternative in anyof was specified for '%s'",
1317 SvPV_nolen (sv_2mortal (newRV_inc ((SV *)av))));
1318 }
1319 }
1320  
1321 24 ▶ if (UNLIKELY (!SvROK (typesv)))
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY (!SvROK (typesv))) 0 24 24
1322 0 ▶ croak ("encountered type (%s, 0x%x) was specified for '%s'",
Branch: 1 branch
BranchTrueFalseTotal
croak ("encountered type (%s, 0x%x) was specified for '%s'", 0 0 0
1323 SvPV_nolen (typesv), (unsigned int)SvFLAGS (typesv),
1324 SvPV_nolen (sv_2mortal (newRV_inc ((SV *)av))));
1325  
1326 24 ▶ if (!SvOBJECT (SvRV (typesv)) && SvTYPE (SvRV (typesv)) == SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOBJECT (SvRV (typesv)) && SvTYPE (SvRV (typesv)) == SVt_PVAV) 10 14 24
if (!SvOBJECT (SvRV (typesv)) && SvTYPE (SvRV (typesv)) == SVt_PVAV) 10 0 10
1327 {
1328 10 typeav = (AV *)SvRV (typesv);
1329 10 ▶ if (len != av_len (typeav))
Branch: 1 branch
BranchTrueFalseTotal
if (len != av_len (typeav)) 0 10 10
1330 0 croak ("array '%s' has different number of elements as in specified type '%s'",
1331 SvPV_nolen (sv_2mortal (newRV_inc ((SV *)av))),
1332 SvPV_nolen (typesv));
1333 }
1334 14 ▶ else if (SvOBJECT (SvRV (typesv)) &&
Branch: 1 branch
BranchTrueFalseTotal
else if (SvOBJECT (SvRV (typesv)) && 14 0 14
1335 14 ▶ SvTYPE (SvRV (typesv)) < SVt_PVAV)
Branch: 1 branch
BranchTrueFalseTotal
SvTYPE (SvRV (typesv)) < SVt_PVAV) 14 0 14
1336 14 {
1337 14 HV *stash = SvSTASH (SvRV (typesv));
1338 14 ▶ char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL;
Branch: 7 branches
BranchTrueFalseTotal
char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL; 14 0 14
char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL; 14 0 14
char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL; 14 0 14
char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL; 0 14 14
char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL; 0 0 0
char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL; 14 0 14
char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL; 0 14 14
1339 14 ▶ if (LIKELY (name && strEQ (name, JSON_TYPE_ARRAYOF_CLASS)))
Branch: 2 branches
BranchTrueFalseTotal
if (LIKELY (name && strEQ (name, JSON_TYPE_ARRAYOF_CLASS))) 14 0 14
if (LIKELY (name && strEQ (name, JSON_TYPE_ARRAYOF_CLASS))) 14 0 14
1340 14 typesv = (SV *)SvRV (typesv);
1341 else
1342 0 ▶ croak ("encountered type (%s, 0x%x) was specified for '%s'",
Branch: 1 branch
BranchTrueFalseTotal
croak ("encountered type (%s, 0x%x) was specified for '%s'", 0 0 0
1343 SvPV_nolen (typesv), (unsigned int)SvFLAGS (typesv),
1344 SvPV_nolen (sv_2mortal (newRV_inc ((SV *)av))));
1345 }
1346 else
1347 0 ▶ croak ("encountered type (%s, 0x%x) was specified for '%s'",
Branch: 1 branch
BranchTrueFalseTotal
croak ("encountered type (%s, 0x%x) was specified for '%s'", 0 0 0
1348 SvPV_nolen (typesv), (unsigned int)SvFLAGS (typesv),
1349 SvPV_nolen (sv_2mortal (newRV_inc ((SV *)av))));
1350 }
1351  
1352 18687 encode_ch (aTHX_ enc, '[');
1353
1354 18687 ▶ if (len >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (len >= 0) 18607 80 18687
1355 {
1356 18607 encode_nl (aTHX_ enc); ++enc->indent;
1357  
1358 87472 ▶ for (i = 0; i <= len; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= len; ++i) 68880 18592 87472
1359 {
1360 68880 SV **svp = av_fetch (av, i, 0);
1361  
1362 68880 ▶ if (typeav)
Branch: 1 branch
BranchTrueFalseTotal
if (typeav) 24 68856 68880
1363 {
1364 24 SV **typerv = av_fetch (typeav, i, 0);
1365 24 ▶ if (typerv)
Branch: 1 branch
BranchTrueFalseTotal
if (typerv) 24 0 24
1366 24 typesv = *typerv;
1367 }
1368  
1369 68880 encode_indent (aTHX_ enc);
1370  
1371 68880 ▶ if (svp)
Branch: 1 branch
BranchTrueFalseTotal
if (svp) 68865 15 68880
1372 68865 encode_sv (aTHX_ enc, *svp, typesv);
1373 else
1374 15 encode_const_str (aTHX_ enc, "null", 4, 0);
1375  
1376 68865 ▶ if (i < len)
Branch: 1 branch
BranchTrueFalseTotal
if (i < len) 50273 18592 68865
1377 50273 encode_comma (aTHX_ enc);
1378 }
1379  
1380 18592 encode_nl (aTHX_ enc); --enc->indent; encode_indent (aTHX_ enc);
1381 }
1382
1383 18672 encode_ch (aTHX_ enc, ']');
1384 18672 }
1385  
1386 INLINE void
1387 88367 retrieve_hk (pTHX_ HE *he, char **key, I32 *klen)
1388 {
1389 int utf8;
1390  
1391 88367 ▶ if (He_IS_SVKEY(he))
Branch: 1 branch
BranchTrueFalseTotal
if (He_IS_SVKEY(he)) 11 88356 88367
1392 {
1393 STRLEN len;
1394 11 ▶ SV *sv = HeSVKEY (he);
Branch: 2 branches
BranchTrueFalseTotal
SV *sv = HeSVKEY (he); 11 0 11
SV *sv = HeSVKEY (he); 11 0 11
1395 11 *key = SvPV (sv, len);
1396 11 *klen = (I32)len;
1397 11 utf8 = SvUTF8 (sv);
1398 }
1399 else
1400 {
1401 88356 *key = HeKEY (he);
1402 88356 *klen = HeKLEN (he);
1403 88356 utf8 = HeKUTF8 (he);
1404 }
1405  
1406 88367 ▶ if (utf8) *klen = -(*klen);
Branch: 1 branch
BranchTrueFalseTotal
if (utf8) *klen = -(*klen); 17 88350 88367
1407 88367 }
1408  
1409 static void
1410 88367 encode_hk (pTHX_ enc_t *enc, char *key, I32 klen)
1411 {
1412 88367 encode_ch (aTHX_ enc, '"');
1413 88367 encode_str (aTHX_ enc, key, klen < 0 ? -klen : klen, klen < 0);
1414 88367 encode_ch (aTHX_ enc, '"');
1415  
1416 88367 ▶ if (enc->json.flags & F_SPACE_BEFORE) encode_space (aTHX_ enc);
Branch: 1 branch
BranchTrueFalseTotal
if (enc->json.flags & F_SPACE_BEFORE) encode_space (aTHX_ enc); 11 88356 88367
1417 88367 encode_ch (aTHX_ enc, ':');
1418 88367 ▶ if (enc->json.flags & F_SPACE_AFTER ) encode_space (aTHX_ enc);
Branch: 1 branch
BranchTrueFalseTotal
if (enc->json.flags & F_SPACE_AFTER ) encode_space (aTHX_ enc); 11 88356 88367
1419 88367 }
1420  
1421 /* compare hash entries, used when all keys are bytestrings */
1422 static int
1423 28389 he_cmp_fast (const void *a_, const void *b_)
1424 {
1425 int cmp;
1426  
1427 28389 HE *a = *(HE **)a_;
1428 28389 HE *b = *(HE **)b_;
1429  
1430 28389 STRLEN la = HeKLEN (a);
1431 28389 STRLEN lb = HeKLEN (b);
1432  
1433 28389 ▶ if (!(cmp = memcmp (HeKEY (b), HeKEY (a), lb < la ? lb : la)))
Branch: 1 branch
BranchTrueFalseTotal
if (!(cmp = memcmp (HeKEY (b), HeKEY (a), lb < la ? lb : la))) 235 28154 28389
1434 235 cmp = lb - la;
1435  
1436 28389 return cmp;
1437 }
1438  
1439 /*
1440 * Compare two UTF-8 strings by UTF-16 code unit order per RFC 8785 (JCS).
1441 * Characters outside the BMP (U+10000+) expand to surrogate pairs,
1442 * and the comparison is by the resulting 16-bit code unit array.
1443 * Returns negative if a_sv < b_sv, 0 if equal, positive if a_sv > b_sv.
1444 */
1445 static int
1446 401 utf16_cmp (pTHX_ SV *a_sv, SV *b_sv)
1447 {
1448 STRLEN alen, blen;
1449 401 ▶ U8 *ap = (U8 *)SvPV_const (a_sv, alen);
Branch: 1 branch
BranchTrueFalseTotal
U8 *ap = (U8 *)SvPV_const (a_sv, alen); 401 0 401
1450 401 U8 *bp = (U8 *)SvPV_const (b_sv, blen);
1451 401 U16 pending_a = 0, pending_b = 0;
1452  
1453 for (;;)
1454 60 {
1455 U32 a_unit, b_unit;
1456  
1457 461 ▶ if (pending_a)
Branch: 1 branch
BranchTrueFalseTotal
if (pending_a) 3 458 461
1458 {
1459 3 a_unit = pending_a;
1460 3 pending_a = 0;
1461 }
1462 458 ▶ else if (alen && !SvUTF8 (a_sv))
Branch: 2 branches
BranchTrueFalseTotal
else if (alen && !SvUTF8 (a_sv)) 446 12 458
else if (alen && !SvUTF8 (a_sv)) 423 23 446
1463 {
1464 423 a_unit = *ap++; /* byte string: each byte is one Latin-1 character */
1465 423 alen--;
1466 }
1467 35 ▶ else if (alen)
Branch: 1 branch
BranchTrueFalseTotal
else if (alen) 23 12 35
1468 {
1469 {
1470 UV uv;
1471 23 STRLEN clen = 0;
1472 #if PERL_VERSION >= 32
1473 23 uv = utf8n_to_uvchr (ap, alen, &clen, UTF8_CHECK_ONLY);
1474 #elif PERL_VERSION >= 8
1475 uv = utf8n_to_uvuni (ap, alen, &clen, UTF8_CHECK_ONLY);
1476 #else
1477 uv = utf8_to_uv (ap, alen, &clen, UTF8_CHECK_ONLY);
1478 #endif
1479 23 ▶ if (UNLIKELY (clen == 0 || clen > alen))
Branch: 2 branches
BranchTrueFalseTotal
if (UNLIKELY (clen == 0 || clen > alen)) 23 0 23
if (UNLIKELY (clen == 0 || clen > alen)) 0 23 23
1480 {
1481 /* invalid UTF-8, treat as raw byte */
1482 0 a_unit = *ap++;
1483 0 alen--;
1484 }
1485 else
1486 {
1487 23 ap += clen;
1488 23 alen -= clen;
1489 23 ▶ if (uv >= 0x10000)
Branch: 1 branch
BranchTrueFalseTotal
if (uv >= 0x10000) 6 17 23
1490 {
1491 6 uv -= 0x10000;
1492 6 a_unit = 0xD800 | (uv >> 10);
1493 6 pending_a = 0xDC00 | (uv & 0x3FF);
1494 }
1495 else
1496 17 a_unit = (U16)uv;
1497 }
1498 }
1499 }
1500 else
1501 12 a_unit = 0x10000; /* sentinel: beyond valid 16-bit range */
1502  
1503 461 ▶ if (pending_b)
Branch: 1 branch
BranchTrueFalseTotal
if (pending_b) 3 458 461
1504 {
1505 3 b_unit = pending_b;
1506 3 pending_b = 0;
1507 }
1508 458 ▶ else if (blen && !SvUTF8 (b_sv))
Branch: 2 branches
BranchTrueFalseTotal
else if (blen && !SvUTF8 (b_sv)) 453 5 458
else if (blen && !SvUTF8 (b_sv)) 435 18 453
1509 {
1510 435 b_unit = *bp++; /* byte string: each byte is one Latin-1 character */
1511 435 blen--;
1512 }
1513 23 ▶ else if (blen)
Branch: 1 branch
BranchTrueFalseTotal
else if (blen) 18 5 23
1514 {
1515 {
1516 UV uv;
1517 18 STRLEN clen = 0;
1518 #if PERL_VERSION >= 32
1519 18 uv = utf8n_to_uvchr (bp, blen, &clen, UTF8_CHECK_ONLY);
1520 #elif PERL_VERSION >= 8
1521 uv = utf8n_to_uvuni (bp, blen, &clen, UTF8_CHECK_ONLY);
1522 #else
1523 uv = utf8_to_uv (bp, blen, &clen, UTF8_CHECK_ONLY);
1524 #endif
1525 18 ▶ if (UNLIKELY (clen == 0 || clen > blen))
Branch: 2 branches
BranchTrueFalseTotal
if (UNLIKELY (clen == 0 || clen > blen)) 18 0 18
if (UNLIKELY (clen == 0 || clen > blen)) 0 18 18
1526 {
1527 /* invalid UTF-8, treat as raw byte */
1528 0 b_unit = *bp++;
1529 0 blen--;
1530 }
1531 else
1532 {
1533 18 bp += clen;
1534 18 blen -= clen;
1535 18 ▶ if (uv >= 0x10000)
Branch: 1 branch
BranchTrueFalseTotal
if (uv >= 0x10000) 5 13 18
1536 {
1537 5 uv -= 0x10000;
1538 5 b_unit = 0xD800 | (uv >> 10);
1539 5 pending_b = 0xDC00 | (uv & 0x3FF);
1540 }
1541 else
1542 13 b_unit = (U16)uv;
1543 }
1544 }
1545 }
1546 else
1547 5 b_unit = 0x10000;
1548  
1549 461 ▶ if (a_unit == 0x10000 && b_unit == 0x10000)
Branch: 2 branches
BranchTrueFalseTotal
if (a_unit == 0x10000 && b_unit == 0x10000) 12 449 461
if (a_unit == 0x10000 && b_unit == 0x10000) 0 12 12
1550 0 return 0;
1551 461 ▶ if (a_unit == 0x10000)
Branch: 1 branch
BranchTrueFalseTotal
if (a_unit == 0x10000) 12 449 461
1552 12 return -1;
1553 449 ▶ if (b_unit == 0x10000)
Branch: 1 branch
BranchTrueFalseTotal
if (b_unit == 0x10000) 5 444 449
1554 5 return 1;
1555 444 ▶ if (a_unit != b_unit)
Branch: 1 branch
BranchTrueFalseTotal
if (a_unit != b_unit) 384 60 444
1556 384 ▶ return a_unit < b_unit ? -1 : 1;
Branch: 1 branch
BranchTrueFalseTotal
return a_unit < b_unit ? -1 : 1; 194 190 384
1557 }
1558 }
1559  
1560 /* compare hash entries, used when some keys are SV's or UTF-8 */
1561 static int
1562 388 he_cmp_slow (const void *a, const void *b)
1563 {
1564 dTHX;
1565 388 ▶ return utf16_cmp (aTHX_ HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a));
Branch: 4 branches
BranchTrueFalseTotal
return utf16_cmp (aTHX_ HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a)); 388 0 388
return utf16_cmp (aTHX_ HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a)); 0 388 388
return utf16_cmp (aTHX_ HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a)); 388 0 388
return utf16_cmp (aTHX_ HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a)); 0 388 388
1566 }
1567  
1568 /* Private copy of one entry of a tied hash: a HE plus room for a
1569 HEK which holds only an SV* key */
1570 struct tied_entry_copy
1571 {
1572 HE he;
1573 char hek[sizeof (struct hek) + sizeof (SV *) + 2];
1574 };
1575  
1576 /* compare tied hash entries, guaranteed SV's */
1577 static int
1578 13 he_cmp_tied (const void *a, const void *b)
1579 {
1580 dTHX;
1581 /* skip GMAGIC */
1582 13 return utf16_cmp (aTHX_ HeKEY_sv (*(HE **)b), HeKEY_sv (*(HE **)a));
1583 }
1584  
1585 static void
1586 26225 encode_hv (pTHX_ enc_t *enc, HV *hv, SV *typesv)
1587 {
1588 26225 HV *typehv = NULL;
1589 HE *he;
1590  
1591 26225 ▶ if (enc->indent >= enc->json.max_depth)
Branch: 1 branch
BranchTrueFalseTotal
if (enc->indent >= enc->json.max_depth) 1 26224 26225
1592 1 croak (ERR_NESTING_EXCEEDED);
1593  
1594 26224 ▶ if (UNLIKELY (SvOK (typesv)))
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY (SvOK (typesv))) 29 26195 26224
1595 {
1596 29 ▶ if (SvROK (typesv) &&
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK (typesv) && 29 0 29
1597 29 ▶ SvOBJECT (SvRV (typesv)) &&
Branch: 1 branch
BranchTrueFalseTotal
SvOBJECT (SvRV (typesv)) && 7 22 29
1598 7 ▶ SvTYPE (SvRV (typesv)) == SVt_PVAV)
Branch: 1 branch
BranchTrueFalseTotal
SvTYPE (SvRV (typesv)) == SVt_PVAV) 4 3 7
1599 {
1600 4 HV *stash = SvSTASH (SvRV (typesv));
1601 4 ▶ char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL;
Branch: 7 branches
BranchTrueFalseTotal
char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL; 4 0 4
char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL; 4 0 4
char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL; 4 0 4
char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL; 0 4 4
char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL; 0 0 0
char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL; 4 0 4
char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL; 0 4 4
1602 4 ▶ if (LIKELY (name && strEQ (name, JSON_TYPE_ANYOF_CLASS)))
Branch: 2 branches
BranchTrueFalseTotal
if (LIKELY (name && strEQ (name, JSON_TYPE_ANYOF_CLASS))) 4 0 4
if (LIKELY (name && strEQ (name, JSON_TYPE_ANYOF_CLASS))) 4 0 4
1603 {
1604 4 AV *type_any = (AV *)SvRV (typesv);
1605 4 SV **typesv_ref = av_fetch (type_any, JSON_TYPE_ANYOF_HASH_INDEX, 0);
1606 4 ▶ if (UNLIKELY (!typesv_ref))
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY (!typesv_ref)) 0 4 4
1607 0 ▶ croak ("incorrectly constructed anyof type (%s, 0x%x) was specified for '%s'",
Branch: 1 branch
BranchTrueFalseTotal
croak ("incorrectly constructed anyof type (%s, 0x%x) was specified for '%s'", 0 0 0
1608 SvPV_nolen (typesv), (unsigned int)SvFLAGS (typesv),
1609 SvPV_nolen (sv_2mortal (newRV_inc ((SV *)hv))));
1610 4 typesv = *typesv_ref;
1611 4 SvGETMAGIC (typesv);
1612 4 ▶ if (!SvOK (typesv))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK (typesv)) 0 4 4
1613 0 croak ("no hash alternative in anyof was specified for '%s'",
1614 SvPV_nolen (sv_2mortal (newRV_inc ((SV *)hv))));
1615 }
1616 }
1617  
1618 29 ▶ if (UNLIKELY (!SvROK (typesv)))
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY (!SvROK (typesv))) 0 29 29
1619 0 ▶ croak ("encountered type (%s, 0x%x) was specified for '%s'",
Branch: 1 branch
BranchTrueFalseTotal
croak ("encountered type (%s, 0x%x) was specified for '%s'", 0 0 0
1620 SvPV_nolen (typesv), (unsigned int)SvFLAGS (typesv),
1621 SvPV_nolen (sv_2mortal (newRV_inc ((SV *)hv))));
1622  
1623 29 ▶ if (!SvOBJECT (SvRV (typesv)) && SvTYPE (SvRV (typesv)) == SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOBJECT (SvRV (typesv)) && SvTYPE (SvRV (typesv)) == SVt_PVHV) 24 5 29
if (!SvOBJECT (SvRV (typesv)) && SvTYPE (SvRV (typesv)) == SVt_PVHV) 24 0 24
1624 24 typehv = (HV *)SvRV (typesv);
1625 5 ▶ else if (SvOBJECT (SvRV (typesv)) &&
Branch: 1 branch
BranchTrueFalseTotal
else if (SvOBJECT (SvRV (typesv)) && 5 0 5
1626 5 ▶ SvTYPE (SvRV (typesv)) < SVt_PVAV)
Branch: 1 branch
BranchTrueFalseTotal
SvTYPE (SvRV (typesv)) < SVt_PVAV) 5 0 5
1627 5 {
1628 5 HV *stash = SvSTASH (SvRV (typesv));
1629 5 ▶ char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL;
Branch: 7 branches
BranchTrueFalseTotal
char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL; 5 0 5
char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL; 5 0 5
char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL; 5 0 5
char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL; 0 5 5
char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL; 0 0 0
char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL; 5 0 5
char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL; 0 5 5
1630 5 ▶ if (LIKELY (name && strEQ (name, JSON_TYPE_HASHOF_CLASS)))
Branch: 2 branches
BranchTrueFalseTotal
if (LIKELY (name && strEQ (name, JSON_TYPE_HASHOF_CLASS))) 5 0 5
if (LIKELY (name && strEQ (name, JSON_TYPE_HASHOF_CLASS))) 5 0 5
1631 5 typesv = (SV *)SvRV (typesv);
1632 else
1633 0 ▶ croak ("encountered type (%s, 0x%x) was specified for '%s'",
Branch: 1 branch
BranchTrueFalseTotal
croak ("encountered type (%s, 0x%x) was specified for '%s'", 0 0 0
1634 SvPV_nolen (typesv), (unsigned int)SvFLAGS (typesv),
1635 SvPV_nolen (sv_2mortal (newRV_inc ((SV *)hv))));
1636 }
1637 else
1638 0 ▶ croak ("encountered type (%s, 0x%x) was specified for '%s'",
Branch: 1 branch
BranchTrueFalseTotal
croak ("encountered type (%s, 0x%x) was specified for '%s'", 0 0 0
1639 SvPV_nolen (typesv), (unsigned int)SvFLAGS (typesv),
1640 SvPV_nolen (sv_2mortal (newRV_inc ((SV *)hv))));
1641 }
1642  
1643  
1644 26224 encode_ch (aTHX_ enc, '{');
1645  
1646 /* for canonical output we have to sort by keys first */
1647 /* caused by randomised hash orderings or unknown tied behaviour. */
1648 26224 ▶ if (enc->json.flags & F_CANONICAL)
Branch: 1 branch
BranchTrueFalseTotal
if (enc->json.flags & F_CANONICAL) 10275 15949 26224
1649 {
1650 10275 RITER_T i, count = hv_iterinit (hv);
1651 HE *hes_stack [STACK_HES];
1652 10275 HE **hes = hes_stack;
1653 10275 int is_tied = 0;
1654  
1655 10275 ▶ if (SvMAGICAL (hv))
Branch: 1 branch
BranchTrueFalseTotal
if (SvMAGICAL (hv)) 4 10271 10275
1656 {
1657 4 ▶ if (SvTIED_mg((SV*)hv, PERL_MAGIC_tied))
Branch: 2 branches
BranchTrueFalseTotal
if (SvTIED_mg((SV*)hv, PERL_MAGIC_tied)) 4 0 4
if (SvTIED_mg((SV*)hv, PERL_MAGIC_tied)) 4 0 4
1658 4 is_tied = 1;
1659 /* really should be calling magic_scalarpack(hv, mg) here, but I doubt it will be correct */
1660 /* TODO For tied hashes we should check if the iterator is already canonical (same sort order)
1661 as it would be with a DB tree e.g. and skip our slow sorting. */
1662  
1663 /* need to count by iterating. could improve by dynamically building the vector below */
1664 /* but I don't care for the speed of this special case. */
1665 4 count = 0;
1666 16 ▶ while (hv_iternext (hv))
Branch: 1 branch
BranchTrueFalseTotal
while (hv_iternext (hv)) 12 4 16
1667 12 ++count;
1668  
1669 4 (void)hv_iterinit (hv);
1670 }
1671  
1672 /* one key does not need to be sorted */
1673 10275 ▶ if (count > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) 10269 6 10275
1674 {
1675 10269 int has_utf8 = 0;
1676 10269 struct tied_entry_copy *tied_entries = NULL;
1677  
1678 /* allocate larger arrays on the heap */
1679 10269 ▶ if (count > STACK_HES)
Branch: 1 branch
BranchTrueFalseTotal
if (count > STACK_HES) 1 10268 10269
1680 {
1681 1 SV *sv = sv_2mortal (NEWSV (0, count * sizeof (*hes)));
1682 1 hes = (HE **)SvPVX (sv);
1683 }
1684  
1685 /* one zeroed mortal buffer for the copies of all tied entries.
1686 i < count is enforced below, so it cannot overflow. */
1687 10269 ▶ if (UNLIKELY (is_tied))
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY (is_tied)) 4 10265 10269
1688 {
1689 4 SV *copies_sv = sv_2mortal (NEWSV (0, count * sizeof (struct tied_entry_copy)));
1690 4 tied_entries = (struct tied_entry_copy *)SvPVX (copies_sv);
1691 4 Zero (tied_entries, count, struct tied_entry_copy);
1692 }
1693  
1694 10269 i = 0;
1695 /* fill the HE vector and check if SVKEY or UTF8 */
1696 41028 ▶ while ((he = hv_iternext (hv)))
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext (hv))) 30760 10268 41028
1697 {
1698 30760 ▶ if (UNLIKELY(i >= count))
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY(i >= count)) 1 30759 30760
1699 1 ▶ croak ("Unstable %shash key counts %d vs %d in subsequent runs",
Branch: 1 branch
BranchTrueFalseTotal
croak ("Unstable %shash key counts %d vs %d in subsequent runs", 1 0 1
1700 is_tied ? "tied " : "", (int)count, (int)i + 1);
1701 30759 ▶ if (UNLIKELY(is_tied))
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY(is_tied)) 12 30747 30759
1702 { /* tied entries are completely freed by perl on each scan iteration,
1703 so we will make a private copy here. It lives in the mortal buffer
1704 allocated above, so perl frees it later (at the next FREETMPS),
1705 no matter how this function is left, by a normal return()
1706 or by croak() */
1707 12 HE *he1 = &tied_entries[i].he;
1708 12 he1->hent_hek = (HEK *)tied_entries[i].hek;
1709 12 HeVAL(he1) = hv_iterval(hv, he);
1710 12 HeSVKEY_set (he1, hv_iterkeysv(he));
1711 12 hes[i++] = he1;
1712 }
1713 else
1714 30747 hes[i++] = he;
1715 /* check the SV for UTF8 and seperate use bytes handling */
1716 30759 ▶ if (!has_utf8)
Branch: 1 branch
BranchTrueFalseTotal
if (!has_utf8) 30701 58 30759
1717 {
1718 30701 ▶ if (He_IS_SVKEY(he))
Branch: 1 branch
BranchTrueFalseTotal
if (He_IS_SVKEY(he)) 12 30689 30701
1719 12 ▶ has_utf8 = SvUTF8(HeSVKEY(he));
Branch: 2 branches
BranchTrueFalseTotal
has_utf8 = SvUTF8(HeSVKEY(he)); 12 0 12
has_utf8 = SvUTF8(HeSVKEY(he)); 12 0 12
1720 else
1721 30689 has_utf8 = HeKUTF8(he);
1722 }
1723 }
1724  
1725 /* Undefined behaviour when the two iterations do not result in the same count.
1726 With threads::shared or broken tie. The last HEs might be NULL then or we'll
1727 miss some. */
1728 10268 ▶ if (i != count)
Branch: 1 branch
BranchTrueFalseTotal
if (i != count) 0 10268 10268
1729 0 ▶ croak ("Unstable %shash key counts %d vs %d in subsequent runs",
Branch: 1 branch
BranchTrueFalseTotal
croak ("Unstable %shash key counts %d vs %d in subsequent runs", 0 0 0
1730 is_tied ? "tied " : "", (int)count, (int)i);
1731 assert (i == count);
1732  
1733 /* one key does not need to be sorted */
1734 10268 ▶ if (count > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (count > 1) 10250 18 10268
1735 {
1736  
1737 10250 ▶ if (!has_utf8)
Branch: 1 branch
BranchTrueFalseTotal
if (!has_utf8) 10239 11 10250
1738 {
1739 /* TODO With threads::shared check for qsort_r */
1740 10239 ▶ qsort (hes, count, sizeof (HE *), is_tied ? he_cmp_tied : he_cmp_fast);
Branch: 1 branch
BranchTrueFalseTotal
qsort (hes, count, sizeof (HE *), is_tied ? he_cmp_tied : he_cmp_fast); 3 10236 10239
1741 }
1742 else
1743 {
1744 /* hack to forcefully disable "use bytes".
1745 Changed in 5.9.4 a98fe34d09e2476f1a21bfb9dc730dc9ab02b0b4 */
1746 11 COP cop = *PL_curcop;
1747 #if PERL_VERSION < 10
1748 cop.op_private &= ~HINT_BYTES;
1749 #else
1750 11 cop.cop_hints &= ~HINT_BYTES;
1751 #endif
1752  
1753 11 ENTER; SAVETMPS;
1754  
1755 11 SAVEVPTR (PL_curcop);
1756 11 PL_curcop = &cop;
1757  
1758 /* TODO With threads::shared check for qsort_r */
1759 11 ▶ qsort (hes, count, sizeof (HE *), is_tied ? he_cmp_tied : he_cmp_slow);
Branch: 1 branch
BranchTrueFalseTotal
qsort (hes, count, sizeof (HE *), is_tied ? he_cmp_tied : he_cmp_slow); 0 11 11
1760  
1761 11 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 11 0 11
1762 11 LEAVE;
1763 }
1764 }
1765  
1766 10268 encode_nl (aTHX_ enc); ++enc->indent;
1767  
1768 41021 ▶ while (count--)
Branch: 1 branch
BranchTrueFalseTotal
while (count--) 30756 10265 41021
1769 {
1770 char *key;
1771 I32 klen;
1772  
1773 30756 encode_indent (aTHX_ enc);
1774 30756 he = hes[count];
1775 30756 retrieve_hk (aTHX_ he, &key, &klen);
1776 30756 encode_hk (aTHX_ enc, key, klen);
1777  
1778 30756 ▶ if (UNLIKELY (PTR2UV (typehv)))
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY (PTR2UV (typehv))) 51 30705 30756
1779 {
1780 51 SV **typesv_ref = hv_fetch (typehv, key, klen, 0);
1781 51 ▶ if (UNLIKELY (!typesv_ref))
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY (!typesv_ref)) 1 50 51
1782 1 croak ("no type was specified for hash key '%s'", key);
1783  
1784 50 typesv = *typesv_ref;
1785 }
1786  
1787 61500 ▶ encode_sv(aTHX_ enc,
Branch: 1 branch
BranchTrueFalseTotal
encode_sv(aTHX_ enc, 30745 10 30755
1788 30745 ▶ (is_tied || !SvMAGICAL(hv)) ? HeVAL(he)
Branch: 1 branch
BranchTrueFalseTotal
(is_tied || !SvMAGICAL(hv)) ? HeVAL(he) 30745 0 30745
1789 0 : hv_iterval(hv, he),
1790 typesv);
1791  
1792 30753 ▶ if (count)
Branch: 1 branch
BranchTrueFalseTotal
if (count) 20488 10265 30753
1793 20488 encode_comma(aTHX_ enc);
1794 }
1795  
1796 10265 encode_nl (aTHX_ enc); --enc->indent; encode_indent (aTHX_ enc);
1797 }
1798 }
1799 else
1800 {
1801 15949 ▶ if (hv_iterinit (hv) || SvMAGICAL (hv))
Branch: 2 branches
BranchTrueFalseTotal
if (hv_iterinit (hv) || SvMAGICAL (hv)) 5 15944 15949
if (hv_iterinit (hv) || SvMAGICAL (hv)) 1 4 5
1802 15945 ▶ if ((he = hv_iternext (hv)))
Branch: 1 branch
BranchTrueFalseTotal
if ((he = hv_iternext (hv))) 15945 0 15945
1803 {
1804 15945 encode_nl (aTHX_ enc); ++enc->indent;
1805  
1806 for (;;)
1807 41666 {
1808 char *key;
1809 I32 klen;
1810  
1811 57611 encode_indent (aTHX_ enc);
1812 57611 retrieve_hk (aTHX_ he, &key, &klen);
1813 57611 encode_hk (aTHX_ enc, key, klen);
1814  
1815 57611 ▶ if (UNLIKELY (PTR2UV (typehv)))
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY (PTR2UV (typehv))) 0 57611 57611
1816 {
1817 0 SV **typesv_ref = hv_fetch (typehv, key, klen, 0);
1818 0 ▶ if (UNLIKELY (!typesv_ref))
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY (!typesv_ref)) 0 0 0
1819 0 croak ("no type was specified for hash key '%s'", key);
1820  
1821 0 typesv = *typesv_ref;
1822 }
1823  
1824 57611 encode_sv(aTHX_ enc,
1825 57611 ▶ UNLIKELY(SvMAGICAL(hv)) ? hv_iterval(hv, he)
Branch: 1 branch
BranchTrueFalseTotal
UNLIKELY(SvMAGICAL(hv)) ? hv_iterval(hv, he) 1 57610 57611
1826 : HeVAL(he),
1827 typesv);
1828  
1829 57607 ▶ if (!(he = hv_iternext(hv)))
Branch: 1 branch
BranchTrueFalseTotal
if (!(he = hv_iternext(hv))) 15941 41666 57607
1830 15941 break;
1831  
1832 41666 encode_comma(aTHX_ enc);
1833 }
1834  
1835 15941 encode_nl (aTHX_ enc); --enc->indent; encode_indent (aTHX_ enc);
1836 }
1837 }
1838  
1839 26216 encode_ch (aTHX_ enc, '}');
1840 26216 }
1841  
1842 /* implement convert_blessed, sv is already unref'ed here */
1843 static void
1844 19 encode_stringify(pTHX_ enc_t *enc, SV *sv, int isref)
1845 {
1846 19 char *str = NULL;
1847 STRLEN len;
1848 19 SV *pv = NULL;
1849 19 svtype type = SvTYPE(sv);
1850 #if PERL_VERSION <= 8
1851 MAGIC *mg;
1852 #endif
1853  
1854 /* SvAMAGIC without the ref */
1855 #if PERL_VERSION > 17
1856 #define MyAMG(sv) (SvOBJECT(sv) && HvAMAGIC(SvSTASH(sv)))
1857 #else
1858 #if PERL_VERSION > 8
1859 #define MyAMG(sv) (SvOBJECT(sv) && (SvFLAGS(sv) & SVf_AMAGIC))
1860 #else
1861 #define MyAMG(sv) (SvOBJECT(sv) && ((SvFLAGS(sv) & SVf_AMAGIC) \
1862 || ((mg = mg_find((SV*)SvSTASH(sv), PERL_MAGIC_overload_table)) \
1863 && mg->mg_ptr && AMT_AMAGIC((AMT*)mg->mg_ptr))))
1864 #endif
1865 #endif
1866  
1867 19 ▶ if (isref && SvAMAGIC(sv))
Branch: 4 branches
BranchTrueFalseTotal
if (isref && SvAMAGIC(sv)) 10 9 19
if (isref && SvAMAGIC(sv)) 10 0 10
if (isref && SvAMAGIC(sv)) 10 0 10
if (isref && SvAMAGIC(sv)) 10 0 10
1868 ;
1869 /* if no string overload found, check allow_stringify, allow_unknown
1870 and allow_blessed. */
1871 9 ▶ else if (!MyAMG(sv) && !(enc->json.flags & F_ALLOW_STRINGIFY)) {
Branch: 3 branches
BranchTrueFalseTotal
else if (!MyAMG(sv) && !(enc->json.flags & F_ALLOW_STRINGIFY)) { 9 0 9
else if (!MyAMG(sv) && !(enc->json.flags & F_ALLOW_STRINGIFY)) { 3 6 9
else if (!MyAMG(sv) && !(enc->json.flags & F_ALLOW_STRINGIFY)) { 3 0 3
1872 3 ▶ if ((isref != 0) && ((enc->json.flags & F_ALLOW_UNKNOWN) == 0)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((isref != 0) && ((enc->json.flags & F_ALLOW_UNKNOWN) == 0)) { 0 3 3
if ((isref != 0) && ((enc->json.flags & F_ALLOW_UNKNOWN) == 0)) { 0 0 0
1873 0 croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1 "
1874 "without allow_unknown",
1875 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
1876 }
1877 3 ▶ else if ((isref == 0) && ((enc->json.flags & F_ALLOW_BLESSED) == 0)) {
Branch: 2 branches
BranchTrueFalseTotal
else if ((isref == 0) && ((enc->json.flags & F_ALLOW_BLESSED) == 0)) { 3 0 3
else if ((isref == 0) && ((enc->json.flags & F_ALLOW_BLESSED) == 0)) { 1 2 3
1878 2 ▶ croak ("encountered %s '%s', but allow_blessed, allow_stringify or "
Branch: 1 branch
BranchTrueFalseTotal
croak ("encountered %s '%s', but allow_blessed, allow_stringify or " 1 0 1
1879 "TO_JSON/FREEZE method missing",
1880 SvOBJECT(sv) ? "object" : "reference",
1881 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
1882 }
1883 2 encode_const_str (aTHX_ enc, "null", 4, 0);
1884 10 return;
1885 }
1886 /* sv_2pv_flags does not accept those types: */
1887 16 ▶ if (type != SVt_PVAV && type != SVt_PVHV && type != SVt_PVFM) {
Branch: 3 branches
BranchTrueFalseTotal
if (type != SVt_PVAV && type != SVt_PVHV && type != SVt_PVFM) { 16 0 16
if (type != SVt_PVAV && type != SVt_PVHV && type != SVt_PVFM) { 15 1 16
if (type != SVt_PVAV && type != SVt_PVHV && type != SVt_PVFM) { 15 0 15
1888 /* the essential of pp_stringify */
1889 #if PERL_VERSION > 7
1890 15 pv = newSVpvs("");
1891 15 ▶ if (!isref && !(enc->json.flags & F_ALLOW_STRINGIFY)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!isref && !(enc->json.flags & F_ALLOW_STRINGIFY)) { 5 10 15
if (!isref && !(enc->json.flags & F_ALLOW_STRINGIFY)) { 5 0 5
1892 5 sv_copypv(pv, sv_2mortal (newRV_inc (sv)));
1893 } else {
1894 10 sv_copypv(pv, sv);
1895 }
1896 15 ▶ SvSETMAGIC(pv);
Branch: 1 branch
BranchTrueFalseTotal
SvSETMAGIC(pv); 0 15 15
1897 15 str = SvPVutf8_force(pv, len);
1898 #else
1899 char *s;
1900 if (isref) {
1901 pv = AMG_CALLun(sv,string);
1902 len = SvCUR(pv);
1903 str = SvPVX(pv);
1904 SvREFCNT_inc(pv);
1905 }
1906 else {
1907 pv = newSVpvs("");
1908 s = SvPV(sv,len);
1909 sv_setpvn(pv,s,len);
1910 if (SvUTF8(sv))
1911 SvUTF8_on(pv);
1912 else
1913 SvUTF8_off(pv);
1914 SvSETMAGIC(pv);
1915 str = SvPVutf8_force(pv, len);
1916 }
1917 #endif
1918 15 ▶ if (!len && !SvOBJECT (sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!len && !SvOBJECT (sv)) { 1 14 15
if (!len && !SvOBJECT (sv)) { 0 1 1
1919 0 encode_const_str (aTHX_ enc, "null", 4, 0);
1920 0 SvREFCNT_dec(pv);
1921 0 return;
1922 }
1923 } else {
1924 /* manually call all possible magic on AV, HV, FM */
1925 1 ▶ if (SvGMAGICAL(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (SvGMAGICAL(sv)) 0 1 1
1926 0 mg_get(sv);
1927 1 ▶ if (MyAMG(sv)) { /* force a RV here */
Branch: 2 branches
BranchTrueFalseTotal
if (MyAMG(sv)) { /* force a RV here */ 1 0 1
if (MyAMG(sv)) { /* force a RV here */ 1 0 1
1928 1 SV* rv = newRV_inc(sv);
1929 #if PERL_VERSION <= 8
1930 HV *stash = SvSTASH(sv);
1931 if (!SvSTASH(rv) || !(SvFLAGS(sv) & SVf_AMAGIC)) {
1932 sv_bless(rv, stash);
1933 Gv_AMupdate(stash);
1934 SvFLAGS(sv) |= SVf_AMAGIC;
1935 }
1936 #endif
1937 #if PERL_VERSION > 13
1938 {
1939 /* GH #128: protect from endless recursion via "" overload.
1940 Temporarily clear convert_blessed and allow_stringify on the
1941 original JSON object so re-entrant encode calls won't loop. */
1942 1 U32 flags = enc->orig_json->flags;
1943 1 enc->orig_json->flags &= ~(F_ALLOW_STRINGIFY|F_CONV_BLESSED);
1944 1 pv = AMG_CALLunary(rv, string_amg);
1945 1 enc->orig_json->flags = flags;
1946 }
1947 #else
1948 pv = AMG_CALLun(rv, string);
1949 #endif
1950 1 ▶ TAINT_IF(pv && SvTAINTED(pv));
Branch: 4 branches
BranchTrueFalseTotal
TAINT_IF(pv && SvTAINTED(pv)); 1 0 1
TAINT_IF(pv && SvTAINTED(pv)); 0 1 1
TAINT_IF(pv && SvTAINTED(pv)); 0 0 0
TAINT_IF(pv && SvTAINTED(pv)); 0 1 1
1951 1 ▶ if (pv && SvPOK(pv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (pv && SvPOK(pv)) { 1 0 1
if (pv && SvPOK(pv)) { 1 0 1
1952 1 str = SvPVutf8_force(pv, len);
1953 1 encode_ch (aTHX_ enc, '"');
1954 1 encode_str (aTHX_ enc, str, len, 1);
1955 1 encode_ch (aTHX_ enc, '"');
1956 1 SvREFCNT_dec(rv);
1957 1 return;
1958 }
1959 0 SvREFCNT_dec(rv);
1960 0 pv = NULL; /* owned by the overload call */
1961 }
1962 }
1963 15 ▶ if (UNLIKELY(isref == 1
Branch: 6 branches
BranchTrueFalseTotal
if (UNLIKELY(isref == 1 10 5 15
if (UNLIKELY(isref == 1 10 0 10
if (UNLIKELY(isref == 1 10 5 15
if (UNLIKELY(isref == 1 10 0 10
if (UNLIKELY(isref == 1 10 5 15
if (UNLIKELY(isref == 1 0 10 10
1964 && (enc->json.flags & F_ALLOW_BIGNUM) && str && str[0] == '+')) {
1965 0 str++;
1966 0 len--;
1967 }
1968 /* if ALLOW_BIGNUM and Math::Big* and NaN => according to stringify_infnan */
1969 15 ▶ if (UNLIKELY(
Branch: 12 branches
BranchTrueFalseTotal
if (UNLIKELY( 10 5 15
if (UNLIKELY( 10 0 10
if (UNLIKELY( 10 5 15
if (UNLIKELY( 10 0 10
if (UNLIKELY( 10 5 15
if (UNLIKELY( 7 3 10
if (UNLIKELY( 0 7 7
if (UNLIKELY( 7 3 10
if (UNLIKELY( 2 5 7
if (UNLIKELY( 5 5 10
if (UNLIKELY( 2 3 5
if (UNLIKELY( 7 3 10
1970 (enc->json.flags & F_ALLOW_BIGNUM)
1971 && str
1972 && SvROK(sv)
1973 && (memEQc(str, "NaN") || memEQc(str, "nan") ||
1974 memEQc(str, "inf") || memEQc(str, "-inf"))))
1975 {
1976 7 ▶ if (is_bignum_obj (aTHX_ SvRV (sv)))
Branch: 1 branch
BranchTrueFalseTotal
if (is_bignum_obj (aTHX_ SvRV (sv))) 7 0 7
1977 {
1978 7 ▶ if (enc->json.infnan_mode == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (enc->json.infnan_mode == 0) { 4 3 7
1979 4 encode_const_str (aTHX_ enc, "null", 4, 0);
1980 4 ▶ if (pv) SvREFCNT_dec(pv);
Branch: 1 branch
BranchTrueFalseTotal
if (pv) SvREFCNT_dec(pv); 4 0 4
1981 4 return;
1982 3 ▶ } else if (enc->json.infnan_mode == 3) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (enc->json.infnan_mode == 3) { 3 0 3
1983 3 ▶ if (memEQc(str, "NaN") || memEQc(str, "nan"))
Branch: 2 branches
BranchTrueFalseTotal
if (memEQc(str, "NaN") || memEQc(str, "nan")) 2 1 3
if (memEQc(str, "NaN") || memEQc(str, "nan")) 0 2 2
1984 1 encode_const_str (aTHX_ enc, "nan", 3, 0);
1985 2 ▶ else if (memEQc(str, "inf"))
Branch: 1 branch
BranchTrueFalseTotal
else if (memEQc(str, "inf")) 1 1 2
1986 1 encode_const_str (aTHX_ enc, "inf", 3, 0);
1987 else
1988 1 encode_const_str (aTHX_ enc, "-inf", 4, 0);
1989 3 ▶ if (pv) SvREFCNT_dec(pv);
Branch: 1 branch
BranchTrueFalseTotal
if (pv) SvREFCNT_dec(pv); 3 0 3
1990 3 return;
1991 }
1992 }
1993 }
1994 8 ▶ if (!str)
Branch: 1 branch
BranchTrueFalseTotal
if (!str) 0 8 8
1995 0 encode_const_str (aTHX_ enc, "null", 4, 0);
1996 else {
1997 8 ▶ if (isref != 1)
Branch: 1 branch
BranchTrueFalseTotal
if (isref != 1) 5 3 8
1998 5 encode_ch (aTHX_ enc, '"');
1999 8 encode_str (aTHX_ enc, str, len, 1);
2000 8 ▶ if (isref != 1)
Branch: 1 branch
BranchTrueFalseTotal
if (isref != 1) 5 3 8
2001 5 encode_ch (aTHX_ enc, '"');
2002 }
2003 8 ▶ if (pv) SvREFCNT_dec(pv);
Branch: 1 branch
BranchTrueFalseTotal
if (pv) SvREFCNT_dec(pv); 8 0 8
2004 #undef MyAMG
2005 }
2006  
2007 INLINE int
2008 84 encode_bool_obj (pTHX_ enc_t *enc, SV *sv, int force_conversion, int as_string)
2009 {
2010 84 ▶ if (is_bool_obj (aTHX_ sv))
Branch: 1 branch
BranchTrueFalseTotal
if (is_bool_obj (aTHX_ sv)) 39 45 84
2011 {
2012 39 ▶ if (as_string)
Branch: 1 branch
BranchTrueFalseTotal
if (as_string) 7 32 39
2013 7 encode_ch (aTHX_ enc, '"');
2014 /* we need to apply threads_shared magic */
2015 if
2016 #ifdef USE_ITHREADS
2017 (SvIV (sv))
2018 #else
2019 39 ▶ (SvIV_nomg (sv))
Branch: 1 branch
BranchTrueFalseTotal
(SvIV_nomg (sv)) 22 17 39
2020 #endif
2021 22 encode_const_str (aTHX_ enc, "true", 4, 0);
2022 else
2023 17 encode_const_str (aTHX_ enc, "false", 5, 0);
2024 39 ▶ if (as_string)
Branch: 1 branch
BranchTrueFalseTotal
if (as_string) 7 32 39
2025 7 encode_ch (aTHX_ enc, '"');
2026 }
2027 45 ▶ else if (force_conversion && enc->json.flags & (F_ALLOW_BLESSED|F_CONV_BLESSED))
Branch: 2 branches
BranchTrueFalseTotal
else if (force_conversion && enc->json.flags & (F_ALLOW_BLESSED|F_CONV_BLESSED)) 0 45 45
else if (force_conversion && enc->json.flags & (F_ALLOW_BLESSED|F_CONV_BLESSED)) 0 0 0
2028 {
2029 0 ▶ if (as_string)
Branch: 1 branch
BranchTrueFalseTotal
if (as_string) 0 0 0
2030 0 encode_ch (aTHX_ enc, '"');
2031 0 ▶ if (SvTRUE_nomg (sv))
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE_nomg (sv)) 0 0 0
2032 0 encode_const_str (aTHX_ enc, "true", 4, 0);
2033 else
2034 0 encode_const_str (aTHX_ enc, "false", 5, 0);
2035 0 ▶ if (as_string)
Branch: 1 branch
BranchTrueFalseTotal
if (as_string) 0 0 0
2036 0 encode_ch (aTHX_ enc, '"');
2037 }
2038 else
2039 45 return 0;
2040  
2041 39 return 1;
2042 }
2043  
2044 INLINE int
2045 27 encode_bool_ref (pTHX_ enc_t *enc, SV *sv)
2046 {
2047 27 int bool_type = ref_bool_type (aTHX_ sv);
2048  
2049 27 ▶ if (bool_type == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (bool_type == 1) 5 22 27
2050 5 encode_const_str (aTHX_ enc, "true", 4, 0);
2051 22 ▶ else if (bool_type == 0)
Branch: 1 branch
BranchTrueFalseTotal
else if (bool_type == 0) 3 19 22
2052 3 encode_const_str (aTHX_ enc, "false", 5, 0);
2053 else
2054 19 return 0;
2055  
2056 8 return 1;
2057 }
2058  
2059 /* encode objects, arrays and special \0=false and \1=true values
2060 and other representations of booleans: JSON::PP::Boolean, Mojo::JSON::_Bool
2061 */
2062 static void
2063 98 encode_rv (pTHX_ enc_t *enc, SV *rv)
2064 {
2065 svtype svt;
2066 GV *method;
2067 98 SV *sv = SvRV(rv);
2068  
2069 98 svt = SvTYPE (sv);
2070  
2071 98 ▶ if (UNLIKELY (SvOBJECT (sv)))
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY (SvOBJECT (sv))) 68 30 98
2072 {
2073 68 ▶ if (!encode_bool_obj (aTHX_ enc, sv, 0, 0))
Branch: 1 branch
BranchTrueFalseTotal
if (!encode_bool_obj (aTHX_ enc, sv, 0, 0)) 45 23 68
2074 {
2075 45 HV *stash = SvSTASH (sv);
2076 45 ▶ if ((enc->json.flags & F_ALLOW_TAGS)
Branch: 1 branch
BranchTrueFalseTotal
if ((enc->json.flags & F_ALLOW_TAGS) 8 37 45
2077 8 ▶ && (method = gv_fetchmethod_autoload (stash, "FREEZE", 0)))
Branch: 1 branch
BranchTrueFalseTotal
&& (method = gv_fetchmethod_autoload (stash, "FREEZE", 0))) 7 1 8
2078 6 {
2079 dMY_CXT;
2080 7 dSP;
2081 int count, items;
2082  
2083 7 ENTER; SAVETMPS;
2084 7 ▶ PUSHMARK (SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK (SP); 0 7 7
2085 7 ▶ EXTEND (SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND (SP, 2); 0 7 7
2086 7 PUSHs (rv);
2087 7 PUSHs (MY_CXT.sv_json);
2088  
2089 7 PUTBACK;
2090 7 count = call_sv ((SV *)GvCV (method), G_ARRAY);
2091 7 items = count;
2092 7 SPAGAIN;
2093  
2094 7 ▶ if (!json_validate (&enc->json))
Branch: 1 branch
BranchTrueFalseTotal
if (!json_validate (&enc->json)) 0 7 7
2095 0 croak (NULL);
2096  
2097 /* catch this surprisingly common error */
2098 7 ▶ if (SvROK (TOPs) && SvRV (TOPs) == sv)
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK (TOPs) && SvRV (TOPs) == sv) 3 4 7
if (SvROK (TOPs) && SvRV (TOPs) == sv) 1 2 3
2099 1 ▶ croak ("%s::FREEZE method returned same object as was passed instead of a new one",
Branch: 6 branches
BranchTrueFalseTotal
croak ("%s::FREEZE method returned same object as was passed instead of a new one", 1 0 1
croak ("%s::FREEZE method returned same object as was passed instead of a new one", 1 0 1
croak ("%s::FREEZE method returned same object as was passed instead of a new one", 0 1 1
croak ("%s::FREEZE method returned same object as was passed instead of a new one", 0 0 0
croak ("%s::FREEZE method returned same object as was passed instead of a new one", 1 0 1
croak ("%s::FREEZE method returned same object as was passed instead of a new one", 0 1 1
2100 HvNAME (SvSTASH (sv)));
2101  
2102 6 encode_ch (aTHX_ enc, '(');
2103 6 encode_ch (aTHX_ enc, '"');
2104 6 ▶ encode_str (aTHX_ enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash));
Branch: 19 branches
BranchTrueFalseTotal
encode_str (aTHX_ enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); 6 0 6
encode_str (aTHX_ enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); 6 0 6
encode_str (aTHX_ enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); 0 6 6
encode_str (aTHX_ enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); 0 0 0
encode_str (aTHX_ enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); 6 0 6
encode_str (aTHX_ enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); 0 6 6
encode_str (aTHX_ enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); 0 6 6
encode_str (aTHX_ enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); 6 0 6
encode_str (aTHX_ enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); 6 0 6
encode_str (aTHX_ enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); 0 6 6
encode_str (aTHX_ enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); 0 0 0
encode_str (aTHX_ enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); 6 0 6
encode_str (aTHX_ enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); 0 6 6
encode_str (aTHX_ enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); 6 0 6
encode_str (aTHX_ enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); 6 0 6
encode_str (aTHX_ enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); 0 6 6
encode_str (aTHX_ enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); 0 0 0
encode_str (aTHX_ enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); 6 0 6
encode_str (aTHX_ enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); 0 6 6
2105 6 encode_ch (aTHX_ enc, '"');
2106 6 encode_ch (aTHX_ enc, ')');
2107 6 encode_ch (aTHX_ enc, '[');
2108  
2109 261 ▶ while (count)
Branch: 1 branch
BranchTrueFalseTotal
while (count) 255 6 261
2110 {
2111 255 encode_sv (aTHX_ enc, SP[1 - count--], &PL_sv_undef);
2112 255 SPAGAIN;
2113  
2114 255 ▶ if (count)
Branch: 1 branch
BranchTrueFalseTotal
if (count) 249 6 255
2115 249 encode_ch (aTHX_ enc, ',');
2116 }
2117  
2118 6 encode_ch (aTHX_ enc, ']');
2119  
2120 6 SP -= items;
2121 6 PUTBACK;
2122  
2123 6 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 6 0 6
2124 }
2125 38 ▶ else if ((enc->json.flags & F_CONV_BLESSED)
Branch: 1 branch
BranchTrueFalseTotal
else if ((enc->json.flags & F_CONV_BLESSED) 19 19 38
2126 19 ▶ && (method = gv_fetchmethod_autoload (stash, "TO_JSON", 0)))
Branch: 1 branch
BranchTrueFalseTotal
&& (method = gv_fetchmethod_autoload (stash, "TO_JSON", 0))) 10 9 19
2127 8 {
2128 10 dSP;
2129  
2130 10 ENTER; SAVETMPS;
2131 10 ▶ PUSHMARK (SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK (SP); 0 10 10
2132 10 ▶ XPUSHs (rv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs (rv); 0 10 10
2133  
2134 /* calling with G_SCALAR ensures that we always get a 1 return value */
2135 10 PUTBACK;
2136 10 call_sv ((SV *)GvCV (method), G_SCALAR);
2137 10 SPAGAIN;
2138
2139 10 ▶ if (!json_validate (&enc->json))
Branch: 1 branch
BranchTrueFalseTotal
if (!json_validate (&enc->json)) 0 10 10
2140 0 croak (NULL);
2141  
2142 /* catch this surprisingly common error */
2143 10 ▶ if (SvROK (TOPs) && SvRV (TOPs) == sv)
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK (TOPs) && SvRV (TOPs) == sv) 6 4 10
if (SvROK (TOPs) && SvRV (TOPs) == sv) 2 4 6
2144 2 ▶ croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv)));
Branch: 6 branches
BranchTrueFalseTotal
croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); 2 0 2
croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); 2 0 2
croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); 0 2 2
croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); 0 0 0
croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); 2 0 2
croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); 0 2 2
2145  
2146 8 sv = POPs;
2147 8 PUTBACK;
2148  
2149 8 encode_sv (aTHX_ enc, sv, &PL_sv_undef);
2150  
2151 8 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 8 0 8
2152 }
2153 28 ▶ else if ((enc->json.flags & F_ALLOW_BIGNUM) && is_bignum_obj (aTHX_ sv))
Branch: 2 branches
BranchTrueFalseTotal
else if ((enc->json.flags & F_ALLOW_BIGNUM) && is_bignum_obj (aTHX_ sv)) 11 17 28
else if ((enc->json.flags & F_ALLOW_BIGNUM) && is_bignum_obj (aTHX_ sv)) 11 0 11
2154 11 {
2155 11 SV *compact = bignum_compact (aTHX_ rv);
2156 11 ▶ if (compact)
Branch: 1 branch
BranchTrueFalseTotal
if (compact) 1 10 11
2157 {
2158 STRLEN clen;
2159 1 char *cstr = SvPV (compact, clen);
2160 1 encode_str (aTHX_ enc, cstr, clen, 0);
2161 }
2162 else
2163 10 encode_stringify(aTHX_ enc, rv, 1);
2164 }
2165 17 ▶ else if (enc->json.flags & F_CONV_BLESSED)
Branch: 1 branch
BranchTrueFalseTotal
else if (enc->json.flags & F_CONV_BLESSED) 9 8 17
2166 9 encode_stringify(aTHX_ enc, sv, 0);
2167 8 ▶ else if (enc->json.flags & F_ALLOW_BLESSED)
Branch: 1 branch
BranchTrueFalseTotal
else if (enc->json.flags & F_ALLOW_BLESSED) 4 4 8
2168 4 encode_const_str (aTHX_ enc, "null", 4, 0);
2169 else
2170 {
2171 /* Use default stringification, bypassing any "" overload (GH #191). */
2172 4 ▶ const char *name = HvNAME(SvSTASH(sv));
Branch: 6 branches
BranchTrueFalseTotal
const char *name = HvNAME(SvSTASH(sv)); 4 0 4
const char *name = HvNAME(SvSTASH(sv)); 4 0 4
const char *name = HvNAME(SvSTASH(sv)); 0 4 4
const char *name = HvNAME(SvSTASH(sv)); 0 0 0
const char *name = HvNAME(SvSTASH(sv)); 4 0 4
const char *name = HvNAME(SvSTASH(sv)); 0 4 4
2173 4 ▶ SV *desc = sv_2mortal(newSVpvf("%s=%s(0x%p)",
Branch: 1 branch
BranchTrueFalseTotal
SV *desc = sv_2mortal(newSVpvf("%s=%s(0x%p)", 4 0 4
2174 name ? name : "(unknown)", sv_reftype(sv, 0), sv));
2175 4 croak ("encountered object '%s', but neither allow_blessed, convert_blessed nor allow_tags settings are enabled (or TO_JSON/FREEZE method missing)",
2176 SvPV_nolen (desc));
2177 }
2178 }
2179 }
2180 30 ▶ else if (svt < SVt_PVAV && svt != SVt_PVGV && svt != SVt_PVHV && svt != SVt_PVAV)
Branch: 4 branches
BranchTrueFalseTotal
else if (svt < SVt_PVAV && svt != SVt_PVGV && svt != SVt_PVHV && svt != SVt_PVAV) 26 4 30
else if (svt < SVt_PVAV && svt != SVt_PVGV && svt != SVt_PVHV && svt != SVt_PVAV) 24 2 26
else if (svt < SVt_PVAV && svt != SVt_PVGV && svt != SVt_PVHV && svt != SVt_PVAV) 24 0 24
else if (svt < SVt_PVAV && svt != SVt_PVGV && svt != SVt_PVHV && svt != SVt_PVAV) 24 0 24
2181 {
2182 31 ▶ if (!encode_bool_ref (aTHX_ enc, sv))
Branch: 1 branch
BranchTrueFalseTotal
if (!encode_bool_ref (aTHX_ enc, sv)) 19 5 24
2183 {
2184 19 ▶ if (enc->json.flags & F_ALLOW_STRINGIFY)
Branch: 1 branch
BranchTrueFalseTotal
if (enc->json.flags & F_ALLOW_STRINGIFY) 0 19 19
2185 0 encode_stringify(aTHX_ enc, sv, SvROK(sv));
2186 19 ▶ else if (enc->json.flags & F_ALLOW_UNKNOWN)
Branch: 1 branch
BranchTrueFalseTotal
else if (enc->json.flags & F_ALLOW_UNKNOWN) 7 12 19
2187 7 encode_const_str (aTHX_ enc, "null", 4, 0);
2188 else
2189 {
2190 /* Use default stringification, bypassing any "" overload (GH #191). */
2191 12 ▶ const char *name = SvOBJECT(sv) ? HvNAME(SvSTASH(sv)) : NULL;
Branch: 7 branches
BranchTrueFalseTotal
const char *name = SvOBJECT(sv) ? HvNAME(SvSTASH(sv)) : NULL; 0 12 12
const char *name = SvOBJECT(sv) ? HvNAME(SvSTASH(sv)) : NULL; 0 0 0
const char *name = SvOBJECT(sv) ? HvNAME(SvSTASH(sv)) : NULL; 0 0 0
const char *name = SvOBJECT(sv) ? HvNAME(SvSTASH(sv)) : NULL; 0 0 0
const char *name = SvOBJECT(sv) ? HvNAME(SvSTASH(sv)) : NULL; 0 0 0
const char *name = SvOBJECT(sv) ? HvNAME(SvSTASH(sv)) : NULL; 0 0 0
const char *name = SvOBJECT(sv) ? HvNAME(SvSTASH(sv)) : NULL; 0 0 0
2192 12 ▶ SV *desc = sv_2mortal(newSVpvf("%s=%s(0x%p)",
Branch: 1 branch
BranchTrueFalseTotal
SV *desc = sv_2mortal(newSVpvf("%s=%s(0x%p)", 0 12 12
2193 name ? name : "(unknown)", sv_reftype(sv, 0), sv));
2194 12 croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1",
2195 SvPV_nolen (desc));
2196 }
2197 }
2198 }
2199 6 ▶ else if (enc->json.flags & F_ALLOW_UNKNOWN)
Branch: 1 branch
BranchTrueFalseTotal
else if (enc->json.flags & F_ALLOW_UNKNOWN) 2 4 6
2200 2 encode_const_str (aTHX_ enc, "null", 4, 0);
2201 else
2202 {
2203 /* Use default stringification, bypassing any "" overload (GH #191). */
2204 4 SV *desc = sv_2mortal(newSVpvf("%s(0x%p)",
2205 sv_reftype(sv, 0), sv));
2206 4 croak ("encountered %s, but JSON can only represent references to arrays or hashes",
2207 SvPV_nolen (desc));
2208 }
2209 74 }
2210  
2211 static void
2212 114 encode_bool (pTHX_ enc_t *enc, SV *sv)
2213 {
2214 svtype svt;
2215  
2216 114 ▶ if (!SvROK (sv))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK (sv)) 102 12 114
2217 {
2218 102 if (
2219 #ifdef PERL_HAVE_BOOLEANS
2220 102 ▶ UNLIKELY (sv == &PL_sv_yes) || (SvIsBOOL(sv) && SvTRUE(sv))
Branch: 3 branches
BranchTrueFalseTotal
UNLIKELY (sv == &PL_sv_yes) || (SvIsBOOL(sv) && SvTRUE(sv)) 94 8 102
UNLIKELY (sv == &PL_sv_yes) || (SvIsBOOL(sv) && SvTRUE(sv)) 15 79 94
UNLIKELY (sv == &PL_sv_yes) || (SvIsBOOL(sv) && SvTRUE(sv)) 4 11 15
2221 #else
2222 UNLIKELY (sv == &PL_sv_yes)
2223 #endif
2224 )
2225 12 encode_const_str (aTHX_ enc, "true", 4, 0);
2226 90 else if (
2227 #ifdef PERL_HAVE_BOOLEANS
2228 90 ▶ UNLIKELY (sv == &PL_sv_no) || (SvIsBOOL(sv) && !SvTRUE(sv))
Branch: 3 branches
BranchTrueFalseTotal
UNLIKELY (sv == &PL_sv_no) || (SvIsBOOL(sv) && !SvTRUE(sv)) 84 6 90
UNLIKELY (sv == &PL_sv_no) || (SvIsBOOL(sv) && !SvTRUE(sv)) 5 79 84
UNLIKELY (sv == &PL_sv_no) || (SvIsBOOL(sv) && !SvTRUE(sv)) 5 0 5
2229 #else
2230 UNLIKELY (sv == &PL_sv_no)
2231 #endif
2232 )
2233 11 encode_const_str (aTHX_ enc, "false", 5, 0);
2234 79 ▶ else if (!SvOK (sv))
Branch: 1 branch
BranchTrueFalseTotal
else if (!SvOK (sv)) 1 78 79
2235 1 encode_const_str (aTHX_ enc, "false", 5, 0);
2236 78 ▶ else if (SvTRUE_nomg (sv))
Branch: 1 branch
BranchTrueFalseTotal
else if (SvTRUE_nomg (sv)) 64 14 78
2237 64 encode_const_str (aTHX_ enc, "true", 4, 0);
2238 else
2239 14 encode_const_str (aTHX_ enc, "false", 5, 0);
2240 }
2241 else
2242 {
2243 12 sv = SvRV (sv);
2244 12 svt = SvTYPE (sv);
2245  
2246 12 ▶ if (UNLIKELY (SvOBJECT (sv)))
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY (SvOBJECT (sv))) 9 3 12
2247 {
2248 9 ▶ if (!encode_bool_obj (aTHX_ enc, sv, 1, 0))
Branch: 1 branch
BranchTrueFalseTotal
if (!encode_bool_obj (aTHX_ enc, sv, 1, 0)) 0 9 9
2249 {
2250 /* Use default stringification, bypassing any "" overload (GH #191). */
2251 0 ▶ const char *name = HvNAME(SvSTASH(sv));
Branch: 6 branches
BranchTrueFalseTotal
const char *name = HvNAME(SvSTASH(sv)); 0 0 0
const char *name = HvNAME(SvSTASH(sv)); 0 0 0
const char *name = HvNAME(SvSTASH(sv)); 0 0 0
const char *name = HvNAME(SvSTASH(sv)); 0 0 0
const char *name = HvNAME(SvSTASH(sv)); 0 0 0
const char *name = HvNAME(SvSTASH(sv)); 0 0 0
2252 0 ▶ SV *desc = sv_2mortal(newSVpvf("%s=%s(0x%p)",
Branch: 1 branch
BranchTrueFalseTotal
SV *desc = sv_2mortal(newSVpvf("%s=%s(0x%p)", 0 0 0
2253 name ? name : "(unknown)", sv_reftype(sv, 0), sv));
2254 0 croak ("encountered object '%s', but convert_blessed is not enabled",
2255 SvPV_nolen (desc));
2256 }
2257 }
2258 3 ▶ else if (svt < SVt_PVAV && svt != SVt_PVGV)
Branch: 2 branches
BranchTrueFalseTotal
else if (svt < SVt_PVAV && svt != SVt_PVGV) 3 0 3
else if (svt < SVt_PVAV && svt != SVt_PVGV) 3 0 3
2259 {
2260 3 ▶ if (!encode_bool_ref (aTHX_ enc, sv))
Branch: 1 branch
BranchTrueFalseTotal
if (!encode_bool_ref (aTHX_ enc, sv)) 0 3 3
2261 {
2262 /* Use default stringification, bypassing any "" overload (GH #191). */
2263 0 ▶ const char *name = HvNAME(SvSTASH(sv));
Branch: 6 branches
BranchTrueFalseTotal
const char *name = HvNAME(SvSTASH(sv)); 0 0 0
const char *name = HvNAME(SvSTASH(sv)); 0 0 0
const char *name = HvNAME(SvSTASH(sv)); 0 0 0
const char *name = HvNAME(SvSTASH(sv)); 0 0 0
const char *name = HvNAME(SvSTASH(sv)); 0 0 0
const char *name = HvNAME(SvSTASH(sv)); 0 0 0
2264 0 ▶ SV *desc = sv_2mortal(newSVpvf("%s=%s(0x%p)",
Branch: 1 branch
BranchTrueFalseTotal
SV *desc = sv_2mortal(newSVpvf("%s=%s(0x%p)", 0 0 0
2265 name ? name : "(unknown)", sv_reftype(sv, 0), sv));
2266 0 croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1",
2267 SvPV_nolen (desc));
2268 }
2269 }
2270 else
2271 {
2272 /* Use default stringification, bypassing any "" overload (GH #191). */
2273 0 ▶ const char *name = SvOBJECT(sv) ? HvNAME(SvSTASH(sv)) : NULL;
Branch: 7 branches
BranchTrueFalseTotal
const char *name = SvOBJECT(sv) ? HvNAME(SvSTASH(sv)) : NULL; 0 0 0
const char *name = SvOBJECT(sv) ? HvNAME(SvSTASH(sv)) : NULL; 0 0 0
const char *name = SvOBJECT(sv) ? HvNAME(SvSTASH(sv)) : NULL; 0 0 0
const char *name = SvOBJECT(sv) ? HvNAME(SvSTASH(sv)) : NULL; 0 0 0
const char *name = SvOBJECT(sv) ? HvNAME(SvSTASH(sv)) : NULL; 0 0 0
const char *name = SvOBJECT(sv) ? HvNAME(SvSTASH(sv)) : NULL; 0 0 0
const char *name = SvOBJECT(sv) ? HvNAME(SvSTASH(sv)) : NULL; 0 0 0
2274 0 ▶ SV *desc = sv_2mortal(newSVpvf("%s=%s(0x%p)",
Branch: 1 branch
BranchTrueFalseTotal
SV *desc = sv_2mortal(newSVpvf("%s=%s(0x%p)", 0 0 0
2275 name ? name : "(unknown)", sv_reftype(sv, 0), sv));
2276 0 croak ("encountered %s, but does not represent boolean",
2277 SvPV_nolen (desc));
2278 }
2279 }
2280 114 }
2281  
2282 static void
2283 32 sv_to_ivuv (pTHX_ SV *sv, int *is_neg, IV *iv, UV *uv)
2284 {
2285 32 *iv = SvIV_nomg (sv);
2286 32 *uv = (UV)(*iv);
2287 /* SvIV and SvUV may modify SvIsUV flag */
2288 32 *is_neg = !SvIsUV (sv);
2289 32 ▶ if (!*is_neg)
Branch: 1 branch
BranchTrueFalseTotal
if (!*is_neg) 9 23 32
2290 {
2291 9 *uv = SvUV_nomg (sv);
2292 9 *iv = (IV)(*uv);
2293 }
2294 32 }
2295  
2296 /* 2 ASCII digits for every value 0..99, used by uv_to_str() below (GH #237) */
2297 static const char digits_100[] =
2298 "00010203040506070809"
2299 "10111213141516171819"
2300 "20212223242526272829"
2301 "30313233343536373839"
2302 "40414243444546474849"
2303 "50515253545556575859"
2304 "60616263646566676869"
2305 "70717273747576777879"
2306 "80818283848586878889"
2307 "90919293949596979899";
2308  
2309 /* format uv in decimal into the buffer ending at bufend (exclusive),
2310 * writing backwards, and return a pointer to the first digit written.
2311 * uses a 2-digits-at-a-time lookup table, much faster than snprintf for
2312 * both small and large integers. */
2313 static char *
2314 2887 uv_to_str (char *bufend, UV uv)
2315 {
2316 2887 char *p = bufend;
2317  
2318 20281 ▶ while (uv >= 100)
Branch: 1 branch
BranchTrueFalseTotal
while (uv >= 100) 17394 2887 20281
2319 {
2320 17394 UV rem = (uv % 100) * 2;
2321 17394 uv /= 100;
2322 17394 *--p = digits_100[rem + 1];
2323 17394 *--p = digits_100[rem];
2324 }
2325  
2326 2887 ▶ if (uv >= 10)
Branch: 1 branch
BranchTrueFalseTotal
if (uv >= 10) 2872 15 2887
2327 {
2328 2872 UV idx = uv * 2;
2329 2872 *--p = digits_100[idx + 1];
2330 2872 *--p = digits_100[idx];
2331 }
2332 else
2333 15 *--p = '0' + (char)uv;
2334  
2335 2887 return p;
2336 }
2337  
2338 static void
2339 159181 encode_sv (pTHX_ enc_t *enc, SV *sv, SV *typesv)
2340 {
2341 159181 IV type = 0;
2342 159181 int can_be_null = 0;
2343 159181 int process_ref = 0;
2344 159181 int force_conversion = 0;
2345  
2346 159181 SvGETMAGIC (sv);
2347 159181 SvGETMAGIC (typesv);
2348  
2349 /* Unwrap json_type_optional() markers: encode() only ever sees
2350 values that are actually present in the data, so a key's "may be
2351 absent" marker is irrelevant here -- just use the wrapped type.
2352 This lets one schema be shared between encode() and
2353 Cpanel::JSON::XS::Type::check_type() / decode_and_validate(),
2354 where json_type_optional() marks a hash key as not required
2355 (GH #242). */
2356 159189 ▶ while (UNLIKELY (SvROK (typesv) && SvOBJECT (SvRV (typesv))))
Branch: 2 branches
BranchTrueFalseTotal
while (UNLIKELY (SvROK (typesv) && SvOBJECT (SvRV (typesv)))) 144 159045 159189
while (UNLIKELY (SvROK (typesv) && SvOBJECT (SvRV (typesv)))) 115 29 144
2357 {
2358 115 HV *stash = SvSTASH (SvRV (typesv));
2359 115 ▶ char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL;
Branch: 7 branches
BranchTrueFalseTotal
char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL; 115 0 115
char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL; 115 0 115
char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL; 115 0 115
char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL; 0 115 115
char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL; 0 0 0
char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL; 115 0 115
char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL; 0 115 115
2360 115 ▶ if (!(name && strEQ (name, JSON_TYPE_OPTIONAL_CLASS)))
Branch: 2 branches
BranchTrueFalseTotal
if (!(name && strEQ (name, JSON_TYPE_OPTIONAL_CLASS))) 115 0 115
if (!(name && strEQ (name, JSON_TYPE_OPTIONAL_CLASS))) 8 107 115
2361 break;
2362 8 typesv = SvRV (typesv);
2363 8 SvGETMAGIC (typesv);
2364 }
2365  
2366 159181 ▶ if (UNLIKELY (!(SvOK (typesv)) && (enc->json.flags & F_REQUIRE_TYPES) && !(enc->json.flags & F_TYPE_ALL_STRING)))
Branch: 4 branches
BranchTrueFalseTotal
if (UNLIKELY (!(SvOK (typesv)) && (enc->json.flags & F_REQUIRE_TYPES) && !(enc->json.flags & F_TYPE_ALL_STRING))) 158705 476 159181
if (UNLIKELY (!(SvOK (typesv)) && (enc->json.flags & F_REQUIRE_TYPES) && !(enc->json.flags & F_TYPE_ALL_STRING))) 29 158676 158705
if (UNLIKELY (!(SvOK (typesv)) && (enc->json.flags & F_REQUIRE_TYPES) && !(enc->json.flags & F_TYPE_ALL_STRING))) 29 159152 159181
if (UNLIKELY (!(SvOK (typesv)) && (enc->json.flags & F_REQUIRE_TYPES) && !(enc->json.flags & F_TYPE_ALL_STRING))) 8 21 29
2367 8 croak ("type for '%s' was not specified", SvPV_nolen (sv));
2368  
2369 159173 ▶ if (SvROK (sv) && !SvOBJECT (SvRV (sv)))
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK (sv) && !SvOBJECT (SvRV (sv))) 45071 114102 159173
if (SvROK (sv) && !SvOBJECT (SvRV (sv))) 44946 125 45071
2370 {
2371 44946 svtype svt = SvTYPE (SvRV (sv));
2372 44946 ▶ if (svt == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
if (svt == SVt_PVHV) 26225 18721 44946
2373 {
2374 26225 encode_hv (aTHX_ enc, (HV *)SvRV (sv), typesv);
2375 26216 return;
2376 }
2377 18721 ▶ else if (svt == SVt_PVAV)
Branch: 1 branch
BranchTrueFalseTotal
else if (svt == SVt_PVAV) 18688 33 18721
2378 {
2379 18688 encode_av (aTHX_ enc, (AV *)SvRV (sv), typesv);
2380 18672 return;
2381 }
2382 }
2383  
2384 114260 ▶ if (UNLIKELY (!(SvOK (typesv)) && (enc->json.flags & F_TYPE_ALL_STRING)))
Branch: 2 branches
BranchTrueFalseTotal
if (UNLIKELY (!(SvOK (typesv)) && (enc->json.flags & F_TYPE_ALL_STRING))) 113837 423 114260
if (UNLIKELY (!(SvOK (typesv)) && (enc->json.flags & F_TYPE_ALL_STRING))) 27 113810 113837
2385 {
2386 /* Don't force STRING on blessed objects that would be handled by
2387 allow_blessed/convert_blessed/allow_tags (GH #175). */
2388 30 ▶ if (!(SvROK (sv) && SvOBJECT (SvRV (sv))
Branch: 1 branch
BranchTrueFalseTotal
if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) 7 20 27
2389 7 ▶ && (enc->json.flags & (F_ALLOW_BLESSED|F_CONV_BLESSED|F_ALLOW_TAGS))
Branch: 1 branch
BranchTrueFalseTotal
&& (enc->json.flags & (F_ALLOW_BLESSED|F_CONV_BLESSED|F_ALLOW_TAGS)) 5 2 7
2390 5 ▶ && !is_bool_obj (aTHX_ SvRV (sv))
Branch: 1 branch
BranchTrueFalseTotal
&& !is_bool_obj (aTHX_ SvRV (sv)) 3 2 5
2391 3 && !is_bignum_obj (aTHX_ SvRV (sv))))
2392 24 typesv = sv_2mortal (newSViv (JSON_TYPE_STRING | JSON_TYPE_CAN_BE_NULL));
2393 }
2394  
2395 114260 ▶ if (UNLIKELY (SvOK (typesv)))
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY (SvOK (typesv))) 447 113813 114260
2396 {
2397 447 ▶ if (SvROK (sv) && SvOBJECT (SvRV (sv)) && !(enc->json.flags & (F_ALLOW_TAGS|F_CONV_BLESSED|F_ALLOW_BLESSED)) && !is_bool_obj (aTHX_ SvRV (sv)) && !is_bignum_obj (aTHX_ SvRV (sv)))
Branch: 5 branches
BranchTrueFalseTotal
if (SvROK (sv) && SvOBJECT (SvRV (sv)) && !(enc->json.flags & (F_ALLOW_TAGS|F_CONV_BLESSED|F_ALLOW_BLESSED)) && !is_bool_obj (aTHX_ SvRV (sv)) && !is_bignum_obj (aTHX_ SvRV (sv))) 60 387 447
if (SvROK (sv) && SvOBJECT (SvRV (sv)) && !(enc->json.flags & (F_ALLOW_TAGS|F_CONV_BLESSED|F_ALLOW_BLESSED)) && !is_bool_obj (aTHX_ SvRV (sv)) && !is_bignum_obj (aTHX_ SvRV (sv))) 57 3 60
if (SvROK (sv) && SvOBJECT (SvRV (sv)) && !(enc->json.flags & (F_ALLOW_TAGS|F_CONV_BLESSED|F_ALLOW_BLESSED)) && !is_bool_obj (aTHX_ SvRV (sv)) && !is_bignum_obj (aTHX_ SvRV (sv))) 55 2 57
if (SvROK (sv) && SvOBJECT (SvRV (sv)) && !(enc->json.flags & (F_ALLOW_TAGS|F_CONV_BLESSED|F_ALLOW_BLESSED)) && !is_bool_obj (aTHX_ SvRV (sv)) && !is_bignum_obj (aTHX_ SvRV (sv))) 34 21 55
if (SvROK (sv) && SvOBJECT (SvRV (sv)) && !(enc->json.flags & (F_ALLOW_TAGS|F_CONV_BLESSED|F_ALLOW_BLESSED)) && !is_bool_obj (aTHX_ SvRV (sv)) && !is_bignum_obj (aTHX_ SvRV (sv))) 2 32 34
2398 {
2399 /* Use default stringification, bypassing any "" overload (GH #191). */
2400 2 ▶ const char *name = HvNAME(SvSTASH(SvRV(sv)));
Branch: 6 branches
BranchTrueFalseTotal
const char *name = HvNAME(SvSTASH(SvRV(sv))); 2 0 2
const char *name = HvNAME(SvSTASH(SvRV(sv))); 2 0 2
const char *name = HvNAME(SvSTASH(SvRV(sv))); 0 2 2
const char *name = HvNAME(SvSTASH(SvRV(sv))); 0 0 0
const char *name = HvNAME(SvSTASH(SvRV(sv))); 2 0 2
const char *name = HvNAME(SvSTASH(SvRV(sv))); 0 2 2
2401 2 ▶ SV *desc = sv_2mortal(newSVpvf("%s=%s(0x%p)",
Branch: 1 branch
BranchTrueFalseTotal
SV *desc = sv_2mortal(newSVpvf("%s=%s(0x%p)", 2 0 2
2402 name ? name : "(unknown)", sv_reftype(SvRV(sv), 0), SvRV(sv)));
2403 2 croak ("encountered object '%s', but neither allow_blessed, convert_blessed nor allow_tags settings are enabled (or TO_JSON/FREEZE method missing)", SvPV_nolen (desc));
2404 }
2405  
2406 445 ▶ if (!SvIOKp (typesv))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvIOKp (typesv)) 83 362 445
2407 {
2408 83 ▶ if (SvROK (typesv) &&
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK (typesv) && 83 0 83
2409 83 ▶ SvOBJECT (SvRV (typesv)) &&
Branch: 1 branch
BranchTrueFalseTotal
SvOBJECT (SvRV (typesv)) && 83 0 83
2410 83 ▶ SvTYPE (SvRV (typesv)) == SVt_PVAV)
Branch: 1 branch
BranchTrueFalseTotal
SvTYPE (SvRV (typesv)) == SVt_PVAV) 83 0 83
2411 83 {
2412 83 HV *stash = SvSTASH (SvRV (typesv));
2413 83 ▶ char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL;
Branch: 7 branches
BranchTrueFalseTotal
char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL; 83 0 83
char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL; 83 0 83
char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL; 83 0 83
char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL; 0 83 83
char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL; 0 0 0
char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL; 83 0 83
char *name = LIKELY (!!stash) ? HvNAME (stash) : NULL; 0 83 83
2414 83 ▶ if (LIKELY (name && strEQ (name, JSON_TYPE_ANYOF_CLASS)))
Branch: 2 branches
BranchTrueFalseTotal
if (LIKELY (name && strEQ (name, JSON_TYPE_ANYOF_CLASS))) 83 0 83
if (LIKELY (name && strEQ (name, JSON_TYPE_ANYOF_CLASS))) 83 0 83
2415 83 {
2416 83 AV *type_any = (AV *)SvRV (typesv);
2417 83 SV **typesv_ref = av_fetch (type_any, JSON_TYPE_ANYOF_SCALAR_INDEX, 0);
2418 83 ▶ if (UNLIKELY (!typesv_ref))
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY (!typesv_ref)) 0 83 83
2419 0 ▶ croak ("incorrectly constructed anyof type (%s, 0x%x) was specified for '%s'",
Branch: 1 branch
BranchTrueFalseTotal
croak ("incorrectly constructed anyof type (%s, 0x%x) was specified for '%s'", 0 0 0
2420 SvPV_nolen (typesv), (unsigned int)SvFLAGS (typesv),
2421 SvPV_nolen (sv));
2422 83 typesv = *typesv_ref;
2423 83 SvGETMAGIC (typesv);
2424 83 ▶ if (!SvIOKp (typesv))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvIOKp (typesv)) 0 83 83
2425 0 croak ("no scalar alternative in anyof was specified for '%s'", SvPV_nolen (sv));
2426 }
2427 else
2428 0 ▶ croak ("encountered type (%s, 0x%x) was specified for '%s'",
Branch: 1 branch
BranchTrueFalseTotal
croak ("encountered type (%s, 0x%x) was specified for '%s'", 0 0 0
2429 SvPV_nolen (typesv), (unsigned int)SvFLAGS (typesv),
2430 SvPV_nolen (sv));
2431 }
2432 else
2433 0 ▶ croak ("encountered type (%s, 0x%x) was specified for '%s'",
Branch: 1 branch
BranchTrueFalseTotal
croak ("encountered type (%s, 0x%x) was specified for '%s'", 0 0 0
2434 SvPV_nolen (typesv), (unsigned int)SvFLAGS (typesv),
2435 SvPV_nolen (sv));
2436 }
2437 445 type = SvIVX (typesv);
2438 }
2439  
2440 114258 ▶ if (UNLIKELY (type))
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY (type)) 443 113815 114258
2441 {
2442 443 force_conversion = 1;
2443 443 can_be_null = (type & JSON_TYPE_CAN_BE_NULL);
2444 443 type &= ~JSON_TYPE_CAN_BE_NULL;
2445 }
2446 else
2447 {
2448 113815 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ( 113807 8 113815
2449 #ifdef PERL_HAVE_BOOLEANS
2450 227622 ▶ UNLIKELY (sv == &PL_sv_yes || sv == &PL_sv_no || SvIsBOOL(sv))
Branch: 3 branches
BranchTrueFalseTotal
UNLIKELY (sv == &PL_sv_yes || sv == &PL_sv_no || SvIsBOOL(sv)) 113811 4 113815
UNLIKELY (sv == &PL_sv_yes || sv == &PL_sv_no || SvIsBOOL(sv)) 4 113807 113811
UNLIKELY (sv == &PL_sv_yes || sv == &PL_sv_no || SvIsBOOL(sv)) 5 113802 113807
2451 #else
2452 UNLIKELY (sv == &PL_sv_yes || sv == &PL_sv_no)
2453 #endif
2454 13 ) type = JSON_TYPE_BOOL;
2455 113802 ▶ else if (SvNOK (sv)) type = JSON_TYPE_FLOAT;
Branch: 1 branch
BranchTrueFalseTotal
else if (SvNOK (sv)) type = JSON_TYPE_FLOAT; 319 113483 113802
2456 113483 ▶ else if (SvIOKp (sv)) type = JSON_TYPE_INT;
Branch: 1 branch
BranchTrueFalseTotal
else if (SvIOKp (sv)) type = JSON_TYPE_INT; 60658 52825 113483
2457 52825 ▶ else if (SvPOKp (sv)) type = JSON_TYPE_STRING;
Branch: 1 branch
BranchTrueFalseTotal
else if (SvPOKp (sv)) type = JSON_TYPE_STRING; 46371 6454 52825
2458 6454 ▶ else if (SvROK (sv)) process_ref = 1;
Branch: 1 branch
BranchTrueFalseTotal
else if (SvROK (sv)) process_ref = 1; 98 6356 6454
2459 6356 ▶ else if (!SvOK (sv)) can_be_null = 1;
Branch: 1 branch
BranchTrueFalseTotal
else if (!SvOK (sv)) can_be_null = 1; 6356 0 6356
2460 }
2461  
2462 114258 ▶ if (can_be_null && !SvOK (sv))
Branch: 2 branches
BranchTrueFalseTotal
if (can_be_null && !SvOK (sv)) 6498 107760 114258
if (can_be_null && !SvOK (sv)) 6375 123 6498
2463 6375 encode_const_str (aTHX_ enc, "null", 4, 0);
2464 107883 ▶ else if (type == JSON_TYPE_BOOL)
Branch: 1 branch
BranchTrueFalseTotal
else if (type == JSON_TYPE_BOOL) 114 107769 107883
2465 114 encode_bool (aTHX_ enc, sv);
2466 107769 ▶ else if (type == JSON_TYPE_FLOAT)
Branch: 1 branch
BranchTrueFalseTotal
else if (type == JSON_TYPE_FLOAT) 402 107367 107769
2467 {
2468 402 int is_bigobj = 0;
2469 402 char *savecur = NULL, *saveend = NULL;
2470 402 char inf_or_nan = 0;
2471 #ifdef NEED_NUMERIC_LOCALE_C
2472 # ifdef HAS_USELOCALE
2473 locale_t oldloc = (locale_t)0;
2474 locale_t newloc;
2475 # endif
2476 bool loc_changed = FALSE;
2477 char *locale = NULL;
2478 #endif
2479 402 NV nv = 0;
2480 402 int had_nokp = SvNOKp(sv);
2481  
2482 402 ▶ if (UNLIKELY (SvROK (sv) && SvOBJECT (SvRV (sv))) && (enc->json.flags & F_ALLOW_BIGNUM) && is_bignum_obj (aTHX_ SvRV (sv)))
Branch: 4 branches
BranchTrueFalseTotal
if (UNLIKELY (SvROK (sv) && SvOBJECT (SvRV (sv))) && (enc->json.flags & F_ALLOW_BIGNUM) && is_bignum_obj (aTHX_ SvRV (sv))) 19 383 402
if (UNLIKELY (SvROK (sv) && SvOBJECT (SvRV (sv))) && (enc->json.flags & F_ALLOW_BIGNUM) && is_bignum_obj (aTHX_ SvRV (sv))) 19 0 19
if (UNLIKELY (SvROK (sv) && SvOBJECT (SvRV (sv))) && (enc->json.flags & F_ALLOW_BIGNUM) && is_bignum_obj (aTHX_ SvRV (sv))) 16 3 19
if (UNLIKELY (SvROK (sv) && SvOBJECT (SvRV (sv))) && (enc->json.flags & F_ALLOW_BIGNUM) && is_bignum_obj (aTHX_ SvRV (sv))) 16 0 16
2483 16 is_bigobj = 1;
2484  
2485 402 ▶ if (UNLIKELY (is_bigobj))
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY (is_bigobj)) 16 386 402
2486 {
2487 STRLEN len;
2488 16 SV *compact = bignum_compact (aTHX_ sv);
2489 32 ▶ char *str = compact ? SvPV (compact, len) : SvPV_nomg (sv, len);
Branch: 1 branch
BranchTrueFalseTotal
char *str = compact ? SvPV (compact, len) : SvPV_nomg (sv, len); 0 16 16
2490 16 ▶ if (UNLIKELY (str[0] == '+'))
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY (str[0] == '+')) 0 16 16
2491 {
2492 0 str++;
2493 0 len--;
2494 }
2495 16 ▶ if (UNLIKELY (memEQc (str, "NaN") || memEQc (str, "nan")))
Branch: 2 branches
BranchTrueFalseTotal
if (UNLIKELY (memEQc (str, "NaN") || memEQc (str, "nan"))) 14 2 16
if (UNLIKELY (memEQc (str, "NaN") || memEQc (str, "nan"))) 0 14 14
2496 {
2497 2 nv = NV_NAN;
2498 2 is_bigobj = 0;
2499 }
2500 14 ▶ else if (UNLIKELY (memEQc (str, "inf")))
Branch: 1 branch
BranchTrueFalseTotal
else if (UNLIKELY (memEQc (str, "inf"))) 2 12 14
2501 {
2502 2 nv = NV_INF;
2503 2 is_bigobj = 0;
2504 }
2505 12 ▶ else if (UNLIKELY (memEQc (str, "-inf")))
Branch: 1 branch
BranchTrueFalseTotal
else if (UNLIKELY (memEQc (str, "-inf"))) 2 10 12
2506 {
2507 2 nv = -NV_INF;
2508 2 is_bigobj = 0;
2509 }
2510 else
2511 {
2512 10 need (aTHX_ enc, len+1+2); /* +2 for '.0' */
2513 10 savecur = enc->cur;
2514 10 saveend = enc->end;
2515 10 memcpy (enc->cur, str, len);
2516 10 *(enc->cur+len) = '\0';
2517 }
2518 }
2519 386 ▶ else if (SvNOKp (sv))
Branch: 1 branch
BranchTrueFalseTotal
else if (SvNOKp (sv)) 357 29 386
2520 {
2521 357 nv = SvNVX (sv);
2522 }
2523 else
2524 {
2525 29 ▶ if (enc->json.flags & F_ALLOW_BIGNUM)
Branch: 1 branch
BranchTrueFalseTotal
if (enc->json.flags & F_ALLOW_BIGNUM) 15 14 29
2526 {
2527 STRLEN len;
2528 char *str;
2529 SV *pv;
2530 SV *errsv;
2531 int numtype;
2532  
2533 15 str = SvPV_nomg (sv, len);
2534  
2535 15 numtype = grok_number (str, len, NULL);
2536 15 ▶ if (UNLIKELY (numtype & IS_NUMBER_INFINITY))
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY (numtype & IS_NUMBER_INFINITY)) 2 13 15
2537 2 ▶ nv = (numtype & IS_NUMBER_NEG) ? -NV_INF : NV_INF;
Branch: 1 branch
BranchTrueFalseTotal
nv = (numtype & IS_NUMBER_NEG) ? -NV_INF : NV_INF; 1 1 2
2538 13 ▶ else if (UNLIKELY (numtype & IS_NUMBER_NAN))
Branch: 1 branch
BranchTrueFalseTotal
else if (UNLIKELY (numtype & IS_NUMBER_NAN)) 1 12 13
2539 1 nv = NV_NAN;
2540 12 ▶ else if (UNLIKELY (!numtype))
Branch: 1 branch
BranchTrueFalseTotal
else if (UNLIKELY (!numtype)) 0 12 12
2541 0 nv = SvNV_nomg (sv);
2542 else
2543 {
2544 12 pv = newSVpvs ("require Math::BigFloat && Math::BigFloat->new(\"");
2545 12 sv_catpvn (pv, str, len);
2546 12 sv_catpvs (pv, "\");");
2547  
2548 12 eval_sv (pv, G_SCALAR);
2549 12 SvREFCNT_dec (pv);
2550  
2551 /* rethrow current error */
2552 12 ▶ errsv = ERRSV;
Branch: 1 branch
BranchTrueFalseTotal
errsv = ERRSV; 12 0 12
2553 12 ▶ if (SvROK (errsv))
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK (errsv)) 0 12 12
2554 0 croak (NULL);
2555 12 ▶ else if (SvTRUE (errsv))
Branch: 1 branch
BranchTrueFalseTotal
else if (SvTRUE (errsv)) 0 12 12
2556 0 croak ("%" SVf, SVfARG (errsv));
2557  
2558 {
2559 12 dSP;
2560 12 pv = POPs;
2561 12 PUTBACK;
2562 }
2563  
2564 {
2565 12 SV *compact = bignum_compact (aTHX_ pv);
2566 24 ▶ str = compact ? SvPV (compact, len) : SvPV (pv, len);
Branch: 1 branch
BranchTrueFalseTotal
str = compact ? SvPV (compact, len) : SvPV (pv, len); 0 12 12
2567 }
2568 12 ▶ if (UNLIKELY (str[0] == '+'))
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY (str[0] == '+')) 0 12 12
2569 {
2570 0 str++;
2571 0 len--;
2572 }
2573 12 need (aTHX_ enc, len+1);
2574 12 savecur = enc->cur;
2575 12 saveend = enc->end;
2576 12 memcpy (enc->cur, str, len);
2577 12 *(enc->cur+len) = '\0';
2578 12 is_bigobj = 1;
2579 }
2580 }
2581 else
2582 {
2583  
2584 #if PERL_VERSION < 8 || (PERL_VERSION == 8 && PERL_SUBVERSION < 8)
2585 if (SvPOKp (sv))
2586 {
2587 int numtype = grok_number (SvPVX (sv), SvCUR (sv), NULL);
2588 if (UNLIKELY (numtype & IS_NUMBER_INFINITY))
2589 nv = (numtype & IS_NUMBER_NEG) ? -NV_INF : NV_INF;
2590 else if (UNLIKELY (numtype & IS_NUMBER_NAN))
2591 nv = NV_NAN;
2592 else
2593 nv = SvNV_nomg (sv);
2594 }
2595 else
2596 {
2597 nv = SvNV_nomg (sv);
2598 }
2599 #else
2600 14 nv = SvNV_nomg (sv);
2601 #endif
2602 }
2603 }
2604  
2605 402 ▶ if (LIKELY (!is_bigobj))
Branch: 1 branch
BranchTrueFalseTotal
if (LIKELY (!is_bigobj)) 380 22 402
2606 {
2607 /* trust that perl will do the right thing w.r.t. JSON syntax. */
2608 380 need (aTHX_ enc, NV_DIG + 32);
2609 380 savecur = enc->cur;
2610 380 saveend = enc->end;
2611  
2612 380 ▶ if (force_conversion)
Branch: 1 branch
BranchTrueFalseTotal
if (force_conversion) 61 319 380
2613 {
2614 61 had_nokp = 0;
2615 #if defined(USE_QUADMATH) && defined(WIN32) /* Use Perl_isinf */
2616 if (UNLIKELY(Perl_isinf(nv)))
2617  
2618 #elif defined(USE_QUADMATH) && defined(HAVE_ISINFL)
2619 if (UNLIKELY(isinfl(nv)))
2620 #else
2621 61 ▶ if (UNLIKELY(isinf(nv)))
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY(isinf(nv))) 16 45 61
2622 #endif
2623 16 ▶ nv = (nv > 0) ? NV_MAX : -NV_MAX;
Branch: 1 branch
BranchTrueFalseTotal
nv = (nv > 0) ? NV_MAX : -NV_MAX; 8 8 16
2624 #if defined(USE_QUADMATH) && defined(WIN32) /* Use Perl_isnan */
2625 if (UNLIKELY(Perl_isnan(nv)))
2626  
2627 #elif defined(USE_QUADMATH) && defined(HAVE_ISNANL)
2628 if (UNLIKELY(isnanl(nv)))
2629 #else
2630 61 ▶ if (UNLIKELY(isnan(nv)))
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY(isnan(nv))) 6 55 61
2631 #endif
2632 6 nv = 0;
2633 }
2634 /* With no stringify_infnan we can skip the conversion, returning null. */
2635 319 ▶ else if (enc->json.infnan_mode == 0)
Branch: 1 branch
BranchTrueFalseTotal
else if (enc->json.infnan_mode == 0) 304 15 319
2636 {
2637 #if defined(USE_QUADMATH) && defined(WIN32) /* Use Perl_isinf */
2638 if (UNLIKELY(Perl_isinf(nv)))
2639  
2640 #elif defined(USE_QUADMATH) && defined(HAVE_ISINFL)
2641 if (UNLIKELY(isinfl(nv)))
2642 #else
2643 304 ▶ if (UNLIKELY(isinf(nv)))
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY(isinf(nv))) 2 302 304
2644 #endif
2645 {
2646 2 ▶ inf_or_nan = (nv > 0) ? 1 : 2;
Branch: 1 branch
BranchTrueFalseTotal
inf_or_nan = (nv > 0) ? 1 : 2; 1 1 2
2647 2 goto is_inf_or_nan;
2648 }
2649 #if defined(USE_QUADMATH) && defined(WIN32) /* Use Perl_isnan */
2650 if (UNLIKELY(Perl_isnan(nv)))
2651  
2652 #elif defined(USE_QUADMATH) && defined(HAVE_ISNANL)
2653 if (UNLIKELY(isnanl(nv)))
2654 #else
2655 302 ▶ if (UNLIKELY(isnan(nv)))
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY(isnan(nv))) 3 299 302
2656 #endif
2657 {
2658 3 inf_or_nan = 3;
2659 3 goto is_inf_or_nan;
2660 }
2661 }
2662 /* locale insensitive sprintf radix #96 */
2663 #ifdef NEED_NUMERIC_LOCALE_C
2664 locale = setlocale(LC_NUMERIC, NULL);
2665 if (!locale || strNE(locale, "C"))
2666 {
2667 loc_changed = TRUE;
2668 # ifdef HAS_USELOCALE
2669 /* thread-safe variant for children not changing the global state */
2670 oldloc = uselocale((locale_t)0);
2671 if (oldloc == LC_GLOBAL_LOCALE)
2672 newloc = newlocale(LC_NUMERIC_MASK, "C", (locale_t)0);
2673 else
2674 newloc = newlocale(LC_NUMERIC_MASK, "C", oldloc);
2675 uselocale(newloc);
2676 # else
2677 setlocale(LC_NUMERIC, "C");
2678 # endif
2679 }
2680 #endif
2681 /* untested. see GH #83 */
2682 #if defined(__s390x__) && defined(USE_LONG_DOUBLE)
2683 {
2684 STRLEN len = enc->end - enc->cur;
2685 SV* sv = newSVpvn_flags(enc->cur, len, 0);
2686 sv_grow(sv, NV_DIG);
2687 sv_upgrade(sv, SVt_PVNV);
2688 SvNVX(sv) = nv;
2689 sv_2pv_flags(sv, &len, 0);
2690 enc->cur = SvPVX(sv);
2691 enc->end = SvEND(ev);
2692 }
2693 #elif defined(USE_QUADMATH)
2694 quadmath_snprintf(enc->cur, enc->end - enc->cur, "%.*Qg", (int)NV_DIG, nv);
2695 #else
2696 375 PERL_UNUSED_RESULT(Gconvert (nv, NV_DIG, 0, enc->cur));
2697 #endif
2698  
2699 #ifdef NEED_NUMERIC_LOCALE_C
2700 if (loc_changed)
2701 {
2702 # ifdef HAS_USELOCALE
2703 (void)uselocale(oldloc);
2704 if (newloc)
2705 freelocale(newloc);
2706 # else
2707 (void)setlocale(LC_NUMERIC, locale);
2708 # endif
2709 }
2710 #endif
2711  
2712 /* #if defined(_AIX) || defined(_AIX50)
2713 * (void)fprintf (stderr, "#|# >%s<\n", enc->cur);
2714 * #endif */
2715 #ifdef STR_INF4
2716 if (UNLIKELY(strEQc(enc->cur, STR_INF)
2717 || strEQc(enc->cur, STR_INF2)
2718 || strEQc(enc->cur, STR_INF3)
2719 || strEQc(enc->cur, STR_INF4)))
2720 #elif defined(STR_INF2)
2721 if (UNLIKELY(strEQc(enc->cur, STR_INF)
2722 || strEQc(enc->cur, STR_INF2)))
2723 #else
2724 375 ▶ if (UNLIKELY(strEQc(enc->cur, STR_INF)))
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY(strEQc(enc->cur, STR_INF))) 3 372 375
2725 #endif
2726 3 inf_or_nan = 1;
2727 #if defined(__hpux)
2728 else if (UNLIKELY(strEQc(enc->cur, STR_NEG_INF)))
2729 inf_or_nan = 2;
2730 else if (UNLIKELY(strEQc(enc->cur, STR_NEG_NAN)))
2731 inf_or_nan = 3;
2732 #endif
2733 #if defined(_AIX) || defined(_AIX50)
2734 else if (UNLIKELY(strEQc(enc->cur, STR_INF)
2735 || strEQc(enc->cur, STR_INF2)))
2736 inf_or_nan = 1;
2737 else if (UNLIKELY(strEQc(enc->cur, STR_NEG_INF)))
2738 inf_or_nan = 2;
2739 #endif
2740 else if
2741 #ifdef HAVE_QNAN
2742 # ifdef STR_QNAN2
2743 (UNLIKELY(strEQc(enc->cur, STR_NAN)
2744 || strEQc(enc->cur, STR_QNAN)
2745 || strEQc(enc->cur, STR_NAN2)
2746 || strEQc(enc->cur, STR_QNAN2)))
2747 # else
2748 (UNLIKELY(strEQc(enc->cur, STR_NAN)
2749 || strEQc(enc->cur, STR_QNAN)))
2750 # endif
2751 #else
2752 372 ▶ (UNLIKELY(strEQc(enc->cur, STR_NAN)))
Branch: 1 branch
BranchTrueFalseTotal
(UNLIKELY(strEQc(enc->cur, STR_NAN))) 3 369 372
2753 #endif
2754 3 inf_or_nan = 3;
2755 369 ▶ else if (*enc->cur == '-') {
Branch: 1 branch
BranchTrueFalseTotal
else if (*enc->cur == '-') { 25 344 369
2756 #ifdef STR_INF4
2757 if (UNLIKELY(strEQc(enc->cur+1, STR_INF)
2758 || strEQc(enc->cur+1, STR_INF2)
2759 || strEQc(enc->cur+1, STR_INF3)
2760 || strEQc(enc->cur+1, STR_INF4)))
2761 #elif defined(STR_INF2)
2762 if (UNLIKELY(strEQc(enc->cur+1, STR_INF)
2763 || strEQc(enc->cur+1, STR_INF2)))
2764 #else
2765 25 ▶ if (UNLIKELY(strEQc(enc->cur+1, STR_INF)))
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY(strEQc(enc->cur+1, STR_INF))) 3 22 25
2766 #endif
2767 3 inf_or_nan = 2;
2768 else if
2769 #ifdef HAVE_QNAN
2770 # ifdef STR_QNAN2
2771 (UNLIKELY(strEQc(enc->cur+1, STR_NAN)
2772 || strEQc(enc->cur+1, STR_QNAN)
2773 || strEQc(enc->cur+1, STR_NAN2)
2774 || strEQc(enc->cur+1, STR_QNAN2)))
2775 # else
2776 (UNLIKELY(strEQc(enc->cur+1, STR_NAN)
2777 || strEQc(enc->cur+1, STR_QNAN)))
2778 # endif
2779 #else
2780 22 ▶ (UNLIKELY(strEQc(enc->cur+1, STR_NAN)))
Branch: 1 branch
BranchTrueFalseTotal
(UNLIKELY(strEQc(enc->cur+1, STR_NAN))) 6 16 22
2781 #endif
2782 6 inf_or_nan = 3;
2783 }
2784 375 ▶ if (UNLIKELY(inf_or_nan)) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY(inf_or_nan)) { 15 360 375
2785 15 is_inf_or_nan:
2786 20 ▶ if (enc->json.infnan_mode == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (enc->json.infnan_mode == 0) { 5 15 20
2787 5 strncpy(enc->cur, "null\0", 5);
2788 }
2789 15 ▶ else if (enc->json.infnan_mode == 1) {
Branch: 1 branch
BranchTrueFalseTotal
else if (enc->json.infnan_mode == 1) { 5 10 15
2790 5 const int l = strlen(enc->cur);
2791 5 memmove(enc->cur+1, enc->cur, l);
2792 5 *enc->cur = '"';
2793 5 *(enc->cur + l+1) = '"';
2794 5 *(enc->cur + l+2) = 0;
2795 }
2796 10 ▶ else if (enc->json.infnan_mode == 3) {
Branch: 1 branch
BranchTrueFalseTotal
else if (enc->json.infnan_mode == 3) { 5 5 10
2797 5 ▶ if (inf_or_nan == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (inf_or_nan == 1) 1 4 5
2798 1 strncpy(enc->cur, "\"inf\"\0", 6);
2799 4 ▶ else if (inf_or_nan == 2)
Branch: 1 branch
BranchTrueFalseTotal
else if (inf_or_nan == 2) 1 3 4
2800 1 strncpy(enc->cur, "\"-inf\"\0", 7);
2801 3 ▶ else if (inf_or_nan == 3)
Branch: 1 branch
BranchTrueFalseTotal
else if (inf_or_nan == 3) 3 0 3
2802 3 strncpy(enc->cur, "\"nan\"\0", 6);
2803 }
2804 5 ▶ else if (enc->json.infnan_mode != 2) {
Branch: 1 branch
BranchTrueFalseTotal
else if (enc->json.infnan_mode != 2) { 0 5 5
2805 0 croak ("invalid stringify_infnan mode %c. Must be 0, 1, 2 or 3",
2806 enc->json.infnan_mode);
2807 }
2808 }
2809  
2810 }
2811  
2812 402 ▶ if (!force_conversion && SvPOKp (sv) && !strEQ(enc->cur, SvPVX (sv))) {
Branch: 3 branches
BranchTrueFalseTotal
if (!force_conversion && SvPOKp (sv) && !strEQ(enc->cur, SvPVX (sv))) { 319 83 402
if (!force_conversion && SvPOKp (sv) && !strEQ(enc->cur, SvPVX (sv))) { 0 319 319
if (!force_conversion && SvPOKp (sv) && !strEQ(enc->cur, SvPVX (sv))) { 0 0 0
2813 0 char *str = SvPVX (sv);
2814 0 STRLEN len = SvCUR (sv);
2815 0 enc->cur = savecur;
2816 0 enc->end = saveend;
2817 0 encode_ch (aTHX_ enc, '"');
2818 0 encode_str (aTHX_ enc, str, len, SvUTF8 (sv));
2819 0 encode_ch (aTHX_ enc, '"');
2820 0 *enc->cur = 0;
2821 }
2822 else {
2823 NV intpart;
2824 402 ▶ if (!( inf_or_nan || (had_nokp && Perl_modf(SvNVX(sv), &intpart))
Branch: 3 branches
BranchTrueFalseTotal
if (!( inf_or_nan || (had_nokp && Perl_modf(SvNVX(sv), &intpart)) 382 20 402
if (!( inf_or_nan || (had_nokp && Perl_modf(SvNVX(sv), &intpart)) 299 83 382
if (!( inf_or_nan || (had_nokp && Perl_modf(SvNVX(sv), &intpart)) 134 165 299
2825 #if NV_DIG < 31
2826 217 ▶ || (!force_conversion && had_nokp
Branch: 2 branches
BranchTrueFalseTotal
|| (!force_conversion && had_nokp 134 83 217
|| (!force_conversion && had_nokp 134 0 134
2827 134 ▶ && (SvNVX(sv) > (NV)UV_MAX || SvNVX(sv) < (NV)IV_MIN))
Branch: 2 branches
BranchTrueFalseTotal
&& (SvNVX(sv) > (NV)UV_MAX || SvNVX(sv) < (NV)IV_MIN)) 127 7 134
&& (SvNVX(sv) > (NV)UV_MAX || SvNVX(sv) < (NV)IV_MIN)) 127 0 127
2828 #endif
2829 210 ▶ || (!force_conversion && SvIOK(sv))
Branch: 2 branches
BranchTrueFalseTotal
|| (!force_conversion && SvIOK(sv)) 127 83 210
|| (!force_conversion && SvIOK(sv)) 120 7 127
2830 203 ▶ || strchr(enc->cur,'e')
Branch: 1 branch
BranchTrueFalseTotal
|| strchr(enc->cur,'e') 187 16 203
2831 187 ▶ || strchr(enc->cur,'E')
Branch: 1 branch
BranchTrueFalseTotal
|| strchr(enc->cur,'E') 187 0 187
2832 187 ▶ || strchr(savecur,'.')
Branch: 1 branch
BranchTrueFalseTotal
|| strchr(savecur,'.') 175 12 187
2833 #if PERL_VERSION < 10
2834 /* !!1 with 5.8 */
2835 || (SvPOKp(sv) && strEQc(SvPVX(sv), "1")
2836 && SvNVX(sv) == 1.0) /* yes */
2837 #endif
2838 ) )
2839 {
2840 175 char *tempend = enc->cur + strlen(enc->cur);
2841 175 strncpy(tempend, ".0\0", 3);
2842 }
2843 402 enc->cur += strlen (enc->cur);
2844 }
2845 }
2846 107367 ▶ else if (type == JSON_TYPE_INT)
Branch: 1 branch
BranchTrueFalseTotal
else if (type == JSON_TYPE_INT) 60815 46552 107367
2847 {
2848 char *savecur, *saveend;
2849 /* we assume we can always read an IV as a UV and vice versa */
2850 /* we assume two's complement */
2851 /* we assume no aliasing issues in the union */
2852 60815 UV uv = 0;
2853 60815 IV iv = 0;
2854 60815 int is_neg = 0;
2855  
2856 60815 ▶ if (UNLIKELY (SvROK (sv) && SvOBJECT (SvRV (sv)))
Branch: 2 branches
BranchTrueFalseTotal
if (UNLIKELY (SvROK (sv) && SvOBJECT (SvRV (sv))) 20 60795 60815
if (UNLIKELY (SvROK (sv) && SvOBJECT (SvRV (sv))) 20 0 20
2857 20 ▶ && (enc->json.flags & F_ALLOW_BIGNUM))
Branch: 1 branch
BranchTrueFalseTotal
&& (enc->json.flags & F_ALLOW_BIGNUM)) 16 4 20
2858 {
2859 16 HV *stash = SvSTASH (SvRV (sv));
2860 16 ▶ int is_bigint = (stash && stash == gv_stashpvs ("Math::BigInt", 0));
Branch: 2 branches
BranchTrueFalseTotal
int is_bigint = (stash && stash == gv_stashpvs ("Math::BigInt", 0)); 16 0 16
int is_bigint = (stash && stash == gv_stashpvs ("Math::BigInt", 0)); 8 8 16
2861 16 ▶ int is_bigfloat = (stash && stash == gv_stashpvs ("Math::BigFloat", 0));
Branch: 2 branches
BranchTrueFalseTotal
int is_bigfloat = (stash && stash == gv_stashpvs ("Math::BigFloat", 0)); 16 0 16
int is_bigfloat = (stash && stash == gv_stashpvs ("Math::BigFloat", 0)); 8 8 16
2862  
2863 16 ▶ if (is_bigint || is_bigfloat)
Branch: 2 branches
BranchTrueFalseTotal
if (is_bigint || is_bigfloat) 8 8 16
if (is_bigint || is_bigfloat) 8 0 8
2864 {
2865 STRLEN len;
2866 char *str;
2867  
2868 16 ▶ if (is_bigfloat)
Branch: 1 branch
BranchTrueFalseTotal
if (is_bigfloat) 8 8 16
2869 {
2870 8 dSP;
2871 int is_negative;
2872  
2873 8 ENTER; SAVETMPS;
2874 8 ▶ PUSHMARK (SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK (SP); 0 8 8
2875 8 ▶ XPUSHs (sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs (sv); 0 8 8
2876 8 PUTBACK;
2877  
2878 8 call_method ("is_negative", G_SCALAR);
2879  
2880 8 SPAGAIN;
2881 8 is_negative = SvTRUEx (POPs);
2882 8 PUTBACK;
2883  
2884 8 ▶ PUSHMARK (SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK (SP); 0 8 8
2885 8 ▶ XPUSHs (sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs (sv); 0 8 8
2886 8 PUTBACK;
2887  
2888 /* This bceil/bfloor logic can be replaced by just one "bint" method call
2889 * but it is not supported by older Math::BigFloat versions.
2890 * Older Math::BigFloat versions have also "as_number" method which should
2891 * do same thing as "bint" method but it is broken and loose precision.
2892 * This bceil/bfloor logic needs Math::BigFloat 1.16 which is in Perl 5.8.0. */
2893 8 ▶ call_method (is_negative ? "bceil" : "bfloor", G_SCALAR);
Branch: 1 branch
BranchTrueFalseTotal
call_method (is_negative ? "bceil" : "bfloor", G_SCALAR); 3 5 8
2894  
2895 8 SPAGAIN;
2896 8 sv = POPs;
2897 8 PUTBACK;
2898 }
2899  
2900 16 str = SvPV_nomg (sv, len);
2901 16 ▶ if (UNLIKELY (str[0] == '+'))
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY (str[0] == '+')) 0 16 16
2902 {
2903 0 str++;
2904 0 len--;
2905 }
2906  
2907 16 ▶ if (UNLIKELY (strEQc (str, "NaN") || strEQc (str, "nan")))
Branch: 2 branches
BranchTrueFalseTotal
if (UNLIKELY (strEQc (str, "NaN") || strEQc (str, "nan"))) 14 2 16
if (UNLIKELY (strEQc (str, "NaN") || strEQc (str, "nan"))) 0 14 14
2908 {
2909 2 encode_const_str (aTHX_ enc, "0", 1, 0);
2910 }
2911 14 ▶ else if (UNLIKELY (strEQc (str, "inf")))
Branch: 1 branch
BranchTrueFalseTotal
else if (UNLIKELY (strEQc (str, "inf"))) 2 12 14
2912 {
2913 2 need (aTHX_ enc, IVUV_MAXCHARS);
2914 2 savecur = enc->cur;
2915 2 saveend = enc->end;
2916 2 enc->cur += snprintf (enc->cur, IVUV_MAXCHARS, "%" UVuf, UV_MAX);
2917 }
2918 12 ▶ else if (UNLIKELY (strEQc (str, "-inf")))
Branch: 1 branch
BranchTrueFalseTotal
else if (UNLIKELY (strEQc (str, "-inf"))) 2 10 12
2919 {
2920 2 need (aTHX_ enc, IVUV_MAXCHARS);
2921 2 savecur = enc->cur;
2922 2 saveend = enc->end;
2923 2 enc->cur += snprintf (enc->cur, IVUV_MAXCHARS, "%" IVdf, IV_MIN);
2924 }
2925 else
2926 {
2927 10 need (aTHX_ enc, len+1);
2928 10 savecur = enc->cur;
2929 10 saveend = enc->end;
2930 10 memcpy (enc->cur, str, len);
2931 10 enc->cur += len;
2932 10 *enc->cur = '\0';
2933 }
2934  
2935 16 ▶ if (is_bigfloat)
Branch: 1 branch
BranchTrueFalseTotal
if (is_bigfloat) 8 8 16
2936 {
2937 8 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 8 0 8
2938 8 LEAVE;
2939 }
2940  
2941 16 return;
2942 }
2943 }
2944  
2945 60799 ▶ if (SvIOK (sv))
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK (sv)) 60713 86 60799
2946 {
2947 60713 is_neg = !SvIsUV (sv);
2948 60713 iv = SvIVX (sv);
2949 60713 uv = SvUVX (sv);
2950 }
2951 86 ▶ else if (SvPOKp (sv))
Branch: 1 branch
BranchTrueFalseTotal
else if (SvPOKp (sv)) 59 27 86
2952 {
2953 59 int numtype = grok_number (SvPVX (sv), SvCUR (sv), &uv);
2954 59 ▶ if (numtype & IS_NUMBER_IN_UV)
Branch: 1 branch
BranchTrueFalseTotal
if (numtype & IS_NUMBER_IN_UV) 43 16 59
2955 {
2956 43 ▶ if (numtype & IS_NUMBER_NEG)
Branch: 1 branch
BranchTrueFalseTotal
if (numtype & IS_NUMBER_NEG) 10 33 43
2957 {
2958 10 is_neg = 1;
2959 10 ▶ if (LIKELY(uv <= (UV)(IV_MAX) + 1))
Branch: 1 branch
BranchTrueFalseTotal
if (LIKELY(uv <= (UV)(IV_MAX) + 1)) 3 7 10
2960 3 iv = -(IV)uv;
2961 else
2962 {
2963 7 iv = IV_MIN; /* underflow, but F_ALLOW_BIGNUM can handle this */
2964 7 numtype |= IS_NUMBER_GREATER_THAN_UV_MAX;
2965 }
2966 10 uv = (UV)iv;
2967 }
2968 else
2969 33 iv = (IV)uv;
2970 }
2971 16 ▶ else if (UNLIKELY (numtype & IS_NUMBER_INFINITY))
Branch: 1 branch
BranchTrueFalseTotal
else if (UNLIKELY (numtype & IS_NUMBER_INFINITY)) 2 14 16
2972 {
2973 2 is_neg = (numtype & IS_NUMBER_NEG);
2974 2 ▶ if (is_neg)
Branch: 1 branch
BranchTrueFalseTotal
if (is_neg) 1 1 2
2975 {
2976 1 iv = IV_MIN;
2977 1 uv = (UV)iv;
2978 }
2979 else
2980 {
2981 1 uv = UV_MAX;
2982 1 iv = (IV)uv;
2983 }
2984 }
2985  
2986 59 ▶ if ((numtype & (IS_NUMBER_GREATER_THAN_UV_MAX|IS_NUMBER_NOT_INT))
Branch: 1 branch
BranchTrueFalseTotal
if ((numtype & (IS_NUMBER_GREATER_THAN_UV_MAX|IS_NUMBER_NOT_INT)) 21 38 59
2987 21 ▶ && !(numtype & (IS_NUMBER_NAN|IS_NUMBER_INFINITY))
Branch: 1 branch
BranchTrueFalseTotal
&& !(numtype & (IS_NUMBER_NAN|IS_NUMBER_INFINITY)) 17 4 21
2988 17 ▶ && (enc->json.flags & F_ALLOW_BIGNUM))
Branch: 1 branch
BranchTrueFalseTotal
&& (enc->json.flags & F_ALLOW_BIGNUM)) 12 5 17
2989 {
2990 STRLEN len;
2991 char *str;
2992 SV *pv;
2993 SV *errsv;
2994  
2995 12 ▶ if (numtype & IS_NUMBER_NOT_INT)
Branch: 1 branch
BranchTrueFalseTotal
if (numtype & IS_NUMBER_NOT_INT) 6 6 12
2996 6 pv = newSVpvs ("my $obj; require Math::BigFloat && ($obj = Math::BigFloat->new(\"");
2997 else
2998 6 pv = newSVpvs ("require Math::BigInt && return Math::BigInt->new(\"");
2999  
3000 12 sv_catpvn (pv, SvPVX (sv), SvCUR (sv));
3001  
3002 12 ▶ if (numtype & IS_NUMBER_NOT_INT)
Branch: 1 branch
BranchTrueFalseTotal
if (numtype & IS_NUMBER_NOT_INT) 6 6 12
3003 /* This bceil/bfloor logic can be replaced by just one "bint" method call
3004 * but it is not supported by older Math::BigFloat versions.
3005 * Older Math::BigFloat versions have also "as_number" method which should
3006 * do same thing as "bint" method but it is broken and loose precision.
3007 * This bceil/bfloor logic needs Math::BigFloat 1.16 which is in Perl 5.8.0. */
3008 6 sv_catpvs (pv, "\")) && ($obj->is_negative ? $obj->bceil : $obj->bfloor);");
3009 else
3010 6 sv_catpvs (pv, "\");");
3011  
3012 12 eval_sv (pv, G_SCALAR);
3013 12 SvREFCNT_dec (pv);
3014  
3015 12 ▶ if (!json_validate (&enc->json))
Branch: 1 branch
BranchTrueFalseTotal
if (!json_validate (&enc->json)) 0 12 12
3016 0 croak (NULL);
3017
3018 /* rethrow current error */
3019 12 ▶ errsv = ERRSV;
Branch: 1 branch
BranchTrueFalseTotal
errsv = ERRSV; 12 0 12
3020 12 ▶ if (SvROK (errsv))
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK (errsv)) 0 12 12
3021 0 croak (NULL);
3022 12 ▶ else if (SvTRUE (errsv))
Branch: 1 branch
BranchTrueFalseTotal
else if (SvTRUE (errsv)) 0 12 12
3023 0 croak ("%" SVf, SVfARG (errsv));
3024  
3025 {
3026 12 dSP;
3027 12 pv = POPs;
3028 12 PUTBACK;
3029 }
3030  
3031 12 str = SvPV (pv, len);
3032 12 ▶ if (UNLIKELY (str[0] == '+'))
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY (str[0] == '+')) 0 12 12
3033 {
3034 0 str++;
3035 0 len--;
3036 }
3037 12 need (aTHX_ enc, len+1);
3038 12 savecur = enc->cur;
3039 12 saveend = enc->end;
3040 12 memcpy (enc->cur, str, len);
3041 12 enc->cur += len;
3042 12 *enc->cur = '\0';
3043  
3044 12 return;
3045 }
3046 47 ▶ else if (!(numtype & (IS_NUMBER_IN_UV|IS_NUMBER_INFINITY|IS_NUMBER_NAN)))
Branch: 1 branch
BranchTrueFalseTotal
else if (!(numtype & (IS_NUMBER_IN_UV|IS_NUMBER_INFINITY|IS_NUMBER_NAN))) 5 42 47
3047 {
3048 5 sv_to_ivuv (aTHX_ sv, &is_neg, &iv, &uv);
3049 }
3050 }
3051 else
3052 {
3053 #if PERL_VERSION < 8
3054 /* SvIV() and SvUV() in Perl 5.6 does not handle Inf and NaN in NV slot */
3055 if (SvNOKp (sv) && UNLIKELY (isinf (SvNVX (sv))))
3056 {
3057 if (SvNVX (sv) < 0)
3058 {
3059 is_neg = 1;
3060 iv = IV_MIN;
3061 uv = (UV)iv;
3062 }
3063 else
3064 {
3065 uv = UV_MAX;
3066 iv = (IV)uv;
3067 }
3068 }
3069 else if (!SvNOKp (sv) || LIKELY (!isnan (SvNVX (sv))))
3070 #endif
3071 27 sv_to_ivuv (aTHX_ sv, &is_neg, &iv, &uv);
3072 }
3073 60787 ▶ if (is_neg ? iv <= 59000 && iv >= -59000
Branch: 4 branches
BranchTrueFalseTotal
if (is_neg ? iv <= 59000 && iv >= -59000 60742 45 60787
if (is_neg ? iv <= 59000 && iv >= -59000 57881 2861 60742
if (is_neg ? iv <= 59000 && iv >= -59000 57871 10 57881
if (is_neg ? iv <= 59000 && iv >= -59000 57900 2887 60787
3074 : uv <= 59000)
3075 {
3076 /* optimise the "small number case" */
3077 /* code will likely be branchless and use only a single multiplication */
3078 /* works for numbers up to 59074 */
3079 57900 I32 i = iv;
3080 U32 u;
3081 57900 char digit, nz = 0;
3082  
3083 57900 need (aTHX_ enc, 6);
3084 57900 savecur = enc->cur;
3085 57900 saveend = enc->end;
3086  
3087 57900 *enc->cur = '-'; enc->cur += i < 0 ? 1 : 0;
3088 57900 u = i < 0 ? -i : i;
3089  
3090 /* convert to 4.28 fixed-point representation */
3091 57900 u = u * ((0xfffffff + 10000) / 10000); /* 10**5, 5 fractional digits */
3092  
3093 /* now output digit by digit, each time masking out the integer part */
3094 /* and multiplying by 5 while moving the decimal point one to the right, */
3095 /* resulting in a net multiplication by 10. */
3096 /* we always write the digit to memory but conditionally increment */
3097 /* the pointer, to enable the use of conditional move instructions. */
3098 57900 ▶ digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffffUL) * 5;
Branch: 2 branches
BranchTrueFalseTotal
digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffffUL) * 5; 57900 0 57900
digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffffUL) * 5; 7 57893 57900
3099 57900 ▶ digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffffUL) * 5;
Branch: 2 branches
BranchTrueFalseTotal
digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffffUL) * 5; 57893 7 57900
digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffffUL) * 5; 14975 42918 57893
3100 57900 ▶ digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffffUL) * 5;
Branch: 2 branches
BranchTrueFalseTotal
digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffffUL) * 5; 42918 14982 57900
digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffffUL) * 5; 17504 25414 42918
3101 57900 ▶ digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffffUL) * 5;
Branch: 2 branches
BranchTrueFalseTotal
digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffffUL) * 5; 25414 32486 57900
digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffffUL) * 5; 4876 20538 25414
3102 57900 digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; /* correctly generate '0' */
3103 57900 *enc->cur = 0;
3104 }
3105 else
3106 {
3107 /* large integer: 100-digit lookup table (GH #237), much faster
3108 * than the snprintf-based formatting used before. */
3109 char ibuf[IVUV_MAXCHARS];
3110 2887 char *ibufend = ibuf + sizeof (ibuf);
3111 char *idigits;
3112 STRLEN ilen;
3113 2887 ▶ int ineg = is_neg && iv < 0;
Branch: 2 branches
BranchTrueFalseTotal
int ineg = is_neg && iv < 0; 2871 16 2887
int ineg = is_neg && iv < 0; 10 2861 2871
3114 2887 ▶ UV auv = !is_neg ? uv : ineg ? (UV)0 - (UV)iv : (UV)iv;
Branch: 2 branches
BranchTrueFalseTotal
UV auv = !is_neg ? uv : ineg ? (UV)0 - (UV)iv : (UV)iv; 2871 16 2887
UV auv = !is_neg ? uv : ineg ? (UV)0 - (UV)iv : (UV)iv; 10 2861 2871
3115  
3116 2887 need (aTHX_ enc, IVUV_MAXCHARS);
3117 2887 savecur = enc->cur;
3118 2887 saveend = enc->end;
3119  
3120 2887 idigits = uv_to_str (ibufend, auv);
3121 2887 ▶ if (ineg)
Branch: 1 branch
BranchTrueFalseTotal
if (ineg) 10 2877 2887
3122 10 *--idigits = '-';
3123  
3124 2887 ilen = ibufend - idigits;
3125 2887 memcpy (enc->cur, idigits, ilen);
3126 2887 enc->cur += ilen;
3127 2887 *enc->cur = 0;
3128 }
3129  
3130 60787 ▶ if (!force_conversion && SvPOKp (sv) && !strEQ(savecur, SvPVX (sv))) {
Branch: 3 branches
BranchTrueFalseTotal
if (!force_conversion && SvPOKp (sv) && !strEQ(savecur, SvPVX (sv))) { 60658 129 60787
if (!force_conversion && SvPOKp (sv) && !strEQ(savecur, SvPVX (sv))) { 10136 50522 60658
if (!force_conversion && SvPOKp (sv) && !strEQ(savecur, SvPVX (sv))) { 2 10134 10136
3131 2 char *str = SvPVX (sv);
3132 2 STRLEN len = SvCUR (sv);
3133 2 enc->cur = savecur;
3134 2 enc->end = saveend;
3135 2 encode_ch (aTHX_ enc, '"');
3136 2 encode_str (aTHX_ enc, str, len, SvUTF8 (sv));
3137 2 encode_ch (aTHX_ enc, '"');
3138 2 *enc->cur = 0;
3139 }
3140 }
3141 46552 ▶ else if (type == JSON_TYPE_STRING)
Branch: 1 branch
BranchTrueFalseTotal
else if (type == JSON_TYPE_STRING) 46454 98 46552
3142 {
3143 46454 ▶ if (UNLIKELY (sv == &PL_sv_yes))
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY (sv == &PL_sv_yes)) 1 46453 46454
3144 {
3145 1 encode_ch (aTHX_ enc, '"');
3146 1 encode_const_str (aTHX_ enc, "true", 4, 0);
3147 1 encode_ch (aTHX_ enc, '"');
3148 }
3149 46453 ▶ else if (UNLIKELY (sv == &PL_sv_no))
Branch: 1 branch
BranchTrueFalseTotal
else if (UNLIKELY (sv == &PL_sv_no)) 1 46452 46453
3150 {
3151 1 encode_ch (aTHX_ enc, '"');
3152 1 encode_const_str (aTHX_ enc, "false", 5, 0);
3153 1 encode_ch (aTHX_ enc, '"');
3154 }
3155 46452 ▶ else if (!UNLIKELY (SvROK(sv) && SvOBJECT (SvRV(sv))) || !encode_bool_obj (aTHX_ enc, SvRV(sv), 0, 1))
Branch: 3 branches
BranchTrueFalseTotal
else if (!UNLIKELY (SvROK(sv) && SvOBJECT (SvRV(sv))) || !encode_bool_obj (aTHX_ enc, SvRV(sv), 0, 1)) 7 46445 46452
else if (!UNLIKELY (SvROK(sv) && SvOBJECT (SvRV(sv))) || !encode_bool_obj (aTHX_ enc, SvRV(sv), 0, 1)) 7 0 7
else if (!UNLIKELY (SvROK(sv) && SvOBJECT (SvRV(sv))) || !encode_bool_obj (aTHX_ enc, SvRV(sv), 0, 1)) 0 7 7
3156 {
3157 char *str;
3158 STRLEN len;
3159 46445 ▶ if (SvPOKp (sv))
Branch: 1 branch
BranchTrueFalseTotal
if (SvPOKp (sv)) 46419 26 46445
3160 {
3161 46419 str = SvPVX (sv);
3162 46419 len = SvCUR (sv);
3163 }
3164 else
3165 {
3166 26 str = SvPV_nomg (sv, len);
3167 }
3168 46445 encode_ch (aTHX_ enc, '"');
3169 46445 encode_str (aTHX_ enc, str, len, SvUTF8 (sv));
3170 46445 encode_ch (aTHX_ enc, '"');
3171 }
3172 }
3173 98 ▶ else if (process_ref)
Branch: 1 branch
BranchTrueFalseTotal
else if (process_ref) 98 0 98
3174 98 encode_rv (aTHX_ enc, sv);
3175 0 ▶ else if (enc->json.flags & F_ALLOW_UNKNOWN)
Branch: 1 branch
BranchTrueFalseTotal
else if (enc->json.flags & F_ALLOW_UNKNOWN) 0 0 0
3176 0 encode_const_str (aTHX_ enc, "null", 4, 0);
3177 else
3178 0 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, check your input data",
3179 SvPV_nolen (sv), (unsigned int)SvFLAGS (sv));
3180 }
3181  
3182 static SV *
3183 1682 encode_json (pTHX_ SV *scalar, JSON *json, SV *typesv)
3184 {
3185 enc_t enc;
3186  
3187 1682 ▶ if (!(json->flags & F_ALLOW_NONREF) && json_nonref (aTHX_ scalar))
Branch: 2 branches
BranchTrueFalseTotal
if (!(json->flags & F_ALLOW_NONREF) && json_nonref (aTHX_ scalar)) 10 1672 1682
if (!(json->flags & F_ALLOW_NONREF) && json_nonref (aTHX_ scalar)) 1 9 10
3188 1 croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)");
3189  
3190 1681 enc.json = *json;
3191 1681 enc.fp = 0;
3192 1681 enc.orig_json = json;
3193 1681 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
3194 1681 enc.cur = SvPVX (enc.sv);
3195 1681 enc.end = SvEND (enc.sv);
3196 1681 enc.indent = 0;
3197 3362 enc.limit = enc.json.flags & F_ASCII ? 0x000080UL
3198 3240 ▶ : enc.json.flags & F_BINARY ? 0x000080UL
Branch: 1 branch
BranchTrueFalseTotal
: enc.json.flags & F_BINARY ? 0x000080UL 1559 122 1681
3199 2915 ▶ : enc.json.flags & F_LATIN1 ? 0x000100UL
Branch: 1 branch
BranchTrueFalseTotal
: enc.json.flags & F_LATIN1 ? 0x000100UL 1356 203 1559
3200 1356 ▶ : 0x110000UL;
Branch: 1 branch
BranchTrueFalseTotal
: 0x110000UL; 5 1351 1356
3201  
3202 1681 SvPOK_only (enc.sv);
3203 1681 encode_sv (aTHX_ &enc, scalar, typesv);
3204 1643 encode_nl (aTHX_ &enc);
3205  
3206 1643 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
3207 1643 *SvEND (enc.sv) = 0; /* many xs functions expect a trailing 0 for text strings */
3208  
3209 1643 ▶ if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_BINARY | F_UTF8)))
Branch: 1 branch
BranchTrueFalseTotal
if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_BINARY | F_UTF8))) 704 939 1643
3210 704 SvUTF8_on (enc.sv);
3211  
3212 1643 ▶ if (enc.json.flags & F_SHRINK)
Branch: 1 branch
BranchTrueFalseTotal
if (enc.json.flags & F_SHRINK) 200 1443 1643
3213 200 shrink (aTHX_ enc.sv);
3214  
3215 1643 return enc.sv;
3216 }
3217  
3218 /* like encode_json, but flushes the (bounded) buffer to a filehandle as it
3219 * fills up instead of growing a single scalar to hold the whole result.
3220 * Returns the total number of bytes (octets) written. */
3221 static UV
3222 7 encode_json_to (pTHX_ SV *scalar, JSON *json, SV *typesv, PerlIO *fp)
3223 {
3224 enc_t enc;
3225 STRLEN pending;
3226  
3227 7 ▶ if (!(json->flags & F_ALLOW_NONREF) && json_nonref (aTHX_ scalar))
Branch: 2 branches
BranchTrueFalseTotal
if (!(json->flags & F_ALLOW_NONREF) && json_nonref (aTHX_ scalar)) 1 6 7
if (!(json->flags & F_ALLOW_NONREF) && json_nonref (aTHX_ scalar)) 1 0 1
3228 1 croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)");
3229  
3230 6 enc.json = *json;
3231 6 enc.fp = fp;
3232 6 enc.written = 0;
3233 6 enc.orig_json = json;
3234 6 enc.sv = sv_2mortal (NEWSV (0, STREAM_BUFSIZE));
3235 6 enc.cur = SvPVX (enc.sv);
3236 6 enc.end = SvPVX (enc.sv) + SvLEN (enc.sv) - 1;
3237 6 enc.indent = 0;
3238 12 enc.limit = enc.json.flags & F_ASCII ? 0x000080UL
3239 12 ▶ : enc.json.flags & F_BINARY ? 0x000080UL
Branch: 1 branch
BranchTrueFalseTotal
: enc.json.flags & F_BINARY ? 0x000080UL 6 0 6
3240 12 ▶ : enc.json.flags & F_LATIN1 ? 0x000100UL
Branch: 1 branch
BranchTrueFalseTotal
: enc.json.flags & F_LATIN1 ? 0x000100UL 6 0 6
3241 6 ▶ : 0x110000UL;
Branch: 1 branch
BranchTrueFalseTotal
: 0x110000UL; 0 6 6
3242  
3243 6 SvPOK_only (enc.sv);
3244 6 encode_sv (aTHX_ &enc, scalar, typesv);
3245 6 encode_nl (aTHX_ &enc);
3246  
3247 6 pending = enc.cur - (char *)SvPVX (enc.sv);
3248 6 ▶ if (pending)
Branch: 1 branch
BranchTrueFalseTotal
if (pending) 6 0 6
3249 {
3250 6 ▶ if (PerlIO_write (enc.fp, SvPVX (enc.sv), pending) != (SSize_t)pending)
Branch: 1 branch
BranchTrueFalseTotal
if (PerlIO_write (enc.fp, SvPVX (enc.sv), pending) != (SSize_t)pending) 1 5 6
3251 1 croak ("Cpanel::JSON::XS::encode_to: error writing to filehandle: %s",
3252 Strerror (errno));
3253 5 enc.written += pending;
3254 }
3255  
3256 5 ▶ if (PerlIO_error (enc.fp))
Branch: 1 branch
BranchTrueFalseTotal
if (PerlIO_error (enc.fp)) 0 5 5
3257 0 croak ("Cpanel::JSON::XS::encode_to: error writing to filehandle: %s",
3258 Strerror (errno));
3259  
3260 5 return enc.written;
3261 }
3262  
3263 /*/////////////////////////////////////////////////////////////////////////// */
3264 /* decoder */
3265  
3266 /* structure used for decoding JSON */
3267 typedef struct
3268 {
3269 char *cur; /* current parser pointer */
3270 char *end; /* end of input string */
3271 const char *err; /* parse error, if != 0 */
3272 JSON json;
3273 U32 depth; /* recursion depth */
3274 U32 maxdepth; /* recursion depth limit */
3275 char utf8_checked; /* cjson_utf8_validate ran on the rest of the buffer */
3276 char utf8_trusted; /* ... and the rest of the buffer is valid UTF-8 */
3277 char *utf8_end; /* end of what that validation covers */
3278 } dec_t;
3279  
3280  
3281  
3282 INLINE void
3283 10 decode_comment (dec_t *dec)
3284 {
3285 /* only '#'-style comments allowed a.t.m. */
3286 53 ▶ while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d && dec->cur < dec->end)
Branch: 4 branches
BranchTrueFalseTotal
while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d && dec->cur < dec->end) 52 1 53
while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d && dec->cur < dec->end) 43 9 52
while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d && dec->cur < dec->end) 43 0 43
while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d && dec->cur < dec->end) 43 0 43
3287 43 ++dec->cur;
3288 10 }
3289  
3290 INLINE void
3291 316227 decode_ws (dec_t *dec)
3292 {
3293 316227 ▶ if (dec->cur >= dec->end)
Branch: 1 branch
BranchTrueFalseTotal
if (dec->cur >= dec->end) 924 315303 316227
3294 924 return;
3295 for (;;)
3296 2424 {
3297 317727 char ch = *dec->cur;
3298  
3299 317727 ▶ if (ch > 0x20)
Branch: 1 branch
BranchTrueFalseTotal
if (ch > 0x20) 315223 2504 317727
3300 {
3301 315223 ▶ if (UNLIKELY(ch == '#'))
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY(ch == '#')) 12 315211 315223
3302 {
3303 12 ▶ if (dec->json.flags & F_RELAXED)
Branch: 1 branch
BranchTrueFalseTotal
if (dec->json.flags & F_RELAXED) 10 2 12
3304 10 decode_comment (dec);
3305 else
3306 2 break;
3307 }
3308 else
3309 315211 break;
3310 }
3311 2504 ▶ else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)
Branch: 4 branches
BranchTrueFalseTotal
else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09) 208 2296 2504
else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09) 92 116 208
else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09) 92 0 92
else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09) 90 2 92
3312 90 break; /* parse error, but let higher level handle it, gives better error messages */
3313  
3314 2424 ▶ if (dec->cur < dec->end)
Branch: 1 branch
BranchTrueFalseTotal
if (dec->cur < dec->end) 2423 1 2424
3315 2423 ++dec->cur;
3316 }
3317 }
3318  
3319 #define ERR(reason) SB dec->err = reason; goto fail; SE
3320  
3321 #define EXPECT_CH(ch) SB \
3322 if (*dec->cur != ch) \
3323 ERR (# ch " expected"); \
3324 ++dec->cur; \
3325 SE
3326  
3327 #define DEC_INC_DEPTH if (++dec->depth > dec->json.max_depth) ERR (ERR_NESTING_EXCEEDED)
3328 #define DEC_DEC_DEPTH --dec->depth
3329  
3330 static SV *decode_sv (pTHX_ dec_t *dec, SV *typesv);
3331  
3332 /* #regen code
3333 my $i;
3334 for ($i = 0; $i < 256; ++$i){
3335 print
3336 " $i >= '0' && $i <= '9' ? $i - '0' : $i >= 'a' && $i <= 'f' ? $i - 'a' + 10
3337 : $i >= 'A' && $i <= 'F' ? $i - 'A' + 10 : -1 ,
3338 ";
3339 }
3340 */
3341 static const signed char decode_hexdigit[256] = {
3342 0 >= '0' && 0 <= '9' ? 0 - '0' : 0 >= 'a' && 0 <= 'f' ? 0 - 'a' + 10
3343 : 0 >= 'A' && 0 <= 'F' ? 0 - 'A' + 10 : -1 ,
3344 1 >= '0' && 1 <= '9' ? 1 - '0' : 1 >= 'a' && 1 <= 'f' ? 1 - 'a' + 10
3345 : 1 >= 'A' && 1 <= 'F' ? 1 - 'A' + 10 : -1 ,
3346 2 >= '0' && 2 <= '9' ? 2 - '0' : 2 >= 'a' && 2 <= 'f' ? 2 - 'a' + 10
3347 : 2 >= 'A' && 2 <= 'F' ? 2 - 'A' + 10 : -1 ,
3348 3 >= '0' && 3 <= '9' ? 3 - '0' : 3 >= 'a' && 3 <= 'f' ? 3 - 'a' + 10
3349 : 3 >= 'A' && 3 <= 'F' ? 3 - 'A' + 10 : -1 ,
3350 4 >= '0' && 4 <= '9' ? 4 - '0' : 4 >= 'a' && 4 <= 'f' ? 4 - 'a' + 10
3351 : 4 >= 'A' && 4 <= 'F' ? 4 - 'A' + 10 : -1 ,
3352 5 >= '0' && 5 <= '9' ? 5 - '0' : 5 >= 'a' && 5 <= 'f' ? 5 - 'a' + 10
3353 : 5 >= 'A' && 5 <= 'F' ? 5 - 'A' + 10 : -1 ,
3354 6 >= '0' && 6 <= '9' ? 6 - '0' : 6 >= 'a' && 6 <= 'f' ? 6 - 'a' + 10
3355 : 6 >= 'A' && 6 <= 'F' ? 6 - 'A' + 10 : -1 ,
3356 7 >= '0' && 7 <= '9' ? 7 - '0' : 7 >= 'a' && 7 <= 'f' ? 7 - 'a' + 10
3357 : 7 >= 'A' && 7 <= 'F' ? 7 - 'A' + 10 : -1 ,
3358 8 >= '0' && 8 <= '9' ? 8 - '0' : 8 >= 'a' && 8 <= 'f' ? 8 - 'a' + 10
3359 : 8 >= 'A' && 8 <= 'F' ? 8 - 'A' + 10 : -1 ,
3360 9 >= '0' && 9 <= '9' ? 9 - '0' : 9 >= 'a' && 9 <= 'f' ? 9 - 'a' + 10
3361 : 9 >= 'A' && 9 <= 'F' ? 9 - 'A' + 10 : -1 ,
3362 10 >= '0' && 10 <= '9' ? 10 - '0' : 10 >= 'a' && 10 <= 'f' ? 10 - 'a' + 10
3363 : 10 >= 'A' && 10 <= 'F' ? 10 - 'A' + 10 : -1 ,
3364 11 >= '0' && 11 <= '9' ? 11 - '0' : 11 >= 'a' && 11 <= 'f' ? 11 - 'a' + 10
3365 : 11 >= 'A' && 11 <= 'F' ? 11 - 'A' + 10 : -1 ,
3366 12 >= '0' && 12 <= '9' ? 12 - '0' : 12 >= 'a' && 12 <= 'f' ? 12 - 'a' + 10
3367 : 12 >= 'A' && 12 <= 'F' ? 12 - 'A' + 10 : -1 ,
3368 13 >= '0' && 13 <= '9' ? 13 - '0' : 13 >= 'a' && 13 <= 'f' ? 13 - 'a' + 10
3369 : 13 >= 'A' && 13 <= 'F' ? 13 - 'A' + 10 : -1 ,
3370 14 >= '0' && 14 <= '9' ? 14 - '0' : 14 >= 'a' && 14 <= 'f' ? 14 - 'a' + 10
3371 : 14 >= 'A' && 14 <= 'F' ? 14 - 'A' + 10 : -1 ,
3372 15 >= '0' && 15 <= '9' ? 15 - '0' : 15 >= 'a' && 15 <= 'f' ? 15 - 'a' + 10
3373 : 15 >= 'A' && 15 <= 'F' ? 15 - 'A' + 10 : -1 ,
3374 16 >= '0' && 16 <= '9' ? 16 - '0' : 16 >= 'a' && 16 <= 'f' ? 16 - 'a' + 10
3375 : 16 >= 'A' && 16 <= 'F' ? 16 - 'A' + 10 : -1 ,
3376 17 >= '0' && 17 <= '9' ? 17 - '0' : 17 >= 'a' && 17 <= 'f' ? 17 - 'a' + 10
3377 : 17 >= 'A' && 17 <= 'F' ? 17 - 'A' + 10 : -1 ,
3378 18 >= '0' && 18 <= '9' ? 18 - '0' : 18 >= 'a' && 18 <= 'f' ? 18 - 'a' + 10
3379 : 18 >= 'A' && 18 <= 'F' ? 18 - 'A' + 10 : -1 ,
3380 19 >= '0' && 19 <= '9' ? 19 - '0' : 19 >= 'a' && 19 <= 'f' ? 19 - 'a' + 10
3381 : 19 >= 'A' && 19 <= 'F' ? 19 - 'A' + 10 : -1 ,
3382 20 >= '0' && 20 <= '9' ? 20 - '0' : 20 >= 'a' && 20 <= 'f' ? 20 - 'a' + 10
3383 : 20 >= 'A' && 20 <= 'F' ? 20 - 'A' + 10 : -1 ,
3384 21 >= '0' && 21 <= '9' ? 21 - '0' : 21 >= 'a' && 21 <= 'f' ? 21 - 'a' + 10
3385 : 21 >= 'A' && 21 <= 'F' ? 21 - 'A' + 10 : -1 ,
3386 22 >= '0' && 22 <= '9' ? 22 - '0' : 22 >= 'a' && 22 <= 'f' ? 22 - 'a' + 10
3387 : 22 >= 'A' && 22 <= 'F' ? 22 - 'A' + 10 : -1 ,
3388 23 >= '0' && 23 <= '9' ? 23 - '0' : 23 >= 'a' && 23 <= 'f' ? 23 - 'a' + 10
3389 : 23 >= 'A' && 23 <= 'F' ? 23 - 'A' + 10 : -1 ,
3390 24 >= '0' && 24 <= '9' ? 24 - '0' : 24 >= 'a' && 24 <= 'f' ? 24 - 'a' + 10
3391 : 24 >= 'A' && 24 <= 'F' ? 24 - 'A' + 10 : -1 ,
3392 25 >= '0' && 25 <= '9' ? 25 - '0' : 25 >= 'a' && 25 <= 'f' ? 25 - 'a' + 10
3393 : 25 >= 'A' && 25 <= 'F' ? 25 - 'A' + 10 : -1 ,
3394 26 >= '0' && 26 <= '9' ? 26 - '0' : 26 >= 'a' && 26 <= 'f' ? 26 - 'a' + 10
3395 : 26 >= 'A' && 26 <= 'F' ? 26 - 'A' + 10 : -1 ,
3396 27 >= '0' && 27 <= '9' ? 27 - '0' : 27 >= 'a' && 27 <= 'f' ? 27 - 'a' + 10
3397 : 27 >= 'A' && 27 <= 'F' ? 27 - 'A' + 10 : -1 ,
3398 28 >= '0' && 28 <= '9' ? 28 - '0' : 28 >= 'a' && 28 <= 'f' ? 28 - 'a' + 10
3399 : 28 >= 'A' && 28 <= 'F' ? 28 - 'A' + 10 : -1 ,
3400 29 >= '0' && 29 <= '9' ? 29 - '0' : 29 >= 'a' && 29 <= 'f' ? 29 - 'a' + 10
3401 : 29 >= 'A' && 29 <= 'F' ? 29 - 'A' + 10 : -1 ,
3402 30 >= '0' && 30 <= '9' ? 30 - '0' : 30 >= 'a' && 30 <= 'f' ? 30 - 'a' + 10
3403 : 30 >= 'A' && 30 <= 'F' ? 30 - 'A' + 10 : -1 ,
3404 31 >= '0' && 31 <= '9' ? 31 - '0' : 31 >= 'a' && 31 <= 'f' ? 31 - 'a' + 10
3405 : 31 >= 'A' && 31 <= 'F' ? 31 - 'A' + 10 : -1 ,
3406 32 >= '0' && 32 <= '9' ? 32 - '0' : 32 >= 'a' && 32 <= 'f' ? 32 - 'a' + 10
3407 : 32 >= 'A' && 32 <= 'F' ? 32 - 'A' + 10 : -1 ,
3408 33 >= '0' && 33 <= '9' ? 33 - '0' : 33 >= 'a' && 33 <= 'f' ? 33 - 'a' + 10
3409 : 33 >= 'A' && 33 <= 'F' ? 33 - 'A' + 10 : -1 ,
3410 34 >= '0' && 34 <= '9' ? 34 - '0' : 34 >= 'a' && 34 <= 'f' ? 34 - 'a' + 10
3411 : 34 >= 'A' && 34 <= 'F' ? 34 - 'A' + 10 : -1 ,
3412 35 >= '0' && 35 <= '9' ? 35 - '0' : 35 >= 'a' && 35 <= 'f' ? 35 - 'a' + 10
3413 : 35 >= 'A' && 35 <= 'F' ? 35 - 'A' + 10 : -1 ,
3414 36 >= '0' && 36 <= '9' ? 36 - '0' : 36 >= 'a' && 36 <= 'f' ? 36 - 'a' + 10
3415 : 36 >= 'A' && 36 <= 'F' ? 36 - 'A' + 10 : -1 ,
3416 37 >= '0' && 37 <= '9' ? 37 - '0' : 37 >= 'a' && 37 <= 'f' ? 37 - 'a' + 10
3417 : 37 >= 'A' && 37 <= 'F' ? 37 - 'A' + 10 : -1 ,
3418 38 >= '0' && 38 <= '9' ? 38 - '0' : 38 >= 'a' && 38 <= 'f' ? 38 - 'a' + 10
3419 : 38 >= 'A' && 38 <= 'F' ? 38 - 'A' + 10 : -1 ,
3420 39 >= '0' && 39 <= '9' ? 39 - '0' : 39 >= 'a' && 39 <= 'f' ? 39 - 'a' + 10
3421 : 39 >= 'A' && 39 <= 'F' ? 39 - 'A' + 10 : -1 ,
3422 40 >= '0' && 40 <= '9' ? 40 - '0' : 40 >= 'a' && 40 <= 'f' ? 40 - 'a' + 10
3423 : 40 >= 'A' && 40 <= 'F' ? 40 - 'A' + 10 : -1 ,
3424 41 >= '0' && 41 <= '9' ? 41 - '0' : 41 >= 'a' && 41 <= 'f' ? 41 - 'a' + 10
3425 : 41 >= 'A' && 41 <= 'F' ? 41 - 'A' + 10 : -1 ,
3426 42 >= '0' && 42 <= '9' ? 42 - '0' : 42 >= 'a' && 42 <= 'f' ? 42 - 'a' + 10
3427 : 42 >= 'A' && 42 <= 'F' ? 42 - 'A' + 10 : -1 ,
3428 43 >= '0' && 43 <= '9' ? 43 - '0' : 43 >= 'a' && 43 <= 'f' ? 43 - 'a' + 10
3429 : 43 >= 'A' && 43 <= 'F' ? 43 - 'A' + 10 : -1 ,
3430 44 >= '0' && 44 <= '9' ? 44 - '0' : 44 >= 'a' && 44 <= 'f' ? 44 - 'a' + 10
3431 : 44 >= 'A' && 44 <= 'F' ? 44 - 'A' + 10 : -1 ,
3432 45 >= '0' && 45 <= '9' ? 45 - '0' : 45 >= 'a' && 45 <= 'f' ? 45 - 'a' + 10
3433 : 45 >= 'A' && 45 <= 'F' ? 45 - 'A' + 10 : -1 ,
3434 46 >= '0' && 46 <= '9' ? 46 - '0' : 46 >= 'a' && 46 <= 'f' ? 46 - 'a' + 10
3435 : 46 >= 'A' && 46 <= 'F' ? 46 - 'A' + 10 : -1 ,
3436 47 >= '0' && 47 <= '9' ? 47 - '0' : 47 >= 'a' && 47 <= 'f' ? 47 - 'a' + 10
3437 : 47 >= 'A' && 47 <= 'F' ? 47 - 'A' + 10 : -1 ,
3438 48 >= '0' && 48 <= '9' ? 48 - '0' : 48 >= 'a' && 48 <= 'f' ? 48 - 'a' + 10
3439 : 48 >= 'A' && 48 <= 'F' ? 48 - 'A' + 10 : -1 ,
3440 49 >= '0' && 49 <= '9' ? 49 - '0' : 49 >= 'a' && 49 <= 'f' ? 49 - 'a' + 10
3441 : 49 >= 'A' && 49 <= 'F' ? 49 - 'A' + 10 : -1 ,
3442 50 >= '0' && 50 <= '9' ? 50 - '0' : 50 >= 'a' && 50 <= 'f' ? 50 - 'a' + 10
3443 : 50 >= 'A' && 50 <= 'F' ? 50 - 'A' + 10 : -1 ,
3444 51 >= '0' && 51 <= '9' ? 51 - '0' : 51 >= 'a' && 51 <= 'f' ? 51 - 'a' + 10
3445 : 51 >= 'A' && 51 <= 'F' ? 51 - 'A' + 10 : -1 ,
3446 52 >= '0' && 52 <= '9' ? 52 - '0' : 52 >= 'a' && 52 <= 'f' ? 52 - 'a' + 10
3447 : 52 >= 'A' && 52 <= 'F' ? 52 - 'A' + 10 : -1 ,
3448 53 >= '0' && 53 <= '9' ? 53 - '0' : 53 >= 'a' && 53 <= 'f' ? 53 - 'a' + 10
3449 : 53 >= 'A' && 53 <= 'F' ? 53 - 'A' + 10 : -1 ,
3450 54 >= '0' && 54 <= '9' ? 54 - '0' : 54 >= 'a' && 54 <= 'f' ? 54 - 'a' + 10
3451 : 54 >= 'A' && 54 <= 'F' ? 54 - 'A' + 10 : -1 ,
3452 55 >= '0' && 55 <= '9' ? 55 - '0' : 55 >= 'a' && 55 <= 'f' ? 55 - 'a' + 10
3453 : 55 >= 'A' && 55 <= 'F' ? 55 - 'A' + 10 : -1 ,
3454 56 >= '0' && 56 <= '9' ? 56 - '0' : 56 >= 'a' && 56 <= 'f' ? 56 - 'a' + 10
3455 : 56 >= 'A' && 56 <= 'F' ? 56 - 'A' + 10 : -1 ,
3456 57 >= '0' && 57 <= '9' ? 57 - '0' : 57 >= 'a' && 57 <= 'f' ? 57 - 'a' + 10
3457 : 57 >= 'A' && 57 <= 'F' ? 57 - 'A' + 10 : -1 ,
3458 58 >= '0' && 58 <= '9' ? 58 - '0' : 58 >= 'a' && 58 <= 'f' ? 58 - 'a' + 10
3459 : 58 >= 'A' && 58 <= 'F' ? 58 - 'A' + 10 : -1 ,
3460 59 >= '0' && 59 <= '9' ? 59 - '0' : 59 >= 'a' && 59 <= 'f' ? 59 - 'a' + 10
3461 : 59 >= 'A' && 59 <= 'F' ? 59 - 'A' + 10 : -1 ,
3462 60 >= '0' && 60 <= '9' ? 60 - '0' : 60 >= 'a' && 60 <= 'f' ? 60 - 'a' + 10
3463 : 60 >= 'A' && 60 <= 'F' ? 60 - 'A' + 10 : -1 ,
3464 61 >= '0' && 61 <= '9' ? 61 - '0' : 61 >= 'a' && 61 <= 'f' ? 61 - 'a' + 10
3465 : 61 >= 'A' && 61 <= 'F' ? 61 - 'A' + 10 : -1 ,
3466 62 >= '0' && 62 <= '9' ? 62 - '0' : 62 >= 'a' && 62 <= 'f' ? 62 - 'a' + 10
3467 : 62 >= 'A' && 62 <= 'F' ? 62 - 'A' + 10 : -1 ,
3468 63 >= '0' && 63 <= '9' ? 63 - '0' : 63 >= 'a' && 63 <= 'f' ? 63 - 'a' + 10
3469 : 63 >= 'A' && 63 <= 'F' ? 63 - 'A' + 10 : -1 ,
3470 64 >= '0' && 64 <= '9' ? 64 - '0' : 64 >= 'a' && 64 <= 'f' ? 64 - 'a' + 10
3471 : 64 >= 'A' && 64 <= 'F' ? 64 - 'A' + 10 : -1 ,
3472 65 >= '0' && 65 <= '9' ? 65 - '0' : 65 >= 'a' && 65 <= 'f' ? 65 - 'a' + 10
3473 : 65 >= 'A' && 65 <= 'F' ? 65 - 'A' + 10 : -1 ,
3474 66 >= '0' && 66 <= '9' ? 66 - '0' : 66 >= 'a' && 66 <= 'f' ? 66 - 'a' + 10
3475 : 66 >= 'A' && 66 <= 'F' ? 66 - 'A' + 10 : -1 ,
3476 67 >= '0' && 67 <= '9' ? 67 - '0' : 67 >= 'a' && 67 <= 'f' ? 67 - 'a' + 10
3477 : 67 >= 'A' && 67 <= 'F' ? 67 - 'A' + 10 : -1 ,
3478 68 >= '0' && 68 <= '9' ? 68 - '0' : 68 >= 'a' && 68 <= 'f' ? 68 - 'a' + 10
3479 : 68 >= 'A' && 68 <= 'F' ? 68 - 'A' + 10 : -1 ,
3480 69 >= '0' && 69 <= '9' ? 69 - '0' : 69 >= 'a' && 69 <= 'f' ? 69 - 'a' + 10
3481 : 69 >= 'A' && 69 <= 'F' ? 69 - 'A' + 10 : -1 ,
3482 70 >= '0' && 70 <= '9' ? 70 - '0' : 70 >= 'a' && 70 <= 'f' ? 70 - 'a' + 10
3483 : 70 >= 'A' && 70 <= 'F' ? 70 - 'A' + 10 : -1 ,
3484 71 >= '0' && 71 <= '9' ? 71 - '0' : 71 >= 'a' && 71 <= 'f' ? 71 - 'a' + 10
3485 : 71 >= 'A' && 71 <= 'F' ? 71 - 'A' + 10 : -1 ,
3486 72 >= '0' && 72 <= '9' ? 72 - '0' : 72 >= 'a' && 72 <= 'f' ? 72 - 'a' + 10
3487 : 72 >= 'A' && 72 <= 'F' ? 72 - 'A' + 10 : -1 ,
3488 73 >= '0' && 73 <= '9' ? 73 - '0' : 73 >= 'a' && 73 <= 'f' ? 73 - 'a' + 10
3489 : 73 >= 'A' && 73 <= 'F' ? 73 - 'A' + 10 : -1 ,
3490 74 >= '0' && 74 <= '9' ? 74 - '0' : 74 >= 'a' && 74 <= 'f' ? 74 - 'a' + 10
3491 : 74 >= 'A' && 74 <= 'F' ? 74 - 'A' + 10 : -1 ,
3492 75 >= '0' && 75 <= '9' ? 75 - '0' : 75 >= 'a' && 75 <= 'f' ? 75 - 'a' + 10
3493 : 75 >= 'A' && 75 <= 'F' ? 75 - 'A' + 10 : -1 ,
3494 76 >= '0' && 76 <= '9' ? 76 - '0' : 76 >= 'a' && 76 <= 'f' ? 76 - 'a' + 10
3495 : 76 >= 'A' && 76 <= 'F' ? 76 - 'A' + 10 : -1 ,
3496 77 >= '0' && 77 <= '9' ? 77 - '0' : 77 >= 'a' && 77 <= 'f' ? 77 - 'a' + 10
3497 : 77 >= 'A' && 77 <= 'F' ? 77 - 'A' + 10 : -1 ,
3498 78 >= '0' && 78 <= '9' ? 78 - '0' : 78 >= 'a' && 78 <= 'f' ? 78 - 'a' + 10
3499 : 78 >= 'A' && 78 <= 'F' ? 78 - 'A' + 10 : -1 ,
3500 79 >= '0' && 79 <= '9' ? 79 - '0' : 79 >= 'a' && 79 <= 'f' ? 79 - 'a' + 10
3501 : 79 >= 'A' && 79 <= 'F' ? 79 - 'A' + 10 : -1 ,
3502 80 >= '0' && 80 <= '9' ? 80 - '0' : 80 >= 'a' && 80 <= 'f' ? 80 - 'a' + 10
3503 : 80 >= 'A' && 80 <= 'F' ? 80 - 'A' + 10 : -1 ,
3504 81 >= '0' && 81 <= '9' ? 81 - '0' : 81 >= 'a' && 81 <= 'f' ? 81 - 'a' + 10
3505 : 81 >= 'A' && 81 <= 'F' ? 81 - 'A' + 10 : -1 ,
3506 82 >= '0' && 82 <= '9' ? 82 - '0' : 82 >= 'a' && 82 <= 'f' ? 82 - 'a' + 10
3507 : 82 >= 'A' && 82 <= 'F' ? 82 - 'A' + 10 : -1 ,
3508 83 >= '0' && 83 <= '9' ? 83 - '0' : 83 >= 'a' && 83 <= 'f' ? 83 - 'a' + 10
3509 : 83 >= 'A' && 83 <= 'F' ? 83 - 'A' + 10 : -1 ,
3510 84 >= '0' && 84 <= '9' ? 84 - '0' : 84 >= 'a' && 84 <= 'f' ? 84 - 'a' + 10
3511 : 84 >= 'A' && 84 <= 'F' ? 84 - 'A' + 10 : -1 ,
3512 85 >= '0' && 85 <= '9' ? 85 - '0' : 85 >= 'a' && 85 <= 'f' ? 85 - 'a' + 10
3513 : 85 >= 'A' && 85 <= 'F' ? 85 - 'A' + 10 : -1 ,
3514 86 >= '0' && 86 <= '9' ? 86 - '0' : 86 >= 'a' && 86 <= 'f' ? 86 - 'a' + 10
3515 : 86 >= 'A' && 86 <= 'F' ? 86 - 'A' + 10 : -1 ,
3516 87 >= '0' && 87 <= '9' ? 87 - '0' : 87 >= 'a' && 87 <= 'f' ? 87 - 'a' + 10
3517 : 87 >= 'A' && 87 <= 'F' ? 87 - 'A' + 10 : -1 ,
3518 88 >= '0' && 88 <= '9' ? 88 - '0' : 88 >= 'a' && 88 <= 'f' ? 88 - 'a' + 10
3519 : 88 >= 'A' && 88 <= 'F' ? 88 - 'A' + 10 : -1 ,
3520 89 >= '0' && 89 <= '9' ? 89 - '0' : 89 >= 'a' && 89 <= 'f' ? 89 - 'a' + 10
3521 : 89 >= 'A' && 89 <= 'F' ? 89 - 'A' + 10 : -1 ,
3522 90 >= '0' && 90 <= '9' ? 90 - '0' : 90 >= 'a' && 90 <= 'f' ? 90 - 'a' + 10
3523 : 90 >= 'A' && 90 <= 'F' ? 90 - 'A' + 10 : -1 ,
3524 91 >= '0' && 91 <= '9' ? 91 - '0' : 91 >= 'a' && 91 <= 'f' ? 91 - 'a' + 10
3525 : 91 >= 'A' && 91 <= 'F' ? 91 - 'A' + 10 : -1 ,
3526 92 >= '0' && 92 <= '9' ? 92 - '0' : 92 >= 'a' && 92 <= 'f' ? 92 - 'a' + 10
3527 : 92 >= 'A' && 92 <= 'F' ? 92 - 'A' + 10 : -1 ,
3528 93 >= '0' && 93 <= '9' ? 93 - '0' : 93 >= 'a' && 93 <= 'f' ? 93 - 'a' + 10
3529 : 93 >= 'A' && 93 <= 'F' ? 93 - 'A' + 10 : -1 ,
3530 94 >= '0' && 94 <= '9' ? 94 - '0' : 94 >= 'a' && 94 <= 'f' ? 94 - 'a' + 10
3531 : 94 >= 'A' && 94 <= 'F' ? 94 - 'A' + 10 : -1 ,
3532 95 >= '0' && 95 <= '9' ? 95 - '0' : 95 >= 'a' && 95 <= 'f' ? 95 - 'a' + 10
3533 : 95 >= 'A' && 95 <= 'F' ? 95 - 'A' + 10 : -1 ,
3534 96 >= '0' && 96 <= '9' ? 96 - '0' : 96 >= 'a' && 96 <= 'f' ? 96 - 'a' + 10
3535 : 96 >= 'A' && 96 <= 'F' ? 96 - 'A' + 10 : -1 ,
3536 97 >= '0' && 97 <= '9' ? 97 - '0' : 97 >= 'a' && 97 <= 'f' ? 97 - 'a' + 10
3537 : 97 >= 'A' && 97 <= 'F' ? 97 - 'A' + 10 : -1 ,
3538 98 >= '0' && 98 <= '9' ? 98 - '0' : 98 >= 'a' && 98 <= 'f' ? 98 - 'a' + 10
3539 : 98 >= 'A' && 98 <= 'F' ? 98 - 'A' + 10 : -1 ,
3540 99 >= '0' && 99 <= '9' ? 99 - '0' : 99 >= 'a' && 99 <= 'f' ? 99 - 'a' + 10
3541 : 99 >= 'A' && 99 <= 'F' ? 99 - 'A' + 10 : -1 ,
3542 100 >= '0' && 100 <= '9' ? 100 - '0' : 100 >= 'a' && 100 <= 'f' ? 100 - 'a' + 10
3543 : 100 >= 'A' && 100 <= 'F' ? 100 - 'A' + 10 : -1 ,
3544 101 >= '0' && 101 <= '9' ? 101 - '0' : 101 >= 'a' && 101 <= 'f' ? 101 - 'a' + 10
3545 : 101 >= 'A' && 101 <= 'F' ? 101 - 'A' + 10 : -1 ,
3546 102 >= '0' && 102 <= '9' ? 102 - '0' : 102 >= 'a' && 102 <= 'f' ? 102 - 'a' + 10
3547 : 102 >= 'A' && 102 <= 'F' ? 102 - 'A' + 10 : -1 ,
3548 103 >= '0' && 103 <= '9' ? 103 - '0' : 103 >= 'a' && 103 <= 'f' ? 103 - 'a' + 10
3549 : 103 >= 'A' && 103 <= 'F' ? 103 - 'A' + 10 : -1 ,
3550 104 >= '0' && 104 <= '9' ? 104 - '0' : 104 >= 'a' && 104 <= 'f' ? 104 - 'a' + 10
3551 : 104 >= 'A' && 104 <= 'F' ? 104 - 'A' + 10 : -1 ,
3552 105 >= '0' && 105 <= '9' ? 105 - '0' : 105 >= 'a' && 105 <= 'f' ? 105 - 'a' + 10
3553 : 105 >= 'A' && 105 <= 'F' ? 105 - 'A' + 10 : -1 ,
3554 106 >= '0' && 106 <= '9' ? 106 - '0' : 106 >= 'a' && 106 <= 'f' ? 106 - 'a' + 10
3555 : 106 >= 'A' && 106 <= 'F' ? 106 - 'A' + 10 : -1 ,
3556 107 >= '0' && 107 <= '9' ? 107 - '0' : 107 >= 'a' && 107 <= 'f' ? 107 - 'a' + 10
3557 : 107 >= 'A' && 107 <= 'F' ? 107 - 'A' + 10 : -1 ,
3558 108 >= '0' && 108 <= '9' ? 108 - '0' : 108 >= 'a' && 108 <= 'f' ? 108 - 'a' + 10
3559 : 108 >= 'A' && 108 <= 'F' ? 108 - 'A' + 10 : -1 ,
3560 109 >= '0' && 109 <= '9' ? 109 - '0' : 109 >= 'a' && 109 <= 'f' ? 109 - 'a' + 10
3561 : 109 >= 'A' && 109 <= 'F' ? 109 - 'A' + 10 : -1 ,
3562 110 >= '0' && 110 <= '9' ? 110 - '0' : 110 >= 'a' && 110 <= 'f' ? 110 - 'a' + 10
3563 : 110 >= 'A' && 110 <= 'F' ? 110 - 'A' + 10 : -1 ,
3564 111 >= '0' && 111 <= '9' ? 111 - '0' : 111 >= 'a' && 111 <= 'f' ? 111 - 'a' + 10
3565 : 111 >= 'A' && 111 <= 'F' ? 111 - 'A' + 10 : -1 ,
3566 112 >= '0' && 112 <= '9' ? 112 - '0' : 112 >= 'a' && 112 <= 'f' ? 112 - 'a' + 10
3567 : 112 >= 'A' && 112 <= 'F' ? 112 - 'A' + 10 : -1 ,
3568 113 >= '0' && 113 <= '9' ? 113 - '0' : 113 >= 'a' && 113 <= 'f' ? 113 - 'a' + 10
3569 : 113 >= 'A' && 113 <= 'F' ? 113 - 'A' + 10 : -1 ,
3570 114 >= '0' && 114 <= '9' ? 114 - '0' : 114 >= 'a' && 114 <= 'f' ? 114 - 'a' + 10
3571 : 114 >= 'A' && 114 <= 'F' ? 114 - 'A' + 10 : -1 ,
3572 115 >= '0' && 115 <= '9' ? 115 - '0' : 115 >= 'a' && 115 <= 'f' ? 115 - 'a' + 10
3573 : 115 >= 'A' && 115 <= 'F' ? 115 - 'A' + 10 : -1 ,
3574 116 >= '0' && 116 <= '9' ? 116 - '0' : 116 >= 'a' && 116 <= 'f' ? 116 - 'a' + 10
3575 : 116 >= 'A' && 116 <= 'F' ? 116 - 'A' + 10 : -1 ,
3576 117 >= '0' && 117 <= '9' ? 117 - '0' : 117 >= 'a' && 117 <= 'f' ? 117 - 'a' + 10
3577 : 117 >= 'A' && 117 <= 'F' ? 117 - 'A' + 10 : -1 ,
3578 118 >= '0' && 118 <= '9' ? 118 - '0' : 118 >= 'a' && 118 <= 'f' ? 118 - 'a' + 10
3579 : 118 >= 'A' && 118 <= 'F' ? 118 - 'A' + 10 : -1 ,
3580 119 >= '0' && 119 <= '9' ? 119 - '0' : 119 >= 'a' && 119 <= 'f' ? 119 - 'a' + 10
3581 : 119 >= 'A' && 119 <= 'F' ? 119 - 'A' + 10 : -1 ,
3582 120 >= '0' && 120 <= '9' ? 120 - '0' : 120 >= 'a' && 120 <= 'f' ? 120 - 'a' + 10
3583 : 120 >= 'A' && 120 <= 'F' ? 120 - 'A' + 10 : -1 ,
3584 121 >= '0' && 121 <= '9' ? 121 - '0' : 121 >= 'a' && 121 <= 'f' ? 121 - 'a' + 10
3585 : 121 >= 'A' && 121 <= 'F' ? 121 - 'A' + 10 : -1 ,
3586 122 >= '0' && 122 <= '9' ? 122 - '0' : 122 >= 'a' && 122 <= 'f' ? 122 - 'a' + 10
3587 : 122 >= 'A' && 122 <= 'F' ? 122 - 'A' + 10 : -1 ,
3588 123 >= '0' && 123 <= '9' ? 123 - '0' : 123 >= 'a' && 123 <= 'f' ? 123 - 'a' + 10
3589 : 123 >= 'A' && 123 <= 'F' ? 123 - 'A' + 10 : -1 ,
3590 124 >= '0' && 124 <= '9' ? 124 - '0' : 124 >= 'a' && 124 <= 'f' ? 124 - 'a' + 10
3591 : 124 >= 'A' && 124 <= 'F' ? 124 - 'A' + 10 : -1 ,
3592 125 >= '0' && 125 <= '9' ? 125 - '0' : 125 >= 'a' && 125 <= 'f' ? 125 - 'a' + 10
3593 : 125 >= 'A' && 125 <= 'F' ? 125 - 'A' + 10 : -1 ,
3594 126 >= '0' && 126 <= '9' ? 126 - '0' : 126 >= 'a' && 126 <= 'f' ? 126 - 'a' + 10
3595 : 126 >= 'A' && 126 <= 'F' ? 126 - 'A' + 10 : -1 ,
3596 127 >= '0' && 127 <= '9' ? 127 - '0' : 127 >= 'a' && 127 <= 'f' ? 127 - 'a' + 10
3597 : 127 >= 'A' && 127 <= 'F' ? 127 - 'A' + 10 : -1 ,
3598 128 >= '0' && 128 <= '9' ? 128 - '0' : 128 >= 'a' && 128 <= 'f' ? 128 - 'a' + 10
3599 : 128 >= 'A' && 128 <= 'F' ? 128 - 'A' + 10 : -1 ,
3600 129 >= '0' && 129 <= '9' ? 129 - '0' : 129 >= 'a' && 129 <= 'f' ? 129 - 'a' + 10
3601 : 129 >= 'A' && 129 <= 'F' ? 129 - 'A' + 10 : -1 ,
3602 130 >= '0' && 130 <= '9' ? 130 - '0' : 130 >= 'a' && 130 <= 'f' ? 130 - 'a' + 10
3603 : 130 >= 'A' && 130 <= 'F' ? 130 - 'A' + 10 : -1 ,
3604 131 >= '0' && 131 <= '9' ? 131 - '0' : 131 >= 'a' && 131 <= 'f' ? 131 - 'a' + 10
3605 : 131 >= 'A' && 131 <= 'F' ? 131 - 'A' + 10 : -1 ,
3606 132 >= '0' && 132 <= '9' ? 132 - '0' : 132 >= 'a' && 132 <= 'f' ? 132 - 'a' + 10
3607 : 132 >= 'A' && 132 <= 'F' ? 132 - 'A' + 10 : -1 ,
3608 133 >= '0' && 133 <= '9' ? 133 - '0' : 133 >= 'a' && 133 <= 'f' ? 133 - 'a' + 10
3609 : 133 >= 'A' && 133 <= 'F' ? 133 - 'A' + 10 : -1 ,
3610 134 >= '0' && 134 <= '9' ? 134 - '0' : 134 >= 'a' && 134 <= 'f' ? 134 - 'a' + 10
3611 : 134 >= 'A' && 134 <= 'F' ? 134 - 'A' + 10 : -1 ,
3612 135 >= '0' && 135 <= '9' ? 135 - '0' : 135 >= 'a' && 135 <= 'f' ? 135 - 'a' + 10
3613 : 135 >= 'A' && 135 <= 'F' ? 135 - 'A' + 10 : -1 ,
3614 136 >= '0' && 136 <= '9' ? 136 - '0' : 136 >= 'a' && 136 <= 'f' ? 136 - 'a' + 10
3615 : 136 >= 'A' && 136 <= 'F' ? 136 - 'A' + 10 : -1 ,
3616 137 >= '0' && 137 <= '9' ? 137 - '0' : 137 >= 'a' && 137 <= 'f' ? 137 - 'a' + 10
3617 : 137 >= 'A' && 137 <= 'F' ? 137 - 'A' + 10 : -1 ,
3618 138 >= '0' && 138 <= '9' ? 138 - '0' : 138 >= 'a' && 138 <= 'f' ? 138 - 'a' + 10
3619 : 138 >= 'A' && 138 <= 'F' ? 138 - 'A' + 10 : -1 ,
3620 139 >= '0' && 139 <= '9' ? 139 - '0' : 139 >= 'a' && 139 <= 'f' ? 139 - 'a' + 10
3621 : 139 >= 'A' && 139 <= 'F' ? 139 - 'A' + 10 : -1 ,
3622 140 >= '0' && 140 <= '9' ? 140 - '0' : 140 >= 'a' && 140 <= 'f' ? 140 - 'a' + 10
3623 : 140 >= 'A' && 140 <= 'F' ? 140 - 'A' + 10 : -1 ,
3624 141 >= '0' && 141 <= '9' ? 141 - '0' : 141 >= 'a' && 141 <= 'f' ? 141 - 'a' + 10
3625 : 141 >= 'A' && 141 <= 'F' ? 141 - 'A' + 10 : -1 ,
3626 142 >= '0' && 142 <= '9' ? 142 - '0' : 142 >= 'a' && 142 <= 'f' ? 142 - 'a' + 10
3627 : 142 >= 'A' && 142 <= 'F' ? 142 - 'A' + 10 : -1 ,
3628 143 >= '0' && 143 <= '9' ? 143 - '0' : 143 >= 'a' && 143 <= 'f' ? 143 - 'a' + 10
3629 : 143 >= 'A' && 143 <= 'F' ? 143 - 'A' + 10 : -1 ,
3630 144 >= '0' && 144 <= '9' ? 144 - '0' : 144 >= 'a' && 144 <= 'f' ? 144 - 'a' + 10
3631 : 144 >= 'A' && 144 <= 'F' ? 144 - 'A' + 10 : -1 ,
3632 145 >= '0' && 145 <= '9' ? 145 - '0' : 145 >= 'a' && 145 <= 'f' ? 145 - 'a' + 10
3633 : 145 >= 'A' && 145 <= 'F' ? 145 - 'A' + 10 : -1 ,
3634 146 >= '0' && 146 <= '9' ? 146 - '0' : 146 >= 'a' && 146 <= 'f' ? 146 - 'a' + 10
3635 : 146 >= 'A' && 146 <= 'F' ? 146 - 'A' + 10 : -1 ,
3636 147 >= '0' && 147 <= '9' ? 147 - '0' : 147 >= 'a' && 147 <= 'f' ? 147 - 'a' + 10
3637 : 147 >= 'A' && 147 <= 'F' ? 147 - 'A' + 10 : -1 ,
3638 148 >= '0' && 148 <= '9' ? 148 - '0' : 148 >= 'a' && 148 <= 'f' ? 148 - 'a' + 10
3639 : 148 >= 'A' && 148 <= 'F' ? 148 - 'A' + 10 : -1 ,
3640 149 >= '0' && 149 <= '9' ? 149 - '0' : 149 >= 'a' && 149 <= 'f' ? 149 - 'a' + 10
3641 : 149 >= 'A' && 149 <= 'F' ? 149 - 'A' + 10 : -1 ,
3642 150 >= '0' && 150 <= '9' ? 150 - '0' : 150 >= 'a' && 150 <= 'f' ? 150 - 'a' + 10
3643 : 150 >= 'A' && 150 <= 'F' ? 150 - 'A' + 10 : -1 ,
3644 151 >= '0' && 151 <= '9' ? 151 - '0' : 151 >= 'a' && 151 <= 'f' ? 151 - 'a' + 10
3645 : 151 >= 'A' && 151 <= 'F' ? 151 - 'A' + 10 : -1 ,
3646 152 >= '0' && 152 <= '9' ? 152 - '0' : 152 >= 'a' && 152 <= 'f' ? 152 - 'a' + 10
3647 : 152 >= 'A' && 152 <= 'F' ? 152 - 'A' + 10 : -1 ,
3648 153 >= '0' && 153 <= '9' ? 153 - '0' : 153 >= 'a' && 153 <= 'f' ? 153 - 'a' + 10
3649 : 153 >= 'A' && 153 <= 'F' ? 153 - 'A' + 10 : -1 ,
3650 154 >= '0' && 154 <= '9' ? 154 - '0' : 154 >= 'a' && 154 <= 'f' ? 154 - 'a' + 10
3651 : 154 >= 'A' && 154 <= 'F' ? 154 - 'A' + 10 : -1 ,
3652 155 >= '0' && 155 <= '9' ? 155 - '0' : 155 >= 'a' && 155 <= 'f' ? 155 - 'a' + 10
3653 : 155 >= 'A' && 155 <= 'F' ? 155 - 'A' + 10 : -1 ,
3654 156 >= '0' && 156 <= '9' ? 156 - '0' : 156 >= 'a' && 156 <= 'f' ? 156 - 'a' + 10
3655 : 156 >= 'A' && 156 <= 'F' ? 156 - 'A' + 10 : -1 ,
3656 157 >= '0' && 157 <= '9' ? 157 - '0' : 157 >= 'a' && 157 <= 'f' ? 157 - 'a' + 10
3657 : 157 >= 'A' && 157 <= 'F' ? 157 - 'A' + 10 : -1 ,
3658 158 >= '0' && 158 <= '9' ? 158 - '0' : 158 >= 'a' && 158 <= 'f' ? 158 - 'a' + 10
3659 : 158 >= 'A' && 158 <= 'F' ? 158 - 'A' + 10 : -1 ,
3660 159 >= '0' && 159 <= '9' ? 159 - '0' : 159 >= 'a' && 159 <= 'f' ? 159 - 'a' + 10
3661 : 159 >= 'A' && 159 <= 'F' ? 159 - 'A' + 10 : -1 ,
3662 160 >= '0' && 160 <= '9' ? 160 - '0' : 160 >= 'a' && 160 <= 'f' ? 160 - 'a' + 10
3663 : 160 >= 'A' && 160 <= 'F' ? 160 - 'A' + 10 : -1 ,
3664 161 >= '0' && 161 <= '9' ? 161 - '0' : 161 >= 'a' && 161 <= 'f' ? 161 - 'a' + 10
3665 : 161 >= 'A' && 161 <= 'F' ? 161 - 'A' + 10 : -1 ,
3666 162 >= '0' && 162 <= '9' ? 162 - '0' : 162 >= 'a' && 162 <= 'f' ? 162 - 'a' + 10
3667 : 162 >= 'A' && 162 <= 'F' ? 162 - 'A' + 10 : -1 ,
3668 163 >= '0' && 163 <= '9' ? 163 - '0' : 163 >= 'a' && 163 <= 'f' ? 163 - 'a' + 10
3669 : 163 >= 'A' && 163 <= 'F' ? 163 - 'A' + 10 : -1 ,
3670 164 >= '0' && 164 <= '9' ? 164 - '0' : 164 >= 'a' && 164 <= 'f' ? 164 - 'a' + 10
3671 : 164 >= 'A' && 164 <= 'F' ? 164 - 'A' + 10 : -1 ,
3672 165 >= '0' && 165 <= '9' ? 165 - '0' : 165 >= 'a' && 165 <= 'f' ? 165 - 'a' + 10
3673 : 165 >= 'A' && 165 <= 'F' ? 165 - 'A' + 10 : -1 ,
3674 166 >= '0' && 166 <= '9' ? 166 - '0' : 166 >= 'a' && 166 <= 'f' ? 166 - 'a' + 10
3675 : 166 >= 'A' && 166 <= 'F' ? 166 - 'A' + 10 : -1 ,
3676 167 >= '0' && 167 <= '9' ? 167 - '0' : 167 >= 'a' && 167 <= 'f' ? 167 - 'a' + 10
3677 : 167 >= 'A' && 167 <= 'F' ? 167 - 'A' + 10 : -1 ,
3678 168 >= '0' && 168 <= '9' ? 168 - '0' : 168 >= 'a' && 168 <= 'f' ? 168 - 'a' + 10
3679 : 168 >= 'A' && 168 <= 'F' ? 168 - 'A' + 10 : -1 ,
3680 169 >= '0' && 169 <= '9' ? 169 - '0' : 169 >= 'a' && 169 <= 'f' ? 169 - 'a' + 10
3681 : 169 >= 'A' && 169 <= 'F' ? 169 - 'A' + 10 : -1 ,
3682 170 >= '0' && 170 <= '9' ? 170 - '0' : 170 >= 'a' && 170 <= 'f' ? 170 - 'a' + 10
3683 : 170 >= 'A' && 170 <= 'F' ? 170 - 'A' + 10 : -1 ,
3684 171 >= '0' && 171 <= '9' ? 171 - '0' : 171 >= 'a' && 171 <= 'f' ? 171 - 'a' + 10
3685 : 171 >= 'A' && 171 <= 'F' ? 171 - 'A' + 10 : -1 ,
3686 172 >= '0' && 172 <= '9' ? 172 - '0' : 172 >= 'a' && 172 <= 'f' ? 172 - 'a' + 10
3687 : 172 >= 'A' && 172 <= 'F' ? 172 - 'A' + 10 : -1 ,
3688 173 >= '0' && 173 <= '9' ? 173 - '0' : 173 >= 'a' && 173 <= 'f' ? 173 - 'a' + 10
3689 : 173 >= 'A' && 173 <= 'F' ? 173 - 'A' + 10 : -1 ,
3690 174 >= '0' && 174 <= '9' ? 174 - '0' : 174 >= 'a' && 174 <= 'f' ? 174 - 'a' + 10
3691 : 174 >= 'A' && 174 <= 'F' ? 174 - 'A' + 10 : -1 ,
3692 175 >= '0' && 175 <= '9' ? 175 - '0' : 175 >= 'a' && 175 <= 'f' ? 175 - 'a' + 10
3693 : 175 >= 'A' && 175 <= 'F' ? 175 - 'A' + 10 : -1 ,
3694 176 >= '0' && 176 <= '9' ? 176 - '0' : 176 >= 'a' && 176 <= 'f' ? 176 - 'a' + 10
3695 : 176 >= 'A' && 176 <= 'F' ? 176 - 'A' + 10 : -1 ,
3696 177 >= '0' && 177 <= '9' ? 177 - '0' : 177 >= 'a' && 177 <= 'f' ? 177 - 'a' + 10
3697 : 177 >= 'A' && 177 <= 'F' ? 177 - 'A' + 10 : -1 ,
3698 178 >= '0' && 178 <= '9' ? 178 - '0' : 178 >= 'a' && 178 <= 'f' ? 178 - 'a' + 10
3699 : 178 >= 'A' && 178 <= 'F' ? 178 - 'A' + 10 : -1 ,
3700 179 >= '0' && 179 <= '9' ? 179 - '0' : 179 >= 'a' && 179 <= 'f' ? 179 - 'a' + 10
3701 : 179 >= 'A' && 179 <= 'F' ? 179 - 'A' + 10 : -1 ,
3702 180 >= '0' && 180 <= '9' ? 180 - '0' : 180 >= 'a' && 180 <= 'f' ? 180 - 'a' + 10
3703 : 180 >= 'A' && 180 <= 'F' ? 180 - 'A' + 10 : -1 ,
3704 181 >= '0' && 181 <= '9' ? 181 - '0' : 181 >= 'a' && 181 <= 'f' ? 181 - 'a' + 10
3705 : 181 >= 'A' && 181 <= 'F' ? 181 - 'A' + 10 : -1 ,
3706 182 >= '0' && 182 <= '9' ? 182 - '0' : 182 >= 'a' && 182 <= 'f' ? 182 - 'a' + 10
3707 : 182 >= 'A' && 182 <= 'F' ? 182 - 'A' + 10 : -1 ,
3708 183 >= '0' && 183 <= '9' ? 183 - '0' : 183 >= 'a' && 183 <= 'f' ? 183 - 'a' + 10
3709 : 183 >= 'A' && 183 <= 'F' ? 183 - 'A' + 10 : -1 ,
3710 184 >= '0' && 184 <= '9' ? 184 - '0' : 184 >= 'a' && 184 <= 'f' ? 184 - 'a' + 10
3711 : 184 >= 'A' && 184 <= 'F' ? 184 - 'A' + 10 : -1 ,
3712 185 >= '0' && 185 <= '9' ? 185 - '0' : 185 >= 'a' && 185 <= 'f' ? 185 - 'a' + 10
3713 : 185 >= 'A' && 185 <= 'F' ? 185 - 'A' + 10 : -1 ,
3714 186 >= '0' && 186 <= '9' ? 186 - '0' : 186 >= 'a' && 186 <= 'f' ? 186 - 'a' + 10
3715 : 186 >= 'A' && 186 <= 'F' ? 186 - 'A' + 10 : -1 ,
3716 187 >= '0' && 187 <= '9' ? 187 - '0' : 187 >= 'a' && 187 <= 'f' ? 187 - 'a' + 10
3717 : 187 >= 'A' && 187 <= 'F' ? 187 - 'A' + 10 : -1 ,
3718 188 >= '0' && 188 <= '9' ? 188 - '0' : 188 >= 'a' && 188 <= 'f' ? 188 - 'a' + 10
3719 : 188 >= 'A' && 188 <= 'F' ? 188 - 'A' + 10 : -1 ,
3720 189 >= '0' && 189 <= '9' ? 189 - '0' : 189 >= 'a' && 189 <= 'f' ? 189 - 'a' + 10
3721 : 189 >= 'A' && 189 <= 'F' ? 189 - 'A' + 10 : -1 ,
3722 190 >= '0' && 190 <= '9' ? 190 - '0' : 190 >= 'a' && 190 <= 'f' ? 190 - 'a' + 10
3723 : 190 >= 'A' && 190 <= 'F' ? 190 - 'A' + 10 : -1 ,
3724 191 >= '0' && 191 <= '9' ? 191 - '0' : 191 >= 'a' && 191 <= 'f' ? 191 - 'a' + 10
3725 : 191 >= 'A' && 191 <= 'F' ? 191 - 'A' + 10 : -1 ,
3726 192 >= '0' && 192 <= '9' ? 192 - '0' : 192 >= 'a' && 192 <= 'f' ? 192 - 'a' + 10
3727 : 192 >= 'A' && 192 <= 'F' ? 192 - 'A' + 10 : -1 ,
3728 193 >= '0' && 193 <= '9' ? 193 - '0' : 193 >= 'a' && 193 <= 'f' ? 193 - 'a' + 10
3729 : 193 >= 'A' && 193 <= 'F' ? 193 - 'A' + 10 : -1 ,
3730 194 >= '0' && 194 <= '9' ? 194 - '0' : 194 >= 'a' && 194 <= 'f' ? 194 - 'a' + 10
3731 : 194 >= 'A' && 194 <= 'F' ? 194 - 'A' + 10 : -1 ,
3732 195 >= '0' && 195 <= '9' ? 195 - '0' : 195 >= 'a' && 195 <= 'f' ? 195 - 'a' + 10
3733 : 195 >= 'A' && 195 <= 'F' ? 195 - 'A' + 10 : -1 ,
3734 196 >= '0' && 196 <= '9' ? 196 - '0' : 196 >= 'a' && 196 <= 'f' ? 196 - 'a' + 10
3735 : 196 >= 'A' && 196 <= 'F' ? 196 - 'A' + 10 : -1 ,
3736 197 >= '0' && 197 <= '9' ? 197 - '0' : 197 >= 'a' && 197 <= 'f' ? 197 - 'a' + 10
3737 : 197 >= 'A' && 197 <= 'F' ? 197 - 'A' + 10 : -1 ,
3738 198 >= '0' && 198 <= '9' ? 198 - '0' : 198 >= 'a' && 198 <= 'f' ? 198 - 'a' + 10
3739 : 198 >= 'A' && 198 <= 'F' ? 198 - 'A' + 10 : -1 ,
3740 199 >= '0' && 199 <= '9' ? 199 - '0' : 199 >= 'a' && 199 <= 'f' ? 199 - 'a' + 10
3741 : 199 >= 'A' && 199 <= 'F' ? 199 - 'A' + 10 : -1 ,
3742 200 >= '0' && 200 <= '9' ? 200 - '0' : 200 >= 'a' && 200 <= 'f' ? 200 - 'a' + 10
3743 : 200 >= 'A' && 200 <= 'F' ? 200 - 'A' + 10 : -1 ,
3744 201 >= '0' && 201 <= '9' ? 201 - '0' : 201 >= 'a' && 201 <= 'f' ? 201 - 'a' + 10
3745 : 201 >= 'A' && 201 <= 'F' ? 201 - 'A' + 10 : -1 ,
3746 202 >= '0' && 202 <= '9' ? 202 - '0' : 202 >= 'a' && 202 <= 'f' ? 202 - 'a' + 10
3747 : 202 >= 'A' && 202 <= 'F' ? 202 - 'A' + 10 : -1 ,
3748 203 >= '0' && 203 <= '9' ? 203 - '0' : 203 >= 'a' && 203 <= 'f' ? 203 - 'a' + 10
3749 : 203 >= 'A' && 203 <= 'F' ? 203 - 'A' + 10 : -1 ,
3750 204 >= '0' && 204 <= '9' ? 204 - '0' : 204 >= 'a' && 204 <= 'f' ? 204 - 'a' + 10
3751 : 204 >= 'A' && 204 <= 'F' ? 204 - 'A' + 10 : -1 ,
3752 205 >= '0' && 205 <= '9' ? 205 - '0' : 205 >= 'a' && 205 <= 'f' ? 205 - 'a' + 10
3753 : 205 >= 'A' && 205 <= 'F' ? 205 - 'A' + 10 : -1 ,
3754 206 >= '0' && 206 <= '9' ? 206 - '0' : 206 >= 'a' && 206 <= 'f' ? 206 - 'a' + 10
3755 : 206 >= 'A' && 206 <= 'F' ? 206 - 'A' + 10 : -1 ,
3756 207 >= '0' && 207 <= '9' ? 207 - '0' : 207 >= 'a' && 207 <= 'f' ? 207 - 'a' + 10
3757 : 207 >= 'A' && 207 <= 'F' ? 207 - 'A' + 10 : -1 ,
3758 208 >= '0' && 208 <= '9' ? 208 - '0' : 208 >= 'a' && 208 <= 'f' ? 208 - 'a' + 10
3759 : 208 >= 'A' && 208 <= 'F' ? 208 - 'A' + 10 : -1 ,
3760 209 >= '0' && 209 <= '9' ? 209 - '0' : 209 >= 'a' && 209 <= 'f' ? 209 - 'a' + 10
3761 : 209 >= 'A' && 209 <= 'F' ? 209 - 'A' + 10 : -1 ,
3762 210 >= '0' && 210 <= '9' ? 210 - '0' : 210 >= 'a' && 210 <= 'f' ? 210 - 'a' + 10
3763 : 210 >= 'A' && 210 <= 'F' ? 210 - 'A' + 10 : -1 ,
3764 211 >= '0' && 211 <= '9' ? 211 - '0' : 211 >= 'a' && 211 <= 'f' ? 211 - 'a' + 10
3765 : 211 >= 'A' && 211 <= 'F' ? 211 - 'A' + 10 : -1 ,
3766 212 >= '0' && 212 <= '9' ? 212 - '0' : 212 >= 'a' && 212 <= 'f' ? 212 - 'a' + 10
3767 : 212 >= 'A' && 212 <= 'F' ? 212 - 'A' + 10 : -1 ,
3768 213 >= '0' && 213 <= '9' ? 213 - '0' : 213 >= 'a' && 213 <= 'f' ? 213 - 'a' + 10
3769 : 213 >= 'A' && 213 <= 'F' ? 213 - 'A' + 10 : -1 ,
3770 214 >= '0' && 214 <= '9' ? 214 - '0' : 214 >= 'a' && 214 <= 'f' ? 214 - 'a' + 10
3771 : 214 >= 'A' && 214 <= 'F' ? 214 - 'A' + 10 : -1 ,
3772 215 >= '0' && 215 <= '9' ? 215 - '0' : 215 >= 'a' && 215 <= 'f' ? 215 - 'a' + 10
3773 : 215 >= 'A' && 215 <= 'F' ? 215 - 'A' + 10 : -1 ,
3774 216 >= '0' && 216 <= '9' ? 216 - '0' : 216 >= 'a' && 216 <= 'f' ? 216 - 'a' + 10
3775 : 216 >= 'A' && 216 <= 'F' ? 216 - 'A' + 10 : -1 ,
3776 217 >= '0' && 217 <= '9' ? 217 - '0' : 217 >= 'a' && 217 <= 'f' ? 217 - 'a' + 10
3777 : 217 >= 'A' && 217 <= 'F' ? 217 - 'A' + 10 : -1 ,
3778 218 >= '0' && 218 <= '9' ? 218 - '0' : 218 >= 'a' && 218 <= 'f' ? 218 - 'a' + 10
3779 : 218 >= 'A' && 218 <= 'F' ? 218 - 'A' + 10 : -1 ,
3780 219 >= '0' && 219 <= '9' ? 219 - '0' : 219 >= 'a' && 219 <= 'f' ? 219 - 'a' + 10
3781 : 219 >= 'A' && 219 <= 'F' ? 219 - 'A' + 10 : -1 ,
3782 220 >= '0' && 220 <= '9' ? 220 - '0' : 220 >= 'a' && 220 <= 'f' ? 220 - 'a' + 10
3783 : 220 >= 'A' && 220 <= 'F' ? 220 - 'A' + 10 : -1 ,
3784 221 >= '0' && 221 <= '9' ? 221 - '0' : 221 >= 'a' && 221 <= 'f' ? 221 - 'a' + 10
3785 : 221 >= 'A' && 221 <= 'F' ? 221 - 'A' + 10 : -1 ,
3786 222 >= '0' && 222 <= '9' ? 222 - '0' : 222 >= 'a' && 222 <= 'f' ? 222 - 'a' + 10
3787 : 222 >= 'A' && 222 <= 'F' ? 222 - 'A' + 10 : -1 ,
3788 223 >= '0' && 223 <= '9' ? 223 - '0' : 223 >= 'a' && 223 <= 'f' ? 223 - 'a' + 10
3789 : 223 >= 'A' && 223 <= 'F' ? 223 - 'A' + 10 : -1 ,
3790 224 >= '0' && 224 <= '9' ? 224 - '0' : 224 >= 'a' && 224 <= 'f' ? 224 - 'a' + 10
3791 : 224 >= 'A' && 224 <= 'F' ? 224 - 'A' + 10 : -1 ,
3792 225 >= '0' && 225 <= '9' ? 225 - '0' : 225 >= 'a' && 225 <= 'f' ? 225 - 'a' + 10
3793 : 225 >= 'A' && 225 <= 'F' ? 225 - 'A' + 10 : -1 ,
3794 226 >= '0' && 226 <= '9' ? 226 - '0' : 226 >= 'a' && 226 <= 'f' ? 226 - 'a' + 10
3795 : 226 >= 'A' && 226 <= 'F' ? 226 - 'A' + 10 : -1 ,
3796 227 >= '0' && 227 <= '9' ? 227 - '0' : 227 >= 'a' && 227 <= 'f' ? 227 - 'a' + 10
3797 : 227 >= 'A' && 227 <= 'F' ? 227 - 'A' + 10 : -1 ,
3798 228 >= '0' && 228 <= '9' ? 228 - '0' : 228 >= 'a' && 228 <= 'f' ? 228 - 'a' + 10
3799 : 228 >= 'A' && 228 <= 'F' ? 228 - 'A' + 10 : -1 ,
3800 229 >= '0' && 229 <= '9' ? 229 - '0' : 229 >= 'a' && 229 <= 'f' ? 229 - 'a' + 10
3801 : 229 >= 'A' && 229 <= 'F' ? 229 - 'A' + 10 : -1 ,
3802 230 >= '0' && 230 <= '9' ? 230 - '0' : 230 >= 'a' && 230 <= 'f' ? 230 - 'a' + 10
3803 : 230 >= 'A' && 230 <= 'F' ? 230 - 'A' + 10 : -1 ,
3804 231 >= '0' && 231 <= '9' ? 231 - '0' : 231 >= 'a' && 231 <= 'f' ? 231 - 'a' + 10
3805 : 231 >= 'A' && 231 <= 'F' ? 231 - 'A' + 10 : -1 ,
3806 232 >= '0' && 232 <= '9' ? 232 - '0' : 232 >= 'a' && 232 <= 'f' ? 232 - 'a' + 10
3807 : 232 >= 'A' && 232 <= 'F' ? 232 - 'A' + 10 : -1 ,
3808 233 >= '0' && 233 <= '9' ? 233 - '0' : 233 >= 'a' && 233 <= 'f' ? 233 - 'a' + 10
3809 : 233 >= 'A' && 233 <= 'F' ? 233 - 'A' + 10 : -1 ,
3810 234 >= '0' && 234 <= '9' ? 234 - '0' : 234 >= 'a' && 234 <= 'f' ? 234 - 'a' + 10
3811 : 234 >= 'A' && 234 <= 'F' ? 234 - 'A' + 10 : -1 ,
3812 235 >= '0' && 235 <= '9' ? 235 - '0' : 235 >= 'a' && 235 <= 'f' ? 235 - 'a' + 10
3813 : 235 >= 'A' && 235 <= 'F' ? 235 - 'A' + 10 : -1 ,
3814 236 >= '0' && 236 <= '9' ? 236 - '0' : 236 >= 'a' && 236 <= 'f' ? 236 - 'a' + 10
3815 : 236 >= 'A' && 236 <= 'F' ? 236 - 'A' + 10 : -1 ,
3816 237 >= '0' && 237 <= '9' ? 237 - '0' : 237 >= 'a' && 237 <= 'f' ? 237 - 'a' + 10
3817 : 237 >= 'A' && 237 <= 'F' ? 237 - 'A' + 10 : -1 ,
3818 238 >= '0' && 238 <= '9' ? 238 - '0' : 238 >= 'a' && 238 <= 'f' ? 238 - 'a' + 10
3819 : 238 >= 'A' && 238 <= 'F' ? 238 - 'A' + 10 : -1 ,
3820 239 >= '0' && 239 <= '9' ? 239 - '0' : 239 >= 'a' && 239 <= 'f' ? 239 - 'a' + 10
3821 : 239 >= 'A' && 239 <= 'F' ? 239 - 'A' + 10 : -1 ,
3822 240 >= '0' && 240 <= '9' ? 240 - '0' : 240 >= 'a' && 240 <= 'f' ? 240 - 'a' + 10
3823 : 240 >= 'A' && 240 <= 'F' ? 240 - 'A' + 10 : -1 ,
3824 241 >= '0' && 241 <= '9' ? 241 - '0' : 241 >= 'a' && 241 <= 'f' ? 241 - 'a' + 10
3825 : 241 >= 'A' && 241 <= 'F' ? 241 - 'A' + 10 : -1 ,
3826 242 >= '0' && 242 <= '9' ? 242 - '0' : 242 >= 'a' && 242 <= 'f' ? 242 - 'a' + 10
3827 : 242 >= 'A' && 242 <= 'F' ? 242 - 'A' + 10 : -1 ,
3828 243 >= '0' && 243 <= '9' ? 243 - '0' : 243 >= 'a' && 243 <= 'f' ? 243 - 'a' + 10
3829 : 243 >= 'A' && 243 <= 'F' ? 243 - 'A' + 10 : -1 ,
3830 244 >= '0' && 244 <= '9' ? 244 - '0' : 244 >= 'a' && 244 <= 'f' ? 244 - 'a' + 10
3831 : 244 >= 'A' && 244 <= 'F' ? 244 - 'A' + 10 : -1 ,
3832 245 >= '0' && 245 <= '9' ? 245 - '0' : 245 >= 'a' && 245 <= 'f' ? 245 - 'a' + 10
3833 : 245 >= 'A' && 245 <= 'F' ? 245 - 'A' + 10 : -1 ,
3834 246 >= '0' && 246 <= '9' ? 246 - '0' : 246 >= 'a' && 246 <= 'f' ? 246 - 'a' + 10
3835 : 246 >= 'A' && 246 <= 'F' ? 246 - 'A' + 10 : -1 ,
3836 247 >= '0' && 247 <= '9' ? 247 - '0' : 247 >= 'a' && 247 <= 'f' ? 247 - 'a' + 10
3837 : 247 >= 'A' && 247 <= 'F' ? 247 - 'A' + 10 : -1 ,
3838 248 >= '0' && 248 <= '9' ? 248 - '0' : 248 >= 'a' && 248 <= 'f' ? 248 - 'a' + 10
3839 : 248 >= 'A' && 248 <= 'F' ? 248 - 'A' + 10 : -1 ,
3840 249 >= '0' && 249 <= '9' ? 249 - '0' : 249 >= 'a' && 249 <= 'f' ? 249 - 'a' + 10
3841 : 249 >= 'A' && 249 <= 'F' ? 249 - 'A' + 10 : -1 ,
3842 250 >= '0' && 250 <= '9' ? 250 - '0' : 250 >= 'a' && 250 <= 'f' ? 250 - 'a' + 10
3843 : 250 >= 'A' && 250 <= 'F' ? 250 - 'A' + 10 : -1 ,
3844 251 >= '0' && 251 <= '9' ? 251 - '0' : 251 >= 'a' && 251 <= 'f' ? 251 - 'a' + 10
3845 : 251 >= 'A' && 251 <= 'F' ? 251 - 'A' + 10 : -1 ,
3846 252 >= '0' && 252 <= '9' ? 252 - '0' : 252 >= 'a' && 252 <= 'f' ? 252 - 'a' + 10
3847 : 252 >= 'A' && 252 <= 'F' ? 252 - 'A' + 10 : -1 ,
3848 253 >= '0' && 253 <= '9' ? 253 - '0' : 253 >= 'a' && 253 <= 'f' ? 253 - 'a' + 10
3849 : 253 >= 'A' && 253 <= 'F' ? 253 - 'A' + 10 : -1 ,
3850 254 >= '0' && 254 <= '9' ? 254 - '0' : 254 >= 'a' && 254 <= 'f' ? 254 - 'a' + 10
3851 : 254 >= 'A' && 254 <= 'F' ? 254 - 'A' + 10 : -1 ,
3852 255 >= '0' && 255 <= '9' ? 255 - '0' : 255 >= 'a' && 255 <= 'f' ? 255 - 'a' + 10
3853 : 255 >= 'A' && 255 <= 'F' ? 255 - 'A' + 10 : -1
3854 };
3855  
3856 static UV
3857 2137 decode_4hex (dec_t *dec)
3858 {
3859 signed char d1, d2, d3, d4;
3860 2137 unsigned char *cur = (unsigned char *)dec->cur;
3861  
3862 2137 ▶ d1 = decode_hexdigit [cur [0]]; if (UNLIKELY(d1 < 0)) ERR ("exactly four hexadecimal digits expected");
Branch: 1 branch
BranchTrueFalseTotal
d1 = decode_hexdigit [cur [0]]; if (UNLIKELY(d1 < 0)) ERR ("exactly four hexadecimal digits expected"); 3 2134 2137
3863 2134 ▶ d2 = decode_hexdigit [cur [1]]; if (UNLIKELY(d2 < 0)) ERR ("exactly four hexadecimal digits expected");
Branch: 1 branch
BranchTrueFalseTotal
d2 = decode_hexdigit [cur [1]]; if (UNLIKELY(d2 < 0)) ERR ("exactly four hexadecimal digits expected"); 2 2132 2134
3864 2132 ▶ d3 = decode_hexdigit [cur [2]]; if (UNLIKELY(d3 < 0)) ERR ("exactly four hexadecimal digits expected");
Branch: 1 branch
BranchTrueFalseTotal
d3 = decode_hexdigit [cur [2]]; if (UNLIKELY(d3 < 0)) ERR ("exactly four hexadecimal digits expected"); 1 2131 2132
3865 2131 ▶ d4 = decode_hexdigit [cur [3]]; if (UNLIKELY(d4 < 0)) ERR ("exactly four hexadecimal digits expected");
Branch: 1 branch
BranchTrueFalseTotal
d4 = decode_hexdigit [cur [3]]; if (UNLIKELY(d4 < 0)) ERR ("exactly four hexadecimal digits expected"); 1 2130 2131
3866  
3867 2130 dec->cur += 4;
3868  
3869 2130 return ((UV)d1) << 12
3870 2130 | ((UV)d2) << 8
3871 2130 | ((UV)d3) << 4
3872 2130 | ((UV)d4);
3873  
3874 7 fail:
3875 7 return (UV)-1;
3876 }
3877  
3878 static UV
3879 2509 decode_2hex (dec_t *dec)
3880 {
3881 signed char d1, d2;
3882 2509 unsigned char *cur = (unsigned char *)dec->cur;
3883  
3884 2509 ▶ d1 = decode_hexdigit [cur [0]]; if (UNLIKELY(d1 < 0)) ERR ("exactly two hexadecimal digits expected");
Branch: 1 branch
BranchTrueFalseTotal
d1 = decode_hexdigit [cur [0]]; if (UNLIKELY(d1 < 0)) ERR ("exactly two hexadecimal digits expected"); 1 2508 2509
3885 2508 ▶ d2 = decode_hexdigit [cur [1]]; if (UNLIKELY(d2 < 0)) ERR ("exactly two hexadecimal digits expected");
Branch: 1 branch
BranchTrueFalseTotal
d2 = decode_hexdigit [cur [1]]; if (UNLIKELY(d2 < 0)) ERR ("exactly two hexadecimal digits expected"); 0 2508 2508
3886 2508 dec->cur += 2;
3887 2508 return ((UV)d1) << 4
3888 2508 | ((UV)d2);
3889 1 fail:
3890 1 return (UV)-1;
3891 }
3892  
3893 static UV
3894 3 decode_3oct (dec_t *dec)
3895 {
3896 IV d1, d2, d3;
3897 3 unsigned char *cur = (unsigned char *)dec->cur;
3898  
3899 3 ▶ d1 = (IV)(cur[0] - '0'); if (d1 < 0 || d1 > 7) ERR ("exactly three octal digits expected");
Branch: 2 branches
BranchTrueFalseTotal
d1 = (IV)(cur[0] - '0'); if (d1 < 0 || d1 > 7) ERR ("exactly three octal digits expected"); 3 0 3
d1 = (IV)(cur[0] - '0'); if (d1 < 0 || d1 > 7) ERR ("exactly three octal digits expected"); 0 3 3
3900 3 ▶ d2 = (IV)(cur[1] - '0'); if (d2 < 0 || d2 > 7) ERR ("exactly three octal digits expected");
Branch: 2 branches
BranchTrueFalseTotal
d2 = (IV)(cur[1] - '0'); if (d2 < 0 || d2 > 7) ERR ("exactly three octal digits expected"); 3 0 3
d2 = (IV)(cur[1] - '0'); if (d2 < 0 || d2 > 7) ERR ("exactly three octal digits expected"); 1 2 3
3901 2 ▶ d3 = (IV)(cur[2] - '0'); if (d3 < 0 || d3 > 7) ERR ("exactly three octal digits expected");
Branch: 2 branches
BranchTrueFalseTotal
d3 = (IV)(cur[2] - '0'); if (d3 < 0 || d3 > 7) ERR ("exactly three octal digits expected"); 2 0 2
d3 = (IV)(cur[2] - '0'); if (d3 < 0 || d3 > 7) ERR ("exactly three octal digits expected"); 0 2 2
3902 2 dec->cur += 3;
3903 2 return (d1 * 64) + (d2 * 8) + d3;
3904 1 fail:
3905 1 return (UV)-1;
3906 }
3907  
3908 static SV *
3909 37380 _decode_str (pTHX_ dec_t *dec, char endstr)
3910 {
3911 37380 SV *sv = 0;
3912 37380 int utf8 = 0;
3913 37380 char *dec_cur = dec->cur;
3914 unsigned char ch;
3915 assert(endstr == 0x27 || endstr == '"');
3916  
3917 do
3918 {
3919 char buf [SHORT_STRING_LEN + UTF8_MAXBYTES];
3920 37381 char *cur = buf;
3921  
3922 do
3923 {
3924 1251859 ch = *(unsigned char *)dec_cur++;
3925  
3926 1251859 ▶ if (UNLIKELY(ch == endstr))
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY(ch == endstr)) 37252 1214607 1251859
3927 {
3928 37252 ▶ if (ch == 0x27 && !(dec->json.flags & F_ALLOW_SQUOTE)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ch == 0x27 && !(dec->json.flags & F_ALLOW_SQUOTE)) { 16 37236 37252
if (ch == 0x27 && !(dec->json.flags & F_ALLOW_SQUOTE)) { 0 16 16
3929 127 ERR("'\"' expected");
3930 }
3931 37252 --dec_cur;
3932 37252 break;
3933 }
3934 1214607 ▶ else if (UNLIKELY(ch == '\\'))
Branch: 1 branch
BranchTrueFalseTotal
else if (UNLIKELY(ch == '\\')) 9587 1205020 1214607
3935 {
3936 9587 switch (*dec_cur)
3937 {
3938 4491 case '\\':
3939 case '/':
3940 4491 case '"': *cur++ = *dec_cur++; break;
3941  
3942 62 case 'b': ++dec_cur; *cur++ = '\010'; break;
3943 158 case 't': ++dec_cur; *cur++ = '\011'; break;
3944 455 case 'n': ++dec_cur; *cur++ = '\012'; break;
3945 50 case 'f': ++dec_cur; *cur++ = '\014'; break;
3946 46 case 'r': ++dec_cur; *cur++ = '\015'; break;
3947 2 case '\'':
3948 {
3949 2 ▶ if( dec->json.flags & F_ALLOW_SQUOTE ) {
Branch: 1 branch
BranchTrueFalseTotal
if( dec->json.flags & F_ALLOW_SQUOTE ) { 2 0 2
3950 2 *cur++ = *dec_cur++;
3951 } else {
3952 0 --dec_cur;
3953 0 ERR ("illegal backslash escape sequence in string");
3954 }
3955 2 break;
3956 }
3957 2511 case 'x':
3958 {
3959 unsigned char c;
3960 2511 ▶ if (!(dec->json.flags & F_BINARY))
Branch: 1 branch
BranchTrueFalseTotal
if (!(dec->json.flags & F_BINARY)) 2 2509 2511
3961 2 ERR ("illegal hex character in non-binary string");
3962 2509 ++dec_cur;
3963 2509 dec->cur = dec_cur;
3964 2509 c = (unsigned char)decode_2hex (dec);
3965 2509 ▶ if (c == (unsigned char)((UV)-1))
Branch: 1 branch
BranchTrueFalseTotal
if (c == (unsigned char)((UV)-1)) 1 2508 2509
3966 1 goto fail;
3967 2508 *cur++ = c;
3968 2508 dec_cur += 2;
3969 2508 break;
3970 }
3971 4 case '0': case '1': case '2': case '3':
3972 case '4': case '5': case '6': case '7':
3973 {
3974 char c;
3975 4 ▶ if (!(dec->json.flags & F_BINARY))
Branch: 1 branch
BranchTrueFalseTotal
if (!(dec->json.flags & F_BINARY)) 1 3 4
3976 1 ERR ("illegal octal character in non-binary string");
3977 3 dec->cur = dec_cur;
3978 3 c = (char)decode_3oct (dec);
3979 3 ▶ if (c == (char)-1)
Branch: 1 branch
BranchTrueFalseTotal
if (c == (char)-1) 1 2 3
3980 1 goto fail;
3981 2 *cur++ = c;
3982 2 dec_cur += 3;
3983 2 break;
3984 }
3985 1798 case 'u':
3986 {
3987 UV lo, hi;
3988 1798 ++dec_cur;
3989  
3990 1798 dec->cur = dec_cur;
3991 1798 hi = decode_4hex (dec);
3992 1798 dec_cur = dec->cur;
3993 1798 ▶ if (hi == (UV)-1)
Branch: 1 branch
BranchTrueFalseTotal
if (hi == (UV)-1) 3 1795 1798
3994 3 goto fail;
3995 1795 ▶ if (dec->json.flags & F_BINARY)
Branch: 1 branch
BranchTrueFalseTotal
if (dec->json.flags & F_BINARY) 1 1794 1795
3996 1 ERR ("illegal unicode character in binary string");
3997  
3998 /* possibly a surrogate pair */
3999 1794 ▶ if (hi >= 0xd800) {
Branch: 1 branch
BranchTrueFalseTotal
if (hi >= 0xd800) { 367 1427 1794
4000 367 ▶ if (hi < 0xdc00) {
Branch: 1 branch
BranchTrueFalseTotal
if (hi < 0xdc00) { 349 18 367
4001 349 ▶ if (dec_cur [0] != '\\' || dec_cur [1] != 'u')
Branch: 2 branches
BranchTrueFalseTotal
if (dec_cur [0] != '\\' || dec_cur [1] != 'u') 342 7 349
if (dec_cur [0] != '\\' || dec_cur [1] != 'u') 3 339 342
4002 10 ERR ("missing low surrogate character in surrogate pair");
4003  
4004 339 dec_cur += 2;
4005  
4006 339 dec->cur = dec_cur;
4007 339 lo = decode_4hex (dec);
4008 339 dec_cur = dec->cur;
4009 339 ▶ if (lo == (UV)-1)
Branch: 1 branch
BranchTrueFalseTotal
if (lo == (UV)-1) 4 335 339
4010 4 goto fail;
4011  
4012 335 ▶ if (lo < 0xdc00 || lo >= 0xe000)
Branch: 2 branches
BranchTrueFalseTotal
if (lo < 0xdc00 || lo >= 0xe000) 329 6 335
if (lo < 0xdc00 || lo >= 0xe000) 0 329 329
4013 6 ERR ("surrogate pair expected");
4014  
4015 329 hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000;
4016 329 ▶ if (UNLIKELY(
Branch: 3 branches
BranchTrueFalseTotal
if (UNLIKELY( 327 2 329
if (UNLIKELY( 324 3 327
if (UNLIKELY( 0 324 324
4017 !(dec->json.flags & F_RELAXED)
4018 && (((hi & 0xfffe) == 0xfffe)
4019 || ((hi & 0xffff) == 0xffff)))) {
4020 WARNER_NONCHAR(hi);
4021 }
4022 }
4023 18 ▶ else if (UNLIKELY(hi < 0xe000)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (UNLIKELY(hi < 0xe000)) { 9 9 18
4024 9 ERR ("missing high surrogate character in surrogate pair");
4025 }
4026 else
4027  
4028 /* check 66 noncharacters U+FDD0..U+FDEF, U+FFFE, U+FFFF
4029 and U+1FFFE, U+1FFFF, U+2FFFE, U+2FFFF, ... U+10FFFE, U+10FFFF (issue #74)
4030 and warn as in core.
4031 See http://www.unicode.org/versions/corrigendum9.html.
4032  
4033 https://www.rfc-editor.org/errata_search.php?rfc=7159&eid=3984
4034 The WG's consensus was to leave the full range present
4035 in the ABNF and add the interoperability guidance about
4036 values outside the Unicode accepted range.
4037 */
4038 9 ▶ if (UNLIKELY(
Branch: 7 branches
BranchTrueFalseTotal
if (UNLIKELY( 7 2 9
if (UNLIKELY( 3 4 7
if (UNLIKELY( 1 2 3
if (UNLIKELY( 6 1 7
if (UNLIKELY( 2 4 6
if (UNLIKELY( 2 0 2
if (UNLIKELY( 2 4 6
4039 !(dec->json.flags & F_RELAXED)
4040 && ((hi >= 0xfdd0 && hi <= 0xfdef)
4041 || (hi >= 0xfffe && hi <= 0xffff)))) {
4042 WARNER_NONCHAR(hi);
4043 }
4044 }
4045 1765 ▶ if (hi >= 0x80)
Branch: 1 branch
BranchTrueFalseTotal
if (hi >= 0x80) 1088 677 1765
4046 {
4047 1088 utf8 = 1;
4048 1088 cur = (char*)encode_utf8 ((U8*)cur, hi);
4049 }
4050 else
4051 677 *cur++ = (unsigned char)hi;
4052 }
4053 1765 break;
4054  
4055 10 default:
4056 10 --dec_cur;
4057 10 ERR ("illegal backslash escape sequence in string");
4058 }
4059 }
4060 1205020 ▶ else if (LIKELY(ch >= 0x20 && ch < 0x80)) {
Branch: 2 branches
BranchTrueFalseTotal
else if (LIKELY(ch >= 0x20 && ch < 0x80)) { 1205002 18 1205020
else if (LIKELY(ch >= 0x20 && ch < 0x80)) { 1203804 1198 1205002
4061 1203804 *cur++ = ch;
4062 /* Ending ' already handled above with (ch == endstr) cid #165321 */
4063 }
4064 1216 ▶ else if (ch >= 0x80)
Branch: 1 branch
BranchTrueFalseTotal
else if (ch >= 0x80) 1198 18 1216
4065 {
4066 STRLEN clen;
4067  
4068 1198 --dec_cur;
4069  
4070 /* The first non-ascii byte of the document triggers one SIMD
4071 validation of the rest of the buffer. While that holds, a
4072 character's length follows from its lead byte alone and the
4073 per-character checks in decode_utf8() are redundant; those
4074 checks are what make 3- and 4-byte sequences call into perl's
4075 utf8n_to_uvchr(), so that is where the saving is (GH #213).
4076 utf8_ensure_checked() is out of line because it runs only
4077 once; a failed validation falls back to the old path, so
4078 relaxed mode and every error message stay unchanged. */
4079 1198 ▶ if (UNLIKELY (!dec->utf8_checked))
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY (!dec->utf8_checked)) 181 1017 1198
4080 181 utf8_ensure_checked (aTHX_ (U8*)dec_cur,
4081 181 ▶ dec->utf8_end > dec_cur ? dec->utf8_end - dec_cur : 0,
Branch: 1 branch
BranchTrueFalseTotal
dec->utf8_end > dec_cur ? dec->utf8_end - dec_cur : 0, 181 0 181
4082 &dec->utf8_checked, &dec->utf8_trusted);
4083  
4084 1198 ▶ if (LIKELY (dec->utf8_trusted && dec_cur < dec->utf8_end))
Branch: 2 branches
BranchTrueFalseTotal
if (LIKELY (dec->utf8_trusted && dec_cur < dec->utf8_end)) 1049 149 1198
if (LIKELY (dec->utf8_trusted && dec_cur < dec->utf8_end)) 1049 0 1049
4085 1049 ▶ clen = CJSON_UTF8_CLEN (ch);
Branch: 1 branch
BranchTrueFalseTotal
clen = CJSON_UTF8_CLEN (ch); 373 676 1049
4086 else
4087 149 decode_utf8 (aTHX_ (U8*)dec_cur, dec->end - dec_cur,
4088 149 dec->json.flags & F_RELAXED, &clen);
4089  
4090 1198 ▶ if (UNLIKELY (clen == (STRLEN)-1))
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY (clen == (STRLEN)-1)) 62 1136 1198
4091 62 ERR ("malformed UTF-8 character in JSON string");
4092  
4093 do
4094 2991 *cur++ = *dec_cur++;
4095 2991 ▶ while (--clen);
Branch: 1 branch
BranchTrueFalseTotal
while (--clen); 1855 1136 2991
4096  
4097 1136 utf8 = 1;
4098 }
4099 18 ▶ else if (dec->json.flags & F_RELAXED && ch == '\t') {
Branch: 2 branches
BranchTrueFalseTotal
else if (dec->json.flags & F_RELAXED && ch == '\t') { 1 17 18
else if (dec->json.flags & F_RELAXED && ch == '\t') { 1 0 1
4100 1 *cur++ = ch;
4101 } else
4102 {
4103 17 --dec_cur;
4104  
4105 17 ▶ if (!ch)
Branch: 1 branch
BranchTrueFalseTotal
if (!ch) 10 7 17
4106 10 ERR ("unexpected end of string while parsing JSON string");
4107 else
4108 7 ERR ("invalid character encountered while parsing JSON string");
4109 }
4110 }
4111 1214480 ▶ while (cur < buf + SHORT_STRING_LEN);
Branch: 1 branch
BranchTrueFalseTotal
while (cur < buf + SHORT_STRING_LEN); 1214478 2 1214480
4112  
4113 {
4114 37254 STRLEN len = cur - buf;
4115  
4116 37254 ▶ if (sv)
Branch: 1 branch
BranchTrueFalseTotal
if (sv) 1 37253 37254
4117 {
4118 1 STRLEN cur = SvCUR (sv);
4119  
4120 1 ▶ if (SvLEN (sv) <= cur + len)
Branch: 1 branch
BranchTrueFalseTotal
if (SvLEN (sv) <= cur + len) 1 0 1
4121 1 ▶ SvGROW (sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW (sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); 1 0 1
SvGROW (sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); 1 0 1
4122  
4123 1 memcpy (SvPVX (sv) + SvCUR (sv), buf, len);
4124 1 SvCUR_set (sv, SvCUR (sv) + len);
4125 }
4126 else
4127 37253 sv = newSVpvn (buf, len);
4128 }
4129 }
4130 37254 ▶ while (*dec_cur != endstr);
Branch: 1 branch
BranchTrueFalseTotal
while (*dec_cur != endstr); 1 37253 37254
4131  
4132 37253 ++dec_cur;
4133  
4134 37253 ▶ if (sv)
Branch: 1 branch
BranchTrueFalseTotal
if (sv) 37253 0 37253
4135 {
4136 37253 SvPOK_only (sv);
4137 37253 *SvEND (sv) = 0;
4138  
4139 37253 ▶ if (utf8)
Branch: 1 branch
BranchTrueFalseTotal
if (utf8) 435 36818 37253
4140 435 SvUTF8_on (sv);
4141 }
4142 else
4143 0 sv = newSVpvn ("", 0);
4144  
4145 37253 dec->cur = dec_cur;
4146 37253 return sv;
4147  
4148 127 fail:
4149 127 dec->cur = dec_cur;
4150 127 return 0;
4151 }
4152  
4153 INLINE SV *
4154 24610 decode_str (pTHX_ dec_t *dec)
4155 {
4156 24610 return _decode_str(aTHX_ dec, '"');
4157 }
4158  
4159 INLINE SV *
4160 8 decode_str_sq (pTHX_ dec_t *dec)
4161 {
4162 8 return _decode_str(aTHX_ dec, 0x27);
4163 }
4164  
4165 static SV *
4166 37953 decode_num (pTHX_ dec_t *dec, SV *typesv)
4167 {
4168 37953 int is_nv = 0;
4169 37953 char *start = dec->cur;
4170  
4171 /* [minus] */
4172 37953 ▶ if (*dec->cur == '-')
Branch: 1 branch
BranchTrueFalseTotal
if (*dec->cur == '-') 61 37892 37953
4173 61 ++dec->cur;
4174  
4175 37953 ▶ if (*dec->cur == '0')
Branch: 1 branch
BranchTrueFalseTotal
if (*dec->cur == '0') 62 37891 37953
4176 {
4177 62 ++dec->cur;
4178 62 ▶ if (*dec->cur >= '0' && *dec->cur <= '9')
Branch: 2 branches
BranchTrueFalseTotal
if (*dec->cur >= '0' && *dec->cur <= '9') 30 32 62
if (*dec->cur >= '0' && *dec->cur <= '9') 7 23 30
4179 7 ERR ("malformed number (leading zero must not be followed by another digit)");
4180 }
4181 37891 ▶ else if (*dec->cur < '0' || *dec->cur > '9')
Branch: 2 branches
BranchTrueFalseTotal
else if (*dec->cur < '0' || *dec->cur > '9') 37888 3 37891
else if (*dec->cur < '0' || *dec->cur > '9') 5 37883 37888
4182 8 ERR ("malformed number (no digits after initial minus)");
4183 else
4184 do
4185 {
4186 130212 ++dec->cur;
4187 }
4188 130212 ▶ while (*dec->cur >= '0' && *dec->cur <= '9');
Branch: 2 branches
BranchTrueFalseTotal
while (*dec->cur >= '0' && *dec->cur <= '9'); 98984 31228 130212
while (*dec->cur >= '0' && *dec->cur <= '9'); 92329 6655 98984
4189  
4190 /* [frac] */
4191 37938 ▶ if (*dec->cur == '.')
Branch: 1 branch
BranchTrueFalseTotal
if (*dec->cur == '.') 71 37867 37938
4192 {
4193 71 ++dec->cur;
4194  
4195 71 ▶ if (*dec->cur < '0' || *dec->cur > '9')
Branch: 2 branches
BranchTrueFalseTotal
if (*dec->cur < '0' || *dec->cur > '9') 69 2 71
if (*dec->cur < '0' || *dec->cur > '9') 7 62 69
4196 9 ERR ("malformed number (no digits after decimal point)");
4197  
4198 do
4199 {
4200 259 ++dec->cur;
4201 }
4202 259 ▶ while (*dec->cur >= '0' && *dec->cur <= '9');
Branch: 2 branches
BranchTrueFalseTotal
while (*dec->cur >= '0' && *dec->cur <= '9'); 232 27 259
while (*dec->cur >= '0' && *dec->cur <= '9'); 197 35 232
4203 62 ▶ if (*dec->cur == '.')
Branch: 1 branch
BranchTrueFalseTotal
if (*dec->cur == '.') 4 58 62
4204 4 ERR ("malformed number (two decimal points)");
4205  
4206 58 is_nv = 1;
4207 }
4208  
4209 /* [exp] */
4210 37925 ▶ if (*dec->cur == 'e' || *dec->cur == 'E')
Branch: 2 branches
BranchTrueFalseTotal
if (*dec->cur == 'e' || *dec->cur == 'E') 37869 56 37925
if (*dec->cur == 'e' || *dec->cur == 'E') 8 37861 37869
4211 {
4212 64 ++dec->cur;
4213  
4214 64 ▶ if (*dec->cur == '-' || *dec->cur == '+')
Branch: 2 branches
BranchTrueFalseTotal
if (*dec->cur == '-' || *dec->cur == '+') 53 11 64
if (*dec->cur == '-' || *dec->cur == '+') 23 30 53
4215 34 ++dec->cur;
4216  
4217 64 ▶ if (*dec->cur < '0' || *dec->cur > '9')
Branch: 2 branches
BranchTrueFalseTotal
if (*dec->cur < '0' || *dec->cur > '9') 60 4 64
if (*dec->cur < '0' || *dec->cur > '9') 13 47 60
4218 17 ERR ("malformed number (no digits after exp sign)");
4219  
4220 do
4221 {
4222 232 ++dec->cur;
4223 }
4224 232 ▶ while (*dec->cur >= '0' && *dec->cur <= '9');
Branch: 2 branches
BranchTrueFalseTotal
while (*dec->cur >= '0' && *dec->cur <= '9'); 210 22 232
while (*dec->cur >= '0' && *dec->cur <= '9'); 185 25 210
4225 47 ▶ if (*dec->cur == '.' && is_nv == 1)
Branch: 2 branches
BranchTrueFalseTotal
if (*dec->cur == '.' && is_nv == 1) 1 46 47
if (*dec->cur == '.' && is_nv == 1) 1 0 1
4226 1 ERR ("malformed number (two decimal points)");
4227  
4228 46 is_nv = 1;
4229 }
4230  
4231 37907 ▶ if (!is_nv)
Branch: 1 branch
BranchTrueFalseTotal
if (!is_nv) 37825 82 37907
4232 {
4233 37825 int len = dec->cur - start;
4234  
4235 37825 ▶ if (typesv)
Branch: 1 branch
BranchTrueFalseTotal
if (typesv) 10 37815 37825
4236 10 sv_setiv_mg (typesv, JSON_TYPE_INT);
4237  
4238 /* special case the rather common 1..5-digit-int case */
4239 37825 ▶ if (*start == '-')
Branch: 1 branch
BranchTrueFalseTotal
if (*start == '-') 31 37794 37825
4240 31 switch (len)
4241 {
4242 16 case 2: return newSViv (-(IV)( start [1] - '0' * 1));
4243 5 case 3: return newSViv (-(IV)( start [1] * 10 + start [2] - '0' * 11));
4244 3 case 4: return newSViv (-(IV)( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111));
4245 1 case 5: return newSViv (-(IV)( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111));
4246 1 case 6: return newSViv (-(IV)(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111));
4247 }
4248 else
4249 37794 switch (len)
4250 {
4251 8649 case 1: return newSViv ( start [0] - '0' * 1);
4252 4056 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11);
4253 15387 case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111);
4254 6872 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111);
4255 10 case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111);
4256 }
4257  
4258 {
4259 UV uv;
4260 2825 int numtype = grok_number (start, len, &uv);
4261 2825 ▶ if (numtype & IS_NUMBER_IN_UV) {
Branch: 1 branch
BranchTrueFalseTotal
if (numtype & IS_NUMBER_IN_UV) { 2819 6 2825
4262 2819 ▶ if (numtype & IS_NUMBER_NEG)
Branch: 1 branch
BranchTrueFalseTotal
if (numtype & IS_NUMBER_NEG) 3 2816 2819
4263 {
4264 3 ▶ if (uv <= (UV)(IV_MAX) + 1)
Branch: 1 branch
BranchTrueFalseTotal
if (uv <= (UV)(IV_MAX) + 1) 3 0 3
4265 2819 return newSViv (-(IV)uv);
4266 }
4267 else
4268 2816 return newSVuv (uv);
4269 }
4270 }
4271  
4272 6 len -= *start == '-' ? 1 : 0;
4273  
4274 /* does not fit into IV or UV, try NV */
4275 6 ▶ if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len)
Branch: 1 branch
BranchTrueFalseTotal
if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) 0 6 6
4276 #if defined (LDBL_DIG)
4277 || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len)
4278 #endif
4279 )
4280 /* fits into NV without loss of precision */
4281 0 return newSVnv (json_atof (start));
4282  
4283 6 ▶ if (dec->json.flags & F_ALLOW_BIGNUM) {
Branch: 1 branch
BranchTrueFalseTotal
if (dec->json.flags & F_ALLOW_BIGNUM) { 2 4 6
4284 SV *errsv;
4285 2 SV* pv = newSVpvs("require Math::BigInt && return Math::BigInt->new(\"");
4286 2 sv_catpvn(pv, start, dec->cur - start);
4287 2 sv_catpvs(pv, "\");");
4288 2 eval_sv(pv, G_SCALAR);
4289 2 SvREFCNT_dec(pv);
4290 /* rethrow current error */
4291 2 ▶ errsv = ERRSV;
Branch: 1 branch
BranchTrueFalseTotal
errsv = ERRSV; 2 0 2
4292 2 ▶ if (SvROK (errsv))
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK (errsv)) 0 2 2
4293 0 croak (NULL);
4294 2 ▶ else if (SvTRUE (errsv))
Branch: 1 branch
BranchTrueFalseTotal
else if (SvTRUE (errsv)) 0 2 2
4295 0 croak ("%" SVf, SVfARG (errsv));
4296 {
4297 2 dSP;
4298 2 SV *retval = SvREFCNT_inc(POPs);
4299 2 PUTBACK;
4300 2 return retval;
4301 }
4302 }
4303  
4304 /* everything else fails, convert it to a string */
4305 4 return newSVpvn (start, dec->cur - start);
4306 }
4307  
4308 82 ▶ if (typesv)
Branch: 1 branch
BranchTrueFalseTotal
if (typesv) 4 78 82
4309 4 sv_setiv_mg (typesv, JSON_TYPE_FLOAT);
4310  
4311 82 ▶ if (dec->json.flags & F_ALLOW_BIGNUM) {
Branch: 1 branch
BranchTrueFalseTotal
if (dec->json.flags & F_ALLOW_BIGNUM) { 5 77 82
4312 SV *errsv;
4313 5 SV* pv = newSVpvs("require Math::BigFloat && return Math::BigFloat->new(\"");
4314 5 sv_catpvn(pv, start, dec->cur - start);
4315 5 sv_catpvs(pv, "\");");
4316 5 eval_sv(pv, G_SCALAR);
4317 5 SvREFCNT_dec(pv);
4318 /* rethrow current error */
4319 5 ▶ errsv = ERRSV;
Branch: 1 branch
BranchTrueFalseTotal
errsv = ERRSV; 5 0 5
4320 5 ▶ if (SvROK (errsv))
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK (errsv)) 0 5 5
4321 0 croak (NULL);
4322 5 ▶ else if (SvTRUE (errsv))
Branch: 1 branch
BranchTrueFalseTotal
else if (SvTRUE (errsv)) 0 5 5
4323 0 croak ("%" SVf, SVfARG (errsv));
4324 {
4325 5 dSP;
4326 5 SV *retval = SvREFCNT_inc(POPs);
4327 5 PUTBACK;
4328 5 return retval;
4329 }
4330 }
4331  
4332 /* loss of precision here */
4333 77 return newSVnv (json_atof (start));
4334  
4335 46 fail:
4336 46 return 0;
4337 }
4338  
4339 static SV *
4340 18957 decode_av (pTHX_ dec_t *dec, SV *typesv)
4341 {
4342 18957 AV *av = newAV ();
4343 18957 AV *typeav = NULL;
4344 SV *typerv;
4345  
4346 18957 ▶ DEC_INC_DEPTH;
Branch: 1 branch
BranchTrueFalseTotal
DEC_INC_DEPTH; 5 18952 18957
4347 18952 decode_ws (dec);
4348  
4349 18952 ▶ if (typesv)
Branch: 1 branch
BranchTrueFalseTotal
if (typesv) 8 18944 18952
4350 {
4351 8 typeav = newAV ();
4352 8 typerv = newRV_noinc ((SV *)typeav);
4353 8 ▶ SvSetMagicSV (typesv, typerv);
Branch: 2 branches
BranchTrueFalseTotal
SvSetMagicSV (typesv, typerv); 8 0 8
SvSetMagicSV (typesv, typerv); 0 8 8
4354 /* SvSetMagicSV increments refcount of typerv, so we need to decrement it */
4355 8 SvREFCNT_dec (typerv);
4356 }
4357  
4358 18952 ▶ if (*dec->cur == ']')
Branch: 1 branch
BranchTrueFalseTotal
if (*dec->cur == ']') 30 18922 18952
4359 30 ++dec->cur;
4360 else
4361 for (;;)
4362 28077 {
4363 SV *value;
4364 46999 SV *value_typesv = NULL;
4365  
4366 46999 ▶ if (typesv)
Branch: 1 branch
BranchTrueFalseTotal
if (typesv) 19 46980 46999
4367 {
4368 19 value_typesv = newSV (0);
4369 19 av_push (typeav, value_typesv);
4370 }
4371  
4372 46999 value = decode_sv (aTHX_ dec, value_typesv);
4373 46999 ▶ if (!value)
Branch: 1 branch
BranchTrueFalseTotal
if (!value) 1510 45489 46999
4374 1510 goto fail;
4375  
4376 45489 av_push (av, value);
4377  
4378 45489 decode_ws (dec);
4379  
4380 45489 ▶ if (*dec->cur == ']')
Branch: 1 branch
BranchTrueFalseTotal
if (*dec->cur == ']') 17385 28104 45489
4381 {
4382 17385 ++dec->cur;
4383 17385 break;
4384 }
4385
4386 28104 ▶ if (*dec->cur != ',')
Branch: 1 branch
BranchTrueFalseTotal
if (*dec->cur != ',') 25 28079 28104
4387 25 ERR (", or ] expected while parsing array");
4388  
4389 28079 ++dec->cur;
4390  
4391 28079 decode_ws (dec);
4392  
4393 28079 ▶ if (*dec->cur == ']' && dec->json.flags & F_RELAXED)
Branch: 2 branches
BranchTrueFalseTotal
if (*dec->cur == ']' && dec->json.flags & F_RELAXED) 5 28074 28079
if (*dec->cur == ']' && dec->json.flags & F_RELAXED) 2 3 5
4394 {
4395 2 ++dec->cur;
4396 2 break;
4397 }
4398 }
4399  
4400 17417 DEC_DEC_DEPTH;
4401 17417 return newRV_noinc ((SV *)av);
4402  
4403 1540 fail:
4404 1540 SvREFCNT_dec (av);
4405 1540 DEC_DEC_DEPTH;
4406 1540 return 0;
4407 }
4408  
4409 static void
4410 41879 hv_store_str (pTHX_ HV* hv, char *key, U32 len, SV* value)
4411 {
4412 U32 i;
4413 #if PERL_VERSION > 8 || (PERL_VERSION == 8 && PERL_SUBVERSION >= 9)
4414 41879 int utf8 = 0;
4415 #else
4416 I32 ulen = (I32)len;
4417 #endif
4418 /* check utf8 hash key */
4419 308117 ▶ for (i=0; i<len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i=0; i<len; i++) { 266243 41874 308117
4420 266243 ▶ if ((signed char)key[i] < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((signed char)key[i] < 0) { 5 266238 266243
4421 #if PERL_VERSION > 8 || (PERL_VERSION == 8 && PERL_SUBVERSION >= 9)
4422 5 utf8 = HVhek_UTF8;
4423 #else
4424 ulen = -(I32)len;
4425 #endif
4426 5 break;
4427 }
4428 }
4429 #if PERL_VERSION > 8 || (PERL_VERSION == 8 && PERL_SUBVERSION >= 9)
4430 41879 hv_common (hv, NULL, key, len, utf8,
4431 HV_FETCH_ISSTORE|HV_FETCH_JUST_SV, value, 0);
4432 #else
4433 hv_store (hv, key, ulen, value, 0);
4434 #endif
4435 41879 }
4436  
4437 static SV *
4438 16148 decode_hv (pTHX_ dec_t *dec, SV *typesv)
4439 {
4440 SV *sv;
4441 16148 HV *hv = newHV ();
4442 16148 HV *typehv = NULL;
4443 SV *typerv;
4444 16148 int allow_squote = dec->json.flags & F_ALLOW_SQUOTE;
4445 16148 int allow_barekey = dec->json.flags & F_ALLOW_BAREKEY;
4446 16148 int allow_dupkeys = dec->json.flags & F_ALLOW_DUPKEYS;
4447 16148 int dupkeys_as_arrayref = dec->json.flags & F_DUPKEYS_AS_AREF;
4448 16148 HV *dupkeys_seen = NULL;
4449 16148 char endstr = '"';
4450  
4451 16148 ▶ DEC_INC_DEPTH;
Branch: 1 branch
BranchTrueFalseTotal
DEC_INC_DEPTH; 0 16148 16148
4452 16148 decode_ws (dec);
4453  
4454 16148 ▶ if (typesv)
Branch: 1 branch
BranchTrueFalseTotal
if (typesv) 6 16142 16148
4455 {
4456 6 typehv = newHV ();
4457 6 typerv = newRV_noinc ((SV *)typehv);
4458 6 ▶ SvSetMagicSV (typesv, typerv);
Branch: 2 branches
BranchTrueFalseTotal
SvSetMagicSV (typesv, typerv); 6 0 6
SvSetMagicSV (typesv, typerv); 0 6 6
4459 /* SvSetMagicSV increments refcount of typerv, so we need to decrement it */
4460 6 SvREFCNT_dec (typerv);
4461 }
4462  
4463 16148 ▶ if (*dec->cur == '}')
Branch: 1 branch
BranchTrueFalseTotal
if (*dec->cur == '}') 18 16130 16148
4464 18 ++dec->cur;
4465 else
4466 for (;;)
4467 39331 {
4468 55461 int is_bare = allow_barekey;
4469  
4470 55461 ▶ if (UNLIKELY(allow_barekey
Branch: 4 branches
BranchTrueFalseTotal
if (UNLIKELY(allow_barekey 34 55427 55461
if (UNLIKELY(allow_barekey 18 16 34
if (UNLIKELY(allow_barekey 18 55443 55461
if (UNLIKELY(allow_barekey 18 0 18
4471 && *dec->cur >= 'A' && *dec->cur <= 'z'))
4472 ;
4473 55443 ▶ else if (UNLIKELY(allow_squote)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (UNLIKELY(allow_squote)) { 21 55422 55443
4474 21 ▶ if (*dec->cur != '"' && *dec->cur != 0x27) {
Branch: 2 branches
BranchTrueFalseTotal
if (*dec->cur != '"' && *dec->cur != 0x27) { 9 12 21
if (*dec->cur != '"' && *dec->cur != 0x27) { 1 8 9
4475 1 ERR ("'\"' or ''' expected");
4476 }
4477 20 ▶ else if (*dec->cur == 0x27)
Branch: 1 branch
BranchTrueFalseTotal
else if (*dec->cur == 0x27) 8 12 20
4478 8 endstr = 0x27;
4479 20 is_bare = 0;
4480 20 ++dec->cur;
4481 } else {
4482 55422 ▶ EXPECT_CH ('"');
Branch: 1 branch
BranchTrueFalseTotal
EXPECT_CH ('"'); 24 55398 55422
4483 55398 is_bare = 0;
4484 }
4485  
4486 /* heuristic: assume that */
4487 /* a) decode_str + hv_store_ent are abysmally slow. */
4488 /* b) most hash keys are short, simple ascii text. */
4489 /* => try to "fast-match" such strings to avoid */
4490 /* the overhead of decode_str + hv_store_ent. */
4491 {
4492 SV *value;
4493 55436 SV *value_typesv = NULL;
4494 55436 SV *old_value = NULL;
4495 55436 char *p = dec->cur;
4496 55436 char *e = p + 24; /* only try up to 24 bytes */
4497 bool key_exists;
4498  
4499 for (;;)
4500 {
4501 /* the >= 0x80 is false on most architectures */
4502 /* !*p must be checked before !is_bare: a bare-key scan has no
4503 bounds guard and would run one byte past dec->end otherwise */
4504 622559 ▶ if (UNLIKELY(!*p || (!is_bare &&
Branch: 12 branches
BranchTrueFalseTotal
if (UNLIKELY(!*p || (!is_bare && 622557 2 622559
if (UNLIKELY(!*p || (!is_bare && 622493 64 622557
if (UNLIKELY(!*p || (!is_bare && 609961 12532 622493
if (UNLIKELY(!*p || (!is_bare && 6 609955 609961
if (UNLIKELY(!*p || (!is_bare && 609955 12538 622493
if (UNLIKELY(!*p || (!is_bare && 0 609955 609955
if (UNLIKELY(!*p || (!is_bare && 609955 12538 622493
if (UNLIKELY(!*p || (!is_bare && 203 609752 609955
if (UNLIKELY(!*p || (!is_bare && 609752 12741 622493
if (UNLIKELY(!*p || (!is_bare && 19 609733 609752
if (UNLIKELY(!*p || (!is_bare && 12760 609733 622493
if (UNLIKELY(!*p || (!is_bare && 12760 609797 622557
4505 (p == e || *p < 0x20 || *(U8*)p >= 0x80 || *p == '\\'
4506 || allow_squote))))
4507 {
4508 /* slow path, back up and use decode_str */
4509 /* utf8 hash keys are handled here */
4510 12762 SV *keysv = _decode_str (aTHX_ dec, endstr);
4511 12762 ▶ if (!keysv)
Branch: 1 branch
BranchTrueFalseTotal
if (!keysv) 5 12757 12762
4512 5 goto fail;
4513  
4514 12757 key_exists = hv_exists_ent (hv, keysv, 0);
4515 12757 ▶ if (UNLIKELY (key_exists)) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY (key_exists)) { 6 12751 12757
4516 6 ▶ if (!allow_dupkeys)
Branch: 1 branch
BranchTrueFalseTotal
if (!allow_dupkeys) 1 5 6
4517 {
4518 1 SvREFCNT_dec (keysv);
4519 1 ERR ("Duplicate keys not allowed");
4520 }
4521 5 ▶ else if (dupkeys_as_arrayref) {
Branch: 1 branch
BranchTrueFalseTotal
else if (dupkeys_as_arrayref) { 3 2 5
4522 // extend the value to arrayref or push
4523 3 old_value = HeVAL(hv_fetch_ent (hv, keysv, 0, 0));
4524 3 SvREFCNT_inc (old_value);
4525 3 ▶ if (!dupkeys_seen
Branch: 1 branch
BranchTrueFalseTotal
if (!dupkeys_seen 1 2 3
4526 1 ▶ || !hv_exists_ent (dupkeys_seen, keysv, 0))
Branch: 1 branch
BranchTrueFalseTotal
|| !hv_exists_ent (dupkeys_seen, keysv, 0)) 1 0 1
4527 {
4528 3 AV *av = newAV ();
4529 3 av_extend (av, 2);
4530 3 ▶ if (av_store (av, 0, old_value))
Branch: 1 branch
BranchTrueFalseTotal
if (av_store (av, 0, old_value)) 3 0 3
4531 3 old_value = newRV_noinc ((SV*)av);
4532 3 ▶ if (!dupkeys_seen)
Branch: 1 branch
BranchTrueFalseTotal
if (!dupkeys_seen) 2 1 3
4533 {
4534 2 dupkeys_seen = newHV ();
4535 2 sv_2mortal ((SV *)dupkeys_seen);
4536 }
4537 3 (void)hv_store_ent (dupkeys_seen, keysv,
4538 newSV (0), 0);
4539 }
4540 3 sv_2mortal (old_value); /* also freed on every error exit */
4541 } // else overwrite it below
4542 }
4543 12756 decode_ws (dec);
4544 12756 ▶ if (*dec->cur != ':')
Branch: 1 branch
BranchTrueFalseTotal
if (*dec->cur != ':') 0 12756 12756
4545 0 SvREFCNT_dec (keysv);
4546 12756 ▶ EXPECT_CH (':');
Branch: 1 branch
BranchTrueFalseTotal
EXPECT_CH (':'); 0 12756 12756
4547 12756 decode_ws (dec);
4548  
4549 12756 ▶ if (typesv)
Branch: 1 branch
BranchTrueFalseTotal
if (typesv) 0 12756 12756
4550 {
4551 0 value_typesv = newSV (0);
4552 0 (void)hv_store_ent (typehv, keysv, value_typesv, 0);
4553 }
4554  
4555 12756 value = decode_sv (aTHX_ dec, value_typesv);
4556 12756 ▶ if (!value)
Branch: 1 branch
BranchTrueFalseTotal
if (!value) 1 12755 12756
4557 {
4558 1 SvREFCNT_dec (keysv);
4559 1 goto fail;
4560 }
4561  
4562 12755 ▶ if (UNLIKELY (key_exists && dupkeys_as_arrayref && old_value))
Branch: 4 branches
BranchTrueFalseTotal
if (UNLIKELY (key_exists && dupkeys_as_arrayref && old_value)) 5 12750 12755
if (UNLIKELY (key_exists && dupkeys_as_arrayref && old_value)) 3 2 5
if (UNLIKELY (key_exists && dupkeys_as_arrayref && old_value)) 3 12752 12755
if (UNLIKELY (key_exists && dupkeys_as_arrayref && old_value)) 3 0 3
4563 {
4564 3 ▶ if (UNLIKELY (!SvROK (old_value)
Branch: 2 branches
BranchTrueFalseTotal
if (UNLIKELY (!SvROK (old_value) 3 0 3
if (UNLIKELY (!SvROK (old_value) 0 3 3
4565 || SvTYPE (SvRV (old_value)) != SVt_PVAV))
4566 {
4567 0 SvREFCNT_dec (value);
4568 0 SvREFCNT_dec (keysv);
4569 0 ERR ("Invalid dupkeys_as_arrayref hash key");
4570 }
4571 3 av_push ((AV*)SvRV (old_value), value);
4572 3 (void)hv_store_ent (hv, keysv, SvREFCNT_inc (old_value), 0);
4573 }
4574 else
4575 {
4576 12752 (void)hv_store_ent (hv, keysv, value, 0);
4577 }
4578 12755 SvREFCNT_dec (keysv);
4579  
4580 12755 break;
4581 }
4582 609797 ▶ else if (*p == endstr
Branch: 1 branch
BranchTrueFalseTotal
else if (*p == endstr 567140 42657 609797
4583 567140 ▶ || (is_bare &&
Branch: 1 branch
BranchTrueFalseTotal
|| (is_bare && 64 567076 567140
4584 64 ▶ (*p == ':' || *p == ' ' || *p == 0x0a
Branch: 3 branches
BranchTrueFalseTotal
(*p == ':' || *p == ' ' || *p == 0x0a 49 15 64
(*p == ':' || *p == ' ' || *p == 0x0a 48 1 49
(*p == ':' || *p == ' ' || *p == 0x0a 48 0 48
4585 48 ▶ || *p == 0x0d || *p == 0x09)))
Branch: 2 branches
BranchTrueFalseTotal
|| *p == 0x0d || *p == 0x09))) 48 0 48
|| *p == 0x0d || *p == 0x09))) 1 47 48
4586 {
4587 /* fast path, got a simple key */
4588 42674 char *key = dec->cur;
4589 42674 U32 len = p - key;
4590 42674 I32 klen = (I32)len;
4591 U32 i;
4592 assert(p >= key && (p - key) < I32_MAX);
4593 #if PTRSIZE >= 8
4594 /* hv_store can only handle I32 len, which might overflow */
4595 /* perl5 just silently truncates it, cperl panics */
4596 42674 ▶ if (UNLIKELY(p - key > I32_MAX))
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY(p - key > I32_MAX)) 0 42674 42674
4597 0 ERR ("Hash key too large");
4598 #endif
4599 42674 ▶ if (is_bare)
Branch: 1 branch
BranchTrueFalseTotal
if (is_bare) 17 42657 42674
4600 52 ▶ for (i = 0; i < len; )
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; ) 37 15 52
4601 {
4602 37 unsigned char c = (unsigned char)key[i];
4603 37 ▶ if (c >= 0x80)
Branch: 1 branch
BranchTrueFalseTotal
if (c >= 0x80) 8 29 37
4604 {
4605 STRLEN clen;
4606 8 decode_utf8 (aTHX_ (U8*)key + i, len - i,
4607 8 dec->json.flags & F_RELAXED, &clen);
4608 8 ▶ if (clen == (STRLEN)-1 || clen == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (clen == (STRLEN)-1 || clen == 0) 7 1 8
if (clen == (STRLEN)-1 || clen == 0) 0 7 7
4609 1 ERR ("malformed UTF-8 character in bare hash key");
4610 7 i += clen;
4611 }
4612 29 ▶ else if (c < 0x20 || c == '\\')
Branch: 2 branches
BranchTrueFalseTotal
else if (c < 0x20 || c == '\\') 28 1 29
else if (c < 0x20 || c == '\\') 0 28 28
4613 1 ERR ("invalid character in bare hash key");
4614 else
4615 28 i++;
4616 }
4617 /* negative klen: look the key up as UTF-8, as hv_store_str stores it */
4618 308999 ▶ for (i = 0; i < len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) 266333 42666 308999
4619 266333 ▶ if ((signed char)key[i] < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((signed char)key[i] < 0) 6 266327 266333
4620 {
4621 6 klen = -klen;
4622 6 break;
4623 }
4624 42672 key_exists = hv_exists (hv, key, klen);
4625 42672 ▶ if (UNLIKELY (key_exists)) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY (key_exists)) { 25 42647 42672
4626 25 ▶ if (!allow_dupkeys)
Branch: 1 branch
BranchTrueFalseTotal
if (!allow_dupkeys) 7 18 25
4627 7 ERR ("Duplicate keys not allowed");
4628 18 ▶ else if (dupkeys_as_arrayref) {
Branch: 1 branch
BranchTrueFalseTotal
else if (dupkeys_as_arrayref) { 16 2 18
4629 // extend the value to arrayref or push
4630 16 SV** rv = hv_fetch (hv, key, klen, 0);
4631 16 old_value = *rv;
4632 16 SvREFCNT_inc (old_value);
4633 16 ▶ if (!dupkeys_seen
Branch: 1 branch
BranchTrueFalseTotal
if (!dupkeys_seen 7 9 16
4634 7 ▶ || !hv_exists (dupkeys_seen, key, klen))
Branch: 1 branch
BranchTrueFalseTotal
|| !hv_exists (dupkeys_seen, key, klen)) 5 2 7
4635 {
4636 14 AV *av = newAV ();
4637 14 av_extend (av, 2);
4638 14 ▶ if (av_store (av, 0, old_value))
Branch: 1 branch
BranchTrueFalseTotal
if (av_store (av, 0, old_value)) 14 0 14
4639 14 old_value = newRV_noinc ((SV*)av);
4640 14 ▶ if (!dupkeys_seen)
Branch: 1 branch
BranchTrueFalseTotal
if (!dupkeys_seen) 9 5 14
4641 {
4642 9 dupkeys_seen = newHV ();
4643 9 sv_2mortal ((SV *)dupkeys_seen);
4644 }
4645 14 (void)hv_store (dupkeys_seen, key, klen,
4646 newSV (0), 0);
4647 }
4648 16 sv_2mortal (old_value); /* also freed on every error exit */
4649 } // else overwrite it below
4650 }
4651  
4652 42665 dec->cur = p + 1;
4653 42665 ▶ if (dec->cur >= dec->end)
Branch: 1 branch
BranchTrueFalseTotal
if (dec->cur >= dec->end) 3 42662 42665
4654 3 ▶ EXPECT_CH (':');
Branch: 1 branch
BranchTrueFalseTotal
EXPECT_CH (':'); 3 0 3
4655 42662 decode_ws (dec);
4656 42662 ▶ if (*p != ':') EXPECT_CH (':');
Branch: 2 branches
BranchTrueFalseTotal
if (*p != ':') EXPECT_CH (':'); 42651 11 42662
if (*p != ':') EXPECT_CH (':'); 6 42645 42651
4657 42656 decode_ws (dec);
4658  
4659 42656 ▶ if (typesv)
Branch: 1 branch
BranchTrueFalseTotal
if (typesv) 19 42637 42656
4660 {
4661 19 value_typesv = newSV (0);
4662 19 (void)hv_store (typehv, key, klen, value_typesv, 0);
4663 }
4664  
4665 42656 value = decode_sv (aTHX_ dec, value_typesv);
4666 42656 ▶ if (!value)
Branch: 1 branch
BranchTrueFalseTotal
if (!value) 777 41879 42656
4667 777 goto fail;
4668  
4669 41879 ▶ if (UNLIKELY (key_exists && dupkeys_as_arrayref))
Branch: 2 branches
BranchTrueFalseTotal
if (UNLIKELY (key_exists && dupkeys_as_arrayref)) 18 41861 41879
if (UNLIKELY (key_exists && dupkeys_as_arrayref)) 16 2 18
4670 {
4671 16 ▶ if (UNLIKELY (!SvROK (old_value)
Branch: 2 branches
BranchTrueFalseTotal
if (UNLIKELY (!SvROK (old_value) 16 0 16
if (UNLIKELY (!SvROK (old_value) 0 16 16
4672 || SvTYPE (SvRV (old_value)) != SVt_PVAV))
4673 {
4674 0 SvREFCNT_dec (value);
4675 0 ERR ("Invalid dupkeys_as_arrayref hash key");
4676 }
4677 16 av_push ((AV*)SvRV (old_value), value);
4678 16 hv_store_str (aTHX_ hv, key, len, SvREFCNT_inc (old_value));
4679 }
4680 else
4681 {
4682 41863 hv_store_str (aTHX_ hv, key, len, value);
4683 }
4684 41879 break;
4685 }
4686 567123 ++p;
4687 567123 ▶ if (p > dec->end) {
Branch: 1 branch
BranchTrueFalseTotal
if (p > dec->end) { 0 567123 567123
4688 0 dec->cur = p;
4689 0 ▶ EXPECT_CH (':');
Branch: 1 branch
BranchTrueFalseTotal
EXPECT_CH (':'); 0 0 0
4690 }
4691 }
4692 }
4693  
4694 54634 decode_ws (dec);
4695  
4696 54634 ▶ if (*dec->cur == '}')
Branch: 1 branch
BranchTrueFalseTotal
if (*dec->cur == '}') 15297 39337 54634
4697 {
4698 15297 ++dec->cur;
4699 15297 break;
4700 }
4701  
4702 39337 ▶ if (*dec->cur != ',')
Branch: 1 branch
BranchTrueFalseTotal
if (*dec->cur != ',') 5 39332 39337
4703 5 ERR (", or } expected while parsing object/hash");
4704  
4705 39332 ++dec->cur;
4706  
4707 39332 decode_ws (dec);
4708  
4709 39332 ▶ if (*dec->cur == '}' && dec->json.flags & F_RELAXED)
Branch: 2 branches
BranchTrueFalseTotal
if (*dec->cur == '}' && dec->json.flags & F_RELAXED) 3 39329 39332
if (*dec->cur == '}' && dec->json.flags & F_RELAXED) 1 2 3
4710 {
4711 1 ++dec->cur;
4712 1 break;
4713 }
4714 }
4715  
4716 15316 DEC_DEC_DEPTH;
4717 15316 sv = newRV_noinc ((SV *)hv);
4718  
4719 /* check filter callbacks */
4720 15316 ▶ if (dec->json.flags & F_HOOK)
Branch: 1 branch
BranchTrueFalseTotal
if (dec->json.flags & F_HOOK) 19 15297 15316
4721 {
4722 19 ▶ if (dec->json.cb_sk_object && HvKEYS (hv) == 1)
Branch: 3 branches
BranchTrueFalseTotal
if (dec->json.cb_sk_object && HvKEYS (hv) == 1) 15 4 19
if (dec->json.cb_sk_object && HvKEYS (hv) == 1) 0 15 15
if (dec->json.cb_sk_object && HvKEYS (hv) == 1) 11 4 15
4723 {
4724 11 HE *cb = NULL, *he;
4725  
4726 11 hv_iterinit (hv);
4727 11 he = hv_iternext (hv);
4728 11 hv_iterinit (hv);
4729  
4730 /* the next line creates a mortal sv each time it's called. */
4731 /* might want to optimise this for common cases. */
4732 11 ▶ if (LIKELY((UV)he))
Branch: 1 branch
BranchTrueFalseTotal
if (LIKELY((UV)he)) 11 0 11
4733 11 cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0);
4734  
4735 11 ▶ if (cb)
Branch: 1 branch
BranchTrueFalseTotal
if (cb) 9 2 11
4736 {
4737 9 dSP;
4738 I32 count;
4739  
4740 9 ENTER; SAVETMPS;
4741 9 ▶ PUSHMARK (SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK (SP); 0 9 9
4742 9 ▶ XPUSHs (HeVAL (he));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs (HeVAL (he)); 0 9 9
4743 9 sv_2mortal (sv);
4744  
4745 9 PUTBACK; count = call_sv (sv_2mortal (SvREFCNT_inc (HeVAL (cb))), G_ARRAY); SPAGAIN;
4746  
4747 8 ▶ if (!json_validate (&dec->json))
Branch: 1 branch
BranchTrueFalseTotal
if (!json_validate (&dec->json)) 0 8 8
4748 0 croak (NULL);
4749  
4750 8 ▶ if (count == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (count == 1) 6 2 8
4751 {
4752 6 sv = newSVsv (POPs);
4753 6 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 6 0 6
4754 6 return sv;
4755 }
4756  
4757 2 SvREFCNT_inc (sv);
4758 2 SP -= count;
4759 2 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 2 0 2
4760 }
4761 }
4762  
4763 12 ▶ if (dec->json.cb_object)
Branch: 1 branch
BranchTrueFalseTotal
if (dec->json.cb_object) 9 3 12
4764 {
4765 9 dSP;
4766 I32 count;
4767  
4768 9 ENTER; SAVETMPS;
4769 9 ▶ PUSHMARK (SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK (SP); 0 9 9
4770 9 ▶ XPUSHs (sv_2mortal (sv));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs (sv_2mortal (sv)); 0 9 9
4771  
4772 9 PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN;
4773  
4774 8 ▶ if (!json_validate (&dec->json))
Branch: 1 branch
BranchTrueFalseTotal
if (!json_validate (&dec->json)) 0 8 8
4775 0 croak (NULL);
4776  
4777 8 ▶ if (count == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (count == 1) 3 5 8
4778 {
4779 3 sv = newSVsv (POPs);
4780 3 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 3 0 3
4781 3 return sv;
4782 }
4783  
4784 5 SvREFCNT_inc (sv);
4785 5 SP -= count;
4786 5 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 5 0 5
4787 }
4788 }
4789  
4790 15305 return sv;
4791  
4792 832 fail:
4793 832 SvREFCNT_dec (hv);
4794 832 DEC_DEC_DEPTH;
4795 832 return 0;
4796 }
4797  
4798 static SV *
4799 69 decode_tag (pTHX_ dec_t *dec)
4800 {
4801 69 SV *tag = 0;
4802 69 SV *val = 0;
4803  
4804 69 ▶ if (!(dec->json.flags & F_ALLOW_TAGS))
Branch: 1 branch
BranchTrueFalseTotal
if (!(dec->json.flags & F_ALLOW_TAGS)) 0 69 69
4805 0 ERR ("malformed JSON string, neither array, object, number, string or atom");
4806  
4807 69 ++dec->cur;
4808  
4809 69 decode_ws (dec);
4810  
4811 69 ▶ DEC_INC_DEPTH; /* a "(" here recurses straight back into decode_tag */
Branch: 1 branch
BranchTrueFalseTotal
DEC_INC_DEPTH; /* a "(" here recurses straight back into decode_tag */ 2 67 69
4812 67 tag = decode_sv (aTHX_ dec, NULL);
4813 67 DEC_DEC_DEPTH;
4814 67 ▶ if (!tag)
Branch: 1 branch
BranchTrueFalseTotal
if (!tag) 32 35 67
4815 32 goto fail;
4816  
4817 35 ▶ if (!SvPOK (tag))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvPOK (tag)) 0 35 35
4818 0 ERR ("malformed JSON string, (tag) must be a string");
4819  
4820 35 decode_ws (dec);
4821  
4822 35 ▶ if (*dec->cur != ')')
Branch: 1 branch
BranchTrueFalseTotal
if (*dec->cur != ')') 0 35 35
4823 0 ERR (") expected after tag");
4824  
4825 35 ++dec->cur;
4826  
4827 35 decode_ws (dec);
4828  
4829 35 ▶ if (*dec->cur == '[')
Branch: 1 branch
BranchTrueFalseTotal
if (*dec->cur == '[') 3 32 35
4830 3 val = decode_sv (aTHX_ dec, NULL); /* decode_av charges this level */
4831 else
4832 {
4833 32 ▶ DEC_INC_DEPTH; /* ("a")("a")... recurses through the value too */
Branch: 1 branch
BranchTrueFalseTotal
DEC_INC_DEPTH; /* ("a")("a")... recurses through the value too */ 0 32 32
4834 32 val = decode_sv (aTHX_ dec, NULL);
4835 32 DEC_DEC_DEPTH;
4836 }
4837 35 ▶ if (!val)
Branch: 1 branch
BranchTrueFalseTotal
if (!val) 32 3 35
4838 32 goto fail;
4839  
4840 3 ▶ if (!SvROK (val) || SvTYPE (SvRV (val)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK (val) || SvTYPE (SvRV (val)) != SVt_PVAV) 3 0 3
if (!SvROK (val) || SvTYPE (SvRV (val)) != SVt_PVAV) 0 3 3
4841 0 ERR ("malformed JSON string, tag value must be an array");
4842  
4843 {
4844 dMY_CXT;
4845 3 AV *av = (AV *)SvRV (val);
4846 3 HVMAX_T i, len = av_len (av) + 1;
4847 3 HV *stash = gv_stashsv (tag, 0);
4848 SV *sv;
4849 GV *method;
4850 3 dSP;
4851  
4852 3 ▶ if (!stash)
Branch: 1 branch
BranchTrueFalseTotal
if (!stash) 0 3 3
4853 0 ERR ("cannot decode perl-object (package does not exist)");
4854  
4855 3 method = gv_fetchmethod_autoload (stash, "THAW", 0);
4856  
4857 3 ▶ if (!method)
Branch: 1 branch
BranchTrueFalseTotal
if (!method) 0 3 3
4858 0 ERR ("cannot decode perl-object (package does not have a THAW method)");
4859  
4860 3 ENTER; SAVETMPS;
4861 3 SAVEFREESV (tag); /* also freed when THAW dies */
4862 3 SAVEFREESV (val);
4863 3 ▶ PUSHMARK (SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK (SP); 0 3 3
4864 3 ▶ EXTEND (SP, len + 2);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND (SP, len + 2); 3 0 3
EXTEND (SP, len + 2); 0 3 3
4865 /* we re-bless the reference to get overload and other niceties right */
4866 3 PUSHs (tag);
4867 3 PUSHs (MY_CXT.sv_json);
4868  
4869 7 ▶ for (i = 0; i < len; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; ++i) 4 3 7
4870 4 PUSHs (*av_fetch (av, i, 1));
4871  
4872 3 PUTBACK;
4873 3 call_sv ((SV *)GvCV (method), G_SCALAR);
4874 1 SPAGAIN;
4875  
4876 1 ▶ if (!json_validate (&dec->json))
Branch: 1 branch
BranchTrueFalseTotal
if (!json_validate (&dec->json)) 0 1 1
4877 0 croak (NULL);
4878 1 sv = SvREFCNT_inc (POPs);
4879  
4880 1 PUTBACK;
4881  
4882 1 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 1 0 1
4883  
4884 1 return sv;
4885 }
4886  
4887 66 fail:
4888 66 SvREFCNT_dec (tag);
4889 66 SvREFCNT_dec (val);
4890 66 return 0;
4891 }
4892  
4893 static SV *
4894 104143 decode_sv (pTHX_ dec_t *dec, SV *typesv)
4895 {
4896 /* the beauty of JSON: you need exactly one character lookahead */
4897 /* to parse everything. */
4898 104143 switch (*dec->cur)
4899 {
4900 24610 case '"':
4901 24610 ++dec->cur;
4902 24610 ▶ if (typesv)
Branch: 1 branch
BranchTrueFalseTotal
if (typesv) 15 24595 24610
4903 15 sv_setiv_mg (typesv, JSON_TYPE_STRING);
4904 24610 return decode_str (aTHX_ dec);
4905 12 case 0x27:
4906 12 ▶ if (dec->json.flags & F_ALLOW_SQUOTE) {
Branch: 1 branch
BranchTrueFalseTotal
if (dec->json.flags & F_ALLOW_SQUOTE) { 8 4 12
4907 8 ++dec->cur;
4908 8 ▶ if (typesv)
Branch: 1 branch
BranchTrueFalseTotal
if (typesv) 0 8 8
4909 0 sv_setiv_mg (typesv, JSON_TYPE_STRING);
4910 8 return decode_str_sq (aTHX_ dec);
4911 }
4912 4 ERR ("malformed JSON string, neither tag, array, object, number, string or atom");
4913 break;
4914 18957 case '[': ++dec->cur; return decode_av (aTHX_ dec, typesv);
4915 16148 case '{': ++dec->cur; return decode_hv (aTHX_ dec, typesv);
4916 69 case '(': return decode_tag (aTHX_ dec);
4917  
4918 37953 case '-':
4919 case '0': case '1': case '2': case '3': case '4':
4920 case '5': case '6': case '7': case '8': case '9':
4921 37953 return decode_num (aTHX_ dec, typesv);
4922  
4923 33 case 't':
4924 33 ▶ if (dec->end - dec->cur >= 4 && memEQc(dec->cur, "true"))
Branch: 2 branches
BranchTrueFalseTotal
if (dec->end - dec->cur >= 4 && memEQc(dec->cur, "true")) 32 1 33
if (dec->end - dec->cur >= 4 && memEQc(dec->cur, "true")) 29 3 32
4925 {
4926 dMY_CXT;
4927 29 dec->cur += 4;
4928 29 ▶ if (typesv)
Branch: 1 branch
BranchTrueFalseTotal
if (typesv) 5 24 29
4929 5 sv_setiv_mg (typesv, JSON_TYPE_BOOL);
4930 29 ▶ if (dec->json.flags & F_UNBLESSED_BOOL)
Branch: 1 branch
BranchTrueFalseTotal
if (dec->json.flags & F_UNBLESSED_BOOL) 4 25 29
4931 4 return newSVsv (&PL_sv_yes);
4932 25 return newSVsv(MY_CXT.json_true);
4933 }
4934 else
4935 4 ERR ("'true' expected");
4936  
4937 break;
4938  
4939 24 case 'f':
4940 24 ▶ if (dec->end - dec->cur >= 5 && memEQc(dec->cur, "false"))
Branch: 2 branches
BranchTrueFalseTotal
if (dec->end - dec->cur >= 5 && memEQc(dec->cur, "false")) 23 1 24
if (dec->end - dec->cur >= 5 && memEQc(dec->cur, "false")) 22 1 23
4941 {
4942 dMY_CXT;
4943 22 dec->cur += 5;
4944 22 ▶ if (typesv)
Branch: 1 branch
BranchTrueFalseTotal
if (typesv) 5 17 22
4945 5 sv_setiv_mg (typesv, JSON_TYPE_BOOL);
4946 22 ▶ if (dec->json.flags & F_UNBLESSED_BOOL)
Branch: 1 branch
BranchTrueFalseTotal
if (dec->json.flags & F_UNBLESSED_BOOL) 4 18 22
4947 4 return newSVsv (&PL_sv_no);
4948 18 return newSVsv(MY_CXT.json_false);
4949 }
4950 else
4951 2 ERR ("'false' expected");
4952  
4953 break;
4954  
4955 6263 case 'n':
4956 6263 ▶ if (dec->end - dec->cur >= 4 && memEQc(dec->cur, "null"))
Branch: 2 branches
BranchTrueFalseTotal
if (dec->end - dec->cur >= 4 && memEQc(dec->cur, "null")) 6262 1 6263
if (dec->end - dec->cur >= 4 && memEQc(dec->cur, "null")) 6260 2 6262
4957 {
4958 6260 dec->cur += 4;
4959 6260 ▶ if (typesv)
Branch: 1 branch
BranchTrueFalseTotal
if (typesv) 3 6257 6260
4960 3 sv_setiv_mg (typesv, JSON_TYPE_NULL);
4961 6260 return newSVsv(&PL_sv_undef);
4962 }
4963 else
4964 3 ERR ("'null' expected");
4965  
4966 break;
4967  
4968 74 default:
4969 74 ERR ("malformed JSON string, neither tag, array, object, number, string or atom");
4970 break;
4971 }
4972  
4973 87 fail:
4974 87 return 0;
4975 }
4976  
4977 /* decode UTF32-LE/... to UTF-8:
4978 $utf8 = Encode::decode("UTF-32", $string); */
4979 static SV *
4980 8 decode_bom(pTHX_ const char* encoding, SV* string, STRLEN offset)
4981 {
4982 8 dSP;
4983 I32 items;
4984 PERL_UNUSED_ARG(offset);
4985  
4986 #ifndef HAVE_DECODE_BOM
4987 croak ("Cannot handle multibyte BOM yet");
4988 return string;
4989 #else
4990 8 ENTER;
4991 #if PERL_VERSION > 18
4992 /* on older perls (<5.20) this corrupts ax */
4993 8 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT, newSVpvs("Encode"),
4994 NULL, NULL, NULL);
4995 #else
4996 if (!get_cvs("Encode::decode", GV_NOADD_NOINIT|GV_NO_SVGMAGIC))
4997 croak("Multibyte BOM needs to use Encode before");
4998 #endif
4999 8 LEAVE;
5000 8 ENTER;
5001 8 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 8 8
5002 8 ▶ XPUSHs(newSVpvn(encoding, strlen(encoding)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(newSVpvn(encoding, strlen(encoding))); 0 8 8
5003 8 ▶ XPUSHs(string);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(string); 0 8 8
5004 8 PUTBACK;
5005 /* Calling Encode::Unicode::decode_xs would be faster, but we'd need the blessed
5006 enc hash from find_encoding() then. e.g. $Encode::Encoding{'UTF-16LE'}
5007 bless {Name=>UTF-16,size=>2,endian=>'',ucs2=>undef}, 'Encode::Unicode';
5008 And currenty we enjoy the simplicity of the BOM offset advance by
5009 endianness autodetection.
5010 */
5011 8 items = call_sv(MUTABLE_SV(get_cvs("Encode::decode",
5012 GV_NOADD_NOINIT|GV_NO_SVGMAGIC)), G_SCALAR);
5013 8 SPAGAIN;
5014 8 ▶ if (items >= 0 && SvPOK(TOPs)) {
Branch: 2 branches
BranchTrueFalseTotal
if (items >= 0 && SvPOK(TOPs)) { 8 0 8
if (items >= 0 && SvPOK(TOPs)) { 8 0 8
5015 8 LEAVE;
5016 8 SvUTF8_on(TOPs);
5017 8 return POPs;
5018 } else {
5019 0 LEAVE;
5020 0 return string;
5021 }
5022 #endif
5023 }
5024  
5025 static SV *
5026 1632 decode_json (pTHX_ SV *string, JSON *json, STRLEN *offset_return, SV *typesv)
5027 {
5028 dec_t dec;
5029 SV *sv;
5030 1632 STRLEN len, offset = 0;
5031 1632 int converted = 0;
5032 /*dMY_CXT;*/
5033  
5034 1632 ▶ if (!json_validate (json))
Branch: 1 branch
BranchTrueFalseTotal
if (!json_validate (json)) 0 1632 1632
5035 0 croak (NULL);
5036 /* work around bugs in 5.10 where manipulating magic values
5037 * makes perl ignore the magic in subsequent accesses.
5038 * also make a copy of non-PV values, to get them into a clean
5039 * state (SvPV should do that, but it's buggy, see below).
5040 * But breaks decode_prefix with offset.
5041 */
5042 /*SvGETMAGIC (string);*/
5043 1632 ▶ if (SvMAGICAL (string) || !SvPOK (string) || SvIsCOW_shared_hash(string))
Branch: 4 branches
BranchTrueFalseTotal
if (SvMAGICAL (string) || !SvPOK (string) || SvIsCOW_shared_hash(string)) 1521 111 1632
if (SvMAGICAL (string) || !SvPOK (string) || SvIsCOW_shared_hash(string)) 1515 6 1521
if (SvMAGICAL (string) || !SvPOK (string) || SvIsCOW_shared_hash(string)) 243 1272 1515
if (SvMAGICAL (string) || !SvPOK (string) || SvIsCOW_shared_hash(string)) 1 242 243
5044 118 string = sv_2mortal (newSVsv (string));
5045  
5046 1632 ▶ SvUPGRADE (string, SVt_PV);
Branch: 1 branch
BranchTrueFalseTotal
SvUPGRADE (string, SVt_PV); 5 1627 1632
5047  
5048 /* work around a bug in perl 5.10, which causes SvCUR to fail an
5049 * assertion with -DDEBUGGING, although SvCUR is documented to
5050 * return the xpv_cur field which certainly exists after upgrading.
5051 * according to nicholas clark, calling SvPOK fixes this.
5052 * But it doesn't fix it, so try another workaround, call SvPV_nolen
5053 * and hope for the best.
5054 * Damnit, SvPV_nolen still trips over yet another assertion. This
5055 * assertion business is seriously broken, try yet another workaround
5056 * for the broken -DDEBUGGING.
5057 */
5058 {
5059 #ifdef DEBUGGING
5060 len = SvOK (string) ? sv_len (string) : 0;
5061 #else
5062 1632 len = SvCUR (string);
5063 #endif
5064  
5065 1632 ▶ if (UNLIKELY(len > json->max_size && json->max_size))
Branch: 2 branches
BranchTrueFalseTotal
if (UNLIKELY(len > json->max_size && json->max_size)) 1625 7 1632
if (UNLIKELY(len > json->max_size && json->max_size)) 1 1624 1625
5066 1 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu",
5067 (unsigned long)len, (unsigned long)json->max_size);
5068 }
5069  
5070 /* Detect BOM and possibly convert to UTF-8 and set UTF8 flag.
5071  
5072 https://tools.ietf.org/html/rfc7159#section-8.1
5073 JSON text SHALL be encoded in UTF-8, UTF-16, or UTF-32.
5074 Byte Order Mark - While section 8.1 states "Implementations MUST
5075 NOT add a byte order mark to the beginning of a JSON text",
5076 "implementations (...) MAY ignore the presence of a byte order
5077 mark rather than treating it as an error". */
5078 1631 ▶ if (UNLIKELY(len > 2 && SvPOK(string) && !json->incr_pos)) {
Branch: 4 branches
BranchTrueFalseTotal
if (UNLIKELY(len > 2 && SvPOK(string) && !json->incr_pos)) { 1577 54 1631
if (UNLIKELY(len > 2 && SvPOK(string) && !json->incr_pos)) { 1577 0 1577
if (UNLIKELY(len > 2 && SvPOK(string) && !json->incr_pos)) { 1577 54 1631
if (UNLIKELY(len > 2 && SvPOK(string) && !json->incr_pos)) { 1296 281 1577
5079 1296 U8 *s = (U8*)SvPVX (string);
5080 1296 ▶ if (*s >= 0xEF) {
Branch: 1 branch
BranchTrueFalseTotal
if (*s >= 0xEF) { 15 1281 1296
5081 15 ▶ if (len >= 3 && memEQc(s, UTF8BOM)) {
Branch: 2 branches
BranchTrueFalseTotal
if (len >= 3 && memEQc(s, UTF8BOM)) { 15 0 15
if (len >= 3 && memEQc(s, UTF8BOM)) { 8 7 15
5082 8 converted = 1 + (json->flags & F_UTF8);
5083 8 json->flags |= F_UTF8;
5084 8 offset = 3;
5085 8 SvUTF8_on(string);
5086 /* omitting the endian name will skip the BOM in the result */
5087 7 ▶ } else if (len >= 4 && memEQc(s, UTF32BOM)) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len >= 4 && memEQc(s, UTF32BOM)) { 7 0 7
} else if (len >= 4 && memEQc(s, UTF32BOM)) { 2 5 7
5088 2 string = decode_bom(aTHX_ "UTF-32", string, 4);
5089 2 converted = 1 + (json->flags & F_UTF8);
5090 2 json->flags |= F_UTF8;
5091 5 ▶ } else if (memEQc(s, UTF16BOM)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (memEQc(s, UTF16BOM)) { 2 3 5
5092 2 string = decode_bom(aTHX_ "UTF-16", string, 2);
5093 2 converted = 1 + (json->flags & F_UTF8);
5094 2 json->flags |= F_UTF8;
5095 3 ▶ } else if (memEQc(s, UTF16BOM_BE)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (memEQc(s, UTF16BOM_BE)) { 2 1 3
5096 2 string = decode_bom(aTHX_ "UTF-16", string, 2);
5097 2 converted = 1 + (json->flags & F_UTF8);
5098 2 json->flags |= F_UTF8;
5099 }
5100 1281 ▶ } else if (UNLIKELY(len >= 4 && !*s && memEQc(s, UTF32BOM_BE))) {
Branch: 4 branches
BranchTrueFalseTotal
} else if (UNLIKELY(len >= 4 && !*s && memEQc(s, UTF32BOM_BE))) { 1236 45 1281
} else if (UNLIKELY(len >= 4 && !*s && memEQc(s, UTF32BOM_BE))) { 2 1234 1236
} else if (UNLIKELY(len >= 4 && !*s && memEQc(s, UTF32BOM_BE))) { 2 1279 1281
} else if (UNLIKELY(len >= 4 && !*s && memEQc(s, UTF32BOM_BE))) { 2 0 2
5101 2 string = decode_bom(aTHX_ "UTF-32", string, 4);
5102 2 converted = 1 + (json->flags & F_UTF8);
5103 2 json->flags |= F_UTF8;
5104 }
5105 }
5106  
5107 1631 ▶ if (LIKELY(!converted)) {
Branch: 1 branch
BranchTrueFalseTotal
if (LIKELY(!converted)) { 1615 16 1631
5108 1615 ▶ if (DECODE_WANTS_OCTETS (json))
Branch: 1 branch
BranchTrueFalseTotal
if (DECODE_WANTS_OCTETS (json)) 891 724 1615
5109 891 sv_utf8_downgrade (string, 0);
5110 else
5111 724 sv_utf8_upgrade (string);
5112 }
5113  
5114 /* should basically be a NOP but needed for 5.6 with undef */
5115 1630 ▶ if (!SvPOK(string))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvPOK(string)) 0 1630 1630
5116 0 ▶ SvGROW (string, SvCUR (string) + 1);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW (string, SvCUR (string) + 1); 0 0 0
SvGROW (string, SvCUR (string) + 1); 0 0 0
5117  
5118 1630 dec.json = *json;
5119 1630 dec.cur = SvPVX (string) + offset;
5120 1630 dec.end = SvEND (string);
5121 1630 dec.err = 0;
5122 1630 dec.depth = 0;
5123 1630 dec.utf8_checked = 0;
5124 1630 dec.utf8_trusted = 0;
5125 /* incr_parse: validate only the text being decoded, not everything buffered after it */
5126 284 ▶ dec.utf8_end = string == json->incr_text && json->incr_pos && json->incr_pos <= SvCUR (string)
Branch: 2 branches
BranchTrueFalseTotal
dec.utf8_end = string == json->incr_text && json->incr_pos && json->incr_pos <= SvCUR (string) 284 0 284
dec.utf8_end = string == json->incr_text && json->incr_pos && json->incr_pos <= SvCUR (string) 284 0 284
5127 1914 ▶ ? SvPVX (string) + json->incr_pos : dec.end;
Branch: 1 branch
BranchTrueFalseTotal
? SvPVX (string) + json->incr_pos : dec.end; 284 1346 1630
5128  
5129 /* a filter may replace or remove these while this decode still uses them */
5130 1630 ▶ if (dec.json.cb_object)
Branch: 1 branch
BranchTrueFalseTotal
if (dec.json.cb_object) 11 1619 1630
5131 11 sv_2mortal (SvREFCNT_inc (dec.json.cb_object));
5132 1630 ▶ if (dec.json.cb_sk_object)
Branch: 1 branch
BranchTrueFalseTotal
if (dec.json.cb_sk_object) 15 1615 1630
5133 15 sv_2mortal (SvREFCNT_inc ((SV *)dec.json.cb_sk_object));
5134  
5135 1630 ▶ if (dec.json.cb_object || dec.json.cb_sk_object)
Branch: 2 branches
BranchTrueFalseTotal
if (dec.json.cb_object || dec.json.cb_sk_object) 1619 11 1630
if (dec.json.cb_object || dec.json.cb_sk_object) 7 1612 1619
5136 18 dec.json.flags |= F_HOOK;
5137  
5138 1630 *dec.end = 0; /* this should basically be a nop, too, but make sure it's there */
5139  
5140 1630 decode_ws (&dec);
5141 1630 sv = decode_sv (aTHX_ &dec, typesv);
5142  
5143 1626 ▶ if (offset_return) {
Branch: 1 branch
BranchTrueFalseTotal
if (offset_return) { 296 1330 1626
5144 296 char *base = SvPVX (string) + offset;
5145 296 ▶ if (dec.cur < base || dec.cur > SvEND (string))
Branch: 2 branches
BranchTrueFalseTotal
if (dec.cur < base || dec.cur > SvEND (string)) 296 0 296
if (dec.cur < base || dec.cur > SvEND (string)) 0 296 296
5146 0 *offset_return = 0;
5147 else
5148 296 *offset_return = dec.cur - base;
5149 }
5150  
5151 1626 ▶ if (!(offset_return || !sv))
Branch: 2 branches
BranchTrueFalseTotal
if (!(offset_return || !sv)) 1330 296 1626
if (!(offset_return || !sv)) 994 336 1330
5152 {
5153 /* check for trailing garbage */
5154 994 decode_ws (&dec);
5155  
5156 994 ▶ if ((dec.end - dec.cur) || *dec.cur)
Branch: 2 branches
BranchTrueFalseTotal
if ((dec.end - dec.cur) || *dec.cur) 973 21 994
if ((dec.end - dec.cur) || *dec.cur) 0 973 973
5157 {
5158 21 dec.err = "garbage after JSON object";
5159 21 SvREFCNT_dec (sv);
5160 21 sv = NULL;
5161 }
5162 }
5163 1626 ▶ if (!sv)
Branch: 1 branch
BranchTrueFalseTotal
if (!sv) 362 1264 1626
5164 {
5165 362 SV *uni = sv_newmortal ();
5166 362 COP cop = *PL_curcop;
5167 362 ▶ if (dec.cur >= dec.end) // overshoot
Branch: 1 branch
BranchTrueFalseTotal
if (dec.cur >= dec.end) // overshoot 41 321 362
5168 {
5169 41 croak ("%s, at character offset %d",
5170 dec.err,
5171 (int)ptr_to_index (aTHX_ string, dec.cur - SvPVX(string)));
5172 }
5173 #if PERL_VERSION >= 8
5174 /* horrible hack to silence warning inside pv_uni_display */
5175 /* TODO: Can be omitted with newer perls */
5176 321 cop.cop_warnings = pWARN_NONE;
5177 321 ENTER;
5178 321 SAVEVPTR (PL_curcop);
5179 321 PL_curcop = &cop;
5180 321 pv_uni_display (uni, (U8*)dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ);
5181 321 LEAVE;
5182 #endif
5183 642 ▶ croak ("%s, at character offset %d (before \"%s\")",
Branch: 1 branch
BranchTrueFalseTotal
croak ("%s, at character offset %d (before \"%s\")", 321 0 321
5184 dec.err,
5185 (int)ptr_to_index (aTHX_ string, dec.cur - SvPVX(string)),
5186 dec.cur < dec.end ? SvPV_nolen (uni) : "(end of string)");
5187 }
5188  
5189 1264 ▶ if (!(dec.json.flags & F_ALLOW_NONREF) && json_nonref(aTHX_ sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(dec.json.flags & F_ALLOW_NONREF) && json_nonref(aTHX_ sv)) { 25 1239 1264
if (!(dec.json.flags & F_ALLOW_NONREF) && json_nonref(aTHX_ sv)) { 11 14 25
5190 11 SvREFCNT_dec (sv);
5191 11 croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)");
5192 }
5193  
5194 1253 ▶ if (UNLIKELY(converted && !(converted - 1))) /* with BOM, and UTF8 was not set */
Branch: 2 branches
BranchTrueFalseTotal
if (UNLIKELY(converted && !(converted - 1))) /* with BOM, and UTF8 was not set */ 12 1241 1253
if (UNLIKELY(converted && !(converted - 1))) /* with BOM, and UTF8 was not set */ 1 11 12
5195 1 json->flags &= ~F_UTF8;
5196 1253 return sv_2mortal (sv);
5197 }
5198  
5199 /*/////////////////////////////////////////////////////////////////////////// */
5200 /* incremental parser */
5201  
5202 /* a number or true/false/null at the start of the buffer: not self-delimiting, so its
5203 end depends on how the input was chunked */
5204 static int
5205 283 incr_starts_bare_scalar (JSON *self)
5206 {
5207 283 const char *p = SvPVX (self->incr_text);
5208 for (;;)
5209 {
5210 522 ▶ while (*p == 0x20 || *p == 0x09 || *p == 0x0a || *p == 0x0d)
Branch: 4 branches
BranchTrueFalseTotal
while (*p == 0x20 || *p == 0x09 || *p == 0x0a || *p == 0x0d) 230 292 522
while (*p == 0x20 || *p == 0x09 || *p == 0x0a || *p == 0x0d) 0 292 292
while (*p == 0x20 || *p == 0x09 || *p == 0x0a || *p == 0x0d) 5 287 292
while (*p == 0x20 || *p == 0x09 || *p == 0x0a || *p == 0x0d) 0 287 287
5211 235 ++p;
5212 287 ▶ if (*p != '#' || !(self->flags & F_RELAXED))
Branch: 2 branches
BranchTrueFalseTotal
if (*p != '#' || !(self->flags & F_RELAXED)) 4 283 287
if (*p != '#' || !(self->flags & F_RELAXED)) 4 0 4
5213 break;
5214 22 ▶ while (*p && *p != 0x0a && *p != 0x0d)
Branch: 3 branches
BranchTrueFalseTotal
while (*p && *p != 0x0a && *p != 0x0d) 22 0 22
while (*p && *p != 0x0a && *p != 0x0d) 18 4 22
while (*p && *p != 0x0a && *p != 0x0d) 18 0 18
5215 18 ++p;
5216 }
5217 283 ▶ return *p == '-' || (*p >= '0' && *p <= '9')
Branch: 2 branches
BranchTrueFalseTotal
return *p == '-' || (*p >= '0' && *p <= '9') 268 15 283
return *p == '-' || (*p >= '0' && *p <= '9') 267 1 268
5218 566 ▶ || strnEQ (p, "true", 4) || strnEQ (p, "false", 5) || strnEQ (p, "null", 4);
Branch: 4 branches
BranchTrueFalseTotal
|| strnEQ (p, "true", 4) || strnEQ (p, "false", 5) || strnEQ (p, "null", 4); 283 0 283
|| strnEQ (p, "true", 4) || strnEQ (p, "false", 5) || strnEQ (p, "null", 4); 282 0 282
|| strnEQ (p, "true", 4) || strnEQ (p, "false", 5) || strnEQ (p, "null", 4); 282 0 282
|| strnEQ (p, "true", 4) || strnEQ (p, "false", 5) || strnEQ (p, "null", 4); 0 282 282
5219 }
5220  
5221 /* Note that our good friend gcc-9.x crashes here, which looks like one of the
5222 well-known internal gcc tree-optimizer bugs. */
5223 #ifdef BROKEN_GCC_OPT
5224 // or __attribute__((optimize("no-tree-vectorize")))
5225 __attribute__((optimize("O0")))
5226 #endif
5227 static void
5228 353 incr_parse (JSON *self)
5229 {
5230 353 const char *p = SvPVX (self->incr_text) + self->incr_pos;
5231  
5232 /* the state machine here is a bit convoluted and could be simplified a lot */
5233 /* but this would make it slower, so... */
5234  
5235 for (;;)
5236 {
5237 /*printf ("loop pod %d *p<%c><%s>, mode %d nest %d\n", p - SvPVX (self->incr_text), *p, p, self->incr_mode, self->incr_nest);//D */
5238 362 switch (self->incr_mode)
5239 {
5240 /* only used for initial whitespace skipping */
5241 580 case INCR_M_WS:
5242 for (;;)
5243 {
5244 580 ▶ if (*p > 0x20)
Branch: 1 branch
BranchTrueFalseTotal
if (*p > 0x20) 292 288 580
5245 {
5246 292 ▶ if (*p == '#')
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '#') 6 286 292
5247 {
5248 6 self->incr_mode = INCR_M_C0;
5249 6 goto incr_m_c;
5250 }
5251 else
5252 {
5253 286 self->incr_mode = INCR_M_JSON;
5254 286 goto incr_m_json;
5255 }
5256 }
5257 288 ▶ else if (!*p)
Branch: 1 branch
BranchTrueFalseTotal
else if (!*p) 48 240 288
5258 48 goto interrupt;
5259  
5260 240 ++p;
5261 }
5262  
5263 /* skip a single char inside a string (for \\-processing) */
5264 0 case INCR_M_BS:
5265 0 ▶ if (!*p)
Branch: 1 branch
BranchTrueFalseTotal
if (!*p) 0 0 0
5266 0 goto interrupt;
5267  
5268 0 ++p;
5269 0 self->incr_mode = INCR_M_STR;
5270 0 goto incr_m_str;
5271  
5272 /* skip a single char inside a single-quoted string (for \\-processing) */
5273 0 case INCR_M_SQBS:
5274 0 ▶ if (!*p)
Branch: 1 branch
BranchTrueFalseTotal
if (!*p) 0 0 0
5275 0 goto interrupt;
5276  
5277 0 ++p;
5278 0 self->incr_mode = INCR_M_SQSTR;
5279 0 goto incr_m_sqstr;
5280  
5281 /* inside #-style comments */
5282 case INCR_M_C0:
5283 case INCR_M_C1:
5284 9 incr_m_c:
5285 for (;;)
5286 {
5287 45 ▶ if (*p == '\n')
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '\n') 9 36 45
5288 {
5289 9 ▶ self->incr_mode = self->incr_mode == INCR_M_C0 ? INCR_M_WS : INCR_M_JSON;
Branch: 1 branch
BranchTrueFalseTotal
self->incr_mode = self->incr_mode == INCR_M_C0 ? INCR_M_WS : INCR_M_JSON; 6 3 9
5290 9 break;
5291 }
5292 36 ▶ else if (!*p)
Branch: 1 branch
BranchTrueFalseTotal
else if (!*p) 0 36 36
5293 0 goto interrupt;
5294  
5295 36 ++p;
5296 }
5297  
5298 9 break;
5299  
5300 /* inside a string */
5301 case INCR_M_STR:
5302 593 incr_m_str:
5303 for (;;)
5304 {
5305 2143 ▶ if (*p == '"')
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '"') 589 1554 2143
5306 {
5307 589 ++p;
5308 589 self->incr_mode = INCR_M_JSON;
5309  
5310 589 ▶ if (!self->incr_nest)
Branch: 1 branch
BranchTrueFalseTotal
if (!self->incr_nest) 16 573 589
5311 16 goto interrupt;
5312  
5313 573 goto incr_m_json;
5314 }
5315 1554 ▶ else if (*p == '\\')
Branch: 1 branch
BranchTrueFalseTotal
else if (*p == '\\') 498 1056 1554
5316 {
5317 498 ++p; /* "virtually" consumes character after \ */
5318  
5319 498 ▶ if (!*p) /* if at end of string we have to switch modes */
Branch: 1 branch
BranchTrueFalseTotal
if (!*p) /* if at end of string we have to switch modes */ 0 498 498
5320 {
5321 0 self->incr_mode = INCR_M_BS;
5322 0 goto interrupt;
5323 }
5324 }
5325 1056 ▶ else if (!*p)
Branch: 1 branch
BranchTrueFalseTotal
else if (!*p) 4 1052 1056
5326 4 goto interrupt;
5327  
5328 1550 ++p;
5329 }
5330  
5331 /* inside a single-quoted string (allow_singlequote) */
5332 case INCR_M_SQSTR:
5333 15 incr_m_sqstr:
5334 for (;;)
5335 {
5336 31 ▶ if (*p == '\'')
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '\'') 7 24 31
5337 {
5338 7 ++p;
5339 7 self->incr_mode = INCR_M_JSON;
5340  
5341 7 ▶ if (!self->incr_nest)
Branch: 1 branch
BranchTrueFalseTotal
if (!self->incr_nest) 0 7 7
5342 0 goto interrupt;
5343  
5344 7 goto incr_m_json;
5345 }
5346 24 ▶ else if (*p == '\\')
Branch: 1 branch
BranchTrueFalseTotal
else if (*p == '\\') 1 23 24
5347 {
5348 1 ++p;
5349  
5350 1 ▶ if (!*p)
Branch: 1 branch
BranchTrueFalseTotal
if (!*p) 0 1 1
5351 {
5352 0 self->incr_mode = INCR_M_SQBS;
5353 0 goto interrupt;
5354 }
5355 }
5356 23 ▶ else if (!*p)
Branch: 1 branch
BranchTrueFalseTotal
else if (!*p) 8 15 23
5357 8 goto interrupt;
5358  
5359 16 ++p;
5360 }
5361  
5362 /* after initial ws, outside string */
5363 case INCR_M_JSON:
5364 876 incr_m_json:
5365 for (;;)
5366 {
5367 3291 switch (*p++)
5368 {
5369 8 case 0:
5370 8 --p;
5371 8 goto interrupt;
5372  
5373 683 case 0x09:
5374 case 0x0a:
5375 case 0x0d:
5376 case 0x20:
5377 683 ▶ if (!self->incr_nest)
Branch: 1 branch
BranchTrueFalseTotal
if (!self->incr_nest) 1 682 683
5378 {
5379 1 --p; /* do not eat the whitespace, let the next round do it */
5380 1 goto interrupt;
5381 }
5382 682 break;
5383  
5384 589 case '"':
5385 589 self->incr_mode = INCR_M_STR;
5386 589 goto incr_m_str;
5387  
5388 7 case '\'':
5389 7 ▶ if (self->flags & F_ALLOW_SQUOTE)
Branch: 1 branch
BranchTrueFalseTotal
if (self->flags & F_ALLOW_SQUOTE) 7 0 7
5390 {
5391 7 self->incr_mode = INCR_M_SQSTR;
5392 7 goto incr_m_sqstr;
5393 }
5394 0 break;
5395  
5396 369 case '[':
5397 case '{':
5398 case '(':
5399 369 ▶ if (++self->incr_nest > (int)self->max_depth)
Branch: 1 branch
BranchTrueFalseTotal
if (++self->incr_nest > (int)self->max_depth) 1 368 369
5400 1 croak (ERR_NESTING_EXCEEDED);
5401 368 break;
5402  
5403 366 case ']':
5404 case '}':
5405 366 ▶ if (--self->incr_nest <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (--self->incr_nest <= 0) 267 99 366
5406 267 goto interrupt;
5407 99 break;
5408  
5409 0 case ')':
5410 0 --self->incr_nest;
5411 0 break;
5412  
5413 3 case '#':
5414 3 self->incr_mode = INCR_M_C1;
5415 3 goto incr_m_c;
5416 }
5417 }
5418 }
5419 }
5420  
5421 352 interrupt:
5422 352 self->incr_pos = p - SvPVX (self->incr_text);
5423 /*printf ("interrupt<%.*s>\n", self->incr_pos, SvPVX(self->incr_text));//D */
5424 /*printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D */
5425 352 }
5426  
5427 /*/////////////////////////////////////////////////////////////////////////// */
5428 /* XS interface functions */
5429  
5430 MODULE = Cpanel::JSON::XS PACKAGE = Cpanel::JSON::XS
5431  
5432 #if PERL_VERSION > 7
5433 # define NODEBUG_ON \
5434 CvNODEBUG_on (get_cv ("Cpanel::JSON::XS::incr_text", 0));
5435 #else
5436 # define NODEBUG_ON
5437 #endif
5438  
5439 BOOT:
5440 {
5441 HV *stash;
5442 MY_CXT_INIT;
5443 69 init_MY_CXT(aTHX_ &MY_CXT);
5444  
5445 69 stash = gv_stashpvs(JSON_TYPE_CLASS, GV_ADD);
5446 69 newCONSTSUB(stash, "JSON_TYPE_BOOL", newSViv(JSON_TYPE_BOOL));
5447 69 newCONSTSUB(stash, "JSON_TYPE_INT", newSViv(JSON_TYPE_INT));
5448 69 newCONSTSUB(stash, "JSON_TYPE_FLOAT", newSViv(JSON_TYPE_FLOAT));
5449 69 newCONSTSUB(stash, "JSON_TYPE_STRING", newSViv(JSON_TYPE_STRING));
5450 69 newCONSTSUB(stash, "JSON_TYPE_NULL", newSViv(JSON_TYPE_NULL));
5451 69 newCONSTSUB(stash, "JSON_TYPE_INT_OR_NULL", newSViv(JSON_TYPE_INT | JSON_TYPE_CAN_BE_NULL));
5452 69 newCONSTSUB(stash, "JSON_TYPE_BOOL_OR_NULL", newSViv(JSON_TYPE_BOOL | JSON_TYPE_CAN_BE_NULL));
5453 69 newCONSTSUB(stash, "JSON_TYPE_FLOAT_OR_NULL", newSViv(JSON_TYPE_FLOAT | JSON_TYPE_CAN_BE_NULL));
5454 69 newCONSTSUB(stash, "JSON_TYPE_STRING_OR_NULL", newSViv(JSON_TYPE_STRING | JSON_TYPE_CAN_BE_NULL));
5455 69 newCONSTSUB(stash, "JSON_TYPE_CAN_BE_NULL", newSViv(JSON_TYPE_CAN_BE_NULL));
5456 69 newCONSTSUB(stash, "JSON_TYPE_ARRAYOF_CLASS", newSVpvs(JSON_TYPE_ARRAYOF_CLASS));
5457 69 newCONSTSUB(stash, "JSON_TYPE_HASHOF_CLASS", newSVpvs(JSON_TYPE_HASHOF_CLASS));
5458 69 newCONSTSUB(stash, "JSON_TYPE_ANYOF_CLASS", newSVpvs(JSON_TYPE_ANYOF_CLASS));
5459 69 newCONSTSUB(stash, "JSON_TYPE_OPTIONAL_CLASS", newSVpvs(JSON_TYPE_OPTIONAL_CLASS));
5460  
5461 69 NODEBUG_ON; /* the debugger completely breaks lvalue subs */
5462 }
5463  
5464 PROTOTYPES: DISABLE
5465  
5466  
5467 #_if PERL_IMPLICIT_CONTEXT for embedding, but no ithreads, then CLONE is never
5468 # called
5469  
5470 #ifdef USE_ITHREADS
5471  
5472 void CLONE (...)
5473 PPCODE:
5474 MY_CXT_CLONE; /* possible declaration */
5475 init_MY_CXT(aTHX_ &MY_CXT);
5476 /* skip implicit PUTBACK, returning @_ to caller, more efficient*/
5477 return;
5478  
5479 #endif
5480  
5481 void END(...)
5482 PREINIT:
5483 dMY_CXT;
5484 SV * sv;
5485 PPCODE:
5486 69 sv = MY_CXT.sv_json;
5487 69 MY_CXT.sv_json = NULL;
5488 69 ▶ if (sv && SvOK (sv))
Branch: 2 branches
BranchTrueFalseTotal
if (sv && SvOK (sv)) 69 0 69
if (sv && SvOK (sv)) 69 0 69
5489 69 SvREFCNT_dec_NN (sv);
5490 /* skip implicit PUTBACK, returning @_ to caller, more efficient*/
5491 69 return;
5492  
5493 void new (char *klass)
5494 PREINIT:
5495 HV *stash;
5496 PPCODE:
5497 dMY_CXT;
5498 660 SV *pv = NEWSV (0, sizeof (JSON));
5499 660 SvPOK_only (pv);
5500 660 json_init ((JSON *)SvPVX (pv));
5501 #ifdef JSON_MG_DUP
5502 sv_magicext (pv, pv, PERL_MAGIC_ext, &json_mg_vtbl, NULL, 0)->mg_flags |= MGf_DUP;
5503 #endif
5504 660 ▶ if (SvROK (ST(0))) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK (ST(0))) { 2 658 660
5505 /* called as $obj->new — extract real class name from the object */
5506 2 stash = SvSTASH (SvRV (ST(0)));
5507 2 ▶ if (!stash)
Branch: 1 branch
BranchTrueFalseTotal
if (!stash) 0 2 2
5508 0 croak ("Cannot create a %s object from an unblessed reference", klass);
5509 } else {
5510 658 ▶ stash = strlen(klass) == 16 && strEQc (klass, "Cpanel::JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1);
Branch: 2 branches
BranchTrueFalseTotal
stash = strlen(klass) == 16 && strEQc (klass, "Cpanel::JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1); 657 1 658
stash = strlen(klass) == 16 && strEQc (klass, "Cpanel::JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1); 657 0 657
5511 }
5512 660 ▶ XPUSHs (sv_2mortal (sv_bless (
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs (sv_2mortal (sv_bless ( 0 660 660
5513 newRV_noinc (pv), stash
5514 )));
5515  
5516 void ascii (JSON *self, int enable = 1)
5517 ALIAS:
5518 ascii = F_ASCII
5519 latin1 = F_LATIN1
5520 binary = F_BINARY
5521 utf8 = F_UTF8
5522 indent = F_INDENT
5523 canonical = F_CANONICAL
5524 space_before = F_SPACE_BEFORE
5525 space_after = F_SPACE_AFTER
5526 pretty = F_PRETTY
5527 allow_nonref = F_ALLOW_NONREF
5528 shrink = F_SHRINK
5529 allow_blessed = F_ALLOW_BLESSED
5530 convert_blessed = F_CONV_BLESSED
5531 relaxed = SET_RELAXED
5532 allow_unknown = F_ALLOW_UNKNOWN
5533 allow_tags = F_ALLOW_TAGS
5534 allow_barekey = F_ALLOW_BAREKEY
5535 allow_singlequote = F_ALLOW_SQUOTE
5536 allow_bignum = F_ALLOW_BIGNUM
5537 escape_slash = F_ESCAPE_SLASH
5538 allow_stringify = F_ALLOW_STRINGIFY
5539 unblessed_bool = F_UNBLESSED_BOOL
5540 allow_dupkeys = F_ALLOW_DUPKEYS
5541 require_types = F_REQUIRE_TYPES
5542 type_all_string = F_TYPE_ALL_STRING
5543 dupkeys_as_arrayref = F_DUPKEYS_AS_AREF
5544 PPCODE:
5545 1088 ▶ if (enable)
Branch: 1 branch
BranchTrueFalseTotal
if (enable) 1064 24 1088
5546 1064 self->flags |= ix;
5547 else
5548 24 self->flags &= ~ix;
5549 1088 ▶ # Turning on DUPKEYS_AS_AREF also turns on ALLOW_DUPKEYS
Branch: 2 branches
BranchTrueFalseTotal
# Turning on DUPKEYS_AS_AREF also turns on ALLOW_DUPKEYS 4 1084 1088
# Turning on DUPKEYS_AS_AREF also turns on ALLOW_DUPKEYS 4 0 4
5550 4 # But turning off DUPKEYS_AS_AREF does not
5551 1088 ▶ if (ix == F_DUPKEYS_AS_AREF && enable != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (ix == F_DUPKEYS_AS_AREF && enable != 0) 32 1056 1088
5552 32 self->flags |= F_ALLOW_DUPKEYS;
5553 1088 ▶ if (ix == F_ALLOW_NONREF)
Branch: 1 branch
BranchTrueFalseTotal
if (ix == F_ALLOW_NONREF) 0 1088 1088
5554 self->flags |= F_NONREF_SET;
5555 XPUSHs (ST (0));
5556  
5557 void get_ascii (JSON *self)
5558 ALIAS:
5559 get_ascii = F_ASCII
5560 get_latin1 = F_LATIN1
5561 get_binary = F_BINARY
5562 get_utf8 = F_UTF8
5563 get_indent = F_INDENT
5564 get_canonical = F_CANONICAL
5565 get_space_before = F_SPACE_BEFORE
5566 get_space_after = F_SPACE_AFTER
5567 get_allow_nonref = F_ALLOW_NONREF
5568 get_shrink = F_SHRINK
5569 get_allow_blessed = F_ALLOW_BLESSED
5570 get_convert_blessed = F_CONV_BLESSED
5571 get_relaxed = F_RELAXED
5572 get_allow_unknown = F_ALLOW_UNKNOWN
5573 get_allow_tags = F_ALLOW_TAGS
5574 get_allow_barekey = F_ALLOW_BAREKEY
5575 get_allow_singlequote = F_ALLOW_SQUOTE
5576 get_allow_bignum = F_ALLOW_BIGNUM
5577 get_escape_slash = F_ESCAPE_SLASH
5578 get_allow_stringify = F_ALLOW_STRINGIFY
5579 get_unblessed_bool = F_UNBLESSED_BOOL
5580 get_allow_dupkeys = F_ALLOW_DUPKEYS
5581 get_require_types = F_REQUIRE_TYPES
5582 get_type_all_string = F_TYPE_ALL_STRING
5583 get_dupkeys_as_arrayref = F_DUPKEYS_AS_AREF
5584 PPCODE:
5585 0 ▶ XPUSHs (boolSV (self->flags & ix));
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs (boolSV (self->flags & ix)); 0 0 0
XPUSHs (boolSV (self->flags & ix)); 0 0 0
5586  
5587 void indent_length (JSON *self, int val = INDENT_STEP)
5588 PPCODE:
5589 1 ▶ if (0 <= val && val <= 15) {
Branch: 2 branches
BranchTrueFalseTotal
if (0 <= val && val <= 15) { 1 0 1
if (0 <= val && val <= 15) { 1 0 1
5590 1 self->indent_length = val;
5591 } else {
5592 0 warn("The acceptable range of indent_length() is 0 to 15.");
5593 }
5594 1 ▶ XPUSHs (ST (0));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs (ST (0)); 0 1 1
5595  
5596 U32 get_indent_length (JSON *self)
5597 CODE:
5598 0 ▶ RETVAL = self->indent_length;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->indent_length; 0 0 0
5599 OUTPUT:
5600 RETVAL
5601  
5602 void max_depth (JSON *self, U32 max_depth = 0x80000000UL)
5603 PPCODE:
5604 7 self->max_depth = max_depth;
5605 7 ▶ XPUSHs (ST (0));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs (ST (0)); 0 7 7
5606  
5607 U32 get_max_depth (JSON *self)
5608 CODE:
5609 0 ▶ RETVAL = self->max_depth;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->max_depth; 0 0 0
5610 OUTPUT:
5611 RETVAL
5612  
5613 void max_size (JSON *self, U32 max_size = 0)
5614 PPCODE:
5615 6 self->max_size = max_size;
5616 6 ▶ XPUSHs (ST (0));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs (ST (0)); 0 6 6
5617  
5618 int get_max_size (JSON *self)
5619 CODE:
5620 0 ▶ RETVAL = self->max_size;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->max_size; 0 0 0
5621 OUTPUT:
5622 RETVAL
5623  
5624 void stringify_infnan (JSON *self, IV infnan_mode = 1)
5625 PPCODE:
5626 9 ▶ if (infnan_mode > 3 || infnan_mode < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (infnan_mode > 3 || infnan_mode < 0) { 9 0 9
if (infnan_mode > 3 || infnan_mode < 0) { 0 9 9
5627 0 croak ("invalid stringify_infnan mode %d. Must be 0, 1, 2 or 3", (int)infnan_mode);
5628 }
5629 9 self->infnan_mode = (unsigned char)infnan_mode;
5630 9 ▶ XPUSHs (ST (0));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs (ST (0)); 0 9 9
5631
5632 int get_stringify_infnan (JSON *self)
5633 CODE:
5634 0 ▶ RETVAL = (int)self->infnan_mode;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (int)self->infnan_mode; 0 0 0
5635 OUTPUT:
5636 RETVAL
5637  
5638 void sort_by (JSON *self, SV* cb = &PL_sv_yes)
5639 PPCODE:
5640 {
5641 3 SvREFCNT_dec (self->cb_sort_by);
5642 3 ▶ self->cb_sort_by = SvOK (cb) ? newSVsv (cb) : 0;
Branch: 1 branch
BranchTrueFalseTotal
self->cb_sort_by = SvOK (cb) ? newSVsv (cb) : 0; 3 0 3
5643 3 ▶ if (self->cb_sort_by)
Branch: 1 branch
BranchTrueFalseTotal
if (self->cb_sort_by) 3 0 3
5644 3 self->flags |= F_CANONICAL;
5645  
5646 3 ▶ XPUSHs (ST (0));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs (ST (0)); 0 3 3
5647 }
5648  
5649
5650 void filter_json_object (JSON *self, SV *cb = &PL_sv_undef)
5651 PPCODE:
5652 {
5653 6 SvREFCNT_dec (self->cb_object);
5654 6 ▶ self->cb_object = SvOK (cb) ? newSVsv (cb) : 0;
Branch: 1 branch
BranchTrueFalseTotal
self->cb_object = SvOK (cb) ? newSVsv (cb) : 0; 5 1 6
5655  
5656 6 ▶ XPUSHs (ST (0));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs (ST (0)); 0 6 6
5657 }
5658  
5659 void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef)
5660 PPCODE:
5661 {
5662 10 ▶ if (!self->cb_sk_object)
Branch: 1 branch
BranchTrueFalseTotal
if (!self->cb_sk_object) 6 4 10
5663 6 self->cb_sk_object = newHV ();
5664  
5665 10 ▶ if (SvOK (cb))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK (cb)) 8 2 10
5666 8 (void)hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0);
5667 else
5668 {
5669 2 (void)hv_delete_ent (self->cb_sk_object, key, G_DISCARD, 0);
5670  
5671 2 ▶ if (!HvKEYS (self->cb_sk_object))
Branch: 2 branches
BranchTrueFalseTotal
if (!HvKEYS (self->cb_sk_object)) 0 2 2
if (!HvKEYS (self->cb_sk_object)) 1 1 2
5672 {
5673 1 SvREFCNT_dec (self->cb_sk_object);
5674 1 self->cb_sk_object = 0;
5675 }
5676 }
5677  
5678 10 ▶ XPUSHs (ST (0));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs (ST (0)); 0 10 10
5679 }
5680  
5681 void encode (JSON *self, SV *scalar, SV *typesv = &PL_sv_undef)
5682 PPCODE:
5683 1458 PUTBACK; scalar = encode_json (aTHX_ scalar, self, typesv); SPAGAIN;
5684 1419 ▶ XPUSHs (scalar);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs (scalar); 0 1419 1419
5685  
5686 UV encode_to (JSON *self, OutputStream fh, SV *scalar, SV *typesv = &PL_sv_undef)
5687 CODE:
5688 7 ▶ PUTBACK; RETVAL = encode_json_to (aTHX_ scalar, self, typesv, fh); SPAGAIN;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; RETVAL = encode_json_to (aTHX_ scalar, self, typesv, fh); SPAGAIN; 0 5 5
5689 OUTPUT:
5690 RETVAL
5691  
5692 void decode (JSON *self, SV *jsonstr, SV *typesv = NULL)
5693 PPCODE:
5694 1118 PUTBACK; jsonstr = decode_json (aTHX_ jsonstr, self, 0, typesv); SPAGAIN;
5695 798 ▶ XPUSHs (jsonstr);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs (jsonstr); 0 798 798
5696  
5697 void decode_prefix (JSON *self, SV *jsonstr, SV *typesv = NULL)
5698 PPCODE:
5699 {
5700 SV *sv;
5701 STRLEN offset;
5702 12 PUTBACK; sv = decode_json (aTHX_ jsonstr, self, &offset, typesv); SPAGAIN;
5703 10 ▶ EXTEND (SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND (SP, 2); 0 10 10
5704 10 PUSHs (sv);
5705 10 PUSHs (sv_2mortal (newSVuv (ptr_to_index (aTHX_ jsonstr, offset))));
5706 }
5707  
5708 void incr_parse (JSON *self, SV *jsonstr = 0)
5709 PPCODE:
5710 {
5711 654 ▶ if (!self->incr_text)
Branch: 1 branch
BranchTrueFalseTotal
if (!self->incr_text) 40 614 654
5712 40 self->incr_text = newSVpvn ("", 0);
5713  
5714 /* if utf8-ness doesn't match the decoder, need to upgrade/downgrade */
5715 654 ▶ if (!DECODE_WANTS_OCTETS (self) == !SvUTF8 (self->incr_text)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!DECODE_WANTS_OCTETS (self) == !SvUTF8 (self->incr_text)) { 39 615 654
5716 39 ▶ if (DECODE_WANTS_OCTETS (self))
Branch: 1 branch
BranchTrueFalseTotal
if (DECODE_WANTS_OCTETS (self)) 1 38 39
5717 {
5718 1 ▶ if (self->incr_pos)
Branch: 1 branch
BranchTrueFalseTotal
if (self->incr_pos) 1 0 1
5719 1 self->incr_pos = utf8_length ((U8 *)SvPVX (self->incr_text),
5720 (U8 *)SvPVX (self->incr_text) + self->incr_pos);
5721  
5722 1 sv_utf8_downgrade (self->incr_text, 0);
5723 }
5724 else
5725 {
5726 38 sv_utf8_upgrade (self->incr_text);
5727  
5728 38 ▶ if (self->incr_pos)
Branch: 1 branch
BranchTrueFalseTotal
if (self->incr_pos) 1 37 38
5729 1 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos)
5730 1 - (U8 *)SvPVX (self->incr_text);
5731 }
5732 }
5733  
5734 /* append data, if any */
5735 654 ▶ if (jsonstr)
Branch: 1 branch
BranchTrueFalseTotal
if (jsonstr) 349 305 654
5736 {
5737 /* make sure both strings have same encoding */
5738 349 ▶ if (SvUTF8 (jsonstr) != SvUTF8 (self->incr_text)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8 (jsonstr) != SvUTF8 (self->incr_text)) { 341 8 349
5739 341 ▶ if (SvUTF8 (jsonstr))
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8 (jsonstr)) 0 341 341
5740 0 sv_utf8_downgrade (jsonstr, 0);
5741 else
5742 341 sv_utf8_upgrade (jsonstr);
5743 }
5744  
5745 /* and then just blindly append */
5746 {
5747 STRLEN len;
5748 349 const char *str = SvPV (jsonstr, len);
5749 349 STRLEN cur = SvCUR (self->incr_text);
5750  
5751 349 ▶ if (SvLEN (self->incr_text) <= cur + len)
Branch: 1 branch
BranchTrueFalseTotal
if (SvLEN (self->incr_text) <= cur + len) 144 205 349
5752 144 ▶ SvGROW (self->incr_text, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW (self->incr_text, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); 144 0 144
SvGROW (self->incr_text, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); 144 0 144
5753  
5754 349 Move (str, SvEND (self->incr_text), len, char);
5755 349 SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len);
5756 349 *SvEND (self->incr_text) = 0; /* this should basically be a nop, too, but make sure it's there */
5757 }
5758 }
5759  
5760 654 ▶ if (GIMME_V == G_VOID && UNLIKELY(self->max_size && SvCUR (self->incr_text) > self->max_size))
Branch: 3 branches
BranchTrueFalseTotal
if (GIMME_V == G_VOID && UNLIKELY(self->max_size && SvCUR (self->incr_text) > self->max_size)) 310 344 654
if (GIMME_V == G_VOID && UNLIKELY(self->max_size && SvCUR (self->incr_text) > self->max_size)) 1 309 310
if (GIMME_V == G_VOID && UNLIKELY(self->max_size && SvCUR (self->incr_text) > self->max_size)) 1 0 1
5761 1 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu",
5762 (unsigned long)SvCUR (self->incr_text), (unsigned long)self->max_size);
5763  
5764 653 ▶ if (GIMME_V != G_VOID)
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V != G_VOID) 344 309 653
5765 do
5766 {
5767 SV *sv;
5768 STRLEN offset;
5769 char *endp;
5770  
5771 354 ▶ if (!INCR_DONE (self))
Branch: 2 branches
BranchTrueFalseTotal
if (!INCR_DONE (self)) 337 17 354
if (!INCR_DONE (self)) 336 1 337
5772 {
5773 353 incr_parse (self);
5774  
5775 352 ▶ if (UNLIKELY(self->max_size && INCR_SIZE (self) > self->max_size))
Branch: 4 branches
BranchTrueFalseTotal
if (UNLIKELY(self->max_size && INCR_SIZE (self) > self->max_size)) 3 349 352
if (UNLIKELY(self->max_size && INCR_SIZE (self) > self->max_size)) 2 1 3
if (UNLIKELY(self->max_size && INCR_SIZE (self) > self->max_size)) 1 1 2
if (UNLIKELY(self->max_size && INCR_SIZE (self) > self->max_size)) 2 1 3
5776 2 ▶ croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu",
Branch: 2 branches
BranchTrueFalseTotal
croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", 2 0 2
croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", 1 1 2
5777 (unsigned long)INCR_SIZE (self), (unsigned long)self->max_size);
5778  
5779 350 ▶ if (!INCR_DONE (self))
Branch: 2 branches
BranchTrueFalseTotal
if (!INCR_DONE (self)) 333 17 350
if (!INCR_DONE (self)) 49 284 333
5780 {
5781 /* as an optimisation, do not accumulate white space in the incr buffer */
5782 66 ▶ if (self->incr_mode == INCR_M_WS && self->incr_pos)
Branch: 2 branches
BranchTrueFalseTotal
if (self->incr_mode == INCR_M_WS && self->incr_pos) 47 19 66
if (self->incr_mode == INCR_M_WS && self->incr_pos) 5 42 47
5783 {
5784 5 self->incr_pos = 0;
5785 5 SvCUR_set (self->incr_text, 0);
5786 }
5787  
5788 66 break;
5789 }
5790 }
5791  
5792 285 ▶ if (!(self->flags & F_NONREF_SET) && incr_starts_bare_scalar (self))
Branch: 2 branches
BranchTrueFalseTotal
if (!(self->flags & F_NONREF_SET) && incr_starts_bare_scalar (self)) 283 2 285
if (!(self->flags & F_NONREF_SET) && incr_starts_bare_scalar (self)) 1 282 283
5793 1 croak ("incremental parsing of a top-level number, true, false or null needs an explicit allow_nonref");
5794  
5795 284 PUTBACK; sv = decode_json (aTHX_ self->incr_text, self, &offset, NULL); SPAGAIN;
5796 279 ▶ XPUSHs (sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs (sv); 0 279 279
5797  
5798 279 endp = SvPVX(self->incr_text) + offset;
5799 279 self->incr_pos -= offset;
5800 279 self->incr_nest = 0;
5801 279 self->incr_mode = 0;
5802 #if PERL_VERSION > 9
5803 279 sv_chop (self->incr_text, (const char* const)endp);
5804 #else
5805 sv_chop (self->incr_text, (char*)endp);
5806 #endif
5807 }
5808 279 ▶ while (GIMME_V == G_ARRAY);
Branch: 1 branch
BranchTrueFalseTotal
while (GIMME_V == G_ARRAY); 10 269 279
5809 }
5810  
5811 #if PERL_VERSION > 6
5812  
5813 SV *incr_text (JSON *self)
5814 ATTRS: lvalue
5815 PPCODE:
5816 {
5817 PERL_UNUSED_VAR(RETVAL);
5818 290 ▶ if (UNLIKELY(self->incr_pos))
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY(self->incr_pos)) 0 290 290
5819 {
5820 /* We might want to return a copy of the rest.
5821 But incr_parse already chops the start at the end, so this can
5822 only happen on concurrent accesses to incr_parse */
5823 0 croak ("incr_text can not be called when the incremental parser already started parsing");
5824 }
5825 290 ▶ ST(0) = self->incr_text ? self->incr_text : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = self->incr_text ? self->incr_text : &PL_sv_undef; 289 1 290
5826 290 XSRETURN(1);
5827 }
5828  
5829 #else
5830  
5831 SV *incr_text (JSON *self)
5832 PPCODE:
5833 {
5834 if (UNLIKELY(self->incr_pos))
5835 croak ("incr_text can not be called when the incremental parser already started parsing");
5836  
5837 ST(0) = self->incr_text ? self->incr_text : &PL_sv_undef;
5838 XSRETURN(1);
5839 }
5840  
5841 #endif
5842  
5843 void incr_skip (JSON *self)
5844 CODE:
5845 {
5846 2 ▶ if (self->incr_pos)
Branch: 1 branch
BranchTrueFalseTotal
if (self->incr_pos) 2 0 2
5847 {
5848 4 sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + self->incr_pos);
5849 2 self->incr_pos = 0;
5850 2 self->incr_nest = 0;
5851 2 self->incr_mode = 0;
5852 }
5853 }
5854  
5855 void incr_reset (JSON *self)
5856 CODE:
5857 {
5858 1 ▶ if (self->incr_text)
Branch: 1 branch
BranchTrueFalseTotal
if (self->incr_text) 1 0 1
5859 1 SvREFCNT_dec (self->incr_text);
5860 1 self->incr_text = NULL;
5861 1 self->incr_pos = 0;
5862 1 self->incr_nest = 0;
5863 1 self->incr_mode = 0;
5864 }
5865  
5866 void DESTROY (JSON *self)
5867 CODE:
5868 660 ▶ if (!json_validate (self))
Branch: 1 branch
BranchTrueFalseTotal
if (!json_validate (self)) 0 660 660
5869 0 return;
5870 660 ▶ # verify cb_sk_object for a valid HV
Branch: 2 branches
BranchTrueFalseTotal
# verify cb_sk_object for a valid HV 5 655 660
# verify cb_sk_object for a valid HV 5 0 5
5871 5 if (self->cb_sk_object && (SvTYPE (self->cb_sk_object) == SVt_PVHV))
5872 660 ▶ SvREFCNT_dec (self->cb_sk_object);
Branch: 2 branches
BranchTrueFalseTotal
SvREFCNT_dec (self->cb_sk_object); 4 656 660
SvREFCNT_dec (self->cb_sk_object); 4 0 4
5873 4 if (self->cb_object && SvOK (self->cb_object))
5874 660 ▶ SvREFCNT_dec (self->cb_object);
Branch: 2 branches
BranchTrueFalseTotal
SvREFCNT_dec (self->cb_object); 1 659 660
SvREFCNT_dec (self->cb_object); 1 0 1
5875 1 if (self->cb_sort_by && SvOK (self->cb_sort_by))
5876 660 ▶ SvREFCNT_dec (self->cb_sort_by);
Branch: 1 branch
BranchTrueFalseTotal
SvREFCNT_dec (self->cb_sort_by); 39 621 660
5877 39 if (self->incr_text)
5878 SvREFCNT_dec (self->incr_text);
5879  
5880 PROTOTYPES: ENABLE
5881  
5882 void encode_json (SV *scalar, SV *typesv = &PL_sv_undef)
5883 ALIAS:
5884 _to_json = 0
5885 encode_json = F_UTF8
5886 PPCODE:
5887 {
5888 JSON json;
5889 224 json_init (&json);
5890 224 json.flags |= ix;
5891 224 PUTBACK; scalar = encode_json (aTHX_ scalar, &json, typesv); SPAGAIN;
5892 224 ▶ XPUSHs (scalar);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs (scalar); 0 224 224
5893 }
5894  
5895 void decode_json (SV *jsonstr, SV *allow_nonref = NULL, SV *typesv = NULL)
5896 ALIAS:
5897 _from_json = 0
5898 decode_json = F_UTF8
5899 PPCODE:
5900 {
5901 JSON json;
5902 218 json_init (&json);
5903 218 json.flags |= ix;
5904 218 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 17 201 218
5905 /* allow_nonref arg explicitly given: override the default */
5906 17 ▶ if (!SvTRUE (allow_nonref))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvTRUE (allow_nonref)) 7 10 17
5907 7 json.flags &= ~F_ALLOW_NONREF;
5908 }
5909 218 PUTBACK; jsonstr = decode_json (aTHX_ jsonstr, &json, 0, typesv); SPAGAIN;
5910 166 ▶ XPUSHs (jsonstr);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs (jsonstr); 0 166 166
5911 }
« utf8_range.c ↑ Summary