XS.xs

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