| 1 |
|
| #include "EXTERN.h" |
| 2 |
|
| #include "perl.h" |
| 3 |
|
| #include "XSUB.h" |
| 4 |
|
| |
| 5 |
|
| #include <assert.h> |
| 6 |
|
| #include <string.h> |
| 7 |
|
| #include <stdlib.h> |
| 8 |
|
| #include <stdio.h> |
| 9 |
|
| #include <limits.h> |
| 10 |
|
| #include <float.h> |
| 11 |
|
| #include <inttypes.h> |
| 12 |
|
| |
| 13 |
|
| #if defined(__BORLANDC__) || defined(_MSC_VER) |
| 14 |
|
| # define snprintf _snprintf // C compilers have this in stdio.h |
| 15 |
|
| #endif |
| 16 |
|
| |
| 17 |
|
| // some old perls do not have this, try to make it work, no |
| 18 |
|
| // guarantees, though. if it breaks, you get to keep the pieces. |
| 19 |
|
| #ifndef UTF8_MAXBYTES |
| 20 |
|
| # define UTF8_MAXBYTES 13 |
| 21 |
|
| #endif |
| 22 |
|
| |
| 23 |
|
| // compatibility with perl <5.18 |
| 24 |
|
| #ifndef HvNAMELEN_get |
| 25 |
|
| # define HvNAMELEN_get(hv) strlen (HvNAME (hv)) |
| 26 |
|
| #endif |
| 27 |
|
| #ifndef HvNAMELEN |
| 28 |
|
| # define HvNAMELEN(hv) HvNAMELEN_get (hv) |
| 29 |
|
| #endif |
| 30 |
|
| #ifndef HvNAMEUTF8 |
| 31 |
|
| # define HvNAMEUTF8(hv) 0 |
| 32 |
|
| #endif |
| 33 |
|
| |
| 34 |
|
| // three extra for rounding, sign, and end of string |
| 35 |
|
| #define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 3) |
| 36 |
|
| |
| 37 |
|
| #define F_ASCII 0x00000001UL |
| 38 |
|
| #define F_LATIN1 0x00000002UL |
| 39 |
|
| #define F_UTF8 0x00000004UL |
| 40 |
|
| #define F_INDENT 0x00000008UL |
| 41 |
|
| #define F_CANONICAL 0x00000010UL |
| 42 |
|
| #define F_SPACE_BEFORE 0x00000020UL |
| 43 |
|
| #define F_SPACE_AFTER 0x00000040UL |
| 44 |
|
| #define F_ALLOW_NONREF 0x00000100UL |
| 45 |
|
| #define F_SHRINK 0x00000200UL |
| 46 |
|
| #define F_ALLOW_BLESSED 0x00000400UL |
| 47 |
|
| #define F_CONV_BLESSED 0x00000800UL |
| 48 |
|
| #define F_RELAXED 0x00001000UL |
| 49 |
|
| #define F_ALLOW_UNKNOWN 0x00002000UL |
| 50 |
|
| #define F_ALLOW_TAGS 0x00004000UL |
| 51 |
|
| #define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing |
| 52 |
|
| |
| 53 |
|
| #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
| 54 |
|
| |
| 55 |
|
| #define INIT_SIZE 64 // initial scalar size to be allocated |
| 56 |
|
| #define INDENT_STEP 3 // spaces per indentation level |
| 57 |
|
| |
| 58 |
|
| #define SHORT_STRING_LEN 16384 // special-case strings of up to this size |
| 59 |
|
| |
| 60 |
|
| #define DECODE_WANTS_OCTETS(json) ((json)->flags & F_UTF8) |
| 61 |
|
| |
| 62 |
|
| #define SB do { |
| 63 |
|
| #define SE } while (0) |
| 64 |
|
| |
| 65 |
|
| #if __GNUC__ >= 3 |
| 66 |
|
| # define expect(expr,value) __builtin_expect ((expr), (value)) |
| 67 |
|
| # define INLINE static inline |
| 68 |
|
| #else |
| 69 |
|
| # define expect(expr,value) (expr) |
| 70 |
|
| # define INLINE static |
| 71 |
|
| #endif |
| 72 |
|
| |
| 73 |
|
| #define expect_false(expr) expect ((expr) != 0, 0) |
| 74 |
|
| #define expect_true(expr) expect ((expr) != 0, 1) |
| 75 |
|
| |
| 76 |
|
| #define IN_RANGE_INC(type,val,beg,end) \ |
| 77 |
|
| ((unsigned type)((unsigned type)(val) - (unsigned type)(beg)) \ |
| 78 |
|
| <= (unsigned type)((unsigned type)(end) - (unsigned type)(beg))) |
| 79 |
|
| |
| 80 |
|
| #define ERR_NESTING_EXCEEDED "json text or perl structure exceeds maximum nesting level (max_depth set too low?)" |
| 81 |
|
| |
| 82 |
|
| #ifdef USE_ITHREADS |
| 83 |
|
| # define JSON_STASH (expect_true (json_stash) ? json_stash : gv_stashpv ("JSON::XS", 1)) |
| 84 |
|
| # define BOOL_STASH (expect_true (bool_stash) ? bool_stash : gv_stashpv ("Types::Serialiser::Boolean", 1)) |
| 85 |
|
| # define GET_BOOL(value) (expect_true (bool_ ## value) ? bool_ ## value : get_bool ("Types::Serialiser::" # value)) |
| 86 |
|
| #else |
| 87 |
|
| # define JSON_STASH json_stash |
| 88 |
|
| # define BOOL_STASH bool_stash |
| 89 |
|
| # define GET_BOOL(value) bool_ ## value |
| 90 |
|
| #endif |
| 91 |
|
| |
| 92 |
|
| // the amount of HEs to allocate on the stack, when sorting keys |
| 93 |
|
| #define STACK_HES 64 |
| 94 |
|
| |
| 95 |
|
| static HV *json_stash, *bool_stash; // JSON::XS::, Types::Serialiser::Boolean:: |
| 96 |
|
| static SV *bool_false, *bool_true; |
| 97 |
|
| static SV *sv_json; |
| 98 |
|
| |
| 99 |
|
| enum { |
| 100 |
|
| INCR_M_WS = 0, // initial whitespace skipping, must be 0 |
| 101 |
|
| INCR_M_TFN, // inside true/false/null |
| 102 |
|
| INCR_M_NUM, // inside number |
| 103 |
|
| INCR_M_STR, // inside string |
| 104 |
|
| INCR_M_BS, // inside backslash |
| 105 |
|
| INCR_M_C0, // inside comment in initial whitespace sequence |
| 106 |
|
| INCR_M_C1, // inside comment in other places |
| 107 |
|
| INCR_M_JSON // outside anything, count nesting |
| 108 |
|
| }; |
| 109 |
|
| |
| 110 |
|
| #define INCR_DONE(json) ((json)->incr_nest <= 0 && (json)->incr_mode == INCR_M_JSON) |
| 111 |
|
| |
| 112 |
|
| typedef struct { |
| 113 |
|
| U32 flags; |
| 114 |
|
| U32 max_depth; |
| 115 |
|
| STRLEN max_size; |
| 116 |
|
| |
| 117 |
|
| SV *cb_object; |
| 118 |
|
| HV *cb_sk_object; |
| 119 |
|
| |
| 120 |
|
| // for the incremental parser |
| 121 |
|
| SV *incr_text; // the source text so far |
| 122 |
|
| STRLEN incr_pos; // the current offset into the text |
| 123 |
|
| int incr_nest; // {[]}-nesting level |
| 124 |
|
| unsigned char incr_mode; |
| 125 |
|
| |
| 126 |
|
| SV *v_false, *v_true; |
| 127 |
|
| } JSON; |
| 128 |
|
| |
| 129 |
|
| INLINE void |
| 130 |
49490 |
| json_init (JSON *json) |
| 131 |
|
| { |
| 132 |
|
| static const JSON init = { F_ALLOW_NONREF, 512 }; |
| 133 |
|
| |
| 134 |
49490 |
| *json = init; |
| 135 |
49490 |
| } |
| 136 |
|
| |
| 137 |
|
| ///////////////////////////////////////////////////////////////////////////// |
| 138 |
|
| // utility functions |
| 139 |
|
| |
| 140 |
|
| INLINE SV * |
| 141 |
50 |
| get_bool (const char *name) |
| 142 |
|
| { |
| 143 |
50 |
| SV *sv = get_sv (name, 1); |
| 144 |
|
| |
| 145 |
50 |
| SvREADONLY_on (sv); |
| 146 |
50 |
| SvREADONLY_on (SvRV (sv)); |
| 147 |
|
| |
| 148 |
50 |
| return sv; |
| 149 |
|
| } |
| 150 |
|
| |
| 151 |
|
| INLINE void |
| 152 |
9216 |
| shrink (SV *sv) |
| 153 |
|
| { |
| 154 |
9216 |
| sv_utf8_downgrade (sv, 1); |
| 155 |
|
| |
| 156 |
9216 |
▶ | if (SvLEN (sv) > SvCUR (sv) + 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvLEN (sv) > SvCUR (sv) + 1) |
9211 |
5 |
9216 |
|
| 157 |
|
| { |
| 158 |
|
| #ifdef SvPV_shrink_to_cur |
| 159 |
9211 |
▶ | SvPV_shrink_to_cur (sv); |
Branch: 3 branches
| Branch | True | False | Total |
| SvPV_shrink_to_cur (sv); |
9193 |
18 |
9211 |
| SvPV_shrink_to_cur (sv); |
0 |
9193 |
9193 |
| SvPV_shrink_to_cur (sv); |
9155 |
56 |
9211 |
|
| 160 |
|
| #elif defined (SvPV_renew) |
| 161 |
|
| SvPV_renew (sv, SvCUR (sv) + 1); |
| 162 |
|
| #endif |
| 163 |
|
| } |
| 164 |
9216 |
| } |
| 165 |
|
| |
| 166 |
|
| /* adds two STRLENs together, slow, and with paranoia */ |
| 167 |
|
| static STRLEN |
| 168 |
179886 |
| strlen_sum (STRLEN l1, STRLEN l2) |
| 169 |
|
| { |
| 170 |
179886 |
| size_t sum = l1 + l2; |
| 171 |
|
| |
| 172 |
179886 |
▶ | if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) |
Branch: 1 branch
| Branch | True | False | Total |
| if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) |
0 |
179886 |
179886 |
|
| 173 |
0 |
| croak ("JSON::XS: string size overflow"); |
| 174 |
|
| |
| 175 |
179886 |
| return sum; |
| 176 |
|
| } |
| 177 |
|
| |
| 178 |
|
| /* similar to SvGROW, but somewhat safer and guarantees exponential realloc strategy */ |
| 179 |
|
| static char * |
| 180 |
89943 |
| json_sv_grow (SV *sv, size_t len1, size_t len2) |
| 181 |
|
| { |
| 182 |
89943 |
| len1 = strlen_sum (len1, len2); |
| 183 |
89943 |
| len1 = strlen_sum (len1, (len1 >> 1) + 1); |
| 184 |
|
| |
| 185 |
89943 |
▶ | if (len1 > 4096 - 24) |
Branch: 1 branch
| Branch | True | False | Total |
| if (len1 > 4096 - 24) |
6452 |
83491 |
89943 |
|
| 186 |
6452 |
| len1 = (len1 | 4095) - 24; |
| 187 |
|
| |
| 188 |
89943 |
▶ | return SvGROW (sv, len1); |
Branch: 2 branches
| Branch | True | False | Total |
| return SvGROW (sv, len1); |
89943 |
0 |
89943 |
| return SvGROW (sv, len1); |
64874 |
25069 |
89943 |
|
| 189 |
|
| } |
| 190 |
|
| |
| 191 |
|
| // decode a utf-8 character and return it, or (UV)-1 in |
| 192 |
|
| // case of an error. |
| 193 |
|
| // we special-case "safe" characters from U+80 .. U+7FF, |
| 194 |
|
| // but use the very good perl function to parse anything else. |
| 195 |
|
| // note that we never call this function for a ascii codepoints |
| 196 |
|
| INLINE UV |
| 197 |
8161152 |
| decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
| 198 |
|
| { |
| 199 |
8161152 |
▶ | if (expect_true (len >= 2 |
Branch: 4 branches
| Branch | True | False | Total |
| if (expect_true (len >= 2 |
8161143 |
9 |
8161152 |
| if (expect_true (len >= 2 |
1418766 |
6742377 |
8161143 |
| if (expect_true (len >= 2 |
1418766 |
0 |
1418766 |
| if (expect_true (len >= 2 |
1418766 |
6742386 |
8161152 |
|
| 200 |
|
| && IN_RANGE_INC (char, s[0], 0xc2, 0xdf) |
| 201 |
|
| && IN_RANGE_INC (char, s[1], 0x80, 0xbf))) |
| 202 |
|
| { |
| 203 |
1418766 |
| *clen = 2; |
| 204 |
1418766 |
| return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
| 205 |
|
| } |
| 206 |
|
| else |
| 207 |
6742386 |
| return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
| 208 |
|
| } |
| 209 |
|
| |
| 210 |
|
| // likewise for encoding, also never called for ascii codepoints |
| 211 |
|
| // this function takes advantage of this fact, although current gccs |
| 212 |
|
| // seem to optimise the check for >= 0x80 away anyways |
| 213 |
|
| INLINE unsigned char * |
| 214 |
4522979 |
| encode_utf8 (unsigned char *s, UV ch) |
| 215 |
|
| { |
| 216 |
4522979 |
▶ | if (expect_false (ch < 0x000080)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (expect_false (ch < 0x000080)) |
0 |
4522979 |
4522979 |
|
| 217 |
0 |
| *s++ = ch; |
| 218 |
4522979 |
▶ | else if (expect_true (ch < 0x000800)) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (expect_true (ch < 0x000800)) |
2277494 |
2245485 |
4522979 |
|
| 219 |
2277494 |
| *s++ = 0xc0 | ( ch >> 6), |
| 220 |
2277494 |
| *s++ = 0x80 | ( ch & 0x3f); |
| 221 |
2245485 |
▶ | else if ( ch < 0x010000) |
Branch: 1 branch
| Branch | True | False | Total |
| else if ( ch < 0x010000) |
1117751 |
1127734 |
2245485 |
|
| 222 |
1117751 |
| *s++ = 0xe0 | ( ch >> 12), |
| 223 |
1117751 |
| *s++ = 0x80 | ((ch >> 6) & 0x3f), |
| 224 |
1117751 |
| *s++ = 0x80 | ( ch & 0x3f); |
| 225 |
1127734 |
▶ | else if ( ch < 0x110000) |
Branch: 1 branch
| Branch | True | False | Total |
| else if ( ch < 0x110000) |
1127734 |
0 |
1127734 |
|
| 226 |
1127734 |
| *s++ = 0xf0 | ( ch >> 18), |
| 227 |
1127734 |
| *s++ = 0x80 | ((ch >> 12) & 0x3f), |
| 228 |
1127734 |
| *s++ = 0x80 | ((ch >> 6) & 0x3f), |
| 229 |
1127734 |
| *s++ = 0x80 | ( ch & 0x3f); |
| 230 |
|
| |
| 231 |
4522979 |
| return s; |
| 232 |
|
| } |
| 233 |
|
| |
| 234 |
|
| // convert offset pointer to character index, sv must be string |
| 235 |
|
| static STRLEN |
| 236 |
74 |
| ptr_to_index (SV *sv, char *offset) |
| 237 |
|
| { |
| 238 |
74 |
| return SvUTF8 (sv) |
| 239 |
35 |
| ? utf8_distance (offset, SvPVX (sv)) |
| 240 |
109 |
▶ | : offset - SvPVX (sv); |
Branch: 1 branch
| Branch | True | False | Total |
| : offset - SvPVX (sv); |
35 |
39 |
74 |
|
| 241 |
|
| } |
| 242 |
|
| |
| 243 |
|
| ///////////////////////////////////////////////////////////////////////////// |
| 244 |
|
| // fp hell |
| 245 |
|
| |
| 246 |
|
| // scan a group of digits, and a trailing exponent |
| 247 |
|
| static void |
| 248 |
114 |
| json_atof_scan1 (const char *s, NV *accum, int *expo, int postdp, int maxdepth) |
| 249 |
|
| { |
| 250 |
114 |
| UV uaccum = 0; |
| 251 |
114 |
| int eaccum = 0; |
| 252 |
|
| |
| 253 |
|
| // if we recurse too deep, skip all remaining digits |
| 254 |
|
| // to avoid a stack overflow attack |
| 255 |
114 |
▶ | if (expect_false (--maxdepth <= 0)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (expect_false (--maxdepth <= 0)) |
114 |
0 |
114 |
|
| 256 |
0 |
▶ | while (*s >= '0' && *s <= '9') |
Branch: 2 branches
| Branch | True | False | Total |
| while (*s >= '0' && *s <= '9') |
0 |
0 |
0 |
| while (*s >= '0' && *s <= '9') |
0 |
0 |
0 |
|
| 257 |
0 |
| ++s; |
| 258 |
|
| |
| 259 |
|
| for (;;) |
| 260 |
215 |
| { |
| 261 |
329 |
| U8 dig = *s - '0'; |
| 262 |
|
| |
| 263 |
329 |
▶ | if (expect_false (dig >= 10)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (expect_false (dig >= 10)) |
114 |
215 |
329 |
|
| 264 |
|
| { |
| 265 |
114 |
▶ | if (dig == (U8)('.' - '0')) |
Branch: 1 branch
| Branch | True | False | Total |
| if (dig == (U8)('.' - '0')) |
46 |
68 |
114 |
|
| 266 |
|
| { |
| 267 |
46 |
| ++s; |
| 268 |
46 |
| json_atof_scan1 (s, accum, expo, 1, maxdepth); |
| 269 |
|
| } |
| 270 |
68 |
▶ | else if ((dig | ' ') == 'e' - '0') |
Branch: 1 branch
| Branch | True | False | Total |
| else if ((dig | ' ') == 'e' - '0') |
53 |
15 |
68 |
|
| 271 |
|
| { |
| 272 |
53 |
| int exp2 = 0; |
| 273 |
53 |
| int neg = 0; |
| 274 |
|
| |
| 275 |
53 |
| ++s; |
| 276 |
|
| |
| 277 |
53 |
▶ | if (*s == '-') |
Branch: 1 branch
| Branch | True | False | Total |
| if (*s == '-') |
6 |
47 |
53 |
|
| 278 |
|
| { |
| 279 |
6 |
| ++s; |
| 280 |
6 |
| neg = 1; |
| 281 |
|
| } |
| 282 |
47 |
▶ | else if (*s == '+') |
Branch: 1 branch
| Branch | True | False | Total |
| else if (*s == '+') |
29 |
18 |
47 |
|
| 283 |
29 |
| ++s; |
| 284 |
|
| |
| 285 |
138 |
▶ | while (*s >= '0' && *s <= '9') |
Branch: 2 branches
| Branch | True | False | Total |
| while (*s >= '0' && *s <= '9') |
88 |
50 |
138 |
| while (*s >= '0' && *s <= '9') |
85 |
3 |
88 |
|
| 286 |
85 |
| exp2 = exp2 * 10 + (*s++ - '0'); |
| 287 |
|
| |
| 288 |
53 |
▶ | *expo += neg ? -exp2 : exp2; |
Branch: 1 branch
| Branch | True | False | Total |
| *expo += neg ? -exp2 : exp2; |
6 |
47 |
53 |
|
| 289 |
|
| } |
| 290 |
|
| |
| 291 |
114 |
| break; |
| 292 |
|
| } |
| 293 |
|
| |
| 294 |
215 |
| ++s; |
| 295 |
|
| |
| 296 |
215 |
| uaccum = uaccum * 10 + dig; |
| 297 |
215 |
| ++eaccum; |
| 298 |
|
| |
| 299 |
|
| // if we have too many digits, then recurse for more |
| 300 |
|
| // we actually do this for rather few digits |
| 301 |
215 |
▶ | if (uaccum >= (UV_MAX - 9) / 10) |
Branch: 1 branch
| Branch | True | False | Total |
| if (uaccum >= (UV_MAX - 9) / 10) |
0 |
215 |
215 |
|
| 302 |
|
| { |
| 303 |
0 |
▶ | if (postdp) *expo -= eaccum; |
Branch: 1 branch
| Branch | True | False | Total |
| if (postdp) *expo -= eaccum; |
0 |
0 |
0 |
|
| 304 |
0 |
| json_atof_scan1 (s, accum, expo, postdp, maxdepth); |
| 305 |
0 |
▶ | if (postdp) *expo += eaccum; |
Branch: 1 branch
| Branch | True | False | Total |
| if (postdp) *expo += eaccum; |
0 |
0 |
0 |
|
| 306 |
|
| |
| 307 |
0 |
| break; |
| 308 |
|
| } |
| 309 |
|
| } |
| 310 |
|
| |
| 311 |
|
| // this relies greatly on the quality of the pow () |
| 312 |
|
| // implementation of the platform, but a good |
| 313 |
|
| // implementation is hard to beat. |
| 314 |
|
| // (IEEE 754 conformant ones are required to be exact) |
| 315 |
114 |
▶ | if (postdp) *expo -= eaccum; |
Branch: 1 branch
| Branch | True | False | Total |
| if (postdp) *expo -= eaccum; |
46 |
68 |
114 |
|
| 316 |
114 |
| *accum += uaccum * Perl_pow (10., *expo); |
| 317 |
114 |
| *expo += eaccum; |
| 318 |
114 |
| } |
| 319 |
|
| |
| 320 |
|
| static NV |
| 321 |
68 |
| json_atof (const char *s) |
| 322 |
|
| { |
| 323 |
68 |
| NV accum = 0.; |
| 324 |
68 |
| int expo = 0; |
| 325 |
68 |
| int neg = 0; |
| 326 |
|
| |
| 327 |
68 |
▶ | if (*s == '-') |
Branch: 1 branch
| Branch | True | False | Total |
| if (*s == '-') |
16 |
52 |
68 |
|
| 328 |
|
| { |
| 329 |
16 |
| ++s; |
| 330 |
16 |
| neg = 1; |
| 331 |
|
| } |
| 332 |
|
| |
| 333 |
|
| // a recursion depth of ten gives us >>500 bits |
| 334 |
68 |
| json_atof_scan1 (s, &accum, &expo, 0, 10); |
| 335 |
|
| |
| 336 |
68 |
▶ | return neg ? -accum : accum; |
Branch: 1 branch
| Branch | True | False | Total |
| return neg ? -accum : accum; |
16 |
52 |
68 |
|
| 337 |
|
| } |
| 338 |
|
| |
| 339 |
|
| // target of scalar reference is bool? -1 == nope, 0 == false, 1 == true |
| 340 |
|
| static int |
| 341 |
15765 |
| ref_bool_type (SV *sv) |
| 342 |
|
| { |
| 343 |
15765 |
| svtype svt = SvTYPE (sv); |
| 344 |
|
| |
| 345 |
15765 |
▶ | if (svt < SVt_PVAV) |
Branch: 1 branch
| Branch | True | False | Total |
| if (svt < SVt_PVAV) |
9 |
15756 |
15765 |
|
| 346 |
|
| { |
| 347 |
9 |
| STRLEN len = 0; |
| 348 |
17 |
▶ | char *pv = svt ? SvPV (sv, len) : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| char *pv = svt ? SvPV (sv, len) : 0; |
8 |
1 |
9 |
|
| 349 |
|
| |
| 350 |
9 |
▶ | if (len == 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (len == 1) |
3 |
6 |
9 |
|
| 351 |
3 |
▶ | if (*pv == '1') |
Branch: 1 branch
| Branch | True | False | Total |
| if (*pv == '1') |
1 |
2 |
3 |
|
| 352 |
2 |
| return 1; |
| 353 |
2 |
▶ | else if (*pv == '0') |
Branch: 1 branch
| Branch | True | False | Total |
| else if (*pv == '0') |
1 |
1 |
2 |
|
| 354 |
1 |
| return 0; |
| 355 |
|
| } |
| 356 |
|
| |
| 357 |
15763 |
| return -1; |
| 358 |
|
| } |
| 359 |
|
| |
| 360 |
|
| // returns whether scalar is not a reference in the sense of allow_nonref |
| 361 |
|
| static int |
| 362 |
15757 |
| json_nonref (SV *scalar) |
| 363 |
|
| { |
| 364 |
15757 |
▶ | if (!SvROK (scalar)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvROK (scalar)) |
1 |
15756 |
15757 |
|
| 365 |
1 |
| return 1; |
| 366 |
|
| |
| 367 |
15756 |
| scalar = SvRV (scalar); |
| 368 |
|
| |
| 369 |
15756 |
▶ | if (SvTYPE (scalar) >= SVt_PVMG) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvTYPE (scalar) >= SVt_PVMG) |
15756 |
0 |
15756 |
|
| 370 |
|
| { |
| 371 |
15756 |
▶ | if (SvSTASH (scalar) == bool_stash) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvSTASH (scalar) == bool_stash) |
0 |
15756 |
15756 |
|
| 372 |
0 |
| return 1; |
| 373 |
|
| |
| 374 |
15756 |
▶ | if (!SvOBJECT (scalar) && ref_bool_type (scalar) >= 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvOBJECT (scalar) && ref_bool_type (scalar) >= 0) |
15756 |
0 |
15756 |
| if (!SvOBJECT (scalar) && ref_bool_type (scalar) >= 0) |
0 |
15756 |
15756 |
|
| 375 |
0 |
| return 1; |
| 376 |
|
| } |
| 377 |
|
| |
| 378 |
15756 |
| return 0; |
| 379 |
|
| } |
| 380 |
|
| |
| 381 |
|
| ///////////////////////////////////////////////////////////////////////////// |
| 382 |
|
| // encoder |
| 383 |
|
| |
| 384 |
|
| // structure used for encoding JSON |
| 385 |
|
| typedef struct |
| 386 |
|
| { |
| 387 |
|
| char *cur; // SvPVX (sv) + current output position |
| 388 |
|
| char *end; // SvEND (sv) |
| 389 |
|
| SV *sv; // result scalar |
| 390 |
|
| JSON json; |
| 391 |
|
| U32 indent; // indentation level |
| 392 |
|
| UV limit; // escape character values >= this value when encoding |
| 393 |
|
| } enc_t; |
| 394 |
|
| |
| 395 |
|
| INLINE void |
| 396 |
7745598 |
| need (enc_t *enc, STRLEN len) |
| 397 |
|
| { |
| 398 |
7745598 |
▶ | if (expect_false ((uintptr_t)(enc->end - enc->cur) < len)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (expect_false ((uintptr_t)(enc->end - enc->cur) < len)) |
89800 |
7655798 |
7745598 |
|
| 399 |
|
| { |
| 400 |
89800 |
| STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); |
| 401 |
89800 |
| char *buf = json_sv_grow (enc->sv, cur, len); |
| 402 |
89800 |
| enc->cur = buf + cur; |
| 403 |
89800 |
| enc->end = buf + SvLEN (enc->sv) - 1; |
| 404 |
|
| } |
| 405 |
7745598 |
| } |
| 406 |
|
| |
| 407 |
|
| INLINE void |
| 408 |
104373 |
| encode_ch (enc_t *enc, char ch) |
| 409 |
|
| { |
| 410 |
104373 |
| need (enc, 1); |
| 411 |
104373 |
| *enc->cur++ = ch; |
| 412 |
104373 |
| } |
| 413 |
|
| |
| 414 |
|
| static void |
| 415 |
26037 |
| encode_str (enc_t *enc, char *str, STRLEN len, int is_utf8) |
| 416 |
|
| { |
| 417 |
26037 |
| char *end = str + len; |
| 418 |
|
| |
| 419 |
26037 |
| need (enc, len); |
| 420 |
|
| |
| 421 |
9504874 |
▶ | while (str < end) |
Branch: 1 branch
| Branch | True | False | Total |
| while (str < end) |
9478837 |
26037 |
9504874 |
|
| 422 |
|
| { |
| 423 |
9478837 |
| unsigned char ch = *(unsigned char *)str; |
| 424 |
|
| |
| 425 |
9478837 |
▶ | if (expect_true (ch >= 0x20 && ch < 0x80)) // most common case |
Branch: 3 branches
| Branch | True | False | Total |
| if (expect_true (ch >= 0x20 && ch < 0x80)) // most common case |
8785304 |
693533 |
9478837 |
| if (expect_true (ch >= 0x20 && ch < 0x80)) // most common case |
1904995 |
6880309 |
8785304 |
| if (expect_true (ch >= 0x20 && ch < 0x80)) // most common case |
1904995 |
7573842 |
9478837 |
|
| 426 |
|
| { |
| 427 |
1904995 |
▶ | if (expect_false (ch == '"')) // but with slow exceptions |
Branch: 1 branch
| Branch | True | False | Total |
| if (expect_false (ch == '"')) // but with slow exceptions |
21118 |
1883877 |
1904995 |
|
| 428 |
|
| { |
| 429 |
21118 |
| need (enc, len + 1); |
| 430 |
21118 |
| *enc->cur++ = '\\'; |
| 431 |
21118 |
| *enc->cur++ = '"'; |
| 432 |
|
| } |
| 433 |
1883877 |
▶ | else if (expect_false (ch == '\\')) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (expect_false (ch == '\\')) |
19396 |
1864481 |
1883877 |
|
| 434 |
|
| { |
| 435 |
19396 |
| need (enc, len + 1); |
| 436 |
19396 |
| *enc->cur++ = '\\'; |
| 437 |
19396 |
| *enc->cur++ = '\\'; |
| 438 |
|
| } |
| 439 |
|
| else |
| 440 |
1864481 |
| *enc->cur++ = ch; |
| 441 |
|
| |
| 442 |
1904995 |
| ++str; |
| 443 |
|
| } |
| 444 |
|
| else |
| 445 |
|
| { |
| 446 |
7573842 |
| switch (ch) |
| 447 |
|
| { |
| 448 |
21215 |
| case '\010': need (enc, len + 1); *enc->cur++ = '\\'; *enc->cur++ = 'b'; ++str; break; |
| 449 |
25934 |
| case '\011': need (enc, len + 1); *enc->cur++ = '\\'; *enc->cur++ = 't'; ++str; break; |
| 450 |
22278 |
| case '\012': need (enc, len + 1); *enc->cur++ = '\\'; *enc->cur++ = 'n'; ++str; break; |
| 451 |
21847 |
| case '\014': need (enc, len + 1); *enc->cur++ = '\\'; *enc->cur++ = 'f'; ++str; break; |
| 452 |
21599 |
| case '\015': need (enc, len + 1); *enc->cur++ = '\\'; *enc->cur++ = 'r'; ++str; break; |
| 453 |
|
| |
| 454 |
7460969 |
| default: |
| 455 |
|
| { |
| 456 |
|
| STRLEN clen; |
| 457 |
|
| UV uch; |
| 458 |
|
| |
| 459 |
7460969 |
▶ | if (is_utf8) |
Branch: 1 branch
| Branch | True | False | Total |
| if (is_utf8) |
4721101 |
2739868 |
7460969 |
|
| 460 |
|
| { |
| 461 |
4721101 |
| uch = decode_utf8 (str, end - str, &clen); |
| 462 |
4721101 |
▶ | if (clen == (STRLEN)-1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (clen == (STRLEN)-1) |
0 |
4721101 |
4721101 |
|
| 463 |
0 |
| croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
| 464 |
|
| } |
| 465 |
|
| else |
| 466 |
|
| { |
| 467 |
2739868 |
| uch = ch; |
| 468 |
2739868 |
| clen = 1; |
| 469 |
|
| } |
| 470 |
|
| |
| 471 |
7460969 |
▶ | if (uch < 0x80/*0x20*/ || uch >= enc->limit) |
Branch: 2 branches
| Branch | True | False | Total |
| if (uch < 0x80/*0x20*/ || uch >= enc->limit) |
6880309 |
580660 |
7460969 |
| if (uch < 0x80/*0x20*/ || uch >= enc->limit) |
3440024 |
3440285 |
6880309 |
|
| 472 |
|
| { |
| 473 |
4020684 |
▶ | if (uch >= 0x10000UL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (uch >= 0x10000UL) |
1127734 |
2892950 |
4020684 |
|
| 474 |
|
| { |
| 475 |
1127734 |
▶ | if (uch >= 0x110000UL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (uch >= 0x110000UL) |
0 |
1127734 |
1127734 |
|
| 476 |
0 |
| croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
| 477 |
|
| |
| 478 |
1127734 |
| need (enc, len + 11); |
| 479 |
1127734 |
| sprintf (enc->cur, "\\u%04x\\u%04x", |
| 480 |
1127734 |
| (int)((uch - 0x10000) / 0x400 + 0xD800), |
| 481 |
1127734 |
| (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
| 482 |
1127734 |
| enc->cur += 12; |
| 483 |
|
| } |
| 484 |
|
| else |
| 485 |
|
| { |
| 486 |
2892950 |
| need (enc, len + 5); |
| 487 |
2892950 |
| *enc->cur++ = '\\'; |
| 488 |
2892950 |
| *enc->cur++ = 'u'; |
| 489 |
2892950 |
| *enc->cur++ = PL_hexdigit [ uch >> 12 ]; |
| 490 |
2892950 |
| *enc->cur++ = PL_hexdigit [(uch >> 8) & 15]; |
| 491 |
2892950 |
| *enc->cur++ = PL_hexdigit [(uch >> 4) & 15]; |
| 492 |
2892950 |
| *enc->cur++ = PL_hexdigit [(uch >> 0) & 15]; |
| 493 |
|
| } |
| 494 |
|
| |
| 495 |
4020684 |
| str += clen; |
| 496 |
|
| } |
| 497 |
3440285 |
▶ | else if (enc->json.flags & F_LATIN1) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (enc->json.flags & F_LATIN1) |
2 |
3440283 |
3440285 |
|
| 498 |
|
| { |
| 499 |
2 |
| *enc->cur++ = uch; |
| 500 |
2 |
| str += clen; |
| 501 |
|
| } |
| 502 |
3440283 |
▶ | else if (is_utf8) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (is_utf8) |
2357539 |
1082744 |
3440283 |
|
| 503 |
|
| { |
| 504 |
2357539 |
| need (enc, len + clen); |
| 505 |
|
| do |
| 506 |
|
| { |
| 507 |
8088342 |
| *enc->cur++ = *str++; |
| 508 |
|
| } |
| 509 |
8088342 |
▶ | while (--clen); |
Branch: 1 branch
| Branch | True | False | Total |
| while (--clen); |
5730803 |
2357539 |
8088342 |
|
| 510 |
|
| } |
| 511 |
|
| else |
| 512 |
|
| { |
| 513 |
1082744 |
| need (enc, len + UTF8_MAXBYTES - 1); // never more than 11 bytes needed |
| 514 |
1082744 |
| enc->cur = encode_utf8 (enc->cur, uch); |
| 515 |
1082744 |
| ++str; |
| 516 |
|
| } |
| 517 |
|
| } |
| 518 |
|
| } |
| 519 |
|
| } |
| 520 |
|
| |
| 521 |
9478837 |
| --len; |
| 522 |
|
| } |
| 523 |
26037 |
| } |
| 524 |
|
| |
| 525 |
|
| INLINE void |
| 526 |
51709 |
| encode_indent (enc_t *enc) |
| 527 |
|
| { |
| 528 |
51709 |
▶ | if (enc->json.flags & F_INDENT) |
Branch: 1 branch
| Branch | True | False | Total |
| if (enc->json.flags & F_INDENT) |
23 |
51686 |
51709 |
|
| 529 |
|
| { |
| 530 |
23 |
| int spaces = enc->indent * INDENT_STEP; |
| 531 |
|
| |
| 532 |
23 |
| need (enc, spaces); |
| 533 |
23 |
| memset (enc->cur, ' ', spaces); |
| 534 |
23 |
| enc->cur += spaces; |
| 535 |
|
| } |
| 536 |
51709 |
| } |
| 537 |
|
| |
| 538 |
|
| INLINE void |
| 539 |
10 |
| encode_space (enc_t *enc) |
| 540 |
|
| { |
| 541 |
10 |
| need (enc, 1); |
| 542 |
10 |
| encode_ch (enc, ' '); |
| 543 |
10 |
| } |
| 544 |
|
| |
| 545 |
|
| INLINE void |
| 546 |
75705 |
| encode_nl (enc_t *enc) |
| 547 |
|
| { |
| 548 |
75705 |
▶ | if (enc->json.flags & F_INDENT) |
Branch: 1 branch
| Branch | True | False | Total |
| if (enc->json.flags & F_INDENT) |
30 |
75675 |
75705 |
|
| 549 |
|
| { |
| 550 |
30 |
| need (enc, 1); |
| 551 |
30 |
| encode_ch (enc, '\n'); |
| 552 |
|
| } |
| 553 |
75705 |
| } |
| 554 |
|
| |
| 555 |
|
| INLINE void |
| 556 |
1100 |
| encode_comma (enc_t *enc) |
| 557 |
|
| { |
| 558 |
1100 |
| encode_ch (enc, ','); |
| 559 |
|
| |
| 560 |
1100 |
▶ | if (enc->json.flags & F_INDENT) |
Branch: 1 branch
| Branch | True | False | Total |
| if (enc->json.flags & F_INDENT) |
5 |
1095 |
1100 |
|
| 561 |
5 |
| encode_nl (enc); |
| 562 |
1095 |
▶ | else if (enc->json.flags & F_SPACE_AFTER) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (enc->json.flags & F_SPACE_AFTER) |
0 |
1095 |
1095 |
|
| 563 |
0 |
| encode_space (enc); |
| 564 |
1100 |
| } |
| 565 |
|
| |
| 566 |
|
| static void encode_sv (enc_t *enc, SV *sv); |
| 567 |
|
| |
| 568 |
|
| static void |
| 569 |
24994 |
| encode_av (enc_t *enc, AV *av) |
| 570 |
|
| { |
| 571 |
24994 |
| int i, len = av_len (av); |
| 572 |
|
| |
| 573 |
24994 |
▶ | if (enc->indent >= enc->json.max_depth) |
Branch: 1 branch
| Branch | True | False | Total |
| if (enc->indent >= enc->json.max_depth) |
1 |
24993 |
24994 |
|
| 574 |
1 |
| croak (ERR_NESTING_EXCEEDED); |
| 575 |
|
| |
| 576 |
24993 |
| encode_ch (enc, '['); |
| 577 |
|
| |
| 578 |
24993 |
▶ | if (len >= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (len >= 0) |
24983 |
10 |
24993 |
|
| 579 |
|
| { |
| 580 |
24983 |
| encode_nl (enc); ++enc->indent; |
| 581 |
|
| |
| 582 |
50750 |
▶ | for (i = 0; i <= len; ++i) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i <= len; ++i) |
25776 |
24974 |
50750 |
|
| 583 |
|
| { |
| 584 |
25776 |
| SV **svp = av_fetch (av, i, 0); |
| 585 |
|
| |
| 586 |
25776 |
| encode_indent (enc); |
| 587 |
|
| |
| 588 |
25776 |
▶ | if (svp) |
Branch: 1 branch
| Branch | True | False | Total |
| if (svp) |
25774 |
2 |
25776 |
|
| 589 |
25774 |
| encode_sv (enc, *svp); |
| 590 |
|
| else |
| 591 |
2 |
| encode_str (enc, "null", 4, 0); |
| 592 |
|
| |
| 593 |
25767 |
▶ | if (i < len) |
Branch: 1 branch
| Branch | True | False | Total |
| if (i < len) |
793 |
24974 |
25767 |
|
| 594 |
793 |
| encode_comma (enc); |
| 595 |
|
| } |
| 596 |
|
| |
| 597 |
24974 |
| encode_nl (enc); --enc->indent; encode_indent (enc); |
| 598 |
|
| } |
| 599 |
|
| |
| 600 |
24984 |
| encode_ch (enc, ']'); |
| 601 |
24984 |
| } |
| 602 |
|
| |
| 603 |
|
| static void |
| 604 |
634 |
| encode_hk (enc_t *enc, HE *he) |
| 605 |
|
| { |
| 606 |
634 |
| encode_ch (enc, '"'); |
| 607 |
|
| |
| 608 |
634 |
▶ | if (HeKLEN (he) == HEf_SVKEY) |
Branch: 1 branch
| Branch | True | False | Total |
| if (HeKLEN (he) == HEf_SVKEY) |
1 |
633 |
634 |
|
| 609 |
|
| { |
| 610 |
1 |
▶ | SV *sv = HeSVKEY (he); |
Branch: 2 branches
| Branch | True | False | Total |
| SV *sv = HeSVKEY (he); |
1 |
0 |
1 |
| SV *sv = HeSVKEY (he); |
1 |
0 |
1 |
|
| 611 |
|
| STRLEN len; |
| 612 |
|
| char *str; |
| 613 |
|
| |
| 614 |
1 |
| SvGETMAGIC (sv); |
| 615 |
1 |
| str = SvPV (sv, len); |
| 616 |
|
| |
| 617 |
1 |
| encode_str (enc, str, len, SvUTF8 (sv)); |
| 618 |
|
| } |
| 619 |
|
| else |
| 620 |
633 |
| encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
| 621 |
|
| |
| 622 |
634 |
| encode_ch (enc, '"'); |
| 623 |
|
| |
| 624 |
634 |
▶ | if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); |
Branch: 1 branch
| Branch | True | False | Total |
| if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); |
5 |
629 |
634 |
|
| 625 |
634 |
| encode_ch (enc, ':'); |
| 626 |
634 |
▶ | if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); |
Branch: 1 branch
| Branch | True | False | Total |
| if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); |
5 |
629 |
634 |
|
| 627 |
634 |
| } |
| 628 |
|
| |
| 629 |
|
| // compare hash entries, used when all keys are bytestrings |
| 630 |
|
| static int |
| 631 |
224 |
| he_cmp_fast (const void *a_, const void *b_) |
| 632 |
|
| { |
| 633 |
|
| int cmp; |
| 634 |
|
| |
| 635 |
224 |
| HE *a = *(HE **)a_; |
| 636 |
224 |
| HE *b = *(HE **)b_; |
| 637 |
|
| |
| 638 |
224 |
| STRLEN la = HeKLEN (a); |
| 639 |
224 |
| STRLEN lb = HeKLEN (b); |
| 640 |
|
| |
| 641 |
224 |
▶ | if (!(cmp = memcmp (HeKEY (b), HeKEY (a), lb < la ? lb : la))) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(cmp = memcmp (HeKEY (b), HeKEY (a), lb < la ? lb : la))) |
198 |
26 |
224 |
|
| 642 |
198 |
| cmp = lb - la; |
| 643 |
|
| |
| 644 |
224 |
| return cmp; |
| 645 |
|
| } |
| 646 |
|
| |
| 647 |
|
| // compare hash entries, used when some keys are sv's or utf-x |
| 648 |
|
| static int |
| 649 |
366 |
| he_cmp_slow (const void *a, const void *b) |
| 650 |
|
| { |
| 651 |
366 |
▶ | return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a)); |
Branch: 4 branches
| Branch | True | False | Total |
| return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a)); |
366 |
0 |
366 |
| return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a)); |
0 |
366 |
366 |
| return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a)); |
366 |
0 |
366 |
| return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a)); |
0 |
366 |
366 |
|
| 652 |
|
| } |
| 653 |
|
| |
| 654 |
|
| static void |
| 655 |
338 |
| encode_hv (enc_t *enc, HV *hv) |
| 656 |
|
| { |
| 657 |
|
| HE *he; |
| 658 |
|
| |
| 659 |
338 |
▶ | if (enc->indent >= enc->json.max_depth) |
Branch: 1 branch
| Branch | True | False | Total |
| if (enc->indent >= enc->json.max_depth) |
1 |
337 |
338 |
|
| 660 |
1 |
| croak (ERR_NESTING_EXCEEDED); |
| 661 |
|
| |
| 662 |
337 |
| encode_ch (enc, '{'); |
| 663 |
|
| |
| 664 |
|
| // for canonical output we have to sort by keys first |
| 665 |
|
| // actually, this is mostly due to the stupid so-called |
| 666 |
|
| // security workaround added somewhere in 5.8.x |
| 667 |
|
| // that randomises hash orderings |
| 668 |
337 |
▶ | if (enc->json.flags & F_CANONICAL && !SvRMAGICAL (hv)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (enc->json.flags & F_CANONICAL && !SvRMAGICAL (hv)) |
219 |
118 |
337 |
| if (enc->json.flags & F_CANONICAL && !SvRMAGICAL (hv)) |
219 |
0 |
219 |
|
| 669 |
219 |
| { |
| 670 |
219 |
| int count = hv_iterinit (hv); |
| 671 |
|
| |
| 672 |
219 |
▶ | if (SvMAGICAL (hv)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvMAGICAL (hv)) |
0 |
219 |
219 |
|
| 673 |
|
| { |
| 674 |
|
| // need to count by iterating. could improve by dynamically building the vector below |
| 675 |
|
| // but I don't care for the speed of this special case. |
| 676 |
|
| // note also that we will run into undefined behaviour when the two iterations |
| 677 |
|
| // do not result in the same count, something I might care for in some later release. |
| 678 |
|
| |
| 679 |
0 |
| count = 0; |
| 680 |
0 |
▶ | while (hv_iternext (hv)) |
Branch: 1 branch
| Branch | True | False | Total |
| while (hv_iternext (hv)) |
0 |
0 |
0 |
|
| 681 |
0 |
| ++count; |
| 682 |
|
| |
| 683 |
0 |
| hv_iterinit (hv); |
| 684 |
|
| } |
| 685 |
|
| |
| 686 |
219 |
▶ | if (count) |
Branch: 1 branch
| Branch | True | False | Total |
| if (count) |
213 |
6 |
219 |
|
| 687 |
|
| { |
| 688 |
213 |
| int i, fast = 1; |
| 689 |
|
| HE *hes_stack [STACK_HES]; |
| 690 |
213 |
| HE **hes = hes_stack; |
| 691 |
|
| |
| 692 |
|
| // allocate larger arrays on the heap |
| 693 |
213 |
▶ | if (count > STACK_HES) |
Branch: 1 branch
| Branch | True | False | Total |
| if (count > STACK_HES) |
0 |
213 |
213 |
|
| 694 |
|
| { |
| 695 |
0 |
| SV *sv = sv_2mortal (NEWSV (0, count * sizeof (*hes))); |
| 696 |
0 |
| hes = (HE **)SvPVX (sv); |
| 697 |
|
| } |
| 698 |
|
| |
| 699 |
213 |
| i = 0; |
| 700 |
730 |
▶ | while ((he = hv_iternext (hv))) |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext (hv))) |
517 |
213 |
730 |
|
| 701 |
|
| { |
| 702 |
517 |
| hes [i++] = he; |
| 703 |
517 |
▶ | if (HeKLEN (he) < 0 || HeKUTF8 (he)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (HeKLEN (he) < 0 || HeKUTF8 (he)) |
517 |
0 |
517 |
| if (HeKLEN (he) < 0 || HeKUTF8 (he)) |
3 |
514 |
517 |
|
| 704 |
3 |
| fast = 0; |
| 705 |
|
| } |
| 706 |
|
| |
| 707 |
|
| assert (i == count); |
| 708 |
|
| |
| 709 |
213 |
▶ | if (fast) |
Branch: 1 branch
| Branch | True | False | Total |
| if (fast) |
210 |
3 |
213 |
|
| 710 |
210 |
| qsort (hes, count, sizeof (HE *), he_cmp_fast); |
| 711 |
|
| else |
| 712 |
|
| { |
| 713 |
|
| // hack to forcefully disable "use bytes" |
| 714 |
3 |
| COP cop = *PL_curcop; |
| 715 |
3 |
| cop.op_private = 0; |
| 716 |
|
| |
| 717 |
3 |
| ENTER; |
| 718 |
3 |
| SAVETMPS; |
| 719 |
|
| |
| 720 |
3 |
| SAVEVPTR (PL_curcop); |
| 721 |
3 |
| PL_curcop = &cop; |
| 722 |
|
| |
| 723 |
3 |
| qsort (hes, count, sizeof (HE *), he_cmp_slow); |
| 724 |
|
| |
| 725 |
3 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
3 |
0 |
3 |
|
| 726 |
3 |
| LEAVE; |
| 727 |
|
| } |
| 728 |
|
| |
| 729 |
213 |
| encode_nl (enc); ++enc->indent; |
| 730 |
|
| |
| 731 |
730 |
▶ | while (count--) |
Branch: 1 branch
| Branch | True | False | Total |
| while (count--) |
517 |
213 |
730 |
|
| 732 |
|
| { |
| 733 |
517 |
| encode_indent (enc); |
| 734 |
517 |
| he = hes [count]; |
| 735 |
517 |
| encode_hk (enc, he); |
| 736 |
517 |
▶ | encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
Branch: 1 branch
| Branch | True | False | Total |
| encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
0 |
517 |
517 |
|
| 737 |
|
| |
| 738 |
517 |
▶ | if (count) |
Branch: 1 branch
| Branch | True | False | Total |
| if (count) |
304 |
213 |
517 |
|
| 739 |
304 |
| encode_comma (enc); |
| 740 |
|
| } |
| 741 |
|
| |
| 742 |
213 |
| encode_nl (enc); --enc->indent; encode_indent (enc); |
| 743 |
|
| } |
| 744 |
|
| } |
| 745 |
|
| else |
| 746 |
|
| { |
| 747 |
118 |
▶ | if (hv_iterinit (hv) || SvMAGICAL (hv)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (hv_iterinit (hv) || SvMAGICAL (hv)) |
5 |
113 |
118 |
| if (hv_iterinit (hv) || SvMAGICAL (hv)) |
1 |
4 |
5 |
|
| 748 |
114 |
▶ | if ((he = hv_iternext (hv))) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((he = hv_iternext (hv))) |
114 |
0 |
114 |
|
| 749 |
|
| { |
| 750 |
114 |
| encode_nl (enc); ++enc->indent; |
| 751 |
|
| |
| 752 |
|
| for (;;) |
| 753 |
|
| { |
| 754 |
117 |
| encode_indent (enc); |
| 755 |
117 |
| encode_hk (enc, he); |
| 756 |
117 |
▶ | encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
Branch: 1 branch
| Branch | True | False | Total |
| encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
1 |
116 |
117 |
|
| 757 |
|
| |
| 758 |
115 |
▶ | if (!(he = hv_iternext (hv))) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(he = hv_iternext (hv))) |
112 |
3 |
115 |
|
| 759 |
112 |
| break; |
| 760 |
|
| |
| 761 |
3 |
| encode_comma (enc); |
| 762 |
|
| } |
| 763 |
|
| |
| 764 |
112 |
| encode_nl (enc); --enc->indent; encode_indent (enc); |
| 765 |
|
| } |
| 766 |
|
| } |
| 767 |
|
| |
| 768 |
335 |
| encode_ch (enc, '}'); |
| 769 |
335 |
| } |
| 770 |
|
| |
| 771 |
|
| // encode objects, arrays and special \0=false and \1=true values. |
| 772 |
|
| static void |
| 773 |
25366 |
| encode_rv (enc_t *enc, SV *sv) |
| 774 |
|
| { |
| 775 |
|
| svtype svt; |
| 776 |
|
| GV *method; |
| 777 |
|
| |
| 778 |
25366 |
| SvGETMAGIC (sv); |
| 779 |
25366 |
| svt = SvTYPE (sv); |
| 780 |
|
| |
| 781 |
25366 |
▶ | if (expect_false (SvOBJECT (sv))) |
Branch: 1 branch
| Branch | True | False | Total |
| if (expect_false (SvOBJECT (sv))) |
24 |
25342 |
25366 |
|
| 782 |
|
| { |
| 783 |
24 |
| HV *stash = SvSTASH (sv); |
| 784 |
|
| |
| 785 |
24 |
▶ | if (stash == bool_stash) |
Branch: 1 branch
| Branch | True | False | Total |
| if (stash == bool_stash) |
16 |
8 |
24 |
|
| 786 |
|
| { |
| 787 |
16 |
▶ | if (SvIV (sv)) encode_str (enc, "true" , 4, 0); |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvIV (sv)) encode_str (enc, "true" , 4, 0); |
8 |
8 |
16 |
|
| 788 |
8 |
| else encode_str (enc, "false", 5, 0); |
| 789 |
|
| } |
| 790 |
8 |
▶ | else if ((enc->json.flags & F_ALLOW_TAGS) && (method = gv_fetchmethod_autoload (stash, "FREEZE", 0))) |
Branch: 2 branches
| Branch | True | False | Total |
| else if ((enc->json.flags & F_ALLOW_TAGS) && (method = gv_fetchmethod_autoload (stash, "FREEZE", 0))) |
2 |
6 |
8 |
| else if ((enc->json.flags & F_ALLOW_TAGS) && (method = gv_fetchmethod_autoload (stash, "FREEZE", 0))) |
1 |
1 |
2 |
|
| 791 |
1 |
| { |
| 792 |
|
| int count; |
| 793 |
1 |
| dSP; |
| 794 |
|
| |
| 795 |
1 |
| ENTER; SAVETMPS; |
| 796 |
1 |
▶ | PUSHMARK (SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK (SP); |
0 |
1 |
1 |
|
| 797 |
1 |
▶ | EXTEND (SP, 2); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND (SP, 2); |
0 |
1 |
1 |
|
| 798 |
|
| // we re-bless the reference to get overload and other niceties right |
| 799 |
1 |
| PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
| 800 |
1 |
| PUSHs (sv_json); |
| 801 |
|
| |
| 802 |
1 |
| PUTBACK; |
| 803 |
1 |
| count = call_sv ((SV *)GvCV (method), G_ARRAY); |
| 804 |
1 |
| SPAGAIN; |
| 805 |
|
| |
| 806 |
|
| // catch this surprisingly common error |
| 807 |
1 |
▶ | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK (TOPs) && SvRV (TOPs) == sv) |
0 |
1 |
1 |
| if (SvROK (TOPs) && SvRV (TOPs) == sv) |
0 |
0 |
0 |
|
| 808 |
0 |
▶ | croak ("%s::FREEZE method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
Branch: 6 branches
| Branch | True | False | Total |
| croak ("%s::FREEZE method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
0 |
0 |
0 |
| croak ("%s::FREEZE method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
0 |
0 |
0 |
| croak ("%s::FREEZE method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
0 |
0 |
0 |
| croak ("%s::FREEZE method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
0 |
0 |
0 |
| croak ("%s::FREEZE method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
0 |
0 |
0 |
| croak ("%s::FREEZE method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
0 |
0 |
0 |
|
| 809 |
|
| |
| 810 |
1 |
| encode_ch (enc, '('); |
| 811 |
1 |
| encode_ch (enc, '"'); |
| 812 |
1 |
▶ | encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); |
Branch: 19 branches
| Branch | True | False | Total |
| encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); |
1 |
0 |
1 |
| encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); |
1 |
0 |
1 |
| encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); |
0 |
1 |
1 |
| encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); |
0 |
0 |
0 |
| encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); |
1 |
0 |
1 |
| encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); |
0 |
1 |
1 |
| encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); |
0 |
1 |
1 |
| encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); |
1 |
0 |
1 |
| encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); |
1 |
0 |
1 |
| encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); |
0 |
1 |
1 |
| encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); |
0 |
0 |
0 |
| encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); |
1 |
0 |
1 |
| encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); |
0 |
1 |
1 |
| encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); |
1 |
0 |
1 |
| encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); |
1 |
0 |
1 |
| encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); |
0 |
1 |
1 |
| encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); |
0 |
0 |
0 |
| encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); |
1 |
0 |
1 |
| encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); |
0 |
1 |
1 |
|
| 813 |
1 |
| encode_ch (enc, '"'); |
| 814 |
1 |
| encode_ch (enc, ')'); |
| 815 |
1 |
| encode_ch (enc, '['); |
| 816 |
|
| |
| 817 |
1 |
▶ | if (count) |
Branch: 1 branch
| Branch | True | False | Total |
| if (count) |
1 |
0 |
1 |
|
| 818 |
|
| { |
| 819 |
|
| int i; |
| 820 |
|
| |
| 821 |
3 |
▶ | for (i = 0; i < count - 1; ++i) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < count - 1; ++i) |
2 |
1 |
3 |
|
| 822 |
|
| { |
| 823 |
2 |
| PUTBACK; |
| 824 |
2 |
| encode_sv (enc, SP[i + 1 - count]); |
| 825 |
2 |
| SPAGAIN; |
| 826 |
2 |
| encode_ch (enc, ','); |
| 827 |
|
| } |
| 828 |
|
| |
| 829 |
1 |
| PUTBACK; |
| 830 |
1 |
| encode_sv (enc, TOPs); |
| 831 |
1 |
| SPAGAIN; |
| 832 |
|
| |
| 833 |
1 |
| SP -= count; |
| 834 |
|
| } |
| 835 |
|
| |
| 836 |
1 |
| PUTBACK; |
| 837 |
|
| |
| 838 |
1 |
| encode_ch (enc, ']'); |
| 839 |
|
| |
| 840 |
1 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
1 |
0 |
1 |
|
| 841 |
|
| } |
| 842 |
7 |
▶ | else if ((enc->json.flags & F_CONV_BLESSED) && (method = gv_fetchmethod_autoload (stash, "TO_JSON", 0))) |
Branch: 2 branches
| Branch | True | False | Total |
| else if ((enc->json.flags & F_CONV_BLESSED) && (method = gv_fetchmethod_autoload (stash, "TO_JSON", 0))) |
3 |
4 |
7 |
| else if ((enc->json.flags & F_CONV_BLESSED) && (method = gv_fetchmethod_autoload (stash, "TO_JSON", 0))) |
2 |
1 |
3 |
|
| 843 |
2 |
| { |
| 844 |
2 |
| dSP; |
| 845 |
|
| |
| 846 |
2 |
| ENTER; SAVETMPS; |
| 847 |
2 |
▶ | PUSHMARK (SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK (SP); |
0 |
2 |
2 |
|
| 848 |
|
| // we re-bless the reference to get overload and other niceties right |
| 849 |
2 |
▶ | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
0 |
2 |
2 |
|
| 850 |
|
| |
| 851 |
|
| // calling with G_SCALAR ensures that we always get a 1 return value |
| 852 |
2 |
| PUTBACK; |
| 853 |
2 |
| call_sv ((SV *)GvCV (method), G_SCALAR); |
| 854 |
2 |
| SPAGAIN; |
| 855 |
|
| |
| 856 |
|
| // catch this surprisingly common error |
| 857 |
2 |
▶ | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK (TOPs) && SvRV (TOPs) == sv) |
1 |
1 |
2 |
| if (SvROK (TOPs) && SvRV (TOPs) == sv) |
0 |
1 |
1 |
|
| 858 |
0 |
▶ | croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
Branch: 6 branches
| Branch | True | False | Total |
| 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))); |
0 |
0 |
0 |
| 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))); |
0 |
0 |
0 |
| 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))); |
0 |
0 |
0 |
|
| 859 |
|
| |
| 860 |
2 |
| sv = POPs; |
| 861 |
2 |
| PUTBACK; |
| 862 |
|
| |
| 863 |
2 |
| encode_sv (enc, sv); |
| 864 |
|
| |
| 865 |
2 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
2 |
0 |
2 |
|
| 866 |
|
| } |
| 867 |
5 |
▶ | else if (enc->json.flags & F_ALLOW_BLESSED) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (enc->json.flags & F_ALLOW_BLESSED) |
3 |
2 |
5 |
|
| 868 |
3 |
| encode_str (enc, "null", 4, 0); |
| 869 |
|
| else |
| 870 |
4 |
| croak ("encountered object '%s', but neither allow_blessed, convert_blessed nor allow_tags settings are enabled (or TO_JSON/FREEZE method missing)", |
| 871 |
|
| SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
| 872 |
|
| } |
| 873 |
25342 |
▶ | else if (svt == SVt_PVHV) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (svt == SVt_PVHV) |
338 |
25004 |
25342 |
|
| 874 |
338 |
| encode_hv (enc, (HV *)sv); |
| 875 |
25004 |
▶ | else if (svt == SVt_PVAV) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (svt == SVt_PVAV) |
24994 |
10 |
25004 |
|
| 876 |
24994 |
| encode_av (enc, (AV *)sv); |
| 877 |
10 |
▶ | else if (svt < SVt_PVAV) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (svt < SVt_PVAV) |
9 |
1 |
10 |
|
| 878 |
|
| { |
| 879 |
9 |
| int bool_type = ref_bool_type (sv); |
| 880 |
|
| |
| 881 |
9 |
▶ | if (bool_type == 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (bool_type == 1) |
1 |
8 |
9 |
|
| 882 |
1 |
| encode_str (enc, "true", 4, 0); |
| 883 |
8 |
▶ | else if (bool_type == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (bool_type == 0) |
1 |
7 |
8 |
|
| 884 |
1 |
| encode_str (enc, "false", 5, 0); |
| 885 |
7 |
▶ | else if (enc->json.flags & F_ALLOW_UNKNOWN) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (enc->json.flags & F_ALLOW_UNKNOWN) |
0 |
7 |
7 |
|
| 886 |
0 |
| encode_str (enc, "null", 4, 0); |
| 887 |
|
| else |
| 888 |
14 |
| croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
| 889 |
|
| SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
| 890 |
|
| } |
| 891 |
1 |
▶ | else if (enc->json.flags & F_ALLOW_UNKNOWN) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (enc->json.flags & F_ALLOW_UNKNOWN) |
0 |
1 |
1 |
|
| 892 |
0 |
| encode_str (enc, "null", 4, 0); |
| 893 |
|
| else |
| 894 |
2 |
| croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
| 895 |
|
| SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
| 896 |
25343 |
| } |
| 897 |
|
| |
| 898 |
|
| static void |
| 899 |
51516 |
| encode_sv (enc_t *enc, SV *sv) |
| 900 |
|
| { |
| 901 |
51516 |
| SvGETMAGIC (sv); |
| 902 |
|
| |
| 903 |
51516 |
▶ | if (SvPOKp (sv)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvPOKp (sv)) |
25337 |
26179 |
51516 |
|
| 904 |
|
| { |
| 905 |
|
| STRLEN len; |
| 906 |
25337 |
| char *str = SvPV (sv, len); |
| 907 |
25337 |
| encode_ch (enc, '"'); |
| 908 |
25337 |
| encode_str (enc, str, len, SvUTF8 (sv)); |
| 909 |
25337 |
| encode_ch (enc, '"'); |
| 910 |
|
| } |
| 911 |
26179 |
▶ | else if (SvNOKp (sv)) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (SvNOKp (sv)) |
76 |
26103 |
26179 |
|
| 912 |
|
| { |
| 913 |
|
| // trust that perl will do the right thing w.r.t. JSON syntax. |
| 914 |
76 |
| need (enc, NV_DIG + 32); |
| 915 |
76 |
| Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
| 916 |
76 |
| enc->cur += strlen (enc->cur); |
| 917 |
|
| } |
| 918 |
26103 |
▶ | else if (SvIOKp (sv)) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (SvIOKp (sv)) |
695 |
25408 |
26103 |
|
| 919 |
|
| { |
| 920 |
|
| // we assume we can always read an IV as a UV and vice versa |
| 921 |
|
| // we assume two's complement |
| 922 |
|
| // we assume no aliasing issues in the union |
| 923 |
1390 |
▶ | if (SvIsUV (sv) ? SvUVX (sv) <= 59000 |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvIsUV (sv) ? SvUVX (sv) <= 59000 |
0 |
695 |
695 |
| if (SvIsUV (sv) ? SvUVX (sv) <= 59000 |
692 |
3 |
695 |
|
| 924 |
695 |
▶ | : SvIVX (sv) <= 59000 && SvIVX (sv) >= -59000) |
Branch: 2 branches
| Branch | True | False | Total |
| : SvIVX (sv) <= 59000 && SvIVX (sv) >= -59000) |
692 |
3 |
695 |
| : SvIVX (sv) <= 59000 && SvIVX (sv) >= -59000) |
692 |
0 |
692 |
|
| 925 |
|
| { |
| 926 |
|
| // optimise the "small number case" |
| 927 |
|
| // code will likely be branchless and use only a single multiplication |
| 928 |
|
| // 4.28 works for numbers up to 59074 |
| 929 |
692 |
| I32 i = SvIVX (sv); |
| 930 |
|
| U32 u; |
| 931 |
692 |
| char digit, nz = 0; |
| 932 |
|
| |
| 933 |
692 |
| need (enc, 6); |
| 934 |
|
| |
| 935 |
692 |
| *enc->cur = '-'; enc->cur += i < 0 ? 1 : 0; |
| 936 |
692 |
| u = i < 0 ? -i : i; // not undefined due to range check above |
| 937 |
|
| |
| 938 |
|
| // convert to 4.28 fixed-point representation |
| 939 |
692 |
| u = u * ((0xfffffff + 10000) / 10000); // 10**5, 5 fractional digits |
| 940 |
|
| |
| 941 |
|
| // now output digit by digit, each time masking out the integer part |
| 942 |
|
| // and multiplying by 5 while moving the decimal point one to the right, |
| 943 |
|
| // resulting in a net multiplication by 10. |
| 944 |
|
| // we always write the digit to memory but conditionally increment |
| 945 |
|
| // the pointer, to enable the use of conditional move instructions. |
| 946 |
692 |
▶ | digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffffUL) * 5; |
Branch: 2 branches
| Branch | True | False | Total |
| digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffffUL) * 5; |
692 |
0 |
692 |
| digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffffUL) * 5; |
6 |
686 |
692 |
|
| 947 |
692 |
▶ | digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffffUL) * 5; |
Branch: 2 branches
| Branch | True | False | Total |
| digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffffUL) * 5; |
686 |
6 |
692 |
| digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffffUL) * 5; |
3 |
683 |
686 |
|
| 948 |
692 |
▶ | digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffffUL) * 5; |
Branch: 2 branches
| Branch | True | False | Total |
| digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffffUL) * 5; |
683 |
9 |
692 |
| digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffffUL) * 5; |
0 |
683 |
683 |
|
| 949 |
692 |
▶ | digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffffUL) * 5; |
Branch: 2 branches
| Branch | True | False | Total |
| digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffffUL) * 5; |
683 |
9 |
692 |
| digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffffUL) * 5; |
5 |
678 |
683 |
|
| 950 |
692 |
| digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0' |
| 951 |
|
| } |
| 952 |
|
| else |
| 953 |
|
| { |
| 954 |
|
| // large integer, use the (rather slow) snprintf way. |
| 955 |
3 |
| need (enc, IVUV_MAXCHARS); |
| 956 |
3 |
| enc->cur += |
| 957 |
3 |
| SvIsUV(sv) |
| 958 |
0 |
| ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv)) |
| 959 |
3 |
▶ | : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv)); |
Branch: 1 branch
| Branch | True | False | Total |
| : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv)); |
0 |
3 |
3 |
|
| 960 |
|
| } |
| 961 |
|
| } |
| 962 |
25408 |
▶ | else if (SvROK (sv)) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (SvROK (sv)) |
25366 |
42 |
25408 |
|
| 963 |
25366 |
| encode_rv (enc, SvRV (sv)); |
| 964 |
42 |
▶ | else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN) |
Branch: 2 branches
| Branch | True | False | Total |
| else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN) |
0 |
42 |
42 |
| else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN) |
0 |
0 |
0 |
|
| 965 |
42 |
| encode_str (enc, "null", 4, 0); |
| 966 |
|
| else |
| 967 |
0 |
| croak ("encountered perl type (%s,0x%x) that JSON cannot handle, check your input data", |
| 968 |
|
| SvPV_nolen (sv), (unsigned int)SvFLAGS (sv)); |
| 969 |
51493 |
| } |
| 970 |
|
| |
| 971 |
|
| static SV * |
| 972 |
25103 |
| encode_json (SV *scalar, JSON *json) |
| 973 |
|
| { |
| 974 |
|
| enc_t enc; |
| 975 |
|
| |
| 976 |
25103 |
▶ | if (!(json->flags & F_ALLOW_NONREF) && json_nonref (scalar)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(json->flags & F_ALLOW_NONREF) && json_nonref (scalar)) |
15558 |
9545 |
25103 |
| if (!(json->flags & F_ALLOW_NONREF) && json_nonref (scalar)) |
0 |
15558 |
15558 |
|
| 977 |
0 |
| croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
| 978 |
|
| |
| 979 |
25103 |
| enc.json = *json; |
| 980 |
25103 |
| enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
| 981 |
25103 |
| enc.cur = SvPVX (enc.sv); |
| 982 |
25103 |
| enc.end = SvEND (enc.sv); |
| 983 |
25103 |
| enc.indent = 0; |
| 984 |
50206 |
| enc.limit = enc.json.flags & F_ASCII ? 0x000080UL |
| 985 |
37915 |
▶ | : enc.json.flags & F_LATIN1 ? 0x000100UL |
Branch: 1 branch
| Branch | True | False | Total |
| : enc.json.flags & F_LATIN1 ? 0x000100UL |
12812 |
12291 |
25103 |
|
| 986 |
12812 |
▶ | : 0x110000UL; |
Branch: 1 branch
| Branch | True | False | Total |
| : 0x110000UL; |
2 |
12810 |
12812 |
|
| 987 |
|
| |
| 988 |
25103 |
| SvPOK_only (enc.sv); |
| 989 |
25103 |
| encode_sv (&enc, scalar); |
| 990 |
25091 |
| encode_nl (&enc); |
| 991 |
|
| |
| 992 |
25091 |
| SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
| 993 |
25091 |
| *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
| 994 |
|
| |
| 995 |
25091 |
▶ | if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
6460 |
18631 |
25091 |
|
| 996 |
6460 |
| SvUTF8_on (enc.sv); |
| 997 |
|
| |
| 998 |
25091 |
▶ | if (enc.json.flags & F_SHRINK) |
Branch: 1 branch
| Branch | True | False | Total |
| if (enc.json.flags & F_SHRINK) |
9216 |
15875 |
25091 |
|
| 999 |
9216 |
| shrink (enc.sv); |
| 1000 |
|
| |
| 1001 |
25091 |
| return enc.sv; |
| 1002 |
|
| } |
| 1003 |
|
| |
| 1004 |
|
| ///////////////////////////////////////////////////////////////////////////// |
| 1005 |
|
| // decoder |
| 1006 |
|
| |
| 1007 |
|
| // structure used for decoding JSON |
| 1008 |
|
| typedef struct |
| 1009 |
|
| { |
| 1010 |
|
| char *cur; // current parser pointer |
| 1011 |
|
| char *end; // end of input string |
| 1012 |
|
| const char *err; // parse error, if != 0 |
| 1013 |
|
| JSON json; |
| 1014 |
|
| U32 depth; // recursion depth |
| 1015 |
|
| U32 maxdepth; // recursion depth limit |
| 1016 |
|
| } dec_t; |
| 1017 |
|
| |
| 1018 |
|
| INLINE void |
| 1019 |
9 |
| decode_comment (dec_t *dec) |
| 1020 |
|
| { |
| 1021 |
|
| // only '#'-style comments allowed a.t.m. |
| 1022 |
|
| |
| 1023 |
47 |
▶ | while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d) |
Branch: 3 branches
| Branch | True | False | Total |
| while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d) |
47 |
0 |
47 |
| while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d) |
38 |
9 |
47 |
| while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d) |
38 |
0 |
38 |
|
| 1024 |
38 |
| ++dec->cur; |
| 1025 |
9 |
| } |
| 1026 |
|
| |
| 1027 |
|
| INLINE void |
| 1028 |
109119 |
| decode_ws (dec_t *dec) |
| 1029 |
|
| { |
| 1030 |
|
| for (;;) |
| 1031 |
2234 |
| { |
| 1032 |
111353 |
| char ch = *dec->cur; |
| 1033 |
|
| |
| 1034 |
111353 |
▶ | if (ch > 0x20) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ch > 0x20) |
84282 |
27071 |
111353 |
|
| 1035 |
|
| { |
| 1036 |
84282 |
▶ | if (expect_false (ch == '#')) |
Branch: 1 branch
| Branch | True | False | Total |
| if (expect_false (ch == '#')) |
9 |
84273 |
84282 |
|
| 1037 |
|
| { |
| 1038 |
9 |
▶ | if (dec->json.flags & F_RELAXED) |
Branch: 1 branch
| Branch | True | False | Total |
| if (dec->json.flags & F_RELAXED) |
9 |
0 |
9 |
|
| 1039 |
9 |
| decode_comment (dec); |
| 1040 |
|
| else |
| 1041 |
0 |
| break; |
| 1042 |
|
| } |
| 1043 |
|
| else |
| 1044 |
84273 |
| break; |
| 1045 |
|
| } |
| 1046 |
27071 |
▶ | else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09) |
Branch: 4 branches
| Branch | True | False | Total |
| else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09) |
24920 |
2151 |
27071 |
| else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09) |
24847 |
73 |
24920 |
| else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09) |
24847 |
0 |
24847 |
| else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09) |
1 |
24846 |
24847 |
|
| 1047 |
|
| break; // parse error, but let higher level handle it, gives better error messages |
| 1048 |
|
| else |
| 1049 |
2225 |
| ++dec->cur; |
| 1050 |
|
| } |
| 1051 |
109119 |
| } |
| 1052 |
|
| |
| 1053 |
|
| #define ERR(reason) SB dec->err = reason; goto fail; SE |
| 1054 |
|
| |
| 1055 |
|
| #define EXPECT_CH(ch) SB \ |
| 1056 |
|
| if (*dec->cur != ch) \ |
| 1057 |
|
| ERR (# ch " expected"); \ |
| 1058 |
|
| ++dec->cur; \ |
| 1059 |
|
| SE |
| 1060 |
|
| |
| 1061 |
|
| #define DEC_INC_DEPTH if (++dec->depth > dec->json.max_depth) ERR (ERR_NESTING_EXCEEDED) |
| 1062 |
|
| #define DEC_DEC_DEPTH --dec->depth |
| 1063 |
|
| |
| 1064 |
|
| static SV *decode_sv (dec_t *dec); |
| 1065 |
|
| |
| 1066 |
|
| static signed char decode_hexdigit[256]; |
| 1067 |
|
| |
| 1068 |
|
| static UV |
| 1069 |
5148634 |
| decode_4hex (dec_t *dec) |
| 1070 |
|
| { |
| 1071 |
|
| signed char d1, d2, d3, d4; |
| 1072 |
5148634 |
| unsigned char *cur = (unsigned char *)dec->cur; |
| 1073 |
|
| |
| 1074 |
5148634 |
▶ | d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("exactly four hexadecimal digits expected"); |
Branch: 1 branch
| Branch | True | False | Total |
| d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("exactly four hexadecimal digits expected"); |
0 |
5148634 |
5148634 |
|
| 1075 |
5148634 |
▶ | d2 = decode_hexdigit [cur [1]]; if (expect_false (d2 < 0)) ERR ("exactly four hexadecimal digits expected"); |
Branch: 1 branch
| Branch | True | False | Total |
| d2 = decode_hexdigit [cur [1]]; if (expect_false (d2 < 0)) ERR ("exactly four hexadecimal digits expected"); |
0 |
5148634 |
5148634 |
|
| 1076 |
5148634 |
▶ | d3 = decode_hexdigit [cur [2]]; if (expect_false (d3 < 0)) ERR ("exactly four hexadecimal digits expected"); |
Branch: 1 branch
| Branch | True | False | Total |
| d3 = decode_hexdigit [cur [2]]; if (expect_false (d3 < 0)) ERR ("exactly four hexadecimal digits expected"); |
0 |
5148634 |
5148634 |
|
| 1077 |
5148634 |
▶ | d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("exactly four hexadecimal digits expected"); |
Branch: 1 branch
| Branch | True | False | Total |
| d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("exactly four hexadecimal digits expected"); |
0 |
5148634 |
5148634 |
|
| 1078 |
|
| |
| 1079 |
5148634 |
| dec->cur += 4; |
| 1080 |
|
| |
| 1081 |
5148634 |
| return ((UV)d1) << 12 |
| 1082 |
5148634 |
| | ((UV)d2) << 8 |
| 1083 |
5148634 |
| | ((UV)d3) << 4 |
| 1084 |
5148634 |
| | ((UV)d4); |
| 1085 |
|
| |
| 1086 |
0 |
| fail: |
| 1087 |
0 |
| return (UV)-1; |
| 1088 |
|
| } |
| 1089 |
|
| |
| 1090 |
|
| static SV * |
| 1091 |
25520 |
| decode_str (dec_t *dec) |
| 1092 |
|
| { |
| 1093 |
25520 |
| SV *sv = 0; |
| 1094 |
25520 |
| int utf8 = 0; |
| 1095 |
25520 |
| char *dec_cur = dec->cur; |
| 1096 |
|
| |
| 1097 |
|
| do |
| 1098 |
|
| { |
| 1099 |
|
| char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
| 1100 |
25520 |
| char *cur = buf; |
| 1101 |
|
| |
| 1102 |
|
| do |
| 1103 |
|
| { |
| 1104 |
9502874 |
| unsigned char ch = *(unsigned char *)dec_cur++; |
| 1105 |
|
| |
| 1106 |
9502874 |
▶ | if (expect_false (ch == '"')) |
Branch: 1 branch
| Branch | True | False | Total |
| if (expect_false (ch == '"')) |
25505 |
9477369 |
9502874 |
|
| 1107 |
|
| { |
| 1108 |
25505 |
| --dec_cur; |
| 1109 |
25505 |
| break; |
| 1110 |
|
| } |
| 1111 |
9477369 |
▶ | else if (expect_false (ch == '\\')) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (expect_false (ch == '\\')) |
4174274 |
5303095 |
9477369 |
|
| 1112 |
|
| { |
| 1113 |
4174274 |
| switch (*dec_cur) |
| 1114 |
|
| { |
| 1115 |
40505 |
| case '\\': |
| 1116 |
|
| case '/': |
| 1117 |
40505 |
| case '"': *cur++ = *dec_cur++; break; |
| 1118 |
|
| |
| 1119 |
21213 |
| case 'b': ++dec_cur; *cur++ = '\010'; break; |
| 1120 |
25933 |
| case 't': ++dec_cur; *cur++ = '\011'; break; |
| 1121 |
22278 |
| case 'n': ++dec_cur; *cur++ = '\012'; break; |
| 1122 |
21845 |
| case 'f': ++dec_cur; *cur++ = '\014'; break; |
| 1123 |
21597 |
| case 'r': ++dec_cur; *cur++ = '\015'; break; |
| 1124 |
|
| |
| 1125 |
4020899 |
| case 'u': |
| 1126 |
|
| { |
| 1127 |
|
| UV lo, hi; |
| 1128 |
4020899 |
| ++dec_cur; |
| 1129 |
|
| |
| 1130 |
4020899 |
| dec->cur = dec_cur; |
| 1131 |
4020899 |
| hi = decode_4hex (dec); |
| 1132 |
4020899 |
| dec_cur = dec->cur; |
| 1133 |
4020899 |
▶ | if (hi == (UV)-1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (hi == (UV)-1) |
0 |
4020899 |
4020899 |
|
| 1134 |
15 |
| goto fail; |
| 1135 |
|
| |
| 1136 |
|
| // possibly a surrogate pair |
| 1137 |
4020899 |
▶ | if (hi >= 0xd800) |
Branch: 1 branch
| Branch | True | False | Total |
| if (hi >= 0xd800) |
1136912 |
2883987 |
4020899 |
|
| 1138 |
1136912 |
▶ | if (hi < 0xdc00) |
Branch: 1 branch
| Branch | True | False | Total |
| if (hi < 0xdc00) |
1127736 |
9176 |
1136912 |
|
| 1139 |
|
| { |
| 1140 |
1127736 |
▶ | if (dec_cur [0] != '\\' || dec_cur [1] != 'u') |
Branch: 2 branches
| Branch | True | False | Total |
| if (dec_cur [0] != '\\' || dec_cur [1] != 'u') |
1127735 |
1 |
1127736 |
| if (dec_cur [0] != '\\' || dec_cur [1] != 'u') |
0 |
1127735 |
1127735 |
|
| 1141 |
1 |
| ERR ("missing low surrogate character in surrogate pair"); |
| 1142 |
|
| |
| 1143 |
1127735 |
| dec_cur += 2; |
| 1144 |
|
| |
| 1145 |
1127735 |
| dec->cur = dec_cur; |
| 1146 |
1127735 |
| lo = decode_4hex (dec); |
| 1147 |
1127735 |
| dec_cur = dec->cur; |
| 1148 |
1127735 |
▶ | if (lo == (UV)-1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (lo == (UV)-1) |
0 |
1127735 |
1127735 |
|
| 1149 |
0 |
| goto fail; |
| 1150 |
|
| |
| 1151 |
1127735 |
▶ | if (lo < 0xdc00 || lo >= 0xe000) |
Branch: 2 branches
| Branch | True | False | Total |
| if (lo < 0xdc00 || lo >= 0xe000) |
1127734 |
1 |
1127735 |
| if (lo < 0xdc00 || lo >= 0xe000) |
0 |
1127734 |
1127734 |
|
| 1152 |
1 |
| ERR ("surrogate pair expected"); |
| 1153 |
|
| |
| 1154 |
1127734 |
| hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000; |
| 1155 |
|
| } |
| 1156 |
9176 |
▶ | else if (hi < 0xe000) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (hi < 0xe000) |
1 |
9175 |
9176 |
|
| 1157 |
1 |
| ERR ("missing high surrogate character in surrogate pair"); |
| 1158 |
|
| |
| 1159 |
4020896 |
▶ | if (hi >= 0x80) |
Branch: 1 branch
| Branch | True | False | Total |
| if (hi >= 0x80) |
3440235 |
580661 |
4020896 |
|
| 1160 |
|
| { |
| 1161 |
3440235 |
| utf8 = 1; |
| 1162 |
|
| |
| 1163 |
3440235 |
| cur = encode_utf8 (cur, hi); |
| 1164 |
|
| } |
| 1165 |
|
| else |
| 1166 |
580661 |
| *cur++ = hi; |
| 1167 |
|
| } |
| 1168 |
4020896 |
| break; |
| 1169 |
|
| |
| 1170 |
4 |
| default: |
| 1171 |
4 |
| --dec_cur; |
| 1172 |
4 |
| ERR ("illegal backslash escape sequence in string"); |
| 1173 |
|
| } |
| 1174 |
|
| } |
| 1175 |
5303095 |
▶ | else if (expect_true (ch >= 0x20 && ch < 0x80)) |
Branch: 3 branches
| Branch | True | False | Total |
| else if (expect_true (ch >= 0x20 && ch < 0x80)) |
5303090 |
5 |
5303095 |
| else if (expect_true (ch >= 0x20 && ch < 0x80)) |
1863039 |
3440051 |
5303090 |
| else if (expect_true (ch >= 0x20 && ch < 0x80)) |
1863039 |
3440056 |
5303095 |
|
| 1176 |
1863039 |
| *cur++ = ch; |
| 1177 |
3440056 |
▶ | else if (ch >= 0x80) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (ch >= 0x80) |
3440051 |
5 |
3440056 |
|
| 1178 |
|
| { |
| 1179 |
|
| STRLEN clen; |
| 1180 |
|
| |
| 1181 |
3440051 |
| --dec_cur; |
| 1182 |
|
| |
| 1183 |
3440051 |
| decode_utf8 (dec_cur, dec->end - dec_cur, &clen); |
| 1184 |
3440051 |
▶ | if (clen == (STRLEN)-1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (clen == (STRLEN)-1) |
3 |
3440048 |
3440051 |
|
| 1185 |
3 |
| ERR ("malformed UTF-8 character in JSON string"); |
| 1186 |
|
| |
| 1187 |
|
| do |
| 1188 |
10253127 |
| *cur++ = *dec_cur++; |
| 1189 |
10253127 |
▶ | while (--clen); |
Branch: 1 branch
| Branch | True | False | Total |
| while (--clen); |
6813079 |
3440048 |
10253127 |
|
| 1190 |
|
| |
| 1191 |
3440048 |
| utf8 = 1; |
| 1192 |
|
| } |
| 1193 |
5 |
▶ | else if (ch == '\t' && dec->json.flags & F_RELAXED) |
Branch: 2 branches
| Branch | True | False | Total |
| else if (ch == '\t' && dec->json.flags & F_RELAXED) |
1 |
4 |
5 |
| else if (ch == '\t' && dec->json.flags & F_RELAXED) |
0 |
1 |
1 |
|
| 1194 |
0 |
| *cur++ = ch; |
| 1195 |
|
| else |
| 1196 |
|
| { |
| 1197 |
5 |
| --dec_cur; |
| 1198 |
|
| |
| 1199 |
5 |
▶ | if (!ch) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ch) |
1 |
4 |
5 |
|
| 1200 |
1 |
| ERR ("unexpected end of string while parsing JSON string"); |
| 1201 |
|
| else |
| 1202 |
4 |
| ERR ("invalid character encountered while parsing JSON string"); |
| 1203 |
|
| } |
| 1204 |
|
| } |
| 1205 |
9477354 |
▶ | while (cur < buf + SHORT_STRING_LEN); |
Branch: 1 branch
| Branch | True | False | Total |
| while (cur < buf + SHORT_STRING_LEN); |
9477354 |
0 |
9477354 |
|
| 1206 |
|
| |
| 1207 |
|
| { |
| 1208 |
25505 |
| STRLEN len = cur - buf; |
| 1209 |
|
| |
| 1210 |
25505 |
▶ | if (sv) |
Branch: 1 branch
| Branch | True | False | Total |
| if (sv) |
0 |
25505 |
25505 |
|
| 1211 |
|
| { |
| 1212 |
0 |
| STRLEN cur = SvCUR (sv); |
| 1213 |
|
| |
| 1214 |
0 |
▶ | if (SvLEN (sv) - cur <= len) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvLEN (sv) - cur <= len) |
0 |
0 |
0 |
|
| 1215 |
0 |
| json_sv_grow (sv, cur, len); |
| 1216 |
|
| |
| 1217 |
0 |
| memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
| 1218 |
0 |
| SvCUR_set (sv, SvCUR (sv) + len); |
| 1219 |
|
| } |
| 1220 |
|
| else |
| 1221 |
25505 |
| sv = newSVpvn (buf, len); |
| 1222 |
|
| } |
| 1223 |
|
| } |
| 1224 |
25505 |
▶ | while (*dec_cur != '"'); |
Branch: 1 branch
| Branch | True | False | Total |
| while (*dec_cur != '"'); |
0 |
25505 |
25505 |
|
| 1225 |
|
| |
| 1226 |
25505 |
| ++dec_cur; |
| 1227 |
|
| |
| 1228 |
25505 |
▶ | if (sv) |
Branch: 1 branch
| Branch | True | False | Total |
| if (sv) |
25505 |
0 |
25505 |
|
| 1229 |
|
| { |
| 1230 |
25505 |
| SvPOK_only (sv); |
| 1231 |
25505 |
| *SvEND (sv) = 0; |
| 1232 |
|
| |
| 1233 |
25505 |
▶ | if (utf8) |
Branch: 1 branch
| Branch | True | False | Total |
| if (utf8) |
23764 |
1741 |
25505 |
|
| 1234 |
23764 |
| SvUTF8_on (sv); |
| 1235 |
|
| } |
| 1236 |
|
| else |
| 1237 |
0 |
| sv = newSVpvn ("", 0); |
| 1238 |
|
| |
| 1239 |
25505 |
| dec->cur = dec_cur; |
| 1240 |
25505 |
| return sv; |
| 1241 |
|
| |
| 1242 |
15 |
| fail: |
| 1243 |
15 |
| SvREFCNT_dec (sv); |
| 1244 |
15 |
| dec->cur = dec_cur; |
| 1245 |
15 |
| return 0; |
| 1246 |
|
| } |
| 1247 |
|
| |
| 1248 |
|
| static SV * |
| 1249 |
778 |
| decode_num (dec_t *dec) |
| 1250 |
|
| { |
| 1251 |
778 |
| int is_nv = 0; |
| 1252 |
778 |
| char *start = dec->cur; |
| 1253 |
|
| |
| 1254 |
|
| // [minus] |
| 1255 |
778 |
▶ | if (*dec->cur == '-') |
Branch: 1 branch
| Branch | True | False | Total |
| if (*dec->cur == '-') |
32 |
746 |
778 |
|
| 1256 |
32 |
| ++dec->cur; |
| 1257 |
|
| |
| 1258 |
778 |
▶ | if (*dec->cur == '0') |
Branch: 1 branch
| Branch | True | False | Total |
| if (*dec->cur == '0') |
43 |
735 |
778 |
|
| 1259 |
|
| { |
| 1260 |
43 |
| ++dec->cur; |
| 1261 |
43 |
▶ | if (*dec->cur >= '0' && *dec->cur <= '9') |
Branch: 2 branches
| Branch | True | False | Total |
| if (*dec->cur >= '0' && *dec->cur <= '9') |
10 |
33 |
43 |
| if (*dec->cur >= '0' && *dec->cur <= '9') |
3 |
7 |
10 |
|
| 1262 |
3 |
| ERR ("malformed number (leading zero must not be followed by another digit)"); |
| 1263 |
|
| } |
| 1264 |
735 |
▶ | else if (*dec->cur < '0' || *dec->cur > '9') |
Branch: 2 branches
| Branch | True | False | Total |
| else if (*dec->cur < '0' || *dec->cur > '9') |
735 |
0 |
735 |
| else if (*dec->cur < '0' || *dec->cur > '9') |
1 |
734 |
735 |
|
| 1265 |
1 |
| ERR ("malformed number (no digits after initial minus)"); |
| 1266 |
|
| else |
| 1267 |
|
| do |
| 1268 |
|
| { |
| 1269 |
880 |
| ++dec->cur; |
| 1270 |
|
| } |
| 1271 |
880 |
▶ | while (*dec->cur >= '0' && *dec->cur <= '9'); |
Branch: 2 branches
| Branch | True | False | Total |
| while (*dec->cur >= '0' && *dec->cur <= '9'); |
326 |
554 |
880 |
| while (*dec->cur >= '0' && *dec->cur <= '9'); |
146 |
180 |
326 |
|
| 1272 |
|
| |
| 1273 |
|
| // [frac] |
| 1274 |
774 |
▶ | if (*dec->cur == '.') |
Branch: 1 branch
| Branch | True | False | Total |
| if (*dec->cur == '.') |
47 |
727 |
774 |
|
| 1275 |
|
| { |
| 1276 |
47 |
| ++dec->cur; |
| 1277 |
|
| |
| 1278 |
47 |
▶ | if (*dec->cur < '0' || *dec->cur > '9') |
Branch: 2 branches
| Branch | True | False | Total |
| if (*dec->cur < '0' || *dec->cur > '9') |
46 |
1 |
47 |
| if (*dec->cur < '0' || *dec->cur > '9') |
0 |
46 |
46 |
|
| 1279 |
1 |
| ERR ("malformed number (no digits after decimal point)"); |
| 1280 |
|
| |
| 1281 |
|
| do |
| 1282 |
|
| { |
| 1283 |
124 |
| ++dec->cur; |
| 1284 |
|
| } |
| 1285 |
124 |
▶ | while (*dec->cur >= '0' && *dec->cur <= '9'); |
Branch: 2 branches
| Branch | True | False | Total |
| while (*dec->cur >= '0' && *dec->cur <= '9'); |
112 |
12 |
124 |
| while (*dec->cur >= '0' && *dec->cur <= '9'); |
78 |
34 |
112 |
|
| 1286 |
|
| |
| 1287 |
46 |
| is_nv = 1; |
| 1288 |
|
| } |
| 1289 |
|
| |
| 1290 |
|
| // [exp] |
| 1291 |
773 |
▶ | if (*dec->cur == 'e' || *dec->cur == 'E') |
Branch: 2 branches
| Branch | True | False | Total |
| if (*dec->cur == 'e' || *dec->cur == 'E') |
741 |
32 |
773 |
| if (*dec->cur == 'e' || *dec->cur == 'E') |
25 |
716 |
741 |
|
| 1292 |
|
| { |
| 1293 |
57 |
| ++dec->cur; |
| 1294 |
|
| |
| 1295 |
57 |
▶ | if (*dec->cur == '-' || *dec->cur == '+') |
Branch: 2 branches
| Branch | True | False | Total |
| if (*dec->cur == '-' || *dec->cur == '+') |
51 |
6 |
57 |
| if (*dec->cur == '-' || *dec->cur == '+') |
31 |
20 |
51 |
|
| 1296 |
37 |
| ++dec->cur; |
| 1297 |
|
| |
| 1298 |
57 |
▶ | if (*dec->cur < '0' || *dec->cur > '9') |
Branch: 2 branches
| Branch | True | False | Total |
| if (*dec->cur < '0' || *dec->cur > '9') |
55 |
2 |
57 |
| if (*dec->cur < '0' || *dec->cur > '9') |
2 |
53 |
55 |
|
| 1299 |
4 |
| ERR ("malformed number (no digits after exp sign)"); |
| 1300 |
|
| |
| 1301 |
|
| do |
| 1302 |
|
| { |
| 1303 |
85 |
| ++dec->cur; |
| 1304 |
|
| } |
| 1305 |
85 |
▶ | while (*dec->cur >= '0' && *dec->cur <= '9'); |
Branch: 2 branches
| Branch | True | False | Total |
| while (*dec->cur >= '0' && *dec->cur <= '9'); |
35 |
50 |
85 |
| while (*dec->cur >= '0' && *dec->cur <= '9'); |
32 |
3 |
35 |
|
| 1306 |
|
| |
| 1307 |
53 |
| is_nv = 1; |
| 1308 |
|
| } |
| 1309 |
|
| |
| 1310 |
769 |
▶ | if (!is_nv) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!is_nv) |
701 |
68 |
769 |
|
| 1311 |
|
| { |
| 1312 |
701 |
| int len = dec->cur - start; |
| 1313 |
|
| |
| 1314 |
|
| // special case the rather common 1..5-digit-int case |
| 1315 |
701 |
▶ | if (*start == '-') |
Branch: 1 branch
| Branch | True | False | Total |
| if (*start == '-') |
13 |
688 |
701 |
|
| 1316 |
13 |
| switch (len) |
| 1317 |
|
| { |
| 1318 |
6 |
| case 2: return newSViv (-(IV)( start [1] - '0' * 1)); |
| 1319 |
4 |
| case 3: return newSViv (-(IV)( start [1] * 10 + start [2] - '0' * 11)); |
| 1320 |
1 |
| case 4: return newSViv (-(IV)( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
| 1321 |
0 |
| case 5: return newSViv (-(IV)( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
| 1322 |
0 |
| case 6: return newSViv (-(IV)(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111)); |
| 1323 |
|
| } |
| 1324 |
|
| else |
| 1325 |
688 |
| switch (len) |
| 1326 |
|
| { |
| 1327 |
663 |
| case 1: return newSViv ( start [0] - '0' * 1); |
| 1328 |
4 |
| case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
| 1329 |
3 |
| case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
| 1330 |
3 |
| case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
| 1331 |
7 |
| case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111); |
| 1332 |
|
| } |
| 1333 |
|
| |
| 1334 |
|
| { |
| 1335 |
|
| UV uv; |
| 1336 |
10 |
| int numtype = grok_number (start, len, &uv); |
| 1337 |
10 |
▶ | if (numtype & IS_NUMBER_IN_UV) |
Branch: 1 branch
| Branch | True | False | Total |
| if (numtype & IS_NUMBER_IN_UV) |
10 |
0 |
10 |
|
| 1338 |
10 |
▶ | if (numtype & IS_NUMBER_NEG) |
Branch: 1 branch
| Branch | True | False | Total |
| if (numtype & IS_NUMBER_NEG) |
2 |
8 |
10 |
|
| 1339 |
|
| { |
| 1340 |
2 |
▶ | if (uv < (UV)IV_MIN) |
Branch: 1 branch
| Branch | True | False | Total |
| if (uv < (UV)IV_MIN) |
2 |
0 |
2 |
|
| 1341 |
10 |
| return newSViv (-(IV)uv); |
| 1342 |
|
| } |
| 1343 |
|
| else |
| 1344 |
8 |
| return newSVuv (uv); |
| 1345 |
|
| } |
| 1346 |
|
| |
| 1347 |
0 |
| len -= *start == '-' ? 1 : 0; |
| 1348 |
|
| |
| 1349 |
|
| // does not fit into IV or UV, try NV |
| 1350 |
0 |
▶ | if (len <= NV_DIG) |
Branch: 1 branch
| Branch | True | False | Total |
| if (len <= NV_DIG) |
0 |
0 |
0 |
|
| 1351 |
|
| // fits into NV without loss of precision |
| 1352 |
0 |
| return newSVnv (json_atof (start)); |
| 1353 |
|
| |
| 1354 |
|
| // everything else fails, convert it to a string |
| 1355 |
0 |
| return newSVpvn (start, dec->cur - start); |
| 1356 |
|
| } |
| 1357 |
|
| |
| 1358 |
|
| // loss of precision here |
| 1359 |
68 |
| return newSVnv (json_atof (start)); |
| 1360 |
|
| |
| 1361 |
9 |
| fail: |
| 1362 |
9 |
| return 0; |
| 1363 |
|
| } |
| 1364 |
|
| |
| 1365 |
|
| static SV * |
| 1366 |
26105 |
| decode_av (dec_t *dec) |
| 1367 |
|
| { |
| 1368 |
26105 |
| AV *av = newAV (); |
| 1369 |
|
| |
| 1370 |
26105 |
▶ | DEC_INC_DEPTH; |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_INC_DEPTH; |
3 |
26102 |
26105 |
|
| 1371 |
26102 |
| decode_ws (dec); |
| 1372 |
|
| |
| 1373 |
26102 |
▶ | if (*dec->cur == ']') |
Branch: 1 branch
| Branch | True | False | Total |
| if (*dec->cur == ']') |
26079 |
23 |
26102 |
|
| 1374 |
23 |
| ++dec->cur; |
| 1375 |
|
| else |
| 1376 |
|
| for (;;) |
| 1377 |
754 |
| { |
| 1378 |
|
| SV *value; |
| 1379 |
|
| |
| 1380 |
26833 |
| value = decode_sv (dec); |
| 1381 |
26833 |
▶ | if (!value) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!value) |
563 |
26270 |
26833 |
|
| 1382 |
563 |
| goto fail; |
| 1383 |
|
| |
| 1384 |
26270 |
| av_push (av, value); |
| 1385 |
|
| |
| 1386 |
26270 |
| decode_ws (dec); |
| 1387 |
|
| |
| 1388 |
26270 |
▶ | if (*dec->cur == ']') |
Branch: 1 branch
| Branch | True | False | Total |
| if (*dec->cur == ']') |
25510 |
760 |
26270 |
|
| 1389 |
|
| { |
| 1390 |
25510 |
| ++dec->cur; |
| 1391 |
25510 |
| break; |
| 1392 |
|
| } |
| 1393 |
|
| |
| 1394 |
760 |
▶ | if (*dec->cur != ',') |
Branch: 1 branch
| Branch | True | False | Total |
| if (*dec->cur != ',') |
4 |
756 |
760 |
|
| 1395 |
4 |
| ERR (", or ] expected while parsing array"); |
| 1396 |
|
| |
| 1397 |
756 |
| ++dec->cur; |
| 1398 |
|
| |
| 1399 |
756 |
| decode_ws (dec); |
| 1400 |
|
| |
| 1401 |
756 |
▶ | if (*dec->cur == ']' && dec->json.flags & F_RELAXED) |
Branch: 2 branches
| Branch | True | False | Total |
| if (*dec->cur == ']' && dec->json.flags & F_RELAXED) |
3 |
753 |
756 |
| if (*dec->cur == ']' && dec->json.flags & F_RELAXED) |
2 |
1 |
3 |
|
| 1402 |
|
| { |
| 1403 |
2 |
| ++dec->cur; |
| 1404 |
2 |
| break; |
| 1405 |
|
| } |
| 1406 |
|
| } |
| 1407 |
|
| |
| 1408 |
25535 |
| DEC_DEC_DEPTH; |
| 1409 |
25535 |
| return newRV_noinc ((SV *)av); |
| 1410 |
|
| |
| 1411 |
570 |
| fail: |
| 1412 |
570 |
| SvREFCNT_dec (av); |
| 1413 |
570 |
| DEC_DEC_DEPTH; |
| 1414 |
570 |
| return 0; |
| 1415 |
|
| } |
| 1416 |
|
| |
| 1417 |
|
| static SV * |
| 1418 |
1354 |
| decode_hv (dec_t *dec) |
| 1419 |
|
| { |
| 1420 |
|
| SV *sv; |
| 1421 |
1354 |
| HV *hv = newHV (); |
| 1422 |
|
| |
| 1423 |
1354 |
▶ | DEC_INC_DEPTH; |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_INC_DEPTH; |
0 |
1354 |
1354 |
|
| 1424 |
1354 |
| decode_ws (dec); |
| 1425 |
|
| |
| 1426 |
1354 |
▶ | if (*dec->cur == '}') |
Branch: 1 branch
| Branch | True | False | Total |
| if (*dec->cur == '}') |
8 |
1346 |
1354 |
|
| 1427 |
8 |
| ++dec->cur; |
| 1428 |
|
| else |
| 1429 |
|
| for (;;) |
| 1430 |
|
| { |
| 1431 |
1619 |
▶ | EXPECT_CH ('"'); |
Branch: 1 branch
| Branch | True | False | Total |
| EXPECT_CH ('"'); |
4 |
1615 |
1619 |
|
| 1432 |
|
| |
| 1433 |
|
| // heuristic: assume that |
| 1434 |
|
| // a) decode_str + hv_store_ent are abysmally slow. |
| 1435 |
|
| // b) most hash keys are short, simple ascii text. |
| 1436 |
|
| // => try to "fast-match" such strings to avoid |
| 1437 |
|
| // the overhead of decode_str + hv_store_ent. |
| 1438 |
|
| { |
| 1439 |
|
| SV *value; |
| 1440 |
1615 |
| char *p = dec->cur; |
| 1441 |
1615 |
| char *e = p + 24; // only try up to 24 bytes |
| 1442 |
|
| |
| 1443 |
|
| for (;;) |
| 1444 |
|
| { |
| 1445 |
|
| // the >= 0x80 is false on most architectures |
| 1446 |
2440 |
▶ | if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') |
Branch: 3 branches
| Branch | True | False | Total |
| if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') |
2438 |
2 |
2440 |
| if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') |
2438 |
0 |
2438 |
| if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') |
200 |
2238 |
2438 |
|
| 1447 |
|
| { |
| 1448 |
|
| // slow path, back up and use decode_str |
| 1449 |
202 |
| SV *key = decode_str (dec); |
| 1450 |
202 |
▶ | if (!key) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!key) |
0 |
202 |
202 |
|
| 1451 |
0 |
| goto fail; |
| 1452 |
|
| |
| 1453 |
202 |
| decode_ws (dec); |
| 1454 |
|
| |
| 1455 |
202 |
▶ | if (*dec->cur != ':') // expect ':' |
Branch: 1 branch
| Branch | True | False | Total |
| if (*dec->cur != ':') // expect ':' |
0 |
202 |
202 |
|
| 1456 |
|
| { |
| 1457 |
0 |
| SvREFCNT_dec_NN (key); |
| 1458 |
0 |
| ERR (":" " expected"); |
| 1459 |
|
| } |
| 1460 |
|
| |
| 1461 |
202 |
| ++dec->cur; |
| 1462 |
|
| |
| 1463 |
202 |
| decode_ws (dec); |
| 1464 |
202 |
| value = decode_sv (dec); |
| 1465 |
202 |
▶ | if (!value) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!value) |
1 |
201 |
202 |
|
| 1466 |
|
| { |
| 1467 |
1 |
| SvREFCNT_dec (key); |
| 1468 |
1 |
| goto fail; |
| 1469 |
|
| } |
| 1470 |
|
| |
| 1471 |
201 |
| hv_store_ent (hv, key, value, 0); |
| 1472 |
201 |
| SvREFCNT_dec (key); |
| 1473 |
|
| |
| 1474 |
201 |
| break; |
| 1475 |
|
| } |
| 1476 |
2238 |
▶ | else if (*p == '"') |
Branch: 1 branch
| Branch | True | False | Total |
| else if (*p == '"') |
1413 |
825 |
2238 |
|
| 1477 |
|
| { |
| 1478 |
|
| // fast path, got a simple key |
| 1479 |
1413 |
| char *key = dec->cur; |
| 1480 |
1413 |
| int len = p - key; |
| 1481 |
1413 |
| dec->cur = p + 1; |
| 1482 |
|
| |
| 1483 |
1413 |
▶ | decode_ws (dec); EXPECT_CH (':'); |
Branch: 1 branch
| Branch | True | False | Total |
| decode_ws (dec); EXPECT_CH (':'); |
3 |
1410 |
1413 |
|
| 1484 |
|
| |
| 1485 |
1410 |
| decode_ws (dec); |
| 1486 |
1410 |
| value = decode_sv (dec); |
| 1487 |
1410 |
▶ | if (!value) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!value) |
515 |
895 |
1410 |
|
| 1488 |
515 |
| goto fail; |
| 1489 |
|
| |
| 1490 |
895 |
| hv_store (hv, key, len, value, 0); |
| 1491 |
|
| |
| 1492 |
895 |
| break; |
| 1493 |
|
| } |
| 1494 |
|
| |
| 1495 |
825 |
| ++p; |
| 1496 |
|
| } |
| 1497 |
|
| } |
| 1498 |
|
| |
| 1499 |
1096 |
| decode_ws (dec); |
| 1500 |
|
| |
| 1501 |
1096 |
▶ | if (*dec->cur == '}') |
Branch: 1 branch
| Branch | True | False | Total |
| if (*dec->cur == '}') |
819 |
277 |
1096 |
|
| 1502 |
|
| { |
| 1503 |
819 |
| ++dec->cur; |
| 1504 |
819 |
| break; |
| 1505 |
|
| } |
| 1506 |
|
| |
| 1507 |
277 |
▶ | if (*dec->cur != ',') |
Branch: 1 branch
| Branch | True | False | Total |
| if (*dec->cur != ',') |
3 |
274 |
277 |
|
| 1508 |
3 |
| ERR (", or } expected while parsing object/hash"); |
| 1509 |
|
| |
| 1510 |
274 |
| ++dec->cur; |
| 1511 |
|
| |
| 1512 |
274 |
| decode_ws (dec); |
| 1513 |
|
| |
| 1514 |
274 |
▶ | if (*dec->cur == '}' && dec->json.flags & F_RELAXED) |
Branch: 2 branches
| Branch | True | False | Total |
| if (*dec->cur == '}' && dec->json.flags & F_RELAXED) |
2 |
272 |
274 |
| if (*dec->cur == '}' && dec->json.flags & F_RELAXED) |
1 |
1 |
2 |
|
| 1515 |
|
| { |
| 1516 |
1 |
| ++dec->cur; |
| 1517 |
1 |
| break; |
| 1518 |
|
| } |
| 1519 |
|
| } |
| 1520 |
|
| |
| 1521 |
828 |
| DEC_DEC_DEPTH; |
| 1522 |
828 |
| sv = newRV_noinc ((SV *)hv); |
| 1523 |
|
| |
| 1524 |
|
| // check filter callbacks |
| 1525 |
828 |
▶ | if (expect_false (dec->json.flags & F_HOOK)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (expect_false (dec->json.flags & F_HOOK)) |
9 |
819 |
828 |
|
| 1526 |
|
| { |
| 1527 |
9 |
▶ | if (dec->json.cb_sk_object && HvKEYS (hv) == 1) |
Branch: 3 branches
| Branch | True | False | Total |
| if (dec->json.cb_sk_object && HvKEYS (hv) == 1) |
9 |
0 |
9 |
| if (dec->json.cb_sk_object && HvKEYS (hv) == 1) |
0 |
9 |
9 |
| if (dec->json.cb_sk_object && HvKEYS (hv) == 1) |
5 |
4 |
9 |
|
| 1528 |
|
| { |
| 1529 |
|
| HE *cb, *he; |
| 1530 |
|
| |
| 1531 |
5 |
| hv_iterinit (hv); |
| 1532 |
5 |
| he = hv_iternext (hv); |
| 1533 |
5 |
| hv_iterinit (hv); |
| 1534 |
|
| |
| 1535 |
|
| // the next line creates a mortal sv each time it's called. |
| 1536 |
|
| // might want to optimise this for common cases. |
| 1537 |
5 |
| cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0); |
| 1538 |
|
| |
| 1539 |
5 |
▶ | if (cb) |
Branch: 1 branch
| Branch | True | False | Total |
| if (cb) |
4 |
1 |
5 |
|
| 1540 |
|
| { |
| 1541 |
4 |
| dSP; |
| 1542 |
|
| int count; |
| 1543 |
|
| |
| 1544 |
4 |
| ENTER; SAVETMPS; |
| 1545 |
4 |
▶ | PUSHMARK (SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK (SP); |
0 |
4 |
4 |
|
| 1546 |
4 |
▶ | XPUSHs (HeVAL (he)); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs (HeVAL (he)); |
0 |
4 |
4 |
|
| 1547 |
4 |
| sv_2mortal (sv); |
| 1548 |
|
| |
| 1549 |
4 |
| PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
| 1550 |
|
| |
| 1551 |
4 |
▶ | if (count == 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (count == 1) |
3 |
1 |
4 |
|
| 1552 |
|
| { |
| 1553 |
3 |
| sv = newSVsv (POPs); |
| 1554 |
3 |
| PUTBACK; |
| 1555 |
3 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
3 |
0 |
3 |
|
| 1556 |
3 |
| return sv; |
| 1557 |
|
| } |
| 1558 |
1 |
▶ | else if (count) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (count) |
0 |
1 |
1 |
|
| 1559 |
0 |
| croak ("filter_json_single_key_object callbacks must not return more than one scalar"); |
| 1560 |
|
| |
| 1561 |
1 |
| PUTBACK; |
| 1562 |
|
| |
| 1563 |
1 |
| SvREFCNT_inc (sv); |
| 1564 |
|
| |
| 1565 |
1 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
1 |
0 |
1 |
|
| 1566 |
|
| } |
| 1567 |
|
| } |
| 1568 |
|
| |
| 1569 |
6 |
▶ | if (dec->json.cb_object) |
Branch: 1 branch
| Branch | True | False | Total |
| if (dec->json.cb_object) |
5 |
1 |
6 |
|
| 1570 |
|
| { |
| 1571 |
5 |
| dSP; |
| 1572 |
|
| int count; |
| 1573 |
|
| |
| 1574 |
5 |
| ENTER; SAVETMPS; |
| 1575 |
5 |
▶ | PUSHMARK (SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK (SP); |
0 |
5 |
5 |
|
| 1576 |
5 |
▶ | XPUSHs (sv_2mortal (sv)); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs (sv_2mortal (sv)); |
0 |
5 |
5 |
|
| 1577 |
|
| |
| 1578 |
5 |
| PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
| 1579 |
|
| |
| 1580 |
5 |
▶ | if (count == 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (count == 1) |
2 |
3 |
5 |
|
| 1581 |
2 |
| sv = newSVsv (POPs); |
| 1582 |
3 |
▶ | else if (count == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (count == 0) |
3 |
0 |
3 |
|
| 1583 |
3 |
| SvREFCNT_inc (sv); |
| 1584 |
|
| else |
| 1585 |
0 |
| croak ("filter_json_object callbacks must not return more than one scalar"); |
| 1586 |
|
| |
| 1587 |
5 |
| PUTBACK; |
| 1588 |
|
| |
| 1589 |
5 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
5 |
0 |
5 |
|
| 1590 |
|
| } |
| 1591 |
|
| } |
| 1592 |
|
| |
| 1593 |
825 |
| return sv; |
| 1594 |
|
| |
| 1595 |
526 |
| fail: |
| 1596 |
526 |
| SvREFCNT_dec (hv); |
| 1597 |
526 |
| DEC_DEC_DEPTH; |
| 1598 |
526 |
| return 0; |
| 1599 |
|
| } |
| 1600 |
|
| |
| 1601 |
|
| static SV * |
| 1602 |
6 |
| decode_tag (dec_t *dec) |
| 1603 |
|
| { |
| 1604 |
6 |
| SV *tag = 0; |
| 1605 |
6 |
| SV *val = 0; |
| 1606 |
|
| |
| 1607 |
6 |
▶ | if (!(dec->json.flags & F_ALLOW_TAGS)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(dec->json.flags & F_ALLOW_TAGS)) |
0 |
6 |
6 |
|
| 1608 |
0 |
| ERR ("malformed JSON string, neither array, object, number, string or atom"); |
| 1609 |
|
| |
| 1610 |
6 |
▶ | DEC_INC_DEPTH; |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_INC_DEPTH; |
1 |
5 |
6 |
|
| 1611 |
|
| |
| 1612 |
5 |
| ++dec->cur; |
| 1613 |
|
| |
| 1614 |
5 |
| decode_ws (dec); |
| 1615 |
|
| |
| 1616 |
5 |
| tag = decode_sv (dec); |
| 1617 |
5 |
▶ | if (!tag) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!tag) |
4 |
1 |
5 |
|
| 1618 |
4 |
| goto fail; |
| 1619 |
|
| |
| 1620 |
1 |
▶ | if (!SvPOK (tag)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvPOK (tag)) |
0 |
1 |
1 |
|
| 1621 |
0 |
| ERR ("malformed JSON string, (tag) must be a string"); |
| 1622 |
|
| |
| 1623 |
1 |
| decode_ws (dec); |
| 1624 |
|
| |
| 1625 |
1 |
▶ | if (*dec->cur != ')') |
Branch: 1 branch
| Branch | True | False | Total |
| if (*dec->cur != ')') |
0 |
1 |
1 |
|
| 1626 |
0 |
| ERR (") expected after tag"); |
| 1627 |
|
| |
| 1628 |
1 |
| ++dec->cur; |
| 1629 |
|
| |
| 1630 |
1 |
| decode_ws (dec); |
| 1631 |
|
| |
| 1632 |
1 |
| val = decode_sv (dec); |
| 1633 |
1 |
▶ | if (!val) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!val) |
0 |
1 |
1 |
|
| 1634 |
0 |
| goto fail; |
| 1635 |
|
| |
| 1636 |
1 |
▶ | if (!SvROK (val) || SvTYPE (SvRV (val)) != SVt_PVAV) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvROK (val) || SvTYPE (SvRV (val)) != SVt_PVAV) |
1 |
0 |
1 |
| if (!SvROK (val) || SvTYPE (SvRV (val)) != SVt_PVAV) |
0 |
1 |
1 |
|
| 1637 |
0 |
| ERR ("malformed JSON string, tag value must be an array"); |
| 1638 |
|
| |
| 1639 |
|
| { |
| 1640 |
1 |
| AV *av = (AV *)SvRV (val); |
| 1641 |
1 |
| int i, len = av_len (av) + 1; |
| 1642 |
1 |
| HV *stash = gv_stashsv (tag, 0); |
| 1643 |
|
| SV *sv; |
| 1644 |
|
| |
| 1645 |
1 |
▶ | if (!stash) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!stash) |
0 |
1 |
1 |
|
| 1646 |
0 |
| ERR ("cannot decode perl-object (package does not exist)"); |
| 1647 |
|
| |
| 1648 |
1 |
| GV *method = gv_fetchmethod_autoload (stash, "THAW", 0); |
| 1649 |
|
| |
| 1650 |
1 |
▶ | if (!method) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!method) |
0 |
1 |
1 |
|
| 1651 |
0 |
| ERR ("cannot decode perl-object (package does not have a THAW method)"); |
| 1652 |
|
| |
| 1653 |
1 |
| dSP; |
| 1654 |
|
| |
| 1655 |
1 |
| ENTER; SAVETMPS; |
| 1656 |
1 |
▶ | PUSHMARK (SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK (SP); |
0 |
1 |
1 |
|
| 1657 |
1 |
▶ | EXTEND (SP, len + 2); |
Branch: 2 branches
| Branch | True | False | Total |
| EXTEND (SP, len + 2); |
1 |
0 |
1 |
| EXTEND (SP, len + 2); |
0 |
1 |
1 |
|
| 1658 |
|
| // we re-bless the reference to get overload and other niceties right |
| 1659 |
1 |
| PUSHs (tag); |
| 1660 |
1 |
| PUSHs (sv_json); |
| 1661 |
|
| |
| 1662 |
4 |
▶ | for (i = 0; i < len; ++i) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < len; ++i) |
3 |
1 |
4 |
|
| 1663 |
3 |
| PUSHs (*av_fetch (av, i, 1)); |
| 1664 |
|
| |
| 1665 |
1 |
| PUTBACK; |
| 1666 |
1 |
| call_sv ((SV *)GvCV (method), G_SCALAR); |
| 1667 |
1 |
| SPAGAIN; |
| 1668 |
|
| |
| 1669 |
1 |
| SvREFCNT_dec (tag); |
| 1670 |
1 |
| SvREFCNT_dec (val); |
| 1671 |
1 |
| sv = SvREFCNT_inc (POPs); |
| 1672 |
|
| |
| 1673 |
1 |
| PUTBACK; |
| 1674 |
|
| |
| 1675 |
1 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
1 |
0 |
1 |
|
| 1676 |
|
| |
| 1677 |
1 |
| DEC_DEC_DEPTH; |
| 1678 |
1 |
| return sv; |
| 1679 |
|
| } |
| 1680 |
|
| |
| 1681 |
5 |
| fail: |
| 1682 |
5 |
| SvREFCNT_dec (tag); |
| 1683 |
5 |
| SvREFCNT_dec (val); |
| 1684 |
5 |
| return 0; |
| 1685 |
|
| } |
| 1686 |
|
| |
| 1687 |
|
| static SV * |
| 1688 |
53640 |
| decode_sv (dec_t *dec) |
| 1689 |
|
| { |
| 1690 |
|
| // the beauty of JSON: you need exactly one character lookahead |
| 1691 |
|
| // to parse everything. |
| 1692 |
53640 |
| switch (*dec->cur) |
| 1693 |
|
| { |
| 1694 |
25318 |
| case '"': ++dec->cur; return decode_str (dec); |
| 1695 |
26105 |
| case '[': ++dec->cur; return decode_av (dec); |
| 1696 |
1354 |
| case '{': ++dec->cur; return decode_hv (dec); |
| 1697 |
6 |
| case '(': return decode_tag (dec); |
| 1698 |
|
| |
| 1699 |
778 |
| case '-': |
| 1700 |
|
| case '0': case '1': case '2': case '3': case '4': |
| 1701 |
|
| case '5': case '6': case '7': case '8': case '9': |
| 1702 |
778 |
| return decode_num (dec); |
| 1703 |
|
| |
| 1704 |
6 |
| case 'f': |
| 1705 |
6 |
▶ | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
6 |
0 |
6 |
| if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
6 |
0 |
6 |
|
| 1706 |
|
| { |
| 1707 |
6 |
| dec->cur += 5; |
| 1708 |
|
| |
| 1709 |
6 |
▶ | if (expect_false (!dec->json.v_false)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (expect_false (!dec->json.v_false)) |
4 |
2 |
6 |
|
| 1710 |
4 |
| dec->json.v_false = GET_BOOL (false); |
| 1711 |
|
| |
| 1712 |
6 |
| return newSVsv (dec->json.v_false); |
| 1713 |
|
| } |
| 1714 |
|
| else |
| 1715 |
0 |
| ERR ("'false' expected"); |
| 1716 |
|
| |
| 1717 |
|
| break; |
| 1718 |
|
| |
| 1719 |
10 |
| case 't': |
| 1720 |
10 |
▶ | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
10 |
0 |
10 |
| if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
9 |
1 |
10 |
|
| 1721 |
|
| { |
| 1722 |
9 |
| dec->cur += 4; |
| 1723 |
|
| |
| 1724 |
9 |
▶ | if (expect_false (!dec->json.v_true)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (expect_false (!dec->json.v_true)) |
7 |
2 |
9 |
|
| 1725 |
7 |
| dec->json.v_true = GET_BOOL (true); |
| 1726 |
|
| |
| 1727 |
9 |
| return newSVsv (dec->json.v_true); |
| 1728 |
|
| } |
| 1729 |
|
| else |
| 1730 |
1 |
| ERR ("'true' expected"); |
| 1731 |
|
| |
| 1732 |
|
| break; |
| 1733 |
|
| |
| 1734 |
43 |
| case 'n': |
| 1735 |
43 |
▶ | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "null", 4)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "null", 4)) |
43 |
0 |
43 |
| if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "null", 4)) |
42 |
1 |
43 |
|
| 1736 |
|
| { |
| 1737 |
42 |
| dec->cur += 4; |
| 1738 |
42 |
| return newSVsv (&PL_sv_undef); |
| 1739 |
|
| } |
| 1740 |
|
| else |
| 1741 |
1 |
| ERR ("'null' expected"); |
| 1742 |
|
| |
| 1743 |
|
| break; |
| 1744 |
|
| |
| 1745 |
20 |
| default: |
| 1746 |
20 |
| ERR ("malformed JSON string, neither tag, array, object, number, string or atom"); |
| 1747 |
|
| break; |
| 1748 |
|
| } |
| 1749 |
|
| |
| 1750 |
22 |
| fail: |
| 1751 |
22 |
| return 0; |
| 1752 |
|
| } |
| 1753 |
|
| |
| 1754 |
|
| static SV * |
| 1755 |
25190 |
| decode_json (SV *string, JSON *json, STRLEN *offset_return) |
| 1756 |
|
| { |
| 1757 |
|
| dec_t dec; |
| 1758 |
|
| SV *sv; |
| 1759 |
|
| |
| 1760 |
|
| /* work around bugs in 5.10 where manipulating magic values |
| 1761 |
|
| * makes perl ignore the magic in subsequent accesses. |
| 1762 |
|
| * also make a copy of non-PV values, to get them into a clean |
| 1763 |
|
| * state (SvPV should do that, but it's buggy, see below). |
| 1764 |
|
| * |
| 1765 |
|
| * SvIsCOW_shared_hash works around a bug in perl (possibly 5.16), |
| 1766 |
|
| * as reported by Reini Urban. |
| 1767 |
|
| */ |
| 1768 |
|
| /*SvGETMAGIC (string);*/ |
| 1769 |
25190 |
▶ | if (SvMAGICAL (string) || !SvPOK (string) || SvIsCOW_shared_hash (string)) |
Branch: 4 branches
| Branch | True | False | Total |
| if (SvMAGICAL (string) || !SvPOK (string) || SvIsCOW_shared_hash (string)) |
25068 |
122 |
25190 |
| if (SvMAGICAL (string) || !SvPOK (string) || SvIsCOW_shared_hash (string)) |
25063 |
5 |
25068 |
| if (SvMAGICAL (string) || !SvPOK (string) || SvIsCOW_shared_hash (string)) |
88 |
24975 |
25063 |
| if (SvMAGICAL (string) || !SvPOK (string) || SvIsCOW_shared_hash (string)) |
0 |
88 |
88 |
|
| 1770 |
127 |
| string = sv_2mortal (newSVsv (string)); |
| 1771 |
|
| |
| 1772 |
25190 |
▶ | SvUPGRADE (string, SVt_PV); |
Branch: 1 branch
| Branch | True | False | Total |
| SvUPGRADE (string, SVt_PV); |
4 |
25186 |
25190 |
|
| 1773 |
|
| |
| 1774 |
|
| /* work around a bug in perl 5.10, which causes SvCUR to fail an |
| 1775 |
|
| * assertion with -DDEBUGGING, although SvCUR is documented to |
| 1776 |
|
| * return the xpv_cur field which certainly exists after upgrading. |
| 1777 |
|
| * according to nicholas clark, calling SvPOK fixes this. |
| 1778 |
|
| * But it doesn't fix it, so try another workaround, call SvPV_nolen |
| 1779 |
|
| * and hope for the best. |
| 1780 |
|
| * Damnit, SvPV_nolen still trips over yet another assertion. This |
| 1781 |
|
| * assertion business is seriously broken, try yet another workaround |
| 1782 |
|
| * for the broken -DDEBUGGING. |
| 1783 |
|
| */ |
| 1784 |
|
| { |
| 1785 |
|
| #ifdef DEBUGGING |
| 1786 |
|
| STRLEN offset = SvOK (string) ? sv_len (string) : 0; |
| 1787 |
|
| #else |
| 1788 |
25190 |
| STRLEN offset = SvCUR (string); |
| 1789 |
|
| #endif |
| 1790 |
|
| |
| 1791 |
25190 |
▶ | if (offset > json->max_size && json->max_size) |
Branch: 2 branches
| Branch | True | False | Total |
| if (offset > json->max_size && json->max_size) |
25185 |
5 |
25190 |
| if (offset > json->max_size && json->max_size) |
1 |
25184 |
25185 |
|
| 1792 |
1 |
| croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
| 1793 |
|
| (unsigned long)SvCUR (string), (unsigned long)json->max_size); |
| 1794 |
|
| } |
| 1795 |
|
| |
| 1796 |
25189 |
▶ | if (DECODE_WANTS_OCTETS (json)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (DECODE_WANTS_OCTETS (json)) |
12386 |
12803 |
25189 |
|
| 1797 |
12386 |
| sv_utf8_downgrade (string, 0); |
| 1798 |
|
| else |
| 1799 |
12803 |
| sv_utf8_upgrade (string); |
| 1800 |
|
| |
| 1801 |
25189 |
▶ | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
Branch: 2 branches
| Branch | True | False | Total |
| SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
25173 |
16 |
25189 |
| SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
0 |
25173 |
25173 |
|
| 1802 |
|
| |
| 1803 |
25189 |
| dec.json = *json; |
| 1804 |
25189 |
| dec.cur = SvPVX (string); |
| 1805 |
25189 |
| dec.end = SvEND (string); |
| 1806 |
25189 |
| dec.err = 0; |
| 1807 |
25189 |
| dec.depth = 0; |
| 1808 |
|
| |
| 1809 |
25189 |
▶ | if (dec.json.cb_object || dec.json.cb_sk_object) |
Branch: 2 branches
| Branch | True | False | Total |
| if (dec.json.cb_object || dec.json.cb_sk_object) |
25181 |
8 |
25189 |
| if (dec.json.cb_object || dec.json.cb_sk_object) |
2 |
25179 |
25181 |
|
| 1810 |
10 |
| dec.json.flags |= F_HOOK; |
| 1811 |
|
| |
| 1812 |
25189 |
| *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
| 1813 |
|
| |
| 1814 |
25189 |
| decode_ws (&dec); |
| 1815 |
25189 |
| sv = decode_sv (&dec); |
| 1816 |
|
| |
| 1817 |
25189 |
▶ | if (offset_return) |
Branch: 1 branch
| Branch | True | False | Total |
| if (offset_return) |
284 |
24905 |
25189 |
|
| 1818 |
284 |
| *offset_return = dec.cur - SvPVX (string); |
| 1819 |
24905 |
▶ | else if (sv) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (sv) |
24844 |
61 |
24905 |
|
| 1820 |
|
| { |
| 1821 |
|
| // check for trailing garbage |
| 1822 |
24844 |
| decode_ws (&dec); |
| 1823 |
|
| |
| 1824 |
24844 |
▶ | if (dec.cur != dec.end) |
Branch: 1 branch
| Branch | True | False | Total |
| if (dec.cur != dec.end) |
8 |
24836 |
24844 |
|
| 1825 |
|
| { |
| 1826 |
8 |
| dec.err = "garbage after JSON object"; |
| 1827 |
8 |
| SvREFCNT_dec (sv); |
| 1828 |
8 |
| sv = 0; |
| 1829 |
|
| } |
| 1830 |
|
| } |
| 1831 |
|
| |
| 1832 |
25189 |
▶ | if (!sv) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sv) |
72 |
25117 |
25189 |
|
| 1833 |
|
| { |
| 1834 |
72 |
| SV *uni = sv_newmortal (); |
| 1835 |
|
| |
| 1836 |
|
| // horrible hack to silence warning inside pv_uni_display |
| 1837 |
72 |
| COP cop = *PL_curcop; |
| 1838 |
72 |
| cop.cop_warnings = pWARN_NONE; |
| 1839 |
72 |
| ENTER; |
| 1840 |
72 |
| SAVEVPTR (PL_curcop); |
| 1841 |
72 |
| PL_curcop = &cop; |
| 1842 |
72 |
| pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
| 1843 |
72 |
| LEAVE; |
| 1844 |
|
| |
| 1845 |
135 |
▶ | croak ("%s, at character offset %d (before \"%s\")", |
Branch: 1 branch
| Branch | True | False | Total |
| croak ("%s, at character offset %d (before \"%s\")", |
63 |
9 |
72 |
|
| 1846 |
|
| dec.err, |
| 1847 |
|
| (int)ptr_to_index (string, dec.cur), |
| 1848 |
|
| dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
| 1849 |
|
| } |
| 1850 |
|
| |
| 1851 |
25117 |
| sv = sv_2mortal (sv); |
| 1852 |
|
| |
| 1853 |
25117 |
▶ | if (!(dec.json.flags & F_ALLOW_NONREF) && json_nonref (sv)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(dec.json.flags & F_ALLOW_NONREF) && json_nonref (sv)) |
199 |
24918 |
25117 |
| if (!(dec.json.flags & F_ALLOW_NONREF) && json_nonref (sv)) |
1 |
198 |
199 |
|
| 1854 |
1 |
| croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
| 1855 |
|
| |
| 1856 |
25116 |
| return sv; |
| 1857 |
|
| } |
| 1858 |
|
| |
| 1859 |
|
| ///////////////////////////////////////////////////////////////////////////// |
| 1860 |
|
| // incremental parser |
| 1861 |
|
| |
| 1862 |
|
| static void |
| 1863 |
326 |
| incr_parse (JSON *self) |
| 1864 |
|
| { |
| 1865 |
326 |
| const char *p = SvPVX (self->incr_text) + self->incr_pos; |
| 1866 |
|
| |
| 1867 |
|
| // the state machine here is a bit convoluted and could be simplified a lot |
| 1868 |
|
| // but this would make it slower, so... |
| 1869 |
|
| |
| 1870 |
|
| for (;;) |
| 1871 |
|
| { |
| 1872 |
335 |
| switch (self->incr_mode) |
| 1873 |
|
| { |
| 1874 |
|
| // reached end of a scalar, see if we are inside a nested structure or not |
| 1875 |
1007 |
| end_of_scalar: |
| 1876 |
1007 |
| self->incr_mode = INCR_M_JSON; |
| 1877 |
|
| |
| 1878 |
1007 |
▶ | if (self->incr_nest) // end of a scalar inside array, object or tag |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->incr_nest) // end of a scalar inside array, object or tag |
978 |
29 |
1007 |
|
| 1879 |
978 |
| goto incr_m_json; |
| 1880 |
|
| else // end of scalar outside structure, json text ends here |
| 1881 |
29 |
| goto interrupt; |
| 1882 |
|
| |
| 1883 |
|
| // only used for initial whitespace skipping |
| 1884 |
587 |
| case INCR_M_WS: |
| 1885 |
|
| for (;;) |
| 1886 |
|
| { |
| 1887 |
587 |
▶ | if (*p > 0x20) |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p > 0x20) |
289 |
298 |
587 |
|
| 1888 |
|
| { |
| 1889 |
289 |
▶ | if (*p == '#') |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '#') |
6 |
283 |
289 |
|
| 1890 |
|
| { |
| 1891 |
6 |
| self->incr_mode = INCR_M_C0; |
| 1892 |
6 |
| goto incr_m_c; |
| 1893 |
|
| } |
| 1894 |
|
| else |
| 1895 |
|
| { |
| 1896 |
283 |
| self->incr_mode = INCR_M_JSON; |
| 1897 |
283 |
| goto incr_m_json; |
| 1898 |
|
| } |
| 1899 |
|
| } |
| 1900 |
298 |
▶ | else if (!*p) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (!*p) |
41 |
257 |
298 |
|
| 1901 |
41 |
| goto nul; |
| 1902 |
|
| |
| 1903 |
257 |
| ++p; |
| 1904 |
|
| } |
| 1905 |
|
| |
| 1906 |
|
| // skip a single char inside a string (for \\-processing) |
| 1907 |
0 |
| case INCR_M_BS: |
| 1908 |
0 |
▶ | if (!*p) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!*p) |
0 |
0 |
0 |
|
| 1909 |
0 |
| goto nul; |
| 1910 |
|
| |
| 1911 |
0 |
| ++p; |
| 1912 |
0 |
| self->incr_mode = INCR_M_STR; |
| 1913 |
0 |
| goto incr_m_str; |
| 1914 |
|
| |
| 1915 |
|
| // inside #-style comments |
| 1916 |
|
| case INCR_M_C0: |
| 1917 |
|
| case INCR_M_C1: |
| 1918 |
9 |
| incr_m_c: |
| 1919 |
|
| for (;;) |
| 1920 |
|
| { |
| 1921 |
45 |
▶ | if (*p == '\n') |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '\n') |
9 |
36 |
45 |
|
| 1922 |
|
| { |
| 1923 |
9 |
▶ | self->incr_mode = self->incr_mode == INCR_M_C0 ? INCR_M_WS : INCR_M_JSON; |
Branch: 1 branch
| Branch | True | False | Total |
| self->incr_mode = self->incr_mode == INCR_M_C0 ? INCR_M_WS : INCR_M_JSON; |
6 |
3 |
9 |
|
| 1924 |
9 |
| break; |
| 1925 |
|
| } |
| 1926 |
36 |
▶ | else if (!*p) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (!*p) |
0 |
36 |
36 |
|
| 1927 |
0 |
| goto nul; |
| 1928 |
|
| |
| 1929 |
36 |
| ++p; |
| 1930 |
|
| } |
| 1931 |
|
| |
| 1932 |
9 |
| break; |
| 1933 |
|
| |
| 1934 |
|
| // inside true/false/null |
| 1935 |
|
| case INCR_M_TFN: |
| 1936 |
35 |
| incr_m_tfn: |
| 1937 |
|
| for (;;) |
| 1938 |
140 |
▶ | switch (*p++) |
Branch: 1 branch
| Branch | True | False | Total |
| switch (*p++) |
105 |
35 |
140 |
|
| 1939 |
|
| { |
| 1940 |
105 |
| case 'r': case 'u': case 'e': // tRUE, falsE, nUll |
| 1941 |
|
| case 'a': case 'l': case 's': // fALSe, nuLL |
| 1942 |
|
| // allowed |
| 1943 |
105 |
| break; |
| 1944 |
|
| |
| 1945 |
35 |
| default: |
| 1946 |
35 |
| --p; |
| 1947 |
35 |
| goto end_of_scalar; |
| 1948 |
|
| } |
| 1949 |
|
| |
| 1950 |
|
| // inside a number |
| 1951 |
|
| case INCR_M_NUM: |
| 1952 |
391 |
| incr_m_num: |
| 1953 |
|
| for (;;) |
| 1954 |
484 |
▶ | switch (*p++) |
Branch: 1 branch
| Branch | True | False | Total |
| switch (*p++) |
93 |
391 |
484 |
|
| 1955 |
|
| { |
| 1956 |
93 |
| case 'e': case 'E': case '.': case '+': |
| 1957 |
|
| case '-': |
| 1958 |
|
| case '0': case '1': case '2': case '3': case '4': |
| 1959 |
|
| case '5': case '6': case '7': case '8': case '9': |
| 1960 |
|
| // allowed |
| 1961 |
93 |
| break; |
| 1962 |
|
| |
| 1963 |
391 |
| default: |
| 1964 |
391 |
| --p; |
| 1965 |
391 |
| goto end_of_scalar; |
| 1966 |
|
| } |
| 1967 |
|
| |
| 1968 |
|
| // inside a string |
| 1969 |
|
| case INCR_M_STR: |
| 1970 |
581 |
| incr_m_str: |
| 1971 |
|
| for (;;) |
| 1972 |
|
| { |
| 1973 |
2073 |
▶ | if (*p == '"') |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '"') |
581 |
1492 |
2073 |
|
| 1974 |
|
| { |
| 1975 |
581 |
| ++p; |
| 1976 |
581 |
| goto end_of_scalar; |
| 1977 |
|
| } |
| 1978 |
1492 |
▶ | else if (*p == '\\') |
Branch: 1 branch
| Branch | True | False | Total |
| else if (*p == '\\') |
496 |
996 |
1492 |
|
| 1979 |
|
| { |
| 1980 |
496 |
| ++p; // "virtually" consumes character after \ |
| 1981 |
|
| |
| 1982 |
496 |
▶ | if (!*p) // if at end of string we have to switch modes |
Branch: 1 branch
| Branch | True | False | Total |
| if (!*p) // if at end of string we have to switch modes |
0 |
496 |
496 |
|
| 1983 |
|
| { |
| 1984 |
0 |
| self->incr_mode = INCR_M_BS; |
| 1985 |
0 |
| goto nul; |
| 1986 |
|
| } |
| 1987 |
|
| } |
| 1988 |
996 |
▶ | else if (!*p) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (!*p) |
0 |
996 |
996 |
|
| 1989 |
0 |
| goto nul; |
| 1990 |
|
| |
| 1991 |
1492 |
| ++p; |
| 1992 |
|
| } |
| 1993 |
|
| |
| 1994 |
|
| // after initial ws, outside string |
| 1995 |
|
| case INCR_M_JSON: |
| 1996 |
1266 |
| incr_m_json: |
| 1997 |
|
| for (;;) |
| 1998 |
|
| { |
| 1999 |
3137 |
| switch (*p++) |
| 2000 |
|
| { |
| 2001 |
2 |
| case 0: |
| 2002 |
2 |
| --p; |
| 2003 |
2 |
| goto nul; |
| 2004 |
|
| |
| 2005 |
682 |
| case 0x09: |
| 2006 |
|
| case 0x0a: |
| 2007 |
|
| case 0x0d: |
| 2008 |
|
| case 0x20: |
| 2009 |
682 |
▶ | if (!self->incr_nest) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!self->incr_nest) |
0 |
682 |
682 |
|
| 2010 |
|
| { |
| 2011 |
0 |
| --p; // do not eat the whitespace, let the next round do it |
| 2012 |
0 |
| goto interrupt; |
| 2013 |
|
| } |
| 2014 |
682 |
| break; |
| 2015 |
|
| |
| 2016 |
|
| // the following three blocks handle scalars. this makes the parser |
| 2017 |
|
| // more strict than required inside arrays or objects, and could |
| 2018 |
|
| // be moved to a special case on the toplevel (except strings) |
| 2019 |
35 |
| case 't': |
| 2020 |
|
| case 'f': |
| 2021 |
|
| case 'n': |
| 2022 |
35 |
| self->incr_mode = INCR_M_TFN; |
| 2023 |
35 |
| goto incr_m_tfn; |
| 2024 |
|
| |
| 2025 |
391 |
| case '-': |
| 2026 |
|
| case '0': case '1': case '2': case '3': case '4': |
| 2027 |
|
| case '5': case '6': case '7': case '8': case '9': |
| 2028 |
391 |
| self->incr_mode = INCR_M_NUM; |
| 2029 |
391 |
| goto incr_m_num; |
| 2030 |
|
| |
| 2031 |
581 |
| case '"': |
| 2032 |
581 |
| self->incr_mode = INCR_M_STR; |
| 2033 |
581 |
| goto incr_m_str; |
| 2034 |
|
| |
| 2035 |
355 |
| case '[': |
| 2036 |
|
| case '{': |
| 2037 |
|
| case '(': |
| 2038 |
355 |
▶ | if (++self->incr_nest > self->max_depth) |
Branch: 1 branch
| Branch | True | False | Total |
| if (++self->incr_nest > self->max_depth) |
1 |
354 |
355 |
|
| 2039 |
1 |
| croak (ERR_NESTING_EXCEEDED); |
| 2040 |
354 |
| break; |
| 2041 |
|
| |
| 2042 |
352 |
| case ']': |
| 2043 |
|
| case '}': |
| 2044 |
352 |
▶ | if (--self->incr_nest <= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (--self->incr_nest <= 0) |
253 |
99 |
352 |
|
| 2045 |
253 |
| goto interrupt; |
| 2046 |
99 |
| break; |
| 2047 |
|
| |
| 2048 |
0 |
| case ')': |
| 2049 |
0 |
| --self->incr_nest; |
| 2050 |
0 |
| break; |
| 2051 |
|
| |
| 2052 |
3 |
| case '#': |
| 2053 |
3 |
| self->incr_mode = INCR_M_C1; |
| 2054 |
3 |
| goto incr_m_c; |
| 2055 |
|
| } |
| 2056 |
|
| } |
| 2057 |
|
| } |
| 2058 |
|
| |
| 2059 |
9 |
| modechange: |
| 2060 |
|
| ; |
| 2061 |
|
| } |
| 2062 |
|
| |
| 2063 |
43 |
| nul: // nul character found |
| 2064 |
43 |
▶ | if (!*p && p != SvEND (self->incr_text)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!*p && p != SvEND (self->incr_text)) |
43 |
0 |
43 |
| if (!*p && p != SvEND (self->incr_text)) |
43 |
0 |
43 |
|
| 2065 |
|
| { |
| 2066 |
0 |
| self->incr_pos = p - SvPVX (self->incr_text) + 1; // skip 0 byte for incr_skip |
| 2067 |
0 |
| croak ("invalid NUL character in JSON text"); |
| 2068 |
|
| } |
| 2069 |
|
| |
| 2070 |
43 |
| interrupt: |
| 2071 |
325 |
| self->incr_pos = p - SvPVX (self->incr_text); |
| 2072 |
|
| |
| 2073 |
|
| //printf ("interrupt<%.*s>\n", self->incr_pos, SvPVX(self->incr_text));//D |
| 2074 |
|
| //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D |
| 2075 |
325 |
| } |
| 2076 |
|
| |
| 2077 |
|
| ///////////////////////////////////////////////////////////////////////////// |
| 2078 |
|
| // XS interface functions |
| 2079 |
|
| |
| 2080 |
|
| MODULE = JSON::XS PACKAGE = JSON::XS |
| 2081 |
|
| |
| 2082 |
|
| BOOT: |
| 2083 |
|
| { |
| 2084 |
|
| int i; |
| 2085 |
|
| |
| 2086 |
6425 |
▶ | for (i = 0; i < 256; ++i) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 256; ++i) |
6400 |
25 |
6425 |
|
| 2087 |
20200 |
▶ | decode_hexdigit [i] = |
Branch: 3 branches
| Branch | True | False | Total |
| decode_hexdigit [i] = |
5200 |
1200 |
6400 |
| decode_hexdigit [i] = |
3975 |
2175 |
6150 |
| decode_hexdigit [i] = |
4625 |
1375 |
6000 |
|
| 2088 |
5200 |
▶ | i >= '0' && i <= '9' ? i - '0' |
Branch: 1 branch
| Branch | True | False | Total |
| i >= '0' && i <= '9' ? i - '0' |
250 |
4950 |
5200 |
|
| 2089 |
3975 |
▶ | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
Branch: 1 branch
| Branch | True | False | Total |
| : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
150 |
3825 |
3975 |
|
| 2090 |
4625 |
▶ | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
Branch: 1 branch
| Branch | True | False | Total |
| : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
150 |
4475 |
4625 |
|
| 2091 |
|
| : -1; |
| 2092 |
|
| |
| 2093 |
25 |
| json_stash = gv_stashpv ("JSON::XS" , 1); |
| 2094 |
25 |
| bool_stash = gv_stashpv ("Types::Serialiser::Boolean", 1); |
| 2095 |
25 |
| bool_false = get_bool ("Types::Serialiser::false"); |
| 2096 |
25 |
| bool_true = get_bool ("Types::Serialiser::true"); |
| 2097 |
|
| |
| 2098 |
25 |
| sv_json = newSVpv ("JSON", 0); |
| 2099 |
25 |
| SvREADONLY_on (sv_json); |
| 2100 |
|
| |
| 2101 |
25 |
| CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */ |
| 2102 |
|
| } |
| 2103 |
|
| |
| 2104 |
|
| PROTOTYPES: DISABLE |
| 2105 |
|
| |
| 2106 |
|
| void CLONE (...) |
| 2107 |
|
| CODE: |
| 2108 |
|
| // as long as these writes are atomic, the race should not matter |
| 2109 |
|
| // as existing threads either already use 0, or use the old value, |
| 2110 |
|
| // which is sitll correct for the initial thread. |
| 2111 |
0 |
| json_stash = 0; |
| 2112 |
0 |
| bool_stash = 0; |
| 2113 |
0 |
| bool_false = 0; |
| 2114 |
0 |
| bool_true = 0; |
| 2115 |
|
| |
| 2116 |
|
| void new (char *klass) |
| 2117 |
|
| PPCODE: |
| 2118 |
|
| { |
| 2119 |
43105 |
| SV *pv = NEWSV (0, sizeof (JSON)); |
| 2120 |
43105 |
| SvPOK_only (pv); |
| 2121 |
43105 |
| json_init ((JSON *)SvPVX (pv)); |
| 2122 |
43105 |
▶ | XPUSHs (sv_2mortal (sv_bless ( |
Branch: 2 branches
| Branch | True | False | Total |
| XPUSHs (sv_2mortal (sv_bless ( |
0 |
43105 |
43105 |
| XPUSHs (sv_2mortal (sv_bless ( |
0 |
43105 |
43105 |
|
| 2123 |
|
| newRV_noinc (pv), |
| 2124 |
|
| strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1) |
| 2125 |
|
| ))); |
| 2126 |
|
| } |
| 2127 |
|
| |
| 2128 |
|
| void boolean_values (JSON *self, SV *v_false = 0, SV *v_true = 0) |
| 2129 |
|
| PPCODE: |
| 2130 |
0 |
| self->v_false = newSVsv (v_false); |
| 2131 |
0 |
| self->v_true = newSVsv (v_true); |
| 2132 |
0 |
▶ | XPUSHs (ST (0)); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs (ST (0)); |
0 |
0 |
0 |
|
| 2133 |
|
| |
| 2134 |
|
| void get_boolean_values (JSON *self) |
| 2135 |
|
| PPCODE: |
| 2136 |
0 |
▶ | if (self->v_false && self->v_true) |
Branch: 2 branches
| Branch | True | False | Total |
| if (self->v_false && self->v_true) |
0 |
0 |
0 |
| if (self->v_false && self->v_true) |
0 |
0 |
0 |
|
| 2137 |
|
| { |
| 2138 |
0 |
▶ | EXTEND (SP, 2); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND (SP, 2); |
0 |
0 |
0 |
|
| 2139 |
0 |
| PUSHs (self->v_false); |
| 2140 |
0 |
| PUSHs (self->v_true); |
| 2141 |
|
| } |
| 2142 |
|
| |
| 2143 |
|
| void ascii (JSON *self, int enable = 1) |
| 2144 |
|
| ALIAS: |
| 2145 |
|
| ascii = F_ASCII |
| 2146 |
|
| latin1 = F_LATIN1 |
| 2147 |
|
| utf8 = F_UTF8 |
| 2148 |
|
| indent = F_INDENT |
| 2149 |
|
| canonical = F_CANONICAL |
| 2150 |
|
| space_before = F_SPACE_BEFORE |
| 2151 |
|
| space_after = F_SPACE_AFTER |
| 2152 |
|
| pretty = F_PRETTY |
| 2153 |
|
| allow_nonref = F_ALLOW_NONREF |
| 2154 |
|
| shrink = F_SHRINK |
| 2155 |
|
| allow_blessed = F_ALLOW_BLESSED |
| 2156 |
|
| convert_blessed = F_CONV_BLESSED |
| 2157 |
|
| relaxed = F_RELAXED |
| 2158 |
|
| allow_unknown = F_ALLOW_UNKNOWN |
| 2159 |
|
| allow_tags = F_ALLOW_TAGS |
| 2160 |
|
| PPCODE: |
| 2161 |
|
| { |
| 2162 |
76894 |
▶ | if (enable) |
Branch: 1 branch
| Branch | True | False | Total |
| if (enable) |
61528 |
15366 |
76894 |
|
| 2163 |
61528 |
| self->flags |= ix; |
| 2164 |
|
| else |
| 2165 |
15366 |
| self->flags &= ~ix; |
| 2166 |
|
| |
| 2167 |
76894 |
▶ | XPUSHs (ST (0)); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs (ST (0)); |
0 |
76894 |
76894 |
|
| 2168 |
|
| } |
| 2169 |
|
| |
| 2170 |
|
| void get_ascii (JSON *self) |
| 2171 |
|
| ALIAS: |
| 2172 |
|
| get_ascii = F_ASCII |
| 2173 |
|
| get_latin1 = F_LATIN1 |
| 2174 |
|
| get_utf8 = F_UTF8 |
| 2175 |
|
| get_indent = F_INDENT |
| 2176 |
|
| get_canonical = F_CANONICAL |
| 2177 |
|
| get_space_before = F_SPACE_BEFORE |
| 2178 |
|
| get_space_after = F_SPACE_AFTER |
| 2179 |
|
| get_allow_nonref = F_ALLOW_NONREF |
| 2180 |
|
| get_shrink = F_SHRINK |
| 2181 |
|
| get_allow_blessed = F_ALLOW_BLESSED |
| 2182 |
|
| get_convert_blessed = F_CONV_BLESSED |
| 2183 |
|
| get_relaxed = F_RELAXED |
| 2184 |
|
| get_allow_unknown = F_ALLOW_UNKNOWN |
| 2185 |
|
| get_allow_tags = F_ALLOW_TAGS |
| 2186 |
|
| PPCODE: |
| 2187 |
0 |
▶ | XPUSHs (boolSV (self->flags & ix)); |
Branch: 2 branches
| Branch | True | False | Total |
| XPUSHs (boolSV (self->flags & ix)); |
0 |
0 |
0 |
| XPUSHs (boolSV (self->flags & ix)); |
0 |
0 |
0 |
|
| 2188 |
|
| |
| 2189 |
|
| void max_depth (JSON *self, U32 max_depth = 0x80000000UL) |
| 2190 |
|
| PPCODE: |
| 2191 |
6 |
| self->max_depth = max_depth; |
| 2192 |
6 |
▶ | XPUSHs (ST (0)); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs (ST (0)); |
0 |
6 |
6 |
|
| 2193 |
|
| |
| 2194 |
|
| U32 get_max_depth (JSON *self) |
| 2195 |
|
| CODE: |
| 2196 |
0 |
▶ | RETVAL = self->max_depth; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = self->max_depth; |
0 |
0 |
0 |
|
| 2197 |
|
| OUTPUT: |
| 2198 |
|
| RETVAL |
| 2199 |
|
| |
| 2200 |
|
| void max_size (JSON *self, U32 max_size = 0) |
| 2201 |
|
| PPCODE: |
| 2202 |
3 |
| self->max_size = max_size; |
| 2203 |
3 |
▶ | XPUSHs (ST (0)); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs (ST (0)); |
0 |
3 |
3 |
|
| 2204 |
|
| |
| 2205 |
|
| int get_max_size (JSON *self) |
| 2206 |
|
| CODE: |
| 2207 |
0 |
▶ | RETVAL = self->max_size; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = self->max_size; |
0 |
0 |
0 |
|
| 2208 |
|
| OUTPUT: |
| 2209 |
|
| RETVAL |
| 2210 |
|
| |
| 2211 |
|
| void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) |
| 2212 |
|
| PPCODE: |
| 2213 |
|
| { |
| 2214 |
3 |
| SvREFCNT_dec (self->cb_object); |
| 2215 |
3 |
▶ | self->cb_object = SvOK (cb) ? newSVsv (cb) : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| self->cb_object = SvOK (cb) ? newSVsv (cb) : 0; |
2 |
1 |
3 |
|
| 2216 |
|
| |
| 2217 |
3 |
▶ | XPUSHs (ST (0)); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs (ST (0)); |
0 |
3 |
3 |
|
| 2218 |
|
| } |
| 2219 |
|
| |
| 2220 |
|
| void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef) |
| 2221 |
|
| PPCODE: |
| 2222 |
|
| { |
| 2223 |
4 |
▶ | if (!self->cb_sk_object) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!self->cb_sk_object) |
1 |
3 |
4 |
|
| 2224 |
1 |
| self->cb_sk_object = newHV (); |
| 2225 |
|
| |
| 2226 |
4 |
▶ | if (SvOK (cb)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK (cb)) |
3 |
1 |
4 |
|
| 2227 |
3 |
| hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
| 2228 |
|
| else |
| 2229 |
|
| { |
| 2230 |
1 |
| hv_delete_ent (self->cb_sk_object, key, G_DISCARD, 0); |
| 2231 |
|
| |
| 2232 |
1 |
▶ | if (!HvKEYS (self->cb_sk_object)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!HvKEYS (self->cb_sk_object)) |
0 |
1 |
1 |
| if (!HvKEYS (self->cb_sk_object)) |
0 |
1 |
1 |
|
| 2233 |
|
| { |
| 2234 |
0 |
| SvREFCNT_dec (self->cb_sk_object); |
| 2235 |
0 |
| self->cb_sk_object = 0; |
| 2236 |
|
| } |
| 2237 |
|
| } |
| 2238 |
|
| |
| 2239 |
4 |
▶ | XPUSHs (ST (0)); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs (ST (0)); |
0 |
4 |
4 |
|
| 2240 |
|
| } |
| 2241 |
|
| |
| 2242 |
|
| void encode (JSON *self, SV *scalar) |
| 2243 |
|
| PPCODE: |
| 2244 |
24920 |
| PUTBACK; scalar = encode_json (scalar, self); SPAGAIN; |
| 2245 |
24908 |
▶ | XPUSHs (scalar); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs (scalar); |
0 |
24908 |
24908 |
|
| 2246 |
|
| |
| 2247 |
|
| void decode (JSON *self, SV *jsonstr) |
| 2248 |
|
| PPCODE: |
| 2249 |
18704 |
| PUTBACK; jsonstr = decode_json (jsonstr, self, 0); SPAGAIN; |
| 2250 |
18639 |
▶ | XPUSHs (jsonstr); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs (jsonstr); |
0 |
18639 |
18639 |
|
| 2251 |
|
| |
| 2252 |
|
| void decode_prefix (JSON *self, SV *jsonstr) |
| 2253 |
|
| PPCODE: |
| 2254 |
|
| { |
| 2255 |
|
| SV *sv; |
| 2256 |
|
| STRLEN offset; |
| 2257 |
2 |
| PUTBACK; sv = decode_json (jsonstr, self, &offset); SPAGAIN; |
| 2258 |
2 |
▶ | EXTEND (SP, 2); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND (SP, 2); |
0 |
2 |
2 |
|
| 2259 |
2 |
| PUSHs (sv); |
| 2260 |
2 |
| PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, SvPV_nolen (jsonstr) + offset)))); |
| 2261 |
|
| } |
| 2262 |
|
| |
| 2263 |
|
| void incr_parse (JSON *self, SV *jsonstr = 0) |
| 2264 |
|
| PPCODE: |
| 2265 |
|
| { |
| 2266 |
666 |
▶ | if (!self->incr_text) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!self->incr_text) |
25 |
641 |
666 |
|
| 2267 |
25 |
| self->incr_text = newSVpvn ("", 0); |
| 2268 |
|
| |
| 2269 |
|
| /* if utf8-ness doesn't match the decoder, need to upgrade/downgrade */ |
| 2270 |
666 |
▶ | if (!DECODE_WANTS_OCTETS (self) == !SvUTF8 (self->incr_text)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!DECODE_WANTS_OCTETS (self) == !SvUTF8 (self->incr_text)) |
25 |
641 |
666 |
|
| 2271 |
25 |
▶ | if (DECODE_WANTS_OCTETS (self)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (DECODE_WANTS_OCTETS (self)) |
0 |
25 |
25 |
|
| 2272 |
|
| { |
| 2273 |
0 |
▶ | if (self->incr_pos) |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->incr_pos) |
0 |
0 |
0 |
|
| 2274 |
0 |
| self->incr_pos = utf8_length ((U8 *)SvPVX (self->incr_text), |
| 2275 |
|
| (U8 *)SvPVX (self->incr_text) + self->incr_pos); |
| 2276 |
|
| |
| 2277 |
0 |
| sv_utf8_downgrade (self->incr_text, 0); |
| 2278 |
|
| } |
| 2279 |
|
| else |
| 2280 |
|
| { |
| 2281 |
25 |
| sv_utf8_upgrade (self->incr_text); |
| 2282 |
|
| |
| 2283 |
25 |
▶ | if (self->incr_pos) |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->incr_pos) |
0 |
25 |
25 |
|
| 2284 |
0 |
| self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) |
| 2285 |
0 |
| - (U8 *)SvPVX (self->incr_text); |
| 2286 |
|
| } |
| 2287 |
|
| |
| 2288 |
|
| // append data, if any |
| 2289 |
666 |
▶ | if (jsonstr) |
Branch: 1 branch
| Branch | True | False | Total |
| if (jsonstr) |
347 |
319 |
666 |
|
| 2290 |
|
| { |
| 2291 |
|
| /* make sure both strings have same encoding */ |
| 2292 |
347 |
▶ | if (SvUTF8 (jsonstr) != SvUTF8 (self->incr_text)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvUTF8 (jsonstr) != SvUTF8 (self->incr_text)) |
346 |
1 |
347 |
|
| 2293 |
346 |
▶ | if (SvUTF8 (jsonstr)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvUTF8 (jsonstr)) |
0 |
346 |
346 |
|
| 2294 |
0 |
| sv_utf8_downgrade (jsonstr, 0); |
| 2295 |
|
| else |
| 2296 |
346 |
| sv_utf8_upgrade (jsonstr); |
| 2297 |
|
| |
| 2298 |
|
| /* and then just blindly append */ |
| 2299 |
|
| { |
| 2300 |
|
| STRLEN len; |
| 2301 |
347 |
| const char *str = SvPV (jsonstr, len); |
| 2302 |
347 |
| STRLEN cur = SvCUR (self->incr_text); |
| 2303 |
|
| |
| 2304 |
347 |
▶ | if (SvLEN (self->incr_text) - cur <= len) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvLEN (self->incr_text) - cur <= len) |
143 |
204 |
347 |
|
| 2305 |
143 |
| json_sv_grow (self->incr_text, cur, len); |
| 2306 |
|
| |
| 2307 |
347 |
| Move (str, SvEND (self->incr_text), len, char); |
| 2308 |
347 |
| SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len); |
| 2309 |
347 |
| *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there |
| 2310 |
|
| } |
| 2311 |
|
| } |
| 2312 |
|
| |
| 2313 |
666 |
▶ | if (GIMME_V != G_VOID) |
Branch: 1 branch
| Branch | True | False | Total |
| if (GIMME_V != G_VOID) |
326 |
340 |
666 |
|
| 2314 |
|
| do |
| 2315 |
|
| { |
| 2316 |
|
| SV *sv; |
| 2317 |
|
| STRLEN offset; |
| 2318 |
|
| |
| 2319 |
327 |
▶ | if (!INCR_DONE (self)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!INCR_DONE (self)) |
325 |
2 |
327 |
| if (!INCR_DONE (self)) |
324 |
1 |
325 |
|
| 2320 |
|
| { |
| 2321 |
326 |
| incr_parse (self); |
| 2322 |
|
| |
| 2323 |
325 |
▶ | if (self->incr_pos > self->max_size && self->max_size) |
Branch: 2 branches
| Branch | True | False | Total |
| if (self->incr_pos > self->max_size && self->max_size) |
287 |
38 |
325 |
| if (self->incr_pos > self->max_size && self->max_size) |
1 |
286 |
287 |
|
| 2324 |
1 |
| croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
| 2325 |
|
| (unsigned long)self->incr_pos, (unsigned long)self->max_size); |
| 2326 |
|
| |
| 2327 |
324 |
▶ | if (!INCR_DONE (self)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!INCR_DONE (self)) |
322 |
2 |
324 |
| if (!INCR_DONE (self)) |
41 |
281 |
322 |
|
| 2328 |
|
| { |
| 2329 |
|
| // as an optimisation, do not accumulate white space in the incr buffer |
| 2330 |
43 |
▶ | if (self->incr_mode == INCR_M_WS && self->incr_pos) |
Branch: 2 branches
| Branch | True | False | Total |
| if (self->incr_mode == INCR_M_WS && self->incr_pos) |
41 |
2 |
43 |
| if (self->incr_mode == INCR_M_WS && self->incr_pos) |
4 |
37 |
41 |
|
| 2331 |
|
| { |
| 2332 |
4 |
| self->incr_pos = 0; |
| 2333 |
4 |
| SvCUR_set (self->incr_text, 0); |
| 2334 |
4 |
| *SvEND (self->incr_text) = 0; |
| 2335 |
|
| } |
| 2336 |
|
| |
| 2337 |
43 |
| break; |
| 2338 |
|
| } |
| 2339 |
|
| } |
| 2340 |
|
| |
| 2341 |
282 |
| PUTBACK; sv = decode_json (self->incr_text, self, &offset); SPAGAIN; |
| 2342 |
279 |
▶ | XPUSHs (sv); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs (sv); |
0 |
279 |
279 |
|
| 2343 |
|
| |
| 2344 |
279 |
| self->incr_pos = 0; |
| 2345 |
279 |
| self->incr_nest = 0; |
| 2346 |
279 |
| self->incr_mode = 0; |
| 2347 |
|
| |
| 2348 |
279 |
| sv_chop (self->incr_text, SvPVX (self->incr_text) + offset); |
| 2349 |
|
| } |
| 2350 |
279 |
▶ | while (GIMME_V == G_ARRAY); |
Branch: 1 branch
| Branch | True | False | Total |
| while (GIMME_V == G_ARRAY); |
1 |
278 |
279 |
|
| 2351 |
|
| } |
| 2352 |
|
| |
| 2353 |
|
| SV *incr_text (JSON *self) |
| 2354 |
|
| ATTRS: lvalue |
| 2355 |
|
| CODE: |
| 2356 |
|
| { |
| 2357 |
304 |
▶ | if (self->incr_pos) |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->incr_pos) |
0 |
304 |
304 |
|
| 2358 |
0 |
| croak ("incr_text can not be called when the incremental parser already started parsing"); |
| 2359 |
|
| |
| 2360 |
304 |
▶ | RETVAL = self->incr_text ? SvREFCNT_inc (self->incr_text) : &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = self->incr_text ? SvREFCNT_inc (self->incr_text) : &PL_sv_undef; |
304 |
0 |
304 |
|
| 2361 |
|
| } |
| 2362 |
|
| OUTPUT: |
| 2363 |
|
| RETVAL |
| 2364 |
|
| |
| 2365 |
|
| void incr_skip (JSON *self) |
| 2366 |
|
| CODE: |
| 2367 |
|
| { |
| 2368 |
2 |
▶ | if (self->incr_pos) |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->incr_pos) |
2 |
0 |
2 |
|
| 2369 |
|
| { |
| 2370 |
4 |
| sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + self->incr_pos); |
| 2371 |
2 |
| self->incr_pos = 0; |
| 2372 |
2 |
| self->incr_nest = 0; |
| 2373 |
2 |
| self->incr_mode = 0; |
| 2374 |
|
| } |
| 2375 |
|
| } |
| 2376 |
|
| |
| 2377 |
|
| void incr_reset (JSON *self) |
| 2378 |
|
| CODE: |
| 2379 |
|
| { |
| 2380 |
0 |
| SvREFCNT_dec (self->incr_text); |
| 2381 |
0 |
| self->incr_text = 0; |
| 2382 |
0 |
| self->incr_pos = 0; |
| 2383 |
0 |
| self->incr_nest = 0; |
| 2384 |
0 |
| self->incr_mode = 0; |
| 2385 |
|
| } |
| 2386 |
|
| |
| 2387 |
|
| void DESTROY (JSON *self) |
| 2388 |
|
| CODE: |
| 2389 |
43105 |
| SvREFCNT_dec (self->v_false); |
| 2390 |
43105 |
| SvREFCNT_dec (self->v_true); |
| 2391 |
43105 |
| SvREFCNT_dec (self->cb_sk_object); |
| 2392 |
43105 |
| SvREFCNT_dec (self->cb_object); |
| 2393 |
43105 |
| SvREFCNT_dec (self->incr_text); |
| 2394 |
|
| |
| 2395 |
|
| PROTOTYPES: ENABLE |
| 2396 |
|
| |
| 2397 |
|
| void encode_json (SV *scalar) |
| 2398 |
|
| PPCODE: |
| 2399 |
|
| { |
| 2400 |
|
| JSON json; |
| 2401 |
183 |
| json_init (&json); |
| 2402 |
183 |
| json.flags |= F_UTF8; |
| 2403 |
183 |
| PUTBACK; scalar = encode_json (scalar, &json); SPAGAIN; |
| 2404 |
183 |
▶ | XPUSHs (scalar); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs (scalar); |
0 |
183 |
183 |
|
| 2405 |
|
| } |
| 2406 |
|
| |
| 2407 |
|
| void decode_json (SV *jsonstr) |
| 2408 |
|
| PPCODE: |
| 2409 |
|
| { |
| 2410 |
|
| JSON json; |
| 2411 |
6202 |
| json_init (&json); |
| 2412 |
6202 |
| json.flags |= F_UTF8; |
| 2413 |
6202 |
| PUTBACK; jsonstr = decode_json (jsonstr, &json, 0); SPAGAIN; |
| 2414 |
6196 |
▶ | XPUSHs (jsonstr); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs (jsonstr); |
0 |
6196 |
6196 |
|
| 2415 |
|
| } |
| 2416 |
|
| |