/
XS.xs
5131 lines (4579 loc) · 171 KB
/
XS.xs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define PERL_NO_GET_CONTEXT
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#define NEED_load_module
#define NEED_newCONSTSUB
#define NEED_vload_module
#define NEED_vnewSVpvf
#define NEED_warner
#define NEED_grok_number
#define NEED_grok_numeric_radix
#define NEED_newRV_noinc
#define NEED_sv_2pv_flags
#include "ppport.h"
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <float.h>
#if defined(__BORLANDC__) || defined(_MSC_VER)
# define snprintf _snprintf // C compilers have this in stdio.h
#endif
#ifndef PERL_UNUSED_RESULT
# if defined(__GNUC__) && defined(HASATTRIBUTE_WARN_UNUSED_RESULT)
# define PERL_UNUSED_RESULT(v) STMT_START { __typeof__(v) z = (v); (void)sizeof(z); } STMT_END
# else
# define PERL_UNUSED_RESULT(v) ((void)(v))
# endif
#endif
#if defined(_AIX) && (!defined(HAS_LONG_DOUBLE) || AIX_WORKAROUND)
#define HAVE_NO_POWL
#endif
/* Freebsd 10: It has powl, but it is too bad. strtold is good. RT #101265 */
#if defined(__FreeBSD__) && defined(__clang__) && defined(USE_LONG_DOUBLE)
#define HAVE_BAD_POWL
#endif
#if PERL_VERSION < 22 && defined(HAS_SETLOCALE)
#define NEED_NUMERIC_LOCALE_C
#ifdef I_XLOCALE
#include <xlocale.h>
#endif
#endif
/* FIXME: still a refcount error */
#define HAVE_DECODE_BOM
#define UTF8BOM "\357\273\277" /* EF BB BF */
/* UTF16/32BOM is deprecated, RFC 8259 */
#define UTF16BOM "\377\376" /* FF FE or +UFEFF */
#define UTF16BOM_BE "\376\377" /* FE FF */
#define UTF32BOM "\377\376\000\000" /* FF FE 00 00 or +UFEFF */
#define UTF32BOM_BE "\000\000\376\377" /* 00 00 FE FF */
/* Need to disable broken gcc-9.[0-3] -O1. Assume gcc-10 is also still broken */
/* Only gcc defines __GNUC_PATCHLEVEL__, clang and icc do define __GNUC__ */
#if defined(__GNUC__) && defined(__GNUC_PATCHLEVEL__) && \
(((__GNUC__ == 9) && (__GNUC_MINOR__ <= 3) || \
(__GNUC__ > 9)))
# define BROKEN_GCC_OPT
#endif
/* mingw with USE_LONG_DOUBLE (and implied USE_MINGW_ANSI_STDIO) do use the
non-msvcrt inf/nan stringification in sprintf(). */
#if defined(WIN32) && !defined(__USE_MINGW_ANSI_STDIO) && !defined(USE_LONG_DOUBLE)
/* new ucrtd.dll runtime? We do not probe the runtime or variants in the Makefile.PL yet. */
#define STR_INF "inf"
#define STR_INF2 "inf.0"
#define STR_NAN "nan"
#define STR_QNAN "nan(ind)"
/* old standard msvcrt.dll */
#define STR_INF3 "1.#INF"
#define STR_INF4 "1.#INF.0"
#define STR_NAN2 "1.#IND"
#define STR_QNAN2 "1.#QNAN"
#define HAVE_QNAN
#elif defined(sun) || defined(__sun)
#define STR_INF "Infinity"
#define STR_NAN "NaN"
#elif defined(__hpux)
#define STR_INF "++"
#define STR_NAN "-?"
#define HAVE_NEG_NAN
#define STR_NEG_INF "---"
#define STR_NEG_NAN "?"
#elif defined(_AIX)
/* xlC compiler: __TOS_AIX__ FIXME: This does not work yet. GH #165 */
#define STR_INF "INF.0"
#define STR_INF2 "-INF.0"
#define HAVE_NEG_NAN
#define HAVE_QNAN
#define STR_NAN "NaN"
//#define STR_QNAN "NaNQ"
#define STR_QNAN "NANQ"
#else
#define STR_INF "inf"
#define STR_NAN "nan"
#endif
/* NV_INF compatibility for Perl 5.6 */
#if !defined(NV_INF) && defined(USE_LONG_DOUBLE) && defined(LDBL_INFINITY)
# define NV_INF LDBL_INFINITY
#endif
#if !defined(NV_INF) && defined(DBL_INFINITY)
# define NV_INF (NV)DBL_INFINITY
#endif
#if !defined(NV_INF) && defined(INFINITY)
# define NV_INF (NV)INFINITY
#endif
#if !defined(NV_INF) && defined(INF)
# define NV_INF (NV)INF
#endif
#if !defined(NV_INF) && defined(USE_LONG_DOUBLE) && defined(HUGE_VALL)
# define NV_INF (NV)HUGE_VALL
#endif
#if !defined(NV_INF) && defined(HUGE_VAL)
# define NV_INF (NV)HUGE_VAL
#endif
/* NV_NAN compatibility for Perl 5.6 */
#if !defined(NV_NAN) && defined(USE_LONG_DOUBLE)
# if !defined(NV_NAN) && defined(LDBL_NAN)
# define NV_NAN LDBL_NAN
# endif
# if !defined(NV_NAN) && defined(LDBL_QNAN)
# define NV_NAN LDBL_QNAN
# endif
# if !defined(NV_NAN) && defined(LDBL_SNAN)
# define NV_NAN LDBL_SNAN
# endif
#endif
#if !defined(NV_NAN) && defined(DBL_NAN)
# define NV_NAN (NV)DBL_NAN
#endif
#if !defined(NV_NAN) && defined(DBL_QNAN)
# define NV_NAN (NV)DBL_QNAN
#endif
#if !defined(NV_NAN) && defined(DBL_SNAN)
# define NV_NAN (NV)DBL_SNAN
#endif
#if !defined(NV_NAN) && defined(QNAN)
# define NV_NAN (NV)QNAN
#endif
#if !defined(NV_NAN) && defined(SNAN)
# define NV_NAN (NV)SNAN
#endif
#if !defined(NV_NAN) && defined(NAN)
# define NV_NAN (NV)NAN
#endif
/* modfl() segfaults for -Duselongdouble && 64-bit mingw64 && mingw
runtime version 4.0 [perl #125924] */
#if defined(USE_LONG_DOUBLE) && defined(__MINGW64__) \
&& __MINGW64_VERSION_MAJOR == 4 && __MINGW64_VERSION_MINOR == 0
#undef HAS_MODFL
#undef Perl_modf
#define Perl_modf(nv, ip) mingw_modfl(nv, ip)
long double
mingw_modfl(long double x, long double *ip)
{
*ip = truncl(x);
return (x == *ip ? copysignl(0.0L, x) : x - *ip);
}
#endif
/* some old perls do not have this, try to make it work, no */
/* guarantees, though. if it breaks, you get to keep the pieces. */
#ifndef UTF8_MAXBYTES
# define UTF8_MAXBYTES 13
#endif
/* 5.6: */
#ifndef IS_NUMBER_IN_UV
#define IS_NUMBER_IN_UV 0x01 /* number within UV range (maybe not
int). value returned in pointed-
to UV */
#define IS_NUMBER_GREATER_THAN_UV_MAX 0x02 /* pointed to UV undefined */
#define IS_NUMBER_NOT_INT 0x04 /* saw . or E notation */
#define IS_NUMBER_NEG 0x08 /* leading minus sign */
#define IS_NUMBER_INFINITY 0x10 /* this is big */
#define IS_NUMBER_NAN 0x20 /* this is not */
#endif
#ifndef UNI_DISPLAY_QQ
#define UNI_DISPLAY_ISPRINT 0x0001
#define UNI_DISPLAY_BACKSLASH 0x0002
#define UNI_DISPLAY_QQ (UNI_DISPLAY_ISPRINT|UNI_DISPLAY_BACKSLASH)
#define UNI_DISPLAY_REGEX (UNI_DISPLAY_ISPRINT|UNI_DISPLAY_BACKSLASH)
#endif
/* with 5.6 hek can only be non-utf8 */
#ifndef HeKUTF8
#define HeKUTF8(he) 0
#endif
#ifndef GV_NOADD_NOINIT
#define GV_NOADD_NOINIT 0
#endif
/* since 5.8.1 */
#ifndef SvIsCOW_shared_hash
#define SvIsCOW_shared_hash(pv) 0
#endif
/* 5.8.1 has a broken assert_not_ROK */
#if PERL_VERSION == 8 && PERL_SUBVERSION == 1
# undef assert_not_ROK
# if defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
# define assert_not_ROK(sv) ({assert(!SvROK(sv) || !SvRV(sv));}),
# else
# define assert_not_ROK(sv)
# endif
#endif
/* 5.8 problem, it was renamed to HINT_BYTES with 5.8.0 */
#if PERL_VERSION < 7
#ifndef HINT_BYTES
#define HINT_BYTES 8
#endif
#endif
/* compatibility with perl <5.14 */
/* added with 5.13.6 */
#ifndef sv_cmp_flags
# define sv_cmp_flags(a,b,flags) sv_cmp((a),(b))
#endif
#ifndef SvTRUE_nomg
#define SvTRUE_nomg SvTRUE
#endif
#ifndef SvNV_nomg
#define SvNV_nomg SvNV
#endif
#ifndef PERL_UNICODE_MAX
#define PERL_UNICODE_MAX 0x10FFFF
#endif
#ifndef HvNAMELEN_get
# define HvNAMELEN_get(hv) strlen (HvNAME (hv))
#endif
#ifndef HvNAMELEN
# define HvNAMELEN(hv) HvNAMELEN_get (hv)
#endif
#ifndef HvNAMEUTF8
# define HvNAMEUTF8(hv) 0
#endif
/* since 5.14 check use warnings 'nonchar' */
#ifdef WARN_NONCHAR
#define WARNER_NONCHAR(hi) \
Perl_ck_warner_d(aTHX_ packWARN(WARN_NONCHAR), \
"Unicode non-character U+%04" UVXf " is not " \
"recommended for open interchange", hi)
/* before check use warnings 'utf8' */
#elif PERL_VERSION > 10
#define WARNER_NONCHAR(hi) \
Perl_ck_warner_d(aTHX_ packWARN(WARN_UTF8), \
"Unicode non-character U+%04" UVXf " is illegal " \
"for interchange", hi)
#else
#define WARNER_NONCHAR(hi) \
Perl_warner(aTHX_ packWARN(WARN_UTF8), \
"Unicode non-character U+%04lX is illegal", (unsigned long)hi)
#endif
/* since 5.16 */
#ifndef GV_NO_SVGMAGIC
#define GV_NO_SVGMAGIC 0
#endif
/* since 5.18 */
#ifndef SvREFCNT_dec_NN
#define SvREFCNT_dec_NN(sv) SvREFCNT_dec(sv)
#endif
/* from cperl */
#ifndef strEQc
/* the buffer ends with \0, includes comparison of the \0.
better than strEQ as it uses memcmp, word-wise comparison. */
# define strEQc(s, c) memEQ(s, ("" c ""), sizeof(c))
#endif
#ifndef memEQc
/* excluding the final \0, so the string s may continue */
# define memEQc(s, c) memEQ(s, ("" c ""), sizeof(c)-1)
#endif
#ifndef He_IS_SVKEY
# define He_IS_SVKEY(he) HeKLEN (he) == HEf_SVKEY
#endif
/* av_len has 2 different possible types */
#ifndef HVMAX_T
# if PERL_VERSION >= 20
# define HVMAX_T SSize_t
# else
# define HVMAX_T I32
# endif
#endif
/* and riter 3 */
#ifndef RITER_T
# ifdef USE_CPERL
# if PERL_VERSION >= 25
# define RITER_T U32
# else
# define RITER_T SSize_t
# endif
# else
# define RITER_T I32
# endif
#endif
// i.e. "JSON" in big-endian
#define JSON_MAGIC 0x4A534F4E
/* types */
#define JSON_TYPE_SCALAR 0x0000
#define JSON_TYPE_BOOL 0x0001
#define JSON_TYPE_INT 0x0002
#define JSON_TYPE_FLOAT 0x0003
#define JSON_TYPE_STRING 0x0004
/* flags */
#define JSON_TYPE_CAN_BE_NULL 0x0100
/* null type */
#define JSON_TYPE_NULL JSON_TYPE_CAN_BE_NULL
/* classes */
#define JSON_TYPE_CLASS "Cpanel::JSON::XS::Type"
#define JSON_TYPE_ARRAYOF_CLASS "Cpanel::JSON::XS::Type::ArrayOf"
#define JSON_TYPE_HASHOF_CLASS "Cpanel::JSON::XS::Type::HashOf"
#define JSON_TYPE_ANYOF_CLASS "Cpanel::JSON::XS::Type::AnyOf"
#define JSON_TYPE_ANYOF_SCALAR_INDEX 0
#define JSON_TYPE_ANYOF_ARRAY_INDEX 1
#define JSON_TYPE_ANYOF_HASH_INDEX 2
/* three extra for rounding, sign, and end of string */
#define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 3)
#define F_ASCII 0x00000001UL
#define F_LATIN1 0x00000002UL
#define F_UTF8 0x00000004UL
#define F_INDENT 0x00000008UL
#define F_CANONICAL 0x00000010UL
#define F_SPACE_BEFORE 0x00000020UL
#define F_SPACE_AFTER 0x00000040UL
#define F_ALLOW_NONREF 0x00000100UL
#define F_SHRINK 0x00000200UL
#define F_ALLOW_BLESSED 0x00000400UL
#define F_CONV_BLESSED 0x00000800UL
#define F_RELAXED 0x00001000UL
#define F_ALLOW_UNKNOWN 0x00002000UL
#define F_ALLOW_TAGS 0x00004000UL
#define F_BINARY 0x00008000UL
#define F_ALLOW_BAREKEY 0x00010000UL
#define F_ALLOW_SQUOTE 0x00020000UL
#define F_ALLOW_BIGNUM 0x00040000UL
#define F_ESCAPE_SLASH 0x00080000UL
#define F_SORT_BY 0x00100000UL
#define F_ALLOW_STRINGIFY 0x00200000UL
#define F_UNBLESSED_BOOL 0x00400000UL
#define F_ALLOW_DUPKEYS 0x00800000UL
#define F_REQUIRE_TYPES 0x01000000UL
#define F_TYPE_ALL_STRING 0x02000000UL
#define F_DUPKEYS_AS_AREF 0x04000000UL
#define F_DUPKEYS_FIRST 0x08000000UL /* internal only */
#define F_HOOK 0x80000000UL /* some hooks exist, so slow-path processing */
#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER
#define SET_RELAXED (F_RELAXED | F_ALLOW_BAREKEY | F_ALLOW_SQUOTE | F_ALLOW_DUPKEYS)
#define INIT_SIZE 32 /* initial scalar size to be allocated */
#define INDENT_STEP 3 /* default spaces per indentation level */
#define SHORT_STRING_LEN 16384 /* special-case strings of up to this size */
#define DECODE_WANTS_OCTETS(json) ((json)->flags & F_UTF8)
#define SB do {
#define SE } while (0)
#if __GNUC__ >= 3
# define _expect(expr,value) __builtin_expect ((expr), (value))
# define INLINE static inline
#else
# define _expect(expr,value) (expr)
# define INLINE static
#endif
#ifndef LIKELY
#define LIKELY(expr) _expect ((long)(expr) != 0, 1)
#define UNLIKELY(expr) _expect ((long)(expr) != 0, 0)
#endif
#define IN_RANGE_INC(type,val,beg,end) \
((unsigned type)((unsigned type)(val) - (unsigned type)(beg)) \
<= (unsigned type)((unsigned type)(end) - (unsigned type)(beg)))
#define ERR_NESTING_EXCEEDED "json text or perl structure exceeds maximum nesting level (max_depth set too low?)"
# define JSON_STASH MY_CXT.json_stash
#define MY_CXT_KEY "Cpanel::JSON::XS::_guts"
typedef struct {
HV *json_stash; /* Cpanel::JSON::XS:: */
HV *json_boolean_stash; /* JSON::PP::Boolean:: */
HV *jsonold_boolean_stash; /* JSON::XS::Boolean:: if empty will be (HV*)1 */
HV *mojo_boolean_stash; /* Mojo::JSON::_Bool:: if empty will be (HV*)1 */
SV *json_true, *json_false;
SV *sv_json;
} my_cxt_t;
/* the amount of HEs to allocate on the stack, when sorting keys */
#define STACK_HES 64
START_MY_CXT
INLINE SV * get_bool (pTHX_ const char *name);
enum {
INCR_M_WS = 0, /* initial whitespace skipping, must be 0 */
INCR_M_STR, /* inside string */
INCR_M_BS, /* inside backslash */
INCR_M_C0, /* inside comment in initial whitespace sequence */
INCR_M_C1, /* inside comment in other places */
INCR_M_JSON /* outside anything, count nesting */
};
#define INCR_DONE(json) ((json)->incr_nest <= 0 && (json)->incr_mode == INCR_M_JSON)
typedef struct {
U32 flags;
U32 max_depth;
U32 indent_length; /* how much padding to use when indenting */
STRLEN max_size;
SV *cb_object;
HV *cb_sk_object;
SV *cb_sort_by;
/* for the incremental parser */
SV *incr_text; /* the source text so far */
STRLEN incr_pos; /* the current offset into the text */
int incr_nest; /* {[]}-nesting level */
unsigned char incr_mode;
unsigned char infnan_mode;
/* corruption check */
U32 magic;
} JSON;
INLINE void
json_init (JSON *json)
{
Zero (json, 1, JSON);
json->max_depth = 512;
json->indent_length = INDENT_STEP;
json->magic = JSON_MAGIC;
}
/* dTHX/threads TODO
END dtor not needed for these, all of these *s refcnts are owned by the stash. */
static void
init_MY_CXT(pTHX_ my_cxt_t * cxt)
{
cxt->json_stash = gv_stashpvn ("Cpanel::JSON::XS", sizeof("Cpanel::JSON::XS")-1, 1);
cxt->json_boolean_stash = gv_stashpvn ("JSON::PP::Boolean", sizeof("JSON::PP::Boolean")-1, 1);
cxt->jsonold_boolean_stash = gv_stashpvn ("JSON::XS::Boolean", sizeof("JSON::XS::Boolean")-1, 0);
cxt->mojo_boolean_stash = gv_stashpvn ("Mojo::JSON::_Bool", sizeof("Mojo::JSON::_Bool")-1, 0);
if ( !cxt->mojo_boolean_stash )
cxt->mojo_boolean_stash = (HV*)1; /* invalid ptr to compare against, better than a NULL stash */
if ( !cxt->jsonold_boolean_stash )
cxt->jsonold_boolean_stash = (HV*)1;
cxt->json_true = get_bool (aTHX_ "Cpanel::JSON::XS::true");
cxt->json_false = get_bool (aTHX_ "Cpanel::JSON::XS::false");
cxt->sv_json = newSVpv ("JSON", 0);
SvREADONLY_on (cxt->sv_json);
}
/*/////////////////////////////////////////////////////////////////////////// */
/* utility functions */
/* Validate the JSON struct which might get corrupted by wrong FREEZE/THAW
methods, or other serializers, or corrupting our magic object.
E.g. https://github.com/rurban/Cpanel-JSON-XS/issues/192 */
INLINE bool
json_validate (JSON *json)
{
return json->magic == JSON_MAGIC;
}
/* Unpacks the 2 boolean objects from the global references */
INLINE SV *
get_bool (pTHX_ const char *name)
{
dMY_CXT;
#if PERL_VERSION > 7
SV *sv = get_sv (name, 1);
#else
SV *sv = GvSV(gv_fetchpv(name, 1, SVt_PV));
#endif
SV* rv = SvRV(sv);
if (!SvOBJECT(sv) || !SvSTASH(sv)) {
SvREADONLY_off (sv);
SvREADONLY_off (rv);
(void)sv_bless(sv, MY_CXT.json_boolean_stash); /* bless the ref */
}
SvREADONLY_on (rv);
SvREADONLY_on (sv);
return sv;
}
INLINE void
shrink (pTHX_ SV *sv)
{
/* ignore errors */
(void)sv_utf8_downgrade (sv, 1);
if (SvLEN (sv) > SvCUR (sv) + 1)
{
#ifdef SvPV_shrink_to_cur
SvPV_shrink_to_cur (sv);
#elif defined (SvPV_renew)
SvPV_renew (sv, SvCUR (sv) + 1);
#endif
}
}
/* Decode an utf-8 character and return it, or (UV)-1 in
case of an error.
We special-case "safe" characters from U+80 .. U+7FF,
but use the very good perl function until 5.36 to parse anything else.
note that we never call this function for an ascii codepoints.
With 5.36 perl5 removed the API to decode utf8 again with flags for relaxed,
so we have to hack around this regression again.
*/
INLINE UV
decode_utf8 (pTHX_ unsigned char *s, STRLEN len, int relaxed, STRLEN *clen)
{
if (LIKELY(len >= 2
&& IN_RANGE_INC (char, s[0], 0xc2, 0xdf)
&& IN_RANGE_INC (char, s[1], 0x80, 0xbf)))
{
*clen = 2;
return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f);
}
else {
/* Since perl 5.14 we can disallow illegal unicode above U+10FFFF.
Before we could only warn with warnings 'utf8'.
We accept only valid unicode, unless we are in the relaxed mode,
which allows SUPER, above U+10FFFF.
*/
#if PERL_VERSION > 36
UV c = utf8n_to_uvchr (s, len, clen,
UTF8_CHECK_ONLY | (relaxed ? 0 : UTF8_DISALLOW_SUPER));
#elif PERL_VERSION > 12
UV c = utf8n_to_uvuni (s, len, clen,
UTF8_CHECK_ONLY | (relaxed ? 0 : UTF8_DISALLOW_SUPER));
#elif PERL_VERSION >= 8
UV c = utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY);
#endif
#if PERL_VERSION >= 8 && PERL_VERSION <= 12
if (c > PERL_UNICODE_MAX && !relaxed)
*clen = -1;
#endif
#if PERL_VERSION >= 8
return c;
#else
/* 5.6 does not detect certain ill-formed sequences, esp. overflows,
which are security relevant. so we add code to detect these. */
UV c = utf8_to_uv(s, len, clen, UTF8_CHECK_ONLY);
if (!relaxed) {
if (!c || c > PERL_UNICODE_MAX)
*clen = -1;
/* need to check manually for some overflows. 5.6 unicode bug */
else if (len >= 2
&& IN_RANGE_INC (char, s[0], 0xc0, 0xfe)
&& !IN_RANGE_INC (char, s[0], 0xc2, 0xdf)) {
U8 *s0, *send;
UV uv = *s;
UV expectlen = UTF8SKIP(s);
#define UTF_CONTINUATION_MASK ((U8) ((1U << 6) - 1))
#define UTF_ACCUMULATION_OVERFLOW_MASK \
(((UV) UTF_CONTINUATION_MASK) << ((sizeof(UV) * 8) - 6))
s0 = s;
/*printf ("maybe overlong <%.*s> %d/%d %x %x\n", len, s, c,
*clen, s[0], s[1]);*/
if (*clen > 4) {
*clen = -1;
return c;
}
send = (U8*) s0 + ((expectlen <= len) ? len : len);
for (s = s0 + 1; s < send; s++) {
if (LIKELY(UTF8_IS_CONTINUATION(*s))) {
if (uv & UTF_ACCUMULATION_OVERFLOW_MASK) {
/*printf ("overflow\n");*/
*clen = -1;
return c;
}
uv = UTF8_ACCUMULATE(uv, *s);
}
else {
/*printf ("unexpected non continuation\n");*/
*clen = -1;
return c;
}
}
}
}
return c;
#endif
}
}
/* Likewise for encoding, also never called for ascii codepoints. */
/* This function takes advantage of this fact, although current gcc's */
/* seem to optimise the check for >= 0x80 away anyways. */
INLINE unsigned char *
encode_utf8 (unsigned char *s, UV ch)
{
if (UNLIKELY(ch < 0x000080))
*s++ = ch;
else if (LIKELY(ch < 0x000800))
*s++ = 0xc0 | ( ch >> 6),
*s++ = 0x80 | ( ch & 0x3f);
else if (ch < 0x010000)
*s++ = 0xe0 | ( ch >> 12),
*s++ = 0x80 | ((ch >> 6) & 0x3f),
*s++ = 0x80 | ( ch & 0x3f);
else if (ch < 0x110000)
*s++ = 0xf0 | ( ch >> 18),
*s++ = 0x80 | ((ch >> 12) & 0x3f),
*s++ = 0x80 | ((ch >> 6) & 0x3f),
*s++ = 0x80 | ( ch & 0x3f);
return s;
}
/* convert offset to character index, sv must be string */
static STRLEN
ptr_to_index (pTHX_ SV *sv, const STRLEN offset)
{
return SvUTF8 (sv)
? (STRLEN)utf8_distance ((U8*)(SvPVX(sv)+offset), (U8*)SvPVX (sv))
: offset;
}
/*/////////////////////////////////////////////////////////////////////////// */
/* fp hell */
#ifdef HAVE_NO_POWL
/* Ulisse Monari: this is a patch for AIX 5.3, perl 5.8.8 without HAS_LONG_DOUBLE
There Perl_pow maps to pow(...) - NOT TO powl(...), core dumps at Perl_pow(...)
Base code is from http://bytes.com/topic/c/answers/748317-replacement-pow-function
This is my change to fs_pow that goes into libc/libm for calling fmod/exp/log.
NEED TO MODIFY Makefile, after perl Makefile.PL by adding "-lm" onto the LDDLFLAGS line */
static double fs_powEx(double x, double y)
{
double p = 0;
if (0 > x && fmod(y, 1) == 0) {
if (fmod(y, 2) == 0) {
p = exp(log(-x) * y);
} else {
p = -exp(log(-x) * y);
}
} else {
if (x != 0 || 0 >= y) {
p = exp(log( x) * y);
}
}
return p;
}
#endif
#ifndef Perl_strtod
/* scan a group of digits, and a trailing exponent */
static void
json_atof_scan1 (const char *s, NV *accum, int *expo, int postdp, int maxdepth)
{
UV uaccum = 0;
int eaccum = 0;
#if defined(HAVE_BAD_POWL)
*accum = strtold(s, NULL);
#else
/* if we recurse too deep, skip all remaining digits */
/* to avoid a stack overflow attack */
if (UNLIKELY(--maxdepth <= 0))
while (((U8)*s - '0') < 10)
++s;
for (;;)
{
U8 dig = (U8)*s - '0';
if (UNLIKELY(dig >= 10))
{
if (dig == (U8)((U8)'.' - (U8)'0'))
{
++s;
json_atof_scan1 (s, accum, expo, 1, maxdepth);
}
else if ((dig | ' ') == 'e' - '0')
{
int exp2 = 0;
int neg = 0;
++s;
if (*s == '-')
{
++s;
neg = 1;
}
else if (*s == '+')
++s;
while ((dig = (U8)*s - '0') < 10)
exp2 = exp2 * 10 + *s++ - '0';
*expo += neg ? -exp2 : exp2;
}
break;
}
++s;
uaccum = uaccum * 10 + dig;
++eaccum;
/* if we have too many digits, then recurse for more */
/* we actually do this for rather few digits */
if (uaccum >= (UV_MAX - 9) / 10)
{
if (postdp) *expo -= eaccum;
json_atof_scan1 (s, accum, expo, postdp, maxdepth);
if (postdp) *expo += eaccum;
break;
}
}
/* This relies greatly on the quality of the pow ()
implementation of the platform, but a good
implementation is hard to beat.
(IEEE 754 conformant ones are required to be exact) */
if (postdp)
*expo -= eaccum;
#ifdef HAVE_NO_POWL
/* powf() unfortunately is not accurate enough */
*accum += uaccum * fs_powEx(10., *expo );
#else
*accum += uaccum * Perl_pow (10., *expo);
#endif
*expo += eaccum;
#endif
}
#endif
static NV
json_atof (const char *s)
{
NV accum = 0.;
#ifndef Perl_strtod
int expo = 0;
#endif
int neg = 0;
if (*s == '-')
{
++s;
neg = 1;
}
/* Fix accuracy mismatches compared to perl (using strtod).
Available since v5.21.4, but see perl5 af5a4640bf25a62438c05f73a87c1d6be6096b02.
Resets LC_NUMERIC only since v5.29.10. (dot, not comma)
See GH #154, where the pow method may return a different result than strtod.
*/
#ifdef Perl_strtod
accum = Perl_strtod (s, NULL);
#else
/* a recursion depth of ten gives us >>500 bits */
json_atof_scan1 (s, &accum, &expo, 0, 10);
#endif
return neg ? -accum : accum;
}
INLINE int
is_bignum_obj (pTHX_ SV *sv)
{
HV *stash = SvSTASH (sv);
return (stash == gv_stashpvs ("Math::BigInt", 0) || stash == gv_stashpvs ("Math::BigFloat", 0)) ? 1 : 0;
}
INLINE int
is_bool_obj (pTHX_ SV *sv)
{
dMY_CXT;
HV *bstash = MY_CXT.json_boolean_stash; /* JSON-XS-3.x interop (Types::Serialiser/JSON::PP::Boolean) */
HV *oldstash = MY_CXT.jsonold_boolean_stash; /* JSON-XS-2.x interop (JSON::XS::Boolean) */
HV *mstash = MY_CXT.mojo_boolean_stash; /* Mojo::JSON::_Bool interop */
HV *stash = SvSTASH (sv);
return (stash == bstash || stash == mstash || stash == oldstash) ? 1 : 0;
}
/* target of scalar reference is bool? -1 == nope, 0 == false, 1 == true */
static int
ref_bool_type (pTHX_ SV *sv)
{
svtype svt = SvTYPE (sv);
if (svt < SVt_PVAV)
{
STRLEN len = 0;
char *pv = svt ? SvPV_nomg (sv, len) : 0;
if (len == 1) {
if (*pv == '1')
return 1;
else if (*pv == '0')
return 0;
}
}
return -1;
}
/* returns whether scalar is not a reference in the sense of allow_nonref */
static int
json_nonref (pTHX_ SV *scalar)
{
if (!SvROK (scalar))
return 1;
scalar = SvRV (scalar);
if (!SvOBJECT (scalar) && ref_bool_type (aTHX_ scalar) >= 0)
return 1;
if (SvOBJECT (scalar) && is_bool_obj (aTHX_ scalar))
return 1;
return 0;
}
/*/////////////////////////////////////////////////////////////////////////// */
/* encoder */
/* structure used for encoding JSON */
typedef struct
{
char *cur; /* SvPVX (sv) + current output position */
char *end; /* SvEND (sv) */
SV *sv; /* result scalar */
JSON json;
U32 indent; /* indentation level */
UV limit; /* escape character values >= this value when encoding */
} enc_t;
INLINE void
need (pTHX_ enc_t *enc, STRLEN len)
{
#if PERL_VERSION > 8 || (PERL_VERSION == 8 && PERL_SUBVERSION >= 1)
DEBUG_v(Perl_deb(aTHX_ "need enc: %p %p %4ld, want: %lu\n", enc->cur, enc->end,
(long)(enc->end - enc->cur), (unsigned long)len));
#endif
assert(enc->cur <= enc->end);
if (UNLIKELY(enc->cur + len >= enc->end))
{
STRLEN cur = enc->cur - (char *)SvPVX (enc->sv);
SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
enc->cur = SvPVX (enc->sv) + cur;
enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
}
}
INLINE void
encode_ch (pTHX_ enc_t *enc, char ch)
{
need (aTHX_ enc, 1);
*enc->cur++ = ch;
}
static void
encode_str (pTHX_ enc_t *enc, char *str, STRLEN len, int is_utf8)
{
char *end = str + len;
#if PERL_VERSION < 8
/* perl5.6 encodes to utf8 automatically, reverse it */
if (is_utf8 && (enc->json.flags & F_BINARY))
{
str = (char *)utf8_to_bytes((U8*)str, &len);
if (!str)
croak ("illegal unicode character in binary string", str);
end = str + len;
}
#endif
need (aTHX_ enc, len);
while (str < end)
{
unsigned char ch = *(unsigned char *)str;
#if PERL_VERSION > 8 || (PERL_VERSION == 8 && PERL_SUBVERSION >= 1)
DEBUG_v(Perl_deb(aTHX_ "str enc: %p %p %4ld, want: %lu\n", enc->cur, enc->end,
(long)(enc->end - enc->cur), (long unsigned)len));
#endif
if (LIKELY(ch >= 0x20 && ch < 0x80)) /* most common case */
{
assert(enc->cur <= enc->end);
if (UNLIKELY(ch == '"')) /* but with slow exceptions */
{
need (aTHX_ enc, 2);
*enc->cur++ = '\\';
*enc->cur++ = '"';
++len;
}
else if (UNLIKELY(ch == '\\'))
{
need (aTHX_ enc, 2);
*enc->cur++ = '\\';
*enc->cur++ = '\\';
++len;
}
else if (UNLIKELY(ch == '/' && (enc->json.flags & F_ESCAPE_SLASH)))
{
need (aTHX_ enc, 2);
*enc->cur++ = '\\';
*enc->cur++ = '/';
++len;
}
else {
need (aTHX_ enc, 1);
*enc->cur++ = ch;
}
++str;
}
else
{
assert(enc->cur <= enc->end);
switch (ch)
{
case '\010': need (aTHX_ enc, 2);
*enc->cur++ = '\\'; *enc->cur++ = 'b'; ++len; ++str; break;
case '\011': need (aTHX_ enc, 2);
*enc->cur++ = '\\'; *enc->cur++ = 't'; ++len; ++str; break;
case '\012': need (aTHX_ enc, 2);
*enc->cur++ = '\\'; *enc->cur++ = 'n'; ++len; ++str; break;
case '\014': need (aTHX_ enc, 2);
*enc->cur++ = '\\'; *enc->cur++ = 'f'; ++len; ++str; break;
case '\015': need (aTHX_ enc, 2);
*enc->cur++ = '\\'; *enc->cur++ = 'r'; ++len; ++str; break;
default:
{
STRLEN clen;
UV uch;
if (is_utf8 && !(enc->json.flags & F_BINARY))
{
uch = decode_utf8 (aTHX_ (unsigned char *)str, end - str,
enc->json.flags & F_RELAXED, &clen);
if (clen == (STRLEN)-1)
croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str);
}
else
{
uch = ch;
clen = 1;
}
if (uch < 0x80/*0x20*/ || uch >= enc->limit)
{
if (enc->json.flags & F_BINARY)
{
/* MB cannot arrive here */
need (aTHX_ enc, 4);
*enc->cur++ = '\\';
*enc->cur++ = 'x';
*enc->cur++ = PL_hexdigit [(uch >> 4) & 15];
*enc->cur++ = PL_hexdigit [ uch & 15];
len += 3;
}
else if (uch >= 0x10000UL)
{
if (uch >= 0x110000UL)
croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch);
need (aTHX_ enc, 12);
sprintf (enc->cur, "\\u%04x\\u%04x",
(int)((uch - 0x10000) / 0x400 + 0xD800),
(int)((uch - 0x10000) % 0x400 + 0xDC00));
enc->cur += 12;