XS.xs

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