Repository navigation
Expand file tree
/
Copy pathtinygltf_json_c.h
More file actions
1743 lines (1619 loc) · 55.9 KB
/
Copy pathtinygltf_json_c.h
File metadata and controls
1743 lines (1619 loc) · 55.9 KB
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
#ifndef TINYGLTF_JSON_C_H_
#define TINYGLTF_JSON_C_H_
/*
* Floating-point conversion attribution:
*
* - JSON number parsing in this C11 port is based on fast_float.
* Upstream: https://github.com/fastfloat/fast_float
* Copyright (c) 2021 The fast_float authors.
* License: Apache License 2.0, MIT License, or Boost Software License 1.0.
*
* - JSON number serialization in this C11 port is based on Dragonbox-style
* shortest round-trippable binary floating-point to decimal conversion.
* Upstream: https://github.com/jk-jeon/dragonbox
* Copyright 2020-2024 Junekey Jeon.
* License: Apache License 2.0 with LLVM Exceptions, or Boost Software
* License 1.0.
*/
#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef enum tg3json_type {
TG3JSON_NULL = 0,
TG3JSON_BOOL = 1,
TG3JSON_INT = 2,
TG3JSON_REAL = 3,
TG3JSON_STRING = 4,
TG3JSON_ARRAY = 5,
TG3JSON_OBJECT = 6
} tg3json_type;
typedef struct tg3json_value tg3json_value;
typedef struct tg3json_string {
char *ptr;
size_t len;
} tg3json_string;
typedef struct tg3json_array {
tg3json_value *items;
size_t count;
} tg3json_array;
typedef struct tg3json_object_entry {
char *key;
size_t key_len;
tg3json_value *value;
} tg3json_object_entry;
typedef struct tg3json_object {
tg3json_object_entry *items;
size_t count;
} tg3json_object;
struct tg3json_value {
tg3json_type type;
union {
int boolean;
int64_t integer;
double real;
tg3json_string string;
tg3json_array array;
tg3json_object object;
} u;
};
int tg3json_parse_n(const char *data, size_t len, size_t depth_limit,
tg3json_value *out_value, const char **out_error_pos);
int tg3json_parse(const char *begin, const char *end, size_t depth_limit,
tg3json_value *out_value, const char **out_error_pos);
typedef struct tg3json_parse_options {
size_t depth_limit; /* 0 = default */
size_t memory_budget; /* 0 = unlimited */
size_t max_single_alloc; /* 0 = unlimited */
size_t max_string_length; /* 0 = unlimited */
int parse_float32; /* 1 = round JSON reals to float */
} tg3json_parse_options;
int tg3json_parse_n_opts(const char *data, size_t len,
const tg3json_parse_options *options,
tg3json_value *out_value,
const char **out_error_pos);
void tg3json_value_free(tg3json_value *value);
void tg3json_value_init_null(tg3json_value *value);
void tg3json_value_init_bool(tg3json_value *value, int boolean_value);
void tg3json_value_init_int(tg3json_value *value, int64_t integer_value);
void tg3json_value_init_real(tg3json_value *value, double real_value);
int tg3json_value_init_string_n(tg3json_value *value, const char *str, size_t len);
int tg3json_value_init_string(tg3json_value *value, const char *str);
void tg3json_value_init_array(tg3json_value *value);
void tg3json_value_init_object(tg3json_value *value);
int tg3json_value_copy(tg3json_value *dst, const tg3json_value *src);
const tg3json_value *tg3json_object_get(const tg3json_value *object,
const char *key);
const tg3json_value *tg3json_object_get_n(const tg3json_value *object,
const char *key, size_t key_len);
tg3json_value *tg3json_object_get_mut(tg3json_value *object, const char *key);
tg3json_value *tg3json_object_get_mut_n(tg3json_value *object,
const char *key, size_t key_len);
const tg3json_object_entry *tg3json_object_at(const tg3json_value *object,
size_t index);
size_t tg3json_object_size(const tg3json_value *object);
int tg3json_object_set_take_n(tg3json_value *object, const char *key, size_t key_len,
tg3json_value *value);
int tg3json_object_set_take(tg3json_value *object, const char *key,
tg3json_value *value);
int tg3json_object_set_copy_n(tg3json_value *object, const char *key, size_t key_len,
const tg3json_value *value);
int tg3json_object_set_copy(tg3json_value *object, const char *key,
const tg3json_value *value);
const tg3json_value *tg3json_array_get(const tg3json_value *array, size_t index);
size_t tg3json_array_size(const tg3json_value *array);
int tg3json_array_append_take(tg3json_value *array, tg3json_value *value);
int tg3json_array_append_copy(tg3json_value *array, const tg3json_value *value);
char *tg3json_stringify(const tg3json_value *value, size_t *out_len);
char *tg3json_stringify_pretty(const tg3json_value *value, int indent, size_t *out_len);
#ifdef __cplusplus
} /* extern "C" */
#endif
#ifdef TINYGLTF_JSON_C_IMPLEMENTATION
#ifndef TINYGLTF_JSON_NO_STDLIB
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <float.h>
#if defined(_MSC_VER) || (defined(LDBL_MANT_DIG) && LDBL_MANT_DIG <= 53)
#define TG3JSON_USE_STDLIB_FPCONV 1
#endif
#endif
#ifndef TG3JSON_USE_STDLIB_FPCONV
#define TG3JSON_USE_STDLIB_FPCONV 0
#endif
#ifdef TINYGLTF_JSON_NO_STDLIB
static void *tg3json__memcpy_fallback(void *dst, const void *src, size_t sz) {
unsigned char *d = (unsigned char *)dst;
const unsigned char *s = (const unsigned char *)src;
while (sz--) *d++ = *s++;
return dst;
}
static void *tg3json__memset_fallback(void *dst, int val, size_t sz) {
unsigned char *d = (unsigned char *)dst;
while (sz--) *d++ = (unsigned char)val;
return dst;
}
static int tg3json__memcmp_fallback(const void *a, const void *b, size_t sz) {
const unsigned char *pa = (const unsigned char *)a;
const unsigned char *pb = (const unsigned char *)b;
while (sz--) {
if (*pa != *pb) return (int)*pa - (int)*pb;
++pa;
++pb;
}
return 0;
}
static size_t tg3json__strlen_fallback(const char *str) {
const char *p = str;
while (*p) ++p;
return (size_t)(p - str);
}
#endif
#ifndef TINYGLTF_JSON_MALLOC
#ifdef TINYGLTF_JSON_NO_STDLIB
#define TINYGLTF_JSON_MALLOC(sz) NULL
#else
#define TINYGLTF_JSON_MALLOC(sz) malloc(sz)
#endif
#endif
#ifndef TINYGLTF_JSON_REALLOC
#ifdef TINYGLTF_JSON_NO_STDLIB
#define TINYGLTF_JSON_REALLOC(ptr, sz) NULL
#else
#define TINYGLTF_JSON_REALLOC(ptr, sz) realloc(ptr, sz)
#endif
#endif
#ifndef TINYGLTF_JSON_FREE
#ifdef TINYGLTF_JSON_NO_STDLIB
#define TINYGLTF_JSON_FREE(ptr) ((void)(ptr))
#else
#define TINYGLTF_JSON_FREE(ptr) free(ptr)
#endif
#endif
#ifndef TINYGLTF_JSON_MEMCPY
#ifdef TINYGLTF_JSON_NO_STDLIB
#define TINYGLTF_JSON_MEMCPY(dst, src, sz) tg3json__memcpy_fallback((dst), (src), (sz))
#else
#define TINYGLTF_JSON_MEMCPY(dst, src, sz) memcpy(dst, src, sz)
#endif
#endif
#ifndef TINYGLTF_JSON_MEMSET
#ifdef TINYGLTF_JSON_NO_STDLIB
#define TINYGLTF_JSON_MEMSET(dst, val, sz) tg3json__memset_fallback((dst), (val), (sz))
#else
#define TINYGLTF_JSON_MEMSET(dst, val, sz) memset(dst, val, sz)
#endif
#endif
#ifndef TINYGLTF_JSON_MEMCMP
#ifdef TINYGLTF_JSON_NO_STDLIB
#define TINYGLTF_JSON_MEMCMP(a, b, sz) tg3json__memcmp_fallback((a), (b), (sz))
#else
#define TINYGLTF_JSON_MEMCMP(a, b, sz) memcmp(a, b, sz)
#endif
#endif
#ifndef TINYGLTF_JSON_STRLEN
#ifdef TINYGLTF_JSON_NO_STDLIB
#define TINYGLTF_JSON_STRLEN(str) tg3json__strlen_fallback((str))
#else
#define TINYGLTF_JSON_STRLEN(str) strlen(str)
#endif
#endif
static size_t tg3json__itoa(int64_t value, char *buf) {
char *p = buf;
uint64_t val;
char tmp[24];
int i = 0;
if (value < 0) {
*p++ = '-';
val = (value == -9223372036854775807LL - 1) ? 9223372036854775808ULL : (uint64_t)(-value);
} else {
val = (uint64_t)value;
}
if (val == 0) {
*p++ = '0';
*p = '\0';
return (size_t)(p - buf);
}
while (val > 0) {
tmp[i++] = (char)('0' + (val % 10));
val /= 10;
}
while (i > 0) {
*p++ = tmp[--i];
}
*p = '\0';
return (size_t)(p - buf);
}
#if !TG3JSON_USE_STDLIB_FPCONV
static size_t tg3json__utoa(uint64_t value, char *buf) {
char tmp[32];
size_t n = 0;
size_t i = 0;
if (value == 0) {
buf[0] = '0';
buf[1] = '\0';
return 1;
}
while (value > 0) {
tmp[n++] = (char)('0' + (value % 10u));
value /= 10u;
}
while (n > 0) buf[i++] = tmp[--n];
buf[i] = '\0';
return i;
}
#endif /* !TG3JSON_USE_STDLIB_FPCONV */
static uint64_t tg3json__double_bits(double v) {
uint64_t bits = 0;
TINYGLTF_JSON_MEMCPY(&bits, &v, sizeof(bits));
return bits;
}
static int tg3json__is_nan_bits(uint64_t bits) {
return ((bits & 0x7ff0000000000000ULL) == 0x7ff0000000000000ULL) &&
((bits & 0x000fffffffffffffULL) != 0);
}
static int tg3json__is_inf_bits(uint64_t bits) {
return (bits & 0x7fffffffffffffffULL) == 0x7ff0000000000000ULL;
}
#if !TG3JSON_USE_STDLIB_FPCONV
static double tg3json__double_from_bits(uint64_t bits) {
double v = 0.0;
TINYGLTF_JSON_MEMCPY(&v, &bits, sizeof(v));
return v;
}
static long double tg3json__pow10_ld(int exp10) {
long double v = 1.0L;
if (exp10 < 0) {
exp10 = -exp10;
while (exp10 >= 16) {
v *= 1.0e-16L;
exp10 -= 16;
}
while (exp10-- > 0) v *= 0.1L;
} else {
while (exp10 >= 16) {
v *= 1.0e16L;
exp10 -= 16;
}
while (exp10-- > 0) v *= 10.0L;
}
return v;
}
#endif
static int tg3json__parse_f64_c(const char *start, const char *end, double *out) {
#if TG3JSON_USE_STDLIB_FPCONV
size_t len = (size_t)(end - start);
char stack_buf[128];
char *buf = stack_buf;
char *parse_end = NULL;
double v;
if (len + 1 > sizeof(stack_buf)) {
buf = (char *)TINYGLTF_JSON_MALLOC(len + 1);
if (!buf) return 0;
}
if (len > 0) TINYGLTF_JSON_MEMCPY(buf, start, len);
buf[len] = '\0';
v = strtod(buf, &parse_end);
if (parse_end != buf + len || tg3json__is_inf_bits(tg3json__double_bits(v))) {
if (buf != stack_buf) TINYGLTF_JSON_FREE(buf);
return 0;
}
if (buf != stack_buf) TINYGLTF_JSON_FREE(buf);
*out = v;
return 1;
#else
const char *p = start;
int neg = 0;
int saw_digit = 0;
int nonzero_seen = 0;
int exp10 = 0;
uint64_t sig = 0;
int sig_digits = 0;
long double extra = 0.0L;
int extra_digits = 0;
int exp_sign = 1;
int exp_lit = 0;
long double v;
if (p < end && *p == '-') {
neg = 1;
++p;
}
if (p >= end) return 0;
if (*p == '0') {
saw_digit = 1;
++p;
} else if (*p >= '1' && *p <= '9') {
do {
int d = *p - '0';
saw_digit = 1;
nonzero_seen = 1;
if (sig_digits < 19) {
sig = sig * 10u + (uint64_t)d;
++sig_digits;
} else if (extra_digits < 64) {
extra = extra * 10.0L + (long double)d;
++extra_digits;
} else {
++exp10;
}
++p;
} while (p < end && *p >= '0' && *p <= '9');
} else {
return 0;
}
if (p < end && *p == '.') {
++p;
if (p >= end || *p < '0' || *p > '9') return 0;
do {
int d = *p - '0';
saw_digit = 1;
if (d != 0 || nonzero_seen) {
nonzero_seen = nonzero_seen || (d != 0);
if (sig_digits < 19) {
sig = sig * 10u + (uint64_t)d;
++sig_digits;
--exp10;
} else if (extra_digits < 64) {
extra = extra * 10.0L + (long double)d;
++extra_digits;
--exp10;
} else {
--exp10;
}
} else {
--exp10;
}
++p;
} while (p < end && *p >= '0' && *p <= '9');
}
if (!saw_digit) return 0;
if (p < end && (*p == 'e' || *p == 'E')) {
++p;
if (p < end && (*p == '+' || *p == '-')) {
exp_sign = (*p == '-') ? -1 : 1;
++p;
}
if (p >= end || *p < '0' || *p > '9') return 0;
while (p < end && *p >= '0' && *p <= '9') {
if (exp_lit < 10000) exp_lit = exp_lit * 10 + (*p - '0');
++p;
}
exp10 += exp_sign * exp_lit;
}
if (p != end) return 0;
if (!nonzero_seen || sig_digits == 0) {
*out = neg ? tg3json__double_from_bits(0x8000000000000000ULL) : 0.0;
return 1;
}
if (exp10 > 309) return 0;
if (exp10 < -4000) {
*out = neg ? tg3json__double_from_bits(0x8000000000000000ULL) : 0.0;
return 1;
}
v = (long double)sig;
if (extra_digits > 0) {
v = v * tg3json__pow10_ld(extra_digits) + extra;
}
if (exp10 != 0) v *= tg3json__pow10_ld(exp10);
if (neg) v = -v;
*out = (double)v;
if (tg3json__is_inf_bits(tg3json__double_bits(*out))) return 0;
return 1;
#endif
}
#if !TG3JSON_USE_STDLIB_FPCONV
static char *tg3json__write_exp(char *p, int exp10) {
char tmp[16];
size_t n;
*p++ = 'e';
if (exp10 < 0) {
*p++ = '-';
exp10 = -exp10;
}
n = tg3json__utoa((uint64_t)exp10, tmp);
TINYGLTF_JSON_MEMCPY(p, tmp, n);
return p + n;
}
static char *tg3json__format_decimal_digits(char *out, const char *digits,
int ndigits, int dec_exp,
int negative) {
char *p = out;
int i;
int output_exp = dec_exp + ndigits - 1;
if (negative) *p++ = '-';
if (output_exp < -4 || output_exp >= 16) {
*p++ = digits[0];
if (ndigits > 1) {
*p++ = '.';
for (i = 1; i < ndigits; ++i) *p++ = digits[i];
}
return tg3json__write_exp(p, output_exp);
}
if (dec_exp >= 0) {
for (i = 0; i < ndigits; ++i) *p++ = digits[i];
for (i = 0; i < dec_exp; ++i) *p++ = '0';
return p;
}
if (dec_exp + ndigits > 0) {
int int_digits = dec_exp + ndigits;
for (i = 0; i < int_digits; ++i) *p++ = digits[i];
*p++ = '.';
for (; i < ndigits; ++i) *p++ = digits[i];
return p;
}
*p++ = '0';
*p++ = '.';
for (i = 0; i < -(dec_exp + ndigits); ++i) *p++ = '0';
for (i = 0; i < ndigits; ++i) *p++ = digits[i];
return p;
}
#endif /* !TG3JSON_USE_STDLIB_FPCONV */
static int tg3json__same_f64(double a, double b) {
return tg3json__double_bits(a) == tg3json__double_bits(b);
}
#if TG3JSON_USE_STDLIB_FPCONV
static void tg3json__normalize_exponent(char *s) {
char *e = s;
char *dst;
int neg = 0;
while (*e && *e != 'e' && *e != 'E') ++e;
if (!*e) return;
*e++ = 'e';
dst = e;
if (*e == '+' || *e == '-') {
neg = (*e == '-');
++e;
}
while (*e == '0') ++e;
if (neg) *dst++ = '-';
if (!*e) {
*dst++ = '0';
} else {
while (*e) *dst++ = *e++;
}
*dst = '\0';
}
static void tg3json__expand_short_plain_decimal(char *s) {
char *p = s;
char *e;
char digits[32];
char out[96];
int negative = 0;
int ndigits = 0;
int int_digits = 0;
int exp10 = 0;
int new_point;
int i;
char *q = out;
if (*p == '-') {
negative = 1;
++p;
}
e = p;
while (*e && *e != 'e') ++e;
if (!*e) return;
for (; p < e; ++p) {
if (*p == '.') {
int_digits = ndigits;
} else {
if (ndigits < (int)(sizeof(digits) - 1)) digits[ndigits++] = *p;
}
}
if (int_digits == 0) int_digits = ndigits;
++e;
if (*e == '-') {
int sign = -1;
++e;
while (*e >= '0' && *e <= '9') exp10 = exp10 * 10 + (*e++ - '0');
exp10 *= sign;
} else {
while (*e >= '0' && *e <= '9') exp10 = exp10 * 10 + (*e++ - '0');
}
if (exp10 < -4 || exp10 >= 16) return;
new_point = int_digits + exp10;
if (negative) *q++ = '-';
if (new_point <= 0) {
*q++ = '0';
*q++ = '.';
for (i = 0; i < -new_point; ++i) *q++ = '0';
for (i = 0; i < ndigits; ++i) *q++ = digits[i];
} else if (new_point >= ndigits) {
for (i = 0; i < ndigits; ++i) *q++ = digits[i];
for (i = ndigits; i < new_point; ++i) *q++ = '0';
} else {
for (i = 0; i < new_point; ++i) *q++ = digits[i];
*q++ = '.';
for (; i < ndigits; ++i) *q++ = digits[i];
}
*q = '\0';
if (q > out) {
char *dot = out;
while (*dot && *dot != '.') ++dot;
if (*dot == '.') {
char *end = q - 1;
while (end > dot && *end == '0') *end-- = '\0';
if (end == dot) *end = '\0';
}
}
TINYGLTF_JSON_MEMCPY(s, out, (size_t)(TINYGLTF_JSON_STRLEN(out) + 1));
}
#endif
static char *tg3json__dtoa_c(double value, char *buf) {
uint64_t bits = tg3json__double_bits(value);
int negative = (int)(bits >> 63);
uint64_t abits = bits & 0x7fffffffffffffffULL;
#if !TG3JSON_USE_STDLIB_FPCONV
long double x;
int dec_e = 0;
char digits[24];
int ndigits = 17;
int i;
char best[80];
size_t best_len;
#endif
if (tg3json__is_nan_bits(bits)) {
TINYGLTF_JSON_MEMCPY(buf, "nan", 3);
return buf + 3;
}
if (tg3json__is_inf_bits(bits)) {
if (negative) {
TINYGLTF_JSON_MEMCPY(buf, "-inf", 4);
return buf + 4;
}
TINYGLTF_JSON_MEMCPY(buf, "inf", 3);
return buf + 3;
}
if (abits == 0) {
*buf++ = '0';
return buf;
}
if (bits == 0x3ff0000000000000ULL) {
*buf++ = '1';
return buf;
}
if (bits == 0xbff0000000000000ULL) {
*buf++ = '-';
*buf++ = '1';
return buf;
}
#if TG3JSON_USE_STDLIB_FPCONV
{
int precision;
char candidate[96];
char shortest[96];
shortest[0] = '\0';
for (precision = 1; precision <= 17; ++precision) {
char fmt[8];
double parsed = 0.0;
int n;
n = snprintf(fmt, sizeof(fmt), "%%.%dg", precision);
if (n <= 0 || n >= (int)sizeof(fmt)) continue;
n = snprintf(candidate, sizeof(candidate), fmt, value);
if (n <= 0 || n >= (int)sizeof(candidate)) continue;
tg3json__normalize_exponent(candidate);
tg3json__expand_short_plain_decimal(candidate);
if (tg3json__parse_f64_c(candidate, candidate + TINYGLTF_JSON_STRLEN(candidate), &parsed) &&
tg3json__same_f64(parsed, value)) {
TINYGLTF_JSON_MEMCPY(shortest, candidate, TINYGLTF_JSON_STRLEN(candidate) + 1);
break;
}
}
if (!shortest[0]) {
int n = snprintf(shortest, sizeof(shortest), "%.17g", value);
if (n <= 0 || n >= (int)sizeof(shortest)) return buf;
tg3json__normalize_exponent(shortest);
tg3json__expand_short_plain_decimal(shortest);
}
TINYGLTF_JSON_MEMCPY(buf, shortest, TINYGLTF_JSON_STRLEN(shortest));
return buf + TINYGLTF_JSON_STRLEN(shortest);
}
#else
x = negative ? -(long double)value : (long double)value;
while (x >= 1.0e16L) {
x *= 1.0e-16L;
dec_e += 16;
}
while (x >= 10.0L) {
x *= 0.1L;
++dec_e;
}
while (x < 1.0L) {
x *= 10.0L;
--dec_e;
}
for (i = 0; i < 18; ++i) {
int d = (int)x;
if (d < 0) d = 0;
if (d > 9) d = 9;
digits[i] = (char)('0' + d);
x = (x - (long double)d) * 10.0L;
}
if (digits[17] >= '5') {
int carry = 1;
for (i = 16; i >= 0 && carry; --i) {
if (digits[i] == '9') {
digits[i] = '0';
} else {
digits[i]++;
carry = 0;
}
}
if (carry) {
digits[0] = '1';
for (i = 1; i < 17; ++i) digits[i] = '0';
++dec_e;
}
}
while (ndigits > 1 && digits[ndigits - 1] == '0') --ndigits;
{
char *end = tg3json__format_decimal_digits(best, digits, ndigits,
dec_e - ndigits + 1,
negative);
*end = '\0';
best_len = (size_t)(end - best);
}
for (i = ndigits - 1; i >= 1; --i) {
char candidate[80];
char *end;
double parsed = 0.0;
end = tg3json__format_decimal_digits(candidate, digits, i,
dec_e - i + 1, negative);
*end = '\0';
if (tg3json__parse_f64_c(candidate, end, &parsed) &&
tg3json__same_f64(parsed, value)) {
TINYGLTF_JSON_MEMCPY(best, candidate, (size_t)(end - candidate) + 1);
best_len = (size_t)(end - candidate);
} else {
break;
}
}
TINYGLTF_JSON_MEMCPY(buf, best, best_len);
return buf + best_len;
#endif
}
typedef struct tg3json__parser {
const char *cur;
const char *end;
const char *error;
size_t depth_limit;
size_t memory_budget;
size_t max_single_alloc;
size_t max_string_length;
size_t allocated;
int parse_float32;
} tg3json__parser;
typedef struct tg3json__buffer {
tg3json__parser *parser;
char *data;
size_t len;
size_t cap;
} tg3json__buffer;
static void tg3json__init_value(tg3json_value *value) {
if (!value) return;
TINYGLTF_JSON_MEMSET(value, 0, sizeof(*value));
value->type = TG3JSON_NULL;
}
static char *tg3json__strndup_local(const char *src, size_t len) {
char *dst = (char *)TINYGLTF_JSON_MALLOC(len + 1);
if (!dst) return NULL;
if (len > 0) TINYGLTF_JSON_MEMCPY(dst, src, len);
dst[len] = '\0';
return dst;
}
static void *tg3json__parser_alloc(tg3json__parser *parser, size_t size) {
void *ptr;
if (!parser) return TINYGLTF_JSON_MALLOC(size);
if (parser->max_single_alloc && size > parser->max_single_alloc) return NULL;
if (parser->memory_budget &&
(size > parser->memory_budget || parser->allocated > parser->memory_budget - size)) {
return NULL;
}
ptr = TINYGLTF_JSON_MALLOC(size);
if (!ptr) return NULL;
parser->allocated += size;
return ptr;
}
static int tg3json__reserve_bytes(void **ptr, size_t elem_size,
size_t needed, size_t *capacity) {
void *new_ptr;
size_t new_cap;
if (needed <= *capacity) return 1;
new_cap = (*capacity > 0) ? *capacity : 8;
while (new_cap < needed) {
if (new_cap > ((size_t)-1) / 2) {
new_cap = needed;
break;
}
new_cap *= 2;
}
if (elem_size != 0 && new_cap > ((size_t)-1) / elem_size) return 0;
new_ptr = TINYGLTF_JSON_REALLOC(*ptr, elem_size * new_cap);
if (!new_ptr) return 0;
*ptr = new_ptr;
*capacity = new_cap;
return 1;
}
static int tg3json__reserve_bytes_parser(tg3json__parser *parser, void **ptr,
size_t elem_size, size_t needed,
size_t *capacity) {
void *new_ptr;
size_t new_cap;
size_t old_bytes;
size_t new_bytes;
if (needed <= *capacity) return 1;
new_cap = (*capacity > 0) ? *capacity : 8;
while (new_cap < needed) {
if (new_cap > ((size_t)-1) / 2) {
new_cap = needed;
break;
}
new_cap *= 2;
}
if (elem_size != 0 && new_cap > ((size_t)-1) / elem_size) return 0;
old_bytes = elem_size * (*capacity);
new_bytes = elem_size * new_cap;
if (parser) {
size_t delta = (new_bytes > old_bytes) ? (new_bytes - old_bytes) : 0;
if (parser->max_single_alloc && new_bytes > parser->max_single_alloc) return 0;
if (parser->memory_budget && delta > 0 &&
(delta > parser->memory_budget ||
parser->allocated > parser->memory_budget - delta)) {
return 0;
}
}
new_ptr = TINYGLTF_JSON_REALLOC(*ptr, new_bytes);
if (!new_ptr) return 0;
if (parser && new_bytes > old_bytes) parser->allocated += new_bytes - old_bytes;
*ptr = new_ptr;
*capacity = new_cap;
return 1;
}
static const char *tg3json__skip_ws(const char *p, const char *end) {
while (p < end) {
unsigned char c = (unsigned char)*p;
if (c != ' ' && c != '\n' && c != '\r' && c != '\t') break;
++p;
}
return p;
}
static void tg3json__set_error(tg3json__parser *parser, const char *pos) {
if (!parser->error) parser->error = pos;
}
static int tg3json__buf_append(tg3json__buffer *buf, const char *src, size_t len) {
if (len == 0) return 1;
if (!tg3json__reserve_bytes_parser(buf->parser, (void **)&buf->data, 1,
buf->len + len + 1, &buf->cap)) {
return 0;
}
TINYGLTF_JSON_MEMCPY(buf->data + buf->len, src, len);
buf->len += len;
buf->data[buf->len] = '\0';
return 1;
}
static int tg3json__buf_putc(tg3json__buffer *buf, char c) {
if (!tg3json__reserve_bytes_parser(buf->parser, (void **)&buf->data, 1,
buf->len + 2, &buf->cap)) {
return 0;
}
buf->data[buf->len++] = c;
buf->data[buf->len] = '\0';
return 1;
}
static int tg3json__hex4(const char *p, uint32_t *out) {
uint32_t value = 0;
size_t i;
for (i = 0; i < 4; ++i) {
unsigned char c = (unsigned char)p[i];
value <<= 4;
if (c >= '0' && c <= '9') value |= (uint32_t)(c - '0');
else if (c >= 'a' && c <= 'f') value |= (uint32_t)(10 + c - 'a');
else if (c >= 'A' && c <= 'F') value |= (uint32_t)(10 + c - 'A');
else return 0;
}
*out = value;
return 1;
}
static int tg3json__append_utf8(tg3json__buffer *buf, uint32_t cp) {
char tmp[4];
size_t len = 0;
if (cp <= 0x7Fu) {
tmp[len++] = (char)cp;
} else if (cp <= 0x7FFu) {
tmp[len++] = (char)(0xC0u | ((cp >> 6) & 0x1Fu));
tmp[len++] = (char)(0x80u | (cp & 0x3Fu));
} else if (cp <= 0xFFFFu) {
tmp[len++] = (char)(0xE0u | ((cp >> 12) & 0x0Fu));
tmp[len++] = (char)(0x80u | ((cp >> 6) & 0x3Fu));
tmp[len++] = (char)(0x80u | (cp & 0x3Fu));
} else if (cp <= 0x10FFFFu) {
tmp[len++] = (char)(0xF0u | ((cp >> 18) & 0x07u));
tmp[len++] = (char)(0x80u | ((cp >> 12) & 0x3Fu));
tmp[len++] = (char)(0x80u | ((cp >> 6) & 0x3Fu));
tmp[len++] = (char)(0x80u | (cp & 0x3Fu));
} else {
return 0;
}
return tg3json__buf_append(buf, tmp, len);
}
static int tg3json__parse_string_raw(tg3json__parser *parser,
char **out_str, size_t *out_len) {
tg3json__buffer buf;
const char *start;
TINYGLTF_JSON_MEMSET(&buf, 0, sizeof(buf));
buf.parser = parser;
if (parser->cur >= parser->end || *parser->cur != '"') {
tg3json__set_error(parser, parser->cur);
return 0;
}
++parser->cur;
start = parser->cur;
while (parser->cur < parser->end) {
unsigned char c = (unsigned char)*parser->cur;
if (c == '"') {
size_t final_len = buf.len + (size_t)(parser->cur - start);
if (parser->max_string_length && final_len > parser->max_string_length) goto oom;
if (!tg3json__buf_append(&buf, start, (size_t)(parser->cur - start))) goto oom;
++parser->cur;
*out_str = buf.data;
*out_len = buf.len;
return 1;
}
if (c == '\\') {
uint32_t codepoint;
size_t pending_len = (size_t)(parser->cur - start);
if (parser->max_string_length && buf.len + pending_len > parser->max_string_length) goto oom;
if (!tg3json__buf_append(&buf, start, (size_t)(parser->cur - start))) goto oom;
++parser->cur;
if (parser->cur >= parser->end) break;
switch (*parser->cur) {
case '"': if (!tg3json__buf_putc(&buf, '"')) goto oom; break;
case '\\': if (!tg3json__buf_putc(&buf, '\\')) goto oom; break;
case '/': if (!tg3json__buf_putc(&buf, '/')) goto oom; break;
case 'b': if (!tg3json__buf_putc(&buf, '\b')) goto oom; break;
case 'f': if (!tg3json__buf_putc(&buf, '\f')) goto oom; break;
case 'n': if (!tg3json__buf_putc(&buf, '\n')) goto oom; break;
case 'r': if (!tg3json__buf_putc(&buf, '\r')) goto oom; break;
case 't': if (!tg3json__buf_putc(&buf, '\t')) goto oom; break;
case 'u': {
if ((size_t)(parser->end - parser->cur) < 5) break;
if (!tg3json__hex4(parser->cur + 1, &codepoint)) break;
parser->cur += 4;
if (codepoint >= 0xD800u && codepoint <= 0xDBFFu) {
uint32_t low;
if ((size_t)(parser->end - parser->cur) < 7 || parser->cur[1] != '\\' || parser->cur[2] != 'u') break;
if (!tg3json__hex4(parser->cur + 3, &low)) break;
if (low < 0xDC00u || low > 0xDFFFu) break;
codepoint = 0x10000u + (((codepoint - 0xD800u) << 10) | (low - 0xDC00u));
parser->cur += 6;
} else if (codepoint >= 0xDC00u && codepoint <= 0xDFFFu) {
break;
}
if (!tg3json__append_utf8(&buf, codepoint)) goto oom;
break;
}
default:
tg3json__set_error(parser, parser->cur);
TINYGLTF_JSON_FREE(buf.data);
return 0;
}
++parser->cur;
start = parser->cur;
continue;
}
if (c < 0x20u) break;
++parser->cur;
}
tg3json__set_error(parser, parser->cur);
TINYGLTF_JSON_FREE(buf.data);
return 0;
oom:
tg3json__set_error(parser, parser->cur);
TINYGLTF_JSON_FREE(buf.data);
return 0;
}
static int tg3json__parse_value(tg3json__parser *parser, size_t depth,
tg3json_value *out_value);
static int tg3json__parse_int64_span(const char *start, const char *end,
int64_t *out) {
const char *p = start;
uint64_t value = 0;
uint64_t limit = (uint64_t)INT64_MAX;
int neg = 0;
if (p < end && *p == '-') {
neg = 1;
limit += 1u;
++p;
}
if (p >= end) return 0;
while (p < end) {
unsigned digit = (unsigned)(*p - '0');
if (digit > 9u) return 0;
if (value > (limit - digit) / 10u) return 0;